xref: /openbmc/linux/fs/xfs/xfs_inode.c (revision 93f958f9c41f0bfd10627a2279457df64004d957)
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"
28a4fbe6abSDave Chinner #include "xfs_inode.h"
2957062787SDave Chinner #include "xfs_da_format.h"
30c24b5dfaSDave Chinner #include "xfs_da_btree.h"
31c24b5dfaSDave Chinner #include "xfs_dir2.h"
32a844f451SNathan Scott #include "xfs_attr_sf.h"
33c24b5dfaSDave Chinner #include "xfs_attr.h"
34239880efSDave Chinner #include "xfs_trans_space.h"
35239880efSDave Chinner #include "xfs_trans.h"
361da177e4SLinus Torvalds #include "xfs_buf_item.h"
37a844f451SNathan Scott #include "xfs_inode_item.h"
38a844f451SNathan Scott #include "xfs_ialloc.h"
39a844f451SNathan Scott #include "xfs_bmap.h"
4068988114SDave Chinner #include "xfs_bmap_util.h"
411da177e4SLinus Torvalds #include "xfs_error.h"
421da177e4SLinus Torvalds #include "xfs_quota.h"
432a82b8beSDavid Chinner #include "xfs_filestream.h"
4493848a99SChristoph Hellwig #include "xfs_cksum.h"
450b1b213fSChristoph Hellwig #include "xfs_trace.h"
4633479e05SDave Chinner #include "xfs_icache.h"
47c24b5dfaSDave Chinner #include "xfs_symlink.h"
48239880efSDave Chinner #include "xfs_trans_priv.h"
49239880efSDave Chinner #include "xfs_log.h"
50a4fbe6abSDave Chinner #include "xfs_bmap_btree.h"
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds kmem_zone_t *xfs_inode_zone;
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds /*
558f04c47aSChristoph Hellwig  * Used in xfs_itruncate_extents().  This is the maximum number of extents
561da177e4SLinus Torvalds  * freed from a file in a single transaction.
571da177e4SLinus Torvalds  */
581da177e4SLinus Torvalds #define	XFS_ITRUNC_MAX_EXTENTS	2
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds STATIC int xfs_iflush_int(xfs_inode_t *, xfs_buf_t *);
611da177e4SLinus Torvalds 
62ab297431SZhi Yong Wu STATIC int xfs_iunlink_remove(xfs_trans_t *, xfs_inode_t *);
63ab297431SZhi Yong Wu 
642a0ec1d9SDave Chinner /*
652a0ec1d9SDave Chinner  * helper function to extract extent size hint from inode
662a0ec1d9SDave Chinner  */
672a0ec1d9SDave Chinner xfs_extlen_t
682a0ec1d9SDave Chinner xfs_get_extsz_hint(
692a0ec1d9SDave Chinner 	struct xfs_inode	*ip)
702a0ec1d9SDave Chinner {
712a0ec1d9SDave Chinner 	if ((ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE) && ip->i_d.di_extsize)
722a0ec1d9SDave Chinner 		return ip->i_d.di_extsize;
732a0ec1d9SDave Chinner 	if (XFS_IS_REALTIME_INODE(ip))
742a0ec1d9SDave Chinner 		return ip->i_mount->m_sb.sb_rextsize;
752a0ec1d9SDave Chinner 	return 0;
762a0ec1d9SDave Chinner }
772a0ec1d9SDave Chinner 
78fa96acadSDave Chinner /*
79efa70be1SChristoph Hellwig  * These two are wrapper routines around the xfs_ilock() routine used to
80efa70be1SChristoph Hellwig  * centralize some grungy code.  They are used in places that wish to lock the
81efa70be1SChristoph Hellwig  * inode solely for reading the extents.  The reason these places can't just
82efa70be1SChristoph Hellwig  * call xfs_ilock(ip, XFS_ILOCK_SHARED) is that the inode lock also guards to
83efa70be1SChristoph Hellwig  * bringing in of the extents from disk for a file in b-tree format.  If the
84efa70be1SChristoph Hellwig  * inode is in b-tree format, then we need to lock the inode exclusively until
85efa70be1SChristoph Hellwig  * the extents are read in.  Locking it exclusively all the time would limit
86efa70be1SChristoph Hellwig  * our parallelism unnecessarily, though.  What we do instead is check to see
87efa70be1SChristoph Hellwig  * if the extents have been read in yet, and only lock the inode exclusively
88efa70be1SChristoph Hellwig  * if they have not.
89fa96acadSDave Chinner  *
90efa70be1SChristoph Hellwig  * The functions return a value which should be given to the corresponding
9101f4f327SChristoph Hellwig  * xfs_iunlock() call.
92fa96acadSDave Chinner  */
93fa96acadSDave Chinner uint
94309ecac8SChristoph Hellwig xfs_ilock_data_map_shared(
95309ecac8SChristoph Hellwig 	struct xfs_inode	*ip)
96fa96acadSDave Chinner {
97309ecac8SChristoph Hellwig 	uint			lock_mode = XFS_ILOCK_SHARED;
98fa96acadSDave Chinner 
99309ecac8SChristoph Hellwig 	if (ip->i_d.di_format == XFS_DINODE_FMT_BTREE &&
100309ecac8SChristoph Hellwig 	    (ip->i_df.if_flags & XFS_IFEXTENTS) == 0)
101fa96acadSDave Chinner 		lock_mode = XFS_ILOCK_EXCL;
102fa96acadSDave Chinner 	xfs_ilock(ip, lock_mode);
103fa96acadSDave Chinner 	return lock_mode;
104fa96acadSDave Chinner }
105fa96acadSDave Chinner 
106efa70be1SChristoph Hellwig uint
107efa70be1SChristoph Hellwig xfs_ilock_attr_map_shared(
108efa70be1SChristoph Hellwig 	struct xfs_inode	*ip)
109fa96acadSDave Chinner {
110efa70be1SChristoph Hellwig 	uint			lock_mode = XFS_ILOCK_SHARED;
111efa70be1SChristoph Hellwig 
112efa70be1SChristoph Hellwig 	if (ip->i_d.di_aformat == XFS_DINODE_FMT_BTREE &&
113efa70be1SChristoph Hellwig 	    (ip->i_afp->if_flags & XFS_IFEXTENTS) == 0)
114efa70be1SChristoph Hellwig 		lock_mode = XFS_ILOCK_EXCL;
115efa70be1SChristoph Hellwig 	xfs_ilock(ip, lock_mode);
116efa70be1SChristoph Hellwig 	return lock_mode;
117fa96acadSDave Chinner }
118fa96acadSDave Chinner 
119fa96acadSDave Chinner /*
120653c60b6SDave Chinner  * The xfs inode contains 3 multi-reader locks: the i_iolock the i_mmap_lock and
121653c60b6SDave Chinner  * the i_lock.  This routine allows various combinations of the locks to be
122653c60b6SDave Chinner  * obtained.
123fa96acadSDave Chinner  *
124653c60b6SDave Chinner  * The 3 locks should always be ordered so that the IO lock is obtained first,
125653c60b6SDave Chinner  * the mmap lock second and the ilock last in order to prevent deadlock.
126fa96acadSDave Chinner  *
127653c60b6SDave Chinner  * Basic locking order:
128653c60b6SDave Chinner  *
129653c60b6SDave Chinner  * i_iolock -> i_mmap_lock -> page_lock -> i_ilock
130653c60b6SDave Chinner  *
131653c60b6SDave Chinner  * mmap_sem locking order:
132653c60b6SDave Chinner  *
133653c60b6SDave Chinner  * i_iolock -> page lock -> mmap_sem
134653c60b6SDave Chinner  * mmap_sem -> i_mmap_lock -> page_lock
135653c60b6SDave Chinner  *
136653c60b6SDave Chinner  * The difference in mmap_sem locking order mean that we cannot hold the
137653c60b6SDave Chinner  * i_mmap_lock over syscall based read(2)/write(2) based IO. These IO paths can
138653c60b6SDave Chinner  * fault in pages during copy in/out (for buffered IO) or require the mmap_sem
139653c60b6SDave Chinner  * in get_user_pages() to map the user pages into the kernel address space for
140653c60b6SDave Chinner  * direct IO. Similarly the i_iolock cannot be taken inside a page fault because
141653c60b6SDave Chinner  * page faults already hold the mmap_sem.
142653c60b6SDave Chinner  *
143653c60b6SDave Chinner  * Hence to serialise fully against both syscall and mmap based IO, we need to
144653c60b6SDave Chinner  * take both the i_iolock and the i_mmap_lock. These locks should *only* be both
145653c60b6SDave Chinner  * taken in places where we need to invalidate the page cache in a race
146653c60b6SDave Chinner  * free manner (e.g. truncate, hole punch and other extent manipulation
147653c60b6SDave Chinner  * functions).
148fa96acadSDave Chinner  */
149fa96acadSDave Chinner void
150fa96acadSDave Chinner xfs_ilock(
151fa96acadSDave Chinner 	xfs_inode_t		*ip,
152fa96acadSDave Chinner 	uint			lock_flags)
153fa96acadSDave Chinner {
154fa96acadSDave Chinner 	trace_xfs_ilock(ip, lock_flags, _RET_IP_);
155fa96acadSDave Chinner 
156fa96acadSDave Chinner 	/*
157fa96acadSDave Chinner 	 * You can't set both SHARED and EXCL for the same lock,
158fa96acadSDave Chinner 	 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
159fa96acadSDave Chinner 	 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
160fa96acadSDave Chinner 	 */
161fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
162fa96acadSDave Chinner 	       (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
163653c60b6SDave Chinner 	ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
164653c60b6SDave Chinner 	       (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
165fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
166fa96acadSDave Chinner 	       (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
1670952c818SDave Chinner 	ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
168fa96acadSDave Chinner 
169fa96acadSDave Chinner 	if (lock_flags & XFS_IOLOCK_EXCL)
170fa96acadSDave Chinner 		mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
171fa96acadSDave Chinner 	else if (lock_flags & XFS_IOLOCK_SHARED)
172fa96acadSDave Chinner 		mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
173fa96acadSDave Chinner 
174653c60b6SDave Chinner 	if (lock_flags & XFS_MMAPLOCK_EXCL)
175653c60b6SDave Chinner 		mrupdate_nested(&ip->i_mmaplock, XFS_MMAPLOCK_DEP(lock_flags));
176653c60b6SDave Chinner 	else if (lock_flags & XFS_MMAPLOCK_SHARED)
177653c60b6SDave Chinner 		mraccess_nested(&ip->i_mmaplock, XFS_MMAPLOCK_DEP(lock_flags));
178653c60b6SDave Chinner 
179fa96acadSDave Chinner 	if (lock_flags & XFS_ILOCK_EXCL)
180fa96acadSDave Chinner 		mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
181fa96acadSDave Chinner 	else if (lock_flags & XFS_ILOCK_SHARED)
182fa96acadSDave Chinner 		mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
183fa96acadSDave Chinner }
184fa96acadSDave Chinner 
185fa96acadSDave Chinner /*
186fa96acadSDave Chinner  * This is just like xfs_ilock(), except that the caller
187fa96acadSDave Chinner  * is guaranteed not to sleep.  It returns 1 if it gets
188fa96acadSDave Chinner  * the requested locks and 0 otherwise.  If the IO lock is
189fa96acadSDave Chinner  * obtained but the inode lock cannot be, then the IO lock
190fa96acadSDave Chinner  * is dropped before returning.
191fa96acadSDave Chinner  *
192fa96acadSDave Chinner  * ip -- the inode being locked
193fa96acadSDave Chinner  * lock_flags -- this parameter indicates the inode's locks to be
194fa96acadSDave Chinner  *       to be locked.  See the comment for xfs_ilock() for a list
195fa96acadSDave Chinner  *	 of valid values.
196fa96acadSDave Chinner  */
197fa96acadSDave Chinner int
198fa96acadSDave Chinner xfs_ilock_nowait(
199fa96acadSDave Chinner 	xfs_inode_t		*ip,
200fa96acadSDave Chinner 	uint			lock_flags)
201fa96acadSDave Chinner {
202fa96acadSDave Chinner 	trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_);
203fa96acadSDave Chinner 
204fa96acadSDave Chinner 	/*
205fa96acadSDave Chinner 	 * You can't set both SHARED and EXCL for the same lock,
206fa96acadSDave Chinner 	 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
207fa96acadSDave Chinner 	 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
208fa96acadSDave Chinner 	 */
209fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
210fa96acadSDave Chinner 	       (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
211653c60b6SDave Chinner 	ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
212653c60b6SDave Chinner 	       (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
213fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
214fa96acadSDave Chinner 	       (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
2150952c818SDave Chinner 	ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
216fa96acadSDave Chinner 
217fa96acadSDave Chinner 	if (lock_flags & XFS_IOLOCK_EXCL) {
218fa96acadSDave Chinner 		if (!mrtryupdate(&ip->i_iolock))
219fa96acadSDave Chinner 			goto out;
220fa96acadSDave Chinner 	} else if (lock_flags & XFS_IOLOCK_SHARED) {
221fa96acadSDave Chinner 		if (!mrtryaccess(&ip->i_iolock))
222fa96acadSDave Chinner 			goto out;
223fa96acadSDave Chinner 	}
224653c60b6SDave Chinner 
225653c60b6SDave Chinner 	if (lock_flags & XFS_MMAPLOCK_EXCL) {
226653c60b6SDave Chinner 		if (!mrtryupdate(&ip->i_mmaplock))
227653c60b6SDave Chinner 			goto out_undo_iolock;
228653c60b6SDave Chinner 	} else if (lock_flags & XFS_MMAPLOCK_SHARED) {
229653c60b6SDave Chinner 		if (!mrtryaccess(&ip->i_mmaplock))
230653c60b6SDave Chinner 			goto out_undo_iolock;
231653c60b6SDave Chinner 	}
232653c60b6SDave Chinner 
233fa96acadSDave Chinner 	if (lock_flags & XFS_ILOCK_EXCL) {
234fa96acadSDave Chinner 		if (!mrtryupdate(&ip->i_lock))
235653c60b6SDave Chinner 			goto out_undo_mmaplock;
236fa96acadSDave Chinner 	} else if (lock_flags & XFS_ILOCK_SHARED) {
237fa96acadSDave Chinner 		if (!mrtryaccess(&ip->i_lock))
238653c60b6SDave Chinner 			goto out_undo_mmaplock;
239fa96acadSDave Chinner 	}
240fa96acadSDave Chinner 	return 1;
241fa96acadSDave Chinner 
242653c60b6SDave Chinner out_undo_mmaplock:
243653c60b6SDave Chinner 	if (lock_flags & XFS_MMAPLOCK_EXCL)
244653c60b6SDave Chinner 		mrunlock_excl(&ip->i_mmaplock);
245653c60b6SDave Chinner 	else if (lock_flags & XFS_MMAPLOCK_SHARED)
246653c60b6SDave Chinner 		mrunlock_shared(&ip->i_mmaplock);
247fa96acadSDave Chinner out_undo_iolock:
248fa96acadSDave Chinner 	if (lock_flags & XFS_IOLOCK_EXCL)
249fa96acadSDave Chinner 		mrunlock_excl(&ip->i_iolock);
250fa96acadSDave Chinner 	else if (lock_flags & XFS_IOLOCK_SHARED)
251fa96acadSDave Chinner 		mrunlock_shared(&ip->i_iolock);
252fa96acadSDave Chinner out:
253fa96acadSDave Chinner 	return 0;
254fa96acadSDave Chinner }
255fa96acadSDave Chinner 
256fa96acadSDave Chinner /*
257fa96acadSDave Chinner  * xfs_iunlock() is used to drop the inode locks acquired with
258fa96acadSDave Chinner  * xfs_ilock() and xfs_ilock_nowait().  The caller must pass
259fa96acadSDave Chinner  * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
260fa96acadSDave Chinner  * that we know which locks to drop.
261fa96acadSDave Chinner  *
262fa96acadSDave Chinner  * ip -- the inode being unlocked
263fa96acadSDave Chinner  * lock_flags -- this parameter indicates the inode's locks to be
264fa96acadSDave Chinner  *       to be unlocked.  See the comment for xfs_ilock() for a list
265fa96acadSDave Chinner  *	 of valid values for this parameter.
266fa96acadSDave Chinner  *
267fa96acadSDave Chinner  */
268fa96acadSDave Chinner void
269fa96acadSDave Chinner xfs_iunlock(
270fa96acadSDave Chinner 	xfs_inode_t		*ip,
271fa96acadSDave Chinner 	uint			lock_flags)
272fa96acadSDave Chinner {
273fa96acadSDave Chinner 	/*
274fa96acadSDave Chinner 	 * You can't set both SHARED and EXCL for the same lock,
275fa96acadSDave Chinner 	 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
276fa96acadSDave Chinner 	 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
277fa96acadSDave Chinner 	 */
278fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
279fa96acadSDave Chinner 	       (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
280653c60b6SDave Chinner 	ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
281653c60b6SDave Chinner 	       (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
282fa96acadSDave Chinner 	ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
283fa96acadSDave Chinner 	       (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
2840952c818SDave Chinner 	ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
285fa96acadSDave Chinner 	ASSERT(lock_flags != 0);
286fa96acadSDave Chinner 
287fa96acadSDave Chinner 	if (lock_flags & XFS_IOLOCK_EXCL)
288fa96acadSDave Chinner 		mrunlock_excl(&ip->i_iolock);
289fa96acadSDave Chinner 	else if (lock_flags & XFS_IOLOCK_SHARED)
290fa96acadSDave Chinner 		mrunlock_shared(&ip->i_iolock);
291fa96acadSDave Chinner 
292653c60b6SDave Chinner 	if (lock_flags & XFS_MMAPLOCK_EXCL)
293653c60b6SDave Chinner 		mrunlock_excl(&ip->i_mmaplock);
294653c60b6SDave Chinner 	else if (lock_flags & XFS_MMAPLOCK_SHARED)
295653c60b6SDave Chinner 		mrunlock_shared(&ip->i_mmaplock);
296653c60b6SDave Chinner 
297fa96acadSDave Chinner 	if (lock_flags & XFS_ILOCK_EXCL)
298fa96acadSDave Chinner 		mrunlock_excl(&ip->i_lock);
299fa96acadSDave Chinner 	else if (lock_flags & XFS_ILOCK_SHARED)
300fa96acadSDave Chinner 		mrunlock_shared(&ip->i_lock);
301fa96acadSDave Chinner 
302fa96acadSDave Chinner 	trace_xfs_iunlock(ip, lock_flags, _RET_IP_);
303fa96acadSDave Chinner }
304fa96acadSDave Chinner 
305fa96acadSDave Chinner /*
306fa96acadSDave Chinner  * give up write locks.  the i/o lock cannot be held nested
307fa96acadSDave Chinner  * if it is being demoted.
308fa96acadSDave Chinner  */
309fa96acadSDave Chinner void
310fa96acadSDave Chinner xfs_ilock_demote(
311fa96acadSDave Chinner 	xfs_inode_t		*ip,
312fa96acadSDave Chinner 	uint			lock_flags)
313fa96acadSDave Chinner {
314653c60b6SDave Chinner 	ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL));
315653c60b6SDave Chinner 	ASSERT((lock_flags &
316653c60b6SDave Chinner 		~(XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
317fa96acadSDave Chinner 
318fa96acadSDave Chinner 	if (lock_flags & XFS_ILOCK_EXCL)
319fa96acadSDave Chinner 		mrdemote(&ip->i_lock);
320653c60b6SDave Chinner 	if (lock_flags & XFS_MMAPLOCK_EXCL)
321653c60b6SDave Chinner 		mrdemote(&ip->i_mmaplock);
322fa96acadSDave Chinner 	if (lock_flags & XFS_IOLOCK_EXCL)
323fa96acadSDave Chinner 		mrdemote(&ip->i_iolock);
324fa96acadSDave Chinner 
325fa96acadSDave Chinner 	trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_);
326fa96acadSDave Chinner }
327fa96acadSDave Chinner 
328742ae1e3SDave Chinner #if defined(DEBUG) || defined(XFS_WARN)
329fa96acadSDave Chinner int
330fa96acadSDave Chinner xfs_isilocked(
331fa96acadSDave Chinner 	xfs_inode_t		*ip,
332fa96acadSDave Chinner 	uint			lock_flags)
333fa96acadSDave Chinner {
334fa96acadSDave Chinner 	if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) {
335fa96acadSDave Chinner 		if (!(lock_flags & XFS_ILOCK_SHARED))
336fa96acadSDave Chinner 			return !!ip->i_lock.mr_writer;
337fa96acadSDave Chinner 		return rwsem_is_locked(&ip->i_lock.mr_lock);
338fa96acadSDave Chinner 	}
339fa96acadSDave Chinner 
340653c60b6SDave Chinner 	if (lock_flags & (XFS_MMAPLOCK_EXCL|XFS_MMAPLOCK_SHARED)) {
341653c60b6SDave Chinner 		if (!(lock_flags & XFS_MMAPLOCK_SHARED))
342653c60b6SDave Chinner 			return !!ip->i_mmaplock.mr_writer;
343653c60b6SDave Chinner 		return rwsem_is_locked(&ip->i_mmaplock.mr_lock);
344653c60b6SDave Chinner 	}
345653c60b6SDave Chinner 
346fa96acadSDave Chinner 	if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) {
347fa96acadSDave Chinner 		if (!(lock_flags & XFS_IOLOCK_SHARED))
348fa96acadSDave Chinner 			return !!ip->i_iolock.mr_writer;
349fa96acadSDave Chinner 		return rwsem_is_locked(&ip->i_iolock.mr_lock);
350fa96acadSDave Chinner 	}
351fa96acadSDave Chinner 
352fa96acadSDave Chinner 	ASSERT(0);
353fa96acadSDave Chinner 	return 0;
354fa96acadSDave Chinner }
355fa96acadSDave Chinner #endif
356fa96acadSDave Chinner 
357c24b5dfaSDave Chinner #ifdef DEBUG
358c24b5dfaSDave Chinner int xfs_locked_n;
359c24b5dfaSDave Chinner int xfs_small_retries;
360c24b5dfaSDave Chinner int xfs_middle_retries;
361c24b5dfaSDave Chinner int xfs_lots_retries;
362c24b5dfaSDave Chinner int xfs_lock_delays;
363c24b5dfaSDave Chinner #endif
364c24b5dfaSDave Chinner 
365b6a9947eSDave Chinner /*
366b6a9947eSDave Chinner  * xfs_lockdep_subclass_ok() is only used in an ASSERT, so is only called when
367b6a9947eSDave Chinner  * DEBUG or XFS_WARN is set. And MAX_LOCKDEP_SUBCLASSES is then only defined
368b6a9947eSDave Chinner  * when CONFIG_LOCKDEP is set. Hence the complex define below to avoid build
369b6a9947eSDave Chinner  * errors and warnings.
370b6a9947eSDave Chinner  */
371b6a9947eSDave Chinner #if (defined(DEBUG) || defined(XFS_WARN)) && defined(CONFIG_LOCKDEP)
3723403ccc0SDave Chinner static bool
3733403ccc0SDave Chinner xfs_lockdep_subclass_ok(
3743403ccc0SDave Chinner 	int subclass)
3753403ccc0SDave Chinner {
3763403ccc0SDave Chinner 	return subclass < MAX_LOCKDEP_SUBCLASSES;
3773403ccc0SDave Chinner }
3783403ccc0SDave Chinner #else
3793403ccc0SDave Chinner #define xfs_lockdep_subclass_ok(subclass)	(true)
3803403ccc0SDave Chinner #endif
3813403ccc0SDave Chinner 
382c24b5dfaSDave Chinner /*
383653c60b6SDave Chinner  * Bump the subclass so xfs_lock_inodes() acquires each lock with a different
3840952c818SDave Chinner  * value. This can be called for any type of inode lock combination, including
3850952c818SDave Chinner  * parent locking. Care must be taken to ensure we don't overrun the subclass
3860952c818SDave Chinner  * storage fields in the class mask we build.
387c24b5dfaSDave Chinner  */
388c24b5dfaSDave Chinner static inline int
389c24b5dfaSDave Chinner xfs_lock_inumorder(int lock_mode, int subclass)
390c24b5dfaSDave Chinner {
3910952c818SDave Chinner 	int	class = 0;
3920952c818SDave Chinner 
3930952c818SDave Chinner 	ASSERT(!(lock_mode & (XFS_ILOCK_PARENT | XFS_ILOCK_RTBITMAP |
3940952c818SDave Chinner 			      XFS_ILOCK_RTSUM)));
3953403ccc0SDave Chinner 	ASSERT(xfs_lockdep_subclass_ok(subclass));
3960952c818SDave Chinner 
397653c60b6SDave Chinner 	if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) {
3980952c818SDave Chinner 		ASSERT(subclass <= XFS_IOLOCK_MAX_SUBCLASS);
3993403ccc0SDave Chinner 		ASSERT(xfs_lockdep_subclass_ok(subclass +
4003403ccc0SDave Chinner 						XFS_IOLOCK_PARENT_VAL));
4010952c818SDave Chinner 		class += subclass << XFS_IOLOCK_SHIFT;
4020952c818SDave Chinner 		if (lock_mode & XFS_IOLOCK_PARENT)
4030952c818SDave Chinner 			class += XFS_IOLOCK_PARENT_VAL << XFS_IOLOCK_SHIFT;
404653c60b6SDave Chinner 	}
405653c60b6SDave Chinner 
406653c60b6SDave Chinner 	if (lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) {
4070952c818SDave Chinner 		ASSERT(subclass <= XFS_MMAPLOCK_MAX_SUBCLASS);
4080952c818SDave Chinner 		class += subclass << XFS_MMAPLOCK_SHIFT;
409653c60b6SDave Chinner 	}
410653c60b6SDave Chinner 
4110952c818SDave Chinner 	if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) {
4120952c818SDave Chinner 		ASSERT(subclass <= XFS_ILOCK_MAX_SUBCLASS);
4130952c818SDave Chinner 		class += subclass << XFS_ILOCK_SHIFT;
4140952c818SDave Chinner 	}
415c24b5dfaSDave Chinner 
4160952c818SDave Chinner 	return (lock_mode & ~XFS_LOCK_SUBCLASS_MASK) | class;
417c24b5dfaSDave Chinner }
418c24b5dfaSDave Chinner 
419c24b5dfaSDave Chinner /*
42095afcf5cSDave Chinner  * The following routine will lock n inodes in exclusive mode.  We assume the
42195afcf5cSDave Chinner  * caller calls us with the inodes in i_ino order.
422c24b5dfaSDave Chinner  *
42395afcf5cSDave Chinner  * We need to detect deadlock where an inode that we lock is in the AIL and we
42495afcf5cSDave Chinner  * start waiting for another inode that is locked by a thread in a long running
42595afcf5cSDave Chinner  * transaction (such as truncate). This can result in deadlock since the long
42695afcf5cSDave Chinner  * running trans might need to wait for the inode we just locked in order to
42795afcf5cSDave Chinner  * push the tail and free space in the log.
4280952c818SDave Chinner  *
4290952c818SDave Chinner  * xfs_lock_inodes() can only be used to lock one type of lock at a time -
4300952c818SDave Chinner  * the iolock, the mmaplock or the ilock, but not more than one at a time. If we
4310952c818SDave Chinner  * lock more than one at a time, lockdep will report false positives saying we
4320952c818SDave Chinner  * have violated locking orders.
433c24b5dfaSDave Chinner  */
434c24b5dfaSDave Chinner void
435c24b5dfaSDave Chinner xfs_lock_inodes(
436c24b5dfaSDave Chinner 	xfs_inode_t	**ips,
437c24b5dfaSDave Chinner 	int		inodes,
438c24b5dfaSDave Chinner 	uint		lock_mode)
439c24b5dfaSDave Chinner {
440c24b5dfaSDave Chinner 	int		attempts = 0, i, j, try_lock;
441c24b5dfaSDave Chinner 	xfs_log_item_t	*lp;
442c24b5dfaSDave Chinner 
4430952c818SDave Chinner 	/*
4440952c818SDave Chinner 	 * Currently supports between 2 and 5 inodes with exclusive locking.  We
4450952c818SDave Chinner 	 * support an arbitrary depth of locking here, but absolute limits on
4460952c818SDave Chinner 	 * inodes depend on the the type of locking and the limits placed by
4470952c818SDave Chinner 	 * lockdep annotations in xfs_lock_inumorder.  These are all checked by
4480952c818SDave Chinner 	 * the asserts.
4490952c818SDave Chinner 	 */
45095afcf5cSDave Chinner 	ASSERT(ips && inodes >= 2 && inodes <= 5);
4510952c818SDave Chinner 	ASSERT(lock_mode & (XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL |
4520952c818SDave Chinner 			    XFS_ILOCK_EXCL));
4530952c818SDave Chinner 	ASSERT(!(lock_mode & (XFS_IOLOCK_SHARED | XFS_MMAPLOCK_SHARED |
4540952c818SDave Chinner 			      XFS_ILOCK_SHARED)));
4550952c818SDave Chinner 	ASSERT(!(lock_mode & XFS_IOLOCK_EXCL) ||
4560952c818SDave Chinner 		inodes <= XFS_IOLOCK_MAX_SUBCLASS + 1);
4570952c818SDave Chinner 	ASSERT(!(lock_mode & XFS_MMAPLOCK_EXCL) ||
4580952c818SDave Chinner 		inodes <= XFS_MMAPLOCK_MAX_SUBCLASS + 1);
4590952c818SDave Chinner 	ASSERT(!(lock_mode & XFS_ILOCK_EXCL) ||
4600952c818SDave Chinner 		inodes <= XFS_ILOCK_MAX_SUBCLASS + 1);
4610952c818SDave Chinner 
4620952c818SDave Chinner 	if (lock_mode & XFS_IOLOCK_EXCL) {
4630952c818SDave Chinner 		ASSERT(!(lock_mode & (XFS_MMAPLOCK_EXCL | XFS_ILOCK_EXCL)));
4640952c818SDave Chinner 	} else if (lock_mode & XFS_MMAPLOCK_EXCL)
4650952c818SDave Chinner 		ASSERT(!(lock_mode & XFS_ILOCK_EXCL));
466c24b5dfaSDave Chinner 
467c24b5dfaSDave Chinner 	try_lock = 0;
468c24b5dfaSDave Chinner 	i = 0;
469c24b5dfaSDave Chinner again:
470c24b5dfaSDave Chinner 	for (; i < inodes; i++) {
471c24b5dfaSDave Chinner 		ASSERT(ips[i]);
472c24b5dfaSDave Chinner 
473c24b5dfaSDave Chinner 		if (i && (ips[i] == ips[i - 1]))	/* Already locked */
474c24b5dfaSDave Chinner 			continue;
475c24b5dfaSDave Chinner 
476c24b5dfaSDave Chinner 		/*
47795afcf5cSDave Chinner 		 * If try_lock is not set yet, make sure all locked inodes are
47895afcf5cSDave Chinner 		 * not in the AIL.  If any are, set try_lock to be used later.
479c24b5dfaSDave Chinner 		 */
480c24b5dfaSDave Chinner 		if (!try_lock) {
481c24b5dfaSDave Chinner 			for (j = (i - 1); j >= 0 && !try_lock; j--) {
482c24b5dfaSDave Chinner 				lp = (xfs_log_item_t *)ips[j]->i_itemp;
48395afcf5cSDave Chinner 				if (lp && (lp->li_flags & XFS_LI_IN_AIL))
484c24b5dfaSDave Chinner 					try_lock++;
485c24b5dfaSDave Chinner 			}
486c24b5dfaSDave Chinner 		}
487c24b5dfaSDave Chinner 
488c24b5dfaSDave Chinner 		/*
489c24b5dfaSDave Chinner 		 * If any of the previous locks we have locked is in the AIL,
490c24b5dfaSDave Chinner 		 * we must TRY to get the second and subsequent locks. If
491c24b5dfaSDave Chinner 		 * we can't get any, we must release all we have
492c24b5dfaSDave Chinner 		 * and try again.
493c24b5dfaSDave Chinner 		 */
49495afcf5cSDave Chinner 		if (!try_lock) {
49595afcf5cSDave Chinner 			xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
49695afcf5cSDave Chinner 			continue;
49795afcf5cSDave Chinner 		}
498c24b5dfaSDave Chinner 
49995afcf5cSDave Chinner 		/* try_lock means we have an inode locked that is in the AIL. */
500c24b5dfaSDave Chinner 		ASSERT(i != 0);
50195afcf5cSDave Chinner 		if (xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i)))
50295afcf5cSDave Chinner 			continue;
50395afcf5cSDave Chinner 
50495afcf5cSDave Chinner 		/*
50595afcf5cSDave Chinner 		 * Unlock all previous guys and try again.  xfs_iunlock will try
50695afcf5cSDave Chinner 		 * to push the tail if the inode is in the AIL.
50795afcf5cSDave Chinner 		 */
508c24b5dfaSDave Chinner 		attempts++;
509c24b5dfaSDave Chinner 		for (j = i - 1; j >= 0; j--) {
510c24b5dfaSDave Chinner 			/*
51195afcf5cSDave Chinner 			 * Check to see if we've already unlocked this one.  Not
51295afcf5cSDave Chinner 			 * the first one going back, and the inode ptr is the
51395afcf5cSDave Chinner 			 * same.
514c24b5dfaSDave Chinner 			 */
51595afcf5cSDave Chinner 			if (j != (i - 1) && ips[j] == ips[j + 1])
516c24b5dfaSDave Chinner 				continue;
517c24b5dfaSDave Chinner 
518c24b5dfaSDave Chinner 			xfs_iunlock(ips[j], lock_mode);
519c24b5dfaSDave Chinner 		}
520c24b5dfaSDave Chinner 
521c24b5dfaSDave Chinner 		if ((attempts % 5) == 0) {
522c24b5dfaSDave Chinner 			delay(1); /* Don't just spin the CPU */
523c24b5dfaSDave Chinner #ifdef DEBUG
524c24b5dfaSDave Chinner 			xfs_lock_delays++;
525c24b5dfaSDave Chinner #endif
526c24b5dfaSDave Chinner 		}
527c24b5dfaSDave Chinner 		i = 0;
528c24b5dfaSDave Chinner 		try_lock = 0;
529c24b5dfaSDave Chinner 		goto again;
530c24b5dfaSDave Chinner 	}
531c24b5dfaSDave Chinner 
532c24b5dfaSDave Chinner #ifdef DEBUG
533c24b5dfaSDave Chinner 	if (attempts) {
534c24b5dfaSDave Chinner 		if (attempts < 5) xfs_small_retries++;
535c24b5dfaSDave Chinner 		else if (attempts < 100) xfs_middle_retries++;
536c24b5dfaSDave Chinner 		else xfs_lots_retries++;
537c24b5dfaSDave Chinner 	} else {
538c24b5dfaSDave Chinner 		xfs_locked_n++;
539c24b5dfaSDave Chinner 	}
540c24b5dfaSDave Chinner #endif
541c24b5dfaSDave Chinner }
542c24b5dfaSDave Chinner 
543c24b5dfaSDave Chinner /*
544653c60b6SDave Chinner  * xfs_lock_two_inodes() can only be used to lock one type of lock at a time -
545653c60b6SDave Chinner  * the iolock, the mmaplock or the ilock, but not more than one at a time. If we
546653c60b6SDave Chinner  * lock more than one at a time, lockdep will report false positives saying we
547653c60b6SDave Chinner  * have violated locking orders.
548c24b5dfaSDave Chinner  */
549c24b5dfaSDave Chinner void
550c24b5dfaSDave Chinner xfs_lock_two_inodes(
551c24b5dfaSDave Chinner 	xfs_inode_t		*ip0,
552c24b5dfaSDave Chinner 	xfs_inode_t		*ip1,
553c24b5dfaSDave Chinner 	uint			lock_mode)
554c24b5dfaSDave Chinner {
555c24b5dfaSDave Chinner 	xfs_inode_t		*temp;
556c24b5dfaSDave Chinner 	int			attempts = 0;
557c24b5dfaSDave Chinner 	xfs_log_item_t		*lp;
558c24b5dfaSDave Chinner 
559653c60b6SDave Chinner 	if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) {
560653c60b6SDave Chinner 		ASSERT(!(lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)));
561653c60b6SDave Chinner 		ASSERT(!(lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
562653c60b6SDave Chinner 	} else if (lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL))
563653c60b6SDave Chinner 		ASSERT(!(lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
564653c60b6SDave Chinner 
565c24b5dfaSDave Chinner 	ASSERT(ip0->i_ino != ip1->i_ino);
566c24b5dfaSDave Chinner 
567c24b5dfaSDave Chinner 	if (ip0->i_ino > ip1->i_ino) {
568c24b5dfaSDave Chinner 		temp = ip0;
569c24b5dfaSDave Chinner 		ip0 = ip1;
570c24b5dfaSDave Chinner 		ip1 = temp;
571c24b5dfaSDave Chinner 	}
572c24b5dfaSDave Chinner 
573c24b5dfaSDave Chinner  again:
574c24b5dfaSDave Chinner 	xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
575c24b5dfaSDave Chinner 
576c24b5dfaSDave Chinner 	/*
577c24b5dfaSDave Chinner 	 * If the first lock we have locked is in the AIL, we must TRY to get
578c24b5dfaSDave Chinner 	 * the second lock. If we can't get it, we must release the first one
579c24b5dfaSDave Chinner 	 * and try again.
580c24b5dfaSDave Chinner 	 */
581c24b5dfaSDave Chinner 	lp = (xfs_log_item_t *)ip0->i_itemp;
582c24b5dfaSDave Chinner 	if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
583c24b5dfaSDave Chinner 		if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
584c24b5dfaSDave Chinner 			xfs_iunlock(ip0, lock_mode);
585c24b5dfaSDave Chinner 			if ((++attempts % 5) == 0)
586c24b5dfaSDave Chinner 				delay(1); /* Don't just spin the CPU */
587c24b5dfaSDave Chinner 			goto again;
588c24b5dfaSDave Chinner 		}
589c24b5dfaSDave Chinner 	} else {
590c24b5dfaSDave Chinner 		xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
591c24b5dfaSDave Chinner 	}
592c24b5dfaSDave Chinner }
593c24b5dfaSDave Chinner 
594c24b5dfaSDave Chinner 
595fa96acadSDave Chinner void
596fa96acadSDave Chinner __xfs_iflock(
597fa96acadSDave Chinner 	struct xfs_inode	*ip)
598fa96acadSDave Chinner {
599fa96acadSDave Chinner 	wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IFLOCK_BIT);
600fa96acadSDave Chinner 	DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IFLOCK_BIT);
601fa96acadSDave Chinner 
602fa96acadSDave Chinner 	do {
603fa96acadSDave Chinner 		prepare_to_wait_exclusive(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
604fa96acadSDave Chinner 		if (xfs_isiflocked(ip))
605fa96acadSDave Chinner 			io_schedule();
606fa96acadSDave Chinner 	} while (!xfs_iflock_nowait(ip));
607fa96acadSDave Chinner 
608fa96acadSDave Chinner 	finish_wait(wq, &wait.wait);
609fa96acadSDave Chinner }
610fa96acadSDave Chinner 
6111da177e4SLinus Torvalds STATIC uint
6121da177e4SLinus Torvalds _xfs_dic2xflags(
61358f88ca2SDave Chinner 	__uint16_t		di_flags,
61458f88ca2SDave Chinner 	uint64_t		di_flags2,
61558f88ca2SDave Chinner 	bool			has_attr)
6161da177e4SLinus Torvalds {
6171da177e4SLinus Torvalds 	uint			flags = 0;
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 	if (di_flags & XFS_DIFLAG_ANY) {
6201da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_REALTIME)
621e7b89481SDave Chinner 			flags |= FS_XFLAG_REALTIME;
6221da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_PREALLOC)
623e7b89481SDave Chinner 			flags |= FS_XFLAG_PREALLOC;
6241da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_IMMUTABLE)
625e7b89481SDave Chinner 			flags |= FS_XFLAG_IMMUTABLE;
6261da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_APPEND)
627e7b89481SDave Chinner 			flags |= FS_XFLAG_APPEND;
6281da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_SYNC)
629e7b89481SDave Chinner 			flags |= FS_XFLAG_SYNC;
6301da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_NOATIME)
631e7b89481SDave Chinner 			flags |= FS_XFLAG_NOATIME;
6321da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_NODUMP)
633e7b89481SDave Chinner 			flags |= FS_XFLAG_NODUMP;
6341da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_RTINHERIT)
635e7b89481SDave Chinner 			flags |= FS_XFLAG_RTINHERIT;
6361da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_PROJINHERIT)
637e7b89481SDave Chinner 			flags |= FS_XFLAG_PROJINHERIT;
6381da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_NOSYMLINKS)
639e7b89481SDave Chinner 			flags |= FS_XFLAG_NOSYMLINKS;
640dd9f438eSNathan Scott 		if (di_flags & XFS_DIFLAG_EXTSIZE)
641e7b89481SDave Chinner 			flags |= FS_XFLAG_EXTSIZE;
642dd9f438eSNathan Scott 		if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
643e7b89481SDave Chinner 			flags |= FS_XFLAG_EXTSZINHERIT;
644d3446eacSBarry Naujok 		if (di_flags & XFS_DIFLAG_NODEFRAG)
645e7b89481SDave Chinner 			flags |= FS_XFLAG_NODEFRAG;
6462a82b8beSDavid Chinner 		if (di_flags & XFS_DIFLAG_FILESTREAM)
647e7b89481SDave Chinner 			flags |= FS_XFLAG_FILESTREAM;
6481da177e4SLinus Torvalds 	}
6491da177e4SLinus Torvalds 
65058f88ca2SDave Chinner 	if (di_flags2 & XFS_DIFLAG2_ANY) {
65158f88ca2SDave Chinner 		if (di_flags2 & XFS_DIFLAG2_DAX)
65258f88ca2SDave Chinner 			flags |= FS_XFLAG_DAX;
65358f88ca2SDave Chinner 	}
65458f88ca2SDave Chinner 
65558f88ca2SDave Chinner 	if (has_attr)
65658f88ca2SDave Chinner 		flags |= FS_XFLAG_HASATTR;
65758f88ca2SDave Chinner 
6581da177e4SLinus Torvalds 	return flags;
6591da177e4SLinus Torvalds }
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds uint
6621da177e4SLinus Torvalds xfs_ip2xflags(
66358f88ca2SDave Chinner 	struct xfs_inode	*ip)
6641da177e4SLinus Torvalds {
66558f88ca2SDave Chinner 	struct xfs_icdinode	*dic = &ip->i_d;
6661da177e4SLinus Torvalds 
66758f88ca2SDave Chinner 	return _xfs_dic2xflags(dic->di_flags, dic->di_flags2, XFS_IFORK_Q(ip));
6681da177e4SLinus Torvalds }
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds uint
6711da177e4SLinus Torvalds xfs_dic2xflags(
67258f88ca2SDave Chinner 	struct xfs_dinode	*dip)
6731da177e4SLinus Torvalds {
67458f88ca2SDave Chinner 	return _xfs_dic2xflags(be16_to_cpu(dip->di_flags),
67558f88ca2SDave Chinner 				be64_to_cpu(dip->di_flags2), XFS_DFORK_Q(dip));
6761da177e4SLinus Torvalds }
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds /*
679c24b5dfaSDave Chinner  * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
680c24b5dfaSDave Chinner  * is allowed, otherwise it has to be an exact match. If a CI match is found,
681c24b5dfaSDave Chinner  * ci_name->name will point to a the actual name (caller must free) or
682c24b5dfaSDave Chinner  * will be set to NULL if an exact match is found.
683c24b5dfaSDave Chinner  */
684c24b5dfaSDave Chinner int
685c24b5dfaSDave Chinner xfs_lookup(
686c24b5dfaSDave Chinner 	xfs_inode_t		*dp,
687c24b5dfaSDave Chinner 	struct xfs_name		*name,
688c24b5dfaSDave Chinner 	xfs_inode_t		**ipp,
689c24b5dfaSDave Chinner 	struct xfs_name		*ci_name)
690c24b5dfaSDave Chinner {
691c24b5dfaSDave Chinner 	xfs_ino_t		inum;
692c24b5dfaSDave Chinner 	int			error;
693c24b5dfaSDave Chinner 
694c24b5dfaSDave Chinner 	trace_xfs_lookup(dp, name);
695c24b5dfaSDave Chinner 
696c24b5dfaSDave Chinner 	if (XFS_FORCED_SHUTDOWN(dp->i_mount))
6972451337dSDave Chinner 		return -EIO;
698c24b5dfaSDave Chinner 
699dbad7c99SDave Chinner 	xfs_ilock(dp, XFS_IOLOCK_SHARED);
700c24b5dfaSDave Chinner 	error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
701c24b5dfaSDave Chinner 	if (error)
702dbad7c99SDave Chinner 		goto out_unlock;
703c24b5dfaSDave Chinner 
704c24b5dfaSDave Chinner 	error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
705c24b5dfaSDave Chinner 	if (error)
706c24b5dfaSDave Chinner 		goto out_free_name;
707c24b5dfaSDave Chinner 
708dbad7c99SDave Chinner 	xfs_iunlock(dp, XFS_IOLOCK_SHARED);
709c24b5dfaSDave Chinner 	return 0;
710c24b5dfaSDave Chinner 
711c24b5dfaSDave Chinner out_free_name:
712c24b5dfaSDave Chinner 	if (ci_name)
713c24b5dfaSDave Chinner 		kmem_free(ci_name->name);
714dbad7c99SDave Chinner out_unlock:
715dbad7c99SDave Chinner 	xfs_iunlock(dp, XFS_IOLOCK_SHARED);
716c24b5dfaSDave Chinner 	*ipp = NULL;
717c24b5dfaSDave Chinner 	return error;
718c24b5dfaSDave Chinner }
719c24b5dfaSDave Chinner 
720c24b5dfaSDave Chinner /*
7211da177e4SLinus Torvalds  * Allocate an inode on disk and return a copy of its in-core version.
7221da177e4SLinus Torvalds  * The in-core inode is locked exclusively.  Set mode, nlink, and rdev
7231da177e4SLinus Torvalds  * appropriately within the inode.  The uid and gid for the inode are
7241da177e4SLinus Torvalds  * set according to the contents of the given cred structure.
7251da177e4SLinus Torvalds  *
7261da177e4SLinus Torvalds  * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
727cd856db6SCarlos Maiolino  * has a free inode available, call xfs_iget() to obtain the in-core
728cd856db6SCarlos Maiolino  * version of the allocated inode.  Finally, fill in the inode and
729cd856db6SCarlos Maiolino  * log its initial contents.  In this case, ialloc_context would be
730cd856db6SCarlos Maiolino  * set to NULL.
7311da177e4SLinus Torvalds  *
732cd856db6SCarlos Maiolino  * If xfs_dialloc() does not have an available inode, it will replenish
733cd856db6SCarlos Maiolino  * its supply by doing an allocation. Since we can only do one
734cd856db6SCarlos Maiolino  * allocation within a transaction without deadlocks, we must commit
735cd856db6SCarlos Maiolino  * the current transaction before returning the inode itself.
736cd856db6SCarlos Maiolino  * In this case, therefore, we will set ialloc_context and return.
7371da177e4SLinus Torvalds  * The caller should then commit the current transaction, start a new
7381da177e4SLinus Torvalds  * transaction, and call xfs_ialloc() again to actually get the inode.
7391da177e4SLinus Torvalds  *
7401da177e4SLinus Torvalds  * To ensure that some other process does not grab the inode that
7411da177e4SLinus Torvalds  * was allocated during the first call to xfs_ialloc(), this routine
7421da177e4SLinus Torvalds  * also returns the [locked] bp pointing to the head of the freelist
7431da177e4SLinus Torvalds  * as ialloc_context.  The caller should hold this buffer across
7441da177e4SLinus Torvalds  * the commit and pass it back into this routine on the second call.
745b11f94d5SDavid Chinner  *
746b11f94d5SDavid Chinner  * If we are allocating quota inodes, we do not have a parent inode
747b11f94d5SDavid Chinner  * to attach to or associate with (i.e. pip == NULL) because they
748b11f94d5SDavid Chinner  * are not linked into the directory structure - they are attached
749b11f94d5SDavid Chinner  * directly to the superblock - and so have no parent.
7501da177e4SLinus Torvalds  */
7511da177e4SLinus Torvalds int
7521da177e4SLinus Torvalds xfs_ialloc(
7531da177e4SLinus Torvalds 	xfs_trans_t	*tp,
7541da177e4SLinus Torvalds 	xfs_inode_t	*pip,
755576b1d67SAl Viro 	umode_t		mode,
75631b084aeSNathan Scott 	xfs_nlink_t	nlink,
7571da177e4SLinus Torvalds 	xfs_dev_t	rdev,
7586743099cSArkadiusz Mi?kiewicz 	prid_t		prid,
7591da177e4SLinus Torvalds 	int		okalloc,
7601da177e4SLinus Torvalds 	xfs_buf_t	**ialloc_context,
7611da177e4SLinus Torvalds 	xfs_inode_t	**ipp)
7621da177e4SLinus Torvalds {
76393848a99SChristoph Hellwig 	struct xfs_mount *mp = tp->t_mountp;
7641da177e4SLinus Torvalds 	xfs_ino_t	ino;
7651da177e4SLinus Torvalds 	xfs_inode_t	*ip;
7661da177e4SLinus Torvalds 	uint		flags;
7671da177e4SLinus Torvalds 	int		error;
768e076b0f3SDave Chinner 	struct timespec	tv;
7693987848cSDave Chinner 	struct inode	*inode;
7701da177e4SLinus Torvalds 
7711da177e4SLinus Torvalds 	/*
7721da177e4SLinus Torvalds 	 * Call the space management code to pick
7731da177e4SLinus Torvalds 	 * the on-disk inode to be allocated.
7741da177e4SLinus Torvalds 	 */
775b11f94d5SDavid Chinner 	error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
77608358906SChristoph Hellwig 			    ialloc_context, &ino);
777bf904248SDavid Chinner 	if (error)
7781da177e4SLinus Torvalds 		return error;
77908358906SChristoph Hellwig 	if (*ialloc_context || ino == NULLFSINO) {
7801da177e4SLinus Torvalds 		*ipp = NULL;
7811da177e4SLinus Torvalds 		return 0;
7821da177e4SLinus Torvalds 	}
7831da177e4SLinus Torvalds 	ASSERT(*ialloc_context == NULL);
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds 	/*
7861da177e4SLinus Torvalds 	 * Get the in-core inode with the lock held exclusively.
7871da177e4SLinus Torvalds 	 * This is because we're setting fields here we need
7881da177e4SLinus Torvalds 	 * to prevent others from looking at until we're done.
7891da177e4SLinus Torvalds 	 */
79093848a99SChristoph Hellwig 	error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE,
791ec3ba85fSChristoph Hellwig 			 XFS_ILOCK_EXCL, &ip);
792bf904248SDavid Chinner 	if (error)
7931da177e4SLinus Torvalds 		return error;
7941da177e4SLinus Torvalds 	ASSERT(ip != NULL);
7953987848cSDave Chinner 	inode = VFS_I(ip);
7961da177e4SLinus Torvalds 
797263997a6SDave Chinner 	/*
798263997a6SDave Chinner 	 * We always convert v1 inodes to v2 now - we only support filesystems
799263997a6SDave Chinner 	 * with >= v2 inode capability, so there is no reason for ever leaving
800263997a6SDave Chinner 	 * an inode in v1 format.
801263997a6SDave Chinner 	 */
802263997a6SDave Chinner 	if (ip->i_d.di_version == 1)
803263997a6SDave Chinner 		ip->i_d.di_version = 2;
804263997a6SDave Chinner 
805576b1d67SAl Viro 	ip->i_d.di_mode = mode;
8061da177e4SLinus Torvalds 	ip->i_d.di_onlink = 0;
8071da177e4SLinus Torvalds 	ip->i_d.di_nlink = nlink;
8081da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_nlink == nlink);
8097aab1b28SDwight Engen 	ip->i_d.di_uid = xfs_kuid_to_uid(current_fsuid());
8107aab1b28SDwight Engen 	ip->i_d.di_gid = xfs_kgid_to_gid(current_fsgid());
8116743099cSArkadiusz Mi?kiewicz 	xfs_set_projid(ip, prid);
8121da177e4SLinus Torvalds 
813bd186aa9SChristoph Hellwig 	if (pip && XFS_INHERIT_GID(pip)) {
8141da177e4SLinus Torvalds 		ip->i_d.di_gid = pip->i_d.di_gid;
815abbede1bSAl Viro 		if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) {
8161da177e4SLinus Torvalds 			ip->i_d.di_mode |= S_ISGID;
8171da177e4SLinus Torvalds 		}
8181da177e4SLinus Torvalds 	}
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds 	/*
8211da177e4SLinus Torvalds 	 * If the group ID of the new file does not match the effective group
8221da177e4SLinus Torvalds 	 * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
8231da177e4SLinus Torvalds 	 * (and only if the irix_sgid_inherit compatibility variable is set).
8241da177e4SLinus Torvalds 	 */
8251da177e4SLinus Torvalds 	if ((irix_sgid_inherit) &&
8261da177e4SLinus Torvalds 	    (ip->i_d.di_mode & S_ISGID) &&
8277aab1b28SDwight Engen 	    (!in_group_p(xfs_gid_to_kgid(ip->i_d.di_gid)))) {
8281da177e4SLinus Torvalds 		ip->i_d.di_mode &= ~S_ISGID;
8291da177e4SLinus Torvalds 	}
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds 	ip->i_d.di_size = 0;
8321da177e4SLinus Torvalds 	ip->i_d.di_nextents = 0;
8331da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_nblocks == 0);
834dff35fd4SChristoph Hellwig 
835e076b0f3SDave Chinner 	tv = current_fs_time(mp->m_super);
8363987848cSDave Chinner 	inode->i_mtime = tv;
8373987848cSDave Chinner 	inode->i_atime = tv;
8383987848cSDave Chinner 	inode->i_ctime = tv;
839dff35fd4SChristoph Hellwig 
8401da177e4SLinus Torvalds 	/*
8411da177e4SLinus Torvalds 	 * di_gen will have been taken care of in xfs_iread.
8421da177e4SLinus Torvalds 	 */
8431da177e4SLinus Torvalds 	ip->i_d.di_extsize = 0;
8441da177e4SLinus Torvalds 	ip->i_d.di_dmevmask = 0;
8451da177e4SLinus Torvalds 	ip->i_d.di_dmstate = 0;
8461da177e4SLinus Torvalds 	ip->i_d.di_flags = 0;
84793848a99SChristoph Hellwig 
84893848a99SChristoph Hellwig 	if (ip->i_d.di_version == 3) {
84993848a99SChristoph Hellwig 		ip->i_d.di_changecount = 1;
85093848a99SChristoph Hellwig 		ip->i_d.di_flags2 = 0;
8513987848cSDave Chinner 		ip->i_d.di_crtime.t_sec = (__int32_t)tv.tv_sec;
8523987848cSDave Chinner 		ip->i_d.di_crtime.t_nsec = (__int32_t)tv.tv_nsec;
85393848a99SChristoph Hellwig 	}
85493848a99SChristoph Hellwig 
85593848a99SChristoph Hellwig 
8561da177e4SLinus Torvalds 	flags = XFS_ILOG_CORE;
8571da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
8581da177e4SLinus Torvalds 	case S_IFIFO:
8591da177e4SLinus Torvalds 	case S_IFCHR:
8601da177e4SLinus Torvalds 	case S_IFBLK:
8611da177e4SLinus Torvalds 	case S_IFSOCK:
8621da177e4SLinus Torvalds 		ip->i_d.di_format = XFS_DINODE_FMT_DEV;
8631da177e4SLinus Torvalds 		ip->i_df.if_u2.if_rdev = rdev;
8641da177e4SLinus Torvalds 		ip->i_df.if_flags = 0;
8651da177e4SLinus Torvalds 		flags |= XFS_ILOG_DEV;
8661da177e4SLinus Torvalds 		break;
8671da177e4SLinus Torvalds 	case S_IFREG:
8681da177e4SLinus Torvalds 	case S_IFDIR:
869b11f94d5SDavid Chinner 		if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
87058f88ca2SDave Chinner 			uint64_t	di_flags2 = 0;
871365ca83dSNathan Scott 			uint		di_flags = 0;
872365ca83dSNathan Scott 
873abbede1bSAl Viro 			if (S_ISDIR(mode)) {
874365ca83dSNathan Scott 				if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
875365ca83dSNathan Scott 					di_flags |= XFS_DIFLAG_RTINHERIT;
876dd9f438eSNathan Scott 				if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
877dd9f438eSNathan Scott 					di_flags |= XFS_DIFLAG_EXTSZINHERIT;
878dd9f438eSNathan Scott 					ip->i_d.di_extsize = pip->i_d.di_extsize;
879dd9f438eSNathan Scott 				}
8809336e3a7SDave Chinner 				if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
8819336e3a7SDave Chinner 					di_flags |= XFS_DIFLAG_PROJINHERIT;
882abbede1bSAl Viro 			} else if (S_ISREG(mode)) {
883613d7043SChristoph Hellwig 				if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
884365ca83dSNathan Scott 					di_flags |= XFS_DIFLAG_REALTIME;
885dd9f438eSNathan Scott 				if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
886dd9f438eSNathan Scott 					di_flags |= XFS_DIFLAG_EXTSIZE;
887dd9f438eSNathan Scott 					ip->i_d.di_extsize = pip->i_d.di_extsize;
888dd9f438eSNathan Scott 				}
8891da177e4SLinus Torvalds 			}
8901da177e4SLinus Torvalds 			if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
8911da177e4SLinus Torvalds 			    xfs_inherit_noatime)
892365ca83dSNathan Scott 				di_flags |= XFS_DIFLAG_NOATIME;
8931da177e4SLinus Torvalds 			if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
8941da177e4SLinus Torvalds 			    xfs_inherit_nodump)
895365ca83dSNathan Scott 				di_flags |= XFS_DIFLAG_NODUMP;
8961da177e4SLinus Torvalds 			if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
8971da177e4SLinus Torvalds 			    xfs_inherit_sync)
898365ca83dSNathan Scott 				di_flags |= XFS_DIFLAG_SYNC;
8991da177e4SLinus Torvalds 			if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
9001da177e4SLinus Torvalds 			    xfs_inherit_nosymlinks)
901365ca83dSNathan Scott 				di_flags |= XFS_DIFLAG_NOSYMLINKS;
902d3446eacSBarry Naujok 			if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
903d3446eacSBarry Naujok 			    xfs_inherit_nodefrag)
904d3446eacSBarry Naujok 				di_flags |= XFS_DIFLAG_NODEFRAG;
9052a82b8beSDavid Chinner 			if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
9062a82b8beSDavid Chinner 				di_flags |= XFS_DIFLAG_FILESTREAM;
90758f88ca2SDave Chinner 			if (pip->i_d.di_flags2 & XFS_DIFLAG2_DAX)
90858f88ca2SDave Chinner 				di_flags2 |= XFS_DIFLAG2_DAX;
90958f88ca2SDave Chinner 
910365ca83dSNathan Scott 			ip->i_d.di_flags |= di_flags;
91158f88ca2SDave Chinner 			ip->i_d.di_flags2 |= di_flags2;
9121da177e4SLinus Torvalds 		}
9131da177e4SLinus Torvalds 		/* FALLTHROUGH */
9141da177e4SLinus Torvalds 	case S_IFLNK:
9151da177e4SLinus Torvalds 		ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
9161da177e4SLinus Torvalds 		ip->i_df.if_flags = XFS_IFEXTENTS;
9171da177e4SLinus Torvalds 		ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
9181da177e4SLinus Torvalds 		ip->i_df.if_u1.if_extents = NULL;
9191da177e4SLinus Torvalds 		break;
9201da177e4SLinus Torvalds 	default:
9211da177e4SLinus Torvalds 		ASSERT(0);
9221da177e4SLinus Torvalds 	}
9231da177e4SLinus Torvalds 	/*
9241da177e4SLinus Torvalds 	 * Attribute fork settings for new inode.
9251da177e4SLinus Torvalds 	 */
9261da177e4SLinus Torvalds 	ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
9271da177e4SLinus Torvalds 	ip->i_d.di_anextents = 0;
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds 	/*
9301da177e4SLinus Torvalds 	 * Log the new values stuffed into the inode.
9311da177e4SLinus Torvalds 	 */
932ddc3415aSChristoph Hellwig 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
9331da177e4SLinus Torvalds 	xfs_trans_log_inode(tp, ip, flags);
9341da177e4SLinus Torvalds 
93558c90473SDave Chinner 	/* now that we have an i_mode we can setup the inode structure */
93641be8bedSChristoph Hellwig 	xfs_setup_inode(ip);
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds 	*ipp = ip;
9391da177e4SLinus Torvalds 	return 0;
9401da177e4SLinus Torvalds }
9411da177e4SLinus Torvalds 
942e546cb79SDave Chinner /*
943e546cb79SDave Chinner  * Allocates a new inode from disk and return a pointer to the
944e546cb79SDave Chinner  * incore copy. This routine will internally commit the current
945e546cb79SDave Chinner  * transaction and allocate a new one if the Space Manager needed
946e546cb79SDave Chinner  * to do an allocation to replenish the inode free-list.
947e546cb79SDave Chinner  *
948e546cb79SDave Chinner  * This routine is designed to be called from xfs_create and
949e546cb79SDave Chinner  * xfs_create_dir.
950e546cb79SDave Chinner  *
951e546cb79SDave Chinner  */
952e546cb79SDave Chinner int
953e546cb79SDave Chinner xfs_dir_ialloc(
954e546cb79SDave Chinner 	xfs_trans_t	**tpp,		/* input: current transaction;
955e546cb79SDave Chinner 					   output: may be a new transaction. */
956e546cb79SDave Chinner 	xfs_inode_t	*dp,		/* directory within whose allocate
957e546cb79SDave Chinner 					   the inode. */
958e546cb79SDave Chinner 	umode_t		mode,
959e546cb79SDave Chinner 	xfs_nlink_t	nlink,
960e546cb79SDave Chinner 	xfs_dev_t	rdev,
961e546cb79SDave Chinner 	prid_t		prid,		/* project id */
962e546cb79SDave Chinner 	int		okalloc,	/* ok to allocate new space */
963e546cb79SDave Chinner 	xfs_inode_t	**ipp,		/* pointer to inode; it will be
964e546cb79SDave Chinner 					   locked. */
965e546cb79SDave Chinner 	int		*committed)
966e546cb79SDave Chinner 
967e546cb79SDave Chinner {
968e546cb79SDave Chinner 	xfs_trans_t	*tp;
969e546cb79SDave Chinner 	xfs_inode_t	*ip;
970e546cb79SDave Chinner 	xfs_buf_t	*ialloc_context = NULL;
971e546cb79SDave Chinner 	int		code;
972e546cb79SDave Chinner 	void		*dqinfo;
973e546cb79SDave Chinner 	uint		tflags;
974e546cb79SDave Chinner 
975e546cb79SDave Chinner 	tp = *tpp;
976e546cb79SDave Chinner 	ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
977e546cb79SDave Chinner 
978e546cb79SDave Chinner 	/*
979e546cb79SDave Chinner 	 * xfs_ialloc will return a pointer to an incore inode if
980e546cb79SDave Chinner 	 * the Space Manager has an available inode on the free
981e546cb79SDave Chinner 	 * list. Otherwise, it will do an allocation and replenish
982e546cb79SDave Chinner 	 * the freelist.  Since we can only do one allocation per
983e546cb79SDave Chinner 	 * transaction without deadlocks, we will need to commit the
984e546cb79SDave Chinner 	 * current transaction and start a new one.  We will then
985e546cb79SDave Chinner 	 * need to call xfs_ialloc again to get the inode.
986e546cb79SDave Chinner 	 *
987e546cb79SDave Chinner 	 * If xfs_ialloc did an allocation to replenish the freelist,
988e546cb79SDave Chinner 	 * it returns the bp containing the head of the freelist as
989e546cb79SDave Chinner 	 * ialloc_context. We will hold a lock on it across the
990e546cb79SDave Chinner 	 * transaction commit so that no other process can steal
991e546cb79SDave Chinner 	 * the inode(s) that we've just allocated.
992e546cb79SDave Chinner 	 */
993e546cb79SDave Chinner 	code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid, okalloc,
994e546cb79SDave Chinner 			  &ialloc_context, &ip);
995e546cb79SDave Chinner 
996e546cb79SDave Chinner 	/*
997e546cb79SDave Chinner 	 * Return an error if we were unable to allocate a new inode.
998e546cb79SDave Chinner 	 * This should only happen if we run out of space on disk or
999e546cb79SDave Chinner 	 * encounter a disk error.
1000e546cb79SDave Chinner 	 */
1001e546cb79SDave Chinner 	if (code) {
1002e546cb79SDave Chinner 		*ipp = NULL;
1003e546cb79SDave Chinner 		return code;
1004e546cb79SDave Chinner 	}
1005e546cb79SDave Chinner 	if (!ialloc_context && !ip) {
1006e546cb79SDave Chinner 		*ipp = NULL;
10072451337dSDave Chinner 		return -ENOSPC;
1008e546cb79SDave Chinner 	}
1009e546cb79SDave Chinner 
1010e546cb79SDave Chinner 	/*
1011e546cb79SDave Chinner 	 * If the AGI buffer is non-NULL, then we were unable to get an
1012e546cb79SDave Chinner 	 * inode in one operation.  We need to commit the current
1013e546cb79SDave Chinner 	 * transaction and call xfs_ialloc() again.  It is guaranteed
1014e546cb79SDave Chinner 	 * to succeed the second time.
1015e546cb79SDave Chinner 	 */
1016e546cb79SDave Chinner 	if (ialloc_context) {
1017e546cb79SDave Chinner 		/*
1018e546cb79SDave Chinner 		 * Normally, xfs_trans_commit releases all the locks.
1019e546cb79SDave Chinner 		 * We call bhold to hang on to the ialloc_context across
1020e546cb79SDave Chinner 		 * the commit.  Holding this buffer prevents any other
1021e546cb79SDave Chinner 		 * processes from doing any allocations in this
1022e546cb79SDave Chinner 		 * allocation group.
1023e546cb79SDave Chinner 		 */
1024e546cb79SDave Chinner 		xfs_trans_bhold(tp, ialloc_context);
1025e546cb79SDave Chinner 
1026e546cb79SDave Chinner 		/*
1027e546cb79SDave Chinner 		 * We want the quota changes to be associated with the next
1028e546cb79SDave Chinner 		 * transaction, NOT this one. So, detach the dqinfo from this
1029e546cb79SDave Chinner 		 * and attach it to the next transaction.
1030e546cb79SDave Chinner 		 */
1031e546cb79SDave Chinner 		dqinfo = NULL;
1032e546cb79SDave Chinner 		tflags = 0;
1033e546cb79SDave Chinner 		if (tp->t_dqinfo) {
1034e546cb79SDave Chinner 			dqinfo = (void *)tp->t_dqinfo;
1035e546cb79SDave Chinner 			tp->t_dqinfo = NULL;
1036e546cb79SDave Chinner 			tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY;
1037e546cb79SDave Chinner 			tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY);
1038e546cb79SDave Chinner 		}
1039e546cb79SDave Chinner 
10402e6db6c4SChristoph Hellwig 		code = xfs_trans_roll(&tp, 0);
10412e6db6c4SChristoph Hellwig 		if (committed != NULL)
1042e546cb79SDave Chinner 			*committed = 1;
10433d3c8b52SJie Liu 
1044e546cb79SDave Chinner 		/*
1045e546cb79SDave Chinner 		 * Re-attach the quota info that we detached from prev trx.
1046e546cb79SDave Chinner 		 */
1047e546cb79SDave Chinner 		if (dqinfo) {
1048e546cb79SDave Chinner 			tp->t_dqinfo = dqinfo;
1049e546cb79SDave Chinner 			tp->t_flags |= tflags;
1050e546cb79SDave Chinner 		}
1051e546cb79SDave Chinner 
1052e546cb79SDave Chinner 		if (code) {
1053e546cb79SDave Chinner 			xfs_buf_relse(ialloc_context);
10542e6db6c4SChristoph Hellwig 			*tpp = tp;
1055e546cb79SDave Chinner 			*ipp = NULL;
1056e546cb79SDave Chinner 			return code;
1057e546cb79SDave Chinner 		}
1058e546cb79SDave Chinner 		xfs_trans_bjoin(tp, ialloc_context);
1059e546cb79SDave Chinner 
1060e546cb79SDave Chinner 		/*
1061e546cb79SDave Chinner 		 * Call ialloc again. Since we've locked out all
1062e546cb79SDave Chinner 		 * other allocations in this allocation group,
1063e546cb79SDave Chinner 		 * this call should always succeed.
1064e546cb79SDave Chinner 		 */
1065e546cb79SDave Chinner 		code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid,
1066e546cb79SDave Chinner 				  okalloc, &ialloc_context, &ip);
1067e546cb79SDave Chinner 
1068e546cb79SDave Chinner 		/*
1069e546cb79SDave Chinner 		 * If we get an error at this point, return to the caller
1070e546cb79SDave Chinner 		 * so that the current transaction can be aborted.
1071e546cb79SDave Chinner 		 */
1072e546cb79SDave Chinner 		if (code) {
1073e546cb79SDave Chinner 			*tpp = tp;
1074e546cb79SDave Chinner 			*ipp = NULL;
1075e546cb79SDave Chinner 			return code;
1076e546cb79SDave Chinner 		}
1077e546cb79SDave Chinner 		ASSERT(!ialloc_context && ip);
1078e546cb79SDave Chinner 
1079e546cb79SDave Chinner 	} else {
1080e546cb79SDave Chinner 		if (committed != NULL)
1081e546cb79SDave Chinner 			*committed = 0;
1082e546cb79SDave Chinner 	}
1083e546cb79SDave Chinner 
1084e546cb79SDave Chinner 	*ipp = ip;
1085e546cb79SDave Chinner 	*tpp = tp;
1086e546cb79SDave Chinner 
1087e546cb79SDave Chinner 	return 0;
1088e546cb79SDave Chinner }
1089e546cb79SDave Chinner 
1090e546cb79SDave Chinner /*
1091e546cb79SDave Chinner  * Decrement the link count on an inode & log the change.
1092e546cb79SDave Chinner  * If this causes the link count to go to zero, initiate the
1093e546cb79SDave Chinner  * logging activity required to truncate a file.
1094e546cb79SDave Chinner  */
1095e546cb79SDave Chinner int				/* error */
1096e546cb79SDave Chinner xfs_droplink(
1097e546cb79SDave Chinner 	xfs_trans_t *tp,
1098e546cb79SDave Chinner 	xfs_inode_t *ip)
1099e546cb79SDave Chinner {
1100e546cb79SDave Chinner 	int	error;
1101e546cb79SDave Chinner 
1102e546cb79SDave Chinner 	xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1103e546cb79SDave Chinner 
1104e546cb79SDave Chinner 	ASSERT (ip->i_d.di_nlink > 0);
1105e546cb79SDave Chinner 	ip->i_d.di_nlink--;
1106e546cb79SDave Chinner 	drop_nlink(VFS_I(ip));
1107e546cb79SDave Chinner 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1108e546cb79SDave Chinner 
1109e546cb79SDave Chinner 	error = 0;
1110e546cb79SDave Chinner 	if (ip->i_d.di_nlink == 0) {
1111e546cb79SDave Chinner 		/*
1112e546cb79SDave Chinner 		 * We're dropping the last link to this file.
1113e546cb79SDave Chinner 		 * Move the on-disk inode to the AGI unlinked list.
1114e546cb79SDave Chinner 		 * From xfs_inactive() we will pull the inode from
1115e546cb79SDave Chinner 		 * the list and free it.
1116e546cb79SDave Chinner 		 */
1117e546cb79SDave Chinner 		error = xfs_iunlink(tp, ip);
1118e546cb79SDave Chinner 	}
1119e546cb79SDave Chinner 	return error;
1120e546cb79SDave Chinner }
1121e546cb79SDave Chinner 
1122e546cb79SDave Chinner /*
1123e546cb79SDave Chinner  * Increment the link count on an inode & log the change.
1124e546cb79SDave Chinner  */
1125e546cb79SDave Chinner int
1126e546cb79SDave Chinner xfs_bumplink(
1127e546cb79SDave Chinner 	xfs_trans_t *tp,
1128e546cb79SDave Chinner 	xfs_inode_t *ip)
1129e546cb79SDave Chinner {
1130e546cb79SDave Chinner 	xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1131e546cb79SDave Chinner 
1132263997a6SDave Chinner 	ASSERT(ip->i_d.di_version > 1);
1133ab297431SZhi Yong Wu 	ASSERT(ip->i_d.di_nlink > 0 || (VFS_I(ip)->i_state & I_LINKABLE));
1134e546cb79SDave Chinner 	ip->i_d.di_nlink++;
1135e546cb79SDave Chinner 	inc_nlink(VFS_I(ip));
1136e546cb79SDave Chinner 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1137e546cb79SDave Chinner 	return 0;
1138e546cb79SDave Chinner }
1139e546cb79SDave Chinner 
1140c24b5dfaSDave Chinner int
1141c24b5dfaSDave Chinner xfs_create(
1142c24b5dfaSDave Chinner 	xfs_inode_t		*dp,
1143c24b5dfaSDave Chinner 	struct xfs_name		*name,
1144c24b5dfaSDave Chinner 	umode_t			mode,
1145c24b5dfaSDave Chinner 	xfs_dev_t		rdev,
1146c24b5dfaSDave Chinner 	xfs_inode_t		**ipp)
1147c24b5dfaSDave Chinner {
1148c24b5dfaSDave Chinner 	int			is_dir = S_ISDIR(mode);
1149c24b5dfaSDave Chinner 	struct xfs_mount	*mp = dp->i_mount;
1150c24b5dfaSDave Chinner 	struct xfs_inode	*ip = NULL;
1151c24b5dfaSDave Chinner 	struct xfs_trans	*tp = NULL;
1152c24b5dfaSDave Chinner 	int			error;
1153c24b5dfaSDave Chinner 	xfs_bmap_free_t		free_list;
1154c24b5dfaSDave Chinner 	xfs_fsblock_t		first_block;
1155c24b5dfaSDave Chinner 	bool                    unlock_dp_on_error = false;
1156c24b5dfaSDave Chinner 	prid_t			prid;
1157c24b5dfaSDave Chinner 	struct xfs_dquot	*udqp = NULL;
1158c24b5dfaSDave Chinner 	struct xfs_dquot	*gdqp = NULL;
1159c24b5dfaSDave Chinner 	struct xfs_dquot	*pdqp = NULL;
1160062647a8SBrian Foster 	struct xfs_trans_res	*tres;
1161c24b5dfaSDave Chinner 	uint			resblks;
1162c24b5dfaSDave Chinner 
1163c24b5dfaSDave Chinner 	trace_xfs_create(dp, name);
1164c24b5dfaSDave Chinner 
1165c24b5dfaSDave Chinner 	if (XFS_FORCED_SHUTDOWN(mp))
11662451337dSDave Chinner 		return -EIO;
1167c24b5dfaSDave Chinner 
1168163467d3SZhi Yong Wu 	prid = xfs_get_initial_prid(dp);
1169c24b5dfaSDave Chinner 
1170c24b5dfaSDave Chinner 	/*
1171c24b5dfaSDave Chinner 	 * Make sure that we have allocated dquot(s) on disk.
1172c24b5dfaSDave Chinner 	 */
11737aab1b28SDwight Engen 	error = xfs_qm_vop_dqalloc(dp, xfs_kuid_to_uid(current_fsuid()),
11747aab1b28SDwight Engen 					xfs_kgid_to_gid(current_fsgid()), prid,
1175c24b5dfaSDave Chinner 					XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
1176c24b5dfaSDave Chinner 					&udqp, &gdqp, &pdqp);
1177c24b5dfaSDave Chinner 	if (error)
1178c24b5dfaSDave Chinner 		return error;
1179c24b5dfaSDave Chinner 
1180c24b5dfaSDave Chinner 	if (is_dir) {
1181c24b5dfaSDave Chinner 		rdev = 0;
1182c24b5dfaSDave Chinner 		resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
1183062647a8SBrian Foster 		tres = &M_RES(mp)->tr_mkdir;
1184c24b5dfaSDave Chinner 		tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
1185c24b5dfaSDave Chinner 	} else {
1186c24b5dfaSDave Chinner 		resblks = XFS_CREATE_SPACE_RES(mp, name->len);
1187062647a8SBrian Foster 		tres = &M_RES(mp)->tr_create;
1188c24b5dfaSDave Chinner 		tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1189c24b5dfaSDave Chinner 	}
1190c24b5dfaSDave Chinner 
1191c24b5dfaSDave Chinner 	/*
1192c24b5dfaSDave Chinner 	 * Initially assume that the file does not exist and
1193c24b5dfaSDave Chinner 	 * reserve the resources for that case.  If that is not
1194c24b5dfaSDave Chinner 	 * the case we'll drop the one we have and get a more
1195c24b5dfaSDave Chinner 	 * appropriate transaction later.
1196c24b5dfaSDave Chinner 	 */
1197062647a8SBrian Foster 	error = xfs_trans_reserve(tp, tres, resblks, 0);
11982451337dSDave Chinner 	if (error == -ENOSPC) {
1199c24b5dfaSDave Chinner 		/* flush outstanding delalloc blocks and retry */
1200c24b5dfaSDave Chinner 		xfs_flush_inodes(mp);
1201062647a8SBrian Foster 		error = xfs_trans_reserve(tp, tres, resblks, 0);
1202c24b5dfaSDave Chinner 	}
12032451337dSDave Chinner 	if (error == -ENOSPC) {
1204c24b5dfaSDave Chinner 		/* No space at all so try a "no-allocation" reservation */
1205c24b5dfaSDave Chinner 		resblks = 0;
1206062647a8SBrian Foster 		error = xfs_trans_reserve(tp, tres, 0, 0);
1207c24b5dfaSDave Chinner 	}
12084906e215SChristoph Hellwig 	if (error)
1209c24b5dfaSDave Chinner 		goto out_trans_cancel;
12104906e215SChristoph Hellwig 
1211c24b5dfaSDave Chinner 
1212dbad7c99SDave Chinner 	xfs_ilock(dp, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL |
1213dbad7c99SDave Chinner 		      XFS_IOLOCK_PARENT | XFS_ILOCK_PARENT);
1214c24b5dfaSDave Chinner 	unlock_dp_on_error = true;
1215c24b5dfaSDave Chinner 
1216c24b5dfaSDave Chinner 	xfs_bmap_init(&free_list, &first_block);
1217c24b5dfaSDave Chinner 
1218c24b5dfaSDave Chinner 	/*
1219c24b5dfaSDave Chinner 	 * Reserve disk quota and the inode.
1220c24b5dfaSDave Chinner 	 */
1221c24b5dfaSDave Chinner 	error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
1222c24b5dfaSDave Chinner 						pdqp, resblks, 1, 0);
1223c24b5dfaSDave Chinner 	if (error)
1224c24b5dfaSDave Chinner 		goto out_trans_cancel;
1225c24b5dfaSDave Chinner 
122694f3cad5SEric Sandeen 	if (!resblks) {
122794f3cad5SEric Sandeen 		error = xfs_dir_canenter(tp, dp, name);
1228c24b5dfaSDave Chinner 		if (error)
1229c24b5dfaSDave Chinner 			goto out_trans_cancel;
123094f3cad5SEric Sandeen 	}
1231c24b5dfaSDave Chinner 
1232c24b5dfaSDave Chinner 	/*
1233c24b5dfaSDave Chinner 	 * A newly created regular or special file just has one directory
1234c24b5dfaSDave Chinner 	 * entry pointing to them, but a directory also the "." entry
1235c24b5dfaSDave Chinner 	 * pointing to itself.
1236c24b5dfaSDave Chinner 	 */
1237c24b5dfaSDave Chinner 	error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev,
1238f6106efaSEric Sandeen 			       prid, resblks > 0, &ip, NULL);
1239d6077aa3SJan Kara 	if (error)
1240c24b5dfaSDave Chinner 		goto out_trans_cancel;
1241c24b5dfaSDave Chinner 
1242c24b5dfaSDave Chinner 	/*
1243c24b5dfaSDave Chinner 	 * Now we join the directory inode to the transaction.  We do not do it
1244c24b5dfaSDave Chinner 	 * earlier because xfs_dir_ialloc might commit the previous transaction
1245c24b5dfaSDave Chinner 	 * (and release all the locks).  An error from here on will result in
1246c24b5dfaSDave Chinner 	 * the transaction cancel unlocking dp so don't do it explicitly in the
1247c24b5dfaSDave Chinner 	 * error path.
1248c24b5dfaSDave Chinner 	 */
1249dbad7c99SDave Chinner 	xfs_trans_ijoin(tp, dp, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1250c24b5dfaSDave Chinner 	unlock_dp_on_error = false;
1251c24b5dfaSDave Chinner 
1252c24b5dfaSDave Chinner 	error = xfs_dir_createname(tp, dp, name, ip->i_ino,
1253c24b5dfaSDave Chinner 					&first_block, &free_list, resblks ?
1254c24b5dfaSDave Chinner 					resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
1255c24b5dfaSDave Chinner 	if (error) {
12562451337dSDave Chinner 		ASSERT(error != -ENOSPC);
12574906e215SChristoph Hellwig 		goto out_trans_cancel;
1258c24b5dfaSDave Chinner 	}
1259c24b5dfaSDave Chinner 	xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1260c24b5dfaSDave Chinner 	xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1261c24b5dfaSDave Chinner 
1262c24b5dfaSDave Chinner 	if (is_dir) {
1263c24b5dfaSDave Chinner 		error = xfs_dir_init(tp, ip, dp);
1264c24b5dfaSDave Chinner 		if (error)
1265c24b5dfaSDave Chinner 			goto out_bmap_cancel;
1266c24b5dfaSDave Chinner 
1267c24b5dfaSDave Chinner 		error = xfs_bumplink(tp, dp);
1268c24b5dfaSDave Chinner 		if (error)
1269c24b5dfaSDave Chinner 			goto out_bmap_cancel;
1270c24b5dfaSDave Chinner 	}
1271c24b5dfaSDave Chinner 
1272c24b5dfaSDave Chinner 	/*
1273c24b5dfaSDave Chinner 	 * If this is a synchronous mount, make sure that the
1274c24b5dfaSDave Chinner 	 * create transaction goes to disk before returning to
1275c24b5dfaSDave Chinner 	 * the user.
1276c24b5dfaSDave Chinner 	 */
1277c24b5dfaSDave Chinner 	if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1278c24b5dfaSDave Chinner 		xfs_trans_set_sync(tp);
1279c24b5dfaSDave Chinner 
1280c24b5dfaSDave Chinner 	/*
1281c24b5dfaSDave Chinner 	 * Attach the dquot(s) to the inodes and modify them incore.
1282c24b5dfaSDave Chinner 	 * These ids of the inode couldn't have changed since the new
1283c24b5dfaSDave Chinner 	 * inode has been locked ever since it was created.
1284c24b5dfaSDave Chinner 	 */
1285c24b5dfaSDave Chinner 	xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
1286c24b5dfaSDave Chinner 
1287f6106efaSEric Sandeen 	error = xfs_bmap_finish(&tp, &free_list, NULL);
1288c24b5dfaSDave Chinner 	if (error)
1289c24b5dfaSDave Chinner 		goto out_bmap_cancel;
1290c24b5dfaSDave Chinner 
129170393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
1292c24b5dfaSDave Chinner 	if (error)
1293c24b5dfaSDave Chinner 		goto out_release_inode;
1294c24b5dfaSDave Chinner 
1295c24b5dfaSDave Chinner 	xfs_qm_dqrele(udqp);
1296c24b5dfaSDave Chinner 	xfs_qm_dqrele(gdqp);
1297c24b5dfaSDave Chinner 	xfs_qm_dqrele(pdqp);
1298c24b5dfaSDave Chinner 
1299c24b5dfaSDave Chinner 	*ipp = ip;
1300c24b5dfaSDave Chinner 	return 0;
1301c24b5dfaSDave Chinner 
1302c24b5dfaSDave Chinner  out_bmap_cancel:
1303c24b5dfaSDave Chinner 	xfs_bmap_cancel(&free_list);
1304c24b5dfaSDave Chinner  out_trans_cancel:
13054906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
1306c24b5dfaSDave Chinner  out_release_inode:
1307c24b5dfaSDave Chinner 	/*
130858c90473SDave Chinner 	 * Wait until after the current transaction is aborted to finish the
130958c90473SDave Chinner 	 * setup of the inode and release the inode.  This prevents recursive
131058c90473SDave Chinner 	 * transactions and deadlocks from xfs_inactive.
1311c24b5dfaSDave Chinner 	 */
131258c90473SDave Chinner 	if (ip) {
131358c90473SDave Chinner 		xfs_finish_inode_setup(ip);
1314c24b5dfaSDave Chinner 		IRELE(ip);
131558c90473SDave Chinner 	}
1316c24b5dfaSDave Chinner 
1317c24b5dfaSDave Chinner 	xfs_qm_dqrele(udqp);
1318c24b5dfaSDave Chinner 	xfs_qm_dqrele(gdqp);
1319c24b5dfaSDave Chinner 	xfs_qm_dqrele(pdqp);
1320c24b5dfaSDave Chinner 
1321c24b5dfaSDave Chinner 	if (unlock_dp_on_error)
1322dbad7c99SDave Chinner 		xfs_iunlock(dp, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1323c24b5dfaSDave Chinner 	return error;
1324c24b5dfaSDave Chinner }
1325c24b5dfaSDave Chinner 
1326c24b5dfaSDave Chinner int
132799b6436bSZhi Yong Wu xfs_create_tmpfile(
132899b6436bSZhi Yong Wu 	struct xfs_inode	*dp,
132999b6436bSZhi Yong Wu 	struct dentry		*dentry,
1330330033d6SBrian Foster 	umode_t			mode,
1331330033d6SBrian Foster 	struct xfs_inode	**ipp)
133299b6436bSZhi Yong Wu {
133399b6436bSZhi Yong Wu 	struct xfs_mount	*mp = dp->i_mount;
133499b6436bSZhi Yong Wu 	struct xfs_inode	*ip = NULL;
133599b6436bSZhi Yong Wu 	struct xfs_trans	*tp = NULL;
133699b6436bSZhi Yong Wu 	int			error;
133799b6436bSZhi Yong Wu 	prid_t                  prid;
133899b6436bSZhi Yong Wu 	struct xfs_dquot	*udqp = NULL;
133999b6436bSZhi Yong Wu 	struct xfs_dquot	*gdqp = NULL;
134099b6436bSZhi Yong Wu 	struct xfs_dquot	*pdqp = NULL;
134199b6436bSZhi Yong Wu 	struct xfs_trans_res	*tres;
134299b6436bSZhi Yong Wu 	uint			resblks;
134399b6436bSZhi Yong Wu 
134499b6436bSZhi Yong Wu 	if (XFS_FORCED_SHUTDOWN(mp))
13452451337dSDave Chinner 		return -EIO;
134699b6436bSZhi Yong Wu 
134799b6436bSZhi Yong Wu 	prid = xfs_get_initial_prid(dp);
134899b6436bSZhi Yong Wu 
134999b6436bSZhi Yong Wu 	/*
135099b6436bSZhi Yong Wu 	 * Make sure that we have allocated dquot(s) on disk.
135199b6436bSZhi Yong Wu 	 */
135299b6436bSZhi Yong Wu 	error = xfs_qm_vop_dqalloc(dp, xfs_kuid_to_uid(current_fsuid()),
135399b6436bSZhi Yong Wu 				xfs_kgid_to_gid(current_fsgid()), prid,
135499b6436bSZhi Yong Wu 				XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
135599b6436bSZhi Yong Wu 				&udqp, &gdqp, &pdqp);
135699b6436bSZhi Yong Wu 	if (error)
135799b6436bSZhi Yong Wu 		return error;
135899b6436bSZhi Yong Wu 
135999b6436bSZhi Yong Wu 	resblks = XFS_IALLOC_SPACE_RES(mp);
136099b6436bSZhi Yong Wu 	tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE_TMPFILE);
136199b6436bSZhi Yong Wu 
136299b6436bSZhi Yong Wu 	tres = &M_RES(mp)->tr_create_tmpfile;
136399b6436bSZhi Yong Wu 	error = xfs_trans_reserve(tp, tres, resblks, 0);
13642451337dSDave Chinner 	if (error == -ENOSPC) {
136599b6436bSZhi Yong Wu 		/* No space at all so try a "no-allocation" reservation */
136699b6436bSZhi Yong Wu 		resblks = 0;
136799b6436bSZhi Yong Wu 		error = xfs_trans_reserve(tp, tres, 0, 0);
136899b6436bSZhi Yong Wu 	}
13694906e215SChristoph Hellwig 	if (error)
137099b6436bSZhi Yong Wu 		goto out_trans_cancel;
137199b6436bSZhi Yong Wu 
137299b6436bSZhi Yong Wu 	error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
137399b6436bSZhi Yong Wu 						pdqp, resblks, 1, 0);
137499b6436bSZhi Yong Wu 	if (error)
137599b6436bSZhi Yong Wu 		goto out_trans_cancel;
137699b6436bSZhi Yong Wu 
137799b6436bSZhi Yong Wu 	error = xfs_dir_ialloc(&tp, dp, mode, 1, 0,
137899b6436bSZhi Yong Wu 				prid, resblks > 0, &ip, NULL);
1379d6077aa3SJan Kara 	if (error)
138099b6436bSZhi Yong Wu 		goto out_trans_cancel;
138199b6436bSZhi Yong Wu 
138299b6436bSZhi Yong Wu 	if (mp->m_flags & XFS_MOUNT_WSYNC)
138399b6436bSZhi Yong Wu 		xfs_trans_set_sync(tp);
138499b6436bSZhi Yong Wu 
138599b6436bSZhi Yong Wu 	/*
138699b6436bSZhi Yong Wu 	 * Attach the dquot(s) to the inodes and modify them incore.
138799b6436bSZhi Yong Wu 	 * These ids of the inode couldn't have changed since the new
138899b6436bSZhi Yong Wu 	 * inode has been locked ever since it was created.
138999b6436bSZhi Yong Wu 	 */
139099b6436bSZhi Yong Wu 	xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
139199b6436bSZhi Yong Wu 
139299b6436bSZhi Yong Wu 	ip->i_d.di_nlink--;
139399b6436bSZhi Yong Wu 	error = xfs_iunlink(tp, ip);
139499b6436bSZhi Yong Wu 	if (error)
13954906e215SChristoph Hellwig 		goto out_trans_cancel;
139699b6436bSZhi Yong Wu 
139770393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
139899b6436bSZhi Yong Wu 	if (error)
139999b6436bSZhi Yong Wu 		goto out_release_inode;
140099b6436bSZhi Yong Wu 
140199b6436bSZhi Yong Wu 	xfs_qm_dqrele(udqp);
140299b6436bSZhi Yong Wu 	xfs_qm_dqrele(gdqp);
140399b6436bSZhi Yong Wu 	xfs_qm_dqrele(pdqp);
140499b6436bSZhi Yong Wu 
1405330033d6SBrian Foster 	*ipp = ip;
140699b6436bSZhi Yong Wu 	return 0;
140799b6436bSZhi Yong Wu 
140899b6436bSZhi Yong Wu  out_trans_cancel:
14094906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
141099b6436bSZhi Yong Wu  out_release_inode:
141199b6436bSZhi Yong Wu 	/*
141258c90473SDave Chinner 	 * Wait until after the current transaction is aborted to finish the
141358c90473SDave Chinner 	 * setup of the inode and release the inode.  This prevents recursive
141458c90473SDave Chinner 	 * transactions and deadlocks from xfs_inactive.
141599b6436bSZhi Yong Wu 	 */
141658c90473SDave Chinner 	if (ip) {
141758c90473SDave Chinner 		xfs_finish_inode_setup(ip);
141899b6436bSZhi Yong Wu 		IRELE(ip);
141958c90473SDave Chinner 	}
142099b6436bSZhi Yong Wu 
142199b6436bSZhi Yong Wu 	xfs_qm_dqrele(udqp);
142299b6436bSZhi Yong Wu 	xfs_qm_dqrele(gdqp);
142399b6436bSZhi Yong Wu 	xfs_qm_dqrele(pdqp);
142499b6436bSZhi Yong Wu 
142599b6436bSZhi Yong Wu 	return error;
142699b6436bSZhi Yong Wu }
142799b6436bSZhi Yong Wu 
142899b6436bSZhi Yong Wu int
1429c24b5dfaSDave Chinner xfs_link(
1430c24b5dfaSDave Chinner 	xfs_inode_t		*tdp,
1431c24b5dfaSDave Chinner 	xfs_inode_t		*sip,
1432c24b5dfaSDave Chinner 	struct xfs_name		*target_name)
1433c24b5dfaSDave Chinner {
1434c24b5dfaSDave Chinner 	xfs_mount_t		*mp = tdp->i_mount;
1435c24b5dfaSDave Chinner 	xfs_trans_t		*tp;
1436c24b5dfaSDave Chinner 	int			error;
1437c24b5dfaSDave Chinner 	xfs_bmap_free_t         free_list;
1438c24b5dfaSDave Chinner 	xfs_fsblock_t           first_block;
1439c24b5dfaSDave Chinner 	int			resblks;
1440c24b5dfaSDave Chinner 
1441c24b5dfaSDave Chinner 	trace_xfs_link(tdp, target_name);
1442c24b5dfaSDave Chinner 
1443c24b5dfaSDave Chinner 	ASSERT(!S_ISDIR(sip->i_d.di_mode));
1444c24b5dfaSDave Chinner 
1445c24b5dfaSDave Chinner 	if (XFS_FORCED_SHUTDOWN(mp))
14462451337dSDave Chinner 		return -EIO;
1447c24b5dfaSDave Chinner 
1448c24b5dfaSDave Chinner 	error = xfs_qm_dqattach(sip, 0);
1449c24b5dfaSDave Chinner 	if (error)
1450c24b5dfaSDave Chinner 		goto std_return;
1451c24b5dfaSDave Chinner 
1452c24b5dfaSDave Chinner 	error = xfs_qm_dqattach(tdp, 0);
1453c24b5dfaSDave Chinner 	if (error)
1454c24b5dfaSDave Chinner 		goto std_return;
1455c24b5dfaSDave Chinner 
1456c24b5dfaSDave Chinner 	tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
1457c24b5dfaSDave Chinner 	resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
14583d3c8b52SJie Liu 	error = xfs_trans_reserve(tp, &M_RES(mp)->tr_link, resblks, 0);
14592451337dSDave Chinner 	if (error == -ENOSPC) {
1460c24b5dfaSDave Chinner 		resblks = 0;
14613d3c8b52SJie Liu 		error = xfs_trans_reserve(tp, &M_RES(mp)->tr_link, 0, 0);
1462c24b5dfaSDave Chinner 	}
14634906e215SChristoph Hellwig 	if (error)
1464c24b5dfaSDave Chinner 		goto error_return;
1465c24b5dfaSDave Chinner 
1466dbad7c99SDave Chinner 	xfs_ilock(tdp, XFS_IOLOCK_EXCL | XFS_IOLOCK_PARENT);
1467c24b5dfaSDave Chinner 	xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
1468c24b5dfaSDave Chinner 
1469c24b5dfaSDave Chinner 	xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
1470dbad7c99SDave Chinner 	xfs_trans_ijoin(tp, tdp, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1471c24b5dfaSDave Chinner 
1472c24b5dfaSDave Chinner 	/*
1473c24b5dfaSDave Chinner 	 * If we are using project inheritance, we only allow hard link
1474c24b5dfaSDave Chinner 	 * creation in our tree when the project IDs are the same; else
1475c24b5dfaSDave Chinner 	 * the tree quota mechanism could be circumvented.
1476c24b5dfaSDave Chinner 	 */
1477c24b5dfaSDave Chinner 	if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
1478c24b5dfaSDave Chinner 		     (xfs_get_projid(tdp) != xfs_get_projid(sip)))) {
14792451337dSDave Chinner 		error = -EXDEV;
1480c24b5dfaSDave Chinner 		goto error_return;
1481c24b5dfaSDave Chinner 	}
1482c24b5dfaSDave Chinner 
148394f3cad5SEric Sandeen 	if (!resblks) {
148494f3cad5SEric Sandeen 		error = xfs_dir_canenter(tp, tdp, target_name);
1485c24b5dfaSDave Chinner 		if (error)
1486c24b5dfaSDave Chinner 			goto error_return;
148794f3cad5SEric Sandeen 	}
1488c24b5dfaSDave Chinner 
1489c24b5dfaSDave Chinner 	xfs_bmap_init(&free_list, &first_block);
1490c24b5dfaSDave Chinner 
1491ab297431SZhi Yong Wu 	if (sip->i_d.di_nlink == 0) {
1492ab297431SZhi Yong Wu 		error = xfs_iunlink_remove(tp, sip);
1493ab297431SZhi Yong Wu 		if (error)
14944906e215SChristoph Hellwig 			goto error_return;
1495ab297431SZhi Yong Wu 	}
1496ab297431SZhi Yong Wu 
1497c24b5dfaSDave Chinner 	error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
1498c24b5dfaSDave Chinner 					&first_block, &free_list, resblks);
1499c24b5dfaSDave Chinner 	if (error)
15004906e215SChristoph Hellwig 		goto error_return;
1501c24b5dfaSDave Chinner 	xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1502c24b5dfaSDave Chinner 	xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
1503c24b5dfaSDave Chinner 
1504c24b5dfaSDave Chinner 	error = xfs_bumplink(tp, sip);
1505c24b5dfaSDave Chinner 	if (error)
15064906e215SChristoph Hellwig 		goto error_return;
1507c24b5dfaSDave Chinner 
1508c24b5dfaSDave Chinner 	/*
1509c24b5dfaSDave Chinner 	 * If this is a synchronous mount, make sure that the
1510c24b5dfaSDave Chinner 	 * link transaction goes to disk before returning to
1511c24b5dfaSDave Chinner 	 * the user.
1512c24b5dfaSDave Chinner 	 */
1513f6106efaSEric Sandeen 	if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1514c24b5dfaSDave Chinner 		xfs_trans_set_sync(tp);
1515c24b5dfaSDave Chinner 
1516f6106efaSEric Sandeen 	error = xfs_bmap_finish(&tp, &free_list, NULL);
1517c24b5dfaSDave Chinner 	if (error) {
1518c24b5dfaSDave Chinner 		xfs_bmap_cancel(&free_list);
15194906e215SChristoph Hellwig 		goto error_return;
1520c24b5dfaSDave Chinner 	}
1521c24b5dfaSDave Chinner 
152270393313SChristoph Hellwig 	return xfs_trans_commit(tp);
1523c24b5dfaSDave Chinner 
1524c24b5dfaSDave Chinner  error_return:
15254906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
1526c24b5dfaSDave Chinner  std_return:
1527c24b5dfaSDave Chinner 	return error;
1528c24b5dfaSDave Chinner }
1529c24b5dfaSDave Chinner 
15301da177e4SLinus Torvalds /*
15318f04c47aSChristoph Hellwig  * Free up the underlying blocks past new_size.  The new size must be smaller
15328f04c47aSChristoph Hellwig  * than the current size.  This routine can be used both for the attribute and
15338f04c47aSChristoph Hellwig  * data fork, and does not modify the inode size, which is left to the caller.
15341da177e4SLinus Torvalds  *
1535f6485057SDavid Chinner  * The transaction passed to this routine must have made a permanent log
1536f6485057SDavid Chinner  * reservation of at least XFS_ITRUNCATE_LOG_RES.  This routine may commit the
1537f6485057SDavid Chinner  * given transaction and start new ones, so make sure everything involved in
1538f6485057SDavid Chinner  * the transaction is tidy before calling here.  Some transaction will be
1539f6485057SDavid Chinner  * returned to the caller to be committed.  The incoming transaction must
1540f6485057SDavid Chinner  * already include the inode, and both inode locks must be held exclusively.
1541f6485057SDavid Chinner  * The inode must also be "held" within the transaction.  On return the inode
1542f6485057SDavid Chinner  * will be "held" within the returned transaction.  This routine does NOT
1543f6485057SDavid Chinner  * require any disk space to be reserved for it within the transaction.
15441da177e4SLinus Torvalds  *
1545f6485057SDavid Chinner  * If we get an error, we must return with the inode locked and linked into the
1546f6485057SDavid Chinner  * current transaction. This keeps things simple for the higher level code,
1547f6485057SDavid Chinner  * because it always knows that the inode is locked and held in the transaction
1548f6485057SDavid Chinner  * that returns to it whether errors occur or not.  We don't mark the inode
1549f6485057SDavid Chinner  * dirty on error so that transactions can be easily aborted if possible.
15501da177e4SLinus Torvalds  */
15511da177e4SLinus Torvalds int
15528f04c47aSChristoph Hellwig xfs_itruncate_extents(
15538f04c47aSChristoph Hellwig 	struct xfs_trans	**tpp,
15548f04c47aSChristoph Hellwig 	struct xfs_inode	*ip,
15558f04c47aSChristoph Hellwig 	int			whichfork,
15568f04c47aSChristoph Hellwig 	xfs_fsize_t		new_size)
15571da177e4SLinus Torvalds {
15588f04c47aSChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
15598f04c47aSChristoph Hellwig 	struct xfs_trans	*tp = *tpp;
15608f04c47aSChristoph Hellwig 	xfs_bmap_free_t		free_list;
15611da177e4SLinus Torvalds 	xfs_fsblock_t		first_block;
15621da177e4SLinus Torvalds 	xfs_fileoff_t		first_unmap_block;
15631da177e4SLinus Torvalds 	xfs_fileoff_t		last_block;
15648f04c47aSChristoph Hellwig 	xfs_filblks_t		unmap_len;
15658f04c47aSChristoph Hellwig 	int			error = 0;
15668f04c47aSChristoph Hellwig 	int			done = 0;
15671da177e4SLinus Torvalds 
15680b56185bSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
15690b56185bSChristoph Hellwig 	ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
15700b56185bSChristoph Hellwig 	       xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1571ce7ae151SChristoph Hellwig 	ASSERT(new_size <= XFS_ISIZE(ip));
15728f04c47aSChristoph Hellwig 	ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
15731da177e4SLinus Torvalds 	ASSERT(ip->i_itemp != NULL);
1574898621d5SChristoph Hellwig 	ASSERT(ip->i_itemp->ili_lock_flags == 0);
15751da177e4SLinus Torvalds 	ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
15761da177e4SLinus Torvalds 
1577673e8e59SChristoph Hellwig 	trace_xfs_itruncate_extents_start(ip, new_size);
1578673e8e59SChristoph Hellwig 
15791da177e4SLinus Torvalds 	/*
15801da177e4SLinus Torvalds 	 * Since it is possible for space to become allocated beyond
15811da177e4SLinus Torvalds 	 * the end of the file (in a crash where the space is allocated
15821da177e4SLinus Torvalds 	 * but the inode size is not yet updated), simply remove any
15831da177e4SLinus Torvalds 	 * blocks which show up between the new EOF and the maximum
15841da177e4SLinus Torvalds 	 * possible file size.  If the first block to be removed is
15851da177e4SLinus Torvalds 	 * beyond the maximum file size (ie it is the same as last_block),
15861da177e4SLinus Torvalds 	 * then there is nothing to do.
15871da177e4SLinus Torvalds 	 */
15888f04c47aSChristoph Hellwig 	first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
158932972383SDave Chinner 	last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
15908f04c47aSChristoph Hellwig 	if (first_unmap_block == last_block)
15918f04c47aSChristoph Hellwig 		return 0;
15928f04c47aSChristoph Hellwig 
15938f04c47aSChristoph Hellwig 	ASSERT(first_unmap_block < last_block);
15941da177e4SLinus Torvalds 	unmap_len = last_block - first_unmap_block + 1;
15951da177e4SLinus Torvalds 	while (!done) {
15969d87c319SEric Sandeen 		xfs_bmap_init(&free_list, &first_block);
15978f04c47aSChristoph Hellwig 		error = xfs_bunmapi(tp, ip,
15983e57ecf6SOlaf Weber 				    first_unmap_block, unmap_len,
15998f04c47aSChristoph Hellwig 				    xfs_bmapi_aflag(whichfork),
16001da177e4SLinus Torvalds 				    XFS_ITRUNC_MAX_EXTENTS,
16013e57ecf6SOlaf Weber 				    &first_block, &free_list,
1602b4e9181eSChristoph Hellwig 				    &done);
16038f04c47aSChristoph Hellwig 		if (error)
16048f04c47aSChristoph Hellwig 			goto out_bmap_cancel;
16051da177e4SLinus Torvalds 
16061da177e4SLinus Torvalds 		/*
16071da177e4SLinus Torvalds 		 * Duplicate the transaction that has the permanent
16081da177e4SLinus Torvalds 		 * reservation and commit the old transaction.
16091da177e4SLinus Torvalds 		 */
1610f6106efaSEric Sandeen 		error = xfs_bmap_finish(&tp, &free_list, ip);
16118f04c47aSChristoph Hellwig 		if (error)
16128f04c47aSChristoph Hellwig 			goto out_bmap_cancel;
16131da177e4SLinus Torvalds 
16142e6db6c4SChristoph Hellwig 		error = xfs_trans_roll(&tp, ip);
16151da177e4SLinus Torvalds 		if (error)
16168f04c47aSChristoph Hellwig 			goto out;
16171da177e4SLinus Torvalds 	}
16188f04c47aSChristoph Hellwig 
1619673e8e59SChristoph Hellwig 	/*
1620673e8e59SChristoph Hellwig 	 * Always re-log the inode so that our permanent transaction can keep
1621673e8e59SChristoph Hellwig 	 * on rolling it forward in the log.
1622673e8e59SChristoph Hellwig 	 */
1623673e8e59SChristoph Hellwig 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1624673e8e59SChristoph Hellwig 
1625673e8e59SChristoph Hellwig 	trace_xfs_itruncate_extents_end(ip, new_size);
1626673e8e59SChristoph Hellwig 
16278f04c47aSChristoph Hellwig out:
16288f04c47aSChristoph Hellwig 	*tpp = tp;
16298f04c47aSChristoph Hellwig 	return error;
16308f04c47aSChristoph Hellwig out_bmap_cancel:
16311da177e4SLinus Torvalds 	/*
16328f04c47aSChristoph Hellwig 	 * If the bunmapi call encounters an error, return to the caller where
16338f04c47aSChristoph Hellwig 	 * the transaction can be properly aborted.  We just need to make sure
16348f04c47aSChristoph Hellwig 	 * we're not holding any resources that we were not when we came in.
16351da177e4SLinus Torvalds 	 */
16368f04c47aSChristoph Hellwig 	xfs_bmap_cancel(&free_list);
16378f04c47aSChristoph Hellwig 	goto out;
16388f04c47aSChristoph Hellwig }
16398f04c47aSChristoph Hellwig 
1640c24b5dfaSDave Chinner int
1641c24b5dfaSDave Chinner xfs_release(
1642c24b5dfaSDave Chinner 	xfs_inode_t	*ip)
1643c24b5dfaSDave Chinner {
1644c24b5dfaSDave Chinner 	xfs_mount_t	*mp = ip->i_mount;
1645c24b5dfaSDave Chinner 	int		error;
1646c24b5dfaSDave Chinner 
1647c24b5dfaSDave Chinner 	if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
1648c24b5dfaSDave Chinner 		return 0;
1649c24b5dfaSDave Chinner 
1650c24b5dfaSDave Chinner 	/* If this is a read-only mount, don't do this (would generate I/O) */
1651c24b5dfaSDave Chinner 	if (mp->m_flags & XFS_MOUNT_RDONLY)
1652c24b5dfaSDave Chinner 		return 0;
1653c24b5dfaSDave Chinner 
1654c24b5dfaSDave Chinner 	if (!XFS_FORCED_SHUTDOWN(mp)) {
1655c24b5dfaSDave Chinner 		int truncated;
1656c24b5dfaSDave Chinner 
1657c24b5dfaSDave Chinner 		/*
1658c24b5dfaSDave Chinner 		 * If we previously truncated this file and removed old data
1659c24b5dfaSDave Chinner 		 * in the process, we want to initiate "early" writeout on
1660c24b5dfaSDave Chinner 		 * the last close.  This is an attempt to combat the notorious
1661c24b5dfaSDave Chinner 		 * NULL files problem which is particularly noticeable from a
1662c24b5dfaSDave Chinner 		 * truncate down, buffered (re-)write (delalloc), followed by
1663c24b5dfaSDave Chinner 		 * a crash.  What we are effectively doing here is
1664c24b5dfaSDave Chinner 		 * significantly reducing the time window where we'd otherwise
1665c24b5dfaSDave Chinner 		 * be exposed to that problem.
1666c24b5dfaSDave Chinner 		 */
1667c24b5dfaSDave Chinner 		truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
1668c24b5dfaSDave Chinner 		if (truncated) {
1669c24b5dfaSDave Chinner 			xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
1670eac152b4SDave Chinner 			if (ip->i_delayed_blks > 0) {
16712451337dSDave Chinner 				error = filemap_flush(VFS_I(ip)->i_mapping);
1672c24b5dfaSDave Chinner 				if (error)
1673c24b5dfaSDave Chinner 					return error;
1674c24b5dfaSDave Chinner 			}
1675c24b5dfaSDave Chinner 		}
1676c24b5dfaSDave Chinner 	}
1677c24b5dfaSDave Chinner 
1678c24b5dfaSDave Chinner 	if (ip->i_d.di_nlink == 0)
1679c24b5dfaSDave Chinner 		return 0;
1680c24b5dfaSDave Chinner 
1681c24b5dfaSDave Chinner 	if (xfs_can_free_eofblocks(ip, false)) {
1682c24b5dfaSDave Chinner 
1683c24b5dfaSDave Chinner 		/*
1684c24b5dfaSDave Chinner 		 * If we can't get the iolock just skip truncating the blocks
1685c24b5dfaSDave Chinner 		 * past EOF because we could deadlock with the mmap_sem
1686c24b5dfaSDave Chinner 		 * otherwise.  We'll get another chance to drop them once the
1687c24b5dfaSDave Chinner 		 * last reference to the inode is dropped, so we'll never leak
1688c24b5dfaSDave Chinner 		 * blocks permanently.
1689c24b5dfaSDave Chinner 		 *
1690c24b5dfaSDave Chinner 		 * Further, check if the inode is being opened, written and
1691c24b5dfaSDave Chinner 		 * closed frequently and we have delayed allocation blocks
1692c24b5dfaSDave Chinner 		 * outstanding (e.g. streaming writes from the NFS server),
1693c24b5dfaSDave Chinner 		 * truncating the blocks past EOF will cause fragmentation to
1694c24b5dfaSDave Chinner 		 * occur.
1695c24b5dfaSDave Chinner 		 *
1696c24b5dfaSDave Chinner 		 * In this case don't do the truncation, either, but we have to
1697c24b5dfaSDave Chinner 		 * be careful how we detect this case. Blocks beyond EOF show
1698c24b5dfaSDave Chinner 		 * up as i_delayed_blks even when the inode is clean, so we
1699c24b5dfaSDave Chinner 		 * need to truncate them away first before checking for a dirty
1700c24b5dfaSDave Chinner 		 * release. Hence on the first dirty close we will still remove
1701c24b5dfaSDave Chinner 		 * the speculative allocation, but after that we will leave it
1702c24b5dfaSDave Chinner 		 * in place.
1703c24b5dfaSDave Chinner 		 */
1704c24b5dfaSDave Chinner 		if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
1705c24b5dfaSDave Chinner 			return 0;
1706c24b5dfaSDave Chinner 
1707c24b5dfaSDave Chinner 		error = xfs_free_eofblocks(mp, ip, true);
17082451337dSDave Chinner 		if (error && error != -EAGAIN)
1709c24b5dfaSDave Chinner 			return error;
1710c24b5dfaSDave Chinner 
1711c24b5dfaSDave Chinner 		/* delalloc blocks after truncation means it really is dirty */
1712c24b5dfaSDave Chinner 		if (ip->i_delayed_blks)
1713c24b5dfaSDave Chinner 			xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
1714c24b5dfaSDave Chinner 	}
1715c24b5dfaSDave Chinner 	return 0;
1716c24b5dfaSDave Chinner }
1717c24b5dfaSDave Chinner 
1718c24b5dfaSDave Chinner /*
1719f7be2d7fSBrian Foster  * xfs_inactive_truncate
1720f7be2d7fSBrian Foster  *
1721f7be2d7fSBrian Foster  * Called to perform a truncate when an inode becomes unlinked.
1722f7be2d7fSBrian Foster  */
1723f7be2d7fSBrian Foster STATIC int
1724f7be2d7fSBrian Foster xfs_inactive_truncate(
1725f7be2d7fSBrian Foster 	struct xfs_inode *ip)
1726f7be2d7fSBrian Foster {
1727f7be2d7fSBrian Foster 	struct xfs_mount	*mp = ip->i_mount;
1728f7be2d7fSBrian Foster 	struct xfs_trans	*tp;
1729f7be2d7fSBrian Foster 	int			error;
1730f7be2d7fSBrian Foster 
1731f7be2d7fSBrian Foster 	tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1732f7be2d7fSBrian Foster 	error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
1733f7be2d7fSBrian Foster 	if (error) {
1734f7be2d7fSBrian Foster 		ASSERT(XFS_FORCED_SHUTDOWN(mp));
17354906e215SChristoph Hellwig 		xfs_trans_cancel(tp);
1736f7be2d7fSBrian Foster 		return error;
1737f7be2d7fSBrian Foster 	}
1738f7be2d7fSBrian Foster 
1739f7be2d7fSBrian Foster 	xfs_ilock(ip, XFS_ILOCK_EXCL);
1740f7be2d7fSBrian Foster 	xfs_trans_ijoin(tp, ip, 0);
1741f7be2d7fSBrian Foster 
1742f7be2d7fSBrian Foster 	/*
1743f7be2d7fSBrian Foster 	 * Log the inode size first to prevent stale data exposure in the event
1744f7be2d7fSBrian Foster 	 * of a system crash before the truncate completes. See the related
1745f7be2d7fSBrian Foster 	 * comment in xfs_setattr_size() for details.
1746f7be2d7fSBrian Foster 	 */
1747f7be2d7fSBrian Foster 	ip->i_d.di_size = 0;
1748f7be2d7fSBrian Foster 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1749f7be2d7fSBrian Foster 
1750f7be2d7fSBrian Foster 	error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
1751f7be2d7fSBrian Foster 	if (error)
1752f7be2d7fSBrian Foster 		goto error_trans_cancel;
1753f7be2d7fSBrian Foster 
1754f7be2d7fSBrian Foster 	ASSERT(ip->i_d.di_nextents == 0);
1755f7be2d7fSBrian Foster 
175670393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
1757f7be2d7fSBrian Foster 	if (error)
1758f7be2d7fSBrian Foster 		goto error_unlock;
1759f7be2d7fSBrian Foster 
1760f7be2d7fSBrian Foster 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
1761f7be2d7fSBrian Foster 	return 0;
1762f7be2d7fSBrian Foster 
1763f7be2d7fSBrian Foster error_trans_cancel:
17644906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
1765f7be2d7fSBrian Foster error_unlock:
1766f7be2d7fSBrian Foster 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
1767f7be2d7fSBrian Foster 	return error;
1768f7be2d7fSBrian Foster }
1769f7be2d7fSBrian Foster 
1770f7be2d7fSBrian Foster /*
177188877d2bSBrian Foster  * xfs_inactive_ifree()
177288877d2bSBrian Foster  *
177388877d2bSBrian Foster  * Perform the inode free when an inode is unlinked.
177488877d2bSBrian Foster  */
177588877d2bSBrian Foster STATIC int
177688877d2bSBrian Foster xfs_inactive_ifree(
177788877d2bSBrian Foster 	struct xfs_inode *ip)
177888877d2bSBrian Foster {
177988877d2bSBrian Foster 	xfs_bmap_free_t		free_list;
178088877d2bSBrian Foster 	xfs_fsblock_t		first_block;
178188877d2bSBrian Foster 	struct xfs_mount	*mp = ip->i_mount;
178288877d2bSBrian Foster 	struct xfs_trans	*tp;
178388877d2bSBrian Foster 	int			error;
178488877d2bSBrian Foster 
178588877d2bSBrian Foster 	tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
17869d43b180SBrian Foster 
17879d43b180SBrian Foster 	/*
17889d43b180SBrian Foster 	 * The ifree transaction might need to allocate blocks for record
17899d43b180SBrian Foster 	 * insertion to the finobt. We don't want to fail here at ENOSPC, so
17909d43b180SBrian Foster 	 * allow ifree to dip into the reserved block pool if necessary.
17919d43b180SBrian Foster 	 *
17929d43b180SBrian Foster 	 * Freeing large sets of inodes generally means freeing inode chunks,
17939d43b180SBrian Foster 	 * directory and file data blocks, so this should be relatively safe.
17949d43b180SBrian Foster 	 * Only under severe circumstances should it be possible to free enough
17959d43b180SBrian Foster 	 * inodes to exhaust the reserve block pool via finobt expansion while
17969d43b180SBrian Foster 	 * at the same time not creating free space in the filesystem.
17979d43b180SBrian Foster 	 *
17989d43b180SBrian Foster 	 * Send a warning if the reservation does happen to fail, as the inode
17999d43b180SBrian Foster 	 * now remains allocated and sits on the unlinked list until the fs is
18009d43b180SBrian Foster 	 * repaired.
18019d43b180SBrian Foster 	 */
18029d43b180SBrian Foster 	tp->t_flags |= XFS_TRANS_RESERVE;
18039d43b180SBrian Foster 	error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ifree,
18049d43b180SBrian Foster 				  XFS_IFREE_SPACE_RES(mp), 0);
180588877d2bSBrian Foster 	if (error) {
18062451337dSDave Chinner 		if (error == -ENOSPC) {
18079d43b180SBrian Foster 			xfs_warn_ratelimited(mp,
18089d43b180SBrian Foster 			"Failed to remove inode(s) from unlinked list. "
18099d43b180SBrian Foster 			"Please free space, unmount and run xfs_repair.");
18109d43b180SBrian Foster 		} else {
181188877d2bSBrian Foster 			ASSERT(XFS_FORCED_SHUTDOWN(mp));
18129d43b180SBrian Foster 		}
18134906e215SChristoph Hellwig 		xfs_trans_cancel(tp);
181488877d2bSBrian Foster 		return error;
181588877d2bSBrian Foster 	}
181688877d2bSBrian Foster 
181788877d2bSBrian Foster 	xfs_ilock(ip, XFS_ILOCK_EXCL);
181888877d2bSBrian Foster 	xfs_trans_ijoin(tp, ip, 0);
181988877d2bSBrian Foster 
182088877d2bSBrian Foster 	xfs_bmap_init(&free_list, &first_block);
182188877d2bSBrian Foster 	error = xfs_ifree(tp, ip, &free_list);
182288877d2bSBrian Foster 	if (error) {
182388877d2bSBrian Foster 		/*
182488877d2bSBrian Foster 		 * If we fail to free the inode, shut down.  The cancel
182588877d2bSBrian Foster 		 * might do that, we need to make sure.  Otherwise the
182688877d2bSBrian Foster 		 * inode might be lost for a long time or forever.
182788877d2bSBrian Foster 		 */
182888877d2bSBrian Foster 		if (!XFS_FORCED_SHUTDOWN(mp)) {
182988877d2bSBrian Foster 			xfs_notice(mp, "%s: xfs_ifree returned error %d",
183088877d2bSBrian Foster 				__func__, error);
183188877d2bSBrian Foster 			xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
183288877d2bSBrian Foster 		}
18334906e215SChristoph Hellwig 		xfs_trans_cancel(tp);
183488877d2bSBrian Foster 		xfs_iunlock(ip, XFS_ILOCK_EXCL);
183588877d2bSBrian Foster 		return error;
183688877d2bSBrian Foster 	}
183788877d2bSBrian Foster 
183888877d2bSBrian Foster 	/*
183988877d2bSBrian Foster 	 * Credit the quota account(s). The inode is gone.
184088877d2bSBrian Foster 	 */
184188877d2bSBrian Foster 	xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
184288877d2bSBrian Foster 
184388877d2bSBrian Foster 	/*
1844d4a97a04SBrian Foster 	 * Just ignore errors at this point.  There is nothing we can do except
1845d4a97a04SBrian Foster 	 * to try to keep going. Make sure it's not a silent error.
184688877d2bSBrian Foster 	 */
1847f6106efaSEric Sandeen 	error = xfs_bmap_finish(&tp, &free_list, NULL);
1848d4a97a04SBrian Foster 	if (error) {
184988877d2bSBrian Foster 		xfs_notice(mp, "%s: xfs_bmap_finish returned error %d",
185088877d2bSBrian Foster 			__func__, error);
1851d4a97a04SBrian Foster 		xfs_bmap_cancel(&free_list);
1852d4a97a04SBrian Foster 	}
185370393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
185488877d2bSBrian Foster 	if (error)
185588877d2bSBrian Foster 		xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
185688877d2bSBrian Foster 			__func__, error);
185788877d2bSBrian Foster 
185888877d2bSBrian Foster 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
185988877d2bSBrian Foster 	return 0;
186088877d2bSBrian Foster }
186188877d2bSBrian Foster 
186288877d2bSBrian Foster /*
1863c24b5dfaSDave Chinner  * xfs_inactive
1864c24b5dfaSDave Chinner  *
1865c24b5dfaSDave Chinner  * This is called when the vnode reference count for the vnode
1866c24b5dfaSDave Chinner  * goes to zero.  If the file has been unlinked, then it must
1867c24b5dfaSDave Chinner  * now be truncated.  Also, we clear all of the read-ahead state
1868c24b5dfaSDave Chinner  * kept for the inode here since the file is now closed.
1869c24b5dfaSDave Chinner  */
187074564fb4SBrian Foster void
1871c24b5dfaSDave Chinner xfs_inactive(
1872c24b5dfaSDave Chinner 	xfs_inode_t	*ip)
1873c24b5dfaSDave Chinner {
18743d3c8b52SJie Liu 	struct xfs_mount	*mp;
1875c24b5dfaSDave Chinner 	int			error;
1876c24b5dfaSDave Chinner 	int			truncate = 0;
1877c24b5dfaSDave Chinner 
1878c24b5dfaSDave Chinner 	/*
1879c24b5dfaSDave Chinner 	 * If the inode is already free, then there can be nothing
1880c24b5dfaSDave Chinner 	 * to clean up here.
1881c24b5dfaSDave Chinner 	 */
1882d948709bSBen Myers 	if (ip->i_d.di_mode == 0) {
1883c24b5dfaSDave Chinner 		ASSERT(ip->i_df.if_real_bytes == 0);
1884c24b5dfaSDave Chinner 		ASSERT(ip->i_df.if_broot_bytes == 0);
188574564fb4SBrian Foster 		return;
1886c24b5dfaSDave Chinner 	}
1887c24b5dfaSDave Chinner 
1888c24b5dfaSDave Chinner 	mp = ip->i_mount;
1889c24b5dfaSDave Chinner 
1890c24b5dfaSDave Chinner 	/* If this is a read-only mount, don't do this (would generate I/O) */
1891c24b5dfaSDave Chinner 	if (mp->m_flags & XFS_MOUNT_RDONLY)
189274564fb4SBrian Foster 		return;
1893c24b5dfaSDave Chinner 
1894c24b5dfaSDave Chinner 	if (ip->i_d.di_nlink != 0) {
1895c24b5dfaSDave Chinner 		/*
1896c24b5dfaSDave Chinner 		 * force is true because we are evicting an inode from the
1897c24b5dfaSDave Chinner 		 * cache. Post-eof blocks must be freed, lest we end up with
1898c24b5dfaSDave Chinner 		 * broken free space accounting.
1899c24b5dfaSDave Chinner 		 */
190074564fb4SBrian Foster 		if (xfs_can_free_eofblocks(ip, true))
190174564fb4SBrian Foster 			xfs_free_eofblocks(mp, ip, false);
190274564fb4SBrian Foster 
190374564fb4SBrian Foster 		return;
1904c24b5dfaSDave Chinner 	}
1905c24b5dfaSDave Chinner 
1906c24b5dfaSDave Chinner 	if (S_ISREG(ip->i_d.di_mode) &&
1907c24b5dfaSDave Chinner 	    (ip->i_d.di_size != 0 || XFS_ISIZE(ip) != 0 ||
1908c24b5dfaSDave Chinner 	     ip->i_d.di_nextents > 0 || ip->i_delayed_blks > 0))
1909c24b5dfaSDave Chinner 		truncate = 1;
1910c24b5dfaSDave Chinner 
1911c24b5dfaSDave Chinner 	error = xfs_qm_dqattach(ip, 0);
1912c24b5dfaSDave Chinner 	if (error)
191374564fb4SBrian Foster 		return;
1914c24b5dfaSDave Chinner 
1915f7be2d7fSBrian Foster 	if (S_ISLNK(ip->i_d.di_mode))
191636b21ddeSBrian Foster 		error = xfs_inactive_symlink(ip);
1917f7be2d7fSBrian Foster 	else if (truncate)
1918f7be2d7fSBrian Foster 		error = xfs_inactive_truncate(ip);
191936b21ddeSBrian Foster 	if (error)
192074564fb4SBrian Foster 		return;
1921c24b5dfaSDave Chinner 
1922c24b5dfaSDave Chinner 	/*
1923c24b5dfaSDave Chinner 	 * If there are attributes associated with the file then blow them away
1924c24b5dfaSDave Chinner 	 * now.  The code calls a routine that recursively deconstructs the
19256dfe5a04SDave Chinner 	 * attribute fork. If also blows away the in-core attribute fork.
1926c24b5dfaSDave Chinner 	 */
19276dfe5a04SDave Chinner 	if (XFS_IFORK_Q(ip)) {
1928c24b5dfaSDave Chinner 		error = xfs_attr_inactive(ip);
1929c24b5dfaSDave Chinner 		if (error)
193074564fb4SBrian Foster 			return;
1931c24b5dfaSDave Chinner 	}
1932c24b5dfaSDave Chinner 
19336dfe5a04SDave Chinner 	ASSERT(!ip->i_afp);
1934c24b5dfaSDave Chinner 	ASSERT(ip->i_d.di_anextents == 0);
19356dfe5a04SDave Chinner 	ASSERT(ip->i_d.di_forkoff == 0);
1936c24b5dfaSDave Chinner 
1937c24b5dfaSDave Chinner 	/*
1938c24b5dfaSDave Chinner 	 * Free the inode.
1939c24b5dfaSDave Chinner 	 */
194088877d2bSBrian Foster 	error = xfs_inactive_ifree(ip);
1941c24b5dfaSDave Chinner 	if (error)
194274564fb4SBrian Foster 		return;
1943c24b5dfaSDave Chinner 
1944c24b5dfaSDave Chinner 	/*
1945c24b5dfaSDave Chinner 	 * Release the dquots held by inode, if any.
1946c24b5dfaSDave Chinner 	 */
1947c24b5dfaSDave Chinner 	xfs_qm_dqdetach(ip);
1948c24b5dfaSDave Chinner }
1949c24b5dfaSDave Chinner 
19501da177e4SLinus Torvalds /*
19511da177e4SLinus Torvalds  * This is called when the inode's link count goes to 0.
19521da177e4SLinus Torvalds  * We place the on-disk inode on a list in the AGI.  It
19531da177e4SLinus Torvalds  * will be pulled from this list when the inode is freed.
19541da177e4SLinus Torvalds  */
19551da177e4SLinus Torvalds int
19561da177e4SLinus Torvalds xfs_iunlink(
19571da177e4SLinus Torvalds 	xfs_trans_t	*tp,
19581da177e4SLinus Torvalds 	xfs_inode_t	*ip)
19591da177e4SLinus Torvalds {
19601da177e4SLinus Torvalds 	xfs_mount_t	*mp;
19611da177e4SLinus Torvalds 	xfs_agi_t	*agi;
19621da177e4SLinus Torvalds 	xfs_dinode_t	*dip;
19631da177e4SLinus Torvalds 	xfs_buf_t	*agibp;
19641da177e4SLinus Torvalds 	xfs_buf_t	*ibp;
19651da177e4SLinus Torvalds 	xfs_agino_t	agino;
19661da177e4SLinus Torvalds 	short		bucket_index;
19671da177e4SLinus Torvalds 	int		offset;
19681da177e4SLinus Torvalds 	int		error;
19691da177e4SLinus Torvalds 
19701da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_nlink == 0);
19711da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_mode != 0);
19721da177e4SLinus Torvalds 
19731da177e4SLinus Torvalds 	mp = tp->t_mountp;
19741da177e4SLinus Torvalds 
19751da177e4SLinus Torvalds 	/*
19761da177e4SLinus Torvalds 	 * Get the agi buffer first.  It ensures lock ordering
19771da177e4SLinus Torvalds 	 * on the list.
19781da177e4SLinus Torvalds 	 */
19795e1be0fbSChristoph Hellwig 	error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
1980859d7182SVlad Apostolov 	if (error)
19811da177e4SLinus Torvalds 		return error;
19821da177e4SLinus Torvalds 	agi = XFS_BUF_TO_AGI(agibp);
19835e1be0fbSChristoph Hellwig 
19841da177e4SLinus Torvalds 	/*
19851da177e4SLinus Torvalds 	 * Get the index into the agi hash table for the
19861da177e4SLinus Torvalds 	 * list this inode will go on.
19871da177e4SLinus Torvalds 	 */
19881da177e4SLinus Torvalds 	agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
19891da177e4SLinus Torvalds 	ASSERT(agino != 0);
19901da177e4SLinus Torvalds 	bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
19911da177e4SLinus Torvalds 	ASSERT(agi->agi_unlinked[bucket_index]);
199216259e7dSChristoph Hellwig 	ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
19931da177e4SLinus Torvalds 
199469ef921bSChristoph Hellwig 	if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
19951da177e4SLinus Torvalds 		/*
19961da177e4SLinus Torvalds 		 * There is already another inode in the bucket we need
19971da177e4SLinus Torvalds 		 * to add ourselves to.  Add us at the front of the list.
19981da177e4SLinus Torvalds 		 * Here we put the head pointer into our next pointer,
19991da177e4SLinus Torvalds 		 * and then we fall through to point the head at us.
20001da177e4SLinus Torvalds 		 */
2001475ee413SChristoph Hellwig 		error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
2002475ee413SChristoph Hellwig 				       0, 0);
2003c319b58bSVlad Apostolov 		if (error)
2004c319b58bSVlad Apostolov 			return error;
2005c319b58bSVlad Apostolov 
200669ef921bSChristoph Hellwig 		ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
20071da177e4SLinus Torvalds 		dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
200892bfc6e7SChristoph Hellwig 		offset = ip->i_imap.im_boffset +
20091da177e4SLinus Torvalds 			offsetof(xfs_dinode_t, di_next_unlinked);
20100a32c26eSDave Chinner 
20110a32c26eSDave Chinner 		/* need to recalc the inode CRC if appropriate */
20120a32c26eSDave Chinner 		xfs_dinode_calc_crc(mp, dip);
20130a32c26eSDave Chinner 
20141da177e4SLinus Torvalds 		xfs_trans_inode_buf(tp, ibp);
20151da177e4SLinus Torvalds 		xfs_trans_log_buf(tp, ibp, offset,
20161da177e4SLinus Torvalds 				  (offset + sizeof(xfs_agino_t) - 1));
20171da177e4SLinus Torvalds 		xfs_inobp_check(mp, ibp);
20181da177e4SLinus Torvalds 	}
20191da177e4SLinus Torvalds 
20201da177e4SLinus Torvalds 	/*
20211da177e4SLinus Torvalds 	 * Point the bucket head pointer at the inode being inserted.
20221da177e4SLinus Torvalds 	 */
20231da177e4SLinus Torvalds 	ASSERT(agino != 0);
202416259e7dSChristoph Hellwig 	agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
20251da177e4SLinus Torvalds 	offset = offsetof(xfs_agi_t, agi_unlinked) +
20261da177e4SLinus Torvalds 		(sizeof(xfs_agino_t) * bucket_index);
2027f19b872bSDave Chinner 	xfs_trans_buf_set_type(tp, agibp, XFS_BLFT_AGI_BUF);
20281da177e4SLinus Torvalds 	xfs_trans_log_buf(tp, agibp, offset,
20291da177e4SLinus Torvalds 			  (offset + sizeof(xfs_agino_t) - 1));
20301da177e4SLinus Torvalds 	return 0;
20311da177e4SLinus Torvalds }
20321da177e4SLinus Torvalds 
20331da177e4SLinus Torvalds /*
20341da177e4SLinus Torvalds  * Pull the on-disk inode from the AGI unlinked list.
20351da177e4SLinus Torvalds  */
20361da177e4SLinus Torvalds STATIC int
20371da177e4SLinus Torvalds xfs_iunlink_remove(
20381da177e4SLinus Torvalds 	xfs_trans_t	*tp,
20391da177e4SLinus Torvalds 	xfs_inode_t	*ip)
20401da177e4SLinus Torvalds {
20411da177e4SLinus Torvalds 	xfs_ino_t	next_ino;
20421da177e4SLinus Torvalds 	xfs_mount_t	*mp;
20431da177e4SLinus Torvalds 	xfs_agi_t	*agi;
20441da177e4SLinus Torvalds 	xfs_dinode_t	*dip;
20451da177e4SLinus Torvalds 	xfs_buf_t	*agibp;
20461da177e4SLinus Torvalds 	xfs_buf_t	*ibp;
20471da177e4SLinus Torvalds 	xfs_agnumber_t	agno;
20481da177e4SLinus Torvalds 	xfs_agino_t	agino;
20491da177e4SLinus Torvalds 	xfs_agino_t	next_agino;
20501da177e4SLinus Torvalds 	xfs_buf_t	*last_ibp;
20516fdf8cccSNathan Scott 	xfs_dinode_t	*last_dip = NULL;
20521da177e4SLinus Torvalds 	short		bucket_index;
20536fdf8cccSNathan Scott 	int		offset, last_offset = 0;
20541da177e4SLinus Torvalds 	int		error;
20551da177e4SLinus Torvalds 
20561da177e4SLinus Torvalds 	mp = tp->t_mountp;
20571da177e4SLinus Torvalds 	agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
20581da177e4SLinus Torvalds 
20591da177e4SLinus Torvalds 	/*
20601da177e4SLinus Torvalds 	 * Get the agi buffer first.  It ensures lock ordering
20611da177e4SLinus Torvalds 	 * on the list.
20621da177e4SLinus Torvalds 	 */
20635e1be0fbSChristoph Hellwig 	error = xfs_read_agi(mp, tp, agno, &agibp);
20645e1be0fbSChristoph Hellwig 	if (error)
20651da177e4SLinus Torvalds 		return error;
20665e1be0fbSChristoph Hellwig 
20671da177e4SLinus Torvalds 	agi = XFS_BUF_TO_AGI(agibp);
20685e1be0fbSChristoph Hellwig 
20691da177e4SLinus Torvalds 	/*
20701da177e4SLinus Torvalds 	 * Get the index into the agi hash table for the
20711da177e4SLinus Torvalds 	 * list this inode will go on.
20721da177e4SLinus Torvalds 	 */
20731da177e4SLinus Torvalds 	agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
20741da177e4SLinus Torvalds 	ASSERT(agino != 0);
20751da177e4SLinus Torvalds 	bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
207669ef921bSChristoph Hellwig 	ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO));
20771da177e4SLinus Torvalds 	ASSERT(agi->agi_unlinked[bucket_index]);
20781da177e4SLinus Torvalds 
207916259e7dSChristoph Hellwig 	if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
20801da177e4SLinus Torvalds 		/*
2081475ee413SChristoph Hellwig 		 * We're at the head of the list.  Get the inode's on-disk
2082475ee413SChristoph Hellwig 		 * buffer to see if there is anyone after us on the list.
2083475ee413SChristoph Hellwig 		 * Only modify our next pointer if it is not already NULLAGINO.
2084475ee413SChristoph Hellwig 		 * This saves us the overhead of dealing with the buffer when
2085475ee413SChristoph Hellwig 		 * there is no need to change it.
20861da177e4SLinus Torvalds 		 */
2087475ee413SChristoph Hellwig 		error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
2088475ee413SChristoph Hellwig 				       0, 0);
20891da177e4SLinus Torvalds 		if (error) {
2090475ee413SChristoph Hellwig 			xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
20910b932cccSDave Chinner 				__func__, error);
20921da177e4SLinus Torvalds 			return error;
20931da177e4SLinus Torvalds 		}
2094347d1c01SChristoph Hellwig 		next_agino = be32_to_cpu(dip->di_next_unlinked);
20951da177e4SLinus Torvalds 		ASSERT(next_agino != 0);
20961da177e4SLinus Torvalds 		if (next_agino != NULLAGINO) {
2097347d1c01SChristoph Hellwig 			dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
209892bfc6e7SChristoph Hellwig 			offset = ip->i_imap.im_boffset +
20991da177e4SLinus Torvalds 				offsetof(xfs_dinode_t, di_next_unlinked);
21000a32c26eSDave Chinner 
21010a32c26eSDave Chinner 			/* need to recalc the inode CRC if appropriate */
21020a32c26eSDave Chinner 			xfs_dinode_calc_crc(mp, dip);
21030a32c26eSDave Chinner 
21041da177e4SLinus Torvalds 			xfs_trans_inode_buf(tp, ibp);
21051da177e4SLinus Torvalds 			xfs_trans_log_buf(tp, ibp, offset,
21061da177e4SLinus Torvalds 					  (offset + sizeof(xfs_agino_t) - 1));
21071da177e4SLinus Torvalds 			xfs_inobp_check(mp, ibp);
21081da177e4SLinus Torvalds 		} else {
21091da177e4SLinus Torvalds 			xfs_trans_brelse(tp, ibp);
21101da177e4SLinus Torvalds 		}
21111da177e4SLinus Torvalds 		/*
21121da177e4SLinus Torvalds 		 * Point the bucket head pointer at the next inode.
21131da177e4SLinus Torvalds 		 */
21141da177e4SLinus Torvalds 		ASSERT(next_agino != 0);
21151da177e4SLinus Torvalds 		ASSERT(next_agino != agino);
211616259e7dSChristoph Hellwig 		agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
21171da177e4SLinus Torvalds 		offset = offsetof(xfs_agi_t, agi_unlinked) +
21181da177e4SLinus Torvalds 			(sizeof(xfs_agino_t) * bucket_index);
2119f19b872bSDave Chinner 		xfs_trans_buf_set_type(tp, agibp, XFS_BLFT_AGI_BUF);
21201da177e4SLinus Torvalds 		xfs_trans_log_buf(tp, agibp, offset,
21211da177e4SLinus Torvalds 				  (offset + sizeof(xfs_agino_t) - 1));
21221da177e4SLinus Torvalds 	} else {
21231da177e4SLinus Torvalds 		/*
21241da177e4SLinus Torvalds 		 * We need to search the list for the inode being freed.
21251da177e4SLinus Torvalds 		 */
212616259e7dSChristoph Hellwig 		next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
21271da177e4SLinus Torvalds 		last_ibp = NULL;
21281da177e4SLinus Torvalds 		while (next_agino != agino) {
2129129dbc9aSChristoph Hellwig 			struct xfs_imap	imap;
2130129dbc9aSChristoph Hellwig 
2131129dbc9aSChristoph Hellwig 			if (last_ibp)
21321da177e4SLinus Torvalds 				xfs_trans_brelse(tp, last_ibp);
2133129dbc9aSChristoph Hellwig 
2134129dbc9aSChristoph Hellwig 			imap.im_blkno = 0;
21351da177e4SLinus Torvalds 			next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
2136129dbc9aSChristoph Hellwig 
2137129dbc9aSChristoph Hellwig 			error = xfs_imap(mp, tp, next_ino, &imap, 0);
21381da177e4SLinus Torvalds 			if (error) {
21390b932cccSDave Chinner 				xfs_warn(mp,
2140129dbc9aSChristoph Hellwig 	"%s: xfs_imap returned error %d.",
21410b932cccSDave Chinner 					 __func__, error);
21421da177e4SLinus Torvalds 				return error;
21431da177e4SLinus Torvalds 			}
2144129dbc9aSChristoph Hellwig 
2145129dbc9aSChristoph Hellwig 			error = xfs_imap_to_bp(mp, tp, &imap, &last_dip,
2146129dbc9aSChristoph Hellwig 					       &last_ibp, 0, 0);
2147129dbc9aSChristoph Hellwig 			if (error) {
2148129dbc9aSChristoph Hellwig 				xfs_warn(mp,
2149129dbc9aSChristoph Hellwig 	"%s: xfs_imap_to_bp returned error %d.",
2150129dbc9aSChristoph Hellwig 					__func__, error);
2151129dbc9aSChristoph Hellwig 				return error;
2152129dbc9aSChristoph Hellwig 			}
2153129dbc9aSChristoph Hellwig 
2154129dbc9aSChristoph Hellwig 			last_offset = imap.im_boffset;
2155347d1c01SChristoph Hellwig 			next_agino = be32_to_cpu(last_dip->di_next_unlinked);
21561da177e4SLinus Torvalds 			ASSERT(next_agino != NULLAGINO);
21571da177e4SLinus Torvalds 			ASSERT(next_agino != 0);
21581da177e4SLinus Torvalds 		}
2159475ee413SChristoph Hellwig 
21601da177e4SLinus Torvalds 		/*
2161475ee413SChristoph Hellwig 		 * Now last_ibp points to the buffer previous to us on the
2162475ee413SChristoph Hellwig 		 * unlinked list.  Pull us from the list.
21631da177e4SLinus Torvalds 		 */
2164475ee413SChristoph Hellwig 		error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
2165475ee413SChristoph Hellwig 				       0, 0);
21661da177e4SLinus Torvalds 		if (error) {
2167475ee413SChristoph Hellwig 			xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.",
21680b932cccSDave Chinner 				__func__, error);
21691da177e4SLinus Torvalds 			return error;
21701da177e4SLinus Torvalds 		}
2171347d1c01SChristoph Hellwig 		next_agino = be32_to_cpu(dip->di_next_unlinked);
21721da177e4SLinus Torvalds 		ASSERT(next_agino != 0);
21731da177e4SLinus Torvalds 		ASSERT(next_agino != agino);
21741da177e4SLinus Torvalds 		if (next_agino != NULLAGINO) {
2175347d1c01SChristoph Hellwig 			dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
217692bfc6e7SChristoph Hellwig 			offset = ip->i_imap.im_boffset +
21771da177e4SLinus Torvalds 				offsetof(xfs_dinode_t, di_next_unlinked);
21780a32c26eSDave Chinner 
21790a32c26eSDave Chinner 			/* need to recalc the inode CRC if appropriate */
21800a32c26eSDave Chinner 			xfs_dinode_calc_crc(mp, dip);
21810a32c26eSDave Chinner 
21821da177e4SLinus Torvalds 			xfs_trans_inode_buf(tp, ibp);
21831da177e4SLinus Torvalds 			xfs_trans_log_buf(tp, ibp, offset,
21841da177e4SLinus Torvalds 					  (offset + sizeof(xfs_agino_t) - 1));
21851da177e4SLinus Torvalds 			xfs_inobp_check(mp, ibp);
21861da177e4SLinus Torvalds 		} else {
21871da177e4SLinus Torvalds 			xfs_trans_brelse(tp, ibp);
21881da177e4SLinus Torvalds 		}
21891da177e4SLinus Torvalds 		/*
21901da177e4SLinus Torvalds 		 * Point the previous inode on the list to the next inode.
21911da177e4SLinus Torvalds 		 */
2192347d1c01SChristoph Hellwig 		last_dip->di_next_unlinked = cpu_to_be32(next_agino);
21931da177e4SLinus Torvalds 		ASSERT(next_agino != 0);
21941da177e4SLinus Torvalds 		offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
21950a32c26eSDave Chinner 
21960a32c26eSDave Chinner 		/* need to recalc the inode CRC if appropriate */
21970a32c26eSDave Chinner 		xfs_dinode_calc_crc(mp, last_dip);
21980a32c26eSDave Chinner 
21991da177e4SLinus Torvalds 		xfs_trans_inode_buf(tp, last_ibp);
22001da177e4SLinus Torvalds 		xfs_trans_log_buf(tp, last_ibp, offset,
22011da177e4SLinus Torvalds 				  (offset + sizeof(xfs_agino_t) - 1));
22021da177e4SLinus Torvalds 		xfs_inobp_check(mp, last_ibp);
22031da177e4SLinus Torvalds 	}
22041da177e4SLinus Torvalds 	return 0;
22051da177e4SLinus Torvalds }
22061da177e4SLinus Torvalds 
22075b3eed75SDave Chinner /*
22080b8182dbSZhi Yong Wu  * A big issue when freeing the inode cluster is that we _cannot_ skip any
22095b3eed75SDave Chinner  * inodes that are in memory - they all must be marked stale and attached to
22105b3eed75SDave Chinner  * the cluster buffer.
22115b3eed75SDave Chinner  */
22122a30f36dSChandra Seetharaman STATIC int
22131da177e4SLinus Torvalds xfs_ifree_cluster(
22141da177e4SLinus Torvalds 	xfs_inode_t		*free_ip,
22151da177e4SLinus Torvalds 	xfs_trans_t		*tp,
221609b56604SBrian Foster 	struct xfs_icluster	*xic)
22171da177e4SLinus Torvalds {
22181da177e4SLinus Torvalds 	xfs_mount_t		*mp = free_ip->i_mount;
22191da177e4SLinus Torvalds 	int			blks_per_cluster;
2220982e939eSJie Liu 	int			inodes_per_cluster;
22211da177e4SLinus Torvalds 	int			nbufs;
22225b257b4aSDave Chinner 	int			i, j;
22233cdaa189SBrian Foster 	int			ioffset;
22241da177e4SLinus Torvalds 	xfs_daddr_t		blkno;
22251da177e4SLinus Torvalds 	xfs_buf_t		*bp;
22265b257b4aSDave Chinner 	xfs_inode_t		*ip;
22271da177e4SLinus Torvalds 	xfs_inode_log_item_t	*iip;
22281da177e4SLinus Torvalds 	xfs_log_item_t		*lip;
22295017e97dSDave Chinner 	struct xfs_perag	*pag;
223009b56604SBrian Foster 	xfs_ino_t		inum;
22311da177e4SLinus Torvalds 
223209b56604SBrian Foster 	inum = xic->first_ino;
22335017e97dSDave Chinner 	pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
2234982e939eSJie Liu 	blks_per_cluster = xfs_icluster_size_fsb(mp);
2235982e939eSJie Liu 	inodes_per_cluster = blks_per_cluster << mp->m_sb.sb_inopblog;
2236126cd105SJie Liu 	nbufs = mp->m_ialloc_blks / blks_per_cluster;
22371da177e4SLinus Torvalds 
2238982e939eSJie Liu 	for (j = 0; j < nbufs; j++, inum += inodes_per_cluster) {
223909b56604SBrian Foster 		/*
224009b56604SBrian Foster 		 * The allocation bitmap tells us which inodes of the chunk were
224109b56604SBrian Foster 		 * physically allocated. Skip the cluster if an inode falls into
224209b56604SBrian Foster 		 * a sparse region.
224309b56604SBrian Foster 		 */
22443cdaa189SBrian Foster 		ioffset = inum - xic->first_ino;
22453cdaa189SBrian Foster 		if ((xic->alloc & XFS_INOBT_MASK(ioffset)) == 0) {
22463cdaa189SBrian Foster 			ASSERT(do_mod(ioffset, inodes_per_cluster) == 0);
224709b56604SBrian Foster 			continue;
224809b56604SBrian Foster 		}
224909b56604SBrian Foster 
22501da177e4SLinus Torvalds 		blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
22511da177e4SLinus Torvalds 					 XFS_INO_TO_AGBNO(mp, inum));
22521da177e4SLinus Torvalds 
22531da177e4SLinus Torvalds 		/*
22545b257b4aSDave Chinner 		 * We obtain and lock the backing buffer first in the process
22555b257b4aSDave Chinner 		 * here, as we have to ensure that any dirty inode that we
22565b257b4aSDave Chinner 		 * can't get the flush lock on is attached to the buffer.
22575b257b4aSDave Chinner 		 * If we scan the in-memory inodes first, then buffer IO can
22585b257b4aSDave Chinner 		 * complete before we get a lock on it, and hence we may fail
22595b257b4aSDave Chinner 		 * to mark all the active inodes on the buffer stale.
22601da177e4SLinus Torvalds 		 */
22611da177e4SLinus Torvalds 		bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
2262b6aff29fSDave Chinner 					mp->m_bsize * blks_per_cluster,
2263b6aff29fSDave Chinner 					XBF_UNMAPPED);
22641da177e4SLinus Torvalds 
22652a30f36dSChandra Seetharaman 		if (!bp)
22662451337dSDave Chinner 			return -ENOMEM;
2267b0f539deSDave Chinner 
2268b0f539deSDave Chinner 		/*
2269b0f539deSDave Chinner 		 * This buffer may not have been correctly initialised as we
2270b0f539deSDave Chinner 		 * didn't read it from disk. That's not important because we are
2271b0f539deSDave Chinner 		 * only using to mark the buffer as stale in the log, and to
2272b0f539deSDave Chinner 		 * attach stale cached inodes on it. That means it will never be
2273b0f539deSDave Chinner 		 * dispatched for IO. If it is, we want to know about it, and we
2274b0f539deSDave Chinner 		 * want it to fail. We can acheive this by adding a write
2275b0f539deSDave Chinner 		 * verifier to the buffer.
2276b0f539deSDave Chinner 		 */
22771813dd64SDave Chinner 		 bp->b_ops = &xfs_inode_buf_ops;
2278b0f539deSDave Chinner 
22795b257b4aSDave Chinner 		/*
22805b257b4aSDave Chinner 		 * Walk the inodes already attached to the buffer and mark them
22815b257b4aSDave Chinner 		 * stale. These will all have the flush locks held, so an
22825b3eed75SDave Chinner 		 * in-memory inode walk can't lock them. By marking them all
22835b3eed75SDave Chinner 		 * stale first, we will not attempt to lock them in the loop
22845b3eed75SDave Chinner 		 * below as the XFS_ISTALE flag will be set.
22855b257b4aSDave Chinner 		 */
2286adadbeefSChristoph Hellwig 		lip = bp->b_fspriv;
22871da177e4SLinus Torvalds 		while (lip) {
22881da177e4SLinus Torvalds 			if (lip->li_type == XFS_LI_INODE) {
22891da177e4SLinus Torvalds 				iip = (xfs_inode_log_item_t *)lip;
22901da177e4SLinus Torvalds 				ASSERT(iip->ili_logged == 1);
2291ca30b2a7SChristoph Hellwig 				lip->li_cb = xfs_istale_done;
22927b2e2a31SDavid Chinner 				xfs_trans_ail_copy_lsn(mp->m_ail,
22937b2e2a31SDavid Chinner 							&iip->ili_flush_lsn,
22947b2e2a31SDavid Chinner 							&iip->ili_item.li_lsn);
2295e5ffd2bbSDavid Chinner 				xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
22961da177e4SLinus Torvalds 			}
22971da177e4SLinus Torvalds 			lip = lip->li_bio_list;
22981da177e4SLinus Torvalds 		}
22991da177e4SLinus Torvalds 
23005b3eed75SDave Chinner 
23015b257b4aSDave Chinner 		/*
23025b257b4aSDave Chinner 		 * For each inode in memory attempt to add it to the inode
23035b257b4aSDave Chinner 		 * buffer and set it up for being staled on buffer IO
23045b257b4aSDave Chinner 		 * completion.  This is safe as we've locked out tail pushing
23055b257b4aSDave Chinner 		 * and flushing by locking the buffer.
23065b257b4aSDave Chinner 		 *
23075b257b4aSDave Chinner 		 * We have already marked every inode that was part of a
23085b257b4aSDave Chinner 		 * transaction stale above, which means there is no point in
23095b257b4aSDave Chinner 		 * even trying to lock them.
23105b257b4aSDave Chinner 		 */
2311982e939eSJie Liu 		for (i = 0; i < inodes_per_cluster; i++) {
23125b3eed75SDave Chinner retry:
23131a3e8f3dSDave Chinner 			rcu_read_lock();
23145b257b4aSDave Chinner 			ip = radix_tree_lookup(&pag->pag_ici_root,
23155b257b4aSDave Chinner 					XFS_INO_TO_AGINO(mp, (inum + i)));
23161da177e4SLinus Torvalds 
23171a3e8f3dSDave Chinner 			/* Inode not in memory, nothing to do */
23181a3e8f3dSDave Chinner 			if (!ip) {
23191a3e8f3dSDave Chinner 				rcu_read_unlock();
23205b257b4aSDave Chinner 				continue;
23215b257b4aSDave Chinner 			}
23225b257b4aSDave Chinner 
23235b3eed75SDave Chinner 			/*
23241a3e8f3dSDave Chinner 			 * because this is an RCU protected lookup, we could
23251a3e8f3dSDave Chinner 			 * find a recently freed or even reallocated inode
23261a3e8f3dSDave Chinner 			 * during the lookup. We need to check under the
23271a3e8f3dSDave Chinner 			 * i_flags_lock for a valid inode here. Skip it if it
23281a3e8f3dSDave Chinner 			 * is not valid, the wrong inode or stale.
23291a3e8f3dSDave Chinner 			 */
23301a3e8f3dSDave Chinner 			spin_lock(&ip->i_flags_lock);
23311a3e8f3dSDave Chinner 			if (ip->i_ino != inum + i ||
23321a3e8f3dSDave Chinner 			    __xfs_iflags_test(ip, XFS_ISTALE)) {
23331a3e8f3dSDave Chinner 				spin_unlock(&ip->i_flags_lock);
23341a3e8f3dSDave Chinner 				rcu_read_unlock();
23351a3e8f3dSDave Chinner 				continue;
23361a3e8f3dSDave Chinner 			}
23371a3e8f3dSDave Chinner 			spin_unlock(&ip->i_flags_lock);
23381a3e8f3dSDave Chinner 
23391a3e8f3dSDave Chinner 			/*
23405b3eed75SDave Chinner 			 * Don't try to lock/unlock the current inode, but we
23415b3eed75SDave Chinner 			 * _cannot_ skip the other inodes that we did not find
23425b3eed75SDave Chinner 			 * in the list attached to the buffer and are not
23435b3eed75SDave Chinner 			 * already marked stale. If we can't lock it, back off
23445b3eed75SDave Chinner 			 * and retry.
23455b3eed75SDave Chinner 			 */
23465b257b4aSDave Chinner 			if (ip != free_ip &&
23475b257b4aSDave Chinner 			    !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
23481a3e8f3dSDave Chinner 				rcu_read_unlock();
23495b3eed75SDave Chinner 				delay(1);
23505b3eed75SDave Chinner 				goto retry;
23515b257b4aSDave Chinner 			}
23521a3e8f3dSDave Chinner 			rcu_read_unlock();
23535b257b4aSDave Chinner 
23545b3eed75SDave Chinner 			xfs_iflock(ip);
23555b257b4aSDave Chinner 			xfs_iflags_set(ip, XFS_ISTALE);
23565b257b4aSDave Chinner 
23575b3eed75SDave Chinner 			/*
23585b3eed75SDave Chinner 			 * we don't need to attach clean inodes or those only
23595b3eed75SDave Chinner 			 * with unlogged changes (which we throw away, anyway).
23605b3eed75SDave Chinner 			 */
23615b257b4aSDave Chinner 			iip = ip->i_itemp;
23625b3eed75SDave Chinner 			if (!iip || xfs_inode_clean(ip)) {
23635b257b4aSDave Chinner 				ASSERT(ip != free_ip);
23641da177e4SLinus Torvalds 				xfs_ifunlock(ip);
23651da177e4SLinus Torvalds 				xfs_iunlock(ip, XFS_ILOCK_EXCL);
23661da177e4SLinus Torvalds 				continue;
23671da177e4SLinus Torvalds 			}
23681da177e4SLinus Torvalds 
2369f5d8d5c4SChristoph Hellwig 			iip->ili_last_fields = iip->ili_fields;
2370f5d8d5c4SChristoph Hellwig 			iip->ili_fields = 0;
2371fc0561ceSDave Chinner 			iip->ili_fsync_fields = 0;
23721da177e4SLinus Torvalds 			iip->ili_logged = 1;
23737b2e2a31SDavid Chinner 			xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
23747b2e2a31SDavid Chinner 						&iip->ili_item.li_lsn);
23751da177e4SLinus Torvalds 
2376ca30b2a7SChristoph Hellwig 			xfs_buf_attach_iodone(bp, xfs_istale_done,
2377ca30b2a7SChristoph Hellwig 						  &iip->ili_item);
23785b257b4aSDave Chinner 
23795b257b4aSDave Chinner 			if (ip != free_ip)
23801da177e4SLinus Torvalds 				xfs_iunlock(ip, XFS_ILOCK_EXCL);
23811da177e4SLinus Torvalds 		}
23821da177e4SLinus Torvalds 
23831da177e4SLinus Torvalds 		xfs_trans_stale_inode_buf(tp, bp);
23841da177e4SLinus Torvalds 		xfs_trans_binval(tp, bp);
23851da177e4SLinus Torvalds 	}
23861da177e4SLinus Torvalds 
23875017e97dSDave Chinner 	xfs_perag_put(pag);
23882a30f36dSChandra Seetharaman 	return 0;
23891da177e4SLinus Torvalds }
23901da177e4SLinus Torvalds 
23911da177e4SLinus Torvalds /*
23921da177e4SLinus Torvalds  * This is called to return an inode to the inode free list.
23931da177e4SLinus Torvalds  * The inode should already be truncated to 0 length and have
23941da177e4SLinus Torvalds  * no pages associated with it.  This routine also assumes that
23951da177e4SLinus Torvalds  * the inode is already a part of the transaction.
23961da177e4SLinus Torvalds  *
23971da177e4SLinus Torvalds  * The on-disk copy of the inode will have been added to the list
23981da177e4SLinus Torvalds  * of unlinked inodes in the AGI. We need to remove the inode from
23991da177e4SLinus Torvalds  * that list atomically with respect to freeing it here.
24001da177e4SLinus Torvalds  */
24011da177e4SLinus Torvalds int
24021da177e4SLinus Torvalds xfs_ifree(
24031da177e4SLinus Torvalds 	xfs_trans_t	*tp,
24041da177e4SLinus Torvalds 	xfs_inode_t	*ip,
24051da177e4SLinus Torvalds 	xfs_bmap_free_t	*flist)
24061da177e4SLinus Torvalds {
24071da177e4SLinus Torvalds 	int			error;
240809b56604SBrian Foster 	struct xfs_icluster	xic = { 0 };
24091da177e4SLinus Torvalds 
2410579aa9caSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
24111da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_nlink == 0);
24121da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_nextents == 0);
24131da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_anextents == 0);
2414ce7ae151SChristoph Hellwig 	ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode));
24151da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_nblocks == 0);
24161da177e4SLinus Torvalds 
24171da177e4SLinus Torvalds 	/*
24181da177e4SLinus Torvalds 	 * Pull the on-disk inode from the AGI unlinked list.
24191da177e4SLinus Torvalds 	 */
24201da177e4SLinus Torvalds 	error = xfs_iunlink_remove(tp, ip);
24211baaed8fSDave Chinner 	if (error)
24221da177e4SLinus Torvalds 		return error;
24231da177e4SLinus Torvalds 
242409b56604SBrian Foster 	error = xfs_difree(tp, ip->i_ino, flist, &xic);
24251baaed8fSDave Chinner 	if (error)
24261da177e4SLinus Torvalds 		return error;
24271baaed8fSDave Chinner 
24281da177e4SLinus Torvalds 	ip->i_d.di_mode = 0;		/* mark incore inode as free */
24291da177e4SLinus Torvalds 	ip->i_d.di_flags = 0;
24301da177e4SLinus Torvalds 	ip->i_d.di_dmevmask = 0;
24311da177e4SLinus Torvalds 	ip->i_d.di_forkoff = 0;		/* mark the attr fork not in use */
24321da177e4SLinus Torvalds 	ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
24331da177e4SLinus Torvalds 	ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
24341da177e4SLinus Torvalds 	/*
24351da177e4SLinus Torvalds 	 * Bump the generation count so no one will be confused
24361da177e4SLinus Torvalds 	 * by reincarnations of this inode.
24371da177e4SLinus Torvalds 	 */
24381da177e4SLinus Torvalds 	ip->i_d.di_gen++;
24391da177e4SLinus Torvalds 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
24401da177e4SLinus Torvalds 
244109b56604SBrian Foster 	if (xic.deleted)
244209b56604SBrian Foster 		error = xfs_ifree_cluster(ip, tp, &xic);
24431da177e4SLinus Torvalds 
24442a30f36dSChandra Seetharaman 	return error;
24451da177e4SLinus Torvalds }
24461da177e4SLinus Torvalds 
24471da177e4SLinus Torvalds /*
244860ec6783SChristoph Hellwig  * This is called to unpin an inode.  The caller must have the inode locked
244960ec6783SChristoph Hellwig  * in at least shared mode so that the buffer cannot be subsequently pinned
245060ec6783SChristoph Hellwig  * once someone is waiting for it to be unpinned.
24511da177e4SLinus Torvalds  */
245260ec6783SChristoph Hellwig static void
2453f392e631SChristoph Hellwig xfs_iunpin(
245460ec6783SChristoph Hellwig 	struct xfs_inode	*ip)
2455a3f74ffbSDavid Chinner {
2456579aa9caSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
2457a3f74ffbSDavid Chinner 
24584aaf15d1SDave Chinner 	trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
24594aaf15d1SDave Chinner 
2460a3f74ffbSDavid Chinner 	/* Give the log a push to start the unpinning I/O */
246160ec6783SChristoph Hellwig 	xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0);
2462a14a348bSChristoph Hellwig 
2463a3f74ffbSDavid Chinner }
2464a3f74ffbSDavid Chinner 
2465f392e631SChristoph Hellwig static void
2466f392e631SChristoph Hellwig __xfs_iunpin_wait(
2467f392e631SChristoph Hellwig 	struct xfs_inode	*ip)
2468f392e631SChristoph Hellwig {
2469f392e631SChristoph Hellwig 	wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
2470f392e631SChristoph Hellwig 	DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
2471f392e631SChristoph Hellwig 
2472f392e631SChristoph Hellwig 	xfs_iunpin(ip);
2473f392e631SChristoph Hellwig 
2474f392e631SChristoph Hellwig 	do {
2475f392e631SChristoph Hellwig 		prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2476f392e631SChristoph Hellwig 		if (xfs_ipincount(ip))
2477f392e631SChristoph Hellwig 			io_schedule();
2478f392e631SChristoph Hellwig 	} while (xfs_ipincount(ip));
2479f392e631SChristoph Hellwig 	finish_wait(wq, &wait.wait);
2480f392e631SChristoph Hellwig }
2481f392e631SChristoph Hellwig 
2482777df5afSDave Chinner void
24831da177e4SLinus Torvalds xfs_iunpin_wait(
248460ec6783SChristoph Hellwig 	struct xfs_inode	*ip)
24851da177e4SLinus Torvalds {
2486f392e631SChristoph Hellwig 	if (xfs_ipincount(ip))
2487f392e631SChristoph Hellwig 		__xfs_iunpin_wait(ip);
24881da177e4SLinus Torvalds }
24891da177e4SLinus Torvalds 
249027320369SDave Chinner /*
249127320369SDave Chinner  * Removing an inode from the namespace involves removing the directory entry
249227320369SDave Chinner  * and dropping the link count on the inode. Removing the directory entry can
249327320369SDave Chinner  * result in locking an AGF (directory blocks were freed) and removing a link
249427320369SDave Chinner  * count can result in placing the inode on an unlinked list which results in
249527320369SDave Chinner  * locking an AGI.
249627320369SDave Chinner  *
249727320369SDave Chinner  * The big problem here is that we have an ordering constraint on AGF and AGI
249827320369SDave Chinner  * locking - inode allocation locks the AGI, then can allocate a new extent for
249927320369SDave Chinner  * new inodes, locking the AGF after the AGI. Similarly, freeing the inode
250027320369SDave Chinner  * removes the inode from the unlinked list, requiring that we lock the AGI
250127320369SDave Chinner  * first, and then freeing the inode can result in an inode chunk being freed
250227320369SDave Chinner  * and hence freeing disk space requiring that we lock an AGF.
250327320369SDave Chinner  *
250427320369SDave Chinner  * Hence the ordering that is imposed by other parts of the code is AGI before
250527320369SDave Chinner  * AGF. This means we cannot remove the directory entry before we drop the inode
250627320369SDave Chinner  * reference count and put it on the unlinked list as this results in a lock
250727320369SDave Chinner  * order of AGF then AGI, and this can deadlock against inode allocation and
250827320369SDave Chinner  * freeing. Therefore we must drop the link counts before we remove the
250927320369SDave Chinner  * directory entry.
251027320369SDave Chinner  *
251127320369SDave Chinner  * This is still safe from a transactional point of view - it is not until we
251227320369SDave Chinner  * get to xfs_bmap_finish() that we have the possibility of multiple
251327320369SDave Chinner  * transactions in this operation. Hence as long as we remove the directory
251427320369SDave Chinner  * entry and drop the link count in the first transaction of the remove
251527320369SDave Chinner  * operation, there are no transactional constraints on the ordering here.
251627320369SDave Chinner  */
2517c24b5dfaSDave Chinner int
2518c24b5dfaSDave Chinner xfs_remove(
2519c24b5dfaSDave Chinner 	xfs_inode_t             *dp,
2520c24b5dfaSDave Chinner 	struct xfs_name		*name,
2521c24b5dfaSDave Chinner 	xfs_inode_t		*ip)
2522c24b5dfaSDave Chinner {
2523c24b5dfaSDave Chinner 	xfs_mount_t		*mp = dp->i_mount;
2524c24b5dfaSDave Chinner 	xfs_trans_t             *tp = NULL;
2525c24b5dfaSDave Chinner 	int			is_dir = S_ISDIR(ip->i_d.di_mode);
2526c24b5dfaSDave Chinner 	int                     error = 0;
2527c24b5dfaSDave Chinner 	xfs_bmap_free_t         free_list;
2528c24b5dfaSDave Chinner 	xfs_fsblock_t           first_block;
2529c24b5dfaSDave Chinner 	uint			resblks;
2530c24b5dfaSDave Chinner 
2531c24b5dfaSDave Chinner 	trace_xfs_remove(dp, name);
2532c24b5dfaSDave Chinner 
2533c24b5dfaSDave Chinner 	if (XFS_FORCED_SHUTDOWN(mp))
25342451337dSDave Chinner 		return -EIO;
2535c24b5dfaSDave Chinner 
2536c24b5dfaSDave Chinner 	error = xfs_qm_dqattach(dp, 0);
2537c24b5dfaSDave Chinner 	if (error)
2538c24b5dfaSDave Chinner 		goto std_return;
2539c24b5dfaSDave Chinner 
2540c24b5dfaSDave Chinner 	error = xfs_qm_dqattach(ip, 0);
2541c24b5dfaSDave Chinner 	if (error)
2542c24b5dfaSDave Chinner 		goto std_return;
2543c24b5dfaSDave Chinner 
254432296f86SDave Chinner 	if (is_dir)
2545c24b5dfaSDave Chinner 		tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
254632296f86SDave Chinner 	else
2547c24b5dfaSDave Chinner 		tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
2548c24b5dfaSDave Chinner 
2549c24b5dfaSDave Chinner 	/*
2550c24b5dfaSDave Chinner 	 * We try to get the real space reservation first,
2551c24b5dfaSDave Chinner 	 * allowing for directory btree deletion(s) implying
2552c24b5dfaSDave Chinner 	 * possible bmap insert(s).  If we can't get the space
2553c24b5dfaSDave Chinner 	 * reservation then we use 0 instead, and avoid the bmap
2554c24b5dfaSDave Chinner 	 * btree insert(s) in the directory code by, if the bmap
2555c24b5dfaSDave Chinner 	 * insert tries to happen, instead trimming the LAST
2556c24b5dfaSDave Chinner 	 * block from the directory.
2557c24b5dfaSDave Chinner 	 */
2558c24b5dfaSDave Chinner 	resblks = XFS_REMOVE_SPACE_RES(mp);
25593d3c8b52SJie Liu 	error = xfs_trans_reserve(tp, &M_RES(mp)->tr_remove, resblks, 0);
25602451337dSDave Chinner 	if (error == -ENOSPC) {
2561c24b5dfaSDave Chinner 		resblks = 0;
25623d3c8b52SJie Liu 		error = xfs_trans_reserve(tp, &M_RES(mp)->tr_remove, 0, 0);
2563c24b5dfaSDave Chinner 	}
2564c24b5dfaSDave Chinner 	if (error) {
25652451337dSDave Chinner 		ASSERT(error != -ENOSPC);
2566c24b5dfaSDave Chinner 		goto out_trans_cancel;
2567c24b5dfaSDave Chinner 	}
2568c24b5dfaSDave Chinner 
2569dbad7c99SDave Chinner 	xfs_ilock(dp, XFS_IOLOCK_EXCL | XFS_IOLOCK_PARENT);
2570c24b5dfaSDave Chinner 	xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
2571c24b5dfaSDave Chinner 
2572dbad7c99SDave Chinner 	xfs_trans_ijoin(tp, dp, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
2573c24b5dfaSDave Chinner 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2574c24b5dfaSDave Chinner 
2575c24b5dfaSDave Chinner 	/*
2576c24b5dfaSDave Chinner 	 * If we're removing a directory perform some additional validation.
2577c24b5dfaSDave Chinner 	 */
2578c24b5dfaSDave Chinner 	if (is_dir) {
2579c24b5dfaSDave Chinner 		ASSERT(ip->i_d.di_nlink >= 2);
2580c24b5dfaSDave Chinner 		if (ip->i_d.di_nlink != 2) {
25812451337dSDave Chinner 			error = -ENOTEMPTY;
2582c24b5dfaSDave Chinner 			goto out_trans_cancel;
2583c24b5dfaSDave Chinner 		}
2584c24b5dfaSDave Chinner 		if (!xfs_dir_isempty(ip)) {
25852451337dSDave Chinner 			error = -ENOTEMPTY;
2586c24b5dfaSDave Chinner 			goto out_trans_cancel;
2587c24b5dfaSDave Chinner 		}
2588c24b5dfaSDave Chinner 
258927320369SDave Chinner 		/* Drop the link from ip's "..".  */
2590c24b5dfaSDave Chinner 		error = xfs_droplink(tp, dp);
2591c24b5dfaSDave Chinner 		if (error)
259227320369SDave Chinner 			goto out_trans_cancel;
2593c24b5dfaSDave Chinner 
259427320369SDave Chinner 		/* Drop the "." link from ip to self.  */
2595c24b5dfaSDave Chinner 		error = xfs_droplink(tp, ip);
2596c24b5dfaSDave Chinner 		if (error)
259727320369SDave Chinner 			goto out_trans_cancel;
2598c24b5dfaSDave Chinner 	} else {
2599c24b5dfaSDave Chinner 		/*
2600c24b5dfaSDave Chinner 		 * When removing a non-directory we need to log the parent
2601c24b5dfaSDave Chinner 		 * inode here.  For a directory this is done implicitly
2602c24b5dfaSDave Chinner 		 * by the xfs_droplink call for the ".." entry.
2603c24b5dfaSDave Chinner 		 */
2604c24b5dfaSDave Chinner 		xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2605c24b5dfaSDave Chinner 	}
260627320369SDave Chinner 	xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2607c24b5dfaSDave Chinner 
260827320369SDave Chinner 	/* Drop the link from dp to ip. */
2609c24b5dfaSDave Chinner 	error = xfs_droplink(tp, ip);
2610c24b5dfaSDave Chinner 	if (error)
261127320369SDave Chinner 		goto out_trans_cancel;
2612c24b5dfaSDave Chinner 
261327320369SDave Chinner 	xfs_bmap_init(&free_list, &first_block);
261427320369SDave Chinner 	error = xfs_dir_removename(tp, dp, name, ip->i_ino,
261527320369SDave Chinner 					&first_block, &free_list, resblks);
261627320369SDave Chinner 	if (error) {
26172451337dSDave Chinner 		ASSERT(error != -ENOENT);
261827320369SDave Chinner 		goto out_bmap_cancel;
261927320369SDave Chinner 	}
262027320369SDave Chinner 
2621c24b5dfaSDave Chinner 	/*
2622c24b5dfaSDave Chinner 	 * If this is a synchronous mount, make sure that the
2623c24b5dfaSDave Chinner 	 * remove transaction goes to disk before returning to
2624c24b5dfaSDave Chinner 	 * the user.
2625c24b5dfaSDave Chinner 	 */
2626c24b5dfaSDave Chinner 	if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
2627c24b5dfaSDave Chinner 		xfs_trans_set_sync(tp);
2628c24b5dfaSDave Chinner 
2629f6106efaSEric Sandeen 	error = xfs_bmap_finish(&tp, &free_list, NULL);
2630c24b5dfaSDave Chinner 	if (error)
2631c24b5dfaSDave Chinner 		goto out_bmap_cancel;
2632c24b5dfaSDave Chinner 
263370393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
2634c24b5dfaSDave Chinner 	if (error)
2635c24b5dfaSDave Chinner 		goto std_return;
2636c24b5dfaSDave Chinner 
26372cd2ef6aSChristoph Hellwig 	if (is_dir && xfs_inode_is_filestream(ip))
2638c24b5dfaSDave Chinner 		xfs_filestream_deassociate(ip);
2639c24b5dfaSDave Chinner 
2640c24b5dfaSDave Chinner 	return 0;
2641c24b5dfaSDave Chinner 
2642c24b5dfaSDave Chinner  out_bmap_cancel:
2643c24b5dfaSDave Chinner 	xfs_bmap_cancel(&free_list);
2644c24b5dfaSDave Chinner  out_trans_cancel:
26454906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
2646c24b5dfaSDave Chinner  std_return:
2647c24b5dfaSDave Chinner 	return error;
2648c24b5dfaSDave Chinner }
2649c24b5dfaSDave Chinner 
2650f6bba201SDave Chinner /*
2651f6bba201SDave Chinner  * Enter all inodes for a rename transaction into a sorted array.
2652f6bba201SDave Chinner  */
265395afcf5cSDave Chinner #define __XFS_SORT_INODES	5
2654f6bba201SDave Chinner STATIC void
2655f6bba201SDave Chinner xfs_sort_for_rename(
265695afcf5cSDave Chinner 	struct xfs_inode	*dp1,	/* in: old (source) directory inode */
265795afcf5cSDave Chinner 	struct xfs_inode	*dp2,	/* in: new (target) directory inode */
265895afcf5cSDave Chinner 	struct xfs_inode	*ip1,	/* in: inode of old entry */
265995afcf5cSDave Chinner 	struct xfs_inode	*ip2,	/* in: inode of new entry */
266095afcf5cSDave Chinner 	struct xfs_inode	*wip,	/* in: whiteout inode */
266195afcf5cSDave Chinner 	struct xfs_inode	**i_tab,/* out: sorted array of inodes */
266295afcf5cSDave Chinner 	int			*num_inodes)  /* in/out: inodes in array */
2663f6bba201SDave Chinner {
2664f6bba201SDave Chinner 	int			i, j;
2665f6bba201SDave Chinner 
266695afcf5cSDave Chinner 	ASSERT(*num_inodes == __XFS_SORT_INODES);
266795afcf5cSDave Chinner 	memset(i_tab, 0, *num_inodes * sizeof(struct xfs_inode *));
266895afcf5cSDave Chinner 
2669f6bba201SDave Chinner 	/*
2670f6bba201SDave Chinner 	 * i_tab contains a list of pointers to inodes.  We initialize
2671f6bba201SDave Chinner 	 * the table here & we'll sort it.  We will then use it to
2672f6bba201SDave Chinner 	 * order the acquisition of the inode locks.
2673f6bba201SDave Chinner 	 *
2674f6bba201SDave Chinner 	 * Note that the table may contain duplicates.  e.g., dp1 == dp2.
2675f6bba201SDave Chinner 	 */
267695afcf5cSDave Chinner 	i = 0;
267795afcf5cSDave Chinner 	i_tab[i++] = dp1;
267895afcf5cSDave Chinner 	i_tab[i++] = dp2;
267995afcf5cSDave Chinner 	i_tab[i++] = ip1;
268095afcf5cSDave Chinner 	if (ip2)
268195afcf5cSDave Chinner 		i_tab[i++] = ip2;
268295afcf5cSDave Chinner 	if (wip)
268395afcf5cSDave Chinner 		i_tab[i++] = wip;
268495afcf5cSDave Chinner 	*num_inodes = i;
2685f6bba201SDave Chinner 
2686f6bba201SDave Chinner 	/*
2687f6bba201SDave Chinner 	 * Sort the elements via bubble sort.  (Remember, there are at
268895afcf5cSDave Chinner 	 * most 5 elements to sort, so this is adequate.)
2689f6bba201SDave Chinner 	 */
2690f6bba201SDave Chinner 	for (i = 0; i < *num_inodes; i++) {
2691f6bba201SDave Chinner 		for (j = 1; j < *num_inodes; j++) {
2692f6bba201SDave Chinner 			if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) {
269395afcf5cSDave Chinner 				struct xfs_inode *temp = i_tab[j];
2694f6bba201SDave Chinner 				i_tab[j] = i_tab[j-1];
2695f6bba201SDave Chinner 				i_tab[j-1] = temp;
2696f6bba201SDave Chinner 			}
2697f6bba201SDave Chinner 		}
2698f6bba201SDave Chinner 	}
2699f6bba201SDave Chinner }
2700f6bba201SDave Chinner 
2701310606b0SDave Chinner static int
2702310606b0SDave Chinner xfs_finish_rename(
2703310606b0SDave Chinner 	struct xfs_trans	*tp,
2704310606b0SDave Chinner 	struct xfs_bmap_free	*free_list)
2705310606b0SDave Chinner {
2706310606b0SDave Chinner 	int			error;
2707310606b0SDave Chinner 
2708310606b0SDave Chinner 	/*
2709310606b0SDave Chinner 	 * If this is a synchronous mount, make sure that the rename transaction
2710310606b0SDave Chinner 	 * goes to disk before returning to the user.
2711310606b0SDave Chinner 	 */
2712310606b0SDave Chinner 	if (tp->t_mountp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
2713310606b0SDave Chinner 		xfs_trans_set_sync(tp);
2714310606b0SDave Chinner 
2715f6106efaSEric Sandeen 	error = xfs_bmap_finish(&tp, free_list, NULL);
2716310606b0SDave Chinner 	if (error) {
2717310606b0SDave Chinner 		xfs_bmap_cancel(free_list);
27184906e215SChristoph Hellwig 		xfs_trans_cancel(tp);
2719310606b0SDave Chinner 		return error;
2720310606b0SDave Chinner 	}
2721310606b0SDave Chinner 
272270393313SChristoph Hellwig 	return xfs_trans_commit(tp);
2723310606b0SDave Chinner }
2724310606b0SDave Chinner 
2725f6bba201SDave Chinner /*
2726d31a1825SCarlos Maiolino  * xfs_cross_rename()
2727d31a1825SCarlos Maiolino  *
2728d31a1825SCarlos Maiolino  * responsible for handling RENAME_EXCHANGE flag in renameat2() sytemcall
2729d31a1825SCarlos Maiolino  */
2730d31a1825SCarlos Maiolino STATIC int
2731d31a1825SCarlos Maiolino xfs_cross_rename(
2732d31a1825SCarlos Maiolino 	struct xfs_trans	*tp,
2733d31a1825SCarlos Maiolino 	struct xfs_inode	*dp1,
2734d31a1825SCarlos Maiolino 	struct xfs_name		*name1,
2735d31a1825SCarlos Maiolino 	struct xfs_inode	*ip1,
2736d31a1825SCarlos Maiolino 	struct xfs_inode	*dp2,
2737d31a1825SCarlos Maiolino 	struct xfs_name		*name2,
2738d31a1825SCarlos Maiolino 	struct xfs_inode	*ip2,
2739d31a1825SCarlos Maiolino 	struct xfs_bmap_free	*free_list,
2740d31a1825SCarlos Maiolino 	xfs_fsblock_t		*first_block,
2741d31a1825SCarlos Maiolino 	int			spaceres)
2742d31a1825SCarlos Maiolino {
2743d31a1825SCarlos Maiolino 	int		error = 0;
2744d31a1825SCarlos Maiolino 	int		ip1_flags = 0;
2745d31a1825SCarlos Maiolino 	int		ip2_flags = 0;
2746d31a1825SCarlos Maiolino 	int		dp2_flags = 0;
2747d31a1825SCarlos Maiolino 
2748d31a1825SCarlos Maiolino 	/* Swap inode number for dirent in first parent */
2749d31a1825SCarlos Maiolino 	error = xfs_dir_replace(tp, dp1, name1,
2750d31a1825SCarlos Maiolino 				ip2->i_ino,
2751d31a1825SCarlos Maiolino 				first_block, free_list, spaceres);
2752d31a1825SCarlos Maiolino 	if (error)
2753eeacd321SDave Chinner 		goto out_trans_abort;
2754d31a1825SCarlos Maiolino 
2755d31a1825SCarlos Maiolino 	/* Swap inode number for dirent in second parent */
2756d31a1825SCarlos Maiolino 	error = xfs_dir_replace(tp, dp2, name2,
2757d31a1825SCarlos Maiolino 				ip1->i_ino,
2758d31a1825SCarlos Maiolino 				first_block, free_list, spaceres);
2759d31a1825SCarlos Maiolino 	if (error)
2760eeacd321SDave Chinner 		goto out_trans_abort;
2761d31a1825SCarlos Maiolino 
2762d31a1825SCarlos Maiolino 	/*
2763d31a1825SCarlos Maiolino 	 * If we're renaming one or more directories across different parents,
2764d31a1825SCarlos Maiolino 	 * update the respective ".." entries (and link counts) to match the new
2765d31a1825SCarlos Maiolino 	 * parents.
2766d31a1825SCarlos Maiolino 	 */
2767d31a1825SCarlos Maiolino 	if (dp1 != dp2) {
2768d31a1825SCarlos Maiolino 		dp2_flags = XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
2769d31a1825SCarlos Maiolino 
2770d31a1825SCarlos Maiolino 		if (S_ISDIR(ip2->i_d.di_mode)) {
2771d31a1825SCarlos Maiolino 			error = xfs_dir_replace(tp, ip2, &xfs_name_dotdot,
2772d31a1825SCarlos Maiolino 						dp1->i_ino, first_block,
2773d31a1825SCarlos Maiolino 						free_list, spaceres);
2774d31a1825SCarlos Maiolino 			if (error)
2775eeacd321SDave Chinner 				goto out_trans_abort;
2776d31a1825SCarlos Maiolino 
2777d31a1825SCarlos Maiolino 			/* transfer ip2 ".." reference to dp1 */
2778d31a1825SCarlos Maiolino 			if (!S_ISDIR(ip1->i_d.di_mode)) {
2779d31a1825SCarlos Maiolino 				error = xfs_droplink(tp, dp2);
2780d31a1825SCarlos Maiolino 				if (error)
2781eeacd321SDave Chinner 					goto out_trans_abort;
2782d31a1825SCarlos Maiolino 				error = xfs_bumplink(tp, dp1);
2783d31a1825SCarlos Maiolino 				if (error)
2784eeacd321SDave Chinner 					goto out_trans_abort;
2785d31a1825SCarlos Maiolino 			}
2786d31a1825SCarlos Maiolino 
2787d31a1825SCarlos Maiolino 			/*
2788d31a1825SCarlos Maiolino 			 * Although ip1 isn't changed here, userspace needs
2789d31a1825SCarlos Maiolino 			 * to be warned about the change, so that applications
2790d31a1825SCarlos Maiolino 			 * relying on it (like backup ones), will properly
2791d31a1825SCarlos Maiolino 			 * notify the change
2792d31a1825SCarlos Maiolino 			 */
2793d31a1825SCarlos Maiolino 			ip1_flags |= XFS_ICHGTIME_CHG;
2794d31a1825SCarlos Maiolino 			ip2_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
2795d31a1825SCarlos Maiolino 		}
2796d31a1825SCarlos Maiolino 
2797d31a1825SCarlos Maiolino 		if (S_ISDIR(ip1->i_d.di_mode)) {
2798d31a1825SCarlos Maiolino 			error = xfs_dir_replace(tp, ip1, &xfs_name_dotdot,
2799d31a1825SCarlos Maiolino 						dp2->i_ino, first_block,
2800d31a1825SCarlos Maiolino 						free_list, spaceres);
2801d31a1825SCarlos Maiolino 			if (error)
2802eeacd321SDave Chinner 				goto out_trans_abort;
2803d31a1825SCarlos Maiolino 
2804d31a1825SCarlos Maiolino 			/* transfer ip1 ".." reference to dp2 */
2805d31a1825SCarlos Maiolino 			if (!S_ISDIR(ip2->i_d.di_mode)) {
2806d31a1825SCarlos Maiolino 				error = xfs_droplink(tp, dp1);
2807d31a1825SCarlos Maiolino 				if (error)
2808eeacd321SDave Chinner 					goto out_trans_abort;
2809d31a1825SCarlos Maiolino 				error = xfs_bumplink(tp, dp2);
2810d31a1825SCarlos Maiolino 				if (error)
2811eeacd321SDave Chinner 					goto out_trans_abort;
2812d31a1825SCarlos Maiolino 			}
2813d31a1825SCarlos Maiolino 
2814d31a1825SCarlos Maiolino 			/*
2815d31a1825SCarlos Maiolino 			 * Although ip2 isn't changed here, userspace needs
2816d31a1825SCarlos Maiolino 			 * to be warned about the change, so that applications
2817d31a1825SCarlos Maiolino 			 * relying on it (like backup ones), will properly
2818d31a1825SCarlos Maiolino 			 * notify the change
2819d31a1825SCarlos Maiolino 			 */
2820d31a1825SCarlos Maiolino 			ip1_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
2821d31a1825SCarlos Maiolino 			ip2_flags |= XFS_ICHGTIME_CHG;
2822d31a1825SCarlos Maiolino 		}
2823d31a1825SCarlos Maiolino 	}
2824d31a1825SCarlos Maiolino 
2825d31a1825SCarlos Maiolino 	if (ip1_flags) {
2826d31a1825SCarlos Maiolino 		xfs_trans_ichgtime(tp, ip1, ip1_flags);
2827d31a1825SCarlos Maiolino 		xfs_trans_log_inode(tp, ip1, XFS_ILOG_CORE);
2828d31a1825SCarlos Maiolino 	}
2829d31a1825SCarlos Maiolino 	if (ip2_flags) {
2830d31a1825SCarlos Maiolino 		xfs_trans_ichgtime(tp, ip2, ip2_flags);
2831d31a1825SCarlos Maiolino 		xfs_trans_log_inode(tp, ip2, XFS_ILOG_CORE);
2832d31a1825SCarlos Maiolino 	}
2833d31a1825SCarlos Maiolino 	if (dp2_flags) {
2834d31a1825SCarlos Maiolino 		xfs_trans_ichgtime(tp, dp2, dp2_flags);
2835d31a1825SCarlos Maiolino 		xfs_trans_log_inode(tp, dp2, XFS_ILOG_CORE);
2836d31a1825SCarlos Maiolino 	}
2837d31a1825SCarlos Maiolino 	xfs_trans_ichgtime(tp, dp1, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2838d31a1825SCarlos Maiolino 	xfs_trans_log_inode(tp, dp1, XFS_ILOG_CORE);
2839eeacd321SDave Chinner 	return xfs_finish_rename(tp, free_list);
2840eeacd321SDave Chinner 
2841eeacd321SDave Chinner out_trans_abort:
2842eeacd321SDave Chinner 	xfs_bmap_cancel(free_list);
28434906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
2844d31a1825SCarlos Maiolino 	return error;
2845d31a1825SCarlos Maiolino }
2846d31a1825SCarlos Maiolino 
2847d31a1825SCarlos Maiolino /*
28487dcf5c3eSDave Chinner  * xfs_rename_alloc_whiteout()
28497dcf5c3eSDave Chinner  *
28507dcf5c3eSDave Chinner  * Return a referenced, unlinked, unlocked inode that that can be used as a
28517dcf5c3eSDave Chinner  * whiteout in a rename transaction. We use a tmpfile inode here so that if we
28527dcf5c3eSDave Chinner  * crash between allocating the inode and linking it into the rename transaction
28537dcf5c3eSDave Chinner  * recovery will free the inode and we won't leak it.
28547dcf5c3eSDave Chinner  */
28557dcf5c3eSDave Chinner static int
28567dcf5c3eSDave Chinner xfs_rename_alloc_whiteout(
28577dcf5c3eSDave Chinner 	struct xfs_inode	*dp,
28587dcf5c3eSDave Chinner 	struct xfs_inode	**wip)
28597dcf5c3eSDave Chinner {
28607dcf5c3eSDave Chinner 	struct xfs_inode	*tmpfile;
28617dcf5c3eSDave Chinner 	int			error;
28627dcf5c3eSDave Chinner 
28637dcf5c3eSDave Chinner 	error = xfs_create_tmpfile(dp, NULL, S_IFCHR | WHITEOUT_MODE, &tmpfile);
28647dcf5c3eSDave Chinner 	if (error)
28657dcf5c3eSDave Chinner 		return error;
28667dcf5c3eSDave Chinner 
286722419ac9SBrian Foster 	/*
286822419ac9SBrian Foster 	 * Prepare the tmpfile inode as if it were created through the VFS.
286922419ac9SBrian Foster 	 * Otherwise, the link increment paths will complain about nlink 0->1.
287022419ac9SBrian Foster 	 * Drop the link count as done by d_tmpfile(), complete the inode setup
287122419ac9SBrian Foster 	 * and flag it as linkable.
287222419ac9SBrian Foster 	 */
287322419ac9SBrian Foster 	drop_nlink(VFS_I(tmpfile));
28747dcf5c3eSDave Chinner 	xfs_finish_inode_setup(tmpfile);
28757dcf5c3eSDave Chinner 	VFS_I(tmpfile)->i_state |= I_LINKABLE;
28767dcf5c3eSDave Chinner 
28777dcf5c3eSDave Chinner 	*wip = tmpfile;
28787dcf5c3eSDave Chinner 	return 0;
28797dcf5c3eSDave Chinner }
28807dcf5c3eSDave Chinner 
28817dcf5c3eSDave Chinner /*
2882f6bba201SDave Chinner  * xfs_rename
2883f6bba201SDave Chinner  */
2884f6bba201SDave Chinner int
2885f6bba201SDave Chinner xfs_rename(
28867dcf5c3eSDave Chinner 	struct xfs_inode	*src_dp,
2887f6bba201SDave Chinner 	struct xfs_name		*src_name,
28887dcf5c3eSDave Chinner 	struct xfs_inode	*src_ip,
28897dcf5c3eSDave Chinner 	struct xfs_inode	*target_dp,
2890f6bba201SDave Chinner 	struct xfs_name		*target_name,
28917dcf5c3eSDave Chinner 	struct xfs_inode	*target_ip,
2892d31a1825SCarlos Maiolino 	unsigned int		flags)
2893f6bba201SDave Chinner {
28947dcf5c3eSDave Chinner 	struct xfs_mount	*mp = src_dp->i_mount;
28957dcf5c3eSDave Chinner 	struct xfs_trans	*tp;
28967dcf5c3eSDave Chinner 	struct xfs_bmap_free	free_list;
2897f6bba201SDave Chinner 	xfs_fsblock_t		first_block;
28987dcf5c3eSDave Chinner 	struct xfs_inode	*wip = NULL;		/* whiteout inode */
28997dcf5c3eSDave Chinner 	struct xfs_inode	*inodes[__XFS_SORT_INODES];
290095afcf5cSDave Chinner 	int			num_inodes = __XFS_SORT_INODES;
29012b93681fSDave Chinner 	bool			new_parent = (src_dp != target_dp);
29022b93681fSDave Chinner 	bool			src_is_directory = S_ISDIR(src_ip->i_d.di_mode);
2903f6bba201SDave Chinner 	int			spaceres;
29047dcf5c3eSDave Chinner 	int			error;
2905f6bba201SDave Chinner 
2906f6bba201SDave Chinner 	trace_xfs_rename(src_dp, target_dp, src_name, target_name);
2907f6bba201SDave Chinner 
2908eeacd321SDave Chinner 	if ((flags & RENAME_EXCHANGE) && !target_ip)
2909eeacd321SDave Chinner 		return -EINVAL;
2910f6bba201SDave Chinner 
29117dcf5c3eSDave Chinner 	/*
29127dcf5c3eSDave Chinner 	 * If we are doing a whiteout operation, allocate the whiteout inode
29137dcf5c3eSDave Chinner 	 * we will be placing at the target and ensure the type is set
29147dcf5c3eSDave Chinner 	 * appropriately.
29157dcf5c3eSDave Chinner 	 */
29167dcf5c3eSDave Chinner 	if (flags & RENAME_WHITEOUT) {
29177dcf5c3eSDave Chinner 		ASSERT(!(flags & (RENAME_NOREPLACE | RENAME_EXCHANGE)));
29187dcf5c3eSDave Chinner 		error = xfs_rename_alloc_whiteout(target_dp, &wip);
29197dcf5c3eSDave Chinner 		if (error)
29207dcf5c3eSDave Chinner 			return error;
2921f6bba201SDave Chinner 
29227dcf5c3eSDave Chinner 		/* setup target dirent info as whiteout */
29237dcf5c3eSDave Chinner 		src_name->type = XFS_DIR3_FT_CHRDEV;
29247dcf5c3eSDave Chinner 	}
29257dcf5c3eSDave Chinner 
29267dcf5c3eSDave Chinner 	xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip, wip,
2927f6bba201SDave Chinner 				inodes, &num_inodes);
2928f6bba201SDave Chinner 
2929f6bba201SDave Chinner 	tp = xfs_trans_alloc(mp, XFS_TRANS_RENAME);
2930f6bba201SDave Chinner 	spaceres = XFS_RENAME_SPACE_RES(mp, target_name->len);
29313d3c8b52SJie Liu 	error = xfs_trans_reserve(tp, &M_RES(mp)->tr_rename, spaceres, 0);
29322451337dSDave Chinner 	if (error == -ENOSPC) {
2933f6bba201SDave Chinner 		spaceres = 0;
29343d3c8b52SJie Liu 		error = xfs_trans_reserve(tp, &M_RES(mp)->tr_rename, 0, 0);
2935f6bba201SDave Chinner 	}
2936445883e8SDave Chinner 	if (error)
2937445883e8SDave Chinner 		goto out_trans_cancel;
2938f6bba201SDave Chinner 
2939f6bba201SDave Chinner 	/*
2940f6bba201SDave Chinner 	 * Attach the dquots to the inodes
2941f6bba201SDave Chinner 	 */
2942f6bba201SDave Chinner 	error = xfs_qm_vop_rename_dqattach(inodes);
2943445883e8SDave Chinner 	if (error)
2944445883e8SDave Chinner 		goto out_trans_cancel;
2945f6bba201SDave Chinner 
2946f6bba201SDave Chinner 	/*
2947f6bba201SDave Chinner 	 * Lock all the participating inodes. Depending upon whether
2948f6bba201SDave Chinner 	 * the target_name exists in the target directory, and
2949f6bba201SDave Chinner 	 * whether the target directory is the same as the source
2950f6bba201SDave Chinner 	 * directory, we can lock from 2 to 4 inodes.
2951f6bba201SDave Chinner 	 */
2952dbad7c99SDave Chinner 	if (!new_parent)
2953dbad7c99SDave Chinner 		xfs_ilock(src_dp, XFS_IOLOCK_EXCL | XFS_IOLOCK_PARENT);
2954dbad7c99SDave Chinner 	else
2955dbad7c99SDave Chinner 		xfs_lock_two_inodes(src_dp, target_dp,
2956dbad7c99SDave Chinner 				    XFS_IOLOCK_EXCL | XFS_IOLOCK_PARENT);
2957dbad7c99SDave Chinner 
2958f6bba201SDave Chinner 	xfs_lock_inodes(inodes, num_inodes, XFS_ILOCK_EXCL);
2959f6bba201SDave Chinner 
2960f6bba201SDave Chinner 	/*
2961f6bba201SDave Chinner 	 * Join all the inodes to the transaction. From this point on,
2962f6bba201SDave Chinner 	 * we can rely on either trans_commit or trans_cancel to unlock
2963f6bba201SDave Chinner 	 * them.
2964f6bba201SDave Chinner 	 */
2965dbad7c99SDave Chinner 	xfs_trans_ijoin(tp, src_dp, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
2966f6bba201SDave Chinner 	if (new_parent)
2967dbad7c99SDave Chinner 		xfs_trans_ijoin(tp, target_dp, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
2968f6bba201SDave Chinner 	xfs_trans_ijoin(tp, src_ip, XFS_ILOCK_EXCL);
2969f6bba201SDave Chinner 	if (target_ip)
2970f6bba201SDave Chinner 		xfs_trans_ijoin(tp, target_ip, XFS_ILOCK_EXCL);
29717dcf5c3eSDave Chinner 	if (wip)
29727dcf5c3eSDave Chinner 		xfs_trans_ijoin(tp, wip, XFS_ILOCK_EXCL);
2973f6bba201SDave Chinner 
2974f6bba201SDave Chinner 	/*
2975f6bba201SDave Chinner 	 * If we are using project inheritance, we only allow renames
2976f6bba201SDave Chinner 	 * into our tree when the project IDs are the same; else the
2977f6bba201SDave Chinner 	 * tree quota mechanism would be circumvented.
2978f6bba201SDave Chinner 	 */
2979f6bba201SDave Chinner 	if (unlikely((target_dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2980f6bba201SDave Chinner 		     (xfs_get_projid(target_dp) != xfs_get_projid(src_ip)))) {
29812451337dSDave Chinner 		error = -EXDEV;
2982445883e8SDave Chinner 		goto out_trans_cancel;
2983f6bba201SDave Chinner 	}
2984f6bba201SDave Chinner 
2985445883e8SDave Chinner 	xfs_bmap_init(&free_list, &first_block);
2986445883e8SDave Chinner 
2987eeacd321SDave Chinner 	/* RENAME_EXCHANGE is unique from here on. */
2988eeacd321SDave Chinner 	if (flags & RENAME_EXCHANGE)
2989eeacd321SDave Chinner 		return xfs_cross_rename(tp, src_dp, src_name, src_ip,
2990d31a1825SCarlos Maiolino 					target_dp, target_name, target_ip,
2991d31a1825SCarlos Maiolino 					&free_list, &first_block, spaceres);
2992d31a1825SCarlos Maiolino 
2993d31a1825SCarlos Maiolino 	/*
2994f6bba201SDave Chinner 	 * Set up the target.
2995f6bba201SDave Chinner 	 */
2996f6bba201SDave Chinner 	if (target_ip == NULL) {
2997f6bba201SDave Chinner 		/*
2998f6bba201SDave Chinner 		 * If there's no space reservation, check the entry will
2999f6bba201SDave Chinner 		 * fit before actually inserting it.
3000f6bba201SDave Chinner 		 */
300194f3cad5SEric Sandeen 		if (!spaceres) {
300294f3cad5SEric Sandeen 			error = xfs_dir_canenter(tp, target_dp, target_name);
3003f6bba201SDave Chinner 			if (error)
3004445883e8SDave Chinner 				goto out_trans_cancel;
300594f3cad5SEric Sandeen 		}
3006f6bba201SDave Chinner 		/*
3007f6bba201SDave Chinner 		 * If target does not exist and the rename crosses
3008f6bba201SDave Chinner 		 * directories, adjust the target directory link count
3009f6bba201SDave Chinner 		 * to account for the ".." reference from the new entry.
3010f6bba201SDave Chinner 		 */
3011f6bba201SDave Chinner 		error = xfs_dir_createname(tp, target_dp, target_name,
3012f6bba201SDave Chinner 						src_ip->i_ino, &first_block,
3013f6bba201SDave Chinner 						&free_list, spaceres);
3014f6bba201SDave Chinner 		if (error)
30154906e215SChristoph Hellwig 			goto out_bmap_cancel;
3016f6bba201SDave Chinner 
3017f6bba201SDave Chinner 		xfs_trans_ichgtime(tp, target_dp,
3018f6bba201SDave Chinner 					XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3019f6bba201SDave Chinner 
3020f6bba201SDave Chinner 		if (new_parent && src_is_directory) {
3021f6bba201SDave Chinner 			error = xfs_bumplink(tp, target_dp);
3022f6bba201SDave Chinner 			if (error)
30234906e215SChristoph Hellwig 				goto out_bmap_cancel;
3024f6bba201SDave Chinner 		}
3025f6bba201SDave Chinner 	} else { /* target_ip != NULL */
3026f6bba201SDave Chinner 		/*
3027f6bba201SDave Chinner 		 * If target exists and it's a directory, check that both
3028f6bba201SDave Chinner 		 * target and source are directories and that target can be
3029f6bba201SDave Chinner 		 * destroyed, or that neither is a directory.
3030f6bba201SDave Chinner 		 */
3031f6bba201SDave Chinner 		if (S_ISDIR(target_ip->i_d.di_mode)) {
3032f6bba201SDave Chinner 			/*
3033f6bba201SDave Chinner 			 * Make sure target dir is empty.
3034f6bba201SDave Chinner 			 */
3035f6bba201SDave Chinner 			if (!(xfs_dir_isempty(target_ip)) ||
3036f6bba201SDave Chinner 			    (target_ip->i_d.di_nlink > 2)) {
30372451337dSDave Chinner 				error = -EEXIST;
3038445883e8SDave Chinner 				goto out_trans_cancel;
3039f6bba201SDave Chinner 			}
3040f6bba201SDave Chinner 		}
3041f6bba201SDave Chinner 
3042f6bba201SDave Chinner 		/*
3043f6bba201SDave Chinner 		 * Link the source inode under the target name.
3044f6bba201SDave Chinner 		 * If the source inode is a directory and we are moving
3045f6bba201SDave Chinner 		 * it across directories, its ".." entry will be
3046f6bba201SDave Chinner 		 * inconsistent until we replace that down below.
3047f6bba201SDave Chinner 		 *
3048f6bba201SDave Chinner 		 * In case there is already an entry with the same
3049f6bba201SDave Chinner 		 * name at the destination directory, remove it first.
3050f6bba201SDave Chinner 		 */
3051f6bba201SDave Chinner 		error = xfs_dir_replace(tp, target_dp, target_name,
3052f6bba201SDave Chinner 					src_ip->i_ino,
3053f6bba201SDave Chinner 					&first_block, &free_list, spaceres);
3054f6bba201SDave Chinner 		if (error)
30554906e215SChristoph Hellwig 			goto out_bmap_cancel;
3056f6bba201SDave Chinner 
3057f6bba201SDave Chinner 		xfs_trans_ichgtime(tp, target_dp,
3058f6bba201SDave Chinner 					XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3059f6bba201SDave Chinner 
3060f6bba201SDave Chinner 		/*
3061f6bba201SDave Chinner 		 * Decrement the link count on the target since the target
3062f6bba201SDave Chinner 		 * dir no longer points to it.
3063f6bba201SDave Chinner 		 */
3064f6bba201SDave Chinner 		error = xfs_droplink(tp, target_ip);
3065f6bba201SDave Chinner 		if (error)
30664906e215SChristoph Hellwig 			goto out_bmap_cancel;
3067f6bba201SDave Chinner 
3068f6bba201SDave Chinner 		if (src_is_directory) {
3069f6bba201SDave Chinner 			/*
3070f6bba201SDave Chinner 			 * Drop the link from the old "." entry.
3071f6bba201SDave Chinner 			 */
3072f6bba201SDave Chinner 			error = xfs_droplink(tp, target_ip);
3073f6bba201SDave Chinner 			if (error)
30744906e215SChristoph Hellwig 				goto out_bmap_cancel;
3075f6bba201SDave Chinner 		}
3076f6bba201SDave Chinner 	} /* target_ip != NULL */
3077f6bba201SDave Chinner 
3078f6bba201SDave Chinner 	/*
3079f6bba201SDave Chinner 	 * Remove the source.
3080f6bba201SDave Chinner 	 */
3081f6bba201SDave Chinner 	if (new_parent && src_is_directory) {
3082f6bba201SDave Chinner 		/*
3083f6bba201SDave Chinner 		 * Rewrite the ".." entry to point to the new
3084f6bba201SDave Chinner 		 * directory.
3085f6bba201SDave Chinner 		 */
3086f6bba201SDave Chinner 		error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot,
3087f6bba201SDave Chinner 					target_dp->i_ino,
3088f6bba201SDave Chinner 					&first_block, &free_list, spaceres);
30892451337dSDave Chinner 		ASSERT(error != -EEXIST);
3090f6bba201SDave Chinner 		if (error)
30914906e215SChristoph Hellwig 			goto out_bmap_cancel;
3092f6bba201SDave Chinner 	}
3093f6bba201SDave Chinner 
3094f6bba201SDave Chinner 	/*
3095f6bba201SDave Chinner 	 * We always want to hit the ctime on the source inode.
3096f6bba201SDave Chinner 	 *
3097f6bba201SDave Chinner 	 * This isn't strictly required by the standards since the source
3098f6bba201SDave Chinner 	 * inode isn't really being changed, but old unix file systems did
3099f6bba201SDave Chinner 	 * it and some incremental backup programs won't work without it.
3100f6bba201SDave Chinner 	 */
3101f6bba201SDave Chinner 	xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG);
3102f6bba201SDave Chinner 	xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE);
3103f6bba201SDave Chinner 
3104f6bba201SDave Chinner 	/*
3105f6bba201SDave Chinner 	 * Adjust the link count on src_dp.  This is necessary when
3106f6bba201SDave Chinner 	 * renaming a directory, either within one parent when
3107f6bba201SDave Chinner 	 * the target existed, or across two parent directories.
3108f6bba201SDave Chinner 	 */
3109f6bba201SDave Chinner 	if (src_is_directory && (new_parent || target_ip != NULL)) {
3110f6bba201SDave Chinner 
3111f6bba201SDave Chinner 		/*
3112f6bba201SDave Chinner 		 * Decrement link count on src_directory since the
3113f6bba201SDave Chinner 		 * entry that's moved no longer points to it.
3114f6bba201SDave Chinner 		 */
3115f6bba201SDave Chinner 		error = xfs_droplink(tp, src_dp);
3116f6bba201SDave Chinner 		if (error)
31174906e215SChristoph Hellwig 			goto out_bmap_cancel;
3118f6bba201SDave Chinner 	}
3119f6bba201SDave Chinner 
31207dcf5c3eSDave Chinner 	/*
31217dcf5c3eSDave Chinner 	 * For whiteouts, we only need to update the source dirent with the
31227dcf5c3eSDave Chinner 	 * inode number of the whiteout inode rather than removing it
31237dcf5c3eSDave Chinner 	 * altogether.
31247dcf5c3eSDave Chinner 	 */
31257dcf5c3eSDave Chinner 	if (wip) {
31267dcf5c3eSDave Chinner 		error = xfs_dir_replace(tp, src_dp, src_name, wip->i_ino,
31277dcf5c3eSDave Chinner 					&first_block, &free_list, spaceres);
31287dcf5c3eSDave Chinner 	} else
3129f6bba201SDave Chinner 		error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino,
3130f6bba201SDave Chinner 					   &first_block, &free_list, spaceres);
3131f6bba201SDave Chinner 	if (error)
31324906e215SChristoph Hellwig 		goto out_bmap_cancel;
3133f6bba201SDave Chinner 
31347dcf5c3eSDave Chinner 	/*
31357dcf5c3eSDave Chinner 	 * For whiteouts, we need to bump the link count on the whiteout inode.
31367dcf5c3eSDave Chinner 	 * This means that failures all the way up to this point leave the inode
31377dcf5c3eSDave Chinner 	 * on the unlinked list and so cleanup is a simple matter of dropping
31387dcf5c3eSDave Chinner 	 * the remaining reference to it. If we fail here after bumping the link
31397dcf5c3eSDave Chinner 	 * count, we're shutting down the filesystem so we'll never see the
31407dcf5c3eSDave Chinner 	 * intermediate state on disk.
31417dcf5c3eSDave Chinner 	 */
31427dcf5c3eSDave Chinner 	if (wip) {
314322419ac9SBrian Foster 		ASSERT(VFS_I(wip)->i_nlink == 0 && wip->i_d.di_nlink == 0);
31447dcf5c3eSDave Chinner 		error = xfs_bumplink(tp, wip);
31457dcf5c3eSDave Chinner 		if (error)
31464906e215SChristoph Hellwig 			goto out_bmap_cancel;
31477dcf5c3eSDave Chinner 		error = xfs_iunlink_remove(tp, wip);
31487dcf5c3eSDave Chinner 		if (error)
31494906e215SChristoph Hellwig 			goto out_bmap_cancel;
31507dcf5c3eSDave Chinner 		xfs_trans_log_inode(tp, wip, XFS_ILOG_CORE);
31517dcf5c3eSDave Chinner 
31527dcf5c3eSDave Chinner 		/*
31537dcf5c3eSDave Chinner 		 * Now we have a real link, clear the "I'm a tmpfile" state
31547dcf5c3eSDave Chinner 		 * flag from the inode so it doesn't accidentally get misused in
31557dcf5c3eSDave Chinner 		 * future.
31567dcf5c3eSDave Chinner 		 */
31577dcf5c3eSDave Chinner 		VFS_I(wip)->i_state &= ~I_LINKABLE;
31587dcf5c3eSDave Chinner 	}
3159f6bba201SDave Chinner 
3160f6bba201SDave Chinner 	xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3161f6bba201SDave Chinner 	xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE);
3162f6bba201SDave Chinner 	if (new_parent)
3163f6bba201SDave Chinner 		xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE);
3164f6bba201SDave Chinner 
31657dcf5c3eSDave Chinner 	error = xfs_finish_rename(tp, &free_list);
31667dcf5c3eSDave Chinner 	if (wip)
31677dcf5c3eSDave Chinner 		IRELE(wip);
31687dcf5c3eSDave Chinner 	return error;
3169f6bba201SDave Chinner 
3170445883e8SDave Chinner out_bmap_cancel:
3171f6bba201SDave Chinner 	xfs_bmap_cancel(&free_list);
3172445883e8SDave Chinner out_trans_cancel:
31734906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
31747dcf5c3eSDave Chinner 	if (wip)
31757dcf5c3eSDave Chinner 		IRELE(wip);
3176f6bba201SDave Chinner 	return error;
3177f6bba201SDave Chinner }
3178f6bba201SDave Chinner 
3179bad55843SDavid Chinner STATIC int
3180bad55843SDavid Chinner xfs_iflush_cluster(
3181bad55843SDavid Chinner 	xfs_inode_t	*ip,
3182bad55843SDavid Chinner 	xfs_buf_t	*bp)
3183bad55843SDavid Chinner {
3184bad55843SDavid Chinner 	xfs_mount_t		*mp = ip->i_mount;
31855017e97dSDave Chinner 	struct xfs_perag	*pag;
3186bad55843SDavid Chinner 	unsigned long		first_index, mask;
3187c8f5f12eSDavid Chinner 	unsigned long		inodes_per_cluster;
3188bad55843SDavid Chinner 	int			ilist_size;
3189bad55843SDavid Chinner 	xfs_inode_t		**ilist;
3190bad55843SDavid Chinner 	xfs_inode_t		*iq;
3191bad55843SDavid Chinner 	int			nr_found;
3192bad55843SDavid Chinner 	int			clcount = 0;
3193bad55843SDavid Chinner 	int			bufwasdelwri;
3194bad55843SDavid Chinner 	int			i;
3195bad55843SDavid Chinner 
31965017e97dSDave Chinner 	pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
3197bad55843SDavid Chinner 
31980f49efd8SJie Liu 	inodes_per_cluster = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog;
3199c8f5f12eSDavid Chinner 	ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
320049383b0eSDavid Chinner 	ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS);
3201bad55843SDavid Chinner 	if (!ilist)
320244b56e0aSDave Chinner 		goto out_put;
3203bad55843SDavid Chinner 
32040f49efd8SJie Liu 	mask = ~(((mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog)) - 1);
3205bad55843SDavid Chinner 	first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
32061a3e8f3dSDave Chinner 	rcu_read_lock();
3207bad55843SDavid Chinner 	/* really need a gang lookup range call here */
3208bad55843SDavid Chinner 	nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist,
3209c8f5f12eSDavid Chinner 					first_index, inodes_per_cluster);
3210bad55843SDavid Chinner 	if (nr_found == 0)
3211bad55843SDavid Chinner 		goto out_free;
3212bad55843SDavid Chinner 
3213bad55843SDavid Chinner 	for (i = 0; i < nr_found; i++) {
3214bad55843SDavid Chinner 		iq = ilist[i];
3215bad55843SDavid Chinner 		if (iq == ip)
3216bad55843SDavid Chinner 			continue;
32171a3e8f3dSDave Chinner 
32181a3e8f3dSDave Chinner 		/*
32191a3e8f3dSDave Chinner 		 * because this is an RCU protected lookup, we could find a
32201a3e8f3dSDave Chinner 		 * recently freed or even reallocated inode during the lookup.
32211a3e8f3dSDave Chinner 		 * We need to check under the i_flags_lock for a valid inode
32221a3e8f3dSDave Chinner 		 * here. Skip it if it is not valid or the wrong inode.
32231a3e8f3dSDave Chinner 		 */
32241a3e8f3dSDave Chinner 		spin_lock(&ip->i_flags_lock);
32251a3e8f3dSDave Chinner 		if (!ip->i_ino ||
32261a3e8f3dSDave Chinner 		    (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) {
32271a3e8f3dSDave Chinner 			spin_unlock(&ip->i_flags_lock);
32281a3e8f3dSDave Chinner 			continue;
32291a3e8f3dSDave Chinner 		}
32301a3e8f3dSDave Chinner 		spin_unlock(&ip->i_flags_lock);
32311a3e8f3dSDave Chinner 
3232bad55843SDavid Chinner 		/*
3233bad55843SDavid Chinner 		 * Do an un-protected check to see if the inode is dirty and
3234bad55843SDavid Chinner 		 * is a candidate for flushing.  These checks will be repeated
3235bad55843SDavid Chinner 		 * later after the appropriate locks are acquired.
3236bad55843SDavid Chinner 		 */
323733540408SDavid Chinner 		if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0)
3238bad55843SDavid Chinner 			continue;
3239bad55843SDavid Chinner 
3240bad55843SDavid Chinner 		/*
3241bad55843SDavid Chinner 		 * Try to get locks.  If any are unavailable or it is pinned,
3242bad55843SDavid Chinner 		 * then this inode cannot be flushed and is skipped.
3243bad55843SDavid Chinner 		 */
3244bad55843SDavid Chinner 
3245bad55843SDavid Chinner 		if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED))
3246bad55843SDavid Chinner 			continue;
3247bad55843SDavid Chinner 		if (!xfs_iflock_nowait(iq)) {
3248bad55843SDavid Chinner 			xfs_iunlock(iq, XFS_ILOCK_SHARED);
3249bad55843SDavid Chinner 			continue;
3250bad55843SDavid Chinner 		}
3251bad55843SDavid Chinner 		if (xfs_ipincount(iq)) {
3252bad55843SDavid Chinner 			xfs_ifunlock(iq);
3253bad55843SDavid Chinner 			xfs_iunlock(iq, XFS_ILOCK_SHARED);
3254bad55843SDavid Chinner 			continue;
3255bad55843SDavid Chinner 		}
3256bad55843SDavid Chinner 
3257bad55843SDavid Chinner 		/*
3258bad55843SDavid Chinner 		 * arriving here means that this inode can be flushed.  First
3259bad55843SDavid Chinner 		 * re-check that it's dirty before flushing.
3260bad55843SDavid Chinner 		 */
326133540408SDavid Chinner 		if (!xfs_inode_clean(iq)) {
3262bad55843SDavid Chinner 			int	error;
3263bad55843SDavid Chinner 			error = xfs_iflush_int(iq, bp);
3264bad55843SDavid Chinner 			if (error) {
3265bad55843SDavid Chinner 				xfs_iunlock(iq, XFS_ILOCK_SHARED);
3266bad55843SDavid Chinner 				goto cluster_corrupt_out;
3267bad55843SDavid Chinner 			}
3268bad55843SDavid Chinner 			clcount++;
3269bad55843SDavid Chinner 		} else {
3270bad55843SDavid Chinner 			xfs_ifunlock(iq);
3271bad55843SDavid Chinner 		}
3272bad55843SDavid Chinner 		xfs_iunlock(iq, XFS_ILOCK_SHARED);
3273bad55843SDavid Chinner 	}
3274bad55843SDavid Chinner 
3275bad55843SDavid Chinner 	if (clcount) {
3276ff6d6af2SBill O'Donnell 		XFS_STATS_INC(mp, xs_icluster_flushcnt);
3277ff6d6af2SBill O'Donnell 		XFS_STATS_ADD(mp, xs_icluster_flushinode, clcount);
3278bad55843SDavid Chinner 	}
3279bad55843SDavid Chinner 
3280bad55843SDavid Chinner out_free:
32811a3e8f3dSDave Chinner 	rcu_read_unlock();
3282f0e2d93cSDenys Vlasenko 	kmem_free(ilist);
328344b56e0aSDave Chinner out_put:
328444b56e0aSDave Chinner 	xfs_perag_put(pag);
3285bad55843SDavid Chinner 	return 0;
3286bad55843SDavid Chinner 
3287bad55843SDavid Chinner 
3288bad55843SDavid Chinner cluster_corrupt_out:
3289bad55843SDavid Chinner 	/*
3290bad55843SDavid Chinner 	 * Corruption detected in the clustering loop.  Invalidate the
3291bad55843SDavid Chinner 	 * inode buffer and shut down the filesystem.
3292bad55843SDavid Chinner 	 */
32931a3e8f3dSDave Chinner 	rcu_read_unlock();
3294bad55843SDavid Chinner 	/*
329543ff2122SChristoph Hellwig 	 * Clean up the buffer.  If it was delwri, just release it --
3296bad55843SDavid Chinner 	 * brelse can handle it with no problems.  If not, shut down the
3297bad55843SDavid Chinner 	 * filesystem before releasing the buffer.
3298bad55843SDavid Chinner 	 */
329943ff2122SChristoph Hellwig 	bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q);
3300bad55843SDavid Chinner 	if (bufwasdelwri)
3301bad55843SDavid Chinner 		xfs_buf_relse(bp);
3302bad55843SDavid Chinner 
3303bad55843SDavid Chinner 	xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
3304bad55843SDavid Chinner 
3305bad55843SDavid Chinner 	if (!bufwasdelwri) {
3306bad55843SDavid Chinner 		/*
3307bad55843SDavid Chinner 		 * Just like incore_relse: if we have b_iodone functions,
3308bad55843SDavid Chinner 		 * mark the buffer as an error and call them.  Otherwise
3309bad55843SDavid Chinner 		 * mark it as stale and brelse.
3310bad55843SDavid Chinner 		 */
3311cb669ca5SChristoph Hellwig 		if (bp->b_iodone) {
3312bad55843SDavid Chinner 			XFS_BUF_UNDONE(bp);
3313c867cb61SChristoph Hellwig 			xfs_buf_stale(bp);
33142451337dSDave Chinner 			xfs_buf_ioerror(bp, -EIO);
3315e8aaba9aSDave Chinner 			xfs_buf_ioend(bp);
3316bad55843SDavid Chinner 		} else {
3317c867cb61SChristoph Hellwig 			xfs_buf_stale(bp);
3318bad55843SDavid Chinner 			xfs_buf_relse(bp);
3319bad55843SDavid Chinner 		}
3320bad55843SDavid Chinner 	}
3321bad55843SDavid Chinner 
3322bad55843SDavid Chinner 	/*
3323bad55843SDavid Chinner 	 * Unlocks the flush lock
3324bad55843SDavid Chinner 	 */
332504913fddSDave Chinner 	xfs_iflush_abort(iq, false);
3326f0e2d93cSDenys Vlasenko 	kmem_free(ilist);
332744b56e0aSDave Chinner 	xfs_perag_put(pag);
33282451337dSDave Chinner 	return -EFSCORRUPTED;
3329bad55843SDavid Chinner }
3330bad55843SDavid Chinner 
33311da177e4SLinus Torvalds /*
33324c46819aSChristoph Hellwig  * Flush dirty inode metadata into the backing buffer.
33334c46819aSChristoph Hellwig  *
33344c46819aSChristoph Hellwig  * The caller must have the inode lock and the inode flush lock held.  The
33354c46819aSChristoph Hellwig  * inode lock will still be held upon return to the caller, and the inode
33364c46819aSChristoph Hellwig  * flush lock will be released after the inode has reached the disk.
33374c46819aSChristoph Hellwig  *
33384c46819aSChristoph Hellwig  * The caller must write out the buffer returned in *bpp and release it.
33391da177e4SLinus Torvalds  */
33401da177e4SLinus Torvalds int
33411da177e4SLinus Torvalds xfs_iflush(
33424c46819aSChristoph Hellwig 	struct xfs_inode	*ip,
33434c46819aSChristoph Hellwig 	struct xfs_buf		**bpp)
33441da177e4SLinus Torvalds {
33454c46819aSChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
33464c46819aSChristoph Hellwig 	struct xfs_buf		*bp;
33474c46819aSChristoph Hellwig 	struct xfs_dinode	*dip;
33481da177e4SLinus Torvalds 	int			error;
33491da177e4SLinus Torvalds 
3350ff6d6af2SBill O'Donnell 	XFS_STATS_INC(mp, xs_iflush_count);
33511da177e4SLinus Torvalds 
3352579aa9caSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
3353474fce06SChristoph Hellwig 	ASSERT(xfs_isiflocked(ip));
33541da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
33558096b1ebSChristoph Hellwig 	       ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
33561da177e4SLinus Torvalds 
33574c46819aSChristoph Hellwig 	*bpp = NULL;
33581da177e4SLinus Torvalds 
33591da177e4SLinus Torvalds 	xfs_iunpin_wait(ip);
33601da177e4SLinus Torvalds 
33611da177e4SLinus Torvalds 	/*
33624b6a4688SDave Chinner 	 * For stale inodes we cannot rely on the backing buffer remaining
33634b6a4688SDave Chinner 	 * stale in cache for the remaining life of the stale inode and so
3364475ee413SChristoph Hellwig 	 * xfs_imap_to_bp() below may give us a buffer that no longer contains
33654b6a4688SDave Chinner 	 * inodes below. We have to check this after ensuring the inode is
33664b6a4688SDave Chinner 	 * unpinned so that it is safe to reclaim the stale inode after the
33674b6a4688SDave Chinner 	 * flush call.
33684b6a4688SDave Chinner 	 */
33694b6a4688SDave Chinner 	if (xfs_iflags_test(ip, XFS_ISTALE)) {
33704b6a4688SDave Chinner 		xfs_ifunlock(ip);
33714b6a4688SDave Chinner 		return 0;
33724b6a4688SDave Chinner 	}
33734b6a4688SDave Chinner 
33744b6a4688SDave Chinner 	/*
33751da177e4SLinus Torvalds 	 * This may have been unpinned because the filesystem is shutting
33761da177e4SLinus Torvalds 	 * down forcibly. If that's the case we must not write this inode
337732ce90a4SChristoph Hellwig 	 * to disk, because the log record didn't make it to disk.
337832ce90a4SChristoph Hellwig 	 *
337932ce90a4SChristoph Hellwig 	 * We also have to remove the log item from the AIL in this case,
338032ce90a4SChristoph Hellwig 	 * as we wait for an empty AIL as part of the unmount process.
33811da177e4SLinus Torvalds 	 */
33821da177e4SLinus Torvalds 	if (XFS_FORCED_SHUTDOWN(mp)) {
33832451337dSDave Chinner 		error = -EIO;
338432ce90a4SChristoph Hellwig 		goto abort_out;
33851da177e4SLinus Torvalds 	}
33861da177e4SLinus Torvalds 
33871da177e4SLinus Torvalds 	/*
3388a3f74ffbSDavid Chinner 	 * Get the buffer containing the on-disk inode.
3389a3f74ffbSDavid Chinner 	 */
3390475ee413SChristoph Hellwig 	error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK,
3391475ee413SChristoph Hellwig 			       0);
3392a3f74ffbSDavid Chinner 	if (error || !bp) {
3393a3f74ffbSDavid Chinner 		xfs_ifunlock(ip);
3394a3f74ffbSDavid Chinner 		return error;
3395a3f74ffbSDavid Chinner 	}
3396a3f74ffbSDavid Chinner 
3397a3f74ffbSDavid Chinner 	/*
33981da177e4SLinus Torvalds 	 * First flush out the inode that xfs_iflush was called with.
33991da177e4SLinus Torvalds 	 */
34001da177e4SLinus Torvalds 	error = xfs_iflush_int(ip, bp);
3401bad55843SDavid Chinner 	if (error)
34021da177e4SLinus Torvalds 		goto corrupt_out;
34031da177e4SLinus Torvalds 
34041da177e4SLinus Torvalds 	/*
3405a3f74ffbSDavid Chinner 	 * If the buffer is pinned then push on the log now so we won't
3406a3f74ffbSDavid Chinner 	 * get stuck waiting in the write for too long.
3407a3f74ffbSDavid Chinner 	 */
3408811e64c7SChandra Seetharaman 	if (xfs_buf_ispinned(bp))
3409a14a348bSChristoph Hellwig 		xfs_log_force(mp, 0);
3410a3f74ffbSDavid Chinner 
3411a3f74ffbSDavid Chinner 	/*
34121da177e4SLinus Torvalds 	 * inode clustering:
34131da177e4SLinus Torvalds 	 * see if other inodes can be gathered into this write
34141da177e4SLinus Torvalds 	 */
3415bad55843SDavid Chinner 	error = xfs_iflush_cluster(ip, bp);
3416bad55843SDavid Chinner 	if (error)
34171da177e4SLinus Torvalds 		goto cluster_corrupt_out;
34181da177e4SLinus Torvalds 
34194c46819aSChristoph Hellwig 	*bpp = bp;
34204c46819aSChristoph Hellwig 	return 0;
34211da177e4SLinus Torvalds 
34221da177e4SLinus Torvalds corrupt_out:
34231da177e4SLinus Torvalds 	xfs_buf_relse(bp);
34247d04a335SNathan Scott 	xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
34251da177e4SLinus Torvalds cluster_corrupt_out:
34262451337dSDave Chinner 	error = -EFSCORRUPTED;
342732ce90a4SChristoph Hellwig abort_out:
34281da177e4SLinus Torvalds 	/*
34291da177e4SLinus Torvalds 	 * Unlocks the flush lock
34301da177e4SLinus Torvalds 	 */
343104913fddSDave Chinner 	xfs_iflush_abort(ip, false);
343232ce90a4SChristoph Hellwig 	return error;
34331da177e4SLinus Torvalds }
34341da177e4SLinus Torvalds 
34351da177e4SLinus Torvalds STATIC int
34361da177e4SLinus Torvalds xfs_iflush_int(
343793848a99SChristoph Hellwig 	struct xfs_inode	*ip,
343893848a99SChristoph Hellwig 	struct xfs_buf		*bp)
34391da177e4SLinus Torvalds {
344093848a99SChristoph Hellwig 	struct xfs_inode_log_item *iip = ip->i_itemp;
344193848a99SChristoph Hellwig 	struct xfs_dinode	*dip;
344293848a99SChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
34431da177e4SLinus Torvalds 
3444579aa9caSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
3445474fce06SChristoph Hellwig 	ASSERT(xfs_isiflocked(ip));
34461da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
34478096b1ebSChristoph Hellwig 	       ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
344893848a99SChristoph Hellwig 	ASSERT(iip != NULL && iip->ili_fields != 0);
3449263997a6SDave Chinner 	ASSERT(ip->i_d.di_version > 1);
34501da177e4SLinus Torvalds 
34511da177e4SLinus Torvalds 	/* set *dip = inode's place in the buffer */
345288ee2df7SChristoph Hellwig 	dip = xfs_buf_offset(bp, ip->i_imap.im_boffset);
34531da177e4SLinus Torvalds 
345469ef921bSChristoph Hellwig 	if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
34551da177e4SLinus Torvalds 			       mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
34566a19d939SDave Chinner 		xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
34576a19d939SDave Chinner 			"%s: Bad inode %Lu magic number 0x%x, ptr 0x%p",
34586a19d939SDave Chinner 			__func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
34591da177e4SLinus Torvalds 		goto corrupt_out;
34601da177e4SLinus Torvalds 	}
3461abbede1bSAl Viro 	if (S_ISREG(ip->i_d.di_mode)) {
34621da177e4SLinus Torvalds 		if (XFS_TEST_ERROR(
34631da177e4SLinus Torvalds 		    (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
34641da177e4SLinus Torvalds 		    (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
34651da177e4SLinus Torvalds 		    mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
34666a19d939SDave Chinner 			xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
34676a19d939SDave Chinner 				"%s: Bad regular inode %Lu, ptr 0x%p",
34686a19d939SDave Chinner 				__func__, ip->i_ino, ip);
34691da177e4SLinus Torvalds 			goto corrupt_out;
34701da177e4SLinus Torvalds 		}
3471abbede1bSAl Viro 	} else if (S_ISDIR(ip->i_d.di_mode)) {
34721da177e4SLinus Torvalds 		if (XFS_TEST_ERROR(
34731da177e4SLinus Torvalds 		    (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
34741da177e4SLinus Torvalds 		    (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
34751da177e4SLinus Torvalds 		    (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
34761da177e4SLinus Torvalds 		    mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
34776a19d939SDave Chinner 			xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
34786a19d939SDave Chinner 				"%s: Bad directory inode %Lu, ptr 0x%p",
34796a19d939SDave Chinner 				__func__, ip->i_ino, ip);
34801da177e4SLinus Torvalds 			goto corrupt_out;
34811da177e4SLinus Torvalds 		}
34821da177e4SLinus Torvalds 	}
34831da177e4SLinus Torvalds 	if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
34841da177e4SLinus Torvalds 				ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
34851da177e4SLinus Torvalds 				XFS_RANDOM_IFLUSH_5)) {
34866a19d939SDave Chinner 		xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
34876a19d939SDave Chinner 			"%s: detected corrupt incore inode %Lu, "
34886a19d939SDave Chinner 			"total extents = %d, nblocks = %Ld, ptr 0x%p",
34896a19d939SDave Chinner 			__func__, ip->i_ino,
34901da177e4SLinus Torvalds 			ip->i_d.di_nextents + ip->i_d.di_anextents,
34916a19d939SDave Chinner 			ip->i_d.di_nblocks, ip);
34921da177e4SLinus Torvalds 		goto corrupt_out;
34931da177e4SLinus Torvalds 	}
34941da177e4SLinus Torvalds 	if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
34951da177e4SLinus Torvalds 				mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
34966a19d939SDave Chinner 		xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
34976a19d939SDave Chinner 			"%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
34986a19d939SDave Chinner 			__func__, ip->i_ino, ip->i_d.di_forkoff, ip);
34991da177e4SLinus Torvalds 		goto corrupt_out;
35001da177e4SLinus Torvalds 	}
3501e60896d8SDave Chinner 
35021da177e4SLinus Torvalds 	/*
3503263997a6SDave Chinner 	 * Inode item log recovery for v2 inodes are dependent on the
3504e60896d8SDave Chinner 	 * di_flushiter count for correct sequencing. We bump the flush
3505e60896d8SDave Chinner 	 * iteration count so we can detect flushes which postdate a log record
3506e60896d8SDave Chinner 	 * during recovery. This is redundant as we now log every change and
3507e60896d8SDave Chinner 	 * hence this can't happen but we need to still do it to ensure
3508e60896d8SDave Chinner 	 * backwards compatibility with old kernels that predate logging all
3509e60896d8SDave Chinner 	 * inode changes.
35101da177e4SLinus Torvalds 	 */
3511e60896d8SDave Chinner 	if (ip->i_d.di_version < 3)
35121da177e4SLinus Torvalds 		ip->i_d.di_flushiter++;
35131da177e4SLinus Torvalds 
35141da177e4SLinus Torvalds 	/*
35153987848cSDave Chinner 	 * Copy the dirty parts of the inode into the on-disk inode.  We always
35163987848cSDave Chinner 	 * copy out the core of the inode, because if the inode is dirty at all
35173987848cSDave Chinner 	 * the core must be.
35181da177e4SLinus Torvalds 	 */
3519*93f958f9SDave Chinner 	xfs_inode_to_disk(ip, dip, iip->ili_item.li_lsn);
35201da177e4SLinus Torvalds 
35211da177e4SLinus Torvalds 	/* Wrap, we never let the log put out DI_MAX_FLUSH */
35221da177e4SLinus Torvalds 	if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
35231da177e4SLinus Torvalds 		ip->i_d.di_flushiter = 0;
35241da177e4SLinus Torvalds 
3525fd9fdba6SEric Sandeen 	xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK);
3526e4ac967bSDavid Chinner 	if (XFS_IFORK_Q(ip))
3527fd9fdba6SEric Sandeen 		xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK);
35281da177e4SLinus Torvalds 	xfs_inobp_check(mp, bp);
35291da177e4SLinus Torvalds 
35301da177e4SLinus Torvalds 	/*
3531f5d8d5c4SChristoph Hellwig 	 * We've recorded everything logged in the inode, so we'd like to clear
3532f5d8d5c4SChristoph Hellwig 	 * the ili_fields bits so we don't log and flush things unnecessarily.
3533f5d8d5c4SChristoph Hellwig 	 * However, we can't stop logging all this information until the data
3534f5d8d5c4SChristoph Hellwig 	 * we've copied into the disk buffer is written to disk.  If we did we
3535f5d8d5c4SChristoph Hellwig 	 * might overwrite the copy of the inode in the log with all the data
3536f5d8d5c4SChristoph Hellwig 	 * after re-logging only part of it, and in the face of a crash we
3537f5d8d5c4SChristoph Hellwig 	 * wouldn't have all the data we need to recover.
35381da177e4SLinus Torvalds 	 *
3539f5d8d5c4SChristoph Hellwig 	 * What we do is move the bits to the ili_last_fields field.  When
3540f5d8d5c4SChristoph Hellwig 	 * logging the inode, these bits are moved back to the ili_fields field.
3541f5d8d5c4SChristoph Hellwig 	 * In the xfs_iflush_done() routine we clear ili_last_fields, since we
3542f5d8d5c4SChristoph Hellwig 	 * know that the information those bits represent is permanently on
3543f5d8d5c4SChristoph Hellwig 	 * disk.  As long as the flush completes before the inode is logged
3544f5d8d5c4SChristoph Hellwig 	 * again, then both ili_fields and ili_last_fields will be cleared.
35451da177e4SLinus Torvalds 	 *
3546f5d8d5c4SChristoph Hellwig 	 * We can play with the ili_fields bits here, because the inode lock
3547f5d8d5c4SChristoph Hellwig 	 * must be held exclusively in order to set bits there and the flush
3548f5d8d5c4SChristoph Hellwig 	 * lock protects the ili_last_fields bits.  Set ili_logged so the flush
3549f5d8d5c4SChristoph Hellwig 	 * done routine can tell whether or not to look in the AIL.  Also, store
3550f5d8d5c4SChristoph Hellwig 	 * the current LSN of the inode so that we can tell whether the item has
3551f5d8d5c4SChristoph Hellwig 	 * moved in the AIL from xfs_iflush_done().  In order to read the lsn we
3552f5d8d5c4SChristoph Hellwig 	 * need the AIL lock, because it is a 64 bit value that cannot be read
3553f5d8d5c4SChristoph Hellwig 	 * atomically.
35541da177e4SLinus Torvalds 	 */
3555f5d8d5c4SChristoph Hellwig 	iip->ili_last_fields = iip->ili_fields;
3556f5d8d5c4SChristoph Hellwig 	iip->ili_fields = 0;
3557fc0561ceSDave Chinner 	iip->ili_fsync_fields = 0;
35581da177e4SLinus Torvalds 	iip->ili_logged = 1;
35591da177e4SLinus Torvalds 
35607b2e2a31SDavid Chinner 	xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
35617b2e2a31SDavid Chinner 				&iip->ili_item.li_lsn);
35621da177e4SLinus Torvalds 
35631da177e4SLinus Torvalds 	/*
35641da177e4SLinus Torvalds 	 * Attach the function xfs_iflush_done to the inode's
35651da177e4SLinus Torvalds 	 * buffer.  This will remove the inode from the AIL
35661da177e4SLinus Torvalds 	 * and unlock the inode's flush lock when the inode is
35671da177e4SLinus Torvalds 	 * completely written to disk.
35681da177e4SLinus Torvalds 	 */
3569ca30b2a7SChristoph Hellwig 	xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
35701da177e4SLinus Torvalds 
357193848a99SChristoph Hellwig 	/* generate the checksum. */
357293848a99SChristoph Hellwig 	xfs_dinode_calc_crc(mp, dip);
357393848a99SChristoph Hellwig 
3574adadbeefSChristoph Hellwig 	ASSERT(bp->b_fspriv != NULL);
3575cb669ca5SChristoph Hellwig 	ASSERT(bp->b_iodone != NULL);
35761da177e4SLinus Torvalds 	return 0;
35771da177e4SLinus Torvalds 
35781da177e4SLinus Torvalds corrupt_out:
35792451337dSDave Chinner 	return -EFSCORRUPTED;
35801da177e4SLinus Torvalds }
3581