1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
6 #include <linux/iversion.h>
10 #include "xfs_shared.h"
11 #include "xfs_format.h"
12 #include "xfs_log_format.h"
13 #include "xfs_trans_resv.h"
14 #include "xfs_mount.h"
15 #include "xfs_defer.h"
16 #include "xfs_inode.h"
19 #include "xfs_trans_space.h"
20 #include "xfs_trans.h"
21 #include "xfs_buf_item.h"
22 #include "xfs_inode_item.h"
23 #include "xfs_ialloc.h"
25 #include "xfs_bmap_util.h"
26 #include "xfs_errortag.h"
27 #include "xfs_error.h"
28 #include "xfs_quota.h"
29 #include "xfs_filestream.h"
30 #include "xfs_trace.h"
31 #include "xfs_icache.h"
32 #include "xfs_symlink.h"
33 #include "xfs_trans_priv.h"
35 #include "xfs_bmap_btree.h"
36 #include "xfs_reflink.h"
38 #include "xfs_log_priv.h"
40 struct kmem_cache *xfs_inode_cache;
43 * Used in xfs_itruncate_extents(). This is the maximum number of extents
44 * freed from a file in a single transaction.
46 #define XFS_ITRUNC_MAX_EXTENTS 2
48 STATIC int xfs_iunlink(struct xfs_trans *, struct xfs_inode *);
49 STATIC int xfs_iunlink_remove(struct xfs_trans *tp, struct xfs_perag *pag,
53 * helper function to extract extent size hint from inode
60 * No point in aligning allocations if we need to COW to actually
63 if (xfs_is_always_cow_inode(ip))
65 if ((ip->i_diflags & XFS_DIFLAG_EXTSIZE) && ip->i_extsize)
67 if (XFS_IS_REALTIME_INODE(ip))
68 return ip->i_mount->m_sb.sb_rextsize;
73 * Helper function to extract CoW extent size hint from inode.
74 * Between the extent size hint and the CoW extent size hint, we
75 * return the greater of the two. If the value is zero (automatic),
76 * use the default size.
79 xfs_get_cowextsz_hint(
85 if (ip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE)
87 b = xfs_get_extsz_hint(ip);
91 return XFS_DEFAULT_COWEXTSZ_HINT;
96 * These two are wrapper routines around the xfs_ilock() routine used to
97 * centralize some grungy code. They are used in places that wish to lock the
98 * inode solely for reading the extents. The reason these places can't just
99 * call xfs_ilock(ip, XFS_ILOCK_SHARED) is that the inode lock also guards to
100 * bringing in of the extents from disk for a file in b-tree format. If the
101 * inode is in b-tree format, then we need to lock the inode exclusively until
102 * the extents are read in. Locking it exclusively all the time would limit
103 * our parallelism unnecessarily, though. What we do instead is check to see
104 * if the extents have been read in yet, and only lock the inode exclusively
107 * The functions return a value which should be given to the corresponding
108 * xfs_iunlock() call.
111 xfs_ilock_data_map_shared(
112 struct xfs_inode *ip)
114 uint lock_mode = XFS_ILOCK_SHARED;
116 if (xfs_need_iread_extents(&ip->i_df))
117 lock_mode = XFS_ILOCK_EXCL;
118 xfs_ilock(ip, lock_mode);
123 xfs_ilock_attr_map_shared(
124 struct xfs_inode *ip)
126 uint lock_mode = XFS_ILOCK_SHARED;
128 if (ip->i_afp && xfs_need_iread_extents(ip->i_afp))
129 lock_mode = XFS_ILOCK_EXCL;
130 xfs_ilock(ip, lock_mode);
135 * In addition to i_rwsem in the VFS inode, the xfs inode contains 2
136 * multi-reader locks: invalidate_lock and the i_lock. This routine allows
137 * various combinations of the locks to be obtained.
139 * The 3 locks should always be ordered so that the IO lock is obtained first,
140 * the mmap lock second and the ilock last in order to prevent deadlock.
142 * Basic locking order:
144 * i_rwsem -> invalidate_lock -> page_lock -> i_ilock
146 * mmap_lock locking order:
148 * i_rwsem -> page lock -> mmap_lock
149 * mmap_lock -> invalidate_lock -> page_lock
151 * The difference in mmap_lock locking order mean that we cannot hold the
152 * invalidate_lock over syscall based read(2)/write(2) based IO. These IO paths
153 * can fault in pages during copy in/out (for buffered IO) or require the
154 * mmap_lock in get_user_pages() to map the user pages into the kernel address
155 * space for direct IO. Similarly the i_rwsem cannot be taken inside a page
156 * fault because page faults already hold the mmap_lock.
158 * Hence to serialise fully against both syscall and mmap based IO, we need to
159 * take both the i_rwsem and the invalidate_lock. These locks should *only* be
160 * both taken in places where we need to invalidate the page cache in a race
161 * free manner (e.g. truncate, hole punch and other extent manipulation
169 trace_xfs_ilock(ip, lock_flags, _RET_IP_);
172 * You can't set both SHARED and EXCL for the same lock,
173 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
174 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
176 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
177 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
178 ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
179 (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
180 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
181 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
182 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
184 if (lock_flags & XFS_IOLOCK_EXCL) {
185 down_write_nested(&VFS_I(ip)->i_rwsem,
186 XFS_IOLOCK_DEP(lock_flags));
187 } else if (lock_flags & XFS_IOLOCK_SHARED) {
188 down_read_nested(&VFS_I(ip)->i_rwsem,
189 XFS_IOLOCK_DEP(lock_flags));
192 if (lock_flags & XFS_MMAPLOCK_EXCL) {
193 down_write_nested(&VFS_I(ip)->i_mapping->invalidate_lock,
194 XFS_MMAPLOCK_DEP(lock_flags));
195 } else if (lock_flags & XFS_MMAPLOCK_SHARED) {
196 down_read_nested(&VFS_I(ip)->i_mapping->invalidate_lock,
197 XFS_MMAPLOCK_DEP(lock_flags));
200 if (lock_flags & XFS_ILOCK_EXCL)
201 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
202 else if (lock_flags & XFS_ILOCK_SHARED)
203 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
207 * This is just like xfs_ilock(), except that the caller
208 * is guaranteed not to sleep. It returns 1 if it gets
209 * the requested locks and 0 otherwise. If the IO lock is
210 * obtained but the inode lock cannot be, then the IO lock
211 * is dropped before returning.
213 * ip -- the inode being locked
214 * lock_flags -- this parameter indicates the inode's locks to be
215 * to be locked. See the comment for xfs_ilock() for a list
223 trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_);
226 * You can't set both SHARED and EXCL for the same lock,
227 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
228 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
230 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
231 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
232 ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
233 (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
234 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
235 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
236 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
238 if (lock_flags & XFS_IOLOCK_EXCL) {
239 if (!down_write_trylock(&VFS_I(ip)->i_rwsem))
241 } else if (lock_flags & XFS_IOLOCK_SHARED) {
242 if (!down_read_trylock(&VFS_I(ip)->i_rwsem))
246 if (lock_flags & XFS_MMAPLOCK_EXCL) {
247 if (!down_write_trylock(&VFS_I(ip)->i_mapping->invalidate_lock))
248 goto out_undo_iolock;
249 } else if (lock_flags & XFS_MMAPLOCK_SHARED) {
250 if (!down_read_trylock(&VFS_I(ip)->i_mapping->invalidate_lock))
251 goto out_undo_iolock;
254 if (lock_flags & XFS_ILOCK_EXCL) {
255 if (!mrtryupdate(&ip->i_lock))
256 goto out_undo_mmaplock;
257 } else if (lock_flags & XFS_ILOCK_SHARED) {
258 if (!mrtryaccess(&ip->i_lock))
259 goto out_undo_mmaplock;
264 if (lock_flags & XFS_MMAPLOCK_EXCL)
265 up_write(&VFS_I(ip)->i_mapping->invalidate_lock);
266 else if (lock_flags & XFS_MMAPLOCK_SHARED)
267 up_read(&VFS_I(ip)->i_mapping->invalidate_lock);
269 if (lock_flags & XFS_IOLOCK_EXCL)
270 up_write(&VFS_I(ip)->i_rwsem);
271 else if (lock_flags & XFS_IOLOCK_SHARED)
272 up_read(&VFS_I(ip)->i_rwsem);
278 * xfs_iunlock() is used to drop the inode locks acquired with
279 * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
280 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
281 * that we know which locks to drop.
283 * ip -- the inode being unlocked
284 * lock_flags -- this parameter indicates the inode's locks to be
285 * to be unlocked. See the comment for xfs_ilock() for a list
286 * of valid values for this parameter.
295 * You can't set both SHARED and EXCL for the same lock,
296 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
297 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
299 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
300 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
301 ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
302 (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
303 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
304 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
305 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
306 ASSERT(lock_flags != 0);
308 if (lock_flags & XFS_IOLOCK_EXCL)
309 up_write(&VFS_I(ip)->i_rwsem);
310 else if (lock_flags & XFS_IOLOCK_SHARED)
311 up_read(&VFS_I(ip)->i_rwsem);
313 if (lock_flags & XFS_MMAPLOCK_EXCL)
314 up_write(&VFS_I(ip)->i_mapping->invalidate_lock);
315 else if (lock_flags & XFS_MMAPLOCK_SHARED)
316 up_read(&VFS_I(ip)->i_mapping->invalidate_lock);
318 if (lock_flags & XFS_ILOCK_EXCL)
319 mrunlock_excl(&ip->i_lock);
320 else if (lock_flags & XFS_ILOCK_SHARED)
321 mrunlock_shared(&ip->i_lock);
323 trace_xfs_iunlock(ip, lock_flags, _RET_IP_);
327 * give up write locks. the i/o lock cannot be held nested
328 * if it is being demoted.
335 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL));
337 ~(XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
339 if (lock_flags & XFS_ILOCK_EXCL)
340 mrdemote(&ip->i_lock);
341 if (lock_flags & XFS_MMAPLOCK_EXCL)
342 downgrade_write(&VFS_I(ip)->i_mapping->invalidate_lock);
343 if (lock_flags & XFS_IOLOCK_EXCL)
344 downgrade_write(&VFS_I(ip)->i_rwsem);
346 trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_);
349 #if defined(DEBUG) || defined(XFS_WARN)
351 __xfs_rwsem_islocked(
352 struct rw_semaphore *rwsem,
356 return rwsem_is_locked(rwsem);
359 return lockdep_is_held_type(rwsem, 0);
362 * We are checking that the lock is held at least in shared
363 * mode but don't care that it might be held exclusively
364 * (i.e. shared | excl). Hence we check if the lock is held
365 * in any mode rather than an explicit shared mode.
367 return lockdep_is_held_type(rwsem, -1);
372 struct xfs_inode *ip,
375 if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) {
376 if (!(lock_flags & XFS_ILOCK_SHARED))
377 return !!ip->i_lock.mr_writer;
378 return rwsem_is_locked(&ip->i_lock.mr_lock);
381 if (lock_flags & (XFS_MMAPLOCK_EXCL|XFS_MMAPLOCK_SHARED)) {
382 return __xfs_rwsem_islocked(&VFS_I(ip)->i_rwsem,
383 (lock_flags & XFS_IOLOCK_SHARED));
386 if (lock_flags & (XFS_IOLOCK_EXCL | XFS_IOLOCK_SHARED)) {
387 return __xfs_rwsem_islocked(&VFS_I(ip)->i_rwsem,
388 (lock_flags & XFS_IOLOCK_SHARED));
397 * xfs_lockdep_subclass_ok() is only used in an ASSERT, so is only called when
398 * DEBUG or XFS_WARN is set. And MAX_LOCKDEP_SUBCLASSES is then only defined
399 * when CONFIG_LOCKDEP is set. Hence the complex define below to avoid build
400 * errors and warnings.
402 #if (defined(DEBUG) || defined(XFS_WARN)) && defined(CONFIG_LOCKDEP)
404 xfs_lockdep_subclass_ok(
407 return subclass < MAX_LOCKDEP_SUBCLASSES;
410 #define xfs_lockdep_subclass_ok(subclass) (true)
414 * Bump the subclass so xfs_lock_inodes() acquires each lock with a different
415 * value. This can be called for any type of inode lock combination, including
416 * parent locking. Care must be taken to ensure we don't overrun the subclass
417 * storage fields in the class mask we build.
420 xfs_lock_inumorder(int lock_mode, int subclass)
424 ASSERT(!(lock_mode & (XFS_ILOCK_PARENT | XFS_ILOCK_RTBITMAP |
426 ASSERT(xfs_lockdep_subclass_ok(subclass));
428 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) {
429 ASSERT(subclass <= XFS_IOLOCK_MAX_SUBCLASS);
430 class += subclass << XFS_IOLOCK_SHIFT;
433 if (lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) {
434 ASSERT(subclass <= XFS_MMAPLOCK_MAX_SUBCLASS);
435 class += subclass << XFS_MMAPLOCK_SHIFT;
438 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) {
439 ASSERT(subclass <= XFS_ILOCK_MAX_SUBCLASS);
440 class += subclass << XFS_ILOCK_SHIFT;
443 return (lock_mode & ~XFS_LOCK_SUBCLASS_MASK) | class;
447 * The following routine will lock n inodes in exclusive mode. We assume the
448 * caller calls us with the inodes in i_ino order.
450 * We need to detect deadlock where an inode that we lock is in the AIL and we
451 * start waiting for another inode that is locked by a thread in a long running
452 * transaction (such as truncate). This can result in deadlock since the long
453 * running trans might need to wait for the inode we just locked in order to
454 * push the tail and free space in the log.
456 * xfs_lock_inodes() can only be used to lock one type of lock at a time -
457 * the iolock, the mmaplock or the ilock, but not more than one at a time. If we
458 * lock more than one at a time, lockdep will report false positives saying we
459 * have violated locking orders.
463 struct xfs_inode **ips,
467 int attempts = 0, i, j, try_lock;
468 struct xfs_log_item *lp;
471 * Currently supports between 2 and 5 inodes with exclusive locking. We
472 * support an arbitrary depth of locking here, but absolute limits on
473 * inodes depend on the type of locking and the limits placed by
474 * lockdep annotations in xfs_lock_inumorder. These are all checked by
477 ASSERT(ips && inodes >= 2 && inodes <= 5);
478 ASSERT(lock_mode & (XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL |
480 ASSERT(!(lock_mode & (XFS_IOLOCK_SHARED | XFS_MMAPLOCK_SHARED |
482 ASSERT(!(lock_mode & XFS_MMAPLOCK_EXCL) ||
483 inodes <= XFS_MMAPLOCK_MAX_SUBCLASS + 1);
484 ASSERT(!(lock_mode & XFS_ILOCK_EXCL) ||
485 inodes <= XFS_ILOCK_MAX_SUBCLASS + 1);
487 if (lock_mode & XFS_IOLOCK_EXCL) {
488 ASSERT(!(lock_mode & (XFS_MMAPLOCK_EXCL | XFS_ILOCK_EXCL)));
489 } else if (lock_mode & XFS_MMAPLOCK_EXCL)
490 ASSERT(!(lock_mode & XFS_ILOCK_EXCL));
495 for (; i < inodes; i++) {
498 if (i && (ips[i] == ips[i - 1])) /* Already locked */
502 * If try_lock is not set yet, make sure all locked inodes are
503 * not in the AIL. If any are, set try_lock to be used later.
506 for (j = (i - 1); j >= 0 && !try_lock; j--) {
507 lp = &ips[j]->i_itemp->ili_item;
508 if (lp && test_bit(XFS_LI_IN_AIL, &lp->li_flags))
514 * If any of the previous locks we have locked is in the AIL,
515 * we must TRY to get the second and subsequent locks. If
516 * we can't get any, we must release all we have
520 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
524 /* try_lock means we have an inode locked that is in the AIL. */
526 if (xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i)))
530 * Unlock all previous guys and try again. xfs_iunlock will try
531 * to push the tail if the inode is in the AIL.
534 for (j = i - 1; j >= 0; j--) {
536 * Check to see if we've already unlocked this one. Not
537 * the first one going back, and the inode ptr is the
540 if (j != (i - 1) && ips[j] == ips[j + 1])
543 xfs_iunlock(ips[j], lock_mode);
546 if ((attempts % 5) == 0) {
547 delay(1); /* Don't just spin the CPU */
556 * xfs_lock_two_inodes() can only be used to lock ilock. The iolock and
557 * mmaplock must be double-locked separately since we use i_rwsem and
558 * invalidate_lock for that. We now support taking one lock EXCL and the
563 struct xfs_inode *ip0,
565 struct xfs_inode *ip1,
569 struct xfs_log_item *lp;
571 ASSERT(hweight32(ip0_mode) == 1);
572 ASSERT(hweight32(ip1_mode) == 1);
573 ASSERT(!(ip0_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)));
574 ASSERT(!(ip1_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)));
575 ASSERT(!(ip0_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)));
576 ASSERT(!(ip1_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)));
577 ASSERT(ip0->i_ino != ip1->i_ino);
579 if (ip0->i_ino > ip1->i_ino) {
581 swap(ip0_mode, ip1_mode);
585 xfs_ilock(ip0, xfs_lock_inumorder(ip0_mode, 0));
588 * If the first lock we have locked is in the AIL, we must TRY to get
589 * the second lock. If we can't get it, we must release the first one
592 lp = &ip0->i_itemp->ili_item;
593 if (lp && test_bit(XFS_LI_IN_AIL, &lp->li_flags)) {
594 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(ip1_mode, 1))) {
595 xfs_iunlock(ip0, ip0_mode);
596 if ((++attempts % 5) == 0)
597 delay(1); /* Don't just spin the CPU */
601 xfs_ilock(ip1, xfs_lock_inumorder(ip1_mode, 1));
607 struct xfs_inode *ip)
611 if (ip->i_diflags & XFS_DIFLAG_ANY) {
612 if (ip->i_diflags & XFS_DIFLAG_REALTIME)
613 flags |= FS_XFLAG_REALTIME;
614 if (ip->i_diflags & XFS_DIFLAG_PREALLOC)
615 flags |= FS_XFLAG_PREALLOC;
616 if (ip->i_diflags & XFS_DIFLAG_IMMUTABLE)
617 flags |= FS_XFLAG_IMMUTABLE;
618 if (ip->i_diflags & XFS_DIFLAG_APPEND)
619 flags |= FS_XFLAG_APPEND;
620 if (ip->i_diflags & XFS_DIFLAG_SYNC)
621 flags |= FS_XFLAG_SYNC;
622 if (ip->i_diflags & XFS_DIFLAG_NOATIME)
623 flags |= FS_XFLAG_NOATIME;
624 if (ip->i_diflags & XFS_DIFLAG_NODUMP)
625 flags |= FS_XFLAG_NODUMP;
626 if (ip->i_diflags & XFS_DIFLAG_RTINHERIT)
627 flags |= FS_XFLAG_RTINHERIT;
628 if (ip->i_diflags & XFS_DIFLAG_PROJINHERIT)
629 flags |= FS_XFLAG_PROJINHERIT;
630 if (ip->i_diflags & XFS_DIFLAG_NOSYMLINKS)
631 flags |= FS_XFLAG_NOSYMLINKS;
632 if (ip->i_diflags & XFS_DIFLAG_EXTSIZE)
633 flags |= FS_XFLAG_EXTSIZE;
634 if (ip->i_diflags & XFS_DIFLAG_EXTSZINHERIT)
635 flags |= FS_XFLAG_EXTSZINHERIT;
636 if (ip->i_diflags & XFS_DIFLAG_NODEFRAG)
637 flags |= FS_XFLAG_NODEFRAG;
638 if (ip->i_diflags & XFS_DIFLAG_FILESTREAM)
639 flags |= FS_XFLAG_FILESTREAM;
642 if (ip->i_diflags2 & XFS_DIFLAG2_ANY) {
643 if (ip->i_diflags2 & XFS_DIFLAG2_DAX)
644 flags |= FS_XFLAG_DAX;
645 if (ip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE)
646 flags |= FS_XFLAG_COWEXTSIZE;
650 flags |= FS_XFLAG_HASATTR;
655 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
656 * is allowed, otherwise it has to be an exact match. If a CI match is found,
657 * ci_name->name will point to a the actual name (caller must free) or
658 * will be set to NULL if an exact match is found.
662 struct xfs_inode *dp,
663 const struct xfs_name *name,
664 struct xfs_inode **ipp,
665 struct xfs_name *ci_name)
670 trace_xfs_lookup(dp, name);
672 if (xfs_is_shutdown(dp->i_mount))
675 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
679 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
687 kmem_free(ci_name->name);
693 /* Propagate di_flags from a parent inode to a child inode. */
695 xfs_inode_inherit_flags(
696 struct xfs_inode *ip,
697 const struct xfs_inode *pip)
699 unsigned int di_flags = 0;
700 xfs_failaddr_t failaddr;
701 umode_t mode = VFS_I(ip)->i_mode;
704 if (pip->i_diflags & XFS_DIFLAG_RTINHERIT)
705 di_flags |= XFS_DIFLAG_RTINHERIT;
706 if (pip->i_diflags & XFS_DIFLAG_EXTSZINHERIT) {
707 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
708 ip->i_extsize = pip->i_extsize;
710 if (pip->i_diflags & XFS_DIFLAG_PROJINHERIT)
711 di_flags |= XFS_DIFLAG_PROJINHERIT;
712 } else if (S_ISREG(mode)) {
713 if ((pip->i_diflags & XFS_DIFLAG_RTINHERIT) &&
714 xfs_has_realtime(ip->i_mount))
715 di_flags |= XFS_DIFLAG_REALTIME;
716 if (pip->i_diflags & XFS_DIFLAG_EXTSZINHERIT) {
717 di_flags |= XFS_DIFLAG_EXTSIZE;
718 ip->i_extsize = pip->i_extsize;
721 if ((pip->i_diflags & XFS_DIFLAG_NOATIME) &&
723 di_flags |= XFS_DIFLAG_NOATIME;
724 if ((pip->i_diflags & XFS_DIFLAG_NODUMP) &&
726 di_flags |= XFS_DIFLAG_NODUMP;
727 if ((pip->i_diflags & XFS_DIFLAG_SYNC) &&
729 di_flags |= XFS_DIFLAG_SYNC;
730 if ((pip->i_diflags & XFS_DIFLAG_NOSYMLINKS) &&
731 xfs_inherit_nosymlinks)
732 di_flags |= XFS_DIFLAG_NOSYMLINKS;
733 if ((pip->i_diflags & XFS_DIFLAG_NODEFRAG) &&
734 xfs_inherit_nodefrag)
735 di_flags |= XFS_DIFLAG_NODEFRAG;
736 if (pip->i_diflags & XFS_DIFLAG_FILESTREAM)
737 di_flags |= XFS_DIFLAG_FILESTREAM;
739 ip->i_diflags |= di_flags;
742 * Inode verifiers on older kernels only check that the extent size
743 * hint is an integer multiple of the rt extent size on realtime files.
744 * They did not check the hint alignment on a directory with both
745 * rtinherit and extszinherit flags set. If the misaligned hint is
746 * propagated from a directory into a new realtime file, new file
747 * allocations will fail due to math errors in the rt allocator and/or
748 * trip the verifiers. Validate the hint settings in the new file so
749 * that we don't let broken hints propagate.
751 failaddr = xfs_inode_validate_extsize(ip->i_mount, ip->i_extsize,
752 VFS_I(ip)->i_mode, ip->i_diflags);
754 ip->i_diflags &= ~(XFS_DIFLAG_EXTSIZE |
755 XFS_DIFLAG_EXTSZINHERIT);
760 /* Propagate di_flags2 from a parent inode to a child inode. */
762 xfs_inode_inherit_flags2(
763 struct xfs_inode *ip,
764 const struct xfs_inode *pip)
766 xfs_failaddr_t failaddr;
768 if (pip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE) {
769 ip->i_diflags2 |= XFS_DIFLAG2_COWEXTSIZE;
770 ip->i_cowextsize = pip->i_cowextsize;
772 if (pip->i_diflags2 & XFS_DIFLAG2_DAX)
773 ip->i_diflags2 |= XFS_DIFLAG2_DAX;
775 /* Don't let invalid cowextsize hints propagate. */
776 failaddr = xfs_inode_validate_cowextsize(ip->i_mount, ip->i_cowextsize,
777 VFS_I(ip)->i_mode, ip->i_diflags, ip->i_diflags2);
779 ip->i_diflags2 &= ~XFS_DIFLAG2_COWEXTSIZE;
780 ip->i_cowextsize = 0;
785 * Initialise a newly allocated inode and return the in-core inode to the
786 * caller locked exclusively.
790 struct user_namespace *mnt_userns,
791 struct xfs_trans *tp,
792 struct xfs_inode *pip,
799 struct xfs_inode **ipp)
801 struct inode *dir = pip ? VFS_I(pip) : NULL;
802 struct xfs_mount *mp = tp->t_mountp;
803 struct xfs_inode *ip;
806 struct timespec64 tv;
810 * Protect against obviously corrupt allocation btree records. Later
811 * xfs_iget checks will catch re-allocation of other active in-memory
812 * and on-disk inodes. If we don't catch reallocating the parent inode
813 * here we will deadlock in xfs_iget() so we have to do these checks
816 if ((pip && ino == pip->i_ino) || !xfs_verify_dir_ino(mp, ino)) {
817 xfs_alert(mp, "Allocated a known in-use inode 0x%llx!", ino);
818 return -EFSCORRUPTED;
822 * Get the in-core inode with the lock held exclusively to prevent
823 * others from looking at until we're done.
825 error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE, XFS_ILOCK_EXCL, &ip);
831 set_nlink(inode, nlink);
832 inode->i_rdev = rdev;
835 if (dir && !(dir->i_mode & S_ISGID) && xfs_has_grpid(mp)) {
836 inode_fsuid_set(inode, mnt_userns);
837 inode->i_gid = dir->i_gid;
838 inode->i_mode = mode;
840 inode_init_owner(mnt_userns, inode, dir, mode);
844 * If the group ID of the new file does not match the effective group
845 * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
846 * (and only if the irix_sgid_inherit compatibility variable is set).
848 if (irix_sgid_inherit &&
849 (inode->i_mode & S_ISGID) &&
850 !in_group_p(i_gid_into_mnt(mnt_userns, inode)))
851 inode->i_mode &= ~S_ISGID;
854 ip->i_df.if_nextents = 0;
855 ASSERT(ip->i_nblocks == 0);
857 tv = current_time(inode);
865 if (xfs_has_v3inodes(mp)) {
866 inode_set_iversion(inode, 1);
867 ip->i_cowextsize = 0;
871 flags = XFS_ILOG_CORE;
872 switch (mode & S_IFMT) {
877 ip->i_df.if_format = XFS_DINODE_FMT_DEV;
878 flags |= XFS_ILOG_DEV;
882 if (pip && (pip->i_diflags & XFS_DIFLAG_ANY))
883 xfs_inode_inherit_flags(ip, pip);
884 if (pip && (pip->i_diflags2 & XFS_DIFLAG2_ANY))
885 xfs_inode_inherit_flags2(ip, pip);
888 ip->i_df.if_format = XFS_DINODE_FMT_EXTENTS;
889 ip->i_df.if_bytes = 0;
890 ip->i_df.if_u1.if_root = NULL;
897 * If we need to create attributes immediately after allocating the
898 * inode, initialise an empty attribute fork right now. We use the
899 * default fork offset for attributes here as we don't know exactly what
900 * size or how many attributes we might be adding. We can do this
901 * safely here because we know the data fork is completely empty and
902 * this saves us from needing to run a separate transaction to set the
903 * fork offset in the immediate future.
905 if (init_xattrs && xfs_has_attr(mp)) {
906 ip->i_forkoff = xfs_default_attroffset(ip) >> 3;
907 ip->i_afp = xfs_ifork_alloc(XFS_DINODE_FMT_EXTENTS, 0);
911 * Log the new values stuffed into the inode.
913 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
914 xfs_trans_log_inode(tp, ip, flags);
916 /* now that we have an i_mode we can setup the inode structure */
924 * Decrement the link count on an inode & log the change. If this causes the
925 * link count to go to zero, move the inode to AGI unlinked list so that it can
926 * be freed when the last active reference goes away via xfs_inactive().
928 static int /* error */
933 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
935 drop_nlink(VFS_I(ip));
936 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
938 if (VFS_I(ip)->i_nlink)
941 return xfs_iunlink(tp, ip);
945 * Increment the link count on an inode & log the change.
952 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
954 inc_nlink(VFS_I(ip));
955 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
960 struct user_namespace *mnt_userns,
962 struct xfs_name *name,
968 int is_dir = S_ISDIR(mode);
969 struct xfs_mount *mp = dp->i_mount;
970 struct xfs_inode *ip = NULL;
971 struct xfs_trans *tp = NULL;
973 bool unlock_dp_on_error = false;
975 struct xfs_dquot *udqp = NULL;
976 struct xfs_dquot *gdqp = NULL;
977 struct xfs_dquot *pdqp = NULL;
978 struct xfs_trans_res *tres;
982 trace_xfs_create(dp, name);
984 if (xfs_is_shutdown(mp))
987 prid = xfs_get_initial_prid(dp);
990 * Make sure that we have allocated dquot(s) on disk.
992 error = xfs_qm_vop_dqalloc(dp, mapped_fsuid(mnt_userns, &init_user_ns),
993 mapped_fsgid(mnt_userns, &init_user_ns), prid,
994 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
995 &udqp, &gdqp, &pdqp);
1000 resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
1001 tres = &M_RES(mp)->tr_mkdir;
1003 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
1004 tres = &M_RES(mp)->tr_create;
1008 * Initially assume that the file does not exist and
1009 * reserve the resources for that case. If that is not
1010 * the case we'll drop the one we have and get a more
1011 * appropriate transaction later.
1013 error = xfs_trans_alloc_icreate(mp, tres, udqp, gdqp, pdqp, resblks,
1015 if (error == -ENOSPC) {
1016 /* flush outstanding delalloc blocks and retry */
1017 xfs_flush_inodes(mp);
1018 error = xfs_trans_alloc_icreate(mp, tres, udqp, gdqp, pdqp,
1022 goto out_release_dquots;
1024 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1025 unlock_dp_on_error = true;
1027 error = xfs_iext_count_may_overflow(dp, XFS_DATA_FORK,
1028 XFS_IEXT_DIR_MANIP_CNT(mp));
1030 goto out_trans_cancel;
1033 * A newly created regular or special file just has one directory
1034 * entry pointing to them, but a directory also the "." entry
1035 * pointing to itself.
1037 error = xfs_dialloc(&tp, dp->i_ino, mode, &ino);
1039 error = xfs_init_new_inode(mnt_userns, tp, dp, ino, mode,
1040 is_dir ? 2 : 1, rdev, prid, init_xattrs, &ip);
1042 goto out_trans_cancel;
1045 * Now we join the directory inode to the transaction. We do not do it
1046 * earlier because xfs_dialloc might commit the previous transaction
1047 * (and release all the locks). An error from here on will result in
1048 * the transaction cancel unlocking dp so don't do it explicitly in the
1051 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1052 unlock_dp_on_error = false;
1054 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
1055 resblks - XFS_IALLOC_SPACE_RES(mp));
1057 ASSERT(error != -ENOSPC);
1058 goto out_trans_cancel;
1060 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1061 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1064 error = xfs_dir_init(tp, ip, dp);
1066 goto out_trans_cancel;
1068 xfs_bumplink(tp, dp);
1072 * If this is a synchronous mount, make sure that the
1073 * create transaction goes to disk before returning to
1076 if (xfs_has_wsync(mp) || xfs_has_dirsync(mp))
1077 xfs_trans_set_sync(tp);
1080 * Attach the dquot(s) to the inodes and modify them incore.
1081 * These ids of the inode couldn't have changed since the new
1082 * inode has been locked ever since it was created.
1084 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
1086 error = xfs_trans_commit(tp);
1088 goto out_release_inode;
1090 xfs_qm_dqrele(udqp);
1091 xfs_qm_dqrele(gdqp);
1092 xfs_qm_dqrele(pdqp);
1098 xfs_trans_cancel(tp);
1101 * Wait until after the current transaction is aborted to finish the
1102 * setup of the inode and release the inode. This prevents recursive
1103 * transactions and deadlocks from xfs_inactive.
1106 xfs_finish_inode_setup(ip);
1110 xfs_qm_dqrele(udqp);
1111 xfs_qm_dqrele(gdqp);
1112 xfs_qm_dqrele(pdqp);
1114 if (unlock_dp_on_error)
1115 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1121 struct user_namespace *mnt_userns,
1122 struct xfs_inode *dp,
1124 struct xfs_inode **ipp)
1126 struct xfs_mount *mp = dp->i_mount;
1127 struct xfs_inode *ip = NULL;
1128 struct xfs_trans *tp = NULL;
1131 struct xfs_dquot *udqp = NULL;
1132 struct xfs_dquot *gdqp = NULL;
1133 struct xfs_dquot *pdqp = NULL;
1134 struct xfs_trans_res *tres;
1138 if (xfs_is_shutdown(mp))
1141 prid = xfs_get_initial_prid(dp);
1144 * Make sure that we have allocated dquot(s) on disk.
1146 error = xfs_qm_vop_dqalloc(dp, mapped_fsuid(mnt_userns, &init_user_ns),
1147 mapped_fsgid(mnt_userns, &init_user_ns), prid,
1148 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
1149 &udqp, &gdqp, &pdqp);
1153 resblks = XFS_IALLOC_SPACE_RES(mp);
1154 tres = &M_RES(mp)->tr_create_tmpfile;
1156 error = xfs_trans_alloc_icreate(mp, tres, udqp, gdqp, pdqp, resblks,
1159 goto out_release_dquots;
1161 error = xfs_dialloc(&tp, dp->i_ino, mode, &ino);
1163 error = xfs_init_new_inode(mnt_userns, tp, dp, ino, mode,
1164 0, 0, prid, false, &ip);
1166 goto out_trans_cancel;
1168 if (xfs_has_wsync(mp))
1169 xfs_trans_set_sync(tp);
1172 * Attach the dquot(s) to the inodes and modify them incore.
1173 * These ids of the inode couldn't have changed since the new
1174 * inode has been locked ever since it was created.
1176 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
1178 error = xfs_iunlink(tp, ip);
1180 goto out_trans_cancel;
1182 error = xfs_trans_commit(tp);
1184 goto out_release_inode;
1186 xfs_qm_dqrele(udqp);
1187 xfs_qm_dqrele(gdqp);
1188 xfs_qm_dqrele(pdqp);
1194 xfs_trans_cancel(tp);
1197 * Wait until after the current transaction is aborted to finish the
1198 * setup of the inode and release the inode. This prevents recursive
1199 * transactions and deadlocks from xfs_inactive.
1202 xfs_finish_inode_setup(ip);
1206 xfs_qm_dqrele(udqp);
1207 xfs_qm_dqrele(gdqp);
1208 xfs_qm_dqrele(pdqp);
1217 struct xfs_name *target_name)
1219 xfs_mount_t *mp = tdp->i_mount;
1221 int error, nospace_error = 0;
1224 trace_xfs_link(tdp, target_name);
1226 ASSERT(!S_ISDIR(VFS_I(sip)->i_mode));
1228 if (xfs_is_shutdown(mp))
1231 error = xfs_qm_dqattach(sip);
1235 error = xfs_qm_dqattach(tdp);
1239 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
1240 error = xfs_trans_alloc_dir(tdp, &M_RES(mp)->tr_link, sip, &resblks,
1241 &tp, &nospace_error);
1245 error = xfs_iext_count_may_overflow(tdp, XFS_DATA_FORK,
1246 XFS_IEXT_DIR_MANIP_CNT(mp));
1251 * If we are using project inheritance, we only allow hard link
1252 * creation in our tree when the project IDs are the same; else
1253 * the tree quota mechanism could be circumvented.
1255 if (unlikely((tdp->i_diflags & XFS_DIFLAG_PROJINHERIT) &&
1256 tdp->i_projid != sip->i_projid)) {
1262 error = xfs_dir_canenter(tp, tdp, target_name);
1268 * Handle initial link state of O_TMPFILE inode
1270 if (VFS_I(sip)->i_nlink == 0) {
1271 struct xfs_perag *pag;
1273 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, sip->i_ino));
1274 error = xfs_iunlink_remove(tp, pag, sip);
1280 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
1284 xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1285 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
1287 xfs_bumplink(tp, sip);
1290 * If this is a synchronous mount, make sure that the
1291 * link transaction goes to disk before returning to
1294 if (xfs_has_wsync(mp) || xfs_has_dirsync(mp))
1295 xfs_trans_set_sync(tp);
1297 return xfs_trans_commit(tp);
1300 xfs_trans_cancel(tp);
1302 if (error == -ENOSPC && nospace_error)
1303 error = nospace_error;
1307 /* Clear the reflink flag and the cowblocks tag if possible. */
1309 xfs_itruncate_clear_reflink_flags(
1310 struct xfs_inode *ip)
1312 struct xfs_ifork *dfork;
1313 struct xfs_ifork *cfork;
1315 if (!xfs_is_reflink_inode(ip))
1317 dfork = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1318 cfork = XFS_IFORK_PTR(ip, XFS_COW_FORK);
1319 if (dfork->if_bytes == 0 && cfork->if_bytes == 0)
1320 ip->i_diflags2 &= ~XFS_DIFLAG2_REFLINK;
1321 if (cfork->if_bytes == 0)
1322 xfs_inode_clear_cowblocks_tag(ip);
1326 * Free up the underlying blocks past new_size. The new size must be smaller
1327 * than the current size. This routine can be used both for the attribute and
1328 * data fork, and does not modify the inode size, which is left to the caller.
1330 * The transaction passed to this routine must have made a permanent log
1331 * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the
1332 * given transaction and start new ones, so make sure everything involved in
1333 * the transaction is tidy before calling here. Some transaction will be
1334 * returned to the caller to be committed. The incoming transaction must
1335 * already include the inode, and both inode locks must be held exclusively.
1336 * The inode must also be "held" within the transaction. On return the inode
1337 * will be "held" within the returned transaction. This routine does NOT
1338 * require any disk space to be reserved for it within the transaction.
1340 * If we get an error, we must return with the inode locked and linked into the
1341 * current transaction. This keeps things simple for the higher level code,
1342 * because it always knows that the inode is locked and held in the transaction
1343 * that returns to it whether errors occur or not. We don't mark the inode
1344 * dirty on error so that transactions can be easily aborted if possible.
1347 xfs_itruncate_extents_flags(
1348 struct xfs_trans **tpp,
1349 struct xfs_inode *ip,
1351 xfs_fsize_t new_size,
1354 struct xfs_mount *mp = ip->i_mount;
1355 struct xfs_trans *tp = *tpp;
1356 xfs_fileoff_t first_unmap_block;
1357 xfs_filblks_t unmap_len;
1360 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1361 ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
1362 xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1363 ASSERT(new_size <= XFS_ISIZE(ip));
1364 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1365 ASSERT(ip->i_itemp != NULL);
1366 ASSERT(ip->i_itemp->ili_lock_flags == 0);
1367 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1369 trace_xfs_itruncate_extents_start(ip, new_size);
1371 flags |= xfs_bmapi_aflag(whichfork);
1374 * Since it is possible for space to become allocated beyond
1375 * the end of the file (in a crash where the space is allocated
1376 * but the inode size is not yet updated), simply remove any
1377 * blocks which show up between the new EOF and the maximum
1378 * possible file size.
1380 * We have to free all the blocks to the bmbt maximum offset, even if
1381 * the page cache can't scale that far.
1383 first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
1384 if (!xfs_verify_fileoff(mp, first_unmap_block)) {
1385 WARN_ON_ONCE(first_unmap_block > XFS_MAX_FILEOFF);
1389 unmap_len = XFS_MAX_FILEOFF - first_unmap_block + 1;
1390 while (unmap_len > 0) {
1391 ASSERT(tp->t_firstblock == NULLFSBLOCK);
1392 error = __xfs_bunmapi(tp, ip, first_unmap_block, &unmap_len,
1393 flags, XFS_ITRUNC_MAX_EXTENTS);
1397 /* free the just unmapped extents */
1398 error = xfs_defer_finish(&tp);
1403 if (whichfork == XFS_DATA_FORK) {
1404 /* Remove all pending CoW reservations. */
1405 error = xfs_reflink_cancel_cow_blocks(ip, &tp,
1406 first_unmap_block, XFS_MAX_FILEOFF, true);
1410 xfs_itruncate_clear_reflink_flags(ip);
1414 * Always re-log the inode so that our permanent transaction can keep
1415 * on rolling it forward in the log.
1417 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1419 trace_xfs_itruncate_extents_end(ip, new_size);
1430 xfs_mount_t *mp = ip->i_mount;
1433 if (!S_ISREG(VFS_I(ip)->i_mode) || (VFS_I(ip)->i_mode == 0))
1436 /* If this is a read-only mount, don't do this (would generate I/O) */
1437 if (xfs_is_readonly(mp))
1440 if (!xfs_is_shutdown(mp)) {
1444 * If we previously truncated this file and removed old data
1445 * in the process, we want to initiate "early" writeout on
1446 * the last close. This is an attempt to combat the notorious
1447 * NULL files problem which is particularly noticeable from a
1448 * truncate down, buffered (re-)write (delalloc), followed by
1449 * a crash. What we are effectively doing here is
1450 * significantly reducing the time window where we'd otherwise
1451 * be exposed to that problem.
1453 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
1455 xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
1456 if (ip->i_delayed_blks > 0) {
1457 error = filemap_flush(VFS_I(ip)->i_mapping);
1464 if (VFS_I(ip)->i_nlink == 0)
1468 * If we can't get the iolock just skip truncating the blocks past EOF
1469 * because we could deadlock with the mmap_lock otherwise. We'll get
1470 * another chance to drop them once the last reference to the inode is
1471 * dropped, so we'll never leak blocks permanently.
1473 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL))
1476 if (xfs_can_free_eofblocks(ip, false)) {
1478 * Check if the inode is being opened, written and closed
1479 * frequently and we have delayed allocation blocks outstanding
1480 * (e.g. streaming writes from the NFS server), truncating the
1481 * blocks past EOF will cause fragmentation to occur.
1483 * In this case don't do the truncation, but we have to be
1484 * careful how we detect this case. Blocks beyond EOF show up as
1485 * i_delayed_blks even when the inode is clean, so we need to
1486 * truncate them away first before checking for a dirty release.
1487 * Hence on the first dirty close we will still remove the
1488 * speculative allocation, but after that we will leave it in
1491 if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
1494 error = xfs_free_eofblocks(ip);
1498 /* delalloc blocks after truncation means it really is dirty */
1499 if (ip->i_delayed_blks)
1500 xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
1504 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1509 * xfs_inactive_truncate
1511 * Called to perform a truncate when an inode becomes unlinked.
1514 xfs_inactive_truncate(
1515 struct xfs_inode *ip)
1517 struct xfs_mount *mp = ip->i_mount;
1518 struct xfs_trans *tp;
1521 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
1523 ASSERT(xfs_is_shutdown(mp));
1526 xfs_ilock(ip, XFS_ILOCK_EXCL);
1527 xfs_trans_ijoin(tp, ip, 0);
1530 * Log the inode size first to prevent stale data exposure in the event
1531 * of a system crash before the truncate completes. See the related
1532 * comment in xfs_vn_setattr_size() for details.
1534 ip->i_disk_size = 0;
1535 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1537 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
1539 goto error_trans_cancel;
1541 ASSERT(ip->i_df.if_nextents == 0);
1543 error = xfs_trans_commit(tp);
1547 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1551 xfs_trans_cancel(tp);
1553 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1558 * xfs_inactive_ifree()
1560 * Perform the inode free when an inode is unlinked.
1564 struct xfs_inode *ip)
1566 struct xfs_mount *mp = ip->i_mount;
1567 struct xfs_trans *tp;
1571 * We try to use a per-AG reservation for any block needed by the finobt
1572 * tree, but as the finobt feature predates the per-AG reservation
1573 * support a degraded file system might not have enough space for the
1574 * reservation at mount time. In that case try to dip into the reserved
1577 * Send a warning if the reservation does happen to fail, as the inode
1578 * now remains allocated and sits on the unlinked list until the fs is
1581 if (unlikely(mp->m_finobt_nores)) {
1582 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ifree,
1583 XFS_IFREE_SPACE_RES(mp), 0, XFS_TRANS_RESERVE,
1586 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ifree, 0, 0, 0, &tp);
1589 if (error == -ENOSPC) {
1590 xfs_warn_ratelimited(mp,
1591 "Failed to remove inode(s) from unlinked list. "
1592 "Please free space, unmount and run xfs_repair.");
1594 ASSERT(xfs_is_shutdown(mp));
1600 * We do not hold the inode locked across the entire rolling transaction
1601 * here. We only need to hold it for the first transaction that
1602 * xfs_ifree() builds, which may mark the inode XFS_ISTALE if the
1603 * underlying cluster buffer is freed. Relogging an XFS_ISTALE inode
1604 * here breaks the relationship between cluster buffer invalidation and
1605 * stale inode invalidation on cluster buffer item journal commit
1606 * completion, and can result in leaving dirty stale inodes hanging
1609 * We have no need for serialising this inode operation against other
1610 * operations - we freed the inode and hence reallocation is required
1611 * and that will serialise on reallocating the space the deferops need
1612 * to free. Hence we can unlock the inode on the first commit of
1613 * the transaction rather than roll it right through the deferops. This
1614 * avoids relogging the XFS_ISTALE inode.
1616 * We check that xfs_ifree() hasn't grown an internal transaction roll
1617 * by asserting that the inode is still locked when it returns.
1619 xfs_ilock(ip, XFS_ILOCK_EXCL);
1620 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1622 error = xfs_ifree(tp, ip);
1623 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1626 * If we fail to free the inode, shut down. The cancel
1627 * might do that, we need to make sure. Otherwise the
1628 * inode might be lost for a long time or forever.
1630 if (!xfs_is_shutdown(mp)) {
1631 xfs_notice(mp, "%s: xfs_ifree returned error %d",
1633 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1635 xfs_trans_cancel(tp);
1640 * Credit the quota account(s). The inode is gone.
1642 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1645 * Just ignore errors at this point. There is nothing we can do except
1646 * to try to keep going. Make sure it's not a silent error.
1648 error = xfs_trans_commit(tp);
1650 xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
1657 * Returns true if we need to update the on-disk metadata before we can free
1658 * the memory used by this inode. Updates include freeing post-eof
1659 * preallocations; freeing COW staging extents; and marking the inode free in
1660 * the inobt if it is on the unlinked list.
1663 xfs_inode_needs_inactive(
1664 struct xfs_inode *ip)
1666 struct xfs_mount *mp = ip->i_mount;
1667 struct xfs_ifork *cow_ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
1670 * If the inode is already free, then there can be nothing
1673 if (VFS_I(ip)->i_mode == 0)
1676 /* If this is a read-only mount, don't do this (would generate I/O) */
1677 if (xfs_is_readonly(mp))
1680 /* If the log isn't running, push inodes straight to reclaim. */
1681 if (xfs_is_shutdown(mp) || xfs_has_norecovery(mp))
1684 /* Metadata inodes require explicit resource cleanup. */
1685 if (xfs_is_metadata_inode(ip))
1688 /* Want to clean out the cow blocks if there are any. */
1689 if (cow_ifp && cow_ifp->if_bytes > 0)
1692 /* Unlinked files must be freed. */
1693 if (VFS_I(ip)->i_nlink == 0)
1697 * This file isn't being freed, so check if there are post-eof blocks
1698 * to free. @force is true because we are evicting an inode from the
1699 * cache. Post-eof blocks must be freed, lest we end up with broken
1700 * free space accounting.
1702 * Note: don't bother with iolock here since lockdep complains about
1703 * acquiring it in reclaim context. We have the only reference to the
1704 * inode at this point anyways.
1706 return xfs_can_free_eofblocks(ip, true);
1712 * This is called when the vnode reference count for the vnode
1713 * goes to zero. If the file has been unlinked, then it must
1714 * now be truncated. Also, we clear all of the read-ahead state
1715 * kept for the inode here since the file is now closed.
1721 struct xfs_mount *mp;
1726 * If the inode is already free, then there can be nothing
1729 if (VFS_I(ip)->i_mode == 0) {
1730 ASSERT(ip->i_df.if_broot_bytes == 0);
1735 ASSERT(!xfs_iflags_test(ip, XFS_IRECOVERY));
1737 /* If this is a read-only mount, don't do this (would generate I/O) */
1738 if (xfs_is_readonly(mp))
1741 /* Metadata inodes require explicit resource cleanup. */
1742 if (xfs_is_metadata_inode(ip))
1745 /* Try to clean out the cow blocks if there are any. */
1746 if (xfs_inode_has_cow_data(ip))
1747 xfs_reflink_cancel_cow_range(ip, 0, NULLFILEOFF, true);
1749 if (VFS_I(ip)->i_nlink != 0) {
1751 * force is true because we are evicting an inode from the
1752 * cache. Post-eof blocks must be freed, lest we end up with
1753 * broken free space accounting.
1755 * Note: don't bother with iolock here since lockdep complains
1756 * about acquiring it in reclaim context. We have the only
1757 * reference to the inode at this point anyways.
1759 if (xfs_can_free_eofblocks(ip, true))
1760 xfs_free_eofblocks(ip);
1765 if (S_ISREG(VFS_I(ip)->i_mode) &&
1766 (ip->i_disk_size != 0 || XFS_ISIZE(ip) != 0 ||
1767 ip->i_df.if_nextents > 0 || ip->i_delayed_blks > 0))
1770 error = xfs_qm_dqattach(ip);
1774 if (S_ISLNK(VFS_I(ip)->i_mode))
1775 error = xfs_inactive_symlink(ip);
1777 error = xfs_inactive_truncate(ip);
1782 * If there are attributes associated with the file then blow them away
1783 * now. The code calls a routine that recursively deconstructs the
1784 * attribute fork. If also blows away the in-core attribute fork.
1786 if (XFS_IFORK_Q(ip)) {
1787 error = xfs_attr_inactive(ip);
1793 ASSERT(ip->i_forkoff == 0);
1798 xfs_inactive_ifree(ip);
1802 * We're done making metadata updates for this inode, so we can release
1803 * the attached dquots.
1805 xfs_qm_dqdetach(ip);
1809 * In-Core Unlinked List Lookups
1810 * =============================
1812 * Every inode is supposed to be reachable from some other piece of metadata
1813 * with the exception of the root directory. Inodes with a connection to a
1814 * file descriptor but not linked from anywhere in the on-disk directory tree
1815 * are collectively known as unlinked inodes, though the filesystem itself
1816 * maintains links to these inodes so that on-disk metadata are consistent.
1818 * XFS implements a per-AG on-disk hash table of unlinked inodes. The AGI
1819 * header contains a number of buckets that point to an inode, and each inode
1820 * record has a pointer to the next inode in the hash chain. This
1821 * singly-linked list causes scaling problems in the iunlink remove function
1822 * because we must walk that list to find the inode that points to the inode
1823 * being removed from the unlinked hash bucket list.
1825 * What if we modelled the unlinked list as a collection of records capturing
1826 * "X.next_unlinked = Y" relations? If we indexed those records on Y, we'd
1827 * have a fast way to look up unlinked list predecessors, which avoids the
1828 * slow list walk. That's exactly what we do here (in-core) with a per-AG
1831 * Because this is a backref cache, we ignore operational failures since the
1832 * iunlink code can fall back to the slow bucket walk. The only errors that
1833 * should bubble out are for obviously incorrect situations.
1835 * All users of the backref cache MUST hold the AGI buffer lock to serialize
1836 * access or have otherwise provided for concurrency control.
1839 /* Capture a "X.next_unlinked = Y" relationship. */
1840 struct xfs_iunlink {
1841 struct rhash_head iu_rhash_head;
1842 xfs_agino_t iu_agino; /* X */
1843 xfs_agino_t iu_next_unlinked; /* Y */
1846 /* Unlinked list predecessor lookup hashtable construction */
1848 xfs_iunlink_obj_cmpfn(
1849 struct rhashtable_compare_arg *arg,
1852 const xfs_agino_t *key = arg->key;
1853 const struct xfs_iunlink *iu = obj;
1855 if (iu->iu_next_unlinked != *key)
1860 static const struct rhashtable_params xfs_iunlink_hash_params = {
1861 .min_size = XFS_AGI_UNLINKED_BUCKETS,
1862 .key_len = sizeof(xfs_agino_t),
1863 .key_offset = offsetof(struct xfs_iunlink,
1865 .head_offset = offsetof(struct xfs_iunlink, iu_rhash_head),
1866 .automatic_shrinking = true,
1867 .obj_cmpfn = xfs_iunlink_obj_cmpfn,
1871 * Return X, where X.next_unlinked == @agino. Returns NULLAGINO if no such
1872 * relation is found.
1875 xfs_iunlink_lookup_backref(
1876 struct xfs_perag *pag,
1879 struct xfs_iunlink *iu;
1881 iu = rhashtable_lookup_fast(&pag->pagi_unlinked_hash, &agino,
1882 xfs_iunlink_hash_params);
1883 return iu ? iu->iu_agino : NULLAGINO;
1887 * Take ownership of an iunlink cache entry and insert it into the hash table.
1888 * If successful, the entry will be owned by the cache; if not, it is freed.
1889 * Either way, the caller does not own @iu after this call.
1892 xfs_iunlink_insert_backref(
1893 struct xfs_perag *pag,
1894 struct xfs_iunlink *iu)
1898 error = rhashtable_insert_fast(&pag->pagi_unlinked_hash,
1899 &iu->iu_rhash_head, xfs_iunlink_hash_params);
1901 * Fail loudly if there already was an entry because that's a sign of
1902 * corruption of in-memory data. Also fail loudly if we see an error
1903 * code we didn't anticipate from the rhashtable code. Currently we
1904 * only anticipate ENOMEM.
1907 WARN(error != -ENOMEM, "iunlink cache insert error %d", error);
1911 * Absorb any runtime errors that aren't a result of corruption because
1912 * this is a cache and we can always fall back to bucket list scanning.
1914 if (error != 0 && error != -EEXIST)
1919 /* Remember that @prev_agino.next_unlinked = @this_agino. */
1921 xfs_iunlink_add_backref(
1922 struct xfs_perag *pag,
1923 xfs_agino_t prev_agino,
1924 xfs_agino_t this_agino)
1926 struct xfs_iunlink *iu;
1928 if (XFS_TEST_ERROR(false, pag->pag_mount, XFS_ERRTAG_IUNLINK_FALLBACK))
1931 iu = kmem_zalloc(sizeof(*iu), KM_NOFS);
1932 iu->iu_agino = prev_agino;
1933 iu->iu_next_unlinked = this_agino;
1935 return xfs_iunlink_insert_backref(pag, iu);
1939 * Replace X.next_unlinked = @agino with X.next_unlinked = @next_unlinked.
1940 * If @next_unlinked is NULLAGINO, we drop the backref and exit. If there
1941 * wasn't any such entry then we don't bother.
1944 xfs_iunlink_change_backref(
1945 struct xfs_perag *pag,
1947 xfs_agino_t next_unlinked)
1949 struct xfs_iunlink *iu;
1952 /* Look up the old entry; if there wasn't one then exit. */
1953 iu = rhashtable_lookup_fast(&pag->pagi_unlinked_hash, &agino,
1954 xfs_iunlink_hash_params);
1959 * Remove the entry. This shouldn't ever return an error, but if we
1960 * couldn't remove the old entry we don't want to add it again to the
1961 * hash table, and if the entry disappeared on us then someone's
1962 * violated the locking rules and we need to fail loudly. Either way
1963 * we cannot remove the inode because internal state is or would have
1966 error = rhashtable_remove_fast(&pag->pagi_unlinked_hash,
1967 &iu->iu_rhash_head, xfs_iunlink_hash_params);
1971 /* If there is no new next entry just free our item and return. */
1972 if (next_unlinked == NULLAGINO) {
1977 /* Update the entry and re-add it to the hash table. */
1978 iu->iu_next_unlinked = next_unlinked;
1979 return xfs_iunlink_insert_backref(pag, iu);
1982 /* Set up the in-core predecessor structures. */
1985 struct xfs_perag *pag)
1987 return rhashtable_init(&pag->pagi_unlinked_hash,
1988 &xfs_iunlink_hash_params);
1991 /* Free the in-core predecessor structures. */
1993 xfs_iunlink_free_item(
1997 struct xfs_iunlink *iu = ptr;
1998 bool *freed_anything = arg;
2000 *freed_anything = true;
2005 xfs_iunlink_destroy(
2006 struct xfs_perag *pag)
2008 bool freed_anything = false;
2010 rhashtable_free_and_destroy(&pag->pagi_unlinked_hash,
2011 xfs_iunlink_free_item, &freed_anything);
2013 ASSERT(freed_anything == false || xfs_is_shutdown(pag->pag_mount));
2017 * Point the AGI unlinked bucket at an inode and log the results. The caller
2018 * is responsible for validating the old value.
2021 xfs_iunlink_update_bucket(
2022 struct xfs_trans *tp,
2023 struct xfs_perag *pag,
2024 struct xfs_buf *agibp,
2025 unsigned int bucket_index,
2026 xfs_agino_t new_agino)
2028 struct xfs_agi *agi = agibp->b_addr;
2029 xfs_agino_t old_value;
2032 ASSERT(xfs_verify_agino_or_null(tp->t_mountp, pag->pag_agno, new_agino));
2034 old_value = be32_to_cpu(agi->agi_unlinked[bucket_index]);
2035 trace_xfs_iunlink_update_bucket(tp->t_mountp, pag->pag_agno, bucket_index,
2036 old_value, new_agino);
2039 * We should never find the head of the list already set to the value
2040 * passed in because either we're adding or removing ourselves from the
2043 if (old_value == new_agino) {
2044 xfs_buf_mark_corrupt(agibp);
2045 return -EFSCORRUPTED;
2048 agi->agi_unlinked[bucket_index] = cpu_to_be32(new_agino);
2049 offset = offsetof(struct xfs_agi, agi_unlinked) +
2050 (sizeof(xfs_agino_t) * bucket_index);
2051 xfs_trans_log_buf(tp, agibp, offset, offset + sizeof(xfs_agino_t) - 1);
2055 /* Set an on-disk inode's next_unlinked pointer. */
2057 xfs_iunlink_update_dinode(
2058 struct xfs_trans *tp,
2059 struct xfs_perag *pag,
2061 struct xfs_buf *ibp,
2062 struct xfs_dinode *dip,
2063 struct xfs_imap *imap,
2064 xfs_agino_t next_agino)
2066 struct xfs_mount *mp = tp->t_mountp;
2069 ASSERT(xfs_verify_agino_or_null(mp, pag->pag_agno, next_agino));
2071 trace_xfs_iunlink_update_dinode(mp, pag->pag_agno, agino,
2072 be32_to_cpu(dip->di_next_unlinked), next_agino);
2074 dip->di_next_unlinked = cpu_to_be32(next_agino);
2075 offset = imap->im_boffset +
2076 offsetof(struct xfs_dinode, di_next_unlinked);
2078 /* need to recalc the inode CRC if appropriate */
2079 xfs_dinode_calc_crc(mp, dip);
2080 xfs_trans_inode_buf(tp, ibp);
2081 xfs_trans_log_buf(tp, ibp, offset, offset + sizeof(xfs_agino_t) - 1);
2084 /* Set an in-core inode's unlinked pointer and return the old value. */
2086 xfs_iunlink_update_inode(
2087 struct xfs_trans *tp,
2088 struct xfs_inode *ip,
2089 struct xfs_perag *pag,
2090 xfs_agino_t next_agino,
2091 xfs_agino_t *old_next_agino)
2093 struct xfs_mount *mp = tp->t_mountp;
2094 struct xfs_dinode *dip;
2095 struct xfs_buf *ibp;
2096 xfs_agino_t old_value;
2099 ASSERT(xfs_verify_agino_or_null(mp, pag->pag_agno, next_agino));
2101 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &ibp);
2104 dip = xfs_buf_offset(ibp, ip->i_imap.im_boffset);
2106 /* Make sure the old pointer isn't garbage. */
2107 old_value = be32_to_cpu(dip->di_next_unlinked);
2108 if (!xfs_verify_agino_or_null(mp, pag->pag_agno, old_value)) {
2109 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, dip,
2110 sizeof(*dip), __this_address);
2111 error = -EFSCORRUPTED;
2116 * Since we're updating a linked list, we should never find that the
2117 * current pointer is the same as the new value, unless we're
2118 * terminating the list.
2120 *old_next_agino = old_value;
2121 if (old_value == next_agino) {
2122 if (next_agino != NULLAGINO) {
2123 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__,
2124 dip, sizeof(*dip), __this_address);
2125 error = -EFSCORRUPTED;
2130 /* Ok, update the new pointer. */
2131 xfs_iunlink_update_dinode(tp, pag, XFS_INO_TO_AGINO(mp, ip->i_ino),
2132 ibp, dip, &ip->i_imap, next_agino);
2135 xfs_trans_brelse(tp, ibp);
2140 * This is called when the inode's link count has gone to 0 or we are creating
2141 * a tmpfile via O_TMPFILE. The inode @ip must have nlink == 0.
2143 * We place the on-disk inode on a list in the AGI. It will be pulled from this
2144 * list when the inode is freed.
2148 struct xfs_trans *tp,
2149 struct xfs_inode *ip)
2151 struct xfs_mount *mp = tp->t_mountp;
2152 struct xfs_perag *pag;
2153 struct xfs_agi *agi;
2154 struct xfs_buf *agibp;
2155 xfs_agino_t next_agino;
2156 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
2157 short bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
2160 ASSERT(VFS_I(ip)->i_nlink == 0);
2161 ASSERT(VFS_I(ip)->i_mode != 0);
2162 trace_xfs_iunlink(ip);
2164 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
2166 /* Get the agi buffer first. It ensures lock ordering on the list. */
2167 error = xfs_read_agi(mp, tp, pag->pag_agno, &agibp);
2170 agi = agibp->b_addr;
2173 * Get the index into the agi hash table for the list this inode will
2174 * go on. Make sure the pointer isn't garbage and that this inode
2175 * isn't already on the list.
2177 next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
2178 if (next_agino == agino ||
2179 !xfs_verify_agino_or_null(mp, pag->pag_agno, next_agino)) {
2180 xfs_buf_mark_corrupt(agibp);
2181 error = -EFSCORRUPTED;
2185 if (next_agino != NULLAGINO) {
2186 xfs_agino_t old_agino;
2189 * There is already another inode in the bucket, so point this
2190 * inode to the current head of the list.
2192 error = xfs_iunlink_update_inode(tp, ip, pag, next_agino,
2196 ASSERT(old_agino == NULLAGINO);
2199 * agino has been unlinked, add a backref from the next inode
2202 error = xfs_iunlink_add_backref(pag, agino, next_agino);
2207 /* Point the head of the list to point to this inode. */
2208 error = xfs_iunlink_update_bucket(tp, pag, agibp, bucket_index, agino);
2214 /* Return the imap, dinode pointer, and buffer for an inode. */
2216 xfs_iunlink_map_ino(
2217 struct xfs_trans *tp,
2218 xfs_agnumber_t agno,
2220 struct xfs_imap *imap,
2221 struct xfs_dinode **dipp,
2222 struct xfs_buf **bpp)
2224 struct xfs_mount *mp = tp->t_mountp;
2228 error = xfs_imap(mp, tp, XFS_AGINO_TO_INO(mp, agno, agino), imap, 0);
2230 xfs_warn(mp, "%s: xfs_imap returned error %d.",
2235 error = xfs_imap_to_bp(mp, tp, imap, bpp);
2237 xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
2242 *dipp = xfs_buf_offset(*bpp, imap->im_boffset);
2247 * Walk the unlinked chain from @head_agino until we find the inode that
2248 * points to @target_agino. Return the inode number, map, dinode pointer,
2249 * and inode cluster buffer of that inode as @agino, @imap, @dipp, and @bpp.
2251 * @tp, @pag, @head_agino, and @target_agino are input parameters.
2252 * @agino, @imap, @dipp, and @bpp are all output parameters.
2254 * Do not call this function if @target_agino is the head of the list.
2257 xfs_iunlink_map_prev(
2258 struct xfs_trans *tp,
2259 struct xfs_perag *pag,
2260 xfs_agino_t head_agino,
2261 xfs_agino_t target_agino,
2263 struct xfs_imap *imap,
2264 struct xfs_dinode **dipp,
2265 struct xfs_buf **bpp)
2267 struct xfs_mount *mp = tp->t_mountp;
2268 xfs_agino_t next_agino;
2271 ASSERT(head_agino != target_agino);
2274 /* See if our backref cache can find it faster. */
2275 *agino = xfs_iunlink_lookup_backref(pag, target_agino);
2276 if (*agino != NULLAGINO) {
2277 error = xfs_iunlink_map_ino(tp, pag->pag_agno, *agino, imap,
2282 if (be32_to_cpu((*dipp)->di_next_unlinked) == target_agino)
2286 * If we get here the cache contents were corrupt, so drop the
2287 * buffer and fall back to walking the bucket list.
2289 xfs_trans_brelse(tp, *bpp);
2294 trace_xfs_iunlink_map_prev_fallback(mp, pag->pag_agno);
2296 /* Otherwise, walk the entire bucket until we find it. */
2297 next_agino = head_agino;
2298 while (next_agino != target_agino) {
2299 xfs_agino_t unlinked_agino;
2302 xfs_trans_brelse(tp, *bpp);
2304 *agino = next_agino;
2305 error = xfs_iunlink_map_ino(tp, pag->pag_agno, next_agino, imap,
2310 unlinked_agino = be32_to_cpu((*dipp)->di_next_unlinked);
2312 * Make sure this pointer is valid and isn't an obvious
2315 if (!xfs_verify_agino(mp, pag->pag_agno, unlinked_agino) ||
2316 next_agino == unlinked_agino) {
2317 XFS_CORRUPTION_ERROR(__func__,
2318 XFS_ERRLEVEL_LOW, mp,
2319 *dipp, sizeof(**dipp));
2320 error = -EFSCORRUPTED;
2323 next_agino = unlinked_agino;
2330 * Pull the on-disk inode from the AGI unlinked list.
2334 struct xfs_trans *tp,
2335 struct xfs_perag *pag,
2336 struct xfs_inode *ip)
2338 struct xfs_mount *mp = tp->t_mountp;
2339 struct xfs_agi *agi;
2340 struct xfs_buf *agibp;
2341 struct xfs_buf *last_ibp;
2342 struct xfs_dinode *last_dip = NULL;
2343 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
2344 xfs_agino_t next_agino;
2345 xfs_agino_t head_agino;
2346 short bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
2349 trace_xfs_iunlink_remove(ip);
2351 /* Get the agi buffer first. It ensures lock ordering on the list. */
2352 error = xfs_read_agi(mp, tp, pag->pag_agno, &agibp);
2355 agi = agibp->b_addr;
2358 * Get the index into the agi hash table for the list this inode will
2359 * go on. Make sure the head pointer isn't garbage.
2361 head_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
2362 if (!xfs_verify_agino(mp, pag->pag_agno, head_agino)) {
2363 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
2365 return -EFSCORRUPTED;
2369 * Set our inode's next_unlinked pointer to NULL and then return
2370 * the old pointer value so that we can update whatever was previous
2371 * to us in the list to point to whatever was next in the list.
2373 error = xfs_iunlink_update_inode(tp, ip, pag, NULLAGINO, &next_agino);
2378 * If there was a backref pointing from the next inode back to this
2379 * one, remove it because we've removed this inode from the list.
2381 * Later, if this inode was in the middle of the list we'll update
2382 * this inode's backref to point from the next inode.
2384 if (next_agino != NULLAGINO) {
2385 error = xfs_iunlink_change_backref(pag, next_agino, NULLAGINO);
2390 if (head_agino != agino) {
2391 struct xfs_imap imap;
2392 xfs_agino_t prev_agino;
2394 /* We need to search the list for the inode being freed. */
2395 error = xfs_iunlink_map_prev(tp, pag, head_agino, agino,
2396 &prev_agino, &imap, &last_dip, &last_ibp);
2400 /* Point the previous inode on the list to the next inode. */
2401 xfs_iunlink_update_dinode(tp, pag, prev_agino, last_ibp,
2402 last_dip, &imap, next_agino);
2405 * Now we deal with the backref for this inode. If this inode
2406 * pointed at a real inode, change the backref that pointed to
2407 * us to point to our old next. If this inode was the end of
2408 * the list, delete the backref that pointed to us. Note that
2409 * change_backref takes care of deleting the backref if
2410 * next_agino is NULLAGINO.
2412 return xfs_iunlink_change_backref(agibp->b_pag, agino,
2416 /* Point the head of the list to the next unlinked inode. */
2417 return xfs_iunlink_update_bucket(tp, pag, agibp, bucket_index,
2422 * Look up the inode number specified and if it is not already marked XFS_ISTALE
2423 * mark it stale. We should only find clean inodes in this lookup that aren't
2427 xfs_ifree_mark_inode_stale(
2428 struct xfs_perag *pag,
2429 struct xfs_inode *free_ip,
2432 struct xfs_mount *mp = pag->pag_mount;
2433 struct xfs_inode_log_item *iip;
2434 struct xfs_inode *ip;
2438 ip = radix_tree_lookup(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, inum));
2440 /* Inode not in memory, nothing to do */
2447 * because this is an RCU protected lookup, we could find a recently
2448 * freed or even reallocated inode during the lookup. We need to check
2449 * under the i_flags_lock for a valid inode here. Skip it if it is not
2450 * valid, the wrong inode or stale.
2452 spin_lock(&ip->i_flags_lock);
2453 if (ip->i_ino != inum || __xfs_iflags_test(ip, XFS_ISTALE))
2454 goto out_iflags_unlock;
2457 * Don't try to lock/unlock the current inode, but we _cannot_ skip the
2458 * other inodes that we did not find in the list attached to the buffer
2459 * and are not already marked stale. If we can't lock it, back off and
2462 if (ip != free_ip) {
2463 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
2464 spin_unlock(&ip->i_flags_lock);
2470 ip->i_flags |= XFS_ISTALE;
2473 * If the inode is flushing, it is already attached to the buffer. All
2474 * we needed to do here is mark the inode stale so buffer IO completion
2475 * will remove it from the AIL.
2478 if (__xfs_iflags_test(ip, XFS_IFLUSHING)) {
2479 ASSERT(!list_empty(&iip->ili_item.li_bio_list));
2480 ASSERT(iip->ili_last_fields);
2485 * Inodes not attached to the buffer can be released immediately.
2486 * Everything else has to go through xfs_iflush_abort() on journal
2487 * commit as the flock synchronises removal of the inode from the
2488 * cluster buffer against inode reclaim.
2490 if (!iip || list_empty(&iip->ili_item.li_bio_list))
2493 __xfs_iflags_set(ip, XFS_IFLUSHING);
2494 spin_unlock(&ip->i_flags_lock);
2497 /* we have a dirty inode in memory that has not yet been flushed. */
2498 spin_lock(&iip->ili_lock);
2499 iip->ili_last_fields = iip->ili_fields;
2500 iip->ili_fields = 0;
2501 iip->ili_fsync_fields = 0;
2502 spin_unlock(&iip->ili_lock);
2503 ASSERT(iip->ili_last_fields);
2506 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2511 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2513 spin_unlock(&ip->i_flags_lock);
2518 * A big issue when freeing the inode cluster is that we _cannot_ skip any
2519 * inodes that are in memory - they all must be marked stale and attached to
2520 * the cluster buffer.
2524 struct xfs_trans *tp,
2525 struct xfs_perag *pag,
2526 struct xfs_inode *free_ip,
2527 struct xfs_icluster *xic)
2529 struct xfs_mount *mp = free_ip->i_mount;
2530 struct xfs_ino_geometry *igeo = M_IGEO(mp);
2533 xfs_ino_t inum = xic->first_ino;
2539 nbufs = igeo->ialloc_blks / igeo->blocks_per_cluster;
2541 for (j = 0; j < nbufs; j++, inum += igeo->inodes_per_cluster) {
2543 * The allocation bitmap tells us which inodes of the chunk were
2544 * physically allocated. Skip the cluster if an inode falls into
2547 ioffset = inum - xic->first_ino;
2548 if ((xic->alloc & XFS_INOBT_MASK(ioffset)) == 0) {
2549 ASSERT(ioffset % igeo->inodes_per_cluster == 0);
2553 blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
2554 XFS_INO_TO_AGBNO(mp, inum));
2557 * We obtain and lock the backing buffer first in the process
2558 * here to ensure dirty inodes attached to the buffer remain in
2559 * the flushing state while we mark them stale.
2561 * If we scan the in-memory inodes first, then buffer IO can
2562 * complete before we get a lock on it, and hence we may fail
2563 * to mark all the active inodes on the buffer stale.
2565 error = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
2566 mp->m_bsize * igeo->blocks_per_cluster,
2572 * This buffer may not have been correctly initialised as we
2573 * didn't read it from disk. That's not important because we are
2574 * only using to mark the buffer as stale in the log, and to
2575 * attach stale cached inodes on it. That means it will never be
2576 * dispatched for IO. If it is, we want to know about it, and we
2577 * want it to fail. We can acheive this by adding a write
2578 * verifier to the buffer.
2580 bp->b_ops = &xfs_inode_buf_ops;
2583 * Now we need to set all the cached clean inodes as XFS_ISTALE,
2584 * too. This requires lookups, and will skip inodes that we've
2585 * already marked XFS_ISTALE.
2587 for (i = 0; i < igeo->inodes_per_cluster; i++)
2588 xfs_ifree_mark_inode_stale(pag, free_ip, inum + i);
2590 xfs_trans_stale_inode_buf(tp, bp);
2591 xfs_trans_binval(tp, bp);
2597 * This is called to return an inode to the inode free list.
2598 * The inode should already be truncated to 0 length and have
2599 * no pages associated with it. This routine also assumes that
2600 * the inode is already a part of the transaction.
2602 * The on-disk copy of the inode will have been added to the list
2603 * of unlinked inodes in the AGI. We need to remove the inode from
2604 * that list atomically with respect to freeing it here.
2608 struct xfs_trans *tp,
2609 struct xfs_inode *ip)
2611 struct xfs_mount *mp = ip->i_mount;
2612 struct xfs_perag *pag;
2613 struct xfs_icluster xic = { 0 };
2614 struct xfs_inode_log_item *iip = ip->i_itemp;
2617 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
2618 ASSERT(VFS_I(ip)->i_nlink == 0);
2619 ASSERT(ip->i_df.if_nextents == 0);
2620 ASSERT(ip->i_disk_size == 0 || !S_ISREG(VFS_I(ip)->i_mode));
2621 ASSERT(ip->i_nblocks == 0);
2623 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
2626 * Pull the on-disk inode from the AGI unlinked list.
2628 error = xfs_iunlink_remove(tp, pag, ip);
2632 error = xfs_difree(tp, pag, ip->i_ino, &xic);
2637 * Free any local-format data sitting around before we reset the
2638 * data fork to extents format. Note that the attr fork data has
2639 * already been freed by xfs_attr_inactive.
2641 if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
2642 kmem_free(ip->i_df.if_u1.if_data);
2643 ip->i_df.if_u1.if_data = NULL;
2644 ip->i_df.if_bytes = 0;
2647 VFS_I(ip)->i_mode = 0; /* mark incore inode as free */
2649 ip->i_diflags2 = mp->m_ino_geo.new_diflags2;
2650 ip->i_forkoff = 0; /* mark the attr fork not in use */
2651 ip->i_df.if_format = XFS_DINODE_FMT_EXTENTS;
2652 if (xfs_iflags_test(ip, XFS_IPRESERVE_DM_FIELDS))
2653 xfs_iflags_clear(ip, XFS_IPRESERVE_DM_FIELDS);
2655 /* Don't attempt to replay owner changes for a deleted inode */
2656 spin_lock(&iip->ili_lock);
2657 iip->ili_fields &= ~(XFS_ILOG_AOWNER | XFS_ILOG_DOWNER);
2658 spin_unlock(&iip->ili_lock);
2661 * Bump the generation count so no one will be confused
2662 * by reincarnations of this inode.
2664 VFS_I(ip)->i_generation++;
2665 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2668 error = xfs_ifree_cluster(tp, pag, ip, &xic);
2675 * This is called to unpin an inode. The caller must have the inode locked
2676 * in at least shared mode so that the buffer cannot be subsequently pinned
2677 * once someone is waiting for it to be unpinned.
2681 struct xfs_inode *ip)
2683 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
2685 trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
2687 /* Give the log a push to start the unpinning I/O */
2688 xfs_log_force_seq(ip->i_mount, ip->i_itemp->ili_commit_seq, 0, NULL);
2694 struct xfs_inode *ip)
2696 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
2697 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
2702 prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
2703 if (xfs_ipincount(ip))
2705 } while (xfs_ipincount(ip));
2706 finish_wait(wq, &wait.wq_entry);
2711 struct xfs_inode *ip)
2713 if (xfs_ipincount(ip))
2714 __xfs_iunpin_wait(ip);
2718 * Removing an inode from the namespace involves removing the directory entry
2719 * and dropping the link count on the inode. Removing the directory entry can
2720 * result in locking an AGF (directory blocks were freed) and removing a link
2721 * count can result in placing the inode on an unlinked list which results in
2724 * The big problem here is that we have an ordering constraint on AGF and AGI
2725 * locking - inode allocation locks the AGI, then can allocate a new extent for
2726 * new inodes, locking the AGF after the AGI. Similarly, freeing the inode
2727 * removes the inode from the unlinked list, requiring that we lock the AGI
2728 * first, and then freeing the inode can result in an inode chunk being freed
2729 * and hence freeing disk space requiring that we lock an AGF.
2731 * Hence the ordering that is imposed by other parts of the code is AGI before
2732 * AGF. This means we cannot remove the directory entry before we drop the inode
2733 * reference count and put it on the unlinked list as this results in a lock
2734 * order of AGF then AGI, and this can deadlock against inode allocation and
2735 * freeing. Therefore we must drop the link counts before we remove the
2738 * This is still safe from a transactional point of view - it is not until we
2739 * get to xfs_defer_finish() that we have the possibility of multiple
2740 * transactions in this operation. Hence as long as we remove the directory
2741 * entry and drop the link count in the first transaction of the remove
2742 * operation, there are no transactional constraints on the ordering here.
2747 struct xfs_name *name,
2750 xfs_mount_t *mp = dp->i_mount;
2751 xfs_trans_t *tp = NULL;
2752 int is_dir = S_ISDIR(VFS_I(ip)->i_mode);
2757 trace_xfs_remove(dp, name);
2759 if (xfs_is_shutdown(mp))
2762 error = xfs_qm_dqattach(dp);
2766 error = xfs_qm_dqattach(ip);
2771 * We try to get the real space reservation first, allowing for
2772 * directory btree deletion(s) implying possible bmap insert(s). If we
2773 * can't get the space reservation then we use 0 instead, and avoid the
2774 * bmap btree insert(s) in the directory code by, if the bmap insert
2775 * tries to happen, instead trimming the LAST block from the directory.
2777 * Ignore EDQUOT and ENOSPC being returned via nospace_error because
2778 * the directory code can handle a reservationless update and we don't
2779 * want to prevent a user from trying to free space by deleting things.
2781 resblks = XFS_REMOVE_SPACE_RES(mp);
2782 error = xfs_trans_alloc_dir(dp, &M_RES(mp)->tr_remove, ip, &resblks,
2785 ASSERT(error != -ENOSPC);
2790 * If we're removing a directory perform some additional validation.
2793 ASSERT(VFS_I(ip)->i_nlink >= 2);
2794 if (VFS_I(ip)->i_nlink != 2) {
2796 goto out_trans_cancel;
2798 if (!xfs_dir_isempty(ip)) {
2800 goto out_trans_cancel;
2803 /* Drop the link from ip's "..". */
2804 error = xfs_droplink(tp, dp);
2806 goto out_trans_cancel;
2808 /* Drop the "." link from ip to self. */
2809 error = xfs_droplink(tp, ip);
2811 goto out_trans_cancel;
2814 * Point the unlinked child directory's ".." entry to the root
2815 * directory to eliminate back-references to inodes that may
2816 * get freed before the child directory is closed. If the fs
2817 * gets shrunk, this can lead to dirent inode validation errors.
2819 if (dp->i_ino != tp->t_mountp->m_sb.sb_rootino) {
2820 error = xfs_dir_replace(tp, ip, &xfs_name_dotdot,
2821 tp->t_mountp->m_sb.sb_rootino, 0);
2827 * When removing a non-directory we need to log the parent
2828 * inode here. For a directory this is done implicitly
2829 * by the xfs_droplink call for the ".." entry.
2831 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2833 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2835 /* Drop the link from dp to ip. */
2836 error = xfs_droplink(tp, ip);
2838 goto out_trans_cancel;
2840 error = xfs_dir_removename(tp, dp, name, ip->i_ino, resblks);
2842 ASSERT(error != -ENOENT);
2843 goto out_trans_cancel;
2847 * If this is a synchronous mount, make sure that the
2848 * remove transaction goes to disk before returning to
2851 if (xfs_has_wsync(mp) || xfs_has_dirsync(mp))
2852 xfs_trans_set_sync(tp);
2854 error = xfs_trans_commit(tp);
2858 if (is_dir && xfs_inode_is_filestream(ip))
2859 xfs_filestream_deassociate(ip);
2864 xfs_trans_cancel(tp);
2870 * Enter all inodes for a rename transaction into a sorted array.
2872 #define __XFS_SORT_INODES 5
2874 xfs_sort_for_rename(
2875 struct xfs_inode *dp1, /* in: old (source) directory inode */
2876 struct xfs_inode *dp2, /* in: new (target) directory inode */
2877 struct xfs_inode *ip1, /* in: inode of old entry */
2878 struct xfs_inode *ip2, /* in: inode of new entry */
2879 struct xfs_inode *wip, /* in: whiteout inode */
2880 struct xfs_inode **i_tab,/* out: sorted array of inodes */
2881 int *num_inodes) /* in/out: inodes in array */
2885 ASSERT(*num_inodes == __XFS_SORT_INODES);
2886 memset(i_tab, 0, *num_inodes * sizeof(struct xfs_inode *));
2889 * i_tab contains a list of pointers to inodes. We initialize
2890 * the table here & we'll sort it. We will then use it to
2891 * order the acquisition of the inode locks.
2893 * Note that the table may contain duplicates. e.g., dp1 == dp2.
2906 * Sort the elements via bubble sort. (Remember, there are at
2907 * most 5 elements to sort, so this is adequate.)
2909 for (i = 0; i < *num_inodes; i++) {
2910 for (j = 1; j < *num_inodes; j++) {
2911 if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) {
2912 struct xfs_inode *temp = i_tab[j];
2913 i_tab[j] = i_tab[j-1];
2922 struct xfs_trans *tp)
2925 * If this is a synchronous mount, make sure that the rename transaction
2926 * goes to disk before returning to the user.
2928 if (xfs_has_wsync(tp->t_mountp) || xfs_has_dirsync(tp->t_mountp))
2929 xfs_trans_set_sync(tp);
2931 return xfs_trans_commit(tp);
2935 * xfs_cross_rename()
2937 * responsible for handling RENAME_EXCHANGE flag in renameat2() syscall
2941 struct xfs_trans *tp,
2942 struct xfs_inode *dp1,
2943 struct xfs_name *name1,
2944 struct xfs_inode *ip1,
2945 struct xfs_inode *dp2,
2946 struct xfs_name *name2,
2947 struct xfs_inode *ip2,
2955 /* Swap inode number for dirent in first parent */
2956 error = xfs_dir_replace(tp, dp1, name1, ip2->i_ino, spaceres);
2958 goto out_trans_abort;
2960 /* Swap inode number for dirent in second parent */
2961 error = xfs_dir_replace(tp, dp2, name2, ip1->i_ino, spaceres);
2963 goto out_trans_abort;
2966 * If we're renaming one or more directories across different parents,
2967 * update the respective ".." entries (and link counts) to match the new
2971 dp2_flags = XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
2973 if (S_ISDIR(VFS_I(ip2)->i_mode)) {
2974 error = xfs_dir_replace(tp, ip2, &xfs_name_dotdot,
2975 dp1->i_ino, spaceres);
2977 goto out_trans_abort;
2979 /* transfer ip2 ".." reference to dp1 */
2980 if (!S_ISDIR(VFS_I(ip1)->i_mode)) {
2981 error = xfs_droplink(tp, dp2);
2983 goto out_trans_abort;
2984 xfs_bumplink(tp, dp1);
2988 * Although ip1 isn't changed here, userspace needs
2989 * to be warned about the change, so that applications
2990 * relying on it (like backup ones), will properly
2993 ip1_flags |= XFS_ICHGTIME_CHG;
2994 ip2_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
2997 if (S_ISDIR(VFS_I(ip1)->i_mode)) {
2998 error = xfs_dir_replace(tp, ip1, &xfs_name_dotdot,
2999 dp2->i_ino, spaceres);
3001 goto out_trans_abort;
3003 /* transfer ip1 ".." reference to dp2 */
3004 if (!S_ISDIR(VFS_I(ip2)->i_mode)) {
3005 error = xfs_droplink(tp, dp1);
3007 goto out_trans_abort;
3008 xfs_bumplink(tp, dp2);
3012 * Although ip2 isn't changed here, userspace needs
3013 * to be warned about the change, so that applications
3014 * relying on it (like backup ones), will properly
3017 ip1_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
3018 ip2_flags |= XFS_ICHGTIME_CHG;
3023 xfs_trans_ichgtime(tp, ip1, ip1_flags);
3024 xfs_trans_log_inode(tp, ip1, XFS_ILOG_CORE);
3027 xfs_trans_ichgtime(tp, ip2, ip2_flags);
3028 xfs_trans_log_inode(tp, ip2, XFS_ILOG_CORE);
3031 xfs_trans_ichgtime(tp, dp2, dp2_flags);
3032 xfs_trans_log_inode(tp, dp2, XFS_ILOG_CORE);
3034 xfs_trans_ichgtime(tp, dp1, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3035 xfs_trans_log_inode(tp, dp1, XFS_ILOG_CORE);
3036 return xfs_finish_rename(tp);
3039 xfs_trans_cancel(tp);
3044 * xfs_rename_alloc_whiteout()
3046 * Return a referenced, unlinked, unlocked inode that can be used as a
3047 * whiteout in a rename transaction. We use a tmpfile inode here so that if we
3048 * crash between allocating the inode and linking it into the rename transaction
3049 * recovery will free the inode and we won't leak it.
3052 xfs_rename_alloc_whiteout(
3053 struct user_namespace *mnt_userns,
3054 struct xfs_inode *dp,
3055 struct xfs_inode **wip)
3057 struct xfs_inode *tmpfile;
3060 error = xfs_create_tmpfile(mnt_userns, dp, S_IFCHR | WHITEOUT_MODE,
3066 * Prepare the tmpfile inode as if it were created through the VFS.
3067 * Complete the inode setup and flag it as linkable. nlink is already
3068 * zero, so we can skip the drop_nlink.
3070 xfs_setup_iops(tmpfile);
3071 xfs_finish_inode_setup(tmpfile);
3072 VFS_I(tmpfile)->i_state |= I_LINKABLE;
3083 struct user_namespace *mnt_userns,
3084 struct xfs_inode *src_dp,
3085 struct xfs_name *src_name,
3086 struct xfs_inode *src_ip,
3087 struct xfs_inode *target_dp,
3088 struct xfs_name *target_name,
3089 struct xfs_inode *target_ip,
3092 struct xfs_mount *mp = src_dp->i_mount;
3093 struct xfs_trans *tp;
3094 struct xfs_inode *wip = NULL; /* whiteout inode */
3095 struct xfs_inode *inodes[__XFS_SORT_INODES];
3097 int num_inodes = __XFS_SORT_INODES;
3098 bool new_parent = (src_dp != target_dp);
3099 bool src_is_directory = S_ISDIR(VFS_I(src_ip)->i_mode);
3101 bool retried = false;
3102 int error, nospace_error = 0;
3104 trace_xfs_rename(src_dp, target_dp, src_name, target_name);
3106 if ((flags & RENAME_EXCHANGE) && !target_ip)
3110 * If we are doing a whiteout operation, allocate the whiteout inode
3111 * we will be placing at the target and ensure the type is set
3114 if (flags & RENAME_WHITEOUT) {
3115 error = xfs_rename_alloc_whiteout(mnt_userns, target_dp, &wip);
3119 /* setup target dirent info as whiteout */
3120 src_name->type = XFS_DIR3_FT_CHRDEV;
3123 xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip, wip,
3124 inodes, &num_inodes);
3128 spaceres = XFS_RENAME_SPACE_RES(mp, target_name->len);
3129 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_rename, spaceres, 0, 0, &tp);
3130 if (error == -ENOSPC) {
3131 nospace_error = error;
3133 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_rename, 0, 0, 0,
3137 goto out_release_wip;
3140 * Attach the dquots to the inodes
3142 error = xfs_qm_vop_rename_dqattach(inodes);
3144 goto out_trans_cancel;
3147 * Lock all the participating inodes. Depending upon whether
3148 * the target_name exists in the target directory, and
3149 * whether the target directory is the same as the source
3150 * directory, we can lock from 2 to 4 inodes.
3152 xfs_lock_inodes(inodes, num_inodes, XFS_ILOCK_EXCL);
3155 * Join all the inodes to the transaction. From this point on,
3156 * we can rely on either trans_commit or trans_cancel to unlock
3159 xfs_trans_ijoin(tp, src_dp, XFS_ILOCK_EXCL);
3161 xfs_trans_ijoin(tp, target_dp, XFS_ILOCK_EXCL);
3162 xfs_trans_ijoin(tp, src_ip, XFS_ILOCK_EXCL);
3164 xfs_trans_ijoin(tp, target_ip, XFS_ILOCK_EXCL);
3166 xfs_trans_ijoin(tp, wip, XFS_ILOCK_EXCL);
3169 * If we are using project inheritance, we only allow renames
3170 * into our tree when the project IDs are the same; else the
3171 * tree quota mechanism would be circumvented.
3173 if (unlikely((target_dp->i_diflags & XFS_DIFLAG_PROJINHERIT) &&
3174 target_dp->i_projid != src_ip->i_projid)) {
3176 goto out_trans_cancel;
3179 /* RENAME_EXCHANGE is unique from here on. */
3180 if (flags & RENAME_EXCHANGE)
3181 return xfs_cross_rename(tp, src_dp, src_name, src_ip,
3182 target_dp, target_name, target_ip,
3186 * Try to reserve quota to handle an expansion of the target directory.
3187 * We'll allow the rename to continue in reservationless mode if we hit
3188 * a space usage constraint. If we trigger reservationless mode, save
3189 * the errno if there isn't any free space in the target directory.
3191 if (spaceres != 0) {
3192 error = xfs_trans_reserve_quota_nblks(tp, target_dp, spaceres,
3194 if (error == -EDQUOT || error == -ENOSPC) {
3196 xfs_trans_cancel(tp);
3197 xfs_blockgc_free_quota(target_dp, 0);
3202 nospace_error = error;
3207 goto out_trans_cancel;
3211 * Check for expected errors before we dirty the transaction
3212 * so we can return an error without a transaction abort.
3214 * Extent count overflow check:
3216 * From the perspective of src_dp, a rename operation is essentially a
3217 * directory entry remove operation. Hence the only place where we check
3218 * for extent count overflow for src_dp is in
3219 * xfs_bmap_del_extent_real(). xfs_bmap_del_extent_real() returns
3220 * -ENOSPC when it detects a possible extent count overflow and in
3221 * response, the higher layers of directory handling code do the
3223 * 1. Data/Free blocks: XFS lets these blocks linger until a
3224 * future remove operation removes them.
3225 * 2. Dabtree blocks: XFS swaps the blocks with the last block in the
3226 * Leaf space and unmaps the last block.
3228 * For target_dp, there are two cases depending on whether the
3229 * destination directory entry exists or not.
3231 * When destination directory entry does not exist (i.e. target_ip ==
3232 * NULL), extent count overflow check is performed only when transaction
3233 * has a non-zero sized space reservation associated with it. With a
3234 * zero-sized space reservation, XFS allows a rename operation to
3235 * continue only when the directory has sufficient free space in its
3236 * data/leaf/free space blocks to hold the new entry.
3238 * When destination directory entry exists (i.e. target_ip != NULL), all
3239 * we need to do is change the inode number associated with the already
3240 * existing entry. Hence there is no need to perform an extent count
3243 if (target_ip == NULL) {
3245 * If there's no space reservation, check the entry will
3246 * fit before actually inserting it.
3249 error = xfs_dir_canenter(tp, target_dp, target_name);
3251 goto out_trans_cancel;
3253 error = xfs_iext_count_may_overflow(target_dp,
3255 XFS_IEXT_DIR_MANIP_CNT(mp));
3257 goto out_trans_cancel;
3261 * If target exists and it's a directory, check that whether
3262 * it can be destroyed.
3264 if (S_ISDIR(VFS_I(target_ip)->i_mode) &&
3265 (!xfs_dir_isempty(target_ip) ||
3266 (VFS_I(target_ip)->i_nlink > 2))) {
3268 goto out_trans_cancel;
3273 * Lock the AGI buffers we need to handle bumping the nlink of the
3274 * whiteout inode off the unlinked list and to handle dropping the
3275 * nlink of the target inode. Per locking order rules, do this in
3276 * increasing AG order and before directory block allocation tries to
3277 * grab AGFs because we grab AGIs before AGFs.
3279 * The (vfs) caller must ensure that if src is a directory then
3280 * target_ip is either null or an empty directory.
3282 for (i = 0; i < num_inodes && inodes[i] != NULL; i++) {
3283 if (inodes[i] == wip ||
3284 (inodes[i] == target_ip &&
3285 (VFS_I(target_ip)->i_nlink == 1 || src_is_directory))) {
3287 xfs_agnumber_t agno;
3289 agno = XFS_INO_TO_AGNO(mp, inodes[i]->i_ino);
3290 error = xfs_read_agi(mp, tp, agno, &bp);
3292 goto out_trans_cancel;
3297 * Directory entry creation below may acquire the AGF. Remove
3298 * the whiteout from the unlinked list first to preserve correct
3299 * AGI/AGF locking order. This dirties the transaction so failures
3300 * after this point will abort and log recovery will clean up the
3303 * For whiteouts, we need to bump the link count on the whiteout
3304 * inode. After this point, we have a real link, clear the tmpfile
3305 * state flag from the inode so it doesn't accidentally get misused
3309 struct xfs_perag *pag;
3311 ASSERT(VFS_I(wip)->i_nlink == 0);
3313 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, wip->i_ino));
3314 error = xfs_iunlink_remove(tp, pag, wip);
3317 goto out_trans_cancel;
3319 xfs_bumplink(tp, wip);
3320 VFS_I(wip)->i_state &= ~I_LINKABLE;
3324 * Set up the target.
3326 if (target_ip == NULL) {
3328 * If target does not exist and the rename crosses
3329 * directories, adjust the target directory link count
3330 * to account for the ".." reference from the new entry.
3332 error = xfs_dir_createname(tp, target_dp, target_name,
3333 src_ip->i_ino, spaceres);
3335 goto out_trans_cancel;
3337 xfs_trans_ichgtime(tp, target_dp,
3338 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3340 if (new_parent && src_is_directory) {
3341 xfs_bumplink(tp, target_dp);
3343 } else { /* target_ip != NULL */
3345 * Link the source inode under the target name.
3346 * If the source inode is a directory and we are moving
3347 * it across directories, its ".." entry will be
3348 * inconsistent until we replace that down below.
3350 * In case there is already an entry with the same
3351 * name at the destination directory, remove it first.
3353 error = xfs_dir_replace(tp, target_dp, target_name,
3354 src_ip->i_ino, spaceres);
3356 goto out_trans_cancel;
3358 xfs_trans_ichgtime(tp, target_dp,
3359 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3362 * Decrement the link count on the target since the target
3363 * dir no longer points to it.
3365 error = xfs_droplink(tp, target_ip);
3367 goto out_trans_cancel;
3369 if (src_is_directory) {
3371 * Drop the link from the old "." entry.
3373 error = xfs_droplink(tp, target_ip);
3375 goto out_trans_cancel;
3377 } /* target_ip != NULL */
3380 * Remove the source.
3382 if (new_parent && src_is_directory) {
3384 * Rewrite the ".." entry to point to the new
3387 error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot,
3388 target_dp->i_ino, spaceres);
3389 ASSERT(error != -EEXIST);
3391 goto out_trans_cancel;
3395 * We always want to hit the ctime on the source inode.
3397 * This isn't strictly required by the standards since the source
3398 * inode isn't really being changed, but old unix file systems did
3399 * it and some incremental backup programs won't work without it.
3401 xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG);
3402 xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE);
3405 * Adjust the link count on src_dp. This is necessary when
3406 * renaming a directory, either within one parent when
3407 * the target existed, or across two parent directories.
3409 if (src_is_directory && (new_parent || target_ip != NULL)) {
3412 * Decrement link count on src_directory since the
3413 * entry that's moved no longer points to it.
3415 error = xfs_droplink(tp, src_dp);
3417 goto out_trans_cancel;
3421 * For whiteouts, we only need to update the source dirent with the
3422 * inode number of the whiteout inode rather than removing it
3426 error = xfs_dir_replace(tp, src_dp, src_name, wip->i_ino,
3430 * NOTE: We don't need to check for extent count overflow here
3431 * because the dir remove name code will leave the dir block in
3432 * place if the extent count would overflow.
3434 error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino,
3439 goto out_trans_cancel;
3441 xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3442 xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE);
3444 xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE);
3446 error = xfs_finish_rename(tp);
3452 xfs_trans_cancel(tp);
3456 if (error == -ENOSPC && nospace_error)
3457 error = nospace_error;
3463 struct xfs_inode *ip,
3466 struct xfs_inode_log_item *iip = ip->i_itemp;
3467 struct xfs_dinode *dip;
3468 struct xfs_mount *mp = ip->i_mount;
3471 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
3472 ASSERT(xfs_iflags_test(ip, XFS_IFLUSHING));
3473 ASSERT(ip->i_df.if_format != XFS_DINODE_FMT_BTREE ||
3474 ip->i_df.if_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
3475 ASSERT(iip->ili_item.li_buf == bp);
3477 dip = xfs_buf_offset(bp, ip->i_imap.im_boffset);
3480 * We don't flush the inode if any of the following checks fail, but we
3481 * do still update the log item and attach to the backing buffer as if
3482 * the flush happened. This is a formality to facilitate predictable
3483 * error handling as the caller will shutdown and fail the buffer.
3485 error = -EFSCORRUPTED;
3486 if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
3487 mp, XFS_ERRTAG_IFLUSH_1)) {
3488 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3489 "%s: Bad inode %Lu magic number 0x%x, ptr "PTR_FMT,
3490 __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
3493 if (S_ISREG(VFS_I(ip)->i_mode)) {
3495 ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS &&
3496 ip->i_df.if_format != XFS_DINODE_FMT_BTREE,
3497 mp, XFS_ERRTAG_IFLUSH_3)) {
3498 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3499 "%s: Bad regular inode %Lu, ptr "PTR_FMT,
3500 __func__, ip->i_ino, ip);
3503 } else if (S_ISDIR(VFS_I(ip)->i_mode)) {
3505 ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS &&
3506 ip->i_df.if_format != XFS_DINODE_FMT_BTREE &&
3507 ip->i_df.if_format != XFS_DINODE_FMT_LOCAL,
3508 mp, XFS_ERRTAG_IFLUSH_4)) {
3509 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3510 "%s: Bad directory inode %Lu, ptr "PTR_FMT,
3511 __func__, ip->i_ino, ip);
3515 if (XFS_TEST_ERROR(ip->i_df.if_nextents + xfs_ifork_nextents(ip->i_afp) >
3516 ip->i_nblocks, mp, XFS_ERRTAG_IFLUSH_5)) {
3517 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3518 "%s: detected corrupt incore inode %Lu, "
3519 "total extents = %d, nblocks = %Ld, ptr "PTR_FMT,
3520 __func__, ip->i_ino,
3521 ip->i_df.if_nextents + xfs_ifork_nextents(ip->i_afp),
3525 if (XFS_TEST_ERROR(ip->i_forkoff > mp->m_sb.sb_inodesize,
3526 mp, XFS_ERRTAG_IFLUSH_6)) {
3527 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3528 "%s: bad inode %Lu, forkoff 0x%x, ptr "PTR_FMT,
3529 __func__, ip->i_ino, ip->i_forkoff, ip);
3534 * Inode item log recovery for v2 inodes are dependent on the flushiter
3535 * count for correct sequencing. We bump the flush iteration count so
3536 * we can detect flushes which postdate a log record during recovery.
3537 * This is redundant as we now log every change and hence this can't
3538 * happen but we need to still do it to ensure backwards compatibility
3539 * with old kernels that predate logging all inode changes.
3541 if (!xfs_has_v3inodes(mp))
3545 * If there are inline format data / attr forks attached to this inode,
3546 * make sure they are not corrupt.
3548 if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL &&
3549 xfs_ifork_verify_local_data(ip))
3551 if (ip->i_afp && ip->i_afp->if_format == XFS_DINODE_FMT_LOCAL &&
3552 xfs_ifork_verify_local_attr(ip))
3556 * Copy the dirty parts of the inode into the on-disk inode. We always
3557 * copy out the core of the inode, because if the inode is dirty at all
3560 xfs_inode_to_disk(ip, dip, iip->ili_item.li_lsn);
3562 /* Wrap, we never let the log put out DI_MAX_FLUSH */
3563 if (!xfs_has_v3inodes(mp)) {
3564 if (ip->i_flushiter == DI_MAX_FLUSH)
3565 ip->i_flushiter = 0;
3568 xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK);
3569 if (XFS_IFORK_Q(ip))
3570 xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK);
3573 * We've recorded everything logged in the inode, so we'd like to clear
3574 * the ili_fields bits so we don't log and flush things unnecessarily.
3575 * However, we can't stop logging all this information until the data
3576 * we've copied into the disk buffer is written to disk. If we did we
3577 * might overwrite the copy of the inode in the log with all the data
3578 * after re-logging only part of it, and in the face of a crash we
3579 * wouldn't have all the data we need to recover.
3581 * What we do is move the bits to the ili_last_fields field. When
3582 * logging the inode, these bits are moved back to the ili_fields field.
3583 * In the xfs_buf_inode_iodone() routine we clear ili_last_fields, since
3584 * we know that the information those bits represent is permanently on
3585 * disk. As long as the flush completes before the inode is logged
3586 * again, then both ili_fields and ili_last_fields will be cleared.
3590 spin_lock(&iip->ili_lock);
3591 iip->ili_last_fields = iip->ili_fields;
3592 iip->ili_fields = 0;
3593 iip->ili_fsync_fields = 0;
3594 spin_unlock(&iip->ili_lock);
3597 * Store the current LSN of the inode so that we can tell whether the
3598 * item has moved in the AIL from xfs_buf_inode_iodone().
3600 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
3601 &iip->ili_item.li_lsn);
3603 /* generate the checksum. */
3604 xfs_dinode_calc_crc(mp, dip);
3609 * Non-blocking flush of dirty inode metadata into the backing buffer.
3611 * The caller must have a reference to the inode and hold the cluster buffer
3612 * locked. The function will walk across all the inodes on the cluster buffer it
3613 * can find and lock without blocking, and flush them to the cluster buffer.
3615 * On successful flushing of at least one inode, the caller must write out the
3616 * buffer and release it. If no inodes are flushed, -EAGAIN will be returned and
3617 * the caller needs to release the buffer. On failure, the filesystem will be
3618 * shut down, the buffer will have been unlocked and released, and EFSCORRUPTED
3625 struct xfs_mount *mp = bp->b_mount;
3626 struct xfs_log_item *lip, *n;
3627 struct xfs_inode *ip;
3628 struct xfs_inode_log_item *iip;
3633 * We must use the safe variant here as on shutdown xfs_iflush_abort()
3634 * will remove itself from the list.
3636 list_for_each_entry_safe(lip, n, &bp->b_li_list, li_bio_list) {
3637 iip = (struct xfs_inode_log_item *)lip;
3638 ip = iip->ili_inode;
3641 * Quick and dirty check to avoid locks if possible.
3643 if (__xfs_iflags_test(ip, XFS_IRECLAIM | XFS_IFLUSHING))
3645 if (xfs_ipincount(ip))
3649 * The inode is still attached to the buffer, which means it is
3650 * dirty but reclaim might try to grab it. Check carefully for
3651 * that, and grab the ilock while still holding the i_flags_lock
3652 * to guarantee reclaim will not be able to reclaim this inode
3653 * once we drop the i_flags_lock.
3655 spin_lock(&ip->i_flags_lock);
3656 ASSERT(!__xfs_iflags_test(ip, XFS_ISTALE));
3657 if (__xfs_iflags_test(ip, XFS_IRECLAIM | XFS_IFLUSHING)) {
3658 spin_unlock(&ip->i_flags_lock);
3663 * ILOCK will pin the inode against reclaim and prevent
3664 * concurrent transactions modifying the inode while we are
3665 * flushing the inode. If we get the lock, set the flushing
3666 * state before we drop the i_flags_lock.
3668 if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
3669 spin_unlock(&ip->i_flags_lock);
3672 __xfs_iflags_set(ip, XFS_IFLUSHING);
3673 spin_unlock(&ip->i_flags_lock);
3676 * Abort flushing this inode if we are shut down because the
3677 * inode may not currently be in the AIL. This can occur when
3678 * log I/O failure unpins the inode without inserting into the
3679 * AIL, leaving a dirty/unpinned inode attached to the buffer
3680 * that otherwise looks like it should be flushed.
3682 if (xlog_is_shutdown(mp->m_log)) {
3683 xfs_iunpin_wait(ip);
3684 xfs_iflush_abort(ip);
3685 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3690 /* don't block waiting on a log force to unpin dirty inodes */
3691 if (xfs_ipincount(ip)) {
3692 xfs_iflags_clear(ip, XFS_IFLUSHING);
3693 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3697 if (!xfs_inode_clean(ip))
3698 error = xfs_iflush(ip, bp);
3700 xfs_iflags_clear(ip, XFS_IFLUSHING);
3701 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3709 * Shutdown first so we kill the log before we release this
3710 * buffer. If it is an INODE_ALLOC buffer and pins the tail
3711 * of the log, failing it before the _log_ is shut down can
3712 * result in the log tail being moved forward in the journal
3713 * on disk because log writes can still be taking place. Hence
3714 * unpinning the tail will allow the ICREATE intent to be
3715 * removed from the log an recovery will fail with uninitialised
3716 * inode cluster buffers.
3718 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
3719 bp->b_flags |= XBF_ASYNC;
3720 xfs_buf_ioend_fail(bp);
3727 XFS_STATS_INC(mp, xs_icluster_flushcnt);
3728 XFS_STATS_ADD(mp, xs_icluster_flushinode, clcount);
3733 /* Release an inode. */
3736 struct xfs_inode *ip)
3738 trace_xfs_irele(ip, _RET_IP_);
3743 * Ensure all commited transactions touching the inode are written to the log.
3746 xfs_log_force_inode(
3747 struct xfs_inode *ip)
3751 xfs_ilock(ip, XFS_ILOCK_SHARED);
3752 if (xfs_ipincount(ip))
3753 seq = ip->i_itemp->ili_commit_seq;
3754 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3758 return xfs_log_force_seq(ip->i_mount, seq, XFS_LOG_SYNC, NULL);
3762 * Grab the exclusive iolock for a data copy from src to dest, making sure to
3763 * abide vfs locking order (lowest pointer value goes first) and breaking the
3764 * layout leases before proceeding. The loop is needed because we cannot call
3765 * the blocking break_layout() with the iolocks held, and therefore have to
3766 * back out both locks.
3769 xfs_iolock_two_inodes_and_break_layout(
3779 /* Wait to break both inodes' layouts before we start locking. */
3780 error = break_layout(src, true);
3784 error = break_layout(dest, true);
3789 /* Lock one inode and make sure nobody got in and leased it. */
3791 error = break_layout(src, false);
3794 if (error == -EWOULDBLOCK)
3802 /* Lock the other inode and make sure nobody got in and leased it. */
3803 inode_lock_nested(dest, I_MUTEX_NONDIR2);
3804 error = break_layout(dest, false);
3808 if (error == -EWOULDBLOCK)
3817 * Lock two inodes so that userspace cannot initiate I/O via file syscalls or
3822 struct xfs_inode *ip1,
3823 struct xfs_inode *ip2)
3827 ret = xfs_iolock_two_inodes_and_break_layout(VFS_I(ip1), VFS_I(ip2));
3830 filemap_invalidate_lock_two(VFS_I(ip1)->i_mapping,
3831 VFS_I(ip2)->i_mapping);
3835 /* Unlock both inodes to allow IO and mmap activity. */
3837 xfs_iunlock2_io_mmap(
3838 struct xfs_inode *ip1,
3839 struct xfs_inode *ip2)
3841 filemap_invalidate_unlock_two(VFS_I(ip1)->i_mapping,
3842 VFS_I(ip2)->i_mapping);
3843 inode_unlock(VFS_I(ip2));
3845 inode_unlock(VFS_I(ip1));