xref: /openbmc/linux/fs/xfs/xfs_inode.c (revision 36b21dde6e899d2f25077b7c239d2d6bcff17d73)
11da177e4SLinus Torvalds /*
23e57ecf6SOlaf Weber  * Copyright (c) 2000-2006 Silicon Graphics, Inc.
37b718769SNathan Scott  * All Rights Reserved.
41da177e4SLinus Torvalds  *
57b718769SNathan Scott  * This program is free software; you can redistribute it and/or
67b718769SNathan Scott  * modify it under the terms of the GNU General Public License as
71da177e4SLinus Torvalds  * published by the Free Software Foundation.
81da177e4SLinus Torvalds  *
97b718769SNathan Scott  * This program is distributed in the hope that it would be useful,
107b718769SNathan Scott  * but WITHOUT ANY WARRANTY; without even the implied warranty of
117b718769SNathan Scott  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
127b718769SNathan Scott  * GNU General Public License for more details.
131da177e4SLinus Torvalds  *
147b718769SNathan Scott  * You should have received a copy of the GNU General Public License
157b718769SNathan Scott  * along with this program; if not, write the Free Software Foundation,
167b718769SNathan Scott  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
171da177e4SLinus Torvalds  */
1840ebd81dSRobert P. J. Day #include <linux/log2.h>
1940ebd81dSRobert P. J. Day 
201da177e4SLinus Torvalds #include "xfs.h"
21a844f451SNathan Scott #include "xfs_fs.h"
226ca1c906SDave Chinner #include "xfs_format.h"
231da177e4SLinus Torvalds #include "xfs_log.h"
24a844f451SNathan Scott #include "xfs_inum.h"
251da177e4SLinus Torvalds #include "xfs_trans.h"
26c24b5dfaSDave Chinner #include "xfs_trans_space.h"
271da177e4SLinus Torvalds #include "xfs_trans_priv.h"
281da177e4SLinus Torvalds #include "xfs_sb.h"
291da177e4SLinus Torvalds #include "xfs_ag.h"
301da177e4SLinus Torvalds #include "xfs_mount.h"
31c24b5dfaSDave Chinner #include "xfs_da_btree.h"
32c24b5dfaSDave Chinner #include "xfs_dir2_format.h"
33c24b5dfaSDave Chinner #include "xfs_dir2.h"
341da177e4SLinus Torvalds #include "xfs_bmap_btree.h"
35a844f451SNathan Scott #include "xfs_alloc_btree.h"
361da177e4SLinus Torvalds #include "xfs_ialloc_btree.h"
37a844f451SNathan Scott #include "xfs_attr_sf.h"
38c24b5dfaSDave Chinner #include "xfs_attr.h"
391da177e4SLinus Torvalds #include "xfs_dinode.h"
401da177e4SLinus Torvalds #include "xfs_inode.h"
411da177e4SLinus Torvalds #include "xfs_buf_item.h"
42a844f451SNathan Scott #include "xfs_inode_item.h"
43a844f451SNathan Scott #include "xfs_btree.h"
44a844f451SNathan Scott #include "xfs_alloc.h"
45a844f451SNathan Scott #include "xfs_ialloc.h"
46a844f451SNathan Scott #include "xfs_bmap.h"
4768988114SDave Chinner #include "xfs_bmap_util.h"
481da177e4SLinus Torvalds #include "xfs_error.h"
491da177e4SLinus Torvalds #include "xfs_quota.h"
502a82b8beSDavid Chinner #include "xfs_filestream.h"
5193848a99SChristoph Hellwig #include "xfs_cksum.h"
520b1b213fSChristoph Hellwig #include "xfs_trace.h"
5333479e05SDave Chinner #include "xfs_icache.h"
54c24b5dfaSDave Chinner #include "xfs_symlink.h"
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds kmem_zone_t *xfs_inode_zone;
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds /*
598f04c47aSChristoph Hellwig  * Used in xfs_itruncate_extents().  This is the maximum number of extents
601da177e4SLinus Torvalds  * freed from a file in a single transaction.
611da177e4SLinus Torvalds  */
621da177e4SLinus Torvalds #define	XFS_ITRUNC_MAX_EXTENTS	2
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds STATIC int xfs_iflush_int(xfs_inode_t *, xfs_buf_t *);
651da177e4SLinus Torvalds 
662a0ec1d9SDave Chinner /*
672a0ec1d9SDave Chinner  * helper function to extract extent size hint from inode
682a0ec1d9SDave Chinner  */
692a0ec1d9SDave Chinner xfs_extlen_t
702a0ec1d9SDave Chinner xfs_get_extsz_hint(
712a0ec1d9SDave Chinner 	struct xfs_inode	*ip)
722a0ec1d9SDave Chinner {
732a0ec1d9SDave Chinner 	if ((ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE) && ip->i_d.di_extsize)
742a0ec1d9SDave Chinner 		return ip->i_d.di_extsize;
752a0ec1d9SDave Chinner 	if (XFS_IS_REALTIME_INODE(ip))
762a0ec1d9SDave Chinner 		return ip->i_mount->m_sb.sb_rextsize;
772a0ec1d9SDave Chinner 	return 0;
782a0ec1d9SDave Chinner }
792a0ec1d9SDave Chinner 
80fa96acadSDave Chinner /*
81fa96acadSDave Chinner  * This is a wrapper routine around the xfs_ilock() routine used to centralize
82fa96acadSDave Chinner  * some grungy code.  It is used in places that wish to lock the inode solely
83fa96acadSDave Chinner  * for reading the extents.  The reason these places can't just call
84fa96acadSDave Chinner  * xfs_ilock(SHARED) is that the inode lock also guards to bringing in of the
85fa96acadSDave Chinner  * extents from disk for a file in b-tree format.  If the inode is in b-tree
86fa96acadSDave Chinner  * format, then we need to lock the inode exclusively until the extents are read
87fa96acadSDave Chinner  * in.  Locking it exclusively all the time would limit our parallelism
88fa96acadSDave Chinner  * unnecessarily, though.  What we do instead is check to see if the extents
89fa96acadSDave Chinner  * have been read in yet, and only lock the inode exclusively if they have not.
90fa96acadSDave Chinner  *
91fa96acadSDave Chinner  * The function returns a value which should be given to the corresponding
92fa96acadSDave Chinner  * xfs_iunlock_map_shared().  This value is the mode in which the lock was
93fa96acadSDave Chinner  * actually taken.
94fa96acadSDave Chinner  */
95fa96acadSDave Chinner uint
96fa96acadSDave Chinner xfs_ilock_map_shared(
97fa96acadSDave Chinner 	xfs_inode_t	*ip)
98fa96acadSDave Chinner {
99fa96acadSDave Chinner 	uint	lock_mode;
100fa96acadSDave Chinner 
101fa96acadSDave Chinner 	if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
102fa96acadSDave Chinner 	    ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
103fa96acadSDave Chinner 		lock_mode = XFS_ILOCK_EXCL;
104fa96acadSDave Chinner 	} else {
105fa96acadSDave Chinner 		lock_mode = XFS_ILOCK_SHARED;
106fa96acadSDave Chinner 	}
107fa96acadSDave Chinner 
108fa96acadSDave Chinner 	xfs_ilock(ip, lock_mode);
109fa96acadSDave Chinner 
110fa96acadSDave Chinner 	return lock_mode;
111fa96acadSDave Chinner }
112fa96acadSDave Chinner 
113fa96acadSDave Chinner /*
114fa96acadSDave Chinner  * This is simply the unlock routine to go with xfs_ilock_map_shared().
115fa96acadSDave Chinner  * All it does is call xfs_iunlock() with the given lock_mode.
116fa96acadSDave Chinner  */
117fa96acadSDave Chinner void
118fa96acadSDave Chinner xfs_iunlock_map_shared(
119fa96acadSDave Chinner 	xfs_inode_t	*ip,
120fa96acadSDave Chinner 	unsigned int	lock_mode)
121fa96acadSDave Chinner {
122fa96acadSDave Chinner 	xfs_iunlock(ip, lock_mode);
123fa96acadSDave Chinner }
124fa96acadSDave Chinner 
125fa96acadSDave Chinner /*
126fa96acadSDave Chinner  * The xfs inode contains 2 locks: a multi-reader lock called the
127fa96acadSDave Chinner  * i_iolock and a multi-reader lock called the i_lock.  This routine
128fa96acadSDave Chinner  * allows either or both of the locks to be obtained.
129fa96acadSDave Chinner  *
130fa96acadSDave Chinner  * The 2 locks should always be ordered so that the IO lock is
131fa96acadSDave Chinner  * obtained first in order to prevent deadlock.
132fa96acadSDave Chinner  *
133fa96acadSDave Chinner  * ip -- the inode being locked
134fa96acadSDave Chinner  * lock_flags -- this parameter indicates the inode's locks
135fa96acadSDave Chinner  *       to be locked.  It can be:
136fa96acadSDave Chinner  *		XFS_IOLOCK_SHARED,
137fa96acadSDave Chinner  *		XFS_IOLOCK_EXCL,
138fa96acadSDave Chinner  *		XFS_ILOCK_SHARED,
139fa96acadSDave Chinner  *		XFS_ILOCK_EXCL,
140fa96acadSDave Chinner  *		XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
141fa96acadSDave Chinner  *		XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
142fa96acadSDave Chinner  *		XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
143fa96acadSDave Chinner  *		XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
144fa96acadSDave Chinner  */
145fa96acadSDave Chinner void
146fa96acadSDave Chinner xfs_ilock(
147fa96acadSDave Chinner 	xfs_inode_t		*ip,
148fa96acadSDave Chinner 	uint			lock_flags)
149fa96acadSDave Chinner {
150fa96acadSDave Chinner 	trace_xfs_ilock(ip, lock_flags, _RET_IP_);
151fa96acadSDave Chinner 
152fa96acadSDave Chinner 	/*
153fa96acadSDave Chinner 	 * You can't set both SHARED and EXCL for the same lock,
154fa96acadSDave Chinner 	 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
155fa96acadSDave Chinner 	 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
156fa96acadSDave Chinner 	 */
157fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
158fa96acadSDave Chinner 	       (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
159fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
160fa96acadSDave Chinner 	       (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
161fa96acadSDave Chinner 	ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
162fa96acadSDave Chinner 
163fa96acadSDave Chinner 	if (lock_flags & XFS_IOLOCK_EXCL)
164fa96acadSDave Chinner 		mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
165fa96acadSDave Chinner 	else if (lock_flags & XFS_IOLOCK_SHARED)
166fa96acadSDave Chinner 		mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
167fa96acadSDave Chinner 
168fa96acadSDave Chinner 	if (lock_flags & XFS_ILOCK_EXCL)
169fa96acadSDave Chinner 		mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
170fa96acadSDave Chinner 	else if (lock_flags & XFS_ILOCK_SHARED)
171fa96acadSDave Chinner 		mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
172fa96acadSDave Chinner }
173fa96acadSDave Chinner 
174fa96acadSDave Chinner /*
175fa96acadSDave Chinner  * This is just like xfs_ilock(), except that the caller
176fa96acadSDave Chinner  * is guaranteed not to sleep.  It returns 1 if it gets
177fa96acadSDave Chinner  * the requested locks and 0 otherwise.  If the IO lock is
178fa96acadSDave Chinner  * obtained but the inode lock cannot be, then the IO lock
179fa96acadSDave Chinner  * is dropped before returning.
180fa96acadSDave Chinner  *
181fa96acadSDave Chinner  * ip -- the inode being locked
182fa96acadSDave Chinner  * lock_flags -- this parameter indicates the inode's locks to be
183fa96acadSDave Chinner  *       to be locked.  See the comment for xfs_ilock() for a list
184fa96acadSDave Chinner  *	 of valid values.
185fa96acadSDave Chinner  */
186fa96acadSDave Chinner int
187fa96acadSDave Chinner xfs_ilock_nowait(
188fa96acadSDave Chinner 	xfs_inode_t		*ip,
189fa96acadSDave Chinner 	uint			lock_flags)
190fa96acadSDave Chinner {
191fa96acadSDave Chinner 	trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_);
192fa96acadSDave Chinner 
193fa96acadSDave Chinner 	/*
194fa96acadSDave Chinner 	 * You can't set both SHARED and EXCL for the same lock,
195fa96acadSDave Chinner 	 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
196fa96acadSDave Chinner 	 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
197fa96acadSDave Chinner 	 */
198fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
199fa96acadSDave Chinner 	       (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
200fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
201fa96acadSDave Chinner 	       (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
202fa96acadSDave Chinner 	ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
203fa96acadSDave Chinner 
204fa96acadSDave Chinner 	if (lock_flags & XFS_IOLOCK_EXCL) {
205fa96acadSDave Chinner 		if (!mrtryupdate(&ip->i_iolock))
206fa96acadSDave Chinner 			goto out;
207fa96acadSDave Chinner 	} else if (lock_flags & XFS_IOLOCK_SHARED) {
208fa96acadSDave Chinner 		if (!mrtryaccess(&ip->i_iolock))
209fa96acadSDave Chinner 			goto out;
210fa96acadSDave Chinner 	}
211fa96acadSDave Chinner 	if (lock_flags & XFS_ILOCK_EXCL) {
212fa96acadSDave Chinner 		if (!mrtryupdate(&ip->i_lock))
213fa96acadSDave Chinner 			goto out_undo_iolock;
214fa96acadSDave Chinner 	} else if (lock_flags & XFS_ILOCK_SHARED) {
215fa96acadSDave Chinner 		if (!mrtryaccess(&ip->i_lock))
216fa96acadSDave Chinner 			goto out_undo_iolock;
217fa96acadSDave Chinner 	}
218fa96acadSDave Chinner 	return 1;
219fa96acadSDave Chinner 
220fa96acadSDave Chinner  out_undo_iolock:
221fa96acadSDave Chinner 	if (lock_flags & XFS_IOLOCK_EXCL)
222fa96acadSDave Chinner 		mrunlock_excl(&ip->i_iolock);
223fa96acadSDave Chinner 	else if (lock_flags & XFS_IOLOCK_SHARED)
224fa96acadSDave Chinner 		mrunlock_shared(&ip->i_iolock);
225fa96acadSDave Chinner  out:
226fa96acadSDave Chinner 	return 0;
227fa96acadSDave Chinner }
228fa96acadSDave Chinner 
229fa96acadSDave Chinner /*
230fa96acadSDave Chinner  * xfs_iunlock() is used to drop the inode locks acquired with
231fa96acadSDave Chinner  * xfs_ilock() and xfs_ilock_nowait().  The caller must pass
232fa96acadSDave Chinner  * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
233fa96acadSDave Chinner  * that we know which locks to drop.
234fa96acadSDave Chinner  *
235fa96acadSDave Chinner  * ip -- the inode being unlocked
236fa96acadSDave Chinner  * lock_flags -- this parameter indicates the inode's locks to be
237fa96acadSDave Chinner  *       to be unlocked.  See the comment for xfs_ilock() for a list
238fa96acadSDave Chinner  *	 of valid values for this parameter.
239fa96acadSDave Chinner  *
240fa96acadSDave Chinner  */
241fa96acadSDave Chinner void
242fa96acadSDave Chinner xfs_iunlock(
243fa96acadSDave Chinner 	xfs_inode_t		*ip,
244fa96acadSDave Chinner 	uint			lock_flags)
245fa96acadSDave Chinner {
246fa96acadSDave Chinner 	/*
247fa96acadSDave Chinner 	 * You can't set both SHARED and EXCL for the same lock,
248fa96acadSDave Chinner 	 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
249fa96acadSDave Chinner 	 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
250fa96acadSDave Chinner 	 */
251fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
252fa96acadSDave Chinner 	       (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
253fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
254fa96acadSDave Chinner 	       (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
255fa96acadSDave Chinner 	ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
256fa96acadSDave Chinner 	ASSERT(lock_flags != 0);
257fa96acadSDave Chinner 
258fa96acadSDave Chinner 	if (lock_flags & XFS_IOLOCK_EXCL)
259fa96acadSDave Chinner 		mrunlock_excl(&ip->i_iolock);
260fa96acadSDave Chinner 	else if (lock_flags & XFS_IOLOCK_SHARED)
261fa96acadSDave Chinner 		mrunlock_shared(&ip->i_iolock);
262fa96acadSDave Chinner 
263fa96acadSDave Chinner 	if (lock_flags & XFS_ILOCK_EXCL)
264fa96acadSDave Chinner 		mrunlock_excl(&ip->i_lock);
265fa96acadSDave Chinner 	else if (lock_flags & XFS_ILOCK_SHARED)
266fa96acadSDave Chinner 		mrunlock_shared(&ip->i_lock);
267fa96acadSDave Chinner 
268fa96acadSDave Chinner 	trace_xfs_iunlock(ip, lock_flags, _RET_IP_);
269fa96acadSDave Chinner }
270fa96acadSDave Chinner 
271fa96acadSDave Chinner /*
272fa96acadSDave Chinner  * give up write locks.  the i/o lock cannot be held nested
273fa96acadSDave Chinner  * if it is being demoted.
274fa96acadSDave Chinner  */
275fa96acadSDave Chinner void
276fa96acadSDave Chinner xfs_ilock_demote(
277fa96acadSDave Chinner 	xfs_inode_t		*ip,
278fa96acadSDave Chinner 	uint			lock_flags)
279fa96acadSDave Chinner {
280fa96acadSDave Chinner 	ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
281fa96acadSDave Chinner 	ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
282fa96acadSDave Chinner 
283fa96acadSDave Chinner 	if (lock_flags & XFS_ILOCK_EXCL)
284fa96acadSDave Chinner 		mrdemote(&ip->i_lock);
285fa96acadSDave Chinner 	if (lock_flags & XFS_IOLOCK_EXCL)
286fa96acadSDave Chinner 		mrdemote(&ip->i_iolock);
287fa96acadSDave Chinner 
288fa96acadSDave Chinner 	trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_);
289fa96acadSDave Chinner }
290fa96acadSDave Chinner 
291742ae1e3SDave Chinner #if defined(DEBUG) || defined(XFS_WARN)
292fa96acadSDave Chinner int
293fa96acadSDave Chinner xfs_isilocked(
294fa96acadSDave Chinner 	xfs_inode_t		*ip,
295fa96acadSDave Chinner 	uint			lock_flags)
296fa96acadSDave Chinner {
297fa96acadSDave Chinner 	if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) {
298fa96acadSDave Chinner 		if (!(lock_flags & XFS_ILOCK_SHARED))
299fa96acadSDave Chinner 			return !!ip->i_lock.mr_writer;
300fa96acadSDave Chinner 		return rwsem_is_locked(&ip->i_lock.mr_lock);
301fa96acadSDave Chinner 	}
302fa96acadSDave Chinner 
303fa96acadSDave Chinner 	if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) {
304fa96acadSDave Chinner 		if (!(lock_flags & XFS_IOLOCK_SHARED))
305fa96acadSDave Chinner 			return !!ip->i_iolock.mr_writer;
306fa96acadSDave Chinner 		return rwsem_is_locked(&ip->i_iolock.mr_lock);
307fa96acadSDave Chinner 	}
308fa96acadSDave Chinner 
309fa96acadSDave Chinner 	ASSERT(0);
310fa96acadSDave Chinner 	return 0;
311fa96acadSDave Chinner }
312fa96acadSDave Chinner #endif
313fa96acadSDave Chinner 
314c24b5dfaSDave Chinner #ifdef DEBUG
315c24b5dfaSDave Chinner int xfs_locked_n;
316c24b5dfaSDave Chinner int xfs_small_retries;
317c24b5dfaSDave Chinner int xfs_middle_retries;
318c24b5dfaSDave Chinner int xfs_lots_retries;
319c24b5dfaSDave Chinner int xfs_lock_delays;
320c24b5dfaSDave Chinner #endif
321c24b5dfaSDave Chinner 
322c24b5dfaSDave Chinner /*
323c24b5dfaSDave Chinner  * Bump the subclass so xfs_lock_inodes() acquires each lock with
324c24b5dfaSDave Chinner  * a different value
325c24b5dfaSDave Chinner  */
326c24b5dfaSDave Chinner static inline int
327c24b5dfaSDave Chinner xfs_lock_inumorder(int lock_mode, int subclass)
328c24b5dfaSDave Chinner {
329c24b5dfaSDave Chinner 	if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
330c24b5dfaSDave Chinner 		lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
331c24b5dfaSDave Chinner 	if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
332c24b5dfaSDave Chinner 		lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
333c24b5dfaSDave Chinner 
334c24b5dfaSDave Chinner 	return lock_mode;
335c24b5dfaSDave Chinner }
336c24b5dfaSDave Chinner 
337c24b5dfaSDave Chinner /*
338c24b5dfaSDave Chinner  * The following routine will lock n inodes in exclusive mode.
339c24b5dfaSDave Chinner  * We assume the caller calls us with the inodes in i_ino order.
340c24b5dfaSDave Chinner  *
341c24b5dfaSDave Chinner  * We need to detect deadlock where an inode that we lock
342c24b5dfaSDave Chinner  * is in the AIL and we start waiting for another inode that is locked
343c24b5dfaSDave Chinner  * by a thread in a long running transaction (such as truncate). This can
344c24b5dfaSDave Chinner  * result in deadlock since the long running trans might need to wait
345c24b5dfaSDave Chinner  * for the inode we just locked in order to push the tail and free space
346c24b5dfaSDave Chinner  * in the log.
347c24b5dfaSDave Chinner  */
348c24b5dfaSDave Chinner void
349c24b5dfaSDave Chinner xfs_lock_inodes(
350c24b5dfaSDave Chinner 	xfs_inode_t	**ips,
351c24b5dfaSDave Chinner 	int		inodes,
352c24b5dfaSDave Chinner 	uint		lock_mode)
353c24b5dfaSDave Chinner {
354c24b5dfaSDave Chinner 	int		attempts = 0, i, j, try_lock;
355c24b5dfaSDave Chinner 	xfs_log_item_t	*lp;
356c24b5dfaSDave Chinner 
357c24b5dfaSDave Chinner 	ASSERT(ips && (inodes >= 2)); /* we need at least two */
358c24b5dfaSDave Chinner 
359c24b5dfaSDave Chinner 	try_lock = 0;
360c24b5dfaSDave Chinner 	i = 0;
361c24b5dfaSDave Chinner 
362c24b5dfaSDave Chinner again:
363c24b5dfaSDave Chinner 	for (; i < inodes; i++) {
364c24b5dfaSDave Chinner 		ASSERT(ips[i]);
365c24b5dfaSDave Chinner 
366c24b5dfaSDave Chinner 		if (i && (ips[i] == ips[i-1]))	/* Already locked */
367c24b5dfaSDave Chinner 			continue;
368c24b5dfaSDave Chinner 
369c24b5dfaSDave Chinner 		/*
370c24b5dfaSDave Chinner 		 * If try_lock is not set yet, make sure all locked inodes
371c24b5dfaSDave Chinner 		 * are not in the AIL.
372c24b5dfaSDave Chinner 		 * If any are, set try_lock to be used later.
373c24b5dfaSDave Chinner 		 */
374c24b5dfaSDave Chinner 
375c24b5dfaSDave Chinner 		if (!try_lock) {
376c24b5dfaSDave Chinner 			for (j = (i - 1); j >= 0 && !try_lock; j--) {
377c24b5dfaSDave Chinner 				lp = (xfs_log_item_t *)ips[j]->i_itemp;
378c24b5dfaSDave Chinner 				if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
379c24b5dfaSDave Chinner 					try_lock++;
380c24b5dfaSDave Chinner 				}
381c24b5dfaSDave Chinner 			}
382c24b5dfaSDave Chinner 		}
383c24b5dfaSDave Chinner 
384c24b5dfaSDave Chinner 		/*
385c24b5dfaSDave Chinner 		 * If any of the previous locks we have locked is in the AIL,
386c24b5dfaSDave Chinner 		 * we must TRY to get the second and subsequent locks. If
387c24b5dfaSDave Chinner 		 * we can't get any, we must release all we have
388c24b5dfaSDave Chinner 		 * and try again.
389c24b5dfaSDave Chinner 		 */
390c24b5dfaSDave Chinner 
391c24b5dfaSDave Chinner 		if (try_lock) {
392c24b5dfaSDave Chinner 			/* try_lock must be 0 if i is 0. */
393c24b5dfaSDave Chinner 			/*
394c24b5dfaSDave Chinner 			 * try_lock means we have an inode locked
395c24b5dfaSDave Chinner 			 * that is in the AIL.
396c24b5dfaSDave Chinner 			 */
397c24b5dfaSDave Chinner 			ASSERT(i != 0);
398c24b5dfaSDave Chinner 			if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
399c24b5dfaSDave Chinner 				attempts++;
400c24b5dfaSDave Chinner 
401c24b5dfaSDave Chinner 				/*
402c24b5dfaSDave Chinner 				 * Unlock all previous guys and try again.
403c24b5dfaSDave Chinner 				 * xfs_iunlock will try to push the tail
404c24b5dfaSDave Chinner 				 * if the inode is in the AIL.
405c24b5dfaSDave Chinner 				 */
406c24b5dfaSDave Chinner 
407c24b5dfaSDave Chinner 				for(j = i - 1; j >= 0; j--) {
408c24b5dfaSDave Chinner 
409c24b5dfaSDave Chinner 					/*
410c24b5dfaSDave Chinner 					 * Check to see if we've already
411c24b5dfaSDave Chinner 					 * unlocked this one.
412c24b5dfaSDave Chinner 					 * Not the first one going back,
413c24b5dfaSDave Chinner 					 * and the inode ptr is the same.
414c24b5dfaSDave Chinner 					 */
415c24b5dfaSDave Chinner 					if ((j != (i - 1)) && ips[j] ==
416c24b5dfaSDave Chinner 								ips[j+1])
417c24b5dfaSDave Chinner 						continue;
418c24b5dfaSDave Chinner 
419c24b5dfaSDave Chinner 					xfs_iunlock(ips[j], lock_mode);
420c24b5dfaSDave Chinner 				}
421c24b5dfaSDave Chinner 
422c24b5dfaSDave Chinner 				if ((attempts % 5) == 0) {
423c24b5dfaSDave Chinner 					delay(1); /* Don't just spin the CPU */
424c24b5dfaSDave Chinner #ifdef DEBUG
425c24b5dfaSDave Chinner 					xfs_lock_delays++;
426c24b5dfaSDave Chinner #endif
427c24b5dfaSDave Chinner 				}
428c24b5dfaSDave Chinner 				i = 0;
429c24b5dfaSDave Chinner 				try_lock = 0;
430c24b5dfaSDave Chinner 				goto again;
431c24b5dfaSDave Chinner 			}
432c24b5dfaSDave Chinner 		} else {
433c24b5dfaSDave Chinner 			xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
434c24b5dfaSDave Chinner 		}
435c24b5dfaSDave Chinner 	}
436c24b5dfaSDave Chinner 
437c24b5dfaSDave Chinner #ifdef DEBUG
438c24b5dfaSDave Chinner 	if (attempts) {
439c24b5dfaSDave Chinner 		if (attempts < 5) xfs_small_retries++;
440c24b5dfaSDave Chinner 		else if (attempts < 100) xfs_middle_retries++;
441c24b5dfaSDave Chinner 		else xfs_lots_retries++;
442c24b5dfaSDave Chinner 	} else {
443c24b5dfaSDave Chinner 		xfs_locked_n++;
444c24b5dfaSDave Chinner 	}
445c24b5dfaSDave Chinner #endif
446c24b5dfaSDave Chinner }
447c24b5dfaSDave Chinner 
448c24b5dfaSDave Chinner /*
449c24b5dfaSDave Chinner  * xfs_lock_two_inodes() can only be used to lock one type of lock
450c24b5dfaSDave Chinner  * at a time - the iolock or the ilock, but not both at once. If
451c24b5dfaSDave Chinner  * we lock both at once, lockdep will report false positives saying
452c24b5dfaSDave Chinner  * we have violated locking orders.
453c24b5dfaSDave Chinner  */
454c24b5dfaSDave Chinner void
455c24b5dfaSDave Chinner xfs_lock_two_inodes(
456c24b5dfaSDave Chinner 	xfs_inode_t		*ip0,
457c24b5dfaSDave Chinner 	xfs_inode_t		*ip1,
458c24b5dfaSDave Chinner 	uint			lock_mode)
459c24b5dfaSDave Chinner {
460c24b5dfaSDave Chinner 	xfs_inode_t		*temp;
461c24b5dfaSDave Chinner 	int			attempts = 0;
462c24b5dfaSDave Chinner 	xfs_log_item_t		*lp;
463c24b5dfaSDave Chinner 
464c24b5dfaSDave Chinner 	if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
465c24b5dfaSDave Chinner 		ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
466c24b5dfaSDave Chinner 	ASSERT(ip0->i_ino != ip1->i_ino);
467c24b5dfaSDave Chinner 
468c24b5dfaSDave Chinner 	if (ip0->i_ino > ip1->i_ino) {
469c24b5dfaSDave Chinner 		temp = ip0;
470c24b5dfaSDave Chinner 		ip0 = ip1;
471c24b5dfaSDave Chinner 		ip1 = temp;
472c24b5dfaSDave Chinner 	}
473c24b5dfaSDave Chinner 
474c24b5dfaSDave Chinner  again:
475c24b5dfaSDave Chinner 	xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
476c24b5dfaSDave Chinner 
477c24b5dfaSDave Chinner 	/*
478c24b5dfaSDave Chinner 	 * If the first lock we have locked is in the AIL, we must TRY to get
479c24b5dfaSDave Chinner 	 * the second lock. If we can't get it, we must release the first one
480c24b5dfaSDave Chinner 	 * and try again.
481c24b5dfaSDave Chinner 	 */
482c24b5dfaSDave Chinner 	lp = (xfs_log_item_t *)ip0->i_itemp;
483c24b5dfaSDave Chinner 	if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
484c24b5dfaSDave Chinner 		if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
485c24b5dfaSDave Chinner 			xfs_iunlock(ip0, lock_mode);
486c24b5dfaSDave Chinner 			if ((++attempts % 5) == 0)
487c24b5dfaSDave Chinner 				delay(1); /* Don't just spin the CPU */
488c24b5dfaSDave Chinner 			goto again;
489c24b5dfaSDave Chinner 		}
490c24b5dfaSDave Chinner 	} else {
491c24b5dfaSDave Chinner 		xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
492c24b5dfaSDave Chinner 	}
493c24b5dfaSDave Chinner }
494c24b5dfaSDave Chinner 
495c24b5dfaSDave Chinner 
496fa96acadSDave Chinner void
497fa96acadSDave Chinner __xfs_iflock(
498fa96acadSDave Chinner 	struct xfs_inode	*ip)
499fa96acadSDave Chinner {
500fa96acadSDave Chinner 	wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IFLOCK_BIT);
501fa96acadSDave Chinner 	DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IFLOCK_BIT);
502fa96acadSDave Chinner 
503fa96acadSDave Chinner 	do {
504fa96acadSDave Chinner 		prepare_to_wait_exclusive(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
505fa96acadSDave Chinner 		if (xfs_isiflocked(ip))
506fa96acadSDave Chinner 			io_schedule();
507fa96acadSDave Chinner 	} while (!xfs_iflock_nowait(ip));
508fa96acadSDave Chinner 
509fa96acadSDave Chinner 	finish_wait(wq, &wait.wait);
510fa96acadSDave Chinner }
511fa96acadSDave Chinner 
5121da177e4SLinus Torvalds STATIC uint
5131da177e4SLinus Torvalds _xfs_dic2xflags(
5141da177e4SLinus Torvalds 	__uint16_t		di_flags)
5151da177e4SLinus Torvalds {
5161da177e4SLinus Torvalds 	uint			flags = 0;
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 	if (di_flags & XFS_DIFLAG_ANY) {
5191da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_REALTIME)
5201da177e4SLinus Torvalds 			flags |= XFS_XFLAG_REALTIME;
5211da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_PREALLOC)
5221da177e4SLinus Torvalds 			flags |= XFS_XFLAG_PREALLOC;
5231da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_IMMUTABLE)
5241da177e4SLinus Torvalds 			flags |= XFS_XFLAG_IMMUTABLE;
5251da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_APPEND)
5261da177e4SLinus Torvalds 			flags |= XFS_XFLAG_APPEND;
5271da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_SYNC)
5281da177e4SLinus Torvalds 			flags |= XFS_XFLAG_SYNC;
5291da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_NOATIME)
5301da177e4SLinus Torvalds 			flags |= XFS_XFLAG_NOATIME;
5311da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_NODUMP)
5321da177e4SLinus Torvalds 			flags |= XFS_XFLAG_NODUMP;
5331da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_RTINHERIT)
5341da177e4SLinus Torvalds 			flags |= XFS_XFLAG_RTINHERIT;
5351da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_PROJINHERIT)
5361da177e4SLinus Torvalds 			flags |= XFS_XFLAG_PROJINHERIT;
5371da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_NOSYMLINKS)
5381da177e4SLinus Torvalds 			flags |= XFS_XFLAG_NOSYMLINKS;
539dd9f438eSNathan Scott 		if (di_flags & XFS_DIFLAG_EXTSIZE)
540dd9f438eSNathan Scott 			flags |= XFS_XFLAG_EXTSIZE;
541dd9f438eSNathan Scott 		if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
542dd9f438eSNathan Scott 			flags |= XFS_XFLAG_EXTSZINHERIT;
543d3446eacSBarry Naujok 		if (di_flags & XFS_DIFLAG_NODEFRAG)
544d3446eacSBarry Naujok 			flags |= XFS_XFLAG_NODEFRAG;
5452a82b8beSDavid Chinner 		if (di_flags & XFS_DIFLAG_FILESTREAM)
5462a82b8beSDavid Chinner 			flags |= XFS_XFLAG_FILESTREAM;
5471da177e4SLinus Torvalds 	}
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds 	return flags;
5501da177e4SLinus Torvalds }
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds uint
5531da177e4SLinus Torvalds xfs_ip2xflags(
5541da177e4SLinus Torvalds 	xfs_inode_t		*ip)
5551da177e4SLinus Torvalds {
556347d1c01SChristoph Hellwig 	xfs_icdinode_t		*dic = &ip->i_d;
5571da177e4SLinus Torvalds 
558a916e2bdSNathan Scott 	return _xfs_dic2xflags(dic->di_flags) |
55945ba598eSChristoph Hellwig 				(XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0);
5601da177e4SLinus Torvalds }
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds uint
5631da177e4SLinus Torvalds xfs_dic2xflags(
56445ba598eSChristoph Hellwig 	xfs_dinode_t		*dip)
5651da177e4SLinus Torvalds {
56681591fe2SChristoph Hellwig 	return _xfs_dic2xflags(be16_to_cpu(dip->di_flags)) |
56745ba598eSChristoph Hellwig 				(XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0);
5681da177e4SLinus Torvalds }
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds /*
571c24b5dfaSDave Chinner  * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
572c24b5dfaSDave Chinner  * is allowed, otherwise it has to be an exact match. If a CI match is found,
573c24b5dfaSDave Chinner  * ci_name->name will point to a the actual name (caller must free) or
574c24b5dfaSDave Chinner  * will be set to NULL if an exact match is found.
575c24b5dfaSDave Chinner  */
576c24b5dfaSDave Chinner int
577c24b5dfaSDave Chinner xfs_lookup(
578c24b5dfaSDave Chinner 	xfs_inode_t		*dp,
579c24b5dfaSDave Chinner 	struct xfs_name		*name,
580c24b5dfaSDave Chinner 	xfs_inode_t		**ipp,
581c24b5dfaSDave Chinner 	struct xfs_name		*ci_name)
582c24b5dfaSDave Chinner {
583c24b5dfaSDave Chinner 	xfs_ino_t		inum;
584c24b5dfaSDave Chinner 	int			error;
585c24b5dfaSDave Chinner 	uint			lock_mode;
586c24b5dfaSDave Chinner 
587c24b5dfaSDave Chinner 	trace_xfs_lookup(dp, name);
588c24b5dfaSDave Chinner 
589c24b5dfaSDave Chinner 	if (XFS_FORCED_SHUTDOWN(dp->i_mount))
590c24b5dfaSDave Chinner 		return XFS_ERROR(EIO);
591c24b5dfaSDave Chinner 
592c24b5dfaSDave Chinner 	lock_mode = xfs_ilock_map_shared(dp);
593c24b5dfaSDave Chinner 	error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
594c24b5dfaSDave Chinner 	xfs_iunlock_map_shared(dp, lock_mode);
595c24b5dfaSDave Chinner 
596c24b5dfaSDave Chinner 	if (error)
597c24b5dfaSDave Chinner 		goto out;
598c24b5dfaSDave Chinner 
599c24b5dfaSDave Chinner 	error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
600c24b5dfaSDave Chinner 	if (error)
601c24b5dfaSDave Chinner 		goto out_free_name;
602c24b5dfaSDave Chinner 
603c24b5dfaSDave Chinner 	return 0;
604c24b5dfaSDave Chinner 
605c24b5dfaSDave Chinner out_free_name:
606c24b5dfaSDave Chinner 	if (ci_name)
607c24b5dfaSDave Chinner 		kmem_free(ci_name->name);
608c24b5dfaSDave Chinner out:
609c24b5dfaSDave Chinner 	*ipp = NULL;
610c24b5dfaSDave Chinner 	return error;
611c24b5dfaSDave Chinner }
612c24b5dfaSDave Chinner 
613c24b5dfaSDave Chinner /*
6141da177e4SLinus Torvalds  * Allocate an inode on disk and return a copy of its in-core version.
6151da177e4SLinus Torvalds  * The in-core inode is locked exclusively.  Set mode, nlink, and rdev
6161da177e4SLinus Torvalds  * appropriately within the inode.  The uid and gid for the inode are
6171da177e4SLinus Torvalds  * set according to the contents of the given cred structure.
6181da177e4SLinus Torvalds  *
6191da177e4SLinus Torvalds  * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
620cd856db6SCarlos Maiolino  * has a free inode available, call xfs_iget() to obtain the in-core
621cd856db6SCarlos Maiolino  * version of the allocated inode.  Finally, fill in the inode and
622cd856db6SCarlos Maiolino  * log its initial contents.  In this case, ialloc_context would be
623cd856db6SCarlos Maiolino  * set to NULL.
6241da177e4SLinus Torvalds  *
625cd856db6SCarlos Maiolino  * If xfs_dialloc() does not have an available inode, it will replenish
626cd856db6SCarlos Maiolino  * its supply by doing an allocation. Since we can only do one
627cd856db6SCarlos Maiolino  * allocation within a transaction without deadlocks, we must commit
628cd856db6SCarlos Maiolino  * the current transaction before returning the inode itself.
629cd856db6SCarlos Maiolino  * In this case, therefore, we will set ialloc_context and return.
6301da177e4SLinus Torvalds  * The caller should then commit the current transaction, start a new
6311da177e4SLinus Torvalds  * transaction, and call xfs_ialloc() again to actually get the inode.
6321da177e4SLinus Torvalds  *
6331da177e4SLinus Torvalds  * To ensure that some other process does not grab the inode that
6341da177e4SLinus Torvalds  * was allocated during the first call to xfs_ialloc(), this routine
6351da177e4SLinus Torvalds  * also returns the [locked] bp pointing to the head of the freelist
6361da177e4SLinus Torvalds  * as ialloc_context.  The caller should hold this buffer across
6371da177e4SLinus Torvalds  * the commit and pass it back into this routine on the second call.
638b11f94d5SDavid Chinner  *
639b11f94d5SDavid Chinner  * If we are allocating quota inodes, we do not have a parent inode
640b11f94d5SDavid Chinner  * to attach to or associate with (i.e. pip == NULL) because they
641b11f94d5SDavid Chinner  * are not linked into the directory structure - they are attached
642b11f94d5SDavid Chinner  * directly to the superblock - and so have no parent.
6431da177e4SLinus Torvalds  */
6441da177e4SLinus Torvalds int
6451da177e4SLinus Torvalds xfs_ialloc(
6461da177e4SLinus Torvalds 	xfs_trans_t	*tp,
6471da177e4SLinus Torvalds 	xfs_inode_t	*pip,
648576b1d67SAl Viro 	umode_t		mode,
64931b084aeSNathan Scott 	xfs_nlink_t	nlink,
6501da177e4SLinus Torvalds 	xfs_dev_t	rdev,
6516743099cSArkadiusz Mi?kiewicz 	prid_t		prid,
6521da177e4SLinus Torvalds 	int		okalloc,
6531da177e4SLinus Torvalds 	xfs_buf_t	**ialloc_context,
6541da177e4SLinus Torvalds 	xfs_inode_t	**ipp)
6551da177e4SLinus Torvalds {
65693848a99SChristoph Hellwig 	struct xfs_mount *mp = tp->t_mountp;
6571da177e4SLinus Torvalds 	xfs_ino_t	ino;
6581da177e4SLinus Torvalds 	xfs_inode_t	*ip;
6591da177e4SLinus Torvalds 	uint		flags;
6601da177e4SLinus Torvalds 	int		error;
661dff35fd4SChristoph Hellwig 	timespec_t	tv;
662bf904248SDavid Chinner 	int		filestreams = 0;
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 	/*
6651da177e4SLinus Torvalds 	 * Call the space management code to pick
6661da177e4SLinus Torvalds 	 * the on-disk inode to be allocated.
6671da177e4SLinus Torvalds 	 */
668b11f94d5SDavid Chinner 	error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
66908358906SChristoph Hellwig 			    ialloc_context, &ino);
670bf904248SDavid Chinner 	if (error)
6711da177e4SLinus Torvalds 		return error;
67208358906SChristoph Hellwig 	if (*ialloc_context || ino == NULLFSINO) {
6731da177e4SLinus Torvalds 		*ipp = NULL;
6741da177e4SLinus Torvalds 		return 0;
6751da177e4SLinus Torvalds 	}
6761da177e4SLinus Torvalds 	ASSERT(*ialloc_context == NULL);
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds 	/*
6791da177e4SLinus Torvalds 	 * Get the in-core inode with the lock held exclusively.
6801da177e4SLinus Torvalds 	 * This is because we're setting fields here we need
6811da177e4SLinus Torvalds 	 * to prevent others from looking at until we're done.
6821da177e4SLinus Torvalds 	 */
68393848a99SChristoph Hellwig 	error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE,
684ec3ba85fSChristoph Hellwig 			 XFS_ILOCK_EXCL, &ip);
685bf904248SDavid Chinner 	if (error)
6861da177e4SLinus Torvalds 		return error;
6871da177e4SLinus Torvalds 	ASSERT(ip != NULL);
6881da177e4SLinus Torvalds 
689576b1d67SAl Viro 	ip->i_d.di_mode = mode;
6901da177e4SLinus Torvalds 	ip->i_d.di_onlink = 0;
6911da177e4SLinus Torvalds 	ip->i_d.di_nlink = nlink;
6921da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_nlink == nlink);
6937aab1b28SDwight Engen 	ip->i_d.di_uid = xfs_kuid_to_uid(current_fsuid());
6947aab1b28SDwight Engen 	ip->i_d.di_gid = xfs_kgid_to_gid(current_fsgid());
6956743099cSArkadiusz Mi?kiewicz 	xfs_set_projid(ip, prid);
6961da177e4SLinus Torvalds 	memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 	/*
6991da177e4SLinus Torvalds 	 * If the superblock version is up to where we support new format
7001da177e4SLinus Torvalds 	 * inodes and this is currently an old format inode, then change
7011da177e4SLinus Torvalds 	 * the inode version number now.  This way we only do the conversion
7021da177e4SLinus Torvalds 	 * here rather than here and in the flush/logging code.
7031da177e4SLinus Torvalds 	 */
70493848a99SChristoph Hellwig 	if (xfs_sb_version_hasnlink(&mp->m_sb) &&
70551ce16d5SChristoph Hellwig 	    ip->i_d.di_version == 1) {
70651ce16d5SChristoph Hellwig 		ip->i_d.di_version = 2;
7071da177e4SLinus Torvalds 		/*
7081da177e4SLinus Torvalds 		 * We've already zeroed the old link count, the projid field,
7091da177e4SLinus Torvalds 		 * and the pad field.
7101da177e4SLinus Torvalds 		 */
7111da177e4SLinus Torvalds 	}
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 	/*
7141da177e4SLinus Torvalds 	 * Project ids won't be stored on disk if we are using a version 1 inode.
7151da177e4SLinus Torvalds 	 */
71651ce16d5SChristoph Hellwig 	if ((prid != 0) && (ip->i_d.di_version == 1))
7171da177e4SLinus Torvalds 		xfs_bump_ino_vers2(tp, ip);
7181da177e4SLinus Torvalds 
719bd186aa9SChristoph Hellwig 	if (pip && XFS_INHERIT_GID(pip)) {
7201da177e4SLinus Torvalds 		ip->i_d.di_gid = pip->i_d.di_gid;
721abbede1bSAl Viro 		if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) {
7221da177e4SLinus Torvalds 			ip->i_d.di_mode |= S_ISGID;
7231da177e4SLinus Torvalds 		}
7241da177e4SLinus Torvalds 	}
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 	/*
7271da177e4SLinus Torvalds 	 * If the group ID of the new file does not match the effective group
7281da177e4SLinus Torvalds 	 * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
7291da177e4SLinus Torvalds 	 * (and only if the irix_sgid_inherit compatibility variable is set).
7301da177e4SLinus Torvalds 	 */
7311da177e4SLinus Torvalds 	if ((irix_sgid_inherit) &&
7321da177e4SLinus Torvalds 	    (ip->i_d.di_mode & S_ISGID) &&
7337aab1b28SDwight Engen 	    (!in_group_p(xfs_gid_to_kgid(ip->i_d.di_gid)))) {
7341da177e4SLinus Torvalds 		ip->i_d.di_mode &= ~S_ISGID;
7351da177e4SLinus Torvalds 	}
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds 	ip->i_d.di_size = 0;
7381da177e4SLinus Torvalds 	ip->i_d.di_nextents = 0;
7391da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_nblocks == 0);
740dff35fd4SChristoph Hellwig 
741dff35fd4SChristoph Hellwig 	nanotime(&tv);
742dff35fd4SChristoph Hellwig 	ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
743dff35fd4SChristoph Hellwig 	ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
744dff35fd4SChristoph Hellwig 	ip->i_d.di_atime = ip->i_d.di_mtime;
745dff35fd4SChristoph Hellwig 	ip->i_d.di_ctime = ip->i_d.di_mtime;
746dff35fd4SChristoph Hellwig 
7471da177e4SLinus Torvalds 	/*
7481da177e4SLinus Torvalds 	 * di_gen will have been taken care of in xfs_iread.
7491da177e4SLinus Torvalds 	 */
7501da177e4SLinus Torvalds 	ip->i_d.di_extsize = 0;
7511da177e4SLinus Torvalds 	ip->i_d.di_dmevmask = 0;
7521da177e4SLinus Torvalds 	ip->i_d.di_dmstate = 0;
7531da177e4SLinus Torvalds 	ip->i_d.di_flags = 0;
75493848a99SChristoph Hellwig 
75593848a99SChristoph Hellwig 	if (ip->i_d.di_version == 3) {
75693848a99SChristoph Hellwig 		ASSERT(ip->i_d.di_ino == ino);
75793848a99SChristoph Hellwig 		ASSERT(uuid_equal(&ip->i_d.di_uuid, &mp->m_sb.sb_uuid));
75893848a99SChristoph Hellwig 		ip->i_d.di_crc = 0;
75993848a99SChristoph Hellwig 		ip->i_d.di_changecount = 1;
76093848a99SChristoph Hellwig 		ip->i_d.di_lsn = 0;
76193848a99SChristoph Hellwig 		ip->i_d.di_flags2 = 0;
76293848a99SChristoph Hellwig 		memset(&(ip->i_d.di_pad2[0]), 0, sizeof(ip->i_d.di_pad2));
76393848a99SChristoph Hellwig 		ip->i_d.di_crtime = ip->i_d.di_mtime;
76493848a99SChristoph Hellwig 	}
76593848a99SChristoph Hellwig 
76693848a99SChristoph Hellwig 
7671da177e4SLinus Torvalds 	flags = XFS_ILOG_CORE;
7681da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
7691da177e4SLinus Torvalds 	case S_IFIFO:
7701da177e4SLinus Torvalds 	case S_IFCHR:
7711da177e4SLinus Torvalds 	case S_IFBLK:
7721da177e4SLinus Torvalds 	case S_IFSOCK:
7731da177e4SLinus Torvalds 		ip->i_d.di_format = XFS_DINODE_FMT_DEV;
7741da177e4SLinus Torvalds 		ip->i_df.if_u2.if_rdev = rdev;
7751da177e4SLinus Torvalds 		ip->i_df.if_flags = 0;
7761da177e4SLinus Torvalds 		flags |= XFS_ILOG_DEV;
7771da177e4SLinus Torvalds 		break;
7781da177e4SLinus Torvalds 	case S_IFREG:
779bf904248SDavid Chinner 		/*
780bf904248SDavid Chinner 		 * we can't set up filestreams until after the VFS inode
781bf904248SDavid Chinner 		 * is set up properly.
782bf904248SDavid Chinner 		 */
783bf904248SDavid Chinner 		if (pip && xfs_inode_is_filestream(pip))
784bf904248SDavid Chinner 			filestreams = 1;
7852a82b8beSDavid Chinner 		/* fall through */
7861da177e4SLinus Torvalds 	case S_IFDIR:
787b11f94d5SDavid Chinner 		if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
788365ca83dSNathan Scott 			uint	di_flags = 0;
789365ca83dSNathan Scott 
790abbede1bSAl Viro 			if (S_ISDIR(mode)) {
791365ca83dSNathan Scott 				if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
792365ca83dSNathan Scott 					di_flags |= XFS_DIFLAG_RTINHERIT;
793dd9f438eSNathan Scott 				if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
794dd9f438eSNathan Scott 					di_flags |= XFS_DIFLAG_EXTSZINHERIT;
795dd9f438eSNathan Scott 					ip->i_d.di_extsize = pip->i_d.di_extsize;
796dd9f438eSNathan Scott 				}
797abbede1bSAl Viro 			} else if (S_ISREG(mode)) {
798613d7043SChristoph Hellwig 				if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
799365ca83dSNathan Scott 					di_flags |= XFS_DIFLAG_REALTIME;
800dd9f438eSNathan Scott 				if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
801dd9f438eSNathan Scott 					di_flags |= XFS_DIFLAG_EXTSIZE;
802dd9f438eSNathan Scott 					ip->i_d.di_extsize = pip->i_d.di_extsize;
803dd9f438eSNathan Scott 				}
8041da177e4SLinus Torvalds 			}
8051da177e4SLinus Torvalds 			if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
8061da177e4SLinus Torvalds 			    xfs_inherit_noatime)
807365ca83dSNathan Scott 				di_flags |= XFS_DIFLAG_NOATIME;
8081da177e4SLinus Torvalds 			if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
8091da177e4SLinus Torvalds 			    xfs_inherit_nodump)
810365ca83dSNathan Scott 				di_flags |= XFS_DIFLAG_NODUMP;
8111da177e4SLinus Torvalds 			if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
8121da177e4SLinus Torvalds 			    xfs_inherit_sync)
813365ca83dSNathan Scott 				di_flags |= XFS_DIFLAG_SYNC;
8141da177e4SLinus Torvalds 			if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
8151da177e4SLinus Torvalds 			    xfs_inherit_nosymlinks)
816365ca83dSNathan Scott 				di_flags |= XFS_DIFLAG_NOSYMLINKS;
817365ca83dSNathan Scott 			if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
818365ca83dSNathan Scott 				di_flags |= XFS_DIFLAG_PROJINHERIT;
819d3446eacSBarry Naujok 			if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
820d3446eacSBarry Naujok 			    xfs_inherit_nodefrag)
821d3446eacSBarry Naujok 				di_flags |= XFS_DIFLAG_NODEFRAG;
8222a82b8beSDavid Chinner 			if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
8232a82b8beSDavid Chinner 				di_flags |= XFS_DIFLAG_FILESTREAM;
824365ca83dSNathan Scott 			ip->i_d.di_flags |= di_flags;
8251da177e4SLinus Torvalds 		}
8261da177e4SLinus Torvalds 		/* FALLTHROUGH */
8271da177e4SLinus Torvalds 	case S_IFLNK:
8281da177e4SLinus Torvalds 		ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
8291da177e4SLinus Torvalds 		ip->i_df.if_flags = XFS_IFEXTENTS;
8301da177e4SLinus Torvalds 		ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
8311da177e4SLinus Torvalds 		ip->i_df.if_u1.if_extents = NULL;
8321da177e4SLinus Torvalds 		break;
8331da177e4SLinus Torvalds 	default:
8341da177e4SLinus Torvalds 		ASSERT(0);
8351da177e4SLinus Torvalds 	}
8361da177e4SLinus Torvalds 	/*
8371da177e4SLinus Torvalds 	 * Attribute fork settings for new inode.
8381da177e4SLinus Torvalds 	 */
8391da177e4SLinus Torvalds 	ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
8401da177e4SLinus Torvalds 	ip->i_d.di_anextents = 0;
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 	/*
8431da177e4SLinus Torvalds 	 * Log the new values stuffed into the inode.
8441da177e4SLinus Torvalds 	 */
845ddc3415aSChristoph Hellwig 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
8461da177e4SLinus Torvalds 	xfs_trans_log_inode(tp, ip, flags);
8471da177e4SLinus Torvalds 
848b83bd138SNathan Scott 	/* now that we have an i_mode we can setup inode ops and unlock */
84941be8bedSChristoph Hellwig 	xfs_setup_inode(ip);
8501da177e4SLinus Torvalds 
851bf904248SDavid Chinner 	/* now we have set up the vfs inode we can associate the filestream */
852bf904248SDavid Chinner 	if (filestreams) {
853bf904248SDavid Chinner 		error = xfs_filestream_associate(pip, ip);
854bf904248SDavid Chinner 		if (error < 0)
855bf904248SDavid Chinner 			return -error;
856bf904248SDavid Chinner 		if (!error)
857bf904248SDavid Chinner 			xfs_iflags_set(ip, XFS_IFILESTREAM);
858bf904248SDavid Chinner 	}
859bf904248SDavid Chinner 
8601da177e4SLinus Torvalds 	*ipp = ip;
8611da177e4SLinus Torvalds 	return 0;
8621da177e4SLinus Torvalds }
8631da177e4SLinus Torvalds 
864e546cb79SDave Chinner /*
865e546cb79SDave Chinner  * Allocates a new inode from disk and return a pointer to the
866e546cb79SDave Chinner  * incore copy. This routine will internally commit the current
867e546cb79SDave Chinner  * transaction and allocate a new one if the Space Manager needed
868e546cb79SDave Chinner  * to do an allocation to replenish the inode free-list.
869e546cb79SDave Chinner  *
870e546cb79SDave Chinner  * This routine is designed to be called from xfs_create and
871e546cb79SDave Chinner  * xfs_create_dir.
872e546cb79SDave Chinner  *
873e546cb79SDave Chinner  */
874e546cb79SDave Chinner int
875e546cb79SDave Chinner xfs_dir_ialloc(
876e546cb79SDave Chinner 	xfs_trans_t	**tpp,		/* input: current transaction;
877e546cb79SDave Chinner 					   output: may be a new transaction. */
878e546cb79SDave Chinner 	xfs_inode_t	*dp,		/* directory within whose allocate
879e546cb79SDave Chinner 					   the inode. */
880e546cb79SDave Chinner 	umode_t		mode,
881e546cb79SDave Chinner 	xfs_nlink_t	nlink,
882e546cb79SDave Chinner 	xfs_dev_t	rdev,
883e546cb79SDave Chinner 	prid_t		prid,		/* project id */
884e546cb79SDave Chinner 	int		okalloc,	/* ok to allocate new space */
885e546cb79SDave Chinner 	xfs_inode_t	**ipp,		/* pointer to inode; it will be
886e546cb79SDave Chinner 					   locked. */
887e546cb79SDave Chinner 	int		*committed)
888e546cb79SDave Chinner 
889e546cb79SDave Chinner {
890e546cb79SDave Chinner 	xfs_trans_t	*tp;
891e546cb79SDave Chinner 	xfs_trans_t	*ntp;
892e546cb79SDave Chinner 	xfs_inode_t	*ip;
893e546cb79SDave Chinner 	xfs_buf_t	*ialloc_context = NULL;
894e546cb79SDave Chinner 	int		code;
895e546cb79SDave Chinner 	void		*dqinfo;
896e546cb79SDave Chinner 	uint		tflags;
897e546cb79SDave Chinner 
898e546cb79SDave Chinner 	tp = *tpp;
899e546cb79SDave Chinner 	ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
900e546cb79SDave Chinner 
901e546cb79SDave Chinner 	/*
902e546cb79SDave Chinner 	 * xfs_ialloc will return a pointer to an incore inode if
903e546cb79SDave Chinner 	 * the Space Manager has an available inode on the free
904e546cb79SDave Chinner 	 * list. Otherwise, it will do an allocation and replenish
905e546cb79SDave Chinner 	 * the freelist.  Since we can only do one allocation per
906e546cb79SDave Chinner 	 * transaction without deadlocks, we will need to commit the
907e546cb79SDave Chinner 	 * current transaction and start a new one.  We will then
908e546cb79SDave Chinner 	 * need to call xfs_ialloc again to get the inode.
909e546cb79SDave Chinner 	 *
910e546cb79SDave Chinner 	 * If xfs_ialloc did an allocation to replenish the freelist,
911e546cb79SDave Chinner 	 * it returns the bp containing the head of the freelist as
912e546cb79SDave Chinner 	 * ialloc_context. We will hold a lock on it across the
913e546cb79SDave Chinner 	 * transaction commit so that no other process can steal
914e546cb79SDave Chinner 	 * the inode(s) that we've just allocated.
915e546cb79SDave Chinner 	 */
916e546cb79SDave Chinner 	code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid, okalloc,
917e546cb79SDave Chinner 			  &ialloc_context, &ip);
918e546cb79SDave Chinner 
919e546cb79SDave Chinner 	/*
920e546cb79SDave Chinner 	 * Return an error if we were unable to allocate a new inode.
921e546cb79SDave Chinner 	 * This should only happen if we run out of space on disk or
922e546cb79SDave Chinner 	 * encounter a disk error.
923e546cb79SDave Chinner 	 */
924e546cb79SDave Chinner 	if (code) {
925e546cb79SDave Chinner 		*ipp = NULL;
926e546cb79SDave Chinner 		return code;
927e546cb79SDave Chinner 	}
928e546cb79SDave Chinner 	if (!ialloc_context && !ip) {
929e546cb79SDave Chinner 		*ipp = NULL;
930e546cb79SDave Chinner 		return XFS_ERROR(ENOSPC);
931e546cb79SDave Chinner 	}
932e546cb79SDave Chinner 
933e546cb79SDave Chinner 	/*
934e546cb79SDave Chinner 	 * If the AGI buffer is non-NULL, then we were unable to get an
935e546cb79SDave Chinner 	 * inode in one operation.  We need to commit the current
936e546cb79SDave Chinner 	 * transaction and call xfs_ialloc() again.  It is guaranteed
937e546cb79SDave Chinner 	 * to succeed the second time.
938e546cb79SDave Chinner 	 */
939e546cb79SDave Chinner 	if (ialloc_context) {
9403d3c8b52SJie Liu 		struct xfs_trans_res tres;
9413d3c8b52SJie Liu 
942e546cb79SDave Chinner 		/*
943e546cb79SDave Chinner 		 * Normally, xfs_trans_commit releases all the locks.
944e546cb79SDave Chinner 		 * We call bhold to hang on to the ialloc_context across
945e546cb79SDave Chinner 		 * the commit.  Holding this buffer prevents any other
946e546cb79SDave Chinner 		 * processes from doing any allocations in this
947e546cb79SDave Chinner 		 * allocation group.
948e546cb79SDave Chinner 		 */
949e546cb79SDave Chinner 		xfs_trans_bhold(tp, ialloc_context);
950e546cb79SDave Chinner 		/*
951e546cb79SDave Chinner 		 * Save the log reservation so we can use
952e546cb79SDave Chinner 		 * them in the next transaction.
953e546cb79SDave Chinner 		 */
9543d3c8b52SJie Liu 		tres.tr_logres = xfs_trans_get_log_res(tp);
9553d3c8b52SJie Liu 		tres.tr_logcount = xfs_trans_get_log_count(tp);
956e546cb79SDave Chinner 
957e546cb79SDave Chinner 		/*
958e546cb79SDave Chinner 		 * We want the quota changes to be associated with the next
959e546cb79SDave Chinner 		 * transaction, NOT this one. So, detach the dqinfo from this
960e546cb79SDave Chinner 		 * and attach it to the next transaction.
961e546cb79SDave Chinner 		 */
962e546cb79SDave Chinner 		dqinfo = NULL;
963e546cb79SDave Chinner 		tflags = 0;
964e546cb79SDave Chinner 		if (tp->t_dqinfo) {
965e546cb79SDave Chinner 			dqinfo = (void *)tp->t_dqinfo;
966e546cb79SDave Chinner 			tp->t_dqinfo = NULL;
967e546cb79SDave Chinner 			tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY;
968e546cb79SDave Chinner 			tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY);
969e546cb79SDave Chinner 		}
970e546cb79SDave Chinner 
971e546cb79SDave Chinner 		ntp = xfs_trans_dup(tp);
972e546cb79SDave Chinner 		code = xfs_trans_commit(tp, 0);
973e546cb79SDave Chinner 		tp = ntp;
974e546cb79SDave Chinner 		if (committed != NULL) {
975e546cb79SDave Chinner 			*committed = 1;
976e546cb79SDave Chinner 		}
977e546cb79SDave Chinner 		/*
978e546cb79SDave Chinner 		 * If we get an error during the commit processing,
979e546cb79SDave Chinner 		 * release the buffer that is still held and return
980e546cb79SDave Chinner 		 * to the caller.
981e546cb79SDave Chinner 		 */
982e546cb79SDave Chinner 		if (code) {
983e546cb79SDave Chinner 			xfs_buf_relse(ialloc_context);
984e546cb79SDave Chinner 			if (dqinfo) {
985e546cb79SDave Chinner 				tp->t_dqinfo = dqinfo;
986e546cb79SDave Chinner 				xfs_trans_free_dqinfo(tp);
987e546cb79SDave Chinner 			}
988e546cb79SDave Chinner 			*tpp = ntp;
989e546cb79SDave Chinner 			*ipp = NULL;
990e546cb79SDave Chinner 			return code;
991e546cb79SDave Chinner 		}
992e546cb79SDave Chinner 
993e546cb79SDave Chinner 		/*
994e546cb79SDave Chinner 		 * transaction commit worked ok so we can drop the extra ticket
995e546cb79SDave Chinner 		 * reference that we gained in xfs_trans_dup()
996e546cb79SDave Chinner 		 */
997e546cb79SDave Chinner 		xfs_log_ticket_put(tp->t_ticket);
9983d3c8b52SJie Liu 		tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
9993d3c8b52SJie Liu 		code = xfs_trans_reserve(tp, &tres, 0, 0);
10003d3c8b52SJie Liu 
1001e546cb79SDave Chinner 		/*
1002e546cb79SDave Chinner 		 * Re-attach the quota info that we detached from prev trx.
1003e546cb79SDave Chinner 		 */
1004e546cb79SDave Chinner 		if (dqinfo) {
1005e546cb79SDave Chinner 			tp->t_dqinfo = dqinfo;
1006e546cb79SDave Chinner 			tp->t_flags |= tflags;
1007e546cb79SDave Chinner 		}
1008e546cb79SDave Chinner 
1009e546cb79SDave Chinner 		if (code) {
1010e546cb79SDave Chinner 			xfs_buf_relse(ialloc_context);
1011e546cb79SDave Chinner 			*tpp = ntp;
1012e546cb79SDave Chinner 			*ipp = NULL;
1013e546cb79SDave Chinner 			return code;
1014e546cb79SDave Chinner 		}
1015e546cb79SDave Chinner 		xfs_trans_bjoin(tp, ialloc_context);
1016e546cb79SDave Chinner 
1017e546cb79SDave Chinner 		/*
1018e546cb79SDave Chinner 		 * Call ialloc again. Since we've locked out all
1019e546cb79SDave Chinner 		 * other allocations in this allocation group,
1020e546cb79SDave Chinner 		 * this call should always succeed.
1021e546cb79SDave Chinner 		 */
1022e546cb79SDave Chinner 		code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid,
1023e546cb79SDave Chinner 				  okalloc, &ialloc_context, &ip);
1024e546cb79SDave Chinner 
1025e546cb79SDave Chinner 		/*
1026e546cb79SDave Chinner 		 * If we get an error at this point, return to the caller
1027e546cb79SDave Chinner 		 * so that the current transaction can be aborted.
1028e546cb79SDave Chinner 		 */
1029e546cb79SDave Chinner 		if (code) {
1030e546cb79SDave Chinner 			*tpp = tp;
1031e546cb79SDave Chinner 			*ipp = NULL;
1032e546cb79SDave Chinner 			return code;
1033e546cb79SDave Chinner 		}
1034e546cb79SDave Chinner 		ASSERT(!ialloc_context && ip);
1035e546cb79SDave Chinner 
1036e546cb79SDave Chinner 	} else {
1037e546cb79SDave Chinner 		if (committed != NULL)
1038e546cb79SDave Chinner 			*committed = 0;
1039e546cb79SDave Chinner 	}
1040e546cb79SDave Chinner 
1041e546cb79SDave Chinner 	*ipp = ip;
1042e546cb79SDave Chinner 	*tpp = tp;
1043e546cb79SDave Chinner 
1044e546cb79SDave Chinner 	return 0;
1045e546cb79SDave Chinner }
1046e546cb79SDave Chinner 
1047e546cb79SDave Chinner /*
1048e546cb79SDave Chinner  * Decrement the link count on an inode & log the change.
1049e546cb79SDave Chinner  * If this causes the link count to go to zero, initiate the
1050e546cb79SDave Chinner  * logging activity required to truncate a file.
1051e546cb79SDave Chinner  */
1052e546cb79SDave Chinner int				/* error */
1053e546cb79SDave Chinner xfs_droplink(
1054e546cb79SDave Chinner 	xfs_trans_t *tp,
1055e546cb79SDave Chinner 	xfs_inode_t *ip)
1056e546cb79SDave Chinner {
1057e546cb79SDave Chinner 	int	error;
1058e546cb79SDave Chinner 
1059e546cb79SDave Chinner 	xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1060e546cb79SDave Chinner 
1061e546cb79SDave Chinner 	ASSERT (ip->i_d.di_nlink > 0);
1062e546cb79SDave Chinner 	ip->i_d.di_nlink--;
1063e546cb79SDave Chinner 	drop_nlink(VFS_I(ip));
1064e546cb79SDave Chinner 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1065e546cb79SDave Chinner 
1066e546cb79SDave Chinner 	error = 0;
1067e546cb79SDave Chinner 	if (ip->i_d.di_nlink == 0) {
1068e546cb79SDave Chinner 		/*
1069e546cb79SDave Chinner 		 * We're dropping the last link to this file.
1070e546cb79SDave Chinner 		 * Move the on-disk inode to the AGI unlinked list.
1071e546cb79SDave Chinner 		 * From xfs_inactive() we will pull the inode from
1072e546cb79SDave Chinner 		 * the list and free it.
1073e546cb79SDave Chinner 		 */
1074e546cb79SDave Chinner 		error = xfs_iunlink(tp, ip);
1075e546cb79SDave Chinner 	}
1076e546cb79SDave Chinner 	return error;
1077e546cb79SDave Chinner }
1078e546cb79SDave Chinner 
1079e546cb79SDave Chinner /*
1080e546cb79SDave Chinner  * This gets called when the inode's version needs to be changed from 1 to 2.
1081e546cb79SDave Chinner  * Currently this happens when the nlink field overflows the old 16-bit value
1082e546cb79SDave Chinner  * or when chproj is called to change the project for the first time.
1083e546cb79SDave Chinner  * As a side effect the superblock version will also get rev'd
1084e546cb79SDave Chinner  * to contain the NLINK bit.
1085e546cb79SDave Chinner  */
1086e546cb79SDave Chinner void
1087e546cb79SDave Chinner xfs_bump_ino_vers2(
1088e546cb79SDave Chinner 	xfs_trans_t	*tp,
1089e546cb79SDave Chinner 	xfs_inode_t	*ip)
1090e546cb79SDave Chinner {
1091e546cb79SDave Chinner 	xfs_mount_t	*mp;
1092e546cb79SDave Chinner 
1093e546cb79SDave Chinner 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1094e546cb79SDave Chinner 	ASSERT(ip->i_d.di_version == 1);
1095e546cb79SDave Chinner 
1096e546cb79SDave Chinner 	ip->i_d.di_version = 2;
1097e546cb79SDave Chinner 	ip->i_d.di_onlink = 0;
1098e546cb79SDave Chinner 	memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
1099e546cb79SDave Chinner 	mp = tp->t_mountp;
1100e546cb79SDave Chinner 	if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
1101e546cb79SDave Chinner 		spin_lock(&mp->m_sb_lock);
1102e546cb79SDave Chinner 		if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
1103e546cb79SDave Chinner 			xfs_sb_version_addnlink(&mp->m_sb);
1104e546cb79SDave Chinner 			spin_unlock(&mp->m_sb_lock);
1105e546cb79SDave Chinner 			xfs_mod_sb(tp, XFS_SB_VERSIONNUM);
1106e546cb79SDave Chinner 		} else {
1107e546cb79SDave Chinner 			spin_unlock(&mp->m_sb_lock);
1108e546cb79SDave Chinner 		}
1109e546cb79SDave Chinner 	}
1110e546cb79SDave Chinner 	/* Caller must log the inode */
1111e546cb79SDave Chinner }
1112e546cb79SDave Chinner 
1113e546cb79SDave Chinner /*
1114e546cb79SDave Chinner  * Increment the link count on an inode & log the change.
1115e546cb79SDave Chinner  */
1116e546cb79SDave Chinner int
1117e546cb79SDave Chinner xfs_bumplink(
1118e546cb79SDave Chinner 	xfs_trans_t *tp,
1119e546cb79SDave Chinner 	xfs_inode_t *ip)
1120e546cb79SDave Chinner {
1121e546cb79SDave Chinner 	xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1122e546cb79SDave Chinner 
1123e546cb79SDave Chinner 	ASSERT(ip->i_d.di_nlink > 0);
1124e546cb79SDave Chinner 	ip->i_d.di_nlink++;
1125e546cb79SDave Chinner 	inc_nlink(VFS_I(ip));
1126e546cb79SDave Chinner 	if ((ip->i_d.di_version == 1) &&
1127e546cb79SDave Chinner 	    (ip->i_d.di_nlink > XFS_MAXLINK_1)) {
1128e546cb79SDave Chinner 		/*
1129e546cb79SDave Chinner 		 * The inode has increased its number of links beyond
1130e546cb79SDave Chinner 		 * what can fit in an old format inode.  It now needs
1131e546cb79SDave Chinner 		 * to be converted to a version 2 inode with a 32 bit
1132e546cb79SDave Chinner 		 * link count.  If this is the first inode in the file
1133e546cb79SDave Chinner 		 * system to do this, then we need to bump the superblock
1134e546cb79SDave Chinner 		 * version number as well.
1135e546cb79SDave Chinner 		 */
1136e546cb79SDave Chinner 		xfs_bump_ino_vers2(tp, ip);
1137e546cb79SDave Chinner 	}
1138e546cb79SDave Chinner 
1139e546cb79SDave Chinner 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1140e546cb79SDave Chinner 	return 0;
1141e546cb79SDave Chinner }
1142e546cb79SDave Chinner 
1143c24b5dfaSDave Chinner int
1144c24b5dfaSDave Chinner xfs_create(
1145c24b5dfaSDave Chinner 	xfs_inode_t		*dp,
1146c24b5dfaSDave Chinner 	struct xfs_name		*name,
1147c24b5dfaSDave Chinner 	umode_t			mode,
1148c24b5dfaSDave Chinner 	xfs_dev_t		rdev,
1149c24b5dfaSDave Chinner 	xfs_inode_t		**ipp)
1150c24b5dfaSDave Chinner {
1151c24b5dfaSDave Chinner 	int			is_dir = S_ISDIR(mode);
1152c24b5dfaSDave Chinner 	struct xfs_mount	*mp = dp->i_mount;
1153c24b5dfaSDave Chinner 	struct xfs_inode	*ip = NULL;
1154c24b5dfaSDave Chinner 	struct xfs_trans	*tp = NULL;
1155c24b5dfaSDave Chinner 	int			error;
1156c24b5dfaSDave Chinner 	xfs_bmap_free_t		free_list;
1157c24b5dfaSDave Chinner 	xfs_fsblock_t		first_block;
1158c24b5dfaSDave Chinner 	bool                    unlock_dp_on_error = false;
1159c24b5dfaSDave Chinner 	uint			cancel_flags;
1160c24b5dfaSDave Chinner 	int			committed;
1161c24b5dfaSDave Chinner 	prid_t			prid;
1162c24b5dfaSDave Chinner 	struct xfs_dquot	*udqp = NULL;
1163c24b5dfaSDave Chinner 	struct xfs_dquot	*gdqp = NULL;
1164c24b5dfaSDave Chinner 	struct xfs_dquot	*pdqp = NULL;
11653d3c8b52SJie Liu 	struct xfs_trans_res	tres;
1166c24b5dfaSDave Chinner 	uint			resblks;
1167c24b5dfaSDave Chinner 
1168c24b5dfaSDave Chinner 	trace_xfs_create(dp, name);
1169c24b5dfaSDave Chinner 
1170c24b5dfaSDave Chinner 	if (XFS_FORCED_SHUTDOWN(mp))
1171c24b5dfaSDave Chinner 		return XFS_ERROR(EIO);
1172c24b5dfaSDave Chinner 
1173c24b5dfaSDave Chinner 	if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1174c24b5dfaSDave Chinner 		prid = xfs_get_projid(dp);
1175c24b5dfaSDave Chinner 	else
1176c24b5dfaSDave Chinner 		prid = XFS_PROJID_DEFAULT;
1177c24b5dfaSDave Chinner 
1178c24b5dfaSDave Chinner 	/*
1179c24b5dfaSDave Chinner 	 * Make sure that we have allocated dquot(s) on disk.
1180c24b5dfaSDave Chinner 	 */
11817aab1b28SDwight Engen 	error = xfs_qm_vop_dqalloc(dp, xfs_kuid_to_uid(current_fsuid()),
11827aab1b28SDwight Engen 					xfs_kgid_to_gid(current_fsgid()), prid,
1183c24b5dfaSDave Chinner 					XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
1184c24b5dfaSDave Chinner 					&udqp, &gdqp, &pdqp);
1185c24b5dfaSDave Chinner 	if (error)
1186c24b5dfaSDave Chinner 		return error;
1187c24b5dfaSDave Chinner 
1188c24b5dfaSDave Chinner 	if (is_dir) {
1189c24b5dfaSDave Chinner 		rdev = 0;
1190c24b5dfaSDave Chinner 		resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
11913d3c8b52SJie Liu 		tres.tr_logres = M_RES(mp)->tr_mkdir.tr_logres;
11923d3c8b52SJie Liu 		tres.tr_logcount = XFS_MKDIR_LOG_COUNT;
1193c24b5dfaSDave Chinner 		tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
1194c24b5dfaSDave Chinner 	} else {
1195c24b5dfaSDave Chinner 		resblks = XFS_CREATE_SPACE_RES(mp, name->len);
11963d3c8b52SJie Liu 		tres.tr_logres = M_RES(mp)->tr_create.tr_logres;
11973d3c8b52SJie Liu 		tres.tr_logcount = XFS_CREATE_LOG_COUNT;
1198c24b5dfaSDave Chinner 		tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1199c24b5dfaSDave Chinner 	}
1200c24b5dfaSDave Chinner 
1201c24b5dfaSDave Chinner 	cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1202c24b5dfaSDave Chinner 
1203c24b5dfaSDave Chinner 	/*
1204c24b5dfaSDave Chinner 	 * Initially assume that the file does not exist and
1205c24b5dfaSDave Chinner 	 * reserve the resources for that case.  If that is not
1206c24b5dfaSDave Chinner 	 * the case we'll drop the one we have and get a more
1207c24b5dfaSDave Chinner 	 * appropriate transaction later.
1208c24b5dfaSDave Chinner 	 */
12093d3c8b52SJie Liu 	tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
12103d3c8b52SJie Liu 	error = xfs_trans_reserve(tp, &tres, resblks, 0);
1211c24b5dfaSDave Chinner 	if (error == ENOSPC) {
1212c24b5dfaSDave Chinner 		/* flush outstanding delalloc blocks and retry */
1213c24b5dfaSDave Chinner 		xfs_flush_inodes(mp);
12143d3c8b52SJie Liu 		error = xfs_trans_reserve(tp, &tres, resblks, 0);
1215c24b5dfaSDave Chinner 	}
1216c24b5dfaSDave Chinner 	if (error == ENOSPC) {
1217c24b5dfaSDave Chinner 		/* No space at all so try a "no-allocation" reservation */
1218c24b5dfaSDave Chinner 		resblks = 0;
12193d3c8b52SJie Liu 		error = xfs_trans_reserve(tp, &tres, 0, 0);
1220c24b5dfaSDave Chinner 	}
1221c24b5dfaSDave Chinner 	if (error) {
1222c24b5dfaSDave Chinner 		cancel_flags = 0;
1223c24b5dfaSDave Chinner 		goto out_trans_cancel;
1224c24b5dfaSDave Chinner 	}
1225c24b5dfaSDave Chinner 
1226c24b5dfaSDave Chinner 	xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1227c24b5dfaSDave Chinner 	unlock_dp_on_error = true;
1228c24b5dfaSDave Chinner 
1229c24b5dfaSDave Chinner 	xfs_bmap_init(&free_list, &first_block);
1230c24b5dfaSDave Chinner 
1231c24b5dfaSDave Chinner 	/*
1232c24b5dfaSDave Chinner 	 * Reserve disk quota and the inode.
1233c24b5dfaSDave Chinner 	 */
1234c24b5dfaSDave Chinner 	error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
1235c24b5dfaSDave Chinner 						pdqp, resblks, 1, 0);
1236c24b5dfaSDave Chinner 	if (error)
1237c24b5dfaSDave Chinner 		goto out_trans_cancel;
1238c24b5dfaSDave Chinner 
1239c24b5dfaSDave Chinner 	error = xfs_dir_canenter(tp, dp, name, resblks);
1240c24b5dfaSDave Chinner 	if (error)
1241c24b5dfaSDave Chinner 		goto out_trans_cancel;
1242c24b5dfaSDave Chinner 
1243c24b5dfaSDave Chinner 	/*
1244c24b5dfaSDave Chinner 	 * A newly created regular or special file just has one directory
1245c24b5dfaSDave Chinner 	 * entry pointing to them, but a directory also the "." entry
1246c24b5dfaSDave Chinner 	 * pointing to itself.
1247c24b5dfaSDave Chinner 	 */
1248c24b5dfaSDave Chinner 	error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev,
1249c24b5dfaSDave Chinner 			       prid, resblks > 0, &ip, &committed);
1250c24b5dfaSDave Chinner 	if (error) {
1251c24b5dfaSDave Chinner 		if (error == ENOSPC)
1252c24b5dfaSDave Chinner 			goto out_trans_cancel;
1253c24b5dfaSDave Chinner 		goto out_trans_abort;
1254c24b5dfaSDave Chinner 	}
1255c24b5dfaSDave Chinner 
1256c24b5dfaSDave Chinner 	/*
1257c24b5dfaSDave Chinner 	 * Now we join the directory inode to the transaction.  We do not do it
1258c24b5dfaSDave Chinner 	 * earlier because xfs_dir_ialloc might commit the previous transaction
1259c24b5dfaSDave Chinner 	 * (and release all the locks).  An error from here on will result in
1260c24b5dfaSDave Chinner 	 * the transaction cancel unlocking dp so don't do it explicitly in the
1261c24b5dfaSDave Chinner 	 * error path.
1262c24b5dfaSDave Chinner 	 */
1263c24b5dfaSDave Chinner 	xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1264c24b5dfaSDave Chinner 	unlock_dp_on_error = false;
1265c24b5dfaSDave Chinner 
1266c24b5dfaSDave Chinner 	error = xfs_dir_createname(tp, dp, name, ip->i_ino,
1267c24b5dfaSDave Chinner 					&first_block, &free_list, resblks ?
1268c24b5dfaSDave Chinner 					resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
1269c24b5dfaSDave Chinner 	if (error) {
1270c24b5dfaSDave Chinner 		ASSERT(error != ENOSPC);
1271c24b5dfaSDave Chinner 		goto out_trans_abort;
1272c24b5dfaSDave Chinner 	}
1273c24b5dfaSDave Chinner 	xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1274c24b5dfaSDave Chinner 	xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1275c24b5dfaSDave Chinner 
1276c24b5dfaSDave Chinner 	if (is_dir) {
1277c24b5dfaSDave Chinner 		error = xfs_dir_init(tp, ip, dp);
1278c24b5dfaSDave Chinner 		if (error)
1279c24b5dfaSDave Chinner 			goto out_bmap_cancel;
1280c24b5dfaSDave Chinner 
1281c24b5dfaSDave Chinner 		error = xfs_bumplink(tp, dp);
1282c24b5dfaSDave Chinner 		if (error)
1283c24b5dfaSDave Chinner 			goto out_bmap_cancel;
1284c24b5dfaSDave Chinner 	}
1285c24b5dfaSDave Chinner 
1286c24b5dfaSDave Chinner 	/*
1287c24b5dfaSDave Chinner 	 * If this is a synchronous mount, make sure that the
1288c24b5dfaSDave Chinner 	 * create transaction goes to disk before returning to
1289c24b5dfaSDave Chinner 	 * the user.
1290c24b5dfaSDave Chinner 	 */
1291c24b5dfaSDave Chinner 	if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1292c24b5dfaSDave Chinner 		xfs_trans_set_sync(tp);
1293c24b5dfaSDave Chinner 
1294c24b5dfaSDave Chinner 	/*
1295c24b5dfaSDave Chinner 	 * Attach the dquot(s) to the inodes and modify them incore.
1296c24b5dfaSDave Chinner 	 * These ids of the inode couldn't have changed since the new
1297c24b5dfaSDave Chinner 	 * inode has been locked ever since it was created.
1298c24b5dfaSDave Chinner 	 */
1299c24b5dfaSDave Chinner 	xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
1300c24b5dfaSDave Chinner 
1301c24b5dfaSDave Chinner 	error = xfs_bmap_finish(&tp, &free_list, &committed);
1302c24b5dfaSDave Chinner 	if (error)
1303c24b5dfaSDave Chinner 		goto out_bmap_cancel;
1304c24b5dfaSDave Chinner 
1305c24b5dfaSDave Chinner 	error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1306c24b5dfaSDave Chinner 	if (error)
1307c24b5dfaSDave Chinner 		goto out_release_inode;
1308c24b5dfaSDave Chinner 
1309c24b5dfaSDave Chinner 	xfs_qm_dqrele(udqp);
1310c24b5dfaSDave Chinner 	xfs_qm_dqrele(gdqp);
1311c24b5dfaSDave Chinner 	xfs_qm_dqrele(pdqp);
1312c24b5dfaSDave Chinner 
1313c24b5dfaSDave Chinner 	*ipp = ip;
1314c24b5dfaSDave Chinner 	return 0;
1315c24b5dfaSDave Chinner 
1316c24b5dfaSDave Chinner  out_bmap_cancel:
1317c24b5dfaSDave Chinner 	xfs_bmap_cancel(&free_list);
1318c24b5dfaSDave Chinner  out_trans_abort:
1319c24b5dfaSDave Chinner 	cancel_flags |= XFS_TRANS_ABORT;
1320c24b5dfaSDave Chinner  out_trans_cancel:
1321c24b5dfaSDave Chinner 	xfs_trans_cancel(tp, cancel_flags);
1322c24b5dfaSDave Chinner  out_release_inode:
1323c24b5dfaSDave Chinner 	/*
1324c24b5dfaSDave Chinner 	 * Wait until after the current transaction is aborted to
1325c24b5dfaSDave Chinner 	 * release the inode.  This prevents recursive transactions
1326c24b5dfaSDave Chinner 	 * and deadlocks from xfs_inactive.
1327c24b5dfaSDave Chinner 	 */
1328c24b5dfaSDave Chinner 	if (ip)
1329c24b5dfaSDave Chinner 		IRELE(ip);
1330c24b5dfaSDave Chinner 
1331c24b5dfaSDave Chinner 	xfs_qm_dqrele(udqp);
1332c24b5dfaSDave Chinner 	xfs_qm_dqrele(gdqp);
1333c24b5dfaSDave Chinner 	xfs_qm_dqrele(pdqp);
1334c24b5dfaSDave Chinner 
1335c24b5dfaSDave Chinner 	if (unlock_dp_on_error)
1336c24b5dfaSDave Chinner 		xfs_iunlock(dp, XFS_ILOCK_EXCL);
1337c24b5dfaSDave Chinner 	return error;
1338c24b5dfaSDave Chinner }
1339c24b5dfaSDave Chinner 
1340c24b5dfaSDave Chinner int
1341c24b5dfaSDave Chinner xfs_link(
1342c24b5dfaSDave Chinner 	xfs_inode_t		*tdp,
1343c24b5dfaSDave Chinner 	xfs_inode_t		*sip,
1344c24b5dfaSDave Chinner 	struct xfs_name		*target_name)
1345c24b5dfaSDave Chinner {
1346c24b5dfaSDave Chinner 	xfs_mount_t		*mp = tdp->i_mount;
1347c24b5dfaSDave Chinner 	xfs_trans_t		*tp;
1348c24b5dfaSDave Chinner 	int			error;
1349c24b5dfaSDave Chinner 	xfs_bmap_free_t         free_list;
1350c24b5dfaSDave Chinner 	xfs_fsblock_t           first_block;
1351c24b5dfaSDave Chinner 	int			cancel_flags;
1352c24b5dfaSDave Chinner 	int			committed;
1353c24b5dfaSDave Chinner 	int			resblks;
1354c24b5dfaSDave Chinner 
1355c24b5dfaSDave Chinner 	trace_xfs_link(tdp, target_name);
1356c24b5dfaSDave Chinner 
1357c24b5dfaSDave Chinner 	ASSERT(!S_ISDIR(sip->i_d.di_mode));
1358c24b5dfaSDave Chinner 
1359c24b5dfaSDave Chinner 	if (XFS_FORCED_SHUTDOWN(mp))
1360c24b5dfaSDave Chinner 		return XFS_ERROR(EIO);
1361c24b5dfaSDave Chinner 
1362c24b5dfaSDave Chinner 	error = xfs_qm_dqattach(sip, 0);
1363c24b5dfaSDave Chinner 	if (error)
1364c24b5dfaSDave Chinner 		goto std_return;
1365c24b5dfaSDave Chinner 
1366c24b5dfaSDave Chinner 	error = xfs_qm_dqattach(tdp, 0);
1367c24b5dfaSDave Chinner 	if (error)
1368c24b5dfaSDave Chinner 		goto std_return;
1369c24b5dfaSDave Chinner 
1370c24b5dfaSDave Chinner 	tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
1371c24b5dfaSDave Chinner 	cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1372c24b5dfaSDave Chinner 	resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
13733d3c8b52SJie Liu 	error = xfs_trans_reserve(tp, &M_RES(mp)->tr_link, resblks, 0);
1374c24b5dfaSDave Chinner 	if (error == ENOSPC) {
1375c24b5dfaSDave Chinner 		resblks = 0;
13763d3c8b52SJie Liu 		error = xfs_trans_reserve(tp, &M_RES(mp)->tr_link, 0, 0);
1377c24b5dfaSDave Chinner 	}
1378c24b5dfaSDave Chinner 	if (error) {
1379c24b5dfaSDave Chinner 		cancel_flags = 0;
1380c24b5dfaSDave Chinner 		goto error_return;
1381c24b5dfaSDave Chinner 	}
1382c24b5dfaSDave Chinner 
1383c24b5dfaSDave Chinner 	xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
1384c24b5dfaSDave Chinner 
1385c24b5dfaSDave Chinner 	xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
1386c24b5dfaSDave Chinner 	xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
1387c24b5dfaSDave Chinner 
1388c24b5dfaSDave Chinner 	/*
1389c24b5dfaSDave Chinner 	 * If we are using project inheritance, we only allow hard link
1390c24b5dfaSDave Chinner 	 * creation in our tree when the project IDs are the same; else
1391c24b5dfaSDave Chinner 	 * the tree quota mechanism could be circumvented.
1392c24b5dfaSDave Chinner 	 */
1393c24b5dfaSDave Chinner 	if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
1394c24b5dfaSDave Chinner 		     (xfs_get_projid(tdp) != xfs_get_projid(sip)))) {
1395c24b5dfaSDave Chinner 		error = XFS_ERROR(EXDEV);
1396c24b5dfaSDave Chinner 		goto error_return;
1397c24b5dfaSDave Chinner 	}
1398c24b5dfaSDave Chinner 
1399c24b5dfaSDave Chinner 	error = xfs_dir_canenter(tp, tdp, target_name, resblks);
1400c24b5dfaSDave Chinner 	if (error)
1401c24b5dfaSDave Chinner 		goto error_return;
1402c24b5dfaSDave Chinner 
1403c24b5dfaSDave Chinner 	xfs_bmap_init(&free_list, &first_block);
1404c24b5dfaSDave Chinner 
1405c24b5dfaSDave Chinner 	error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
1406c24b5dfaSDave Chinner 					&first_block, &free_list, resblks);
1407c24b5dfaSDave Chinner 	if (error)
1408c24b5dfaSDave Chinner 		goto abort_return;
1409c24b5dfaSDave Chinner 	xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1410c24b5dfaSDave Chinner 	xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
1411c24b5dfaSDave Chinner 
1412c24b5dfaSDave Chinner 	error = xfs_bumplink(tp, sip);
1413c24b5dfaSDave Chinner 	if (error)
1414c24b5dfaSDave Chinner 		goto abort_return;
1415c24b5dfaSDave Chinner 
1416c24b5dfaSDave Chinner 	/*
1417c24b5dfaSDave Chinner 	 * If this is a synchronous mount, make sure that the
1418c24b5dfaSDave Chinner 	 * link transaction goes to disk before returning to
1419c24b5dfaSDave Chinner 	 * the user.
1420c24b5dfaSDave Chinner 	 */
1421c24b5dfaSDave Chinner 	if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1422c24b5dfaSDave Chinner 		xfs_trans_set_sync(tp);
1423c24b5dfaSDave Chinner 	}
1424c24b5dfaSDave Chinner 
1425c24b5dfaSDave Chinner 	error = xfs_bmap_finish (&tp, &free_list, &committed);
1426c24b5dfaSDave Chinner 	if (error) {
1427c24b5dfaSDave Chinner 		xfs_bmap_cancel(&free_list);
1428c24b5dfaSDave Chinner 		goto abort_return;
1429c24b5dfaSDave Chinner 	}
1430c24b5dfaSDave Chinner 
1431c24b5dfaSDave Chinner 	return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1432c24b5dfaSDave Chinner 
1433c24b5dfaSDave Chinner  abort_return:
1434c24b5dfaSDave Chinner 	cancel_flags |= XFS_TRANS_ABORT;
1435c24b5dfaSDave Chinner  error_return:
1436c24b5dfaSDave Chinner 	xfs_trans_cancel(tp, cancel_flags);
1437c24b5dfaSDave Chinner  std_return:
1438c24b5dfaSDave Chinner 	return error;
1439c24b5dfaSDave Chinner }
1440c24b5dfaSDave Chinner 
14411da177e4SLinus Torvalds /*
14428f04c47aSChristoph Hellwig  * Free up the underlying blocks past new_size.  The new size must be smaller
14438f04c47aSChristoph Hellwig  * than the current size.  This routine can be used both for the attribute and
14448f04c47aSChristoph Hellwig  * data fork, and does not modify the inode size, which is left to the caller.
14451da177e4SLinus Torvalds  *
1446f6485057SDavid Chinner  * The transaction passed to this routine must have made a permanent log
1447f6485057SDavid Chinner  * reservation of at least XFS_ITRUNCATE_LOG_RES.  This routine may commit the
1448f6485057SDavid Chinner  * given transaction and start new ones, so make sure everything involved in
1449f6485057SDavid Chinner  * the transaction is tidy before calling here.  Some transaction will be
1450f6485057SDavid Chinner  * returned to the caller to be committed.  The incoming transaction must
1451f6485057SDavid Chinner  * already include the inode, and both inode locks must be held exclusively.
1452f6485057SDavid Chinner  * The inode must also be "held" within the transaction.  On return the inode
1453f6485057SDavid Chinner  * will be "held" within the returned transaction.  This routine does NOT
1454f6485057SDavid Chinner  * require any disk space to be reserved for it within the transaction.
14551da177e4SLinus Torvalds  *
1456f6485057SDavid Chinner  * If we get an error, we must return with the inode locked and linked into the
1457f6485057SDavid Chinner  * current transaction. This keeps things simple for the higher level code,
1458f6485057SDavid Chinner  * because it always knows that the inode is locked and held in the transaction
1459f6485057SDavid Chinner  * that returns to it whether errors occur or not.  We don't mark the inode
1460f6485057SDavid Chinner  * dirty on error so that transactions can be easily aborted if possible.
14611da177e4SLinus Torvalds  */
14621da177e4SLinus Torvalds int
14638f04c47aSChristoph Hellwig xfs_itruncate_extents(
14648f04c47aSChristoph Hellwig 	struct xfs_trans	**tpp,
14658f04c47aSChristoph Hellwig 	struct xfs_inode	*ip,
14668f04c47aSChristoph Hellwig 	int			whichfork,
14678f04c47aSChristoph Hellwig 	xfs_fsize_t		new_size)
14681da177e4SLinus Torvalds {
14698f04c47aSChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
14708f04c47aSChristoph Hellwig 	struct xfs_trans	*tp = *tpp;
14718f04c47aSChristoph Hellwig 	struct xfs_trans	*ntp;
14728f04c47aSChristoph Hellwig 	xfs_bmap_free_t		free_list;
14731da177e4SLinus Torvalds 	xfs_fsblock_t		first_block;
14741da177e4SLinus Torvalds 	xfs_fileoff_t		first_unmap_block;
14751da177e4SLinus Torvalds 	xfs_fileoff_t		last_block;
14768f04c47aSChristoph Hellwig 	xfs_filblks_t		unmap_len;
14771da177e4SLinus Torvalds 	int			committed;
14788f04c47aSChristoph Hellwig 	int			error = 0;
14798f04c47aSChristoph Hellwig 	int			done = 0;
14801da177e4SLinus Torvalds 
14810b56185bSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
14820b56185bSChristoph Hellwig 	ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
14830b56185bSChristoph Hellwig 	       xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1484ce7ae151SChristoph Hellwig 	ASSERT(new_size <= XFS_ISIZE(ip));
14858f04c47aSChristoph Hellwig 	ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
14861da177e4SLinus Torvalds 	ASSERT(ip->i_itemp != NULL);
1487898621d5SChristoph Hellwig 	ASSERT(ip->i_itemp->ili_lock_flags == 0);
14881da177e4SLinus Torvalds 	ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
14891da177e4SLinus Torvalds 
1490673e8e59SChristoph Hellwig 	trace_xfs_itruncate_extents_start(ip, new_size);
1491673e8e59SChristoph Hellwig 
14921da177e4SLinus Torvalds 	/*
14931da177e4SLinus Torvalds 	 * Since it is possible for space to become allocated beyond
14941da177e4SLinus Torvalds 	 * the end of the file (in a crash where the space is allocated
14951da177e4SLinus Torvalds 	 * but the inode size is not yet updated), simply remove any
14961da177e4SLinus Torvalds 	 * blocks which show up between the new EOF and the maximum
14971da177e4SLinus Torvalds 	 * possible file size.  If the first block to be removed is
14981da177e4SLinus Torvalds 	 * beyond the maximum file size (ie it is the same as last_block),
14991da177e4SLinus Torvalds 	 * then there is nothing to do.
15001da177e4SLinus Torvalds 	 */
15018f04c47aSChristoph Hellwig 	first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
150232972383SDave Chinner 	last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
15038f04c47aSChristoph Hellwig 	if (first_unmap_block == last_block)
15048f04c47aSChristoph Hellwig 		return 0;
15058f04c47aSChristoph Hellwig 
15068f04c47aSChristoph Hellwig 	ASSERT(first_unmap_block < last_block);
15071da177e4SLinus Torvalds 	unmap_len = last_block - first_unmap_block + 1;
15081da177e4SLinus Torvalds 	while (!done) {
15099d87c319SEric Sandeen 		xfs_bmap_init(&free_list, &first_block);
15108f04c47aSChristoph Hellwig 		error = xfs_bunmapi(tp, ip,
15113e57ecf6SOlaf Weber 				    first_unmap_block, unmap_len,
15128f04c47aSChristoph Hellwig 				    xfs_bmapi_aflag(whichfork),
15131da177e4SLinus Torvalds 				    XFS_ITRUNC_MAX_EXTENTS,
15143e57ecf6SOlaf Weber 				    &first_block, &free_list,
1515b4e9181eSChristoph Hellwig 				    &done);
15168f04c47aSChristoph Hellwig 		if (error)
15178f04c47aSChristoph Hellwig 			goto out_bmap_cancel;
15181da177e4SLinus Torvalds 
15191da177e4SLinus Torvalds 		/*
15201da177e4SLinus Torvalds 		 * Duplicate the transaction that has the permanent
15211da177e4SLinus Torvalds 		 * reservation and commit the old transaction.
15221da177e4SLinus Torvalds 		 */
15238f04c47aSChristoph Hellwig 		error = xfs_bmap_finish(&tp, &free_list, &committed);
1524898621d5SChristoph Hellwig 		if (committed)
1525ddc3415aSChristoph Hellwig 			xfs_trans_ijoin(tp, ip, 0);
15268f04c47aSChristoph Hellwig 		if (error)
15278f04c47aSChristoph Hellwig 			goto out_bmap_cancel;
15281da177e4SLinus Torvalds 
15291da177e4SLinus Torvalds 		if (committed) {
15301da177e4SLinus Torvalds 			/*
1531f6485057SDavid Chinner 			 * Mark the inode dirty so it will be logged and
1532e5720eecSDavid Chinner 			 * moved forward in the log as part of every commit.
15331da177e4SLinus Torvalds 			 */
15348f04c47aSChristoph Hellwig 			xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
15351da177e4SLinus Torvalds 		}
1536f6485057SDavid Chinner 
15378f04c47aSChristoph Hellwig 		ntp = xfs_trans_dup(tp);
15388f04c47aSChristoph Hellwig 		error = xfs_trans_commit(tp, 0);
15398f04c47aSChristoph Hellwig 		tp = ntp;
1540f6485057SDavid Chinner 
1541ddc3415aSChristoph Hellwig 		xfs_trans_ijoin(tp, ip, 0);
1542f6485057SDavid Chinner 
1543cc09c0dcSDave Chinner 		if (error)
15448f04c47aSChristoph Hellwig 			goto out;
15458f04c47aSChristoph Hellwig 
1546cc09c0dcSDave Chinner 		/*
15478f04c47aSChristoph Hellwig 		 * Transaction commit worked ok so we can drop the extra ticket
1548cc09c0dcSDave Chinner 		 * reference that we gained in xfs_trans_dup()
1549cc09c0dcSDave Chinner 		 */
15508f04c47aSChristoph Hellwig 		xfs_log_ticket_put(tp->t_ticket);
15513d3c8b52SJie Liu 		error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
15521da177e4SLinus Torvalds 		if (error)
15538f04c47aSChristoph Hellwig 			goto out;
15541da177e4SLinus Torvalds 	}
15558f04c47aSChristoph Hellwig 
1556673e8e59SChristoph Hellwig 	/*
1557673e8e59SChristoph Hellwig 	 * Always re-log the inode so that our permanent transaction can keep
1558673e8e59SChristoph Hellwig 	 * on rolling it forward in the log.
1559673e8e59SChristoph Hellwig 	 */
1560673e8e59SChristoph Hellwig 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1561673e8e59SChristoph Hellwig 
1562673e8e59SChristoph Hellwig 	trace_xfs_itruncate_extents_end(ip, new_size);
1563673e8e59SChristoph Hellwig 
15648f04c47aSChristoph Hellwig out:
15658f04c47aSChristoph Hellwig 	*tpp = tp;
15668f04c47aSChristoph Hellwig 	return error;
15678f04c47aSChristoph Hellwig out_bmap_cancel:
15681da177e4SLinus Torvalds 	/*
15698f04c47aSChristoph Hellwig 	 * If the bunmapi call encounters an error, return to the caller where
15708f04c47aSChristoph Hellwig 	 * the transaction can be properly aborted.  We just need to make sure
15718f04c47aSChristoph Hellwig 	 * we're not holding any resources that we were not when we came in.
15721da177e4SLinus Torvalds 	 */
15738f04c47aSChristoph Hellwig 	xfs_bmap_cancel(&free_list);
15748f04c47aSChristoph Hellwig 	goto out;
15758f04c47aSChristoph Hellwig }
15768f04c47aSChristoph Hellwig 
1577c24b5dfaSDave Chinner int
1578c24b5dfaSDave Chinner xfs_release(
1579c24b5dfaSDave Chinner 	xfs_inode_t	*ip)
1580c24b5dfaSDave Chinner {
1581c24b5dfaSDave Chinner 	xfs_mount_t	*mp = ip->i_mount;
1582c24b5dfaSDave Chinner 	int		error;
1583c24b5dfaSDave Chinner 
1584c24b5dfaSDave Chinner 	if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
1585c24b5dfaSDave Chinner 		return 0;
1586c24b5dfaSDave Chinner 
1587c24b5dfaSDave Chinner 	/* If this is a read-only mount, don't do this (would generate I/O) */
1588c24b5dfaSDave Chinner 	if (mp->m_flags & XFS_MOUNT_RDONLY)
1589c24b5dfaSDave Chinner 		return 0;
1590c24b5dfaSDave Chinner 
1591c24b5dfaSDave Chinner 	if (!XFS_FORCED_SHUTDOWN(mp)) {
1592c24b5dfaSDave Chinner 		int truncated;
1593c24b5dfaSDave Chinner 
1594c24b5dfaSDave Chinner 		/*
1595c24b5dfaSDave Chinner 		 * If we are using filestreams, and we have an unlinked
1596c24b5dfaSDave Chinner 		 * file that we are processing the last close on, then nothing
1597c24b5dfaSDave Chinner 		 * will be able to reopen and write to this file. Purge this
1598c24b5dfaSDave Chinner 		 * inode from the filestreams cache so that it doesn't delay
1599c24b5dfaSDave Chinner 		 * teardown of the inode.
1600c24b5dfaSDave Chinner 		 */
1601c24b5dfaSDave Chinner 		if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1602c24b5dfaSDave Chinner 			xfs_filestream_deassociate(ip);
1603c24b5dfaSDave Chinner 
1604c24b5dfaSDave Chinner 		/*
1605c24b5dfaSDave Chinner 		 * If we previously truncated this file and removed old data
1606c24b5dfaSDave Chinner 		 * in the process, we want to initiate "early" writeout on
1607c24b5dfaSDave Chinner 		 * the last close.  This is an attempt to combat the notorious
1608c24b5dfaSDave Chinner 		 * NULL files problem which is particularly noticeable from a
1609c24b5dfaSDave Chinner 		 * truncate down, buffered (re-)write (delalloc), followed by
1610c24b5dfaSDave Chinner 		 * a crash.  What we are effectively doing here is
1611c24b5dfaSDave Chinner 		 * significantly reducing the time window where we'd otherwise
1612c24b5dfaSDave Chinner 		 * be exposed to that problem.
1613c24b5dfaSDave Chinner 		 */
1614c24b5dfaSDave Chinner 		truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
1615c24b5dfaSDave Chinner 		if (truncated) {
1616c24b5dfaSDave Chinner 			xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
1617c24b5dfaSDave Chinner 			if (VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0) {
1618c24b5dfaSDave Chinner 				error = -filemap_flush(VFS_I(ip)->i_mapping);
1619c24b5dfaSDave Chinner 				if (error)
1620c24b5dfaSDave Chinner 					return error;
1621c24b5dfaSDave Chinner 			}
1622c24b5dfaSDave Chinner 		}
1623c24b5dfaSDave Chinner 	}
1624c24b5dfaSDave Chinner 
1625c24b5dfaSDave Chinner 	if (ip->i_d.di_nlink == 0)
1626c24b5dfaSDave Chinner 		return 0;
1627c24b5dfaSDave Chinner 
1628c24b5dfaSDave Chinner 	if (xfs_can_free_eofblocks(ip, false)) {
1629c24b5dfaSDave Chinner 
1630c24b5dfaSDave Chinner 		/*
1631c24b5dfaSDave Chinner 		 * If we can't get the iolock just skip truncating the blocks
1632c24b5dfaSDave Chinner 		 * past EOF because we could deadlock with the mmap_sem
1633c24b5dfaSDave Chinner 		 * otherwise.  We'll get another chance to drop them once the
1634c24b5dfaSDave Chinner 		 * last reference to the inode is dropped, so we'll never leak
1635c24b5dfaSDave Chinner 		 * blocks permanently.
1636c24b5dfaSDave Chinner 		 *
1637c24b5dfaSDave Chinner 		 * Further, check if the inode is being opened, written and
1638c24b5dfaSDave Chinner 		 * closed frequently and we have delayed allocation blocks
1639c24b5dfaSDave Chinner 		 * outstanding (e.g. streaming writes from the NFS server),
1640c24b5dfaSDave Chinner 		 * truncating the blocks past EOF will cause fragmentation to
1641c24b5dfaSDave Chinner 		 * occur.
1642c24b5dfaSDave Chinner 		 *
1643c24b5dfaSDave Chinner 		 * In this case don't do the truncation, either, but we have to
1644c24b5dfaSDave Chinner 		 * be careful how we detect this case. Blocks beyond EOF show
1645c24b5dfaSDave Chinner 		 * up as i_delayed_blks even when the inode is clean, so we
1646c24b5dfaSDave Chinner 		 * need to truncate them away first before checking for a dirty
1647c24b5dfaSDave Chinner 		 * release. Hence on the first dirty close we will still remove
1648c24b5dfaSDave Chinner 		 * the speculative allocation, but after that we will leave it
1649c24b5dfaSDave Chinner 		 * in place.
1650c24b5dfaSDave Chinner 		 */
1651c24b5dfaSDave Chinner 		if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
1652c24b5dfaSDave Chinner 			return 0;
1653c24b5dfaSDave Chinner 
1654c24b5dfaSDave Chinner 		error = xfs_free_eofblocks(mp, ip, true);
1655c24b5dfaSDave Chinner 		if (error && error != EAGAIN)
1656c24b5dfaSDave Chinner 			return error;
1657c24b5dfaSDave Chinner 
1658c24b5dfaSDave Chinner 		/* delalloc blocks after truncation means it really is dirty */
1659c24b5dfaSDave Chinner 		if (ip->i_delayed_blks)
1660c24b5dfaSDave Chinner 			xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
1661c24b5dfaSDave Chinner 	}
1662c24b5dfaSDave Chinner 	return 0;
1663c24b5dfaSDave Chinner }
1664c24b5dfaSDave Chinner 
1665c24b5dfaSDave Chinner /*
1666c24b5dfaSDave Chinner  * xfs_inactive
1667c24b5dfaSDave Chinner  *
1668c24b5dfaSDave Chinner  * This is called when the vnode reference count for the vnode
1669c24b5dfaSDave Chinner  * goes to zero.  If the file has been unlinked, then it must
1670c24b5dfaSDave Chinner  * now be truncated.  Also, we clear all of the read-ahead state
1671c24b5dfaSDave Chinner  * kept for the inode here since the file is now closed.
1672c24b5dfaSDave Chinner  */
1673c24b5dfaSDave Chinner int
1674c24b5dfaSDave Chinner xfs_inactive(
1675c24b5dfaSDave Chinner 	xfs_inode_t	*ip)
1676c24b5dfaSDave Chinner {
1677c24b5dfaSDave Chinner 	xfs_bmap_free_t		free_list;
1678c24b5dfaSDave Chinner 	xfs_fsblock_t		first_block;
1679c24b5dfaSDave Chinner 	int			committed;
16803d3c8b52SJie Liu 	struct xfs_trans	*tp;
16813d3c8b52SJie Liu 	struct xfs_mount	*mp;
16823d3c8b52SJie Liu 	struct xfs_trans_res	*resp;
1683c24b5dfaSDave Chinner 	int			error;
1684c24b5dfaSDave Chinner 	int			truncate = 0;
1685c24b5dfaSDave Chinner 
1686c24b5dfaSDave Chinner 	/*
1687c24b5dfaSDave Chinner 	 * If the inode is already free, then there can be nothing
1688c24b5dfaSDave Chinner 	 * to clean up here.
1689c24b5dfaSDave Chinner 	 */
1690d948709bSBen Myers 	if (ip->i_d.di_mode == 0) {
1691c24b5dfaSDave Chinner 		ASSERT(ip->i_df.if_real_bytes == 0);
1692c24b5dfaSDave Chinner 		ASSERT(ip->i_df.if_broot_bytes == 0);
1693c24b5dfaSDave Chinner 		return VN_INACTIVE_CACHE;
1694c24b5dfaSDave Chinner 	}
1695c24b5dfaSDave Chinner 
1696c24b5dfaSDave Chinner 	mp = ip->i_mount;
1697c24b5dfaSDave Chinner 
1698c24b5dfaSDave Chinner 	error = 0;
1699c24b5dfaSDave Chinner 
1700c24b5dfaSDave Chinner 	/* If this is a read-only mount, don't do this (would generate I/O) */
1701c24b5dfaSDave Chinner 	if (mp->m_flags & XFS_MOUNT_RDONLY)
1702c24b5dfaSDave Chinner 		goto out;
1703c24b5dfaSDave Chinner 
1704c24b5dfaSDave Chinner 	if (ip->i_d.di_nlink != 0) {
1705c24b5dfaSDave Chinner 		/*
1706c24b5dfaSDave Chinner 		 * force is true because we are evicting an inode from the
1707c24b5dfaSDave Chinner 		 * cache. Post-eof blocks must be freed, lest we end up with
1708c24b5dfaSDave Chinner 		 * broken free space accounting.
1709c24b5dfaSDave Chinner 		 */
1710c24b5dfaSDave Chinner 		if (xfs_can_free_eofblocks(ip, true)) {
1711c24b5dfaSDave Chinner 			error = xfs_free_eofblocks(mp, ip, false);
1712c24b5dfaSDave Chinner 			if (error)
1713c24b5dfaSDave Chinner 				return VN_INACTIVE_CACHE;
1714c24b5dfaSDave Chinner 		}
1715c24b5dfaSDave Chinner 		goto out;
1716c24b5dfaSDave Chinner 	}
1717c24b5dfaSDave Chinner 
1718c24b5dfaSDave Chinner 	if (S_ISREG(ip->i_d.di_mode) &&
1719c24b5dfaSDave Chinner 	    (ip->i_d.di_size != 0 || XFS_ISIZE(ip) != 0 ||
1720c24b5dfaSDave Chinner 	     ip->i_d.di_nextents > 0 || ip->i_delayed_blks > 0))
1721c24b5dfaSDave Chinner 		truncate = 1;
1722c24b5dfaSDave Chinner 
1723c24b5dfaSDave Chinner 	error = xfs_qm_dqattach(ip, 0);
1724c24b5dfaSDave Chinner 	if (error)
1725c24b5dfaSDave Chinner 		return VN_INACTIVE_CACHE;
1726c24b5dfaSDave Chinner 
1727*36b21ddeSBrian Foster 	if (S_ISLNK(ip->i_d.di_mode)) {
1728*36b21ddeSBrian Foster 		error = xfs_inactive_symlink(ip);
1729*36b21ddeSBrian Foster 		if (error)
1730*36b21ddeSBrian Foster 			goto out;
1731*36b21ddeSBrian Foster 	}
1732*36b21ddeSBrian Foster 
1733c24b5dfaSDave Chinner 	tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1734*36b21ddeSBrian Foster 	resp = truncate ? &M_RES(mp)->tr_itruncate : &M_RES(mp)->tr_ifree;
17353d3c8b52SJie Liu 
17363d3c8b52SJie Liu 	error = xfs_trans_reserve(tp, resp, 0, 0);
1737c24b5dfaSDave Chinner 	if (error) {
1738c24b5dfaSDave Chinner 		ASSERT(XFS_FORCED_SHUTDOWN(mp));
1739c24b5dfaSDave Chinner 		xfs_trans_cancel(tp, 0);
1740c24b5dfaSDave Chinner 		return VN_INACTIVE_CACHE;
1741c24b5dfaSDave Chinner 	}
1742c24b5dfaSDave Chinner 
1743c24b5dfaSDave Chinner 	xfs_ilock(ip, XFS_ILOCK_EXCL);
1744c24b5dfaSDave Chinner 	xfs_trans_ijoin(tp, ip, 0);
1745c24b5dfaSDave Chinner 
1746*36b21ddeSBrian Foster 	if (truncate) {
1747c24b5dfaSDave Chinner 		ip->i_d.di_size = 0;
1748c24b5dfaSDave Chinner 		xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1749c24b5dfaSDave Chinner 
1750c24b5dfaSDave Chinner 		error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
1751c24b5dfaSDave Chinner 		if (error)
1752c24b5dfaSDave Chinner 			goto out_cancel;
1753c24b5dfaSDave Chinner 
1754c24b5dfaSDave Chinner 		ASSERT(ip->i_d.di_nextents == 0);
1755c24b5dfaSDave Chinner 	}
1756c24b5dfaSDave Chinner 
1757c24b5dfaSDave Chinner 	/*
1758c24b5dfaSDave Chinner 	 * If there are attributes associated with the file then blow them away
1759c24b5dfaSDave Chinner 	 * now.  The code calls a routine that recursively deconstructs the
1760c24b5dfaSDave Chinner 	 * attribute fork.  We need to just commit the current transaction
1761c24b5dfaSDave Chinner 	 * because we can't use it for xfs_attr_inactive().
1762c24b5dfaSDave Chinner 	 */
1763c24b5dfaSDave Chinner 	if (ip->i_d.di_anextents > 0) {
1764c24b5dfaSDave Chinner 		ASSERT(ip->i_d.di_forkoff != 0);
1765c24b5dfaSDave Chinner 
1766c24b5dfaSDave Chinner 		error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1767c24b5dfaSDave Chinner 		if (error)
1768c24b5dfaSDave Chinner 			goto out_unlock;
1769c24b5dfaSDave Chinner 
1770c24b5dfaSDave Chinner 		xfs_iunlock(ip, XFS_ILOCK_EXCL);
1771c24b5dfaSDave Chinner 
1772c24b5dfaSDave Chinner 		error = xfs_attr_inactive(ip);
1773c24b5dfaSDave Chinner 		if (error)
1774c24b5dfaSDave Chinner 			goto out;
1775c24b5dfaSDave Chinner 
1776c24b5dfaSDave Chinner 		tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
17773d3c8b52SJie Liu 		error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ifree, 0, 0);
1778c24b5dfaSDave Chinner 		if (error) {
1779c24b5dfaSDave Chinner 			xfs_trans_cancel(tp, 0);
1780c24b5dfaSDave Chinner 			goto out;
1781c24b5dfaSDave Chinner 		}
1782c24b5dfaSDave Chinner 
1783c24b5dfaSDave Chinner 		xfs_ilock(ip, XFS_ILOCK_EXCL);
1784c24b5dfaSDave Chinner 		xfs_trans_ijoin(tp, ip, 0);
1785c24b5dfaSDave Chinner 	}
1786c24b5dfaSDave Chinner 
1787c24b5dfaSDave Chinner 	if (ip->i_afp)
1788c24b5dfaSDave Chinner 		xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1789c24b5dfaSDave Chinner 
1790c24b5dfaSDave Chinner 	ASSERT(ip->i_d.di_anextents == 0);
1791c24b5dfaSDave Chinner 
1792c24b5dfaSDave Chinner 	/*
1793c24b5dfaSDave Chinner 	 * Free the inode.
1794c24b5dfaSDave Chinner 	 */
1795c24b5dfaSDave Chinner 	xfs_bmap_init(&free_list, &first_block);
1796c24b5dfaSDave Chinner 	error = xfs_ifree(tp, ip, &free_list);
1797c24b5dfaSDave Chinner 	if (error) {
1798c24b5dfaSDave Chinner 		/*
1799c24b5dfaSDave Chinner 		 * If we fail to free the inode, shut down.  The cancel
1800c24b5dfaSDave Chinner 		 * might do that, we need to make sure.  Otherwise the
1801c24b5dfaSDave Chinner 		 * inode might be lost for a long time or forever.
1802c24b5dfaSDave Chinner 		 */
1803c24b5dfaSDave Chinner 		if (!XFS_FORCED_SHUTDOWN(mp)) {
1804c24b5dfaSDave Chinner 			xfs_notice(mp, "%s: xfs_ifree returned error %d",
1805c24b5dfaSDave Chinner 				__func__, error);
1806c24b5dfaSDave Chinner 			xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1807c24b5dfaSDave Chinner 		}
1808c24b5dfaSDave Chinner 		xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1809c24b5dfaSDave Chinner 	} else {
1810c24b5dfaSDave Chinner 		/*
1811c24b5dfaSDave Chinner 		 * Credit the quota account(s). The inode is gone.
1812c24b5dfaSDave Chinner 		 */
1813c24b5dfaSDave Chinner 		xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1814c24b5dfaSDave Chinner 
1815c24b5dfaSDave Chinner 		/*
1816c24b5dfaSDave Chinner 		 * Just ignore errors at this point.  There is nothing we can
1817c24b5dfaSDave Chinner 		 * do except to try to keep going. Make sure it's not a silent
1818c24b5dfaSDave Chinner 		 * error.
1819c24b5dfaSDave Chinner 		 */
1820c24b5dfaSDave Chinner 		error = xfs_bmap_finish(&tp,  &free_list, &committed);
1821c24b5dfaSDave Chinner 		if (error)
1822c24b5dfaSDave Chinner 			xfs_notice(mp, "%s: xfs_bmap_finish returned error %d",
1823c24b5dfaSDave Chinner 				__func__, error);
1824c24b5dfaSDave Chinner 		error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1825c24b5dfaSDave Chinner 		if (error)
1826c24b5dfaSDave Chinner 			xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
1827c24b5dfaSDave Chinner 				__func__, error);
1828c24b5dfaSDave Chinner 	}
1829c24b5dfaSDave Chinner 
1830c24b5dfaSDave Chinner 	/*
1831c24b5dfaSDave Chinner 	 * Release the dquots held by inode, if any.
1832c24b5dfaSDave Chinner 	 */
1833c24b5dfaSDave Chinner 	xfs_qm_dqdetach(ip);
1834c24b5dfaSDave Chinner out_unlock:
1835c24b5dfaSDave Chinner 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
1836c24b5dfaSDave Chinner out:
1837c24b5dfaSDave Chinner 	return VN_INACTIVE_CACHE;
1838c24b5dfaSDave Chinner out_cancel:
1839c24b5dfaSDave Chinner 	xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1840c24b5dfaSDave Chinner 	goto out_unlock;
1841c24b5dfaSDave Chinner }
1842c24b5dfaSDave Chinner 
18431da177e4SLinus Torvalds /*
18441da177e4SLinus Torvalds  * This is called when the inode's link count goes to 0.
18451da177e4SLinus Torvalds  * We place the on-disk inode on a list in the AGI.  It
18461da177e4SLinus Torvalds  * will be pulled from this list when the inode is freed.
18471da177e4SLinus Torvalds  */
18481da177e4SLinus Torvalds int
18491da177e4SLinus Torvalds xfs_iunlink(
18501da177e4SLinus Torvalds 	xfs_trans_t	*tp,
18511da177e4SLinus Torvalds 	xfs_inode_t	*ip)
18521da177e4SLinus Torvalds {
18531da177e4SLinus Torvalds 	xfs_mount_t	*mp;
18541da177e4SLinus Torvalds 	xfs_agi_t	*agi;
18551da177e4SLinus Torvalds 	xfs_dinode_t	*dip;
18561da177e4SLinus Torvalds 	xfs_buf_t	*agibp;
18571da177e4SLinus Torvalds 	xfs_buf_t	*ibp;
18581da177e4SLinus Torvalds 	xfs_agino_t	agino;
18591da177e4SLinus Torvalds 	short		bucket_index;
18601da177e4SLinus Torvalds 	int		offset;
18611da177e4SLinus Torvalds 	int		error;
18621da177e4SLinus Torvalds 
18631da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_nlink == 0);
18641da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_mode != 0);
18651da177e4SLinus Torvalds 
18661da177e4SLinus Torvalds 	mp = tp->t_mountp;
18671da177e4SLinus Torvalds 
18681da177e4SLinus Torvalds 	/*
18691da177e4SLinus Torvalds 	 * Get the agi buffer first.  It ensures lock ordering
18701da177e4SLinus Torvalds 	 * on the list.
18711da177e4SLinus Torvalds 	 */
18725e1be0fbSChristoph Hellwig 	error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
1873859d7182SVlad Apostolov 	if (error)
18741da177e4SLinus Torvalds 		return error;
18751da177e4SLinus Torvalds 	agi = XFS_BUF_TO_AGI(agibp);
18765e1be0fbSChristoph Hellwig 
18771da177e4SLinus Torvalds 	/*
18781da177e4SLinus Torvalds 	 * Get the index into the agi hash table for the
18791da177e4SLinus Torvalds 	 * list this inode will go on.
18801da177e4SLinus Torvalds 	 */
18811da177e4SLinus Torvalds 	agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
18821da177e4SLinus Torvalds 	ASSERT(agino != 0);
18831da177e4SLinus Torvalds 	bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
18841da177e4SLinus Torvalds 	ASSERT(agi->agi_unlinked[bucket_index]);
188516259e7dSChristoph Hellwig 	ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
18861da177e4SLinus Torvalds 
188769ef921bSChristoph Hellwig 	if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
18881da177e4SLinus Torvalds 		/*
18891da177e4SLinus Torvalds 		 * There is already another inode in the bucket we need
18901da177e4SLinus Torvalds 		 * to add ourselves to.  Add us at the front of the list.
18911da177e4SLinus Torvalds 		 * Here we put the head pointer into our next pointer,
18921da177e4SLinus Torvalds 		 * and then we fall through to point the head at us.
18931da177e4SLinus Torvalds 		 */
1894475ee413SChristoph Hellwig 		error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1895475ee413SChristoph Hellwig 				       0, 0);
1896c319b58bSVlad Apostolov 		if (error)
1897c319b58bSVlad Apostolov 			return error;
1898c319b58bSVlad Apostolov 
189969ef921bSChristoph Hellwig 		ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
19001da177e4SLinus Torvalds 		dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
190192bfc6e7SChristoph Hellwig 		offset = ip->i_imap.im_boffset +
19021da177e4SLinus Torvalds 			offsetof(xfs_dinode_t, di_next_unlinked);
19030a32c26eSDave Chinner 
19040a32c26eSDave Chinner 		/* need to recalc the inode CRC if appropriate */
19050a32c26eSDave Chinner 		xfs_dinode_calc_crc(mp, dip);
19060a32c26eSDave Chinner 
19071da177e4SLinus Torvalds 		xfs_trans_inode_buf(tp, ibp);
19081da177e4SLinus Torvalds 		xfs_trans_log_buf(tp, ibp, offset,
19091da177e4SLinus Torvalds 				  (offset + sizeof(xfs_agino_t) - 1));
19101da177e4SLinus Torvalds 		xfs_inobp_check(mp, ibp);
19111da177e4SLinus Torvalds 	}
19121da177e4SLinus Torvalds 
19131da177e4SLinus Torvalds 	/*
19141da177e4SLinus Torvalds 	 * Point the bucket head pointer at the inode being inserted.
19151da177e4SLinus Torvalds 	 */
19161da177e4SLinus Torvalds 	ASSERT(agino != 0);
191716259e7dSChristoph Hellwig 	agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
19181da177e4SLinus Torvalds 	offset = offsetof(xfs_agi_t, agi_unlinked) +
19191da177e4SLinus Torvalds 		(sizeof(xfs_agino_t) * bucket_index);
19201da177e4SLinus Torvalds 	xfs_trans_log_buf(tp, agibp, offset,
19211da177e4SLinus Torvalds 			  (offset + sizeof(xfs_agino_t) - 1));
19221da177e4SLinus Torvalds 	return 0;
19231da177e4SLinus Torvalds }
19241da177e4SLinus Torvalds 
19251da177e4SLinus Torvalds /*
19261da177e4SLinus Torvalds  * Pull the on-disk inode from the AGI unlinked list.
19271da177e4SLinus Torvalds  */
19281da177e4SLinus Torvalds STATIC int
19291da177e4SLinus Torvalds xfs_iunlink_remove(
19301da177e4SLinus Torvalds 	xfs_trans_t	*tp,
19311da177e4SLinus Torvalds 	xfs_inode_t	*ip)
19321da177e4SLinus Torvalds {
19331da177e4SLinus Torvalds 	xfs_ino_t	next_ino;
19341da177e4SLinus Torvalds 	xfs_mount_t	*mp;
19351da177e4SLinus Torvalds 	xfs_agi_t	*agi;
19361da177e4SLinus Torvalds 	xfs_dinode_t	*dip;
19371da177e4SLinus Torvalds 	xfs_buf_t	*agibp;
19381da177e4SLinus Torvalds 	xfs_buf_t	*ibp;
19391da177e4SLinus Torvalds 	xfs_agnumber_t	agno;
19401da177e4SLinus Torvalds 	xfs_agino_t	agino;
19411da177e4SLinus Torvalds 	xfs_agino_t	next_agino;
19421da177e4SLinus Torvalds 	xfs_buf_t	*last_ibp;
19436fdf8cccSNathan Scott 	xfs_dinode_t	*last_dip = NULL;
19441da177e4SLinus Torvalds 	short		bucket_index;
19456fdf8cccSNathan Scott 	int		offset, last_offset = 0;
19461da177e4SLinus Torvalds 	int		error;
19471da177e4SLinus Torvalds 
19481da177e4SLinus Torvalds 	mp = tp->t_mountp;
19491da177e4SLinus Torvalds 	agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
19501da177e4SLinus Torvalds 
19511da177e4SLinus Torvalds 	/*
19521da177e4SLinus Torvalds 	 * Get the agi buffer first.  It ensures lock ordering
19531da177e4SLinus Torvalds 	 * on the list.
19541da177e4SLinus Torvalds 	 */
19555e1be0fbSChristoph Hellwig 	error = xfs_read_agi(mp, tp, agno, &agibp);
19565e1be0fbSChristoph Hellwig 	if (error)
19571da177e4SLinus Torvalds 		return error;
19585e1be0fbSChristoph Hellwig 
19591da177e4SLinus Torvalds 	agi = XFS_BUF_TO_AGI(agibp);
19605e1be0fbSChristoph Hellwig 
19611da177e4SLinus Torvalds 	/*
19621da177e4SLinus Torvalds 	 * Get the index into the agi hash table for the
19631da177e4SLinus Torvalds 	 * list this inode will go on.
19641da177e4SLinus Torvalds 	 */
19651da177e4SLinus Torvalds 	agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
19661da177e4SLinus Torvalds 	ASSERT(agino != 0);
19671da177e4SLinus Torvalds 	bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
196869ef921bSChristoph Hellwig 	ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO));
19691da177e4SLinus Torvalds 	ASSERT(agi->agi_unlinked[bucket_index]);
19701da177e4SLinus Torvalds 
197116259e7dSChristoph Hellwig 	if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
19721da177e4SLinus Torvalds 		/*
1973475ee413SChristoph Hellwig 		 * We're at the head of the list.  Get the inode's on-disk
1974475ee413SChristoph Hellwig 		 * buffer to see if there is anyone after us on the list.
1975475ee413SChristoph Hellwig 		 * Only modify our next pointer if it is not already NULLAGINO.
1976475ee413SChristoph Hellwig 		 * This saves us the overhead of dealing with the buffer when
1977475ee413SChristoph Hellwig 		 * there is no need to change it.
19781da177e4SLinus Torvalds 		 */
1979475ee413SChristoph Hellwig 		error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1980475ee413SChristoph Hellwig 				       0, 0);
19811da177e4SLinus Torvalds 		if (error) {
1982475ee413SChristoph Hellwig 			xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
19830b932cccSDave Chinner 				__func__, error);
19841da177e4SLinus Torvalds 			return error;
19851da177e4SLinus Torvalds 		}
1986347d1c01SChristoph Hellwig 		next_agino = be32_to_cpu(dip->di_next_unlinked);
19871da177e4SLinus Torvalds 		ASSERT(next_agino != 0);
19881da177e4SLinus Torvalds 		if (next_agino != NULLAGINO) {
1989347d1c01SChristoph Hellwig 			dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
199092bfc6e7SChristoph Hellwig 			offset = ip->i_imap.im_boffset +
19911da177e4SLinus Torvalds 				offsetof(xfs_dinode_t, di_next_unlinked);
19920a32c26eSDave Chinner 
19930a32c26eSDave Chinner 			/* need to recalc the inode CRC if appropriate */
19940a32c26eSDave Chinner 			xfs_dinode_calc_crc(mp, dip);
19950a32c26eSDave Chinner 
19961da177e4SLinus Torvalds 			xfs_trans_inode_buf(tp, ibp);
19971da177e4SLinus Torvalds 			xfs_trans_log_buf(tp, ibp, offset,
19981da177e4SLinus Torvalds 					  (offset + sizeof(xfs_agino_t) - 1));
19991da177e4SLinus Torvalds 			xfs_inobp_check(mp, ibp);
20001da177e4SLinus Torvalds 		} else {
20011da177e4SLinus Torvalds 			xfs_trans_brelse(tp, ibp);
20021da177e4SLinus Torvalds 		}
20031da177e4SLinus Torvalds 		/*
20041da177e4SLinus Torvalds 		 * Point the bucket head pointer at the next inode.
20051da177e4SLinus Torvalds 		 */
20061da177e4SLinus Torvalds 		ASSERT(next_agino != 0);
20071da177e4SLinus Torvalds 		ASSERT(next_agino != agino);
200816259e7dSChristoph Hellwig 		agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
20091da177e4SLinus Torvalds 		offset = offsetof(xfs_agi_t, agi_unlinked) +
20101da177e4SLinus Torvalds 			(sizeof(xfs_agino_t) * bucket_index);
20111da177e4SLinus Torvalds 		xfs_trans_log_buf(tp, agibp, offset,
20121da177e4SLinus Torvalds 				  (offset + sizeof(xfs_agino_t) - 1));
20131da177e4SLinus Torvalds 	} else {
20141da177e4SLinus Torvalds 		/*
20151da177e4SLinus Torvalds 		 * We need to search the list for the inode being freed.
20161da177e4SLinus Torvalds 		 */
201716259e7dSChristoph Hellwig 		next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
20181da177e4SLinus Torvalds 		last_ibp = NULL;
20191da177e4SLinus Torvalds 		while (next_agino != agino) {
2020129dbc9aSChristoph Hellwig 			struct xfs_imap	imap;
2021129dbc9aSChristoph Hellwig 
2022129dbc9aSChristoph Hellwig 			if (last_ibp)
20231da177e4SLinus Torvalds 				xfs_trans_brelse(tp, last_ibp);
2024129dbc9aSChristoph Hellwig 
2025129dbc9aSChristoph Hellwig 			imap.im_blkno = 0;
20261da177e4SLinus Torvalds 			next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
2027129dbc9aSChristoph Hellwig 
2028129dbc9aSChristoph Hellwig 			error = xfs_imap(mp, tp, next_ino, &imap, 0);
20291da177e4SLinus Torvalds 			if (error) {
20300b932cccSDave Chinner 				xfs_warn(mp,
2031129dbc9aSChristoph Hellwig 	"%s: xfs_imap returned error %d.",
20320b932cccSDave Chinner 					 __func__, error);
20331da177e4SLinus Torvalds 				return error;
20341da177e4SLinus Torvalds 			}
2035129dbc9aSChristoph Hellwig 
2036129dbc9aSChristoph Hellwig 			error = xfs_imap_to_bp(mp, tp, &imap, &last_dip,
2037129dbc9aSChristoph Hellwig 					       &last_ibp, 0, 0);
2038129dbc9aSChristoph Hellwig 			if (error) {
2039129dbc9aSChristoph Hellwig 				xfs_warn(mp,
2040129dbc9aSChristoph Hellwig 	"%s: xfs_imap_to_bp returned error %d.",
2041129dbc9aSChristoph Hellwig 					__func__, error);
2042129dbc9aSChristoph Hellwig 				return error;
2043129dbc9aSChristoph Hellwig 			}
2044129dbc9aSChristoph Hellwig 
2045129dbc9aSChristoph Hellwig 			last_offset = imap.im_boffset;
2046347d1c01SChristoph Hellwig 			next_agino = be32_to_cpu(last_dip->di_next_unlinked);
20471da177e4SLinus Torvalds 			ASSERT(next_agino != NULLAGINO);
20481da177e4SLinus Torvalds 			ASSERT(next_agino != 0);
20491da177e4SLinus Torvalds 		}
2050475ee413SChristoph Hellwig 
20511da177e4SLinus Torvalds 		/*
2052475ee413SChristoph Hellwig 		 * Now last_ibp points to the buffer previous to us on the
2053475ee413SChristoph Hellwig 		 * unlinked list.  Pull us from the list.
20541da177e4SLinus Torvalds 		 */
2055475ee413SChristoph Hellwig 		error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
2056475ee413SChristoph Hellwig 				       0, 0);
20571da177e4SLinus Torvalds 		if (error) {
2058475ee413SChristoph Hellwig 			xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.",
20590b932cccSDave Chinner 				__func__, error);
20601da177e4SLinus Torvalds 			return error;
20611da177e4SLinus Torvalds 		}
2062347d1c01SChristoph Hellwig 		next_agino = be32_to_cpu(dip->di_next_unlinked);
20631da177e4SLinus Torvalds 		ASSERT(next_agino != 0);
20641da177e4SLinus Torvalds 		ASSERT(next_agino != agino);
20651da177e4SLinus Torvalds 		if (next_agino != NULLAGINO) {
2066347d1c01SChristoph Hellwig 			dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
206792bfc6e7SChristoph Hellwig 			offset = ip->i_imap.im_boffset +
20681da177e4SLinus Torvalds 				offsetof(xfs_dinode_t, di_next_unlinked);
20690a32c26eSDave Chinner 
20700a32c26eSDave Chinner 			/* need to recalc the inode CRC if appropriate */
20710a32c26eSDave Chinner 			xfs_dinode_calc_crc(mp, dip);
20720a32c26eSDave Chinner 
20731da177e4SLinus Torvalds 			xfs_trans_inode_buf(tp, ibp);
20741da177e4SLinus Torvalds 			xfs_trans_log_buf(tp, ibp, offset,
20751da177e4SLinus Torvalds 					  (offset + sizeof(xfs_agino_t) - 1));
20761da177e4SLinus Torvalds 			xfs_inobp_check(mp, ibp);
20771da177e4SLinus Torvalds 		} else {
20781da177e4SLinus Torvalds 			xfs_trans_brelse(tp, ibp);
20791da177e4SLinus Torvalds 		}
20801da177e4SLinus Torvalds 		/*
20811da177e4SLinus Torvalds 		 * Point the previous inode on the list to the next inode.
20821da177e4SLinus Torvalds 		 */
2083347d1c01SChristoph Hellwig 		last_dip->di_next_unlinked = cpu_to_be32(next_agino);
20841da177e4SLinus Torvalds 		ASSERT(next_agino != 0);
20851da177e4SLinus Torvalds 		offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
20860a32c26eSDave Chinner 
20870a32c26eSDave Chinner 		/* need to recalc the inode CRC if appropriate */
20880a32c26eSDave Chinner 		xfs_dinode_calc_crc(mp, last_dip);
20890a32c26eSDave Chinner 
20901da177e4SLinus Torvalds 		xfs_trans_inode_buf(tp, last_ibp);
20911da177e4SLinus Torvalds 		xfs_trans_log_buf(tp, last_ibp, offset,
20921da177e4SLinus Torvalds 				  (offset + sizeof(xfs_agino_t) - 1));
20931da177e4SLinus Torvalds 		xfs_inobp_check(mp, last_ibp);
20941da177e4SLinus Torvalds 	}
20951da177e4SLinus Torvalds 	return 0;
20961da177e4SLinus Torvalds }
20971da177e4SLinus Torvalds 
20985b3eed75SDave Chinner /*
20990b8182dbSZhi Yong Wu  * A big issue when freeing the inode cluster is that we _cannot_ skip any
21005b3eed75SDave Chinner  * inodes that are in memory - they all must be marked stale and attached to
21015b3eed75SDave Chinner  * the cluster buffer.
21025b3eed75SDave Chinner  */
21032a30f36dSChandra Seetharaman STATIC int
21041da177e4SLinus Torvalds xfs_ifree_cluster(
21051da177e4SLinus Torvalds 	xfs_inode_t	*free_ip,
21061da177e4SLinus Torvalds 	xfs_trans_t	*tp,
21071da177e4SLinus Torvalds 	xfs_ino_t	inum)
21081da177e4SLinus Torvalds {
21091da177e4SLinus Torvalds 	xfs_mount_t		*mp = free_ip->i_mount;
21101da177e4SLinus Torvalds 	int			blks_per_cluster;
21111da177e4SLinus Torvalds 	int			nbufs;
21121da177e4SLinus Torvalds 	int			ninodes;
21135b257b4aSDave Chinner 	int			i, j;
21141da177e4SLinus Torvalds 	xfs_daddr_t		blkno;
21151da177e4SLinus Torvalds 	xfs_buf_t		*bp;
21165b257b4aSDave Chinner 	xfs_inode_t		*ip;
21171da177e4SLinus Torvalds 	xfs_inode_log_item_t	*iip;
21181da177e4SLinus Torvalds 	xfs_log_item_t		*lip;
21195017e97dSDave Chinner 	struct xfs_perag	*pag;
21201da177e4SLinus Torvalds 
21215017e97dSDave Chinner 	pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
21221da177e4SLinus Torvalds 	if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
21231da177e4SLinus Torvalds 		blks_per_cluster = 1;
21241da177e4SLinus Torvalds 		ninodes = mp->m_sb.sb_inopblock;
21251da177e4SLinus Torvalds 		nbufs = XFS_IALLOC_BLOCKS(mp);
21261da177e4SLinus Torvalds 	} else {
21271da177e4SLinus Torvalds 		blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
21281da177e4SLinus Torvalds 					mp->m_sb.sb_blocksize;
21291da177e4SLinus Torvalds 		ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
21301da177e4SLinus Torvalds 		nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster;
21311da177e4SLinus Torvalds 	}
21321da177e4SLinus Torvalds 
21331da177e4SLinus Torvalds 	for (j = 0; j < nbufs; j++, inum += ninodes) {
21341da177e4SLinus Torvalds 		blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
21351da177e4SLinus Torvalds 					 XFS_INO_TO_AGBNO(mp, inum));
21361da177e4SLinus Torvalds 
21371da177e4SLinus Torvalds 		/*
21385b257b4aSDave Chinner 		 * We obtain and lock the backing buffer first in the process
21395b257b4aSDave Chinner 		 * here, as we have to ensure that any dirty inode that we
21405b257b4aSDave Chinner 		 * can't get the flush lock on is attached to the buffer.
21415b257b4aSDave Chinner 		 * If we scan the in-memory inodes first, then buffer IO can
21425b257b4aSDave Chinner 		 * complete before we get a lock on it, and hence we may fail
21435b257b4aSDave Chinner 		 * to mark all the active inodes on the buffer stale.
21441da177e4SLinus Torvalds 		 */
21451da177e4SLinus Torvalds 		bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
2146b6aff29fSDave Chinner 					mp->m_bsize * blks_per_cluster,
2147b6aff29fSDave Chinner 					XBF_UNMAPPED);
21481da177e4SLinus Torvalds 
21492a30f36dSChandra Seetharaman 		if (!bp)
21502a30f36dSChandra Seetharaman 			return ENOMEM;
2151b0f539deSDave Chinner 
2152b0f539deSDave Chinner 		/*
2153b0f539deSDave Chinner 		 * This buffer may not have been correctly initialised as we
2154b0f539deSDave Chinner 		 * didn't read it from disk. That's not important because we are
2155b0f539deSDave Chinner 		 * only using to mark the buffer as stale in the log, and to
2156b0f539deSDave Chinner 		 * attach stale cached inodes on it. That means it will never be
2157b0f539deSDave Chinner 		 * dispatched for IO. If it is, we want to know about it, and we
2158b0f539deSDave Chinner 		 * want it to fail. We can acheive this by adding a write
2159b0f539deSDave Chinner 		 * verifier to the buffer.
2160b0f539deSDave Chinner 		 */
21611813dd64SDave Chinner 		 bp->b_ops = &xfs_inode_buf_ops;
2162b0f539deSDave Chinner 
21635b257b4aSDave Chinner 		/*
21645b257b4aSDave Chinner 		 * Walk the inodes already attached to the buffer and mark them
21655b257b4aSDave Chinner 		 * stale. These will all have the flush locks held, so an
21665b3eed75SDave Chinner 		 * in-memory inode walk can't lock them. By marking them all
21675b3eed75SDave Chinner 		 * stale first, we will not attempt to lock them in the loop
21685b3eed75SDave Chinner 		 * below as the XFS_ISTALE flag will be set.
21695b257b4aSDave Chinner 		 */
2170adadbeefSChristoph Hellwig 		lip = bp->b_fspriv;
21711da177e4SLinus Torvalds 		while (lip) {
21721da177e4SLinus Torvalds 			if (lip->li_type == XFS_LI_INODE) {
21731da177e4SLinus Torvalds 				iip = (xfs_inode_log_item_t *)lip;
21741da177e4SLinus Torvalds 				ASSERT(iip->ili_logged == 1);
2175ca30b2a7SChristoph Hellwig 				lip->li_cb = xfs_istale_done;
21767b2e2a31SDavid Chinner 				xfs_trans_ail_copy_lsn(mp->m_ail,
21777b2e2a31SDavid Chinner 							&iip->ili_flush_lsn,
21787b2e2a31SDavid Chinner 							&iip->ili_item.li_lsn);
2179e5ffd2bbSDavid Chinner 				xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
21801da177e4SLinus Torvalds 			}
21811da177e4SLinus Torvalds 			lip = lip->li_bio_list;
21821da177e4SLinus Torvalds 		}
21831da177e4SLinus Torvalds 
21845b3eed75SDave Chinner 
21855b257b4aSDave Chinner 		/*
21865b257b4aSDave Chinner 		 * For each inode in memory attempt to add it to the inode
21875b257b4aSDave Chinner 		 * buffer and set it up for being staled on buffer IO
21885b257b4aSDave Chinner 		 * completion.  This is safe as we've locked out tail pushing
21895b257b4aSDave Chinner 		 * and flushing by locking the buffer.
21905b257b4aSDave Chinner 		 *
21915b257b4aSDave Chinner 		 * We have already marked every inode that was part of a
21925b257b4aSDave Chinner 		 * transaction stale above, which means there is no point in
21935b257b4aSDave Chinner 		 * even trying to lock them.
21945b257b4aSDave Chinner 		 */
21955b257b4aSDave Chinner 		for (i = 0; i < ninodes; i++) {
21965b3eed75SDave Chinner retry:
21971a3e8f3dSDave Chinner 			rcu_read_lock();
21985b257b4aSDave Chinner 			ip = radix_tree_lookup(&pag->pag_ici_root,
21995b257b4aSDave Chinner 					XFS_INO_TO_AGINO(mp, (inum + i)));
22001da177e4SLinus Torvalds 
22011a3e8f3dSDave Chinner 			/* Inode not in memory, nothing to do */
22021a3e8f3dSDave Chinner 			if (!ip) {
22031a3e8f3dSDave Chinner 				rcu_read_unlock();
22045b257b4aSDave Chinner 				continue;
22055b257b4aSDave Chinner 			}
22065b257b4aSDave Chinner 
22075b3eed75SDave Chinner 			/*
22081a3e8f3dSDave Chinner 			 * because this is an RCU protected lookup, we could
22091a3e8f3dSDave Chinner 			 * find a recently freed or even reallocated inode
22101a3e8f3dSDave Chinner 			 * during the lookup. We need to check under the
22111a3e8f3dSDave Chinner 			 * i_flags_lock for a valid inode here. Skip it if it
22121a3e8f3dSDave Chinner 			 * is not valid, the wrong inode or stale.
22131a3e8f3dSDave Chinner 			 */
22141a3e8f3dSDave Chinner 			spin_lock(&ip->i_flags_lock);
22151a3e8f3dSDave Chinner 			if (ip->i_ino != inum + i ||
22161a3e8f3dSDave Chinner 			    __xfs_iflags_test(ip, XFS_ISTALE)) {
22171a3e8f3dSDave Chinner 				spin_unlock(&ip->i_flags_lock);
22181a3e8f3dSDave Chinner 				rcu_read_unlock();
22191a3e8f3dSDave Chinner 				continue;
22201a3e8f3dSDave Chinner 			}
22211a3e8f3dSDave Chinner 			spin_unlock(&ip->i_flags_lock);
22221a3e8f3dSDave Chinner 
22231a3e8f3dSDave Chinner 			/*
22245b3eed75SDave Chinner 			 * Don't try to lock/unlock the current inode, but we
22255b3eed75SDave Chinner 			 * _cannot_ skip the other inodes that we did not find
22265b3eed75SDave Chinner 			 * in the list attached to the buffer and are not
22275b3eed75SDave Chinner 			 * already marked stale. If we can't lock it, back off
22285b3eed75SDave Chinner 			 * and retry.
22295b3eed75SDave Chinner 			 */
22305b257b4aSDave Chinner 			if (ip != free_ip &&
22315b257b4aSDave Chinner 			    !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
22321a3e8f3dSDave Chinner 				rcu_read_unlock();
22335b3eed75SDave Chinner 				delay(1);
22345b3eed75SDave Chinner 				goto retry;
22355b257b4aSDave Chinner 			}
22361a3e8f3dSDave Chinner 			rcu_read_unlock();
22375b257b4aSDave Chinner 
22385b3eed75SDave Chinner 			xfs_iflock(ip);
22395b257b4aSDave Chinner 			xfs_iflags_set(ip, XFS_ISTALE);
22405b257b4aSDave Chinner 
22415b3eed75SDave Chinner 			/*
22425b3eed75SDave Chinner 			 * we don't need to attach clean inodes or those only
22435b3eed75SDave Chinner 			 * with unlogged changes (which we throw away, anyway).
22445b3eed75SDave Chinner 			 */
22455b257b4aSDave Chinner 			iip = ip->i_itemp;
22465b3eed75SDave Chinner 			if (!iip || xfs_inode_clean(ip)) {
22475b257b4aSDave Chinner 				ASSERT(ip != free_ip);
22481da177e4SLinus Torvalds 				xfs_ifunlock(ip);
22491da177e4SLinus Torvalds 				xfs_iunlock(ip, XFS_ILOCK_EXCL);
22501da177e4SLinus Torvalds 				continue;
22511da177e4SLinus Torvalds 			}
22521da177e4SLinus Torvalds 
2253f5d8d5c4SChristoph Hellwig 			iip->ili_last_fields = iip->ili_fields;
2254f5d8d5c4SChristoph Hellwig 			iip->ili_fields = 0;
22551da177e4SLinus Torvalds 			iip->ili_logged = 1;
22567b2e2a31SDavid Chinner 			xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
22577b2e2a31SDavid Chinner 						&iip->ili_item.li_lsn);
22581da177e4SLinus Torvalds 
2259ca30b2a7SChristoph Hellwig 			xfs_buf_attach_iodone(bp, xfs_istale_done,
2260ca30b2a7SChristoph Hellwig 						  &iip->ili_item);
22615b257b4aSDave Chinner 
22625b257b4aSDave Chinner 			if (ip != free_ip)
22631da177e4SLinus Torvalds 				xfs_iunlock(ip, XFS_ILOCK_EXCL);
22641da177e4SLinus Torvalds 		}
22651da177e4SLinus Torvalds 
22661da177e4SLinus Torvalds 		xfs_trans_stale_inode_buf(tp, bp);
22671da177e4SLinus Torvalds 		xfs_trans_binval(tp, bp);
22681da177e4SLinus Torvalds 	}
22691da177e4SLinus Torvalds 
22705017e97dSDave Chinner 	xfs_perag_put(pag);
22712a30f36dSChandra Seetharaman 	return 0;
22721da177e4SLinus Torvalds }
22731da177e4SLinus Torvalds 
22741da177e4SLinus Torvalds /*
22751da177e4SLinus Torvalds  * This is called to return an inode to the inode free list.
22761da177e4SLinus Torvalds  * The inode should already be truncated to 0 length and have
22771da177e4SLinus Torvalds  * no pages associated with it.  This routine also assumes that
22781da177e4SLinus Torvalds  * the inode is already a part of the transaction.
22791da177e4SLinus Torvalds  *
22801da177e4SLinus Torvalds  * The on-disk copy of the inode will have been added to the list
22811da177e4SLinus Torvalds  * of unlinked inodes in the AGI. We need to remove the inode from
22821da177e4SLinus Torvalds  * that list atomically with respect to freeing it here.
22831da177e4SLinus Torvalds  */
22841da177e4SLinus Torvalds int
22851da177e4SLinus Torvalds xfs_ifree(
22861da177e4SLinus Torvalds 	xfs_trans_t	*tp,
22871da177e4SLinus Torvalds 	xfs_inode_t	*ip,
22881da177e4SLinus Torvalds 	xfs_bmap_free_t	*flist)
22891da177e4SLinus Torvalds {
22901da177e4SLinus Torvalds 	int			error;
22911da177e4SLinus Torvalds 	int			delete;
22921da177e4SLinus Torvalds 	xfs_ino_t		first_ino;
22931da177e4SLinus Torvalds 
2294579aa9caSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
22951da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_nlink == 0);
22961da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_nextents == 0);
22971da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_anextents == 0);
2298ce7ae151SChristoph Hellwig 	ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode));
22991da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_nblocks == 0);
23001da177e4SLinus Torvalds 
23011da177e4SLinus Torvalds 	/*
23021da177e4SLinus Torvalds 	 * Pull the on-disk inode from the AGI unlinked list.
23031da177e4SLinus Torvalds 	 */
23041da177e4SLinus Torvalds 	error = xfs_iunlink_remove(tp, ip);
23051baaed8fSDave Chinner 	if (error)
23061da177e4SLinus Torvalds 		return error;
23071da177e4SLinus Torvalds 
23081da177e4SLinus Torvalds 	error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino);
23091baaed8fSDave Chinner 	if (error)
23101da177e4SLinus Torvalds 		return error;
23111baaed8fSDave Chinner 
23121da177e4SLinus Torvalds 	ip->i_d.di_mode = 0;		/* mark incore inode as free */
23131da177e4SLinus Torvalds 	ip->i_d.di_flags = 0;
23141da177e4SLinus Torvalds 	ip->i_d.di_dmevmask = 0;
23151da177e4SLinus Torvalds 	ip->i_d.di_forkoff = 0;		/* mark the attr fork not in use */
23161da177e4SLinus Torvalds 	ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
23171da177e4SLinus Torvalds 	ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
23181da177e4SLinus Torvalds 	/*
23191da177e4SLinus Torvalds 	 * Bump the generation count so no one will be confused
23201da177e4SLinus Torvalds 	 * by reincarnations of this inode.
23211da177e4SLinus Torvalds 	 */
23221da177e4SLinus Torvalds 	ip->i_d.di_gen++;
23231da177e4SLinus Torvalds 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
23241da177e4SLinus Torvalds 
23251baaed8fSDave Chinner 	if (delete)
23262a30f36dSChandra Seetharaman 		error = xfs_ifree_cluster(ip, tp, first_ino);
23271da177e4SLinus Torvalds 
23282a30f36dSChandra Seetharaman 	return error;
23291da177e4SLinus Torvalds }
23301da177e4SLinus Torvalds 
23311da177e4SLinus Torvalds /*
233260ec6783SChristoph Hellwig  * This is called to unpin an inode.  The caller must have the inode locked
233360ec6783SChristoph Hellwig  * in at least shared mode so that the buffer cannot be subsequently pinned
233460ec6783SChristoph Hellwig  * once someone is waiting for it to be unpinned.
23351da177e4SLinus Torvalds  */
233660ec6783SChristoph Hellwig static void
2337f392e631SChristoph Hellwig xfs_iunpin(
233860ec6783SChristoph Hellwig 	struct xfs_inode	*ip)
2339a3f74ffbSDavid Chinner {
2340579aa9caSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
2341a3f74ffbSDavid Chinner 
23424aaf15d1SDave Chinner 	trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
23434aaf15d1SDave Chinner 
2344a3f74ffbSDavid Chinner 	/* Give the log a push to start the unpinning I/O */
234560ec6783SChristoph Hellwig 	xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0);
2346a14a348bSChristoph Hellwig 
2347a3f74ffbSDavid Chinner }
2348a3f74ffbSDavid Chinner 
2349f392e631SChristoph Hellwig static void
2350f392e631SChristoph Hellwig __xfs_iunpin_wait(
2351f392e631SChristoph Hellwig 	struct xfs_inode	*ip)
2352f392e631SChristoph Hellwig {
2353f392e631SChristoph Hellwig 	wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
2354f392e631SChristoph Hellwig 	DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
2355f392e631SChristoph Hellwig 
2356f392e631SChristoph Hellwig 	xfs_iunpin(ip);
2357f392e631SChristoph Hellwig 
2358f392e631SChristoph Hellwig 	do {
2359f392e631SChristoph Hellwig 		prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2360f392e631SChristoph Hellwig 		if (xfs_ipincount(ip))
2361f392e631SChristoph Hellwig 			io_schedule();
2362f392e631SChristoph Hellwig 	} while (xfs_ipincount(ip));
2363f392e631SChristoph Hellwig 	finish_wait(wq, &wait.wait);
2364f392e631SChristoph Hellwig }
2365f392e631SChristoph Hellwig 
2366777df5afSDave Chinner void
23671da177e4SLinus Torvalds xfs_iunpin_wait(
236860ec6783SChristoph Hellwig 	struct xfs_inode	*ip)
23691da177e4SLinus Torvalds {
2370f392e631SChristoph Hellwig 	if (xfs_ipincount(ip))
2371f392e631SChristoph Hellwig 		__xfs_iunpin_wait(ip);
23721da177e4SLinus Torvalds }
23731da177e4SLinus Torvalds 
2374c24b5dfaSDave Chinner int
2375c24b5dfaSDave Chinner xfs_remove(
2376c24b5dfaSDave Chinner 	xfs_inode_t             *dp,
2377c24b5dfaSDave Chinner 	struct xfs_name		*name,
2378c24b5dfaSDave Chinner 	xfs_inode_t		*ip)
2379c24b5dfaSDave Chinner {
2380c24b5dfaSDave Chinner 	xfs_mount_t		*mp = dp->i_mount;
2381c24b5dfaSDave Chinner 	xfs_trans_t             *tp = NULL;
2382c24b5dfaSDave Chinner 	int			is_dir = S_ISDIR(ip->i_d.di_mode);
2383c24b5dfaSDave Chinner 	int                     error = 0;
2384c24b5dfaSDave Chinner 	xfs_bmap_free_t         free_list;
2385c24b5dfaSDave Chinner 	xfs_fsblock_t           first_block;
2386c24b5dfaSDave Chinner 	int			cancel_flags;
2387c24b5dfaSDave Chinner 	int			committed;
2388c24b5dfaSDave Chinner 	int			link_zero;
2389c24b5dfaSDave Chinner 	uint			resblks;
2390c24b5dfaSDave Chinner 	uint			log_count;
2391c24b5dfaSDave Chinner 
2392c24b5dfaSDave Chinner 	trace_xfs_remove(dp, name);
2393c24b5dfaSDave Chinner 
2394c24b5dfaSDave Chinner 	if (XFS_FORCED_SHUTDOWN(mp))
2395c24b5dfaSDave Chinner 		return XFS_ERROR(EIO);
2396c24b5dfaSDave Chinner 
2397c24b5dfaSDave Chinner 	error = xfs_qm_dqattach(dp, 0);
2398c24b5dfaSDave Chinner 	if (error)
2399c24b5dfaSDave Chinner 		goto std_return;
2400c24b5dfaSDave Chinner 
2401c24b5dfaSDave Chinner 	error = xfs_qm_dqattach(ip, 0);
2402c24b5dfaSDave Chinner 	if (error)
2403c24b5dfaSDave Chinner 		goto std_return;
2404c24b5dfaSDave Chinner 
2405c24b5dfaSDave Chinner 	if (is_dir) {
2406c24b5dfaSDave Chinner 		tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
2407c24b5dfaSDave Chinner 		log_count = XFS_DEFAULT_LOG_COUNT;
2408c24b5dfaSDave Chinner 	} else {
2409c24b5dfaSDave Chinner 		tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
2410c24b5dfaSDave Chinner 		log_count = XFS_REMOVE_LOG_COUNT;
2411c24b5dfaSDave Chinner 	}
2412c24b5dfaSDave Chinner 	cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2413c24b5dfaSDave Chinner 
2414c24b5dfaSDave Chinner 	/*
2415c24b5dfaSDave Chinner 	 * We try to get the real space reservation first,
2416c24b5dfaSDave Chinner 	 * allowing for directory btree deletion(s) implying
2417c24b5dfaSDave Chinner 	 * possible bmap insert(s).  If we can't get the space
2418c24b5dfaSDave Chinner 	 * reservation then we use 0 instead, and avoid the bmap
2419c24b5dfaSDave Chinner 	 * btree insert(s) in the directory code by, if the bmap
2420c24b5dfaSDave Chinner 	 * insert tries to happen, instead trimming the LAST
2421c24b5dfaSDave Chinner 	 * block from the directory.
2422c24b5dfaSDave Chinner 	 */
2423c24b5dfaSDave Chinner 	resblks = XFS_REMOVE_SPACE_RES(mp);
24243d3c8b52SJie Liu 	error = xfs_trans_reserve(tp, &M_RES(mp)->tr_remove, resblks, 0);
2425c24b5dfaSDave Chinner 	if (error == ENOSPC) {
2426c24b5dfaSDave Chinner 		resblks = 0;
24273d3c8b52SJie Liu 		error = xfs_trans_reserve(tp, &M_RES(mp)->tr_remove, 0, 0);
2428c24b5dfaSDave Chinner 	}
2429c24b5dfaSDave Chinner 	if (error) {
2430c24b5dfaSDave Chinner 		ASSERT(error != ENOSPC);
2431c24b5dfaSDave Chinner 		cancel_flags = 0;
2432c24b5dfaSDave Chinner 		goto out_trans_cancel;
2433c24b5dfaSDave Chinner 	}
2434c24b5dfaSDave Chinner 
2435c24b5dfaSDave Chinner 	xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
2436c24b5dfaSDave Chinner 
2437c24b5dfaSDave Chinner 	xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2438c24b5dfaSDave Chinner 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2439c24b5dfaSDave Chinner 
2440c24b5dfaSDave Chinner 	/*
2441c24b5dfaSDave Chinner 	 * If we're removing a directory perform some additional validation.
2442c24b5dfaSDave Chinner 	 */
2443c24b5dfaSDave Chinner 	if (is_dir) {
2444c24b5dfaSDave Chinner 		ASSERT(ip->i_d.di_nlink >= 2);
2445c24b5dfaSDave Chinner 		if (ip->i_d.di_nlink != 2) {
2446c24b5dfaSDave Chinner 			error = XFS_ERROR(ENOTEMPTY);
2447c24b5dfaSDave Chinner 			goto out_trans_cancel;
2448c24b5dfaSDave Chinner 		}
2449c24b5dfaSDave Chinner 		if (!xfs_dir_isempty(ip)) {
2450c24b5dfaSDave Chinner 			error = XFS_ERROR(ENOTEMPTY);
2451c24b5dfaSDave Chinner 			goto out_trans_cancel;
2452c24b5dfaSDave Chinner 		}
2453c24b5dfaSDave Chinner 	}
2454c24b5dfaSDave Chinner 
2455c24b5dfaSDave Chinner 	xfs_bmap_init(&free_list, &first_block);
2456c24b5dfaSDave Chinner 	error = xfs_dir_removename(tp, dp, name, ip->i_ino,
2457c24b5dfaSDave Chinner 					&first_block, &free_list, resblks);
2458c24b5dfaSDave Chinner 	if (error) {
2459c24b5dfaSDave Chinner 		ASSERT(error != ENOENT);
2460c24b5dfaSDave Chinner 		goto out_bmap_cancel;
2461c24b5dfaSDave Chinner 	}
2462c24b5dfaSDave Chinner 	xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2463c24b5dfaSDave Chinner 
2464c24b5dfaSDave Chinner 	if (is_dir) {
2465c24b5dfaSDave Chinner 		/*
2466c24b5dfaSDave Chinner 		 * Drop the link from ip's "..".
2467c24b5dfaSDave Chinner 		 */
2468c24b5dfaSDave Chinner 		error = xfs_droplink(tp, dp);
2469c24b5dfaSDave Chinner 		if (error)
2470c24b5dfaSDave Chinner 			goto out_bmap_cancel;
2471c24b5dfaSDave Chinner 
2472c24b5dfaSDave Chinner 		/*
2473c24b5dfaSDave Chinner 		 * Drop the "." link from ip to self.
2474c24b5dfaSDave Chinner 		 */
2475c24b5dfaSDave Chinner 		error = xfs_droplink(tp, ip);
2476c24b5dfaSDave Chinner 		if (error)
2477c24b5dfaSDave Chinner 			goto out_bmap_cancel;
2478c24b5dfaSDave Chinner 	} else {
2479c24b5dfaSDave Chinner 		/*
2480c24b5dfaSDave Chinner 		 * When removing a non-directory we need to log the parent
2481c24b5dfaSDave Chinner 		 * inode here.  For a directory this is done implicitly
2482c24b5dfaSDave Chinner 		 * by the xfs_droplink call for the ".." entry.
2483c24b5dfaSDave Chinner 		 */
2484c24b5dfaSDave Chinner 		xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2485c24b5dfaSDave Chinner 	}
2486c24b5dfaSDave Chinner 
2487c24b5dfaSDave Chinner 	/*
2488c24b5dfaSDave Chinner 	 * Drop the link from dp to ip.
2489c24b5dfaSDave Chinner 	 */
2490c24b5dfaSDave Chinner 	error = xfs_droplink(tp, ip);
2491c24b5dfaSDave Chinner 	if (error)
2492c24b5dfaSDave Chinner 		goto out_bmap_cancel;
2493c24b5dfaSDave Chinner 
2494c24b5dfaSDave Chinner 	/*
2495c24b5dfaSDave Chinner 	 * Determine if this is the last link while
2496c24b5dfaSDave Chinner 	 * we are in the transaction.
2497c24b5dfaSDave Chinner 	 */
2498c24b5dfaSDave Chinner 	link_zero = (ip->i_d.di_nlink == 0);
2499c24b5dfaSDave Chinner 
2500c24b5dfaSDave Chinner 	/*
2501c24b5dfaSDave Chinner 	 * If this is a synchronous mount, make sure that the
2502c24b5dfaSDave Chinner 	 * remove transaction goes to disk before returning to
2503c24b5dfaSDave Chinner 	 * the user.
2504c24b5dfaSDave Chinner 	 */
2505c24b5dfaSDave Chinner 	if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
2506c24b5dfaSDave Chinner 		xfs_trans_set_sync(tp);
2507c24b5dfaSDave Chinner 
2508c24b5dfaSDave Chinner 	error = xfs_bmap_finish(&tp, &free_list, &committed);
2509c24b5dfaSDave Chinner 	if (error)
2510c24b5dfaSDave Chinner 		goto out_bmap_cancel;
2511c24b5dfaSDave Chinner 
2512c24b5dfaSDave Chinner 	error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2513c24b5dfaSDave Chinner 	if (error)
2514c24b5dfaSDave Chinner 		goto std_return;
2515c24b5dfaSDave Chinner 
2516c24b5dfaSDave Chinner 	/*
2517c24b5dfaSDave Chinner 	 * If we are using filestreams, kill the stream association.
2518c24b5dfaSDave Chinner 	 * If the file is still open it may get a new one but that
2519c24b5dfaSDave Chinner 	 * will get killed on last close in xfs_close() so we don't
2520c24b5dfaSDave Chinner 	 * have to worry about that.
2521c24b5dfaSDave Chinner 	 */
2522c24b5dfaSDave Chinner 	if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
2523c24b5dfaSDave Chinner 		xfs_filestream_deassociate(ip);
2524c24b5dfaSDave Chinner 
2525c24b5dfaSDave Chinner 	return 0;
2526c24b5dfaSDave Chinner 
2527c24b5dfaSDave Chinner  out_bmap_cancel:
2528c24b5dfaSDave Chinner 	xfs_bmap_cancel(&free_list);
2529c24b5dfaSDave Chinner 	cancel_flags |= XFS_TRANS_ABORT;
2530c24b5dfaSDave Chinner  out_trans_cancel:
2531c24b5dfaSDave Chinner 	xfs_trans_cancel(tp, cancel_flags);
2532c24b5dfaSDave Chinner  std_return:
2533c24b5dfaSDave Chinner 	return error;
2534c24b5dfaSDave Chinner }
2535c24b5dfaSDave Chinner 
2536f6bba201SDave Chinner /*
2537f6bba201SDave Chinner  * Enter all inodes for a rename transaction into a sorted array.
2538f6bba201SDave Chinner  */
2539f6bba201SDave Chinner STATIC void
2540f6bba201SDave Chinner xfs_sort_for_rename(
2541f6bba201SDave Chinner 	xfs_inode_t	*dp1,	/* in: old (source) directory inode */
2542f6bba201SDave Chinner 	xfs_inode_t	*dp2,	/* in: new (target) directory inode */
2543f6bba201SDave Chinner 	xfs_inode_t	*ip1,	/* in: inode of old entry */
2544f6bba201SDave Chinner 	xfs_inode_t	*ip2,	/* in: inode of new entry, if it
2545f6bba201SDave Chinner 				   already exists, NULL otherwise. */
2546f6bba201SDave Chinner 	xfs_inode_t	**i_tab,/* out: array of inode returned, sorted */
2547f6bba201SDave Chinner 	int		*num_inodes)  /* out: number of inodes in array */
2548f6bba201SDave Chinner {
2549f6bba201SDave Chinner 	xfs_inode_t		*temp;
2550f6bba201SDave Chinner 	int			i, j;
2551f6bba201SDave Chinner 
2552f6bba201SDave Chinner 	/*
2553f6bba201SDave Chinner 	 * i_tab contains a list of pointers to inodes.  We initialize
2554f6bba201SDave Chinner 	 * the table here & we'll sort it.  We will then use it to
2555f6bba201SDave Chinner 	 * order the acquisition of the inode locks.
2556f6bba201SDave Chinner 	 *
2557f6bba201SDave Chinner 	 * Note that the table may contain duplicates.  e.g., dp1 == dp2.
2558f6bba201SDave Chinner 	 */
2559f6bba201SDave Chinner 	i_tab[0] = dp1;
2560f6bba201SDave Chinner 	i_tab[1] = dp2;
2561f6bba201SDave Chinner 	i_tab[2] = ip1;
2562f6bba201SDave Chinner 	if (ip2) {
2563f6bba201SDave Chinner 		*num_inodes = 4;
2564f6bba201SDave Chinner 		i_tab[3] = ip2;
2565f6bba201SDave Chinner 	} else {
2566f6bba201SDave Chinner 		*num_inodes = 3;
2567f6bba201SDave Chinner 		i_tab[3] = NULL;
2568f6bba201SDave Chinner 	}
2569f6bba201SDave Chinner 
2570f6bba201SDave Chinner 	/*
2571f6bba201SDave Chinner 	 * Sort the elements via bubble sort.  (Remember, there are at
2572f6bba201SDave Chinner 	 * most 4 elements to sort, so this is adequate.)
2573f6bba201SDave Chinner 	 */
2574f6bba201SDave Chinner 	for (i = 0; i < *num_inodes; i++) {
2575f6bba201SDave Chinner 		for (j = 1; j < *num_inodes; j++) {
2576f6bba201SDave Chinner 			if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) {
2577f6bba201SDave Chinner 				temp = i_tab[j];
2578f6bba201SDave Chinner 				i_tab[j] = i_tab[j-1];
2579f6bba201SDave Chinner 				i_tab[j-1] = temp;
2580f6bba201SDave Chinner 			}
2581f6bba201SDave Chinner 		}
2582f6bba201SDave Chinner 	}
2583f6bba201SDave Chinner }
2584f6bba201SDave Chinner 
2585f6bba201SDave Chinner /*
2586f6bba201SDave Chinner  * xfs_rename
2587f6bba201SDave Chinner  */
2588f6bba201SDave Chinner int
2589f6bba201SDave Chinner xfs_rename(
2590f6bba201SDave Chinner 	xfs_inode_t	*src_dp,
2591f6bba201SDave Chinner 	struct xfs_name	*src_name,
2592f6bba201SDave Chinner 	xfs_inode_t	*src_ip,
2593f6bba201SDave Chinner 	xfs_inode_t	*target_dp,
2594f6bba201SDave Chinner 	struct xfs_name	*target_name,
2595f6bba201SDave Chinner 	xfs_inode_t	*target_ip)
2596f6bba201SDave Chinner {
2597f6bba201SDave Chinner 	xfs_trans_t	*tp = NULL;
2598f6bba201SDave Chinner 	xfs_mount_t	*mp = src_dp->i_mount;
2599f6bba201SDave Chinner 	int		new_parent;		/* moving to a new dir */
2600f6bba201SDave Chinner 	int		src_is_directory;	/* src_name is a directory */
2601f6bba201SDave Chinner 	int		error;
2602f6bba201SDave Chinner 	xfs_bmap_free_t free_list;
2603f6bba201SDave Chinner 	xfs_fsblock_t   first_block;
2604f6bba201SDave Chinner 	int		cancel_flags;
2605f6bba201SDave Chinner 	int		committed;
2606f6bba201SDave Chinner 	xfs_inode_t	*inodes[4];
2607f6bba201SDave Chinner 	int		spaceres;
2608f6bba201SDave Chinner 	int		num_inodes;
2609f6bba201SDave Chinner 
2610f6bba201SDave Chinner 	trace_xfs_rename(src_dp, target_dp, src_name, target_name);
2611f6bba201SDave Chinner 
2612f6bba201SDave Chinner 	new_parent = (src_dp != target_dp);
2613f6bba201SDave Chinner 	src_is_directory = S_ISDIR(src_ip->i_d.di_mode);
2614f6bba201SDave Chinner 
2615f6bba201SDave Chinner 	xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip,
2616f6bba201SDave Chinner 				inodes, &num_inodes);
2617f6bba201SDave Chinner 
2618f6bba201SDave Chinner 	xfs_bmap_init(&free_list, &first_block);
2619f6bba201SDave Chinner 	tp = xfs_trans_alloc(mp, XFS_TRANS_RENAME);
2620f6bba201SDave Chinner 	cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2621f6bba201SDave Chinner 	spaceres = XFS_RENAME_SPACE_RES(mp, target_name->len);
26223d3c8b52SJie Liu 	error = xfs_trans_reserve(tp, &M_RES(mp)->tr_rename, spaceres, 0);
2623f6bba201SDave Chinner 	if (error == ENOSPC) {
2624f6bba201SDave Chinner 		spaceres = 0;
26253d3c8b52SJie Liu 		error = xfs_trans_reserve(tp, &M_RES(mp)->tr_rename, 0, 0);
2626f6bba201SDave Chinner 	}
2627f6bba201SDave Chinner 	if (error) {
2628f6bba201SDave Chinner 		xfs_trans_cancel(tp, 0);
2629f6bba201SDave Chinner 		goto std_return;
2630f6bba201SDave Chinner 	}
2631f6bba201SDave Chinner 
2632f6bba201SDave Chinner 	/*
2633f6bba201SDave Chinner 	 * Attach the dquots to the inodes
2634f6bba201SDave Chinner 	 */
2635f6bba201SDave Chinner 	error = xfs_qm_vop_rename_dqattach(inodes);
2636f6bba201SDave Chinner 	if (error) {
2637f6bba201SDave Chinner 		xfs_trans_cancel(tp, cancel_flags);
2638f6bba201SDave Chinner 		goto std_return;
2639f6bba201SDave Chinner 	}
2640f6bba201SDave Chinner 
2641f6bba201SDave Chinner 	/*
2642f6bba201SDave Chinner 	 * Lock all the participating inodes. Depending upon whether
2643f6bba201SDave Chinner 	 * the target_name exists in the target directory, and
2644f6bba201SDave Chinner 	 * whether the target directory is the same as the source
2645f6bba201SDave Chinner 	 * directory, we can lock from 2 to 4 inodes.
2646f6bba201SDave Chinner 	 */
2647f6bba201SDave Chinner 	xfs_lock_inodes(inodes, num_inodes, XFS_ILOCK_EXCL);
2648f6bba201SDave Chinner 
2649f6bba201SDave Chinner 	/*
2650f6bba201SDave Chinner 	 * Join all the inodes to the transaction. From this point on,
2651f6bba201SDave Chinner 	 * we can rely on either trans_commit or trans_cancel to unlock
2652f6bba201SDave Chinner 	 * them.
2653f6bba201SDave Chinner 	 */
2654f6bba201SDave Chinner 	xfs_trans_ijoin(tp, src_dp, XFS_ILOCK_EXCL);
2655f6bba201SDave Chinner 	if (new_parent)
2656f6bba201SDave Chinner 		xfs_trans_ijoin(tp, target_dp, XFS_ILOCK_EXCL);
2657f6bba201SDave Chinner 	xfs_trans_ijoin(tp, src_ip, XFS_ILOCK_EXCL);
2658f6bba201SDave Chinner 	if (target_ip)
2659f6bba201SDave Chinner 		xfs_trans_ijoin(tp, target_ip, XFS_ILOCK_EXCL);
2660f6bba201SDave Chinner 
2661f6bba201SDave Chinner 	/*
2662f6bba201SDave Chinner 	 * If we are using project inheritance, we only allow renames
2663f6bba201SDave Chinner 	 * into our tree when the project IDs are the same; else the
2664f6bba201SDave Chinner 	 * tree quota mechanism would be circumvented.
2665f6bba201SDave Chinner 	 */
2666f6bba201SDave Chinner 	if (unlikely((target_dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2667f6bba201SDave Chinner 		     (xfs_get_projid(target_dp) != xfs_get_projid(src_ip)))) {
2668f6bba201SDave Chinner 		error = XFS_ERROR(EXDEV);
2669f6bba201SDave Chinner 		goto error_return;
2670f6bba201SDave Chinner 	}
2671f6bba201SDave Chinner 
2672f6bba201SDave Chinner 	/*
2673f6bba201SDave Chinner 	 * Set up the target.
2674f6bba201SDave Chinner 	 */
2675f6bba201SDave Chinner 	if (target_ip == NULL) {
2676f6bba201SDave Chinner 		/*
2677f6bba201SDave Chinner 		 * If there's no space reservation, check the entry will
2678f6bba201SDave Chinner 		 * fit before actually inserting it.
2679f6bba201SDave Chinner 		 */
2680f6bba201SDave Chinner 		error = xfs_dir_canenter(tp, target_dp, target_name, spaceres);
2681f6bba201SDave Chinner 		if (error)
2682f6bba201SDave Chinner 			goto error_return;
2683f6bba201SDave Chinner 		/*
2684f6bba201SDave Chinner 		 * If target does not exist and the rename crosses
2685f6bba201SDave Chinner 		 * directories, adjust the target directory link count
2686f6bba201SDave Chinner 		 * to account for the ".." reference from the new entry.
2687f6bba201SDave Chinner 		 */
2688f6bba201SDave Chinner 		error = xfs_dir_createname(tp, target_dp, target_name,
2689f6bba201SDave Chinner 						src_ip->i_ino, &first_block,
2690f6bba201SDave Chinner 						&free_list, spaceres);
2691f6bba201SDave Chinner 		if (error == ENOSPC)
2692f6bba201SDave Chinner 			goto error_return;
2693f6bba201SDave Chinner 		if (error)
2694f6bba201SDave Chinner 			goto abort_return;
2695f6bba201SDave Chinner 
2696f6bba201SDave Chinner 		xfs_trans_ichgtime(tp, target_dp,
2697f6bba201SDave Chinner 					XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2698f6bba201SDave Chinner 
2699f6bba201SDave Chinner 		if (new_parent && src_is_directory) {
2700f6bba201SDave Chinner 			error = xfs_bumplink(tp, target_dp);
2701f6bba201SDave Chinner 			if (error)
2702f6bba201SDave Chinner 				goto abort_return;
2703f6bba201SDave Chinner 		}
2704f6bba201SDave Chinner 	} else { /* target_ip != NULL */
2705f6bba201SDave Chinner 		/*
2706f6bba201SDave Chinner 		 * If target exists and it's a directory, check that both
2707f6bba201SDave Chinner 		 * target and source are directories and that target can be
2708f6bba201SDave Chinner 		 * destroyed, or that neither is a directory.
2709f6bba201SDave Chinner 		 */
2710f6bba201SDave Chinner 		if (S_ISDIR(target_ip->i_d.di_mode)) {
2711f6bba201SDave Chinner 			/*
2712f6bba201SDave Chinner 			 * Make sure target dir is empty.
2713f6bba201SDave Chinner 			 */
2714f6bba201SDave Chinner 			if (!(xfs_dir_isempty(target_ip)) ||
2715f6bba201SDave Chinner 			    (target_ip->i_d.di_nlink > 2)) {
2716f6bba201SDave Chinner 				error = XFS_ERROR(EEXIST);
2717f6bba201SDave Chinner 				goto error_return;
2718f6bba201SDave Chinner 			}
2719f6bba201SDave Chinner 		}
2720f6bba201SDave Chinner 
2721f6bba201SDave Chinner 		/*
2722f6bba201SDave Chinner 		 * Link the source inode under the target name.
2723f6bba201SDave Chinner 		 * If the source inode is a directory and we are moving
2724f6bba201SDave Chinner 		 * it across directories, its ".." entry will be
2725f6bba201SDave Chinner 		 * inconsistent until we replace that down below.
2726f6bba201SDave Chinner 		 *
2727f6bba201SDave Chinner 		 * In case there is already an entry with the same
2728f6bba201SDave Chinner 		 * name at the destination directory, remove it first.
2729f6bba201SDave Chinner 		 */
2730f6bba201SDave Chinner 		error = xfs_dir_replace(tp, target_dp, target_name,
2731f6bba201SDave Chinner 					src_ip->i_ino,
2732f6bba201SDave Chinner 					&first_block, &free_list, spaceres);
2733f6bba201SDave Chinner 		if (error)
2734f6bba201SDave Chinner 			goto abort_return;
2735f6bba201SDave Chinner 
2736f6bba201SDave Chinner 		xfs_trans_ichgtime(tp, target_dp,
2737f6bba201SDave Chinner 					XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2738f6bba201SDave Chinner 
2739f6bba201SDave Chinner 		/*
2740f6bba201SDave Chinner 		 * Decrement the link count on the target since the target
2741f6bba201SDave Chinner 		 * dir no longer points to it.
2742f6bba201SDave Chinner 		 */
2743f6bba201SDave Chinner 		error = xfs_droplink(tp, target_ip);
2744f6bba201SDave Chinner 		if (error)
2745f6bba201SDave Chinner 			goto abort_return;
2746f6bba201SDave Chinner 
2747f6bba201SDave Chinner 		if (src_is_directory) {
2748f6bba201SDave Chinner 			/*
2749f6bba201SDave Chinner 			 * Drop the link from the old "." entry.
2750f6bba201SDave Chinner 			 */
2751f6bba201SDave Chinner 			error = xfs_droplink(tp, target_ip);
2752f6bba201SDave Chinner 			if (error)
2753f6bba201SDave Chinner 				goto abort_return;
2754f6bba201SDave Chinner 		}
2755f6bba201SDave Chinner 	} /* target_ip != NULL */
2756f6bba201SDave Chinner 
2757f6bba201SDave Chinner 	/*
2758f6bba201SDave Chinner 	 * Remove the source.
2759f6bba201SDave Chinner 	 */
2760f6bba201SDave Chinner 	if (new_parent && src_is_directory) {
2761f6bba201SDave Chinner 		/*
2762f6bba201SDave Chinner 		 * Rewrite the ".." entry to point to the new
2763f6bba201SDave Chinner 		 * directory.
2764f6bba201SDave Chinner 		 */
2765f6bba201SDave Chinner 		error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot,
2766f6bba201SDave Chinner 					target_dp->i_ino,
2767f6bba201SDave Chinner 					&first_block, &free_list, spaceres);
2768f6bba201SDave Chinner 		ASSERT(error != EEXIST);
2769f6bba201SDave Chinner 		if (error)
2770f6bba201SDave Chinner 			goto abort_return;
2771f6bba201SDave Chinner 	}
2772f6bba201SDave Chinner 
2773f6bba201SDave Chinner 	/*
2774f6bba201SDave Chinner 	 * We always want to hit the ctime on the source inode.
2775f6bba201SDave Chinner 	 *
2776f6bba201SDave Chinner 	 * This isn't strictly required by the standards since the source
2777f6bba201SDave Chinner 	 * inode isn't really being changed, but old unix file systems did
2778f6bba201SDave Chinner 	 * it and some incremental backup programs won't work without it.
2779f6bba201SDave Chinner 	 */
2780f6bba201SDave Chinner 	xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG);
2781f6bba201SDave Chinner 	xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE);
2782f6bba201SDave Chinner 
2783f6bba201SDave Chinner 	/*
2784f6bba201SDave Chinner 	 * Adjust the link count on src_dp.  This is necessary when
2785f6bba201SDave Chinner 	 * renaming a directory, either within one parent when
2786f6bba201SDave Chinner 	 * the target existed, or across two parent directories.
2787f6bba201SDave Chinner 	 */
2788f6bba201SDave Chinner 	if (src_is_directory && (new_parent || target_ip != NULL)) {
2789f6bba201SDave Chinner 
2790f6bba201SDave Chinner 		/*
2791f6bba201SDave Chinner 		 * Decrement link count on src_directory since the
2792f6bba201SDave Chinner 		 * entry that's moved no longer points to it.
2793f6bba201SDave Chinner 		 */
2794f6bba201SDave Chinner 		error = xfs_droplink(tp, src_dp);
2795f6bba201SDave Chinner 		if (error)
2796f6bba201SDave Chinner 			goto abort_return;
2797f6bba201SDave Chinner 	}
2798f6bba201SDave Chinner 
2799f6bba201SDave Chinner 	error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino,
2800f6bba201SDave Chinner 					&first_block, &free_list, spaceres);
2801f6bba201SDave Chinner 	if (error)
2802f6bba201SDave Chinner 		goto abort_return;
2803f6bba201SDave Chinner 
2804f6bba201SDave Chinner 	xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2805f6bba201SDave Chinner 	xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE);
2806f6bba201SDave Chinner 	if (new_parent)
2807f6bba201SDave Chinner 		xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE);
2808f6bba201SDave Chinner 
2809f6bba201SDave Chinner 	/*
2810f6bba201SDave Chinner 	 * If this is a synchronous mount, make sure that the
2811f6bba201SDave Chinner 	 * rename transaction goes to disk before returning to
2812f6bba201SDave Chinner 	 * the user.
2813f6bba201SDave Chinner 	 */
2814f6bba201SDave Chinner 	if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2815f6bba201SDave Chinner 		xfs_trans_set_sync(tp);
2816f6bba201SDave Chinner 	}
2817f6bba201SDave Chinner 
2818f6bba201SDave Chinner 	error = xfs_bmap_finish(&tp, &free_list, &committed);
2819f6bba201SDave Chinner 	if (error) {
2820f6bba201SDave Chinner 		xfs_bmap_cancel(&free_list);
2821f6bba201SDave Chinner 		xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
2822f6bba201SDave Chinner 				 XFS_TRANS_ABORT));
2823f6bba201SDave Chinner 		goto std_return;
2824f6bba201SDave Chinner 	}
2825f6bba201SDave Chinner 
2826f6bba201SDave Chinner 	/*
2827f6bba201SDave Chinner 	 * trans_commit will unlock src_ip, target_ip & decrement
2828f6bba201SDave Chinner 	 * the vnode references.
2829f6bba201SDave Chinner 	 */
2830f6bba201SDave Chinner 	return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2831f6bba201SDave Chinner 
2832f6bba201SDave Chinner  abort_return:
2833f6bba201SDave Chinner 	cancel_flags |= XFS_TRANS_ABORT;
2834f6bba201SDave Chinner  error_return:
2835f6bba201SDave Chinner 	xfs_bmap_cancel(&free_list);
2836f6bba201SDave Chinner 	xfs_trans_cancel(tp, cancel_flags);
2837f6bba201SDave Chinner  std_return:
2838f6bba201SDave Chinner 	return error;
2839f6bba201SDave Chinner }
2840f6bba201SDave Chinner 
2841bad55843SDavid Chinner STATIC int
2842bad55843SDavid Chinner xfs_iflush_cluster(
2843bad55843SDavid Chinner 	xfs_inode_t	*ip,
2844bad55843SDavid Chinner 	xfs_buf_t	*bp)
2845bad55843SDavid Chinner {
2846bad55843SDavid Chinner 	xfs_mount_t		*mp = ip->i_mount;
28475017e97dSDave Chinner 	struct xfs_perag	*pag;
2848bad55843SDavid Chinner 	unsigned long		first_index, mask;
2849c8f5f12eSDavid Chinner 	unsigned long		inodes_per_cluster;
2850bad55843SDavid Chinner 	int			ilist_size;
2851bad55843SDavid Chinner 	xfs_inode_t		**ilist;
2852bad55843SDavid Chinner 	xfs_inode_t		*iq;
2853bad55843SDavid Chinner 	int			nr_found;
2854bad55843SDavid Chinner 	int			clcount = 0;
2855bad55843SDavid Chinner 	int			bufwasdelwri;
2856bad55843SDavid Chinner 	int			i;
2857bad55843SDavid Chinner 
28585017e97dSDave Chinner 	pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
2859bad55843SDavid Chinner 
2860c8f5f12eSDavid Chinner 	inodes_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog;
2861c8f5f12eSDavid Chinner 	ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
286249383b0eSDavid Chinner 	ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS);
2863bad55843SDavid Chinner 	if (!ilist)
286444b56e0aSDave Chinner 		goto out_put;
2865bad55843SDavid Chinner 
2866bad55843SDavid Chinner 	mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
2867bad55843SDavid Chinner 	first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
28681a3e8f3dSDave Chinner 	rcu_read_lock();
2869bad55843SDavid Chinner 	/* really need a gang lookup range call here */
2870bad55843SDavid Chinner 	nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist,
2871c8f5f12eSDavid Chinner 					first_index, inodes_per_cluster);
2872bad55843SDavid Chinner 	if (nr_found == 0)
2873bad55843SDavid Chinner 		goto out_free;
2874bad55843SDavid Chinner 
2875bad55843SDavid Chinner 	for (i = 0; i < nr_found; i++) {
2876bad55843SDavid Chinner 		iq = ilist[i];
2877bad55843SDavid Chinner 		if (iq == ip)
2878bad55843SDavid Chinner 			continue;
28791a3e8f3dSDave Chinner 
28801a3e8f3dSDave Chinner 		/*
28811a3e8f3dSDave Chinner 		 * because this is an RCU protected lookup, we could find a
28821a3e8f3dSDave Chinner 		 * recently freed or even reallocated inode during the lookup.
28831a3e8f3dSDave Chinner 		 * We need to check under the i_flags_lock for a valid inode
28841a3e8f3dSDave Chinner 		 * here. Skip it if it is not valid or the wrong inode.
28851a3e8f3dSDave Chinner 		 */
28861a3e8f3dSDave Chinner 		spin_lock(&ip->i_flags_lock);
28871a3e8f3dSDave Chinner 		if (!ip->i_ino ||
28881a3e8f3dSDave Chinner 		    (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) {
28891a3e8f3dSDave Chinner 			spin_unlock(&ip->i_flags_lock);
28901a3e8f3dSDave Chinner 			continue;
28911a3e8f3dSDave Chinner 		}
28921a3e8f3dSDave Chinner 		spin_unlock(&ip->i_flags_lock);
28931a3e8f3dSDave Chinner 
2894bad55843SDavid Chinner 		/*
2895bad55843SDavid Chinner 		 * Do an un-protected check to see if the inode is dirty and
2896bad55843SDavid Chinner 		 * is a candidate for flushing.  These checks will be repeated
2897bad55843SDavid Chinner 		 * later after the appropriate locks are acquired.
2898bad55843SDavid Chinner 		 */
289933540408SDavid Chinner 		if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0)
2900bad55843SDavid Chinner 			continue;
2901bad55843SDavid Chinner 
2902bad55843SDavid Chinner 		/*
2903bad55843SDavid Chinner 		 * Try to get locks.  If any are unavailable or it is pinned,
2904bad55843SDavid Chinner 		 * then this inode cannot be flushed and is skipped.
2905bad55843SDavid Chinner 		 */
2906bad55843SDavid Chinner 
2907bad55843SDavid Chinner 		if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED))
2908bad55843SDavid Chinner 			continue;
2909bad55843SDavid Chinner 		if (!xfs_iflock_nowait(iq)) {
2910bad55843SDavid Chinner 			xfs_iunlock(iq, XFS_ILOCK_SHARED);
2911bad55843SDavid Chinner 			continue;
2912bad55843SDavid Chinner 		}
2913bad55843SDavid Chinner 		if (xfs_ipincount(iq)) {
2914bad55843SDavid Chinner 			xfs_ifunlock(iq);
2915bad55843SDavid Chinner 			xfs_iunlock(iq, XFS_ILOCK_SHARED);
2916bad55843SDavid Chinner 			continue;
2917bad55843SDavid Chinner 		}
2918bad55843SDavid Chinner 
2919bad55843SDavid Chinner 		/*
2920bad55843SDavid Chinner 		 * arriving here means that this inode can be flushed.  First
2921bad55843SDavid Chinner 		 * re-check that it's dirty before flushing.
2922bad55843SDavid Chinner 		 */
292333540408SDavid Chinner 		if (!xfs_inode_clean(iq)) {
2924bad55843SDavid Chinner 			int	error;
2925bad55843SDavid Chinner 			error = xfs_iflush_int(iq, bp);
2926bad55843SDavid Chinner 			if (error) {
2927bad55843SDavid Chinner 				xfs_iunlock(iq, XFS_ILOCK_SHARED);
2928bad55843SDavid Chinner 				goto cluster_corrupt_out;
2929bad55843SDavid Chinner 			}
2930bad55843SDavid Chinner 			clcount++;
2931bad55843SDavid Chinner 		} else {
2932bad55843SDavid Chinner 			xfs_ifunlock(iq);
2933bad55843SDavid Chinner 		}
2934bad55843SDavid Chinner 		xfs_iunlock(iq, XFS_ILOCK_SHARED);
2935bad55843SDavid Chinner 	}
2936bad55843SDavid Chinner 
2937bad55843SDavid Chinner 	if (clcount) {
2938bad55843SDavid Chinner 		XFS_STATS_INC(xs_icluster_flushcnt);
2939bad55843SDavid Chinner 		XFS_STATS_ADD(xs_icluster_flushinode, clcount);
2940bad55843SDavid Chinner 	}
2941bad55843SDavid Chinner 
2942bad55843SDavid Chinner out_free:
29431a3e8f3dSDave Chinner 	rcu_read_unlock();
2944f0e2d93cSDenys Vlasenko 	kmem_free(ilist);
294544b56e0aSDave Chinner out_put:
294644b56e0aSDave Chinner 	xfs_perag_put(pag);
2947bad55843SDavid Chinner 	return 0;
2948bad55843SDavid Chinner 
2949bad55843SDavid Chinner 
2950bad55843SDavid Chinner cluster_corrupt_out:
2951bad55843SDavid Chinner 	/*
2952bad55843SDavid Chinner 	 * Corruption detected in the clustering loop.  Invalidate the
2953bad55843SDavid Chinner 	 * inode buffer and shut down the filesystem.
2954bad55843SDavid Chinner 	 */
29551a3e8f3dSDave Chinner 	rcu_read_unlock();
2956bad55843SDavid Chinner 	/*
295743ff2122SChristoph Hellwig 	 * Clean up the buffer.  If it was delwri, just release it --
2958bad55843SDavid Chinner 	 * brelse can handle it with no problems.  If not, shut down the
2959bad55843SDavid Chinner 	 * filesystem before releasing the buffer.
2960bad55843SDavid Chinner 	 */
296143ff2122SChristoph Hellwig 	bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q);
2962bad55843SDavid Chinner 	if (bufwasdelwri)
2963bad55843SDavid Chinner 		xfs_buf_relse(bp);
2964bad55843SDavid Chinner 
2965bad55843SDavid Chinner 	xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
2966bad55843SDavid Chinner 
2967bad55843SDavid Chinner 	if (!bufwasdelwri) {
2968bad55843SDavid Chinner 		/*
2969bad55843SDavid Chinner 		 * Just like incore_relse: if we have b_iodone functions,
2970bad55843SDavid Chinner 		 * mark the buffer as an error and call them.  Otherwise
2971bad55843SDavid Chinner 		 * mark it as stale and brelse.
2972bad55843SDavid Chinner 		 */
2973cb669ca5SChristoph Hellwig 		if (bp->b_iodone) {
2974bad55843SDavid Chinner 			XFS_BUF_UNDONE(bp);
2975c867cb61SChristoph Hellwig 			xfs_buf_stale(bp);
29765a52c2a5SChandra Seetharaman 			xfs_buf_ioerror(bp, EIO);
29771a1a3e97SChristoph Hellwig 			xfs_buf_ioend(bp, 0);
2978bad55843SDavid Chinner 		} else {
2979c867cb61SChristoph Hellwig 			xfs_buf_stale(bp);
2980bad55843SDavid Chinner 			xfs_buf_relse(bp);
2981bad55843SDavid Chinner 		}
2982bad55843SDavid Chinner 	}
2983bad55843SDavid Chinner 
2984bad55843SDavid Chinner 	/*
2985bad55843SDavid Chinner 	 * Unlocks the flush lock
2986bad55843SDavid Chinner 	 */
298704913fddSDave Chinner 	xfs_iflush_abort(iq, false);
2988f0e2d93cSDenys Vlasenko 	kmem_free(ilist);
298944b56e0aSDave Chinner 	xfs_perag_put(pag);
2990bad55843SDavid Chinner 	return XFS_ERROR(EFSCORRUPTED);
2991bad55843SDavid Chinner }
2992bad55843SDavid Chinner 
29931da177e4SLinus Torvalds /*
29944c46819aSChristoph Hellwig  * Flush dirty inode metadata into the backing buffer.
29954c46819aSChristoph Hellwig  *
29964c46819aSChristoph Hellwig  * The caller must have the inode lock and the inode flush lock held.  The
29974c46819aSChristoph Hellwig  * inode lock will still be held upon return to the caller, and the inode
29984c46819aSChristoph Hellwig  * flush lock will be released after the inode has reached the disk.
29994c46819aSChristoph Hellwig  *
30004c46819aSChristoph Hellwig  * The caller must write out the buffer returned in *bpp and release it.
30011da177e4SLinus Torvalds  */
30021da177e4SLinus Torvalds int
30031da177e4SLinus Torvalds xfs_iflush(
30044c46819aSChristoph Hellwig 	struct xfs_inode	*ip,
30054c46819aSChristoph Hellwig 	struct xfs_buf		**bpp)
30061da177e4SLinus Torvalds {
30074c46819aSChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
30084c46819aSChristoph Hellwig 	struct xfs_buf		*bp;
30094c46819aSChristoph Hellwig 	struct xfs_dinode	*dip;
30101da177e4SLinus Torvalds 	int			error;
30111da177e4SLinus Torvalds 
30121da177e4SLinus Torvalds 	XFS_STATS_INC(xs_iflush_count);
30131da177e4SLinus Torvalds 
3014579aa9caSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
3015474fce06SChristoph Hellwig 	ASSERT(xfs_isiflocked(ip));
30161da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
30178096b1ebSChristoph Hellwig 	       ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
30181da177e4SLinus Torvalds 
30194c46819aSChristoph Hellwig 	*bpp = NULL;
30201da177e4SLinus Torvalds 
30211da177e4SLinus Torvalds 	xfs_iunpin_wait(ip);
30221da177e4SLinus Torvalds 
30231da177e4SLinus Torvalds 	/*
30244b6a4688SDave Chinner 	 * For stale inodes we cannot rely on the backing buffer remaining
30254b6a4688SDave Chinner 	 * stale in cache for the remaining life of the stale inode and so
3026475ee413SChristoph Hellwig 	 * xfs_imap_to_bp() below may give us a buffer that no longer contains
30274b6a4688SDave Chinner 	 * inodes below. We have to check this after ensuring the inode is
30284b6a4688SDave Chinner 	 * unpinned so that it is safe to reclaim the stale inode after the
30294b6a4688SDave Chinner 	 * flush call.
30304b6a4688SDave Chinner 	 */
30314b6a4688SDave Chinner 	if (xfs_iflags_test(ip, XFS_ISTALE)) {
30324b6a4688SDave Chinner 		xfs_ifunlock(ip);
30334b6a4688SDave Chinner 		return 0;
30344b6a4688SDave Chinner 	}
30354b6a4688SDave Chinner 
30364b6a4688SDave Chinner 	/*
30371da177e4SLinus Torvalds 	 * This may have been unpinned because the filesystem is shutting
30381da177e4SLinus Torvalds 	 * down forcibly. If that's the case we must not write this inode
303932ce90a4SChristoph Hellwig 	 * to disk, because the log record didn't make it to disk.
304032ce90a4SChristoph Hellwig 	 *
304132ce90a4SChristoph Hellwig 	 * We also have to remove the log item from the AIL in this case,
304232ce90a4SChristoph Hellwig 	 * as we wait for an empty AIL as part of the unmount process.
30431da177e4SLinus Torvalds 	 */
30441da177e4SLinus Torvalds 	if (XFS_FORCED_SHUTDOWN(mp)) {
304532ce90a4SChristoph Hellwig 		error = XFS_ERROR(EIO);
304632ce90a4SChristoph Hellwig 		goto abort_out;
30471da177e4SLinus Torvalds 	}
30481da177e4SLinus Torvalds 
30491da177e4SLinus Torvalds 	/*
3050a3f74ffbSDavid Chinner 	 * Get the buffer containing the on-disk inode.
3051a3f74ffbSDavid Chinner 	 */
3052475ee413SChristoph Hellwig 	error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK,
3053475ee413SChristoph Hellwig 			       0);
3054a3f74ffbSDavid Chinner 	if (error || !bp) {
3055a3f74ffbSDavid Chinner 		xfs_ifunlock(ip);
3056a3f74ffbSDavid Chinner 		return error;
3057a3f74ffbSDavid Chinner 	}
3058a3f74ffbSDavid Chinner 
3059a3f74ffbSDavid Chinner 	/*
30601da177e4SLinus Torvalds 	 * First flush out the inode that xfs_iflush was called with.
30611da177e4SLinus Torvalds 	 */
30621da177e4SLinus Torvalds 	error = xfs_iflush_int(ip, bp);
3063bad55843SDavid Chinner 	if (error)
30641da177e4SLinus Torvalds 		goto corrupt_out;
30651da177e4SLinus Torvalds 
30661da177e4SLinus Torvalds 	/*
3067a3f74ffbSDavid Chinner 	 * If the buffer is pinned then push on the log now so we won't
3068a3f74ffbSDavid Chinner 	 * get stuck waiting in the write for too long.
3069a3f74ffbSDavid Chinner 	 */
3070811e64c7SChandra Seetharaman 	if (xfs_buf_ispinned(bp))
3071a14a348bSChristoph Hellwig 		xfs_log_force(mp, 0);
3072a3f74ffbSDavid Chinner 
3073a3f74ffbSDavid Chinner 	/*
30741da177e4SLinus Torvalds 	 * inode clustering:
30751da177e4SLinus Torvalds 	 * see if other inodes can be gathered into this write
30761da177e4SLinus Torvalds 	 */
3077bad55843SDavid Chinner 	error = xfs_iflush_cluster(ip, bp);
3078bad55843SDavid Chinner 	if (error)
30791da177e4SLinus Torvalds 		goto cluster_corrupt_out;
30801da177e4SLinus Torvalds 
30814c46819aSChristoph Hellwig 	*bpp = bp;
30824c46819aSChristoph Hellwig 	return 0;
30831da177e4SLinus Torvalds 
30841da177e4SLinus Torvalds corrupt_out:
30851da177e4SLinus Torvalds 	xfs_buf_relse(bp);
30867d04a335SNathan Scott 	xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
30871da177e4SLinus Torvalds cluster_corrupt_out:
308832ce90a4SChristoph Hellwig 	error = XFS_ERROR(EFSCORRUPTED);
308932ce90a4SChristoph Hellwig abort_out:
30901da177e4SLinus Torvalds 	/*
30911da177e4SLinus Torvalds 	 * Unlocks the flush lock
30921da177e4SLinus Torvalds 	 */
309304913fddSDave Chinner 	xfs_iflush_abort(ip, false);
309432ce90a4SChristoph Hellwig 	return error;
30951da177e4SLinus Torvalds }
30961da177e4SLinus Torvalds 
30971da177e4SLinus Torvalds STATIC int
30981da177e4SLinus Torvalds xfs_iflush_int(
309993848a99SChristoph Hellwig 	struct xfs_inode	*ip,
310093848a99SChristoph Hellwig 	struct xfs_buf		*bp)
31011da177e4SLinus Torvalds {
310293848a99SChristoph Hellwig 	struct xfs_inode_log_item *iip = ip->i_itemp;
310393848a99SChristoph Hellwig 	struct xfs_dinode	*dip;
310493848a99SChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
31051da177e4SLinus Torvalds 
3106579aa9caSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
3107474fce06SChristoph Hellwig 	ASSERT(xfs_isiflocked(ip));
31081da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
31098096b1ebSChristoph Hellwig 	       ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
311093848a99SChristoph Hellwig 	ASSERT(iip != NULL && iip->ili_fields != 0);
31111da177e4SLinus Torvalds 
31121da177e4SLinus Torvalds 	/* set *dip = inode's place in the buffer */
311392bfc6e7SChristoph Hellwig 	dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
31141da177e4SLinus Torvalds 
311569ef921bSChristoph Hellwig 	if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
31161da177e4SLinus Torvalds 			       mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
31176a19d939SDave Chinner 		xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
31186a19d939SDave Chinner 			"%s: Bad inode %Lu magic number 0x%x, ptr 0x%p",
31196a19d939SDave Chinner 			__func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
31201da177e4SLinus Torvalds 		goto corrupt_out;
31211da177e4SLinus Torvalds 	}
31221da177e4SLinus Torvalds 	if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
31231da177e4SLinus Torvalds 				mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
31246a19d939SDave Chinner 		xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
31256a19d939SDave Chinner 			"%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
31266a19d939SDave Chinner 			__func__, ip->i_ino, ip, ip->i_d.di_magic);
31271da177e4SLinus Torvalds 		goto corrupt_out;
31281da177e4SLinus Torvalds 	}
3129abbede1bSAl Viro 	if (S_ISREG(ip->i_d.di_mode)) {
31301da177e4SLinus Torvalds 		if (XFS_TEST_ERROR(
31311da177e4SLinus Torvalds 		    (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
31321da177e4SLinus Torvalds 		    (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
31331da177e4SLinus Torvalds 		    mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
31346a19d939SDave Chinner 			xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
31356a19d939SDave Chinner 				"%s: Bad regular inode %Lu, ptr 0x%p",
31366a19d939SDave Chinner 				__func__, ip->i_ino, ip);
31371da177e4SLinus Torvalds 			goto corrupt_out;
31381da177e4SLinus Torvalds 		}
3139abbede1bSAl Viro 	} else if (S_ISDIR(ip->i_d.di_mode)) {
31401da177e4SLinus Torvalds 		if (XFS_TEST_ERROR(
31411da177e4SLinus Torvalds 		    (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
31421da177e4SLinus Torvalds 		    (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
31431da177e4SLinus Torvalds 		    (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
31441da177e4SLinus Torvalds 		    mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
31456a19d939SDave Chinner 			xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
31466a19d939SDave Chinner 				"%s: Bad directory inode %Lu, ptr 0x%p",
31476a19d939SDave Chinner 				__func__, ip->i_ino, ip);
31481da177e4SLinus Torvalds 			goto corrupt_out;
31491da177e4SLinus Torvalds 		}
31501da177e4SLinus Torvalds 	}
31511da177e4SLinus Torvalds 	if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
31521da177e4SLinus Torvalds 				ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
31531da177e4SLinus Torvalds 				XFS_RANDOM_IFLUSH_5)) {
31546a19d939SDave Chinner 		xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
31556a19d939SDave Chinner 			"%s: detected corrupt incore inode %Lu, "
31566a19d939SDave Chinner 			"total extents = %d, nblocks = %Ld, ptr 0x%p",
31576a19d939SDave Chinner 			__func__, ip->i_ino,
31581da177e4SLinus Torvalds 			ip->i_d.di_nextents + ip->i_d.di_anextents,
31596a19d939SDave Chinner 			ip->i_d.di_nblocks, ip);
31601da177e4SLinus Torvalds 		goto corrupt_out;
31611da177e4SLinus Torvalds 	}
31621da177e4SLinus Torvalds 	if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
31631da177e4SLinus Torvalds 				mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
31646a19d939SDave Chinner 		xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
31656a19d939SDave Chinner 			"%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
31666a19d939SDave Chinner 			__func__, ip->i_ino, ip->i_d.di_forkoff, ip);
31671da177e4SLinus Torvalds 		goto corrupt_out;
31681da177e4SLinus Torvalds 	}
3169e60896d8SDave Chinner 
31701da177e4SLinus Torvalds 	/*
3171e60896d8SDave Chinner 	 * Inode item log recovery for v1/v2 inodes are dependent on the
3172e60896d8SDave Chinner 	 * di_flushiter count for correct sequencing. We bump the flush
3173e60896d8SDave Chinner 	 * iteration count so we can detect flushes which postdate a log record
3174e60896d8SDave Chinner 	 * during recovery. This is redundant as we now log every change and
3175e60896d8SDave Chinner 	 * hence this can't happen but we need to still do it to ensure
3176e60896d8SDave Chinner 	 * backwards compatibility with old kernels that predate logging all
3177e60896d8SDave Chinner 	 * inode changes.
31781da177e4SLinus Torvalds 	 */
3179e60896d8SDave Chinner 	if (ip->i_d.di_version < 3)
31801da177e4SLinus Torvalds 		ip->i_d.di_flushiter++;
31811da177e4SLinus Torvalds 
31821da177e4SLinus Torvalds 	/*
31831da177e4SLinus Torvalds 	 * Copy the dirty parts of the inode into the on-disk
31841da177e4SLinus Torvalds 	 * inode.  We always copy out the core of the inode,
31851da177e4SLinus Torvalds 	 * because if the inode is dirty at all the core must
31861da177e4SLinus Torvalds 	 * be.
31871da177e4SLinus Torvalds 	 */
318881591fe2SChristoph Hellwig 	xfs_dinode_to_disk(dip, &ip->i_d);
31891da177e4SLinus Torvalds 
31901da177e4SLinus Torvalds 	/* Wrap, we never let the log put out DI_MAX_FLUSH */
31911da177e4SLinus Torvalds 	if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
31921da177e4SLinus Torvalds 		ip->i_d.di_flushiter = 0;
31931da177e4SLinus Torvalds 
31941da177e4SLinus Torvalds 	/*
31951da177e4SLinus Torvalds 	 * If this is really an old format inode and the superblock version
31961da177e4SLinus Torvalds 	 * has not been updated to support only new format inodes, then
31971da177e4SLinus Torvalds 	 * convert back to the old inode format.  If the superblock version
31981da177e4SLinus Torvalds 	 * has been updated, then make the conversion permanent.
31991da177e4SLinus Torvalds 	 */
320051ce16d5SChristoph Hellwig 	ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb));
320151ce16d5SChristoph Hellwig 	if (ip->i_d.di_version == 1) {
320262118709SEric Sandeen 		if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
32031da177e4SLinus Torvalds 			/*
32041da177e4SLinus Torvalds 			 * Convert it back.
32051da177e4SLinus Torvalds 			 */
32061da177e4SLinus Torvalds 			ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
320781591fe2SChristoph Hellwig 			dip->di_onlink = cpu_to_be16(ip->i_d.di_nlink);
32081da177e4SLinus Torvalds 		} else {
32091da177e4SLinus Torvalds 			/*
32101da177e4SLinus Torvalds 			 * The superblock version has already been bumped,
32111da177e4SLinus Torvalds 			 * so just make the conversion to the new inode
32121da177e4SLinus Torvalds 			 * format permanent.
32131da177e4SLinus Torvalds 			 */
321451ce16d5SChristoph Hellwig 			ip->i_d.di_version = 2;
321551ce16d5SChristoph Hellwig 			dip->di_version = 2;
32161da177e4SLinus Torvalds 			ip->i_d.di_onlink = 0;
321781591fe2SChristoph Hellwig 			dip->di_onlink = 0;
32181da177e4SLinus Torvalds 			memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
321981591fe2SChristoph Hellwig 			memset(&(dip->di_pad[0]), 0,
322081591fe2SChristoph Hellwig 			      sizeof(dip->di_pad));
32216743099cSArkadiusz Mi?kiewicz 			ASSERT(xfs_get_projid(ip) == 0);
32221da177e4SLinus Torvalds 		}
32231da177e4SLinus Torvalds 	}
32241da177e4SLinus Torvalds 
3225e4ac967bSDavid Chinner 	xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp);
3226e4ac967bSDavid Chinner 	if (XFS_IFORK_Q(ip))
3227e4ac967bSDavid Chinner 		xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp);
32281da177e4SLinus Torvalds 	xfs_inobp_check(mp, bp);
32291da177e4SLinus Torvalds 
32301da177e4SLinus Torvalds 	/*
3231f5d8d5c4SChristoph Hellwig 	 * We've recorded everything logged in the inode, so we'd like to clear
3232f5d8d5c4SChristoph Hellwig 	 * the ili_fields bits so we don't log and flush things unnecessarily.
3233f5d8d5c4SChristoph Hellwig 	 * However, we can't stop logging all this information until the data
3234f5d8d5c4SChristoph Hellwig 	 * we've copied into the disk buffer is written to disk.  If we did we
3235f5d8d5c4SChristoph Hellwig 	 * might overwrite the copy of the inode in the log with all the data
3236f5d8d5c4SChristoph Hellwig 	 * after re-logging only part of it, and in the face of a crash we
3237f5d8d5c4SChristoph Hellwig 	 * wouldn't have all the data we need to recover.
32381da177e4SLinus Torvalds 	 *
3239f5d8d5c4SChristoph Hellwig 	 * What we do is move the bits to the ili_last_fields field.  When
3240f5d8d5c4SChristoph Hellwig 	 * logging the inode, these bits are moved back to the ili_fields field.
3241f5d8d5c4SChristoph Hellwig 	 * In the xfs_iflush_done() routine we clear ili_last_fields, since we
3242f5d8d5c4SChristoph Hellwig 	 * know that the information those bits represent is permanently on
3243f5d8d5c4SChristoph Hellwig 	 * disk.  As long as the flush completes before the inode is logged
3244f5d8d5c4SChristoph Hellwig 	 * again, then both ili_fields and ili_last_fields will be cleared.
32451da177e4SLinus Torvalds 	 *
3246f5d8d5c4SChristoph Hellwig 	 * We can play with the ili_fields bits here, because the inode lock
3247f5d8d5c4SChristoph Hellwig 	 * must be held exclusively in order to set bits there and the flush
3248f5d8d5c4SChristoph Hellwig 	 * lock protects the ili_last_fields bits.  Set ili_logged so the flush
3249f5d8d5c4SChristoph Hellwig 	 * done routine can tell whether or not to look in the AIL.  Also, store
3250f5d8d5c4SChristoph Hellwig 	 * the current LSN of the inode so that we can tell whether the item has
3251f5d8d5c4SChristoph Hellwig 	 * moved in the AIL from xfs_iflush_done().  In order to read the lsn we
3252f5d8d5c4SChristoph Hellwig 	 * need the AIL lock, because it is a 64 bit value that cannot be read
3253f5d8d5c4SChristoph Hellwig 	 * atomically.
32541da177e4SLinus Torvalds 	 */
3255f5d8d5c4SChristoph Hellwig 	iip->ili_last_fields = iip->ili_fields;
3256f5d8d5c4SChristoph Hellwig 	iip->ili_fields = 0;
32571da177e4SLinus Torvalds 	iip->ili_logged = 1;
32581da177e4SLinus Torvalds 
32597b2e2a31SDavid Chinner 	xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
32607b2e2a31SDavid Chinner 				&iip->ili_item.li_lsn);
32611da177e4SLinus Torvalds 
32621da177e4SLinus Torvalds 	/*
32631da177e4SLinus Torvalds 	 * Attach the function xfs_iflush_done to the inode's
32641da177e4SLinus Torvalds 	 * buffer.  This will remove the inode from the AIL
32651da177e4SLinus Torvalds 	 * and unlock the inode's flush lock when the inode is
32661da177e4SLinus Torvalds 	 * completely written to disk.
32671da177e4SLinus Torvalds 	 */
3268ca30b2a7SChristoph Hellwig 	xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
32691da177e4SLinus Torvalds 
327093848a99SChristoph Hellwig 	/* update the lsn in the on disk inode if required */
327193848a99SChristoph Hellwig 	if (ip->i_d.di_version == 3)
327293848a99SChristoph Hellwig 		dip->di_lsn = cpu_to_be64(iip->ili_item.li_lsn);
327393848a99SChristoph Hellwig 
327493848a99SChristoph Hellwig 	/* generate the checksum. */
327593848a99SChristoph Hellwig 	xfs_dinode_calc_crc(mp, dip);
327693848a99SChristoph Hellwig 
3277adadbeefSChristoph Hellwig 	ASSERT(bp->b_fspriv != NULL);
3278cb669ca5SChristoph Hellwig 	ASSERT(bp->b_iodone != NULL);
32791da177e4SLinus Torvalds 	return 0;
32801da177e4SLinus Torvalds 
32811da177e4SLinus Torvalds corrupt_out:
32821da177e4SLinus Torvalds 	return XFS_ERROR(EFSCORRUPTED);
32831da177e4SLinus Torvalds }
3284