xref: /openbmc/linux/fs/xfs/xfs_inode.c (revision c1e8d7c6a7a682e1405e3e242d32fc377fd196ff)
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_iflush_int(struct xfs_inode *, struct xfs_buf *);
4854d7b5c1SDave Chinner STATIC int xfs_iunlink(struct xfs_trans *, struct xfs_inode *);
4954d7b5c1SDave Chinner STATIC int xfs_iunlink_remove(struct xfs_trans *, struct xfs_inode *);
50ab297431SZhi Yong Wu 
512a0ec1d9SDave Chinner /*
522a0ec1d9SDave Chinner  * helper function to extract extent size hint from inode
532a0ec1d9SDave Chinner  */
542a0ec1d9SDave Chinner xfs_extlen_t
552a0ec1d9SDave Chinner xfs_get_extsz_hint(
562a0ec1d9SDave Chinner 	struct xfs_inode	*ip)
572a0ec1d9SDave Chinner {
58bdb2ed2dSChristoph Hellwig 	/*
59bdb2ed2dSChristoph Hellwig 	 * No point in aligning allocations if we need to COW to actually
60bdb2ed2dSChristoph Hellwig 	 * write to them.
61bdb2ed2dSChristoph Hellwig 	 */
62bdb2ed2dSChristoph Hellwig 	if (xfs_is_always_cow_inode(ip))
63bdb2ed2dSChristoph Hellwig 		return 0;
642a0ec1d9SDave Chinner 	if ((ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE) && ip->i_d.di_extsize)
652a0ec1d9SDave Chinner 		return ip->i_d.di_extsize;
662a0ec1d9SDave Chinner 	if (XFS_IS_REALTIME_INODE(ip))
672a0ec1d9SDave Chinner 		return ip->i_mount->m_sb.sb_rextsize;
682a0ec1d9SDave Chinner 	return 0;
692a0ec1d9SDave Chinner }
702a0ec1d9SDave Chinner 
71fa96acadSDave Chinner /*
72f7ca3522SDarrick J. Wong  * Helper function to extract CoW extent size hint from inode.
73f7ca3522SDarrick J. Wong  * Between the extent size hint and the CoW extent size hint, we
74e153aa79SDarrick J. Wong  * return the greater of the two.  If the value is zero (automatic),
75e153aa79SDarrick J. Wong  * use the default size.
76f7ca3522SDarrick J. Wong  */
77f7ca3522SDarrick J. Wong xfs_extlen_t
78f7ca3522SDarrick J. Wong xfs_get_cowextsz_hint(
79f7ca3522SDarrick J. Wong 	struct xfs_inode	*ip)
80f7ca3522SDarrick J. Wong {
81f7ca3522SDarrick J. Wong 	xfs_extlen_t		a, b;
82f7ca3522SDarrick J. Wong 
83f7ca3522SDarrick J. Wong 	a = 0;
84f7ca3522SDarrick J. Wong 	if (ip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE)
85f7ca3522SDarrick J. Wong 		a = ip->i_d.di_cowextsize;
86f7ca3522SDarrick J. Wong 	b = xfs_get_extsz_hint(ip);
87f7ca3522SDarrick J. Wong 
88e153aa79SDarrick J. Wong 	a = max(a, b);
89e153aa79SDarrick J. Wong 	if (a == 0)
90e153aa79SDarrick J. Wong 		return XFS_DEFAULT_COWEXTSZ_HINT;
91f7ca3522SDarrick J. Wong 	return a;
92f7ca3522SDarrick J. Wong }
93f7ca3522SDarrick J. Wong 
94f7ca3522SDarrick J. Wong /*
95efa70be1SChristoph Hellwig  * These two are wrapper routines around the xfs_ilock() routine used to
96efa70be1SChristoph Hellwig  * centralize some grungy code.  They are used in places that wish to lock the
97efa70be1SChristoph Hellwig  * inode solely for reading the extents.  The reason these places can't just
98efa70be1SChristoph Hellwig  * call xfs_ilock(ip, XFS_ILOCK_SHARED) is that the inode lock also guards to
99efa70be1SChristoph Hellwig  * bringing in of the extents from disk for a file in b-tree format.  If the
100efa70be1SChristoph Hellwig  * inode is in b-tree format, then we need to lock the inode exclusively until
101efa70be1SChristoph Hellwig  * the extents are read in.  Locking it exclusively all the time would limit
102efa70be1SChristoph Hellwig  * our parallelism unnecessarily, though.  What we do instead is check to see
103efa70be1SChristoph Hellwig  * if the extents have been read in yet, and only lock the inode exclusively
104efa70be1SChristoph Hellwig  * if they have not.
105fa96acadSDave Chinner  *
106efa70be1SChristoph Hellwig  * The functions return a value which should be given to the corresponding
10701f4f327SChristoph Hellwig  * xfs_iunlock() call.
108fa96acadSDave Chinner  */
109fa96acadSDave Chinner uint
110309ecac8SChristoph Hellwig xfs_ilock_data_map_shared(
111309ecac8SChristoph Hellwig 	struct xfs_inode	*ip)
112fa96acadSDave Chinner {
113309ecac8SChristoph Hellwig 	uint			lock_mode = XFS_ILOCK_SHARED;
114fa96acadSDave Chinner 
115f7e67b20SChristoph Hellwig 	if (ip->i_df.if_format == XFS_DINODE_FMT_BTREE &&
116309ecac8SChristoph Hellwig 	    (ip->i_df.if_flags & XFS_IFEXTENTS) == 0)
117fa96acadSDave Chinner 		lock_mode = XFS_ILOCK_EXCL;
118fa96acadSDave Chinner 	xfs_ilock(ip, lock_mode);
119fa96acadSDave Chinner 	return lock_mode;
120fa96acadSDave Chinner }
121fa96acadSDave Chinner 
122efa70be1SChristoph Hellwig uint
123efa70be1SChristoph Hellwig xfs_ilock_attr_map_shared(
124efa70be1SChristoph Hellwig 	struct xfs_inode	*ip)
125fa96acadSDave Chinner {
126efa70be1SChristoph Hellwig 	uint			lock_mode = XFS_ILOCK_SHARED;
127efa70be1SChristoph Hellwig 
128f7e67b20SChristoph Hellwig 	if (ip->i_afp &&
129f7e67b20SChristoph Hellwig 	    ip->i_afp->if_format == XFS_DINODE_FMT_BTREE &&
130efa70be1SChristoph Hellwig 	    (ip->i_afp->if_flags & XFS_IFEXTENTS) == 0)
131efa70be1SChristoph Hellwig 		lock_mode = XFS_ILOCK_EXCL;
132efa70be1SChristoph Hellwig 	xfs_ilock(ip, lock_mode);
133efa70be1SChristoph Hellwig 	return lock_mode;
134fa96acadSDave Chinner }
135fa96acadSDave Chinner 
136fa96acadSDave Chinner /*
13765523218SChristoph Hellwig  * In addition to i_rwsem in the VFS inode, the xfs inode contains 2
13865523218SChristoph Hellwig  * multi-reader locks: i_mmap_lock and the i_lock.  This routine allows
13965523218SChristoph Hellwig  * various combinations of the locks to be obtained.
140fa96acadSDave Chinner  *
141653c60b6SDave Chinner  * The 3 locks should always be ordered so that the IO lock is obtained first,
142653c60b6SDave Chinner  * the mmap lock second and the ilock last in order to prevent deadlock.
143fa96acadSDave Chinner  *
144653c60b6SDave Chinner  * Basic locking order:
145653c60b6SDave Chinner  *
14665523218SChristoph Hellwig  * i_rwsem -> i_mmap_lock -> page_lock -> i_ilock
147653c60b6SDave Chinner  *
148*c1e8d7c6SMichel Lespinasse  * mmap_lock locking order:
149653c60b6SDave Chinner  *
150*c1e8d7c6SMichel Lespinasse  * i_rwsem -> page lock -> mmap_lock
151*c1e8d7c6SMichel Lespinasse  * mmap_lock -> i_mmap_lock -> page_lock
152653c60b6SDave Chinner  *
153*c1e8d7c6SMichel Lespinasse  * The difference in mmap_lock locking order mean that we cannot hold the
154653c60b6SDave Chinner  * i_mmap_lock over syscall based read(2)/write(2) based IO. These IO paths can
155*c1e8d7c6SMichel Lespinasse  * fault in pages during copy in/out (for buffered IO) or require the mmap_lock
156653c60b6SDave Chinner  * in get_user_pages() to map the user pages into the kernel address space for
15765523218SChristoph Hellwig  * direct IO. Similarly the i_rwsem cannot be taken inside a page fault because
158*c1e8d7c6SMichel Lespinasse  * page faults already hold the mmap_lock.
159653c60b6SDave Chinner  *
160653c60b6SDave Chinner  * Hence to serialise fully against both syscall and mmap based IO, we need to
16165523218SChristoph Hellwig  * take both the i_rwsem and the i_mmap_lock. These locks should *only* be both
162653c60b6SDave Chinner  * taken in places where we need to invalidate the page cache in a race
163653c60b6SDave Chinner  * free manner (e.g. truncate, hole punch and other extent manipulation
164653c60b6SDave Chinner  * functions).
165fa96acadSDave Chinner  */
166fa96acadSDave Chinner void
167fa96acadSDave Chinner xfs_ilock(
168fa96acadSDave Chinner 	xfs_inode_t		*ip,
169fa96acadSDave Chinner 	uint			lock_flags)
170fa96acadSDave Chinner {
171fa96acadSDave Chinner 	trace_xfs_ilock(ip, lock_flags, _RET_IP_);
172fa96acadSDave Chinner 
173fa96acadSDave Chinner 	/*
174fa96acadSDave Chinner 	 * You can't set both SHARED and EXCL for the same lock,
175fa96acadSDave Chinner 	 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
176fa96acadSDave Chinner 	 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
177fa96acadSDave Chinner 	 */
178fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
179fa96acadSDave Chinner 	       (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
180653c60b6SDave Chinner 	ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
181653c60b6SDave Chinner 	       (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
182fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
183fa96acadSDave Chinner 	       (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
1840952c818SDave Chinner 	ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
185fa96acadSDave Chinner 
18665523218SChristoph Hellwig 	if (lock_flags & XFS_IOLOCK_EXCL) {
18765523218SChristoph Hellwig 		down_write_nested(&VFS_I(ip)->i_rwsem,
18865523218SChristoph Hellwig 				  XFS_IOLOCK_DEP(lock_flags));
18965523218SChristoph Hellwig 	} else if (lock_flags & XFS_IOLOCK_SHARED) {
19065523218SChristoph Hellwig 		down_read_nested(&VFS_I(ip)->i_rwsem,
19165523218SChristoph Hellwig 				 XFS_IOLOCK_DEP(lock_flags));
19265523218SChristoph Hellwig 	}
193fa96acadSDave Chinner 
194653c60b6SDave Chinner 	if (lock_flags & XFS_MMAPLOCK_EXCL)
195653c60b6SDave Chinner 		mrupdate_nested(&ip->i_mmaplock, XFS_MMAPLOCK_DEP(lock_flags));
196653c60b6SDave Chinner 	else if (lock_flags & XFS_MMAPLOCK_SHARED)
197653c60b6SDave Chinner 		mraccess_nested(&ip->i_mmaplock, XFS_MMAPLOCK_DEP(lock_flags));
198653c60b6SDave Chinner 
199fa96acadSDave Chinner 	if (lock_flags & XFS_ILOCK_EXCL)
200fa96acadSDave Chinner 		mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
201fa96acadSDave Chinner 	else if (lock_flags & XFS_ILOCK_SHARED)
202fa96acadSDave Chinner 		mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
203fa96acadSDave Chinner }
204fa96acadSDave Chinner 
205fa96acadSDave Chinner /*
206fa96acadSDave Chinner  * This is just like xfs_ilock(), except that the caller
207fa96acadSDave Chinner  * is guaranteed not to sleep.  It returns 1 if it gets
208fa96acadSDave Chinner  * the requested locks and 0 otherwise.  If the IO lock is
209fa96acadSDave Chinner  * obtained but the inode lock cannot be, then the IO lock
210fa96acadSDave Chinner  * is dropped before returning.
211fa96acadSDave Chinner  *
212fa96acadSDave Chinner  * ip -- the inode being locked
213fa96acadSDave Chinner  * lock_flags -- this parameter indicates the inode's locks to be
214fa96acadSDave Chinner  *       to be locked.  See the comment for xfs_ilock() for a list
215fa96acadSDave Chinner  *	 of valid values.
216fa96acadSDave Chinner  */
217fa96acadSDave Chinner int
218fa96acadSDave Chinner xfs_ilock_nowait(
219fa96acadSDave Chinner 	xfs_inode_t		*ip,
220fa96acadSDave Chinner 	uint			lock_flags)
221fa96acadSDave Chinner {
222fa96acadSDave Chinner 	trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_);
223fa96acadSDave Chinner 
224fa96acadSDave Chinner 	/*
225fa96acadSDave Chinner 	 * You can't set both SHARED and EXCL for the same lock,
226fa96acadSDave Chinner 	 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
227fa96acadSDave Chinner 	 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
228fa96acadSDave Chinner 	 */
229fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
230fa96acadSDave Chinner 	       (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
231653c60b6SDave Chinner 	ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
232653c60b6SDave Chinner 	       (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
233fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
234fa96acadSDave Chinner 	       (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
2350952c818SDave Chinner 	ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
236fa96acadSDave Chinner 
237fa96acadSDave Chinner 	if (lock_flags & XFS_IOLOCK_EXCL) {
23865523218SChristoph Hellwig 		if (!down_write_trylock(&VFS_I(ip)->i_rwsem))
239fa96acadSDave Chinner 			goto out;
240fa96acadSDave Chinner 	} else if (lock_flags & XFS_IOLOCK_SHARED) {
24165523218SChristoph Hellwig 		if (!down_read_trylock(&VFS_I(ip)->i_rwsem))
242fa96acadSDave Chinner 			goto out;
243fa96acadSDave Chinner 	}
244653c60b6SDave Chinner 
245653c60b6SDave Chinner 	if (lock_flags & XFS_MMAPLOCK_EXCL) {
246653c60b6SDave Chinner 		if (!mrtryupdate(&ip->i_mmaplock))
247653c60b6SDave Chinner 			goto out_undo_iolock;
248653c60b6SDave Chinner 	} else if (lock_flags & XFS_MMAPLOCK_SHARED) {
249653c60b6SDave Chinner 		if (!mrtryaccess(&ip->i_mmaplock))
250653c60b6SDave Chinner 			goto out_undo_iolock;
251653c60b6SDave Chinner 	}
252653c60b6SDave Chinner 
253fa96acadSDave Chinner 	if (lock_flags & XFS_ILOCK_EXCL) {
254fa96acadSDave Chinner 		if (!mrtryupdate(&ip->i_lock))
255653c60b6SDave Chinner 			goto out_undo_mmaplock;
256fa96acadSDave Chinner 	} else if (lock_flags & XFS_ILOCK_SHARED) {
257fa96acadSDave Chinner 		if (!mrtryaccess(&ip->i_lock))
258653c60b6SDave Chinner 			goto out_undo_mmaplock;
259fa96acadSDave Chinner 	}
260fa96acadSDave Chinner 	return 1;
261fa96acadSDave Chinner 
262653c60b6SDave Chinner out_undo_mmaplock:
263653c60b6SDave Chinner 	if (lock_flags & XFS_MMAPLOCK_EXCL)
264653c60b6SDave Chinner 		mrunlock_excl(&ip->i_mmaplock);
265653c60b6SDave Chinner 	else if (lock_flags & XFS_MMAPLOCK_SHARED)
266653c60b6SDave Chinner 		mrunlock_shared(&ip->i_mmaplock);
267fa96acadSDave Chinner out_undo_iolock:
268fa96acadSDave Chinner 	if (lock_flags & XFS_IOLOCK_EXCL)
26965523218SChristoph Hellwig 		up_write(&VFS_I(ip)->i_rwsem);
270fa96acadSDave Chinner 	else if (lock_flags & XFS_IOLOCK_SHARED)
27165523218SChristoph Hellwig 		up_read(&VFS_I(ip)->i_rwsem);
272fa96acadSDave Chinner out:
273fa96acadSDave Chinner 	return 0;
274fa96acadSDave Chinner }
275fa96acadSDave Chinner 
276fa96acadSDave Chinner /*
277fa96acadSDave Chinner  * xfs_iunlock() is used to drop the inode locks acquired with
278fa96acadSDave Chinner  * xfs_ilock() and xfs_ilock_nowait().  The caller must pass
279fa96acadSDave Chinner  * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
280fa96acadSDave Chinner  * that we know which locks to drop.
281fa96acadSDave Chinner  *
282fa96acadSDave Chinner  * ip -- the inode being unlocked
283fa96acadSDave Chinner  * lock_flags -- this parameter indicates the inode's locks to be
284fa96acadSDave Chinner  *       to be unlocked.  See the comment for xfs_ilock() for a list
285fa96acadSDave Chinner  *	 of valid values for this parameter.
286fa96acadSDave Chinner  *
287fa96acadSDave Chinner  */
288fa96acadSDave Chinner void
289fa96acadSDave Chinner xfs_iunlock(
290fa96acadSDave Chinner 	xfs_inode_t		*ip,
291fa96acadSDave Chinner 	uint			lock_flags)
292fa96acadSDave Chinner {
293fa96acadSDave Chinner 	/*
294fa96acadSDave Chinner 	 * You can't set both SHARED and EXCL for the same lock,
295fa96acadSDave Chinner 	 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
296fa96acadSDave Chinner 	 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
297fa96acadSDave Chinner 	 */
298fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
299fa96acadSDave Chinner 	       (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
300653c60b6SDave Chinner 	ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
301653c60b6SDave Chinner 	       (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
302fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
303fa96acadSDave Chinner 	       (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
3040952c818SDave Chinner 	ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
305fa96acadSDave Chinner 	ASSERT(lock_flags != 0);
306fa96acadSDave Chinner 
307fa96acadSDave Chinner 	if (lock_flags & XFS_IOLOCK_EXCL)
30865523218SChristoph Hellwig 		up_write(&VFS_I(ip)->i_rwsem);
309fa96acadSDave Chinner 	else if (lock_flags & XFS_IOLOCK_SHARED)
31065523218SChristoph Hellwig 		up_read(&VFS_I(ip)->i_rwsem);
311fa96acadSDave Chinner 
312653c60b6SDave Chinner 	if (lock_flags & XFS_MMAPLOCK_EXCL)
313653c60b6SDave Chinner 		mrunlock_excl(&ip->i_mmaplock);
314653c60b6SDave Chinner 	else if (lock_flags & XFS_MMAPLOCK_SHARED)
315653c60b6SDave Chinner 		mrunlock_shared(&ip->i_mmaplock);
316653c60b6SDave Chinner 
317fa96acadSDave Chinner 	if (lock_flags & XFS_ILOCK_EXCL)
318fa96acadSDave Chinner 		mrunlock_excl(&ip->i_lock);
319fa96acadSDave Chinner 	else if (lock_flags & XFS_ILOCK_SHARED)
320fa96acadSDave Chinner 		mrunlock_shared(&ip->i_lock);
321fa96acadSDave Chinner 
322fa96acadSDave Chinner 	trace_xfs_iunlock(ip, lock_flags, _RET_IP_);
323fa96acadSDave Chinner }
324fa96acadSDave Chinner 
325fa96acadSDave Chinner /*
326fa96acadSDave Chinner  * give up write locks.  the i/o lock cannot be held nested
327fa96acadSDave Chinner  * if it is being demoted.
328fa96acadSDave Chinner  */
329fa96acadSDave Chinner void
330fa96acadSDave Chinner xfs_ilock_demote(
331fa96acadSDave Chinner 	xfs_inode_t		*ip,
332fa96acadSDave Chinner 	uint			lock_flags)
333fa96acadSDave Chinner {
334653c60b6SDave Chinner 	ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL));
335653c60b6SDave Chinner 	ASSERT((lock_flags &
336653c60b6SDave Chinner 		~(XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
337fa96acadSDave Chinner 
338fa96acadSDave Chinner 	if (lock_flags & XFS_ILOCK_EXCL)
339fa96acadSDave Chinner 		mrdemote(&ip->i_lock);
340653c60b6SDave Chinner 	if (lock_flags & XFS_MMAPLOCK_EXCL)
341653c60b6SDave Chinner 		mrdemote(&ip->i_mmaplock);
342fa96acadSDave Chinner 	if (lock_flags & XFS_IOLOCK_EXCL)
34365523218SChristoph Hellwig 		downgrade_write(&VFS_I(ip)->i_rwsem);
344fa96acadSDave Chinner 
345fa96acadSDave Chinner 	trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_);
346fa96acadSDave Chinner }
347fa96acadSDave Chinner 
348742ae1e3SDave Chinner #if defined(DEBUG) || defined(XFS_WARN)
349fa96acadSDave Chinner int
350fa96acadSDave Chinner xfs_isilocked(
351fa96acadSDave Chinner 	xfs_inode_t		*ip,
352fa96acadSDave Chinner 	uint			lock_flags)
353fa96acadSDave Chinner {
354fa96acadSDave Chinner 	if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) {
355fa96acadSDave Chinner 		if (!(lock_flags & XFS_ILOCK_SHARED))
356fa96acadSDave Chinner 			return !!ip->i_lock.mr_writer;
357fa96acadSDave Chinner 		return rwsem_is_locked(&ip->i_lock.mr_lock);
358fa96acadSDave Chinner 	}
359fa96acadSDave Chinner 
360653c60b6SDave Chinner 	if (lock_flags & (XFS_MMAPLOCK_EXCL|XFS_MMAPLOCK_SHARED)) {
361653c60b6SDave Chinner 		if (!(lock_flags & XFS_MMAPLOCK_SHARED))
362653c60b6SDave Chinner 			return !!ip->i_mmaplock.mr_writer;
363653c60b6SDave Chinner 		return rwsem_is_locked(&ip->i_mmaplock.mr_lock);
364653c60b6SDave Chinner 	}
365653c60b6SDave Chinner 
366fa96acadSDave Chinner 	if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) {
367fa96acadSDave Chinner 		if (!(lock_flags & XFS_IOLOCK_SHARED))
36865523218SChristoph Hellwig 			return !debug_locks ||
36965523218SChristoph Hellwig 				lockdep_is_held_type(&VFS_I(ip)->i_rwsem, 0);
37065523218SChristoph Hellwig 		return rwsem_is_locked(&VFS_I(ip)->i_rwsem);
371fa96acadSDave Chinner 	}
372fa96acadSDave Chinner 
373fa96acadSDave Chinner 	ASSERT(0);
374fa96acadSDave Chinner 	return 0;
375fa96acadSDave Chinner }
376fa96acadSDave Chinner #endif
377fa96acadSDave Chinner 
378b6a9947eSDave Chinner /*
379b6a9947eSDave Chinner  * xfs_lockdep_subclass_ok() is only used in an ASSERT, so is only called when
380b6a9947eSDave Chinner  * DEBUG or XFS_WARN is set. And MAX_LOCKDEP_SUBCLASSES is then only defined
381b6a9947eSDave Chinner  * when CONFIG_LOCKDEP is set. Hence the complex define below to avoid build
382b6a9947eSDave Chinner  * errors and warnings.
383b6a9947eSDave Chinner  */
384b6a9947eSDave Chinner #if (defined(DEBUG) || defined(XFS_WARN)) && defined(CONFIG_LOCKDEP)
3853403ccc0SDave Chinner static bool
3863403ccc0SDave Chinner xfs_lockdep_subclass_ok(
3873403ccc0SDave Chinner 	int subclass)
3883403ccc0SDave Chinner {
3893403ccc0SDave Chinner 	return subclass < MAX_LOCKDEP_SUBCLASSES;
3903403ccc0SDave Chinner }
3913403ccc0SDave Chinner #else
3923403ccc0SDave Chinner #define xfs_lockdep_subclass_ok(subclass)	(true)
3933403ccc0SDave Chinner #endif
3943403ccc0SDave Chinner 
395c24b5dfaSDave Chinner /*
396653c60b6SDave Chinner  * Bump the subclass so xfs_lock_inodes() acquires each lock with a different
3970952c818SDave Chinner  * value. This can be called for any type of inode lock combination, including
3980952c818SDave Chinner  * parent locking. Care must be taken to ensure we don't overrun the subclass
3990952c818SDave Chinner  * storage fields in the class mask we build.
400c24b5dfaSDave Chinner  */
401c24b5dfaSDave Chinner static inline int
402c24b5dfaSDave Chinner xfs_lock_inumorder(int lock_mode, int subclass)
403c24b5dfaSDave Chinner {
4040952c818SDave Chinner 	int	class = 0;
4050952c818SDave Chinner 
4060952c818SDave Chinner 	ASSERT(!(lock_mode & (XFS_ILOCK_PARENT | XFS_ILOCK_RTBITMAP |
4070952c818SDave Chinner 			      XFS_ILOCK_RTSUM)));
4083403ccc0SDave Chinner 	ASSERT(xfs_lockdep_subclass_ok(subclass));
4090952c818SDave Chinner 
410653c60b6SDave Chinner 	if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) {
4110952c818SDave Chinner 		ASSERT(subclass <= XFS_IOLOCK_MAX_SUBCLASS);
4120952c818SDave Chinner 		class += subclass << XFS_IOLOCK_SHIFT;
413653c60b6SDave Chinner 	}
414653c60b6SDave Chinner 
415653c60b6SDave Chinner 	if (lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) {
4160952c818SDave Chinner 		ASSERT(subclass <= XFS_MMAPLOCK_MAX_SUBCLASS);
4170952c818SDave Chinner 		class += subclass << XFS_MMAPLOCK_SHIFT;
418653c60b6SDave Chinner 	}
419653c60b6SDave Chinner 
4200952c818SDave Chinner 	if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) {
4210952c818SDave Chinner 		ASSERT(subclass <= XFS_ILOCK_MAX_SUBCLASS);
4220952c818SDave Chinner 		class += subclass << XFS_ILOCK_SHIFT;
4230952c818SDave Chinner 	}
424c24b5dfaSDave Chinner 
4250952c818SDave Chinner 	return (lock_mode & ~XFS_LOCK_SUBCLASS_MASK) | class;
426c24b5dfaSDave Chinner }
427c24b5dfaSDave Chinner 
428c24b5dfaSDave Chinner /*
42995afcf5cSDave Chinner  * The following routine will lock n inodes in exclusive mode.  We assume the
43095afcf5cSDave Chinner  * caller calls us with the inodes in i_ino order.
431c24b5dfaSDave Chinner  *
43295afcf5cSDave Chinner  * We need to detect deadlock where an inode that we lock is in the AIL and we
43395afcf5cSDave Chinner  * start waiting for another inode that is locked by a thread in a long running
43495afcf5cSDave Chinner  * transaction (such as truncate). This can result in deadlock since the long
43595afcf5cSDave Chinner  * running trans might need to wait for the inode we just locked in order to
43695afcf5cSDave Chinner  * push the tail and free space in the log.
4370952c818SDave Chinner  *
4380952c818SDave Chinner  * xfs_lock_inodes() can only be used to lock one type of lock at a time -
4390952c818SDave Chinner  * the iolock, the mmaplock or the ilock, but not more than one at a time. If we
4400952c818SDave Chinner  * lock more than one at a time, lockdep will report false positives saying we
4410952c818SDave Chinner  * have violated locking orders.
442c24b5dfaSDave Chinner  */
4430d5a75e9SEric Sandeen static void
444c24b5dfaSDave Chinner xfs_lock_inodes(
445efe2330fSChristoph Hellwig 	struct xfs_inode	**ips,
446c24b5dfaSDave Chinner 	int			inodes,
447c24b5dfaSDave Chinner 	uint			lock_mode)
448c24b5dfaSDave Chinner {
449c24b5dfaSDave Chinner 	int			attempts = 0, i, j, try_lock;
450efe2330fSChristoph Hellwig 	struct xfs_log_item	*lp;
451c24b5dfaSDave Chinner 
4520952c818SDave Chinner 	/*
4530952c818SDave Chinner 	 * Currently supports between 2 and 5 inodes with exclusive locking.  We
4540952c818SDave Chinner 	 * support an arbitrary depth of locking here, but absolute limits on
4550952c818SDave Chinner 	 * inodes depend on the the type of locking and the limits placed by
4560952c818SDave Chinner 	 * lockdep annotations in xfs_lock_inumorder.  These are all checked by
4570952c818SDave Chinner 	 * the asserts.
4580952c818SDave Chinner 	 */
45995afcf5cSDave Chinner 	ASSERT(ips && inodes >= 2 && inodes <= 5);
4600952c818SDave Chinner 	ASSERT(lock_mode & (XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL |
4610952c818SDave Chinner 			    XFS_ILOCK_EXCL));
4620952c818SDave Chinner 	ASSERT(!(lock_mode & (XFS_IOLOCK_SHARED | XFS_MMAPLOCK_SHARED |
4630952c818SDave Chinner 			      XFS_ILOCK_SHARED)));
4640952c818SDave Chinner 	ASSERT(!(lock_mode & XFS_MMAPLOCK_EXCL) ||
4650952c818SDave Chinner 		inodes <= XFS_MMAPLOCK_MAX_SUBCLASS + 1);
4660952c818SDave Chinner 	ASSERT(!(lock_mode & XFS_ILOCK_EXCL) ||
4670952c818SDave Chinner 		inodes <= XFS_ILOCK_MAX_SUBCLASS + 1);
4680952c818SDave Chinner 
4690952c818SDave Chinner 	if (lock_mode & XFS_IOLOCK_EXCL) {
4700952c818SDave Chinner 		ASSERT(!(lock_mode & (XFS_MMAPLOCK_EXCL | XFS_ILOCK_EXCL)));
4710952c818SDave Chinner 	} else if (lock_mode & XFS_MMAPLOCK_EXCL)
4720952c818SDave Chinner 		ASSERT(!(lock_mode & XFS_ILOCK_EXCL));
473c24b5dfaSDave Chinner 
474c24b5dfaSDave Chinner 	try_lock = 0;
475c24b5dfaSDave Chinner 	i = 0;
476c24b5dfaSDave Chinner again:
477c24b5dfaSDave Chinner 	for (; i < inodes; i++) {
478c24b5dfaSDave Chinner 		ASSERT(ips[i]);
479c24b5dfaSDave Chinner 
480c24b5dfaSDave Chinner 		if (i && (ips[i] == ips[i - 1]))	/* Already locked */
481c24b5dfaSDave Chinner 			continue;
482c24b5dfaSDave Chinner 
483c24b5dfaSDave Chinner 		/*
48495afcf5cSDave Chinner 		 * If try_lock is not set yet, make sure all locked inodes are
48595afcf5cSDave Chinner 		 * not in the AIL.  If any are, set try_lock to be used later.
486c24b5dfaSDave Chinner 		 */
487c24b5dfaSDave Chinner 		if (!try_lock) {
488c24b5dfaSDave Chinner 			for (j = (i - 1); j >= 0 && !try_lock; j--) {
489b3b14aacSChristoph Hellwig 				lp = &ips[j]->i_itemp->ili_item;
49022525c17SDave Chinner 				if (lp && test_bit(XFS_LI_IN_AIL, &lp->li_flags))
491c24b5dfaSDave Chinner 					try_lock++;
492c24b5dfaSDave Chinner 			}
493c24b5dfaSDave Chinner 		}
494c24b5dfaSDave Chinner 
495c24b5dfaSDave Chinner 		/*
496c24b5dfaSDave Chinner 		 * If any of the previous locks we have locked is in the AIL,
497c24b5dfaSDave Chinner 		 * we must TRY to get the second and subsequent locks. If
498c24b5dfaSDave Chinner 		 * we can't get any, we must release all we have
499c24b5dfaSDave Chinner 		 * and try again.
500c24b5dfaSDave Chinner 		 */
50195afcf5cSDave Chinner 		if (!try_lock) {
50295afcf5cSDave Chinner 			xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
50395afcf5cSDave Chinner 			continue;
50495afcf5cSDave Chinner 		}
505c24b5dfaSDave Chinner 
50695afcf5cSDave Chinner 		/* try_lock means we have an inode locked that is in the AIL. */
507c24b5dfaSDave Chinner 		ASSERT(i != 0);
50895afcf5cSDave Chinner 		if (xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i)))
50995afcf5cSDave Chinner 			continue;
51095afcf5cSDave Chinner 
51195afcf5cSDave Chinner 		/*
51295afcf5cSDave Chinner 		 * Unlock all previous guys and try again.  xfs_iunlock will try
51395afcf5cSDave Chinner 		 * to push the tail if the inode is in the AIL.
51495afcf5cSDave Chinner 		 */
515c24b5dfaSDave Chinner 		attempts++;
516c24b5dfaSDave Chinner 		for (j = i - 1; j >= 0; j--) {
517c24b5dfaSDave Chinner 			/*
51895afcf5cSDave Chinner 			 * Check to see if we've already unlocked this one.  Not
51995afcf5cSDave Chinner 			 * the first one going back, and the inode ptr is the
52095afcf5cSDave Chinner 			 * same.
521c24b5dfaSDave Chinner 			 */
52295afcf5cSDave Chinner 			if (j != (i - 1) && ips[j] == ips[j + 1])
523c24b5dfaSDave Chinner 				continue;
524c24b5dfaSDave Chinner 
525c24b5dfaSDave Chinner 			xfs_iunlock(ips[j], lock_mode);
526c24b5dfaSDave Chinner 		}
527c24b5dfaSDave Chinner 
528c24b5dfaSDave Chinner 		if ((attempts % 5) == 0) {
529c24b5dfaSDave Chinner 			delay(1); /* Don't just spin the CPU */
530c24b5dfaSDave Chinner 		}
531c24b5dfaSDave Chinner 		i = 0;
532c24b5dfaSDave Chinner 		try_lock = 0;
533c24b5dfaSDave Chinner 		goto again;
534c24b5dfaSDave Chinner 	}
535c24b5dfaSDave Chinner }
536c24b5dfaSDave Chinner 
537c24b5dfaSDave Chinner /*
538653c60b6SDave Chinner  * xfs_lock_two_inodes() can only be used to lock one type of lock at a time -
5397c2d238aSDarrick J. Wong  * the mmaplock or the ilock, but not more than one type at a time. If we lock
5407c2d238aSDarrick J. Wong  * more than one at a time, lockdep will report false positives saying we have
5417c2d238aSDarrick J. Wong  * violated locking orders.  The iolock must be double-locked separately since
5427c2d238aSDarrick J. Wong  * we use i_rwsem for that.  We now support taking one lock EXCL and the other
5437c2d238aSDarrick J. Wong  * SHARED.
544c24b5dfaSDave Chinner  */
545c24b5dfaSDave Chinner void
546c24b5dfaSDave Chinner xfs_lock_two_inodes(
5477c2d238aSDarrick J. Wong 	struct xfs_inode	*ip0,
5487c2d238aSDarrick J. Wong 	uint			ip0_mode,
5497c2d238aSDarrick J. Wong 	struct xfs_inode	*ip1,
5507c2d238aSDarrick J. Wong 	uint			ip1_mode)
551c24b5dfaSDave Chinner {
5527c2d238aSDarrick J. Wong 	struct xfs_inode	*temp;
5537c2d238aSDarrick J. Wong 	uint			mode_temp;
554c24b5dfaSDave Chinner 	int			attempts = 0;
555efe2330fSChristoph Hellwig 	struct xfs_log_item	*lp;
556c24b5dfaSDave Chinner 
5577c2d238aSDarrick J. Wong 	ASSERT(hweight32(ip0_mode) == 1);
5587c2d238aSDarrick J. Wong 	ASSERT(hweight32(ip1_mode) == 1);
5597c2d238aSDarrick J. Wong 	ASSERT(!(ip0_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)));
5607c2d238aSDarrick J. Wong 	ASSERT(!(ip1_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)));
5617c2d238aSDarrick J. Wong 	ASSERT(!(ip0_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) ||
5627c2d238aSDarrick J. Wong 	       !(ip0_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
5637c2d238aSDarrick J. Wong 	ASSERT(!(ip1_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) ||
5647c2d238aSDarrick J. Wong 	       !(ip1_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
5657c2d238aSDarrick J. Wong 	ASSERT(!(ip1_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) ||
5667c2d238aSDarrick J. Wong 	       !(ip0_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
5677c2d238aSDarrick J. Wong 	ASSERT(!(ip0_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) ||
5687c2d238aSDarrick J. Wong 	       !(ip1_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
569653c60b6SDave Chinner 
570c24b5dfaSDave Chinner 	ASSERT(ip0->i_ino != ip1->i_ino);
571c24b5dfaSDave Chinner 
572c24b5dfaSDave Chinner 	if (ip0->i_ino > ip1->i_ino) {
573c24b5dfaSDave Chinner 		temp = ip0;
574c24b5dfaSDave Chinner 		ip0 = ip1;
575c24b5dfaSDave Chinner 		ip1 = temp;
5767c2d238aSDarrick J. Wong 		mode_temp = ip0_mode;
5777c2d238aSDarrick J. Wong 		ip0_mode = ip1_mode;
5787c2d238aSDarrick J. Wong 		ip1_mode = mode_temp;
579c24b5dfaSDave Chinner 	}
580c24b5dfaSDave Chinner 
581c24b5dfaSDave Chinner  again:
5827c2d238aSDarrick J. Wong 	xfs_ilock(ip0, xfs_lock_inumorder(ip0_mode, 0));
583c24b5dfaSDave Chinner 
584c24b5dfaSDave Chinner 	/*
585c24b5dfaSDave Chinner 	 * If the first lock we have locked is in the AIL, we must TRY to get
586c24b5dfaSDave Chinner 	 * the second lock. If we can't get it, we must release the first one
587c24b5dfaSDave Chinner 	 * and try again.
588c24b5dfaSDave Chinner 	 */
589b3b14aacSChristoph Hellwig 	lp = &ip0->i_itemp->ili_item;
59022525c17SDave Chinner 	if (lp && test_bit(XFS_LI_IN_AIL, &lp->li_flags)) {
5917c2d238aSDarrick J. Wong 		if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(ip1_mode, 1))) {
5927c2d238aSDarrick J. Wong 			xfs_iunlock(ip0, ip0_mode);
593c24b5dfaSDave Chinner 			if ((++attempts % 5) == 0)
594c24b5dfaSDave Chinner 				delay(1); /* Don't just spin the CPU */
595c24b5dfaSDave Chinner 			goto again;
596c24b5dfaSDave Chinner 		}
597c24b5dfaSDave Chinner 	} else {
5987c2d238aSDarrick J. Wong 		xfs_ilock(ip1, xfs_lock_inumorder(ip1_mode, 1));
599c24b5dfaSDave Chinner 	}
600c24b5dfaSDave Chinner }
601c24b5dfaSDave Chinner 
602fa96acadSDave Chinner void
603fa96acadSDave Chinner __xfs_iflock(
604fa96acadSDave Chinner 	struct xfs_inode	*ip)
605fa96acadSDave Chinner {
606fa96acadSDave Chinner 	wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IFLOCK_BIT);
607fa96acadSDave Chinner 	DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IFLOCK_BIT);
608fa96acadSDave Chinner 
609fa96acadSDave Chinner 	do {
61021417136SIngo Molnar 		prepare_to_wait_exclusive(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
611fa96acadSDave Chinner 		if (xfs_isiflocked(ip))
612fa96acadSDave Chinner 			io_schedule();
613fa96acadSDave Chinner 	} while (!xfs_iflock_nowait(ip));
614fa96acadSDave Chinner 
61521417136SIngo Molnar 	finish_wait(wq, &wait.wq_entry);
616fa96acadSDave Chinner }
617fa96acadSDave Chinner 
6181da177e4SLinus Torvalds STATIC uint
6191da177e4SLinus Torvalds _xfs_dic2xflags(
620c8ce540dSDarrick J. Wong 	uint16_t		di_flags,
62158f88ca2SDave Chinner 	uint64_t		di_flags2,
62258f88ca2SDave Chinner 	bool			has_attr)
6231da177e4SLinus Torvalds {
6241da177e4SLinus Torvalds 	uint			flags = 0;
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds 	if (di_flags & XFS_DIFLAG_ANY) {
6271da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_REALTIME)
628e7b89481SDave Chinner 			flags |= FS_XFLAG_REALTIME;
6291da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_PREALLOC)
630e7b89481SDave Chinner 			flags |= FS_XFLAG_PREALLOC;
6311da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_IMMUTABLE)
632e7b89481SDave Chinner 			flags |= FS_XFLAG_IMMUTABLE;
6331da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_APPEND)
634e7b89481SDave Chinner 			flags |= FS_XFLAG_APPEND;
6351da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_SYNC)
636e7b89481SDave Chinner 			flags |= FS_XFLAG_SYNC;
6371da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_NOATIME)
638e7b89481SDave Chinner 			flags |= FS_XFLAG_NOATIME;
6391da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_NODUMP)
640e7b89481SDave Chinner 			flags |= FS_XFLAG_NODUMP;
6411da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_RTINHERIT)
642e7b89481SDave Chinner 			flags |= FS_XFLAG_RTINHERIT;
6431da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_PROJINHERIT)
644e7b89481SDave Chinner 			flags |= FS_XFLAG_PROJINHERIT;
6451da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_NOSYMLINKS)
646e7b89481SDave Chinner 			flags |= FS_XFLAG_NOSYMLINKS;
647dd9f438eSNathan Scott 		if (di_flags & XFS_DIFLAG_EXTSIZE)
648e7b89481SDave Chinner 			flags |= FS_XFLAG_EXTSIZE;
649dd9f438eSNathan Scott 		if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
650e7b89481SDave Chinner 			flags |= FS_XFLAG_EXTSZINHERIT;
651d3446eacSBarry Naujok 		if (di_flags & XFS_DIFLAG_NODEFRAG)
652e7b89481SDave Chinner 			flags |= FS_XFLAG_NODEFRAG;
6532a82b8beSDavid Chinner 		if (di_flags & XFS_DIFLAG_FILESTREAM)
654e7b89481SDave Chinner 			flags |= FS_XFLAG_FILESTREAM;
6551da177e4SLinus Torvalds 	}
6561da177e4SLinus Torvalds 
65758f88ca2SDave Chinner 	if (di_flags2 & XFS_DIFLAG2_ANY) {
65858f88ca2SDave Chinner 		if (di_flags2 & XFS_DIFLAG2_DAX)
65958f88ca2SDave Chinner 			flags |= FS_XFLAG_DAX;
660f7ca3522SDarrick J. Wong 		if (di_flags2 & XFS_DIFLAG2_COWEXTSIZE)
661f7ca3522SDarrick J. Wong 			flags |= FS_XFLAG_COWEXTSIZE;
66258f88ca2SDave Chinner 	}
66358f88ca2SDave Chinner 
66458f88ca2SDave Chinner 	if (has_attr)
66558f88ca2SDave Chinner 		flags |= FS_XFLAG_HASATTR;
66658f88ca2SDave Chinner 
6671da177e4SLinus Torvalds 	return flags;
6681da177e4SLinus Torvalds }
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds uint
6711da177e4SLinus Torvalds xfs_ip2xflags(
67258f88ca2SDave Chinner 	struct xfs_inode	*ip)
6731da177e4SLinus Torvalds {
67458f88ca2SDave Chinner 	struct xfs_icdinode	*dic = &ip->i_d;
6751da177e4SLinus Torvalds 
67658f88ca2SDave Chinner 	return _xfs_dic2xflags(dic->di_flags, dic->di_flags2, XFS_IFORK_Q(ip));
6771da177e4SLinus Torvalds }
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds /*
680c24b5dfaSDave Chinner  * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
681c24b5dfaSDave Chinner  * is allowed, otherwise it has to be an exact match. If a CI match is found,
682c24b5dfaSDave Chinner  * ci_name->name will point to a the actual name (caller must free) or
683c24b5dfaSDave Chinner  * will be set to NULL if an exact match is found.
684c24b5dfaSDave Chinner  */
685c24b5dfaSDave Chinner int
686c24b5dfaSDave Chinner xfs_lookup(
687c24b5dfaSDave Chinner 	xfs_inode_t		*dp,
688c24b5dfaSDave Chinner 	struct xfs_name		*name,
689c24b5dfaSDave Chinner 	xfs_inode_t		**ipp,
690c24b5dfaSDave Chinner 	struct xfs_name		*ci_name)
691c24b5dfaSDave Chinner {
692c24b5dfaSDave Chinner 	xfs_ino_t		inum;
693c24b5dfaSDave Chinner 	int			error;
694c24b5dfaSDave Chinner 
695c24b5dfaSDave Chinner 	trace_xfs_lookup(dp, name);
696c24b5dfaSDave Chinner 
697c24b5dfaSDave Chinner 	if (XFS_FORCED_SHUTDOWN(dp->i_mount))
6982451337dSDave Chinner 		return -EIO;
699c24b5dfaSDave Chinner 
700c24b5dfaSDave Chinner 	error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
701c24b5dfaSDave Chinner 	if (error)
702dbad7c99SDave Chinner 		goto out_unlock;
703c24b5dfaSDave Chinner 
704c24b5dfaSDave Chinner 	error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
705c24b5dfaSDave Chinner 	if (error)
706c24b5dfaSDave Chinner 		goto out_free_name;
707c24b5dfaSDave Chinner 
708c24b5dfaSDave Chinner 	return 0;
709c24b5dfaSDave Chinner 
710c24b5dfaSDave Chinner out_free_name:
711c24b5dfaSDave Chinner 	if (ci_name)
712c24b5dfaSDave Chinner 		kmem_free(ci_name->name);
713dbad7c99SDave Chinner out_unlock:
714c24b5dfaSDave Chinner 	*ipp = NULL;
715c24b5dfaSDave Chinner 	return error;
716c24b5dfaSDave Chinner }
717c24b5dfaSDave Chinner 
718c24b5dfaSDave Chinner /*
7191da177e4SLinus Torvalds  * Allocate an inode on disk and return a copy of its in-core version.
7201da177e4SLinus Torvalds  * The in-core inode is locked exclusively.  Set mode, nlink, and rdev
7211da177e4SLinus Torvalds  * appropriately within the inode.  The uid and gid for the inode are
7221da177e4SLinus Torvalds  * set according to the contents of the given cred structure.
7231da177e4SLinus Torvalds  *
7241da177e4SLinus Torvalds  * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
725cd856db6SCarlos Maiolino  * has a free inode available, call xfs_iget() to obtain the in-core
726cd856db6SCarlos Maiolino  * version of the allocated inode.  Finally, fill in the inode and
727cd856db6SCarlos Maiolino  * log its initial contents.  In this case, ialloc_context would be
728cd856db6SCarlos Maiolino  * set to NULL.
7291da177e4SLinus Torvalds  *
730cd856db6SCarlos Maiolino  * If xfs_dialloc() does not have an available inode, it will replenish
731cd856db6SCarlos Maiolino  * its supply by doing an allocation. Since we can only do one
732cd856db6SCarlos Maiolino  * allocation within a transaction without deadlocks, we must commit
733cd856db6SCarlos Maiolino  * the current transaction before returning the inode itself.
734cd856db6SCarlos Maiolino  * In this case, therefore, we will set ialloc_context and return.
7351da177e4SLinus Torvalds  * The caller should then commit the current transaction, start a new
7361da177e4SLinus Torvalds  * transaction, and call xfs_ialloc() again to actually get the inode.
7371da177e4SLinus Torvalds  *
7381da177e4SLinus Torvalds  * To ensure that some other process does not grab the inode that
7391da177e4SLinus Torvalds  * was allocated during the first call to xfs_ialloc(), this routine
7401da177e4SLinus Torvalds  * also returns the [locked] bp pointing to the head of the freelist
7411da177e4SLinus Torvalds  * as ialloc_context.  The caller should hold this buffer across
7421da177e4SLinus Torvalds  * the commit and pass it back into this routine on the second call.
743b11f94d5SDavid Chinner  *
744b11f94d5SDavid Chinner  * If we are allocating quota inodes, we do not have a parent inode
745b11f94d5SDavid Chinner  * to attach to or associate with (i.e. pip == NULL) because they
746b11f94d5SDavid Chinner  * are not linked into the directory structure - they are attached
747b11f94d5SDavid Chinner  * directly to the superblock - and so have no parent.
7481da177e4SLinus Torvalds  */
7490d5a75e9SEric Sandeen static int
7501da177e4SLinus Torvalds xfs_ialloc(
7511da177e4SLinus Torvalds 	xfs_trans_t	*tp,
7521da177e4SLinus Torvalds 	xfs_inode_t	*pip,
753576b1d67SAl Viro 	umode_t		mode,
75431b084aeSNathan Scott 	xfs_nlink_t	nlink,
75566f36464SChristoph Hellwig 	dev_t		rdev,
7566743099cSArkadiusz Mi?kiewicz 	prid_t		prid,
7571da177e4SLinus Torvalds 	xfs_buf_t	**ialloc_context,
7581da177e4SLinus Torvalds 	xfs_inode_t	**ipp)
7591da177e4SLinus Torvalds {
76093848a99SChristoph Hellwig 	struct xfs_mount *mp = tp->t_mountp;
7611da177e4SLinus Torvalds 	xfs_ino_t	ino;
7621da177e4SLinus Torvalds 	xfs_inode_t	*ip;
7631da177e4SLinus Torvalds 	uint		flags;
7641da177e4SLinus Torvalds 	int		error;
76595582b00SDeepa Dinamani 	struct timespec64 tv;
7663987848cSDave Chinner 	struct inode	*inode;
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds 	/*
7691da177e4SLinus Torvalds 	 * Call the space management code to pick
7701da177e4SLinus Torvalds 	 * the on-disk inode to be allocated.
7711da177e4SLinus Torvalds 	 */
772f59cf5c2SChristoph Hellwig 	error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode,
77308358906SChristoph Hellwig 			    ialloc_context, &ino);
774bf904248SDavid Chinner 	if (error)
7751da177e4SLinus Torvalds 		return error;
77608358906SChristoph Hellwig 	if (*ialloc_context || ino == NULLFSINO) {
7771da177e4SLinus Torvalds 		*ipp = NULL;
7781da177e4SLinus Torvalds 		return 0;
7791da177e4SLinus Torvalds 	}
7801da177e4SLinus Torvalds 	ASSERT(*ialloc_context == NULL);
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 	/*
7838b26984dSDave Chinner 	 * Protect against obviously corrupt allocation btree records. Later
7848b26984dSDave Chinner 	 * xfs_iget checks will catch re-allocation of other active in-memory
7858b26984dSDave Chinner 	 * and on-disk inodes. If we don't catch reallocating the parent inode
7868b26984dSDave Chinner 	 * here we will deadlock in xfs_iget() so we have to do these checks
7878b26984dSDave Chinner 	 * first.
7888b26984dSDave Chinner 	 */
7898b26984dSDave Chinner 	if ((pip && ino == pip->i_ino) || !xfs_verify_dir_ino(mp, ino)) {
7908b26984dSDave Chinner 		xfs_alert(mp, "Allocated a known in-use inode 0x%llx!", ino);
7918b26984dSDave Chinner 		return -EFSCORRUPTED;
7928b26984dSDave Chinner 	}
7938b26984dSDave Chinner 
7948b26984dSDave Chinner 	/*
7951da177e4SLinus Torvalds 	 * Get the in-core inode with the lock held exclusively.
7961da177e4SLinus Torvalds 	 * This is because we're setting fields here we need
7971da177e4SLinus Torvalds 	 * to prevent others from looking at until we're done.
7981da177e4SLinus Torvalds 	 */
79993848a99SChristoph Hellwig 	error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE,
800ec3ba85fSChristoph Hellwig 			 XFS_ILOCK_EXCL, &ip);
801bf904248SDavid Chinner 	if (error)
8021da177e4SLinus Torvalds 		return error;
8031da177e4SLinus Torvalds 	ASSERT(ip != NULL);
8043987848cSDave Chinner 	inode = VFS_I(ip);
805c19b3b05SDave Chinner 	inode->i_mode = mode;
80654d7b5c1SDave Chinner 	set_nlink(inode, nlink);
8073d8f2821SChristoph Hellwig 	inode->i_uid = current_fsuid();
80866f36464SChristoph Hellwig 	inode->i_rdev = rdev;
809de7a866fSChristoph Hellwig 	ip->i_d.di_projid = prid;
8101da177e4SLinus Torvalds 
811bd186aa9SChristoph Hellwig 	if (pip && XFS_INHERIT_GID(pip)) {
8123d8f2821SChristoph Hellwig 		inode->i_gid = VFS_I(pip)->i_gid;
813c19b3b05SDave Chinner 		if ((VFS_I(pip)->i_mode & S_ISGID) && S_ISDIR(mode))
814c19b3b05SDave Chinner 			inode->i_mode |= S_ISGID;
8153d8f2821SChristoph Hellwig 	} else {
8163d8f2821SChristoph Hellwig 		inode->i_gid = current_fsgid();
8171da177e4SLinus Torvalds 	}
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 	/*
8201da177e4SLinus Torvalds 	 * If the group ID of the new file does not match the effective group
8211da177e4SLinus Torvalds 	 * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
8221da177e4SLinus Torvalds 	 * (and only if the irix_sgid_inherit compatibility variable is set).
8231da177e4SLinus Torvalds 	 */
82454295159SChristoph Hellwig 	if (irix_sgid_inherit &&
82554295159SChristoph Hellwig 	    (inode->i_mode & S_ISGID) && !in_group_p(inode->i_gid))
826c19b3b05SDave Chinner 		inode->i_mode &= ~S_ISGID;
8271da177e4SLinus Torvalds 
8281da177e4SLinus Torvalds 	ip->i_d.di_size = 0;
829daf83964SChristoph Hellwig 	ip->i_df.if_nextents = 0;
8301da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_nblocks == 0);
831dff35fd4SChristoph Hellwig 
832c2050a45SDeepa Dinamani 	tv = current_time(inode);
8333987848cSDave Chinner 	inode->i_mtime = tv;
8343987848cSDave Chinner 	inode->i_atime = tv;
8353987848cSDave Chinner 	inode->i_ctime = tv;
836dff35fd4SChristoph Hellwig 
8371da177e4SLinus Torvalds 	ip->i_d.di_extsize = 0;
8381da177e4SLinus Torvalds 	ip->i_d.di_dmevmask = 0;
8391da177e4SLinus Torvalds 	ip->i_d.di_dmstate = 0;
8401da177e4SLinus Torvalds 	ip->i_d.di_flags = 0;
84193848a99SChristoph Hellwig 
8426471e9c5SChristoph Hellwig 	if (xfs_sb_version_has_v3inode(&mp->m_sb)) {
843f0e28280SJeff Layton 		inode_set_iversion(inode, 1);
84493848a99SChristoph Hellwig 		ip->i_d.di_flags2 = 0;
845f7ca3522SDarrick J. Wong 		ip->i_d.di_cowextsize = 0;
8468d2d878dSChristoph Hellwig 		ip->i_d.di_crtime = tv;
84793848a99SChristoph Hellwig 	}
84893848a99SChristoph Hellwig 
8491da177e4SLinus Torvalds 	flags = XFS_ILOG_CORE;
8501da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
8511da177e4SLinus Torvalds 	case S_IFIFO:
8521da177e4SLinus Torvalds 	case S_IFCHR:
8531da177e4SLinus Torvalds 	case S_IFBLK:
8541da177e4SLinus Torvalds 	case S_IFSOCK:
855f7e67b20SChristoph Hellwig 		ip->i_df.if_format = XFS_DINODE_FMT_DEV;
8561da177e4SLinus Torvalds 		ip->i_df.if_flags = 0;
8571da177e4SLinus Torvalds 		flags |= XFS_ILOG_DEV;
8581da177e4SLinus Torvalds 		break;
8591da177e4SLinus Torvalds 	case S_IFREG:
8601da177e4SLinus Torvalds 	case S_IFDIR:
861b11f94d5SDavid Chinner 		if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
862365ca83dSNathan Scott 			uint		di_flags = 0;
863365ca83dSNathan Scott 
864abbede1bSAl Viro 			if (S_ISDIR(mode)) {
865365ca83dSNathan Scott 				if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
866365ca83dSNathan Scott 					di_flags |= XFS_DIFLAG_RTINHERIT;
867dd9f438eSNathan Scott 				if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
868dd9f438eSNathan Scott 					di_flags |= XFS_DIFLAG_EXTSZINHERIT;
869dd9f438eSNathan Scott 					ip->i_d.di_extsize = pip->i_d.di_extsize;
870dd9f438eSNathan Scott 				}
8719336e3a7SDave Chinner 				if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
8729336e3a7SDave Chinner 					di_flags |= XFS_DIFLAG_PROJINHERIT;
873abbede1bSAl Viro 			} else if (S_ISREG(mode)) {
874613d7043SChristoph Hellwig 				if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
875365ca83dSNathan Scott 					di_flags |= XFS_DIFLAG_REALTIME;
876dd9f438eSNathan Scott 				if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
877dd9f438eSNathan Scott 					di_flags |= XFS_DIFLAG_EXTSIZE;
878dd9f438eSNathan Scott 					ip->i_d.di_extsize = pip->i_d.di_extsize;
879dd9f438eSNathan Scott 				}
8801da177e4SLinus Torvalds 			}
8811da177e4SLinus Torvalds 			if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
8821da177e4SLinus Torvalds 			    xfs_inherit_noatime)
883365ca83dSNathan Scott 				di_flags |= XFS_DIFLAG_NOATIME;
8841da177e4SLinus Torvalds 			if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
8851da177e4SLinus Torvalds 			    xfs_inherit_nodump)
886365ca83dSNathan Scott 				di_flags |= XFS_DIFLAG_NODUMP;
8871da177e4SLinus Torvalds 			if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
8881da177e4SLinus Torvalds 			    xfs_inherit_sync)
889365ca83dSNathan Scott 				di_flags |= XFS_DIFLAG_SYNC;
8901da177e4SLinus Torvalds 			if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
8911da177e4SLinus Torvalds 			    xfs_inherit_nosymlinks)
892365ca83dSNathan Scott 				di_flags |= XFS_DIFLAG_NOSYMLINKS;
893d3446eacSBarry Naujok 			if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
894d3446eacSBarry Naujok 			    xfs_inherit_nodefrag)
895d3446eacSBarry Naujok 				di_flags |= XFS_DIFLAG_NODEFRAG;
8962a82b8beSDavid Chinner 			if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
8972a82b8beSDavid Chinner 				di_flags |= XFS_DIFLAG_FILESTREAM;
89858f88ca2SDave Chinner 
899365ca83dSNathan Scott 			ip->i_d.di_flags |= di_flags;
9001da177e4SLinus Torvalds 		}
901b3d1d375SChristoph Hellwig 		if (pip && (pip->i_d.di_flags2 & XFS_DIFLAG2_ANY)) {
902f7ca3522SDarrick J. Wong 			if (pip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) {
903b3d1d375SChristoph Hellwig 				ip->i_d.di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
904f7ca3522SDarrick J. Wong 				ip->i_d.di_cowextsize = pip->i_d.di_cowextsize;
905f7ca3522SDarrick J. Wong 			}
90656bdf855SLukas Czerner 			if (pip->i_d.di_flags2 & XFS_DIFLAG2_DAX)
907b3d1d375SChristoph Hellwig 				ip->i_d.di_flags2 |= XFS_DIFLAG2_DAX;
908f7ca3522SDarrick J. Wong 		}
9091da177e4SLinus Torvalds 		/* FALLTHROUGH */
9101da177e4SLinus Torvalds 	case S_IFLNK:
911f7e67b20SChristoph Hellwig 		ip->i_df.if_format = XFS_DINODE_FMT_EXTENTS;
9121da177e4SLinus Torvalds 		ip->i_df.if_flags = XFS_IFEXTENTS;
913fcacbc3fSChristoph Hellwig 		ip->i_df.if_bytes = 0;
9146bdcf26aSChristoph Hellwig 		ip->i_df.if_u1.if_root = NULL;
9151da177e4SLinus Torvalds 		break;
9161da177e4SLinus Torvalds 	default:
9171da177e4SLinus Torvalds 		ASSERT(0);
9181da177e4SLinus Torvalds 	}
9191da177e4SLinus Torvalds 
9201da177e4SLinus Torvalds 	/*
9211da177e4SLinus Torvalds 	 * Log the new values stuffed into the inode.
9221da177e4SLinus Torvalds 	 */
923ddc3415aSChristoph Hellwig 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
9241da177e4SLinus Torvalds 	xfs_trans_log_inode(tp, ip, flags);
9251da177e4SLinus Torvalds 
92658c90473SDave Chinner 	/* now that we have an i_mode we can setup the inode structure */
92741be8bedSChristoph Hellwig 	xfs_setup_inode(ip);
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds 	*ipp = ip;
9301da177e4SLinus Torvalds 	return 0;
9311da177e4SLinus Torvalds }
9321da177e4SLinus Torvalds 
933e546cb79SDave Chinner /*
934e546cb79SDave Chinner  * Allocates a new inode from disk and return a pointer to the
935e546cb79SDave Chinner  * incore copy. This routine will internally commit the current
936e546cb79SDave Chinner  * transaction and allocate a new one if the Space Manager needed
937e546cb79SDave Chinner  * to do an allocation to replenish the inode free-list.
938e546cb79SDave Chinner  *
939e546cb79SDave Chinner  * This routine is designed to be called from xfs_create and
940e546cb79SDave Chinner  * xfs_create_dir.
941e546cb79SDave Chinner  *
942e546cb79SDave Chinner  */
943e546cb79SDave Chinner int
944e546cb79SDave Chinner xfs_dir_ialloc(
945e546cb79SDave Chinner 	xfs_trans_t	**tpp,		/* input: current transaction;
946e546cb79SDave Chinner 					   output: may be a new transaction. */
947e546cb79SDave Chinner 	xfs_inode_t	*dp,		/* directory within whose allocate
948e546cb79SDave Chinner 					   the inode. */
949e546cb79SDave Chinner 	umode_t		mode,
950e546cb79SDave Chinner 	xfs_nlink_t	nlink,
95166f36464SChristoph Hellwig 	dev_t		rdev,
952e546cb79SDave Chinner 	prid_t		prid,		/* project id */
953c959025eSChandan Rajendra 	xfs_inode_t	**ipp)		/* pointer to inode; it will be
954e546cb79SDave Chinner 					   locked. */
955e546cb79SDave Chinner {
956e546cb79SDave Chinner 	xfs_trans_t	*tp;
957e546cb79SDave Chinner 	xfs_inode_t	*ip;
958e546cb79SDave Chinner 	xfs_buf_t	*ialloc_context = NULL;
959e546cb79SDave Chinner 	int		code;
960e546cb79SDave Chinner 	void		*dqinfo;
961e546cb79SDave Chinner 	uint		tflags;
962e546cb79SDave Chinner 
963e546cb79SDave Chinner 	tp = *tpp;
964e546cb79SDave Chinner 	ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
965e546cb79SDave Chinner 
966e546cb79SDave Chinner 	/*
967e546cb79SDave Chinner 	 * xfs_ialloc will return a pointer to an incore inode if
968e546cb79SDave Chinner 	 * the Space Manager has an available inode on the free
969e546cb79SDave Chinner 	 * list. Otherwise, it will do an allocation and replenish
970e546cb79SDave Chinner 	 * the freelist.  Since we can only do one allocation per
971e546cb79SDave Chinner 	 * transaction without deadlocks, we will need to commit the
972e546cb79SDave Chinner 	 * current transaction and start a new one.  We will then
973e546cb79SDave Chinner 	 * need to call xfs_ialloc again to get the inode.
974e546cb79SDave Chinner 	 *
975e546cb79SDave Chinner 	 * If xfs_ialloc did an allocation to replenish the freelist,
976e546cb79SDave Chinner 	 * it returns the bp containing the head of the freelist as
977e546cb79SDave Chinner 	 * ialloc_context. We will hold a lock on it across the
978e546cb79SDave Chinner 	 * transaction commit so that no other process can steal
979e546cb79SDave Chinner 	 * the inode(s) that we've just allocated.
980e546cb79SDave Chinner 	 */
981f59cf5c2SChristoph Hellwig 	code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid, &ialloc_context,
982f59cf5c2SChristoph Hellwig 			&ip);
983e546cb79SDave Chinner 
984e546cb79SDave Chinner 	/*
985e546cb79SDave Chinner 	 * Return an error if we were unable to allocate a new inode.
986e546cb79SDave Chinner 	 * This should only happen if we run out of space on disk or
987e546cb79SDave Chinner 	 * encounter a disk error.
988e546cb79SDave Chinner 	 */
989e546cb79SDave Chinner 	if (code) {
990e546cb79SDave Chinner 		*ipp = NULL;
991e546cb79SDave Chinner 		return code;
992e546cb79SDave Chinner 	}
993e546cb79SDave Chinner 	if (!ialloc_context && !ip) {
994e546cb79SDave Chinner 		*ipp = NULL;
9952451337dSDave Chinner 		return -ENOSPC;
996e546cb79SDave Chinner 	}
997e546cb79SDave Chinner 
998e546cb79SDave Chinner 	/*
999e546cb79SDave Chinner 	 * If the AGI buffer is non-NULL, then we were unable to get an
1000e546cb79SDave Chinner 	 * inode in one operation.  We need to commit the current
1001e546cb79SDave Chinner 	 * transaction and call xfs_ialloc() again.  It is guaranteed
1002e546cb79SDave Chinner 	 * to succeed the second time.
1003e546cb79SDave Chinner 	 */
1004e546cb79SDave Chinner 	if (ialloc_context) {
1005e546cb79SDave Chinner 		/*
1006e546cb79SDave Chinner 		 * Normally, xfs_trans_commit releases all the locks.
1007e546cb79SDave Chinner 		 * We call bhold to hang on to the ialloc_context across
1008e546cb79SDave Chinner 		 * the commit.  Holding this buffer prevents any other
1009e546cb79SDave Chinner 		 * processes from doing any allocations in this
1010e546cb79SDave Chinner 		 * allocation group.
1011e546cb79SDave Chinner 		 */
1012e546cb79SDave Chinner 		xfs_trans_bhold(tp, ialloc_context);
1013e546cb79SDave Chinner 
1014e546cb79SDave Chinner 		/*
1015e546cb79SDave Chinner 		 * We want the quota changes to be associated with the next
1016e546cb79SDave Chinner 		 * transaction, NOT this one. So, detach the dqinfo from this
1017e546cb79SDave Chinner 		 * and attach it to the next transaction.
1018e546cb79SDave Chinner 		 */
1019e546cb79SDave Chinner 		dqinfo = NULL;
1020e546cb79SDave Chinner 		tflags = 0;
1021e546cb79SDave Chinner 		if (tp->t_dqinfo) {
1022e546cb79SDave Chinner 			dqinfo = (void *)tp->t_dqinfo;
1023e546cb79SDave Chinner 			tp->t_dqinfo = NULL;
1024e546cb79SDave Chinner 			tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY;
1025e546cb79SDave Chinner 			tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY);
1026e546cb79SDave Chinner 		}
1027e546cb79SDave Chinner 
1028411350dfSChristoph Hellwig 		code = xfs_trans_roll(&tp);
10293d3c8b52SJie Liu 
1030e546cb79SDave Chinner 		/*
1031e546cb79SDave Chinner 		 * Re-attach the quota info that we detached from prev trx.
1032e546cb79SDave Chinner 		 */
1033e546cb79SDave Chinner 		if (dqinfo) {
1034e546cb79SDave Chinner 			tp->t_dqinfo = dqinfo;
1035e546cb79SDave Chinner 			tp->t_flags |= tflags;
1036e546cb79SDave Chinner 		}
1037e546cb79SDave Chinner 
1038e546cb79SDave Chinner 		if (code) {
1039e546cb79SDave Chinner 			xfs_buf_relse(ialloc_context);
10402e6db6c4SChristoph Hellwig 			*tpp = tp;
1041e546cb79SDave Chinner 			*ipp = NULL;
1042e546cb79SDave Chinner 			return code;
1043e546cb79SDave Chinner 		}
1044e546cb79SDave Chinner 		xfs_trans_bjoin(tp, ialloc_context);
1045e546cb79SDave Chinner 
1046e546cb79SDave Chinner 		/*
1047e546cb79SDave Chinner 		 * Call ialloc again. Since we've locked out all
1048e546cb79SDave Chinner 		 * other allocations in this allocation group,
1049e546cb79SDave Chinner 		 * this call should always succeed.
1050e546cb79SDave Chinner 		 */
1051e546cb79SDave Chinner 		code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid,
1052f59cf5c2SChristoph Hellwig 				  &ialloc_context, &ip);
1053e546cb79SDave Chinner 
1054e546cb79SDave Chinner 		/*
1055e546cb79SDave Chinner 		 * If we get an error at this point, return to the caller
1056e546cb79SDave Chinner 		 * so that the current transaction can be aborted.
1057e546cb79SDave Chinner 		 */
1058e546cb79SDave Chinner 		if (code) {
1059e546cb79SDave Chinner 			*tpp = tp;
1060e546cb79SDave Chinner 			*ipp = NULL;
1061e546cb79SDave Chinner 			return code;
1062e546cb79SDave Chinner 		}
1063e546cb79SDave Chinner 		ASSERT(!ialloc_context && ip);
1064e546cb79SDave Chinner 
1065e546cb79SDave Chinner 	}
1066e546cb79SDave Chinner 
1067e546cb79SDave Chinner 	*ipp = ip;
1068e546cb79SDave Chinner 	*tpp = tp;
1069e546cb79SDave Chinner 
1070e546cb79SDave Chinner 	return 0;
1071e546cb79SDave Chinner }
1072e546cb79SDave Chinner 
1073e546cb79SDave Chinner /*
107454d7b5c1SDave Chinner  * Decrement the link count on an inode & log the change.  If this causes the
107554d7b5c1SDave Chinner  * link count to go to zero, move the inode to AGI unlinked list so that it can
107654d7b5c1SDave Chinner  * be freed when the last active reference goes away via xfs_inactive().
1077e546cb79SDave Chinner  */
10780d5a75e9SEric Sandeen static int			/* error */
1079e546cb79SDave Chinner xfs_droplink(
1080e546cb79SDave Chinner 	xfs_trans_t *tp,
1081e546cb79SDave Chinner 	xfs_inode_t *ip)
1082e546cb79SDave Chinner {
1083e546cb79SDave Chinner 	xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1084e546cb79SDave Chinner 
1085e546cb79SDave Chinner 	drop_nlink(VFS_I(ip));
1086e546cb79SDave Chinner 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1087e546cb79SDave Chinner 
108854d7b5c1SDave Chinner 	if (VFS_I(ip)->i_nlink)
108954d7b5c1SDave Chinner 		return 0;
109054d7b5c1SDave Chinner 
109154d7b5c1SDave Chinner 	return xfs_iunlink(tp, ip);
1092e546cb79SDave Chinner }
1093e546cb79SDave Chinner 
1094e546cb79SDave Chinner /*
1095e546cb79SDave Chinner  * Increment the link count on an inode & log the change.
1096e546cb79SDave Chinner  */
109791083269SEric Sandeen static void
1098e546cb79SDave Chinner xfs_bumplink(
1099e546cb79SDave Chinner 	xfs_trans_t *tp,
1100e546cb79SDave Chinner 	xfs_inode_t *ip)
1101e546cb79SDave Chinner {
1102e546cb79SDave Chinner 	xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1103e546cb79SDave Chinner 
1104e546cb79SDave Chinner 	inc_nlink(VFS_I(ip));
1105e546cb79SDave Chinner 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1106e546cb79SDave Chinner }
1107e546cb79SDave Chinner 
1108c24b5dfaSDave Chinner int
1109c24b5dfaSDave Chinner xfs_create(
1110c24b5dfaSDave Chinner 	xfs_inode_t		*dp,
1111c24b5dfaSDave Chinner 	struct xfs_name		*name,
1112c24b5dfaSDave Chinner 	umode_t			mode,
111366f36464SChristoph Hellwig 	dev_t			rdev,
1114c24b5dfaSDave Chinner 	xfs_inode_t		**ipp)
1115c24b5dfaSDave Chinner {
1116c24b5dfaSDave Chinner 	int			is_dir = S_ISDIR(mode);
1117c24b5dfaSDave Chinner 	struct xfs_mount	*mp = dp->i_mount;
1118c24b5dfaSDave Chinner 	struct xfs_inode	*ip = NULL;
1119c24b5dfaSDave Chinner 	struct xfs_trans	*tp = NULL;
1120c24b5dfaSDave Chinner 	int			error;
1121c24b5dfaSDave Chinner 	bool                    unlock_dp_on_error = false;
1122c24b5dfaSDave Chinner 	prid_t			prid;
1123c24b5dfaSDave Chinner 	struct xfs_dquot	*udqp = NULL;
1124c24b5dfaSDave Chinner 	struct xfs_dquot	*gdqp = NULL;
1125c24b5dfaSDave Chinner 	struct xfs_dquot	*pdqp = NULL;
1126062647a8SBrian Foster 	struct xfs_trans_res	*tres;
1127c24b5dfaSDave Chinner 	uint			resblks;
1128c24b5dfaSDave Chinner 
1129c24b5dfaSDave Chinner 	trace_xfs_create(dp, name);
1130c24b5dfaSDave Chinner 
1131c24b5dfaSDave Chinner 	if (XFS_FORCED_SHUTDOWN(mp))
11322451337dSDave Chinner 		return -EIO;
1133c24b5dfaSDave Chinner 
1134163467d3SZhi Yong Wu 	prid = xfs_get_initial_prid(dp);
1135c24b5dfaSDave Chinner 
1136c24b5dfaSDave Chinner 	/*
1137c24b5dfaSDave Chinner 	 * Make sure that we have allocated dquot(s) on disk.
1138c24b5dfaSDave Chinner 	 */
113954295159SChristoph Hellwig 	error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
1140c24b5dfaSDave Chinner 					XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
1141c24b5dfaSDave Chinner 					&udqp, &gdqp, &pdqp);
1142c24b5dfaSDave Chinner 	if (error)
1143c24b5dfaSDave Chinner 		return error;
1144c24b5dfaSDave Chinner 
1145c24b5dfaSDave Chinner 	if (is_dir) {
1146c24b5dfaSDave Chinner 		resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
1147062647a8SBrian Foster 		tres = &M_RES(mp)->tr_mkdir;
1148c24b5dfaSDave Chinner 	} else {
1149c24b5dfaSDave Chinner 		resblks = XFS_CREATE_SPACE_RES(mp, name->len);
1150062647a8SBrian Foster 		tres = &M_RES(mp)->tr_create;
1151c24b5dfaSDave Chinner 	}
1152c24b5dfaSDave Chinner 
1153c24b5dfaSDave Chinner 	/*
1154c24b5dfaSDave Chinner 	 * Initially assume that the file does not exist and
1155c24b5dfaSDave Chinner 	 * reserve the resources for that case.  If that is not
1156c24b5dfaSDave Chinner 	 * the case we'll drop the one we have and get a more
1157c24b5dfaSDave Chinner 	 * appropriate transaction later.
1158c24b5dfaSDave Chinner 	 */
1159253f4911SChristoph Hellwig 	error = xfs_trans_alloc(mp, tres, resblks, 0, 0, &tp);
11602451337dSDave Chinner 	if (error == -ENOSPC) {
1161c24b5dfaSDave Chinner 		/* flush outstanding delalloc blocks and retry */
1162c24b5dfaSDave Chinner 		xfs_flush_inodes(mp);
1163253f4911SChristoph Hellwig 		error = xfs_trans_alloc(mp, tres, resblks, 0, 0, &tp);
1164c24b5dfaSDave Chinner 	}
11654906e215SChristoph Hellwig 	if (error)
1166253f4911SChristoph Hellwig 		goto out_release_inode;
1167c24b5dfaSDave Chinner 
116865523218SChristoph Hellwig 	xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1169c24b5dfaSDave Chinner 	unlock_dp_on_error = true;
1170c24b5dfaSDave Chinner 
1171c24b5dfaSDave Chinner 	/*
1172c24b5dfaSDave Chinner 	 * Reserve disk quota and the inode.
1173c24b5dfaSDave Chinner 	 */
1174c24b5dfaSDave Chinner 	error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
1175c24b5dfaSDave Chinner 						pdqp, resblks, 1, 0);
1176c24b5dfaSDave Chinner 	if (error)
1177c24b5dfaSDave Chinner 		goto out_trans_cancel;
1178c24b5dfaSDave Chinner 
1179c24b5dfaSDave Chinner 	/*
1180c24b5dfaSDave Chinner 	 * A newly created regular or special file just has one directory
1181c24b5dfaSDave Chinner 	 * entry pointing to them, but a directory also the "." entry
1182c24b5dfaSDave Chinner 	 * pointing to itself.
1183c24b5dfaSDave Chinner 	 */
1184c959025eSChandan Rajendra 	error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev, prid, &ip);
1185d6077aa3SJan Kara 	if (error)
1186c24b5dfaSDave Chinner 		goto out_trans_cancel;
1187c24b5dfaSDave Chinner 
1188c24b5dfaSDave Chinner 	/*
1189c24b5dfaSDave Chinner 	 * Now we join the directory inode to the transaction.  We do not do it
1190c24b5dfaSDave Chinner 	 * earlier because xfs_dir_ialloc might commit the previous transaction
1191c24b5dfaSDave Chinner 	 * (and release all the locks).  An error from here on will result in
1192c24b5dfaSDave Chinner 	 * the transaction cancel unlocking dp so don't do it explicitly in the
1193c24b5dfaSDave Chinner 	 * error path.
1194c24b5dfaSDave Chinner 	 */
119565523218SChristoph Hellwig 	xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1196c24b5dfaSDave Chinner 	unlock_dp_on_error = false;
1197c24b5dfaSDave Chinner 
1198381eee69SBrian Foster 	error = xfs_dir_createname(tp, dp, name, ip->i_ino,
119963337b63SKaixu Xia 					resblks - XFS_IALLOC_SPACE_RES(mp));
1200c24b5dfaSDave Chinner 	if (error) {
12012451337dSDave Chinner 		ASSERT(error != -ENOSPC);
12024906e215SChristoph Hellwig 		goto out_trans_cancel;
1203c24b5dfaSDave Chinner 	}
1204c24b5dfaSDave Chinner 	xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1205c24b5dfaSDave Chinner 	xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1206c24b5dfaSDave Chinner 
1207c24b5dfaSDave Chinner 	if (is_dir) {
1208c24b5dfaSDave Chinner 		error = xfs_dir_init(tp, ip, dp);
1209c24b5dfaSDave Chinner 		if (error)
1210c8eac49eSBrian Foster 			goto out_trans_cancel;
1211c24b5dfaSDave Chinner 
121291083269SEric Sandeen 		xfs_bumplink(tp, dp);
1213c24b5dfaSDave Chinner 	}
1214c24b5dfaSDave Chinner 
1215c24b5dfaSDave Chinner 	/*
1216c24b5dfaSDave Chinner 	 * If this is a synchronous mount, make sure that the
1217c24b5dfaSDave Chinner 	 * create transaction goes to disk before returning to
1218c24b5dfaSDave Chinner 	 * the user.
1219c24b5dfaSDave Chinner 	 */
1220c24b5dfaSDave Chinner 	if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1221c24b5dfaSDave Chinner 		xfs_trans_set_sync(tp);
1222c24b5dfaSDave Chinner 
1223c24b5dfaSDave Chinner 	/*
1224c24b5dfaSDave Chinner 	 * Attach the dquot(s) to the inodes and modify them incore.
1225c24b5dfaSDave Chinner 	 * These ids of the inode couldn't have changed since the new
1226c24b5dfaSDave Chinner 	 * inode has been locked ever since it was created.
1227c24b5dfaSDave Chinner 	 */
1228c24b5dfaSDave Chinner 	xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
1229c24b5dfaSDave Chinner 
123070393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
1231c24b5dfaSDave Chinner 	if (error)
1232c24b5dfaSDave Chinner 		goto out_release_inode;
1233c24b5dfaSDave Chinner 
1234c24b5dfaSDave Chinner 	xfs_qm_dqrele(udqp);
1235c24b5dfaSDave Chinner 	xfs_qm_dqrele(gdqp);
1236c24b5dfaSDave Chinner 	xfs_qm_dqrele(pdqp);
1237c24b5dfaSDave Chinner 
1238c24b5dfaSDave Chinner 	*ipp = ip;
1239c24b5dfaSDave Chinner 	return 0;
1240c24b5dfaSDave Chinner 
1241c24b5dfaSDave Chinner  out_trans_cancel:
12424906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
1243c24b5dfaSDave Chinner  out_release_inode:
1244c24b5dfaSDave Chinner 	/*
124558c90473SDave Chinner 	 * Wait until after the current transaction is aborted to finish the
124658c90473SDave Chinner 	 * setup of the inode and release the inode.  This prevents recursive
124758c90473SDave Chinner 	 * transactions and deadlocks from xfs_inactive.
1248c24b5dfaSDave Chinner 	 */
124958c90473SDave Chinner 	if (ip) {
125058c90473SDave Chinner 		xfs_finish_inode_setup(ip);
125144a8736bSDarrick J. Wong 		xfs_irele(ip);
125258c90473SDave Chinner 	}
1253c24b5dfaSDave Chinner 
1254c24b5dfaSDave Chinner 	xfs_qm_dqrele(udqp);
1255c24b5dfaSDave Chinner 	xfs_qm_dqrele(gdqp);
1256c24b5dfaSDave Chinner 	xfs_qm_dqrele(pdqp);
1257c24b5dfaSDave Chinner 
1258c24b5dfaSDave Chinner 	if (unlock_dp_on_error)
125965523218SChristoph Hellwig 		xfs_iunlock(dp, XFS_ILOCK_EXCL);
1260c24b5dfaSDave Chinner 	return error;
1261c24b5dfaSDave Chinner }
1262c24b5dfaSDave Chinner 
1263c24b5dfaSDave Chinner int
126499b6436bSZhi Yong Wu xfs_create_tmpfile(
126599b6436bSZhi Yong Wu 	struct xfs_inode	*dp,
1266330033d6SBrian Foster 	umode_t			mode,
1267330033d6SBrian Foster 	struct xfs_inode	**ipp)
126899b6436bSZhi Yong Wu {
126999b6436bSZhi Yong Wu 	struct xfs_mount	*mp = dp->i_mount;
127099b6436bSZhi Yong Wu 	struct xfs_inode	*ip = NULL;
127199b6436bSZhi Yong Wu 	struct xfs_trans	*tp = NULL;
127299b6436bSZhi Yong Wu 	int			error;
127399b6436bSZhi Yong Wu 	prid_t                  prid;
127499b6436bSZhi Yong Wu 	struct xfs_dquot	*udqp = NULL;
127599b6436bSZhi Yong Wu 	struct xfs_dquot	*gdqp = NULL;
127699b6436bSZhi Yong Wu 	struct xfs_dquot	*pdqp = NULL;
127799b6436bSZhi Yong Wu 	struct xfs_trans_res	*tres;
127899b6436bSZhi Yong Wu 	uint			resblks;
127999b6436bSZhi Yong Wu 
128099b6436bSZhi Yong Wu 	if (XFS_FORCED_SHUTDOWN(mp))
12812451337dSDave Chinner 		return -EIO;
128299b6436bSZhi Yong Wu 
128399b6436bSZhi Yong Wu 	prid = xfs_get_initial_prid(dp);
128499b6436bSZhi Yong Wu 
128599b6436bSZhi Yong Wu 	/*
128699b6436bSZhi Yong Wu 	 * Make sure that we have allocated dquot(s) on disk.
128799b6436bSZhi Yong Wu 	 */
128854295159SChristoph Hellwig 	error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
128999b6436bSZhi Yong Wu 				XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
129099b6436bSZhi Yong Wu 				&udqp, &gdqp, &pdqp);
129199b6436bSZhi Yong Wu 	if (error)
129299b6436bSZhi Yong Wu 		return error;
129399b6436bSZhi Yong Wu 
129499b6436bSZhi Yong Wu 	resblks = XFS_IALLOC_SPACE_RES(mp);
129599b6436bSZhi Yong Wu 	tres = &M_RES(mp)->tr_create_tmpfile;
1296253f4911SChristoph Hellwig 
1297253f4911SChristoph Hellwig 	error = xfs_trans_alloc(mp, tres, resblks, 0, 0, &tp);
12984906e215SChristoph Hellwig 	if (error)
1299253f4911SChristoph Hellwig 		goto out_release_inode;
130099b6436bSZhi Yong Wu 
130199b6436bSZhi Yong Wu 	error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
130299b6436bSZhi Yong Wu 						pdqp, resblks, 1, 0);
130399b6436bSZhi Yong Wu 	if (error)
130499b6436bSZhi Yong Wu 		goto out_trans_cancel;
130599b6436bSZhi Yong Wu 
1306c4a6bf7fSDarrick J. Wong 	error = xfs_dir_ialloc(&tp, dp, mode, 0, 0, prid, &ip);
1307d6077aa3SJan Kara 	if (error)
130899b6436bSZhi Yong Wu 		goto out_trans_cancel;
130999b6436bSZhi Yong Wu 
131099b6436bSZhi Yong Wu 	if (mp->m_flags & XFS_MOUNT_WSYNC)
131199b6436bSZhi Yong Wu 		xfs_trans_set_sync(tp);
131299b6436bSZhi Yong Wu 
131399b6436bSZhi Yong Wu 	/*
131499b6436bSZhi Yong Wu 	 * Attach the dquot(s) to the inodes and modify them incore.
131599b6436bSZhi Yong Wu 	 * These ids of the inode couldn't have changed since the new
131699b6436bSZhi Yong Wu 	 * inode has been locked ever since it was created.
131799b6436bSZhi Yong Wu 	 */
131899b6436bSZhi Yong Wu 	xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
131999b6436bSZhi Yong Wu 
132099b6436bSZhi Yong Wu 	error = xfs_iunlink(tp, ip);
132199b6436bSZhi Yong Wu 	if (error)
13224906e215SChristoph Hellwig 		goto out_trans_cancel;
132399b6436bSZhi Yong Wu 
132470393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
132599b6436bSZhi Yong Wu 	if (error)
132699b6436bSZhi Yong Wu 		goto out_release_inode;
132799b6436bSZhi Yong Wu 
132899b6436bSZhi Yong Wu 	xfs_qm_dqrele(udqp);
132999b6436bSZhi Yong Wu 	xfs_qm_dqrele(gdqp);
133099b6436bSZhi Yong Wu 	xfs_qm_dqrele(pdqp);
133199b6436bSZhi Yong Wu 
1332330033d6SBrian Foster 	*ipp = ip;
133399b6436bSZhi Yong Wu 	return 0;
133499b6436bSZhi Yong Wu 
133599b6436bSZhi Yong Wu  out_trans_cancel:
13364906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
133799b6436bSZhi Yong Wu  out_release_inode:
133899b6436bSZhi Yong Wu 	/*
133958c90473SDave Chinner 	 * Wait until after the current transaction is aborted to finish the
134058c90473SDave Chinner 	 * setup of the inode and release the inode.  This prevents recursive
134158c90473SDave Chinner 	 * transactions and deadlocks from xfs_inactive.
134299b6436bSZhi Yong Wu 	 */
134358c90473SDave Chinner 	if (ip) {
134458c90473SDave Chinner 		xfs_finish_inode_setup(ip);
134544a8736bSDarrick J. Wong 		xfs_irele(ip);
134658c90473SDave Chinner 	}
134799b6436bSZhi Yong Wu 
134899b6436bSZhi Yong Wu 	xfs_qm_dqrele(udqp);
134999b6436bSZhi Yong Wu 	xfs_qm_dqrele(gdqp);
135099b6436bSZhi Yong Wu 	xfs_qm_dqrele(pdqp);
135199b6436bSZhi Yong Wu 
135299b6436bSZhi Yong Wu 	return error;
135399b6436bSZhi Yong Wu }
135499b6436bSZhi Yong Wu 
135599b6436bSZhi Yong Wu int
1356c24b5dfaSDave Chinner xfs_link(
1357c24b5dfaSDave Chinner 	xfs_inode_t		*tdp,
1358c24b5dfaSDave Chinner 	xfs_inode_t		*sip,
1359c24b5dfaSDave Chinner 	struct xfs_name		*target_name)
1360c24b5dfaSDave Chinner {
1361c24b5dfaSDave Chinner 	xfs_mount_t		*mp = tdp->i_mount;
1362c24b5dfaSDave Chinner 	xfs_trans_t		*tp;
1363c24b5dfaSDave Chinner 	int			error;
1364c24b5dfaSDave Chinner 	int			resblks;
1365c24b5dfaSDave Chinner 
1366c24b5dfaSDave Chinner 	trace_xfs_link(tdp, target_name);
1367c24b5dfaSDave Chinner 
1368c19b3b05SDave Chinner 	ASSERT(!S_ISDIR(VFS_I(sip)->i_mode));
1369c24b5dfaSDave Chinner 
1370c24b5dfaSDave Chinner 	if (XFS_FORCED_SHUTDOWN(mp))
13712451337dSDave Chinner 		return -EIO;
1372c24b5dfaSDave Chinner 
1373c14cfccaSDarrick J. Wong 	error = xfs_qm_dqattach(sip);
1374c24b5dfaSDave Chinner 	if (error)
1375c24b5dfaSDave Chinner 		goto std_return;
1376c24b5dfaSDave Chinner 
1377c14cfccaSDarrick J. Wong 	error = xfs_qm_dqattach(tdp);
1378c24b5dfaSDave Chinner 	if (error)
1379c24b5dfaSDave Chinner 		goto std_return;
1380c24b5dfaSDave Chinner 
1381c24b5dfaSDave Chinner 	resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
1382253f4911SChristoph Hellwig 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_link, resblks, 0, 0, &tp);
13832451337dSDave Chinner 	if (error == -ENOSPC) {
1384c24b5dfaSDave Chinner 		resblks = 0;
1385253f4911SChristoph Hellwig 		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_link, 0, 0, 0, &tp);
1386c24b5dfaSDave Chinner 	}
13874906e215SChristoph Hellwig 	if (error)
1388253f4911SChristoph Hellwig 		goto std_return;
1389c24b5dfaSDave Chinner 
13907c2d238aSDarrick J. Wong 	xfs_lock_two_inodes(sip, XFS_ILOCK_EXCL, tdp, XFS_ILOCK_EXCL);
1391c24b5dfaSDave Chinner 
1392c24b5dfaSDave Chinner 	xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
139365523218SChristoph Hellwig 	xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
1394c24b5dfaSDave Chinner 
1395c24b5dfaSDave Chinner 	/*
1396c24b5dfaSDave Chinner 	 * If we are using project inheritance, we only allow hard link
1397c24b5dfaSDave Chinner 	 * creation in our tree when the project IDs are the same; else
1398c24b5dfaSDave Chinner 	 * the tree quota mechanism could be circumvented.
1399c24b5dfaSDave Chinner 	 */
1400c24b5dfaSDave Chinner 	if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
1401de7a866fSChristoph Hellwig 		     tdp->i_d.di_projid != sip->i_d.di_projid)) {
14022451337dSDave Chinner 		error = -EXDEV;
1403c24b5dfaSDave Chinner 		goto error_return;
1404c24b5dfaSDave Chinner 	}
1405c24b5dfaSDave Chinner 
140694f3cad5SEric Sandeen 	if (!resblks) {
140794f3cad5SEric Sandeen 		error = xfs_dir_canenter(tp, tdp, target_name);
1408c24b5dfaSDave Chinner 		if (error)
1409c24b5dfaSDave Chinner 			goto error_return;
141094f3cad5SEric Sandeen 	}
1411c24b5dfaSDave Chinner 
141254d7b5c1SDave Chinner 	/*
141354d7b5c1SDave Chinner 	 * Handle initial link state of O_TMPFILE inode
141454d7b5c1SDave Chinner 	 */
141554d7b5c1SDave Chinner 	if (VFS_I(sip)->i_nlink == 0) {
1416ab297431SZhi Yong Wu 		error = xfs_iunlink_remove(tp, sip);
1417ab297431SZhi Yong Wu 		if (error)
14184906e215SChristoph Hellwig 			goto error_return;
1419ab297431SZhi Yong Wu 	}
1420ab297431SZhi Yong Wu 
1421c24b5dfaSDave Chinner 	error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
1422381eee69SBrian Foster 				   resblks);
1423c24b5dfaSDave Chinner 	if (error)
14244906e215SChristoph Hellwig 		goto error_return;
1425c24b5dfaSDave Chinner 	xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1426c24b5dfaSDave Chinner 	xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
1427c24b5dfaSDave Chinner 
142891083269SEric Sandeen 	xfs_bumplink(tp, sip);
1429c24b5dfaSDave Chinner 
1430c24b5dfaSDave Chinner 	/*
1431c24b5dfaSDave Chinner 	 * If this is a synchronous mount, make sure that the
1432c24b5dfaSDave Chinner 	 * link transaction goes to disk before returning to
1433c24b5dfaSDave Chinner 	 * the user.
1434c24b5dfaSDave Chinner 	 */
1435f6106efaSEric Sandeen 	if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1436c24b5dfaSDave Chinner 		xfs_trans_set_sync(tp);
1437c24b5dfaSDave Chinner 
143870393313SChristoph Hellwig 	return xfs_trans_commit(tp);
1439c24b5dfaSDave Chinner 
1440c24b5dfaSDave Chinner  error_return:
14414906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
1442c24b5dfaSDave Chinner  std_return:
1443c24b5dfaSDave Chinner 	return error;
1444c24b5dfaSDave Chinner }
1445c24b5dfaSDave Chinner 
1446363e59baSDarrick J. Wong /* Clear the reflink flag and the cowblocks tag if possible. */
1447363e59baSDarrick J. Wong static void
1448363e59baSDarrick J. Wong xfs_itruncate_clear_reflink_flags(
1449363e59baSDarrick J. Wong 	struct xfs_inode	*ip)
1450363e59baSDarrick J. Wong {
1451363e59baSDarrick J. Wong 	struct xfs_ifork	*dfork;
1452363e59baSDarrick J. Wong 	struct xfs_ifork	*cfork;
1453363e59baSDarrick J. Wong 
1454363e59baSDarrick J. Wong 	if (!xfs_is_reflink_inode(ip))
1455363e59baSDarrick J. Wong 		return;
1456363e59baSDarrick J. Wong 	dfork = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1457363e59baSDarrick J. Wong 	cfork = XFS_IFORK_PTR(ip, XFS_COW_FORK);
1458363e59baSDarrick J. Wong 	if (dfork->if_bytes == 0 && cfork->if_bytes == 0)
1459363e59baSDarrick J. Wong 		ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
1460363e59baSDarrick J. Wong 	if (cfork->if_bytes == 0)
1461363e59baSDarrick J. Wong 		xfs_inode_clear_cowblocks_tag(ip);
1462363e59baSDarrick J. Wong }
1463363e59baSDarrick J. Wong 
14641da177e4SLinus Torvalds /*
14658f04c47aSChristoph Hellwig  * Free up the underlying blocks past new_size.  The new size must be smaller
14668f04c47aSChristoph Hellwig  * than the current size.  This routine can be used both for the attribute and
14678f04c47aSChristoph Hellwig  * data fork, and does not modify the inode size, which is left to the caller.
14681da177e4SLinus Torvalds  *
1469f6485057SDavid Chinner  * The transaction passed to this routine must have made a permanent log
1470f6485057SDavid Chinner  * reservation of at least XFS_ITRUNCATE_LOG_RES.  This routine may commit the
1471f6485057SDavid Chinner  * given transaction and start new ones, so make sure everything involved in
1472f6485057SDavid Chinner  * the transaction is tidy before calling here.  Some transaction will be
1473f6485057SDavid Chinner  * returned to the caller to be committed.  The incoming transaction must
1474f6485057SDavid Chinner  * already include the inode, and both inode locks must be held exclusively.
1475f6485057SDavid Chinner  * The inode must also be "held" within the transaction.  On return the inode
1476f6485057SDavid Chinner  * will be "held" within the returned transaction.  This routine does NOT
1477f6485057SDavid Chinner  * require any disk space to be reserved for it within the transaction.
14781da177e4SLinus Torvalds  *
1479f6485057SDavid Chinner  * If we get an error, we must return with the inode locked and linked into the
1480f6485057SDavid Chinner  * current transaction. This keeps things simple for the higher level code,
1481f6485057SDavid Chinner  * because it always knows that the inode is locked and held in the transaction
1482f6485057SDavid Chinner  * that returns to it whether errors occur or not.  We don't mark the inode
1483f6485057SDavid Chinner  * dirty on error so that transactions can be easily aborted if possible.
14841da177e4SLinus Torvalds  */
14851da177e4SLinus Torvalds int
14864e529339SBrian Foster xfs_itruncate_extents_flags(
14878f04c47aSChristoph Hellwig 	struct xfs_trans	**tpp,
14888f04c47aSChristoph Hellwig 	struct xfs_inode	*ip,
14898f04c47aSChristoph Hellwig 	int			whichfork,
149013b86fc3SBrian Foster 	xfs_fsize_t		new_size,
14914e529339SBrian Foster 	int			flags)
14921da177e4SLinus Torvalds {
14938f04c47aSChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
14948f04c47aSChristoph Hellwig 	struct xfs_trans	*tp = *tpp;
14951da177e4SLinus Torvalds 	xfs_fileoff_t		first_unmap_block;
14968f04c47aSChristoph Hellwig 	xfs_filblks_t		unmap_len;
14978f04c47aSChristoph Hellwig 	int			error = 0;
14981da177e4SLinus Torvalds 
14990b56185bSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
15000b56185bSChristoph Hellwig 	ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
15010b56185bSChristoph Hellwig 	       xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1502ce7ae151SChristoph Hellwig 	ASSERT(new_size <= XFS_ISIZE(ip));
15038f04c47aSChristoph Hellwig 	ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
15041da177e4SLinus Torvalds 	ASSERT(ip->i_itemp != NULL);
1505898621d5SChristoph Hellwig 	ASSERT(ip->i_itemp->ili_lock_flags == 0);
15061da177e4SLinus Torvalds 	ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
15071da177e4SLinus Torvalds 
1508673e8e59SChristoph Hellwig 	trace_xfs_itruncate_extents_start(ip, new_size);
1509673e8e59SChristoph Hellwig 
15104e529339SBrian Foster 	flags |= xfs_bmapi_aflag(whichfork);
151113b86fc3SBrian Foster 
15121da177e4SLinus Torvalds 	/*
15131da177e4SLinus Torvalds 	 * Since it is possible for space to become allocated beyond
15141da177e4SLinus Torvalds 	 * the end of the file (in a crash where the space is allocated
15151da177e4SLinus Torvalds 	 * but the inode size is not yet updated), simply remove any
15161da177e4SLinus Torvalds 	 * blocks which show up between the new EOF and the maximum
15174bbb04abSDarrick J. Wong 	 * possible file size.
15184bbb04abSDarrick J. Wong 	 *
15194bbb04abSDarrick J. Wong 	 * We have to free all the blocks to the bmbt maximum offset, even if
15204bbb04abSDarrick J. Wong 	 * the page cache can't scale that far.
15211da177e4SLinus Torvalds 	 */
15228f04c47aSChristoph Hellwig 	first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
15234bbb04abSDarrick J. Wong 	if (first_unmap_block >= XFS_MAX_FILEOFF) {
15244bbb04abSDarrick J. Wong 		WARN_ON_ONCE(first_unmap_block > XFS_MAX_FILEOFF);
15258f04c47aSChristoph Hellwig 		return 0;
15264bbb04abSDarrick J. Wong 	}
15278f04c47aSChristoph Hellwig 
15284bbb04abSDarrick J. Wong 	unmap_len = XFS_MAX_FILEOFF - first_unmap_block + 1;
15294bbb04abSDarrick J. Wong 	while (unmap_len > 0) {
153002dff7bfSBrian Foster 		ASSERT(tp->t_firstblock == NULLFSBLOCK);
15314bbb04abSDarrick J. Wong 		error = __xfs_bunmapi(tp, ip, first_unmap_block, &unmap_len,
15324bbb04abSDarrick J. Wong 				flags, XFS_ITRUNC_MAX_EXTENTS);
15338f04c47aSChristoph Hellwig 		if (error)
1534d5a2e289SBrian Foster 			goto out;
15351da177e4SLinus Torvalds 
15361da177e4SLinus Torvalds 		/*
15371da177e4SLinus Torvalds 		 * Duplicate the transaction that has the permanent
15381da177e4SLinus Torvalds 		 * reservation and commit the old transaction.
15391da177e4SLinus Torvalds 		 */
15409e28a242SBrian Foster 		error = xfs_defer_finish(&tp);
15418f04c47aSChristoph Hellwig 		if (error)
15429b1f4e98SBrian Foster 			goto out;
15431da177e4SLinus Torvalds 
1544411350dfSChristoph Hellwig 		error = xfs_trans_roll_inode(&tp, ip);
15451da177e4SLinus Torvalds 		if (error)
15468f04c47aSChristoph Hellwig 			goto out;
15471da177e4SLinus Torvalds 	}
15488f04c47aSChristoph Hellwig 
15494919d42aSDarrick J. Wong 	if (whichfork == XFS_DATA_FORK) {
1550aa8968f2SDarrick J. Wong 		/* Remove all pending CoW reservations. */
15514919d42aSDarrick J. Wong 		error = xfs_reflink_cancel_cow_blocks(ip, &tp,
15524bbb04abSDarrick J. Wong 				first_unmap_block, XFS_MAX_FILEOFF, true);
1553aa8968f2SDarrick J. Wong 		if (error)
1554aa8968f2SDarrick J. Wong 			goto out;
1555aa8968f2SDarrick J. Wong 
1556363e59baSDarrick J. Wong 		xfs_itruncate_clear_reflink_flags(ip);
15574919d42aSDarrick J. Wong 	}
1558aa8968f2SDarrick J. Wong 
1559673e8e59SChristoph Hellwig 	/*
1560673e8e59SChristoph Hellwig 	 * Always re-log the inode so that our permanent transaction can keep
1561673e8e59SChristoph Hellwig 	 * on rolling it forward in the log.
1562673e8e59SChristoph Hellwig 	 */
1563673e8e59SChristoph Hellwig 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1564673e8e59SChristoph Hellwig 
1565673e8e59SChristoph Hellwig 	trace_xfs_itruncate_extents_end(ip, new_size);
1566673e8e59SChristoph Hellwig 
15678f04c47aSChristoph Hellwig out:
15688f04c47aSChristoph Hellwig 	*tpp = tp;
15698f04c47aSChristoph Hellwig 	return error;
15708f04c47aSChristoph Hellwig }
15718f04c47aSChristoph Hellwig 
1572c24b5dfaSDave Chinner int
1573c24b5dfaSDave Chinner xfs_release(
1574c24b5dfaSDave Chinner 	xfs_inode_t	*ip)
1575c24b5dfaSDave Chinner {
1576c24b5dfaSDave Chinner 	xfs_mount_t	*mp = ip->i_mount;
1577c24b5dfaSDave Chinner 	int		error;
1578c24b5dfaSDave Chinner 
1579c19b3b05SDave Chinner 	if (!S_ISREG(VFS_I(ip)->i_mode) || (VFS_I(ip)->i_mode == 0))
1580c24b5dfaSDave Chinner 		return 0;
1581c24b5dfaSDave Chinner 
1582c24b5dfaSDave Chinner 	/* If this is a read-only mount, don't do this (would generate I/O) */
1583c24b5dfaSDave Chinner 	if (mp->m_flags & XFS_MOUNT_RDONLY)
1584c24b5dfaSDave Chinner 		return 0;
1585c24b5dfaSDave Chinner 
1586c24b5dfaSDave Chinner 	if (!XFS_FORCED_SHUTDOWN(mp)) {
1587c24b5dfaSDave Chinner 		int truncated;
1588c24b5dfaSDave Chinner 
1589c24b5dfaSDave Chinner 		/*
1590c24b5dfaSDave Chinner 		 * If we previously truncated this file and removed old data
1591c24b5dfaSDave Chinner 		 * in the process, we want to initiate "early" writeout on
1592c24b5dfaSDave Chinner 		 * the last close.  This is an attempt to combat the notorious
1593c24b5dfaSDave Chinner 		 * NULL files problem which is particularly noticeable from a
1594c24b5dfaSDave Chinner 		 * truncate down, buffered (re-)write (delalloc), followed by
1595c24b5dfaSDave Chinner 		 * a crash.  What we are effectively doing here is
1596c24b5dfaSDave Chinner 		 * significantly reducing the time window where we'd otherwise
1597c24b5dfaSDave Chinner 		 * be exposed to that problem.
1598c24b5dfaSDave Chinner 		 */
1599c24b5dfaSDave Chinner 		truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
1600c24b5dfaSDave Chinner 		if (truncated) {
1601c24b5dfaSDave Chinner 			xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
1602eac152b4SDave Chinner 			if (ip->i_delayed_blks > 0) {
16032451337dSDave Chinner 				error = filemap_flush(VFS_I(ip)->i_mapping);
1604c24b5dfaSDave Chinner 				if (error)
1605c24b5dfaSDave Chinner 					return error;
1606c24b5dfaSDave Chinner 			}
1607c24b5dfaSDave Chinner 		}
1608c24b5dfaSDave Chinner 	}
1609c24b5dfaSDave Chinner 
161054d7b5c1SDave Chinner 	if (VFS_I(ip)->i_nlink == 0)
1611c24b5dfaSDave Chinner 		return 0;
1612c24b5dfaSDave Chinner 
1613c24b5dfaSDave Chinner 	if (xfs_can_free_eofblocks(ip, false)) {
1614c24b5dfaSDave Chinner 
1615c24b5dfaSDave Chinner 		/*
1616a36b9261SBrian Foster 		 * Check if the inode is being opened, written and closed
1617a36b9261SBrian Foster 		 * frequently and we have delayed allocation blocks outstanding
1618a36b9261SBrian Foster 		 * (e.g. streaming writes from the NFS server), truncating the
1619a36b9261SBrian Foster 		 * blocks past EOF will cause fragmentation to occur.
1620a36b9261SBrian Foster 		 *
1621a36b9261SBrian Foster 		 * In this case don't do the truncation, but we have to be
1622a36b9261SBrian Foster 		 * careful how we detect this case. Blocks beyond EOF show up as
1623a36b9261SBrian Foster 		 * i_delayed_blks even when the inode is clean, so we need to
1624a36b9261SBrian Foster 		 * truncate them away first before checking for a dirty release.
1625a36b9261SBrian Foster 		 * Hence on the first dirty close we will still remove the
1626a36b9261SBrian Foster 		 * speculative allocation, but after that we will leave it in
1627a36b9261SBrian Foster 		 * place.
1628a36b9261SBrian Foster 		 */
1629a36b9261SBrian Foster 		if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
1630a36b9261SBrian Foster 			return 0;
1631a36b9261SBrian Foster 		/*
1632c24b5dfaSDave Chinner 		 * If we can't get the iolock just skip truncating the blocks
1633*c1e8d7c6SMichel Lespinasse 		 * past EOF because we could deadlock with the mmap_lock
1634c24b5dfaSDave Chinner 		 * otherwise. We'll get another chance to drop them once the
1635c24b5dfaSDave Chinner 		 * last reference to the inode is dropped, so we'll never leak
1636c24b5dfaSDave Chinner 		 * blocks permanently.
1637c24b5dfaSDave Chinner 		 */
1638a36b9261SBrian Foster 		if (xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
1639a36b9261SBrian Foster 			error = xfs_free_eofblocks(ip);
1640a36b9261SBrian Foster 			xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1641a36b9261SBrian Foster 			if (error)
1642c24b5dfaSDave Chinner 				return error;
1643a36b9261SBrian Foster 		}
1644c24b5dfaSDave Chinner 
1645c24b5dfaSDave Chinner 		/* delalloc blocks after truncation means it really is dirty */
1646c24b5dfaSDave Chinner 		if (ip->i_delayed_blks)
1647c24b5dfaSDave Chinner 			xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
1648c24b5dfaSDave Chinner 	}
1649c24b5dfaSDave Chinner 	return 0;
1650c24b5dfaSDave Chinner }
1651c24b5dfaSDave Chinner 
1652c24b5dfaSDave Chinner /*
1653f7be2d7fSBrian Foster  * xfs_inactive_truncate
1654f7be2d7fSBrian Foster  *
1655f7be2d7fSBrian Foster  * Called to perform a truncate when an inode becomes unlinked.
1656f7be2d7fSBrian Foster  */
1657f7be2d7fSBrian Foster STATIC int
1658f7be2d7fSBrian Foster xfs_inactive_truncate(
1659f7be2d7fSBrian Foster 	struct xfs_inode *ip)
1660f7be2d7fSBrian Foster {
1661f7be2d7fSBrian Foster 	struct xfs_mount	*mp = ip->i_mount;
1662f7be2d7fSBrian Foster 	struct xfs_trans	*tp;
1663f7be2d7fSBrian Foster 	int			error;
1664f7be2d7fSBrian Foster 
1665253f4911SChristoph Hellwig 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
1666f7be2d7fSBrian Foster 	if (error) {
1667f7be2d7fSBrian Foster 		ASSERT(XFS_FORCED_SHUTDOWN(mp));
1668f7be2d7fSBrian Foster 		return error;
1669f7be2d7fSBrian Foster 	}
1670f7be2d7fSBrian Foster 	xfs_ilock(ip, XFS_ILOCK_EXCL);
1671f7be2d7fSBrian Foster 	xfs_trans_ijoin(tp, ip, 0);
1672f7be2d7fSBrian Foster 
1673f7be2d7fSBrian Foster 	/*
1674f7be2d7fSBrian Foster 	 * Log the inode size first to prevent stale data exposure in the event
1675f7be2d7fSBrian Foster 	 * of a system crash before the truncate completes. See the related
167669bca807SJan Kara 	 * comment in xfs_vn_setattr_size() for details.
1677f7be2d7fSBrian Foster 	 */
1678f7be2d7fSBrian Foster 	ip->i_d.di_size = 0;
1679f7be2d7fSBrian Foster 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1680f7be2d7fSBrian Foster 
1681f7be2d7fSBrian Foster 	error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
1682f7be2d7fSBrian Foster 	if (error)
1683f7be2d7fSBrian Foster 		goto error_trans_cancel;
1684f7be2d7fSBrian Foster 
1685daf83964SChristoph Hellwig 	ASSERT(ip->i_df.if_nextents == 0);
1686f7be2d7fSBrian Foster 
168770393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
1688f7be2d7fSBrian Foster 	if (error)
1689f7be2d7fSBrian Foster 		goto error_unlock;
1690f7be2d7fSBrian Foster 
1691f7be2d7fSBrian Foster 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
1692f7be2d7fSBrian Foster 	return 0;
1693f7be2d7fSBrian Foster 
1694f7be2d7fSBrian Foster error_trans_cancel:
16954906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
1696f7be2d7fSBrian Foster error_unlock:
1697f7be2d7fSBrian Foster 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
1698f7be2d7fSBrian Foster 	return error;
1699f7be2d7fSBrian Foster }
1700f7be2d7fSBrian Foster 
1701f7be2d7fSBrian Foster /*
170288877d2bSBrian Foster  * xfs_inactive_ifree()
170388877d2bSBrian Foster  *
170488877d2bSBrian Foster  * Perform the inode free when an inode is unlinked.
170588877d2bSBrian Foster  */
170688877d2bSBrian Foster STATIC int
170788877d2bSBrian Foster xfs_inactive_ifree(
170888877d2bSBrian Foster 	struct xfs_inode *ip)
170988877d2bSBrian Foster {
171088877d2bSBrian Foster 	struct xfs_mount	*mp = ip->i_mount;
171188877d2bSBrian Foster 	struct xfs_trans	*tp;
171288877d2bSBrian Foster 	int			error;
171388877d2bSBrian Foster 
17149d43b180SBrian Foster 	/*
171576d771b4SChristoph Hellwig 	 * We try to use a per-AG reservation for any block needed by the finobt
171676d771b4SChristoph Hellwig 	 * tree, but as the finobt feature predates the per-AG reservation
171776d771b4SChristoph Hellwig 	 * support a degraded file system might not have enough space for the
171876d771b4SChristoph Hellwig 	 * reservation at mount time.  In that case try to dip into the reserved
171976d771b4SChristoph Hellwig 	 * pool and pray.
17209d43b180SBrian Foster 	 *
17219d43b180SBrian Foster 	 * Send a warning if the reservation does happen to fail, as the inode
17229d43b180SBrian Foster 	 * now remains allocated and sits on the unlinked list until the fs is
17239d43b180SBrian Foster 	 * repaired.
17249d43b180SBrian Foster 	 */
1725e1f6ca11SDarrick J. Wong 	if (unlikely(mp->m_finobt_nores)) {
1726253f4911SChristoph Hellwig 		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ifree,
172776d771b4SChristoph Hellwig 				XFS_IFREE_SPACE_RES(mp), 0, XFS_TRANS_RESERVE,
172876d771b4SChristoph Hellwig 				&tp);
172976d771b4SChristoph Hellwig 	} else {
173076d771b4SChristoph Hellwig 		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ifree, 0, 0, 0, &tp);
173176d771b4SChristoph Hellwig 	}
173288877d2bSBrian Foster 	if (error) {
17332451337dSDave Chinner 		if (error == -ENOSPC) {
17349d43b180SBrian Foster 			xfs_warn_ratelimited(mp,
17359d43b180SBrian Foster 			"Failed to remove inode(s) from unlinked list. "
17369d43b180SBrian Foster 			"Please free space, unmount and run xfs_repair.");
17379d43b180SBrian Foster 		} else {
173888877d2bSBrian Foster 			ASSERT(XFS_FORCED_SHUTDOWN(mp));
17399d43b180SBrian Foster 		}
174088877d2bSBrian Foster 		return error;
174188877d2bSBrian Foster 	}
174288877d2bSBrian Foster 
174388877d2bSBrian Foster 	xfs_ilock(ip, XFS_ILOCK_EXCL);
174488877d2bSBrian Foster 	xfs_trans_ijoin(tp, ip, 0);
174588877d2bSBrian Foster 
17460e0417f3SBrian Foster 	error = xfs_ifree(tp, ip);
174788877d2bSBrian Foster 	if (error) {
174888877d2bSBrian Foster 		/*
174988877d2bSBrian Foster 		 * If we fail to free the inode, shut down.  The cancel
175088877d2bSBrian Foster 		 * might do that, we need to make sure.  Otherwise the
175188877d2bSBrian Foster 		 * inode might be lost for a long time or forever.
175288877d2bSBrian Foster 		 */
175388877d2bSBrian Foster 		if (!XFS_FORCED_SHUTDOWN(mp)) {
175488877d2bSBrian Foster 			xfs_notice(mp, "%s: xfs_ifree returned error %d",
175588877d2bSBrian Foster 				__func__, error);
175688877d2bSBrian Foster 			xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
175788877d2bSBrian Foster 		}
17584906e215SChristoph Hellwig 		xfs_trans_cancel(tp);
175988877d2bSBrian Foster 		xfs_iunlock(ip, XFS_ILOCK_EXCL);
176088877d2bSBrian Foster 		return error;
176188877d2bSBrian Foster 	}
176288877d2bSBrian Foster 
176388877d2bSBrian Foster 	/*
176488877d2bSBrian Foster 	 * Credit the quota account(s). The inode is gone.
176588877d2bSBrian Foster 	 */
176688877d2bSBrian Foster 	xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
176788877d2bSBrian Foster 
176888877d2bSBrian Foster 	/*
1769d4a97a04SBrian Foster 	 * Just ignore errors at this point.  There is nothing we can do except
1770d4a97a04SBrian Foster 	 * to try to keep going. Make sure it's not a silent error.
177188877d2bSBrian Foster 	 */
177270393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
177388877d2bSBrian Foster 	if (error)
177488877d2bSBrian Foster 		xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
177588877d2bSBrian Foster 			__func__, error);
177688877d2bSBrian Foster 
177788877d2bSBrian Foster 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
177888877d2bSBrian Foster 	return 0;
177988877d2bSBrian Foster }
178088877d2bSBrian Foster 
178188877d2bSBrian Foster /*
1782c24b5dfaSDave Chinner  * xfs_inactive
1783c24b5dfaSDave Chinner  *
1784c24b5dfaSDave Chinner  * This is called when the vnode reference count for the vnode
1785c24b5dfaSDave Chinner  * goes to zero.  If the file has been unlinked, then it must
1786c24b5dfaSDave Chinner  * now be truncated.  Also, we clear all of the read-ahead state
1787c24b5dfaSDave Chinner  * kept for the inode here since the file is now closed.
1788c24b5dfaSDave Chinner  */
178974564fb4SBrian Foster void
1790c24b5dfaSDave Chinner xfs_inactive(
1791c24b5dfaSDave Chinner 	xfs_inode_t	*ip)
1792c24b5dfaSDave Chinner {
17933d3c8b52SJie Liu 	struct xfs_mount	*mp;
1794c24b5dfaSDave Chinner 	int			error;
1795c24b5dfaSDave Chinner 	int			truncate = 0;
1796c24b5dfaSDave Chinner 
1797c24b5dfaSDave Chinner 	/*
1798c24b5dfaSDave Chinner 	 * If the inode is already free, then there can be nothing
1799c24b5dfaSDave Chinner 	 * to clean up here.
1800c24b5dfaSDave Chinner 	 */
1801c19b3b05SDave Chinner 	if (VFS_I(ip)->i_mode == 0) {
1802c24b5dfaSDave Chinner 		ASSERT(ip->i_df.if_broot_bytes == 0);
180374564fb4SBrian Foster 		return;
1804c24b5dfaSDave Chinner 	}
1805c24b5dfaSDave Chinner 
1806c24b5dfaSDave Chinner 	mp = ip->i_mount;
180717c12bcdSDarrick J. Wong 	ASSERT(!xfs_iflags_test(ip, XFS_IRECOVERY));
1808c24b5dfaSDave Chinner 
1809c24b5dfaSDave Chinner 	/* If this is a read-only mount, don't do this (would generate I/O) */
1810c24b5dfaSDave Chinner 	if (mp->m_flags & XFS_MOUNT_RDONLY)
181174564fb4SBrian Foster 		return;
1812c24b5dfaSDave Chinner 
18136231848cSDarrick J. Wong 	/* Try to clean out the cow blocks if there are any. */
181451d62690SChristoph Hellwig 	if (xfs_inode_has_cow_data(ip))
18156231848cSDarrick J. Wong 		xfs_reflink_cancel_cow_range(ip, 0, NULLFILEOFF, true);
18166231848cSDarrick J. Wong 
181754d7b5c1SDave Chinner 	if (VFS_I(ip)->i_nlink != 0) {
1818c24b5dfaSDave Chinner 		/*
1819c24b5dfaSDave Chinner 		 * force is true because we are evicting an inode from the
1820c24b5dfaSDave Chinner 		 * cache. Post-eof blocks must be freed, lest we end up with
1821c24b5dfaSDave Chinner 		 * broken free space accounting.
18223b4683c2SBrian Foster 		 *
18233b4683c2SBrian Foster 		 * Note: don't bother with iolock here since lockdep complains
18243b4683c2SBrian Foster 		 * about acquiring it in reclaim context. We have the only
18253b4683c2SBrian Foster 		 * reference to the inode at this point anyways.
1826c24b5dfaSDave Chinner 		 */
18273b4683c2SBrian Foster 		if (xfs_can_free_eofblocks(ip, true))
1828a36b9261SBrian Foster 			xfs_free_eofblocks(ip);
182974564fb4SBrian Foster 
183074564fb4SBrian Foster 		return;
1831c24b5dfaSDave Chinner 	}
1832c24b5dfaSDave Chinner 
1833c19b3b05SDave Chinner 	if (S_ISREG(VFS_I(ip)->i_mode) &&
1834c24b5dfaSDave Chinner 	    (ip->i_d.di_size != 0 || XFS_ISIZE(ip) != 0 ||
1835daf83964SChristoph Hellwig 	     ip->i_df.if_nextents > 0 || ip->i_delayed_blks > 0))
1836c24b5dfaSDave Chinner 		truncate = 1;
1837c24b5dfaSDave Chinner 
1838c14cfccaSDarrick J. Wong 	error = xfs_qm_dqattach(ip);
1839c24b5dfaSDave Chinner 	if (error)
184074564fb4SBrian Foster 		return;
1841c24b5dfaSDave Chinner 
1842c19b3b05SDave Chinner 	if (S_ISLNK(VFS_I(ip)->i_mode))
184336b21ddeSBrian Foster 		error = xfs_inactive_symlink(ip);
1844f7be2d7fSBrian Foster 	else if (truncate)
1845f7be2d7fSBrian Foster 		error = xfs_inactive_truncate(ip);
184636b21ddeSBrian Foster 	if (error)
184774564fb4SBrian Foster 		return;
1848c24b5dfaSDave Chinner 
1849c24b5dfaSDave Chinner 	/*
1850c24b5dfaSDave Chinner 	 * If there are attributes associated with the file then blow them away
1851c24b5dfaSDave Chinner 	 * now.  The code calls a routine that recursively deconstructs the
18526dfe5a04SDave Chinner 	 * attribute fork. If also blows away the in-core attribute fork.
1853c24b5dfaSDave Chinner 	 */
18546dfe5a04SDave Chinner 	if (XFS_IFORK_Q(ip)) {
1855c24b5dfaSDave Chinner 		error = xfs_attr_inactive(ip);
1856c24b5dfaSDave Chinner 		if (error)
185774564fb4SBrian Foster 			return;
1858c24b5dfaSDave Chinner 	}
1859c24b5dfaSDave Chinner 
18606dfe5a04SDave Chinner 	ASSERT(!ip->i_afp);
18616dfe5a04SDave Chinner 	ASSERT(ip->i_d.di_forkoff == 0);
1862c24b5dfaSDave Chinner 
1863c24b5dfaSDave Chinner 	/*
1864c24b5dfaSDave Chinner 	 * Free the inode.
1865c24b5dfaSDave Chinner 	 */
186688877d2bSBrian Foster 	error = xfs_inactive_ifree(ip);
1867c24b5dfaSDave Chinner 	if (error)
186874564fb4SBrian Foster 		return;
1869c24b5dfaSDave Chinner 
1870c24b5dfaSDave Chinner 	/*
1871c24b5dfaSDave Chinner 	 * Release the dquots held by inode, if any.
1872c24b5dfaSDave Chinner 	 */
1873c24b5dfaSDave Chinner 	xfs_qm_dqdetach(ip);
1874c24b5dfaSDave Chinner }
1875c24b5dfaSDave Chinner 
18761da177e4SLinus Torvalds /*
18779b247179SDarrick J. Wong  * In-Core Unlinked List Lookups
18789b247179SDarrick J. Wong  * =============================
18799b247179SDarrick J. Wong  *
18809b247179SDarrick J. Wong  * Every inode is supposed to be reachable from some other piece of metadata
18819b247179SDarrick J. Wong  * with the exception of the root directory.  Inodes with a connection to a
18829b247179SDarrick J. Wong  * file descriptor but not linked from anywhere in the on-disk directory tree
18839b247179SDarrick J. Wong  * are collectively known as unlinked inodes, though the filesystem itself
18849b247179SDarrick J. Wong  * maintains links to these inodes so that on-disk metadata are consistent.
18859b247179SDarrick J. Wong  *
18869b247179SDarrick J. Wong  * XFS implements a per-AG on-disk hash table of unlinked inodes.  The AGI
18879b247179SDarrick J. Wong  * header contains a number of buckets that point to an inode, and each inode
18889b247179SDarrick J. Wong  * record has a pointer to the next inode in the hash chain.  This
18899b247179SDarrick J. Wong  * singly-linked list causes scaling problems in the iunlink remove function
18909b247179SDarrick J. Wong  * because we must walk that list to find the inode that points to the inode
18919b247179SDarrick J. Wong  * being removed from the unlinked hash bucket list.
18929b247179SDarrick J. Wong  *
18939b247179SDarrick J. Wong  * What if we modelled the unlinked list as a collection of records capturing
18949b247179SDarrick J. Wong  * "X.next_unlinked = Y" relations?  If we indexed those records on Y, we'd
18959b247179SDarrick J. Wong  * have a fast way to look up unlinked list predecessors, which avoids the
18969b247179SDarrick J. Wong  * slow list walk.  That's exactly what we do here (in-core) with a per-AG
18979b247179SDarrick J. Wong  * rhashtable.
18989b247179SDarrick J. Wong  *
18999b247179SDarrick J. Wong  * Because this is a backref cache, we ignore operational failures since the
19009b247179SDarrick J. Wong  * iunlink code can fall back to the slow bucket walk.  The only errors that
19019b247179SDarrick J. Wong  * should bubble out are for obviously incorrect situations.
19029b247179SDarrick J. Wong  *
19039b247179SDarrick J. Wong  * All users of the backref cache MUST hold the AGI buffer lock to serialize
19049b247179SDarrick J. Wong  * access or have otherwise provided for concurrency control.
19059b247179SDarrick J. Wong  */
19069b247179SDarrick J. Wong 
19079b247179SDarrick J. Wong /* Capture a "X.next_unlinked = Y" relationship. */
19089b247179SDarrick J. Wong struct xfs_iunlink {
19099b247179SDarrick J. Wong 	struct rhash_head	iu_rhash_head;
19109b247179SDarrick J. Wong 	xfs_agino_t		iu_agino;		/* X */
19119b247179SDarrick J. Wong 	xfs_agino_t		iu_next_unlinked;	/* Y */
19129b247179SDarrick J. Wong };
19139b247179SDarrick J. Wong 
19149b247179SDarrick J. Wong /* Unlinked list predecessor lookup hashtable construction */
19159b247179SDarrick J. Wong static int
19169b247179SDarrick J. Wong xfs_iunlink_obj_cmpfn(
19179b247179SDarrick J. Wong 	struct rhashtable_compare_arg	*arg,
19189b247179SDarrick J. Wong 	const void			*obj)
19199b247179SDarrick J. Wong {
19209b247179SDarrick J. Wong 	const xfs_agino_t		*key = arg->key;
19219b247179SDarrick J. Wong 	const struct xfs_iunlink	*iu = obj;
19229b247179SDarrick J. Wong 
19239b247179SDarrick J. Wong 	if (iu->iu_next_unlinked != *key)
19249b247179SDarrick J. Wong 		return 1;
19259b247179SDarrick J. Wong 	return 0;
19269b247179SDarrick J. Wong }
19279b247179SDarrick J. Wong 
19289b247179SDarrick J. Wong static const struct rhashtable_params xfs_iunlink_hash_params = {
19299b247179SDarrick J. Wong 	.min_size		= XFS_AGI_UNLINKED_BUCKETS,
19309b247179SDarrick J. Wong 	.key_len		= sizeof(xfs_agino_t),
19319b247179SDarrick J. Wong 	.key_offset		= offsetof(struct xfs_iunlink,
19329b247179SDarrick J. Wong 					   iu_next_unlinked),
19339b247179SDarrick J. Wong 	.head_offset		= offsetof(struct xfs_iunlink, iu_rhash_head),
19349b247179SDarrick J. Wong 	.automatic_shrinking	= true,
19359b247179SDarrick J. Wong 	.obj_cmpfn		= xfs_iunlink_obj_cmpfn,
19369b247179SDarrick J. Wong };
19379b247179SDarrick J. Wong 
19389b247179SDarrick J. Wong /*
19399b247179SDarrick J. Wong  * Return X, where X.next_unlinked == @agino.  Returns NULLAGINO if no such
19409b247179SDarrick J. Wong  * relation is found.
19419b247179SDarrick J. Wong  */
19429b247179SDarrick J. Wong static xfs_agino_t
19439b247179SDarrick J. Wong xfs_iunlink_lookup_backref(
19449b247179SDarrick J. Wong 	struct xfs_perag	*pag,
19459b247179SDarrick J. Wong 	xfs_agino_t		agino)
19469b247179SDarrick J. Wong {
19479b247179SDarrick J. Wong 	struct xfs_iunlink	*iu;
19489b247179SDarrick J. Wong 
19499b247179SDarrick J. Wong 	iu = rhashtable_lookup_fast(&pag->pagi_unlinked_hash, &agino,
19509b247179SDarrick J. Wong 			xfs_iunlink_hash_params);
19519b247179SDarrick J. Wong 	return iu ? iu->iu_agino : NULLAGINO;
19529b247179SDarrick J. Wong }
19539b247179SDarrick J. Wong 
19549b247179SDarrick J. Wong /*
19559b247179SDarrick J. Wong  * Take ownership of an iunlink cache entry and insert it into the hash table.
19569b247179SDarrick J. Wong  * If successful, the entry will be owned by the cache; if not, it is freed.
19579b247179SDarrick J. Wong  * Either way, the caller does not own @iu after this call.
19589b247179SDarrick J. Wong  */
19599b247179SDarrick J. Wong static int
19609b247179SDarrick J. Wong xfs_iunlink_insert_backref(
19619b247179SDarrick J. Wong 	struct xfs_perag	*pag,
19629b247179SDarrick J. Wong 	struct xfs_iunlink	*iu)
19639b247179SDarrick J. Wong {
19649b247179SDarrick J. Wong 	int			error;
19659b247179SDarrick J. Wong 
19669b247179SDarrick J. Wong 	error = rhashtable_insert_fast(&pag->pagi_unlinked_hash,
19679b247179SDarrick J. Wong 			&iu->iu_rhash_head, xfs_iunlink_hash_params);
19689b247179SDarrick J. Wong 	/*
19699b247179SDarrick J. Wong 	 * Fail loudly if there already was an entry because that's a sign of
19709b247179SDarrick J. Wong 	 * corruption of in-memory data.  Also fail loudly if we see an error
19719b247179SDarrick J. Wong 	 * code we didn't anticipate from the rhashtable code.  Currently we
19729b247179SDarrick J. Wong 	 * only anticipate ENOMEM.
19739b247179SDarrick J. Wong 	 */
19749b247179SDarrick J. Wong 	if (error) {
19759b247179SDarrick J. Wong 		WARN(error != -ENOMEM, "iunlink cache insert error %d", error);
19769b247179SDarrick J. Wong 		kmem_free(iu);
19779b247179SDarrick J. Wong 	}
19789b247179SDarrick J. Wong 	/*
19799b247179SDarrick J. Wong 	 * Absorb any runtime errors that aren't a result of corruption because
19809b247179SDarrick J. Wong 	 * this is a cache and we can always fall back to bucket list scanning.
19819b247179SDarrick J. Wong 	 */
19829b247179SDarrick J. Wong 	if (error != 0 && error != -EEXIST)
19839b247179SDarrick J. Wong 		error = 0;
19849b247179SDarrick J. Wong 	return error;
19859b247179SDarrick J. Wong }
19869b247179SDarrick J. Wong 
19879b247179SDarrick J. Wong /* Remember that @prev_agino.next_unlinked = @this_agino. */
19889b247179SDarrick J. Wong static int
19899b247179SDarrick J. Wong xfs_iunlink_add_backref(
19909b247179SDarrick J. Wong 	struct xfs_perag	*pag,
19919b247179SDarrick J. Wong 	xfs_agino_t		prev_agino,
19929b247179SDarrick J. Wong 	xfs_agino_t		this_agino)
19939b247179SDarrick J. Wong {
19949b247179SDarrick J. Wong 	struct xfs_iunlink	*iu;
19959b247179SDarrick J. Wong 
19969b247179SDarrick J. Wong 	if (XFS_TEST_ERROR(false, pag->pag_mount, XFS_ERRTAG_IUNLINK_FALLBACK))
19979b247179SDarrick J. Wong 		return 0;
19989b247179SDarrick J. Wong 
1999707e0ddaSTetsuo Handa 	iu = kmem_zalloc(sizeof(*iu), KM_NOFS);
20009b247179SDarrick J. Wong 	iu->iu_agino = prev_agino;
20019b247179SDarrick J. Wong 	iu->iu_next_unlinked = this_agino;
20029b247179SDarrick J. Wong 
20039b247179SDarrick J. Wong 	return xfs_iunlink_insert_backref(pag, iu);
20049b247179SDarrick J. Wong }
20059b247179SDarrick J. Wong 
20069b247179SDarrick J. Wong /*
20079b247179SDarrick J. Wong  * Replace X.next_unlinked = @agino with X.next_unlinked = @next_unlinked.
20089b247179SDarrick J. Wong  * If @next_unlinked is NULLAGINO, we drop the backref and exit.  If there
20099b247179SDarrick J. Wong  * wasn't any such entry then we don't bother.
20109b247179SDarrick J. Wong  */
20119b247179SDarrick J. Wong static int
20129b247179SDarrick J. Wong xfs_iunlink_change_backref(
20139b247179SDarrick J. Wong 	struct xfs_perag	*pag,
20149b247179SDarrick J. Wong 	xfs_agino_t		agino,
20159b247179SDarrick J. Wong 	xfs_agino_t		next_unlinked)
20169b247179SDarrick J. Wong {
20179b247179SDarrick J. Wong 	struct xfs_iunlink	*iu;
20189b247179SDarrick J. Wong 	int			error;
20199b247179SDarrick J. Wong 
20209b247179SDarrick J. Wong 	/* Look up the old entry; if there wasn't one then exit. */
20219b247179SDarrick J. Wong 	iu = rhashtable_lookup_fast(&pag->pagi_unlinked_hash, &agino,
20229b247179SDarrick J. Wong 			xfs_iunlink_hash_params);
20239b247179SDarrick J. Wong 	if (!iu)
20249b247179SDarrick J. Wong 		return 0;
20259b247179SDarrick J. Wong 
20269b247179SDarrick J. Wong 	/*
20279b247179SDarrick J. Wong 	 * Remove the entry.  This shouldn't ever return an error, but if we
20289b247179SDarrick J. Wong 	 * couldn't remove the old entry we don't want to add it again to the
20299b247179SDarrick J. Wong 	 * hash table, and if the entry disappeared on us then someone's
20309b247179SDarrick J. Wong 	 * violated the locking rules and we need to fail loudly.  Either way
20319b247179SDarrick J. Wong 	 * we cannot remove the inode because internal state is or would have
20329b247179SDarrick J. Wong 	 * been corrupt.
20339b247179SDarrick J. Wong 	 */
20349b247179SDarrick J. Wong 	error = rhashtable_remove_fast(&pag->pagi_unlinked_hash,
20359b247179SDarrick J. Wong 			&iu->iu_rhash_head, xfs_iunlink_hash_params);
20369b247179SDarrick J. Wong 	if (error)
20379b247179SDarrick J. Wong 		return error;
20389b247179SDarrick J. Wong 
20399b247179SDarrick J. Wong 	/* If there is no new next entry just free our item and return. */
20409b247179SDarrick J. Wong 	if (next_unlinked == NULLAGINO) {
20419b247179SDarrick J. Wong 		kmem_free(iu);
20429b247179SDarrick J. Wong 		return 0;
20439b247179SDarrick J. Wong 	}
20449b247179SDarrick J. Wong 
20459b247179SDarrick J. Wong 	/* Update the entry and re-add it to the hash table. */
20469b247179SDarrick J. Wong 	iu->iu_next_unlinked = next_unlinked;
20479b247179SDarrick J. Wong 	return xfs_iunlink_insert_backref(pag, iu);
20489b247179SDarrick J. Wong }
20499b247179SDarrick J. Wong 
20509b247179SDarrick J. Wong /* Set up the in-core predecessor structures. */
20519b247179SDarrick J. Wong int
20529b247179SDarrick J. Wong xfs_iunlink_init(
20539b247179SDarrick J. Wong 	struct xfs_perag	*pag)
20549b247179SDarrick J. Wong {
20559b247179SDarrick J. Wong 	return rhashtable_init(&pag->pagi_unlinked_hash,
20569b247179SDarrick J. Wong 			&xfs_iunlink_hash_params);
20579b247179SDarrick J. Wong }
20589b247179SDarrick J. Wong 
20599b247179SDarrick J. Wong /* Free the in-core predecessor structures. */
20609b247179SDarrick J. Wong static void
20619b247179SDarrick J. Wong xfs_iunlink_free_item(
20629b247179SDarrick J. Wong 	void			*ptr,
20639b247179SDarrick J. Wong 	void			*arg)
20649b247179SDarrick J. Wong {
20659b247179SDarrick J. Wong 	struct xfs_iunlink	*iu = ptr;
20669b247179SDarrick J. Wong 	bool			*freed_anything = arg;
20679b247179SDarrick J. Wong 
20689b247179SDarrick J. Wong 	*freed_anything = true;
20699b247179SDarrick J. Wong 	kmem_free(iu);
20709b247179SDarrick J. Wong }
20719b247179SDarrick J. Wong 
20729b247179SDarrick J. Wong void
20739b247179SDarrick J. Wong xfs_iunlink_destroy(
20749b247179SDarrick J. Wong 	struct xfs_perag	*pag)
20759b247179SDarrick J. Wong {
20769b247179SDarrick J. Wong 	bool			freed_anything = false;
20779b247179SDarrick J. Wong 
20789b247179SDarrick J. Wong 	rhashtable_free_and_destroy(&pag->pagi_unlinked_hash,
20799b247179SDarrick J. Wong 			xfs_iunlink_free_item, &freed_anything);
20809b247179SDarrick J. Wong 
20819b247179SDarrick J. Wong 	ASSERT(freed_anything == false || XFS_FORCED_SHUTDOWN(pag->pag_mount));
20829b247179SDarrick J. Wong }
20839b247179SDarrick J. Wong 
20849b247179SDarrick J. Wong /*
20859a4a5118SDarrick J. Wong  * Point the AGI unlinked bucket at an inode and log the results.  The caller
20869a4a5118SDarrick J. Wong  * is responsible for validating the old value.
20879a4a5118SDarrick J. Wong  */
20889a4a5118SDarrick J. Wong STATIC int
20899a4a5118SDarrick J. Wong xfs_iunlink_update_bucket(
20909a4a5118SDarrick J. Wong 	struct xfs_trans	*tp,
20919a4a5118SDarrick J. Wong 	xfs_agnumber_t		agno,
20929a4a5118SDarrick J. Wong 	struct xfs_buf		*agibp,
20939a4a5118SDarrick J. Wong 	unsigned int		bucket_index,
20949a4a5118SDarrick J. Wong 	xfs_agino_t		new_agino)
20959a4a5118SDarrick J. Wong {
2096370c782bSChristoph Hellwig 	struct xfs_agi		*agi = agibp->b_addr;
20979a4a5118SDarrick J. Wong 	xfs_agino_t		old_value;
20989a4a5118SDarrick J. Wong 	int			offset;
20999a4a5118SDarrick J. Wong 
21009a4a5118SDarrick J. Wong 	ASSERT(xfs_verify_agino_or_null(tp->t_mountp, agno, new_agino));
21019a4a5118SDarrick J. Wong 
21029a4a5118SDarrick J. Wong 	old_value = be32_to_cpu(agi->agi_unlinked[bucket_index]);
21039a4a5118SDarrick J. Wong 	trace_xfs_iunlink_update_bucket(tp->t_mountp, agno, bucket_index,
21049a4a5118SDarrick J. Wong 			old_value, new_agino);
21059a4a5118SDarrick J. Wong 
21069a4a5118SDarrick J. Wong 	/*
21079a4a5118SDarrick J. Wong 	 * We should never find the head of the list already set to the value
21089a4a5118SDarrick J. Wong 	 * passed in because either we're adding or removing ourselves from the
21099a4a5118SDarrick J. Wong 	 * head of the list.
21109a4a5118SDarrick J. Wong 	 */
2111a5155b87SDarrick J. Wong 	if (old_value == new_agino) {
21128d57c216SDarrick J. Wong 		xfs_buf_mark_corrupt(agibp);
21139a4a5118SDarrick J. Wong 		return -EFSCORRUPTED;
2114a5155b87SDarrick J. Wong 	}
21159a4a5118SDarrick J. Wong 
21169a4a5118SDarrick J. Wong 	agi->agi_unlinked[bucket_index] = cpu_to_be32(new_agino);
21179a4a5118SDarrick J. Wong 	offset = offsetof(struct xfs_agi, agi_unlinked) +
21189a4a5118SDarrick J. Wong 			(sizeof(xfs_agino_t) * bucket_index);
21199a4a5118SDarrick J. Wong 	xfs_trans_log_buf(tp, agibp, offset, offset + sizeof(xfs_agino_t) - 1);
21209a4a5118SDarrick J. Wong 	return 0;
21219a4a5118SDarrick J. Wong }
21229a4a5118SDarrick J. Wong 
2123f2fc16a3SDarrick J. Wong /* Set an on-disk inode's next_unlinked pointer. */
2124f2fc16a3SDarrick J. Wong STATIC void
2125f2fc16a3SDarrick J. Wong xfs_iunlink_update_dinode(
2126f2fc16a3SDarrick J. Wong 	struct xfs_trans	*tp,
2127f2fc16a3SDarrick J. Wong 	xfs_agnumber_t		agno,
2128f2fc16a3SDarrick J. Wong 	xfs_agino_t		agino,
2129f2fc16a3SDarrick J. Wong 	struct xfs_buf		*ibp,
2130f2fc16a3SDarrick J. Wong 	struct xfs_dinode	*dip,
2131f2fc16a3SDarrick J. Wong 	struct xfs_imap		*imap,
2132f2fc16a3SDarrick J. Wong 	xfs_agino_t		next_agino)
2133f2fc16a3SDarrick J. Wong {
2134f2fc16a3SDarrick J. Wong 	struct xfs_mount	*mp = tp->t_mountp;
2135f2fc16a3SDarrick J. Wong 	int			offset;
2136f2fc16a3SDarrick J. Wong 
2137f2fc16a3SDarrick J. Wong 	ASSERT(xfs_verify_agino_or_null(mp, agno, next_agino));
2138f2fc16a3SDarrick J. Wong 
2139f2fc16a3SDarrick J. Wong 	trace_xfs_iunlink_update_dinode(mp, agno, agino,
2140f2fc16a3SDarrick J. Wong 			be32_to_cpu(dip->di_next_unlinked), next_agino);
2141f2fc16a3SDarrick J. Wong 
2142f2fc16a3SDarrick J. Wong 	dip->di_next_unlinked = cpu_to_be32(next_agino);
2143f2fc16a3SDarrick J. Wong 	offset = imap->im_boffset +
2144f2fc16a3SDarrick J. Wong 			offsetof(struct xfs_dinode, di_next_unlinked);
2145f2fc16a3SDarrick J. Wong 
2146f2fc16a3SDarrick J. Wong 	/* need to recalc the inode CRC if appropriate */
2147f2fc16a3SDarrick J. Wong 	xfs_dinode_calc_crc(mp, dip);
2148f2fc16a3SDarrick J. Wong 	xfs_trans_inode_buf(tp, ibp);
2149f2fc16a3SDarrick J. Wong 	xfs_trans_log_buf(tp, ibp, offset, offset + sizeof(xfs_agino_t) - 1);
2150f2fc16a3SDarrick J. Wong 	xfs_inobp_check(mp, ibp);
2151f2fc16a3SDarrick J. Wong }
2152f2fc16a3SDarrick J. Wong 
2153f2fc16a3SDarrick J. Wong /* Set an in-core inode's unlinked pointer and return the old value. */
2154f2fc16a3SDarrick J. Wong STATIC int
2155f2fc16a3SDarrick J. Wong xfs_iunlink_update_inode(
2156f2fc16a3SDarrick J. Wong 	struct xfs_trans	*tp,
2157f2fc16a3SDarrick J. Wong 	struct xfs_inode	*ip,
2158f2fc16a3SDarrick J. Wong 	xfs_agnumber_t		agno,
2159f2fc16a3SDarrick J. Wong 	xfs_agino_t		next_agino,
2160f2fc16a3SDarrick J. Wong 	xfs_agino_t		*old_next_agino)
2161f2fc16a3SDarrick J. Wong {
2162f2fc16a3SDarrick J. Wong 	struct xfs_mount	*mp = tp->t_mountp;
2163f2fc16a3SDarrick J. Wong 	struct xfs_dinode	*dip;
2164f2fc16a3SDarrick J. Wong 	struct xfs_buf		*ibp;
2165f2fc16a3SDarrick J. Wong 	xfs_agino_t		old_value;
2166f2fc16a3SDarrick J. Wong 	int			error;
2167f2fc16a3SDarrick J. Wong 
2168f2fc16a3SDarrick J. Wong 	ASSERT(xfs_verify_agino_or_null(mp, agno, next_agino));
2169f2fc16a3SDarrick J. Wong 
2170c1995079SBrian Foster 	error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp, 0);
2171f2fc16a3SDarrick J. Wong 	if (error)
2172f2fc16a3SDarrick J. Wong 		return error;
2173f2fc16a3SDarrick J. Wong 
2174f2fc16a3SDarrick J. Wong 	/* Make sure the old pointer isn't garbage. */
2175f2fc16a3SDarrick J. Wong 	old_value = be32_to_cpu(dip->di_next_unlinked);
2176f2fc16a3SDarrick J. Wong 	if (!xfs_verify_agino_or_null(mp, agno, old_value)) {
2177a5155b87SDarrick J. Wong 		xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, dip,
2178a5155b87SDarrick J. Wong 				sizeof(*dip), __this_address);
2179f2fc16a3SDarrick J. Wong 		error = -EFSCORRUPTED;
2180f2fc16a3SDarrick J. Wong 		goto out;
2181f2fc16a3SDarrick J. Wong 	}
2182f2fc16a3SDarrick J. Wong 
2183f2fc16a3SDarrick J. Wong 	/*
2184f2fc16a3SDarrick J. Wong 	 * Since we're updating a linked list, we should never find that the
2185f2fc16a3SDarrick J. Wong 	 * current pointer is the same as the new value, unless we're
2186f2fc16a3SDarrick J. Wong 	 * terminating the list.
2187f2fc16a3SDarrick J. Wong 	 */
2188f2fc16a3SDarrick J. Wong 	*old_next_agino = old_value;
2189f2fc16a3SDarrick J. Wong 	if (old_value == next_agino) {
2190a5155b87SDarrick J. Wong 		if (next_agino != NULLAGINO) {
2191a5155b87SDarrick J. Wong 			xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__,
2192a5155b87SDarrick J. Wong 					dip, sizeof(*dip), __this_address);
2193f2fc16a3SDarrick J. Wong 			error = -EFSCORRUPTED;
2194a5155b87SDarrick J. Wong 		}
2195f2fc16a3SDarrick J. Wong 		goto out;
2196f2fc16a3SDarrick J. Wong 	}
2197f2fc16a3SDarrick J. Wong 
2198f2fc16a3SDarrick J. Wong 	/* Ok, update the new pointer. */
2199f2fc16a3SDarrick J. Wong 	xfs_iunlink_update_dinode(tp, agno, XFS_INO_TO_AGINO(mp, ip->i_ino),
2200f2fc16a3SDarrick J. Wong 			ibp, dip, &ip->i_imap, next_agino);
2201f2fc16a3SDarrick J. Wong 	return 0;
2202f2fc16a3SDarrick J. Wong out:
2203f2fc16a3SDarrick J. Wong 	xfs_trans_brelse(tp, ibp);
2204f2fc16a3SDarrick J. Wong 	return error;
2205f2fc16a3SDarrick J. Wong }
2206f2fc16a3SDarrick J. Wong 
22079a4a5118SDarrick J. Wong /*
2208c4a6bf7fSDarrick J. Wong  * This is called when the inode's link count has gone to 0 or we are creating
2209c4a6bf7fSDarrick J. Wong  * a tmpfile via O_TMPFILE.  The inode @ip must have nlink == 0.
221054d7b5c1SDave Chinner  *
221154d7b5c1SDave Chinner  * We place the on-disk inode on a list in the AGI.  It will be pulled from this
221254d7b5c1SDave Chinner  * list when the inode is freed.
22131da177e4SLinus Torvalds  */
221454d7b5c1SDave Chinner STATIC int
22151da177e4SLinus Torvalds xfs_iunlink(
221654d7b5c1SDave Chinner 	struct xfs_trans	*tp,
221754d7b5c1SDave Chinner 	struct xfs_inode	*ip)
22181da177e4SLinus Torvalds {
22195837f625SDarrick J. Wong 	struct xfs_mount	*mp = tp->t_mountp;
22205837f625SDarrick J. Wong 	struct xfs_agi		*agi;
22215837f625SDarrick J. Wong 	struct xfs_buf		*agibp;
222286bfd375SDarrick J. Wong 	xfs_agino_t		next_agino;
22235837f625SDarrick J. Wong 	xfs_agnumber_t		agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
22245837f625SDarrick J. Wong 	xfs_agino_t		agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
22255837f625SDarrick J. Wong 	short			bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
22261da177e4SLinus Torvalds 	int			error;
22271da177e4SLinus Torvalds 
2228c4a6bf7fSDarrick J. Wong 	ASSERT(VFS_I(ip)->i_nlink == 0);
2229c19b3b05SDave Chinner 	ASSERT(VFS_I(ip)->i_mode != 0);
22304664c66cSDarrick J. Wong 	trace_xfs_iunlink(ip);
22311da177e4SLinus Torvalds 
22325837f625SDarrick J. Wong 	/* Get the agi buffer first.  It ensures lock ordering on the list. */
22335837f625SDarrick J. Wong 	error = xfs_read_agi(mp, tp, agno, &agibp);
2234859d7182SVlad Apostolov 	if (error)
22351da177e4SLinus Torvalds 		return error;
2236370c782bSChristoph Hellwig 	agi = agibp->b_addr;
22375e1be0fbSChristoph Hellwig 
22381da177e4SLinus Torvalds 	/*
223986bfd375SDarrick J. Wong 	 * Get the index into the agi hash table for the list this inode will
224086bfd375SDarrick J. Wong 	 * go on.  Make sure the pointer isn't garbage and that this inode
224186bfd375SDarrick J. Wong 	 * isn't already on the list.
22421da177e4SLinus Torvalds 	 */
224386bfd375SDarrick J. Wong 	next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
224486bfd375SDarrick J. Wong 	if (next_agino == agino ||
2245a5155b87SDarrick J. Wong 	    !xfs_verify_agino_or_null(mp, agno, next_agino)) {
22468d57c216SDarrick J. Wong 		xfs_buf_mark_corrupt(agibp);
224786bfd375SDarrick J. Wong 		return -EFSCORRUPTED;
2248a5155b87SDarrick J. Wong 	}
22491da177e4SLinus Torvalds 
225086bfd375SDarrick J. Wong 	if (next_agino != NULLAGINO) {
22519b247179SDarrick J. Wong 		struct xfs_perag	*pag;
2252f2fc16a3SDarrick J. Wong 		xfs_agino_t		old_agino;
2253f2fc16a3SDarrick J. Wong 
22541da177e4SLinus Torvalds 		/*
2255f2fc16a3SDarrick J. Wong 		 * There is already another inode in the bucket, so point this
2256f2fc16a3SDarrick J. Wong 		 * inode to the current head of the list.
22571da177e4SLinus Torvalds 		 */
2258f2fc16a3SDarrick J. Wong 		error = xfs_iunlink_update_inode(tp, ip, agno, next_agino,
2259f2fc16a3SDarrick J. Wong 				&old_agino);
2260c319b58bSVlad Apostolov 		if (error)
2261c319b58bSVlad Apostolov 			return error;
2262f2fc16a3SDarrick J. Wong 		ASSERT(old_agino == NULLAGINO);
22639b247179SDarrick J. Wong 
22649b247179SDarrick J. Wong 		/*
22659b247179SDarrick J. Wong 		 * agino has been unlinked, add a backref from the next inode
22669b247179SDarrick J. Wong 		 * back to agino.
22679b247179SDarrick J. Wong 		 */
22689b247179SDarrick J. Wong 		pag = xfs_perag_get(mp, agno);
22699b247179SDarrick J. Wong 		error = xfs_iunlink_add_backref(pag, agino, next_agino);
22709b247179SDarrick J. Wong 		xfs_perag_put(pag);
22719b247179SDarrick J. Wong 		if (error)
22729b247179SDarrick J. Wong 			return error;
22731da177e4SLinus Torvalds 	}
22741da177e4SLinus Torvalds 
22759a4a5118SDarrick J. Wong 	/* Point the head of the list to point to this inode. */
22769a4a5118SDarrick J. Wong 	return xfs_iunlink_update_bucket(tp, agno, agibp, bucket_index, agino);
22771da177e4SLinus Torvalds }
22781da177e4SLinus Torvalds 
227923ffa52cSDarrick J. Wong /* Return the imap, dinode pointer, and buffer for an inode. */
228023ffa52cSDarrick J. Wong STATIC int
228123ffa52cSDarrick J. Wong xfs_iunlink_map_ino(
228223ffa52cSDarrick J. Wong 	struct xfs_trans	*tp,
228323ffa52cSDarrick J. Wong 	xfs_agnumber_t		agno,
228423ffa52cSDarrick J. Wong 	xfs_agino_t		agino,
228523ffa52cSDarrick J. Wong 	struct xfs_imap		*imap,
228623ffa52cSDarrick J. Wong 	struct xfs_dinode	**dipp,
228723ffa52cSDarrick J. Wong 	struct xfs_buf		**bpp)
228823ffa52cSDarrick J. Wong {
228923ffa52cSDarrick J. Wong 	struct xfs_mount	*mp = tp->t_mountp;
229023ffa52cSDarrick J. Wong 	int			error;
229123ffa52cSDarrick J. Wong 
229223ffa52cSDarrick J. Wong 	imap->im_blkno = 0;
229323ffa52cSDarrick J. Wong 	error = xfs_imap(mp, tp, XFS_AGINO_TO_INO(mp, agno, agino), imap, 0);
229423ffa52cSDarrick J. Wong 	if (error) {
229523ffa52cSDarrick J. Wong 		xfs_warn(mp, "%s: xfs_imap returned error %d.",
229623ffa52cSDarrick J. Wong 				__func__, error);
229723ffa52cSDarrick J. Wong 		return error;
229823ffa52cSDarrick J. Wong 	}
229923ffa52cSDarrick J. Wong 
2300c1995079SBrian Foster 	error = xfs_imap_to_bp(mp, tp, imap, dipp, bpp, 0);
230123ffa52cSDarrick J. Wong 	if (error) {
230223ffa52cSDarrick J. Wong 		xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
230323ffa52cSDarrick J. Wong 				__func__, error);
230423ffa52cSDarrick J. Wong 		return error;
230523ffa52cSDarrick J. Wong 	}
230623ffa52cSDarrick J. Wong 
230723ffa52cSDarrick J. Wong 	return 0;
230823ffa52cSDarrick J. Wong }
230923ffa52cSDarrick J. Wong 
231023ffa52cSDarrick J. Wong /*
231123ffa52cSDarrick J. Wong  * Walk the unlinked chain from @head_agino until we find the inode that
231223ffa52cSDarrick J. Wong  * points to @target_agino.  Return the inode number, map, dinode pointer,
231323ffa52cSDarrick J. Wong  * and inode cluster buffer of that inode as @agino, @imap, @dipp, and @bpp.
231423ffa52cSDarrick J. Wong  *
231523ffa52cSDarrick J. Wong  * @tp, @pag, @head_agino, and @target_agino are input parameters.
231623ffa52cSDarrick J. Wong  * @agino, @imap, @dipp, and @bpp are all output parameters.
231723ffa52cSDarrick J. Wong  *
231823ffa52cSDarrick J. Wong  * Do not call this function if @target_agino is the head of the list.
231923ffa52cSDarrick J. Wong  */
232023ffa52cSDarrick J. Wong STATIC int
232123ffa52cSDarrick J. Wong xfs_iunlink_map_prev(
232223ffa52cSDarrick J. Wong 	struct xfs_trans	*tp,
232323ffa52cSDarrick J. Wong 	xfs_agnumber_t		agno,
232423ffa52cSDarrick J. Wong 	xfs_agino_t		head_agino,
232523ffa52cSDarrick J. Wong 	xfs_agino_t		target_agino,
232623ffa52cSDarrick J. Wong 	xfs_agino_t		*agino,
232723ffa52cSDarrick J. Wong 	struct xfs_imap		*imap,
232823ffa52cSDarrick J. Wong 	struct xfs_dinode	**dipp,
23299b247179SDarrick J. Wong 	struct xfs_buf		**bpp,
23309b247179SDarrick J. Wong 	struct xfs_perag	*pag)
233123ffa52cSDarrick J. Wong {
233223ffa52cSDarrick J. Wong 	struct xfs_mount	*mp = tp->t_mountp;
233323ffa52cSDarrick J. Wong 	xfs_agino_t		next_agino;
233423ffa52cSDarrick J. Wong 	int			error;
233523ffa52cSDarrick J. Wong 
233623ffa52cSDarrick J. Wong 	ASSERT(head_agino != target_agino);
233723ffa52cSDarrick J. Wong 	*bpp = NULL;
233823ffa52cSDarrick J. Wong 
23399b247179SDarrick J. Wong 	/* See if our backref cache can find it faster. */
23409b247179SDarrick J. Wong 	*agino = xfs_iunlink_lookup_backref(pag, target_agino);
23419b247179SDarrick J. Wong 	if (*agino != NULLAGINO) {
23429b247179SDarrick J. Wong 		error = xfs_iunlink_map_ino(tp, agno, *agino, imap, dipp, bpp);
23439b247179SDarrick J. Wong 		if (error)
23449b247179SDarrick J. Wong 			return error;
23459b247179SDarrick J. Wong 
23469b247179SDarrick J. Wong 		if (be32_to_cpu((*dipp)->di_next_unlinked) == target_agino)
23479b247179SDarrick J. Wong 			return 0;
23489b247179SDarrick J. Wong 
23499b247179SDarrick J. Wong 		/*
23509b247179SDarrick J. Wong 		 * If we get here the cache contents were corrupt, so drop the
23519b247179SDarrick J. Wong 		 * buffer and fall back to walking the bucket list.
23529b247179SDarrick J. Wong 		 */
23539b247179SDarrick J. Wong 		xfs_trans_brelse(tp, *bpp);
23549b247179SDarrick J. Wong 		*bpp = NULL;
23559b247179SDarrick J. Wong 		WARN_ON_ONCE(1);
23569b247179SDarrick J. Wong 	}
23579b247179SDarrick J. Wong 
23589b247179SDarrick J. Wong 	trace_xfs_iunlink_map_prev_fallback(mp, agno);
23599b247179SDarrick J. Wong 
23609b247179SDarrick J. Wong 	/* Otherwise, walk the entire bucket until we find it. */
236123ffa52cSDarrick J. Wong 	next_agino = head_agino;
236223ffa52cSDarrick J. Wong 	while (next_agino != target_agino) {
236323ffa52cSDarrick J. Wong 		xfs_agino_t	unlinked_agino;
236423ffa52cSDarrick J. Wong 
236523ffa52cSDarrick J. Wong 		if (*bpp)
236623ffa52cSDarrick J. Wong 			xfs_trans_brelse(tp, *bpp);
236723ffa52cSDarrick J. Wong 
236823ffa52cSDarrick J. Wong 		*agino = next_agino;
236923ffa52cSDarrick J. Wong 		error = xfs_iunlink_map_ino(tp, agno, next_agino, imap, dipp,
237023ffa52cSDarrick J. Wong 				bpp);
237123ffa52cSDarrick J. Wong 		if (error)
237223ffa52cSDarrick J. Wong 			return error;
237323ffa52cSDarrick J. Wong 
237423ffa52cSDarrick J. Wong 		unlinked_agino = be32_to_cpu((*dipp)->di_next_unlinked);
237523ffa52cSDarrick J. Wong 		/*
237623ffa52cSDarrick J. Wong 		 * Make sure this pointer is valid and isn't an obvious
237723ffa52cSDarrick J. Wong 		 * infinite loop.
237823ffa52cSDarrick J. Wong 		 */
237923ffa52cSDarrick J. Wong 		if (!xfs_verify_agino(mp, agno, unlinked_agino) ||
238023ffa52cSDarrick J. Wong 		    next_agino == unlinked_agino) {
238123ffa52cSDarrick J. Wong 			XFS_CORRUPTION_ERROR(__func__,
238223ffa52cSDarrick J. Wong 					XFS_ERRLEVEL_LOW, mp,
238323ffa52cSDarrick J. Wong 					*dipp, sizeof(**dipp));
238423ffa52cSDarrick J. Wong 			error = -EFSCORRUPTED;
238523ffa52cSDarrick J. Wong 			return error;
238623ffa52cSDarrick J. Wong 		}
238723ffa52cSDarrick J. Wong 		next_agino = unlinked_agino;
238823ffa52cSDarrick J. Wong 	}
238923ffa52cSDarrick J. Wong 
239023ffa52cSDarrick J. Wong 	return 0;
239123ffa52cSDarrick J. Wong }
239223ffa52cSDarrick J. Wong 
23931da177e4SLinus Torvalds /*
23941da177e4SLinus Torvalds  * Pull the on-disk inode from the AGI unlinked list.
23951da177e4SLinus Torvalds  */
23961da177e4SLinus Torvalds STATIC int
23971da177e4SLinus Torvalds xfs_iunlink_remove(
23985837f625SDarrick J. Wong 	struct xfs_trans	*tp,
23995837f625SDarrick J. Wong 	struct xfs_inode	*ip)
24001da177e4SLinus Torvalds {
24015837f625SDarrick J. Wong 	struct xfs_mount	*mp = tp->t_mountp;
24025837f625SDarrick J. Wong 	struct xfs_agi		*agi;
24035837f625SDarrick J. Wong 	struct xfs_buf		*agibp;
24045837f625SDarrick J. Wong 	struct xfs_buf		*last_ibp;
24055837f625SDarrick J. Wong 	struct xfs_dinode	*last_dip = NULL;
24069b247179SDarrick J. Wong 	struct xfs_perag	*pag = NULL;
24075837f625SDarrick J. Wong 	xfs_agnumber_t		agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
24085837f625SDarrick J. Wong 	xfs_agino_t		agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
24091da177e4SLinus Torvalds 	xfs_agino_t		next_agino;
2410b1d2a068SDarrick J. Wong 	xfs_agino_t		head_agino;
24115837f625SDarrick J. Wong 	short			bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
24121da177e4SLinus Torvalds 	int			error;
24131da177e4SLinus Torvalds 
24144664c66cSDarrick J. Wong 	trace_xfs_iunlink_remove(ip);
24154664c66cSDarrick J. Wong 
24165837f625SDarrick J. Wong 	/* Get the agi buffer first.  It ensures lock ordering on the list. */
24175e1be0fbSChristoph Hellwig 	error = xfs_read_agi(mp, tp, agno, &agibp);
24185e1be0fbSChristoph Hellwig 	if (error)
24191da177e4SLinus Torvalds 		return error;
2420370c782bSChristoph Hellwig 	agi = agibp->b_addr;
24215e1be0fbSChristoph Hellwig 
24221da177e4SLinus Torvalds 	/*
242386bfd375SDarrick J. Wong 	 * Get the index into the agi hash table for the list this inode will
242486bfd375SDarrick J. Wong 	 * go on.  Make sure the head pointer isn't garbage.
24251da177e4SLinus Torvalds 	 */
2426b1d2a068SDarrick J. Wong 	head_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
2427b1d2a068SDarrick J. Wong 	if (!xfs_verify_agino(mp, agno, head_agino)) {
2428d2e73665SDarrick J. Wong 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
2429d2e73665SDarrick J. Wong 				agi, sizeof(*agi));
2430d2e73665SDarrick J. Wong 		return -EFSCORRUPTED;
2431d2e73665SDarrick J. Wong 	}
24321da177e4SLinus Torvalds 
24331da177e4SLinus Torvalds 	/*
2434b1d2a068SDarrick J. Wong 	 * Set our inode's next_unlinked pointer to NULL and then return
2435b1d2a068SDarrick J. Wong 	 * the old pointer value so that we can update whatever was previous
2436b1d2a068SDarrick J. Wong 	 * to us in the list to point to whatever was next in the list.
24371da177e4SLinus Torvalds 	 */
2438b1d2a068SDarrick J. Wong 	error = xfs_iunlink_update_inode(tp, ip, agno, NULLAGINO, &next_agino);
2439f2fc16a3SDarrick J. Wong 	if (error)
24401da177e4SLinus Torvalds 		return error;
24419a4a5118SDarrick J. Wong 
24429b247179SDarrick J. Wong 	/*
24439b247179SDarrick J. Wong 	 * If there was a backref pointing from the next inode back to this
24449b247179SDarrick J. Wong 	 * one, remove it because we've removed this inode from the list.
24459b247179SDarrick J. Wong 	 *
24469b247179SDarrick J. Wong 	 * Later, if this inode was in the middle of the list we'll update
24479b247179SDarrick J. Wong 	 * this inode's backref to point from the next inode.
24489b247179SDarrick J. Wong 	 */
24499b247179SDarrick J. Wong 	if (next_agino != NULLAGINO) {
24509b247179SDarrick J. Wong 		pag = xfs_perag_get(mp, agno);
24519b247179SDarrick J. Wong 		error = xfs_iunlink_change_backref(pag, next_agino,
24529b247179SDarrick J. Wong 				NULLAGINO);
24539b247179SDarrick J. Wong 		if (error)
24549b247179SDarrick J. Wong 			goto out;
24559b247179SDarrick J. Wong 	}
24569b247179SDarrick J. Wong 
2457b1d2a068SDarrick J. Wong 	if (head_agino == agino) {
24589a4a5118SDarrick J. Wong 		/* Point the head of the list to the next unlinked inode. */
24599a4a5118SDarrick J. Wong 		error = xfs_iunlink_update_bucket(tp, agno, agibp, bucket_index,
24609a4a5118SDarrick J. Wong 				next_agino);
24619a4a5118SDarrick J. Wong 		if (error)
24629b247179SDarrick J. Wong 			goto out;
24631da177e4SLinus Torvalds 	} else {
2464f2fc16a3SDarrick J. Wong 		struct xfs_imap	imap;
2465f2fc16a3SDarrick J. Wong 		xfs_agino_t	prev_agino;
2466f2fc16a3SDarrick J. Wong 
24679b247179SDarrick J. Wong 		if (!pag)
24689b247179SDarrick J. Wong 			pag = xfs_perag_get(mp, agno);
24699b247179SDarrick J. Wong 
247023ffa52cSDarrick J. Wong 		/* We need to search the list for the inode being freed. */
2471b1d2a068SDarrick J. Wong 		error = xfs_iunlink_map_prev(tp, agno, head_agino, agino,
24729b247179SDarrick J. Wong 				&prev_agino, &imap, &last_dip, &last_ibp,
24739b247179SDarrick J. Wong 				pag);
247423ffa52cSDarrick J. Wong 		if (error)
24759b247179SDarrick J. Wong 			goto out;
2476475ee413SChristoph Hellwig 
2477f2fc16a3SDarrick J. Wong 		/* Point the previous inode on the list to the next inode. */
2478f2fc16a3SDarrick J. Wong 		xfs_iunlink_update_dinode(tp, agno, prev_agino, last_ibp,
2479f2fc16a3SDarrick J. Wong 				last_dip, &imap, next_agino);
24809b247179SDarrick J. Wong 
24819b247179SDarrick J. Wong 		/*
24829b247179SDarrick J. Wong 		 * Now we deal with the backref for this inode.  If this inode
24839b247179SDarrick J. Wong 		 * pointed at a real inode, change the backref that pointed to
24849b247179SDarrick J. Wong 		 * us to point to our old next.  If this inode was the end of
24859b247179SDarrick J. Wong 		 * the list, delete the backref that pointed to us.  Note that
24869b247179SDarrick J. Wong 		 * change_backref takes care of deleting the backref if
24879b247179SDarrick J. Wong 		 * next_agino is NULLAGINO.
24889b247179SDarrick J. Wong 		 */
24899b247179SDarrick J. Wong 		error = xfs_iunlink_change_backref(pag, agino, next_agino);
24909b247179SDarrick J. Wong 		if (error)
24919b247179SDarrick J. Wong 			goto out;
24921da177e4SLinus Torvalds 	}
24939b247179SDarrick J. Wong 
24949b247179SDarrick J. Wong out:
24959b247179SDarrick J. Wong 	if (pag)
24969b247179SDarrick J. Wong 		xfs_perag_put(pag);
24979b247179SDarrick J. Wong 	return error;
24981da177e4SLinus Torvalds }
24991da177e4SLinus Torvalds 
25005b3eed75SDave Chinner /*
25015806165aSDave Chinner  * Look up the inode number specified and mark it stale if it is found. If it is
25025806165aSDave Chinner  * dirty, return the inode so it can be attached to the cluster buffer so it can
25035806165aSDave Chinner  * be processed appropriately when the cluster free transaction completes.
25045806165aSDave Chinner  */
25055806165aSDave Chinner static struct xfs_inode *
25065806165aSDave Chinner xfs_ifree_get_one_inode(
25075806165aSDave Chinner 	struct xfs_perag	*pag,
25085806165aSDave Chinner 	struct xfs_inode	*free_ip,
2509d9fdd0adSBrian Foster 	xfs_ino_t		inum)
25105806165aSDave Chinner {
25115806165aSDave Chinner 	struct xfs_mount	*mp = pag->pag_mount;
25125806165aSDave Chinner 	struct xfs_inode	*ip;
25135806165aSDave Chinner 
25145806165aSDave Chinner retry:
25155806165aSDave Chinner 	rcu_read_lock();
25165806165aSDave Chinner 	ip = radix_tree_lookup(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, inum));
25175806165aSDave Chinner 
25185806165aSDave Chinner 	/* Inode not in memory, nothing to do */
25195806165aSDave Chinner 	if (!ip)
25205806165aSDave Chinner 		goto out_rcu_unlock;
25215806165aSDave Chinner 
25225806165aSDave Chinner 	/*
25235806165aSDave Chinner 	 * because this is an RCU protected lookup, we could find a recently
25245806165aSDave Chinner 	 * freed or even reallocated inode during the lookup. We need to check
25255806165aSDave Chinner 	 * under the i_flags_lock for a valid inode here. Skip it if it is not
25265806165aSDave Chinner 	 * valid, the wrong inode or stale.
25275806165aSDave Chinner 	 */
25285806165aSDave Chinner 	spin_lock(&ip->i_flags_lock);
25295806165aSDave Chinner 	if (ip->i_ino != inum || __xfs_iflags_test(ip, XFS_ISTALE)) {
25305806165aSDave Chinner 		spin_unlock(&ip->i_flags_lock);
25315806165aSDave Chinner 		goto out_rcu_unlock;
25325806165aSDave Chinner 	}
25335806165aSDave Chinner 	spin_unlock(&ip->i_flags_lock);
25345806165aSDave Chinner 
25355806165aSDave Chinner 	/*
25365806165aSDave Chinner 	 * Don't try to lock/unlock the current inode, but we _cannot_ skip the
25375806165aSDave Chinner 	 * other inodes that we did not find in the list attached to the buffer
25385806165aSDave Chinner 	 * and are not already marked stale. If we can't lock it, back off and
25395806165aSDave Chinner 	 * retry.
25405806165aSDave Chinner 	 */
25415806165aSDave Chinner 	if (ip != free_ip) {
25425806165aSDave Chinner 		if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
25435806165aSDave Chinner 			rcu_read_unlock();
25445806165aSDave Chinner 			delay(1);
25455806165aSDave Chinner 			goto retry;
25465806165aSDave Chinner 		}
25475806165aSDave Chinner 
25485806165aSDave Chinner 		/*
25495806165aSDave Chinner 		 * Check the inode number again in case we're racing with
25505806165aSDave Chinner 		 * freeing in xfs_reclaim_inode().  See the comments in that
25515806165aSDave Chinner 		 * function for more information as to why the initial check is
25525806165aSDave Chinner 		 * not sufficient.
25535806165aSDave Chinner 		 */
25545806165aSDave Chinner 		if (ip->i_ino != inum) {
25555806165aSDave Chinner 			xfs_iunlock(ip, XFS_ILOCK_EXCL);
25565806165aSDave Chinner 			goto out_rcu_unlock;
25575806165aSDave Chinner 		}
25585806165aSDave Chinner 	}
25595806165aSDave Chinner 	rcu_read_unlock();
25605806165aSDave Chinner 
25615806165aSDave Chinner 	xfs_iflock(ip);
25625806165aSDave Chinner 	xfs_iflags_set(ip, XFS_ISTALE);
25635806165aSDave Chinner 
25645806165aSDave Chinner 	/*
25655806165aSDave Chinner 	 * We don't need to attach clean inodes or those only with unlogged
25665806165aSDave Chinner 	 * changes (which we throw away, anyway).
25675806165aSDave Chinner 	 */
25685806165aSDave Chinner 	if (!ip->i_itemp || xfs_inode_clean(ip)) {
25695806165aSDave Chinner 		ASSERT(ip != free_ip);
25705806165aSDave Chinner 		xfs_ifunlock(ip);
25715806165aSDave Chinner 		xfs_iunlock(ip, XFS_ILOCK_EXCL);
25725806165aSDave Chinner 		goto out_no_inode;
25735806165aSDave Chinner 	}
25745806165aSDave Chinner 	return ip;
25755806165aSDave Chinner 
25765806165aSDave Chinner out_rcu_unlock:
25775806165aSDave Chinner 	rcu_read_unlock();
25785806165aSDave Chinner out_no_inode:
25795806165aSDave Chinner 	return NULL;
25805806165aSDave Chinner }
25815806165aSDave Chinner 
25825806165aSDave Chinner /*
25830b8182dbSZhi Yong Wu  * A big issue when freeing the inode cluster is that we _cannot_ skip any
25845b3eed75SDave Chinner  * inodes that are in memory - they all must be marked stale and attached to
25855b3eed75SDave Chinner  * the cluster buffer.
25865b3eed75SDave Chinner  */
25872a30f36dSChandra Seetharaman STATIC int
25881da177e4SLinus Torvalds xfs_ifree_cluster(
25891da177e4SLinus Torvalds 	xfs_inode_t		*free_ip,
25901da177e4SLinus Torvalds 	xfs_trans_t		*tp,
259109b56604SBrian Foster 	struct xfs_icluster	*xic)
25921da177e4SLinus Torvalds {
25931da177e4SLinus Torvalds 	xfs_mount_t		*mp = free_ip->i_mount;
25941da177e4SLinus Torvalds 	int			nbufs;
25955b257b4aSDave Chinner 	int			i, j;
25963cdaa189SBrian Foster 	int			ioffset;
25971da177e4SLinus Torvalds 	xfs_daddr_t		blkno;
25981da177e4SLinus Torvalds 	xfs_buf_t		*bp;
25995b257b4aSDave Chinner 	xfs_inode_t		*ip;
2600fd9cbe51SChristoph Hellwig 	struct xfs_inode_log_item *iip;
2601643c8c05SCarlos Maiolino 	struct xfs_log_item	*lip;
26025017e97dSDave Chinner 	struct xfs_perag	*pag;
2603ef325959SDarrick J. Wong 	struct xfs_ino_geometry	*igeo = M_IGEO(mp);
260409b56604SBrian Foster 	xfs_ino_t		inum;
2605ce92464cSDarrick J. Wong 	int			error;
26061da177e4SLinus Torvalds 
260709b56604SBrian Foster 	inum = xic->first_ino;
26085017e97dSDave Chinner 	pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
2609ef325959SDarrick J. Wong 	nbufs = igeo->ialloc_blks / igeo->blocks_per_cluster;
26101da177e4SLinus Torvalds 
2611ef325959SDarrick J. Wong 	for (j = 0; j < nbufs; j++, inum += igeo->inodes_per_cluster) {
261209b56604SBrian Foster 		/*
261309b56604SBrian Foster 		 * The allocation bitmap tells us which inodes of the chunk were
261409b56604SBrian Foster 		 * physically allocated. Skip the cluster if an inode falls into
261509b56604SBrian Foster 		 * a sparse region.
261609b56604SBrian Foster 		 */
26173cdaa189SBrian Foster 		ioffset = inum - xic->first_ino;
26183cdaa189SBrian Foster 		if ((xic->alloc & XFS_INOBT_MASK(ioffset)) == 0) {
2619ef325959SDarrick J. Wong 			ASSERT(ioffset % igeo->inodes_per_cluster == 0);
262009b56604SBrian Foster 			continue;
262109b56604SBrian Foster 		}
262209b56604SBrian Foster 
26231da177e4SLinus Torvalds 		blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
26241da177e4SLinus Torvalds 					 XFS_INO_TO_AGBNO(mp, inum));
26251da177e4SLinus Torvalds 
26261da177e4SLinus Torvalds 		/*
26275b257b4aSDave Chinner 		 * We obtain and lock the backing buffer first in the process
26285b257b4aSDave Chinner 		 * here, as we have to ensure that any dirty inode that we
26295b257b4aSDave Chinner 		 * can't get the flush lock on is attached to the buffer.
26305b257b4aSDave Chinner 		 * If we scan the in-memory inodes first, then buffer IO can
26315b257b4aSDave Chinner 		 * complete before we get a lock on it, and hence we may fail
26325b257b4aSDave Chinner 		 * to mark all the active inodes on the buffer stale.
26331da177e4SLinus Torvalds 		 */
2634ce92464cSDarrick J. Wong 		error = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
2635ef325959SDarrick J. Wong 				mp->m_bsize * igeo->blocks_per_cluster,
2636ce92464cSDarrick J. Wong 				XBF_UNMAPPED, &bp);
2637ce92464cSDarrick J. Wong 		if (error)
2638ce92464cSDarrick J. Wong 			return error;
2639b0f539deSDave Chinner 
2640b0f539deSDave Chinner 		/*
2641b0f539deSDave Chinner 		 * This buffer may not have been correctly initialised as we
2642b0f539deSDave Chinner 		 * didn't read it from disk. That's not important because we are
2643b0f539deSDave Chinner 		 * only using to mark the buffer as stale in the log, and to
2644b0f539deSDave Chinner 		 * attach stale cached inodes on it. That means it will never be
2645b0f539deSDave Chinner 		 * dispatched for IO. If it is, we want to know about it, and we
2646b0f539deSDave Chinner 		 * want it to fail. We can acheive this by adding a write
2647b0f539deSDave Chinner 		 * verifier to the buffer.
2648b0f539deSDave Chinner 		 */
26491813dd64SDave Chinner 		bp->b_ops = &xfs_inode_buf_ops;
2650b0f539deSDave Chinner 
26515b257b4aSDave Chinner 		/*
26525b257b4aSDave Chinner 		 * Walk the inodes already attached to the buffer and mark them
26535b257b4aSDave Chinner 		 * stale. These will all have the flush locks held, so an
26545b3eed75SDave Chinner 		 * in-memory inode walk can't lock them. By marking them all
26555b3eed75SDave Chinner 		 * stale first, we will not attempt to lock them in the loop
26565b3eed75SDave Chinner 		 * below as the XFS_ISTALE flag will be set.
26575b257b4aSDave Chinner 		 */
2658643c8c05SCarlos Maiolino 		list_for_each_entry(lip, &bp->b_li_list, li_bio_list) {
26591da177e4SLinus Torvalds 			if (lip->li_type == XFS_LI_INODE) {
2660fd9cbe51SChristoph Hellwig 				iip = (struct xfs_inode_log_item *)lip;
26611da177e4SLinus Torvalds 				ASSERT(iip->ili_logged == 1);
2662ca30b2a7SChristoph Hellwig 				lip->li_cb = xfs_istale_done;
26637b2e2a31SDavid Chinner 				xfs_trans_ail_copy_lsn(mp->m_ail,
26647b2e2a31SDavid Chinner 							&iip->ili_flush_lsn,
26657b2e2a31SDavid Chinner 							&iip->ili_item.li_lsn);
2666e5ffd2bbSDavid Chinner 				xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
26671da177e4SLinus Torvalds 			}
26681da177e4SLinus Torvalds 		}
26691da177e4SLinus Torvalds 
26705b3eed75SDave Chinner 
26715b257b4aSDave Chinner 		/*
26725b257b4aSDave Chinner 		 * For each inode in memory attempt to add it to the inode
26735b257b4aSDave Chinner 		 * buffer and set it up for being staled on buffer IO
26745b257b4aSDave Chinner 		 * completion.  This is safe as we've locked out tail pushing
26755b257b4aSDave Chinner 		 * and flushing by locking the buffer.
26765b257b4aSDave Chinner 		 *
26775b257b4aSDave Chinner 		 * We have already marked every inode that was part of a
26785b257b4aSDave Chinner 		 * transaction stale above, which means there is no point in
26795b257b4aSDave Chinner 		 * even trying to lock them.
26805b257b4aSDave Chinner 		 */
2681ef325959SDarrick J. Wong 		for (i = 0; i < igeo->inodes_per_cluster; i++) {
26825806165aSDave Chinner 			ip = xfs_ifree_get_one_inode(pag, free_ip, inum + i);
26835806165aSDave Chinner 			if (!ip)
26845b257b4aSDave Chinner 				continue;
26855b257b4aSDave Chinner 
26865b257b4aSDave Chinner 			iip = ip->i_itemp;
2687f5d8d5c4SChristoph Hellwig 			iip->ili_last_fields = iip->ili_fields;
2688f5d8d5c4SChristoph Hellwig 			iip->ili_fields = 0;
2689fc0561ceSDave Chinner 			iip->ili_fsync_fields = 0;
26901da177e4SLinus Torvalds 			iip->ili_logged = 1;
26917b2e2a31SDavid Chinner 			xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
26927b2e2a31SDavid Chinner 						&iip->ili_item.li_lsn);
26931da177e4SLinus Torvalds 
2694ca30b2a7SChristoph Hellwig 			xfs_buf_attach_iodone(bp, xfs_istale_done,
2695ca30b2a7SChristoph Hellwig 						  &iip->ili_item);
26965b257b4aSDave Chinner 
26975b257b4aSDave Chinner 			if (ip != free_ip)
26981da177e4SLinus Torvalds 				xfs_iunlock(ip, XFS_ILOCK_EXCL);
26991da177e4SLinus Torvalds 		}
27001da177e4SLinus Torvalds 
27011da177e4SLinus Torvalds 		xfs_trans_stale_inode_buf(tp, bp);
27021da177e4SLinus Torvalds 		xfs_trans_binval(tp, bp);
27031da177e4SLinus Torvalds 	}
27041da177e4SLinus Torvalds 
27055017e97dSDave Chinner 	xfs_perag_put(pag);
27062a30f36dSChandra Seetharaman 	return 0;
27071da177e4SLinus Torvalds }
27081da177e4SLinus Torvalds 
27091da177e4SLinus Torvalds /*
27101da177e4SLinus Torvalds  * This is called to return an inode to the inode free list.
27111da177e4SLinus Torvalds  * The inode should already be truncated to 0 length and have
27121da177e4SLinus Torvalds  * no pages associated with it.  This routine also assumes that
27131da177e4SLinus Torvalds  * the inode is already a part of the transaction.
27141da177e4SLinus Torvalds  *
27151da177e4SLinus Torvalds  * The on-disk copy of the inode will have been added to the list
27161da177e4SLinus Torvalds  * of unlinked inodes in the AGI. We need to remove the inode from
27171da177e4SLinus Torvalds  * that list atomically with respect to freeing it here.
27181da177e4SLinus Torvalds  */
27191da177e4SLinus Torvalds int
27201da177e4SLinus Torvalds xfs_ifree(
27210e0417f3SBrian Foster 	struct xfs_trans	*tp,
27220e0417f3SBrian Foster 	struct xfs_inode	*ip)
27231da177e4SLinus Torvalds {
27241da177e4SLinus Torvalds 	int			error;
272509b56604SBrian Foster 	struct xfs_icluster	xic = { 0 };
27261da177e4SLinus Torvalds 
2727579aa9caSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
272854d7b5c1SDave Chinner 	ASSERT(VFS_I(ip)->i_nlink == 0);
2729daf83964SChristoph Hellwig 	ASSERT(ip->i_df.if_nextents == 0);
2730c19b3b05SDave Chinner 	ASSERT(ip->i_d.di_size == 0 || !S_ISREG(VFS_I(ip)->i_mode));
27311da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_nblocks == 0);
27321da177e4SLinus Torvalds 
27331da177e4SLinus Torvalds 	/*
27341da177e4SLinus Torvalds 	 * Pull the on-disk inode from the AGI unlinked list.
27351da177e4SLinus Torvalds 	 */
27361da177e4SLinus Torvalds 	error = xfs_iunlink_remove(tp, ip);
27371baaed8fSDave Chinner 	if (error)
27381da177e4SLinus Torvalds 		return error;
27391da177e4SLinus Torvalds 
27400e0417f3SBrian Foster 	error = xfs_difree(tp, ip->i_ino, &xic);
27411baaed8fSDave Chinner 	if (error)
27421da177e4SLinus Torvalds 		return error;
27431baaed8fSDave Chinner 
2744b2c20045SChristoph Hellwig 	/*
2745b2c20045SChristoph Hellwig 	 * Free any local-format data sitting around before we reset the
2746b2c20045SChristoph Hellwig 	 * data fork to extents format.  Note that the attr fork data has
2747b2c20045SChristoph Hellwig 	 * already been freed by xfs_attr_inactive.
2748b2c20045SChristoph Hellwig 	 */
2749f7e67b20SChristoph Hellwig 	if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
2750b2c20045SChristoph Hellwig 		kmem_free(ip->i_df.if_u1.if_data);
2751b2c20045SChristoph Hellwig 		ip->i_df.if_u1.if_data = NULL;
2752b2c20045SChristoph Hellwig 		ip->i_df.if_bytes = 0;
2753b2c20045SChristoph Hellwig 	}
275498c4f78dSDarrick J. Wong 
2755c19b3b05SDave Chinner 	VFS_I(ip)->i_mode = 0;		/* mark incore inode as free */
27561da177e4SLinus Torvalds 	ip->i_d.di_flags = 0;
2757beaae8cdSDarrick J. Wong 	ip->i_d.di_flags2 = 0;
27581da177e4SLinus Torvalds 	ip->i_d.di_dmevmask = 0;
27591da177e4SLinus Torvalds 	ip->i_d.di_forkoff = 0;		/* mark the attr fork not in use */
2760f7e67b20SChristoph Hellwig 	ip->i_df.if_format = XFS_DINODE_FMT_EXTENTS;
2761dc1baa71SEric Sandeen 
2762dc1baa71SEric Sandeen 	/* Don't attempt to replay owner changes for a deleted inode */
2763dc1baa71SEric Sandeen 	ip->i_itemp->ili_fields &= ~(XFS_ILOG_AOWNER|XFS_ILOG_DOWNER);
2764dc1baa71SEric Sandeen 
27651da177e4SLinus Torvalds 	/*
27661da177e4SLinus Torvalds 	 * Bump the generation count so no one will be confused
27671da177e4SLinus Torvalds 	 * by reincarnations of this inode.
27681da177e4SLinus Torvalds 	 */
27699e9a2674SDave Chinner 	VFS_I(ip)->i_generation++;
27701da177e4SLinus Torvalds 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
27711da177e4SLinus Torvalds 
277209b56604SBrian Foster 	if (xic.deleted)
277309b56604SBrian Foster 		error = xfs_ifree_cluster(ip, tp, &xic);
27741da177e4SLinus Torvalds 
27752a30f36dSChandra Seetharaman 	return error;
27761da177e4SLinus Torvalds }
27771da177e4SLinus Torvalds 
27781da177e4SLinus Torvalds /*
277960ec6783SChristoph Hellwig  * This is called to unpin an inode.  The caller must have the inode locked
278060ec6783SChristoph Hellwig  * in at least shared mode so that the buffer cannot be subsequently pinned
278160ec6783SChristoph Hellwig  * once someone is waiting for it to be unpinned.
27821da177e4SLinus Torvalds  */
278360ec6783SChristoph Hellwig static void
2784f392e631SChristoph Hellwig xfs_iunpin(
278560ec6783SChristoph Hellwig 	struct xfs_inode	*ip)
2786a3f74ffbSDavid Chinner {
2787579aa9caSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
2788a3f74ffbSDavid Chinner 
27894aaf15d1SDave Chinner 	trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
27904aaf15d1SDave Chinner 
2791a3f74ffbSDavid Chinner 	/* Give the log a push to start the unpinning I/O */
2792656de4ffSChristoph Hellwig 	xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0, NULL);
2793a14a348bSChristoph Hellwig 
2794a3f74ffbSDavid Chinner }
2795a3f74ffbSDavid Chinner 
2796f392e631SChristoph Hellwig static void
2797f392e631SChristoph Hellwig __xfs_iunpin_wait(
2798f392e631SChristoph Hellwig 	struct xfs_inode	*ip)
2799f392e631SChristoph Hellwig {
2800f392e631SChristoph Hellwig 	wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
2801f392e631SChristoph Hellwig 	DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
2802f392e631SChristoph Hellwig 
2803f392e631SChristoph Hellwig 	xfs_iunpin(ip);
2804f392e631SChristoph Hellwig 
2805f392e631SChristoph Hellwig 	do {
280621417136SIngo Molnar 		prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
2807f392e631SChristoph Hellwig 		if (xfs_ipincount(ip))
2808f392e631SChristoph Hellwig 			io_schedule();
2809f392e631SChristoph Hellwig 	} while (xfs_ipincount(ip));
281021417136SIngo Molnar 	finish_wait(wq, &wait.wq_entry);
2811f392e631SChristoph Hellwig }
2812f392e631SChristoph Hellwig 
2813777df5afSDave Chinner void
28141da177e4SLinus Torvalds xfs_iunpin_wait(
281560ec6783SChristoph Hellwig 	struct xfs_inode	*ip)
28161da177e4SLinus Torvalds {
2817f392e631SChristoph Hellwig 	if (xfs_ipincount(ip))
2818f392e631SChristoph Hellwig 		__xfs_iunpin_wait(ip);
28191da177e4SLinus Torvalds }
28201da177e4SLinus Torvalds 
282127320369SDave Chinner /*
282227320369SDave Chinner  * Removing an inode from the namespace involves removing the directory entry
282327320369SDave Chinner  * and dropping the link count on the inode. Removing the directory entry can
282427320369SDave Chinner  * result in locking an AGF (directory blocks were freed) and removing a link
282527320369SDave Chinner  * count can result in placing the inode on an unlinked list which results in
282627320369SDave Chinner  * locking an AGI.
282727320369SDave Chinner  *
282827320369SDave Chinner  * The big problem here is that we have an ordering constraint on AGF and AGI
282927320369SDave Chinner  * locking - inode allocation locks the AGI, then can allocate a new extent for
283027320369SDave Chinner  * new inodes, locking the AGF after the AGI. Similarly, freeing the inode
283127320369SDave Chinner  * removes the inode from the unlinked list, requiring that we lock the AGI
283227320369SDave Chinner  * first, and then freeing the inode can result in an inode chunk being freed
283327320369SDave Chinner  * and hence freeing disk space requiring that we lock an AGF.
283427320369SDave Chinner  *
283527320369SDave Chinner  * Hence the ordering that is imposed by other parts of the code is AGI before
283627320369SDave Chinner  * AGF. This means we cannot remove the directory entry before we drop the inode
283727320369SDave Chinner  * reference count and put it on the unlinked list as this results in a lock
283827320369SDave Chinner  * order of AGF then AGI, and this can deadlock against inode allocation and
283927320369SDave Chinner  * freeing. Therefore we must drop the link counts before we remove the
284027320369SDave Chinner  * directory entry.
284127320369SDave Chinner  *
284227320369SDave Chinner  * This is still safe from a transactional point of view - it is not until we
2843310a75a3SDarrick J. Wong  * get to xfs_defer_finish() that we have the possibility of multiple
284427320369SDave Chinner  * transactions in this operation. Hence as long as we remove the directory
284527320369SDave Chinner  * entry and drop the link count in the first transaction of the remove
284627320369SDave Chinner  * operation, there are no transactional constraints on the ordering here.
284727320369SDave Chinner  */
2848c24b5dfaSDave Chinner int
2849c24b5dfaSDave Chinner xfs_remove(
2850c24b5dfaSDave Chinner 	xfs_inode_t             *dp,
2851c24b5dfaSDave Chinner 	struct xfs_name		*name,
2852c24b5dfaSDave Chinner 	xfs_inode_t		*ip)
2853c24b5dfaSDave Chinner {
2854c24b5dfaSDave Chinner 	xfs_mount_t		*mp = dp->i_mount;
2855c24b5dfaSDave Chinner 	xfs_trans_t             *tp = NULL;
2856c19b3b05SDave Chinner 	int			is_dir = S_ISDIR(VFS_I(ip)->i_mode);
2857c24b5dfaSDave Chinner 	int                     error = 0;
2858c24b5dfaSDave Chinner 	uint			resblks;
2859c24b5dfaSDave Chinner 
2860c24b5dfaSDave Chinner 	trace_xfs_remove(dp, name);
2861c24b5dfaSDave Chinner 
2862c24b5dfaSDave Chinner 	if (XFS_FORCED_SHUTDOWN(mp))
28632451337dSDave Chinner 		return -EIO;
2864c24b5dfaSDave Chinner 
2865c14cfccaSDarrick J. Wong 	error = xfs_qm_dqattach(dp);
2866c24b5dfaSDave Chinner 	if (error)
2867c24b5dfaSDave Chinner 		goto std_return;
2868c24b5dfaSDave Chinner 
2869c14cfccaSDarrick J. Wong 	error = xfs_qm_dqattach(ip);
2870c24b5dfaSDave Chinner 	if (error)
2871c24b5dfaSDave Chinner 		goto std_return;
2872c24b5dfaSDave Chinner 
2873c24b5dfaSDave Chinner 	/*
2874c24b5dfaSDave Chinner 	 * We try to get the real space reservation first,
2875c24b5dfaSDave Chinner 	 * allowing for directory btree deletion(s) implying
2876c24b5dfaSDave Chinner 	 * possible bmap insert(s).  If we can't get the space
2877c24b5dfaSDave Chinner 	 * reservation then we use 0 instead, and avoid the bmap
2878c24b5dfaSDave Chinner 	 * btree insert(s) in the directory code by, if the bmap
2879c24b5dfaSDave Chinner 	 * insert tries to happen, instead trimming the LAST
2880c24b5dfaSDave Chinner 	 * block from the directory.
2881c24b5dfaSDave Chinner 	 */
2882c24b5dfaSDave Chinner 	resblks = XFS_REMOVE_SPACE_RES(mp);
2883253f4911SChristoph Hellwig 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_remove, resblks, 0, 0, &tp);
28842451337dSDave Chinner 	if (error == -ENOSPC) {
2885c24b5dfaSDave Chinner 		resblks = 0;
2886253f4911SChristoph Hellwig 		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_remove, 0, 0, 0,
2887253f4911SChristoph Hellwig 				&tp);
2888c24b5dfaSDave Chinner 	}
2889c24b5dfaSDave Chinner 	if (error) {
28902451337dSDave Chinner 		ASSERT(error != -ENOSPC);
2891253f4911SChristoph Hellwig 		goto std_return;
2892c24b5dfaSDave Chinner 	}
2893c24b5dfaSDave Chinner 
28947c2d238aSDarrick J. Wong 	xfs_lock_two_inodes(dp, XFS_ILOCK_EXCL, ip, XFS_ILOCK_EXCL);
2895c24b5dfaSDave Chinner 
289665523218SChristoph Hellwig 	xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2897c24b5dfaSDave Chinner 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2898c24b5dfaSDave Chinner 
2899c24b5dfaSDave Chinner 	/*
2900c24b5dfaSDave Chinner 	 * If we're removing a directory perform some additional validation.
2901c24b5dfaSDave Chinner 	 */
2902c24b5dfaSDave Chinner 	if (is_dir) {
290354d7b5c1SDave Chinner 		ASSERT(VFS_I(ip)->i_nlink >= 2);
290454d7b5c1SDave Chinner 		if (VFS_I(ip)->i_nlink != 2) {
29052451337dSDave Chinner 			error = -ENOTEMPTY;
2906c24b5dfaSDave Chinner 			goto out_trans_cancel;
2907c24b5dfaSDave Chinner 		}
2908c24b5dfaSDave Chinner 		if (!xfs_dir_isempty(ip)) {
29092451337dSDave Chinner 			error = -ENOTEMPTY;
2910c24b5dfaSDave Chinner 			goto out_trans_cancel;
2911c24b5dfaSDave Chinner 		}
2912c24b5dfaSDave Chinner 
291327320369SDave Chinner 		/* Drop the link from ip's "..".  */
2914c24b5dfaSDave Chinner 		error = xfs_droplink(tp, dp);
2915c24b5dfaSDave Chinner 		if (error)
291627320369SDave Chinner 			goto out_trans_cancel;
2917c24b5dfaSDave Chinner 
291827320369SDave Chinner 		/* Drop the "." link from ip to self.  */
2919c24b5dfaSDave Chinner 		error = xfs_droplink(tp, ip);
2920c24b5dfaSDave Chinner 		if (error)
292127320369SDave Chinner 			goto out_trans_cancel;
2922c24b5dfaSDave Chinner 	} else {
2923c24b5dfaSDave Chinner 		/*
2924c24b5dfaSDave Chinner 		 * When removing a non-directory we need to log the parent
2925c24b5dfaSDave Chinner 		 * inode here.  For a directory this is done implicitly
2926c24b5dfaSDave Chinner 		 * by the xfs_droplink call for the ".." entry.
2927c24b5dfaSDave Chinner 		 */
2928c24b5dfaSDave Chinner 		xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2929c24b5dfaSDave Chinner 	}
293027320369SDave Chinner 	xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2931c24b5dfaSDave Chinner 
293227320369SDave Chinner 	/* Drop the link from dp to ip. */
2933c24b5dfaSDave Chinner 	error = xfs_droplink(tp, ip);
2934c24b5dfaSDave Chinner 	if (error)
293527320369SDave Chinner 		goto out_trans_cancel;
2936c24b5dfaSDave Chinner 
2937381eee69SBrian Foster 	error = xfs_dir_removename(tp, dp, name, ip->i_ino, resblks);
293827320369SDave Chinner 	if (error) {
29392451337dSDave Chinner 		ASSERT(error != -ENOENT);
2940c8eac49eSBrian Foster 		goto out_trans_cancel;
294127320369SDave Chinner 	}
294227320369SDave Chinner 
2943c24b5dfaSDave Chinner 	/*
2944c24b5dfaSDave Chinner 	 * If this is a synchronous mount, make sure that the
2945c24b5dfaSDave Chinner 	 * remove transaction goes to disk before returning to
2946c24b5dfaSDave Chinner 	 * the user.
2947c24b5dfaSDave Chinner 	 */
2948c24b5dfaSDave Chinner 	if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
2949c24b5dfaSDave Chinner 		xfs_trans_set_sync(tp);
2950c24b5dfaSDave Chinner 
295170393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
2952c24b5dfaSDave Chinner 	if (error)
2953c24b5dfaSDave Chinner 		goto std_return;
2954c24b5dfaSDave Chinner 
29552cd2ef6aSChristoph Hellwig 	if (is_dir && xfs_inode_is_filestream(ip))
2956c24b5dfaSDave Chinner 		xfs_filestream_deassociate(ip);
2957c24b5dfaSDave Chinner 
2958c24b5dfaSDave Chinner 	return 0;
2959c24b5dfaSDave Chinner 
2960c24b5dfaSDave Chinner  out_trans_cancel:
29614906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
2962c24b5dfaSDave Chinner  std_return:
2963c24b5dfaSDave Chinner 	return error;
2964c24b5dfaSDave Chinner }
2965c24b5dfaSDave Chinner 
2966f6bba201SDave Chinner /*
2967f6bba201SDave Chinner  * Enter all inodes for a rename transaction into a sorted array.
2968f6bba201SDave Chinner  */
296995afcf5cSDave Chinner #define __XFS_SORT_INODES	5
2970f6bba201SDave Chinner STATIC void
2971f6bba201SDave Chinner xfs_sort_for_rename(
297295afcf5cSDave Chinner 	struct xfs_inode	*dp1,	/* in: old (source) directory inode */
297395afcf5cSDave Chinner 	struct xfs_inode	*dp2,	/* in: new (target) directory inode */
297495afcf5cSDave Chinner 	struct xfs_inode	*ip1,	/* in: inode of old entry */
297595afcf5cSDave Chinner 	struct xfs_inode	*ip2,	/* in: inode of new entry */
297695afcf5cSDave Chinner 	struct xfs_inode	*wip,	/* in: whiteout inode */
297795afcf5cSDave Chinner 	struct xfs_inode	**i_tab,/* out: sorted array of inodes */
297895afcf5cSDave Chinner 	int			*num_inodes)  /* in/out: inodes in array */
2979f6bba201SDave Chinner {
2980f6bba201SDave Chinner 	int			i, j;
2981f6bba201SDave Chinner 
298295afcf5cSDave Chinner 	ASSERT(*num_inodes == __XFS_SORT_INODES);
298395afcf5cSDave Chinner 	memset(i_tab, 0, *num_inodes * sizeof(struct xfs_inode *));
298495afcf5cSDave Chinner 
2985f6bba201SDave Chinner 	/*
2986f6bba201SDave Chinner 	 * i_tab contains a list of pointers to inodes.  We initialize
2987f6bba201SDave Chinner 	 * the table here & we'll sort it.  We will then use it to
2988f6bba201SDave Chinner 	 * order the acquisition of the inode locks.
2989f6bba201SDave Chinner 	 *
2990f6bba201SDave Chinner 	 * Note that the table may contain duplicates.  e.g., dp1 == dp2.
2991f6bba201SDave Chinner 	 */
299295afcf5cSDave Chinner 	i = 0;
299395afcf5cSDave Chinner 	i_tab[i++] = dp1;
299495afcf5cSDave Chinner 	i_tab[i++] = dp2;
299595afcf5cSDave Chinner 	i_tab[i++] = ip1;
299695afcf5cSDave Chinner 	if (ip2)
299795afcf5cSDave Chinner 		i_tab[i++] = ip2;
299895afcf5cSDave Chinner 	if (wip)
299995afcf5cSDave Chinner 		i_tab[i++] = wip;
300095afcf5cSDave Chinner 	*num_inodes = i;
3001f6bba201SDave Chinner 
3002f6bba201SDave Chinner 	/*
3003f6bba201SDave Chinner 	 * Sort the elements via bubble sort.  (Remember, there are at
300495afcf5cSDave Chinner 	 * most 5 elements to sort, so this is adequate.)
3005f6bba201SDave Chinner 	 */
3006f6bba201SDave Chinner 	for (i = 0; i < *num_inodes; i++) {
3007f6bba201SDave Chinner 		for (j = 1; j < *num_inodes; j++) {
3008f6bba201SDave Chinner 			if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) {
300995afcf5cSDave Chinner 				struct xfs_inode *temp = i_tab[j];
3010f6bba201SDave Chinner 				i_tab[j] = i_tab[j-1];
3011f6bba201SDave Chinner 				i_tab[j-1] = temp;
3012f6bba201SDave Chinner 			}
3013f6bba201SDave Chinner 		}
3014f6bba201SDave Chinner 	}
3015f6bba201SDave Chinner }
3016f6bba201SDave Chinner 
3017310606b0SDave Chinner static int
3018310606b0SDave Chinner xfs_finish_rename(
3019c9cfdb38SBrian Foster 	struct xfs_trans	*tp)
3020310606b0SDave Chinner {
3021310606b0SDave Chinner 	/*
3022310606b0SDave Chinner 	 * If this is a synchronous mount, make sure that the rename transaction
3023310606b0SDave Chinner 	 * goes to disk before returning to the user.
3024310606b0SDave Chinner 	 */
3025310606b0SDave Chinner 	if (tp->t_mountp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
3026310606b0SDave Chinner 		xfs_trans_set_sync(tp);
3027310606b0SDave Chinner 
302870393313SChristoph Hellwig 	return xfs_trans_commit(tp);
3029310606b0SDave Chinner }
3030310606b0SDave Chinner 
3031f6bba201SDave Chinner /*
3032d31a1825SCarlos Maiolino  * xfs_cross_rename()
3033d31a1825SCarlos Maiolino  *
3034d31a1825SCarlos Maiolino  * responsible for handling RENAME_EXCHANGE flag in renameat2() sytemcall
3035d31a1825SCarlos Maiolino  */
3036d31a1825SCarlos Maiolino STATIC int
3037d31a1825SCarlos Maiolino xfs_cross_rename(
3038d31a1825SCarlos Maiolino 	struct xfs_trans	*tp,
3039d31a1825SCarlos Maiolino 	struct xfs_inode	*dp1,
3040d31a1825SCarlos Maiolino 	struct xfs_name		*name1,
3041d31a1825SCarlos Maiolino 	struct xfs_inode	*ip1,
3042d31a1825SCarlos Maiolino 	struct xfs_inode	*dp2,
3043d31a1825SCarlos Maiolino 	struct xfs_name		*name2,
3044d31a1825SCarlos Maiolino 	struct xfs_inode	*ip2,
3045d31a1825SCarlos Maiolino 	int			spaceres)
3046d31a1825SCarlos Maiolino {
3047d31a1825SCarlos Maiolino 	int		error = 0;
3048d31a1825SCarlos Maiolino 	int		ip1_flags = 0;
3049d31a1825SCarlos Maiolino 	int		ip2_flags = 0;
3050d31a1825SCarlos Maiolino 	int		dp2_flags = 0;
3051d31a1825SCarlos Maiolino 
3052d31a1825SCarlos Maiolino 	/* Swap inode number for dirent in first parent */
3053381eee69SBrian Foster 	error = xfs_dir_replace(tp, dp1, name1, ip2->i_ino, spaceres);
3054d31a1825SCarlos Maiolino 	if (error)
3055eeacd321SDave Chinner 		goto out_trans_abort;
3056d31a1825SCarlos Maiolino 
3057d31a1825SCarlos Maiolino 	/* Swap inode number for dirent in second parent */
3058381eee69SBrian Foster 	error = xfs_dir_replace(tp, dp2, name2, ip1->i_ino, spaceres);
3059d31a1825SCarlos Maiolino 	if (error)
3060eeacd321SDave Chinner 		goto out_trans_abort;
3061d31a1825SCarlos Maiolino 
3062d31a1825SCarlos Maiolino 	/*
3063d31a1825SCarlos Maiolino 	 * If we're renaming one or more directories across different parents,
3064d31a1825SCarlos Maiolino 	 * update the respective ".." entries (and link counts) to match the new
3065d31a1825SCarlos Maiolino 	 * parents.
3066d31a1825SCarlos Maiolino 	 */
3067d31a1825SCarlos Maiolino 	if (dp1 != dp2) {
3068d31a1825SCarlos Maiolino 		dp2_flags = XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
3069d31a1825SCarlos Maiolino 
3070c19b3b05SDave Chinner 		if (S_ISDIR(VFS_I(ip2)->i_mode)) {
3071d31a1825SCarlos Maiolino 			error = xfs_dir_replace(tp, ip2, &xfs_name_dotdot,
3072381eee69SBrian Foster 						dp1->i_ino, spaceres);
3073d31a1825SCarlos Maiolino 			if (error)
3074eeacd321SDave Chinner 				goto out_trans_abort;
3075d31a1825SCarlos Maiolino 
3076d31a1825SCarlos Maiolino 			/* transfer ip2 ".." reference to dp1 */
3077c19b3b05SDave Chinner 			if (!S_ISDIR(VFS_I(ip1)->i_mode)) {
3078d31a1825SCarlos Maiolino 				error = xfs_droplink(tp, dp2);
3079d31a1825SCarlos Maiolino 				if (error)
3080eeacd321SDave Chinner 					goto out_trans_abort;
308191083269SEric Sandeen 				xfs_bumplink(tp, dp1);
3082d31a1825SCarlos Maiolino 			}
3083d31a1825SCarlos Maiolino 
3084d31a1825SCarlos Maiolino 			/*
3085d31a1825SCarlos Maiolino 			 * Although ip1 isn't changed here, userspace needs
3086d31a1825SCarlos Maiolino 			 * to be warned about the change, so that applications
3087d31a1825SCarlos Maiolino 			 * relying on it (like backup ones), will properly
3088d31a1825SCarlos Maiolino 			 * notify the change
3089d31a1825SCarlos Maiolino 			 */
3090d31a1825SCarlos Maiolino 			ip1_flags |= XFS_ICHGTIME_CHG;
3091d31a1825SCarlos Maiolino 			ip2_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
3092d31a1825SCarlos Maiolino 		}
3093d31a1825SCarlos Maiolino 
3094c19b3b05SDave Chinner 		if (S_ISDIR(VFS_I(ip1)->i_mode)) {
3095d31a1825SCarlos Maiolino 			error = xfs_dir_replace(tp, ip1, &xfs_name_dotdot,
3096381eee69SBrian Foster 						dp2->i_ino, spaceres);
3097d31a1825SCarlos Maiolino 			if (error)
3098eeacd321SDave Chinner 				goto out_trans_abort;
3099d31a1825SCarlos Maiolino 
3100d31a1825SCarlos Maiolino 			/* transfer ip1 ".." reference to dp2 */
3101c19b3b05SDave Chinner 			if (!S_ISDIR(VFS_I(ip2)->i_mode)) {
3102d31a1825SCarlos Maiolino 				error = xfs_droplink(tp, dp1);
3103d31a1825SCarlos Maiolino 				if (error)
3104eeacd321SDave Chinner 					goto out_trans_abort;
310591083269SEric Sandeen 				xfs_bumplink(tp, dp2);
3106d31a1825SCarlos Maiolino 			}
3107d31a1825SCarlos Maiolino 
3108d31a1825SCarlos Maiolino 			/*
3109d31a1825SCarlos Maiolino 			 * Although ip2 isn't changed here, userspace needs
3110d31a1825SCarlos Maiolino 			 * to be warned about the change, so that applications
3111d31a1825SCarlos Maiolino 			 * relying on it (like backup ones), will properly
3112d31a1825SCarlos Maiolino 			 * notify the change
3113d31a1825SCarlos Maiolino 			 */
3114d31a1825SCarlos Maiolino 			ip1_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
3115d31a1825SCarlos Maiolino 			ip2_flags |= XFS_ICHGTIME_CHG;
3116d31a1825SCarlos Maiolino 		}
3117d31a1825SCarlos Maiolino 	}
3118d31a1825SCarlos Maiolino 
3119d31a1825SCarlos Maiolino 	if (ip1_flags) {
3120d31a1825SCarlos Maiolino 		xfs_trans_ichgtime(tp, ip1, ip1_flags);
3121d31a1825SCarlos Maiolino 		xfs_trans_log_inode(tp, ip1, XFS_ILOG_CORE);
3122d31a1825SCarlos Maiolino 	}
3123d31a1825SCarlos Maiolino 	if (ip2_flags) {
3124d31a1825SCarlos Maiolino 		xfs_trans_ichgtime(tp, ip2, ip2_flags);
3125d31a1825SCarlos Maiolino 		xfs_trans_log_inode(tp, ip2, XFS_ILOG_CORE);
3126d31a1825SCarlos Maiolino 	}
3127d31a1825SCarlos Maiolino 	if (dp2_flags) {
3128d31a1825SCarlos Maiolino 		xfs_trans_ichgtime(tp, dp2, dp2_flags);
3129d31a1825SCarlos Maiolino 		xfs_trans_log_inode(tp, dp2, XFS_ILOG_CORE);
3130d31a1825SCarlos Maiolino 	}
3131d31a1825SCarlos Maiolino 	xfs_trans_ichgtime(tp, dp1, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3132d31a1825SCarlos Maiolino 	xfs_trans_log_inode(tp, dp1, XFS_ILOG_CORE);
3133c9cfdb38SBrian Foster 	return xfs_finish_rename(tp);
3134eeacd321SDave Chinner 
3135eeacd321SDave Chinner out_trans_abort:
31364906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
3137d31a1825SCarlos Maiolino 	return error;
3138d31a1825SCarlos Maiolino }
3139d31a1825SCarlos Maiolino 
3140d31a1825SCarlos Maiolino /*
31417dcf5c3eSDave Chinner  * xfs_rename_alloc_whiteout()
31427dcf5c3eSDave Chinner  *
31437dcf5c3eSDave Chinner  * Return a referenced, unlinked, unlocked inode that that can be used as a
31447dcf5c3eSDave Chinner  * whiteout in a rename transaction. We use a tmpfile inode here so that if we
31457dcf5c3eSDave Chinner  * crash between allocating the inode and linking it into the rename transaction
31467dcf5c3eSDave Chinner  * recovery will free the inode and we won't leak it.
31477dcf5c3eSDave Chinner  */
31487dcf5c3eSDave Chinner static int
31497dcf5c3eSDave Chinner xfs_rename_alloc_whiteout(
31507dcf5c3eSDave Chinner 	struct xfs_inode	*dp,
31517dcf5c3eSDave Chinner 	struct xfs_inode	**wip)
31527dcf5c3eSDave Chinner {
31537dcf5c3eSDave Chinner 	struct xfs_inode	*tmpfile;
31547dcf5c3eSDave Chinner 	int			error;
31557dcf5c3eSDave Chinner 
3156a1f69417SEric Sandeen 	error = xfs_create_tmpfile(dp, S_IFCHR | WHITEOUT_MODE, &tmpfile);
31577dcf5c3eSDave Chinner 	if (error)
31587dcf5c3eSDave Chinner 		return error;
31597dcf5c3eSDave Chinner 
316022419ac9SBrian Foster 	/*
316122419ac9SBrian Foster 	 * Prepare the tmpfile inode as if it were created through the VFS.
3162c4a6bf7fSDarrick J. Wong 	 * Complete the inode setup and flag it as linkable.  nlink is already
3163c4a6bf7fSDarrick J. Wong 	 * zero, so we can skip the drop_nlink.
316422419ac9SBrian Foster 	 */
31652b3d1d41SChristoph Hellwig 	xfs_setup_iops(tmpfile);
31667dcf5c3eSDave Chinner 	xfs_finish_inode_setup(tmpfile);
31677dcf5c3eSDave Chinner 	VFS_I(tmpfile)->i_state |= I_LINKABLE;
31687dcf5c3eSDave Chinner 
31697dcf5c3eSDave Chinner 	*wip = tmpfile;
31707dcf5c3eSDave Chinner 	return 0;
31717dcf5c3eSDave Chinner }
31727dcf5c3eSDave Chinner 
31737dcf5c3eSDave Chinner /*
3174f6bba201SDave Chinner  * xfs_rename
3175f6bba201SDave Chinner  */
3176f6bba201SDave Chinner int
3177f6bba201SDave Chinner xfs_rename(
31787dcf5c3eSDave Chinner 	struct xfs_inode	*src_dp,
3179f6bba201SDave Chinner 	struct xfs_name		*src_name,
31807dcf5c3eSDave Chinner 	struct xfs_inode	*src_ip,
31817dcf5c3eSDave Chinner 	struct xfs_inode	*target_dp,
3182f6bba201SDave Chinner 	struct xfs_name		*target_name,
31837dcf5c3eSDave Chinner 	struct xfs_inode	*target_ip,
3184d31a1825SCarlos Maiolino 	unsigned int		flags)
3185f6bba201SDave Chinner {
31867dcf5c3eSDave Chinner 	struct xfs_mount	*mp = src_dp->i_mount;
31877dcf5c3eSDave Chinner 	struct xfs_trans	*tp;
31887dcf5c3eSDave Chinner 	struct xfs_inode	*wip = NULL;		/* whiteout inode */
31897dcf5c3eSDave Chinner 	struct xfs_inode	*inodes[__XFS_SORT_INODES];
319093597ae8Skaixuxia 	struct xfs_buf		*agibp;
319195afcf5cSDave Chinner 	int			num_inodes = __XFS_SORT_INODES;
31922b93681fSDave Chinner 	bool			new_parent = (src_dp != target_dp);
3193c19b3b05SDave Chinner 	bool			src_is_directory = S_ISDIR(VFS_I(src_ip)->i_mode);
3194f6bba201SDave Chinner 	int			spaceres;
31957dcf5c3eSDave Chinner 	int			error;
3196f6bba201SDave Chinner 
3197f6bba201SDave Chinner 	trace_xfs_rename(src_dp, target_dp, src_name, target_name);
3198f6bba201SDave Chinner 
3199eeacd321SDave Chinner 	if ((flags & RENAME_EXCHANGE) && !target_ip)
3200eeacd321SDave Chinner 		return -EINVAL;
3201f6bba201SDave Chinner 
32027dcf5c3eSDave Chinner 	/*
32037dcf5c3eSDave Chinner 	 * If we are doing a whiteout operation, allocate the whiteout inode
32047dcf5c3eSDave Chinner 	 * we will be placing at the target and ensure the type is set
32057dcf5c3eSDave Chinner 	 * appropriately.
32067dcf5c3eSDave Chinner 	 */
32077dcf5c3eSDave Chinner 	if (flags & RENAME_WHITEOUT) {
32087dcf5c3eSDave Chinner 		ASSERT(!(flags & (RENAME_NOREPLACE | RENAME_EXCHANGE)));
32097dcf5c3eSDave Chinner 		error = xfs_rename_alloc_whiteout(target_dp, &wip);
32107dcf5c3eSDave Chinner 		if (error)
32117dcf5c3eSDave Chinner 			return error;
3212f6bba201SDave Chinner 
32137dcf5c3eSDave Chinner 		/* setup target dirent info as whiteout */
32147dcf5c3eSDave Chinner 		src_name->type = XFS_DIR3_FT_CHRDEV;
32157dcf5c3eSDave Chinner 	}
32167dcf5c3eSDave Chinner 
32177dcf5c3eSDave Chinner 	xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip, wip,
3218f6bba201SDave Chinner 				inodes, &num_inodes);
3219f6bba201SDave Chinner 
3220f6bba201SDave Chinner 	spaceres = XFS_RENAME_SPACE_RES(mp, target_name->len);
3221253f4911SChristoph Hellwig 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_rename, spaceres, 0, 0, &tp);
32222451337dSDave Chinner 	if (error == -ENOSPC) {
3223f6bba201SDave Chinner 		spaceres = 0;
3224253f4911SChristoph Hellwig 		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_rename, 0, 0, 0,
3225253f4911SChristoph Hellwig 				&tp);
3226f6bba201SDave Chinner 	}
3227445883e8SDave Chinner 	if (error)
3228253f4911SChristoph Hellwig 		goto out_release_wip;
3229f6bba201SDave Chinner 
3230f6bba201SDave Chinner 	/*
3231f6bba201SDave Chinner 	 * Attach the dquots to the inodes
3232f6bba201SDave Chinner 	 */
3233f6bba201SDave Chinner 	error = xfs_qm_vop_rename_dqattach(inodes);
3234445883e8SDave Chinner 	if (error)
3235445883e8SDave Chinner 		goto out_trans_cancel;
3236f6bba201SDave Chinner 
3237f6bba201SDave Chinner 	/*
3238f6bba201SDave Chinner 	 * Lock all the participating inodes. Depending upon whether
3239f6bba201SDave Chinner 	 * the target_name exists in the target directory, and
3240f6bba201SDave Chinner 	 * whether the target directory is the same as the source
3241f6bba201SDave Chinner 	 * directory, we can lock from 2 to 4 inodes.
3242f6bba201SDave Chinner 	 */
3243f6bba201SDave Chinner 	xfs_lock_inodes(inodes, num_inodes, XFS_ILOCK_EXCL);
3244f6bba201SDave Chinner 
3245f6bba201SDave Chinner 	/*
3246f6bba201SDave Chinner 	 * Join all the inodes to the transaction. From this point on,
3247f6bba201SDave Chinner 	 * we can rely on either trans_commit or trans_cancel to unlock
3248f6bba201SDave Chinner 	 * them.
3249f6bba201SDave Chinner 	 */
325065523218SChristoph Hellwig 	xfs_trans_ijoin(tp, src_dp, XFS_ILOCK_EXCL);
3251f6bba201SDave Chinner 	if (new_parent)
325265523218SChristoph Hellwig 		xfs_trans_ijoin(tp, target_dp, XFS_ILOCK_EXCL);
3253f6bba201SDave Chinner 	xfs_trans_ijoin(tp, src_ip, XFS_ILOCK_EXCL);
3254f6bba201SDave Chinner 	if (target_ip)
3255f6bba201SDave Chinner 		xfs_trans_ijoin(tp, target_ip, XFS_ILOCK_EXCL);
32567dcf5c3eSDave Chinner 	if (wip)
32577dcf5c3eSDave Chinner 		xfs_trans_ijoin(tp, wip, XFS_ILOCK_EXCL);
3258f6bba201SDave Chinner 
3259f6bba201SDave Chinner 	/*
3260f6bba201SDave Chinner 	 * If we are using project inheritance, we only allow renames
3261f6bba201SDave Chinner 	 * into our tree when the project IDs are the same; else the
3262f6bba201SDave Chinner 	 * tree quota mechanism would be circumvented.
3263f6bba201SDave Chinner 	 */
3264f6bba201SDave Chinner 	if (unlikely((target_dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
3265de7a866fSChristoph Hellwig 		     target_dp->i_d.di_projid != src_ip->i_d.di_projid)) {
32662451337dSDave Chinner 		error = -EXDEV;
3267445883e8SDave Chinner 		goto out_trans_cancel;
3268f6bba201SDave Chinner 	}
3269f6bba201SDave Chinner 
3270eeacd321SDave Chinner 	/* RENAME_EXCHANGE is unique from here on. */
3271eeacd321SDave Chinner 	if (flags & RENAME_EXCHANGE)
3272eeacd321SDave Chinner 		return xfs_cross_rename(tp, src_dp, src_name, src_ip,
3273d31a1825SCarlos Maiolino 					target_dp, target_name, target_ip,
3274f16dea54SBrian Foster 					spaceres);
3275d31a1825SCarlos Maiolino 
3276d31a1825SCarlos Maiolino 	/*
3277bc56ad8cSkaixuxia 	 * Check for expected errors before we dirty the transaction
3278bc56ad8cSkaixuxia 	 * so we can return an error without a transaction abort.
3279f6bba201SDave Chinner 	 */
3280f6bba201SDave Chinner 	if (target_ip == NULL) {
3281f6bba201SDave Chinner 		/*
3282f6bba201SDave Chinner 		 * If there's no space reservation, check the entry will
3283f6bba201SDave Chinner 		 * fit before actually inserting it.
3284f6bba201SDave Chinner 		 */
328594f3cad5SEric Sandeen 		if (!spaceres) {
328694f3cad5SEric Sandeen 			error = xfs_dir_canenter(tp, target_dp, target_name);
3287f6bba201SDave Chinner 			if (error)
3288445883e8SDave Chinner 				goto out_trans_cancel;
328994f3cad5SEric Sandeen 		}
3290bc56ad8cSkaixuxia 	} else {
3291bc56ad8cSkaixuxia 		/*
3292bc56ad8cSkaixuxia 		 * If target exists and it's a directory, check that whether
3293bc56ad8cSkaixuxia 		 * it can be destroyed.
3294bc56ad8cSkaixuxia 		 */
3295bc56ad8cSkaixuxia 		if (S_ISDIR(VFS_I(target_ip)->i_mode) &&
3296bc56ad8cSkaixuxia 		    (!xfs_dir_isempty(target_ip) ||
3297bc56ad8cSkaixuxia 		     (VFS_I(target_ip)->i_nlink > 2))) {
3298bc56ad8cSkaixuxia 			error = -EEXIST;
3299bc56ad8cSkaixuxia 			goto out_trans_cancel;
3300bc56ad8cSkaixuxia 		}
3301bc56ad8cSkaixuxia 	}
3302bc56ad8cSkaixuxia 
3303bc56ad8cSkaixuxia 	/*
3304bc56ad8cSkaixuxia 	 * Directory entry creation below may acquire the AGF. Remove
3305bc56ad8cSkaixuxia 	 * the whiteout from the unlinked list first to preserve correct
3306bc56ad8cSkaixuxia 	 * AGI/AGF locking order. This dirties the transaction so failures
3307bc56ad8cSkaixuxia 	 * after this point will abort and log recovery will clean up the
3308bc56ad8cSkaixuxia 	 * mess.
3309bc56ad8cSkaixuxia 	 *
3310bc56ad8cSkaixuxia 	 * For whiteouts, we need to bump the link count on the whiteout
3311bc56ad8cSkaixuxia 	 * inode. After this point, we have a real link, clear the tmpfile
3312bc56ad8cSkaixuxia 	 * state flag from the inode so it doesn't accidentally get misused
3313bc56ad8cSkaixuxia 	 * in future.
3314bc56ad8cSkaixuxia 	 */
3315bc56ad8cSkaixuxia 	if (wip) {
3316bc56ad8cSkaixuxia 		ASSERT(VFS_I(wip)->i_nlink == 0);
3317bc56ad8cSkaixuxia 		error = xfs_iunlink_remove(tp, wip);
3318bc56ad8cSkaixuxia 		if (error)
3319bc56ad8cSkaixuxia 			goto out_trans_cancel;
3320bc56ad8cSkaixuxia 
3321bc56ad8cSkaixuxia 		xfs_bumplink(tp, wip);
3322bc56ad8cSkaixuxia 		VFS_I(wip)->i_state &= ~I_LINKABLE;
3323bc56ad8cSkaixuxia 	}
3324bc56ad8cSkaixuxia 
3325bc56ad8cSkaixuxia 	/*
3326bc56ad8cSkaixuxia 	 * Set up the target.
3327bc56ad8cSkaixuxia 	 */
3328bc56ad8cSkaixuxia 	if (target_ip == NULL) {
3329f6bba201SDave Chinner 		/*
3330f6bba201SDave Chinner 		 * If target does not exist and the rename crosses
3331f6bba201SDave Chinner 		 * directories, adjust the target directory link count
3332f6bba201SDave Chinner 		 * to account for the ".." reference from the new entry.
3333f6bba201SDave Chinner 		 */
3334f6bba201SDave Chinner 		error = xfs_dir_createname(tp, target_dp, target_name,
3335381eee69SBrian Foster 					   src_ip->i_ino, spaceres);
3336f6bba201SDave Chinner 		if (error)
3337c8eac49eSBrian Foster 			goto out_trans_cancel;
3338f6bba201SDave Chinner 
3339f6bba201SDave Chinner 		xfs_trans_ichgtime(tp, target_dp,
3340f6bba201SDave Chinner 					XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3341f6bba201SDave Chinner 
3342f6bba201SDave Chinner 		if (new_parent && src_is_directory) {
334391083269SEric Sandeen 			xfs_bumplink(tp, target_dp);
3344f6bba201SDave Chinner 		}
3345f6bba201SDave Chinner 	} else { /* target_ip != NULL */
3346f6bba201SDave Chinner 		/*
3347f6bba201SDave Chinner 		 * Link the source inode under the target name.
3348f6bba201SDave Chinner 		 * If the source inode is a directory and we are moving
3349f6bba201SDave Chinner 		 * it across directories, its ".." entry will be
3350f6bba201SDave Chinner 		 * inconsistent until we replace that down below.
3351f6bba201SDave Chinner 		 *
3352f6bba201SDave Chinner 		 * In case there is already an entry with the same
3353f6bba201SDave Chinner 		 * name at the destination directory, remove it first.
3354f6bba201SDave Chinner 		 */
335593597ae8Skaixuxia 
335693597ae8Skaixuxia 		/*
335793597ae8Skaixuxia 		 * Check whether the replace operation will need to allocate
335893597ae8Skaixuxia 		 * blocks.  This happens when the shortform directory lacks
335993597ae8Skaixuxia 		 * space and we have to convert it to a block format directory.
336093597ae8Skaixuxia 		 * When more blocks are necessary, we must lock the AGI first
336193597ae8Skaixuxia 		 * to preserve locking order (AGI -> AGF).
336293597ae8Skaixuxia 		 */
336393597ae8Skaixuxia 		if (xfs_dir2_sf_replace_needblock(target_dp, src_ip->i_ino)) {
336493597ae8Skaixuxia 			error = xfs_read_agi(mp, tp,
336593597ae8Skaixuxia 					XFS_INO_TO_AGNO(mp, target_ip->i_ino),
336693597ae8Skaixuxia 					&agibp);
336793597ae8Skaixuxia 			if (error)
336893597ae8Skaixuxia 				goto out_trans_cancel;
336993597ae8Skaixuxia 		}
337093597ae8Skaixuxia 
3371f6bba201SDave Chinner 		error = xfs_dir_replace(tp, target_dp, target_name,
3372381eee69SBrian Foster 					src_ip->i_ino, spaceres);
3373f6bba201SDave Chinner 		if (error)
3374c8eac49eSBrian Foster 			goto out_trans_cancel;
3375f6bba201SDave Chinner 
3376f6bba201SDave Chinner 		xfs_trans_ichgtime(tp, target_dp,
3377f6bba201SDave Chinner 					XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3378f6bba201SDave Chinner 
3379f6bba201SDave Chinner 		/*
3380f6bba201SDave Chinner 		 * Decrement the link count on the target since the target
3381f6bba201SDave Chinner 		 * dir no longer points to it.
3382f6bba201SDave Chinner 		 */
3383f6bba201SDave Chinner 		error = xfs_droplink(tp, target_ip);
3384f6bba201SDave Chinner 		if (error)
3385c8eac49eSBrian Foster 			goto out_trans_cancel;
3386f6bba201SDave Chinner 
3387f6bba201SDave Chinner 		if (src_is_directory) {
3388f6bba201SDave Chinner 			/*
3389f6bba201SDave Chinner 			 * Drop the link from the old "." entry.
3390f6bba201SDave Chinner 			 */
3391f6bba201SDave Chinner 			error = xfs_droplink(tp, target_ip);
3392f6bba201SDave Chinner 			if (error)
3393c8eac49eSBrian Foster 				goto out_trans_cancel;
3394f6bba201SDave Chinner 		}
3395f6bba201SDave Chinner 	} /* target_ip != NULL */
3396f6bba201SDave Chinner 
3397f6bba201SDave Chinner 	/*
3398f6bba201SDave Chinner 	 * Remove the source.
3399f6bba201SDave Chinner 	 */
3400f6bba201SDave Chinner 	if (new_parent && src_is_directory) {
3401f6bba201SDave Chinner 		/*
3402f6bba201SDave Chinner 		 * Rewrite the ".." entry to point to the new
3403f6bba201SDave Chinner 		 * directory.
3404f6bba201SDave Chinner 		 */
3405f6bba201SDave Chinner 		error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot,
3406381eee69SBrian Foster 					target_dp->i_ino, spaceres);
34072451337dSDave Chinner 		ASSERT(error != -EEXIST);
3408f6bba201SDave Chinner 		if (error)
3409c8eac49eSBrian Foster 			goto out_trans_cancel;
3410f6bba201SDave Chinner 	}
3411f6bba201SDave Chinner 
3412f6bba201SDave Chinner 	/*
3413f6bba201SDave Chinner 	 * We always want to hit the ctime on the source inode.
3414f6bba201SDave Chinner 	 *
3415f6bba201SDave Chinner 	 * This isn't strictly required by the standards since the source
3416f6bba201SDave Chinner 	 * inode isn't really being changed, but old unix file systems did
3417f6bba201SDave Chinner 	 * it and some incremental backup programs won't work without it.
3418f6bba201SDave Chinner 	 */
3419f6bba201SDave Chinner 	xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG);
3420f6bba201SDave Chinner 	xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE);
3421f6bba201SDave Chinner 
3422f6bba201SDave Chinner 	/*
3423f6bba201SDave Chinner 	 * Adjust the link count on src_dp.  This is necessary when
3424f6bba201SDave Chinner 	 * renaming a directory, either within one parent when
3425f6bba201SDave Chinner 	 * the target existed, or across two parent directories.
3426f6bba201SDave Chinner 	 */
3427f6bba201SDave Chinner 	if (src_is_directory && (new_parent || target_ip != NULL)) {
3428f6bba201SDave Chinner 
3429f6bba201SDave Chinner 		/*
3430f6bba201SDave Chinner 		 * Decrement link count on src_directory since the
3431f6bba201SDave Chinner 		 * entry that's moved no longer points to it.
3432f6bba201SDave Chinner 		 */
3433f6bba201SDave Chinner 		error = xfs_droplink(tp, src_dp);
3434f6bba201SDave Chinner 		if (error)
3435c8eac49eSBrian Foster 			goto out_trans_cancel;
3436f6bba201SDave Chinner 	}
3437f6bba201SDave Chinner 
34387dcf5c3eSDave Chinner 	/*
34397dcf5c3eSDave Chinner 	 * For whiteouts, we only need to update the source dirent with the
34407dcf5c3eSDave Chinner 	 * inode number of the whiteout inode rather than removing it
34417dcf5c3eSDave Chinner 	 * altogether.
34427dcf5c3eSDave Chinner 	 */
34437dcf5c3eSDave Chinner 	if (wip) {
34447dcf5c3eSDave Chinner 		error = xfs_dir_replace(tp, src_dp, src_name, wip->i_ino,
3445381eee69SBrian Foster 					spaceres);
34467dcf5c3eSDave Chinner 	} else
3447f6bba201SDave Chinner 		error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino,
3448381eee69SBrian Foster 					   spaceres);
3449f6bba201SDave Chinner 	if (error)
3450c8eac49eSBrian Foster 		goto out_trans_cancel;
3451f6bba201SDave Chinner 
3452f6bba201SDave Chinner 	xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3453f6bba201SDave Chinner 	xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE);
3454f6bba201SDave Chinner 	if (new_parent)
3455f6bba201SDave Chinner 		xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE);
3456f6bba201SDave Chinner 
3457c9cfdb38SBrian Foster 	error = xfs_finish_rename(tp);
34587dcf5c3eSDave Chinner 	if (wip)
345944a8736bSDarrick J. Wong 		xfs_irele(wip);
34607dcf5c3eSDave Chinner 	return error;
3461f6bba201SDave Chinner 
3462445883e8SDave Chinner out_trans_cancel:
34634906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
3464253f4911SChristoph Hellwig out_release_wip:
34657dcf5c3eSDave Chinner 	if (wip)
346644a8736bSDarrick J. Wong 		xfs_irele(wip);
3467f6bba201SDave Chinner 	return error;
3468f6bba201SDave Chinner }
3469f6bba201SDave Chinner 
3470bad55843SDavid Chinner STATIC int
3471bad55843SDavid Chinner xfs_iflush_cluster(
347219429363SDave Chinner 	struct xfs_inode	*ip,
347319429363SDave Chinner 	struct xfs_buf		*bp)
3474bad55843SDavid Chinner {
347519429363SDave Chinner 	struct xfs_mount	*mp = ip->i_mount;
34765017e97dSDave Chinner 	struct xfs_perag	*pag;
3477bad55843SDavid Chinner 	unsigned long		first_index, mask;
347819429363SDave Chinner 	int			cilist_size;
347919429363SDave Chinner 	struct xfs_inode	**cilist;
348019429363SDave Chinner 	struct xfs_inode	*cip;
3481ef325959SDarrick J. Wong 	struct xfs_ino_geometry	*igeo = M_IGEO(mp);
3482f2019299SBrian Foster 	int			error = 0;
3483bad55843SDavid Chinner 	int			nr_found;
3484bad55843SDavid Chinner 	int			clcount = 0;
3485bad55843SDavid Chinner 	int			i;
3486bad55843SDavid Chinner 
34875017e97dSDave Chinner 	pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
3488bad55843SDavid Chinner 
34894b4d98ccSDarrick J. Wong 	cilist_size = igeo->inodes_per_cluster * sizeof(struct xfs_inode *);
349019429363SDave Chinner 	cilist = kmem_alloc(cilist_size, KM_MAYFAIL|KM_NOFS);
349119429363SDave Chinner 	if (!cilist)
349244b56e0aSDave Chinner 		goto out_put;
3493bad55843SDavid Chinner 
34944b4d98ccSDarrick J. Wong 	mask = ~(igeo->inodes_per_cluster - 1);
3495bad55843SDavid Chinner 	first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
34961a3e8f3dSDave Chinner 	rcu_read_lock();
3497bad55843SDavid Chinner 	/* really need a gang lookup range call here */
349819429363SDave Chinner 	nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)cilist,
34994b4d98ccSDarrick J. Wong 					first_index, igeo->inodes_per_cluster);
3500bad55843SDavid Chinner 	if (nr_found == 0)
3501bad55843SDavid Chinner 		goto out_free;
3502bad55843SDavid Chinner 
3503bad55843SDavid Chinner 	for (i = 0; i < nr_found; i++) {
350419429363SDave Chinner 		cip = cilist[i];
350519429363SDave Chinner 		if (cip == ip)
3506bad55843SDavid Chinner 			continue;
35071a3e8f3dSDave Chinner 
35081a3e8f3dSDave Chinner 		/*
35091a3e8f3dSDave Chinner 		 * because this is an RCU protected lookup, we could find a
35101a3e8f3dSDave Chinner 		 * recently freed or even reallocated inode during the lookup.
35111a3e8f3dSDave Chinner 		 * We need to check under the i_flags_lock for a valid inode
35121a3e8f3dSDave Chinner 		 * here. Skip it if it is not valid or the wrong inode.
35131a3e8f3dSDave Chinner 		 */
351419429363SDave Chinner 		spin_lock(&cip->i_flags_lock);
351519429363SDave Chinner 		if (!cip->i_ino ||
351619429363SDave Chinner 		    __xfs_iflags_test(cip, XFS_ISTALE)) {
351719429363SDave Chinner 			spin_unlock(&cip->i_flags_lock);
35181a3e8f3dSDave Chinner 			continue;
35191a3e8f3dSDave Chinner 		}
35205a90e53eSDave Chinner 
35215a90e53eSDave Chinner 		/*
35225a90e53eSDave Chinner 		 * Once we fall off the end of the cluster, no point checking
35235a90e53eSDave Chinner 		 * any more inodes in the list because they will also all be
35245a90e53eSDave Chinner 		 * outside the cluster.
35255a90e53eSDave Chinner 		 */
352619429363SDave Chinner 		if ((XFS_INO_TO_AGINO(mp, cip->i_ino) & mask) != first_index) {
352719429363SDave Chinner 			spin_unlock(&cip->i_flags_lock);
35285a90e53eSDave Chinner 			break;
35295a90e53eSDave Chinner 		}
353019429363SDave Chinner 		spin_unlock(&cip->i_flags_lock);
35311a3e8f3dSDave Chinner 
3532bad55843SDavid Chinner 		/*
3533bad55843SDavid Chinner 		 * Do an un-protected check to see if the inode is dirty and
3534bad55843SDavid Chinner 		 * is a candidate for flushing.  These checks will be repeated
3535bad55843SDavid Chinner 		 * later after the appropriate locks are acquired.
3536bad55843SDavid Chinner 		 */
353719429363SDave Chinner 		if (xfs_inode_clean(cip) && xfs_ipincount(cip) == 0)
3538bad55843SDavid Chinner 			continue;
3539bad55843SDavid Chinner 
3540bad55843SDavid Chinner 		/*
3541bad55843SDavid Chinner 		 * Try to get locks.  If any are unavailable or it is pinned,
3542bad55843SDavid Chinner 		 * then this inode cannot be flushed and is skipped.
3543bad55843SDavid Chinner 		 */
3544bad55843SDavid Chinner 
354519429363SDave Chinner 		if (!xfs_ilock_nowait(cip, XFS_ILOCK_SHARED))
3546bad55843SDavid Chinner 			continue;
354719429363SDave Chinner 		if (!xfs_iflock_nowait(cip)) {
354819429363SDave Chinner 			xfs_iunlock(cip, XFS_ILOCK_SHARED);
3549bad55843SDavid Chinner 			continue;
3550bad55843SDavid Chinner 		}
355119429363SDave Chinner 		if (xfs_ipincount(cip)) {
355219429363SDave Chinner 			xfs_ifunlock(cip);
355319429363SDave Chinner 			xfs_iunlock(cip, XFS_ILOCK_SHARED);
3554bad55843SDavid Chinner 			continue;
3555bad55843SDavid Chinner 		}
3556bad55843SDavid Chinner 
35578a17d7ddSDave Chinner 
35588a17d7ddSDave Chinner 		/*
35598a17d7ddSDave Chinner 		 * Check the inode number again, just to be certain we are not
35608a17d7ddSDave Chinner 		 * racing with freeing in xfs_reclaim_inode(). See the comments
35618a17d7ddSDave Chinner 		 * in that function for more information as to why the initial
35628a17d7ddSDave Chinner 		 * check is not sufficient.
35638a17d7ddSDave Chinner 		 */
356419429363SDave Chinner 		if (!cip->i_ino) {
356519429363SDave Chinner 			xfs_ifunlock(cip);
356619429363SDave Chinner 			xfs_iunlock(cip, XFS_ILOCK_SHARED);
3567bad55843SDavid Chinner 			continue;
3568bad55843SDavid Chinner 		}
3569bad55843SDavid Chinner 
3570bad55843SDavid Chinner 		/*
3571bad55843SDavid Chinner 		 * arriving here means that this inode can be flushed.  First
3572bad55843SDavid Chinner 		 * re-check that it's dirty before flushing.
3573bad55843SDavid Chinner 		 */
357419429363SDave Chinner 		if (!xfs_inode_clean(cip)) {
357519429363SDave Chinner 			error = xfs_iflush_int(cip, bp);
3576bad55843SDavid Chinner 			if (error) {
357719429363SDave Chinner 				xfs_iunlock(cip, XFS_ILOCK_SHARED);
3578f2019299SBrian Foster 				goto out_free;
3579bad55843SDavid Chinner 			}
3580bad55843SDavid Chinner 			clcount++;
3581bad55843SDavid Chinner 		} else {
358219429363SDave Chinner 			xfs_ifunlock(cip);
3583bad55843SDavid Chinner 		}
358419429363SDave Chinner 		xfs_iunlock(cip, XFS_ILOCK_SHARED);
3585bad55843SDavid Chinner 	}
3586bad55843SDavid Chinner 
3587bad55843SDavid Chinner 	if (clcount) {
3588ff6d6af2SBill O'Donnell 		XFS_STATS_INC(mp, xs_icluster_flushcnt);
3589ff6d6af2SBill O'Donnell 		XFS_STATS_ADD(mp, xs_icluster_flushinode, clcount);
3590bad55843SDavid Chinner 	}
3591bad55843SDavid Chinner 
3592bad55843SDavid Chinner out_free:
35931a3e8f3dSDave Chinner 	rcu_read_unlock();
359419429363SDave Chinner 	kmem_free(cilist);
359544b56e0aSDave Chinner out_put:
359644b56e0aSDave Chinner 	xfs_perag_put(pag);
3597f2019299SBrian Foster 	return error;
3598bad55843SDavid Chinner }
3599bad55843SDavid Chinner 
36001da177e4SLinus Torvalds /*
36014c46819aSChristoph Hellwig  * Flush dirty inode metadata into the backing buffer.
36024c46819aSChristoph Hellwig  *
36034c46819aSChristoph Hellwig  * The caller must have the inode lock and the inode flush lock held.  The
36044c46819aSChristoph Hellwig  * inode lock will still be held upon return to the caller, and the inode
36054c46819aSChristoph Hellwig  * flush lock will be released after the inode has reached the disk.
36064c46819aSChristoph Hellwig  *
36074c46819aSChristoph Hellwig  * The caller must write out the buffer returned in *bpp and release it.
36081da177e4SLinus Torvalds  */
36091da177e4SLinus Torvalds int
36101da177e4SLinus Torvalds xfs_iflush(
36114c46819aSChristoph Hellwig 	struct xfs_inode	*ip,
36124c46819aSChristoph Hellwig 	struct xfs_buf		**bpp)
36131da177e4SLinus Torvalds {
36144c46819aSChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
3615b1438f47SDave Chinner 	struct xfs_buf		*bp = NULL;
36164c46819aSChristoph Hellwig 	struct xfs_dinode	*dip;
36171da177e4SLinus Torvalds 	int			error;
36181da177e4SLinus Torvalds 
3619ff6d6af2SBill O'Donnell 	XFS_STATS_INC(mp, xs_iflush_count);
36201da177e4SLinus Torvalds 
3621579aa9caSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
3622474fce06SChristoph Hellwig 	ASSERT(xfs_isiflocked(ip));
3623f7e67b20SChristoph Hellwig 	ASSERT(ip->i_df.if_format != XFS_DINODE_FMT_BTREE ||
3624daf83964SChristoph Hellwig 	       ip->i_df.if_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
36251da177e4SLinus Torvalds 
36264c46819aSChristoph Hellwig 	*bpp = NULL;
36271da177e4SLinus Torvalds 
36281da177e4SLinus Torvalds 	xfs_iunpin_wait(ip);
36291da177e4SLinus Torvalds 
36301da177e4SLinus Torvalds 	/*
36314b6a4688SDave Chinner 	 * For stale inodes we cannot rely on the backing buffer remaining
36324b6a4688SDave Chinner 	 * stale in cache for the remaining life of the stale inode and so
3633475ee413SChristoph Hellwig 	 * xfs_imap_to_bp() below may give us a buffer that no longer contains
36344b6a4688SDave Chinner 	 * inodes below. We have to check this after ensuring the inode is
36354b6a4688SDave Chinner 	 * unpinned so that it is safe to reclaim the stale inode after the
36364b6a4688SDave Chinner 	 * flush call.
36374b6a4688SDave Chinner 	 */
36384b6a4688SDave Chinner 	if (xfs_iflags_test(ip, XFS_ISTALE)) {
36394b6a4688SDave Chinner 		xfs_ifunlock(ip);
36404b6a4688SDave Chinner 		return 0;
36414b6a4688SDave Chinner 	}
36424b6a4688SDave Chinner 
36434b6a4688SDave Chinner 	/*
3644b1438f47SDave Chinner 	 * Get the buffer containing the on-disk inode. We are doing a try-lock
3645f2019299SBrian Foster 	 * operation here, so we may get an EAGAIN error. In that case, return
3646f2019299SBrian Foster 	 * leaving the inode dirty.
3647b1438f47SDave Chinner 	 *
3648b1438f47SDave Chinner 	 * If we get any other error, we effectively have a corruption situation
3649f2019299SBrian Foster 	 * and we cannot flush the inode. Abort the flush and shut down.
3650a3f74ffbSDavid Chinner 	 */
3651c1995079SBrian Foster 	error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK);
3652b1438f47SDave Chinner 	if (error == -EAGAIN) {
3653a3f74ffbSDavid Chinner 		xfs_ifunlock(ip);
3654a3f74ffbSDavid Chinner 		return error;
3655a3f74ffbSDavid Chinner 	}
3656b1438f47SDave Chinner 	if (error)
3657f2019299SBrian Foster 		goto abort;
36581da177e4SLinus Torvalds 
36591da177e4SLinus Torvalds 	/*
3660a3f74ffbSDavid Chinner 	 * If the buffer is pinned then push on the log now so we won't
3661a3f74ffbSDavid Chinner 	 * get stuck waiting in the write for too long.
3662a3f74ffbSDavid Chinner 	 */
3663811e64c7SChandra Seetharaman 	if (xfs_buf_ispinned(bp))
3664a14a348bSChristoph Hellwig 		xfs_log_force(mp, 0);
3665a3f74ffbSDavid Chinner 
3666a3f74ffbSDavid Chinner 	/*
3667f2019299SBrian Foster 	 * Flush the provided inode then attempt to gather others from the
3668f2019299SBrian Foster 	 * cluster into the write.
3669e53946dbSDave Chinner 	 *
3670f2019299SBrian Foster 	 * Note: Once we attempt to flush an inode, we must run buffer
3671f2019299SBrian Foster 	 * completion callbacks on any failure. If this fails, simulate an I/O
3672f2019299SBrian Foster 	 * failure on the buffer and shut down.
36731da177e4SLinus Torvalds 	 */
3674f2019299SBrian Foster 	error = xfs_iflush_int(ip, bp);
3675f2019299SBrian Foster 	if (!error)
3676bad55843SDavid Chinner 		error = xfs_iflush_cluster(ip, bp);
3677f2019299SBrian Foster 	if (error) {
3678f2019299SBrian Foster 		bp->b_flags |= XBF_ASYNC;
3679f2019299SBrian Foster 		xfs_buf_ioend_fail(bp);
3680f2019299SBrian Foster 		goto shutdown;
3681f2019299SBrian Foster 	}
36821da177e4SLinus Torvalds 
36834c46819aSChristoph Hellwig 	*bpp = bp;
36844c46819aSChristoph Hellwig 	return 0;
36851da177e4SLinus Torvalds 
3686f2019299SBrian Foster abort:
368788fc1879SBrian Foster 	xfs_iflush_abort(ip);
3688f2019299SBrian Foster shutdown:
3689f2019299SBrian Foster 	xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
369032ce90a4SChristoph Hellwig 	return error;
36911da177e4SLinus Torvalds }
36921da177e4SLinus Torvalds 
36931da177e4SLinus Torvalds STATIC int
36941da177e4SLinus Torvalds xfs_iflush_int(
369593848a99SChristoph Hellwig 	struct xfs_inode	*ip,
369693848a99SChristoph Hellwig 	struct xfs_buf		*bp)
36971da177e4SLinus Torvalds {
369893848a99SChristoph Hellwig 	struct xfs_inode_log_item *iip = ip->i_itemp;
369993848a99SChristoph Hellwig 	struct xfs_dinode	*dip;
370093848a99SChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
3701f2019299SBrian Foster 	int			error;
37021da177e4SLinus Torvalds 
3703579aa9caSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
3704474fce06SChristoph Hellwig 	ASSERT(xfs_isiflocked(ip));
3705f7e67b20SChristoph Hellwig 	ASSERT(ip->i_df.if_format != XFS_DINODE_FMT_BTREE ||
3706daf83964SChristoph Hellwig 	       ip->i_df.if_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
370793848a99SChristoph Hellwig 	ASSERT(iip != NULL && iip->ili_fields != 0);
37081da177e4SLinus Torvalds 
370988ee2df7SChristoph Hellwig 	dip = xfs_buf_offset(bp, ip->i_imap.im_boffset);
37101da177e4SLinus Torvalds 
3711f2019299SBrian Foster 	/*
3712f2019299SBrian Foster 	 * We don't flush the inode if any of the following checks fail, but we
3713f2019299SBrian Foster 	 * do still update the log item and attach to the backing buffer as if
3714f2019299SBrian Foster 	 * the flush happened. This is a formality to facilitate predictable
3715f2019299SBrian Foster 	 * error handling as the caller will shutdown and fail the buffer.
3716f2019299SBrian Foster 	 */
3717f2019299SBrian Foster 	error = -EFSCORRUPTED;
371869ef921bSChristoph Hellwig 	if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
37199e24cfd0SDarrick J. Wong 			       mp, XFS_ERRTAG_IFLUSH_1)) {
37206a19d939SDave Chinner 		xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3721c9690043SDarrick J. Wong 			"%s: Bad inode %Lu magic number 0x%x, ptr "PTR_FMT,
37226a19d939SDave Chinner 			__func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
3723f2019299SBrian Foster 		goto flush_out;
37241da177e4SLinus Torvalds 	}
3725c19b3b05SDave Chinner 	if (S_ISREG(VFS_I(ip)->i_mode)) {
37261da177e4SLinus Torvalds 		if (XFS_TEST_ERROR(
3727f7e67b20SChristoph Hellwig 		    ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS &&
3728f7e67b20SChristoph Hellwig 		    ip->i_df.if_format != XFS_DINODE_FMT_BTREE,
37299e24cfd0SDarrick J. Wong 		    mp, XFS_ERRTAG_IFLUSH_3)) {
37306a19d939SDave Chinner 			xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3731c9690043SDarrick J. Wong 				"%s: Bad regular inode %Lu, ptr "PTR_FMT,
37326a19d939SDave Chinner 				__func__, ip->i_ino, ip);
3733f2019299SBrian Foster 			goto flush_out;
37341da177e4SLinus Torvalds 		}
3735c19b3b05SDave Chinner 	} else if (S_ISDIR(VFS_I(ip)->i_mode)) {
37361da177e4SLinus Torvalds 		if (XFS_TEST_ERROR(
3737f7e67b20SChristoph Hellwig 		    ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS &&
3738f7e67b20SChristoph Hellwig 		    ip->i_df.if_format != XFS_DINODE_FMT_BTREE &&
3739f7e67b20SChristoph Hellwig 		    ip->i_df.if_format != XFS_DINODE_FMT_LOCAL,
37409e24cfd0SDarrick J. Wong 		    mp, XFS_ERRTAG_IFLUSH_4)) {
37416a19d939SDave Chinner 			xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3742c9690043SDarrick J. Wong 				"%s: Bad directory inode %Lu, ptr "PTR_FMT,
37436a19d939SDave Chinner 				__func__, ip->i_ino, ip);
3744f2019299SBrian Foster 			goto flush_out;
37451da177e4SLinus Torvalds 		}
37461da177e4SLinus Torvalds 	}
3747daf83964SChristoph Hellwig 	if (XFS_TEST_ERROR(ip->i_df.if_nextents + xfs_ifork_nextents(ip->i_afp) >
37489e24cfd0SDarrick J. Wong 				ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5)) {
37496a19d939SDave Chinner 		xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
37506a19d939SDave Chinner 			"%s: detected corrupt incore inode %Lu, "
3751c9690043SDarrick J. Wong 			"total extents = %d, nblocks = %Ld, ptr "PTR_FMT,
37526a19d939SDave Chinner 			__func__, ip->i_ino,
3753daf83964SChristoph Hellwig 			ip->i_df.if_nextents + xfs_ifork_nextents(ip->i_afp),
37546a19d939SDave Chinner 			ip->i_d.di_nblocks, ip);
3755f2019299SBrian Foster 		goto flush_out;
37561da177e4SLinus Torvalds 	}
37571da177e4SLinus Torvalds 	if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
37589e24cfd0SDarrick J. Wong 				mp, XFS_ERRTAG_IFLUSH_6)) {
37596a19d939SDave Chinner 		xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3760c9690043SDarrick J. Wong 			"%s: bad inode %Lu, forkoff 0x%x, ptr "PTR_FMT,
37616a19d939SDave Chinner 			__func__, ip->i_ino, ip->i_d.di_forkoff, ip);
3762f2019299SBrian Foster 		goto flush_out;
37631da177e4SLinus Torvalds 	}
3764e60896d8SDave Chinner 
37651da177e4SLinus Torvalds 	/*
3766263997a6SDave Chinner 	 * Inode item log recovery for v2 inodes are dependent on the
3767e60896d8SDave Chinner 	 * di_flushiter count for correct sequencing. We bump the flush
3768e60896d8SDave Chinner 	 * iteration count so we can detect flushes which postdate a log record
3769e60896d8SDave Chinner 	 * during recovery. This is redundant as we now log every change and
3770e60896d8SDave Chinner 	 * hence this can't happen but we need to still do it to ensure
3771e60896d8SDave Chinner 	 * backwards compatibility with old kernels that predate logging all
3772e60896d8SDave Chinner 	 * inode changes.
37731da177e4SLinus Torvalds 	 */
37746471e9c5SChristoph Hellwig 	if (!xfs_sb_version_has_v3inode(&mp->m_sb))
37751da177e4SLinus Torvalds 		ip->i_d.di_flushiter++;
37761da177e4SLinus Torvalds 
37770f45a1b2SChristoph Hellwig 	/*
37780f45a1b2SChristoph Hellwig 	 * If there are inline format data / attr forks attached to this inode,
37790f45a1b2SChristoph Hellwig 	 * make sure they are not corrupt.
37800f45a1b2SChristoph Hellwig 	 */
3781f7e67b20SChristoph Hellwig 	if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL &&
37820f45a1b2SChristoph Hellwig 	    xfs_ifork_verify_local_data(ip))
37830f45a1b2SChristoph Hellwig 		goto flush_out;
3784f7e67b20SChristoph Hellwig 	if (ip->i_afp && ip->i_afp->if_format == XFS_DINODE_FMT_LOCAL &&
37850f45a1b2SChristoph Hellwig 	    xfs_ifork_verify_local_attr(ip))
3786f2019299SBrian Foster 		goto flush_out;
3787005c5db8SDarrick J. Wong 
37881da177e4SLinus Torvalds 	/*
37893987848cSDave Chinner 	 * Copy the dirty parts of the inode into the on-disk inode.  We always
37903987848cSDave Chinner 	 * copy out the core of the inode, because if the inode is dirty at all
37913987848cSDave Chinner 	 * the core must be.
37921da177e4SLinus Torvalds 	 */
379393f958f9SDave Chinner 	xfs_inode_to_disk(ip, dip, iip->ili_item.li_lsn);
37941da177e4SLinus Torvalds 
37951da177e4SLinus Torvalds 	/* Wrap, we never let the log put out DI_MAX_FLUSH */
37961da177e4SLinus Torvalds 	if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
37971da177e4SLinus Torvalds 		ip->i_d.di_flushiter = 0;
37981da177e4SLinus Torvalds 
3799005c5db8SDarrick J. Wong 	xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK);
3800005c5db8SDarrick J. Wong 	if (XFS_IFORK_Q(ip))
3801005c5db8SDarrick J. Wong 		xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK);
38021da177e4SLinus Torvalds 	xfs_inobp_check(mp, bp);
38031da177e4SLinus Torvalds 
38041da177e4SLinus Torvalds 	/*
3805f5d8d5c4SChristoph Hellwig 	 * We've recorded everything logged in the inode, so we'd like to clear
3806f5d8d5c4SChristoph Hellwig 	 * the ili_fields bits so we don't log and flush things unnecessarily.
3807f5d8d5c4SChristoph Hellwig 	 * However, we can't stop logging all this information until the data
3808f5d8d5c4SChristoph Hellwig 	 * we've copied into the disk buffer is written to disk.  If we did we
3809f5d8d5c4SChristoph Hellwig 	 * might overwrite the copy of the inode in the log with all the data
3810f5d8d5c4SChristoph Hellwig 	 * after re-logging only part of it, and in the face of a crash we
3811f5d8d5c4SChristoph Hellwig 	 * wouldn't have all the data we need to recover.
38121da177e4SLinus Torvalds 	 *
3813f5d8d5c4SChristoph Hellwig 	 * What we do is move the bits to the ili_last_fields field.  When
3814f5d8d5c4SChristoph Hellwig 	 * logging the inode, these bits are moved back to the ili_fields field.
3815f5d8d5c4SChristoph Hellwig 	 * In the xfs_iflush_done() routine we clear ili_last_fields, since we
3816f5d8d5c4SChristoph Hellwig 	 * know that the information those bits represent is permanently on
3817f5d8d5c4SChristoph Hellwig 	 * disk.  As long as the flush completes before the inode is logged
3818f5d8d5c4SChristoph Hellwig 	 * again, then both ili_fields and ili_last_fields will be cleared.
38191da177e4SLinus Torvalds 	 *
3820f5d8d5c4SChristoph Hellwig 	 * We can play with the ili_fields bits here, because the inode lock
3821f5d8d5c4SChristoph Hellwig 	 * must be held exclusively in order to set bits there and the flush
3822f5d8d5c4SChristoph Hellwig 	 * lock protects the ili_last_fields bits.  Set ili_logged so the flush
3823f5d8d5c4SChristoph Hellwig 	 * done routine can tell whether or not to look in the AIL.  Also, store
3824f5d8d5c4SChristoph Hellwig 	 * the current LSN of the inode so that we can tell whether the item has
3825f5d8d5c4SChristoph Hellwig 	 * moved in the AIL from xfs_iflush_done().  In order to read the lsn we
3826f5d8d5c4SChristoph Hellwig 	 * need the AIL lock, because it is a 64 bit value that cannot be read
3827f5d8d5c4SChristoph Hellwig 	 * atomically.
38281da177e4SLinus Torvalds 	 */
3829f2019299SBrian Foster 	error = 0;
3830f2019299SBrian Foster flush_out:
3831f5d8d5c4SChristoph Hellwig 	iip->ili_last_fields = iip->ili_fields;
3832f5d8d5c4SChristoph Hellwig 	iip->ili_fields = 0;
3833fc0561ceSDave Chinner 	iip->ili_fsync_fields = 0;
38341da177e4SLinus Torvalds 	iip->ili_logged = 1;
38351da177e4SLinus Torvalds 
38367b2e2a31SDavid Chinner 	xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
38377b2e2a31SDavid Chinner 				&iip->ili_item.li_lsn);
38381da177e4SLinus Torvalds 
38391da177e4SLinus Torvalds 	/*
3840f2019299SBrian Foster 	 * Attach the inode item callback to the buffer whether the flush
3841f2019299SBrian Foster 	 * succeeded or not. If not, the caller will shut down and fail I/O
3842f2019299SBrian Foster 	 * completion on the buffer to remove the inode from the AIL and release
3843f2019299SBrian Foster 	 * the flush lock.
38441da177e4SLinus Torvalds 	 */
3845ca30b2a7SChristoph Hellwig 	xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
38461da177e4SLinus Torvalds 
384793848a99SChristoph Hellwig 	/* generate the checksum. */
384893848a99SChristoph Hellwig 	xfs_dinode_calc_crc(mp, dip);
384993848a99SChristoph Hellwig 
3850643c8c05SCarlos Maiolino 	ASSERT(!list_empty(&bp->b_li_list));
3851cb669ca5SChristoph Hellwig 	ASSERT(bp->b_iodone != NULL);
3852f2019299SBrian Foster 	return error;
38531da177e4SLinus Torvalds }
385444a8736bSDarrick J. Wong 
385544a8736bSDarrick J. Wong /* Release an inode. */
385644a8736bSDarrick J. Wong void
385744a8736bSDarrick J. Wong xfs_irele(
385844a8736bSDarrick J. Wong 	struct xfs_inode	*ip)
385944a8736bSDarrick J. Wong {
386044a8736bSDarrick J. Wong 	trace_xfs_irele(ip, _RET_IP_);
386144a8736bSDarrick J. Wong 	iput(VFS_I(ip));
386244a8736bSDarrick J. Wong }
386354fbdd10SChristoph Hellwig 
386454fbdd10SChristoph Hellwig /*
386554fbdd10SChristoph Hellwig  * Ensure all commited transactions touching the inode are written to the log.
386654fbdd10SChristoph Hellwig  */
386754fbdd10SChristoph Hellwig int
386854fbdd10SChristoph Hellwig xfs_log_force_inode(
386954fbdd10SChristoph Hellwig 	struct xfs_inode	*ip)
387054fbdd10SChristoph Hellwig {
387154fbdd10SChristoph Hellwig 	xfs_lsn_t		lsn = 0;
387254fbdd10SChristoph Hellwig 
387354fbdd10SChristoph Hellwig 	xfs_ilock(ip, XFS_ILOCK_SHARED);
387454fbdd10SChristoph Hellwig 	if (xfs_ipincount(ip))
387554fbdd10SChristoph Hellwig 		lsn = ip->i_itemp->ili_last_lsn;
387654fbdd10SChristoph Hellwig 	xfs_iunlock(ip, XFS_ILOCK_SHARED);
387754fbdd10SChristoph Hellwig 
387854fbdd10SChristoph Hellwig 	if (!lsn)
387954fbdd10SChristoph Hellwig 		return 0;
388054fbdd10SChristoph Hellwig 	return xfs_log_force_lsn(ip->i_mount, lsn, XFS_LOG_SYNC, NULL);
388154fbdd10SChristoph Hellwig }
3882