xfs: reflect sb features in xfs_mount
[linux-block.git] / fs / xfs / xfs_inode.c
CommitLineData
0b61f8a4 1// SPDX-License-Identifier: GPL-2.0
1da177e4 2/*
3e57ecf6 3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
7b718769 4 * All Rights Reserved.
1da177e4 5 */
f0e28280 6#include <linux/iversion.h>
40ebd81d 7
1da177e4 8#include "xfs.h"
a844f451 9#include "xfs_fs.h"
70a9883c 10#include "xfs_shared.h"
239880ef
DC
11#include "xfs_format.h"
12#include "xfs_log_format.h"
13#include "xfs_trans_resv.h"
1da177e4 14#include "xfs_mount.h"
3ab78df2 15#include "xfs_defer.h"
a4fbe6ab 16#include "xfs_inode.h"
c24b5dfa 17#include "xfs_dir2.h"
c24b5dfa 18#include "xfs_attr.h"
239880ef
DC
19#include "xfs_trans_space.h"
20#include "xfs_trans.h"
1da177e4 21#include "xfs_buf_item.h"
a844f451 22#include "xfs_inode_item.h"
a844f451
NS
23#include "xfs_ialloc.h"
24#include "xfs_bmap.h"
68988114 25#include "xfs_bmap_util.h"
e9e899a2 26#include "xfs_errortag.h"
1da177e4 27#include "xfs_error.h"
1da177e4 28#include "xfs_quota.h"
2a82b8be 29#include "xfs_filestream.h"
0b1b213f 30#include "xfs_trace.h"
33479e05 31#include "xfs_icache.h"
c24b5dfa 32#include "xfs_symlink.h"
239880ef
DC
33#include "xfs_trans_priv.h"
34#include "xfs_log.h"
a4fbe6ab 35#include "xfs_bmap_btree.h"
aa8968f2 36#include "xfs_reflink.h"
9bbafc71 37#include "xfs_ag.h"
1da177e4 38
1da177e4 39kmem_zone_t *xfs_inode_zone;
1da177e4
LT
40
41/*
8f04c47a 42 * Used in xfs_itruncate_extents(). This is the maximum number of extents
1da177e4
LT
43 * freed from a file in a single transaction.
44 */
45#define XFS_ITRUNC_MAX_EXTENTS 2
46
54d7b5c1 47STATIC int xfs_iunlink(struct xfs_trans *, struct xfs_inode *);
f40aadb2
DC
48STATIC int xfs_iunlink_remove(struct xfs_trans *tp, struct xfs_perag *pag,
49 struct xfs_inode *);
ab297431 50
2a0ec1d9
DC
51/*
52 * helper function to extract extent size hint from inode
53 */
54xfs_extlen_t
55xfs_get_extsz_hint(
56 struct xfs_inode *ip)
57{
bdb2ed2d
CH
58 /*
59 * No point in aligning allocations if we need to COW to actually
60 * write to them.
61 */
62 if (xfs_is_always_cow_inode(ip))
63 return 0;
db07349d 64 if ((ip->i_diflags & XFS_DIFLAG_EXTSIZE) && ip->i_extsize)
031474c2 65 return ip->i_extsize;
2a0ec1d9
DC
66 if (XFS_IS_REALTIME_INODE(ip))
67 return ip->i_mount->m_sb.sb_rextsize;
68 return 0;
69}
70
f7ca3522
DW
71/*
72 * Helper function to extract CoW extent size hint from inode.
73 * Between the extent size hint and the CoW extent size hint, we
e153aa79
DW
74 * return the greater of the two. If the value is zero (automatic),
75 * use the default size.
f7ca3522
DW
76 */
77xfs_extlen_t
78xfs_get_cowextsz_hint(
79 struct xfs_inode *ip)
80{
81 xfs_extlen_t a, b;
82
83 a = 0;
3e09ab8f 84 if (ip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE)
b33ce57d 85 a = ip->i_cowextsize;
f7ca3522
DW
86 b = xfs_get_extsz_hint(ip);
87
e153aa79
DW
88 a = max(a, b);
89 if (a == 0)
90 return XFS_DEFAULT_COWEXTSZ_HINT;
91 return a;
f7ca3522
DW
92}
93
fa96acad 94/*
efa70be1
CH
95 * These two are wrapper routines around the xfs_ilock() routine used to
96 * centralize some grungy code. They are used in places that wish to lock the
97 * inode solely for reading the extents. The reason these places can't just
98 * call xfs_ilock(ip, XFS_ILOCK_SHARED) is that the inode lock also guards to
99 * bringing in of the extents from disk for a file in b-tree format. If the
100 * inode is in b-tree format, then we need to lock the inode exclusively until
101 * the extents are read in. Locking it exclusively all the time would limit
102 * our parallelism unnecessarily, though. What we do instead is check to see
103 * if the extents have been read in yet, and only lock the inode exclusively
104 * if they have not.
fa96acad 105 *
efa70be1 106 * The functions return a value which should be given to the corresponding
01f4f327 107 * xfs_iunlock() call.
fa96acad
DC
108 */
109uint
309ecac8
CH
110xfs_ilock_data_map_shared(
111 struct xfs_inode *ip)
fa96acad 112{
309ecac8 113 uint lock_mode = XFS_ILOCK_SHARED;
fa96acad 114
b2197a36 115 if (xfs_need_iread_extents(&ip->i_df))
fa96acad 116 lock_mode = XFS_ILOCK_EXCL;
fa96acad 117 xfs_ilock(ip, lock_mode);
fa96acad
DC
118 return lock_mode;
119}
120
efa70be1
CH
121uint
122xfs_ilock_attr_map_shared(
123 struct xfs_inode *ip)
fa96acad 124{
efa70be1
CH
125 uint lock_mode = XFS_ILOCK_SHARED;
126
b2197a36 127 if (ip->i_afp && xfs_need_iread_extents(ip->i_afp))
efa70be1
CH
128 lock_mode = XFS_ILOCK_EXCL;
129 xfs_ilock(ip, lock_mode);
130 return lock_mode;
fa96acad
DC
131}
132
133/*
65523218
CH
134 * In addition to i_rwsem in the VFS inode, the xfs inode contains 2
135 * multi-reader locks: i_mmap_lock and the i_lock. This routine allows
136 * various combinations of the locks to be obtained.
fa96acad 137 *
653c60b6
DC
138 * The 3 locks should always be ordered so that the IO lock is obtained first,
139 * the mmap lock second and the ilock last in order to prevent deadlock.
fa96acad 140 *
653c60b6
DC
141 * Basic locking order:
142 *
65523218 143 * i_rwsem -> i_mmap_lock -> page_lock -> i_ilock
653c60b6 144 *
c1e8d7c6 145 * mmap_lock locking order:
653c60b6 146 *
c1e8d7c6
ML
147 * i_rwsem -> page lock -> mmap_lock
148 * mmap_lock -> i_mmap_lock -> page_lock
653c60b6 149 *
c1e8d7c6 150 * The difference in mmap_lock locking order mean that we cannot hold the
653c60b6 151 * i_mmap_lock over syscall based read(2)/write(2) based IO. These IO paths can
c1e8d7c6 152 * fault in pages during copy in/out (for buffered IO) or require the mmap_lock
653c60b6 153 * in get_user_pages() to map the user pages into the kernel address space for
65523218 154 * direct IO. Similarly the i_rwsem cannot be taken inside a page fault because
c1e8d7c6 155 * page faults already hold the mmap_lock.
653c60b6
DC
156 *
157 * Hence to serialise fully against both syscall and mmap based IO, we need to
65523218 158 * take both the i_rwsem and the i_mmap_lock. These locks should *only* be both
653c60b6
DC
159 * taken in places where we need to invalidate the page cache in a race
160 * free manner (e.g. truncate, hole punch and other extent manipulation
161 * functions).
fa96acad
DC
162 */
163void
164xfs_ilock(
165 xfs_inode_t *ip,
166 uint lock_flags)
167{
168 trace_xfs_ilock(ip, lock_flags, _RET_IP_);
169
170 /*
171 * You can't set both SHARED and EXCL for the same lock,
172 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
173 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
174 */
175 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
176 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
653c60b6
DC
177 ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
178 (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
fa96acad
DC
179 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
180 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
0952c818 181 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
fa96acad 182
65523218
CH
183 if (lock_flags & XFS_IOLOCK_EXCL) {
184 down_write_nested(&VFS_I(ip)->i_rwsem,
185 XFS_IOLOCK_DEP(lock_flags));
186 } else if (lock_flags & XFS_IOLOCK_SHARED) {
187 down_read_nested(&VFS_I(ip)->i_rwsem,
188 XFS_IOLOCK_DEP(lock_flags));
189 }
fa96acad 190
653c60b6
DC
191 if (lock_flags & XFS_MMAPLOCK_EXCL)
192 mrupdate_nested(&ip->i_mmaplock, XFS_MMAPLOCK_DEP(lock_flags));
193 else if (lock_flags & XFS_MMAPLOCK_SHARED)
194 mraccess_nested(&ip->i_mmaplock, XFS_MMAPLOCK_DEP(lock_flags));
195
fa96acad
DC
196 if (lock_flags & XFS_ILOCK_EXCL)
197 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
198 else if (lock_flags & XFS_ILOCK_SHARED)
199 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
200}
201
202/*
203 * This is just like xfs_ilock(), except that the caller
204 * is guaranteed not to sleep. It returns 1 if it gets
205 * the requested locks and 0 otherwise. If the IO lock is
206 * obtained but the inode lock cannot be, then the IO lock
207 * is dropped before returning.
208 *
209 * ip -- the inode being locked
210 * lock_flags -- this parameter indicates the inode's locks to be
211 * to be locked. See the comment for xfs_ilock() for a list
212 * of valid values.
213 */
214int
215xfs_ilock_nowait(
216 xfs_inode_t *ip,
217 uint lock_flags)
218{
219 trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_);
220
221 /*
222 * You can't set both SHARED and EXCL for the same lock,
223 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
224 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
225 */
226 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
227 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
653c60b6
DC
228 ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
229 (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
fa96acad
DC
230 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
231 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
0952c818 232 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
fa96acad
DC
233
234 if (lock_flags & XFS_IOLOCK_EXCL) {
65523218 235 if (!down_write_trylock(&VFS_I(ip)->i_rwsem))
fa96acad
DC
236 goto out;
237 } else if (lock_flags & XFS_IOLOCK_SHARED) {
65523218 238 if (!down_read_trylock(&VFS_I(ip)->i_rwsem))
fa96acad
DC
239 goto out;
240 }
653c60b6
DC
241
242 if (lock_flags & XFS_MMAPLOCK_EXCL) {
243 if (!mrtryupdate(&ip->i_mmaplock))
244 goto out_undo_iolock;
245 } else if (lock_flags & XFS_MMAPLOCK_SHARED) {
246 if (!mrtryaccess(&ip->i_mmaplock))
247 goto out_undo_iolock;
248 }
249
fa96acad
DC
250 if (lock_flags & XFS_ILOCK_EXCL) {
251 if (!mrtryupdate(&ip->i_lock))
653c60b6 252 goto out_undo_mmaplock;
fa96acad
DC
253 } else if (lock_flags & XFS_ILOCK_SHARED) {
254 if (!mrtryaccess(&ip->i_lock))
653c60b6 255 goto out_undo_mmaplock;
fa96acad
DC
256 }
257 return 1;
258
653c60b6
DC
259out_undo_mmaplock:
260 if (lock_flags & XFS_MMAPLOCK_EXCL)
261 mrunlock_excl(&ip->i_mmaplock);
262 else if (lock_flags & XFS_MMAPLOCK_SHARED)
263 mrunlock_shared(&ip->i_mmaplock);
264out_undo_iolock:
fa96acad 265 if (lock_flags & XFS_IOLOCK_EXCL)
65523218 266 up_write(&VFS_I(ip)->i_rwsem);
fa96acad 267 else if (lock_flags & XFS_IOLOCK_SHARED)
65523218 268 up_read(&VFS_I(ip)->i_rwsem);
653c60b6 269out:
fa96acad
DC
270 return 0;
271}
272
273/*
274 * xfs_iunlock() is used to drop the inode locks acquired with
275 * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
276 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
277 * that we know which locks to drop.
278 *
279 * ip -- the inode being unlocked
280 * lock_flags -- this parameter indicates the inode's locks to be
281 * to be unlocked. See the comment for xfs_ilock() for a list
282 * of valid values for this parameter.
283 *
284 */
285void
286xfs_iunlock(
287 xfs_inode_t *ip,
288 uint lock_flags)
289{
290 /*
291 * You can't set both SHARED and EXCL for the same lock,
292 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
293 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
294 */
295 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
296 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
653c60b6
DC
297 ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
298 (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
fa96acad
DC
299 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
300 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
0952c818 301 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
fa96acad
DC
302 ASSERT(lock_flags != 0);
303
304 if (lock_flags & XFS_IOLOCK_EXCL)
65523218 305 up_write(&VFS_I(ip)->i_rwsem);
fa96acad 306 else if (lock_flags & XFS_IOLOCK_SHARED)
65523218 307 up_read(&VFS_I(ip)->i_rwsem);
fa96acad 308
653c60b6
DC
309 if (lock_flags & XFS_MMAPLOCK_EXCL)
310 mrunlock_excl(&ip->i_mmaplock);
311 else if (lock_flags & XFS_MMAPLOCK_SHARED)
312 mrunlock_shared(&ip->i_mmaplock);
313
fa96acad
DC
314 if (lock_flags & XFS_ILOCK_EXCL)
315 mrunlock_excl(&ip->i_lock);
316 else if (lock_flags & XFS_ILOCK_SHARED)
317 mrunlock_shared(&ip->i_lock);
318
319 trace_xfs_iunlock(ip, lock_flags, _RET_IP_);
320}
321
322/*
323 * give up write locks. the i/o lock cannot be held nested
324 * if it is being demoted.
325 */
326void
327xfs_ilock_demote(
328 xfs_inode_t *ip,
329 uint lock_flags)
330{
653c60b6
DC
331 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL));
332 ASSERT((lock_flags &
333 ~(XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
fa96acad
DC
334
335 if (lock_flags & XFS_ILOCK_EXCL)
336 mrdemote(&ip->i_lock);
653c60b6
DC
337 if (lock_flags & XFS_MMAPLOCK_EXCL)
338 mrdemote(&ip->i_mmaplock);
fa96acad 339 if (lock_flags & XFS_IOLOCK_EXCL)
65523218 340 downgrade_write(&VFS_I(ip)->i_rwsem);
fa96acad
DC
341
342 trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_);
343}
344
742ae1e3 345#if defined(DEBUG) || defined(XFS_WARN)
fa96acad
DC
346int
347xfs_isilocked(
348 xfs_inode_t *ip,
349 uint lock_flags)
350{
351 if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) {
352 if (!(lock_flags & XFS_ILOCK_SHARED))
353 return !!ip->i_lock.mr_writer;
354 return rwsem_is_locked(&ip->i_lock.mr_lock);
355 }
356
653c60b6
DC
357 if (lock_flags & (XFS_MMAPLOCK_EXCL|XFS_MMAPLOCK_SHARED)) {
358 if (!(lock_flags & XFS_MMAPLOCK_SHARED))
359 return !!ip->i_mmaplock.mr_writer;
360 return rwsem_is_locked(&ip->i_mmaplock.mr_lock);
361 }
362
fa96acad
DC
363 if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) {
364 if (!(lock_flags & XFS_IOLOCK_SHARED))
65523218
CH
365 return !debug_locks ||
366 lockdep_is_held_type(&VFS_I(ip)->i_rwsem, 0);
367 return rwsem_is_locked(&VFS_I(ip)->i_rwsem);
fa96acad
DC
368 }
369
370 ASSERT(0);
371 return 0;
372}
373#endif
374
b6a9947e
DC
375/*
376 * xfs_lockdep_subclass_ok() is only used in an ASSERT, so is only called when
377 * DEBUG or XFS_WARN is set. And MAX_LOCKDEP_SUBCLASSES is then only defined
378 * when CONFIG_LOCKDEP is set. Hence the complex define below to avoid build
379 * errors and warnings.
380 */
381#if (defined(DEBUG) || defined(XFS_WARN)) && defined(CONFIG_LOCKDEP)
3403ccc0
DC
382static bool
383xfs_lockdep_subclass_ok(
384 int subclass)
385{
386 return subclass < MAX_LOCKDEP_SUBCLASSES;
387}
388#else
389#define xfs_lockdep_subclass_ok(subclass) (true)
390#endif
391
c24b5dfa 392/*
653c60b6 393 * Bump the subclass so xfs_lock_inodes() acquires each lock with a different
0952c818
DC
394 * value. This can be called for any type of inode lock combination, including
395 * parent locking. Care must be taken to ensure we don't overrun the subclass
396 * storage fields in the class mask we build.
c24b5dfa
DC
397 */
398static inline int
399xfs_lock_inumorder(int lock_mode, int subclass)
400{
0952c818
DC
401 int class = 0;
402
403 ASSERT(!(lock_mode & (XFS_ILOCK_PARENT | XFS_ILOCK_RTBITMAP |
404 XFS_ILOCK_RTSUM)));
3403ccc0 405 ASSERT(xfs_lockdep_subclass_ok(subclass));
0952c818 406
653c60b6 407 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) {
0952c818 408 ASSERT(subclass <= XFS_IOLOCK_MAX_SUBCLASS);
0952c818 409 class += subclass << XFS_IOLOCK_SHIFT;
653c60b6
DC
410 }
411
412 if (lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) {
0952c818
DC
413 ASSERT(subclass <= XFS_MMAPLOCK_MAX_SUBCLASS);
414 class += subclass << XFS_MMAPLOCK_SHIFT;
653c60b6
DC
415 }
416
0952c818
DC
417 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) {
418 ASSERT(subclass <= XFS_ILOCK_MAX_SUBCLASS);
419 class += subclass << XFS_ILOCK_SHIFT;
420 }
c24b5dfa 421
0952c818 422 return (lock_mode & ~XFS_LOCK_SUBCLASS_MASK) | class;
c24b5dfa
DC
423}
424
425/*
95afcf5c
DC
426 * The following routine will lock n inodes in exclusive mode. We assume the
427 * caller calls us with the inodes in i_ino order.
c24b5dfa 428 *
95afcf5c
DC
429 * We need to detect deadlock where an inode that we lock is in the AIL and we
430 * start waiting for another inode that is locked by a thread in a long running
431 * transaction (such as truncate). This can result in deadlock since the long
432 * running trans might need to wait for the inode we just locked in order to
433 * push the tail and free space in the log.
0952c818
DC
434 *
435 * xfs_lock_inodes() can only be used to lock one type of lock at a time -
436 * the iolock, the mmaplock or the ilock, but not more than one at a time. If we
437 * lock more than one at a time, lockdep will report false positives saying we
438 * have violated locking orders.
c24b5dfa 439 */
0d5a75e9 440static void
c24b5dfa 441xfs_lock_inodes(
efe2330f
CH
442 struct xfs_inode **ips,
443 int inodes,
444 uint lock_mode)
c24b5dfa 445{
efe2330f
CH
446 int attempts = 0, i, j, try_lock;
447 struct xfs_log_item *lp;
c24b5dfa 448
0952c818
DC
449 /*
450 * Currently supports between 2 and 5 inodes with exclusive locking. We
451 * support an arbitrary depth of locking here, but absolute limits on
b63da6c8 452 * inodes depend on the type of locking and the limits placed by
0952c818
DC
453 * lockdep annotations in xfs_lock_inumorder. These are all checked by
454 * the asserts.
455 */
95afcf5c 456 ASSERT(ips && inodes >= 2 && inodes <= 5);
0952c818
DC
457 ASSERT(lock_mode & (XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL |
458 XFS_ILOCK_EXCL));
459 ASSERT(!(lock_mode & (XFS_IOLOCK_SHARED | XFS_MMAPLOCK_SHARED |
460 XFS_ILOCK_SHARED)));
0952c818
DC
461 ASSERT(!(lock_mode & XFS_MMAPLOCK_EXCL) ||
462 inodes <= XFS_MMAPLOCK_MAX_SUBCLASS + 1);
463 ASSERT(!(lock_mode & XFS_ILOCK_EXCL) ||
464 inodes <= XFS_ILOCK_MAX_SUBCLASS + 1);
465
466 if (lock_mode & XFS_IOLOCK_EXCL) {
467 ASSERT(!(lock_mode & (XFS_MMAPLOCK_EXCL | XFS_ILOCK_EXCL)));
468 } else if (lock_mode & XFS_MMAPLOCK_EXCL)
469 ASSERT(!(lock_mode & XFS_ILOCK_EXCL));
c24b5dfa
DC
470
471 try_lock = 0;
472 i = 0;
c24b5dfa
DC
473again:
474 for (; i < inodes; i++) {
475 ASSERT(ips[i]);
476
95afcf5c 477 if (i && (ips[i] == ips[i - 1])) /* Already locked */
c24b5dfa
DC
478 continue;
479
480 /*
95afcf5c
DC
481 * If try_lock is not set yet, make sure all locked inodes are
482 * not in the AIL. If any are, set try_lock to be used later.
c24b5dfa 483 */
c24b5dfa
DC
484 if (!try_lock) {
485 for (j = (i - 1); j >= 0 && !try_lock; j--) {
b3b14aac 486 lp = &ips[j]->i_itemp->ili_item;
22525c17 487 if (lp && test_bit(XFS_LI_IN_AIL, &lp->li_flags))
c24b5dfa 488 try_lock++;
c24b5dfa
DC
489 }
490 }
491
492 /*
493 * If any of the previous locks we have locked is in the AIL,
494 * we must TRY to get the second and subsequent locks. If
495 * we can't get any, we must release all we have
496 * and try again.
497 */
95afcf5c
DC
498 if (!try_lock) {
499 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
500 continue;
501 }
502
503 /* try_lock means we have an inode locked that is in the AIL. */
504 ASSERT(i != 0);
505 if (xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i)))
506 continue;
c24b5dfa 507
95afcf5c
DC
508 /*
509 * Unlock all previous guys and try again. xfs_iunlock will try
510 * to push the tail if the inode is in the AIL.
511 */
512 attempts++;
513 for (j = i - 1; j >= 0; j--) {
c24b5dfa 514 /*
95afcf5c
DC
515 * Check to see if we've already unlocked this one. Not
516 * the first one going back, and the inode ptr is the
517 * same.
c24b5dfa 518 */
95afcf5c
DC
519 if (j != (i - 1) && ips[j] == ips[j + 1])
520 continue;
c24b5dfa 521
95afcf5c
DC
522 xfs_iunlock(ips[j], lock_mode);
523 }
c24b5dfa 524
95afcf5c
DC
525 if ((attempts % 5) == 0) {
526 delay(1); /* Don't just spin the CPU */
c24b5dfa 527 }
95afcf5c
DC
528 i = 0;
529 try_lock = 0;
530 goto again;
c24b5dfa 531 }
c24b5dfa
DC
532}
533
534/*
653c60b6 535 * xfs_lock_two_inodes() can only be used to lock one type of lock at a time -
7c2d238a
DW
536 * the mmaplock or the ilock, but not more than one type at a time. If we lock
537 * more than one at a time, lockdep will report false positives saying we have
538 * violated locking orders. The iolock must be double-locked separately since
539 * we use i_rwsem for that. We now support taking one lock EXCL and the other
540 * SHARED.
c24b5dfa
DC
541 */
542void
543xfs_lock_two_inodes(
7c2d238a
DW
544 struct xfs_inode *ip0,
545 uint ip0_mode,
546 struct xfs_inode *ip1,
547 uint ip1_mode)
c24b5dfa 548{
7c2d238a
DW
549 struct xfs_inode *temp;
550 uint mode_temp;
c24b5dfa 551 int attempts = 0;
efe2330f 552 struct xfs_log_item *lp;
c24b5dfa 553
7c2d238a
DW
554 ASSERT(hweight32(ip0_mode) == 1);
555 ASSERT(hweight32(ip1_mode) == 1);
556 ASSERT(!(ip0_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)));
557 ASSERT(!(ip1_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)));
558 ASSERT(!(ip0_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) ||
559 !(ip0_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
560 ASSERT(!(ip1_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) ||
561 !(ip1_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
562 ASSERT(!(ip1_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) ||
563 !(ip0_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
564 ASSERT(!(ip0_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) ||
565 !(ip1_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
653c60b6 566
c24b5dfa
DC
567 ASSERT(ip0->i_ino != ip1->i_ino);
568
569 if (ip0->i_ino > ip1->i_ino) {
570 temp = ip0;
571 ip0 = ip1;
572 ip1 = temp;
7c2d238a
DW
573 mode_temp = ip0_mode;
574 ip0_mode = ip1_mode;
575 ip1_mode = mode_temp;
c24b5dfa
DC
576 }
577
578 again:
7c2d238a 579 xfs_ilock(ip0, xfs_lock_inumorder(ip0_mode, 0));
c24b5dfa
DC
580
581 /*
582 * If the first lock we have locked is in the AIL, we must TRY to get
583 * the second lock. If we can't get it, we must release the first one
584 * and try again.
585 */
b3b14aac 586 lp = &ip0->i_itemp->ili_item;
22525c17 587 if (lp && test_bit(XFS_LI_IN_AIL, &lp->li_flags)) {
7c2d238a
DW
588 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(ip1_mode, 1))) {
589 xfs_iunlock(ip0, ip0_mode);
c24b5dfa
DC
590 if ((++attempts % 5) == 0)
591 delay(1); /* Don't just spin the CPU */
592 goto again;
593 }
594 } else {
7c2d238a 595 xfs_ilock(ip1, xfs_lock_inumorder(ip1_mode, 1));
c24b5dfa
DC
596 }
597}
598
4422501d
CH
599uint
600xfs_ip2xflags(
601 struct xfs_inode *ip)
1da177e4
LT
602{
603 uint flags = 0;
604
4422501d
CH
605 if (ip->i_diflags & XFS_DIFLAG_ANY) {
606 if (ip->i_diflags & XFS_DIFLAG_REALTIME)
e7b89481 607 flags |= FS_XFLAG_REALTIME;
4422501d 608 if (ip->i_diflags & XFS_DIFLAG_PREALLOC)
e7b89481 609 flags |= FS_XFLAG_PREALLOC;
4422501d 610 if (ip->i_diflags & XFS_DIFLAG_IMMUTABLE)
e7b89481 611 flags |= FS_XFLAG_IMMUTABLE;
4422501d 612 if (ip->i_diflags & XFS_DIFLAG_APPEND)
e7b89481 613 flags |= FS_XFLAG_APPEND;
4422501d 614 if (ip->i_diflags & XFS_DIFLAG_SYNC)
e7b89481 615 flags |= FS_XFLAG_SYNC;
4422501d 616 if (ip->i_diflags & XFS_DIFLAG_NOATIME)
e7b89481 617 flags |= FS_XFLAG_NOATIME;
4422501d 618 if (ip->i_diflags & XFS_DIFLAG_NODUMP)
e7b89481 619 flags |= FS_XFLAG_NODUMP;
4422501d 620 if (ip->i_diflags & XFS_DIFLAG_RTINHERIT)
e7b89481 621 flags |= FS_XFLAG_RTINHERIT;
4422501d 622 if (ip->i_diflags & XFS_DIFLAG_PROJINHERIT)
e7b89481 623 flags |= FS_XFLAG_PROJINHERIT;
4422501d 624 if (ip->i_diflags & XFS_DIFLAG_NOSYMLINKS)
e7b89481 625 flags |= FS_XFLAG_NOSYMLINKS;
4422501d 626 if (ip->i_diflags & XFS_DIFLAG_EXTSIZE)
e7b89481 627 flags |= FS_XFLAG_EXTSIZE;
4422501d 628 if (ip->i_diflags & XFS_DIFLAG_EXTSZINHERIT)
e7b89481 629 flags |= FS_XFLAG_EXTSZINHERIT;
4422501d 630 if (ip->i_diflags & XFS_DIFLAG_NODEFRAG)
e7b89481 631 flags |= FS_XFLAG_NODEFRAG;
4422501d 632 if (ip->i_diflags & XFS_DIFLAG_FILESTREAM)
e7b89481 633 flags |= FS_XFLAG_FILESTREAM;
1da177e4
LT
634 }
635
4422501d
CH
636 if (ip->i_diflags2 & XFS_DIFLAG2_ANY) {
637 if (ip->i_diflags2 & XFS_DIFLAG2_DAX)
58f88ca2 638 flags |= FS_XFLAG_DAX;
4422501d 639 if (ip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE)
f7ca3522 640 flags |= FS_XFLAG_COWEXTSIZE;
58f88ca2
DC
641 }
642
4422501d 643 if (XFS_IFORK_Q(ip))
58f88ca2 644 flags |= FS_XFLAG_HASATTR;
1da177e4
LT
645 return flags;
646}
647
c24b5dfa
DC
648/*
649 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
650 * is allowed, otherwise it has to be an exact match. If a CI match is found,
651 * ci_name->name will point to a the actual name (caller must free) or
652 * will be set to NULL if an exact match is found.
653 */
654int
655xfs_lookup(
656 xfs_inode_t *dp,
657 struct xfs_name *name,
658 xfs_inode_t **ipp,
659 struct xfs_name *ci_name)
660{
661 xfs_ino_t inum;
662 int error;
c24b5dfa
DC
663
664 trace_xfs_lookup(dp, name);
665
666 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
2451337d 667 return -EIO;
c24b5dfa 668
c24b5dfa 669 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
c24b5dfa 670 if (error)
dbad7c99 671 goto out_unlock;
c24b5dfa
DC
672
673 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
674 if (error)
675 goto out_free_name;
676
677 return 0;
678
679out_free_name:
680 if (ci_name)
681 kmem_free(ci_name->name);
dbad7c99 682out_unlock:
c24b5dfa
DC
683 *ipp = NULL;
684 return error;
685}
686
8a569d71
DW
687/* Propagate di_flags from a parent inode to a child inode. */
688static void
689xfs_inode_inherit_flags(
690 struct xfs_inode *ip,
691 const struct xfs_inode *pip)
692{
693 unsigned int di_flags = 0;
603f000b 694 xfs_failaddr_t failaddr;
8a569d71
DW
695 umode_t mode = VFS_I(ip)->i_mode;
696
697 if (S_ISDIR(mode)) {
db07349d 698 if (pip->i_diflags & XFS_DIFLAG_RTINHERIT)
8a569d71 699 di_flags |= XFS_DIFLAG_RTINHERIT;
db07349d 700 if (pip->i_diflags & XFS_DIFLAG_EXTSZINHERIT) {
8a569d71 701 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
031474c2 702 ip->i_extsize = pip->i_extsize;
8a569d71 703 }
db07349d 704 if (pip->i_diflags & XFS_DIFLAG_PROJINHERIT)
8a569d71
DW
705 di_flags |= XFS_DIFLAG_PROJINHERIT;
706 } else if (S_ISREG(mode)) {
db07349d 707 if ((pip->i_diflags & XFS_DIFLAG_RTINHERIT) &&
d4f2c14c 708 xfs_sb_version_hasrealtime(&ip->i_mount->m_sb))
8a569d71 709 di_flags |= XFS_DIFLAG_REALTIME;
db07349d 710 if (pip->i_diflags & XFS_DIFLAG_EXTSZINHERIT) {
8a569d71 711 di_flags |= XFS_DIFLAG_EXTSIZE;
031474c2 712 ip->i_extsize = pip->i_extsize;
8a569d71
DW
713 }
714 }
db07349d 715 if ((pip->i_diflags & XFS_DIFLAG_NOATIME) &&
8a569d71
DW
716 xfs_inherit_noatime)
717 di_flags |= XFS_DIFLAG_NOATIME;
db07349d 718 if ((pip->i_diflags & XFS_DIFLAG_NODUMP) &&
8a569d71
DW
719 xfs_inherit_nodump)
720 di_flags |= XFS_DIFLAG_NODUMP;
db07349d 721 if ((pip->i_diflags & XFS_DIFLAG_SYNC) &&
8a569d71
DW
722 xfs_inherit_sync)
723 di_flags |= XFS_DIFLAG_SYNC;
db07349d 724 if ((pip->i_diflags & XFS_DIFLAG_NOSYMLINKS) &&
8a569d71
DW
725 xfs_inherit_nosymlinks)
726 di_flags |= XFS_DIFLAG_NOSYMLINKS;
db07349d 727 if ((pip->i_diflags & XFS_DIFLAG_NODEFRAG) &&
8a569d71
DW
728 xfs_inherit_nodefrag)
729 di_flags |= XFS_DIFLAG_NODEFRAG;
db07349d 730 if (pip->i_diflags & XFS_DIFLAG_FILESTREAM)
8a569d71
DW
731 di_flags |= XFS_DIFLAG_FILESTREAM;
732
db07349d 733 ip->i_diflags |= di_flags;
603f000b
DW
734
735 /*
736 * Inode verifiers on older kernels only check that the extent size
737 * hint is an integer multiple of the rt extent size on realtime files.
738 * They did not check the hint alignment on a directory with both
739 * rtinherit and extszinherit flags set. If the misaligned hint is
740 * propagated from a directory into a new realtime file, new file
741 * allocations will fail due to math errors in the rt allocator and/or
742 * trip the verifiers. Validate the hint settings in the new file so
743 * that we don't let broken hints propagate.
744 */
745 failaddr = xfs_inode_validate_extsize(ip->i_mount, ip->i_extsize,
746 VFS_I(ip)->i_mode, ip->i_diflags);
747 if (failaddr) {
748 ip->i_diflags &= ~(XFS_DIFLAG_EXTSIZE |
749 XFS_DIFLAG_EXTSZINHERIT);
750 ip->i_extsize = 0;
751 }
8a569d71
DW
752}
753
754/* Propagate di_flags2 from a parent inode to a child inode. */
755static void
756xfs_inode_inherit_flags2(
757 struct xfs_inode *ip,
758 const struct xfs_inode *pip)
759{
603f000b
DW
760 xfs_failaddr_t failaddr;
761
3e09ab8f
CH
762 if (pip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE) {
763 ip->i_diflags2 |= XFS_DIFLAG2_COWEXTSIZE;
b33ce57d 764 ip->i_cowextsize = pip->i_cowextsize;
8a569d71 765 }
3e09ab8f
CH
766 if (pip->i_diflags2 & XFS_DIFLAG2_DAX)
767 ip->i_diflags2 |= XFS_DIFLAG2_DAX;
603f000b
DW
768
769 /* Don't let invalid cowextsize hints propagate. */
770 failaddr = xfs_inode_validate_cowextsize(ip->i_mount, ip->i_cowextsize,
771 VFS_I(ip)->i_mode, ip->i_diflags, ip->i_diflags2);
772 if (failaddr) {
773 ip->i_diflags2 &= ~XFS_DIFLAG2_COWEXTSIZE;
774 ip->i_cowextsize = 0;
775 }
8a569d71
DW
776}
777
1da177e4 778/*
1abcf261
DC
779 * Initialise a newly allocated inode and return the in-core inode to the
780 * caller locked exclusively.
1da177e4 781 */
b652afd9 782int
1abcf261 783xfs_init_new_inode(
f736d93d 784 struct user_namespace *mnt_userns,
1abcf261
DC
785 struct xfs_trans *tp,
786 struct xfs_inode *pip,
787 xfs_ino_t ino,
788 umode_t mode,
789 xfs_nlink_t nlink,
790 dev_t rdev,
791 prid_t prid,
e6a688c3 792 bool init_xattrs,
1abcf261 793 struct xfs_inode **ipp)
1da177e4 794{
01ea173e 795 struct inode *dir = pip ? VFS_I(pip) : NULL;
1abcf261
DC
796 struct xfs_mount *mp = tp->t_mountp;
797 struct xfs_inode *ip;
798 unsigned int flags;
799 int error;
800 struct timespec64 tv;
801 struct inode *inode;
1da177e4 802
8b26984d
DC
803 /*
804 * Protect against obviously corrupt allocation btree records. Later
805 * xfs_iget checks will catch re-allocation of other active in-memory
806 * and on-disk inodes. If we don't catch reallocating the parent inode
807 * here we will deadlock in xfs_iget() so we have to do these checks
808 * first.
809 */
810 if ((pip && ino == pip->i_ino) || !xfs_verify_dir_ino(mp, ino)) {
811 xfs_alert(mp, "Allocated a known in-use inode 0x%llx!", ino);
812 return -EFSCORRUPTED;
813 }
814
1da177e4 815 /*
1abcf261
DC
816 * Get the in-core inode with the lock held exclusively to prevent
817 * others from looking at until we're done.
1da177e4 818 */
1abcf261 819 error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE, XFS_ILOCK_EXCL, &ip);
bf904248 820 if (error)
1da177e4 821 return error;
1abcf261 822
1da177e4 823 ASSERT(ip != NULL);
3987848c 824 inode = VFS_I(ip);
54d7b5c1 825 set_nlink(inode, nlink);
66f36464 826 inode->i_rdev = rdev;
ceaf603c 827 ip->i_projid = prid;
1da177e4 828
01ea173e
CH
829 if (dir && !(dir->i_mode & S_ISGID) &&
830 (mp->m_flags & XFS_MOUNT_GRPID)) {
db998553 831 inode_fsuid_set(inode, mnt_userns);
01ea173e
CH
832 inode->i_gid = dir->i_gid;
833 inode->i_mode = mode;
3d8f2821 834 } else {
7d6beb71 835 inode_init_owner(mnt_userns, inode, dir, mode);
1da177e4
LT
836 }
837
838 /*
839 * If the group ID of the new file does not match the effective group
840 * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
841 * (and only if the irix_sgid_inherit compatibility variable is set).
842 */
54295159 843 if (irix_sgid_inherit &&
f736d93d
CH
844 (inode->i_mode & S_ISGID) &&
845 !in_group_p(i_gid_into_mnt(mnt_userns, inode)))
c19b3b05 846 inode->i_mode &= ~S_ISGID;
1da177e4 847
13d2c10b 848 ip->i_disk_size = 0;
daf83964 849 ip->i_df.if_nextents = 0;
6e73a545 850 ASSERT(ip->i_nblocks == 0);
dff35fd4 851
c2050a45 852 tv = current_time(inode);
3987848c
DC
853 inode->i_mtime = tv;
854 inode->i_atime = tv;
855 inode->i_ctime = tv;
dff35fd4 856
031474c2 857 ip->i_extsize = 0;
db07349d 858 ip->i_diflags = 0;
93848a99 859
6471e9c5 860 if (xfs_sb_version_has_v3inode(&mp->m_sb)) {
f0e28280 861 inode_set_iversion(inode, 1);
b33ce57d 862 ip->i_cowextsize = 0;
e98d5e88 863 ip->i_crtime = tv;
93848a99
CH
864 }
865
1da177e4
LT
866 flags = XFS_ILOG_CORE;
867 switch (mode & S_IFMT) {
868 case S_IFIFO:
869 case S_IFCHR:
870 case S_IFBLK:
871 case S_IFSOCK:
f7e67b20 872 ip->i_df.if_format = XFS_DINODE_FMT_DEV;
1da177e4
LT
873 flags |= XFS_ILOG_DEV;
874 break;
875 case S_IFREG:
876 case S_IFDIR:
db07349d 877 if (pip && (pip->i_diflags & XFS_DIFLAG_ANY))
8a569d71 878 xfs_inode_inherit_flags(ip, pip);
3e09ab8f 879 if (pip && (pip->i_diflags2 & XFS_DIFLAG2_ANY))
8a569d71 880 xfs_inode_inherit_flags2(ip, pip);
53004ee7 881 fallthrough;
1da177e4 882 case S_IFLNK:
f7e67b20 883 ip->i_df.if_format = XFS_DINODE_FMT_EXTENTS;
fcacbc3f 884 ip->i_df.if_bytes = 0;
6bdcf26a 885 ip->i_df.if_u1.if_root = NULL;
1da177e4
LT
886 break;
887 default:
888 ASSERT(0);
889 }
1da177e4 890
e6a688c3
DC
891 /*
892 * If we need to create attributes immediately after allocating the
893 * inode, initialise an empty attribute fork right now. We use the
894 * default fork offset for attributes here as we don't know exactly what
895 * size or how many attributes we might be adding. We can do this
896 * safely here because we know the data fork is completely empty and
897 * this saves us from needing to run a separate transaction to set the
898 * fork offset in the immediate future.
899 */
2442ee15 900 if (init_xattrs && xfs_sb_version_hasattr(&mp->m_sb)) {
7821ea30 901 ip->i_forkoff = xfs_default_attroffset(ip) >> 3;
e6a688c3
DC
902 ip->i_afp = xfs_ifork_alloc(XFS_DINODE_FMT_EXTENTS, 0);
903 }
904
1da177e4
LT
905 /*
906 * Log the new values stuffed into the inode.
907 */
ddc3415a 908 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1da177e4
LT
909 xfs_trans_log_inode(tp, ip, flags);
910
58c90473 911 /* now that we have an i_mode we can setup the inode structure */
41be8bed 912 xfs_setup_inode(ip);
1da177e4
LT
913
914 *ipp = ip;
915 return 0;
916}
917
e546cb79 918/*
54d7b5c1
DC
919 * Decrement the link count on an inode & log the change. If this causes the
920 * link count to go to zero, move the inode to AGI unlinked list so that it can
921 * be freed when the last active reference goes away via xfs_inactive().
e546cb79 922 */
0d5a75e9 923static int /* error */
e546cb79
DC
924xfs_droplink(
925 xfs_trans_t *tp,
926 xfs_inode_t *ip)
927{
e546cb79
DC
928 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
929
e546cb79
DC
930 drop_nlink(VFS_I(ip));
931 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
932
54d7b5c1
DC
933 if (VFS_I(ip)->i_nlink)
934 return 0;
935
936 return xfs_iunlink(tp, ip);
e546cb79
DC
937}
938
e546cb79
DC
939/*
940 * Increment the link count on an inode & log the change.
941 */
91083269 942static void
e546cb79
DC
943xfs_bumplink(
944 xfs_trans_t *tp,
945 xfs_inode_t *ip)
946{
947 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
948
e546cb79 949 inc_nlink(VFS_I(ip));
e546cb79 950 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
e546cb79
DC
951}
952
c24b5dfa
DC
953int
954xfs_create(
f736d93d 955 struct user_namespace *mnt_userns,
c24b5dfa
DC
956 xfs_inode_t *dp,
957 struct xfs_name *name,
958 umode_t mode,
66f36464 959 dev_t rdev,
e6a688c3 960 bool init_xattrs,
c24b5dfa
DC
961 xfs_inode_t **ipp)
962{
963 int is_dir = S_ISDIR(mode);
964 struct xfs_mount *mp = dp->i_mount;
965 struct xfs_inode *ip = NULL;
966 struct xfs_trans *tp = NULL;
967 int error;
c24b5dfa 968 bool unlock_dp_on_error = false;
c24b5dfa
DC
969 prid_t prid;
970 struct xfs_dquot *udqp = NULL;
971 struct xfs_dquot *gdqp = NULL;
972 struct xfs_dquot *pdqp = NULL;
062647a8 973 struct xfs_trans_res *tres;
c24b5dfa 974 uint resblks;
b652afd9 975 xfs_ino_t ino;
c24b5dfa
DC
976
977 trace_xfs_create(dp, name);
978
979 if (XFS_FORCED_SHUTDOWN(mp))
2451337d 980 return -EIO;
c24b5dfa 981
163467d3 982 prid = xfs_get_initial_prid(dp);
c24b5dfa
DC
983
984 /*
985 * Make sure that we have allocated dquot(s) on disk.
986 */
a65e58e7
CB
987 error = xfs_qm_vop_dqalloc(dp, mapped_fsuid(mnt_userns),
988 mapped_fsgid(mnt_userns), prid,
b5a08423
DW
989 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
990 &udqp, &gdqp, &pdqp);
c24b5dfa
DC
991 if (error)
992 return error;
993
994 if (is_dir) {
c24b5dfa 995 resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
062647a8 996 tres = &M_RES(mp)->tr_mkdir;
c24b5dfa
DC
997 } else {
998 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
062647a8 999 tres = &M_RES(mp)->tr_create;
c24b5dfa
DC
1000 }
1001
c24b5dfa
DC
1002 /*
1003 * Initially assume that the file does not exist and
1004 * reserve the resources for that case. If that is not
1005 * the case we'll drop the one we have and get a more
1006 * appropriate transaction later.
1007 */
f2f7b9ff
DW
1008 error = xfs_trans_alloc_icreate(mp, tres, udqp, gdqp, pdqp, resblks,
1009 &tp);
2451337d 1010 if (error == -ENOSPC) {
c24b5dfa
DC
1011 /* flush outstanding delalloc blocks and retry */
1012 xfs_flush_inodes(mp);
f2f7b9ff
DW
1013 error = xfs_trans_alloc_icreate(mp, tres, udqp, gdqp, pdqp,
1014 resblks, &tp);
c24b5dfa 1015 }
4906e215 1016 if (error)
f2f7b9ff 1017 goto out_release_dquots;
c24b5dfa 1018
65523218 1019 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
c24b5dfa
DC
1020 unlock_dp_on_error = true;
1021
f5d92749
CB
1022 error = xfs_iext_count_may_overflow(dp, XFS_DATA_FORK,
1023 XFS_IEXT_DIR_MANIP_CNT(mp));
1024 if (error)
1025 goto out_trans_cancel;
1026
c24b5dfa
DC
1027 /*
1028 * A newly created regular or special file just has one directory
1029 * entry pointing to them, but a directory also the "." entry
1030 * pointing to itself.
1031 */
b652afd9
DC
1032 error = xfs_dialloc(&tp, dp->i_ino, mode, &ino);
1033 if (!error)
1034 error = xfs_init_new_inode(mnt_userns, tp, dp, ino, mode,
1035 is_dir ? 2 : 1, rdev, prid, init_xattrs, &ip);
d6077aa3 1036 if (error)
4906e215 1037 goto out_trans_cancel;
c24b5dfa
DC
1038
1039 /*
1040 * Now we join the directory inode to the transaction. We do not do it
b652afd9 1041 * earlier because xfs_dialloc might commit the previous transaction
c24b5dfa
DC
1042 * (and release all the locks). An error from here on will result in
1043 * the transaction cancel unlocking dp so don't do it explicitly in the
1044 * error path.
1045 */
65523218 1046 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
c24b5dfa
DC
1047 unlock_dp_on_error = false;
1048
381eee69 1049 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
63337b63 1050 resblks - XFS_IALLOC_SPACE_RES(mp));
c24b5dfa 1051 if (error) {
2451337d 1052 ASSERT(error != -ENOSPC);
4906e215 1053 goto out_trans_cancel;
c24b5dfa
DC
1054 }
1055 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1056 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1057
1058 if (is_dir) {
1059 error = xfs_dir_init(tp, ip, dp);
1060 if (error)
c8eac49e 1061 goto out_trans_cancel;
c24b5dfa 1062
91083269 1063 xfs_bumplink(tp, dp);
c24b5dfa
DC
1064 }
1065
1066 /*
1067 * If this is a synchronous mount, make sure that the
1068 * create transaction goes to disk before returning to
1069 * the user.
1070 */
1071 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1072 xfs_trans_set_sync(tp);
1073
1074 /*
1075 * Attach the dquot(s) to the inodes and modify them incore.
1076 * These ids of the inode couldn't have changed since the new
1077 * inode has been locked ever since it was created.
1078 */
1079 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
1080
70393313 1081 error = xfs_trans_commit(tp);
c24b5dfa
DC
1082 if (error)
1083 goto out_release_inode;
1084
1085 xfs_qm_dqrele(udqp);
1086 xfs_qm_dqrele(gdqp);
1087 xfs_qm_dqrele(pdqp);
1088
1089 *ipp = ip;
1090 return 0;
1091
c24b5dfa 1092 out_trans_cancel:
4906e215 1093 xfs_trans_cancel(tp);
c24b5dfa
DC
1094 out_release_inode:
1095 /*
58c90473
DC
1096 * Wait until after the current transaction is aborted to finish the
1097 * setup of the inode and release the inode. This prevents recursive
1098 * transactions and deadlocks from xfs_inactive.
c24b5dfa 1099 */
58c90473
DC
1100 if (ip) {
1101 xfs_finish_inode_setup(ip);
44a8736b 1102 xfs_irele(ip);
58c90473 1103 }
f2f7b9ff 1104 out_release_dquots:
c24b5dfa
DC
1105 xfs_qm_dqrele(udqp);
1106 xfs_qm_dqrele(gdqp);
1107 xfs_qm_dqrele(pdqp);
1108
1109 if (unlock_dp_on_error)
65523218 1110 xfs_iunlock(dp, XFS_ILOCK_EXCL);
c24b5dfa
DC
1111 return error;
1112}
1113
99b6436b
ZYW
1114int
1115xfs_create_tmpfile(
f736d93d 1116 struct user_namespace *mnt_userns,
99b6436b 1117 struct xfs_inode *dp,
330033d6
BF
1118 umode_t mode,
1119 struct xfs_inode **ipp)
99b6436b
ZYW
1120{
1121 struct xfs_mount *mp = dp->i_mount;
1122 struct xfs_inode *ip = NULL;
1123 struct xfs_trans *tp = NULL;
1124 int error;
99b6436b
ZYW
1125 prid_t prid;
1126 struct xfs_dquot *udqp = NULL;
1127 struct xfs_dquot *gdqp = NULL;
1128 struct xfs_dquot *pdqp = NULL;
1129 struct xfs_trans_res *tres;
1130 uint resblks;
b652afd9 1131 xfs_ino_t ino;
99b6436b
ZYW
1132
1133 if (XFS_FORCED_SHUTDOWN(mp))
2451337d 1134 return -EIO;
99b6436b
ZYW
1135
1136 prid = xfs_get_initial_prid(dp);
1137
1138 /*
1139 * Make sure that we have allocated dquot(s) on disk.
1140 */
a65e58e7
CB
1141 error = xfs_qm_vop_dqalloc(dp, mapped_fsuid(mnt_userns),
1142 mapped_fsgid(mnt_userns), prid,
b5a08423
DW
1143 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
1144 &udqp, &gdqp, &pdqp);
99b6436b
ZYW
1145 if (error)
1146 return error;
1147
1148 resblks = XFS_IALLOC_SPACE_RES(mp);
99b6436b 1149 tres = &M_RES(mp)->tr_create_tmpfile;
253f4911 1150
f2f7b9ff
DW
1151 error = xfs_trans_alloc_icreate(mp, tres, udqp, gdqp, pdqp, resblks,
1152 &tp);
99b6436b 1153 if (error)
f2f7b9ff 1154 goto out_release_dquots;
99b6436b 1155
b652afd9
DC
1156 error = xfs_dialloc(&tp, dp->i_ino, mode, &ino);
1157 if (!error)
1158 error = xfs_init_new_inode(mnt_userns, tp, dp, ino, mode,
1159 0, 0, prid, false, &ip);
d6077aa3 1160 if (error)
4906e215 1161 goto out_trans_cancel;
99b6436b
ZYW
1162
1163 if (mp->m_flags & XFS_MOUNT_WSYNC)
1164 xfs_trans_set_sync(tp);
1165
1166 /*
1167 * Attach the dquot(s) to the inodes and modify them incore.
1168 * These ids of the inode couldn't have changed since the new
1169 * inode has been locked ever since it was created.
1170 */
1171 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
1172
99b6436b
ZYW
1173 error = xfs_iunlink(tp, ip);
1174 if (error)
4906e215 1175 goto out_trans_cancel;
99b6436b 1176
70393313 1177 error = xfs_trans_commit(tp);
99b6436b
ZYW
1178 if (error)
1179 goto out_release_inode;
1180
1181 xfs_qm_dqrele(udqp);
1182 xfs_qm_dqrele(gdqp);
1183 xfs_qm_dqrele(pdqp);
1184
330033d6 1185 *ipp = ip;
99b6436b
ZYW
1186 return 0;
1187
99b6436b 1188 out_trans_cancel:
4906e215 1189 xfs_trans_cancel(tp);
99b6436b
ZYW
1190 out_release_inode:
1191 /*
58c90473
DC
1192 * Wait until after the current transaction is aborted to finish the
1193 * setup of the inode and release the inode. This prevents recursive
1194 * transactions and deadlocks from xfs_inactive.
99b6436b 1195 */
58c90473
DC
1196 if (ip) {
1197 xfs_finish_inode_setup(ip);
44a8736b 1198 xfs_irele(ip);
58c90473 1199 }
f2f7b9ff 1200 out_release_dquots:
99b6436b
ZYW
1201 xfs_qm_dqrele(udqp);
1202 xfs_qm_dqrele(gdqp);
1203 xfs_qm_dqrele(pdqp);
1204
1205 return error;
1206}
1207
c24b5dfa
DC
1208int
1209xfs_link(
1210 xfs_inode_t *tdp,
1211 xfs_inode_t *sip,
1212 struct xfs_name *target_name)
1213{
1214 xfs_mount_t *mp = tdp->i_mount;
1215 xfs_trans_t *tp;
1216 int error;
c24b5dfa
DC
1217 int resblks;
1218
1219 trace_xfs_link(tdp, target_name);
1220
c19b3b05 1221 ASSERT(!S_ISDIR(VFS_I(sip)->i_mode));
c24b5dfa
DC
1222
1223 if (XFS_FORCED_SHUTDOWN(mp))
2451337d 1224 return -EIO;
c24b5dfa 1225
c14cfcca 1226 error = xfs_qm_dqattach(sip);
c24b5dfa
DC
1227 if (error)
1228 goto std_return;
1229
c14cfcca 1230 error = xfs_qm_dqattach(tdp);
c24b5dfa
DC
1231 if (error)
1232 goto std_return;
1233
c24b5dfa 1234 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
253f4911 1235 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_link, resblks, 0, 0, &tp);
2451337d 1236 if (error == -ENOSPC) {
c24b5dfa 1237 resblks = 0;
253f4911 1238 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_link, 0, 0, 0, &tp);
c24b5dfa 1239 }
4906e215 1240 if (error)
253f4911 1241 goto std_return;
c24b5dfa 1242
7c2d238a 1243 xfs_lock_two_inodes(sip, XFS_ILOCK_EXCL, tdp, XFS_ILOCK_EXCL);
c24b5dfa
DC
1244
1245 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
65523218 1246 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
c24b5dfa 1247
f5d92749
CB
1248 error = xfs_iext_count_may_overflow(tdp, XFS_DATA_FORK,
1249 XFS_IEXT_DIR_MANIP_CNT(mp));
1250 if (error)
1251 goto error_return;
1252
c24b5dfa
DC
1253 /*
1254 * If we are using project inheritance, we only allow hard link
1255 * creation in our tree when the project IDs are the same; else
1256 * the tree quota mechanism could be circumvented.
1257 */
db07349d 1258 if (unlikely((tdp->i_diflags & XFS_DIFLAG_PROJINHERIT) &&
ceaf603c 1259 tdp->i_projid != sip->i_projid)) {
2451337d 1260 error = -EXDEV;
c24b5dfa
DC
1261 goto error_return;
1262 }
1263
94f3cad5
ES
1264 if (!resblks) {
1265 error = xfs_dir_canenter(tp, tdp, target_name);
1266 if (error)
1267 goto error_return;
1268 }
c24b5dfa 1269
54d7b5c1
DC
1270 /*
1271 * Handle initial link state of O_TMPFILE inode
1272 */
1273 if (VFS_I(sip)->i_nlink == 0) {
f40aadb2
DC
1274 struct xfs_perag *pag;
1275
1276 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, sip->i_ino));
1277 error = xfs_iunlink_remove(tp, pag, sip);
1278 xfs_perag_put(pag);
ab297431 1279 if (error)
4906e215 1280 goto error_return;
ab297431
ZYW
1281 }
1282
c24b5dfa 1283 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
381eee69 1284 resblks);
c24b5dfa 1285 if (error)
4906e215 1286 goto error_return;
c24b5dfa
DC
1287 xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1288 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
1289
91083269 1290 xfs_bumplink(tp, sip);
c24b5dfa
DC
1291
1292 /*
1293 * If this is a synchronous mount, make sure that the
1294 * link transaction goes to disk before returning to
1295 * the user.
1296 */
f6106efa 1297 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
c24b5dfa 1298 xfs_trans_set_sync(tp);
c24b5dfa 1299
70393313 1300 return xfs_trans_commit(tp);
c24b5dfa 1301
c24b5dfa 1302 error_return:
4906e215 1303 xfs_trans_cancel(tp);
c24b5dfa
DC
1304 std_return:
1305 return error;
1306}
1307
363e59ba
DW
1308/* Clear the reflink flag and the cowblocks tag if possible. */
1309static void
1310xfs_itruncate_clear_reflink_flags(
1311 struct xfs_inode *ip)
1312{
1313 struct xfs_ifork *dfork;
1314 struct xfs_ifork *cfork;
1315
1316 if (!xfs_is_reflink_inode(ip))
1317 return;
1318 dfork = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1319 cfork = XFS_IFORK_PTR(ip, XFS_COW_FORK);
1320 if (dfork->if_bytes == 0 && cfork->if_bytes == 0)
3e09ab8f 1321 ip->i_diflags2 &= ~XFS_DIFLAG2_REFLINK;
363e59ba
DW
1322 if (cfork->if_bytes == 0)
1323 xfs_inode_clear_cowblocks_tag(ip);
1324}
1325
1da177e4 1326/*
8f04c47a
CH
1327 * Free up the underlying blocks past new_size. The new size must be smaller
1328 * than the current size. This routine can be used both for the attribute and
1329 * data fork, and does not modify the inode size, which is left to the caller.
1da177e4 1330 *
f6485057
DC
1331 * The transaction passed to this routine must have made a permanent log
1332 * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the
1333 * given transaction and start new ones, so make sure everything involved in
1334 * the transaction is tidy before calling here. Some transaction will be
1335 * returned to the caller to be committed. The incoming transaction must
1336 * already include the inode, and both inode locks must be held exclusively.
1337 * The inode must also be "held" within the transaction. On return the inode
1338 * will be "held" within the returned transaction. This routine does NOT
1339 * require any disk space to be reserved for it within the transaction.
1da177e4 1340 *
f6485057
DC
1341 * If we get an error, we must return with the inode locked and linked into the
1342 * current transaction. This keeps things simple for the higher level code,
1343 * because it always knows that the inode is locked and held in the transaction
1344 * that returns to it whether errors occur or not. We don't mark the inode
1345 * dirty on error so that transactions can be easily aborted if possible.
1da177e4
LT
1346 */
1347int
4e529339 1348xfs_itruncate_extents_flags(
8f04c47a
CH
1349 struct xfs_trans **tpp,
1350 struct xfs_inode *ip,
1351 int whichfork,
13b86fc3 1352 xfs_fsize_t new_size,
4e529339 1353 int flags)
1da177e4 1354{
8f04c47a
CH
1355 struct xfs_mount *mp = ip->i_mount;
1356 struct xfs_trans *tp = *tpp;
8f04c47a 1357 xfs_fileoff_t first_unmap_block;
8f04c47a 1358 xfs_filblks_t unmap_len;
8f04c47a 1359 int error = 0;
1da177e4 1360
0b56185b
CH
1361 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1362 ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
1363 xfs_isilocked(ip, XFS_IOLOCK_EXCL));
ce7ae151 1364 ASSERT(new_size <= XFS_ISIZE(ip));
8f04c47a 1365 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1da177e4 1366 ASSERT(ip->i_itemp != NULL);
898621d5 1367 ASSERT(ip->i_itemp->ili_lock_flags == 0);
8f04c47a 1368 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1da177e4 1369
673e8e59
CH
1370 trace_xfs_itruncate_extents_start(ip, new_size);
1371
4e529339 1372 flags |= xfs_bmapi_aflag(whichfork);
13b86fc3 1373
1da177e4
LT
1374 /*
1375 * Since it is possible for space to become allocated beyond
1376 * the end of the file (in a crash where the space is allocated
1377 * but the inode size is not yet updated), simply remove any
1378 * blocks which show up between the new EOF and the maximum
4bbb04ab
DW
1379 * possible file size.
1380 *
1381 * We have to free all the blocks to the bmbt maximum offset, even if
1382 * the page cache can't scale that far.
1da177e4 1383 */
8f04c47a 1384 first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
33005fd0 1385 if (!xfs_verify_fileoff(mp, first_unmap_block)) {
4bbb04ab 1386 WARN_ON_ONCE(first_unmap_block > XFS_MAX_FILEOFF);
8f04c47a 1387 return 0;
4bbb04ab 1388 }
8f04c47a 1389
4bbb04ab
DW
1390 unmap_len = XFS_MAX_FILEOFF - first_unmap_block + 1;
1391 while (unmap_len > 0) {
02dff7bf 1392 ASSERT(tp->t_firstblock == NULLFSBLOCK);
4bbb04ab
DW
1393 error = __xfs_bunmapi(tp, ip, first_unmap_block, &unmap_len,
1394 flags, XFS_ITRUNC_MAX_EXTENTS);
8f04c47a 1395 if (error)
d5a2e289 1396 goto out;
1da177e4 1397
6dd379c7 1398 /* free the just unmapped extents */
9e28a242 1399 error = xfs_defer_finish(&tp);
8f04c47a 1400 if (error)
9b1f4e98 1401 goto out;
1da177e4 1402 }
8f04c47a 1403
4919d42a
DW
1404 if (whichfork == XFS_DATA_FORK) {
1405 /* Remove all pending CoW reservations. */
1406 error = xfs_reflink_cancel_cow_blocks(ip, &tp,
4bbb04ab 1407 first_unmap_block, XFS_MAX_FILEOFF, true);
4919d42a
DW
1408 if (error)
1409 goto out;
aa8968f2 1410
4919d42a
DW
1411 xfs_itruncate_clear_reflink_flags(ip);
1412 }
aa8968f2 1413
673e8e59
CH
1414 /*
1415 * Always re-log the inode so that our permanent transaction can keep
1416 * on rolling it forward in the log.
1417 */
1418 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1419
1420 trace_xfs_itruncate_extents_end(ip, new_size);
1421
8f04c47a
CH
1422out:
1423 *tpp = tp;
1424 return error;
8f04c47a
CH
1425}
1426
c24b5dfa
DC
1427int
1428xfs_release(
1429 xfs_inode_t *ip)
1430{
1431 xfs_mount_t *mp = ip->i_mount;
7d88329e 1432 int error = 0;
c24b5dfa 1433
c19b3b05 1434 if (!S_ISREG(VFS_I(ip)->i_mode) || (VFS_I(ip)->i_mode == 0))
c24b5dfa
DC
1435 return 0;
1436
1437 /* If this is a read-only mount, don't do this (would generate I/O) */
1438 if (mp->m_flags & XFS_MOUNT_RDONLY)
1439 return 0;
1440
1441 if (!XFS_FORCED_SHUTDOWN(mp)) {
1442 int truncated;
1443
c24b5dfa
DC
1444 /*
1445 * If we previously truncated this file and removed old data
1446 * in the process, we want to initiate "early" writeout on
1447 * the last close. This is an attempt to combat the notorious
1448 * NULL files problem which is particularly noticeable from a
1449 * truncate down, buffered (re-)write (delalloc), followed by
1450 * a crash. What we are effectively doing here is
1451 * significantly reducing the time window where we'd otherwise
1452 * be exposed to that problem.
1453 */
1454 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
1455 if (truncated) {
1456 xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
eac152b4 1457 if (ip->i_delayed_blks > 0) {
2451337d 1458 error = filemap_flush(VFS_I(ip)->i_mapping);
c24b5dfa
DC
1459 if (error)
1460 return error;
1461 }
1462 }
1463 }
1464
54d7b5c1 1465 if (VFS_I(ip)->i_nlink == 0)
c24b5dfa
DC
1466 return 0;
1467
7d88329e
DW
1468 /*
1469 * If we can't get the iolock just skip truncating the blocks past EOF
1470 * because we could deadlock with the mmap_lock otherwise. We'll get
1471 * another chance to drop them once the last reference to the inode is
1472 * dropped, so we'll never leak blocks permanently.
1473 */
1474 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL))
1475 return 0;
c24b5dfa 1476
7d88329e 1477 if (xfs_can_free_eofblocks(ip, false)) {
a36b9261
BF
1478 /*
1479 * Check if the inode is being opened, written and closed
1480 * frequently and we have delayed allocation blocks outstanding
1481 * (e.g. streaming writes from the NFS server), truncating the
1482 * blocks past EOF will cause fragmentation to occur.
1483 *
1484 * In this case don't do the truncation, but we have to be
1485 * careful how we detect this case. Blocks beyond EOF show up as
1486 * i_delayed_blks even when the inode is clean, so we need to
1487 * truncate them away first before checking for a dirty release.
1488 * Hence on the first dirty close we will still remove the
1489 * speculative allocation, but after that we will leave it in
1490 * place.
1491 */
1492 if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
7d88329e
DW
1493 goto out_unlock;
1494
1495 error = xfs_free_eofblocks(ip);
1496 if (error)
1497 goto out_unlock;
c24b5dfa
DC
1498
1499 /* delalloc blocks after truncation means it really is dirty */
1500 if (ip->i_delayed_blks)
1501 xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
1502 }
7d88329e
DW
1503
1504out_unlock:
1505 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1506 return error;
c24b5dfa
DC
1507}
1508
f7be2d7f
BF
1509/*
1510 * xfs_inactive_truncate
1511 *
1512 * Called to perform a truncate when an inode becomes unlinked.
1513 */
1514STATIC int
1515xfs_inactive_truncate(
1516 struct xfs_inode *ip)
1517{
1518 struct xfs_mount *mp = ip->i_mount;
1519 struct xfs_trans *tp;
1520 int error;
1521
253f4911 1522 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
f7be2d7f
BF
1523 if (error) {
1524 ASSERT(XFS_FORCED_SHUTDOWN(mp));
f7be2d7f
BF
1525 return error;
1526 }
f7be2d7f
BF
1527 xfs_ilock(ip, XFS_ILOCK_EXCL);
1528 xfs_trans_ijoin(tp, ip, 0);
1529
1530 /*
1531 * Log the inode size first to prevent stale data exposure in the event
1532 * of a system crash before the truncate completes. See the related
69bca807 1533 * comment in xfs_vn_setattr_size() for details.
f7be2d7f 1534 */
13d2c10b 1535 ip->i_disk_size = 0;
f7be2d7f
BF
1536 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1537
1538 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
1539 if (error)
1540 goto error_trans_cancel;
1541
daf83964 1542 ASSERT(ip->i_df.if_nextents == 0);
f7be2d7f 1543
70393313 1544 error = xfs_trans_commit(tp);
f7be2d7f
BF
1545 if (error)
1546 goto error_unlock;
1547
1548 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1549 return 0;
1550
1551error_trans_cancel:
4906e215 1552 xfs_trans_cancel(tp);
f7be2d7f
BF
1553error_unlock:
1554 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1555 return error;
1556}
1557
88877d2b
BF
1558/*
1559 * xfs_inactive_ifree()
1560 *
1561 * Perform the inode free when an inode is unlinked.
1562 */
1563STATIC int
1564xfs_inactive_ifree(
1565 struct xfs_inode *ip)
1566{
88877d2b
BF
1567 struct xfs_mount *mp = ip->i_mount;
1568 struct xfs_trans *tp;
1569 int error;
1570
9d43b180 1571 /*
76d771b4
CH
1572 * We try to use a per-AG reservation for any block needed by the finobt
1573 * tree, but as the finobt feature predates the per-AG reservation
1574 * support a degraded file system might not have enough space for the
1575 * reservation at mount time. In that case try to dip into the reserved
1576 * pool and pray.
9d43b180
BF
1577 *
1578 * Send a warning if the reservation does happen to fail, as the inode
1579 * now remains allocated and sits on the unlinked list until the fs is
1580 * repaired.
1581 */
e1f6ca11 1582 if (unlikely(mp->m_finobt_nores)) {
76d771b4
CH
1583 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ifree,
1584 XFS_IFREE_SPACE_RES(mp), 0, XFS_TRANS_RESERVE,
1585 &tp);
1586 } else {
1587 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ifree, 0, 0, 0, &tp);
1588 }
88877d2b 1589 if (error) {
2451337d 1590 if (error == -ENOSPC) {
9d43b180
BF
1591 xfs_warn_ratelimited(mp,
1592 "Failed to remove inode(s) from unlinked list. "
1593 "Please free space, unmount and run xfs_repair.");
1594 } else {
1595 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1596 }
88877d2b
BF
1597 return error;
1598 }
1599
96355d5a
DC
1600 /*
1601 * We do not hold the inode locked across the entire rolling transaction
1602 * here. We only need to hold it for the first transaction that
1603 * xfs_ifree() builds, which may mark the inode XFS_ISTALE if the
1604 * underlying cluster buffer is freed. Relogging an XFS_ISTALE inode
1605 * here breaks the relationship between cluster buffer invalidation and
1606 * stale inode invalidation on cluster buffer item journal commit
1607 * completion, and can result in leaving dirty stale inodes hanging
1608 * around in memory.
1609 *
1610 * We have no need for serialising this inode operation against other
1611 * operations - we freed the inode and hence reallocation is required
1612 * and that will serialise on reallocating the space the deferops need
1613 * to free. Hence we can unlock the inode on the first commit of
1614 * the transaction rather than roll it right through the deferops. This
1615 * avoids relogging the XFS_ISTALE inode.
1616 *
1617 * We check that xfs_ifree() hasn't grown an internal transaction roll
1618 * by asserting that the inode is still locked when it returns.
1619 */
88877d2b 1620 xfs_ilock(ip, XFS_ILOCK_EXCL);
96355d5a 1621 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
88877d2b 1622
0e0417f3 1623 error = xfs_ifree(tp, ip);
96355d5a 1624 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
88877d2b
BF
1625 if (error) {
1626 /*
1627 * If we fail to free the inode, shut down. The cancel
1628 * might do that, we need to make sure. Otherwise the
1629 * inode might be lost for a long time or forever.
1630 */
1631 if (!XFS_FORCED_SHUTDOWN(mp)) {
1632 xfs_notice(mp, "%s: xfs_ifree returned error %d",
1633 __func__, error);
1634 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1635 }
4906e215 1636 xfs_trans_cancel(tp);
88877d2b
BF
1637 return error;
1638 }
1639
1640 /*
1641 * Credit the quota account(s). The inode is gone.
1642 */
1643 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1644
1645 /*
d4a97a04
BF
1646 * Just ignore errors at this point. There is nothing we can do except
1647 * to try to keep going. Make sure it's not a silent error.
88877d2b 1648 */
70393313 1649 error = xfs_trans_commit(tp);
88877d2b
BF
1650 if (error)
1651 xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
1652 __func__, error);
1653
88877d2b
BF
1654 return 0;
1655}
1656
62af7d54
DW
1657/*
1658 * Returns true if we need to update the on-disk metadata before we can free
1659 * the memory used by this inode. Updates include freeing post-eof
1660 * preallocations; freeing COW staging extents; and marking the inode free in
1661 * the inobt if it is on the unlinked list.
1662 */
1663bool
1664xfs_inode_needs_inactive(
1665 struct xfs_inode *ip)
1666{
1667 struct xfs_mount *mp = ip->i_mount;
1668 struct xfs_ifork *cow_ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
1669
1670 /*
1671 * If the inode is already free, then there can be nothing
1672 * to clean up here.
1673 */
1674 if (VFS_I(ip)->i_mode == 0)
1675 return false;
1676
1677 /* If this is a read-only mount, don't do this (would generate I/O) */
1678 if (mp->m_flags & XFS_MOUNT_RDONLY)
1679 return false;
1680
1681 /* If the log isn't running, push inodes straight to reclaim. */
1682 if (XFS_FORCED_SHUTDOWN(mp) || (mp->m_flags & XFS_MOUNT_NORECOVERY))
1683 return false;
1684
1685 /* Metadata inodes require explicit resource cleanup. */
1686 if (xfs_is_metadata_inode(ip))
1687 return false;
1688
1689 /* Want to clean out the cow blocks if there are any. */
1690 if (cow_ifp && cow_ifp->if_bytes > 0)
1691 return true;
1692
1693 /* Unlinked files must be freed. */
1694 if (VFS_I(ip)->i_nlink == 0)
1695 return true;
1696
1697 /*
1698 * This file isn't being freed, so check if there are post-eof blocks
1699 * to free. @force is true because we are evicting an inode from the
1700 * cache. Post-eof blocks must be freed, lest we end up with broken
1701 * free space accounting.
1702 *
1703 * Note: don't bother with iolock here since lockdep complains about
1704 * acquiring it in reclaim context. We have the only reference to the
1705 * inode at this point anyways.
1706 */
1707 return xfs_can_free_eofblocks(ip, true);
1708}
1709
c24b5dfa
DC
1710/*
1711 * xfs_inactive
1712 *
1713 * This is called when the vnode reference count for the vnode
1714 * goes to zero. If the file has been unlinked, then it must
1715 * now be truncated. Also, we clear all of the read-ahead state
1716 * kept for the inode here since the file is now closed.
1717 */
74564fb4 1718void
c24b5dfa
DC
1719xfs_inactive(
1720 xfs_inode_t *ip)
1721{
3d3c8b52 1722 struct xfs_mount *mp;
3d3c8b52
JL
1723 int error;
1724 int truncate = 0;
c24b5dfa
DC
1725
1726 /*
1727 * If the inode is already free, then there can be nothing
1728 * to clean up here.
1729 */
c19b3b05 1730 if (VFS_I(ip)->i_mode == 0) {
c24b5dfa 1731 ASSERT(ip->i_df.if_broot_bytes == 0);
3ea06d73 1732 goto out;
c24b5dfa
DC
1733 }
1734
1735 mp = ip->i_mount;
17c12bcd 1736 ASSERT(!xfs_iflags_test(ip, XFS_IRECOVERY));
c24b5dfa 1737
c24b5dfa
DC
1738 /* If this is a read-only mount, don't do this (would generate I/O) */
1739 if (mp->m_flags & XFS_MOUNT_RDONLY)
3ea06d73 1740 goto out;
c24b5dfa 1741
383e32b0
DW
1742 /* Metadata inodes require explicit resource cleanup. */
1743 if (xfs_is_metadata_inode(ip))
3ea06d73 1744 goto out;
383e32b0 1745
6231848c 1746 /* Try to clean out the cow blocks if there are any. */
51d62690 1747 if (xfs_inode_has_cow_data(ip))
6231848c
DW
1748 xfs_reflink_cancel_cow_range(ip, 0, NULLFILEOFF, true);
1749
54d7b5c1 1750 if (VFS_I(ip)->i_nlink != 0) {
c24b5dfa
DC
1751 /*
1752 * force is true because we are evicting an inode from the
1753 * cache. Post-eof blocks must be freed, lest we end up with
1754 * broken free space accounting.
3b4683c2
BF
1755 *
1756 * Note: don't bother with iolock here since lockdep complains
1757 * about acquiring it in reclaim context. We have the only
1758 * reference to the inode at this point anyways.
c24b5dfa 1759 */
3b4683c2 1760 if (xfs_can_free_eofblocks(ip, true))
a36b9261 1761 xfs_free_eofblocks(ip);
74564fb4 1762
3ea06d73 1763 goto out;
c24b5dfa
DC
1764 }
1765
c19b3b05 1766 if (S_ISREG(VFS_I(ip)->i_mode) &&
13d2c10b 1767 (ip->i_disk_size != 0 || XFS_ISIZE(ip) != 0 ||
daf83964 1768 ip->i_df.if_nextents > 0 || ip->i_delayed_blks > 0))
c24b5dfa
DC
1769 truncate = 1;
1770
c14cfcca 1771 error = xfs_qm_dqattach(ip);
c24b5dfa 1772 if (error)
3ea06d73 1773 goto out;
c24b5dfa 1774
c19b3b05 1775 if (S_ISLNK(VFS_I(ip)->i_mode))
36b21dde 1776 error = xfs_inactive_symlink(ip);
f7be2d7f
BF
1777 else if (truncate)
1778 error = xfs_inactive_truncate(ip);
1779 if (error)
3ea06d73 1780 goto out;
c24b5dfa
DC
1781
1782 /*
1783 * If there are attributes associated with the file then blow them away
1784 * now. The code calls a routine that recursively deconstructs the
6dfe5a04 1785 * attribute fork. If also blows away the in-core attribute fork.
c24b5dfa 1786 */
6dfe5a04 1787 if (XFS_IFORK_Q(ip)) {
c24b5dfa
DC
1788 error = xfs_attr_inactive(ip);
1789 if (error)
3ea06d73 1790 goto out;
c24b5dfa
DC
1791 }
1792
6dfe5a04 1793 ASSERT(!ip->i_afp);
7821ea30 1794 ASSERT(ip->i_forkoff == 0);
c24b5dfa
DC
1795
1796 /*
1797 * Free the inode.
1798 */
3ea06d73 1799 xfs_inactive_ifree(ip);
c24b5dfa 1800
3ea06d73 1801out:
c24b5dfa 1802 /*
3ea06d73
DW
1803 * We're done making metadata updates for this inode, so we can release
1804 * the attached dquots.
c24b5dfa
DC
1805 */
1806 xfs_qm_dqdetach(ip);
c24b5dfa
DC
1807}
1808
9b247179
DW
1809/*
1810 * In-Core Unlinked List Lookups
1811 * =============================
1812 *
1813 * Every inode is supposed to be reachable from some other piece of metadata
1814 * with the exception of the root directory. Inodes with a connection to a
1815 * file descriptor but not linked from anywhere in the on-disk directory tree
1816 * are collectively known as unlinked inodes, though the filesystem itself
1817 * maintains links to these inodes so that on-disk metadata are consistent.
1818 *
1819 * XFS implements a per-AG on-disk hash table of unlinked inodes. The AGI
1820 * header contains a number of buckets that point to an inode, and each inode
1821 * record has a pointer to the next inode in the hash chain. This
1822 * singly-linked list causes scaling problems in the iunlink remove function
1823 * because we must walk that list to find the inode that points to the inode
1824 * being removed from the unlinked hash bucket list.
1825 *
1826 * What if we modelled the unlinked list as a collection of records capturing
1827 * "X.next_unlinked = Y" relations? If we indexed those records on Y, we'd
1828 * have a fast way to look up unlinked list predecessors, which avoids the
1829 * slow list walk. That's exactly what we do here (in-core) with a per-AG
1830 * rhashtable.
1831 *
1832 * Because this is a backref cache, we ignore operational failures since the
1833 * iunlink code can fall back to the slow bucket walk. The only errors that
1834 * should bubble out are for obviously incorrect situations.
1835 *
1836 * All users of the backref cache MUST hold the AGI buffer lock to serialize
1837 * access or have otherwise provided for concurrency control.
1838 */
1839
1840/* Capture a "X.next_unlinked = Y" relationship. */
1841struct xfs_iunlink {
1842 struct rhash_head iu_rhash_head;
1843 xfs_agino_t iu_agino; /* X */
1844 xfs_agino_t iu_next_unlinked; /* Y */
1845};
1846
1847/* Unlinked list predecessor lookup hashtable construction */
1848static int
1849xfs_iunlink_obj_cmpfn(
1850 struct rhashtable_compare_arg *arg,
1851 const void *obj)
1852{
1853 const xfs_agino_t *key = arg->key;
1854 const struct xfs_iunlink *iu = obj;
1855
1856 if (iu->iu_next_unlinked != *key)
1857 return 1;
1858 return 0;
1859}
1860
1861static const struct rhashtable_params xfs_iunlink_hash_params = {
1862 .min_size = XFS_AGI_UNLINKED_BUCKETS,
1863 .key_len = sizeof(xfs_agino_t),
1864 .key_offset = offsetof(struct xfs_iunlink,
1865 iu_next_unlinked),
1866 .head_offset = offsetof(struct xfs_iunlink, iu_rhash_head),
1867 .automatic_shrinking = true,
1868 .obj_cmpfn = xfs_iunlink_obj_cmpfn,
1869};
1870
1871/*
1872 * Return X, where X.next_unlinked == @agino. Returns NULLAGINO if no such
1873 * relation is found.
1874 */
1875static xfs_agino_t
1876xfs_iunlink_lookup_backref(
1877 struct xfs_perag *pag,
1878 xfs_agino_t agino)
1879{
1880 struct xfs_iunlink *iu;
1881
1882 iu = rhashtable_lookup_fast(&pag->pagi_unlinked_hash, &agino,
1883 xfs_iunlink_hash_params);
1884 return iu ? iu->iu_agino : NULLAGINO;
1885}
1886
1887/*
1888 * Take ownership of an iunlink cache entry and insert it into the hash table.
1889 * If successful, the entry will be owned by the cache; if not, it is freed.
1890 * Either way, the caller does not own @iu after this call.
1891 */
1892static int
1893xfs_iunlink_insert_backref(
1894 struct xfs_perag *pag,
1895 struct xfs_iunlink *iu)
1896{
1897 int error;
1898
1899 error = rhashtable_insert_fast(&pag->pagi_unlinked_hash,
1900 &iu->iu_rhash_head, xfs_iunlink_hash_params);
1901 /*
1902 * Fail loudly if there already was an entry because that's a sign of
1903 * corruption of in-memory data. Also fail loudly if we see an error
1904 * code we didn't anticipate from the rhashtable code. Currently we
1905 * only anticipate ENOMEM.
1906 */
1907 if (error) {
1908 WARN(error != -ENOMEM, "iunlink cache insert error %d", error);
1909 kmem_free(iu);
1910 }
1911 /*
1912 * Absorb any runtime errors that aren't a result of corruption because
1913 * this is a cache and we can always fall back to bucket list scanning.
1914 */
1915 if (error != 0 && error != -EEXIST)
1916 error = 0;
1917 return error;
1918}
1919
1920/* Remember that @prev_agino.next_unlinked = @this_agino. */
1921static int
1922xfs_iunlink_add_backref(
1923 struct xfs_perag *pag,
1924 xfs_agino_t prev_agino,
1925 xfs_agino_t this_agino)
1926{
1927 struct xfs_iunlink *iu;
1928
1929 if (XFS_TEST_ERROR(false, pag->pag_mount, XFS_ERRTAG_IUNLINK_FALLBACK))
1930 return 0;
1931
707e0dda 1932 iu = kmem_zalloc(sizeof(*iu), KM_NOFS);
9b247179
DW
1933 iu->iu_agino = prev_agino;
1934 iu->iu_next_unlinked = this_agino;
1935
1936 return xfs_iunlink_insert_backref(pag, iu);
1937}
1938
1939/*
1940 * Replace X.next_unlinked = @agino with X.next_unlinked = @next_unlinked.
1941 * If @next_unlinked is NULLAGINO, we drop the backref and exit. If there
1942 * wasn't any such entry then we don't bother.
1943 */
1944static int
1945xfs_iunlink_change_backref(
1946 struct xfs_perag *pag,
1947 xfs_agino_t agino,
1948 xfs_agino_t next_unlinked)
1949{
1950 struct xfs_iunlink *iu;
1951 int error;
1952
1953 /* Look up the old entry; if there wasn't one then exit. */
1954 iu = rhashtable_lookup_fast(&pag->pagi_unlinked_hash, &agino,
1955 xfs_iunlink_hash_params);
1956 if (!iu)
1957 return 0;
1958
1959 /*
1960 * Remove the entry. This shouldn't ever return an error, but if we
1961 * couldn't remove the old entry we don't want to add it again to the
1962 * hash table, and if the entry disappeared on us then someone's
1963 * violated the locking rules and we need to fail loudly. Either way
1964 * we cannot remove the inode because internal state is or would have
1965 * been corrupt.
1966 */
1967 error = rhashtable_remove_fast(&pag->pagi_unlinked_hash,
1968 &iu->iu_rhash_head, xfs_iunlink_hash_params);
1969 if (error)
1970 return error;
1971
1972 /* If there is no new next entry just free our item and return. */
1973 if (next_unlinked == NULLAGINO) {
1974 kmem_free(iu);
1975 return 0;
1976 }
1977
1978 /* Update the entry and re-add it to the hash table. */
1979 iu->iu_next_unlinked = next_unlinked;
1980 return xfs_iunlink_insert_backref(pag, iu);
1981}
1982
1983/* Set up the in-core predecessor structures. */
1984int
1985xfs_iunlink_init(
1986 struct xfs_perag *pag)
1987{
1988 return rhashtable_init(&pag->pagi_unlinked_hash,
1989 &xfs_iunlink_hash_params);
1990}
1991
1992/* Free the in-core predecessor structures. */
1993static void
1994xfs_iunlink_free_item(
1995 void *ptr,
1996 void *arg)
1997{
1998 struct xfs_iunlink *iu = ptr;
1999 bool *freed_anything = arg;
2000
2001 *freed_anything = true;
2002 kmem_free(iu);
2003}
2004
2005void
2006xfs_iunlink_destroy(
2007 struct xfs_perag *pag)
2008{
2009 bool freed_anything = false;
2010
2011 rhashtable_free_and_destroy(&pag->pagi_unlinked_hash,
2012 xfs_iunlink_free_item, &freed_anything);
2013
2014 ASSERT(freed_anything == false || XFS_FORCED_SHUTDOWN(pag->pag_mount));
2015}
2016
9a4a5118
DW
2017/*
2018 * Point the AGI unlinked bucket at an inode and log the results. The caller
2019 * is responsible for validating the old value.
2020 */
2021STATIC int
2022xfs_iunlink_update_bucket(
2023 struct xfs_trans *tp,
f40aadb2 2024 struct xfs_perag *pag,
9a4a5118
DW
2025 struct xfs_buf *agibp,
2026 unsigned int bucket_index,
2027 xfs_agino_t new_agino)
2028{
370c782b 2029 struct xfs_agi *agi = agibp->b_addr;
9a4a5118
DW
2030 xfs_agino_t old_value;
2031 int offset;
2032
f40aadb2 2033 ASSERT(xfs_verify_agino_or_null(tp->t_mountp, pag->pag_agno, new_agino));
9a4a5118
DW
2034
2035 old_value = be32_to_cpu(agi->agi_unlinked[bucket_index]);
f40aadb2 2036 trace_xfs_iunlink_update_bucket(tp->t_mountp, pag->pag_agno, bucket_index,
9a4a5118
DW
2037 old_value, new_agino);
2038
2039 /*
2040 * We should never find the head of the list already set to the value
2041 * passed in because either we're adding or removing ourselves from the
2042 * head of the list.
2043 */
a5155b87 2044 if (old_value == new_agino) {
8d57c216 2045 xfs_buf_mark_corrupt(agibp);
9a4a5118 2046 return -EFSCORRUPTED;
a5155b87 2047 }
9a4a5118
DW
2048
2049 agi->agi_unlinked[bucket_index] = cpu_to_be32(new_agino);
2050 offset = offsetof(struct xfs_agi, agi_unlinked) +
2051 (sizeof(xfs_agino_t) * bucket_index);
2052 xfs_trans_log_buf(tp, agibp, offset, offset + sizeof(xfs_agino_t) - 1);
2053 return 0;
2054}
2055
f2fc16a3
DW
2056/* Set an on-disk inode's next_unlinked pointer. */
2057STATIC void
2058xfs_iunlink_update_dinode(
2059 struct xfs_trans *tp,
f40aadb2 2060 struct xfs_perag *pag,
f2fc16a3
DW
2061 xfs_agino_t agino,
2062 struct xfs_buf *ibp,
2063 struct xfs_dinode *dip,
2064 struct xfs_imap *imap,
2065 xfs_agino_t next_agino)
2066{
2067 struct xfs_mount *mp = tp->t_mountp;
2068 int offset;
2069
f40aadb2 2070 ASSERT(xfs_verify_agino_or_null(mp, pag->pag_agno, next_agino));
f2fc16a3 2071
f40aadb2 2072 trace_xfs_iunlink_update_dinode(mp, pag->pag_agno, agino,
f2fc16a3
DW
2073 be32_to_cpu(dip->di_next_unlinked), next_agino);
2074
2075 dip->di_next_unlinked = cpu_to_be32(next_agino);
2076 offset = imap->im_boffset +
2077 offsetof(struct xfs_dinode, di_next_unlinked);
2078
2079 /* need to recalc the inode CRC if appropriate */
2080 xfs_dinode_calc_crc(mp, dip);
2081 xfs_trans_inode_buf(tp, ibp);
2082 xfs_trans_log_buf(tp, ibp, offset, offset + sizeof(xfs_agino_t) - 1);
f2fc16a3
DW
2083}
2084
2085/* Set an in-core inode's unlinked pointer and return the old value. */
2086STATIC int
2087xfs_iunlink_update_inode(
2088 struct xfs_trans *tp,
2089 struct xfs_inode *ip,
f40aadb2 2090 struct xfs_perag *pag,
f2fc16a3
DW
2091 xfs_agino_t next_agino,
2092 xfs_agino_t *old_next_agino)
2093{
2094 struct xfs_mount *mp = tp->t_mountp;
2095 struct xfs_dinode *dip;
2096 struct xfs_buf *ibp;
2097 xfs_agino_t old_value;
2098 int error;
2099
f40aadb2 2100 ASSERT(xfs_verify_agino_or_null(mp, pag->pag_agno, next_agino));
f2fc16a3 2101
af9dcdde 2102 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &ibp);
f2fc16a3
DW
2103 if (error)
2104 return error;
af9dcdde 2105 dip = xfs_buf_offset(ibp, ip->i_imap.im_boffset);
f2fc16a3
DW
2106
2107 /* Make sure the old pointer isn't garbage. */
2108 old_value = be32_to_cpu(dip->di_next_unlinked);
f40aadb2 2109 if (!xfs_verify_agino_or_null(mp, pag->pag_agno, old_value)) {
a5155b87
DW
2110 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, dip,
2111 sizeof(*dip), __this_address);
f2fc16a3
DW
2112 error = -EFSCORRUPTED;
2113 goto out;
2114 }
2115
2116 /*
2117 * Since we're updating a linked list, we should never find that the
2118 * current pointer is the same as the new value, unless we're
2119 * terminating the list.
2120 */
2121 *old_next_agino = old_value;
2122 if (old_value == next_agino) {
a5155b87
DW
2123 if (next_agino != NULLAGINO) {
2124 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__,
2125 dip, sizeof(*dip), __this_address);
f2fc16a3 2126 error = -EFSCORRUPTED;
a5155b87 2127 }
f2fc16a3
DW
2128 goto out;
2129 }
2130
2131 /* Ok, update the new pointer. */
f40aadb2 2132 xfs_iunlink_update_dinode(tp, pag, XFS_INO_TO_AGINO(mp, ip->i_ino),
f2fc16a3
DW
2133 ibp, dip, &ip->i_imap, next_agino);
2134 return 0;
2135out:
2136 xfs_trans_brelse(tp, ibp);
2137 return error;
2138}
2139
1da177e4 2140/*
c4a6bf7f
DW
2141 * This is called when the inode's link count has gone to 0 or we are creating
2142 * a tmpfile via O_TMPFILE. The inode @ip must have nlink == 0.
54d7b5c1
DC
2143 *
2144 * We place the on-disk inode on a list in the AGI. It will be pulled from this
2145 * list when the inode is freed.
1da177e4 2146 */
54d7b5c1 2147STATIC int
1da177e4 2148xfs_iunlink(
5837f625
DW
2149 struct xfs_trans *tp,
2150 struct xfs_inode *ip)
1da177e4 2151{
5837f625 2152 struct xfs_mount *mp = tp->t_mountp;
f40aadb2 2153 struct xfs_perag *pag;
5837f625 2154 struct xfs_agi *agi;
5837f625 2155 struct xfs_buf *agibp;
86bfd375 2156 xfs_agino_t next_agino;
5837f625
DW
2157 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
2158 short bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
5837f625 2159 int error;
1da177e4 2160
c4a6bf7f 2161 ASSERT(VFS_I(ip)->i_nlink == 0);
c19b3b05 2162 ASSERT(VFS_I(ip)->i_mode != 0);
4664c66c 2163 trace_xfs_iunlink(ip);
1da177e4 2164
f40aadb2
DC
2165 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
2166
5837f625 2167 /* Get the agi buffer first. It ensures lock ordering on the list. */
f40aadb2 2168 error = xfs_read_agi(mp, tp, pag->pag_agno, &agibp);
859d7182 2169 if (error)
f40aadb2 2170 goto out;
370c782b 2171 agi = agibp->b_addr;
5e1be0fb 2172
1da177e4 2173 /*
86bfd375
DW
2174 * Get the index into the agi hash table for the list this inode will
2175 * go on. Make sure the pointer isn't garbage and that this inode
2176 * isn't already on the list.
1da177e4 2177 */
86bfd375
DW
2178 next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
2179 if (next_agino == agino ||
f40aadb2 2180 !xfs_verify_agino_or_null(mp, pag->pag_agno, next_agino)) {
8d57c216 2181 xfs_buf_mark_corrupt(agibp);
f40aadb2
DC
2182 error = -EFSCORRUPTED;
2183 goto out;
a5155b87 2184 }
1da177e4 2185
86bfd375 2186 if (next_agino != NULLAGINO) {
9b247179 2187 xfs_agino_t old_agino;
f2fc16a3 2188
1da177e4 2189 /*
f2fc16a3
DW
2190 * There is already another inode in the bucket, so point this
2191 * inode to the current head of the list.
1da177e4 2192 */
f40aadb2 2193 error = xfs_iunlink_update_inode(tp, ip, pag, next_agino,
f2fc16a3 2194 &old_agino);
c319b58b 2195 if (error)
f40aadb2 2196 goto out;
f2fc16a3 2197 ASSERT(old_agino == NULLAGINO);
9b247179
DW
2198
2199 /*
2200 * agino has been unlinked, add a backref from the next inode
2201 * back to agino.
2202 */
f40aadb2 2203 error = xfs_iunlink_add_backref(pag, agino, next_agino);
9b247179 2204 if (error)
f40aadb2 2205 goto out;
1da177e4
LT
2206 }
2207
9a4a5118 2208 /* Point the head of the list to point to this inode. */
f40aadb2
DC
2209 error = xfs_iunlink_update_bucket(tp, pag, agibp, bucket_index, agino);
2210out:
2211 xfs_perag_put(pag);
2212 return error;
1da177e4
LT
2213}
2214
23ffa52c
DW
2215/* Return the imap, dinode pointer, and buffer for an inode. */
2216STATIC int
2217xfs_iunlink_map_ino(
2218 struct xfs_trans *tp,
2219 xfs_agnumber_t agno,
2220 xfs_agino_t agino,
2221 struct xfs_imap *imap,
2222 struct xfs_dinode **dipp,
2223 struct xfs_buf **bpp)
2224{
2225 struct xfs_mount *mp = tp->t_mountp;
2226 int error;
2227
2228 imap->im_blkno = 0;
2229 error = xfs_imap(mp, tp, XFS_AGINO_TO_INO(mp, agno, agino), imap, 0);
2230 if (error) {
2231 xfs_warn(mp, "%s: xfs_imap returned error %d.",
2232 __func__, error);
2233 return error;
2234 }
2235
af9dcdde 2236 error = xfs_imap_to_bp(mp, tp, imap, bpp);
23ffa52c
DW
2237 if (error) {
2238 xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
2239 __func__, error);
2240 return error;
2241 }
2242
af9dcdde 2243 *dipp = xfs_buf_offset(*bpp, imap->im_boffset);
23ffa52c
DW
2244 return 0;
2245}
2246
2247/*
2248 * Walk the unlinked chain from @head_agino until we find the inode that
2249 * points to @target_agino. Return the inode number, map, dinode pointer,
2250 * and inode cluster buffer of that inode as @agino, @imap, @dipp, and @bpp.
2251 *
2252 * @tp, @pag, @head_agino, and @target_agino are input parameters.
2253 * @agino, @imap, @dipp, and @bpp are all output parameters.
2254 *
2255 * Do not call this function if @target_agino is the head of the list.
2256 */
2257STATIC int
2258xfs_iunlink_map_prev(
2259 struct xfs_trans *tp,
f40aadb2 2260 struct xfs_perag *pag,
23ffa52c
DW
2261 xfs_agino_t head_agino,
2262 xfs_agino_t target_agino,
2263 xfs_agino_t *agino,
2264 struct xfs_imap *imap,
2265 struct xfs_dinode **dipp,
f40aadb2 2266 struct xfs_buf **bpp)
23ffa52c
DW
2267{
2268 struct xfs_mount *mp = tp->t_mountp;
2269 xfs_agino_t next_agino;
2270 int error;
2271
2272 ASSERT(head_agino != target_agino);
2273 *bpp = NULL;
2274
9b247179
DW
2275 /* See if our backref cache can find it faster. */
2276 *agino = xfs_iunlink_lookup_backref(pag, target_agino);
2277 if (*agino != NULLAGINO) {
f40aadb2
DC
2278 error = xfs_iunlink_map_ino(tp, pag->pag_agno, *agino, imap,
2279 dipp, bpp);
9b247179
DW
2280 if (error)
2281 return error;
2282
2283 if (be32_to_cpu((*dipp)->di_next_unlinked) == target_agino)
2284 return 0;
2285
2286 /*
2287 * If we get here the cache contents were corrupt, so drop the
2288 * buffer and fall back to walking the bucket list.
2289 */
2290 xfs_trans_brelse(tp, *bpp);
2291 *bpp = NULL;
2292 WARN_ON_ONCE(1);
2293 }
2294
f40aadb2 2295 trace_xfs_iunlink_map_prev_fallback(mp, pag->pag_agno);
9b247179
DW
2296
2297 /* Otherwise, walk the entire bucket until we find it. */
23ffa52c
DW
2298 next_agino = head_agino;
2299 while (next_agino != target_agino) {
2300 xfs_agino_t unlinked_agino;
2301
2302 if (*bpp)
2303 xfs_trans_brelse(tp, *bpp);
2304
2305 *agino = next_agino;
f40aadb2
DC
2306 error = xfs_iunlink_map_ino(tp, pag->pag_agno, next_agino, imap,
2307 dipp, bpp);
23ffa52c
DW
2308 if (error)
2309 return error;
2310
2311 unlinked_agino = be32_to_cpu((*dipp)->di_next_unlinked);
2312 /*
2313 * Make sure this pointer is valid and isn't an obvious
2314 * infinite loop.
2315 */
f40aadb2 2316 if (!xfs_verify_agino(mp, pag->pag_agno, unlinked_agino) ||
23ffa52c
DW
2317 next_agino == unlinked_agino) {
2318 XFS_CORRUPTION_ERROR(__func__,
2319 XFS_ERRLEVEL_LOW, mp,
2320 *dipp, sizeof(**dipp));
2321 error = -EFSCORRUPTED;
2322 return error;
2323 }
2324 next_agino = unlinked_agino;
2325 }
2326
2327 return 0;
2328}
2329
1da177e4
LT
2330/*
2331 * Pull the on-disk inode from the AGI unlinked list.
2332 */
2333STATIC int
2334xfs_iunlink_remove(
5837f625 2335 struct xfs_trans *tp,
f40aadb2 2336 struct xfs_perag *pag,
5837f625 2337 struct xfs_inode *ip)
1da177e4 2338{
5837f625
DW
2339 struct xfs_mount *mp = tp->t_mountp;
2340 struct xfs_agi *agi;
5837f625 2341 struct xfs_buf *agibp;
5837f625
DW
2342 struct xfs_buf *last_ibp;
2343 struct xfs_dinode *last_dip = NULL;
5837f625
DW
2344 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
2345 xfs_agino_t next_agino;
b1d2a068 2346 xfs_agino_t head_agino;
5837f625 2347 short bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
5837f625 2348 int error;
1da177e4 2349
4664c66c
DW
2350 trace_xfs_iunlink_remove(ip);
2351
5837f625 2352 /* Get the agi buffer first. It ensures lock ordering on the list. */
f40aadb2 2353 error = xfs_read_agi(mp, tp, pag->pag_agno, &agibp);
5e1be0fb 2354 if (error)
1da177e4 2355 return error;
370c782b 2356 agi = agibp->b_addr;
5e1be0fb 2357
1da177e4 2358 /*
86bfd375
DW
2359 * Get the index into the agi hash table for the list this inode will
2360 * go on. Make sure the head pointer isn't garbage.
1da177e4 2361 */
b1d2a068 2362 head_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
f40aadb2 2363 if (!xfs_verify_agino(mp, pag->pag_agno, head_agino)) {
d2e73665
DW
2364 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
2365 agi, sizeof(*agi));
2366 return -EFSCORRUPTED;
2367 }
1da177e4 2368
b1d2a068
DW
2369 /*
2370 * Set our inode's next_unlinked pointer to NULL and then return
2371 * the old pointer value so that we can update whatever was previous
2372 * to us in the list to point to whatever was next in the list.
2373 */
f40aadb2 2374 error = xfs_iunlink_update_inode(tp, ip, pag, NULLAGINO, &next_agino);
b1d2a068
DW
2375 if (error)
2376 return error;
9a4a5118 2377
9b247179
DW
2378 /*
2379 * If there was a backref pointing from the next inode back to this
2380 * one, remove it because we've removed this inode from the list.
2381 *
2382 * Later, if this inode was in the middle of the list we'll update
2383 * this inode's backref to point from the next inode.
2384 */
2385 if (next_agino != NULLAGINO) {
f40aadb2 2386 error = xfs_iunlink_change_backref(pag, next_agino, NULLAGINO);
9b247179 2387 if (error)
92a00544 2388 return error;
9b247179
DW
2389 }
2390
92a00544 2391 if (head_agino != agino) {
f2fc16a3
DW
2392 struct xfs_imap imap;
2393 xfs_agino_t prev_agino;
2394
23ffa52c 2395 /* We need to search the list for the inode being freed. */
f40aadb2
DC
2396 error = xfs_iunlink_map_prev(tp, pag, head_agino, agino,
2397 &prev_agino, &imap, &last_dip, &last_ibp);
23ffa52c 2398 if (error)
92a00544 2399 return error;
475ee413 2400
f2fc16a3 2401 /* Point the previous inode on the list to the next inode. */
f40aadb2 2402 xfs_iunlink_update_dinode(tp, pag, prev_agino, last_ibp,
f2fc16a3 2403 last_dip, &imap, next_agino);
9b247179
DW
2404
2405 /*
2406 * Now we deal with the backref for this inode. If this inode
2407 * pointed at a real inode, change the backref that pointed to
2408 * us to point to our old next. If this inode was the end of
2409 * the list, delete the backref that pointed to us. Note that
2410 * change_backref takes care of deleting the backref if
2411 * next_agino is NULLAGINO.
2412 */
92a00544
GX
2413 return xfs_iunlink_change_backref(agibp->b_pag, agino,
2414 next_agino);
1da177e4 2415 }
9b247179 2416
92a00544 2417 /* Point the head of the list to the next unlinked inode. */
f40aadb2 2418 return xfs_iunlink_update_bucket(tp, pag, agibp, bucket_index,
92a00544 2419 next_agino);
1da177e4
LT
2420}
2421
5806165a 2422/*
71e3e356
DC
2423 * Look up the inode number specified and if it is not already marked XFS_ISTALE
2424 * mark it stale. We should only find clean inodes in this lookup that aren't
2425 * already stale.
5806165a 2426 */
71e3e356
DC
2427static void
2428xfs_ifree_mark_inode_stale(
f40aadb2 2429 struct xfs_perag *pag,
5806165a 2430 struct xfs_inode *free_ip,
d9fdd0ad 2431 xfs_ino_t inum)
5806165a 2432{
f40aadb2 2433 struct xfs_mount *mp = pag->pag_mount;
71e3e356 2434 struct xfs_inode_log_item *iip;
5806165a
DC
2435 struct xfs_inode *ip;
2436
2437retry:
2438 rcu_read_lock();
2439 ip = radix_tree_lookup(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, inum));
2440
2441 /* Inode not in memory, nothing to do */
71e3e356
DC
2442 if (!ip) {
2443 rcu_read_unlock();
2444 return;
2445 }
5806165a
DC
2446
2447 /*
2448 * because this is an RCU protected lookup, we could find a recently
2449 * freed or even reallocated inode during the lookup. We need to check
2450 * under the i_flags_lock for a valid inode here. Skip it if it is not
2451 * valid, the wrong inode or stale.
2452 */
2453 spin_lock(&ip->i_flags_lock);
718ecc50
DC
2454 if (ip->i_ino != inum || __xfs_iflags_test(ip, XFS_ISTALE))
2455 goto out_iflags_unlock;
5806165a
DC
2456
2457 /*
2458 * Don't try to lock/unlock the current inode, but we _cannot_ skip the
2459 * other inodes that we did not find in the list attached to the buffer
2460 * and are not already marked stale. If we can't lock it, back off and
2461 * retry.
2462 */
2463 if (ip != free_ip) {
2464 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
71e3e356 2465 spin_unlock(&ip->i_flags_lock);
5806165a
DC
2466 rcu_read_unlock();
2467 delay(1);
2468 goto retry;
2469 }
5806165a 2470 }
71e3e356 2471 ip->i_flags |= XFS_ISTALE;
5806165a 2472
71e3e356 2473 /*
718ecc50 2474 * If the inode is flushing, it is already attached to the buffer. All
71e3e356
DC
2475 * we needed to do here is mark the inode stale so buffer IO completion
2476 * will remove it from the AIL.
2477 */
2478 iip = ip->i_itemp;
718ecc50 2479 if (__xfs_iflags_test(ip, XFS_IFLUSHING)) {
71e3e356
DC
2480 ASSERT(!list_empty(&iip->ili_item.li_bio_list));
2481 ASSERT(iip->ili_last_fields);
2482 goto out_iunlock;
2483 }
5806165a
DC
2484
2485 /*
48d55e2a
DC
2486 * Inodes not attached to the buffer can be released immediately.
2487 * Everything else has to go through xfs_iflush_abort() on journal
2488 * commit as the flock synchronises removal of the inode from the
2489 * cluster buffer against inode reclaim.
5806165a 2490 */
718ecc50 2491 if (!iip || list_empty(&iip->ili_item.li_bio_list))
71e3e356 2492 goto out_iunlock;
718ecc50
DC
2493
2494 __xfs_iflags_set(ip, XFS_IFLUSHING);
2495 spin_unlock(&ip->i_flags_lock);
2496 rcu_read_unlock();
5806165a 2497
71e3e356 2498 /* we have a dirty inode in memory that has not yet been flushed. */
71e3e356
DC
2499 spin_lock(&iip->ili_lock);
2500 iip->ili_last_fields = iip->ili_fields;
2501 iip->ili_fields = 0;
2502 iip->ili_fsync_fields = 0;
2503 spin_unlock(&iip->ili_lock);
71e3e356
DC
2504 ASSERT(iip->ili_last_fields);
2505
718ecc50
DC
2506 if (ip != free_ip)
2507 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2508 return;
2509
71e3e356
DC
2510out_iunlock:
2511 if (ip != free_ip)
2512 xfs_iunlock(ip, XFS_ILOCK_EXCL);
718ecc50
DC
2513out_iflags_unlock:
2514 spin_unlock(&ip->i_flags_lock);
2515 rcu_read_unlock();
5806165a
DC
2516}
2517
5b3eed75 2518/*
0b8182db 2519 * A big issue when freeing the inode cluster is that we _cannot_ skip any
5b3eed75
DC
2520 * inodes that are in memory - they all must be marked stale and attached to
2521 * the cluster buffer.
2522 */
f40aadb2 2523static int
1da177e4 2524xfs_ifree_cluster(
71e3e356 2525 struct xfs_trans *tp,
f40aadb2
DC
2526 struct xfs_perag *pag,
2527 struct xfs_inode *free_ip,
09b56604 2528 struct xfs_icluster *xic)
1da177e4 2529{
71e3e356
DC
2530 struct xfs_mount *mp = free_ip->i_mount;
2531 struct xfs_ino_geometry *igeo = M_IGEO(mp);
2532 struct xfs_buf *bp;
2533 xfs_daddr_t blkno;
2534 xfs_ino_t inum = xic->first_ino;
1da177e4 2535 int nbufs;
5b257b4a 2536 int i, j;
3cdaa189 2537 int ioffset;
ce92464c 2538 int error;
1da177e4 2539
ef325959 2540 nbufs = igeo->ialloc_blks / igeo->blocks_per_cluster;
1da177e4 2541
ef325959 2542 for (j = 0; j < nbufs; j++, inum += igeo->inodes_per_cluster) {
09b56604
BF
2543 /*
2544 * The allocation bitmap tells us which inodes of the chunk were
2545 * physically allocated. Skip the cluster if an inode falls into
2546 * a sparse region.
2547 */
3cdaa189
BF
2548 ioffset = inum - xic->first_ino;
2549 if ((xic->alloc & XFS_INOBT_MASK(ioffset)) == 0) {
ef325959 2550 ASSERT(ioffset % igeo->inodes_per_cluster == 0);
09b56604
BF
2551 continue;
2552 }
2553
1da177e4
LT
2554 blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
2555 XFS_INO_TO_AGBNO(mp, inum));
2556
5b257b4a
DC
2557 /*
2558 * We obtain and lock the backing buffer first in the process
718ecc50
DC
2559 * here to ensure dirty inodes attached to the buffer remain in
2560 * the flushing state while we mark them stale.
2561 *
5b257b4a
DC
2562 * If we scan the in-memory inodes first, then buffer IO can
2563 * complete before we get a lock on it, and hence we may fail
2564 * to mark all the active inodes on the buffer stale.
2565 */
ce92464c
DW
2566 error = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
2567 mp->m_bsize * igeo->blocks_per_cluster,
2568 XBF_UNMAPPED, &bp);
71e3e356 2569 if (error)
ce92464c 2570 return error;
b0f539de
DC
2571
2572 /*
2573 * This buffer may not have been correctly initialised as we
2574 * didn't read it from disk. That's not important because we are
2575 * only using to mark the buffer as stale in the log, and to
2576 * attach stale cached inodes on it. That means it will never be
2577 * dispatched for IO. If it is, we want to know about it, and we
2578 * want it to fail. We can acheive this by adding a write
2579 * verifier to the buffer.
2580 */
8c4ce794 2581 bp->b_ops = &xfs_inode_buf_ops;
b0f539de 2582
5b257b4a 2583 /*
71e3e356
DC
2584 * Now we need to set all the cached clean inodes as XFS_ISTALE,
2585 * too. This requires lookups, and will skip inodes that we've
2586 * already marked XFS_ISTALE.
1da177e4 2587 */
71e3e356 2588 for (i = 0; i < igeo->inodes_per_cluster; i++)
f40aadb2 2589 xfs_ifree_mark_inode_stale(pag, free_ip, inum + i);
1da177e4 2590
5b3eed75 2591 xfs_trans_stale_inode_buf(tp, bp);
1da177e4
LT
2592 xfs_trans_binval(tp, bp);
2593 }
2a30f36d 2594 return 0;
1da177e4
LT
2595}
2596
2597/*
2598 * This is called to return an inode to the inode free list.
2599 * The inode should already be truncated to 0 length and have
2600 * no pages associated with it. This routine also assumes that
2601 * the inode is already a part of the transaction.
2602 *
2603 * The on-disk copy of the inode will have been added to the list
2604 * of unlinked inodes in the AGI. We need to remove the inode from
2605 * that list atomically with respect to freeing it here.
2606 */
2607int
2608xfs_ifree(
0e0417f3
BF
2609 struct xfs_trans *tp,
2610 struct xfs_inode *ip)
1da177e4 2611{
f40aadb2
DC
2612 struct xfs_mount *mp = ip->i_mount;
2613 struct xfs_perag *pag;
09b56604 2614 struct xfs_icluster xic = { 0 };
1319ebef 2615 struct xfs_inode_log_item *iip = ip->i_itemp;
f40aadb2 2616 int error;
1da177e4 2617
579aa9ca 2618 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
54d7b5c1 2619 ASSERT(VFS_I(ip)->i_nlink == 0);
daf83964 2620 ASSERT(ip->i_df.if_nextents == 0);
13d2c10b 2621 ASSERT(ip->i_disk_size == 0 || !S_ISREG(VFS_I(ip)->i_mode));
6e73a545 2622 ASSERT(ip->i_nblocks == 0);
1da177e4 2623
f40aadb2
DC
2624 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
2625
1da177e4
LT
2626 /*
2627 * Pull the on-disk inode from the AGI unlinked list.
2628 */
f40aadb2 2629 error = xfs_iunlink_remove(tp, pag, ip);
1baaed8f 2630 if (error)
f40aadb2 2631 goto out;
1da177e4 2632
f40aadb2 2633 error = xfs_difree(tp, pag, ip->i_ino, &xic);
1baaed8f 2634 if (error)
f40aadb2 2635 goto out;
1baaed8f 2636
b2c20045
CH
2637 /*
2638 * Free any local-format data sitting around before we reset the
2639 * data fork to extents format. Note that the attr fork data has
2640 * already been freed by xfs_attr_inactive.
2641 */
f7e67b20 2642 if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
b2c20045
CH
2643 kmem_free(ip->i_df.if_u1.if_data);
2644 ip->i_df.if_u1.if_data = NULL;
2645 ip->i_df.if_bytes = 0;
2646 }
98c4f78d 2647
c19b3b05 2648 VFS_I(ip)->i_mode = 0; /* mark incore inode as free */
db07349d 2649 ip->i_diflags = 0;
f40aadb2 2650 ip->i_diflags2 = mp->m_ino_geo.new_diflags2;
7821ea30 2651 ip->i_forkoff = 0; /* mark the attr fork not in use */
f7e67b20 2652 ip->i_df.if_format = XFS_DINODE_FMT_EXTENTS;
9b3beb02
CH
2653 if (xfs_iflags_test(ip, XFS_IPRESERVE_DM_FIELDS))
2654 xfs_iflags_clear(ip, XFS_IPRESERVE_DM_FIELDS);
dc1baa71
ES
2655
2656 /* Don't attempt to replay owner changes for a deleted inode */
1319ebef
DC
2657 spin_lock(&iip->ili_lock);
2658 iip->ili_fields &= ~(XFS_ILOG_AOWNER | XFS_ILOG_DOWNER);
2659 spin_unlock(&iip->ili_lock);
dc1baa71 2660
1da177e4
LT
2661 /*
2662 * Bump the generation count so no one will be confused
2663 * by reincarnations of this inode.
2664 */
9e9a2674 2665 VFS_I(ip)->i_generation++;
1da177e4
LT
2666 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2667
09b56604 2668 if (xic.deleted)
f40aadb2
DC
2669 error = xfs_ifree_cluster(tp, pag, ip, &xic);
2670out:
2671 xfs_perag_put(pag);
2a30f36d 2672 return error;
1da177e4
LT
2673}
2674
1da177e4 2675/*
60ec6783
CH
2676 * This is called to unpin an inode. The caller must have the inode locked
2677 * in at least shared mode so that the buffer cannot be subsequently pinned
2678 * once someone is waiting for it to be unpinned.
1da177e4 2679 */
60ec6783 2680static void
f392e631 2681xfs_iunpin(
60ec6783 2682 struct xfs_inode *ip)
1da177e4 2683{
579aa9ca 2684 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
1da177e4 2685
4aaf15d1
DC
2686 trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
2687
a3f74ffb 2688 /* Give the log a push to start the unpinning I/O */
5f9b4b0d 2689 xfs_log_force_seq(ip->i_mount, ip->i_itemp->ili_commit_seq, 0, NULL);
a14a348b 2690
a3f74ffb 2691}
1da177e4 2692
f392e631
CH
2693static void
2694__xfs_iunpin_wait(
2695 struct xfs_inode *ip)
2696{
2697 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
2698 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
2699
2700 xfs_iunpin(ip);
2701
2702 do {
21417136 2703 prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
f392e631
CH
2704 if (xfs_ipincount(ip))
2705 io_schedule();
2706 } while (xfs_ipincount(ip));
21417136 2707 finish_wait(wq, &wait.wq_entry);
f392e631
CH
2708}
2709
777df5af 2710void
a3f74ffb 2711xfs_iunpin_wait(
60ec6783 2712 struct xfs_inode *ip)
a3f74ffb 2713{
f392e631
CH
2714 if (xfs_ipincount(ip))
2715 __xfs_iunpin_wait(ip);
1da177e4
LT
2716}
2717
27320369
DC
2718/*
2719 * Removing an inode from the namespace involves removing the directory entry
2720 * and dropping the link count on the inode. Removing the directory entry can
2721 * result in locking an AGF (directory blocks were freed) and removing a link
2722 * count can result in placing the inode on an unlinked list which results in
2723 * locking an AGI.
2724 *
2725 * The big problem here is that we have an ordering constraint on AGF and AGI
2726 * locking - inode allocation locks the AGI, then can allocate a new extent for
2727 * new inodes, locking the AGF after the AGI. Similarly, freeing the inode
2728 * removes the inode from the unlinked list, requiring that we lock the AGI
2729 * first, and then freeing the inode can result in an inode chunk being freed
2730 * and hence freeing disk space requiring that we lock an AGF.
2731 *
2732 * Hence the ordering that is imposed by other parts of the code is AGI before
2733 * AGF. This means we cannot remove the directory entry before we drop the inode
2734 * reference count and put it on the unlinked list as this results in a lock
2735 * order of AGF then AGI, and this can deadlock against inode allocation and
2736 * freeing. Therefore we must drop the link counts before we remove the
2737 * directory entry.
2738 *
2739 * This is still safe from a transactional point of view - it is not until we
310a75a3 2740 * get to xfs_defer_finish() that we have the possibility of multiple
27320369
DC
2741 * transactions in this operation. Hence as long as we remove the directory
2742 * entry and drop the link count in the first transaction of the remove
2743 * operation, there are no transactional constraints on the ordering here.
2744 */
c24b5dfa
DC
2745int
2746xfs_remove(
2747 xfs_inode_t *dp,
2748 struct xfs_name *name,
2749 xfs_inode_t *ip)
2750{
2751 xfs_mount_t *mp = dp->i_mount;
2752 xfs_trans_t *tp = NULL;
c19b3b05 2753 int is_dir = S_ISDIR(VFS_I(ip)->i_mode);
c24b5dfa 2754 int error = 0;
c24b5dfa 2755 uint resblks;
c24b5dfa
DC
2756
2757 trace_xfs_remove(dp, name);
2758
2759 if (XFS_FORCED_SHUTDOWN(mp))
2451337d 2760 return -EIO;
c24b5dfa 2761
c14cfcca 2762 error = xfs_qm_dqattach(dp);
c24b5dfa
DC
2763 if (error)
2764 goto std_return;
2765
c14cfcca 2766 error = xfs_qm_dqattach(ip);
c24b5dfa
DC
2767 if (error)
2768 goto std_return;
2769
c24b5dfa
DC
2770 /*
2771 * We try to get the real space reservation first,
2772 * allowing for directory btree deletion(s) implying
2773 * possible bmap insert(s). If we can't get the space
2774 * reservation then we use 0 instead, and avoid the bmap
2775 * btree insert(s) in the directory code by, if the bmap
2776 * insert tries to happen, instead trimming the LAST
2777 * block from the directory.
2778 */
2779 resblks = XFS_REMOVE_SPACE_RES(mp);
253f4911 2780 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_remove, resblks, 0, 0, &tp);
2451337d 2781 if (error == -ENOSPC) {
c24b5dfa 2782 resblks = 0;
253f4911
CH
2783 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_remove, 0, 0, 0,
2784 &tp);
c24b5dfa
DC
2785 }
2786 if (error) {
2451337d 2787 ASSERT(error != -ENOSPC);
253f4911 2788 goto std_return;
c24b5dfa
DC
2789 }
2790
7c2d238a 2791 xfs_lock_two_inodes(dp, XFS_ILOCK_EXCL, ip, XFS_ILOCK_EXCL);
c24b5dfa 2792
65523218 2793 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
c24b5dfa
DC
2794 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2795
2796 /*
2797 * If we're removing a directory perform some additional validation.
2798 */
2799 if (is_dir) {
54d7b5c1
DC
2800 ASSERT(VFS_I(ip)->i_nlink >= 2);
2801 if (VFS_I(ip)->i_nlink != 2) {
2451337d 2802 error = -ENOTEMPTY;
c24b5dfa
DC
2803 goto out_trans_cancel;
2804 }
2805 if (!xfs_dir_isempty(ip)) {
2451337d 2806 error = -ENOTEMPTY;
c24b5dfa
DC
2807 goto out_trans_cancel;
2808 }
c24b5dfa 2809
27320369 2810 /* Drop the link from ip's "..". */
c24b5dfa
DC
2811 error = xfs_droplink(tp, dp);
2812 if (error)
27320369 2813 goto out_trans_cancel;
c24b5dfa 2814
27320369 2815 /* Drop the "." link from ip to self. */
c24b5dfa
DC
2816 error = xfs_droplink(tp, ip);
2817 if (error)
27320369 2818 goto out_trans_cancel;
5838d035
DW
2819
2820 /*
2821 * Point the unlinked child directory's ".." entry to the root
2822 * directory to eliminate back-references to inodes that may
2823 * get freed before the child directory is closed. If the fs
2824 * gets shrunk, this can lead to dirent inode validation errors.
2825 */
2826 if (dp->i_ino != tp->t_mountp->m_sb.sb_rootino) {
2827 error = xfs_dir_replace(tp, ip, &xfs_name_dotdot,
2828 tp->t_mountp->m_sb.sb_rootino, 0);
2829 if (error)
2830 return error;
2831 }
c24b5dfa
DC
2832 } else {
2833 /*
2834 * When removing a non-directory we need to log the parent
2835 * inode here. For a directory this is done implicitly
2836 * by the xfs_droplink call for the ".." entry.
2837 */
2838 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2839 }
27320369 2840 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
c24b5dfa 2841
27320369 2842 /* Drop the link from dp to ip. */
c24b5dfa
DC
2843 error = xfs_droplink(tp, ip);
2844 if (error)
27320369 2845 goto out_trans_cancel;
c24b5dfa 2846
381eee69 2847 error = xfs_dir_removename(tp, dp, name, ip->i_ino, resblks);
27320369 2848 if (error) {
2451337d 2849 ASSERT(error != -ENOENT);
c8eac49e 2850 goto out_trans_cancel;
27320369
DC
2851 }
2852
c24b5dfa
DC
2853 /*
2854 * If this is a synchronous mount, make sure that the
2855 * remove transaction goes to disk before returning to
2856 * the user.
2857 */
2858 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
2859 xfs_trans_set_sync(tp);
2860
70393313 2861 error = xfs_trans_commit(tp);
c24b5dfa
DC
2862 if (error)
2863 goto std_return;
2864
2cd2ef6a 2865 if (is_dir && xfs_inode_is_filestream(ip))
c24b5dfa
DC
2866 xfs_filestream_deassociate(ip);
2867
2868 return 0;
2869
c24b5dfa 2870 out_trans_cancel:
4906e215 2871 xfs_trans_cancel(tp);
c24b5dfa
DC
2872 std_return:
2873 return error;
2874}
2875
f6bba201
DC
2876/*
2877 * Enter all inodes for a rename transaction into a sorted array.
2878 */
95afcf5c 2879#define __XFS_SORT_INODES 5
f6bba201
DC
2880STATIC void
2881xfs_sort_for_rename(
95afcf5c
DC
2882 struct xfs_inode *dp1, /* in: old (source) directory inode */
2883 struct xfs_inode *dp2, /* in: new (target) directory inode */
2884 struct xfs_inode *ip1, /* in: inode of old entry */
2885 struct xfs_inode *ip2, /* in: inode of new entry */
2886 struct xfs_inode *wip, /* in: whiteout inode */
2887 struct xfs_inode **i_tab,/* out: sorted array of inodes */
2888 int *num_inodes) /* in/out: inodes in array */
f6bba201 2889{
f6bba201
DC
2890 int i, j;
2891
95afcf5c
DC
2892 ASSERT(*num_inodes == __XFS_SORT_INODES);
2893 memset(i_tab, 0, *num_inodes * sizeof(struct xfs_inode *));
2894
f6bba201
DC
2895 /*
2896 * i_tab contains a list of pointers to inodes. We initialize
2897 * the table here & we'll sort it. We will then use it to
2898 * order the acquisition of the inode locks.
2899 *
2900 * Note that the table may contain duplicates. e.g., dp1 == dp2.
2901 */
95afcf5c
DC
2902 i = 0;
2903 i_tab[i++] = dp1;
2904 i_tab[i++] = dp2;
2905 i_tab[i++] = ip1;
2906 if (ip2)
2907 i_tab[i++] = ip2;
2908 if (wip)
2909 i_tab[i++] = wip;
2910 *num_inodes = i;
f6bba201
DC
2911
2912 /*
2913 * Sort the elements via bubble sort. (Remember, there are at
95afcf5c 2914 * most 5 elements to sort, so this is adequate.)
f6bba201
DC
2915 */
2916 for (i = 0; i < *num_inodes; i++) {
2917 for (j = 1; j < *num_inodes; j++) {
2918 if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) {
95afcf5c 2919 struct xfs_inode *temp = i_tab[j];
f6bba201
DC
2920 i_tab[j] = i_tab[j-1];
2921 i_tab[j-1] = temp;
2922 }
2923 }
2924 }
2925}
2926
310606b0
DC
2927static int
2928xfs_finish_rename(
c9cfdb38 2929 struct xfs_trans *tp)
310606b0 2930{
310606b0
DC
2931 /*
2932 * If this is a synchronous mount, make sure that the rename transaction
2933 * goes to disk before returning to the user.
2934 */
2935 if (tp->t_mountp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
2936 xfs_trans_set_sync(tp);
2937
70393313 2938 return xfs_trans_commit(tp);
310606b0
DC
2939}
2940
d31a1825
CM
2941/*
2942 * xfs_cross_rename()
2943 *
0145225e 2944 * responsible for handling RENAME_EXCHANGE flag in renameat2() syscall
d31a1825
CM
2945 */
2946STATIC int
2947xfs_cross_rename(
2948 struct xfs_trans *tp,
2949 struct xfs_inode *dp1,
2950 struct xfs_name *name1,
2951 struct xfs_inode *ip1,
2952 struct xfs_inode *dp2,
2953 struct xfs_name *name2,
2954 struct xfs_inode *ip2,
d31a1825
CM
2955 int spaceres)
2956{
2957 int error = 0;
2958 int ip1_flags = 0;
2959 int ip2_flags = 0;
2960 int dp2_flags = 0;
2961
2962 /* Swap inode number for dirent in first parent */
381eee69 2963 error = xfs_dir_replace(tp, dp1, name1, ip2->i_ino, spaceres);
d31a1825 2964 if (error)
eeacd321 2965 goto out_trans_abort;
d31a1825
CM
2966
2967 /* Swap inode number for dirent in second parent */
381eee69 2968 error = xfs_dir_replace(tp, dp2, name2, ip1->i_ino, spaceres);
d31a1825 2969 if (error)
eeacd321 2970 goto out_trans_abort;
d31a1825
CM
2971
2972 /*
2973 * If we're renaming one or more directories across different parents,
2974 * update the respective ".." entries (and link counts) to match the new
2975 * parents.
2976 */
2977 if (dp1 != dp2) {
2978 dp2_flags = XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
2979
c19b3b05 2980 if (S_ISDIR(VFS_I(ip2)->i_mode)) {
d31a1825 2981 error = xfs_dir_replace(tp, ip2, &xfs_name_dotdot,
381eee69 2982 dp1->i_ino, spaceres);
d31a1825 2983 if (error)
eeacd321 2984 goto out_trans_abort;
d31a1825
CM
2985
2986 /* transfer ip2 ".." reference to dp1 */
c19b3b05 2987 if (!S_ISDIR(VFS_I(ip1)->i_mode)) {
d31a1825
CM
2988 error = xfs_droplink(tp, dp2);
2989 if (error)
eeacd321 2990 goto out_trans_abort;
91083269 2991 xfs_bumplink(tp, dp1);
d31a1825
CM
2992 }
2993
2994 /*
2995 * Although ip1 isn't changed here, userspace needs
2996 * to be warned about the change, so that applications
2997 * relying on it (like backup ones), will properly
2998 * notify the change
2999 */
3000 ip1_flags |= XFS_ICHGTIME_CHG;
3001 ip2_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
3002 }
3003
c19b3b05 3004 if (S_ISDIR(VFS_I(ip1)->i_mode)) {
d31a1825 3005 error = xfs_dir_replace(tp, ip1, &xfs_name_dotdot,
381eee69 3006 dp2->i_ino, spaceres);
d31a1825 3007 if (error)
eeacd321 3008 goto out_trans_abort;
d31a1825
CM
3009
3010 /* transfer ip1 ".." reference to dp2 */
c19b3b05 3011 if (!S_ISDIR(VFS_I(ip2)->i_mode)) {
d31a1825
CM
3012 error = xfs_droplink(tp, dp1);
3013 if (error)
eeacd321 3014 goto out_trans_abort;
91083269 3015 xfs_bumplink(tp, dp2);
d31a1825
CM
3016 }
3017
3018 /*
3019 * Although ip2 isn't changed here, userspace needs
3020 * to be warned about the change, so that applications
3021 * relying on it (like backup ones), will properly
3022 * notify the change
3023 */
3024 ip1_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
3025 ip2_flags |= XFS_ICHGTIME_CHG;
3026 }
3027 }
3028
3029 if (ip1_flags) {
3030 xfs_trans_ichgtime(tp, ip1, ip1_flags);
3031 xfs_trans_log_inode(tp, ip1, XFS_ILOG_CORE);
3032 }
3033 if (ip2_flags) {
3034 xfs_trans_ichgtime(tp, ip2, ip2_flags);
3035 xfs_trans_log_inode(tp, ip2, XFS_ILOG_CORE);
3036 }
3037 if (dp2_flags) {
3038 xfs_trans_ichgtime(tp, dp2, dp2_flags);
3039 xfs_trans_log_inode(tp, dp2, XFS_ILOG_CORE);
3040 }
3041 xfs_trans_ichgtime(tp, dp1, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3042 xfs_trans_log_inode(tp, dp1, XFS_ILOG_CORE);
c9cfdb38 3043 return xfs_finish_rename(tp);
eeacd321
DC
3044
3045out_trans_abort:
4906e215 3046 xfs_trans_cancel(tp);
d31a1825
CM
3047 return error;
3048}
3049
7dcf5c3e
DC
3050/*
3051 * xfs_rename_alloc_whiteout()
3052 *
b63da6c8 3053 * Return a referenced, unlinked, unlocked inode that can be used as a
7dcf5c3e
DC
3054 * whiteout in a rename transaction. We use a tmpfile inode here so that if we
3055 * crash between allocating the inode and linking it into the rename transaction
3056 * recovery will free the inode and we won't leak it.
3057 */
3058static int
3059xfs_rename_alloc_whiteout(
f736d93d 3060 struct user_namespace *mnt_userns,
7dcf5c3e
DC
3061 struct xfs_inode *dp,
3062 struct xfs_inode **wip)
3063{
3064 struct xfs_inode *tmpfile;
3065 int error;
3066
f736d93d
CH
3067 error = xfs_create_tmpfile(mnt_userns, dp, S_IFCHR | WHITEOUT_MODE,
3068 &tmpfile);
7dcf5c3e
DC
3069 if (error)
3070 return error;
3071
22419ac9
BF
3072 /*
3073 * Prepare the tmpfile inode as if it were created through the VFS.
c4a6bf7f
DW
3074 * Complete the inode setup and flag it as linkable. nlink is already
3075 * zero, so we can skip the drop_nlink.
22419ac9 3076 */
2b3d1d41 3077 xfs_setup_iops(tmpfile);
7dcf5c3e
DC
3078 xfs_finish_inode_setup(tmpfile);
3079 VFS_I(tmpfile)->i_state |= I_LINKABLE;
3080
3081 *wip = tmpfile;
3082 return 0;
3083}
3084
f6bba201
DC
3085/*
3086 * xfs_rename
3087 */
3088int
3089xfs_rename(
f736d93d 3090 struct user_namespace *mnt_userns,
7dcf5c3e
DC
3091 struct xfs_inode *src_dp,
3092 struct xfs_name *src_name,
3093 struct xfs_inode *src_ip,
3094 struct xfs_inode *target_dp,
3095 struct xfs_name *target_name,
3096 struct xfs_inode *target_ip,
3097 unsigned int flags)
f6bba201 3098{
7dcf5c3e
DC
3099 struct xfs_mount *mp = src_dp->i_mount;
3100 struct xfs_trans *tp;
7dcf5c3e
DC
3101 struct xfs_inode *wip = NULL; /* whiteout inode */
3102 struct xfs_inode *inodes[__XFS_SORT_INODES];
6da1b4b1 3103 int i;
7dcf5c3e 3104 int num_inodes = __XFS_SORT_INODES;
2b93681f 3105 bool new_parent = (src_dp != target_dp);
c19b3b05 3106 bool src_is_directory = S_ISDIR(VFS_I(src_ip)->i_mode);
7dcf5c3e
DC
3107 int spaceres;
3108 int error;
f6bba201
DC
3109
3110 trace_xfs_rename(src_dp, target_dp, src_name, target_name);
3111
eeacd321
DC
3112 if ((flags & RENAME_EXCHANGE) && !target_ip)
3113 return -EINVAL;
3114
7dcf5c3e
DC
3115 /*
3116 * If we are doing a whiteout operation, allocate the whiteout inode
3117 * we will be placing at the target and ensure the type is set
3118 * appropriately.
3119 */
3120 if (flags & RENAME_WHITEOUT) {
3121 ASSERT(!(flags & (RENAME_NOREPLACE | RENAME_EXCHANGE)));
f736d93d 3122 error = xfs_rename_alloc_whiteout(mnt_userns, target_dp, &wip);
7dcf5c3e
DC
3123 if (error)
3124 return error;
3125
3126 /* setup target dirent info as whiteout */
3127 src_name->type = XFS_DIR3_FT_CHRDEV;
3128 }
f6bba201 3129
7dcf5c3e 3130 xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip, wip,
f6bba201
DC
3131 inodes, &num_inodes);
3132
f6bba201 3133 spaceres = XFS_RENAME_SPACE_RES(mp, target_name->len);
253f4911 3134 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_rename, spaceres, 0, 0, &tp);
2451337d 3135 if (error == -ENOSPC) {
f6bba201 3136 spaceres = 0;
253f4911
CH
3137 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_rename, 0, 0, 0,
3138 &tp);
f6bba201 3139 }
445883e8 3140 if (error)
253f4911 3141 goto out_release_wip;
f6bba201
DC
3142
3143 /*
3144 * Attach the dquots to the inodes
3145 */
3146 error = xfs_qm_vop_rename_dqattach(inodes);
445883e8
DC
3147 if (error)
3148 goto out_trans_cancel;
f6bba201
DC
3149
3150 /*
3151 * Lock all the participating inodes. Depending upon whether
3152 * the target_name exists in the target directory, and
3153 * whether the target directory is the same as the source
3154 * directory, we can lock from 2 to 4 inodes.
3155 */
3156 xfs_lock_inodes(inodes, num_inodes, XFS_ILOCK_EXCL);
3157
3158 /*
3159 * Join all the inodes to the transaction. From this point on,
3160 * we can rely on either trans_commit or trans_cancel to unlock
3161 * them.
3162 */
65523218 3163 xfs_trans_ijoin(tp, src_dp, XFS_ILOCK_EXCL);
f6bba201 3164 if (new_parent)
65523218 3165 xfs_trans_ijoin(tp, target_dp, XFS_ILOCK_EXCL);
f6bba201
DC
3166 xfs_trans_ijoin(tp, src_ip, XFS_ILOCK_EXCL);
3167 if (target_ip)
3168 xfs_trans_ijoin(tp, target_ip, XFS_ILOCK_EXCL);
7dcf5c3e
DC
3169 if (wip)
3170 xfs_trans_ijoin(tp, wip, XFS_ILOCK_EXCL);
f6bba201
DC
3171
3172 /*
3173 * If we are using project inheritance, we only allow renames
3174 * into our tree when the project IDs are the same; else the
3175 * tree quota mechanism would be circumvented.
3176 */
db07349d 3177 if (unlikely((target_dp->i_diflags & XFS_DIFLAG_PROJINHERIT) &&
ceaf603c 3178 target_dp->i_projid != src_ip->i_projid)) {
2451337d 3179 error = -EXDEV;
445883e8 3180 goto out_trans_cancel;
f6bba201
DC
3181 }
3182
eeacd321
DC
3183 /* RENAME_EXCHANGE is unique from here on. */
3184 if (flags & RENAME_EXCHANGE)
3185 return xfs_cross_rename(tp, src_dp, src_name, src_ip,
3186 target_dp, target_name, target_ip,
f16dea54 3187 spaceres);
d31a1825 3188
f6bba201 3189 /*
bc56ad8c 3190 * Check for expected errors before we dirty the transaction
3191 * so we can return an error without a transaction abort.
02092a2f
CB
3192 *
3193 * Extent count overflow check:
3194 *
3195 * From the perspective of src_dp, a rename operation is essentially a
3196 * directory entry remove operation. Hence the only place where we check
3197 * for extent count overflow for src_dp is in
3198 * xfs_bmap_del_extent_real(). xfs_bmap_del_extent_real() returns
3199 * -ENOSPC when it detects a possible extent count overflow and in
3200 * response, the higher layers of directory handling code do the
3201 * following:
3202 * 1. Data/Free blocks: XFS lets these blocks linger until a
3203 * future remove operation removes them.
3204 * 2. Dabtree blocks: XFS swaps the blocks with the last block in the
3205 * Leaf space and unmaps the last block.
3206 *
3207 * For target_dp, there are two cases depending on whether the
3208 * destination directory entry exists or not.
3209 *
3210 * When destination directory entry does not exist (i.e. target_ip ==
3211 * NULL), extent count overflow check is performed only when transaction
3212 * has a non-zero sized space reservation associated with it. With a
3213 * zero-sized space reservation, XFS allows a rename operation to
3214 * continue only when the directory has sufficient free space in its
3215 * data/leaf/free space blocks to hold the new entry.
3216 *
3217 * When destination directory entry exists (i.e. target_ip != NULL), all
3218 * we need to do is change the inode number associated with the already
3219 * existing entry. Hence there is no need to perform an extent count
3220 * overflow check.
f6bba201
DC
3221 */
3222 if (target_ip == NULL) {
3223 /*
3224 * If there's no space reservation, check the entry will
3225 * fit before actually inserting it.
3226 */
94f3cad5
ES
3227 if (!spaceres) {
3228 error = xfs_dir_canenter(tp, target_dp, target_name);
3229 if (error)
445883e8 3230 goto out_trans_cancel;
02092a2f
CB
3231 } else {
3232 error = xfs_iext_count_may_overflow(target_dp,
3233 XFS_DATA_FORK,
3234 XFS_IEXT_DIR_MANIP_CNT(mp));
3235 if (error)
3236 goto out_trans_cancel;
94f3cad5 3237 }
bc56ad8c 3238 } else {
3239 /*
3240 * If target exists and it's a directory, check that whether
3241 * it can be destroyed.
3242 */
3243 if (S_ISDIR(VFS_I(target_ip)->i_mode) &&
3244 (!xfs_dir_isempty(target_ip) ||
3245 (VFS_I(target_ip)->i_nlink > 2))) {
3246 error = -EEXIST;
3247 goto out_trans_cancel;
3248 }
3249 }
3250
6da1b4b1
DW
3251 /*
3252 * Lock the AGI buffers we need to handle bumping the nlink of the
3253 * whiteout inode off the unlinked list and to handle dropping the
3254 * nlink of the target inode. Per locking order rules, do this in
3255 * increasing AG order and before directory block allocation tries to
3256 * grab AGFs because we grab AGIs before AGFs.
3257 *
3258 * The (vfs) caller must ensure that if src is a directory then
3259 * target_ip is either null or an empty directory.
3260 */
3261 for (i = 0; i < num_inodes && inodes[i] != NULL; i++) {
3262 if (inodes[i] == wip ||
3263 (inodes[i] == target_ip &&
3264 (VFS_I(target_ip)->i_nlink == 1 || src_is_directory))) {
3265 struct xfs_buf *bp;
3266 xfs_agnumber_t agno;
3267
3268 agno = XFS_INO_TO_AGNO(mp, inodes[i]->i_ino);
3269 error = xfs_read_agi(mp, tp, agno, &bp);
3270 if (error)
3271 goto out_trans_cancel;
3272 }
3273 }
3274
bc56ad8c 3275 /*
3276 * Directory entry creation below may acquire the AGF. Remove
3277 * the whiteout from the unlinked list first to preserve correct
3278 * AGI/AGF locking order. This dirties the transaction so failures
3279 * after this point will abort and log recovery will clean up the
3280 * mess.
3281 *
3282 * For whiteouts, we need to bump the link count on the whiteout
3283 * inode. After this point, we have a real link, clear the tmpfile
3284 * state flag from the inode so it doesn't accidentally get misused
3285 * in future.
3286 */
3287 if (wip) {
f40aadb2
DC
3288 struct xfs_perag *pag;
3289
bc56ad8c 3290 ASSERT(VFS_I(wip)->i_nlink == 0);
f40aadb2
DC
3291
3292 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, wip->i_ino));
3293 error = xfs_iunlink_remove(tp, pag, wip);
3294 xfs_perag_put(pag);
bc56ad8c 3295 if (error)
3296 goto out_trans_cancel;
3297
3298 xfs_bumplink(tp, wip);
bc56ad8c 3299 VFS_I(wip)->i_state &= ~I_LINKABLE;
3300 }
3301
3302 /*
3303 * Set up the target.
3304 */
3305 if (target_ip == NULL) {
f6bba201
DC
3306 /*
3307 * If target does not exist and the rename crosses
3308 * directories, adjust the target directory link count
3309 * to account for the ".." reference from the new entry.
3310 */
3311 error = xfs_dir_createname(tp, target_dp, target_name,
381eee69 3312 src_ip->i_ino, spaceres);
f6bba201 3313 if (error)
c8eac49e 3314 goto out_trans_cancel;
f6bba201
DC
3315
3316 xfs_trans_ichgtime(tp, target_dp,
3317 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3318
3319 if (new_parent && src_is_directory) {
91083269 3320 xfs_bumplink(tp, target_dp);
f6bba201
DC
3321 }
3322 } else { /* target_ip != NULL */
f6bba201
DC
3323 /*
3324 * Link the source inode under the target name.
3325 * If the source inode is a directory and we are moving
3326 * it across directories, its ".." entry will be
3327 * inconsistent until we replace that down below.
3328 *
3329 * In case there is already an entry with the same
3330 * name at the destination directory, remove it first.
3331 */
3332 error = xfs_dir_replace(tp, target_dp, target_name,
381eee69 3333 src_ip->i_ino, spaceres);
f6bba201 3334 if (error)
c8eac49e 3335 goto out_trans_cancel;
f6bba201
DC
3336
3337 xfs_trans_ichgtime(tp, target_dp,
3338 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3339
3340 /*
3341 * Decrement the link count on the target since the target
3342 * dir no longer points to it.
3343 */
3344 error = xfs_droplink(tp, target_ip);
3345 if (error)
c8eac49e 3346 goto out_trans_cancel;
f6bba201
DC
3347
3348 if (src_is_directory) {
3349 /*
3350 * Drop the link from the old "." entry.
3351 */
3352 error = xfs_droplink(tp, target_ip);
3353 if (error)
c8eac49e 3354 goto out_trans_cancel;
f6bba201
DC
3355 }
3356 } /* target_ip != NULL */
3357
3358 /*
3359 * Remove the source.
3360 */
3361 if (new_parent && src_is_directory) {
3362 /*
3363 * Rewrite the ".." entry to point to the new
3364 * directory.
3365 */
3366 error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot,
381eee69 3367 target_dp->i_ino, spaceres);
2451337d 3368 ASSERT(error != -EEXIST);
f6bba201 3369 if (error)
c8eac49e 3370 goto out_trans_cancel;
f6bba201
DC
3371 }
3372
3373 /*
3374 * We always want to hit the ctime on the source inode.
3375 *
3376 * This isn't strictly required by the standards since the source
3377 * inode isn't really being changed, but old unix file systems did
3378 * it and some incremental backup programs won't work without it.
3379 */
3380 xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG);
3381 xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE);
3382
3383 /*
3384 * Adjust the link count on src_dp. This is necessary when
3385 * renaming a directory, either within one parent when
3386 * the target existed, or across two parent directories.
3387 */
3388 if (src_is_directory && (new_parent || target_ip != NULL)) {
3389
3390 /*
3391 * Decrement link count on src_directory since the
3392 * entry that's moved no longer points to it.
3393 */
3394 error = xfs_droplink(tp, src_dp);
3395 if (error)
c8eac49e 3396 goto out_trans_cancel;
f6bba201
DC
3397 }
3398
7dcf5c3e
DC
3399 /*
3400 * For whiteouts, we only need to update the source dirent with the
3401 * inode number of the whiteout inode rather than removing it
3402 * altogether.
3403 */
3404 if (wip) {
3405 error = xfs_dir_replace(tp, src_dp, src_name, wip->i_ino,
381eee69 3406 spaceres);
02092a2f
CB
3407 } else {
3408 /*
3409 * NOTE: We don't need to check for extent count overflow here
3410 * because the dir remove name code will leave the dir block in
3411 * place if the extent count would overflow.
3412 */
7dcf5c3e 3413 error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino,
381eee69 3414 spaceres);
02092a2f
CB
3415 }
3416
f6bba201 3417 if (error)
c8eac49e 3418 goto out_trans_cancel;
f6bba201 3419
f6bba201
DC
3420 xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3421 xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE);
3422 if (new_parent)
3423 xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE);
f6bba201 3424
c9cfdb38 3425 error = xfs_finish_rename(tp);
7dcf5c3e 3426 if (wip)
44a8736b 3427 xfs_irele(wip);
7dcf5c3e 3428 return error;
f6bba201 3429
445883e8 3430out_trans_cancel:
4906e215 3431 xfs_trans_cancel(tp);
253f4911 3432out_release_wip:
7dcf5c3e 3433 if (wip)
44a8736b 3434 xfs_irele(wip);
f6bba201
DC
3435 return error;
3436}
3437
e6187b34
DC
3438static int
3439xfs_iflush(
93848a99
CH
3440 struct xfs_inode *ip,
3441 struct xfs_buf *bp)
1da177e4 3442{
93848a99
CH
3443 struct xfs_inode_log_item *iip = ip->i_itemp;
3444 struct xfs_dinode *dip;
3445 struct xfs_mount *mp = ip->i_mount;
f2019299 3446 int error;
1da177e4 3447
579aa9ca 3448 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
718ecc50 3449 ASSERT(xfs_iflags_test(ip, XFS_IFLUSHING));
f7e67b20 3450 ASSERT(ip->i_df.if_format != XFS_DINODE_FMT_BTREE ||
daf83964 3451 ip->i_df.if_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
90c60e16 3452 ASSERT(iip->ili_item.li_buf == bp);
1da177e4 3453
88ee2df7 3454 dip = xfs_buf_offset(bp, ip->i_imap.im_boffset);
1da177e4 3455
f2019299
BF
3456 /*
3457 * We don't flush the inode if any of the following checks fail, but we
3458 * do still update the log item and attach to the backing buffer as if
3459 * the flush happened. This is a formality to facilitate predictable
3460 * error handling as the caller will shutdown and fail the buffer.
3461 */
3462 error = -EFSCORRUPTED;
69ef921b 3463 if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
9e24cfd0 3464 mp, XFS_ERRTAG_IFLUSH_1)) {
6a19d939 3465 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
c9690043 3466 "%s: Bad inode %Lu magic number 0x%x, ptr "PTR_FMT,
6a19d939 3467 __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
f2019299 3468 goto flush_out;
1da177e4 3469 }
c19b3b05 3470 if (S_ISREG(VFS_I(ip)->i_mode)) {
1da177e4 3471 if (XFS_TEST_ERROR(
f7e67b20
CH
3472 ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS &&
3473 ip->i_df.if_format != XFS_DINODE_FMT_BTREE,
9e24cfd0 3474 mp, XFS_ERRTAG_IFLUSH_3)) {
6a19d939 3475 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
c9690043 3476 "%s: Bad regular inode %Lu, ptr "PTR_FMT,
6a19d939 3477 __func__, ip->i_ino, ip);
f2019299 3478 goto flush_out;
1da177e4 3479 }
c19b3b05 3480 } else if (S_ISDIR(VFS_I(ip)->i_mode)) {
1da177e4 3481 if (XFS_TEST_ERROR(
f7e67b20
CH
3482 ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS &&
3483 ip->i_df.if_format != XFS_DINODE_FMT_BTREE &&
3484 ip->i_df.if_format != XFS_DINODE_FMT_LOCAL,
9e24cfd0 3485 mp, XFS_ERRTAG_IFLUSH_4)) {
6a19d939 3486 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
c9690043 3487 "%s: Bad directory inode %Lu, ptr "PTR_FMT,
6a19d939 3488 __func__, ip->i_ino, ip);
f2019299 3489 goto flush_out;
1da177e4
LT
3490 }
3491 }
daf83964 3492 if (XFS_TEST_ERROR(ip->i_df.if_nextents + xfs_ifork_nextents(ip->i_afp) >
6e73a545 3493 ip->i_nblocks, mp, XFS_ERRTAG_IFLUSH_5)) {
6a19d939
DC
3494 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3495 "%s: detected corrupt incore inode %Lu, "
c9690043 3496 "total extents = %d, nblocks = %Ld, ptr "PTR_FMT,
6a19d939 3497 __func__, ip->i_ino,
daf83964 3498 ip->i_df.if_nextents + xfs_ifork_nextents(ip->i_afp),
6e73a545 3499 ip->i_nblocks, ip);
f2019299 3500 goto flush_out;
1da177e4 3501 }
7821ea30 3502 if (XFS_TEST_ERROR(ip->i_forkoff > mp->m_sb.sb_inodesize,
9e24cfd0 3503 mp, XFS_ERRTAG_IFLUSH_6)) {
6a19d939 3504 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
c9690043 3505 "%s: bad inode %Lu, forkoff 0x%x, ptr "PTR_FMT,
7821ea30 3506 __func__, ip->i_ino, ip->i_forkoff, ip);
f2019299 3507 goto flush_out;
1da177e4 3508 }
e60896d8 3509
1da177e4 3510 /*
965e0a1a
CH
3511 * Inode item log recovery for v2 inodes are dependent on the flushiter
3512 * count for correct sequencing. We bump the flush iteration count so
3513 * we can detect flushes which postdate a log record during recovery.
3514 * This is redundant as we now log every change and hence this can't
3515 * happen but we need to still do it to ensure backwards compatibility
3516 * with old kernels that predate logging all inode changes.
1da177e4 3517 */
6471e9c5 3518 if (!xfs_sb_version_has_v3inode(&mp->m_sb))
965e0a1a 3519 ip->i_flushiter++;
1da177e4 3520
0f45a1b2
CH
3521 /*
3522 * If there are inline format data / attr forks attached to this inode,
3523 * make sure they are not corrupt.
3524 */
f7e67b20 3525 if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL &&
0f45a1b2
CH
3526 xfs_ifork_verify_local_data(ip))
3527 goto flush_out;
f7e67b20 3528 if (ip->i_afp && ip->i_afp->if_format == XFS_DINODE_FMT_LOCAL &&
0f45a1b2 3529 xfs_ifork_verify_local_attr(ip))
f2019299 3530 goto flush_out;
005c5db8 3531
1da177e4 3532 /*
3987848c
DC
3533 * Copy the dirty parts of the inode into the on-disk inode. We always
3534 * copy out the core of the inode, because if the inode is dirty at all
3535 * the core must be.
1da177e4 3536 */
93f958f9 3537 xfs_inode_to_disk(ip, dip, iip->ili_item.li_lsn);
1da177e4
LT
3538
3539 /* Wrap, we never let the log put out DI_MAX_FLUSH */
ee7b83fd
CH
3540 if (!xfs_sb_version_has_v3inode(&mp->m_sb)) {
3541 if (ip->i_flushiter == DI_MAX_FLUSH)
3542 ip->i_flushiter = 0;
3543 }
1da177e4 3544
005c5db8
DW
3545 xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK);
3546 if (XFS_IFORK_Q(ip))
3547 xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK);
1da177e4
LT
3548
3549 /*
f5d8d5c4
CH
3550 * We've recorded everything logged in the inode, so we'd like to clear
3551 * the ili_fields bits so we don't log and flush things unnecessarily.
3552 * However, we can't stop logging all this information until the data
3553 * we've copied into the disk buffer is written to disk. If we did we
3554 * might overwrite the copy of the inode in the log with all the data
3555 * after re-logging only part of it, and in the face of a crash we
3556 * wouldn't have all the data we need to recover.
1da177e4 3557 *
f5d8d5c4
CH
3558 * What we do is move the bits to the ili_last_fields field. When
3559 * logging the inode, these bits are moved back to the ili_fields field.
664ffb8a
CH
3560 * In the xfs_buf_inode_iodone() routine we clear ili_last_fields, since
3561 * we know that the information those bits represent is permanently on
f5d8d5c4
CH
3562 * disk. As long as the flush completes before the inode is logged
3563 * again, then both ili_fields and ili_last_fields will be cleared.
1da177e4 3564 */
f2019299
BF
3565 error = 0;
3566flush_out:
1319ebef 3567 spin_lock(&iip->ili_lock);
93848a99
CH
3568 iip->ili_last_fields = iip->ili_fields;
3569 iip->ili_fields = 0;
fc0561ce 3570 iip->ili_fsync_fields = 0;
1319ebef 3571 spin_unlock(&iip->ili_lock);
1da177e4 3572
1319ebef
DC
3573 /*
3574 * Store the current LSN of the inode so that we can tell whether the
664ffb8a 3575 * item has moved in the AIL from xfs_buf_inode_iodone().
1319ebef 3576 */
93848a99
CH
3577 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
3578 &iip->ili_item.li_lsn);
1da177e4 3579
93848a99
CH
3580 /* generate the checksum. */
3581 xfs_dinode_calc_crc(mp, dip);
f2019299 3582 return error;
1da177e4 3583}
44a8736b 3584
e6187b34
DC
3585/*
3586 * Non-blocking flush of dirty inode metadata into the backing buffer.
3587 *
3588 * The caller must have a reference to the inode and hold the cluster buffer
3589 * locked. The function will walk across all the inodes on the cluster buffer it
3590 * can find and lock without blocking, and flush them to the cluster buffer.
3591 *
5717ea4d
DC
3592 * On successful flushing of at least one inode, the caller must write out the
3593 * buffer and release it. If no inodes are flushed, -EAGAIN will be returned and
3594 * the caller needs to release the buffer. On failure, the filesystem will be
3595 * shut down, the buffer will have been unlocked and released, and EFSCORRUPTED
3596 * will be returned.
e6187b34
DC
3597 */
3598int
3599xfs_iflush_cluster(
e6187b34
DC
3600 struct xfs_buf *bp)
3601{
5717ea4d
DC
3602 struct xfs_mount *mp = bp->b_mount;
3603 struct xfs_log_item *lip, *n;
3604 struct xfs_inode *ip;
3605 struct xfs_inode_log_item *iip;
e6187b34 3606 int clcount = 0;
5717ea4d 3607 int error = 0;
e6187b34 3608
5717ea4d
DC
3609 /*
3610 * We must use the safe variant here as on shutdown xfs_iflush_abort()
3611 * can remove itself from the list.
3612 */
3613 list_for_each_entry_safe(lip, n, &bp->b_li_list, li_bio_list) {
3614 iip = (struct xfs_inode_log_item *)lip;
3615 ip = iip->ili_inode;
e6187b34
DC
3616
3617 /*
5717ea4d 3618 * Quick and dirty check to avoid locks if possible.
e6187b34 3619 */
718ecc50 3620 if (__xfs_iflags_test(ip, XFS_IRECLAIM | XFS_IFLUSHING))
5717ea4d
DC
3621 continue;
3622 if (xfs_ipincount(ip))
e6187b34 3623 continue;
e6187b34
DC
3624
3625 /*
5717ea4d
DC
3626 * The inode is still attached to the buffer, which means it is
3627 * dirty but reclaim might try to grab it. Check carefully for
3628 * that, and grab the ilock while still holding the i_flags_lock
3629 * to guarantee reclaim will not be able to reclaim this inode
3630 * once we drop the i_flags_lock.
e6187b34 3631 */
5717ea4d
DC
3632 spin_lock(&ip->i_flags_lock);
3633 ASSERT(!__xfs_iflags_test(ip, XFS_ISTALE));
718ecc50 3634 if (__xfs_iflags_test(ip, XFS_IRECLAIM | XFS_IFLUSHING)) {
5717ea4d
DC
3635 spin_unlock(&ip->i_flags_lock);
3636 continue;
e6187b34 3637 }
e6187b34
DC
3638
3639 /*
5717ea4d
DC
3640 * ILOCK will pin the inode against reclaim and prevent
3641 * concurrent transactions modifying the inode while we are
718ecc50
DC
3642 * flushing the inode. If we get the lock, set the flushing
3643 * state before we drop the i_flags_lock.
e6187b34 3644 */
5717ea4d
DC
3645 if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
3646 spin_unlock(&ip->i_flags_lock);
e6187b34 3647 continue;
5717ea4d 3648 }
718ecc50 3649 __xfs_iflags_set(ip, XFS_IFLUSHING);
5717ea4d 3650 spin_unlock(&ip->i_flags_lock);
e6187b34 3651
e6187b34 3652 /*
5717ea4d
DC
3653 * Abort flushing this inode if we are shut down because the
3654 * inode may not currently be in the AIL. This can occur when
3655 * log I/O failure unpins the inode without inserting into the
3656 * AIL, leaving a dirty/unpinned inode attached to the buffer
3657 * that otherwise looks like it should be flushed.
e6187b34 3658 */
5717ea4d
DC
3659 if (XFS_FORCED_SHUTDOWN(mp)) {
3660 xfs_iunpin_wait(ip);
5717ea4d
DC
3661 xfs_iflush_abort(ip);
3662 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3663 error = -EIO;
e6187b34
DC
3664 continue;
3665 }
3666
5717ea4d
DC
3667 /* don't block waiting on a log force to unpin dirty inodes */
3668 if (xfs_ipincount(ip)) {
718ecc50 3669 xfs_iflags_clear(ip, XFS_IFLUSHING);
5717ea4d
DC
3670 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3671 continue;
e6187b34 3672 }
e6187b34 3673
5717ea4d
DC
3674 if (!xfs_inode_clean(ip))
3675 error = xfs_iflush(ip, bp);
3676 else
718ecc50 3677 xfs_iflags_clear(ip, XFS_IFLUSHING);
5717ea4d
DC
3678 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3679 if (error)
3680 break;
3681 clcount++;
e6187b34
DC
3682 }
3683
e6187b34
DC
3684 if (error) {
3685 bp->b_flags |= XBF_ASYNC;
3686 xfs_buf_ioend_fail(bp);
3687 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
5717ea4d 3688 return error;
e6187b34 3689 }
5717ea4d
DC
3690
3691 if (!clcount)
3692 return -EAGAIN;
3693
3694 XFS_STATS_INC(mp, xs_icluster_flushcnt);
3695 XFS_STATS_ADD(mp, xs_icluster_flushinode, clcount);
3696 return 0;
3697
e6187b34
DC
3698}
3699
44a8736b
DW
3700/* Release an inode. */
3701void
3702xfs_irele(
3703 struct xfs_inode *ip)
3704{
3705 trace_xfs_irele(ip, _RET_IP_);
3706 iput(VFS_I(ip));
3707}
54fbdd10
CH
3708
3709/*
3710 * Ensure all commited transactions touching the inode are written to the log.
3711 */
3712int
3713xfs_log_force_inode(
3714 struct xfs_inode *ip)
3715{
5f9b4b0d 3716 xfs_csn_t seq = 0;
54fbdd10
CH
3717
3718 xfs_ilock(ip, XFS_ILOCK_SHARED);
3719 if (xfs_ipincount(ip))
5f9b4b0d 3720 seq = ip->i_itemp->ili_commit_seq;
54fbdd10
CH
3721 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3722
5f9b4b0d 3723 if (!seq)
54fbdd10 3724 return 0;
5f9b4b0d 3725 return xfs_log_force_seq(ip->i_mount, seq, XFS_LOG_SYNC, NULL);
54fbdd10 3726}
e2aaee9c
DW
3727
3728/*
3729 * Grab the exclusive iolock for a data copy from src to dest, making sure to
3730 * abide vfs locking order (lowest pointer value goes first) and breaking the
3731 * layout leases before proceeding. The loop is needed because we cannot call
3732 * the blocking break_layout() with the iolocks held, and therefore have to
3733 * back out both locks.
3734 */
3735static int
3736xfs_iolock_two_inodes_and_break_layout(
3737 struct inode *src,
3738 struct inode *dest)
3739{
3740 int error;
3741
3742 if (src > dest)
3743 swap(src, dest);
3744
3745retry:
3746 /* Wait to break both inodes' layouts before we start locking. */
3747 error = break_layout(src, true);
3748 if (error)
3749 return error;
3750 if (src != dest) {
3751 error = break_layout(dest, true);
3752 if (error)
3753 return error;
3754 }
3755
3756 /* Lock one inode and make sure nobody got in and leased it. */
3757 inode_lock(src);
3758 error = break_layout(src, false);
3759 if (error) {
3760 inode_unlock(src);
3761 if (error == -EWOULDBLOCK)
3762 goto retry;
3763 return error;
3764 }
3765
3766 if (src == dest)
3767 return 0;
3768
3769 /* Lock the other inode and make sure nobody got in and leased it. */
3770 inode_lock_nested(dest, I_MUTEX_NONDIR2);
3771 error = break_layout(dest, false);
3772 if (error) {
3773 inode_unlock(src);
3774 inode_unlock(dest);
3775 if (error == -EWOULDBLOCK)
3776 goto retry;
3777 return error;
3778 }
3779
3780 return 0;
3781}
3782
3783/*
3784 * Lock two inodes so that userspace cannot initiate I/O via file syscalls or
3785 * mmap activity.
3786 */
3787int
3788xfs_ilock2_io_mmap(
3789 struct xfs_inode *ip1,
3790 struct xfs_inode *ip2)
3791{
3792 int ret;
3793
3794 ret = xfs_iolock_two_inodes_and_break_layout(VFS_I(ip1), VFS_I(ip2));
3795 if (ret)
3796 return ret;
3797 if (ip1 == ip2)
3798 xfs_ilock(ip1, XFS_MMAPLOCK_EXCL);
3799 else
3800 xfs_lock_two_inodes(ip1, XFS_MMAPLOCK_EXCL,
3801 ip2, XFS_MMAPLOCK_EXCL);
3802 return 0;
3803}
3804
3805/* Unlock both inodes to allow IO and mmap activity. */
3806void
3807xfs_iunlock2_io_mmap(
3808 struct xfs_inode *ip1,
3809 struct xfs_inode *ip2)
3810{
3811 bool same_inode = (ip1 == ip2);
3812
3813 xfs_iunlock(ip2, XFS_MMAPLOCK_EXCL);
3814 if (!same_inode)
3815 xfs_iunlock(ip1, XFS_MMAPLOCK_EXCL);
3816 inode_unlock(VFS_I(ip2));
3817 if (!same_inode)
3818 inode_unlock(VFS_I(ip1));
3819}