Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * Copyright (c) 2000-2006 Silicon Graphics, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  * All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) #include <linux/iversion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include "xfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include "xfs_fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include "xfs_shared.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include "xfs_format.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include "xfs_log_format.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include "xfs_trans_resv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include "xfs_sb.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include "xfs_mount.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include "xfs_defer.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include "xfs_inode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include "xfs_dir2.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include "xfs_attr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include "xfs_trans_space.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include "xfs_trans.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include "xfs_buf_item.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include "xfs_inode_item.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include "xfs_ialloc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include "xfs_bmap.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include "xfs_bmap_util.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include "xfs_errortag.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include "xfs_error.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include "xfs_quota.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include "xfs_filestream.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include "xfs_trace.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include "xfs_icache.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include "xfs_symlink.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include "xfs_trans_priv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include "xfs_log.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include "xfs_bmap_btree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #include "xfs_reflink.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) kmem_zone_t *xfs_inode_zone;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42)  * Used in xfs_itruncate_extents().  This is the maximum number of extents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43)  * freed from a file in a single transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) #define	XFS_ITRUNC_MAX_EXTENTS	2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) STATIC int xfs_iunlink(struct xfs_trans *, struct xfs_inode *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) STATIC int xfs_iunlink_remove(struct xfs_trans *, struct xfs_inode *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51)  * helper function to extract extent size hint from inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) xfs_extlen_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) xfs_get_extsz_hint(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 	struct xfs_inode	*ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	 * No point in aligning allocations if we need to COW to actually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	 * write to them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 	if (xfs_is_always_cow_inode(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 	if ((ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE) && ip->i_d.di_extsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 		return ip->i_d.di_extsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 	if (XFS_IS_REALTIME_INODE(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 		return ip->i_mount->m_sb.sb_rextsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71)  * Helper function to extract CoW extent size hint from inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72)  * Between the extent size hint and the CoW extent size hint, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73)  * return the greater of the two.  If the value is zero (automatic),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74)  * use the default size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) xfs_extlen_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) xfs_get_cowextsz_hint(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 	struct xfs_inode	*ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	xfs_extlen_t		a, b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	a = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 	if (ip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 		a = ip->i_d.di_cowextsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 	b = xfs_get_extsz_hint(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	a = max(a, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	if (a == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 		return XFS_DEFAULT_COWEXTSZ_HINT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	return a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94)  * These two are wrapper routines around the xfs_ilock() routine used to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95)  * centralize some grungy code.  They are used in places that wish to lock the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96)  * inode solely for reading the extents.  The reason these places can't just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97)  * call xfs_ilock(ip, XFS_ILOCK_SHARED) is that the inode lock also guards to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98)  * bringing in of the extents from disk for a file in b-tree format.  If the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99)  * inode is in b-tree format, then we need to lock the inode exclusively until
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100)  * the extents are read in.  Locking it exclusively all the time would limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101)  * our parallelism unnecessarily, though.  What we do instead is check to see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102)  * if the extents have been read in yet, and only lock the inode exclusively
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103)  * if they have not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105)  * The functions return a value which should be given to the corresponding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106)  * xfs_iunlock() call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) uint
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) xfs_ilock_data_map_shared(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	struct xfs_inode	*ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	uint			lock_mode = XFS_ILOCK_SHARED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	if (ip->i_df.if_format == XFS_DINODE_FMT_BTREE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	    (ip->i_df.if_flags & XFS_IFEXTENTS) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 		lock_mode = XFS_ILOCK_EXCL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	xfs_ilock(ip, lock_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	return lock_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) uint
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) xfs_ilock_attr_map_shared(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	struct xfs_inode	*ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	uint			lock_mode = XFS_ILOCK_SHARED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	if (ip->i_afp &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	    ip->i_afp->if_format == XFS_DINODE_FMT_BTREE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	    (ip->i_afp->if_flags & XFS_IFEXTENTS) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 		lock_mode = XFS_ILOCK_EXCL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	xfs_ilock(ip, lock_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	return lock_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136)  * In addition to i_rwsem in the VFS inode, the xfs inode contains 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137)  * multi-reader locks: i_mmap_lock and the i_lock.  This routine allows
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138)  * various combinations of the locks to be obtained.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140)  * The 3 locks should always be ordered so that the IO lock is obtained first,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141)  * the mmap lock second and the ilock last in order to prevent deadlock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143)  * Basic locking order:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145)  * i_rwsem -> i_mmap_lock -> page_lock -> i_ilock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147)  * mmap_lock locking order:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149)  * i_rwsem -> page lock -> mmap_lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150)  * mmap_lock -> i_mmap_lock -> page_lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152)  * The difference in mmap_lock locking order mean that we cannot hold the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153)  * i_mmap_lock over syscall based read(2)/write(2) based IO. These IO paths can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154)  * fault in pages during copy in/out (for buffered IO) or require the mmap_lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155)  * in get_user_pages() to map the user pages into the kernel address space for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156)  * direct IO. Similarly the i_rwsem cannot be taken inside a page fault because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157)  * page faults already hold the mmap_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159)  * Hence to serialise fully against both syscall and mmap based IO, we need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160)  * take both the i_rwsem and the i_mmap_lock. These locks should *only* be both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161)  * taken in places where we need to invalidate the page cache in a race
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162)  * free manner (e.g. truncate, hole punch and other extent manipulation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163)  * functions).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) xfs_ilock(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	xfs_inode_t		*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	uint			lock_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	trace_xfs_ilock(ip, lock_flags, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	 * You can't set both SHARED and EXCL for the same lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	       (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	       (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	       (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	if (lock_flags & XFS_IOLOCK_EXCL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 		down_write_nested(&VFS_I(ip)->i_rwsem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 				  XFS_IOLOCK_DEP(lock_flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	} else if (lock_flags & XFS_IOLOCK_SHARED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 		down_read_nested(&VFS_I(ip)->i_rwsem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 				 XFS_IOLOCK_DEP(lock_flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	if (lock_flags & XFS_MMAPLOCK_EXCL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 		mrupdate_nested(&ip->i_mmaplock, XFS_MMAPLOCK_DEP(lock_flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	else if (lock_flags & XFS_MMAPLOCK_SHARED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 		mraccess_nested(&ip->i_mmaplock, XFS_MMAPLOCK_DEP(lock_flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	if (lock_flags & XFS_ILOCK_EXCL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 		mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	else if (lock_flags & XFS_ILOCK_SHARED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 		mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205)  * This is just like xfs_ilock(), except that the caller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206)  * is guaranteed not to sleep.  It returns 1 if it gets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207)  * the requested locks and 0 otherwise.  If the IO lock is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208)  * obtained but the inode lock cannot be, then the IO lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209)  * is dropped before returning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211)  * ip -- the inode being locked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212)  * lock_flags -- this parameter indicates the inode's locks to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213)  *       to be locked.  See the comment for xfs_ilock() for a list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214)  *	 of valid values.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) xfs_ilock_nowait(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	xfs_inode_t		*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	uint			lock_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	 * You can't set both SHARED and EXCL for the same lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 	       (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	       (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	       (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	if (lock_flags & XFS_IOLOCK_EXCL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 		if (!down_write_trylock(&VFS_I(ip)->i_rwsem))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	} else if (lock_flags & XFS_IOLOCK_SHARED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 		if (!down_read_trylock(&VFS_I(ip)->i_rwsem))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	if (lock_flags & XFS_MMAPLOCK_EXCL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 		if (!mrtryupdate(&ip->i_mmaplock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 			goto out_undo_iolock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	} else if (lock_flags & XFS_MMAPLOCK_SHARED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 		if (!mrtryaccess(&ip->i_mmaplock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 			goto out_undo_iolock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	if (lock_flags & XFS_ILOCK_EXCL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 		if (!mrtryupdate(&ip->i_lock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 			goto out_undo_mmaplock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	} else if (lock_flags & XFS_ILOCK_SHARED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 		if (!mrtryaccess(&ip->i_lock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 			goto out_undo_mmaplock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) out_undo_mmaplock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	if (lock_flags & XFS_MMAPLOCK_EXCL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 		mrunlock_excl(&ip->i_mmaplock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	else if (lock_flags & XFS_MMAPLOCK_SHARED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 		mrunlock_shared(&ip->i_mmaplock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) out_undo_iolock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 	if (lock_flags & XFS_IOLOCK_EXCL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 		up_write(&VFS_I(ip)->i_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	else if (lock_flags & XFS_IOLOCK_SHARED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 		up_read(&VFS_I(ip)->i_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276)  * xfs_iunlock() is used to drop the inode locks acquired with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277)  * xfs_ilock() and xfs_ilock_nowait().  The caller must pass
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278)  * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279)  * that we know which locks to drop.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281)  * ip -- the inode being unlocked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282)  * lock_flags -- this parameter indicates the inode's locks to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283)  *       to be unlocked.  See the comment for xfs_ilock() for a list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284)  *	 of valid values for this parameter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) xfs_iunlock(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	xfs_inode_t		*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	uint			lock_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	 * You can't set both SHARED and EXCL for the same lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	       (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	       (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	       (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	ASSERT(lock_flags != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	if (lock_flags & XFS_IOLOCK_EXCL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 		up_write(&VFS_I(ip)->i_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	else if (lock_flags & XFS_IOLOCK_SHARED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 		up_read(&VFS_I(ip)->i_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	if (lock_flags & XFS_MMAPLOCK_EXCL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 		mrunlock_excl(&ip->i_mmaplock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	else if (lock_flags & XFS_MMAPLOCK_SHARED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 		mrunlock_shared(&ip->i_mmaplock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	if (lock_flags & XFS_ILOCK_EXCL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 		mrunlock_excl(&ip->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	else if (lock_flags & XFS_ILOCK_SHARED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 		mrunlock_shared(&ip->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	trace_xfs_iunlock(ip, lock_flags, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325)  * give up write locks.  the i/o lock cannot be held nested
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326)  * if it is being demoted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) xfs_ilock_demote(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	xfs_inode_t		*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	uint			lock_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	ASSERT((lock_flags &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 		~(XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	if (lock_flags & XFS_ILOCK_EXCL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 		mrdemote(&ip->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	if (lock_flags & XFS_MMAPLOCK_EXCL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 		mrdemote(&ip->i_mmaplock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	if (lock_flags & XFS_IOLOCK_EXCL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 		downgrade_write(&VFS_I(ip)->i_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) #if defined(DEBUG) || defined(XFS_WARN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) xfs_isilocked(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	xfs_inode_t		*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	uint			lock_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 		if (!(lock_flags & XFS_ILOCK_SHARED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 			return !!ip->i_lock.mr_writer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 		return rwsem_is_locked(&ip->i_lock.mr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	if (lock_flags & (XFS_MMAPLOCK_EXCL|XFS_MMAPLOCK_SHARED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 		if (!(lock_flags & XFS_MMAPLOCK_SHARED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 			return !!ip->i_mmaplock.mr_writer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 		return rwsem_is_locked(&ip->i_mmaplock.mr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 		if (!(lock_flags & XFS_IOLOCK_SHARED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 			return !debug_locks ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 				lockdep_is_held_type(&VFS_I(ip)->i_rwsem, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 		return rwsem_is_locked(&VFS_I(ip)->i_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	ASSERT(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378)  * xfs_lockdep_subclass_ok() is only used in an ASSERT, so is only called when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379)  * DEBUG or XFS_WARN is set. And MAX_LOCKDEP_SUBCLASSES is then only defined
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380)  * when CONFIG_LOCKDEP is set. Hence the complex define below to avoid build
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381)  * errors and warnings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) #if (defined(DEBUG) || defined(XFS_WARN)) && defined(CONFIG_LOCKDEP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) static bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) xfs_lockdep_subclass_ok(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	int subclass)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	return subclass < MAX_LOCKDEP_SUBCLASSES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) #define xfs_lockdep_subclass_ok(subclass)	(true)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395)  * Bump the subclass so xfs_lock_inodes() acquires each lock with a different
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396)  * value. This can be called for any type of inode lock combination, including
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397)  * parent locking. Care must be taken to ensure we don't overrun the subclass
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398)  * storage fields in the class mask we build.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) static inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) xfs_lock_inumorder(int lock_mode, int subclass)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	int	class = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	ASSERT(!(lock_mode & (XFS_ILOCK_PARENT | XFS_ILOCK_RTBITMAP |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 			      XFS_ILOCK_RTSUM)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	ASSERT(xfs_lockdep_subclass_ok(subclass));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 		ASSERT(subclass <= XFS_IOLOCK_MAX_SUBCLASS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 		class += subclass << XFS_IOLOCK_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	if (lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 		ASSERT(subclass <= XFS_MMAPLOCK_MAX_SUBCLASS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 		class += subclass << XFS_MMAPLOCK_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 		ASSERT(subclass <= XFS_ILOCK_MAX_SUBCLASS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 		class += subclass << XFS_ILOCK_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	return (lock_mode & ~XFS_LOCK_SUBCLASS_MASK) | class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428)  * The following routine will lock n inodes in exclusive mode.  We assume the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429)  * caller calls us with the inodes in i_ino order.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431)  * We need to detect deadlock where an inode that we lock is in the AIL and we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432)  * start waiting for another inode that is locked by a thread in a long running
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433)  * transaction (such as truncate). This can result in deadlock since the long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434)  * running trans might need to wait for the inode we just locked in order to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435)  * push the tail and free space in the log.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437)  * xfs_lock_inodes() can only be used to lock one type of lock at a time -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438)  * the iolock, the mmaplock or the ilock, but not more than one at a time. If we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439)  * lock more than one at a time, lockdep will report false positives saying we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440)  * have violated locking orders.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) xfs_lock_inodes(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	struct xfs_inode	**ips,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	int			inodes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	uint			lock_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	int			attempts = 0, i, j, try_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	struct xfs_log_item	*lp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	 * Currently supports between 2 and 5 inodes with exclusive locking.  We
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	 * support an arbitrary depth of locking here, but absolute limits on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	 * inodes depend on the type of locking and the limits placed by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	 * lockdep annotations in xfs_lock_inumorder.  These are all checked by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	 * the asserts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	ASSERT(ips && inodes >= 2 && inodes <= 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	ASSERT(lock_mode & (XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 			    XFS_ILOCK_EXCL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	ASSERT(!(lock_mode & (XFS_IOLOCK_SHARED | XFS_MMAPLOCK_SHARED |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 			      XFS_ILOCK_SHARED)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	ASSERT(!(lock_mode & XFS_MMAPLOCK_EXCL) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 		inodes <= XFS_MMAPLOCK_MAX_SUBCLASS + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	ASSERT(!(lock_mode & XFS_ILOCK_EXCL) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 		inodes <= XFS_ILOCK_MAX_SUBCLASS + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	if (lock_mode & XFS_IOLOCK_EXCL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 		ASSERT(!(lock_mode & (XFS_MMAPLOCK_EXCL | XFS_ILOCK_EXCL)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	} else if (lock_mode & XFS_MMAPLOCK_EXCL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 		ASSERT(!(lock_mode & XFS_ILOCK_EXCL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	try_lock = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	for (; i < inodes; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 		ASSERT(ips[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 		if (i && (ips[i] == ips[i - 1]))	/* Already locked */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 		 * If try_lock is not set yet, make sure all locked inodes are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 		 * not in the AIL.  If any are, set try_lock to be used later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 		if (!try_lock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 			for (j = (i - 1); j >= 0 && !try_lock; j--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 				lp = &ips[j]->i_itemp->ili_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 				if (lp && test_bit(XFS_LI_IN_AIL, &lp->li_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 					try_lock++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 		 * If any of the previous locks we have locked is in the AIL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 		 * we must TRY to get the second and subsequent locks. If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 		 * we can't get any, we must release all we have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 		 * and try again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 		if (!try_lock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 			xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 		/* try_lock means we have an inode locked that is in the AIL. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 		ASSERT(i != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 		if (xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 		 * Unlock all previous guys and try again.  xfs_iunlock will try
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 		 * to push the tail if the inode is in the AIL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 		attempts++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 		for (j = i - 1; j >= 0; j--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 			 * Check to see if we've already unlocked this one.  Not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 			 * the first one going back, and the inode ptr is the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 			 * same.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 			if (j != (i - 1) && ips[j] == ips[j + 1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 			xfs_iunlock(ips[j], lock_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 		if ((attempts % 5) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 			delay(1); /* Don't just spin the CPU */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 		i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 		try_lock = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 		goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537)  * xfs_lock_two_inodes() can only be used to lock one type of lock at a time -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538)  * the mmaplock or the ilock, but not more than one type at a time. If we lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539)  * more than one at a time, lockdep will report false positives saying we have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540)  * violated locking orders.  The iolock must be double-locked separately since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541)  * we use i_rwsem for that.  We now support taking one lock EXCL and the other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542)  * SHARED.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) xfs_lock_two_inodes(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	struct xfs_inode	*ip0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	uint			ip0_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	struct xfs_inode	*ip1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	uint			ip1_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	struct xfs_inode	*temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	uint			mode_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	int			attempts = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	struct xfs_log_item	*lp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	ASSERT(hweight32(ip0_mode) == 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	ASSERT(hweight32(ip1_mode) == 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	ASSERT(!(ip0_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	ASSERT(!(ip1_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	ASSERT(!(ip0_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	       !(ip0_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	ASSERT(!(ip1_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	       !(ip1_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	ASSERT(!(ip1_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	       !(ip0_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	ASSERT(!(ip0_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	       !(ip1_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	ASSERT(ip0->i_ino != ip1->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	if (ip0->i_ino > ip1->i_ino) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 		temp = ip0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 		ip0 = ip1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 		ip1 = temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 		mode_temp = ip0_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 		ip0_mode = ip1_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 		ip1_mode = mode_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580)  again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	xfs_ilock(ip0, xfs_lock_inumorder(ip0_mode, 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	 * If the first lock we have locked is in the AIL, we must TRY to get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	 * the second lock. If we can't get it, we must release the first one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	 * and try again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	lp = &ip0->i_itemp->ili_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	if (lp && test_bit(XFS_LI_IN_AIL, &lp->li_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 		if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(ip1_mode, 1))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 			xfs_iunlock(ip0, ip0_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 			if ((++attempts % 5) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 				delay(1); /* Don't just spin the CPU */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 			goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 		xfs_ilock(ip1, xfs_lock_inumorder(ip1_mode, 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) STATIC uint
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) _xfs_dic2xflags(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	uint16_t		di_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	uint64_t		di_flags2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	bool			has_attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	uint			flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	if (di_flags & XFS_DIFLAG_ANY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 		if (di_flags & XFS_DIFLAG_REALTIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 			flags |= FS_XFLAG_REALTIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 		if (di_flags & XFS_DIFLAG_PREALLOC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 			flags |= FS_XFLAG_PREALLOC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 		if (di_flags & XFS_DIFLAG_IMMUTABLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 			flags |= FS_XFLAG_IMMUTABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 		if (di_flags & XFS_DIFLAG_APPEND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 			flags |= FS_XFLAG_APPEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 		if (di_flags & XFS_DIFLAG_SYNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 			flags |= FS_XFLAG_SYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 		if (di_flags & XFS_DIFLAG_NOATIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 			flags |= FS_XFLAG_NOATIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 		if (di_flags & XFS_DIFLAG_NODUMP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 			flags |= FS_XFLAG_NODUMP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 		if (di_flags & XFS_DIFLAG_RTINHERIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 			flags |= FS_XFLAG_RTINHERIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 		if (di_flags & XFS_DIFLAG_PROJINHERIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 			flags |= FS_XFLAG_PROJINHERIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 		if (di_flags & XFS_DIFLAG_NOSYMLINKS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 			flags |= FS_XFLAG_NOSYMLINKS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 		if (di_flags & XFS_DIFLAG_EXTSIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 			flags |= FS_XFLAG_EXTSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 		if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 			flags |= FS_XFLAG_EXTSZINHERIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 		if (di_flags & XFS_DIFLAG_NODEFRAG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 			flags |= FS_XFLAG_NODEFRAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 		if (di_flags & XFS_DIFLAG_FILESTREAM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 			flags |= FS_XFLAG_FILESTREAM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	if (di_flags2 & XFS_DIFLAG2_ANY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 		if (di_flags2 & XFS_DIFLAG2_DAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 			flags |= FS_XFLAG_DAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 		if (di_flags2 & XFS_DIFLAG2_COWEXTSIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 			flags |= FS_XFLAG_COWEXTSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	if (has_attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 		flags |= FS_XFLAG_HASATTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	return flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) uint
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) xfs_ip2xflags(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	struct xfs_inode	*ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	struct xfs_icdinode	*dic = &ip->i_d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	return _xfs_dic2xflags(dic->di_flags, dic->di_flags2, XFS_IFORK_Q(ip));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663)  * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664)  * is allowed, otherwise it has to be an exact match. If a CI match is found,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665)  * ci_name->name will point to a the actual name (caller must free) or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666)  * will be set to NULL if an exact match is found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) xfs_lookup(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	xfs_inode_t		*dp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	struct xfs_name		*name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	xfs_inode_t		**ipp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	struct xfs_name		*ci_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	xfs_ino_t		inum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	trace_xfs_lookup(dp, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	if (XFS_FORCED_SHUTDOWN(dp->i_mount))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 		goto out_free_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) out_free_name:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	if (ci_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 		kmem_free(ci_name->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	*ipp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) /* Propagate di_flags from a parent inode to a child inode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) xfs_inode_inherit_flags(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	const struct xfs_inode	*pip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	unsigned int		di_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	umode_t			mode = VFS_I(ip)->i_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	if (S_ISDIR(mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 		if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 			di_flags |= XFS_DIFLAG_RTINHERIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 		if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 			di_flags |= XFS_DIFLAG_EXTSZINHERIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 			ip->i_d.di_extsize = pip->i_d.di_extsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 		if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 			di_flags |= XFS_DIFLAG_PROJINHERIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	} else if (S_ISREG(mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 		if ((pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 		    xfs_sb_version_hasrealtime(&ip->i_mount->m_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 			di_flags |= XFS_DIFLAG_REALTIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 		if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 			di_flags |= XFS_DIFLAG_EXTSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 			ip->i_d.di_extsize = pip->i_d.di_extsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	    xfs_inherit_noatime)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 		di_flags |= XFS_DIFLAG_NOATIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	    xfs_inherit_nodump)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 		di_flags |= XFS_DIFLAG_NODUMP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	    xfs_inherit_sync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 		di_flags |= XFS_DIFLAG_SYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	    xfs_inherit_nosymlinks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 		di_flags |= XFS_DIFLAG_NOSYMLINKS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	    xfs_inherit_nodefrag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 		di_flags |= XFS_DIFLAG_NODEFRAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 		di_flags |= XFS_DIFLAG_FILESTREAM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	ip->i_d.di_flags |= di_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) /* Propagate di_flags2 from a parent inode to a child inode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) xfs_inode_inherit_flags2(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	const struct xfs_inode	*pip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	if (pip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 		ip->i_d.di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 		ip->i_d.di_cowextsize = pip->i_d.di_cowextsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	if (pip->i_d.di_flags2 & XFS_DIFLAG2_DAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 		ip->i_d.di_flags2 |= XFS_DIFLAG2_DAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764)  * Allocate an inode on disk and return a copy of its in-core version.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765)  * The in-core inode is locked exclusively.  Set mode, nlink, and rdev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766)  * appropriately within the inode.  The uid and gid for the inode are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767)  * set according to the contents of the given cred structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769)  * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770)  * has a free inode available, call xfs_iget() to obtain the in-core
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771)  * version of the allocated inode.  Finally, fill in the inode and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772)  * log its initial contents.  In this case, ialloc_context would be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773)  * set to NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775)  * If xfs_dialloc() does not have an available inode, it will replenish
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776)  * its supply by doing an allocation. Since we can only do one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777)  * allocation within a transaction without deadlocks, we must commit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778)  * the current transaction before returning the inode itself.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779)  * In this case, therefore, we will set ialloc_context and return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780)  * The caller should then commit the current transaction, start a new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781)  * transaction, and call xfs_ialloc() again to actually get the inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783)  * To ensure that some other process does not grab the inode that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784)  * was allocated during the first call to xfs_ialloc(), this routine
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785)  * also returns the [locked] bp pointing to the head of the freelist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786)  * as ialloc_context.  The caller should hold this buffer across
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787)  * the commit and pass it back into this routine on the second call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789)  * If we are allocating quota inodes, we do not have a parent inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790)  * to attach to or associate with (i.e. pip == NULL) because they
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791)  * are not linked into the directory structure - they are attached
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792)  * directly to the superblock - and so have no parent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) xfs_ialloc(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	xfs_trans_t	*tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	xfs_inode_t	*pip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	umode_t		mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	xfs_nlink_t	nlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	dev_t		rdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	prid_t		prid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	xfs_buf_t	**ialloc_context,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	xfs_inode_t	**ipp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 	struct xfs_mount *mp = tp->t_mountp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	xfs_ino_t	ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	xfs_inode_t	*ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	uint		flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	int		error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	struct timespec64 tv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	struct inode	*inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	 * Call the space management code to pick
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	 * the on-disk inode to be allocated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 			    ialloc_context, &ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	if (*ialloc_context || ino == NULLFSINO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 		*ipp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	ASSERT(*ialloc_context == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	 * Protect against obviously corrupt allocation btree records. Later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	 * xfs_iget checks will catch re-allocation of other active in-memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	 * and on-disk inodes. If we don't catch reallocating the parent inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	 * here we will deadlock in xfs_iget() so we have to do these checks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	 * first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	if ((pip && ino == pip->i_ino) || !xfs_verify_dir_ino(mp, ino)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 		xfs_alert(mp, "Allocated a known in-use inode 0x%llx!", ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 		return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	 * Get the in-core inode with the lock held exclusively.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	 * This is because we're setting fields here we need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	 * to prevent others from looking at until we're done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 			 XFS_ILOCK_EXCL, &ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	ASSERT(ip != NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	inode = VFS_I(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	inode->i_mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	set_nlink(inode, nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	inode->i_uid = current_fsuid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	inode->i_rdev = rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	ip->i_d.di_projid = prid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	if (pip && XFS_INHERIT_GID(pip)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 		inode->i_gid = VFS_I(pip)->i_gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 		if ((VFS_I(pip)->i_mode & S_ISGID) && S_ISDIR(mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 			inode->i_mode |= S_ISGID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 		inode->i_gid = current_fsgid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	 * If the group ID of the new file does not match the effective group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	 * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	 * (and only if the irix_sgid_inherit compatibility variable is set).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	if (irix_sgid_inherit &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	    (inode->i_mode & S_ISGID) && !in_group_p(inode->i_gid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 		inode->i_mode &= ~S_ISGID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	ip->i_d.di_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	ip->i_df.if_nextents = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	ASSERT(ip->i_d.di_nblocks == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	tv = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	inode->i_mtime = tv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	inode->i_atime = tv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	inode->i_ctime = tv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	ip->i_d.di_extsize = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	ip->i_d.di_dmevmask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	ip->i_d.di_dmstate = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	ip->i_d.di_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	if (xfs_sb_version_has_v3inode(&mp->m_sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 		inode_set_iversion(inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 		ip->i_d.di_flags2 = mp->m_ino_geo.new_diflags2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 		ip->i_d.di_cowextsize = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 		ip->i_d.di_crtime = tv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	flags = XFS_ILOG_CORE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	switch (mode & S_IFMT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	case S_IFIFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	case S_IFCHR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	case S_IFBLK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	case S_IFSOCK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 		ip->i_df.if_format = XFS_DINODE_FMT_DEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 		ip->i_df.if_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 		flags |= XFS_ILOG_DEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	case S_IFREG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	case S_IFDIR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 		if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 			xfs_inode_inherit_flags(ip, pip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 		if (pip && (pip->i_d.di_flags2 & XFS_DIFLAG2_ANY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 			xfs_inode_inherit_flags2(ip, pip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 		/* FALLTHROUGH */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	case S_IFLNK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 		ip->i_df.if_format = XFS_DINODE_FMT_EXTENTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 		ip->i_df.if_flags = XFS_IFEXTENTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 		ip->i_df.if_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		ip->i_df.if_u1.if_root = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 		ASSERT(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	 * Log the new values stuffed into the inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	xfs_trans_log_inode(tp, ip, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	/* now that we have an i_mode we can setup the inode structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	xfs_setup_inode(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	*ipp = ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935)  * Allocates a new inode from disk and return a pointer to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936)  * incore copy. This routine will internally commit the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937)  * transaction and allocate a new one if the Space Manager needed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938)  * to do an allocation to replenish the inode free-list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940)  * This routine is designed to be called from xfs_create and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941)  * xfs_create_dir.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) xfs_dir_ialloc(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	xfs_trans_t	**tpp,		/* input: current transaction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 					   output: may be a new transaction. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	xfs_inode_t	*dp,		/* directory within whose allocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 					   the inode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	umode_t		mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	xfs_nlink_t	nlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	dev_t		rdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	prid_t		prid,		/* project id */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	xfs_inode_t	**ipp)		/* pointer to inode; it will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 					   locked. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	xfs_trans_t	*tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	xfs_inode_t	*ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	xfs_buf_t	*ialloc_context = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	int		code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	void		*dqinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	uint		tflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	tp = *tpp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	 * xfs_ialloc will return a pointer to an incore inode if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	 * the Space Manager has an available inode on the free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	 * list. Otherwise, it will do an allocation and replenish
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 	 * the freelist.  Since we can only do one allocation per
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	 * transaction without deadlocks, we will need to commit the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	 * current transaction and start a new one.  We will then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 	 * need to call xfs_ialloc again to get the inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 	 * If xfs_ialloc did an allocation to replenish the freelist,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	 * it returns the bp containing the head of the freelist as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	 * ialloc_context. We will hold a lock on it across the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	 * transaction commit so that no other process can steal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	 * the inode(s) that we've just allocated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid, &ialloc_context,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 			&ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	 * Return an error if we were unable to allocate a new inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	 * This should only happen if we run out of space on disk or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	 * encounter a disk error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	if (code) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 		*ipp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 		return code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	if (!ialloc_context && !ip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 		*ipp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 		return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	 * If the AGI buffer is non-NULL, then we were unable to get an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	 * inode in one operation.  We need to commit the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	 * transaction and call xfs_ialloc() again.  It is guaranteed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	 * to succeed the second time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	if (ialloc_context) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 		 * Normally, xfs_trans_commit releases all the locks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 		 * We call bhold to hang on to the ialloc_context across
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 		 * the commit.  Holding this buffer prevents any other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 		 * processes from doing any allocations in this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 		 * allocation group.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 		xfs_trans_bhold(tp, ialloc_context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 		 * We want the quota changes to be associated with the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 		 * transaction, NOT this one. So, detach the dqinfo from this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 		 * and attach it to the next transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 		dqinfo = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 		tflags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 		if (tp->t_dqinfo) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 			dqinfo = (void *)tp->t_dqinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 			tp->t_dqinfo = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 			tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 			tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 		code = xfs_trans_roll(&tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 		 * Re-attach the quota info that we detached from prev trx.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 		if (dqinfo) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 			tp->t_dqinfo = dqinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 			tp->t_flags |= tflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 		if (code) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 			xfs_buf_relse(ialloc_context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 			*tpp = tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 			*ipp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 			return code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 		xfs_trans_bjoin(tp, ialloc_context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 		 * Call ialloc again. Since we've locked out all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 		 * other allocations in this allocation group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 		 * this call should always succeed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 		code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 				  &ialloc_context, &ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 		 * If we get an error at this point, return to the caller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 		 * so that the current transaction can be aborted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 		if (code) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 			*tpp = tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 			*ipp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 			return code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 		ASSERT(!ialloc_context && ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 	*ipp = ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	*tpp = tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075)  * Decrement the link count on an inode & log the change.  If this causes the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076)  * link count to go to zero, move the inode to AGI unlinked list so that it can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077)  * be freed when the last active reference goes away via xfs_inactive().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) static int			/* error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) xfs_droplink(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	xfs_trans_t *tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	xfs_inode_t *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	drop_nlink(VFS_I(ip));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	if (VFS_I(ip)->i_nlink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	return xfs_iunlink(tp, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096)  * Increment the link count on an inode & log the change.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) xfs_bumplink(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	xfs_trans_t *tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	xfs_inode_t *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 	xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	inc_nlink(VFS_I(ip));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) xfs_create(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	xfs_inode_t		*dp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	struct xfs_name		*name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	umode_t			mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	dev_t			rdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	xfs_inode_t		**ipp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	int			is_dir = S_ISDIR(mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	struct xfs_mount	*mp = dp->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	struct xfs_inode	*ip = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	struct xfs_trans	*tp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	bool                    unlock_dp_on_error = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	prid_t			prid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	struct xfs_dquot	*udqp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	struct xfs_dquot	*gdqp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	struct xfs_dquot	*pdqp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	struct xfs_trans_res	*tres;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	uint			resblks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	trace_xfs_create(dp, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	if (XFS_FORCED_SHUTDOWN(mp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	prid = xfs_get_initial_prid(dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	 * Make sure that we have allocated dquot(s) on disk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 					XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 					&udqp, &gdqp, &pdqp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	if (is_dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 		resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 		tres = &M_RES(mp)->tr_mkdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 		resblks = XFS_CREATE_SPACE_RES(mp, name->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 		tres = &M_RES(mp)->tr_create;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	 * Initially assume that the file does not exist and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	 * reserve the resources for that case.  If that is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	 * the case we'll drop the one we have and get a more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	 * appropriate transaction later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	error = xfs_trans_alloc(mp, tres, resblks, 0, 0, &tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	if (error == -ENOSPC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 		/* flush outstanding delalloc blocks and retry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 		xfs_flush_inodes(mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 		error = xfs_trans_alloc(mp, tres, resblks, 0, 0, &tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 		goto out_release_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 	xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	unlock_dp_on_error = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	 * Reserve disk quota and the inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 						pdqp, resblks, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 		goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 	 * A newly created regular or special file just has one directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	 * entry pointing to them, but a directory also the "." entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 	 * pointing to itself.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev, prid, &ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 		goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	 * Now we join the directory inode to the transaction.  We do not do it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 	 * earlier because xfs_dir_ialloc might commit the previous transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 	 * (and release all the locks).  An error from here on will result in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	 * the transaction cancel unlocking dp so don't do it explicitly in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	 * error path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 	xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	unlock_dp_on_error = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 	error = xfs_dir_createname(tp, dp, name, ip->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 					resblks - XFS_IALLOC_SPACE_RES(mp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 		ASSERT(error != -ENOSPC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 		goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 	if (is_dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 		error = xfs_dir_init(tp, ip, dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 			goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 		xfs_bumplink(tp, dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	 * If this is a synchronous mount, make sure that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	 * create transaction goes to disk before returning to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 	 * the user.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 		xfs_trans_set_sync(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	 * Attach the dquot(s) to the inodes and modify them incore.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	 * These ids of the inode couldn't have changed since the new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 	 * inode has been locked ever since it was created.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	error = xfs_trans_commit(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 		goto out_release_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 	xfs_qm_dqrele(udqp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 	xfs_qm_dqrele(gdqp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	xfs_qm_dqrele(pdqp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	*ipp = ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242)  out_trans_cancel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 	xfs_trans_cancel(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244)  out_release_inode:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	 * Wait until after the current transaction is aborted to finish the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 	 * setup of the inode and release the inode.  This prevents recursive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 	 * transactions and deadlocks from xfs_inactive.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	if (ip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 		xfs_finish_inode_setup(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 		xfs_irele(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	xfs_qm_dqrele(udqp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	xfs_qm_dqrele(gdqp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	xfs_qm_dqrele(pdqp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 	if (unlock_dp_on_error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 		xfs_iunlock(dp, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) xfs_create_tmpfile(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 	struct xfs_inode	*dp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 	umode_t			mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	struct xfs_inode	**ipp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 	struct xfs_mount	*mp = dp->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 	struct xfs_inode	*ip = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 	struct xfs_trans	*tp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	prid_t                  prid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 	struct xfs_dquot	*udqp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 	struct xfs_dquot	*gdqp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 	struct xfs_dquot	*pdqp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 	struct xfs_trans_res	*tres;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 	uint			resblks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 	if (XFS_FORCED_SHUTDOWN(mp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 	prid = xfs_get_initial_prid(dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 	 * Make sure that we have allocated dquot(s) on disk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 	error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 				XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 				&udqp, &gdqp, &pdqp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 	resblks = XFS_IALLOC_SPACE_RES(mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 	tres = &M_RES(mp)->tr_create_tmpfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 	error = xfs_trans_alloc(mp, tres, resblks, 0, 0, &tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 		goto out_release_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 						pdqp, resblks, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 		goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	error = xfs_dir_ialloc(&tp, dp, mode, 0, 0, prid, &ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 		goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 	if (mp->m_flags & XFS_MOUNT_WSYNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 		xfs_trans_set_sync(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	 * Attach the dquot(s) to the inodes and modify them incore.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 	 * These ids of the inode couldn't have changed since the new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	 * inode has been locked ever since it was created.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 	xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 	error = xfs_iunlink(tp, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 		goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 	error = xfs_trans_commit(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 		goto out_release_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 	xfs_qm_dqrele(udqp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 	xfs_qm_dqrele(gdqp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 	xfs_qm_dqrele(pdqp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 	*ipp = ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336)  out_trans_cancel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 	xfs_trans_cancel(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338)  out_release_inode:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 	 * Wait until after the current transaction is aborted to finish the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 	 * setup of the inode and release the inode.  This prevents recursive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 	 * transactions and deadlocks from xfs_inactive.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 	if (ip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 		xfs_finish_inode_setup(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 		xfs_irele(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 	xfs_qm_dqrele(udqp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 	xfs_qm_dqrele(gdqp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 	xfs_qm_dqrele(pdqp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) xfs_link(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 	xfs_inode_t		*tdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	xfs_inode_t		*sip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 	struct xfs_name		*target_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	xfs_mount_t		*mp = tdp->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	xfs_trans_t		*tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 	int			resblks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 	trace_xfs_link(tdp, target_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 	ASSERT(!S_ISDIR(VFS_I(sip)->i_mode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 	if (XFS_FORCED_SHUTDOWN(mp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	error = xfs_qm_dqattach(sip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 		goto std_return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 	error = xfs_qm_dqattach(tdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 		goto std_return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 	resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_link, resblks, 0, 0, &tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 	if (error == -ENOSPC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 		resblks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_link, 0, 0, 0, &tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 		goto std_return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 	xfs_lock_two_inodes(sip, XFS_ILOCK_EXCL, tdp, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 	xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 	xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 	 * If we are using project inheritance, we only allow hard link
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 	 * creation in our tree when the project IDs are the same; else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 	 * the tree quota mechanism could be circumvented.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 	if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 		     tdp->i_d.di_projid != sip->i_d.di_projid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 		error = -EXDEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 		goto error_return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 	if (!resblks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 		error = xfs_dir_canenter(tp, tdp, target_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 			goto error_return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 	 * Handle initial link state of O_TMPFILE inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 	if (VFS_I(sip)->i_nlink == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 		error = xfs_iunlink_remove(tp, sip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 			goto error_return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 	error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 				   resblks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 		goto error_return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 	xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 	xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 	xfs_bumplink(tp, sip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 	 * If this is a synchronous mount, make sure that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 	 * link transaction goes to disk before returning to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 	 * the user.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 	if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 		xfs_trans_set_sync(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 	return xfs_trans_commit(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441)  error_return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 	xfs_trans_cancel(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443)  std_return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) /* Clear the reflink flag and the cowblocks tag if possible. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) xfs_itruncate_clear_reflink_flags(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 	struct xfs_inode	*ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 	struct xfs_ifork	*dfork;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 	struct xfs_ifork	*cfork;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 	if (!xfs_is_reflink_inode(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 	dfork = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	cfork = XFS_IFORK_PTR(ip, XFS_COW_FORK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	if (dfork->if_bytes == 0 && cfork->if_bytes == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 		ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	if (cfork->if_bytes == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 		xfs_inode_clear_cowblocks_tag(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466)  * Free up the underlying blocks past new_size.  The new size must be smaller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467)  * than the current size.  This routine can be used both for the attribute and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468)  * data fork, and does not modify the inode size, which is left to the caller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470)  * The transaction passed to this routine must have made a permanent log
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471)  * reservation of at least XFS_ITRUNCATE_LOG_RES.  This routine may commit the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472)  * given transaction and start new ones, so make sure everything involved in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473)  * the transaction is tidy before calling here.  Some transaction will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474)  * returned to the caller to be committed.  The incoming transaction must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475)  * already include the inode, and both inode locks must be held exclusively.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476)  * The inode must also be "held" within the transaction.  On return the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477)  * will be "held" within the returned transaction.  This routine does NOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478)  * require any disk space to be reserved for it within the transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480)  * If we get an error, we must return with the inode locked and linked into the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481)  * current transaction. This keeps things simple for the higher level code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482)  * because it always knows that the inode is locked and held in the transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483)  * that returns to it whether errors occur or not.  We don't mark the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484)  * dirty on error so that transactions can be easily aborted if possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) xfs_itruncate_extents_flags(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 	struct xfs_trans	**tpp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 	int			whichfork,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 	xfs_fsize_t		new_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 	int			flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 	struct xfs_mount	*mp = ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 	struct xfs_trans	*tp = *tpp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 	xfs_fileoff_t		first_unmap_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 	xfs_filblks_t		unmap_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 	int			error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 	ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 	       xfs_isilocked(ip, XFS_IOLOCK_EXCL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 	ASSERT(new_size <= XFS_ISIZE(ip));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 	ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 	ASSERT(ip->i_itemp != NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 	ASSERT(ip->i_itemp->ili_lock_flags == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 	ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 	trace_xfs_itruncate_extents_start(ip, new_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 	flags |= xfs_bmapi_aflag(whichfork);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	 * Since it is possible for space to become allocated beyond
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 	 * the end of the file (in a crash where the space is allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 	 * but the inode size is not yet updated), simply remove any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 	 * blocks which show up between the new EOF and the maximum
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 	 * possible file size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 	 * We have to free all the blocks to the bmbt maximum offset, even if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 	 * the page cache can't scale that far.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 	first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 	if (first_unmap_block >= XFS_MAX_FILEOFF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 		WARN_ON_ONCE(first_unmap_block > XFS_MAX_FILEOFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 	unmap_len = XFS_MAX_FILEOFF - first_unmap_block + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 	while (unmap_len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 		ASSERT(tp->t_firstblock == NULLFSBLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 		error = __xfs_bunmapi(tp, ip, first_unmap_block, &unmap_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 				flags, XFS_ITRUNC_MAX_EXTENTS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 		/* free the just unmapped extents */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 		error = xfs_defer_finish(&tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 	if (whichfork == XFS_DATA_FORK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 		/* Remove all pending CoW reservations. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 		error = xfs_reflink_cancel_cow_blocks(ip, &tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 				first_unmap_block, XFS_MAX_FILEOFF, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 		xfs_itruncate_clear_reflink_flags(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 	 * Always re-log the inode so that our permanent transaction can keep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 	 * on rolling it forward in the log.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 	trace_xfs_itruncate_extents_end(ip, new_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 	*tpp = tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) xfs_release(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 	xfs_inode_t	*ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 	xfs_mount_t	*mp = ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 	int		error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 	if (!S_ISREG(VFS_I(ip)->i_mode) || (VFS_I(ip)->i_mode == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 	/* If this is a read-only mount, don't do this (would generate I/O) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 	if (mp->m_flags & XFS_MOUNT_RDONLY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 	if (!XFS_FORCED_SHUTDOWN(mp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 		int truncated;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 		 * If we previously truncated this file and removed old data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 		 * in the process, we want to initiate "early" writeout on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 		 * the last close.  This is an attempt to combat the notorious
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 		 * NULL files problem which is particularly noticeable from a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 		 * truncate down, buffered (re-)write (delalloc), followed by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 		 * a crash.  What we are effectively doing here is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 		 * significantly reducing the time window where we'd otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 		 * be exposed to that problem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 		truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 		if (truncated) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 			xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 			if (ip->i_delayed_blks > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 				error = filemap_flush(VFS_I(ip)->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 				if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 					return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 	if (VFS_I(ip)->i_nlink == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 	if (xfs_can_free_eofblocks(ip, false)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 		 * Check if the inode is being opened, written and closed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 		 * frequently and we have delayed allocation blocks outstanding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 		 * (e.g. streaming writes from the NFS server), truncating the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 		 * blocks past EOF will cause fragmentation to occur.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 		 * In this case don't do the truncation, but we have to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 		 * careful how we detect this case. Blocks beyond EOF show up as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 		 * i_delayed_blks even when the inode is clean, so we need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 		 * truncate them away first before checking for a dirty release.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 		 * Hence on the first dirty close we will still remove the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 		 * speculative allocation, but after that we will leave it in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 		 * place.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 		if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 		 * If we can't get the iolock just skip truncating the blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 		 * past EOF because we could deadlock with the mmap_lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 		 * otherwise. We'll get another chance to drop them once the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 		 * last reference to the inode is dropped, so we'll never leak
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 		 * blocks permanently.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 		if (xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 			error = xfs_free_eofblocks(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 			xfs_iunlock(ip, XFS_IOLOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 				return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 		/* delalloc blocks after truncation means it really is dirty */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 		if (ip->i_delayed_blks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 			xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647)  * xfs_inactive_truncate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649)  * Called to perform a truncate when an inode becomes unlinked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) xfs_inactive_truncate(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 	struct xfs_inode *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 	struct xfs_mount	*mp = ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 	struct xfs_trans	*tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 		ASSERT(XFS_FORCED_SHUTDOWN(mp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 	xfs_ilock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 	xfs_trans_ijoin(tp, ip, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 	 * Log the inode size first to prevent stale data exposure in the event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 	 * of a system crash before the truncate completes. See the related
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 	 * comment in xfs_vn_setattr_size() for details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 	ip->i_d.di_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 	error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 		goto error_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 	ASSERT(ip->i_df.if_nextents == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 	error = xfs_trans_commit(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 		goto error_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) error_trans_cancel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 	xfs_trans_cancel(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) error_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696)  * xfs_inactive_ifree()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698)  * Perform the inode free when an inode is unlinked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) xfs_inactive_ifree(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 	struct xfs_inode *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 	struct xfs_mount	*mp = ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 	struct xfs_trans	*tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 	 * We try to use a per-AG reservation for any block needed by the finobt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 	 * tree, but as the finobt feature predates the per-AG reservation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 	 * support a degraded file system might not have enough space for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 	 * reservation at mount time.  In that case try to dip into the reserved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 	 * pool and pray.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 	 * Send a warning if the reservation does happen to fail, as the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 	 * now remains allocated and sits on the unlinked list until the fs is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 	 * repaired.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 	if (unlikely(mp->m_finobt_nores)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ifree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 				XFS_IFREE_SPACE_RES(mp), 0, XFS_TRANS_RESERVE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 				&tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ifree, 0, 0, 0, &tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 		if (error == -ENOSPC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 			xfs_warn_ratelimited(mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 			"Failed to remove inode(s) from unlinked list. "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 			"Please free space, unmount and run xfs_repair.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 			ASSERT(XFS_FORCED_SHUTDOWN(mp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 	 * We do not hold the inode locked across the entire rolling transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 	 * here. We only need to hold it for the first transaction that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 	 * xfs_ifree() builds, which may mark the inode XFS_ISTALE if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 	 * underlying cluster buffer is freed. Relogging an XFS_ISTALE inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 	 * here breaks the relationship between cluster buffer invalidation and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 	 * stale inode invalidation on cluster buffer item journal commit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 	 * completion, and can result in leaving dirty stale inodes hanging
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 	 * around in memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 	 * We have no need for serialising this inode operation against other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 	 * operations - we freed the inode and hence reallocation is required
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 	 * and that will serialise on reallocating the space the deferops need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 	 * to free. Hence we can unlock the inode on the first commit of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 	 * the transaction rather than roll it right through the deferops. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 	 * avoids relogging the XFS_ISTALE inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 	 * We check that xfs_ifree() hasn't grown an internal transaction roll
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 	 * by asserting that the inode is still locked when it returns.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 	xfs_ilock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 	error = xfs_ifree(tp, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 		 * If we fail to free the inode, shut down.  The cancel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 		 * might do that, we need to make sure.  Otherwise the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 		 * inode might be lost for a long time or forever.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 		if (!XFS_FORCED_SHUTDOWN(mp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 			xfs_notice(mp, "%s: xfs_ifree returned error %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 				__func__, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 			xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 		xfs_trans_cancel(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 	 * Credit the quota account(s). The inode is gone.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 	xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 	 * Just ignore errors at this point.  There is nothing we can do except
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 	 * to try to keep going. Make sure it's not a silent error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 	error = xfs_trans_commit(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 		xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 			__func__, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795)  * xfs_inactive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797)  * This is called when the vnode reference count for the vnode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798)  * goes to zero.  If the file has been unlinked, then it must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799)  * now be truncated.  Also, we clear all of the read-ahead state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800)  * kept for the inode here since the file is now closed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) xfs_inactive(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 	xfs_inode_t	*ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 	struct xfs_mount	*mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 	int			truncate = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 	 * If the inode is already free, then there can be nothing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 	 * to clean up here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 	if (VFS_I(ip)->i_mode == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 		ASSERT(ip->i_df.if_broot_bytes == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 	mp = ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 	ASSERT(!xfs_iflags_test(ip, XFS_IRECOVERY));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 	/* If this is a read-only mount, don't do this (would generate I/O) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 	if (mp->m_flags & XFS_MOUNT_RDONLY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 	/* Try to clean out the cow blocks if there are any. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 	if (xfs_inode_has_cow_data(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 		xfs_reflink_cancel_cow_range(ip, 0, NULLFILEOFF, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 	if (VFS_I(ip)->i_nlink != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 		 * force is true because we are evicting an inode from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 		 * cache. Post-eof blocks must be freed, lest we end up with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 		 * broken free space accounting.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 		 * Note: don't bother with iolock here since lockdep complains
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 		 * about acquiring it in reclaim context. We have the only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 		 * reference to the inode at this point anyways.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 		if (xfs_can_free_eofblocks(ip, true))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 			xfs_free_eofblocks(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 	if (S_ISREG(VFS_I(ip)->i_mode) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 	    (ip->i_d.di_size != 0 || XFS_ISIZE(ip) != 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 	     ip->i_df.if_nextents > 0 || ip->i_delayed_blks > 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 		truncate = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 	error = xfs_qm_dqattach(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 	if (S_ISLNK(VFS_I(ip)->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 		error = xfs_inactive_symlink(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 	else if (truncate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 		error = xfs_inactive_truncate(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 	 * If there are attributes associated with the file then blow them away
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 	 * now.  The code calls a routine that recursively deconstructs the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 	 * attribute fork. If also blows away the in-core attribute fork.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 	if (XFS_IFORK_Q(ip)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 		error = xfs_attr_inactive(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 	ASSERT(!ip->i_afp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 	ASSERT(ip->i_d.di_forkoff == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 	 * Free the inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 	error = xfs_inactive_ifree(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 	 * Release the dquots held by inode, if any.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 	xfs_qm_dqdetach(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890)  * In-Core Unlinked List Lookups
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891)  * =============================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893)  * Every inode is supposed to be reachable from some other piece of metadata
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894)  * with the exception of the root directory.  Inodes with a connection to a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895)  * file descriptor but not linked from anywhere in the on-disk directory tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896)  * are collectively known as unlinked inodes, though the filesystem itself
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897)  * maintains links to these inodes so that on-disk metadata are consistent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899)  * XFS implements a per-AG on-disk hash table of unlinked inodes.  The AGI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900)  * header contains a number of buckets that point to an inode, and each inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901)  * record has a pointer to the next inode in the hash chain.  This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902)  * singly-linked list causes scaling problems in the iunlink remove function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903)  * because we must walk that list to find the inode that points to the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904)  * being removed from the unlinked hash bucket list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906)  * What if we modelled the unlinked list as a collection of records capturing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907)  * "X.next_unlinked = Y" relations?  If we indexed those records on Y, we'd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908)  * have a fast way to look up unlinked list predecessors, which avoids the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909)  * slow list walk.  That's exactly what we do here (in-core) with a per-AG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910)  * rhashtable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912)  * Because this is a backref cache, we ignore operational failures since the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913)  * iunlink code can fall back to the slow bucket walk.  The only errors that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914)  * should bubble out are for obviously incorrect situations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916)  * All users of the backref cache MUST hold the AGI buffer lock to serialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917)  * access or have otherwise provided for concurrency control.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) /* Capture a "X.next_unlinked = Y" relationship. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) struct xfs_iunlink {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 	struct rhash_head	iu_rhash_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 	xfs_agino_t		iu_agino;		/* X */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 	xfs_agino_t		iu_next_unlinked;	/* Y */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) /* Unlinked list predecessor lookup hashtable construction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) xfs_iunlink_obj_cmpfn(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 	struct rhashtable_compare_arg	*arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 	const void			*obj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 	const xfs_agino_t		*key = arg->key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 	const struct xfs_iunlink	*iu = obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 	if (iu->iu_next_unlinked != *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) static const struct rhashtable_params xfs_iunlink_hash_params = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 	.min_size		= XFS_AGI_UNLINKED_BUCKETS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 	.key_len		= sizeof(xfs_agino_t),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 	.key_offset		= offsetof(struct xfs_iunlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 					   iu_next_unlinked),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 	.head_offset		= offsetof(struct xfs_iunlink, iu_rhash_head),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 	.automatic_shrinking	= true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 	.obj_cmpfn		= xfs_iunlink_obj_cmpfn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952)  * Return X, where X.next_unlinked == @agino.  Returns NULLAGINO if no such
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953)  * relation is found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) static xfs_agino_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) xfs_iunlink_lookup_backref(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 	struct xfs_perag	*pag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 	xfs_agino_t		agino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 	struct xfs_iunlink	*iu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 	iu = rhashtable_lookup_fast(&pag->pagi_unlinked_hash, &agino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 			xfs_iunlink_hash_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 	return iu ? iu->iu_agino : NULLAGINO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968)  * Take ownership of an iunlink cache entry and insert it into the hash table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969)  * If successful, the entry will be owned by the cache; if not, it is freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970)  * Either way, the caller does not own @iu after this call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) xfs_iunlink_insert_backref(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 	struct xfs_perag	*pag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 	struct xfs_iunlink	*iu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 	error = rhashtable_insert_fast(&pag->pagi_unlinked_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 			&iu->iu_rhash_head, xfs_iunlink_hash_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 	 * Fail loudly if there already was an entry because that's a sign of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 	 * corruption of in-memory data.  Also fail loudly if we see an error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 	 * code we didn't anticipate from the rhashtable code.  Currently we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 	 * only anticipate ENOMEM.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 		WARN(error != -ENOMEM, "iunlink cache insert error %d", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 		kmem_free(iu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 	 * Absorb any runtime errors that aren't a result of corruption because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 	 * this is a cache and we can always fall back to bucket list scanning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 	if (error != 0 && error != -EEXIST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 		error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) /* Remember that @prev_agino.next_unlinked = @this_agino. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) xfs_iunlink_add_backref(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 	struct xfs_perag	*pag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 	xfs_agino_t		prev_agino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 	xfs_agino_t		this_agino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 	struct xfs_iunlink	*iu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 	if (XFS_TEST_ERROR(false, pag->pag_mount, XFS_ERRTAG_IUNLINK_FALLBACK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 	iu = kmem_zalloc(sizeof(*iu), KM_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 	iu->iu_agino = prev_agino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 	iu->iu_next_unlinked = this_agino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 	return xfs_iunlink_insert_backref(pag, iu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020)  * Replace X.next_unlinked = @agino with X.next_unlinked = @next_unlinked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021)  * If @next_unlinked is NULLAGINO, we drop the backref and exit.  If there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022)  * wasn't any such entry then we don't bother.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) xfs_iunlink_change_backref(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 	struct xfs_perag	*pag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 	xfs_agino_t		agino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 	xfs_agino_t		next_unlinked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 	struct xfs_iunlink	*iu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 	/* Look up the old entry; if there wasn't one then exit. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 	iu = rhashtable_lookup_fast(&pag->pagi_unlinked_hash, &agino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 			xfs_iunlink_hash_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 	if (!iu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 	 * Remove the entry.  This shouldn't ever return an error, but if we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 	 * couldn't remove the old entry we don't want to add it again to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 	 * hash table, and if the entry disappeared on us then someone's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 	 * violated the locking rules and we need to fail loudly.  Either way
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 	 * we cannot remove the inode because internal state is or would have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 	 * been corrupt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 	error = rhashtable_remove_fast(&pag->pagi_unlinked_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 			&iu->iu_rhash_head, xfs_iunlink_hash_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 	/* If there is no new next entry just free our item and return. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 	if (next_unlinked == NULLAGINO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 		kmem_free(iu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 	/* Update the entry and re-add it to the hash table. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 	iu->iu_next_unlinked = next_unlinked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 	return xfs_iunlink_insert_backref(pag, iu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) /* Set up the in-core predecessor structures. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) xfs_iunlink_init(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 	struct xfs_perag	*pag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 	return rhashtable_init(&pag->pagi_unlinked_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 			&xfs_iunlink_hash_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) /* Free the in-core predecessor structures. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) xfs_iunlink_free_item(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 	void			*ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 	void			*arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 	struct xfs_iunlink	*iu = ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 	bool			*freed_anything = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 	*freed_anything = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) 	kmem_free(iu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) xfs_iunlink_destroy(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) 	struct xfs_perag	*pag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 	bool			freed_anything = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 	rhashtable_free_and_destroy(&pag->pagi_unlinked_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 			xfs_iunlink_free_item, &freed_anything);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 	ASSERT(freed_anything == false || XFS_FORCED_SHUTDOWN(pag->pag_mount));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098)  * Point the AGI unlinked bucket at an inode and log the results.  The caller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099)  * is responsible for validating the old value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) xfs_iunlink_update_bucket(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 	struct xfs_trans	*tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 	xfs_agnumber_t		agno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 	struct xfs_buf		*agibp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 	unsigned int		bucket_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 	xfs_agino_t		new_agino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 	struct xfs_agi		*agi = agibp->b_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 	xfs_agino_t		old_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 	int			offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 	ASSERT(xfs_verify_agino_or_null(tp->t_mountp, agno, new_agino));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 	old_value = be32_to_cpu(agi->agi_unlinked[bucket_index]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 	trace_xfs_iunlink_update_bucket(tp->t_mountp, agno, bucket_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 			old_value, new_agino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 	 * We should never find the head of the list already set to the value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 	 * passed in because either we're adding or removing ourselves from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 	 * head of the list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 	if (old_value == new_agino) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 		xfs_buf_mark_corrupt(agibp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 		return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 	agi->agi_unlinked[bucket_index] = cpu_to_be32(new_agino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 	offset = offsetof(struct xfs_agi, agi_unlinked) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 			(sizeof(xfs_agino_t) * bucket_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 	xfs_trans_log_buf(tp, agibp, offset, offset + sizeof(xfs_agino_t) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) /* Set an on-disk inode's next_unlinked pointer. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) STATIC void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) xfs_iunlink_update_dinode(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 	struct xfs_trans	*tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 	xfs_agnumber_t		agno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 	xfs_agino_t		agino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) 	struct xfs_buf		*ibp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 	struct xfs_dinode	*dip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) 	struct xfs_imap		*imap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 	xfs_agino_t		next_agino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) 	struct xfs_mount	*mp = tp->t_mountp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 	int			offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) 	ASSERT(xfs_verify_agino_or_null(mp, agno, next_agino));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) 	trace_xfs_iunlink_update_dinode(mp, agno, agino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) 			be32_to_cpu(dip->di_next_unlinked), next_agino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) 	dip->di_next_unlinked = cpu_to_be32(next_agino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 	offset = imap->im_boffset +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) 			offsetof(struct xfs_dinode, di_next_unlinked);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 	/* need to recalc the inode CRC if appropriate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) 	xfs_dinode_calc_crc(mp, dip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) 	xfs_trans_inode_buf(tp, ibp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 	xfs_trans_log_buf(tp, ibp, offset, offset + sizeof(xfs_agino_t) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) /* Set an in-core inode's unlinked pointer and return the old value. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) xfs_iunlink_update_inode(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 	struct xfs_trans	*tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) 	xfs_agnumber_t		agno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) 	xfs_agino_t		next_agino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) 	xfs_agino_t		*old_next_agino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) 	struct xfs_mount	*mp = tp->t_mountp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) 	struct xfs_dinode	*dip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) 	struct xfs_buf		*ibp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) 	xfs_agino_t		old_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) 	ASSERT(xfs_verify_agino_or_null(mp, agno, next_agino));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) 	error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) 	/* Make sure the old pointer isn't garbage. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) 	old_value = be32_to_cpu(dip->di_next_unlinked);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) 	if (!xfs_verify_agino_or_null(mp, agno, old_value)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) 		xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, dip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 				sizeof(*dip), __this_address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) 		error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) 	 * Since we're updating a linked list, we should never find that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 	 * current pointer is the same as the new value, unless we're
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) 	 * terminating the list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 	*old_next_agino = old_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) 	if (old_value == next_agino) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) 		if (next_agino != NULLAGINO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) 			xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) 					dip, sizeof(*dip), __this_address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) 	/* Ok, update the new pointer. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) 	xfs_iunlink_update_dinode(tp, agno, XFS_INO_TO_AGINO(mp, ip->i_ino),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) 			ibp, dip, &ip->i_imap, next_agino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) 	xfs_trans_brelse(tp, ibp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220)  * This is called when the inode's link count has gone to 0 or we are creating
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221)  * a tmpfile via O_TMPFILE.  The inode @ip must have nlink == 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223)  * We place the on-disk inode on a list in the AGI.  It will be pulled from this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224)  * list when the inode is freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) xfs_iunlink(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) 	struct xfs_trans	*tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) 	struct xfs_inode	*ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) 	struct xfs_mount	*mp = tp->t_mountp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 	struct xfs_agi		*agi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) 	struct xfs_buf		*agibp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) 	xfs_agino_t		next_agino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) 	xfs_agnumber_t		agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) 	xfs_agino_t		agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) 	short			bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) 	ASSERT(VFS_I(ip)->i_nlink == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) 	ASSERT(VFS_I(ip)->i_mode != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) 	trace_xfs_iunlink(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) 	/* Get the agi buffer first.  It ensures lock ordering on the list. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) 	error = xfs_read_agi(mp, tp, agno, &agibp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) 	agi = agibp->b_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) 	 * Get the index into the agi hash table for the list this inode will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) 	 * go on.  Make sure the pointer isn't garbage and that this inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) 	 * isn't already on the list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) 	next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) 	if (next_agino == agino ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) 	    !xfs_verify_agino_or_null(mp, agno, next_agino)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) 		xfs_buf_mark_corrupt(agibp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) 		return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) 	if (next_agino != NULLAGINO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) 		xfs_agino_t		old_agino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) 		 * There is already another inode in the bucket, so point this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) 		 * inode to the current head of the list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) 		error = xfs_iunlink_update_inode(tp, ip, agno, next_agino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) 				&old_agino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) 		ASSERT(old_agino == NULLAGINO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) 		 * agino has been unlinked, add a backref from the next inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) 		 * back to agino.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) 		error = xfs_iunlink_add_backref(agibp->b_pag, agino, next_agino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) 	/* Point the head of the list to point to this inode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) 	return xfs_iunlink_update_bucket(tp, agno, agibp, bucket_index, agino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) /* Return the imap, dinode pointer, and buffer for an inode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) xfs_iunlink_map_ino(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) 	struct xfs_trans	*tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) 	xfs_agnumber_t		agno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) 	xfs_agino_t		agino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) 	struct xfs_imap		*imap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) 	struct xfs_dinode	**dipp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) 	struct xfs_buf		**bpp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) 	struct xfs_mount	*mp = tp->t_mountp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) 	imap->im_blkno = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) 	error = xfs_imap(mp, tp, XFS_AGINO_TO_INO(mp, agno, agino), imap, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) 		xfs_warn(mp, "%s: xfs_imap returned error %d.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) 				__func__, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) 	error = xfs_imap_to_bp(mp, tp, imap, dipp, bpp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) 		xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) 				__func__, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320)  * Walk the unlinked chain from @head_agino until we find the inode that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321)  * points to @target_agino.  Return the inode number, map, dinode pointer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322)  * and inode cluster buffer of that inode as @agino, @imap, @dipp, and @bpp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324)  * @tp, @pag, @head_agino, and @target_agino are input parameters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325)  * @agino, @imap, @dipp, and @bpp are all output parameters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327)  * Do not call this function if @target_agino is the head of the list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) xfs_iunlink_map_prev(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) 	struct xfs_trans	*tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) 	xfs_agnumber_t		agno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) 	xfs_agino_t		head_agino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) 	xfs_agino_t		target_agino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) 	xfs_agino_t		*agino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) 	struct xfs_imap		*imap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) 	struct xfs_dinode	**dipp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) 	struct xfs_buf		**bpp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) 	struct xfs_perag	*pag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) 	struct xfs_mount	*mp = tp->t_mountp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) 	xfs_agino_t		next_agino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) 	ASSERT(head_agino != target_agino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) 	*bpp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) 	/* See if our backref cache can find it faster. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) 	*agino = xfs_iunlink_lookup_backref(pag, target_agino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) 	if (*agino != NULLAGINO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) 		error = xfs_iunlink_map_ino(tp, agno, *agino, imap, dipp, bpp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) 		if (be32_to_cpu((*dipp)->di_next_unlinked) == target_agino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) 		 * If we get here the cache contents were corrupt, so drop the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) 		 * buffer and fall back to walking the bucket list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) 		xfs_trans_brelse(tp, *bpp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) 		*bpp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) 		WARN_ON_ONCE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) 	trace_xfs_iunlink_map_prev_fallback(mp, agno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) 	/* Otherwise, walk the entire bucket until we find it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) 	next_agino = head_agino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) 	while (next_agino != target_agino) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) 		xfs_agino_t	unlinked_agino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) 		if (*bpp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) 			xfs_trans_brelse(tp, *bpp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) 		*agino = next_agino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) 		error = xfs_iunlink_map_ino(tp, agno, next_agino, imap, dipp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) 				bpp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) 		unlinked_agino = be32_to_cpu((*dipp)->di_next_unlinked);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) 		 * Make sure this pointer is valid and isn't an obvious
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) 		 * infinite loop.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) 		if (!xfs_verify_agino(mp, agno, unlinked_agino) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) 		    next_agino == unlinked_agino) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) 			XFS_CORRUPTION_ERROR(__func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) 					XFS_ERRLEVEL_LOW, mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) 					*dipp, sizeof(**dipp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) 		next_agino = unlinked_agino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403)  * Pull the on-disk inode from the AGI unlinked list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) xfs_iunlink_remove(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) 	struct xfs_trans	*tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) 	struct xfs_inode	*ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) 	struct xfs_mount	*mp = tp->t_mountp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) 	struct xfs_agi		*agi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) 	struct xfs_buf		*agibp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) 	struct xfs_buf		*last_ibp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) 	struct xfs_dinode	*last_dip = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) 	xfs_agnumber_t		agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) 	xfs_agino_t		agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) 	xfs_agino_t		next_agino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) 	xfs_agino_t		head_agino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) 	short			bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) 	trace_xfs_iunlink_remove(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) 	/* Get the agi buffer first.  It ensures lock ordering on the list. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) 	error = xfs_read_agi(mp, tp, agno, &agibp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) 	agi = agibp->b_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) 	 * Get the index into the agi hash table for the list this inode will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) 	 * go on.  Make sure the head pointer isn't garbage.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) 	head_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) 	if (!xfs_verify_agino(mp, agno, head_agino)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) 				agi, sizeof(*agi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) 		return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) 	 * Set our inode's next_unlinked pointer to NULL and then return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) 	 * the old pointer value so that we can update whatever was previous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) 	 * to us in the list to point to whatever was next in the list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) 	error = xfs_iunlink_update_inode(tp, ip, agno, NULLAGINO, &next_agino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) 	 * If there was a backref pointing from the next inode back to this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) 	 * one, remove it because we've removed this inode from the list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) 	 * Later, if this inode was in the middle of the list we'll update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) 	 * this inode's backref to point from the next inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) 	if (next_agino != NULLAGINO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) 		error = xfs_iunlink_change_backref(agibp->b_pag, next_agino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) 				NULLAGINO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) 	if (head_agino != agino) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) 		struct xfs_imap	imap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) 		xfs_agino_t	prev_agino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) 		/* We need to search the list for the inode being freed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) 		error = xfs_iunlink_map_prev(tp, agno, head_agino, agino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) 				&prev_agino, &imap, &last_dip, &last_ibp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) 				agibp->b_pag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) 		/* Point the previous inode on the list to the next inode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) 		xfs_iunlink_update_dinode(tp, agno, prev_agino, last_ibp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) 				last_dip, &imap, next_agino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) 		 * Now we deal with the backref for this inode.  If this inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) 		 * pointed at a real inode, change the backref that pointed to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) 		 * us to point to our old next.  If this inode was the end of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) 		 * the list, delete the backref that pointed to us.  Note that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) 		 * change_backref takes care of deleting the backref if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) 		 * next_agino is NULLAGINO.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) 		return xfs_iunlink_change_backref(agibp->b_pag, agino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) 				next_agino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) 	/* Point the head of the list to the next unlinked inode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) 	return xfs_iunlink_update_bucket(tp, agno, agibp, bucket_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) 			next_agino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497)  * Look up the inode number specified and if it is not already marked XFS_ISTALE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498)  * mark it stale. We should only find clean inodes in this lookup that aren't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499)  * already stale.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) xfs_ifree_mark_inode_stale(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) 	struct xfs_buf		*bp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) 	struct xfs_inode	*free_ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) 	xfs_ino_t		inum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) 	struct xfs_mount	*mp = bp->b_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) 	struct xfs_perag	*pag = bp->b_pag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) 	struct xfs_inode_log_item *iip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) 	struct xfs_inode	*ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) 	ip = radix_tree_lookup(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, inum));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) 	/* Inode not in memory, nothing to do */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) 	if (!ip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) 		rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) 	 * because this is an RCU protected lookup, we could find a recently
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) 	 * freed or even reallocated inode during the lookup. We need to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) 	 * under the i_flags_lock for a valid inode here. Skip it if it is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) 	 * valid, the wrong inode or stale.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) 	spin_lock(&ip->i_flags_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) 	if (ip->i_ino != inum || __xfs_iflags_test(ip, XFS_ISTALE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) 		goto out_iflags_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) 	 * Don't try to lock/unlock the current inode, but we _cannot_ skip the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) 	 * other inodes that we did not find in the list attached to the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) 	 * and are not already marked stale. If we can't lock it, back off and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) 	 * retry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) 	if (ip != free_ip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) 		if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) 			spin_unlock(&ip->i_flags_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) 			rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) 			delay(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) 			goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) 	ip->i_flags |= XFS_ISTALE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) 	 * If the inode is flushing, it is already attached to the buffer.  All
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) 	 * we needed to do here is mark the inode stale so buffer IO completion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) 	 * will remove it from the AIL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) 	iip = ip->i_itemp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) 	if (__xfs_iflags_test(ip, XFS_IFLUSHING)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) 		ASSERT(!list_empty(&iip->ili_item.li_bio_list));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) 		ASSERT(iip->ili_last_fields);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) 		goto out_iunlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) 	 * Inodes not attached to the buffer can be released immediately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) 	 * Everything else has to go through xfs_iflush_abort() on journal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) 	 * commit as the flock synchronises removal of the inode from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) 	 * cluster buffer against inode reclaim.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) 	if (!iip || list_empty(&iip->ili_item.li_bio_list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) 		goto out_iunlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) 	__xfs_iflags_set(ip, XFS_IFLUSHING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) 	spin_unlock(&ip->i_flags_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) 	/* we have a dirty inode in memory that has not yet been flushed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) 	spin_lock(&iip->ili_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) 	iip->ili_last_fields = iip->ili_fields;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) 	iip->ili_fields = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) 	iip->ili_fsync_fields = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) 	spin_unlock(&iip->ili_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) 	ASSERT(iip->ili_last_fields);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) 	if (ip != free_ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) 		xfs_iunlock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) out_iunlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) 	if (ip != free_ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) 		xfs_iunlock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) out_iflags_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) 	spin_unlock(&ip->i_flags_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594)  * A big issue when freeing the inode cluster is that we _cannot_ skip any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595)  * inodes that are in memory - they all must be marked stale and attached to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596)  * the cluster buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) xfs_ifree_cluster(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) 	struct xfs_inode	*free_ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) 	struct xfs_trans	*tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) 	struct xfs_icluster	*xic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) 	struct xfs_mount	*mp = free_ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) 	struct xfs_ino_geometry	*igeo = M_IGEO(mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) 	struct xfs_buf		*bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) 	xfs_daddr_t		blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) 	xfs_ino_t		inum = xic->first_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) 	int			nbufs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) 	int			i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) 	int			ioffset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) 	nbufs = igeo->ialloc_blks / igeo->blocks_per_cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) 	for (j = 0; j < nbufs; j++, inum += igeo->inodes_per_cluster) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) 		 * The allocation bitmap tells us which inodes of the chunk were
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) 		 * physically allocated. Skip the cluster if an inode falls into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) 		 * a sparse region.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) 		ioffset = inum - xic->first_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) 		if ((xic->alloc & XFS_INOBT_MASK(ioffset)) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) 			ASSERT(ioffset % igeo->inodes_per_cluster == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) 		blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) 					 XFS_INO_TO_AGBNO(mp, inum));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) 		 * We obtain and lock the backing buffer first in the process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) 		 * here to ensure dirty inodes attached to the buffer remain in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) 		 * the flushing state while we mark them stale.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) 		 * If we scan the in-memory inodes first, then buffer IO can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) 		 * complete before we get a lock on it, and hence we may fail
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) 		 * to mark all the active inodes on the buffer stale.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) 		error = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) 				mp->m_bsize * igeo->blocks_per_cluster,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) 				XBF_UNMAPPED, &bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) 		 * This buffer may not have been correctly initialised as we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) 		 * didn't read it from disk. That's not important because we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) 		 * only using to mark the buffer as stale in the log, and to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) 		 * attach stale cached inodes on it. That means it will never be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) 		 * dispatched for IO. If it is, we want to know about it, and we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) 		 * want it to fail. We can acheive this by adding a write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) 		 * verifier to the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) 		bp->b_ops = &xfs_inode_buf_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) 		 * Now we need to set all the cached clean inodes as XFS_ISTALE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) 		 * too. This requires lookups, and will skip inodes that we've
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) 		 * already marked XFS_ISTALE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) 		for (i = 0; i < igeo->inodes_per_cluster; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) 			xfs_ifree_mark_inode_stale(bp, free_ip, inum + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) 		xfs_trans_stale_inode_buf(tp, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) 		xfs_trans_binval(tp, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672)  * This is called to return an inode to the inode free list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673)  * The inode should already be truncated to 0 length and have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674)  * no pages associated with it.  This routine also assumes that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675)  * the inode is already a part of the transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677)  * The on-disk copy of the inode will have been added to the list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678)  * of unlinked inodes in the AGI. We need to remove the inode from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679)  * that list atomically with respect to freeing it here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) xfs_ifree(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) 	struct xfs_trans	*tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) 	struct xfs_inode	*ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) 	struct xfs_icluster	xic = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) 	struct xfs_inode_log_item *iip = ip->i_itemp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) 	ASSERT(VFS_I(ip)->i_nlink == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) 	ASSERT(ip->i_df.if_nextents == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) 	ASSERT(ip->i_d.di_size == 0 || !S_ISREG(VFS_I(ip)->i_mode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) 	ASSERT(ip->i_d.di_nblocks == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) 	 * Pull the on-disk inode from the AGI unlinked list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) 	error = xfs_iunlink_remove(tp, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) 	error = xfs_difree(tp, ip->i_ino, &xic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) 	 * Free any local-format data sitting around before we reset the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) 	 * data fork to extents format.  Note that the attr fork data has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) 	 * already been freed by xfs_attr_inactive.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) 	if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) 		kmem_free(ip->i_df.if_u1.if_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) 		ip->i_df.if_u1.if_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) 		ip->i_df.if_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) 	VFS_I(ip)->i_mode = 0;		/* mark incore inode as free */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) 	ip->i_d.di_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) 	ip->i_d.di_flags2 = ip->i_mount->m_ino_geo.new_diflags2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) 	ip->i_d.di_dmevmask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) 	ip->i_d.di_forkoff = 0;		/* mark the attr fork not in use */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) 	ip->i_df.if_format = XFS_DINODE_FMT_EXTENTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725) 	/* Don't attempt to replay owner changes for a deleted inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726) 	spin_lock(&iip->ili_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) 	iip->ili_fields &= ~(XFS_ILOG_AOWNER | XFS_ILOG_DOWNER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) 	spin_unlock(&iip->ili_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) 	 * Bump the generation count so no one will be confused
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) 	 * by reincarnations of this inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) 	VFS_I(ip)->i_generation++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) 	if (xic.deleted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) 		error = xfs_ifree_cluster(ip, tp, &xic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744)  * This is called to unpin an inode.  The caller must have the inode locked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745)  * in at least shared mode so that the buffer cannot be subsequently pinned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746)  * once someone is waiting for it to be unpinned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749) xfs_iunpin(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) 	struct xfs_inode	*ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) 	trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) 	/* Give the log a push to start the unpinning I/O */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) 	xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) __xfs_iunpin_wait(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) 	struct xfs_inode	*ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) 	wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) 	DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) 	xfs_iunpin(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) 		prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) 		if (xfs_ipincount(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) 			io_schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) 	} while (xfs_ipincount(ip));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) 	finish_wait(wq, &wait.wq_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) xfs_iunpin_wait(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) 	struct xfs_inode	*ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) 	if (xfs_ipincount(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) 		__xfs_iunpin_wait(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787)  * Removing an inode from the namespace involves removing the directory entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788)  * and dropping the link count on the inode. Removing the directory entry can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789)  * result in locking an AGF (directory blocks were freed) and removing a link
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790)  * count can result in placing the inode on an unlinked list which results in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791)  * locking an AGI.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793)  * The big problem here is that we have an ordering constraint on AGF and AGI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794)  * locking - inode allocation locks the AGI, then can allocate a new extent for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795)  * new inodes, locking the AGF after the AGI. Similarly, freeing the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796)  * removes the inode from the unlinked list, requiring that we lock the AGI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797)  * first, and then freeing the inode can result in an inode chunk being freed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798)  * and hence freeing disk space requiring that we lock an AGF.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800)  * Hence the ordering that is imposed by other parts of the code is AGI before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801)  * AGF. This means we cannot remove the directory entry before we drop the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802)  * reference count and put it on the unlinked list as this results in a lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803)  * order of AGF then AGI, and this can deadlock against inode allocation and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804)  * freeing. Therefore we must drop the link counts before we remove the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805)  * directory entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807)  * This is still safe from a transactional point of view - it is not until we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808)  * get to xfs_defer_finish() that we have the possibility of multiple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809)  * transactions in this operation. Hence as long as we remove the directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810)  * entry and drop the link count in the first transaction of the remove
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811)  * operation, there are no transactional constraints on the ordering here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) xfs_remove(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) 	xfs_inode_t             *dp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) 	struct xfs_name		*name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) 	xfs_inode_t		*ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) 	xfs_mount_t		*mp = dp->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) 	xfs_trans_t             *tp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) 	int			is_dir = S_ISDIR(VFS_I(ip)->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) 	int                     error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) 	uint			resblks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) 	trace_xfs_remove(dp, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827) 	if (XFS_FORCED_SHUTDOWN(mp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) 	error = xfs_qm_dqattach(dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832) 		goto std_return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) 	error = xfs_qm_dqattach(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) 		goto std_return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) 	 * We try to get the real space reservation first,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) 	 * allowing for directory btree deletion(s) implying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841) 	 * possible bmap insert(s).  If we can't get the space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) 	 * reservation then we use 0 instead, and avoid the bmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) 	 * btree insert(s) in the directory code by, if the bmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) 	 * insert tries to happen, instead trimming the LAST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) 	 * block from the directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) 	resblks = XFS_REMOVE_SPACE_RES(mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_remove, resblks, 0, 0, &tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) 	if (error == -ENOSPC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850) 		resblks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) 		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_remove, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) 				&tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) 		ASSERT(error != -ENOSPC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) 		goto std_return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) 	xfs_lock_two_inodes(dp, XFS_ILOCK_EXCL, ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) 	xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) 	 * If we're removing a directory perform some additional validation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) 	if (is_dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) 		ASSERT(VFS_I(ip)->i_nlink >= 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) 		if (VFS_I(ip)->i_nlink != 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) 			error = -ENOTEMPTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) 			goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873) 		if (!xfs_dir_isempty(ip)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) 			error = -ENOTEMPTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) 			goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) 		/* Drop the link from ip's "..".  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) 		error = xfs_droplink(tp, dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) 			goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) 		/* Drop the "." link from ip to self.  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) 		error = xfs_droplink(tp, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886) 			goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889) 		 * When removing a non-directory we need to log the parent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890) 		 * inode here.  For a directory this is done implicitly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891) 		 * by the xfs_droplink call for the ".." entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) 		xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) 	xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897) 	/* Drop the link from dp to ip. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) 	error = xfs_droplink(tp, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) 		goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902) 	error = xfs_dir_removename(tp, dp, name, ip->i_ino, resblks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904) 		ASSERT(error != -ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905) 		goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909) 	 * If this is a synchronous mount, make sure that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) 	 * remove transaction goes to disk before returning to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911) 	 * the user.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) 	if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) 		xfs_trans_set_sync(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916) 	error = xfs_trans_commit(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) 		goto std_return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920) 	if (is_dir && xfs_inode_is_filestream(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921) 		xfs_filestream_deassociate(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925)  out_trans_cancel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926) 	xfs_trans_cancel(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927)  std_return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932)  * Enter all inodes for a rename transaction into a sorted array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934) #define __XFS_SORT_INODES	5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935) STATIC void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2936) xfs_sort_for_rename(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2937) 	struct xfs_inode	*dp1,	/* in: old (source) directory inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2938) 	struct xfs_inode	*dp2,	/* in: new (target) directory inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2939) 	struct xfs_inode	*ip1,	/* in: inode of old entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2940) 	struct xfs_inode	*ip2,	/* in: inode of new entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2941) 	struct xfs_inode	*wip,	/* in: whiteout inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2942) 	struct xfs_inode	**i_tab,/* out: sorted array of inodes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2943) 	int			*num_inodes)  /* in/out: inodes in array */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2944) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2945) 	int			i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2946) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2947) 	ASSERT(*num_inodes == __XFS_SORT_INODES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2948) 	memset(i_tab, 0, *num_inodes * sizeof(struct xfs_inode *));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2950) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2951) 	 * i_tab contains a list of pointers to inodes.  We initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2952) 	 * the table here & we'll sort it.  We will then use it to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2953) 	 * order the acquisition of the inode locks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2954) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2955) 	 * Note that the table may contain duplicates.  e.g., dp1 == dp2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2956) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2957) 	i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2958) 	i_tab[i++] = dp1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2959) 	i_tab[i++] = dp2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2960) 	i_tab[i++] = ip1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2961) 	if (ip2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2962) 		i_tab[i++] = ip2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2963) 	if (wip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2964) 		i_tab[i++] = wip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2965) 	*num_inodes = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2967) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2968) 	 * Sort the elements via bubble sort.  (Remember, there are at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2969) 	 * most 5 elements to sort, so this is adequate.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2970) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2971) 	for (i = 0; i < *num_inodes; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2972) 		for (j = 1; j < *num_inodes; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2973) 			if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2974) 				struct xfs_inode *temp = i_tab[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2975) 				i_tab[j] = i_tab[j-1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2976) 				i_tab[j-1] = temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2977) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2978) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2979) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2980) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2982) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2983) xfs_finish_rename(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2984) 	struct xfs_trans	*tp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2985) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2986) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2987) 	 * If this is a synchronous mount, make sure that the rename transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2988) 	 * goes to disk before returning to the user.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2989) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2990) 	if (tp->t_mountp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2991) 		xfs_trans_set_sync(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2992) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2993) 	return xfs_trans_commit(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2994) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2995) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2996) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2997)  * xfs_cross_rename()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2998)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2999)  * responsible for handling RENAME_EXCHANGE flag in renameat2() sytemcall
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3000)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3001) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3002) xfs_cross_rename(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3003) 	struct xfs_trans	*tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3004) 	struct xfs_inode	*dp1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3005) 	struct xfs_name		*name1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3006) 	struct xfs_inode	*ip1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3007) 	struct xfs_inode	*dp2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3008) 	struct xfs_name		*name2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3009) 	struct xfs_inode	*ip2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3010) 	int			spaceres)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3011) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3012) 	int		error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3013) 	int		ip1_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3014) 	int		ip2_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3015) 	int		dp2_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3016) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3017) 	/* Swap inode number for dirent in first parent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3018) 	error = xfs_dir_replace(tp, dp1, name1, ip2->i_ino, spaceres);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3019) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3020) 		goto out_trans_abort;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3022) 	/* Swap inode number for dirent in second parent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3023) 	error = xfs_dir_replace(tp, dp2, name2, ip1->i_ino, spaceres);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3024) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3025) 		goto out_trans_abort;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3027) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3028) 	 * If we're renaming one or more directories across different parents,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3029) 	 * update the respective ".." entries (and link counts) to match the new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3030) 	 * parents.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3031) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3032) 	if (dp1 != dp2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3033) 		dp2_flags = XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3034) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3035) 		if (S_ISDIR(VFS_I(ip2)->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3036) 			error = xfs_dir_replace(tp, ip2, &xfs_name_dotdot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3037) 						dp1->i_ino, spaceres);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3038) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3039) 				goto out_trans_abort;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3040) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3041) 			/* transfer ip2 ".." reference to dp1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3042) 			if (!S_ISDIR(VFS_I(ip1)->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3043) 				error = xfs_droplink(tp, dp2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3044) 				if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3045) 					goto out_trans_abort;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3046) 				xfs_bumplink(tp, dp1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3047) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3048) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3049) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3050) 			 * Although ip1 isn't changed here, userspace needs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3051) 			 * to be warned about the change, so that applications
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3052) 			 * relying on it (like backup ones), will properly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3053) 			 * notify the change
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3054) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3055) 			ip1_flags |= XFS_ICHGTIME_CHG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3056) 			ip2_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3057) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3058) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3059) 		if (S_ISDIR(VFS_I(ip1)->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3060) 			error = xfs_dir_replace(tp, ip1, &xfs_name_dotdot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3061) 						dp2->i_ino, spaceres);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3062) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3063) 				goto out_trans_abort;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3064) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3065) 			/* transfer ip1 ".." reference to dp2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3066) 			if (!S_ISDIR(VFS_I(ip2)->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3067) 				error = xfs_droplink(tp, dp1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3068) 				if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3069) 					goto out_trans_abort;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3070) 				xfs_bumplink(tp, dp2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3071) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3072) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3073) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3074) 			 * Although ip2 isn't changed here, userspace needs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3075) 			 * to be warned about the change, so that applications
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3076) 			 * relying on it (like backup ones), will properly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3077) 			 * notify the change
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3078) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3079) 			ip1_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3080) 			ip2_flags |= XFS_ICHGTIME_CHG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3081) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3082) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3084) 	if (ip1_flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3085) 		xfs_trans_ichgtime(tp, ip1, ip1_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3086) 		xfs_trans_log_inode(tp, ip1, XFS_ILOG_CORE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3087) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3088) 	if (ip2_flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3089) 		xfs_trans_ichgtime(tp, ip2, ip2_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3090) 		xfs_trans_log_inode(tp, ip2, XFS_ILOG_CORE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3091) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3092) 	if (dp2_flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3093) 		xfs_trans_ichgtime(tp, dp2, dp2_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3094) 		xfs_trans_log_inode(tp, dp2, XFS_ILOG_CORE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3095) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3096) 	xfs_trans_ichgtime(tp, dp1, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3097) 	xfs_trans_log_inode(tp, dp1, XFS_ILOG_CORE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3098) 	return xfs_finish_rename(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3099) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3100) out_trans_abort:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3101) 	xfs_trans_cancel(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3102) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3105) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3106)  * xfs_rename_alloc_whiteout()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3107)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3108)  * Return a referenced, unlinked, unlocked inode that can be used as a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3109)  * whiteout in a rename transaction. We use a tmpfile inode here so that if we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3110)  * crash between allocating the inode and linking it into the rename transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3111)  * recovery will free the inode and we won't leak it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3112)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3113) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3114) xfs_rename_alloc_whiteout(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3115) 	struct xfs_inode	*dp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3116) 	struct xfs_inode	**wip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3118) 	struct xfs_inode	*tmpfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3119) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3121) 	error = xfs_create_tmpfile(dp, S_IFCHR | WHITEOUT_MODE, &tmpfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3122) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3123) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3125) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3126) 	 * Prepare the tmpfile inode as if it were created through the VFS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3127) 	 * Complete the inode setup and flag it as linkable.  nlink is already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3128) 	 * zero, so we can skip the drop_nlink.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3129) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3130) 	xfs_setup_iops(tmpfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3131) 	xfs_finish_inode_setup(tmpfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3132) 	VFS_I(tmpfile)->i_state |= I_LINKABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3134) 	*wip = tmpfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3135) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3138) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3139)  * xfs_rename
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3140)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3141) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3142) xfs_rename(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3143) 	struct xfs_inode	*src_dp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3144) 	struct xfs_name		*src_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3145) 	struct xfs_inode	*src_ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3146) 	struct xfs_inode	*target_dp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3147) 	struct xfs_name		*target_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3148) 	struct xfs_inode	*target_ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3149) 	unsigned int		flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3151) 	struct xfs_mount	*mp = src_dp->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3152) 	struct xfs_trans	*tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3153) 	struct xfs_inode	*wip = NULL;		/* whiteout inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3154) 	struct xfs_inode	*inodes[__XFS_SORT_INODES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3155) 	struct xfs_buf		*agibp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3156) 	int			num_inodes = __XFS_SORT_INODES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3157) 	bool			new_parent = (src_dp != target_dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3158) 	bool			src_is_directory = S_ISDIR(VFS_I(src_ip)->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3159) 	int			spaceres;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3160) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3162) 	trace_xfs_rename(src_dp, target_dp, src_name, target_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3164) 	if ((flags & RENAME_EXCHANGE) && !target_ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3165) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3167) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3168) 	 * If we are doing a whiteout operation, allocate the whiteout inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3169) 	 * we will be placing at the target and ensure the type is set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3170) 	 * appropriately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3171) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3172) 	if (flags & RENAME_WHITEOUT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3173) 		ASSERT(!(flags & (RENAME_NOREPLACE | RENAME_EXCHANGE)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3174) 		error = xfs_rename_alloc_whiteout(target_dp, &wip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3175) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3176) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3178) 		/* setup target dirent info as whiteout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3179) 		src_name->type = XFS_DIR3_FT_CHRDEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3180) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3182) 	xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip, wip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3183) 				inodes, &num_inodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3185) 	spaceres = XFS_RENAME_SPACE_RES(mp, target_name->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3186) 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_rename, spaceres, 0, 0, &tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3187) 	if (error == -ENOSPC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3188) 		spaceres = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3189) 		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_rename, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3190) 				&tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3191) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3192) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3193) 		goto out_release_wip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3195) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3196) 	 * Attach the dquots to the inodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3197) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3198) 	error = xfs_qm_vop_rename_dqattach(inodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3199) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3200) 		goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3202) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3203) 	 * Lock all the participating inodes. Depending upon whether
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3204) 	 * the target_name exists in the target directory, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3205) 	 * whether the target directory is the same as the source
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3206) 	 * directory, we can lock from 2 to 4 inodes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3207) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3208) 	xfs_lock_inodes(inodes, num_inodes, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3210) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3211) 	 * Join all the inodes to the transaction. From this point on,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3212) 	 * we can rely on either trans_commit or trans_cancel to unlock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3213) 	 * them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3214) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3215) 	xfs_trans_ijoin(tp, src_dp, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3216) 	if (new_parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3217) 		xfs_trans_ijoin(tp, target_dp, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3218) 	xfs_trans_ijoin(tp, src_ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3219) 	if (target_ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3220) 		xfs_trans_ijoin(tp, target_ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3221) 	if (wip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3222) 		xfs_trans_ijoin(tp, wip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3224) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3225) 	 * If we are using project inheritance, we only allow renames
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3226) 	 * into our tree when the project IDs are the same; else the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3227) 	 * tree quota mechanism would be circumvented.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3228) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3229) 	if (unlikely((target_dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3230) 		     target_dp->i_d.di_projid != src_ip->i_d.di_projid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3231) 		error = -EXDEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3232) 		goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3233) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3235) 	/* RENAME_EXCHANGE is unique from here on. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3236) 	if (flags & RENAME_EXCHANGE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3237) 		return xfs_cross_rename(tp, src_dp, src_name, src_ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3238) 					target_dp, target_name, target_ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3239) 					spaceres);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3241) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3242) 	 * Check for expected errors before we dirty the transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3243) 	 * so we can return an error without a transaction abort.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3244) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3245) 	if (target_ip == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3246) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3247) 		 * If there's no space reservation, check the entry will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3248) 		 * fit before actually inserting it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3249) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3250) 		if (!spaceres) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3251) 			error = xfs_dir_canenter(tp, target_dp, target_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3252) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3253) 				goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3254) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3255) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3256) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3257) 		 * If target exists and it's a directory, check that whether
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3258) 		 * it can be destroyed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3259) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3260) 		if (S_ISDIR(VFS_I(target_ip)->i_mode) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3261) 		    (!xfs_dir_isempty(target_ip) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3262) 		     (VFS_I(target_ip)->i_nlink > 2))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3263) 			error = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3264) 			goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3265) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3266) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3268) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3269) 	 * Directory entry creation below may acquire the AGF. Remove
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3270) 	 * the whiteout from the unlinked list first to preserve correct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3271) 	 * AGI/AGF locking order. This dirties the transaction so failures
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3272) 	 * after this point will abort and log recovery will clean up the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3273) 	 * mess.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3274) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3275) 	 * For whiteouts, we need to bump the link count on the whiteout
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3276) 	 * inode. After this point, we have a real link, clear the tmpfile
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3277) 	 * state flag from the inode so it doesn't accidentally get misused
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3278) 	 * in future.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3279) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3280) 	if (wip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3281) 		ASSERT(VFS_I(wip)->i_nlink == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3282) 		error = xfs_iunlink_remove(tp, wip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3283) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3284) 			goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3286) 		xfs_bumplink(tp, wip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3287) 		VFS_I(wip)->i_state &= ~I_LINKABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3288) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3290) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3291) 	 * Set up the target.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3292) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3293) 	if (target_ip == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3294) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3295) 		 * If target does not exist and the rename crosses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3296) 		 * directories, adjust the target directory link count
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3297) 		 * to account for the ".." reference from the new entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3298) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3299) 		error = xfs_dir_createname(tp, target_dp, target_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3300) 					   src_ip->i_ino, spaceres);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3301) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3302) 			goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3304) 		xfs_trans_ichgtime(tp, target_dp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3305) 					XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3307) 		if (new_parent && src_is_directory) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3308) 			xfs_bumplink(tp, target_dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3309) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3310) 	} else { /* target_ip != NULL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3311) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3312) 		 * Link the source inode under the target name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3313) 		 * If the source inode is a directory and we are moving
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3314) 		 * it across directories, its ".." entry will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3315) 		 * inconsistent until we replace that down below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3316) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3317) 		 * In case there is already an entry with the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3318) 		 * name at the destination directory, remove it first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3319) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3321) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3322) 		 * Check whether the replace operation will need to allocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3323) 		 * blocks.  This happens when the shortform directory lacks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3324) 		 * space and we have to convert it to a block format directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3325) 		 * When more blocks are necessary, we must lock the AGI first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3326) 		 * to preserve locking order (AGI -> AGF).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3327) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3328) 		if (xfs_dir2_sf_replace_needblock(target_dp, src_ip->i_ino)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3329) 			error = xfs_read_agi(mp, tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3330) 					XFS_INO_TO_AGNO(mp, target_ip->i_ino),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3331) 					&agibp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3332) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3333) 				goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3334) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3336) 		error = xfs_dir_replace(tp, target_dp, target_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3337) 					src_ip->i_ino, spaceres);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3338) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3339) 			goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3341) 		xfs_trans_ichgtime(tp, target_dp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3342) 					XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3344) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3345) 		 * Decrement the link count on the target since the target
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3346) 		 * dir no longer points to it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3347) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3348) 		error = xfs_droplink(tp, target_ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3349) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3350) 			goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3352) 		if (src_is_directory) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3353) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3354) 			 * Drop the link from the old "." entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3355) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3356) 			error = xfs_droplink(tp, target_ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3357) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3358) 				goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3359) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3360) 	} /* target_ip != NULL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3362) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3363) 	 * Remove the source.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3364) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3365) 	if (new_parent && src_is_directory) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3366) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3367) 		 * Rewrite the ".." entry to point to the new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3368) 		 * directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3369) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3370) 		error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3371) 					target_dp->i_ino, spaceres);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3372) 		ASSERT(error != -EEXIST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3373) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3374) 			goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3375) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3377) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3378) 	 * We always want to hit the ctime on the source inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3379) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3380) 	 * This isn't strictly required by the standards since the source
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3381) 	 * inode isn't really being changed, but old unix file systems did
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3382) 	 * it and some incremental backup programs won't work without it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3383) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3384) 	xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3385) 	xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3387) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3388) 	 * Adjust the link count on src_dp.  This is necessary when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3389) 	 * renaming a directory, either within one parent when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3390) 	 * the target existed, or across two parent directories.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3391) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3392) 	if (src_is_directory && (new_parent || target_ip != NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3394) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3395) 		 * Decrement link count on src_directory since the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3396) 		 * entry that's moved no longer points to it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3397) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3398) 		error = xfs_droplink(tp, src_dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3399) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3400) 			goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3401) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3403) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3404) 	 * For whiteouts, we only need to update the source dirent with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3405) 	 * inode number of the whiteout inode rather than removing it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3406) 	 * altogether.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3407) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3408) 	if (wip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3409) 		error = xfs_dir_replace(tp, src_dp, src_name, wip->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3410) 					spaceres);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3411) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3412) 		error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3413) 					   spaceres);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3414) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3415) 		goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3417) 	xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3418) 	xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3419) 	if (new_parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3420) 		xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3422) 	error = xfs_finish_rename(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3423) 	if (wip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3424) 		xfs_irele(wip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3425) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3427) out_trans_cancel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3428) 	xfs_trans_cancel(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3429) out_release_wip:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3430) 	if (wip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3431) 		xfs_irele(wip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3432) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3435) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3436) xfs_iflush(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3437) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3438) 	struct xfs_buf		*bp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3439) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3440) 	struct xfs_inode_log_item *iip = ip->i_itemp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3441) 	struct xfs_dinode	*dip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3442) 	struct xfs_mount	*mp = ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3443) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3445) 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3446) 	ASSERT(xfs_iflags_test(ip, XFS_IFLUSHING));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3447) 	ASSERT(ip->i_df.if_format != XFS_DINODE_FMT_BTREE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3448) 	       ip->i_df.if_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3449) 	ASSERT(iip->ili_item.li_buf == bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3451) 	dip = xfs_buf_offset(bp, ip->i_imap.im_boffset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3453) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3454) 	 * We don't flush the inode if any of the following checks fail, but we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3455) 	 * do still update the log item and attach to the backing buffer as if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3456) 	 * the flush happened. This is a formality to facilitate predictable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3457) 	 * error handling as the caller will shutdown and fail the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3458) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3459) 	error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3460) 	if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3461) 			       mp, XFS_ERRTAG_IFLUSH_1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3462) 		xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3463) 			"%s: Bad inode %Lu magic number 0x%x, ptr "PTR_FMT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3464) 			__func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3465) 		goto flush_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3466) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3467) 	if (S_ISREG(VFS_I(ip)->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3468) 		if (XFS_TEST_ERROR(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3469) 		    ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3470) 		    ip->i_df.if_format != XFS_DINODE_FMT_BTREE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3471) 		    mp, XFS_ERRTAG_IFLUSH_3)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3472) 			xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3473) 				"%s: Bad regular inode %Lu, ptr "PTR_FMT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3474) 				__func__, ip->i_ino, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3475) 			goto flush_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3476) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3477) 	} else if (S_ISDIR(VFS_I(ip)->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3478) 		if (XFS_TEST_ERROR(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3479) 		    ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3480) 		    ip->i_df.if_format != XFS_DINODE_FMT_BTREE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3481) 		    ip->i_df.if_format != XFS_DINODE_FMT_LOCAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3482) 		    mp, XFS_ERRTAG_IFLUSH_4)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3483) 			xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3484) 				"%s: Bad directory inode %Lu, ptr "PTR_FMT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3485) 				__func__, ip->i_ino, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3486) 			goto flush_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3487) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3488) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3489) 	if (XFS_TEST_ERROR(ip->i_df.if_nextents + xfs_ifork_nextents(ip->i_afp) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3490) 				ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3491) 		xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3492) 			"%s: detected corrupt incore inode %Lu, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3493) 			"total extents = %d, nblocks = %Ld, ptr "PTR_FMT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3494) 			__func__, ip->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3495) 			ip->i_df.if_nextents + xfs_ifork_nextents(ip->i_afp),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3496) 			ip->i_d.di_nblocks, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3497) 		goto flush_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3498) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3499) 	if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3500) 				mp, XFS_ERRTAG_IFLUSH_6)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3501) 		xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3502) 			"%s: bad inode %Lu, forkoff 0x%x, ptr "PTR_FMT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3503) 			__func__, ip->i_ino, ip->i_d.di_forkoff, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3504) 		goto flush_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3505) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3507) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3508) 	 * Inode item log recovery for v2 inodes are dependent on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3509) 	 * di_flushiter count for correct sequencing. We bump the flush
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3510) 	 * iteration count so we can detect flushes which postdate a log record
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3511) 	 * during recovery. This is redundant as we now log every change and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3512) 	 * hence this can't happen but we need to still do it to ensure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3513) 	 * backwards compatibility with old kernels that predate logging all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3514) 	 * inode changes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3515) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3516) 	if (!xfs_sb_version_has_v3inode(&mp->m_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3517) 		ip->i_d.di_flushiter++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3519) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3520) 	 * If there are inline format data / attr forks attached to this inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3521) 	 * make sure they are not corrupt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3522) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3523) 	if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3524) 	    xfs_ifork_verify_local_data(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3525) 		goto flush_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3526) 	if (ip->i_afp && ip->i_afp->if_format == XFS_DINODE_FMT_LOCAL &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3527) 	    xfs_ifork_verify_local_attr(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3528) 		goto flush_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3530) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3531) 	 * Copy the dirty parts of the inode into the on-disk inode.  We always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3532) 	 * copy out the core of the inode, because if the inode is dirty at all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3533) 	 * the core must be.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3534) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3535) 	xfs_inode_to_disk(ip, dip, iip->ili_item.li_lsn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3537) 	/* Wrap, we never let the log put out DI_MAX_FLUSH */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3538) 	if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3539) 		ip->i_d.di_flushiter = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3541) 	xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3542) 	if (XFS_IFORK_Q(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3543) 		xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3545) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3546) 	 * We've recorded everything logged in the inode, so we'd like to clear
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3547) 	 * the ili_fields bits so we don't log and flush things unnecessarily.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3548) 	 * However, we can't stop logging all this information until the data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3549) 	 * we've copied into the disk buffer is written to disk.  If we did we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3550) 	 * might overwrite the copy of the inode in the log with all the data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3551) 	 * after re-logging only part of it, and in the face of a crash we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3552) 	 * wouldn't have all the data we need to recover.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3553) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3554) 	 * What we do is move the bits to the ili_last_fields field.  When
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3555) 	 * logging the inode, these bits are moved back to the ili_fields field.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3556) 	 * In the xfs_buf_inode_iodone() routine we clear ili_last_fields, since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3557) 	 * we know that the information those bits represent is permanently on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3558) 	 * disk.  As long as the flush completes before the inode is logged
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3559) 	 * again, then both ili_fields and ili_last_fields will be cleared.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3560) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3561) 	error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3562) flush_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3563) 	spin_lock(&iip->ili_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3564) 	iip->ili_last_fields = iip->ili_fields;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3565) 	iip->ili_fields = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3566) 	iip->ili_fsync_fields = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3567) 	spin_unlock(&iip->ili_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3569) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3570) 	 * Store the current LSN of the inode so that we can tell whether the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3571) 	 * item has moved in the AIL from xfs_buf_inode_iodone().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3572) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3573) 	xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3574) 				&iip->ili_item.li_lsn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3576) 	/* generate the checksum. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3577) 	xfs_dinode_calc_crc(mp, dip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3578) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3579) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3581) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3582)  * Non-blocking flush of dirty inode metadata into the backing buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3583)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3584)  * The caller must have a reference to the inode and hold the cluster buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3585)  * locked. The function will walk across all the inodes on the cluster buffer it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3586)  * can find and lock without blocking, and flush them to the cluster buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3587)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3588)  * On successful flushing of at least one inode, the caller must write out the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3589)  * buffer and release it. If no inodes are flushed, -EAGAIN will be returned and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3590)  * the caller needs to release the buffer. On failure, the filesystem will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3591)  * shut down, the buffer will have been unlocked and released, and EFSCORRUPTED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3592)  * will be returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3593)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3594) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3595) xfs_iflush_cluster(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3596) 	struct xfs_buf		*bp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3597) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3598) 	struct xfs_mount	*mp = bp->b_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3599) 	struct xfs_log_item	*lip, *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3600) 	struct xfs_inode	*ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3601) 	struct xfs_inode_log_item *iip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3602) 	int			clcount = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3603) 	int			error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3605) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3606) 	 * We must use the safe variant here as on shutdown xfs_iflush_abort()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3607) 	 * can remove itself from the list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3608) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3609) 	list_for_each_entry_safe(lip, n, &bp->b_li_list, li_bio_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3610) 		iip = (struct xfs_inode_log_item *)lip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3611) 		ip = iip->ili_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3613) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3614) 		 * Quick and dirty check to avoid locks if possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3615) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3616) 		if (__xfs_iflags_test(ip, XFS_IRECLAIM | XFS_IFLUSHING))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3617) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3618) 		if (xfs_ipincount(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3619) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3621) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3622) 		 * The inode is still attached to the buffer, which means it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3623) 		 * dirty but reclaim might try to grab it. Check carefully for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3624) 		 * that, and grab the ilock while still holding the i_flags_lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3625) 		 * to guarantee reclaim will not be able to reclaim this inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3626) 		 * once we drop the i_flags_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3627) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3628) 		spin_lock(&ip->i_flags_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3629) 		ASSERT(!__xfs_iflags_test(ip, XFS_ISTALE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3630) 		if (__xfs_iflags_test(ip, XFS_IRECLAIM | XFS_IFLUSHING)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3631) 			spin_unlock(&ip->i_flags_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3632) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3633) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3635) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3636) 		 * ILOCK will pin the inode against reclaim and prevent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3637) 		 * concurrent transactions modifying the inode while we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3638) 		 * flushing the inode. If we get the lock, set the flushing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3639) 		 * state before we drop the i_flags_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3640) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3641) 		if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3642) 			spin_unlock(&ip->i_flags_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3643) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3644) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3645) 		__xfs_iflags_set(ip, XFS_IFLUSHING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3646) 		spin_unlock(&ip->i_flags_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3648) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3649) 		 * Abort flushing this inode if we are shut down because the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3650) 		 * inode may not currently be in the AIL. This can occur when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3651) 		 * log I/O failure unpins the inode without inserting into the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3652) 		 * AIL, leaving a dirty/unpinned inode attached to the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3653) 		 * that otherwise looks like it should be flushed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3654) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3655) 		if (XFS_FORCED_SHUTDOWN(mp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3656) 			xfs_iunpin_wait(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3657) 			xfs_iflush_abort(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3658) 			xfs_iunlock(ip, XFS_ILOCK_SHARED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3659) 			error = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3660) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3661) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3663) 		/* don't block waiting on a log force to unpin dirty inodes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3664) 		if (xfs_ipincount(ip)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3665) 			xfs_iflags_clear(ip, XFS_IFLUSHING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3666) 			xfs_iunlock(ip, XFS_ILOCK_SHARED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3667) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3668) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3670) 		if (!xfs_inode_clean(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3671) 			error = xfs_iflush(ip, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3672) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3673) 			xfs_iflags_clear(ip, XFS_IFLUSHING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3674) 		xfs_iunlock(ip, XFS_ILOCK_SHARED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3675) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3676) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3677) 		clcount++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3678) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3680) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3681) 		bp->b_flags |= XBF_ASYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3682) 		xfs_buf_ioend_fail(bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3683) 		xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3684) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3685) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3687) 	if (!clcount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3688) 		return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3690) 	XFS_STATS_INC(mp, xs_icluster_flushcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3691) 	XFS_STATS_ADD(mp, xs_icluster_flushinode, clcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3692) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3693) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3694) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3696) /* Release an inode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3697) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3698) xfs_irele(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3699) 	struct xfs_inode	*ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3700) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3701) 	trace_xfs_irele(ip, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3702) 	iput(VFS_I(ip));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3703) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3705) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3706)  * Ensure all commited transactions touching the inode are written to the log.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3707)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3708) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3709) xfs_log_force_inode(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3710) 	struct xfs_inode	*ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3711) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3712) 	xfs_lsn_t		lsn = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3713) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3714) 	xfs_ilock(ip, XFS_ILOCK_SHARED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3715) 	if (xfs_ipincount(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3716) 		lsn = ip->i_itemp->ili_last_lsn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3717) 	xfs_iunlock(ip, XFS_ILOCK_SHARED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3719) 	if (!lsn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3720) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3721) 	return xfs_log_force_lsn(ip->i_mount, lsn, XFS_LOG_SYNC, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3722) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3724) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3725)  * Grab the exclusive iolock for a data copy from src to dest, making sure to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3726)  * abide vfs locking order (lowest pointer value goes first) and breaking the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3727)  * layout leases before proceeding.  The loop is needed because we cannot call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3728)  * the blocking break_layout() with the iolocks held, and therefore have to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3729)  * back out both locks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3730)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3731) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3732) xfs_iolock_two_inodes_and_break_layout(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3733) 	struct inode		*src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3734) 	struct inode		*dest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3735) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3736) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3738) 	if (src > dest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3739) 		swap(src, dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3741) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3742) 	/* Wait to break both inodes' layouts before we start locking. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3743) 	error = break_layout(src, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3744) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3745) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3746) 	if (src != dest) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3747) 		error = break_layout(dest, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3748) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3749) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3750) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3752) 	/* Lock one inode and make sure nobody got in and leased it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3753) 	inode_lock(src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3754) 	error = break_layout(src, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3755) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3756) 		inode_unlock(src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3757) 		if (error == -EWOULDBLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3758) 			goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3759) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3760) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3762) 	if (src == dest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3763) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3765) 	/* Lock the other inode and make sure nobody got in and leased it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3766) 	inode_lock_nested(dest, I_MUTEX_NONDIR2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3767) 	error = break_layout(dest, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3768) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3769) 		inode_unlock(src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3770) 		inode_unlock(dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3771) 		if (error == -EWOULDBLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3772) 			goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3773) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3774) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3776) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3777) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3779) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3780)  * Lock two inodes so that userspace cannot initiate I/O via file syscalls or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3781)  * mmap activity.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3782)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3783) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3784) xfs_ilock2_io_mmap(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3785) 	struct xfs_inode	*ip1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3786) 	struct xfs_inode	*ip2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3787) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3788) 	int			ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3790) 	ret = xfs_iolock_two_inodes_and_break_layout(VFS_I(ip1), VFS_I(ip2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3791) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3792) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3793) 	if (ip1 == ip2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3794) 		xfs_ilock(ip1, XFS_MMAPLOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3795) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3796) 		xfs_lock_two_inodes(ip1, XFS_MMAPLOCK_EXCL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3797) 				    ip2, XFS_MMAPLOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3798) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3799) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3801) /* Unlock both inodes to allow IO and mmap activity. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3802) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3803) xfs_iunlock2_io_mmap(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3804) 	struct xfs_inode	*ip1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3805) 	struct xfs_inode	*ip2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3806) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3807) 	bool			same_inode = (ip1 == ip2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3809) 	xfs_iunlock(ip2, XFS_MMAPLOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3810) 	if (!same_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3811) 		xfs_iunlock(ip1, XFS_MMAPLOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3812) 	inode_unlock(VFS_I(ip2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3813) 	if (!same_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3814) 		inode_unlock(VFS_I(ip1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3815) }