blob: 8d204d516621282757561940f5efb62a32221369 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11004 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Jeff Laytonf0e28282017-12-11 06:35:19 -05006#include <linux/iversion.h>
Robert P. J. Day40ebd812007-11-23 16:30:51 +11007
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +11009#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +110010#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110011#include "xfs_format.h"
12#include "xfs_log_format.h"
13#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "xfs_mount.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100015#include "xfs_defer.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110016#include "xfs_inode.h"
Dave Chinnerc24b5df2013-08-12 20:49:45 +100017#include "xfs_dir2.h"
Dave Chinnerc24b5df2013-08-12 20:49:45 +100018#include "xfs_attr.h"
Dave Chinner239880e2013-10-23 10:50:10 +110019#include "xfs_trans_space.h"
20#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_buf_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_inode_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_ialloc.h"
24#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100025#include "xfs_bmap_util.h"
Darrick J. Wonge9e899a2017-10-31 12:04:49 -070026#include "xfs_errortag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_quota.h"
David Chinner2a82b8b2007-07-11 11:09:12 +100029#include "xfs_filestream.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000030#include "xfs_trace.h"
Dave Chinner33479e02012-10-08 21:56:11 +110031#include "xfs_icache.h"
Dave Chinnerc24b5df2013-08-12 20:49:45 +100032#include "xfs_symlink.h"
Dave Chinner239880e2013-10-23 10:50:10 +110033#include "xfs_trans_priv.h"
34#include "xfs_log.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110035#include "xfs_bmap_btree.h"
Darrick J. Wongaa8968f2016-10-03 09:11:38 -070036#include "xfs_reflink.h"
Dave Chinner9bbafc712021-06-02 10:48:24 +100037#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039kmem_zone_t *xfs_inode_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/*
Christoph Hellwig8f04c472011-07-08 14:34:34 +020042 * Used in xfs_itruncate_extents(). This is the maximum number of extents
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * freed from a file in a single transaction.
44 */
45#define XFS_ITRUNC_MAX_EXTENTS 2
46
Dave Chinner54d7b5c2016-02-09 16:54:58 +110047STATIC int xfs_iunlink(struct xfs_trans *, struct xfs_inode *);
48STATIC int xfs_iunlink_remove(struct xfs_trans *, struct xfs_inode *);
Zhi Yong Wuab297432013-12-18 08:22:41 +080049
Dave Chinner2a0ec1d2012-04-23 15:59:02 +100050/*
51 * helper function to extract extent size hint from inode
52 */
53xfs_extlen_t
54xfs_get_extsz_hint(
55 struct xfs_inode *ip)
56{
Christoph Hellwigbdb2ed22019-10-14 10:07:21 -070057 /*
58 * No point in aligning allocations if we need to COW to actually
59 * write to them.
60 */
61 if (xfs_is_always_cow_inode(ip))
62 return 0;
Christoph Hellwigdb073492021-03-29 11:11:44 -070063 if ((ip->i_diflags & XFS_DIFLAG_EXTSIZE) && ip->i_extsize)
Christoph Hellwig031474c2021-03-29 11:11:41 -070064 return ip->i_extsize;
Dave Chinner2a0ec1d2012-04-23 15:59:02 +100065 if (XFS_IS_REALTIME_INODE(ip))
66 return ip->i_mount->m_sb.sb_rextsize;
67 return 0;
68}
69
Dave Chinnerfa96aca2012-10-08 21:56:10 +110070/*
Darrick J. Wongf7ca3522016-10-03 09:11:43 -070071 * Helper function to extract CoW extent size hint from inode.
72 * Between the extent size hint and the CoW extent size hint, we
Darrick J. Wonge153aa72016-10-03 09:11:49 -070073 * return the greater of the two. If the value is zero (automatic),
74 * use the default size.
Darrick J. Wongf7ca3522016-10-03 09:11:43 -070075 */
76xfs_extlen_t
77xfs_get_cowextsz_hint(
78 struct xfs_inode *ip)
79{
80 xfs_extlen_t a, b;
81
82 a = 0;
Christoph Hellwig3e09ab82021-03-29 11:11:45 -070083 if (ip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE)
Christoph Hellwigb33ce572021-03-29 11:11:42 -070084 a = ip->i_cowextsize;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -070085 b = xfs_get_extsz_hint(ip);
86
Darrick J. Wonge153aa72016-10-03 09:11:49 -070087 a = max(a, b);
88 if (a == 0)
89 return XFS_DEFAULT_COWEXTSZ_HINT;
90 return a;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -070091}
92
93/*
Christoph Hellwigefa70be2013-12-18 02:14:39 -080094 * These two are wrapper routines around the xfs_ilock() routine used to
95 * centralize some grungy code. They are used in places that wish to lock the
96 * inode solely for reading the extents. The reason these places can't just
97 * call xfs_ilock(ip, XFS_ILOCK_SHARED) is that the inode lock also guards to
98 * bringing in of the extents from disk for a file in b-tree format. If the
99 * inode is in b-tree format, then we need to lock the inode exclusively until
100 * the extents are read in. Locking it exclusively all the time would limit
101 * our parallelism unnecessarily, though. What we do instead is check to see
102 * if the extents have been read in yet, and only lock the inode exclusively
103 * if they have not.
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100104 *
Christoph Hellwigefa70be2013-12-18 02:14:39 -0800105 * The functions return a value which should be given to the corresponding
Christoph Hellwig01f4f322013-12-06 12:30:08 -0800106 * xfs_iunlock() call.
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100107 */
108uint
Christoph Hellwig309ecac82013-12-06 12:30:09 -0800109xfs_ilock_data_map_shared(
110 struct xfs_inode *ip)
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100111{
Christoph Hellwig309ecac82013-12-06 12:30:09 -0800112 uint lock_mode = XFS_ILOCK_SHARED;
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100113
Christoph Hellwigb2197a32021-04-13 11:15:12 -0700114 if (xfs_need_iread_extents(&ip->i_df))
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100115 lock_mode = XFS_ILOCK_EXCL;
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100116 xfs_ilock(ip, lock_mode);
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100117 return lock_mode;
118}
119
Christoph Hellwigefa70be2013-12-18 02:14:39 -0800120uint
121xfs_ilock_attr_map_shared(
122 struct xfs_inode *ip)
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100123{
Christoph Hellwigefa70be2013-12-18 02:14:39 -0800124 uint lock_mode = XFS_ILOCK_SHARED;
125
Christoph Hellwigb2197a32021-04-13 11:15:12 -0700126 if (ip->i_afp && xfs_need_iread_extents(ip->i_afp))
Christoph Hellwigefa70be2013-12-18 02:14:39 -0800127 lock_mode = XFS_ILOCK_EXCL;
128 xfs_ilock(ip, lock_mode);
129 return lock_mode;
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100130}
131
132/*
Christoph Hellwig65523212016-11-30 14:33:25 +1100133 * In addition to i_rwsem in the VFS inode, the xfs inode contains 2
134 * multi-reader locks: i_mmap_lock and the i_lock. This routine allows
135 * various combinations of the locks to be obtained.
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100136 *
Dave Chinner653c60b2015-02-23 21:43:37 +1100137 * The 3 locks should always be ordered so that the IO lock is obtained first,
138 * the mmap lock second and the ilock last in order to prevent deadlock.
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100139 *
Dave Chinner653c60b2015-02-23 21:43:37 +1100140 * Basic locking order:
141 *
Christoph Hellwig65523212016-11-30 14:33:25 +1100142 * i_rwsem -> i_mmap_lock -> page_lock -> i_ilock
Dave Chinner653c60b2015-02-23 21:43:37 +1100143 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700144 * mmap_lock locking order:
Dave Chinner653c60b2015-02-23 21:43:37 +1100145 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700146 * i_rwsem -> page lock -> mmap_lock
147 * mmap_lock -> i_mmap_lock -> page_lock
Dave Chinner653c60b2015-02-23 21:43:37 +1100148 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700149 * The difference in mmap_lock locking order mean that we cannot hold the
Dave Chinner653c60b2015-02-23 21:43:37 +1100150 * i_mmap_lock over syscall based read(2)/write(2) based IO. These IO paths can
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700151 * fault in pages during copy in/out (for buffered IO) or require the mmap_lock
Dave Chinner653c60b2015-02-23 21:43:37 +1100152 * in get_user_pages() to map the user pages into the kernel address space for
Christoph Hellwig65523212016-11-30 14:33:25 +1100153 * direct IO. Similarly the i_rwsem cannot be taken inside a page fault because
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700154 * page faults already hold the mmap_lock.
Dave Chinner653c60b2015-02-23 21:43:37 +1100155 *
156 * Hence to serialise fully against both syscall and mmap based IO, we need to
Christoph Hellwig65523212016-11-30 14:33:25 +1100157 * take both the i_rwsem and the i_mmap_lock. These locks should *only* be both
Dave Chinner653c60b2015-02-23 21:43:37 +1100158 * taken in places where we need to invalidate the page cache in a race
159 * free manner (e.g. truncate, hole punch and other extent manipulation
160 * functions).
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100161 */
162void
163xfs_ilock(
164 xfs_inode_t *ip,
165 uint lock_flags)
166{
167 trace_xfs_ilock(ip, lock_flags, _RET_IP_);
168
169 /*
170 * You can't set both SHARED and EXCL for the same lock,
171 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
172 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
173 */
174 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
175 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
Dave Chinner653c60b2015-02-23 21:43:37 +1100176 ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
177 (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100178 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
179 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Dave Chinner0952c812015-08-19 10:32:49 +1000180 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100181
Christoph Hellwig65523212016-11-30 14:33:25 +1100182 if (lock_flags & XFS_IOLOCK_EXCL) {
183 down_write_nested(&VFS_I(ip)->i_rwsem,
184 XFS_IOLOCK_DEP(lock_flags));
185 } else if (lock_flags & XFS_IOLOCK_SHARED) {
186 down_read_nested(&VFS_I(ip)->i_rwsem,
187 XFS_IOLOCK_DEP(lock_flags));
188 }
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100189
Dave Chinner653c60b2015-02-23 21:43:37 +1100190 if (lock_flags & XFS_MMAPLOCK_EXCL)
191 mrupdate_nested(&ip->i_mmaplock, XFS_MMAPLOCK_DEP(lock_flags));
192 else if (lock_flags & XFS_MMAPLOCK_SHARED)
193 mraccess_nested(&ip->i_mmaplock, XFS_MMAPLOCK_DEP(lock_flags));
194
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100195 if (lock_flags & XFS_ILOCK_EXCL)
196 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
197 else if (lock_flags & XFS_ILOCK_SHARED)
198 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
199}
200
201/*
202 * This is just like xfs_ilock(), except that the caller
203 * is guaranteed not to sleep. It returns 1 if it gets
204 * the requested locks and 0 otherwise. If the IO lock is
205 * obtained but the inode lock cannot be, then the IO lock
206 * is dropped before returning.
207 *
208 * ip -- the inode being locked
209 * lock_flags -- this parameter indicates the inode's locks to be
210 * to be locked. See the comment for xfs_ilock() for a list
211 * of valid values.
212 */
213int
214xfs_ilock_nowait(
215 xfs_inode_t *ip,
216 uint lock_flags)
217{
218 trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_);
219
220 /*
221 * You can't set both SHARED and EXCL for the same lock,
222 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
223 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
224 */
225 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
226 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
Dave Chinner653c60b2015-02-23 21:43:37 +1100227 ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
228 (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100229 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
230 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Dave Chinner0952c812015-08-19 10:32:49 +1000231 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100232
233 if (lock_flags & XFS_IOLOCK_EXCL) {
Christoph Hellwig65523212016-11-30 14:33:25 +1100234 if (!down_write_trylock(&VFS_I(ip)->i_rwsem))
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100235 goto out;
236 } else if (lock_flags & XFS_IOLOCK_SHARED) {
Christoph Hellwig65523212016-11-30 14:33:25 +1100237 if (!down_read_trylock(&VFS_I(ip)->i_rwsem))
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100238 goto out;
239 }
Dave Chinner653c60b2015-02-23 21:43:37 +1100240
241 if (lock_flags & XFS_MMAPLOCK_EXCL) {
242 if (!mrtryupdate(&ip->i_mmaplock))
243 goto out_undo_iolock;
244 } else if (lock_flags & XFS_MMAPLOCK_SHARED) {
245 if (!mrtryaccess(&ip->i_mmaplock))
246 goto out_undo_iolock;
247 }
248
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100249 if (lock_flags & XFS_ILOCK_EXCL) {
250 if (!mrtryupdate(&ip->i_lock))
Dave Chinner653c60b2015-02-23 21:43:37 +1100251 goto out_undo_mmaplock;
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100252 } else if (lock_flags & XFS_ILOCK_SHARED) {
253 if (!mrtryaccess(&ip->i_lock))
Dave Chinner653c60b2015-02-23 21:43:37 +1100254 goto out_undo_mmaplock;
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100255 }
256 return 1;
257
Dave Chinner653c60b2015-02-23 21:43:37 +1100258out_undo_mmaplock:
259 if (lock_flags & XFS_MMAPLOCK_EXCL)
260 mrunlock_excl(&ip->i_mmaplock);
261 else if (lock_flags & XFS_MMAPLOCK_SHARED)
262 mrunlock_shared(&ip->i_mmaplock);
263out_undo_iolock:
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100264 if (lock_flags & XFS_IOLOCK_EXCL)
Christoph Hellwig65523212016-11-30 14:33:25 +1100265 up_write(&VFS_I(ip)->i_rwsem);
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100266 else if (lock_flags & XFS_IOLOCK_SHARED)
Christoph Hellwig65523212016-11-30 14:33:25 +1100267 up_read(&VFS_I(ip)->i_rwsem);
Dave Chinner653c60b2015-02-23 21:43:37 +1100268out:
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100269 return 0;
270}
271
272/*
273 * xfs_iunlock() is used to drop the inode locks acquired with
274 * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
275 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
276 * that we know which locks to drop.
277 *
278 * ip -- the inode being unlocked
279 * lock_flags -- this parameter indicates the inode's locks to be
280 * to be unlocked. See the comment for xfs_ilock() for a list
281 * of valid values for this parameter.
282 *
283 */
284void
285xfs_iunlock(
286 xfs_inode_t *ip,
287 uint lock_flags)
288{
289 /*
290 * You can't set both SHARED and EXCL for the same lock,
291 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
292 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
293 */
294 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
295 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
Dave Chinner653c60b2015-02-23 21:43:37 +1100296 ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
297 (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100298 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
299 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Dave Chinner0952c812015-08-19 10:32:49 +1000300 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100301 ASSERT(lock_flags != 0);
302
303 if (lock_flags & XFS_IOLOCK_EXCL)
Christoph Hellwig65523212016-11-30 14:33:25 +1100304 up_write(&VFS_I(ip)->i_rwsem);
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100305 else if (lock_flags & XFS_IOLOCK_SHARED)
Christoph Hellwig65523212016-11-30 14:33:25 +1100306 up_read(&VFS_I(ip)->i_rwsem);
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100307
Dave Chinner653c60b2015-02-23 21:43:37 +1100308 if (lock_flags & XFS_MMAPLOCK_EXCL)
309 mrunlock_excl(&ip->i_mmaplock);
310 else if (lock_flags & XFS_MMAPLOCK_SHARED)
311 mrunlock_shared(&ip->i_mmaplock);
312
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100313 if (lock_flags & XFS_ILOCK_EXCL)
314 mrunlock_excl(&ip->i_lock);
315 else if (lock_flags & XFS_ILOCK_SHARED)
316 mrunlock_shared(&ip->i_lock);
317
318 trace_xfs_iunlock(ip, lock_flags, _RET_IP_);
319}
320
321/*
322 * give up write locks. the i/o lock cannot be held nested
323 * if it is being demoted.
324 */
325void
326xfs_ilock_demote(
327 xfs_inode_t *ip,
328 uint lock_flags)
329{
Dave Chinner653c60b2015-02-23 21:43:37 +1100330 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL));
331 ASSERT((lock_flags &
332 ~(XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100333
334 if (lock_flags & XFS_ILOCK_EXCL)
335 mrdemote(&ip->i_lock);
Dave Chinner653c60b2015-02-23 21:43:37 +1100336 if (lock_flags & XFS_MMAPLOCK_EXCL)
337 mrdemote(&ip->i_mmaplock);
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100338 if (lock_flags & XFS_IOLOCK_EXCL)
Christoph Hellwig65523212016-11-30 14:33:25 +1100339 downgrade_write(&VFS_I(ip)->i_rwsem);
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100340
341 trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_);
342}
343
Dave Chinner742ae1e2013-04-30 21:39:34 +1000344#if defined(DEBUG) || defined(XFS_WARN)
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100345int
346xfs_isilocked(
347 xfs_inode_t *ip,
348 uint lock_flags)
349{
350 if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) {
351 if (!(lock_flags & XFS_ILOCK_SHARED))
352 return !!ip->i_lock.mr_writer;
353 return rwsem_is_locked(&ip->i_lock.mr_lock);
354 }
355
Dave Chinner653c60b2015-02-23 21:43:37 +1100356 if (lock_flags & (XFS_MMAPLOCK_EXCL|XFS_MMAPLOCK_SHARED)) {
357 if (!(lock_flags & XFS_MMAPLOCK_SHARED))
358 return !!ip->i_mmaplock.mr_writer;
359 return rwsem_is_locked(&ip->i_mmaplock.mr_lock);
360 }
361
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100362 if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) {
363 if (!(lock_flags & XFS_IOLOCK_SHARED))
Christoph Hellwig65523212016-11-30 14:33:25 +1100364 return !debug_locks ||
365 lockdep_is_held_type(&VFS_I(ip)->i_rwsem, 0);
366 return rwsem_is_locked(&VFS_I(ip)->i_rwsem);
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100367 }
368
369 ASSERT(0);
370 return 0;
371}
372#endif
373
Dave Chinnerb6a99472015-08-25 10:05:13 +1000374/*
375 * xfs_lockdep_subclass_ok() is only used in an ASSERT, so is only called when
376 * DEBUG or XFS_WARN is set. And MAX_LOCKDEP_SUBCLASSES is then only defined
377 * when CONFIG_LOCKDEP is set. Hence the complex define below to avoid build
378 * errors and warnings.
379 */
380#if (defined(DEBUG) || defined(XFS_WARN)) && defined(CONFIG_LOCKDEP)
Dave Chinner3403ccc2015-08-20 09:27:49 +1000381static bool
382xfs_lockdep_subclass_ok(
383 int subclass)
384{
385 return subclass < MAX_LOCKDEP_SUBCLASSES;
386}
387#else
388#define xfs_lockdep_subclass_ok(subclass) (true)
389#endif
390
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000391/*
Dave Chinner653c60b2015-02-23 21:43:37 +1100392 * Bump the subclass so xfs_lock_inodes() acquires each lock with a different
Dave Chinner0952c812015-08-19 10:32:49 +1000393 * value. This can be called for any type of inode lock combination, including
394 * parent locking. Care must be taken to ensure we don't overrun the subclass
395 * storage fields in the class mask we build.
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000396 */
397static inline int
398xfs_lock_inumorder(int lock_mode, int subclass)
399{
Dave Chinner0952c812015-08-19 10:32:49 +1000400 int class = 0;
401
402 ASSERT(!(lock_mode & (XFS_ILOCK_PARENT | XFS_ILOCK_RTBITMAP |
403 XFS_ILOCK_RTSUM)));
Dave Chinner3403ccc2015-08-20 09:27:49 +1000404 ASSERT(xfs_lockdep_subclass_ok(subclass));
Dave Chinner0952c812015-08-19 10:32:49 +1000405
Dave Chinner653c60b2015-02-23 21:43:37 +1100406 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) {
Dave Chinner0952c812015-08-19 10:32:49 +1000407 ASSERT(subclass <= XFS_IOLOCK_MAX_SUBCLASS);
Dave Chinner0952c812015-08-19 10:32:49 +1000408 class += subclass << XFS_IOLOCK_SHIFT;
Dave Chinner653c60b2015-02-23 21:43:37 +1100409 }
410
411 if (lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) {
Dave Chinner0952c812015-08-19 10:32:49 +1000412 ASSERT(subclass <= XFS_MMAPLOCK_MAX_SUBCLASS);
413 class += subclass << XFS_MMAPLOCK_SHIFT;
Dave Chinner653c60b2015-02-23 21:43:37 +1100414 }
415
Dave Chinner0952c812015-08-19 10:32:49 +1000416 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) {
417 ASSERT(subclass <= XFS_ILOCK_MAX_SUBCLASS);
418 class += subclass << XFS_ILOCK_SHIFT;
419 }
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000420
Dave Chinner0952c812015-08-19 10:32:49 +1000421 return (lock_mode & ~XFS_LOCK_SUBCLASS_MASK) | class;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000422}
423
424/*
Dave Chinner95afcf52015-03-25 14:03:32 +1100425 * The following routine will lock n inodes in exclusive mode. We assume the
426 * caller calls us with the inodes in i_ino order.
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000427 *
Dave Chinner95afcf52015-03-25 14:03:32 +1100428 * We need to detect deadlock where an inode that we lock is in the AIL and we
429 * start waiting for another inode that is locked by a thread in a long running
430 * transaction (such as truncate). This can result in deadlock since the long
431 * running trans might need to wait for the inode we just locked in order to
432 * push the tail and free space in the log.
Dave Chinner0952c812015-08-19 10:32:49 +1000433 *
434 * xfs_lock_inodes() can only be used to lock one type of lock at a time -
435 * the iolock, the mmaplock or the ilock, but not more than one at a time. If we
436 * lock more than one at a time, lockdep will report false positives saying we
437 * have violated locking orders.
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000438 */
Eric Sandeen0d5a75e2016-06-01 17:38:15 +1000439static void
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000440xfs_lock_inodes(
Christoph Hellwigefe23302019-06-28 19:27:33 -0700441 struct xfs_inode **ips,
442 int inodes,
443 uint lock_mode)
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000444{
Christoph Hellwigefe23302019-06-28 19:27:33 -0700445 int attempts = 0, i, j, try_lock;
446 struct xfs_log_item *lp;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000447
Dave Chinner0952c812015-08-19 10:32:49 +1000448 /*
449 * Currently supports between 2 and 5 inodes with exclusive locking. We
450 * support an arbitrary depth of locking here, but absolute limits on
Randy Dunlapb63da6c2020-08-05 08:49:58 -0700451 * inodes depend on the type of locking and the limits placed by
Dave Chinner0952c812015-08-19 10:32:49 +1000452 * lockdep annotations in xfs_lock_inumorder. These are all checked by
453 * the asserts.
454 */
Dave Chinner95afcf52015-03-25 14:03:32 +1100455 ASSERT(ips && inodes >= 2 && inodes <= 5);
Dave Chinner0952c812015-08-19 10:32:49 +1000456 ASSERT(lock_mode & (XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL |
457 XFS_ILOCK_EXCL));
458 ASSERT(!(lock_mode & (XFS_IOLOCK_SHARED | XFS_MMAPLOCK_SHARED |
459 XFS_ILOCK_SHARED)));
Dave Chinner0952c812015-08-19 10:32:49 +1000460 ASSERT(!(lock_mode & XFS_MMAPLOCK_EXCL) ||
461 inodes <= XFS_MMAPLOCK_MAX_SUBCLASS + 1);
462 ASSERT(!(lock_mode & XFS_ILOCK_EXCL) ||
463 inodes <= XFS_ILOCK_MAX_SUBCLASS + 1);
464
465 if (lock_mode & XFS_IOLOCK_EXCL) {
466 ASSERT(!(lock_mode & (XFS_MMAPLOCK_EXCL | XFS_ILOCK_EXCL)));
467 } else if (lock_mode & XFS_MMAPLOCK_EXCL)
468 ASSERT(!(lock_mode & XFS_ILOCK_EXCL));
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000469
470 try_lock = 0;
471 i = 0;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000472again:
473 for (; i < inodes; i++) {
474 ASSERT(ips[i]);
475
Dave Chinner95afcf52015-03-25 14:03:32 +1100476 if (i && (ips[i] == ips[i - 1])) /* Already locked */
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000477 continue;
478
479 /*
Dave Chinner95afcf52015-03-25 14:03:32 +1100480 * If try_lock is not set yet, make sure all locked inodes are
481 * not in the AIL. If any are, set try_lock to be used later.
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000482 */
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000483 if (!try_lock) {
484 for (j = (i - 1); j >= 0 && !try_lock; j--) {
Christoph Hellwigb3b14aa2019-06-28 19:27:33 -0700485 lp = &ips[j]->i_itemp->ili_item;
Dave Chinner22525c12018-05-09 07:47:34 -0700486 if (lp && test_bit(XFS_LI_IN_AIL, &lp->li_flags))
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000487 try_lock++;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000488 }
489 }
490
491 /*
492 * If any of the previous locks we have locked is in the AIL,
493 * we must TRY to get the second and subsequent locks. If
494 * we can't get any, we must release all we have
495 * and try again.
496 */
Dave Chinner95afcf52015-03-25 14:03:32 +1100497 if (!try_lock) {
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000498 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
Dave Chinner95afcf52015-03-25 14:03:32 +1100499 continue;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000500 }
Dave Chinner95afcf52015-03-25 14:03:32 +1100501
502 /* try_lock means we have an inode locked that is in the AIL. */
503 ASSERT(i != 0);
504 if (xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i)))
505 continue;
506
507 /*
508 * Unlock all previous guys and try again. xfs_iunlock will try
509 * to push the tail if the inode is in the AIL.
510 */
511 attempts++;
512 for (j = i - 1; j >= 0; j--) {
513 /*
514 * Check to see if we've already unlocked this one. Not
515 * the first one going back, and the inode ptr is the
516 * same.
517 */
518 if (j != (i - 1) && ips[j] == ips[j + 1])
519 continue;
520
521 xfs_iunlock(ips[j], lock_mode);
522 }
523
524 if ((attempts % 5) == 0) {
525 delay(1); /* Don't just spin the CPU */
Dave Chinner95afcf52015-03-25 14:03:32 +1100526 }
527 i = 0;
528 try_lock = 0;
529 goto again;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000530 }
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000531}
532
533/*
Dave Chinner653c60b2015-02-23 21:43:37 +1100534 * xfs_lock_two_inodes() can only be used to lock one type of lock at a time -
Darrick J. Wong7c2d2382018-01-26 15:27:33 -0800535 * the mmaplock or the ilock, but not more than one type at a time. If we lock
536 * more than one at a time, lockdep will report false positives saying we have
537 * violated locking orders. The iolock must be double-locked separately since
538 * we use i_rwsem for that. We now support taking one lock EXCL and the other
539 * SHARED.
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000540 */
541void
542xfs_lock_two_inodes(
Darrick J. Wong7c2d2382018-01-26 15:27:33 -0800543 struct xfs_inode *ip0,
544 uint ip0_mode,
545 struct xfs_inode *ip1,
546 uint ip1_mode)
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000547{
Darrick J. Wong7c2d2382018-01-26 15:27:33 -0800548 struct xfs_inode *temp;
549 uint mode_temp;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000550 int attempts = 0;
Christoph Hellwigefe23302019-06-28 19:27:33 -0700551 struct xfs_log_item *lp;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000552
Darrick J. Wong7c2d2382018-01-26 15:27:33 -0800553 ASSERT(hweight32(ip0_mode) == 1);
554 ASSERT(hweight32(ip1_mode) == 1);
555 ASSERT(!(ip0_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)));
556 ASSERT(!(ip1_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)));
557 ASSERT(!(ip0_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) ||
558 !(ip0_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
559 ASSERT(!(ip1_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) ||
560 !(ip1_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
561 ASSERT(!(ip1_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) ||
562 !(ip0_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
563 ASSERT(!(ip0_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) ||
564 !(ip1_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
Dave Chinner653c60b2015-02-23 21:43:37 +1100565
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000566 ASSERT(ip0->i_ino != ip1->i_ino);
567
568 if (ip0->i_ino > ip1->i_ino) {
569 temp = ip0;
570 ip0 = ip1;
571 ip1 = temp;
Darrick J. Wong7c2d2382018-01-26 15:27:33 -0800572 mode_temp = ip0_mode;
573 ip0_mode = ip1_mode;
574 ip1_mode = mode_temp;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000575 }
576
577 again:
Darrick J. Wong7c2d2382018-01-26 15:27:33 -0800578 xfs_ilock(ip0, xfs_lock_inumorder(ip0_mode, 0));
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000579
580 /*
581 * If the first lock we have locked is in the AIL, we must TRY to get
582 * the second lock. If we can't get it, we must release the first one
583 * and try again.
584 */
Christoph Hellwigb3b14aa2019-06-28 19:27:33 -0700585 lp = &ip0->i_itemp->ili_item;
Dave Chinner22525c12018-05-09 07:47:34 -0700586 if (lp && test_bit(XFS_LI_IN_AIL, &lp->li_flags)) {
Darrick J. Wong7c2d2382018-01-26 15:27:33 -0800587 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(ip1_mode, 1))) {
588 xfs_iunlock(ip0, ip0_mode);
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000589 if ((++attempts % 5) == 0)
590 delay(1); /* Don't just spin the CPU */
591 goto again;
592 }
593 } else {
Darrick J. Wong7c2d2382018-01-26 15:27:33 -0800594 xfs_ilock(ip1, xfs_lock_inumorder(ip1_mode, 1));
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000595 }
596}
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598uint
599xfs_ip2xflags(
Dave Chinner58f88ca2016-01-04 16:44:15 +1100600 struct xfs_inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Christoph Hellwig44225012021-03-29 11:11:46 -0700602 uint flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Christoph Hellwig44225012021-03-29 11:11:46 -0700604 if (ip->i_diflags & XFS_DIFLAG_ANY) {
605 if (ip->i_diflags & XFS_DIFLAG_REALTIME)
606 flags |= FS_XFLAG_REALTIME;
607 if (ip->i_diflags & XFS_DIFLAG_PREALLOC)
608 flags |= FS_XFLAG_PREALLOC;
609 if (ip->i_diflags & XFS_DIFLAG_IMMUTABLE)
610 flags |= FS_XFLAG_IMMUTABLE;
611 if (ip->i_diflags & XFS_DIFLAG_APPEND)
612 flags |= FS_XFLAG_APPEND;
613 if (ip->i_diflags & XFS_DIFLAG_SYNC)
614 flags |= FS_XFLAG_SYNC;
615 if (ip->i_diflags & XFS_DIFLAG_NOATIME)
616 flags |= FS_XFLAG_NOATIME;
617 if (ip->i_diflags & XFS_DIFLAG_NODUMP)
618 flags |= FS_XFLAG_NODUMP;
619 if (ip->i_diflags & XFS_DIFLAG_RTINHERIT)
620 flags |= FS_XFLAG_RTINHERIT;
621 if (ip->i_diflags & XFS_DIFLAG_PROJINHERIT)
622 flags |= FS_XFLAG_PROJINHERIT;
623 if (ip->i_diflags & XFS_DIFLAG_NOSYMLINKS)
624 flags |= FS_XFLAG_NOSYMLINKS;
625 if (ip->i_diflags & XFS_DIFLAG_EXTSIZE)
626 flags |= FS_XFLAG_EXTSIZE;
627 if (ip->i_diflags & XFS_DIFLAG_EXTSZINHERIT)
628 flags |= FS_XFLAG_EXTSZINHERIT;
629 if (ip->i_diflags & XFS_DIFLAG_NODEFRAG)
630 flags |= FS_XFLAG_NODEFRAG;
631 if (ip->i_diflags & XFS_DIFLAG_FILESTREAM)
632 flags |= FS_XFLAG_FILESTREAM;
633 }
634
635 if (ip->i_diflags2 & XFS_DIFLAG2_ANY) {
636 if (ip->i_diflags2 & XFS_DIFLAG2_DAX)
637 flags |= FS_XFLAG_DAX;
638 if (ip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE)
639 flags |= FS_XFLAG_COWEXTSIZE;
640 }
641
642 if (XFS_IFORK_Q(ip))
643 flags |= FS_XFLAG_HASATTR;
644 return flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647/*
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000648 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
649 * is allowed, otherwise it has to be an exact match. If a CI match is found,
650 * ci_name->name will point to a the actual name (caller must free) or
651 * will be set to NULL if an exact match is found.
652 */
653int
654xfs_lookup(
655 xfs_inode_t *dp,
656 struct xfs_name *name,
657 xfs_inode_t **ipp,
658 struct xfs_name *ci_name)
659{
660 xfs_ino_t inum;
661 int error;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000662
663 trace_xfs_lookup(dp, name);
664
665 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000666 return -EIO;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000667
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000668 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000669 if (error)
Dave Chinnerdbad7c92015-08-19 10:33:00 +1000670 goto out_unlock;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000671
672 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
673 if (error)
674 goto out_free_name;
675
676 return 0;
677
678out_free_name:
679 if (ci_name)
680 kmem_free(ci_name->name);
Dave Chinnerdbad7c92015-08-19 10:33:00 +1000681out_unlock:
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000682 *ipp = NULL;
683 return error;
684}
685
Darrick J. Wong8a569d72020-09-13 10:16:40 -0700686/* Propagate di_flags from a parent inode to a child inode. */
687static void
688xfs_inode_inherit_flags(
689 struct xfs_inode *ip,
690 const struct xfs_inode *pip)
691{
692 unsigned int di_flags = 0;
693 umode_t mode = VFS_I(ip)->i_mode;
694
695 if (S_ISDIR(mode)) {
Christoph Hellwigdb073492021-03-29 11:11:44 -0700696 if (pip->i_diflags & XFS_DIFLAG_RTINHERIT)
Darrick J. Wong8a569d72020-09-13 10:16:40 -0700697 di_flags |= XFS_DIFLAG_RTINHERIT;
Christoph Hellwigdb073492021-03-29 11:11:44 -0700698 if (pip->i_diflags & XFS_DIFLAG_EXTSZINHERIT) {
Darrick J. Wong8a569d72020-09-13 10:16:40 -0700699 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
Christoph Hellwig031474c2021-03-29 11:11:41 -0700700 ip->i_extsize = pip->i_extsize;
Darrick J. Wong8a569d72020-09-13 10:16:40 -0700701 }
Christoph Hellwigdb073492021-03-29 11:11:44 -0700702 if (pip->i_diflags & XFS_DIFLAG_PROJINHERIT)
Darrick J. Wong8a569d72020-09-13 10:16:40 -0700703 di_flags |= XFS_DIFLAG_PROJINHERIT;
704 } else if (S_ISREG(mode)) {
Christoph Hellwigdb073492021-03-29 11:11:44 -0700705 if ((pip->i_diflags & XFS_DIFLAG_RTINHERIT) &&
Darrick J. Wongd4f2c142020-09-13 10:16:41 -0700706 xfs_sb_version_hasrealtime(&ip->i_mount->m_sb))
Darrick J. Wong8a569d72020-09-13 10:16:40 -0700707 di_flags |= XFS_DIFLAG_REALTIME;
Christoph Hellwigdb073492021-03-29 11:11:44 -0700708 if (pip->i_diflags & XFS_DIFLAG_EXTSZINHERIT) {
Darrick J. Wong8a569d72020-09-13 10:16:40 -0700709 di_flags |= XFS_DIFLAG_EXTSIZE;
Christoph Hellwig031474c2021-03-29 11:11:41 -0700710 ip->i_extsize = pip->i_extsize;
Darrick J. Wong8a569d72020-09-13 10:16:40 -0700711 }
712 }
Christoph Hellwigdb073492021-03-29 11:11:44 -0700713 if ((pip->i_diflags & XFS_DIFLAG_NOATIME) &&
Darrick J. Wong8a569d72020-09-13 10:16:40 -0700714 xfs_inherit_noatime)
715 di_flags |= XFS_DIFLAG_NOATIME;
Christoph Hellwigdb073492021-03-29 11:11:44 -0700716 if ((pip->i_diflags & XFS_DIFLAG_NODUMP) &&
Darrick J. Wong8a569d72020-09-13 10:16:40 -0700717 xfs_inherit_nodump)
718 di_flags |= XFS_DIFLAG_NODUMP;
Christoph Hellwigdb073492021-03-29 11:11:44 -0700719 if ((pip->i_diflags & XFS_DIFLAG_SYNC) &&
Darrick J. Wong8a569d72020-09-13 10:16:40 -0700720 xfs_inherit_sync)
721 di_flags |= XFS_DIFLAG_SYNC;
Christoph Hellwigdb073492021-03-29 11:11:44 -0700722 if ((pip->i_diflags & XFS_DIFLAG_NOSYMLINKS) &&
Darrick J. Wong8a569d72020-09-13 10:16:40 -0700723 xfs_inherit_nosymlinks)
724 di_flags |= XFS_DIFLAG_NOSYMLINKS;
Christoph Hellwigdb073492021-03-29 11:11:44 -0700725 if ((pip->i_diflags & XFS_DIFLAG_NODEFRAG) &&
Darrick J. Wong8a569d72020-09-13 10:16:40 -0700726 xfs_inherit_nodefrag)
727 di_flags |= XFS_DIFLAG_NODEFRAG;
Christoph Hellwigdb073492021-03-29 11:11:44 -0700728 if (pip->i_diflags & XFS_DIFLAG_FILESTREAM)
Darrick J. Wong8a569d72020-09-13 10:16:40 -0700729 di_flags |= XFS_DIFLAG_FILESTREAM;
730
Christoph Hellwigdb073492021-03-29 11:11:44 -0700731 ip->i_diflags |= di_flags;
Darrick J. Wong8a569d72020-09-13 10:16:40 -0700732}
733
734/* Propagate di_flags2 from a parent inode to a child inode. */
735static void
736xfs_inode_inherit_flags2(
737 struct xfs_inode *ip,
738 const struct xfs_inode *pip)
739{
Christoph Hellwig3e09ab82021-03-29 11:11:45 -0700740 if (pip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE) {
741 ip->i_diflags2 |= XFS_DIFLAG2_COWEXTSIZE;
Christoph Hellwigb33ce572021-03-29 11:11:42 -0700742 ip->i_cowextsize = pip->i_cowextsize;
Darrick J. Wong8a569d72020-09-13 10:16:40 -0700743 }
Christoph Hellwig3e09ab82021-03-29 11:11:45 -0700744 if (pip->i_diflags2 & XFS_DIFLAG2_DAX)
745 ip->i_diflags2 |= XFS_DIFLAG2_DAX;
Darrick J. Wong8a569d72020-09-13 10:16:40 -0700746}
747
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000748/*
Dave Chinner1abcf262020-12-09 10:05:15 -0800749 * Initialise a newly allocated inode and return the in-core inode to the
750 * caller locked exclusively.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 */
Dave Chinnerb652afd2021-06-02 10:48:24 +1000752int
Dave Chinner1abcf262020-12-09 10:05:15 -0800753xfs_init_new_inode(
Christoph Hellwigf736d932021-01-21 14:19:58 +0100754 struct user_namespace *mnt_userns,
Dave Chinner1abcf262020-12-09 10:05:15 -0800755 struct xfs_trans *tp,
756 struct xfs_inode *pip,
757 xfs_ino_t ino,
758 umode_t mode,
759 xfs_nlink_t nlink,
760 dev_t rdev,
761 prid_t prid,
Dave Chinnere6a688c2021-03-22 09:52:03 -0700762 bool init_xattrs,
Dave Chinner1abcf262020-12-09 10:05:15 -0800763 struct xfs_inode **ipp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Christoph Hellwig01ea1732021-01-22 16:48:18 -0800765 struct inode *dir = pip ? VFS_I(pip) : NULL;
Dave Chinner1abcf262020-12-09 10:05:15 -0800766 struct xfs_mount *mp = tp->t_mountp;
767 struct xfs_inode *ip;
768 unsigned int flags;
769 int error;
770 struct timespec64 tv;
771 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 /*
Dave Chinner8b269842018-04-17 17:17:35 -0700774 * Protect against obviously corrupt allocation btree records. Later
775 * xfs_iget checks will catch re-allocation of other active in-memory
776 * and on-disk inodes. If we don't catch reallocating the parent inode
777 * here we will deadlock in xfs_iget() so we have to do these checks
778 * first.
779 */
780 if ((pip && ino == pip->i_ino) || !xfs_verify_dir_ino(mp, ino)) {
781 xfs_alert(mp, "Allocated a known in-use inode 0x%llx!", ino);
782 return -EFSCORRUPTED;
783 }
784
785 /*
Dave Chinner1abcf262020-12-09 10:05:15 -0800786 * Get the in-core inode with the lock held exclusively to prevent
787 * others from looking at until we're done.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 */
Dave Chinner1abcf262020-12-09 10:05:15 -0800789 error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE, XFS_ILOCK_EXCL, &ip);
David Chinnerbf904242008-10-30 17:36:14 +1100790 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 return error;
Dave Chinner1abcf262020-12-09 10:05:15 -0800792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 ASSERT(ip != NULL);
Dave Chinner39878482016-02-09 16:54:58 +1100794 inode = VFS_I(ip);
Dave Chinner54d7b5c2016-02-09 16:54:58 +1100795 set_nlink(inode, nlink);
Christoph Hellwig66f36462017-10-19 11:07:09 -0700796 inode->i_rdev = rdev;
Christoph Hellwigceaf6032021-03-29 11:11:39 -0700797 ip->i_projid = prid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Christoph Hellwig01ea1732021-01-22 16:48:18 -0800799 if (dir && !(dir->i_mode & S_ISGID) &&
800 (mp->m_flags & XFS_MOUNT_GRPID)) {
Christian Braunerdb998552021-03-20 13:26:24 +0100801 inode_fsuid_set(inode, mnt_userns);
Christoph Hellwig01ea1732021-01-22 16:48:18 -0800802 inode->i_gid = dir->i_gid;
803 inode->i_mode = mode;
Christoph Hellwig3d8f2822020-02-21 08:31:26 -0800804 } else {
Linus Torvalds7d6beb72021-02-23 13:39:45 -0800805 inode_init_owner(mnt_userns, inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
807
808 /*
809 * If the group ID of the new file does not match the effective group
810 * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
811 * (and only if the irix_sgid_inherit compatibility variable is set).
812 */
Christoph Hellwig54295152020-02-21 08:31:27 -0800813 if (irix_sgid_inherit &&
Christoph Hellwigf736d932021-01-21 14:19:58 +0100814 (inode->i_mode & S_ISGID) &&
815 !in_group_p(i_gid_into_mnt(mnt_userns, inode)))
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100816 inode->i_mode &= ~S_ISGID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Christoph Hellwig13d2c102021-03-29 11:11:40 -0700818 ip->i_disk_size = 0;
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700819 ip->i_df.if_nextents = 0;
Christoph Hellwig6e73a542021-03-29 11:11:40 -0700820 ASSERT(ip->i_nblocks == 0);
Christoph Hellwigdff35fd2008-08-13 16:44:15 +1000821
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700822 tv = current_time(inode);
Dave Chinner39878482016-02-09 16:54:58 +1100823 inode->i_mtime = tv;
824 inode->i_atime = tv;
825 inode->i_ctime = tv;
Christoph Hellwigdff35fd2008-08-13 16:44:15 +1000826
Christoph Hellwig031474c2021-03-29 11:11:41 -0700827 ip->i_extsize = 0;
Christoph Hellwigdb073492021-03-29 11:11:44 -0700828 ip->i_diflags = 0;
Christoph Hellwig93848a92013-04-03 16:11:17 +1100829
Christoph Hellwig6471e9c2020-03-18 08:15:11 -0700830 if (xfs_sb_version_has_v3inode(&mp->m_sb)) {
Jeff Laytonf0e28282017-12-11 06:35:19 -0500831 inode_set_iversion(inode, 1);
Christoph Hellwigb33ce572021-03-29 11:11:42 -0700832 ip->i_cowextsize = 0;
Christoph Hellwige98d5e82021-03-29 11:11:45 -0700833 ip->i_crtime = tv;
Christoph Hellwig93848a92013-04-03 16:11:17 +1100834 }
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 flags = XFS_ILOG_CORE;
837 switch (mode & S_IFMT) {
838 case S_IFIFO:
839 case S_IFCHR:
840 case S_IFBLK:
841 case S_IFSOCK:
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700842 ip->i_df.if_format = XFS_DINODE_FMT_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 flags |= XFS_ILOG_DEV;
844 break;
845 case S_IFREG:
846 case S_IFDIR:
Christoph Hellwigdb073492021-03-29 11:11:44 -0700847 if (pip && (pip->i_diflags & XFS_DIFLAG_ANY))
Darrick J. Wong8a569d72020-09-13 10:16:40 -0700848 xfs_inode_inherit_flags(ip, pip);
Christoph Hellwig3e09ab82021-03-29 11:11:45 -0700849 if (pip && (pip->i_diflags2 & XFS_DIFLAG2_ANY))
Darrick J. Wong8a569d72020-09-13 10:16:40 -0700850 xfs_inode_inherit_flags2(ip, pip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 /* FALLTHROUGH */
852 case S_IFLNK:
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700853 ip->i_df.if_format = XFS_DINODE_FMT_EXTENTS;
Christoph Hellwigfcacbc32018-07-17 16:51:50 -0700854 ip->i_df.if_bytes = 0;
Christoph Hellwig6bdcf262017-11-03 10:34:46 -0700855 ip->i_df.if_u1.if_root = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 break;
857 default:
858 ASSERT(0);
859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 /*
Dave Chinnere6a688c2021-03-22 09:52:03 -0700862 * If we need to create attributes immediately after allocating the
863 * inode, initialise an empty attribute fork right now. We use the
864 * default fork offset for attributes here as we don't know exactly what
865 * size or how many attributes we might be adding. We can do this
866 * safely here because we know the data fork is completely empty and
867 * this saves us from needing to run a separate transaction to set the
868 * fork offset in the immediate future.
869 */
Dave Chinner2442ee12021-04-06 07:01:00 -0700870 if (init_xattrs && xfs_sb_version_hasattr(&mp->m_sb)) {
Christoph Hellwig7821ea32021-03-29 11:11:44 -0700871 ip->i_forkoff = xfs_default_attroffset(ip) >> 3;
Dave Chinnere6a688c2021-03-22 09:52:03 -0700872 ip->i_afp = xfs_ifork_alloc(XFS_DINODE_FMT_EXTENTS, 0);
873 }
874
875 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 * Log the new values stuffed into the inode.
877 */
Christoph Hellwigddc34152011-09-19 15:00:54 +0000878 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 xfs_trans_log_inode(tp, ip, flags);
880
Dave Chinner58c90472015-02-23 22:38:08 +1100881 /* now that we have an i_mode we can setup the inode structure */
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000882 xfs_setup_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 *ipp = ip;
885 return 0;
886}
887
Dave Chinnere546cb72013-08-12 20:49:47 +1000888/*
Dave Chinner54d7b5c2016-02-09 16:54:58 +1100889 * Decrement the link count on an inode & log the change. If this causes the
890 * link count to go to zero, move the inode to AGI unlinked list so that it can
891 * be freed when the last active reference goes away via xfs_inactive().
Dave Chinnere546cb72013-08-12 20:49:47 +1000892 */
Eric Sandeen0d5a75e2016-06-01 17:38:15 +1000893static int /* error */
Dave Chinnere546cb72013-08-12 20:49:47 +1000894xfs_droplink(
895 xfs_trans_t *tp,
896 xfs_inode_t *ip)
897{
Dave Chinnere546cb72013-08-12 20:49:47 +1000898 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
899
Dave Chinnere546cb72013-08-12 20:49:47 +1000900 drop_nlink(VFS_I(ip));
901 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
902
Dave Chinner54d7b5c2016-02-09 16:54:58 +1100903 if (VFS_I(ip)->i_nlink)
904 return 0;
905
906 return xfs_iunlink(tp, ip);
Dave Chinnere546cb72013-08-12 20:49:47 +1000907}
908
909/*
Dave Chinnere546cb72013-08-12 20:49:47 +1000910 * Increment the link count on an inode & log the change.
911 */
Eric Sandeen91083262019-05-01 20:26:30 -0700912static void
Dave Chinnere546cb72013-08-12 20:49:47 +1000913xfs_bumplink(
914 xfs_trans_t *tp,
915 xfs_inode_t *ip)
916{
917 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
918
Dave Chinnere546cb72013-08-12 20:49:47 +1000919 inc_nlink(VFS_I(ip));
Dave Chinnere546cb72013-08-12 20:49:47 +1000920 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Dave Chinnere546cb72013-08-12 20:49:47 +1000921}
922
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000923int
924xfs_create(
Christoph Hellwigf736d932021-01-21 14:19:58 +0100925 struct user_namespace *mnt_userns,
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000926 xfs_inode_t *dp,
927 struct xfs_name *name,
928 umode_t mode,
Christoph Hellwig66f36462017-10-19 11:07:09 -0700929 dev_t rdev,
Dave Chinnere6a688c2021-03-22 09:52:03 -0700930 bool init_xattrs,
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000931 xfs_inode_t **ipp)
932{
933 int is_dir = S_ISDIR(mode);
934 struct xfs_mount *mp = dp->i_mount;
935 struct xfs_inode *ip = NULL;
936 struct xfs_trans *tp = NULL;
937 int error;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000938 bool unlock_dp_on_error = false;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000939 prid_t prid;
940 struct xfs_dquot *udqp = NULL;
941 struct xfs_dquot *gdqp = NULL;
942 struct xfs_dquot *pdqp = NULL;
Brian Foster062647a2014-11-28 14:00:16 +1100943 struct xfs_trans_res *tres;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000944 uint resblks;
Dave Chinnerb652afd2021-06-02 10:48:24 +1000945 xfs_ino_t ino;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000946
947 trace_xfs_create(dp, name);
948
949 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +1000950 return -EIO;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000951
Zhi Yong Wu163467d2013-12-18 08:22:39 +0800952 prid = xfs_get_initial_prid(dp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000953
954 /*
955 * Make sure that we have allocated dquot(s) on disk.
956 */
Christian Braunera65e58e2021-03-20 13:26:22 +0100957 error = xfs_qm_vop_dqalloc(dp, mapped_fsuid(mnt_userns),
958 mapped_fsgid(mnt_userns), prid,
Darrick J. Wongb5a08422021-03-02 09:32:52 -0800959 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
960 &udqp, &gdqp, &pdqp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000961 if (error)
962 return error;
963
964 if (is_dir) {
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000965 resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
Brian Foster062647a2014-11-28 14:00:16 +1100966 tres = &M_RES(mp)->tr_mkdir;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000967 } else {
968 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
Brian Foster062647a2014-11-28 14:00:16 +1100969 tres = &M_RES(mp)->tr_create;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000970 }
971
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000972 /*
973 * Initially assume that the file does not exist and
974 * reserve the resources for that case. If that is not
975 * the case we'll drop the one we have and get a more
976 * appropriate transaction later.
977 */
Darrick J. Wongf2f7b9f2021-01-27 12:07:57 -0800978 error = xfs_trans_alloc_icreate(mp, tres, udqp, gdqp, pdqp, resblks,
979 &tp);
Dave Chinner24513372014-06-25 14:58:08 +1000980 if (error == -ENOSPC) {
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000981 /* flush outstanding delalloc blocks and retry */
982 xfs_flush_inodes(mp);
Darrick J. Wongf2f7b9f2021-01-27 12:07:57 -0800983 error = xfs_trans_alloc_icreate(mp, tres, udqp, gdqp, pdqp,
984 resblks, &tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000985 }
Christoph Hellwig4906e212015-06-04 13:47:56 +1000986 if (error)
Darrick J. Wongf2f7b9f2021-01-27 12:07:57 -0800987 goto out_release_dquots;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000988
Christoph Hellwig65523212016-11-30 14:33:25 +1100989 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000990 unlock_dp_on_error = true;
991
Chandan Babu Rf5d92742021-01-22 16:48:12 -0800992 error = xfs_iext_count_may_overflow(dp, XFS_DATA_FORK,
993 XFS_IEXT_DIR_MANIP_CNT(mp));
994 if (error)
995 goto out_trans_cancel;
996
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000997 /*
998 * A newly created regular or special file just has one directory
999 * entry pointing to them, but a directory also the "." entry
1000 * pointing to itself.
1001 */
Dave Chinnerb652afd2021-06-02 10:48:24 +10001002 error = xfs_dialloc(&tp, dp->i_ino, mode, &ino);
1003 if (!error)
1004 error = xfs_init_new_inode(mnt_userns, tp, dp, ino, mode,
1005 is_dir ? 2 : 1, rdev, prid, init_xattrs, &ip);
Jan Karad6077aa2015-07-29 11:52:08 +10001006 if (error)
Christoph Hellwig4906e212015-06-04 13:47:56 +10001007 goto out_trans_cancel;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001008
1009 /*
1010 * Now we join the directory inode to the transaction. We do not do it
Dave Chinnerb652afd2021-06-02 10:48:24 +10001011 * earlier because xfs_dialloc might commit the previous transaction
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001012 * (and release all the locks). An error from here on will result in
1013 * the transaction cancel unlocking dp so don't do it explicitly in the
1014 * error path.
1015 */
Christoph Hellwig65523212016-11-30 14:33:25 +11001016 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001017 unlock_dp_on_error = false;
1018
Brian Foster381eee62018-07-11 22:26:21 -07001019 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
Kaixu Xia63337b62020-03-27 08:28:39 -07001020 resblks - XFS_IALLOC_SPACE_RES(mp));
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001021 if (error) {
Dave Chinner24513372014-06-25 14:58:08 +10001022 ASSERT(error != -ENOSPC);
Christoph Hellwig4906e212015-06-04 13:47:56 +10001023 goto out_trans_cancel;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001024 }
1025 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1026 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1027
1028 if (is_dir) {
1029 error = xfs_dir_init(tp, ip, dp);
1030 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -07001031 goto out_trans_cancel;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001032
Eric Sandeen91083262019-05-01 20:26:30 -07001033 xfs_bumplink(tp, dp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001034 }
1035
1036 /*
1037 * If this is a synchronous mount, make sure that the
1038 * create transaction goes to disk before returning to
1039 * the user.
1040 */
1041 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1042 xfs_trans_set_sync(tp);
1043
1044 /*
1045 * Attach the dquot(s) to the inodes and modify them incore.
1046 * These ids of the inode couldn't have changed since the new
1047 * inode has been locked ever since it was created.
1048 */
1049 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
1050
Christoph Hellwig70393312015-06-04 13:48:08 +10001051 error = xfs_trans_commit(tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001052 if (error)
1053 goto out_release_inode;
1054
1055 xfs_qm_dqrele(udqp);
1056 xfs_qm_dqrele(gdqp);
1057 xfs_qm_dqrele(pdqp);
1058
1059 *ipp = ip;
1060 return 0;
1061
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001062 out_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001063 xfs_trans_cancel(tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001064 out_release_inode:
1065 /*
Dave Chinner58c90472015-02-23 22:38:08 +11001066 * Wait until after the current transaction is aborted to finish the
1067 * setup of the inode and release the inode. This prevents recursive
1068 * transactions and deadlocks from xfs_inactive.
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001069 */
Dave Chinner58c90472015-02-23 22:38:08 +11001070 if (ip) {
1071 xfs_finish_inode_setup(ip);
Darrick J. Wong44a87362018-07-25 12:52:32 -07001072 xfs_irele(ip);
Dave Chinner58c90472015-02-23 22:38:08 +11001073 }
Darrick J. Wongf2f7b9f2021-01-27 12:07:57 -08001074 out_release_dquots:
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001075 xfs_qm_dqrele(udqp);
1076 xfs_qm_dqrele(gdqp);
1077 xfs_qm_dqrele(pdqp);
1078
1079 if (unlock_dp_on_error)
Christoph Hellwig65523212016-11-30 14:33:25 +11001080 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001081 return error;
1082}
1083
1084int
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001085xfs_create_tmpfile(
Christoph Hellwigf736d932021-01-21 14:19:58 +01001086 struct user_namespace *mnt_userns,
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001087 struct xfs_inode *dp,
Brian Foster330033d2014-04-17 08:15:30 +10001088 umode_t mode,
1089 struct xfs_inode **ipp)
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001090{
1091 struct xfs_mount *mp = dp->i_mount;
1092 struct xfs_inode *ip = NULL;
1093 struct xfs_trans *tp = NULL;
1094 int error;
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001095 prid_t prid;
1096 struct xfs_dquot *udqp = NULL;
1097 struct xfs_dquot *gdqp = NULL;
1098 struct xfs_dquot *pdqp = NULL;
1099 struct xfs_trans_res *tres;
1100 uint resblks;
Dave Chinnerb652afd2021-06-02 10:48:24 +10001101 xfs_ino_t ino;
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001102
1103 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10001104 return -EIO;
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001105
1106 prid = xfs_get_initial_prid(dp);
1107
1108 /*
1109 * Make sure that we have allocated dquot(s) on disk.
1110 */
Christian Braunera65e58e2021-03-20 13:26:22 +01001111 error = xfs_qm_vop_dqalloc(dp, mapped_fsuid(mnt_userns),
1112 mapped_fsgid(mnt_userns), prid,
Darrick J. Wongb5a08422021-03-02 09:32:52 -08001113 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
1114 &udqp, &gdqp, &pdqp);
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001115 if (error)
1116 return error;
1117
1118 resblks = XFS_IALLOC_SPACE_RES(mp);
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001119 tres = &M_RES(mp)->tr_create_tmpfile;
Christoph Hellwig253f4912016-04-06 09:19:55 +10001120
Darrick J. Wongf2f7b9f2021-01-27 12:07:57 -08001121 error = xfs_trans_alloc_icreate(mp, tres, udqp, gdqp, pdqp, resblks,
1122 &tp);
Christoph Hellwig4906e212015-06-04 13:47:56 +10001123 if (error)
Darrick J. Wongf2f7b9f2021-01-27 12:07:57 -08001124 goto out_release_dquots;
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001125
Dave Chinnerb652afd2021-06-02 10:48:24 +10001126 error = xfs_dialloc(&tp, dp->i_ino, mode, &ino);
1127 if (!error)
1128 error = xfs_init_new_inode(mnt_userns, tp, dp, ino, mode,
1129 0, 0, prid, false, &ip);
Jan Karad6077aa2015-07-29 11:52:08 +10001130 if (error)
Christoph Hellwig4906e212015-06-04 13:47:56 +10001131 goto out_trans_cancel;
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001132
1133 if (mp->m_flags & XFS_MOUNT_WSYNC)
1134 xfs_trans_set_sync(tp);
1135
1136 /*
1137 * Attach the dquot(s) to the inodes and modify them incore.
1138 * These ids of the inode couldn't have changed since the new
1139 * inode has been locked ever since it was created.
1140 */
1141 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
1142
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001143 error = xfs_iunlink(tp, ip);
1144 if (error)
Christoph Hellwig4906e212015-06-04 13:47:56 +10001145 goto out_trans_cancel;
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001146
Christoph Hellwig70393312015-06-04 13:48:08 +10001147 error = xfs_trans_commit(tp);
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001148 if (error)
1149 goto out_release_inode;
1150
1151 xfs_qm_dqrele(udqp);
1152 xfs_qm_dqrele(gdqp);
1153 xfs_qm_dqrele(pdqp);
1154
Brian Foster330033d2014-04-17 08:15:30 +10001155 *ipp = ip;
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001156 return 0;
1157
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001158 out_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001159 xfs_trans_cancel(tp);
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001160 out_release_inode:
1161 /*
Dave Chinner58c90472015-02-23 22:38:08 +11001162 * Wait until after the current transaction is aborted to finish the
1163 * setup of the inode and release the inode. This prevents recursive
1164 * transactions and deadlocks from xfs_inactive.
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001165 */
Dave Chinner58c90472015-02-23 22:38:08 +11001166 if (ip) {
1167 xfs_finish_inode_setup(ip);
Darrick J. Wong44a87362018-07-25 12:52:32 -07001168 xfs_irele(ip);
Dave Chinner58c90472015-02-23 22:38:08 +11001169 }
Darrick J. Wongf2f7b9f2021-01-27 12:07:57 -08001170 out_release_dquots:
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001171 xfs_qm_dqrele(udqp);
1172 xfs_qm_dqrele(gdqp);
1173 xfs_qm_dqrele(pdqp);
1174
1175 return error;
1176}
1177
1178int
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001179xfs_link(
1180 xfs_inode_t *tdp,
1181 xfs_inode_t *sip,
1182 struct xfs_name *target_name)
1183{
1184 xfs_mount_t *mp = tdp->i_mount;
1185 xfs_trans_t *tp;
1186 int error;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001187 int resblks;
1188
1189 trace_xfs_link(tdp, target_name);
1190
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001191 ASSERT(!S_ISDIR(VFS_I(sip)->i_mode));
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001192
1193 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10001194 return -EIO;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001195
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -07001196 error = xfs_qm_dqattach(sip);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001197 if (error)
1198 goto std_return;
1199
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -07001200 error = xfs_qm_dqattach(tdp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001201 if (error)
1202 goto std_return;
1203
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001204 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001205 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_link, resblks, 0, 0, &tp);
Dave Chinner24513372014-06-25 14:58:08 +10001206 if (error == -ENOSPC) {
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001207 resblks = 0;
Christoph Hellwig253f4912016-04-06 09:19:55 +10001208 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_link, 0, 0, 0, &tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001209 }
Christoph Hellwig4906e212015-06-04 13:47:56 +10001210 if (error)
Christoph Hellwig253f4912016-04-06 09:19:55 +10001211 goto std_return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001212
Darrick J. Wong7c2d2382018-01-26 15:27:33 -08001213 xfs_lock_two_inodes(sip, XFS_ILOCK_EXCL, tdp, XFS_ILOCK_EXCL);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001214
1215 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
Christoph Hellwig65523212016-11-30 14:33:25 +11001216 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001217
Chandan Babu Rf5d92742021-01-22 16:48:12 -08001218 error = xfs_iext_count_may_overflow(tdp, XFS_DATA_FORK,
1219 XFS_IEXT_DIR_MANIP_CNT(mp));
1220 if (error)
1221 goto error_return;
1222
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001223 /*
1224 * If we are using project inheritance, we only allow hard link
1225 * creation in our tree when the project IDs are the same; else
1226 * the tree quota mechanism could be circumvented.
1227 */
Christoph Hellwigdb073492021-03-29 11:11:44 -07001228 if (unlikely((tdp->i_diflags & XFS_DIFLAG_PROJINHERIT) &&
Christoph Hellwigceaf6032021-03-29 11:11:39 -07001229 tdp->i_projid != sip->i_projid)) {
Dave Chinner24513372014-06-25 14:58:08 +10001230 error = -EXDEV;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001231 goto error_return;
1232 }
1233
Eric Sandeen94f3cad2014-09-09 11:57:52 +10001234 if (!resblks) {
1235 error = xfs_dir_canenter(tp, tdp, target_name);
1236 if (error)
1237 goto error_return;
1238 }
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001239
Dave Chinner54d7b5c2016-02-09 16:54:58 +11001240 /*
1241 * Handle initial link state of O_TMPFILE inode
1242 */
1243 if (VFS_I(sip)->i_nlink == 0) {
Zhi Yong Wuab297432013-12-18 08:22:41 +08001244 error = xfs_iunlink_remove(tp, sip);
1245 if (error)
Christoph Hellwig4906e212015-06-04 13:47:56 +10001246 goto error_return;
Zhi Yong Wuab297432013-12-18 08:22:41 +08001247 }
1248
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001249 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
Brian Foster381eee62018-07-11 22:26:21 -07001250 resblks);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001251 if (error)
Christoph Hellwig4906e212015-06-04 13:47:56 +10001252 goto error_return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001253 xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1254 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
1255
Eric Sandeen91083262019-05-01 20:26:30 -07001256 xfs_bumplink(tp, sip);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001257
1258 /*
1259 * If this is a synchronous mount, make sure that the
1260 * link transaction goes to disk before returning to
1261 * the user.
1262 */
Eric Sandeenf6106ef2016-01-11 11:34:01 +11001263 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001264 xfs_trans_set_sync(tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001265
Christoph Hellwig70393312015-06-04 13:48:08 +10001266 return xfs_trans_commit(tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001267
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001268 error_return:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001269 xfs_trans_cancel(tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001270 std_return:
1271 return error;
1272}
1273
Darrick J. Wong363e59b2017-12-14 15:42:59 -08001274/* Clear the reflink flag and the cowblocks tag if possible. */
1275static void
1276xfs_itruncate_clear_reflink_flags(
1277 struct xfs_inode *ip)
1278{
1279 struct xfs_ifork *dfork;
1280 struct xfs_ifork *cfork;
1281
1282 if (!xfs_is_reflink_inode(ip))
1283 return;
1284 dfork = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1285 cfork = XFS_IFORK_PTR(ip, XFS_COW_FORK);
1286 if (dfork->if_bytes == 0 && cfork->if_bytes == 0)
Christoph Hellwig3e09ab82021-03-29 11:11:45 -07001287 ip->i_diflags2 &= ~XFS_DIFLAG2_REFLINK;
Darrick J. Wong363e59b2017-12-14 15:42:59 -08001288 if (cfork->if_bytes == 0)
1289 xfs_inode_clear_cowblocks_tag(ip);
1290}
1291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292/*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001293 * Free up the underlying blocks past new_size. The new size must be smaller
1294 * than the current size. This routine can be used both for the attribute and
1295 * data fork, and does not modify the inode size, which is left to the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 *
David Chinnerf6485052008-04-17 16:50:04 +10001297 * The transaction passed to this routine must have made a permanent log
1298 * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the
1299 * given transaction and start new ones, so make sure everything involved in
1300 * the transaction is tidy before calling here. Some transaction will be
1301 * returned to the caller to be committed. The incoming transaction must
1302 * already include the inode, and both inode locks must be held exclusively.
1303 * The inode must also be "held" within the transaction. On return the inode
1304 * will be "held" within the returned transaction. This routine does NOT
1305 * require any disk space to be reserved for it within the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 *
David Chinnerf6485052008-04-17 16:50:04 +10001307 * If we get an error, we must return with the inode locked and linked into the
1308 * current transaction. This keeps things simple for the higher level code,
1309 * because it always knows that the inode is locked and held in the transaction
1310 * that returns to it whether errors occur or not. We don't mark the inode
1311 * dirty on error so that transactions can be easily aborted if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 */
1313int
Brian Foster4e529332018-05-10 09:35:42 -07001314xfs_itruncate_extents_flags(
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001315 struct xfs_trans **tpp,
1316 struct xfs_inode *ip,
1317 int whichfork,
Brian Foster13b86fc2018-05-09 08:45:04 -07001318 xfs_fsize_t new_size,
Brian Foster4e529332018-05-10 09:35:42 -07001319 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001321 struct xfs_mount *mp = ip->i_mount;
1322 struct xfs_trans *tp = *tpp;
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001323 xfs_fileoff_t first_unmap_block;
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001324 xfs_filblks_t unmap_len;
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001325 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Christoph Hellwig0b561852012-07-04 11:13:31 -04001327 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1328 ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
1329 xfs_isilocked(ip, XFS_IOLOCK_EXCL));
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00001330 ASSERT(new_size <= XFS_ISIZE(ip));
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001331 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 ASSERT(ip->i_itemp != NULL);
Christoph Hellwig898621d2010-06-24 11:36:58 +10001333 ASSERT(ip->i_itemp->ili_lock_flags == 0);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001334 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Christoph Hellwig673e8e52011-12-18 20:00:04 +00001336 trace_xfs_itruncate_extents_start(ip, new_size);
1337
Brian Foster4e529332018-05-10 09:35:42 -07001338 flags |= xfs_bmapi_aflag(whichfork);
Brian Foster13b86fc2018-05-09 08:45:04 -07001339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 /*
1341 * Since it is possible for space to become allocated beyond
1342 * the end of the file (in a crash where the space is allocated
1343 * but the inode size is not yet updated), simply remove any
1344 * blocks which show up between the new EOF and the maximum
Darrick J. Wong4bbb04a2020-01-02 13:20:13 -08001345 * possible file size.
1346 *
1347 * We have to free all the blocks to the bmbt maximum offset, even if
1348 * the page cache can't scale that far.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001350 first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
Darrick J. Wong33005fd2020-12-04 13:28:35 -08001351 if (!xfs_verify_fileoff(mp, first_unmap_block)) {
Darrick J. Wong4bbb04a2020-01-02 13:20:13 -08001352 WARN_ON_ONCE(first_unmap_block > XFS_MAX_FILEOFF);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001353 return 0;
Darrick J. Wong4bbb04a2020-01-02 13:20:13 -08001354 }
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001355
Darrick J. Wong4bbb04a2020-01-02 13:20:13 -08001356 unmap_len = XFS_MAX_FILEOFF - first_unmap_block + 1;
1357 while (unmap_len > 0) {
Brian Foster02dff7b2018-07-24 13:43:07 -07001358 ASSERT(tp->t_firstblock == NULLFSBLOCK);
Darrick J. Wong4bbb04a2020-01-02 13:20:13 -08001359 error = __xfs_bunmapi(tp, ip, first_unmap_block, &unmap_len,
1360 flags, XFS_ITRUNC_MAX_EXTENTS);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001361 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +10001362 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Brian Foster6dd379c2020-09-15 20:44:46 -07001364 /* free the just unmapped extents */
Brian Foster9e28a242018-07-24 13:43:15 -07001365 error = xfs_defer_finish(&tp);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001366 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -07001367 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001369
Darrick J. Wong4919d422018-04-10 08:28:33 -07001370 if (whichfork == XFS_DATA_FORK) {
1371 /* Remove all pending CoW reservations. */
1372 error = xfs_reflink_cancel_cow_blocks(ip, &tp,
Darrick J. Wong4bbb04a2020-01-02 13:20:13 -08001373 first_unmap_block, XFS_MAX_FILEOFF, true);
Darrick J. Wong4919d422018-04-10 08:28:33 -07001374 if (error)
1375 goto out;
Darrick J. Wongaa8968f2016-10-03 09:11:38 -07001376
Darrick J. Wong4919d422018-04-10 08:28:33 -07001377 xfs_itruncate_clear_reflink_flags(ip);
1378 }
Darrick J. Wongaa8968f2016-10-03 09:11:38 -07001379
Christoph Hellwig673e8e52011-12-18 20:00:04 +00001380 /*
1381 * Always re-log the inode so that our permanent transaction can keep
1382 * on rolling it forward in the log.
1383 */
1384 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1385
1386 trace_xfs_itruncate_extents_end(ip, new_size);
1387
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001388out:
1389 *tpp = tp;
1390 return error;
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001391}
1392
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001393int
1394xfs_release(
1395 xfs_inode_t *ip)
1396{
1397 xfs_mount_t *mp = ip->i_mount;
Darrick J. Wong7d883292021-03-23 16:59:31 -07001398 int error = 0;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001399
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001400 if (!S_ISREG(VFS_I(ip)->i_mode) || (VFS_I(ip)->i_mode == 0))
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001401 return 0;
1402
1403 /* If this is a read-only mount, don't do this (would generate I/O) */
1404 if (mp->m_flags & XFS_MOUNT_RDONLY)
1405 return 0;
1406
1407 if (!XFS_FORCED_SHUTDOWN(mp)) {
1408 int truncated;
1409
1410 /*
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001411 * If we previously truncated this file and removed old data
1412 * in the process, we want to initiate "early" writeout on
1413 * the last close. This is an attempt to combat the notorious
1414 * NULL files problem which is particularly noticeable from a
1415 * truncate down, buffered (re-)write (delalloc), followed by
1416 * a crash. What we are effectively doing here is
1417 * significantly reducing the time window where we'd otherwise
1418 * be exposed to that problem.
1419 */
1420 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
1421 if (truncated) {
1422 xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
Dave Chinnereac152b2014-08-04 13:22:49 +10001423 if (ip->i_delayed_blks > 0) {
Dave Chinner24513372014-06-25 14:58:08 +10001424 error = filemap_flush(VFS_I(ip)->i_mapping);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001425 if (error)
1426 return error;
1427 }
1428 }
1429 }
1430
Dave Chinner54d7b5c2016-02-09 16:54:58 +11001431 if (VFS_I(ip)->i_nlink == 0)
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001432 return 0;
1433
Darrick J. Wong7d883292021-03-23 16:59:31 -07001434 /*
1435 * If we can't get the iolock just skip truncating the blocks past EOF
1436 * because we could deadlock with the mmap_lock otherwise. We'll get
1437 * another chance to drop them once the last reference to the inode is
1438 * dropped, so we'll never leak blocks permanently.
1439 */
1440 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL))
1441 return 0;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001442
Darrick J. Wong7d883292021-03-23 16:59:31 -07001443 if (xfs_can_free_eofblocks(ip, false)) {
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001444 /*
Brian Fostera36b9262017-01-27 23:22:55 -08001445 * Check if the inode is being opened, written and closed
1446 * frequently and we have delayed allocation blocks outstanding
1447 * (e.g. streaming writes from the NFS server), truncating the
1448 * blocks past EOF will cause fragmentation to occur.
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001449 *
Brian Fostera36b9262017-01-27 23:22:55 -08001450 * In this case don't do the truncation, but we have to be
1451 * careful how we detect this case. Blocks beyond EOF show up as
1452 * i_delayed_blks even when the inode is clean, so we need to
1453 * truncate them away first before checking for a dirty release.
1454 * Hence on the first dirty close we will still remove the
1455 * speculative allocation, but after that we will leave it in
1456 * place.
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001457 */
1458 if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
Darrick J. Wong7d883292021-03-23 16:59:31 -07001459 goto out_unlock;
1460
1461 error = xfs_free_eofblocks(ip);
1462 if (error)
1463 goto out_unlock;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001464
1465 /* delalloc blocks after truncation means it really is dirty */
1466 if (ip->i_delayed_blks)
1467 xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
1468 }
Darrick J. Wong7d883292021-03-23 16:59:31 -07001469
1470out_unlock:
1471 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1472 return error;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001473}
1474
1475/*
Brian Fosterf7be2d72013-09-20 11:06:10 -04001476 * xfs_inactive_truncate
1477 *
1478 * Called to perform a truncate when an inode becomes unlinked.
1479 */
1480STATIC int
1481xfs_inactive_truncate(
1482 struct xfs_inode *ip)
1483{
1484 struct xfs_mount *mp = ip->i_mount;
1485 struct xfs_trans *tp;
1486 int error;
1487
Christoph Hellwig253f4912016-04-06 09:19:55 +10001488 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
Brian Fosterf7be2d72013-09-20 11:06:10 -04001489 if (error) {
1490 ASSERT(XFS_FORCED_SHUTDOWN(mp));
Brian Fosterf7be2d72013-09-20 11:06:10 -04001491 return error;
1492 }
Brian Fosterf7be2d72013-09-20 11:06:10 -04001493 xfs_ilock(ip, XFS_ILOCK_EXCL);
1494 xfs_trans_ijoin(tp, ip, 0);
1495
1496 /*
1497 * Log the inode size first to prevent stale data exposure in the event
1498 * of a system crash before the truncate completes. See the related
Jan Kara69bca802016-05-26 14:46:43 +02001499 * comment in xfs_vn_setattr_size() for details.
Brian Fosterf7be2d72013-09-20 11:06:10 -04001500 */
Christoph Hellwig13d2c102021-03-29 11:11:40 -07001501 ip->i_disk_size = 0;
Brian Fosterf7be2d72013-09-20 11:06:10 -04001502 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1503
1504 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
1505 if (error)
1506 goto error_trans_cancel;
1507
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001508 ASSERT(ip->i_df.if_nextents == 0);
Brian Fosterf7be2d72013-09-20 11:06:10 -04001509
Christoph Hellwig70393312015-06-04 13:48:08 +10001510 error = xfs_trans_commit(tp);
Brian Fosterf7be2d72013-09-20 11:06:10 -04001511 if (error)
1512 goto error_unlock;
1513
1514 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1515 return 0;
1516
1517error_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001518 xfs_trans_cancel(tp);
Brian Fosterf7be2d72013-09-20 11:06:10 -04001519error_unlock:
1520 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1521 return error;
1522}
1523
1524/*
Brian Foster88877d22013-09-20 11:06:11 -04001525 * xfs_inactive_ifree()
1526 *
1527 * Perform the inode free when an inode is unlinked.
1528 */
1529STATIC int
1530xfs_inactive_ifree(
1531 struct xfs_inode *ip)
1532{
Brian Foster88877d22013-09-20 11:06:11 -04001533 struct xfs_mount *mp = ip->i_mount;
1534 struct xfs_trans *tp;
1535 int error;
1536
Brian Foster9d43b182014-04-24 16:00:52 +10001537 /*
Christoph Hellwig76d771b2017-01-25 07:49:35 -08001538 * We try to use a per-AG reservation for any block needed by the finobt
1539 * tree, but as the finobt feature predates the per-AG reservation
1540 * support a degraded file system might not have enough space for the
1541 * reservation at mount time. In that case try to dip into the reserved
1542 * pool and pray.
Brian Foster9d43b182014-04-24 16:00:52 +10001543 *
1544 * Send a warning if the reservation does happen to fail, as the inode
1545 * now remains allocated and sits on the unlinked list until the fs is
1546 * repaired.
1547 */
Darrick J. Wonge1f6ca12019-02-14 09:33:15 -08001548 if (unlikely(mp->m_finobt_nores)) {
Christoph Hellwig76d771b2017-01-25 07:49:35 -08001549 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ifree,
1550 XFS_IFREE_SPACE_RES(mp), 0, XFS_TRANS_RESERVE,
1551 &tp);
1552 } else {
1553 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ifree, 0, 0, 0, &tp);
1554 }
Brian Foster88877d22013-09-20 11:06:11 -04001555 if (error) {
Dave Chinner24513372014-06-25 14:58:08 +10001556 if (error == -ENOSPC) {
Brian Foster9d43b182014-04-24 16:00:52 +10001557 xfs_warn_ratelimited(mp,
1558 "Failed to remove inode(s) from unlinked list. "
1559 "Please free space, unmount and run xfs_repair.");
1560 } else {
1561 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1562 }
Brian Foster88877d22013-09-20 11:06:11 -04001563 return error;
1564 }
1565
Dave Chinner96355d5a2020-06-29 14:48:45 -07001566 /*
1567 * We do not hold the inode locked across the entire rolling transaction
1568 * here. We only need to hold it for the first transaction that
1569 * xfs_ifree() builds, which may mark the inode XFS_ISTALE if the
1570 * underlying cluster buffer is freed. Relogging an XFS_ISTALE inode
1571 * here breaks the relationship between cluster buffer invalidation and
1572 * stale inode invalidation on cluster buffer item journal commit
1573 * completion, and can result in leaving dirty stale inodes hanging
1574 * around in memory.
1575 *
1576 * We have no need for serialising this inode operation against other
1577 * operations - we freed the inode and hence reallocation is required
1578 * and that will serialise on reallocating the space the deferops need
1579 * to free. Hence we can unlock the inode on the first commit of
1580 * the transaction rather than roll it right through the deferops. This
1581 * avoids relogging the XFS_ISTALE inode.
1582 *
1583 * We check that xfs_ifree() hasn't grown an internal transaction roll
1584 * by asserting that the inode is still locked when it returns.
1585 */
Brian Foster88877d22013-09-20 11:06:11 -04001586 xfs_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinner96355d5a2020-06-29 14:48:45 -07001587 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Brian Foster88877d22013-09-20 11:06:11 -04001588
Brian Foster0e0417f2018-07-11 22:26:07 -07001589 error = xfs_ifree(tp, ip);
Dave Chinner96355d5a2020-06-29 14:48:45 -07001590 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Brian Foster88877d22013-09-20 11:06:11 -04001591 if (error) {
1592 /*
1593 * If we fail to free the inode, shut down. The cancel
1594 * might do that, we need to make sure. Otherwise the
1595 * inode might be lost for a long time or forever.
1596 */
1597 if (!XFS_FORCED_SHUTDOWN(mp)) {
1598 xfs_notice(mp, "%s: xfs_ifree returned error %d",
1599 __func__, error);
1600 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1601 }
Christoph Hellwig4906e212015-06-04 13:47:56 +10001602 xfs_trans_cancel(tp);
Brian Foster88877d22013-09-20 11:06:11 -04001603 return error;
1604 }
1605
1606 /*
1607 * Credit the quota account(s). The inode is gone.
1608 */
1609 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1610
1611 /*
Brian Fosterd4a97a02015-08-19 10:01:40 +10001612 * Just ignore errors at this point. There is nothing we can do except
1613 * to try to keep going. Make sure it's not a silent error.
Brian Foster88877d22013-09-20 11:06:11 -04001614 */
Christoph Hellwig70393312015-06-04 13:48:08 +10001615 error = xfs_trans_commit(tp);
Brian Foster88877d22013-09-20 11:06:11 -04001616 if (error)
1617 xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
1618 __func__, error);
1619
Brian Foster88877d22013-09-20 11:06:11 -04001620 return 0;
1621}
1622
1623/*
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001624 * xfs_inactive
1625 *
1626 * This is called when the vnode reference count for the vnode
1627 * goes to zero. If the file has been unlinked, then it must
1628 * now be truncated. Also, we clear all of the read-ahead state
1629 * kept for the inode here since the file is now closed.
1630 */
Brian Foster74564fb2013-09-20 11:06:12 -04001631void
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001632xfs_inactive(
1633 xfs_inode_t *ip)
1634{
Jie Liu3d3c8b52013-08-12 20:49:59 +10001635 struct xfs_mount *mp;
Jie Liu3d3c8b52013-08-12 20:49:59 +10001636 int error;
1637 int truncate = 0;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001638
1639 /*
1640 * If the inode is already free, then there can be nothing
1641 * to clean up here.
1642 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001643 if (VFS_I(ip)->i_mode == 0) {
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001644 ASSERT(ip->i_df.if_broot_bytes == 0);
Brian Foster74564fb2013-09-20 11:06:12 -04001645 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001646 }
1647
1648 mp = ip->i_mount;
Darrick J. Wong17c12bc2016-10-03 09:11:29 -07001649 ASSERT(!xfs_iflags_test(ip, XFS_IRECOVERY));
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001650
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001651 /* If this is a read-only mount, don't do this (would generate I/O) */
1652 if (mp->m_flags & XFS_MOUNT_RDONLY)
Brian Foster74564fb2013-09-20 11:06:12 -04001653 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001654
Darrick J. Wong383e32b2021-03-22 09:51:54 -07001655 /* Metadata inodes require explicit resource cleanup. */
1656 if (xfs_is_metadata_inode(ip))
1657 return;
1658
Darrick J. Wong62318482018-03-06 17:08:31 -08001659 /* Try to clean out the cow blocks if there are any. */
Christoph Hellwig51d62692018-07-17 16:51:51 -07001660 if (xfs_inode_has_cow_data(ip))
Darrick J. Wong62318482018-03-06 17:08:31 -08001661 xfs_reflink_cancel_cow_range(ip, 0, NULLFILEOFF, true);
1662
Dave Chinner54d7b5c2016-02-09 16:54:58 +11001663 if (VFS_I(ip)->i_nlink != 0) {
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001664 /*
1665 * force is true because we are evicting an inode from the
1666 * cache. Post-eof blocks must be freed, lest we end up with
1667 * broken free space accounting.
Brian Foster3b4683c2017-04-11 10:50:05 -07001668 *
1669 * Note: don't bother with iolock here since lockdep complains
1670 * about acquiring it in reclaim context. We have the only
1671 * reference to the inode at this point anyways.
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001672 */
Brian Foster3b4683c2017-04-11 10:50:05 -07001673 if (xfs_can_free_eofblocks(ip, true))
Brian Fostera36b9262017-01-27 23:22:55 -08001674 xfs_free_eofblocks(ip);
Brian Foster74564fb2013-09-20 11:06:12 -04001675
1676 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001677 }
1678
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001679 if (S_ISREG(VFS_I(ip)->i_mode) &&
Christoph Hellwig13d2c102021-03-29 11:11:40 -07001680 (ip->i_disk_size != 0 || XFS_ISIZE(ip) != 0 ||
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001681 ip->i_df.if_nextents > 0 || ip->i_delayed_blks > 0))
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001682 truncate = 1;
1683
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -07001684 error = xfs_qm_dqattach(ip);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001685 if (error)
Brian Foster74564fb2013-09-20 11:06:12 -04001686 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001687
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001688 if (S_ISLNK(VFS_I(ip)->i_mode))
Brian Foster36b21dd2013-09-20 11:06:09 -04001689 error = xfs_inactive_symlink(ip);
Brian Fosterf7be2d72013-09-20 11:06:10 -04001690 else if (truncate)
1691 error = xfs_inactive_truncate(ip);
1692 if (error)
Brian Foster74564fb2013-09-20 11:06:12 -04001693 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001694
1695 /*
1696 * If there are attributes associated with the file then blow them away
1697 * now. The code calls a routine that recursively deconstructs the
Dave Chinner6dfe5a02015-05-29 07:40:08 +10001698 * attribute fork. If also blows away the in-core attribute fork.
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001699 */
Dave Chinner6dfe5a02015-05-29 07:40:08 +10001700 if (XFS_IFORK_Q(ip)) {
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001701 error = xfs_attr_inactive(ip);
1702 if (error)
Brian Foster74564fb2013-09-20 11:06:12 -04001703 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001704 }
1705
Dave Chinner6dfe5a02015-05-29 07:40:08 +10001706 ASSERT(!ip->i_afp);
Christoph Hellwig7821ea32021-03-29 11:11:44 -07001707 ASSERT(ip->i_forkoff == 0);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001708
1709 /*
1710 * Free the inode.
1711 */
Brian Foster88877d22013-09-20 11:06:11 -04001712 error = xfs_inactive_ifree(ip);
1713 if (error)
Brian Foster74564fb2013-09-20 11:06:12 -04001714 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001715
1716 /*
1717 * Release the dquots held by inode, if any.
1718 */
1719 xfs_qm_dqdetach(ip);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001720}
1721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722/*
Darrick J. Wong9b247172019-02-07 10:37:16 -08001723 * In-Core Unlinked List Lookups
1724 * =============================
1725 *
1726 * Every inode is supposed to be reachable from some other piece of metadata
1727 * with the exception of the root directory. Inodes with a connection to a
1728 * file descriptor but not linked from anywhere in the on-disk directory tree
1729 * are collectively known as unlinked inodes, though the filesystem itself
1730 * maintains links to these inodes so that on-disk metadata are consistent.
1731 *
1732 * XFS implements a per-AG on-disk hash table of unlinked inodes. The AGI
1733 * header contains a number of buckets that point to an inode, and each inode
1734 * record has a pointer to the next inode in the hash chain. This
1735 * singly-linked list causes scaling problems in the iunlink remove function
1736 * because we must walk that list to find the inode that points to the inode
1737 * being removed from the unlinked hash bucket list.
1738 *
1739 * What if we modelled the unlinked list as a collection of records capturing
1740 * "X.next_unlinked = Y" relations? If we indexed those records on Y, we'd
1741 * have a fast way to look up unlinked list predecessors, which avoids the
1742 * slow list walk. That's exactly what we do here (in-core) with a per-AG
1743 * rhashtable.
1744 *
1745 * Because this is a backref cache, we ignore operational failures since the
1746 * iunlink code can fall back to the slow bucket walk. The only errors that
1747 * should bubble out are for obviously incorrect situations.
1748 *
1749 * All users of the backref cache MUST hold the AGI buffer lock to serialize
1750 * access or have otherwise provided for concurrency control.
1751 */
1752
1753/* Capture a "X.next_unlinked = Y" relationship. */
1754struct xfs_iunlink {
1755 struct rhash_head iu_rhash_head;
1756 xfs_agino_t iu_agino; /* X */
1757 xfs_agino_t iu_next_unlinked; /* Y */
1758};
1759
1760/* Unlinked list predecessor lookup hashtable construction */
1761static int
1762xfs_iunlink_obj_cmpfn(
1763 struct rhashtable_compare_arg *arg,
1764 const void *obj)
1765{
1766 const xfs_agino_t *key = arg->key;
1767 const struct xfs_iunlink *iu = obj;
1768
1769 if (iu->iu_next_unlinked != *key)
1770 return 1;
1771 return 0;
1772}
1773
1774static const struct rhashtable_params xfs_iunlink_hash_params = {
1775 .min_size = XFS_AGI_UNLINKED_BUCKETS,
1776 .key_len = sizeof(xfs_agino_t),
1777 .key_offset = offsetof(struct xfs_iunlink,
1778 iu_next_unlinked),
1779 .head_offset = offsetof(struct xfs_iunlink, iu_rhash_head),
1780 .automatic_shrinking = true,
1781 .obj_cmpfn = xfs_iunlink_obj_cmpfn,
1782};
1783
1784/*
1785 * Return X, where X.next_unlinked == @agino. Returns NULLAGINO if no such
1786 * relation is found.
1787 */
1788static xfs_agino_t
1789xfs_iunlink_lookup_backref(
1790 struct xfs_perag *pag,
1791 xfs_agino_t agino)
1792{
1793 struct xfs_iunlink *iu;
1794
1795 iu = rhashtable_lookup_fast(&pag->pagi_unlinked_hash, &agino,
1796 xfs_iunlink_hash_params);
1797 return iu ? iu->iu_agino : NULLAGINO;
1798}
1799
1800/*
1801 * Take ownership of an iunlink cache entry and insert it into the hash table.
1802 * If successful, the entry will be owned by the cache; if not, it is freed.
1803 * Either way, the caller does not own @iu after this call.
1804 */
1805static int
1806xfs_iunlink_insert_backref(
1807 struct xfs_perag *pag,
1808 struct xfs_iunlink *iu)
1809{
1810 int error;
1811
1812 error = rhashtable_insert_fast(&pag->pagi_unlinked_hash,
1813 &iu->iu_rhash_head, xfs_iunlink_hash_params);
1814 /*
1815 * Fail loudly if there already was an entry because that's a sign of
1816 * corruption of in-memory data. Also fail loudly if we see an error
1817 * code we didn't anticipate from the rhashtable code. Currently we
1818 * only anticipate ENOMEM.
1819 */
1820 if (error) {
1821 WARN(error != -ENOMEM, "iunlink cache insert error %d", error);
1822 kmem_free(iu);
1823 }
1824 /*
1825 * Absorb any runtime errors that aren't a result of corruption because
1826 * this is a cache and we can always fall back to bucket list scanning.
1827 */
1828 if (error != 0 && error != -EEXIST)
1829 error = 0;
1830 return error;
1831}
1832
1833/* Remember that @prev_agino.next_unlinked = @this_agino. */
1834static int
1835xfs_iunlink_add_backref(
1836 struct xfs_perag *pag,
1837 xfs_agino_t prev_agino,
1838 xfs_agino_t this_agino)
1839{
1840 struct xfs_iunlink *iu;
1841
1842 if (XFS_TEST_ERROR(false, pag->pag_mount, XFS_ERRTAG_IUNLINK_FALLBACK))
1843 return 0;
1844
Tetsuo Handa707e0dd2019-08-26 12:06:22 -07001845 iu = kmem_zalloc(sizeof(*iu), KM_NOFS);
Darrick J. Wong9b247172019-02-07 10:37:16 -08001846 iu->iu_agino = prev_agino;
1847 iu->iu_next_unlinked = this_agino;
1848
1849 return xfs_iunlink_insert_backref(pag, iu);
1850}
1851
1852/*
1853 * Replace X.next_unlinked = @agino with X.next_unlinked = @next_unlinked.
1854 * If @next_unlinked is NULLAGINO, we drop the backref and exit. If there
1855 * wasn't any such entry then we don't bother.
1856 */
1857static int
1858xfs_iunlink_change_backref(
1859 struct xfs_perag *pag,
1860 xfs_agino_t agino,
1861 xfs_agino_t next_unlinked)
1862{
1863 struct xfs_iunlink *iu;
1864 int error;
1865
1866 /* Look up the old entry; if there wasn't one then exit. */
1867 iu = rhashtable_lookup_fast(&pag->pagi_unlinked_hash, &agino,
1868 xfs_iunlink_hash_params);
1869 if (!iu)
1870 return 0;
1871
1872 /*
1873 * Remove the entry. This shouldn't ever return an error, but if we
1874 * couldn't remove the old entry we don't want to add it again to the
1875 * hash table, and if the entry disappeared on us then someone's
1876 * violated the locking rules and we need to fail loudly. Either way
1877 * we cannot remove the inode because internal state is or would have
1878 * been corrupt.
1879 */
1880 error = rhashtable_remove_fast(&pag->pagi_unlinked_hash,
1881 &iu->iu_rhash_head, xfs_iunlink_hash_params);
1882 if (error)
1883 return error;
1884
1885 /* If there is no new next entry just free our item and return. */
1886 if (next_unlinked == NULLAGINO) {
1887 kmem_free(iu);
1888 return 0;
1889 }
1890
1891 /* Update the entry and re-add it to the hash table. */
1892 iu->iu_next_unlinked = next_unlinked;
1893 return xfs_iunlink_insert_backref(pag, iu);
1894}
1895
1896/* Set up the in-core predecessor structures. */
1897int
1898xfs_iunlink_init(
1899 struct xfs_perag *pag)
1900{
1901 return rhashtable_init(&pag->pagi_unlinked_hash,
1902 &xfs_iunlink_hash_params);
1903}
1904
1905/* Free the in-core predecessor structures. */
1906static void
1907xfs_iunlink_free_item(
1908 void *ptr,
1909 void *arg)
1910{
1911 struct xfs_iunlink *iu = ptr;
1912 bool *freed_anything = arg;
1913
1914 *freed_anything = true;
1915 kmem_free(iu);
1916}
1917
1918void
1919xfs_iunlink_destroy(
1920 struct xfs_perag *pag)
1921{
1922 bool freed_anything = false;
1923
1924 rhashtable_free_and_destroy(&pag->pagi_unlinked_hash,
1925 xfs_iunlink_free_item, &freed_anything);
1926
1927 ASSERT(freed_anything == false || XFS_FORCED_SHUTDOWN(pag->pag_mount));
1928}
1929
1930/*
Darrick J. Wong9a4a5112019-02-07 10:37:14 -08001931 * Point the AGI unlinked bucket at an inode and log the results. The caller
1932 * is responsible for validating the old value.
1933 */
1934STATIC int
1935xfs_iunlink_update_bucket(
1936 struct xfs_trans *tp,
1937 xfs_agnumber_t agno,
1938 struct xfs_buf *agibp,
1939 unsigned int bucket_index,
1940 xfs_agino_t new_agino)
1941{
Christoph Hellwig370c7822020-03-10 08:57:29 -07001942 struct xfs_agi *agi = agibp->b_addr;
Darrick J. Wong9a4a5112019-02-07 10:37:14 -08001943 xfs_agino_t old_value;
1944 int offset;
1945
1946 ASSERT(xfs_verify_agino_or_null(tp->t_mountp, agno, new_agino));
1947
1948 old_value = be32_to_cpu(agi->agi_unlinked[bucket_index]);
1949 trace_xfs_iunlink_update_bucket(tp->t_mountp, agno, bucket_index,
1950 old_value, new_agino);
1951
1952 /*
1953 * We should never find the head of the list already set to the value
1954 * passed in because either we're adding or removing ourselves from the
1955 * head of the list.
1956 */
Darrick J. Wonga5155b82019-11-02 09:40:53 -07001957 if (old_value == new_agino) {
Darrick J. Wong8d57c212020-03-11 10:37:54 -07001958 xfs_buf_mark_corrupt(agibp);
Darrick J. Wong9a4a5112019-02-07 10:37:14 -08001959 return -EFSCORRUPTED;
Darrick J. Wonga5155b82019-11-02 09:40:53 -07001960 }
Darrick J. Wong9a4a5112019-02-07 10:37:14 -08001961
1962 agi->agi_unlinked[bucket_index] = cpu_to_be32(new_agino);
1963 offset = offsetof(struct xfs_agi, agi_unlinked) +
1964 (sizeof(xfs_agino_t) * bucket_index);
1965 xfs_trans_log_buf(tp, agibp, offset, offset + sizeof(xfs_agino_t) - 1);
1966 return 0;
1967}
1968
Darrick J. Wongf2fc16a2019-02-07 10:37:15 -08001969/* Set an on-disk inode's next_unlinked pointer. */
1970STATIC void
1971xfs_iunlink_update_dinode(
1972 struct xfs_trans *tp,
1973 xfs_agnumber_t agno,
1974 xfs_agino_t agino,
1975 struct xfs_buf *ibp,
1976 struct xfs_dinode *dip,
1977 struct xfs_imap *imap,
1978 xfs_agino_t next_agino)
1979{
1980 struct xfs_mount *mp = tp->t_mountp;
1981 int offset;
1982
1983 ASSERT(xfs_verify_agino_or_null(mp, agno, next_agino));
1984
1985 trace_xfs_iunlink_update_dinode(mp, agno, agino,
1986 be32_to_cpu(dip->di_next_unlinked), next_agino);
1987
1988 dip->di_next_unlinked = cpu_to_be32(next_agino);
1989 offset = imap->im_boffset +
1990 offsetof(struct xfs_dinode, di_next_unlinked);
1991
1992 /* need to recalc the inode CRC if appropriate */
1993 xfs_dinode_calc_crc(mp, dip);
1994 xfs_trans_inode_buf(tp, ibp);
1995 xfs_trans_log_buf(tp, ibp, offset, offset + sizeof(xfs_agino_t) - 1);
Darrick J. Wongf2fc16a2019-02-07 10:37:15 -08001996}
1997
1998/* Set an in-core inode's unlinked pointer and return the old value. */
1999STATIC int
2000xfs_iunlink_update_inode(
2001 struct xfs_trans *tp,
2002 struct xfs_inode *ip,
2003 xfs_agnumber_t agno,
2004 xfs_agino_t next_agino,
2005 xfs_agino_t *old_next_agino)
2006{
2007 struct xfs_mount *mp = tp->t_mountp;
2008 struct xfs_dinode *dip;
2009 struct xfs_buf *ibp;
2010 xfs_agino_t old_value;
2011 int error;
2012
2013 ASSERT(xfs_verify_agino_or_null(mp, agno, next_agino));
2014
Christoph Hellwigaf9dcdd2021-03-29 11:11:37 -07002015 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &ibp);
Darrick J. Wongf2fc16a2019-02-07 10:37:15 -08002016 if (error)
2017 return error;
Christoph Hellwigaf9dcdd2021-03-29 11:11:37 -07002018 dip = xfs_buf_offset(ibp, ip->i_imap.im_boffset);
Darrick J. Wongf2fc16a2019-02-07 10:37:15 -08002019
2020 /* Make sure the old pointer isn't garbage. */
2021 old_value = be32_to_cpu(dip->di_next_unlinked);
2022 if (!xfs_verify_agino_or_null(mp, agno, old_value)) {
Darrick J. Wonga5155b82019-11-02 09:40:53 -07002023 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, dip,
2024 sizeof(*dip), __this_address);
Darrick J. Wongf2fc16a2019-02-07 10:37:15 -08002025 error = -EFSCORRUPTED;
2026 goto out;
2027 }
2028
2029 /*
2030 * Since we're updating a linked list, we should never find that the
2031 * current pointer is the same as the new value, unless we're
2032 * terminating the list.
2033 */
2034 *old_next_agino = old_value;
2035 if (old_value == next_agino) {
Darrick J. Wonga5155b82019-11-02 09:40:53 -07002036 if (next_agino != NULLAGINO) {
2037 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__,
2038 dip, sizeof(*dip), __this_address);
Darrick J. Wongf2fc16a2019-02-07 10:37:15 -08002039 error = -EFSCORRUPTED;
Darrick J. Wonga5155b82019-11-02 09:40:53 -07002040 }
Darrick J. Wongf2fc16a2019-02-07 10:37:15 -08002041 goto out;
2042 }
2043
2044 /* Ok, update the new pointer. */
2045 xfs_iunlink_update_dinode(tp, agno, XFS_INO_TO_AGINO(mp, ip->i_ino),
2046 ibp, dip, &ip->i_imap, next_agino);
2047 return 0;
2048out:
2049 xfs_trans_brelse(tp, ibp);
2050 return error;
2051}
2052
Darrick J. Wong9a4a5112019-02-07 10:37:14 -08002053/*
Darrick J. Wongc4a6bf72019-02-13 11:15:17 -08002054 * This is called when the inode's link count has gone to 0 or we are creating
2055 * a tmpfile via O_TMPFILE. The inode @ip must have nlink == 0.
Dave Chinner54d7b5c2016-02-09 16:54:58 +11002056 *
2057 * We place the on-disk inode on a list in the AGI. It will be pulled from this
2058 * list when the inode is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 */
Dave Chinner54d7b5c2016-02-09 16:54:58 +11002060STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061xfs_iunlink(
Darrick J. Wong5837f622019-02-07 10:37:13 -08002062 struct xfs_trans *tp,
2063 struct xfs_inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
Darrick J. Wong5837f622019-02-07 10:37:13 -08002065 struct xfs_mount *mp = tp->t_mountp;
2066 struct xfs_agi *agi;
Darrick J. Wong5837f622019-02-07 10:37:13 -08002067 struct xfs_buf *agibp;
Darrick J. Wong86bfd372019-02-07 10:37:14 -08002068 xfs_agino_t next_agino;
Darrick J. Wong5837f622019-02-07 10:37:13 -08002069 xfs_agnumber_t agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
2070 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
2071 short bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
Darrick J. Wong5837f622019-02-07 10:37:13 -08002072 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Darrick J. Wongc4a6bf72019-02-13 11:15:17 -08002074 ASSERT(VFS_I(ip)->i_nlink == 0);
Dave Chinnerc19b3b052016-02-09 16:54:58 +11002075 ASSERT(VFS_I(ip)->i_mode != 0);
Darrick J. Wong4664c662019-02-07 10:37:16 -08002076 trace_xfs_iunlink(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Darrick J. Wong5837f622019-02-07 10:37:13 -08002078 /* Get the agi buffer first. It ensures lock ordering on the list. */
2079 error = xfs_read_agi(mp, tp, agno, &agibp);
Vlad Apostolov859d7182007-10-11 17:44:18 +10002080 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 return error;
Christoph Hellwig370c7822020-03-10 08:57:29 -07002082 agi = agibp->b_addr;
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 /*
Darrick J. Wong86bfd372019-02-07 10:37:14 -08002085 * Get the index into the agi hash table for the list this inode will
2086 * go on. Make sure the pointer isn't garbage and that this inode
2087 * isn't already on the list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 */
Darrick J. Wong86bfd372019-02-07 10:37:14 -08002089 next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
2090 if (next_agino == agino ||
Darrick J. Wonga5155b82019-11-02 09:40:53 -07002091 !xfs_verify_agino_or_null(mp, agno, next_agino)) {
Darrick J. Wong8d57c212020-03-11 10:37:54 -07002092 xfs_buf_mark_corrupt(agibp);
Darrick J. Wong86bfd372019-02-07 10:37:14 -08002093 return -EFSCORRUPTED;
Darrick J. Wonga5155b82019-11-02 09:40:53 -07002094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
Darrick J. Wong86bfd372019-02-07 10:37:14 -08002096 if (next_agino != NULLAGINO) {
Darrick J. Wong9b247172019-02-07 10:37:16 -08002097 xfs_agino_t old_agino;
Darrick J. Wongf2fc16a2019-02-07 10:37:15 -08002098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 /*
Darrick J. Wongf2fc16a2019-02-07 10:37:15 -08002100 * There is already another inode in the bucket, so point this
2101 * inode to the current head of the list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 */
Darrick J. Wongf2fc16a2019-02-07 10:37:15 -08002103 error = xfs_iunlink_update_inode(tp, ip, agno, next_agino,
2104 &old_agino);
Vlad Apostolovc319b582007-11-23 16:27:51 +11002105 if (error)
2106 return error;
Darrick J. Wongf2fc16a2019-02-07 10:37:15 -08002107 ASSERT(old_agino == NULLAGINO);
Darrick J. Wong9b247172019-02-07 10:37:16 -08002108
2109 /*
2110 * agino has been unlinked, add a backref from the next inode
2111 * back to agino.
2112 */
Gao Xiang92a00542020-07-13 09:13:00 -07002113 error = xfs_iunlink_add_backref(agibp->b_pag, agino, next_agino);
Darrick J. Wong9b247172019-02-07 10:37:16 -08002114 if (error)
2115 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 }
2117
Darrick J. Wong9a4a5112019-02-07 10:37:14 -08002118 /* Point the head of the list to point to this inode. */
2119 return xfs_iunlink_update_bucket(tp, agno, agibp, bucket_index, agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120}
2121
Darrick J. Wong23ffa522019-02-07 10:37:15 -08002122/* Return the imap, dinode pointer, and buffer for an inode. */
2123STATIC int
2124xfs_iunlink_map_ino(
2125 struct xfs_trans *tp,
2126 xfs_agnumber_t agno,
2127 xfs_agino_t agino,
2128 struct xfs_imap *imap,
2129 struct xfs_dinode **dipp,
2130 struct xfs_buf **bpp)
2131{
2132 struct xfs_mount *mp = tp->t_mountp;
2133 int error;
2134
2135 imap->im_blkno = 0;
2136 error = xfs_imap(mp, tp, XFS_AGINO_TO_INO(mp, agno, agino), imap, 0);
2137 if (error) {
2138 xfs_warn(mp, "%s: xfs_imap returned error %d.",
2139 __func__, error);
2140 return error;
2141 }
2142
Christoph Hellwigaf9dcdd2021-03-29 11:11:37 -07002143 error = xfs_imap_to_bp(mp, tp, imap, bpp);
Darrick J. Wong23ffa522019-02-07 10:37:15 -08002144 if (error) {
2145 xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
2146 __func__, error);
2147 return error;
2148 }
2149
Christoph Hellwigaf9dcdd2021-03-29 11:11:37 -07002150 *dipp = xfs_buf_offset(*bpp, imap->im_boffset);
Darrick J. Wong23ffa522019-02-07 10:37:15 -08002151 return 0;
2152}
2153
2154/*
2155 * Walk the unlinked chain from @head_agino until we find the inode that
2156 * points to @target_agino. Return the inode number, map, dinode pointer,
2157 * and inode cluster buffer of that inode as @agino, @imap, @dipp, and @bpp.
2158 *
2159 * @tp, @pag, @head_agino, and @target_agino are input parameters.
2160 * @agino, @imap, @dipp, and @bpp are all output parameters.
2161 *
2162 * Do not call this function if @target_agino is the head of the list.
2163 */
2164STATIC int
2165xfs_iunlink_map_prev(
2166 struct xfs_trans *tp,
2167 xfs_agnumber_t agno,
2168 xfs_agino_t head_agino,
2169 xfs_agino_t target_agino,
2170 xfs_agino_t *agino,
2171 struct xfs_imap *imap,
2172 struct xfs_dinode **dipp,
Darrick J. Wong9b247172019-02-07 10:37:16 -08002173 struct xfs_buf **bpp,
2174 struct xfs_perag *pag)
Darrick J. Wong23ffa522019-02-07 10:37:15 -08002175{
2176 struct xfs_mount *mp = tp->t_mountp;
2177 xfs_agino_t next_agino;
2178 int error;
2179
2180 ASSERT(head_agino != target_agino);
2181 *bpp = NULL;
2182
Darrick J. Wong9b247172019-02-07 10:37:16 -08002183 /* See if our backref cache can find it faster. */
2184 *agino = xfs_iunlink_lookup_backref(pag, target_agino);
2185 if (*agino != NULLAGINO) {
2186 error = xfs_iunlink_map_ino(tp, agno, *agino, imap, dipp, bpp);
2187 if (error)
2188 return error;
2189
2190 if (be32_to_cpu((*dipp)->di_next_unlinked) == target_agino)
2191 return 0;
2192
2193 /*
2194 * If we get here the cache contents were corrupt, so drop the
2195 * buffer and fall back to walking the bucket list.
2196 */
2197 xfs_trans_brelse(tp, *bpp);
2198 *bpp = NULL;
2199 WARN_ON_ONCE(1);
2200 }
2201
2202 trace_xfs_iunlink_map_prev_fallback(mp, agno);
2203
2204 /* Otherwise, walk the entire bucket until we find it. */
Darrick J. Wong23ffa522019-02-07 10:37:15 -08002205 next_agino = head_agino;
2206 while (next_agino != target_agino) {
2207 xfs_agino_t unlinked_agino;
2208
2209 if (*bpp)
2210 xfs_trans_brelse(tp, *bpp);
2211
2212 *agino = next_agino;
2213 error = xfs_iunlink_map_ino(tp, agno, next_agino, imap, dipp,
2214 bpp);
2215 if (error)
2216 return error;
2217
2218 unlinked_agino = be32_to_cpu((*dipp)->di_next_unlinked);
2219 /*
2220 * Make sure this pointer is valid and isn't an obvious
2221 * infinite loop.
2222 */
2223 if (!xfs_verify_agino(mp, agno, unlinked_agino) ||
2224 next_agino == unlinked_agino) {
2225 XFS_CORRUPTION_ERROR(__func__,
2226 XFS_ERRLEVEL_LOW, mp,
2227 *dipp, sizeof(**dipp));
2228 error = -EFSCORRUPTED;
2229 return error;
2230 }
2231 next_agino = unlinked_agino;
2232 }
2233
2234 return 0;
2235}
2236
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237/*
2238 * Pull the on-disk inode from the AGI unlinked list.
2239 */
2240STATIC int
2241xfs_iunlink_remove(
Darrick J. Wong5837f622019-02-07 10:37:13 -08002242 struct xfs_trans *tp,
2243 struct xfs_inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244{
Darrick J. Wong5837f622019-02-07 10:37:13 -08002245 struct xfs_mount *mp = tp->t_mountp;
2246 struct xfs_agi *agi;
Darrick J. Wong5837f622019-02-07 10:37:13 -08002247 struct xfs_buf *agibp;
Darrick J. Wong5837f622019-02-07 10:37:13 -08002248 struct xfs_buf *last_ibp;
2249 struct xfs_dinode *last_dip = NULL;
Darrick J. Wong5837f622019-02-07 10:37:13 -08002250 xfs_agnumber_t agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
2251 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
2252 xfs_agino_t next_agino;
Darrick J. Wongb1d2a062019-02-07 10:37:15 -08002253 xfs_agino_t head_agino;
Darrick J. Wong5837f622019-02-07 10:37:13 -08002254 short bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
Darrick J. Wong5837f622019-02-07 10:37:13 -08002255 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Darrick J. Wong4664c662019-02-07 10:37:16 -08002257 trace_xfs_iunlink_remove(ip);
2258
Darrick J. Wong5837f622019-02-07 10:37:13 -08002259 /* Get the agi buffer first. It ensures lock ordering on the list. */
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002260 error = xfs_read_agi(mp, tp, agno, &agibp);
2261 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 return error;
Christoph Hellwig370c7822020-03-10 08:57:29 -07002263 agi = agibp->b_addr;
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 /*
Darrick J. Wong86bfd372019-02-07 10:37:14 -08002266 * Get the index into the agi hash table for the list this inode will
2267 * go on. Make sure the head pointer isn't garbage.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 */
Darrick J. Wongb1d2a062019-02-07 10:37:15 -08002269 head_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
2270 if (!xfs_verify_agino(mp, agno, head_agino)) {
Darrick J. Wongd2e73662018-06-04 11:27:51 -07002271 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
2272 agi, sizeof(*agi));
2273 return -EFSCORRUPTED;
2274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Darrick J. Wongb1d2a062019-02-07 10:37:15 -08002276 /*
2277 * Set our inode's next_unlinked pointer to NULL and then return
2278 * the old pointer value so that we can update whatever was previous
2279 * to us in the list to point to whatever was next in the list.
2280 */
2281 error = xfs_iunlink_update_inode(tp, ip, agno, NULLAGINO, &next_agino);
2282 if (error)
2283 return error;
Darrick J. Wong9a4a5112019-02-07 10:37:14 -08002284
Darrick J. Wong9b247172019-02-07 10:37:16 -08002285 /*
2286 * If there was a backref pointing from the next inode back to this
2287 * one, remove it because we've removed this inode from the list.
2288 *
2289 * Later, if this inode was in the middle of the list we'll update
2290 * this inode's backref to point from the next inode.
2291 */
2292 if (next_agino != NULLAGINO) {
Gao Xiang92a00542020-07-13 09:13:00 -07002293 error = xfs_iunlink_change_backref(agibp->b_pag, next_agino,
Darrick J. Wong9b247172019-02-07 10:37:16 -08002294 NULLAGINO);
2295 if (error)
Gao Xiang92a00542020-07-13 09:13:00 -07002296 return error;
Darrick J. Wong9b247172019-02-07 10:37:16 -08002297 }
2298
Gao Xiang92a00542020-07-13 09:13:00 -07002299 if (head_agino != agino) {
Darrick J. Wongf2fc16a2019-02-07 10:37:15 -08002300 struct xfs_imap imap;
2301 xfs_agino_t prev_agino;
2302
Darrick J. Wong23ffa522019-02-07 10:37:15 -08002303 /* We need to search the list for the inode being freed. */
Darrick J. Wongb1d2a062019-02-07 10:37:15 -08002304 error = xfs_iunlink_map_prev(tp, agno, head_agino, agino,
Darrick J. Wong9b247172019-02-07 10:37:16 -08002305 &prev_agino, &imap, &last_dip, &last_ibp,
Gao Xiang92a00542020-07-13 09:13:00 -07002306 agibp->b_pag);
Darrick J. Wong23ffa522019-02-07 10:37:15 -08002307 if (error)
Gao Xiang92a00542020-07-13 09:13:00 -07002308 return error;
Christoph Hellwig475ee412012-07-03 12:21:22 -04002309
Darrick J. Wongf2fc16a2019-02-07 10:37:15 -08002310 /* Point the previous inode on the list to the next inode. */
2311 xfs_iunlink_update_dinode(tp, agno, prev_agino, last_ibp,
2312 last_dip, &imap, next_agino);
Darrick J. Wong9b247172019-02-07 10:37:16 -08002313
2314 /*
2315 * Now we deal with the backref for this inode. If this inode
2316 * pointed at a real inode, change the backref that pointed to
2317 * us to point to our old next. If this inode was the end of
2318 * the list, delete the backref that pointed to us. Note that
2319 * change_backref takes care of deleting the backref if
2320 * next_agino is NULLAGINO.
2321 */
Gao Xiang92a00542020-07-13 09:13:00 -07002322 return xfs_iunlink_change_backref(agibp->b_pag, agino,
2323 next_agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 }
Darrick J. Wong9b247172019-02-07 10:37:16 -08002325
Gao Xiang92a00542020-07-13 09:13:00 -07002326 /* Point the head of the list to the next unlinked inode. */
2327 return xfs_iunlink_update_bucket(tp, agno, agibp, bucket_index,
2328 next_agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329}
2330
Dave Chinner5b3eed72010-08-24 11:42:41 +10002331/*
Dave Chinner71e3e352020-06-29 14:49:18 -07002332 * Look up the inode number specified and if it is not already marked XFS_ISTALE
2333 * mark it stale. We should only find clean inodes in this lookup that aren't
2334 * already stale.
Dave Chinner58061652020-03-24 20:10:30 -07002335 */
Dave Chinner71e3e352020-06-29 14:49:18 -07002336static void
2337xfs_ifree_mark_inode_stale(
2338 struct xfs_buf *bp,
Dave Chinner58061652020-03-24 20:10:30 -07002339 struct xfs_inode *free_ip,
Brian Fosterd9fdd0a2020-04-02 08:18:57 -07002340 xfs_ino_t inum)
Dave Chinner58061652020-03-24 20:10:30 -07002341{
Dave Chinner71e3e352020-06-29 14:49:18 -07002342 struct xfs_mount *mp = bp->b_mount;
2343 struct xfs_perag *pag = bp->b_pag;
2344 struct xfs_inode_log_item *iip;
Dave Chinner58061652020-03-24 20:10:30 -07002345 struct xfs_inode *ip;
2346
2347retry:
2348 rcu_read_lock();
2349 ip = radix_tree_lookup(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, inum));
2350
2351 /* Inode not in memory, nothing to do */
Dave Chinner71e3e352020-06-29 14:49:18 -07002352 if (!ip) {
2353 rcu_read_unlock();
2354 return;
2355 }
Dave Chinner58061652020-03-24 20:10:30 -07002356
2357 /*
2358 * because this is an RCU protected lookup, we could find a recently
2359 * freed or even reallocated inode during the lookup. We need to check
2360 * under the i_flags_lock for a valid inode here. Skip it if it is not
2361 * valid, the wrong inode or stale.
2362 */
2363 spin_lock(&ip->i_flags_lock);
Dave Chinner718ecc52020-08-17 16:41:01 -07002364 if (ip->i_ino != inum || __xfs_iflags_test(ip, XFS_ISTALE))
2365 goto out_iflags_unlock;
Dave Chinner58061652020-03-24 20:10:30 -07002366
2367 /*
2368 * Don't try to lock/unlock the current inode, but we _cannot_ skip the
2369 * other inodes that we did not find in the list attached to the buffer
2370 * and are not already marked stale. If we can't lock it, back off and
2371 * retry.
2372 */
2373 if (ip != free_ip) {
2374 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
Dave Chinner71e3e352020-06-29 14:49:18 -07002375 spin_unlock(&ip->i_flags_lock);
Dave Chinner58061652020-03-24 20:10:30 -07002376 rcu_read_unlock();
2377 delay(1);
2378 goto retry;
2379 }
Dave Chinner58061652020-03-24 20:10:30 -07002380 }
Dave Chinner71e3e352020-06-29 14:49:18 -07002381 ip->i_flags |= XFS_ISTALE;
Dave Chinner58061652020-03-24 20:10:30 -07002382
Dave Chinner58061652020-03-24 20:10:30 -07002383 /*
Dave Chinner718ecc52020-08-17 16:41:01 -07002384 * If the inode is flushing, it is already attached to the buffer. All
Dave Chinner71e3e352020-06-29 14:49:18 -07002385 * we needed to do here is mark the inode stale so buffer IO completion
2386 * will remove it from the AIL.
Dave Chinner58061652020-03-24 20:10:30 -07002387 */
Dave Chinner71e3e352020-06-29 14:49:18 -07002388 iip = ip->i_itemp;
Dave Chinner718ecc52020-08-17 16:41:01 -07002389 if (__xfs_iflags_test(ip, XFS_IFLUSHING)) {
Dave Chinner71e3e352020-06-29 14:49:18 -07002390 ASSERT(!list_empty(&iip->ili_item.li_bio_list));
2391 ASSERT(iip->ili_last_fields);
2392 goto out_iunlock;
Dave Chinner58061652020-03-24 20:10:30 -07002393 }
Dave Chinner58061652020-03-24 20:10:30 -07002394
Dave Chinner71e3e352020-06-29 14:49:18 -07002395 /*
Dave Chinner48d55e22020-06-29 14:49:18 -07002396 * Inodes not attached to the buffer can be released immediately.
2397 * Everything else has to go through xfs_iflush_abort() on journal
2398 * commit as the flock synchronises removal of the inode from the
2399 * cluster buffer against inode reclaim.
Dave Chinner71e3e352020-06-29 14:49:18 -07002400 */
Dave Chinner718ecc52020-08-17 16:41:01 -07002401 if (!iip || list_empty(&iip->ili_item.li_bio_list))
Dave Chinner71e3e352020-06-29 14:49:18 -07002402 goto out_iunlock;
Dave Chinner718ecc52020-08-17 16:41:01 -07002403
2404 __xfs_iflags_set(ip, XFS_IFLUSHING);
2405 spin_unlock(&ip->i_flags_lock);
2406 rcu_read_unlock();
Dave Chinner71e3e352020-06-29 14:49:18 -07002407
2408 /* we have a dirty inode in memory that has not yet been flushed. */
Dave Chinner71e3e352020-06-29 14:49:18 -07002409 spin_lock(&iip->ili_lock);
2410 iip->ili_last_fields = iip->ili_fields;
2411 iip->ili_fields = 0;
2412 iip->ili_fsync_fields = 0;
2413 spin_unlock(&iip->ili_lock);
Dave Chinner71e3e352020-06-29 14:49:18 -07002414 ASSERT(iip->ili_last_fields);
2415
Dave Chinner718ecc52020-08-17 16:41:01 -07002416 if (ip != free_ip)
2417 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2418 return;
2419
Dave Chinner71e3e352020-06-29 14:49:18 -07002420out_iunlock:
2421 if (ip != free_ip)
2422 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner718ecc52020-08-17 16:41:01 -07002423out_iflags_unlock:
2424 spin_unlock(&ip->i_flags_lock);
2425 rcu_read_unlock();
Dave Chinner58061652020-03-24 20:10:30 -07002426}
2427
2428/*
Zhi Yong Wu0b8182d2013-08-12 03:14:59 +00002429 * A big issue when freeing the inode cluster is that we _cannot_ skip any
Dave Chinner5b3eed72010-08-24 11:42:41 +10002430 * inodes that are in memory - they all must be marked stale and attached to
2431 * the cluster buffer.
2432 */
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00002433STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434xfs_ifree_cluster(
Dave Chinner71e3e352020-06-29 14:49:18 -07002435 struct xfs_inode *free_ip,
2436 struct xfs_trans *tp,
Brian Foster09b56602015-05-29 09:26:03 +10002437 struct xfs_icluster *xic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438{
Dave Chinner71e3e352020-06-29 14:49:18 -07002439 struct xfs_mount *mp = free_ip->i_mount;
2440 struct xfs_ino_geometry *igeo = M_IGEO(mp);
2441 struct xfs_buf *bp;
2442 xfs_daddr_t blkno;
2443 xfs_ino_t inum = xic->first_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 int nbufs;
Dave Chinner5b257b42010-06-03 16:22:29 +10002445 int i, j;
Brian Foster3cdaa182015-06-04 13:03:34 +10002446 int ioffset;
Darrick J. Wongce924642020-01-23 17:01:18 -08002447 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Darrick J. Wongef325952019-06-05 11:19:34 -07002449 nbufs = igeo->ialloc_blks / igeo->blocks_per_cluster;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Darrick J. Wongef325952019-06-05 11:19:34 -07002451 for (j = 0; j < nbufs; j++, inum += igeo->inodes_per_cluster) {
Brian Foster09b56602015-05-29 09:26:03 +10002452 /*
2453 * The allocation bitmap tells us which inodes of the chunk were
2454 * physically allocated. Skip the cluster if an inode falls into
2455 * a sparse region.
2456 */
Brian Foster3cdaa182015-06-04 13:03:34 +10002457 ioffset = inum - xic->first_ino;
2458 if ((xic->alloc & XFS_INOBT_MASK(ioffset)) == 0) {
Darrick J. Wongef325952019-06-05 11:19:34 -07002459 ASSERT(ioffset % igeo->inodes_per_cluster == 0);
Brian Foster09b56602015-05-29 09:26:03 +10002460 continue;
2461 }
2462
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
2464 XFS_INO_TO_AGBNO(mp, inum));
2465
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 /*
Dave Chinner5b257b42010-06-03 16:22:29 +10002467 * We obtain and lock the backing buffer first in the process
Dave Chinner718ecc52020-08-17 16:41:01 -07002468 * here to ensure dirty inodes attached to the buffer remain in
2469 * the flushing state while we mark them stale.
2470 *
Dave Chinner5b257b42010-06-03 16:22:29 +10002471 * If we scan the in-memory inodes first, then buffer IO can
2472 * complete before we get a lock on it, and hence we may fail
2473 * to mark all the active inodes on the buffer stale.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 */
Darrick J. Wongce924642020-01-23 17:01:18 -08002475 error = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
2476 mp->m_bsize * igeo->blocks_per_cluster,
2477 XBF_UNMAPPED, &bp);
Dave Chinner71e3e352020-06-29 14:49:18 -07002478 if (error)
Darrick J. Wongce924642020-01-23 17:01:18 -08002479 return error;
Dave Chinnerb0f539d2012-11-14 17:53:49 +11002480
2481 /*
2482 * This buffer may not have been correctly initialised as we
2483 * didn't read it from disk. That's not important because we are
2484 * only using to mark the buffer as stale in the log, and to
2485 * attach stale cached inodes on it. That means it will never be
2486 * dispatched for IO. If it is, we want to know about it, and we
2487 * want it to fail. We can acheive this by adding a write
2488 * verifier to the buffer.
2489 */
Colin Ian King8c4ce792018-12-12 08:46:20 -08002490 bp->b_ops = &xfs_inode_buf_ops;
Dave Chinnerb0f539d2012-11-14 17:53:49 +11002491
Dave Chinner5b257b42010-06-03 16:22:29 +10002492 /*
Dave Chinner71e3e352020-06-29 14:49:18 -07002493 * Now we need to set all the cached clean inodes as XFS_ISTALE,
2494 * too. This requires lookups, and will skip inodes that we've
2495 * already marked XFS_ISTALE.
Dave Chinner5b257b42010-06-03 16:22:29 +10002496 */
Dave Chinner71e3e352020-06-29 14:49:18 -07002497 for (i = 0; i < igeo->inodes_per_cluster; i++)
2498 xfs_ifree_mark_inode_stale(bp, free_ip, inum + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
Dave Chinner5b3eed72010-08-24 11:42:41 +10002500 xfs_trans_stale_inode_buf(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 xfs_trans_binval(tp, bp);
2502 }
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00002503 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504}
2505
2506/*
2507 * This is called to return an inode to the inode free list.
2508 * The inode should already be truncated to 0 length and have
2509 * no pages associated with it. This routine also assumes that
2510 * the inode is already a part of the transaction.
2511 *
2512 * The on-disk copy of the inode will have been added to the list
2513 * of unlinked inodes in the AGI. We need to remove the inode from
2514 * that list atomically with respect to freeing it here.
2515 */
2516int
2517xfs_ifree(
Brian Foster0e0417f2018-07-11 22:26:07 -07002518 struct xfs_trans *tp,
2519 struct xfs_inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520{
2521 int error;
Brian Foster09b56602015-05-29 09:26:03 +10002522 struct xfs_icluster xic = { 0 };
Dave Chinner1319ebe2020-06-29 14:48:46 -07002523 struct xfs_inode_log_item *iip = ip->i_itemp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002525 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Dave Chinner54d7b5c2016-02-09 16:54:58 +11002526 ASSERT(VFS_I(ip)->i_nlink == 0);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07002527 ASSERT(ip->i_df.if_nextents == 0);
Christoph Hellwig13d2c102021-03-29 11:11:40 -07002528 ASSERT(ip->i_disk_size == 0 || !S_ISREG(VFS_I(ip)->i_mode));
Christoph Hellwig6e73a542021-03-29 11:11:40 -07002529 ASSERT(ip->i_nblocks == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
2531 /*
2532 * Pull the on-disk inode from the AGI unlinked list.
2533 */
2534 error = xfs_iunlink_remove(tp, ip);
Dave Chinner1baaed82013-06-27 16:04:50 +10002535 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
Brian Foster0e0417f2018-07-11 22:26:07 -07002538 error = xfs_difree(tp, ip->i_ino, &xic);
Dave Chinner1baaed82013-06-27 16:04:50 +10002539 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 return error;
Dave Chinner1baaed82013-06-27 16:04:50 +10002541
Christoph Hellwigb2c20042020-05-18 10:27:21 -07002542 /*
2543 * Free any local-format data sitting around before we reset the
2544 * data fork to extents format. Note that the attr fork data has
2545 * already been freed by xfs_attr_inactive.
2546 */
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07002547 if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
Christoph Hellwigb2c20042020-05-18 10:27:21 -07002548 kmem_free(ip->i_df.if_u1.if_data);
2549 ip->i_df.if_u1.if_data = NULL;
2550 ip->i_df.if_bytes = 0;
2551 }
Darrick J. Wong98c4f782017-11-22 12:21:07 -08002552
Dave Chinnerc19b3b052016-02-09 16:54:58 +11002553 VFS_I(ip)->i_mode = 0; /* mark incore inode as free */
Christoph Hellwigdb073492021-03-29 11:11:44 -07002554 ip->i_diflags = 0;
Christoph Hellwig3e09ab82021-03-29 11:11:45 -07002555 ip->i_diflags2 = ip->i_mount->m_ino_geo.new_diflags2;
Christoph Hellwig7821ea32021-03-29 11:11:44 -07002556 ip->i_forkoff = 0; /* mark the attr fork not in use */
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07002557 ip->i_df.if_format = XFS_DINODE_FMT_EXTENTS;
Christoph Hellwig9b3beb02021-03-29 11:11:38 -07002558 if (xfs_iflags_test(ip, XFS_IPRESERVE_DM_FIELDS))
2559 xfs_iflags_clear(ip, XFS_IPRESERVE_DM_FIELDS);
Eric Sandeendc1baa72018-03-28 17:48:08 -07002560
2561 /* Don't attempt to replay owner changes for a deleted inode */
Dave Chinner1319ebe2020-06-29 14:48:46 -07002562 spin_lock(&iip->ili_lock);
2563 iip->ili_fields &= ~(XFS_ILOG_AOWNER | XFS_ILOG_DOWNER);
2564 spin_unlock(&iip->ili_lock);
Eric Sandeendc1baa72018-03-28 17:48:08 -07002565
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 /*
2567 * Bump the generation count so no one will be confused
2568 * by reincarnations of this inode.
2569 */
Dave Chinner9e9a2672016-02-09 16:54:58 +11002570 VFS_I(ip)->i_generation++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2572
Brian Foster09b56602015-05-29 09:26:03 +10002573 if (xic.deleted)
2574 error = xfs_ifree_cluster(ip, tp, &xic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00002576 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577}
2578
2579/*
Christoph Hellwig60ec6782010-02-17 19:43:56 +00002580 * This is called to unpin an inode. The caller must have the inode locked
2581 * in at least shared mode so that the buffer cannot be subsequently pinned
2582 * once someone is waiting for it to be unpinned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 */
Christoph Hellwig60ec6782010-02-17 19:43:56 +00002584static void
Christoph Hellwigf392e632011-12-18 20:00:10 +00002585xfs_iunpin(
Christoph Hellwig60ec6782010-02-17 19:43:56 +00002586 struct xfs_inode *ip)
David Chinnera3f74ff2008-03-06 13:43:42 +11002587{
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002588 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
David Chinnera3f74ff2008-03-06 13:43:42 +11002589
Dave Chinner4aaf15d2010-03-08 11:24:07 +11002590 trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
2591
David Chinnera3f74ff2008-03-06 13:43:42 +11002592 /* Give the log a push to start the unpinning I/O */
Christoph Hellwig656de4f2018-03-13 23:15:28 -07002593 xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0, NULL);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002594
David Chinnera3f74ff2008-03-06 13:43:42 +11002595}
2596
Christoph Hellwigf392e632011-12-18 20:00:10 +00002597static void
2598__xfs_iunpin_wait(
2599 struct xfs_inode *ip)
2600{
2601 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
2602 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
2603
2604 xfs_iunpin(ip);
2605
2606 do {
Ingo Molnar21417132017-03-05 11:25:39 +01002607 prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
Christoph Hellwigf392e632011-12-18 20:00:10 +00002608 if (xfs_ipincount(ip))
2609 io_schedule();
2610 } while (xfs_ipincount(ip));
Ingo Molnar21417132017-03-05 11:25:39 +01002611 finish_wait(wq, &wait.wq_entry);
Christoph Hellwigf392e632011-12-18 20:00:10 +00002612}
2613
Dave Chinner777df5a2010-02-06 12:37:26 +11002614void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615xfs_iunpin_wait(
Christoph Hellwig60ec6782010-02-17 19:43:56 +00002616 struct xfs_inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617{
Christoph Hellwigf392e632011-12-18 20:00:10 +00002618 if (xfs_ipincount(ip))
2619 __xfs_iunpin_wait(ip);
David Chinnera3f74ff2008-03-06 13:43:42 +11002620}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
Dave Chinner27320362013-10-29 22:11:44 +11002622/*
2623 * Removing an inode from the namespace involves removing the directory entry
2624 * and dropping the link count on the inode. Removing the directory entry can
2625 * result in locking an AGF (directory blocks were freed) and removing a link
2626 * count can result in placing the inode on an unlinked list which results in
2627 * locking an AGI.
2628 *
2629 * The big problem here is that we have an ordering constraint on AGF and AGI
2630 * locking - inode allocation locks the AGI, then can allocate a new extent for
2631 * new inodes, locking the AGF after the AGI. Similarly, freeing the inode
2632 * removes the inode from the unlinked list, requiring that we lock the AGI
2633 * first, and then freeing the inode can result in an inode chunk being freed
2634 * and hence freeing disk space requiring that we lock an AGF.
2635 *
2636 * Hence the ordering that is imposed by other parts of the code is AGI before
2637 * AGF. This means we cannot remove the directory entry before we drop the inode
2638 * reference count and put it on the unlinked list as this results in a lock
2639 * order of AGF then AGI, and this can deadlock against inode allocation and
2640 * freeing. Therefore we must drop the link counts before we remove the
2641 * directory entry.
2642 *
2643 * This is still safe from a transactional point of view - it is not until we
Darrick J. Wong310a75a2016-08-03 11:18:10 +10002644 * get to xfs_defer_finish() that we have the possibility of multiple
Dave Chinner27320362013-10-29 22:11:44 +11002645 * transactions in this operation. Hence as long as we remove the directory
2646 * entry and drop the link count in the first transaction of the remove
2647 * operation, there are no transactional constraints on the ordering here.
2648 */
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002649int
2650xfs_remove(
2651 xfs_inode_t *dp,
2652 struct xfs_name *name,
2653 xfs_inode_t *ip)
2654{
2655 xfs_mount_t *mp = dp->i_mount;
2656 xfs_trans_t *tp = NULL;
Dave Chinnerc19b3b052016-02-09 16:54:58 +11002657 int is_dir = S_ISDIR(VFS_I(ip)->i_mode);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002658 int error = 0;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002659 uint resblks;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002660
2661 trace_xfs_remove(dp, name);
2662
2663 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10002664 return -EIO;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002665
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -07002666 error = xfs_qm_dqattach(dp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002667 if (error)
2668 goto std_return;
2669
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -07002670 error = xfs_qm_dqattach(ip);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002671 if (error)
2672 goto std_return;
2673
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002674 /*
2675 * We try to get the real space reservation first,
2676 * allowing for directory btree deletion(s) implying
2677 * possible bmap insert(s). If we can't get the space
2678 * reservation then we use 0 instead, and avoid the bmap
2679 * btree insert(s) in the directory code by, if the bmap
2680 * insert tries to happen, instead trimming the LAST
2681 * block from the directory.
2682 */
2683 resblks = XFS_REMOVE_SPACE_RES(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10002684 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_remove, resblks, 0, 0, &tp);
Dave Chinner24513372014-06-25 14:58:08 +10002685 if (error == -ENOSPC) {
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002686 resblks = 0;
Christoph Hellwig253f4912016-04-06 09:19:55 +10002687 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_remove, 0, 0, 0,
2688 &tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002689 }
2690 if (error) {
Dave Chinner24513372014-06-25 14:58:08 +10002691 ASSERT(error != -ENOSPC);
Christoph Hellwig253f4912016-04-06 09:19:55 +10002692 goto std_return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002693 }
2694
Darrick J. Wong7c2d2382018-01-26 15:27:33 -08002695 xfs_lock_two_inodes(dp, XFS_ILOCK_EXCL, ip, XFS_ILOCK_EXCL);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002696
Christoph Hellwig65523212016-11-30 14:33:25 +11002697 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002698 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2699
2700 /*
2701 * If we're removing a directory perform some additional validation.
2702 */
2703 if (is_dir) {
Dave Chinner54d7b5c2016-02-09 16:54:58 +11002704 ASSERT(VFS_I(ip)->i_nlink >= 2);
2705 if (VFS_I(ip)->i_nlink != 2) {
Dave Chinner24513372014-06-25 14:58:08 +10002706 error = -ENOTEMPTY;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002707 goto out_trans_cancel;
2708 }
2709 if (!xfs_dir_isempty(ip)) {
Dave Chinner24513372014-06-25 14:58:08 +10002710 error = -ENOTEMPTY;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002711 goto out_trans_cancel;
2712 }
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002713
Dave Chinner27320362013-10-29 22:11:44 +11002714 /* Drop the link from ip's "..". */
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002715 error = xfs_droplink(tp, dp);
2716 if (error)
Dave Chinner27320362013-10-29 22:11:44 +11002717 goto out_trans_cancel;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002718
Dave Chinner27320362013-10-29 22:11:44 +11002719 /* Drop the "." link from ip to self. */
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002720 error = xfs_droplink(tp, ip);
2721 if (error)
Dave Chinner27320362013-10-29 22:11:44 +11002722 goto out_trans_cancel;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002723 } else {
2724 /*
2725 * When removing a non-directory we need to log the parent
2726 * inode here. For a directory this is done implicitly
2727 * by the xfs_droplink call for the ".." entry.
2728 */
2729 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2730 }
Dave Chinner27320362013-10-29 22:11:44 +11002731 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002732
Dave Chinner27320362013-10-29 22:11:44 +11002733 /* Drop the link from dp to ip. */
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002734 error = xfs_droplink(tp, ip);
2735 if (error)
Dave Chinner27320362013-10-29 22:11:44 +11002736 goto out_trans_cancel;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002737
Brian Foster381eee62018-07-11 22:26:21 -07002738 error = xfs_dir_removename(tp, dp, name, ip->i_ino, resblks);
Dave Chinner27320362013-10-29 22:11:44 +11002739 if (error) {
Dave Chinner24513372014-06-25 14:58:08 +10002740 ASSERT(error != -ENOENT);
Brian Fosterc8eac492018-07-24 13:43:13 -07002741 goto out_trans_cancel;
Dave Chinner27320362013-10-29 22:11:44 +11002742 }
2743
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002744 /*
2745 * If this is a synchronous mount, make sure that the
2746 * remove transaction goes to disk before returning to
2747 * the user.
2748 */
2749 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
2750 xfs_trans_set_sync(tp);
2751
Christoph Hellwig70393312015-06-04 13:48:08 +10002752 error = xfs_trans_commit(tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002753 if (error)
2754 goto std_return;
2755
Christoph Hellwig2cd2ef62014-04-23 07:11:51 +10002756 if (is_dir && xfs_inode_is_filestream(ip))
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002757 xfs_filestream_deassociate(ip);
2758
2759 return 0;
2760
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002761 out_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10002762 xfs_trans_cancel(tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002763 std_return:
2764 return error;
2765}
2766
Dave Chinnerf6bba202013-08-12 20:49:46 +10002767/*
2768 * Enter all inodes for a rename transaction into a sorted array.
2769 */
Dave Chinner95afcf52015-03-25 14:03:32 +11002770#define __XFS_SORT_INODES 5
Dave Chinnerf6bba202013-08-12 20:49:46 +10002771STATIC void
2772xfs_sort_for_rename(
Dave Chinner95afcf52015-03-25 14:03:32 +11002773 struct xfs_inode *dp1, /* in: old (source) directory inode */
2774 struct xfs_inode *dp2, /* in: new (target) directory inode */
2775 struct xfs_inode *ip1, /* in: inode of old entry */
2776 struct xfs_inode *ip2, /* in: inode of new entry */
2777 struct xfs_inode *wip, /* in: whiteout inode */
2778 struct xfs_inode **i_tab,/* out: sorted array of inodes */
2779 int *num_inodes) /* in/out: inodes in array */
Dave Chinnerf6bba202013-08-12 20:49:46 +10002780{
Dave Chinnerf6bba202013-08-12 20:49:46 +10002781 int i, j;
2782
Dave Chinner95afcf52015-03-25 14:03:32 +11002783 ASSERT(*num_inodes == __XFS_SORT_INODES);
2784 memset(i_tab, 0, *num_inodes * sizeof(struct xfs_inode *));
2785
Dave Chinnerf6bba202013-08-12 20:49:46 +10002786 /*
2787 * i_tab contains a list of pointers to inodes. We initialize
2788 * the table here & we'll sort it. We will then use it to
2789 * order the acquisition of the inode locks.
2790 *
2791 * Note that the table may contain duplicates. e.g., dp1 == dp2.
2792 */
Dave Chinner95afcf52015-03-25 14:03:32 +11002793 i = 0;
2794 i_tab[i++] = dp1;
2795 i_tab[i++] = dp2;
2796 i_tab[i++] = ip1;
2797 if (ip2)
2798 i_tab[i++] = ip2;
2799 if (wip)
2800 i_tab[i++] = wip;
2801 *num_inodes = i;
Dave Chinnerf6bba202013-08-12 20:49:46 +10002802
2803 /*
2804 * Sort the elements via bubble sort. (Remember, there are at
Dave Chinner95afcf52015-03-25 14:03:32 +11002805 * most 5 elements to sort, so this is adequate.)
Dave Chinnerf6bba202013-08-12 20:49:46 +10002806 */
2807 for (i = 0; i < *num_inodes; i++) {
2808 for (j = 1; j < *num_inodes; j++) {
2809 if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) {
Dave Chinner95afcf52015-03-25 14:03:32 +11002810 struct xfs_inode *temp = i_tab[j];
Dave Chinnerf6bba202013-08-12 20:49:46 +10002811 i_tab[j] = i_tab[j-1];
2812 i_tab[j-1] = temp;
2813 }
2814 }
2815 }
2816}
2817
Dave Chinner310606b2015-03-25 14:06:07 +11002818static int
2819xfs_finish_rename(
Brian Fosterc9cfdb32018-07-11 22:26:08 -07002820 struct xfs_trans *tp)
Dave Chinner310606b2015-03-25 14:06:07 +11002821{
Dave Chinner310606b2015-03-25 14:06:07 +11002822 /*
2823 * If this is a synchronous mount, make sure that the rename transaction
2824 * goes to disk before returning to the user.
2825 */
2826 if (tp->t_mountp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
2827 xfs_trans_set_sync(tp);
2828
Christoph Hellwig70393312015-06-04 13:48:08 +10002829 return xfs_trans_commit(tp);
Dave Chinner310606b2015-03-25 14:06:07 +11002830}
2831
Dave Chinnerf6bba202013-08-12 20:49:46 +10002832/*
Carlos Maiolinod31a1822014-12-24 08:51:42 +11002833 * xfs_cross_rename()
2834 *
Bhaskar Chowdhury01452252021-03-23 16:59:30 -07002835 * responsible for handling RENAME_EXCHANGE flag in renameat2() syscall
Carlos Maiolinod31a1822014-12-24 08:51:42 +11002836 */
2837STATIC int
2838xfs_cross_rename(
2839 struct xfs_trans *tp,
2840 struct xfs_inode *dp1,
2841 struct xfs_name *name1,
2842 struct xfs_inode *ip1,
2843 struct xfs_inode *dp2,
2844 struct xfs_name *name2,
2845 struct xfs_inode *ip2,
Carlos Maiolinod31a1822014-12-24 08:51:42 +11002846 int spaceres)
2847{
2848 int error = 0;
2849 int ip1_flags = 0;
2850 int ip2_flags = 0;
2851 int dp2_flags = 0;
2852
2853 /* Swap inode number for dirent in first parent */
Brian Foster381eee62018-07-11 22:26:21 -07002854 error = xfs_dir_replace(tp, dp1, name1, ip2->i_ino, spaceres);
Carlos Maiolinod31a1822014-12-24 08:51:42 +11002855 if (error)
Dave Chinnereeacd322015-03-25 14:08:07 +11002856 goto out_trans_abort;
Carlos Maiolinod31a1822014-12-24 08:51:42 +11002857
2858 /* Swap inode number for dirent in second parent */
Brian Foster381eee62018-07-11 22:26:21 -07002859 error = xfs_dir_replace(tp, dp2, name2, ip1->i_ino, spaceres);
Carlos Maiolinod31a1822014-12-24 08:51:42 +11002860 if (error)
Dave Chinnereeacd322015-03-25 14:08:07 +11002861 goto out_trans_abort;
Carlos Maiolinod31a1822014-12-24 08:51:42 +11002862
2863 /*
2864 * If we're renaming one or more directories across different parents,
2865 * update the respective ".." entries (and link counts) to match the new
2866 * parents.
2867 */
2868 if (dp1 != dp2) {
2869 dp2_flags = XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
2870
Dave Chinnerc19b3b052016-02-09 16:54:58 +11002871 if (S_ISDIR(VFS_I(ip2)->i_mode)) {
Carlos Maiolinod31a1822014-12-24 08:51:42 +11002872 error = xfs_dir_replace(tp, ip2, &xfs_name_dotdot,
Brian Foster381eee62018-07-11 22:26:21 -07002873 dp1->i_ino, spaceres);
Carlos Maiolinod31a1822014-12-24 08:51:42 +11002874 if (error)
Dave Chinnereeacd322015-03-25 14:08:07 +11002875 goto out_trans_abort;
Carlos Maiolinod31a1822014-12-24 08:51:42 +11002876
2877 /* transfer ip2 ".." reference to dp1 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +11002878 if (!S_ISDIR(VFS_I(ip1)->i_mode)) {
Carlos Maiolinod31a1822014-12-24 08:51:42 +11002879 error = xfs_droplink(tp, dp2);
2880 if (error)
Dave Chinnereeacd322015-03-25 14:08:07 +11002881 goto out_trans_abort;
Eric Sandeen91083262019-05-01 20:26:30 -07002882 xfs_bumplink(tp, dp1);
Carlos Maiolinod31a1822014-12-24 08:51:42 +11002883 }
2884
2885 /*
2886 * Although ip1 isn't changed here, userspace needs
2887 * to be warned about the change, so that applications
2888 * relying on it (like backup ones), will properly
2889 * notify the change
2890 */
2891 ip1_flags |= XFS_ICHGTIME_CHG;
2892 ip2_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
2893 }
2894
Dave Chinnerc19b3b052016-02-09 16:54:58 +11002895 if (S_ISDIR(VFS_I(ip1)->i_mode)) {
Carlos Maiolinod31a1822014-12-24 08:51:42 +11002896 error = xfs_dir_replace(tp, ip1, &xfs_name_dotdot,
Brian Foster381eee62018-07-11 22:26:21 -07002897 dp2->i_ino, spaceres);
Carlos Maiolinod31a1822014-12-24 08:51:42 +11002898 if (error)
Dave Chinnereeacd322015-03-25 14:08:07 +11002899 goto out_trans_abort;
Carlos Maiolinod31a1822014-12-24 08:51:42 +11002900
2901 /* transfer ip1 ".." reference to dp2 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +11002902 if (!S_ISDIR(VFS_I(ip2)->i_mode)) {
Carlos Maiolinod31a1822014-12-24 08:51:42 +11002903 error = xfs_droplink(tp, dp1);
2904 if (error)
Dave Chinnereeacd322015-03-25 14:08:07 +11002905 goto out_trans_abort;
Eric Sandeen91083262019-05-01 20:26:30 -07002906 xfs_bumplink(tp, dp2);
Carlos Maiolinod31a1822014-12-24 08:51:42 +11002907 }
2908
2909 /*
2910 * Although ip2 isn't changed here, userspace needs
2911 * to be warned about the change, so that applications
2912 * relying on it (like backup ones), will properly
2913 * notify the change
2914 */
2915 ip1_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
2916 ip2_flags |= XFS_ICHGTIME_CHG;
2917 }
2918 }
2919
2920 if (ip1_flags) {
2921 xfs_trans_ichgtime(tp, ip1, ip1_flags);
2922 xfs_trans_log_inode(tp, ip1, XFS_ILOG_CORE);
2923 }
2924 if (ip2_flags) {
2925 xfs_trans_ichgtime(tp, ip2, ip2_flags);
2926 xfs_trans_log_inode(tp, ip2, XFS_ILOG_CORE);
2927 }
2928 if (dp2_flags) {
2929 xfs_trans_ichgtime(tp, dp2, dp2_flags);
2930 xfs_trans_log_inode(tp, dp2, XFS_ILOG_CORE);
2931 }
2932 xfs_trans_ichgtime(tp, dp1, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2933 xfs_trans_log_inode(tp, dp1, XFS_ILOG_CORE);
Brian Fosterc9cfdb32018-07-11 22:26:08 -07002934 return xfs_finish_rename(tp);
Dave Chinnereeacd322015-03-25 14:08:07 +11002935
2936out_trans_abort:
Christoph Hellwig4906e212015-06-04 13:47:56 +10002937 xfs_trans_cancel(tp);
Carlos Maiolinod31a1822014-12-24 08:51:42 +11002938 return error;
2939}
2940
2941/*
Dave Chinner7dcf5c32015-03-25 14:08:08 +11002942 * xfs_rename_alloc_whiteout()
2943 *
Randy Dunlapb63da6c2020-08-05 08:49:58 -07002944 * Return a referenced, unlinked, unlocked inode that can be used as a
Dave Chinner7dcf5c32015-03-25 14:08:08 +11002945 * whiteout in a rename transaction. We use a tmpfile inode here so that if we
2946 * crash between allocating the inode and linking it into the rename transaction
2947 * recovery will free the inode and we won't leak it.
2948 */
2949static int
2950xfs_rename_alloc_whiteout(
Christoph Hellwigf736d932021-01-21 14:19:58 +01002951 struct user_namespace *mnt_userns,
Dave Chinner7dcf5c32015-03-25 14:08:08 +11002952 struct xfs_inode *dp,
2953 struct xfs_inode **wip)
2954{
2955 struct xfs_inode *tmpfile;
2956 int error;
2957
Christoph Hellwigf736d932021-01-21 14:19:58 +01002958 error = xfs_create_tmpfile(mnt_userns, dp, S_IFCHR | WHITEOUT_MODE,
2959 &tmpfile);
Dave Chinner7dcf5c32015-03-25 14:08:08 +11002960 if (error)
2961 return error;
2962
Brian Foster22419ac2015-05-29 08:14:55 +10002963 /*
2964 * Prepare the tmpfile inode as if it were created through the VFS.
Darrick J. Wongc4a6bf72019-02-13 11:15:17 -08002965 * Complete the inode setup and flag it as linkable. nlink is already
2966 * zero, so we can skip the drop_nlink.
Brian Foster22419ac2015-05-29 08:14:55 +10002967 */
Christoph Hellwig2b3d1d42016-04-06 07:48:27 +10002968 xfs_setup_iops(tmpfile);
Dave Chinner7dcf5c32015-03-25 14:08:08 +11002969 xfs_finish_inode_setup(tmpfile);
2970 VFS_I(tmpfile)->i_state |= I_LINKABLE;
2971
2972 *wip = tmpfile;
2973 return 0;
2974}
2975
2976/*
Dave Chinnerf6bba202013-08-12 20:49:46 +10002977 * xfs_rename
2978 */
2979int
2980xfs_rename(
Christoph Hellwigf736d932021-01-21 14:19:58 +01002981 struct user_namespace *mnt_userns,
Dave Chinner7dcf5c32015-03-25 14:08:08 +11002982 struct xfs_inode *src_dp,
2983 struct xfs_name *src_name,
2984 struct xfs_inode *src_ip,
2985 struct xfs_inode *target_dp,
2986 struct xfs_name *target_name,
2987 struct xfs_inode *target_ip,
2988 unsigned int flags)
Dave Chinnerf6bba202013-08-12 20:49:46 +10002989{
Dave Chinner7dcf5c32015-03-25 14:08:08 +11002990 struct xfs_mount *mp = src_dp->i_mount;
2991 struct xfs_trans *tp;
Dave Chinner7dcf5c32015-03-25 14:08:08 +11002992 struct xfs_inode *wip = NULL; /* whiteout inode */
2993 struct xfs_inode *inodes[__XFS_SORT_INODES];
Darrick J. Wong6da1b4b2021-01-22 16:48:32 -08002994 int i;
Dave Chinner7dcf5c32015-03-25 14:08:08 +11002995 int num_inodes = __XFS_SORT_INODES;
Dave Chinner2b936812015-03-25 15:12:30 +11002996 bool new_parent = (src_dp != target_dp);
Dave Chinnerc19b3b052016-02-09 16:54:58 +11002997 bool src_is_directory = S_ISDIR(VFS_I(src_ip)->i_mode);
Dave Chinner7dcf5c32015-03-25 14:08:08 +11002998 int spaceres;
2999 int error;
Dave Chinnerf6bba202013-08-12 20:49:46 +10003000
3001 trace_xfs_rename(src_dp, target_dp, src_name, target_name);
3002
Dave Chinnereeacd322015-03-25 14:08:07 +11003003 if ((flags & RENAME_EXCHANGE) && !target_ip)
3004 return -EINVAL;
Dave Chinnerf6bba202013-08-12 20:49:46 +10003005
Dave Chinner7dcf5c32015-03-25 14:08:08 +11003006 /*
3007 * If we are doing a whiteout operation, allocate the whiteout inode
3008 * we will be placing at the target and ensure the type is set
3009 * appropriately.
3010 */
3011 if (flags & RENAME_WHITEOUT) {
3012 ASSERT(!(flags & (RENAME_NOREPLACE | RENAME_EXCHANGE)));
Christoph Hellwigf736d932021-01-21 14:19:58 +01003013 error = xfs_rename_alloc_whiteout(mnt_userns, target_dp, &wip);
Dave Chinner7dcf5c32015-03-25 14:08:08 +11003014 if (error)
3015 return error;
Dave Chinnerf6bba202013-08-12 20:49:46 +10003016
Dave Chinner7dcf5c32015-03-25 14:08:08 +11003017 /* setup target dirent info as whiteout */
3018 src_name->type = XFS_DIR3_FT_CHRDEV;
3019 }
3020
3021 xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip, wip,
Dave Chinnerf6bba202013-08-12 20:49:46 +10003022 inodes, &num_inodes);
3023
Dave Chinnerf6bba202013-08-12 20:49:46 +10003024 spaceres = XFS_RENAME_SPACE_RES(mp, target_name->len);
Christoph Hellwig253f4912016-04-06 09:19:55 +10003025 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_rename, spaceres, 0, 0, &tp);
Dave Chinner24513372014-06-25 14:58:08 +10003026 if (error == -ENOSPC) {
Dave Chinnerf6bba202013-08-12 20:49:46 +10003027 spaceres = 0;
Christoph Hellwig253f4912016-04-06 09:19:55 +10003028 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_rename, 0, 0, 0,
3029 &tp);
Dave Chinnerf6bba202013-08-12 20:49:46 +10003030 }
Dave Chinner445883e2015-03-25 14:05:43 +11003031 if (error)
Christoph Hellwig253f4912016-04-06 09:19:55 +10003032 goto out_release_wip;
Dave Chinnerf6bba202013-08-12 20:49:46 +10003033
3034 /*
3035 * Attach the dquots to the inodes
3036 */
3037 error = xfs_qm_vop_rename_dqattach(inodes);
Dave Chinner445883e2015-03-25 14:05:43 +11003038 if (error)
3039 goto out_trans_cancel;
Dave Chinnerf6bba202013-08-12 20:49:46 +10003040
3041 /*
3042 * Lock all the participating inodes. Depending upon whether
3043 * the target_name exists in the target directory, and
3044 * whether the target directory is the same as the source
3045 * directory, we can lock from 2 to 4 inodes.
3046 */
3047 xfs_lock_inodes(inodes, num_inodes, XFS_ILOCK_EXCL);
3048
3049 /*
3050 * Join all the inodes to the transaction. From this point on,
3051 * we can rely on either trans_commit or trans_cancel to unlock
3052 * them.
3053 */
Christoph Hellwig65523212016-11-30 14:33:25 +11003054 xfs_trans_ijoin(tp, src_dp, XFS_ILOCK_EXCL);
Dave Chinnerf6bba202013-08-12 20:49:46 +10003055 if (new_parent)
Christoph Hellwig65523212016-11-30 14:33:25 +11003056 xfs_trans_ijoin(tp, target_dp, XFS_ILOCK_EXCL);
Dave Chinnerf6bba202013-08-12 20:49:46 +10003057 xfs_trans_ijoin(tp, src_ip, XFS_ILOCK_EXCL);
3058 if (target_ip)
3059 xfs_trans_ijoin(tp, target_ip, XFS_ILOCK_EXCL);
Dave Chinner7dcf5c32015-03-25 14:08:08 +11003060 if (wip)
3061 xfs_trans_ijoin(tp, wip, XFS_ILOCK_EXCL);
Dave Chinnerf6bba202013-08-12 20:49:46 +10003062
3063 /*
3064 * If we are using project inheritance, we only allow renames
3065 * into our tree when the project IDs are the same; else the
3066 * tree quota mechanism would be circumvented.
3067 */
Christoph Hellwigdb073492021-03-29 11:11:44 -07003068 if (unlikely((target_dp->i_diflags & XFS_DIFLAG_PROJINHERIT) &&
Christoph Hellwigceaf6032021-03-29 11:11:39 -07003069 target_dp->i_projid != src_ip->i_projid)) {
Dave Chinner24513372014-06-25 14:58:08 +10003070 error = -EXDEV;
Dave Chinner445883e2015-03-25 14:05:43 +11003071 goto out_trans_cancel;
Dave Chinnerf6bba202013-08-12 20:49:46 +10003072 }
3073
Dave Chinnereeacd322015-03-25 14:08:07 +11003074 /* RENAME_EXCHANGE is unique from here on. */
3075 if (flags & RENAME_EXCHANGE)
3076 return xfs_cross_rename(tp, src_dp, src_name, src_ip,
3077 target_dp, target_name, target_ip,
Brian Fosterf16dea52018-07-11 22:26:20 -07003078 spaceres);
Carlos Maiolinod31a1822014-12-24 08:51:42 +11003079
3080 /*
kaixuxiabc56ad82019-09-03 21:06:50 -07003081 * Check for expected errors before we dirty the transaction
3082 * so we can return an error without a transaction abort.
Chandan Babu R02092a22021-01-22 16:48:13 -08003083 *
3084 * Extent count overflow check:
3085 *
3086 * From the perspective of src_dp, a rename operation is essentially a
3087 * directory entry remove operation. Hence the only place where we check
3088 * for extent count overflow for src_dp is in
3089 * xfs_bmap_del_extent_real(). xfs_bmap_del_extent_real() returns
3090 * -ENOSPC when it detects a possible extent count overflow and in
3091 * response, the higher layers of directory handling code do the
3092 * following:
3093 * 1. Data/Free blocks: XFS lets these blocks linger until a
3094 * future remove operation removes them.
3095 * 2. Dabtree blocks: XFS swaps the blocks with the last block in the
3096 * Leaf space and unmaps the last block.
3097 *
3098 * For target_dp, there are two cases depending on whether the
3099 * destination directory entry exists or not.
3100 *
3101 * When destination directory entry does not exist (i.e. target_ip ==
3102 * NULL), extent count overflow check is performed only when transaction
3103 * has a non-zero sized space reservation associated with it. With a
3104 * zero-sized space reservation, XFS allows a rename operation to
3105 * continue only when the directory has sufficient free space in its
3106 * data/leaf/free space blocks to hold the new entry.
3107 *
3108 * When destination directory entry exists (i.e. target_ip != NULL), all
3109 * we need to do is change the inode number associated with the already
3110 * existing entry. Hence there is no need to perform an extent count
3111 * overflow check.
Dave Chinnerf6bba202013-08-12 20:49:46 +10003112 */
3113 if (target_ip == NULL) {
3114 /*
3115 * If there's no space reservation, check the entry will
3116 * fit before actually inserting it.
3117 */
Eric Sandeen94f3cad2014-09-09 11:57:52 +10003118 if (!spaceres) {
3119 error = xfs_dir_canenter(tp, target_dp, target_name);
3120 if (error)
Dave Chinner445883e2015-03-25 14:05:43 +11003121 goto out_trans_cancel;
Chandan Babu R02092a22021-01-22 16:48:13 -08003122 } else {
3123 error = xfs_iext_count_may_overflow(target_dp,
3124 XFS_DATA_FORK,
3125 XFS_IEXT_DIR_MANIP_CNT(mp));
3126 if (error)
3127 goto out_trans_cancel;
Eric Sandeen94f3cad2014-09-09 11:57:52 +10003128 }
kaixuxiabc56ad82019-09-03 21:06:50 -07003129 } else {
3130 /*
3131 * If target exists and it's a directory, check that whether
3132 * it can be destroyed.
3133 */
3134 if (S_ISDIR(VFS_I(target_ip)->i_mode) &&
3135 (!xfs_dir_isempty(target_ip) ||
3136 (VFS_I(target_ip)->i_nlink > 2))) {
3137 error = -EEXIST;
3138 goto out_trans_cancel;
3139 }
3140 }
3141
3142 /*
Darrick J. Wong6da1b4b2021-01-22 16:48:32 -08003143 * Lock the AGI buffers we need to handle bumping the nlink of the
3144 * whiteout inode off the unlinked list and to handle dropping the
3145 * nlink of the target inode. Per locking order rules, do this in
3146 * increasing AG order and before directory block allocation tries to
3147 * grab AGFs because we grab AGIs before AGFs.
3148 *
3149 * The (vfs) caller must ensure that if src is a directory then
3150 * target_ip is either null or an empty directory.
3151 */
3152 for (i = 0; i < num_inodes && inodes[i] != NULL; i++) {
3153 if (inodes[i] == wip ||
3154 (inodes[i] == target_ip &&
3155 (VFS_I(target_ip)->i_nlink == 1 || src_is_directory))) {
3156 struct xfs_buf *bp;
3157 xfs_agnumber_t agno;
3158
3159 agno = XFS_INO_TO_AGNO(mp, inodes[i]->i_ino);
3160 error = xfs_read_agi(mp, tp, agno, &bp);
3161 if (error)
3162 goto out_trans_cancel;
3163 }
3164 }
3165
3166 /*
kaixuxiabc56ad82019-09-03 21:06:50 -07003167 * Directory entry creation below may acquire the AGF. Remove
3168 * the whiteout from the unlinked list first to preserve correct
3169 * AGI/AGF locking order. This dirties the transaction so failures
3170 * after this point will abort and log recovery will clean up the
3171 * mess.
3172 *
3173 * For whiteouts, we need to bump the link count on the whiteout
3174 * inode. After this point, we have a real link, clear the tmpfile
3175 * state flag from the inode so it doesn't accidentally get misused
3176 * in future.
3177 */
3178 if (wip) {
3179 ASSERT(VFS_I(wip)->i_nlink == 0);
3180 error = xfs_iunlink_remove(tp, wip);
3181 if (error)
3182 goto out_trans_cancel;
3183
3184 xfs_bumplink(tp, wip);
kaixuxiabc56ad82019-09-03 21:06:50 -07003185 VFS_I(wip)->i_state &= ~I_LINKABLE;
3186 }
3187
3188 /*
3189 * Set up the target.
3190 */
3191 if (target_ip == NULL) {
Dave Chinnerf6bba202013-08-12 20:49:46 +10003192 /*
3193 * If target does not exist and the rename crosses
3194 * directories, adjust the target directory link count
3195 * to account for the ".." reference from the new entry.
3196 */
3197 error = xfs_dir_createname(tp, target_dp, target_name,
Brian Foster381eee62018-07-11 22:26:21 -07003198 src_ip->i_ino, spaceres);
Dave Chinnerf6bba202013-08-12 20:49:46 +10003199 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -07003200 goto out_trans_cancel;
Dave Chinnerf6bba202013-08-12 20:49:46 +10003201
3202 xfs_trans_ichgtime(tp, target_dp,
3203 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3204
3205 if (new_parent && src_is_directory) {
Eric Sandeen91083262019-05-01 20:26:30 -07003206 xfs_bumplink(tp, target_dp);
Dave Chinnerf6bba202013-08-12 20:49:46 +10003207 }
3208 } else { /* target_ip != NULL */
3209 /*
Dave Chinnerf6bba202013-08-12 20:49:46 +10003210 * Link the source inode under the target name.
3211 * If the source inode is a directory and we are moving
3212 * it across directories, its ".." entry will be
3213 * inconsistent until we replace that down below.
3214 *
3215 * In case there is already an entry with the same
3216 * name at the destination directory, remove it first.
3217 */
3218 error = xfs_dir_replace(tp, target_dp, target_name,
Brian Foster381eee62018-07-11 22:26:21 -07003219 src_ip->i_ino, spaceres);
Dave Chinnerf6bba202013-08-12 20:49:46 +10003220 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -07003221 goto out_trans_cancel;
Dave Chinnerf6bba202013-08-12 20:49:46 +10003222
3223 xfs_trans_ichgtime(tp, target_dp,
3224 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3225
3226 /*
3227 * Decrement the link count on the target since the target
3228 * dir no longer points to it.
3229 */
3230 error = xfs_droplink(tp, target_ip);
3231 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -07003232 goto out_trans_cancel;
Dave Chinnerf6bba202013-08-12 20:49:46 +10003233
3234 if (src_is_directory) {
3235 /*
3236 * Drop the link from the old "." entry.
3237 */
3238 error = xfs_droplink(tp, target_ip);
3239 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -07003240 goto out_trans_cancel;
Dave Chinnerf6bba202013-08-12 20:49:46 +10003241 }
3242 } /* target_ip != NULL */
3243
3244 /*
3245 * Remove the source.
3246 */
3247 if (new_parent && src_is_directory) {
3248 /*
3249 * Rewrite the ".." entry to point to the new
3250 * directory.
3251 */
3252 error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot,
Brian Foster381eee62018-07-11 22:26:21 -07003253 target_dp->i_ino, spaceres);
Dave Chinner24513372014-06-25 14:58:08 +10003254 ASSERT(error != -EEXIST);
Dave Chinnerf6bba202013-08-12 20:49:46 +10003255 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -07003256 goto out_trans_cancel;
Dave Chinnerf6bba202013-08-12 20:49:46 +10003257 }
3258
3259 /*
3260 * We always want to hit the ctime on the source inode.
3261 *
3262 * This isn't strictly required by the standards since the source
3263 * inode isn't really being changed, but old unix file systems did
3264 * it and some incremental backup programs won't work without it.
3265 */
3266 xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG);
3267 xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE);
3268
3269 /*
3270 * Adjust the link count on src_dp. This is necessary when
3271 * renaming a directory, either within one parent when
3272 * the target existed, or across two parent directories.
3273 */
3274 if (src_is_directory && (new_parent || target_ip != NULL)) {
3275
3276 /*
3277 * Decrement link count on src_directory since the
3278 * entry that's moved no longer points to it.
3279 */
3280 error = xfs_droplink(tp, src_dp);
3281 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -07003282 goto out_trans_cancel;
Dave Chinnerf6bba202013-08-12 20:49:46 +10003283 }
3284
Dave Chinner7dcf5c32015-03-25 14:08:08 +11003285 /*
3286 * For whiteouts, we only need to update the source dirent with the
3287 * inode number of the whiteout inode rather than removing it
3288 * altogether.
3289 */
3290 if (wip) {
3291 error = xfs_dir_replace(tp, src_dp, src_name, wip->i_ino,
Brian Foster381eee62018-07-11 22:26:21 -07003292 spaceres);
Chandan Babu R02092a22021-01-22 16:48:13 -08003293 } else {
3294 /*
3295 * NOTE: We don't need to check for extent count overflow here
3296 * because the dir remove name code will leave the dir block in
3297 * place if the extent count would overflow.
3298 */
Dave Chinner7dcf5c32015-03-25 14:08:08 +11003299 error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino,
Brian Foster381eee62018-07-11 22:26:21 -07003300 spaceres);
Chandan Babu R02092a22021-01-22 16:48:13 -08003301 }
3302
Dave Chinnerf6bba202013-08-12 20:49:46 +10003303 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -07003304 goto out_trans_cancel;
Dave Chinnerf6bba202013-08-12 20:49:46 +10003305
3306 xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3307 xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE);
3308 if (new_parent)
3309 xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE);
3310
Brian Fosterc9cfdb32018-07-11 22:26:08 -07003311 error = xfs_finish_rename(tp);
Dave Chinner7dcf5c32015-03-25 14:08:08 +11003312 if (wip)
Darrick J. Wong44a87362018-07-25 12:52:32 -07003313 xfs_irele(wip);
Dave Chinner7dcf5c32015-03-25 14:08:08 +11003314 return error;
Dave Chinnerf6bba202013-08-12 20:49:46 +10003315
Dave Chinner445883e2015-03-25 14:05:43 +11003316out_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10003317 xfs_trans_cancel(tp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10003318out_release_wip:
Dave Chinner7dcf5c32015-03-25 14:08:08 +11003319 if (wip)
Darrick J. Wong44a87362018-07-25 12:52:32 -07003320 xfs_irele(wip);
Dave Chinnerf6bba202013-08-12 20:49:46 +10003321 return error;
3322}
3323
Dave Chinnere6187b32020-06-29 14:49:19 -07003324static int
3325xfs_iflush(
Christoph Hellwig93848a92013-04-03 16:11:17 +11003326 struct xfs_inode *ip,
3327 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328{
Christoph Hellwig93848a92013-04-03 16:11:17 +11003329 struct xfs_inode_log_item *iip = ip->i_itemp;
3330 struct xfs_dinode *dip;
3331 struct xfs_mount *mp = ip->i_mount;
Brian Fosterf2019292020-05-06 13:25:20 -07003332 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10003334 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Dave Chinner718ecc52020-08-17 16:41:01 -07003335 ASSERT(xfs_iflags_test(ip, XFS_IFLUSHING));
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07003336 ASSERT(ip->i_df.if_format != XFS_DINODE_FMT_BTREE ||
Christoph Hellwigdaf83962020-05-18 10:27:22 -07003337 ip->i_df.if_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
Dave Chinner90c60e12020-06-29 14:49:19 -07003338 ASSERT(iip->ili_item.li_buf == bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10003340 dip = xfs_buf_offset(bp, ip->i_imap.im_boffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
Brian Fosterf2019292020-05-06 13:25:20 -07003342 /*
3343 * We don't flush the inode if any of the following checks fail, but we
3344 * do still update the log item and attach to the backing buffer as if
3345 * the flush happened. This is a formality to facilitate predictable
3346 * error handling as the caller will shutdown and fail the buffer.
3347 */
3348 error = -EFSCORRUPTED;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02003349 if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07003350 mp, XFS_ERRTAG_IFLUSH_1)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11003351 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
Darrick J. Wongc9690042018-01-09 12:02:55 -08003352 "%s: Bad inode %Lu magic number 0x%x, ptr "PTR_FMT,
Dave Chinner6a19d932011-03-07 10:02:35 +11003353 __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
Brian Fosterf2019292020-05-06 13:25:20 -07003354 goto flush_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 }
Dave Chinnerc19b3b052016-02-09 16:54:58 +11003356 if (S_ISREG(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 if (XFS_TEST_ERROR(
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07003358 ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS &&
3359 ip->i_df.if_format != XFS_DINODE_FMT_BTREE,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07003360 mp, XFS_ERRTAG_IFLUSH_3)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11003361 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
Darrick J. Wongc9690042018-01-09 12:02:55 -08003362 "%s: Bad regular inode %Lu, ptr "PTR_FMT,
Dave Chinner6a19d932011-03-07 10:02:35 +11003363 __func__, ip->i_ino, ip);
Brian Fosterf2019292020-05-06 13:25:20 -07003364 goto flush_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 }
Dave Chinnerc19b3b052016-02-09 16:54:58 +11003366 } else if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 if (XFS_TEST_ERROR(
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07003368 ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS &&
3369 ip->i_df.if_format != XFS_DINODE_FMT_BTREE &&
3370 ip->i_df.if_format != XFS_DINODE_FMT_LOCAL,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07003371 mp, XFS_ERRTAG_IFLUSH_4)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11003372 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
Darrick J. Wongc9690042018-01-09 12:02:55 -08003373 "%s: Bad directory inode %Lu, ptr "PTR_FMT,
Dave Chinner6a19d932011-03-07 10:02:35 +11003374 __func__, ip->i_ino, ip);
Brian Fosterf2019292020-05-06 13:25:20 -07003375 goto flush_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 }
3377 }
Christoph Hellwigdaf83962020-05-18 10:27:22 -07003378 if (XFS_TEST_ERROR(ip->i_df.if_nextents + xfs_ifork_nextents(ip->i_afp) >
Christoph Hellwig6e73a542021-03-29 11:11:40 -07003379 ip->i_nblocks, mp, XFS_ERRTAG_IFLUSH_5)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11003380 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3381 "%s: detected corrupt incore inode %Lu, "
Darrick J. Wongc9690042018-01-09 12:02:55 -08003382 "total extents = %d, nblocks = %Ld, ptr "PTR_FMT,
Dave Chinner6a19d932011-03-07 10:02:35 +11003383 __func__, ip->i_ino,
Christoph Hellwigdaf83962020-05-18 10:27:22 -07003384 ip->i_df.if_nextents + xfs_ifork_nextents(ip->i_afp),
Christoph Hellwig6e73a542021-03-29 11:11:40 -07003385 ip->i_nblocks, ip);
Brian Fosterf2019292020-05-06 13:25:20 -07003386 goto flush_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 }
Christoph Hellwig7821ea32021-03-29 11:11:44 -07003388 if (XFS_TEST_ERROR(ip->i_forkoff > mp->m_sb.sb_inodesize,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07003389 mp, XFS_ERRTAG_IFLUSH_6)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11003390 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
Darrick J. Wongc9690042018-01-09 12:02:55 -08003391 "%s: bad inode %Lu, forkoff 0x%x, ptr "PTR_FMT,
Christoph Hellwig7821ea32021-03-29 11:11:44 -07003392 __func__, ip->i_ino, ip->i_forkoff, ip);
Brian Fosterf2019292020-05-06 13:25:20 -07003393 goto flush_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 }
Dave Chinnere60896d2013-07-24 15:47:30 +10003395
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 /*
Christoph Hellwig965e0a12021-03-29 11:11:42 -07003397 * Inode item log recovery for v2 inodes are dependent on the flushiter
3398 * count for correct sequencing. We bump the flush iteration count so
3399 * we can detect flushes which postdate a log record during recovery.
3400 * This is redundant as we now log every change and hence this can't
3401 * happen but we need to still do it to ensure backwards compatibility
3402 * with old kernels that predate logging all inode changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 */
Christoph Hellwig6471e9c2020-03-18 08:15:11 -07003404 if (!xfs_sb_version_has_v3inode(&mp->m_sb))
Christoph Hellwig965e0a12021-03-29 11:11:42 -07003405 ip->i_flushiter++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406
Christoph Hellwig0f45a1b2020-05-14 14:01:31 -07003407 /*
3408 * If there are inline format data / attr forks attached to this inode,
3409 * make sure they are not corrupt.
3410 */
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07003411 if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL &&
Christoph Hellwig0f45a1b2020-05-14 14:01:31 -07003412 xfs_ifork_verify_local_data(ip))
3413 goto flush_out;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07003414 if (ip->i_afp && ip->i_afp->if_format == XFS_DINODE_FMT_LOCAL &&
Christoph Hellwig0f45a1b2020-05-14 14:01:31 -07003415 xfs_ifork_verify_local_attr(ip))
Brian Fosterf2019292020-05-06 13:25:20 -07003416 goto flush_out;
Darrick J. Wong005c5db2017-03-28 14:51:10 -07003417
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 /*
Dave Chinner39878482016-02-09 16:54:58 +11003419 * Copy the dirty parts of the inode into the on-disk inode. We always
3420 * copy out the core of the inode, because if the inode is dirty at all
3421 * the core must be.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 */
Dave Chinner93f958f2016-02-09 16:54:58 +11003423 xfs_inode_to_disk(ip, dip, iip->ili_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424
3425 /* Wrap, we never let the log put out DI_MAX_FLUSH */
Christoph Hellwigee7b83f2021-03-29 11:11:43 -07003426 if (!xfs_sb_version_has_v3inode(&mp->m_sb)) {
3427 if (ip->i_flushiter == DI_MAX_FLUSH)
3428 ip->i_flushiter = 0;
3429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430
Darrick J. Wong005c5db2017-03-28 14:51:10 -07003431 xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK);
3432 if (XFS_IFORK_Q(ip))
3433 xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434
3435 /*
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00003436 * We've recorded everything logged in the inode, so we'd like to clear
3437 * the ili_fields bits so we don't log and flush things unnecessarily.
3438 * However, we can't stop logging all this information until the data
3439 * we've copied into the disk buffer is written to disk. If we did we
3440 * might overwrite the copy of the inode in the log with all the data
3441 * after re-logging only part of it, and in the face of a crash we
3442 * wouldn't have all the data we need to recover.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 *
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00003444 * What we do is move the bits to the ili_last_fields field. When
3445 * logging the inode, these bits are moved back to the ili_fields field.
Christoph Hellwig664ffb82020-09-01 10:55:29 -07003446 * In the xfs_buf_inode_iodone() routine we clear ili_last_fields, since
3447 * we know that the information those bits represent is permanently on
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00003448 * disk. As long as the flush completes before the inode is logged
3449 * again, then both ili_fields and ili_last_fields will be cleared.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 */
Brian Fosterf2019292020-05-06 13:25:20 -07003451 error = 0;
3452flush_out:
Dave Chinner1319ebe2020-06-29 14:48:46 -07003453 spin_lock(&iip->ili_lock);
Christoph Hellwig93848a92013-04-03 16:11:17 +11003454 iip->ili_last_fields = iip->ili_fields;
3455 iip->ili_fields = 0;
Dave Chinnerfc0561c2015-11-03 13:14:59 +11003456 iip->ili_fsync_fields = 0;
Dave Chinner1319ebe2020-06-29 14:48:46 -07003457 spin_unlock(&iip->ili_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458
Dave Chinner1319ebe2020-06-29 14:48:46 -07003459 /*
3460 * Store the current LSN of the inode so that we can tell whether the
Christoph Hellwig664ffb82020-09-01 10:55:29 -07003461 * item has moved in the AIL from xfs_buf_inode_iodone().
Dave Chinner1319ebe2020-06-29 14:48:46 -07003462 */
Christoph Hellwig93848a92013-04-03 16:11:17 +11003463 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
3464 &iip->ili_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465
Christoph Hellwig93848a92013-04-03 16:11:17 +11003466 /* generate the checksum. */
3467 xfs_dinode_calc_crc(mp, dip);
Brian Fosterf2019292020-05-06 13:25:20 -07003468 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469}
Darrick J. Wong44a87362018-07-25 12:52:32 -07003470
Dave Chinnere6187b32020-06-29 14:49:19 -07003471/*
3472 * Non-blocking flush of dirty inode metadata into the backing buffer.
3473 *
3474 * The caller must have a reference to the inode and hold the cluster buffer
3475 * locked. The function will walk across all the inodes on the cluster buffer it
3476 * can find and lock without blocking, and flush them to the cluster buffer.
3477 *
Dave Chinner5717ea42020-06-29 14:49:20 -07003478 * On successful flushing of at least one inode, the caller must write out the
3479 * buffer and release it. If no inodes are flushed, -EAGAIN will be returned and
3480 * the caller needs to release the buffer. On failure, the filesystem will be
3481 * shut down, the buffer will have been unlocked and released, and EFSCORRUPTED
3482 * will be returned.
Dave Chinnere6187b32020-06-29 14:49:19 -07003483 */
3484int
3485xfs_iflush_cluster(
Dave Chinnere6187b32020-06-29 14:49:19 -07003486 struct xfs_buf *bp)
3487{
Dave Chinner5717ea42020-06-29 14:49:20 -07003488 struct xfs_mount *mp = bp->b_mount;
3489 struct xfs_log_item *lip, *n;
3490 struct xfs_inode *ip;
3491 struct xfs_inode_log_item *iip;
Dave Chinnere6187b32020-06-29 14:49:19 -07003492 int clcount = 0;
Dave Chinner5717ea42020-06-29 14:49:20 -07003493 int error = 0;
Dave Chinnere6187b32020-06-29 14:49:19 -07003494
Dave Chinner5717ea42020-06-29 14:49:20 -07003495 /*
3496 * We must use the safe variant here as on shutdown xfs_iflush_abort()
3497 * can remove itself from the list.
3498 */
3499 list_for_each_entry_safe(lip, n, &bp->b_li_list, li_bio_list) {
3500 iip = (struct xfs_inode_log_item *)lip;
3501 ip = iip->ili_inode;
Dave Chinnere6187b32020-06-29 14:49:19 -07003502
3503 /*
Dave Chinner5717ea42020-06-29 14:49:20 -07003504 * Quick and dirty check to avoid locks if possible.
Dave Chinnere6187b32020-06-29 14:49:19 -07003505 */
Dave Chinner718ecc52020-08-17 16:41:01 -07003506 if (__xfs_iflags_test(ip, XFS_IRECLAIM | XFS_IFLUSHING))
Dave Chinner5717ea42020-06-29 14:49:20 -07003507 continue;
3508 if (xfs_ipincount(ip))
3509 continue;
3510
3511 /*
3512 * The inode is still attached to the buffer, which means it is
3513 * dirty but reclaim might try to grab it. Check carefully for
3514 * that, and grab the ilock while still holding the i_flags_lock
3515 * to guarantee reclaim will not be able to reclaim this inode
3516 * once we drop the i_flags_lock.
3517 */
3518 spin_lock(&ip->i_flags_lock);
3519 ASSERT(!__xfs_iflags_test(ip, XFS_ISTALE));
Dave Chinner718ecc52020-08-17 16:41:01 -07003520 if (__xfs_iflags_test(ip, XFS_IRECLAIM | XFS_IFLUSHING)) {
Dave Chinner5717ea42020-06-29 14:49:20 -07003521 spin_unlock(&ip->i_flags_lock);
Dave Chinnere6187b32020-06-29 14:49:19 -07003522 continue;
3523 }
3524
3525 /*
Dave Chinner5717ea42020-06-29 14:49:20 -07003526 * ILOCK will pin the inode against reclaim and prevent
3527 * concurrent transactions modifying the inode while we are
Dave Chinner718ecc52020-08-17 16:41:01 -07003528 * flushing the inode. If we get the lock, set the flushing
3529 * state before we drop the i_flags_lock.
Dave Chinnere6187b32020-06-29 14:49:19 -07003530 */
Dave Chinner5717ea42020-06-29 14:49:20 -07003531 if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
3532 spin_unlock(&ip->i_flags_lock);
3533 continue;
3534 }
Dave Chinner718ecc52020-08-17 16:41:01 -07003535 __xfs_iflags_set(ip, XFS_IFLUSHING);
Dave Chinner5717ea42020-06-29 14:49:20 -07003536 spin_unlock(&ip->i_flags_lock);
3537
3538 /*
Dave Chinner5717ea42020-06-29 14:49:20 -07003539 * Abort flushing this inode if we are shut down because the
3540 * inode may not currently be in the AIL. This can occur when
3541 * log I/O failure unpins the inode without inserting into the
3542 * AIL, leaving a dirty/unpinned inode attached to the buffer
3543 * that otherwise looks like it should be flushed.
3544 */
3545 if (XFS_FORCED_SHUTDOWN(mp)) {
3546 xfs_iunpin_wait(ip);
Dave Chinner5717ea42020-06-29 14:49:20 -07003547 xfs_iflush_abort(ip);
3548 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3549 error = -EIO;
3550 continue;
3551 }
3552
3553 /* don't block waiting on a log force to unpin dirty inodes */
3554 if (xfs_ipincount(ip)) {
Dave Chinner718ecc52020-08-17 16:41:01 -07003555 xfs_iflags_clear(ip, XFS_IFLUSHING);
Dave Chinner5717ea42020-06-29 14:49:20 -07003556 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3557 continue;
3558 }
3559
3560 if (!xfs_inode_clean(ip))
3561 error = xfs_iflush(ip, bp);
3562 else
Dave Chinner718ecc52020-08-17 16:41:01 -07003563 xfs_iflags_clear(ip, XFS_IFLUSHING);
Dave Chinner5717ea42020-06-29 14:49:20 -07003564 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3565 if (error)
Dave Chinnere6187b32020-06-29 14:49:19 -07003566 break;
Dave Chinner5717ea42020-06-29 14:49:20 -07003567 clcount++;
Dave Chinnere6187b32020-06-29 14:49:19 -07003568 }
3569
Dave Chinnere6187b32020-06-29 14:49:19 -07003570 if (error) {
3571 bp->b_flags |= XBF_ASYNC;
3572 xfs_buf_ioend_fail(bp);
3573 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Dave Chinner5717ea42020-06-29 14:49:20 -07003574 return error;
Dave Chinnere6187b32020-06-29 14:49:19 -07003575 }
Dave Chinner5717ea42020-06-29 14:49:20 -07003576
3577 if (!clcount)
3578 return -EAGAIN;
3579
3580 XFS_STATS_INC(mp, xs_icluster_flushcnt);
3581 XFS_STATS_ADD(mp, xs_icluster_flushinode, clcount);
3582 return 0;
3583
Dave Chinnere6187b32020-06-29 14:49:19 -07003584}
3585
Darrick J. Wong44a87362018-07-25 12:52:32 -07003586/* Release an inode. */
3587void
3588xfs_irele(
3589 struct xfs_inode *ip)
3590{
3591 trace_xfs_irele(ip, _RET_IP_);
3592 iput(VFS_I(ip));
3593}
Christoph Hellwig54fbdd12020-04-03 11:45:37 -07003594
3595/*
3596 * Ensure all commited transactions touching the inode are written to the log.
3597 */
3598int
3599xfs_log_force_inode(
3600 struct xfs_inode *ip)
3601{
3602 xfs_lsn_t lsn = 0;
3603
3604 xfs_ilock(ip, XFS_ILOCK_SHARED);
3605 if (xfs_ipincount(ip))
3606 lsn = ip->i_itemp->ili_last_lsn;
3607 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3608
3609 if (!lsn)
3610 return 0;
3611 return xfs_log_force_lsn(ip->i_mount, lsn, XFS_LOG_SYNC, NULL);
3612}
Darrick J. Wonge2aaee92020-06-29 14:47:20 -07003613
3614/*
3615 * Grab the exclusive iolock for a data copy from src to dest, making sure to
3616 * abide vfs locking order (lowest pointer value goes first) and breaking the
3617 * layout leases before proceeding. The loop is needed because we cannot call
3618 * the blocking break_layout() with the iolocks held, and therefore have to
3619 * back out both locks.
3620 */
3621static int
3622xfs_iolock_two_inodes_and_break_layout(
3623 struct inode *src,
3624 struct inode *dest)
3625{
3626 int error;
3627
3628 if (src > dest)
3629 swap(src, dest);
3630
3631retry:
3632 /* Wait to break both inodes' layouts before we start locking. */
3633 error = break_layout(src, true);
3634 if (error)
3635 return error;
3636 if (src != dest) {
3637 error = break_layout(dest, true);
3638 if (error)
3639 return error;
3640 }
3641
3642 /* Lock one inode and make sure nobody got in and leased it. */
3643 inode_lock(src);
3644 error = break_layout(src, false);
3645 if (error) {
3646 inode_unlock(src);
3647 if (error == -EWOULDBLOCK)
3648 goto retry;
3649 return error;
3650 }
3651
3652 if (src == dest)
3653 return 0;
3654
3655 /* Lock the other inode and make sure nobody got in and leased it. */
3656 inode_lock_nested(dest, I_MUTEX_NONDIR2);
3657 error = break_layout(dest, false);
3658 if (error) {
3659 inode_unlock(src);
3660 inode_unlock(dest);
3661 if (error == -EWOULDBLOCK)
3662 goto retry;
3663 return error;
3664 }
3665
3666 return 0;
3667}
3668
3669/*
3670 * Lock two inodes so that userspace cannot initiate I/O via file syscalls or
3671 * mmap activity.
3672 */
3673int
3674xfs_ilock2_io_mmap(
3675 struct xfs_inode *ip1,
3676 struct xfs_inode *ip2)
3677{
3678 int ret;
3679
3680 ret = xfs_iolock_two_inodes_and_break_layout(VFS_I(ip1), VFS_I(ip2));
3681 if (ret)
3682 return ret;
3683 if (ip1 == ip2)
3684 xfs_ilock(ip1, XFS_MMAPLOCK_EXCL);
3685 else
3686 xfs_lock_two_inodes(ip1, XFS_MMAPLOCK_EXCL,
3687 ip2, XFS_MMAPLOCK_EXCL);
3688 return 0;
3689}
3690
3691/* Unlock both inodes to allow IO and mmap activity. */
3692void
3693xfs_iunlock2_io_mmap(
3694 struct xfs_inode *ip1,
3695 struct xfs_inode *ip2)
3696{
3697 bool same_inode = (ip1 == ip2);
3698
3699 xfs_iunlock(ip2, XFS_MMAPLOCK_EXCL);
3700 if (!same_inode)
3701 xfs_iunlock(ip1, XFS_MMAPLOCK_EXCL);
3702 inode_unlock(VFS_I(ip2));
3703 if (!same_inode)
3704 inode_unlock(VFS_I(ip1));
3705}