blob: 9a0a565262667e41a848f39c9f6d9cb9c13440b1 [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. */
509 error = xfs_refcount_free_cow_extent(ip->i_mount,
Brian Foster27356a02018-07-11 22:26:18 -0700510 (*tpp)->t_dfops, del.br_startblock,
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100511 del.br_blockcount);
Darrick J. Wong174edb02016-10-03 09:11:39 -0700512 if (error)
513 break;
514
Brian Foster27356a02018-07-11 22:26:18 -0700515 xfs_bmap_add_free(ip->i_mount, (*tpp)->t_dfops,
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100516 del.br_startblock, del.br_blockcount,
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700517 NULL);
518
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700519 /* Roll the transaction */
Brian Foster27356a02018-07-11 22:26:18 -0700520 xfs_defer_ijoin((*tpp)->t_dfops, ip);
Brian Foster9e28a242018-07-24 13:43:15 -0700521 error = xfs_defer_finish(tpp);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700522 if (error) {
Brian Foster9e28a242018-07-24 13:43:15 -0700523 xfs_defer_cancel(*tpp);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700524 break;
525 }
526
527 /* Remove the mapping from the CoW fork. */
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700528 xfs_bmap_del_extent_cow(ip, &icur, &got, &del);
Darrick J. Wong4b4c1322018-01-19 09:05:48 -0800529
530 /* Remove the quota reservation */
531 error = xfs_trans_reserve_quota_nblks(NULL, ip,
532 -(long)del.br_blockcount, 0,
533 XFS_QMOPT_RES_REGBLKS);
534 if (error)
535 break;
Darrick J. Wong9d40fba2017-12-10 18:03:55 -0800536 } else {
537 /* Didn't do anything, push cursor back. */
538 xfs_iext_prev(ifp, &icur);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700539 }
Christoph Hellwig41caabd2017-11-03 10:34:44 -0700540next_extent:
541 if (!xfs_iext_get_extent(ifp, &icur, &got))
Brian Fosterc17a8ef2016-10-24 14:21:08 +1100542 break;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700543 }
544
Brian Fosterc17a8ef2016-10-24 14:21:08 +1100545 /* clear tag if cow fork is emptied */
546 if (!ifp->if_bytes)
547 xfs_inode_clear_cowblocks_tag(ip);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700548 return error;
549}
550
551/*
Christoph Hellwig3802a342017-03-07 16:45:58 -0800552 * Cancel CoW reservations for some byte range of an inode.
553 *
554 * If cancel_real is true this function cancels all COW fork extents for the
555 * inode; if cancel_real is false, real extents are not cleared.
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700556 */
557int
558xfs_reflink_cancel_cow_range(
559 struct xfs_inode *ip,
560 xfs_off_t offset,
Christoph Hellwig3802a342017-03-07 16:45:58 -0800561 xfs_off_t count,
562 bool cancel_real)
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700563{
564 struct xfs_trans *tp;
565 xfs_fileoff_t offset_fsb;
566 xfs_fileoff_t end_fsb;
567 int error;
568
569 trace_xfs_reflink_cancel_cow_range(ip, offset, count);
Darrick J. Wong63646fc2016-10-10 16:47:32 +1100570 ASSERT(xfs_is_reflink_inode(ip));
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700571
572 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
573 if (count == NULLFILEOFF)
574 end_fsb = NULLFILEOFF;
575 else
576 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
577
578 /* Start a rolling transaction to remove the mappings */
579 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
Dave Chinner4df0f7f2018-03-06 17:07:22 -0800580 0, 0, XFS_TRANS_NOFS, &tp);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700581 if (error)
582 goto out;
583
584 xfs_ilock(ip, XFS_ILOCK_EXCL);
585 xfs_trans_ijoin(tp, ip, 0);
586
587 /* Scrape out the old CoW reservations */
Christoph Hellwig3802a342017-03-07 16:45:58 -0800588 error = xfs_reflink_cancel_cow_blocks(ip, &tp, offset_fsb, end_fsb,
589 cancel_real);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700590 if (error)
591 goto out_cancel;
592
593 error = xfs_trans_commit(tp);
594
595 xfs_iunlock(ip, XFS_ILOCK_EXCL);
596 return error;
597
598out_cancel:
599 xfs_trans_cancel(tp);
600 xfs_iunlock(ip, XFS_ILOCK_EXCL);
601out:
602 trace_xfs_reflink_cancel_cow_range_error(ip, error, _RET_IP_);
603 return error;
604}
605
606/*
607 * Remap parts of a file's data fork after a successful CoW.
608 */
609int
610xfs_reflink_end_cow(
611 struct xfs_inode *ip,
612 xfs_off_t offset,
613 xfs_off_t count)
614{
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100615 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
Christoph Hellwig4ab86712016-11-24 11:39:50 +1100616 struct xfs_bmbt_irec got, del;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700617 struct xfs_trans *tp;
618 xfs_fileoff_t offset_fsb;
619 xfs_fileoff_t end_fsb;
Christoph Hellwig4ab86712016-11-24 11:39:50 +1100620 int error;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700621 unsigned int resblks;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700622 xfs_filblks_t rlen;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700623 struct xfs_iext_cursor icur;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700624
625 trace_xfs_reflink_end_cow(ip, offset, count);
626
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100627 /* No COW extents? That's easy! */
628 if (ifp->if_bytes == 0)
629 return 0;
630
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700631 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
632 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700633
Darrick J. Wongfe0be232017-04-12 12:26:07 -0700634 /*
635 * Start a rolling transaction to switch the mappings. We're
636 * unlikely ever to have to remap 16T worth of single-block
637 * extents, so just cap the worst case extent count to 2^32-1.
638 * Stick a warning in just in case, and avoid 64-bit division.
639 */
640 BUILD_BUG_ON(MAX_RW_COUNT > UINT_MAX);
641 if (end_fsb - offset_fsb > UINT_MAX) {
642 error = -EFSCORRUPTED;
643 xfs_force_shutdown(ip->i_mount, SHUTDOWN_CORRUPT_INCORE);
644 ASSERT(0);
645 goto out;
646 }
647 resblks = XFS_NEXTENTADD_SPACE_RES(ip->i_mount,
648 (unsigned int)(end_fsb - offset_fsb),
649 XFS_DATA_FORK);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700650 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
Dave Chinner4df0f7f2018-03-06 17:07:22 -0800651 resblks, 0, XFS_TRANS_RESERVE | XFS_TRANS_NOFS, &tp);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700652 if (error)
653 goto out;
654
655 xfs_ilock(ip, XFS_ILOCK_EXCL);
656 xfs_trans_ijoin(tp, ip, 0);
657
Christoph Hellwigdc560152017-10-23 16:32:39 -0700658 /*
659 * In case of racing, overlapping AIO writes no COW extents might be
660 * left by the time I/O completes for the loser of the race. In that
661 * case we are done.
662 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700663 if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got))
Christoph Hellwigdc560152017-10-23 16:32:39 -0700664 goto out_cancel;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700665
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100666 /* Walk backwards until we're out of the I/O range... */
667 while (got.br_startoff + got.br_blockcount > offset_fsb) {
668 del = got;
669 xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
670
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700671 /* Extent delete may have bumped ext forward */
Christoph Hellwigdf79b812018-03-13 23:15:33 -0700672 if (!del.br_blockcount)
673 goto prev_extent;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700674
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100675 ASSERT(!isnullstartblock(got.br_startblock));
676
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800677 /*
678 * Don't remap unwritten extents; these are
679 * speculatively preallocated CoW extents that have been
680 * allocated but have not yet been involved in a write.
681 */
Christoph Hellwigdf79b812018-03-13 23:15:33 -0700682 if (got.br_state == XFS_EXT_UNWRITTEN)
683 goto prev_extent;
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800684
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100685 /* Unmap the old blocks in the data fork. */
Brian Fosterc8eac492018-07-24 13:43:13 -0700686 ASSERT(tp->t_dfops && tp->t_firstblock == NULLFSBLOCK);
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100687 rlen = del.br_blockcount;
Brian Foster2af52842018-07-11 22:26:25 -0700688 error = __xfs_bunmapi(tp, ip, del.br_startoff, &rlen, 0, 1);
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100689 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -0700690 goto out_cancel;
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100691
692 /* Trim the extent to whatever got unmapped. */
693 if (rlen) {
694 xfs_trim_extent(&del, del.br_startoff + rlen,
695 del.br_blockcount - rlen);
696 }
697 trace_xfs_reflink_cow_remap(ip, &del);
698
699 /* Free the CoW orphan record. */
Brian Foster4bcfa612018-07-11 22:26:13 -0700700 error = xfs_refcount_free_cow_extent(tp->t_mountp, tp->t_dfops,
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100701 del.br_startblock, del.br_blockcount);
702 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -0700703 goto out_cancel;
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100704
705 /* Map the new blocks into the data fork. */
Brian Foster4bcfa612018-07-11 22:26:13 -0700706 error = xfs_bmap_map_extent(tp->t_mountp, tp->t_dfops, ip,
707 &del);
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100708 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -0700709 goto out_cancel;
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100710
Darrick J. Wong4b4c1322018-01-19 09:05:48 -0800711 /* Charge this new data fork mapping to the on-disk quota. */
712 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_DELBCOUNT,
713 (long)del.br_blockcount);
714
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100715 /* Remove the mapping from the CoW fork. */
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700716 xfs_bmap_del_extent_cow(ip, &icur, &got, &del);
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100717
Brian Foster4bcfa612018-07-11 22:26:13 -0700718 xfs_defer_ijoin(tp->t_dfops, ip);
Brian Foster9e28a242018-07-24 13:43:15 -0700719 error = xfs_defer_finish(&tp);
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100720 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -0700721 goto out_cancel;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700722 if (!xfs_iext_get_extent(ifp, &icur, &got))
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100723 break;
Christoph Hellwigdf79b812018-03-13 23:15:33 -0700724 continue;
725prev_extent:
726 if (!xfs_iext_prev_extent(ifp, &icur, &got))
727 break;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700728 }
729
730 error = xfs_trans_commit(tp);
731 xfs_iunlock(ip, XFS_ILOCK_EXCL);
732 if (error)
733 goto out;
734 return 0;
735
Christoph Hellwige12199f2017-10-03 08:58:33 -0700736out_cancel:
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700737 xfs_trans_cancel(tp);
738 xfs_iunlock(ip, XFS_ILOCK_EXCL);
739out:
740 trace_xfs_reflink_end_cow_error(ip, error, _RET_IP_);
741 return error;
742}
Darrick J. Wong174edb02016-10-03 09:11:39 -0700743
744/*
745 * Free leftover CoW reservations that didn't get cleaned out.
746 */
747int
748xfs_reflink_recover_cow(
749 struct xfs_mount *mp)
750{
751 xfs_agnumber_t agno;
752 int error = 0;
753
754 if (!xfs_sb_version_hasreflink(&mp->m_sb))
755 return 0;
756
757 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
758 error = xfs_refcount_recover_cow_leftovers(mp, agno);
759 if (error)
760 break;
761 }
762
763 return error;
764}
Darrick J. Wong862bb362016-10-03 09:11:40 -0700765
766/*
767 * Reflinking (Block) Ranges of Two Files Together
768 *
769 * First, ensure that the reflink flag is set on both inodes. The flag is an
770 * optimization to avoid unnecessary refcount btree lookups in the write path.
771 *
772 * Now we can iteratively remap the range of extents (and holes) in src to the
773 * corresponding ranges in dest. Let drange and srange denote the ranges of
774 * logical blocks in dest and src touched by the reflink operation.
775 *
776 * While the length of drange is greater than zero,
777 * - Read src's bmbt at the start of srange ("imap")
778 * - If imap doesn't exist, make imap appear to start at the end of srange
779 * with zero length.
780 * - If imap starts before srange, advance imap to start at srange.
781 * - If imap goes beyond srange, truncate imap to end at the end of srange.
782 * - Punch (imap start - srange start + imap len) blocks from dest at
783 * offset (drange start).
784 * - If imap points to a real range of pblks,
785 * > Increase the refcount of the imap's pblks
786 * > Map imap's pblks into dest at the offset
787 * (drange start + imap start - srange start)
788 * - Advance drange and srange by (imap start - srange start + imap len)
789 *
790 * Finally, if the reflink made dest longer, update both the in-core and
791 * on-disk file sizes.
792 *
793 * ASCII Art Demonstration:
794 *
795 * Let's say we want to reflink this source file:
796 *
797 * ----SSSSSSS-SSSSS----SSSSSS (src file)
798 * <-------------------->
799 *
800 * into this destination file:
801 *
802 * --DDDDDDDDDDDDDDDDDDD--DDD (dest file)
803 * <-------------------->
804 * '-' means a hole, and 'S' and 'D' are written blocks in the src and dest.
805 * Observe that the range has different logical offsets in either file.
806 *
807 * Consider that the first extent in the source file doesn't line up with our
808 * reflink range. Unmapping and remapping are separate operations, so we can
809 * unmap more blocks from the destination file than we remap.
810 *
811 * ----SSSSSSS-SSSSS----SSSSSS
812 * <------->
813 * --DDDDD---------DDDDD--DDD
814 * <------->
815 *
816 * Now remap the source extent into the destination file:
817 *
818 * ----SSSSSSS-SSSSS----SSSSSS
819 * <------->
820 * --DDDDD--SSSSSSSDDDDD--DDD
821 * <------->
822 *
823 * Do likewise with the second hole and extent in our range. Holes in the
824 * unmap range don't affect our operation.
825 *
826 * ----SSSSSSS-SSSSS----SSSSSS
827 * <---->
828 * --DDDDD--SSSSSSS-SSSSS-DDD
829 * <---->
830 *
831 * Finally, unmap and remap part of the third extent. This will increase the
832 * size of the destination file.
833 *
834 * ----SSSSSSS-SSSSS----SSSSSS
835 * <----->
836 * --DDDDD--SSSSSSS-SSSSS----SSS
837 * <----->
838 *
839 * Once we update the destination file's i_size, we're done.
840 */
841
842/*
843 * Ensure the reflink bit is set in both inodes.
844 */
845STATIC int
846xfs_reflink_set_inode_flag(
847 struct xfs_inode *src,
848 struct xfs_inode *dest)
849{
850 struct xfs_mount *mp = src->i_mount;
851 int error;
852 struct xfs_trans *tp;
853
854 if (xfs_is_reflink_inode(src) && xfs_is_reflink_inode(dest))
855 return 0;
856
857 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
858 if (error)
859 goto out_error;
860
861 /* Lock both files against IO */
862 if (src->i_ino == dest->i_ino)
863 xfs_ilock(src, XFS_ILOCK_EXCL);
864 else
Darrick J. Wong7c2d2382018-01-26 15:27:33 -0800865 xfs_lock_two_inodes(src, XFS_ILOCK_EXCL, dest, XFS_ILOCK_EXCL);
Darrick J. Wong862bb362016-10-03 09:11:40 -0700866
867 if (!xfs_is_reflink_inode(src)) {
868 trace_xfs_reflink_set_inode_flag(src);
869 xfs_trans_ijoin(tp, src, XFS_ILOCK_EXCL);
870 src->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
871 xfs_trans_log_inode(tp, src, XFS_ILOG_CORE);
872 xfs_ifork_init_cow(src);
873 } else
874 xfs_iunlock(src, XFS_ILOCK_EXCL);
875
876 if (src->i_ino == dest->i_ino)
877 goto commit_flags;
878
879 if (!xfs_is_reflink_inode(dest)) {
880 trace_xfs_reflink_set_inode_flag(dest);
881 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
882 dest->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
883 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
884 xfs_ifork_init_cow(dest);
885 } else
886 xfs_iunlock(dest, XFS_ILOCK_EXCL);
887
888commit_flags:
889 error = xfs_trans_commit(tp);
890 if (error)
891 goto out_error;
892 return error;
893
894out_error:
895 trace_xfs_reflink_set_inode_flag_error(dest, error, _RET_IP_);
896 return error;
897}
898
899/*
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700900 * Update destination inode size & cowextsize hint, if necessary.
Darrick J. Wong862bb362016-10-03 09:11:40 -0700901 */
902STATIC int
903xfs_reflink_update_dest(
904 struct xfs_inode *dest,
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700905 xfs_off_t newlen,
Christoph Hellwigc5ecb422017-02-06 17:45:51 -0800906 xfs_extlen_t cowextsize,
907 bool is_dedupe)
Darrick J. Wong862bb362016-10-03 09:11:40 -0700908{
909 struct xfs_mount *mp = dest->i_mount;
910 struct xfs_trans *tp;
911 int error;
912
Christoph Hellwigc5ecb422017-02-06 17:45:51 -0800913 if (is_dedupe && newlen <= i_size_read(VFS_I(dest)) && cowextsize == 0)
Darrick J. Wong862bb362016-10-03 09:11:40 -0700914 return 0;
915
916 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
917 if (error)
918 goto out_error;
919
920 xfs_ilock(dest, XFS_ILOCK_EXCL);
921 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
922
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700923 if (newlen > i_size_read(VFS_I(dest))) {
924 trace_xfs_reflink_update_inode_size(dest, newlen);
925 i_size_write(VFS_I(dest), newlen);
926 dest->i_d.di_size = newlen;
927 }
928
929 if (cowextsize) {
930 dest->i_d.di_cowextsize = cowextsize;
931 dest->i_d.di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
932 }
933
Christoph Hellwigc5ecb422017-02-06 17:45:51 -0800934 if (!is_dedupe) {
935 xfs_trans_ichgtime(tp, dest,
936 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
937 }
Darrick J. Wong862bb362016-10-03 09:11:40 -0700938 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
939
940 error = xfs_trans_commit(tp);
941 if (error)
942 goto out_error;
943 return error;
944
945out_error:
946 trace_xfs_reflink_update_inode_size_error(dest, error, _RET_IP_);
947 return error;
948}
949
950/*
Darrick J. Wong6fa164b2016-10-03 09:11:45 -0700951 * Do we have enough reserve in this AG to handle a reflink? The refcount
952 * btree already reserved all the space it needs, but the rmap btree can grow
953 * infinitely, so we won't allow more reflinks when the AG is down to the
954 * btree reserves.
955 */
956static int
957xfs_reflink_ag_has_free_space(
958 struct xfs_mount *mp,
959 xfs_agnumber_t agno)
960{
961 struct xfs_perag *pag;
962 int error = 0;
963
964 if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
965 return 0;
966
967 pag = xfs_perag_get(mp, agno);
Brian Foster21592862018-03-09 14:01:59 -0800968 if (xfs_ag_resv_critical(pag, XFS_AG_RESV_RMAPBT) ||
Darrick J. Wong6fa164b2016-10-03 09:11:45 -0700969 xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA))
970 error = -ENOSPC;
971 xfs_perag_put(pag);
972 return error;
973}
974
975/*
Darrick J. Wong862bb362016-10-03 09:11:40 -0700976 * Unmap a range of blocks from a file, then map other blocks into the hole.
977 * The range to unmap is (destoff : destoff + srcioff + irec->br_blockcount).
978 * The extent irec is mapped into dest at irec->br_startoff.
979 */
980STATIC int
981xfs_reflink_remap_extent(
982 struct xfs_inode *ip,
983 struct xfs_bmbt_irec *irec,
984 xfs_fileoff_t destoff,
985 xfs_off_t new_isize)
986{
987 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig9c4f29d2017-03-28 14:53:35 -0700988 bool real_extent = xfs_bmap_is_real_extent(irec);
Darrick J. Wong862bb362016-10-03 09:11:40 -0700989 struct xfs_trans *tp;
Darrick J. Wong862bb362016-10-03 09:11:40 -0700990 unsigned int resblks;
Darrick J. Wong862bb362016-10-03 09:11:40 -0700991 struct xfs_bmbt_irec uirec;
Darrick J. Wong862bb362016-10-03 09:11:40 -0700992 xfs_filblks_t rlen;
993 xfs_filblks_t unmap_len;
994 xfs_off_t newlen;
995 int error;
996
997 unmap_len = irec->br_startoff + irec->br_blockcount - destoff;
998 trace_xfs_reflink_punch_range(ip, destoff, unmap_len);
999
Darrick J. Wong6fa164b2016-10-03 09:11:45 -07001000 /* No reflinking if we're low on space */
1001 if (real_extent) {
1002 error = xfs_reflink_ag_has_free_space(mp,
1003 XFS_FSB_TO_AGNO(mp, irec->br_startblock));
1004 if (error)
1005 goto out;
1006 }
1007
Darrick J. Wong862bb362016-10-03 09:11:40 -07001008 /* Start a rolling transaction to switch the mappings */
1009 resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
1010 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
1011 if (error)
1012 goto out;
1013
1014 xfs_ilock(ip, XFS_ILOCK_EXCL);
1015 xfs_trans_ijoin(tp, ip, 0);
1016
1017 /* If we're not just clearing space, then do we have enough quota? */
1018 if (real_extent) {
1019 error = xfs_trans_reserve_quota_nblks(tp, ip,
1020 irec->br_blockcount, 0, XFS_QMOPT_RES_REGBLKS);
1021 if (error)
1022 goto out_cancel;
1023 }
1024
1025 trace_xfs_reflink_remap(ip, irec->br_startoff,
1026 irec->br_blockcount, irec->br_startblock);
1027
1028 /* Unmap the old blocks in the data fork. */
1029 rlen = unmap_len;
1030 while (rlen) {
Brian Fosterc8eac492018-07-24 13:43:13 -07001031 ASSERT(tp->t_dfops && tp->t_firstblock == NULLFSBLOCK);
Brian Foster2af52842018-07-11 22:26:25 -07001032 error = __xfs_bunmapi(tp, ip, destoff, &rlen, 0, 1);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001033 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -07001034 goto out_cancel;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001035
1036 /*
1037 * Trim the extent to whatever got unmapped.
1038 * Remember, bunmapi works backwards.
1039 */
1040 uirec.br_startblock = irec->br_startblock + rlen;
1041 uirec.br_startoff = irec->br_startoff + rlen;
1042 uirec.br_blockcount = unmap_len - rlen;
1043 unmap_len = rlen;
1044
1045 /* If this isn't a real mapping, we're done. */
1046 if (!real_extent || uirec.br_blockcount == 0)
1047 goto next_extent;
1048
1049 trace_xfs_reflink_remap(ip, uirec.br_startoff,
1050 uirec.br_blockcount, uirec.br_startblock);
1051
1052 /* Update the refcount tree */
Brian Foster4bcfa612018-07-11 22:26:13 -07001053 error = xfs_refcount_increase_extent(mp, tp->t_dfops, &uirec);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001054 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -07001055 goto out_cancel;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001056
1057 /* Map the new blocks into the data fork. */
Brian Foster4bcfa612018-07-11 22:26:13 -07001058 error = xfs_bmap_map_extent(mp, tp->t_dfops, ip, &uirec);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001059 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -07001060 goto out_cancel;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001061
1062 /* Update quota accounting. */
1063 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT,
1064 uirec.br_blockcount);
1065
1066 /* Update dest isize if needed. */
1067 newlen = XFS_FSB_TO_B(mp,
1068 uirec.br_startoff + uirec.br_blockcount);
1069 newlen = min_t(xfs_off_t, newlen, new_isize);
1070 if (newlen > i_size_read(VFS_I(ip))) {
1071 trace_xfs_reflink_update_inode_size(ip, newlen);
1072 i_size_write(VFS_I(ip), newlen);
1073 ip->i_d.di_size = newlen;
1074 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1075 }
1076
1077next_extent:
1078 /* Process all the deferred stuff. */
Brian Foster4bcfa612018-07-11 22:26:13 -07001079 xfs_defer_ijoin(tp->t_dfops, ip);
Brian Foster9e28a242018-07-24 13:43:15 -07001080 error = xfs_defer_finish(&tp);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001081 if (error)
Brian Fosterc8eac492018-07-24 13:43:13 -07001082 goto out_cancel;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001083 }
1084
1085 error = xfs_trans_commit(tp);
1086 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1087 if (error)
1088 goto out;
1089 return 0;
1090
Darrick J. Wong862bb362016-10-03 09:11:40 -07001091out_cancel:
1092 xfs_trans_cancel(tp);
1093 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1094out:
1095 trace_xfs_reflink_remap_extent_error(ip, error, _RET_IP_);
1096 return error;
1097}
1098
1099/*
1100 * Iteratively remap one file's extents (and holes) to another's.
1101 */
1102STATIC int
1103xfs_reflink_remap_blocks(
1104 struct xfs_inode *src,
1105 xfs_fileoff_t srcoff,
1106 struct xfs_inode *dest,
1107 xfs_fileoff_t destoff,
1108 xfs_filblks_t len,
1109 xfs_off_t new_isize)
1110{
1111 struct xfs_bmbt_irec imap;
1112 int nimaps;
1113 int error = 0;
1114 xfs_filblks_t range_len;
1115
1116 /* drange = (destoff, destoff + len); srange = (srcoff, srcoff + len) */
1117 while (len) {
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001118 uint lock_mode;
1119
Darrick J. Wong862bb362016-10-03 09:11:40 -07001120 trace_xfs_reflink_remap_blocks_loop(src, srcoff, len,
1121 dest, destoff);
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001122
Darrick J. Wong862bb362016-10-03 09:11:40 -07001123 /* Read extent from the source file */
1124 nimaps = 1;
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001125 lock_mode = xfs_ilock_data_map_shared(src);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001126 error = xfs_bmapi_read(src, srcoff, len, &imap, &nimaps, 0);
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001127 xfs_iunlock(src, lock_mode);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001128 if (error)
1129 goto err;
1130 ASSERT(nimaps == 1);
1131
1132 trace_xfs_reflink_remap_imap(src, srcoff, len, XFS_IO_OVERWRITE,
1133 &imap);
1134
1135 /* Translate imap into the destination file. */
1136 range_len = imap.br_startoff + imap.br_blockcount - srcoff;
1137 imap.br_startoff += destoff - srcoff;
1138
1139 /* Clear dest from destoff to the end of imap and map it in. */
1140 error = xfs_reflink_remap_extent(dest, &imap, destoff,
1141 new_isize);
1142 if (error)
1143 goto err;
1144
1145 if (fatal_signal_pending(current)) {
1146 error = -EINTR;
1147 goto err;
1148 }
1149
1150 /* Advance drange/srange */
1151 srcoff += range_len;
1152 destoff += range_len;
1153 len -= range_len;
1154 }
1155
1156 return 0;
1157
1158err:
1159 trace_xfs_reflink_remap_blocks_error(dest, error, _RET_IP_);
1160 return error;
1161}
1162
1163/*
Darrick J. Wong1364b1d42018-01-18 13:55:20 -08001164 * Grab the exclusive iolock for a data copy from src to dest, making
1165 * sure to abide vfs locking order (lowest pointer value goes first) and
1166 * breaking the pnfs layout leases on dest before proceeding. The loop
1167 * is needed because we cannot call the blocking break_layout() with the
1168 * src iolock held, and therefore have to back out both locks.
1169 */
1170static int
1171xfs_iolock_two_inodes_and_break_layout(
1172 struct inode *src,
1173 struct inode *dest)
1174{
1175 int error;
1176
1177retry:
1178 if (src < dest) {
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001179 inode_lock_shared(src);
Darrick J. Wong1364b1d42018-01-18 13:55:20 -08001180 inode_lock_nested(dest, I_MUTEX_NONDIR2);
1181 } else {
1182 /* src >= dest */
1183 inode_lock(dest);
1184 }
1185
1186 error = break_layout(dest, false);
1187 if (error == -EWOULDBLOCK) {
1188 inode_unlock(dest);
1189 if (src < dest)
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001190 inode_unlock_shared(src);
Darrick J. Wong1364b1d42018-01-18 13:55:20 -08001191 error = break_layout(dest, true);
1192 if (error)
1193 return error;
1194 goto retry;
1195 }
1196 if (error) {
1197 inode_unlock(dest);
1198 if (src < dest)
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001199 inode_unlock_shared(src);
Darrick J. Wong1364b1d42018-01-18 13:55:20 -08001200 return error;
1201 }
1202 if (src > dest)
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001203 inode_lock_shared_nested(src, I_MUTEX_NONDIR2);
Darrick J. Wong1364b1d42018-01-18 13:55:20 -08001204 return 0;
1205}
1206
1207/*
Darrick J. Wong862bb362016-10-03 09:11:40 -07001208 * Link a range of blocks from one file to another.
1209 */
1210int
1211xfs_reflink_remap_range(
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001212 struct file *file_in,
1213 loff_t pos_in,
1214 struct file *file_out,
1215 loff_t pos_out,
1216 u64 len,
1217 bool is_dedupe)
Darrick J. Wong862bb362016-10-03 09:11:40 -07001218{
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001219 struct inode *inode_in = file_inode(file_in);
1220 struct xfs_inode *src = XFS_I(inode_in);
1221 struct inode *inode_out = file_inode(file_out);
1222 struct xfs_inode *dest = XFS_I(inode_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001223 struct xfs_mount *mp = src->i_mount;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001224 bool same_inode = (inode_in == inode_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001225 xfs_fileoff_t sfsbno, dfsbno;
1226 xfs_filblks_t fsblen;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001227 xfs_extlen_t cowextsize;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001228 ssize_t ret;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001229
1230 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1231 return -EOPNOTSUPP;
1232
1233 if (XFS_FORCED_SHUTDOWN(mp))
1234 return -EIO;
1235
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001236 /* Lock both files against IO */
Darrick J. Wong1364b1d42018-01-18 13:55:20 -08001237 ret = xfs_iolock_two_inodes_and_break_layout(inode_in, inode_out);
1238 if (ret)
1239 return ret;
Christoph Hellwig65523212016-11-30 14:33:25 +11001240 if (same_inode)
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001241 xfs_ilock(src, XFS_MMAPLOCK_EXCL);
Christoph Hellwig65523212016-11-30 14:33:25 +11001242 else
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001243 xfs_lock_two_inodes(src, XFS_MMAPLOCK_SHARED, dest,
Darrick J. Wong7c2d2382018-01-26 15:27:33 -08001244 XFS_MMAPLOCK_EXCL);
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001245
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001246 /* Check file eligibility and prepare for block sharing. */
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001247 ret = -EINVAL;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001248 /* Don't reflink realtime inodes */
1249 if (XFS_IS_REALTIME_INODE(src) || XFS_IS_REALTIME_INODE(dest))
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001250 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001251
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001252 /* Don't share DAX file data for now. */
1253 if (IS_DAX(inode_in) || IS_DAX(inode_out))
1254 goto out_unlock;
Darrick J. Wongcc714662016-10-03 09:11:41 -07001255
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001256 ret = vfs_clone_file_prep_inodes(inode_in, pos_in, inode_out, pos_out,
1257 &len, is_dedupe);
Darrick J. Wong22725ce2016-12-19 15:13:26 -08001258 if (ret <= 0)
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001259 goto out_unlock;
1260
Darrick J. Wong09ac8622018-01-19 08:56:04 -08001261 /* Attach dquots to dest inode before changing block map */
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -07001262 ret = xfs_qm_dqattach(dest);
Darrick J. Wong09ac8622018-01-19 08:56:04 -08001263 if (ret)
1264 goto out_unlock;
1265
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001266 trace_xfs_reflink_remap_range(src, pos_in, len, dest, pos_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001267
Darrick J. Wong5c989a02017-12-10 18:03:54 -08001268 /*
1269 * Clear out post-eof preallocations because we don't have page cache
1270 * backing the delayed allocations and they'll never get freed on
1271 * their own.
1272 */
1273 if (xfs_can_free_eofblocks(dest, true)) {
1274 ret = xfs_free_eofblocks(dest);
1275 if (ret)
1276 goto out_unlock;
1277 }
1278
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001279 /* Set flags and remap blocks. */
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001280 ret = xfs_reflink_set_inode_flag(src, dest);
1281 if (ret)
1282 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001283
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001284 dfsbno = XFS_B_TO_FSBT(mp, pos_out);
1285 sfsbno = XFS_B_TO_FSBT(mp, pos_in);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001286 fsblen = XFS_B_TO_FSB(mp, len);
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001287 ret = xfs_reflink_remap_blocks(src, sfsbno, dest, dfsbno, fsblen,
1288 pos_out + len);
1289 if (ret)
1290 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001291
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001292 /* Zap any page cache for the destination file's range. */
1293 truncate_inode_pages_range(&inode_out->i_data, pos_out,
1294 PAGE_ALIGN(pos_out + len) - 1);
1295
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001296 /*
1297 * Carry the cowextsize hint from src to dest if we're sharing the
1298 * entire source file to the entire destination file, the source file
1299 * has a cowextsize hint, and the destination file does not.
1300 */
1301 cowextsize = 0;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001302 if (pos_in == 0 && len == i_size_read(inode_in) &&
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001303 (src->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) &&
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001304 pos_out == 0 && len >= i_size_read(inode_out) &&
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001305 !(dest->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1306 cowextsize = src->i_d.di_cowextsize;
1307
Christoph Hellwigc5ecb422017-02-06 17:45:51 -08001308 ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize,
1309 is_dedupe);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001310
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001311out_unlock:
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001312 xfs_iunlock(dest, XFS_MMAPLOCK_EXCL);
Christoph Hellwig65523212016-11-30 14:33:25 +11001313 if (!same_inode)
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001314 xfs_iunlock(src, XFS_MMAPLOCK_SHARED);
1315 inode_unlock(inode_out);
1316 if (!same_inode)
1317 inode_unlock_shared(inode_in);
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001318 if (ret)
1319 trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_);
1320 return ret;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001321}
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001322
1323/*
1324 * The user wants to preemptively CoW all shared blocks in this file,
1325 * which enables us to turn off the reflink flag. Iterate all
1326 * extents which are not prealloc/delalloc to see which ranges are
1327 * mentioned in the refcount tree, then read those blocks into the
1328 * pagecache, dirty them, fsync them back out, and then we can update
1329 * the inode flag. What happens if we run out of memory? :)
1330 */
1331STATIC int
1332xfs_reflink_dirty_extents(
1333 struct xfs_inode *ip,
1334 xfs_fileoff_t fbno,
1335 xfs_filblks_t end,
1336 xfs_off_t isize)
1337{
1338 struct xfs_mount *mp = ip->i_mount;
1339 xfs_agnumber_t agno;
1340 xfs_agblock_t agbno;
1341 xfs_extlen_t aglen;
1342 xfs_agblock_t rbno;
1343 xfs_extlen_t rlen;
1344 xfs_off_t fpos;
1345 xfs_off_t flen;
1346 struct xfs_bmbt_irec map[2];
1347 int nmaps;
Darrick J. Wong9780643c2016-10-10 16:49:18 +11001348 int error = 0;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001349
1350 while (end - fbno > 0) {
1351 nmaps = 1;
1352 /*
1353 * Look for extents in the file. Skip holes, delalloc, or
1354 * unwritten extents; they can't be reflinked.
1355 */
1356 error = xfs_bmapi_read(ip, fbno, end - fbno, map, &nmaps, 0);
1357 if (error)
1358 goto out;
1359 if (nmaps == 0)
1360 break;
Christoph Hellwig9c4f29d2017-03-28 14:53:35 -07001361 if (!xfs_bmap_is_real_extent(&map[0]))
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001362 goto next;
1363
1364 map[1] = map[0];
1365 while (map[1].br_blockcount) {
1366 agno = XFS_FSB_TO_AGNO(mp, map[1].br_startblock);
1367 agbno = XFS_FSB_TO_AGBNO(mp, map[1].br_startblock);
1368 aglen = map[1].br_blockcount;
1369
Darrick J. Wong92ff7282017-06-16 11:00:10 -07001370 error = xfs_reflink_find_shared(mp, NULL, agno, agbno,
1371 aglen, &rbno, &rlen, true);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001372 if (error)
1373 goto out;
1374 if (rbno == NULLAGBLOCK)
1375 break;
1376
1377 /* Dirty the pages */
1378 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1379 fpos = XFS_FSB_TO_B(mp, map[1].br_startoff +
1380 (rbno - agbno));
1381 flen = XFS_FSB_TO_B(mp, rlen);
1382 if (fpos + flen > isize)
1383 flen = isize - fpos;
1384 error = iomap_file_dirty(VFS_I(ip), fpos, flen,
1385 &xfs_iomap_ops);
1386 xfs_ilock(ip, XFS_ILOCK_EXCL);
1387 if (error)
1388 goto out;
1389
1390 map[1].br_blockcount -= (rbno - agbno + rlen);
1391 map[1].br_startoff += (rbno - agbno + rlen);
1392 map[1].br_startblock += (rbno - agbno + rlen);
1393 }
1394
1395next:
1396 fbno = map[0].br_startoff + map[0].br_blockcount;
1397 }
1398out:
1399 return error;
1400}
1401
Darrick J. Wongea7cdd72017-06-16 11:00:11 -07001402/* Does this inode need the reflink flag? */
1403int
1404xfs_reflink_inode_has_shared_extents(
1405 struct xfs_trans *tp,
1406 struct xfs_inode *ip,
1407 bool *has_shared)
1408{
1409 struct xfs_bmbt_irec got;
1410 struct xfs_mount *mp = ip->i_mount;
1411 struct xfs_ifork *ifp;
1412 xfs_agnumber_t agno;
1413 xfs_agblock_t agbno;
1414 xfs_extlen_t aglen;
1415 xfs_agblock_t rbno;
1416 xfs_extlen_t rlen;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001417 struct xfs_iext_cursor icur;
Darrick J. Wongea7cdd72017-06-16 11:00:11 -07001418 bool found;
1419 int error;
1420
1421 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1422 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1423 error = xfs_iread_extents(tp, ip, XFS_DATA_FORK);
1424 if (error)
1425 return error;
1426 }
1427
1428 *has_shared = false;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001429 found = xfs_iext_lookup_extent(ip, ifp, 0, &icur, &got);
Darrick J. Wongea7cdd72017-06-16 11:00:11 -07001430 while (found) {
1431 if (isnullstartblock(got.br_startblock) ||
1432 got.br_state != XFS_EXT_NORM)
1433 goto next;
1434 agno = XFS_FSB_TO_AGNO(mp, got.br_startblock);
1435 agbno = XFS_FSB_TO_AGBNO(mp, got.br_startblock);
1436 aglen = got.br_blockcount;
1437
1438 error = xfs_reflink_find_shared(mp, tp, agno, agbno, aglen,
1439 &rbno, &rlen, false);
1440 if (error)
1441 return error;
1442 /* Is there still a shared block here? */
1443 if (rbno != NULLAGBLOCK) {
1444 *has_shared = true;
1445 return 0;
1446 }
1447next:
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001448 found = xfs_iext_next_extent(ifp, &icur, &got);
Darrick J. Wongea7cdd72017-06-16 11:00:11 -07001449 }
1450
1451 return 0;
1452}
1453
Dave Chinner844e5e72018-05-09 07:49:10 -07001454/*
1455 * Clear the inode reflink flag if there are no shared extents.
1456 *
1457 * The caller is responsible for joining the inode to the transaction passed in.
1458 * The inode will be joined to the transaction that is returned to the caller.
1459 */
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001460int
1461xfs_reflink_clear_inode_flag(
1462 struct xfs_inode *ip,
1463 struct xfs_trans **tpp)
1464{
Darrick J. Wongea7cdd72017-06-16 11:00:11 -07001465 bool needs_flag;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001466 int error = 0;
1467
Darrick J. Wong63646fc2016-10-10 16:47:32 +11001468 ASSERT(xfs_is_reflink_inode(ip));
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001469
Darrick J. Wongea7cdd72017-06-16 11:00:11 -07001470 error = xfs_reflink_inode_has_shared_extents(*tpp, ip, &needs_flag);
1471 if (error || needs_flag)
1472 return error;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001473
1474 /*
1475 * We didn't find any shared blocks so turn off the reflink flag.
1476 * First, get rid of any leftover CoW mappings.
1477 */
Christoph Hellwig3802a342017-03-07 16:45:58 -08001478 error = xfs_reflink_cancel_cow_blocks(ip, tpp, 0, NULLFILEOFF, true);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001479 if (error)
1480 return error;
1481
1482 /* Clear the inode flag. */
1483 trace_xfs_reflink_unset_inode_flag(ip);
1484 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
Darrick J. Wong83104d42016-10-03 09:11:46 -07001485 xfs_inode_clear_cowblocks_tag(ip);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001486 xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
1487
1488 return error;
1489}
1490
1491/*
1492 * Clear the inode reflink flag if there are no shared extents and the size
1493 * hasn't changed.
1494 */
1495STATIC int
1496xfs_reflink_try_clear_inode_flag(
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001497 struct xfs_inode *ip)
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001498{
1499 struct xfs_mount *mp = ip->i_mount;
1500 struct xfs_trans *tp;
1501 int error = 0;
1502
1503 /* Start a rolling transaction to remove the mappings */
1504 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
1505 if (error)
1506 return error;
1507
1508 xfs_ilock(ip, XFS_ILOCK_EXCL);
1509 xfs_trans_ijoin(tp, ip, 0);
1510
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001511 error = xfs_reflink_clear_inode_flag(ip, &tp);
1512 if (error)
1513 goto cancel;
1514
1515 error = xfs_trans_commit(tp);
1516 if (error)
1517 goto out;
1518
1519 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1520 return 0;
1521cancel:
1522 xfs_trans_cancel(tp);
1523out:
1524 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1525 return error;
1526}
1527
1528/*
1529 * Pre-COW all shared blocks within a given byte range of a file and turn off
1530 * the reflink flag if we unshare all of the file's blocks.
1531 */
1532int
1533xfs_reflink_unshare(
1534 struct xfs_inode *ip,
1535 xfs_off_t offset,
1536 xfs_off_t len)
1537{
1538 struct xfs_mount *mp = ip->i_mount;
1539 xfs_fileoff_t fbno;
1540 xfs_filblks_t end;
1541 xfs_off_t isize;
1542 int error;
1543
1544 if (!xfs_is_reflink_inode(ip))
1545 return 0;
1546
1547 trace_xfs_reflink_unshare(ip, offset, len);
1548
1549 inode_dio_wait(VFS_I(ip));
1550
1551 /* Try to CoW the selected ranges */
1552 xfs_ilock(ip, XFS_ILOCK_EXCL);
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001553 fbno = XFS_B_TO_FSBT(mp, offset);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001554 isize = i_size_read(VFS_I(ip));
1555 end = XFS_B_TO_FSB(mp, offset + len);
1556 error = xfs_reflink_dirty_extents(ip, fbno, end, isize);
1557 if (error)
1558 goto out_unlock;
1559 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1560
1561 /* Wait for the IO to finish */
1562 error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
1563 if (error)
1564 goto out;
1565
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001566 /* Turn off the reflink flag if possible. */
1567 error = xfs_reflink_try_clear_inode_flag(ip);
1568 if (error)
1569 goto out;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001570
1571 return 0;
1572
1573out_unlock:
1574 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1575out:
1576 trace_xfs_reflink_unshare_error(ip, error, _RET_IP_);
1577 return error;
1578}