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