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