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)  * Copyright (c) 2012 Red Hat, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #include "xfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include "xfs_fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include "xfs_shared.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include "xfs_format.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include "xfs_log_format.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include "xfs_trans_resv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include "xfs_bit.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include "xfs_mount.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include "xfs_defer.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include "xfs_inode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include "xfs_btree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include "xfs_trans.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include "xfs_alloc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include "xfs_bmap.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include "xfs_bmap_util.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include "xfs_bmap_btree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include "xfs_rtalloc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include "xfs_error.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include "xfs_quota.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include "xfs_trans_space.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include "xfs_trace.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include "xfs_icache.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include "xfs_iomap.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include "xfs_reflink.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) /* Kernel only BMAP related definitions and functions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35)  * Convert the given file system block to a disk block.  We have to treat it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36)  * differently based on whether the file is a real time file or not, because the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37)  * bmap code does.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) xfs_daddr_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) xfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 	if (XFS_IS_REALTIME_INODE(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 		return XFS_FSB_TO_BB(ip->i_mount, fsb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 	return XFS_FSB_TO_DADDR(ip->i_mount, fsb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48)  * Routine to zero an extent on disk allocated to the specific inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50)  * The VFS functions take a linearised filesystem block offset, so we have to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51)  * convert the sparse xfs fsb to the right format first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52)  * VFS types are real funky, too.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) xfs_zero_extent(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 	xfs_fsblock_t		start_fsb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	xfs_off_t		count_fsb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	struct xfs_mount	*mp = ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 	struct xfs_buftarg	*target = xfs_inode_buftarg(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 	xfs_daddr_t		sector = xfs_fsb_to_db(ip, start_fsb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 	sector_t		block = XFS_BB_TO_FSBT(mp, sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 	return blkdev_issue_zeroout(target->bt_bdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 		block << (mp->m_super->s_blocksize_bits - 9),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 		count_fsb << (mp->m_super->s_blocksize_bits - 9),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 		GFP_NOFS, 0);
^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) #ifdef CONFIG_XFS_RT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) xfs_bmap_rtalloc(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	struct xfs_bmalloca	*ap)	/* bmap alloc argument struct */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	int		error;		/* error return value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	xfs_mount_t	*mp;		/* mount point structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 	xfs_extlen_t	prod = 0;	/* product factor for allocators */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	xfs_extlen_t	mod = 0;	/* product factor for allocators */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	xfs_extlen_t	ralen = 0;	/* realtime allocation length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 	xfs_extlen_t	align;		/* minimum allocation alignment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	xfs_rtblock_t	rtb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	mp = ap->ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 	align = xfs_get_extsz_hint(ap->ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	prod = align / mp->m_sb.sb_rextsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 					align, 1, ap->eof, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 					ap->conv, &ap->offset, &ap->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	ASSERT(ap->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 	ASSERT(ap->length % mp->m_sb.sb_rextsize == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	 * If the offset & length are not perfectly aligned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	 * then kill prod, it will just get us in trouble.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	div_u64_rem(ap->offset, align, &mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 	if (mod || ap->length % align)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 		prod = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	 * Set ralen to be the actual requested length in rtextents.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	ralen = ap->length / mp->m_sb.sb_rextsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	 * If the old value was close enough to MAXEXTLEN that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	 * we rounded up to it, cut it back so it's valid again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	 * Note that if it's a really large request (bigger than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	 * MAXEXTLEN), we don't hear about that number, and can't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	 * adjust the starting point to match it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 		ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	 * Lock out modifications to both the RT bitmap and summary inodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	xfs_trans_ijoin(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	xfs_trans_ijoin(ap->tp, mp->m_rsumip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	 * If it's an allocation to an empty file at offset 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	 * pick an extent that will space things out in the rt area.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	if (ap->eof && ap->offset == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 		xfs_rtblock_t rtx; /* realtime extent no */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 		error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 		ap->blkno = rtx * mp->m_sb.sb_rextsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 		ap->blkno = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	xfs_bmap_adjacent(ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	 * Realtime allocation, done through xfs_rtallocate_extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	do_div(ap->blkno, mp->m_sb.sb_rextsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	rtb = ap->blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	ap->length = ralen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, ap->length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 				&ralen, ap->wasdel, prod, &rtb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	ap->blkno = rtb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	if (ap->blkno != NULLFSBLOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 		ap->blkno *= mp->m_sb.sb_rextsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 		ralen *= mp->m_sb.sb_rextsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 		ap->length = ralen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 		ap->ip->i_d.di_nblocks += ralen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 		xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 		if (ap->wasdel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 			ap->ip->i_delayed_blks -= ralen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 		 * Adjust the disk quota also. This was reserved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 		 * earlier.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 		xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 			ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 					XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 		ap->length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) #endif /* CONFIG_XFS_RT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176)  * Extent tree block counting routines.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180)  * Count leaf blocks given a range of extent records.  Delayed allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181)  * extents are not counted towards the totals.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) xfs_extnum_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) xfs_bmap_count_leaves(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	struct xfs_ifork	*ifp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	xfs_filblks_t		*count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	struct xfs_iext_cursor	icur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	struct xfs_bmbt_irec	got;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	xfs_extnum_t		numrecs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	for_each_xfs_iext(ifp, &icur, &got) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 		if (!isnullstartblock(got.br_startblock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 			*count += got.br_blockcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 			numrecs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	return numrecs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203)  * Count fsblocks of the given fork.  Delayed allocation extents are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204)  * not counted towards the totals.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) xfs_bmap_count_blocks(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 	struct xfs_trans	*tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	int			whichfork,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 	xfs_extnum_t		*nextents,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 	xfs_filblks_t		*count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	struct xfs_mount	*mp = ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	struct xfs_ifork	*ifp = XFS_IFORK_PTR(ip, whichfork);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	struct xfs_btree_cur	*cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	xfs_extlen_t		btblocks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	*nextents = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	*count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	if (!ifp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	switch (ifp->if_format) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	case XFS_DINODE_FMT_BTREE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 		if (!(ifp->if_flags & XFS_IFEXTENTS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 			error = xfs_iread_extents(tp, ip, whichfork);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 				return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 		cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 		error = xfs_btree_count_blocks(cur, &btblocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 		xfs_btree_del_cursor(cur, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 		 * xfs_btree_count_blocks includes the root block contained in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 		 * the inode fork in @btblocks, so subtract one because we're
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 		 * only interested in allocated disk blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 		*count += btblocks - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 		/* fall through */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	case XFS_DINODE_FMT_EXTENTS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 		*nextents = xfs_bmap_count_leaves(ifp, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) xfs_getbmap_report_one(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	struct getbmapx		*bmv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	struct kgetbmap		*out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	int64_t			bmv_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	struct xfs_bmbt_irec	*got)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	struct kgetbmap		*p = out + bmv->bmv_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	bool			shared = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	error = xfs_reflink_trim_around_shared(ip, got, &shared);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	if (isnullstartblock(got->br_startblock) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	    got->br_startblock == DELAYSTARTBLOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 		 * Delalloc extents that start beyond EOF can occur due to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 		 * speculative EOF allocation when the delalloc extent is larger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 		 * than the largest freespace extent at conversion time.  These
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 		 * extents cannot be converted by data writeback, so can exist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 		 * here even if we are not supposed to be finding delalloc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 		 * extents.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 		if (got->br_startoff < XFS_B_TO_FSB(ip->i_mount, XFS_ISIZE(ip)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 			ASSERT((bmv->bmv_iflags & BMV_IF_DELALLOC) != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 		p->bmv_oflags |= BMV_OF_DELALLOC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 		p->bmv_block = -2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 		p->bmv_block = xfs_fsb_to_db(ip, got->br_startblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	if (got->br_state == XFS_EXT_UNWRITTEN &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	    (bmv->bmv_iflags & BMV_IF_PREALLOC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 		p->bmv_oflags |= BMV_OF_PREALLOC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	if (shared)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 		p->bmv_oflags |= BMV_OF_SHARED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	p->bmv_offset = XFS_FSB_TO_BB(ip->i_mount, got->br_startoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	p->bmv_length = XFS_FSB_TO_BB(ip->i_mount, got->br_blockcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	bmv->bmv_offset = p->bmv_offset + p->bmv_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	bmv->bmv_length = max(0LL, bmv_end - bmv->bmv_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	bmv->bmv_entries++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) xfs_getbmap_report_hole(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	struct getbmapx		*bmv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	struct kgetbmap		*out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	int64_t			bmv_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	xfs_fileoff_t		bno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	xfs_fileoff_t		end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	struct kgetbmap		*p = out + bmv->bmv_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	if (bmv->bmv_iflags & BMV_IF_NO_HOLES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	p->bmv_block = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	p->bmv_offset = XFS_FSB_TO_BB(ip->i_mount, bno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	p->bmv_length = XFS_FSB_TO_BB(ip->i_mount, end - bno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	bmv->bmv_offset = p->bmv_offset + p->bmv_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	bmv->bmv_length = max(0LL, bmv_end - bmv->bmv_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	bmv->bmv_entries++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) static inline bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) xfs_getbmap_full(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	struct getbmapx		*bmv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	return bmv->bmv_length == 0 || bmv->bmv_entries >= bmv->bmv_count - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) static bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) xfs_getbmap_next_rec(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	struct xfs_bmbt_irec	*rec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	xfs_fileoff_t		total_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	xfs_fileoff_t		end = rec->br_startoff + rec->br_blockcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	if (end == total_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	rec->br_startoff += rec->br_blockcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	if (!isnullstartblock(rec->br_startblock) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	    rec->br_startblock != DELAYSTARTBLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 		rec->br_startblock += rec->br_blockcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	rec->br_blockcount = total_end - end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356)  * Get inode's extents as described in bmv, and format for output.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357)  * Calls formatter to fill the user's buffer until all extents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358)  * are mapped, until the passed-in bmv->bmv_count slots have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359)  * been filled, or until the formatter short-circuits the loop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360)  * if it is tracking filled-in extents on its own.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) int						/* error code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) xfs_getbmap(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	struct getbmapx		*bmv,		/* user bmap structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	struct kgetbmap		*out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	struct xfs_mount	*mp = ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	int			iflags = bmv->bmv_iflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	int			whichfork, lock, error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	int64_t			bmv_end, max_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	xfs_fileoff_t		bno, first_bno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	struct xfs_ifork	*ifp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	struct xfs_bmbt_irec	got, rec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	xfs_filblks_t		len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	struct xfs_iext_cursor	icur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	if (bmv->bmv_iflags & ~BMV_IF_VALID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) #ifndef DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	/* Only allow CoW fork queries if we're debugging. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	if (iflags & BMV_IF_COWFORK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	if ((iflags & BMV_IF_ATTRFORK) && (iflags & BMV_IF_COWFORK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	if (bmv->bmv_length < -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	bmv->bmv_entries = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	if (bmv->bmv_length == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	if (iflags & BMV_IF_ATTRFORK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 		whichfork = XFS_ATTR_FORK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	else if (iflags & BMV_IF_COWFORK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 		whichfork = XFS_COW_FORK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 		whichfork = XFS_DATA_FORK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	ifp = XFS_IFORK_PTR(ip, whichfork);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	xfs_ilock(ip, XFS_IOLOCK_SHARED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	switch (whichfork) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	case XFS_ATTR_FORK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 		if (!XFS_IFORK_Q(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 			goto out_unlock_iolock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 		max_len = 1LL << 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 		lock = xfs_ilock_attr_map_shared(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	case XFS_COW_FORK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 		/* No CoW fork? Just return */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 		if (!ifp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 			goto out_unlock_iolock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 		if (xfs_get_cowextsz_hint(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 			max_len = mp->m_super->s_maxbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 			max_len = XFS_ISIZE(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 		lock = XFS_ILOCK_SHARED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 		xfs_ilock(ip, lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	case XFS_DATA_FORK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 		if (!(iflags & BMV_IF_DELALLOC) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 		    (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_d.di_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 			error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 				goto out_unlock_iolock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 			 * Even after flushing the inode, there can still be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 			 * delalloc blocks on the inode beyond EOF due to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 			 * speculative preallocation.  These are not removed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 			 * until the release function is called or the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 			 * is inactivated.  Hence we cannot assert here that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 			 * ip->i_delayed_blks == 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 		if (xfs_get_extsz_hint(ip) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 		    (ip->i_d.di_flags &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 		     (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 			max_len = mp->m_super->s_maxbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 			max_len = XFS_ISIZE(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 		lock = xfs_ilock_data_map_shared(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 		break;
^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) 	switch (ifp->if_format) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	case XFS_DINODE_FMT_EXTENTS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	case XFS_DINODE_FMT_BTREE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	case XFS_DINODE_FMT_LOCAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 		/* Local format inode forks report no extents. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 		goto out_unlock_ilock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 		error = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 		goto out_unlock_ilock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	if (bmv->bmv_length == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 		max_len = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, max_len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 		bmv->bmv_length = max(0LL, max_len - bmv->bmv_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	bmv_end = bmv->bmv_offset + bmv->bmv_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	first_bno = bno = XFS_BB_TO_FSBT(mp, bmv->bmv_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	len = XFS_BB_TO_FSB(mp, bmv->bmv_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	if (!(ifp->if_flags & XFS_IFEXTENTS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 		error = xfs_iread_extents(NULL, ip, whichfork);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 			goto out_unlock_ilock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 		 * Report a whole-file hole if the delalloc flag is set to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 		 * stay compatible with the old implementation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 		if (iflags & BMV_IF_DELALLOC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 			xfs_getbmap_report_hole(ip, bmv, out, bmv_end, bno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 					XFS_B_TO_FSB(mp, XFS_ISIZE(ip)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 		goto out_unlock_ilock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	while (!xfs_getbmap_full(bmv)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 		xfs_trim_extent(&got, first_bno, len);
^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) 		 * Report an entry for a hole if this extent doesn't directly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 		 * follow the previous one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 		if (got.br_startoff > bno) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 			xfs_getbmap_report_hole(ip, bmv, out, bmv_end, bno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 					got.br_startoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 			if (xfs_getbmap_full(bmv))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 				break;
^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) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 		 * In order to report shared extents accurately, we report each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 		 * distinct shared / unshared part of a single bmbt record with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 		 * an individual getbmapx record.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 		bno = got.br_startoff + got.br_blockcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 		rec = got;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 			error = xfs_getbmap_report_one(ip, bmv, out, bmv_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 					&rec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 			if (error || xfs_getbmap_full(bmv))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 				goto out_unlock_ilock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 		} while (xfs_getbmap_next_rec(&rec, bno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 		if (!xfs_iext_next_extent(ifp, &icur, &got)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 			xfs_fileoff_t	end = XFS_B_TO_FSB(mp, XFS_ISIZE(ip));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 			out[bmv->bmv_entries - 1].bmv_oflags |= BMV_OF_LAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 			if (whichfork != XFS_ATTR_FORK && bno < end &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 			    !xfs_getbmap_full(bmv)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 				xfs_getbmap_report_hole(ip, bmv, out, bmv_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 						bno, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 		if (bno >= first_bno + len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 			break;
^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) out_unlock_ilock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	xfs_iunlock(ip, lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) out_unlock_iolock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	xfs_iunlock(ip, XFS_IOLOCK_SHARED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544)  * Dead simple method of punching delalyed allocation blocks from a range in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545)  * the inode.  This will always punch out both the start and end blocks, even
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546)  * if the ranges only partially overlap them, so it is up to the caller to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547)  * ensure that partial blocks are not passed in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) xfs_bmap_punch_delalloc_range(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	xfs_fileoff_t		start_fsb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	xfs_fileoff_t		length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	struct xfs_ifork	*ifp = &ip->i_df;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	xfs_fileoff_t		end_fsb = start_fsb + length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	struct xfs_bmbt_irec	got, del;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	struct xfs_iext_cursor	icur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	int			error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	ASSERT(ifp->if_flags & XFS_IFEXTENTS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	xfs_ilock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	while (got.br_startoff + got.br_blockcount > start_fsb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 		del = got;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 		xfs_trim_extent(&del, start_fsb, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 		 * A delete can push the cursor forward. Step back to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 		 * previous extent on non-delalloc or extents outside the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 		 * target range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 		if (!del.br_blockcount ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 		    !isnullstartblock(del.br_startblock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 			if (!xfs_iext_prev_extent(ifp, &icur, &got))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 		error = xfs_bmap_del_extent_delay(ip, XFS_DATA_FORK, &icur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 						  &got, &del);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 		if (error || !xfs_iext_get_extent(ifp, &icur, &got))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595)  * Test whether it is appropriate to check an inode for and free post EOF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596)  * blocks. The 'force' parameter determines whether we should also consider
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597)  * regular files that are marked preallocated or append-only.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) xfs_can_free_eofblocks(struct xfs_inode *ip, bool force)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	/* prealloc/delalloc exists only on regular files */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	if (!S_ISREG(VFS_I(ip)->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	 * Zero sized files with no cached pages and delalloc blocks will not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	 * have speculative prealloc/delalloc blocks to remove.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	if (VFS_I(ip)->i_size == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	    VFS_I(ip)->i_mapping->nrpages == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	    ip->i_delayed_blks == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	/* If we haven't read in the extent list, then don't do it now. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	if (!(ip->i_df.if_flags & XFS_IFEXTENTS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	 * Do not free real preallocated or append-only files unless the file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	 * has delalloc blocks and we are forced to remove them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	if (ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 		if (!force || ip->i_delayed_blks == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631)  * This is called to free any blocks beyond eof. The caller must hold
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632)  * IOLOCK_EXCL unless we are in the inode reclaim path and have the only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633)  * reference to the inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) xfs_free_eofblocks(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	struct xfs_inode	*ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	struct xfs_trans	*tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	xfs_fileoff_t		end_fsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	xfs_fileoff_t		last_fsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	xfs_filblks_t		map_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	int			nimaps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	struct xfs_bmbt_irec	imap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	struct xfs_mount	*mp = ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	 * Figure out if there are any blocks beyond the end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	 * of the file.  If not, then there is nothing to do.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_ISIZE(ip));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	last_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	if (last_fsb <= end_fsb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	map_len = last_fsb - end_fsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	nimaps = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	xfs_ilock(ip, XFS_ILOCK_SHARED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	error = xfs_bmapi_read(ip, end_fsb, map_len, &imap, &nimaps, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	xfs_iunlock(ip, XFS_ILOCK_SHARED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	 * If there are blocks after the end of file, truncate the file to its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	 * current size to free them up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	if (!error && (nimaps != 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	    (imap.br_startblock != HOLESTARTBLOCK ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	     ip->i_delayed_blks)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 		 * Attach the dquots to the inode up front.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 		error = xfs_qm_dqattach(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 		/* wait on dio to ensure i_size has settled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 		inode_dio_wait(VFS_I(ip));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 				&tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 		if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 			ASSERT(XFS_FORCED_SHUTDOWN(mp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 		xfs_ilock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 		xfs_trans_ijoin(tp, ip, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 		 * Do not update the on-disk file size.  If we update the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 		 * on-disk file size and then the system crashes before the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 		 * contents of the file are flushed to disk then the files
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 		 * may be full of holes (ie NULL files bug).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 		error = xfs_itruncate_extents_flags(&tp, ip, XFS_DATA_FORK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 					XFS_ISIZE(ip), XFS_BMAPI_NODISCARD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 		if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 			 * If we get an error at this point we simply don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 			 * bother truncating the file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 			xfs_trans_cancel(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 			error = xfs_trans_commit(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 			if (!error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 				xfs_inode_clear_eofblocks_tag(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 		xfs_iunlock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) xfs_alloc_file_space(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	xfs_off_t		offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	xfs_off_t		len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	int			alloc_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	xfs_mount_t		*mp = ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	xfs_off_t		count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	xfs_filblks_t		allocated_fsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	xfs_filblks_t		allocatesize_fsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	xfs_extlen_t		extsz, temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	xfs_fileoff_t		startoffset_fsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	xfs_fileoff_t		endoffset_fsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	int			nimaps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	int			quota_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	int			rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	xfs_trans_t		*tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	xfs_bmbt_irec_t		imaps[1], *imapp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	uint			qblocks, resblks, resrtextents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	trace_xfs_alloc_file_space(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	if (XFS_FORCED_SHUTDOWN(mp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	error = xfs_qm_dqattach(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	if (len <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 	rt = XFS_IS_REALTIME_INODE(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	extsz = xfs_get_extsz_hint(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	count = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	imapp = &imaps[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	nimaps = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	startoffset_fsb	= XFS_B_TO_FSBT(mp, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	endoffset_fsb = XFS_B_TO_FSB(mp, offset + count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	allocatesize_fsb = endoffset_fsb - startoffset_fsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	 * Allocate file space until done or until there is an error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	while (allocatesize_fsb && !error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 		xfs_fileoff_t	s, e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 		 * Determine space reservations for data/realtime.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 		if (unlikely(extsz)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 			s = startoffset_fsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 			do_div(s, extsz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 			s *= extsz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 			e = startoffset_fsb + allocatesize_fsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 			div_u64_rem(startoffset_fsb, extsz, &temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 			if (temp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 				e += temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 			div_u64_rem(e, extsz, &temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 			if (temp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 				e += extsz - temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 			s = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 			e = allocatesize_fsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 		 * The transaction reservation is limited to a 32-bit block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 		 * count, hence we need to limit the number of blocks we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 		 * trying to reserve to avoid an overflow. We can't allocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 		 * more than @nimaps extents, and an extent is limited on disk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 		 * to MAXEXTLEN (21 bits), so use that to enforce the limit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 		resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 		if (unlikely(rt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 			resrtextents = qblocks = resblks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 			resrtextents /= mp->m_sb.sb_rextsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 			resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 			quota_flag = XFS_QMOPT_RES_RTBLKS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 			resrtextents = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 			resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 			quota_flag = XFS_QMOPT_RES_REGBLKS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 		 * Allocate and setup the transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 				resrtextents, 0, &tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 		 * Check for running out of space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 		if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 			 * Free the transaction structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 			ASSERT(error == -ENOSPC || XFS_FORCED_SHUTDOWN(mp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 		xfs_ilock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 		error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 						      0, quota_flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 			goto error1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 		xfs_trans_ijoin(tp, ip, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 		error = xfs_bmapi_write(tp, ip, startoffset_fsb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 					allocatesize_fsb, alloc_type, 0, imapp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 					&nimaps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 			goto error0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 		 * Complete the transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 		error = xfs_trans_commit(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 		xfs_iunlock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 		allocated_fsb = imapp->br_blockcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		if (nimaps == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 			error = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 		startoffset_fsb += allocated_fsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 		allocatesize_fsb -= allocated_fsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) error0:	/* unlock inode, unreserve quota blocks, cancel trans */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) error1:	/* Just cancel transaction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	xfs_trans_cancel(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) xfs_unmap_extent(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	xfs_fileoff_t		startoffset_fsb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	xfs_filblks_t		len_fsb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	int			*done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	struct xfs_mount	*mp = ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	struct xfs_trans	*tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	uint			resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 		ASSERT(error == -ENOSPC || XFS_FORCED_SHUTDOWN(mp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	xfs_ilock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	error = xfs_trans_reserve_quota(tp, mp, ip->i_udquot, ip->i_gdquot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 			ip->i_pdquot, resblks, 0, XFS_QMOPT_RES_REGBLKS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 		goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	xfs_trans_ijoin(tp, ip, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	error = xfs_bunmapi(tp, ip, startoffset_fsb, len_fsb, 0, 2, done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 		goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	error = xfs_trans_commit(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) out_trans_cancel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	xfs_trans_cancel(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) /* Caller must first wait for the completion of any pending DIOs if required. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) xfs_flush_unmap_range(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	xfs_off_t		offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	xfs_off_t		len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	struct xfs_mount	*mp = ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	struct inode		*inode = VFS_I(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	xfs_off_t		rounding, start, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	rounding = max_t(xfs_off_t, 1 << mp->m_sb.sb_blocklog, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	start = round_down(offset, rounding);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	end = round_up(offset + len, rounding) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	error = filemap_write_and_wait_range(inode->i_mapping, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	truncate_pagecache_range(inode, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) xfs_free_file_space(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	xfs_off_t		offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	xfs_off_t		len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	struct xfs_mount	*mp = ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	xfs_fileoff_t		startoffset_fsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	xfs_fileoff_t		endoffset_fsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	int			done = 0, error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 	trace_xfs_free_file_space(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	error = xfs_qm_dqattach(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	if (len <= 0)	/* if nothing being freed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	startoffset_fsb = XFS_B_TO_FSB(mp, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	/* We can only free complete realtime extents. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	if (XFS_IS_REALTIME_INODE(ip) && mp->m_sb.sb_rextsize > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 		startoffset_fsb = roundup_64(startoffset_fsb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 					     mp->m_sb.sb_rextsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 		endoffset_fsb = rounddown_64(endoffset_fsb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 					     mp->m_sb.sb_rextsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	 * Need to zero the stuff we're not freeing, on disk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	if (endoffset_fsb > startoffset_fsb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 		while (!done) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 			error = xfs_unmap_extent(ip, startoffset_fsb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 					endoffset_fsb - startoffset_fsb, &done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 				return error;
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	 * Now that we've unmap all full blocks we'll have to zero out any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 	 * partial block at the beginning and/or end.  iomap_zero_range is smart
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	 * enough to skip any holes, including those we just created, but we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	 * must take care not to zero beyond EOF and enlarge i_size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	if (offset >= XFS_ISIZE(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	if (offset + len > XFS_ISIZE(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 		len = XFS_ISIZE(ip) - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	error = iomap_zero_range(VFS_I(ip), offset, len, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 			&xfs_buffered_write_iomap_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	 * If we zeroed right up to EOF and EOF straddles a page boundary we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	 * must make sure that the post-EOF area is also zeroed because the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	 * page could be mmap'd and iomap_zero_range doesn't do that for us.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	 * Writeback of the eof page will do this, albeit clumsily.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	if (offset + len >= XFS_ISIZE(ip) && offset_in_page(offset + len) > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 		error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 				round_down(offset + len, PAGE_SIZE), LLONG_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) xfs_prepare_shift(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	loff_t			offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	struct xfs_mount	*mp = ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	 * Trim eofblocks to avoid shifting uninitialized post-eof preallocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	 * into the accessible region of the file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	if (xfs_can_free_eofblocks(ip, true)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 		error = xfs_free_eofblocks(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 			return error;
^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) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	 * Shift operations must stabilize the start block offset boundary along
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	 * with the full range of the operation. If we don't, a COW writeback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	 * completion could race with an insert, front merge with the start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	 * extent (after split) during the shift and corrupt the file. Start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	 * with the block just prior to the start to stabilize the boundary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	offset = round_down(offset, 1 << mp->m_sb.sb_blocklog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	if (offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 		offset -= (1 << mp->m_sb.sb_blocklog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	 * Writeback and invalidate cache for the remainder of the file as we're
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	 * about to shift down every extent from offset to EOF.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	error = xfs_flush_unmap_range(ip, offset, XFS_ISIZE(ip));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	 * Clean out anything hanging around in the cow fork now that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	 * we've flushed all the dirty data out to disk to avoid having
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	 * CoW extents at the wrong offsets.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	if (xfs_inode_has_cow_data(ip)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 		error = xfs_reflink_cancel_cow_range(ip, offset, NULLFILEOFF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 				true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051)  * xfs_collapse_file_space()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052)  *	This routine frees disk space and shift extent for the given file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053)  *	The first thing we do is to free data blocks in the specified range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054)  *	by calling xfs_free_file_space(). It would also sync dirty data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055)  *	and invalidate page cache over the region on which collapse range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056)  *	is working. And Shift extent records to the left to cover a hole.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057)  * RETURNS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058)  *	0 on success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059)  *	errno on error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) xfs_collapse_file_space(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	xfs_off_t		offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	xfs_off_t		len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 	struct xfs_mount	*mp = ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	struct xfs_trans	*tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	xfs_fileoff_t		next_fsb = XFS_B_TO_FSB(mp, offset + len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	xfs_fileoff_t		shift_fsb = XFS_B_TO_FSB(mp, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 	bool			done = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	trace_xfs_collapse_file_space(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	error = xfs_free_file_space(ip, offset, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	error = xfs_prepare_shift(ip, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	xfs_ilock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	xfs_trans_ijoin(tp, ip, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	while (!done) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 		error = xfs_bmap_collapse_extents(tp, ip, &next_fsb, shift_fsb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 				&done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 			goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 		if (done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 		/* finish any deferred frees and roll the transaction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 		error = xfs_defer_finish(&tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 			goto out_trans_cancel;
^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) 	error = xfs_trans_commit(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) out_trans_cancel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	xfs_trans_cancel(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120)  * xfs_insert_file_space()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121)  *	This routine create hole space by shifting extents for the given file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122)  *	The first thing we do is to sync dirty data and invalidate page cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123)  *	over the region on which insert range is working. And split an extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124)  *	to two extents at given offset by calling xfs_bmap_split_extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125)  *	And shift all extent records which are laying between [offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126)  *	last allocated extent] to the right to reserve hole range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127)  * RETURNS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128)  *	0 on success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129)  *	errno on error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) xfs_insert_file_space(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	loff_t			offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	loff_t			len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	struct xfs_mount	*mp = ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	struct xfs_trans	*tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	xfs_fileoff_t		stop_fsb = XFS_B_TO_FSB(mp, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	xfs_fileoff_t		next_fsb = NULLFSBLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	xfs_fileoff_t		shift_fsb = XFS_B_TO_FSB(mp, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	bool			done = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	trace_xfs_insert_file_space(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	error = xfs_bmap_can_insert_extents(ip, stop_fsb, shift_fsb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	error = xfs_prepare_shift(ip, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 			XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 	xfs_ilock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	xfs_trans_ijoin(tp, ip, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	 * The extent shifting code works on extent granularity. So, if stop_fsb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 	 * is not the starting block of extent, we need to split the extent at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 	 * stop_fsb.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	error = xfs_bmap_split_extent(tp, ip, stop_fsb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 		goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 		error = xfs_defer_finish(&tp);
^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) 		error = xfs_bmap_insert_extents(tp, ip, &next_fsb, shift_fsb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 				&done, stop_fsb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 			goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 	} while (!done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 	error = xfs_trans_commit(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) out_trans_cancel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 	xfs_trans_cancel(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197)  * We need to check that the format of the data fork in the temporary inode is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198)  * valid for the target inode before doing the swap. This is not a problem with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199)  * attr1 because of the fixed fork offset, but attr2 has a dynamically sized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200)  * data fork depending on the space the attribute fork is taking so we can get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201)  * invalid formats on the target inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203)  * E.g. target has space for 7 extents in extent format, temp inode only has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204)  * space for 6.  If we defragment down to 7 extents, then the tmp format is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205)  * btree, but when swapped it needs to be in extent format. Hence we can't just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206)  * blindly swap data forks on attr2 filesystems.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208)  * Note that we check the swap in both directions so that we don't end up with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209)  * a corrupt temporary inode, either.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211)  * Note that fixing the way xfs_fsr sets up the attribute fork in the source
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212)  * inode will prevent this situation from occurring, so all we do here is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213)  * reject and log the attempt. basically we are putting the responsibility on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214)  * userspace to get this right.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) xfs_swap_extents_check_format(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	struct xfs_inode	*ip,	/* target inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 	struct xfs_inode	*tip)	/* tmp inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	struct xfs_ifork	*ifp = &ip->i_df;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	struct xfs_ifork	*tifp = &tip->i_df;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 	/* User/group/project quota ids must match if quotas are enforced. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	if (XFS_IS_QUOTA_ON(ip->i_mount) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	    (!uid_eq(VFS_I(ip)->i_uid, VFS_I(tip)->i_uid) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 	     !gid_eq(VFS_I(ip)->i_gid, VFS_I(tip)->i_gid) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 	     ip->i_d.di_projid != tip->i_d.di_projid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	/* Should never get a local format */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 	if (ifp->if_format == XFS_DINODE_FMT_LOCAL ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 	    tifp->if_format == XFS_DINODE_FMT_LOCAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	 * if the target inode has less extents that then temporary inode then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 	 * why did userspace call us?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	if (ifp->if_nextents < tifp->if_nextents)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	 * If we have to use the (expensive) rmap swap method, we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 	 * handle any number of extents and any format.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 	if (xfs_sb_version_hasrmapbt(&ip->i_mount->m_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 	 * if the target inode is in extent form and the temp inode is in btree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 	 * form then we will end up with the target inode in the wrong format
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	 * as we already know there are less extents in the temp inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	if (ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	    tifp->if_format == XFS_DINODE_FMT_BTREE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 	/* Check temp in extent form to max in target */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 	if (tifp->if_format == XFS_DINODE_FMT_EXTENTS &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	    tifp->if_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 	/* Check target in extent form to max in temp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 	if (ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 	    ifp->if_nextents > XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 	 * If we are in a btree format, check that the temp root block will fit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 	 * in the target and that it has enough extents to be in btree format
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 	 * in the target.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	 * Note that we have to be careful to allow btree->extent conversions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 	 * (a common defrag case) which will occur when the temp inode is in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 	 * extent format...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 	if (tifp->if_format == XFS_DINODE_FMT_BTREE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 		if (XFS_IFORK_Q(ip) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 		    XFS_BMAP_BMDR_SPACE(tifp->if_broot) > XFS_IFORK_BOFF(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 		if (tifp->if_nextents <= XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 	/* Reciprocal target->temp btree format checks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 	if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 		if (XFS_IFORK_Q(tip) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 		    XFS_BMAP_BMDR_SPACE(ip->i_df.if_broot) > XFS_IFORK_BOFF(tip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 		if (ifp->if_nextents <= XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) xfs_swap_extent_flush(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	struct xfs_inode	*ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	int	error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	truncate_pagecache_range(VFS_I(ip), 0, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 	/* Verify O_DIRECT for ftmp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 	if (VFS_I(ip)->i_mapping->nrpages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 	return 0;
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316)  * Move extents from one file to another, when rmap is enabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) xfs_swap_extent_rmap(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 	struct xfs_trans		**tpp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 	struct xfs_inode		*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 	struct xfs_inode		*tip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 	struct xfs_trans		*tp = *tpp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 	struct xfs_bmbt_irec		irec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 	struct xfs_bmbt_irec		uirec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 	struct xfs_bmbt_irec		tirec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 	xfs_fileoff_t			offset_fsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 	xfs_fileoff_t			end_fsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 	xfs_filblks_t			count_fsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 	int				error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 	xfs_filblks_t			ilen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 	xfs_filblks_t			rlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	int				nimaps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 	uint64_t			tip_flags2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 	 * If the source file has shared blocks, we must flag the donor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 	 * file as having shared blocks so that we get the shared-block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 	 * rmap functions when we go to fix up the rmaps.  The flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 	 * will be switch for reals later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 	tip_flags2 = tip->i_d.di_flags2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 	if (ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 		tip->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 	offset_fsb = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 	end_fsb = XFS_B_TO_FSB(ip->i_mount, i_size_read(VFS_I(ip)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 	count_fsb = (xfs_filblks_t)(end_fsb - offset_fsb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 	while (count_fsb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 		/* Read extent from the donor file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 		nimaps = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 		error = xfs_bmapi_read(tip, offset_fsb, count_fsb, &tirec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 				&nimaps, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 		ASSERT(nimaps == 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 		ASSERT(tirec.br_startblock != DELAYSTARTBLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 		trace_xfs_swap_extent_rmap_remap(tip, &tirec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 		ilen = tirec.br_blockcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 		/* Unmap the old blocks in the source file. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 		while (tirec.br_blockcount) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 			ASSERT(tp->t_firstblock == NULLFSBLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 			trace_xfs_swap_extent_rmap_remap_piece(tip, &tirec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 			/* Read extent from the source file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 			nimaps = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 			error = xfs_bmapi_read(ip, tirec.br_startoff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 					tirec.br_blockcount, &irec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 					&nimaps, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 			ASSERT(nimaps == 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 			ASSERT(tirec.br_startoff == irec.br_startoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 			trace_xfs_swap_extent_rmap_remap_piece(ip, &irec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 			/* Trim the extent. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 			uirec = tirec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 			uirec.br_blockcount = rlen = min_t(xfs_filblks_t,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 					tirec.br_blockcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 					irec.br_blockcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 			trace_xfs_swap_extent_rmap_remap_piece(tip, &uirec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 			/* Remove the mapping from the donor file. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 			xfs_bmap_unmap_extent(tp, tip, &uirec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 			/* Remove the mapping from the source file. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 			xfs_bmap_unmap_extent(tp, ip, &irec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 			/* Map the donor file's blocks into the source file. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 			xfs_bmap_map_extent(tp, ip, &uirec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 			/* Map the source file's blocks into the donor file. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 			xfs_bmap_map_extent(tp, tip, &irec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 			error = xfs_defer_finish(tpp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 			tp = *tpp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 			tirec.br_startoff += rlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 			if (tirec.br_startblock != HOLESTARTBLOCK &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 			    tirec.br_startblock != DELAYSTARTBLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 				tirec.br_startblock += rlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 			tirec.br_blockcount -= rlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 		/* Roll on... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 		count_fsb -= ilen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 		offset_fsb += ilen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 	tip->i_d.di_flags2 = tip_flags2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 	trace_xfs_swap_extent_rmap_error(ip, error, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 	tip->i_d.di_flags2 = tip_flags2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) /* Swap the extents of two files by swapping data forks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) xfs_swap_extent_forks(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 	struct xfs_trans	*tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 	struct xfs_inode	*tip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 	int			*src_log_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 	int			*target_log_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 	xfs_filblks_t		aforkblks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 	xfs_filblks_t		taforkblks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 	xfs_extnum_t		junk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 	uint64_t		tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 	 * Count the number of extended attribute blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 	if (XFS_IFORK_Q(ip) && ip->i_afp->if_nextents > 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 	    ip->i_afp->if_format != XFS_DINODE_FMT_LOCAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 		error = xfs_bmap_count_blocks(tp, ip, XFS_ATTR_FORK, &junk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 				&aforkblks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 	if (XFS_IFORK_Q(tip) && tip->i_afp->if_nextents > 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	    tip->i_afp->if_format != XFS_DINODE_FMT_LOCAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 		error = xfs_bmap_count_blocks(tp, tip, XFS_ATTR_FORK, &junk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 				&taforkblks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	 * Btree format (v3) inodes have the inode number stamped in the bmbt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 	 * block headers. We can't start changing the bmbt blocks until the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	 * inode owner change is logged so recovery does the right thing in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 	 * event of a crash. Set the owner change log flags now and leave the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 	 * bmbt scan as the last step.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 	if (xfs_sb_version_has_v3inode(&ip->i_mount->m_sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 		if (ip->i_df.if_format == XFS_DINODE_FMT_BTREE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 			(*target_log_flags) |= XFS_ILOG_DOWNER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 		if (tip->i_df.if_format == XFS_DINODE_FMT_BTREE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 			(*src_log_flags) |= XFS_ILOG_DOWNER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 	 * Swap the data forks of the inodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 	swap(ip->i_df, tip->i_df);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 	 * Fix the on-disk inode values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 	tmp = (uint64_t)ip->i_d.di_nblocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 	ip->i_d.di_nblocks = tip->i_d.di_nblocks - taforkblks + aforkblks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	tip->i_d.di_nblocks = tmp + taforkblks - aforkblks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	 * The extents in the source inode could still contain speculative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 	 * preallocation beyond EOF (e.g. the file is open but not modified
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	 * while defrag is in progress). In that case, we need to copy over the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 	 * number of delalloc blocks the data fork in the source inode is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 	 * tracking beyond EOF so that when the fork is truncated away when the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 	 * temporary inode is unlinked we don't underrun the i_delayed_blks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 	 * counter on that inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 	ASSERT(tip->i_delayed_blks == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 	tip->i_delayed_blks = ip->i_delayed_blks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 	ip->i_delayed_blks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 	switch (ip->i_df.if_format) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 	case XFS_DINODE_FMT_EXTENTS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 		(*src_log_flags) |= XFS_ILOG_DEXT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 	case XFS_DINODE_FMT_BTREE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 		ASSERT(!xfs_sb_version_has_v3inode(&ip->i_mount->m_sb) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 		       (*src_log_flags & XFS_ILOG_DOWNER));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 		(*src_log_flags) |= XFS_ILOG_DBROOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 	switch (tip->i_df.if_format) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 	case XFS_DINODE_FMT_EXTENTS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 		(*target_log_flags) |= XFS_ILOG_DEXT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 	case XFS_DINODE_FMT_BTREE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 		(*target_log_flags) |= XFS_ILOG_DBROOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 		ASSERT(!xfs_sb_version_has_v3inode(&ip->i_mount->m_sb) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 		       (*target_log_flags & XFS_ILOG_DOWNER));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523)  * Fix up the owners of the bmbt blocks to refer to the current inode. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524)  * change owner scan attempts to order all modified buffers in the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525)  * transaction. In the event of ordered buffer failure, the offending buffer is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526)  * physically logged as a fallback and the scan returns -EAGAIN. We must roll
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527)  * the transaction in this case to replenish the fallback log reservation and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528)  * restart the scan. This process repeats until the scan completes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) xfs_swap_change_owner(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 	struct xfs_trans	**tpp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	struct xfs_inode	*tmpip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 	struct xfs_trans	*tp = *tpp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 		error = xfs_bmbt_change_owner(tp, ip, XFS_DATA_FORK, ip->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 					      NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 		/* success or fatal error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 		if (error != -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 		error = xfs_trans_roll(tpp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 		tp = *tpp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 		 * Redirty both inodes so they can relog and keep the log tail
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 		 * moving forward.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 		xfs_trans_ijoin(tp, ip, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 		xfs_trans_ijoin(tp, tmpip, 0);
^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) 		xfs_trans_log_inode(tp, tmpip, XFS_ILOG_CORE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 	} while (true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) xfs_swap_extents(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 	struct xfs_inode	*ip,	/* target inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 	struct xfs_inode	*tip,	/* tmp inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 	struct xfs_swapext	*sxp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 	struct xfs_mount	*mp = ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 	struct xfs_trans	*tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 	struct xfs_bstat	*sbp = &sxp->sx_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 	int			src_log_flags, target_log_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 	int			error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 	int			lock_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 	uint64_t		f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 	int			resblks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 	unsigned int		flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 	 * Lock the inodes against other IO, page faults and truncate to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	 * begin with.  Then we can ensure the inodes are flushed and have no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 	 * page cache safely. Once we have done this we can take the ilocks and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 	 * do the rest of the checks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 	lock_two_nondirectories(VFS_I(ip), VFS_I(tip));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 	lock_flags = XFS_MMAPLOCK_EXCL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 	xfs_lock_two_inodes(ip, XFS_MMAPLOCK_EXCL, tip, XFS_MMAPLOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 	/* Verify that both files have the same format */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 	if ((VFS_I(ip)->i_mode & S_IFMT) != (VFS_I(tip)->i_mode & S_IFMT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 		error = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 	/* Verify both files are either real-time or non-realtime */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 	if (XFS_IS_REALTIME_INODE(ip) != XFS_IS_REALTIME_INODE(tip)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 		error = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 		goto out_unlock;
^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) 	error = xfs_qm_dqattach(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 	error = xfs_qm_dqattach(tip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 	error = xfs_swap_extent_flush(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 	error = xfs_swap_extent_flush(tip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 	if (xfs_inode_has_cow_data(tip)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 		error = xfs_reflink_cancel_cow_range(tip, 0, NULLFILEOFF, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 	 * Extent "swapping" with rmap requires a permanent reservation and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 	 * a block reservation because it's really just a remap operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 	 * performed with log redo items!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 	if (xfs_sb_version_hasrmapbt(&mp->m_sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 		int		w = XFS_DATA_FORK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 		uint32_t	ipnext = ip->i_df.if_nextents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 		uint32_t	tipnext	= tip->i_df.if_nextents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 		 * Conceptually this shouldn't affect the shape of either bmbt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 		 * but since we atomically move extents one by one, we reserve
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 		 * enough space to rebuild both trees.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 		resblks = XFS_SWAP_RMAP_SPACE_RES(mp, ipnext, w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 		resblks +=  XFS_SWAP_RMAP_SPACE_RES(mp, tipnext, w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 		 * If either inode straddles a bmapbt block allocation boundary,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 		 * the rmapbt algorithm triggers repeated allocs and frees as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 		 * extents are remapped. This can exhaust the block reservation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 		 * prematurely and cause shutdown. Return freed blocks to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 		 * transaction reservation to counter this behavior.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 		flags |= XFS_TRANS_RES_FDBLKS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 				&tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 	 * Lock and join the inodes to the tansaction so that transaction commit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 	 * or cancel will unlock the inodes from this point onwards.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 	xfs_lock_two_inodes(ip, XFS_ILOCK_EXCL, tip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 	lock_flags |= XFS_ILOCK_EXCL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 	xfs_trans_ijoin(tp, ip, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 	xfs_trans_ijoin(tp, tip, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 	/* Verify all data are being swapped */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 	if (sxp->sx_offset != 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 	    sxp->sx_length != ip->i_d.di_size ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 	    sxp->sx_length != tip->i_d.di_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 		error = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 		goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 	trace_xfs_swap_extent_before(ip, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 	trace_xfs_swap_extent_before(tip, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 	/* check inode formats now that data is flushed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 	error = xfs_swap_extents_check_format(ip, tip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 		xfs_notice(mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 		    "%s: inode 0x%llx format is incompatible for exchanging.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 				__func__, ip->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 		goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 	 * Compare the current change & modify times with that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 	 * passed in.  If they differ, we abort this swap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 	 * This is the mechanism used to ensure the calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 	 * process that the file was not changed out from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 	 * under it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 	if ((sbp->bs_ctime.tv_sec != VFS_I(ip)->i_ctime.tv_sec) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 	    (sbp->bs_ctime.tv_nsec != VFS_I(ip)->i_ctime.tv_nsec) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 	    (sbp->bs_mtime.tv_sec != VFS_I(ip)->i_mtime.tv_sec) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 	    (sbp->bs_mtime.tv_nsec != VFS_I(ip)->i_mtime.tv_nsec)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 		error = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 		goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 	 * Note the trickiness in setting the log flags - we set the owner log
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 	 * flag on the opposite inode (i.e. the inode we are setting the new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 	 * owner to be) because once we swap the forks and log that, log
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 	 * recovery is going to see the fork as owned by the swapped inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 	 * not the pre-swapped inodes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 	src_log_flags = XFS_ILOG_CORE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 	target_log_flags = XFS_ILOG_CORE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 	if (xfs_sb_version_hasrmapbt(&mp->m_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 		error = xfs_swap_extent_rmap(&tp, ip, tip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 		error = xfs_swap_extent_forks(tp, ip, tip, &src_log_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 				&target_log_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 		goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 	/* Do we have to swap reflink flags? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 	if ((ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK) ^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 	    (tip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 		f = ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 		ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 		ip->i_d.di_flags2 |= tip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 		tip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 		tip->i_d.di_flags2 |= f & XFS_DIFLAG2_REFLINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 	/* Swap the cow forks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 	if (xfs_sb_version_hasreflink(&mp->m_sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 		ASSERT(!ip->i_cowfp ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 		       ip->i_cowfp->if_format == XFS_DINODE_FMT_EXTENTS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 		ASSERT(!tip->i_cowfp ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 		       tip->i_cowfp->if_format == XFS_DINODE_FMT_EXTENTS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 		swap(ip->i_cowfp, tip->i_cowfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 		if (ip->i_cowfp && ip->i_cowfp->if_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 			xfs_inode_set_cowblocks_tag(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 			xfs_inode_clear_cowblocks_tag(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 		if (tip->i_cowfp && tip->i_cowfp->if_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 			xfs_inode_set_cowblocks_tag(tip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 			xfs_inode_clear_cowblocks_tag(tip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 	xfs_trans_log_inode(tp, ip,  src_log_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 	xfs_trans_log_inode(tp, tip, target_log_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 	 * The extent forks have been swapped, but crc=1,rmapbt=0 filesystems
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 	 * have inode number owner values in the bmbt blocks that still refer to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 	 * the old inode. Scan each bmbt to fix up the owner values with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 	 * inode number of the current inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 	if (src_log_flags & XFS_ILOG_DOWNER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 		error = xfs_swap_change_owner(&tp, ip, tip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 			goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 	if (target_log_flags & XFS_ILOG_DOWNER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 		error = xfs_swap_change_owner(&tp, tip, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 			goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 	 * If this is a synchronous mount, make sure that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 	 * transaction goes to disk before returning to the user.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 	if (mp->m_flags & XFS_MOUNT_WSYNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 		xfs_trans_set_sync(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 	error = xfs_trans_commit(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 	trace_xfs_swap_extent_after(ip, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 	trace_xfs_swap_extent_after(tip, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 	xfs_iunlock(ip, lock_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 	xfs_iunlock(tip, lock_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 	unlock_two_nondirectories(VFS_I(ip), VFS_I(tip));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) out_trans_cancel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 	xfs_trans_cancel(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 	goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) }