xref: /openbmc/linux/fs/xfs/xfs_inode.c (revision f7ca35227253dc8244fd908140b06010e67a31e5)
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"
2270a9883cSDave Chinner #include "xfs_shared.h"
23239880efSDave Chinner #include "xfs_format.h"
24239880efSDave Chinner #include "xfs_log_format.h"
25239880efSDave Chinner #include "xfs_trans_resv.h"
261da177e4SLinus Torvalds #include "xfs_sb.h"
271da177e4SLinus Torvalds #include "xfs_mount.h"
283ab78df2SDarrick J. Wong #include "xfs_defer.h"
29a4fbe6abSDave Chinner #include "xfs_inode.h"
3057062787SDave Chinner #include "xfs_da_format.h"
31c24b5dfaSDave Chinner #include "xfs_da_btree.h"
32c24b5dfaSDave Chinner #include "xfs_dir2.h"
33a844f451SNathan Scott #include "xfs_attr_sf.h"
34c24b5dfaSDave Chinner #include "xfs_attr.h"
35239880efSDave Chinner #include "xfs_trans_space.h"
36239880efSDave Chinner #include "xfs_trans.h"
371da177e4SLinus Torvalds #include "xfs_buf_item.h"
38a844f451SNathan Scott #include "xfs_inode_item.h"
39a844f451SNathan Scott #include "xfs_ialloc.h"
40a844f451SNathan Scott #include "xfs_bmap.h"
4168988114SDave Chinner #include "xfs_bmap_util.h"
421da177e4SLinus Torvalds #include "xfs_error.h"
431da177e4SLinus Torvalds #include "xfs_quota.h"
442a82b8beSDavid Chinner #include "xfs_filestream.h"
4593848a99SChristoph Hellwig #include "xfs_cksum.h"
460b1b213fSChristoph Hellwig #include "xfs_trace.h"
4733479e05SDave Chinner #include "xfs_icache.h"
48c24b5dfaSDave Chinner #include "xfs_symlink.h"
49239880efSDave Chinner #include "xfs_trans_priv.h"
50239880efSDave Chinner #include "xfs_log.h"
51a4fbe6abSDave Chinner #include "xfs_bmap_btree.h"
52aa8968f2SDarrick J. Wong #include "xfs_reflink.h"
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds kmem_zone_t *xfs_inode_zone;
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds /*
578f04c47aSChristoph Hellwig  * Used in xfs_itruncate_extents().  This is the maximum number of extents
581da177e4SLinus Torvalds  * freed from a file in a single transaction.
591da177e4SLinus Torvalds  */
601da177e4SLinus Torvalds #define	XFS_ITRUNC_MAX_EXTENTS	2
611da177e4SLinus Torvalds 
6254d7b5c1SDave Chinner STATIC int xfs_iflush_int(struct xfs_inode *, struct xfs_buf *);
6354d7b5c1SDave Chinner STATIC int xfs_iunlink(struct xfs_trans *, struct xfs_inode *);
6454d7b5c1SDave Chinner STATIC int xfs_iunlink_remove(struct xfs_trans *, struct xfs_inode *);
65ab297431SZhi Yong Wu 
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 /*
81*f7ca3522SDarrick J. Wong  * Helper function to extract CoW extent size hint from inode.
82*f7ca3522SDarrick J. Wong  * Between the extent size hint and the CoW extent size hint, we
83*f7ca3522SDarrick J. Wong  * return the greater of the two.
84*f7ca3522SDarrick J. Wong  */
85*f7ca3522SDarrick J. Wong xfs_extlen_t
86*f7ca3522SDarrick J. Wong xfs_get_cowextsz_hint(
87*f7ca3522SDarrick J. Wong 	struct xfs_inode	*ip)
88*f7ca3522SDarrick J. Wong {
89*f7ca3522SDarrick J. Wong 	xfs_extlen_t		a, b;
90*f7ca3522SDarrick J. Wong 
91*f7ca3522SDarrick J. Wong 	a = 0;
92*f7ca3522SDarrick J. Wong 	if (ip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE)
93*f7ca3522SDarrick J. Wong 		a = ip->i_d.di_cowextsize;
94*f7ca3522SDarrick J. Wong 	b = xfs_get_extsz_hint(ip);
95*f7ca3522SDarrick J. Wong 
96*f7ca3522SDarrick J. Wong 	if (a > b)
97*f7ca3522SDarrick J. Wong 		return a;
98*f7ca3522SDarrick J. Wong 	return b;
99*f7ca3522SDarrick J. Wong }
100*f7ca3522SDarrick J. Wong 
101*f7ca3522SDarrick J. Wong /*
102efa70be1SChristoph Hellwig  * These two are wrapper routines around the xfs_ilock() routine used to
103efa70be1SChristoph Hellwig  * centralize some grungy code.  They are used in places that wish to lock the
104efa70be1SChristoph Hellwig  * inode solely for reading the extents.  The reason these places can't just
105efa70be1SChristoph Hellwig  * call xfs_ilock(ip, XFS_ILOCK_SHARED) is that the inode lock also guards to
106efa70be1SChristoph Hellwig  * bringing in of the extents from disk for a file in b-tree format.  If the
107efa70be1SChristoph Hellwig  * inode is in b-tree format, then we need to lock the inode exclusively until
108efa70be1SChristoph Hellwig  * the extents are read in.  Locking it exclusively all the time would limit
109efa70be1SChristoph Hellwig  * our parallelism unnecessarily, though.  What we do instead is check to see
110efa70be1SChristoph Hellwig  * if the extents have been read in yet, and only lock the inode exclusively
111efa70be1SChristoph Hellwig  * if they have not.
112fa96acadSDave Chinner  *
113efa70be1SChristoph Hellwig  * The functions return a value which should be given to the corresponding
11401f4f327SChristoph Hellwig  * xfs_iunlock() call.
115fa96acadSDave Chinner  */
116fa96acadSDave Chinner uint
117309ecac8SChristoph Hellwig xfs_ilock_data_map_shared(
118309ecac8SChristoph Hellwig 	struct xfs_inode	*ip)
119fa96acadSDave Chinner {
120309ecac8SChristoph Hellwig 	uint			lock_mode = XFS_ILOCK_SHARED;
121fa96acadSDave Chinner 
122309ecac8SChristoph Hellwig 	if (ip->i_d.di_format == XFS_DINODE_FMT_BTREE &&
123309ecac8SChristoph Hellwig 	    (ip->i_df.if_flags & XFS_IFEXTENTS) == 0)
124fa96acadSDave Chinner 		lock_mode = XFS_ILOCK_EXCL;
125fa96acadSDave Chinner 	xfs_ilock(ip, lock_mode);
126fa96acadSDave Chinner 	return lock_mode;
127fa96acadSDave Chinner }
128fa96acadSDave Chinner 
129efa70be1SChristoph Hellwig uint
130efa70be1SChristoph Hellwig xfs_ilock_attr_map_shared(
131efa70be1SChristoph Hellwig 	struct xfs_inode	*ip)
132fa96acadSDave Chinner {
133efa70be1SChristoph Hellwig 	uint			lock_mode = XFS_ILOCK_SHARED;
134efa70be1SChristoph Hellwig 
135efa70be1SChristoph Hellwig 	if (ip->i_d.di_aformat == XFS_DINODE_FMT_BTREE &&
136efa70be1SChristoph Hellwig 	    (ip->i_afp->if_flags & XFS_IFEXTENTS) == 0)
137efa70be1SChristoph Hellwig 		lock_mode = XFS_ILOCK_EXCL;
138efa70be1SChristoph Hellwig 	xfs_ilock(ip, lock_mode);
139efa70be1SChristoph Hellwig 	return lock_mode;
140fa96acadSDave Chinner }
141fa96acadSDave Chinner 
142fa96acadSDave Chinner /*
143653c60b6SDave Chinner  * The xfs inode contains 3 multi-reader locks: the i_iolock the i_mmap_lock and
144653c60b6SDave Chinner  * the i_lock.  This routine allows various combinations of the locks to be
145653c60b6SDave Chinner  * obtained.
146fa96acadSDave Chinner  *
147653c60b6SDave Chinner  * The 3 locks should always be ordered so that the IO lock is obtained first,
148653c60b6SDave Chinner  * the mmap lock second and the ilock last in order to prevent deadlock.
149fa96acadSDave Chinner  *
150653c60b6SDave Chinner  * Basic locking order:
151653c60b6SDave Chinner  *
152653c60b6SDave Chinner  * i_iolock -> i_mmap_lock -> page_lock -> i_ilock
153653c60b6SDave Chinner  *
154653c60b6SDave Chinner  * mmap_sem locking order:
155653c60b6SDave Chinner  *
156653c60b6SDave Chinner  * i_iolock -> page lock -> mmap_sem
157653c60b6SDave Chinner  * mmap_sem -> i_mmap_lock -> page_lock
158653c60b6SDave Chinner  *
159653c60b6SDave Chinner  * The difference in mmap_sem locking order mean that we cannot hold the
160653c60b6SDave Chinner  * i_mmap_lock over syscall based read(2)/write(2) based IO. These IO paths can
161653c60b6SDave Chinner  * fault in pages during copy in/out (for buffered IO) or require the mmap_sem
162653c60b6SDave Chinner  * in get_user_pages() to map the user pages into the kernel address space for
163653c60b6SDave Chinner  * direct IO. Similarly the i_iolock cannot be taken inside a page fault because
164653c60b6SDave Chinner  * page faults already hold the mmap_sem.
165653c60b6SDave Chinner  *
166653c60b6SDave Chinner  * Hence to serialise fully against both syscall and mmap based IO, we need to
167653c60b6SDave Chinner  * take both the i_iolock and the i_mmap_lock. These locks should *only* be both
168653c60b6SDave Chinner  * taken in places where we need to invalidate the page cache in a race
169653c60b6SDave Chinner  * free manner (e.g. truncate, hole punch and other extent manipulation
170653c60b6SDave Chinner  * functions).
171fa96acadSDave Chinner  */
172fa96acadSDave Chinner void
173fa96acadSDave Chinner xfs_ilock(
174fa96acadSDave Chinner 	xfs_inode_t		*ip,
175fa96acadSDave Chinner 	uint			lock_flags)
176fa96acadSDave Chinner {
177fa96acadSDave Chinner 	trace_xfs_ilock(ip, lock_flags, _RET_IP_);
178fa96acadSDave Chinner 
179fa96acadSDave Chinner 	/*
180fa96acadSDave Chinner 	 * You can't set both SHARED and EXCL for the same lock,
181fa96acadSDave Chinner 	 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
182fa96acadSDave Chinner 	 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
183fa96acadSDave Chinner 	 */
184fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
185fa96acadSDave Chinner 	       (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
186653c60b6SDave Chinner 	ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
187653c60b6SDave Chinner 	       (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
188fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
189fa96acadSDave Chinner 	       (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
1900952c818SDave Chinner 	ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
191fa96acadSDave Chinner 
192fa96acadSDave Chinner 	if (lock_flags & XFS_IOLOCK_EXCL)
193fa96acadSDave Chinner 		mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
194fa96acadSDave Chinner 	else if (lock_flags & XFS_IOLOCK_SHARED)
195fa96acadSDave Chinner 		mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
196fa96acadSDave Chinner 
197653c60b6SDave Chinner 	if (lock_flags & XFS_MMAPLOCK_EXCL)
198653c60b6SDave Chinner 		mrupdate_nested(&ip->i_mmaplock, XFS_MMAPLOCK_DEP(lock_flags));
199653c60b6SDave Chinner 	else if (lock_flags & XFS_MMAPLOCK_SHARED)
200653c60b6SDave Chinner 		mraccess_nested(&ip->i_mmaplock, XFS_MMAPLOCK_DEP(lock_flags));
201653c60b6SDave Chinner 
202fa96acadSDave Chinner 	if (lock_flags & XFS_ILOCK_EXCL)
203fa96acadSDave Chinner 		mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
204fa96acadSDave Chinner 	else if (lock_flags & XFS_ILOCK_SHARED)
205fa96acadSDave Chinner 		mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
206fa96acadSDave Chinner }
207fa96acadSDave Chinner 
208fa96acadSDave Chinner /*
209fa96acadSDave Chinner  * This is just like xfs_ilock(), except that the caller
210fa96acadSDave Chinner  * is guaranteed not to sleep.  It returns 1 if it gets
211fa96acadSDave Chinner  * the requested locks and 0 otherwise.  If the IO lock is
212fa96acadSDave Chinner  * obtained but the inode lock cannot be, then the IO lock
213fa96acadSDave Chinner  * is dropped before returning.
214fa96acadSDave Chinner  *
215fa96acadSDave Chinner  * ip -- the inode being locked
216fa96acadSDave Chinner  * lock_flags -- this parameter indicates the inode's locks to be
217fa96acadSDave Chinner  *       to be locked.  See the comment for xfs_ilock() for a list
218fa96acadSDave Chinner  *	 of valid values.
219fa96acadSDave Chinner  */
220fa96acadSDave Chinner int
221fa96acadSDave Chinner xfs_ilock_nowait(
222fa96acadSDave Chinner 	xfs_inode_t		*ip,
223fa96acadSDave Chinner 	uint			lock_flags)
224fa96acadSDave Chinner {
225fa96acadSDave Chinner 	trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_);
226fa96acadSDave Chinner 
227fa96acadSDave Chinner 	/*
228fa96acadSDave Chinner 	 * You can't set both SHARED and EXCL for the same lock,
229fa96acadSDave Chinner 	 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
230fa96acadSDave Chinner 	 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
231fa96acadSDave Chinner 	 */
232fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
233fa96acadSDave Chinner 	       (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
234653c60b6SDave Chinner 	ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
235653c60b6SDave Chinner 	       (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
236fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
237fa96acadSDave Chinner 	       (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
2380952c818SDave Chinner 	ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
239fa96acadSDave Chinner 
240fa96acadSDave Chinner 	if (lock_flags & XFS_IOLOCK_EXCL) {
241fa96acadSDave Chinner 		if (!mrtryupdate(&ip->i_iolock))
242fa96acadSDave Chinner 			goto out;
243fa96acadSDave Chinner 	} else if (lock_flags & XFS_IOLOCK_SHARED) {
244fa96acadSDave Chinner 		if (!mrtryaccess(&ip->i_iolock))
245fa96acadSDave Chinner 			goto out;
246fa96acadSDave Chinner 	}
247653c60b6SDave Chinner 
248653c60b6SDave Chinner 	if (lock_flags & XFS_MMAPLOCK_EXCL) {
249653c60b6SDave Chinner 		if (!mrtryupdate(&ip->i_mmaplock))
250653c60b6SDave Chinner 			goto out_undo_iolock;
251653c60b6SDave Chinner 	} else if (lock_flags & XFS_MMAPLOCK_SHARED) {
252653c60b6SDave Chinner 		if (!mrtryaccess(&ip->i_mmaplock))
253653c60b6SDave Chinner 			goto out_undo_iolock;
254653c60b6SDave Chinner 	}
255653c60b6SDave Chinner 
256fa96acadSDave Chinner 	if (lock_flags & XFS_ILOCK_EXCL) {
257fa96acadSDave Chinner 		if (!mrtryupdate(&ip->i_lock))
258653c60b6SDave Chinner 			goto out_undo_mmaplock;
259fa96acadSDave Chinner 	} else if (lock_flags & XFS_ILOCK_SHARED) {
260fa96acadSDave Chinner 		if (!mrtryaccess(&ip->i_lock))
261653c60b6SDave Chinner 			goto out_undo_mmaplock;
262fa96acadSDave Chinner 	}
263fa96acadSDave Chinner 	return 1;
264fa96acadSDave Chinner 
265653c60b6SDave Chinner out_undo_mmaplock:
266653c60b6SDave Chinner 	if (lock_flags & XFS_MMAPLOCK_EXCL)
267653c60b6SDave Chinner 		mrunlock_excl(&ip->i_mmaplock);
268653c60b6SDave Chinner 	else if (lock_flags & XFS_MMAPLOCK_SHARED)
269653c60b6SDave Chinner 		mrunlock_shared(&ip->i_mmaplock);
270fa96acadSDave Chinner out_undo_iolock:
271fa96acadSDave Chinner 	if (lock_flags & XFS_IOLOCK_EXCL)
272fa96acadSDave Chinner 		mrunlock_excl(&ip->i_iolock);
273fa96acadSDave Chinner 	else if (lock_flags & XFS_IOLOCK_SHARED)
274fa96acadSDave Chinner 		mrunlock_shared(&ip->i_iolock);
275fa96acadSDave Chinner out:
276fa96acadSDave Chinner 	return 0;
277fa96acadSDave Chinner }
278fa96acadSDave Chinner 
279fa96acadSDave Chinner /*
280fa96acadSDave Chinner  * xfs_iunlock() is used to drop the inode locks acquired with
281fa96acadSDave Chinner  * xfs_ilock() and xfs_ilock_nowait().  The caller must pass
282fa96acadSDave Chinner  * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
283fa96acadSDave Chinner  * that we know which locks to drop.
284fa96acadSDave Chinner  *
285fa96acadSDave Chinner  * ip -- the inode being unlocked
286fa96acadSDave Chinner  * lock_flags -- this parameter indicates the inode's locks to be
287fa96acadSDave Chinner  *       to be unlocked.  See the comment for xfs_ilock() for a list
288fa96acadSDave Chinner  *	 of valid values for this parameter.
289fa96acadSDave Chinner  *
290fa96acadSDave Chinner  */
291fa96acadSDave Chinner void
292fa96acadSDave Chinner xfs_iunlock(
293fa96acadSDave Chinner 	xfs_inode_t		*ip,
294fa96acadSDave Chinner 	uint			lock_flags)
295fa96acadSDave Chinner {
296fa96acadSDave Chinner 	/*
297fa96acadSDave Chinner 	 * You can't set both SHARED and EXCL for the same lock,
298fa96acadSDave Chinner 	 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
299fa96acadSDave Chinner 	 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
300fa96acadSDave Chinner 	 */
301fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
302fa96acadSDave Chinner 	       (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
303653c60b6SDave Chinner 	ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
304653c60b6SDave Chinner 	       (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
305fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
306fa96acadSDave Chinner 	       (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
3070952c818SDave Chinner 	ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
308fa96acadSDave Chinner 	ASSERT(lock_flags != 0);
309fa96acadSDave Chinner 
310fa96acadSDave Chinner 	if (lock_flags & XFS_IOLOCK_EXCL)
311fa96acadSDave Chinner 		mrunlock_excl(&ip->i_iolock);
312fa96acadSDave Chinner 	else if (lock_flags & XFS_IOLOCK_SHARED)
313fa96acadSDave Chinner 		mrunlock_shared(&ip->i_iolock);
314fa96acadSDave Chinner 
315653c60b6SDave Chinner 	if (lock_flags & XFS_MMAPLOCK_EXCL)
316653c60b6SDave Chinner 		mrunlock_excl(&ip->i_mmaplock);
317653c60b6SDave Chinner 	else if (lock_flags & XFS_MMAPLOCK_SHARED)
318653c60b6SDave Chinner 		mrunlock_shared(&ip->i_mmaplock);
319653c60b6SDave Chinner 
320fa96acadSDave Chinner 	if (lock_flags & XFS_ILOCK_EXCL)
321fa96acadSDave Chinner 		mrunlock_excl(&ip->i_lock);
322fa96acadSDave Chinner 	else if (lock_flags & XFS_ILOCK_SHARED)
323fa96acadSDave Chinner 		mrunlock_shared(&ip->i_lock);
324fa96acadSDave Chinner 
325fa96acadSDave Chinner 	trace_xfs_iunlock(ip, lock_flags, _RET_IP_);
326fa96acadSDave Chinner }
327fa96acadSDave Chinner 
328fa96acadSDave Chinner /*
329fa96acadSDave Chinner  * give up write locks.  the i/o lock cannot be held nested
330fa96acadSDave Chinner  * if it is being demoted.
331fa96acadSDave Chinner  */
332fa96acadSDave Chinner void
333fa96acadSDave Chinner xfs_ilock_demote(
334fa96acadSDave Chinner 	xfs_inode_t		*ip,
335fa96acadSDave Chinner 	uint			lock_flags)
336fa96acadSDave Chinner {
337653c60b6SDave Chinner 	ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL));
338653c60b6SDave Chinner 	ASSERT((lock_flags &
339653c60b6SDave Chinner 		~(XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
340fa96acadSDave Chinner 
341fa96acadSDave Chinner 	if (lock_flags & XFS_ILOCK_EXCL)
342fa96acadSDave Chinner 		mrdemote(&ip->i_lock);
343653c60b6SDave Chinner 	if (lock_flags & XFS_MMAPLOCK_EXCL)
344653c60b6SDave Chinner 		mrdemote(&ip->i_mmaplock);
345fa96acadSDave Chinner 	if (lock_flags & XFS_IOLOCK_EXCL)
346fa96acadSDave Chinner 		mrdemote(&ip->i_iolock);
347fa96acadSDave Chinner 
348fa96acadSDave Chinner 	trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_);
349fa96acadSDave Chinner }
350fa96acadSDave Chinner 
351742ae1e3SDave Chinner #if defined(DEBUG) || defined(XFS_WARN)
352fa96acadSDave Chinner int
353fa96acadSDave Chinner xfs_isilocked(
354fa96acadSDave Chinner 	xfs_inode_t		*ip,
355fa96acadSDave Chinner 	uint			lock_flags)
356fa96acadSDave Chinner {
357fa96acadSDave Chinner 	if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) {
358fa96acadSDave Chinner 		if (!(lock_flags & XFS_ILOCK_SHARED))
359fa96acadSDave Chinner 			return !!ip->i_lock.mr_writer;
360fa96acadSDave Chinner 		return rwsem_is_locked(&ip->i_lock.mr_lock);
361fa96acadSDave Chinner 	}
362fa96acadSDave Chinner 
363653c60b6SDave Chinner 	if (lock_flags & (XFS_MMAPLOCK_EXCL|XFS_MMAPLOCK_SHARED)) {
364653c60b6SDave Chinner 		if (!(lock_flags & XFS_MMAPLOCK_SHARED))
365653c60b6SDave Chinner 			return !!ip->i_mmaplock.mr_writer;
366653c60b6SDave Chinner 		return rwsem_is_locked(&ip->i_mmaplock.mr_lock);
367653c60b6SDave Chinner 	}
368653c60b6SDave Chinner 
369fa96acadSDave Chinner 	if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) {
370fa96acadSDave Chinner 		if (!(lock_flags & XFS_IOLOCK_SHARED))
371fa96acadSDave Chinner 			return !!ip->i_iolock.mr_writer;
372fa96acadSDave Chinner 		return rwsem_is_locked(&ip->i_iolock.mr_lock);
373fa96acadSDave Chinner 	}
374fa96acadSDave Chinner 
375fa96acadSDave Chinner 	ASSERT(0);
376fa96acadSDave Chinner 	return 0;
377fa96acadSDave Chinner }
378fa96acadSDave Chinner #endif
379fa96acadSDave Chinner 
380c24b5dfaSDave Chinner #ifdef DEBUG
381c24b5dfaSDave Chinner int xfs_locked_n;
382c24b5dfaSDave Chinner int xfs_small_retries;
383c24b5dfaSDave Chinner int xfs_middle_retries;
384c24b5dfaSDave Chinner int xfs_lots_retries;
385c24b5dfaSDave Chinner int xfs_lock_delays;
386c24b5dfaSDave Chinner #endif
387c24b5dfaSDave Chinner 
388b6a9947eSDave Chinner /*
389b6a9947eSDave Chinner  * xfs_lockdep_subclass_ok() is only used in an ASSERT, so is only called when
390b6a9947eSDave Chinner  * DEBUG or XFS_WARN is set. And MAX_LOCKDEP_SUBCLASSES is then only defined
391b6a9947eSDave Chinner  * when CONFIG_LOCKDEP is set. Hence the complex define below to avoid build
392b6a9947eSDave Chinner  * errors and warnings.
393b6a9947eSDave Chinner  */
394b6a9947eSDave Chinner #if (defined(DEBUG) || defined(XFS_WARN)) && defined(CONFIG_LOCKDEP)
3953403ccc0SDave Chinner static bool
3963403ccc0SDave Chinner xfs_lockdep_subclass_ok(
3973403ccc0SDave Chinner 	int subclass)
3983403ccc0SDave Chinner {
3993403ccc0SDave Chinner 	return subclass < MAX_LOCKDEP_SUBCLASSES;
4003403ccc0SDave Chinner }
4013403ccc0SDave Chinner #else
4023403ccc0SDave Chinner #define xfs_lockdep_subclass_ok(subclass)	(true)
4033403ccc0SDave Chinner #endif
4043403ccc0SDave Chinner 
405c24b5dfaSDave Chinner /*
406653c60b6SDave Chinner  * Bump the subclass so xfs_lock_inodes() acquires each lock with a different
4070952c818SDave Chinner  * value. This can be called for any type of inode lock combination, including
4080952c818SDave Chinner  * parent locking. Care must be taken to ensure we don't overrun the subclass
4090952c818SDave Chinner  * storage fields in the class mask we build.
410c24b5dfaSDave Chinner  */
411c24b5dfaSDave Chinner static inline int
412c24b5dfaSDave Chinner xfs_lock_inumorder(int lock_mode, int subclass)
413c24b5dfaSDave Chinner {
4140952c818SDave Chinner 	int	class = 0;
4150952c818SDave Chinner 
4160952c818SDave Chinner 	ASSERT(!(lock_mode & (XFS_ILOCK_PARENT | XFS_ILOCK_RTBITMAP |
4170952c818SDave Chinner 			      XFS_ILOCK_RTSUM)));
4183403ccc0SDave Chinner 	ASSERT(xfs_lockdep_subclass_ok(subclass));
4190952c818SDave Chinner 
420653c60b6SDave Chinner 	if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) {
4210952c818SDave Chinner 		ASSERT(subclass <= XFS_IOLOCK_MAX_SUBCLASS);
4223403ccc0SDave Chinner 		ASSERT(xfs_lockdep_subclass_ok(subclass +
4233403ccc0SDave Chinner 						XFS_IOLOCK_PARENT_VAL));
4240952c818SDave Chinner 		class += subclass << XFS_IOLOCK_SHIFT;
4250952c818SDave Chinner 		if (lock_mode & XFS_IOLOCK_PARENT)
4260952c818SDave Chinner 			class += XFS_IOLOCK_PARENT_VAL << XFS_IOLOCK_SHIFT;
427653c60b6SDave Chinner 	}
428653c60b6SDave Chinner 
429653c60b6SDave Chinner 	if (lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) {
4300952c818SDave Chinner 		ASSERT(subclass <= XFS_MMAPLOCK_MAX_SUBCLASS);
4310952c818SDave Chinner 		class += subclass << XFS_MMAPLOCK_SHIFT;
432653c60b6SDave Chinner 	}
433653c60b6SDave Chinner 
4340952c818SDave Chinner 	if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) {
4350952c818SDave Chinner 		ASSERT(subclass <= XFS_ILOCK_MAX_SUBCLASS);
4360952c818SDave Chinner 		class += subclass << XFS_ILOCK_SHIFT;
4370952c818SDave Chinner 	}
438c24b5dfaSDave Chinner 
4390952c818SDave Chinner 	return (lock_mode & ~XFS_LOCK_SUBCLASS_MASK) | class;
440c24b5dfaSDave Chinner }
441c24b5dfaSDave Chinner 
442c24b5dfaSDave Chinner /*
44395afcf5cSDave Chinner  * The following routine will lock n inodes in exclusive mode.  We assume the
44495afcf5cSDave Chinner  * caller calls us with the inodes in i_ino order.
445c24b5dfaSDave Chinner  *
44695afcf5cSDave Chinner  * We need to detect deadlock where an inode that we lock is in the AIL and we
44795afcf5cSDave Chinner  * start waiting for another inode that is locked by a thread in a long running
44895afcf5cSDave Chinner  * transaction (such as truncate). This can result in deadlock since the long
44995afcf5cSDave Chinner  * running trans might need to wait for the inode we just locked in order to
45095afcf5cSDave Chinner  * push the tail and free space in the log.
4510952c818SDave Chinner  *
4520952c818SDave Chinner  * xfs_lock_inodes() can only be used to lock one type of lock at a time -
4530952c818SDave Chinner  * the iolock, the mmaplock or the ilock, but not more than one at a time. If we
4540952c818SDave Chinner  * lock more than one at a time, lockdep will report false positives saying we
4550952c818SDave Chinner  * have violated locking orders.
456c24b5dfaSDave Chinner  */
4570d5a75e9SEric Sandeen static void
458c24b5dfaSDave Chinner xfs_lock_inodes(
459c24b5dfaSDave Chinner 	xfs_inode_t	**ips,
460c24b5dfaSDave Chinner 	int		inodes,
461c24b5dfaSDave Chinner 	uint		lock_mode)
462c24b5dfaSDave Chinner {
463c24b5dfaSDave Chinner 	int		attempts = 0, i, j, try_lock;
464c24b5dfaSDave Chinner 	xfs_log_item_t	*lp;
465c24b5dfaSDave Chinner 
4660952c818SDave Chinner 	/*
4670952c818SDave Chinner 	 * Currently supports between 2 and 5 inodes with exclusive locking.  We
4680952c818SDave Chinner 	 * support an arbitrary depth of locking here, but absolute limits on
4690952c818SDave Chinner 	 * inodes depend on the the type of locking and the limits placed by
4700952c818SDave Chinner 	 * lockdep annotations in xfs_lock_inumorder.  These are all checked by
4710952c818SDave Chinner 	 * the asserts.
4720952c818SDave Chinner 	 */
47395afcf5cSDave Chinner 	ASSERT(ips && inodes >= 2 && inodes <= 5);
4740952c818SDave Chinner 	ASSERT(lock_mode & (XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL |
4750952c818SDave Chinner 			    XFS_ILOCK_EXCL));
4760952c818SDave Chinner 	ASSERT(!(lock_mode & (XFS_IOLOCK_SHARED | XFS_MMAPLOCK_SHARED |
4770952c818SDave Chinner 			      XFS_ILOCK_SHARED)));
4780952c818SDave Chinner 	ASSERT(!(lock_mode & XFS_IOLOCK_EXCL) ||
4790952c818SDave Chinner 		inodes <= XFS_IOLOCK_MAX_SUBCLASS + 1);
4800952c818SDave Chinner 	ASSERT(!(lock_mode & XFS_MMAPLOCK_EXCL) ||
4810952c818SDave Chinner 		inodes <= XFS_MMAPLOCK_MAX_SUBCLASS + 1);
4820952c818SDave Chinner 	ASSERT(!(lock_mode & XFS_ILOCK_EXCL) ||
4830952c818SDave Chinner 		inodes <= XFS_ILOCK_MAX_SUBCLASS + 1);
4840952c818SDave Chinner 
4850952c818SDave Chinner 	if (lock_mode & XFS_IOLOCK_EXCL) {
4860952c818SDave Chinner 		ASSERT(!(lock_mode & (XFS_MMAPLOCK_EXCL | XFS_ILOCK_EXCL)));
4870952c818SDave Chinner 	} else if (lock_mode & XFS_MMAPLOCK_EXCL)
4880952c818SDave Chinner 		ASSERT(!(lock_mode & XFS_ILOCK_EXCL));
489c24b5dfaSDave Chinner 
490c24b5dfaSDave Chinner 	try_lock = 0;
491c24b5dfaSDave Chinner 	i = 0;
492c24b5dfaSDave Chinner again:
493c24b5dfaSDave Chinner 	for (; i < inodes; i++) {
494c24b5dfaSDave Chinner 		ASSERT(ips[i]);
495c24b5dfaSDave Chinner 
496c24b5dfaSDave Chinner 		if (i && (ips[i] == ips[i - 1]))	/* Already locked */
497c24b5dfaSDave Chinner 			continue;
498c24b5dfaSDave Chinner 
499c24b5dfaSDave Chinner 		/*
50095afcf5cSDave Chinner 		 * If try_lock is not set yet, make sure all locked inodes are
50195afcf5cSDave Chinner 		 * not in the AIL.  If any are, set try_lock to be used later.
502c24b5dfaSDave Chinner 		 */
503c24b5dfaSDave Chinner 		if (!try_lock) {
504c24b5dfaSDave Chinner 			for (j = (i - 1); j >= 0 && !try_lock; j--) {
505c24b5dfaSDave Chinner 				lp = (xfs_log_item_t *)ips[j]->i_itemp;
50695afcf5cSDave Chinner 				if (lp && (lp->li_flags & XFS_LI_IN_AIL))
507c24b5dfaSDave Chinner 					try_lock++;
508c24b5dfaSDave Chinner 			}
509c24b5dfaSDave Chinner 		}
510c24b5dfaSDave Chinner 
511c24b5dfaSDave Chinner 		/*
512c24b5dfaSDave Chinner 		 * If any of the previous locks we have locked is in the AIL,
513c24b5dfaSDave Chinner 		 * we must TRY to get the second and subsequent locks. If
514c24b5dfaSDave Chinner 		 * we can't get any, we must release all we have
515c24b5dfaSDave Chinner 		 * and try again.
516c24b5dfaSDave Chinner 		 */
51795afcf5cSDave Chinner 		if (!try_lock) {
51895afcf5cSDave Chinner 			xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
51995afcf5cSDave Chinner 			continue;
52095afcf5cSDave Chinner 		}
521c24b5dfaSDave Chinner 
52295afcf5cSDave Chinner 		/* try_lock means we have an inode locked that is in the AIL. */
523c24b5dfaSDave Chinner 		ASSERT(i != 0);
52495afcf5cSDave Chinner 		if (xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i)))
52595afcf5cSDave Chinner 			continue;
52695afcf5cSDave Chinner 
52795afcf5cSDave Chinner 		/*
52895afcf5cSDave Chinner 		 * Unlock all previous guys and try again.  xfs_iunlock will try
52995afcf5cSDave Chinner 		 * to push the tail if the inode is in the AIL.
53095afcf5cSDave Chinner 		 */
531c24b5dfaSDave Chinner 		attempts++;
532c24b5dfaSDave Chinner 		for (j = i - 1; j >= 0; j--) {
533c24b5dfaSDave Chinner 			/*
53495afcf5cSDave Chinner 			 * Check to see if we've already unlocked this one.  Not
53595afcf5cSDave Chinner 			 * the first one going back, and the inode ptr is the
53695afcf5cSDave Chinner 			 * same.
537c24b5dfaSDave Chinner 			 */
53895afcf5cSDave Chinner 			if (j != (i - 1) && ips[j] == ips[j + 1])
539c24b5dfaSDave Chinner 				continue;
540c24b5dfaSDave Chinner 
541c24b5dfaSDave Chinner 			xfs_iunlock(ips[j], lock_mode);
542c24b5dfaSDave Chinner 		}
543c24b5dfaSDave Chinner 
544c24b5dfaSDave Chinner 		if ((attempts % 5) == 0) {
545c24b5dfaSDave Chinner 			delay(1); /* Don't just spin the CPU */
546c24b5dfaSDave Chinner #ifdef DEBUG
547c24b5dfaSDave Chinner 			xfs_lock_delays++;
548c24b5dfaSDave Chinner #endif
549c24b5dfaSDave Chinner 		}
550c24b5dfaSDave Chinner 		i = 0;
551c24b5dfaSDave Chinner 		try_lock = 0;
552c24b5dfaSDave Chinner 		goto again;
553c24b5dfaSDave Chinner 	}
554c24b5dfaSDave Chinner 
555c24b5dfaSDave Chinner #ifdef DEBUG
556c24b5dfaSDave Chinner 	if (attempts) {
557c24b5dfaSDave Chinner 		if (attempts < 5) xfs_small_retries++;
558c24b5dfaSDave Chinner 		else if (attempts < 100) xfs_middle_retries++;
559c24b5dfaSDave Chinner 		else xfs_lots_retries++;
560c24b5dfaSDave Chinner 	} else {
561c24b5dfaSDave Chinner 		xfs_locked_n++;
562c24b5dfaSDave Chinner 	}
563c24b5dfaSDave Chinner #endif
564c24b5dfaSDave Chinner }
565c24b5dfaSDave Chinner 
566c24b5dfaSDave Chinner /*
567653c60b6SDave Chinner  * xfs_lock_two_inodes() can only be used to lock one type of lock at a time -
568653c60b6SDave Chinner  * the iolock, the mmaplock or the ilock, but not more than one at a time. If we
569653c60b6SDave Chinner  * lock more than one at a time, lockdep will report false positives saying we
570653c60b6SDave Chinner  * have violated locking orders.
571c24b5dfaSDave Chinner  */
572c24b5dfaSDave Chinner void
573c24b5dfaSDave Chinner xfs_lock_two_inodes(
574c24b5dfaSDave Chinner 	xfs_inode_t		*ip0,
575c24b5dfaSDave Chinner 	xfs_inode_t		*ip1,
576c24b5dfaSDave Chinner 	uint			lock_mode)
577c24b5dfaSDave Chinner {
578c24b5dfaSDave Chinner 	xfs_inode_t		*temp;
579c24b5dfaSDave Chinner 	int			attempts = 0;
580c24b5dfaSDave Chinner 	xfs_log_item_t		*lp;
581c24b5dfaSDave Chinner 
582653c60b6SDave Chinner 	if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) {
583653c60b6SDave Chinner 		ASSERT(!(lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)));
584653c60b6SDave Chinner 		ASSERT(!(lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
585653c60b6SDave Chinner 	} else if (lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL))
586653c60b6SDave Chinner 		ASSERT(!(lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
587653c60b6SDave Chinner 
588c24b5dfaSDave Chinner 	ASSERT(ip0->i_ino != ip1->i_ino);
589c24b5dfaSDave Chinner 
590c24b5dfaSDave Chinner 	if (ip0->i_ino > ip1->i_ino) {
591c24b5dfaSDave Chinner 		temp = ip0;
592c24b5dfaSDave Chinner 		ip0 = ip1;
593c24b5dfaSDave Chinner 		ip1 = temp;
594c24b5dfaSDave Chinner 	}
595c24b5dfaSDave Chinner 
596c24b5dfaSDave Chinner  again:
597c24b5dfaSDave Chinner 	xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
598c24b5dfaSDave Chinner 
599c24b5dfaSDave Chinner 	/*
600c24b5dfaSDave Chinner 	 * If the first lock we have locked is in the AIL, we must TRY to get
601c24b5dfaSDave Chinner 	 * the second lock. If we can't get it, we must release the first one
602c24b5dfaSDave Chinner 	 * and try again.
603c24b5dfaSDave Chinner 	 */
604c24b5dfaSDave Chinner 	lp = (xfs_log_item_t *)ip0->i_itemp;
605c24b5dfaSDave Chinner 	if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
606c24b5dfaSDave Chinner 		if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
607c24b5dfaSDave Chinner 			xfs_iunlock(ip0, lock_mode);
608c24b5dfaSDave Chinner 			if ((++attempts % 5) == 0)
609c24b5dfaSDave Chinner 				delay(1); /* Don't just spin the CPU */
610c24b5dfaSDave Chinner 			goto again;
611c24b5dfaSDave Chinner 		}
612c24b5dfaSDave Chinner 	} else {
613c24b5dfaSDave Chinner 		xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
614c24b5dfaSDave Chinner 	}
615c24b5dfaSDave Chinner }
616c24b5dfaSDave Chinner 
617c24b5dfaSDave Chinner 
618fa96acadSDave Chinner void
619fa96acadSDave Chinner __xfs_iflock(
620fa96acadSDave Chinner 	struct xfs_inode	*ip)
621fa96acadSDave Chinner {
622fa96acadSDave Chinner 	wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IFLOCK_BIT);
623fa96acadSDave Chinner 	DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IFLOCK_BIT);
624fa96acadSDave Chinner 
625fa96acadSDave Chinner 	do {
626fa96acadSDave Chinner 		prepare_to_wait_exclusive(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
627fa96acadSDave Chinner 		if (xfs_isiflocked(ip))
628fa96acadSDave Chinner 			io_schedule();
629fa96acadSDave Chinner 	} while (!xfs_iflock_nowait(ip));
630fa96acadSDave Chinner 
631fa96acadSDave Chinner 	finish_wait(wq, &wait.wait);
632fa96acadSDave Chinner }
633fa96acadSDave Chinner 
6341da177e4SLinus Torvalds STATIC uint
6351da177e4SLinus Torvalds _xfs_dic2xflags(
63658f88ca2SDave Chinner 	__uint16_t		di_flags,
63758f88ca2SDave Chinner 	uint64_t		di_flags2,
63858f88ca2SDave Chinner 	bool			has_attr)
6391da177e4SLinus Torvalds {
6401da177e4SLinus Torvalds 	uint			flags = 0;
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	if (di_flags & XFS_DIFLAG_ANY) {
6431da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_REALTIME)
644e7b89481SDave Chinner 			flags |= FS_XFLAG_REALTIME;
6451da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_PREALLOC)
646e7b89481SDave Chinner 			flags |= FS_XFLAG_PREALLOC;
6471da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_IMMUTABLE)
648e7b89481SDave Chinner 			flags |= FS_XFLAG_IMMUTABLE;
6491da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_APPEND)
650e7b89481SDave Chinner 			flags |= FS_XFLAG_APPEND;
6511da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_SYNC)
652e7b89481SDave Chinner 			flags |= FS_XFLAG_SYNC;
6531da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_NOATIME)
654e7b89481SDave Chinner 			flags |= FS_XFLAG_NOATIME;
6551da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_NODUMP)
656e7b89481SDave Chinner 			flags |= FS_XFLAG_NODUMP;
6571da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_RTINHERIT)
658e7b89481SDave Chinner 			flags |= FS_XFLAG_RTINHERIT;
6591da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_PROJINHERIT)
660e7b89481SDave Chinner 			flags |= FS_XFLAG_PROJINHERIT;
6611da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_NOSYMLINKS)
662e7b89481SDave Chinner 			flags |= FS_XFLAG_NOSYMLINKS;
663dd9f438eSNathan Scott 		if (di_flags & XFS_DIFLAG_EXTSIZE)
664e7b89481SDave Chinner 			flags |= FS_XFLAG_EXTSIZE;
665dd9f438eSNathan Scott 		if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
666e7b89481SDave Chinner 			flags |= FS_XFLAG_EXTSZINHERIT;
667d3446eacSBarry Naujok 		if (di_flags & XFS_DIFLAG_NODEFRAG)
668e7b89481SDave Chinner 			flags |= FS_XFLAG_NODEFRAG;
6692a82b8beSDavid Chinner 		if (di_flags & XFS_DIFLAG_FILESTREAM)
670e7b89481SDave Chinner 			flags |= FS_XFLAG_FILESTREAM;
6711da177e4SLinus Torvalds 	}
6721da177e4SLinus Torvalds 
67358f88ca2SDave Chinner 	if (di_flags2 & XFS_DIFLAG2_ANY) {
67458f88ca2SDave Chinner 		if (di_flags2 & XFS_DIFLAG2_DAX)
67558f88ca2SDave Chinner 			flags |= FS_XFLAG_DAX;
676*f7ca3522SDarrick J. Wong 		if (di_flags2 & XFS_DIFLAG2_COWEXTSIZE)
677*f7ca3522SDarrick J. Wong 			flags |= FS_XFLAG_COWEXTSIZE;
67858f88ca2SDave Chinner 	}
67958f88ca2SDave Chinner 
68058f88ca2SDave Chinner 	if (has_attr)
68158f88ca2SDave Chinner 		flags |= FS_XFLAG_HASATTR;
68258f88ca2SDave Chinner 
6831da177e4SLinus Torvalds 	return flags;
6841da177e4SLinus Torvalds }
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds uint
6871da177e4SLinus Torvalds xfs_ip2xflags(
68858f88ca2SDave Chinner 	struct xfs_inode	*ip)
6891da177e4SLinus Torvalds {
69058f88ca2SDave Chinner 	struct xfs_icdinode	*dic = &ip->i_d;
6911da177e4SLinus Torvalds 
69258f88ca2SDave Chinner 	return _xfs_dic2xflags(dic->di_flags, dic->di_flags2, XFS_IFORK_Q(ip));
6931da177e4SLinus Torvalds }
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds /*
696c24b5dfaSDave Chinner  * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
697c24b5dfaSDave Chinner  * is allowed, otherwise it has to be an exact match. If a CI match is found,
698c24b5dfaSDave Chinner  * ci_name->name will point to a the actual name (caller must free) or
699c24b5dfaSDave Chinner  * will be set to NULL if an exact match is found.
700c24b5dfaSDave Chinner  */
701c24b5dfaSDave Chinner int
702c24b5dfaSDave Chinner xfs_lookup(
703c24b5dfaSDave Chinner 	xfs_inode_t		*dp,
704c24b5dfaSDave Chinner 	struct xfs_name		*name,
705c24b5dfaSDave Chinner 	xfs_inode_t		**ipp,
706c24b5dfaSDave Chinner 	struct xfs_name		*ci_name)
707c24b5dfaSDave Chinner {
708c24b5dfaSDave Chinner 	xfs_ino_t		inum;
709c24b5dfaSDave Chinner 	int			error;
710c24b5dfaSDave Chinner 
711c24b5dfaSDave Chinner 	trace_xfs_lookup(dp, name);
712c24b5dfaSDave Chinner 
713c24b5dfaSDave Chinner 	if (XFS_FORCED_SHUTDOWN(dp->i_mount))
7142451337dSDave Chinner 		return -EIO;
715c24b5dfaSDave Chinner 
716dbad7c99SDave Chinner 	xfs_ilock(dp, XFS_IOLOCK_SHARED);
717c24b5dfaSDave Chinner 	error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
718c24b5dfaSDave Chinner 	if (error)
719dbad7c99SDave Chinner 		goto out_unlock;
720c24b5dfaSDave Chinner 
721c24b5dfaSDave Chinner 	error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
722c24b5dfaSDave Chinner 	if (error)
723c24b5dfaSDave Chinner 		goto out_free_name;
724c24b5dfaSDave Chinner 
725dbad7c99SDave Chinner 	xfs_iunlock(dp, XFS_IOLOCK_SHARED);
726c24b5dfaSDave Chinner 	return 0;
727c24b5dfaSDave Chinner 
728c24b5dfaSDave Chinner out_free_name:
729c24b5dfaSDave Chinner 	if (ci_name)
730c24b5dfaSDave Chinner 		kmem_free(ci_name->name);
731dbad7c99SDave Chinner out_unlock:
732dbad7c99SDave Chinner 	xfs_iunlock(dp, XFS_IOLOCK_SHARED);
733c24b5dfaSDave Chinner 	*ipp = NULL;
734c24b5dfaSDave Chinner 	return error;
735c24b5dfaSDave Chinner }
736c24b5dfaSDave Chinner 
737c24b5dfaSDave Chinner /*
7381da177e4SLinus Torvalds  * Allocate an inode on disk and return a copy of its in-core version.
7391da177e4SLinus Torvalds  * The in-core inode is locked exclusively.  Set mode, nlink, and rdev
7401da177e4SLinus Torvalds  * appropriately within the inode.  The uid and gid for the inode are
7411da177e4SLinus Torvalds  * set according to the contents of the given cred structure.
7421da177e4SLinus Torvalds  *
7431da177e4SLinus Torvalds  * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
744cd856db6SCarlos Maiolino  * has a free inode available, call xfs_iget() to obtain the in-core
745cd856db6SCarlos Maiolino  * version of the allocated inode.  Finally, fill in the inode and
746cd856db6SCarlos Maiolino  * log its initial contents.  In this case, ialloc_context would be
747cd856db6SCarlos Maiolino  * set to NULL.
7481da177e4SLinus Torvalds  *
749cd856db6SCarlos Maiolino  * If xfs_dialloc() does not have an available inode, it will replenish
750cd856db6SCarlos Maiolino  * its supply by doing an allocation. Since we can only do one
751cd856db6SCarlos Maiolino  * allocation within a transaction without deadlocks, we must commit
752cd856db6SCarlos Maiolino  * the current transaction before returning the inode itself.
753cd856db6SCarlos Maiolino  * In this case, therefore, we will set ialloc_context and return.
7541da177e4SLinus Torvalds  * The caller should then commit the current transaction, start a new
7551da177e4SLinus Torvalds  * transaction, and call xfs_ialloc() again to actually get the inode.
7561da177e4SLinus Torvalds  *
7571da177e4SLinus Torvalds  * To ensure that some other process does not grab the inode that
7581da177e4SLinus Torvalds  * was allocated during the first call to xfs_ialloc(), this routine
7591da177e4SLinus Torvalds  * also returns the [locked] bp pointing to the head of the freelist
7601da177e4SLinus Torvalds  * as ialloc_context.  The caller should hold this buffer across
7611da177e4SLinus Torvalds  * the commit and pass it back into this routine on the second call.
762b11f94d5SDavid Chinner  *
763b11f94d5SDavid Chinner  * If we are allocating quota inodes, we do not have a parent inode
764b11f94d5SDavid Chinner  * to attach to or associate with (i.e. pip == NULL) because they
765b11f94d5SDavid Chinner  * are not linked into the directory structure - they are attached
766b11f94d5SDavid Chinner  * directly to the superblock - and so have no parent.
7671da177e4SLinus Torvalds  */
7680d5a75e9SEric Sandeen static int
7691da177e4SLinus Torvalds xfs_ialloc(
7701da177e4SLinus Torvalds 	xfs_trans_t	*tp,
7711da177e4SLinus Torvalds 	xfs_inode_t	*pip,
772576b1d67SAl Viro 	umode_t		mode,
77331b084aeSNathan Scott 	xfs_nlink_t	nlink,
7741da177e4SLinus Torvalds 	xfs_dev_t	rdev,
7756743099cSArkadiusz Mi?kiewicz 	prid_t		prid,
7761da177e4SLinus Torvalds 	int		okalloc,
7771da177e4SLinus Torvalds 	xfs_buf_t	**ialloc_context,
7781da177e4SLinus Torvalds 	xfs_inode_t	**ipp)
7791da177e4SLinus Torvalds {
78093848a99SChristoph Hellwig 	struct xfs_mount *mp = tp->t_mountp;
7811da177e4SLinus Torvalds 	xfs_ino_t	ino;
7821da177e4SLinus Torvalds 	xfs_inode_t	*ip;
7831da177e4SLinus Torvalds 	uint		flags;
7841da177e4SLinus Torvalds 	int		error;
785e076b0f3SDave Chinner 	struct timespec	tv;
7863987848cSDave Chinner 	struct inode	*inode;
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds 	/*
7891da177e4SLinus Torvalds 	 * Call the space management code to pick
7901da177e4SLinus Torvalds 	 * the on-disk inode to be allocated.
7911da177e4SLinus Torvalds 	 */
792b11f94d5SDavid Chinner 	error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
79308358906SChristoph Hellwig 			    ialloc_context, &ino);
794bf904248SDavid Chinner 	if (error)
7951da177e4SLinus Torvalds 		return error;
79608358906SChristoph Hellwig 	if (*ialloc_context || ino == NULLFSINO) {
7971da177e4SLinus Torvalds 		*ipp = NULL;
7981da177e4SLinus Torvalds 		return 0;
7991da177e4SLinus Torvalds 	}
8001da177e4SLinus Torvalds 	ASSERT(*ialloc_context == NULL);
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 	/*
8031da177e4SLinus Torvalds 	 * Get the in-core inode with the lock held exclusively.
8041da177e4SLinus Torvalds 	 * This is because we're setting fields here we need
8051da177e4SLinus Torvalds 	 * to prevent others from looking at until we're done.
8061da177e4SLinus Torvalds 	 */
80793848a99SChristoph Hellwig 	error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE,
808ec3ba85fSChristoph Hellwig 			 XFS_ILOCK_EXCL, &ip);
809bf904248SDavid Chinner 	if (error)
8101da177e4SLinus Torvalds 		return error;
8111da177e4SLinus Torvalds 	ASSERT(ip != NULL);
8123987848cSDave Chinner 	inode = VFS_I(ip);
8131da177e4SLinus Torvalds 
814263997a6SDave Chinner 	/*
815263997a6SDave Chinner 	 * We always convert v1 inodes to v2 now - we only support filesystems
816263997a6SDave Chinner 	 * with >= v2 inode capability, so there is no reason for ever leaving
817263997a6SDave Chinner 	 * an inode in v1 format.
818263997a6SDave Chinner 	 */
819263997a6SDave Chinner 	if (ip->i_d.di_version == 1)
820263997a6SDave Chinner 		ip->i_d.di_version = 2;
821263997a6SDave Chinner 
822c19b3b05SDave Chinner 	inode->i_mode = mode;
82354d7b5c1SDave Chinner 	set_nlink(inode, nlink);
8247aab1b28SDwight Engen 	ip->i_d.di_uid = xfs_kuid_to_uid(current_fsuid());
8257aab1b28SDwight Engen 	ip->i_d.di_gid = xfs_kgid_to_gid(current_fsgid());
8266743099cSArkadiusz Mi?kiewicz 	xfs_set_projid(ip, prid);
8271da177e4SLinus Torvalds 
828bd186aa9SChristoph Hellwig 	if (pip && XFS_INHERIT_GID(pip)) {
8291da177e4SLinus Torvalds 		ip->i_d.di_gid = pip->i_d.di_gid;
830c19b3b05SDave Chinner 		if ((VFS_I(pip)->i_mode & S_ISGID) && S_ISDIR(mode))
831c19b3b05SDave Chinner 			inode->i_mode |= S_ISGID;
8321da177e4SLinus Torvalds 	}
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds 	/*
8351da177e4SLinus Torvalds 	 * If the group ID of the new file does not match the effective group
8361da177e4SLinus Torvalds 	 * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
8371da177e4SLinus Torvalds 	 * (and only if the irix_sgid_inherit compatibility variable is set).
8381da177e4SLinus Torvalds 	 */
8391da177e4SLinus Torvalds 	if ((irix_sgid_inherit) &&
840c19b3b05SDave Chinner 	    (inode->i_mode & S_ISGID) &&
841c19b3b05SDave Chinner 	    (!in_group_p(xfs_gid_to_kgid(ip->i_d.di_gid))))
842c19b3b05SDave Chinner 		inode->i_mode &= ~S_ISGID;
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds 	ip->i_d.di_size = 0;
8451da177e4SLinus Torvalds 	ip->i_d.di_nextents = 0;
8461da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_nblocks == 0);
847dff35fd4SChristoph Hellwig 
848e076b0f3SDave Chinner 	tv = current_fs_time(mp->m_super);
8493987848cSDave Chinner 	inode->i_mtime = tv;
8503987848cSDave Chinner 	inode->i_atime = tv;
8513987848cSDave Chinner 	inode->i_ctime = tv;
852dff35fd4SChristoph Hellwig 
8531da177e4SLinus Torvalds 	ip->i_d.di_extsize = 0;
8541da177e4SLinus Torvalds 	ip->i_d.di_dmevmask = 0;
8551da177e4SLinus Torvalds 	ip->i_d.di_dmstate = 0;
8561da177e4SLinus Torvalds 	ip->i_d.di_flags = 0;
85793848a99SChristoph Hellwig 
85893848a99SChristoph Hellwig 	if (ip->i_d.di_version == 3) {
85983e06f21SDave Chinner 		inode->i_version = 1;
86093848a99SChristoph Hellwig 		ip->i_d.di_flags2 = 0;
861*f7ca3522SDarrick J. Wong 		ip->i_d.di_cowextsize = 0;
8623987848cSDave Chinner 		ip->i_d.di_crtime.t_sec = (__int32_t)tv.tv_sec;
8633987848cSDave Chinner 		ip->i_d.di_crtime.t_nsec = (__int32_t)tv.tv_nsec;
86493848a99SChristoph Hellwig 	}
86593848a99SChristoph Hellwig 
86693848a99SChristoph Hellwig 
8671da177e4SLinus Torvalds 	flags = XFS_ILOG_CORE;
8681da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
8691da177e4SLinus Torvalds 	case S_IFIFO:
8701da177e4SLinus Torvalds 	case S_IFCHR:
8711da177e4SLinus Torvalds 	case S_IFBLK:
8721da177e4SLinus Torvalds 	case S_IFSOCK:
8731da177e4SLinus Torvalds 		ip->i_d.di_format = XFS_DINODE_FMT_DEV;
8741da177e4SLinus Torvalds 		ip->i_df.if_u2.if_rdev = rdev;
8751da177e4SLinus Torvalds 		ip->i_df.if_flags = 0;
8761da177e4SLinus Torvalds 		flags |= XFS_ILOG_DEV;
8771da177e4SLinus Torvalds 		break;
8781da177e4SLinus Torvalds 	case S_IFREG:
8791da177e4SLinus Torvalds 	case S_IFDIR:
880b11f94d5SDavid Chinner 		if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
88158f88ca2SDave Chinner 			uint64_t	di_flags2 = 0;
882365ca83dSNathan Scott 			uint		di_flags = 0;
883365ca83dSNathan Scott 
884abbede1bSAl Viro 			if (S_ISDIR(mode)) {
885365ca83dSNathan Scott 				if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
886365ca83dSNathan Scott 					di_flags |= XFS_DIFLAG_RTINHERIT;
887dd9f438eSNathan Scott 				if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
888dd9f438eSNathan Scott 					di_flags |= XFS_DIFLAG_EXTSZINHERIT;
889dd9f438eSNathan Scott 					ip->i_d.di_extsize = pip->i_d.di_extsize;
890dd9f438eSNathan Scott 				}
8919336e3a7SDave Chinner 				if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
8929336e3a7SDave Chinner 					di_flags |= XFS_DIFLAG_PROJINHERIT;
893abbede1bSAl Viro 			} else if (S_ISREG(mode)) {
894613d7043SChristoph Hellwig 				if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
895365ca83dSNathan Scott 					di_flags |= XFS_DIFLAG_REALTIME;
896dd9f438eSNathan Scott 				if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
897dd9f438eSNathan Scott 					di_flags |= XFS_DIFLAG_EXTSIZE;
898dd9f438eSNathan Scott 					ip->i_d.di_extsize = pip->i_d.di_extsize;
899dd9f438eSNathan Scott 				}
9001da177e4SLinus Torvalds 			}
9011da177e4SLinus Torvalds 			if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
9021da177e4SLinus Torvalds 			    xfs_inherit_noatime)
903365ca83dSNathan Scott 				di_flags |= XFS_DIFLAG_NOATIME;
9041da177e4SLinus Torvalds 			if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
9051da177e4SLinus Torvalds 			    xfs_inherit_nodump)
906365ca83dSNathan Scott 				di_flags |= XFS_DIFLAG_NODUMP;
9071da177e4SLinus Torvalds 			if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
9081da177e4SLinus Torvalds 			    xfs_inherit_sync)
909365ca83dSNathan Scott 				di_flags |= XFS_DIFLAG_SYNC;
9101da177e4SLinus Torvalds 			if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
9111da177e4SLinus Torvalds 			    xfs_inherit_nosymlinks)
912365ca83dSNathan Scott 				di_flags |= XFS_DIFLAG_NOSYMLINKS;
913d3446eacSBarry Naujok 			if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
914d3446eacSBarry Naujok 			    xfs_inherit_nodefrag)
915d3446eacSBarry Naujok 				di_flags |= XFS_DIFLAG_NODEFRAG;
9162a82b8beSDavid Chinner 			if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
9172a82b8beSDavid Chinner 				di_flags |= XFS_DIFLAG_FILESTREAM;
91858f88ca2SDave Chinner 			if (pip->i_d.di_flags2 & XFS_DIFLAG2_DAX)
91958f88ca2SDave Chinner 				di_flags2 |= XFS_DIFLAG2_DAX;
92058f88ca2SDave Chinner 
921365ca83dSNathan Scott 			ip->i_d.di_flags |= di_flags;
92258f88ca2SDave Chinner 			ip->i_d.di_flags2 |= di_flags2;
9231da177e4SLinus Torvalds 		}
924*f7ca3522SDarrick J. Wong 		if (pip &&
925*f7ca3522SDarrick J. Wong 		    (pip->i_d.di_flags2 & XFS_DIFLAG2_ANY) &&
926*f7ca3522SDarrick J. Wong 		    pip->i_d.di_version == 3 &&
927*f7ca3522SDarrick J. Wong 		    ip->i_d.di_version == 3) {
928*f7ca3522SDarrick J. Wong 			if (pip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) {
929*f7ca3522SDarrick J. Wong 				ip->i_d.di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
930*f7ca3522SDarrick J. Wong 				ip->i_d.di_cowextsize = pip->i_d.di_cowextsize;
931*f7ca3522SDarrick J. Wong 			}
932*f7ca3522SDarrick J. Wong 		}
9331da177e4SLinus Torvalds 		/* FALLTHROUGH */
9341da177e4SLinus Torvalds 	case S_IFLNK:
9351da177e4SLinus Torvalds 		ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
9361da177e4SLinus Torvalds 		ip->i_df.if_flags = XFS_IFEXTENTS;
9371da177e4SLinus Torvalds 		ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
9381da177e4SLinus Torvalds 		ip->i_df.if_u1.if_extents = NULL;
9391da177e4SLinus Torvalds 		break;
9401da177e4SLinus Torvalds 	default:
9411da177e4SLinus Torvalds 		ASSERT(0);
9421da177e4SLinus Torvalds 	}
9431da177e4SLinus Torvalds 	/*
9441da177e4SLinus Torvalds 	 * Attribute fork settings for new inode.
9451da177e4SLinus Torvalds 	 */
9461da177e4SLinus Torvalds 	ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
9471da177e4SLinus Torvalds 	ip->i_d.di_anextents = 0;
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 	/*
9501da177e4SLinus Torvalds 	 * Log the new values stuffed into the inode.
9511da177e4SLinus Torvalds 	 */
952ddc3415aSChristoph Hellwig 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
9531da177e4SLinus Torvalds 	xfs_trans_log_inode(tp, ip, flags);
9541da177e4SLinus Torvalds 
95558c90473SDave Chinner 	/* now that we have an i_mode we can setup the inode structure */
95641be8bedSChristoph Hellwig 	xfs_setup_inode(ip);
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds 	*ipp = ip;
9591da177e4SLinus Torvalds 	return 0;
9601da177e4SLinus Torvalds }
9611da177e4SLinus Torvalds 
962e546cb79SDave Chinner /*
963e546cb79SDave Chinner  * Allocates a new inode from disk and return a pointer to the
964e546cb79SDave Chinner  * incore copy. This routine will internally commit the current
965e546cb79SDave Chinner  * transaction and allocate a new one if the Space Manager needed
966e546cb79SDave Chinner  * to do an allocation to replenish the inode free-list.
967e546cb79SDave Chinner  *
968e546cb79SDave Chinner  * This routine is designed to be called from xfs_create and
969e546cb79SDave Chinner  * xfs_create_dir.
970e546cb79SDave Chinner  *
971e546cb79SDave Chinner  */
972e546cb79SDave Chinner int
973e546cb79SDave Chinner xfs_dir_ialloc(
974e546cb79SDave Chinner 	xfs_trans_t	**tpp,		/* input: current transaction;
975e546cb79SDave Chinner 					   output: may be a new transaction. */
976e546cb79SDave Chinner 	xfs_inode_t	*dp,		/* directory within whose allocate
977e546cb79SDave Chinner 					   the inode. */
978e546cb79SDave Chinner 	umode_t		mode,
979e546cb79SDave Chinner 	xfs_nlink_t	nlink,
980e546cb79SDave Chinner 	xfs_dev_t	rdev,
981e546cb79SDave Chinner 	prid_t		prid,		/* project id */
982e546cb79SDave Chinner 	int		okalloc,	/* ok to allocate new space */
983e546cb79SDave Chinner 	xfs_inode_t	**ipp,		/* pointer to inode; it will be
984e546cb79SDave Chinner 					   locked. */
985e546cb79SDave Chinner 	int		*committed)
986e546cb79SDave Chinner 
987e546cb79SDave Chinner {
988e546cb79SDave Chinner 	xfs_trans_t	*tp;
989e546cb79SDave Chinner 	xfs_inode_t	*ip;
990e546cb79SDave Chinner 	xfs_buf_t	*ialloc_context = NULL;
991e546cb79SDave Chinner 	int		code;
992e546cb79SDave Chinner 	void		*dqinfo;
993e546cb79SDave Chinner 	uint		tflags;
994e546cb79SDave Chinner 
995e546cb79SDave Chinner 	tp = *tpp;
996e546cb79SDave Chinner 	ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
997e546cb79SDave Chinner 
998e546cb79SDave Chinner 	/*
999e546cb79SDave Chinner 	 * xfs_ialloc will return a pointer to an incore inode if
1000e546cb79SDave Chinner 	 * the Space Manager has an available inode on the free
1001e546cb79SDave Chinner 	 * list. Otherwise, it will do an allocation and replenish
1002e546cb79SDave Chinner 	 * the freelist.  Since we can only do one allocation per
1003e546cb79SDave Chinner 	 * transaction without deadlocks, we will need to commit the
1004e546cb79SDave Chinner 	 * current transaction and start a new one.  We will then
1005e546cb79SDave Chinner 	 * need to call xfs_ialloc again to get the inode.
1006e546cb79SDave Chinner 	 *
1007e546cb79SDave Chinner 	 * If xfs_ialloc did an allocation to replenish the freelist,
1008e546cb79SDave Chinner 	 * it returns the bp containing the head of the freelist as
1009e546cb79SDave Chinner 	 * ialloc_context. We will hold a lock on it across the
1010e546cb79SDave Chinner 	 * transaction commit so that no other process can steal
1011e546cb79SDave Chinner 	 * the inode(s) that we've just allocated.
1012e546cb79SDave Chinner 	 */
1013e546cb79SDave Chinner 	code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid, okalloc,
1014e546cb79SDave Chinner 			  &ialloc_context, &ip);
1015e546cb79SDave Chinner 
1016e546cb79SDave Chinner 	/*
1017e546cb79SDave Chinner 	 * Return an error if we were unable to allocate a new inode.
1018e546cb79SDave Chinner 	 * This should only happen if we run out of space on disk or
1019e546cb79SDave Chinner 	 * encounter a disk error.
1020e546cb79SDave Chinner 	 */
1021e546cb79SDave Chinner 	if (code) {
1022e546cb79SDave Chinner 		*ipp = NULL;
1023e546cb79SDave Chinner 		return code;
1024e546cb79SDave Chinner 	}
1025e546cb79SDave Chinner 	if (!ialloc_context && !ip) {
1026e546cb79SDave Chinner 		*ipp = NULL;
10272451337dSDave Chinner 		return -ENOSPC;
1028e546cb79SDave Chinner 	}
1029e546cb79SDave Chinner 
1030e546cb79SDave Chinner 	/*
1031e546cb79SDave Chinner 	 * If the AGI buffer is non-NULL, then we were unable to get an
1032e546cb79SDave Chinner 	 * inode in one operation.  We need to commit the current
1033e546cb79SDave Chinner 	 * transaction and call xfs_ialloc() again.  It is guaranteed
1034e546cb79SDave Chinner 	 * to succeed the second time.
1035e546cb79SDave Chinner 	 */
1036e546cb79SDave Chinner 	if (ialloc_context) {
1037e546cb79SDave Chinner 		/*
1038e546cb79SDave Chinner 		 * Normally, xfs_trans_commit releases all the locks.
1039e546cb79SDave Chinner 		 * We call bhold to hang on to the ialloc_context across
1040e546cb79SDave Chinner 		 * the commit.  Holding this buffer prevents any other
1041e546cb79SDave Chinner 		 * processes from doing any allocations in this
1042e546cb79SDave Chinner 		 * allocation group.
1043e546cb79SDave Chinner 		 */
1044e546cb79SDave Chinner 		xfs_trans_bhold(tp, ialloc_context);
1045e546cb79SDave Chinner 
1046e546cb79SDave Chinner 		/*
1047e546cb79SDave Chinner 		 * We want the quota changes to be associated with the next
1048e546cb79SDave Chinner 		 * transaction, NOT this one. So, detach the dqinfo from this
1049e546cb79SDave Chinner 		 * and attach it to the next transaction.
1050e546cb79SDave Chinner 		 */
1051e546cb79SDave Chinner 		dqinfo = NULL;
1052e546cb79SDave Chinner 		tflags = 0;
1053e546cb79SDave Chinner 		if (tp->t_dqinfo) {
1054e546cb79SDave Chinner 			dqinfo = (void *)tp->t_dqinfo;
1055e546cb79SDave Chinner 			tp->t_dqinfo = NULL;
1056e546cb79SDave Chinner 			tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY;
1057e546cb79SDave Chinner 			tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY);
1058e546cb79SDave Chinner 		}
1059e546cb79SDave Chinner 
10606e3e6d55SEryu Guan 		code = xfs_trans_roll(&tp, NULL);
10612e6db6c4SChristoph Hellwig 		if (committed != NULL)
1062e546cb79SDave Chinner 			*committed = 1;
10633d3c8b52SJie Liu 
1064e546cb79SDave Chinner 		/*
1065e546cb79SDave Chinner 		 * Re-attach the quota info that we detached from prev trx.
1066e546cb79SDave Chinner 		 */
1067e546cb79SDave Chinner 		if (dqinfo) {
1068e546cb79SDave Chinner 			tp->t_dqinfo = dqinfo;
1069e546cb79SDave Chinner 			tp->t_flags |= tflags;
1070e546cb79SDave Chinner 		}
1071e546cb79SDave Chinner 
1072e546cb79SDave Chinner 		if (code) {
1073e546cb79SDave Chinner 			xfs_buf_relse(ialloc_context);
10742e6db6c4SChristoph Hellwig 			*tpp = tp;
1075e546cb79SDave Chinner 			*ipp = NULL;
1076e546cb79SDave Chinner 			return code;
1077e546cb79SDave Chinner 		}
1078e546cb79SDave Chinner 		xfs_trans_bjoin(tp, ialloc_context);
1079e546cb79SDave Chinner 
1080e546cb79SDave Chinner 		/*
1081e546cb79SDave Chinner 		 * Call ialloc again. Since we've locked out all
1082e546cb79SDave Chinner 		 * other allocations in this allocation group,
1083e546cb79SDave Chinner 		 * this call should always succeed.
1084e546cb79SDave Chinner 		 */
1085e546cb79SDave Chinner 		code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid,
1086e546cb79SDave Chinner 				  okalloc, &ialloc_context, &ip);
1087e546cb79SDave Chinner 
1088e546cb79SDave Chinner 		/*
1089e546cb79SDave Chinner 		 * If we get an error at this point, return to the caller
1090e546cb79SDave Chinner 		 * so that the current transaction can be aborted.
1091e546cb79SDave Chinner 		 */
1092e546cb79SDave Chinner 		if (code) {
1093e546cb79SDave Chinner 			*tpp = tp;
1094e546cb79SDave Chinner 			*ipp = NULL;
1095e546cb79SDave Chinner 			return code;
1096e546cb79SDave Chinner 		}
1097e546cb79SDave Chinner 		ASSERT(!ialloc_context && ip);
1098e546cb79SDave Chinner 
1099e546cb79SDave Chinner 	} else {
1100e546cb79SDave Chinner 		if (committed != NULL)
1101e546cb79SDave Chinner 			*committed = 0;
1102e546cb79SDave Chinner 	}
1103e546cb79SDave Chinner 
1104e546cb79SDave Chinner 	*ipp = ip;
1105e546cb79SDave Chinner 	*tpp = tp;
1106e546cb79SDave Chinner 
1107e546cb79SDave Chinner 	return 0;
1108e546cb79SDave Chinner }
1109e546cb79SDave Chinner 
1110e546cb79SDave Chinner /*
111154d7b5c1SDave Chinner  * Decrement the link count on an inode & log the change.  If this causes the
111254d7b5c1SDave Chinner  * link count to go to zero, move the inode to AGI unlinked list so that it can
111354d7b5c1SDave Chinner  * be freed when the last active reference goes away via xfs_inactive().
1114e546cb79SDave Chinner  */
11150d5a75e9SEric Sandeen static int			/* error */
1116e546cb79SDave Chinner xfs_droplink(
1117e546cb79SDave Chinner 	xfs_trans_t *tp,
1118e546cb79SDave Chinner 	xfs_inode_t *ip)
1119e546cb79SDave Chinner {
1120e546cb79SDave Chinner 	xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1121e546cb79SDave Chinner 
1122e546cb79SDave Chinner 	drop_nlink(VFS_I(ip));
1123e546cb79SDave Chinner 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1124e546cb79SDave Chinner 
112554d7b5c1SDave Chinner 	if (VFS_I(ip)->i_nlink)
112654d7b5c1SDave Chinner 		return 0;
112754d7b5c1SDave Chinner 
112854d7b5c1SDave Chinner 	return xfs_iunlink(tp, ip);
1129e546cb79SDave Chinner }
1130e546cb79SDave Chinner 
1131e546cb79SDave Chinner /*
1132e546cb79SDave Chinner  * Increment the link count on an inode & log the change.
1133e546cb79SDave Chinner  */
11340d5a75e9SEric Sandeen static int
1135e546cb79SDave Chinner xfs_bumplink(
1136e546cb79SDave Chinner 	xfs_trans_t *tp,
1137e546cb79SDave Chinner 	xfs_inode_t *ip)
1138e546cb79SDave Chinner {
1139e546cb79SDave Chinner 	xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1140e546cb79SDave Chinner 
1141263997a6SDave Chinner 	ASSERT(ip->i_d.di_version > 1);
1142e546cb79SDave Chinner 	inc_nlink(VFS_I(ip));
1143e546cb79SDave Chinner 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1144e546cb79SDave Chinner 	return 0;
1145e546cb79SDave Chinner }
1146e546cb79SDave Chinner 
1147c24b5dfaSDave Chinner int
1148c24b5dfaSDave Chinner xfs_create(
1149c24b5dfaSDave Chinner 	xfs_inode_t		*dp,
1150c24b5dfaSDave Chinner 	struct xfs_name		*name,
1151c24b5dfaSDave Chinner 	umode_t			mode,
1152c24b5dfaSDave Chinner 	xfs_dev_t		rdev,
1153c24b5dfaSDave Chinner 	xfs_inode_t		**ipp)
1154c24b5dfaSDave Chinner {
1155c24b5dfaSDave Chinner 	int			is_dir = S_ISDIR(mode);
1156c24b5dfaSDave Chinner 	struct xfs_mount	*mp = dp->i_mount;
1157c24b5dfaSDave Chinner 	struct xfs_inode	*ip = NULL;
1158c24b5dfaSDave Chinner 	struct xfs_trans	*tp = NULL;
1159c24b5dfaSDave Chinner 	int			error;
11602c3234d1SDarrick J. Wong 	struct xfs_defer_ops	dfops;
1161c24b5dfaSDave Chinner 	xfs_fsblock_t		first_block;
1162c24b5dfaSDave Chinner 	bool                    unlock_dp_on_error = false;
1163c24b5dfaSDave Chinner 	prid_t			prid;
1164c24b5dfaSDave Chinner 	struct xfs_dquot	*udqp = NULL;
1165c24b5dfaSDave Chinner 	struct xfs_dquot	*gdqp = NULL;
1166c24b5dfaSDave Chinner 	struct xfs_dquot	*pdqp = NULL;
1167062647a8SBrian Foster 	struct xfs_trans_res	*tres;
1168c24b5dfaSDave Chinner 	uint			resblks;
1169c24b5dfaSDave Chinner 
1170c24b5dfaSDave Chinner 	trace_xfs_create(dp, name);
1171c24b5dfaSDave Chinner 
1172c24b5dfaSDave Chinner 	if (XFS_FORCED_SHUTDOWN(mp))
11732451337dSDave Chinner 		return -EIO;
1174c24b5dfaSDave Chinner 
1175163467d3SZhi Yong Wu 	prid = xfs_get_initial_prid(dp);
1176c24b5dfaSDave Chinner 
1177c24b5dfaSDave Chinner 	/*
1178c24b5dfaSDave Chinner 	 * Make sure that we have allocated dquot(s) on disk.
1179c24b5dfaSDave Chinner 	 */
11807aab1b28SDwight Engen 	error = xfs_qm_vop_dqalloc(dp, xfs_kuid_to_uid(current_fsuid()),
11817aab1b28SDwight Engen 					xfs_kgid_to_gid(current_fsgid()), prid,
1182c24b5dfaSDave Chinner 					XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
1183c24b5dfaSDave Chinner 					&udqp, &gdqp, &pdqp);
1184c24b5dfaSDave Chinner 	if (error)
1185c24b5dfaSDave Chinner 		return error;
1186c24b5dfaSDave Chinner 
1187c24b5dfaSDave Chinner 	if (is_dir) {
1188c24b5dfaSDave Chinner 		rdev = 0;
1189c24b5dfaSDave Chinner 		resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
1190062647a8SBrian Foster 		tres = &M_RES(mp)->tr_mkdir;
1191c24b5dfaSDave Chinner 	} else {
1192c24b5dfaSDave Chinner 		resblks = XFS_CREATE_SPACE_RES(mp, name->len);
1193062647a8SBrian Foster 		tres = &M_RES(mp)->tr_create;
1194c24b5dfaSDave Chinner 	}
1195c24b5dfaSDave Chinner 
1196c24b5dfaSDave Chinner 	/*
1197c24b5dfaSDave Chinner 	 * Initially assume that the file does not exist and
1198c24b5dfaSDave Chinner 	 * reserve the resources for that case.  If that is not
1199c24b5dfaSDave Chinner 	 * the case we'll drop the one we have and get a more
1200c24b5dfaSDave Chinner 	 * appropriate transaction later.
1201c24b5dfaSDave Chinner 	 */
1202253f4911SChristoph Hellwig 	error = xfs_trans_alloc(mp, tres, resblks, 0, 0, &tp);
12032451337dSDave Chinner 	if (error == -ENOSPC) {
1204c24b5dfaSDave Chinner 		/* flush outstanding delalloc blocks and retry */
1205c24b5dfaSDave Chinner 		xfs_flush_inodes(mp);
1206253f4911SChristoph Hellwig 		error = xfs_trans_alloc(mp, tres, resblks, 0, 0, &tp);
1207c24b5dfaSDave Chinner 	}
12082451337dSDave Chinner 	if (error == -ENOSPC) {
1209c24b5dfaSDave Chinner 		/* No space at all so try a "no-allocation" reservation */
1210c24b5dfaSDave Chinner 		resblks = 0;
1211253f4911SChristoph Hellwig 		error = xfs_trans_alloc(mp, tres, 0, 0, 0, &tp);
1212c24b5dfaSDave Chinner 	}
12134906e215SChristoph Hellwig 	if (error)
1214253f4911SChristoph Hellwig 		goto out_release_inode;
1215c24b5dfaSDave Chinner 
1216dbad7c99SDave Chinner 	xfs_ilock(dp, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL |
1217dbad7c99SDave Chinner 		      XFS_IOLOCK_PARENT | XFS_ILOCK_PARENT);
1218c24b5dfaSDave Chinner 	unlock_dp_on_error = true;
1219c24b5dfaSDave Chinner 
12202c3234d1SDarrick J. Wong 	xfs_defer_init(&dfops, &first_block);
1221c24b5dfaSDave Chinner 
1222c24b5dfaSDave Chinner 	/*
1223c24b5dfaSDave Chinner 	 * Reserve disk quota and the inode.
1224c24b5dfaSDave Chinner 	 */
1225c24b5dfaSDave Chinner 	error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
1226c24b5dfaSDave Chinner 						pdqp, resblks, 1, 0);
1227c24b5dfaSDave Chinner 	if (error)
1228c24b5dfaSDave Chinner 		goto out_trans_cancel;
1229c24b5dfaSDave Chinner 
123094f3cad5SEric Sandeen 	if (!resblks) {
123194f3cad5SEric Sandeen 		error = xfs_dir_canenter(tp, dp, name);
1232c24b5dfaSDave Chinner 		if (error)
1233c24b5dfaSDave Chinner 			goto out_trans_cancel;
123494f3cad5SEric Sandeen 	}
1235c24b5dfaSDave Chinner 
1236c24b5dfaSDave Chinner 	/*
1237c24b5dfaSDave Chinner 	 * A newly created regular or special file just has one directory
1238c24b5dfaSDave Chinner 	 * entry pointing to them, but a directory also the "." entry
1239c24b5dfaSDave Chinner 	 * pointing to itself.
1240c24b5dfaSDave Chinner 	 */
1241c24b5dfaSDave Chinner 	error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev,
1242f6106efaSEric Sandeen 			       prid, resblks > 0, &ip, NULL);
1243d6077aa3SJan Kara 	if (error)
1244c24b5dfaSDave Chinner 		goto out_trans_cancel;
1245c24b5dfaSDave Chinner 
1246c24b5dfaSDave Chinner 	/*
1247c24b5dfaSDave Chinner 	 * Now we join the directory inode to the transaction.  We do not do it
1248c24b5dfaSDave Chinner 	 * earlier because xfs_dir_ialloc might commit the previous transaction
1249c24b5dfaSDave Chinner 	 * (and release all the locks).  An error from here on will result in
1250c24b5dfaSDave Chinner 	 * the transaction cancel unlocking dp so don't do it explicitly in the
1251c24b5dfaSDave Chinner 	 * error path.
1252c24b5dfaSDave Chinner 	 */
1253dbad7c99SDave Chinner 	xfs_trans_ijoin(tp, dp, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1254c24b5dfaSDave Chinner 	unlock_dp_on_error = false;
1255c24b5dfaSDave Chinner 
1256c24b5dfaSDave Chinner 	error = xfs_dir_createname(tp, dp, name, ip->i_ino,
12572c3234d1SDarrick J. Wong 					&first_block, &dfops, resblks ?
1258c24b5dfaSDave Chinner 					resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
1259c24b5dfaSDave Chinner 	if (error) {
12602451337dSDave Chinner 		ASSERT(error != -ENOSPC);
12614906e215SChristoph Hellwig 		goto out_trans_cancel;
1262c24b5dfaSDave Chinner 	}
1263c24b5dfaSDave Chinner 	xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1264c24b5dfaSDave Chinner 	xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1265c24b5dfaSDave Chinner 
1266c24b5dfaSDave Chinner 	if (is_dir) {
1267c24b5dfaSDave Chinner 		error = xfs_dir_init(tp, ip, dp);
1268c24b5dfaSDave Chinner 		if (error)
1269c24b5dfaSDave Chinner 			goto out_bmap_cancel;
1270c24b5dfaSDave Chinner 
1271c24b5dfaSDave Chinner 		error = xfs_bumplink(tp, dp);
1272c24b5dfaSDave Chinner 		if (error)
1273c24b5dfaSDave Chinner 			goto out_bmap_cancel;
1274c24b5dfaSDave Chinner 	}
1275c24b5dfaSDave Chinner 
1276c24b5dfaSDave Chinner 	/*
1277c24b5dfaSDave Chinner 	 * If this is a synchronous mount, make sure that the
1278c24b5dfaSDave Chinner 	 * create transaction goes to disk before returning to
1279c24b5dfaSDave Chinner 	 * the user.
1280c24b5dfaSDave Chinner 	 */
1281c24b5dfaSDave Chinner 	if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1282c24b5dfaSDave Chinner 		xfs_trans_set_sync(tp);
1283c24b5dfaSDave Chinner 
1284c24b5dfaSDave Chinner 	/*
1285c24b5dfaSDave Chinner 	 * Attach the dquot(s) to the inodes and modify them incore.
1286c24b5dfaSDave Chinner 	 * These ids of the inode couldn't have changed since the new
1287c24b5dfaSDave Chinner 	 * inode has been locked ever since it was created.
1288c24b5dfaSDave Chinner 	 */
1289c24b5dfaSDave Chinner 	xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
1290c24b5dfaSDave Chinner 
12912c3234d1SDarrick J. Wong 	error = xfs_defer_finish(&tp, &dfops, NULL);
1292c24b5dfaSDave Chinner 	if (error)
1293c24b5dfaSDave Chinner 		goto out_bmap_cancel;
1294c24b5dfaSDave Chinner 
129570393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
1296c24b5dfaSDave Chinner 	if (error)
1297c24b5dfaSDave Chinner 		goto out_release_inode;
1298c24b5dfaSDave Chinner 
1299c24b5dfaSDave Chinner 	xfs_qm_dqrele(udqp);
1300c24b5dfaSDave Chinner 	xfs_qm_dqrele(gdqp);
1301c24b5dfaSDave Chinner 	xfs_qm_dqrele(pdqp);
1302c24b5dfaSDave Chinner 
1303c24b5dfaSDave Chinner 	*ipp = ip;
1304c24b5dfaSDave Chinner 	return 0;
1305c24b5dfaSDave Chinner 
1306c24b5dfaSDave Chinner  out_bmap_cancel:
13072c3234d1SDarrick J. Wong 	xfs_defer_cancel(&dfops);
1308c24b5dfaSDave Chinner  out_trans_cancel:
13094906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
1310c24b5dfaSDave Chinner  out_release_inode:
1311c24b5dfaSDave Chinner 	/*
131258c90473SDave Chinner 	 * Wait until after the current transaction is aborted to finish the
131358c90473SDave Chinner 	 * setup of the inode and release the inode.  This prevents recursive
131458c90473SDave Chinner 	 * transactions and deadlocks from xfs_inactive.
1315c24b5dfaSDave Chinner 	 */
131658c90473SDave Chinner 	if (ip) {
131758c90473SDave Chinner 		xfs_finish_inode_setup(ip);
1318c24b5dfaSDave Chinner 		IRELE(ip);
131958c90473SDave Chinner 	}
1320c24b5dfaSDave Chinner 
1321c24b5dfaSDave Chinner 	xfs_qm_dqrele(udqp);
1322c24b5dfaSDave Chinner 	xfs_qm_dqrele(gdqp);
1323c24b5dfaSDave Chinner 	xfs_qm_dqrele(pdqp);
1324c24b5dfaSDave Chinner 
1325c24b5dfaSDave Chinner 	if (unlock_dp_on_error)
1326dbad7c99SDave Chinner 		xfs_iunlock(dp, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1327c24b5dfaSDave Chinner 	return error;
1328c24b5dfaSDave Chinner }
1329c24b5dfaSDave Chinner 
1330c24b5dfaSDave Chinner int
133199b6436bSZhi Yong Wu xfs_create_tmpfile(
133299b6436bSZhi Yong Wu 	struct xfs_inode	*dp,
133399b6436bSZhi Yong Wu 	struct dentry		*dentry,
1334330033d6SBrian Foster 	umode_t			mode,
1335330033d6SBrian Foster 	struct xfs_inode	**ipp)
133699b6436bSZhi Yong Wu {
133799b6436bSZhi Yong Wu 	struct xfs_mount	*mp = dp->i_mount;
133899b6436bSZhi Yong Wu 	struct xfs_inode	*ip = NULL;
133999b6436bSZhi Yong Wu 	struct xfs_trans	*tp = NULL;
134099b6436bSZhi Yong Wu 	int			error;
134199b6436bSZhi Yong Wu 	prid_t                  prid;
134299b6436bSZhi Yong Wu 	struct xfs_dquot	*udqp = NULL;
134399b6436bSZhi Yong Wu 	struct xfs_dquot	*gdqp = NULL;
134499b6436bSZhi Yong Wu 	struct xfs_dquot	*pdqp = NULL;
134599b6436bSZhi Yong Wu 	struct xfs_trans_res	*tres;
134699b6436bSZhi Yong Wu 	uint			resblks;
134799b6436bSZhi Yong Wu 
134899b6436bSZhi Yong Wu 	if (XFS_FORCED_SHUTDOWN(mp))
13492451337dSDave Chinner 		return -EIO;
135099b6436bSZhi Yong Wu 
135199b6436bSZhi Yong Wu 	prid = xfs_get_initial_prid(dp);
135299b6436bSZhi Yong Wu 
135399b6436bSZhi Yong Wu 	/*
135499b6436bSZhi Yong Wu 	 * Make sure that we have allocated dquot(s) on disk.
135599b6436bSZhi Yong Wu 	 */
135699b6436bSZhi Yong Wu 	error = xfs_qm_vop_dqalloc(dp, xfs_kuid_to_uid(current_fsuid()),
135799b6436bSZhi Yong Wu 				xfs_kgid_to_gid(current_fsgid()), prid,
135899b6436bSZhi Yong Wu 				XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
135999b6436bSZhi Yong Wu 				&udqp, &gdqp, &pdqp);
136099b6436bSZhi Yong Wu 	if (error)
136199b6436bSZhi Yong Wu 		return error;
136299b6436bSZhi Yong Wu 
136399b6436bSZhi Yong Wu 	resblks = XFS_IALLOC_SPACE_RES(mp);
136499b6436bSZhi Yong Wu 	tres = &M_RES(mp)->tr_create_tmpfile;
1365253f4911SChristoph Hellwig 
1366253f4911SChristoph Hellwig 	error = xfs_trans_alloc(mp, tres, resblks, 0, 0, &tp);
13672451337dSDave Chinner 	if (error == -ENOSPC) {
136899b6436bSZhi Yong Wu 		/* No space at all so try a "no-allocation" reservation */
136999b6436bSZhi Yong Wu 		resblks = 0;
1370253f4911SChristoph Hellwig 		error = xfs_trans_alloc(mp, tres, 0, 0, 0, &tp);
137199b6436bSZhi Yong Wu 	}
13724906e215SChristoph Hellwig 	if (error)
1373253f4911SChristoph Hellwig 		goto out_release_inode;
137499b6436bSZhi Yong Wu 
137599b6436bSZhi Yong Wu 	error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
137699b6436bSZhi Yong Wu 						pdqp, resblks, 1, 0);
137799b6436bSZhi Yong Wu 	if (error)
137899b6436bSZhi Yong Wu 		goto out_trans_cancel;
137999b6436bSZhi Yong Wu 
138099b6436bSZhi Yong Wu 	error = xfs_dir_ialloc(&tp, dp, mode, 1, 0,
138199b6436bSZhi Yong Wu 				prid, resblks > 0, &ip, NULL);
1382d6077aa3SJan Kara 	if (error)
138399b6436bSZhi Yong Wu 		goto out_trans_cancel;
138499b6436bSZhi Yong Wu 
138599b6436bSZhi Yong Wu 	if (mp->m_flags & XFS_MOUNT_WSYNC)
138699b6436bSZhi Yong Wu 		xfs_trans_set_sync(tp);
138799b6436bSZhi Yong Wu 
138899b6436bSZhi Yong Wu 	/*
138999b6436bSZhi Yong Wu 	 * Attach the dquot(s) to the inodes and modify them incore.
139099b6436bSZhi Yong Wu 	 * These ids of the inode couldn't have changed since the new
139199b6436bSZhi Yong Wu 	 * inode has been locked ever since it was created.
139299b6436bSZhi Yong Wu 	 */
139399b6436bSZhi Yong Wu 	xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
139499b6436bSZhi Yong Wu 
139599b6436bSZhi Yong Wu 	error = xfs_iunlink(tp, ip);
139699b6436bSZhi Yong Wu 	if (error)
13974906e215SChristoph Hellwig 		goto out_trans_cancel;
139899b6436bSZhi Yong Wu 
139970393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
140099b6436bSZhi Yong Wu 	if (error)
140199b6436bSZhi Yong Wu 		goto out_release_inode;
140299b6436bSZhi Yong Wu 
140399b6436bSZhi Yong Wu 	xfs_qm_dqrele(udqp);
140499b6436bSZhi Yong Wu 	xfs_qm_dqrele(gdqp);
140599b6436bSZhi Yong Wu 	xfs_qm_dqrele(pdqp);
140699b6436bSZhi Yong Wu 
1407330033d6SBrian Foster 	*ipp = ip;
140899b6436bSZhi Yong Wu 	return 0;
140999b6436bSZhi Yong Wu 
141099b6436bSZhi Yong Wu  out_trans_cancel:
14114906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
141299b6436bSZhi Yong Wu  out_release_inode:
141399b6436bSZhi Yong Wu 	/*
141458c90473SDave Chinner 	 * Wait until after the current transaction is aborted to finish the
141558c90473SDave Chinner 	 * setup of the inode and release the inode.  This prevents recursive
141658c90473SDave Chinner 	 * transactions and deadlocks from xfs_inactive.
141799b6436bSZhi Yong Wu 	 */
141858c90473SDave Chinner 	if (ip) {
141958c90473SDave Chinner 		xfs_finish_inode_setup(ip);
142099b6436bSZhi Yong Wu 		IRELE(ip);
142158c90473SDave Chinner 	}
142299b6436bSZhi Yong Wu 
142399b6436bSZhi Yong Wu 	xfs_qm_dqrele(udqp);
142499b6436bSZhi Yong Wu 	xfs_qm_dqrele(gdqp);
142599b6436bSZhi Yong Wu 	xfs_qm_dqrele(pdqp);
142699b6436bSZhi Yong Wu 
142799b6436bSZhi Yong Wu 	return error;
142899b6436bSZhi Yong Wu }
142999b6436bSZhi Yong Wu 
143099b6436bSZhi Yong Wu int
1431c24b5dfaSDave Chinner xfs_link(
1432c24b5dfaSDave Chinner 	xfs_inode_t		*tdp,
1433c24b5dfaSDave Chinner 	xfs_inode_t		*sip,
1434c24b5dfaSDave Chinner 	struct xfs_name		*target_name)
1435c24b5dfaSDave Chinner {
1436c24b5dfaSDave Chinner 	xfs_mount_t		*mp = tdp->i_mount;
1437c24b5dfaSDave Chinner 	xfs_trans_t		*tp;
1438c24b5dfaSDave Chinner 	int			error;
14392c3234d1SDarrick J. Wong 	struct xfs_defer_ops	dfops;
1440c24b5dfaSDave Chinner 	xfs_fsblock_t           first_block;
1441c24b5dfaSDave Chinner 	int			resblks;
1442c24b5dfaSDave Chinner 
1443c24b5dfaSDave Chinner 	trace_xfs_link(tdp, target_name);
1444c24b5dfaSDave Chinner 
1445c19b3b05SDave Chinner 	ASSERT(!S_ISDIR(VFS_I(sip)->i_mode));
1446c24b5dfaSDave Chinner 
1447c24b5dfaSDave Chinner 	if (XFS_FORCED_SHUTDOWN(mp))
14482451337dSDave Chinner 		return -EIO;
1449c24b5dfaSDave Chinner 
1450c24b5dfaSDave Chinner 	error = xfs_qm_dqattach(sip, 0);
1451c24b5dfaSDave Chinner 	if (error)
1452c24b5dfaSDave Chinner 		goto std_return;
1453c24b5dfaSDave Chinner 
1454c24b5dfaSDave Chinner 	error = xfs_qm_dqattach(tdp, 0);
1455c24b5dfaSDave Chinner 	if (error)
1456c24b5dfaSDave Chinner 		goto std_return;
1457c24b5dfaSDave Chinner 
1458c24b5dfaSDave Chinner 	resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
1459253f4911SChristoph Hellwig 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_link, resblks, 0, 0, &tp);
14602451337dSDave Chinner 	if (error == -ENOSPC) {
1461c24b5dfaSDave Chinner 		resblks = 0;
1462253f4911SChristoph Hellwig 		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_link, 0, 0, 0, &tp);
1463c24b5dfaSDave Chinner 	}
14644906e215SChristoph Hellwig 	if (error)
1465253f4911SChristoph Hellwig 		goto std_return;
1466c24b5dfaSDave Chinner 
1467dbad7c99SDave Chinner 	xfs_ilock(tdp, XFS_IOLOCK_EXCL | XFS_IOLOCK_PARENT);
1468c24b5dfaSDave Chinner 	xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
1469c24b5dfaSDave Chinner 
1470c24b5dfaSDave Chinner 	xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
1471dbad7c99SDave Chinner 	xfs_trans_ijoin(tp, tdp, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1472c24b5dfaSDave Chinner 
1473c24b5dfaSDave Chinner 	/*
1474c24b5dfaSDave Chinner 	 * If we are using project inheritance, we only allow hard link
1475c24b5dfaSDave Chinner 	 * creation in our tree when the project IDs are the same; else
1476c24b5dfaSDave Chinner 	 * the tree quota mechanism could be circumvented.
1477c24b5dfaSDave Chinner 	 */
1478c24b5dfaSDave Chinner 	if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
1479c24b5dfaSDave Chinner 		     (xfs_get_projid(tdp) != xfs_get_projid(sip)))) {
14802451337dSDave Chinner 		error = -EXDEV;
1481c24b5dfaSDave Chinner 		goto error_return;
1482c24b5dfaSDave Chinner 	}
1483c24b5dfaSDave Chinner 
148494f3cad5SEric Sandeen 	if (!resblks) {
148594f3cad5SEric Sandeen 		error = xfs_dir_canenter(tp, tdp, target_name);
1486c24b5dfaSDave Chinner 		if (error)
1487c24b5dfaSDave Chinner 			goto error_return;
148894f3cad5SEric Sandeen 	}
1489c24b5dfaSDave Chinner 
14902c3234d1SDarrick J. Wong 	xfs_defer_init(&dfops, &first_block);
1491c24b5dfaSDave Chinner 
149254d7b5c1SDave Chinner 	/*
149354d7b5c1SDave Chinner 	 * Handle initial link state of O_TMPFILE inode
149454d7b5c1SDave Chinner 	 */
149554d7b5c1SDave Chinner 	if (VFS_I(sip)->i_nlink == 0) {
1496ab297431SZhi Yong Wu 		error = xfs_iunlink_remove(tp, sip);
1497ab297431SZhi Yong Wu 		if (error)
14984906e215SChristoph Hellwig 			goto error_return;
1499ab297431SZhi Yong Wu 	}
1500ab297431SZhi Yong Wu 
1501c24b5dfaSDave Chinner 	error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
15022c3234d1SDarrick J. Wong 					&first_block, &dfops, resblks);
1503c24b5dfaSDave Chinner 	if (error)
15044906e215SChristoph Hellwig 		goto error_return;
1505c24b5dfaSDave Chinner 	xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1506c24b5dfaSDave Chinner 	xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
1507c24b5dfaSDave Chinner 
1508c24b5dfaSDave Chinner 	error = xfs_bumplink(tp, sip);
1509c24b5dfaSDave Chinner 	if (error)
15104906e215SChristoph Hellwig 		goto error_return;
1511c24b5dfaSDave Chinner 
1512c24b5dfaSDave Chinner 	/*
1513c24b5dfaSDave Chinner 	 * If this is a synchronous mount, make sure that the
1514c24b5dfaSDave Chinner 	 * link transaction goes to disk before returning to
1515c24b5dfaSDave Chinner 	 * the user.
1516c24b5dfaSDave Chinner 	 */
1517f6106efaSEric Sandeen 	if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1518c24b5dfaSDave Chinner 		xfs_trans_set_sync(tp);
1519c24b5dfaSDave Chinner 
15202c3234d1SDarrick J. Wong 	error = xfs_defer_finish(&tp, &dfops, NULL);
1521c24b5dfaSDave Chinner 	if (error) {
15222c3234d1SDarrick J. Wong 		xfs_defer_cancel(&dfops);
15234906e215SChristoph Hellwig 		goto error_return;
1524c24b5dfaSDave Chinner 	}
1525c24b5dfaSDave Chinner 
152670393313SChristoph Hellwig 	return xfs_trans_commit(tp);
1527c24b5dfaSDave Chinner 
1528c24b5dfaSDave Chinner  error_return:
15294906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
1530c24b5dfaSDave Chinner  std_return:
1531c24b5dfaSDave Chinner 	return error;
1532c24b5dfaSDave Chinner }
1533c24b5dfaSDave Chinner 
15341da177e4SLinus Torvalds /*
15358f04c47aSChristoph Hellwig  * Free up the underlying blocks past new_size.  The new size must be smaller
15368f04c47aSChristoph Hellwig  * than the current size.  This routine can be used both for the attribute and
15378f04c47aSChristoph Hellwig  * data fork, and does not modify the inode size, which is left to the caller.
15381da177e4SLinus Torvalds  *
1539f6485057SDavid Chinner  * The transaction passed to this routine must have made a permanent log
1540f6485057SDavid Chinner  * reservation of at least XFS_ITRUNCATE_LOG_RES.  This routine may commit the
1541f6485057SDavid Chinner  * given transaction and start new ones, so make sure everything involved in
1542f6485057SDavid Chinner  * the transaction is tidy before calling here.  Some transaction will be
1543f6485057SDavid Chinner  * returned to the caller to be committed.  The incoming transaction must
1544f6485057SDavid Chinner  * already include the inode, and both inode locks must be held exclusively.
1545f6485057SDavid Chinner  * The inode must also be "held" within the transaction.  On return the inode
1546f6485057SDavid Chinner  * will be "held" within the returned transaction.  This routine does NOT
1547f6485057SDavid Chinner  * require any disk space to be reserved for it within the transaction.
15481da177e4SLinus Torvalds  *
1549f6485057SDavid Chinner  * If we get an error, we must return with the inode locked and linked into the
1550f6485057SDavid Chinner  * current transaction. This keeps things simple for the higher level code,
1551f6485057SDavid Chinner  * because it always knows that the inode is locked and held in the transaction
1552f6485057SDavid Chinner  * that returns to it whether errors occur or not.  We don't mark the inode
1553f6485057SDavid Chinner  * dirty on error so that transactions can be easily aborted if possible.
15541da177e4SLinus Torvalds  */
15551da177e4SLinus Torvalds int
15568f04c47aSChristoph Hellwig xfs_itruncate_extents(
15578f04c47aSChristoph Hellwig 	struct xfs_trans	**tpp,
15588f04c47aSChristoph Hellwig 	struct xfs_inode	*ip,
15598f04c47aSChristoph Hellwig 	int			whichfork,
15608f04c47aSChristoph Hellwig 	xfs_fsize_t		new_size)
15611da177e4SLinus Torvalds {
15628f04c47aSChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
15638f04c47aSChristoph Hellwig 	struct xfs_trans	*tp = *tpp;
15642c3234d1SDarrick J. Wong 	struct xfs_defer_ops	dfops;
15651da177e4SLinus Torvalds 	xfs_fsblock_t		first_block;
15661da177e4SLinus Torvalds 	xfs_fileoff_t		first_unmap_block;
15671da177e4SLinus Torvalds 	xfs_fileoff_t		last_block;
15688f04c47aSChristoph Hellwig 	xfs_filblks_t		unmap_len;
15698f04c47aSChristoph Hellwig 	int			error = 0;
15708f04c47aSChristoph Hellwig 	int			done = 0;
15711da177e4SLinus Torvalds 
15720b56185bSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
15730b56185bSChristoph Hellwig 	ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
15740b56185bSChristoph Hellwig 	       xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1575ce7ae151SChristoph Hellwig 	ASSERT(new_size <= XFS_ISIZE(ip));
15768f04c47aSChristoph Hellwig 	ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
15771da177e4SLinus Torvalds 	ASSERT(ip->i_itemp != NULL);
1578898621d5SChristoph Hellwig 	ASSERT(ip->i_itemp->ili_lock_flags == 0);
15791da177e4SLinus Torvalds 	ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
15801da177e4SLinus Torvalds 
1581673e8e59SChristoph Hellwig 	trace_xfs_itruncate_extents_start(ip, new_size);
1582673e8e59SChristoph Hellwig 
15831da177e4SLinus Torvalds 	/*
15841da177e4SLinus Torvalds 	 * Since it is possible for space to become allocated beyond
15851da177e4SLinus Torvalds 	 * the end of the file (in a crash where the space is allocated
15861da177e4SLinus Torvalds 	 * but the inode size is not yet updated), simply remove any
15871da177e4SLinus Torvalds 	 * blocks which show up between the new EOF and the maximum
15881da177e4SLinus Torvalds 	 * possible file size.  If the first block to be removed is
15891da177e4SLinus Torvalds 	 * beyond the maximum file size (ie it is the same as last_block),
15901da177e4SLinus Torvalds 	 * then there is nothing to do.
15911da177e4SLinus Torvalds 	 */
15928f04c47aSChristoph Hellwig 	first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
159332972383SDave Chinner 	last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
15948f04c47aSChristoph Hellwig 	if (first_unmap_block == last_block)
15958f04c47aSChristoph Hellwig 		return 0;
15968f04c47aSChristoph Hellwig 
15978f04c47aSChristoph Hellwig 	ASSERT(first_unmap_block < last_block);
15981da177e4SLinus Torvalds 	unmap_len = last_block - first_unmap_block + 1;
15991da177e4SLinus Torvalds 	while (!done) {
16002c3234d1SDarrick J. Wong 		xfs_defer_init(&dfops, &first_block);
16018f04c47aSChristoph Hellwig 		error = xfs_bunmapi(tp, ip,
16023e57ecf6SOlaf Weber 				    first_unmap_block, unmap_len,
16038f04c47aSChristoph Hellwig 				    xfs_bmapi_aflag(whichfork),
16041da177e4SLinus Torvalds 				    XFS_ITRUNC_MAX_EXTENTS,
16052c3234d1SDarrick J. Wong 				    &first_block, &dfops,
1606b4e9181eSChristoph Hellwig 				    &done);
16078f04c47aSChristoph Hellwig 		if (error)
16088f04c47aSChristoph Hellwig 			goto out_bmap_cancel;
16091da177e4SLinus Torvalds 
16101da177e4SLinus Torvalds 		/*
16111da177e4SLinus Torvalds 		 * Duplicate the transaction that has the permanent
16121da177e4SLinus Torvalds 		 * reservation and commit the old transaction.
16131da177e4SLinus Torvalds 		 */
16142c3234d1SDarrick J. Wong 		error = xfs_defer_finish(&tp, &dfops, ip);
16158f04c47aSChristoph Hellwig 		if (error)
16168f04c47aSChristoph Hellwig 			goto out_bmap_cancel;
16171da177e4SLinus Torvalds 
16182e6db6c4SChristoph Hellwig 		error = xfs_trans_roll(&tp, ip);
16191da177e4SLinus Torvalds 		if (error)
16208f04c47aSChristoph Hellwig 			goto out;
16211da177e4SLinus Torvalds 	}
16228f04c47aSChristoph Hellwig 
1623aa8968f2SDarrick J. Wong 	/* Remove all pending CoW reservations. */
1624aa8968f2SDarrick J. Wong 	error = xfs_reflink_cancel_cow_blocks(ip, &tp, first_unmap_block,
1625aa8968f2SDarrick J. Wong 			last_block);
1626aa8968f2SDarrick J. Wong 	if (error)
1627aa8968f2SDarrick J. Wong 		goto out;
1628aa8968f2SDarrick J. Wong 
1629aa8968f2SDarrick J. Wong 	/*
1630aa8968f2SDarrick J. Wong 	 * Clear the reflink flag if we truncated everything.
1631aa8968f2SDarrick J. Wong 	 */
1632aa8968f2SDarrick J. Wong 	if (ip->i_d.di_nblocks == 0 && xfs_is_reflink_inode(ip))
1633aa8968f2SDarrick J. Wong 		ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
1634aa8968f2SDarrick J. Wong 
1635673e8e59SChristoph Hellwig 	/*
1636673e8e59SChristoph Hellwig 	 * Always re-log the inode so that our permanent transaction can keep
1637673e8e59SChristoph Hellwig 	 * on rolling it forward in the log.
1638673e8e59SChristoph Hellwig 	 */
1639673e8e59SChristoph Hellwig 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1640673e8e59SChristoph Hellwig 
1641673e8e59SChristoph Hellwig 	trace_xfs_itruncate_extents_end(ip, new_size);
1642673e8e59SChristoph Hellwig 
16438f04c47aSChristoph Hellwig out:
16448f04c47aSChristoph Hellwig 	*tpp = tp;
16458f04c47aSChristoph Hellwig 	return error;
16468f04c47aSChristoph Hellwig out_bmap_cancel:
16471da177e4SLinus Torvalds 	/*
16488f04c47aSChristoph Hellwig 	 * If the bunmapi call encounters an error, return to the caller where
16498f04c47aSChristoph Hellwig 	 * the transaction can be properly aborted.  We just need to make sure
16508f04c47aSChristoph Hellwig 	 * we're not holding any resources that we were not when we came in.
16511da177e4SLinus Torvalds 	 */
16522c3234d1SDarrick J. Wong 	xfs_defer_cancel(&dfops);
16538f04c47aSChristoph Hellwig 	goto out;
16548f04c47aSChristoph Hellwig }
16558f04c47aSChristoph Hellwig 
1656c24b5dfaSDave Chinner int
1657c24b5dfaSDave Chinner xfs_release(
1658c24b5dfaSDave Chinner 	xfs_inode_t	*ip)
1659c24b5dfaSDave Chinner {
1660c24b5dfaSDave Chinner 	xfs_mount_t	*mp = ip->i_mount;
1661c24b5dfaSDave Chinner 	int		error;
1662c24b5dfaSDave Chinner 
1663c19b3b05SDave Chinner 	if (!S_ISREG(VFS_I(ip)->i_mode) || (VFS_I(ip)->i_mode == 0))
1664c24b5dfaSDave Chinner 		return 0;
1665c24b5dfaSDave Chinner 
1666c24b5dfaSDave Chinner 	/* If this is a read-only mount, don't do this (would generate I/O) */
1667c24b5dfaSDave Chinner 	if (mp->m_flags & XFS_MOUNT_RDONLY)
1668c24b5dfaSDave Chinner 		return 0;
1669c24b5dfaSDave Chinner 
1670c24b5dfaSDave Chinner 	if (!XFS_FORCED_SHUTDOWN(mp)) {
1671c24b5dfaSDave Chinner 		int truncated;
1672c24b5dfaSDave Chinner 
1673c24b5dfaSDave Chinner 		/*
1674c24b5dfaSDave Chinner 		 * If we previously truncated this file and removed old data
1675c24b5dfaSDave Chinner 		 * in the process, we want to initiate "early" writeout on
1676c24b5dfaSDave Chinner 		 * the last close.  This is an attempt to combat the notorious
1677c24b5dfaSDave Chinner 		 * NULL files problem which is particularly noticeable from a
1678c24b5dfaSDave Chinner 		 * truncate down, buffered (re-)write (delalloc), followed by
1679c24b5dfaSDave Chinner 		 * a crash.  What we are effectively doing here is
1680c24b5dfaSDave Chinner 		 * significantly reducing the time window where we'd otherwise
1681c24b5dfaSDave Chinner 		 * be exposed to that problem.
1682c24b5dfaSDave Chinner 		 */
1683c24b5dfaSDave Chinner 		truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
1684c24b5dfaSDave Chinner 		if (truncated) {
1685c24b5dfaSDave Chinner 			xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
1686eac152b4SDave Chinner 			if (ip->i_delayed_blks > 0) {
16872451337dSDave Chinner 				error = filemap_flush(VFS_I(ip)->i_mapping);
1688c24b5dfaSDave Chinner 				if (error)
1689c24b5dfaSDave Chinner 					return error;
1690c24b5dfaSDave Chinner 			}
1691c24b5dfaSDave Chinner 		}
1692c24b5dfaSDave Chinner 	}
1693c24b5dfaSDave Chinner 
169454d7b5c1SDave Chinner 	if (VFS_I(ip)->i_nlink == 0)
1695c24b5dfaSDave Chinner 		return 0;
1696c24b5dfaSDave Chinner 
1697c24b5dfaSDave Chinner 	if (xfs_can_free_eofblocks(ip, false)) {
1698c24b5dfaSDave Chinner 
1699c24b5dfaSDave Chinner 		/*
1700c24b5dfaSDave Chinner 		 * If we can't get the iolock just skip truncating the blocks
1701c24b5dfaSDave Chinner 		 * past EOF because we could deadlock with the mmap_sem
1702c24b5dfaSDave Chinner 		 * otherwise.  We'll get another chance to drop them once the
1703c24b5dfaSDave Chinner 		 * last reference to the inode is dropped, so we'll never leak
1704c24b5dfaSDave Chinner 		 * blocks permanently.
1705c24b5dfaSDave Chinner 		 *
1706c24b5dfaSDave Chinner 		 * Further, check if the inode is being opened, written and
1707c24b5dfaSDave Chinner 		 * closed frequently and we have delayed allocation blocks
1708c24b5dfaSDave Chinner 		 * outstanding (e.g. streaming writes from the NFS server),
1709c24b5dfaSDave Chinner 		 * truncating the blocks past EOF will cause fragmentation to
1710c24b5dfaSDave Chinner 		 * occur.
1711c24b5dfaSDave Chinner 		 *
1712c24b5dfaSDave Chinner 		 * In this case don't do the truncation, either, but we have to
1713c24b5dfaSDave Chinner 		 * be careful how we detect this case. Blocks beyond EOF show
1714c24b5dfaSDave Chinner 		 * up as i_delayed_blks even when the inode is clean, so we
1715c24b5dfaSDave Chinner 		 * need to truncate them away first before checking for a dirty
1716c24b5dfaSDave Chinner 		 * release. Hence on the first dirty close we will still remove
1717c24b5dfaSDave Chinner 		 * the speculative allocation, but after that we will leave it
1718c24b5dfaSDave Chinner 		 * in place.
1719c24b5dfaSDave Chinner 		 */
1720c24b5dfaSDave Chinner 		if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
1721c24b5dfaSDave Chinner 			return 0;
1722c24b5dfaSDave Chinner 
1723c24b5dfaSDave Chinner 		error = xfs_free_eofblocks(mp, ip, true);
17242451337dSDave Chinner 		if (error && error != -EAGAIN)
1725c24b5dfaSDave Chinner 			return error;
1726c24b5dfaSDave Chinner 
1727c24b5dfaSDave Chinner 		/* delalloc blocks after truncation means it really is dirty */
1728c24b5dfaSDave Chinner 		if (ip->i_delayed_blks)
1729c24b5dfaSDave Chinner 			xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
1730c24b5dfaSDave Chinner 	}
1731c24b5dfaSDave Chinner 	return 0;
1732c24b5dfaSDave Chinner }
1733c24b5dfaSDave Chinner 
1734c24b5dfaSDave Chinner /*
1735f7be2d7fSBrian Foster  * xfs_inactive_truncate
1736f7be2d7fSBrian Foster  *
1737f7be2d7fSBrian Foster  * Called to perform a truncate when an inode becomes unlinked.
1738f7be2d7fSBrian Foster  */
1739f7be2d7fSBrian Foster STATIC int
1740f7be2d7fSBrian Foster xfs_inactive_truncate(
1741f7be2d7fSBrian Foster 	struct xfs_inode *ip)
1742f7be2d7fSBrian Foster {
1743f7be2d7fSBrian Foster 	struct xfs_mount	*mp = ip->i_mount;
1744f7be2d7fSBrian Foster 	struct xfs_trans	*tp;
1745f7be2d7fSBrian Foster 	int			error;
1746f7be2d7fSBrian Foster 
1747253f4911SChristoph Hellwig 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
1748f7be2d7fSBrian Foster 	if (error) {
1749f7be2d7fSBrian Foster 		ASSERT(XFS_FORCED_SHUTDOWN(mp));
1750f7be2d7fSBrian Foster 		return error;
1751f7be2d7fSBrian Foster 	}
1752f7be2d7fSBrian Foster 
1753f7be2d7fSBrian Foster 	xfs_ilock(ip, XFS_ILOCK_EXCL);
1754f7be2d7fSBrian Foster 	xfs_trans_ijoin(tp, ip, 0);
1755f7be2d7fSBrian Foster 
1756f7be2d7fSBrian Foster 	/*
1757f7be2d7fSBrian Foster 	 * Log the inode size first to prevent stale data exposure in the event
1758f7be2d7fSBrian Foster 	 * of a system crash before the truncate completes. See the related
1759f7be2d7fSBrian Foster 	 * comment in xfs_setattr_size() for details.
1760f7be2d7fSBrian Foster 	 */
1761f7be2d7fSBrian Foster 	ip->i_d.di_size = 0;
1762f7be2d7fSBrian Foster 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1763f7be2d7fSBrian Foster 
1764f7be2d7fSBrian Foster 	error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
1765f7be2d7fSBrian Foster 	if (error)
1766f7be2d7fSBrian Foster 		goto error_trans_cancel;
1767f7be2d7fSBrian Foster 
1768f7be2d7fSBrian Foster 	ASSERT(ip->i_d.di_nextents == 0);
1769f7be2d7fSBrian Foster 
177070393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
1771f7be2d7fSBrian Foster 	if (error)
1772f7be2d7fSBrian Foster 		goto error_unlock;
1773f7be2d7fSBrian Foster 
1774f7be2d7fSBrian Foster 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
1775f7be2d7fSBrian Foster 	return 0;
1776f7be2d7fSBrian Foster 
1777f7be2d7fSBrian Foster error_trans_cancel:
17784906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
1779f7be2d7fSBrian Foster error_unlock:
1780f7be2d7fSBrian Foster 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
1781f7be2d7fSBrian Foster 	return error;
1782f7be2d7fSBrian Foster }
1783f7be2d7fSBrian Foster 
1784f7be2d7fSBrian Foster /*
178588877d2bSBrian Foster  * xfs_inactive_ifree()
178688877d2bSBrian Foster  *
178788877d2bSBrian Foster  * Perform the inode free when an inode is unlinked.
178888877d2bSBrian Foster  */
178988877d2bSBrian Foster STATIC int
179088877d2bSBrian Foster xfs_inactive_ifree(
179188877d2bSBrian Foster 	struct xfs_inode *ip)
179288877d2bSBrian Foster {
17932c3234d1SDarrick J. Wong 	struct xfs_defer_ops	dfops;
179488877d2bSBrian Foster 	xfs_fsblock_t		first_block;
179588877d2bSBrian Foster 	struct xfs_mount	*mp = ip->i_mount;
179688877d2bSBrian Foster 	struct xfs_trans	*tp;
179788877d2bSBrian Foster 	int			error;
179888877d2bSBrian Foster 
17999d43b180SBrian Foster 	/*
18009d43b180SBrian Foster 	 * The ifree transaction might need to allocate blocks for record
18019d43b180SBrian Foster 	 * insertion to the finobt. We don't want to fail here at ENOSPC, so
18029d43b180SBrian Foster 	 * allow ifree to dip into the reserved block pool if necessary.
18039d43b180SBrian Foster 	 *
18049d43b180SBrian Foster 	 * Freeing large sets of inodes generally means freeing inode chunks,
18059d43b180SBrian Foster 	 * directory and file data blocks, so this should be relatively safe.
18069d43b180SBrian Foster 	 * Only under severe circumstances should it be possible to free enough
18079d43b180SBrian Foster 	 * inodes to exhaust the reserve block pool via finobt expansion while
18089d43b180SBrian Foster 	 * at the same time not creating free space in the filesystem.
18099d43b180SBrian Foster 	 *
18109d43b180SBrian Foster 	 * Send a warning if the reservation does happen to fail, as the inode
18119d43b180SBrian Foster 	 * now remains allocated and sits on the unlinked list until the fs is
18129d43b180SBrian Foster 	 * repaired.
18139d43b180SBrian Foster 	 */
1814253f4911SChristoph Hellwig 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ifree,
1815253f4911SChristoph Hellwig 			XFS_IFREE_SPACE_RES(mp), 0, XFS_TRANS_RESERVE, &tp);
181688877d2bSBrian Foster 	if (error) {
18172451337dSDave Chinner 		if (error == -ENOSPC) {
18189d43b180SBrian Foster 			xfs_warn_ratelimited(mp,
18199d43b180SBrian Foster 			"Failed to remove inode(s) from unlinked list. "
18209d43b180SBrian Foster 			"Please free space, unmount and run xfs_repair.");
18219d43b180SBrian Foster 		} else {
182288877d2bSBrian Foster 			ASSERT(XFS_FORCED_SHUTDOWN(mp));
18239d43b180SBrian Foster 		}
182488877d2bSBrian Foster 		return error;
182588877d2bSBrian Foster 	}
182688877d2bSBrian Foster 
182788877d2bSBrian Foster 	xfs_ilock(ip, XFS_ILOCK_EXCL);
182888877d2bSBrian Foster 	xfs_trans_ijoin(tp, ip, 0);
182988877d2bSBrian Foster 
18302c3234d1SDarrick J. Wong 	xfs_defer_init(&dfops, &first_block);
18312c3234d1SDarrick J. Wong 	error = xfs_ifree(tp, ip, &dfops);
183288877d2bSBrian Foster 	if (error) {
183388877d2bSBrian Foster 		/*
183488877d2bSBrian Foster 		 * If we fail to free the inode, shut down.  The cancel
183588877d2bSBrian Foster 		 * might do that, we need to make sure.  Otherwise the
183688877d2bSBrian Foster 		 * inode might be lost for a long time or forever.
183788877d2bSBrian Foster 		 */
183888877d2bSBrian Foster 		if (!XFS_FORCED_SHUTDOWN(mp)) {
183988877d2bSBrian Foster 			xfs_notice(mp, "%s: xfs_ifree returned error %d",
184088877d2bSBrian Foster 				__func__, error);
184188877d2bSBrian Foster 			xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
184288877d2bSBrian Foster 		}
18434906e215SChristoph Hellwig 		xfs_trans_cancel(tp);
184488877d2bSBrian Foster 		xfs_iunlock(ip, XFS_ILOCK_EXCL);
184588877d2bSBrian Foster 		return error;
184688877d2bSBrian Foster 	}
184788877d2bSBrian Foster 
184888877d2bSBrian Foster 	/*
184988877d2bSBrian Foster 	 * Credit the quota account(s). The inode is gone.
185088877d2bSBrian Foster 	 */
185188877d2bSBrian Foster 	xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
185288877d2bSBrian Foster 
185388877d2bSBrian Foster 	/*
1854d4a97a04SBrian Foster 	 * Just ignore errors at this point.  There is nothing we can do except
1855d4a97a04SBrian Foster 	 * to try to keep going. Make sure it's not a silent error.
185688877d2bSBrian Foster 	 */
18572c3234d1SDarrick J. Wong 	error = xfs_defer_finish(&tp, &dfops, NULL);
1858d4a97a04SBrian Foster 	if (error) {
1859310a75a3SDarrick J. Wong 		xfs_notice(mp, "%s: xfs_defer_finish returned error %d",
186088877d2bSBrian Foster 			__func__, error);
18612c3234d1SDarrick J. Wong 		xfs_defer_cancel(&dfops);
1862d4a97a04SBrian Foster 	}
186370393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
186488877d2bSBrian Foster 	if (error)
186588877d2bSBrian Foster 		xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
186688877d2bSBrian Foster 			__func__, error);
186788877d2bSBrian Foster 
186888877d2bSBrian Foster 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
186988877d2bSBrian Foster 	return 0;
187088877d2bSBrian Foster }
187188877d2bSBrian Foster 
187288877d2bSBrian Foster /*
1873c24b5dfaSDave Chinner  * xfs_inactive
1874c24b5dfaSDave Chinner  *
1875c24b5dfaSDave Chinner  * This is called when the vnode reference count for the vnode
1876c24b5dfaSDave Chinner  * goes to zero.  If the file has been unlinked, then it must
1877c24b5dfaSDave Chinner  * now be truncated.  Also, we clear all of the read-ahead state
1878c24b5dfaSDave Chinner  * kept for the inode here since the file is now closed.
1879c24b5dfaSDave Chinner  */
188074564fb4SBrian Foster void
1881c24b5dfaSDave Chinner xfs_inactive(
1882c24b5dfaSDave Chinner 	xfs_inode_t	*ip)
1883c24b5dfaSDave Chinner {
18843d3c8b52SJie Liu 	struct xfs_mount	*mp;
1885c24b5dfaSDave Chinner 	int			error;
1886c24b5dfaSDave Chinner 	int			truncate = 0;
1887c24b5dfaSDave Chinner 
1888c24b5dfaSDave Chinner 	/*
1889c24b5dfaSDave Chinner 	 * If the inode is already free, then there can be nothing
1890c24b5dfaSDave Chinner 	 * to clean up here.
1891c24b5dfaSDave Chinner 	 */
1892c19b3b05SDave Chinner 	if (VFS_I(ip)->i_mode == 0) {
1893c24b5dfaSDave Chinner 		ASSERT(ip->i_df.if_real_bytes == 0);
1894c24b5dfaSDave Chinner 		ASSERT(ip->i_df.if_broot_bytes == 0);
189574564fb4SBrian Foster 		return;
1896c24b5dfaSDave Chinner 	}
1897c24b5dfaSDave Chinner 
1898c24b5dfaSDave Chinner 	mp = ip->i_mount;
189917c12bcdSDarrick J. Wong 	ASSERT(!xfs_iflags_test(ip, XFS_IRECOVERY));
1900c24b5dfaSDave Chinner 
1901c24b5dfaSDave Chinner 	/* If this is a read-only mount, don't do this (would generate I/O) */
1902c24b5dfaSDave Chinner 	if (mp->m_flags & XFS_MOUNT_RDONLY)
190374564fb4SBrian Foster 		return;
1904c24b5dfaSDave Chinner 
190554d7b5c1SDave Chinner 	if (VFS_I(ip)->i_nlink != 0) {
1906c24b5dfaSDave Chinner 		/*
1907c24b5dfaSDave Chinner 		 * force is true because we are evicting an inode from the
1908c24b5dfaSDave Chinner 		 * cache. Post-eof blocks must be freed, lest we end up with
1909c24b5dfaSDave Chinner 		 * broken free space accounting.
1910c24b5dfaSDave Chinner 		 */
191174564fb4SBrian Foster 		if (xfs_can_free_eofblocks(ip, true))
191274564fb4SBrian Foster 			xfs_free_eofblocks(mp, ip, false);
191374564fb4SBrian Foster 
191474564fb4SBrian Foster 		return;
1915c24b5dfaSDave Chinner 	}
1916c24b5dfaSDave Chinner 
1917c19b3b05SDave Chinner 	if (S_ISREG(VFS_I(ip)->i_mode) &&
1918c24b5dfaSDave Chinner 	    (ip->i_d.di_size != 0 || XFS_ISIZE(ip) != 0 ||
1919c24b5dfaSDave Chinner 	     ip->i_d.di_nextents > 0 || ip->i_delayed_blks > 0))
1920c24b5dfaSDave Chinner 		truncate = 1;
1921c24b5dfaSDave Chinner 
1922c24b5dfaSDave Chinner 	error = xfs_qm_dqattach(ip, 0);
1923c24b5dfaSDave Chinner 	if (error)
192474564fb4SBrian Foster 		return;
1925c24b5dfaSDave Chinner 
1926c19b3b05SDave Chinner 	if (S_ISLNK(VFS_I(ip)->i_mode))
192736b21ddeSBrian Foster 		error = xfs_inactive_symlink(ip);
1928f7be2d7fSBrian Foster 	else if (truncate)
1929f7be2d7fSBrian Foster 		error = xfs_inactive_truncate(ip);
193036b21ddeSBrian Foster 	if (error)
193174564fb4SBrian Foster 		return;
1932c24b5dfaSDave Chinner 
1933c24b5dfaSDave Chinner 	/*
1934c24b5dfaSDave Chinner 	 * If there are attributes associated with the file then blow them away
1935c24b5dfaSDave Chinner 	 * now.  The code calls a routine that recursively deconstructs the
19366dfe5a04SDave Chinner 	 * attribute fork. If also blows away the in-core attribute fork.
1937c24b5dfaSDave Chinner 	 */
19386dfe5a04SDave Chinner 	if (XFS_IFORK_Q(ip)) {
1939c24b5dfaSDave Chinner 		error = xfs_attr_inactive(ip);
1940c24b5dfaSDave Chinner 		if (error)
194174564fb4SBrian Foster 			return;
1942c24b5dfaSDave Chinner 	}
1943c24b5dfaSDave Chinner 
19446dfe5a04SDave Chinner 	ASSERT(!ip->i_afp);
1945c24b5dfaSDave Chinner 	ASSERT(ip->i_d.di_anextents == 0);
19466dfe5a04SDave Chinner 	ASSERT(ip->i_d.di_forkoff == 0);
1947c24b5dfaSDave Chinner 
1948c24b5dfaSDave Chinner 	/*
1949c24b5dfaSDave Chinner 	 * Free the inode.
1950c24b5dfaSDave Chinner 	 */
195188877d2bSBrian Foster 	error = xfs_inactive_ifree(ip);
1952c24b5dfaSDave Chinner 	if (error)
195374564fb4SBrian Foster 		return;
1954c24b5dfaSDave Chinner 
1955c24b5dfaSDave Chinner 	/*
1956c24b5dfaSDave Chinner 	 * Release the dquots held by inode, if any.
1957c24b5dfaSDave Chinner 	 */
1958c24b5dfaSDave Chinner 	xfs_qm_dqdetach(ip);
1959c24b5dfaSDave Chinner }
1960c24b5dfaSDave Chinner 
19611da177e4SLinus Torvalds /*
196254d7b5c1SDave Chinner  * This is called when the inode's link count goes to 0 or we are creating a
196354d7b5c1SDave Chinner  * tmpfile via O_TMPFILE. In the case of a tmpfile, @ignore_linkcount will be
196454d7b5c1SDave Chinner  * set to true as the link count is dropped to zero by the VFS after we've
196554d7b5c1SDave Chinner  * created the file successfully, so we have to add it to the unlinked list
196654d7b5c1SDave Chinner  * while the link count is non-zero.
196754d7b5c1SDave Chinner  *
196854d7b5c1SDave Chinner  * We place the on-disk inode on a list in the AGI.  It will be pulled from this
196954d7b5c1SDave Chinner  * list when the inode is freed.
19701da177e4SLinus Torvalds  */
197154d7b5c1SDave Chinner STATIC int
19721da177e4SLinus Torvalds xfs_iunlink(
197354d7b5c1SDave Chinner 	struct xfs_trans *tp,
197454d7b5c1SDave Chinner 	struct xfs_inode *ip)
19751da177e4SLinus Torvalds {
197654d7b5c1SDave Chinner 	xfs_mount_t	*mp = tp->t_mountp;
19771da177e4SLinus Torvalds 	xfs_agi_t	*agi;
19781da177e4SLinus Torvalds 	xfs_dinode_t	*dip;
19791da177e4SLinus Torvalds 	xfs_buf_t	*agibp;
19801da177e4SLinus Torvalds 	xfs_buf_t	*ibp;
19811da177e4SLinus Torvalds 	xfs_agino_t	agino;
19821da177e4SLinus Torvalds 	short		bucket_index;
19831da177e4SLinus Torvalds 	int		offset;
19841da177e4SLinus Torvalds 	int		error;
19851da177e4SLinus Torvalds 
1986c19b3b05SDave Chinner 	ASSERT(VFS_I(ip)->i_mode != 0);
19871da177e4SLinus Torvalds 
19881da177e4SLinus Torvalds 	/*
19891da177e4SLinus Torvalds 	 * Get the agi buffer first.  It ensures lock ordering
19901da177e4SLinus Torvalds 	 * on the list.
19911da177e4SLinus Torvalds 	 */
19925e1be0fbSChristoph Hellwig 	error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
1993859d7182SVlad Apostolov 	if (error)
19941da177e4SLinus Torvalds 		return error;
19951da177e4SLinus Torvalds 	agi = XFS_BUF_TO_AGI(agibp);
19965e1be0fbSChristoph Hellwig 
19971da177e4SLinus Torvalds 	/*
19981da177e4SLinus Torvalds 	 * Get the index into the agi hash table for the
19991da177e4SLinus Torvalds 	 * list this inode will go on.
20001da177e4SLinus Torvalds 	 */
20011da177e4SLinus Torvalds 	agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
20021da177e4SLinus Torvalds 	ASSERT(agino != 0);
20031da177e4SLinus Torvalds 	bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
20041da177e4SLinus Torvalds 	ASSERT(agi->agi_unlinked[bucket_index]);
200516259e7dSChristoph Hellwig 	ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
20061da177e4SLinus Torvalds 
200769ef921bSChristoph Hellwig 	if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
20081da177e4SLinus Torvalds 		/*
20091da177e4SLinus Torvalds 		 * There is already another inode in the bucket we need
20101da177e4SLinus Torvalds 		 * to add ourselves to.  Add us at the front of the list.
20111da177e4SLinus Torvalds 		 * Here we put the head pointer into our next pointer,
20121da177e4SLinus Torvalds 		 * and then we fall through to point the head at us.
20131da177e4SLinus Torvalds 		 */
2014475ee413SChristoph Hellwig 		error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
2015475ee413SChristoph Hellwig 				       0, 0);
2016c319b58bSVlad Apostolov 		if (error)
2017c319b58bSVlad Apostolov 			return error;
2018c319b58bSVlad Apostolov 
201969ef921bSChristoph Hellwig 		ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
20201da177e4SLinus Torvalds 		dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
202192bfc6e7SChristoph Hellwig 		offset = ip->i_imap.im_boffset +
20221da177e4SLinus Torvalds 			offsetof(xfs_dinode_t, di_next_unlinked);
20230a32c26eSDave Chinner 
20240a32c26eSDave Chinner 		/* need to recalc the inode CRC if appropriate */
20250a32c26eSDave Chinner 		xfs_dinode_calc_crc(mp, dip);
20260a32c26eSDave Chinner 
20271da177e4SLinus Torvalds 		xfs_trans_inode_buf(tp, ibp);
20281da177e4SLinus Torvalds 		xfs_trans_log_buf(tp, ibp, offset,
20291da177e4SLinus Torvalds 				  (offset + sizeof(xfs_agino_t) - 1));
20301da177e4SLinus Torvalds 		xfs_inobp_check(mp, ibp);
20311da177e4SLinus Torvalds 	}
20321da177e4SLinus Torvalds 
20331da177e4SLinus Torvalds 	/*
20341da177e4SLinus Torvalds 	 * Point the bucket head pointer at the inode being inserted.
20351da177e4SLinus Torvalds 	 */
20361da177e4SLinus Torvalds 	ASSERT(agino != 0);
203716259e7dSChristoph Hellwig 	agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
20381da177e4SLinus Torvalds 	offset = offsetof(xfs_agi_t, agi_unlinked) +
20391da177e4SLinus Torvalds 		(sizeof(xfs_agino_t) * bucket_index);
2040f19b872bSDave Chinner 	xfs_trans_buf_set_type(tp, agibp, XFS_BLFT_AGI_BUF);
20411da177e4SLinus Torvalds 	xfs_trans_log_buf(tp, agibp, offset,
20421da177e4SLinus Torvalds 			  (offset + sizeof(xfs_agino_t) - 1));
20431da177e4SLinus Torvalds 	return 0;
20441da177e4SLinus Torvalds }
20451da177e4SLinus Torvalds 
20461da177e4SLinus Torvalds /*
20471da177e4SLinus Torvalds  * Pull the on-disk inode from the AGI unlinked list.
20481da177e4SLinus Torvalds  */
20491da177e4SLinus Torvalds STATIC int
20501da177e4SLinus Torvalds xfs_iunlink_remove(
20511da177e4SLinus Torvalds 	xfs_trans_t	*tp,
20521da177e4SLinus Torvalds 	xfs_inode_t	*ip)
20531da177e4SLinus Torvalds {
20541da177e4SLinus Torvalds 	xfs_ino_t	next_ino;
20551da177e4SLinus Torvalds 	xfs_mount_t	*mp;
20561da177e4SLinus Torvalds 	xfs_agi_t	*agi;
20571da177e4SLinus Torvalds 	xfs_dinode_t	*dip;
20581da177e4SLinus Torvalds 	xfs_buf_t	*agibp;
20591da177e4SLinus Torvalds 	xfs_buf_t	*ibp;
20601da177e4SLinus Torvalds 	xfs_agnumber_t	agno;
20611da177e4SLinus Torvalds 	xfs_agino_t	agino;
20621da177e4SLinus Torvalds 	xfs_agino_t	next_agino;
20631da177e4SLinus Torvalds 	xfs_buf_t	*last_ibp;
20646fdf8cccSNathan Scott 	xfs_dinode_t	*last_dip = NULL;
20651da177e4SLinus Torvalds 	short		bucket_index;
20666fdf8cccSNathan Scott 	int		offset, last_offset = 0;
20671da177e4SLinus Torvalds 	int		error;
20681da177e4SLinus Torvalds 
20691da177e4SLinus Torvalds 	mp = tp->t_mountp;
20701da177e4SLinus Torvalds 	agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
20711da177e4SLinus Torvalds 
20721da177e4SLinus Torvalds 	/*
20731da177e4SLinus Torvalds 	 * Get the agi buffer first.  It ensures lock ordering
20741da177e4SLinus Torvalds 	 * on the list.
20751da177e4SLinus Torvalds 	 */
20765e1be0fbSChristoph Hellwig 	error = xfs_read_agi(mp, tp, agno, &agibp);
20775e1be0fbSChristoph Hellwig 	if (error)
20781da177e4SLinus Torvalds 		return error;
20795e1be0fbSChristoph Hellwig 
20801da177e4SLinus Torvalds 	agi = XFS_BUF_TO_AGI(agibp);
20815e1be0fbSChristoph Hellwig 
20821da177e4SLinus Torvalds 	/*
20831da177e4SLinus Torvalds 	 * Get the index into the agi hash table for the
20841da177e4SLinus Torvalds 	 * list this inode will go on.
20851da177e4SLinus Torvalds 	 */
20861da177e4SLinus Torvalds 	agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
20871da177e4SLinus Torvalds 	ASSERT(agino != 0);
20881da177e4SLinus Torvalds 	bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
208969ef921bSChristoph Hellwig 	ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO));
20901da177e4SLinus Torvalds 	ASSERT(agi->agi_unlinked[bucket_index]);
20911da177e4SLinus Torvalds 
209216259e7dSChristoph Hellwig 	if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
20931da177e4SLinus Torvalds 		/*
2094475ee413SChristoph Hellwig 		 * We're at the head of the list.  Get the inode's on-disk
2095475ee413SChristoph Hellwig 		 * buffer to see if there is anyone after us on the list.
2096475ee413SChristoph Hellwig 		 * Only modify our next pointer if it is not already NULLAGINO.
2097475ee413SChristoph Hellwig 		 * This saves us the overhead of dealing with the buffer when
2098475ee413SChristoph Hellwig 		 * there is no need to change it.
20991da177e4SLinus Torvalds 		 */
2100475ee413SChristoph Hellwig 		error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
2101475ee413SChristoph Hellwig 				       0, 0);
21021da177e4SLinus Torvalds 		if (error) {
2103475ee413SChristoph Hellwig 			xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
21040b932cccSDave Chinner 				__func__, error);
21051da177e4SLinus Torvalds 			return error;
21061da177e4SLinus Torvalds 		}
2107347d1c01SChristoph Hellwig 		next_agino = be32_to_cpu(dip->di_next_unlinked);
21081da177e4SLinus Torvalds 		ASSERT(next_agino != 0);
21091da177e4SLinus Torvalds 		if (next_agino != NULLAGINO) {
2110347d1c01SChristoph Hellwig 			dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
211192bfc6e7SChristoph Hellwig 			offset = ip->i_imap.im_boffset +
21121da177e4SLinus Torvalds 				offsetof(xfs_dinode_t, di_next_unlinked);
21130a32c26eSDave Chinner 
21140a32c26eSDave Chinner 			/* need to recalc the inode CRC if appropriate */
21150a32c26eSDave Chinner 			xfs_dinode_calc_crc(mp, dip);
21160a32c26eSDave Chinner 
21171da177e4SLinus Torvalds 			xfs_trans_inode_buf(tp, ibp);
21181da177e4SLinus Torvalds 			xfs_trans_log_buf(tp, ibp, offset,
21191da177e4SLinus Torvalds 					  (offset + sizeof(xfs_agino_t) - 1));
21201da177e4SLinus Torvalds 			xfs_inobp_check(mp, ibp);
21211da177e4SLinus Torvalds 		} else {
21221da177e4SLinus Torvalds 			xfs_trans_brelse(tp, ibp);
21231da177e4SLinus Torvalds 		}
21241da177e4SLinus Torvalds 		/*
21251da177e4SLinus Torvalds 		 * Point the bucket head pointer at the next inode.
21261da177e4SLinus Torvalds 		 */
21271da177e4SLinus Torvalds 		ASSERT(next_agino != 0);
21281da177e4SLinus Torvalds 		ASSERT(next_agino != agino);
212916259e7dSChristoph Hellwig 		agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
21301da177e4SLinus Torvalds 		offset = offsetof(xfs_agi_t, agi_unlinked) +
21311da177e4SLinus Torvalds 			(sizeof(xfs_agino_t) * bucket_index);
2132f19b872bSDave Chinner 		xfs_trans_buf_set_type(tp, agibp, XFS_BLFT_AGI_BUF);
21331da177e4SLinus Torvalds 		xfs_trans_log_buf(tp, agibp, offset,
21341da177e4SLinus Torvalds 				  (offset + sizeof(xfs_agino_t) - 1));
21351da177e4SLinus Torvalds 	} else {
21361da177e4SLinus Torvalds 		/*
21371da177e4SLinus Torvalds 		 * We need to search the list for the inode being freed.
21381da177e4SLinus Torvalds 		 */
213916259e7dSChristoph Hellwig 		next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
21401da177e4SLinus Torvalds 		last_ibp = NULL;
21411da177e4SLinus Torvalds 		while (next_agino != agino) {
2142129dbc9aSChristoph Hellwig 			struct xfs_imap	imap;
2143129dbc9aSChristoph Hellwig 
2144129dbc9aSChristoph Hellwig 			if (last_ibp)
21451da177e4SLinus Torvalds 				xfs_trans_brelse(tp, last_ibp);
2146129dbc9aSChristoph Hellwig 
2147129dbc9aSChristoph Hellwig 			imap.im_blkno = 0;
21481da177e4SLinus Torvalds 			next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
2149129dbc9aSChristoph Hellwig 
2150129dbc9aSChristoph Hellwig 			error = xfs_imap(mp, tp, next_ino, &imap, 0);
21511da177e4SLinus Torvalds 			if (error) {
21520b932cccSDave Chinner 				xfs_warn(mp,
2153129dbc9aSChristoph Hellwig 	"%s: xfs_imap returned error %d.",
21540b932cccSDave Chinner 					 __func__, error);
21551da177e4SLinus Torvalds 				return error;
21561da177e4SLinus Torvalds 			}
2157129dbc9aSChristoph Hellwig 
2158129dbc9aSChristoph Hellwig 			error = xfs_imap_to_bp(mp, tp, &imap, &last_dip,
2159129dbc9aSChristoph Hellwig 					       &last_ibp, 0, 0);
2160129dbc9aSChristoph Hellwig 			if (error) {
2161129dbc9aSChristoph Hellwig 				xfs_warn(mp,
2162129dbc9aSChristoph Hellwig 	"%s: xfs_imap_to_bp returned error %d.",
2163129dbc9aSChristoph Hellwig 					__func__, error);
2164129dbc9aSChristoph Hellwig 				return error;
2165129dbc9aSChristoph Hellwig 			}
2166129dbc9aSChristoph Hellwig 
2167129dbc9aSChristoph Hellwig 			last_offset = imap.im_boffset;
2168347d1c01SChristoph Hellwig 			next_agino = be32_to_cpu(last_dip->di_next_unlinked);
21691da177e4SLinus Torvalds 			ASSERT(next_agino != NULLAGINO);
21701da177e4SLinus Torvalds 			ASSERT(next_agino != 0);
21711da177e4SLinus Torvalds 		}
2172475ee413SChristoph Hellwig 
21731da177e4SLinus Torvalds 		/*
2174475ee413SChristoph Hellwig 		 * Now last_ibp points to the buffer previous to us on the
2175475ee413SChristoph Hellwig 		 * unlinked list.  Pull us from the list.
21761da177e4SLinus Torvalds 		 */
2177475ee413SChristoph Hellwig 		error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
2178475ee413SChristoph Hellwig 				       0, 0);
21791da177e4SLinus Torvalds 		if (error) {
2180475ee413SChristoph Hellwig 			xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.",
21810b932cccSDave Chinner 				__func__, error);
21821da177e4SLinus Torvalds 			return error;
21831da177e4SLinus Torvalds 		}
2184347d1c01SChristoph Hellwig 		next_agino = be32_to_cpu(dip->di_next_unlinked);
21851da177e4SLinus Torvalds 		ASSERT(next_agino != 0);
21861da177e4SLinus Torvalds 		ASSERT(next_agino != agino);
21871da177e4SLinus Torvalds 		if (next_agino != NULLAGINO) {
2188347d1c01SChristoph Hellwig 			dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
218992bfc6e7SChristoph Hellwig 			offset = ip->i_imap.im_boffset +
21901da177e4SLinus Torvalds 				offsetof(xfs_dinode_t, di_next_unlinked);
21910a32c26eSDave Chinner 
21920a32c26eSDave Chinner 			/* need to recalc the inode CRC if appropriate */
21930a32c26eSDave Chinner 			xfs_dinode_calc_crc(mp, dip);
21940a32c26eSDave Chinner 
21951da177e4SLinus Torvalds 			xfs_trans_inode_buf(tp, ibp);
21961da177e4SLinus Torvalds 			xfs_trans_log_buf(tp, ibp, offset,
21971da177e4SLinus Torvalds 					  (offset + sizeof(xfs_agino_t) - 1));
21981da177e4SLinus Torvalds 			xfs_inobp_check(mp, ibp);
21991da177e4SLinus Torvalds 		} else {
22001da177e4SLinus Torvalds 			xfs_trans_brelse(tp, ibp);
22011da177e4SLinus Torvalds 		}
22021da177e4SLinus Torvalds 		/*
22031da177e4SLinus Torvalds 		 * Point the previous inode on the list to the next inode.
22041da177e4SLinus Torvalds 		 */
2205347d1c01SChristoph Hellwig 		last_dip->di_next_unlinked = cpu_to_be32(next_agino);
22061da177e4SLinus Torvalds 		ASSERT(next_agino != 0);
22071da177e4SLinus Torvalds 		offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
22080a32c26eSDave Chinner 
22090a32c26eSDave Chinner 		/* need to recalc the inode CRC if appropriate */
22100a32c26eSDave Chinner 		xfs_dinode_calc_crc(mp, last_dip);
22110a32c26eSDave Chinner 
22121da177e4SLinus Torvalds 		xfs_trans_inode_buf(tp, last_ibp);
22131da177e4SLinus Torvalds 		xfs_trans_log_buf(tp, last_ibp, offset,
22141da177e4SLinus Torvalds 				  (offset + sizeof(xfs_agino_t) - 1));
22151da177e4SLinus Torvalds 		xfs_inobp_check(mp, last_ibp);
22161da177e4SLinus Torvalds 	}
22171da177e4SLinus Torvalds 	return 0;
22181da177e4SLinus Torvalds }
22191da177e4SLinus Torvalds 
22205b3eed75SDave Chinner /*
22210b8182dbSZhi Yong Wu  * A big issue when freeing the inode cluster is that we _cannot_ skip any
22225b3eed75SDave Chinner  * inodes that are in memory - they all must be marked stale and attached to
22235b3eed75SDave Chinner  * the cluster buffer.
22245b3eed75SDave Chinner  */
22252a30f36dSChandra Seetharaman STATIC int
22261da177e4SLinus Torvalds xfs_ifree_cluster(
22271da177e4SLinus Torvalds 	xfs_inode_t		*free_ip,
22281da177e4SLinus Torvalds 	xfs_trans_t		*tp,
222909b56604SBrian Foster 	struct xfs_icluster	*xic)
22301da177e4SLinus Torvalds {
22311da177e4SLinus Torvalds 	xfs_mount_t		*mp = free_ip->i_mount;
22321da177e4SLinus Torvalds 	int			blks_per_cluster;
2233982e939eSJie Liu 	int			inodes_per_cluster;
22341da177e4SLinus Torvalds 	int			nbufs;
22355b257b4aSDave Chinner 	int			i, j;
22363cdaa189SBrian Foster 	int			ioffset;
22371da177e4SLinus Torvalds 	xfs_daddr_t		blkno;
22381da177e4SLinus Torvalds 	xfs_buf_t		*bp;
22395b257b4aSDave Chinner 	xfs_inode_t		*ip;
22401da177e4SLinus Torvalds 	xfs_inode_log_item_t	*iip;
22411da177e4SLinus Torvalds 	xfs_log_item_t		*lip;
22425017e97dSDave Chinner 	struct xfs_perag	*pag;
224309b56604SBrian Foster 	xfs_ino_t		inum;
22441da177e4SLinus Torvalds 
224509b56604SBrian Foster 	inum = xic->first_ino;
22465017e97dSDave Chinner 	pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
2247982e939eSJie Liu 	blks_per_cluster = xfs_icluster_size_fsb(mp);
2248982e939eSJie Liu 	inodes_per_cluster = blks_per_cluster << mp->m_sb.sb_inopblog;
2249126cd105SJie Liu 	nbufs = mp->m_ialloc_blks / blks_per_cluster;
22501da177e4SLinus Torvalds 
2251982e939eSJie Liu 	for (j = 0; j < nbufs; j++, inum += inodes_per_cluster) {
225209b56604SBrian Foster 		/*
225309b56604SBrian Foster 		 * The allocation bitmap tells us which inodes of the chunk were
225409b56604SBrian Foster 		 * physically allocated. Skip the cluster if an inode falls into
225509b56604SBrian Foster 		 * a sparse region.
225609b56604SBrian Foster 		 */
22573cdaa189SBrian Foster 		ioffset = inum - xic->first_ino;
22583cdaa189SBrian Foster 		if ((xic->alloc & XFS_INOBT_MASK(ioffset)) == 0) {
22593cdaa189SBrian Foster 			ASSERT(do_mod(ioffset, inodes_per_cluster) == 0);
226009b56604SBrian Foster 			continue;
226109b56604SBrian Foster 		}
226209b56604SBrian Foster 
22631da177e4SLinus Torvalds 		blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
22641da177e4SLinus Torvalds 					 XFS_INO_TO_AGBNO(mp, inum));
22651da177e4SLinus Torvalds 
22661da177e4SLinus Torvalds 		/*
22675b257b4aSDave Chinner 		 * We obtain and lock the backing buffer first in the process
22685b257b4aSDave Chinner 		 * here, as we have to ensure that any dirty inode that we
22695b257b4aSDave Chinner 		 * can't get the flush lock on is attached to the buffer.
22705b257b4aSDave Chinner 		 * If we scan the in-memory inodes first, then buffer IO can
22715b257b4aSDave Chinner 		 * complete before we get a lock on it, and hence we may fail
22725b257b4aSDave Chinner 		 * to mark all the active inodes on the buffer stale.
22731da177e4SLinus Torvalds 		 */
22741da177e4SLinus Torvalds 		bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
2275b6aff29fSDave Chinner 					mp->m_bsize * blks_per_cluster,
2276b6aff29fSDave Chinner 					XBF_UNMAPPED);
22771da177e4SLinus Torvalds 
22782a30f36dSChandra Seetharaman 		if (!bp)
22792451337dSDave Chinner 			return -ENOMEM;
2280b0f539deSDave Chinner 
2281b0f539deSDave Chinner 		/*
2282b0f539deSDave Chinner 		 * This buffer may not have been correctly initialised as we
2283b0f539deSDave Chinner 		 * didn't read it from disk. That's not important because we are
2284b0f539deSDave Chinner 		 * only using to mark the buffer as stale in the log, and to
2285b0f539deSDave Chinner 		 * attach stale cached inodes on it. That means it will never be
2286b0f539deSDave Chinner 		 * dispatched for IO. If it is, we want to know about it, and we
2287b0f539deSDave Chinner 		 * want it to fail. We can acheive this by adding a write
2288b0f539deSDave Chinner 		 * verifier to the buffer.
2289b0f539deSDave Chinner 		 */
22901813dd64SDave Chinner 		 bp->b_ops = &xfs_inode_buf_ops;
2291b0f539deSDave Chinner 
22925b257b4aSDave Chinner 		/*
22935b257b4aSDave Chinner 		 * Walk the inodes already attached to the buffer and mark them
22945b257b4aSDave Chinner 		 * stale. These will all have the flush locks held, so an
22955b3eed75SDave Chinner 		 * in-memory inode walk can't lock them. By marking them all
22965b3eed75SDave Chinner 		 * stale first, we will not attempt to lock them in the loop
22975b3eed75SDave Chinner 		 * below as the XFS_ISTALE flag will be set.
22985b257b4aSDave Chinner 		 */
2299adadbeefSChristoph Hellwig 		lip = bp->b_fspriv;
23001da177e4SLinus Torvalds 		while (lip) {
23011da177e4SLinus Torvalds 			if (lip->li_type == XFS_LI_INODE) {
23021da177e4SLinus Torvalds 				iip = (xfs_inode_log_item_t *)lip;
23031da177e4SLinus Torvalds 				ASSERT(iip->ili_logged == 1);
2304ca30b2a7SChristoph Hellwig 				lip->li_cb = xfs_istale_done;
23057b2e2a31SDavid Chinner 				xfs_trans_ail_copy_lsn(mp->m_ail,
23067b2e2a31SDavid Chinner 							&iip->ili_flush_lsn,
23077b2e2a31SDavid Chinner 							&iip->ili_item.li_lsn);
2308e5ffd2bbSDavid Chinner 				xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
23091da177e4SLinus Torvalds 			}
23101da177e4SLinus Torvalds 			lip = lip->li_bio_list;
23111da177e4SLinus Torvalds 		}
23121da177e4SLinus Torvalds 
23135b3eed75SDave Chinner 
23145b257b4aSDave Chinner 		/*
23155b257b4aSDave Chinner 		 * For each inode in memory attempt to add it to the inode
23165b257b4aSDave Chinner 		 * buffer and set it up for being staled on buffer IO
23175b257b4aSDave Chinner 		 * completion.  This is safe as we've locked out tail pushing
23185b257b4aSDave Chinner 		 * and flushing by locking the buffer.
23195b257b4aSDave Chinner 		 *
23205b257b4aSDave Chinner 		 * We have already marked every inode that was part of a
23215b257b4aSDave Chinner 		 * transaction stale above, which means there is no point in
23225b257b4aSDave Chinner 		 * even trying to lock them.
23235b257b4aSDave Chinner 		 */
2324982e939eSJie Liu 		for (i = 0; i < inodes_per_cluster; i++) {
23255b3eed75SDave Chinner retry:
23261a3e8f3dSDave Chinner 			rcu_read_lock();
23275b257b4aSDave Chinner 			ip = radix_tree_lookup(&pag->pag_ici_root,
23285b257b4aSDave Chinner 					XFS_INO_TO_AGINO(mp, (inum + i)));
23291da177e4SLinus Torvalds 
23301a3e8f3dSDave Chinner 			/* Inode not in memory, nothing to do */
23311a3e8f3dSDave Chinner 			if (!ip) {
23321a3e8f3dSDave Chinner 				rcu_read_unlock();
23335b257b4aSDave Chinner 				continue;
23345b257b4aSDave Chinner 			}
23355b257b4aSDave Chinner 
23365b3eed75SDave Chinner 			/*
23371a3e8f3dSDave Chinner 			 * because this is an RCU protected lookup, we could
23381a3e8f3dSDave Chinner 			 * find a recently freed or even reallocated inode
23391a3e8f3dSDave Chinner 			 * during the lookup. We need to check under the
23401a3e8f3dSDave Chinner 			 * i_flags_lock for a valid inode here. Skip it if it
23411a3e8f3dSDave Chinner 			 * is not valid, the wrong inode or stale.
23421a3e8f3dSDave Chinner 			 */
23431a3e8f3dSDave Chinner 			spin_lock(&ip->i_flags_lock);
23441a3e8f3dSDave Chinner 			if (ip->i_ino != inum + i ||
23451a3e8f3dSDave Chinner 			    __xfs_iflags_test(ip, XFS_ISTALE)) {
23461a3e8f3dSDave Chinner 				spin_unlock(&ip->i_flags_lock);
23471a3e8f3dSDave Chinner 				rcu_read_unlock();
23481a3e8f3dSDave Chinner 				continue;
23491a3e8f3dSDave Chinner 			}
23501a3e8f3dSDave Chinner 			spin_unlock(&ip->i_flags_lock);
23511a3e8f3dSDave Chinner 
23521a3e8f3dSDave Chinner 			/*
23535b3eed75SDave Chinner 			 * Don't try to lock/unlock the current inode, but we
23545b3eed75SDave Chinner 			 * _cannot_ skip the other inodes that we did not find
23555b3eed75SDave Chinner 			 * in the list attached to the buffer and are not
23565b3eed75SDave Chinner 			 * already marked stale. If we can't lock it, back off
23575b3eed75SDave Chinner 			 * and retry.
23585b3eed75SDave Chinner 			 */
23595b257b4aSDave Chinner 			if (ip != free_ip &&
23605b257b4aSDave Chinner 			    !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
23611a3e8f3dSDave Chinner 				rcu_read_unlock();
23625b3eed75SDave Chinner 				delay(1);
23635b3eed75SDave Chinner 				goto retry;
23645b257b4aSDave Chinner 			}
23651a3e8f3dSDave Chinner 			rcu_read_unlock();
23665b257b4aSDave Chinner 
23675b3eed75SDave Chinner 			xfs_iflock(ip);
23685b257b4aSDave Chinner 			xfs_iflags_set(ip, XFS_ISTALE);
23695b257b4aSDave Chinner 
23705b3eed75SDave Chinner 			/*
23715b3eed75SDave Chinner 			 * we don't need to attach clean inodes or those only
23725b3eed75SDave Chinner 			 * with unlogged changes (which we throw away, anyway).
23735b3eed75SDave Chinner 			 */
23745b257b4aSDave Chinner 			iip = ip->i_itemp;
23755b3eed75SDave Chinner 			if (!iip || xfs_inode_clean(ip)) {
23765b257b4aSDave Chinner 				ASSERT(ip != free_ip);
23771da177e4SLinus Torvalds 				xfs_ifunlock(ip);
23781da177e4SLinus Torvalds 				xfs_iunlock(ip, XFS_ILOCK_EXCL);
23791da177e4SLinus Torvalds 				continue;
23801da177e4SLinus Torvalds 			}
23811da177e4SLinus Torvalds 
2382f5d8d5c4SChristoph Hellwig 			iip->ili_last_fields = iip->ili_fields;
2383f5d8d5c4SChristoph Hellwig 			iip->ili_fields = 0;
2384fc0561ceSDave Chinner 			iip->ili_fsync_fields = 0;
23851da177e4SLinus Torvalds 			iip->ili_logged = 1;
23867b2e2a31SDavid Chinner 			xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
23877b2e2a31SDavid Chinner 						&iip->ili_item.li_lsn);
23881da177e4SLinus Torvalds 
2389ca30b2a7SChristoph Hellwig 			xfs_buf_attach_iodone(bp, xfs_istale_done,
2390ca30b2a7SChristoph Hellwig 						  &iip->ili_item);
23915b257b4aSDave Chinner 
23925b257b4aSDave Chinner 			if (ip != free_ip)
23931da177e4SLinus Torvalds 				xfs_iunlock(ip, XFS_ILOCK_EXCL);
23941da177e4SLinus Torvalds 		}
23951da177e4SLinus Torvalds 
23961da177e4SLinus Torvalds 		xfs_trans_stale_inode_buf(tp, bp);
23971da177e4SLinus Torvalds 		xfs_trans_binval(tp, bp);
23981da177e4SLinus Torvalds 	}
23991da177e4SLinus Torvalds 
24005017e97dSDave Chinner 	xfs_perag_put(pag);
24012a30f36dSChandra Seetharaman 	return 0;
24021da177e4SLinus Torvalds }
24031da177e4SLinus Torvalds 
24041da177e4SLinus Torvalds /*
24051da177e4SLinus Torvalds  * This is called to return an inode to the inode free list.
24061da177e4SLinus Torvalds  * The inode should already be truncated to 0 length and have
24071da177e4SLinus Torvalds  * no pages associated with it.  This routine also assumes that
24081da177e4SLinus Torvalds  * the inode is already a part of the transaction.
24091da177e4SLinus Torvalds  *
24101da177e4SLinus Torvalds  * The on-disk copy of the inode will have been added to the list
24111da177e4SLinus Torvalds  * of unlinked inodes in the AGI. We need to remove the inode from
24121da177e4SLinus Torvalds  * that list atomically with respect to freeing it here.
24131da177e4SLinus Torvalds  */
24141da177e4SLinus Torvalds int
24151da177e4SLinus Torvalds xfs_ifree(
24161da177e4SLinus Torvalds 	xfs_trans_t	*tp,
24171da177e4SLinus Torvalds 	xfs_inode_t	*ip,
24182c3234d1SDarrick J. Wong 	struct xfs_defer_ops	*dfops)
24191da177e4SLinus Torvalds {
24201da177e4SLinus Torvalds 	int			error;
242109b56604SBrian Foster 	struct xfs_icluster	xic = { 0 };
24221da177e4SLinus Torvalds 
2423579aa9caSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
242454d7b5c1SDave Chinner 	ASSERT(VFS_I(ip)->i_nlink == 0);
24251da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_nextents == 0);
24261da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_anextents == 0);
2427c19b3b05SDave Chinner 	ASSERT(ip->i_d.di_size == 0 || !S_ISREG(VFS_I(ip)->i_mode));
24281da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_nblocks == 0);
24291da177e4SLinus Torvalds 
24301da177e4SLinus Torvalds 	/*
24311da177e4SLinus Torvalds 	 * Pull the on-disk inode from the AGI unlinked list.
24321da177e4SLinus Torvalds 	 */
24331da177e4SLinus Torvalds 	error = xfs_iunlink_remove(tp, ip);
24341baaed8fSDave Chinner 	if (error)
24351da177e4SLinus Torvalds 		return error;
24361da177e4SLinus Torvalds 
24372c3234d1SDarrick J. Wong 	error = xfs_difree(tp, ip->i_ino, dfops, &xic);
24381baaed8fSDave Chinner 	if (error)
24391da177e4SLinus Torvalds 		return error;
24401baaed8fSDave Chinner 
2441c19b3b05SDave Chinner 	VFS_I(ip)->i_mode = 0;		/* mark incore inode as free */
24421da177e4SLinus Torvalds 	ip->i_d.di_flags = 0;
24431da177e4SLinus Torvalds 	ip->i_d.di_dmevmask = 0;
24441da177e4SLinus Torvalds 	ip->i_d.di_forkoff = 0;		/* mark the attr fork not in use */
24451da177e4SLinus Torvalds 	ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
24461da177e4SLinus Torvalds 	ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
24471da177e4SLinus Torvalds 	/*
24481da177e4SLinus Torvalds 	 * Bump the generation count so no one will be confused
24491da177e4SLinus Torvalds 	 * by reincarnations of this inode.
24501da177e4SLinus Torvalds 	 */
24519e9a2674SDave Chinner 	VFS_I(ip)->i_generation++;
24521da177e4SLinus Torvalds 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
24531da177e4SLinus Torvalds 
245409b56604SBrian Foster 	if (xic.deleted)
245509b56604SBrian Foster 		error = xfs_ifree_cluster(ip, tp, &xic);
24561da177e4SLinus Torvalds 
24572a30f36dSChandra Seetharaman 	return error;
24581da177e4SLinus Torvalds }
24591da177e4SLinus Torvalds 
24601da177e4SLinus Torvalds /*
246160ec6783SChristoph Hellwig  * This is called to unpin an inode.  The caller must have the inode locked
246260ec6783SChristoph Hellwig  * in at least shared mode so that the buffer cannot be subsequently pinned
246360ec6783SChristoph Hellwig  * once someone is waiting for it to be unpinned.
24641da177e4SLinus Torvalds  */
246560ec6783SChristoph Hellwig static void
2466f392e631SChristoph Hellwig xfs_iunpin(
246760ec6783SChristoph Hellwig 	struct xfs_inode	*ip)
2468a3f74ffbSDavid Chinner {
2469579aa9caSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
2470a3f74ffbSDavid Chinner 
24714aaf15d1SDave Chinner 	trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
24724aaf15d1SDave Chinner 
2473a3f74ffbSDavid Chinner 	/* Give the log a push to start the unpinning I/O */
247460ec6783SChristoph Hellwig 	xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0);
2475a14a348bSChristoph Hellwig 
2476a3f74ffbSDavid Chinner }
2477a3f74ffbSDavid Chinner 
2478f392e631SChristoph Hellwig static void
2479f392e631SChristoph Hellwig __xfs_iunpin_wait(
2480f392e631SChristoph Hellwig 	struct xfs_inode	*ip)
2481f392e631SChristoph Hellwig {
2482f392e631SChristoph Hellwig 	wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
2483f392e631SChristoph Hellwig 	DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
2484f392e631SChristoph Hellwig 
2485f392e631SChristoph Hellwig 	xfs_iunpin(ip);
2486f392e631SChristoph Hellwig 
2487f392e631SChristoph Hellwig 	do {
2488f392e631SChristoph Hellwig 		prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2489f392e631SChristoph Hellwig 		if (xfs_ipincount(ip))
2490f392e631SChristoph Hellwig 			io_schedule();
2491f392e631SChristoph Hellwig 	} while (xfs_ipincount(ip));
2492f392e631SChristoph Hellwig 	finish_wait(wq, &wait.wait);
2493f392e631SChristoph Hellwig }
2494f392e631SChristoph Hellwig 
2495777df5afSDave Chinner void
24961da177e4SLinus Torvalds xfs_iunpin_wait(
249760ec6783SChristoph Hellwig 	struct xfs_inode	*ip)
24981da177e4SLinus Torvalds {
2499f392e631SChristoph Hellwig 	if (xfs_ipincount(ip))
2500f392e631SChristoph Hellwig 		__xfs_iunpin_wait(ip);
25011da177e4SLinus Torvalds }
25021da177e4SLinus Torvalds 
250327320369SDave Chinner /*
250427320369SDave Chinner  * Removing an inode from the namespace involves removing the directory entry
250527320369SDave Chinner  * and dropping the link count on the inode. Removing the directory entry can
250627320369SDave Chinner  * result in locking an AGF (directory blocks were freed) and removing a link
250727320369SDave Chinner  * count can result in placing the inode on an unlinked list which results in
250827320369SDave Chinner  * locking an AGI.
250927320369SDave Chinner  *
251027320369SDave Chinner  * The big problem here is that we have an ordering constraint on AGF and AGI
251127320369SDave Chinner  * locking - inode allocation locks the AGI, then can allocate a new extent for
251227320369SDave Chinner  * new inodes, locking the AGF after the AGI. Similarly, freeing the inode
251327320369SDave Chinner  * removes the inode from the unlinked list, requiring that we lock the AGI
251427320369SDave Chinner  * first, and then freeing the inode can result in an inode chunk being freed
251527320369SDave Chinner  * and hence freeing disk space requiring that we lock an AGF.
251627320369SDave Chinner  *
251727320369SDave Chinner  * Hence the ordering that is imposed by other parts of the code is AGI before
251827320369SDave Chinner  * AGF. This means we cannot remove the directory entry before we drop the inode
251927320369SDave Chinner  * reference count and put it on the unlinked list as this results in a lock
252027320369SDave Chinner  * order of AGF then AGI, and this can deadlock against inode allocation and
252127320369SDave Chinner  * freeing. Therefore we must drop the link counts before we remove the
252227320369SDave Chinner  * directory entry.
252327320369SDave Chinner  *
252427320369SDave Chinner  * This is still safe from a transactional point of view - it is not until we
2525310a75a3SDarrick J. Wong  * get to xfs_defer_finish() that we have the possibility of multiple
252627320369SDave Chinner  * transactions in this operation. Hence as long as we remove the directory
252727320369SDave Chinner  * entry and drop the link count in the first transaction of the remove
252827320369SDave Chinner  * operation, there are no transactional constraints on the ordering here.
252927320369SDave Chinner  */
2530c24b5dfaSDave Chinner int
2531c24b5dfaSDave Chinner xfs_remove(
2532c24b5dfaSDave Chinner 	xfs_inode_t             *dp,
2533c24b5dfaSDave Chinner 	struct xfs_name		*name,
2534c24b5dfaSDave Chinner 	xfs_inode_t		*ip)
2535c24b5dfaSDave Chinner {
2536c24b5dfaSDave Chinner 	xfs_mount_t		*mp = dp->i_mount;
2537c24b5dfaSDave Chinner 	xfs_trans_t             *tp = NULL;
2538c19b3b05SDave Chinner 	int			is_dir = S_ISDIR(VFS_I(ip)->i_mode);
2539c24b5dfaSDave Chinner 	int                     error = 0;
25402c3234d1SDarrick J. Wong 	struct xfs_defer_ops	dfops;
2541c24b5dfaSDave Chinner 	xfs_fsblock_t           first_block;
2542c24b5dfaSDave Chinner 	uint			resblks;
2543c24b5dfaSDave Chinner 
2544c24b5dfaSDave Chinner 	trace_xfs_remove(dp, name);
2545c24b5dfaSDave Chinner 
2546c24b5dfaSDave Chinner 	if (XFS_FORCED_SHUTDOWN(mp))
25472451337dSDave Chinner 		return -EIO;
2548c24b5dfaSDave Chinner 
2549c24b5dfaSDave Chinner 	error = xfs_qm_dqattach(dp, 0);
2550c24b5dfaSDave Chinner 	if (error)
2551c24b5dfaSDave Chinner 		goto std_return;
2552c24b5dfaSDave Chinner 
2553c24b5dfaSDave Chinner 	error = xfs_qm_dqattach(ip, 0);
2554c24b5dfaSDave Chinner 	if (error)
2555c24b5dfaSDave Chinner 		goto std_return;
2556c24b5dfaSDave Chinner 
2557c24b5dfaSDave Chinner 	/*
2558c24b5dfaSDave Chinner 	 * We try to get the real space reservation first,
2559c24b5dfaSDave Chinner 	 * allowing for directory btree deletion(s) implying
2560c24b5dfaSDave Chinner 	 * possible bmap insert(s).  If we can't get the space
2561c24b5dfaSDave Chinner 	 * reservation then we use 0 instead, and avoid the bmap
2562c24b5dfaSDave Chinner 	 * btree insert(s) in the directory code by, if the bmap
2563c24b5dfaSDave Chinner 	 * insert tries to happen, instead trimming the LAST
2564c24b5dfaSDave Chinner 	 * block from the directory.
2565c24b5dfaSDave Chinner 	 */
2566c24b5dfaSDave Chinner 	resblks = XFS_REMOVE_SPACE_RES(mp);
2567253f4911SChristoph Hellwig 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_remove, resblks, 0, 0, &tp);
25682451337dSDave Chinner 	if (error == -ENOSPC) {
2569c24b5dfaSDave Chinner 		resblks = 0;
2570253f4911SChristoph Hellwig 		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_remove, 0, 0, 0,
2571253f4911SChristoph Hellwig 				&tp);
2572c24b5dfaSDave Chinner 	}
2573c24b5dfaSDave Chinner 	if (error) {
25742451337dSDave Chinner 		ASSERT(error != -ENOSPC);
2575253f4911SChristoph Hellwig 		goto std_return;
2576c24b5dfaSDave Chinner 	}
2577c24b5dfaSDave Chinner 
2578dbad7c99SDave Chinner 	xfs_ilock(dp, XFS_IOLOCK_EXCL | XFS_IOLOCK_PARENT);
2579c24b5dfaSDave Chinner 	xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
2580c24b5dfaSDave Chinner 
2581dbad7c99SDave Chinner 	xfs_trans_ijoin(tp, dp, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
2582c24b5dfaSDave Chinner 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2583c24b5dfaSDave Chinner 
2584c24b5dfaSDave Chinner 	/*
2585c24b5dfaSDave Chinner 	 * If we're removing a directory perform some additional validation.
2586c24b5dfaSDave Chinner 	 */
2587c24b5dfaSDave Chinner 	if (is_dir) {
258854d7b5c1SDave Chinner 		ASSERT(VFS_I(ip)->i_nlink >= 2);
258954d7b5c1SDave Chinner 		if (VFS_I(ip)->i_nlink != 2) {
25902451337dSDave Chinner 			error = -ENOTEMPTY;
2591c24b5dfaSDave Chinner 			goto out_trans_cancel;
2592c24b5dfaSDave Chinner 		}
2593c24b5dfaSDave Chinner 		if (!xfs_dir_isempty(ip)) {
25942451337dSDave Chinner 			error = -ENOTEMPTY;
2595c24b5dfaSDave Chinner 			goto out_trans_cancel;
2596c24b5dfaSDave Chinner 		}
2597c24b5dfaSDave Chinner 
259827320369SDave Chinner 		/* Drop the link from ip's "..".  */
2599c24b5dfaSDave Chinner 		error = xfs_droplink(tp, dp);
2600c24b5dfaSDave Chinner 		if (error)
260127320369SDave Chinner 			goto out_trans_cancel;
2602c24b5dfaSDave Chinner 
260327320369SDave Chinner 		/* Drop the "." link from ip to self.  */
2604c24b5dfaSDave Chinner 		error = xfs_droplink(tp, ip);
2605c24b5dfaSDave Chinner 		if (error)
260627320369SDave Chinner 			goto out_trans_cancel;
2607c24b5dfaSDave Chinner 	} else {
2608c24b5dfaSDave Chinner 		/*
2609c24b5dfaSDave Chinner 		 * When removing a non-directory we need to log the parent
2610c24b5dfaSDave Chinner 		 * inode here.  For a directory this is done implicitly
2611c24b5dfaSDave Chinner 		 * by the xfs_droplink call for the ".." entry.
2612c24b5dfaSDave Chinner 		 */
2613c24b5dfaSDave Chinner 		xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2614c24b5dfaSDave Chinner 	}
261527320369SDave Chinner 	xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2616c24b5dfaSDave Chinner 
261727320369SDave Chinner 	/* Drop the link from dp to ip. */
2618c24b5dfaSDave Chinner 	error = xfs_droplink(tp, ip);
2619c24b5dfaSDave Chinner 	if (error)
262027320369SDave Chinner 		goto out_trans_cancel;
2621c24b5dfaSDave Chinner 
26222c3234d1SDarrick J. Wong 	xfs_defer_init(&dfops, &first_block);
262327320369SDave Chinner 	error = xfs_dir_removename(tp, dp, name, ip->i_ino,
26242c3234d1SDarrick J. Wong 					&first_block, &dfops, resblks);
262527320369SDave Chinner 	if (error) {
26262451337dSDave Chinner 		ASSERT(error != -ENOENT);
262727320369SDave Chinner 		goto out_bmap_cancel;
262827320369SDave Chinner 	}
262927320369SDave Chinner 
2630c24b5dfaSDave Chinner 	/*
2631c24b5dfaSDave Chinner 	 * If this is a synchronous mount, make sure that the
2632c24b5dfaSDave Chinner 	 * remove transaction goes to disk before returning to
2633c24b5dfaSDave Chinner 	 * the user.
2634c24b5dfaSDave Chinner 	 */
2635c24b5dfaSDave Chinner 	if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
2636c24b5dfaSDave Chinner 		xfs_trans_set_sync(tp);
2637c24b5dfaSDave Chinner 
26382c3234d1SDarrick J. Wong 	error = xfs_defer_finish(&tp, &dfops, NULL);
2639c24b5dfaSDave Chinner 	if (error)
2640c24b5dfaSDave Chinner 		goto out_bmap_cancel;
2641c24b5dfaSDave Chinner 
264270393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
2643c24b5dfaSDave Chinner 	if (error)
2644c24b5dfaSDave Chinner 		goto std_return;
2645c24b5dfaSDave Chinner 
26462cd2ef6aSChristoph Hellwig 	if (is_dir && xfs_inode_is_filestream(ip))
2647c24b5dfaSDave Chinner 		xfs_filestream_deassociate(ip);
2648c24b5dfaSDave Chinner 
2649c24b5dfaSDave Chinner 	return 0;
2650c24b5dfaSDave Chinner 
2651c24b5dfaSDave Chinner  out_bmap_cancel:
26522c3234d1SDarrick J. Wong 	xfs_defer_cancel(&dfops);
2653c24b5dfaSDave Chinner  out_trans_cancel:
26544906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
2655c24b5dfaSDave Chinner  std_return:
2656c24b5dfaSDave Chinner 	return error;
2657c24b5dfaSDave Chinner }
2658c24b5dfaSDave Chinner 
2659f6bba201SDave Chinner /*
2660f6bba201SDave Chinner  * Enter all inodes for a rename transaction into a sorted array.
2661f6bba201SDave Chinner  */
266295afcf5cSDave Chinner #define __XFS_SORT_INODES	5
2663f6bba201SDave Chinner STATIC void
2664f6bba201SDave Chinner xfs_sort_for_rename(
266595afcf5cSDave Chinner 	struct xfs_inode	*dp1,	/* in: old (source) directory inode */
266695afcf5cSDave Chinner 	struct xfs_inode	*dp2,	/* in: new (target) directory inode */
266795afcf5cSDave Chinner 	struct xfs_inode	*ip1,	/* in: inode of old entry */
266895afcf5cSDave Chinner 	struct xfs_inode	*ip2,	/* in: inode of new entry */
266995afcf5cSDave Chinner 	struct xfs_inode	*wip,	/* in: whiteout inode */
267095afcf5cSDave Chinner 	struct xfs_inode	**i_tab,/* out: sorted array of inodes */
267195afcf5cSDave Chinner 	int			*num_inodes)  /* in/out: inodes in array */
2672f6bba201SDave Chinner {
2673f6bba201SDave Chinner 	int			i, j;
2674f6bba201SDave Chinner 
267595afcf5cSDave Chinner 	ASSERT(*num_inodes == __XFS_SORT_INODES);
267695afcf5cSDave Chinner 	memset(i_tab, 0, *num_inodes * sizeof(struct xfs_inode *));
267795afcf5cSDave Chinner 
2678f6bba201SDave Chinner 	/*
2679f6bba201SDave Chinner 	 * i_tab contains a list of pointers to inodes.  We initialize
2680f6bba201SDave Chinner 	 * the table here & we'll sort it.  We will then use it to
2681f6bba201SDave Chinner 	 * order the acquisition of the inode locks.
2682f6bba201SDave Chinner 	 *
2683f6bba201SDave Chinner 	 * Note that the table may contain duplicates.  e.g., dp1 == dp2.
2684f6bba201SDave Chinner 	 */
268595afcf5cSDave Chinner 	i = 0;
268695afcf5cSDave Chinner 	i_tab[i++] = dp1;
268795afcf5cSDave Chinner 	i_tab[i++] = dp2;
268895afcf5cSDave Chinner 	i_tab[i++] = ip1;
268995afcf5cSDave Chinner 	if (ip2)
269095afcf5cSDave Chinner 		i_tab[i++] = ip2;
269195afcf5cSDave Chinner 	if (wip)
269295afcf5cSDave Chinner 		i_tab[i++] = wip;
269395afcf5cSDave Chinner 	*num_inodes = i;
2694f6bba201SDave Chinner 
2695f6bba201SDave Chinner 	/*
2696f6bba201SDave Chinner 	 * Sort the elements via bubble sort.  (Remember, there are at
269795afcf5cSDave Chinner 	 * most 5 elements to sort, so this is adequate.)
2698f6bba201SDave Chinner 	 */
2699f6bba201SDave Chinner 	for (i = 0; i < *num_inodes; i++) {
2700f6bba201SDave Chinner 		for (j = 1; j < *num_inodes; j++) {
2701f6bba201SDave Chinner 			if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) {
270295afcf5cSDave Chinner 				struct xfs_inode *temp = i_tab[j];
2703f6bba201SDave Chinner 				i_tab[j] = i_tab[j-1];
2704f6bba201SDave Chinner 				i_tab[j-1] = temp;
2705f6bba201SDave Chinner 			}
2706f6bba201SDave Chinner 		}
2707f6bba201SDave Chinner 	}
2708f6bba201SDave Chinner }
2709f6bba201SDave Chinner 
2710310606b0SDave Chinner static int
2711310606b0SDave Chinner xfs_finish_rename(
2712310606b0SDave Chinner 	struct xfs_trans	*tp,
27132c3234d1SDarrick J. Wong 	struct xfs_defer_ops	*dfops)
2714310606b0SDave Chinner {
2715310606b0SDave Chinner 	int			error;
2716310606b0SDave Chinner 
2717310606b0SDave Chinner 	/*
2718310606b0SDave Chinner 	 * If this is a synchronous mount, make sure that the rename transaction
2719310606b0SDave Chinner 	 * goes to disk before returning to the user.
2720310606b0SDave Chinner 	 */
2721310606b0SDave Chinner 	if (tp->t_mountp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
2722310606b0SDave Chinner 		xfs_trans_set_sync(tp);
2723310606b0SDave Chinner 
27242c3234d1SDarrick J. Wong 	error = xfs_defer_finish(&tp, dfops, NULL);
2725310606b0SDave Chinner 	if (error) {
27262c3234d1SDarrick J. Wong 		xfs_defer_cancel(dfops);
27274906e215SChristoph Hellwig 		xfs_trans_cancel(tp);
2728310606b0SDave Chinner 		return error;
2729310606b0SDave Chinner 	}
2730310606b0SDave Chinner 
273170393313SChristoph Hellwig 	return xfs_trans_commit(tp);
2732310606b0SDave Chinner }
2733310606b0SDave Chinner 
2734f6bba201SDave Chinner /*
2735d31a1825SCarlos Maiolino  * xfs_cross_rename()
2736d31a1825SCarlos Maiolino  *
2737d31a1825SCarlos Maiolino  * responsible for handling RENAME_EXCHANGE flag in renameat2() sytemcall
2738d31a1825SCarlos Maiolino  */
2739d31a1825SCarlos Maiolino STATIC int
2740d31a1825SCarlos Maiolino xfs_cross_rename(
2741d31a1825SCarlos Maiolino 	struct xfs_trans	*tp,
2742d31a1825SCarlos Maiolino 	struct xfs_inode	*dp1,
2743d31a1825SCarlos Maiolino 	struct xfs_name		*name1,
2744d31a1825SCarlos Maiolino 	struct xfs_inode	*ip1,
2745d31a1825SCarlos Maiolino 	struct xfs_inode	*dp2,
2746d31a1825SCarlos Maiolino 	struct xfs_name		*name2,
2747d31a1825SCarlos Maiolino 	struct xfs_inode	*ip2,
27482c3234d1SDarrick J. Wong 	struct xfs_defer_ops	*dfops,
2749d31a1825SCarlos Maiolino 	xfs_fsblock_t		*first_block,
2750d31a1825SCarlos Maiolino 	int			spaceres)
2751d31a1825SCarlos Maiolino {
2752d31a1825SCarlos Maiolino 	int		error = 0;
2753d31a1825SCarlos Maiolino 	int		ip1_flags = 0;
2754d31a1825SCarlos Maiolino 	int		ip2_flags = 0;
2755d31a1825SCarlos Maiolino 	int		dp2_flags = 0;
2756d31a1825SCarlos Maiolino 
2757d31a1825SCarlos Maiolino 	/* Swap inode number for dirent in first parent */
2758d31a1825SCarlos Maiolino 	error = xfs_dir_replace(tp, dp1, name1,
2759d31a1825SCarlos Maiolino 				ip2->i_ino,
27602c3234d1SDarrick J. Wong 				first_block, dfops, spaceres);
2761d31a1825SCarlos Maiolino 	if (error)
2762eeacd321SDave Chinner 		goto out_trans_abort;
2763d31a1825SCarlos Maiolino 
2764d31a1825SCarlos Maiolino 	/* Swap inode number for dirent in second parent */
2765d31a1825SCarlos Maiolino 	error = xfs_dir_replace(tp, dp2, name2,
2766d31a1825SCarlos Maiolino 				ip1->i_ino,
27672c3234d1SDarrick J. Wong 				first_block, dfops, spaceres);
2768d31a1825SCarlos Maiolino 	if (error)
2769eeacd321SDave Chinner 		goto out_trans_abort;
2770d31a1825SCarlos Maiolino 
2771d31a1825SCarlos Maiolino 	/*
2772d31a1825SCarlos Maiolino 	 * If we're renaming one or more directories across different parents,
2773d31a1825SCarlos Maiolino 	 * update the respective ".." entries (and link counts) to match the new
2774d31a1825SCarlos Maiolino 	 * parents.
2775d31a1825SCarlos Maiolino 	 */
2776d31a1825SCarlos Maiolino 	if (dp1 != dp2) {
2777d31a1825SCarlos Maiolino 		dp2_flags = XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
2778d31a1825SCarlos Maiolino 
2779c19b3b05SDave Chinner 		if (S_ISDIR(VFS_I(ip2)->i_mode)) {
2780d31a1825SCarlos Maiolino 			error = xfs_dir_replace(tp, ip2, &xfs_name_dotdot,
2781d31a1825SCarlos Maiolino 						dp1->i_ino, first_block,
27822c3234d1SDarrick J. Wong 						dfops, spaceres);
2783d31a1825SCarlos Maiolino 			if (error)
2784eeacd321SDave Chinner 				goto out_trans_abort;
2785d31a1825SCarlos Maiolino 
2786d31a1825SCarlos Maiolino 			/* transfer ip2 ".." reference to dp1 */
2787c19b3b05SDave Chinner 			if (!S_ISDIR(VFS_I(ip1)->i_mode)) {
2788d31a1825SCarlos Maiolino 				error = xfs_droplink(tp, dp2);
2789d31a1825SCarlos Maiolino 				if (error)
2790eeacd321SDave Chinner 					goto out_trans_abort;
2791d31a1825SCarlos Maiolino 				error = xfs_bumplink(tp, dp1);
2792d31a1825SCarlos Maiolino 				if (error)
2793eeacd321SDave Chinner 					goto out_trans_abort;
2794d31a1825SCarlos Maiolino 			}
2795d31a1825SCarlos Maiolino 
2796d31a1825SCarlos Maiolino 			/*
2797d31a1825SCarlos Maiolino 			 * Although ip1 isn't changed here, userspace needs
2798d31a1825SCarlos Maiolino 			 * to be warned about the change, so that applications
2799d31a1825SCarlos Maiolino 			 * relying on it (like backup ones), will properly
2800d31a1825SCarlos Maiolino 			 * notify the change
2801d31a1825SCarlos Maiolino 			 */
2802d31a1825SCarlos Maiolino 			ip1_flags |= XFS_ICHGTIME_CHG;
2803d31a1825SCarlos Maiolino 			ip2_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
2804d31a1825SCarlos Maiolino 		}
2805d31a1825SCarlos Maiolino 
2806c19b3b05SDave Chinner 		if (S_ISDIR(VFS_I(ip1)->i_mode)) {
2807d31a1825SCarlos Maiolino 			error = xfs_dir_replace(tp, ip1, &xfs_name_dotdot,
2808d31a1825SCarlos Maiolino 						dp2->i_ino, first_block,
28092c3234d1SDarrick J. Wong 						dfops, spaceres);
2810d31a1825SCarlos Maiolino 			if (error)
2811eeacd321SDave Chinner 				goto out_trans_abort;
2812d31a1825SCarlos Maiolino 
2813d31a1825SCarlos Maiolino 			/* transfer ip1 ".." reference to dp2 */
2814c19b3b05SDave Chinner 			if (!S_ISDIR(VFS_I(ip2)->i_mode)) {
2815d31a1825SCarlos Maiolino 				error = xfs_droplink(tp, dp1);
2816d31a1825SCarlos Maiolino 				if (error)
2817eeacd321SDave Chinner 					goto out_trans_abort;
2818d31a1825SCarlos Maiolino 				error = xfs_bumplink(tp, dp2);
2819d31a1825SCarlos Maiolino 				if (error)
2820eeacd321SDave Chinner 					goto out_trans_abort;
2821d31a1825SCarlos Maiolino 			}
2822d31a1825SCarlos Maiolino 
2823d31a1825SCarlos Maiolino 			/*
2824d31a1825SCarlos Maiolino 			 * Although ip2 isn't changed here, userspace needs
2825d31a1825SCarlos Maiolino 			 * to be warned about the change, so that applications
2826d31a1825SCarlos Maiolino 			 * relying on it (like backup ones), will properly
2827d31a1825SCarlos Maiolino 			 * notify the change
2828d31a1825SCarlos Maiolino 			 */
2829d31a1825SCarlos Maiolino 			ip1_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
2830d31a1825SCarlos Maiolino 			ip2_flags |= XFS_ICHGTIME_CHG;
2831d31a1825SCarlos Maiolino 		}
2832d31a1825SCarlos Maiolino 	}
2833d31a1825SCarlos Maiolino 
2834d31a1825SCarlos Maiolino 	if (ip1_flags) {
2835d31a1825SCarlos Maiolino 		xfs_trans_ichgtime(tp, ip1, ip1_flags);
2836d31a1825SCarlos Maiolino 		xfs_trans_log_inode(tp, ip1, XFS_ILOG_CORE);
2837d31a1825SCarlos Maiolino 	}
2838d31a1825SCarlos Maiolino 	if (ip2_flags) {
2839d31a1825SCarlos Maiolino 		xfs_trans_ichgtime(tp, ip2, ip2_flags);
2840d31a1825SCarlos Maiolino 		xfs_trans_log_inode(tp, ip2, XFS_ILOG_CORE);
2841d31a1825SCarlos Maiolino 	}
2842d31a1825SCarlos Maiolino 	if (dp2_flags) {
2843d31a1825SCarlos Maiolino 		xfs_trans_ichgtime(tp, dp2, dp2_flags);
2844d31a1825SCarlos Maiolino 		xfs_trans_log_inode(tp, dp2, XFS_ILOG_CORE);
2845d31a1825SCarlos Maiolino 	}
2846d31a1825SCarlos Maiolino 	xfs_trans_ichgtime(tp, dp1, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2847d31a1825SCarlos Maiolino 	xfs_trans_log_inode(tp, dp1, XFS_ILOG_CORE);
28482c3234d1SDarrick J. Wong 	return xfs_finish_rename(tp, dfops);
2849eeacd321SDave Chinner 
2850eeacd321SDave Chinner out_trans_abort:
28512c3234d1SDarrick J. Wong 	xfs_defer_cancel(dfops);
28524906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
2853d31a1825SCarlos Maiolino 	return error;
2854d31a1825SCarlos Maiolino }
2855d31a1825SCarlos Maiolino 
2856d31a1825SCarlos Maiolino /*
28577dcf5c3eSDave Chinner  * xfs_rename_alloc_whiteout()
28587dcf5c3eSDave Chinner  *
28597dcf5c3eSDave Chinner  * Return a referenced, unlinked, unlocked inode that that can be used as a
28607dcf5c3eSDave Chinner  * whiteout in a rename transaction. We use a tmpfile inode here so that if we
28617dcf5c3eSDave Chinner  * crash between allocating the inode and linking it into the rename transaction
28627dcf5c3eSDave Chinner  * recovery will free the inode and we won't leak it.
28637dcf5c3eSDave Chinner  */
28647dcf5c3eSDave Chinner static int
28657dcf5c3eSDave Chinner xfs_rename_alloc_whiteout(
28667dcf5c3eSDave Chinner 	struct xfs_inode	*dp,
28677dcf5c3eSDave Chinner 	struct xfs_inode	**wip)
28687dcf5c3eSDave Chinner {
28697dcf5c3eSDave Chinner 	struct xfs_inode	*tmpfile;
28707dcf5c3eSDave Chinner 	int			error;
28717dcf5c3eSDave Chinner 
28727dcf5c3eSDave Chinner 	error = xfs_create_tmpfile(dp, NULL, S_IFCHR | WHITEOUT_MODE, &tmpfile);
28737dcf5c3eSDave Chinner 	if (error)
28747dcf5c3eSDave Chinner 		return error;
28757dcf5c3eSDave Chinner 
287622419ac9SBrian Foster 	/*
287722419ac9SBrian Foster 	 * Prepare the tmpfile inode as if it were created through the VFS.
287822419ac9SBrian Foster 	 * Otherwise, the link increment paths will complain about nlink 0->1.
287922419ac9SBrian Foster 	 * Drop the link count as done by d_tmpfile(), complete the inode setup
288022419ac9SBrian Foster 	 * and flag it as linkable.
288122419ac9SBrian Foster 	 */
288222419ac9SBrian Foster 	drop_nlink(VFS_I(tmpfile));
28832b3d1d41SChristoph Hellwig 	xfs_setup_iops(tmpfile);
28847dcf5c3eSDave Chinner 	xfs_finish_inode_setup(tmpfile);
28857dcf5c3eSDave Chinner 	VFS_I(tmpfile)->i_state |= I_LINKABLE;
28867dcf5c3eSDave Chinner 
28877dcf5c3eSDave Chinner 	*wip = tmpfile;
28887dcf5c3eSDave Chinner 	return 0;
28897dcf5c3eSDave Chinner }
28907dcf5c3eSDave Chinner 
28917dcf5c3eSDave Chinner /*
2892f6bba201SDave Chinner  * xfs_rename
2893f6bba201SDave Chinner  */
2894f6bba201SDave Chinner int
2895f6bba201SDave Chinner xfs_rename(
28967dcf5c3eSDave Chinner 	struct xfs_inode	*src_dp,
2897f6bba201SDave Chinner 	struct xfs_name		*src_name,
28987dcf5c3eSDave Chinner 	struct xfs_inode	*src_ip,
28997dcf5c3eSDave Chinner 	struct xfs_inode	*target_dp,
2900f6bba201SDave Chinner 	struct xfs_name		*target_name,
29017dcf5c3eSDave Chinner 	struct xfs_inode	*target_ip,
2902d31a1825SCarlos Maiolino 	unsigned int		flags)
2903f6bba201SDave Chinner {
29047dcf5c3eSDave Chinner 	struct xfs_mount	*mp = src_dp->i_mount;
29057dcf5c3eSDave Chinner 	struct xfs_trans	*tp;
29062c3234d1SDarrick J. Wong 	struct xfs_defer_ops	dfops;
2907f6bba201SDave Chinner 	xfs_fsblock_t		first_block;
29087dcf5c3eSDave Chinner 	struct xfs_inode	*wip = NULL;		/* whiteout inode */
29097dcf5c3eSDave Chinner 	struct xfs_inode	*inodes[__XFS_SORT_INODES];
291095afcf5cSDave Chinner 	int			num_inodes = __XFS_SORT_INODES;
29112b93681fSDave Chinner 	bool			new_parent = (src_dp != target_dp);
2912c19b3b05SDave Chinner 	bool			src_is_directory = S_ISDIR(VFS_I(src_ip)->i_mode);
2913f6bba201SDave Chinner 	int			spaceres;
29147dcf5c3eSDave Chinner 	int			error;
2915f6bba201SDave Chinner 
2916f6bba201SDave Chinner 	trace_xfs_rename(src_dp, target_dp, src_name, target_name);
2917f6bba201SDave Chinner 
2918eeacd321SDave Chinner 	if ((flags & RENAME_EXCHANGE) && !target_ip)
2919eeacd321SDave Chinner 		return -EINVAL;
2920f6bba201SDave Chinner 
29217dcf5c3eSDave Chinner 	/*
29227dcf5c3eSDave Chinner 	 * If we are doing a whiteout operation, allocate the whiteout inode
29237dcf5c3eSDave Chinner 	 * we will be placing at the target and ensure the type is set
29247dcf5c3eSDave Chinner 	 * appropriately.
29257dcf5c3eSDave Chinner 	 */
29267dcf5c3eSDave Chinner 	if (flags & RENAME_WHITEOUT) {
29277dcf5c3eSDave Chinner 		ASSERT(!(flags & (RENAME_NOREPLACE | RENAME_EXCHANGE)));
29287dcf5c3eSDave Chinner 		error = xfs_rename_alloc_whiteout(target_dp, &wip);
29297dcf5c3eSDave Chinner 		if (error)
29307dcf5c3eSDave Chinner 			return error;
2931f6bba201SDave Chinner 
29327dcf5c3eSDave Chinner 		/* setup target dirent info as whiteout */
29337dcf5c3eSDave Chinner 		src_name->type = XFS_DIR3_FT_CHRDEV;
29347dcf5c3eSDave Chinner 	}
29357dcf5c3eSDave Chinner 
29367dcf5c3eSDave Chinner 	xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip, wip,
2937f6bba201SDave Chinner 				inodes, &num_inodes);
2938f6bba201SDave Chinner 
2939f6bba201SDave Chinner 	spaceres = XFS_RENAME_SPACE_RES(mp, target_name->len);
2940253f4911SChristoph Hellwig 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_rename, spaceres, 0, 0, &tp);
29412451337dSDave Chinner 	if (error == -ENOSPC) {
2942f6bba201SDave Chinner 		spaceres = 0;
2943253f4911SChristoph Hellwig 		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_rename, 0, 0, 0,
2944253f4911SChristoph Hellwig 				&tp);
2945f6bba201SDave Chinner 	}
2946445883e8SDave Chinner 	if (error)
2947253f4911SChristoph Hellwig 		goto out_release_wip;
2948f6bba201SDave Chinner 
2949f6bba201SDave Chinner 	/*
2950f6bba201SDave Chinner 	 * Attach the dquots to the inodes
2951f6bba201SDave Chinner 	 */
2952f6bba201SDave Chinner 	error = xfs_qm_vop_rename_dqattach(inodes);
2953445883e8SDave Chinner 	if (error)
2954445883e8SDave Chinner 		goto out_trans_cancel;
2955f6bba201SDave Chinner 
2956f6bba201SDave Chinner 	/*
2957f6bba201SDave Chinner 	 * Lock all the participating inodes. Depending upon whether
2958f6bba201SDave Chinner 	 * the target_name exists in the target directory, and
2959f6bba201SDave Chinner 	 * whether the target directory is the same as the source
2960f6bba201SDave Chinner 	 * directory, we can lock from 2 to 4 inodes.
2961f6bba201SDave Chinner 	 */
2962dbad7c99SDave Chinner 	if (!new_parent)
2963dbad7c99SDave Chinner 		xfs_ilock(src_dp, XFS_IOLOCK_EXCL | XFS_IOLOCK_PARENT);
2964dbad7c99SDave Chinner 	else
2965dbad7c99SDave Chinner 		xfs_lock_two_inodes(src_dp, target_dp,
2966dbad7c99SDave Chinner 				    XFS_IOLOCK_EXCL | XFS_IOLOCK_PARENT);
2967dbad7c99SDave Chinner 
2968f6bba201SDave Chinner 	xfs_lock_inodes(inodes, num_inodes, XFS_ILOCK_EXCL);
2969f6bba201SDave Chinner 
2970f6bba201SDave Chinner 	/*
2971f6bba201SDave Chinner 	 * Join all the inodes to the transaction. From this point on,
2972f6bba201SDave Chinner 	 * we can rely on either trans_commit or trans_cancel to unlock
2973f6bba201SDave Chinner 	 * them.
2974f6bba201SDave Chinner 	 */
2975dbad7c99SDave Chinner 	xfs_trans_ijoin(tp, src_dp, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
2976f6bba201SDave Chinner 	if (new_parent)
2977dbad7c99SDave Chinner 		xfs_trans_ijoin(tp, target_dp, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
2978f6bba201SDave Chinner 	xfs_trans_ijoin(tp, src_ip, XFS_ILOCK_EXCL);
2979f6bba201SDave Chinner 	if (target_ip)
2980f6bba201SDave Chinner 		xfs_trans_ijoin(tp, target_ip, XFS_ILOCK_EXCL);
29817dcf5c3eSDave Chinner 	if (wip)
29827dcf5c3eSDave Chinner 		xfs_trans_ijoin(tp, wip, XFS_ILOCK_EXCL);
2983f6bba201SDave Chinner 
2984f6bba201SDave Chinner 	/*
2985f6bba201SDave Chinner 	 * If we are using project inheritance, we only allow renames
2986f6bba201SDave Chinner 	 * into our tree when the project IDs are the same; else the
2987f6bba201SDave Chinner 	 * tree quota mechanism would be circumvented.
2988f6bba201SDave Chinner 	 */
2989f6bba201SDave Chinner 	if (unlikely((target_dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2990f6bba201SDave Chinner 		     (xfs_get_projid(target_dp) != xfs_get_projid(src_ip)))) {
29912451337dSDave Chinner 		error = -EXDEV;
2992445883e8SDave Chinner 		goto out_trans_cancel;
2993f6bba201SDave Chinner 	}
2994f6bba201SDave Chinner 
29952c3234d1SDarrick J. Wong 	xfs_defer_init(&dfops, &first_block);
2996445883e8SDave Chinner 
2997eeacd321SDave Chinner 	/* RENAME_EXCHANGE is unique from here on. */
2998eeacd321SDave Chinner 	if (flags & RENAME_EXCHANGE)
2999eeacd321SDave Chinner 		return xfs_cross_rename(tp, src_dp, src_name, src_ip,
3000d31a1825SCarlos Maiolino 					target_dp, target_name, target_ip,
30012c3234d1SDarrick J. Wong 					&dfops, &first_block, spaceres);
3002d31a1825SCarlos Maiolino 
3003d31a1825SCarlos Maiolino 	/*
3004f6bba201SDave Chinner 	 * Set up the target.
3005f6bba201SDave Chinner 	 */
3006f6bba201SDave Chinner 	if (target_ip == NULL) {
3007f6bba201SDave Chinner 		/*
3008f6bba201SDave Chinner 		 * If there's no space reservation, check the entry will
3009f6bba201SDave Chinner 		 * fit before actually inserting it.
3010f6bba201SDave Chinner 		 */
301194f3cad5SEric Sandeen 		if (!spaceres) {
301294f3cad5SEric Sandeen 			error = xfs_dir_canenter(tp, target_dp, target_name);
3013f6bba201SDave Chinner 			if (error)
3014445883e8SDave Chinner 				goto out_trans_cancel;
301594f3cad5SEric Sandeen 		}
3016f6bba201SDave Chinner 		/*
3017f6bba201SDave Chinner 		 * If target does not exist and the rename crosses
3018f6bba201SDave Chinner 		 * directories, adjust the target directory link count
3019f6bba201SDave Chinner 		 * to account for the ".." reference from the new entry.
3020f6bba201SDave Chinner 		 */
3021f6bba201SDave Chinner 		error = xfs_dir_createname(tp, target_dp, target_name,
3022f6bba201SDave Chinner 						src_ip->i_ino, &first_block,
30232c3234d1SDarrick J. Wong 						&dfops, spaceres);
3024f6bba201SDave Chinner 		if (error)
30254906e215SChristoph Hellwig 			goto out_bmap_cancel;
3026f6bba201SDave Chinner 
3027f6bba201SDave Chinner 		xfs_trans_ichgtime(tp, target_dp,
3028f6bba201SDave Chinner 					XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3029f6bba201SDave Chinner 
3030f6bba201SDave Chinner 		if (new_parent && src_is_directory) {
3031f6bba201SDave Chinner 			error = xfs_bumplink(tp, target_dp);
3032f6bba201SDave Chinner 			if (error)
30334906e215SChristoph Hellwig 				goto out_bmap_cancel;
3034f6bba201SDave Chinner 		}
3035f6bba201SDave Chinner 	} else { /* target_ip != NULL */
3036f6bba201SDave Chinner 		/*
3037f6bba201SDave Chinner 		 * If target exists and it's a directory, check that both
3038f6bba201SDave Chinner 		 * target and source are directories and that target can be
3039f6bba201SDave Chinner 		 * destroyed, or that neither is a directory.
3040f6bba201SDave Chinner 		 */
3041c19b3b05SDave Chinner 		if (S_ISDIR(VFS_I(target_ip)->i_mode)) {
3042f6bba201SDave Chinner 			/*
3043f6bba201SDave Chinner 			 * Make sure target dir is empty.
3044f6bba201SDave Chinner 			 */
3045f6bba201SDave Chinner 			if (!(xfs_dir_isempty(target_ip)) ||
304654d7b5c1SDave Chinner 			    (VFS_I(target_ip)->i_nlink > 2)) {
30472451337dSDave Chinner 				error = -EEXIST;
3048445883e8SDave Chinner 				goto out_trans_cancel;
3049f6bba201SDave Chinner 			}
3050f6bba201SDave Chinner 		}
3051f6bba201SDave Chinner 
3052f6bba201SDave Chinner 		/*
3053f6bba201SDave Chinner 		 * Link the source inode under the target name.
3054f6bba201SDave Chinner 		 * If the source inode is a directory and we are moving
3055f6bba201SDave Chinner 		 * it across directories, its ".." entry will be
3056f6bba201SDave Chinner 		 * inconsistent until we replace that down below.
3057f6bba201SDave Chinner 		 *
3058f6bba201SDave Chinner 		 * In case there is already an entry with the same
3059f6bba201SDave Chinner 		 * name at the destination directory, remove it first.
3060f6bba201SDave Chinner 		 */
3061f6bba201SDave Chinner 		error = xfs_dir_replace(tp, target_dp, target_name,
3062f6bba201SDave Chinner 					src_ip->i_ino,
30632c3234d1SDarrick J. Wong 					&first_block, &dfops, spaceres);
3064f6bba201SDave Chinner 		if (error)
30654906e215SChristoph Hellwig 			goto out_bmap_cancel;
3066f6bba201SDave Chinner 
3067f6bba201SDave Chinner 		xfs_trans_ichgtime(tp, target_dp,
3068f6bba201SDave Chinner 					XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3069f6bba201SDave Chinner 
3070f6bba201SDave Chinner 		/*
3071f6bba201SDave Chinner 		 * Decrement the link count on the target since the target
3072f6bba201SDave Chinner 		 * dir no longer points to it.
3073f6bba201SDave Chinner 		 */
3074f6bba201SDave Chinner 		error = xfs_droplink(tp, target_ip);
3075f6bba201SDave Chinner 		if (error)
30764906e215SChristoph Hellwig 			goto out_bmap_cancel;
3077f6bba201SDave Chinner 
3078f6bba201SDave Chinner 		if (src_is_directory) {
3079f6bba201SDave Chinner 			/*
3080f6bba201SDave Chinner 			 * Drop the link from the old "." entry.
3081f6bba201SDave Chinner 			 */
3082f6bba201SDave Chinner 			error = xfs_droplink(tp, target_ip);
3083f6bba201SDave Chinner 			if (error)
30844906e215SChristoph Hellwig 				goto out_bmap_cancel;
3085f6bba201SDave Chinner 		}
3086f6bba201SDave Chinner 	} /* target_ip != NULL */
3087f6bba201SDave Chinner 
3088f6bba201SDave Chinner 	/*
3089f6bba201SDave Chinner 	 * Remove the source.
3090f6bba201SDave Chinner 	 */
3091f6bba201SDave Chinner 	if (new_parent && src_is_directory) {
3092f6bba201SDave Chinner 		/*
3093f6bba201SDave Chinner 		 * Rewrite the ".." entry to point to the new
3094f6bba201SDave Chinner 		 * directory.
3095f6bba201SDave Chinner 		 */
3096f6bba201SDave Chinner 		error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot,
3097f6bba201SDave Chinner 					target_dp->i_ino,
30982c3234d1SDarrick J. Wong 					&first_block, &dfops, spaceres);
30992451337dSDave Chinner 		ASSERT(error != -EEXIST);
3100f6bba201SDave Chinner 		if (error)
31014906e215SChristoph Hellwig 			goto out_bmap_cancel;
3102f6bba201SDave Chinner 	}
3103f6bba201SDave Chinner 
3104f6bba201SDave Chinner 	/*
3105f6bba201SDave Chinner 	 * We always want to hit the ctime on the source inode.
3106f6bba201SDave Chinner 	 *
3107f6bba201SDave Chinner 	 * This isn't strictly required by the standards since the source
3108f6bba201SDave Chinner 	 * inode isn't really being changed, but old unix file systems did
3109f6bba201SDave Chinner 	 * it and some incremental backup programs won't work without it.
3110f6bba201SDave Chinner 	 */
3111f6bba201SDave Chinner 	xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG);
3112f6bba201SDave Chinner 	xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE);
3113f6bba201SDave Chinner 
3114f6bba201SDave Chinner 	/*
3115f6bba201SDave Chinner 	 * Adjust the link count on src_dp.  This is necessary when
3116f6bba201SDave Chinner 	 * renaming a directory, either within one parent when
3117f6bba201SDave Chinner 	 * the target existed, or across two parent directories.
3118f6bba201SDave Chinner 	 */
3119f6bba201SDave Chinner 	if (src_is_directory && (new_parent || target_ip != NULL)) {
3120f6bba201SDave Chinner 
3121f6bba201SDave Chinner 		/*
3122f6bba201SDave Chinner 		 * Decrement link count on src_directory since the
3123f6bba201SDave Chinner 		 * entry that's moved no longer points to it.
3124f6bba201SDave Chinner 		 */
3125f6bba201SDave Chinner 		error = xfs_droplink(tp, src_dp);
3126f6bba201SDave Chinner 		if (error)
31274906e215SChristoph Hellwig 			goto out_bmap_cancel;
3128f6bba201SDave Chinner 	}
3129f6bba201SDave Chinner 
31307dcf5c3eSDave Chinner 	/*
31317dcf5c3eSDave Chinner 	 * For whiteouts, we only need to update the source dirent with the
31327dcf5c3eSDave Chinner 	 * inode number of the whiteout inode rather than removing it
31337dcf5c3eSDave Chinner 	 * altogether.
31347dcf5c3eSDave Chinner 	 */
31357dcf5c3eSDave Chinner 	if (wip) {
31367dcf5c3eSDave Chinner 		error = xfs_dir_replace(tp, src_dp, src_name, wip->i_ino,
31372c3234d1SDarrick J. Wong 					&first_block, &dfops, spaceres);
31387dcf5c3eSDave Chinner 	} else
3139f6bba201SDave Chinner 		error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino,
31402c3234d1SDarrick J. Wong 					   &first_block, &dfops, spaceres);
3141f6bba201SDave Chinner 	if (error)
31424906e215SChristoph Hellwig 		goto out_bmap_cancel;
3143f6bba201SDave Chinner 
31447dcf5c3eSDave Chinner 	/*
31457dcf5c3eSDave Chinner 	 * For whiteouts, we need to bump the link count on the whiteout inode.
31467dcf5c3eSDave Chinner 	 * This means that failures all the way up to this point leave the inode
31477dcf5c3eSDave Chinner 	 * on the unlinked list and so cleanup is a simple matter of dropping
31487dcf5c3eSDave Chinner 	 * the remaining reference to it. If we fail here after bumping the link
31497dcf5c3eSDave Chinner 	 * count, we're shutting down the filesystem so we'll never see the
31507dcf5c3eSDave Chinner 	 * intermediate state on disk.
31517dcf5c3eSDave Chinner 	 */
31527dcf5c3eSDave Chinner 	if (wip) {
315354d7b5c1SDave Chinner 		ASSERT(VFS_I(wip)->i_nlink == 0);
31547dcf5c3eSDave Chinner 		error = xfs_bumplink(tp, wip);
31557dcf5c3eSDave Chinner 		if (error)
31564906e215SChristoph Hellwig 			goto out_bmap_cancel;
31577dcf5c3eSDave Chinner 		error = xfs_iunlink_remove(tp, wip);
31587dcf5c3eSDave Chinner 		if (error)
31594906e215SChristoph Hellwig 			goto out_bmap_cancel;
31607dcf5c3eSDave Chinner 		xfs_trans_log_inode(tp, wip, XFS_ILOG_CORE);
31617dcf5c3eSDave Chinner 
31627dcf5c3eSDave Chinner 		/*
31637dcf5c3eSDave Chinner 		 * Now we have a real link, clear the "I'm a tmpfile" state
31647dcf5c3eSDave Chinner 		 * flag from the inode so it doesn't accidentally get misused in
31657dcf5c3eSDave Chinner 		 * future.
31667dcf5c3eSDave Chinner 		 */
31677dcf5c3eSDave Chinner 		VFS_I(wip)->i_state &= ~I_LINKABLE;
31687dcf5c3eSDave Chinner 	}
3169f6bba201SDave Chinner 
3170f6bba201SDave Chinner 	xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3171f6bba201SDave Chinner 	xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE);
3172f6bba201SDave Chinner 	if (new_parent)
3173f6bba201SDave Chinner 		xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE);
3174f6bba201SDave Chinner 
31752c3234d1SDarrick J. Wong 	error = xfs_finish_rename(tp, &dfops);
31767dcf5c3eSDave Chinner 	if (wip)
31777dcf5c3eSDave Chinner 		IRELE(wip);
31787dcf5c3eSDave Chinner 	return error;
3179f6bba201SDave Chinner 
3180445883e8SDave Chinner out_bmap_cancel:
31812c3234d1SDarrick J. Wong 	xfs_defer_cancel(&dfops);
3182445883e8SDave Chinner out_trans_cancel:
31834906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
3184253f4911SChristoph Hellwig out_release_wip:
31857dcf5c3eSDave Chinner 	if (wip)
31867dcf5c3eSDave Chinner 		IRELE(wip);
3187f6bba201SDave Chinner 	return error;
3188f6bba201SDave Chinner }
3189f6bba201SDave Chinner 
3190bad55843SDavid Chinner STATIC int
3191bad55843SDavid Chinner xfs_iflush_cluster(
319219429363SDave Chinner 	struct xfs_inode	*ip,
319319429363SDave Chinner 	struct xfs_buf		*bp)
3194bad55843SDavid Chinner {
319519429363SDave Chinner 	struct xfs_mount	*mp = ip->i_mount;
31965017e97dSDave Chinner 	struct xfs_perag	*pag;
3197bad55843SDavid Chinner 	unsigned long		first_index, mask;
3198c8f5f12eSDavid Chinner 	unsigned long		inodes_per_cluster;
319919429363SDave Chinner 	int			cilist_size;
320019429363SDave Chinner 	struct xfs_inode	**cilist;
320119429363SDave Chinner 	struct xfs_inode	*cip;
3202bad55843SDavid Chinner 	int			nr_found;
3203bad55843SDavid Chinner 	int			clcount = 0;
3204bad55843SDavid Chinner 	int			bufwasdelwri;
3205bad55843SDavid Chinner 	int			i;
3206bad55843SDavid Chinner 
32075017e97dSDave Chinner 	pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
3208bad55843SDavid Chinner 
32090f49efd8SJie Liu 	inodes_per_cluster = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog;
321019429363SDave Chinner 	cilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
321119429363SDave Chinner 	cilist = kmem_alloc(cilist_size, KM_MAYFAIL|KM_NOFS);
321219429363SDave Chinner 	if (!cilist)
321344b56e0aSDave Chinner 		goto out_put;
3214bad55843SDavid Chinner 
32150f49efd8SJie Liu 	mask = ~(((mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog)) - 1);
3216bad55843SDavid Chinner 	first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
32171a3e8f3dSDave Chinner 	rcu_read_lock();
3218bad55843SDavid Chinner 	/* really need a gang lookup range call here */
321919429363SDave Chinner 	nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)cilist,
3220c8f5f12eSDavid Chinner 					first_index, inodes_per_cluster);
3221bad55843SDavid Chinner 	if (nr_found == 0)
3222bad55843SDavid Chinner 		goto out_free;
3223bad55843SDavid Chinner 
3224bad55843SDavid Chinner 	for (i = 0; i < nr_found; i++) {
322519429363SDave Chinner 		cip = cilist[i];
322619429363SDave Chinner 		if (cip == ip)
3227bad55843SDavid Chinner 			continue;
32281a3e8f3dSDave Chinner 
32291a3e8f3dSDave Chinner 		/*
32301a3e8f3dSDave Chinner 		 * because this is an RCU protected lookup, we could find a
32311a3e8f3dSDave Chinner 		 * recently freed or even reallocated inode during the lookup.
32321a3e8f3dSDave Chinner 		 * We need to check under the i_flags_lock for a valid inode
32331a3e8f3dSDave Chinner 		 * here. Skip it if it is not valid or the wrong inode.
32341a3e8f3dSDave Chinner 		 */
323519429363SDave Chinner 		spin_lock(&cip->i_flags_lock);
323619429363SDave Chinner 		if (!cip->i_ino ||
323719429363SDave Chinner 		    __xfs_iflags_test(cip, XFS_ISTALE)) {
323819429363SDave Chinner 			spin_unlock(&cip->i_flags_lock);
32391a3e8f3dSDave Chinner 			continue;
32401a3e8f3dSDave Chinner 		}
32415a90e53eSDave Chinner 
32425a90e53eSDave Chinner 		/*
32435a90e53eSDave Chinner 		 * Once we fall off the end of the cluster, no point checking
32445a90e53eSDave Chinner 		 * any more inodes in the list because they will also all be
32455a90e53eSDave Chinner 		 * outside the cluster.
32465a90e53eSDave Chinner 		 */
324719429363SDave Chinner 		if ((XFS_INO_TO_AGINO(mp, cip->i_ino) & mask) != first_index) {
324819429363SDave Chinner 			spin_unlock(&cip->i_flags_lock);
32495a90e53eSDave Chinner 			break;
32505a90e53eSDave Chinner 		}
325119429363SDave Chinner 		spin_unlock(&cip->i_flags_lock);
32521a3e8f3dSDave Chinner 
3253bad55843SDavid Chinner 		/*
3254bad55843SDavid Chinner 		 * Do an un-protected check to see if the inode is dirty and
3255bad55843SDavid Chinner 		 * is a candidate for flushing.  These checks will be repeated
3256bad55843SDavid Chinner 		 * later after the appropriate locks are acquired.
3257bad55843SDavid Chinner 		 */
325819429363SDave Chinner 		if (xfs_inode_clean(cip) && xfs_ipincount(cip) == 0)
3259bad55843SDavid Chinner 			continue;
3260bad55843SDavid Chinner 
3261bad55843SDavid Chinner 		/*
3262bad55843SDavid Chinner 		 * Try to get locks.  If any are unavailable or it is pinned,
3263bad55843SDavid Chinner 		 * then this inode cannot be flushed and is skipped.
3264bad55843SDavid Chinner 		 */
3265bad55843SDavid Chinner 
326619429363SDave Chinner 		if (!xfs_ilock_nowait(cip, XFS_ILOCK_SHARED))
3267bad55843SDavid Chinner 			continue;
326819429363SDave Chinner 		if (!xfs_iflock_nowait(cip)) {
326919429363SDave Chinner 			xfs_iunlock(cip, XFS_ILOCK_SHARED);
3270bad55843SDavid Chinner 			continue;
3271bad55843SDavid Chinner 		}
327219429363SDave Chinner 		if (xfs_ipincount(cip)) {
327319429363SDave Chinner 			xfs_ifunlock(cip);
327419429363SDave Chinner 			xfs_iunlock(cip, XFS_ILOCK_SHARED);
3275bad55843SDavid Chinner 			continue;
3276bad55843SDavid Chinner 		}
3277bad55843SDavid Chinner 
32788a17d7ddSDave Chinner 
32798a17d7ddSDave Chinner 		/*
32808a17d7ddSDave Chinner 		 * Check the inode number again, just to be certain we are not
32818a17d7ddSDave Chinner 		 * racing with freeing in xfs_reclaim_inode(). See the comments
32828a17d7ddSDave Chinner 		 * in that function for more information as to why the initial
32838a17d7ddSDave Chinner 		 * check is not sufficient.
32848a17d7ddSDave Chinner 		 */
328519429363SDave Chinner 		if (!cip->i_ino) {
328619429363SDave Chinner 			xfs_ifunlock(cip);
328719429363SDave Chinner 			xfs_iunlock(cip, XFS_ILOCK_SHARED);
3288bad55843SDavid Chinner 			continue;
3289bad55843SDavid Chinner 		}
3290bad55843SDavid Chinner 
3291bad55843SDavid Chinner 		/*
3292bad55843SDavid Chinner 		 * arriving here means that this inode can be flushed.  First
3293bad55843SDavid Chinner 		 * re-check that it's dirty before flushing.
3294bad55843SDavid Chinner 		 */
329519429363SDave Chinner 		if (!xfs_inode_clean(cip)) {
3296bad55843SDavid Chinner 			int	error;
329719429363SDave Chinner 			error = xfs_iflush_int(cip, bp);
3298bad55843SDavid Chinner 			if (error) {
329919429363SDave Chinner 				xfs_iunlock(cip, XFS_ILOCK_SHARED);
3300bad55843SDavid Chinner 				goto cluster_corrupt_out;
3301bad55843SDavid Chinner 			}
3302bad55843SDavid Chinner 			clcount++;
3303bad55843SDavid Chinner 		} else {
330419429363SDave Chinner 			xfs_ifunlock(cip);
3305bad55843SDavid Chinner 		}
330619429363SDave Chinner 		xfs_iunlock(cip, XFS_ILOCK_SHARED);
3307bad55843SDavid Chinner 	}
3308bad55843SDavid Chinner 
3309bad55843SDavid Chinner 	if (clcount) {
3310ff6d6af2SBill O'Donnell 		XFS_STATS_INC(mp, xs_icluster_flushcnt);
3311ff6d6af2SBill O'Donnell 		XFS_STATS_ADD(mp, xs_icluster_flushinode, clcount);
3312bad55843SDavid Chinner 	}
3313bad55843SDavid Chinner 
3314bad55843SDavid Chinner out_free:
33151a3e8f3dSDave Chinner 	rcu_read_unlock();
331619429363SDave Chinner 	kmem_free(cilist);
331744b56e0aSDave Chinner out_put:
331844b56e0aSDave Chinner 	xfs_perag_put(pag);
3319bad55843SDavid Chinner 	return 0;
3320bad55843SDavid Chinner 
3321bad55843SDavid Chinner 
3322bad55843SDavid Chinner cluster_corrupt_out:
3323bad55843SDavid Chinner 	/*
3324bad55843SDavid Chinner 	 * Corruption detected in the clustering loop.  Invalidate the
3325bad55843SDavid Chinner 	 * inode buffer and shut down the filesystem.
3326bad55843SDavid Chinner 	 */
33271a3e8f3dSDave Chinner 	rcu_read_unlock();
3328bad55843SDavid Chinner 	/*
332943ff2122SChristoph Hellwig 	 * Clean up the buffer.  If it was delwri, just release it --
3330bad55843SDavid Chinner 	 * brelse can handle it with no problems.  If not, shut down the
3331bad55843SDavid Chinner 	 * filesystem before releasing the buffer.
3332bad55843SDavid Chinner 	 */
333343ff2122SChristoph Hellwig 	bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q);
3334bad55843SDavid Chinner 	if (bufwasdelwri)
3335bad55843SDavid Chinner 		xfs_buf_relse(bp);
3336bad55843SDavid Chinner 
3337bad55843SDavid Chinner 	xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
3338bad55843SDavid Chinner 
3339bad55843SDavid Chinner 	if (!bufwasdelwri) {
3340bad55843SDavid Chinner 		/*
3341bad55843SDavid Chinner 		 * Just like incore_relse: if we have b_iodone functions,
3342bad55843SDavid Chinner 		 * mark the buffer as an error and call them.  Otherwise
3343bad55843SDavid Chinner 		 * mark it as stale and brelse.
3344bad55843SDavid Chinner 		 */
3345cb669ca5SChristoph Hellwig 		if (bp->b_iodone) {
3346b0388bf1SDave Chinner 			bp->b_flags &= ~XBF_DONE;
3347c867cb61SChristoph Hellwig 			xfs_buf_stale(bp);
33482451337dSDave Chinner 			xfs_buf_ioerror(bp, -EIO);
3349e8aaba9aSDave Chinner 			xfs_buf_ioend(bp);
3350bad55843SDavid Chinner 		} else {
3351c867cb61SChristoph Hellwig 			xfs_buf_stale(bp);
3352bad55843SDavid Chinner 			xfs_buf_relse(bp);
3353bad55843SDavid Chinner 		}
3354bad55843SDavid Chinner 	}
3355bad55843SDavid Chinner 
3356bad55843SDavid Chinner 	/*
3357bad55843SDavid Chinner 	 * Unlocks the flush lock
3358bad55843SDavid Chinner 	 */
335919429363SDave Chinner 	xfs_iflush_abort(cip, false);
336019429363SDave Chinner 	kmem_free(cilist);
336144b56e0aSDave Chinner 	xfs_perag_put(pag);
33622451337dSDave Chinner 	return -EFSCORRUPTED;
3363bad55843SDavid Chinner }
3364bad55843SDavid Chinner 
33651da177e4SLinus Torvalds /*
33664c46819aSChristoph Hellwig  * Flush dirty inode metadata into the backing buffer.
33674c46819aSChristoph Hellwig  *
33684c46819aSChristoph Hellwig  * The caller must have the inode lock and the inode flush lock held.  The
33694c46819aSChristoph Hellwig  * inode lock will still be held upon return to the caller, and the inode
33704c46819aSChristoph Hellwig  * flush lock will be released after the inode has reached the disk.
33714c46819aSChristoph Hellwig  *
33724c46819aSChristoph Hellwig  * The caller must write out the buffer returned in *bpp and release it.
33731da177e4SLinus Torvalds  */
33741da177e4SLinus Torvalds int
33751da177e4SLinus Torvalds xfs_iflush(
33764c46819aSChristoph Hellwig 	struct xfs_inode	*ip,
33774c46819aSChristoph Hellwig 	struct xfs_buf		**bpp)
33781da177e4SLinus Torvalds {
33794c46819aSChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
3380b1438f47SDave Chinner 	struct xfs_buf		*bp = NULL;
33814c46819aSChristoph Hellwig 	struct xfs_dinode	*dip;
33821da177e4SLinus Torvalds 	int			error;
33831da177e4SLinus Torvalds 
3384ff6d6af2SBill O'Donnell 	XFS_STATS_INC(mp, xs_iflush_count);
33851da177e4SLinus Torvalds 
3386579aa9caSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
3387474fce06SChristoph Hellwig 	ASSERT(xfs_isiflocked(ip));
33881da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
33898096b1ebSChristoph Hellwig 	       ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
33901da177e4SLinus Torvalds 
33914c46819aSChristoph Hellwig 	*bpp = NULL;
33921da177e4SLinus Torvalds 
33931da177e4SLinus Torvalds 	xfs_iunpin_wait(ip);
33941da177e4SLinus Torvalds 
33951da177e4SLinus Torvalds 	/*
33964b6a4688SDave Chinner 	 * For stale inodes we cannot rely on the backing buffer remaining
33974b6a4688SDave Chinner 	 * stale in cache for the remaining life of the stale inode and so
3398475ee413SChristoph Hellwig 	 * xfs_imap_to_bp() below may give us a buffer that no longer contains
33994b6a4688SDave Chinner 	 * inodes below. We have to check this after ensuring the inode is
34004b6a4688SDave Chinner 	 * unpinned so that it is safe to reclaim the stale inode after the
34014b6a4688SDave Chinner 	 * flush call.
34024b6a4688SDave Chinner 	 */
34034b6a4688SDave Chinner 	if (xfs_iflags_test(ip, XFS_ISTALE)) {
34044b6a4688SDave Chinner 		xfs_ifunlock(ip);
34054b6a4688SDave Chinner 		return 0;
34064b6a4688SDave Chinner 	}
34074b6a4688SDave Chinner 
34084b6a4688SDave Chinner 	/*
34091da177e4SLinus Torvalds 	 * This may have been unpinned because the filesystem is shutting
34101da177e4SLinus Torvalds 	 * down forcibly. If that's the case we must not write this inode
341132ce90a4SChristoph Hellwig 	 * to disk, because the log record didn't make it to disk.
341232ce90a4SChristoph Hellwig 	 *
341332ce90a4SChristoph Hellwig 	 * We also have to remove the log item from the AIL in this case,
341432ce90a4SChristoph Hellwig 	 * as we wait for an empty AIL as part of the unmount process.
34151da177e4SLinus Torvalds 	 */
34161da177e4SLinus Torvalds 	if (XFS_FORCED_SHUTDOWN(mp)) {
34172451337dSDave Chinner 		error = -EIO;
341832ce90a4SChristoph Hellwig 		goto abort_out;
34191da177e4SLinus Torvalds 	}
34201da177e4SLinus Torvalds 
34211da177e4SLinus Torvalds 	/*
3422b1438f47SDave Chinner 	 * Get the buffer containing the on-disk inode. We are doing a try-lock
3423b1438f47SDave Chinner 	 * operation here, so we may get  an EAGAIN error. In that case, we
3424b1438f47SDave Chinner 	 * simply want to return with the inode still dirty.
3425b1438f47SDave Chinner 	 *
3426b1438f47SDave Chinner 	 * If we get any other error, we effectively have a corruption situation
3427b1438f47SDave Chinner 	 * and we cannot flush the inode, so we treat it the same as failing
3428b1438f47SDave Chinner 	 * xfs_iflush_int().
3429a3f74ffbSDavid Chinner 	 */
3430475ee413SChristoph Hellwig 	error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK,
3431475ee413SChristoph Hellwig 			       0);
3432b1438f47SDave Chinner 	if (error == -EAGAIN) {
3433a3f74ffbSDavid Chinner 		xfs_ifunlock(ip);
3434a3f74ffbSDavid Chinner 		return error;
3435a3f74ffbSDavid Chinner 	}
3436b1438f47SDave Chinner 	if (error)
3437b1438f47SDave Chinner 		goto corrupt_out;
3438a3f74ffbSDavid Chinner 
3439a3f74ffbSDavid Chinner 	/*
34401da177e4SLinus Torvalds 	 * First flush out the inode that xfs_iflush was called with.
34411da177e4SLinus Torvalds 	 */
34421da177e4SLinus Torvalds 	error = xfs_iflush_int(ip, bp);
3443bad55843SDavid Chinner 	if (error)
34441da177e4SLinus Torvalds 		goto corrupt_out;
34451da177e4SLinus Torvalds 
34461da177e4SLinus Torvalds 	/*
3447a3f74ffbSDavid Chinner 	 * If the buffer is pinned then push on the log now so we won't
3448a3f74ffbSDavid Chinner 	 * get stuck waiting in the write for too long.
3449a3f74ffbSDavid Chinner 	 */
3450811e64c7SChandra Seetharaman 	if (xfs_buf_ispinned(bp))
3451a14a348bSChristoph Hellwig 		xfs_log_force(mp, 0);
3452a3f74ffbSDavid Chinner 
3453a3f74ffbSDavid Chinner 	/*
34541da177e4SLinus Torvalds 	 * inode clustering:
34551da177e4SLinus Torvalds 	 * see if other inodes can be gathered into this write
34561da177e4SLinus Torvalds 	 */
3457bad55843SDavid Chinner 	error = xfs_iflush_cluster(ip, bp);
3458bad55843SDavid Chinner 	if (error)
34591da177e4SLinus Torvalds 		goto cluster_corrupt_out;
34601da177e4SLinus Torvalds 
34614c46819aSChristoph Hellwig 	*bpp = bp;
34624c46819aSChristoph Hellwig 	return 0;
34631da177e4SLinus Torvalds 
34641da177e4SLinus Torvalds corrupt_out:
3465b1438f47SDave Chinner 	if (bp)
34661da177e4SLinus Torvalds 		xfs_buf_relse(bp);
34677d04a335SNathan Scott 	xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
34681da177e4SLinus Torvalds cluster_corrupt_out:
34692451337dSDave Chinner 	error = -EFSCORRUPTED;
347032ce90a4SChristoph Hellwig abort_out:
34711da177e4SLinus Torvalds 	/*
34721da177e4SLinus Torvalds 	 * Unlocks the flush lock
34731da177e4SLinus Torvalds 	 */
347404913fddSDave Chinner 	xfs_iflush_abort(ip, false);
347532ce90a4SChristoph Hellwig 	return error;
34761da177e4SLinus Torvalds }
34771da177e4SLinus Torvalds 
34781da177e4SLinus Torvalds STATIC int
34791da177e4SLinus Torvalds xfs_iflush_int(
348093848a99SChristoph Hellwig 	struct xfs_inode	*ip,
348193848a99SChristoph Hellwig 	struct xfs_buf		*bp)
34821da177e4SLinus Torvalds {
348393848a99SChristoph Hellwig 	struct xfs_inode_log_item *iip = ip->i_itemp;
348493848a99SChristoph Hellwig 	struct xfs_dinode	*dip;
348593848a99SChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
34861da177e4SLinus Torvalds 
3487579aa9caSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
3488474fce06SChristoph Hellwig 	ASSERT(xfs_isiflocked(ip));
34891da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
34908096b1ebSChristoph Hellwig 	       ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
349193848a99SChristoph Hellwig 	ASSERT(iip != NULL && iip->ili_fields != 0);
3492263997a6SDave Chinner 	ASSERT(ip->i_d.di_version > 1);
34931da177e4SLinus Torvalds 
34941da177e4SLinus Torvalds 	/* set *dip = inode's place in the buffer */
349588ee2df7SChristoph Hellwig 	dip = xfs_buf_offset(bp, ip->i_imap.im_boffset);
34961da177e4SLinus Torvalds 
349769ef921bSChristoph Hellwig 	if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
34981da177e4SLinus Torvalds 			       mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
34996a19d939SDave Chinner 		xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
35006a19d939SDave Chinner 			"%s: Bad inode %Lu magic number 0x%x, ptr 0x%p",
35016a19d939SDave Chinner 			__func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
35021da177e4SLinus Torvalds 		goto corrupt_out;
35031da177e4SLinus Torvalds 	}
3504c19b3b05SDave Chinner 	if (S_ISREG(VFS_I(ip)->i_mode)) {
35051da177e4SLinus Torvalds 		if (XFS_TEST_ERROR(
35061da177e4SLinus Torvalds 		    (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
35071da177e4SLinus Torvalds 		    (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
35081da177e4SLinus Torvalds 		    mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
35096a19d939SDave Chinner 			xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
35106a19d939SDave Chinner 				"%s: Bad regular inode %Lu, ptr 0x%p",
35116a19d939SDave Chinner 				__func__, ip->i_ino, ip);
35121da177e4SLinus Torvalds 			goto corrupt_out;
35131da177e4SLinus Torvalds 		}
3514c19b3b05SDave Chinner 	} else if (S_ISDIR(VFS_I(ip)->i_mode)) {
35151da177e4SLinus Torvalds 		if (XFS_TEST_ERROR(
35161da177e4SLinus Torvalds 		    (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
35171da177e4SLinus Torvalds 		    (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
35181da177e4SLinus Torvalds 		    (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
35191da177e4SLinus Torvalds 		    mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
35206a19d939SDave Chinner 			xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
35216a19d939SDave Chinner 				"%s: Bad directory inode %Lu, ptr 0x%p",
35226a19d939SDave Chinner 				__func__, ip->i_ino, ip);
35231da177e4SLinus Torvalds 			goto corrupt_out;
35241da177e4SLinus Torvalds 		}
35251da177e4SLinus Torvalds 	}
35261da177e4SLinus Torvalds 	if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
35271da177e4SLinus Torvalds 				ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
35281da177e4SLinus Torvalds 				XFS_RANDOM_IFLUSH_5)) {
35296a19d939SDave Chinner 		xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
35306a19d939SDave Chinner 			"%s: detected corrupt incore inode %Lu, "
35316a19d939SDave Chinner 			"total extents = %d, nblocks = %Ld, ptr 0x%p",
35326a19d939SDave Chinner 			__func__, ip->i_ino,
35331da177e4SLinus Torvalds 			ip->i_d.di_nextents + ip->i_d.di_anextents,
35346a19d939SDave Chinner 			ip->i_d.di_nblocks, ip);
35351da177e4SLinus Torvalds 		goto corrupt_out;
35361da177e4SLinus Torvalds 	}
35371da177e4SLinus Torvalds 	if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
35381da177e4SLinus Torvalds 				mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
35396a19d939SDave Chinner 		xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
35406a19d939SDave Chinner 			"%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
35416a19d939SDave Chinner 			__func__, ip->i_ino, ip->i_d.di_forkoff, ip);
35421da177e4SLinus Torvalds 		goto corrupt_out;
35431da177e4SLinus Torvalds 	}
3544e60896d8SDave Chinner 
35451da177e4SLinus Torvalds 	/*
3546263997a6SDave Chinner 	 * Inode item log recovery for v2 inodes are dependent on the
3547e60896d8SDave Chinner 	 * di_flushiter count for correct sequencing. We bump the flush
3548e60896d8SDave Chinner 	 * iteration count so we can detect flushes which postdate a log record
3549e60896d8SDave Chinner 	 * during recovery. This is redundant as we now log every change and
3550e60896d8SDave Chinner 	 * hence this can't happen but we need to still do it to ensure
3551e60896d8SDave Chinner 	 * backwards compatibility with old kernels that predate logging all
3552e60896d8SDave Chinner 	 * inode changes.
35531da177e4SLinus Torvalds 	 */
3554e60896d8SDave Chinner 	if (ip->i_d.di_version < 3)
35551da177e4SLinus Torvalds 		ip->i_d.di_flushiter++;
35561da177e4SLinus Torvalds 
35571da177e4SLinus Torvalds 	/*
35583987848cSDave Chinner 	 * Copy the dirty parts of the inode into the on-disk inode.  We always
35593987848cSDave Chinner 	 * copy out the core of the inode, because if the inode is dirty at all
35603987848cSDave Chinner 	 * the core must be.
35611da177e4SLinus Torvalds 	 */
356293f958f9SDave Chinner 	xfs_inode_to_disk(ip, dip, iip->ili_item.li_lsn);
35631da177e4SLinus Torvalds 
35641da177e4SLinus Torvalds 	/* Wrap, we never let the log put out DI_MAX_FLUSH */
35651da177e4SLinus Torvalds 	if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
35661da177e4SLinus Torvalds 		ip->i_d.di_flushiter = 0;
35671da177e4SLinus Torvalds 
3568fd9fdba6SEric Sandeen 	xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK);
3569e4ac967bSDavid Chinner 	if (XFS_IFORK_Q(ip))
3570fd9fdba6SEric Sandeen 		xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK);
35711da177e4SLinus Torvalds 	xfs_inobp_check(mp, bp);
35721da177e4SLinus Torvalds 
35731da177e4SLinus Torvalds 	/*
3574f5d8d5c4SChristoph Hellwig 	 * We've recorded everything logged in the inode, so we'd like to clear
3575f5d8d5c4SChristoph Hellwig 	 * the ili_fields bits so we don't log and flush things unnecessarily.
3576f5d8d5c4SChristoph Hellwig 	 * However, we can't stop logging all this information until the data
3577f5d8d5c4SChristoph Hellwig 	 * we've copied into the disk buffer is written to disk.  If we did we
3578f5d8d5c4SChristoph Hellwig 	 * might overwrite the copy of the inode in the log with all the data
3579f5d8d5c4SChristoph Hellwig 	 * after re-logging only part of it, and in the face of a crash we
3580f5d8d5c4SChristoph Hellwig 	 * wouldn't have all the data we need to recover.
35811da177e4SLinus Torvalds 	 *
3582f5d8d5c4SChristoph Hellwig 	 * What we do is move the bits to the ili_last_fields field.  When
3583f5d8d5c4SChristoph Hellwig 	 * logging the inode, these bits are moved back to the ili_fields field.
3584f5d8d5c4SChristoph Hellwig 	 * In the xfs_iflush_done() routine we clear ili_last_fields, since we
3585f5d8d5c4SChristoph Hellwig 	 * know that the information those bits represent is permanently on
3586f5d8d5c4SChristoph Hellwig 	 * disk.  As long as the flush completes before the inode is logged
3587f5d8d5c4SChristoph Hellwig 	 * again, then both ili_fields and ili_last_fields will be cleared.
35881da177e4SLinus Torvalds 	 *
3589f5d8d5c4SChristoph Hellwig 	 * We can play with the ili_fields bits here, because the inode lock
3590f5d8d5c4SChristoph Hellwig 	 * must be held exclusively in order to set bits there and the flush
3591f5d8d5c4SChristoph Hellwig 	 * lock protects the ili_last_fields bits.  Set ili_logged so the flush
3592f5d8d5c4SChristoph Hellwig 	 * done routine can tell whether or not to look in the AIL.  Also, store
3593f5d8d5c4SChristoph Hellwig 	 * the current LSN of the inode so that we can tell whether the item has
3594f5d8d5c4SChristoph Hellwig 	 * moved in the AIL from xfs_iflush_done().  In order to read the lsn we
3595f5d8d5c4SChristoph Hellwig 	 * need the AIL lock, because it is a 64 bit value that cannot be read
3596f5d8d5c4SChristoph Hellwig 	 * atomically.
35971da177e4SLinus Torvalds 	 */
3598f5d8d5c4SChristoph Hellwig 	iip->ili_last_fields = iip->ili_fields;
3599f5d8d5c4SChristoph Hellwig 	iip->ili_fields = 0;
3600fc0561ceSDave Chinner 	iip->ili_fsync_fields = 0;
36011da177e4SLinus Torvalds 	iip->ili_logged = 1;
36021da177e4SLinus Torvalds 
36037b2e2a31SDavid Chinner 	xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
36047b2e2a31SDavid Chinner 				&iip->ili_item.li_lsn);
36051da177e4SLinus Torvalds 
36061da177e4SLinus Torvalds 	/*
36071da177e4SLinus Torvalds 	 * Attach the function xfs_iflush_done to the inode's
36081da177e4SLinus Torvalds 	 * buffer.  This will remove the inode from the AIL
36091da177e4SLinus Torvalds 	 * and unlock the inode's flush lock when the inode is
36101da177e4SLinus Torvalds 	 * completely written to disk.
36111da177e4SLinus Torvalds 	 */
3612ca30b2a7SChristoph Hellwig 	xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
36131da177e4SLinus Torvalds 
361493848a99SChristoph Hellwig 	/* generate the checksum. */
361593848a99SChristoph Hellwig 	xfs_dinode_calc_crc(mp, dip);
361693848a99SChristoph Hellwig 
3617adadbeefSChristoph Hellwig 	ASSERT(bp->b_fspriv != NULL);
3618cb669ca5SChristoph Hellwig 	ASSERT(bp->b_iodone != NULL);
36191da177e4SLinus Torvalds 	return 0;
36201da177e4SLinus Torvalds 
36211da177e4SLinus Torvalds corrupt_out:
36222451337dSDave Chinner 	return -EFSCORRUPTED;
36231da177e4SLinus Torvalds }
3624