blob: 2bfbcf28b1bd282613ad1a803cb7fda596170cd5 [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_sb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "xfs_mount.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100016#include "xfs_defer.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110017#include "xfs_inode.h"
Dave Chinnerc24b5df2013-08-12 20:49:45 +100018#include "xfs_dir2.h"
Dave Chinnerc24b5df2013-08-12 20:49:45 +100019#include "xfs_attr.h"
Dave Chinner239880e2013-10-23 10:50:10 +110020#include "xfs_trans_space.h"
21#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_buf_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inode_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_ialloc.h"
25#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100026#include "xfs_bmap_util.h"
Darrick J. Wonge9e899a2017-10-31 12:04:49 -070027#include "xfs_errortag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_quota.h"
David Chinner2a82b8b2007-07-11 11:09:12 +100030#include "xfs_filestream.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000031#include "xfs_trace.h"
Dave Chinner33479e02012-10-08 21:56:11 +110032#include "xfs_icache.h"
Dave Chinnerc24b5df2013-08-12 20:49:45 +100033#include "xfs_symlink.h"
Dave Chinner239880e2013-10-23 10:50:10 +110034#include "xfs_trans_priv.h"
35#include "xfs_log.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110036#include "xfs_bmap_btree.h"
Darrick J. Wongaa8968f2016-10-03 09:11:38 -070037#include "xfs_reflink.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;
Dave Chinner2a0ec1d2012-04-23 15:59:02 +100063 if ((ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE) && ip->i_d.di_extsize)
64 return ip->i_d.di_extsize;
65 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;
83 if (ip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE)
84 a = ip->i_d.di_cowextsize;
85 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 Hellwigf7e67b22020-05-18 10:28:05 -0700114 if (ip->i_df.if_format == XFS_DINODE_FMT_BTREE &&
Christoph Hellwig309ecac82013-12-06 12:30:09 -0800115 (ip->i_df.if_flags & XFS_IFEXTENTS) == 0)
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100116 lock_mode = XFS_ILOCK_EXCL;
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100117 xfs_ilock(ip, lock_mode);
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100118 return lock_mode;
119}
120
Christoph Hellwigefa70be2013-12-18 02:14:39 -0800121uint
122xfs_ilock_attr_map_shared(
123 struct xfs_inode *ip)
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100124{
Christoph Hellwigefa70be2013-12-18 02:14:39 -0800125 uint lock_mode = XFS_ILOCK_SHARED;
126
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700127 if (ip->i_afp &&
128 ip->i_afp->if_format == XFS_DINODE_FMT_BTREE &&
Christoph Hellwigefa70be2013-12-18 02:14:39 -0800129 (ip->i_afp->if_flags & XFS_IFEXTENTS) == 0)
130 lock_mode = XFS_ILOCK_EXCL;
131 xfs_ilock(ip, lock_mode);
132 return lock_mode;
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100133}
134
135/*
Christoph Hellwig65523212016-11-30 14:33:25 +1100136 * In addition to i_rwsem in the VFS inode, the xfs inode contains 2
137 * multi-reader locks: i_mmap_lock and the i_lock. This routine allows
138 * various combinations of the locks to be obtained.
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100139 *
Dave Chinner653c60b2015-02-23 21:43:37 +1100140 * The 3 locks should always be ordered so that the IO lock is obtained first,
141 * the mmap lock second and the ilock last in order to prevent deadlock.
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100142 *
Dave Chinner653c60b2015-02-23 21:43:37 +1100143 * Basic locking order:
144 *
Christoph Hellwig65523212016-11-30 14:33:25 +1100145 * i_rwsem -> i_mmap_lock -> page_lock -> i_ilock
Dave Chinner653c60b2015-02-23 21:43:37 +1100146 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700147 * mmap_lock locking order:
Dave Chinner653c60b2015-02-23 21:43:37 +1100148 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700149 * i_rwsem -> page lock -> mmap_lock
150 * mmap_lock -> i_mmap_lock -> page_lock
Dave Chinner653c60b2015-02-23 21:43:37 +1100151 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700152 * The difference in mmap_lock locking order mean that we cannot hold the
Dave Chinner653c60b2015-02-23 21:43:37 +1100153 * i_mmap_lock over syscall based read(2)/write(2) based IO. These IO paths can
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700154 * fault in pages during copy in/out (for buffered IO) or require the mmap_lock
Dave Chinner653c60b2015-02-23 21:43:37 +1100155 * in get_user_pages() to map the user pages into the kernel address space for
Christoph Hellwig65523212016-11-30 14:33:25 +1100156 * direct IO. Similarly the i_rwsem cannot be taken inside a page fault because
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700157 * page faults already hold the mmap_lock.
Dave Chinner653c60b2015-02-23 21:43:37 +1100158 *
159 * Hence to serialise fully against both syscall and mmap based IO, we need to
Christoph Hellwig65523212016-11-30 14:33:25 +1100160 * take both the i_rwsem and the i_mmap_lock. These locks should *only* be both
Dave Chinner653c60b2015-02-23 21:43:37 +1100161 * taken in places where we need to invalidate the page cache in a race
162 * free manner (e.g. truncate, hole punch and other extent manipulation
163 * functions).
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100164 */
165void
166xfs_ilock(
167 xfs_inode_t *ip,
168 uint lock_flags)
169{
170 trace_xfs_ilock(ip, lock_flags, _RET_IP_);
171
172 /*
173 * You can't set both SHARED and EXCL for the same lock,
174 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
175 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
176 */
177 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
178 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
Dave Chinner653c60b2015-02-23 21:43:37 +1100179 ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
180 (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100181 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
182 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Dave Chinner0952c812015-08-19 10:32:49 +1000183 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100184
Christoph Hellwig65523212016-11-30 14:33:25 +1100185 if (lock_flags & XFS_IOLOCK_EXCL) {
186 down_write_nested(&VFS_I(ip)->i_rwsem,
187 XFS_IOLOCK_DEP(lock_flags));
188 } else if (lock_flags & XFS_IOLOCK_SHARED) {
189 down_read_nested(&VFS_I(ip)->i_rwsem,
190 XFS_IOLOCK_DEP(lock_flags));
191 }
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100192
Dave Chinner653c60b2015-02-23 21:43:37 +1100193 if (lock_flags & XFS_MMAPLOCK_EXCL)
194 mrupdate_nested(&ip->i_mmaplock, XFS_MMAPLOCK_DEP(lock_flags));
195 else if (lock_flags & XFS_MMAPLOCK_SHARED)
196 mraccess_nested(&ip->i_mmaplock, XFS_MMAPLOCK_DEP(lock_flags));
197
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100198 if (lock_flags & XFS_ILOCK_EXCL)
199 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
200 else if (lock_flags & XFS_ILOCK_SHARED)
201 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
202}
203
204/*
205 * This is just like xfs_ilock(), except that the caller
206 * is guaranteed not to sleep. It returns 1 if it gets
207 * the requested locks and 0 otherwise. If the IO lock is
208 * obtained but the inode lock cannot be, then the IO lock
209 * is dropped before returning.
210 *
211 * ip -- the inode being locked
212 * lock_flags -- this parameter indicates the inode's locks to be
213 * to be locked. See the comment for xfs_ilock() for a list
214 * of valid values.
215 */
216int
217xfs_ilock_nowait(
218 xfs_inode_t *ip,
219 uint lock_flags)
220{
221 trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_);
222
223 /*
224 * You can't set both SHARED and EXCL for the same lock,
225 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
226 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
227 */
228 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
229 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
Dave Chinner653c60b2015-02-23 21:43:37 +1100230 ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
231 (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100232 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
233 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Dave Chinner0952c812015-08-19 10:32:49 +1000234 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100235
236 if (lock_flags & XFS_IOLOCK_EXCL) {
Christoph Hellwig65523212016-11-30 14:33:25 +1100237 if (!down_write_trylock(&VFS_I(ip)->i_rwsem))
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100238 goto out;
239 } else if (lock_flags & XFS_IOLOCK_SHARED) {
Christoph Hellwig65523212016-11-30 14:33:25 +1100240 if (!down_read_trylock(&VFS_I(ip)->i_rwsem))
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100241 goto out;
242 }
Dave Chinner653c60b2015-02-23 21:43:37 +1100243
244 if (lock_flags & XFS_MMAPLOCK_EXCL) {
245 if (!mrtryupdate(&ip->i_mmaplock))
246 goto out_undo_iolock;
247 } else if (lock_flags & XFS_MMAPLOCK_SHARED) {
248 if (!mrtryaccess(&ip->i_mmaplock))
249 goto out_undo_iolock;
250 }
251
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100252 if (lock_flags & XFS_ILOCK_EXCL) {
253 if (!mrtryupdate(&ip->i_lock))
Dave Chinner653c60b2015-02-23 21:43:37 +1100254 goto out_undo_mmaplock;
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100255 } else if (lock_flags & XFS_ILOCK_SHARED) {
256 if (!mrtryaccess(&ip->i_lock))
Dave Chinner653c60b2015-02-23 21:43:37 +1100257 goto out_undo_mmaplock;
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100258 }
259 return 1;
260
Dave Chinner653c60b2015-02-23 21:43:37 +1100261out_undo_mmaplock:
262 if (lock_flags & XFS_MMAPLOCK_EXCL)
263 mrunlock_excl(&ip->i_mmaplock);
264 else if (lock_flags & XFS_MMAPLOCK_SHARED)
265 mrunlock_shared(&ip->i_mmaplock);
266out_undo_iolock:
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100267 if (lock_flags & XFS_IOLOCK_EXCL)
Christoph Hellwig65523212016-11-30 14:33:25 +1100268 up_write(&VFS_I(ip)->i_rwsem);
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100269 else if (lock_flags & XFS_IOLOCK_SHARED)
Christoph Hellwig65523212016-11-30 14:33:25 +1100270 up_read(&VFS_I(ip)->i_rwsem);
Dave Chinner653c60b2015-02-23 21:43:37 +1100271out:
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100272 return 0;
273}
274
275/*
276 * xfs_iunlock() is used to drop the inode locks acquired with
277 * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
278 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
279 * that we know which locks to drop.
280 *
281 * ip -- the inode being unlocked
282 * lock_flags -- this parameter indicates the inode's locks to be
283 * to be unlocked. See the comment for xfs_ilock() for a list
284 * of valid values for this parameter.
285 *
286 */
287void
288xfs_iunlock(
289 xfs_inode_t *ip,
290 uint lock_flags)
291{
292 /*
293 * You can't set both SHARED and EXCL for the same lock,
294 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
295 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
296 */
297 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
298 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
Dave Chinner653c60b2015-02-23 21:43:37 +1100299 ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
300 (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100301 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
302 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Dave Chinner0952c812015-08-19 10:32:49 +1000303 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100304 ASSERT(lock_flags != 0);
305
306 if (lock_flags & XFS_IOLOCK_EXCL)
Christoph Hellwig65523212016-11-30 14:33:25 +1100307 up_write(&VFS_I(ip)->i_rwsem);
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100308 else if (lock_flags & XFS_IOLOCK_SHARED)
Christoph Hellwig65523212016-11-30 14:33:25 +1100309 up_read(&VFS_I(ip)->i_rwsem);
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100310
Dave Chinner653c60b2015-02-23 21:43:37 +1100311 if (lock_flags & XFS_MMAPLOCK_EXCL)
312 mrunlock_excl(&ip->i_mmaplock);
313 else if (lock_flags & XFS_MMAPLOCK_SHARED)
314 mrunlock_shared(&ip->i_mmaplock);
315
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100316 if (lock_flags & XFS_ILOCK_EXCL)
317 mrunlock_excl(&ip->i_lock);
318 else if (lock_flags & XFS_ILOCK_SHARED)
319 mrunlock_shared(&ip->i_lock);
320
321 trace_xfs_iunlock(ip, lock_flags, _RET_IP_);
322}
323
324/*
325 * give up write locks. the i/o lock cannot be held nested
326 * if it is being demoted.
327 */
328void
329xfs_ilock_demote(
330 xfs_inode_t *ip,
331 uint lock_flags)
332{
Dave Chinner653c60b2015-02-23 21:43:37 +1100333 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL));
334 ASSERT((lock_flags &
335 ~(XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100336
337 if (lock_flags & XFS_ILOCK_EXCL)
338 mrdemote(&ip->i_lock);
Dave Chinner653c60b2015-02-23 21:43:37 +1100339 if (lock_flags & XFS_MMAPLOCK_EXCL)
340 mrdemote(&ip->i_mmaplock);
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100341 if (lock_flags & XFS_IOLOCK_EXCL)
Christoph Hellwig65523212016-11-30 14:33:25 +1100342 downgrade_write(&VFS_I(ip)->i_rwsem);
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100343
344 trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_);
345}
346
Dave Chinner742ae1e2013-04-30 21:39:34 +1000347#if defined(DEBUG) || defined(XFS_WARN)
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100348int
349xfs_isilocked(
350 xfs_inode_t *ip,
351 uint lock_flags)
352{
353 if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) {
354 if (!(lock_flags & XFS_ILOCK_SHARED))
355 return !!ip->i_lock.mr_writer;
356 return rwsem_is_locked(&ip->i_lock.mr_lock);
357 }
358
Dave Chinner653c60b2015-02-23 21:43:37 +1100359 if (lock_flags & (XFS_MMAPLOCK_EXCL|XFS_MMAPLOCK_SHARED)) {
360 if (!(lock_flags & XFS_MMAPLOCK_SHARED))
361 return !!ip->i_mmaplock.mr_writer;
362 return rwsem_is_locked(&ip->i_mmaplock.mr_lock);
363 }
364
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100365 if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) {
366 if (!(lock_flags & XFS_IOLOCK_SHARED))
Christoph Hellwig65523212016-11-30 14:33:25 +1100367 return !debug_locks ||
368 lockdep_is_held_type(&VFS_I(ip)->i_rwsem, 0);
369 return rwsem_is_locked(&VFS_I(ip)->i_rwsem);
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100370 }
371
372 ASSERT(0);
373 return 0;
374}
375#endif
376
Dave Chinnerb6a99472015-08-25 10:05:13 +1000377/*
378 * xfs_lockdep_subclass_ok() is only used in an ASSERT, so is only called when
379 * DEBUG or XFS_WARN is set. And MAX_LOCKDEP_SUBCLASSES is then only defined
380 * when CONFIG_LOCKDEP is set. Hence the complex define below to avoid build
381 * errors and warnings.
382 */
383#if (defined(DEBUG) || defined(XFS_WARN)) && defined(CONFIG_LOCKDEP)
Dave Chinner3403ccc2015-08-20 09:27:49 +1000384static bool
385xfs_lockdep_subclass_ok(
386 int subclass)
387{
388 return subclass < MAX_LOCKDEP_SUBCLASSES;
389}
390#else
391#define xfs_lockdep_subclass_ok(subclass) (true)
392#endif
393
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000394/*
Dave Chinner653c60b2015-02-23 21:43:37 +1100395 * Bump the subclass so xfs_lock_inodes() acquires each lock with a different
Dave Chinner0952c812015-08-19 10:32:49 +1000396 * value. This can be called for any type of inode lock combination, including
397 * parent locking. Care must be taken to ensure we don't overrun the subclass
398 * storage fields in the class mask we build.
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000399 */
400static inline int
401xfs_lock_inumorder(int lock_mode, int subclass)
402{
Dave Chinner0952c812015-08-19 10:32:49 +1000403 int class = 0;
404
405 ASSERT(!(lock_mode & (XFS_ILOCK_PARENT | XFS_ILOCK_RTBITMAP |
406 XFS_ILOCK_RTSUM)));
Dave Chinner3403ccc2015-08-20 09:27:49 +1000407 ASSERT(xfs_lockdep_subclass_ok(subclass));
Dave Chinner0952c812015-08-19 10:32:49 +1000408
Dave Chinner653c60b2015-02-23 21:43:37 +1100409 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) {
Dave Chinner0952c812015-08-19 10:32:49 +1000410 ASSERT(subclass <= XFS_IOLOCK_MAX_SUBCLASS);
Dave Chinner0952c812015-08-19 10:32:49 +1000411 class += subclass << XFS_IOLOCK_SHIFT;
Dave Chinner653c60b2015-02-23 21:43:37 +1100412 }
413
414 if (lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) {
Dave Chinner0952c812015-08-19 10:32:49 +1000415 ASSERT(subclass <= XFS_MMAPLOCK_MAX_SUBCLASS);
416 class += subclass << XFS_MMAPLOCK_SHIFT;
Dave Chinner653c60b2015-02-23 21:43:37 +1100417 }
418
Dave Chinner0952c812015-08-19 10:32:49 +1000419 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) {
420 ASSERT(subclass <= XFS_ILOCK_MAX_SUBCLASS);
421 class += subclass << XFS_ILOCK_SHIFT;
422 }
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000423
Dave Chinner0952c812015-08-19 10:32:49 +1000424 return (lock_mode & ~XFS_LOCK_SUBCLASS_MASK) | class;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000425}
426
427/*
Dave Chinner95afcf52015-03-25 14:03:32 +1100428 * The following routine will lock n inodes in exclusive mode. We assume the
429 * caller calls us with the inodes in i_ino order.
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000430 *
Dave Chinner95afcf52015-03-25 14:03:32 +1100431 * We need to detect deadlock where an inode that we lock is in the AIL and we
432 * start waiting for another inode that is locked by a thread in a long running
433 * transaction (such as truncate). This can result in deadlock since the long
434 * running trans might need to wait for the inode we just locked in order to
435 * push the tail and free space in the log.
Dave Chinner0952c812015-08-19 10:32:49 +1000436 *
437 * xfs_lock_inodes() can only be used to lock one type of lock at a time -
438 * the iolock, the mmaplock or the ilock, but not more than one at a time. If we
439 * lock more than one at a time, lockdep will report false positives saying we
440 * have violated locking orders.
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000441 */
Eric Sandeen0d5a75e2016-06-01 17:38:15 +1000442static void
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000443xfs_lock_inodes(
Christoph Hellwigefe23302019-06-28 19:27:33 -0700444 struct xfs_inode **ips,
445 int inodes,
446 uint lock_mode)
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000447{
Christoph Hellwigefe23302019-06-28 19:27:33 -0700448 int attempts = 0, i, j, try_lock;
449 struct xfs_log_item *lp;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000450
Dave Chinner0952c812015-08-19 10:32:49 +1000451 /*
452 * Currently supports between 2 and 5 inodes with exclusive locking. We
453 * support an arbitrary depth of locking here, but absolute limits on
Randy Dunlapb63da6c2020-08-05 08:49:58 -0700454 * inodes depend on the type of locking and the limits placed by
Dave Chinner0952c812015-08-19 10:32:49 +1000455 * lockdep annotations in xfs_lock_inumorder. These are all checked by
456 * the asserts.
457 */
Dave Chinner95afcf52015-03-25 14:03:32 +1100458 ASSERT(ips && inodes >= 2 && inodes <= 5);
Dave Chinner0952c812015-08-19 10:32:49 +1000459 ASSERT(lock_mode & (XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL |
460 XFS_ILOCK_EXCL));
461 ASSERT(!(lock_mode & (XFS_IOLOCK_SHARED | XFS_MMAPLOCK_SHARED |
462 XFS_ILOCK_SHARED)));
Dave Chinner0952c812015-08-19 10:32:49 +1000463 ASSERT(!(lock_mode & XFS_MMAPLOCK_EXCL) ||
464 inodes <= XFS_MMAPLOCK_MAX_SUBCLASS + 1);
465 ASSERT(!(lock_mode & XFS_ILOCK_EXCL) ||
466 inodes <= XFS_ILOCK_MAX_SUBCLASS + 1);
467
468 if (lock_mode & XFS_IOLOCK_EXCL) {
469 ASSERT(!(lock_mode & (XFS_MMAPLOCK_EXCL | XFS_ILOCK_EXCL)));
470 } else if (lock_mode & XFS_MMAPLOCK_EXCL)
471 ASSERT(!(lock_mode & XFS_ILOCK_EXCL));
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000472
473 try_lock = 0;
474 i = 0;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000475again:
476 for (; i < inodes; i++) {
477 ASSERT(ips[i]);
478
Dave Chinner95afcf52015-03-25 14:03:32 +1100479 if (i && (ips[i] == ips[i - 1])) /* Already locked */
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000480 continue;
481
482 /*
Dave Chinner95afcf52015-03-25 14:03:32 +1100483 * If try_lock is not set yet, make sure all locked inodes are
484 * not in the AIL. If any are, set try_lock to be used later.
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000485 */
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000486 if (!try_lock) {
487 for (j = (i - 1); j >= 0 && !try_lock; j--) {
Christoph Hellwigb3b14aa2019-06-28 19:27:33 -0700488 lp = &ips[j]->i_itemp->ili_item;
Dave Chinner22525c12018-05-09 07:47:34 -0700489 if (lp && test_bit(XFS_LI_IN_AIL, &lp->li_flags))
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000490 try_lock++;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000491 }
492 }
493
494 /*
495 * If any of the previous locks we have locked is in the AIL,
496 * we must TRY to get the second and subsequent locks. If
497 * we can't get any, we must release all we have
498 * and try again.
499 */
Dave Chinner95afcf52015-03-25 14:03:32 +1100500 if (!try_lock) {
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000501 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
Dave Chinner95afcf52015-03-25 14:03:32 +1100502 continue;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000503 }
Dave Chinner95afcf52015-03-25 14:03:32 +1100504
505 /* try_lock means we have an inode locked that is in the AIL. */
506 ASSERT(i != 0);
507 if (xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i)))
508 continue;
509
510 /*
511 * Unlock all previous guys and try again. xfs_iunlock will try
512 * to push the tail if the inode is in the AIL.
513 */
514 attempts++;
515 for (j = i - 1; j >= 0; j--) {
516 /*
517 * Check to see if we've already unlocked this one. Not
518 * the first one going back, and the inode ptr is the
519 * same.
520 */
521 if (j != (i - 1) && ips[j] == ips[j + 1])
522 continue;
523
524 xfs_iunlock(ips[j], lock_mode);
525 }
526
527 if ((attempts % 5) == 0) {
528 delay(1); /* Don't just spin the CPU */
Dave Chinner95afcf52015-03-25 14:03:32 +1100529 }
530 i = 0;
531 try_lock = 0;
532 goto again;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000533 }
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000534}
535
536/*
Dave Chinner653c60b2015-02-23 21:43:37 +1100537 * 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 -0800538 * the mmaplock or the ilock, but not more than one type at a time. If we lock
539 * more than one at a time, lockdep will report false positives saying we have
540 * violated locking orders. The iolock must be double-locked separately since
541 * we use i_rwsem for that. We now support taking one lock EXCL and the other
542 * SHARED.
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000543 */
544void
545xfs_lock_two_inodes(
Darrick J. Wong7c2d2382018-01-26 15:27:33 -0800546 struct xfs_inode *ip0,
547 uint ip0_mode,
548 struct xfs_inode *ip1,
549 uint ip1_mode)
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000550{
Darrick J. Wong7c2d2382018-01-26 15:27:33 -0800551 struct xfs_inode *temp;
552 uint mode_temp;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000553 int attempts = 0;
Christoph Hellwigefe23302019-06-28 19:27:33 -0700554 struct xfs_log_item *lp;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000555
Darrick J. Wong7c2d2382018-01-26 15:27:33 -0800556 ASSERT(hweight32(ip0_mode) == 1);
557 ASSERT(hweight32(ip1_mode) == 1);
558 ASSERT(!(ip0_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)));
559 ASSERT(!(ip1_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)));
560 ASSERT(!(ip0_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) ||
561 !(ip0_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
562 ASSERT(!(ip1_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) ||
563 !(ip1_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
564 ASSERT(!(ip1_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) ||
565 !(ip0_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
566 ASSERT(!(ip0_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) ||
567 !(ip1_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
Dave Chinner653c60b2015-02-23 21:43:37 +1100568
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000569 ASSERT(ip0->i_ino != ip1->i_ino);
570
571 if (ip0->i_ino > ip1->i_ino) {
572 temp = ip0;
573 ip0 = ip1;
574 ip1 = temp;
Darrick J. Wong7c2d2382018-01-26 15:27:33 -0800575 mode_temp = ip0_mode;
576 ip0_mode = ip1_mode;
577 ip1_mode = mode_temp;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000578 }
579
580 again:
Darrick J. Wong7c2d2382018-01-26 15:27:33 -0800581 xfs_ilock(ip0, xfs_lock_inumorder(ip0_mode, 0));
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000582
583 /*
584 * If the first lock we have locked is in the AIL, we must TRY to get
585 * the second lock. If we can't get it, we must release the first one
586 * and try again.
587 */
Christoph Hellwigb3b14aa2019-06-28 19:27:33 -0700588 lp = &ip0->i_itemp->ili_item;
Dave Chinner22525c12018-05-09 07:47:34 -0700589 if (lp && test_bit(XFS_LI_IN_AIL, &lp->li_flags)) {
Darrick J. Wong7c2d2382018-01-26 15:27:33 -0800590 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(ip1_mode, 1))) {
591 xfs_iunlock(ip0, ip0_mode);
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000592 if ((++attempts % 5) == 0)
593 delay(1); /* Don't just spin the CPU */
594 goto again;
595 }
596 } else {
Darrick J. Wong7c2d2382018-01-26 15:27:33 -0800597 xfs_ilock(ip1, xfs_lock_inumorder(ip1_mode, 1));
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000598 }
599}
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601STATIC uint
602_xfs_dic2xflags(
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700603 uint16_t di_flags,
Dave Chinner58f88ca2016-01-04 16:44:15 +1100604 uint64_t di_flags2,
605 bool has_attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 uint flags = 0;
608
609 if (di_flags & XFS_DIFLAG_ANY) {
610 if (di_flags & XFS_DIFLAG_REALTIME)
Dave Chinnere7b89482016-01-04 16:44:15 +1100611 flags |= FS_XFLAG_REALTIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (di_flags & XFS_DIFLAG_PREALLOC)
Dave Chinnere7b89482016-01-04 16:44:15 +1100613 flags |= FS_XFLAG_PREALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (di_flags & XFS_DIFLAG_IMMUTABLE)
Dave Chinnere7b89482016-01-04 16:44:15 +1100615 flags |= FS_XFLAG_IMMUTABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (di_flags & XFS_DIFLAG_APPEND)
Dave Chinnere7b89482016-01-04 16:44:15 +1100617 flags |= FS_XFLAG_APPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (di_flags & XFS_DIFLAG_SYNC)
Dave Chinnere7b89482016-01-04 16:44:15 +1100619 flags |= FS_XFLAG_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (di_flags & XFS_DIFLAG_NOATIME)
Dave Chinnere7b89482016-01-04 16:44:15 +1100621 flags |= FS_XFLAG_NOATIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (di_flags & XFS_DIFLAG_NODUMP)
Dave Chinnere7b89482016-01-04 16:44:15 +1100623 flags |= FS_XFLAG_NODUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (di_flags & XFS_DIFLAG_RTINHERIT)
Dave Chinnere7b89482016-01-04 16:44:15 +1100625 flags |= FS_XFLAG_RTINHERIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (di_flags & XFS_DIFLAG_PROJINHERIT)
Dave Chinnere7b89482016-01-04 16:44:15 +1100627 flags |= FS_XFLAG_PROJINHERIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (di_flags & XFS_DIFLAG_NOSYMLINKS)
Dave Chinnere7b89482016-01-04 16:44:15 +1100629 flags |= FS_XFLAG_NOSYMLINKS;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100630 if (di_flags & XFS_DIFLAG_EXTSIZE)
Dave Chinnere7b89482016-01-04 16:44:15 +1100631 flags |= FS_XFLAG_EXTSIZE;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100632 if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
Dave Chinnere7b89482016-01-04 16:44:15 +1100633 flags |= FS_XFLAG_EXTSZINHERIT;
Barry Naujokd3446ea2006-06-09 14:54:19 +1000634 if (di_flags & XFS_DIFLAG_NODEFRAG)
Dave Chinnere7b89482016-01-04 16:44:15 +1100635 flags |= FS_XFLAG_NODEFRAG;
David Chinner2a82b8b2007-07-11 11:09:12 +1000636 if (di_flags & XFS_DIFLAG_FILESTREAM)
Dave Chinnere7b89482016-01-04 16:44:15 +1100637 flags |= FS_XFLAG_FILESTREAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
639
Dave Chinner58f88ca2016-01-04 16:44:15 +1100640 if (di_flags2 & XFS_DIFLAG2_ANY) {
641 if (di_flags2 & XFS_DIFLAG2_DAX)
642 flags |= FS_XFLAG_DAX;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700643 if (di_flags2 & XFS_DIFLAG2_COWEXTSIZE)
644 flags |= FS_XFLAG_COWEXTSIZE;
Dave Chinner58f88ca2016-01-04 16:44:15 +1100645 }
646
647 if (has_attr)
648 flags |= FS_XFLAG_HASATTR;
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return flags;
651}
652
653uint
654xfs_ip2xflags(
Dave Chinner58f88ca2016-01-04 16:44:15 +1100655 struct xfs_inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Dave Chinner58f88ca2016-01-04 16:44:15 +1100657 struct xfs_icdinode *dic = &ip->i_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Dave Chinner58f88ca2016-01-04 16:44:15 +1100659 return _xfs_dic2xflags(dic->di_flags, dic->di_flags2, XFS_IFORK_Q(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662/*
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000663 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
664 * is allowed, otherwise it has to be an exact match. If a CI match is found,
665 * ci_name->name will point to a the actual name (caller must free) or
666 * will be set to NULL if an exact match is found.
667 */
668int
669xfs_lookup(
670 xfs_inode_t *dp,
671 struct xfs_name *name,
672 xfs_inode_t **ipp,
673 struct xfs_name *ci_name)
674{
675 xfs_ino_t inum;
676 int error;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000677
678 trace_xfs_lookup(dp, name);
679
680 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000681 return -EIO;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000682
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000683 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000684 if (error)
Dave Chinnerdbad7c92015-08-19 10:33:00 +1000685 goto out_unlock;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000686
687 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
688 if (error)
689 goto out_free_name;
690
691 return 0;
692
693out_free_name:
694 if (ci_name)
695 kmem_free(ci_name->name);
Dave Chinnerdbad7c92015-08-19 10:33:00 +1000696out_unlock:
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000697 *ipp = NULL;
698 return error;
699}
700
Darrick J. Wong8a569d72020-09-13 10:16:40 -0700701/* Propagate di_flags from a parent inode to a child inode. */
702static void
703xfs_inode_inherit_flags(
704 struct xfs_inode *ip,
705 const struct xfs_inode *pip)
706{
707 unsigned int di_flags = 0;
708 umode_t mode = VFS_I(ip)->i_mode;
709
710 if (S_ISDIR(mode)) {
711 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
712 di_flags |= XFS_DIFLAG_RTINHERIT;
713 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
714 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
715 ip->i_d.di_extsize = pip->i_d.di_extsize;
716 }
717 if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
718 di_flags |= XFS_DIFLAG_PROJINHERIT;
719 } else if (S_ISREG(mode)) {
Darrick J. Wongd4f2c142020-09-13 10:16:41 -0700720 if ((pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT) &&
721 xfs_sb_version_hasrealtime(&ip->i_mount->m_sb))
Darrick J. Wong8a569d72020-09-13 10:16:40 -0700722 di_flags |= XFS_DIFLAG_REALTIME;
723 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
724 di_flags |= XFS_DIFLAG_EXTSIZE;
725 ip->i_d.di_extsize = pip->i_d.di_extsize;
726 }
727 }
728 if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
729 xfs_inherit_noatime)
730 di_flags |= XFS_DIFLAG_NOATIME;
731 if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
732 xfs_inherit_nodump)
733 di_flags |= XFS_DIFLAG_NODUMP;
734 if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
735 xfs_inherit_sync)
736 di_flags |= XFS_DIFLAG_SYNC;
737 if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
738 xfs_inherit_nosymlinks)
739 di_flags |= XFS_DIFLAG_NOSYMLINKS;
740 if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
741 xfs_inherit_nodefrag)
742 di_flags |= XFS_DIFLAG_NODEFRAG;
743 if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
744 di_flags |= XFS_DIFLAG_FILESTREAM;
745
746 ip->i_d.di_flags |= di_flags;
747}
748
749/* Propagate di_flags2 from a parent inode to a child inode. */
750static void
751xfs_inode_inherit_flags2(
752 struct xfs_inode *ip,
753 const struct xfs_inode *pip)
754{
755 if (pip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) {
756 ip->i_d.di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
757 ip->i_d.di_cowextsize = pip->i_d.di_cowextsize;
758 }
759 if (pip->i_d.di_flags2 & XFS_DIFLAG2_DAX)
760 ip->i_d.di_flags2 |= XFS_DIFLAG2_DAX;
761}
762
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000763/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 * Allocate an inode on disk and return a copy of its in-core version.
765 * The in-core inode is locked exclusively. Set mode, nlink, and rdev
766 * appropriately within the inode. The uid and gid for the inode are
767 * set according to the contents of the given cred structure.
768 *
769 * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
Carlos Maiolinocd856db2012-10-20 11:08:19 -0300770 * has a free inode available, call xfs_iget() to obtain the in-core
771 * version of the allocated inode. Finally, fill in the inode and
772 * log its initial contents. In this case, ialloc_context would be
773 * set to NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 *
Carlos Maiolinocd856db2012-10-20 11:08:19 -0300775 * If xfs_dialloc() does not have an available inode, it will replenish
776 * its supply by doing an allocation. Since we can only do one
777 * allocation within a transaction without deadlocks, we must commit
778 * the current transaction before returning the inode itself.
779 * In this case, therefore, we will set ialloc_context and return.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 * The caller should then commit the current transaction, start a new
781 * transaction, and call xfs_ialloc() again to actually get the inode.
782 *
783 * To ensure that some other process does not grab the inode that
784 * was allocated during the first call to xfs_ialloc(), this routine
785 * also returns the [locked] bp pointing to the head of the freelist
786 * as ialloc_context. The caller should hold this buffer across
787 * the commit and pass it back into this routine on the second call.
David Chinnerb11f94d2007-07-11 11:09:33 +1000788 *
789 * If we are allocating quota inodes, we do not have a parent inode
790 * to attach to or associate with (i.e. pip == NULL) because they
791 * are not linked into the directory structure - they are attached
792 * directly to the superblock - and so have no parent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 */
Eric Sandeen0d5a75e2016-06-01 17:38:15 +1000794static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795xfs_ialloc(
796 xfs_trans_t *tp,
797 xfs_inode_t *pip,
Al Viro576b1d62011-07-26 02:50:15 -0400798 umode_t mode,
Nathan Scott31b084a2005-05-05 13:25:00 -0700799 xfs_nlink_t nlink,
Christoph Hellwig66f36462017-10-19 11:07:09 -0700800 dev_t rdev,
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000801 prid_t prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 xfs_buf_t **ialloc_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 xfs_inode_t **ipp)
804{
Christoph Hellwig93848a92013-04-03 16:11:17 +1100805 struct xfs_mount *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 xfs_ino_t ino;
807 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 uint flags;
809 int error;
Deepa Dinamani95582b02018-05-08 19:36:02 -0700810 struct timespec64 tv;
Dave Chinner39878482016-02-09 16:54:58 +1100811 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 /*
814 * Call the space management code to pick
815 * the on-disk inode to be allocated.
816 */
Christoph Hellwigf59cf5c2017-12-04 17:32:55 -0800817 error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode,
Christoph Hellwig08358902012-07-04 10:54:47 -0400818 ialloc_context, &ino);
David Chinnerbf904242008-10-30 17:36:14 +1100819 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return error;
Christoph Hellwig08358902012-07-04 10:54:47 -0400821 if (*ialloc_context || ino == NULLFSINO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 *ipp = NULL;
823 return 0;
824 }
825 ASSERT(*ialloc_context == NULL);
826
827 /*
Dave Chinner8b269842018-04-17 17:17:35 -0700828 * Protect against obviously corrupt allocation btree records. Later
829 * xfs_iget checks will catch re-allocation of other active in-memory
830 * and on-disk inodes. If we don't catch reallocating the parent inode
831 * here we will deadlock in xfs_iget() so we have to do these checks
832 * first.
833 */
834 if ((pip && ino == pip->i_ino) || !xfs_verify_dir_ino(mp, ino)) {
835 xfs_alert(mp, "Allocated a known in-use inode 0x%llx!", ino);
836 return -EFSCORRUPTED;
837 }
838
839 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 * Get the in-core inode with the lock held exclusively.
841 * This is because we're setting fields here we need
842 * to prevent others from looking at until we're done.
843 */
Christoph Hellwig93848a92013-04-03 16:11:17 +1100844 error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE,
Christoph Hellwigec3ba852011-02-13 13:26:42 +0000845 XFS_ILOCK_EXCL, &ip);
David Chinnerbf904242008-10-30 17:36:14 +1100846 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 ASSERT(ip != NULL);
Dave Chinner39878482016-02-09 16:54:58 +1100849 inode = VFS_I(ip);
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100850 inode->i_mode = mode;
Dave Chinner54d7b5c2016-02-09 16:54:58 +1100851 set_nlink(inode, nlink);
Christoph Hellwig3d8f2822020-02-21 08:31:26 -0800852 inode->i_uid = current_fsuid();
Christoph Hellwig66f36462017-10-19 11:07:09 -0700853 inode->i_rdev = rdev;
Christoph Hellwigde7a8662019-11-12 08:22:54 -0800854 ip->i_d.di_projid = prid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000856 if (pip && XFS_INHERIT_GID(pip)) {
Christoph Hellwig3d8f2822020-02-21 08:31:26 -0800857 inode->i_gid = VFS_I(pip)->i_gid;
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100858 if ((VFS_I(pip)->i_mode & S_ISGID) && S_ISDIR(mode))
859 inode->i_mode |= S_ISGID;
Christoph Hellwig3d8f2822020-02-21 08:31:26 -0800860 } else {
861 inode->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
863
864 /*
865 * If the group ID of the new file does not match the effective group
866 * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
867 * (and only if the irix_sgid_inherit compatibility variable is set).
868 */
Christoph Hellwig54295152020-02-21 08:31:27 -0800869 if (irix_sgid_inherit &&
870 (inode->i_mode & S_ISGID) && !in_group_p(inode->i_gid))
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100871 inode->i_mode &= ~S_ISGID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 ip->i_d.di_size = 0;
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700874 ip->i_df.if_nextents = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 ASSERT(ip->i_d.di_nblocks == 0);
Christoph Hellwigdff35fd2008-08-13 16:44:15 +1000876
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700877 tv = current_time(inode);
Dave Chinner39878482016-02-09 16:54:58 +1100878 inode->i_mtime = tv;
879 inode->i_atime = tv;
880 inode->i_ctime = tv;
Christoph Hellwigdff35fd2008-08-13 16:44:15 +1000881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 ip->i_d.di_extsize = 0;
883 ip->i_d.di_dmevmask = 0;
884 ip->i_d.di_dmstate = 0;
885 ip->i_d.di_flags = 0;
Christoph Hellwig93848a92013-04-03 16:11:17 +1100886
Christoph Hellwig6471e9c2020-03-18 08:15:11 -0700887 if (xfs_sb_version_has_v3inode(&mp->m_sb)) {
Jeff Laytonf0e28282017-12-11 06:35:19 -0500888 inode_set_iversion(inode, 1);
Darrick J. Wongf93e54362020-08-17 09:59:07 -0700889 ip->i_d.di_flags2 = mp->m_ino_geo.new_diflags2;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700890 ip->i_d.di_cowextsize = 0;
Christoph Hellwig8d2d8782019-11-12 08:20:42 -0800891 ip->i_d.di_crtime = tv;
Christoph Hellwig93848a92013-04-03 16:11:17 +1100892 }
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 flags = XFS_ILOG_CORE;
895 switch (mode & S_IFMT) {
896 case S_IFIFO:
897 case S_IFCHR:
898 case S_IFBLK:
899 case S_IFSOCK:
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700900 ip->i_df.if_format = XFS_DINODE_FMT_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 ip->i_df.if_flags = 0;
902 flags |= XFS_ILOG_DEV;
903 break;
904 case S_IFREG:
905 case S_IFDIR:
Darrick J. Wong8a569d72020-09-13 10:16:40 -0700906 if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY))
907 xfs_inode_inherit_flags(ip, pip);
908 if (pip && (pip->i_d.di_flags2 & XFS_DIFLAG2_ANY))
909 xfs_inode_inherit_flags2(ip, pip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 /* FALLTHROUGH */
911 case S_IFLNK:
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700912 ip->i_df.if_format = XFS_DINODE_FMT_EXTENTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 ip->i_df.if_flags = XFS_IFEXTENTS;
Christoph Hellwigfcacbc32018-07-17 16:51:50 -0700914 ip->i_df.if_bytes = 0;
Christoph Hellwig6bdcf262017-11-03 10:34:46 -0700915 ip->i_df.if_u1.if_root = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 break;
917 default:
918 ASSERT(0);
919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 /*
922 * Log the new values stuffed into the inode.
923 */
Christoph Hellwigddc34152011-09-19 15:00:54 +0000924 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 xfs_trans_log_inode(tp, ip, flags);
926
Dave Chinner58c90472015-02-23 22:38:08 +1100927 /* now that we have an i_mode we can setup the inode structure */
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000928 xfs_setup_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 *ipp = ip;
931 return 0;
932}
933
Dave Chinnere546cb72013-08-12 20:49:47 +1000934/*
935 * Allocates a new inode from disk and return a pointer to the
936 * incore copy. This routine will internally commit the current
937 * transaction and allocate a new one if the Space Manager needed
938 * to do an allocation to replenish the inode free-list.
939 *
940 * This routine is designed to be called from xfs_create and
941 * xfs_create_dir.
942 *
943 */
944int
945xfs_dir_ialloc(
946 xfs_trans_t **tpp, /* input: current transaction;
947 output: may be a new transaction. */
948 xfs_inode_t *dp, /* directory within whose allocate
949 the inode. */
950 umode_t mode,
951 xfs_nlink_t nlink,
Christoph Hellwig66f36462017-10-19 11:07:09 -0700952 dev_t rdev,
Dave Chinnere546cb72013-08-12 20:49:47 +1000953 prid_t prid, /* project id */
Chandan Rajendrac9590252018-04-02 15:47:43 -0700954 xfs_inode_t **ipp) /* pointer to inode; it will be
Dave Chinnere546cb72013-08-12 20:49:47 +1000955 locked. */
Dave Chinnere546cb72013-08-12 20:49:47 +1000956{
957 xfs_trans_t *tp;
Dave Chinnere546cb72013-08-12 20:49:47 +1000958 xfs_inode_t *ip;
959 xfs_buf_t *ialloc_context = NULL;
960 int code;
Dave Chinnere546cb72013-08-12 20:49:47 +1000961 void *dqinfo;
962 uint tflags;
963
964 tp = *tpp;
965 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
966
967 /*
968 * xfs_ialloc will return a pointer to an incore inode if
969 * the Space Manager has an available inode on the free
970 * list. Otherwise, it will do an allocation and replenish
971 * the freelist. Since we can only do one allocation per
972 * transaction without deadlocks, we will need to commit the
973 * current transaction and start a new one. We will then
974 * need to call xfs_ialloc again to get the inode.
975 *
976 * If xfs_ialloc did an allocation to replenish the freelist,
977 * it returns the bp containing the head of the freelist as
978 * ialloc_context. We will hold a lock on it across the
979 * transaction commit so that no other process can steal
980 * the inode(s) that we've just allocated.
981 */
Christoph Hellwigf59cf5c2017-12-04 17:32:55 -0800982 code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid, &ialloc_context,
983 &ip);
Dave Chinnere546cb72013-08-12 20:49:47 +1000984
985 /*
986 * Return an error if we were unable to allocate a new inode.
987 * This should only happen if we run out of space on disk or
988 * encounter a disk error.
989 */
990 if (code) {
991 *ipp = NULL;
992 return code;
993 }
994 if (!ialloc_context && !ip) {
995 *ipp = NULL;
Dave Chinner24513372014-06-25 14:58:08 +1000996 return -ENOSPC;
Dave Chinnere546cb72013-08-12 20:49:47 +1000997 }
998
999 /*
1000 * If the AGI buffer is non-NULL, then we were unable to get an
1001 * inode in one operation. We need to commit the current
1002 * transaction and call xfs_ialloc() again. It is guaranteed
1003 * to succeed the second time.
1004 */
1005 if (ialloc_context) {
1006 /*
1007 * Normally, xfs_trans_commit releases all the locks.
1008 * We call bhold to hang on to the ialloc_context across
1009 * the commit. Holding this buffer prevents any other
1010 * processes from doing any allocations in this
1011 * allocation group.
1012 */
1013 xfs_trans_bhold(tp, ialloc_context);
Dave Chinnere546cb72013-08-12 20:49:47 +10001014
1015 /*
1016 * We want the quota changes to be associated with the next
1017 * transaction, NOT this one. So, detach the dqinfo from this
1018 * and attach it to the next transaction.
1019 */
1020 dqinfo = NULL;
1021 tflags = 0;
1022 if (tp->t_dqinfo) {
1023 dqinfo = (void *)tp->t_dqinfo;
1024 tp->t_dqinfo = NULL;
1025 tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY;
1026 tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY);
1027 }
1028
Christoph Hellwig411350d2017-08-28 10:21:03 -07001029 code = xfs_trans_roll(&tp);
Jie Liu3d3c8b52013-08-12 20:49:59 +10001030
Dave Chinnere546cb72013-08-12 20:49:47 +10001031 /*
1032 * Re-attach the quota info that we detached from prev trx.
1033 */
1034 if (dqinfo) {
1035 tp->t_dqinfo = dqinfo;
1036 tp->t_flags |= tflags;
1037 }
1038
1039 if (code) {
1040 xfs_buf_relse(ialloc_context);
Christoph Hellwig2e6db6c2015-06-04 13:47:29 +10001041 *tpp = tp;
Dave Chinnere546cb72013-08-12 20:49:47 +10001042 *ipp = NULL;
1043 return code;
1044 }
1045 xfs_trans_bjoin(tp, ialloc_context);
1046
1047 /*
1048 * Call ialloc again. Since we've locked out all
1049 * other allocations in this allocation group,
1050 * this call should always succeed.
1051 */
1052 code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid,
Christoph Hellwigf59cf5c2017-12-04 17:32:55 -08001053 &ialloc_context, &ip);
Dave Chinnere546cb72013-08-12 20:49:47 +10001054
1055 /*
1056 * If we get an error at this point, return to the caller
1057 * so that the current transaction can be aborted.
1058 */
1059 if (code) {
1060 *tpp = tp;
1061 *ipp = NULL;
1062 return code;
1063 }
1064 ASSERT(!ialloc_context && ip);
1065
Dave Chinnere546cb72013-08-12 20:49:47 +10001066 }
1067
1068 *ipp = ip;
1069 *tpp = tp;
1070
1071 return 0;
1072}
1073
1074/*
Dave Chinner54d7b5c2016-02-09 16:54:58 +11001075 * Decrement the link count on an inode & log the change. If this causes the
1076 * link count to go to zero, move the inode to AGI unlinked list so that it can
1077 * be freed when the last active reference goes away via xfs_inactive().
Dave Chinnere546cb72013-08-12 20:49:47 +10001078 */
Eric Sandeen0d5a75e2016-06-01 17:38:15 +10001079static int /* error */
Dave Chinnere546cb72013-08-12 20:49:47 +10001080xfs_droplink(
1081 xfs_trans_t *tp,
1082 xfs_inode_t *ip)
1083{
Dave Chinnere546cb72013-08-12 20:49:47 +10001084 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1085
Dave Chinnere546cb72013-08-12 20:49:47 +10001086 drop_nlink(VFS_I(ip));
1087 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1088
Dave Chinner54d7b5c2016-02-09 16:54:58 +11001089 if (VFS_I(ip)->i_nlink)
1090 return 0;
1091
1092 return xfs_iunlink(tp, ip);
Dave Chinnere546cb72013-08-12 20:49:47 +10001093}
1094
1095/*
Dave Chinnere546cb72013-08-12 20:49:47 +10001096 * Increment the link count on an inode & log the change.
1097 */
Eric Sandeen91083262019-05-01 20:26:30 -07001098static void
Dave Chinnere546cb72013-08-12 20:49:47 +10001099xfs_bumplink(
1100 xfs_trans_t *tp,
1101 xfs_inode_t *ip)
1102{
1103 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1104
Dave Chinnere546cb72013-08-12 20:49:47 +10001105 inc_nlink(VFS_I(ip));
Dave Chinnere546cb72013-08-12 20:49:47 +10001106 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Dave Chinnere546cb72013-08-12 20:49:47 +10001107}
1108
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001109int
1110xfs_create(
1111 xfs_inode_t *dp,
1112 struct xfs_name *name,
1113 umode_t mode,
Christoph Hellwig66f36462017-10-19 11:07:09 -07001114 dev_t rdev,
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001115 xfs_inode_t **ipp)
1116{
1117 int is_dir = S_ISDIR(mode);
1118 struct xfs_mount *mp = dp->i_mount;
1119 struct xfs_inode *ip = NULL;
1120 struct xfs_trans *tp = NULL;
1121 int error;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001122 bool unlock_dp_on_error = false;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001123 prid_t prid;
1124 struct xfs_dquot *udqp = NULL;
1125 struct xfs_dquot *gdqp = NULL;
1126 struct xfs_dquot *pdqp = NULL;
Brian Foster062647a2014-11-28 14:00:16 +11001127 struct xfs_trans_res *tres;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001128 uint resblks;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001129
1130 trace_xfs_create(dp, name);
1131
1132 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10001133 return -EIO;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001134
Zhi Yong Wu163467d2013-12-18 08:22:39 +08001135 prid = xfs_get_initial_prid(dp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001136
1137 /*
1138 * Make sure that we have allocated dquot(s) on disk.
1139 */
Christoph Hellwig54295152020-02-21 08:31:27 -08001140 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001141 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
1142 &udqp, &gdqp, &pdqp);
1143 if (error)
1144 return error;
1145
1146 if (is_dir) {
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001147 resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
Brian Foster062647a2014-11-28 14:00:16 +11001148 tres = &M_RES(mp)->tr_mkdir;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001149 } else {
1150 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
Brian Foster062647a2014-11-28 14:00:16 +11001151 tres = &M_RES(mp)->tr_create;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001152 }
1153
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001154 /*
1155 * Initially assume that the file does not exist and
1156 * reserve the resources for that case. If that is not
1157 * the case we'll drop the one we have and get a more
1158 * appropriate transaction later.
1159 */
Christoph Hellwig253f4912016-04-06 09:19:55 +10001160 error = xfs_trans_alloc(mp, tres, resblks, 0, 0, &tp);
Dave Chinner24513372014-06-25 14:58:08 +10001161 if (error == -ENOSPC) {
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001162 /* flush outstanding delalloc blocks and retry */
1163 xfs_flush_inodes(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001164 error = xfs_trans_alloc(mp, tres, resblks, 0, 0, &tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001165 }
Christoph Hellwig4906e212015-06-04 13:47:56 +10001166 if (error)
Christoph Hellwig253f4912016-04-06 09:19:55 +10001167 goto out_release_inode;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001168
Christoph Hellwig65523212016-11-30 14:33:25 +11001169 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001170 unlock_dp_on_error = true;
1171
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001172 /*
1173 * Reserve disk quota and the inode.
1174 */
1175 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
1176 pdqp, resblks, 1, 0);
1177 if (error)
1178 goto out_trans_cancel;
1179
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001180 /*
1181 * A newly created regular or special file just has one directory
1182 * entry pointing to them, but a directory also the "." entry
1183 * pointing to itself.
1184 */
Chandan Rajendrac9590252018-04-02 15:47:43 -07001185 error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev, prid, &ip);
Jan Karad6077aa2015-07-29 11:52:08 +10001186 if (error)
Christoph Hellwig4906e212015-06-04 13:47:56 +10001187 goto out_trans_cancel;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001188
1189 /*
1190 * Now we join the directory inode to the transaction. We do not do it
1191 * earlier because xfs_dir_ialloc might commit the previous transaction
1192 * (and release all the locks). An error from here on will result in
1193 * the transaction cancel unlocking dp so don't do it explicitly in the
1194 * error path.
1195 */
Christoph Hellwig65523212016-11-30 14:33:25 +11001196 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001197 unlock_dp_on_error = false;
1198
Brian Foster381eee62018-07-11 22:26:21 -07001199 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
Kaixu Xia63337b62020-03-27 08:28:39 -07001200 resblks - XFS_IALLOC_SPACE_RES(mp));
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001201 if (error) {
Dave Chinner24513372014-06-25 14:58:08 +10001202 ASSERT(error != -ENOSPC);
Christoph Hellwig4906e212015-06-04 13:47:56 +10001203 goto out_trans_cancel;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001204 }
1205 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1206 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1207
1208 if (is_dir) {
1209 error = xfs_dir_init(tp, ip, dp);
1210 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -07001211 goto out_trans_cancel;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001212
Eric Sandeen91083262019-05-01 20:26:30 -07001213 xfs_bumplink(tp, dp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001214 }
1215
1216 /*
1217 * If this is a synchronous mount, make sure that the
1218 * create transaction goes to disk before returning to
1219 * the user.
1220 */
1221 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1222 xfs_trans_set_sync(tp);
1223
1224 /*
1225 * Attach the dquot(s) to the inodes and modify them incore.
1226 * These ids of the inode couldn't have changed since the new
1227 * inode has been locked ever since it was created.
1228 */
1229 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
1230
Christoph Hellwig70393312015-06-04 13:48:08 +10001231 error = xfs_trans_commit(tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001232 if (error)
1233 goto out_release_inode;
1234
1235 xfs_qm_dqrele(udqp);
1236 xfs_qm_dqrele(gdqp);
1237 xfs_qm_dqrele(pdqp);
1238
1239 *ipp = ip;
1240 return 0;
1241
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001242 out_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001243 xfs_trans_cancel(tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001244 out_release_inode:
1245 /*
Dave Chinner58c90472015-02-23 22:38:08 +11001246 * Wait until after the current transaction is aborted to finish the
1247 * setup of the inode and release the inode. This prevents recursive
1248 * transactions and deadlocks from xfs_inactive.
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001249 */
Dave Chinner58c90472015-02-23 22:38:08 +11001250 if (ip) {
1251 xfs_finish_inode_setup(ip);
Darrick J. Wong44a87362018-07-25 12:52:32 -07001252 xfs_irele(ip);
Dave Chinner58c90472015-02-23 22:38:08 +11001253 }
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001254
1255 xfs_qm_dqrele(udqp);
1256 xfs_qm_dqrele(gdqp);
1257 xfs_qm_dqrele(pdqp);
1258
1259 if (unlock_dp_on_error)
Christoph Hellwig65523212016-11-30 14:33:25 +11001260 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001261 return error;
1262}
1263
1264int
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001265xfs_create_tmpfile(
1266 struct xfs_inode *dp,
Brian Foster330033d2014-04-17 08:15:30 +10001267 umode_t mode,
1268 struct xfs_inode **ipp)
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001269{
1270 struct xfs_mount *mp = dp->i_mount;
1271 struct xfs_inode *ip = NULL;
1272 struct xfs_trans *tp = NULL;
1273 int error;
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001274 prid_t prid;
1275 struct xfs_dquot *udqp = NULL;
1276 struct xfs_dquot *gdqp = NULL;
1277 struct xfs_dquot *pdqp = NULL;
1278 struct xfs_trans_res *tres;
1279 uint resblks;
1280
1281 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10001282 return -EIO;
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001283
1284 prid = xfs_get_initial_prid(dp);
1285
1286 /*
1287 * Make sure that we have allocated dquot(s) on disk.
1288 */
Christoph Hellwig54295152020-02-21 08:31:27 -08001289 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001290 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
1291 &udqp, &gdqp, &pdqp);
1292 if (error)
1293 return error;
1294
1295 resblks = XFS_IALLOC_SPACE_RES(mp);
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001296 tres = &M_RES(mp)->tr_create_tmpfile;
Christoph Hellwig253f4912016-04-06 09:19:55 +10001297
1298 error = xfs_trans_alloc(mp, tres, resblks, 0, 0, &tp);
Christoph Hellwig4906e212015-06-04 13:47:56 +10001299 if (error)
Christoph Hellwig253f4912016-04-06 09:19:55 +10001300 goto out_release_inode;
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001301
1302 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
1303 pdqp, resblks, 1, 0);
1304 if (error)
1305 goto out_trans_cancel;
1306
Darrick J. Wongc4a6bf72019-02-13 11:15:17 -08001307 error = xfs_dir_ialloc(&tp, dp, mode, 0, 0, prid, &ip);
Jan Karad6077aa2015-07-29 11:52:08 +10001308 if (error)
Christoph Hellwig4906e212015-06-04 13:47:56 +10001309 goto out_trans_cancel;
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001310
1311 if (mp->m_flags & XFS_MOUNT_WSYNC)
1312 xfs_trans_set_sync(tp);
1313
1314 /*
1315 * Attach the dquot(s) to the inodes and modify them incore.
1316 * These ids of the inode couldn't have changed since the new
1317 * inode has been locked ever since it was created.
1318 */
1319 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
1320
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001321 error = xfs_iunlink(tp, ip);
1322 if (error)
Christoph Hellwig4906e212015-06-04 13:47:56 +10001323 goto out_trans_cancel;
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001324
Christoph Hellwig70393312015-06-04 13:48:08 +10001325 error = xfs_trans_commit(tp);
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001326 if (error)
1327 goto out_release_inode;
1328
1329 xfs_qm_dqrele(udqp);
1330 xfs_qm_dqrele(gdqp);
1331 xfs_qm_dqrele(pdqp);
1332
Brian Foster330033d2014-04-17 08:15:30 +10001333 *ipp = ip;
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001334 return 0;
1335
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001336 out_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001337 xfs_trans_cancel(tp);
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001338 out_release_inode:
1339 /*
Dave Chinner58c90472015-02-23 22:38:08 +11001340 * Wait until after the current transaction is aborted to finish the
1341 * setup of the inode and release the inode. This prevents recursive
1342 * transactions and deadlocks from xfs_inactive.
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001343 */
Dave Chinner58c90472015-02-23 22:38:08 +11001344 if (ip) {
1345 xfs_finish_inode_setup(ip);
Darrick J. Wong44a87362018-07-25 12:52:32 -07001346 xfs_irele(ip);
Dave Chinner58c90472015-02-23 22:38:08 +11001347 }
Zhi Yong Wu99b64362013-12-18 08:22:40 +08001348
1349 xfs_qm_dqrele(udqp);
1350 xfs_qm_dqrele(gdqp);
1351 xfs_qm_dqrele(pdqp);
1352
1353 return error;
1354}
1355
1356int
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001357xfs_link(
1358 xfs_inode_t *tdp,
1359 xfs_inode_t *sip,
1360 struct xfs_name *target_name)
1361{
1362 xfs_mount_t *mp = tdp->i_mount;
1363 xfs_trans_t *tp;
1364 int error;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001365 int resblks;
1366
1367 trace_xfs_link(tdp, target_name);
1368
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001369 ASSERT(!S_ISDIR(VFS_I(sip)->i_mode));
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001370
1371 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10001372 return -EIO;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001373
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -07001374 error = xfs_qm_dqattach(sip);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001375 if (error)
1376 goto std_return;
1377
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -07001378 error = xfs_qm_dqattach(tdp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001379 if (error)
1380 goto std_return;
1381
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001382 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001383 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_link, resblks, 0, 0, &tp);
Dave Chinner24513372014-06-25 14:58:08 +10001384 if (error == -ENOSPC) {
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001385 resblks = 0;
Christoph Hellwig253f4912016-04-06 09:19:55 +10001386 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_link, 0, 0, 0, &tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001387 }
Christoph Hellwig4906e212015-06-04 13:47:56 +10001388 if (error)
Christoph Hellwig253f4912016-04-06 09:19:55 +10001389 goto std_return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001390
Darrick J. Wong7c2d2382018-01-26 15:27:33 -08001391 xfs_lock_two_inodes(sip, XFS_ILOCK_EXCL, tdp, XFS_ILOCK_EXCL);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001392
1393 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
Christoph Hellwig65523212016-11-30 14:33:25 +11001394 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001395
1396 /*
1397 * If we are using project inheritance, we only allow hard link
1398 * creation in our tree when the project IDs are the same; else
1399 * the tree quota mechanism could be circumvented.
1400 */
1401 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
Christoph Hellwigde7a8662019-11-12 08:22:54 -08001402 tdp->i_d.di_projid != sip->i_d.di_projid)) {
Dave Chinner24513372014-06-25 14:58:08 +10001403 error = -EXDEV;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001404 goto error_return;
1405 }
1406
Eric Sandeen94f3cad2014-09-09 11:57:52 +10001407 if (!resblks) {
1408 error = xfs_dir_canenter(tp, tdp, target_name);
1409 if (error)
1410 goto error_return;
1411 }
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001412
Dave Chinner54d7b5c2016-02-09 16:54:58 +11001413 /*
1414 * Handle initial link state of O_TMPFILE inode
1415 */
1416 if (VFS_I(sip)->i_nlink == 0) {
Zhi Yong Wuab297432013-12-18 08:22:41 +08001417 error = xfs_iunlink_remove(tp, sip);
1418 if (error)
Christoph Hellwig4906e212015-06-04 13:47:56 +10001419 goto error_return;
Zhi Yong Wuab297432013-12-18 08:22:41 +08001420 }
1421
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001422 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
Brian Foster381eee62018-07-11 22:26:21 -07001423 resblks);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001424 if (error)
Christoph Hellwig4906e212015-06-04 13:47:56 +10001425 goto error_return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001426 xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1427 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
1428
Eric Sandeen91083262019-05-01 20:26:30 -07001429 xfs_bumplink(tp, sip);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001430
1431 /*
1432 * If this is a synchronous mount, make sure that the
1433 * link transaction goes to disk before returning to
1434 * the user.
1435 */
Eric Sandeenf6106ef2016-01-11 11:34:01 +11001436 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001437 xfs_trans_set_sync(tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001438
Christoph Hellwig70393312015-06-04 13:48:08 +10001439 return xfs_trans_commit(tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001440
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001441 error_return:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001442 xfs_trans_cancel(tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001443 std_return:
1444 return error;
1445}
1446
Darrick J. Wong363e59b2017-12-14 15:42:59 -08001447/* Clear the reflink flag and the cowblocks tag if possible. */
1448static void
1449xfs_itruncate_clear_reflink_flags(
1450 struct xfs_inode *ip)
1451{
1452 struct xfs_ifork *dfork;
1453 struct xfs_ifork *cfork;
1454
1455 if (!xfs_is_reflink_inode(ip))
1456 return;
1457 dfork = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1458 cfork = XFS_IFORK_PTR(ip, XFS_COW_FORK);
1459 if (dfork->if_bytes == 0 && cfork->if_bytes == 0)
1460 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
1461 if (cfork->if_bytes == 0)
1462 xfs_inode_clear_cowblocks_tag(ip);
1463}
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465/*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001466 * Free up the underlying blocks past new_size. The new size must be smaller
1467 * than the current size. This routine can be used both for the attribute and
1468 * data fork, and does not modify the inode size, which is left to the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 *
David Chinnerf6485052008-04-17 16:50:04 +10001470 * The transaction passed to this routine must have made a permanent log
1471 * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the
1472 * given transaction and start new ones, so make sure everything involved in
1473 * the transaction is tidy before calling here. Some transaction will be
1474 * returned to the caller to be committed. The incoming transaction must
1475 * already include the inode, and both inode locks must be held exclusively.
1476 * The inode must also be "held" within the transaction. On return the inode
1477 * will be "held" within the returned transaction. This routine does NOT
1478 * require any disk space to be reserved for it within the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 *
David Chinnerf6485052008-04-17 16:50:04 +10001480 * If we get an error, we must return with the inode locked and linked into the
1481 * current transaction. This keeps things simple for the higher level code,
1482 * because it always knows that the inode is locked and held in the transaction
1483 * that returns to it whether errors occur or not. We don't mark the inode
1484 * dirty on error so that transactions can be easily aborted if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 */
1486int
Brian Foster4e529332018-05-10 09:35:42 -07001487xfs_itruncate_extents_flags(
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001488 struct xfs_trans **tpp,
1489 struct xfs_inode *ip,
1490 int whichfork,
Brian Foster13b86fc2018-05-09 08:45:04 -07001491 xfs_fsize_t new_size,
Brian Foster4e529332018-05-10 09:35:42 -07001492 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001494 struct xfs_mount *mp = ip->i_mount;
1495 struct xfs_trans *tp = *tpp;
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001496 xfs_fileoff_t first_unmap_block;
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001497 xfs_filblks_t unmap_len;
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001498 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Christoph Hellwig0b561852012-07-04 11:13:31 -04001500 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1501 ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
1502 xfs_isilocked(ip, XFS_IOLOCK_EXCL));
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00001503 ASSERT(new_size <= XFS_ISIZE(ip));
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001504 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 ASSERT(ip->i_itemp != NULL);
Christoph Hellwig898621d2010-06-24 11:36:58 +10001506 ASSERT(ip->i_itemp->ili_lock_flags == 0);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001507 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Christoph Hellwig673e8e52011-12-18 20:00:04 +00001509 trace_xfs_itruncate_extents_start(ip, new_size);
1510
Brian Foster4e529332018-05-10 09:35:42 -07001511 flags |= xfs_bmapi_aflag(whichfork);
Brian Foster13b86fc2018-05-09 08:45:04 -07001512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 /*
1514 * Since it is possible for space to become allocated beyond
1515 * the end of the file (in a crash where the space is allocated
1516 * but the inode size is not yet updated), simply remove any
1517 * blocks which show up between the new EOF and the maximum
Darrick J. Wong4bbb04a2020-01-02 13:20:13 -08001518 * possible file size.
1519 *
1520 * We have to free all the blocks to the bmbt maximum offset, even if
1521 * the page cache can't scale that far.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001523 first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
Darrick J. Wong4bbb04a2020-01-02 13:20:13 -08001524 if (first_unmap_block >= XFS_MAX_FILEOFF) {
1525 WARN_ON_ONCE(first_unmap_block > XFS_MAX_FILEOFF);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001526 return 0;
Darrick J. Wong4bbb04a2020-01-02 13:20:13 -08001527 }
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001528
Darrick J. Wong4bbb04a2020-01-02 13:20:13 -08001529 unmap_len = XFS_MAX_FILEOFF - first_unmap_block + 1;
1530 while (unmap_len > 0) {
Brian Foster02dff7b2018-07-24 13:43:07 -07001531 ASSERT(tp->t_firstblock == NULLFSBLOCK);
Darrick J. Wong4bbb04a2020-01-02 13:20:13 -08001532 error = __xfs_bunmapi(tp, ip, first_unmap_block, &unmap_len,
1533 flags, XFS_ITRUNC_MAX_EXTENTS);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001534 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +10001535 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Brian Foster6dd379c2020-09-15 20:44:46 -07001537 /* free the just unmapped extents */
Brian Foster9e28a242018-07-24 13:43:15 -07001538 error = xfs_defer_finish(&tp);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001539 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -07001540 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001542
Darrick J. Wong4919d422018-04-10 08:28:33 -07001543 if (whichfork == XFS_DATA_FORK) {
1544 /* Remove all pending CoW reservations. */
1545 error = xfs_reflink_cancel_cow_blocks(ip, &tp,
Darrick J. Wong4bbb04a2020-01-02 13:20:13 -08001546 first_unmap_block, XFS_MAX_FILEOFF, true);
Darrick J. Wong4919d422018-04-10 08:28:33 -07001547 if (error)
1548 goto out;
Darrick J. Wongaa8968f2016-10-03 09:11:38 -07001549
Darrick J. Wong4919d422018-04-10 08:28:33 -07001550 xfs_itruncate_clear_reflink_flags(ip);
1551 }
Darrick J. Wongaa8968f2016-10-03 09:11:38 -07001552
Christoph Hellwig673e8e52011-12-18 20:00:04 +00001553 /*
1554 * Always re-log the inode so that our permanent transaction can keep
1555 * on rolling it forward in the log.
1556 */
1557 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1558
1559 trace_xfs_itruncate_extents_end(ip, new_size);
1560
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001561out:
1562 *tpp = tp;
1563 return error;
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001564}
1565
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001566int
1567xfs_release(
1568 xfs_inode_t *ip)
1569{
1570 xfs_mount_t *mp = ip->i_mount;
1571 int error;
1572
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001573 if (!S_ISREG(VFS_I(ip)->i_mode) || (VFS_I(ip)->i_mode == 0))
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001574 return 0;
1575
1576 /* If this is a read-only mount, don't do this (would generate I/O) */
1577 if (mp->m_flags & XFS_MOUNT_RDONLY)
1578 return 0;
1579
1580 if (!XFS_FORCED_SHUTDOWN(mp)) {
1581 int truncated;
1582
1583 /*
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001584 * If we previously truncated this file and removed old data
1585 * in the process, we want to initiate "early" writeout on
1586 * the last close. This is an attempt to combat the notorious
1587 * NULL files problem which is particularly noticeable from a
1588 * truncate down, buffered (re-)write (delalloc), followed by
1589 * a crash. What we are effectively doing here is
1590 * significantly reducing the time window where we'd otherwise
1591 * be exposed to that problem.
1592 */
1593 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
1594 if (truncated) {
1595 xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
Dave Chinnereac152b2014-08-04 13:22:49 +10001596 if (ip->i_delayed_blks > 0) {
Dave Chinner24513372014-06-25 14:58:08 +10001597 error = filemap_flush(VFS_I(ip)->i_mapping);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001598 if (error)
1599 return error;
1600 }
1601 }
1602 }
1603
Dave Chinner54d7b5c2016-02-09 16:54:58 +11001604 if (VFS_I(ip)->i_nlink == 0)
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001605 return 0;
1606
1607 if (xfs_can_free_eofblocks(ip, false)) {
1608
1609 /*
Brian Fostera36b9262017-01-27 23:22:55 -08001610 * Check if the inode is being opened, written and closed
1611 * frequently and we have delayed allocation blocks outstanding
1612 * (e.g. streaming writes from the NFS server), truncating the
1613 * blocks past EOF will cause fragmentation to occur.
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001614 *
Brian Fostera36b9262017-01-27 23:22:55 -08001615 * In this case don't do the truncation, but we have to be
1616 * careful how we detect this case. Blocks beyond EOF show up as
1617 * i_delayed_blks even when the inode is clean, so we need to
1618 * truncate them away first before checking for a dirty release.
1619 * Hence on the first dirty close we will still remove the
1620 * speculative allocation, but after that we will leave it in
1621 * place.
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001622 */
1623 if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
1624 return 0;
Brian Fostera36b9262017-01-27 23:22:55 -08001625 /*
1626 * If we can't get the iolock just skip truncating the blocks
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001627 * past EOF because we could deadlock with the mmap_lock
Brian Fostera36b9262017-01-27 23:22:55 -08001628 * otherwise. We'll get another chance to drop them once the
1629 * last reference to the inode is dropped, so we'll never leak
1630 * blocks permanently.
1631 */
1632 if (xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
1633 error = xfs_free_eofblocks(ip);
1634 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1635 if (error)
1636 return error;
1637 }
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001638
1639 /* delalloc blocks after truncation means it really is dirty */
1640 if (ip->i_delayed_blks)
1641 xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
1642 }
1643 return 0;
1644}
1645
1646/*
Brian Fosterf7be2d72013-09-20 11:06:10 -04001647 * xfs_inactive_truncate
1648 *
1649 * Called to perform a truncate when an inode becomes unlinked.
1650 */
1651STATIC int
1652xfs_inactive_truncate(
1653 struct xfs_inode *ip)
1654{
1655 struct xfs_mount *mp = ip->i_mount;
1656 struct xfs_trans *tp;
1657 int error;
1658
Christoph Hellwig253f4912016-04-06 09:19:55 +10001659 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
Brian Fosterf7be2d72013-09-20 11:06:10 -04001660 if (error) {
1661 ASSERT(XFS_FORCED_SHUTDOWN(mp));
Brian Fosterf7be2d72013-09-20 11:06:10 -04001662 return error;
1663 }
Brian Fosterf7be2d72013-09-20 11:06:10 -04001664 xfs_ilock(ip, XFS_ILOCK_EXCL);
1665 xfs_trans_ijoin(tp, ip, 0);
1666
1667 /*
1668 * Log the inode size first to prevent stale data exposure in the event
1669 * of a system crash before the truncate completes. See the related
Jan Kara69bca802016-05-26 14:46:43 +02001670 * comment in xfs_vn_setattr_size() for details.
Brian Fosterf7be2d72013-09-20 11:06:10 -04001671 */
1672 ip->i_d.di_size = 0;
1673 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1674
1675 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
1676 if (error)
1677 goto error_trans_cancel;
1678
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001679 ASSERT(ip->i_df.if_nextents == 0);
Brian Fosterf7be2d72013-09-20 11:06:10 -04001680
Christoph Hellwig70393312015-06-04 13:48:08 +10001681 error = xfs_trans_commit(tp);
Brian Fosterf7be2d72013-09-20 11:06:10 -04001682 if (error)
1683 goto error_unlock;
1684
1685 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1686 return 0;
1687
1688error_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001689 xfs_trans_cancel(tp);
Brian Fosterf7be2d72013-09-20 11:06:10 -04001690error_unlock:
1691 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1692 return error;
1693}
1694
1695/*
Brian Foster88877d22013-09-20 11:06:11 -04001696 * xfs_inactive_ifree()
1697 *
1698 * Perform the inode free when an inode is unlinked.
1699 */
1700STATIC int
1701xfs_inactive_ifree(
1702 struct xfs_inode *ip)
1703{
Brian Foster88877d22013-09-20 11:06:11 -04001704 struct xfs_mount *mp = ip->i_mount;
1705 struct xfs_trans *tp;
1706 int error;
1707
Brian Foster9d43b182014-04-24 16:00:52 +10001708 /*
Christoph Hellwig76d771b2017-01-25 07:49:35 -08001709 * We try to use a per-AG reservation for any block needed by the finobt
1710 * tree, but as the finobt feature predates the per-AG reservation
1711 * support a degraded file system might not have enough space for the
1712 * reservation at mount time. In that case try to dip into the reserved
1713 * pool and pray.
Brian Foster9d43b182014-04-24 16:00:52 +10001714 *
1715 * Send a warning if the reservation does happen to fail, as the inode
1716 * now remains allocated and sits on the unlinked list until the fs is
1717 * repaired.
1718 */
Darrick J. Wonge1f6ca12019-02-14 09:33:15 -08001719 if (unlikely(mp->m_finobt_nores)) {
Christoph Hellwig76d771b2017-01-25 07:49:35 -08001720 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ifree,
1721 XFS_IFREE_SPACE_RES(mp), 0, XFS_TRANS_RESERVE,
1722 &tp);
1723 } else {
1724 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ifree, 0, 0, 0, &tp);
1725 }
Brian Foster88877d22013-09-20 11:06:11 -04001726 if (error) {
Dave Chinner24513372014-06-25 14:58:08 +10001727 if (error == -ENOSPC) {
Brian Foster9d43b182014-04-24 16:00:52 +10001728 xfs_warn_ratelimited(mp,
1729 "Failed to remove inode(s) from unlinked list. "
1730 "Please free space, unmount and run xfs_repair.");
1731 } else {
1732 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1733 }
Brian Foster88877d22013-09-20 11:06:11 -04001734 return error;
1735 }
1736
Dave Chinner96355d5a2020-06-29 14:48:45 -07001737 /*
1738 * We do not hold the inode locked across the entire rolling transaction
1739 * here. We only need to hold it for the first transaction that
1740 * xfs_ifree() builds, which may mark the inode XFS_ISTALE if the
1741 * underlying cluster buffer is freed. Relogging an XFS_ISTALE inode
1742 * here breaks the relationship between cluster buffer invalidation and
1743 * stale inode invalidation on cluster buffer item journal commit
1744 * completion, and can result in leaving dirty stale inodes hanging
1745 * around in memory.
1746 *
1747 * We have no need for serialising this inode operation against other
1748 * operations - we freed the inode and hence reallocation is required
1749 * and that will serialise on reallocating the space the deferops need
1750 * to free. Hence we can unlock the inode on the first commit of
1751 * the transaction rather than roll it right through the deferops. This
1752 * avoids relogging the XFS_ISTALE inode.
1753 *
1754 * We check that xfs_ifree() hasn't grown an internal transaction roll
1755 * by asserting that the inode is still locked when it returns.
1756 */
Brian Foster88877d22013-09-20 11:06:11 -04001757 xfs_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinner96355d5a2020-06-29 14:48:45 -07001758 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Brian Foster88877d22013-09-20 11:06:11 -04001759
Brian Foster0e0417f2018-07-11 22:26:07 -07001760 error = xfs_ifree(tp, ip);
Dave Chinner96355d5a2020-06-29 14:48:45 -07001761 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Brian Foster88877d22013-09-20 11:06:11 -04001762 if (error) {
1763 /*
1764 * If we fail to free the inode, shut down. The cancel
1765 * might do that, we need to make sure. Otherwise the
1766 * inode might be lost for a long time or forever.
1767 */
1768 if (!XFS_FORCED_SHUTDOWN(mp)) {
1769 xfs_notice(mp, "%s: xfs_ifree returned error %d",
1770 __func__, error);
1771 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1772 }
Christoph Hellwig4906e212015-06-04 13:47:56 +10001773 xfs_trans_cancel(tp);
Brian Foster88877d22013-09-20 11:06:11 -04001774 return error;
1775 }
1776
1777 /*
1778 * Credit the quota account(s). The inode is gone.
1779 */
1780 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1781
1782 /*
Brian Fosterd4a97a02015-08-19 10:01:40 +10001783 * Just ignore errors at this point. There is nothing we can do except
1784 * to try to keep going. Make sure it's not a silent error.
Brian Foster88877d22013-09-20 11:06:11 -04001785 */
Christoph Hellwig70393312015-06-04 13:48:08 +10001786 error = xfs_trans_commit(tp);
Brian Foster88877d22013-09-20 11:06:11 -04001787 if (error)
1788 xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
1789 __func__, error);
1790
Brian Foster88877d22013-09-20 11:06:11 -04001791 return 0;
1792}
1793
1794/*
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001795 * xfs_inactive
1796 *
1797 * This is called when the vnode reference count for the vnode
1798 * goes to zero. If the file has been unlinked, then it must
1799 * now be truncated. Also, we clear all of the read-ahead state
1800 * kept for the inode here since the file is now closed.
1801 */
Brian Foster74564fb2013-09-20 11:06:12 -04001802void
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001803xfs_inactive(
1804 xfs_inode_t *ip)
1805{
Jie Liu3d3c8b52013-08-12 20:49:59 +10001806 struct xfs_mount *mp;
Jie Liu3d3c8b52013-08-12 20:49:59 +10001807 int error;
1808 int truncate = 0;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001809
1810 /*
1811 * If the inode is already free, then there can be nothing
1812 * to clean up here.
1813 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001814 if (VFS_I(ip)->i_mode == 0) {
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001815 ASSERT(ip->i_df.if_broot_bytes == 0);
Brian Foster74564fb2013-09-20 11:06:12 -04001816 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001817 }
1818
1819 mp = ip->i_mount;
Darrick J. Wong17c12bc2016-10-03 09:11:29 -07001820 ASSERT(!xfs_iflags_test(ip, XFS_IRECOVERY));
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001821
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001822 /* If this is a read-only mount, don't do this (would generate I/O) */
1823 if (mp->m_flags & XFS_MOUNT_RDONLY)
Brian Foster74564fb2013-09-20 11:06:12 -04001824 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001825
Darrick J. Wong62318482018-03-06 17:08:31 -08001826 /* Try to clean out the cow blocks if there are any. */
Christoph Hellwig51d62692018-07-17 16:51:51 -07001827 if (xfs_inode_has_cow_data(ip))
Darrick J. Wong62318482018-03-06 17:08:31 -08001828 xfs_reflink_cancel_cow_range(ip, 0, NULLFILEOFF, true);
1829
Dave Chinner54d7b5c2016-02-09 16:54:58 +11001830 if (VFS_I(ip)->i_nlink != 0) {
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001831 /*
1832 * force is true because we are evicting an inode from the
1833 * cache. Post-eof blocks must be freed, lest we end up with
1834 * broken free space accounting.
Brian Foster3b4683c2017-04-11 10:50:05 -07001835 *
1836 * Note: don't bother with iolock here since lockdep complains
1837 * about acquiring it in reclaim context. We have the only
1838 * reference to the inode at this point anyways.
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001839 */
Brian Foster3b4683c2017-04-11 10:50:05 -07001840 if (xfs_can_free_eofblocks(ip, true))
Brian Fostera36b9262017-01-27 23:22:55 -08001841 xfs_free_eofblocks(ip);
Brian Foster74564fb2013-09-20 11:06:12 -04001842
1843 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001844 }
1845
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001846 if (S_ISREG(VFS_I(ip)->i_mode) &&
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001847 (ip->i_d.di_size != 0 || XFS_ISIZE(ip) != 0 ||
Christoph Hellwigdaf83962020-05-18 10:27:22 -07001848 ip->i_df.if_nextents > 0 || ip->i_delayed_blks > 0))
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001849 truncate = 1;
1850
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -07001851 error = xfs_qm_dqattach(ip);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001852 if (error)
Brian Foster74564fb2013-09-20 11:06:12 -04001853 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001854
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001855 if (S_ISLNK(VFS_I(ip)->i_mode))
Brian Foster36b21dd2013-09-20 11:06:09 -04001856 error = xfs_inactive_symlink(ip);
Brian Fosterf7be2d72013-09-20 11:06:10 -04001857 else if (truncate)
1858 error = xfs_inactive_truncate(ip);
1859 if (error)
Brian Foster74564fb2013-09-20 11:06:12 -04001860 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001861
1862 /*
1863 * If there are attributes associated with the file then blow them away
1864 * now. The code calls a routine that recursively deconstructs the
Dave Chinner6dfe5a02015-05-29 07:40:08 +10001865 * attribute fork. If also blows away the in-core attribute fork.
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001866 */
Dave Chinner6dfe5a02015-05-29 07:40:08 +10001867 if (XFS_IFORK_Q(ip)) {
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001868 error = xfs_attr_inactive(ip);
1869 if (error)
Brian Foster74564fb2013-09-20 11:06:12 -04001870 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001871 }
1872
Dave Chinner6dfe5a02015-05-29 07:40:08 +10001873 ASSERT(!ip->i_afp);
Dave Chinner6dfe5a02015-05-29 07:40:08 +10001874 ASSERT(ip->i_d.di_forkoff == 0);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001875
1876 /*
1877 * Free the inode.
1878 */
Brian Foster88877d22013-09-20 11:06:11 -04001879 error = xfs_inactive_ifree(ip);
1880 if (error)
Brian Foster74564fb2013-09-20 11:06:12 -04001881 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001882
1883 /*
1884 * Release the dquots held by inode, if any.
1885 */
1886 xfs_qm_dqdetach(ip);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001887}
1888
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889/*
Darrick J. Wong9b247172019-02-07 10:37:16 -08001890 * In-Core Unlinked List Lookups
1891 * =============================
1892 *
1893 * Every inode is supposed to be reachable from some other piece of metadata
1894 * with the exception of the root directory. Inodes with a connection to a
1895 * file descriptor but not linked from anywhere in the on-disk directory tree
1896 * are collectively known as unlinked inodes, though the filesystem itself
1897 * maintains links to these inodes so that on-disk metadata are consistent.
1898 *
1899 * XFS implements a per-AG on-disk hash table of unlinked inodes. The AGI
1900 * header contains a number of buckets that point to an inode, and each inode
1901 * record has a pointer to the next inode in the hash chain. This
1902 * singly-linked list causes scaling problems in the iunlink remove function
1903 * because we must walk that list to find the inode that points to the inode
1904 * being removed from the unlinked hash bucket list.
1905 *
1906 * What if we modelled the unlinked list as a collection of records capturing
1907 * "X.next_unlinked = Y" relations? If we indexed those records on Y, we'd
1908 * have a fast way to look up unlinked list predecessors, which avoids the
1909 * slow list walk. That's exactly what we do here (in-core) with a per-AG
1910 * rhashtable.
1911 *
1912 * Because this is a backref cache, we ignore operational failures since the
1913 * iunlink code can fall back to the slow bucket walk. The only errors that
1914 * should bubble out are for obviously incorrect situations.
1915 *
1916 * All users of the backref cache MUST hold the AGI buffer lock to serialize
1917 * access or have otherwise provided for concurrency control.
1918 */
1919
1920/* Capture a "X.next_unlinked = Y" relationship. */
1921struct xfs_iunlink {
1922 struct rhash_head iu_rhash_head;
1923 xfs_agino_t iu_agino; /* X */
1924 xfs_agino_t iu_next_unlinked; /* Y */
1925};
1926
1927/* Unlinked list predecessor lookup hashtable construction */
1928static int
1929xfs_iunlink_obj_cmpfn(
1930 struct rhashtable_compare_arg *arg,
1931 const void *obj)
1932{
1933 const xfs_agino_t *key = arg->key;
1934 const struct xfs_iunlink *iu = obj;
1935
1936 if (iu->iu_next_unlinked != *key)
1937 return 1;
1938 return 0;
1939}
1940
1941static const struct rhashtable_params xfs_iunlink_hash_params = {
1942 .min_size = XFS_AGI_UNLINKED_BUCKETS,
1943 .key_len = sizeof(xfs_agino_t),
1944 .key_offset = offsetof(struct xfs_iunlink,
1945 iu_next_unlinked),
1946 .head_offset = offsetof(struct xfs_iunlink, iu_rhash_head),
1947 .automatic_shrinking = true,
1948 .obj_cmpfn = xfs_iunlink_obj_cmpfn,
1949};
1950
1951/*
1952 * Return X, where X.next_unlinked == @agino. Returns NULLAGINO if no such
1953 * relation is found.
1954 */
1955static xfs_agino_t
1956xfs_iunlink_lookup_backref(
1957 struct xfs_perag *pag,
1958 xfs_agino_t agino)
1959{
1960 struct xfs_iunlink *iu;
1961
1962 iu = rhashtable_lookup_fast(&pag->pagi_unlinked_hash, &agino,
1963 xfs_iunlink_hash_params);
1964 return iu ? iu->iu_agino : NULLAGINO;
1965}
1966
1967/*
1968 * Take ownership of an iunlink cache entry and insert it into the hash table.
1969 * If successful, the entry will be owned by the cache; if not, it is freed.
1970 * Either way, the caller does not own @iu after this call.
1971 */
1972static int
1973xfs_iunlink_insert_backref(
1974 struct xfs_perag *pag,
1975 struct xfs_iunlink *iu)
1976{
1977 int error;
1978
1979 error = rhashtable_insert_fast(&pag->pagi_unlinked_hash,
1980 &iu->iu_rhash_head, xfs_iunlink_hash_params);
1981 /*
1982 * Fail loudly if there already was an entry because that's a sign of
1983 * corruption of in-memory data. Also fail loudly if we see an error
1984 * code we didn't anticipate from the rhashtable code. Currently we
1985 * only anticipate ENOMEM.
1986 */
1987 if (error) {
1988 WARN(error != -ENOMEM, "iunlink cache insert error %d", error);
1989 kmem_free(iu);
1990 }
1991 /*
1992 * Absorb any runtime errors that aren't a result of corruption because
1993 * this is a cache and we can always fall back to bucket list scanning.
1994 */
1995 if (error != 0 && error != -EEXIST)
1996 error = 0;
1997 return error;
1998}
1999
2000/* Remember that @prev_agino.next_unlinked = @this_agino. */
2001static int
2002xfs_iunlink_add_backref(
2003 struct xfs_perag *pag,
2004 xfs_agino_t prev_agino,
2005 xfs_agino_t this_agino)
2006{
2007 struct xfs_iunlink *iu;
2008
2009 if (XFS_TEST_ERROR(false, pag->pag_mount, XFS_ERRTAG_IUNLINK_FALLBACK))
2010 return 0;
2011
Tetsuo Handa707e0dd2019-08-26 12:06:22 -07002012 iu = kmem_zalloc(sizeof(*iu), KM_NOFS);
Darrick J. Wong9b247172019-02-07 10:37:16 -08002013 iu->iu_agino = prev_agino;
2014 iu->iu_next_unlinked = this_agino;
2015
2016 return xfs_iunlink_insert_backref(pag, iu);
2017}
2018
2019/*
2020 * Replace X.next_unlinked = @agino with X.next_unlinked = @next_unlinked.
2021 * If @next_unlinked is NULLAGINO, we drop the backref and exit. If there
2022 * wasn't any such entry then we don't bother.
2023 */
2024static int
2025xfs_iunlink_change_backref(
2026 struct xfs_perag *pag,
2027 xfs_agino_t agino,
2028 xfs_agino_t next_unlinked)
2029{
2030 struct xfs_iunlink *iu;
2031 int error;
2032
2033 /* Look up the old entry; if there wasn't one then exit. */
2034 iu = rhashtable_lookup_fast(&pag->pagi_unlinked_hash, &agino,
2035 xfs_iunlink_hash_params);
2036 if (!iu)
2037 return 0;
2038
2039 /*
2040 * Remove the entry. This shouldn't ever return an error, but if we
2041 * couldn't remove the old entry we don't want to add it again to the
2042 * hash table, and if the entry disappeared on us then someone's
2043 * violated the locking rules and we need to fail loudly. Either way
2044 * we cannot remove the inode because internal state is or would have
2045 * been corrupt.
2046 */
2047 error = rhashtable_remove_fast(&pag->pagi_unlinked_hash,
2048 &iu->iu_rhash_head, xfs_iunlink_hash_params);
2049 if (error)
2050 return error;
2051
2052 /* If there is no new next entry just free our item and return. */
2053 if (next_unlinked == NULLAGINO) {
2054 kmem_free(iu);
2055 return 0;
2056 }
2057
2058 /* Update the entry and re-add it to the hash table. */
2059 iu->iu_next_unlinked = next_unlinked;
2060 return xfs_iunlink_insert_backref(pag, iu);
2061}
2062
2063/* Set up the in-core predecessor structures. */
2064int
2065xfs_iunlink_init(
2066 struct xfs_perag *pag)
2067{
2068 return rhashtable_init(&pag->pagi_unlinked_hash,
2069 &xfs_iunlink_hash_params);
2070}
2071
2072/* Free the in-core predecessor structures. */
2073static void
2074xfs_iunlink_free_item(
2075 void *ptr,
2076 void *arg)
2077{
2078 struct xfs_iunlink *iu = ptr;
2079 bool *freed_anything = arg;
2080
2081 *freed_anything = true;
2082 kmem_free(iu);
2083}
2084
2085void
2086xfs_iunlink_destroy(
2087 struct xfs_perag *pag)
2088{
2089 bool freed_anything = false;
2090
2091 rhashtable_free_and_destroy(&pag->pagi_unlinked_hash,
2092 xfs_iunlink_free_item, &freed_anything);
2093
2094 ASSERT(freed_anything == false || XFS_FORCED_SHUTDOWN(pag->pag_mount));
2095}
2096
2097/*
Darrick J. Wong9a4a5112019-02-07 10:37:14 -08002098 * Point the AGI unlinked bucket at an inode and log the results. The caller
2099 * is responsible for validating the old value.
2100 */
2101STATIC int
2102xfs_iunlink_update_bucket(
2103 struct xfs_trans *tp,
2104 xfs_agnumber_t agno,
2105 struct xfs_buf *agibp,
2106 unsigned int bucket_index,
2107 xfs_agino_t new_agino)
2108{
Christoph Hellwig370c7822020-03-10 08:57:29 -07002109 struct xfs_agi *agi = agibp->b_addr;
Darrick J. Wong9a4a5112019-02-07 10:37:14 -08002110 xfs_agino_t old_value;
2111 int offset;
2112
2113 ASSERT(xfs_verify_agino_or_null(tp->t_mountp, agno, new_agino));
2114
2115 old_value = be32_to_cpu(agi->agi_unlinked[bucket_index]);
2116 trace_xfs_iunlink_update_bucket(tp->t_mountp, agno, bucket_index,
2117 old_value, new_agino);
2118
2119 /*
2120 * We should never find the head of the list already set to the value
2121 * passed in because either we're adding or removing ourselves from the
2122 * head of the list.
2123 */
Darrick J. Wonga5155b82019-11-02 09:40:53 -07002124 if (old_value == new_agino) {
Darrick J. Wong8d57c212020-03-11 10:37:54 -07002125 xfs_buf_mark_corrupt(agibp);
Darrick J. Wong9a4a5112019-02-07 10:37:14 -08002126 return -EFSCORRUPTED;
Darrick J. Wonga5155b82019-11-02 09:40:53 -07002127 }
Darrick J. Wong9a4a5112019-02-07 10:37:14 -08002128
2129 agi->agi_unlinked[bucket_index] = cpu_to_be32(new_agino);
2130 offset = offsetof(struct xfs_agi, agi_unlinked) +
2131 (sizeof(xfs_agino_t) * bucket_index);
2132 xfs_trans_log_buf(tp, agibp, offset, offset + sizeof(xfs_agino_t) - 1);
2133 return 0;
2134}
2135
Darrick J. Wongf2fc16a2019-02-07 10:37:15 -08002136/* Set an on-disk inode's next_unlinked pointer. */
2137STATIC void
2138xfs_iunlink_update_dinode(
2139 struct xfs_trans *tp,
2140 xfs_agnumber_t agno,
2141 xfs_agino_t agino,
2142 struct xfs_buf *ibp,
2143 struct xfs_dinode *dip,
2144 struct xfs_imap *imap,
2145 xfs_agino_t next_agino)
2146{
2147 struct xfs_mount *mp = tp->t_mountp;
2148 int offset;
2149
2150 ASSERT(xfs_verify_agino_or_null(mp, agno, next_agino));
2151
2152 trace_xfs_iunlink_update_dinode(mp, agno, agino,
2153 be32_to_cpu(dip->di_next_unlinked), next_agino);
2154
2155 dip->di_next_unlinked = cpu_to_be32(next_agino);
2156 offset = imap->im_boffset +
2157 offsetof(struct xfs_dinode, di_next_unlinked);
2158
2159 /* need to recalc the inode CRC if appropriate */
2160 xfs_dinode_calc_crc(mp, dip);
2161 xfs_trans_inode_buf(tp, ibp);
2162 xfs_trans_log_buf(tp, ibp, offset, offset + sizeof(xfs_agino_t) - 1);
Darrick J. Wongf2fc16a2019-02-07 10:37:15 -08002163}
2164
2165/* Set an in-core inode's unlinked pointer and return the old value. */
2166STATIC int
2167xfs_iunlink_update_inode(
2168 struct xfs_trans *tp,
2169 struct xfs_inode *ip,
2170 xfs_agnumber_t agno,
2171 xfs_agino_t next_agino,
2172 xfs_agino_t *old_next_agino)
2173{
2174 struct xfs_mount *mp = tp->t_mountp;
2175 struct xfs_dinode *dip;
2176 struct xfs_buf *ibp;
2177 xfs_agino_t old_value;
2178 int error;
2179
2180 ASSERT(xfs_verify_agino_or_null(mp, agno, next_agino));
2181
Brian Fosterc1995072020-05-06 13:29:20 -07002182 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp, 0);
Darrick J. Wongf2fc16a2019-02-07 10:37:15 -08002183 if (error)
2184 return error;
2185
2186 /* Make sure the old pointer isn't garbage. */
2187 old_value = be32_to_cpu(dip->di_next_unlinked);
2188 if (!xfs_verify_agino_or_null(mp, agno, old_value)) {
Darrick J. Wonga5155b82019-11-02 09:40:53 -07002189 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, dip,
2190 sizeof(*dip), __this_address);
Darrick J. Wongf2fc16a2019-02-07 10:37:15 -08002191 error = -EFSCORRUPTED;
2192 goto out;
2193 }
2194
2195 /*
2196 * Since we're updating a linked list, we should never find that the
2197 * current pointer is the same as the new value, unless we're
2198 * terminating the list.
2199 */
2200 *old_next_agino = old_value;
2201 if (old_value == next_agino) {
Darrick J. Wonga5155b82019-11-02 09:40:53 -07002202 if (next_agino != NULLAGINO) {
2203 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__,
2204 dip, sizeof(*dip), __this_address);
Darrick J. Wongf2fc16a2019-02-07 10:37:15 -08002205 error = -EFSCORRUPTED;
Darrick J. Wonga5155b82019-11-02 09:40:53 -07002206 }
Darrick J. Wongf2fc16a2019-02-07 10:37:15 -08002207 goto out;
2208 }
2209
2210 /* Ok, update the new pointer. */
2211 xfs_iunlink_update_dinode(tp, agno, XFS_INO_TO_AGINO(mp, ip->i_ino),
2212 ibp, dip, &ip->i_imap, next_agino);
2213 return 0;
2214out:
2215 xfs_trans_brelse(tp, ibp);
2216 return error;
2217}
2218
Darrick J. Wong9a4a5112019-02-07 10:37:14 -08002219/*
Darrick J. Wongc4a6bf72019-02-13 11:15:17 -08002220 * This is called when the inode's link count has gone to 0 or we are creating
2221 * a tmpfile via O_TMPFILE. The inode @ip must have nlink == 0.
Dave Chinner54d7b5c2016-02-09 16:54:58 +11002222 *
2223 * We place the on-disk inode on a list in the AGI. It will be pulled from this
2224 * list when the inode is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 */
Dave Chinner54d7b5c2016-02-09 16:54:58 +11002226STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227xfs_iunlink(
Darrick J. Wong5837f622019-02-07 10:37:13 -08002228 struct xfs_trans *tp,
2229 struct xfs_inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
Darrick J. Wong5837f622019-02-07 10:37:13 -08002231 struct xfs_mount *mp = tp->t_mountp;
2232 struct xfs_agi *agi;
Darrick J. Wong5837f622019-02-07 10:37:13 -08002233 struct xfs_buf *agibp;
Darrick J. Wong86bfd372019-02-07 10:37:14 -08002234 xfs_agino_t next_agino;
Darrick J. Wong5837f622019-02-07 10:37:13 -08002235 xfs_agnumber_t agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
2236 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
2237 short bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
Darrick J. Wong5837f622019-02-07 10:37:13 -08002238 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Darrick J. Wongc4a6bf72019-02-13 11:15:17 -08002240 ASSERT(VFS_I(ip)->i_nlink == 0);
Dave Chinnerc19b3b052016-02-09 16:54:58 +11002241 ASSERT(VFS_I(ip)->i_mode != 0);
Darrick J. Wong4664c662019-02-07 10:37:16 -08002242 trace_xfs_iunlink(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Darrick J. Wong5837f622019-02-07 10:37:13 -08002244 /* Get the agi buffer first. It ensures lock ordering on the list. */
2245 error = xfs_read_agi(mp, tp, agno, &agibp);
Vlad Apostolov859d7182007-10-11 17:44:18 +10002246 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 return error;
Christoph Hellwig370c7822020-03-10 08:57:29 -07002248 agi = agibp->b_addr;
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 /*
Darrick J. Wong86bfd372019-02-07 10:37:14 -08002251 * Get the index into the agi hash table for the list this inode will
2252 * go on. Make sure the pointer isn't garbage and that this inode
2253 * isn't already on the list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 */
Darrick J. Wong86bfd372019-02-07 10:37:14 -08002255 next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
2256 if (next_agino == agino ||
Darrick J. Wonga5155b82019-11-02 09:40:53 -07002257 !xfs_verify_agino_or_null(mp, agno, next_agino)) {
Darrick J. Wong8d57c212020-03-11 10:37:54 -07002258 xfs_buf_mark_corrupt(agibp);
Darrick J. Wong86bfd372019-02-07 10:37:14 -08002259 return -EFSCORRUPTED;
Darrick J. Wonga5155b82019-11-02 09:40:53 -07002260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Darrick J. Wong86bfd372019-02-07 10:37:14 -08002262 if (next_agino != NULLAGINO) {
Darrick J. Wong9b247172019-02-07 10:37:16 -08002263 xfs_agino_t old_agino;
Darrick J. Wongf2fc16a2019-02-07 10:37:15 -08002264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 /*
Darrick J. Wongf2fc16a2019-02-07 10:37:15 -08002266 * There is already another inode in the bucket, so point this
2267 * inode to the current head of the list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 */
Darrick J. Wongf2fc16a2019-02-07 10:37:15 -08002269 error = xfs_iunlink_update_inode(tp, ip, agno, next_agino,
2270 &old_agino);
Vlad Apostolovc319b582007-11-23 16:27:51 +11002271 if (error)
2272 return error;
Darrick J. Wongf2fc16a2019-02-07 10:37:15 -08002273 ASSERT(old_agino == NULLAGINO);
Darrick J. Wong9b247172019-02-07 10:37:16 -08002274
2275 /*
2276 * agino has been unlinked, add a backref from the next inode
2277 * back to agino.
2278 */
Gao Xiang92a00542020-07-13 09:13:00 -07002279 error = xfs_iunlink_add_backref(agibp->b_pag, agino, next_agino);
Darrick J. Wong9b247172019-02-07 10:37:16 -08002280 if (error)
2281 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 }
2283
Darrick J. Wong9a4a5112019-02-07 10:37:14 -08002284 /* Point the head of the list to point to this inode. */
2285 return xfs_iunlink_update_bucket(tp, agno, agibp, bucket_index, agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286}
2287
Darrick J. Wong23ffa522019-02-07 10:37:15 -08002288/* Return the imap, dinode pointer, and buffer for an inode. */
2289STATIC int
2290xfs_iunlink_map_ino(
2291 struct xfs_trans *tp,
2292 xfs_agnumber_t agno,
2293 xfs_agino_t agino,
2294 struct xfs_imap *imap,
2295 struct xfs_dinode **dipp,
2296 struct xfs_buf **bpp)
2297{
2298 struct xfs_mount *mp = tp->t_mountp;
2299 int error;
2300
2301 imap->im_blkno = 0;
2302 error = xfs_imap(mp, tp, XFS_AGINO_TO_INO(mp, agno, agino), imap, 0);
2303 if (error) {
2304 xfs_warn(mp, "%s: xfs_imap returned error %d.",
2305 __func__, error);
2306 return error;
2307 }
2308
Brian Fosterc1995072020-05-06 13:29:20 -07002309 error = xfs_imap_to_bp(mp, tp, imap, dipp, bpp, 0);
Darrick J. Wong23ffa522019-02-07 10:37:15 -08002310 if (error) {
2311 xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
2312 __func__, error);
2313 return error;
2314 }
2315
2316 return 0;
2317}
2318
2319/*
2320 * Walk the unlinked chain from @head_agino until we find the inode that
2321 * points to @target_agino. Return the inode number, map, dinode pointer,
2322 * and inode cluster buffer of that inode as @agino, @imap, @dipp, and @bpp.
2323 *
2324 * @tp, @pag, @head_agino, and @target_agino are input parameters.
2325 * @agino, @imap, @dipp, and @bpp are all output parameters.
2326 *
2327 * Do not call this function if @target_agino is the head of the list.
2328 */
2329STATIC int
2330xfs_iunlink_map_prev(
2331 struct xfs_trans *tp,
2332 xfs_agnumber_t agno,
2333 xfs_agino_t head_agino,
2334 xfs_agino_t target_agino,
2335 xfs_agino_t *agino,
2336 struct xfs_imap *imap,
2337 struct xfs_dinode **dipp,
Darrick J. Wong9b247172019-02-07 10:37:16 -08002338 struct xfs_buf **bpp,
2339 struct xfs_perag *pag)
Darrick J. Wong23ffa522019-02-07 10:37:15 -08002340{
2341 struct xfs_mount *mp = tp->t_mountp;
2342 xfs_agino_t next_agino;
2343 int error;
2344
2345 ASSERT(head_agino != target_agino);
2346 *bpp = NULL;
2347
Darrick J. Wong9b247172019-02-07 10:37:16 -08002348 /* See if our backref cache can find it faster. */
2349 *agino = xfs_iunlink_lookup_backref(pag, target_agino);
2350 if (*agino != NULLAGINO) {
2351 error = xfs_iunlink_map_ino(tp, agno, *agino, imap, dipp, bpp);
2352 if (error)
2353 return error;
2354
2355 if (be32_to_cpu((*dipp)->di_next_unlinked) == target_agino)
2356 return 0;
2357
2358 /*
2359 * If we get here the cache contents were corrupt, so drop the
2360 * buffer and fall back to walking the bucket list.
2361 */
2362 xfs_trans_brelse(tp, *bpp);
2363 *bpp = NULL;
2364 WARN_ON_ONCE(1);
2365 }
2366
2367 trace_xfs_iunlink_map_prev_fallback(mp, agno);
2368
2369 /* Otherwise, walk the entire bucket until we find it. */
Darrick J. Wong23ffa522019-02-07 10:37:15 -08002370 next_agino = head_agino;
2371 while (next_agino != target_agino) {
2372 xfs_agino_t unlinked_agino;
2373
2374 if (*bpp)
2375 xfs_trans_brelse(tp, *bpp);
2376
2377 *agino = next_agino;
2378 error = xfs_iunlink_map_ino(tp, agno, next_agino, imap, dipp,
2379 bpp);
2380 if (error)
2381 return error;
2382
2383 unlinked_agino = be32_to_cpu((*dipp)->di_next_unlinked);
2384 /*
2385 * Make sure this pointer is valid and isn't an obvious
2386 * infinite loop.
2387 */
2388 if (!xfs_verify_agino(mp, agno, unlinked_agino) ||
2389 next_agino == unlinked_agino) {
2390 XFS_CORRUPTION_ERROR(__func__,
2391 XFS_ERRLEVEL_LOW, mp,
2392 *dipp, sizeof(**dipp));
2393 error = -EFSCORRUPTED;
2394 return error;
2395 }
2396 next_agino = unlinked_agino;
2397 }
2398
2399 return 0;
2400}
2401
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402/*
2403 * Pull the on-disk inode from the AGI unlinked list.
2404 */
2405STATIC int
2406xfs_iunlink_remove(
Darrick J. Wong5837f622019-02-07 10:37:13 -08002407 struct xfs_trans *tp,
2408 struct xfs_inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409{
Darrick J. Wong5837f622019-02-07 10:37:13 -08002410 struct xfs_mount *mp = tp->t_mountp;
2411 struct xfs_agi *agi;
Darrick J. Wong5837f622019-02-07 10:37:13 -08002412 struct xfs_buf *agibp;
Darrick J. Wong5837f622019-02-07 10:37:13 -08002413 struct xfs_buf *last_ibp;
2414 struct xfs_dinode *last_dip = NULL;
Darrick J. Wong5837f622019-02-07 10:37:13 -08002415 xfs_agnumber_t agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
2416 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
2417 xfs_agino_t next_agino;
Darrick J. Wongb1d2a062019-02-07 10:37:15 -08002418 xfs_agino_t head_agino;
Darrick J. Wong5837f622019-02-07 10:37:13 -08002419 short bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
Darrick J. Wong5837f622019-02-07 10:37:13 -08002420 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
Darrick J. Wong4664c662019-02-07 10:37:16 -08002422 trace_xfs_iunlink_remove(ip);
2423
Darrick J. Wong5837f622019-02-07 10:37:13 -08002424 /* Get the agi buffer first. It ensures lock ordering on the list. */
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002425 error = xfs_read_agi(mp, tp, agno, &agibp);
2426 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 return error;
Christoph Hellwig370c7822020-03-10 08:57:29 -07002428 agi = agibp->b_addr;
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002429
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 /*
Darrick J. Wong86bfd372019-02-07 10:37:14 -08002431 * Get the index into the agi hash table for the list this inode will
2432 * go on. Make sure the head pointer isn't garbage.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 */
Darrick J. Wongb1d2a062019-02-07 10:37:15 -08002434 head_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
2435 if (!xfs_verify_agino(mp, agno, head_agino)) {
Darrick J. Wongd2e73662018-06-04 11:27:51 -07002436 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
2437 agi, sizeof(*agi));
2438 return -EFSCORRUPTED;
2439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
Darrick J. Wongb1d2a062019-02-07 10:37:15 -08002441 /*
2442 * Set our inode's next_unlinked pointer to NULL and then return
2443 * the old pointer value so that we can update whatever was previous
2444 * to us in the list to point to whatever was next in the list.
2445 */
2446 error = xfs_iunlink_update_inode(tp, ip, agno, NULLAGINO, &next_agino);
2447 if (error)
2448 return error;
Darrick J. Wong9a4a5112019-02-07 10:37:14 -08002449
Darrick J. Wong9b247172019-02-07 10:37:16 -08002450 /*
2451 * If there was a backref pointing from the next inode back to this
2452 * one, remove it because we've removed this inode from the list.
2453 *
2454 * Later, if this inode was in the middle of the list we'll update
2455 * this inode's backref to point from the next inode.
2456 */
2457 if (next_agino != NULLAGINO) {
Gao Xiang92a00542020-07-13 09:13:00 -07002458 error = xfs_iunlink_change_backref(agibp->b_pag, next_agino,
Darrick J. Wong9b247172019-02-07 10:37:16 -08002459 NULLAGINO);
2460 if (error)
Gao Xiang92a00542020-07-13 09:13:00 -07002461 return error;
Darrick J. Wong9b247172019-02-07 10:37:16 -08002462 }
2463
Gao Xiang92a00542020-07-13 09:13:00 -07002464 if (head_agino != agino) {
Darrick J. Wongf2fc16a2019-02-07 10:37:15 -08002465 struct xfs_imap imap;
2466 xfs_agino_t prev_agino;
2467
Darrick J. Wong23ffa522019-02-07 10:37:15 -08002468 /* We need to search the list for the inode being freed. */
Darrick J. Wongb1d2a062019-02-07 10:37:15 -08002469 error = xfs_iunlink_map_prev(tp, agno, head_agino, agino,
Darrick J. Wong9b247172019-02-07 10:37:16 -08002470 &prev_agino, &imap, &last_dip, &last_ibp,
Gao Xiang92a00542020-07-13 09:13:00 -07002471 agibp->b_pag);
Darrick J. Wong23ffa522019-02-07 10:37:15 -08002472 if (error)
Gao Xiang92a00542020-07-13 09:13:00 -07002473 return error;
Christoph Hellwig475ee412012-07-03 12:21:22 -04002474
Darrick J. Wongf2fc16a2019-02-07 10:37:15 -08002475 /* Point the previous inode on the list to the next inode. */
2476 xfs_iunlink_update_dinode(tp, agno, prev_agino, last_ibp,
2477 last_dip, &imap, next_agino);
Darrick J. Wong9b247172019-02-07 10:37:16 -08002478
2479 /*
2480 * Now we deal with the backref for this inode. If this inode
2481 * pointed at a real inode, change the backref that pointed to
2482 * us to point to our old next. If this inode was the end of
2483 * the list, delete the backref that pointed to us. Note that
2484 * change_backref takes care of deleting the backref if
2485 * next_agino is NULLAGINO.
2486 */
Gao Xiang92a00542020-07-13 09:13:00 -07002487 return xfs_iunlink_change_backref(agibp->b_pag, agino,
2488 next_agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 }
Darrick J. Wong9b247172019-02-07 10:37:16 -08002490
Gao Xiang92a00542020-07-13 09:13:00 -07002491 /* Point the head of the list to the next unlinked inode. */
2492 return xfs_iunlink_update_bucket(tp, agno, agibp, bucket_index,
2493 next_agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494}
2495
Dave Chinner5b3eed72010-08-24 11:42:41 +10002496/*
Dave Chinner71e3e352020-06-29 14:49:18 -07002497 * Look up the inode number specified and if it is not already marked XFS_ISTALE
2498 * mark it stale. We should only find clean inodes in this lookup that aren't
2499 * already stale.
Dave Chinner58061652020-03-24 20:10:30 -07002500 */
Dave Chinner71e3e352020-06-29 14:49:18 -07002501static void
2502xfs_ifree_mark_inode_stale(
2503 struct xfs_buf *bp,
Dave Chinner58061652020-03-24 20:10:30 -07002504 struct xfs_inode *free_ip,
Brian Fosterd9fdd0a2020-04-02 08:18:57 -07002505 xfs_ino_t inum)
Dave Chinner58061652020-03-24 20:10:30 -07002506{
Dave Chinner71e3e352020-06-29 14:49:18 -07002507 struct xfs_mount *mp = bp->b_mount;
2508 struct xfs_perag *pag = bp->b_pag;
2509 struct xfs_inode_log_item *iip;
Dave Chinner58061652020-03-24 20:10:30 -07002510 struct xfs_inode *ip;
2511
2512retry:
2513 rcu_read_lock();
2514 ip = radix_tree_lookup(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, inum));
2515
2516 /* Inode not in memory, nothing to do */
Dave Chinner71e3e352020-06-29 14:49:18 -07002517 if (!ip) {
2518 rcu_read_unlock();
2519 return;
2520 }
Dave Chinner58061652020-03-24 20:10:30 -07002521
2522 /*
2523 * because this is an RCU protected lookup, we could find a recently
2524 * freed or even reallocated inode during the lookup. We need to check
2525 * under the i_flags_lock for a valid inode here. Skip it if it is not
2526 * valid, the wrong inode or stale.
2527 */
2528 spin_lock(&ip->i_flags_lock);
Dave Chinner718ecc52020-08-17 16:41:01 -07002529 if (ip->i_ino != inum || __xfs_iflags_test(ip, XFS_ISTALE))
2530 goto out_iflags_unlock;
Dave Chinner58061652020-03-24 20:10:30 -07002531
2532 /*
2533 * Don't try to lock/unlock the current inode, but we _cannot_ skip the
2534 * other inodes that we did not find in the list attached to the buffer
2535 * and are not already marked stale. If we can't lock it, back off and
2536 * retry.
2537 */
2538 if (ip != free_ip) {
2539 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
Dave Chinner71e3e352020-06-29 14:49:18 -07002540 spin_unlock(&ip->i_flags_lock);
Dave Chinner58061652020-03-24 20:10:30 -07002541 rcu_read_unlock();
2542 delay(1);
2543 goto retry;
2544 }
Dave Chinner58061652020-03-24 20:10:30 -07002545 }
Dave Chinner71e3e352020-06-29 14:49:18 -07002546 ip->i_flags |= XFS_ISTALE;
Dave Chinner58061652020-03-24 20:10:30 -07002547
Dave Chinner58061652020-03-24 20:10:30 -07002548 /*
Dave Chinner718ecc52020-08-17 16:41:01 -07002549 * If the inode is flushing, it is already attached to the buffer. All
Dave Chinner71e3e352020-06-29 14:49:18 -07002550 * we needed to do here is mark the inode stale so buffer IO completion
2551 * will remove it from the AIL.
Dave Chinner58061652020-03-24 20:10:30 -07002552 */
Dave Chinner71e3e352020-06-29 14:49:18 -07002553 iip = ip->i_itemp;
Dave Chinner718ecc52020-08-17 16:41:01 -07002554 if (__xfs_iflags_test(ip, XFS_IFLUSHING)) {
Dave Chinner71e3e352020-06-29 14:49:18 -07002555 ASSERT(!list_empty(&iip->ili_item.li_bio_list));
2556 ASSERT(iip->ili_last_fields);
2557 goto out_iunlock;
Dave Chinner58061652020-03-24 20:10:30 -07002558 }
Dave Chinner58061652020-03-24 20:10:30 -07002559
Dave Chinner71e3e352020-06-29 14:49:18 -07002560 /*
Dave Chinner48d55e22020-06-29 14:49:18 -07002561 * Inodes not attached to the buffer can be released immediately.
2562 * Everything else has to go through xfs_iflush_abort() on journal
2563 * commit as the flock synchronises removal of the inode from the
2564 * cluster buffer against inode reclaim.
Dave Chinner71e3e352020-06-29 14:49:18 -07002565 */
Dave Chinner718ecc52020-08-17 16:41:01 -07002566 if (!iip || list_empty(&iip->ili_item.li_bio_list))
Dave Chinner71e3e352020-06-29 14:49:18 -07002567 goto out_iunlock;
Dave Chinner718ecc52020-08-17 16:41:01 -07002568
2569 __xfs_iflags_set(ip, XFS_IFLUSHING);
2570 spin_unlock(&ip->i_flags_lock);
2571 rcu_read_unlock();
Dave Chinner71e3e352020-06-29 14:49:18 -07002572
2573 /* we have a dirty inode in memory that has not yet been flushed. */
Dave Chinner71e3e352020-06-29 14:49:18 -07002574 spin_lock(&iip->ili_lock);
2575 iip->ili_last_fields = iip->ili_fields;
2576 iip->ili_fields = 0;
2577 iip->ili_fsync_fields = 0;
2578 spin_unlock(&iip->ili_lock);
Dave Chinner71e3e352020-06-29 14:49:18 -07002579 ASSERT(iip->ili_last_fields);
2580
Dave Chinner718ecc52020-08-17 16:41:01 -07002581 if (ip != free_ip)
2582 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2583 return;
2584
Dave Chinner71e3e352020-06-29 14:49:18 -07002585out_iunlock:
2586 if (ip != free_ip)
2587 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner718ecc52020-08-17 16:41:01 -07002588out_iflags_unlock:
2589 spin_unlock(&ip->i_flags_lock);
2590 rcu_read_unlock();
Dave Chinner58061652020-03-24 20:10:30 -07002591}
2592
2593/*
Zhi Yong Wu0b8182d2013-08-12 03:14:59 +00002594 * A big issue when freeing the inode cluster is that we _cannot_ skip any
Dave Chinner5b3eed72010-08-24 11:42:41 +10002595 * inodes that are in memory - they all must be marked stale and attached to
2596 * the cluster buffer.
2597 */
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00002598STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599xfs_ifree_cluster(
Dave Chinner71e3e352020-06-29 14:49:18 -07002600 struct xfs_inode *free_ip,
2601 struct xfs_trans *tp,
Brian Foster09b56602015-05-29 09:26:03 +10002602 struct xfs_icluster *xic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603{
Dave Chinner71e3e352020-06-29 14:49:18 -07002604 struct xfs_mount *mp = free_ip->i_mount;
2605 struct xfs_ino_geometry *igeo = M_IGEO(mp);
2606 struct xfs_buf *bp;
2607 xfs_daddr_t blkno;
2608 xfs_ino_t inum = xic->first_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 int nbufs;
Dave Chinner5b257b42010-06-03 16:22:29 +10002610 int i, j;
Brian Foster3cdaa182015-06-04 13:03:34 +10002611 int ioffset;
Darrick J. Wongce924642020-01-23 17:01:18 -08002612 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
Darrick J. Wongef325952019-06-05 11:19:34 -07002614 nbufs = igeo->ialloc_blks / igeo->blocks_per_cluster;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
Darrick J. Wongef325952019-06-05 11:19:34 -07002616 for (j = 0; j < nbufs; j++, inum += igeo->inodes_per_cluster) {
Brian Foster09b56602015-05-29 09:26:03 +10002617 /*
2618 * The allocation bitmap tells us which inodes of the chunk were
2619 * physically allocated. Skip the cluster if an inode falls into
2620 * a sparse region.
2621 */
Brian Foster3cdaa182015-06-04 13:03:34 +10002622 ioffset = inum - xic->first_ino;
2623 if ((xic->alloc & XFS_INOBT_MASK(ioffset)) == 0) {
Darrick J. Wongef325952019-06-05 11:19:34 -07002624 ASSERT(ioffset % igeo->inodes_per_cluster == 0);
Brian Foster09b56602015-05-29 09:26:03 +10002625 continue;
2626 }
2627
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
2629 XFS_INO_TO_AGBNO(mp, inum));
2630
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 /*
Dave Chinner5b257b42010-06-03 16:22:29 +10002632 * We obtain and lock the backing buffer first in the process
Dave Chinner718ecc52020-08-17 16:41:01 -07002633 * here to ensure dirty inodes attached to the buffer remain in
2634 * the flushing state while we mark them stale.
2635 *
Dave Chinner5b257b42010-06-03 16:22:29 +10002636 * If we scan the in-memory inodes first, then buffer IO can
2637 * complete before we get a lock on it, and hence we may fail
2638 * to mark all the active inodes on the buffer stale.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 */
Darrick J. Wongce924642020-01-23 17:01:18 -08002640 error = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
2641 mp->m_bsize * igeo->blocks_per_cluster,
2642 XBF_UNMAPPED, &bp);
Dave Chinner71e3e352020-06-29 14:49:18 -07002643 if (error)
Darrick J. Wongce924642020-01-23 17:01:18 -08002644 return error;
Dave Chinnerb0f539d2012-11-14 17:53:49 +11002645
2646 /*
2647 * This buffer may not have been correctly initialised as we
2648 * didn't read it from disk. That's not important because we are
2649 * only using to mark the buffer as stale in the log, and to
2650 * attach stale cached inodes on it. That means it will never be
2651 * dispatched for IO. If it is, we want to know about it, and we
2652 * want it to fail. We can acheive this by adding a write
2653 * verifier to the buffer.
2654 */
Colin Ian King8c4ce792018-12-12 08:46:20 -08002655 bp->b_ops = &xfs_inode_buf_ops;
Dave Chinnerb0f539d2012-11-14 17:53:49 +11002656
Dave Chinner5b257b42010-06-03 16:22:29 +10002657 /*
Dave Chinner71e3e352020-06-29 14:49:18 -07002658 * Now we need to set all the cached clean inodes as XFS_ISTALE,
2659 * too. This requires lookups, and will skip inodes that we've
2660 * already marked XFS_ISTALE.
Dave Chinner5b257b42010-06-03 16:22:29 +10002661 */
Dave Chinner71e3e352020-06-29 14:49:18 -07002662 for (i = 0; i < igeo->inodes_per_cluster; i++)
2663 xfs_ifree_mark_inode_stale(bp, free_ip, inum + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
Dave Chinner5b3eed72010-08-24 11:42:41 +10002665 xfs_trans_stale_inode_buf(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 xfs_trans_binval(tp, bp);
2667 }
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00002668 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669}
2670
2671/*
2672 * This is called to return an inode to the inode free list.
2673 * The inode should already be truncated to 0 length and have
2674 * no pages associated with it. This routine also assumes that
2675 * the inode is already a part of the transaction.
2676 *
2677 * The on-disk copy of the inode will have been added to the list
2678 * of unlinked inodes in the AGI. We need to remove the inode from
2679 * that list atomically with respect to freeing it here.
2680 */
2681int
2682xfs_ifree(
Brian Foster0e0417f2018-07-11 22:26:07 -07002683 struct xfs_trans *tp,
2684 struct xfs_inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685{
2686 int error;
Brian Foster09b56602015-05-29 09:26:03 +10002687 struct xfs_icluster xic = { 0 };
Dave Chinner1319ebe2020-06-29 14:48:46 -07002688 struct xfs_inode_log_item *iip = ip->i_itemp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002690 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Dave Chinner54d7b5c2016-02-09 16:54:58 +11002691 ASSERT(VFS_I(ip)->i_nlink == 0);
Christoph Hellwigdaf83962020-05-18 10:27:22 -07002692 ASSERT(ip->i_df.if_nextents == 0);
Dave Chinnerc19b3b052016-02-09 16:54:58 +11002693 ASSERT(ip->i_d.di_size == 0 || !S_ISREG(VFS_I(ip)->i_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 ASSERT(ip->i_d.di_nblocks == 0);
2695
2696 /*
2697 * Pull the on-disk inode from the AGI unlinked list.
2698 */
2699 error = xfs_iunlink_remove(tp, ip);
Dave Chinner1baaed82013-06-27 16:04:50 +10002700 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
Brian Foster0e0417f2018-07-11 22:26:07 -07002703 error = xfs_difree(tp, ip->i_ino, &xic);
Dave Chinner1baaed82013-06-27 16:04:50 +10002704 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 return error;
Dave Chinner1baaed82013-06-27 16:04:50 +10002706
Christoph Hellwigb2c20042020-05-18 10:27:21 -07002707 /*
2708 * Free any local-format data sitting around before we reset the
2709 * data fork to extents format. Note that the attr fork data has
2710 * already been freed by xfs_attr_inactive.
2711 */
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07002712 if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
Christoph Hellwigb2c20042020-05-18 10:27:21 -07002713 kmem_free(ip->i_df.if_u1.if_data);
2714 ip->i_df.if_u1.if_data = NULL;
2715 ip->i_df.if_bytes = 0;
2716 }
Darrick J. Wong98c4f782017-11-22 12:21:07 -08002717
Dave Chinnerc19b3b052016-02-09 16:54:58 +11002718 VFS_I(ip)->i_mode = 0; /* mark incore inode as free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 ip->i_d.di_flags = 0;
Darrick J. Wongf93e54362020-08-17 09:59:07 -07002720 ip->i_d.di_flags2 = ip->i_mount->m_ino_geo.new_diflags2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 ip->i_d.di_dmevmask = 0;
2722 ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07002723 ip->i_df.if_format = XFS_DINODE_FMT_EXTENTS;
Eric Sandeendc1baa72018-03-28 17:48:08 -07002724
2725 /* Don't attempt to replay owner changes for a deleted inode */
Dave Chinner1319ebe2020-06-29 14:48:46 -07002726 spin_lock(&iip->ili_lock);
2727 iip->ili_fields &= ~(XFS_ILOG_AOWNER | XFS_ILOG_DOWNER);
2728 spin_unlock(&iip->ili_lock);
Eric Sandeendc1baa72018-03-28 17:48:08 -07002729
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 /*
2731 * Bump the generation count so no one will be confused
2732 * by reincarnations of this inode.
2733 */
Dave Chinner9e9a2672016-02-09 16:54:58 +11002734 VFS_I(ip)->i_generation++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2736
Brian Foster09b56602015-05-29 09:26:03 +10002737 if (xic.deleted)
2738 error = xfs_ifree_cluster(ip, tp, &xic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00002740 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741}
2742
2743/*
Christoph Hellwig60ec6782010-02-17 19:43:56 +00002744 * This is called to unpin an inode. The caller must have the inode locked
2745 * in at least shared mode so that the buffer cannot be subsequently pinned
2746 * once someone is waiting for it to be unpinned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 */
Christoph Hellwig60ec6782010-02-17 19:43:56 +00002748static void
Christoph Hellwigf392e632011-12-18 20:00:10 +00002749xfs_iunpin(
Christoph Hellwig60ec6782010-02-17 19:43:56 +00002750 struct xfs_inode *ip)
David Chinnera3f74ff2008-03-06 13:43:42 +11002751{
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002752 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
David Chinnera3f74ff2008-03-06 13:43:42 +11002753
Dave Chinner4aaf15d2010-03-08 11:24:07 +11002754 trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
2755
David Chinnera3f74ff2008-03-06 13:43:42 +11002756 /* Give the log a push to start the unpinning I/O */
Christoph Hellwig656de4f2018-03-13 23:15:28 -07002757 xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0, NULL);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002758
David Chinnera3f74ff2008-03-06 13:43:42 +11002759}
2760
Christoph Hellwigf392e632011-12-18 20:00:10 +00002761static void
2762__xfs_iunpin_wait(
2763 struct xfs_inode *ip)
2764{
2765 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
2766 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
2767
2768 xfs_iunpin(ip);
2769
2770 do {
Ingo Molnar21417132017-03-05 11:25:39 +01002771 prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
Christoph Hellwigf392e632011-12-18 20:00:10 +00002772 if (xfs_ipincount(ip))
2773 io_schedule();
2774 } while (xfs_ipincount(ip));
Ingo Molnar21417132017-03-05 11:25:39 +01002775 finish_wait(wq, &wait.wq_entry);
Christoph Hellwigf392e632011-12-18 20:00:10 +00002776}
2777
Dave Chinner777df5a2010-02-06 12:37:26 +11002778void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779xfs_iunpin_wait(
Christoph Hellwig60ec6782010-02-17 19:43:56 +00002780 struct xfs_inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781{
Christoph Hellwigf392e632011-12-18 20:00:10 +00002782 if (xfs_ipincount(ip))
2783 __xfs_iunpin_wait(ip);
David Chinnera3f74ff2008-03-06 13:43:42 +11002784}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
Dave Chinner27320362013-10-29 22:11:44 +11002786/*
2787 * Removing an inode from the namespace involves removing the directory entry
2788 * and dropping the link count on the inode. Removing the directory entry can
2789 * result in locking an AGF (directory blocks were freed) and removing a link
2790 * count can result in placing the inode on an unlinked list which results in
2791 * locking an AGI.
2792 *
2793 * The big problem here is that we have an ordering constraint on AGF and AGI
2794 * locking - inode allocation locks the AGI, then can allocate a new extent for
2795 * new inodes, locking the AGF after the AGI. Similarly, freeing the inode
2796 * removes the inode from the unlinked list, requiring that we lock the AGI
2797 * first, and then freeing the inode can result in an inode chunk being freed
2798 * and hence freeing disk space requiring that we lock an AGF.
2799 *
2800 * Hence the ordering that is imposed by other parts of the code is AGI before
2801 * AGF. This means we cannot remove the directory entry before we drop the inode
2802 * reference count and put it on the unlinked list as this results in a lock
2803 * order of AGF then AGI, and this can deadlock against inode allocation and
2804 * freeing. Therefore we must drop the link counts before we remove the
2805 * directory entry.
2806 *
2807 * This is still safe from a transactional point of view - it is not until we
Darrick J. Wong310a75a2016-08-03 11:18:10 +10002808 * get to xfs_defer_finish() that we have the possibility of multiple
Dave Chinner27320362013-10-29 22:11:44 +11002809 * transactions in this operation. Hence as long as we remove the directory
2810 * entry and drop the link count in the first transaction of the remove
2811 * operation, there are no transactional constraints on the ordering here.
2812 */
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002813int
2814xfs_remove(
2815 xfs_inode_t *dp,
2816 struct xfs_name *name,
2817 xfs_inode_t *ip)
2818{
2819 xfs_mount_t *mp = dp->i_mount;
2820 xfs_trans_t *tp = NULL;
Dave Chinnerc19b3b052016-02-09 16:54:58 +11002821 int is_dir = S_ISDIR(VFS_I(ip)->i_mode);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002822 int error = 0;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002823 uint resblks;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002824
2825 trace_xfs_remove(dp, name);
2826
2827 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10002828 return -EIO;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002829
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -07002830 error = xfs_qm_dqattach(dp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002831 if (error)
2832 goto std_return;
2833
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -07002834 error = xfs_qm_dqattach(ip);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002835 if (error)
2836 goto std_return;
2837
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002838 /*
2839 * We try to get the real space reservation first,
2840 * allowing for directory btree deletion(s) implying
2841 * possible bmap insert(s). If we can't get the space
2842 * reservation then we use 0 instead, and avoid the bmap
2843 * btree insert(s) in the directory code by, if the bmap
2844 * insert tries to happen, instead trimming the LAST
2845 * block from the directory.
2846 */
2847 resblks = XFS_REMOVE_SPACE_RES(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10002848 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_remove, resblks, 0, 0, &tp);
Dave Chinner24513372014-06-25 14:58:08 +10002849 if (error == -ENOSPC) {
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002850 resblks = 0;
Christoph Hellwig253f4912016-04-06 09:19:55 +10002851 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_remove, 0, 0, 0,
2852 &tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002853 }
2854 if (error) {
Dave Chinner24513372014-06-25 14:58:08 +10002855 ASSERT(error != -ENOSPC);
Christoph Hellwig253f4912016-04-06 09:19:55 +10002856 goto std_return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002857 }
2858
Darrick J. Wong7c2d2382018-01-26 15:27:33 -08002859 xfs_lock_two_inodes(dp, XFS_ILOCK_EXCL, ip, XFS_ILOCK_EXCL);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002860
Christoph Hellwig65523212016-11-30 14:33:25 +11002861 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002862 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2863
2864 /*
2865 * If we're removing a directory perform some additional validation.
2866 */
2867 if (is_dir) {
Dave Chinner54d7b5c2016-02-09 16:54:58 +11002868 ASSERT(VFS_I(ip)->i_nlink >= 2);
2869 if (VFS_I(ip)->i_nlink != 2) {
Dave Chinner24513372014-06-25 14:58:08 +10002870 error = -ENOTEMPTY;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002871 goto out_trans_cancel;
2872 }
2873 if (!xfs_dir_isempty(ip)) {
Dave Chinner24513372014-06-25 14:58:08 +10002874 error = -ENOTEMPTY;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002875 goto out_trans_cancel;
2876 }
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002877
Dave Chinner27320362013-10-29 22:11:44 +11002878 /* Drop the link from ip's "..". */
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002879 error = xfs_droplink(tp, dp);
2880 if (error)
Dave Chinner27320362013-10-29 22:11:44 +11002881 goto out_trans_cancel;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002882
Dave Chinner27320362013-10-29 22:11:44 +11002883 /* Drop the "." link from ip to self. */
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002884 error = xfs_droplink(tp, ip);
2885 if (error)
Dave Chinner27320362013-10-29 22:11:44 +11002886 goto out_trans_cancel;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002887 } else {
2888 /*
2889 * When removing a non-directory we need to log the parent
2890 * inode here. For a directory this is done implicitly
2891 * by the xfs_droplink call for the ".." entry.
2892 */
2893 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2894 }
Dave Chinner27320362013-10-29 22:11:44 +11002895 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002896
Dave Chinner27320362013-10-29 22:11:44 +11002897 /* Drop the link from dp to ip. */
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002898 error = xfs_droplink(tp, ip);
2899 if (error)
Dave Chinner27320362013-10-29 22:11:44 +11002900 goto out_trans_cancel;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002901
Brian Foster381eee62018-07-11 22:26:21 -07002902 error = xfs_dir_removename(tp, dp, name, ip->i_ino, resblks);
Dave Chinner27320362013-10-29 22:11:44 +11002903 if (error) {
Dave Chinner24513372014-06-25 14:58:08 +10002904 ASSERT(error != -ENOENT);
Brian Fosterc8eac492018-07-24 13:43:13 -07002905 goto out_trans_cancel;
Dave Chinner27320362013-10-29 22:11:44 +11002906 }
2907
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002908 /*
2909 * If this is a synchronous mount, make sure that the
2910 * remove transaction goes to disk before returning to
2911 * the user.
2912 */
2913 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
2914 xfs_trans_set_sync(tp);
2915
Christoph Hellwig70393312015-06-04 13:48:08 +10002916 error = xfs_trans_commit(tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002917 if (error)
2918 goto std_return;
2919
Christoph Hellwig2cd2ef62014-04-23 07:11:51 +10002920 if (is_dir && xfs_inode_is_filestream(ip))
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002921 xfs_filestream_deassociate(ip);
2922
2923 return 0;
2924
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002925 out_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10002926 xfs_trans_cancel(tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002927 std_return:
2928 return error;
2929}
2930
Dave Chinnerf6bba202013-08-12 20:49:46 +10002931/*
2932 * Enter all inodes for a rename transaction into a sorted array.
2933 */
Dave Chinner95afcf52015-03-25 14:03:32 +11002934#define __XFS_SORT_INODES 5
Dave Chinnerf6bba202013-08-12 20:49:46 +10002935STATIC void
2936xfs_sort_for_rename(
Dave Chinner95afcf52015-03-25 14:03:32 +11002937 struct xfs_inode *dp1, /* in: old (source) directory inode */
2938 struct xfs_inode *dp2, /* in: new (target) directory inode */
2939 struct xfs_inode *ip1, /* in: inode of old entry */
2940 struct xfs_inode *ip2, /* in: inode of new entry */
2941 struct xfs_inode *wip, /* in: whiteout inode */
2942 struct xfs_inode **i_tab,/* out: sorted array of inodes */
2943 int *num_inodes) /* in/out: inodes in array */
Dave Chinnerf6bba202013-08-12 20:49:46 +10002944{
Dave Chinnerf6bba202013-08-12 20:49:46 +10002945 int i, j;
2946
Dave Chinner95afcf52015-03-25 14:03:32 +11002947 ASSERT(*num_inodes == __XFS_SORT_INODES);
2948 memset(i_tab, 0, *num_inodes * sizeof(struct xfs_inode *));
2949
Dave Chinnerf6bba202013-08-12 20:49:46 +10002950 /*
2951 * i_tab contains a list of pointers to inodes. We initialize
2952 * the table here & we'll sort it. We will then use it to
2953 * order the acquisition of the inode locks.
2954 *
2955 * Note that the table may contain duplicates. e.g., dp1 == dp2.
2956 */
Dave Chinner95afcf52015-03-25 14:03:32 +11002957 i = 0;
2958 i_tab[i++] = dp1;
2959 i_tab[i++] = dp2;
2960 i_tab[i++] = ip1;
2961 if (ip2)
2962 i_tab[i++] = ip2;
2963 if (wip)
2964 i_tab[i++] = wip;
2965 *num_inodes = i;
Dave Chinnerf6bba202013-08-12 20:49:46 +10002966
2967 /*
2968 * Sort the elements via bubble sort. (Remember, there are at
Dave Chinner95afcf52015-03-25 14:03:32 +11002969 * most 5 elements to sort, so this is adequate.)
Dave Chinnerf6bba202013-08-12 20:49:46 +10002970 */
2971 for (i = 0; i < *num_inodes; i++) {
2972 for (j = 1; j < *num_inodes; j++) {
2973 if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) {
Dave Chinner95afcf52015-03-25 14:03:32 +11002974 struct xfs_inode *temp = i_tab[j];
Dave Chinnerf6bba202013-08-12 20:49:46 +10002975 i_tab[j] = i_tab[j-1];
2976 i_tab[j-1] = temp;
2977 }
2978 }
2979 }
2980}
2981
Dave Chinner310606b2015-03-25 14:06:07 +11002982static int
2983xfs_finish_rename(
Brian Fosterc9cfdb32018-07-11 22:26:08 -07002984 struct xfs_trans *tp)
Dave Chinner310606b2015-03-25 14:06:07 +11002985{
Dave Chinner310606b2015-03-25 14:06:07 +11002986 /*
2987 * If this is a synchronous mount, make sure that the rename transaction
2988 * goes to disk before returning to the user.
2989 */
2990 if (tp->t_mountp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
2991 xfs_trans_set_sync(tp);
2992
Christoph Hellwig70393312015-06-04 13:48:08 +10002993 return xfs_trans_commit(tp);
Dave Chinner310606b2015-03-25 14:06:07 +11002994}
2995
Dave Chinnerf6bba202013-08-12 20:49:46 +10002996/*
Carlos Maiolinod31a1822014-12-24 08:51:42 +11002997 * xfs_cross_rename()
2998 *
2999 * responsible for handling RENAME_EXCHANGE flag in renameat2() sytemcall
3000 */
3001STATIC int
3002xfs_cross_rename(
3003 struct xfs_trans *tp,
3004 struct xfs_inode *dp1,
3005 struct xfs_name *name1,
3006 struct xfs_inode *ip1,
3007 struct xfs_inode *dp2,
3008 struct xfs_name *name2,
3009 struct xfs_inode *ip2,
Carlos Maiolinod31a1822014-12-24 08:51:42 +11003010 int spaceres)
3011{
3012 int error = 0;
3013 int ip1_flags = 0;
3014 int ip2_flags = 0;
3015 int dp2_flags = 0;
3016
3017 /* Swap inode number for dirent in first parent */
Brian Foster381eee62018-07-11 22:26:21 -07003018 error = xfs_dir_replace(tp, dp1, name1, ip2->i_ino, spaceres);
Carlos Maiolinod31a1822014-12-24 08:51:42 +11003019 if (error)
Dave Chinnereeacd322015-03-25 14:08:07 +11003020 goto out_trans_abort;
Carlos Maiolinod31a1822014-12-24 08:51:42 +11003021
3022 /* Swap inode number for dirent in second parent */
Brian Foster381eee62018-07-11 22:26:21 -07003023 error = xfs_dir_replace(tp, dp2, name2, ip1->i_ino, spaceres);
Carlos Maiolinod31a1822014-12-24 08:51:42 +11003024 if (error)
Dave Chinnereeacd322015-03-25 14:08:07 +11003025 goto out_trans_abort;
Carlos Maiolinod31a1822014-12-24 08:51:42 +11003026
3027 /*
3028 * If we're renaming one or more directories across different parents,
3029 * update the respective ".." entries (and link counts) to match the new
3030 * parents.
3031 */
3032 if (dp1 != dp2) {
3033 dp2_flags = XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
3034
Dave Chinnerc19b3b052016-02-09 16:54:58 +11003035 if (S_ISDIR(VFS_I(ip2)->i_mode)) {
Carlos Maiolinod31a1822014-12-24 08:51:42 +11003036 error = xfs_dir_replace(tp, ip2, &xfs_name_dotdot,
Brian Foster381eee62018-07-11 22:26:21 -07003037 dp1->i_ino, spaceres);
Carlos Maiolinod31a1822014-12-24 08:51:42 +11003038 if (error)
Dave Chinnereeacd322015-03-25 14:08:07 +11003039 goto out_trans_abort;
Carlos Maiolinod31a1822014-12-24 08:51:42 +11003040
3041 /* transfer ip2 ".." reference to dp1 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +11003042 if (!S_ISDIR(VFS_I(ip1)->i_mode)) {
Carlos Maiolinod31a1822014-12-24 08:51:42 +11003043 error = xfs_droplink(tp, dp2);
3044 if (error)
Dave Chinnereeacd322015-03-25 14:08:07 +11003045 goto out_trans_abort;
Eric Sandeen91083262019-05-01 20:26:30 -07003046 xfs_bumplink(tp, dp1);
Carlos Maiolinod31a1822014-12-24 08:51:42 +11003047 }
3048
3049 /*
3050 * Although ip1 isn't changed here, userspace needs
3051 * to be warned about the change, so that applications
3052 * relying on it (like backup ones), will properly
3053 * notify the change
3054 */
3055 ip1_flags |= XFS_ICHGTIME_CHG;
3056 ip2_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
3057 }
3058
Dave Chinnerc19b3b052016-02-09 16:54:58 +11003059 if (S_ISDIR(VFS_I(ip1)->i_mode)) {
Carlos Maiolinod31a1822014-12-24 08:51:42 +11003060 error = xfs_dir_replace(tp, ip1, &xfs_name_dotdot,
Brian Foster381eee62018-07-11 22:26:21 -07003061 dp2->i_ino, spaceres);
Carlos Maiolinod31a1822014-12-24 08:51:42 +11003062 if (error)
Dave Chinnereeacd322015-03-25 14:08:07 +11003063 goto out_trans_abort;
Carlos Maiolinod31a1822014-12-24 08:51:42 +11003064
3065 /* transfer ip1 ".." reference to dp2 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +11003066 if (!S_ISDIR(VFS_I(ip2)->i_mode)) {
Carlos Maiolinod31a1822014-12-24 08:51:42 +11003067 error = xfs_droplink(tp, dp1);
3068 if (error)
Dave Chinnereeacd322015-03-25 14:08:07 +11003069 goto out_trans_abort;
Eric Sandeen91083262019-05-01 20:26:30 -07003070 xfs_bumplink(tp, dp2);
Carlos Maiolinod31a1822014-12-24 08:51:42 +11003071 }
3072
3073 /*
3074 * Although ip2 isn't changed here, userspace needs
3075 * to be warned about the change, so that applications
3076 * relying on it (like backup ones), will properly
3077 * notify the change
3078 */
3079 ip1_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
3080 ip2_flags |= XFS_ICHGTIME_CHG;
3081 }
3082 }
3083
3084 if (ip1_flags) {
3085 xfs_trans_ichgtime(tp, ip1, ip1_flags);
3086 xfs_trans_log_inode(tp, ip1, XFS_ILOG_CORE);
3087 }
3088 if (ip2_flags) {
3089 xfs_trans_ichgtime(tp, ip2, ip2_flags);
3090 xfs_trans_log_inode(tp, ip2, XFS_ILOG_CORE);
3091 }
3092 if (dp2_flags) {
3093 xfs_trans_ichgtime(tp, dp2, dp2_flags);
3094 xfs_trans_log_inode(tp, dp2, XFS_ILOG_CORE);
3095 }
3096 xfs_trans_ichgtime(tp, dp1, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3097 xfs_trans_log_inode(tp, dp1, XFS_ILOG_CORE);
Brian Fosterc9cfdb32018-07-11 22:26:08 -07003098 return xfs_finish_rename(tp);
Dave Chinnereeacd322015-03-25 14:08:07 +11003099
3100out_trans_abort:
Christoph Hellwig4906e212015-06-04 13:47:56 +10003101 xfs_trans_cancel(tp);
Carlos Maiolinod31a1822014-12-24 08:51:42 +11003102 return error;
3103}
3104
3105/*
Dave Chinner7dcf5c32015-03-25 14:08:08 +11003106 * xfs_rename_alloc_whiteout()
3107 *
Randy Dunlapb63da6c2020-08-05 08:49:58 -07003108 * Return a referenced, unlinked, unlocked inode that can be used as a
Dave Chinner7dcf5c32015-03-25 14:08:08 +11003109 * whiteout in a rename transaction. We use a tmpfile inode here so that if we
3110 * crash between allocating the inode and linking it into the rename transaction
3111 * recovery will free the inode and we won't leak it.
3112 */
3113static int
3114xfs_rename_alloc_whiteout(
3115 struct xfs_inode *dp,
3116 struct xfs_inode **wip)
3117{
3118 struct xfs_inode *tmpfile;
3119 int error;
3120
Eric Sandeena1f69412018-04-06 10:09:42 -07003121 error = xfs_create_tmpfile(dp, S_IFCHR | WHITEOUT_MODE, &tmpfile);
Dave Chinner7dcf5c32015-03-25 14:08:08 +11003122 if (error)
3123 return error;
3124
Brian Foster22419ac2015-05-29 08:14:55 +10003125 /*
3126 * Prepare the tmpfile inode as if it were created through the VFS.
Darrick J. Wongc4a6bf72019-02-13 11:15:17 -08003127 * Complete the inode setup and flag it as linkable. nlink is already
3128 * zero, so we can skip the drop_nlink.
Brian Foster22419ac2015-05-29 08:14:55 +10003129 */
Christoph Hellwig2b3d1d42016-04-06 07:48:27 +10003130 xfs_setup_iops(tmpfile);
Dave Chinner7dcf5c32015-03-25 14:08:08 +11003131 xfs_finish_inode_setup(tmpfile);
3132 VFS_I(tmpfile)->i_state |= I_LINKABLE;
3133
3134 *wip = tmpfile;
3135 return 0;
3136}
3137
3138/*
Dave Chinnerf6bba202013-08-12 20:49:46 +10003139 * xfs_rename
3140 */
3141int
3142xfs_rename(
Dave Chinner7dcf5c32015-03-25 14:08:08 +11003143 struct xfs_inode *src_dp,
3144 struct xfs_name *src_name,
3145 struct xfs_inode *src_ip,
3146 struct xfs_inode *target_dp,
3147 struct xfs_name *target_name,
3148 struct xfs_inode *target_ip,
3149 unsigned int flags)
Dave Chinnerf6bba202013-08-12 20:49:46 +10003150{
Dave Chinner7dcf5c32015-03-25 14:08:08 +11003151 struct xfs_mount *mp = src_dp->i_mount;
3152 struct xfs_trans *tp;
Dave Chinner7dcf5c32015-03-25 14:08:08 +11003153 struct xfs_inode *wip = NULL; /* whiteout inode */
3154 struct xfs_inode *inodes[__XFS_SORT_INODES];
kaixuxia93597ae2019-11-12 08:34:23 -08003155 struct xfs_buf *agibp;
Dave Chinner7dcf5c32015-03-25 14:08:08 +11003156 int num_inodes = __XFS_SORT_INODES;
Dave Chinner2b936812015-03-25 15:12:30 +11003157 bool new_parent = (src_dp != target_dp);
Dave Chinnerc19b3b052016-02-09 16:54:58 +11003158 bool src_is_directory = S_ISDIR(VFS_I(src_ip)->i_mode);
Dave Chinner7dcf5c32015-03-25 14:08:08 +11003159 int spaceres;
3160 int error;
Dave Chinnerf6bba202013-08-12 20:49:46 +10003161
3162 trace_xfs_rename(src_dp, target_dp, src_name, target_name);
3163
Dave Chinnereeacd322015-03-25 14:08:07 +11003164 if ((flags & RENAME_EXCHANGE) && !target_ip)
3165 return -EINVAL;
Dave Chinnerf6bba202013-08-12 20:49:46 +10003166
Dave Chinner7dcf5c32015-03-25 14:08:08 +11003167 /*
3168 * If we are doing a whiteout operation, allocate the whiteout inode
3169 * we will be placing at the target and ensure the type is set
3170 * appropriately.
3171 */
3172 if (flags & RENAME_WHITEOUT) {
3173 ASSERT(!(flags & (RENAME_NOREPLACE | RENAME_EXCHANGE)));
3174 error = xfs_rename_alloc_whiteout(target_dp, &wip);
3175 if (error)
3176 return error;
Dave Chinnerf6bba202013-08-12 20:49:46 +10003177
Dave Chinner7dcf5c32015-03-25 14:08:08 +11003178 /* setup target dirent info as whiteout */
3179 src_name->type = XFS_DIR3_FT_CHRDEV;
3180 }
3181
3182 xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip, wip,
Dave Chinnerf6bba202013-08-12 20:49:46 +10003183 inodes, &num_inodes);
3184
Dave Chinnerf6bba202013-08-12 20:49:46 +10003185 spaceres = XFS_RENAME_SPACE_RES(mp, target_name->len);
Christoph Hellwig253f4912016-04-06 09:19:55 +10003186 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_rename, spaceres, 0, 0, &tp);
Dave Chinner24513372014-06-25 14:58:08 +10003187 if (error == -ENOSPC) {
Dave Chinnerf6bba202013-08-12 20:49:46 +10003188 spaceres = 0;
Christoph Hellwig253f4912016-04-06 09:19:55 +10003189 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_rename, 0, 0, 0,
3190 &tp);
Dave Chinnerf6bba202013-08-12 20:49:46 +10003191 }
Dave Chinner445883e2015-03-25 14:05:43 +11003192 if (error)
Christoph Hellwig253f4912016-04-06 09:19:55 +10003193 goto out_release_wip;
Dave Chinnerf6bba202013-08-12 20:49:46 +10003194
3195 /*
3196 * Attach the dquots to the inodes
3197 */
3198 error = xfs_qm_vop_rename_dqattach(inodes);
Dave Chinner445883e2015-03-25 14:05:43 +11003199 if (error)
3200 goto out_trans_cancel;
Dave Chinnerf6bba202013-08-12 20:49:46 +10003201
3202 /*
3203 * Lock all the participating inodes. Depending upon whether
3204 * the target_name exists in the target directory, and
3205 * whether the target directory is the same as the source
3206 * directory, we can lock from 2 to 4 inodes.
3207 */
3208 xfs_lock_inodes(inodes, num_inodes, XFS_ILOCK_EXCL);
3209
3210 /*
3211 * Join all the inodes to the transaction. From this point on,
3212 * we can rely on either trans_commit or trans_cancel to unlock
3213 * them.
3214 */
Christoph Hellwig65523212016-11-30 14:33:25 +11003215 xfs_trans_ijoin(tp, src_dp, XFS_ILOCK_EXCL);
Dave Chinnerf6bba202013-08-12 20:49:46 +10003216 if (new_parent)
Christoph Hellwig65523212016-11-30 14:33:25 +11003217 xfs_trans_ijoin(tp, target_dp, XFS_ILOCK_EXCL);
Dave Chinnerf6bba202013-08-12 20:49:46 +10003218 xfs_trans_ijoin(tp, src_ip, XFS_ILOCK_EXCL);
3219 if (target_ip)
3220 xfs_trans_ijoin(tp, target_ip, XFS_ILOCK_EXCL);
Dave Chinner7dcf5c32015-03-25 14:08:08 +11003221 if (wip)
3222 xfs_trans_ijoin(tp, wip, XFS_ILOCK_EXCL);
Dave Chinnerf6bba202013-08-12 20:49:46 +10003223
3224 /*
3225 * If we are using project inheritance, we only allow renames
3226 * into our tree when the project IDs are the same; else the
3227 * tree quota mechanism would be circumvented.
3228 */
3229 if (unlikely((target_dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
Christoph Hellwigde7a8662019-11-12 08:22:54 -08003230 target_dp->i_d.di_projid != src_ip->i_d.di_projid)) {
Dave Chinner24513372014-06-25 14:58:08 +10003231 error = -EXDEV;
Dave Chinner445883e2015-03-25 14:05:43 +11003232 goto out_trans_cancel;
Dave Chinnerf6bba202013-08-12 20:49:46 +10003233 }
3234
Dave Chinnereeacd322015-03-25 14:08:07 +11003235 /* RENAME_EXCHANGE is unique from here on. */
3236 if (flags & RENAME_EXCHANGE)
3237 return xfs_cross_rename(tp, src_dp, src_name, src_ip,
3238 target_dp, target_name, target_ip,
Brian Fosterf16dea52018-07-11 22:26:20 -07003239 spaceres);
Carlos Maiolinod31a1822014-12-24 08:51:42 +11003240
3241 /*
kaixuxiabc56ad82019-09-03 21:06:50 -07003242 * Check for expected errors before we dirty the transaction
3243 * so we can return an error without a transaction abort.
Dave Chinnerf6bba202013-08-12 20:49:46 +10003244 */
3245 if (target_ip == NULL) {
3246 /*
3247 * If there's no space reservation, check the entry will
3248 * fit before actually inserting it.
3249 */
Eric Sandeen94f3cad2014-09-09 11:57:52 +10003250 if (!spaceres) {
3251 error = xfs_dir_canenter(tp, target_dp, target_name);
3252 if (error)
Dave Chinner445883e2015-03-25 14:05:43 +11003253 goto out_trans_cancel;
Eric Sandeen94f3cad2014-09-09 11:57:52 +10003254 }
kaixuxiabc56ad82019-09-03 21:06:50 -07003255 } else {
3256 /*
3257 * If target exists and it's a directory, check that whether
3258 * it can be destroyed.
3259 */
3260 if (S_ISDIR(VFS_I(target_ip)->i_mode) &&
3261 (!xfs_dir_isempty(target_ip) ||
3262 (VFS_I(target_ip)->i_nlink > 2))) {
3263 error = -EEXIST;
3264 goto out_trans_cancel;
3265 }
3266 }
3267
3268 /*
3269 * Directory entry creation below may acquire the AGF. Remove
3270 * the whiteout from the unlinked list first to preserve correct
3271 * AGI/AGF locking order. This dirties the transaction so failures
3272 * after this point will abort and log recovery will clean up the
3273 * mess.
3274 *
3275 * For whiteouts, we need to bump the link count on the whiteout
3276 * inode. After this point, we have a real link, clear the tmpfile
3277 * state flag from the inode so it doesn't accidentally get misused
3278 * in future.
3279 */
3280 if (wip) {
3281 ASSERT(VFS_I(wip)->i_nlink == 0);
3282 error = xfs_iunlink_remove(tp, wip);
3283 if (error)
3284 goto out_trans_cancel;
3285
3286 xfs_bumplink(tp, wip);
kaixuxiabc56ad82019-09-03 21:06:50 -07003287 VFS_I(wip)->i_state &= ~I_LINKABLE;
3288 }
3289
3290 /*
3291 * Set up the target.
3292 */
3293 if (target_ip == NULL) {
Dave Chinnerf6bba202013-08-12 20:49:46 +10003294 /*
3295 * If target does not exist and the rename crosses
3296 * directories, adjust the target directory link count
3297 * to account for the ".." reference from the new entry.
3298 */
3299 error = xfs_dir_createname(tp, target_dp, target_name,
Brian Foster381eee62018-07-11 22:26:21 -07003300 src_ip->i_ino, spaceres);
Dave Chinnerf6bba202013-08-12 20:49:46 +10003301 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -07003302 goto out_trans_cancel;
Dave Chinnerf6bba202013-08-12 20:49:46 +10003303
3304 xfs_trans_ichgtime(tp, target_dp,
3305 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3306
3307 if (new_parent && src_is_directory) {
Eric Sandeen91083262019-05-01 20:26:30 -07003308 xfs_bumplink(tp, target_dp);
Dave Chinnerf6bba202013-08-12 20:49:46 +10003309 }
3310 } else { /* target_ip != NULL */
3311 /*
Dave Chinnerf6bba202013-08-12 20:49:46 +10003312 * Link the source inode under the target name.
3313 * If the source inode is a directory and we are moving
3314 * it across directories, its ".." entry will be
3315 * inconsistent until we replace that down below.
3316 *
3317 * In case there is already an entry with the same
3318 * name at the destination directory, remove it first.
3319 */
kaixuxia93597ae2019-11-12 08:34:23 -08003320
3321 /*
3322 * Check whether the replace operation will need to allocate
3323 * blocks. This happens when the shortform directory lacks
3324 * space and we have to convert it to a block format directory.
3325 * When more blocks are necessary, we must lock the AGI first
3326 * to preserve locking order (AGI -> AGF).
3327 */
3328 if (xfs_dir2_sf_replace_needblock(target_dp, src_ip->i_ino)) {
3329 error = xfs_read_agi(mp, tp,
3330 XFS_INO_TO_AGNO(mp, target_ip->i_ino),
3331 &agibp);
3332 if (error)
3333 goto out_trans_cancel;
3334 }
3335
Dave Chinnerf6bba202013-08-12 20:49:46 +10003336 error = xfs_dir_replace(tp, target_dp, target_name,
Brian Foster381eee62018-07-11 22:26:21 -07003337 src_ip->i_ino, spaceres);
Dave Chinnerf6bba202013-08-12 20:49:46 +10003338 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -07003339 goto out_trans_cancel;
Dave Chinnerf6bba202013-08-12 20:49:46 +10003340
3341 xfs_trans_ichgtime(tp, target_dp,
3342 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3343
3344 /*
3345 * Decrement the link count on the target since the target
3346 * dir no longer points to it.
3347 */
3348 error = xfs_droplink(tp, target_ip);
3349 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -07003350 goto out_trans_cancel;
Dave Chinnerf6bba202013-08-12 20:49:46 +10003351
3352 if (src_is_directory) {
3353 /*
3354 * Drop the link from the old "." entry.
3355 */
3356 error = xfs_droplink(tp, target_ip);
3357 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -07003358 goto out_trans_cancel;
Dave Chinnerf6bba202013-08-12 20:49:46 +10003359 }
3360 } /* target_ip != NULL */
3361
3362 /*
3363 * Remove the source.
3364 */
3365 if (new_parent && src_is_directory) {
3366 /*
3367 * Rewrite the ".." entry to point to the new
3368 * directory.
3369 */
3370 error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot,
Brian Foster381eee62018-07-11 22:26:21 -07003371 target_dp->i_ino, spaceres);
Dave Chinner24513372014-06-25 14:58:08 +10003372 ASSERT(error != -EEXIST);
Dave Chinnerf6bba202013-08-12 20:49:46 +10003373 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -07003374 goto out_trans_cancel;
Dave Chinnerf6bba202013-08-12 20:49:46 +10003375 }
3376
3377 /*
3378 * We always want to hit the ctime on the source inode.
3379 *
3380 * This isn't strictly required by the standards since the source
3381 * inode isn't really being changed, but old unix file systems did
3382 * it and some incremental backup programs won't work without it.
3383 */
3384 xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG);
3385 xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE);
3386
3387 /*
3388 * Adjust the link count on src_dp. This is necessary when
3389 * renaming a directory, either within one parent when
3390 * the target existed, or across two parent directories.
3391 */
3392 if (src_is_directory && (new_parent || target_ip != NULL)) {
3393
3394 /*
3395 * Decrement link count on src_directory since the
3396 * entry that's moved no longer points to it.
3397 */
3398 error = xfs_droplink(tp, src_dp);
3399 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -07003400 goto out_trans_cancel;
Dave Chinnerf6bba202013-08-12 20:49:46 +10003401 }
3402
Dave Chinner7dcf5c32015-03-25 14:08:08 +11003403 /*
3404 * For whiteouts, we only need to update the source dirent with the
3405 * inode number of the whiteout inode rather than removing it
3406 * altogether.
3407 */
3408 if (wip) {
3409 error = xfs_dir_replace(tp, src_dp, src_name, wip->i_ino,
Brian Foster381eee62018-07-11 22:26:21 -07003410 spaceres);
Dave Chinner7dcf5c32015-03-25 14:08:08 +11003411 } else
3412 error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino,
Brian Foster381eee62018-07-11 22:26:21 -07003413 spaceres);
Dave Chinnerf6bba202013-08-12 20:49:46 +10003414 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -07003415 goto out_trans_cancel;
Dave Chinnerf6bba202013-08-12 20:49:46 +10003416
3417 xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3418 xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE);
3419 if (new_parent)
3420 xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE);
3421
Brian Fosterc9cfdb32018-07-11 22:26:08 -07003422 error = xfs_finish_rename(tp);
Dave Chinner7dcf5c32015-03-25 14:08:08 +11003423 if (wip)
Darrick J. Wong44a87362018-07-25 12:52:32 -07003424 xfs_irele(wip);
Dave Chinner7dcf5c32015-03-25 14:08:08 +11003425 return error;
Dave Chinnerf6bba202013-08-12 20:49:46 +10003426
Dave Chinner445883e2015-03-25 14:05:43 +11003427out_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10003428 xfs_trans_cancel(tp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10003429out_release_wip:
Dave Chinner7dcf5c32015-03-25 14:08:08 +11003430 if (wip)
Darrick J. Wong44a87362018-07-25 12:52:32 -07003431 xfs_irele(wip);
Dave Chinnerf6bba202013-08-12 20:49:46 +10003432 return error;
3433}
3434
Dave Chinnere6187b32020-06-29 14:49:19 -07003435static int
3436xfs_iflush(
Christoph Hellwig93848a92013-04-03 16:11:17 +11003437 struct xfs_inode *ip,
3438 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439{
Christoph Hellwig93848a92013-04-03 16:11:17 +11003440 struct xfs_inode_log_item *iip = ip->i_itemp;
3441 struct xfs_dinode *dip;
3442 struct xfs_mount *mp = ip->i_mount;
Brian Fosterf2019292020-05-06 13:25:20 -07003443 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10003445 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Dave Chinner718ecc52020-08-17 16:41:01 -07003446 ASSERT(xfs_iflags_test(ip, XFS_IFLUSHING));
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07003447 ASSERT(ip->i_df.if_format != XFS_DINODE_FMT_BTREE ||
Christoph Hellwigdaf83962020-05-18 10:27:22 -07003448 ip->i_df.if_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
Dave Chinner90c60e12020-06-29 14:49:19 -07003449 ASSERT(iip->ili_item.li_buf == bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10003451 dip = xfs_buf_offset(bp, ip->i_imap.im_boffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452
Brian Fosterf2019292020-05-06 13:25:20 -07003453 /*
3454 * We don't flush the inode if any of the following checks fail, but we
3455 * do still update the log item and attach to the backing buffer as if
3456 * the flush happened. This is a formality to facilitate predictable
3457 * error handling as the caller will shutdown and fail the buffer.
3458 */
3459 error = -EFSCORRUPTED;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02003460 if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07003461 mp, XFS_ERRTAG_IFLUSH_1)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11003462 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
Darrick J. Wongc9690042018-01-09 12:02:55 -08003463 "%s: Bad inode %Lu magic number 0x%x, ptr "PTR_FMT,
Dave Chinner6a19d932011-03-07 10:02:35 +11003464 __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
Brian Fosterf2019292020-05-06 13:25:20 -07003465 goto flush_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 }
Dave Chinnerc19b3b052016-02-09 16:54:58 +11003467 if (S_ISREG(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 if (XFS_TEST_ERROR(
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07003469 ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS &&
3470 ip->i_df.if_format != XFS_DINODE_FMT_BTREE,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07003471 mp, XFS_ERRTAG_IFLUSH_3)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11003472 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
Darrick J. Wongc9690042018-01-09 12:02:55 -08003473 "%s: Bad regular inode %Lu, ptr "PTR_FMT,
Dave Chinner6a19d932011-03-07 10:02:35 +11003474 __func__, ip->i_ino, ip);
Brian Fosterf2019292020-05-06 13:25:20 -07003475 goto flush_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 }
Dave Chinnerc19b3b052016-02-09 16:54:58 +11003477 } else if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 if (XFS_TEST_ERROR(
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07003479 ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS &&
3480 ip->i_df.if_format != XFS_DINODE_FMT_BTREE &&
3481 ip->i_df.if_format != XFS_DINODE_FMT_LOCAL,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07003482 mp, XFS_ERRTAG_IFLUSH_4)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11003483 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
Darrick J. Wongc9690042018-01-09 12:02:55 -08003484 "%s: Bad directory inode %Lu, ptr "PTR_FMT,
Dave Chinner6a19d932011-03-07 10:02:35 +11003485 __func__, ip->i_ino, ip);
Brian Fosterf2019292020-05-06 13:25:20 -07003486 goto flush_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 }
3488 }
Christoph Hellwigdaf83962020-05-18 10:27:22 -07003489 if (XFS_TEST_ERROR(ip->i_df.if_nextents + xfs_ifork_nextents(ip->i_afp) >
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07003490 ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11003491 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3492 "%s: detected corrupt incore inode %Lu, "
Darrick J. Wongc9690042018-01-09 12:02:55 -08003493 "total extents = %d, nblocks = %Ld, ptr "PTR_FMT,
Dave Chinner6a19d932011-03-07 10:02:35 +11003494 __func__, ip->i_ino,
Christoph Hellwigdaf83962020-05-18 10:27:22 -07003495 ip->i_df.if_nextents + xfs_ifork_nextents(ip->i_afp),
Dave Chinner6a19d932011-03-07 10:02:35 +11003496 ip->i_d.di_nblocks, ip);
Brian Fosterf2019292020-05-06 13:25:20 -07003497 goto flush_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 }
3499 if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07003500 mp, XFS_ERRTAG_IFLUSH_6)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11003501 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
Darrick J. Wongc9690042018-01-09 12:02:55 -08003502 "%s: bad inode %Lu, forkoff 0x%x, ptr "PTR_FMT,
Dave Chinner6a19d932011-03-07 10:02:35 +11003503 __func__, ip->i_ino, ip->i_d.di_forkoff, ip);
Brian Fosterf2019292020-05-06 13:25:20 -07003504 goto flush_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 }
Dave Chinnere60896d2013-07-24 15:47:30 +10003506
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 /*
Dave Chinner263997a2014-05-20 07:46:40 +10003508 * Inode item log recovery for v2 inodes are dependent on the
Dave Chinnere60896d2013-07-24 15:47:30 +10003509 * di_flushiter count for correct sequencing. We bump the flush
3510 * iteration count so we can detect flushes which postdate a log record
3511 * during recovery. This is redundant as we now log every change and
3512 * hence this can't happen but we need to still do it to ensure
3513 * backwards compatibility with old kernels that predate logging all
3514 * inode changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 */
Christoph Hellwig6471e9c2020-03-18 08:15:11 -07003516 if (!xfs_sb_version_has_v3inode(&mp->m_sb))
Dave Chinnere60896d2013-07-24 15:47:30 +10003517 ip->i_d.di_flushiter++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518
Christoph Hellwig0f45a1b2020-05-14 14:01:31 -07003519 /*
3520 * If there are inline format data / attr forks attached to this inode,
3521 * make sure they are not corrupt.
3522 */
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07003523 if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL &&
Christoph Hellwig0f45a1b2020-05-14 14:01:31 -07003524 xfs_ifork_verify_local_data(ip))
3525 goto flush_out;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -07003526 if (ip->i_afp && ip->i_afp->if_format == XFS_DINODE_FMT_LOCAL &&
Christoph Hellwig0f45a1b2020-05-14 14:01:31 -07003527 xfs_ifork_verify_local_attr(ip))
Brian Fosterf2019292020-05-06 13:25:20 -07003528 goto flush_out;
Darrick J. Wong005c5db2017-03-28 14:51:10 -07003529
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 /*
Dave Chinner39878482016-02-09 16:54:58 +11003531 * Copy the dirty parts of the inode into the on-disk inode. We always
3532 * copy out the core of the inode, because if the inode is dirty at all
3533 * the core must be.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 */
Dave Chinner93f958f2016-02-09 16:54:58 +11003535 xfs_inode_to_disk(ip, dip, iip->ili_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
3537 /* Wrap, we never let the log put out DI_MAX_FLUSH */
3538 if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
3539 ip->i_d.di_flushiter = 0;
3540
Darrick J. Wong005c5db2017-03-28 14:51:10 -07003541 xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK);
3542 if (XFS_IFORK_Q(ip))
3543 xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
3545 /*
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00003546 * We've recorded everything logged in the inode, so we'd like to clear
3547 * the ili_fields bits so we don't log and flush things unnecessarily.
3548 * However, we can't stop logging all this information until the data
3549 * we've copied into the disk buffer is written to disk. If we did we
3550 * might overwrite the copy of the inode in the log with all the data
3551 * after re-logging only part of it, and in the face of a crash we
3552 * wouldn't have all the data we need to recover.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 *
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00003554 * What we do is move the bits to the ili_last_fields field. When
3555 * logging the inode, these bits are moved back to the ili_fields field.
Christoph Hellwig664ffb82020-09-01 10:55:29 -07003556 * In the xfs_buf_inode_iodone() routine we clear ili_last_fields, since
3557 * we know that the information those bits represent is permanently on
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00003558 * disk. As long as the flush completes before the inode is logged
3559 * again, then both ili_fields and ili_last_fields will be cleared.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 */
Brian Fosterf2019292020-05-06 13:25:20 -07003561 error = 0;
3562flush_out:
Dave Chinner1319ebe2020-06-29 14:48:46 -07003563 spin_lock(&iip->ili_lock);
Christoph Hellwig93848a92013-04-03 16:11:17 +11003564 iip->ili_last_fields = iip->ili_fields;
3565 iip->ili_fields = 0;
Dave Chinnerfc0561c2015-11-03 13:14:59 +11003566 iip->ili_fsync_fields = 0;
Dave Chinner1319ebe2020-06-29 14:48:46 -07003567 spin_unlock(&iip->ili_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568
Dave Chinner1319ebe2020-06-29 14:48:46 -07003569 /*
3570 * Store the current LSN of the inode so that we can tell whether the
Christoph Hellwig664ffb82020-09-01 10:55:29 -07003571 * item has moved in the AIL from xfs_buf_inode_iodone().
Dave Chinner1319ebe2020-06-29 14:48:46 -07003572 */
Christoph Hellwig93848a92013-04-03 16:11:17 +11003573 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
3574 &iip->ili_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575
Christoph Hellwig93848a92013-04-03 16:11:17 +11003576 /* generate the checksum. */
3577 xfs_dinode_calc_crc(mp, dip);
Brian Fosterf2019292020-05-06 13:25:20 -07003578 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579}
Darrick J. Wong44a87362018-07-25 12:52:32 -07003580
Dave Chinnere6187b32020-06-29 14:49:19 -07003581/*
3582 * Non-blocking flush of dirty inode metadata into the backing buffer.
3583 *
3584 * The caller must have a reference to the inode and hold the cluster buffer
3585 * locked. The function will walk across all the inodes on the cluster buffer it
3586 * can find and lock without blocking, and flush them to the cluster buffer.
3587 *
Dave Chinner5717ea42020-06-29 14:49:20 -07003588 * On successful flushing of at least one inode, the caller must write out the
3589 * buffer and release it. If no inodes are flushed, -EAGAIN will be returned and
3590 * the caller needs to release the buffer. On failure, the filesystem will be
3591 * shut down, the buffer will have been unlocked and released, and EFSCORRUPTED
3592 * will be returned.
Dave Chinnere6187b32020-06-29 14:49:19 -07003593 */
3594int
3595xfs_iflush_cluster(
Dave Chinnere6187b32020-06-29 14:49:19 -07003596 struct xfs_buf *bp)
3597{
Dave Chinner5717ea42020-06-29 14:49:20 -07003598 struct xfs_mount *mp = bp->b_mount;
3599 struct xfs_log_item *lip, *n;
3600 struct xfs_inode *ip;
3601 struct xfs_inode_log_item *iip;
Dave Chinnere6187b32020-06-29 14:49:19 -07003602 int clcount = 0;
Dave Chinner5717ea42020-06-29 14:49:20 -07003603 int error = 0;
Dave Chinnere6187b32020-06-29 14:49:19 -07003604
Dave Chinner5717ea42020-06-29 14:49:20 -07003605 /*
3606 * We must use the safe variant here as on shutdown xfs_iflush_abort()
3607 * can remove itself from the list.
3608 */
3609 list_for_each_entry_safe(lip, n, &bp->b_li_list, li_bio_list) {
3610 iip = (struct xfs_inode_log_item *)lip;
3611 ip = iip->ili_inode;
Dave Chinnere6187b32020-06-29 14:49:19 -07003612
3613 /*
Dave Chinner5717ea42020-06-29 14:49:20 -07003614 * Quick and dirty check to avoid locks if possible.
Dave Chinnere6187b32020-06-29 14:49:19 -07003615 */
Dave Chinner718ecc52020-08-17 16:41:01 -07003616 if (__xfs_iflags_test(ip, XFS_IRECLAIM | XFS_IFLUSHING))
Dave Chinner5717ea42020-06-29 14:49:20 -07003617 continue;
3618 if (xfs_ipincount(ip))
3619 continue;
3620
3621 /*
3622 * The inode is still attached to the buffer, which means it is
3623 * dirty but reclaim might try to grab it. Check carefully for
3624 * that, and grab the ilock while still holding the i_flags_lock
3625 * to guarantee reclaim will not be able to reclaim this inode
3626 * once we drop the i_flags_lock.
3627 */
3628 spin_lock(&ip->i_flags_lock);
3629 ASSERT(!__xfs_iflags_test(ip, XFS_ISTALE));
Dave Chinner718ecc52020-08-17 16:41:01 -07003630 if (__xfs_iflags_test(ip, XFS_IRECLAIM | XFS_IFLUSHING)) {
Dave Chinner5717ea42020-06-29 14:49:20 -07003631 spin_unlock(&ip->i_flags_lock);
Dave Chinnere6187b32020-06-29 14:49:19 -07003632 continue;
3633 }
3634
3635 /*
Dave Chinner5717ea42020-06-29 14:49:20 -07003636 * ILOCK will pin the inode against reclaim and prevent
3637 * concurrent transactions modifying the inode while we are
Dave Chinner718ecc52020-08-17 16:41:01 -07003638 * flushing the inode. If we get the lock, set the flushing
3639 * state before we drop the i_flags_lock.
Dave Chinnere6187b32020-06-29 14:49:19 -07003640 */
Dave Chinner5717ea42020-06-29 14:49:20 -07003641 if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
3642 spin_unlock(&ip->i_flags_lock);
3643 continue;
3644 }
Dave Chinner718ecc52020-08-17 16:41:01 -07003645 __xfs_iflags_set(ip, XFS_IFLUSHING);
Dave Chinner5717ea42020-06-29 14:49:20 -07003646 spin_unlock(&ip->i_flags_lock);
3647
3648 /*
Dave Chinner5717ea42020-06-29 14:49:20 -07003649 * Abort flushing this inode if we are shut down because the
3650 * inode may not currently be in the AIL. This can occur when
3651 * log I/O failure unpins the inode without inserting into the
3652 * AIL, leaving a dirty/unpinned inode attached to the buffer
3653 * that otherwise looks like it should be flushed.
3654 */
3655 if (XFS_FORCED_SHUTDOWN(mp)) {
3656 xfs_iunpin_wait(ip);
Dave Chinner5717ea42020-06-29 14:49:20 -07003657 xfs_iflush_abort(ip);
3658 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3659 error = -EIO;
3660 continue;
3661 }
3662
3663 /* don't block waiting on a log force to unpin dirty inodes */
3664 if (xfs_ipincount(ip)) {
Dave Chinner718ecc52020-08-17 16:41:01 -07003665 xfs_iflags_clear(ip, XFS_IFLUSHING);
Dave Chinner5717ea42020-06-29 14:49:20 -07003666 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3667 continue;
3668 }
3669
3670 if (!xfs_inode_clean(ip))
3671 error = xfs_iflush(ip, bp);
3672 else
Dave Chinner718ecc52020-08-17 16:41:01 -07003673 xfs_iflags_clear(ip, XFS_IFLUSHING);
Dave Chinner5717ea42020-06-29 14:49:20 -07003674 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3675 if (error)
Dave Chinnere6187b32020-06-29 14:49:19 -07003676 break;
Dave Chinner5717ea42020-06-29 14:49:20 -07003677 clcount++;
Dave Chinnere6187b32020-06-29 14:49:19 -07003678 }
3679
Dave Chinnere6187b32020-06-29 14:49:19 -07003680 if (error) {
3681 bp->b_flags |= XBF_ASYNC;
3682 xfs_buf_ioend_fail(bp);
3683 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Dave Chinner5717ea42020-06-29 14:49:20 -07003684 return error;
Dave Chinnere6187b32020-06-29 14:49:19 -07003685 }
Dave Chinner5717ea42020-06-29 14:49:20 -07003686
3687 if (!clcount)
3688 return -EAGAIN;
3689
3690 XFS_STATS_INC(mp, xs_icluster_flushcnt);
3691 XFS_STATS_ADD(mp, xs_icluster_flushinode, clcount);
3692 return 0;
3693
Dave Chinnere6187b32020-06-29 14:49:19 -07003694}
3695
Darrick J. Wong44a87362018-07-25 12:52:32 -07003696/* Release an inode. */
3697void
3698xfs_irele(
3699 struct xfs_inode *ip)
3700{
3701 trace_xfs_irele(ip, _RET_IP_);
3702 iput(VFS_I(ip));
3703}
Christoph Hellwig54fbdd12020-04-03 11:45:37 -07003704
3705/*
3706 * Ensure all commited transactions touching the inode are written to the log.
3707 */
3708int
3709xfs_log_force_inode(
3710 struct xfs_inode *ip)
3711{
3712 xfs_lsn_t lsn = 0;
3713
3714 xfs_ilock(ip, XFS_ILOCK_SHARED);
3715 if (xfs_ipincount(ip))
3716 lsn = ip->i_itemp->ili_last_lsn;
3717 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3718
3719 if (!lsn)
3720 return 0;
3721 return xfs_log_force_lsn(ip->i_mount, lsn, XFS_LOG_SYNC, NULL);
3722}
Darrick J. Wonge2aaee92020-06-29 14:47:20 -07003723
3724/*
3725 * Grab the exclusive iolock for a data copy from src to dest, making sure to
3726 * abide vfs locking order (lowest pointer value goes first) and breaking the
3727 * layout leases before proceeding. The loop is needed because we cannot call
3728 * the blocking break_layout() with the iolocks held, and therefore have to
3729 * back out both locks.
3730 */
3731static int
3732xfs_iolock_two_inodes_and_break_layout(
3733 struct inode *src,
3734 struct inode *dest)
3735{
3736 int error;
3737
3738 if (src > dest)
3739 swap(src, dest);
3740
3741retry:
3742 /* Wait to break both inodes' layouts before we start locking. */
3743 error = break_layout(src, true);
3744 if (error)
3745 return error;
3746 if (src != dest) {
3747 error = break_layout(dest, true);
3748 if (error)
3749 return error;
3750 }
3751
3752 /* Lock one inode and make sure nobody got in and leased it. */
3753 inode_lock(src);
3754 error = break_layout(src, false);
3755 if (error) {
3756 inode_unlock(src);
3757 if (error == -EWOULDBLOCK)
3758 goto retry;
3759 return error;
3760 }
3761
3762 if (src == dest)
3763 return 0;
3764
3765 /* Lock the other inode and make sure nobody got in and leased it. */
3766 inode_lock_nested(dest, I_MUTEX_NONDIR2);
3767 error = break_layout(dest, false);
3768 if (error) {
3769 inode_unlock(src);
3770 inode_unlock(dest);
3771 if (error == -EWOULDBLOCK)
3772 goto retry;
3773 return error;
3774 }
3775
3776 return 0;
3777}
3778
3779/*
3780 * Lock two inodes so that userspace cannot initiate I/O via file syscalls or
3781 * mmap activity.
3782 */
3783int
3784xfs_ilock2_io_mmap(
3785 struct xfs_inode *ip1,
3786 struct xfs_inode *ip2)
3787{
3788 int ret;
3789
3790 ret = xfs_iolock_two_inodes_and_break_layout(VFS_I(ip1), VFS_I(ip2));
3791 if (ret)
3792 return ret;
3793 if (ip1 == ip2)
3794 xfs_ilock(ip1, XFS_MMAPLOCK_EXCL);
3795 else
3796 xfs_lock_two_inodes(ip1, XFS_MMAPLOCK_EXCL,
3797 ip2, XFS_MMAPLOCK_EXCL);
3798 return 0;
3799}
3800
3801/* Unlock both inodes to allow IO and mmap activity. */
3802void
3803xfs_iunlock2_io_mmap(
3804 struct xfs_inode *ip1,
3805 struct xfs_inode *ip2)
3806{
3807 bool same_inode = (ip1 == ip2);
3808
3809 xfs_iunlock(ip2, XFS_MMAPLOCK_EXCL);
3810 if (!same_inode)
3811 xfs_iunlock(ip1, XFS_MMAPLOCK_EXCL);
3812 inode_unlock(VFS_I(ip2));
3813 if (!same_inode)
3814 inode_unlock(VFS_I(ip1));
3815}