xref: /openbmc/linux/fs/xfs/xfs_inode.c (revision b63da6c8dfa9b2ab3554e8c59ef294d1f28bb9bd)
10b61f8a4SDave Chinner // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
33e57ecf6SOlaf Weber  * Copyright (c) 2000-2006 Silicon Graphics, Inc.
47b718769SNathan Scott  * All Rights Reserved.
51da177e4SLinus Torvalds  */
6f0e28280SJeff Layton #include <linux/iversion.h>
740ebd81dSRobert P. J. Day 
81da177e4SLinus Torvalds #include "xfs.h"
9a844f451SNathan Scott #include "xfs_fs.h"
1070a9883cSDave Chinner #include "xfs_shared.h"
11239880efSDave Chinner #include "xfs_format.h"
12239880efSDave Chinner #include "xfs_log_format.h"
13239880efSDave Chinner #include "xfs_trans_resv.h"
141da177e4SLinus Torvalds #include "xfs_sb.h"
151da177e4SLinus Torvalds #include "xfs_mount.h"
163ab78df2SDarrick J. Wong #include "xfs_defer.h"
17a4fbe6abSDave Chinner #include "xfs_inode.h"
18c24b5dfaSDave Chinner #include "xfs_dir2.h"
19c24b5dfaSDave Chinner #include "xfs_attr.h"
20239880efSDave Chinner #include "xfs_trans_space.h"
21239880efSDave Chinner #include "xfs_trans.h"
221da177e4SLinus Torvalds #include "xfs_buf_item.h"
23a844f451SNathan Scott #include "xfs_inode_item.h"
24a844f451SNathan Scott #include "xfs_ialloc.h"
25a844f451SNathan Scott #include "xfs_bmap.h"
2668988114SDave Chinner #include "xfs_bmap_util.h"
27e9e899a2SDarrick J. Wong #include "xfs_errortag.h"
281da177e4SLinus Torvalds #include "xfs_error.h"
291da177e4SLinus Torvalds #include "xfs_quota.h"
302a82b8beSDavid Chinner #include "xfs_filestream.h"
310b1b213fSChristoph Hellwig #include "xfs_trace.h"
3233479e05SDave Chinner #include "xfs_icache.h"
33c24b5dfaSDave Chinner #include "xfs_symlink.h"
34239880efSDave Chinner #include "xfs_trans_priv.h"
35239880efSDave Chinner #include "xfs_log.h"
36a4fbe6abSDave Chinner #include "xfs_bmap_btree.h"
37aa8968f2SDarrick J. Wong #include "xfs_reflink.h"
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds kmem_zone_t *xfs_inode_zone;
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds /*
428f04c47aSChristoph Hellwig  * Used in xfs_itruncate_extents().  This is the maximum number of extents
431da177e4SLinus Torvalds  * freed from a file in a single transaction.
441da177e4SLinus Torvalds  */
451da177e4SLinus Torvalds #define	XFS_ITRUNC_MAX_EXTENTS	2
461da177e4SLinus Torvalds 
4754d7b5c1SDave Chinner STATIC int xfs_iunlink(struct xfs_trans *, struct xfs_inode *);
4854d7b5c1SDave Chinner STATIC int xfs_iunlink_remove(struct xfs_trans *, struct xfs_inode *);
49ab297431SZhi Yong Wu 
502a0ec1d9SDave Chinner /*
512a0ec1d9SDave Chinner  * helper function to extract extent size hint from inode
522a0ec1d9SDave Chinner  */
532a0ec1d9SDave Chinner xfs_extlen_t
542a0ec1d9SDave Chinner xfs_get_extsz_hint(
552a0ec1d9SDave Chinner 	struct xfs_inode	*ip)
562a0ec1d9SDave Chinner {
57bdb2ed2dSChristoph Hellwig 	/*
58bdb2ed2dSChristoph Hellwig 	 * No point in aligning allocations if we need to COW to actually
59bdb2ed2dSChristoph Hellwig 	 * write to them.
60bdb2ed2dSChristoph Hellwig 	 */
61bdb2ed2dSChristoph Hellwig 	if (xfs_is_always_cow_inode(ip))
62bdb2ed2dSChristoph Hellwig 		return 0;
632a0ec1d9SDave Chinner 	if ((ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE) && ip->i_d.di_extsize)
642a0ec1d9SDave Chinner 		return ip->i_d.di_extsize;
652a0ec1d9SDave Chinner 	if (XFS_IS_REALTIME_INODE(ip))
662a0ec1d9SDave Chinner 		return ip->i_mount->m_sb.sb_rextsize;
672a0ec1d9SDave Chinner 	return 0;
682a0ec1d9SDave Chinner }
692a0ec1d9SDave Chinner 
70fa96acadSDave Chinner /*
71f7ca3522SDarrick J. Wong  * Helper function to extract CoW extent size hint from inode.
72f7ca3522SDarrick J. Wong  * Between the extent size hint and the CoW extent size hint, we
73e153aa79SDarrick J. Wong  * return the greater of the two.  If the value is zero (automatic),
74e153aa79SDarrick J. Wong  * use the default size.
75f7ca3522SDarrick J. Wong  */
76f7ca3522SDarrick J. Wong xfs_extlen_t
77f7ca3522SDarrick J. Wong xfs_get_cowextsz_hint(
78f7ca3522SDarrick J. Wong 	struct xfs_inode	*ip)
79f7ca3522SDarrick J. Wong {
80f7ca3522SDarrick J. Wong 	xfs_extlen_t		a, b;
81f7ca3522SDarrick J. Wong 
82f7ca3522SDarrick J. Wong 	a = 0;
83f7ca3522SDarrick J. Wong 	if (ip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE)
84f7ca3522SDarrick J. Wong 		a = ip->i_d.di_cowextsize;
85f7ca3522SDarrick J. Wong 	b = xfs_get_extsz_hint(ip);
86f7ca3522SDarrick J. Wong 
87e153aa79SDarrick J. Wong 	a = max(a, b);
88e153aa79SDarrick J. Wong 	if (a == 0)
89e153aa79SDarrick J. Wong 		return XFS_DEFAULT_COWEXTSZ_HINT;
90f7ca3522SDarrick J. Wong 	return a;
91f7ca3522SDarrick J. Wong }
92f7ca3522SDarrick J. Wong 
93f7ca3522SDarrick J. Wong /*
94efa70be1SChristoph Hellwig  * These two are wrapper routines around the xfs_ilock() routine used to
95efa70be1SChristoph Hellwig  * centralize some grungy code.  They are used in places that wish to lock the
96efa70be1SChristoph Hellwig  * inode solely for reading the extents.  The reason these places can't just
97efa70be1SChristoph Hellwig  * call xfs_ilock(ip, XFS_ILOCK_SHARED) is that the inode lock also guards to
98efa70be1SChristoph Hellwig  * bringing in of the extents from disk for a file in b-tree format.  If the
99efa70be1SChristoph Hellwig  * inode is in b-tree format, then we need to lock the inode exclusively until
100efa70be1SChristoph Hellwig  * the extents are read in.  Locking it exclusively all the time would limit
101efa70be1SChristoph Hellwig  * our parallelism unnecessarily, though.  What we do instead is check to see
102efa70be1SChristoph Hellwig  * if the extents have been read in yet, and only lock the inode exclusively
103efa70be1SChristoph Hellwig  * if they have not.
104fa96acadSDave Chinner  *
105efa70be1SChristoph Hellwig  * The functions return a value which should be given to the corresponding
10601f4f327SChristoph Hellwig  * xfs_iunlock() call.
107fa96acadSDave Chinner  */
108fa96acadSDave Chinner uint
109309ecac8SChristoph Hellwig xfs_ilock_data_map_shared(
110309ecac8SChristoph Hellwig 	struct xfs_inode	*ip)
111fa96acadSDave Chinner {
112309ecac8SChristoph Hellwig 	uint			lock_mode = XFS_ILOCK_SHARED;
113fa96acadSDave Chinner 
114f7e67b20SChristoph Hellwig 	if (ip->i_df.if_format == XFS_DINODE_FMT_BTREE &&
115309ecac8SChristoph Hellwig 	    (ip->i_df.if_flags & XFS_IFEXTENTS) == 0)
116fa96acadSDave Chinner 		lock_mode = XFS_ILOCK_EXCL;
117fa96acadSDave Chinner 	xfs_ilock(ip, lock_mode);
118fa96acadSDave Chinner 	return lock_mode;
119fa96acadSDave Chinner }
120fa96acadSDave Chinner 
121efa70be1SChristoph Hellwig uint
122efa70be1SChristoph Hellwig xfs_ilock_attr_map_shared(
123efa70be1SChristoph Hellwig 	struct xfs_inode	*ip)
124fa96acadSDave Chinner {
125efa70be1SChristoph Hellwig 	uint			lock_mode = XFS_ILOCK_SHARED;
126efa70be1SChristoph Hellwig 
127f7e67b20SChristoph Hellwig 	if (ip->i_afp &&
128f7e67b20SChristoph Hellwig 	    ip->i_afp->if_format == XFS_DINODE_FMT_BTREE &&
129efa70be1SChristoph Hellwig 	    (ip->i_afp->if_flags & XFS_IFEXTENTS) == 0)
130efa70be1SChristoph Hellwig 		lock_mode = XFS_ILOCK_EXCL;
131efa70be1SChristoph Hellwig 	xfs_ilock(ip, lock_mode);
132efa70be1SChristoph Hellwig 	return lock_mode;
133fa96acadSDave Chinner }
134fa96acadSDave Chinner 
135fa96acadSDave Chinner /*
13665523218SChristoph Hellwig  * In addition to i_rwsem in the VFS inode, the xfs inode contains 2
13765523218SChristoph Hellwig  * multi-reader locks: i_mmap_lock and the i_lock.  This routine allows
13865523218SChristoph Hellwig  * various combinations of the locks to be obtained.
139fa96acadSDave Chinner  *
140653c60b6SDave Chinner  * The 3 locks should always be ordered so that the IO lock is obtained first,
141653c60b6SDave Chinner  * the mmap lock second and the ilock last in order to prevent deadlock.
142fa96acadSDave Chinner  *
143653c60b6SDave Chinner  * Basic locking order:
144653c60b6SDave Chinner  *
14565523218SChristoph Hellwig  * i_rwsem -> i_mmap_lock -> page_lock -> i_ilock
146653c60b6SDave Chinner  *
147c1e8d7c6SMichel Lespinasse  * mmap_lock locking order:
148653c60b6SDave Chinner  *
149c1e8d7c6SMichel Lespinasse  * i_rwsem -> page lock -> mmap_lock
150c1e8d7c6SMichel Lespinasse  * mmap_lock -> i_mmap_lock -> page_lock
151653c60b6SDave Chinner  *
152c1e8d7c6SMichel Lespinasse  * The difference in mmap_lock locking order mean that we cannot hold the
153653c60b6SDave Chinner  * i_mmap_lock over syscall based read(2)/write(2) based IO. These IO paths can
154c1e8d7c6SMichel Lespinasse  * fault in pages during copy in/out (for buffered IO) or require the mmap_lock
155653c60b6SDave Chinner  * in get_user_pages() to map the user pages into the kernel address space for
15665523218SChristoph Hellwig  * direct IO. Similarly the i_rwsem cannot be taken inside a page fault because
157c1e8d7c6SMichel Lespinasse  * page faults already hold the mmap_lock.
158653c60b6SDave Chinner  *
159653c60b6SDave Chinner  * Hence to serialise fully against both syscall and mmap based IO, we need to
16065523218SChristoph Hellwig  * take both the i_rwsem and the i_mmap_lock. These locks should *only* be both
161653c60b6SDave Chinner  * taken in places where we need to invalidate the page cache in a race
162653c60b6SDave Chinner  * free manner (e.g. truncate, hole punch and other extent manipulation
163653c60b6SDave Chinner  * functions).
164fa96acadSDave Chinner  */
165fa96acadSDave Chinner void
166fa96acadSDave Chinner xfs_ilock(
167fa96acadSDave Chinner 	xfs_inode_t		*ip,
168fa96acadSDave Chinner 	uint			lock_flags)
169fa96acadSDave Chinner {
170fa96acadSDave Chinner 	trace_xfs_ilock(ip, lock_flags, _RET_IP_);
171fa96acadSDave Chinner 
172fa96acadSDave Chinner 	/*
173fa96acadSDave Chinner 	 * You can't set both SHARED and EXCL for the same lock,
174fa96acadSDave Chinner 	 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
175fa96acadSDave Chinner 	 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
176fa96acadSDave Chinner 	 */
177fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
178fa96acadSDave Chinner 	       (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
179653c60b6SDave Chinner 	ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
180653c60b6SDave Chinner 	       (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
181fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
182fa96acadSDave Chinner 	       (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
1830952c818SDave Chinner 	ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
184fa96acadSDave Chinner 
18565523218SChristoph Hellwig 	if (lock_flags & XFS_IOLOCK_EXCL) {
18665523218SChristoph Hellwig 		down_write_nested(&VFS_I(ip)->i_rwsem,
18765523218SChristoph Hellwig 				  XFS_IOLOCK_DEP(lock_flags));
18865523218SChristoph Hellwig 	} else if (lock_flags & XFS_IOLOCK_SHARED) {
18965523218SChristoph Hellwig 		down_read_nested(&VFS_I(ip)->i_rwsem,
19065523218SChristoph Hellwig 				 XFS_IOLOCK_DEP(lock_flags));
19165523218SChristoph Hellwig 	}
192fa96acadSDave Chinner 
193653c60b6SDave Chinner 	if (lock_flags & XFS_MMAPLOCK_EXCL)
194653c60b6SDave Chinner 		mrupdate_nested(&ip->i_mmaplock, XFS_MMAPLOCK_DEP(lock_flags));
195653c60b6SDave Chinner 	else if (lock_flags & XFS_MMAPLOCK_SHARED)
196653c60b6SDave Chinner 		mraccess_nested(&ip->i_mmaplock, XFS_MMAPLOCK_DEP(lock_flags));
197653c60b6SDave Chinner 
198fa96acadSDave Chinner 	if (lock_flags & XFS_ILOCK_EXCL)
199fa96acadSDave Chinner 		mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
200fa96acadSDave Chinner 	else if (lock_flags & XFS_ILOCK_SHARED)
201fa96acadSDave Chinner 		mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
202fa96acadSDave Chinner }
203fa96acadSDave Chinner 
204fa96acadSDave Chinner /*
205fa96acadSDave Chinner  * This is just like xfs_ilock(), except that the caller
206fa96acadSDave Chinner  * is guaranteed not to sleep.  It returns 1 if it gets
207fa96acadSDave Chinner  * the requested locks and 0 otherwise.  If the IO lock is
208fa96acadSDave Chinner  * obtained but the inode lock cannot be, then the IO lock
209fa96acadSDave Chinner  * is dropped before returning.
210fa96acadSDave Chinner  *
211fa96acadSDave Chinner  * ip -- the inode being locked
212fa96acadSDave Chinner  * lock_flags -- this parameter indicates the inode's locks to be
213fa96acadSDave Chinner  *       to be locked.  See the comment for xfs_ilock() for a list
214fa96acadSDave Chinner  *	 of valid values.
215fa96acadSDave Chinner  */
216fa96acadSDave Chinner int
217fa96acadSDave Chinner xfs_ilock_nowait(
218fa96acadSDave Chinner 	xfs_inode_t		*ip,
219fa96acadSDave Chinner 	uint			lock_flags)
220fa96acadSDave Chinner {
221fa96acadSDave Chinner 	trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_);
222fa96acadSDave Chinner 
223fa96acadSDave Chinner 	/*
224fa96acadSDave Chinner 	 * You can't set both SHARED and EXCL for the same lock,
225fa96acadSDave Chinner 	 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
226fa96acadSDave Chinner 	 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
227fa96acadSDave Chinner 	 */
228fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
229fa96acadSDave Chinner 	       (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
230653c60b6SDave Chinner 	ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
231653c60b6SDave Chinner 	       (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
232fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
233fa96acadSDave Chinner 	       (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
2340952c818SDave Chinner 	ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
235fa96acadSDave Chinner 
236fa96acadSDave Chinner 	if (lock_flags & XFS_IOLOCK_EXCL) {
23765523218SChristoph Hellwig 		if (!down_write_trylock(&VFS_I(ip)->i_rwsem))
238fa96acadSDave Chinner 			goto out;
239fa96acadSDave Chinner 	} else if (lock_flags & XFS_IOLOCK_SHARED) {
24065523218SChristoph Hellwig 		if (!down_read_trylock(&VFS_I(ip)->i_rwsem))
241fa96acadSDave Chinner 			goto out;
242fa96acadSDave Chinner 	}
243653c60b6SDave Chinner 
244653c60b6SDave Chinner 	if (lock_flags & XFS_MMAPLOCK_EXCL) {
245653c60b6SDave Chinner 		if (!mrtryupdate(&ip->i_mmaplock))
246653c60b6SDave Chinner 			goto out_undo_iolock;
247653c60b6SDave Chinner 	} else if (lock_flags & XFS_MMAPLOCK_SHARED) {
248653c60b6SDave Chinner 		if (!mrtryaccess(&ip->i_mmaplock))
249653c60b6SDave Chinner 			goto out_undo_iolock;
250653c60b6SDave Chinner 	}
251653c60b6SDave Chinner 
252fa96acadSDave Chinner 	if (lock_flags & XFS_ILOCK_EXCL) {
253fa96acadSDave Chinner 		if (!mrtryupdate(&ip->i_lock))
254653c60b6SDave Chinner 			goto out_undo_mmaplock;
255fa96acadSDave Chinner 	} else if (lock_flags & XFS_ILOCK_SHARED) {
256fa96acadSDave Chinner 		if (!mrtryaccess(&ip->i_lock))
257653c60b6SDave Chinner 			goto out_undo_mmaplock;
258fa96acadSDave Chinner 	}
259fa96acadSDave Chinner 	return 1;
260fa96acadSDave Chinner 
261653c60b6SDave Chinner out_undo_mmaplock:
262653c60b6SDave Chinner 	if (lock_flags & XFS_MMAPLOCK_EXCL)
263653c60b6SDave Chinner 		mrunlock_excl(&ip->i_mmaplock);
264653c60b6SDave Chinner 	else if (lock_flags & XFS_MMAPLOCK_SHARED)
265653c60b6SDave Chinner 		mrunlock_shared(&ip->i_mmaplock);
266fa96acadSDave Chinner out_undo_iolock:
267fa96acadSDave Chinner 	if (lock_flags & XFS_IOLOCK_EXCL)
26865523218SChristoph Hellwig 		up_write(&VFS_I(ip)->i_rwsem);
269fa96acadSDave Chinner 	else if (lock_flags & XFS_IOLOCK_SHARED)
27065523218SChristoph Hellwig 		up_read(&VFS_I(ip)->i_rwsem);
271fa96acadSDave Chinner out:
272fa96acadSDave Chinner 	return 0;
273fa96acadSDave Chinner }
274fa96acadSDave Chinner 
275fa96acadSDave Chinner /*
276fa96acadSDave Chinner  * xfs_iunlock() is used to drop the inode locks acquired with
277fa96acadSDave Chinner  * xfs_ilock() and xfs_ilock_nowait().  The caller must pass
278fa96acadSDave Chinner  * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
279fa96acadSDave Chinner  * that we know which locks to drop.
280fa96acadSDave Chinner  *
281fa96acadSDave Chinner  * ip -- the inode being unlocked
282fa96acadSDave Chinner  * lock_flags -- this parameter indicates the inode's locks to be
283fa96acadSDave Chinner  *       to be unlocked.  See the comment for xfs_ilock() for a list
284fa96acadSDave Chinner  *	 of valid values for this parameter.
285fa96acadSDave Chinner  *
286fa96acadSDave Chinner  */
287fa96acadSDave Chinner void
288fa96acadSDave Chinner xfs_iunlock(
289fa96acadSDave Chinner 	xfs_inode_t		*ip,
290fa96acadSDave Chinner 	uint			lock_flags)
291fa96acadSDave Chinner {
292fa96acadSDave Chinner 	/*
293fa96acadSDave Chinner 	 * You can't set both SHARED and EXCL for the same lock,
294fa96acadSDave Chinner 	 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
295fa96acadSDave Chinner 	 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
296fa96acadSDave Chinner 	 */
297fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
298fa96acadSDave Chinner 	       (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
299653c60b6SDave Chinner 	ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
300653c60b6SDave Chinner 	       (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
301fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
302fa96acadSDave Chinner 	       (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
3030952c818SDave Chinner 	ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
304fa96acadSDave Chinner 	ASSERT(lock_flags != 0);
305fa96acadSDave Chinner 
306fa96acadSDave Chinner 	if (lock_flags & XFS_IOLOCK_EXCL)
30765523218SChristoph Hellwig 		up_write(&VFS_I(ip)->i_rwsem);
308fa96acadSDave Chinner 	else if (lock_flags & XFS_IOLOCK_SHARED)
30965523218SChristoph Hellwig 		up_read(&VFS_I(ip)->i_rwsem);
310fa96acadSDave Chinner 
311653c60b6SDave Chinner 	if (lock_flags & XFS_MMAPLOCK_EXCL)
312653c60b6SDave Chinner 		mrunlock_excl(&ip->i_mmaplock);
313653c60b6SDave Chinner 	else if (lock_flags & XFS_MMAPLOCK_SHARED)
314653c60b6SDave Chinner 		mrunlock_shared(&ip->i_mmaplock);
315653c60b6SDave Chinner 
316fa96acadSDave Chinner 	if (lock_flags & XFS_ILOCK_EXCL)
317fa96acadSDave Chinner 		mrunlock_excl(&ip->i_lock);
318fa96acadSDave Chinner 	else if (lock_flags & XFS_ILOCK_SHARED)
319fa96acadSDave Chinner 		mrunlock_shared(&ip->i_lock);
320fa96acadSDave Chinner 
321fa96acadSDave Chinner 	trace_xfs_iunlock(ip, lock_flags, _RET_IP_);
322fa96acadSDave Chinner }
323fa96acadSDave Chinner 
324fa96acadSDave Chinner /*
325fa96acadSDave Chinner  * give up write locks.  the i/o lock cannot be held nested
326fa96acadSDave Chinner  * if it is being demoted.
327fa96acadSDave Chinner  */
328fa96acadSDave Chinner void
329fa96acadSDave Chinner xfs_ilock_demote(
330fa96acadSDave Chinner 	xfs_inode_t		*ip,
331fa96acadSDave Chinner 	uint			lock_flags)
332fa96acadSDave Chinner {
333653c60b6SDave Chinner 	ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL));
334653c60b6SDave Chinner 	ASSERT((lock_flags &
335653c60b6SDave Chinner 		~(XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
336fa96acadSDave Chinner 
337fa96acadSDave Chinner 	if (lock_flags & XFS_ILOCK_EXCL)
338fa96acadSDave Chinner 		mrdemote(&ip->i_lock);
339653c60b6SDave Chinner 	if (lock_flags & XFS_MMAPLOCK_EXCL)
340653c60b6SDave Chinner 		mrdemote(&ip->i_mmaplock);
341fa96acadSDave Chinner 	if (lock_flags & XFS_IOLOCK_EXCL)
34265523218SChristoph Hellwig 		downgrade_write(&VFS_I(ip)->i_rwsem);
343fa96acadSDave Chinner 
344fa96acadSDave Chinner 	trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_);
345fa96acadSDave Chinner }
346fa96acadSDave Chinner 
347742ae1e3SDave Chinner #if defined(DEBUG) || defined(XFS_WARN)
348fa96acadSDave Chinner int
349fa96acadSDave Chinner xfs_isilocked(
350fa96acadSDave Chinner 	xfs_inode_t		*ip,
351fa96acadSDave Chinner 	uint			lock_flags)
352fa96acadSDave Chinner {
353fa96acadSDave Chinner 	if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) {
354fa96acadSDave Chinner 		if (!(lock_flags & XFS_ILOCK_SHARED))
355fa96acadSDave Chinner 			return !!ip->i_lock.mr_writer;
356fa96acadSDave Chinner 		return rwsem_is_locked(&ip->i_lock.mr_lock);
357fa96acadSDave Chinner 	}
358fa96acadSDave Chinner 
359653c60b6SDave Chinner 	if (lock_flags & (XFS_MMAPLOCK_EXCL|XFS_MMAPLOCK_SHARED)) {
360653c60b6SDave Chinner 		if (!(lock_flags & XFS_MMAPLOCK_SHARED))
361653c60b6SDave Chinner 			return !!ip->i_mmaplock.mr_writer;
362653c60b6SDave Chinner 		return rwsem_is_locked(&ip->i_mmaplock.mr_lock);
363653c60b6SDave Chinner 	}
364653c60b6SDave Chinner 
365fa96acadSDave Chinner 	if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) {
366fa96acadSDave Chinner 		if (!(lock_flags & XFS_IOLOCK_SHARED))
36765523218SChristoph Hellwig 			return !debug_locks ||
36865523218SChristoph Hellwig 				lockdep_is_held_type(&VFS_I(ip)->i_rwsem, 0);
36965523218SChristoph Hellwig 		return rwsem_is_locked(&VFS_I(ip)->i_rwsem);
370fa96acadSDave Chinner 	}
371fa96acadSDave Chinner 
372fa96acadSDave Chinner 	ASSERT(0);
373fa96acadSDave Chinner 	return 0;
374fa96acadSDave Chinner }
375fa96acadSDave Chinner #endif
376fa96acadSDave Chinner 
377b6a9947eSDave Chinner /*
378b6a9947eSDave Chinner  * xfs_lockdep_subclass_ok() is only used in an ASSERT, so is only called when
379b6a9947eSDave Chinner  * DEBUG or XFS_WARN is set. And MAX_LOCKDEP_SUBCLASSES is then only defined
380b6a9947eSDave Chinner  * when CONFIG_LOCKDEP is set. Hence the complex define below to avoid build
381b6a9947eSDave Chinner  * errors and warnings.
382b6a9947eSDave Chinner  */
383b6a9947eSDave Chinner #if (defined(DEBUG) || defined(XFS_WARN)) && defined(CONFIG_LOCKDEP)
3843403ccc0SDave Chinner static bool
3853403ccc0SDave Chinner xfs_lockdep_subclass_ok(
3863403ccc0SDave Chinner 	int subclass)
3873403ccc0SDave Chinner {
3883403ccc0SDave Chinner 	return subclass < MAX_LOCKDEP_SUBCLASSES;
3893403ccc0SDave Chinner }
3903403ccc0SDave Chinner #else
3913403ccc0SDave Chinner #define xfs_lockdep_subclass_ok(subclass)	(true)
3923403ccc0SDave Chinner #endif
3933403ccc0SDave Chinner 
394c24b5dfaSDave Chinner /*
395653c60b6SDave Chinner  * Bump the subclass so xfs_lock_inodes() acquires each lock with a different
3960952c818SDave Chinner  * value. This can be called for any type of inode lock combination, including
3970952c818SDave Chinner  * parent locking. Care must be taken to ensure we don't overrun the subclass
3980952c818SDave Chinner  * storage fields in the class mask we build.
399c24b5dfaSDave Chinner  */
400c24b5dfaSDave Chinner static inline int
401c24b5dfaSDave Chinner xfs_lock_inumorder(int lock_mode, int subclass)
402c24b5dfaSDave Chinner {
4030952c818SDave Chinner 	int	class = 0;
4040952c818SDave Chinner 
4050952c818SDave Chinner 	ASSERT(!(lock_mode & (XFS_ILOCK_PARENT | XFS_ILOCK_RTBITMAP |
4060952c818SDave Chinner 			      XFS_ILOCK_RTSUM)));
4073403ccc0SDave Chinner 	ASSERT(xfs_lockdep_subclass_ok(subclass));
4080952c818SDave Chinner 
409653c60b6SDave Chinner 	if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) {
4100952c818SDave Chinner 		ASSERT(subclass <= XFS_IOLOCK_MAX_SUBCLASS);
4110952c818SDave Chinner 		class += subclass << XFS_IOLOCK_SHIFT;
412653c60b6SDave Chinner 	}
413653c60b6SDave Chinner 
414653c60b6SDave Chinner 	if (lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) {
4150952c818SDave Chinner 		ASSERT(subclass <= XFS_MMAPLOCK_MAX_SUBCLASS);
4160952c818SDave Chinner 		class += subclass << XFS_MMAPLOCK_SHIFT;
417653c60b6SDave Chinner 	}
418653c60b6SDave Chinner 
4190952c818SDave Chinner 	if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) {
4200952c818SDave Chinner 		ASSERT(subclass <= XFS_ILOCK_MAX_SUBCLASS);
4210952c818SDave Chinner 		class += subclass << XFS_ILOCK_SHIFT;
4220952c818SDave Chinner 	}
423c24b5dfaSDave Chinner 
4240952c818SDave Chinner 	return (lock_mode & ~XFS_LOCK_SUBCLASS_MASK) | class;
425c24b5dfaSDave Chinner }
426c24b5dfaSDave Chinner 
427c24b5dfaSDave Chinner /*
42895afcf5cSDave Chinner  * The following routine will lock n inodes in exclusive mode.  We assume the
42995afcf5cSDave Chinner  * caller calls us with the inodes in i_ino order.
430c24b5dfaSDave Chinner  *
43195afcf5cSDave Chinner  * We need to detect deadlock where an inode that we lock is in the AIL and we
43295afcf5cSDave Chinner  * start waiting for another inode that is locked by a thread in a long running
43395afcf5cSDave Chinner  * transaction (such as truncate). This can result in deadlock since the long
43495afcf5cSDave Chinner  * running trans might need to wait for the inode we just locked in order to
43595afcf5cSDave Chinner  * push the tail and free space in the log.
4360952c818SDave Chinner  *
4370952c818SDave Chinner  * xfs_lock_inodes() can only be used to lock one type of lock at a time -
4380952c818SDave Chinner  * the iolock, the mmaplock or the ilock, but not more than one at a time. If we
4390952c818SDave Chinner  * lock more than one at a time, lockdep will report false positives saying we
4400952c818SDave Chinner  * have violated locking orders.
441c24b5dfaSDave Chinner  */
4420d5a75e9SEric Sandeen static void
443c24b5dfaSDave Chinner xfs_lock_inodes(
444efe2330fSChristoph Hellwig 	struct xfs_inode	**ips,
445c24b5dfaSDave Chinner 	int			inodes,
446c24b5dfaSDave Chinner 	uint			lock_mode)
447c24b5dfaSDave Chinner {
448c24b5dfaSDave Chinner 	int			attempts = 0, i, j, try_lock;
449efe2330fSChristoph Hellwig 	struct xfs_log_item	*lp;
450c24b5dfaSDave Chinner 
4510952c818SDave Chinner 	/*
4520952c818SDave Chinner 	 * Currently supports between 2 and 5 inodes with exclusive locking.  We
4530952c818SDave Chinner 	 * support an arbitrary depth of locking here, but absolute limits on
454*b63da6c8SRandy Dunlap 	 * inodes depend on the type of locking and the limits placed by
4550952c818SDave Chinner 	 * lockdep annotations in xfs_lock_inumorder.  These are all checked by
4560952c818SDave Chinner 	 * the asserts.
4570952c818SDave Chinner 	 */
45895afcf5cSDave Chinner 	ASSERT(ips && inodes >= 2 && inodes <= 5);
4590952c818SDave Chinner 	ASSERT(lock_mode & (XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL |
4600952c818SDave Chinner 			    XFS_ILOCK_EXCL));
4610952c818SDave Chinner 	ASSERT(!(lock_mode & (XFS_IOLOCK_SHARED | XFS_MMAPLOCK_SHARED |
4620952c818SDave Chinner 			      XFS_ILOCK_SHARED)));
4630952c818SDave Chinner 	ASSERT(!(lock_mode & XFS_MMAPLOCK_EXCL) ||
4640952c818SDave Chinner 		inodes <= XFS_MMAPLOCK_MAX_SUBCLASS + 1);
4650952c818SDave Chinner 	ASSERT(!(lock_mode & XFS_ILOCK_EXCL) ||
4660952c818SDave Chinner 		inodes <= XFS_ILOCK_MAX_SUBCLASS + 1);
4670952c818SDave Chinner 
4680952c818SDave Chinner 	if (lock_mode & XFS_IOLOCK_EXCL) {
4690952c818SDave Chinner 		ASSERT(!(lock_mode & (XFS_MMAPLOCK_EXCL | XFS_ILOCK_EXCL)));
4700952c818SDave Chinner 	} else if (lock_mode & XFS_MMAPLOCK_EXCL)
4710952c818SDave Chinner 		ASSERT(!(lock_mode & XFS_ILOCK_EXCL));
472c24b5dfaSDave Chinner 
473c24b5dfaSDave Chinner 	try_lock = 0;
474c24b5dfaSDave Chinner 	i = 0;
475c24b5dfaSDave Chinner again:
476c24b5dfaSDave Chinner 	for (; i < inodes; i++) {
477c24b5dfaSDave Chinner 		ASSERT(ips[i]);
478c24b5dfaSDave Chinner 
479c24b5dfaSDave Chinner 		if (i && (ips[i] == ips[i - 1]))	/* Already locked */
480c24b5dfaSDave Chinner 			continue;
481c24b5dfaSDave Chinner 
482c24b5dfaSDave Chinner 		/*
48395afcf5cSDave Chinner 		 * If try_lock is not set yet, make sure all locked inodes are
48495afcf5cSDave Chinner 		 * not in the AIL.  If any are, set try_lock to be used later.
485c24b5dfaSDave Chinner 		 */
486c24b5dfaSDave Chinner 		if (!try_lock) {
487c24b5dfaSDave Chinner 			for (j = (i - 1); j >= 0 && !try_lock; j--) {
488b3b14aacSChristoph Hellwig 				lp = &ips[j]->i_itemp->ili_item;
48922525c17SDave Chinner 				if (lp && test_bit(XFS_LI_IN_AIL, &lp->li_flags))
490c24b5dfaSDave Chinner 					try_lock++;
491c24b5dfaSDave Chinner 			}
492c24b5dfaSDave Chinner 		}
493c24b5dfaSDave Chinner 
494c24b5dfaSDave Chinner 		/*
495c24b5dfaSDave Chinner 		 * If any of the previous locks we have locked is in the AIL,
496c24b5dfaSDave Chinner 		 * we must TRY to get the second and subsequent locks. If
497c24b5dfaSDave Chinner 		 * we can't get any, we must release all we have
498c24b5dfaSDave Chinner 		 * and try again.
499c24b5dfaSDave Chinner 		 */
50095afcf5cSDave Chinner 		if (!try_lock) {
50195afcf5cSDave Chinner 			xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
50295afcf5cSDave Chinner 			continue;
50395afcf5cSDave Chinner 		}
504c24b5dfaSDave Chinner 
50595afcf5cSDave Chinner 		/* try_lock means we have an inode locked that is in the AIL. */
506c24b5dfaSDave Chinner 		ASSERT(i != 0);
50795afcf5cSDave Chinner 		if (xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i)))
50895afcf5cSDave Chinner 			continue;
50995afcf5cSDave Chinner 
51095afcf5cSDave Chinner 		/*
51195afcf5cSDave Chinner 		 * Unlock all previous guys and try again.  xfs_iunlock will try
51295afcf5cSDave Chinner 		 * to push the tail if the inode is in the AIL.
51395afcf5cSDave Chinner 		 */
514c24b5dfaSDave Chinner 		attempts++;
515c24b5dfaSDave Chinner 		for (j = i - 1; j >= 0; j--) {
516c24b5dfaSDave Chinner 			/*
51795afcf5cSDave Chinner 			 * Check to see if we've already unlocked this one.  Not
51895afcf5cSDave Chinner 			 * the first one going back, and the inode ptr is the
51995afcf5cSDave Chinner 			 * same.
520c24b5dfaSDave Chinner 			 */
52195afcf5cSDave Chinner 			if (j != (i - 1) && ips[j] == ips[j + 1])
522c24b5dfaSDave Chinner 				continue;
523c24b5dfaSDave Chinner 
524c24b5dfaSDave Chinner 			xfs_iunlock(ips[j], lock_mode);
525c24b5dfaSDave Chinner 		}
526c24b5dfaSDave Chinner 
527c24b5dfaSDave Chinner 		if ((attempts % 5) == 0) {
528c24b5dfaSDave Chinner 			delay(1); /* Don't just spin the CPU */
529c24b5dfaSDave Chinner 		}
530c24b5dfaSDave Chinner 		i = 0;
531c24b5dfaSDave Chinner 		try_lock = 0;
532c24b5dfaSDave Chinner 		goto again;
533c24b5dfaSDave Chinner 	}
534c24b5dfaSDave Chinner }
535c24b5dfaSDave Chinner 
536c24b5dfaSDave Chinner /*
537653c60b6SDave Chinner  * xfs_lock_two_inodes() can only be used to lock one type of lock at a time -
5387c2d238aSDarrick J. Wong  * the mmaplock or the ilock, but not more than one type at a time. If we lock
5397c2d238aSDarrick J. Wong  * more than one at a time, lockdep will report false positives saying we have
5407c2d238aSDarrick J. Wong  * violated locking orders.  The iolock must be double-locked separately since
5417c2d238aSDarrick J. Wong  * we use i_rwsem for that.  We now support taking one lock EXCL and the other
5427c2d238aSDarrick J. Wong  * SHARED.
543c24b5dfaSDave Chinner  */
544c24b5dfaSDave Chinner void
545c24b5dfaSDave Chinner xfs_lock_two_inodes(
5467c2d238aSDarrick J. Wong 	struct xfs_inode	*ip0,
5477c2d238aSDarrick J. Wong 	uint			ip0_mode,
5487c2d238aSDarrick J. Wong 	struct xfs_inode	*ip1,
5497c2d238aSDarrick J. Wong 	uint			ip1_mode)
550c24b5dfaSDave Chinner {
5517c2d238aSDarrick J. Wong 	struct xfs_inode	*temp;
5527c2d238aSDarrick J. Wong 	uint			mode_temp;
553c24b5dfaSDave Chinner 	int			attempts = 0;
554efe2330fSChristoph Hellwig 	struct xfs_log_item	*lp;
555c24b5dfaSDave Chinner 
5567c2d238aSDarrick J. Wong 	ASSERT(hweight32(ip0_mode) == 1);
5577c2d238aSDarrick J. Wong 	ASSERT(hweight32(ip1_mode) == 1);
5587c2d238aSDarrick J. Wong 	ASSERT(!(ip0_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)));
5597c2d238aSDarrick J. Wong 	ASSERT(!(ip1_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)));
5607c2d238aSDarrick J. Wong 	ASSERT(!(ip0_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) ||
5617c2d238aSDarrick J. Wong 	       !(ip0_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
5627c2d238aSDarrick J. Wong 	ASSERT(!(ip1_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) ||
5637c2d238aSDarrick J. Wong 	       !(ip1_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
5647c2d238aSDarrick J. Wong 	ASSERT(!(ip1_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) ||
5657c2d238aSDarrick J. Wong 	       !(ip0_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
5667c2d238aSDarrick J. Wong 	ASSERT(!(ip0_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) ||
5677c2d238aSDarrick J. Wong 	       !(ip1_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
568653c60b6SDave Chinner 
569c24b5dfaSDave Chinner 	ASSERT(ip0->i_ino != ip1->i_ino);
570c24b5dfaSDave Chinner 
571c24b5dfaSDave Chinner 	if (ip0->i_ino > ip1->i_ino) {
572c24b5dfaSDave Chinner 		temp = ip0;
573c24b5dfaSDave Chinner 		ip0 = ip1;
574c24b5dfaSDave Chinner 		ip1 = temp;
5757c2d238aSDarrick J. Wong 		mode_temp = ip0_mode;
5767c2d238aSDarrick J. Wong 		ip0_mode = ip1_mode;
5777c2d238aSDarrick J. Wong 		ip1_mode = mode_temp;
578c24b5dfaSDave Chinner 	}
579c24b5dfaSDave Chinner 
580c24b5dfaSDave Chinner  again:
5817c2d238aSDarrick J. Wong 	xfs_ilock(ip0, xfs_lock_inumorder(ip0_mode, 0));
582c24b5dfaSDave Chinner 
583c24b5dfaSDave Chinner 	/*
584c24b5dfaSDave Chinner 	 * If the first lock we have locked is in the AIL, we must TRY to get
585c24b5dfaSDave Chinner 	 * the second lock. If we can't get it, we must release the first one
586c24b5dfaSDave Chinner 	 * and try again.
587c24b5dfaSDave Chinner 	 */
588b3b14aacSChristoph Hellwig 	lp = &ip0->i_itemp->ili_item;
58922525c17SDave Chinner 	if (lp && test_bit(XFS_LI_IN_AIL, &lp->li_flags)) {
5907c2d238aSDarrick J. Wong 		if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(ip1_mode, 1))) {
5917c2d238aSDarrick J. Wong 			xfs_iunlock(ip0, ip0_mode);
592c24b5dfaSDave Chinner 			if ((++attempts % 5) == 0)
593c24b5dfaSDave Chinner 				delay(1); /* Don't just spin the CPU */
594c24b5dfaSDave Chinner 			goto again;
595c24b5dfaSDave Chinner 		}
596c24b5dfaSDave Chinner 	} else {
5977c2d238aSDarrick J. Wong 		xfs_ilock(ip1, xfs_lock_inumorder(ip1_mode, 1));
598c24b5dfaSDave Chinner 	}
599c24b5dfaSDave Chinner }
600c24b5dfaSDave Chinner 
601fa96acadSDave Chinner void
602fa96acadSDave Chinner __xfs_iflock(
603fa96acadSDave Chinner 	struct xfs_inode	*ip)
604fa96acadSDave Chinner {
605fa96acadSDave Chinner 	wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IFLOCK_BIT);
606fa96acadSDave Chinner 	DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IFLOCK_BIT);
607fa96acadSDave Chinner 
608fa96acadSDave Chinner 	do {
60921417136SIngo Molnar 		prepare_to_wait_exclusive(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
610fa96acadSDave Chinner 		if (xfs_isiflocked(ip))
611fa96acadSDave Chinner 			io_schedule();
612fa96acadSDave Chinner 	} while (!xfs_iflock_nowait(ip));
613fa96acadSDave Chinner 
61421417136SIngo Molnar 	finish_wait(wq, &wait.wq_entry);
615fa96acadSDave Chinner }
616fa96acadSDave Chinner 
6171da177e4SLinus Torvalds STATIC uint
6181da177e4SLinus Torvalds _xfs_dic2xflags(
619c8ce540dSDarrick J. Wong 	uint16_t		di_flags,
62058f88ca2SDave Chinner 	uint64_t		di_flags2,
62158f88ca2SDave Chinner 	bool			has_attr)
6221da177e4SLinus Torvalds {
6231da177e4SLinus Torvalds 	uint			flags = 0;
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 	if (di_flags & XFS_DIFLAG_ANY) {
6261da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_REALTIME)
627e7b89481SDave Chinner 			flags |= FS_XFLAG_REALTIME;
6281da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_PREALLOC)
629e7b89481SDave Chinner 			flags |= FS_XFLAG_PREALLOC;
6301da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_IMMUTABLE)
631e7b89481SDave Chinner 			flags |= FS_XFLAG_IMMUTABLE;
6321da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_APPEND)
633e7b89481SDave Chinner 			flags |= FS_XFLAG_APPEND;
6341da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_SYNC)
635e7b89481SDave Chinner 			flags |= FS_XFLAG_SYNC;
6361da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_NOATIME)
637e7b89481SDave Chinner 			flags |= FS_XFLAG_NOATIME;
6381da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_NODUMP)
639e7b89481SDave Chinner 			flags |= FS_XFLAG_NODUMP;
6401da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_RTINHERIT)
641e7b89481SDave Chinner 			flags |= FS_XFLAG_RTINHERIT;
6421da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_PROJINHERIT)
643e7b89481SDave Chinner 			flags |= FS_XFLAG_PROJINHERIT;
6441da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_NOSYMLINKS)
645e7b89481SDave Chinner 			flags |= FS_XFLAG_NOSYMLINKS;
646dd9f438eSNathan Scott 		if (di_flags & XFS_DIFLAG_EXTSIZE)
647e7b89481SDave Chinner 			flags |= FS_XFLAG_EXTSIZE;
648dd9f438eSNathan Scott 		if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
649e7b89481SDave Chinner 			flags |= FS_XFLAG_EXTSZINHERIT;
650d3446eacSBarry Naujok 		if (di_flags & XFS_DIFLAG_NODEFRAG)
651e7b89481SDave Chinner 			flags |= FS_XFLAG_NODEFRAG;
6522a82b8beSDavid Chinner 		if (di_flags & XFS_DIFLAG_FILESTREAM)
653e7b89481SDave Chinner 			flags |= FS_XFLAG_FILESTREAM;
6541da177e4SLinus Torvalds 	}
6551da177e4SLinus Torvalds 
65658f88ca2SDave Chinner 	if (di_flags2 & XFS_DIFLAG2_ANY) {
65758f88ca2SDave Chinner 		if (di_flags2 & XFS_DIFLAG2_DAX)
65858f88ca2SDave Chinner 			flags |= FS_XFLAG_DAX;
659f7ca3522SDarrick J. Wong 		if (di_flags2 & XFS_DIFLAG2_COWEXTSIZE)
660f7ca3522SDarrick J. Wong 			flags |= FS_XFLAG_COWEXTSIZE;
66158f88ca2SDave Chinner 	}
66258f88ca2SDave Chinner 
66358f88ca2SDave Chinner 	if (has_attr)
66458f88ca2SDave Chinner 		flags |= FS_XFLAG_HASATTR;
66558f88ca2SDave Chinner 
6661da177e4SLinus Torvalds 	return flags;
6671da177e4SLinus Torvalds }
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds uint
6701da177e4SLinus Torvalds xfs_ip2xflags(
67158f88ca2SDave Chinner 	struct xfs_inode	*ip)
6721da177e4SLinus Torvalds {
67358f88ca2SDave Chinner 	struct xfs_icdinode	*dic = &ip->i_d;
6741da177e4SLinus Torvalds 
67558f88ca2SDave Chinner 	return _xfs_dic2xflags(dic->di_flags, dic->di_flags2, XFS_IFORK_Q(ip));
6761da177e4SLinus Torvalds }
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds /*
679c24b5dfaSDave Chinner  * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
680c24b5dfaSDave Chinner  * is allowed, otherwise it has to be an exact match. If a CI match is found,
681c24b5dfaSDave Chinner  * ci_name->name will point to a the actual name (caller must free) or
682c24b5dfaSDave Chinner  * will be set to NULL if an exact match is found.
683c24b5dfaSDave Chinner  */
684c24b5dfaSDave Chinner int
685c24b5dfaSDave Chinner xfs_lookup(
686c24b5dfaSDave Chinner 	xfs_inode_t		*dp,
687c24b5dfaSDave Chinner 	struct xfs_name		*name,
688c24b5dfaSDave Chinner 	xfs_inode_t		**ipp,
689c24b5dfaSDave Chinner 	struct xfs_name		*ci_name)
690c24b5dfaSDave Chinner {
691c24b5dfaSDave Chinner 	xfs_ino_t		inum;
692c24b5dfaSDave Chinner 	int			error;
693c24b5dfaSDave Chinner 
694c24b5dfaSDave Chinner 	trace_xfs_lookup(dp, name);
695c24b5dfaSDave Chinner 
696c24b5dfaSDave Chinner 	if (XFS_FORCED_SHUTDOWN(dp->i_mount))
6972451337dSDave Chinner 		return -EIO;
698c24b5dfaSDave Chinner 
699c24b5dfaSDave Chinner 	error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
700c24b5dfaSDave Chinner 	if (error)
701dbad7c99SDave Chinner 		goto out_unlock;
702c24b5dfaSDave Chinner 
703c24b5dfaSDave Chinner 	error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
704c24b5dfaSDave Chinner 	if (error)
705c24b5dfaSDave Chinner 		goto out_free_name;
706c24b5dfaSDave Chinner 
707c24b5dfaSDave Chinner 	return 0;
708c24b5dfaSDave Chinner 
709c24b5dfaSDave Chinner out_free_name:
710c24b5dfaSDave Chinner 	if (ci_name)
711c24b5dfaSDave Chinner 		kmem_free(ci_name->name);
712dbad7c99SDave Chinner out_unlock:
713c24b5dfaSDave Chinner 	*ipp = NULL;
714c24b5dfaSDave Chinner 	return error;
715c24b5dfaSDave Chinner }
716c24b5dfaSDave Chinner 
717c24b5dfaSDave Chinner /*
7181da177e4SLinus Torvalds  * Allocate an inode on disk and return a copy of its in-core version.
7191da177e4SLinus Torvalds  * The in-core inode is locked exclusively.  Set mode, nlink, and rdev
7201da177e4SLinus Torvalds  * appropriately within the inode.  The uid and gid for the inode are
7211da177e4SLinus Torvalds  * set according to the contents of the given cred structure.
7221da177e4SLinus Torvalds  *
7231da177e4SLinus Torvalds  * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
724cd856db6SCarlos Maiolino  * has a free inode available, call xfs_iget() to obtain the in-core
725cd856db6SCarlos Maiolino  * version of the allocated inode.  Finally, fill in the inode and
726cd856db6SCarlos Maiolino  * log its initial contents.  In this case, ialloc_context would be
727cd856db6SCarlos Maiolino  * set to NULL.
7281da177e4SLinus Torvalds  *
729cd856db6SCarlos Maiolino  * If xfs_dialloc() does not have an available inode, it will replenish
730cd856db6SCarlos Maiolino  * its supply by doing an allocation. Since we can only do one
731cd856db6SCarlos Maiolino  * allocation within a transaction without deadlocks, we must commit
732cd856db6SCarlos Maiolino  * the current transaction before returning the inode itself.
733cd856db6SCarlos Maiolino  * In this case, therefore, we will set ialloc_context and return.
7341da177e4SLinus Torvalds  * The caller should then commit the current transaction, start a new
7351da177e4SLinus Torvalds  * transaction, and call xfs_ialloc() again to actually get the inode.
7361da177e4SLinus Torvalds  *
7371da177e4SLinus Torvalds  * To ensure that some other process does not grab the inode that
7381da177e4SLinus Torvalds  * was allocated during the first call to xfs_ialloc(), this routine
7391da177e4SLinus Torvalds  * also returns the [locked] bp pointing to the head of the freelist
7401da177e4SLinus Torvalds  * as ialloc_context.  The caller should hold this buffer across
7411da177e4SLinus Torvalds  * the commit and pass it back into this routine on the second call.
742b11f94d5SDavid Chinner  *
743b11f94d5SDavid Chinner  * If we are allocating quota inodes, we do not have a parent inode
744b11f94d5SDavid Chinner  * to attach to or associate with (i.e. pip == NULL) because they
745b11f94d5SDavid Chinner  * are not linked into the directory structure - they are attached
746b11f94d5SDavid Chinner  * directly to the superblock - and so have no parent.
7471da177e4SLinus Torvalds  */
7480d5a75e9SEric Sandeen static int
7491da177e4SLinus Torvalds xfs_ialloc(
7501da177e4SLinus Torvalds 	xfs_trans_t	*tp,
7511da177e4SLinus Torvalds 	xfs_inode_t	*pip,
752576b1d67SAl Viro 	umode_t		mode,
75331b084aeSNathan Scott 	xfs_nlink_t	nlink,
75466f36464SChristoph Hellwig 	dev_t		rdev,
7556743099cSArkadiusz Mi?kiewicz 	prid_t		prid,
7561da177e4SLinus Torvalds 	xfs_buf_t	**ialloc_context,
7571da177e4SLinus Torvalds 	xfs_inode_t	**ipp)
7581da177e4SLinus Torvalds {
75993848a99SChristoph Hellwig 	struct xfs_mount *mp = tp->t_mountp;
7601da177e4SLinus Torvalds 	xfs_ino_t	ino;
7611da177e4SLinus Torvalds 	xfs_inode_t	*ip;
7621da177e4SLinus Torvalds 	uint		flags;
7631da177e4SLinus Torvalds 	int		error;
76495582b00SDeepa Dinamani 	struct timespec64 tv;
7653987848cSDave Chinner 	struct inode	*inode;
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds 	/*
7681da177e4SLinus Torvalds 	 * Call the space management code to pick
7691da177e4SLinus Torvalds 	 * the on-disk inode to be allocated.
7701da177e4SLinus Torvalds 	 */
771f59cf5c2SChristoph Hellwig 	error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode,
77208358906SChristoph Hellwig 			    ialloc_context, &ino);
773bf904248SDavid Chinner 	if (error)
7741da177e4SLinus Torvalds 		return error;
77508358906SChristoph Hellwig 	if (*ialloc_context || ino == NULLFSINO) {
7761da177e4SLinus Torvalds 		*ipp = NULL;
7771da177e4SLinus Torvalds 		return 0;
7781da177e4SLinus Torvalds 	}
7791da177e4SLinus Torvalds 	ASSERT(*ialloc_context == NULL);
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 	/*
7828b26984dSDave Chinner 	 * Protect against obviously corrupt allocation btree records. Later
7838b26984dSDave Chinner 	 * xfs_iget checks will catch re-allocation of other active in-memory
7848b26984dSDave Chinner 	 * and on-disk inodes. If we don't catch reallocating the parent inode
7858b26984dSDave Chinner 	 * here we will deadlock in xfs_iget() so we have to do these checks
7868b26984dSDave Chinner 	 * first.
7878b26984dSDave Chinner 	 */
7888b26984dSDave Chinner 	if ((pip && ino == pip->i_ino) || !xfs_verify_dir_ino(mp, ino)) {
7898b26984dSDave Chinner 		xfs_alert(mp, "Allocated a known in-use inode 0x%llx!", ino);
7908b26984dSDave Chinner 		return -EFSCORRUPTED;
7918b26984dSDave Chinner 	}
7928b26984dSDave Chinner 
7938b26984dSDave Chinner 	/*
7941da177e4SLinus Torvalds 	 * Get the in-core inode with the lock held exclusively.
7951da177e4SLinus Torvalds 	 * This is because we're setting fields here we need
7961da177e4SLinus Torvalds 	 * to prevent others from looking at until we're done.
7971da177e4SLinus Torvalds 	 */
79893848a99SChristoph Hellwig 	error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE,
799ec3ba85fSChristoph Hellwig 			 XFS_ILOCK_EXCL, &ip);
800bf904248SDavid Chinner 	if (error)
8011da177e4SLinus Torvalds 		return error;
8021da177e4SLinus Torvalds 	ASSERT(ip != NULL);
8033987848cSDave Chinner 	inode = VFS_I(ip);
804c19b3b05SDave Chinner 	inode->i_mode = mode;
80554d7b5c1SDave Chinner 	set_nlink(inode, nlink);
8063d8f2821SChristoph Hellwig 	inode->i_uid = current_fsuid();
80766f36464SChristoph Hellwig 	inode->i_rdev = rdev;
808de7a866fSChristoph Hellwig 	ip->i_d.di_projid = prid;
8091da177e4SLinus Torvalds 
810bd186aa9SChristoph Hellwig 	if (pip && XFS_INHERIT_GID(pip)) {
8113d8f2821SChristoph Hellwig 		inode->i_gid = VFS_I(pip)->i_gid;
812c19b3b05SDave Chinner 		if ((VFS_I(pip)->i_mode & S_ISGID) && S_ISDIR(mode))
813c19b3b05SDave Chinner 			inode->i_mode |= S_ISGID;
8143d8f2821SChristoph Hellwig 	} else {
8153d8f2821SChristoph Hellwig 		inode->i_gid = current_fsgid();
8161da177e4SLinus Torvalds 	}
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds 	/*
8191da177e4SLinus Torvalds 	 * If the group ID of the new file does not match the effective group
8201da177e4SLinus Torvalds 	 * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
8211da177e4SLinus Torvalds 	 * (and only if the irix_sgid_inherit compatibility variable is set).
8221da177e4SLinus Torvalds 	 */
82354295159SChristoph Hellwig 	if (irix_sgid_inherit &&
82454295159SChristoph Hellwig 	    (inode->i_mode & S_ISGID) && !in_group_p(inode->i_gid))
825c19b3b05SDave Chinner 		inode->i_mode &= ~S_ISGID;
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds 	ip->i_d.di_size = 0;
828daf83964SChristoph Hellwig 	ip->i_df.if_nextents = 0;
8291da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_nblocks == 0);
830dff35fd4SChristoph Hellwig 
831c2050a45SDeepa Dinamani 	tv = current_time(inode);
8323987848cSDave Chinner 	inode->i_mtime = tv;
8333987848cSDave Chinner 	inode->i_atime = tv;
8343987848cSDave Chinner 	inode->i_ctime = tv;
835dff35fd4SChristoph Hellwig 
8361da177e4SLinus Torvalds 	ip->i_d.di_extsize = 0;
8371da177e4SLinus Torvalds 	ip->i_d.di_dmevmask = 0;
8381da177e4SLinus Torvalds 	ip->i_d.di_dmstate = 0;
8391da177e4SLinus Torvalds 	ip->i_d.di_flags = 0;
84093848a99SChristoph Hellwig 
8416471e9c5SChristoph Hellwig 	if (xfs_sb_version_has_v3inode(&mp->m_sb)) {
842f0e28280SJeff Layton 		inode_set_iversion(inode, 1);
84393848a99SChristoph Hellwig 		ip->i_d.di_flags2 = 0;
844f7ca3522SDarrick J. Wong 		ip->i_d.di_cowextsize = 0;
8458d2d878dSChristoph Hellwig 		ip->i_d.di_crtime = tv;
84693848a99SChristoph Hellwig 	}
84793848a99SChristoph Hellwig 
8481da177e4SLinus Torvalds 	flags = XFS_ILOG_CORE;
8491da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
8501da177e4SLinus Torvalds 	case S_IFIFO:
8511da177e4SLinus Torvalds 	case S_IFCHR:
8521da177e4SLinus Torvalds 	case S_IFBLK:
8531da177e4SLinus Torvalds 	case S_IFSOCK:
854f7e67b20SChristoph Hellwig 		ip->i_df.if_format = XFS_DINODE_FMT_DEV;
8551da177e4SLinus Torvalds 		ip->i_df.if_flags = 0;
8561da177e4SLinus Torvalds 		flags |= XFS_ILOG_DEV;
8571da177e4SLinus Torvalds 		break;
8581da177e4SLinus Torvalds 	case S_IFREG:
8591da177e4SLinus Torvalds 	case S_IFDIR:
860b11f94d5SDavid Chinner 		if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
861365ca83dSNathan Scott 			uint		di_flags = 0;
862365ca83dSNathan Scott 
863abbede1bSAl Viro 			if (S_ISDIR(mode)) {
864365ca83dSNathan Scott 				if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
865365ca83dSNathan Scott 					di_flags |= XFS_DIFLAG_RTINHERIT;
866dd9f438eSNathan Scott 				if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
867dd9f438eSNathan Scott 					di_flags |= XFS_DIFLAG_EXTSZINHERIT;
868dd9f438eSNathan Scott 					ip->i_d.di_extsize = pip->i_d.di_extsize;
869dd9f438eSNathan Scott 				}
8709336e3a7SDave Chinner 				if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
8719336e3a7SDave Chinner 					di_flags |= XFS_DIFLAG_PROJINHERIT;
872abbede1bSAl Viro 			} else if (S_ISREG(mode)) {
873613d7043SChristoph Hellwig 				if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
874365ca83dSNathan Scott 					di_flags |= XFS_DIFLAG_REALTIME;
875dd9f438eSNathan Scott 				if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
876dd9f438eSNathan Scott 					di_flags |= XFS_DIFLAG_EXTSIZE;
877dd9f438eSNathan Scott 					ip->i_d.di_extsize = pip->i_d.di_extsize;
878dd9f438eSNathan Scott 				}
8791da177e4SLinus Torvalds 			}
8801da177e4SLinus Torvalds 			if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
8811da177e4SLinus Torvalds 			    xfs_inherit_noatime)
882365ca83dSNathan Scott 				di_flags |= XFS_DIFLAG_NOATIME;
8831da177e4SLinus Torvalds 			if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
8841da177e4SLinus Torvalds 			    xfs_inherit_nodump)
885365ca83dSNathan Scott 				di_flags |= XFS_DIFLAG_NODUMP;
8861da177e4SLinus Torvalds 			if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
8871da177e4SLinus Torvalds 			    xfs_inherit_sync)
888365ca83dSNathan Scott 				di_flags |= XFS_DIFLAG_SYNC;
8891da177e4SLinus Torvalds 			if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
8901da177e4SLinus Torvalds 			    xfs_inherit_nosymlinks)
891365ca83dSNathan Scott 				di_flags |= XFS_DIFLAG_NOSYMLINKS;
892d3446eacSBarry Naujok 			if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
893d3446eacSBarry Naujok 			    xfs_inherit_nodefrag)
894d3446eacSBarry Naujok 				di_flags |= XFS_DIFLAG_NODEFRAG;
8952a82b8beSDavid Chinner 			if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
8962a82b8beSDavid Chinner 				di_flags |= XFS_DIFLAG_FILESTREAM;
89758f88ca2SDave Chinner 
898365ca83dSNathan Scott 			ip->i_d.di_flags |= di_flags;
8991da177e4SLinus Torvalds 		}
900b3d1d375SChristoph Hellwig 		if (pip && (pip->i_d.di_flags2 & XFS_DIFLAG2_ANY)) {
901f7ca3522SDarrick J. Wong 			if (pip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) {
902b3d1d375SChristoph Hellwig 				ip->i_d.di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
903f7ca3522SDarrick J. Wong 				ip->i_d.di_cowextsize = pip->i_d.di_cowextsize;
904f7ca3522SDarrick J. Wong 			}
90556bdf855SLukas Czerner 			if (pip->i_d.di_flags2 & XFS_DIFLAG2_DAX)
906b3d1d375SChristoph Hellwig 				ip->i_d.di_flags2 |= XFS_DIFLAG2_DAX;
907f7ca3522SDarrick J. Wong 		}
9081da177e4SLinus Torvalds 		/* FALLTHROUGH */
9091da177e4SLinus Torvalds 	case S_IFLNK:
910f7e67b20SChristoph Hellwig 		ip->i_df.if_format = XFS_DINODE_FMT_EXTENTS;
9111da177e4SLinus Torvalds 		ip->i_df.if_flags = XFS_IFEXTENTS;
912fcacbc3fSChristoph Hellwig 		ip->i_df.if_bytes = 0;
9136bdcf26aSChristoph Hellwig 		ip->i_df.if_u1.if_root = NULL;
9141da177e4SLinus Torvalds 		break;
9151da177e4SLinus Torvalds 	default:
9161da177e4SLinus Torvalds 		ASSERT(0);
9171da177e4SLinus Torvalds 	}
9181da177e4SLinus Torvalds 
9191da177e4SLinus Torvalds 	/*
9201da177e4SLinus Torvalds 	 * Log the new values stuffed into the inode.
9211da177e4SLinus Torvalds 	 */
922ddc3415aSChristoph Hellwig 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
9231da177e4SLinus Torvalds 	xfs_trans_log_inode(tp, ip, flags);
9241da177e4SLinus Torvalds 
92558c90473SDave Chinner 	/* now that we have an i_mode we can setup the inode structure */
92641be8bedSChristoph Hellwig 	xfs_setup_inode(ip);
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds 	*ipp = ip;
9291da177e4SLinus Torvalds 	return 0;
9301da177e4SLinus Torvalds }
9311da177e4SLinus Torvalds 
932e546cb79SDave Chinner /*
933e546cb79SDave Chinner  * Allocates a new inode from disk and return a pointer to the
934e546cb79SDave Chinner  * incore copy. This routine will internally commit the current
935e546cb79SDave Chinner  * transaction and allocate a new one if the Space Manager needed
936e546cb79SDave Chinner  * to do an allocation to replenish the inode free-list.
937e546cb79SDave Chinner  *
938e546cb79SDave Chinner  * This routine is designed to be called from xfs_create and
939e546cb79SDave Chinner  * xfs_create_dir.
940e546cb79SDave Chinner  *
941e546cb79SDave Chinner  */
942e546cb79SDave Chinner int
943e546cb79SDave Chinner xfs_dir_ialloc(
944e546cb79SDave Chinner 	xfs_trans_t	**tpp,		/* input: current transaction;
945e546cb79SDave Chinner 					   output: may be a new transaction. */
946e546cb79SDave Chinner 	xfs_inode_t	*dp,		/* directory within whose allocate
947e546cb79SDave Chinner 					   the inode. */
948e546cb79SDave Chinner 	umode_t		mode,
949e546cb79SDave Chinner 	xfs_nlink_t	nlink,
95066f36464SChristoph Hellwig 	dev_t		rdev,
951e546cb79SDave Chinner 	prid_t		prid,		/* project id */
952c959025eSChandan Rajendra 	xfs_inode_t	**ipp)		/* pointer to inode; it will be
953e546cb79SDave Chinner 					   locked. */
954e546cb79SDave Chinner {
955e546cb79SDave Chinner 	xfs_trans_t	*tp;
956e546cb79SDave Chinner 	xfs_inode_t	*ip;
957e546cb79SDave Chinner 	xfs_buf_t	*ialloc_context = NULL;
958e546cb79SDave Chinner 	int		code;
959e546cb79SDave Chinner 	void		*dqinfo;
960e546cb79SDave Chinner 	uint		tflags;
961e546cb79SDave Chinner 
962e546cb79SDave Chinner 	tp = *tpp;
963e546cb79SDave Chinner 	ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
964e546cb79SDave Chinner 
965e546cb79SDave Chinner 	/*
966e546cb79SDave Chinner 	 * xfs_ialloc will return a pointer to an incore inode if
967e546cb79SDave Chinner 	 * the Space Manager has an available inode on the free
968e546cb79SDave Chinner 	 * list. Otherwise, it will do an allocation and replenish
969e546cb79SDave Chinner 	 * the freelist.  Since we can only do one allocation per
970e546cb79SDave Chinner 	 * transaction without deadlocks, we will need to commit the
971e546cb79SDave Chinner 	 * current transaction and start a new one.  We will then
972e546cb79SDave Chinner 	 * need to call xfs_ialloc again to get the inode.
973e546cb79SDave Chinner 	 *
974e546cb79SDave Chinner 	 * If xfs_ialloc did an allocation to replenish the freelist,
975e546cb79SDave Chinner 	 * it returns the bp containing the head of the freelist as
976e546cb79SDave Chinner 	 * ialloc_context. We will hold a lock on it across the
977e546cb79SDave Chinner 	 * transaction commit so that no other process can steal
978e546cb79SDave Chinner 	 * the inode(s) that we've just allocated.
979e546cb79SDave Chinner 	 */
980f59cf5c2SChristoph Hellwig 	code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid, &ialloc_context,
981f59cf5c2SChristoph Hellwig 			&ip);
982e546cb79SDave Chinner 
983e546cb79SDave Chinner 	/*
984e546cb79SDave Chinner 	 * Return an error if we were unable to allocate a new inode.
985e546cb79SDave Chinner 	 * This should only happen if we run out of space on disk or
986e546cb79SDave Chinner 	 * encounter a disk error.
987e546cb79SDave Chinner 	 */
988e546cb79SDave Chinner 	if (code) {
989e546cb79SDave Chinner 		*ipp = NULL;
990e546cb79SDave Chinner 		return code;
991e546cb79SDave Chinner 	}
992e546cb79SDave Chinner 	if (!ialloc_context && !ip) {
993e546cb79SDave Chinner 		*ipp = NULL;
9942451337dSDave Chinner 		return -ENOSPC;
995e546cb79SDave Chinner 	}
996e546cb79SDave Chinner 
997e546cb79SDave Chinner 	/*
998e546cb79SDave Chinner 	 * If the AGI buffer is non-NULL, then we were unable to get an
999e546cb79SDave Chinner 	 * inode in one operation.  We need to commit the current
1000e546cb79SDave Chinner 	 * transaction and call xfs_ialloc() again.  It is guaranteed
1001e546cb79SDave Chinner 	 * to succeed the second time.
1002e546cb79SDave Chinner 	 */
1003e546cb79SDave Chinner 	if (ialloc_context) {
1004e546cb79SDave Chinner 		/*
1005e546cb79SDave Chinner 		 * Normally, xfs_trans_commit releases all the locks.
1006e546cb79SDave Chinner 		 * We call bhold to hang on to the ialloc_context across
1007e546cb79SDave Chinner 		 * the commit.  Holding this buffer prevents any other
1008e546cb79SDave Chinner 		 * processes from doing any allocations in this
1009e546cb79SDave Chinner 		 * allocation group.
1010e546cb79SDave Chinner 		 */
1011e546cb79SDave Chinner 		xfs_trans_bhold(tp, ialloc_context);
1012e546cb79SDave Chinner 
1013e546cb79SDave Chinner 		/*
1014e546cb79SDave Chinner 		 * We want the quota changes to be associated with the next
1015e546cb79SDave Chinner 		 * transaction, NOT this one. So, detach the dqinfo from this
1016e546cb79SDave Chinner 		 * and attach it to the next transaction.
1017e546cb79SDave Chinner 		 */
1018e546cb79SDave Chinner 		dqinfo = NULL;
1019e546cb79SDave Chinner 		tflags = 0;
1020e546cb79SDave Chinner 		if (tp->t_dqinfo) {
1021e546cb79SDave Chinner 			dqinfo = (void *)tp->t_dqinfo;
1022e546cb79SDave Chinner 			tp->t_dqinfo = NULL;
1023e546cb79SDave Chinner 			tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY;
1024e546cb79SDave Chinner 			tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY);
1025e546cb79SDave Chinner 		}
1026e546cb79SDave Chinner 
1027411350dfSChristoph Hellwig 		code = xfs_trans_roll(&tp);
10283d3c8b52SJie Liu 
1029e546cb79SDave Chinner 		/*
1030e546cb79SDave Chinner 		 * Re-attach the quota info that we detached from prev trx.
1031e546cb79SDave Chinner 		 */
1032e546cb79SDave Chinner 		if (dqinfo) {
1033e546cb79SDave Chinner 			tp->t_dqinfo = dqinfo;
1034e546cb79SDave Chinner 			tp->t_flags |= tflags;
1035e546cb79SDave Chinner 		}
1036e546cb79SDave Chinner 
1037e546cb79SDave Chinner 		if (code) {
1038e546cb79SDave Chinner 			xfs_buf_relse(ialloc_context);
10392e6db6c4SChristoph Hellwig 			*tpp = tp;
1040e546cb79SDave Chinner 			*ipp = NULL;
1041e546cb79SDave Chinner 			return code;
1042e546cb79SDave Chinner 		}
1043e546cb79SDave Chinner 		xfs_trans_bjoin(tp, ialloc_context);
1044e546cb79SDave Chinner 
1045e546cb79SDave Chinner 		/*
1046e546cb79SDave Chinner 		 * Call ialloc again. Since we've locked out all
1047e546cb79SDave Chinner 		 * other allocations in this allocation group,
1048e546cb79SDave Chinner 		 * this call should always succeed.
1049e546cb79SDave Chinner 		 */
1050e546cb79SDave Chinner 		code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid,
1051f59cf5c2SChristoph Hellwig 				  &ialloc_context, &ip);
1052e546cb79SDave Chinner 
1053e546cb79SDave Chinner 		/*
1054e546cb79SDave Chinner 		 * If we get an error at this point, return to the caller
1055e546cb79SDave Chinner 		 * so that the current transaction can be aborted.
1056e546cb79SDave Chinner 		 */
1057e546cb79SDave Chinner 		if (code) {
1058e546cb79SDave Chinner 			*tpp = tp;
1059e546cb79SDave Chinner 			*ipp = NULL;
1060e546cb79SDave Chinner 			return code;
1061e546cb79SDave Chinner 		}
1062e546cb79SDave Chinner 		ASSERT(!ialloc_context && ip);
1063e546cb79SDave Chinner 
1064e546cb79SDave Chinner 	}
1065e546cb79SDave Chinner 
1066e546cb79SDave Chinner 	*ipp = ip;
1067e546cb79SDave Chinner 	*tpp = tp;
1068e546cb79SDave Chinner 
1069e546cb79SDave Chinner 	return 0;
1070e546cb79SDave Chinner }
1071e546cb79SDave Chinner 
1072e546cb79SDave Chinner /*
107354d7b5c1SDave Chinner  * Decrement the link count on an inode & log the change.  If this causes the
107454d7b5c1SDave Chinner  * link count to go to zero, move the inode to AGI unlinked list so that it can
107554d7b5c1SDave Chinner  * be freed when the last active reference goes away via xfs_inactive().
1076e546cb79SDave Chinner  */
10770d5a75e9SEric Sandeen static int			/* error */
1078e546cb79SDave Chinner xfs_droplink(
1079e546cb79SDave Chinner 	xfs_trans_t *tp,
1080e546cb79SDave Chinner 	xfs_inode_t *ip)
1081e546cb79SDave Chinner {
1082e546cb79SDave Chinner 	xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1083e546cb79SDave Chinner 
1084e546cb79SDave Chinner 	drop_nlink(VFS_I(ip));
1085e546cb79SDave Chinner 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1086e546cb79SDave Chinner 
108754d7b5c1SDave Chinner 	if (VFS_I(ip)->i_nlink)
108854d7b5c1SDave Chinner 		return 0;
108954d7b5c1SDave Chinner 
109054d7b5c1SDave Chinner 	return xfs_iunlink(tp, ip);
1091e546cb79SDave Chinner }
1092e546cb79SDave Chinner 
1093e546cb79SDave Chinner /*
1094e546cb79SDave Chinner  * Increment the link count on an inode & log the change.
1095e546cb79SDave Chinner  */
109691083269SEric Sandeen static void
1097e546cb79SDave Chinner xfs_bumplink(
1098e546cb79SDave Chinner 	xfs_trans_t *tp,
1099e546cb79SDave Chinner 	xfs_inode_t *ip)
1100e546cb79SDave Chinner {
1101e546cb79SDave Chinner 	xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1102e546cb79SDave Chinner 
1103e546cb79SDave Chinner 	inc_nlink(VFS_I(ip));
1104e546cb79SDave Chinner 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1105e546cb79SDave Chinner }
1106e546cb79SDave Chinner 
1107c24b5dfaSDave Chinner int
1108c24b5dfaSDave Chinner xfs_create(
1109c24b5dfaSDave Chinner 	xfs_inode_t		*dp,
1110c24b5dfaSDave Chinner 	struct xfs_name		*name,
1111c24b5dfaSDave Chinner 	umode_t			mode,
111266f36464SChristoph Hellwig 	dev_t			rdev,
1113c24b5dfaSDave Chinner 	xfs_inode_t		**ipp)
1114c24b5dfaSDave Chinner {
1115c24b5dfaSDave Chinner 	int			is_dir = S_ISDIR(mode);
1116c24b5dfaSDave Chinner 	struct xfs_mount	*mp = dp->i_mount;
1117c24b5dfaSDave Chinner 	struct xfs_inode	*ip = NULL;
1118c24b5dfaSDave Chinner 	struct xfs_trans	*tp = NULL;
1119c24b5dfaSDave Chinner 	int			error;
1120c24b5dfaSDave Chinner 	bool                    unlock_dp_on_error = false;
1121c24b5dfaSDave Chinner 	prid_t			prid;
1122c24b5dfaSDave Chinner 	struct xfs_dquot	*udqp = NULL;
1123c24b5dfaSDave Chinner 	struct xfs_dquot	*gdqp = NULL;
1124c24b5dfaSDave Chinner 	struct xfs_dquot	*pdqp = NULL;
1125062647a8SBrian Foster 	struct xfs_trans_res	*tres;
1126c24b5dfaSDave Chinner 	uint			resblks;
1127c24b5dfaSDave Chinner 
1128c24b5dfaSDave Chinner 	trace_xfs_create(dp, name);
1129c24b5dfaSDave Chinner 
1130c24b5dfaSDave Chinner 	if (XFS_FORCED_SHUTDOWN(mp))
11312451337dSDave Chinner 		return -EIO;
1132c24b5dfaSDave Chinner 
1133163467d3SZhi Yong Wu 	prid = xfs_get_initial_prid(dp);
1134c24b5dfaSDave Chinner 
1135c24b5dfaSDave Chinner 	/*
1136c24b5dfaSDave Chinner 	 * Make sure that we have allocated dquot(s) on disk.
1137c24b5dfaSDave Chinner 	 */
113854295159SChristoph Hellwig 	error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
1139c24b5dfaSDave Chinner 					XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
1140c24b5dfaSDave Chinner 					&udqp, &gdqp, &pdqp);
1141c24b5dfaSDave Chinner 	if (error)
1142c24b5dfaSDave Chinner 		return error;
1143c24b5dfaSDave Chinner 
1144c24b5dfaSDave Chinner 	if (is_dir) {
1145c24b5dfaSDave Chinner 		resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
1146062647a8SBrian Foster 		tres = &M_RES(mp)->tr_mkdir;
1147c24b5dfaSDave Chinner 	} else {
1148c24b5dfaSDave Chinner 		resblks = XFS_CREATE_SPACE_RES(mp, name->len);
1149062647a8SBrian Foster 		tres = &M_RES(mp)->tr_create;
1150c24b5dfaSDave Chinner 	}
1151c24b5dfaSDave Chinner 
1152c24b5dfaSDave Chinner 	/*
1153c24b5dfaSDave Chinner 	 * Initially assume that the file does not exist and
1154c24b5dfaSDave Chinner 	 * reserve the resources for that case.  If that is not
1155c24b5dfaSDave Chinner 	 * the case we'll drop the one we have and get a more
1156c24b5dfaSDave Chinner 	 * appropriate transaction later.
1157c24b5dfaSDave Chinner 	 */
1158253f4911SChristoph Hellwig 	error = xfs_trans_alloc(mp, tres, resblks, 0, 0, &tp);
11592451337dSDave Chinner 	if (error == -ENOSPC) {
1160c24b5dfaSDave Chinner 		/* flush outstanding delalloc blocks and retry */
1161c24b5dfaSDave Chinner 		xfs_flush_inodes(mp);
1162253f4911SChristoph Hellwig 		error = xfs_trans_alloc(mp, tres, resblks, 0, 0, &tp);
1163c24b5dfaSDave Chinner 	}
11644906e215SChristoph Hellwig 	if (error)
1165253f4911SChristoph Hellwig 		goto out_release_inode;
1166c24b5dfaSDave Chinner 
116765523218SChristoph Hellwig 	xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1168c24b5dfaSDave Chinner 	unlock_dp_on_error = true;
1169c24b5dfaSDave Chinner 
1170c24b5dfaSDave Chinner 	/*
1171c24b5dfaSDave Chinner 	 * Reserve disk quota and the inode.
1172c24b5dfaSDave Chinner 	 */
1173c24b5dfaSDave Chinner 	error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
1174c24b5dfaSDave Chinner 						pdqp, resblks, 1, 0);
1175c24b5dfaSDave Chinner 	if (error)
1176c24b5dfaSDave Chinner 		goto out_trans_cancel;
1177c24b5dfaSDave Chinner 
1178c24b5dfaSDave Chinner 	/*
1179c24b5dfaSDave Chinner 	 * A newly created regular or special file just has one directory
1180c24b5dfaSDave Chinner 	 * entry pointing to them, but a directory also the "." entry
1181c24b5dfaSDave Chinner 	 * pointing to itself.
1182c24b5dfaSDave Chinner 	 */
1183c959025eSChandan Rajendra 	error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev, prid, &ip);
1184d6077aa3SJan Kara 	if (error)
1185c24b5dfaSDave Chinner 		goto out_trans_cancel;
1186c24b5dfaSDave Chinner 
1187c24b5dfaSDave Chinner 	/*
1188c24b5dfaSDave Chinner 	 * Now we join the directory inode to the transaction.  We do not do it
1189c24b5dfaSDave Chinner 	 * earlier because xfs_dir_ialloc might commit the previous transaction
1190c24b5dfaSDave Chinner 	 * (and release all the locks).  An error from here on will result in
1191c24b5dfaSDave Chinner 	 * the transaction cancel unlocking dp so don't do it explicitly in the
1192c24b5dfaSDave Chinner 	 * error path.
1193c24b5dfaSDave Chinner 	 */
119465523218SChristoph Hellwig 	xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1195c24b5dfaSDave Chinner 	unlock_dp_on_error = false;
1196c24b5dfaSDave Chinner 
1197381eee69SBrian Foster 	error = xfs_dir_createname(tp, dp, name, ip->i_ino,
119863337b63SKaixu Xia 					resblks - XFS_IALLOC_SPACE_RES(mp));
1199c24b5dfaSDave Chinner 	if (error) {
12002451337dSDave Chinner 		ASSERT(error != -ENOSPC);
12014906e215SChristoph Hellwig 		goto out_trans_cancel;
1202c24b5dfaSDave Chinner 	}
1203c24b5dfaSDave Chinner 	xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1204c24b5dfaSDave Chinner 	xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1205c24b5dfaSDave Chinner 
1206c24b5dfaSDave Chinner 	if (is_dir) {
1207c24b5dfaSDave Chinner 		error = xfs_dir_init(tp, ip, dp);
1208c24b5dfaSDave Chinner 		if (error)
1209c8eac49eSBrian Foster 			goto out_trans_cancel;
1210c24b5dfaSDave Chinner 
121191083269SEric Sandeen 		xfs_bumplink(tp, dp);
1212c24b5dfaSDave Chinner 	}
1213c24b5dfaSDave Chinner 
1214c24b5dfaSDave Chinner 	/*
1215c24b5dfaSDave Chinner 	 * If this is a synchronous mount, make sure that the
1216c24b5dfaSDave Chinner 	 * create transaction goes to disk before returning to
1217c24b5dfaSDave Chinner 	 * the user.
1218c24b5dfaSDave Chinner 	 */
1219c24b5dfaSDave Chinner 	if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1220c24b5dfaSDave Chinner 		xfs_trans_set_sync(tp);
1221c24b5dfaSDave Chinner 
1222c24b5dfaSDave Chinner 	/*
1223c24b5dfaSDave Chinner 	 * Attach the dquot(s) to the inodes and modify them incore.
1224c24b5dfaSDave Chinner 	 * These ids of the inode couldn't have changed since the new
1225c24b5dfaSDave Chinner 	 * inode has been locked ever since it was created.
1226c24b5dfaSDave Chinner 	 */
1227c24b5dfaSDave Chinner 	xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
1228c24b5dfaSDave Chinner 
122970393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
1230c24b5dfaSDave Chinner 	if (error)
1231c24b5dfaSDave Chinner 		goto out_release_inode;
1232c24b5dfaSDave Chinner 
1233c24b5dfaSDave Chinner 	xfs_qm_dqrele(udqp);
1234c24b5dfaSDave Chinner 	xfs_qm_dqrele(gdqp);
1235c24b5dfaSDave Chinner 	xfs_qm_dqrele(pdqp);
1236c24b5dfaSDave Chinner 
1237c24b5dfaSDave Chinner 	*ipp = ip;
1238c24b5dfaSDave Chinner 	return 0;
1239c24b5dfaSDave Chinner 
1240c24b5dfaSDave Chinner  out_trans_cancel:
12414906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
1242c24b5dfaSDave Chinner  out_release_inode:
1243c24b5dfaSDave Chinner 	/*
124458c90473SDave Chinner 	 * Wait until after the current transaction is aborted to finish the
124558c90473SDave Chinner 	 * setup of the inode and release the inode.  This prevents recursive
124658c90473SDave Chinner 	 * transactions and deadlocks from xfs_inactive.
1247c24b5dfaSDave Chinner 	 */
124858c90473SDave Chinner 	if (ip) {
124958c90473SDave Chinner 		xfs_finish_inode_setup(ip);
125044a8736bSDarrick J. Wong 		xfs_irele(ip);
125158c90473SDave Chinner 	}
1252c24b5dfaSDave Chinner 
1253c24b5dfaSDave Chinner 	xfs_qm_dqrele(udqp);
1254c24b5dfaSDave Chinner 	xfs_qm_dqrele(gdqp);
1255c24b5dfaSDave Chinner 	xfs_qm_dqrele(pdqp);
1256c24b5dfaSDave Chinner 
1257c24b5dfaSDave Chinner 	if (unlock_dp_on_error)
125865523218SChristoph Hellwig 		xfs_iunlock(dp, XFS_ILOCK_EXCL);
1259c24b5dfaSDave Chinner 	return error;
1260c24b5dfaSDave Chinner }
1261c24b5dfaSDave Chinner 
1262c24b5dfaSDave Chinner int
126399b6436bSZhi Yong Wu xfs_create_tmpfile(
126499b6436bSZhi Yong Wu 	struct xfs_inode	*dp,
1265330033d6SBrian Foster 	umode_t			mode,
1266330033d6SBrian Foster 	struct xfs_inode	**ipp)
126799b6436bSZhi Yong Wu {
126899b6436bSZhi Yong Wu 	struct xfs_mount	*mp = dp->i_mount;
126999b6436bSZhi Yong Wu 	struct xfs_inode	*ip = NULL;
127099b6436bSZhi Yong Wu 	struct xfs_trans	*tp = NULL;
127199b6436bSZhi Yong Wu 	int			error;
127299b6436bSZhi Yong Wu 	prid_t                  prid;
127399b6436bSZhi Yong Wu 	struct xfs_dquot	*udqp = NULL;
127499b6436bSZhi Yong Wu 	struct xfs_dquot	*gdqp = NULL;
127599b6436bSZhi Yong Wu 	struct xfs_dquot	*pdqp = NULL;
127699b6436bSZhi Yong Wu 	struct xfs_trans_res	*tres;
127799b6436bSZhi Yong Wu 	uint			resblks;
127899b6436bSZhi Yong Wu 
127999b6436bSZhi Yong Wu 	if (XFS_FORCED_SHUTDOWN(mp))
12802451337dSDave Chinner 		return -EIO;
128199b6436bSZhi Yong Wu 
128299b6436bSZhi Yong Wu 	prid = xfs_get_initial_prid(dp);
128399b6436bSZhi Yong Wu 
128499b6436bSZhi Yong Wu 	/*
128599b6436bSZhi Yong Wu 	 * Make sure that we have allocated dquot(s) on disk.
128699b6436bSZhi Yong Wu 	 */
128754295159SChristoph Hellwig 	error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
128899b6436bSZhi Yong Wu 				XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
128999b6436bSZhi Yong Wu 				&udqp, &gdqp, &pdqp);
129099b6436bSZhi Yong Wu 	if (error)
129199b6436bSZhi Yong Wu 		return error;
129299b6436bSZhi Yong Wu 
129399b6436bSZhi Yong Wu 	resblks = XFS_IALLOC_SPACE_RES(mp);
129499b6436bSZhi Yong Wu 	tres = &M_RES(mp)->tr_create_tmpfile;
1295253f4911SChristoph Hellwig 
1296253f4911SChristoph Hellwig 	error = xfs_trans_alloc(mp, tres, resblks, 0, 0, &tp);
12974906e215SChristoph Hellwig 	if (error)
1298253f4911SChristoph Hellwig 		goto out_release_inode;
129999b6436bSZhi Yong Wu 
130099b6436bSZhi Yong Wu 	error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
130199b6436bSZhi Yong Wu 						pdqp, resblks, 1, 0);
130299b6436bSZhi Yong Wu 	if (error)
130399b6436bSZhi Yong Wu 		goto out_trans_cancel;
130499b6436bSZhi Yong Wu 
1305c4a6bf7fSDarrick J. Wong 	error = xfs_dir_ialloc(&tp, dp, mode, 0, 0, prid, &ip);
1306d6077aa3SJan Kara 	if (error)
130799b6436bSZhi Yong Wu 		goto out_trans_cancel;
130899b6436bSZhi Yong Wu 
130999b6436bSZhi Yong Wu 	if (mp->m_flags & XFS_MOUNT_WSYNC)
131099b6436bSZhi Yong Wu 		xfs_trans_set_sync(tp);
131199b6436bSZhi Yong Wu 
131299b6436bSZhi Yong Wu 	/*
131399b6436bSZhi Yong Wu 	 * Attach the dquot(s) to the inodes and modify them incore.
131499b6436bSZhi Yong Wu 	 * These ids of the inode couldn't have changed since the new
131599b6436bSZhi Yong Wu 	 * inode has been locked ever since it was created.
131699b6436bSZhi Yong Wu 	 */
131799b6436bSZhi Yong Wu 	xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
131899b6436bSZhi Yong Wu 
131999b6436bSZhi Yong Wu 	error = xfs_iunlink(tp, ip);
132099b6436bSZhi Yong Wu 	if (error)
13214906e215SChristoph Hellwig 		goto out_trans_cancel;
132299b6436bSZhi Yong Wu 
132370393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
132499b6436bSZhi Yong Wu 	if (error)
132599b6436bSZhi Yong Wu 		goto out_release_inode;
132699b6436bSZhi Yong Wu 
132799b6436bSZhi Yong Wu 	xfs_qm_dqrele(udqp);
132899b6436bSZhi Yong Wu 	xfs_qm_dqrele(gdqp);
132999b6436bSZhi Yong Wu 	xfs_qm_dqrele(pdqp);
133099b6436bSZhi Yong Wu 
1331330033d6SBrian Foster 	*ipp = ip;
133299b6436bSZhi Yong Wu 	return 0;
133399b6436bSZhi Yong Wu 
133499b6436bSZhi Yong Wu  out_trans_cancel:
13354906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
133699b6436bSZhi Yong Wu  out_release_inode:
133799b6436bSZhi Yong Wu 	/*
133858c90473SDave Chinner 	 * Wait until after the current transaction is aborted to finish the
133958c90473SDave Chinner 	 * setup of the inode and release the inode.  This prevents recursive
134058c90473SDave Chinner 	 * transactions and deadlocks from xfs_inactive.
134199b6436bSZhi Yong Wu 	 */
134258c90473SDave Chinner 	if (ip) {
134358c90473SDave Chinner 		xfs_finish_inode_setup(ip);
134444a8736bSDarrick J. Wong 		xfs_irele(ip);
134558c90473SDave Chinner 	}
134699b6436bSZhi Yong Wu 
134799b6436bSZhi Yong Wu 	xfs_qm_dqrele(udqp);
134899b6436bSZhi Yong Wu 	xfs_qm_dqrele(gdqp);
134999b6436bSZhi Yong Wu 	xfs_qm_dqrele(pdqp);
135099b6436bSZhi Yong Wu 
135199b6436bSZhi Yong Wu 	return error;
135299b6436bSZhi Yong Wu }
135399b6436bSZhi Yong Wu 
135499b6436bSZhi Yong Wu int
1355c24b5dfaSDave Chinner xfs_link(
1356c24b5dfaSDave Chinner 	xfs_inode_t		*tdp,
1357c24b5dfaSDave Chinner 	xfs_inode_t		*sip,
1358c24b5dfaSDave Chinner 	struct xfs_name		*target_name)
1359c24b5dfaSDave Chinner {
1360c24b5dfaSDave Chinner 	xfs_mount_t		*mp = tdp->i_mount;
1361c24b5dfaSDave Chinner 	xfs_trans_t		*tp;
1362c24b5dfaSDave Chinner 	int			error;
1363c24b5dfaSDave Chinner 	int			resblks;
1364c24b5dfaSDave Chinner 
1365c24b5dfaSDave Chinner 	trace_xfs_link(tdp, target_name);
1366c24b5dfaSDave Chinner 
1367c19b3b05SDave Chinner 	ASSERT(!S_ISDIR(VFS_I(sip)->i_mode));
1368c24b5dfaSDave Chinner 
1369c24b5dfaSDave Chinner 	if (XFS_FORCED_SHUTDOWN(mp))
13702451337dSDave Chinner 		return -EIO;
1371c24b5dfaSDave Chinner 
1372c14cfccaSDarrick J. Wong 	error = xfs_qm_dqattach(sip);
1373c24b5dfaSDave Chinner 	if (error)
1374c24b5dfaSDave Chinner 		goto std_return;
1375c24b5dfaSDave Chinner 
1376c14cfccaSDarrick J. Wong 	error = xfs_qm_dqattach(tdp);
1377c24b5dfaSDave Chinner 	if (error)
1378c24b5dfaSDave Chinner 		goto std_return;
1379c24b5dfaSDave Chinner 
1380c24b5dfaSDave Chinner 	resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
1381253f4911SChristoph Hellwig 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_link, resblks, 0, 0, &tp);
13822451337dSDave Chinner 	if (error == -ENOSPC) {
1383c24b5dfaSDave Chinner 		resblks = 0;
1384253f4911SChristoph Hellwig 		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_link, 0, 0, 0, &tp);
1385c24b5dfaSDave Chinner 	}
13864906e215SChristoph Hellwig 	if (error)
1387253f4911SChristoph Hellwig 		goto std_return;
1388c24b5dfaSDave Chinner 
13897c2d238aSDarrick J. Wong 	xfs_lock_two_inodes(sip, XFS_ILOCK_EXCL, tdp, XFS_ILOCK_EXCL);
1390c24b5dfaSDave Chinner 
1391c24b5dfaSDave Chinner 	xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
139265523218SChristoph Hellwig 	xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
1393c24b5dfaSDave Chinner 
1394c24b5dfaSDave Chinner 	/*
1395c24b5dfaSDave Chinner 	 * If we are using project inheritance, we only allow hard link
1396c24b5dfaSDave Chinner 	 * creation in our tree when the project IDs are the same; else
1397c24b5dfaSDave Chinner 	 * the tree quota mechanism could be circumvented.
1398c24b5dfaSDave Chinner 	 */
1399c24b5dfaSDave Chinner 	if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
1400de7a866fSChristoph Hellwig 		     tdp->i_d.di_projid != sip->i_d.di_projid)) {
14012451337dSDave Chinner 		error = -EXDEV;
1402c24b5dfaSDave Chinner 		goto error_return;
1403c24b5dfaSDave Chinner 	}
1404c24b5dfaSDave Chinner 
140594f3cad5SEric Sandeen 	if (!resblks) {
140694f3cad5SEric Sandeen 		error = xfs_dir_canenter(tp, tdp, target_name);
1407c24b5dfaSDave Chinner 		if (error)
1408c24b5dfaSDave Chinner 			goto error_return;
140994f3cad5SEric Sandeen 	}
1410c24b5dfaSDave Chinner 
141154d7b5c1SDave Chinner 	/*
141254d7b5c1SDave Chinner 	 * Handle initial link state of O_TMPFILE inode
141354d7b5c1SDave Chinner 	 */
141454d7b5c1SDave Chinner 	if (VFS_I(sip)->i_nlink == 0) {
1415ab297431SZhi Yong Wu 		error = xfs_iunlink_remove(tp, sip);
1416ab297431SZhi Yong Wu 		if (error)
14174906e215SChristoph Hellwig 			goto error_return;
1418ab297431SZhi Yong Wu 	}
1419ab297431SZhi Yong Wu 
1420c24b5dfaSDave Chinner 	error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
1421381eee69SBrian Foster 				   resblks);
1422c24b5dfaSDave Chinner 	if (error)
14234906e215SChristoph Hellwig 		goto error_return;
1424c24b5dfaSDave Chinner 	xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1425c24b5dfaSDave Chinner 	xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
1426c24b5dfaSDave Chinner 
142791083269SEric Sandeen 	xfs_bumplink(tp, sip);
1428c24b5dfaSDave Chinner 
1429c24b5dfaSDave Chinner 	/*
1430c24b5dfaSDave Chinner 	 * If this is a synchronous mount, make sure that the
1431c24b5dfaSDave Chinner 	 * link transaction goes to disk before returning to
1432c24b5dfaSDave Chinner 	 * the user.
1433c24b5dfaSDave Chinner 	 */
1434f6106efaSEric Sandeen 	if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1435c24b5dfaSDave Chinner 		xfs_trans_set_sync(tp);
1436c24b5dfaSDave Chinner 
143770393313SChristoph Hellwig 	return xfs_trans_commit(tp);
1438c24b5dfaSDave Chinner 
1439c24b5dfaSDave Chinner  error_return:
14404906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
1441c24b5dfaSDave Chinner  std_return:
1442c24b5dfaSDave Chinner 	return error;
1443c24b5dfaSDave Chinner }
1444c24b5dfaSDave Chinner 
1445363e59baSDarrick J. Wong /* Clear the reflink flag and the cowblocks tag if possible. */
1446363e59baSDarrick J. Wong static void
1447363e59baSDarrick J. Wong xfs_itruncate_clear_reflink_flags(
1448363e59baSDarrick J. Wong 	struct xfs_inode	*ip)
1449363e59baSDarrick J. Wong {
1450363e59baSDarrick J. Wong 	struct xfs_ifork	*dfork;
1451363e59baSDarrick J. Wong 	struct xfs_ifork	*cfork;
1452363e59baSDarrick J. Wong 
1453363e59baSDarrick J. Wong 	if (!xfs_is_reflink_inode(ip))
1454363e59baSDarrick J. Wong 		return;
1455363e59baSDarrick J. Wong 	dfork = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1456363e59baSDarrick J. Wong 	cfork = XFS_IFORK_PTR(ip, XFS_COW_FORK);
1457363e59baSDarrick J. Wong 	if (dfork->if_bytes == 0 && cfork->if_bytes == 0)
1458363e59baSDarrick J. Wong 		ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
1459363e59baSDarrick J. Wong 	if (cfork->if_bytes == 0)
1460363e59baSDarrick J. Wong 		xfs_inode_clear_cowblocks_tag(ip);
1461363e59baSDarrick J. Wong }
1462363e59baSDarrick J. Wong 
14631da177e4SLinus Torvalds /*
14648f04c47aSChristoph Hellwig  * Free up the underlying blocks past new_size.  The new size must be smaller
14658f04c47aSChristoph Hellwig  * than the current size.  This routine can be used both for the attribute and
14668f04c47aSChristoph Hellwig  * data fork, and does not modify the inode size, which is left to the caller.
14671da177e4SLinus Torvalds  *
1468f6485057SDavid Chinner  * The transaction passed to this routine must have made a permanent log
1469f6485057SDavid Chinner  * reservation of at least XFS_ITRUNCATE_LOG_RES.  This routine may commit the
1470f6485057SDavid Chinner  * given transaction and start new ones, so make sure everything involved in
1471f6485057SDavid Chinner  * the transaction is tidy before calling here.  Some transaction will be
1472f6485057SDavid Chinner  * returned to the caller to be committed.  The incoming transaction must
1473f6485057SDavid Chinner  * already include the inode, and both inode locks must be held exclusively.
1474f6485057SDavid Chinner  * The inode must also be "held" within the transaction.  On return the inode
1475f6485057SDavid Chinner  * will be "held" within the returned transaction.  This routine does NOT
1476f6485057SDavid Chinner  * require any disk space to be reserved for it within the transaction.
14771da177e4SLinus Torvalds  *
1478f6485057SDavid Chinner  * If we get an error, we must return with the inode locked and linked into the
1479f6485057SDavid Chinner  * current transaction. This keeps things simple for the higher level code,
1480f6485057SDavid Chinner  * because it always knows that the inode is locked and held in the transaction
1481f6485057SDavid Chinner  * that returns to it whether errors occur or not.  We don't mark the inode
1482f6485057SDavid Chinner  * dirty on error so that transactions can be easily aborted if possible.
14831da177e4SLinus Torvalds  */
14841da177e4SLinus Torvalds int
14854e529339SBrian Foster xfs_itruncate_extents_flags(
14868f04c47aSChristoph Hellwig 	struct xfs_trans	**tpp,
14878f04c47aSChristoph Hellwig 	struct xfs_inode	*ip,
14888f04c47aSChristoph Hellwig 	int			whichfork,
148913b86fc3SBrian Foster 	xfs_fsize_t		new_size,
14904e529339SBrian Foster 	int			flags)
14911da177e4SLinus Torvalds {
14928f04c47aSChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
14938f04c47aSChristoph Hellwig 	struct xfs_trans	*tp = *tpp;
14941da177e4SLinus Torvalds 	xfs_fileoff_t		first_unmap_block;
14958f04c47aSChristoph Hellwig 	xfs_filblks_t		unmap_len;
14968f04c47aSChristoph Hellwig 	int			error = 0;
14971da177e4SLinus Torvalds 
14980b56185bSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
14990b56185bSChristoph Hellwig 	ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
15000b56185bSChristoph Hellwig 	       xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1501ce7ae151SChristoph Hellwig 	ASSERT(new_size <= XFS_ISIZE(ip));
15028f04c47aSChristoph Hellwig 	ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
15031da177e4SLinus Torvalds 	ASSERT(ip->i_itemp != NULL);
1504898621d5SChristoph Hellwig 	ASSERT(ip->i_itemp->ili_lock_flags == 0);
15051da177e4SLinus Torvalds 	ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
15061da177e4SLinus Torvalds 
1507673e8e59SChristoph Hellwig 	trace_xfs_itruncate_extents_start(ip, new_size);
1508673e8e59SChristoph Hellwig 
15094e529339SBrian Foster 	flags |= xfs_bmapi_aflag(whichfork);
151013b86fc3SBrian Foster 
15111da177e4SLinus Torvalds 	/*
15121da177e4SLinus Torvalds 	 * Since it is possible for space to become allocated beyond
15131da177e4SLinus Torvalds 	 * the end of the file (in a crash where the space is allocated
15141da177e4SLinus Torvalds 	 * but the inode size is not yet updated), simply remove any
15151da177e4SLinus Torvalds 	 * blocks which show up between the new EOF and the maximum
15164bbb04abSDarrick J. Wong 	 * possible file size.
15174bbb04abSDarrick J. Wong 	 *
15184bbb04abSDarrick J. Wong 	 * We have to free all the blocks to the bmbt maximum offset, even if
15194bbb04abSDarrick J. Wong 	 * the page cache can't scale that far.
15201da177e4SLinus Torvalds 	 */
15218f04c47aSChristoph Hellwig 	first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
15224bbb04abSDarrick J. Wong 	if (first_unmap_block >= XFS_MAX_FILEOFF) {
15234bbb04abSDarrick J. Wong 		WARN_ON_ONCE(first_unmap_block > XFS_MAX_FILEOFF);
15248f04c47aSChristoph Hellwig 		return 0;
15254bbb04abSDarrick J. Wong 	}
15268f04c47aSChristoph Hellwig 
15274bbb04abSDarrick J. Wong 	unmap_len = XFS_MAX_FILEOFF - first_unmap_block + 1;
15284bbb04abSDarrick J. Wong 	while (unmap_len > 0) {
152902dff7bfSBrian Foster 		ASSERT(tp->t_firstblock == NULLFSBLOCK);
15304bbb04abSDarrick J. Wong 		error = __xfs_bunmapi(tp, ip, first_unmap_block, &unmap_len,
15314bbb04abSDarrick J. Wong 				flags, XFS_ITRUNC_MAX_EXTENTS);
15328f04c47aSChristoph Hellwig 		if (error)
1533d5a2e289SBrian Foster 			goto out;
15341da177e4SLinus Torvalds 
15351da177e4SLinus Torvalds 		/*
15361da177e4SLinus Torvalds 		 * Duplicate the transaction that has the permanent
15371da177e4SLinus Torvalds 		 * reservation and commit the old transaction.
15381da177e4SLinus Torvalds 		 */
15399e28a242SBrian Foster 		error = xfs_defer_finish(&tp);
15408f04c47aSChristoph Hellwig 		if (error)
15419b1f4e98SBrian Foster 			goto out;
15421da177e4SLinus Torvalds 
1543411350dfSChristoph Hellwig 		error = xfs_trans_roll_inode(&tp, ip);
15441da177e4SLinus Torvalds 		if (error)
15458f04c47aSChristoph Hellwig 			goto out;
15461da177e4SLinus Torvalds 	}
15478f04c47aSChristoph Hellwig 
15484919d42aSDarrick J. Wong 	if (whichfork == XFS_DATA_FORK) {
1549aa8968f2SDarrick J. Wong 		/* Remove all pending CoW reservations. */
15504919d42aSDarrick J. Wong 		error = xfs_reflink_cancel_cow_blocks(ip, &tp,
15514bbb04abSDarrick J. Wong 				first_unmap_block, XFS_MAX_FILEOFF, true);
1552aa8968f2SDarrick J. Wong 		if (error)
1553aa8968f2SDarrick J. Wong 			goto out;
1554aa8968f2SDarrick J. Wong 
1555363e59baSDarrick J. Wong 		xfs_itruncate_clear_reflink_flags(ip);
15564919d42aSDarrick J. Wong 	}
1557aa8968f2SDarrick J. Wong 
1558673e8e59SChristoph Hellwig 	/*
1559673e8e59SChristoph Hellwig 	 * Always re-log the inode so that our permanent transaction can keep
1560673e8e59SChristoph Hellwig 	 * on rolling it forward in the log.
1561673e8e59SChristoph Hellwig 	 */
1562673e8e59SChristoph Hellwig 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1563673e8e59SChristoph Hellwig 
1564673e8e59SChristoph Hellwig 	trace_xfs_itruncate_extents_end(ip, new_size);
1565673e8e59SChristoph Hellwig 
15668f04c47aSChristoph Hellwig out:
15678f04c47aSChristoph Hellwig 	*tpp = tp;
15688f04c47aSChristoph Hellwig 	return error;
15698f04c47aSChristoph Hellwig }
15708f04c47aSChristoph Hellwig 
1571c24b5dfaSDave Chinner int
1572c24b5dfaSDave Chinner xfs_release(
1573c24b5dfaSDave Chinner 	xfs_inode_t	*ip)
1574c24b5dfaSDave Chinner {
1575c24b5dfaSDave Chinner 	xfs_mount_t	*mp = ip->i_mount;
1576c24b5dfaSDave Chinner 	int		error;
1577c24b5dfaSDave Chinner 
1578c19b3b05SDave Chinner 	if (!S_ISREG(VFS_I(ip)->i_mode) || (VFS_I(ip)->i_mode == 0))
1579c24b5dfaSDave Chinner 		return 0;
1580c24b5dfaSDave Chinner 
1581c24b5dfaSDave Chinner 	/* If this is a read-only mount, don't do this (would generate I/O) */
1582c24b5dfaSDave Chinner 	if (mp->m_flags & XFS_MOUNT_RDONLY)
1583c24b5dfaSDave Chinner 		return 0;
1584c24b5dfaSDave Chinner 
1585c24b5dfaSDave Chinner 	if (!XFS_FORCED_SHUTDOWN(mp)) {
1586c24b5dfaSDave Chinner 		int truncated;
1587c24b5dfaSDave Chinner 
1588c24b5dfaSDave Chinner 		/*
1589c24b5dfaSDave Chinner 		 * If we previously truncated this file and removed old data
1590c24b5dfaSDave Chinner 		 * in the process, we want to initiate "early" writeout on
1591c24b5dfaSDave Chinner 		 * the last close.  This is an attempt to combat the notorious
1592c24b5dfaSDave Chinner 		 * NULL files problem which is particularly noticeable from a
1593c24b5dfaSDave Chinner 		 * truncate down, buffered (re-)write (delalloc), followed by
1594c24b5dfaSDave Chinner 		 * a crash.  What we are effectively doing here is
1595c24b5dfaSDave Chinner 		 * significantly reducing the time window where we'd otherwise
1596c24b5dfaSDave Chinner 		 * be exposed to that problem.
1597c24b5dfaSDave Chinner 		 */
1598c24b5dfaSDave Chinner 		truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
1599c24b5dfaSDave Chinner 		if (truncated) {
1600c24b5dfaSDave Chinner 			xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
1601eac152b4SDave Chinner 			if (ip->i_delayed_blks > 0) {
16022451337dSDave Chinner 				error = filemap_flush(VFS_I(ip)->i_mapping);
1603c24b5dfaSDave Chinner 				if (error)
1604c24b5dfaSDave Chinner 					return error;
1605c24b5dfaSDave Chinner 			}
1606c24b5dfaSDave Chinner 		}
1607c24b5dfaSDave Chinner 	}
1608c24b5dfaSDave Chinner 
160954d7b5c1SDave Chinner 	if (VFS_I(ip)->i_nlink == 0)
1610c24b5dfaSDave Chinner 		return 0;
1611c24b5dfaSDave Chinner 
1612c24b5dfaSDave Chinner 	if (xfs_can_free_eofblocks(ip, false)) {
1613c24b5dfaSDave Chinner 
1614c24b5dfaSDave Chinner 		/*
1615a36b9261SBrian Foster 		 * Check if the inode is being opened, written and closed
1616a36b9261SBrian Foster 		 * frequently and we have delayed allocation blocks outstanding
1617a36b9261SBrian Foster 		 * (e.g. streaming writes from the NFS server), truncating the
1618a36b9261SBrian Foster 		 * blocks past EOF will cause fragmentation to occur.
1619a36b9261SBrian Foster 		 *
1620a36b9261SBrian Foster 		 * In this case don't do the truncation, but we have to be
1621a36b9261SBrian Foster 		 * careful how we detect this case. Blocks beyond EOF show up as
1622a36b9261SBrian Foster 		 * i_delayed_blks even when the inode is clean, so we need to
1623a36b9261SBrian Foster 		 * truncate them away first before checking for a dirty release.
1624a36b9261SBrian Foster 		 * Hence on the first dirty close we will still remove the
1625a36b9261SBrian Foster 		 * speculative allocation, but after that we will leave it in
1626a36b9261SBrian Foster 		 * place.
1627a36b9261SBrian Foster 		 */
1628a36b9261SBrian Foster 		if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
1629a36b9261SBrian Foster 			return 0;
1630a36b9261SBrian Foster 		/*
1631c24b5dfaSDave Chinner 		 * If we can't get the iolock just skip truncating the blocks
1632c1e8d7c6SMichel Lespinasse 		 * past EOF because we could deadlock with the mmap_lock
1633c24b5dfaSDave Chinner 		 * otherwise. We'll get another chance to drop them once the
1634c24b5dfaSDave Chinner 		 * last reference to the inode is dropped, so we'll never leak
1635c24b5dfaSDave Chinner 		 * blocks permanently.
1636c24b5dfaSDave Chinner 		 */
1637a36b9261SBrian Foster 		if (xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
1638a36b9261SBrian Foster 			error = xfs_free_eofblocks(ip);
1639a36b9261SBrian Foster 			xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1640a36b9261SBrian Foster 			if (error)
1641c24b5dfaSDave Chinner 				return error;
1642a36b9261SBrian Foster 		}
1643c24b5dfaSDave Chinner 
1644c24b5dfaSDave Chinner 		/* delalloc blocks after truncation means it really is dirty */
1645c24b5dfaSDave Chinner 		if (ip->i_delayed_blks)
1646c24b5dfaSDave Chinner 			xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
1647c24b5dfaSDave Chinner 	}
1648c24b5dfaSDave Chinner 	return 0;
1649c24b5dfaSDave Chinner }
1650c24b5dfaSDave Chinner 
1651c24b5dfaSDave Chinner /*
1652f7be2d7fSBrian Foster  * xfs_inactive_truncate
1653f7be2d7fSBrian Foster  *
1654f7be2d7fSBrian Foster  * Called to perform a truncate when an inode becomes unlinked.
1655f7be2d7fSBrian Foster  */
1656f7be2d7fSBrian Foster STATIC int
1657f7be2d7fSBrian Foster xfs_inactive_truncate(
1658f7be2d7fSBrian Foster 	struct xfs_inode *ip)
1659f7be2d7fSBrian Foster {
1660f7be2d7fSBrian Foster 	struct xfs_mount	*mp = ip->i_mount;
1661f7be2d7fSBrian Foster 	struct xfs_trans	*tp;
1662f7be2d7fSBrian Foster 	int			error;
1663f7be2d7fSBrian Foster 
1664253f4911SChristoph Hellwig 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
1665f7be2d7fSBrian Foster 	if (error) {
1666f7be2d7fSBrian Foster 		ASSERT(XFS_FORCED_SHUTDOWN(mp));
1667f7be2d7fSBrian Foster 		return error;
1668f7be2d7fSBrian Foster 	}
1669f7be2d7fSBrian Foster 	xfs_ilock(ip, XFS_ILOCK_EXCL);
1670f7be2d7fSBrian Foster 	xfs_trans_ijoin(tp, ip, 0);
1671f7be2d7fSBrian Foster 
1672f7be2d7fSBrian Foster 	/*
1673f7be2d7fSBrian Foster 	 * Log the inode size first to prevent stale data exposure in the event
1674f7be2d7fSBrian Foster 	 * of a system crash before the truncate completes. See the related
167569bca807SJan Kara 	 * comment in xfs_vn_setattr_size() for details.
1676f7be2d7fSBrian Foster 	 */
1677f7be2d7fSBrian Foster 	ip->i_d.di_size = 0;
1678f7be2d7fSBrian Foster 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1679f7be2d7fSBrian Foster 
1680f7be2d7fSBrian Foster 	error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
1681f7be2d7fSBrian Foster 	if (error)
1682f7be2d7fSBrian Foster 		goto error_trans_cancel;
1683f7be2d7fSBrian Foster 
1684daf83964SChristoph Hellwig 	ASSERT(ip->i_df.if_nextents == 0);
1685f7be2d7fSBrian Foster 
168670393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
1687f7be2d7fSBrian Foster 	if (error)
1688f7be2d7fSBrian Foster 		goto error_unlock;
1689f7be2d7fSBrian Foster 
1690f7be2d7fSBrian Foster 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
1691f7be2d7fSBrian Foster 	return 0;
1692f7be2d7fSBrian Foster 
1693f7be2d7fSBrian Foster error_trans_cancel:
16944906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
1695f7be2d7fSBrian Foster error_unlock:
1696f7be2d7fSBrian Foster 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
1697f7be2d7fSBrian Foster 	return error;
1698f7be2d7fSBrian Foster }
1699f7be2d7fSBrian Foster 
1700f7be2d7fSBrian Foster /*
170188877d2bSBrian Foster  * xfs_inactive_ifree()
170288877d2bSBrian Foster  *
170388877d2bSBrian Foster  * Perform the inode free when an inode is unlinked.
170488877d2bSBrian Foster  */
170588877d2bSBrian Foster STATIC int
170688877d2bSBrian Foster xfs_inactive_ifree(
170788877d2bSBrian Foster 	struct xfs_inode *ip)
170888877d2bSBrian Foster {
170988877d2bSBrian Foster 	struct xfs_mount	*mp = ip->i_mount;
171088877d2bSBrian Foster 	struct xfs_trans	*tp;
171188877d2bSBrian Foster 	int			error;
171288877d2bSBrian Foster 
17139d43b180SBrian Foster 	/*
171476d771b4SChristoph Hellwig 	 * We try to use a per-AG reservation for any block needed by the finobt
171576d771b4SChristoph Hellwig 	 * tree, but as the finobt feature predates the per-AG reservation
171676d771b4SChristoph Hellwig 	 * support a degraded file system might not have enough space for the
171776d771b4SChristoph Hellwig 	 * reservation at mount time.  In that case try to dip into the reserved
171876d771b4SChristoph Hellwig 	 * pool and pray.
17199d43b180SBrian Foster 	 *
17209d43b180SBrian Foster 	 * Send a warning if the reservation does happen to fail, as the inode
17219d43b180SBrian Foster 	 * now remains allocated and sits on the unlinked list until the fs is
17229d43b180SBrian Foster 	 * repaired.
17239d43b180SBrian Foster 	 */
1724e1f6ca11SDarrick J. Wong 	if (unlikely(mp->m_finobt_nores)) {
1725253f4911SChristoph Hellwig 		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ifree,
172676d771b4SChristoph Hellwig 				XFS_IFREE_SPACE_RES(mp), 0, XFS_TRANS_RESERVE,
172776d771b4SChristoph Hellwig 				&tp);
172876d771b4SChristoph Hellwig 	} else {
172976d771b4SChristoph Hellwig 		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ifree, 0, 0, 0, &tp);
173076d771b4SChristoph Hellwig 	}
173188877d2bSBrian Foster 	if (error) {
17322451337dSDave Chinner 		if (error == -ENOSPC) {
17339d43b180SBrian Foster 			xfs_warn_ratelimited(mp,
17349d43b180SBrian Foster 			"Failed to remove inode(s) from unlinked list. "
17359d43b180SBrian Foster 			"Please free space, unmount and run xfs_repair.");
17369d43b180SBrian Foster 		} else {
173788877d2bSBrian Foster 			ASSERT(XFS_FORCED_SHUTDOWN(mp));
17389d43b180SBrian Foster 		}
173988877d2bSBrian Foster 		return error;
174088877d2bSBrian Foster 	}
174188877d2bSBrian Foster 
174296355d5aSDave Chinner 	/*
174396355d5aSDave Chinner 	 * We do not hold the inode locked across the entire rolling transaction
174496355d5aSDave Chinner 	 * here. We only need to hold it for the first transaction that
174596355d5aSDave Chinner 	 * xfs_ifree() builds, which may mark the inode XFS_ISTALE if the
174696355d5aSDave Chinner 	 * underlying cluster buffer is freed. Relogging an XFS_ISTALE inode
174796355d5aSDave Chinner 	 * here breaks the relationship between cluster buffer invalidation and
174896355d5aSDave Chinner 	 * stale inode invalidation on cluster buffer item journal commit
174996355d5aSDave Chinner 	 * completion, and can result in leaving dirty stale inodes hanging
175096355d5aSDave Chinner 	 * around in memory.
175196355d5aSDave Chinner 	 *
175296355d5aSDave Chinner 	 * We have no need for serialising this inode operation against other
175396355d5aSDave Chinner 	 * operations - we freed the inode and hence reallocation is required
175496355d5aSDave Chinner 	 * and that will serialise on reallocating the space the deferops need
175596355d5aSDave Chinner 	 * to free. Hence we can unlock the inode on the first commit of
175696355d5aSDave Chinner 	 * the transaction rather than roll it right through the deferops. This
175796355d5aSDave Chinner 	 * avoids relogging the XFS_ISTALE inode.
175896355d5aSDave Chinner 	 *
175996355d5aSDave Chinner 	 * We check that xfs_ifree() hasn't grown an internal transaction roll
176096355d5aSDave Chinner 	 * by asserting that the inode is still locked when it returns.
176196355d5aSDave Chinner 	 */
176288877d2bSBrian Foster 	xfs_ilock(ip, XFS_ILOCK_EXCL);
176396355d5aSDave Chinner 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
176488877d2bSBrian Foster 
17650e0417f3SBrian Foster 	error = xfs_ifree(tp, ip);
176696355d5aSDave Chinner 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
176788877d2bSBrian Foster 	if (error) {
176888877d2bSBrian Foster 		/*
176988877d2bSBrian Foster 		 * If we fail to free the inode, shut down.  The cancel
177088877d2bSBrian Foster 		 * might do that, we need to make sure.  Otherwise the
177188877d2bSBrian Foster 		 * inode might be lost for a long time or forever.
177288877d2bSBrian Foster 		 */
177388877d2bSBrian Foster 		if (!XFS_FORCED_SHUTDOWN(mp)) {
177488877d2bSBrian Foster 			xfs_notice(mp, "%s: xfs_ifree returned error %d",
177588877d2bSBrian Foster 				__func__, error);
177688877d2bSBrian Foster 			xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
177788877d2bSBrian Foster 		}
17784906e215SChristoph Hellwig 		xfs_trans_cancel(tp);
177988877d2bSBrian Foster 		return error;
178088877d2bSBrian Foster 	}
178188877d2bSBrian Foster 
178288877d2bSBrian Foster 	/*
178388877d2bSBrian Foster 	 * Credit the quota account(s). The inode is gone.
178488877d2bSBrian Foster 	 */
178588877d2bSBrian Foster 	xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
178688877d2bSBrian Foster 
178788877d2bSBrian Foster 	/*
1788d4a97a04SBrian Foster 	 * Just ignore errors at this point.  There is nothing we can do except
1789d4a97a04SBrian Foster 	 * to try to keep going. Make sure it's not a silent error.
179088877d2bSBrian Foster 	 */
179170393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
179288877d2bSBrian Foster 	if (error)
179388877d2bSBrian Foster 		xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
179488877d2bSBrian Foster 			__func__, error);
179588877d2bSBrian Foster 
179688877d2bSBrian Foster 	return 0;
179788877d2bSBrian Foster }
179888877d2bSBrian Foster 
179988877d2bSBrian Foster /*
1800c24b5dfaSDave Chinner  * xfs_inactive
1801c24b5dfaSDave Chinner  *
1802c24b5dfaSDave Chinner  * This is called when the vnode reference count for the vnode
1803c24b5dfaSDave Chinner  * goes to zero.  If the file has been unlinked, then it must
1804c24b5dfaSDave Chinner  * now be truncated.  Also, we clear all of the read-ahead state
1805c24b5dfaSDave Chinner  * kept for the inode here since the file is now closed.
1806c24b5dfaSDave Chinner  */
180774564fb4SBrian Foster void
1808c24b5dfaSDave Chinner xfs_inactive(
1809c24b5dfaSDave Chinner 	xfs_inode_t	*ip)
1810c24b5dfaSDave Chinner {
18113d3c8b52SJie Liu 	struct xfs_mount	*mp;
1812c24b5dfaSDave Chinner 	int			error;
1813c24b5dfaSDave Chinner 	int			truncate = 0;
1814c24b5dfaSDave Chinner 
1815c24b5dfaSDave Chinner 	/*
1816c24b5dfaSDave Chinner 	 * If the inode is already free, then there can be nothing
1817c24b5dfaSDave Chinner 	 * to clean up here.
1818c24b5dfaSDave Chinner 	 */
1819c19b3b05SDave Chinner 	if (VFS_I(ip)->i_mode == 0) {
1820c24b5dfaSDave Chinner 		ASSERT(ip->i_df.if_broot_bytes == 0);
182174564fb4SBrian Foster 		return;
1822c24b5dfaSDave Chinner 	}
1823c24b5dfaSDave Chinner 
1824c24b5dfaSDave Chinner 	mp = ip->i_mount;
182517c12bcdSDarrick J. Wong 	ASSERT(!xfs_iflags_test(ip, XFS_IRECOVERY));
1826c24b5dfaSDave Chinner 
1827c24b5dfaSDave Chinner 	/* If this is a read-only mount, don't do this (would generate I/O) */
1828c24b5dfaSDave Chinner 	if (mp->m_flags & XFS_MOUNT_RDONLY)
182974564fb4SBrian Foster 		return;
1830c24b5dfaSDave Chinner 
18316231848cSDarrick J. Wong 	/* Try to clean out the cow blocks if there are any. */
183251d62690SChristoph Hellwig 	if (xfs_inode_has_cow_data(ip))
18336231848cSDarrick J. Wong 		xfs_reflink_cancel_cow_range(ip, 0, NULLFILEOFF, true);
18346231848cSDarrick J. Wong 
183554d7b5c1SDave Chinner 	if (VFS_I(ip)->i_nlink != 0) {
1836c24b5dfaSDave Chinner 		/*
1837c24b5dfaSDave Chinner 		 * force is true because we are evicting an inode from the
1838c24b5dfaSDave Chinner 		 * cache. Post-eof blocks must be freed, lest we end up with
1839c24b5dfaSDave Chinner 		 * broken free space accounting.
18403b4683c2SBrian Foster 		 *
18413b4683c2SBrian Foster 		 * Note: don't bother with iolock here since lockdep complains
18423b4683c2SBrian Foster 		 * about acquiring it in reclaim context. We have the only
18433b4683c2SBrian Foster 		 * reference to the inode at this point anyways.
1844c24b5dfaSDave Chinner 		 */
18453b4683c2SBrian Foster 		if (xfs_can_free_eofblocks(ip, true))
1846a36b9261SBrian Foster 			xfs_free_eofblocks(ip);
184774564fb4SBrian Foster 
184874564fb4SBrian Foster 		return;
1849c24b5dfaSDave Chinner 	}
1850c24b5dfaSDave Chinner 
1851c19b3b05SDave Chinner 	if (S_ISREG(VFS_I(ip)->i_mode) &&
1852c24b5dfaSDave Chinner 	    (ip->i_d.di_size != 0 || XFS_ISIZE(ip) != 0 ||
1853daf83964SChristoph Hellwig 	     ip->i_df.if_nextents > 0 || ip->i_delayed_blks > 0))
1854c24b5dfaSDave Chinner 		truncate = 1;
1855c24b5dfaSDave Chinner 
1856c14cfccaSDarrick J. Wong 	error = xfs_qm_dqattach(ip);
1857c24b5dfaSDave Chinner 	if (error)
185874564fb4SBrian Foster 		return;
1859c24b5dfaSDave Chinner 
1860c19b3b05SDave Chinner 	if (S_ISLNK(VFS_I(ip)->i_mode))
186136b21ddeSBrian Foster 		error = xfs_inactive_symlink(ip);
1862f7be2d7fSBrian Foster 	else if (truncate)
1863f7be2d7fSBrian Foster 		error = xfs_inactive_truncate(ip);
186436b21ddeSBrian Foster 	if (error)
186574564fb4SBrian Foster 		return;
1866c24b5dfaSDave Chinner 
1867c24b5dfaSDave Chinner 	/*
1868c24b5dfaSDave Chinner 	 * If there are attributes associated with the file then blow them away
1869c24b5dfaSDave Chinner 	 * now.  The code calls a routine that recursively deconstructs the
18706dfe5a04SDave Chinner 	 * attribute fork. If also blows away the in-core attribute fork.
1871c24b5dfaSDave Chinner 	 */
18726dfe5a04SDave Chinner 	if (XFS_IFORK_Q(ip)) {
1873c24b5dfaSDave Chinner 		error = xfs_attr_inactive(ip);
1874c24b5dfaSDave Chinner 		if (error)
187574564fb4SBrian Foster 			return;
1876c24b5dfaSDave Chinner 	}
1877c24b5dfaSDave Chinner 
18786dfe5a04SDave Chinner 	ASSERT(!ip->i_afp);
18796dfe5a04SDave Chinner 	ASSERT(ip->i_d.di_forkoff == 0);
1880c24b5dfaSDave Chinner 
1881c24b5dfaSDave Chinner 	/*
1882c24b5dfaSDave Chinner 	 * Free the inode.
1883c24b5dfaSDave Chinner 	 */
188488877d2bSBrian Foster 	error = xfs_inactive_ifree(ip);
1885c24b5dfaSDave Chinner 	if (error)
188674564fb4SBrian Foster 		return;
1887c24b5dfaSDave Chinner 
1888c24b5dfaSDave Chinner 	/*
1889c24b5dfaSDave Chinner 	 * Release the dquots held by inode, if any.
1890c24b5dfaSDave Chinner 	 */
1891c24b5dfaSDave Chinner 	xfs_qm_dqdetach(ip);
1892c24b5dfaSDave Chinner }
1893c24b5dfaSDave Chinner 
18941da177e4SLinus Torvalds /*
18959b247179SDarrick J. Wong  * In-Core Unlinked List Lookups
18969b247179SDarrick J. Wong  * =============================
18979b247179SDarrick J. Wong  *
18989b247179SDarrick J. Wong  * Every inode is supposed to be reachable from some other piece of metadata
18999b247179SDarrick J. Wong  * with the exception of the root directory.  Inodes with a connection to a
19009b247179SDarrick J. Wong  * file descriptor but not linked from anywhere in the on-disk directory tree
19019b247179SDarrick J. Wong  * are collectively known as unlinked inodes, though the filesystem itself
19029b247179SDarrick J. Wong  * maintains links to these inodes so that on-disk metadata are consistent.
19039b247179SDarrick J. Wong  *
19049b247179SDarrick J. Wong  * XFS implements a per-AG on-disk hash table of unlinked inodes.  The AGI
19059b247179SDarrick J. Wong  * header contains a number of buckets that point to an inode, and each inode
19069b247179SDarrick J. Wong  * record has a pointer to the next inode in the hash chain.  This
19079b247179SDarrick J. Wong  * singly-linked list causes scaling problems in the iunlink remove function
19089b247179SDarrick J. Wong  * because we must walk that list to find the inode that points to the inode
19099b247179SDarrick J. Wong  * being removed from the unlinked hash bucket list.
19109b247179SDarrick J. Wong  *
19119b247179SDarrick J. Wong  * What if we modelled the unlinked list as a collection of records capturing
19129b247179SDarrick J. Wong  * "X.next_unlinked = Y" relations?  If we indexed those records on Y, we'd
19139b247179SDarrick J. Wong  * have a fast way to look up unlinked list predecessors, which avoids the
19149b247179SDarrick J. Wong  * slow list walk.  That's exactly what we do here (in-core) with a per-AG
19159b247179SDarrick J. Wong  * rhashtable.
19169b247179SDarrick J. Wong  *
19179b247179SDarrick J. Wong  * Because this is a backref cache, we ignore operational failures since the
19189b247179SDarrick J. Wong  * iunlink code can fall back to the slow bucket walk.  The only errors that
19199b247179SDarrick J. Wong  * should bubble out are for obviously incorrect situations.
19209b247179SDarrick J. Wong  *
19219b247179SDarrick J. Wong  * All users of the backref cache MUST hold the AGI buffer lock to serialize
19229b247179SDarrick J. Wong  * access or have otherwise provided for concurrency control.
19239b247179SDarrick J. Wong  */
19249b247179SDarrick J. Wong 
19259b247179SDarrick J. Wong /* Capture a "X.next_unlinked = Y" relationship. */
19269b247179SDarrick J. Wong struct xfs_iunlink {
19279b247179SDarrick J. Wong 	struct rhash_head	iu_rhash_head;
19289b247179SDarrick J. Wong 	xfs_agino_t		iu_agino;		/* X */
19299b247179SDarrick J. Wong 	xfs_agino_t		iu_next_unlinked;	/* Y */
19309b247179SDarrick J. Wong };
19319b247179SDarrick J. Wong 
19329b247179SDarrick J. Wong /* Unlinked list predecessor lookup hashtable construction */
19339b247179SDarrick J. Wong static int
19349b247179SDarrick J. Wong xfs_iunlink_obj_cmpfn(
19359b247179SDarrick J. Wong 	struct rhashtable_compare_arg	*arg,
19369b247179SDarrick J. Wong 	const void			*obj)
19379b247179SDarrick J. Wong {
19389b247179SDarrick J. Wong 	const xfs_agino_t		*key = arg->key;
19399b247179SDarrick J. Wong 	const struct xfs_iunlink	*iu = obj;
19409b247179SDarrick J. Wong 
19419b247179SDarrick J. Wong 	if (iu->iu_next_unlinked != *key)
19429b247179SDarrick J. Wong 		return 1;
19439b247179SDarrick J. Wong 	return 0;
19449b247179SDarrick J. Wong }
19459b247179SDarrick J. Wong 
19469b247179SDarrick J. Wong static const struct rhashtable_params xfs_iunlink_hash_params = {
19479b247179SDarrick J. Wong 	.min_size		= XFS_AGI_UNLINKED_BUCKETS,
19489b247179SDarrick J. Wong 	.key_len		= sizeof(xfs_agino_t),
19499b247179SDarrick J. Wong 	.key_offset		= offsetof(struct xfs_iunlink,
19509b247179SDarrick J. Wong 					   iu_next_unlinked),
19519b247179SDarrick J. Wong 	.head_offset		= offsetof(struct xfs_iunlink, iu_rhash_head),
19529b247179SDarrick J. Wong 	.automatic_shrinking	= true,
19539b247179SDarrick J. Wong 	.obj_cmpfn		= xfs_iunlink_obj_cmpfn,
19549b247179SDarrick J. Wong };
19559b247179SDarrick J. Wong 
19569b247179SDarrick J. Wong /*
19579b247179SDarrick J. Wong  * Return X, where X.next_unlinked == @agino.  Returns NULLAGINO if no such
19589b247179SDarrick J. Wong  * relation is found.
19599b247179SDarrick J. Wong  */
19609b247179SDarrick J. Wong static xfs_agino_t
19619b247179SDarrick J. Wong xfs_iunlink_lookup_backref(
19629b247179SDarrick J. Wong 	struct xfs_perag	*pag,
19639b247179SDarrick J. Wong 	xfs_agino_t		agino)
19649b247179SDarrick J. Wong {
19659b247179SDarrick J. Wong 	struct xfs_iunlink	*iu;
19669b247179SDarrick J. Wong 
19679b247179SDarrick J. Wong 	iu = rhashtable_lookup_fast(&pag->pagi_unlinked_hash, &agino,
19689b247179SDarrick J. Wong 			xfs_iunlink_hash_params);
19699b247179SDarrick J. Wong 	return iu ? iu->iu_agino : NULLAGINO;
19709b247179SDarrick J. Wong }
19719b247179SDarrick J. Wong 
19729b247179SDarrick J. Wong /*
19739b247179SDarrick J. Wong  * Take ownership of an iunlink cache entry and insert it into the hash table.
19749b247179SDarrick J. Wong  * If successful, the entry will be owned by the cache; if not, it is freed.
19759b247179SDarrick J. Wong  * Either way, the caller does not own @iu after this call.
19769b247179SDarrick J. Wong  */
19779b247179SDarrick J. Wong static int
19789b247179SDarrick J. Wong xfs_iunlink_insert_backref(
19799b247179SDarrick J. Wong 	struct xfs_perag	*pag,
19809b247179SDarrick J. Wong 	struct xfs_iunlink	*iu)
19819b247179SDarrick J. Wong {
19829b247179SDarrick J. Wong 	int			error;
19839b247179SDarrick J. Wong 
19849b247179SDarrick J. Wong 	error = rhashtable_insert_fast(&pag->pagi_unlinked_hash,
19859b247179SDarrick J. Wong 			&iu->iu_rhash_head, xfs_iunlink_hash_params);
19869b247179SDarrick J. Wong 	/*
19879b247179SDarrick J. Wong 	 * Fail loudly if there already was an entry because that's a sign of
19889b247179SDarrick J. Wong 	 * corruption of in-memory data.  Also fail loudly if we see an error
19899b247179SDarrick J. Wong 	 * code we didn't anticipate from the rhashtable code.  Currently we
19909b247179SDarrick J. Wong 	 * only anticipate ENOMEM.
19919b247179SDarrick J. Wong 	 */
19929b247179SDarrick J. Wong 	if (error) {
19939b247179SDarrick J. Wong 		WARN(error != -ENOMEM, "iunlink cache insert error %d", error);
19949b247179SDarrick J. Wong 		kmem_free(iu);
19959b247179SDarrick J. Wong 	}
19969b247179SDarrick J. Wong 	/*
19979b247179SDarrick J. Wong 	 * Absorb any runtime errors that aren't a result of corruption because
19989b247179SDarrick J. Wong 	 * this is a cache and we can always fall back to bucket list scanning.
19999b247179SDarrick J. Wong 	 */
20009b247179SDarrick J. Wong 	if (error != 0 && error != -EEXIST)
20019b247179SDarrick J. Wong 		error = 0;
20029b247179SDarrick J. Wong 	return error;
20039b247179SDarrick J. Wong }
20049b247179SDarrick J. Wong 
20059b247179SDarrick J. Wong /* Remember that @prev_agino.next_unlinked = @this_agino. */
20069b247179SDarrick J. Wong static int
20079b247179SDarrick J. Wong xfs_iunlink_add_backref(
20089b247179SDarrick J. Wong 	struct xfs_perag	*pag,
20099b247179SDarrick J. Wong 	xfs_agino_t		prev_agino,
20109b247179SDarrick J. Wong 	xfs_agino_t		this_agino)
20119b247179SDarrick J. Wong {
20129b247179SDarrick J. Wong 	struct xfs_iunlink	*iu;
20139b247179SDarrick J. Wong 
20149b247179SDarrick J. Wong 	if (XFS_TEST_ERROR(false, pag->pag_mount, XFS_ERRTAG_IUNLINK_FALLBACK))
20159b247179SDarrick J. Wong 		return 0;
20169b247179SDarrick J. Wong 
2017707e0ddaSTetsuo Handa 	iu = kmem_zalloc(sizeof(*iu), KM_NOFS);
20189b247179SDarrick J. Wong 	iu->iu_agino = prev_agino;
20199b247179SDarrick J. Wong 	iu->iu_next_unlinked = this_agino;
20209b247179SDarrick J. Wong 
20219b247179SDarrick J. Wong 	return xfs_iunlink_insert_backref(pag, iu);
20229b247179SDarrick J. Wong }
20239b247179SDarrick J. Wong 
20249b247179SDarrick J. Wong /*
20259b247179SDarrick J. Wong  * Replace X.next_unlinked = @agino with X.next_unlinked = @next_unlinked.
20269b247179SDarrick J. Wong  * If @next_unlinked is NULLAGINO, we drop the backref and exit.  If there
20279b247179SDarrick J. Wong  * wasn't any such entry then we don't bother.
20289b247179SDarrick J. Wong  */
20299b247179SDarrick J. Wong static int
20309b247179SDarrick J. Wong xfs_iunlink_change_backref(
20319b247179SDarrick J. Wong 	struct xfs_perag	*pag,
20329b247179SDarrick J. Wong 	xfs_agino_t		agino,
20339b247179SDarrick J. Wong 	xfs_agino_t		next_unlinked)
20349b247179SDarrick J. Wong {
20359b247179SDarrick J. Wong 	struct xfs_iunlink	*iu;
20369b247179SDarrick J. Wong 	int			error;
20379b247179SDarrick J. Wong 
20389b247179SDarrick J. Wong 	/* Look up the old entry; if there wasn't one then exit. */
20399b247179SDarrick J. Wong 	iu = rhashtable_lookup_fast(&pag->pagi_unlinked_hash, &agino,
20409b247179SDarrick J. Wong 			xfs_iunlink_hash_params);
20419b247179SDarrick J. Wong 	if (!iu)
20429b247179SDarrick J. Wong 		return 0;
20439b247179SDarrick J. Wong 
20449b247179SDarrick J. Wong 	/*
20459b247179SDarrick J. Wong 	 * Remove the entry.  This shouldn't ever return an error, but if we
20469b247179SDarrick J. Wong 	 * couldn't remove the old entry we don't want to add it again to the
20479b247179SDarrick J. Wong 	 * hash table, and if the entry disappeared on us then someone's
20489b247179SDarrick J. Wong 	 * violated the locking rules and we need to fail loudly.  Either way
20499b247179SDarrick J. Wong 	 * we cannot remove the inode because internal state is or would have
20509b247179SDarrick J. Wong 	 * been corrupt.
20519b247179SDarrick J. Wong 	 */
20529b247179SDarrick J. Wong 	error = rhashtable_remove_fast(&pag->pagi_unlinked_hash,
20539b247179SDarrick J. Wong 			&iu->iu_rhash_head, xfs_iunlink_hash_params);
20549b247179SDarrick J. Wong 	if (error)
20559b247179SDarrick J. Wong 		return error;
20569b247179SDarrick J. Wong 
20579b247179SDarrick J. Wong 	/* If there is no new next entry just free our item and return. */
20589b247179SDarrick J. Wong 	if (next_unlinked == NULLAGINO) {
20599b247179SDarrick J. Wong 		kmem_free(iu);
20609b247179SDarrick J. Wong 		return 0;
20619b247179SDarrick J. Wong 	}
20629b247179SDarrick J. Wong 
20639b247179SDarrick J. Wong 	/* Update the entry and re-add it to the hash table. */
20649b247179SDarrick J. Wong 	iu->iu_next_unlinked = next_unlinked;
20659b247179SDarrick J. Wong 	return xfs_iunlink_insert_backref(pag, iu);
20669b247179SDarrick J. Wong }
20679b247179SDarrick J. Wong 
20689b247179SDarrick J. Wong /* Set up the in-core predecessor structures. */
20699b247179SDarrick J. Wong int
20709b247179SDarrick J. Wong xfs_iunlink_init(
20719b247179SDarrick J. Wong 	struct xfs_perag	*pag)
20729b247179SDarrick J. Wong {
20739b247179SDarrick J. Wong 	return rhashtable_init(&pag->pagi_unlinked_hash,
20749b247179SDarrick J. Wong 			&xfs_iunlink_hash_params);
20759b247179SDarrick J. Wong }
20769b247179SDarrick J. Wong 
20779b247179SDarrick J. Wong /* Free the in-core predecessor structures. */
20789b247179SDarrick J. Wong static void
20799b247179SDarrick J. Wong xfs_iunlink_free_item(
20809b247179SDarrick J. Wong 	void			*ptr,
20819b247179SDarrick J. Wong 	void			*arg)
20829b247179SDarrick J. Wong {
20839b247179SDarrick J. Wong 	struct xfs_iunlink	*iu = ptr;
20849b247179SDarrick J. Wong 	bool			*freed_anything = arg;
20859b247179SDarrick J. Wong 
20869b247179SDarrick J. Wong 	*freed_anything = true;
20879b247179SDarrick J. Wong 	kmem_free(iu);
20889b247179SDarrick J. Wong }
20899b247179SDarrick J. Wong 
20909b247179SDarrick J. Wong void
20919b247179SDarrick J. Wong xfs_iunlink_destroy(
20929b247179SDarrick J. Wong 	struct xfs_perag	*pag)
20939b247179SDarrick J. Wong {
20949b247179SDarrick J. Wong 	bool			freed_anything = false;
20959b247179SDarrick J. Wong 
20969b247179SDarrick J. Wong 	rhashtable_free_and_destroy(&pag->pagi_unlinked_hash,
20979b247179SDarrick J. Wong 			xfs_iunlink_free_item, &freed_anything);
20989b247179SDarrick J. Wong 
20999b247179SDarrick J. Wong 	ASSERT(freed_anything == false || XFS_FORCED_SHUTDOWN(pag->pag_mount));
21009b247179SDarrick J. Wong }
21019b247179SDarrick J. Wong 
21029b247179SDarrick J. Wong /*
21039a4a5118SDarrick J. Wong  * Point the AGI unlinked bucket at an inode and log the results.  The caller
21049a4a5118SDarrick J. Wong  * is responsible for validating the old value.
21059a4a5118SDarrick J. Wong  */
21069a4a5118SDarrick J. Wong STATIC int
21079a4a5118SDarrick J. Wong xfs_iunlink_update_bucket(
21089a4a5118SDarrick J. Wong 	struct xfs_trans	*tp,
21099a4a5118SDarrick J. Wong 	xfs_agnumber_t		agno,
21109a4a5118SDarrick J. Wong 	struct xfs_buf		*agibp,
21119a4a5118SDarrick J. Wong 	unsigned int		bucket_index,
21129a4a5118SDarrick J. Wong 	xfs_agino_t		new_agino)
21139a4a5118SDarrick J. Wong {
2114370c782bSChristoph Hellwig 	struct xfs_agi		*agi = agibp->b_addr;
21159a4a5118SDarrick J. Wong 	xfs_agino_t		old_value;
21169a4a5118SDarrick J. Wong 	int			offset;
21179a4a5118SDarrick J. Wong 
21189a4a5118SDarrick J. Wong 	ASSERT(xfs_verify_agino_or_null(tp->t_mountp, agno, new_agino));
21199a4a5118SDarrick J. Wong 
21209a4a5118SDarrick J. Wong 	old_value = be32_to_cpu(agi->agi_unlinked[bucket_index]);
21219a4a5118SDarrick J. Wong 	trace_xfs_iunlink_update_bucket(tp->t_mountp, agno, bucket_index,
21229a4a5118SDarrick J. Wong 			old_value, new_agino);
21239a4a5118SDarrick J. Wong 
21249a4a5118SDarrick J. Wong 	/*
21259a4a5118SDarrick J. Wong 	 * We should never find the head of the list already set to the value
21269a4a5118SDarrick J. Wong 	 * passed in because either we're adding or removing ourselves from the
21279a4a5118SDarrick J. Wong 	 * head of the list.
21289a4a5118SDarrick J. Wong 	 */
2129a5155b87SDarrick J. Wong 	if (old_value == new_agino) {
21308d57c216SDarrick J. Wong 		xfs_buf_mark_corrupt(agibp);
21319a4a5118SDarrick J. Wong 		return -EFSCORRUPTED;
2132a5155b87SDarrick J. Wong 	}
21339a4a5118SDarrick J. Wong 
21349a4a5118SDarrick J. Wong 	agi->agi_unlinked[bucket_index] = cpu_to_be32(new_agino);
21359a4a5118SDarrick J. Wong 	offset = offsetof(struct xfs_agi, agi_unlinked) +
21369a4a5118SDarrick J. Wong 			(sizeof(xfs_agino_t) * bucket_index);
21379a4a5118SDarrick J. Wong 	xfs_trans_log_buf(tp, agibp, offset, offset + sizeof(xfs_agino_t) - 1);
21389a4a5118SDarrick J. Wong 	return 0;
21399a4a5118SDarrick J. Wong }
21409a4a5118SDarrick J. Wong 
2141f2fc16a3SDarrick J. Wong /* Set an on-disk inode's next_unlinked pointer. */
2142f2fc16a3SDarrick J. Wong STATIC void
2143f2fc16a3SDarrick J. Wong xfs_iunlink_update_dinode(
2144f2fc16a3SDarrick J. Wong 	struct xfs_trans	*tp,
2145f2fc16a3SDarrick J. Wong 	xfs_agnumber_t		agno,
2146f2fc16a3SDarrick J. Wong 	xfs_agino_t		agino,
2147f2fc16a3SDarrick J. Wong 	struct xfs_buf		*ibp,
2148f2fc16a3SDarrick J. Wong 	struct xfs_dinode	*dip,
2149f2fc16a3SDarrick J. Wong 	struct xfs_imap		*imap,
2150f2fc16a3SDarrick J. Wong 	xfs_agino_t		next_agino)
2151f2fc16a3SDarrick J. Wong {
2152f2fc16a3SDarrick J. Wong 	struct xfs_mount	*mp = tp->t_mountp;
2153f2fc16a3SDarrick J. Wong 	int			offset;
2154f2fc16a3SDarrick J. Wong 
2155f2fc16a3SDarrick J. Wong 	ASSERT(xfs_verify_agino_or_null(mp, agno, next_agino));
2156f2fc16a3SDarrick J. Wong 
2157f2fc16a3SDarrick J. Wong 	trace_xfs_iunlink_update_dinode(mp, agno, agino,
2158f2fc16a3SDarrick J. Wong 			be32_to_cpu(dip->di_next_unlinked), next_agino);
2159f2fc16a3SDarrick J. Wong 
2160f2fc16a3SDarrick J. Wong 	dip->di_next_unlinked = cpu_to_be32(next_agino);
2161f2fc16a3SDarrick J. Wong 	offset = imap->im_boffset +
2162f2fc16a3SDarrick J. Wong 			offsetof(struct xfs_dinode, di_next_unlinked);
2163f2fc16a3SDarrick J. Wong 
2164f2fc16a3SDarrick J. Wong 	/* need to recalc the inode CRC if appropriate */
2165f2fc16a3SDarrick J. Wong 	xfs_dinode_calc_crc(mp, dip);
2166f2fc16a3SDarrick J. Wong 	xfs_trans_inode_buf(tp, ibp);
2167f2fc16a3SDarrick J. Wong 	xfs_trans_log_buf(tp, ibp, offset, offset + sizeof(xfs_agino_t) - 1);
2168f2fc16a3SDarrick J. Wong }
2169f2fc16a3SDarrick J. Wong 
2170f2fc16a3SDarrick J. Wong /* Set an in-core inode's unlinked pointer and return the old value. */
2171f2fc16a3SDarrick J. Wong STATIC int
2172f2fc16a3SDarrick J. Wong xfs_iunlink_update_inode(
2173f2fc16a3SDarrick J. Wong 	struct xfs_trans	*tp,
2174f2fc16a3SDarrick J. Wong 	struct xfs_inode	*ip,
2175f2fc16a3SDarrick J. Wong 	xfs_agnumber_t		agno,
2176f2fc16a3SDarrick J. Wong 	xfs_agino_t		next_agino,
2177f2fc16a3SDarrick J. Wong 	xfs_agino_t		*old_next_agino)
2178f2fc16a3SDarrick J. Wong {
2179f2fc16a3SDarrick J. Wong 	struct xfs_mount	*mp = tp->t_mountp;
2180f2fc16a3SDarrick J. Wong 	struct xfs_dinode	*dip;
2181f2fc16a3SDarrick J. Wong 	struct xfs_buf		*ibp;
2182f2fc16a3SDarrick J. Wong 	xfs_agino_t		old_value;
2183f2fc16a3SDarrick J. Wong 	int			error;
2184f2fc16a3SDarrick J. Wong 
2185f2fc16a3SDarrick J. Wong 	ASSERT(xfs_verify_agino_or_null(mp, agno, next_agino));
2186f2fc16a3SDarrick J. Wong 
2187c1995079SBrian Foster 	error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp, 0);
2188f2fc16a3SDarrick J. Wong 	if (error)
2189f2fc16a3SDarrick J. Wong 		return error;
2190f2fc16a3SDarrick J. Wong 
2191f2fc16a3SDarrick J. Wong 	/* Make sure the old pointer isn't garbage. */
2192f2fc16a3SDarrick J. Wong 	old_value = be32_to_cpu(dip->di_next_unlinked);
2193f2fc16a3SDarrick J. Wong 	if (!xfs_verify_agino_or_null(mp, agno, old_value)) {
2194a5155b87SDarrick J. Wong 		xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, dip,
2195a5155b87SDarrick J. Wong 				sizeof(*dip), __this_address);
2196f2fc16a3SDarrick J. Wong 		error = -EFSCORRUPTED;
2197f2fc16a3SDarrick J. Wong 		goto out;
2198f2fc16a3SDarrick J. Wong 	}
2199f2fc16a3SDarrick J. Wong 
2200f2fc16a3SDarrick J. Wong 	/*
2201f2fc16a3SDarrick J. Wong 	 * Since we're updating a linked list, we should never find that the
2202f2fc16a3SDarrick J. Wong 	 * current pointer is the same as the new value, unless we're
2203f2fc16a3SDarrick J. Wong 	 * terminating the list.
2204f2fc16a3SDarrick J. Wong 	 */
2205f2fc16a3SDarrick J. Wong 	*old_next_agino = old_value;
2206f2fc16a3SDarrick J. Wong 	if (old_value == next_agino) {
2207a5155b87SDarrick J. Wong 		if (next_agino != NULLAGINO) {
2208a5155b87SDarrick J. Wong 			xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__,
2209a5155b87SDarrick J. Wong 					dip, sizeof(*dip), __this_address);
2210f2fc16a3SDarrick J. Wong 			error = -EFSCORRUPTED;
2211a5155b87SDarrick J. Wong 		}
2212f2fc16a3SDarrick J. Wong 		goto out;
2213f2fc16a3SDarrick J. Wong 	}
2214f2fc16a3SDarrick J. Wong 
2215f2fc16a3SDarrick J. Wong 	/* Ok, update the new pointer. */
2216f2fc16a3SDarrick J. Wong 	xfs_iunlink_update_dinode(tp, agno, XFS_INO_TO_AGINO(mp, ip->i_ino),
2217f2fc16a3SDarrick J. Wong 			ibp, dip, &ip->i_imap, next_agino);
2218f2fc16a3SDarrick J. Wong 	return 0;
2219f2fc16a3SDarrick J. Wong out:
2220f2fc16a3SDarrick J. Wong 	xfs_trans_brelse(tp, ibp);
2221f2fc16a3SDarrick J. Wong 	return error;
2222f2fc16a3SDarrick J. Wong }
2223f2fc16a3SDarrick J. Wong 
22249a4a5118SDarrick J. Wong /*
2225c4a6bf7fSDarrick J. Wong  * This is called when the inode's link count has gone to 0 or we are creating
2226c4a6bf7fSDarrick J. Wong  * a tmpfile via O_TMPFILE.  The inode @ip must have nlink == 0.
222754d7b5c1SDave Chinner  *
222854d7b5c1SDave Chinner  * We place the on-disk inode on a list in the AGI.  It will be pulled from this
222954d7b5c1SDave Chinner  * list when the inode is freed.
22301da177e4SLinus Torvalds  */
223154d7b5c1SDave Chinner STATIC int
22321da177e4SLinus Torvalds xfs_iunlink(
223354d7b5c1SDave Chinner 	struct xfs_trans	*tp,
223454d7b5c1SDave Chinner 	struct xfs_inode	*ip)
22351da177e4SLinus Torvalds {
22365837f625SDarrick J. Wong 	struct xfs_mount	*mp = tp->t_mountp;
22375837f625SDarrick J. Wong 	struct xfs_agi		*agi;
22385837f625SDarrick J. Wong 	struct xfs_buf		*agibp;
223986bfd375SDarrick J. Wong 	xfs_agino_t		next_agino;
22405837f625SDarrick J. Wong 	xfs_agnumber_t		agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
22415837f625SDarrick J. Wong 	xfs_agino_t		agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
22425837f625SDarrick J. Wong 	short			bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
22431da177e4SLinus Torvalds 	int			error;
22441da177e4SLinus Torvalds 
2245c4a6bf7fSDarrick J. Wong 	ASSERT(VFS_I(ip)->i_nlink == 0);
2246c19b3b05SDave Chinner 	ASSERT(VFS_I(ip)->i_mode != 0);
22474664c66cSDarrick J. Wong 	trace_xfs_iunlink(ip);
22481da177e4SLinus Torvalds 
22495837f625SDarrick J. Wong 	/* Get the agi buffer first.  It ensures lock ordering on the list. */
22505837f625SDarrick J. Wong 	error = xfs_read_agi(mp, tp, agno, &agibp);
2251859d7182SVlad Apostolov 	if (error)
22521da177e4SLinus Torvalds 		return error;
2253370c782bSChristoph Hellwig 	agi = agibp->b_addr;
22545e1be0fbSChristoph Hellwig 
22551da177e4SLinus Torvalds 	/*
225686bfd375SDarrick J. Wong 	 * Get the index into the agi hash table for the list this inode will
225786bfd375SDarrick J. Wong 	 * go on.  Make sure the pointer isn't garbage and that this inode
225886bfd375SDarrick J. Wong 	 * isn't already on the list.
22591da177e4SLinus Torvalds 	 */
226086bfd375SDarrick J. Wong 	next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
226186bfd375SDarrick J. Wong 	if (next_agino == agino ||
2262a5155b87SDarrick J. Wong 	    !xfs_verify_agino_or_null(mp, agno, next_agino)) {
22638d57c216SDarrick J. Wong 		xfs_buf_mark_corrupt(agibp);
226486bfd375SDarrick J. Wong 		return -EFSCORRUPTED;
2265a5155b87SDarrick J. Wong 	}
22661da177e4SLinus Torvalds 
226786bfd375SDarrick J. Wong 	if (next_agino != NULLAGINO) {
2268f2fc16a3SDarrick J. Wong 		xfs_agino_t		old_agino;
2269f2fc16a3SDarrick J. Wong 
22701da177e4SLinus Torvalds 		/*
2271f2fc16a3SDarrick J. Wong 		 * There is already another inode in the bucket, so point this
2272f2fc16a3SDarrick J. Wong 		 * inode to the current head of the list.
22731da177e4SLinus Torvalds 		 */
2274f2fc16a3SDarrick J. Wong 		error = xfs_iunlink_update_inode(tp, ip, agno, next_agino,
2275f2fc16a3SDarrick J. Wong 				&old_agino);
2276c319b58bSVlad Apostolov 		if (error)
2277c319b58bSVlad Apostolov 			return error;
2278f2fc16a3SDarrick J. Wong 		ASSERT(old_agino == NULLAGINO);
22799b247179SDarrick J. Wong 
22809b247179SDarrick J. Wong 		/*
22819b247179SDarrick J. Wong 		 * agino has been unlinked, add a backref from the next inode
22829b247179SDarrick J. Wong 		 * back to agino.
22839b247179SDarrick J. Wong 		 */
228492a00544SGao Xiang 		error = xfs_iunlink_add_backref(agibp->b_pag, agino, next_agino);
22859b247179SDarrick J. Wong 		if (error)
22869b247179SDarrick J. Wong 			return error;
22871da177e4SLinus Torvalds 	}
22881da177e4SLinus Torvalds 
22899a4a5118SDarrick J. Wong 	/* Point the head of the list to point to this inode. */
22909a4a5118SDarrick J. Wong 	return xfs_iunlink_update_bucket(tp, agno, agibp, bucket_index, agino);
22911da177e4SLinus Torvalds }
22921da177e4SLinus Torvalds 
229323ffa52cSDarrick J. Wong /* Return the imap, dinode pointer, and buffer for an inode. */
229423ffa52cSDarrick J. Wong STATIC int
229523ffa52cSDarrick J. Wong xfs_iunlink_map_ino(
229623ffa52cSDarrick J. Wong 	struct xfs_trans	*tp,
229723ffa52cSDarrick J. Wong 	xfs_agnumber_t		agno,
229823ffa52cSDarrick J. Wong 	xfs_agino_t		agino,
229923ffa52cSDarrick J. Wong 	struct xfs_imap		*imap,
230023ffa52cSDarrick J. Wong 	struct xfs_dinode	**dipp,
230123ffa52cSDarrick J. Wong 	struct xfs_buf		**bpp)
230223ffa52cSDarrick J. Wong {
230323ffa52cSDarrick J. Wong 	struct xfs_mount	*mp = tp->t_mountp;
230423ffa52cSDarrick J. Wong 	int			error;
230523ffa52cSDarrick J. Wong 
230623ffa52cSDarrick J. Wong 	imap->im_blkno = 0;
230723ffa52cSDarrick J. Wong 	error = xfs_imap(mp, tp, XFS_AGINO_TO_INO(mp, agno, agino), imap, 0);
230823ffa52cSDarrick J. Wong 	if (error) {
230923ffa52cSDarrick J. Wong 		xfs_warn(mp, "%s: xfs_imap returned error %d.",
231023ffa52cSDarrick J. Wong 				__func__, error);
231123ffa52cSDarrick J. Wong 		return error;
231223ffa52cSDarrick J. Wong 	}
231323ffa52cSDarrick J. Wong 
2314c1995079SBrian Foster 	error = xfs_imap_to_bp(mp, tp, imap, dipp, bpp, 0);
231523ffa52cSDarrick J. Wong 	if (error) {
231623ffa52cSDarrick J. Wong 		xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
231723ffa52cSDarrick J. Wong 				__func__, error);
231823ffa52cSDarrick J. Wong 		return error;
231923ffa52cSDarrick J. Wong 	}
232023ffa52cSDarrick J. Wong 
232123ffa52cSDarrick J. Wong 	return 0;
232223ffa52cSDarrick J. Wong }
232323ffa52cSDarrick J. Wong 
232423ffa52cSDarrick J. Wong /*
232523ffa52cSDarrick J. Wong  * Walk the unlinked chain from @head_agino until we find the inode that
232623ffa52cSDarrick J. Wong  * points to @target_agino.  Return the inode number, map, dinode pointer,
232723ffa52cSDarrick J. Wong  * and inode cluster buffer of that inode as @agino, @imap, @dipp, and @bpp.
232823ffa52cSDarrick J. Wong  *
232923ffa52cSDarrick J. Wong  * @tp, @pag, @head_agino, and @target_agino are input parameters.
233023ffa52cSDarrick J. Wong  * @agino, @imap, @dipp, and @bpp are all output parameters.
233123ffa52cSDarrick J. Wong  *
233223ffa52cSDarrick J. Wong  * Do not call this function if @target_agino is the head of the list.
233323ffa52cSDarrick J. Wong  */
233423ffa52cSDarrick J. Wong STATIC int
233523ffa52cSDarrick J. Wong xfs_iunlink_map_prev(
233623ffa52cSDarrick J. Wong 	struct xfs_trans	*tp,
233723ffa52cSDarrick J. Wong 	xfs_agnumber_t		agno,
233823ffa52cSDarrick J. Wong 	xfs_agino_t		head_agino,
233923ffa52cSDarrick J. Wong 	xfs_agino_t		target_agino,
234023ffa52cSDarrick J. Wong 	xfs_agino_t		*agino,
234123ffa52cSDarrick J. Wong 	struct xfs_imap		*imap,
234223ffa52cSDarrick J. Wong 	struct xfs_dinode	**dipp,
23439b247179SDarrick J. Wong 	struct xfs_buf		**bpp,
23449b247179SDarrick J. Wong 	struct xfs_perag	*pag)
234523ffa52cSDarrick J. Wong {
234623ffa52cSDarrick J. Wong 	struct xfs_mount	*mp = tp->t_mountp;
234723ffa52cSDarrick J. Wong 	xfs_agino_t		next_agino;
234823ffa52cSDarrick J. Wong 	int			error;
234923ffa52cSDarrick J. Wong 
235023ffa52cSDarrick J. Wong 	ASSERT(head_agino != target_agino);
235123ffa52cSDarrick J. Wong 	*bpp = NULL;
235223ffa52cSDarrick J. Wong 
23539b247179SDarrick J. Wong 	/* See if our backref cache can find it faster. */
23549b247179SDarrick J. Wong 	*agino = xfs_iunlink_lookup_backref(pag, target_agino);
23559b247179SDarrick J. Wong 	if (*agino != NULLAGINO) {
23569b247179SDarrick J. Wong 		error = xfs_iunlink_map_ino(tp, agno, *agino, imap, dipp, bpp);
23579b247179SDarrick J. Wong 		if (error)
23589b247179SDarrick J. Wong 			return error;
23599b247179SDarrick J. Wong 
23609b247179SDarrick J. Wong 		if (be32_to_cpu((*dipp)->di_next_unlinked) == target_agino)
23619b247179SDarrick J. Wong 			return 0;
23629b247179SDarrick J. Wong 
23639b247179SDarrick J. Wong 		/*
23649b247179SDarrick J. Wong 		 * If we get here the cache contents were corrupt, so drop the
23659b247179SDarrick J. Wong 		 * buffer and fall back to walking the bucket list.
23669b247179SDarrick J. Wong 		 */
23679b247179SDarrick J. Wong 		xfs_trans_brelse(tp, *bpp);
23689b247179SDarrick J. Wong 		*bpp = NULL;
23699b247179SDarrick J. Wong 		WARN_ON_ONCE(1);
23709b247179SDarrick J. Wong 	}
23719b247179SDarrick J. Wong 
23729b247179SDarrick J. Wong 	trace_xfs_iunlink_map_prev_fallback(mp, agno);
23739b247179SDarrick J. Wong 
23749b247179SDarrick J. Wong 	/* Otherwise, walk the entire bucket until we find it. */
237523ffa52cSDarrick J. Wong 	next_agino = head_agino;
237623ffa52cSDarrick J. Wong 	while (next_agino != target_agino) {
237723ffa52cSDarrick J. Wong 		xfs_agino_t	unlinked_agino;
237823ffa52cSDarrick J. Wong 
237923ffa52cSDarrick J. Wong 		if (*bpp)
238023ffa52cSDarrick J. Wong 			xfs_trans_brelse(tp, *bpp);
238123ffa52cSDarrick J. Wong 
238223ffa52cSDarrick J. Wong 		*agino = next_agino;
238323ffa52cSDarrick J. Wong 		error = xfs_iunlink_map_ino(tp, agno, next_agino, imap, dipp,
238423ffa52cSDarrick J. Wong 				bpp);
238523ffa52cSDarrick J. Wong 		if (error)
238623ffa52cSDarrick J. Wong 			return error;
238723ffa52cSDarrick J. Wong 
238823ffa52cSDarrick J. Wong 		unlinked_agino = be32_to_cpu((*dipp)->di_next_unlinked);
238923ffa52cSDarrick J. Wong 		/*
239023ffa52cSDarrick J. Wong 		 * Make sure this pointer is valid and isn't an obvious
239123ffa52cSDarrick J. Wong 		 * infinite loop.
239223ffa52cSDarrick J. Wong 		 */
239323ffa52cSDarrick J. Wong 		if (!xfs_verify_agino(mp, agno, unlinked_agino) ||
239423ffa52cSDarrick J. Wong 		    next_agino == unlinked_agino) {
239523ffa52cSDarrick J. Wong 			XFS_CORRUPTION_ERROR(__func__,
239623ffa52cSDarrick J. Wong 					XFS_ERRLEVEL_LOW, mp,
239723ffa52cSDarrick J. Wong 					*dipp, sizeof(**dipp));
239823ffa52cSDarrick J. Wong 			error = -EFSCORRUPTED;
239923ffa52cSDarrick J. Wong 			return error;
240023ffa52cSDarrick J. Wong 		}
240123ffa52cSDarrick J. Wong 		next_agino = unlinked_agino;
240223ffa52cSDarrick J. Wong 	}
240323ffa52cSDarrick J. Wong 
240423ffa52cSDarrick J. Wong 	return 0;
240523ffa52cSDarrick J. Wong }
240623ffa52cSDarrick J. Wong 
24071da177e4SLinus Torvalds /*
24081da177e4SLinus Torvalds  * Pull the on-disk inode from the AGI unlinked list.
24091da177e4SLinus Torvalds  */
24101da177e4SLinus Torvalds STATIC int
24111da177e4SLinus Torvalds xfs_iunlink_remove(
24125837f625SDarrick J. Wong 	struct xfs_trans	*tp,
24135837f625SDarrick J. Wong 	struct xfs_inode	*ip)
24141da177e4SLinus Torvalds {
24155837f625SDarrick J. Wong 	struct xfs_mount	*mp = tp->t_mountp;
24165837f625SDarrick J. Wong 	struct xfs_agi		*agi;
24175837f625SDarrick J. Wong 	struct xfs_buf		*agibp;
24185837f625SDarrick J. Wong 	struct xfs_buf		*last_ibp;
24195837f625SDarrick J. Wong 	struct xfs_dinode	*last_dip = NULL;
24205837f625SDarrick J. Wong 	xfs_agnumber_t		agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
24215837f625SDarrick J. Wong 	xfs_agino_t		agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
24221da177e4SLinus Torvalds 	xfs_agino_t		next_agino;
2423b1d2a068SDarrick J. Wong 	xfs_agino_t		head_agino;
24245837f625SDarrick J. Wong 	short			bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
24251da177e4SLinus Torvalds 	int			error;
24261da177e4SLinus Torvalds 
24274664c66cSDarrick J. Wong 	trace_xfs_iunlink_remove(ip);
24284664c66cSDarrick J. Wong 
24295837f625SDarrick J. Wong 	/* Get the agi buffer first.  It ensures lock ordering on the list. */
24305e1be0fbSChristoph Hellwig 	error = xfs_read_agi(mp, tp, agno, &agibp);
24315e1be0fbSChristoph Hellwig 	if (error)
24321da177e4SLinus Torvalds 		return error;
2433370c782bSChristoph Hellwig 	agi = agibp->b_addr;
24345e1be0fbSChristoph Hellwig 
24351da177e4SLinus Torvalds 	/*
243686bfd375SDarrick J. Wong 	 * Get the index into the agi hash table for the list this inode will
243786bfd375SDarrick J. Wong 	 * go on.  Make sure the head pointer isn't garbage.
24381da177e4SLinus Torvalds 	 */
2439b1d2a068SDarrick J. Wong 	head_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
2440b1d2a068SDarrick J. Wong 	if (!xfs_verify_agino(mp, agno, head_agino)) {
2441d2e73665SDarrick J. Wong 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
2442d2e73665SDarrick J. Wong 				agi, sizeof(*agi));
2443d2e73665SDarrick J. Wong 		return -EFSCORRUPTED;
2444d2e73665SDarrick J. Wong 	}
24451da177e4SLinus Torvalds 
24461da177e4SLinus Torvalds 	/*
2447b1d2a068SDarrick J. Wong 	 * Set our inode's next_unlinked pointer to NULL and then return
2448b1d2a068SDarrick J. Wong 	 * the old pointer value so that we can update whatever was previous
2449b1d2a068SDarrick J. Wong 	 * to us in the list to point to whatever was next in the list.
24501da177e4SLinus Torvalds 	 */
2451b1d2a068SDarrick J. Wong 	error = xfs_iunlink_update_inode(tp, ip, agno, NULLAGINO, &next_agino);
2452f2fc16a3SDarrick J. Wong 	if (error)
24531da177e4SLinus Torvalds 		return error;
24549a4a5118SDarrick J. Wong 
24559b247179SDarrick J. Wong 	/*
24569b247179SDarrick J. Wong 	 * If there was a backref pointing from the next inode back to this
24579b247179SDarrick J. Wong 	 * one, remove it because we've removed this inode from the list.
24589b247179SDarrick J. Wong 	 *
24599b247179SDarrick J. Wong 	 * Later, if this inode was in the middle of the list we'll update
24609b247179SDarrick J. Wong 	 * this inode's backref to point from the next inode.
24619b247179SDarrick J. Wong 	 */
24629b247179SDarrick J. Wong 	if (next_agino != NULLAGINO) {
246392a00544SGao Xiang 		error = xfs_iunlink_change_backref(agibp->b_pag, next_agino,
24649b247179SDarrick J. Wong 				NULLAGINO);
24659b247179SDarrick J. Wong 		if (error)
246692a00544SGao Xiang 			return error;
24679b247179SDarrick J. Wong 	}
24689b247179SDarrick J. Wong 
246992a00544SGao Xiang 	if (head_agino != agino) {
2470f2fc16a3SDarrick J. Wong 		struct xfs_imap	imap;
2471f2fc16a3SDarrick J. Wong 		xfs_agino_t	prev_agino;
2472f2fc16a3SDarrick J. Wong 
247323ffa52cSDarrick J. Wong 		/* We need to search the list for the inode being freed. */
2474b1d2a068SDarrick J. Wong 		error = xfs_iunlink_map_prev(tp, agno, head_agino, agino,
24759b247179SDarrick J. Wong 				&prev_agino, &imap, &last_dip, &last_ibp,
247692a00544SGao Xiang 				agibp->b_pag);
247723ffa52cSDarrick J. Wong 		if (error)
247892a00544SGao Xiang 			return error;
2479475ee413SChristoph Hellwig 
2480f2fc16a3SDarrick J. Wong 		/* Point the previous inode on the list to the next inode. */
2481f2fc16a3SDarrick J. Wong 		xfs_iunlink_update_dinode(tp, agno, prev_agino, last_ibp,
2482f2fc16a3SDarrick J. Wong 				last_dip, &imap, next_agino);
24839b247179SDarrick J. Wong 
24849b247179SDarrick J. Wong 		/*
24859b247179SDarrick J. Wong 		 * Now we deal with the backref for this inode.  If this inode
24869b247179SDarrick J. Wong 		 * pointed at a real inode, change the backref that pointed to
24879b247179SDarrick J. Wong 		 * us to point to our old next.  If this inode was the end of
24889b247179SDarrick J. Wong 		 * the list, delete the backref that pointed to us.  Note that
24899b247179SDarrick J. Wong 		 * change_backref takes care of deleting the backref if
24909b247179SDarrick J. Wong 		 * next_agino is NULLAGINO.
24919b247179SDarrick J. Wong 		 */
249292a00544SGao Xiang 		return xfs_iunlink_change_backref(agibp->b_pag, agino,
249392a00544SGao Xiang 				next_agino);
24941da177e4SLinus Torvalds 	}
24959b247179SDarrick J. Wong 
249692a00544SGao Xiang 	/* Point the head of the list to the next unlinked inode. */
249792a00544SGao Xiang 	return xfs_iunlink_update_bucket(tp, agno, agibp, bucket_index,
249892a00544SGao Xiang 			next_agino);
24991da177e4SLinus Torvalds }
25001da177e4SLinus Torvalds 
25015b3eed75SDave Chinner /*
250271e3e356SDave Chinner  * Look up the inode number specified and if it is not already marked XFS_ISTALE
250371e3e356SDave Chinner  * mark it stale. We should only find clean inodes in this lookup that aren't
250471e3e356SDave Chinner  * already stale.
25055806165aSDave Chinner  */
250671e3e356SDave Chinner static void
250771e3e356SDave Chinner xfs_ifree_mark_inode_stale(
250871e3e356SDave Chinner 	struct xfs_buf		*bp,
25095806165aSDave Chinner 	struct xfs_inode	*free_ip,
2510d9fdd0adSBrian Foster 	xfs_ino_t		inum)
25115806165aSDave Chinner {
251271e3e356SDave Chinner 	struct xfs_mount	*mp = bp->b_mount;
251371e3e356SDave Chinner 	struct xfs_perag	*pag = bp->b_pag;
251471e3e356SDave Chinner 	struct xfs_inode_log_item *iip;
25155806165aSDave Chinner 	struct xfs_inode	*ip;
25165806165aSDave Chinner 
25175806165aSDave Chinner retry:
25185806165aSDave Chinner 	rcu_read_lock();
25195806165aSDave Chinner 	ip = radix_tree_lookup(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, inum));
25205806165aSDave Chinner 
25215806165aSDave Chinner 	/* Inode not in memory, nothing to do */
252271e3e356SDave Chinner 	if (!ip) {
252371e3e356SDave Chinner 		rcu_read_unlock();
252471e3e356SDave Chinner 		return;
252571e3e356SDave Chinner 	}
25265806165aSDave Chinner 
25275806165aSDave Chinner 	/*
25285806165aSDave Chinner 	 * because this is an RCU protected lookup, we could find a recently
25295806165aSDave Chinner 	 * freed or even reallocated inode during the lookup. We need to check
25305806165aSDave Chinner 	 * under the i_flags_lock for a valid inode here. Skip it if it is not
25315806165aSDave Chinner 	 * valid, the wrong inode or stale.
25325806165aSDave Chinner 	 */
25335806165aSDave Chinner 	spin_lock(&ip->i_flags_lock);
25345806165aSDave Chinner 	if (ip->i_ino != inum || __xfs_iflags_test(ip, XFS_ISTALE)) {
25355806165aSDave Chinner 		spin_unlock(&ip->i_flags_lock);
253671e3e356SDave Chinner 		rcu_read_unlock();
253771e3e356SDave Chinner 		return;
25385806165aSDave Chinner 	}
25395806165aSDave Chinner 
25405806165aSDave Chinner 	/*
25415806165aSDave Chinner 	 * Don't try to lock/unlock the current inode, but we _cannot_ skip the
25425806165aSDave Chinner 	 * other inodes that we did not find in the list attached to the buffer
25435806165aSDave Chinner 	 * and are not already marked stale. If we can't lock it, back off and
25445806165aSDave Chinner 	 * retry.
25455806165aSDave Chinner 	 */
25465806165aSDave Chinner 	if (ip != free_ip) {
25475806165aSDave Chinner 		if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
254871e3e356SDave Chinner 			spin_unlock(&ip->i_flags_lock);
25495806165aSDave Chinner 			rcu_read_unlock();
25505806165aSDave Chinner 			delay(1);
25515806165aSDave Chinner 			goto retry;
25525806165aSDave Chinner 		}
25535806165aSDave Chinner 	}
255471e3e356SDave Chinner 	ip->i_flags |= XFS_ISTALE;
255571e3e356SDave Chinner 	spin_unlock(&ip->i_flags_lock);
25565806165aSDave Chinner 	rcu_read_unlock();
25575806165aSDave Chinner 
255871e3e356SDave Chinner 	/*
255971e3e356SDave Chinner 	 * If we can't get the flush lock, the inode is already attached.  All
256071e3e356SDave Chinner 	 * we needed to do here is mark the inode stale so buffer IO completion
256171e3e356SDave Chinner 	 * will remove it from the AIL.
256271e3e356SDave Chinner 	 */
256371e3e356SDave Chinner 	iip = ip->i_itemp;
256471e3e356SDave Chinner 	if (!xfs_iflock_nowait(ip)) {
256571e3e356SDave Chinner 		ASSERT(!list_empty(&iip->ili_item.li_bio_list));
256671e3e356SDave Chinner 		ASSERT(iip->ili_last_fields);
256771e3e356SDave Chinner 		goto out_iunlock;
256871e3e356SDave Chinner 	}
25695806165aSDave Chinner 
25705806165aSDave Chinner 	/*
257148d55e2aSDave Chinner 	 * Inodes not attached to the buffer can be released immediately.
257248d55e2aSDave Chinner 	 * Everything else has to go through xfs_iflush_abort() on journal
257348d55e2aSDave Chinner 	 * commit as the flock synchronises removal of the inode from the
257448d55e2aSDave Chinner 	 * cluster buffer against inode reclaim.
25755806165aSDave Chinner 	 */
257648d55e2aSDave Chinner 	if (!iip || list_empty(&iip->ili_item.li_bio_list)) {
25775806165aSDave Chinner 		xfs_ifunlock(ip);
257871e3e356SDave Chinner 		goto out_iunlock;
25795806165aSDave Chinner 	}
25805806165aSDave Chinner 
258171e3e356SDave Chinner 	/* we have a dirty inode in memory that has not yet been flushed. */
258271e3e356SDave Chinner 	spin_lock(&iip->ili_lock);
258371e3e356SDave Chinner 	iip->ili_last_fields = iip->ili_fields;
258471e3e356SDave Chinner 	iip->ili_fields = 0;
258571e3e356SDave Chinner 	iip->ili_fsync_fields = 0;
258671e3e356SDave Chinner 	spin_unlock(&iip->ili_lock);
258771e3e356SDave Chinner 	ASSERT(iip->ili_last_fields);
258871e3e356SDave Chinner 
258971e3e356SDave Chinner out_iunlock:
259071e3e356SDave Chinner 	if (ip != free_ip)
259171e3e356SDave Chinner 		xfs_iunlock(ip, XFS_ILOCK_EXCL);
25925806165aSDave Chinner }
25935806165aSDave Chinner 
25945806165aSDave Chinner /*
25950b8182dbSZhi Yong Wu  * A big issue when freeing the inode cluster is that we _cannot_ skip any
25965b3eed75SDave Chinner  * inodes that are in memory - they all must be marked stale and attached to
25975b3eed75SDave Chinner  * the cluster buffer.
25985b3eed75SDave Chinner  */
25992a30f36dSChandra Seetharaman STATIC int
26001da177e4SLinus Torvalds xfs_ifree_cluster(
260171e3e356SDave Chinner 	struct xfs_inode	*free_ip,
260271e3e356SDave Chinner 	struct xfs_trans	*tp,
260309b56604SBrian Foster 	struct xfs_icluster	*xic)
26041da177e4SLinus Torvalds {
260571e3e356SDave Chinner 	struct xfs_mount	*mp = free_ip->i_mount;
260671e3e356SDave Chinner 	struct xfs_ino_geometry	*igeo = M_IGEO(mp);
260771e3e356SDave Chinner 	struct xfs_buf		*bp;
260871e3e356SDave Chinner 	xfs_daddr_t		blkno;
260971e3e356SDave Chinner 	xfs_ino_t		inum = xic->first_ino;
26101da177e4SLinus Torvalds 	int			nbufs;
26115b257b4aSDave Chinner 	int			i, j;
26123cdaa189SBrian Foster 	int			ioffset;
2613ce92464cSDarrick J. Wong 	int			error;
26141da177e4SLinus Torvalds 
2615ef325959SDarrick J. Wong 	nbufs = igeo->ialloc_blks / igeo->blocks_per_cluster;
26161da177e4SLinus Torvalds 
2617ef325959SDarrick J. Wong 	for (j = 0; j < nbufs; j++, inum += igeo->inodes_per_cluster) {
261809b56604SBrian Foster 		/*
261909b56604SBrian Foster 		 * The allocation bitmap tells us which inodes of the chunk were
262009b56604SBrian Foster 		 * physically allocated. Skip the cluster if an inode falls into
262109b56604SBrian Foster 		 * a sparse region.
262209b56604SBrian Foster 		 */
26233cdaa189SBrian Foster 		ioffset = inum - xic->first_ino;
26243cdaa189SBrian Foster 		if ((xic->alloc & XFS_INOBT_MASK(ioffset)) == 0) {
2625ef325959SDarrick J. Wong 			ASSERT(ioffset % igeo->inodes_per_cluster == 0);
262609b56604SBrian Foster 			continue;
262709b56604SBrian Foster 		}
262809b56604SBrian Foster 
26291da177e4SLinus Torvalds 		blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
26301da177e4SLinus Torvalds 					 XFS_INO_TO_AGBNO(mp, inum));
26311da177e4SLinus Torvalds 
26321da177e4SLinus Torvalds 		/*
26335b257b4aSDave Chinner 		 * We obtain and lock the backing buffer first in the process
26345b257b4aSDave Chinner 		 * here, as we have to ensure that any dirty inode that we
26355b257b4aSDave Chinner 		 * can't get the flush lock on is attached to the buffer.
26365b257b4aSDave Chinner 		 * If we scan the in-memory inodes first, then buffer IO can
26375b257b4aSDave Chinner 		 * complete before we get a lock on it, and hence we may fail
26385b257b4aSDave Chinner 		 * to mark all the active inodes on the buffer stale.
26391da177e4SLinus Torvalds 		 */
2640ce92464cSDarrick J. Wong 		error = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
2641ef325959SDarrick J. Wong 				mp->m_bsize * igeo->blocks_per_cluster,
2642ce92464cSDarrick J. Wong 				XBF_UNMAPPED, &bp);
264371e3e356SDave Chinner 		if (error)
2644ce92464cSDarrick J. Wong 			return error;
2645b0f539deSDave Chinner 
2646b0f539deSDave Chinner 		/*
2647b0f539deSDave Chinner 		 * This buffer may not have been correctly initialised as we
2648b0f539deSDave Chinner 		 * didn't read it from disk. That's not important because we are
2649b0f539deSDave Chinner 		 * only using to mark the buffer as stale in the log, and to
2650b0f539deSDave Chinner 		 * attach stale cached inodes on it. That means it will never be
2651b0f539deSDave Chinner 		 * dispatched for IO. If it is, we want to know about it, and we
2652b0f539deSDave Chinner 		 * want it to fail. We can acheive this by adding a write
2653b0f539deSDave Chinner 		 * verifier to the buffer.
2654b0f539deSDave Chinner 		 */
26551813dd64SDave Chinner 		bp->b_ops = &xfs_inode_buf_ops;
2656b0f539deSDave Chinner 
26575b257b4aSDave Chinner 		/*
265871e3e356SDave Chinner 		 * Now we need to set all the cached clean inodes as XFS_ISTALE,
265971e3e356SDave Chinner 		 * too. This requires lookups, and will skip inodes that we've
266071e3e356SDave Chinner 		 * already marked XFS_ISTALE.
26615b257b4aSDave Chinner 		 */
266271e3e356SDave Chinner 		for (i = 0; i < igeo->inodes_per_cluster; i++)
266371e3e356SDave Chinner 			xfs_ifree_mark_inode_stale(bp, free_ip, inum + i);
26641da177e4SLinus Torvalds 
26651da177e4SLinus Torvalds 		xfs_trans_stale_inode_buf(tp, bp);
26661da177e4SLinus Torvalds 		xfs_trans_binval(tp, bp);
26671da177e4SLinus Torvalds 	}
26682a30f36dSChandra Seetharaman 	return 0;
26691da177e4SLinus Torvalds }
26701da177e4SLinus Torvalds 
26711da177e4SLinus Torvalds /*
26721da177e4SLinus Torvalds  * This is called to return an inode to the inode free list.
26731da177e4SLinus Torvalds  * The inode should already be truncated to 0 length and have
26741da177e4SLinus Torvalds  * no pages associated with it.  This routine also assumes that
26751da177e4SLinus Torvalds  * the inode is already a part of the transaction.
26761da177e4SLinus Torvalds  *
26771da177e4SLinus Torvalds  * The on-disk copy of the inode will have been added to the list
26781da177e4SLinus Torvalds  * of unlinked inodes in the AGI. We need to remove the inode from
26791da177e4SLinus Torvalds  * that list atomically with respect to freeing it here.
26801da177e4SLinus Torvalds  */
26811da177e4SLinus Torvalds int
26821da177e4SLinus Torvalds xfs_ifree(
26830e0417f3SBrian Foster 	struct xfs_trans	*tp,
26840e0417f3SBrian Foster 	struct xfs_inode	*ip)
26851da177e4SLinus Torvalds {
26861da177e4SLinus Torvalds 	int			error;
268709b56604SBrian Foster 	struct xfs_icluster	xic = { 0 };
26881319ebefSDave Chinner 	struct xfs_inode_log_item *iip = ip->i_itemp;
26891da177e4SLinus Torvalds 
2690579aa9caSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
269154d7b5c1SDave Chinner 	ASSERT(VFS_I(ip)->i_nlink == 0);
2692daf83964SChristoph Hellwig 	ASSERT(ip->i_df.if_nextents == 0);
2693c19b3b05SDave Chinner 	ASSERT(ip->i_d.di_size == 0 || !S_ISREG(VFS_I(ip)->i_mode));
26941da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_nblocks == 0);
26951da177e4SLinus Torvalds 
26961da177e4SLinus Torvalds 	/*
26971da177e4SLinus Torvalds 	 * Pull the on-disk inode from the AGI unlinked list.
26981da177e4SLinus Torvalds 	 */
26991da177e4SLinus Torvalds 	error = xfs_iunlink_remove(tp, ip);
27001baaed8fSDave Chinner 	if (error)
27011da177e4SLinus Torvalds 		return error;
27021da177e4SLinus Torvalds 
27030e0417f3SBrian Foster 	error = xfs_difree(tp, ip->i_ino, &xic);
27041baaed8fSDave Chinner 	if (error)
27051da177e4SLinus Torvalds 		return error;
27061baaed8fSDave Chinner 
2707b2c20045SChristoph Hellwig 	/*
2708b2c20045SChristoph Hellwig 	 * Free any local-format data sitting around before we reset the
2709b2c20045SChristoph Hellwig 	 * data fork to extents format.  Note that the attr fork data has
2710b2c20045SChristoph Hellwig 	 * already been freed by xfs_attr_inactive.
2711b2c20045SChristoph Hellwig 	 */
2712f7e67b20SChristoph Hellwig 	if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
2713b2c20045SChristoph Hellwig 		kmem_free(ip->i_df.if_u1.if_data);
2714b2c20045SChristoph Hellwig 		ip->i_df.if_u1.if_data = NULL;
2715b2c20045SChristoph Hellwig 		ip->i_df.if_bytes = 0;
2716b2c20045SChristoph Hellwig 	}
271798c4f78dSDarrick J. Wong 
2718c19b3b05SDave Chinner 	VFS_I(ip)->i_mode = 0;		/* mark incore inode as free */
27191da177e4SLinus Torvalds 	ip->i_d.di_flags = 0;
2720beaae8cdSDarrick J. Wong 	ip->i_d.di_flags2 = 0;
27211da177e4SLinus Torvalds 	ip->i_d.di_dmevmask = 0;
27221da177e4SLinus Torvalds 	ip->i_d.di_forkoff = 0;		/* mark the attr fork not in use */
2723f7e67b20SChristoph Hellwig 	ip->i_df.if_format = XFS_DINODE_FMT_EXTENTS;
2724dc1baa71SEric Sandeen 
2725dc1baa71SEric Sandeen 	/* Don't attempt to replay owner changes for a deleted inode */
27261319ebefSDave Chinner 	spin_lock(&iip->ili_lock);
27271319ebefSDave Chinner 	iip->ili_fields &= ~(XFS_ILOG_AOWNER | XFS_ILOG_DOWNER);
27281319ebefSDave Chinner 	spin_unlock(&iip->ili_lock);
2729dc1baa71SEric Sandeen 
27301da177e4SLinus Torvalds 	/*
27311da177e4SLinus Torvalds 	 * Bump the generation count so no one will be confused
27321da177e4SLinus Torvalds 	 * by reincarnations of this inode.
27331da177e4SLinus Torvalds 	 */
27349e9a2674SDave Chinner 	VFS_I(ip)->i_generation++;
27351da177e4SLinus Torvalds 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
27361da177e4SLinus Torvalds 
273709b56604SBrian Foster 	if (xic.deleted)
273809b56604SBrian Foster 		error = xfs_ifree_cluster(ip, tp, &xic);
27391da177e4SLinus Torvalds 
27402a30f36dSChandra Seetharaman 	return error;
27411da177e4SLinus Torvalds }
27421da177e4SLinus Torvalds 
27431da177e4SLinus Torvalds /*
274460ec6783SChristoph Hellwig  * This is called to unpin an inode.  The caller must have the inode locked
274560ec6783SChristoph Hellwig  * in at least shared mode so that the buffer cannot be subsequently pinned
274660ec6783SChristoph Hellwig  * once someone is waiting for it to be unpinned.
27471da177e4SLinus Torvalds  */
274860ec6783SChristoph Hellwig static void
2749f392e631SChristoph Hellwig xfs_iunpin(
275060ec6783SChristoph Hellwig 	struct xfs_inode	*ip)
2751a3f74ffbSDavid Chinner {
2752579aa9caSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
2753a3f74ffbSDavid Chinner 
27544aaf15d1SDave Chinner 	trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
27554aaf15d1SDave Chinner 
2756a3f74ffbSDavid Chinner 	/* Give the log a push to start the unpinning I/O */
2757656de4ffSChristoph Hellwig 	xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0, NULL);
2758a14a348bSChristoph Hellwig 
2759a3f74ffbSDavid Chinner }
2760a3f74ffbSDavid Chinner 
2761f392e631SChristoph Hellwig static void
2762f392e631SChristoph Hellwig __xfs_iunpin_wait(
2763f392e631SChristoph Hellwig 	struct xfs_inode	*ip)
2764f392e631SChristoph Hellwig {
2765f392e631SChristoph Hellwig 	wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
2766f392e631SChristoph Hellwig 	DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
2767f392e631SChristoph Hellwig 
2768f392e631SChristoph Hellwig 	xfs_iunpin(ip);
2769f392e631SChristoph Hellwig 
2770f392e631SChristoph Hellwig 	do {
277121417136SIngo Molnar 		prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
2772f392e631SChristoph Hellwig 		if (xfs_ipincount(ip))
2773f392e631SChristoph Hellwig 			io_schedule();
2774f392e631SChristoph Hellwig 	} while (xfs_ipincount(ip));
277521417136SIngo Molnar 	finish_wait(wq, &wait.wq_entry);
2776f392e631SChristoph Hellwig }
2777f392e631SChristoph Hellwig 
2778777df5afSDave Chinner void
27791da177e4SLinus Torvalds xfs_iunpin_wait(
278060ec6783SChristoph Hellwig 	struct xfs_inode	*ip)
27811da177e4SLinus Torvalds {
2782f392e631SChristoph Hellwig 	if (xfs_ipincount(ip))
2783f392e631SChristoph Hellwig 		__xfs_iunpin_wait(ip);
27841da177e4SLinus Torvalds }
27851da177e4SLinus Torvalds 
278627320369SDave Chinner /*
278727320369SDave Chinner  * Removing an inode from the namespace involves removing the directory entry
278827320369SDave Chinner  * and dropping the link count on the inode. Removing the directory entry can
278927320369SDave Chinner  * result in locking an AGF (directory blocks were freed) and removing a link
279027320369SDave Chinner  * count can result in placing the inode on an unlinked list which results in
279127320369SDave Chinner  * locking an AGI.
279227320369SDave Chinner  *
279327320369SDave Chinner  * The big problem here is that we have an ordering constraint on AGF and AGI
279427320369SDave Chinner  * locking - inode allocation locks the AGI, then can allocate a new extent for
279527320369SDave Chinner  * new inodes, locking the AGF after the AGI. Similarly, freeing the inode
279627320369SDave Chinner  * removes the inode from the unlinked list, requiring that we lock the AGI
279727320369SDave Chinner  * first, and then freeing the inode can result in an inode chunk being freed
279827320369SDave Chinner  * and hence freeing disk space requiring that we lock an AGF.
279927320369SDave Chinner  *
280027320369SDave Chinner  * Hence the ordering that is imposed by other parts of the code is AGI before
280127320369SDave Chinner  * AGF. This means we cannot remove the directory entry before we drop the inode
280227320369SDave Chinner  * reference count and put it on the unlinked list as this results in a lock
280327320369SDave Chinner  * order of AGF then AGI, and this can deadlock against inode allocation and
280427320369SDave Chinner  * freeing. Therefore we must drop the link counts before we remove the
280527320369SDave Chinner  * directory entry.
280627320369SDave Chinner  *
280727320369SDave Chinner  * This is still safe from a transactional point of view - it is not until we
2808310a75a3SDarrick J. Wong  * get to xfs_defer_finish() that we have the possibility of multiple
280927320369SDave Chinner  * transactions in this operation. Hence as long as we remove the directory
281027320369SDave Chinner  * entry and drop the link count in the first transaction of the remove
281127320369SDave Chinner  * operation, there are no transactional constraints on the ordering here.
281227320369SDave Chinner  */
2813c24b5dfaSDave Chinner int
2814c24b5dfaSDave Chinner xfs_remove(
2815c24b5dfaSDave Chinner 	xfs_inode_t             *dp,
2816c24b5dfaSDave Chinner 	struct xfs_name		*name,
2817c24b5dfaSDave Chinner 	xfs_inode_t		*ip)
2818c24b5dfaSDave Chinner {
2819c24b5dfaSDave Chinner 	xfs_mount_t		*mp = dp->i_mount;
2820c24b5dfaSDave Chinner 	xfs_trans_t             *tp = NULL;
2821c19b3b05SDave Chinner 	int			is_dir = S_ISDIR(VFS_I(ip)->i_mode);
2822c24b5dfaSDave Chinner 	int                     error = 0;
2823c24b5dfaSDave Chinner 	uint			resblks;
2824c24b5dfaSDave Chinner 
2825c24b5dfaSDave Chinner 	trace_xfs_remove(dp, name);
2826c24b5dfaSDave Chinner 
2827c24b5dfaSDave Chinner 	if (XFS_FORCED_SHUTDOWN(mp))
28282451337dSDave Chinner 		return -EIO;
2829c24b5dfaSDave Chinner 
2830c14cfccaSDarrick J. Wong 	error = xfs_qm_dqattach(dp);
2831c24b5dfaSDave Chinner 	if (error)
2832c24b5dfaSDave Chinner 		goto std_return;
2833c24b5dfaSDave Chinner 
2834c14cfccaSDarrick J. Wong 	error = xfs_qm_dqattach(ip);
2835c24b5dfaSDave Chinner 	if (error)
2836c24b5dfaSDave Chinner 		goto std_return;
2837c24b5dfaSDave Chinner 
2838c24b5dfaSDave Chinner 	/*
2839c24b5dfaSDave Chinner 	 * We try to get the real space reservation first,
2840c24b5dfaSDave Chinner 	 * allowing for directory btree deletion(s) implying
2841c24b5dfaSDave Chinner 	 * possible bmap insert(s).  If we can't get the space
2842c24b5dfaSDave Chinner 	 * reservation then we use 0 instead, and avoid the bmap
2843c24b5dfaSDave Chinner 	 * btree insert(s) in the directory code by, if the bmap
2844c24b5dfaSDave Chinner 	 * insert tries to happen, instead trimming the LAST
2845c24b5dfaSDave Chinner 	 * block from the directory.
2846c24b5dfaSDave Chinner 	 */
2847c24b5dfaSDave Chinner 	resblks = XFS_REMOVE_SPACE_RES(mp);
2848253f4911SChristoph Hellwig 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_remove, resblks, 0, 0, &tp);
28492451337dSDave Chinner 	if (error == -ENOSPC) {
2850c24b5dfaSDave Chinner 		resblks = 0;
2851253f4911SChristoph Hellwig 		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_remove, 0, 0, 0,
2852253f4911SChristoph Hellwig 				&tp);
2853c24b5dfaSDave Chinner 	}
2854c24b5dfaSDave Chinner 	if (error) {
28552451337dSDave Chinner 		ASSERT(error != -ENOSPC);
2856253f4911SChristoph Hellwig 		goto std_return;
2857c24b5dfaSDave Chinner 	}
2858c24b5dfaSDave Chinner 
28597c2d238aSDarrick J. Wong 	xfs_lock_two_inodes(dp, XFS_ILOCK_EXCL, ip, XFS_ILOCK_EXCL);
2860c24b5dfaSDave Chinner 
286165523218SChristoph Hellwig 	xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2862c24b5dfaSDave Chinner 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2863c24b5dfaSDave Chinner 
2864c24b5dfaSDave Chinner 	/*
2865c24b5dfaSDave Chinner 	 * If we're removing a directory perform some additional validation.
2866c24b5dfaSDave Chinner 	 */
2867c24b5dfaSDave Chinner 	if (is_dir) {
286854d7b5c1SDave Chinner 		ASSERT(VFS_I(ip)->i_nlink >= 2);
286954d7b5c1SDave Chinner 		if (VFS_I(ip)->i_nlink != 2) {
28702451337dSDave Chinner 			error = -ENOTEMPTY;
2871c24b5dfaSDave Chinner 			goto out_trans_cancel;
2872c24b5dfaSDave Chinner 		}
2873c24b5dfaSDave Chinner 		if (!xfs_dir_isempty(ip)) {
28742451337dSDave Chinner 			error = -ENOTEMPTY;
2875c24b5dfaSDave Chinner 			goto out_trans_cancel;
2876c24b5dfaSDave Chinner 		}
2877c24b5dfaSDave Chinner 
287827320369SDave Chinner 		/* Drop the link from ip's "..".  */
2879c24b5dfaSDave Chinner 		error = xfs_droplink(tp, dp);
2880c24b5dfaSDave Chinner 		if (error)
288127320369SDave Chinner 			goto out_trans_cancel;
2882c24b5dfaSDave Chinner 
288327320369SDave Chinner 		/* Drop the "." link from ip to self.  */
2884c24b5dfaSDave Chinner 		error = xfs_droplink(tp, ip);
2885c24b5dfaSDave Chinner 		if (error)
288627320369SDave Chinner 			goto out_trans_cancel;
2887c24b5dfaSDave Chinner 	} else {
2888c24b5dfaSDave Chinner 		/*
2889c24b5dfaSDave Chinner 		 * When removing a non-directory we need to log the parent
2890c24b5dfaSDave Chinner 		 * inode here.  For a directory this is done implicitly
2891c24b5dfaSDave Chinner 		 * by the xfs_droplink call for the ".." entry.
2892c24b5dfaSDave Chinner 		 */
2893c24b5dfaSDave Chinner 		xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2894c24b5dfaSDave Chinner 	}
289527320369SDave Chinner 	xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2896c24b5dfaSDave Chinner 
289727320369SDave Chinner 	/* Drop the link from dp to ip. */
2898c24b5dfaSDave Chinner 	error = xfs_droplink(tp, ip);
2899c24b5dfaSDave Chinner 	if (error)
290027320369SDave Chinner 		goto out_trans_cancel;
2901c24b5dfaSDave Chinner 
2902381eee69SBrian Foster 	error = xfs_dir_removename(tp, dp, name, ip->i_ino, resblks);
290327320369SDave Chinner 	if (error) {
29042451337dSDave Chinner 		ASSERT(error != -ENOENT);
2905c8eac49eSBrian Foster 		goto out_trans_cancel;
290627320369SDave Chinner 	}
290727320369SDave Chinner 
2908c24b5dfaSDave Chinner 	/*
2909c24b5dfaSDave Chinner 	 * If this is a synchronous mount, make sure that the
2910c24b5dfaSDave Chinner 	 * remove transaction goes to disk before returning to
2911c24b5dfaSDave Chinner 	 * the user.
2912c24b5dfaSDave Chinner 	 */
2913c24b5dfaSDave Chinner 	if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
2914c24b5dfaSDave Chinner 		xfs_trans_set_sync(tp);
2915c24b5dfaSDave Chinner 
291670393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
2917c24b5dfaSDave Chinner 	if (error)
2918c24b5dfaSDave Chinner 		goto std_return;
2919c24b5dfaSDave Chinner 
29202cd2ef6aSChristoph Hellwig 	if (is_dir && xfs_inode_is_filestream(ip))
2921c24b5dfaSDave Chinner 		xfs_filestream_deassociate(ip);
2922c24b5dfaSDave Chinner 
2923c24b5dfaSDave Chinner 	return 0;
2924c24b5dfaSDave Chinner 
2925c24b5dfaSDave Chinner  out_trans_cancel:
29264906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
2927c24b5dfaSDave Chinner  std_return:
2928c24b5dfaSDave Chinner 	return error;
2929c24b5dfaSDave Chinner }
2930c24b5dfaSDave Chinner 
2931f6bba201SDave Chinner /*
2932f6bba201SDave Chinner  * Enter all inodes for a rename transaction into a sorted array.
2933f6bba201SDave Chinner  */
293495afcf5cSDave Chinner #define __XFS_SORT_INODES	5
2935f6bba201SDave Chinner STATIC void
2936f6bba201SDave Chinner xfs_sort_for_rename(
293795afcf5cSDave Chinner 	struct xfs_inode	*dp1,	/* in: old (source) directory inode */
293895afcf5cSDave Chinner 	struct xfs_inode	*dp2,	/* in: new (target) directory inode */
293995afcf5cSDave Chinner 	struct xfs_inode	*ip1,	/* in: inode of old entry */
294095afcf5cSDave Chinner 	struct xfs_inode	*ip2,	/* in: inode of new entry */
294195afcf5cSDave Chinner 	struct xfs_inode	*wip,	/* in: whiteout inode */
294295afcf5cSDave Chinner 	struct xfs_inode	**i_tab,/* out: sorted array of inodes */
294395afcf5cSDave Chinner 	int			*num_inodes)  /* in/out: inodes in array */
2944f6bba201SDave Chinner {
2945f6bba201SDave Chinner 	int			i, j;
2946f6bba201SDave Chinner 
294795afcf5cSDave Chinner 	ASSERT(*num_inodes == __XFS_SORT_INODES);
294895afcf5cSDave Chinner 	memset(i_tab, 0, *num_inodes * sizeof(struct xfs_inode *));
294995afcf5cSDave Chinner 
2950f6bba201SDave Chinner 	/*
2951f6bba201SDave Chinner 	 * i_tab contains a list of pointers to inodes.  We initialize
2952f6bba201SDave Chinner 	 * the table here & we'll sort it.  We will then use it to
2953f6bba201SDave Chinner 	 * order the acquisition of the inode locks.
2954f6bba201SDave Chinner 	 *
2955f6bba201SDave Chinner 	 * Note that the table may contain duplicates.  e.g., dp1 == dp2.
2956f6bba201SDave Chinner 	 */
295795afcf5cSDave Chinner 	i = 0;
295895afcf5cSDave Chinner 	i_tab[i++] = dp1;
295995afcf5cSDave Chinner 	i_tab[i++] = dp2;
296095afcf5cSDave Chinner 	i_tab[i++] = ip1;
296195afcf5cSDave Chinner 	if (ip2)
296295afcf5cSDave Chinner 		i_tab[i++] = ip2;
296395afcf5cSDave Chinner 	if (wip)
296495afcf5cSDave Chinner 		i_tab[i++] = wip;
296595afcf5cSDave Chinner 	*num_inodes = i;
2966f6bba201SDave Chinner 
2967f6bba201SDave Chinner 	/*
2968f6bba201SDave Chinner 	 * Sort the elements via bubble sort.  (Remember, there are at
296995afcf5cSDave Chinner 	 * most 5 elements to sort, so this is adequate.)
2970f6bba201SDave Chinner 	 */
2971f6bba201SDave Chinner 	for (i = 0; i < *num_inodes; i++) {
2972f6bba201SDave Chinner 		for (j = 1; j < *num_inodes; j++) {
2973f6bba201SDave Chinner 			if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) {
297495afcf5cSDave Chinner 				struct xfs_inode *temp = i_tab[j];
2975f6bba201SDave Chinner 				i_tab[j] = i_tab[j-1];
2976f6bba201SDave Chinner 				i_tab[j-1] = temp;
2977f6bba201SDave Chinner 			}
2978f6bba201SDave Chinner 		}
2979f6bba201SDave Chinner 	}
2980f6bba201SDave Chinner }
2981f6bba201SDave Chinner 
2982310606b0SDave Chinner static int
2983310606b0SDave Chinner xfs_finish_rename(
2984c9cfdb38SBrian Foster 	struct xfs_trans	*tp)
2985310606b0SDave Chinner {
2986310606b0SDave Chinner 	/*
2987310606b0SDave Chinner 	 * If this is a synchronous mount, make sure that the rename transaction
2988310606b0SDave Chinner 	 * goes to disk before returning to the user.
2989310606b0SDave Chinner 	 */
2990310606b0SDave Chinner 	if (tp->t_mountp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
2991310606b0SDave Chinner 		xfs_trans_set_sync(tp);
2992310606b0SDave Chinner 
299370393313SChristoph Hellwig 	return xfs_trans_commit(tp);
2994310606b0SDave Chinner }
2995310606b0SDave Chinner 
2996f6bba201SDave Chinner /*
2997d31a1825SCarlos Maiolino  * xfs_cross_rename()
2998d31a1825SCarlos Maiolino  *
2999d31a1825SCarlos Maiolino  * responsible for handling RENAME_EXCHANGE flag in renameat2() sytemcall
3000d31a1825SCarlos Maiolino  */
3001d31a1825SCarlos Maiolino STATIC int
3002d31a1825SCarlos Maiolino xfs_cross_rename(
3003d31a1825SCarlos Maiolino 	struct xfs_trans	*tp,
3004d31a1825SCarlos Maiolino 	struct xfs_inode	*dp1,
3005d31a1825SCarlos Maiolino 	struct xfs_name		*name1,
3006d31a1825SCarlos Maiolino 	struct xfs_inode	*ip1,
3007d31a1825SCarlos Maiolino 	struct xfs_inode	*dp2,
3008d31a1825SCarlos Maiolino 	struct xfs_name		*name2,
3009d31a1825SCarlos Maiolino 	struct xfs_inode	*ip2,
3010d31a1825SCarlos Maiolino 	int			spaceres)
3011d31a1825SCarlos Maiolino {
3012d31a1825SCarlos Maiolino 	int		error = 0;
3013d31a1825SCarlos Maiolino 	int		ip1_flags = 0;
3014d31a1825SCarlos Maiolino 	int		ip2_flags = 0;
3015d31a1825SCarlos Maiolino 	int		dp2_flags = 0;
3016d31a1825SCarlos Maiolino 
3017d31a1825SCarlos Maiolino 	/* Swap inode number for dirent in first parent */
3018381eee69SBrian Foster 	error = xfs_dir_replace(tp, dp1, name1, ip2->i_ino, spaceres);
3019d31a1825SCarlos Maiolino 	if (error)
3020eeacd321SDave Chinner 		goto out_trans_abort;
3021d31a1825SCarlos Maiolino 
3022d31a1825SCarlos Maiolino 	/* Swap inode number for dirent in second parent */
3023381eee69SBrian Foster 	error = xfs_dir_replace(tp, dp2, name2, ip1->i_ino, spaceres);
3024d31a1825SCarlos Maiolino 	if (error)
3025eeacd321SDave Chinner 		goto out_trans_abort;
3026d31a1825SCarlos Maiolino 
3027d31a1825SCarlos Maiolino 	/*
3028d31a1825SCarlos Maiolino 	 * If we're renaming one or more directories across different parents,
3029d31a1825SCarlos Maiolino 	 * update the respective ".." entries (and link counts) to match the new
3030d31a1825SCarlos Maiolino 	 * parents.
3031d31a1825SCarlos Maiolino 	 */
3032d31a1825SCarlos Maiolino 	if (dp1 != dp2) {
3033d31a1825SCarlos Maiolino 		dp2_flags = XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
3034d31a1825SCarlos Maiolino 
3035c19b3b05SDave Chinner 		if (S_ISDIR(VFS_I(ip2)->i_mode)) {
3036d31a1825SCarlos Maiolino 			error = xfs_dir_replace(tp, ip2, &xfs_name_dotdot,
3037381eee69SBrian Foster 						dp1->i_ino, spaceres);
3038d31a1825SCarlos Maiolino 			if (error)
3039eeacd321SDave Chinner 				goto out_trans_abort;
3040d31a1825SCarlos Maiolino 
3041d31a1825SCarlos Maiolino 			/* transfer ip2 ".." reference to dp1 */
3042c19b3b05SDave Chinner 			if (!S_ISDIR(VFS_I(ip1)->i_mode)) {
3043d31a1825SCarlos Maiolino 				error = xfs_droplink(tp, dp2);
3044d31a1825SCarlos Maiolino 				if (error)
3045eeacd321SDave Chinner 					goto out_trans_abort;
304691083269SEric Sandeen 				xfs_bumplink(tp, dp1);
3047d31a1825SCarlos Maiolino 			}
3048d31a1825SCarlos Maiolino 
3049d31a1825SCarlos Maiolino 			/*
3050d31a1825SCarlos Maiolino 			 * Although ip1 isn't changed here, userspace needs
3051d31a1825SCarlos Maiolino 			 * to be warned about the change, so that applications
3052d31a1825SCarlos Maiolino 			 * relying on it (like backup ones), will properly
3053d31a1825SCarlos Maiolino 			 * notify the change
3054d31a1825SCarlos Maiolino 			 */
3055d31a1825SCarlos Maiolino 			ip1_flags |= XFS_ICHGTIME_CHG;
3056d31a1825SCarlos Maiolino 			ip2_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
3057d31a1825SCarlos Maiolino 		}
3058d31a1825SCarlos Maiolino 
3059c19b3b05SDave Chinner 		if (S_ISDIR(VFS_I(ip1)->i_mode)) {
3060d31a1825SCarlos Maiolino 			error = xfs_dir_replace(tp, ip1, &xfs_name_dotdot,
3061381eee69SBrian Foster 						dp2->i_ino, spaceres);
3062d31a1825SCarlos Maiolino 			if (error)
3063eeacd321SDave Chinner 				goto out_trans_abort;
3064d31a1825SCarlos Maiolino 
3065d31a1825SCarlos Maiolino 			/* transfer ip1 ".." reference to dp2 */
3066c19b3b05SDave Chinner 			if (!S_ISDIR(VFS_I(ip2)->i_mode)) {
3067d31a1825SCarlos Maiolino 				error = xfs_droplink(tp, dp1);
3068d31a1825SCarlos Maiolino 				if (error)
3069eeacd321SDave Chinner 					goto out_trans_abort;
307091083269SEric Sandeen 				xfs_bumplink(tp, dp2);
3071d31a1825SCarlos Maiolino 			}
3072d31a1825SCarlos Maiolino 
3073d31a1825SCarlos Maiolino 			/*
3074d31a1825SCarlos Maiolino 			 * Although ip2 isn't changed here, userspace needs
3075d31a1825SCarlos Maiolino 			 * to be warned about the change, so that applications
3076d31a1825SCarlos Maiolino 			 * relying on it (like backup ones), will properly
3077d31a1825SCarlos Maiolino 			 * notify the change
3078d31a1825SCarlos Maiolino 			 */
3079d31a1825SCarlos Maiolino 			ip1_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
3080d31a1825SCarlos Maiolino 			ip2_flags |= XFS_ICHGTIME_CHG;
3081d31a1825SCarlos Maiolino 		}
3082d31a1825SCarlos Maiolino 	}
3083d31a1825SCarlos Maiolino 
3084d31a1825SCarlos Maiolino 	if (ip1_flags) {
3085d31a1825SCarlos Maiolino 		xfs_trans_ichgtime(tp, ip1, ip1_flags);
3086d31a1825SCarlos Maiolino 		xfs_trans_log_inode(tp, ip1, XFS_ILOG_CORE);
3087d31a1825SCarlos Maiolino 	}
3088d31a1825SCarlos Maiolino 	if (ip2_flags) {
3089d31a1825SCarlos Maiolino 		xfs_trans_ichgtime(tp, ip2, ip2_flags);
3090d31a1825SCarlos Maiolino 		xfs_trans_log_inode(tp, ip2, XFS_ILOG_CORE);
3091d31a1825SCarlos Maiolino 	}
3092d31a1825SCarlos Maiolino 	if (dp2_flags) {
3093d31a1825SCarlos Maiolino 		xfs_trans_ichgtime(tp, dp2, dp2_flags);
3094d31a1825SCarlos Maiolino 		xfs_trans_log_inode(tp, dp2, XFS_ILOG_CORE);
3095d31a1825SCarlos Maiolino 	}
3096d31a1825SCarlos Maiolino 	xfs_trans_ichgtime(tp, dp1, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3097d31a1825SCarlos Maiolino 	xfs_trans_log_inode(tp, dp1, XFS_ILOG_CORE);
3098c9cfdb38SBrian Foster 	return xfs_finish_rename(tp);
3099eeacd321SDave Chinner 
3100eeacd321SDave Chinner out_trans_abort:
31014906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
3102d31a1825SCarlos Maiolino 	return error;
3103d31a1825SCarlos Maiolino }
3104d31a1825SCarlos Maiolino 
3105d31a1825SCarlos Maiolino /*
31067dcf5c3eSDave Chinner  * xfs_rename_alloc_whiteout()
31077dcf5c3eSDave Chinner  *
3108*b63da6c8SRandy Dunlap  * Return a referenced, unlinked, unlocked inode that can be used as a
31097dcf5c3eSDave Chinner  * whiteout in a rename transaction. We use a tmpfile inode here so that if we
31107dcf5c3eSDave Chinner  * crash between allocating the inode and linking it into the rename transaction
31117dcf5c3eSDave Chinner  * recovery will free the inode and we won't leak it.
31127dcf5c3eSDave Chinner  */
31137dcf5c3eSDave Chinner static int
31147dcf5c3eSDave Chinner xfs_rename_alloc_whiteout(
31157dcf5c3eSDave Chinner 	struct xfs_inode	*dp,
31167dcf5c3eSDave Chinner 	struct xfs_inode	**wip)
31177dcf5c3eSDave Chinner {
31187dcf5c3eSDave Chinner 	struct xfs_inode	*tmpfile;
31197dcf5c3eSDave Chinner 	int			error;
31207dcf5c3eSDave Chinner 
3121a1f69417SEric Sandeen 	error = xfs_create_tmpfile(dp, S_IFCHR | WHITEOUT_MODE, &tmpfile);
31227dcf5c3eSDave Chinner 	if (error)
31237dcf5c3eSDave Chinner 		return error;
31247dcf5c3eSDave Chinner 
312522419ac9SBrian Foster 	/*
312622419ac9SBrian Foster 	 * Prepare the tmpfile inode as if it were created through the VFS.
3127c4a6bf7fSDarrick J. Wong 	 * Complete the inode setup and flag it as linkable.  nlink is already
3128c4a6bf7fSDarrick J. Wong 	 * zero, so we can skip the drop_nlink.
312922419ac9SBrian Foster 	 */
31302b3d1d41SChristoph Hellwig 	xfs_setup_iops(tmpfile);
31317dcf5c3eSDave Chinner 	xfs_finish_inode_setup(tmpfile);
31327dcf5c3eSDave Chinner 	VFS_I(tmpfile)->i_state |= I_LINKABLE;
31337dcf5c3eSDave Chinner 
31347dcf5c3eSDave Chinner 	*wip = tmpfile;
31357dcf5c3eSDave Chinner 	return 0;
31367dcf5c3eSDave Chinner }
31377dcf5c3eSDave Chinner 
31387dcf5c3eSDave Chinner /*
3139f6bba201SDave Chinner  * xfs_rename
3140f6bba201SDave Chinner  */
3141f6bba201SDave Chinner int
3142f6bba201SDave Chinner xfs_rename(
31437dcf5c3eSDave Chinner 	struct xfs_inode	*src_dp,
3144f6bba201SDave Chinner 	struct xfs_name		*src_name,
31457dcf5c3eSDave Chinner 	struct xfs_inode	*src_ip,
31467dcf5c3eSDave Chinner 	struct xfs_inode	*target_dp,
3147f6bba201SDave Chinner 	struct xfs_name		*target_name,
31487dcf5c3eSDave Chinner 	struct xfs_inode	*target_ip,
3149d31a1825SCarlos Maiolino 	unsigned int		flags)
3150f6bba201SDave Chinner {
31517dcf5c3eSDave Chinner 	struct xfs_mount	*mp = src_dp->i_mount;
31527dcf5c3eSDave Chinner 	struct xfs_trans	*tp;
31537dcf5c3eSDave Chinner 	struct xfs_inode	*wip = NULL;		/* whiteout inode */
31547dcf5c3eSDave Chinner 	struct xfs_inode	*inodes[__XFS_SORT_INODES];
315593597ae8Skaixuxia 	struct xfs_buf		*agibp;
315695afcf5cSDave Chinner 	int			num_inodes = __XFS_SORT_INODES;
31572b93681fSDave Chinner 	bool			new_parent = (src_dp != target_dp);
3158c19b3b05SDave Chinner 	bool			src_is_directory = S_ISDIR(VFS_I(src_ip)->i_mode);
3159f6bba201SDave Chinner 	int			spaceres;
31607dcf5c3eSDave Chinner 	int			error;
3161f6bba201SDave Chinner 
3162f6bba201SDave Chinner 	trace_xfs_rename(src_dp, target_dp, src_name, target_name);
3163f6bba201SDave Chinner 
3164eeacd321SDave Chinner 	if ((flags & RENAME_EXCHANGE) && !target_ip)
3165eeacd321SDave Chinner 		return -EINVAL;
3166f6bba201SDave Chinner 
31677dcf5c3eSDave Chinner 	/*
31687dcf5c3eSDave Chinner 	 * If we are doing a whiteout operation, allocate the whiteout inode
31697dcf5c3eSDave Chinner 	 * we will be placing at the target and ensure the type is set
31707dcf5c3eSDave Chinner 	 * appropriately.
31717dcf5c3eSDave Chinner 	 */
31727dcf5c3eSDave Chinner 	if (flags & RENAME_WHITEOUT) {
31737dcf5c3eSDave Chinner 		ASSERT(!(flags & (RENAME_NOREPLACE | RENAME_EXCHANGE)));
31747dcf5c3eSDave Chinner 		error = xfs_rename_alloc_whiteout(target_dp, &wip);
31757dcf5c3eSDave Chinner 		if (error)
31767dcf5c3eSDave Chinner 			return error;
3177f6bba201SDave Chinner 
31787dcf5c3eSDave Chinner 		/* setup target dirent info as whiteout */
31797dcf5c3eSDave Chinner 		src_name->type = XFS_DIR3_FT_CHRDEV;
31807dcf5c3eSDave Chinner 	}
31817dcf5c3eSDave Chinner 
31827dcf5c3eSDave Chinner 	xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip, wip,
3183f6bba201SDave Chinner 				inodes, &num_inodes);
3184f6bba201SDave Chinner 
3185f6bba201SDave Chinner 	spaceres = XFS_RENAME_SPACE_RES(mp, target_name->len);
3186253f4911SChristoph Hellwig 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_rename, spaceres, 0, 0, &tp);
31872451337dSDave Chinner 	if (error == -ENOSPC) {
3188f6bba201SDave Chinner 		spaceres = 0;
3189253f4911SChristoph Hellwig 		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_rename, 0, 0, 0,
3190253f4911SChristoph Hellwig 				&tp);
3191f6bba201SDave Chinner 	}
3192445883e8SDave Chinner 	if (error)
3193253f4911SChristoph Hellwig 		goto out_release_wip;
3194f6bba201SDave Chinner 
3195f6bba201SDave Chinner 	/*
3196f6bba201SDave Chinner 	 * Attach the dquots to the inodes
3197f6bba201SDave Chinner 	 */
3198f6bba201SDave Chinner 	error = xfs_qm_vop_rename_dqattach(inodes);
3199445883e8SDave Chinner 	if (error)
3200445883e8SDave Chinner 		goto out_trans_cancel;
3201f6bba201SDave Chinner 
3202f6bba201SDave Chinner 	/*
3203f6bba201SDave Chinner 	 * Lock all the participating inodes. Depending upon whether
3204f6bba201SDave Chinner 	 * the target_name exists in the target directory, and
3205f6bba201SDave Chinner 	 * whether the target directory is the same as the source
3206f6bba201SDave Chinner 	 * directory, we can lock from 2 to 4 inodes.
3207f6bba201SDave Chinner 	 */
3208f6bba201SDave Chinner 	xfs_lock_inodes(inodes, num_inodes, XFS_ILOCK_EXCL);
3209f6bba201SDave Chinner 
3210f6bba201SDave Chinner 	/*
3211f6bba201SDave Chinner 	 * Join all the inodes to the transaction. From this point on,
3212f6bba201SDave Chinner 	 * we can rely on either trans_commit or trans_cancel to unlock
3213f6bba201SDave Chinner 	 * them.
3214f6bba201SDave Chinner 	 */
321565523218SChristoph Hellwig 	xfs_trans_ijoin(tp, src_dp, XFS_ILOCK_EXCL);
3216f6bba201SDave Chinner 	if (new_parent)
321765523218SChristoph Hellwig 		xfs_trans_ijoin(tp, target_dp, XFS_ILOCK_EXCL);
3218f6bba201SDave Chinner 	xfs_trans_ijoin(tp, src_ip, XFS_ILOCK_EXCL);
3219f6bba201SDave Chinner 	if (target_ip)
3220f6bba201SDave Chinner 		xfs_trans_ijoin(tp, target_ip, XFS_ILOCK_EXCL);
32217dcf5c3eSDave Chinner 	if (wip)
32227dcf5c3eSDave Chinner 		xfs_trans_ijoin(tp, wip, XFS_ILOCK_EXCL);
3223f6bba201SDave Chinner 
3224f6bba201SDave Chinner 	/*
3225f6bba201SDave Chinner 	 * If we are using project inheritance, we only allow renames
3226f6bba201SDave Chinner 	 * into our tree when the project IDs are the same; else the
3227f6bba201SDave Chinner 	 * tree quota mechanism would be circumvented.
3228f6bba201SDave Chinner 	 */
3229f6bba201SDave Chinner 	if (unlikely((target_dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
3230de7a866fSChristoph Hellwig 		     target_dp->i_d.di_projid != src_ip->i_d.di_projid)) {
32312451337dSDave Chinner 		error = -EXDEV;
3232445883e8SDave Chinner 		goto out_trans_cancel;
3233f6bba201SDave Chinner 	}
3234f6bba201SDave Chinner 
3235eeacd321SDave Chinner 	/* RENAME_EXCHANGE is unique from here on. */
3236eeacd321SDave Chinner 	if (flags & RENAME_EXCHANGE)
3237eeacd321SDave Chinner 		return xfs_cross_rename(tp, src_dp, src_name, src_ip,
3238d31a1825SCarlos Maiolino 					target_dp, target_name, target_ip,
3239f16dea54SBrian Foster 					spaceres);
3240d31a1825SCarlos Maiolino 
3241d31a1825SCarlos Maiolino 	/*
3242bc56ad8cSkaixuxia 	 * Check for expected errors before we dirty the transaction
3243bc56ad8cSkaixuxia 	 * so we can return an error without a transaction abort.
3244f6bba201SDave Chinner 	 */
3245f6bba201SDave Chinner 	if (target_ip == NULL) {
3246f6bba201SDave Chinner 		/*
3247f6bba201SDave Chinner 		 * If there's no space reservation, check the entry will
3248f6bba201SDave Chinner 		 * fit before actually inserting it.
3249f6bba201SDave Chinner 		 */
325094f3cad5SEric Sandeen 		if (!spaceres) {
325194f3cad5SEric Sandeen 			error = xfs_dir_canenter(tp, target_dp, target_name);
3252f6bba201SDave Chinner 			if (error)
3253445883e8SDave Chinner 				goto out_trans_cancel;
325494f3cad5SEric Sandeen 		}
3255bc56ad8cSkaixuxia 	} else {
3256bc56ad8cSkaixuxia 		/*
3257bc56ad8cSkaixuxia 		 * If target exists and it's a directory, check that whether
3258bc56ad8cSkaixuxia 		 * it can be destroyed.
3259bc56ad8cSkaixuxia 		 */
3260bc56ad8cSkaixuxia 		if (S_ISDIR(VFS_I(target_ip)->i_mode) &&
3261bc56ad8cSkaixuxia 		    (!xfs_dir_isempty(target_ip) ||
3262bc56ad8cSkaixuxia 		     (VFS_I(target_ip)->i_nlink > 2))) {
3263bc56ad8cSkaixuxia 			error = -EEXIST;
3264bc56ad8cSkaixuxia 			goto out_trans_cancel;
3265bc56ad8cSkaixuxia 		}
3266bc56ad8cSkaixuxia 	}
3267bc56ad8cSkaixuxia 
3268bc56ad8cSkaixuxia 	/*
3269bc56ad8cSkaixuxia 	 * Directory entry creation below may acquire the AGF. Remove
3270bc56ad8cSkaixuxia 	 * the whiteout from the unlinked list first to preserve correct
3271bc56ad8cSkaixuxia 	 * AGI/AGF locking order. This dirties the transaction so failures
3272bc56ad8cSkaixuxia 	 * after this point will abort and log recovery will clean up the
3273bc56ad8cSkaixuxia 	 * mess.
3274bc56ad8cSkaixuxia 	 *
3275bc56ad8cSkaixuxia 	 * For whiteouts, we need to bump the link count on the whiteout
3276bc56ad8cSkaixuxia 	 * inode. After this point, we have a real link, clear the tmpfile
3277bc56ad8cSkaixuxia 	 * state flag from the inode so it doesn't accidentally get misused
3278bc56ad8cSkaixuxia 	 * in future.
3279bc56ad8cSkaixuxia 	 */
3280bc56ad8cSkaixuxia 	if (wip) {
3281bc56ad8cSkaixuxia 		ASSERT(VFS_I(wip)->i_nlink == 0);
3282bc56ad8cSkaixuxia 		error = xfs_iunlink_remove(tp, wip);
3283bc56ad8cSkaixuxia 		if (error)
3284bc56ad8cSkaixuxia 			goto out_trans_cancel;
3285bc56ad8cSkaixuxia 
3286bc56ad8cSkaixuxia 		xfs_bumplink(tp, wip);
3287bc56ad8cSkaixuxia 		VFS_I(wip)->i_state &= ~I_LINKABLE;
3288bc56ad8cSkaixuxia 	}
3289bc56ad8cSkaixuxia 
3290bc56ad8cSkaixuxia 	/*
3291bc56ad8cSkaixuxia 	 * Set up the target.
3292bc56ad8cSkaixuxia 	 */
3293bc56ad8cSkaixuxia 	if (target_ip == NULL) {
3294f6bba201SDave Chinner 		/*
3295f6bba201SDave Chinner 		 * If target does not exist and the rename crosses
3296f6bba201SDave Chinner 		 * directories, adjust the target directory link count
3297f6bba201SDave Chinner 		 * to account for the ".." reference from the new entry.
3298f6bba201SDave Chinner 		 */
3299f6bba201SDave Chinner 		error = xfs_dir_createname(tp, target_dp, target_name,
3300381eee69SBrian Foster 					   src_ip->i_ino, spaceres);
3301f6bba201SDave Chinner 		if (error)
3302c8eac49eSBrian Foster 			goto out_trans_cancel;
3303f6bba201SDave Chinner 
3304f6bba201SDave Chinner 		xfs_trans_ichgtime(tp, target_dp,
3305f6bba201SDave Chinner 					XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3306f6bba201SDave Chinner 
3307f6bba201SDave Chinner 		if (new_parent && src_is_directory) {
330891083269SEric Sandeen 			xfs_bumplink(tp, target_dp);
3309f6bba201SDave Chinner 		}
3310f6bba201SDave Chinner 	} else { /* target_ip != NULL */
3311f6bba201SDave Chinner 		/*
3312f6bba201SDave Chinner 		 * Link the source inode under the target name.
3313f6bba201SDave Chinner 		 * If the source inode is a directory and we are moving
3314f6bba201SDave Chinner 		 * it across directories, its ".." entry will be
3315f6bba201SDave Chinner 		 * inconsistent until we replace that down below.
3316f6bba201SDave Chinner 		 *
3317f6bba201SDave Chinner 		 * In case there is already an entry with the same
3318f6bba201SDave Chinner 		 * name at the destination directory, remove it first.
3319f6bba201SDave Chinner 		 */
332093597ae8Skaixuxia 
332193597ae8Skaixuxia 		/*
332293597ae8Skaixuxia 		 * Check whether the replace operation will need to allocate
332393597ae8Skaixuxia 		 * blocks.  This happens when the shortform directory lacks
332493597ae8Skaixuxia 		 * space and we have to convert it to a block format directory.
332593597ae8Skaixuxia 		 * When more blocks are necessary, we must lock the AGI first
332693597ae8Skaixuxia 		 * to preserve locking order (AGI -> AGF).
332793597ae8Skaixuxia 		 */
332893597ae8Skaixuxia 		if (xfs_dir2_sf_replace_needblock(target_dp, src_ip->i_ino)) {
332993597ae8Skaixuxia 			error = xfs_read_agi(mp, tp,
333093597ae8Skaixuxia 					XFS_INO_TO_AGNO(mp, target_ip->i_ino),
333193597ae8Skaixuxia 					&agibp);
333293597ae8Skaixuxia 			if (error)
333393597ae8Skaixuxia 				goto out_trans_cancel;
333493597ae8Skaixuxia 		}
333593597ae8Skaixuxia 
3336f6bba201SDave Chinner 		error = xfs_dir_replace(tp, target_dp, target_name,
3337381eee69SBrian Foster 					src_ip->i_ino, spaceres);
3338f6bba201SDave Chinner 		if (error)
3339c8eac49eSBrian Foster 			goto out_trans_cancel;
3340f6bba201SDave Chinner 
3341f6bba201SDave Chinner 		xfs_trans_ichgtime(tp, target_dp,
3342f6bba201SDave Chinner 					XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3343f6bba201SDave Chinner 
3344f6bba201SDave Chinner 		/*
3345f6bba201SDave Chinner 		 * Decrement the link count on the target since the target
3346f6bba201SDave Chinner 		 * dir no longer points to it.
3347f6bba201SDave Chinner 		 */
3348f6bba201SDave Chinner 		error = xfs_droplink(tp, target_ip);
3349f6bba201SDave Chinner 		if (error)
3350c8eac49eSBrian Foster 			goto out_trans_cancel;
3351f6bba201SDave Chinner 
3352f6bba201SDave Chinner 		if (src_is_directory) {
3353f6bba201SDave Chinner 			/*
3354f6bba201SDave Chinner 			 * Drop the link from the old "." entry.
3355f6bba201SDave Chinner 			 */
3356f6bba201SDave Chinner 			error = xfs_droplink(tp, target_ip);
3357f6bba201SDave Chinner 			if (error)
3358c8eac49eSBrian Foster 				goto out_trans_cancel;
3359f6bba201SDave Chinner 		}
3360f6bba201SDave Chinner 	} /* target_ip != NULL */
3361f6bba201SDave Chinner 
3362f6bba201SDave Chinner 	/*
3363f6bba201SDave Chinner 	 * Remove the source.
3364f6bba201SDave Chinner 	 */
3365f6bba201SDave Chinner 	if (new_parent && src_is_directory) {
3366f6bba201SDave Chinner 		/*
3367f6bba201SDave Chinner 		 * Rewrite the ".." entry to point to the new
3368f6bba201SDave Chinner 		 * directory.
3369f6bba201SDave Chinner 		 */
3370f6bba201SDave Chinner 		error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot,
3371381eee69SBrian Foster 					target_dp->i_ino, spaceres);
33722451337dSDave Chinner 		ASSERT(error != -EEXIST);
3373f6bba201SDave Chinner 		if (error)
3374c8eac49eSBrian Foster 			goto out_trans_cancel;
3375f6bba201SDave Chinner 	}
3376f6bba201SDave Chinner 
3377f6bba201SDave Chinner 	/*
3378f6bba201SDave Chinner 	 * We always want to hit the ctime on the source inode.
3379f6bba201SDave Chinner 	 *
3380f6bba201SDave Chinner 	 * This isn't strictly required by the standards since the source
3381f6bba201SDave Chinner 	 * inode isn't really being changed, but old unix file systems did
3382f6bba201SDave Chinner 	 * it and some incremental backup programs won't work without it.
3383f6bba201SDave Chinner 	 */
3384f6bba201SDave Chinner 	xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG);
3385f6bba201SDave Chinner 	xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE);
3386f6bba201SDave Chinner 
3387f6bba201SDave Chinner 	/*
3388f6bba201SDave Chinner 	 * Adjust the link count on src_dp.  This is necessary when
3389f6bba201SDave Chinner 	 * renaming a directory, either within one parent when
3390f6bba201SDave Chinner 	 * the target existed, or across two parent directories.
3391f6bba201SDave Chinner 	 */
3392f6bba201SDave Chinner 	if (src_is_directory && (new_parent || target_ip != NULL)) {
3393f6bba201SDave Chinner 
3394f6bba201SDave Chinner 		/*
3395f6bba201SDave Chinner 		 * Decrement link count on src_directory since the
3396f6bba201SDave Chinner 		 * entry that's moved no longer points to it.
3397f6bba201SDave Chinner 		 */
3398f6bba201SDave Chinner 		error = xfs_droplink(tp, src_dp);
3399f6bba201SDave Chinner 		if (error)
3400c8eac49eSBrian Foster 			goto out_trans_cancel;
3401f6bba201SDave Chinner 	}
3402f6bba201SDave Chinner 
34037dcf5c3eSDave Chinner 	/*
34047dcf5c3eSDave Chinner 	 * For whiteouts, we only need to update the source dirent with the
34057dcf5c3eSDave Chinner 	 * inode number of the whiteout inode rather than removing it
34067dcf5c3eSDave Chinner 	 * altogether.
34077dcf5c3eSDave Chinner 	 */
34087dcf5c3eSDave Chinner 	if (wip) {
34097dcf5c3eSDave Chinner 		error = xfs_dir_replace(tp, src_dp, src_name, wip->i_ino,
3410381eee69SBrian Foster 					spaceres);
34117dcf5c3eSDave Chinner 	} else
3412f6bba201SDave Chinner 		error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino,
3413381eee69SBrian Foster 					   spaceres);
3414f6bba201SDave Chinner 	if (error)
3415c8eac49eSBrian Foster 		goto out_trans_cancel;
3416f6bba201SDave Chinner 
3417f6bba201SDave Chinner 	xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3418f6bba201SDave Chinner 	xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE);
3419f6bba201SDave Chinner 	if (new_parent)
3420f6bba201SDave Chinner 		xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE);
3421f6bba201SDave Chinner 
3422c9cfdb38SBrian Foster 	error = xfs_finish_rename(tp);
34237dcf5c3eSDave Chinner 	if (wip)
342444a8736bSDarrick J. Wong 		xfs_irele(wip);
34257dcf5c3eSDave Chinner 	return error;
3426f6bba201SDave Chinner 
3427445883e8SDave Chinner out_trans_cancel:
34284906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
3429253f4911SChristoph Hellwig out_release_wip:
34307dcf5c3eSDave Chinner 	if (wip)
343144a8736bSDarrick J. Wong 		xfs_irele(wip);
3432f6bba201SDave Chinner 	return error;
3433f6bba201SDave Chinner }
3434f6bba201SDave Chinner 
3435e6187b34SDave Chinner static int
3436e6187b34SDave Chinner xfs_iflush(
343793848a99SChristoph Hellwig 	struct xfs_inode	*ip,
343893848a99SChristoph Hellwig 	struct xfs_buf		*bp)
34391da177e4SLinus Torvalds {
344093848a99SChristoph Hellwig 	struct xfs_inode_log_item *iip = ip->i_itemp;
344193848a99SChristoph Hellwig 	struct xfs_dinode	*dip;
344293848a99SChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
3443f2019299SBrian Foster 	int			error;
34441da177e4SLinus Torvalds 
3445579aa9caSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
3446474fce06SChristoph Hellwig 	ASSERT(xfs_isiflocked(ip));
3447f7e67b20SChristoph Hellwig 	ASSERT(ip->i_df.if_format != XFS_DINODE_FMT_BTREE ||
3448daf83964SChristoph Hellwig 	       ip->i_df.if_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
344990c60e16SDave Chinner 	ASSERT(iip->ili_item.li_buf == bp);
34501da177e4SLinus Torvalds 
345188ee2df7SChristoph Hellwig 	dip = xfs_buf_offset(bp, ip->i_imap.im_boffset);
34521da177e4SLinus Torvalds 
3453f2019299SBrian Foster 	/*
3454f2019299SBrian Foster 	 * We don't flush the inode if any of the following checks fail, but we
3455f2019299SBrian Foster 	 * do still update the log item and attach to the backing buffer as if
3456f2019299SBrian Foster 	 * the flush happened. This is a formality to facilitate predictable
3457f2019299SBrian Foster 	 * error handling as the caller will shutdown and fail the buffer.
3458f2019299SBrian Foster 	 */
3459f2019299SBrian Foster 	error = -EFSCORRUPTED;
346069ef921bSChristoph Hellwig 	if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
34619e24cfd0SDarrick J. Wong 			       mp, XFS_ERRTAG_IFLUSH_1)) {
34626a19d939SDave Chinner 		xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3463c9690043SDarrick J. Wong 			"%s: Bad inode %Lu magic number 0x%x, ptr "PTR_FMT,
34646a19d939SDave Chinner 			__func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
3465f2019299SBrian Foster 		goto flush_out;
34661da177e4SLinus Torvalds 	}
3467c19b3b05SDave Chinner 	if (S_ISREG(VFS_I(ip)->i_mode)) {
34681da177e4SLinus Torvalds 		if (XFS_TEST_ERROR(
3469f7e67b20SChristoph Hellwig 		    ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS &&
3470f7e67b20SChristoph Hellwig 		    ip->i_df.if_format != XFS_DINODE_FMT_BTREE,
34719e24cfd0SDarrick J. Wong 		    mp, XFS_ERRTAG_IFLUSH_3)) {
34726a19d939SDave Chinner 			xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3473c9690043SDarrick J. Wong 				"%s: Bad regular inode %Lu, ptr "PTR_FMT,
34746a19d939SDave Chinner 				__func__, ip->i_ino, ip);
3475f2019299SBrian Foster 			goto flush_out;
34761da177e4SLinus Torvalds 		}
3477c19b3b05SDave Chinner 	} else if (S_ISDIR(VFS_I(ip)->i_mode)) {
34781da177e4SLinus Torvalds 		if (XFS_TEST_ERROR(
3479f7e67b20SChristoph Hellwig 		    ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS &&
3480f7e67b20SChristoph Hellwig 		    ip->i_df.if_format != XFS_DINODE_FMT_BTREE &&
3481f7e67b20SChristoph Hellwig 		    ip->i_df.if_format != XFS_DINODE_FMT_LOCAL,
34829e24cfd0SDarrick J. Wong 		    mp, XFS_ERRTAG_IFLUSH_4)) {
34836a19d939SDave Chinner 			xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3484c9690043SDarrick J. Wong 				"%s: Bad directory inode %Lu, ptr "PTR_FMT,
34856a19d939SDave Chinner 				__func__, ip->i_ino, ip);
3486f2019299SBrian Foster 			goto flush_out;
34871da177e4SLinus Torvalds 		}
34881da177e4SLinus Torvalds 	}
3489daf83964SChristoph Hellwig 	if (XFS_TEST_ERROR(ip->i_df.if_nextents + xfs_ifork_nextents(ip->i_afp) >
34909e24cfd0SDarrick J. Wong 				ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5)) {
34916a19d939SDave Chinner 		xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
34926a19d939SDave Chinner 			"%s: detected corrupt incore inode %Lu, "
3493c9690043SDarrick J. Wong 			"total extents = %d, nblocks = %Ld, ptr "PTR_FMT,
34946a19d939SDave Chinner 			__func__, ip->i_ino,
3495daf83964SChristoph Hellwig 			ip->i_df.if_nextents + xfs_ifork_nextents(ip->i_afp),
34966a19d939SDave Chinner 			ip->i_d.di_nblocks, ip);
3497f2019299SBrian Foster 		goto flush_out;
34981da177e4SLinus Torvalds 	}
34991da177e4SLinus Torvalds 	if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
35009e24cfd0SDarrick J. Wong 				mp, XFS_ERRTAG_IFLUSH_6)) {
35016a19d939SDave Chinner 		xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3502c9690043SDarrick J. Wong 			"%s: bad inode %Lu, forkoff 0x%x, ptr "PTR_FMT,
35036a19d939SDave Chinner 			__func__, ip->i_ino, ip->i_d.di_forkoff, ip);
3504f2019299SBrian Foster 		goto flush_out;
35051da177e4SLinus Torvalds 	}
3506e60896d8SDave Chinner 
35071da177e4SLinus Torvalds 	/*
3508263997a6SDave Chinner 	 * Inode item log recovery for v2 inodes are dependent on the
3509e60896d8SDave Chinner 	 * di_flushiter count for correct sequencing. We bump the flush
3510e60896d8SDave Chinner 	 * iteration count so we can detect flushes which postdate a log record
3511e60896d8SDave Chinner 	 * during recovery. This is redundant as we now log every change and
3512e60896d8SDave Chinner 	 * hence this can't happen but we need to still do it to ensure
3513e60896d8SDave Chinner 	 * backwards compatibility with old kernels that predate logging all
3514e60896d8SDave Chinner 	 * inode changes.
35151da177e4SLinus Torvalds 	 */
35166471e9c5SChristoph Hellwig 	if (!xfs_sb_version_has_v3inode(&mp->m_sb))
35171da177e4SLinus Torvalds 		ip->i_d.di_flushiter++;
35181da177e4SLinus Torvalds 
35190f45a1b2SChristoph Hellwig 	/*
35200f45a1b2SChristoph Hellwig 	 * If there are inline format data / attr forks attached to this inode,
35210f45a1b2SChristoph Hellwig 	 * make sure they are not corrupt.
35220f45a1b2SChristoph Hellwig 	 */
3523f7e67b20SChristoph Hellwig 	if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL &&
35240f45a1b2SChristoph Hellwig 	    xfs_ifork_verify_local_data(ip))
35250f45a1b2SChristoph Hellwig 		goto flush_out;
3526f7e67b20SChristoph Hellwig 	if (ip->i_afp && ip->i_afp->if_format == XFS_DINODE_FMT_LOCAL &&
35270f45a1b2SChristoph Hellwig 	    xfs_ifork_verify_local_attr(ip))
3528f2019299SBrian Foster 		goto flush_out;
3529005c5db8SDarrick J. Wong 
35301da177e4SLinus Torvalds 	/*
35313987848cSDave Chinner 	 * Copy the dirty parts of the inode into the on-disk inode.  We always
35323987848cSDave Chinner 	 * copy out the core of the inode, because if the inode is dirty at all
35333987848cSDave Chinner 	 * the core must be.
35341da177e4SLinus Torvalds 	 */
353593f958f9SDave Chinner 	xfs_inode_to_disk(ip, dip, iip->ili_item.li_lsn);
35361da177e4SLinus Torvalds 
35371da177e4SLinus Torvalds 	/* Wrap, we never let the log put out DI_MAX_FLUSH */
35381da177e4SLinus Torvalds 	if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
35391da177e4SLinus Torvalds 		ip->i_d.di_flushiter = 0;
35401da177e4SLinus Torvalds 
3541005c5db8SDarrick J. Wong 	xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK);
3542005c5db8SDarrick J. Wong 	if (XFS_IFORK_Q(ip))
3543005c5db8SDarrick J. Wong 		xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK);
35441da177e4SLinus Torvalds 
35451da177e4SLinus Torvalds 	/*
3546f5d8d5c4SChristoph Hellwig 	 * We've recorded everything logged in the inode, so we'd like to clear
3547f5d8d5c4SChristoph Hellwig 	 * the ili_fields bits so we don't log and flush things unnecessarily.
3548f5d8d5c4SChristoph Hellwig 	 * However, we can't stop logging all this information until the data
3549f5d8d5c4SChristoph Hellwig 	 * we've copied into the disk buffer is written to disk.  If we did we
3550f5d8d5c4SChristoph Hellwig 	 * might overwrite the copy of the inode in the log with all the data
3551f5d8d5c4SChristoph Hellwig 	 * after re-logging only part of it, and in the face of a crash we
3552f5d8d5c4SChristoph Hellwig 	 * wouldn't have all the data we need to recover.
35531da177e4SLinus Torvalds 	 *
3554f5d8d5c4SChristoph Hellwig 	 * What we do is move the bits to the ili_last_fields field.  When
3555f5d8d5c4SChristoph Hellwig 	 * logging the inode, these bits are moved back to the ili_fields field.
3556f5d8d5c4SChristoph Hellwig 	 * In the xfs_iflush_done() routine we clear ili_last_fields, since we
3557f5d8d5c4SChristoph Hellwig 	 * know that the information those bits represent is permanently on
3558f5d8d5c4SChristoph Hellwig 	 * disk.  As long as the flush completes before the inode is logged
3559f5d8d5c4SChristoph Hellwig 	 * again, then both ili_fields and ili_last_fields will be cleared.
35601da177e4SLinus Torvalds 	 */
3561f2019299SBrian Foster 	error = 0;
3562f2019299SBrian Foster flush_out:
35631319ebefSDave Chinner 	spin_lock(&iip->ili_lock);
3564f5d8d5c4SChristoph Hellwig 	iip->ili_last_fields = iip->ili_fields;
3565f5d8d5c4SChristoph Hellwig 	iip->ili_fields = 0;
3566fc0561ceSDave Chinner 	iip->ili_fsync_fields = 0;
35671319ebefSDave Chinner 	spin_unlock(&iip->ili_lock);
35681da177e4SLinus Torvalds 
35691319ebefSDave Chinner 	/*
35701319ebefSDave Chinner 	 * Store the current LSN of the inode so that we can tell whether the
35711319ebefSDave Chinner 	 * item has moved in the AIL from xfs_iflush_done().
35721319ebefSDave Chinner 	 */
35737b2e2a31SDavid Chinner 	xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
35747b2e2a31SDavid Chinner 				&iip->ili_item.li_lsn);
35751da177e4SLinus Torvalds 
357693848a99SChristoph Hellwig 	/* generate the checksum. */
357793848a99SChristoph Hellwig 	xfs_dinode_calc_crc(mp, dip);
3578f2019299SBrian Foster 	return error;
35791da177e4SLinus Torvalds }
358044a8736bSDarrick J. Wong 
3581e6187b34SDave Chinner /*
3582e6187b34SDave Chinner  * Non-blocking flush of dirty inode metadata into the backing buffer.
3583e6187b34SDave Chinner  *
3584e6187b34SDave Chinner  * The caller must have a reference to the inode and hold the cluster buffer
3585e6187b34SDave Chinner  * locked. The function will walk across all the inodes on the cluster buffer it
3586e6187b34SDave Chinner  * can find and lock without blocking, and flush them to the cluster buffer.
3587e6187b34SDave Chinner  *
35885717ea4dSDave Chinner  * On successful flushing of at least one inode, the caller must write out the
35895717ea4dSDave Chinner  * buffer and release it. If no inodes are flushed, -EAGAIN will be returned and
35905717ea4dSDave Chinner  * the caller needs to release the buffer. On failure, the filesystem will be
35915717ea4dSDave Chinner  * shut down, the buffer will have been unlocked and released, and EFSCORRUPTED
35925717ea4dSDave Chinner  * will be returned.
3593e6187b34SDave Chinner  */
3594e6187b34SDave Chinner int
3595e6187b34SDave Chinner xfs_iflush_cluster(
3596e6187b34SDave Chinner 	struct xfs_buf		*bp)
3597e6187b34SDave Chinner {
35985717ea4dSDave Chinner 	struct xfs_mount	*mp = bp->b_mount;
35995717ea4dSDave Chinner 	struct xfs_log_item	*lip, *n;
36005717ea4dSDave Chinner 	struct xfs_inode	*ip;
36015717ea4dSDave Chinner 	struct xfs_inode_log_item *iip;
3602e6187b34SDave Chinner 	int			clcount = 0;
36035717ea4dSDave Chinner 	int			error = 0;
3604e6187b34SDave Chinner 
3605e6187b34SDave Chinner 	/*
36065717ea4dSDave Chinner 	 * We must use the safe variant here as on shutdown xfs_iflush_abort()
36075717ea4dSDave Chinner 	 * can remove itself from the list.
3608e6187b34SDave Chinner 	 */
36095717ea4dSDave Chinner 	list_for_each_entry_safe(lip, n, &bp->b_li_list, li_bio_list) {
36105717ea4dSDave Chinner 		iip = (struct xfs_inode_log_item *)lip;
36115717ea4dSDave Chinner 		ip = iip->ili_inode;
36125717ea4dSDave Chinner 
36135717ea4dSDave Chinner 		/*
36145717ea4dSDave Chinner 		 * Quick and dirty check to avoid locks if possible.
36155717ea4dSDave Chinner 		 */
36165717ea4dSDave Chinner 		if (__xfs_iflags_test(ip, XFS_IRECLAIM | XFS_IFLOCK))
36175717ea4dSDave Chinner 			continue;
36185717ea4dSDave Chinner 		if (xfs_ipincount(ip))
36195717ea4dSDave Chinner 			continue;
36205717ea4dSDave Chinner 
36215717ea4dSDave Chinner 		/*
36225717ea4dSDave Chinner 		 * The inode is still attached to the buffer, which means it is
36235717ea4dSDave Chinner 		 * dirty but reclaim might try to grab it. Check carefully for
36245717ea4dSDave Chinner 		 * that, and grab the ilock while still holding the i_flags_lock
36255717ea4dSDave Chinner 		 * to guarantee reclaim will not be able to reclaim this inode
36265717ea4dSDave Chinner 		 * once we drop the i_flags_lock.
36275717ea4dSDave Chinner 		 */
36285717ea4dSDave Chinner 		spin_lock(&ip->i_flags_lock);
36295717ea4dSDave Chinner 		ASSERT(!__xfs_iflags_test(ip, XFS_ISTALE));
36305717ea4dSDave Chinner 		if (__xfs_iflags_test(ip, XFS_IRECLAIM | XFS_IFLOCK)) {
36315717ea4dSDave Chinner 			spin_unlock(&ip->i_flags_lock);
3632e6187b34SDave Chinner 			continue;
3633e6187b34SDave Chinner 		}
3634e6187b34SDave Chinner 
3635e6187b34SDave Chinner 		/*
36365717ea4dSDave Chinner 		 * ILOCK will pin the inode against reclaim and prevent
36375717ea4dSDave Chinner 		 * concurrent transactions modifying the inode while we are
36385717ea4dSDave Chinner 		 * flushing the inode.
3639e6187b34SDave Chinner 		 */
36405717ea4dSDave Chinner 		if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
36415717ea4dSDave Chinner 			spin_unlock(&ip->i_flags_lock);
36425717ea4dSDave Chinner 			continue;
36435717ea4dSDave Chinner 		}
36445717ea4dSDave Chinner 		spin_unlock(&ip->i_flags_lock);
36455717ea4dSDave Chinner 
36465717ea4dSDave Chinner 		/*
36475717ea4dSDave Chinner 		 * Skip inodes that are already flush locked as they have
36485717ea4dSDave Chinner 		 * already been written to the buffer.
36495717ea4dSDave Chinner 		 */
36505717ea4dSDave Chinner 		if (!xfs_iflock_nowait(ip)) {
36515717ea4dSDave Chinner 			xfs_iunlock(ip, XFS_ILOCK_SHARED);
36525717ea4dSDave Chinner 			continue;
36535717ea4dSDave Chinner 		}
36545717ea4dSDave Chinner 
36555717ea4dSDave Chinner 		/*
36565717ea4dSDave Chinner 		 * Abort flushing this inode if we are shut down because the
36575717ea4dSDave Chinner 		 * inode may not currently be in the AIL. This can occur when
36585717ea4dSDave Chinner 		 * log I/O failure unpins the inode without inserting into the
36595717ea4dSDave Chinner 		 * AIL, leaving a dirty/unpinned inode attached to the buffer
36605717ea4dSDave Chinner 		 * that otherwise looks like it should be flushed.
36615717ea4dSDave Chinner 		 */
36625717ea4dSDave Chinner 		if (XFS_FORCED_SHUTDOWN(mp)) {
36635717ea4dSDave Chinner 			xfs_iunpin_wait(ip);
36645717ea4dSDave Chinner 			/* xfs_iflush_abort() drops the flush lock */
36655717ea4dSDave Chinner 			xfs_iflush_abort(ip);
36665717ea4dSDave Chinner 			xfs_iunlock(ip, XFS_ILOCK_SHARED);
36675717ea4dSDave Chinner 			error = -EIO;
36685717ea4dSDave Chinner 			continue;
36695717ea4dSDave Chinner 		}
36705717ea4dSDave Chinner 
36715717ea4dSDave Chinner 		/* don't block waiting on a log force to unpin dirty inodes */
36725717ea4dSDave Chinner 		if (xfs_ipincount(ip)) {
36735717ea4dSDave Chinner 			xfs_ifunlock(ip);
36745717ea4dSDave Chinner 			xfs_iunlock(ip, XFS_ILOCK_SHARED);
36755717ea4dSDave Chinner 			continue;
36765717ea4dSDave Chinner 		}
36775717ea4dSDave Chinner 
36785717ea4dSDave Chinner 		if (!xfs_inode_clean(ip))
36795717ea4dSDave Chinner 			error = xfs_iflush(ip, bp);
36805717ea4dSDave Chinner 		else
36815717ea4dSDave Chinner 			xfs_ifunlock(ip);
36825717ea4dSDave Chinner 		xfs_iunlock(ip, XFS_ILOCK_SHARED);
36835717ea4dSDave Chinner 		if (error)
3684e6187b34SDave Chinner 			break;
3685e6187b34SDave Chinner 		clcount++;
3686e6187b34SDave Chinner 	}
3687e6187b34SDave Chinner 
3688e6187b34SDave Chinner 	if (error) {
3689e6187b34SDave Chinner 		bp->b_flags |= XBF_ASYNC;
3690e6187b34SDave Chinner 		xfs_buf_ioend_fail(bp);
3691e6187b34SDave Chinner 		xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
3692e6187b34SDave Chinner 		return error;
3693e6187b34SDave Chinner 	}
3694e6187b34SDave Chinner 
36955717ea4dSDave Chinner 	if (!clcount)
36965717ea4dSDave Chinner 		return -EAGAIN;
36975717ea4dSDave Chinner 
36985717ea4dSDave Chinner 	XFS_STATS_INC(mp, xs_icluster_flushcnt);
36995717ea4dSDave Chinner 	XFS_STATS_ADD(mp, xs_icluster_flushinode, clcount);
37005717ea4dSDave Chinner 	return 0;
37015717ea4dSDave Chinner 
37025717ea4dSDave Chinner }
37035717ea4dSDave Chinner 
370444a8736bSDarrick J. Wong /* Release an inode. */
370544a8736bSDarrick J. Wong void
370644a8736bSDarrick J. Wong xfs_irele(
370744a8736bSDarrick J. Wong 	struct xfs_inode	*ip)
370844a8736bSDarrick J. Wong {
370944a8736bSDarrick J. Wong 	trace_xfs_irele(ip, _RET_IP_);
371044a8736bSDarrick J. Wong 	iput(VFS_I(ip));
371144a8736bSDarrick J. Wong }
371254fbdd10SChristoph Hellwig 
371354fbdd10SChristoph Hellwig /*
371454fbdd10SChristoph Hellwig  * Ensure all commited transactions touching the inode are written to the log.
371554fbdd10SChristoph Hellwig  */
371654fbdd10SChristoph Hellwig int
371754fbdd10SChristoph Hellwig xfs_log_force_inode(
371854fbdd10SChristoph Hellwig 	struct xfs_inode	*ip)
371954fbdd10SChristoph Hellwig {
372054fbdd10SChristoph Hellwig 	xfs_lsn_t		lsn = 0;
372154fbdd10SChristoph Hellwig 
372254fbdd10SChristoph Hellwig 	xfs_ilock(ip, XFS_ILOCK_SHARED);
372354fbdd10SChristoph Hellwig 	if (xfs_ipincount(ip))
372454fbdd10SChristoph Hellwig 		lsn = ip->i_itemp->ili_last_lsn;
372554fbdd10SChristoph Hellwig 	xfs_iunlock(ip, XFS_ILOCK_SHARED);
372654fbdd10SChristoph Hellwig 
372754fbdd10SChristoph Hellwig 	if (!lsn)
372854fbdd10SChristoph Hellwig 		return 0;
372954fbdd10SChristoph Hellwig 	return xfs_log_force_lsn(ip->i_mount, lsn, XFS_LOG_SYNC, NULL);
373054fbdd10SChristoph Hellwig }
3731e2aaee9cSDarrick J. Wong 
3732e2aaee9cSDarrick J. Wong /*
3733e2aaee9cSDarrick J. Wong  * Grab the exclusive iolock for a data copy from src to dest, making sure to
3734e2aaee9cSDarrick J. Wong  * abide vfs locking order (lowest pointer value goes first) and breaking the
3735e2aaee9cSDarrick J. Wong  * layout leases before proceeding.  The loop is needed because we cannot call
3736e2aaee9cSDarrick J. Wong  * the blocking break_layout() with the iolocks held, and therefore have to
3737e2aaee9cSDarrick J. Wong  * back out both locks.
3738e2aaee9cSDarrick J. Wong  */
3739e2aaee9cSDarrick J. Wong static int
3740e2aaee9cSDarrick J. Wong xfs_iolock_two_inodes_and_break_layout(
3741e2aaee9cSDarrick J. Wong 	struct inode		*src,
3742e2aaee9cSDarrick J. Wong 	struct inode		*dest)
3743e2aaee9cSDarrick J. Wong {
3744e2aaee9cSDarrick J. Wong 	int			error;
3745e2aaee9cSDarrick J. Wong 
3746e2aaee9cSDarrick J. Wong 	if (src > dest)
3747e2aaee9cSDarrick J. Wong 		swap(src, dest);
3748e2aaee9cSDarrick J. Wong 
3749e2aaee9cSDarrick J. Wong retry:
3750e2aaee9cSDarrick J. Wong 	/* Wait to break both inodes' layouts before we start locking. */
3751e2aaee9cSDarrick J. Wong 	error = break_layout(src, true);
3752e2aaee9cSDarrick J. Wong 	if (error)
3753e2aaee9cSDarrick J. Wong 		return error;
3754e2aaee9cSDarrick J. Wong 	if (src != dest) {
3755e2aaee9cSDarrick J. Wong 		error = break_layout(dest, true);
3756e2aaee9cSDarrick J. Wong 		if (error)
3757e2aaee9cSDarrick J. Wong 			return error;
3758e2aaee9cSDarrick J. Wong 	}
3759e2aaee9cSDarrick J. Wong 
3760e2aaee9cSDarrick J. Wong 	/* Lock one inode and make sure nobody got in and leased it. */
3761e2aaee9cSDarrick J. Wong 	inode_lock(src);
3762e2aaee9cSDarrick J. Wong 	error = break_layout(src, false);
3763e2aaee9cSDarrick J. Wong 	if (error) {
3764e2aaee9cSDarrick J. Wong 		inode_unlock(src);
3765e2aaee9cSDarrick J. Wong 		if (error == -EWOULDBLOCK)
3766e2aaee9cSDarrick J. Wong 			goto retry;
3767e2aaee9cSDarrick J. Wong 		return error;
3768e2aaee9cSDarrick J. Wong 	}
3769e2aaee9cSDarrick J. Wong 
3770e2aaee9cSDarrick J. Wong 	if (src == dest)
3771e2aaee9cSDarrick J. Wong 		return 0;
3772e2aaee9cSDarrick J. Wong 
3773e2aaee9cSDarrick J. Wong 	/* Lock the other inode and make sure nobody got in and leased it. */
3774e2aaee9cSDarrick J. Wong 	inode_lock_nested(dest, I_MUTEX_NONDIR2);
3775e2aaee9cSDarrick J. Wong 	error = break_layout(dest, false);
3776e2aaee9cSDarrick J. Wong 	if (error) {
3777e2aaee9cSDarrick J. Wong 		inode_unlock(src);
3778e2aaee9cSDarrick J. Wong 		inode_unlock(dest);
3779e2aaee9cSDarrick J. Wong 		if (error == -EWOULDBLOCK)
3780e2aaee9cSDarrick J. Wong 			goto retry;
3781e2aaee9cSDarrick J. Wong 		return error;
3782e2aaee9cSDarrick J. Wong 	}
3783e2aaee9cSDarrick J. Wong 
3784e2aaee9cSDarrick J. Wong 	return 0;
3785e2aaee9cSDarrick J. Wong }
3786e2aaee9cSDarrick J. Wong 
3787e2aaee9cSDarrick J. Wong /*
3788e2aaee9cSDarrick J. Wong  * Lock two inodes so that userspace cannot initiate I/O via file syscalls or
3789e2aaee9cSDarrick J. Wong  * mmap activity.
3790e2aaee9cSDarrick J. Wong  */
3791e2aaee9cSDarrick J. Wong int
3792e2aaee9cSDarrick J. Wong xfs_ilock2_io_mmap(
3793e2aaee9cSDarrick J. Wong 	struct xfs_inode	*ip1,
3794e2aaee9cSDarrick J. Wong 	struct xfs_inode	*ip2)
3795e2aaee9cSDarrick J. Wong {
3796e2aaee9cSDarrick J. Wong 	int			ret;
3797e2aaee9cSDarrick J. Wong 
3798e2aaee9cSDarrick J. Wong 	ret = xfs_iolock_two_inodes_and_break_layout(VFS_I(ip1), VFS_I(ip2));
3799e2aaee9cSDarrick J. Wong 	if (ret)
3800e2aaee9cSDarrick J. Wong 		return ret;
3801e2aaee9cSDarrick J. Wong 	if (ip1 == ip2)
3802e2aaee9cSDarrick J. Wong 		xfs_ilock(ip1, XFS_MMAPLOCK_EXCL);
3803e2aaee9cSDarrick J. Wong 	else
3804e2aaee9cSDarrick J. Wong 		xfs_lock_two_inodes(ip1, XFS_MMAPLOCK_EXCL,
3805e2aaee9cSDarrick J. Wong 				    ip2, XFS_MMAPLOCK_EXCL);
3806e2aaee9cSDarrick J. Wong 	return 0;
3807e2aaee9cSDarrick J. Wong }
3808e2aaee9cSDarrick J. Wong 
3809e2aaee9cSDarrick J. Wong /* Unlock both inodes to allow IO and mmap activity. */
3810e2aaee9cSDarrick J. Wong void
3811e2aaee9cSDarrick J. Wong xfs_iunlock2_io_mmap(
3812e2aaee9cSDarrick J. Wong 	struct xfs_inode	*ip1,
3813e2aaee9cSDarrick J. Wong 	struct xfs_inode	*ip2)
3814e2aaee9cSDarrick J. Wong {
3815e2aaee9cSDarrick J. Wong 	bool			same_inode = (ip1 == ip2);
3816e2aaee9cSDarrick J. Wong 
3817e2aaee9cSDarrick J. Wong 	xfs_iunlock(ip2, XFS_MMAPLOCK_EXCL);
3818e2aaee9cSDarrick J. Wong 	if (!same_inode)
3819e2aaee9cSDarrick J. Wong 		xfs_iunlock(ip1, XFS_MMAPLOCK_EXCL);
3820e2aaee9cSDarrick J. Wong 	inode_unlock(VFS_I(ip2));
3821e2aaee9cSDarrick J. Wong 	if (!same_inode)
3822e2aaee9cSDarrick J. Wong 		inode_unlock(VFS_I(ip1));
3823e2aaee9cSDarrick J. Wong }
3824