blob: cbceb320a2e711fd9a40b0583973c53ba21185f9 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0+
Darrick J. Wong3993bae2016-10-03 09:11:32 -07002/*
3 * Copyright (C) 2016 Oracle. All Rights Reserved.
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004 * Author: Darrick J. Wong <darrick.wong@oracle.com>
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005 */
6#include "xfs.h"
7#include "xfs_fs.h"
8#include "xfs_shared.h"
9#include "xfs_format.h"
10#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
12#include "xfs_mount.h"
13#include "xfs_defer.h"
14#include "xfs_da_format.h"
15#include "xfs_da_btree.h"
16#include "xfs_inode.h"
17#include "xfs_trans.h"
18#include "xfs_inode_item.h"
19#include "xfs_bmap.h"
20#include "xfs_bmap_util.h"
21#include "xfs_error.h"
22#include "xfs_dir2.h"
23#include "xfs_dir2_priv.h"
24#include "xfs_ioctl.h"
25#include "xfs_trace.h"
26#include "xfs_log.h"
27#include "xfs_icache.h"
28#include "xfs_pnfs.h"
Darrick J. Wong174edb02016-10-03 09:11:39 -070029#include "xfs_btree.h"
Darrick J. Wong3993bae2016-10-03 09:11:32 -070030#include "xfs_refcount_btree.h"
31#include "xfs_refcount.h"
32#include "xfs_bmap_btree.h"
33#include "xfs_trans_space.h"
34#include "xfs_bit.h"
35#include "xfs_alloc.h"
36#include "xfs_quota_defs.h"
37#include "xfs_quota.h"
Darrick J. Wong3993bae2016-10-03 09:11:32 -070038#include "xfs_reflink.h"
Darrick J. Wong2a067052016-10-03 09:11:33 -070039#include "xfs_iomap.h"
Darrick J. Wong43caeb12016-10-03 09:11:35 -070040#include "xfs_rmap_btree.h"
Darrick J. Wong6fa164b2016-10-03 09:11:45 -070041#include "xfs_sb.h"
42#include "xfs_ag_resv.h"
Darrick J. Wong3993bae2016-10-03 09:11:32 -070043
44/*
45 * Copy on Write of Shared Blocks
46 *
47 * XFS must preserve "the usual" file semantics even when two files share
48 * the same physical blocks. This means that a write to one file must not
49 * alter the blocks in a different file; the way that we'll do that is
50 * through the use of a copy-on-write mechanism. At a high level, that
51 * means that when we want to write to a shared block, we allocate a new
52 * block, write the data to the new block, and if that succeeds we map the
53 * new block into the file.
54 *
55 * XFS provides a "delayed allocation" mechanism that defers the allocation
56 * of disk blocks to dirty-but-not-yet-mapped file blocks as long as
57 * possible. This reduces fragmentation by enabling the filesystem to ask
58 * for bigger chunks less often, which is exactly what we want for CoW.
59 *
60 * The delalloc mechanism begins when the kernel wants to make a block
61 * writable (write_begin or page_mkwrite). If the offset is not mapped, we
62 * create a delalloc mapping, which is a regular in-core extent, but without
63 * a real startblock. (For delalloc mappings, the startblock encodes both
64 * a flag that this is a delalloc mapping, and a worst-case estimate of how
65 * many blocks might be required to put the mapping into the BMBT.) delalloc
66 * mappings are a reservation against the free space in the filesystem;
67 * adjacent mappings can also be combined into fewer larger mappings.
68 *
Darrick J. Wong5eda4302017-02-02 15:14:02 -080069 * As an optimization, the CoW extent size hint (cowextsz) creates
70 * outsized aligned delalloc reservations in the hope of landing out of
71 * order nearby CoW writes in a single extent on disk, thereby reducing
72 * fragmentation and improving future performance.
73 *
74 * D: --RRRRRRSSSRRRRRRRR--- (data fork)
75 * C: ------DDDDDDD--------- (CoW fork)
76 *
Darrick J. Wong3993bae2016-10-03 09:11:32 -070077 * When dirty pages are being written out (typically in writepage), the
Darrick J. Wong5eda4302017-02-02 15:14:02 -080078 * delalloc reservations are converted into unwritten mappings by
79 * allocating blocks and replacing the delalloc mapping with real ones.
80 * A delalloc mapping can be replaced by several unwritten ones if the
81 * free space is fragmented.
82 *
83 * D: --RRRRRRSSSRRRRRRRR---
84 * C: ------UUUUUUU---------
Darrick J. Wong3993bae2016-10-03 09:11:32 -070085 *
86 * We want to adapt the delalloc mechanism for copy-on-write, since the
87 * write paths are similar. The first two steps (creating the reservation
88 * and allocating the blocks) are exactly the same as delalloc except that
89 * the mappings must be stored in a separate CoW fork because we do not want
90 * to disturb the mapping in the data fork until we're sure that the write
91 * succeeded. IO completion in this case is the process of removing the old
92 * mapping from the data fork and moving the new mapping from the CoW fork to
93 * the data fork. This will be discussed shortly.
94 *
95 * For now, unaligned directio writes will be bounced back to the page cache.
96 * Block-aligned directio writes will use the same mechanism as buffered
97 * writes.
98 *
Darrick J. Wong5eda4302017-02-02 15:14:02 -080099 * Just prior to submitting the actual disk write requests, we convert
100 * the extents representing the range of the file actually being written
101 * (as opposed to extra pieces created for the cowextsize hint) to real
102 * extents. This will become important in the next step:
103 *
104 * D: --RRRRRRSSSRRRRRRRR---
105 * C: ------UUrrUUU---------
106 *
Darrick J. Wong3993bae2016-10-03 09:11:32 -0700107 * CoW remapping must be done after the data block write completes,
108 * because we don't want to destroy the old data fork map until we're sure
109 * the new block has been written. Since the new mappings are kept in a
110 * separate fork, we can simply iterate these mappings to find the ones
111 * that cover the file blocks that we just CoW'd. For each extent, simply
112 * unmap the corresponding range in the data fork, map the new range into
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800113 * the data fork, and remove the extent from the CoW fork. Because of
114 * the presence of the cowextsize hint, however, we must be careful
115 * only to remap the blocks that we've actually written out -- we must
116 * never remap delalloc reservations nor CoW staging blocks that have
117 * yet to be written. This corresponds exactly to the real extents in
118 * the CoW fork:
119 *
120 * D: --RRRRRRrrSRRRRRRRR---
121 * C: ------UU--UUU---------
Darrick J. Wong3993bae2016-10-03 09:11:32 -0700122 *
123 * Since the remapping operation can be applied to an arbitrary file
124 * range, we record the need for the remap step as a flag in the ioend
125 * instead of declaring a new IO type. This is required for direct io
126 * because we only have ioend for the whole dio, and we have to be able to
127 * remember the presence of unwritten blocks and CoW blocks with a single
128 * ioend structure. Better yet, the more ground we can cover with one
129 * ioend, the better.
130 */
Darrick J. Wong2a067052016-10-03 09:11:33 -0700131
132/*
133 * Given an AG extent, find the lowest-numbered run of shared blocks
134 * within that range and return the range in fbno/flen. If
135 * find_end_of_shared is true, return the longest contiguous extent of
136 * shared blocks. If there are no shared extents, fbno and flen will
137 * be set to NULLAGBLOCK and 0, respectively.
138 */
139int
140xfs_reflink_find_shared(
141 struct xfs_mount *mp,
Darrick J. Wong92ff7282017-06-16 11:00:10 -0700142 struct xfs_trans *tp,
Darrick J. Wong2a067052016-10-03 09:11:33 -0700143 xfs_agnumber_t agno,
144 xfs_agblock_t agbno,
145 xfs_extlen_t aglen,
146 xfs_agblock_t *fbno,
147 xfs_extlen_t *flen,
148 bool find_end_of_shared)
149{
150 struct xfs_buf *agbp;
151 struct xfs_btree_cur *cur;
152 int error;
153
Darrick J. Wong92ff7282017-06-16 11:00:10 -0700154 error = xfs_alloc_read_agf(mp, tp, agno, 0, &agbp);
Darrick J. Wong2a067052016-10-03 09:11:33 -0700155 if (error)
156 return error;
Darrick J. Wong10479e22017-07-17 14:30:57 -0700157 if (!agbp)
158 return -ENOMEM;
Darrick J. Wong2a067052016-10-03 09:11:33 -0700159
Brian Fostered7ef8e2018-07-11 22:26:17 -0700160 cur = xfs_refcountbt_init_cursor(mp, tp, agbp, agno);
Darrick J. Wong2a067052016-10-03 09:11:33 -0700161
162 error = xfs_refcount_find_shared(cur, agbno, aglen, fbno, flen,
163 find_end_of_shared);
164
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -0700165 xfs_btree_del_cursor(cur, error);
Darrick J. Wong2a067052016-10-03 09:11:33 -0700166
Darrick J. Wong92ff7282017-06-16 11:00:10 -0700167 xfs_trans_brelse(tp, agbp);
Darrick J. Wong2a067052016-10-03 09:11:33 -0700168 return error;
169}
170
171/*
172 * Trim the mapping to the next block where there's a change in the
173 * shared/unshared status. More specifically, this means that we
174 * find the lowest-numbered extent of shared blocks that coincides with
175 * the given block mapping. If the shared extent overlaps the start of
176 * the mapping, trim the mapping to the end of the shared extent. If
177 * the shared region intersects the mapping, trim the mapping to the
178 * start of the shared extent. If there are no shared regions that
179 * overlap, just return the original extent.
180 */
181int
182xfs_reflink_trim_around_shared(
183 struct xfs_inode *ip,
184 struct xfs_bmbt_irec *irec,
185 bool *shared,
186 bool *trimmed)
187{
188 xfs_agnumber_t agno;
189 xfs_agblock_t agbno;
190 xfs_extlen_t aglen;
191 xfs_agblock_t fbno;
192 xfs_extlen_t flen;
193 int error = 0;
194
195 /* Holes, unwritten, and delalloc extents cannot be shared */
Christoph Hellwig9c4f29d2017-03-28 14:53:35 -0700196 if (!xfs_is_reflink_inode(ip) || !xfs_bmap_is_real_extent(irec)) {
Darrick J. Wong2a067052016-10-03 09:11:33 -0700197 *shared = false;
198 return 0;
199 }
200
201 trace_xfs_reflink_trim_around_shared(ip, irec);
202
203 agno = XFS_FSB_TO_AGNO(ip->i_mount, irec->br_startblock);
204 agbno = XFS_FSB_TO_AGBNO(ip->i_mount, irec->br_startblock);
205 aglen = irec->br_blockcount;
206
Darrick J. Wong92ff7282017-06-16 11:00:10 -0700207 error = xfs_reflink_find_shared(ip->i_mount, NULL, agno, agbno,
Darrick J. Wong2a067052016-10-03 09:11:33 -0700208 aglen, &fbno, &flen, true);
209 if (error)
210 return error;
211
212 *shared = *trimmed = false;
213 if (fbno == NULLAGBLOCK) {
214 /* No shared blocks at all. */
215 return 0;
216 } else if (fbno == agbno) {
217 /*
218 * The start of this extent is shared. Truncate the
219 * mapping at the end of the shared region so that a
220 * subsequent iteration starts at the start of the
221 * unshared region.
222 */
223 irec->br_blockcount = flen;
224 *shared = true;
225 if (flen != aglen)
226 *trimmed = true;
227 return 0;
228 } else {
229 /*
230 * There's a shared extent midway through this extent.
231 * Truncate the mapping at the start of the shared
232 * extent so that a subsequent iteration starts at the
233 * start of the shared region.
234 */
235 irec->br_blockcount = fbno - agbno;
236 *trimmed = true;
237 return 0;
238 }
239}
240
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100241/*
242 * Trim the passed in imap to the next shared/unshared extent boundary, and
243 * if imap->br_startoff points to a shared extent reserve space for it in the
244 * COW fork. In this case *shared is set to true, else to false.
245 *
246 * Note that imap will always contain the block numbers for the existing blocks
247 * in the data fork, as the upper layers need them for read-modify-write
248 * operations.
249 */
250int
251xfs_reflink_reserve_cow(
Darrick J. Wong2a067052016-10-03 09:11:33 -0700252 struct xfs_inode *ip,
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100253 struct xfs_bmbt_irec *imap,
254 bool *shared)
Darrick J. Wong2a067052016-10-03 09:11:33 -0700255{
Christoph Hellwig2755fc442016-11-24 11:39:49 +1100256 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
257 struct xfs_bmbt_irec got;
Christoph Hellwig2755fc442016-11-24 11:39:49 +1100258 int error = 0;
259 bool eof = false, trimmed;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700260 struct xfs_iext_cursor icur;
Darrick J. Wong2a067052016-10-03 09:11:33 -0700261
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100262 /*
263 * Search the COW fork extent list first. This serves two purposes:
264 * first this implement the speculative preallocation using cowextisze,
265 * so that we also unshared block adjacent to shared blocks instead
266 * of just the shared blocks themselves. Second the lookup in the
267 * extent list is generally faster than going out to the shared extent
268 * tree.
269 */
Christoph Hellwig2755fc442016-11-24 11:39:49 +1100270
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700271 if (!xfs_iext_lookup_extent(ip, ifp, imap->br_startoff, &icur, &got))
Christoph Hellwig2755fc442016-11-24 11:39:49 +1100272 eof = true;
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100273 if (!eof && got.br_startoff <= imap->br_startoff) {
274 trace_xfs_reflink_cow_found(ip, imap);
275 xfs_trim_extent(imap, got.br_startoff, got.br_blockcount);
Darrick J. Wong2a067052016-10-03 09:11:33 -0700276
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100277 *shared = true;
278 return 0;
279 }
Darrick J. Wong2a067052016-10-03 09:11:33 -0700280
281 /* Trim the mapping to the nearest shared extent boundary. */
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100282 error = xfs_reflink_trim_around_shared(ip, imap, shared, &trimmed);
Darrick J. Wong2a067052016-10-03 09:11:33 -0700283 if (error)
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100284 return error;
Darrick J. Wong2a067052016-10-03 09:11:33 -0700285
286 /* Not shared? Just report the (potentially capped) extent. */
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100287 if (!*shared)
288 return 0;
Darrick J. Wong2a067052016-10-03 09:11:33 -0700289
290 /*
291 * Fork all the shared blocks from our write offset until the end of
292 * the extent.
293 */
Darrick J. Wong4882c192018-05-04 15:30:22 -0700294 error = xfs_qm_dqattach_locked(ip, false);
Darrick J. Wong2a067052016-10-03 09:11:33 -0700295 if (error)
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100296 return error;
297
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100298 error = xfs_bmapi_reserve_delalloc(ip, XFS_COW_FORK, imap->br_startoff,
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700299 imap->br_blockcount, 0, &got, &icur, eof);
Brian Foster0260d8f2016-11-28 14:57:42 +1100300 if (error == -ENOSPC || error == -EDQUOT)
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100301 trace_xfs_reflink_cow_enospc(ip, imap);
Brian Foster0260d8f2016-11-28 14:57:42 +1100302 if (error)
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100303 return error;
Darrick J. Wong83104d42016-10-03 09:11:46 -0700304
Darrick J. Wong2a067052016-10-03 09:11:33 -0700305 trace_xfs_reflink_cow_alloc(ip, &got);
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100306 return 0;
Darrick J. Wong2a067052016-10-03 09:11:33 -0700307}
Darrick J. Wongef473662016-10-03 09:11:34 -0700308
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800309/* Convert part of an unwritten CoW extent to a real one. */
310STATIC int
311xfs_reflink_convert_cow_extent(
312 struct xfs_inode *ip,
313 struct xfs_bmbt_irec *imap,
314 xfs_fileoff_t offset_fsb,
Brian Foster8a749382018-07-11 22:26:06 -0700315 xfs_filblks_t count_fsb)
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800316{
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800317 int nimaps = 1;
318
319 if (imap->br_state == XFS_EXT_NORM)
320 return 0;
321
Christoph Hellwigdcf95852017-02-06 10:46:01 -0800322 xfs_trim_extent(imap, offset_fsb, count_fsb);
323 trace_xfs_reflink_convert_cow(ip, imap);
324 if (imap->br_blockcount == 0)
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800325 return 0;
Christoph Hellwigdcf95852017-02-06 10:46:01 -0800326 return xfs_bmapi_write(NULL, ip, imap->br_startoff, imap->br_blockcount,
Brian Fostera7beabe2018-07-11 22:26:25 -0700327 XFS_BMAPI_COWFORK | XFS_BMAPI_CONVERT, 0, imap,
Brian Foster3ae2d892018-07-11 22:26:19 -0700328 &nimaps);
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800329}
330
331/* Convert all of the unwritten CoW extents in a file's range to real ones. */
332int
333xfs_reflink_convert_cow(
334 struct xfs_inode *ip,
335 xfs_off_t offset,
336 xfs_off_t count)
337{
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800338 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800339 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
340 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count);
Christoph Hellwigb1214592017-11-03 10:34:44 -0700341 xfs_filblks_t count_fsb = end_fsb - offset_fsb;
342 struct xfs_bmbt_irec imap;
Christoph Hellwigb1214592017-11-03 10:34:44 -0700343 int nimaps = 1, error = 0;
344
345 ASSERT(count != 0);
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800346
347 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigb1214592017-11-03 10:34:44 -0700348 error = xfs_bmapi_write(NULL, ip, offset_fsb, count_fsb,
349 XFS_BMAPI_COWFORK | XFS_BMAPI_CONVERT |
Brian Fostera7beabe2018-07-11 22:26:25 -0700350 XFS_BMAPI_CONVERT_ONLY, 0, &imap, &nimaps);
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800351 xfs_iunlock(ip, XFS_ILOCK_EXCL);
352 return error;
353}
354
Darrick J. Wong0613f162016-10-03 09:11:37 -0700355/* Allocate all CoW reservations covering a range of blocks in a file. */
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800356int
357xfs_reflink_allocate_cow(
Darrick J. Wong0613f162016-10-03 09:11:37 -0700358 struct xfs_inode *ip,
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800359 struct xfs_bmbt_irec *imap,
360 bool *shared,
361 uint *lockmode)
Darrick J. Wong0613f162016-10-03 09:11:37 -0700362{
363 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800364 xfs_fileoff_t offset_fsb = imap->br_startoff;
365 xfs_filblks_t count_fsb = imap->br_blockcount;
366 struct xfs_bmbt_irec got;
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800367 struct xfs_trans *tp = NULL;
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800368 int nimaps, error = 0;
369 bool trimmed;
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800370 xfs_filblks_t resaligned;
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800371 xfs_extlen_t resblks = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700372 struct xfs_iext_cursor icur;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700373
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800374retry:
375 ASSERT(xfs_is_reflink_inode(ip));
Christoph Hellwigc7dbe3f2018-03-13 23:15:31 -0700376 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Darrick J. Wong0613f162016-10-03 09:11:37 -0700377
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800378 /*
379 * Even if the extent is not shared we might have a preallocation for
380 * it in the COW fork. If so use it.
381 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700382 if (xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &got) &&
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800383 got.br_startoff <= offset_fsb) {
384 *shared = true;
385
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800386 /* If we have a real allocation in the COW fork we're done. */
387 if (!isnullstartblock(got.br_startblock)) {
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800388 xfs_trim_extent(&got, offset_fsb, count_fsb);
389 *imap = got;
390 goto convert;
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800391 }
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800392
393 xfs_trim_extent(imap, got.br_startoff, got.br_blockcount);
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800394 } else {
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800395 error = xfs_reflink_trim_around_shared(ip, imap, shared, &trimmed);
396 if (error || !*shared)
397 goto out;
398 }
399
400 if (!tp) {
401 resaligned = xfs_aligned_fsb_count(imap->br_startoff,
402 imap->br_blockcount, xfs_get_cowextsz_hint(ip));
403 resblks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
404
405 xfs_iunlock(ip, *lockmode);
406 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
407 *lockmode = XFS_ILOCK_EXCL;
408 xfs_ilock(ip, *lockmode);
409
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800410 if (error)
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800411 return error;
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100412
Darrick J. Wong4882c192018-05-04 15:30:22 -0700413 error = xfs_qm_dqattach_locked(ip, false);
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800414 if (error)
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800415 goto out;
416 goto retry;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700417 }
418
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800419 error = xfs_trans_reserve_quota_nblks(tp, ip, resblks, 0,
420 XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong0613f162016-10-03 09:11:37 -0700421 if (error)
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800422 goto out;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700423
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800424 xfs_trans_ijoin(tp, ip, 0);
425
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800426 nimaps = 1;
427
428 /* Allocate the entire reservation as unwritten blocks. */
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800429 error = xfs_bmapi_write(tp, ip, imap->br_startoff, imap->br_blockcount,
Brian Foster650919f2018-07-11 22:26:23 -0700430 XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC,
Brian Fostera7beabe2018-07-11 22:26:25 -0700431 resblks, imap, &nimaps);
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800432 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -0700433 goto out_trans_cancel;
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800434
Darrick J. Wong86d692b2017-12-14 15:46:06 -0800435 xfs_inode_set_cowblocks_tag(ip);
436
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800437 /* Finish up. */
Darrick J. Wong0613f162016-10-03 09:11:37 -0700438 error = xfs_trans_commit(tp);
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800439 if (error)
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800440 return error;
Darrick J. Wong9f37bd12018-01-26 11:37:44 -0800441
442 /*
443 * Allocation succeeded but the requested range was not even partially
444 * satisfied? Bail out!
445 */
446 if (nimaps == 0)
447 return -ENOSPC;
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800448convert:
Brian Foster8a749382018-07-11 22:26:06 -0700449 return xfs_reflink_convert_cow_extent(ip, imap, offset_fsb, count_fsb);
Brian Fosterc8eac492018-07-24 13:43:13 -0700450out_trans_cancel:
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800451 xfs_trans_unreserve_quota_nblks(tp, ip, (long)resblks, 0,
452 XFS_QMOPT_RES_REGBLKS);
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800453out:
454 if (tp)
455 xfs_trans_cancel(tp);
456 return error;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700457}
458
Darrick J. Wongef473662016-10-03 09:11:34 -0700459/*
Christoph Hellwig3802a342017-03-07 16:45:58 -0800460 * Cancel CoW reservations for some block range of an inode.
461 *
462 * If cancel_real is true this function cancels all COW fork extents for the
463 * inode; if cancel_real is false, real extents are not cleared.
Dave Chinnerc5295c62018-05-09 07:49:09 -0700464 *
465 * Caller must have already joined the inode to the current transaction. The
466 * inode will be joined to the transaction returned to the caller.
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700467 */
468int
469xfs_reflink_cancel_cow_blocks(
470 struct xfs_inode *ip,
471 struct xfs_trans **tpp,
472 xfs_fileoff_t offset_fsb,
Christoph Hellwig3802a342017-03-07 16:45:58 -0800473 xfs_fileoff_t end_fsb,
474 bool cancel_real)
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700475{
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100476 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
Christoph Hellwigdf5ab1b2016-11-24 11:39:50 +1100477 struct xfs_bmbt_irec got, del;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700478 struct xfs_iext_cursor icur;
Christoph Hellwigdf5ab1b2016-11-24 11:39:50 +1100479 int error = 0;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700480
Christoph Hellwig51d62692018-07-17 16:51:51 -0700481 if (!xfs_inode_has_cow_data(ip))
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700482 return 0;
Christoph Hellwig41caabd2017-11-03 10:34:44 -0700483 if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got))
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100484 return 0;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700485
Christoph Hellwig41caabd2017-11-03 10:34:44 -0700486 /* Walk backwards until we're out of the I/O range... */
487 while (got.br_startoff + got.br_blockcount > offset_fsb) {
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100488 del = got;
489 xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
Christoph Hellwig41caabd2017-11-03 10:34:44 -0700490
491 /* Extent delete may have bumped ext forward */
492 if (!del.br_blockcount) {
493 xfs_iext_prev(ifp, &icur);
494 goto next_extent;
495 }
496
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100497 trace_xfs_reflink_cancel_cow(ip, &del);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700498
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100499 if (isnullstartblock(del.br_startblock)) {
500 error = xfs_bmap_del_extent_delay(ip, XFS_COW_FORK,
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700501 &icur, &got, &del);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700502 if (error)
503 break;
Christoph Hellwig3802a342017-03-07 16:45:58 -0800504 } else if (del.br_state == XFS_EXT_UNWRITTEN || cancel_real) {
Brian Foster1e5ae192018-07-24 13:43:12 -0700505 ASSERT((*tpp)->t_dfops);
506 ASSERT((*tpp)->t_firstblock == NULLFSBLOCK);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700507
Darrick J. Wong174edb02016-10-03 09:11:39 -0700508 /* Free the CoW orphan record. */
Brian Foster0f37d172018-08-01 07:20:34 -0700509 error = xfs_refcount_free_cow_extent(*tpp,
510 del.br_startblock, del.br_blockcount);
Darrick J. Wong174edb02016-10-03 09:11:39 -0700511 if (error)
512 break;
513
Brian Foster0f37d172018-08-01 07:20:34 -0700514 xfs_bmap_add_free(*tpp, del.br_startblock,
515 del.br_blockcount, NULL);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700516
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700517 /* Roll the transaction */
Brian Foster9e28a242018-07-24 13:43:15 -0700518 error = xfs_defer_finish(tpp);
Brian Foster9b1f4e92018-08-01 07:20:33 -0700519 if (error)
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700520 break;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700521
522 /* Remove the mapping from the CoW fork. */
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700523 xfs_bmap_del_extent_cow(ip, &icur, &got, &del);
Darrick J. Wong4b4c1322018-01-19 09:05:48 -0800524
525 /* Remove the quota reservation */
526 error = xfs_trans_reserve_quota_nblks(NULL, ip,
527 -(long)del.br_blockcount, 0,
528 XFS_QMOPT_RES_REGBLKS);
529 if (error)
530 break;
Darrick J. Wong9d40fba2017-12-10 18:03:55 -0800531 } else {
532 /* Didn't do anything, push cursor back. */
533 xfs_iext_prev(ifp, &icur);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700534 }
Christoph Hellwig41caabd2017-11-03 10:34:44 -0700535next_extent:
536 if (!xfs_iext_get_extent(ifp, &icur, &got))
Brian Fosterc17a8ef2016-10-24 14:21:08 +1100537 break;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700538 }
539
Brian Fosterc17a8ef2016-10-24 14:21:08 +1100540 /* clear tag if cow fork is emptied */
541 if (!ifp->if_bytes)
542 xfs_inode_clear_cowblocks_tag(ip);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700543 return error;
544}
545
546/*
Christoph Hellwig3802a342017-03-07 16:45:58 -0800547 * Cancel CoW reservations for some byte range of an inode.
548 *
549 * If cancel_real is true this function cancels all COW fork extents for the
550 * inode; if cancel_real is false, real extents are not cleared.
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700551 */
552int
553xfs_reflink_cancel_cow_range(
554 struct xfs_inode *ip,
555 xfs_off_t offset,
Christoph Hellwig3802a342017-03-07 16:45:58 -0800556 xfs_off_t count,
557 bool cancel_real)
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700558{
559 struct xfs_trans *tp;
560 xfs_fileoff_t offset_fsb;
561 xfs_fileoff_t end_fsb;
562 int error;
563
564 trace_xfs_reflink_cancel_cow_range(ip, offset, count);
Darrick J. Wong63646fc2016-10-10 16:47:32 +1100565 ASSERT(xfs_is_reflink_inode(ip));
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700566
567 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
568 if (count == NULLFILEOFF)
569 end_fsb = NULLFILEOFF;
570 else
571 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
572
573 /* Start a rolling transaction to remove the mappings */
574 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
Dave Chinner4df0f7f2018-03-06 17:07:22 -0800575 0, 0, XFS_TRANS_NOFS, &tp);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700576 if (error)
577 goto out;
578
579 xfs_ilock(ip, XFS_ILOCK_EXCL);
580 xfs_trans_ijoin(tp, ip, 0);
581
582 /* Scrape out the old CoW reservations */
Christoph Hellwig3802a342017-03-07 16:45:58 -0800583 error = xfs_reflink_cancel_cow_blocks(ip, &tp, offset_fsb, end_fsb,
584 cancel_real);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700585 if (error)
586 goto out_cancel;
587
588 error = xfs_trans_commit(tp);
589
590 xfs_iunlock(ip, XFS_ILOCK_EXCL);
591 return error;
592
593out_cancel:
594 xfs_trans_cancel(tp);
595 xfs_iunlock(ip, XFS_ILOCK_EXCL);
596out:
597 trace_xfs_reflink_cancel_cow_range_error(ip, error, _RET_IP_);
598 return error;
599}
600
601/*
602 * Remap parts of a file's data fork after a successful CoW.
603 */
604int
605xfs_reflink_end_cow(
606 struct xfs_inode *ip,
607 xfs_off_t offset,
608 xfs_off_t count)
609{
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100610 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
Christoph Hellwig4ab86712016-11-24 11:39:50 +1100611 struct xfs_bmbt_irec got, del;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700612 struct xfs_trans *tp;
613 xfs_fileoff_t offset_fsb;
614 xfs_fileoff_t end_fsb;
Christoph Hellwig4ab86712016-11-24 11:39:50 +1100615 int error;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700616 unsigned int resblks;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700617 xfs_filblks_t rlen;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700618 struct xfs_iext_cursor icur;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700619
620 trace_xfs_reflink_end_cow(ip, offset, count);
621
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100622 /* No COW extents? That's easy! */
623 if (ifp->if_bytes == 0)
624 return 0;
625
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700626 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
627 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700628
Darrick J. Wongfe0be232017-04-12 12:26:07 -0700629 /*
630 * Start a rolling transaction to switch the mappings. We're
631 * unlikely ever to have to remap 16T worth of single-block
632 * extents, so just cap the worst case extent count to 2^32-1.
633 * Stick a warning in just in case, and avoid 64-bit division.
634 */
635 BUILD_BUG_ON(MAX_RW_COUNT > UINT_MAX);
636 if (end_fsb - offset_fsb > UINT_MAX) {
637 error = -EFSCORRUPTED;
638 xfs_force_shutdown(ip->i_mount, SHUTDOWN_CORRUPT_INCORE);
639 ASSERT(0);
640 goto out;
641 }
642 resblks = XFS_NEXTENTADD_SPACE_RES(ip->i_mount,
643 (unsigned int)(end_fsb - offset_fsb),
644 XFS_DATA_FORK);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700645 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
Dave Chinner4df0f7f2018-03-06 17:07:22 -0800646 resblks, 0, XFS_TRANS_RESERVE | XFS_TRANS_NOFS, &tp);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700647 if (error)
648 goto out;
649
650 xfs_ilock(ip, XFS_ILOCK_EXCL);
651 xfs_trans_ijoin(tp, ip, 0);
652
Christoph Hellwigdc560152017-10-23 16:32:39 -0700653 /*
654 * In case of racing, overlapping AIO writes no COW extents might be
655 * left by the time I/O completes for the loser of the race. In that
656 * case we are done.
657 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700658 if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got))
Christoph Hellwigdc560152017-10-23 16:32:39 -0700659 goto out_cancel;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700660
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100661 /* Walk backwards until we're out of the I/O range... */
662 while (got.br_startoff + got.br_blockcount > offset_fsb) {
663 del = got;
664 xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
665
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700666 /* Extent delete may have bumped ext forward */
Christoph Hellwigdf79b812018-03-13 23:15:33 -0700667 if (!del.br_blockcount)
668 goto prev_extent;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700669
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100670 ASSERT(!isnullstartblock(got.br_startblock));
671
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800672 /*
673 * Don't remap unwritten extents; these are
674 * speculatively preallocated CoW extents that have been
675 * allocated but have not yet been involved in a write.
676 */
Christoph Hellwigdf79b812018-03-13 23:15:33 -0700677 if (got.br_state == XFS_EXT_UNWRITTEN)
678 goto prev_extent;
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800679
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100680 /* Unmap the old blocks in the data fork. */
Brian Fosterc8eac492018-07-24 13:43:13 -0700681 ASSERT(tp->t_dfops && tp->t_firstblock == NULLFSBLOCK);
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100682 rlen = del.br_blockcount;
Brian Foster2af52842018-07-11 22:26:25 -0700683 error = __xfs_bunmapi(tp, ip, del.br_startoff, &rlen, 0, 1);
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100684 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -0700685 goto out_cancel;
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100686
687 /* Trim the extent to whatever got unmapped. */
688 if (rlen) {
689 xfs_trim_extent(&del, del.br_startoff + rlen,
690 del.br_blockcount - rlen);
691 }
692 trace_xfs_reflink_cow_remap(ip, &del);
693
694 /* Free the CoW orphan record. */
Brian Foster0f37d172018-08-01 07:20:34 -0700695 error = xfs_refcount_free_cow_extent(tp, del.br_startblock,
696 del.br_blockcount);
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100697 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -0700698 goto out_cancel;
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100699
700 /* Map the new blocks into the data fork. */
Brian Foster0f37d172018-08-01 07:20:34 -0700701 error = xfs_bmap_map_extent(tp, ip, &del);
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100702 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -0700703 goto out_cancel;
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100704
Darrick J. Wong4b4c1322018-01-19 09:05:48 -0800705 /* Charge this new data fork mapping to the on-disk quota. */
706 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_DELBCOUNT,
707 (long)del.br_blockcount);
708
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100709 /* Remove the mapping from the CoW fork. */
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700710 xfs_bmap_del_extent_cow(ip, &icur, &got, &del);
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100711
Brian Foster9e28a242018-07-24 13:43:15 -0700712 error = xfs_defer_finish(&tp);
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100713 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -0700714 goto out_cancel;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700715 if (!xfs_iext_get_extent(ifp, &icur, &got))
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100716 break;
Christoph Hellwigdf79b812018-03-13 23:15:33 -0700717 continue;
718prev_extent:
719 if (!xfs_iext_prev_extent(ifp, &icur, &got))
720 break;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700721 }
722
723 error = xfs_trans_commit(tp);
724 xfs_iunlock(ip, XFS_ILOCK_EXCL);
725 if (error)
726 goto out;
727 return 0;
728
Christoph Hellwige12199f2017-10-03 08:58:33 -0700729out_cancel:
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700730 xfs_trans_cancel(tp);
731 xfs_iunlock(ip, XFS_ILOCK_EXCL);
732out:
733 trace_xfs_reflink_end_cow_error(ip, error, _RET_IP_);
734 return error;
735}
Darrick J. Wong174edb02016-10-03 09:11:39 -0700736
737/*
738 * Free leftover CoW reservations that didn't get cleaned out.
739 */
740int
741xfs_reflink_recover_cow(
742 struct xfs_mount *mp)
743{
744 xfs_agnumber_t agno;
745 int error = 0;
746
747 if (!xfs_sb_version_hasreflink(&mp->m_sb))
748 return 0;
749
750 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
751 error = xfs_refcount_recover_cow_leftovers(mp, agno);
752 if (error)
753 break;
754 }
755
756 return error;
757}
Darrick J. Wong862bb362016-10-03 09:11:40 -0700758
759/*
760 * Reflinking (Block) Ranges of Two Files Together
761 *
762 * First, ensure that the reflink flag is set on both inodes. The flag is an
763 * optimization to avoid unnecessary refcount btree lookups in the write path.
764 *
765 * Now we can iteratively remap the range of extents (and holes) in src to the
766 * corresponding ranges in dest. Let drange and srange denote the ranges of
767 * logical blocks in dest and src touched by the reflink operation.
768 *
769 * While the length of drange is greater than zero,
770 * - Read src's bmbt at the start of srange ("imap")
771 * - If imap doesn't exist, make imap appear to start at the end of srange
772 * with zero length.
773 * - If imap starts before srange, advance imap to start at srange.
774 * - If imap goes beyond srange, truncate imap to end at the end of srange.
775 * - Punch (imap start - srange start + imap len) blocks from dest at
776 * offset (drange start).
777 * - If imap points to a real range of pblks,
778 * > Increase the refcount of the imap's pblks
779 * > Map imap's pblks into dest at the offset
780 * (drange start + imap start - srange start)
781 * - Advance drange and srange by (imap start - srange start + imap len)
782 *
783 * Finally, if the reflink made dest longer, update both the in-core and
784 * on-disk file sizes.
785 *
786 * ASCII Art Demonstration:
787 *
788 * Let's say we want to reflink this source file:
789 *
790 * ----SSSSSSS-SSSSS----SSSSSS (src file)
791 * <-------------------->
792 *
793 * into this destination file:
794 *
795 * --DDDDDDDDDDDDDDDDDDD--DDD (dest file)
796 * <-------------------->
797 * '-' means a hole, and 'S' and 'D' are written blocks in the src and dest.
798 * Observe that the range has different logical offsets in either file.
799 *
800 * Consider that the first extent in the source file doesn't line up with our
801 * reflink range. Unmapping and remapping are separate operations, so we can
802 * unmap more blocks from the destination file than we remap.
803 *
804 * ----SSSSSSS-SSSSS----SSSSSS
805 * <------->
806 * --DDDDD---------DDDDD--DDD
807 * <------->
808 *
809 * Now remap the source extent into the destination file:
810 *
811 * ----SSSSSSS-SSSSS----SSSSSS
812 * <------->
813 * --DDDDD--SSSSSSSDDDDD--DDD
814 * <------->
815 *
816 * Do likewise with the second hole and extent in our range. Holes in the
817 * unmap range don't affect our operation.
818 *
819 * ----SSSSSSS-SSSSS----SSSSSS
820 * <---->
821 * --DDDDD--SSSSSSS-SSSSS-DDD
822 * <---->
823 *
824 * Finally, unmap and remap part of the third extent. This will increase the
825 * size of the destination file.
826 *
827 * ----SSSSSSS-SSSSS----SSSSSS
828 * <----->
829 * --DDDDD--SSSSSSS-SSSSS----SSS
830 * <----->
831 *
832 * Once we update the destination file's i_size, we're done.
833 */
834
835/*
836 * Ensure the reflink bit is set in both inodes.
837 */
838STATIC int
839xfs_reflink_set_inode_flag(
840 struct xfs_inode *src,
841 struct xfs_inode *dest)
842{
843 struct xfs_mount *mp = src->i_mount;
844 int error;
845 struct xfs_trans *tp;
846
847 if (xfs_is_reflink_inode(src) && xfs_is_reflink_inode(dest))
848 return 0;
849
850 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
851 if (error)
852 goto out_error;
853
854 /* Lock both files against IO */
855 if (src->i_ino == dest->i_ino)
856 xfs_ilock(src, XFS_ILOCK_EXCL);
857 else
Darrick J. Wong7c2d2382018-01-26 15:27:33 -0800858 xfs_lock_two_inodes(src, XFS_ILOCK_EXCL, dest, XFS_ILOCK_EXCL);
Darrick J. Wong862bb362016-10-03 09:11:40 -0700859
860 if (!xfs_is_reflink_inode(src)) {
861 trace_xfs_reflink_set_inode_flag(src);
862 xfs_trans_ijoin(tp, src, XFS_ILOCK_EXCL);
863 src->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
864 xfs_trans_log_inode(tp, src, XFS_ILOG_CORE);
865 xfs_ifork_init_cow(src);
866 } else
867 xfs_iunlock(src, XFS_ILOCK_EXCL);
868
869 if (src->i_ino == dest->i_ino)
870 goto commit_flags;
871
872 if (!xfs_is_reflink_inode(dest)) {
873 trace_xfs_reflink_set_inode_flag(dest);
874 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
875 dest->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
876 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
877 xfs_ifork_init_cow(dest);
878 } else
879 xfs_iunlock(dest, XFS_ILOCK_EXCL);
880
881commit_flags:
882 error = xfs_trans_commit(tp);
883 if (error)
884 goto out_error;
885 return error;
886
887out_error:
888 trace_xfs_reflink_set_inode_flag_error(dest, error, _RET_IP_);
889 return error;
890}
891
892/*
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700893 * Update destination inode size & cowextsize hint, if necessary.
Darrick J. Wong862bb362016-10-03 09:11:40 -0700894 */
895STATIC int
896xfs_reflink_update_dest(
897 struct xfs_inode *dest,
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700898 xfs_off_t newlen,
Christoph Hellwigc5ecb422017-02-06 17:45:51 -0800899 xfs_extlen_t cowextsize,
900 bool is_dedupe)
Darrick J. Wong862bb362016-10-03 09:11:40 -0700901{
902 struct xfs_mount *mp = dest->i_mount;
903 struct xfs_trans *tp;
904 int error;
905
Christoph Hellwigc5ecb422017-02-06 17:45:51 -0800906 if (is_dedupe && newlen <= i_size_read(VFS_I(dest)) && cowextsize == 0)
Darrick J. Wong862bb362016-10-03 09:11:40 -0700907 return 0;
908
909 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
910 if (error)
911 goto out_error;
912
913 xfs_ilock(dest, XFS_ILOCK_EXCL);
914 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
915
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700916 if (newlen > i_size_read(VFS_I(dest))) {
917 trace_xfs_reflink_update_inode_size(dest, newlen);
918 i_size_write(VFS_I(dest), newlen);
919 dest->i_d.di_size = newlen;
920 }
921
922 if (cowextsize) {
923 dest->i_d.di_cowextsize = cowextsize;
924 dest->i_d.di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
925 }
926
Christoph Hellwigc5ecb422017-02-06 17:45:51 -0800927 if (!is_dedupe) {
928 xfs_trans_ichgtime(tp, dest,
929 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
930 }
Darrick J. Wong862bb362016-10-03 09:11:40 -0700931 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
932
933 error = xfs_trans_commit(tp);
934 if (error)
935 goto out_error;
936 return error;
937
938out_error:
939 trace_xfs_reflink_update_inode_size_error(dest, error, _RET_IP_);
940 return error;
941}
942
943/*
Darrick J. Wong6fa164b2016-10-03 09:11:45 -0700944 * Do we have enough reserve in this AG to handle a reflink? The refcount
945 * btree already reserved all the space it needs, but the rmap btree can grow
946 * infinitely, so we won't allow more reflinks when the AG is down to the
947 * btree reserves.
948 */
949static int
950xfs_reflink_ag_has_free_space(
951 struct xfs_mount *mp,
952 xfs_agnumber_t agno)
953{
954 struct xfs_perag *pag;
955 int error = 0;
956
957 if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
958 return 0;
959
960 pag = xfs_perag_get(mp, agno);
Brian Foster21592862018-03-09 14:01:59 -0800961 if (xfs_ag_resv_critical(pag, XFS_AG_RESV_RMAPBT) ||
Darrick J. Wong6fa164b2016-10-03 09:11:45 -0700962 xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA))
963 error = -ENOSPC;
964 xfs_perag_put(pag);
965 return error;
966}
967
968/*
Darrick J. Wong862bb362016-10-03 09:11:40 -0700969 * Unmap a range of blocks from a file, then map other blocks into the hole.
970 * The range to unmap is (destoff : destoff + srcioff + irec->br_blockcount).
971 * The extent irec is mapped into dest at irec->br_startoff.
972 */
973STATIC int
974xfs_reflink_remap_extent(
975 struct xfs_inode *ip,
976 struct xfs_bmbt_irec *irec,
977 xfs_fileoff_t destoff,
978 xfs_off_t new_isize)
979{
980 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig9c4f29d2017-03-28 14:53:35 -0700981 bool real_extent = xfs_bmap_is_real_extent(irec);
Darrick J. Wong862bb362016-10-03 09:11:40 -0700982 struct xfs_trans *tp;
Darrick J. Wong862bb362016-10-03 09:11:40 -0700983 unsigned int resblks;
Darrick J. Wong862bb362016-10-03 09:11:40 -0700984 struct xfs_bmbt_irec uirec;
Darrick J. Wong862bb362016-10-03 09:11:40 -0700985 xfs_filblks_t rlen;
986 xfs_filblks_t unmap_len;
987 xfs_off_t newlen;
988 int error;
989
990 unmap_len = irec->br_startoff + irec->br_blockcount - destoff;
991 trace_xfs_reflink_punch_range(ip, destoff, unmap_len);
992
Darrick J. Wong6fa164b2016-10-03 09:11:45 -0700993 /* No reflinking if we're low on space */
994 if (real_extent) {
995 error = xfs_reflink_ag_has_free_space(mp,
996 XFS_FSB_TO_AGNO(mp, irec->br_startblock));
997 if (error)
998 goto out;
999 }
1000
Darrick J. Wong862bb362016-10-03 09:11:40 -07001001 /* Start a rolling transaction to switch the mappings */
1002 resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
1003 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
1004 if (error)
1005 goto out;
1006
1007 xfs_ilock(ip, XFS_ILOCK_EXCL);
1008 xfs_trans_ijoin(tp, ip, 0);
1009
1010 /* If we're not just clearing space, then do we have enough quota? */
1011 if (real_extent) {
1012 error = xfs_trans_reserve_quota_nblks(tp, ip,
1013 irec->br_blockcount, 0, XFS_QMOPT_RES_REGBLKS);
1014 if (error)
1015 goto out_cancel;
1016 }
1017
1018 trace_xfs_reflink_remap(ip, irec->br_startoff,
1019 irec->br_blockcount, irec->br_startblock);
1020
1021 /* Unmap the old blocks in the data fork. */
1022 rlen = unmap_len;
1023 while (rlen) {
Brian Fosterc8eac492018-07-24 13:43:13 -07001024 ASSERT(tp->t_dfops && tp->t_firstblock == NULLFSBLOCK);
Brian Foster2af52842018-07-11 22:26:25 -07001025 error = __xfs_bunmapi(tp, ip, destoff, &rlen, 0, 1);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001026 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -07001027 goto out_cancel;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001028
1029 /*
1030 * Trim the extent to whatever got unmapped.
1031 * Remember, bunmapi works backwards.
1032 */
1033 uirec.br_startblock = irec->br_startblock + rlen;
1034 uirec.br_startoff = irec->br_startoff + rlen;
1035 uirec.br_blockcount = unmap_len - rlen;
1036 unmap_len = rlen;
1037
1038 /* If this isn't a real mapping, we're done. */
1039 if (!real_extent || uirec.br_blockcount == 0)
1040 goto next_extent;
1041
1042 trace_xfs_reflink_remap(ip, uirec.br_startoff,
1043 uirec.br_blockcount, uirec.br_startblock);
1044
1045 /* Update the refcount tree */
Brian Foster0f37d172018-08-01 07:20:34 -07001046 error = xfs_refcount_increase_extent(tp, &uirec);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001047 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -07001048 goto out_cancel;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001049
1050 /* Map the new blocks into the data fork. */
Brian Foster0f37d172018-08-01 07:20:34 -07001051 error = xfs_bmap_map_extent(tp, ip, &uirec);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001052 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -07001053 goto out_cancel;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001054
1055 /* Update quota accounting. */
1056 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT,
1057 uirec.br_blockcount);
1058
1059 /* Update dest isize if needed. */
1060 newlen = XFS_FSB_TO_B(mp,
1061 uirec.br_startoff + uirec.br_blockcount);
1062 newlen = min_t(xfs_off_t, newlen, new_isize);
1063 if (newlen > i_size_read(VFS_I(ip))) {
1064 trace_xfs_reflink_update_inode_size(ip, newlen);
1065 i_size_write(VFS_I(ip), newlen);
1066 ip->i_d.di_size = newlen;
1067 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1068 }
1069
1070next_extent:
1071 /* Process all the deferred stuff. */
Brian Foster9e28a242018-07-24 13:43:15 -07001072 error = xfs_defer_finish(&tp);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001073 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -07001074 goto out_cancel;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001075 }
1076
1077 error = xfs_trans_commit(tp);
1078 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1079 if (error)
1080 goto out;
1081 return 0;
1082
Darrick J. Wong862bb362016-10-03 09:11:40 -07001083out_cancel:
1084 xfs_trans_cancel(tp);
1085 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1086out:
1087 trace_xfs_reflink_remap_extent_error(ip, error, _RET_IP_);
1088 return error;
1089}
1090
1091/*
1092 * Iteratively remap one file's extents (and holes) to another's.
1093 */
1094STATIC int
1095xfs_reflink_remap_blocks(
1096 struct xfs_inode *src,
1097 xfs_fileoff_t srcoff,
1098 struct xfs_inode *dest,
1099 xfs_fileoff_t destoff,
1100 xfs_filblks_t len,
1101 xfs_off_t new_isize)
1102{
1103 struct xfs_bmbt_irec imap;
1104 int nimaps;
1105 int error = 0;
1106 xfs_filblks_t range_len;
1107
1108 /* drange = (destoff, destoff + len); srange = (srcoff, srcoff + len) */
1109 while (len) {
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001110 uint lock_mode;
1111
Darrick J. Wong862bb362016-10-03 09:11:40 -07001112 trace_xfs_reflink_remap_blocks_loop(src, srcoff, len,
1113 dest, destoff);
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001114
Darrick J. Wong862bb362016-10-03 09:11:40 -07001115 /* Read extent from the source file */
1116 nimaps = 1;
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001117 lock_mode = xfs_ilock_data_map_shared(src);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001118 error = xfs_bmapi_read(src, srcoff, len, &imap, &nimaps, 0);
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001119 xfs_iunlock(src, lock_mode);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001120 if (error)
1121 goto err;
1122 ASSERT(nimaps == 1);
1123
1124 trace_xfs_reflink_remap_imap(src, srcoff, len, XFS_IO_OVERWRITE,
1125 &imap);
1126
1127 /* Translate imap into the destination file. */
1128 range_len = imap.br_startoff + imap.br_blockcount - srcoff;
1129 imap.br_startoff += destoff - srcoff;
1130
1131 /* Clear dest from destoff to the end of imap and map it in. */
1132 error = xfs_reflink_remap_extent(dest, &imap, destoff,
1133 new_isize);
1134 if (error)
1135 goto err;
1136
1137 if (fatal_signal_pending(current)) {
1138 error = -EINTR;
1139 goto err;
1140 }
1141
1142 /* Advance drange/srange */
1143 srcoff += range_len;
1144 destoff += range_len;
1145 len -= range_len;
1146 }
1147
1148 return 0;
1149
1150err:
1151 trace_xfs_reflink_remap_blocks_error(dest, error, _RET_IP_);
1152 return error;
1153}
1154
1155/*
Darrick J. Wong1364b1d42018-01-18 13:55:20 -08001156 * Grab the exclusive iolock for a data copy from src to dest, making
1157 * sure to abide vfs locking order (lowest pointer value goes first) and
1158 * breaking the pnfs layout leases on dest before proceeding. The loop
1159 * is needed because we cannot call the blocking break_layout() with the
1160 * src iolock held, and therefore have to back out both locks.
1161 */
1162static int
1163xfs_iolock_two_inodes_and_break_layout(
1164 struct inode *src,
1165 struct inode *dest)
1166{
1167 int error;
1168
1169retry:
1170 if (src < dest) {
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001171 inode_lock_shared(src);
Darrick J. Wong1364b1d42018-01-18 13:55:20 -08001172 inode_lock_nested(dest, I_MUTEX_NONDIR2);
1173 } else {
1174 /* src >= dest */
1175 inode_lock(dest);
1176 }
1177
1178 error = break_layout(dest, false);
1179 if (error == -EWOULDBLOCK) {
1180 inode_unlock(dest);
1181 if (src < dest)
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001182 inode_unlock_shared(src);
Darrick J. Wong1364b1d42018-01-18 13:55:20 -08001183 error = break_layout(dest, true);
1184 if (error)
1185 return error;
1186 goto retry;
1187 }
1188 if (error) {
1189 inode_unlock(dest);
1190 if (src < dest)
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001191 inode_unlock_shared(src);
Darrick J. Wong1364b1d42018-01-18 13:55:20 -08001192 return error;
1193 }
1194 if (src > dest)
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001195 inode_lock_shared_nested(src, I_MUTEX_NONDIR2);
Darrick J. Wong1364b1d42018-01-18 13:55:20 -08001196 return 0;
1197}
1198
1199/*
Darrick J. Wong862bb362016-10-03 09:11:40 -07001200 * Link a range of blocks from one file to another.
1201 */
1202int
1203xfs_reflink_remap_range(
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001204 struct file *file_in,
1205 loff_t pos_in,
1206 struct file *file_out,
1207 loff_t pos_out,
1208 u64 len,
1209 bool is_dedupe)
Darrick J. Wong862bb362016-10-03 09:11:40 -07001210{
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001211 struct inode *inode_in = file_inode(file_in);
1212 struct xfs_inode *src = XFS_I(inode_in);
1213 struct inode *inode_out = file_inode(file_out);
1214 struct xfs_inode *dest = XFS_I(inode_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001215 struct xfs_mount *mp = src->i_mount;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001216 bool same_inode = (inode_in == inode_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001217 xfs_fileoff_t sfsbno, dfsbno;
1218 xfs_filblks_t fsblen;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001219 xfs_extlen_t cowextsize;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001220 ssize_t ret;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001221
1222 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1223 return -EOPNOTSUPP;
1224
1225 if (XFS_FORCED_SHUTDOWN(mp))
1226 return -EIO;
1227
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001228 /* Lock both files against IO */
Darrick J. Wong1364b1d42018-01-18 13:55:20 -08001229 ret = xfs_iolock_two_inodes_and_break_layout(inode_in, inode_out);
1230 if (ret)
1231 return ret;
Christoph Hellwig65523212016-11-30 14:33:25 +11001232 if (same_inode)
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001233 xfs_ilock(src, XFS_MMAPLOCK_EXCL);
Christoph Hellwig65523212016-11-30 14:33:25 +11001234 else
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001235 xfs_lock_two_inodes(src, XFS_MMAPLOCK_SHARED, dest,
Darrick J. Wong7c2d2382018-01-26 15:27:33 -08001236 XFS_MMAPLOCK_EXCL);
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001237
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001238 /* Check file eligibility and prepare for block sharing. */
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001239 ret = -EINVAL;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001240 /* Don't reflink realtime inodes */
1241 if (XFS_IS_REALTIME_INODE(src) || XFS_IS_REALTIME_INODE(dest))
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001242 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001243
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001244 /* Don't share DAX file data for now. */
1245 if (IS_DAX(inode_in) || IS_DAX(inode_out))
1246 goto out_unlock;
Darrick J. Wongcc714662016-10-03 09:11:41 -07001247
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001248 ret = vfs_clone_file_prep_inodes(inode_in, pos_in, inode_out, pos_out,
1249 &len, is_dedupe);
Darrick J. Wong22725ce2016-12-19 15:13:26 -08001250 if (ret <= 0)
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001251 goto out_unlock;
1252
Darrick J. Wong09ac8622018-01-19 08:56:04 -08001253 /* Attach dquots to dest inode before changing block map */
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -07001254 ret = xfs_qm_dqattach(dest);
Darrick J. Wong09ac8622018-01-19 08:56:04 -08001255 if (ret)
1256 goto out_unlock;
1257
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001258 trace_xfs_reflink_remap_range(src, pos_in, len, dest, pos_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001259
Darrick J. Wong5c989a02017-12-10 18:03:54 -08001260 /*
1261 * Clear out post-eof preallocations because we don't have page cache
1262 * backing the delayed allocations and they'll never get freed on
1263 * their own.
1264 */
1265 if (xfs_can_free_eofblocks(dest, true)) {
1266 ret = xfs_free_eofblocks(dest);
1267 if (ret)
1268 goto out_unlock;
1269 }
1270
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001271 /* Set flags and remap blocks. */
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001272 ret = xfs_reflink_set_inode_flag(src, dest);
1273 if (ret)
1274 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001275
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001276 dfsbno = XFS_B_TO_FSBT(mp, pos_out);
1277 sfsbno = XFS_B_TO_FSBT(mp, pos_in);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001278 fsblen = XFS_B_TO_FSB(mp, len);
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001279 ret = xfs_reflink_remap_blocks(src, sfsbno, dest, dfsbno, fsblen,
1280 pos_out + len);
1281 if (ret)
1282 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001283
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001284 /* Zap any page cache for the destination file's range. */
1285 truncate_inode_pages_range(&inode_out->i_data, pos_out,
1286 PAGE_ALIGN(pos_out + len) - 1);
1287
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001288 /*
1289 * Carry the cowextsize hint from src to dest if we're sharing the
1290 * entire source file to the entire destination file, the source file
1291 * has a cowextsize hint, and the destination file does not.
1292 */
1293 cowextsize = 0;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001294 if (pos_in == 0 && len == i_size_read(inode_in) &&
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001295 (src->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) &&
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001296 pos_out == 0 && len >= i_size_read(inode_out) &&
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001297 !(dest->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1298 cowextsize = src->i_d.di_cowextsize;
1299
Christoph Hellwigc5ecb422017-02-06 17:45:51 -08001300 ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize,
1301 is_dedupe);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001302
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001303out_unlock:
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001304 xfs_iunlock(dest, XFS_MMAPLOCK_EXCL);
Christoph Hellwig65523212016-11-30 14:33:25 +11001305 if (!same_inode)
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001306 xfs_iunlock(src, XFS_MMAPLOCK_SHARED);
1307 inode_unlock(inode_out);
1308 if (!same_inode)
1309 inode_unlock_shared(inode_in);
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001310 if (ret)
1311 trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_);
1312 return ret;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001313}
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001314
1315/*
1316 * The user wants to preemptively CoW all shared blocks in this file,
1317 * which enables us to turn off the reflink flag. Iterate all
1318 * extents which are not prealloc/delalloc to see which ranges are
1319 * mentioned in the refcount tree, then read those blocks into the
1320 * pagecache, dirty them, fsync them back out, and then we can update
1321 * the inode flag. What happens if we run out of memory? :)
1322 */
1323STATIC int
1324xfs_reflink_dirty_extents(
1325 struct xfs_inode *ip,
1326 xfs_fileoff_t fbno,
1327 xfs_filblks_t end,
1328 xfs_off_t isize)
1329{
1330 struct xfs_mount *mp = ip->i_mount;
1331 xfs_agnumber_t agno;
1332 xfs_agblock_t agbno;
1333 xfs_extlen_t aglen;
1334 xfs_agblock_t rbno;
1335 xfs_extlen_t rlen;
1336 xfs_off_t fpos;
1337 xfs_off_t flen;
1338 struct xfs_bmbt_irec map[2];
1339 int nmaps;
Darrick J. Wong9780643c2016-10-10 16:49:18 +11001340 int error = 0;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001341
1342 while (end - fbno > 0) {
1343 nmaps = 1;
1344 /*
1345 * Look for extents in the file. Skip holes, delalloc, or
1346 * unwritten extents; they can't be reflinked.
1347 */
1348 error = xfs_bmapi_read(ip, fbno, end - fbno, map, &nmaps, 0);
1349 if (error)
1350 goto out;
1351 if (nmaps == 0)
1352 break;
Christoph Hellwig9c4f29d2017-03-28 14:53:35 -07001353 if (!xfs_bmap_is_real_extent(&map[0]))
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001354 goto next;
1355
1356 map[1] = map[0];
1357 while (map[1].br_blockcount) {
1358 agno = XFS_FSB_TO_AGNO(mp, map[1].br_startblock);
1359 agbno = XFS_FSB_TO_AGBNO(mp, map[1].br_startblock);
1360 aglen = map[1].br_blockcount;
1361
Darrick J. Wong92ff7282017-06-16 11:00:10 -07001362 error = xfs_reflink_find_shared(mp, NULL, agno, agbno,
1363 aglen, &rbno, &rlen, true);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001364 if (error)
1365 goto out;
1366 if (rbno == NULLAGBLOCK)
1367 break;
1368
1369 /* Dirty the pages */
1370 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1371 fpos = XFS_FSB_TO_B(mp, map[1].br_startoff +
1372 (rbno - agbno));
1373 flen = XFS_FSB_TO_B(mp, rlen);
1374 if (fpos + flen > isize)
1375 flen = isize - fpos;
1376 error = iomap_file_dirty(VFS_I(ip), fpos, flen,
1377 &xfs_iomap_ops);
1378 xfs_ilock(ip, XFS_ILOCK_EXCL);
1379 if (error)
1380 goto out;
1381
1382 map[1].br_blockcount -= (rbno - agbno + rlen);
1383 map[1].br_startoff += (rbno - agbno + rlen);
1384 map[1].br_startblock += (rbno - agbno + rlen);
1385 }
1386
1387next:
1388 fbno = map[0].br_startoff + map[0].br_blockcount;
1389 }
1390out:
1391 return error;
1392}
1393
Darrick J. Wongea7cdd72017-06-16 11:00:11 -07001394/* Does this inode need the reflink flag? */
1395int
1396xfs_reflink_inode_has_shared_extents(
1397 struct xfs_trans *tp,
1398 struct xfs_inode *ip,
1399 bool *has_shared)
1400{
1401 struct xfs_bmbt_irec got;
1402 struct xfs_mount *mp = ip->i_mount;
1403 struct xfs_ifork *ifp;
1404 xfs_agnumber_t agno;
1405 xfs_agblock_t agbno;
1406 xfs_extlen_t aglen;
1407 xfs_agblock_t rbno;
1408 xfs_extlen_t rlen;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001409 struct xfs_iext_cursor icur;
Darrick J. Wongea7cdd72017-06-16 11:00:11 -07001410 bool found;
1411 int error;
1412
1413 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1414 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1415 error = xfs_iread_extents(tp, ip, XFS_DATA_FORK);
1416 if (error)
1417 return error;
1418 }
1419
1420 *has_shared = false;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001421 found = xfs_iext_lookup_extent(ip, ifp, 0, &icur, &got);
Darrick J. Wongea7cdd72017-06-16 11:00:11 -07001422 while (found) {
1423 if (isnullstartblock(got.br_startblock) ||
1424 got.br_state != XFS_EXT_NORM)
1425 goto next;
1426 agno = XFS_FSB_TO_AGNO(mp, got.br_startblock);
1427 agbno = XFS_FSB_TO_AGBNO(mp, got.br_startblock);
1428 aglen = got.br_blockcount;
1429
1430 error = xfs_reflink_find_shared(mp, tp, agno, agbno, aglen,
1431 &rbno, &rlen, false);
1432 if (error)
1433 return error;
1434 /* Is there still a shared block here? */
1435 if (rbno != NULLAGBLOCK) {
1436 *has_shared = true;
1437 return 0;
1438 }
1439next:
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001440 found = xfs_iext_next_extent(ifp, &icur, &got);
Darrick J. Wongea7cdd72017-06-16 11:00:11 -07001441 }
1442
1443 return 0;
1444}
1445
Dave Chinner844e5e72018-05-09 07:49:10 -07001446/*
1447 * Clear the inode reflink flag if there are no shared extents.
1448 *
1449 * The caller is responsible for joining the inode to the transaction passed in.
1450 * The inode will be joined to the transaction that is returned to the caller.
1451 */
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001452int
1453xfs_reflink_clear_inode_flag(
1454 struct xfs_inode *ip,
1455 struct xfs_trans **tpp)
1456{
Darrick J. Wongea7cdd72017-06-16 11:00:11 -07001457 bool needs_flag;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001458 int error = 0;
1459
Darrick J. Wong63646fc2016-10-10 16:47:32 +11001460 ASSERT(xfs_is_reflink_inode(ip));
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001461
Darrick J. Wongea7cdd72017-06-16 11:00:11 -07001462 error = xfs_reflink_inode_has_shared_extents(*tpp, ip, &needs_flag);
1463 if (error || needs_flag)
1464 return error;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001465
1466 /*
1467 * We didn't find any shared blocks so turn off the reflink flag.
1468 * First, get rid of any leftover CoW mappings.
1469 */
Christoph Hellwig3802a342017-03-07 16:45:58 -08001470 error = xfs_reflink_cancel_cow_blocks(ip, tpp, 0, NULLFILEOFF, true);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001471 if (error)
1472 return error;
1473
1474 /* Clear the inode flag. */
1475 trace_xfs_reflink_unset_inode_flag(ip);
1476 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
Darrick J. Wong83104d42016-10-03 09:11:46 -07001477 xfs_inode_clear_cowblocks_tag(ip);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001478 xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
1479
1480 return error;
1481}
1482
1483/*
1484 * Clear the inode reflink flag if there are no shared extents and the size
1485 * hasn't changed.
1486 */
1487STATIC int
1488xfs_reflink_try_clear_inode_flag(
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001489 struct xfs_inode *ip)
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001490{
1491 struct xfs_mount *mp = ip->i_mount;
1492 struct xfs_trans *tp;
1493 int error = 0;
1494
1495 /* Start a rolling transaction to remove the mappings */
1496 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
1497 if (error)
1498 return error;
1499
1500 xfs_ilock(ip, XFS_ILOCK_EXCL);
1501 xfs_trans_ijoin(tp, ip, 0);
1502
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001503 error = xfs_reflink_clear_inode_flag(ip, &tp);
1504 if (error)
1505 goto cancel;
1506
1507 error = xfs_trans_commit(tp);
1508 if (error)
1509 goto out;
1510
1511 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1512 return 0;
1513cancel:
1514 xfs_trans_cancel(tp);
1515out:
1516 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1517 return error;
1518}
1519
1520/*
1521 * Pre-COW all shared blocks within a given byte range of a file and turn off
1522 * the reflink flag if we unshare all of the file's blocks.
1523 */
1524int
1525xfs_reflink_unshare(
1526 struct xfs_inode *ip,
1527 xfs_off_t offset,
1528 xfs_off_t len)
1529{
1530 struct xfs_mount *mp = ip->i_mount;
1531 xfs_fileoff_t fbno;
1532 xfs_filblks_t end;
1533 xfs_off_t isize;
1534 int error;
1535
1536 if (!xfs_is_reflink_inode(ip))
1537 return 0;
1538
1539 trace_xfs_reflink_unshare(ip, offset, len);
1540
1541 inode_dio_wait(VFS_I(ip));
1542
1543 /* Try to CoW the selected ranges */
1544 xfs_ilock(ip, XFS_ILOCK_EXCL);
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001545 fbno = XFS_B_TO_FSBT(mp, offset);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001546 isize = i_size_read(VFS_I(ip));
1547 end = XFS_B_TO_FSB(mp, offset + len);
1548 error = xfs_reflink_dirty_extents(ip, fbno, end, isize);
1549 if (error)
1550 goto out_unlock;
1551 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1552
1553 /* Wait for the IO to finish */
1554 error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
1555 if (error)
1556 goto out;
1557
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001558 /* Turn off the reflink flag if possible. */
1559 error = xfs_reflink_try_clear_inode_flag(ip);
1560 if (error)
1561 goto out;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001562
1563 return 0;
1564
1565out_unlock:
1566 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1567out:
1568 trace_xfs_reflink_unshare_error(ip, error, _RET_IP_);
1569 return error;
1570}