xref: /openbmc/linux/fs/xfs/xfs_inode.c (revision c8eac49ef798a7d00240847f63902caa1388241a)
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  */
640ebd81dSRobert P. J. Day #include <linux/log2.h>
7f0e28280SJeff Layton #include <linux/iversion.h>
840ebd81dSRobert P. J. Day 
91da177e4SLinus Torvalds #include "xfs.h"
10a844f451SNathan Scott #include "xfs_fs.h"
1170a9883cSDave Chinner #include "xfs_shared.h"
12239880efSDave Chinner #include "xfs_format.h"
13239880efSDave Chinner #include "xfs_log_format.h"
14239880efSDave Chinner #include "xfs_trans_resv.h"
151da177e4SLinus Torvalds #include "xfs_sb.h"
161da177e4SLinus Torvalds #include "xfs_mount.h"
173ab78df2SDarrick J. Wong #include "xfs_defer.h"
18a4fbe6abSDave Chinner #include "xfs_inode.h"
1957062787SDave Chinner #include "xfs_da_format.h"
20c24b5dfaSDave Chinner #include "xfs_da_btree.h"
21c24b5dfaSDave Chinner #include "xfs_dir2.h"
22a844f451SNathan Scott #include "xfs_attr_sf.h"
23c24b5dfaSDave Chinner #include "xfs_attr.h"
24239880efSDave Chinner #include "xfs_trans_space.h"
25239880efSDave Chinner #include "xfs_trans.h"
261da177e4SLinus Torvalds #include "xfs_buf_item.h"
27a844f451SNathan Scott #include "xfs_inode_item.h"
28a844f451SNathan Scott #include "xfs_ialloc.h"
29a844f451SNathan Scott #include "xfs_bmap.h"
3068988114SDave Chinner #include "xfs_bmap_util.h"
31e9e899a2SDarrick J. Wong #include "xfs_errortag.h"
321da177e4SLinus Torvalds #include "xfs_error.h"
331da177e4SLinus Torvalds #include "xfs_quota.h"
342a82b8beSDavid Chinner #include "xfs_filestream.h"
3593848a99SChristoph Hellwig #include "xfs_cksum.h"
360b1b213fSChristoph Hellwig #include "xfs_trace.h"
3733479e05SDave Chinner #include "xfs_icache.h"
38c24b5dfaSDave Chinner #include "xfs_symlink.h"
39239880efSDave Chinner #include "xfs_trans_priv.h"
40239880efSDave Chinner #include "xfs_log.h"
41a4fbe6abSDave Chinner #include "xfs_bmap_btree.h"
42aa8968f2SDarrick J. Wong #include "xfs_reflink.h"
43005c5db8SDarrick J. Wong #include "xfs_dir2_priv.h"
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds kmem_zone_t *xfs_inode_zone;
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds /*
488f04c47aSChristoph Hellwig  * Used in xfs_itruncate_extents().  This is the maximum number of extents
491da177e4SLinus Torvalds  * freed from a file in a single transaction.
501da177e4SLinus Torvalds  */
511da177e4SLinus Torvalds #define	XFS_ITRUNC_MAX_EXTENTS	2
521da177e4SLinus Torvalds 
5354d7b5c1SDave Chinner STATIC int xfs_iflush_int(struct xfs_inode *, struct xfs_buf *);
5454d7b5c1SDave Chinner STATIC int xfs_iunlink(struct xfs_trans *, struct xfs_inode *);
5554d7b5c1SDave Chinner STATIC int xfs_iunlink_remove(struct xfs_trans *, struct xfs_inode *);
56ab297431SZhi Yong Wu 
572a0ec1d9SDave Chinner /*
582a0ec1d9SDave Chinner  * helper function to extract extent size hint from inode
592a0ec1d9SDave Chinner  */
602a0ec1d9SDave Chinner xfs_extlen_t
612a0ec1d9SDave Chinner xfs_get_extsz_hint(
622a0ec1d9SDave Chinner 	struct xfs_inode	*ip)
632a0ec1d9SDave Chinner {
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 
115309ecac8SChristoph Hellwig 	if (ip->i_d.di_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 
128efa70be1SChristoph Hellwig 	if (ip->i_d.di_aformat == XFS_DINODE_FMT_BTREE &&
129efa70be1SChristoph Hellwig 	    (ip->i_afp->if_flags & XFS_IFEXTENTS) == 0)
130efa70be1SChristoph Hellwig 		lock_mode = XFS_ILOCK_EXCL;
131efa70be1SChristoph Hellwig 	xfs_ilock(ip, lock_mode);
132efa70be1SChristoph Hellwig 	return lock_mode;
133fa96acadSDave Chinner }
134fa96acadSDave Chinner 
135fa96acadSDave Chinner /*
13665523218SChristoph Hellwig  * In addition to i_rwsem in the VFS inode, the xfs inode contains 2
13765523218SChristoph Hellwig  * multi-reader locks: i_mmap_lock and the i_lock.  This routine allows
13865523218SChristoph Hellwig  * various combinations of the locks to be obtained.
139fa96acadSDave Chinner  *
140653c60b6SDave Chinner  * The 3 locks should always be ordered so that the IO lock is obtained first,
141653c60b6SDave Chinner  * the mmap lock second and the ilock last in order to prevent deadlock.
142fa96acadSDave Chinner  *
143653c60b6SDave Chinner  * Basic locking order:
144653c60b6SDave Chinner  *
14565523218SChristoph Hellwig  * i_rwsem -> i_mmap_lock -> page_lock -> i_ilock
146653c60b6SDave Chinner  *
147653c60b6SDave Chinner  * mmap_sem locking order:
148653c60b6SDave Chinner  *
14965523218SChristoph Hellwig  * i_rwsem -> page lock -> mmap_sem
150653c60b6SDave Chinner  * mmap_sem -> i_mmap_lock -> page_lock
151653c60b6SDave Chinner  *
152653c60b6SDave Chinner  * The difference in mmap_sem locking order mean that we cannot hold the
153653c60b6SDave Chinner  * i_mmap_lock over syscall based read(2)/write(2) based IO. These IO paths can
154653c60b6SDave Chinner  * fault in pages during copy in/out (for buffered IO) or require the mmap_sem
155653c60b6SDave Chinner  * in get_user_pages() to map the user pages into the kernel address space for
15665523218SChristoph Hellwig  * direct IO. Similarly the i_rwsem cannot be taken inside a page fault because
157653c60b6SDave Chinner  * page faults already hold the mmap_sem.
158653c60b6SDave Chinner  *
159653c60b6SDave Chinner  * Hence to serialise fully against both syscall and mmap based IO, we need to
16065523218SChristoph Hellwig  * take both the i_rwsem and the i_mmap_lock. These locks should *only* be both
161653c60b6SDave Chinner  * taken in places where we need to invalidate the page cache in a race
162653c60b6SDave Chinner  * free manner (e.g. truncate, hole punch and other extent manipulation
163653c60b6SDave Chinner  * functions).
164fa96acadSDave Chinner  */
165fa96acadSDave Chinner void
166fa96acadSDave Chinner xfs_ilock(
167fa96acadSDave Chinner 	xfs_inode_t		*ip,
168fa96acadSDave Chinner 	uint			lock_flags)
169fa96acadSDave Chinner {
170fa96acadSDave Chinner 	trace_xfs_ilock(ip, lock_flags, _RET_IP_);
171fa96acadSDave Chinner 
172fa96acadSDave Chinner 	/*
173fa96acadSDave Chinner 	 * You can't set both SHARED and EXCL for the same lock,
174fa96acadSDave Chinner 	 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
175fa96acadSDave Chinner 	 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
176fa96acadSDave Chinner 	 */
177fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
178fa96acadSDave Chinner 	       (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
179653c60b6SDave Chinner 	ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
180653c60b6SDave Chinner 	       (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
181fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
182fa96acadSDave Chinner 	       (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
1830952c818SDave Chinner 	ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
184fa96acadSDave Chinner 
18565523218SChristoph Hellwig 	if (lock_flags & XFS_IOLOCK_EXCL) {
18665523218SChristoph Hellwig 		down_write_nested(&VFS_I(ip)->i_rwsem,
18765523218SChristoph Hellwig 				  XFS_IOLOCK_DEP(lock_flags));
18865523218SChristoph Hellwig 	} else if (lock_flags & XFS_IOLOCK_SHARED) {
18965523218SChristoph Hellwig 		down_read_nested(&VFS_I(ip)->i_rwsem,
19065523218SChristoph Hellwig 				 XFS_IOLOCK_DEP(lock_flags));
19165523218SChristoph Hellwig 	}
192fa96acadSDave Chinner 
193653c60b6SDave Chinner 	if (lock_flags & XFS_MMAPLOCK_EXCL)
194653c60b6SDave Chinner 		mrupdate_nested(&ip->i_mmaplock, XFS_MMAPLOCK_DEP(lock_flags));
195653c60b6SDave Chinner 	else if (lock_flags & XFS_MMAPLOCK_SHARED)
196653c60b6SDave Chinner 		mraccess_nested(&ip->i_mmaplock, XFS_MMAPLOCK_DEP(lock_flags));
197653c60b6SDave Chinner 
198fa96acadSDave Chinner 	if (lock_flags & XFS_ILOCK_EXCL)
199fa96acadSDave Chinner 		mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
200fa96acadSDave Chinner 	else if (lock_flags & XFS_ILOCK_SHARED)
201fa96acadSDave Chinner 		mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
202fa96acadSDave Chinner }
203fa96acadSDave Chinner 
204fa96acadSDave Chinner /*
205fa96acadSDave Chinner  * This is just like xfs_ilock(), except that the caller
206fa96acadSDave Chinner  * is guaranteed not to sleep.  It returns 1 if it gets
207fa96acadSDave Chinner  * the requested locks and 0 otherwise.  If the IO lock is
208fa96acadSDave Chinner  * obtained but the inode lock cannot be, then the IO lock
209fa96acadSDave Chinner  * is dropped before returning.
210fa96acadSDave Chinner  *
211fa96acadSDave Chinner  * ip -- the inode being locked
212fa96acadSDave Chinner  * lock_flags -- this parameter indicates the inode's locks to be
213fa96acadSDave Chinner  *       to be locked.  See the comment for xfs_ilock() for a list
214fa96acadSDave Chinner  *	 of valid values.
215fa96acadSDave Chinner  */
216fa96acadSDave Chinner int
217fa96acadSDave Chinner xfs_ilock_nowait(
218fa96acadSDave Chinner 	xfs_inode_t		*ip,
219fa96acadSDave Chinner 	uint			lock_flags)
220fa96acadSDave Chinner {
221fa96acadSDave Chinner 	trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_);
222fa96acadSDave Chinner 
223fa96acadSDave Chinner 	/*
224fa96acadSDave Chinner 	 * You can't set both SHARED and EXCL for the same lock,
225fa96acadSDave Chinner 	 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
226fa96acadSDave Chinner 	 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
227fa96acadSDave Chinner 	 */
228fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
229fa96acadSDave Chinner 	       (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
230653c60b6SDave Chinner 	ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
231653c60b6SDave Chinner 	       (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
232fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
233fa96acadSDave Chinner 	       (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
2340952c818SDave Chinner 	ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
235fa96acadSDave Chinner 
236fa96acadSDave Chinner 	if (lock_flags & XFS_IOLOCK_EXCL) {
23765523218SChristoph Hellwig 		if (!down_write_trylock(&VFS_I(ip)->i_rwsem))
238fa96acadSDave Chinner 			goto out;
239fa96acadSDave Chinner 	} else if (lock_flags & XFS_IOLOCK_SHARED) {
24065523218SChristoph Hellwig 		if (!down_read_trylock(&VFS_I(ip)->i_rwsem))
241fa96acadSDave Chinner 			goto out;
242fa96acadSDave Chinner 	}
243653c60b6SDave Chinner 
244653c60b6SDave Chinner 	if (lock_flags & XFS_MMAPLOCK_EXCL) {
245653c60b6SDave Chinner 		if (!mrtryupdate(&ip->i_mmaplock))
246653c60b6SDave Chinner 			goto out_undo_iolock;
247653c60b6SDave Chinner 	} else if (lock_flags & XFS_MMAPLOCK_SHARED) {
248653c60b6SDave Chinner 		if (!mrtryaccess(&ip->i_mmaplock))
249653c60b6SDave Chinner 			goto out_undo_iolock;
250653c60b6SDave Chinner 	}
251653c60b6SDave Chinner 
252fa96acadSDave Chinner 	if (lock_flags & XFS_ILOCK_EXCL) {
253fa96acadSDave Chinner 		if (!mrtryupdate(&ip->i_lock))
254653c60b6SDave Chinner 			goto out_undo_mmaplock;
255fa96acadSDave Chinner 	} else if (lock_flags & XFS_ILOCK_SHARED) {
256fa96acadSDave Chinner 		if (!mrtryaccess(&ip->i_lock))
257653c60b6SDave Chinner 			goto out_undo_mmaplock;
258fa96acadSDave Chinner 	}
259fa96acadSDave Chinner 	return 1;
260fa96acadSDave Chinner 
261653c60b6SDave Chinner out_undo_mmaplock:
262653c60b6SDave Chinner 	if (lock_flags & XFS_MMAPLOCK_EXCL)
263653c60b6SDave Chinner 		mrunlock_excl(&ip->i_mmaplock);
264653c60b6SDave Chinner 	else if (lock_flags & XFS_MMAPLOCK_SHARED)
265653c60b6SDave Chinner 		mrunlock_shared(&ip->i_mmaplock);
266fa96acadSDave Chinner out_undo_iolock:
267fa96acadSDave Chinner 	if (lock_flags & XFS_IOLOCK_EXCL)
26865523218SChristoph Hellwig 		up_write(&VFS_I(ip)->i_rwsem);
269fa96acadSDave Chinner 	else if (lock_flags & XFS_IOLOCK_SHARED)
27065523218SChristoph Hellwig 		up_read(&VFS_I(ip)->i_rwsem);
271fa96acadSDave Chinner out:
272fa96acadSDave Chinner 	return 0;
273fa96acadSDave Chinner }
274fa96acadSDave Chinner 
275fa96acadSDave Chinner /*
276fa96acadSDave Chinner  * xfs_iunlock() is used to drop the inode locks acquired with
277fa96acadSDave Chinner  * xfs_ilock() and xfs_ilock_nowait().  The caller must pass
278fa96acadSDave Chinner  * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
279fa96acadSDave Chinner  * that we know which locks to drop.
280fa96acadSDave Chinner  *
281fa96acadSDave Chinner  * ip -- the inode being unlocked
282fa96acadSDave Chinner  * lock_flags -- this parameter indicates the inode's locks to be
283fa96acadSDave Chinner  *       to be unlocked.  See the comment for xfs_ilock() for a list
284fa96acadSDave Chinner  *	 of valid values for this parameter.
285fa96acadSDave Chinner  *
286fa96acadSDave Chinner  */
287fa96acadSDave Chinner void
288fa96acadSDave Chinner xfs_iunlock(
289fa96acadSDave Chinner 	xfs_inode_t		*ip,
290fa96acadSDave Chinner 	uint			lock_flags)
291fa96acadSDave Chinner {
292fa96acadSDave Chinner 	/*
293fa96acadSDave Chinner 	 * You can't set both SHARED and EXCL for the same lock,
294fa96acadSDave Chinner 	 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
295fa96acadSDave Chinner 	 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
296fa96acadSDave Chinner 	 */
297fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
298fa96acadSDave Chinner 	       (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
299653c60b6SDave Chinner 	ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
300653c60b6SDave Chinner 	       (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
301fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
302fa96acadSDave Chinner 	       (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
3030952c818SDave Chinner 	ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
304fa96acadSDave Chinner 	ASSERT(lock_flags != 0);
305fa96acadSDave Chinner 
306fa96acadSDave Chinner 	if (lock_flags & XFS_IOLOCK_EXCL)
30765523218SChristoph Hellwig 		up_write(&VFS_I(ip)->i_rwsem);
308fa96acadSDave Chinner 	else if (lock_flags & XFS_IOLOCK_SHARED)
30965523218SChristoph Hellwig 		up_read(&VFS_I(ip)->i_rwsem);
310fa96acadSDave Chinner 
311653c60b6SDave Chinner 	if (lock_flags & XFS_MMAPLOCK_EXCL)
312653c60b6SDave Chinner 		mrunlock_excl(&ip->i_mmaplock);
313653c60b6SDave Chinner 	else if (lock_flags & XFS_MMAPLOCK_SHARED)
314653c60b6SDave Chinner 		mrunlock_shared(&ip->i_mmaplock);
315653c60b6SDave Chinner 
316fa96acadSDave Chinner 	if (lock_flags & XFS_ILOCK_EXCL)
317fa96acadSDave Chinner 		mrunlock_excl(&ip->i_lock);
318fa96acadSDave Chinner 	else if (lock_flags & XFS_ILOCK_SHARED)
319fa96acadSDave Chinner 		mrunlock_shared(&ip->i_lock);
320fa96acadSDave Chinner 
321fa96acadSDave Chinner 	trace_xfs_iunlock(ip, lock_flags, _RET_IP_);
322fa96acadSDave Chinner }
323fa96acadSDave Chinner 
324fa96acadSDave Chinner /*
325fa96acadSDave Chinner  * give up write locks.  the i/o lock cannot be held nested
326fa96acadSDave Chinner  * if it is being demoted.
327fa96acadSDave Chinner  */
328fa96acadSDave Chinner void
329fa96acadSDave Chinner xfs_ilock_demote(
330fa96acadSDave Chinner 	xfs_inode_t		*ip,
331fa96acadSDave Chinner 	uint			lock_flags)
332fa96acadSDave Chinner {
333653c60b6SDave Chinner 	ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL));
334653c60b6SDave Chinner 	ASSERT((lock_flags &
335653c60b6SDave Chinner 		~(XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
336fa96acadSDave Chinner 
337fa96acadSDave Chinner 	if (lock_flags & XFS_ILOCK_EXCL)
338fa96acadSDave Chinner 		mrdemote(&ip->i_lock);
339653c60b6SDave Chinner 	if (lock_flags & XFS_MMAPLOCK_EXCL)
340653c60b6SDave Chinner 		mrdemote(&ip->i_mmaplock);
341fa96acadSDave Chinner 	if (lock_flags & XFS_IOLOCK_EXCL)
34265523218SChristoph Hellwig 		downgrade_write(&VFS_I(ip)->i_rwsem);
343fa96acadSDave Chinner 
344fa96acadSDave Chinner 	trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_);
345fa96acadSDave Chinner }
346fa96acadSDave Chinner 
347742ae1e3SDave Chinner #if defined(DEBUG) || defined(XFS_WARN)
348fa96acadSDave Chinner int
349fa96acadSDave Chinner xfs_isilocked(
350fa96acadSDave Chinner 	xfs_inode_t		*ip,
351fa96acadSDave Chinner 	uint			lock_flags)
352fa96acadSDave Chinner {
353fa96acadSDave Chinner 	if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) {
354fa96acadSDave Chinner 		if (!(lock_flags & XFS_ILOCK_SHARED))
355fa96acadSDave Chinner 			return !!ip->i_lock.mr_writer;
356fa96acadSDave Chinner 		return rwsem_is_locked(&ip->i_lock.mr_lock);
357fa96acadSDave Chinner 	}
358fa96acadSDave Chinner 
359653c60b6SDave Chinner 	if (lock_flags & (XFS_MMAPLOCK_EXCL|XFS_MMAPLOCK_SHARED)) {
360653c60b6SDave Chinner 		if (!(lock_flags & XFS_MMAPLOCK_SHARED))
361653c60b6SDave Chinner 			return !!ip->i_mmaplock.mr_writer;
362653c60b6SDave Chinner 		return rwsem_is_locked(&ip->i_mmaplock.mr_lock);
363653c60b6SDave Chinner 	}
364653c60b6SDave Chinner 
365fa96acadSDave Chinner 	if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) {
366fa96acadSDave Chinner 		if (!(lock_flags & XFS_IOLOCK_SHARED))
36765523218SChristoph Hellwig 			return !debug_locks ||
36865523218SChristoph Hellwig 				lockdep_is_held_type(&VFS_I(ip)->i_rwsem, 0);
36965523218SChristoph Hellwig 		return rwsem_is_locked(&VFS_I(ip)->i_rwsem);
370fa96acadSDave Chinner 	}
371fa96acadSDave Chinner 
372fa96acadSDave Chinner 	ASSERT(0);
373fa96acadSDave Chinner 	return 0;
374fa96acadSDave Chinner }
375fa96acadSDave Chinner #endif
376fa96acadSDave Chinner 
377b6a9947eSDave Chinner /*
378b6a9947eSDave Chinner  * xfs_lockdep_subclass_ok() is only used in an ASSERT, so is only called when
379b6a9947eSDave Chinner  * DEBUG or XFS_WARN is set. And MAX_LOCKDEP_SUBCLASSES is then only defined
380b6a9947eSDave Chinner  * when CONFIG_LOCKDEP is set. Hence the complex define below to avoid build
381b6a9947eSDave Chinner  * errors and warnings.
382b6a9947eSDave Chinner  */
383b6a9947eSDave Chinner #if (defined(DEBUG) || defined(XFS_WARN)) && defined(CONFIG_LOCKDEP)
3843403ccc0SDave Chinner static bool
3853403ccc0SDave Chinner xfs_lockdep_subclass_ok(
3863403ccc0SDave Chinner 	int subclass)
3873403ccc0SDave Chinner {
3883403ccc0SDave Chinner 	return subclass < MAX_LOCKDEP_SUBCLASSES;
3893403ccc0SDave Chinner }
3903403ccc0SDave Chinner #else
3913403ccc0SDave Chinner #define xfs_lockdep_subclass_ok(subclass)	(true)
3923403ccc0SDave Chinner #endif
3933403ccc0SDave Chinner 
394c24b5dfaSDave Chinner /*
395653c60b6SDave Chinner  * Bump the subclass so xfs_lock_inodes() acquires each lock with a different
3960952c818SDave Chinner  * value. This can be called for any type of inode lock combination, including
3970952c818SDave Chinner  * parent locking. Care must be taken to ensure we don't overrun the subclass
3980952c818SDave Chinner  * storage fields in the class mask we build.
399c24b5dfaSDave Chinner  */
400c24b5dfaSDave Chinner static inline int
401c24b5dfaSDave Chinner xfs_lock_inumorder(int lock_mode, int subclass)
402c24b5dfaSDave Chinner {
4030952c818SDave Chinner 	int	class = 0;
4040952c818SDave Chinner 
4050952c818SDave Chinner 	ASSERT(!(lock_mode & (XFS_ILOCK_PARENT | XFS_ILOCK_RTBITMAP |
4060952c818SDave Chinner 			      XFS_ILOCK_RTSUM)));
4073403ccc0SDave Chinner 	ASSERT(xfs_lockdep_subclass_ok(subclass));
4080952c818SDave Chinner 
409653c60b6SDave Chinner 	if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) {
4100952c818SDave Chinner 		ASSERT(subclass <= XFS_IOLOCK_MAX_SUBCLASS);
4110952c818SDave Chinner 		class += subclass << XFS_IOLOCK_SHIFT;
412653c60b6SDave Chinner 	}
413653c60b6SDave Chinner 
414653c60b6SDave Chinner 	if (lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) {
4150952c818SDave Chinner 		ASSERT(subclass <= XFS_MMAPLOCK_MAX_SUBCLASS);
4160952c818SDave Chinner 		class += subclass << XFS_MMAPLOCK_SHIFT;
417653c60b6SDave Chinner 	}
418653c60b6SDave Chinner 
4190952c818SDave Chinner 	if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) {
4200952c818SDave Chinner 		ASSERT(subclass <= XFS_ILOCK_MAX_SUBCLASS);
4210952c818SDave Chinner 		class += subclass << XFS_ILOCK_SHIFT;
4220952c818SDave Chinner 	}
423c24b5dfaSDave Chinner 
4240952c818SDave Chinner 	return (lock_mode & ~XFS_LOCK_SUBCLASS_MASK) | class;
425c24b5dfaSDave Chinner }
426c24b5dfaSDave Chinner 
427c24b5dfaSDave Chinner /*
42895afcf5cSDave Chinner  * The following routine will lock n inodes in exclusive mode.  We assume the
42995afcf5cSDave Chinner  * caller calls us with the inodes in i_ino order.
430c24b5dfaSDave Chinner  *
43195afcf5cSDave Chinner  * We need to detect deadlock where an inode that we lock is in the AIL and we
43295afcf5cSDave Chinner  * start waiting for another inode that is locked by a thread in a long running
43395afcf5cSDave Chinner  * transaction (such as truncate). This can result in deadlock since the long
43495afcf5cSDave Chinner  * running trans might need to wait for the inode we just locked in order to
43595afcf5cSDave Chinner  * push the tail and free space in the log.
4360952c818SDave Chinner  *
4370952c818SDave Chinner  * xfs_lock_inodes() can only be used to lock one type of lock at a time -
4380952c818SDave Chinner  * the iolock, the mmaplock or the ilock, but not more than one at a time. If we
4390952c818SDave Chinner  * lock more than one at a time, lockdep will report false positives saying we
4400952c818SDave Chinner  * have violated locking orders.
441c24b5dfaSDave Chinner  */
4420d5a75e9SEric Sandeen static void
443c24b5dfaSDave Chinner xfs_lock_inodes(
444c24b5dfaSDave Chinner 	xfs_inode_t	**ips,
445c24b5dfaSDave Chinner 	int		inodes,
446c24b5dfaSDave Chinner 	uint		lock_mode)
447c24b5dfaSDave Chinner {
448c24b5dfaSDave Chinner 	int		attempts = 0, i, j, try_lock;
449c24b5dfaSDave Chinner 	xfs_log_item_t	*lp;
450c24b5dfaSDave Chinner 
4510952c818SDave Chinner 	/*
4520952c818SDave Chinner 	 * Currently supports between 2 and 5 inodes with exclusive locking.  We
4530952c818SDave Chinner 	 * support an arbitrary depth of locking here, but absolute limits on
4540952c818SDave Chinner 	 * inodes depend on the the type of locking and the limits placed by
4550952c818SDave Chinner 	 * lockdep annotations in xfs_lock_inumorder.  These are all checked by
4560952c818SDave Chinner 	 * the asserts.
4570952c818SDave Chinner 	 */
45895afcf5cSDave Chinner 	ASSERT(ips && inodes >= 2 && inodes <= 5);
4590952c818SDave Chinner 	ASSERT(lock_mode & (XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL |
4600952c818SDave Chinner 			    XFS_ILOCK_EXCL));
4610952c818SDave Chinner 	ASSERT(!(lock_mode & (XFS_IOLOCK_SHARED | XFS_MMAPLOCK_SHARED |
4620952c818SDave Chinner 			      XFS_ILOCK_SHARED)));
4630952c818SDave Chinner 	ASSERT(!(lock_mode & XFS_MMAPLOCK_EXCL) ||
4640952c818SDave Chinner 		inodes <= XFS_MMAPLOCK_MAX_SUBCLASS + 1);
4650952c818SDave Chinner 	ASSERT(!(lock_mode & XFS_ILOCK_EXCL) ||
4660952c818SDave Chinner 		inodes <= XFS_ILOCK_MAX_SUBCLASS + 1);
4670952c818SDave Chinner 
4680952c818SDave Chinner 	if (lock_mode & XFS_IOLOCK_EXCL) {
4690952c818SDave Chinner 		ASSERT(!(lock_mode & (XFS_MMAPLOCK_EXCL | XFS_ILOCK_EXCL)));
4700952c818SDave Chinner 	} else if (lock_mode & XFS_MMAPLOCK_EXCL)
4710952c818SDave Chinner 		ASSERT(!(lock_mode & XFS_ILOCK_EXCL));
472c24b5dfaSDave Chinner 
473c24b5dfaSDave Chinner 	try_lock = 0;
474c24b5dfaSDave Chinner 	i = 0;
475c24b5dfaSDave Chinner again:
476c24b5dfaSDave Chinner 	for (; i < inodes; i++) {
477c24b5dfaSDave Chinner 		ASSERT(ips[i]);
478c24b5dfaSDave Chinner 
479c24b5dfaSDave Chinner 		if (i && (ips[i] == ips[i - 1]))	/* Already locked */
480c24b5dfaSDave Chinner 			continue;
481c24b5dfaSDave Chinner 
482c24b5dfaSDave Chinner 		/*
48395afcf5cSDave Chinner 		 * If try_lock is not set yet, make sure all locked inodes are
48495afcf5cSDave Chinner 		 * not in the AIL.  If any are, set try_lock to be used later.
485c24b5dfaSDave Chinner 		 */
486c24b5dfaSDave Chinner 		if (!try_lock) {
487c24b5dfaSDave Chinner 			for (j = (i - 1); j >= 0 && !try_lock; j--) {
488c24b5dfaSDave Chinner 				lp = (xfs_log_item_t *)ips[j]->i_itemp;
48922525c17SDave Chinner 				if (lp && test_bit(XFS_LI_IN_AIL, &lp->li_flags))
490c24b5dfaSDave Chinner 					try_lock++;
491c24b5dfaSDave Chinner 			}
492c24b5dfaSDave Chinner 		}
493c24b5dfaSDave Chinner 
494c24b5dfaSDave Chinner 		/*
495c24b5dfaSDave Chinner 		 * If any of the previous locks we have locked is in the AIL,
496c24b5dfaSDave Chinner 		 * we must TRY to get the second and subsequent locks. If
497c24b5dfaSDave Chinner 		 * we can't get any, we must release all we have
498c24b5dfaSDave Chinner 		 * and try again.
499c24b5dfaSDave Chinner 		 */
50095afcf5cSDave Chinner 		if (!try_lock) {
50195afcf5cSDave Chinner 			xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
50295afcf5cSDave Chinner 			continue;
50395afcf5cSDave Chinner 		}
504c24b5dfaSDave Chinner 
50595afcf5cSDave Chinner 		/* try_lock means we have an inode locked that is in the AIL. */
506c24b5dfaSDave Chinner 		ASSERT(i != 0);
50795afcf5cSDave Chinner 		if (xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i)))
50895afcf5cSDave Chinner 			continue;
50995afcf5cSDave Chinner 
51095afcf5cSDave Chinner 		/*
51195afcf5cSDave Chinner 		 * Unlock all previous guys and try again.  xfs_iunlock will try
51295afcf5cSDave Chinner 		 * to push the tail if the inode is in the AIL.
51395afcf5cSDave Chinner 		 */
514c24b5dfaSDave Chinner 		attempts++;
515c24b5dfaSDave Chinner 		for (j = i - 1; j >= 0; j--) {
516c24b5dfaSDave Chinner 			/*
51795afcf5cSDave Chinner 			 * Check to see if we've already unlocked this one.  Not
51895afcf5cSDave Chinner 			 * the first one going back, and the inode ptr is the
51995afcf5cSDave Chinner 			 * same.
520c24b5dfaSDave Chinner 			 */
52195afcf5cSDave Chinner 			if (j != (i - 1) && ips[j] == ips[j + 1])
522c24b5dfaSDave Chinner 				continue;
523c24b5dfaSDave Chinner 
524c24b5dfaSDave Chinner 			xfs_iunlock(ips[j], lock_mode);
525c24b5dfaSDave Chinner 		}
526c24b5dfaSDave Chinner 
527c24b5dfaSDave Chinner 		if ((attempts % 5) == 0) {
528c24b5dfaSDave Chinner 			delay(1); /* Don't just spin the CPU */
529c24b5dfaSDave Chinner 		}
530c24b5dfaSDave Chinner 		i = 0;
531c24b5dfaSDave Chinner 		try_lock = 0;
532c24b5dfaSDave Chinner 		goto again;
533c24b5dfaSDave Chinner 	}
534c24b5dfaSDave Chinner }
535c24b5dfaSDave Chinner 
536c24b5dfaSDave Chinner /*
537653c60b6SDave Chinner  * xfs_lock_two_inodes() can only be used to lock one type of lock at a time -
5387c2d238aSDarrick J. Wong  * the mmaplock or the ilock, but not more than one type at a time. If we lock
5397c2d238aSDarrick J. Wong  * more than one at a time, lockdep will report false positives saying we have
5407c2d238aSDarrick J. Wong  * violated locking orders.  The iolock must be double-locked separately since
5417c2d238aSDarrick J. Wong  * we use i_rwsem for that.  We now support taking one lock EXCL and the other
5427c2d238aSDarrick J. Wong  * SHARED.
543c24b5dfaSDave Chinner  */
544c24b5dfaSDave Chinner void
545c24b5dfaSDave Chinner xfs_lock_two_inodes(
5467c2d238aSDarrick J. Wong 	struct xfs_inode	*ip0,
5477c2d238aSDarrick J. Wong 	uint			ip0_mode,
5487c2d238aSDarrick J. Wong 	struct xfs_inode	*ip1,
5497c2d238aSDarrick J. Wong 	uint			ip1_mode)
550c24b5dfaSDave Chinner {
5517c2d238aSDarrick J. Wong 	struct xfs_inode	*temp;
5527c2d238aSDarrick J. Wong 	uint			mode_temp;
553c24b5dfaSDave Chinner 	int			attempts = 0;
554c24b5dfaSDave Chinner 	xfs_log_item_t		*lp;
555c24b5dfaSDave Chinner 
5567c2d238aSDarrick J. Wong 	ASSERT(hweight32(ip0_mode) == 1);
5577c2d238aSDarrick J. Wong 	ASSERT(hweight32(ip1_mode) == 1);
5587c2d238aSDarrick J. Wong 	ASSERT(!(ip0_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)));
5597c2d238aSDarrick J. Wong 	ASSERT(!(ip1_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)));
5607c2d238aSDarrick J. Wong 	ASSERT(!(ip0_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) ||
5617c2d238aSDarrick J. Wong 	       !(ip0_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
5627c2d238aSDarrick J. Wong 	ASSERT(!(ip1_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) ||
5637c2d238aSDarrick J. Wong 	       !(ip1_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
5647c2d238aSDarrick J. Wong 	ASSERT(!(ip1_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) ||
5657c2d238aSDarrick J. Wong 	       !(ip0_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
5667c2d238aSDarrick J. Wong 	ASSERT(!(ip0_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) ||
5677c2d238aSDarrick J. Wong 	       !(ip1_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
568653c60b6SDave Chinner 
569c24b5dfaSDave Chinner 	ASSERT(ip0->i_ino != ip1->i_ino);
570c24b5dfaSDave Chinner 
571c24b5dfaSDave Chinner 	if (ip0->i_ino > ip1->i_ino) {
572c24b5dfaSDave Chinner 		temp = ip0;
573c24b5dfaSDave Chinner 		ip0 = ip1;
574c24b5dfaSDave Chinner 		ip1 = temp;
5757c2d238aSDarrick J. Wong 		mode_temp = ip0_mode;
5767c2d238aSDarrick J. Wong 		ip0_mode = ip1_mode;
5777c2d238aSDarrick J. Wong 		ip1_mode = mode_temp;
578c24b5dfaSDave Chinner 	}
579c24b5dfaSDave Chinner 
580c24b5dfaSDave Chinner  again:
5817c2d238aSDarrick J. Wong 	xfs_ilock(ip0, xfs_lock_inumorder(ip0_mode, 0));
582c24b5dfaSDave Chinner 
583c24b5dfaSDave Chinner 	/*
584c24b5dfaSDave Chinner 	 * If the first lock we have locked is in the AIL, we must TRY to get
585c24b5dfaSDave Chinner 	 * the second lock. If we can't get it, we must release the first one
586c24b5dfaSDave Chinner 	 * and try again.
587c24b5dfaSDave Chinner 	 */
588c24b5dfaSDave Chinner 	lp = (xfs_log_item_t *)ip0->i_itemp;
58922525c17SDave Chinner 	if (lp && test_bit(XFS_LI_IN_AIL, &lp->li_flags)) {
5907c2d238aSDarrick J. Wong 		if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(ip1_mode, 1))) {
5917c2d238aSDarrick J. Wong 			xfs_iunlock(ip0, ip0_mode);
592c24b5dfaSDave Chinner 			if ((++attempts % 5) == 0)
593c24b5dfaSDave Chinner 				delay(1); /* Don't just spin the CPU */
594c24b5dfaSDave Chinner 			goto again;
595c24b5dfaSDave Chinner 		}
596c24b5dfaSDave Chinner 	} else {
5977c2d238aSDarrick J. Wong 		xfs_ilock(ip1, xfs_lock_inumorder(ip1_mode, 1));
598c24b5dfaSDave Chinner 	}
599c24b5dfaSDave Chinner }
600c24b5dfaSDave Chinner 
601fa96acadSDave Chinner void
602fa96acadSDave Chinner __xfs_iflock(
603fa96acadSDave Chinner 	struct xfs_inode	*ip)
604fa96acadSDave Chinner {
605fa96acadSDave Chinner 	wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IFLOCK_BIT);
606fa96acadSDave Chinner 	DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IFLOCK_BIT);
607fa96acadSDave Chinner 
608fa96acadSDave Chinner 	do {
60921417136SIngo Molnar 		prepare_to_wait_exclusive(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
610fa96acadSDave Chinner 		if (xfs_isiflocked(ip))
611fa96acadSDave Chinner 			io_schedule();
612fa96acadSDave Chinner 	} while (!xfs_iflock_nowait(ip));
613fa96acadSDave Chinner 
61421417136SIngo Molnar 	finish_wait(wq, &wait.wq_entry);
615fa96acadSDave Chinner }
616fa96acadSDave Chinner 
6171da177e4SLinus Torvalds STATIC uint
6181da177e4SLinus Torvalds _xfs_dic2xflags(
619c8ce540dSDarrick J. Wong 	uint16_t		di_flags,
62058f88ca2SDave Chinner 	uint64_t		di_flags2,
62158f88ca2SDave Chinner 	bool			has_attr)
6221da177e4SLinus Torvalds {
6231da177e4SLinus Torvalds 	uint			flags = 0;
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 	if (di_flags & XFS_DIFLAG_ANY) {
6261da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_REALTIME)
627e7b89481SDave Chinner 			flags |= FS_XFLAG_REALTIME;
6281da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_PREALLOC)
629e7b89481SDave Chinner 			flags |= FS_XFLAG_PREALLOC;
6301da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_IMMUTABLE)
631e7b89481SDave Chinner 			flags |= FS_XFLAG_IMMUTABLE;
6321da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_APPEND)
633e7b89481SDave Chinner 			flags |= FS_XFLAG_APPEND;
6341da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_SYNC)
635e7b89481SDave Chinner 			flags |= FS_XFLAG_SYNC;
6361da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_NOATIME)
637e7b89481SDave Chinner 			flags |= FS_XFLAG_NOATIME;
6381da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_NODUMP)
639e7b89481SDave Chinner 			flags |= FS_XFLAG_NODUMP;
6401da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_RTINHERIT)
641e7b89481SDave Chinner 			flags |= FS_XFLAG_RTINHERIT;
6421da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_PROJINHERIT)
643e7b89481SDave Chinner 			flags |= FS_XFLAG_PROJINHERIT;
6441da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_NOSYMLINKS)
645e7b89481SDave Chinner 			flags |= FS_XFLAG_NOSYMLINKS;
646dd9f438eSNathan Scott 		if (di_flags & XFS_DIFLAG_EXTSIZE)
647e7b89481SDave Chinner 			flags |= FS_XFLAG_EXTSIZE;
648dd9f438eSNathan Scott 		if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
649e7b89481SDave Chinner 			flags |= FS_XFLAG_EXTSZINHERIT;
650d3446eacSBarry Naujok 		if (di_flags & XFS_DIFLAG_NODEFRAG)
651e7b89481SDave Chinner 			flags |= FS_XFLAG_NODEFRAG;
6522a82b8beSDavid Chinner 		if (di_flags & XFS_DIFLAG_FILESTREAM)
653e7b89481SDave Chinner 			flags |= FS_XFLAG_FILESTREAM;
6541da177e4SLinus Torvalds 	}
6551da177e4SLinus Torvalds 
65658f88ca2SDave Chinner 	if (di_flags2 & XFS_DIFLAG2_ANY) {
65758f88ca2SDave Chinner 		if (di_flags2 & XFS_DIFLAG2_DAX)
65858f88ca2SDave Chinner 			flags |= FS_XFLAG_DAX;
659f7ca3522SDarrick J. Wong 		if (di_flags2 & XFS_DIFLAG2_COWEXTSIZE)
660f7ca3522SDarrick J. Wong 			flags |= FS_XFLAG_COWEXTSIZE;
66158f88ca2SDave Chinner 	}
66258f88ca2SDave Chinner 
66358f88ca2SDave Chinner 	if (has_attr)
66458f88ca2SDave Chinner 		flags |= FS_XFLAG_HASATTR;
66558f88ca2SDave Chinner 
6661da177e4SLinus Torvalds 	return flags;
6671da177e4SLinus Torvalds }
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds uint
6701da177e4SLinus Torvalds xfs_ip2xflags(
67158f88ca2SDave Chinner 	struct xfs_inode	*ip)
6721da177e4SLinus Torvalds {
67358f88ca2SDave Chinner 	struct xfs_icdinode	*dic = &ip->i_d;
6741da177e4SLinus Torvalds 
67558f88ca2SDave Chinner 	return _xfs_dic2xflags(dic->di_flags, dic->di_flags2, XFS_IFORK_Q(ip));
6761da177e4SLinus Torvalds }
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds /*
679c24b5dfaSDave Chinner  * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
680c24b5dfaSDave Chinner  * is allowed, otherwise it has to be an exact match. If a CI match is found,
681c24b5dfaSDave Chinner  * ci_name->name will point to a the actual name (caller must free) or
682c24b5dfaSDave Chinner  * will be set to NULL if an exact match is found.
683c24b5dfaSDave Chinner  */
684c24b5dfaSDave Chinner int
685c24b5dfaSDave Chinner xfs_lookup(
686c24b5dfaSDave Chinner 	xfs_inode_t		*dp,
687c24b5dfaSDave Chinner 	struct xfs_name		*name,
688c24b5dfaSDave Chinner 	xfs_inode_t		**ipp,
689c24b5dfaSDave Chinner 	struct xfs_name		*ci_name)
690c24b5dfaSDave Chinner {
691c24b5dfaSDave Chinner 	xfs_ino_t		inum;
692c24b5dfaSDave Chinner 	int			error;
693c24b5dfaSDave Chinner 
694c24b5dfaSDave Chinner 	trace_xfs_lookup(dp, name);
695c24b5dfaSDave Chinner 
696c24b5dfaSDave Chinner 	if (XFS_FORCED_SHUTDOWN(dp->i_mount))
6972451337dSDave Chinner 		return -EIO;
698c24b5dfaSDave Chinner 
699c24b5dfaSDave Chinner 	error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
700c24b5dfaSDave Chinner 	if (error)
701dbad7c99SDave Chinner 		goto out_unlock;
702c24b5dfaSDave Chinner 
703c24b5dfaSDave Chinner 	error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
704c24b5dfaSDave Chinner 	if (error)
705c24b5dfaSDave Chinner 		goto out_free_name;
706c24b5dfaSDave Chinner 
707c24b5dfaSDave Chinner 	return 0;
708c24b5dfaSDave Chinner 
709c24b5dfaSDave Chinner out_free_name:
710c24b5dfaSDave Chinner 	if (ci_name)
711c24b5dfaSDave Chinner 		kmem_free(ci_name->name);
712dbad7c99SDave Chinner out_unlock:
713c24b5dfaSDave Chinner 	*ipp = NULL;
714c24b5dfaSDave Chinner 	return error;
715c24b5dfaSDave Chinner }
716c24b5dfaSDave Chinner 
717c24b5dfaSDave Chinner /*
7181da177e4SLinus Torvalds  * Allocate an inode on disk and return a copy of its in-core version.
7191da177e4SLinus Torvalds  * The in-core inode is locked exclusively.  Set mode, nlink, and rdev
7201da177e4SLinus Torvalds  * appropriately within the inode.  The uid and gid for the inode are
7211da177e4SLinus Torvalds  * set according to the contents of the given cred structure.
7221da177e4SLinus Torvalds  *
7231da177e4SLinus Torvalds  * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
724cd856db6SCarlos Maiolino  * has a free inode available, call xfs_iget() to obtain the in-core
725cd856db6SCarlos Maiolino  * version of the allocated inode.  Finally, fill in the inode and
726cd856db6SCarlos Maiolino  * log its initial contents.  In this case, ialloc_context would be
727cd856db6SCarlos Maiolino  * set to NULL.
7281da177e4SLinus Torvalds  *
729cd856db6SCarlos Maiolino  * If xfs_dialloc() does not have an available inode, it will replenish
730cd856db6SCarlos Maiolino  * its supply by doing an allocation. Since we can only do one
731cd856db6SCarlos Maiolino  * allocation within a transaction without deadlocks, we must commit
732cd856db6SCarlos Maiolino  * the current transaction before returning the inode itself.
733cd856db6SCarlos Maiolino  * In this case, therefore, we will set ialloc_context and return.
7341da177e4SLinus Torvalds  * The caller should then commit the current transaction, start a new
7351da177e4SLinus Torvalds  * transaction, and call xfs_ialloc() again to actually get the inode.
7361da177e4SLinus Torvalds  *
7371da177e4SLinus Torvalds  * To ensure that some other process does not grab the inode that
7381da177e4SLinus Torvalds  * was allocated during the first call to xfs_ialloc(), this routine
7391da177e4SLinus Torvalds  * also returns the [locked] bp pointing to the head of the freelist
7401da177e4SLinus Torvalds  * as ialloc_context.  The caller should hold this buffer across
7411da177e4SLinus Torvalds  * the commit and pass it back into this routine on the second call.
742b11f94d5SDavid Chinner  *
743b11f94d5SDavid Chinner  * If we are allocating quota inodes, we do not have a parent inode
744b11f94d5SDavid Chinner  * to attach to or associate with (i.e. pip == NULL) because they
745b11f94d5SDavid Chinner  * are not linked into the directory structure - they are attached
746b11f94d5SDavid Chinner  * directly to the superblock - and so have no parent.
7471da177e4SLinus Torvalds  */
7480d5a75e9SEric Sandeen static int
7491da177e4SLinus Torvalds xfs_ialloc(
7501da177e4SLinus Torvalds 	xfs_trans_t	*tp,
7511da177e4SLinus Torvalds 	xfs_inode_t	*pip,
752576b1d67SAl Viro 	umode_t		mode,
75331b084aeSNathan Scott 	xfs_nlink_t	nlink,
75466f36464SChristoph Hellwig 	dev_t		rdev,
7556743099cSArkadiusz Mi?kiewicz 	prid_t		prid,
7561da177e4SLinus Torvalds 	xfs_buf_t	**ialloc_context,
7571da177e4SLinus Torvalds 	xfs_inode_t	**ipp)
7581da177e4SLinus Torvalds {
75993848a99SChristoph Hellwig 	struct xfs_mount *mp = tp->t_mountp;
7601da177e4SLinus Torvalds 	xfs_ino_t	ino;
7611da177e4SLinus Torvalds 	xfs_inode_t	*ip;
7621da177e4SLinus Torvalds 	uint		flags;
7631da177e4SLinus Torvalds 	int		error;
76495582b00SDeepa Dinamani 	struct timespec64 tv;
7653987848cSDave Chinner 	struct inode	*inode;
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds 	/*
7681da177e4SLinus Torvalds 	 * Call the space management code to pick
7691da177e4SLinus Torvalds 	 * the on-disk inode to be allocated.
7701da177e4SLinus Torvalds 	 */
771f59cf5c2SChristoph Hellwig 	error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode,
77208358906SChristoph Hellwig 			    ialloc_context, &ino);
773bf904248SDavid Chinner 	if (error)
7741da177e4SLinus Torvalds 		return error;
77508358906SChristoph Hellwig 	if (*ialloc_context || ino == NULLFSINO) {
7761da177e4SLinus Torvalds 		*ipp = NULL;
7771da177e4SLinus Torvalds 		return 0;
7781da177e4SLinus Torvalds 	}
7791da177e4SLinus Torvalds 	ASSERT(*ialloc_context == NULL);
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 	/*
7828b26984dSDave Chinner 	 * Protect against obviously corrupt allocation btree records. Later
7838b26984dSDave Chinner 	 * xfs_iget checks will catch re-allocation of other active in-memory
7848b26984dSDave Chinner 	 * and on-disk inodes. If we don't catch reallocating the parent inode
7858b26984dSDave Chinner 	 * here we will deadlock in xfs_iget() so we have to do these checks
7868b26984dSDave Chinner 	 * first.
7878b26984dSDave Chinner 	 */
7888b26984dSDave Chinner 	if ((pip && ino == pip->i_ino) || !xfs_verify_dir_ino(mp, ino)) {
7898b26984dSDave Chinner 		xfs_alert(mp, "Allocated a known in-use inode 0x%llx!", ino);
7908b26984dSDave Chinner 		return -EFSCORRUPTED;
7918b26984dSDave Chinner 	}
7928b26984dSDave Chinner 
7938b26984dSDave Chinner 	/*
7941da177e4SLinus Torvalds 	 * Get the in-core inode with the lock held exclusively.
7951da177e4SLinus Torvalds 	 * This is because we're setting fields here we need
7961da177e4SLinus Torvalds 	 * to prevent others from looking at until we're done.
7971da177e4SLinus Torvalds 	 */
79893848a99SChristoph Hellwig 	error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE,
799ec3ba85fSChristoph Hellwig 			 XFS_ILOCK_EXCL, &ip);
800bf904248SDavid Chinner 	if (error)
8011da177e4SLinus Torvalds 		return error;
8021da177e4SLinus Torvalds 	ASSERT(ip != NULL);
8033987848cSDave Chinner 	inode = VFS_I(ip);
8041da177e4SLinus Torvalds 
805263997a6SDave Chinner 	/*
806263997a6SDave Chinner 	 * We always convert v1 inodes to v2 now - we only support filesystems
807263997a6SDave Chinner 	 * with >= v2 inode capability, so there is no reason for ever leaving
808263997a6SDave Chinner 	 * an inode in v1 format.
809263997a6SDave Chinner 	 */
810263997a6SDave Chinner 	if (ip->i_d.di_version == 1)
811263997a6SDave Chinner 		ip->i_d.di_version = 2;
812263997a6SDave Chinner 
813c19b3b05SDave Chinner 	inode->i_mode = mode;
81454d7b5c1SDave Chinner 	set_nlink(inode, nlink);
8157aab1b28SDwight Engen 	ip->i_d.di_uid = xfs_kuid_to_uid(current_fsuid());
8167aab1b28SDwight Engen 	ip->i_d.di_gid = xfs_kgid_to_gid(current_fsgid());
81766f36464SChristoph Hellwig 	inode->i_rdev = rdev;
8186743099cSArkadiusz Mi?kiewicz 	xfs_set_projid(ip, prid);
8191da177e4SLinus Torvalds 
820bd186aa9SChristoph Hellwig 	if (pip && XFS_INHERIT_GID(pip)) {
8211da177e4SLinus Torvalds 		ip->i_d.di_gid = pip->i_d.di_gid;
822c19b3b05SDave Chinner 		if ((VFS_I(pip)->i_mode & S_ISGID) && S_ISDIR(mode))
823c19b3b05SDave Chinner 			inode->i_mode |= S_ISGID;
8241da177e4SLinus Torvalds 	}
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds 	/*
8271da177e4SLinus Torvalds 	 * If the group ID of the new file does not match the effective group
8281da177e4SLinus Torvalds 	 * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
8291da177e4SLinus Torvalds 	 * (and only if the irix_sgid_inherit compatibility variable is set).
8301da177e4SLinus Torvalds 	 */
8311da177e4SLinus Torvalds 	if ((irix_sgid_inherit) &&
832c19b3b05SDave Chinner 	    (inode->i_mode & S_ISGID) &&
833c19b3b05SDave Chinner 	    (!in_group_p(xfs_gid_to_kgid(ip->i_d.di_gid))))
834c19b3b05SDave Chinner 		inode->i_mode &= ~S_ISGID;
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds 	ip->i_d.di_size = 0;
8371da177e4SLinus Torvalds 	ip->i_d.di_nextents = 0;
8381da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_nblocks == 0);
839dff35fd4SChristoph Hellwig 
840c2050a45SDeepa Dinamani 	tv = current_time(inode);
8413987848cSDave Chinner 	inode->i_mtime = tv;
8423987848cSDave Chinner 	inode->i_atime = tv;
8433987848cSDave Chinner 	inode->i_ctime = tv;
844dff35fd4SChristoph Hellwig 
8451da177e4SLinus Torvalds 	ip->i_d.di_extsize = 0;
8461da177e4SLinus Torvalds 	ip->i_d.di_dmevmask = 0;
8471da177e4SLinus Torvalds 	ip->i_d.di_dmstate = 0;
8481da177e4SLinus Torvalds 	ip->i_d.di_flags = 0;
84993848a99SChristoph Hellwig 
85093848a99SChristoph Hellwig 	if (ip->i_d.di_version == 3) {
851f0e28280SJeff Layton 		inode_set_iversion(inode, 1);
85293848a99SChristoph Hellwig 		ip->i_d.di_flags2 = 0;
853f7ca3522SDarrick J. Wong 		ip->i_d.di_cowextsize = 0;
854c8ce540dSDarrick J. Wong 		ip->i_d.di_crtime.t_sec = (int32_t)tv.tv_sec;
855c8ce540dSDarrick J. Wong 		ip->i_d.di_crtime.t_nsec = (int32_t)tv.tv_nsec;
85693848a99SChristoph Hellwig 	}
85793848a99SChristoph Hellwig 
85893848a99SChristoph Hellwig 
8591da177e4SLinus Torvalds 	flags = XFS_ILOG_CORE;
8601da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
8611da177e4SLinus Torvalds 	case S_IFIFO:
8621da177e4SLinus Torvalds 	case S_IFCHR:
8631da177e4SLinus Torvalds 	case S_IFBLK:
8641da177e4SLinus Torvalds 	case S_IFSOCK:
8651da177e4SLinus Torvalds 		ip->i_d.di_format = XFS_DINODE_FMT_DEV;
8661da177e4SLinus Torvalds 		ip->i_df.if_flags = 0;
8671da177e4SLinus Torvalds 		flags |= XFS_ILOG_DEV;
8681da177e4SLinus Torvalds 		break;
8691da177e4SLinus Torvalds 	case S_IFREG:
8701da177e4SLinus Torvalds 	case S_IFDIR:
871b11f94d5SDavid Chinner 		if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
872365ca83dSNathan Scott 			uint		di_flags = 0;
873365ca83dSNathan Scott 
874abbede1bSAl Viro 			if (S_ISDIR(mode)) {
875365ca83dSNathan Scott 				if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
876365ca83dSNathan Scott 					di_flags |= XFS_DIFLAG_RTINHERIT;
877dd9f438eSNathan Scott 				if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
878dd9f438eSNathan Scott 					di_flags |= XFS_DIFLAG_EXTSZINHERIT;
879dd9f438eSNathan Scott 					ip->i_d.di_extsize = pip->i_d.di_extsize;
880dd9f438eSNathan Scott 				}
8819336e3a7SDave Chinner 				if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
8829336e3a7SDave Chinner 					di_flags |= XFS_DIFLAG_PROJINHERIT;
883abbede1bSAl Viro 			} else if (S_ISREG(mode)) {
884613d7043SChristoph Hellwig 				if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
885365ca83dSNathan Scott 					di_flags |= XFS_DIFLAG_REALTIME;
886dd9f438eSNathan Scott 				if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
887dd9f438eSNathan Scott 					di_flags |= XFS_DIFLAG_EXTSIZE;
888dd9f438eSNathan Scott 					ip->i_d.di_extsize = pip->i_d.di_extsize;
889dd9f438eSNathan Scott 				}
8901da177e4SLinus Torvalds 			}
8911da177e4SLinus Torvalds 			if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
8921da177e4SLinus Torvalds 			    xfs_inherit_noatime)
893365ca83dSNathan Scott 				di_flags |= XFS_DIFLAG_NOATIME;
8941da177e4SLinus Torvalds 			if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
8951da177e4SLinus Torvalds 			    xfs_inherit_nodump)
896365ca83dSNathan Scott 				di_flags |= XFS_DIFLAG_NODUMP;
8971da177e4SLinus Torvalds 			if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
8981da177e4SLinus Torvalds 			    xfs_inherit_sync)
899365ca83dSNathan Scott 				di_flags |= XFS_DIFLAG_SYNC;
9001da177e4SLinus Torvalds 			if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
9011da177e4SLinus Torvalds 			    xfs_inherit_nosymlinks)
902365ca83dSNathan Scott 				di_flags |= XFS_DIFLAG_NOSYMLINKS;
903d3446eacSBarry Naujok 			if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
904d3446eacSBarry Naujok 			    xfs_inherit_nodefrag)
905d3446eacSBarry Naujok 				di_flags |= XFS_DIFLAG_NODEFRAG;
9062a82b8beSDavid Chinner 			if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
9072a82b8beSDavid Chinner 				di_flags |= XFS_DIFLAG_FILESTREAM;
90858f88ca2SDave Chinner 
909365ca83dSNathan Scott 			ip->i_d.di_flags |= di_flags;
9101da177e4SLinus Torvalds 		}
911f7ca3522SDarrick J. Wong 		if (pip &&
912f7ca3522SDarrick J. Wong 		    (pip->i_d.di_flags2 & XFS_DIFLAG2_ANY) &&
913f7ca3522SDarrick J. Wong 		    pip->i_d.di_version == 3 &&
914f7ca3522SDarrick J. Wong 		    ip->i_d.di_version == 3) {
91556bdf855SLukas Czerner 			uint64_t	di_flags2 = 0;
91656bdf855SLukas Czerner 
917f7ca3522SDarrick J. Wong 			if (pip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) {
91856bdf855SLukas Czerner 				di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
919f7ca3522SDarrick J. Wong 				ip->i_d.di_cowextsize = pip->i_d.di_cowextsize;
920f7ca3522SDarrick J. Wong 			}
92156bdf855SLukas Czerner 			if (pip->i_d.di_flags2 & XFS_DIFLAG2_DAX)
92256bdf855SLukas Czerner 				di_flags2 |= XFS_DIFLAG2_DAX;
92356bdf855SLukas Czerner 
92456bdf855SLukas Czerner 			ip->i_d.di_flags2 |= di_flags2;
925f7ca3522SDarrick J. Wong 		}
9261da177e4SLinus Torvalds 		/* FALLTHROUGH */
9271da177e4SLinus Torvalds 	case S_IFLNK:
9281da177e4SLinus Torvalds 		ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
9291da177e4SLinus Torvalds 		ip->i_df.if_flags = XFS_IFEXTENTS;
9301da177e4SLinus Torvalds 		ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
9316bdcf26aSChristoph Hellwig 		ip->i_df.if_u1.if_root = NULL;
9321da177e4SLinus Torvalds 		break;
9331da177e4SLinus Torvalds 	default:
9341da177e4SLinus Torvalds 		ASSERT(0);
9351da177e4SLinus Torvalds 	}
9361da177e4SLinus Torvalds 	/*
9371da177e4SLinus Torvalds 	 * Attribute fork settings for new inode.
9381da177e4SLinus Torvalds 	 */
9391da177e4SLinus Torvalds 	ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
9401da177e4SLinus Torvalds 	ip->i_d.di_anextents = 0;
9411da177e4SLinus Torvalds 
9421da177e4SLinus Torvalds 	/*
9431da177e4SLinus Torvalds 	 * Log the new values stuffed into the inode.
9441da177e4SLinus Torvalds 	 */
945ddc3415aSChristoph Hellwig 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
9461da177e4SLinus Torvalds 	xfs_trans_log_inode(tp, ip, flags);
9471da177e4SLinus Torvalds 
94858c90473SDave Chinner 	/* now that we have an i_mode we can setup the inode structure */
94941be8bedSChristoph Hellwig 	xfs_setup_inode(ip);
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds 	*ipp = ip;
9521da177e4SLinus Torvalds 	return 0;
9531da177e4SLinus Torvalds }
9541da177e4SLinus Torvalds 
955e546cb79SDave Chinner /*
956e546cb79SDave Chinner  * Allocates a new inode from disk and return a pointer to the
957e546cb79SDave Chinner  * incore copy. This routine will internally commit the current
958e546cb79SDave Chinner  * transaction and allocate a new one if the Space Manager needed
959e546cb79SDave Chinner  * to do an allocation to replenish the inode free-list.
960e546cb79SDave Chinner  *
961e546cb79SDave Chinner  * This routine is designed to be called from xfs_create and
962e546cb79SDave Chinner  * xfs_create_dir.
963e546cb79SDave Chinner  *
964e546cb79SDave Chinner  */
965e546cb79SDave Chinner int
966e546cb79SDave Chinner xfs_dir_ialloc(
967e546cb79SDave Chinner 	xfs_trans_t	**tpp,		/* input: current transaction;
968e546cb79SDave Chinner 					   output: may be a new transaction. */
969e546cb79SDave Chinner 	xfs_inode_t	*dp,		/* directory within whose allocate
970e546cb79SDave Chinner 					   the inode. */
971e546cb79SDave Chinner 	umode_t		mode,
972e546cb79SDave Chinner 	xfs_nlink_t	nlink,
97366f36464SChristoph Hellwig 	dev_t		rdev,
974e546cb79SDave Chinner 	prid_t		prid,		/* project id */
975c959025eSChandan Rajendra 	xfs_inode_t	**ipp)		/* pointer to inode; it will be
976e546cb79SDave Chinner 					   locked. */
977e546cb79SDave Chinner {
978e546cb79SDave Chinner 	xfs_trans_t	*tp;
979e546cb79SDave Chinner 	xfs_inode_t	*ip;
980e546cb79SDave Chinner 	xfs_buf_t	*ialloc_context = NULL;
981e546cb79SDave Chinner 	int		code;
982e546cb79SDave Chinner 	void		*dqinfo;
983e546cb79SDave Chinner 	uint		tflags;
984e546cb79SDave Chinner 
985e546cb79SDave Chinner 	tp = *tpp;
986e546cb79SDave Chinner 	ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
987e546cb79SDave Chinner 
988e546cb79SDave Chinner 	/*
989e546cb79SDave Chinner 	 * xfs_ialloc will return a pointer to an incore inode if
990e546cb79SDave Chinner 	 * the Space Manager has an available inode on the free
991e546cb79SDave Chinner 	 * list. Otherwise, it will do an allocation and replenish
992e546cb79SDave Chinner 	 * the freelist.  Since we can only do one allocation per
993e546cb79SDave Chinner 	 * transaction without deadlocks, we will need to commit the
994e546cb79SDave Chinner 	 * current transaction and start a new one.  We will then
995e546cb79SDave Chinner 	 * need to call xfs_ialloc again to get the inode.
996e546cb79SDave Chinner 	 *
997e546cb79SDave Chinner 	 * If xfs_ialloc did an allocation to replenish the freelist,
998e546cb79SDave Chinner 	 * it returns the bp containing the head of the freelist as
999e546cb79SDave Chinner 	 * ialloc_context. We will hold a lock on it across the
1000e546cb79SDave Chinner 	 * transaction commit so that no other process can steal
1001e546cb79SDave Chinner 	 * the inode(s) that we've just allocated.
1002e546cb79SDave Chinner 	 */
1003f59cf5c2SChristoph Hellwig 	code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid, &ialloc_context,
1004f59cf5c2SChristoph Hellwig 			&ip);
1005e546cb79SDave Chinner 
1006e546cb79SDave Chinner 	/*
1007e546cb79SDave Chinner 	 * Return an error if we were unable to allocate a new inode.
1008e546cb79SDave Chinner 	 * This should only happen if we run out of space on disk or
1009e546cb79SDave Chinner 	 * encounter a disk error.
1010e546cb79SDave Chinner 	 */
1011e546cb79SDave Chinner 	if (code) {
1012e546cb79SDave Chinner 		*ipp = NULL;
1013e546cb79SDave Chinner 		return code;
1014e546cb79SDave Chinner 	}
1015e546cb79SDave Chinner 	if (!ialloc_context && !ip) {
1016e546cb79SDave Chinner 		*ipp = NULL;
10172451337dSDave Chinner 		return -ENOSPC;
1018e546cb79SDave Chinner 	}
1019e546cb79SDave Chinner 
1020e546cb79SDave Chinner 	/*
1021e546cb79SDave Chinner 	 * If the AGI buffer is non-NULL, then we were unable to get an
1022e546cb79SDave Chinner 	 * inode in one operation.  We need to commit the current
1023e546cb79SDave Chinner 	 * transaction and call xfs_ialloc() again.  It is guaranteed
1024e546cb79SDave Chinner 	 * to succeed the second time.
1025e546cb79SDave Chinner 	 */
1026e546cb79SDave Chinner 	if (ialloc_context) {
1027e546cb79SDave Chinner 		/*
1028e546cb79SDave Chinner 		 * Normally, xfs_trans_commit releases all the locks.
1029e546cb79SDave Chinner 		 * We call bhold to hang on to the ialloc_context across
1030e546cb79SDave Chinner 		 * the commit.  Holding this buffer prevents any other
1031e546cb79SDave Chinner 		 * processes from doing any allocations in this
1032e546cb79SDave Chinner 		 * allocation group.
1033e546cb79SDave Chinner 		 */
1034e546cb79SDave Chinner 		xfs_trans_bhold(tp, ialloc_context);
1035e546cb79SDave Chinner 
1036e546cb79SDave Chinner 		/*
1037e546cb79SDave Chinner 		 * We want the quota changes to be associated with the next
1038e546cb79SDave Chinner 		 * transaction, NOT this one. So, detach the dqinfo from this
1039e546cb79SDave Chinner 		 * and attach it to the next transaction.
1040e546cb79SDave Chinner 		 */
1041e546cb79SDave Chinner 		dqinfo = NULL;
1042e546cb79SDave Chinner 		tflags = 0;
1043e546cb79SDave Chinner 		if (tp->t_dqinfo) {
1044e546cb79SDave Chinner 			dqinfo = (void *)tp->t_dqinfo;
1045e546cb79SDave Chinner 			tp->t_dqinfo = NULL;
1046e546cb79SDave Chinner 			tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY;
1047e546cb79SDave Chinner 			tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY);
1048e546cb79SDave Chinner 		}
1049e546cb79SDave Chinner 
1050411350dfSChristoph Hellwig 		code = xfs_trans_roll(&tp);
10513d3c8b52SJie Liu 
1052e546cb79SDave Chinner 		/*
1053e546cb79SDave Chinner 		 * Re-attach the quota info that we detached from prev trx.
1054e546cb79SDave Chinner 		 */
1055e546cb79SDave Chinner 		if (dqinfo) {
1056e546cb79SDave Chinner 			tp->t_dqinfo = dqinfo;
1057e546cb79SDave Chinner 			tp->t_flags |= tflags;
1058e546cb79SDave Chinner 		}
1059e546cb79SDave Chinner 
1060e546cb79SDave Chinner 		if (code) {
1061e546cb79SDave Chinner 			xfs_buf_relse(ialloc_context);
10622e6db6c4SChristoph Hellwig 			*tpp = tp;
1063e546cb79SDave Chinner 			*ipp = NULL;
1064e546cb79SDave Chinner 			return code;
1065e546cb79SDave Chinner 		}
1066e546cb79SDave Chinner 		xfs_trans_bjoin(tp, ialloc_context);
1067e546cb79SDave Chinner 
1068e546cb79SDave Chinner 		/*
1069e546cb79SDave Chinner 		 * Call ialloc again. Since we've locked out all
1070e546cb79SDave Chinner 		 * other allocations in this allocation group,
1071e546cb79SDave Chinner 		 * this call should always succeed.
1072e546cb79SDave Chinner 		 */
1073e546cb79SDave Chinner 		code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid,
1074f59cf5c2SChristoph Hellwig 				  &ialloc_context, &ip);
1075e546cb79SDave Chinner 
1076e546cb79SDave Chinner 		/*
1077e546cb79SDave Chinner 		 * If we get an error at this point, return to the caller
1078e546cb79SDave Chinner 		 * so that the current transaction can be aborted.
1079e546cb79SDave Chinner 		 */
1080e546cb79SDave Chinner 		if (code) {
1081e546cb79SDave Chinner 			*tpp = tp;
1082e546cb79SDave Chinner 			*ipp = NULL;
1083e546cb79SDave Chinner 			return code;
1084e546cb79SDave Chinner 		}
1085e546cb79SDave Chinner 		ASSERT(!ialloc_context && ip);
1086e546cb79SDave Chinner 
1087e546cb79SDave Chinner 	}
1088e546cb79SDave Chinner 
1089e546cb79SDave Chinner 	*ipp = ip;
1090e546cb79SDave Chinner 	*tpp = tp;
1091e546cb79SDave Chinner 
1092e546cb79SDave Chinner 	return 0;
1093e546cb79SDave Chinner }
1094e546cb79SDave Chinner 
1095e546cb79SDave Chinner /*
109654d7b5c1SDave Chinner  * Decrement the link count on an inode & log the change.  If this causes the
109754d7b5c1SDave Chinner  * link count to go to zero, move the inode to AGI unlinked list so that it can
109854d7b5c1SDave Chinner  * be freed when the last active reference goes away via xfs_inactive().
1099e546cb79SDave Chinner  */
11000d5a75e9SEric Sandeen static int			/* error */
1101e546cb79SDave Chinner xfs_droplink(
1102e546cb79SDave Chinner 	xfs_trans_t *tp,
1103e546cb79SDave Chinner 	xfs_inode_t *ip)
1104e546cb79SDave Chinner {
1105e546cb79SDave Chinner 	xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1106e546cb79SDave Chinner 
1107e546cb79SDave Chinner 	drop_nlink(VFS_I(ip));
1108e546cb79SDave Chinner 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1109e546cb79SDave Chinner 
111054d7b5c1SDave Chinner 	if (VFS_I(ip)->i_nlink)
111154d7b5c1SDave Chinner 		return 0;
111254d7b5c1SDave Chinner 
111354d7b5c1SDave Chinner 	return xfs_iunlink(tp, ip);
1114e546cb79SDave Chinner }
1115e546cb79SDave Chinner 
1116e546cb79SDave Chinner /*
1117e546cb79SDave Chinner  * Increment the link count on an inode & log the change.
1118e546cb79SDave Chinner  */
11190d5a75e9SEric Sandeen static int
1120e546cb79SDave Chinner xfs_bumplink(
1121e546cb79SDave Chinner 	xfs_trans_t *tp,
1122e546cb79SDave Chinner 	xfs_inode_t *ip)
1123e546cb79SDave Chinner {
1124e546cb79SDave Chinner 	xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1125e546cb79SDave Chinner 
1126263997a6SDave Chinner 	ASSERT(ip->i_d.di_version > 1);
1127e546cb79SDave Chinner 	inc_nlink(VFS_I(ip));
1128e546cb79SDave Chinner 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1129e546cb79SDave Chinner 	return 0;
1130e546cb79SDave Chinner }
1131e546cb79SDave Chinner 
1132c24b5dfaSDave Chinner int
1133c24b5dfaSDave Chinner xfs_create(
1134c24b5dfaSDave Chinner 	xfs_inode_t		*dp,
1135c24b5dfaSDave Chinner 	struct xfs_name		*name,
1136c24b5dfaSDave Chinner 	umode_t			mode,
113766f36464SChristoph Hellwig 	dev_t			rdev,
1138c24b5dfaSDave Chinner 	xfs_inode_t		**ipp)
1139c24b5dfaSDave Chinner {
1140c24b5dfaSDave Chinner 	int			is_dir = S_ISDIR(mode);
1141c24b5dfaSDave Chinner 	struct xfs_mount	*mp = dp->i_mount;
1142c24b5dfaSDave Chinner 	struct xfs_inode	*ip = NULL;
1143c24b5dfaSDave Chinner 	struct xfs_trans	*tp = NULL;
1144c24b5dfaSDave Chinner 	int			error;
1145c24b5dfaSDave Chinner 	bool                    unlock_dp_on_error = false;
1146c24b5dfaSDave Chinner 	prid_t			prid;
1147c24b5dfaSDave Chinner 	struct xfs_dquot	*udqp = NULL;
1148c24b5dfaSDave Chinner 	struct xfs_dquot	*gdqp = NULL;
1149c24b5dfaSDave Chinner 	struct xfs_dquot	*pdqp = NULL;
1150062647a8SBrian Foster 	struct xfs_trans_res	*tres;
1151c24b5dfaSDave Chinner 	uint			resblks;
1152c24b5dfaSDave Chinner 
1153c24b5dfaSDave Chinner 	trace_xfs_create(dp, name);
1154c24b5dfaSDave Chinner 
1155c24b5dfaSDave Chinner 	if (XFS_FORCED_SHUTDOWN(mp))
11562451337dSDave Chinner 		return -EIO;
1157c24b5dfaSDave Chinner 
1158163467d3SZhi Yong Wu 	prid = xfs_get_initial_prid(dp);
1159c24b5dfaSDave Chinner 
1160c24b5dfaSDave Chinner 	/*
1161c24b5dfaSDave Chinner 	 * Make sure that we have allocated dquot(s) on disk.
1162c24b5dfaSDave Chinner 	 */
11637aab1b28SDwight Engen 	error = xfs_qm_vop_dqalloc(dp, xfs_kuid_to_uid(current_fsuid()),
11647aab1b28SDwight Engen 					xfs_kgid_to_gid(current_fsgid()), prid,
1165c24b5dfaSDave Chinner 					XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
1166c24b5dfaSDave Chinner 					&udqp, &gdqp, &pdqp);
1167c24b5dfaSDave Chinner 	if (error)
1168c24b5dfaSDave Chinner 		return error;
1169c24b5dfaSDave Chinner 
1170c24b5dfaSDave Chinner 	if (is_dir) {
1171c24b5dfaSDave Chinner 		resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
1172062647a8SBrian Foster 		tres = &M_RES(mp)->tr_mkdir;
1173c24b5dfaSDave Chinner 	} else {
1174c24b5dfaSDave Chinner 		resblks = XFS_CREATE_SPACE_RES(mp, name->len);
1175062647a8SBrian Foster 		tres = &M_RES(mp)->tr_create;
1176c24b5dfaSDave Chinner 	}
1177c24b5dfaSDave Chinner 
1178c24b5dfaSDave Chinner 	/*
1179c24b5dfaSDave Chinner 	 * Initially assume that the file does not exist and
1180c24b5dfaSDave Chinner 	 * reserve the resources for that case.  If that is not
1181c24b5dfaSDave Chinner 	 * the case we'll drop the one we have and get a more
1182c24b5dfaSDave Chinner 	 * appropriate transaction later.
1183c24b5dfaSDave Chinner 	 */
1184253f4911SChristoph Hellwig 	error = xfs_trans_alloc(mp, tres, resblks, 0, 0, &tp);
11852451337dSDave Chinner 	if (error == -ENOSPC) {
1186c24b5dfaSDave Chinner 		/* flush outstanding delalloc blocks and retry */
1187c24b5dfaSDave Chinner 		xfs_flush_inodes(mp);
1188253f4911SChristoph Hellwig 		error = xfs_trans_alloc(mp, tres, resblks, 0, 0, &tp);
1189c24b5dfaSDave Chinner 	}
11904906e215SChristoph Hellwig 	if (error)
1191253f4911SChristoph Hellwig 		goto out_release_inode;
1192c24b5dfaSDave Chinner 
119365523218SChristoph Hellwig 	xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1194c24b5dfaSDave Chinner 	unlock_dp_on_error = true;
1195c24b5dfaSDave Chinner 
1196c24b5dfaSDave Chinner 	/*
1197c24b5dfaSDave Chinner 	 * Reserve disk quota and the inode.
1198c24b5dfaSDave Chinner 	 */
1199c24b5dfaSDave Chinner 	error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
1200c24b5dfaSDave Chinner 						pdqp, resblks, 1, 0);
1201c24b5dfaSDave Chinner 	if (error)
1202c24b5dfaSDave Chinner 		goto out_trans_cancel;
1203c24b5dfaSDave Chinner 
1204c24b5dfaSDave Chinner 	/*
1205c24b5dfaSDave Chinner 	 * A newly created regular or special file just has one directory
1206c24b5dfaSDave Chinner 	 * entry pointing to them, but a directory also the "." entry
1207c24b5dfaSDave Chinner 	 * pointing to itself.
1208c24b5dfaSDave Chinner 	 */
1209c959025eSChandan Rajendra 	error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev, prid, &ip);
1210d6077aa3SJan Kara 	if (error)
1211c24b5dfaSDave Chinner 		goto out_trans_cancel;
1212c24b5dfaSDave Chinner 
1213c24b5dfaSDave Chinner 	/*
1214c24b5dfaSDave Chinner 	 * Now we join the directory inode to the transaction.  We do not do it
1215c24b5dfaSDave Chinner 	 * earlier because xfs_dir_ialloc might commit the previous transaction
1216c24b5dfaSDave Chinner 	 * (and release all the locks).  An error from here on will result in
1217c24b5dfaSDave Chinner 	 * the transaction cancel unlocking dp so don't do it explicitly in the
1218c24b5dfaSDave Chinner 	 * error path.
1219c24b5dfaSDave Chinner 	 */
122065523218SChristoph Hellwig 	xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1221c24b5dfaSDave Chinner 	unlock_dp_on_error = false;
1222c24b5dfaSDave Chinner 
1223381eee69SBrian Foster 	error = xfs_dir_createname(tp, dp, name, ip->i_ino,
1224c9cfdb38SBrian Foster 				   resblks ?
1225c24b5dfaSDave Chinner 					resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
1226c24b5dfaSDave Chinner 	if (error) {
12272451337dSDave Chinner 		ASSERT(error != -ENOSPC);
12284906e215SChristoph Hellwig 		goto out_trans_cancel;
1229c24b5dfaSDave Chinner 	}
1230c24b5dfaSDave Chinner 	xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1231c24b5dfaSDave Chinner 	xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1232c24b5dfaSDave Chinner 
1233c24b5dfaSDave Chinner 	if (is_dir) {
1234c24b5dfaSDave Chinner 		error = xfs_dir_init(tp, ip, dp);
1235c24b5dfaSDave Chinner 		if (error)
1236*c8eac49eSBrian Foster 			goto out_trans_cancel;
1237c24b5dfaSDave Chinner 
1238c24b5dfaSDave Chinner 		error = xfs_bumplink(tp, dp);
1239c24b5dfaSDave Chinner 		if (error)
1240*c8eac49eSBrian Foster 			goto out_trans_cancel;
1241c24b5dfaSDave Chinner 	}
1242c24b5dfaSDave Chinner 
1243c24b5dfaSDave Chinner 	/*
1244c24b5dfaSDave Chinner 	 * If this is a synchronous mount, make sure that the
1245c24b5dfaSDave Chinner 	 * create transaction goes to disk before returning to
1246c24b5dfaSDave Chinner 	 * the user.
1247c24b5dfaSDave Chinner 	 */
1248c24b5dfaSDave Chinner 	if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1249c24b5dfaSDave Chinner 		xfs_trans_set_sync(tp);
1250c24b5dfaSDave Chinner 
1251c24b5dfaSDave Chinner 	/*
1252c24b5dfaSDave Chinner 	 * Attach the dquot(s) to the inodes and modify them incore.
1253c24b5dfaSDave Chinner 	 * These ids of the inode couldn't have changed since the new
1254c24b5dfaSDave Chinner 	 * inode has been locked ever since it was created.
1255c24b5dfaSDave Chinner 	 */
1256c24b5dfaSDave Chinner 	xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
1257c24b5dfaSDave Chinner 
125870393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
1259c24b5dfaSDave Chinner 	if (error)
1260c24b5dfaSDave Chinner 		goto out_release_inode;
1261c24b5dfaSDave Chinner 
1262c24b5dfaSDave Chinner 	xfs_qm_dqrele(udqp);
1263c24b5dfaSDave Chinner 	xfs_qm_dqrele(gdqp);
1264c24b5dfaSDave Chinner 	xfs_qm_dqrele(pdqp);
1265c24b5dfaSDave Chinner 
1266c24b5dfaSDave Chinner 	*ipp = ip;
1267c24b5dfaSDave Chinner 	return 0;
1268c24b5dfaSDave Chinner 
1269c24b5dfaSDave Chinner  out_trans_cancel:
12704906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
1271c24b5dfaSDave Chinner  out_release_inode:
1272c24b5dfaSDave Chinner 	/*
127358c90473SDave Chinner 	 * Wait until after the current transaction is aborted to finish the
127458c90473SDave Chinner 	 * setup of the inode and release the inode.  This prevents recursive
127558c90473SDave Chinner 	 * transactions and deadlocks from xfs_inactive.
1276c24b5dfaSDave Chinner 	 */
127758c90473SDave Chinner 	if (ip) {
127858c90473SDave Chinner 		xfs_finish_inode_setup(ip);
1279c24b5dfaSDave Chinner 		IRELE(ip);
128058c90473SDave Chinner 	}
1281c24b5dfaSDave Chinner 
1282c24b5dfaSDave Chinner 	xfs_qm_dqrele(udqp);
1283c24b5dfaSDave Chinner 	xfs_qm_dqrele(gdqp);
1284c24b5dfaSDave Chinner 	xfs_qm_dqrele(pdqp);
1285c24b5dfaSDave Chinner 
1286c24b5dfaSDave Chinner 	if (unlock_dp_on_error)
128765523218SChristoph Hellwig 		xfs_iunlock(dp, XFS_ILOCK_EXCL);
1288c24b5dfaSDave Chinner 	return error;
1289c24b5dfaSDave Chinner }
1290c24b5dfaSDave Chinner 
1291c24b5dfaSDave Chinner int
129299b6436bSZhi Yong Wu xfs_create_tmpfile(
129399b6436bSZhi Yong Wu 	struct xfs_inode	*dp,
1294330033d6SBrian Foster 	umode_t			mode,
1295330033d6SBrian Foster 	struct xfs_inode	**ipp)
129699b6436bSZhi Yong Wu {
129799b6436bSZhi Yong Wu 	struct xfs_mount	*mp = dp->i_mount;
129899b6436bSZhi Yong Wu 	struct xfs_inode	*ip = NULL;
129999b6436bSZhi Yong Wu 	struct xfs_trans	*tp = NULL;
130099b6436bSZhi Yong Wu 	int			error;
130199b6436bSZhi Yong Wu 	prid_t                  prid;
130299b6436bSZhi Yong Wu 	struct xfs_dquot	*udqp = NULL;
130399b6436bSZhi Yong Wu 	struct xfs_dquot	*gdqp = NULL;
130499b6436bSZhi Yong Wu 	struct xfs_dquot	*pdqp = NULL;
130599b6436bSZhi Yong Wu 	struct xfs_trans_res	*tres;
130699b6436bSZhi Yong Wu 	uint			resblks;
130799b6436bSZhi Yong Wu 
130899b6436bSZhi Yong Wu 	if (XFS_FORCED_SHUTDOWN(mp))
13092451337dSDave Chinner 		return -EIO;
131099b6436bSZhi Yong Wu 
131199b6436bSZhi Yong Wu 	prid = xfs_get_initial_prid(dp);
131299b6436bSZhi Yong Wu 
131399b6436bSZhi Yong Wu 	/*
131499b6436bSZhi Yong Wu 	 * Make sure that we have allocated dquot(s) on disk.
131599b6436bSZhi Yong Wu 	 */
131699b6436bSZhi Yong Wu 	error = xfs_qm_vop_dqalloc(dp, xfs_kuid_to_uid(current_fsuid()),
131799b6436bSZhi Yong Wu 				xfs_kgid_to_gid(current_fsgid()), prid,
131899b6436bSZhi Yong Wu 				XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
131999b6436bSZhi Yong Wu 				&udqp, &gdqp, &pdqp);
132099b6436bSZhi Yong Wu 	if (error)
132199b6436bSZhi Yong Wu 		return error;
132299b6436bSZhi Yong Wu 
132399b6436bSZhi Yong Wu 	resblks = XFS_IALLOC_SPACE_RES(mp);
132499b6436bSZhi Yong Wu 	tres = &M_RES(mp)->tr_create_tmpfile;
1325253f4911SChristoph Hellwig 
1326253f4911SChristoph Hellwig 	error = xfs_trans_alloc(mp, tres, resblks, 0, 0, &tp);
13274906e215SChristoph Hellwig 	if (error)
1328253f4911SChristoph Hellwig 		goto out_release_inode;
132999b6436bSZhi Yong Wu 
133099b6436bSZhi Yong Wu 	error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
133199b6436bSZhi Yong Wu 						pdqp, resblks, 1, 0);
133299b6436bSZhi Yong Wu 	if (error)
133399b6436bSZhi Yong Wu 		goto out_trans_cancel;
133499b6436bSZhi Yong Wu 
1335c959025eSChandan Rajendra 	error = xfs_dir_ialloc(&tp, dp, mode, 1, 0, prid, &ip);
1336d6077aa3SJan Kara 	if (error)
133799b6436bSZhi Yong Wu 		goto out_trans_cancel;
133899b6436bSZhi Yong Wu 
133999b6436bSZhi Yong Wu 	if (mp->m_flags & XFS_MOUNT_WSYNC)
134099b6436bSZhi Yong Wu 		xfs_trans_set_sync(tp);
134199b6436bSZhi Yong Wu 
134299b6436bSZhi Yong Wu 	/*
134399b6436bSZhi Yong Wu 	 * Attach the dquot(s) to the inodes and modify them incore.
134499b6436bSZhi Yong Wu 	 * These ids of the inode couldn't have changed since the new
134599b6436bSZhi Yong Wu 	 * inode has been locked ever since it was created.
134699b6436bSZhi Yong Wu 	 */
134799b6436bSZhi Yong Wu 	xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
134899b6436bSZhi Yong Wu 
134999b6436bSZhi Yong Wu 	error = xfs_iunlink(tp, ip);
135099b6436bSZhi Yong Wu 	if (error)
13514906e215SChristoph Hellwig 		goto out_trans_cancel;
135299b6436bSZhi Yong Wu 
135370393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
135499b6436bSZhi Yong Wu 	if (error)
135599b6436bSZhi Yong Wu 		goto out_release_inode;
135699b6436bSZhi Yong Wu 
135799b6436bSZhi Yong Wu 	xfs_qm_dqrele(udqp);
135899b6436bSZhi Yong Wu 	xfs_qm_dqrele(gdqp);
135999b6436bSZhi Yong Wu 	xfs_qm_dqrele(pdqp);
136099b6436bSZhi Yong Wu 
1361330033d6SBrian Foster 	*ipp = ip;
136299b6436bSZhi Yong Wu 	return 0;
136399b6436bSZhi Yong Wu 
136499b6436bSZhi Yong Wu  out_trans_cancel:
13654906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
136699b6436bSZhi Yong Wu  out_release_inode:
136799b6436bSZhi Yong Wu 	/*
136858c90473SDave Chinner 	 * Wait until after the current transaction is aborted to finish the
136958c90473SDave Chinner 	 * setup of the inode and release the inode.  This prevents recursive
137058c90473SDave Chinner 	 * transactions and deadlocks from xfs_inactive.
137199b6436bSZhi Yong Wu 	 */
137258c90473SDave Chinner 	if (ip) {
137358c90473SDave Chinner 		xfs_finish_inode_setup(ip);
137499b6436bSZhi Yong Wu 		IRELE(ip);
137558c90473SDave Chinner 	}
137699b6436bSZhi Yong Wu 
137799b6436bSZhi Yong Wu 	xfs_qm_dqrele(udqp);
137899b6436bSZhi Yong Wu 	xfs_qm_dqrele(gdqp);
137999b6436bSZhi Yong Wu 	xfs_qm_dqrele(pdqp);
138099b6436bSZhi Yong Wu 
138199b6436bSZhi Yong Wu 	return error;
138299b6436bSZhi Yong Wu }
138399b6436bSZhi Yong Wu 
138499b6436bSZhi Yong Wu int
1385c24b5dfaSDave Chinner xfs_link(
1386c24b5dfaSDave Chinner 	xfs_inode_t		*tdp,
1387c24b5dfaSDave Chinner 	xfs_inode_t		*sip,
1388c24b5dfaSDave Chinner 	struct xfs_name		*target_name)
1389c24b5dfaSDave Chinner {
1390c24b5dfaSDave Chinner 	xfs_mount_t		*mp = tdp->i_mount;
1391c24b5dfaSDave Chinner 	xfs_trans_t		*tp;
1392c24b5dfaSDave Chinner 	int			error;
1393c24b5dfaSDave Chinner 	int			resblks;
1394c24b5dfaSDave Chinner 
1395c24b5dfaSDave Chinner 	trace_xfs_link(tdp, target_name);
1396c24b5dfaSDave Chinner 
1397c19b3b05SDave Chinner 	ASSERT(!S_ISDIR(VFS_I(sip)->i_mode));
1398c24b5dfaSDave Chinner 
1399c24b5dfaSDave Chinner 	if (XFS_FORCED_SHUTDOWN(mp))
14002451337dSDave Chinner 		return -EIO;
1401c24b5dfaSDave Chinner 
1402c14cfccaSDarrick J. Wong 	error = xfs_qm_dqattach(sip);
1403c24b5dfaSDave Chinner 	if (error)
1404c24b5dfaSDave Chinner 		goto std_return;
1405c24b5dfaSDave Chinner 
1406c14cfccaSDarrick J. Wong 	error = xfs_qm_dqattach(tdp);
1407c24b5dfaSDave Chinner 	if (error)
1408c24b5dfaSDave Chinner 		goto std_return;
1409c24b5dfaSDave Chinner 
1410c24b5dfaSDave Chinner 	resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
1411253f4911SChristoph Hellwig 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_link, resblks, 0, 0, &tp);
14122451337dSDave Chinner 	if (error == -ENOSPC) {
1413c24b5dfaSDave Chinner 		resblks = 0;
1414253f4911SChristoph Hellwig 		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_link, 0, 0, 0, &tp);
1415c24b5dfaSDave Chinner 	}
14164906e215SChristoph Hellwig 	if (error)
1417253f4911SChristoph Hellwig 		goto std_return;
1418c24b5dfaSDave Chinner 
14197c2d238aSDarrick J. Wong 	xfs_lock_two_inodes(sip, XFS_ILOCK_EXCL, tdp, XFS_ILOCK_EXCL);
1420c24b5dfaSDave Chinner 
1421c24b5dfaSDave Chinner 	xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
142265523218SChristoph Hellwig 	xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
1423c24b5dfaSDave Chinner 
1424c24b5dfaSDave Chinner 	/*
1425c24b5dfaSDave Chinner 	 * If we are using project inheritance, we only allow hard link
1426c24b5dfaSDave Chinner 	 * creation in our tree when the project IDs are the same; else
1427c24b5dfaSDave Chinner 	 * the tree quota mechanism could be circumvented.
1428c24b5dfaSDave Chinner 	 */
1429c24b5dfaSDave Chinner 	if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
1430c24b5dfaSDave Chinner 		     (xfs_get_projid(tdp) != xfs_get_projid(sip)))) {
14312451337dSDave Chinner 		error = -EXDEV;
1432c24b5dfaSDave Chinner 		goto error_return;
1433c24b5dfaSDave Chinner 	}
1434c24b5dfaSDave Chinner 
143594f3cad5SEric Sandeen 	if (!resblks) {
143694f3cad5SEric Sandeen 		error = xfs_dir_canenter(tp, tdp, target_name);
1437c24b5dfaSDave Chinner 		if (error)
1438c24b5dfaSDave Chinner 			goto error_return;
143994f3cad5SEric Sandeen 	}
1440c24b5dfaSDave Chinner 
144154d7b5c1SDave Chinner 	/*
144254d7b5c1SDave Chinner 	 * Handle initial link state of O_TMPFILE inode
144354d7b5c1SDave Chinner 	 */
144454d7b5c1SDave Chinner 	if (VFS_I(sip)->i_nlink == 0) {
1445ab297431SZhi Yong Wu 		error = xfs_iunlink_remove(tp, sip);
1446ab297431SZhi Yong Wu 		if (error)
14474906e215SChristoph Hellwig 			goto error_return;
1448ab297431SZhi Yong Wu 	}
1449ab297431SZhi Yong Wu 
1450c24b5dfaSDave Chinner 	error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
1451381eee69SBrian Foster 				   resblks);
1452c24b5dfaSDave Chinner 	if (error)
14534906e215SChristoph Hellwig 		goto error_return;
1454c24b5dfaSDave Chinner 	xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1455c24b5dfaSDave Chinner 	xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
1456c24b5dfaSDave Chinner 
1457c24b5dfaSDave Chinner 	error = xfs_bumplink(tp, sip);
1458c24b5dfaSDave Chinner 	if (error)
14594906e215SChristoph Hellwig 		goto error_return;
1460c24b5dfaSDave Chinner 
1461c24b5dfaSDave Chinner 	/*
1462c24b5dfaSDave Chinner 	 * If this is a synchronous mount, make sure that the
1463c24b5dfaSDave Chinner 	 * link transaction goes to disk before returning to
1464c24b5dfaSDave Chinner 	 * the user.
1465c24b5dfaSDave Chinner 	 */
1466f6106efaSEric Sandeen 	if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1467c24b5dfaSDave Chinner 		xfs_trans_set_sync(tp);
1468c24b5dfaSDave Chinner 
146970393313SChristoph Hellwig 	return xfs_trans_commit(tp);
1470c24b5dfaSDave Chinner 
1471c24b5dfaSDave Chinner  error_return:
14724906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
1473c24b5dfaSDave Chinner  std_return:
1474c24b5dfaSDave Chinner 	return error;
1475c24b5dfaSDave Chinner }
1476c24b5dfaSDave Chinner 
1477363e59baSDarrick J. Wong /* Clear the reflink flag and the cowblocks tag if possible. */
1478363e59baSDarrick J. Wong static void
1479363e59baSDarrick J. Wong xfs_itruncate_clear_reflink_flags(
1480363e59baSDarrick J. Wong 	struct xfs_inode	*ip)
1481363e59baSDarrick J. Wong {
1482363e59baSDarrick J. Wong 	struct xfs_ifork	*dfork;
1483363e59baSDarrick J. Wong 	struct xfs_ifork	*cfork;
1484363e59baSDarrick J. Wong 
1485363e59baSDarrick J. Wong 	if (!xfs_is_reflink_inode(ip))
1486363e59baSDarrick J. Wong 		return;
1487363e59baSDarrick J. Wong 	dfork = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1488363e59baSDarrick J. Wong 	cfork = XFS_IFORK_PTR(ip, XFS_COW_FORK);
1489363e59baSDarrick J. Wong 	if (dfork->if_bytes == 0 && cfork->if_bytes == 0)
1490363e59baSDarrick J. Wong 		ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
1491363e59baSDarrick J. Wong 	if (cfork->if_bytes == 0)
1492363e59baSDarrick J. Wong 		xfs_inode_clear_cowblocks_tag(ip);
1493363e59baSDarrick J. Wong }
1494363e59baSDarrick J. Wong 
14951da177e4SLinus Torvalds /*
14968f04c47aSChristoph Hellwig  * Free up the underlying blocks past new_size.  The new size must be smaller
14978f04c47aSChristoph Hellwig  * than the current size.  This routine can be used both for the attribute and
14988f04c47aSChristoph Hellwig  * data fork, and does not modify the inode size, which is left to the caller.
14991da177e4SLinus Torvalds  *
1500f6485057SDavid Chinner  * The transaction passed to this routine must have made a permanent log
1501f6485057SDavid Chinner  * reservation of at least XFS_ITRUNCATE_LOG_RES.  This routine may commit the
1502f6485057SDavid Chinner  * given transaction and start new ones, so make sure everything involved in
1503f6485057SDavid Chinner  * the transaction is tidy before calling here.  Some transaction will be
1504f6485057SDavid Chinner  * returned to the caller to be committed.  The incoming transaction must
1505f6485057SDavid Chinner  * already include the inode, and both inode locks must be held exclusively.
1506f6485057SDavid Chinner  * The inode must also be "held" within the transaction.  On return the inode
1507f6485057SDavid Chinner  * will be "held" within the returned transaction.  This routine does NOT
1508f6485057SDavid Chinner  * require any disk space to be reserved for it within the transaction.
15091da177e4SLinus Torvalds  *
1510f6485057SDavid Chinner  * If we get an error, we must return with the inode locked and linked into the
1511f6485057SDavid Chinner  * current transaction. This keeps things simple for the higher level code,
1512f6485057SDavid Chinner  * because it always knows that the inode is locked and held in the transaction
1513f6485057SDavid Chinner  * that returns to it whether errors occur or not.  We don't mark the inode
1514f6485057SDavid Chinner  * dirty on error so that transactions can be easily aborted if possible.
15151da177e4SLinus Torvalds  */
15161da177e4SLinus Torvalds int
15174e529339SBrian Foster xfs_itruncate_extents_flags(
15188f04c47aSChristoph Hellwig 	struct xfs_trans	**tpp,
15198f04c47aSChristoph Hellwig 	struct xfs_inode	*ip,
15208f04c47aSChristoph Hellwig 	int			whichfork,
152113b86fc3SBrian Foster 	xfs_fsize_t		new_size,
15224e529339SBrian Foster 	int			flags)
15231da177e4SLinus Torvalds {
15248f04c47aSChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
15258f04c47aSChristoph Hellwig 	struct xfs_trans	*tp = *tpp;
15261da177e4SLinus Torvalds 	xfs_fileoff_t		first_unmap_block;
15271da177e4SLinus Torvalds 	xfs_fileoff_t		last_block;
15288f04c47aSChristoph Hellwig 	xfs_filblks_t		unmap_len;
15298f04c47aSChristoph Hellwig 	int			error = 0;
15308f04c47aSChristoph Hellwig 	int			done = 0;
15311da177e4SLinus Torvalds 
15320b56185bSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
15330b56185bSChristoph Hellwig 	ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
15340b56185bSChristoph Hellwig 	       xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1535ce7ae151SChristoph Hellwig 	ASSERT(new_size <= XFS_ISIZE(ip));
15368f04c47aSChristoph Hellwig 	ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
15371da177e4SLinus Torvalds 	ASSERT(ip->i_itemp != NULL);
1538898621d5SChristoph Hellwig 	ASSERT(ip->i_itemp->ili_lock_flags == 0);
15391da177e4SLinus Torvalds 	ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
15401da177e4SLinus Torvalds 
1541673e8e59SChristoph Hellwig 	trace_xfs_itruncate_extents_start(ip, new_size);
1542673e8e59SChristoph Hellwig 
15434e529339SBrian Foster 	flags |= xfs_bmapi_aflag(whichfork);
154413b86fc3SBrian Foster 
15451da177e4SLinus Torvalds 	/*
15461da177e4SLinus Torvalds 	 * Since it is possible for space to become allocated beyond
15471da177e4SLinus Torvalds 	 * the end of the file (in a crash where the space is allocated
15481da177e4SLinus Torvalds 	 * but the inode size is not yet updated), simply remove any
15491da177e4SLinus Torvalds 	 * blocks which show up between the new EOF and the maximum
15501da177e4SLinus Torvalds 	 * possible file size.  If the first block to be removed is
15511da177e4SLinus Torvalds 	 * beyond the maximum file size (ie it is the same as last_block),
15521da177e4SLinus Torvalds 	 * then there is nothing to do.
15531da177e4SLinus Torvalds 	 */
15548f04c47aSChristoph Hellwig 	first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
155532972383SDave Chinner 	last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
15568f04c47aSChristoph Hellwig 	if (first_unmap_block == last_block)
15578f04c47aSChristoph Hellwig 		return 0;
15588f04c47aSChristoph Hellwig 
15598f04c47aSChristoph Hellwig 	ASSERT(first_unmap_block < last_block);
15601da177e4SLinus Torvalds 	unmap_len = last_block - first_unmap_block + 1;
15611da177e4SLinus Torvalds 	while (!done) {
156202dff7bfSBrian Foster 		ASSERT(tp->t_firstblock == NULLFSBLOCK);
156313b86fc3SBrian Foster 		error = xfs_bunmapi(tp, ip, first_unmap_block, unmap_len, flags,
15642af52842SBrian Foster 				    XFS_ITRUNC_MAX_EXTENTS, &done);
15658f04c47aSChristoph Hellwig 		if (error)
15668f04c47aSChristoph Hellwig 			goto out_bmap_cancel;
15671da177e4SLinus Torvalds 
15681da177e4SLinus Torvalds 		/*
15691da177e4SLinus Torvalds 		 * Duplicate the transaction that has the permanent
15701da177e4SLinus Torvalds 		 * reservation and commit the old transaction.
15711da177e4SLinus Torvalds 		 */
15724bcfa613SBrian Foster 		xfs_defer_ijoin(tp->t_dfops, ip);
15734bcfa613SBrian Foster 		error = xfs_defer_finish(&tp, tp->t_dfops);
15748f04c47aSChristoph Hellwig 		if (error)
15758f04c47aSChristoph Hellwig 			goto out_bmap_cancel;
15761da177e4SLinus Torvalds 
1577411350dfSChristoph Hellwig 		error = xfs_trans_roll_inode(&tp, ip);
15781da177e4SLinus Torvalds 		if (error)
15798f04c47aSChristoph Hellwig 			goto out;
15801da177e4SLinus Torvalds 	}
15818f04c47aSChristoph Hellwig 
15824919d42aSDarrick J. Wong 	if (whichfork == XFS_DATA_FORK) {
1583aa8968f2SDarrick J. Wong 		/* Remove all pending CoW reservations. */
15844919d42aSDarrick J. Wong 		error = xfs_reflink_cancel_cow_blocks(ip, &tp,
15854919d42aSDarrick J. Wong 				first_unmap_block, last_block, true);
1586aa8968f2SDarrick J. Wong 		if (error)
1587aa8968f2SDarrick J. Wong 			goto out;
1588aa8968f2SDarrick J. Wong 
1589363e59baSDarrick J. Wong 		xfs_itruncate_clear_reflink_flags(ip);
15904919d42aSDarrick J. Wong 	}
1591aa8968f2SDarrick J. Wong 
1592673e8e59SChristoph Hellwig 	/*
1593673e8e59SChristoph Hellwig 	 * Always re-log the inode so that our permanent transaction can keep
1594673e8e59SChristoph Hellwig 	 * on rolling it forward in the log.
1595673e8e59SChristoph Hellwig 	 */
1596673e8e59SChristoph Hellwig 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1597673e8e59SChristoph Hellwig 
1598673e8e59SChristoph Hellwig 	trace_xfs_itruncate_extents_end(ip, new_size);
1599673e8e59SChristoph Hellwig 
16008f04c47aSChristoph Hellwig out:
16018f04c47aSChristoph Hellwig 	*tpp = tp;
16028f04c47aSChristoph Hellwig 	return error;
16038f04c47aSChristoph Hellwig out_bmap_cancel:
16041da177e4SLinus Torvalds 	/*
16058f04c47aSChristoph Hellwig 	 * If the bunmapi call encounters an error, return to the caller where
16068f04c47aSChristoph Hellwig 	 * the transaction can be properly aborted.  We just need to make sure
16078f04c47aSChristoph Hellwig 	 * we're not holding any resources that we were not when we came in.
16081da177e4SLinus Torvalds 	 */
16094bcfa613SBrian Foster 	xfs_defer_cancel(tp->t_dfops);
16108f04c47aSChristoph Hellwig 	goto out;
16118f04c47aSChristoph Hellwig }
16128f04c47aSChristoph Hellwig 
1613c24b5dfaSDave Chinner int
1614c24b5dfaSDave Chinner xfs_release(
1615c24b5dfaSDave Chinner 	xfs_inode_t	*ip)
1616c24b5dfaSDave Chinner {
1617c24b5dfaSDave Chinner 	xfs_mount_t	*mp = ip->i_mount;
1618c24b5dfaSDave Chinner 	int		error;
1619c24b5dfaSDave Chinner 
1620c19b3b05SDave Chinner 	if (!S_ISREG(VFS_I(ip)->i_mode) || (VFS_I(ip)->i_mode == 0))
1621c24b5dfaSDave Chinner 		return 0;
1622c24b5dfaSDave Chinner 
1623c24b5dfaSDave Chinner 	/* If this is a read-only mount, don't do this (would generate I/O) */
1624c24b5dfaSDave Chinner 	if (mp->m_flags & XFS_MOUNT_RDONLY)
1625c24b5dfaSDave Chinner 		return 0;
1626c24b5dfaSDave Chinner 
1627c24b5dfaSDave Chinner 	if (!XFS_FORCED_SHUTDOWN(mp)) {
1628c24b5dfaSDave Chinner 		int truncated;
1629c24b5dfaSDave Chinner 
1630c24b5dfaSDave Chinner 		/*
1631c24b5dfaSDave Chinner 		 * If we previously truncated this file and removed old data
1632c24b5dfaSDave Chinner 		 * in the process, we want to initiate "early" writeout on
1633c24b5dfaSDave Chinner 		 * the last close.  This is an attempt to combat the notorious
1634c24b5dfaSDave Chinner 		 * NULL files problem which is particularly noticeable from a
1635c24b5dfaSDave Chinner 		 * truncate down, buffered (re-)write (delalloc), followed by
1636c24b5dfaSDave Chinner 		 * a crash.  What we are effectively doing here is
1637c24b5dfaSDave Chinner 		 * significantly reducing the time window where we'd otherwise
1638c24b5dfaSDave Chinner 		 * be exposed to that problem.
1639c24b5dfaSDave Chinner 		 */
1640c24b5dfaSDave Chinner 		truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
1641c24b5dfaSDave Chinner 		if (truncated) {
1642c24b5dfaSDave Chinner 			xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
1643eac152b4SDave Chinner 			if (ip->i_delayed_blks > 0) {
16442451337dSDave Chinner 				error = filemap_flush(VFS_I(ip)->i_mapping);
1645c24b5dfaSDave Chinner 				if (error)
1646c24b5dfaSDave Chinner 					return error;
1647c24b5dfaSDave Chinner 			}
1648c24b5dfaSDave Chinner 		}
1649c24b5dfaSDave Chinner 	}
1650c24b5dfaSDave Chinner 
165154d7b5c1SDave Chinner 	if (VFS_I(ip)->i_nlink == 0)
1652c24b5dfaSDave Chinner 		return 0;
1653c24b5dfaSDave Chinner 
1654c24b5dfaSDave Chinner 	if (xfs_can_free_eofblocks(ip, false)) {
1655c24b5dfaSDave Chinner 
1656c24b5dfaSDave Chinner 		/*
1657a36b9261SBrian Foster 		 * Check if the inode is being opened, written and closed
1658a36b9261SBrian Foster 		 * frequently and we have delayed allocation blocks outstanding
1659a36b9261SBrian Foster 		 * (e.g. streaming writes from the NFS server), truncating the
1660a36b9261SBrian Foster 		 * blocks past EOF will cause fragmentation to occur.
1661a36b9261SBrian Foster 		 *
1662a36b9261SBrian Foster 		 * In this case don't do the truncation, but we have to be
1663a36b9261SBrian Foster 		 * careful how we detect this case. Blocks beyond EOF show up as
1664a36b9261SBrian Foster 		 * i_delayed_blks even when the inode is clean, so we need to
1665a36b9261SBrian Foster 		 * truncate them away first before checking for a dirty release.
1666a36b9261SBrian Foster 		 * Hence on the first dirty close we will still remove the
1667a36b9261SBrian Foster 		 * speculative allocation, but after that we will leave it in
1668a36b9261SBrian Foster 		 * place.
1669a36b9261SBrian Foster 		 */
1670a36b9261SBrian Foster 		if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
1671a36b9261SBrian Foster 			return 0;
1672a36b9261SBrian Foster 		/*
1673c24b5dfaSDave Chinner 		 * If we can't get the iolock just skip truncating the blocks
1674c24b5dfaSDave Chinner 		 * past EOF because we could deadlock with the mmap_sem
1675c24b5dfaSDave Chinner 		 * otherwise. We'll get another chance to drop them once the
1676c24b5dfaSDave Chinner 		 * last reference to the inode is dropped, so we'll never leak
1677c24b5dfaSDave Chinner 		 * blocks permanently.
1678c24b5dfaSDave Chinner 		 */
1679a36b9261SBrian Foster 		if (xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
1680a36b9261SBrian Foster 			error = xfs_free_eofblocks(ip);
1681a36b9261SBrian Foster 			xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1682a36b9261SBrian Foster 			if (error)
1683c24b5dfaSDave Chinner 				return error;
1684a36b9261SBrian Foster 		}
1685c24b5dfaSDave Chinner 
1686c24b5dfaSDave Chinner 		/* delalloc blocks after truncation means it really is dirty */
1687c24b5dfaSDave Chinner 		if (ip->i_delayed_blks)
1688c24b5dfaSDave Chinner 			xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
1689c24b5dfaSDave Chinner 	}
1690c24b5dfaSDave Chinner 	return 0;
1691c24b5dfaSDave Chinner }
1692c24b5dfaSDave Chinner 
1693c24b5dfaSDave Chinner /*
1694f7be2d7fSBrian Foster  * xfs_inactive_truncate
1695f7be2d7fSBrian Foster  *
1696f7be2d7fSBrian Foster  * Called to perform a truncate when an inode becomes unlinked.
1697f7be2d7fSBrian Foster  */
1698f7be2d7fSBrian Foster STATIC int
1699f7be2d7fSBrian Foster xfs_inactive_truncate(
1700f7be2d7fSBrian Foster 	struct xfs_inode *ip)
1701f7be2d7fSBrian Foster {
1702f7be2d7fSBrian Foster 	struct xfs_mount	*mp = ip->i_mount;
1703f7be2d7fSBrian Foster 	struct xfs_trans	*tp;
1704f7be2d7fSBrian Foster 	int			error;
1705f7be2d7fSBrian Foster 
1706253f4911SChristoph Hellwig 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
1707f7be2d7fSBrian Foster 	if (error) {
1708f7be2d7fSBrian Foster 		ASSERT(XFS_FORCED_SHUTDOWN(mp));
1709f7be2d7fSBrian Foster 		return error;
1710f7be2d7fSBrian Foster 	}
1711f7be2d7fSBrian Foster 	xfs_ilock(ip, XFS_ILOCK_EXCL);
1712f7be2d7fSBrian Foster 	xfs_trans_ijoin(tp, ip, 0);
1713f7be2d7fSBrian Foster 
1714f7be2d7fSBrian Foster 	/*
1715f7be2d7fSBrian Foster 	 * Log the inode size first to prevent stale data exposure in the event
1716f7be2d7fSBrian Foster 	 * of a system crash before the truncate completes. See the related
171769bca807SJan Kara 	 * comment in xfs_vn_setattr_size() for details.
1718f7be2d7fSBrian Foster 	 */
1719f7be2d7fSBrian Foster 	ip->i_d.di_size = 0;
1720f7be2d7fSBrian Foster 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1721f7be2d7fSBrian Foster 
1722f7be2d7fSBrian Foster 	error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
1723f7be2d7fSBrian Foster 	if (error)
1724f7be2d7fSBrian Foster 		goto error_trans_cancel;
1725f7be2d7fSBrian Foster 
1726f7be2d7fSBrian Foster 	ASSERT(ip->i_d.di_nextents == 0);
1727f7be2d7fSBrian Foster 
172870393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
1729f7be2d7fSBrian Foster 	if (error)
1730f7be2d7fSBrian Foster 		goto error_unlock;
1731f7be2d7fSBrian Foster 
1732f7be2d7fSBrian Foster 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
1733f7be2d7fSBrian Foster 	return 0;
1734f7be2d7fSBrian Foster 
1735f7be2d7fSBrian Foster error_trans_cancel:
17364906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
1737f7be2d7fSBrian Foster error_unlock:
1738f7be2d7fSBrian Foster 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
1739f7be2d7fSBrian Foster 	return error;
1740f7be2d7fSBrian Foster }
1741f7be2d7fSBrian Foster 
1742f7be2d7fSBrian Foster /*
174388877d2bSBrian Foster  * xfs_inactive_ifree()
174488877d2bSBrian Foster  *
174588877d2bSBrian Foster  * Perform the inode free when an inode is unlinked.
174688877d2bSBrian Foster  */
174788877d2bSBrian Foster STATIC int
174888877d2bSBrian Foster xfs_inactive_ifree(
174988877d2bSBrian Foster 	struct xfs_inode *ip)
175088877d2bSBrian Foster {
175188877d2bSBrian Foster 	struct xfs_mount	*mp = ip->i_mount;
175288877d2bSBrian Foster 	struct xfs_trans	*tp;
175388877d2bSBrian Foster 	int			error;
175488877d2bSBrian Foster 
17559d43b180SBrian Foster 	/*
175676d771b4SChristoph Hellwig 	 * We try to use a per-AG reservation for any block needed by the finobt
175776d771b4SChristoph Hellwig 	 * tree, but as the finobt feature predates the per-AG reservation
175876d771b4SChristoph Hellwig 	 * support a degraded file system might not have enough space for the
175976d771b4SChristoph Hellwig 	 * reservation at mount time.  In that case try to dip into the reserved
176076d771b4SChristoph Hellwig 	 * pool and pray.
17619d43b180SBrian Foster 	 *
17629d43b180SBrian Foster 	 * Send a warning if the reservation does happen to fail, as the inode
17639d43b180SBrian Foster 	 * now remains allocated and sits on the unlinked list until the fs is
17649d43b180SBrian Foster 	 * repaired.
17659d43b180SBrian Foster 	 */
176676d771b4SChristoph Hellwig 	if (unlikely(mp->m_inotbt_nores)) {
1767253f4911SChristoph Hellwig 		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ifree,
176876d771b4SChristoph Hellwig 				XFS_IFREE_SPACE_RES(mp), 0, XFS_TRANS_RESERVE,
176976d771b4SChristoph Hellwig 				&tp);
177076d771b4SChristoph Hellwig 	} else {
177176d771b4SChristoph Hellwig 		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ifree, 0, 0, 0, &tp);
177276d771b4SChristoph Hellwig 	}
177388877d2bSBrian Foster 	if (error) {
17742451337dSDave Chinner 		if (error == -ENOSPC) {
17759d43b180SBrian Foster 			xfs_warn_ratelimited(mp,
17769d43b180SBrian Foster 			"Failed to remove inode(s) from unlinked list. "
17779d43b180SBrian Foster 			"Please free space, unmount and run xfs_repair.");
17789d43b180SBrian Foster 		} else {
177988877d2bSBrian Foster 			ASSERT(XFS_FORCED_SHUTDOWN(mp));
17809d43b180SBrian Foster 		}
178188877d2bSBrian Foster 		return error;
178288877d2bSBrian Foster 	}
178388877d2bSBrian Foster 
178488877d2bSBrian Foster 	xfs_ilock(ip, XFS_ILOCK_EXCL);
178588877d2bSBrian Foster 	xfs_trans_ijoin(tp, ip, 0);
178688877d2bSBrian Foster 
17870e0417f3SBrian Foster 	error = xfs_ifree(tp, ip);
178888877d2bSBrian Foster 	if (error) {
178988877d2bSBrian Foster 		/*
179088877d2bSBrian Foster 		 * If we fail to free the inode, shut down.  The cancel
179188877d2bSBrian Foster 		 * might do that, we need to make sure.  Otherwise the
179288877d2bSBrian Foster 		 * inode might be lost for a long time or forever.
179388877d2bSBrian Foster 		 */
179488877d2bSBrian Foster 		if (!XFS_FORCED_SHUTDOWN(mp)) {
179588877d2bSBrian Foster 			xfs_notice(mp, "%s: xfs_ifree returned error %d",
179688877d2bSBrian Foster 				__func__, error);
179788877d2bSBrian Foster 			xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
179888877d2bSBrian Foster 		}
17994906e215SChristoph Hellwig 		xfs_trans_cancel(tp);
180088877d2bSBrian Foster 		xfs_iunlock(ip, XFS_ILOCK_EXCL);
180188877d2bSBrian Foster 		return error;
180288877d2bSBrian Foster 	}
180388877d2bSBrian Foster 
180488877d2bSBrian Foster 	/*
180588877d2bSBrian Foster 	 * Credit the quota account(s). The inode is gone.
180688877d2bSBrian Foster 	 */
180788877d2bSBrian Foster 	xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
180888877d2bSBrian Foster 
180988877d2bSBrian Foster 	/*
1810d4a97a04SBrian Foster 	 * Just ignore errors at this point.  There is nothing we can do except
1811d4a97a04SBrian Foster 	 * to try to keep going. Make sure it's not a silent error.
181288877d2bSBrian Foster 	 */
181370393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
181488877d2bSBrian Foster 	if (error)
181588877d2bSBrian Foster 		xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
181688877d2bSBrian Foster 			__func__, error);
181788877d2bSBrian Foster 
181888877d2bSBrian Foster 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
181988877d2bSBrian Foster 	return 0;
182088877d2bSBrian Foster }
182188877d2bSBrian Foster 
182288877d2bSBrian Foster /*
1823c24b5dfaSDave Chinner  * xfs_inactive
1824c24b5dfaSDave Chinner  *
1825c24b5dfaSDave Chinner  * This is called when the vnode reference count for the vnode
1826c24b5dfaSDave Chinner  * goes to zero.  If the file has been unlinked, then it must
1827c24b5dfaSDave Chinner  * now be truncated.  Also, we clear all of the read-ahead state
1828c24b5dfaSDave Chinner  * kept for the inode here since the file is now closed.
1829c24b5dfaSDave Chinner  */
183074564fb4SBrian Foster void
1831c24b5dfaSDave Chinner xfs_inactive(
1832c24b5dfaSDave Chinner 	xfs_inode_t	*ip)
1833c24b5dfaSDave Chinner {
18343d3c8b52SJie Liu 	struct xfs_mount	*mp;
18356231848cSDarrick J. Wong 	struct xfs_ifork	*cow_ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
1836c24b5dfaSDave Chinner 	int			error;
1837c24b5dfaSDave Chinner 	int			truncate = 0;
1838c24b5dfaSDave Chinner 
1839c24b5dfaSDave Chinner 	/*
1840c24b5dfaSDave Chinner 	 * If the inode is already free, then there can be nothing
1841c24b5dfaSDave Chinner 	 * to clean up here.
1842c24b5dfaSDave Chinner 	 */
1843c19b3b05SDave Chinner 	if (VFS_I(ip)->i_mode == 0) {
1844c24b5dfaSDave Chinner 		ASSERT(ip->i_df.if_real_bytes == 0);
1845c24b5dfaSDave Chinner 		ASSERT(ip->i_df.if_broot_bytes == 0);
184674564fb4SBrian Foster 		return;
1847c24b5dfaSDave Chinner 	}
1848c24b5dfaSDave Chinner 
1849c24b5dfaSDave Chinner 	mp = ip->i_mount;
185017c12bcdSDarrick J. Wong 	ASSERT(!xfs_iflags_test(ip, XFS_IRECOVERY));
1851c24b5dfaSDave Chinner 
1852c24b5dfaSDave Chinner 	/* If this is a read-only mount, don't do this (would generate I/O) */
1853c24b5dfaSDave Chinner 	if (mp->m_flags & XFS_MOUNT_RDONLY)
185474564fb4SBrian Foster 		return;
1855c24b5dfaSDave Chinner 
18566231848cSDarrick J. Wong 	/* Try to clean out the cow blocks if there are any. */
18576231848cSDarrick J. Wong 	if (xfs_is_reflink_inode(ip) && cow_ifp->if_bytes > 0)
18586231848cSDarrick J. Wong 		xfs_reflink_cancel_cow_range(ip, 0, NULLFILEOFF, true);
18596231848cSDarrick J. Wong 
186054d7b5c1SDave Chinner 	if (VFS_I(ip)->i_nlink != 0) {
1861c24b5dfaSDave Chinner 		/*
1862c24b5dfaSDave Chinner 		 * force is true because we are evicting an inode from the
1863c24b5dfaSDave Chinner 		 * cache. Post-eof blocks must be freed, lest we end up with
1864c24b5dfaSDave Chinner 		 * broken free space accounting.
18653b4683c2SBrian Foster 		 *
18663b4683c2SBrian Foster 		 * Note: don't bother with iolock here since lockdep complains
18673b4683c2SBrian Foster 		 * about acquiring it in reclaim context. We have the only
18683b4683c2SBrian Foster 		 * reference to the inode at this point anyways.
1869c24b5dfaSDave Chinner 		 */
18703b4683c2SBrian Foster 		if (xfs_can_free_eofblocks(ip, true))
1871a36b9261SBrian Foster 			xfs_free_eofblocks(ip);
187274564fb4SBrian Foster 
187374564fb4SBrian Foster 		return;
1874c24b5dfaSDave Chinner 	}
1875c24b5dfaSDave Chinner 
1876c19b3b05SDave Chinner 	if (S_ISREG(VFS_I(ip)->i_mode) &&
1877c24b5dfaSDave Chinner 	    (ip->i_d.di_size != 0 || XFS_ISIZE(ip) != 0 ||
1878c24b5dfaSDave Chinner 	     ip->i_d.di_nextents > 0 || ip->i_delayed_blks > 0))
1879c24b5dfaSDave Chinner 		truncate = 1;
1880c24b5dfaSDave Chinner 
1881c14cfccaSDarrick J. Wong 	error = xfs_qm_dqattach(ip);
1882c24b5dfaSDave Chinner 	if (error)
188374564fb4SBrian Foster 		return;
1884c24b5dfaSDave Chinner 
1885c19b3b05SDave Chinner 	if (S_ISLNK(VFS_I(ip)->i_mode))
188636b21ddeSBrian Foster 		error = xfs_inactive_symlink(ip);
1887f7be2d7fSBrian Foster 	else if (truncate)
1888f7be2d7fSBrian Foster 		error = xfs_inactive_truncate(ip);
188936b21ddeSBrian Foster 	if (error)
189074564fb4SBrian Foster 		return;
1891c24b5dfaSDave Chinner 
1892c24b5dfaSDave Chinner 	/*
1893c24b5dfaSDave Chinner 	 * If there are attributes associated with the file then blow them away
1894c24b5dfaSDave Chinner 	 * now.  The code calls a routine that recursively deconstructs the
18956dfe5a04SDave Chinner 	 * attribute fork. If also blows away the in-core attribute fork.
1896c24b5dfaSDave Chinner 	 */
18976dfe5a04SDave Chinner 	if (XFS_IFORK_Q(ip)) {
1898c24b5dfaSDave Chinner 		error = xfs_attr_inactive(ip);
1899c24b5dfaSDave Chinner 		if (error)
190074564fb4SBrian Foster 			return;
1901c24b5dfaSDave Chinner 	}
1902c24b5dfaSDave Chinner 
19036dfe5a04SDave Chinner 	ASSERT(!ip->i_afp);
1904c24b5dfaSDave Chinner 	ASSERT(ip->i_d.di_anextents == 0);
19056dfe5a04SDave Chinner 	ASSERT(ip->i_d.di_forkoff == 0);
1906c24b5dfaSDave Chinner 
1907c24b5dfaSDave Chinner 	/*
1908c24b5dfaSDave Chinner 	 * Free the inode.
1909c24b5dfaSDave Chinner 	 */
191088877d2bSBrian Foster 	error = xfs_inactive_ifree(ip);
1911c24b5dfaSDave Chinner 	if (error)
191274564fb4SBrian Foster 		return;
1913c24b5dfaSDave Chinner 
1914c24b5dfaSDave Chinner 	/*
1915c24b5dfaSDave Chinner 	 * Release the dquots held by inode, if any.
1916c24b5dfaSDave Chinner 	 */
1917c24b5dfaSDave Chinner 	xfs_qm_dqdetach(ip);
1918c24b5dfaSDave Chinner }
1919c24b5dfaSDave Chinner 
19201da177e4SLinus Torvalds /*
192154d7b5c1SDave Chinner  * This is called when the inode's link count goes to 0 or we are creating a
192254d7b5c1SDave Chinner  * tmpfile via O_TMPFILE. In the case of a tmpfile, @ignore_linkcount will be
192354d7b5c1SDave Chinner  * set to true as the link count is dropped to zero by the VFS after we've
192454d7b5c1SDave Chinner  * created the file successfully, so we have to add it to the unlinked list
192554d7b5c1SDave Chinner  * while the link count is non-zero.
192654d7b5c1SDave Chinner  *
192754d7b5c1SDave Chinner  * We place the on-disk inode on a list in the AGI.  It will be pulled from this
192854d7b5c1SDave Chinner  * list when the inode is freed.
19291da177e4SLinus Torvalds  */
193054d7b5c1SDave Chinner STATIC int
19311da177e4SLinus Torvalds xfs_iunlink(
193254d7b5c1SDave Chinner 	struct xfs_trans *tp,
193354d7b5c1SDave Chinner 	struct xfs_inode *ip)
19341da177e4SLinus Torvalds {
193554d7b5c1SDave Chinner 	xfs_mount_t	*mp = tp->t_mountp;
19361da177e4SLinus Torvalds 	xfs_agi_t	*agi;
19371da177e4SLinus Torvalds 	xfs_dinode_t	*dip;
19381da177e4SLinus Torvalds 	xfs_buf_t	*agibp;
19391da177e4SLinus Torvalds 	xfs_buf_t	*ibp;
19401da177e4SLinus Torvalds 	xfs_agino_t	agino;
19411da177e4SLinus Torvalds 	short		bucket_index;
19421da177e4SLinus Torvalds 	int		offset;
19431da177e4SLinus Torvalds 	int		error;
19441da177e4SLinus Torvalds 
1945c19b3b05SDave Chinner 	ASSERT(VFS_I(ip)->i_mode != 0);
19461da177e4SLinus Torvalds 
19471da177e4SLinus Torvalds 	/*
19481da177e4SLinus Torvalds 	 * Get the agi buffer first.  It ensures lock ordering
19491da177e4SLinus Torvalds 	 * on the list.
19501da177e4SLinus Torvalds 	 */
19515e1be0fbSChristoph Hellwig 	error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
1952859d7182SVlad Apostolov 	if (error)
19531da177e4SLinus Torvalds 		return error;
19541da177e4SLinus Torvalds 	agi = XFS_BUF_TO_AGI(agibp);
19555e1be0fbSChristoph Hellwig 
19561da177e4SLinus Torvalds 	/*
19571da177e4SLinus Torvalds 	 * Get the index into the agi hash table for the
19581da177e4SLinus Torvalds 	 * list this inode will go on.
19591da177e4SLinus Torvalds 	 */
19601da177e4SLinus Torvalds 	agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
19611da177e4SLinus Torvalds 	ASSERT(agino != 0);
19621da177e4SLinus Torvalds 	bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
19631da177e4SLinus Torvalds 	ASSERT(agi->agi_unlinked[bucket_index]);
196416259e7dSChristoph Hellwig 	ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
19651da177e4SLinus Torvalds 
196669ef921bSChristoph Hellwig 	if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
19671da177e4SLinus Torvalds 		/*
19681da177e4SLinus Torvalds 		 * There is already another inode in the bucket we need
19691da177e4SLinus Torvalds 		 * to add ourselves to.  Add us at the front of the list.
19701da177e4SLinus Torvalds 		 * Here we put the head pointer into our next pointer,
19711da177e4SLinus Torvalds 		 * and then we fall through to point the head at us.
19721da177e4SLinus Torvalds 		 */
1973475ee413SChristoph Hellwig 		error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1974475ee413SChristoph Hellwig 				       0, 0);
1975c319b58bSVlad Apostolov 		if (error)
1976c319b58bSVlad Apostolov 			return error;
1977c319b58bSVlad Apostolov 
197869ef921bSChristoph Hellwig 		ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
19791da177e4SLinus Torvalds 		dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
198092bfc6e7SChristoph Hellwig 		offset = ip->i_imap.im_boffset +
19811da177e4SLinus Torvalds 			offsetof(xfs_dinode_t, di_next_unlinked);
19820a32c26eSDave Chinner 
19830a32c26eSDave Chinner 		/* need to recalc the inode CRC if appropriate */
19840a32c26eSDave Chinner 		xfs_dinode_calc_crc(mp, dip);
19850a32c26eSDave Chinner 
19861da177e4SLinus Torvalds 		xfs_trans_inode_buf(tp, ibp);
19871da177e4SLinus Torvalds 		xfs_trans_log_buf(tp, ibp, offset,
19881da177e4SLinus Torvalds 				  (offset + sizeof(xfs_agino_t) - 1));
19891da177e4SLinus Torvalds 		xfs_inobp_check(mp, ibp);
19901da177e4SLinus Torvalds 	}
19911da177e4SLinus Torvalds 
19921da177e4SLinus Torvalds 	/*
19931da177e4SLinus Torvalds 	 * Point the bucket head pointer at the inode being inserted.
19941da177e4SLinus Torvalds 	 */
19951da177e4SLinus Torvalds 	ASSERT(agino != 0);
199616259e7dSChristoph Hellwig 	agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
19971da177e4SLinus Torvalds 	offset = offsetof(xfs_agi_t, agi_unlinked) +
19981da177e4SLinus Torvalds 		(sizeof(xfs_agino_t) * bucket_index);
19991da177e4SLinus Torvalds 	xfs_trans_log_buf(tp, agibp, offset,
20001da177e4SLinus Torvalds 			  (offset + sizeof(xfs_agino_t) - 1));
20011da177e4SLinus Torvalds 	return 0;
20021da177e4SLinus Torvalds }
20031da177e4SLinus Torvalds 
20041da177e4SLinus Torvalds /*
20051da177e4SLinus Torvalds  * Pull the on-disk inode from the AGI unlinked list.
20061da177e4SLinus Torvalds  */
20071da177e4SLinus Torvalds STATIC int
20081da177e4SLinus Torvalds xfs_iunlink_remove(
20091da177e4SLinus Torvalds 	xfs_trans_t	*tp,
20101da177e4SLinus Torvalds 	xfs_inode_t	*ip)
20111da177e4SLinus Torvalds {
20121da177e4SLinus Torvalds 	xfs_ino_t	next_ino;
20131da177e4SLinus Torvalds 	xfs_mount_t	*mp;
20141da177e4SLinus Torvalds 	xfs_agi_t	*agi;
20151da177e4SLinus Torvalds 	xfs_dinode_t	*dip;
20161da177e4SLinus Torvalds 	xfs_buf_t	*agibp;
20171da177e4SLinus Torvalds 	xfs_buf_t	*ibp;
20181da177e4SLinus Torvalds 	xfs_agnumber_t	agno;
20191da177e4SLinus Torvalds 	xfs_agino_t	agino;
20201da177e4SLinus Torvalds 	xfs_agino_t	next_agino;
20211da177e4SLinus Torvalds 	xfs_buf_t	*last_ibp;
20226fdf8cccSNathan Scott 	xfs_dinode_t	*last_dip = NULL;
20231da177e4SLinus Torvalds 	short		bucket_index;
20246fdf8cccSNathan Scott 	int		offset, last_offset = 0;
20251da177e4SLinus Torvalds 	int		error;
20261da177e4SLinus Torvalds 
20271da177e4SLinus Torvalds 	mp = tp->t_mountp;
20281da177e4SLinus Torvalds 	agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
20291da177e4SLinus Torvalds 
20301da177e4SLinus Torvalds 	/*
20311da177e4SLinus Torvalds 	 * Get the agi buffer first.  It ensures lock ordering
20321da177e4SLinus Torvalds 	 * on the list.
20331da177e4SLinus Torvalds 	 */
20345e1be0fbSChristoph Hellwig 	error = xfs_read_agi(mp, tp, agno, &agibp);
20355e1be0fbSChristoph Hellwig 	if (error)
20361da177e4SLinus Torvalds 		return error;
20375e1be0fbSChristoph Hellwig 
20381da177e4SLinus Torvalds 	agi = XFS_BUF_TO_AGI(agibp);
20395e1be0fbSChristoph Hellwig 
20401da177e4SLinus Torvalds 	/*
20411da177e4SLinus Torvalds 	 * Get the index into the agi hash table for the
20421da177e4SLinus Torvalds 	 * list this inode will go on.
20431da177e4SLinus Torvalds 	 */
20441da177e4SLinus Torvalds 	agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
2045d2e73665SDarrick J. Wong 	if (!xfs_verify_agino(mp, agno, agino))
2046d2e73665SDarrick J. Wong 		return -EFSCORRUPTED;
20471da177e4SLinus Torvalds 	bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
2048d2e73665SDarrick J. Wong 	if (!xfs_verify_agino(mp, agno,
2049d2e73665SDarrick J. Wong 			be32_to_cpu(agi->agi_unlinked[bucket_index]))) {
2050d2e73665SDarrick J. Wong 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
2051d2e73665SDarrick J. Wong 				agi, sizeof(*agi));
2052d2e73665SDarrick J. Wong 		return -EFSCORRUPTED;
2053d2e73665SDarrick J. Wong 	}
20541da177e4SLinus Torvalds 
205516259e7dSChristoph Hellwig 	if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
20561da177e4SLinus Torvalds 		/*
2057475ee413SChristoph Hellwig 		 * We're at the head of the list.  Get the inode's on-disk
2058475ee413SChristoph Hellwig 		 * buffer to see if there is anyone after us on the list.
2059475ee413SChristoph Hellwig 		 * Only modify our next pointer if it is not already NULLAGINO.
2060475ee413SChristoph Hellwig 		 * This saves us the overhead of dealing with the buffer when
2061475ee413SChristoph Hellwig 		 * there is no need to change it.
20621da177e4SLinus Torvalds 		 */
2063475ee413SChristoph Hellwig 		error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
2064475ee413SChristoph Hellwig 				       0, 0);
20651da177e4SLinus Torvalds 		if (error) {
2066475ee413SChristoph Hellwig 			xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
20670b932cccSDave Chinner 				__func__, error);
20681da177e4SLinus Torvalds 			return error;
20691da177e4SLinus Torvalds 		}
2070347d1c01SChristoph Hellwig 		next_agino = be32_to_cpu(dip->di_next_unlinked);
20711da177e4SLinus Torvalds 		ASSERT(next_agino != 0);
20721da177e4SLinus Torvalds 		if (next_agino != NULLAGINO) {
2073347d1c01SChristoph Hellwig 			dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
207492bfc6e7SChristoph Hellwig 			offset = ip->i_imap.im_boffset +
20751da177e4SLinus Torvalds 				offsetof(xfs_dinode_t, di_next_unlinked);
20760a32c26eSDave Chinner 
20770a32c26eSDave Chinner 			/* need to recalc the inode CRC if appropriate */
20780a32c26eSDave Chinner 			xfs_dinode_calc_crc(mp, dip);
20790a32c26eSDave Chinner 
20801da177e4SLinus Torvalds 			xfs_trans_inode_buf(tp, ibp);
20811da177e4SLinus Torvalds 			xfs_trans_log_buf(tp, ibp, offset,
20821da177e4SLinus Torvalds 					  (offset + sizeof(xfs_agino_t) - 1));
20831da177e4SLinus Torvalds 			xfs_inobp_check(mp, ibp);
20841da177e4SLinus Torvalds 		} else {
20851da177e4SLinus Torvalds 			xfs_trans_brelse(tp, ibp);
20861da177e4SLinus Torvalds 		}
20871da177e4SLinus Torvalds 		/*
20881da177e4SLinus Torvalds 		 * Point the bucket head pointer at the next inode.
20891da177e4SLinus Torvalds 		 */
20901da177e4SLinus Torvalds 		ASSERT(next_agino != 0);
20911da177e4SLinus Torvalds 		ASSERT(next_agino != agino);
209216259e7dSChristoph Hellwig 		agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
20931da177e4SLinus Torvalds 		offset = offsetof(xfs_agi_t, agi_unlinked) +
20941da177e4SLinus Torvalds 			(sizeof(xfs_agino_t) * bucket_index);
20951da177e4SLinus Torvalds 		xfs_trans_log_buf(tp, agibp, offset,
20961da177e4SLinus Torvalds 				  (offset + sizeof(xfs_agino_t) - 1));
20971da177e4SLinus Torvalds 	} else {
20981da177e4SLinus Torvalds 		/*
20991da177e4SLinus Torvalds 		 * We need to search the list for the inode being freed.
21001da177e4SLinus Torvalds 		 */
210116259e7dSChristoph Hellwig 		next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
21021da177e4SLinus Torvalds 		last_ibp = NULL;
21031da177e4SLinus Torvalds 		while (next_agino != agino) {
2104129dbc9aSChristoph Hellwig 			struct xfs_imap	imap;
2105129dbc9aSChristoph Hellwig 
2106129dbc9aSChristoph Hellwig 			if (last_ibp)
21071da177e4SLinus Torvalds 				xfs_trans_brelse(tp, last_ibp);
2108129dbc9aSChristoph Hellwig 
2109129dbc9aSChristoph Hellwig 			imap.im_blkno = 0;
21101da177e4SLinus Torvalds 			next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
2111129dbc9aSChristoph Hellwig 
2112129dbc9aSChristoph Hellwig 			error = xfs_imap(mp, tp, next_ino, &imap, 0);
21131da177e4SLinus Torvalds 			if (error) {
21140b932cccSDave Chinner 				xfs_warn(mp,
2115129dbc9aSChristoph Hellwig 	"%s: xfs_imap returned error %d.",
21160b932cccSDave Chinner 					 __func__, error);
21171da177e4SLinus Torvalds 				return error;
21181da177e4SLinus Torvalds 			}
2119129dbc9aSChristoph Hellwig 
2120129dbc9aSChristoph Hellwig 			error = xfs_imap_to_bp(mp, tp, &imap, &last_dip,
2121129dbc9aSChristoph Hellwig 					       &last_ibp, 0, 0);
2122129dbc9aSChristoph Hellwig 			if (error) {
2123129dbc9aSChristoph Hellwig 				xfs_warn(mp,
2124129dbc9aSChristoph Hellwig 	"%s: xfs_imap_to_bp returned error %d.",
2125129dbc9aSChristoph Hellwig 					__func__, error);
2126129dbc9aSChristoph Hellwig 				return error;
2127129dbc9aSChristoph Hellwig 			}
2128129dbc9aSChristoph Hellwig 
2129129dbc9aSChristoph Hellwig 			last_offset = imap.im_boffset;
2130347d1c01SChristoph Hellwig 			next_agino = be32_to_cpu(last_dip->di_next_unlinked);
2131d2e73665SDarrick J. Wong 			if (!xfs_verify_agino(mp, agno, next_agino)) {
2132d2e73665SDarrick J. Wong 				XFS_CORRUPTION_ERROR(__func__,
2133d2e73665SDarrick J. Wong 						XFS_ERRLEVEL_LOW, mp,
2134d2e73665SDarrick J. Wong 						last_dip, sizeof(*last_dip));
2135d2e73665SDarrick J. Wong 				return -EFSCORRUPTED;
2136d2e73665SDarrick J. Wong 			}
21371da177e4SLinus Torvalds 		}
2138475ee413SChristoph Hellwig 
21391da177e4SLinus Torvalds 		/*
2140475ee413SChristoph Hellwig 		 * Now last_ibp points to the buffer previous to us on the
2141475ee413SChristoph Hellwig 		 * unlinked list.  Pull us from the list.
21421da177e4SLinus Torvalds 		 */
2143475ee413SChristoph Hellwig 		error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
2144475ee413SChristoph Hellwig 				       0, 0);
21451da177e4SLinus Torvalds 		if (error) {
2146475ee413SChristoph Hellwig 			xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.",
21470b932cccSDave Chinner 				__func__, error);
21481da177e4SLinus Torvalds 			return error;
21491da177e4SLinus Torvalds 		}
2150347d1c01SChristoph Hellwig 		next_agino = be32_to_cpu(dip->di_next_unlinked);
21511da177e4SLinus Torvalds 		ASSERT(next_agino != 0);
21521da177e4SLinus Torvalds 		ASSERT(next_agino != agino);
21531da177e4SLinus Torvalds 		if (next_agino != NULLAGINO) {
2154347d1c01SChristoph Hellwig 			dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
215592bfc6e7SChristoph Hellwig 			offset = ip->i_imap.im_boffset +
21561da177e4SLinus Torvalds 				offsetof(xfs_dinode_t, di_next_unlinked);
21570a32c26eSDave Chinner 
21580a32c26eSDave Chinner 			/* need to recalc the inode CRC if appropriate */
21590a32c26eSDave Chinner 			xfs_dinode_calc_crc(mp, dip);
21600a32c26eSDave Chinner 
21611da177e4SLinus Torvalds 			xfs_trans_inode_buf(tp, ibp);
21621da177e4SLinus Torvalds 			xfs_trans_log_buf(tp, ibp, offset,
21631da177e4SLinus Torvalds 					  (offset + sizeof(xfs_agino_t) - 1));
21641da177e4SLinus Torvalds 			xfs_inobp_check(mp, ibp);
21651da177e4SLinus Torvalds 		} else {
21661da177e4SLinus Torvalds 			xfs_trans_brelse(tp, ibp);
21671da177e4SLinus Torvalds 		}
21681da177e4SLinus Torvalds 		/*
21691da177e4SLinus Torvalds 		 * Point the previous inode on the list to the next inode.
21701da177e4SLinus Torvalds 		 */
2171347d1c01SChristoph Hellwig 		last_dip->di_next_unlinked = cpu_to_be32(next_agino);
21721da177e4SLinus Torvalds 		ASSERT(next_agino != 0);
21731da177e4SLinus Torvalds 		offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
21740a32c26eSDave Chinner 
21750a32c26eSDave Chinner 		/* need to recalc the inode CRC if appropriate */
21760a32c26eSDave Chinner 		xfs_dinode_calc_crc(mp, last_dip);
21770a32c26eSDave Chinner 
21781da177e4SLinus Torvalds 		xfs_trans_inode_buf(tp, last_ibp);
21791da177e4SLinus Torvalds 		xfs_trans_log_buf(tp, last_ibp, offset,
21801da177e4SLinus Torvalds 				  (offset + sizeof(xfs_agino_t) - 1));
21811da177e4SLinus Torvalds 		xfs_inobp_check(mp, last_ibp);
21821da177e4SLinus Torvalds 	}
21831da177e4SLinus Torvalds 	return 0;
21841da177e4SLinus Torvalds }
21851da177e4SLinus Torvalds 
21865b3eed75SDave Chinner /*
21870b8182dbSZhi Yong Wu  * A big issue when freeing the inode cluster is that we _cannot_ skip any
21885b3eed75SDave Chinner  * inodes that are in memory - they all must be marked stale and attached to
21895b3eed75SDave Chinner  * the cluster buffer.
21905b3eed75SDave Chinner  */
21912a30f36dSChandra Seetharaman STATIC int
21921da177e4SLinus Torvalds xfs_ifree_cluster(
21931da177e4SLinus Torvalds 	xfs_inode_t		*free_ip,
21941da177e4SLinus Torvalds 	xfs_trans_t		*tp,
219509b56604SBrian Foster 	struct xfs_icluster	*xic)
21961da177e4SLinus Torvalds {
21971da177e4SLinus Torvalds 	xfs_mount_t		*mp = free_ip->i_mount;
21981da177e4SLinus Torvalds 	int			blks_per_cluster;
2199982e939eSJie Liu 	int			inodes_per_cluster;
22001da177e4SLinus Torvalds 	int			nbufs;
22015b257b4aSDave Chinner 	int			i, j;
22023cdaa189SBrian Foster 	int			ioffset;
22031da177e4SLinus Torvalds 	xfs_daddr_t		blkno;
22041da177e4SLinus Torvalds 	xfs_buf_t		*bp;
22055b257b4aSDave Chinner 	xfs_inode_t		*ip;
22061da177e4SLinus Torvalds 	xfs_inode_log_item_t	*iip;
2207643c8c05SCarlos Maiolino 	struct xfs_log_item	*lip;
22085017e97dSDave Chinner 	struct xfs_perag	*pag;
220909b56604SBrian Foster 	xfs_ino_t		inum;
22101da177e4SLinus Torvalds 
221109b56604SBrian Foster 	inum = xic->first_ino;
22125017e97dSDave Chinner 	pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
2213982e939eSJie Liu 	blks_per_cluster = xfs_icluster_size_fsb(mp);
2214982e939eSJie Liu 	inodes_per_cluster = blks_per_cluster << mp->m_sb.sb_inopblog;
2215126cd105SJie Liu 	nbufs = mp->m_ialloc_blks / blks_per_cluster;
22161da177e4SLinus Torvalds 
2217982e939eSJie Liu 	for (j = 0; j < nbufs; j++, inum += inodes_per_cluster) {
221809b56604SBrian Foster 		/*
221909b56604SBrian Foster 		 * The allocation bitmap tells us which inodes of the chunk were
222009b56604SBrian Foster 		 * physically allocated. Skip the cluster if an inode falls into
222109b56604SBrian Foster 		 * a sparse region.
222209b56604SBrian Foster 		 */
22233cdaa189SBrian Foster 		ioffset = inum - xic->first_ino;
22243cdaa189SBrian Foster 		if ((xic->alloc & XFS_INOBT_MASK(ioffset)) == 0) {
22250703a8e1SDave Chinner 			ASSERT(ioffset % inodes_per_cluster == 0);
222609b56604SBrian Foster 			continue;
222709b56604SBrian Foster 		}
222809b56604SBrian Foster 
22291da177e4SLinus Torvalds 		blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
22301da177e4SLinus Torvalds 					 XFS_INO_TO_AGBNO(mp, inum));
22311da177e4SLinus Torvalds 
22321da177e4SLinus Torvalds 		/*
22335b257b4aSDave Chinner 		 * We obtain and lock the backing buffer first in the process
22345b257b4aSDave Chinner 		 * here, as we have to ensure that any dirty inode that we
22355b257b4aSDave Chinner 		 * can't get the flush lock on is attached to the buffer.
22365b257b4aSDave Chinner 		 * If we scan the in-memory inodes first, then buffer IO can
22375b257b4aSDave Chinner 		 * complete before we get a lock on it, and hence we may fail
22385b257b4aSDave Chinner 		 * to mark all the active inodes on the buffer stale.
22391da177e4SLinus Torvalds 		 */
22401da177e4SLinus Torvalds 		bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
2241b6aff29fSDave Chinner 					mp->m_bsize * blks_per_cluster,
2242b6aff29fSDave Chinner 					XBF_UNMAPPED);
22431da177e4SLinus Torvalds 
22442a30f36dSChandra Seetharaman 		if (!bp)
22452451337dSDave Chinner 			return -ENOMEM;
2246b0f539deSDave Chinner 
2247b0f539deSDave Chinner 		/*
2248b0f539deSDave Chinner 		 * This buffer may not have been correctly initialised as we
2249b0f539deSDave Chinner 		 * didn't read it from disk. That's not important because we are
2250b0f539deSDave Chinner 		 * only using to mark the buffer as stale in the log, and to
2251b0f539deSDave Chinner 		 * attach stale cached inodes on it. That means it will never be
2252b0f539deSDave Chinner 		 * dispatched for IO. If it is, we want to know about it, and we
2253b0f539deSDave Chinner 		 * want it to fail. We can acheive this by adding a write
2254b0f539deSDave Chinner 		 * verifier to the buffer.
2255b0f539deSDave Chinner 		 */
22561813dd64SDave Chinner 		 bp->b_ops = &xfs_inode_buf_ops;
2257b0f539deSDave Chinner 
22585b257b4aSDave Chinner 		/*
22595b257b4aSDave Chinner 		 * Walk the inodes already attached to the buffer and mark them
22605b257b4aSDave Chinner 		 * stale. These will all have the flush locks held, so an
22615b3eed75SDave Chinner 		 * in-memory inode walk can't lock them. By marking them all
22625b3eed75SDave Chinner 		 * stale first, we will not attempt to lock them in the loop
22635b3eed75SDave Chinner 		 * below as the XFS_ISTALE flag will be set.
22645b257b4aSDave Chinner 		 */
2265643c8c05SCarlos Maiolino 		list_for_each_entry(lip, &bp->b_li_list, li_bio_list) {
22661da177e4SLinus Torvalds 			if (lip->li_type == XFS_LI_INODE) {
22671da177e4SLinus Torvalds 				iip = (xfs_inode_log_item_t *)lip;
22681da177e4SLinus Torvalds 				ASSERT(iip->ili_logged == 1);
2269ca30b2a7SChristoph Hellwig 				lip->li_cb = xfs_istale_done;
22707b2e2a31SDavid Chinner 				xfs_trans_ail_copy_lsn(mp->m_ail,
22717b2e2a31SDavid Chinner 							&iip->ili_flush_lsn,
22727b2e2a31SDavid Chinner 							&iip->ili_item.li_lsn);
2273e5ffd2bbSDavid Chinner 				xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
22741da177e4SLinus Torvalds 			}
22751da177e4SLinus Torvalds 		}
22761da177e4SLinus Torvalds 
22775b3eed75SDave Chinner 
22785b257b4aSDave Chinner 		/*
22795b257b4aSDave Chinner 		 * For each inode in memory attempt to add it to the inode
22805b257b4aSDave Chinner 		 * buffer and set it up for being staled on buffer IO
22815b257b4aSDave Chinner 		 * completion.  This is safe as we've locked out tail pushing
22825b257b4aSDave Chinner 		 * and flushing by locking the buffer.
22835b257b4aSDave Chinner 		 *
22845b257b4aSDave Chinner 		 * We have already marked every inode that was part of a
22855b257b4aSDave Chinner 		 * transaction stale above, which means there is no point in
22865b257b4aSDave Chinner 		 * even trying to lock them.
22875b257b4aSDave Chinner 		 */
2288982e939eSJie Liu 		for (i = 0; i < inodes_per_cluster; i++) {
22895b3eed75SDave Chinner retry:
22901a3e8f3dSDave Chinner 			rcu_read_lock();
22915b257b4aSDave Chinner 			ip = radix_tree_lookup(&pag->pag_ici_root,
22925b257b4aSDave Chinner 					XFS_INO_TO_AGINO(mp, (inum + i)));
22931da177e4SLinus Torvalds 
22941a3e8f3dSDave Chinner 			/* Inode not in memory, nothing to do */
22951a3e8f3dSDave Chinner 			if (!ip) {
22961a3e8f3dSDave Chinner 				rcu_read_unlock();
22975b257b4aSDave Chinner 				continue;
22985b257b4aSDave Chinner 			}
22995b257b4aSDave Chinner 
23005b3eed75SDave Chinner 			/*
23011a3e8f3dSDave Chinner 			 * because this is an RCU protected lookup, we could
23021a3e8f3dSDave Chinner 			 * find a recently freed or even reallocated inode
23031a3e8f3dSDave Chinner 			 * during the lookup. We need to check under the
23041a3e8f3dSDave Chinner 			 * i_flags_lock for a valid inode here. Skip it if it
23051a3e8f3dSDave Chinner 			 * is not valid, the wrong inode or stale.
23061a3e8f3dSDave Chinner 			 */
23071a3e8f3dSDave Chinner 			spin_lock(&ip->i_flags_lock);
23081a3e8f3dSDave Chinner 			if (ip->i_ino != inum + i ||
23091a3e8f3dSDave Chinner 			    __xfs_iflags_test(ip, XFS_ISTALE)) {
23101a3e8f3dSDave Chinner 				spin_unlock(&ip->i_flags_lock);
23111a3e8f3dSDave Chinner 				rcu_read_unlock();
23121a3e8f3dSDave Chinner 				continue;
23131a3e8f3dSDave Chinner 			}
23141a3e8f3dSDave Chinner 			spin_unlock(&ip->i_flags_lock);
23151a3e8f3dSDave Chinner 
23161a3e8f3dSDave Chinner 			/*
23175b3eed75SDave Chinner 			 * Don't try to lock/unlock the current inode, but we
23185b3eed75SDave Chinner 			 * _cannot_ skip the other inodes that we did not find
23195b3eed75SDave Chinner 			 * in the list attached to the buffer and are not
23205b3eed75SDave Chinner 			 * already marked stale. If we can't lock it, back off
23215b3eed75SDave Chinner 			 * and retry.
23225b3eed75SDave Chinner 			 */
2323f2e9ad21SOmar Sandoval 			if (ip != free_ip) {
2324f2e9ad21SOmar Sandoval 				if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
23251a3e8f3dSDave Chinner 					rcu_read_unlock();
23265b3eed75SDave Chinner 					delay(1);
23275b3eed75SDave Chinner 					goto retry;
23285b257b4aSDave Chinner 				}
2329f2e9ad21SOmar Sandoval 
2330f2e9ad21SOmar Sandoval 				/*
2331f2e9ad21SOmar Sandoval 				 * Check the inode number again in case we're
2332f2e9ad21SOmar Sandoval 				 * racing with freeing in xfs_reclaim_inode().
2333f2e9ad21SOmar Sandoval 				 * See the comments in that function for more
2334f2e9ad21SOmar Sandoval 				 * information as to why the initial check is
2335f2e9ad21SOmar Sandoval 				 * not sufficient.
2336f2e9ad21SOmar Sandoval 				 */
2337f2e9ad21SOmar Sandoval 				if (ip->i_ino != inum + i) {
2338f2e9ad21SOmar Sandoval 					xfs_iunlock(ip, XFS_ILOCK_EXCL);
2339962cc1adSDarrick J. Wong 					rcu_read_unlock();
2340f2e9ad21SOmar Sandoval 					continue;
2341f2e9ad21SOmar Sandoval 				}
2342f2e9ad21SOmar Sandoval 			}
23431a3e8f3dSDave Chinner 			rcu_read_unlock();
23445b257b4aSDave Chinner 
23455b3eed75SDave Chinner 			xfs_iflock(ip);
23465b257b4aSDave Chinner 			xfs_iflags_set(ip, XFS_ISTALE);
23475b257b4aSDave Chinner 
23485b3eed75SDave Chinner 			/*
23495b3eed75SDave Chinner 			 * we don't need to attach clean inodes or those only
23505b3eed75SDave Chinner 			 * with unlogged changes (which we throw away, anyway).
23515b3eed75SDave Chinner 			 */
23525b257b4aSDave Chinner 			iip = ip->i_itemp;
23535b3eed75SDave Chinner 			if (!iip || xfs_inode_clean(ip)) {
23545b257b4aSDave Chinner 				ASSERT(ip != free_ip);
23551da177e4SLinus Torvalds 				xfs_ifunlock(ip);
23561da177e4SLinus Torvalds 				xfs_iunlock(ip, XFS_ILOCK_EXCL);
23571da177e4SLinus Torvalds 				continue;
23581da177e4SLinus Torvalds 			}
23591da177e4SLinus Torvalds 
2360f5d8d5c4SChristoph Hellwig 			iip->ili_last_fields = iip->ili_fields;
2361f5d8d5c4SChristoph Hellwig 			iip->ili_fields = 0;
2362fc0561ceSDave Chinner 			iip->ili_fsync_fields = 0;
23631da177e4SLinus Torvalds 			iip->ili_logged = 1;
23647b2e2a31SDavid Chinner 			xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
23657b2e2a31SDavid Chinner 						&iip->ili_item.li_lsn);
23661da177e4SLinus Torvalds 
2367ca30b2a7SChristoph Hellwig 			xfs_buf_attach_iodone(bp, xfs_istale_done,
2368ca30b2a7SChristoph Hellwig 						  &iip->ili_item);
23695b257b4aSDave Chinner 
23705b257b4aSDave Chinner 			if (ip != free_ip)
23711da177e4SLinus Torvalds 				xfs_iunlock(ip, XFS_ILOCK_EXCL);
23721da177e4SLinus Torvalds 		}
23731da177e4SLinus Torvalds 
23741da177e4SLinus Torvalds 		xfs_trans_stale_inode_buf(tp, bp);
23751da177e4SLinus Torvalds 		xfs_trans_binval(tp, bp);
23761da177e4SLinus Torvalds 	}
23771da177e4SLinus Torvalds 
23785017e97dSDave Chinner 	xfs_perag_put(pag);
23792a30f36dSChandra Seetharaman 	return 0;
23801da177e4SLinus Torvalds }
23811da177e4SLinus Torvalds 
23821da177e4SLinus Torvalds /*
238398c4f78dSDarrick J. Wong  * Free any local-format buffers sitting around before we reset to
238498c4f78dSDarrick J. Wong  * extents format.
238598c4f78dSDarrick J. Wong  */
238698c4f78dSDarrick J. Wong static inline void
238798c4f78dSDarrick J. Wong xfs_ifree_local_data(
238898c4f78dSDarrick J. Wong 	struct xfs_inode	*ip,
238998c4f78dSDarrick J. Wong 	int			whichfork)
239098c4f78dSDarrick J. Wong {
239198c4f78dSDarrick J. Wong 	struct xfs_ifork	*ifp;
239298c4f78dSDarrick J. Wong 
239398c4f78dSDarrick J. Wong 	if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
239498c4f78dSDarrick J. Wong 		return;
239598c4f78dSDarrick J. Wong 
239698c4f78dSDarrick J. Wong 	ifp = XFS_IFORK_PTR(ip, whichfork);
239798c4f78dSDarrick J. Wong 	xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
239898c4f78dSDarrick J. Wong }
239998c4f78dSDarrick J. Wong 
240098c4f78dSDarrick J. Wong /*
24011da177e4SLinus Torvalds  * This is called to return an inode to the inode free list.
24021da177e4SLinus Torvalds  * The inode should already be truncated to 0 length and have
24031da177e4SLinus Torvalds  * no pages associated with it.  This routine also assumes that
24041da177e4SLinus Torvalds  * the inode is already a part of the transaction.
24051da177e4SLinus Torvalds  *
24061da177e4SLinus Torvalds  * The on-disk copy of the inode will have been added to the list
24071da177e4SLinus Torvalds  * of unlinked inodes in the AGI. We need to remove the inode from
24081da177e4SLinus Torvalds  * that list atomically with respect to freeing it here.
24091da177e4SLinus Torvalds  */
24101da177e4SLinus Torvalds int
24111da177e4SLinus Torvalds xfs_ifree(
24120e0417f3SBrian Foster 	struct xfs_trans	*tp,
24130e0417f3SBrian Foster 	struct xfs_inode	*ip)
24141da177e4SLinus Torvalds {
24151da177e4SLinus Torvalds 	int			error;
241609b56604SBrian Foster 	struct xfs_icluster	xic = { 0 };
24171da177e4SLinus Torvalds 
2418579aa9caSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
241954d7b5c1SDave Chinner 	ASSERT(VFS_I(ip)->i_nlink == 0);
24201da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_nextents == 0);
24211da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_anextents == 0);
2422c19b3b05SDave Chinner 	ASSERT(ip->i_d.di_size == 0 || !S_ISREG(VFS_I(ip)->i_mode));
24231da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_nblocks == 0);
24241da177e4SLinus Torvalds 
24251da177e4SLinus Torvalds 	/*
24261da177e4SLinus Torvalds 	 * Pull the on-disk inode from the AGI unlinked list.
24271da177e4SLinus Torvalds 	 */
24281da177e4SLinus Torvalds 	error = xfs_iunlink_remove(tp, ip);
24291baaed8fSDave Chinner 	if (error)
24301da177e4SLinus Torvalds 		return error;
24311da177e4SLinus Torvalds 
24320e0417f3SBrian Foster 	error = xfs_difree(tp, ip->i_ino, &xic);
24331baaed8fSDave Chinner 	if (error)
24341da177e4SLinus Torvalds 		return error;
24351baaed8fSDave Chinner 
243698c4f78dSDarrick J. Wong 	xfs_ifree_local_data(ip, XFS_DATA_FORK);
243798c4f78dSDarrick J. Wong 	xfs_ifree_local_data(ip, XFS_ATTR_FORK);
243898c4f78dSDarrick J. Wong 
2439c19b3b05SDave Chinner 	VFS_I(ip)->i_mode = 0;		/* mark incore inode as free */
24401da177e4SLinus Torvalds 	ip->i_d.di_flags = 0;
2441beaae8cdSDarrick J. Wong 	ip->i_d.di_flags2 = 0;
24421da177e4SLinus Torvalds 	ip->i_d.di_dmevmask = 0;
24431da177e4SLinus Torvalds 	ip->i_d.di_forkoff = 0;		/* mark the attr fork not in use */
24441da177e4SLinus Torvalds 	ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
24451da177e4SLinus Torvalds 	ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
2446dc1baa71SEric Sandeen 
2447dc1baa71SEric Sandeen 	/* Don't attempt to replay owner changes for a deleted inode */
2448dc1baa71SEric Sandeen 	ip->i_itemp->ili_fields &= ~(XFS_ILOG_AOWNER|XFS_ILOG_DOWNER);
2449dc1baa71SEric Sandeen 
24501da177e4SLinus Torvalds 	/*
24511da177e4SLinus Torvalds 	 * Bump the generation count so no one will be confused
24521da177e4SLinus Torvalds 	 * by reincarnations of this inode.
24531da177e4SLinus Torvalds 	 */
24549e9a2674SDave Chinner 	VFS_I(ip)->i_generation++;
24551da177e4SLinus Torvalds 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
24561da177e4SLinus Torvalds 
245709b56604SBrian Foster 	if (xic.deleted)
245809b56604SBrian Foster 		error = xfs_ifree_cluster(ip, tp, &xic);
24591da177e4SLinus Torvalds 
24602a30f36dSChandra Seetharaman 	return error;
24611da177e4SLinus Torvalds }
24621da177e4SLinus Torvalds 
24631da177e4SLinus Torvalds /*
246460ec6783SChristoph Hellwig  * This is called to unpin an inode.  The caller must have the inode locked
246560ec6783SChristoph Hellwig  * in at least shared mode so that the buffer cannot be subsequently pinned
246660ec6783SChristoph Hellwig  * once someone is waiting for it to be unpinned.
24671da177e4SLinus Torvalds  */
246860ec6783SChristoph Hellwig static void
2469f392e631SChristoph Hellwig xfs_iunpin(
247060ec6783SChristoph Hellwig 	struct xfs_inode	*ip)
2471a3f74ffbSDavid Chinner {
2472579aa9caSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
2473a3f74ffbSDavid Chinner 
24744aaf15d1SDave Chinner 	trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
24754aaf15d1SDave Chinner 
2476a3f74ffbSDavid Chinner 	/* Give the log a push to start the unpinning I/O */
2477656de4ffSChristoph Hellwig 	xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0, NULL);
2478a14a348bSChristoph Hellwig 
2479a3f74ffbSDavid Chinner }
2480a3f74ffbSDavid Chinner 
2481f392e631SChristoph Hellwig static void
2482f392e631SChristoph Hellwig __xfs_iunpin_wait(
2483f392e631SChristoph Hellwig 	struct xfs_inode	*ip)
2484f392e631SChristoph Hellwig {
2485f392e631SChristoph Hellwig 	wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
2486f392e631SChristoph Hellwig 	DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
2487f392e631SChristoph Hellwig 
2488f392e631SChristoph Hellwig 	xfs_iunpin(ip);
2489f392e631SChristoph Hellwig 
2490f392e631SChristoph Hellwig 	do {
249121417136SIngo Molnar 		prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
2492f392e631SChristoph Hellwig 		if (xfs_ipincount(ip))
2493f392e631SChristoph Hellwig 			io_schedule();
2494f392e631SChristoph Hellwig 	} while (xfs_ipincount(ip));
249521417136SIngo Molnar 	finish_wait(wq, &wait.wq_entry);
2496f392e631SChristoph Hellwig }
2497f392e631SChristoph Hellwig 
2498777df5afSDave Chinner void
24991da177e4SLinus Torvalds xfs_iunpin_wait(
250060ec6783SChristoph Hellwig 	struct xfs_inode	*ip)
25011da177e4SLinus Torvalds {
2502f392e631SChristoph Hellwig 	if (xfs_ipincount(ip))
2503f392e631SChristoph Hellwig 		__xfs_iunpin_wait(ip);
25041da177e4SLinus Torvalds }
25051da177e4SLinus Torvalds 
250627320369SDave Chinner /*
250727320369SDave Chinner  * Removing an inode from the namespace involves removing the directory entry
250827320369SDave Chinner  * and dropping the link count on the inode. Removing the directory entry can
250927320369SDave Chinner  * result in locking an AGF (directory blocks were freed) and removing a link
251027320369SDave Chinner  * count can result in placing the inode on an unlinked list which results in
251127320369SDave Chinner  * locking an AGI.
251227320369SDave Chinner  *
251327320369SDave Chinner  * The big problem here is that we have an ordering constraint on AGF and AGI
251427320369SDave Chinner  * locking - inode allocation locks the AGI, then can allocate a new extent for
251527320369SDave Chinner  * new inodes, locking the AGF after the AGI. Similarly, freeing the inode
251627320369SDave Chinner  * removes the inode from the unlinked list, requiring that we lock the AGI
251727320369SDave Chinner  * first, and then freeing the inode can result in an inode chunk being freed
251827320369SDave Chinner  * and hence freeing disk space requiring that we lock an AGF.
251927320369SDave Chinner  *
252027320369SDave Chinner  * Hence the ordering that is imposed by other parts of the code is AGI before
252127320369SDave Chinner  * AGF. This means we cannot remove the directory entry before we drop the inode
252227320369SDave Chinner  * reference count and put it on the unlinked list as this results in a lock
252327320369SDave Chinner  * order of AGF then AGI, and this can deadlock against inode allocation and
252427320369SDave Chinner  * freeing. Therefore we must drop the link counts before we remove the
252527320369SDave Chinner  * directory entry.
252627320369SDave Chinner  *
252727320369SDave Chinner  * This is still safe from a transactional point of view - it is not until we
2528310a75a3SDarrick J. Wong  * get to xfs_defer_finish() that we have the possibility of multiple
252927320369SDave Chinner  * transactions in this operation. Hence as long as we remove the directory
253027320369SDave Chinner  * entry and drop the link count in the first transaction of the remove
253127320369SDave Chinner  * operation, there are no transactional constraints on the ordering here.
253227320369SDave Chinner  */
2533c24b5dfaSDave Chinner int
2534c24b5dfaSDave Chinner xfs_remove(
2535c24b5dfaSDave Chinner 	xfs_inode_t             *dp,
2536c24b5dfaSDave Chinner 	struct xfs_name		*name,
2537c24b5dfaSDave Chinner 	xfs_inode_t		*ip)
2538c24b5dfaSDave Chinner {
2539c24b5dfaSDave Chinner 	xfs_mount_t		*mp = dp->i_mount;
2540c24b5dfaSDave Chinner 	xfs_trans_t             *tp = NULL;
2541c19b3b05SDave Chinner 	int			is_dir = S_ISDIR(VFS_I(ip)->i_mode);
2542c24b5dfaSDave Chinner 	int                     error = 0;
2543c24b5dfaSDave Chinner 	uint			resblks;
2544c24b5dfaSDave Chinner 
2545c24b5dfaSDave Chinner 	trace_xfs_remove(dp, name);
2546c24b5dfaSDave Chinner 
2547c24b5dfaSDave Chinner 	if (XFS_FORCED_SHUTDOWN(mp))
25482451337dSDave Chinner 		return -EIO;
2549c24b5dfaSDave Chinner 
2550c14cfccaSDarrick J. Wong 	error = xfs_qm_dqattach(dp);
2551c24b5dfaSDave Chinner 	if (error)
2552c24b5dfaSDave Chinner 		goto std_return;
2553c24b5dfaSDave Chinner 
2554c14cfccaSDarrick J. Wong 	error = xfs_qm_dqattach(ip);
2555c24b5dfaSDave Chinner 	if (error)
2556c24b5dfaSDave Chinner 		goto std_return;
2557c24b5dfaSDave Chinner 
2558c24b5dfaSDave Chinner 	/*
2559c24b5dfaSDave Chinner 	 * We try to get the real space reservation first,
2560c24b5dfaSDave Chinner 	 * allowing for directory btree deletion(s) implying
2561c24b5dfaSDave Chinner 	 * possible bmap insert(s).  If we can't get the space
2562c24b5dfaSDave Chinner 	 * reservation then we use 0 instead, and avoid the bmap
2563c24b5dfaSDave Chinner 	 * btree insert(s) in the directory code by, if the bmap
2564c24b5dfaSDave Chinner 	 * insert tries to happen, instead trimming the LAST
2565c24b5dfaSDave Chinner 	 * block from the directory.
2566c24b5dfaSDave Chinner 	 */
2567c24b5dfaSDave Chinner 	resblks = XFS_REMOVE_SPACE_RES(mp);
2568253f4911SChristoph Hellwig 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_remove, resblks, 0, 0, &tp);
25692451337dSDave Chinner 	if (error == -ENOSPC) {
2570c24b5dfaSDave Chinner 		resblks = 0;
2571253f4911SChristoph Hellwig 		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_remove, 0, 0, 0,
2572253f4911SChristoph Hellwig 				&tp);
2573c24b5dfaSDave Chinner 	}
2574c24b5dfaSDave Chinner 	if (error) {
25752451337dSDave Chinner 		ASSERT(error != -ENOSPC);
2576253f4911SChristoph Hellwig 		goto std_return;
2577c24b5dfaSDave Chinner 	}
2578c24b5dfaSDave Chinner 
25797c2d238aSDarrick J. Wong 	xfs_lock_two_inodes(dp, XFS_ILOCK_EXCL, ip, XFS_ILOCK_EXCL);
2580c24b5dfaSDave Chinner 
258165523218SChristoph Hellwig 	xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2582c24b5dfaSDave Chinner 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2583c24b5dfaSDave Chinner 
2584c24b5dfaSDave Chinner 	/*
2585c24b5dfaSDave Chinner 	 * If we're removing a directory perform some additional validation.
2586c24b5dfaSDave Chinner 	 */
2587c24b5dfaSDave Chinner 	if (is_dir) {
258854d7b5c1SDave Chinner 		ASSERT(VFS_I(ip)->i_nlink >= 2);
258954d7b5c1SDave Chinner 		if (VFS_I(ip)->i_nlink != 2) {
25902451337dSDave Chinner 			error = -ENOTEMPTY;
2591c24b5dfaSDave Chinner 			goto out_trans_cancel;
2592c24b5dfaSDave Chinner 		}
2593c24b5dfaSDave Chinner 		if (!xfs_dir_isempty(ip)) {
25942451337dSDave Chinner 			error = -ENOTEMPTY;
2595c24b5dfaSDave Chinner 			goto out_trans_cancel;
2596c24b5dfaSDave Chinner 		}
2597c24b5dfaSDave Chinner 
259827320369SDave Chinner 		/* Drop the link from ip's "..".  */
2599c24b5dfaSDave Chinner 		error = xfs_droplink(tp, dp);
2600c24b5dfaSDave Chinner 		if (error)
260127320369SDave Chinner 			goto out_trans_cancel;
2602c24b5dfaSDave Chinner 
260327320369SDave Chinner 		/* Drop the "." link from ip to self.  */
2604c24b5dfaSDave Chinner 		error = xfs_droplink(tp, ip);
2605c24b5dfaSDave Chinner 		if (error)
260627320369SDave Chinner 			goto out_trans_cancel;
2607c24b5dfaSDave Chinner 	} else {
2608c24b5dfaSDave Chinner 		/*
2609c24b5dfaSDave Chinner 		 * When removing a non-directory we need to log the parent
2610c24b5dfaSDave Chinner 		 * inode here.  For a directory this is done implicitly
2611c24b5dfaSDave Chinner 		 * by the xfs_droplink call for the ".." entry.
2612c24b5dfaSDave Chinner 		 */
2613c24b5dfaSDave Chinner 		xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2614c24b5dfaSDave Chinner 	}
261527320369SDave Chinner 	xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2616c24b5dfaSDave Chinner 
261727320369SDave Chinner 	/* Drop the link from dp to ip. */
2618c24b5dfaSDave Chinner 	error = xfs_droplink(tp, ip);
2619c24b5dfaSDave Chinner 	if (error)
262027320369SDave Chinner 		goto out_trans_cancel;
2621c24b5dfaSDave Chinner 
2622381eee69SBrian Foster 	error = xfs_dir_removename(tp, dp, name, ip->i_ino, resblks);
262327320369SDave Chinner 	if (error) {
26242451337dSDave Chinner 		ASSERT(error != -ENOENT);
2625*c8eac49eSBrian Foster 		goto out_trans_cancel;
262627320369SDave Chinner 	}
262727320369SDave Chinner 
2628c24b5dfaSDave Chinner 	/*
2629c24b5dfaSDave Chinner 	 * If this is a synchronous mount, make sure that the
2630c24b5dfaSDave Chinner 	 * remove transaction goes to disk before returning to
2631c24b5dfaSDave Chinner 	 * the user.
2632c24b5dfaSDave Chinner 	 */
2633c24b5dfaSDave Chinner 	if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
2634c24b5dfaSDave Chinner 		xfs_trans_set_sync(tp);
2635c24b5dfaSDave Chinner 
263670393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
2637c24b5dfaSDave Chinner 	if (error)
2638c24b5dfaSDave Chinner 		goto std_return;
2639c24b5dfaSDave Chinner 
26402cd2ef6aSChristoph Hellwig 	if (is_dir && xfs_inode_is_filestream(ip))
2641c24b5dfaSDave Chinner 		xfs_filestream_deassociate(ip);
2642c24b5dfaSDave Chinner 
2643c24b5dfaSDave Chinner 	return 0;
2644c24b5dfaSDave Chinner 
2645c24b5dfaSDave Chinner  out_trans_cancel:
26464906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
2647c24b5dfaSDave Chinner  std_return:
2648c24b5dfaSDave Chinner 	return error;
2649c24b5dfaSDave Chinner }
2650c24b5dfaSDave Chinner 
2651f6bba201SDave Chinner /*
2652f6bba201SDave Chinner  * Enter all inodes for a rename transaction into a sorted array.
2653f6bba201SDave Chinner  */
265495afcf5cSDave Chinner #define __XFS_SORT_INODES	5
2655f6bba201SDave Chinner STATIC void
2656f6bba201SDave Chinner xfs_sort_for_rename(
265795afcf5cSDave Chinner 	struct xfs_inode	*dp1,	/* in: old (source) directory inode */
265895afcf5cSDave Chinner 	struct xfs_inode	*dp2,	/* in: new (target) directory inode */
265995afcf5cSDave Chinner 	struct xfs_inode	*ip1,	/* in: inode of old entry */
266095afcf5cSDave Chinner 	struct xfs_inode	*ip2,	/* in: inode of new entry */
266195afcf5cSDave Chinner 	struct xfs_inode	*wip,	/* in: whiteout inode */
266295afcf5cSDave Chinner 	struct xfs_inode	**i_tab,/* out: sorted array of inodes */
266395afcf5cSDave Chinner 	int			*num_inodes)  /* in/out: inodes in array */
2664f6bba201SDave Chinner {
2665f6bba201SDave Chinner 	int			i, j;
2666f6bba201SDave Chinner 
266795afcf5cSDave Chinner 	ASSERT(*num_inodes == __XFS_SORT_INODES);
266895afcf5cSDave Chinner 	memset(i_tab, 0, *num_inodes * sizeof(struct xfs_inode *));
266995afcf5cSDave Chinner 
2670f6bba201SDave Chinner 	/*
2671f6bba201SDave Chinner 	 * i_tab contains a list of pointers to inodes.  We initialize
2672f6bba201SDave Chinner 	 * the table here & we'll sort it.  We will then use it to
2673f6bba201SDave Chinner 	 * order the acquisition of the inode locks.
2674f6bba201SDave Chinner 	 *
2675f6bba201SDave Chinner 	 * Note that the table may contain duplicates.  e.g., dp1 == dp2.
2676f6bba201SDave Chinner 	 */
267795afcf5cSDave Chinner 	i = 0;
267895afcf5cSDave Chinner 	i_tab[i++] = dp1;
267995afcf5cSDave Chinner 	i_tab[i++] = dp2;
268095afcf5cSDave Chinner 	i_tab[i++] = ip1;
268195afcf5cSDave Chinner 	if (ip2)
268295afcf5cSDave Chinner 		i_tab[i++] = ip2;
268395afcf5cSDave Chinner 	if (wip)
268495afcf5cSDave Chinner 		i_tab[i++] = wip;
268595afcf5cSDave Chinner 	*num_inodes = i;
2686f6bba201SDave Chinner 
2687f6bba201SDave Chinner 	/*
2688f6bba201SDave Chinner 	 * Sort the elements via bubble sort.  (Remember, there are at
268995afcf5cSDave Chinner 	 * most 5 elements to sort, so this is adequate.)
2690f6bba201SDave Chinner 	 */
2691f6bba201SDave Chinner 	for (i = 0; i < *num_inodes; i++) {
2692f6bba201SDave Chinner 		for (j = 1; j < *num_inodes; j++) {
2693f6bba201SDave Chinner 			if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) {
269495afcf5cSDave Chinner 				struct xfs_inode *temp = i_tab[j];
2695f6bba201SDave Chinner 				i_tab[j] = i_tab[j-1];
2696f6bba201SDave Chinner 				i_tab[j-1] = temp;
2697f6bba201SDave Chinner 			}
2698f6bba201SDave Chinner 		}
2699f6bba201SDave Chinner 	}
2700f6bba201SDave Chinner }
2701f6bba201SDave Chinner 
2702310606b0SDave Chinner static int
2703310606b0SDave Chinner xfs_finish_rename(
2704c9cfdb38SBrian Foster 	struct xfs_trans	*tp)
2705310606b0SDave Chinner {
2706310606b0SDave Chinner 	/*
2707310606b0SDave Chinner 	 * If this is a synchronous mount, make sure that the rename transaction
2708310606b0SDave Chinner 	 * goes to disk before returning to the user.
2709310606b0SDave Chinner 	 */
2710310606b0SDave Chinner 	if (tp->t_mountp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
2711310606b0SDave Chinner 		xfs_trans_set_sync(tp);
2712310606b0SDave Chinner 
271370393313SChristoph Hellwig 	return xfs_trans_commit(tp);
2714310606b0SDave Chinner }
2715310606b0SDave Chinner 
2716f6bba201SDave Chinner /*
2717d31a1825SCarlos Maiolino  * xfs_cross_rename()
2718d31a1825SCarlos Maiolino  *
2719d31a1825SCarlos Maiolino  * responsible for handling RENAME_EXCHANGE flag in renameat2() sytemcall
2720d31a1825SCarlos Maiolino  */
2721d31a1825SCarlos Maiolino STATIC int
2722d31a1825SCarlos Maiolino xfs_cross_rename(
2723d31a1825SCarlos Maiolino 	struct xfs_trans	*tp,
2724d31a1825SCarlos Maiolino 	struct xfs_inode	*dp1,
2725d31a1825SCarlos Maiolino 	struct xfs_name		*name1,
2726d31a1825SCarlos Maiolino 	struct xfs_inode	*ip1,
2727d31a1825SCarlos Maiolino 	struct xfs_inode	*dp2,
2728d31a1825SCarlos Maiolino 	struct xfs_name		*name2,
2729d31a1825SCarlos Maiolino 	struct xfs_inode	*ip2,
2730d31a1825SCarlos Maiolino 	int			spaceres)
2731d31a1825SCarlos Maiolino {
2732d31a1825SCarlos Maiolino 	int		error = 0;
2733d31a1825SCarlos Maiolino 	int		ip1_flags = 0;
2734d31a1825SCarlos Maiolino 	int		ip2_flags = 0;
2735d31a1825SCarlos Maiolino 	int		dp2_flags = 0;
2736d31a1825SCarlos Maiolino 
2737d31a1825SCarlos Maiolino 	/* Swap inode number for dirent in first parent */
2738381eee69SBrian Foster 	error = xfs_dir_replace(tp, dp1, name1, ip2->i_ino, spaceres);
2739d31a1825SCarlos Maiolino 	if (error)
2740eeacd321SDave Chinner 		goto out_trans_abort;
2741d31a1825SCarlos Maiolino 
2742d31a1825SCarlos Maiolino 	/* Swap inode number for dirent in second parent */
2743381eee69SBrian Foster 	error = xfs_dir_replace(tp, dp2, name2, ip1->i_ino, spaceres);
2744d31a1825SCarlos Maiolino 	if (error)
2745eeacd321SDave Chinner 		goto out_trans_abort;
2746d31a1825SCarlos Maiolino 
2747d31a1825SCarlos Maiolino 	/*
2748d31a1825SCarlos Maiolino 	 * If we're renaming one or more directories across different parents,
2749d31a1825SCarlos Maiolino 	 * update the respective ".." entries (and link counts) to match the new
2750d31a1825SCarlos Maiolino 	 * parents.
2751d31a1825SCarlos Maiolino 	 */
2752d31a1825SCarlos Maiolino 	if (dp1 != dp2) {
2753d31a1825SCarlos Maiolino 		dp2_flags = XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
2754d31a1825SCarlos Maiolino 
2755c19b3b05SDave Chinner 		if (S_ISDIR(VFS_I(ip2)->i_mode)) {
2756d31a1825SCarlos Maiolino 			error = xfs_dir_replace(tp, ip2, &xfs_name_dotdot,
2757381eee69SBrian Foster 						dp1->i_ino, spaceres);
2758d31a1825SCarlos Maiolino 			if (error)
2759eeacd321SDave Chinner 				goto out_trans_abort;
2760d31a1825SCarlos Maiolino 
2761d31a1825SCarlos Maiolino 			/* transfer ip2 ".." reference to dp1 */
2762c19b3b05SDave Chinner 			if (!S_ISDIR(VFS_I(ip1)->i_mode)) {
2763d31a1825SCarlos Maiolino 				error = xfs_droplink(tp, dp2);
2764d31a1825SCarlos Maiolino 				if (error)
2765eeacd321SDave Chinner 					goto out_trans_abort;
2766d31a1825SCarlos Maiolino 				error = xfs_bumplink(tp, dp1);
2767d31a1825SCarlos Maiolino 				if (error)
2768eeacd321SDave Chinner 					goto out_trans_abort;
2769d31a1825SCarlos Maiolino 			}
2770d31a1825SCarlos Maiolino 
2771d31a1825SCarlos Maiolino 			/*
2772d31a1825SCarlos Maiolino 			 * Although ip1 isn't changed here, userspace needs
2773d31a1825SCarlos Maiolino 			 * to be warned about the change, so that applications
2774d31a1825SCarlos Maiolino 			 * relying on it (like backup ones), will properly
2775d31a1825SCarlos Maiolino 			 * notify the change
2776d31a1825SCarlos Maiolino 			 */
2777d31a1825SCarlos Maiolino 			ip1_flags |= XFS_ICHGTIME_CHG;
2778d31a1825SCarlos Maiolino 			ip2_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
2779d31a1825SCarlos Maiolino 		}
2780d31a1825SCarlos Maiolino 
2781c19b3b05SDave Chinner 		if (S_ISDIR(VFS_I(ip1)->i_mode)) {
2782d31a1825SCarlos Maiolino 			error = xfs_dir_replace(tp, ip1, &xfs_name_dotdot,
2783381eee69SBrian Foster 						dp2->i_ino, spaceres);
2784d31a1825SCarlos Maiolino 			if (error)
2785eeacd321SDave Chinner 				goto out_trans_abort;
2786d31a1825SCarlos Maiolino 
2787d31a1825SCarlos Maiolino 			/* transfer ip1 ".." reference to dp2 */
2788c19b3b05SDave Chinner 			if (!S_ISDIR(VFS_I(ip2)->i_mode)) {
2789d31a1825SCarlos Maiolino 				error = xfs_droplink(tp, dp1);
2790d31a1825SCarlos Maiolino 				if (error)
2791eeacd321SDave Chinner 					goto out_trans_abort;
2792d31a1825SCarlos Maiolino 				error = xfs_bumplink(tp, dp2);
2793d31a1825SCarlos Maiolino 				if (error)
2794eeacd321SDave Chinner 					goto out_trans_abort;
2795d31a1825SCarlos Maiolino 			}
2796d31a1825SCarlos Maiolino 
2797d31a1825SCarlos Maiolino 			/*
2798d31a1825SCarlos Maiolino 			 * Although ip2 isn't changed here, userspace needs
2799d31a1825SCarlos Maiolino 			 * to be warned about the change, so that applications
2800d31a1825SCarlos Maiolino 			 * relying on it (like backup ones), will properly
2801d31a1825SCarlos Maiolino 			 * notify the change
2802d31a1825SCarlos Maiolino 			 */
2803d31a1825SCarlos Maiolino 			ip1_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
2804d31a1825SCarlos Maiolino 			ip2_flags |= XFS_ICHGTIME_CHG;
2805d31a1825SCarlos Maiolino 		}
2806d31a1825SCarlos Maiolino 	}
2807d31a1825SCarlos Maiolino 
2808d31a1825SCarlos Maiolino 	if (ip1_flags) {
2809d31a1825SCarlos Maiolino 		xfs_trans_ichgtime(tp, ip1, ip1_flags);
2810d31a1825SCarlos Maiolino 		xfs_trans_log_inode(tp, ip1, XFS_ILOG_CORE);
2811d31a1825SCarlos Maiolino 	}
2812d31a1825SCarlos Maiolino 	if (ip2_flags) {
2813d31a1825SCarlos Maiolino 		xfs_trans_ichgtime(tp, ip2, ip2_flags);
2814d31a1825SCarlos Maiolino 		xfs_trans_log_inode(tp, ip2, XFS_ILOG_CORE);
2815d31a1825SCarlos Maiolino 	}
2816d31a1825SCarlos Maiolino 	if (dp2_flags) {
2817d31a1825SCarlos Maiolino 		xfs_trans_ichgtime(tp, dp2, dp2_flags);
2818d31a1825SCarlos Maiolino 		xfs_trans_log_inode(tp, dp2, XFS_ILOG_CORE);
2819d31a1825SCarlos Maiolino 	}
2820d31a1825SCarlos Maiolino 	xfs_trans_ichgtime(tp, dp1, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2821d31a1825SCarlos Maiolino 	xfs_trans_log_inode(tp, dp1, XFS_ILOG_CORE);
2822c9cfdb38SBrian Foster 	return xfs_finish_rename(tp);
2823eeacd321SDave Chinner 
2824eeacd321SDave Chinner out_trans_abort:
28254906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
2826d31a1825SCarlos Maiolino 	return error;
2827d31a1825SCarlos Maiolino }
2828d31a1825SCarlos Maiolino 
2829d31a1825SCarlos Maiolino /*
28307dcf5c3eSDave Chinner  * xfs_rename_alloc_whiteout()
28317dcf5c3eSDave Chinner  *
28327dcf5c3eSDave Chinner  * Return a referenced, unlinked, unlocked inode that that can be used as a
28337dcf5c3eSDave Chinner  * whiteout in a rename transaction. We use a tmpfile inode here so that if we
28347dcf5c3eSDave Chinner  * crash between allocating the inode and linking it into the rename transaction
28357dcf5c3eSDave Chinner  * recovery will free the inode and we won't leak it.
28367dcf5c3eSDave Chinner  */
28377dcf5c3eSDave Chinner static int
28387dcf5c3eSDave Chinner xfs_rename_alloc_whiteout(
28397dcf5c3eSDave Chinner 	struct xfs_inode	*dp,
28407dcf5c3eSDave Chinner 	struct xfs_inode	**wip)
28417dcf5c3eSDave Chinner {
28427dcf5c3eSDave Chinner 	struct xfs_inode	*tmpfile;
28437dcf5c3eSDave Chinner 	int			error;
28447dcf5c3eSDave Chinner 
2845a1f69417SEric Sandeen 	error = xfs_create_tmpfile(dp, S_IFCHR | WHITEOUT_MODE, &tmpfile);
28467dcf5c3eSDave Chinner 	if (error)
28477dcf5c3eSDave Chinner 		return error;
28487dcf5c3eSDave Chinner 
284922419ac9SBrian Foster 	/*
285022419ac9SBrian Foster 	 * Prepare the tmpfile inode as if it were created through the VFS.
285122419ac9SBrian Foster 	 * Otherwise, the link increment paths will complain about nlink 0->1.
285222419ac9SBrian Foster 	 * Drop the link count as done by d_tmpfile(), complete the inode setup
285322419ac9SBrian Foster 	 * and flag it as linkable.
285422419ac9SBrian Foster 	 */
285522419ac9SBrian Foster 	drop_nlink(VFS_I(tmpfile));
28562b3d1d41SChristoph Hellwig 	xfs_setup_iops(tmpfile);
28577dcf5c3eSDave Chinner 	xfs_finish_inode_setup(tmpfile);
28587dcf5c3eSDave Chinner 	VFS_I(tmpfile)->i_state |= I_LINKABLE;
28597dcf5c3eSDave Chinner 
28607dcf5c3eSDave Chinner 	*wip = tmpfile;
28617dcf5c3eSDave Chinner 	return 0;
28627dcf5c3eSDave Chinner }
28637dcf5c3eSDave Chinner 
28647dcf5c3eSDave Chinner /*
2865f6bba201SDave Chinner  * xfs_rename
2866f6bba201SDave Chinner  */
2867f6bba201SDave Chinner int
2868f6bba201SDave Chinner xfs_rename(
28697dcf5c3eSDave Chinner 	struct xfs_inode	*src_dp,
2870f6bba201SDave Chinner 	struct xfs_name		*src_name,
28717dcf5c3eSDave Chinner 	struct xfs_inode	*src_ip,
28727dcf5c3eSDave Chinner 	struct xfs_inode	*target_dp,
2873f6bba201SDave Chinner 	struct xfs_name		*target_name,
28747dcf5c3eSDave Chinner 	struct xfs_inode	*target_ip,
2875d31a1825SCarlos Maiolino 	unsigned int		flags)
2876f6bba201SDave Chinner {
28777dcf5c3eSDave Chinner 	struct xfs_mount	*mp = src_dp->i_mount;
28787dcf5c3eSDave Chinner 	struct xfs_trans	*tp;
28797dcf5c3eSDave Chinner 	struct xfs_inode	*wip = NULL;		/* whiteout inode */
28807dcf5c3eSDave Chinner 	struct xfs_inode	*inodes[__XFS_SORT_INODES];
288195afcf5cSDave Chinner 	int			num_inodes = __XFS_SORT_INODES;
28822b93681fSDave Chinner 	bool			new_parent = (src_dp != target_dp);
2883c19b3b05SDave Chinner 	bool			src_is_directory = S_ISDIR(VFS_I(src_ip)->i_mode);
2884f6bba201SDave Chinner 	int			spaceres;
28857dcf5c3eSDave Chinner 	int			error;
2886f6bba201SDave Chinner 
2887f6bba201SDave Chinner 	trace_xfs_rename(src_dp, target_dp, src_name, target_name);
2888f6bba201SDave Chinner 
2889eeacd321SDave Chinner 	if ((flags & RENAME_EXCHANGE) && !target_ip)
2890eeacd321SDave Chinner 		return -EINVAL;
2891f6bba201SDave Chinner 
28927dcf5c3eSDave Chinner 	/*
28937dcf5c3eSDave Chinner 	 * If we are doing a whiteout operation, allocate the whiteout inode
28947dcf5c3eSDave Chinner 	 * we will be placing at the target and ensure the type is set
28957dcf5c3eSDave Chinner 	 * appropriately.
28967dcf5c3eSDave Chinner 	 */
28977dcf5c3eSDave Chinner 	if (flags & RENAME_WHITEOUT) {
28987dcf5c3eSDave Chinner 		ASSERT(!(flags & (RENAME_NOREPLACE | RENAME_EXCHANGE)));
28997dcf5c3eSDave Chinner 		error = xfs_rename_alloc_whiteout(target_dp, &wip);
29007dcf5c3eSDave Chinner 		if (error)
29017dcf5c3eSDave Chinner 			return error;
2902f6bba201SDave Chinner 
29037dcf5c3eSDave Chinner 		/* setup target dirent info as whiteout */
29047dcf5c3eSDave Chinner 		src_name->type = XFS_DIR3_FT_CHRDEV;
29057dcf5c3eSDave Chinner 	}
29067dcf5c3eSDave Chinner 
29077dcf5c3eSDave Chinner 	xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip, wip,
2908f6bba201SDave Chinner 				inodes, &num_inodes);
2909f6bba201SDave Chinner 
2910f6bba201SDave Chinner 	spaceres = XFS_RENAME_SPACE_RES(mp, target_name->len);
2911253f4911SChristoph Hellwig 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_rename, spaceres, 0, 0, &tp);
29122451337dSDave Chinner 	if (error == -ENOSPC) {
2913f6bba201SDave Chinner 		spaceres = 0;
2914253f4911SChristoph Hellwig 		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_rename, 0, 0, 0,
2915253f4911SChristoph Hellwig 				&tp);
2916f6bba201SDave Chinner 	}
2917445883e8SDave Chinner 	if (error)
2918253f4911SChristoph Hellwig 		goto out_release_wip;
2919f6bba201SDave Chinner 
2920f6bba201SDave Chinner 	/*
2921f6bba201SDave Chinner 	 * Attach the dquots to the inodes
2922f6bba201SDave Chinner 	 */
2923f6bba201SDave Chinner 	error = xfs_qm_vop_rename_dqattach(inodes);
2924445883e8SDave Chinner 	if (error)
2925445883e8SDave Chinner 		goto out_trans_cancel;
2926f6bba201SDave Chinner 
2927f6bba201SDave Chinner 	/*
2928f6bba201SDave Chinner 	 * Lock all the participating inodes. Depending upon whether
2929f6bba201SDave Chinner 	 * the target_name exists in the target directory, and
2930f6bba201SDave Chinner 	 * whether the target directory is the same as the source
2931f6bba201SDave Chinner 	 * directory, we can lock from 2 to 4 inodes.
2932f6bba201SDave Chinner 	 */
2933f6bba201SDave Chinner 	xfs_lock_inodes(inodes, num_inodes, XFS_ILOCK_EXCL);
2934f6bba201SDave Chinner 
2935f6bba201SDave Chinner 	/*
2936f6bba201SDave Chinner 	 * Join all the inodes to the transaction. From this point on,
2937f6bba201SDave Chinner 	 * we can rely on either trans_commit or trans_cancel to unlock
2938f6bba201SDave Chinner 	 * them.
2939f6bba201SDave Chinner 	 */
294065523218SChristoph Hellwig 	xfs_trans_ijoin(tp, src_dp, XFS_ILOCK_EXCL);
2941f6bba201SDave Chinner 	if (new_parent)
294265523218SChristoph Hellwig 		xfs_trans_ijoin(tp, target_dp, XFS_ILOCK_EXCL);
2943f6bba201SDave Chinner 	xfs_trans_ijoin(tp, src_ip, XFS_ILOCK_EXCL);
2944f6bba201SDave Chinner 	if (target_ip)
2945f6bba201SDave Chinner 		xfs_trans_ijoin(tp, target_ip, XFS_ILOCK_EXCL);
29467dcf5c3eSDave Chinner 	if (wip)
29477dcf5c3eSDave Chinner 		xfs_trans_ijoin(tp, wip, XFS_ILOCK_EXCL);
2948f6bba201SDave Chinner 
2949f6bba201SDave Chinner 	/*
2950f6bba201SDave Chinner 	 * If we are using project inheritance, we only allow renames
2951f6bba201SDave Chinner 	 * into our tree when the project IDs are the same; else the
2952f6bba201SDave Chinner 	 * tree quota mechanism would be circumvented.
2953f6bba201SDave Chinner 	 */
2954f6bba201SDave Chinner 	if (unlikely((target_dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2955f6bba201SDave Chinner 		     (xfs_get_projid(target_dp) != xfs_get_projid(src_ip)))) {
29562451337dSDave Chinner 		error = -EXDEV;
2957445883e8SDave Chinner 		goto out_trans_cancel;
2958f6bba201SDave Chinner 	}
2959f6bba201SDave Chinner 
2960eeacd321SDave Chinner 	/* RENAME_EXCHANGE is unique from here on. */
2961eeacd321SDave Chinner 	if (flags & RENAME_EXCHANGE)
2962eeacd321SDave Chinner 		return xfs_cross_rename(tp, src_dp, src_name, src_ip,
2963d31a1825SCarlos Maiolino 					target_dp, target_name, target_ip,
2964f16dea54SBrian Foster 					spaceres);
2965d31a1825SCarlos Maiolino 
2966d31a1825SCarlos Maiolino 	/*
2967f6bba201SDave Chinner 	 * Set up the target.
2968f6bba201SDave Chinner 	 */
2969f6bba201SDave Chinner 	if (target_ip == NULL) {
2970f6bba201SDave Chinner 		/*
2971f6bba201SDave Chinner 		 * If there's no space reservation, check the entry will
2972f6bba201SDave Chinner 		 * fit before actually inserting it.
2973f6bba201SDave Chinner 		 */
297494f3cad5SEric Sandeen 		if (!spaceres) {
297594f3cad5SEric Sandeen 			error = xfs_dir_canenter(tp, target_dp, target_name);
2976f6bba201SDave Chinner 			if (error)
2977445883e8SDave Chinner 				goto out_trans_cancel;
297894f3cad5SEric Sandeen 		}
2979f6bba201SDave Chinner 		/*
2980f6bba201SDave Chinner 		 * If target does not exist and the rename crosses
2981f6bba201SDave Chinner 		 * directories, adjust the target directory link count
2982f6bba201SDave Chinner 		 * to account for the ".." reference from the new entry.
2983f6bba201SDave Chinner 		 */
2984f6bba201SDave Chinner 		error = xfs_dir_createname(tp, target_dp, target_name,
2985381eee69SBrian Foster 					   src_ip->i_ino, spaceres);
2986f6bba201SDave Chinner 		if (error)
2987*c8eac49eSBrian Foster 			goto out_trans_cancel;
2988f6bba201SDave Chinner 
2989f6bba201SDave Chinner 		xfs_trans_ichgtime(tp, target_dp,
2990f6bba201SDave Chinner 					XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2991f6bba201SDave Chinner 
2992f6bba201SDave Chinner 		if (new_parent && src_is_directory) {
2993f6bba201SDave Chinner 			error = xfs_bumplink(tp, target_dp);
2994f6bba201SDave Chinner 			if (error)
2995*c8eac49eSBrian Foster 				goto out_trans_cancel;
2996f6bba201SDave Chinner 		}
2997f6bba201SDave Chinner 	} else { /* target_ip != NULL */
2998f6bba201SDave Chinner 		/*
2999f6bba201SDave Chinner 		 * If target exists and it's a directory, check that both
3000f6bba201SDave Chinner 		 * target and source are directories and that target can be
3001f6bba201SDave Chinner 		 * destroyed, or that neither is a directory.
3002f6bba201SDave Chinner 		 */
3003c19b3b05SDave Chinner 		if (S_ISDIR(VFS_I(target_ip)->i_mode)) {
3004f6bba201SDave Chinner 			/*
3005f6bba201SDave Chinner 			 * Make sure target dir is empty.
3006f6bba201SDave Chinner 			 */
3007f6bba201SDave Chinner 			if (!(xfs_dir_isempty(target_ip)) ||
300854d7b5c1SDave Chinner 			    (VFS_I(target_ip)->i_nlink > 2)) {
30092451337dSDave Chinner 				error = -EEXIST;
3010445883e8SDave Chinner 				goto out_trans_cancel;
3011f6bba201SDave Chinner 			}
3012f6bba201SDave Chinner 		}
3013f6bba201SDave Chinner 
3014f6bba201SDave Chinner 		/*
3015f6bba201SDave Chinner 		 * Link the source inode under the target name.
3016f6bba201SDave Chinner 		 * If the source inode is a directory and we are moving
3017f6bba201SDave Chinner 		 * it across directories, its ".." entry will be
3018f6bba201SDave Chinner 		 * inconsistent until we replace that down below.
3019f6bba201SDave Chinner 		 *
3020f6bba201SDave Chinner 		 * In case there is already an entry with the same
3021f6bba201SDave Chinner 		 * name at the destination directory, remove it first.
3022f6bba201SDave Chinner 		 */
3023f6bba201SDave Chinner 		error = xfs_dir_replace(tp, target_dp, target_name,
3024381eee69SBrian Foster 					src_ip->i_ino, spaceres);
3025f6bba201SDave Chinner 		if (error)
3026*c8eac49eSBrian Foster 			goto out_trans_cancel;
3027f6bba201SDave Chinner 
3028f6bba201SDave Chinner 		xfs_trans_ichgtime(tp, target_dp,
3029f6bba201SDave Chinner 					XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3030f6bba201SDave Chinner 
3031f6bba201SDave Chinner 		/*
3032f6bba201SDave Chinner 		 * Decrement the link count on the target since the target
3033f6bba201SDave Chinner 		 * dir no longer points to it.
3034f6bba201SDave Chinner 		 */
3035f6bba201SDave Chinner 		error = xfs_droplink(tp, target_ip);
3036f6bba201SDave Chinner 		if (error)
3037*c8eac49eSBrian Foster 			goto out_trans_cancel;
3038f6bba201SDave Chinner 
3039f6bba201SDave Chinner 		if (src_is_directory) {
3040f6bba201SDave Chinner 			/*
3041f6bba201SDave Chinner 			 * Drop the link from the old "." entry.
3042f6bba201SDave Chinner 			 */
3043f6bba201SDave Chinner 			error = xfs_droplink(tp, target_ip);
3044f6bba201SDave Chinner 			if (error)
3045*c8eac49eSBrian Foster 				goto out_trans_cancel;
3046f6bba201SDave Chinner 		}
3047f6bba201SDave Chinner 	} /* target_ip != NULL */
3048f6bba201SDave Chinner 
3049f6bba201SDave Chinner 	/*
3050f6bba201SDave Chinner 	 * Remove the source.
3051f6bba201SDave Chinner 	 */
3052f6bba201SDave Chinner 	if (new_parent && src_is_directory) {
3053f6bba201SDave Chinner 		/*
3054f6bba201SDave Chinner 		 * Rewrite the ".." entry to point to the new
3055f6bba201SDave Chinner 		 * directory.
3056f6bba201SDave Chinner 		 */
3057f6bba201SDave Chinner 		error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot,
3058381eee69SBrian Foster 					target_dp->i_ino, spaceres);
30592451337dSDave Chinner 		ASSERT(error != -EEXIST);
3060f6bba201SDave Chinner 		if (error)
3061*c8eac49eSBrian Foster 			goto out_trans_cancel;
3062f6bba201SDave Chinner 	}
3063f6bba201SDave Chinner 
3064f6bba201SDave Chinner 	/*
3065f6bba201SDave Chinner 	 * We always want to hit the ctime on the source inode.
3066f6bba201SDave Chinner 	 *
3067f6bba201SDave Chinner 	 * This isn't strictly required by the standards since the source
3068f6bba201SDave Chinner 	 * inode isn't really being changed, but old unix file systems did
3069f6bba201SDave Chinner 	 * it and some incremental backup programs won't work without it.
3070f6bba201SDave Chinner 	 */
3071f6bba201SDave Chinner 	xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG);
3072f6bba201SDave Chinner 	xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE);
3073f6bba201SDave Chinner 
3074f6bba201SDave Chinner 	/*
3075f6bba201SDave Chinner 	 * Adjust the link count on src_dp.  This is necessary when
3076f6bba201SDave Chinner 	 * renaming a directory, either within one parent when
3077f6bba201SDave Chinner 	 * the target existed, or across two parent directories.
3078f6bba201SDave Chinner 	 */
3079f6bba201SDave Chinner 	if (src_is_directory && (new_parent || target_ip != NULL)) {
3080f6bba201SDave Chinner 
3081f6bba201SDave Chinner 		/*
3082f6bba201SDave Chinner 		 * Decrement link count on src_directory since the
3083f6bba201SDave Chinner 		 * entry that's moved no longer points to it.
3084f6bba201SDave Chinner 		 */
3085f6bba201SDave Chinner 		error = xfs_droplink(tp, src_dp);
3086f6bba201SDave Chinner 		if (error)
3087*c8eac49eSBrian Foster 			goto out_trans_cancel;
3088f6bba201SDave Chinner 	}
3089f6bba201SDave Chinner 
30907dcf5c3eSDave Chinner 	/*
30917dcf5c3eSDave Chinner 	 * For whiteouts, we only need to update the source dirent with the
30927dcf5c3eSDave Chinner 	 * inode number of the whiteout inode rather than removing it
30937dcf5c3eSDave Chinner 	 * altogether.
30947dcf5c3eSDave Chinner 	 */
30957dcf5c3eSDave Chinner 	if (wip) {
30967dcf5c3eSDave Chinner 		error = xfs_dir_replace(tp, src_dp, src_name, wip->i_ino,
3097381eee69SBrian Foster 					spaceres);
30987dcf5c3eSDave Chinner 	} else
3099f6bba201SDave Chinner 		error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino,
3100381eee69SBrian Foster 					   spaceres);
3101f6bba201SDave Chinner 	if (error)
3102*c8eac49eSBrian Foster 		goto out_trans_cancel;
3103f6bba201SDave Chinner 
31047dcf5c3eSDave Chinner 	/*
31057dcf5c3eSDave Chinner 	 * For whiteouts, we need to bump the link count on the whiteout inode.
31067dcf5c3eSDave Chinner 	 * This means that failures all the way up to this point leave the inode
31077dcf5c3eSDave Chinner 	 * on the unlinked list and so cleanup is a simple matter of dropping
31087dcf5c3eSDave Chinner 	 * the remaining reference to it. If we fail here after bumping the link
31097dcf5c3eSDave Chinner 	 * count, we're shutting down the filesystem so we'll never see the
31107dcf5c3eSDave Chinner 	 * intermediate state on disk.
31117dcf5c3eSDave Chinner 	 */
31127dcf5c3eSDave Chinner 	if (wip) {
311354d7b5c1SDave Chinner 		ASSERT(VFS_I(wip)->i_nlink == 0);
31147dcf5c3eSDave Chinner 		error = xfs_bumplink(tp, wip);
31157dcf5c3eSDave Chinner 		if (error)
3116*c8eac49eSBrian Foster 			goto out_trans_cancel;
31177dcf5c3eSDave Chinner 		error = xfs_iunlink_remove(tp, wip);
31187dcf5c3eSDave Chinner 		if (error)
3119*c8eac49eSBrian Foster 			goto out_trans_cancel;
31207dcf5c3eSDave Chinner 		xfs_trans_log_inode(tp, wip, XFS_ILOG_CORE);
31217dcf5c3eSDave Chinner 
31227dcf5c3eSDave Chinner 		/*
31237dcf5c3eSDave Chinner 		 * Now we have a real link, clear the "I'm a tmpfile" state
31247dcf5c3eSDave Chinner 		 * flag from the inode so it doesn't accidentally get misused in
31257dcf5c3eSDave Chinner 		 * future.
31267dcf5c3eSDave Chinner 		 */
31277dcf5c3eSDave Chinner 		VFS_I(wip)->i_state &= ~I_LINKABLE;
31287dcf5c3eSDave Chinner 	}
3129f6bba201SDave Chinner 
3130f6bba201SDave Chinner 	xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3131f6bba201SDave Chinner 	xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE);
3132f6bba201SDave Chinner 	if (new_parent)
3133f6bba201SDave Chinner 		xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE);
3134f6bba201SDave Chinner 
3135c9cfdb38SBrian Foster 	error = xfs_finish_rename(tp);
31367dcf5c3eSDave Chinner 	if (wip)
31377dcf5c3eSDave Chinner 		IRELE(wip);
31387dcf5c3eSDave Chinner 	return error;
3139f6bba201SDave Chinner 
3140445883e8SDave Chinner out_trans_cancel:
31414906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
3142253f4911SChristoph Hellwig out_release_wip:
31437dcf5c3eSDave Chinner 	if (wip)
31447dcf5c3eSDave Chinner 		IRELE(wip);
3145f6bba201SDave Chinner 	return error;
3146f6bba201SDave Chinner }
3147f6bba201SDave Chinner 
3148bad55843SDavid Chinner STATIC int
3149bad55843SDavid Chinner xfs_iflush_cluster(
315019429363SDave Chinner 	struct xfs_inode	*ip,
315119429363SDave Chinner 	struct xfs_buf		*bp)
3152bad55843SDavid Chinner {
315319429363SDave Chinner 	struct xfs_mount	*mp = ip->i_mount;
31545017e97dSDave Chinner 	struct xfs_perag	*pag;
3155bad55843SDavid Chinner 	unsigned long		first_index, mask;
3156c8f5f12eSDavid Chinner 	unsigned long		inodes_per_cluster;
315719429363SDave Chinner 	int			cilist_size;
315819429363SDave Chinner 	struct xfs_inode	**cilist;
315919429363SDave Chinner 	struct xfs_inode	*cip;
3160bad55843SDavid Chinner 	int			nr_found;
3161bad55843SDavid Chinner 	int			clcount = 0;
3162bad55843SDavid Chinner 	int			i;
3163bad55843SDavid Chinner 
31645017e97dSDave Chinner 	pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
3165bad55843SDavid Chinner 
31660f49efd8SJie Liu 	inodes_per_cluster = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog;
316719429363SDave Chinner 	cilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
316819429363SDave Chinner 	cilist = kmem_alloc(cilist_size, KM_MAYFAIL|KM_NOFS);
316919429363SDave Chinner 	if (!cilist)
317044b56e0aSDave Chinner 		goto out_put;
3171bad55843SDavid Chinner 
31720f49efd8SJie Liu 	mask = ~(((mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog)) - 1);
3173bad55843SDavid Chinner 	first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
31741a3e8f3dSDave Chinner 	rcu_read_lock();
3175bad55843SDavid Chinner 	/* really need a gang lookup range call here */
317619429363SDave Chinner 	nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)cilist,
3177c8f5f12eSDavid Chinner 					first_index, inodes_per_cluster);
3178bad55843SDavid Chinner 	if (nr_found == 0)
3179bad55843SDavid Chinner 		goto out_free;
3180bad55843SDavid Chinner 
3181bad55843SDavid Chinner 	for (i = 0; i < nr_found; i++) {
318219429363SDave Chinner 		cip = cilist[i];
318319429363SDave Chinner 		if (cip == ip)
3184bad55843SDavid Chinner 			continue;
31851a3e8f3dSDave Chinner 
31861a3e8f3dSDave Chinner 		/*
31871a3e8f3dSDave Chinner 		 * because this is an RCU protected lookup, we could find a
31881a3e8f3dSDave Chinner 		 * recently freed or even reallocated inode during the lookup.
31891a3e8f3dSDave Chinner 		 * We need to check under the i_flags_lock for a valid inode
31901a3e8f3dSDave Chinner 		 * here. Skip it if it is not valid or the wrong inode.
31911a3e8f3dSDave Chinner 		 */
319219429363SDave Chinner 		spin_lock(&cip->i_flags_lock);
319319429363SDave Chinner 		if (!cip->i_ino ||
319419429363SDave Chinner 		    __xfs_iflags_test(cip, XFS_ISTALE)) {
319519429363SDave Chinner 			spin_unlock(&cip->i_flags_lock);
31961a3e8f3dSDave Chinner 			continue;
31971a3e8f3dSDave Chinner 		}
31985a90e53eSDave Chinner 
31995a90e53eSDave Chinner 		/*
32005a90e53eSDave Chinner 		 * Once we fall off the end of the cluster, no point checking
32015a90e53eSDave Chinner 		 * any more inodes in the list because they will also all be
32025a90e53eSDave Chinner 		 * outside the cluster.
32035a90e53eSDave Chinner 		 */
320419429363SDave Chinner 		if ((XFS_INO_TO_AGINO(mp, cip->i_ino) & mask) != first_index) {
320519429363SDave Chinner 			spin_unlock(&cip->i_flags_lock);
32065a90e53eSDave Chinner 			break;
32075a90e53eSDave Chinner 		}
320819429363SDave Chinner 		spin_unlock(&cip->i_flags_lock);
32091a3e8f3dSDave Chinner 
3210bad55843SDavid Chinner 		/*
3211bad55843SDavid Chinner 		 * Do an un-protected check to see if the inode is dirty and
3212bad55843SDavid Chinner 		 * is a candidate for flushing.  These checks will be repeated
3213bad55843SDavid Chinner 		 * later after the appropriate locks are acquired.
3214bad55843SDavid Chinner 		 */
321519429363SDave Chinner 		if (xfs_inode_clean(cip) && xfs_ipincount(cip) == 0)
3216bad55843SDavid Chinner 			continue;
3217bad55843SDavid Chinner 
3218bad55843SDavid Chinner 		/*
3219bad55843SDavid Chinner 		 * Try to get locks.  If any are unavailable or it is pinned,
3220bad55843SDavid Chinner 		 * then this inode cannot be flushed and is skipped.
3221bad55843SDavid Chinner 		 */
3222bad55843SDavid Chinner 
322319429363SDave Chinner 		if (!xfs_ilock_nowait(cip, XFS_ILOCK_SHARED))
3224bad55843SDavid Chinner 			continue;
322519429363SDave Chinner 		if (!xfs_iflock_nowait(cip)) {
322619429363SDave Chinner 			xfs_iunlock(cip, XFS_ILOCK_SHARED);
3227bad55843SDavid Chinner 			continue;
3228bad55843SDavid Chinner 		}
322919429363SDave Chinner 		if (xfs_ipincount(cip)) {
323019429363SDave Chinner 			xfs_ifunlock(cip);
323119429363SDave Chinner 			xfs_iunlock(cip, XFS_ILOCK_SHARED);
3232bad55843SDavid Chinner 			continue;
3233bad55843SDavid Chinner 		}
3234bad55843SDavid Chinner 
32358a17d7ddSDave Chinner 
32368a17d7ddSDave Chinner 		/*
32378a17d7ddSDave Chinner 		 * Check the inode number again, just to be certain we are not
32388a17d7ddSDave Chinner 		 * racing with freeing in xfs_reclaim_inode(). See the comments
32398a17d7ddSDave Chinner 		 * in that function for more information as to why the initial
32408a17d7ddSDave Chinner 		 * check is not sufficient.
32418a17d7ddSDave Chinner 		 */
324219429363SDave Chinner 		if (!cip->i_ino) {
324319429363SDave Chinner 			xfs_ifunlock(cip);
324419429363SDave Chinner 			xfs_iunlock(cip, XFS_ILOCK_SHARED);
3245bad55843SDavid Chinner 			continue;
3246bad55843SDavid Chinner 		}
3247bad55843SDavid Chinner 
3248bad55843SDavid Chinner 		/*
3249bad55843SDavid Chinner 		 * arriving here means that this inode can be flushed.  First
3250bad55843SDavid Chinner 		 * re-check that it's dirty before flushing.
3251bad55843SDavid Chinner 		 */
325219429363SDave Chinner 		if (!xfs_inode_clean(cip)) {
3253bad55843SDavid Chinner 			int	error;
325419429363SDave Chinner 			error = xfs_iflush_int(cip, bp);
3255bad55843SDavid Chinner 			if (error) {
325619429363SDave Chinner 				xfs_iunlock(cip, XFS_ILOCK_SHARED);
3257bad55843SDavid Chinner 				goto cluster_corrupt_out;
3258bad55843SDavid Chinner 			}
3259bad55843SDavid Chinner 			clcount++;
3260bad55843SDavid Chinner 		} else {
326119429363SDave Chinner 			xfs_ifunlock(cip);
3262bad55843SDavid Chinner 		}
326319429363SDave Chinner 		xfs_iunlock(cip, XFS_ILOCK_SHARED);
3264bad55843SDavid Chinner 	}
3265bad55843SDavid Chinner 
3266bad55843SDavid Chinner 	if (clcount) {
3267ff6d6af2SBill O'Donnell 		XFS_STATS_INC(mp, xs_icluster_flushcnt);
3268ff6d6af2SBill O'Donnell 		XFS_STATS_ADD(mp, xs_icluster_flushinode, clcount);
3269bad55843SDavid Chinner 	}
3270bad55843SDavid Chinner 
3271bad55843SDavid Chinner out_free:
32721a3e8f3dSDave Chinner 	rcu_read_unlock();
327319429363SDave Chinner 	kmem_free(cilist);
327444b56e0aSDave Chinner out_put:
327544b56e0aSDave Chinner 	xfs_perag_put(pag);
3276bad55843SDavid Chinner 	return 0;
3277bad55843SDavid Chinner 
3278bad55843SDavid Chinner 
3279bad55843SDavid Chinner cluster_corrupt_out:
3280bad55843SDavid Chinner 	/*
3281bad55843SDavid Chinner 	 * Corruption detected in the clustering loop.  Invalidate the
3282bad55843SDavid Chinner 	 * inode buffer and shut down the filesystem.
3283bad55843SDavid Chinner 	 */
32841a3e8f3dSDave Chinner 	rcu_read_unlock();
3285bad55843SDavid Chinner 	xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
3286bad55843SDavid Chinner 
3287bad55843SDavid Chinner 	/*
3288e53946dbSDave Chinner 	 * We'll always have an inode attached to the buffer for completion
3289e53946dbSDave Chinner 	 * process by the time we are called from xfs_iflush(). Hence we have
3290e53946dbSDave Chinner 	 * always need to do IO completion processing to abort the inodes
3291e53946dbSDave Chinner 	 * attached to the buffer.  handle them just like the shutdown case in
3292e53946dbSDave Chinner 	 * xfs_buf_submit().
3293bad55843SDavid Chinner 	 */
3294e53946dbSDave Chinner 	ASSERT(bp->b_iodone);
3295b0388bf1SDave Chinner 	bp->b_flags &= ~XBF_DONE;
3296c867cb61SChristoph Hellwig 	xfs_buf_stale(bp);
32972451337dSDave Chinner 	xfs_buf_ioerror(bp, -EIO);
3298e8aaba9aSDave Chinner 	xfs_buf_ioend(bp);
3299bad55843SDavid Chinner 
3300e53946dbSDave Chinner 	/* abort the corrupt inode, as it was not attached to the buffer */
330119429363SDave Chinner 	xfs_iflush_abort(cip, false);
330219429363SDave Chinner 	kmem_free(cilist);
330344b56e0aSDave Chinner 	xfs_perag_put(pag);
33042451337dSDave Chinner 	return -EFSCORRUPTED;
3305bad55843SDavid Chinner }
3306bad55843SDavid Chinner 
33071da177e4SLinus Torvalds /*
33084c46819aSChristoph Hellwig  * Flush dirty inode metadata into the backing buffer.
33094c46819aSChristoph Hellwig  *
33104c46819aSChristoph Hellwig  * The caller must have the inode lock and the inode flush lock held.  The
33114c46819aSChristoph Hellwig  * inode lock will still be held upon return to the caller, and the inode
33124c46819aSChristoph Hellwig  * flush lock will be released after the inode has reached the disk.
33134c46819aSChristoph Hellwig  *
33144c46819aSChristoph Hellwig  * The caller must write out the buffer returned in *bpp and release it.
33151da177e4SLinus Torvalds  */
33161da177e4SLinus Torvalds int
33171da177e4SLinus Torvalds xfs_iflush(
33184c46819aSChristoph Hellwig 	struct xfs_inode	*ip,
33194c46819aSChristoph Hellwig 	struct xfs_buf		**bpp)
33201da177e4SLinus Torvalds {
33214c46819aSChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
3322b1438f47SDave Chinner 	struct xfs_buf		*bp = NULL;
33234c46819aSChristoph Hellwig 	struct xfs_dinode	*dip;
33241da177e4SLinus Torvalds 	int			error;
33251da177e4SLinus Torvalds 
3326ff6d6af2SBill O'Donnell 	XFS_STATS_INC(mp, xs_iflush_count);
33271da177e4SLinus Torvalds 
3328579aa9caSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
3329474fce06SChristoph Hellwig 	ASSERT(xfs_isiflocked(ip));
33301da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
33318096b1ebSChristoph Hellwig 	       ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
33321da177e4SLinus Torvalds 
33334c46819aSChristoph Hellwig 	*bpp = NULL;
33341da177e4SLinus Torvalds 
33351da177e4SLinus Torvalds 	xfs_iunpin_wait(ip);
33361da177e4SLinus Torvalds 
33371da177e4SLinus Torvalds 	/*
33384b6a4688SDave Chinner 	 * For stale inodes we cannot rely on the backing buffer remaining
33394b6a4688SDave Chinner 	 * stale in cache for the remaining life of the stale inode and so
3340475ee413SChristoph Hellwig 	 * xfs_imap_to_bp() below may give us a buffer that no longer contains
33414b6a4688SDave Chinner 	 * inodes below. We have to check this after ensuring the inode is
33424b6a4688SDave Chinner 	 * unpinned so that it is safe to reclaim the stale inode after the
33434b6a4688SDave Chinner 	 * flush call.
33444b6a4688SDave Chinner 	 */
33454b6a4688SDave Chinner 	if (xfs_iflags_test(ip, XFS_ISTALE)) {
33464b6a4688SDave Chinner 		xfs_ifunlock(ip);
33474b6a4688SDave Chinner 		return 0;
33484b6a4688SDave Chinner 	}
33494b6a4688SDave Chinner 
33504b6a4688SDave Chinner 	/*
33511da177e4SLinus Torvalds 	 * This may have been unpinned because the filesystem is shutting
33521da177e4SLinus Torvalds 	 * down forcibly. If that's the case we must not write this inode
335332ce90a4SChristoph Hellwig 	 * to disk, because the log record didn't make it to disk.
335432ce90a4SChristoph Hellwig 	 *
335532ce90a4SChristoph Hellwig 	 * We also have to remove the log item from the AIL in this case,
335632ce90a4SChristoph Hellwig 	 * as we wait for an empty AIL as part of the unmount process.
33571da177e4SLinus Torvalds 	 */
33581da177e4SLinus Torvalds 	if (XFS_FORCED_SHUTDOWN(mp)) {
33592451337dSDave Chinner 		error = -EIO;
336032ce90a4SChristoph Hellwig 		goto abort_out;
33611da177e4SLinus Torvalds 	}
33621da177e4SLinus Torvalds 
33631da177e4SLinus Torvalds 	/*
3364b1438f47SDave Chinner 	 * Get the buffer containing the on-disk inode. We are doing a try-lock
3365b1438f47SDave Chinner 	 * operation here, so we may get  an EAGAIN error. In that case, we
3366b1438f47SDave Chinner 	 * simply want to return with the inode still dirty.
3367b1438f47SDave Chinner 	 *
3368b1438f47SDave Chinner 	 * If we get any other error, we effectively have a corruption situation
3369b1438f47SDave Chinner 	 * and we cannot flush the inode, so we treat it the same as failing
3370b1438f47SDave Chinner 	 * xfs_iflush_int().
3371a3f74ffbSDavid Chinner 	 */
3372475ee413SChristoph Hellwig 	error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK,
3373475ee413SChristoph Hellwig 			       0);
3374b1438f47SDave Chinner 	if (error == -EAGAIN) {
3375a3f74ffbSDavid Chinner 		xfs_ifunlock(ip);
3376a3f74ffbSDavid Chinner 		return error;
3377a3f74ffbSDavid Chinner 	}
3378b1438f47SDave Chinner 	if (error)
3379b1438f47SDave Chinner 		goto corrupt_out;
3380a3f74ffbSDavid Chinner 
3381a3f74ffbSDavid Chinner 	/*
33821da177e4SLinus Torvalds 	 * First flush out the inode that xfs_iflush was called with.
33831da177e4SLinus Torvalds 	 */
33841da177e4SLinus Torvalds 	error = xfs_iflush_int(ip, bp);
3385bad55843SDavid Chinner 	if (error)
33861da177e4SLinus Torvalds 		goto corrupt_out;
33871da177e4SLinus Torvalds 
33881da177e4SLinus Torvalds 	/*
3389a3f74ffbSDavid Chinner 	 * If the buffer is pinned then push on the log now so we won't
3390a3f74ffbSDavid Chinner 	 * get stuck waiting in the write for too long.
3391a3f74ffbSDavid Chinner 	 */
3392811e64c7SChandra Seetharaman 	if (xfs_buf_ispinned(bp))
3393a14a348bSChristoph Hellwig 		xfs_log_force(mp, 0);
3394a3f74ffbSDavid Chinner 
3395a3f74ffbSDavid Chinner 	/*
3396e53946dbSDave Chinner 	 * inode clustering: try to gather other inodes into this write
3397e53946dbSDave Chinner 	 *
3398e53946dbSDave Chinner 	 * Note: Any error during clustering will result in the filesystem
3399e53946dbSDave Chinner 	 * being shut down and completion callbacks run on the cluster buffer.
3400e53946dbSDave Chinner 	 * As we have already flushed and attached this inode to the buffer,
3401e53946dbSDave Chinner 	 * it has already been aborted and released by xfs_iflush_cluster() and
3402e53946dbSDave Chinner 	 * so we have no further error handling to do here.
34031da177e4SLinus Torvalds 	 */
3404bad55843SDavid Chinner 	error = xfs_iflush_cluster(ip, bp);
3405bad55843SDavid Chinner 	if (error)
3406e53946dbSDave Chinner 		return error;
34071da177e4SLinus Torvalds 
34084c46819aSChristoph Hellwig 	*bpp = bp;
34094c46819aSChristoph Hellwig 	return 0;
34101da177e4SLinus Torvalds 
34111da177e4SLinus Torvalds corrupt_out:
3412b1438f47SDave Chinner 	if (bp)
34131da177e4SLinus Torvalds 		xfs_buf_relse(bp);
34147d04a335SNathan Scott 	xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
341532ce90a4SChristoph Hellwig abort_out:
3416e53946dbSDave Chinner 	/* abort the corrupt inode, as it was not attached to the buffer */
341704913fddSDave Chinner 	xfs_iflush_abort(ip, false);
341832ce90a4SChristoph Hellwig 	return error;
34191da177e4SLinus Torvalds }
34201da177e4SLinus Torvalds 
34219cfb9b47SDarrick J. Wong /*
34229cfb9b47SDarrick J. Wong  * If there are inline format data / attr forks attached to this inode,
34239cfb9b47SDarrick J. Wong  * make sure they're not corrupt.
34249cfb9b47SDarrick J. Wong  */
34259cfb9b47SDarrick J. Wong bool
34269cfb9b47SDarrick J. Wong xfs_inode_verify_forks(
34279cfb9b47SDarrick J. Wong 	struct xfs_inode	*ip)
34289cfb9b47SDarrick J. Wong {
342922431bf3SDarrick J. Wong 	struct xfs_ifork	*ifp;
34309cfb9b47SDarrick J. Wong 	xfs_failaddr_t		fa;
34319cfb9b47SDarrick J. Wong 
34329cfb9b47SDarrick J. Wong 	fa = xfs_ifork_verify_data(ip, &xfs_default_ifork_ops);
34339cfb9b47SDarrick J. Wong 	if (fa) {
343422431bf3SDarrick J. Wong 		ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
343522431bf3SDarrick J. Wong 		xfs_inode_verifier_error(ip, -EFSCORRUPTED, "data fork",
343622431bf3SDarrick J. Wong 				ifp->if_u1.if_data, ifp->if_bytes, fa);
34379cfb9b47SDarrick J. Wong 		return false;
34389cfb9b47SDarrick J. Wong 	}
34399cfb9b47SDarrick J. Wong 
34409cfb9b47SDarrick J. Wong 	fa = xfs_ifork_verify_attr(ip, &xfs_default_ifork_ops);
34419cfb9b47SDarrick J. Wong 	if (fa) {
344222431bf3SDarrick J. Wong 		ifp = XFS_IFORK_PTR(ip, XFS_ATTR_FORK);
344322431bf3SDarrick J. Wong 		xfs_inode_verifier_error(ip, -EFSCORRUPTED, "attr fork",
344422431bf3SDarrick J. Wong 				ifp ? ifp->if_u1.if_data : NULL,
344522431bf3SDarrick J. Wong 				ifp ? ifp->if_bytes : 0, fa);
34469cfb9b47SDarrick J. Wong 		return false;
34479cfb9b47SDarrick J. Wong 	}
34489cfb9b47SDarrick J. Wong 	return true;
34499cfb9b47SDarrick J. Wong }
34509cfb9b47SDarrick J. Wong 
34511da177e4SLinus Torvalds STATIC int
34521da177e4SLinus Torvalds xfs_iflush_int(
345393848a99SChristoph Hellwig 	struct xfs_inode	*ip,
345493848a99SChristoph Hellwig 	struct xfs_buf		*bp)
34551da177e4SLinus Torvalds {
345693848a99SChristoph Hellwig 	struct xfs_inode_log_item *iip = ip->i_itemp;
345793848a99SChristoph Hellwig 	struct xfs_dinode	*dip;
345893848a99SChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
34591da177e4SLinus Torvalds 
3460579aa9caSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
3461474fce06SChristoph Hellwig 	ASSERT(xfs_isiflocked(ip));
34621da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
34638096b1ebSChristoph Hellwig 	       ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
346493848a99SChristoph Hellwig 	ASSERT(iip != NULL && iip->ili_fields != 0);
3465263997a6SDave Chinner 	ASSERT(ip->i_d.di_version > 1);
34661da177e4SLinus Torvalds 
34671da177e4SLinus Torvalds 	/* set *dip = inode's place in the buffer */
346888ee2df7SChristoph Hellwig 	dip = xfs_buf_offset(bp, ip->i_imap.im_boffset);
34691da177e4SLinus Torvalds 
347069ef921bSChristoph Hellwig 	if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
34719e24cfd0SDarrick J. Wong 			       mp, XFS_ERRTAG_IFLUSH_1)) {
34726a19d939SDave Chinner 		xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3473c9690043SDarrick J. Wong 			"%s: Bad inode %Lu magic number 0x%x, ptr "PTR_FMT,
34746a19d939SDave Chinner 			__func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
34751da177e4SLinus Torvalds 		goto corrupt_out;
34761da177e4SLinus Torvalds 	}
3477c19b3b05SDave Chinner 	if (S_ISREG(VFS_I(ip)->i_mode)) {
34781da177e4SLinus Torvalds 		if (XFS_TEST_ERROR(
34791da177e4SLinus Torvalds 		    (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
34801da177e4SLinus Torvalds 		    (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
34819e24cfd0SDarrick J. Wong 		    mp, XFS_ERRTAG_IFLUSH_3)) {
34826a19d939SDave Chinner 			xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3483c9690043SDarrick J. Wong 				"%s: Bad regular inode %Lu, ptr "PTR_FMT,
34846a19d939SDave Chinner 				__func__, ip->i_ino, ip);
34851da177e4SLinus Torvalds 			goto corrupt_out;
34861da177e4SLinus Torvalds 		}
3487c19b3b05SDave Chinner 	} else if (S_ISDIR(VFS_I(ip)->i_mode)) {
34881da177e4SLinus Torvalds 		if (XFS_TEST_ERROR(
34891da177e4SLinus Torvalds 		    (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
34901da177e4SLinus Torvalds 		    (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
34911da177e4SLinus Torvalds 		    (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
34929e24cfd0SDarrick J. Wong 		    mp, XFS_ERRTAG_IFLUSH_4)) {
34936a19d939SDave Chinner 			xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3494c9690043SDarrick J. Wong 				"%s: Bad directory inode %Lu, ptr "PTR_FMT,
34956a19d939SDave Chinner 				__func__, ip->i_ino, ip);
34961da177e4SLinus Torvalds 			goto corrupt_out;
34971da177e4SLinus Torvalds 		}
34981da177e4SLinus Torvalds 	}
34991da177e4SLinus Torvalds 	if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
35009e24cfd0SDarrick J. Wong 				ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5)) {
35016a19d939SDave Chinner 		xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
35026a19d939SDave Chinner 			"%s: detected corrupt incore inode %Lu, "
3503c9690043SDarrick J. Wong 			"total extents = %d, nblocks = %Ld, ptr "PTR_FMT,
35046a19d939SDave Chinner 			__func__, ip->i_ino,
35051da177e4SLinus Torvalds 			ip->i_d.di_nextents + ip->i_d.di_anextents,
35066a19d939SDave Chinner 			ip->i_d.di_nblocks, ip);
35071da177e4SLinus Torvalds 		goto corrupt_out;
35081da177e4SLinus Torvalds 	}
35091da177e4SLinus Torvalds 	if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
35109e24cfd0SDarrick J. Wong 				mp, XFS_ERRTAG_IFLUSH_6)) {
35116a19d939SDave Chinner 		xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3512c9690043SDarrick J. Wong 			"%s: bad inode %Lu, forkoff 0x%x, ptr "PTR_FMT,
35136a19d939SDave Chinner 			__func__, ip->i_ino, ip->i_d.di_forkoff, ip);
35141da177e4SLinus Torvalds 		goto corrupt_out;
35151da177e4SLinus Torvalds 	}
3516e60896d8SDave Chinner 
35171da177e4SLinus Torvalds 	/*
3518263997a6SDave Chinner 	 * Inode item log recovery for v2 inodes are dependent on the
3519e60896d8SDave Chinner 	 * di_flushiter count for correct sequencing. We bump the flush
3520e60896d8SDave Chinner 	 * iteration count so we can detect flushes which postdate a log record
3521e60896d8SDave Chinner 	 * during recovery. This is redundant as we now log every change and
3522e60896d8SDave Chinner 	 * hence this can't happen but we need to still do it to ensure
3523e60896d8SDave Chinner 	 * backwards compatibility with old kernels that predate logging all
3524e60896d8SDave Chinner 	 * inode changes.
35251da177e4SLinus Torvalds 	 */
3526e60896d8SDave Chinner 	if (ip->i_d.di_version < 3)
35271da177e4SLinus Torvalds 		ip->i_d.di_flushiter++;
35281da177e4SLinus Torvalds 
35299cfb9b47SDarrick J. Wong 	/* Check the inline fork data before we write out. */
35309cfb9b47SDarrick J. Wong 	if (!xfs_inode_verify_forks(ip))
3531005c5db8SDarrick J. Wong 		goto corrupt_out;
3532005c5db8SDarrick J. Wong 
35331da177e4SLinus Torvalds 	/*
35343987848cSDave Chinner 	 * Copy the dirty parts of the inode into the on-disk inode.  We always
35353987848cSDave Chinner 	 * copy out the core of the inode, because if the inode is dirty at all
35363987848cSDave Chinner 	 * the core must be.
35371da177e4SLinus Torvalds 	 */
353893f958f9SDave Chinner 	xfs_inode_to_disk(ip, dip, iip->ili_item.li_lsn);
35391da177e4SLinus Torvalds 
35401da177e4SLinus Torvalds 	/* Wrap, we never let the log put out DI_MAX_FLUSH */
35411da177e4SLinus Torvalds 	if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
35421da177e4SLinus Torvalds 		ip->i_d.di_flushiter = 0;
35431da177e4SLinus Torvalds 
3544005c5db8SDarrick J. Wong 	xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK);
3545005c5db8SDarrick J. Wong 	if (XFS_IFORK_Q(ip))
3546005c5db8SDarrick J. Wong 		xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK);
35471da177e4SLinus Torvalds 	xfs_inobp_check(mp, bp);
35481da177e4SLinus Torvalds 
35491da177e4SLinus Torvalds 	/*
3550f5d8d5c4SChristoph Hellwig 	 * We've recorded everything logged in the inode, so we'd like to clear
3551f5d8d5c4SChristoph Hellwig 	 * the ili_fields bits so we don't log and flush things unnecessarily.
3552f5d8d5c4SChristoph Hellwig 	 * However, we can't stop logging all this information until the data
3553f5d8d5c4SChristoph Hellwig 	 * we've copied into the disk buffer is written to disk.  If we did we
3554f5d8d5c4SChristoph Hellwig 	 * might overwrite the copy of the inode in the log with all the data
3555f5d8d5c4SChristoph Hellwig 	 * after re-logging only part of it, and in the face of a crash we
3556f5d8d5c4SChristoph Hellwig 	 * wouldn't have all the data we need to recover.
35571da177e4SLinus Torvalds 	 *
3558f5d8d5c4SChristoph Hellwig 	 * What we do is move the bits to the ili_last_fields field.  When
3559f5d8d5c4SChristoph Hellwig 	 * logging the inode, these bits are moved back to the ili_fields field.
3560f5d8d5c4SChristoph Hellwig 	 * In the xfs_iflush_done() routine we clear ili_last_fields, since we
3561f5d8d5c4SChristoph Hellwig 	 * know that the information those bits represent is permanently on
3562f5d8d5c4SChristoph Hellwig 	 * disk.  As long as the flush completes before the inode is logged
3563f5d8d5c4SChristoph Hellwig 	 * again, then both ili_fields and ili_last_fields will be cleared.
35641da177e4SLinus Torvalds 	 *
3565f5d8d5c4SChristoph Hellwig 	 * We can play with the ili_fields bits here, because the inode lock
3566f5d8d5c4SChristoph Hellwig 	 * must be held exclusively in order to set bits there and the flush
3567f5d8d5c4SChristoph Hellwig 	 * lock protects the ili_last_fields bits.  Set ili_logged so the flush
3568f5d8d5c4SChristoph Hellwig 	 * done routine can tell whether or not to look in the AIL.  Also, store
3569f5d8d5c4SChristoph Hellwig 	 * the current LSN of the inode so that we can tell whether the item has
3570f5d8d5c4SChristoph Hellwig 	 * moved in the AIL from xfs_iflush_done().  In order to read the lsn we
3571f5d8d5c4SChristoph Hellwig 	 * need the AIL lock, because it is a 64 bit value that cannot be read
3572f5d8d5c4SChristoph Hellwig 	 * atomically.
35731da177e4SLinus Torvalds 	 */
3574f5d8d5c4SChristoph Hellwig 	iip->ili_last_fields = iip->ili_fields;
3575f5d8d5c4SChristoph Hellwig 	iip->ili_fields = 0;
3576fc0561ceSDave Chinner 	iip->ili_fsync_fields = 0;
35771da177e4SLinus Torvalds 	iip->ili_logged = 1;
35781da177e4SLinus Torvalds 
35797b2e2a31SDavid Chinner 	xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
35807b2e2a31SDavid Chinner 				&iip->ili_item.li_lsn);
35811da177e4SLinus Torvalds 
35821da177e4SLinus Torvalds 	/*
35831da177e4SLinus Torvalds 	 * Attach the function xfs_iflush_done to the inode's
35841da177e4SLinus Torvalds 	 * buffer.  This will remove the inode from the AIL
35851da177e4SLinus Torvalds 	 * and unlock the inode's flush lock when the inode is
35861da177e4SLinus Torvalds 	 * completely written to disk.
35871da177e4SLinus Torvalds 	 */
3588ca30b2a7SChristoph Hellwig 	xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
35891da177e4SLinus Torvalds 
359093848a99SChristoph Hellwig 	/* generate the checksum. */
359193848a99SChristoph Hellwig 	xfs_dinode_calc_crc(mp, dip);
359293848a99SChristoph Hellwig 
3593643c8c05SCarlos Maiolino 	ASSERT(!list_empty(&bp->b_li_list));
3594cb669ca5SChristoph Hellwig 	ASSERT(bp->b_iodone != NULL);
35951da177e4SLinus Torvalds 	return 0;
35961da177e4SLinus Torvalds 
35971da177e4SLinus Torvalds corrupt_out:
35982451337dSDave Chinner 	return -EFSCORRUPTED;
35991da177e4SLinus Torvalds }
3600