xref: /openbmc/linux/fs/xfs/xfs_inode.c (revision d6077aa339d6580d12bd1089231eea2940383e32)
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));
167fa96acadSDave Chinner 	ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_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));
215fa96acadSDave Chinner 	ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_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));
284fa96acadSDave Chinner 	ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_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 
365c24b5dfaSDave Chinner /*
366653c60b6SDave Chinner  * Bump the subclass so xfs_lock_inodes() acquires each lock with a different
367653c60b6SDave Chinner  * value. This shouldn't be called for page fault locking, but we also need to
368653c60b6SDave Chinner  * ensure we don't overrun the number of lockdep subclasses for the iolock or
369653c60b6SDave Chinner  * mmaplock as that is limited to 12 by the mmap lock lockdep annotations.
370c24b5dfaSDave Chinner  */
371c24b5dfaSDave Chinner static inline int
372c24b5dfaSDave Chinner xfs_lock_inumorder(int lock_mode, int subclass)
373c24b5dfaSDave Chinner {
374653c60b6SDave Chinner 	if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) {
375653c60b6SDave Chinner 		ASSERT(subclass + XFS_LOCK_INUMORDER <
376653c60b6SDave Chinner 			(1 << (XFS_MMAPLOCK_SHIFT - XFS_IOLOCK_SHIFT)));
377c24b5dfaSDave Chinner 		lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
378653c60b6SDave Chinner 	}
379653c60b6SDave Chinner 
380653c60b6SDave Chinner 	if (lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) {
381653c60b6SDave Chinner 		ASSERT(subclass + XFS_LOCK_INUMORDER <
382653c60b6SDave Chinner 			(1 << (XFS_ILOCK_SHIFT - XFS_MMAPLOCK_SHIFT)));
383653c60b6SDave Chinner 		lock_mode |= (subclass + XFS_LOCK_INUMORDER) <<
384653c60b6SDave Chinner 							XFS_MMAPLOCK_SHIFT;
385653c60b6SDave Chinner 	}
386653c60b6SDave Chinner 
387c24b5dfaSDave Chinner 	if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
388c24b5dfaSDave Chinner 		lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
389c24b5dfaSDave Chinner 
390c24b5dfaSDave Chinner 	return lock_mode;
391c24b5dfaSDave Chinner }
392c24b5dfaSDave Chinner 
393c24b5dfaSDave Chinner /*
39495afcf5cSDave Chinner  * The following routine will lock n inodes in exclusive mode.  We assume the
39595afcf5cSDave Chinner  * caller calls us with the inodes in i_ino order.
396c24b5dfaSDave Chinner  *
39795afcf5cSDave Chinner  * We need to detect deadlock where an inode that we lock is in the AIL and we
39895afcf5cSDave Chinner  * start waiting for another inode that is locked by a thread in a long running
39995afcf5cSDave Chinner  * transaction (such as truncate). This can result in deadlock since the long
40095afcf5cSDave Chinner  * running trans might need to wait for the inode we just locked in order to
40195afcf5cSDave Chinner  * push the tail and free space in the log.
402c24b5dfaSDave Chinner  */
403c24b5dfaSDave Chinner void
404c24b5dfaSDave Chinner xfs_lock_inodes(
405c24b5dfaSDave Chinner 	xfs_inode_t	**ips,
406c24b5dfaSDave Chinner 	int		inodes,
407c24b5dfaSDave Chinner 	uint		lock_mode)
408c24b5dfaSDave Chinner {
409c24b5dfaSDave Chinner 	int		attempts = 0, i, j, try_lock;
410c24b5dfaSDave Chinner 	xfs_log_item_t	*lp;
411c24b5dfaSDave Chinner 
41295afcf5cSDave Chinner 	/* currently supports between 2 and 5 inodes */
41395afcf5cSDave Chinner 	ASSERT(ips && inodes >= 2 && inodes <= 5);
414c24b5dfaSDave Chinner 
415c24b5dfaSDave Chinner 	try_lock = 0;
416c24b5dfaSDave Chinner 	i = 0;
417c24b5dfaSDave Chinner again:
418c24b5dfaSDave Chinner 	for (; i < inodes; i++) {
419c24b5dfaSDave Chinner 		ASSERT(ips[i]);
420c24b5dfaSDave Chinner 
421c24b5dfaSDave Chinner 		if (i && (ips[i] == ips[i - 1]))	/* Already locked */
422c24b5dfaSDave Chinner 			continue;
423c24b5dfaSDave Chinner 
424c24b5dfaSDave Chinner 		/*
42595afcf5cSDave Chinner 		 * If try_lock is not set yet, make sure all locked inodes are
42695afcf5cSDave Chinner 		 * not in the AIL.  If any are, set try_lock to be used later.
427c24b5dfaSDave Chinner 		 */
428c24b5dfaSDave Chinner 		if (!try_lock) {
429c24b5dfaSDave Chinner 			for (j = (i - 1); j >= 0 && !try_lock; j--) {
430c24b5dfaSDave Chinner 				lp = (xfs_log_item_t *)ips[j]->i_itemp;
43195afcf5cSDave Chinner 				if (lp && (lp->li_flags & XFS_LI_IN_AIL))
432c24b5dfaSDave Chinner 					try_lock++;
433c24b5dfaSDave Chinner 			}
434c24b5dfaSDave Chinner 		}
435c24b5dfaSDave Chinner 
436c24b5dfaSDave Chinner 		/*
437c24b5dfaSDave Chinner 		 * If any of the previous locks we have locked is in the AIL,
438c24b5dfaSDave Chinner 		 * we must TRY to get the second and subsequent locks. If
439c24b5dfaSDave Chinner 		 * we can't get any, we must release all we have
440c24b5dfaSDave Chinner 		 * and try again.
441c24b5dfaSDave Chinner 		 */
44295afcf5cSDave Chinner 		if (!try_lock) {
44395afcf5cSDave Chinner 			xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
44495afcf5cSDave Chinner 			continue;
44595afcf5cSDave Chinner 		}
446c24b5dfaSDave Chinner 
44795afcf5cSDave Chinner 		/* try_lock means we have an inode locked that is in the AIL. */
448c24b5dfaSDave Chinner 		ASSERT(i != 0);
44995afcf5cSDave Chinner 		if (xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i)))
45095afcf5cSDave Chinner 			continue;
45195afcf5cSDave Chinner 
45295afcf5cSDave Chinner 		/*
45395afcf5cSDave Chinner 		 * Unlock all previous guys and try again.  xfs_iunlock will try
45495afcf5cSDave Chinner 		 * to push the tail if the inode is in the AIL.
45595afcf5cSDave Chinner 		 */
456c24b5dfaSDave Chinner 		attempts++;
457c24b5dfaSDave Chinner 		for (j = i - 1; j >= 0; j--) {
458c24b5dfaSDave Chinner 			/*
45995afcf5cSDave Chinner 			 * Check to see if we've already unlocked this one.  Not
46095afcf5cSDave Chinner 			 * the first one going back, and the inode ptr is the
46195afcf5cSDave Chinner 			 * same.
462c24b5dfaSDave Chinner 			 */
46395afcf5cSDave Chinner 			if (j != (i - 1) && ips[j] == ips[j + 1])
464c24b5dfaSDave Chinner 				continue;
465c24b5dfaSDave Chinner 
466c24b5dfaSDave Chinner 			xfs_iunlock(ips[j], lock_mode);
467c24b5dfaSDave Chinner 		}
468c24b5dfaSDave Chinner 
469c24b5dfaSDave Chinner 		if ((attempts % 5) == 0) {
470c24b5dfaSDave Chinner 			delay(1); /* Don't just spin the CPU */
471c24b5dfaSDave Chinner #ifdef DEBUG
472c24b5dfaSDave Chinner 			xfs_lock_delays++;
473c24b5dfaSDave Chinner #endif
474c24b5dfaSDave Chinner 		}
475c24b5dfaSDave Chinner 		i = 0;
476c24b5dfaSDave Chinner 		try_lock = 0;
477c24b5dfaSDave Chinner 		goto again;
478c24b5dfaSDave Chinner 	}
479c24b5dfaSDave Chinner 
480c24b5dfaSDave Chinner #ifdef DEBUG
481c24b5dfaSDave Chinner 	if (attempts) {
482c24b5dfaSDave Chinner 		if (attempts < 5) xfs_small_retries++;
483c24b5dfaSDave Chinner 		else if (attempts < 100) xfs_middle_retries++;
484c24b5dfaSDave Chinner 		else xfs_lots_retries++;
485c24b5dfaSDave Chinner 	} else {
486c24b5dfaSDave Chinner 		xfs_locked_n++;
487c24b5dfaSDave Chinner 	}
488c24b5dfaSDave Chinner #endif
489c24b5dfaSDave Chinner }
490c24b5dfaSDave Chinner 
491c24b5dfaSDave Chinner /*
492653c60b6SDave Chinner  * xfs_lock_two_inodes() can only be used to lock one type of lock at a time -
493653c60b6SDave Chinner  * the iolock, the mmaplock or the ilock, but not more than one at a time. If we
494653c60b6SDave Chinner  * lock more than one at a time, lockdep will report false positives saying we
495653c60b6SDave Chinner  * have violated locking orders.
496c24b5dfaSDave Chinner  */
497c24b5dfaSDave Chinner void
498c24b5dfaSDave Chinner xfs_lock_two_inodes(
499c24b5dfaSDave Chinner 	xfs_inode_t		*ip0,
500c24b5dfaSDave Chinner 	xfs_inode_t		*ip1,
501c24b5dfaSDave Chinner 	uint			lock_mode)
502c24b5dfaSDave Chinner {
503c24b5dfaSDave Chinner 	xfs_inode_t		*temp;
504c24b5dfaSDave Chinner 	int			attempts = 0;
505c24b5dfaSDave Chinner 	xfs_log_item_t		*lp;
506c24b5dfaSDave Chinner 
507653c60b6SDave Chinner 	if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) {
508653c60b6SDave Chinner 		ASSERT(!(lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)));
509653c60b6SDave Chinner 		ASSERT(!(lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
510653c60b6SDave Chinner 	} else if (lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL))
511653c60b6SDave Chinner 		ASSERT(!(lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
512653c60b6SDave Chinner 
513c24b5dfaSDave Chinner 	ASSERT(ip0->i_ino != ip1->i_ino);
514c24b5dfaSDave Chinner 
515c24b5dfaSDave Chinner 	if (ip0->i_ino > ip1->i_ino) {
516c24b5dfaSDave Chinner 		temp = ip0;
517c24b5dfaSDave Chinner 		ip0 = ip1;
518c24b5dfaSDave Chinner 		ip1 = temp;
519c24b5dfaSDave Chinner 	}
520c24b5dfaSDave Chinner 
521c24b5dfaSDave Chinner  again:
522c24b5dfaSDave Chinner 	xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
523c24b5dfaSDave Chinner 
524c24b5dfaSDave Chinner 	/*
525c24b5dfaSDave Chinner 	 * If the first lock we have locked is in the AIL, we must TRY to get
526c24b5dfaSDave Chinner 	 * the second lock. If we can't get it, we must release the first one
527c24b5dfaSDave Chinner 	 * and try again.
528c24b5dfaSDave Chinner 	 */
529c24b5dfaSDave Chinner 	lp = (xfs_log_item_t *)ip0->i_itemp;
530c24b5dfaSDave Chinner 	if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
531c24b5dfaSDave Chinner 		if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
532c24b5dfaSDave Chinner 			xfs_iunlock(ip0, lock_mode);
533c24b5dfaSDave Chinner 			if ((++attempts % 5) == 0)
534c24b5dfaSDave Chinner 				delay(1); /* Don't just spin the CPU */
535c24b5dfaSDave Chinner 			goto again;
536c24b5dfaSDave Chinner 		}
537c24b5dfaSDave Chinner 	} else {
538c24b5dfaSDave Chinner 		xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
539c24b5dfaSDave Chinner 	}
540c24b5dfaSDave Chinner }
541c24b5dfaSDave Chinner 
542c24b5dfaSDave Chinner 
543fa96acadSDave Chinner void
544fa96acadSDave Chinner __xfs_iflock(
545fa96acadSDave Chinner 	struct xfs_inode	*ip)
546fa96acadSDave Chinner {
547fa96acadSDave Chinner 	wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IFLOCK_BIT);
548fa96acadSDave Chinner 	DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IFLOCK_BIT);
549fa96acadSDave Chinner 
550fa96acadSDave Chinner 	do {
551fa96acadSDave Chinner 		prepare_to_wait_exclusive(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
552fa96acadSDave Chinner 		if (xfs_isiflocked(ip))
553fa96acadSDave Chinner 			io_schedule();
554fa96acadSDave Chinner 	} while (!xfs_iflock_nowait(ip));
555fa96acadSDave Chinner 
556fa96acadSDave Chinner 	finish_wait(wq, &wait.wait);
557fa96acadSDave Chinner }
558fa96acadSDave Chinner 
5591da177e4SLinus Torvalds STATIC uint
5601da177e4SLinus Torvalds _xfs_dic2xflags(
5611da177e4SLinus Torvalds 	__uint16_t		di_flags)
5621da177e4SLinus Torvalds {
5631da177e4SLinus Torvalds 	uint			flags = 0;
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	if (di_flags & XFS_DIFLAG_ANY) {
5661da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_REALTIME)
5671da177e4SLinus Torvalds 			flags |= XFS_XFLAG_REALTIME;
5681da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_PREALLOC)
5691da177e4SLinus Torvalds 			flags |= XFS_XFLAG_PREALLOC;
5701da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_IMMUTABLE)
5711da177e4SLinus Torvalds 			flags |= XFS_XFLAG_IMMUTABLE;
5721da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_APPEND)
5731da177e4SLinus Torvalds 			flags |= XFS_XFLAG_APPEND;
5741da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_SYNC)
5751da177e4SLinus Torvalds 			flags |= XFS_XFLAG_SYNC;
5761da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_NOATIME)
5771da177e4SLinus Torvalds 			flags |= XFS_XFLAG_NOATIME;
5781da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_NODUMP)
5791da177e4SLinus Torvalds 			flags |= XFS_XFLAG_NODUMP;
5801da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_RTINHERIT)
5811da177e4SLinus Torvalds 			flags |= XFS_XFLAG_RTINHERIT;
5821da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_PROJINHERIT)
5831da177e4SLinus Torvalds 			flags |= XFS_XFLAG_PROJINHERIT;
5841da177e4SLinus Torvalds 		if (di_flags & XFS_DIFLAG_NOSYMLINKS)
5851da177e4SLinus Torvalds 			flags |= XFS_XFLAG_NOSYMLINKS;
586dd9f438eSNathan Scott 		if (di_flags & XFS_DIFLAG_EXTSIZE)
587dd9f438eSNathan Scott 			flags |= XFS_XFLAG_EXTSIZE;
588dd9f438eSNathan Scott 		if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
589dd9f438eSNathan Scott 			flags |= XFS_XFLAG_EXTSZINHERIT;
590d3446eacSBarry Naujok 		if (di_flags & XFS_DIFLAG_NODEFRAG)
591d3446eacSBarry Naujok 			flags |= XFS_XFLAG_NODEFRAG;
5922a82b8beSDavid Chinner 		if (di_flags & XFS_DIFLAG_FILESTREAM)
5932a82b8beSDavid Chinner 			flags |= XFS_XFLAG_FILESTREAM;
5941da177e4SLinus Torvalds 	}
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 	return flags;
5971da177e4SLinus Torvalds }
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds uint
6001da177e4SLinus Torvalds xfs_ip2xflags(
6011da177e4SLinus Torvalds 	xfs_inode_t		*ip)
6021da177e4SLinus Torvalds {
603347d1c01SChristoph Hellwig 	xfs_icdinode_t		*dic = &ip->i_d;
6041da177e4SLinus Torvalds 
605a916e2bdSNathan Scott 	return _xfs_dic2xflags(dic->di_flags) |
60645ba598eSChristoph Hellwig 				(XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0);
6071da177e4SLinus Torvalds }
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds uint
6101da177e4SLinus Torvalds xfs_dic2xflags(
61145ba598eSChristoph Hellwig 	xfs_dinode_t		*dip)
6121da177e4SLinus Torvalds {
61381591fe2SChristoph Hellwig 	return _xfs_dic2xflags(be16_to_cpu(dip->di_flags)) |
61445ba598eSChristoph Hellwig 				(XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0);
6151da177e4SLinus Torvalds }
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds /*
618c24b5dfaSDave Chinner  * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
619c24b5dfaSDave Chinner  * is allowed, otherwise it has to be an exact match. If a CI match is found,
620c24b5dfaSDave Chinner  * ci_name->name will point to a the actual name (caller must free) or
621c24b5dfaSDave Chinner  * will be set to NULL if an exact match is found.
622c24b5dfaSDave Chinner  */
623c24b5dfaSDave Chinner int
624c24b5dfaSDave Chinner xfs_lookup(
625c24b5dfaSDave Chinner 	xfs_inode_t		*dp,
626c24b5dfaSDave Chinner 	struct xfs_name		*name,
627c24b5dfaSDave Chinner 	xfs_inode_t		**ipp,
628c24b5dfaSDave Chinner 	struct xfs_name		*ci_name)
629c24b5dfaSDave Chinner {
630c24b5dfaSDave Chinner 	xfs_ino_t		inum;
631c24b5dfaSDave Chinner 	int			error;
632c24b5dfaSDave Chinner 	uint			lock_mode;
633c24b5dfaSDave Chinner 
634c24b5dfaSDave Chinner 	trace_xfs_lookup(dp, name);
635c24b5dfaSDave Chinner 
636c24b5dfaSDave Chinner 	if (XFS_FORCED_SHUTDOWN(dp->i_mount))
6372451337dSDave Chinner 		return -EIO;
638c24b5dfaSDave Chinner 
639309ecac8SChristoph Hellwig 	lock_mode = xfs_ilock_data_map_shared(dp);
640c24b5dfaSDave Chinner 	error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
64101f4f327SChristoph Hellwig 	xfs_iunlock(dp, lock_mode);
642c24b5dfaSDave Chinner 
643c24b5dfaSDave Chinner 	if (error)
644c24b5dfaSDave Chinner 		goto out;
645c24b5dfaSDave Chinner 
646c24b5dfaSDave Chinner 	error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
647c24b5dfaSDave Chinner 	if (error)
648c24b5dfaSDave Chinner 		goto out_free_name;
649c24b5dfaSDave Chinner 
650c24b5dfaSDave Chinner 	return 0;
651c24b5dfaSDave Chinner 
652c24b5dfaSDave Chinner out_free_name:
653c24b5dfaSDave Chinner 	if (ci_name)
654c24b5dfaSDave Chinner 		kmem_free(ci_name->name);
655c24b5dfaSDave Chinner out:
656c24b5dfaSDave Chinner 	*ipp = NULL;
657c24b5dfaSDave Chinner 	return error;
658c24b5dfaSDave Chinner }
659c24b5dfaSDave Chinner 
660c24b5dfaSDave Chinner /*
6611da177e4SLinus Torvalds  * Allocate an inode on disk and return a copy of its in-core version.
6621da177e4SLinus Torvalds  * The in-core inode is locked exclusively.  Set mode, nlink, and rdev
6631da177e4SLinus Torvalds  * appropriately within the inode.  The uid and gid for the inode are
6641da177e4SLinus Torvalds  * set according to the contents of the given cred structure.
6651da177e4SLinus Torvalds  *
6661da177e4SLinus Torvalds  * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
667cd856db6SCarlos Maiolino  * has a free inode available, call xfs_iget() to obtain the in-core
668cd856db6SCarlos Maiolino  * version of the allocated inode.  Finally, fill in the inode and
669cd856db6SCarlos Maiolino  * log its initial contents.  In this case, ialloc_context would be
670cd856db6SCarlos Maiolino  * set to NULL.
6711da177e4SLinus Torvalds  *
672cd856db6SCarlos Maiolino  * If xfs_dialloc() does not have an available inode, it will replenish
673cd856db6SCarlos Maiolino  * its supply by doing an allocation. Since we can only do one
674cd856db6SCarlos Maiolino  * allocation within a transaction without deadlocks, we must commit
675cd856db6SCarlos Maiolino  * the current transaction before returning the inode itself.
676cd856db6SCarlos Maiolino  * In this case, therefore, we will set ialloc_context and return.
6771da177e4SLinus Torvalds  * The caller should then commit the current transaction, start a new
6781da177e4SLinus Torvalds  * transaction, and call xfs_ialloc() again to actually get the inode.
6791da177e4SLinus Torvalds  *
6801da177e4SLinus Torvalds  * To ensure that some other process does not grab the inode that
6811da177e4SLinus Torvalds  * was allocated during the first call to xfs_ialloc(), this routine
6821da177e4SLinus Torvalds  * also returns the [locked] bp pointing to the head of the freelist
6831da177e4SLinus Torvalds  * as ialloc_context.  The caller should hold this buffer across
6841da177e4SLinus Torvalds  * the commit and pass it back into this routine on the second call.
685b11f94d5SDavid Chinner  *
686b11f94d5SDavid Chinner  * If we are allocating quota inodes, we do not have a parent inode
687b11f94d5SDavid Chinner  * to attach to or associate with (i.e. pip == NULL) because they
688b11f94d5SDavid Chinner  * are not linked into the directory structure - they are attached
689b11f94d5SDavid Chinner  * directly to the superblock - and so have no parent.
6901da177e4SLinus Torvalds  */
6911da177e4SLinus Torvalds int
6921da177e4SLinus Torvalds xfs_ialloc(
6931da177e4SLinus Torvalds 	xfs_trans_t	*tp,
6941da177e4SLinus Torvalds 	xfs_inode_t	*pip,
695576b1d67SAl Viro 	umode_t		mode,
69631b084aeSNathan Scott 	xfs_nlink_t	nlink,
6971da177e4SLinus Torvalds 	xfs_dev_t	rdev,
6986743099cSArkadiusz Mi?kiewicz 	prid_t		prid,
6991da177e4SLinus Torvalds 	int		okalloc,
7001da177e4SLinus Torvalds 	xfs_buf_t	**ialloc_context,
7011da177e4SLinus Torvalds 	xfs_inode_t	**ipp)
7021da177e4SLinus Torvalds {
70393848a99SChristoph Hellwig 	struct xfs_mount *mp = tp->t_mountp;
7041da177e4SLinus Torvalds 	xfs_ino_t	ino;
7051da177e4SLinus Torvalds 	xfs_inode_t	*ip;
7061da177e4SLinus Torvalds 	uint		flags;
7071da177e4SLinus Torvalds 	int		error;
708e076b0f3SDave Chinner 	struct timespec	tv;
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 	/*
7111da177e4SLinus Torvalds 	 * Call the space management code to pick
7121da177e4SLinus Torvalds 	 * the on-disk inode to be allocated.
7131da177e4SLinus Torvalds 	 */
714b11f94d5SDavid Chinner 	error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
71508358906SChristoph Hellwig 			    ialloc_context, &ino);
716bf904248SDavid Chinner 	if (error)
7171da177e4SLinus Torvalds 		return error;
71808358906SChristoph Hellwig 	if (*ialloc_context || ino == NULLFSINO) {
7191da177e4SLinus Torvalds 		*ipp = NULL;
7201da177e4SLinus Torvalds 		return 0;
7211da177e4SLinus Torvalds 	}
7221da177e4SLinus Torvalds 	ASSERT(*ialloc_context == NULL);
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds 	/*
7251da177e4SLinus Torvalds 	 * Get the in-core inode with the lock held exclusively.
7261da177e4SLinus Torvalds 	 * This is because we're setting fields here we need
7271da177e4SLinus Torvalds 	 * to prevent others from looking at until we're done.
7281da177e4SLinus Torvalds 	 */
72993848a99SChristoph Hellwig 	error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE,
730ec3ba85fSChristoph Hellwig 			 XFS_ILOCK_EXCL, &ip);
731bf904248SDavid Chinner 	if (error)
7321da177e4SLinus Torvalds 		return error;
7331da177e4SLinus Torvalds 	ASSERT(ip != NULL);
7341da177e4SLinus Torvalds 
735263997a6SDave Chinner 	/*
736263997a6SDave Chinner 	 * We always convert v1 inodes to v2 now - we only support filesystems
737263997a6SDave Chinner 	 * with >= v2 inode capability, so there is no reason for ever leaving
738263997a6SDave Chinner 	 * an inode in v1 format.
739263997a6SDave Chinner 	 */
740263997a6SDave Chinner 	if (ip->i_d.di_version == 1)
741263997a6SDave Chinner 		ip->i_d.di_version = 2;
742263997a6SDave Chinner 
743576b1d67SAl Viro 	ip->i_d.di_mode = mode;
7441da177e4SLinus Torvalds 	ip->i_d.di_onlink = 0;
7451da177e4SLinus Torvalds 	ip->i_d.di_nlink = nlink;
7461da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_nlink == nlink);
7477aab1b28SDwight Engen 	ip->i_d.di_uid = xfs_kuid_to_uid(current_fsuid());
7487aab1b28SDwight Engen 	ip->i_d.di_gid = xfs_kgid_to_gid(current_fsgid());
7496743099cSArkadiusz Mi?kiewicz 	xfs_set_projid(ip, prid);
7501da177e4SLinus Torvalds 	memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
7511da177e4SLinus Torvalds 
752bd186aa9SChristoph Hellwig 	if (pip && XFS_INHERIT_GID(pip)) {
7531da177e4SLinus Torvalds 		ip->i_d.di_gid = pip->i_d.di_gid;
754abbede1bSAl Viro 		if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) {
7551da177e4SLinus Torvalds 			ip->i_d.di_mode |= S_ISGID;
7561da177e4SLinus Torvalds 		}
7571da177e4SLinus Torvalds 	}
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 	/*
7601da177e4SLinus Torvalds 	 * If the group ID of the new file does not match the effective group
7611da177e4SLinus Torvalds 	 * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
7621da177e4SLinus Torvalds 	 * (and only if the irix_sgid_inherit compatibility variable is set).
7631da177e4SLinus Torvalds 	 */
7641da177e4SLinus Torvalds 	if ((irix_sgid_inherit) &&
7651da177e4SLinus Torvalds 	    (ip->i_d.di_mode & S_ISGID) &&
7667aab1b28SDwight Engen 	    (!in_group_p(xfs_gid_to_kgid(ip->i_d.di_gid)))) {
7671da177e4SLinus Torvalds 		ip->i_d.di_mode &= ~S_ISGID;
7681da177e4SLinus Torvalds 	}
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds 	ip->i_d.di_size = 0;
7711da177e4SLinus Torvalds 	ip->i_d.di_nextents = 0;
7721da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_nblocks == 0);
773dff35fd4SChristoph Hellwig 
774e076b0f3SDave Chinner 	tv = current_fs_time(mp->m_super);
775dff35fd4SChristoph Hellwig 	ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
776dff35fd4SChristoph Hellwig 	ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
777dff35fd4SChristoph Hellwig 	ip->i_d.di_atime = ip->i_d.di_mtime;
778dff35fd4SChristoph Hellwig 	ip->i_d.di_ctime = ip->i_d.di_mtime;
779dff35fd4SChristoph Hellwig 
7801da177e4SLinus Torvalds 	/*
7811da177e4SLinus Torvalds 	 * di_gen will have been taken care of in xfs_iread.
7821da177e4SLinus Torvalds 	 */
7831da177e4SLinus Torvalds 	ip->i_d.di_extsize = 0;
7841da177e4SLinus Torvalds 	ip->i_d.di_dmevmask = 0;
7851da177e4SLinus Torvalds 	ip->i_d.di_dmstate = 0;
7861da177e4SLinus Torvalds 	ip->i_d.di_flags = 0;
78793848a99SChristoph Hellwig 
78893848a99SChristoph Hellwig 	if (ip->i_d.di_version == 3) {
78993848a99SChristoph Hellwig 		ASSERT(ip->i_d.di_ino == ino);
79093848a99SChristoph Hellwig 		ASSERT(uuid_equal(&ip->i_d.di_uuid, &mp->m_sb.sb_uuid));
79193848a99SChristoph Hellwig 		ip->i_d.di_crc = 0;
79293848a99SChristoph Hellwig 		ip->i_d.di_changecount = 1;
79393848a99SChristoph Hellwig 		ip->i_d.di_lsn = 0;
79493848a99SChristoph Hellwig 		ip->i_d.di_flags2 = 0;
79593848a99SChristoph Hellwig 		memset(&(ip->i_d.di_pad2[0]), 0, sizeof(ip->i_d.di_pad2));
79693848a99SChristoph Hellwig 		ip->i_d.di_crtime = ip->i_d.di_mtime;
79793848a99SChristoph Hellwig 	}
79893848a99SChristoph Hellwig 
79993848a99SChristoph Hellwig 
8001da177e4SLinus Torvalds 	flags = XFS_ILOG_CORE;
8011da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
8021da177e4SLinus Torvalds 	case S_IFIFO:
8031da177e4SLinus Torvalds 	case S_IFCHR:
8041da177e4SLinus Torvalds 	case S_IFBLK:
8051da177e4SLinus Torvalds 	case S_IFSOCK:
8061da177e4SLinus Torvalds 		ip->i_d.di_format = XFS_DINODE_FMT_DEV;
8071da177e4SLinus Torvalds 		ip->i_df.if_u2.if_rdev = rdev;
8081da177e4SLinus Torvalds 		ip->i_df.if_flags = 0;
8091da177e4SLinus Torvalds 		flags |= XFS_ILOG_DEV;
8101da177e4SLinus Torvalds 		break;
8111da177e4SLinus Torvalds 	case S_IFREG:
8121da177e4SLinus Torvalds 	case S_IFDIR:
813b11f94d5SDavid Chinner 		if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
814365ca83dSNathan Scott 			uint	di_flags = 0;
815365ca83dSNathan Scott 
816abbede1bSAl Viro 			if (S_ISDIR(mode)) {
817365ca83dSNathan Scott 				if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
818365ca83dSNathan Scott 					di_flags |= XFS_DIFLAG_RTINHERIT;
819dd9f438eSNathan Scott 				if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
820dd9f438eSNathan Scott 					di_flags |= XFS_DIFLAG_EXTSZINHERIT;
821dd9f438eSNathan Scott 					ip->i_d.di_extsize = pip->i_d.di_extsize;
822dd9f438eSNathan Scott 				}
8239336e3a7SDave Chinner 				if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
8249336e3a7SDave Chinner 					di_flags |= XFS_DIFLAG_PROJINHERIT;
825abbede1bSAl Viro 			} else if (S_ISREG(mode)) {
826613d7043SChristoph Hellwig 				if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
827365ca83dSNathan Scott 					di_flags |= XFS_DIFLAG_REALTIME;
828dd9f438eSNathan Scott 				if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
829dd9f438eSNathan Scott 					di_flags |= XFS_DIFLAG_EXTSIZE;
830dd9f438eSNathan Scott 					ip->i_d.di_extsize = pip->i_d.di_extsize;
831dd9f438eSNathan Scott 				}
8321da177e4SLinus Torvalds 			}
8331da177e4SLinus Torvalds 			if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
8341da177e4SLinus Torvalds 			    xfs_inherit_noatime)
835365ca83dSNathan Scott 				di_flags |= XFS_DIFLAG_NOATIME;
8361da177e4SLinus Torvalds 			if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
8371da177e4SLinus Torvalds 			    xfs_inherit_nodump)
838365ca83dSNathan Scott 				di_flags |= XFS_DIFLAG_NODUMP;
8391da177e4SLinus Torvalds 			if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
8401da177e4SLinus Torvalds 			    xfs_inherit_sync)
841365ca83dSNathan Scott 				di_flags |= XFS_DIFLAG_SYNC;
8421da177e4SLinus Torvalds 			if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
8431da177e4SLinus Torvalds 			    xfs_inherit_nosymlinks)
844365ca83dSNathan Scott 				di_flags |= XFS_DIFLAG_NOSYMLINKS;
845d3446eacSBarry Naujok 			if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
846d3446eacSBarry Naujok 			    xfs_inherit_nodefrag)
847d3446eacSBarry Naujok 				di_flags |= XFS_DIFLAG_NODEFRAG;
8482a82b8beSDavid Chinner 			if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
8492a82b8beSDavid Chinner 				di_flags |= XFS_DIFLAG_FILESTREAM;
850365ca83dSNathan Scott 			ip->i_d.di_flags |= di_flags;
8511da177e4SLinus Torvalds 		}
8521da177e4SLinus Torvalds 		/* FALLTHROUGH */
8531da177e4SLinus Torvalds 	case S_IFLNK:
8541da177e4SLinus Torvalds 		ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
8551da177e4SLinus Torvalds 		ip->i_df.if_flags = XFS_IFEXTENTS;
8561da177e4SLinus Torvalds 		ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
8571da177e4SLinus Torvalds 		ip->i_df.if_u1.if_extents = NULL;
8581da177e4SLinus Torvalds 		break;
8591da177e4SLinus Torvalds 	default:
8601da177e4SLinus Torvalds 		ASSERT(0);
8611da177e4SLinus Torvalds 	}
8621da177e4SLinus Torvalds 	/*
8631da177e4SLinus Torvalds 	 * Attribute fork settings for new inode.
8641da177e4SLinus Torvalds 	 */
8651da177e4SLinus Torvalds 	ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
8661da177e4SLinus Torvalds 	ip->i_d.di_anextents = 0;
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds 	/*
8691da177e4SLinus Torvalds 	 * Log the new values stuffed into the inode.
8701da177e4SLinus Torvalds 	 */
871ddc3415aSChristoph Hellwig 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
8721da177e4SLinus Torvalds 	xfs_trans_log_inode(tp, ip, flags);
8731da177e4SLinus Torvalds 
87458c90473SDave Chinner 	/* now that we have an i_mode we can setup the inode structure */
87541be8bedSChristoph Hellwig 	xfs_setup_inode(ip);
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 	*ipp = ip;
8781da177e4SLinus Torvalds 	return 0;
8791da177e4SLinus Torvalds }
8801da177e4SLinus Torvalds 
881e546cb79SDave Chinner /*
882e546cb79SDave Chinner  * Allocates a new inode from disk and return a pointer to the
883e546cb79SDave Chinner  * incore copy. This routine will internally commit the current
884e546cb79SDave Chinner  * transaction and allocate a new one if the Space Manager needed
885e546cb79SDave Chinner  * to do an allocation to replenish the inode free-list.
886e546cb79SDave Chinner  *
887e546cb79SDave Chinner  * This routine is designed to be called from xfs_create and
888e546cb79SDave Chinner  * xfs_create_dir.
889e546cb79SDave Chinner  *
890e546cb79SDave Chinner  */
891e546cb79SDave Chinner int
892e546cb79SDave Chinner xfs_dir_ialloc(
893e546cb79SDave Chinner 	xfs_trans_t	**tpp,		/* input: current transaction;
894e546cb79SDave Chinner 					   output: may be a new transaction. */
895e546cb79SDave Chinner 	xfs_inode_t	*dp,		/* directory within whose allocate
896e546cb79SDave Chinner 					   the inode. */
897e546cb79SDave Chinner 	umode_t		mode,
898e546cb79SDave Chinner 	xfs_nlink_t	nlink,
899e546cb79SDave Chinner 	xfs_dev_t	rdev,
900e546cb79SDave Chinner 	prid_t		prid,		/* project id */
901e546cb79SDave Chinner 	int		okalloc,	/* ok to allocate new space */
902e546cb79SDave Chinner 	xfs_inode_t	**ipp,		/* pointer to inode; it will be
903e546cb79SDave Chinner 					   locked. */
904e546cb79SDave Chinner 	int		*committed)
905e546cb79SDave Chinner 
906e546cb79SDave Chinner {
907e546cb79SDave Chinner 	xfs_trans_t	*tp;
908e546cb79SDave Chinner 	xfs_inode_t	*ip;
909e546cb79SDave Chinner 	xfs_buf_t	*ialloc_context = NULL;
910e546cb79SDave Chinner 	int		code;
911e546cb79SDave Chinner 	void		*dqinfo;
912e546cb79SDave Chinner 	uint		tflags;
913e546cb79SDave Chinner 
914e546cb79SDave Chinner 	tp = *tpp;
915e546cb79SDave Chinner 	ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
916e546cb79SDave Chinner 
917e546cb79SDave Chinner 	/*
918e546cb79SDave Chinner 	 * xfs_ialloc will return a pointer to an incore inode if
919e546cb79SDave Chinner 	 * the Space Manager has an available inode on the free
920e546cb79SDave Chinner 	 * list. Otherwise, it will do an allocation and replenish
921e546cb79SDave Chinner 	 * the freelist.  Since we can only do one allocation per
922e546cb79SDave Chinner 	 * transaction without deadlocks, we will need to commit the
923e546cb79SDave Chinner 	 * current transaction and start a new one.  We will then
924e546cb79SDave Chinner 	 * need to call xfs_ialloc again to get the inode.
925e546cb79SDave Chinner 	 *
926e546cb79SDave Chinner 	 * If xfs_ialloc did an allocation to replenish the freelist,
927e546cb79SDave Chinner 	 * it returns the bp containing the head of the freelist as
928e546cb79SDave Chinner 	 * ialloc_context. We will hold a lock on it across the
929e546cb79SDave Chinner 	 * transaction commit so that no other process can steal
930e546cb79SDave Chinner 	 * the inode(s) that we've just allocated.
931e546cb79SDave Chinner 	 */
932e546cb79SDave Chinner 	code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid, okalloc,
933e546cb79SDave Chinner 			  &ialloc_context, &ip);
934e546cb79SDave Chinner 
935e546cb79SDave Chinner 	/*
936e546cb79SDave Chinner 	 * Return an error if we were unable to allocate a new inode.
937e546cb79SDave Chinner 	 * This should only happen if we run out of space on disk or
938e546cb79SDave Chinner 	 * encounter a disk error.
939e546cb79SDave Chinner 	 */
940e546cb79SDave Chinner 	if (code) {
941e546cb79SDave Chinner 		*ipp = NULL;
942e546cb79SDave Chinner 		return code;
943e546cb79SDave Chinner 	}
944e546cb79SDave Chinner 	if (!ialloc_context && !ip) {
945e546cb79SDave Chinner 		*ipp = NULL;
9462451337dSDave Chinner 		return -ENOSPC;
947e546cb79SDave Chinner 	}
948e546cb79SDave Chinner 
949e546cb79SDave Chinner 	/*
950e546cb79SDave Chinner 	 * If the AGI buffer is non-NULL, then we were unable to get an
951e546cb79SDave Chinner 	 * inode in one operation.  We need to commit the current
952e546cb79SDave Chinner 	 * transaction and call xfs_ialloc() again.  It is guaranteed
953e546cb79SDave Chinner 	 * to succeed the second time.
954e546cb79SDave Chinner 	 */
955e546cb79SDave Chinner 	if (ialloc_context) {
956e546cb79SDave Chinner 		/*
957e546cb79SDave Chinner 		 * Normally, xfs_trans_commit releases all the locks.
958e546cb79SDave Chinner 		 * We call bhold to hang on to the ialloc_context across
959e546cb79SDave Chinner 		 * the commit.  Holding this buffer prevents any other
960e546cb79SDave Chinner 		 * processes from doing any allocations in this
961e546cb79SDave Chinner 		 * allocation group.
962e546cb79SDave Chinner 		 */
963e546cb79SDave Chinner 		xfs_trans_bhold(tp, ialloc_context);
964e546cb79SDave Chinner 
965e546cb79SDave Chinner 		/*
966e546cb79SDave Chinner 		 * We want the quota changes to be associated with the next
967e546cb79SDave Chinner 		 * transaction, NOT this one. So, detach the dqinfo from this
968e546cb79SDave Chinner 		 * and attach it to the next transaction.
969e546cb79SDave Chinner 		 */
970e546cb79SDave Chinner 		dqinfo = NULL;
971e546cb79SDave Chinner 		tflags = 0;
972e546cb79SDave Chinner 		if (tp->t_dqinfo) {
973e546cb79SDave Chinner 			dqinfo = (void *)tp->t_dqinfo;
974e546cb79SDave Chinner 			tp->t_dqinfo = NULL;
975e546cb79SDave Chinner 			tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY;
976e546cb79SDave Chinner 			tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY);
977e546cb79SDave Chinner 		}
978e546cb79SDave Chinner 
9792e6db6c4SChristoph Hellwig 		code = xfs_trans_roll(&tp, 0);
9802e6db6c4SChristoph Hellwig 		if (committed != NULL)
981e546cb79SDave Chinner 			*committed = 1;
9823d3c8b52SJie Liu 
983e546cb79SDave Chinner 		/*
984e546cb79SDave Chinner 		 * Re-attach the quota info that we detached from prev trx.
985e546cb79SDave Chinner 		 */
986e546cb79SDave Chinner 		if (dqinfo) {
987e546cb79SDave Chinner 			tp->t_dqinfo = dqinfo;
988e546cb79SDave Chinner 			tp->t_flags |= tflags;
989e546cb79SDave Chinner 		}
990e546cb79SDave Chinner 
991e546cb79SDave Chinner 		if (code) {
992e546cb79SDave Chinner 			xfs_buf_relse(ialloc_context);
9932e6db6c4SChristoph Hellwig 			*tpp = tp;
994e546cb79SDave Chinner 			*ipp = NULL;
995e546cb79SDave Chinner 			return code;
996e546cb79SDave Chinner 		}
997e546cb79SDave Chinner 		xfs_trans_bjoin(tp, ialloc_context);
998e546cb79SDave Chinner 
999e546cb79SDave Chinner 		/*
1000e546cb79SDave Chinner 		 * Call ialloc again. Since we've locked out all
1001e546cb79SDave Chinner 		 * other allocations in this allocation group,
1002e546cb79SDave Chinner 		 * this call should always succeed.
1003e546cb79SDave Chinner 		 */
1004e546cb79SDave Chinner 		code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid,
1005e546cb79SDave Chinner 				  okalloc, &ialloc_context, &ip);
1006e546cb79SDave Chinner 
1007e546cb79SDave Chinner 		/*
1008e546cb79SDave Chinner 		 * If we get an error at this point, return to the caller
1009e546cb79SDave Chinner 		 * so that the current transaction can be aborted.
1010e546cb79SDave Chinner 		 */
1011e546cb79SDave Chinner 		if (code) {
1012e546cb79SDave Chinner 			*tpp = tp;
1013e546cb79SDave Chinner 			*ipp = NULL;
1014e546cb79SDave Chinner 			return code;
1015e546cb79SDave Chinner 		}
1016e546cb79SDave Chinner 		ASSERT(!ialloc_context && ip);
1017e546cb79SDave Chinner 
1018e546cb79SDave Chinner 	} else {
1019e546cb79SDave Chinner 		if (committed != NULL)
1020e546cb79SDave Chinner 			*committed = 0;
1021e546cb79SDave Chinner 	}
1022e546cb79SDave Chinner 
1023e546cb79SDave Chinner 	*ipp = ip;
1024e546cb79SDave Chinner 	*tpp = tp;
1025e546cb79SDave Chinner 
1026e546cb79SDave Chinner 	return 0;
1027e546cb79SDave Chinner }
1028e546cb79SDave Chinner 
1029e546cb79SDave Chinner /*
1030e546cb79SDave Chinner  * Decrement the link count on an inode & log the change.
1031e546cb79SDave Chinner  * If this causes the link count to go to zero, initiate the
1032e546cb79SDave Chinner  * logging activity required to truncate a file.
1033e546cb79SDave Chinner  */
1034e546cb79SDave Chinner int				/* error */
1035e546cb79SDave Chinner xfs_droplink(
1036e546cb79SDave Chinner 	xfs_trans_t *tp,
1037e546cb79SDave Chinner 	xfs_inode_t *ip)
1038e546cb79SDave Chinner {
1039e546cb79SDave Chinner 	int	error;
1040e546cb79SDave Chinner 
1041e546cb79SDave Chinner 	xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1042e546cb79SDave Chinner 
1043e546cb79SDave Chinner 	ASSERT (ip->i_d.di_nlink > 0);
1044e546cb79SDave Chinner 	ip->i_d.di_nlink--;
1045e546cb79SDave Chinner 	drop_nlink(VFS_I(ip));
1046e546cb79SDave Chinner 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1047e546cb79SDave Chinner 
1048e546cb79SDave Chinner 	error = 0;
1049e546cb79SDave Chinner 	if (ip->i_d.di_nlink == 0) {
1050e546cb79SDave Chinner 		/*
1051e546cb79SDave Chinner 		 * We're dropping the last link to this file.
1052e546cb79SDave Chinner 		 * Move the on-disk inode to the AGI unlinked list.
1053e546cb79SDave Chinner 		 * From xfs_inactive() we will pull the inode from
1054e546cb79SDave Chinner 		 * the list and free it.
1055e546cb79SDave Chinner 		 */
1056e546cb79SDave Chinner 		error = xfs_iunlink(tp, ip);
1057e546cb79SDave Chinner 	}
1058e546cb79SDave Chinner 	return error;
1059e546cb79SDave Chinner }
1060e546cb79SDave Chinner 
1061e546cb79SDave Chinner /*
1062e546cb79SDave Chinner  * Increment the link count on an inode & log the change.
1063e546cb79SDave Chinner  */
1064e546cb79SDave Chinner int
1065e546cb79SDave Chinner xfs_bumplink(
1066e546cb79SDave Chinner 	xfs_trans_t *tp,
1067e546cb79SDave Chinner 	xfs_inode_t *ip)
1068e546cb79SDave Chinner {
1069e546cb79SDave Chinner 	xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1070e546cb79SDave Chinner 
1071263997a6SDave Chinner 	ASSERT(ip->i_d.di_version > 1);
1072ab297431SZhi Yong Wu 	ASSERT(ip->i_d.di_nlink > 0 || (VFS_I(ip)->i_state & I_LINKABLE));
1073e546cb79SDave Chinner 	ip->i_d.di_nlink++;
1074e546cb79SDave Chinner 	inc_nlink(VFS_I(ip));
1075e546cb79SDave Chinner 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1076e546cb79SDave Chinner 	return 0;
1077e546cb79SDave Chinner }
1078e546cb79SDave Chinner 
1079c24b5dfaSDave Chinner int
1080c24b5dfaSDave Chinner xfs_create(
1081c24b5dfaSDave Chinner 	xfs_inode_t		*dp,
1082c24b5dfaSDave Chinner 	struct xfs_name		*name,
1083c24b5dfaSDave Chinner 	umode_t			mode,
1084c24b5dfaSDave Chinner 	xfs_dev_t		rdev,
1085c24b5dfaSDave Chinner 	xfs_inode_t		**ipp)
1086c24b5dfaSDave Chinner {
1087c24b5dfaSDave Chinner 	int			is_dir = S_ISDIR(mode);
1088c24b5dfaSDave Chinner 	struct xfs_mount	*mp = dp->i_mount;
1089c24b5dfaSDave Chinner 	struct xfs_inode	*ip = NULL;
1090c24b5dfaSDave Chinner 	struct xfs_trans	*tp = NULL;
1091c24b5dfaSDave Chinner 	int			error;
1092c24b5dfaSDave Chinner 	xfs_bmap_free_t		free_list;
1093c24b5dfaSDave Chinner 	xfs_fsblock_t		first_block;
1094c24b5dfaSDave Chinner 	bool                    unlock_dp_on_error = false;
1095c24b5dfaSDave Chinner 	int			committed;
1096c24b5dfaSDave Chinner 	prid_t			prid;
1097c24b5dfaSDave Chinner 	struct xfs_dquot	*udqp = NULL;
1098c24b5dfaSDave Chinner 	struct xfs_dquot	*gdqp = NULL;
1099c24b5dfaSDave Chinner 	struct xfs_dquot	*pdqp = NULL;
1100062647a8SBrian Foster 	struct xfs_trans_res	*tres;
1101c24b5dfaSDave Chinner 	uint			resblks;
1102c24b5dfaSDave Chinner 
1103c24b5dfaSDave Chinner 	trace_xfs_create(dp, name);
1104c24b5dfaSDave Chinner 
1105c24b5dfaSDave Chinner 	if (XFS_FORCED_SHUTDOWN(mp))
11062451337dSDave Chinner 		return -EIO;
1107c24b5dfaSDave Chinner 
1108163467d3SZhi Yong Wu 	prid = xfs_get_initial_prid(dp);
1109c24b5dfaSDave Chinner 
1110c24b5dfaSDave Chinner 	/*
1111c24b5dfaSDave Chinner 	 * Make sure that we have allocated dquot(s) on disk.
1112c24b5dfaSDave Chinner 	 */
11137aab1b28SDwight Engen 	error = xfs_qm_vop_dqalloc(dp, xfs_kuid_to_uid(current_fsuid()),
11147aab1b28SDwight Engen 					xfs_kgid_to_gid(current_fsgid()), prid,
1115c24b5dfaSDave Chinner 					XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
1116c24b5dfaSDave Chinner 					&udqp, &gdqp, &pdqp);
1117c24b5dfaSDave Chinner 	if (error)
1118c24b5dfaSDave Chinner 		return error;
1119c24b5dfaSDave Chinner 
1120c24b5dfaSDave Chinner 	if (is_dir) {
1121c24b5dfaSDave Chinner 		rdev = 0;
1122c24b5dfaSDave Chinner 		resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
1123062647a8SBrian Foster 		tres = &M_RES(mp)->tr_mkdir;
1124c24b5dfaSDave Chinner 		tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
1125c24b5dfaSDave Chinner 	} else {
1126c24b5dfaSDave Chinner 		resblks = XFS_CREATE_SPACE_RES(mp, name->len);
1127062647a8SBrian Foster 		tres = &M_RES(mp)->tr_create;
1128c24b5dfaSDave Chinner 		tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1129c24b5dfaSDave Chinner 	}
1130c24b5dfaSDave Chinner 
1131c24b5dfaSDave Chinner 	/*
1132c24b5dfaSDave Chinner 	 * Initially assume that the file does not exist and
1133c24b5dfaSDave Chinner 	 * reserve the resources for that case.  If that is not
1134c24b5dfaSDave Chinner 	 * the case we'll drop the one we have and get a more
1135c24b5dfaSDave Chinner 	 * appropriate transaction later.
1136c24b5dfaSDave Chinner 	 */
1137062647a8SBrian Foster 	error = xfs_trans_reserve(tp, tres, resblks, 0);
11382451337dSDave Chinner 	if (error == -ENOSPC) {
1139c24b5dfaSDave Chinner 		/* flush outstanding delalloc blocks and retry */
1140c24b5dfaSDave Chinner 		xfs_flush_inodes(mp);
1141062647a8SBrian Foster 		error = xfs_trans_reserve(tp, tres, resblks, 0);
1142c24b5dfaSDave Chinner 	}
11432451337dSDave Chinner 	if (error == -ENOSPC) {
1144c24b5dfaSDave Chinner 		/* No space at all so try a "no-allocation" reservation */
1145c24b5dfaSDave Chinner 		resblks = 0;
1146062647a8SBrian Foster 		error = xfs_trans_reserve(tp, tres, 0, 0);
1147c24b5dfaSDave Chinner 	}
11484906e215SChristoph Hellwig 	if (error)
1149c24b5dfaSDave Chinner 		goto out_trans_cancel;
11504906e215SChristoph Hellwig 
1151c24b5dfaSDave Chinner 
1152c24b5dfaSDave Chinner 	xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1153c24b5dfaSDave Chinner 	unlock_dp_on_error = true;
1154c24b5dfaSDave Chinner 
1155c24b5dfaSDave Chinner 	xfs_bmap_init(&free_list, &first_block);
1156c24b5dfaSDave Chinner 
1157c24b5dfaSDave Chinner 	/*
1158c24b5dfaSDave Chinner 	 * Reserve disk quota and the inode.
1159c24b5dfaSDave Chinner 	 */
1160c24b5dfaSDave Chinner 	error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
1161c24b5dfaSDave Chinner 						pdqp, resblks, 1, 0);
1162c24b5dfaSDave Chinner 	if (error)
1163c24b5dfaSDave Chinner 		goto out_trans_cancel;
1164c24b5dfaSDave Chinner 
116594f3cad5SEric Sandeen 	if (!resblks) {
116694f3cad5SEric Sandeen 		error = xfs_dir_canenter(tp, dp, name);
1167c24b5dfaSDave Chinner 		if (error)
1168c24b5dfaSDave Chinner 			goto out_trans_cancel;
116994f3cad5SEric Sandeen 	}
1170c24b5dfaSDave Chinner 
1171c24b5dfaSDave Chinner 	/*
1172c24b5dfaSDave Chinner 	 * A newly created regular or special file just has one directory
1173c24b5dfaSDave Chinner 	 * entry pointing to them, but a directory also the "." entry
1174c24b5dfaSDave Chinner 	 * pointing to itself.
1175c24b5dfaSDave Chinner 	 */
1176c24b5dfaSDave Chinner 	error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev,
1177c24b5dfaSDave Chinner 			       prid, resblks > 0, &ip, &committed);
1178*d6077aa3SJan Kara 	if (error)
1179c24b5dfaSDave Chinner 		goto out_trans_cancel;
1180c24b5dfaSDave Chinner 
1181c24b5dfaSDave Chinner 	/*
1182c24b5dfaSDave Chinner 	 * Now we join the directory inode to the transaction.  We do not do it
1183c24b5dfaSDave Chinner 	 * earlier because xfs_dir_ialloc might commit the previous transaction
1184c24b5dfaSDave Chinner 	 * (and release all the locks).  An error from here on will result in
1185c24b5dfaSDave Chinner 	 * the transaction cancel unlocking dp so don't do it explicitly in the
1186c24b5dfaSDave Chinner 	 * error path.
1187c24b5dfaSDave Chinner 	 */
1188c24b5dfaSDave Chinner 	xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1189c24b5dfaSDave Chinner 	unlock_dp_on_error = false;
1190c24b5dfaSDave Chinner 
1191c24b5dfaSDave Chinner 	error = xfs_dir_createname(tp, dp, name, ip->i_ino,
1192c24b5dfaSDave Chinner 					&first_block, &free_list, resblks ?
1193c24b5dfaSDave Chinner 					resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
1194c24b5dfaSDave Chinner 	if (error) {
11952451337dSDave Chinner 		ASSERT(error != -ENOSPC);
11964906e215SChristoph Hellwig 		goto out_trans_cancel;
1197c24b5dfaSDave Chinner 	}
1198c24b5dfaSDave Chinner 	xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1199c24b5dfaSDave Chinner 	xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1200c24b5dfaSDave Chinner 
1201c24b5dfaSDave Chinner 	if (is_dir) {
1202c24b5dfaSDave Chinner 		error = xfs_dir_init(tp, ip, dp);
1203c24b5dfaSDave Chinner 		if (error)
1204c24b5dfaSDave Chinner 			goto out_bmap_cancel;
1205c24b5dfaSDave Chinner 
1206c24b5dfaSDave Chinner 		error = xfs_bumplink(tp, dp);
1207c24b5dfaSDave Chinner 		if (error)
1208c24b5dfaSDave Chinner 			goto out_bmap_cancel;
1209c24b5dfaSDave Chinner 	}
1210c24b5dfaSDave Chinner 
1211c24b5dfaSDave Chinner 	/*
1212c24b5dfaSDave Chinner 	 * If this is a synchronous mount, make sure that the
1213c24b5dfaSDave Chinner 	 * create transaction goes to disk before returning to
1214c24b5dfaSDave Chinner 	 * the user.
1215c24b5dfaSDave Chinner 	 */
1216c24b5dfaSDave Chinner 	if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1217c24b5dfaSDave Chinner 		xfs_trans_set_sync(tp);
1218c24b5dfaSDave Chinner 
1219c24b5dfaSDave Chinner 	/*
1220c24b5dfaSDave Chinner 	 * Attach the dquot(s) to the inodes and modify them incore.
1221c24b5dfaSDave Chinner 	 * These ids of the inode couldn't have changed since the new
1222c24b5dfaSDave Chinner 	 * inode has been locked ever since it was created.
1223c24b5dfaSDave Chinner 	 */
1224c24b5dfaSDave Chinner 	xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
1225c24b5dfaSDave Chinner 
1226c24b5dfaSDave Chinner 	error = xfs_bmap_finish(&tp, &free_list, &committed);
1227c24b5dfaSDave Chinner 	if (error)
1228c24b5dfaSDave Chinner 		goto out_bmap_cancel;
1229c24b5dfaSDave Chinner 
123070393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
1231c24b5dfaSDave Chinner 	if (error)
1232c24b5dfaSDave Chinner 		goto out_release_inode;
1233c24b5dfaSDave Chinner 
1234c24b5dfaSDave Chinner 	xfs_qm_dqrele(udqp);
1235c24b5dfaSDave Chinner 	xfs_qm_dqrele(gdqp);
1236c24b5dfaSDave Chinner 	xfs_qm_dqrele(pdqp);
1237c24b5dfaSDave Chinner 
1238c24b5dfaSDave Chinner 	*ipp = ip;
1239c24b5dfaSDave Chinner 	return 0;
1240c24b5dfaSDave Chinner 
1241c24b5dfaSDave Chinner  out_bmap_cancel:
1242c24b5dfaSDave Chinner 	xfs_bmap_cancel(&free_list);
1243c24b5dfaSDave Chinner  out_trans_cancel:
12444906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
1245c24b5dfaSDave Chinner  out_release_inode:
1246c24b5dfaSDave Chinner 	/*
124758c90473SDave Chinner 	 * Wait until after the current transaction is aborted to finish the
124858c90473SDave Chinner 	 * setup of the inode and release the inode.  This prevents recursive
124958c90473SDave Chinner 	 * transactions and deadlocks from xfs_inactive.
1250c24b5dfaSDave Chinner 	 */
125158c90473SDave Chinner 	if (ip) {
125258c90473SDave Chinner 		xfs_finish_inode_setup(ip);
1253c24b5dfaSDave Chinner 		IRELE(ip);
125458c90473SDave Chinner 	}
1255c24b5dfaSDave Chinner 
1256c24b5dfaSDave Chinner 	xfs_qm_dqrele(udqp);
1257c24b5dfaSDave Chinner 	xfs_qm_dqrele(gdqp);
1258c24b5dfaSDave Chinner 	xfs_qm_dqrele(pdqp);
1259c24b5dfaSDave Chinner 
1260c24b5dfaSDave Chinner 	if (unlock_dp_on_error)
1261c24b5dfaSDave Chinner 		xfs_iunlock(dp, XFS_ILOCK_EXCL);
1262c24b5dfaSDave Chinner 	return error;
1263c24b5dfaSDave Chinner }
1264c24b5dfaSDave Chinner 
1265c24b5dfaSDave Chinner int
126699b6436bSZhi Yong Wu xfs_create_tmpfile(
126799b6436bSZhi Yong Wu 	struct xfs_inode	*dp,
126899b6436bSZhi Yong Wu 	struct dentry		*dentry,
1269330033d6SBrian Foster 	umode_t			mode,
1270330033d6SBrian Foster 	struct xfs_inode	**ipp)
127199b6436bSZhi Yong Wu {
127299b6436bSZhi Yong Wu 	struct xfs_mount	*mp = dp->i_mount;
127399b6436bSZhi Yong Wu 	struct xfs_inode	*ip = NULL;
127499b6436bSZhi Yong Wu 	struct xfs_trans	*tp = NULL;
127599b6436bSZhi Yong Wu 	int			error;
127699b6436bSZhi Yong Wu 	prid_t                  prid;
127799b6436bSZhi Yong Wu 	struct xfs_dquot	*udqp = NULL;
127899b6436bSZhi Yong Wu 	struct xfs_dquot	*gdqp = NULL;
127999b6436bSZhi Yong Wu 	struct xfs_dquot	*pdqp = NULL;
128099b6436bSZhi Yong Wu 	struct xfs_trans_res	*tres;
128199b6436bSZhi Yong Wu 	uint			resblks;
128299b6436bSZhi Yong Wu 
128399b6436bSZhi Yong Wu 	if (XFS_FORCED_SHUTDOWN(mp))
12842451337dSDave Chinner 		return -EIO;
128599b6436bSZhi Yong Wu 
128699b6436bSZhi Yong Wu 	prid = xfs_get_initial_prid(dp);
128799b6436bSZhi Yong Wu 
128899b6436bSZhi Yong Wu 	/*
128999b6436bSZhi Yong Wu 	 * Make sure that we have allocated dquot(s) on disk.
129099b6436bSZhi Yong Wu 	 */
129199b6436bSZhi Yong Wu 	error = xfs_qm_vop_dqalloc(dp, xfs_kuid_to_uid(current_fsuid()),
129299b6436bSZhi Yong Wu 				xfs_kgid_to_gid(current_fsgid()), prid,
129399b6436bSZhi Yong Wu 				XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
129499b6436bSZhi Yong Wu 				&udqp, &gdqp, &pdqp);
129599b6436bSZhi Yong Wu 	if (error)
129699b6436bSZhi Yong Wu 		return error;
129799b6436bSZhi Yong Wu 
129899b6436bSZhi Yong Wu 	resblks = XFS_IALLOC_SPACE_RES(mp);
129999b6436bSZhi Yong Wu 	tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE_TMPFILE);
130099b6436bSZhi Yong Wu 
130199b6436bSZhi Yong Wu 	tres = &M_RES(mp)->tr_create_tmpfile;
130299b6436bSZhi Yong Wu 	error = xfs_trans_reserve(tp, tres, resblks, 0);
13032451337dSDave Chinner 	if (error == -ENOSPC) {
130499b6436bSZhi Yong Wu 		/* No space at all so try a "no-allocation" reservation */
130599b6436bSZhi Yong Wu 		resblks = 0;
130699b6436bSZhi Yong Wu 		error = xfs_trans_reserve(tp, tres, 0, 0);
130799b6436bSZhi Yong Wu 	}
13084906e215SChristoph Hellwig 	if (error)
130999b6436bSZhi Yong Wu 		goto out_trans_cancel;
131099b6436bSZhi Yong Wu 
131199b6436bSZhi Yong Wu 	error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
131299b6436bSZhi Yong Wu 						pdqp, resblks, 1, 0);
131399b6436bSZhi Yong Wu 	if (error)
131499b6436bSZhi Yong Wu 		goto out_trans_cancel;
131599b6436bSZhi Yong Wu 
131699b6436bSZhi Yong Wu 	error = xfs_dir_ialloc(&tp, dp, mode, 1, 0,
131799b6436bSZhi Yong Wu 				prid, resblks > 0, &ip, NULL);
1318*d6077aa3SJan Kara 	if (error)
131999b6436bSZhi Yong Wu 		goto out_trans_cancel;
132099b6436bSZhi Yong Wu 
132199b6436bSZhi Yong Wu 	if (mp->m_flags & XFS_MOUNT_WSYNC)
132299b6436bSZhi Yong Wu 		xfs_trans_set_sync(tp);
132399b6436bSZhi Yong Wu 
132499b6436bSZhi Yong Wu 	/*
132599b6436bSZhi Yong Wu 	 * Attach the dquot(s) to the inodes and modify them incore.
132699b6436bSZhi Yong Wu 	 * These ids of the inode couldn't have changed since the new
132799b6436bSZhi Yong Wu 	 * inode has been locked ever since it was created.
132899b6436bSZhi Yong Wu 	 */
132999b6436bSZhi Yong Wu 	xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
133099b6436bSZhi Yong Wu 
133199b6436bSZhi Yong Wu 	ip->i_d.di_nlink--;
133299b6436bSZhi Yong Wu 	error = xfs_iunlink(tp, ip);
133399b6436bSZhi Yong Wu 	if (error)
13344906e215SChristoph Hellwig 		goto out_trans_cancel;
133599b6436bSZhi Yong Wu 
133670393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
133799b6436bSZhi Yong Wu 	if (error)
133899b6436bSZhi Yong Wu 		goto out_release_inode;
133999b6436bSZhi Yong Wu 
134099b6436bSZhi Yong Wu 	xfs_qm_dqrele(udqp);
134199b6436bSZhi Yong Wu 	xfs_qm_dqrele(gdqp);
134299b6436bSZhi Yong Wu 	xfs_qm_dqrele(pdqp);
134399b6436bSZhi Yong Wu 
1344330033d6SBrian Foster 	*ipp = ip;
134599b6436bSZhi Yong Wu 	return 0;
134699b6436bSZhi Yong Wu 
134799b6436bSZhi Yong Wu  out_trans_cancel:
13484906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
134999b6436bSZhi Yong Wu  out_release_inode:
135099b6436bSZhi Yong Wu 	/*
135158c90473SDave Chinner 	 * Wait until after the current transaction is aborted to finish the
135258c90473SDave Chinner 	 * setup of the inode and release the inode.  This prevents recursive
135358c90473SDave Chinner 	 * transactions and deadlocks from xfs_inactive.
135499b6436bSZhi Yong Wu 	 */
135558c90473SDave Chinner 	if (ip) {
135658c90473SDave Chinner 		xfs_finish_inode_setup(ip);
135799b6436bSZhi Yong Wu 		IRELE(ip);
135858c90473SDave Chinner 	}
135999b6436bSZhi Yong Wu 
136099b6436bSZhi Yong Wu 	xfs_qm_dqrele(udqp);
136199b6436bSZhi Yong Wu 	xfs_qm_dqrele(gdqp);
136299b6436bSZhi Yong Wu 	xfs_qm_dqrele(pdqp);
136399b6436bSZhi Yong Wu 
136499b6436bSZhi Yong Wu 	return error;
136599b6436bSZhi Yong Wu }
136699b6436bSZhi Yong Wu 
136799b6436bSZhi Yong Wu int
1368c24b5dfaSDave Chinner xfs_link(
1369c24b5dfaSDave Chinner 	xfs_inode_t		*tdp,
1370c24b5dfaSDave Chinner 	xfs_inode_t		*sip,
1371c24b5dfaSDave Chinner 	struct xfs_name		*target_name)
1372c24b5dfaSDave Chinner {
1373c24b5dfaSDave Chinner 	xfs_mount_t		*mp = tdp->i_mount;
1374c24b5dfaSDave Chinner 	xfs_trans_t		*tp;
1375c24b5dfaSDave Chinner 	int			error;
1376c24b5dfaSDave Chinner 	xfs_bmap_free_t         free_list;
1377c24b5dfaSDave Chinner 	xfs_fsblock_t           first_block;
1378c24b5dfaSDave Chinner 	int			committed;
1379c24b5dfaSDave Chinner 	int			resblks;
1380c24b5dfaSDave Chinner 
1381c24b5dfaSDave Chinner 	trace_xfs_link(tdp, target_name);
1382c24b5dfaSDave Chinner 
1383c24b5dfaSDave Chinner 	ASSERT(!S_ISDIR(sip->i_d.di_mode));
1384c24b5dfaSDave Chinner 
1385c24b5dfaSDave Chinner 	if (XFS_FORCED_SHUTDOWN(mp))
13862451337dSDave Chinner 		return -EIO;
1387c24b5dfaSDave Chinner 
1388c24b5dfaSDave Chinner 	error = xfs_qm_dqattach(sip, 0);
1389c24b5dfaSDave Chinner 	if (error)
1390c24b5dfaSDave Chinner 		goto std_return;
1391c24b5dfaSDave Chinner 
1392c24b5dfaSDave Chinner 	error = xfs_qm_dqattach(tdp, 0);
1393c24b5dfaSDave Chinner 	if (error)
1394c24b5dfaSDave Chinner 		goto std_return;
1395c24b5dfaSDave Chinner 
1396c24b5dfaSDave Chinner 	tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
1397c24b5dfaSDave Chinner 	resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
13983d3c8b52SJie Liu 	error = xfs_trans_reserve(tp, &M_RES(mp)->tr_link, resblks, 0);
13992451337dSDave Chinner 	if (error == -ENOSPC) {
1400c24b5dfaSDave Chinner 		resblks = 0;
14013d3c8b52SJie Liu 		error = xfs_trans_reserve(tp, &M_RES(mp)->tr_link, 0, 0);
1402c24b5dfaSDave Chinner 	}
14034906e215SChristoph Hellwig 	if (error)
1404c24b5dfaSDave Chinner 		goto error_return;
1405c24b5dfaSDave Chinner 
1406c24b5dfaSDave Chinner 	xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
1407c24b5dfaSDave Chinner 
1408c24b5dfaSDave Chinner 	xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
1409c24b5dfaSDave Chinner 	xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
1410c24b5dfaSDave Chinner 
1411c24b5dfaSDave Chinner 	/*
1412c24b5dfaSDave Chinner 	 * If we are using project inheritance, we only allow hard link
1413c24b5dfaSDave Chinner 	 * creation in our tree when the project IDs are the same; else
1414c24b5dfaSDave Chinner 	 * the tree quota mechanism could be circumvented.
1415c24b5dfaSDave Chinner 	 */
1416c24b5dfaSDave Chinner 	if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
1417c24b5dfaSDave Chinner 		     (xfs_get_projid(tdp) != xfs_get_projid(sip)))) {
14182451337dSDave Chinner 		error = -EXDEV;
1419c24b5dfaSDave Chinner 		goto error_return;
1420c24b5dfaSDave Chinner 	}
1421c24b5dfaSDave Chinner 
142294f3cad5SEric Sandeen 	if (!resblks) {
142394f3cad5SEric Sandeen 		error = xfs_dir_canenter(tp, tdp, target_name);
1424c24b5dfaSDave Chinner 		if (error)
1425c24b5dfaSDave Chinner 			goto error_return;
142694f3cad5SEric Sandeen 	}
1427c24b5dfaSDave Chinner 
1428c24b5dfaSDave Chinner 	xfs_bmap_init(&free_list, &first_block);
1429c24b5dfaSDave Chinner 
1430ab297431SZhi Yong Wu 	if (sip->i_d.di_nlink == 0) {
1431ab297431SZhi Yong Wu 		error = xfs_iunlink_remove(tp, sip);
1432ab297431SZhi Yong Wu 		if (error)
14334906e215SChristoph Hellwig 			goto error_return;
1434ab297431SZhi Yong Wu 	}
1435ab297431SZhi Yong Wu 
1436c24b5dfaSDave Chinner 	error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
1437c24b5dfaSDave Chinner 					&first_block, &free_list, resblks);
1438c24b5dfaSDave Chinner 	if (error)
14394906e215SChristoph Hellwig 		goto error_return;
1440c24b5dfaSDave Chinner 	xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1441c24b5dfaSDave Chinner 	xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
1442c24b5dfaSDave Chinner 
1443c24b5dfaSDave Chinner 	error = xfs_bumplink(tp, sip);
1444c24b5dfaSDave Chinner 	if (error)
14454906e215SChristoph Hellwig 		goto error_return;
1446c24b5dfaSDave Chinner 
1447c24b5dfaSDave Chinner 	/*
1448c24b5dfaSDave Chinner 	 * If this is a synchronous mount, make sure that the
1449c24b5dfaSDave Chinner 	 * link transaction goes to disk before returning to
1450c24b5dfaSDave Chinner 	 * the user.
1451c24b5dfaSDave Chinner 	 */
1452c24b5dfaSDave Chinner 	if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1453c24b5dfaSDave Chinner 		xfs_trans_set_sync(tp);
1454c24b5dfaSDave Chinner 	}
1455c24b5dfaSDave Chinner 
1456c24b5dfaSDave Chinner 	error = xfs_bmap_finish (&tp, &free_list, &committed);
1457c24b5dfaSDave Chinner 	if (error) {
1458c24b5dfaSDave Chinner 		xfs_bmap_cancel(&free_list);
14594906e215SChristoph Hellwig 		goto error_return;
1460c24b5dfaSDave Chinner 	}
1461c24b5dfaSDave Chinner 
146270393313SChristoph Hellwig 	return xfs_trans_commit(tp);
1463c24b5dfaSDave Chinner 
1464c24b5dfaSDave Chinner  error_return:
14654906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
1466c24b5dfaSDave Chinner  std_return:
1467c24b5dfaSDave Chinner 	return error;
1468c24b5dfaSDave Chinner }
1469c24b5dfaSDave Chinner 
14701da177e4SLinus Torvalds /*
14718f04c47aSChristoph Hellwig  * Free up the underlying blocks past new_size.  The new size must be smaller
14728f04c47aSChristoph Hellwig  * than the current size.  This routine can be used both for the attribute and
14738f04c47aSChristoph Hellwig  * data fork, and does not modify the inode size, which is left to the caller.
14741da177e4SLinus Torvalds  *
1475f6485057SDavid Chinner  * The transaction passed to this routine must have made a permanent log
1476f6485057SDavid Chinner  * reservation of at least XFS_ITRUNCATE_LOG_RES.  This routine may commit the
1477f6485057SDavid Chinner  * given transaction and start new ones, so make sure everything involved in
1478f6485057SDavid Chinner  * the transaction is tidy before calling here.  Some transaction will be
1479f6485057SDavid Chinner  * returned to the caller to be committed.  The incoming transaction must
1480f6485057SDavid Chinner  * already include the inode, and both inode locks must be held exclusively.
1481f6485057SDavid Chinner  * The inode must also be "held" within the transaction.  On return the inode
1482f6485057SDavid Chinner  * will be "held" within the returned transaction.  This routine does NOT
1483f6485057SDavid Chinner  * require any disk space to be reserved for it within the transaction.
14841da177e4SLinus Torvalds  *
1485f6485057SDavid Chinner  * If we get an error, we must return with the inode locked and linked into the
1486f6485057SDavid Chinner  * current transaction. This keeps things simple for the higher level code,
1487f6485057SDavid Chinner  * because it always knows that the inode is locked and held in the transaction
1488f6485057SDavid Chinner  * that returns to it whether errors occur or not.  We don't mark the inode
1489f6485057SDavid Chinner  * dirty on error so that transactions can be easily aborted if possible.
14901da177e4SLinus Torvalds  */
14911da177e4SLinus Torvalds int
14928f04c47aSChristoph Hellwig xfs_itruncate_extents(
14938f04c47aSChristoph Hellwig 	struct xfs_trans	**tpp,
14948f04c47aSChristoph Hellwig 	struct xfs_inode	*ip,
14958f04c47aSChristoph Hellwig 	int			whichfork,
14968f04c47aSChristoph Hellwig 	xfs_fsize_t		new_size)
14971da177e4SLinus Torvalds {
14988f04c47aSChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
14998f04c47aSChristoph Hellwig 	struct xfs_trans	*tp = *tpp;
15008f04c47aSChristoph Hellwig 	xfs_bmap_free_t		free_list;
15011da177e4SLinus Torvalds 	xfs_fsblock_t		first_block;
15021da177e4SLinus Torvalds 	xfs_fileoff_t		first_unmap_block;
15031da177e4SLinus Torvalds 	xfs_fileoff_t		last_block;
15048f04c47aSChristoph Hellwig 	xfs_filblks_t		unmap_len;
15051da177e4SLinus Torvalds 	int			committed;
15068f04c47aSChristoph Hellwig 	int			error = 0;
15078f04c47aSChristoph Hellwig 	int			done = 0;
15081da177e4SLinus Torvalds 
15090b56185bSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
15100b56185bSChristoph Hellwig 	ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
15110b56185bSChristoph Hellwig 	       xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1512ce7ae151SChristoph Hellwig 	ASSERT(new_size <= XFS_ISIZE(ip));
15138f04c47aSChristoph Hellwig 	ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
15141da177e4SLinus Torvalds 	ASSERT(ip->i_itemp != NULL);
1515898621d5SChristoph Hellwig 	ASSERT(ip->i_itemp->ili_lock_flags == 0);
15161da177e4SLinus Torvalds 	ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
15171da177e4SLinus Torvalds 
1518673e8e59SChristoph Hellwig 	trace_xfs_itruncate_extents_start(ip, new_size);
1519673e8e59SChristoph Hellwig 
15201da177e4SLinus Torvalds 	/*
15211da177e4SLinus Torvalds 	 * Since it is possible for space to become allocated beyond
15221da177e4SLinus Torvalds 	 * the end of the file (in a crash where the space is allocated
15231da177e4SLinus Torvalds 	 * but the inode size is not yet updated), simply remove any
15241da177e4SLinus Torvalds 	 * blocks which show up between the new EOF and the maximum
15251da177e4SLinus Torvalds 	 * possible file size.  If the first block to be removed is
15261da177e4SLinus Torvalds 	 * beyond the maximum file size (ie it is the same as last_block),
15271da177e4SLinus Torvalds 	 * then there is nothing to do.
15281da177e4SLinus Torvalds 	 */
15298f04c47aSChristoph Hellwig 	first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
153032972383SDave Chinner 	last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
15318f04c47aSChristoph Hellwig 	if (first_unmap_block == last_block)
15328f04c47aSChristoph Hellwig 		return 0;
15338f04c47aSChristoph Hellwig 
15348f04c47aSChristoph Hellwig 	ASSERT(first_unmap_block < last_block);
15351da177e4SLinus Torvalds 	unmap_len = last_block - first_unmap_block + 1;
15361da177e4SLinus Torvalds 	while (!done) {
15379d87c319SEric Sandeen 		xfs_bmap_init(&free_list, &first_block);
15388f04c47aSChristoph Hellwig 		error = xfs_bunmapi(tp, ip,
15393e57ecf6SOlaf Weber 				    first_unmap_block, unmap_len,
15408f04c47aSChristoph Hellwig 				    xfs_bmapi_aflag(whichfork),
15411da177e4SLinus Torvalds 				    XFS_ITRUNC_MAX_EXTENTS,
15423e57ecf6SOlaf Weber 				    &first_block, &free_list,
1543b4e9181eSChristoph Hellwig 				    &done);
15448f04c47aSChristoph Hellwig 		if (error)
15458f04c47aSChristoph Hellwig 			goto out_bmap_cancel;
15461da177e4SLinus Torvalds 
15471da177e4SLinus Torvalds 		/*
15481da177e4SLinus Torvalds 		 * Duplicate the transaction that has the permanent
15491da177e4SLinus Torvalds 		 * reservation and commit the old transaction.
15501da177e4SLinus Torvalds 		 */
15518f04c47aSChristoph Hellwig 		error = xfs_bmap_finish(&tp, &free_list, &committed);
1552898621d5SChristoph Hellwig 		if (committed)
1553ddc3415aSChristoph Hellwig 			xfs_trans_ijoin(tp, ip, 0);
15548f04c47aSChristoph Hellwig 		if (error)
15558f04c47aSChristoph Hellwig 			goto out_bmap_cancel;
15561da177e4SLinus Torvalds 
15572e6db6c4SChristoph Hellwig 		error = xfs_trans_roll(&tp, ip);
15581da177e4SLinus Torvalds 		if (error)
15598f04c47aSChristoph Hellwig 			goto out;
15601da177e4SLinus Torvalds 	}
15618f04c47aSChristoph Hellwig 
1562673e8e59SChristoph Hellwig 	/*
1563673e8e59SChristoph Hellwig 	 * Always re-log the inode so that our permanent transaction can keep
1564673e8e59SChristoph Hellwig 	 * on rolling it forward in the log.
1565673e8e59SChristoph Hellwig 	 */
1566673e8e59SChristoph Hellwig 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1567673e8e59SChristoph Hellwig 
1568673e8e59SChristoph Hellwig 	trace_xfs_itruncate_extents_end(ip, new_size);
1569673e8e59SChristoph Hellwig 
15708f04c47aSChristoph Hellwig out:
15718f04c47aSChristoph Hellwig 	*tpp = tp;
15728f04c47aSChristoph Hellwig 	return error;
15738f04c47aSChristoph Hellwig out_bmap_cancel:
15741da177e4SLinus Torvalds 	/*
15758f04c47aSChristoph Hellwig 	 * If the bunmapi call encounters an error, return to the caller where
15768f04c47aSChristoph Hellwig 	 * the transaction can be properly aborted.  We just need to make sure
15778f04c47aSChristoph Hellwig 	 * we're not holding any resources that we were not when we came in.
15781da177e4SLinus Torvalds 	 */
15798f04c47aSChristoph Hellwig 	xfs_bmap_cancel(&free_list);
15808f04c47aSChristoph Hellwig 	goto out;
15818f04c47aSChristoph Hellwig }
15828f04c47aSChristoph Hellwig 
1583c24b5dfaSDave Chinner int
1584c24b5dfaSDave Chinner xfs_release(
1585c24b5dfaSDave Chinner 	xfs_inode_t	*ip)
1586c24b5dfaSDave Chinner {
1587c24b5dfaSDave Chinner 	xfs_mount_t	*mp = ip->i_mount;
1588c24b5dfaSDave Chinner 	int		error;
1589c24b5dfaSDave Chinner 
1590c24b5dfaSDave Chinner 	if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
1591c24b5dfaSDave Chinner 		return 0;
1592c24b5dfaSDave Chinner 
1593c24b5dfaSDave Chinner 	/* If this is a read-only mount, don't do this (would generate I/O) */
1594c24b5dfaSDave Chinner 	if (mp->m_flags & XFS_MOUNT_RDONLY)
1595c24b5dfaSDave Chinner 		return 0;
1596c24b5dfaSDave Chinner 
1597c24b5dfaSDave Chinner 	if (!XFS_FORCED_SHUTDOWN(mp)) {
1598c24b5dfaSDave Chinner 		int truncated;
1599c24b5dfaSDave Chinner 
1600c24b5dfaSDave Chinner 		/*
1601c24b5dfaSDave Chinner 		 * If we previously truncated this file and removed old data
1602c24b5dfaSDave Chinner 		 * in the process, we want to initiate "early" writeout on
1603c24b5dfaSDave Chinner 		 * the last close.  This is an attempt to combat the notorious
1604c24b5dfaSDave Chinner 		 * NULL files problem which is particularly noticeable from a
1605c24b5dfaSDave Chinner 		 * truncate down, buffered (re-)write (delalloc), followed by
1606c24b5dfaSDave Chinner 		 * a crash.  What we are effectively doing here is
1607c24b5dfaSDave Chinner 		 * significantly reducing the time window where we'd otherwise
1608c24b5dfaSDave Chinner 		 * be exposed to that problem.
1609c24b5dfaSDave Chinner 		 */
1610c24b5dfaSDave Chinner 		truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
1611c24b5dfaSDave Chinner 		if (truncated) {
1612c24b5dfaSDave Chinner 			xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
1613eac152b4SDave Chinner 			if (ip->i_delayed_blks > 0) {
16142451337dSDave Chinner 				error = filemap_flush(VFS_I(ip)->i_mapping);
1615c24b5dfaSDave Chinner 				if (error)
1616c24b5dfaSDave Chinner 					return error;
1617c24b5dfaSDave Chinner 			}
1618c24b5dfaSDave Chinner 		}
1619c24b5dfaSDave Chinner 	}
1620c24b5dfaSDave Chinner 
1621c24b5dfaSDave Chinner 	if (ip->i_d.di_nlink == 0)
1622c24b5dfaSDave Chinner 		return 0;
1623c24b5dfaSDave Chinner 
1624c24b5dfaSDave Chinner 	if (xfs_can_free_eofblocks(ip, false)) {
1625c24b5dfaSDave Chinner 
1626c24b5dfaSDave Chinner 		/*
1627c24b5dfaSDave Chinner 		 * If we can't get the iolock just skip truncating the blocks
1628c24b5dfaSDave Chinner 		 * past EOF because we could deadlock with the mmap_sem
1629c24b5dfaSDave Chinner 		 * otherwise.  We'll get another chance to drop them once the
1630c24b5dfaSDave Chinner 		 * last reference to the inode is dropped, so we'll never leak
1631c24b5dfaSDave Chinner 		 * blocks permanently.
1632c24b5dfaSDave Chinner 		 *
1633c24b5dfaSDave Chinner 		 * Further, check if the inode is being opened, written and
1634c24b5dfaSDave Chinner 		 * closed frequently and we have delayed allocation blocks
1635c24b5dfaSDave Chinner 		 * outstanding (e.g. streaming writes from the NFS server),
1636c24b5dfaSDave Chinner 		 * truncating the blocks past EOF will cause fragmentation to
1637c24b5dfaSDave Chinner 		 * occur.
1638c24b5dfaSDave Chinner 		 *
1639c24b5dfaSDave Chinner 		 * In this case don't do the truncation, either, but we have to
1640c24b5dfaSDave Chinner 		 * be careful how we detect this case. Blocks beyond EOF show
1641c24b5dfaSDave Chinner 		 * up as i_delayed_blks even when the inode is clean, so we
1642c24b5dfaSDave Chinner 		 * need to truncate them away first before checking for a dirty
1643c24b5dfaSDave Chinner 		 * release. Hence on the first dirty close we will still remove
1644c24b5dfaSDave Chinner 		 * the speculative allocation, but after that we will leave it
1645c24b5dfaSDave Chinner 		 * in place.
1646c24b5dfaSDave Chinner 		 */
1647c24b5dfaSDave Chinner 		if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
1648c24b5dfaSDave Chinner 			return 0;
1649c24b5dfaSDave Chinner 
1650c24b5dfaSDave Chinner 		error = xfs_free_eofblocks(mp, ip, true);
16512451337dSDave Chinner 		if (error && error != -EAGAIN)
1652c24b5dfaSDave Chinner 			return error;
1653c24b5dfaSDave Chinner 
1654c24b5dfaSDave Chinner 		/* delalloc blocks after truncation means it really is dirty */
1655c24b5dfaSDave Chinner 		if (ip->i_delayed_blks)
1656c24b5dfaSDave Chinner 			xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
1657c24b5dfaSDave Chinner 	}
1658c24b5dfaSDave Chinner 	return 0;
1659c24b5dfaSDave Chinner }
1660c24b5dfaSDave Chinner 
1661c24b5dfaSDave Chinner /*
1662f7be2d7fSBrian Foster  * xfs_inactive_truncate
1663f7be2d7fSBrian Foster  *
1664f7be2d7fSBrian Foster  * Called to perform a truncate when an inode becomes unlinked.
1665f7be2d7fSBrian Foster  */
1666f7be2d7fSBrian Foster STATIC int
1667f7be2d7fSBrian Foster xfs_inactive_truncate(
1668f7be2d7fSBrian Foster 	struct xfs_inode *ip)
1669f7be2d7fSBrian Foster {
1670f7be2d7fSBrian Foster 	struct xfs_mount	*mp = ip->i_mount;
1671f7be2d7fSBrian Foster 	struct xfs_trans	*tp;
1672f7be2d7fSBrian Foster 	int			error;
1673f7be2d7fSBrian Foster 
1674f7be2d7fSBrian Foster 	tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1675f7be2d7fSBrian Foster 	error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
1676f7be2d7fSBrian Foster 	if (error) {
1677f7be2d7fSBrian Foster 		ASSERT(XFS_FORCED_SHUTDOWN(mp));
16784906e215SChristoph Hellwig 		xfs_trans_cancel(tp);
1679f7be2d7fSBrian Foster 		return error;
1680f7be2d7fSBrian Foster 	}
1681f7be2d7fSBrian Foster 
1682f7be2d7fSBrian Foster 	xfs_ilock(ip, XFS_ILOCK_EXCL);
1683f7be2d7fSBrian Foster 	xfs_trans_ijoin(tp, ip, 0);
1684f7be2d7fSBrian Foster 
1685f7be2d7fSBrian Foster 	/*
1686f7be2d7fSBrian Foster 	 * Log the inode size first to prevent stale data exposure in the event
1687f7be2d7fSBrian Foster 	 * of a system crash before the truncate completes. See the related
1688f7be2d7fSBrian Foster 	 * comment in xfs_setattr_size() for details.
1689f7be2d7fSBrian Foster 	 */
1690f7be2d7fSBrian Foster 	ip->i_d.di_size = 0;
1691f7be2d7fSBrian Foster 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1692f7be2d7fSBrian Foster 
1693f7be2d7fSBrian Foster 	error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
1694f7be2d7fSBrian Foster 	if (error)
1695f7be2d7fSBrian Foster 		goto error_trans_cancel;
1696f7be2d7fSBrian Foster 
1697f7be2d7fSBrian Foster 	ASSERT(ip->i_d.di_nextents == 0);
1698f7be2d7fSBrian Foster 
169970393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
1700f7be2d7fSBrian Foster 	if (error)
1701f7be2d7fSBrian Foster 		goto error_unlock;
1702f7be2d7fSBrian Foster 
1703f7be2d7fSBrian Foster 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
1704f7be2d7fSBrian Foster 	return 0;
1705f7be2d7fSBrian Foster 
1706f7be2d7fSBrian Foster error_trans_cancel:
17074906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
1708f7be2d7fSBrian Foster error_unlock:
1709f7be2d7fSBrian Foster 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
1710f7be2d7fSBrian Foster 	return error;
1711f7be2d7fSBrian Foster }
1712f7be2d7fSBrian Foster 
1713f7be2d7fSBrian Foster /*
171488877d2bSBrian Foster  * xfs_inactive_ifree()
171588877d2bSBrian Foster  *
171688877d2bSBrian Foster  * Perform the inode free when an inode is unlinked.
171788877d2bSBrian Foster  */
171888877d2bSBrian Foster STATIC int
171988877d2bSBrian Foster xfs_inactive_ifree(
172088877d2bSBrian Foster 	struct xfs_inode *ip)
172188877d2bSBrian Foster {
172288877d2bSBrian Foster 	xfs_bmap_free_t		free_list;
172388877d2bSBrian Foster 	xfs_fsblock_t		first_block;
172488877d2bSBrian Foster 	int			committed;
172588877d2bSBrian Foster 	struct xfs_mount	*mp = ip->i_mount;
172688877d2bSBrian Foster 	struct xfs_trans	*tp;
172788877d2bSBrian Foster 	int			error;
172888877d2bSBrian Foster 
172988877d2bSBrian Foster 	tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
17309d43b180SBrian Foster 
17319d43b180SBrian Foster 	/*
17329d43b180SBrian Foster 	 * The ifree transaction might need to allocate blocks for record
17339d43b180SBrian Foster 	 * insertion to the finobt. We don't want to fail here at ENOSPC, so
17349d43b180SBrian Foster 	 * allow ifree to dip into the reserved block pool if necessary.
17359d43b180SBrian Foster 	 *
17369d43b180SBrian Foster 	 * Freeing large sets of inodes generally means freeing inode chunks,
17379d43b180SBrian Foster 	 * directory and file data blocks, so this should be relatively safe.
17389d43b180SBrian Foster 	 * Only under severe circumstances should it be possible to free enough
17399d43b180SBrian Foster 	 * inodes to exhaust the reserve block pool via finobt expansion while
17409d43b180SBrian Foster 	 * at the same time not creating free space in the filesystem.
17419d43b180SBrian Foster 	 *
17429d43b180SBrian Foster 	 * Send a warning if the reservation does happen to fail, as the inode
17439d43b180SBrian Foster 	 * now remains allocated and sits on the unlinked list until the fs is
17449d43b180SBrian Foster 	 * repaired.
17459d43b180SBrian Foster 	 */
17469d43b180SBrian Foster 	tp->t_flags |= XFS_TRANS_RESERVE;
17479d43b180SBrian Foster 	error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ifree,
17489d43b180SBrian Foster 				  XFS_IFREE_SPACE_RES(mp), 0);
174988877d2bSBrian Foster 	if (error) {
17502451337dSDave Chinner 		if (error == -ENOSPC) {
17519d43b180SBrian Foster 			xfs_warn_ratelimited(mp,
17529d43b180SBrian Foster 			"Failed to remove inode(s) from unlinked list. "
17539d43b180SBrian Foster 			"Please free space, unmount and run xfs_repair.");
17549d43b180SBrian Foster 		} else {
175588877d2bSBrian Foster 			ASSERT(XFS_FORCED_SHUTDOWN(mp));
17569d43b180SBrian Foster 		}
17574906e215SChristoph Hellwig 		xfs_trans_cancel(tp);
175888877d2bSBrian Foster 		return error;
175988877d2bSBrian Foster 	}
176088877d2bSBrian Foster 
176188877d2bSBrian Foster 	xfs_ilock(ip, XFS_ILOCK_EXCL);
176288877d2bSBrian Foster 	xfs_trans_ijoin(tp, ip, 0);
176388877d2bSBrian Foster 
176488877d2bSBrian Foster 	xfs_bmap_init(&free_list, &first_block);
176588877d2bSBrian Foster 	error = xfs_ifree(tp, ip, &free_list);
176688877d2bSBrian Foster 	if (error) {
176788877d2bSBrian Foster 		/*
176888877d2bSBrian Foster 		 * If we fail to free the inode, shut down.  The cancel
176988877d2bSBrian Foster 		 * might do that, we need to make sure.  Otherwise the
177088877d2bSBrian Foster 		 * inode might be lost for a long time or forever.
177188877d2bSBrian Foster 		 */
177288877d2bSBrian Foster 		if (!XFS_FORCED_SHUTDOWN(mp)) {
177388877d2bSBrian Foster 			xfs_notice(mp, "%s: xfs_ifree returned error %d",
177488877d2bSBrian Foster 				__func__, error);
177588877d2bSBrian Foster 			xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
177688877d2bSBrian Foster 		}
17774906e215SChristoph Hellwig 		xfs_trans_cancel(tp);
177888877d2bSBrian Foster 		xfs_iunlock(ip, XFS_ILOCK_EXCL);
177988877d2bSBrian Foster 		return error;
178088877d2bSBrian Foster 	}
178188877d2bSBrian Foster 
178288877d2bSBrian Foster 	/*
178388877d2bSBrian Foster 	 * Credit the quota account(s). The inode is gone.
178488877d2bSBrian Foster 	 */
178588877d2bSBrian Foster 	xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
178688877d2bSBrian Foster 
178788877d2bSBrian Foster 	/*
178888877d2bSBrian Foster 	 * Just ignore errors at this point.  There is nothing we can
178988877d2bSBrian Foster 	 * do except to try to keep going. Make sure it's not a silent
179088877d2bSBrian Foster 	 * error.
179188877d2bSBrian Foster 	 */
179288877d2bSBrian Foster 	error = xfs_bmap_finish(&tp,  &free_list, &committed);
179388877d2bSBrian Foster 	if (error)
179488877d2bSBrian Foster 		xfs_notice(mp, "%s: xfs_bmap_finish returned error %d",
179588877d2bSBrian Foster 			__func__, error);
179670393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
179788877d2bSBrian Foster 	if (error)
179888877d2bSBrian Foster 		xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
179988877d2bSBrian Foster 			__func__, error);
180088877d2bSBrian Foster 
180188877d2bSBrian Foster 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
180288877d2bSBrian Foster 	return 0;
180388877d2bSBrian Foster }
180488877d2bSBrian Foster 
180588877d2bSBrian Foster /*
1806c24b5dfaSDave Chinner  * xfs_inactive
1807c24b5dfaSDave Chinner  *
1808c24b5dfaSDave Chinner  * This is called when the vnode reference count for the vnode
1809c24b5dfaSDave Chinner  * goes to zero.  If the file has been unlinked, then it must
1810c24b5dfaSDave Chinner  * now be truncated.  Also, we clear all of the read-ahead state
1811c24b5dfaSDave Chinner  * kept for the inode here since the file is now closed.
1812c24b5dfaSDave Chinner  */
181374564fb4SBrian Foster void
1814c24b5dfaSDave Chinner xfs_inactive(
1815c24b5dfaSDave Chinner 	xfs_inode_t	*ip)
1816c24b5dfaSDave Chinner {
18173d3c8b52SJie Liu 	struct xfs_mount	*mp;
1818c24b5dfaSDave Chinner 	int			error;
1819c24b5dfaSDave Chinner 	int			truncate = 0;
1820c24b5dfaSDave Chinner 
1821c24b5dfaSDave Chinner 	/*
1822c24b5dfaSDave Chinner 	 * If the inode is already free, then there can be nothing
1823c24b5dfaSDave Chinner 	 * to clean up here.
1824c24b5dfaSDave Chinner 	 */
1825d948709bSBen Myers 	if (ip->i_d.di_mode == 0) {
1826c24b5dfaSDave Chinner 		ASSERT(ip->i_df.if_real_bytes == 0);
1827c24b5dfaSDave Chinner 		ASSERT(ip->i_df.if_broot_bytes == 0);
182874564fb4SBrian Foster 		return;
1829c24b5dfaSDave Chinner 	}
1830c24b5dfaSDave Chinner 
1831c24b5dfaSDave Chinner 	mp = ip->i_mount;
1832c24b5dfaSDave Chinner 
1833c24b5dfaSDave Chinner 	/* If this is a read-only mount, don't do this (would generate I/O) */
1834c24b5dfaSDave Chinner 	if (mp->m_flags & XFS_MOUNT_RDONLY)
183574564fb4SBrian Foster 		return;
1836c24b5dfaSDave Chinner 
1837c24b5dfaSDave Chinner 	if (ip->i_d.di_nlink != 0) {
1838c24b5dfaSDave Chinner 		/*
1839c24b5dfaSDave Chinner 		 * force is true because we are evicting an inode from the
1840c24b5dfaSDave Chinner 		 * cache. Post-eof blocks must be freed, lest we end up with
1841c24b5dfaSDave Chinner 		 * broken free space accounting.
1842c24b5dfaSDave Chinner 		 */
184374564fb4SBrian Foster 		if (xfs_can_free_eofblocks(ip, true))
184474564fb4SBrian Foster 			xfs_free_eofblocks(mp, ip, false);
184574564fb4SBrian Foster 
184674564fb4SBrian Foster 		return;
1847c24b5dfaSDave Chinner 	}
1848c24b5dfaSDave Chinner 
1849c24b5dfaSDave Chinner 	if (S_ISREG(ip->i_d.di_mode) &&
1850c24b5dfaSDave Chinner 	    (ip->i_d.di_size != 0 || XFS_ISIZE(ip) != 0 ||
1851c24b5dfaSDave Chinner 	     ip->i_d.di_nextents > 0 || ip->i_delayed_blks > 0))
1852c24b5dfaSDave Chinner 		truncate = 1;
1853c24b5dfaSDave Chinner 
1854c24b5dfaSDave Chinner 	error = xfs_qm_dqattach(ip, 0);
1855c24b5dfaSDave Chinner 	if (error)
185674564fb4SBrian Foster 		return;
1857c24b5dfaSDave Chinner 
1858f7be2d7fSBrian Foster 	if (S_ISLNK(ip->i_d.di_mode))
185936b21ddeSBrian Foster 		error = xfs_inactive_symlink(ip);
1860f7be2d7fSBrian Foster 	else if (truncate)
1861f7be2d7fSBrian Foster 		error = xfs_inactive_truncate(ip);
186236b21ddeSBrian Foster 	if (error)
186374564fb4SBrian Foster 		return;
1864c24b5dfaSDave Chinner 
1865c24b5dfaSDave Chinner 	/*
1866c24b5dfaSDave Chinner 	 * If there are attributes associated with the file then blow them away
1867c24b5dfaSDave Chinner 	 * now.  The code calls a routine that recursively deconstructs the
18686dfe5a04SDave Chinner 	 * attribute fork. If also blows away the in-core attribute fork.
1869c24b5dfaSDave Chinner 	 */
18706dfe5a04SDave Chinner 	if (XFS_IFORK_Q(ip)) {
1871c24b5dfaSDave Chinner 		error = xfs_attr_inactive(ip);
1872c24b5dfaSDave Chinner 		if (error)
187374564fb4SBrian Foster 			return;
1874c24b5dfaSDave Chinner 	}
1875c24b5dfaSDave Chinner 
18766dfe5a04SDave Chinner 	ASSERT(!ip->i_afp);
1877c24b5dfaSDave Chinner 	ASSERT(ip->i_d.di_anextents == 0);
18786dfe5a04SDave Chinner 	ASSERT(ip->i_d.di_forkoff == 0);
1879c24b5dfaSDave Chinner 
1880c24b5dfaSDave Chinner 	/*
1881c24b5dfaSDave Chinner 	 * Free the inode.
1882c24b5dfaSDave Chinner 	 */
188388877d2bSBrian Foster 	error = xfs_inactive_ifree(ip);
1884c24b5dfaSDave Chinner 	if (error)
188574564fb4SBrian Foster 		return;
1886c24b5dfaSDave Chinner 
1887c24b5dfaSDave Chinner 	/*
1888c24b5dfaSDave Chinner 	 * Release the dquots held by inode, if any.
1889c24b5dfaSDave Chinner 	 */
1890c24b5dfaSDave Chinner 	xfs_qm_dqdetach(ip);
1891c24b5dfaSDave Chinner }
1892c24b5dfaSDave Chinner 
18931da177e4SLinus Torvalds /*
18941da177e4SLinus Torvalds  * This is called when the inode's link count goes to 0.
18951da177e4SLinus Torvalds  * We place the on-disk inode on a list in the AGI.  It
18961da177e4SLinus Torvalds  * will be pulled from this list when the inode is freed.
18971da177e4SLinus Torvalds  */
18981da177e4SLinus Torvalds int
18991da177e4SLinus Torvalds xfs_iunlink(
19001da177e4SLinus Torvalds 	xfs_trans_t	*tp,
19011da177e4SLinus Torvalds 	xfs_inode_t	*ip)
19021da177e4SLinus Torvalds {
19031da177e4SLinus Torvalds 	xfs_mount_t	*mp;
19041da177e4SLinus Torvalds 	xfs_agi_t	*agi;
19051da177e4SLinus Torvalds 	xfs_dinode_t	*dip;
19061da177e4SLinus Torvalds 	xfs_buf_t	*agibp;
19071da177e4SLinus Torvalds 	xfs_buf_t	*ibp;
19081da177e4SLinus Torvalds 	xfs_agino_t	agino;
19091da177e4SLinus Torvalds 	short		bucket_index;
19101da177e4SLinus Torvalds 	int		offset;
19111da177e4SLinus Torvalds 	int		error;
19121da177e4SLinus Torvalds 
19131da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_nlink == 0);
19141da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_mode != 0);
19151da177e4SLinus Torvalds 
19161da177e4SLinus Torvalds 	mp = tp->t_mountp;
19171da177e4SLinus Torvalds 
19181da177e4SLinus Torvalds 	/*
19191da177e4SLinus Torvalds 	 * Get the agi buffer first.  It ensures lock ordering
19201da177e4SLinus Torvalds 	 * on the list.
19211da177e4SLinus Torvalds 	 */
19225e1be0fbSChristoph Hellwig 	error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
1923859d7182SVlad Apostolov 	if (error)
19241da177e4SLinus Torvalds 		return error;
19251da177e4SLinus Torvalds 	agi = XFS_BUF_TO_AGI(agibp);
19265e1be0fbSChristoph Hellwig 
19271da177e4SLinus Torvalds 	/*
19281da177e4SLinus Torvalds 	 * Get the index into the agi hash table for the
19291da177e4SLinus Torvalds 	 * list this inode will go on.
19301da177e4SLinus Torvalds 	 */
19311da177e4SLinus Torvalds 	agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
19321da177e4SLinus Torvalds 	ASSERT(agino != 0);
19331da177e4SLinus Torvalds 	bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
19341da177e4SLinus Torvalds 	ASSERT(agi->agi_unlinked[bucket_index]);
193516259e7dSChristoph Hellwig 	ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
19361da177e4SLinus Torvalds 
193769ef921bSChristoph Hellwig 	if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
19381da177e4SLinus Torvalds 		/*
19391da177e4SLinus Torvalds 		 * There is already another inode in the bucket we need
19401da177e4SLinus Torvalds 		 * to add ourselves to.  Add us at the front of the list.
19411da177e4SLinus Torvalds 		 * Here we put the head pointer into our next pointer,
19421da177e4SLinus Torvalds 		 * and then we fall through to point the head at us.
19431da177e4SLinus Torvalds 		 */
1944475ee413SChristoph Hellwig 		error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1945475ee413SChristoph Hellwig 				       0, 0);
1946c319b58bSVlad Apostolov 		if (error)
1947c319b58bSVlad Apostolov 			return error;
1948c319b58bSVlad Apostolov 
194969ef921bSChristoph Hellwig 		ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
19501da177e4SLinus Torvalds 		dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
195192bfc6e7SChristoph Hellwig 		offset = ip->i_imap.im_boffset +
19521da177e4SLinus Torvalds 			offsetof(xfs_dinode_t, di_next_unlinked);
19530a32c26eSDave Chinner 
19540a32c26eSDave Chinner 		/* need to recalc the inode CRC if appropriate */
19550a32c26eSDave Chinner 		xfs_dinode_calc_crc(mp, dip);
19560a32c26eSDave Chinner 
19571da177e4SLinus Torvalds 		xfs_trans_inode_buf(tp, ibp);
19581da177e4SLinus Torvalds 		xfs_trans_log_buf(tp, ibp, offset,
19591da177e4SLinus Torvalds 				  (offset + sizeof(xfs_agino_t) - 1));
19601da177e4SLinus Torvalds 		xfs_inobp_check(mp, ibp);
19611da177e4SLinus Torvalds 	}
19621da177e4SLinus Torvalds 
19631da177e4SLinus Torvalds 	/*
19641da177e4SLinus Torvalds 	 * Point the bucket head pointer at the inode being inserted.
19651da177e4SLinus Torvalds 	 */
19661da177e4SLinus Torvalds 	ASSERT(agino != 0);
196716259e7dSChristoph Hellwig 	agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
19681da177e4SLinus Torvalds 	offset = offsetof(xfs_agi_t, agi_unlinked) +
19691da177e4SLinus Torvalds 		(sizeof(xfs_agino_t) * bucket_index);
1970f19b872bSDave Chinner 	xfs_trans_buf_set_type(tp, agibp, XFS_BLFT_AGI_BUF);
19711da177e4SLinus Torvalds 	xfs_trans_log_buf(tp, agibp, offset,
19721da177e4SLinus Torvalds 			  (offset + sizeof(xfs_agino_t) - 1));
19731da177e4SLinus Torvalds 	return 0;
19741da177e4SLinus Torvalds }
19751da177e4SLinus Torvalds 
19761da177e4SLinus Torvalds /*
19771da177e4SLinus Torvalds  * Pull the on-disk inode from the AGI unlinked list.
19781da177e4SLinus Torvalds  */
19791da177e4SLinus Torvalds STATIC int
19801da177e4SLinus Torvalds xfs_iunlink_remove(
19811da177e4SLinus Torvalds 	xfs_trans_t	*tp,
19821da177e4SLinus Torvalds 	xfs_inode_t	*ip)
19831da177e4SLinus Torvalds {
19841da177e4SLinus Torvalds 	xfs_ino_t	next_ino;
19851da177e4SLinus Torvalds 	xfs_mount_t	*mp;
19861da177e4SLinus Torvalds 	xfs_agi_t	*agi;
19871da177e4SLinus Torvalds 	xfs_dinode_t	*dip;
19881da177e4SLinus Torvalds 	xfs_buf_t	*agibp;
19891da177e4SLinus Torvalds 	xfs_buf_t	*ibp;
19901da177e4SLinus Torvalds 	xfs_agnumber_t	agno;
19911da177e4SLinus Torvalds 	xfs_agino_t	agino;
19921da177e4SLinus Torvalds 	xfs_agino_t	next_agino;
19931da177e4SLinus Torvalds 	xfs_buf_t	*last_ibp;
19946fdf8cccSNathan Scott 	xfs_dinode_t	*last_dip = NULL;
19951da177e4SLinus Torvalds 	short		bucket_index;
19966fdf8cccSNathan Scott 	int		offset, last_offset = 0;
19971da177e4SLinus Torvalds 	int		error;
19981da177e4SLinus Torvalds 
19991da177e4SLinus Torvalds 	mp = tp->t_mountp;
20001da177e4SLinus Torvalds 	agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
20011da177e4SLinus Torvalds 
20021da177e4SLinus Torvalds 	/*
20031da177e4SLinus Torvalds 	 * Get the agi buffer first.  It ensures lock ordering
20041da177e4SLinus Torvalds 	 * on the list.
20051da177e4SLinus Torvalds 	 */
20065e1be0fbSChristoph Hellwig 	error = xfs_read_agi(mp, tp, agno, &agibp);
20075e1be0fbSChristoph Hellwig 	if (error)
20081da177e4SLinus Torvalds 		return error;
20095e1be0fbSChristoph Hellwig 
20101da177e4SLinus Torvalds 	agi = XFS_BUF_TO_AGI(agibp);
20115e1be0fbSChristoph Hellwig 
20121da177e4SLinus Torvalds 	/*
20131da177e4SLinus Torvalds 	 * Get the index into the agi hash table for the
20141da177e4SLinus Torvalds 	 * list this inode will go on.
20151da177e4SLinus Torvalds 	 */
20161da177e4SLinus Torvalds 	agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
20171da177e4SLinus Torvalds 	ASSERT(agino != 0);
20181da177e4SLinus Torvalds 	bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
201969ef921bSChristoph Hellwig 	ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO));
20201da177e4SLinus Torvalds 	ASSERT(agi->agi_unlinked[bucket_index]);
20211da177e4SLinus Torvalds 
202216259e7dSChristoph Hellwig 	if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
20231da177e4SLinus Torvalds 		/*
2024475ee413SChristoph Hellwig 		 * We're at the head of the list.  Get the inode's on-disk
2025475ee413SChristoph Hellwig 		 * buffer to see if there is anyone after us on the list.
2026475ee413SChristoph Hellwig 		 * Only modify our next pointer if it is not already NULLAGINO.
2027475ee413SChristoph Hellwig 		 * This saves us the overhead of dealing with the buffer when
2028475ee413SChristoph Hellwig 		 * there is no need to change it.
20291da177e4SLinus Torvalds 		 */
2030475ee413SChristoph Hellwig 		error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
2031475ee413SChristoph Hellwig 				       0, 0);
20321da177e4SLinus Torvalds 		if (error) {
2033475ee413SChristoph Hellwig 			xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
20340b932cccSDave Chinner 				__func__, error);
20351da177e4SLinus Torvalds 			return error;
20361da177e4SLinus Torvalds 		}
2037347d1c01SChristoph Hellwig 		next_agino = be32_to_cpu(dip->di_next_unlinked);
20381da177e4SLinus Torvalds 		ASSERT(next_agino != 0);
20391da177e4SLinus Torvalds 		if (next_agino != NULLAGINO) {
2040347d1c01SChristoph Hellwig 			dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
204192bfc6e7SChristoph Hellwig 			offset = ip->i_imap.im_boffset +
20421da177e4SLinus Torvalds 				offsetof(xfs_dinode_t, di_next_unlinked);
20430a32c26eSDave Chinner 
20440a32c26eSDave Chinner 			/* need to recalc the inode CRC if appropriate */
20450a32c26eSDave Chinner 			xfs_dinode_calc_crc(mp, dip);
20460a32c26eSDave Chinner 
20471da177e4SLinus Torvalds 			xfs_trans_inode_buf(tp, ibp);
20481da177e4SLinus Torvalds 			xfs_trans_log_buf(tp, ibp, offset,
20491da177e4SLinus Torvalds 					  (offset + sizeof(xfs_agino_t) - 1));
20501da177e4SLinus Torvalds 			xfs_inobp_check(mp, ibp);
20511da177e4SLinus Torvalds 		} else {
20521da177e4SLinus Torvalds 			xfs_trans_brelse(tp, ibp);
20531da177e4SLinus Torvalds 		}
20541da177e4SLinus Torvalds 		/*
20551da177e4SLinus Torvalds 		 * Point the bucket head pointer at the next inode.
20561da177e4SLinus Torvalds 		 */
20571da177e4SLinus Torvalds 		ASSERT(next_agino != 0);
20581da177e4SLinus Torvalds 		ASSERT(next_agino != agino);
205916259e7dSChristoph Hellwig 		agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
20601da177e4SLinus Torvalds 		offset = offsetof(xfs_agi_t, agi_unlinked) +
20611da177e4SLinus Torvalds 			(sizeof(xfs_agino_t) * bucket_index);
2062f19b872bSDave Chinner 		xfs_trans_buf_set_type(tp, agibp, XFS_BLFT_AGI_BUF);
20631da177e4SLinus Torvalds 		xfs_trans_log_buf(tp, agibp, offset,
20641da177e4SLinus Torvalds 				  (offset + sizeof(xfs_agino_t) - 1));
20651da177e4SLinus Torvalds 	} else {
20661da177e4SLinus Torvalds 		/*
20671da177e4SLinus Torvalds 		 * We need to search the list for the inode being freed.
20681da177e4SLinus Torvalds 		 */
206916259e7dSChristoph Hellwig 		next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
20701da177e4SLinus Torvalds 		last_ibp = NULL;
20711da177e4SLinus Torvalds 		while (next_agino != agino) {
2072129dbc9aSChristoph Hellwig 			struct xfs_imap	imap;
2073129dbc9aSChristoph Hellwig 
2074129dbc9aSChristoph Hellwig 			if (last_ibp)
20751da177e4SLinus Torvalds 				xfs_trans_brelse(tp, last_ibp);
2076129dbc9aSChristoph Hellwig 
2077129dbc9aSChristoph Hellwig 			imap.im_blkno = 0;
20781da177e4SLinus Torvalds 			next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
2079129dbc9aSChristoph Hellwig 
2080129dbc9aSChristoph Hellwig 			error = xfs_imap(mp, tp, next_ino, &imap, 0);
20811da177e4SLinus Torvalds 			if (error) {
20820b932cccSDave Chinner 				xfs_warn(mp,
2083129dbc9aSChristoph Hellwig 	"%s: xfs_imap returned error %d.",
20840b932cccSDave Chinner 					 __func__, error);
20851da177e4SLinus Torvalds 				return error;
20861da177e4SLinus Torvalds 			}
2087129dbc9aSChristoph Hellwig 
2088129dbc9aSChristoph Hellwig 			error = xfs_imap_to_bp(mp, tp, &imap, &last_dip,
2089129dbc9aSChristoph Hellwig 					       &last_ibp, 0, 0);
2090129dbc9aSChristoph Hellwig 			if (error) {
2091129dbc9aSChristoph Hellwig 				xfs_warn(mp,
2092129dbc9aSChristoph Hellwig 	"%s: xfs_imap_to_bp returned error %d.",
2093129dbc9aSChristoph Hellwig 					__func__, error);
2094129dbc9aSChristoph Hellwig 				return error;
2095129dbc9aSChristoph Hellwig 			}
2096129dbc9aSChristoph Hellwig 
2097129dbc9aSChristoph Hellwig 			last_offset = imap.im_boffset;
2098347d1c01SChristoph Hellwig 			next_agino = be32_to_cpu(last_dip->di_next_unlinked);
20991da177e4SLinus Torvalds 			ASSERT(next_agino != NULLAGINO);
21001da177e4SLinus Torvalds 			ASSERT(next_agino != 0);
21011da177e4SLinus Torvalds 		}
2102475ee413SChristoph Hellwig 
21031da177e4SLinus Torvalds 		/*
2104475ee413SChristoph Hellwig 		 * Now last_ibp points to the buffer previous to us on the
2105475ee413SChristoph Hellwig 		 * unlinked list.  Pull us from the list.
21061da177e4SLinus Torvalds 		 */
2107475ee413SChristoph Hellwig 		error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
2108475ee413SChristoph Hellwig 				       0, 0);
21091da177e4SLinus Torvalds 		if (error) {
2110475ee413SChristoph Hellwig 			xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.",
21110b932cccSDave Chinner 				__func__, error);
21121da177e4SLinus Torvalds 			return error;
21131da177e4SLinus Torvalds 		}
2114347d1c01SChristoph Hellwig 		next_agino = be32_to_cpu(dip->di_next_unlinked);
21151da177e4SLinus Torvalds 		ASSERT(next_agino != 0);
21161da177e4SLinus Torvalds 		ASSERT(next_agino != agino);
21171da177e4SLinus Torvalds 		if (next_agino != NULLAGINO) {
2118347d1c01SChristoph Hellwig 			dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
211992bfc6e7SChristoph Hellwig 			offset = ip->i_imap.im_boffset +
21201da177e4SLinus Torvalds 				offsetof(xfs_dinode_t, di_next_unlinked);
21210a32c26eSDave Chinner 
21220a32c26eSDave Chinner 			/* need to recalc the inode CRC if appropriate */
21230a32c26eSDave Chinner 			xfs_dinode_calc_crc(mp, dip);
21240a32c26eSDave Chinner 
21251da177e4SLinus Torvalds 			xfs_trans_inode_buf(tp, ibp);
21261da177e4SLinus Torvalds 			xfs_trans_log_buf(tp, ibp, offset,
21271da177e4SLinus Torvalds 					  (offset + sizeof(xfs_agino_t) - 1));
21281da177e4SLinus Torvalds 			xfs_inobp_check(mp, ibp);
21291da177e4SLinus Torvalds 		} else {
21301da177e4SLinus Torvalds 			xfs_trans_brelse(tp, ibp);
21311da177e4SLinus Torvalds 		}
21321da177e4SLinus Torvalds 		/*
21331da177e4SLinus Torvalds 		 * Point the previous inode on the list to the next inode.
21341da177e4SLinus Torvalds 		 */
2135347d1c01SChristoph Hellwig 		last_dip->di_next_unlinked = cpu_to_be32(next_agino);
21361da177e4SLinus Torvalds 		ASSERT(next_agino != 0);
21371da177e4SLinus Torvalds 		offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
21380a32c26eSDave Chinner 
21390a32c26eSDave Chinner 		/* need to recalc the inode CRC if appropriate */
21400a32c26eSDave Chinner 		xfs_dinode_calc_crc(mp, last_dip);
21410a32c26eSDave Chinner 
21421da177e4SLinus Torvalds 		xfs_trans_inode_buf(tp, last_ibp);
21431da177e4SLinus Torvalds 		xfs_trans_log_buf(tp, last_ibp, offset,
21441da177e4SLinus Torvalds 				  (offset + sizeof(xfs_agino_t) - 1));
21451da177e4SLinus Torvalds 		xfs_inobp_check(mp, last_ibp);
21461da177e4SLinus Torvalds 	}
21471da177e4SLinus Torvalds 	return 0;
21481da177e4SLinus Torvalds }
21491da177e4SLinus Torvalds 
21505b3eed75SDave Chinner /*
21510b8182dbSZhi Yong Wu  * A big issue when freeing the inode cluster is that we _cannot_ skip any
21525b3eed75SDave Chinner  * inodes that are in memory - they all must be marked stale and attached to
21535b3eed75SDave Chinner  * the cluster buffer.
21545b3eed75SDave Chinner  */
21552a30f36dSChandra Seetharaman STATIC int
21561da177e4SLinus Torvalds xfs_ifree_cluster(
21571da177e4SLinus Torvalds 	xfs_inode_t		*free_ip,
21581da177e4SLinus Torvalds 	xfs_trans_t		*tp,
215909b56604SBrian Foster 	struct xfs_icluster	*xic)
21601da177e4SLinus Torvalds {
21611da177e4SLinus Torvalds 	xfs_mount_t		*mp = free_ip->i_mount;
21621da177e4SLinus Torvalds 	int			blks_per_cluster;
2163982e939eSJie Liu 	int			inodes_per_cluster;
21641da177e4SLinus Torvalds 	int			nbufs;
21655b257b4aSDave Chinner 	int			i, j;
21663cdaa189SBrian Foster 	int			ioffset;
21671da177e4SLinus Torvalds 	xfs_daddr_t		blkno;
21681da177e4SLinus Torvalds 	xfs_buf_t		*bp;
21695b257b4aSDave Chinner 	xfs_inode_t		*ip;
21701da177e4SLinus Torvalds 	xfs_inode_log_item_t	*iip;
21711da177e4SLinus Torvalds 	xfs_log_item_t		*lip;
21725017e97dSDave Chinner 	struct xfs_perag	*pag;
217309b56604SBrian Foster 	xfs_ino_t		inum;
21741da177e4SLinus Torvalds 
217509b56604SBrian Foster 	inum = xic->first_ino;
21765017e97dSDave Chinner 	pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
2177982e939eSJie Liu 	blks_per_cluster = xfs_icluster_size_fsb(mp);
2178982e939eSJie Liu 	inodes_per_cluster = blks_per_cluster << mp->m_sb.sb_inopblog;
2179126cd105SJie Liu 	nbufs = mp->m_ialloc_blks / blks_per_cluster;
21801da177e4SLinus Torvalds 
2181982e939eSJie Liu 	for (j = 0; j < nbufs; j++, inum += inodes_per_cluster) {
218209b56604SBrian Foster 		/*
218309b56604SBrian Foster 		 * The allocation bitmap tells us which inodes of the chunk were
218409b56604SBrian Foster 		 * physically allocated. Skip the cluster if an inode falls into
218509b56604SBrian Foster 		 * a sparse region.
218609b56604SBrian Foster 		 */
21873cdaa189SBrian Foster 		ioffset = inum - xic->first_ino;
21883cdaa189SBrian Foster 		if ((xic->alloc & XFS_INOBT_MASK(ioffset)) == 0) {
21893cdaa189SBrian Foster 			ASSERT(do_mod(ioffset, inodes_per_cluster) == 0);
219009b56604SBrian Foster 			continue;
219109b56604SBrian Foster 		}
219209b56604SBrian Foster 
21931da177e4SLinus Torvalds 		blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
21941da177e4SLinus Torvalds 					 XFS_INO_TO_AGBNO(mp, inum));
21951da177e4SLinus Torvalds 
21961da177e4SLinus Torvalds 		/*
21975b257b4aSDave Chinner 		 * We obtain and lock the backing buffer first in the process
21985b257b4aSDave Chinner 		 * here, as we have to ensure that any dirty inode that we
21995b257b4aSDave Chinner 		 * can't get the flush lock on is attached to the buffer.
22005b257b4aSDave Chinner 		 * If we scan the in-memory inodes first, then buffer IO can
22015b257b4aSDave Chinner 		 * complete before we get a lock on it, and hence we may fail
22025b257b4aSDave Chinner 		 * to mark all the active inodes on the buffer stale.
22031da177e4SLinus Torvalds 		 */
22041da177e4SLinus Torvalds 		bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
2205b6aff29fSDave Chinner 					mp->m_bsize * blks_per_cluster,
2206b6aff29fSDave Chinner 					XBF_UNMAPPED);
22071da177e4SLinus Torvalds 
22082a30f36dSChandra Seetharaman 		if (!bp)
22092451337dSDave Chinner 			return -ENOMEM;
2210b0f539deSDave Chinner 
2211b0f539deSDave Chinner 		/*
2212b0f539deSDave Chinner 		 * This buffer may not have been correctly initialised as we
2213b0f539deSDave Chinner 		 * didn't read it from disk. That's not important because we are
2214b0f539deSDave Chinner 		 * only using to mark the buffer as stale in the log, and to
2215b0f539deSDave Chinner 		 * attach stale cached inodes on it. That means it will never be
2216b0f539deSDave Chinner 		 * dispatched for IO. If it is, we want to know about it, and we
2217b0f539deSDave Chinner 		 * want it to fail. We can acheive this by adding a write
2218b0f539deSDave Chinner 		 * verifier to the buffer.
2219b0f539deSDave Chinner 		 */
22201813dd64SDave Chinner 		 bp->b_ops = &xfs_inode_buf_ops;
2221b0f539deSDave Chinner 
22225b257b4aSDave Chinner 		/*
22235b257b4aSDave Chinner 		 * Walk the inodes already attached to the buffer and mark them
22245b257b4aSDave Chinner 		 * stale. These will all have the flush locks held, so an
22255b3eed75SDave Chinner 		 * in-memory inode walk can't lock them. By marking them all
22265b3eed75SDave Chinner 		 * stale first, we will not attempt to lock them in the loop
22275b3eed75SDave Chinner 		 * below as the XFS_ISTALE flag will be set.
22285b257b4aSDave Chinner 		 */
2229adadbeefSChristoph Hellwig 		lip = bp->b_fspriv;
22301da177e4SLinus Torvalds 		while (lip) {
22311da177e4SLinus Torvalds 			if (lip->li_type == XFS_LI_INODE) {
22321da177e4SLinus Torvalds 				iip = (xfs_inode_log_item_t *)lip;
22331da177e4SLinus Torvalds 				ASSERT(iip->ili_logged == 1);
2234ca30b2a7SChristoph Hellwig 				lip->li_cb = xfs_istale_done;
22357b2e2a31SDavid Chinner 				xfs_trans_ail_copy_lsn(mp->m_ail,
22367b2e2a31SDavid Chinner 							&iip->ili_flush_lsn,
22377b2e2a31SDavid Chinner 							&iip->ili_item.li_lsn);
2238e5ffd2bbSDavid Chinner 				xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
22391da177e4SLinus Torvalds 			}
22401da177e4SLinus Torvalds 			lip = lip->li_bio_list;
22411da177e4SLinus Torvalds 		}
22421da177e4SLinus Torvalds 
22435b3eed75SDave Chinner 
22445b257b4aSDave Chinner 		/*
22455b257b4aSDave Chinner 		 * For each inode in memory attempt to add it to the inode
22465b257b4aSDave Chinner 		 * buffer and set it up for being staled on buffer IO
22475b257b4aSDave Chinner 		 * completion.  This is safe as we've locked out tail pushing
22485b257b4aSDave Chinner 		 * and flushing by locking the buffer.
22495b257b4aSDave Chinner 		 *
22505b257b4aSDave Chinner 		 * We have already marked every inode that was part of a
22515b257b4aSDave Chinner 		 * transaction stale above, which means there is no point in
22525b257b4aSDave Chinner 		 * even trying to lock them.
22535b257b4aSDave Chinner 		 */
2254982e939eSJie Liu 		for (i = 0; i < inodes_per_cluster; i++) {
22555b3eed75SDave Chinner retry:
22561a3e8f3dSDave Chinner 			rcu_read_lock();
22575b257b4aSDave Chinner 			ip = radix_tree_lookup(&pag->pag_ici_root,
22585b257b4aSDave Chinner 					XFS_INO_TO_AGINO(mp, (inum + i)));
22591da177e4SLinus Torvalds 
22601a3e8f3dSDave Chinner 			/* Inode not in memory, nothing to do */
22611a3e8f3dSDave Chinner 			if (!ip) {
22621a3e8f3dSDave Chinner 				rcu_read_unlock();
22635b257b4aSDave Chinner 				continue;
22645b257b4aSDave Chinner 			}
22655b257b4aSDave Chinner 
22665b3eed75SDave Chinner 			/*
22671a3e8f3dSDave Chinner 			 * because this is an RCU protected lookup, we could
22681a3e8f3dSDave Chinner 			 * find a recently freed or even reallocated inode
22691a3e8f3dSDave Chinner 			 * during the lookup. We need to check under the
22701a3e8f3dSDave Chinner 			 * i_flags_lock for a valid inode here. Skip it if it
22711a3e8f3dSDave Chinner 			 * is not valid, the wrong inode or stale.
22721a3e8f3dSDave Chinner 			 */
22731a3e8f3dSDave Chinner 			spin_lock(&ip->i_flags_lock);
22741a3e8f3dSDave Chinner 			if (ip->i_ino != inum + i ||
22751a3e8f3dSDave Chinner 			    __xfs_iflags_test(ip, XFS_ISTALE)) {
22761a3e8f3dSDave Chinner 				spin_unlock(&ip->i_flags_lock);
22771a3e8f3dSDave Chinner 				rcu_read_unlock();
22781a3e8f3dSDave Chinner 				continue;
22791a3e8f3dSDave Chinner 			}
22801a3e8f3dSDave Chinner 			spin_unlock(&ip->i_flags_lock);
22811a3e8f3dSDave Chinner 
22821a3e8f3dSDave Chinner 			/*
22835b3eed75SDave Chinner 			 * Don't try to lock/unlock the current inode, but we
22845b3eed75SDave Chinner 			 * _cannot_ skip the other inodes that we did not find
22855b3eed75SDave Chinner 			 * in the list attached to the buffer and are not
22865b3eed75SDave Chinner 			 * already marked stale. If we can't lock it, back off
22875b3eed75SDave Chinner 			 * and retry.
22885b3eed75SDave Chinner 			 */
22895b257b4aSDave Chinner 			if (ip != free_ip &&
22905b257b4aSDave Chinner 			    !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
22911a3e8f3dSDave Chinner 				rcu_read_unlock();
22925b3eed75SDave Chinner 				delay(1);
22935b3eed75SDave Chinner 				goto retry;
22945b257b4aSDave Chinner 			}
22951a3e8f3dSDave Chinner 			rcu_read_unlock();
22965b257b4aSDave Chinner 
22975b3eed75SDave Chinner 			xfs_iflock(ip);
22985b257b4aSDave Chinner 			xfs_iflags_set(ip, XFS_ISTALE);
22995b257b4aSDave Chinner 
23005b3eed75SDave Chinner 			/*
23015b3eed75SDave Chinner 			 * we don't need to attach clean inodes or those only
23025b3eed75SDave Chinner 			 * with unlogged changes (which we throw away, anyway).
23035b3eed75SDave Chinner 			 */
23045b257b4aSDave Chinner 			iip = ip->i_itemp;
23055b3eed75SDave Chinner 			if (!iip || xfs_inode_clean(ip)) {
23065b257b4aSDave Chinner 				ASSERT(ip != free_ip);
23071da177e4SLinus Torvalds 				xfs_ifunlock(ip);
23081da177e4SLinus Torvalds 				xfs_iunlock(ip, XFS_ILOCK_EXCL);
23091da177e4SLinus Torvalds 				continue;
23101da177e4SLinus Torvalds 			}
23111da177e4SLinus Torvalds 
2312f5d8d5c4SChristoph Hellwig 			iip->ili_last_fields = iip->ili_fields;
2313f5d8d5c4SChristoph Hellwig 			iip->ili_fields = 0;
23141da177e4SLinus Torvalds 			iip->ili_logged = 1;
23157b2e2a31SDavid Chinner 			xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
23167b2e2a31SDavid Chinner 						&iip->ili_item.li_lsn);
23171da177e4SLinus Torvalds 
2318ca30b2a7SChristoph Hellwig 			xfs_buf_attach_iodone(bp, xfs_istale_done,
2319ca30b2a7SChristoph Hellwig 						  &iip->ili_item);
23205b257b4aSDave Chinner 
23215b257b4aSDave Chinner 			if (ip != free_ip)
23221da177e4SLinus Torvalds 				xfs_iunlock(ip, XFS_ILOCK_EXCL);
23231da177e4SLinus Torvalds 		}
23241da177e4SLinus Torvalds 
23251da177e4SLinus Torvalds 		xfs_trans_stale_inode_buf(tp, bp);
23261da177e4SLinus Torvalds 		xfs_trans_binval(tp, bp);
23271da177e4SLinus Torvalds 	}
23281da177e4SLinus Torvalds 
23295017e97dSDave Chinner 	xfs_perag_put(pag);
23302a30f36dSChandra Seetharaman 	return 0;
23311da177e4SLinus Torvalds }
23321da177e4SLinus Torvalds 
23331da177e4SLinus Torvalds /*
23341da177e4SLinus Torvalds  * This is called to return an inode to the inode free list.
23351da177e4SLinus Torvalds  * The inode should already be truncated to 0 length and have
23361da177e4SLinus Torvalds  * no pages associated with it.  This routine also assumes that
23371da177e4SLinus Torvalds  * the inode is already a part of the transaction.
23381da177e4SLinus Torvalds  *
23391da177e4SLinus Torvalds  * The on-disk copy of the inode will have been added to the list
23401da177e4SLinus Torvalds  * of unlinked inodes in the AGI. We need to remove the inode from
23411da177e4SLinus Torvalds  * that list atomically with respect to freeing it here.
23421da177e4SLinus Torvalds  */
23431da177e4SLinus Torvalds int
23441da177e4SLinus Torvalds xfs_ifree(
23451da177e4SLinus Torvalds 	xfs_trans_t	*tp,
23461da177e4SLinus Torvalds 	xfs_inode_t	*ip,
23471da177e4SLinus Torvalds 	xfs_bmap_free_t	*flist)
23481da177e4SLinus Torvalds {
23491da177e4SLinus Torvalds 	int			error;
235009b56604SBrian Foster 	struct xfs_icluster	xic = { 0 };
23511da177e4SLinus Torvalds 
2352579aa9caSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
23531da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_nlink == 0);
23541da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_nextents == 0);
23551da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_anextents == 0);
2356ce7ae151SChristoph Hellwig 	ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode));
23571da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_nblocks == 0);
23581da177e4SLinus Torvalds 
23591da177e4SLinus Torvalds 	/*
23601da177e4SLinus Torvalds 	 * Pull the on-disk inode from the AGI unlinked list.
23611da177e4SLinus Torvalds 	 */
23621da177e4SLinus Torvalds 	error = xfs_iunlink_remove(tp, ip);
23631baaed8fSDave Chinner 	if (error)
23641da177e4SLinus Torvalds 		return error;
23651da177e4SLinus Torvalds 
236609b56604SBrian Foster 	error = xfs_difree(tp, ip->i_ino, flist, &xic);
23671baaed8fSDave Chinner 	if (error)
23681da177e4SLinus Torvalds 		return error;
23691baaed8fSDave Chinner 
23701da177e4SLinus Torvalds 	ip->i_d.di_mode = 0;		/* mark incore inode as free */
23711da177e4SLinus Torvalds 	ip->i_d.di_flags = 0;
23721da177e4SLinus Torvalds 	ip->i_d.di_dmevmask = 0;
23731da177e4SLinus Torvalds 	ip->i_d.di_forkoff = 0;		/* mark the attr fork not in use */
23741da177e4SLinus Torvalds 	ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
23751da177e4SLinus Torvalds 	ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
23761da177e4SLinus Torvalds 	/*
23771da177e4SLinus Torvalds 	 * Bump the generation count so no one will be confused
23781da177e4SLinus Torvalds 	 * by reincarnations of this inode.
23791da177e4SLinus Torvalds 	 */
23801da177e4SLinus Torvalds 	ip->i_d.di_gen++;
23811da177e4SLinus Torvalds 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
23821da177e4SLinus Torvalds 
238309b56604SBrian Foster 	if (xic.deleted)
238409b56604SBrian Foster 		error = xfs_ifree_cluster(ip, tp, &xic);
23851da177e4SLinus Torvalds 
23862a30f36dSChandra Seetharaman 	return error;
23871da177e4SLinus Torvalds }
23881da177e4SLinus Torvalds 
23891da177e4SLinus Torvalds /*
239060ec6783SChristoph Hellwig  * This is called to unpin an inode.  The caller must have the inode locked
239160ec6783SChristoph Hellwig  * in at least shared mode so that the buffer cannot be subsequently pinned
239260ec6783SChristoph Hellwig  * once someone is waiting for it to be unpinned.
23931da177e4SLinus Torvalds  */
239460ec6783SChristoph Hellwig static void
2395f392e631SChristoph Hellwig xfs_iunpin(
239660ec6783SChristoph Hellwig 	struct xfs_inode	*ip)
2397a3f74ffbSDavid Chinner {
2398579aa9caSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
2399a3f74ffbSDavid Chinner 
24004aaf15d1SDave Chinner 	trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
24014aaf15d1SDave Chinner 
2402a3f74ffbSDavid Chinner 	/* Give the log a push to start the unpinning I/O */
240360ec6783SChristoph Hellwig 	xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0);
2404a14a348bSChristoph Hellwig 
2405a3f74ffbSDavid Chinner }
2406a3f74ffbSDavid Chinner 
2407f392e631SChristoph Hellwig static void
2408f392e631SChristoph Hellwig __xfs_iunpin_wait(
2409f392e631SChristoph Hellwig 	struct xfs_inode	*ip)
2410f392e631SChristoph Hellwig {
2411f392e631SChristoph Hellwig 	wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
2412f392e631SChristoph Hellwig 	DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
2413f392e631SChristoph Hellwig 
2414f392e631SChristoph Hellwig 	xfs_iunpin(ip);
2415f392e631SChristoph Hellwig 
2416f392e631SChristoph Hellwig 	do {
2417f392e631SChristoph Hellwig 		prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2418f392e631SChristoph Hellwig 		if (xfs_ipincount(ip))
2419f392e631SChristoph Hellwig 			io_schedule();
2420f392e631SChristoph Hellwig 	} while (xfs_ipincount(ip));
2421f392e631SChristoph Hellwig 	finish_wait(wq, &wait.wait);
2422f392e631SChristoph Hellwig }
2423f392e631SChristoph Hellwig 
2424777df5afSDave Chinner void
24251da177e4SLinus Torvalds xfs_iunpin_wait(
242660ec6783SChristoph Hellwig 	struct xfs_inode	*ip)
24271da177e4SLinus Torvalds {
2428f392e631SChristoph Hellwig 	if (xfs_ipincount(ip))
2429f392e631SChristoph Hellwig 		__xfs_iunpin_wait(ip);
24301da177e4SLinus Torvalds }
24311da177e4SLinus Torvalds 
243227320369SDave Chinner /*
243327320369SDave Chinner  * Removing an inode from the namespace involves removing the directory entry
243427320369SDave Chinner  * and dropping the link count on the inode. Removing the directory entry can
243527320369SDave Chinner  * result in locking an AGF (directory blocks were freed) and removing a link
243627320369SDave Chinner  * count can result in placing the inode on an unlinked list which results in
243727320369SDave Chinner  * locking an AGI.
243827320369SDave Chinner  *
243927320369SDave Chinner  * The big problem here is that we have an ordering constraint on AGF and AGI
244027320369SDave Chinner  * locking - inode allocation locks the AGI, then can allocate a new extent for
244127320369SDave Chinner  * new inodes, locking the AGF after the AGI. Similarly, freeing the inode
244227320369SDave Chinner  * removes the inode from the unlinked list, requiring that we lock the AGI
244327320369SDave Chinner  * first, and then freeing the inode can result in an inode chunk being freed
244427320369SDave Chinner  * and hence freeing disk space requiring that we lock an AGF.
244527320369SDave Chinner  *
244627320369SDave Chinner  * Hence the ordering that is imposed by other parts of the code is AGI before
244727320369SDave Chinner  * AGF. This means we cannot remove the directory entry before we drop the inode
244827320369SDave Chinner  * reference count and put it on the unlinked list as this results in a lock
244927320369SDave Chinner  * order of AGF then AGI, and this can deadlock against inode allocation and
245027320369SDave Chinner  * freeing. Therefore we must drop the link counts before we remove the
245127320369SDave Chinner  * directory entry.
245227320369SDave Chinner  *
245327320369SDave Chinner  * This is still safe from a transactional point of view - it is not until we
245427320369SDave Chinner  * get to xfs_bmap_finish() that we have the possibility of multiple
245527320369SDave Chinner  * transactions in this operation. Hence as long as we remove the directory
245627320369SDave Chinner  * entry and drop the link count in the first transaction of the remove
245727320369SDave Chinner  * operation, there are no transactional constraints on the ordering here.
245827320369SDave Chinner  */
2459c24b5dfaSDave Chinner int
2460c24b5dfaSDave Chinner xfs_remove(
2461c24b5dfaSDave Chinner 	xfs_inode_t             *dp,
2462c24b5dfaSDave Chinner 	struct xfs_name		*name,
2463c24b5dfaSDave Chinner 	xfs_inode_t		*ip)
2464c24b5dfaSDave Chinner {
2465c24b5dfaSDave Chinner 	xfs_mount_t		*mp = dp->i_mount;
2466c24b5dfaSDave Chinner 	xfs_trans_t             *tp = NULL;
2467c24b5dfaSDave Chinner 	int			is_dir = S_ISDIR(ip->i_d.di_mode);
2468c24b5dfaSDave Chinner 	int                     error = 0;
2469c24b5dfaSDave Chinner 	xfs_bmap_free_t         free_list;
2470c24b5dfaSDave Chinner 	xfs_fsblock_t           first_block;
2471c24b5dfaSDave Chinner 	int			committed;
2472c24b5dfaSDave Chinner 	uint			resblks;
2473c24b5dfaSDave Chinner 
2474c24b5dfaSDave Chinner 	trace_xfs_remove(dp, name);
2475c24b5dfaSDave Chinner 
2476c24b5dfaSDave Chinner 	if (XFS_FORCED_SHUTDOWN(mp))
24772451337dSDave Chinner 		return -EIO;
2478c24b5dfaSDave Chinner 
2479c24b5dfaSDave Chinner 	error = xfs_qm_dqattach(dp, 0);
2480c24b5dfaSDave Chinner 	if (error)
2481c24b5dfaSDave Chinner 		goto std_return;
2482c24b5dfaSDave Chinner 
2483c24b5dfaSDave Chinner 	error = xfs_qm_dqattach(ip, 0);
2484c24b5dfaSDave Chinner 	if (error)
2485c24b5dfaSDave Chinner 		goto std_return;
2486c24b5dfaSDave Chinner 
248732296f86SDave Chinner 	if (is_dir)
2488c24b5dfaSDave Chinner 		tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
248932296f86SDave Chinner 	else
2490c24b5dfaSDave Chinner 		tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
2491c24b5dfaSDave Chinner 
2492c24b5dfaSDave Chinner 	/*
2493c24b5dfaSDave Chinner 	 * We try to get the real space reservation first,
2494c24b5dfaSDave Chinner 	 * allowing for directory btree deletion(s) implying
2495c24b5dfaSDave Chinner 	 * possible bmap insert(s).  If we can't get the space
2496c24b5dfaSDave Chinner 	 * reservation then we use 0 instead, and avoid the bmap
2497c24b5dfaSDave Chinner 	 * btree insert(s) in the directory code by, if the bmap
2498c24b5dfaSDave Chinner 	 * insert tries to happen, instead trimming the LAST
2499c24b5dfaSDave Chinner 	 * block from the directory.
2500c24b5dfaSDave Chinner 	 */
2501c24b5dfaSDave Chinner 	resblks = XFS_REMOVE_SPACE_RES(mp);
25023d3c8b52SJie Liu 	error = xfs_trans_reserve(tp, &M_RES(mp)->tr_remove, resblks, 0);
25032451337dSDave Chinner 	if (error == -ENOSPC) {
2504c24b5dfaSDave Chinner 		resblks = 0;
25053d3c8b52SJie Liu 		error = xfs_trans_reserve(tp, &M_RES(mp)->tr_remove, 0, 0);
2506c24b5dfaSDave Chinner 	}
2507c24b5dfaSDave Chinner 	if (error) {
25082451337dSDave Chinner 		ASSERT(error != -ENOSPC);
2509c24b5dfaSDave Chinner 		goto out_trans_cancel;
2510c24b5dfaSDave Chinner 	}
2511c24b5dfaSDave Chinner 
2512c24b5dfaSDave Chinner 	xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
2513c24b5dfaSDave Chinner 
2514c24b5dfaSDave Chinner 	xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2515c24b5dfaSDave Chinner 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2516c24b5dfaSDave Chinner 
2517c24b5dfaSDave Chinner 	/*
2518c24b5dfaSDave Chinner 	 * If we're removing a directory perform some additional validation.
2519c24b5dfaSDave Chinner 	 */
2520c24b5dfaSDave Chinner 	if (is_dir) {
2521c24b5dfaSDave Chinner 		ASSERT(ip->i_d.di_nlink >= 2);
2522c24b5dfaSDave Chinner 		if (ip->i_d.di_nlink != 2) {
25232451337dSDave Chinner 			error = -ENOTEMPTY;
2524c24b5dfaSDave Chinner 			goto out_trans_cancel;
2525c24b5dfaSDave Chinner 		}
2526c24b5dfaSDave Chinner 		if (!xfs_dir_isempty(ip)) {
25272451337dSDave Chinner 			error = -ENOTEMPTY;
2528c24b5dfaSDave Chinner 			goto out_trans_cancel;
2529c24b5dfaSDave Chinner 		}
2530c24b5dfaSDave Chinner 
253127320369SDave Chinner 		/* Drop the link from ip's "..".  */
2532c24b5dfaSDave Chinner 		error = xfs_droplink(tp, dp);
2533c24b5dfaSDave Chinner 		if (error)
253427320369SDave Chinner 			goto out_trans_cancel;
2535c24b5dfaSDave Chinner 
253627320369SDave Chinner 		/* Drop the "." link from ip to self.  */
2537c24b5dfaSDave Chinner 		error = xfs_droplink(tp, ip);
2538c24b5dfaSDave Chinner 		if (error)
253927320369SDave Chinner 			goto out_trans_cancel;
2540c24b5dfaSDave Chinner 	} else {
2541c24b5dfaSDave Chinner 		/*
2542c24b5dfaSDave Chinner 		 * When removing a non-directory we need to log the parent
2543c24b5dfaSDave Chinner 		 * inode here.  For a directory this is done implicitly
2544c24b5dfaSDave Chinner 		 * by the xfs_droplink call for the ".." entry.
2545c24b5dfaSDave Chinner 		 */
2546c24b5dfaSDave Chinner 		xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2547c24b5dfaSDave Chinner 	}
254827320369SDave Chinner 	xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2549c24b5dfaSDave Chinner 
255027320369SDave Chinner 	/* Drop the link from dp to ip. */
2551c24b5dfaSDave Chinner 	error = xfs_droplink(tp, ip);
2552c24b5dfaSDave Chinner 	if (error)
255327320369SDave Chinner 		goto out_trans_cancel;
2554c24b5dfaSDave Chinner 
255527320369SDave Chinner 	xfs_bmap_init(&free_list, &first_block);
255627320369SDave Chinner 	error = xfs_dir_removename(tp, dp, name, ip->i_ino,
255727320369SDave Chinner 					&first_block, &free_list, resblks);
255827320369SDave Chinner 	if (error) {
25592451337dSDave Chinner 		ASSERT(error != -ENOENT);
256027320369SDave Chinner 		goto out_bmap_cancel;
256127320369SDave Chinner 	}
256227320369SDave Chinner 
2563c24b5dfaSDave Chinner 	/*
2564c24b5dfaSDave Chinner 	 * If this is a synchronous mount, make sure that the
2565c24b5dfaSDave Chinner 	 * remove transaction goes to disk before returning to
2566c24b5dfaSDave Chinner 	 * the user.
2567c24b5dfaSDave Chinner 	 */
2568c24b5dfaSDave Chinner 	if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
2569c24b5dfaSDave Chinner 		xfs_trans_set_sync(tp);
2570c24b5dfaSDave Chinner 
2571c24b5dfaSDave Chinner 	error = xfs_bmap_finish(&tp, &free_list, &committed);
2572c24b5dfaSDave Chinner 	if (error)
2573c24b5dfaSDave Chinner 		goto out_bmap_cancel;
2574c24b5dfaSDave Chinner 
257570393313SChristoph Hellwig 	error = xfs_trans_commit(tp);
2576c24b5dfaSDave Chinner 	if (error)
2577c24b5dfaSDave Chinner 		goto std_return;
2578c24b5dfaSDave Chinner 
25792cd2ef6aSChristoph Hellwig 	if (is_dir && xfs_inode_is_filestream(ip))
2580c24b5dfaSDave Chinner 		xfs_filestream_deassociate(ip);
2581c24b5dfaSDave Chinner 
2582c24b5dfaSDave Chinner 	return 0;
2583c24b5dfaSDave Chinner 
2584c24b5dfaSDave Chinner  out_bmap_cancel:
2585c24b5dfaSDave Chinner 	xfs_bmap_cancel(&free_list);
2586c24b5dfaSDave Chinner  out_trans_cancel:
25874906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
2588c24b5dfaSDave Chinner  std_return:
2589c24b5dfaSDave Chinner 	return error;
2590c24b5dfaSDave Chinner }
2591c24b5dfaSDave Chinner 
2592f6bba201SDave Chinner /*
2593f6bba201SDave Chinner  * Enter all inodes for a rename transaction into a sorted array.
2594f6bba201SDave Chinner  */
259595afcf5cSDave Chinner #define __XFS_SORT_INODES	5
2596f6bba201SDave Chinner STATIC void
2597f6bba201SDave Chinner xfs_sort_for_rename(
259895afcf5cSDave Chinner 	struct xfs_inode	*dp1,	/* in: old (source) directory inode */
259995afcf5cSDave Chinner 	struct xfs_inode	*dp2,	/* in: new (target) directory inode */
260095afcf5cSDave Chinner 	struct xfs_inode	*ip1,	/* in: inode of old entry */
260195afcf5cSDave Chinner 	struct xfs_inode	*ip2,	/* in: inode of new entry */
260295afcf5cSDave Chinner 	struct xfs_inode	*wip,	/* in: whiteout inode */
260395afcf5cSDave Chinner 	struct xfs_inode	**i_tab,/* out: sorted array of inodes */
260495afcf5cSDave Chinner 	int			*num_inodes)  /* in/out: inodes in array */
2605f6bba201SDave Chinner {
2606f6bba201SDave Chinner 	int			i, j;
2607f6bba201SDave Chinner 
260895afcf5cSDave Chinner 	ASSERT(*num_inodes == __XFS_SORT_INODES);
260995afcf5cSDave Chinner 	memset(i_tab, 0, *num_inodes * sizeof(struct xfs_inode *));
261095afcf5cSDave Chinner 
2611f6bba201SDave Chinner 	/*
2612f6bba201SDave Chinner 	 * i_tab contains a list of pointers to inodes.  We initialize
2613f6bba201SDave Chinner 	 * the table here & we'll sort it.  We will then use it to
2614f6bba201SDave Chinner 	 * order the acquisition of the inode locks.
2615f6bba201SDave Chinner 	 *
2616f6bba201SDave Chinner 	 * Note that the table may contain duplicates.  e.g., dp1 == dp2.
2617f6bba201SDave Chinner 	 */
261895afcf5cSDave Chinner 	i = 0;
261995afcf5cSDave Chinner 	i_tab[i++] = dp1;
262095afcf5cSDave Chinner 	i_tab[i++] = dp2;
262195afcf5cSDave Chinner 	i_tab[i++] = ip1;
262295afcf5cSDave Chinner 	if (ip2)
262395afcf5cSDave Chinner 		i_tab[i++] = ip2;
262495afcf5cSDave Chinner 	if (wip)
262595afcf5cSDave Chinner 		i_tab[i++] = wip;
262695afcf5cSDave Chinner 	*num_inodes = i;
2627f6bba201SDave Chinner 
2628f6bba201SDave Chinner 	/*
2629f6bba201SDave Chinner 	 * Sort the elements via bubble sort.  (Remember, there are at
263095afcf5cSDave Chinner 	 * most 5 elements to sort, so this is adequate.)
2631f6bba201SDave Chinner 	 */
2632f6bba201SDave Chinner 	for (i = 0; i < *num_inodes; i++) {
2633f6bba201SDave Chinner 		for (j = 1; j < *num_inodes; j++) {
2634f6bba201SDave Chinner 			if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) {
263595afcf5cSDave Chinner 				struct xfs_inode *temp = i_tab[j];
2636f6bba201SDave Chinner 				i_tab[j] = i_tab[j-1];
2637f6bba201SDave Chinner 				i_tab[j-1] = temp;
2638f6bba201SDave Chinner 			}
2639f6bba201SDave Chinner 		}
2640f6bba201SDave Chinner 	}
2641f6bba201SDave Chinner }
2642f6bba201SDave Chinner 
2643310606b0SDave Chinner static int
2644310606b0SDave Chinner xfs_finish_rename(
2645310606b0SDave Chinner 	struct xfs_trans	*tp,
2646310606b0SDave Chinner 	struct xfs_bmap_free	*free_list)
2647310606b0SDave Chinner {
2648310606b0SDave Chinner 	int			committed = 0;
2649310606b0SDave Chinner 	int			error;
2650310606b0SDave Chinner 
2651310606b0SDave Chinner 	/*
2652310606b0SDave Chinner 	 * If this is a synchronous mount, make sure that the rename transaction
2653310606b0SDave Chinner 	 * goes to disk before returning to the user.
2654310606b0SDave Chinner 	 */
2655310606b0SDave Chinner 	if (tp->t_mountp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
2656310606b0SDave Chinner 		xfs_trans_set_sync(tp);
2657310606b0SDave Chinner 
2658310606b0SDave Chinner 	error = xfs_bmap_finish(&tp, free_list, &committed);
2659310606b0SDave Chinner 	if (error) {
2660310606b0SDave Chinner 		xfs_bmap_cancel(free_list);
26614906e215SChristoph Hellwig 		xfs_trans_cancel(tp);
2662310606b0SDave Chinner 		return error;
2663310606b0SDave Chinner 	}
2664310606b0SDave Chinner 
266570393313SChristoph Hellwig 	return xfs_trans_commit(tp);
2666310606b0SDave Chinner }
2667310606b0SDave Chinner 
2668f6bba201SDave Chinner /*
2669d31a1825SCarlos Maiolino  * xfs_cross_rename()
2670d31a1825SCarlos Maiolino  *
2671d31a1825SCarlos Maiolino  * responsible for handling RENAME_EXCHANGE flag in renameat2() sytemcall
2672d31a1825SCarlos Maiolino  */
2673d31a1825SCarlos Maiolino STATIC int
2674d31a1825SCarlos Maiolino xfs_cross_rename(
2675d31a1825SCarlos Maiolino 	struct xfs_trans	*tp,
2676d31a1825SCarlos Maiolino 	struct xfs_inode	*dp1,
2677d31a1825SCarlos Maiolino 	struct xfs_name		*name1,
2678d31a1825SCarlos Maiolino 	struct xfs_inode	*ip1,
2679d31a1825SCarlos Maiolino 	struct xfs_inode	*dp2,
2680d31a1825SCarlos Maiolino 	struct xfs_name		*name2,
2681d31a1825SCarlos Maiolino 	struct xfs_inode	*ip2,
2682d31a1825SCarlos Maiolino 	struct xfs_bmap_free	*free_list,
2683d31a1825SCarlos Maiolino 	xfs_fsblock_t		*first_block,
2684d31a1825SCarlos Maiolino 	int			spaceres)
2685d31a1825SCarlos Maiolino {
2686d31a1825SCarlos Maiolino 	int		error = 0;
2687d31a1825SCarlos Maiolino 	int		ip1_flags = 0;
2688d31a1825SCarlos Maiolino 	int		ip2_flags = 0;
2689d31a1825SCarlos Maiolino 	int		dp2_flags = 0;
2690d31a1825SCarlos Maiolino 
2691d31a1825SCarlos Maiolino 	/* Swap inode number for dirent in first parent */
2692d31a1825SCarlos Maiolino 	error = xfs_dir_replace(tp, dp1, name1,
2693d31a1825SCarlos Maiolino 				ip2->i_ino,
2694d31a1825SCarlos Maiolino 				first_block, free_list, spaceres);
2695d31a1825SCarlos Maiolino 	if (error)
2696eeacd321SDave Chinner 		goto out_trans_abort;
2697d31a1825SCarlos Maiolino 
2698d31a1825SCarlos Maiolino 	/* Swap inode number for dirent in second parent */
2699d31a1825SCarlos Maiolino 	error = xfs_dir_replace(tp, dp2, name2,
2700d31a1825SCarlos Maiolino 				ip1->i_ino,
2701d31a1825SCarlos Maiolino 				first_block, free_list, spaceres);
2702d31a1825SCarlos Maiolino 	if (error)
2703eeacd321SDave Chinner 		goto out_trans_abort;
2704d31a1825SCarlos Maiolino 
2705d31a1825SCarlos Maiolino 	/*
2706d31a1825SCarlos Maiolino 	 * If we're renaming one or more directories across different parents,
2707d31a1825SCarlos Maiolino 	 * update the respective ".." entries (and link counts) to match the new
2708d31a1825SCarlos Maiolino 	 * parents.
2709d31a1825SCarlos Maiolino 	 */
2710d31a1825SCarlos Maiolino 	if (dp1 != dp2) {
2711d31a1825SCarlos Maiolino 		dp2_flags = XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
2712d31a1825SCarlos Maiolino 
2713d31a1825SCarlos Maiolino 		if (S_ISDIR(ip2->i_d.di_mode)) {
2714d31a1825SCarlos Maiolino 			error = xfs_dir_replace(tp, ip2, &xfs_name_dotdot,
2715d31a1825SCarlos Maiolino 						dp1->i_ino, first_block,
2716d31a1825SCarlos Maiolino 						free_list, spaceres);
2717d31a1825SCarlos Maiolino 			if (error)
2718eeacd321SDave Chinner 				goto out_trans_abort;
2719d31a1825SCarlos Maiolino 
2720d31a1825SCarlos Maiolino 			/* transfer ip2 ".." reference to dp1 */
2721d31a1825SCarlos Maiolino 			if (!S_ISDIR(ip1->i_d.di_mode)) {
2722d31a1825SCarlos Maiolino 				error = xfs_droplink(tp, dp2);
2723d31a1825SCarlos Maiolino 				if (error)
2724eeacd321SDave Chinner 					goto out_trans_abort;
2725d31a1825SCarlos Maiolino 				error = xfs_bumplink(tp, dp1);
2726d31a1825SCarlos Maiolino 				if (error)
2727eeacd321SDave Chinner 					goto out_trans_abort;
2728d31a1825SCarlos Maiolino 			}
2729d31a1825SCarlos Maiolino 
2730d31a1825SCarlos Maiolino 			/*
2731d31a1825SCarlos Maiolino 			 * Although ip1 isn't changed here, userspace needs
2732d31a1825SCarlos Maiolino 			 * to be warned about the change, so that applications
2733d31a1825SCarlos Maiolino 			 * relying on it (like backup ones), will properly
2734d31a1825SCarlos Maiolino 			 * notify the change
2735d31a1825SCarlos Maiolino 			 */
2736d31a1825SCarlos Maiolino 			ip1_flags |= XFS_ICHGTIME_CHG;
2737d31a1825SCarlos Maiolino 			ip2_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
2738d31a1825SCarlos Maiolino 		}
2739d31a1825SCarlos Maiolino 
2740d31a1825SCarlos Maiolino 		if (S_ISDIR(ip1->i_d.di_mode)) {
2741d31a1825SCarlos Maiolino 			error = xfs_dir_replace(tp, ip1, &xfs_name_dotdot,
2742d31a1825SCarlos Maiolino 						dp2->i_ino, first_block,
2743d31a1825SCarlos Maiolino 						free_list, spaceres);
2744d31a1825SCarlos Maiolino 			if (error)
2745eeacd321SDave Chinner 				goto out_trans_abort;
2746d31a1825SCarlos Maiolino 
2747d31a1825SCarlos Maiolino 			/* transfer ip1 ".." reference to dp2 */
2748d31a1825SCarlos Maiolino 			if (!S_ISDIR(ip2->i_d.di_mode)) {
2749d31a1825SCarlos Maiolino 				error = xfs_droplink(tp, dp1);
2750d31a1825SCarlos Maiolino 				if (error)
2751eeacd321SDave Chinner 					goto out_trans_abort;
2752d31a1825SCarlos Maiolino 				error = xfs_bumplink(tp, dp2);
2753d31a1825SCarlos Maiolino 				if (error)
2754eeacd321SDave Chinner 					goto out_trans_abort;
2755d31a1825SCarlos Maiolino 			}
2756d31a1825SCarlos Maiolino 
2757d31a1825SCarlos Maiolino 			/*
2758d31a1825SCarlos Maiolino 			 * Although ip2 isn't changed here, userspace needs
2759d31a1825SCarlos Maiolino 			 * to be warned about the change, so that applications
2760d31a1825SCarlos Maiolino 			 * relying on it (like backup ones), will properly
2761d31a1825SCarlos Maiolino 			 * notify the change
2762d31a1825SCarlos Maiolino 			 */
2763d31a1825SCarlos Maiolino 			ip1_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
2764d31a1825SCarlos Maiolino 			ip2_flags |= XFS_ICHGTIME_CHG;
2765d31a1825SCarlos Maiolino 		}
2766d31a1825SCarlos Maiolino 	}
2767d31a1825SCarlos Maiolino 
2768d31a1825SCarlos Maiolino 	if (ip1_flags) {
2769d31a1825SCarlos Maiolino 		xfs_trans_ichgtime(tp, ip1, ip1_flags);
2770d31a1825SCarlos Maiolino 		xfs_trans_log_inode(tp, ip1, XFS_ILOG_CORE);
2771d31a1825SCarlos Maiolino 	}
2772d31a1825SCarlos Maiolino 	if (ip2_flags) {
2773d31a1825SCarlos Maiolino 		xfs_trans_ichgtime(tp, ip2, ip2_flags);
2774d31a1825SCarlos Maiolino 		xfs_trans_log_inode(tp, ip2, XFS_ILOG_CORE);
2775d31a1825SCarlos Maiolino 	}
2776d31a1825SCarlos Maiolino 	if (dp2_flags) {
2777d31a1825SCarlos Maiolino 		xfs_trans_ichgtime(tp, dp2, dp2_flags);
2778d31a1825SCarlos Maiolino 		xfs_trans_log_inode(tp, dp2, XFS_ILOG_CORE);
2779d31a1825SCarlos Maiolino 	}
2780d31a1825SCarlos Maiolino 	xfs_trans_ichgtime(tp, dp1, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2781d31a1825SCarlos Maiolino 	xfs_trans_log_inode(tp, dp1, XFS_ILOG_CORE);
2782eeacd321SDave Chinner 	return xfs_finish_rename(tp, free_list);
2783eeacd321SDave Chinner 
2784eeacd321SDave Chinner out_trans_abort:
2785eeacd321SDave Chinner 	xfs_bmap_cancel(free_list);
27864906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
2787d31a1825SCarlos Maiolino 	return error;
2788d31a1825SCarlos Maiolino }
2789d31a1825SCarlos Maiolino 
2790d31a1825SCarlos Maiolino /*
27917dcf5c3eSDave Chinner  * xfs_rename_alloc_whiteout()
27927dcf5c3eSDave Chinner  *
27937dcf5c3eSDave Chinner  * Return a referenced, unlinked, unlocked inode that that can be used as a
27947dcf5c3eSDave Chinner  * whiteout in a rename transaction. We use a tmpfile inode here so that if we
27957dcf5c3eSDave Chinner  * crash between allocating the inode and linking it into the rename transaction
27967dcf5c3eSDave Chinner  * recovery will free the inode and we won't leak it.
27977dcf5c3eSDave Chinner  */
27987dcf5c3eSDave Chinner static int
27997dcf5c3eSDave Chinner xfs_rename_alloc_whiteout(
28007dcf5c3eSDave Chinner 	struct xfs_inode	*dp,
28017dcf5c3eSDave Chinner 	struct xfs_inode	**wip)
28027dcf5c3eSDave Chinner {
28037dcf5c3eSDave Chinner 	struct xfs_inode	*tmpfile;
28047dcf5c3eSDave Chinner 	int			error;
28057dcf5c3eSDave Chinner 
28067dcf5c3eSDave Chinner 	error = xfs_create_tmpfile(dp, NULL, S_IFCHR | WHITEOUT_MODE, &tmpfile);
28077dcf5c3eSDave Chinner 	if (error)
28087dcf5c3eSDave Chinner 		return error;
28097dcf5c3eSDave Chinner 
281022419ac9SBrian Foster 	/*
281122419ac9SBrian Foster 	 * Prepare the tmpfile inode as if it were created through the VFS.
281222419ac9SBrian Foster 	 * Otherwise, the link increment paths will complain about nlink 0->1.
281322419ac9SBrian Foster 	 * Drop the link count as done by d_tmpfile(), complete the inode setup
281422419ac9SBrian Foster 	 * and flag it as linkable.
281522419ac9SBrian Foster 	 */
281622419ac9SBrian Foster 	drop_nlink(VFS_I(tmpfile));
28177dcf5c3eSDave Chinner 	xfs_finish_inode_setup(tmpfile);
28187dcf5c3eSDave Chinner 	VFS_I(tmpfile)->i_state |= I_LINKABLE;
28197dcf5c3eSDave Chinner 
28207dcf5c3eSDave Chinner 	*wip = tmpfile;
28217dcf5c3eSDave Chinner 	return 0;
28227dcf5c3eSDave Chinner }
28237dcf5c3eSDave Chinner 
28247dcf5c3eSDave Chinner /*
2825f6bba201SDave Chinner  * xfs_rename
2826f6bba201SDave Chinner  */
2827f6bba201SDave Chinner int
2828f6bba201SDave Chinner xfs_rename(
28297dcf5c3eSDave Chinner 	struct xfs_inode	*src_dp,
2830f6bba201SDave Chinner 	struct xfs_name		*src_name,
28317dcf5c3eSDave Chinner 	struct xfs_inode	*src_ip,
28327dcf5c3eSDave Chinner 	struct xfs_inode	*target_dp,
2833f6bba201SDave Chinner 	struct xfs_name		*target_name,
28347dcf5c3eSDave Chinner 	struct xfs_inode	*target_ip,
2835d31a1825SCarlos Maiolino 	unsigned int		flags)
2836f6bba201SDave Chinner {
28377dcf5c3eSDave Chinner 	struct xfs_mount	*mp = src_dp->i_mount;
28387dcf5c3eSDave Chinner 	struct xfs_trans	*tp;
28397dcf5c3eSDave Chinner 	struct xfs_bmap_free	free_list;
2840f6bba201SDave Chinner 	xfs_fsblock_t		first_block;
28417dcf5c3eSDave Chinner 	struct xfs_inode	*wip = NULL;		/* whiteout inode */
28427dcf5c3eSDave Chinner 	struct xfs_inode	*inodes[__XFS_SORT_INODES];
284395afcf5cSDave Chinner 	int			num_inodes = __XFS_SORT_INODES;
28442b93681fSDave Chinner 	bool			new_parent = (src_dp != target_dp);
28452b93681fSDave Chinner 	bool			src_is_directory = S_ISDIR(src_ip->i_d.di_mode);
2846f6bba201SDave Chinner 	int			spaceres;
28477dcf5c3eSDave Chinner 	int			error;
2848f6bba201SDave Chinner 
2849f6bba201SDave Chinner 	trace_xfs_rename(src_dp, target_dp, src_name, target_name);
2850f6bba201SDave Chinner 
2851eeacd321SDave Chinner 	if ((flags & RENAME_EXCHANGE) && !target_ip)
2852eeacd321SDave Chinner 		return -EINVAL;
2853f6bba201SDave Chinner 
28547dcf5c3eSDave Chinner 	/*
28557dcf5c3eSDave Chinner 	 * If we are doing a whiteout operation, allocate the whiteout inode
28567dcf5c3eSDave Chinner 	 * we will be placing at the target and ensure the type is set
28577dcf5c3eSDave Chinner 	 * appropriately.
28587dcf5c3eSDave Chinner 	 */
28597dcf5c3eSDave Chinner 	if (flags & RENAME_WHITEOUT) {
28607dcf5c3eSDave Chinner 		ASSERT(!(flags & (RENAME_NOREPLACE | RENAME_EXCHANGE)));
28617dcf5c3eSDave Chinner 		error = xfs_rename_alloc_whiteout(target_dp, &wip);
28627dcf5c3eSDave Chinner 		if (error)
28637dcf5c3eSDave Chinner 			return error;
2864f6bba201SDave Chinner 
28657dcf5c3eSDave Chinner 		/* setup target dirent info as whiteout */
28667dcf5c3eSDave Chinner 		src_name->type = XFS_DIR3_FT_CHRDEV;
28677dcf5c3eSDave Chinner 	}
28687dcf5c3eSDave Chinner 
28697dcf5c3eSDave Chinner 	xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip, wip,
2870f6bba201SDave Chinner 				inodes, &num_inodes);
2871f6bba201SDave Chinner 
2872f6bba201SDave Chinner 	tp = xfs_trans_alloc(mp, XFS_TRANS_RENAME);
2873f6bba201SDave Chinner 	spaceres = XFS_RENAME_SPACE_RES(mp, target_name->len);
28743d3c8b52SJie Liu 	error = xfs_trans_reserve(tp, &M_RES(mp)->tr_rename, spaceres, 0);
28752451337dSDave Chinner 	if (error == -ENOSPC) {
2876f6bba201SDave Chinner 		spaceres = 0;
28773d3c8b52SJie Liu 		error = xfs_trans_reserve(tp, &M_RES(mp)->tr_rename, 0, 0);
2878f6bba201SDave Chinner 	}
2879445883e8SDave Chinner 	if (error)
2880445883e8SDave Chinner 		goto out_trans_cancel;
2881f6bba201SDave Chinner 
2882f6bba201SDave Chinner 	/*
2883f6bba201SDave Chinner 	 * Attach the dquots to the inodes
2884f6bba201SDave Chinner 	 */
2885f6bba201SDave Chinner 	error = xfs_qm_vop_rename_dqattach(inodes);
2886445883e8SDave Chinner 	if (error)
2887445883e8SDave Chinner 		goto out_trans_cancel;
2888f6bba201SDave Chinner 
2889f6bba201SDave Chinner 	/*
2890f6bba201SDave Chinner 	 * Lock all the participating inodes. Depending upon whether
2891f6bba201SDave Chinner 	 * the target_name exists in the target directory, and
2892f6bba201SDave Chinner 	 * whether the target directory is the same as the source
2893f6bba201SDave Chinner 	 * directory, we can lock from 2 to 4 inodes.
2894f6bba201SDave Chinner 	 */
2895f6bba201SDave Chinner 	xfs_lock_inodes(inodes, num_inodes, XFS_ILOCK_EXCL);
2896f6bba201SDave Chinner 
2897f6bba201SDave Chinner 	/*
2898f6bba201SDave Chinner 	 * Join all the inodes to the transaction. From this point on,
2899f6bba201SDave Chinner 	 * we can rely on either trans_commit or trans_cancel to unlock
2900f6bba201SDave Chinner 	 * them.
2901f6bba201SDave Chinner 	 */
2902f6bba201SDave Chinner 	xfs_trans_ijoin(tp, src_dp, XFS_ILOCK_EXCL);
2903f6bba201SDave Chinner 	if (new_parent)
2904f6bba201SDave Chinner 		xfs_trans_ijoin(tp, target_dp, XFS_ILOCK_EXCL);
2905f6bba201SDave Chinner 	xfs_trans_ijoin(tp, src_ip, XFS_ILOCK_EXCL);
2906f6bba201SDave Chinner 	if (target_ip)
2907f6bba201SDave Chinner 		xfs_trans_ijoin(tp, target_ip, XFS_ILOCK_EXCL);
29087dcf5c3eSDave Chinner 	if (wip)
29097dcf5c3eSDave Chinner 		xfs_trans_ijoin(tp, wip, XFS_ILOCK_EXCL);
2910f6bba201SDave Chinner 
2911f6bba201SDave Chinner 	/*
2912f6bba201SDave Chinner 	 * If we are using project inheritance, we only allow renames
2913f6bba201SDave Chinner 	 * into our tree when the project IDs are the same; else the
2914f6bba201SDave Chinner 	 * tree quota mechanism would be circumvented.
2915f6bba201SDave Chinner 	 */
2916f6bba201SDave Chinner 	if (unlikely((target_dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2917f6bba201SDave Chinner 		     (xfs_get_projid(target_dp) != xfs_get_projid(src_ip)))) {
29182451337dSDave Chinner 		error = -EXDEV;
2919445883e8SDave Chinner 		goto out_trans_cancel;
2920f6bba201SDave Chinner 	}
2921f6bba201SDave Chinner 
2922445883e8SDave Chinner 	xfs_bmap_init(&free_list, &first_block);
2923445883e8SDave Chinner 
2924eeacd321SDave Chinner 	/* RENAME_EXCHANGE is unique from here on. */
2925eeacd321SDave Chinner 	if (flags & RENAME_EXCHANGE)
2926eeacd321SDave Chinner 		return xfs_cross_rename(tp, src_dp, src_name, src_ip,
2927d31a1825SCarlos Maiolino 					target_dp, target_name, target_ip,
2928d31a1825SCarlos Maiolino 					&free_list, &first_block, spaceres);
2929d31a1825SCarlos Maiolino 
2930d31a1825SCarlos Maiolino 	/*
2931f6bba201SDave Chinner 	 * Set up the target.
2932f6bba201SDave Chinner 	 */
2933f6bba201SDave Chinner 	if (target_ip == NULL) {
2934f6bba201SDave Chinner 		/*
2935f6bba201SDave Chinner 		 * If there's no space reservation, check the entry will
2936f6bba201SDave Chinner 		 * fit before actually inserting it.
2937f6bba201SDave Chinner 		 */
293894f3cad5SEric Sandeen 		if (!spaceres) {
293994f3cad5SEric Sandeen 			error = xfs_dir_canenter(tp, target_dp, target_name);
2940f6bba201SDave Chinner 			if (error)
2941445883e8SDave Chinner 				goto out_trans_cancel;
294294f3cad5SEric Sandeen 		}
2943f6bba201SDave Chinner 		/*
2944f6bba201SDave Chinner 		 * If target does not exist and the rename crosses
2945f6bba201SDave Chinner 		 * directories, adjust the target directory link count
2946f6bba201SDave Chinner 		 * to account for the ".." reference from the new entry.
2947f6bba201SDave Chinner 		 */
2948f6bba201SDave Chinner 		error = xfs_dir_createname(tp, target_dp, target_name,
2949f6bba201SDave Chinner 						src_ip->i_ino, &first_block,
2950f6bba201SDave Chinner 						&free_list, spaceres);
2951f6bba201SDave Chinner 		if (error)
29524906e215SChristoph Hellwig 			goto out_bmap_cancel;
2953f6bba201SDave Chinner 
2954f6bba201SDave Chinner 		xfs_trans_ichgtime(tp, target_dp,
2955f6bba201SDave Chinner 					XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2956f6bba201SDave Chinner 
2957f6bba201SDave Chinner 		if (new_parent && src_is_directory) {
2958f6bba201SDave Chinner 			error = xfs_bumplink(tp, target_dp);
2959f6bba201SDave Chinner 			if (error)
29604906e215SChristoph Hellwig 				goto out_bmap_cancel;
2961f6bba201SDave Chinner 		}
2962f6bba201SDave Chinner 	} else { /* target_ip != NULL */
2963f6bba201SDave Chinner 		/*
2964f6bba201SDave Chinner 		 * If target exists and it's a directory, check that both
2965f6bba201SDave Chinner 		 * target and source are directories and that target can be
2966f6bba201SDave Chinner 		 * destroyed, or that neither is a directory.
2967f6bba201SDave Chinner 		 */
2968f6bba201SDave Chinner 		if (S_ISDIR(target_ip->i_d.di_mode)) {
2969f6bba201SDave Chinner 			/*
2970f6bba201SDave Chinner 			 * Make sure target dir is empty.
2971f6bba201SDave Chinner 			 */
2972f6bba201SDave Chinner 			if (!(xfs_dir_isempty(target_ip)) ||
2973f6bba201SDave Chinner 			    (target_ip->i_d.di_nlink > 2)) {
29742451337dSDave Chinner 				error = -EEXIST;
2975445883e8SDave Chinner 				goto out_trans_cancel;
2976f6bba201SDave Chinner 			}
2977f6bba201SDave Chinner 		}
2978f6bba201SDave Chinner 
2979f6bba201SDave Chinner 		/*
2980f6bba201SDave Chinner 		 * Link the source inode under the target name.
2981f6bba201SDave Chinner 		 * If the source inode is a directory and we are moving
2982f6bba201SDave Chinner 		 * it across directories, its ".." entry will be
2983f6bba201SDave Chinner 		 * inconsistent until we replace that down below.
2984f6bba201SDave Chinner 		 *
2985f6bba201SDave Chinner 		 * In case there is already an entry with the same
2986f6bba201SDave Chinner 		 * name at the destination directory, remove it first.
2987f6bba201SDave Chinner 		 */
2988f6bba201SDave Chinner 		error = xfs_dir_replace(tp, target_dp, target_name,
2989f6bba201SDave Chinner 					src_ip->i_ino,
2990f6bba201SDave Chinner 					&first_block, &free_list, spaceres);
2991f6bba201SDave Chinner 		if (error)
29924906e215SChristoph Hellwig 			goto out_bmap_cancel;
2993f6bba201SDave Chinner 
2994f6bba201SDave Chinner 		xfs_trans_ichgtime(tp, target_dp,
2995f6bba201SDave Chinner 					XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2996f6bba201SDave Chinner 
2997f6bba201SDave Chinner 		/*
2998f6bba201SDave Chinner 		 * Decrement the link count on the target since the target
2999f6bba201SDave Chinner 		 * dir no longer points to it.
3000f6bba201SDave Chinner 		 */
3001f6bba201SDave Chinner 		error = xfs_droplink(tp, target_ip);
3002f6bba201SDave Chinner 		if (error)
30034906e215SChristoph Hellwig 			goto out_bmap_cancel;
3004f6bba201SDave Chinner 
3005f6bba201SDave Chinner 		if (src_is_directory) {
3006f6bba201SDave Chinner 			/*
3007f6bba201SDave Chinner 			 * Drop the link from the old "." entry.
3008f6bba201SDave Chinner 			 */
3009f6bba201SDave Chinner 			error = xfs_droplink(tp, target_ip);
3010f6bba201SDave Chinner 			if (error)
30114906e215SChristoph Hellwig 				goto out_bmap_cancel;
3012f6bba201SDave Chinner 		}
3013f6bba201SDave Chinner 	} /* target_ip != NULL */
3014f6bba201SDave Chinner 
3015f6bba201SDave Chinner 	/*
3016f6bba201SDave Chinner 	 * Remove the source.
3017f6bba201SDave Chinner 	 */
3018f6bba201SDave Chinner 	if (new_parent && src_is_directory) {
3019f6bba201SDave Chinner 		/*
3020f6bba201SDave Chinner 		 * Rewrite the ".." entry to point to the new
3021f6bba201SDave Chinner 		 * directory.
3022f6bba201SDave Chinner 		 */
3023f6bba201SDave Chinner 		error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot,
3024f6bba201SDave Chinner 					target_dp->i_ino,
3025f6bba201SDave Chinner 					&first_block, &free_list, spaceres);
30262451337dSDave Chinner 		ASSERT(error != -EEXIST);
3027f6bba201SDave Chinner 		if (error)
30284906e215SChristoph Hellwig 			goto out_bmap_cancel;
3029f6bba201SDave Chinner 	}
3030f6bba201SDave Chinner 
3031f6bba201SDave Chinner 	/*
3032f6bba201SDave Chinner 	 * We always want to hit the ctime on the source inode.
3033f6bba201SDave Chinner 	 *
3034f6bba201SDave Chinner 	 * This isn't strictly required by the standards since the source
3035f6bba201SDave Chinner 	 * inode isn't really being changed, but old unix file systems did
3036f6bba201SDave Chinner 	 * it and some incremental backup programs won't work without it.
3037f6bba201SDave Chinner 	 */
3038f6bba201SDave Chinner 	xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG);
3039f6bba201SDave Chinner 	xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE);
3040f6bba201SDave Chinner 
3041f6bba201SDave Chinner 	/*
3042f6bba201SDave Chinner 	 * Adjust the link count on src_dp.  This is necessary when
3043f6bba201SDave Chinner 	 * renaming a directory, either within one parent when
3044f6bba201SDave Chinner 	 * the target existed, or across two parent directories.
3045f6bba201SDave Chinner 	 */
3046f6bba201SDave Chinner 	if (src_is_directory && (new_parent || target_ip != NULL)) {
3047f6bba201SDave Chinner 
3048f6bba201SDave Chinner 		/*
3049f6bba201SDave Chinner 		 * Decrement link count on src_directory since the
3050f6bba201SDave Chinner 		 * entry that's moved no longer points to it.
3051f6bba201SDave Chinner 		 */
3052f6bba201SDave Chinner 		error = xfs_droplink(tp, src_dp);
3053f6bba201SDave Chinner 		if (error)
30544906e215SChristoph Hellwig 			goto out_bmap_cancel;
3055f6bba201SDave Chinner 	}
3056f6bba201SDave Chinner 
30577dcf5c3eSDave Chinner 	/*
30587dcf5c3eSDave Chinner 	 * For whiteouts, we only need to update the source dirent with the
30597dcf5c3eSDave Chinner 	 * inode number of the whiteout inode rather than removing it
30607dcf5c3eSDave Chinner 	 * altogether.
30617dcf5c3eSDave Chinner 	 */
30627dcf5c3eSDave Chinner 	if (wip) {
30637dcf5c3eSDave Chinner 		error = xfs_dir_replace(tp, src_dp, src_name, wip->i_ino,
30647dcf5c3eSDave Chinner 					&first_block, &free_list, spaceres);
30657dcf5c3eSDave Chinner 	} else
3066f6bba201SDave Chinner 		error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino,
3067f6bba201SDave Chinner 					   &first_block, &free_list, spaceres);
3068f6bba201SDave Chinner 	if (error)
30694906e215SChristoph Hellwig 		goto out_bmap_cancel;
3070f6bba201SDave Chinner 
30717dcf5c3eSDave Chinner 	/*
30727dcf5c3eSDave Chinner 	 * For whiteouts, we need to bump the link count on the whiteout inode.
30737dcf5c3eSDave Chinner 	 * This means that failures all the way up to this point leave the inode
30747dcf5c3eSDave Chinner 	 * on the unlinked list and so cleanup is a simple matter of dropping
30757dcf5c3eSDave Chinner 	 * the remaining reference to it. If we fail here after bumping the link
30767dcf5c3eSDave Chinner 	 * count, we're shutting down the filesystem so we'll never see the
30777dcf5c3eSDave Chinner 	 * intermediate state on disk.
30787dcf5c3eSDave Chinner 	 */
30797dcf5c3eSDave Chinner 	if (wip) {
308022419ac9SBrian Foster 		ASSERT(VFS_I(wip)->i_nlink == 0 && wip->i_d.di_nlink == 0);
30817dcf5c3eSDave Chinner 		error = xfs_bumplink(tp, wip);
30827dcf5c3eSDave Chinner 		if (error)
30834906e215SChristoph Hellwig 			goto out_bmap_cancel;
30847dcf5c3eSDave Chinner 		error = xfs_iunlink_remove(tp, wip);
30857dcf5c3eSDave Chinner 		if (error)
30864906e215SChristoph Hellwig 			goto out_bmap_cancel;
30877dcf5c3eSDave Chinner 		xfs_trans_log_inode(tp, wip, XFS_ILOG_CORE);
30887dcf5c3eSDave Chinner 
30897dcf5c3eSDave Chinner 		/*
30907dcf5c3eSDave Chinner 		 * Now we have a real link, clear the "I'm a tmpfile" state
30917dcf5c3eSDave Chinner 		 * flag from the inode so it doesn't accidentally get misused in
30927dcf5c3eSDave Chinner 		 * future.
30937dcf5c3eSDave Chinner 		 */
30947dcf5c3eSDave Chinner 		VFS_I(wip)->i_state &= ~I_LINKABLE;
30957dcf5c3eSDave Chinner 	}
3096f6bba201SDave Chinner 
3097f6bba201SDave Chinner 	xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3098f6bba201SDave Chinner 	xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE);
3099f6bba201SDave Chinner 	if (new_parent)
3100f6bba201SDave Chinner 		xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE);
3101f6bba201SDave Chinner 
31027dcf5c3eSDave Chinner 	error = xfs_finish_rename(tp, &free_list);
31037dcf5c3eSDave Chinner 	if (wip)
31047dcf5c3eSDave Chinner 		IRELE(wip);
31057dcf5c3eSDave Chinner 	return error;
3106f6bba201SDave Chinner 
3107445883e8SDave Chinner out_bmap_cancel:
3108f6bba201SDave Chinner 	xfs_bmap_cancel(&free_list);
3109445883e8SDave Chinner out_trans_cancel:
31104906e215SChristoph Hellwig 	xfs_trans_cancel(tp);
31117dcf5c3eSDave Chinner 	if (wip)
31127dcf5c3eSDave Chinner 		IRELE(wip);
3113f6bba201SDave Chinner 	return error;
3114f6bba201SDave Chinner }
3115f6bba201SDave Chinner 
3116bad55843SDavid Chinner STATIC int
3117bad55843SDavid Chinner xfs_iflush_cluster(
3118bad55843SDavid Chinner 	xfs_inode_t	*ip,
3119bad55843SDavid Chinner 	xfs_buf_t	*bp)
3120bad55843SDavid Chinner {
3121bad55843SDavid Chinner 	xfs_mount_t		*mp = ip->i_mount;
31225017e97dSDave Chinner 	struct xfs_perag	*pag;
3123bad55843SDavid Chinner 	unsigned long		first_index, mask;
3124c8f5f12eSDavid Chinner 	unsigned long		inodes_per_cluster;
3125bad55843SDavid Chinner 	int			ilist_size;
3126bad55843SDavid Chinner 	xfs_inode_t		**ilist;
3127bad55843SDavid Chinner 	xfs_inode_t		*iq;
3128bad55843SDavid Chinner 	int			nr_found;
3129bad55843SDavid Chinner 	int			clcount = 0;
3130bad55843SDavid Chinner 	int			bufwasdelwri;
3131bad55843SDavid Chinner 	int			i;
3132bad55843SDavid Chinner 
31335017e97dSDave Chinner 	pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
3134bad55843SDavid Chinner 
31350f49efd8SJie Liu 	inodes_per_cluster = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog;
3136c8f5f12eSDavid Chinner 	ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
313749383b0eSDavid Chinner 	ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS);
3138bad55843SDavid Chinner 	if (!ilist)
313944b56e0aSDave Chinner 		goto out_put;
3140bad55843SDavid Chinner 
31410f49efd8SJie Liu 	mask = ~(((mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog)) - 1);
3142bad55843SDavid Chinner 	first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
31431a3e8f3dSDave Chinner 	rcu_read_lock();
3144bad55843SDavid Chinner 	/* really need a gang lookup range call here */
3145bad55843SDavid Chinner 	nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist,
3146c8f5f12eSDavid Chinner 					first_index, inodes_per_cluster);
3147bad55843SDavid Chinner 	if (nr_found == 0)
3148bad55843SDavid Chinner 		goto out_free;
3149bad55843SDavid Chinner 
3150bad55843SDavid Chinner 	for (i = 0; i < nr_found; i++) {
3151bad55843SDavid Chinner 		iq = ilist[i];
3152bad55843SDavid Chinner 		if (iq == ip)
3153bad55843SDavid Chinner 			continue;
31541a3e8f3dSDave Chinner 
31551a3e8f3dSDave Chinner 		/*
31561a3e8f3dSDave Chinner 		 * because this is an RCU protected lookup, we could find a
31571a3e8f3dSDave Chinner 		 * recently freed or even reallocated inode during the lookup.
31581a3e8f3dSDave Chinner 		 * We need to check under the i_flags_lock for a valid inode
31591a3e8f3dSDave Chinner 		 * here. Skip it if it is not valid or the wrong inode.
31601a3e8f3dSDave Chinner 		 */
31611a3e8f3dSDave Chinner 		spin_lock(&ip->i_flags_lock);
31621a3e8f3dSDave Chinner 		if (!ip->i_ino ||
31631a3e8f3dSDave Chinner 		    (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) {
31641a3e8f3dSDave Chinner 			spin_unlock(&ip->i_flags_lock);
31651a3e8f3dSDave Chinner 			continue;
31661a3e8f3dSDave Chinner 		}
31671a3e8f3dSDave Chinner 		spin_unlock(&ip->i_flags_lock);
31681a3e8f3dSDave Chinner 
3169bad55843SDavid Chinner 		/*
3170bad55843SDavid Chinner 		 * Do an un-protected check to see if the inode is dirty and
3171bad55843SDavid Chinner 		 * is a candidate for flushing.  These checks will be repeated
3172bad55843SDavid Chinner 		 * later after the appropriate locks are acquired.
3173bad55843SDavid Chinner 		 */
317433540408SDavid Chinner 		if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0)
3175bad55843SDavid Chinner 			continue;
3176bad55843SDavid Chinner 
3177bad55843SDavid Chinner 		/*
3178bad55843SDavid Chinner 		 * Try to get locks.  If any are unavailable or it is pinned,
3179bad55843SDavid Chinner 		 * then this inode cannot be flushed and is skipped.
3180bad55843SDavid Chinner 		 */
3181bad55843SDavid Chinner 
3182bad55843SDavid Chinner 		if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED))
3183bad55843SDavid Chinner 			continue;
3184bad55843SDavid Chinner 		if (!xfs_iflock_nowait(iq)) {
3185bad55843SDavid Chinner 			xfs_iunlock(iq, XFS_ILOCK_SHARED);
3186bad55843SDavid Chinner 			continue;
3187bad55843SDavid Chinner 		}
3188bad55843SDavid Chinner 		if (xfs_ipincount(iq)) {
3189bad55843SDavid Chinner 			xfs_ifunlock(iq);
3190bad55843SDavid Chinner 			xfs_iunlock(iq, XFS_ILOCK_SHARED);
3191bad55843SDavid Chinner 			continue;
3192bad55843SDavid Chinner 		}
3193bad55843SDavid Chinner 
3194bad55843SDavid Chinner 		/*
3195bad55843SDavid Chinner 		 * arriving here means that this inode can be flushed.  First
3196bad55843SDavid Chinner 		 * re-check that it's dirty before flushing.
3197bad55843SDavid Chinner 		 */
319833540408SDavid Chinner 		if (!xfs_inode_clean(iq)) {
3199bad55843SDavid Chinner 			int	error;
3200bad55843SDavid Chinner 			error = xfs_iflush_int(iq, bp);
3201bad55843SDavid Chinner 			if (error) {
3202bad55843SDavid Chinner 				xfs_iunlock(iq, XFS_ILOCK_SHARED);
3203bad55843SDavid Chinner 				goto cluster_corrupt_out;
3204bad55843SDavid Chinner 			}
3205bad55843SDavid Chinner 			clcount++;
3206bad55843SDavid Chinner 		} else {
3207bad55843SDavid Chinner 			xfs_ifunlock(iq);
3208bad55843SDavid Chinner 		}
3209bad55843SDavid Chinner 		xfs_iunlock(iq, XFS_ILOCK_SHARED);
3210bad55843SDavid Chinner 	}
3211bad55843SDavid Chinner 
3212bad55843SDavid Chinner 	if (clcount) {
3213bad55843SDavid Chinner 		XFS_STATS_INC(xs_icluster_flushcnt);
3214bad55843SDavid Chinner 		XFS_STATS_ADD(xs_icluster_flushinode, clcount);
3215bad55843SDavid Chinner 	}
3216bad55843SDavid Chinner 
3217bad55843SDavid Chinner out_free:
32181a3e8f3dSDave Chinner 	rcu_read_unlock();
3219f0e2d93cSDenys Vlasenko 	kmem_free(ilist);
322044b56e0aSDave Chinner out_put:
322144b56e0aSDave Chinner 	xfs_perag_put(pag);
3222bad55843SDavid Chinner 	return 0;
3223bad55843SDavid Chinner 
3224bad55843SDavid Chinner 
3225bad55843SDavid Chinner cluster_corrupt_out:
3226bad55843SDavid Chinner 	/*
3227bad55843SDavid Chinner 	 * Corruption detected in the clustering loop.  Invalidate the
3228bad55843SDavid Chinner 	 * inode buffer and shut down the filesystem.
3229bad55843SDavid Chinner 	 */
32301a3e8f3dSDave Chinner 	rcu_read_unlock();
3231bad55843SDavid Chinner 	/*
323243ff2122SChristoph Hellwig 	 * Clean up the buffer.  If it was delwri, just release it --
3233bad55843SDavid Chinner 	 * brelse can handle it with no problems.  If not, shut down the
3234bad55843SDavid Chinner 	 * filesystem before releasing the buffer.
3235bad55843SDavid Chinner 	 */
323643ff2122SChristoph Hellwig 	bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q);
3237bad55843SDavid Chinner 	if (bufwasdelwri)
3238bad55843SDavid Chinner 		xfs_buf_relse(bp);
3239bad55843SDavid Chinner 
3240bad55843SDavid Chinner 	xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
3241bad55843SDavid Chinner 
3242bad55843SDavid Chinner 	if (!bufwasdelwri) {
3243bad55843SDavid Chinner 		/*
3244bad55843SDavid Chinner 		 * Just like incore_relse: if we have b_iodone functions,
3245bad55843SDavid Chinner 		 * mark the buffer as an error and call them.  Otherwise
3246bad55843SDavid Chinner 		 * mark it as stale and brelse.
3247bad55843SDavid Chinner 		 */
3248cb669ca5SChristoph Hellwig 		if (bp->b_iodone) {
3249bad55843SDavid Chinner 			XFS_BUF_UNDONE(bp);
3250c867cb61SChristoph Hellwig 			xfs_buf_stale(bp);
32512451337dSDave Chinner 			xfs_buf_ioerror(bp, -EIO);
3252e8aaba9aSDave Chinner 			xfs_buf_ioend(bp);
3253bad55843SDavid Chinner 		} else {
3254c867cb61SChristoph Hellwig 			xfs_buf_stale(bp);
3255bad55843SDavid Chinner 			xfs_buf_relse(bp);
3256bad55843SDavid Chinner 		}
3257bad55843SDavid Chinner 	}
3258bad55843SDavid Chinner 
3259bad55843SDavid Chinner 	/*
3260bad55843SDavid Chinner 	 * Unlocks the flush lock
3261bad55843SDavid Chinner 	 */
326204913fddSDave Chinner 	xfs_iflush_abort(iq, false);
3263f0e2d93cSDenys Vlasenko 	kmem_free(ilist);
326444b56e0aSDave Chinner 	xfs_perag_put(pag);
32652451337dSDave Chinner 	return -EFSCORRUPTED;
3266bad55843SDavid Chinner }
3267bad55843SDavid Chinner 
32681da177e4SLinus Torvalds /*
32694c46819aSChristoph Hellwig  * Flush dirty inode metadata into the backing buffer.
32704c46819aSChristoph Hellwig  *
32714c46819aSChristoph Hellwig  * The caller must have the inode lock and the inode flush lock held.  The
32724c46819aSChristoph Hellwig  * inode lock will still be held upon return to the caller, and the inode
32734c46819aSChristoph Hellwig  * flush lock will be released after the inode has reached the disk.
32744c46819aSChristoph Hellwig  *
32754c46819aSChristoph Hellwig  * The caller must write out the buffer returned in *bpp and release it.
32761da177e4SLinus Torvalds  */
32771da177e4SLinus Torvalds int
32781da177e4SLinus Torvalds xfs_iflush(
32794c46819aSChristoph Hellwig 	struct xfs_inode	*ip,
32804c46819aSChristoph Hellwig 	struct xfs_buf		**bpp)
32811da177e4SLinus Torvalds {
32824c46819aSChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
32834c46819aSChristoph Hellwig 	struct xfs_buf		*bp;
32844c46819aSChristoph Hellwig 	struct xfs_dinode	*dip;
32851da177e4SLinus Torvalds 	int			error;
32861da177e4SLinus Torvalds 
32871da177e4SLinus Torvalds 	XFS_STATS_INC(xs_iflush_count);
32881da177e4SLinus Torvalds 
3289579aa9caSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
3290474fce06SChristoph Hellwig 	ASSERT(xfs_isiflocked(ip));
32911da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
32928096b1ebSChristoph Hellwig 	       ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
32931da177e4SLinus Torvalds 
32944c46819aSChristoph Hellwig 	*bpp = NULL;
32951da177e4SLinus Torvalds 
32961da177e4SLinus Torvalds 	xfs_iunpin_wait(ip);
32971da177e4SLinus Torvalds 
32981da177e4SLinus Torvalds 	/*
32994b6a4688SDave Chinner 	 * For stale inodes we cannot rely on the backing buffer remaining
33004b6a4688SDave Chinner 	 * stale in cache for the remaining life of the stale inode and so
3301475ee413SChristoph Hellwig 	 * xfs_imap_to_bp() below may give us a buffer that no longer contains
33024b6a4688SDave Chinner 	 * inodes below. We have to check this after ensuring the inode is
33034b6a4688SDave Chinner 	 * unpinned so that it is safe to reclaim the stale inode after the
33044b6a4688SDave Chinner 	 * flush call.
33054b6a4688SDave Chinner 	 */
33064b6a4688SDave Chinner 	if (xfs_iflags_test(ip, XFS_ISTALE)) {
33074b6a4688SDave Chinner 		xfs_ifunlock(ip);
33084b6a4688SDave Chinner 		return 0;
33094b6a4688SDave Chinner 	}
33104b6a4688SDave Chinner 
33114b6a4688SDave Chinner 	/*
33121da177e4SLinus Torvalds 	 * This may have been unpinned because the filesystem is shutting
33131da177e4SLinus Torvalds 	 * down forcibly. If that's the case we must not write this inode
331432ce90a4SChristoph Hellwig 	 * to disk, because the log record didn't make it to disk.
331532ce90a4SChristoph Hellwig 	 *
331632ce90a4SChristoph Hellwig 	 * We also have to remove the log item from the AIL in this case,
331732ce90a4SChristoph Hellwig 	 * as we wait for an empty AIL as part of the unmount process.
33181da177e4SLinus Torvalds 	 */
33191da177e4SLinus Torvalds 	if (XFS_FORCED_SHUTDOWN(mp)) {
33202451337dSDave Chinner 		error = -EIO;
332132ce90a4SChristoph Hellwig 		goto abort_out;
33221da177e4SLinus Torvalds 	}
33231da177e4SLinus Torvalds 
33241da177e4SLinus Torvalds 	/*
3325a3f74ffbSDavid Chinner 	 * Get the buffer containing the on-disk inode.
3326a3f74ffbSDavid Chinner 	 */
3327475ee413SChristoph Hellwig 	error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK,
3328475ee413SChristoph Hellwig 			       0);
3329a3f74ffbSDavid Chinner 	if (error || !bp) {
3330a3f74ffbSDavid Chinner 		xfs_ifunlock(ip);
3331a3f74ffbSDavid Chinner 		return error;
3332a3f74ffbSDavid Chinner 	}
3333a3f74ffbSDavid Chinner 
3334a3f74ffbSDavid Chinner 	/*
33351da177e4SLinus Torvalds 	 * First flush out the inode that xfs_iflush was called with.
33361da177e4SLinus Torvalds 	 */
33371da177e4SLinus Torvalds 	error = xfs_iflush_int(ip, bp);
3338bad55843SDavid Chinner 	if (error)
33391da177e4SLinus Torvalds 		goto corrupt_out;
33401da177e4SLinus Torvalds 
33411da177e4SLinus Torvalds 	/*
3342a3f74ffbSDavid Chinner 	 * If the buffer is pinned then push on the log now so we won't
3343a3f74ffbSDavid Chinner 	 * get stuck waiting in the write for too long.
3344a3f74ffbSDavid Chinner 	 */
3345811e64c7SChandra Seetharaman 	if (xfs_buf_ispinned(bp))
3346a14a348bSChristoph Hellwig 		xfs_log_force(mp, 0);
3347a3f74ffbSDavid Chinner 
3348a3f74ffbSDavid Chinner 	/*
33491da177e4SLinus Torvalds 	 * inode clustering:
33501da177e4SLinus Torvalds 	 * see if other inodes can be gathered into this write
33511da177e4SLinus Torvalds 	 */
3352bad55843SDavid Chinner 	error = xfs_iflush_cluster(ip, bp);
3353bad55843SDavid Chinner 	if (error)
33541da177e4SLinus Torvalds 		goto cluster_corrupt_out;
33551da177e4SLinus Torvalds 
33564c46819aSChristoph Hellwig 	*bpp = bp;
33574c46819aSChristoph Hellwig 	return 0;
33581da177e4SLinus Torvalds 
33591da177e4SLinus Torvalds corrupt_out:
33601da177e4SLinus Torvalds 	xfs_buf_relse(bp);
33617d04a335SNathan Scott 	xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
33621da177e4SLinus Torvalds cluster_corrupt_out:
33632451337dSDave Chinner 	error = -EFSCORRUPTED;
336432ce90a4SChristoph Hellwig abort_out:
33651da177e4SLinus Torvalds 	/*
33661da177e4SLinus Torvalds 	 * Unlocks the flush lock
33671da177e4SLinus Torvalds 	 */
336804913fddSDave Chinner 	xfs_iflush_abort(ip, false);
336932ce90a4SChristoph Hellwig 	return error;
33701da177e4SLinus Torvalds }
33711da177e4SLinus Torvalds 
33721da177e4SLinus Torvalds STATIC int
33731da177e4SLinus Torvalds xfs_iflush_int(
337493848a99SChristoph Hellwig 	struct xfs_inode	*ip,
337593848a99SChristoph Hellwig 	struct xfs_buf		*bp)
33761da177e4SLinus Torvalds {
337793848a99SChristoph Hellwig 	struct xfs_inode_log_item *iip = ip->i_itemp;
337893848a99SChristoph Hellwig 	struct xfs_dinode	*dip;
337993848a99SChristoph Hellwig 	struct xfs_mount	*mp = ip->i_mount;
33801da177e4SLinus Torvalds 
3381579aa9caSChristoph Hellwig 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
3382474fce06SChristoph Hellwig 	ASSERT(xfs_isiflocked(ip));
33831da177e4SLinus Torvalds 	ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
33848096b1ebSChristoph Hellwig 	       ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
338593848a99SChristoph Hellwig 	ASSERT(iip != NULL && iip->ili_fields != 0);
3386263997a6SDave Chinner 	ASSERT(ip->i_d.di_version > 1);
33871da177e4SLinus Torvalds 
33881da177e4SLinus Torvalds 	/* set *dip = inode's place in the buffer */
338988ee2df7SChristoph Hellwig 	dip = xfs_buf_offset(bp, ip->i_imap.im_boffset);
33901da177e4SLinus Torvalds 
339169ef921bSChristoph Hellwig 	if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
33921da177e4SLinus Torvalds 			       mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
33936a19d939SDave Chinner 		xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
33946a19d939SDave Chinner 			"%s: Bad inode %Lu magic number 0x%x, ptr 0x%p",
33956a19d939SDave Chinner 			__func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
33961da177e4SLinus Torvalds 		goto corrupt_out;
33971da177e4SLinus Torvalds 	}
33981da177e4SLinus Torvalds 	if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
33991da177e4SLinus Torvalds 				mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
34006a19d939SDave Chinner 		xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
34016a19d939SDave Chinner 			"%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
34026a19d939SDave Chinner 			__func__, ip->i_ino, ip, ip->i_d.di_magic);
34031da177e4SLinus Torvalds 		goto corrupt_out;
34041da177e4SLinus Torvalds 	}
3405abbede1bSAl Viro 	if (S_ISREG(ip->i_d.di_mode)) {
34061da177e4SLinus Torvalds 		if (XFS_TEST_ERROR(
34071da177e4SLinus Torvalds 		    (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
34081da177e4SLinus Torvalds 		    (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
34091da177e4SLinus Torvalds 		    mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
34106a19d939SDave Chinner 			xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
34116a19d939SDave Chinner 				"%s: Bad regular inode %Lu, ptr 0x%p",
34126a19d939SDave Chinner 				__func__, ip->i_ino, ip);
34131da177e4SLinus Torvalds 			goto corrupt_out;
34141da177e4SLinus Torvalds 		}
3415abbede1bSAl Viro 	} else if (S_ISDIR(ip->i_d.di_mode)) {
34161da177e4SLinus Torvalds 		if (XFS_TEST_ERROR(
34171da177e4SLinus Torvalds 		    (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
34181da177e4SLinus Torvalds 		    (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
34191da177e4SLinus Torvalds 		    (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
34201da177e4SLinus Torvalds 		    mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
34216a19d939SDave Chinner 			xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
34226a19d939SDave Chinner 				"%s: Bad directory inode %Lu, ptr 0x%p",
34236a19d939SDave Chinner 				__func__, ip->i_ino, ip);
34241da177e4SLinus Torvalds 			goto corrupt_out;
34251da177e4SLinus Torvalds 		}
34261da177e4SLinus Torvalds 	}
34271da177e4SLinus Torvalds 	if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
34281da177e4SLinus Torvalds 				ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
34291da177e4SLinus Torvalds 				XFS_RANDOM_IFLUSH_5)) {
34306a19d939SDave Chinner 		xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
34316a19d939SDave Chinner 			"%s: detected corrupt incore inode %Lu, "
34326a19d939SDave Chinner 			"total extents = %d, nblocks = %Ld, ptr 0x%p",
34336a19d939SDave Chinner 			__func__, ip->i_ino,
34341da177e4SLinus Torvalds 			ip->i_d.di_nextents + ip->i_d.di_anextents,
34356a19d939SDave Chinner 			ip->i_d.di_nblocks, ip);
34361da177e4SLinus Torvalds 		goto corrupt_out;
34371da177e4SLinus Torvalds 	}
34381da177e4SLinus Torvalds 	if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
34391da177e4SLinus Torvalds 				mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
34406a19d939SDave Chinner 		xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
34416a19d939SDave Chinner 			"%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
34426a19d939SDave Chinner 			__func__, ip->i_ino, ip->i_d.di_forkoff, ip);
34431da177e4SLinus Torvalds 		goto corrupt_out;
34441da177e4SLinus Torvalds 	}
3445e60896d8SDave Chinner 
34461da177e4SLinus Torvalds 	/*
3447263997a6SDave Chinner 	 * Inode item log recovery for v2 inodes are dependent on the
3448e60896d8SDave Chinner 	 * di_flushiter count for correct sequencing. We bump the flush
3449e60896d8SDave Chinner 	 * iteration count so we can detect flushes which postdate a log record
3450e60896d8SDave Chinner 	 * during recovery. This is redundant as we now log every change and
3451e60896d8SDave Chinner 	 * hence this can't happen but we need to still do it to ensure
3452e60896d8SDave Chinner 	 * backwards compatibility with old kernels that predate logging all
3453e60896d8SDave Chinner 	 * inode changes.
34541da177e4SLinus Torvalds 	 */
3455e60896d8SDave Chinner 	if (ip->i_d.di_version < 3)
34561da177e4SLinus Torvalds 		ip->i_d.di_flushiter++;
34571da177e4SLinus Torvalds 
34581da177e4SLinus Torvalds 	/*
34591da177e4SLinus Torvalds 	 * Copy the dirty parts of the inode into the on-disk
34601da177e4SLinus Torvalds 	 * inode.  We always copy out the core of the inode,
34611da177e4SLinus Torvalds 	 * because if the inode is dirty at all the core must
34621da177e4SLinus Torvalds 	 * be.
34631da177e4SLinus Torvalds 	 */
346481591fe2SChristoph Hellwig 	xfs_dinode_to_disk(dip, &ip->i_d);
34651da177e4SLinus Torvalds 
34661da177e4SLinus Torvalds 	/* Wrap, we never let the log put out DI_MAX_FLUSH */
34671da177e4SLinus Torvalds 	if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
34681da177e4SLinus Torvalds 		ip->i_d.di_flushiter = 0;
34691da177e4SLinus Torvalds 
3470fd9fdba6SEric Sandeen 	xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK);
3471e4ac967bSDavid Chinner 	if (XFS_IFORK_Q(ip))
3472fd9fdba6SEric Sandeen 		xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK);
34731da177e4SLinus Torvalds 	xfs_inobp_check(mp, bp);
34741da177e4SLinus Torvalds 
34751da177e4SLinus Torvalds 	/*
3476f5d8d5c4SChristoph Hellwig 	 * We've recorded everything logged in the inode, so we'd like to clear
3477f5d8d5c4SChristoph Hellwig 	 * the ili_fields bits so we don't log and flush things unnecessarily.
3478f5d8d5c4SChristoph Hellwig 	 * However, we can't stop logging all this information until the data
3479f5d8d5c4SChristoph Hellwig 	 * we've copied into the disk buffer is written to disk.  If we did we
3480f5d8d5c4SChristoph Hellwig 	 * might overwrite the copy of the inode in the log with all the data
3481f5d8d5c4SChristoph Hellwig 	 * after re-logging only part of it, and in the face of a crash we
3482f5d8d5c4SChristoph Hellwig 	 * wouldn't have all the data we need to recover.
34831da177e4SLinus Torvalds 	 *
3484f5d8d5c4SChristoph Hellwig 	 * What we do is move the bits to the ili_last_fields field.  When
3485f5d8d5c4SChristoph Hellwig 	 * logging the inode, these bits are moved back to the ili_fields field.
3486f5d8d5c4SChristoph Hellwig 	 * In the xfs_iflush_done() routine we clear ili_last_fields, since we
3487f5d8d5c4SChristoph Hellwig 	 * know that the information those bits represent is permanently on
3488f5d8d5c4SChristoph Hellwig 	 * disk.  As long as the flush completes before the inode is logged
3489f5d8d5c4SChristoph Hellwig 	 * again, then both ili_fields and ili_last_fields will be cleared.
34901da177e4SLinus Torvalds 	 *
3491f5d8d5c4SChristoph Hellwig 	 * We can play with the ili_fields bits here, because the inode lock
3492f5d8d5c4SChristoph Hellwig 	 * must be held exclusively in order to set bits there and the flush
3493f5d8d5c4SChristoph Hellwig 	 * lock protects the ili_last_fields bits.  Set ili_logged so the flush
3494f5d8d5c4SChristoph Hellwig 	 * done routine can tell whether or not to look in the AIL.  Also, store
3495f5d8d5c4SChristoph Hellwig 	 * the current LSN of the inode so that we can tell whether the item has
3496f5d8d5c4SChristoph Hellwig 	 * moved in the AIL from xfs_iflush_done().  In order to read the lsn we
3497f5d8d5c4SChristoph Hellwig 	 * need the AIL lock, because it is a 64 bit value that cannot be read
3498f5d8d5c4SChristoph Hellwig 	 * atomically.
34991da177e4SLinus Torvalds 	 */
3500f5d8d5c4SChristoph Hellwig 	iip->ili_last_fields = iip->ili_fields;
3501f5d8d5c4SChristoph Hellwig 	iip->ili_fields = 0;
35021da177e4SLinus Torvalds 	iip->ili_logged = 1;
35031da177e4SLinus Torvalds 
35047b2e2a31SDavid Chinner 	xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
35057b2e2a31SDavid Chinner 				&iip->ili_item.li_lsn);
35061da177e4SLinus Torvalds 
35071da177e4SLinus Torvalds 	/*
35081da177e4SLinus Torvalds 	 * Attach the function xfs_iflush_done to the inode's
35091da177e4SLinus Torvalds 	 * buffer.  This will remove the inode from the AIL
35101da177e4SLinus Torvalds 	 * and unlock the inode's flush lock when the inode is
35111da177e4SLinus Torvalds 	 * completely written to disk.
35121da177e4SLinus Torvalds 	 */
3513ca30b2a7SChristoph Hellwig 	xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
35141da177e4SLinus Torvalds 
351593848a99SChristoph Hellwig 	/* update the lsn in the on disk inode if required */
351693848a99SChristoph Hellwig 	if (ip->i_d.di_version == 3)
351793848a99SChristoph Hellwig 		dip->di_lsn = cpu_to_be64(iip->ili_item.li_lsn);
351893848a99SChristoph Hellwig 
351993848a99SChristoph Hellwig 	/* generate the checksum. */
352093848a99SChristoph Hellwig 	xfs_dinode_calc_crc(mp, dip);
352193848a99SChristoph Hellwig 
3522adadbeefSChristoph Hellwig 	ASSERT(bp->b_fspriv != NULL);
3523cb669ca5SChristoph Hellwig 	ASSERT(bp->b_iodone != NULL);
35241da177e4SLinus Torvalds 	return 0;
35251da177e4SLinus Torvalds 
35261da177e4SLinus Torvalds corrupt_out:
35272451337dSDave Chinner 	return -EFSCORRUPTED;
35281da177e4SLinus Torvalds }
3529