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) 2016 Oracle.  All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  * Author: Darrick J. Wong <darrick.wong@oracle.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) #include "xfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #include "xfs_fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include "xfs_shared.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include "xfs_format.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include "xfs_log_format.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include "xfs_trans_resv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include "xfs_mount.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include "xfs_defer.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include "xfs_inode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include "xfs_trans.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include "xfs_bmap.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include "xfs_bmap_util.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include "xfs_trace.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include "xfs_icache.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include "xfs_btree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include "xfs_refcount_btree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include "xfs_refcount.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include "xfs_bmap_btree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include "xfs_trans_space.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include "xfs_bit.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include "xfs_alloc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include "xfs_quota.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include "xfs_reflink.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_sb.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include "xfs_ag_resv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34)  * Copy on Write of Shared Blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36)  * XFS must preserve "the usual" file semantics even when two files share
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37)  * the same physical blocks.  This means that a write to one file must not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38)  * alter the blocks in a different file; the way that we'll do that is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39)  * through the use of a copy-on-write mechanism.  At a high level, that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40)  * means that when we want to write to a shared block, we allocate a new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41)  * block, write the data to the new block, and if that succeeds we map the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42)  * new block into the file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44)  * XFS provides a "delayed allocation" mechanism that defers the allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45)  * of disk blocks to dirty-but-not-yet-mapped file blocks as long as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46)  * possible.  This reduces fragmentation by enabling the filesystem to ask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47)  * for bigger chunks less often, which is exactly what we want for CoW.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49)  * The delalloc mechanism begins when the kernel wants to make a block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50)  * writable (write_begin or page_mkwrite).  If the offset is not mapped, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51)  * create a delalloc mapping, which is a regular in-core extent, but without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52)  * a real startblock.  (For delalloc mappings, the startblock encodes both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53)  * a flag that this is a delalloc mapping, and a worst-case estimate of how
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54)  * many blocks might be required to put the mapping into the BMBT.)  delalloc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55)  * mappings are a reservation against the free space in the filesystem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56)  * adjacent mappings can also be combined into fewer larger mappings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58)  * As an optimization, the CoW extent size hint (cowextsz) creates
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59)  * outsized aligned delalloc reservations in the hope of landing out of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60)  * order nearby CoW writes in a single extent on disk, thereby reducing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61)  * fragmentation and improving future performance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63)  * D: --RRRRRRSSSRRRRRRRR--- (data fork)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64)  * C: ------DDDDDDD--------- (CoW fork)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66)  * When dirty pages are being written out (typically in writepage), the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67)  * delalloc reservations are converted into unwritten mappings by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68)  * allocating blocks and replacing the delalloc mapping with real ones.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69)  * A delalloc mapping can be replaced by several unwritten ones if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70)  * free space is fragmented.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72)  * D: --RRRRRRSSSRRRRRRRR---
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73)  * C: ------UUUUUUU---------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75)  * We want to adapt the delalloc mechanism for copy-on-write, since the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76)  * write paths are similar.  The first two steps (creating the reservation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77)  * and allocating the blocks) are exactly the same as delalloc except that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78)  * the mappings must be stored in a separate CoW fork because we do not want
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79)  * to disturb the mapping in the data fork until we're sure that the write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80)  * succeeded.  IO completion in this case is the process of removing the old
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81)  * mapping from the data fork and moving the new mapping from the CoW fork to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82)  * the data fork.  This will be discussed shortly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84)  * For now, unaligned directio writes will be bounced back to the page cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85)  * Block-aligned directio writes will use the same mechanism as buffered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86)  * writes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88)  * Just prior to submitting the actual disk write requests, we convert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89)  * the extents representing the range of the file actually being written
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90)  * (as opposed to extra pieces created for the cowextsize hint) to real
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91)  * extents.  This will become important in the next step:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93)  * D: --RRRRRRSSSRRRRRRRR---
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94)  * C: ------UUrrUUU---------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96)  * CoW remapping must be done after the data block write completes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97)  * because we don't want to destroy the old data fork map until we're sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98)  * the new block has been written.  Since the new mappings are kept in a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99)  * separate fork, we can simply iterate these mappings to find the ones
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100)  * that cover the file blocks that we just CoW'd.  For each extent, simply
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101)  * unmap the corresponding range in the data fork, map the new range into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102)  * the data fork, and remove the extent from the CoW fork.  Because of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103)  * the presence of the cowextsize hint, however, we must be careful
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104)  * only to remap the blocks that we've actually written out --  we must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105)  * never remap delalloc reservations nor CoW staging blocks that have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106)  * yet to be written.  This corresponds exactly to the real extents in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107)  * the CoW fork:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109)  * D: --RRRRRRrrSRRRRRRRR---
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110)  * C: ------UU--UUU---------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112)  * Since the remapping operation can be applied to an arbitrary file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113)  * range, we record the need for the remap step as a flag in the ioend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114)  * instead of declaring a new IO type.  This is required for direct io
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115)  * because we only have ioend for the whole dio, and we have to be able to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116)  * remember the presence of unwritten blocks and CoW blocks with a single
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117)  * ioend structure.  Better yet, the more ground we can cover with one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118)  * ioend, the better.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122)  * Given an AG extent, find the lowest-numbered run of shared blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123)  * within that range and return the range in fbno/flen.  If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124)  * find_end_of_shared is true, return the longest contiguous extent of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125)  * shared blocks.  If there are no shared extents, fbno and flen will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126)  * be set to NULLAGBLOCK and 0, respectively.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) xfs_reflink_find_shared(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	struct xfs_mount	*mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	struct xfs_trans	*tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	xfs_agnumber_t		agno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	xfs_agblock_t		agbno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	xfs_extlen_t		aglen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	xfs_agblock_t		*fbno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	xfs_extlen_t		*flen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	bool			find_end_of_shared)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	struct xfs_buf		*agbp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	struct xfs_btree_cur	*cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	error = xfs_alloc_read_agf(mp, tp, agno, 0, &agbp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	cur = xfs_refcountbt_init_cursor(mp, tp, agbp, agno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	error = xfs_refcount_find_shared(cur, agbno, aglen, fbno, flen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 			find_end_of_shared);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	xfs_btree_del_cursor(cur, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	xfs_trans_brelse(tp, agbp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159)  * Trim the mapping to the next block where there's a change in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160)  * shared/unshared status.  More specifically, this means that we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161)  * find the lowest-numbered extent of shared blocks that coincides with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162)  * the given block mapping.  If the shared extent overlaps the start of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163)  * the mapping, trim the mapping to the end of the shared extent.  If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164)  * the shared region intersects the mapping, trim the mapping to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165)  * start of the shared extent.  If there are no shared regions that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166)  * overlap, just return the original extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) xfs_reflink_trim_around_shared(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	struct xfs_bmbt_irec	*irec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 	bool			*shared)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	xfs_agnumber_t		agno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	xfs_agblock_t		agbno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	xfs_extlen_t		aglen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	xfs_agblock_t		fbno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	xfs_extlen_t		flen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	int			error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	/* Holes, unwritten, and delalloc extents cannot be shared */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	if (!xfs_is_cow_inode(ip) || !xfs_bmap_is_written_extent(irec)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 		*shared = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	trace_xfs_reflink_trim_around_shared(ip, irec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	agno = XFS_FSB_TO_AGNO(ip->i_mount, irec->br_startblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	agbno = XFS_FSB_TO_AGBNO(ip->i_mount, irec->br_startblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	aglen = irec->br_blockcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	error = xfs_reflink_find_shared(ip->i_mount, NULL, agno, agbno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 			aglen, &fbno, &flen, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	*shared = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	if (fbno == NULLAGBLOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 		/* No shared blocks at all. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	} else if (fbno == agbno) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 		 * The start of this extent is shared.  Truncate the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 		 * mapping at the end of the shared region so that a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 		 * subsequent iteration starts at the start of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 		 * unshared region.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 		irec->br_blockcount = flen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 		*shared = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 		 * There's a shared extent midway through this extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 		 * Truncate the mapping at the start of the shared
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 		 * extent so that a subsequent iteration starts at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 		 * start of the shared region.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 		irec->br_blockcount = fbno - agbno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) xfs_bmap_trim_cow(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	struct xfs_bmbt_irec	*imap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	bool			*shared)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	/* We can't update any real extents in always COW mode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	if (xfs_is_always_cow_inode(ip) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	    !isnullstartblock(imap->br_startblock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 		*shared = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	/* Trim the mapping to the nearest shared extent boundary. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	return xfs_reflink_trim_around_shared(ip, imap, shared);
^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) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) xfs_reflink_convert_cow_locked(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	xfs_fileoff_t		offset_fsb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	xfs_filblks_t		count_fsb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	struct xfs_iext_cursor	icur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	struct xfs_bmbt_irec	got;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	struct xfs_btree_cur	*dummy_cur = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	int			dummy_logflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	int			error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	if (!xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &got))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 		if (got.br_startoff >= offset_fsb + count_fsb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 		if (got.br_state == XFS_EXT_NORM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 		if (WARN_ON_ONCE(isnullstartblock(got.br_startblock)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 		xfs_trim_extent(&got, offset_fsb, count_fsb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 		if (!got.br_blockcount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 		got.br_state = XFS_EXT_NORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 		error = xfs_bmap_add_extent_unwritten_real(NULL, ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 				XFS_COW_FORK, &icur, &dummy_cur, &got,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 				&dummy_logflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	} while (xfs_iext_next_extent(ip->i_cowfp, &icur, &got));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) /* Convert all of the unwritten CoW extents in a file's range to real ones. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) xfs_reflink_convert_cow(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	xfs_off_t		offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	xfs_off_t		count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	struct xfs_mount	*mp = ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	xfs_fileoff_t		offset_fsb = XFS_B_TO_FSBT(mp, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	xfs_fileoff_t		end_fsb = XFS_B_TO_FSB(mp, offset + count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	xfs_filblks_t		count_fsb = end_fsb - offset_fsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	ASSERT(count != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	xfs_ilock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	error = xfs_reflink_convert_cow_locked(ip, offset_fsb, count_fsb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301)  * Find the extent that maps the given range in the COW fork. Even if the extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302)  * is not shared we might have a preallocation for it in the COW fork. If so we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303)  * use it that rather than trigger a new allocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) xfs_find_trim_cow_extent(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	struct xfs_bmbt_irec	*imap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	struct xfs_bmbt_irec	*cmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	bool			*shared,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	bool			*found)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	xfs_fileoff_t		offset_fsb = imap->br_startoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	xfs_filblks_t		count_fsb = imap->br_blockcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	struct xfs_iext_cursor	icur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	*found = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	 * If we don't find an overlapping extent, trim the range we need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	 * allocate to fit the hole we found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	if (!xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, cmap))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 		cmap->br_startoff = offset_fsb + count_fsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	if (cmap->br_startoff > offset_fsb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 		xfs_trim_extent(imap, imap->br_startoff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 				cmap->br_startoff - imap->br_startoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 		return xfs_bmap_trim_cow(ip, imap, shared);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	*shared = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	if (isnullstartblock(cmap->br_startblock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 		xfs_trim_extent(imap, cmap->br_startoff, cmap->br_blockcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 		return 0;
^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) 	/* real extent found - no need to allocate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	xfs_trim_extent(cmap, offset_fsb, count_fsb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	*found = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) /* Allocate all CoW reservations covering a range of blocks in a file. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) xfs_reflink_allocate_cow(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	struct xfs_bmbt_irec	*imap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	struct xfs_bmbt_irec	*cmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	bool			*shared,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	uint			*lockmode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	bool			convert_now)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	struct xfs_mount	*mp = ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	xfs_fileoff_t		offset_fsb = imap->br_startoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	xfs_filblks_t		count_fsb = imap->br_blockcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	struct xfs_trans	*tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	int			nimaps, error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	bool			found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	xfs_filblks_t		resaligned;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	xfs_extlen_t		resblks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	if (!ip->i_cowfp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 		ASSERT(!xfs_is_reflink_inode(ip));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 		xfs_ifork_init_cow(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	error = xfs_find_trim_cow_extent(ip, imap, cmap, shared, &found);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	if (error || !*shared)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	if (found)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 		goto convert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	resaligned = xfs_aligned_fsb_count(imap->br_startoff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 		imap->br_blockcount, xfs_get_cowextsz_hint(ip));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	resblks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	xfs_iunlock(ip, *lockmode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	*lockmode = XFS_ILOCK_EXCL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	xfs_ilock(ip, *lockmode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	error = xfs_qm_dqattach_locked(ip, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 		goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	 * Check for an overlapping extent again now that we dropped the ilock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	error = xfs_find_trim_cow_extent(ip, imap, cmap, shared, &found);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	if (error || !*shared)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 		goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	if (found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 		xfs_trans_cancel(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 		goto convert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	error = xfs_trans_reserve_quota_nblks(tp, ip, resblks, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 			XFS_QMOPT_RES_REGBLKS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 		goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	xfs_trans_ijoin(tp, ip, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	/* Allocate the entire reservation as unwritten blocks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	nimaps = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	error = xfs_bmapi_write(tp, ip, imap->br_startoff, imap->br_blockcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 			XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC, 0, cmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 			&nimaps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 		goto out_unreserve;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	xfs_inode_set_cowblocks_tag(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	error = xfs_trans_commit(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	 * Allocation succeeded but the requested range was not even partially
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	 * satisfied?  Bail out!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	if (nimaps == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 		return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) convert:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	xfs_trim_extent(cmap, offset_fsb, count_fsb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	 * COW fork extents are supposed to remain unwritten until we're ready
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	 * to initiate a disk write.  For direct I/O we are going to write the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	 * data and need the conversion, but for buffered writes we're done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	if (!convert_now || cmap->br_state == XFS_EXT_NORM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	trace_xfs_reflink_convert_cow(ip, cmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	return xfs_reflink_convert_cow_locked(ip, offset_fsb, count_fsb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) out_unreserve:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	xfs_trans_unreserve_quota_nblks(tp, ip, (long)resblks, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 			XFS_QMOPT_RES_REGBLKS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) out_trans_cancel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	xfs_trans_cancel(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448)  * Cancel CoW reservations for some block range of an inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450)  * If cancel_real is true this function cancels all COW fork extents for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451)  * inode; if cancel_real is false, real extents are not cleared.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453)  * Caller must have already joined the inode to the current transaction. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454)  * inode will be joined to the transaction returned to the caller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) xfs_reflink_cancel_cow_blocks(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	struct xfs_inode		*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	struct xfs_trans		**tpp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	xfs_fileoff_t			offset_fsb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	xfs_fileoff_t			end_fsb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	bool				cancel_real)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	struct xfs_ifork		*ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	struct xfs_bmbt_irec		got, del;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	struct xfs_iext_cursor		icur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	int				error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	if (!xfs_inode_has_cow_data(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	/* Walk backwards until we're out of the I/O range... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	while (got.br_startoff + got.br_blockcount > offset_fsb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 		del = got;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 		xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 		/* Extent delete may have bumped ext forward */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 		if (!del.br_blockcount) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 			xfs_iext_prev(ifp, &icur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 			goto next_extent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 		trace_xfs_reflink_cancel_cow(ip, &del);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 		if (isnullstartblock(del.br_startblock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 			error = xfs_bmap_del_extent_delay(ip, XFS_COW_FORK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 					&icur, &got, &del);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 		} else if (del.br_state == XFS_EXT_UNWRITTEN || cancel_real) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 			ASSERT((*tpp)->t_firstblock == NULLFSBLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 			/* Free the CoW orphan record. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 			xfs_refcount_free_cow_extent(*tpp, del.br_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 					del.br_blockcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 			xfs_bmap_add_free(*tpp, del.br_startblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 					  del.br_blockcount, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 			/* Roll the transaction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 			error = xfs_defer_finish(tpp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 			/* Remove the mapping from the CoW fork. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 			xfs_bmap_del_extent_cow(ip, &icur, &got, &del);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 			/* Remove the quota reservation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 			error = xfs_trans_reserve_quota_nblks(NULL, ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 					-(long)del.br_blockcount, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 					XFS_QMOPT_RES_REGBLKS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 			/* Didn't do anything, push cursor back. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 			xfs_iext_prev(ifp, &icur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) next_extent:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 		if (!xfs_iext_get_extent(ifp, &icur, &got))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	/* clear tag if cow fork is emptied */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	if (!ifp->if_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 		xfs_inode_clear_cowblocks_tag(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) }
^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)  * Cancel CoW reservations for some byte range of an inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534)  * If cancel_real is true this function cancels all COW fork extents for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535)  * inode; if cancel_real is false, real extents are not cleared.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) xfs_reflink_cancel_cow_range(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	xfs_off_t		offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	xfs_off_t		count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	bool			cancel_real)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	struct xfs_trans	*tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	xfs_fileoff_t		offset_fsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	xfs_fileoff_t		end_fsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	trace_xfs_reflink_cancel_cow_range(ip, offset, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	ASSERT(ip->i_cowfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	if (count == NULLFILEOFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 		end_fsb = NULLFILEOFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 		end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	/* Start a rolling transaction to remove the mappings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 			0, 0, 0, &tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	xfs_ilock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	xfs_trans_ijoin(tp, ip, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	/* Scrape out the old CoW reservations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	error = xfs_reflink_cancel_cow_blocks(ip, &tp, offset_fsb, end_fsb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 			cancel_real);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 		goto out_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	error = xfs_trans_commit(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) out_cancel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 	xfs_trans_cancel(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	trace_xfs_reflink_cancel_cow_range_error(ip, error, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587)  * Remap part of the CoW fork into the data fork.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589)  * We aim to remap the range starting at @offset_fsb and ending at @end_fsb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590)  * into the data fork; this function will remap what it can (at the end of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591)  * range) and update @end_fsb appropriately.  Each remap gets its own
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592)  * transaction because we can end up merging and splitting bmbt blocks for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593)  * every remap operation and we'd like to keep the block reservation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594)  * requirements as low as possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) xfs_reflink_end_cow_extent(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	xfs_fileoff_t		offset_fsb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	xfs_fileoff_t		*end_fsb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	struct xfs_bmbt_irec	got, del;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	struct xfs_iext_cursor	icur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	struct xfs_mount	*mp = ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	struct xfs_trans	*tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	struct xfs_ifork	*ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	xfs_filblks_t		rlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	unsigned int		resblks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	/* No COW extents?  That's easy! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	if (ifp->if_bytes == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 		*end_fsb = offset_fsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	resblks = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 			XFS_TRANS_RESERVE, &tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	 * Lock the inode.  We have to ijoin without automatic unlock because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	 * the lead transaction is the refcountbt record deletion; the data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	 * fork update follows as a deferred log item.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	xfs_ilock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	xfs_trans_ijoin(tp, ip, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	 * In case of racing, overlapping AIO writes no COW extents might be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	 * left by the time I/O completes for the loser of the race.  In that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	 * case we are done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	if (!xfs_iext_lookup_extent_before(ip, ifp, end_fsb, &icur, &got) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	    got.br_startoff + got.br_blockcount <= offset_fsb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 		*end_fsb = offset_fsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 		goto out_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	 * Structure copy @got into @del, then trim @del to the range that we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	 * were asked to remap.  We preserve @got for the eventual CoW fork
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	 * deletion; from now on @del represents the mapping that we're
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	 * actually remapping.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	del = got;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	xfs_trim_extent(&del, offset_fsb, *end_fsb - offset_fsb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	ASSERT(del.br_blockcount > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	 * Only remap real extents that contain data.  With AIO, speculative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	 * preallocations can leak into the range we are called upon, and we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	 * need to skip them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	if (!xfs_bmap_is_written_extent(&got)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 		*end_fsb = del.br_startoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 		goto out_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	/* Unmap the old blocks in the data fork. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	rlen = del.br_blockcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	error = __xfs_bunmapi(tp, ip, del.br_startoff, &rlen, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 		goto out_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	/* Trim the extent to whatever got unmapped. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	xfs_trim_extent(&del, del.br_startoff + rlen, del.br_blockcount - rlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	trace_xfs_reflink_cow_remap(ip, &del);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	/* Free the CoW orphan record. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	xfs_refcount_free_cow_extent(tp, del.br_startblock, del.br_blockcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	/* Map the new blocks into the data fork. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	xfs_bmap_map_extent(tp, ip, &del);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	/* Charge this new data fork mapping to the on-disk quota. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_DELBCOUNT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 			(long)del.br_blockcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	/* Remove the mapping from the CoW fork. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	xfs_bmap_del_extent_cow(ip, &icur, &got, &del);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	error = xfs_trans_commit(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	/* Update the caller about how much progress we made. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	*end_fsb = del.br_startoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) out_cancel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	xfs_trans_cancel(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702)  * Remap parts of a file's data fork after a successful CoW.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) xfs_reflink_end_cow(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	struct xfs_inode		*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	xfs_off_t			offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	xfs_off_t			count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	xfs_fileoff_t			offset_fsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	xfs_fileoff_t			end_fsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	int				error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	trace_xfs_reflink_end_cow(ip, offset, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	 * Walk backwards until we're out of the I/O range.  The loop function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	 * repeatedly cycles the ILOCK to allocate one transaction per remapped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	 * extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	 * If we're being called by writeback then the pages will still
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	 * have PageWriteback set, which prevents races with reflink remapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	 * and truncate.  Reflink remapping prevents races with writeback by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	 * taking the iolock and mmaplock before flushing the pages and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	 * remapping, which means there won't be any further writeback or page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	 * cache dirtying until the reflink completes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	 * We should never have two threads issuing writeback for the same file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	 * region.  There are also have post-eof checks in the writeback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	 * preparation code so that we don't bother writing out pages that are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	 * about to be truncated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	 * If we're being called as part of directio write completion, the dio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	 * count is still elevated, which reflink and truncate will wait for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	 * Reflink remapping takes the iolock and mmaplock and waits for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	 * pending dio to finish, which should prevent any directio until the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	 * remap completes.  Multiple concurrent directio writes to the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	 * region are handled by end_cow processing only occurring for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	 * threads which succeed; the outcome of multiple overlapping direct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	 * writes is not well defined anyway.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	 * It's possible that a buffered write and a direct write could collide
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	 * here (the buffered write stumbles in after the dio flushes and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	 * invalidates the page cache and immediately queues writeback), but we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	 * have never supported this 100%.  If either disk write succeeds the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 	 * blocks will be remapped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 	while (end_fsb > offset_fsb && !error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 		error = xfs_reflink_end_cow_extent(ip, offset_fsb, &end_fsb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 		trace_xfs_reflink_end_cow_error(ip, error, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) }
^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)  * Free leftover CoW reservations that didn't get cleaned out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) xfs_reflink_recover_cow(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	struct xfs_mount	*mp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	xfs_agnumber_t		agno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	int			error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	if (!xfs_sb_version_hasreflink(&mp->m_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 		error = xfs_refcount_recover_cow_leftovers(mp, agno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782)  * Reflinking (Block) Ranges of Two Files Together
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784)  * First, ensure that the reflink flag is set on both inodes.  The flag is an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785)  * optimization to avoid unnecessary refcount btree lookups in the write path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787)  * Now we can iteratively remap the range of extents (and holes) in src to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788)  * corresponding ranges in dest.  Let drange and srange denote the ranges of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789)  * logical blocks in dest and src touched by the reflink operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791)  * While the length of drange is greater than zero,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792)  *    - Read src's bmbt at the start of srange ("imap")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793)  *    - If imap doesn't exist, make imap appear to start at the end of srange
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794)  *      with zero length.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795)  *    - If imap starts before srange, advance imap to start at srange.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796)  *    - If imap goes beyond srange, truncate imap to end at the end of srange.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797)  *    - Punch (imap start - srange start + imap len) blocks from dest at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798)  *      offset (drange start).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799)  *    - If imap points to a real range of pblks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800)  *         > Increase the refcount of the imap's pblks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801)  *         > Map imap's pblks into dest at the offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802)  *           (drange start + imap start - srange start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803)  *    - Advance drange and srange by (imap start - srange start + imap len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805)  * Finally, if the reflink made dest longer, update both the in-core and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806)  * on-disk file sizes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808)  * ASCII Art Demonstration:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810)  * Let's say we want to reflink this source file:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812)  * ----SSSSSSS-SSSSS----SSSSSS (src file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813)  *   <-------------------->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815)  * into this destination file:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817)  * --DDDDDDDDDDDDDDDDDDD--DDD (dest file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818)  *        <-------------------->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819)  * '-' means a hole, and 'S' and 'D' are written blocks in the src and dest.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820)  * Observe that the range has different logical offsets in either file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822)  * Consider that the first extent in the source file doesn't line up with our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823)  * reflink range.  Unmapping  and remapping are separate operations, so we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824)  * unmap more blocks from the destination file than we remap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826)  * ----SSSSSSS-SSSSS----SSSSSS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827)  *   <------->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828)  * --DDDDD---------DDDDD--DDD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829)  *        <------->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831)  * Now remap the source extent into the destination file:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833)  * ----SSSSSSS-SSSSS----SSSSSS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834)  *   <------->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835)  * --DDDDD--SSSSSSSDDDDD--DDD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836)  *        <------->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838)  * Do likewise with the second hole and extent in our range.  Holes in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839)  * unmap range don't affect our operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841)  * ----SSSSSSS-SSSSS----SSSSSS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842)  *            <---->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843)  * --DDDDD--SSSSSSS-SSSSS-DDD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844)  *                 <---->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846)  * Finally, unmap and remap part of the third extent.  This will increase the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847)  * size of the destination file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849)  * ----SSSSSSS-SSSSS----SSSSSS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850)  *                  <----->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851)  * --DDDDD--SSSSSSS-SSSSS----SSS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852)  *                       <----->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854)  * Once we update the destination file's i_size, we're done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858)  * Ensure the reflink bit is set in both inodes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) xfs_reflink_set_inode_flag(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	struct xfs_inode	*src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	struct xfs_inode	*dest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	struct xfs_mount	*mp = src->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	struct xfs_trans	*tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	if (xfs_is_reflink_inode(src) && xfs_is_reflink_inode(dest))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	/* Lock both files against IO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	if (src->i_ino == dest->i_ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 		xfs_ilock(src, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		xfs_lock_two_inodes(src, XFS_ILOCK_EXCL, dest, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	if (!xfs_is_reflink_inode(src)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 		trace_xfs_reflink_set_inode_flag(src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 		xfs_trans_ijoin(tp, src, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 		src->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 		xfs_trans_log_inode(tp, src, XFS_ILOG_CORE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 		xfs_ifork_init_cow(src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 		xfs_iunlock(src, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	if (src->i_ino == dest->i_ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 		goto commit_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	if (!xfs_is_reflink_inode(dest)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 		trace_xfs_reflink_set_inode_flag(dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 		xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 		dest->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 		xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 		xfs_ifork_init_cow(dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 		xfs_iunlock(dest, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) commit_flags:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	error = xfs_trans_commit(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 		goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) out_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	trace_xfs_reflink_set_inode_flag_error(dest, error, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915)  * Update destination inode size & cowextsize hint, if necessary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) xfs_reflink_update_dest(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	struct xfs_inode	*dest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	xfs_off_t		newlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	xfs_extlen_t		cowextsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	unsigned int		remap_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	struct xfs_mount	*mp = dest->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	struct xfs_trans	*tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	if (newlen <= i_size_read(VFS_I(dest)) && cowextsize == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 		goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	xfs_ilock(dest, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	if (newlen > i_size_read(VFS_I(dest))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 		trace_xfs_reflink_update_inode_size(dest, newlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 		i_size_write(VFS_I(dest), newlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 		dest->i_d.di_size = newlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	if (cowextsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 		dest->i_d.di_cowextsize = cowextsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 		dest->i_d.di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	error = xfs_trans_commit(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 		goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) out_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	trace_xfs_reflink_update_inode_size_error(dest, error, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962)  * Do we have enough reserve in this AG to handle a reflink?  The refcount
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963)  * btree already reserved all the space it needs, but the rmap btree can grow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964)  * infinitely, so we won't allow more reflinks when the AG is down to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965)  * btree reserves.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) xfs_reflink_ag_has_free_space(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	struct xfs_mount	*mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	xfs_agnumber_t		agno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	struct xfs_perag	*pag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	int			error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	pag = xfs_perag_get(mp, agno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	if (xfs_ag_resv_critical(pag, XFS_AG_RESV_RMAPBT) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	    xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 		error = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	xfs_perag_put(pag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987)  * Remap the given extent into the file.  The dmap blockcount will be set to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988)  * the number of blocks that were actually remapped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) xfs_reflink_remap_extent(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	struct xfs_bmbt_irec	*dmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	xfs_off_t		new_isize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	struct xfs_bmbt_irec	smap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	struct xfs_mount	*mp = ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	struct xfs_trans	*tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	xfs_off_t		newlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	int64_t			qres, qdelta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	unsigned int		resblks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	bool			smap_real;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	bool			dmap_written = xfs_bmap_is_written_extent(dmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	int			nimaps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	/* Start a rolling transaction to switch the mappings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	resblks = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 	xfs_ilock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	xfs_trans_ijoin(tp, ip, 0);
^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) 	 * Read what's currently mapped in the destination file into smap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	 * If smap isn't a hole, we will have to remove it before we can add
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	 * dmap to the destination file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	nimaps = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	error = xfs_bmapi_read(ip, dmap->br_startoff, dmap->br_blockcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 			&smap, &nimaps, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 		goto out_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	ASSERT(nimaps == 1 && smap.br_startoff == dmap->br_startoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	smap_real = xfs_bmap_is_real_extent(&smap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	 * We can only remap as many blocks as the smaller of the two extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	 * maps, because we can only remap one extent at a time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	dmap->br_blockcount = min(dmap->br_blockcount, smap.br_blockcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	ASSERT(dmap->br_blockcount == smap.br_blockcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	trace_xfs_reflink_remap_extent_dest(ip, &smap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	 * Two extents mapped to the same physical block must not have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	 * different states; that's filesystem corruption.  Move on to the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	 * extent if they're both holes or both the same physical extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	if (dmap->br_startblock == smap.br_startblock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 		if (dmap->br_state != smap.br_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 		goto out_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	/* If both extents are unwritten, leave them alone. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	if (dmap->br_state == XFS_EXT_UNWRITTEN &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	    smap.br_state == XFS_EXT_UNWRITTEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 		goto out_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	/* No reflinking if the AG of the dest mapping is low on space. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	if (dmap_written) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 		error = xfs_reflink_ag_has_free_space(mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 				XFS_FSB_TO_AGNO(mp, dmap->br_startblock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 			goto out_cancel;
^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) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	 * Compute quota reservation if we think the quota block counter for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	 * this file could increase.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	 * Adding a written extent to the extent map can cause a bmbt split,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	 * and removing a mapped extent from the extent can cause a bmbt split.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 	 * The two operations cannot both cause a split since they operate on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	 * the same index in the bmap btree, so we only need a reservation for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	 * one bmbt split if either thing is happening.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	 * If we are mapping a written extent into the file, we need to have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 	 * enough quota block count reservation to handle the blocks in that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	 * extent.  We log only the delta to the quota block counts, so if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	 * extent we're unmapping also has blocks allocated to it, we don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	 * need a quota reservation for the extent itself.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	 * Note that if we're replacing a delalloc reservation with a written
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	 * extent, we have to take the full quota reservation because removing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	 * the delalloc reservation gives the block count back to the quota
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	 * count.  This is suboptimal, but the VFS flushed the dest range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	 * before we started.  That should have removed all the delalloc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	 * reservations, but we code defensively.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	qres = qdelta = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	if (smap_real || dmap_written)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 		qres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	if (!smap_real && dmap_written)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 		qres += dmap->br_blockcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	if (qres > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 		error = xfs_trans_reserve_quota_nblks(tp, ip, qres, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 				XFS_QMOPT_RES_REGBLKS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 			goto out_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	if (smap_real) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 		 * If the extent we're unmapping is backed by storage (written
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 		 * or not), unmap the extent and drop its refcount.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 		xfs_bmap_unmap_extent(tp, ip, &smap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 		xfs_refcount_decrease_extent(tp, &smap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 		qdelta -= smap.br_blockcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	} else if (smap.br_startblock == DELAYSTARTBLOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 		xfs_filblks_t	len = smap.br_blockcount;
^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) 		 * If the extent we're unmapping is a delalloc reservation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 		 * we can use the regular bunmapi function to release the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 		 * incore state.  Dropping the delalloc reservation takes care
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 		 * of the quota reservation for us.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 		error = __xfs_bunmapi(NULL, ip, smap.br_startoff, &len, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 			goto out_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 		ASSERT(len == 0);
^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) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	 * If the extent we're sharing is backed by written storage, increase
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	 * its refcount and map it into the file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	if (dmap_written) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 		xfs_refcount_increase_extent(tp, dmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 		xfs_bmap_map_extent(tp, ip, dmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 		qdelta += dmap->br_blockcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, qdelta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	/* Update dest isize if needed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	newlen = XFS_FSB_TO_B(mp, dmap->br_startoff + dmap->br_blockcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	newlen = min_t(xfs_off_t, newlen, new_isize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	if (newlen > i_size_read(VFS_I(ip))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 		trace_xfs_reflink_update_inode_size(ip, newlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 		i_size_write(VFS_I(ip), newlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 		ip->i_d.di_size = newlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 		xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	/* Commit everything and unlock. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	error = xfs_trans_commit(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) out_cancel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	xfs_trans_cancel(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 		trace_xfs_reflink_remap_extent_error(ip, error, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) /* Remap a range of one file to the other. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) xfs_reflink_remap_blocks(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 	struct xfs_inode	*src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	loff_t			pos_in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	struct xfs_inode	*dest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	loff_t			pos_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 	loff_t			remap_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	loff_t			*remapped)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	struct xfs_bmbt_irec	imap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	struct xfs_mount	*mp = src->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 	xfs_fileoff_t		srcoff = XFS_B_TO_FSBT(mp, pos_in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 	xfs_fileoff_t		destoff = XFS_B_TO_FSBT(mp, pos_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	xfs_filblks_t		len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	xfs_filblks_t		remapped_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	xfs_off_t		new_isize = pos_out + remap_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	int			nimaps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	int			error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 	len = min_t(xfs_filblks_t, XFS_B_TO_FSB(mp, remap_len),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 			XFS_MAX_FILEOFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 	trace_xfs_reflink_remap_blocks(src, srcoff, len, dest, destoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 	while (len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 		unsigned int	lock_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 		/* Read extent from the source file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 		nimaps = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 		lock_mode = xfs_ilock_data_map_shared(src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 		error = xfs_bmapi_read(src, srcoff, len, &imap, &nimaps, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 		xfs_iunlock(src, lock_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 		 * The caller supposedly flushed all dirty pages in the source
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 		 * file range, which means that writeback should have allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 		 * or deleted all delalloc reservations in that range.  If we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 		 * find one, that's a good sign that something is seriously
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 		 * wrong here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 		ASSERT(nimaps == 1 && imap.br_startoff == srcoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 		if (imap.br_startblock == DELAYSTARTBLOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 			ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 		trace_xfs_reflink_remap_extent_src(src, &imap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 		/* Remap into the destination file at the given offset. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 		imap.br_startoff = destoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 		error = xfs_reflink_remap_extent(dest, &imap, new_isize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 		if (fatal_signal_pending(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 			error = -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 		/* Advance drange/srange */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 		srcoff += imap.br_blockcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 		destoff += imap.br_blockcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 		len -= imap.br_blockcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 		remapped_len += imap.br_blockcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 		trace_xfs_reflink_remap_blocks_error(dest, error, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 	*remapped = min_t(loff_t, remap_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 			  XFS_FSB_TO_B(src->i_mount, remapped_len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233)  * If we're reflinking to a point past the destination file's EOF, we must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234)  * zero any speculative post-EOF preallocations that sit between the old EOF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235)  * and the destination file offset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) xfs_reflink_zero_posteof(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	loff_t			pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 	loff_t			isize = i_size_read(VFS_I(ip));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	if (pos <= isize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 	trace_xfs_zero_eof(ip, isize, pos - isize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 	return iomap_zero_range(VFS_I(ip), isize, pos - isize, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 			&xfs_buffered_write_iomap_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253)  * Prepare two files for range cloning.  Upon a successful return both inodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254)  * will have the iolock and mmaplock held, the page cache of the out file will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255)  * be truncated, and any leases on the out file will have been broken.  This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256)  * function borrows heavily from xfs_file_aio_write_checks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258)  * The VFS allows partial EOF blocks to "match" for dedupe even though it hasn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259)  * checked that the bytes beyond EOF physically match. Hence we cannot use the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260)  * EOF block in the source dedupe range because it's not a complete block match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261)  * hence can introduce a corruption into the file that has it's block replaced.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263)  * In similar fashion, the VFS file cloning also allows partial EOF blocks to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264)  * "block aligned" for the purposes of cloning entire files.  However, if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265)  * source file range includes the EOF block and it lands within the existing EOF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266)  * of the destination file, then we can expose stale data from beyond the source
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267)  * file EOF in the destination file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269)  * XFS doesn't support partial block sharing, so in both cases we have check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270)  * these cases ourselves. For dedupe, we can simply round the length to dedupe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271)  * down to the previous whole block and ignore the partial EOF block. While this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272)  * means we can't dedupe the last block of a file, this is an acceptible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273)  * tradeoff for simplicity on implementation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275)  * For cloning, we want to share the partial EOF block if it is also the new EOF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276)  * block of the destination file. If the partial EOF block lies inside the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277)  * existing destination EOF, then we have to abort the clone to avoid exposing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278)  * stale data in the destination file. Hence we reject these clone attempts with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279)  * -EINVAL in this case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) xfs_reflink_remap_prep(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	struct file		*file_in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 	loff_t			pos_in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 	struct file		*file_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 	loff_t			pos_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 	loff_t			*len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	unsigned int		remap_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 	struct inode		*inode_in = file_inode(file_in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 	struct xfs_inode	*src = XFS_I(inode_in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	struct inode		*inode_out = file_inode(file_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	struct xfs_inode	*dest = XFS_I(inode_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 	int			ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 	/* Lock both files against IO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 	ret = xfs_ilock2_io_mmap(src, dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	/* Check file eligibility and prepare for block sharing. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 	/* Don't reflink realtime inodes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	if (XFS_IS_REALTIME_INODE(src) || XFS_IS_REALTIME_INODE(dest))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	/* Don't share DAX file data for now. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 	if (IS_DAX(inode_in) || IS_DAX(inode_out))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 	ret = generic_remap_file_range_prep(file_in, pos_in, file_out, pos_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 			len, remap_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 	if (ret || *len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 	/* Attach dquots to dest inode before changing block map */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	ret = xfs_qm_dqattach(dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 	 * Zero existing post-eof speculative preallocations in the destination
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 	 * file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 	ret = xfs_reflink_zero_posteof(dest, pos_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 	/* Set flags and remap blocks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 	ret = xfs_reflink_set_inode_flag(src, dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 	 * If pos_out > EOF, we may have dirtied blocks between EOF and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	 * pos_out. In that case, we need to extend the flush and unmap to cover
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 	 * from EOF to the end of the copy length.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 	if (pos_out > XFS_ISIZE(dest)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 		loff_t	flen = *len + (pos_out - XFS_ISIZE(dest));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 		ret = xfs_flush_unmap_range(dest, XFS_ISIZE(dest), flen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 		ret = xfs_flush_unmap_range(dest, pos_out, *len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 	xfs_iunlock2_io_mmap(src, dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) /* Does this inode need the reflink flag? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) xfs_reflink_inode_has_shared_extents(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 	struct xfs_trans		*tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 	struct xfs_inode		*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	bool				*has_shared)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 	struct xfs_bmbt_irec		got;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	struct xfs_mount		*mp = ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	struct xfs_ifork		*ifp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 	xfs_agnumber_t			agno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 	xfs_agblock_t			agbno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 	xfs_extlen_t			aglen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 	xfs_agblock_t			rbno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 	xfs_extlen_t			rlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 	struct xfs_iext_cursor		icur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 	bool				found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 	int				error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 	ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	if (!(ifp->if_flags & XFS_IFEXTENTS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 		error = xfs_iread_extents(tp, ip, XFS_DATA_FORK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 	*has_shared = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 	found = xfs_iext_lookup_extent(ip, ifp, 0, &icur, &got);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 	while (found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 		if (isnullstartblock(got.br_startblock) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 		    got.br_state != XFS_EXT_NORM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 			goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 		agno = XFS_FSB_TO_AGNO(mp, got.br_startblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 		agbno = XFS_FSB_TO_AGBNO(mp, got.br_startblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 		aglen = got.br_blockcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 		error = xfs_reflink_find_shared(mp, tp, agno, agbno, aglen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 				&rbno, &rlen, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 		/* Is there still a shared block here? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 		if (rbno != NULLAGBLOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 			*has_shared = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 		found = xfs_iext_next_extent(ifp, &icur, &got);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407)  * Clear the inode reflink flag if there are no shared extents.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409)  * The caller is responsible for joining the inode to the transaction passed in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410)  * The inode will be joined to the transaction that is returned to the caller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) xfs_reflink_clear_inode_flag(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 	struct xfs_trans	**tpp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 	bool			needs_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 	int			error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 	ASSERT(xfs_is_reflink_inode(ip));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 	error = xfs_reflink_inode_has_shared_extents(*tpp, ip, &needs_flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 	if (error || needs_flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 	 * We didn't find any shared blocks so turn off the reflink flag.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 	 * First, get rid of any leftover CoW mappings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 	error = xfs_reflink_cancel_cow_blocks(ip, tpp, 0, XFS_MAX_FILEOFF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 			true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 	/* Clear the inode flag. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 	trace_xfs_reflink_unset_inode_flag(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 	ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 	xfs_inode_clear_cowblocks_tag(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 	xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445)  * Clear the inode reflink flag if there are no shared extents and the size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446)  * hasn't changed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) xfs_reflink_try_clear_inode_flag(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 	struct xfs_inode	*ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 	struct xfs_mount	*mp = ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 	struct xfs_trans	*tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 	int			error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	/* Start a rolling transaction to remove the mappings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	xfs_ilock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 	xfs_trans_ijoin(tp, ip, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 	error = xfs_reflink_clear_inode_flag(ip, &tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 		goto cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 	error = xfs_trans_commit(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) cancel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 	xfs_trans_cancel(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482)  * Pre-COW all shared blocks within a given byte range of a file and turn off
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483)  * the reflink flag if we unshare all of the file's blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) xfs_reflink_unshare(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 	xfs_off_t		offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 	xfs_off_t		len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 	struct inode		*inode = VFS_I(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 	if (!xfs_is_reflink_inode(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 	trace_xfs_reflink_unshare(ip, offset, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 	inode_dio_wait(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 	error = iomap_file_unshare(inode, offset, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 			&xfs_buffered_write_iomap_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 	error = filemap_write_and_wait_range(inode->i_mapping, offset, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 	/* Turn off the reflink flag if possible. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 	error = xfs_reflink_try_clear_inode_flag(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 	trace_xfs_reflink_unshare_error(ip, error, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) }