blob: 90457c2a7569e887d2870b6925c8835e8ad5deff [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
165 xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
166
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. Wong4c1a67b2017-07-17 14:30:51 -0700317 xfs_fsblock_t first_block = NULLFSBLOCK;
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800318 int nimaps = 1;
319
320 if (imap->br_state == XFS_EXT_NORM)
321 return 0;
322
Christoph Hellwigdcf95852017-02-06 10:46:01 -0800323 xfs_trim_extent(imap, offset_fsb, count_fsb);
324 trace_xfs_reflink_convert_cow(ip, imap);
325 if (imap->br_blockcount == 0)
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800326 return 0;
Christoph Hellwigdcf95852017-02-06 10:46:01 -0800327 return xfs_bmapi_write(NULL, ip, imap->br_startoff, imap->br_blockcount,
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800328 XFS_BMAPI_COWFORK | XFS_BMAPI_CONVERT, &first_block,
Brian Foster6e702a52018-07-11 22:26:12 -0700329 0, imap, &nimaps);
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800330}
331
332/* Convert all of the unwritten CoW extents in a file's range to real ones. */
333int
334xfs_reflink_convert_cow(
335 struct xfs_inode *ip,
336 xfs_off_t offset,
337 xfs_off_t count)
338{
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800339 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800340 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
341 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count);
Christoph Hellwigb1214592017-11-03 10:34:44 -0700342 xfs_filblks_t count_fsb = end_fsb - offset_fsb;
343 struct xfs_bmbt_irec imap;
Christoph Hellwigb1214592017-11-03 10:34:44 -0700344 xfs_fsblock_t first_block = NULLFSBLOCK;
345 int nimaps = 1, error = 0;
346
347 ASSERT(count != 0);
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800348
349 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigb1214592017-11-03 10:34:44 -0700350 error = xfs_bmapi_write(NULL, ip, offset_fsb, count_fsb,
351 XFS_BMAPI_COWFORK | XFS_BMAPI_CONVERT |
Brian Foster6e702a52018-07-11 22:26:12 -0700352 XFS_BMAPI_CONVERT_ONLY, &first_block, 0, &imap,
353 &nimaps);
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800354 xfs_iunlock(ip, XFS_ILOCK_EXCL);
355 return error;
356}
357
Darrick J. Wong0613f162016-10-03 09:11:37 -0700358/* Allocate all CoW reservations covering a range of blocks in a file. */
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800359int
360xfs_reflink_allocate_cow(
Darrick J. Wong0613f162016-10-03 09:11:37 -0700361 struct xfs_inode *ip,
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800362 struct xfs_bmbt_irec *imap,
363 bool *shared,
364 uint *lockmode)
Darrick J. Wong0613f162016-10-03 09:11:37 -0700365{
366 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800367 xfs_fileoff_t offset_fsb = imap->br_startoff;
368 xfs_filblks_t count_fsb = imap->br_blockcount;
369 struct xfs_bmbt_irec got;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700370 struct xfs_defer_ops dfops;
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800371 struct xfs_trans *tp = NULL;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700372 xfs_fsblock_t first_block;
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800373 int nimaps, error = 0;
374 bool trimmed;
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800375 xfs_filblks_t resaligned;
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800376 xfs_extlen_t resblks = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700377 struct xfs_iext_cursor icur;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700378
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800379retry:
380 ASSERT(xfs_is_reflink_inode(ip));
Christoph Hellwigc7dbe3f2018-03-13 23:15:31 -0700381 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Darrick J. Wong0613f162016-10-03 09:11:37 -0700382
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800383 /*
384 * Even if the extent is not shared we might have a preallocation for
385 * it in the COW fork. If so use it.
386 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700387 if (xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &got) &&
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800388 got.br_startoff <= offset_fsb) {
389 *shared = true;
390
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800391 /* If we have a real allocation in the COW fork we're done. */
392 if (!isnullstartblock(got.br_startblock)) {
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800393 xfs_trim_extent(&got, offset_fsb, count_fsb);
394 *imap = got;
395 goto convert;
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800396 }
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800397
398 xfs_trim_extent(imap, got.br_startoff, got.br_blockcount);
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800399 } else {
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800400 error = xfs_reflink_trim_around_shared(ip, imap, shared, &trimmed);
401 if (error || !*shared)
402 goto out;
403 }
404
405 if (!tp) {
406 resaligned = xfs_aligned_fsb_count(imap->br_startoff,
407 imap->br_blockcount, xfs_get_cowextsz_hint(ip));
408 resblks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
409
410 xfs_iunlock(ip, *lockmode);
411 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
412 *lockmode = XFS_ILOCK_EXCL;
413 xfs_ilock(ip, *lockmode);
414
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800415 if (error)
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800416 return error;
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100417
Darrick J. Wong4882c192018-05-04 15:30:22 -0700418 error = xfs_qm_dqattach_locked(ip, false);
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800419 if (error)
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800420 goto out;
421 goto retry;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700422 }
423
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800424 error = xfs_trans_reserve_quota_nblks(tp, ip, resblks, 0,
425 XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong0613f162016-10-03 09:11:37 -0700426 if (error)
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800427 goto out;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700428
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800429 xfs_trans_ijoin(tp, ip, 0);
430
431 xfs_defer_init(&dfops, &first_block);
Brian Foster175d1a02018-07-11 22:26:12 -0700432 tp->t_dfops = &dfops;
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800433 nimaps = 1;
434
435 /* Allocate the entire reservation as unwritten blocks. */
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800436 error = xfs_bmapi_write(tp, ip, imap->br_startoff, imap->br_blockcount,
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800437 XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC, &first_block,
Brian Foster6e702a52018-07-11 22:26:12 -0700438 resblks, imap, &nimaps);
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800439 if (error)
440 goto out_bmap_cancel;
441
Darrick J. Wong86d692b2017-12-14 15:46:06 -0800442 xfs_inode_set_cowblocks_tag(ip);
443
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800444 /* Finish up. */
Brian Foster175d1a02018-07-11 22:26:12 -0700445 error = xfs_defer_finish(&tp, tp->t_dfops);
Darrick J. Wong0613f162016-10-03 09:11:37 -0700446 if (error)
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800447 goto out_bmap_cancel;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700448
449 error = xfs_trans_commit(tp);
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800450 if (error)
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800451 return error;
Darrick J. Wong9f37bd12018-01-26 11:37:44 -0800452
453 /*
454 * Allocation succeeded but the requested range was not even partially
455 * satisfied? Bail out!
456 */
457 if (nimaps == 0)
458 return -ENOSPC;
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800459convert:
Brian Foster8a749382018-07-11 22:26:06 -0700460 return xfs_reflink_convert_cow_extent(ip, imap, offset_fsb, count_fsb);
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800461out_bmap_cancel:
Brian Foster175d1a02018-07-11 22:26:12 -0700462 xfs_defer_cancel(tp->t_dfops);
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800463 xfs_trans_unreserve_quota_nblks(tp, ip, (long)resblks, 0,
464 XFS_QMOPT_RES_REGBLKS);
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800465out:
466 if (tp)
467 xfs_trans_cancel(tp);
468 return error;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700469}
470
Darrick J. Wongef473662016-10-03 09:11:34 -0700471/*
Christoph Hellwig3802a342017-03-07 16:45:58 -0800472 * Cancel CoW reservations for some block range of an inode.
473 *
474 * If cancel_real is true this function cancels all COW fork extents for the
475 * inode; if cancel_real is false, real extents are not cleared.
Dave Chinnerc5295c62018-05-09 07:49:09 -0700476 *
477 * Caller must have already joined the inode to the current transaction. The
478 * inode will be joined to the transaction returned to the caller.
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700479 */
480int
481xfs_reflink_cancel_cow_blocks(
482 struct xfs_inode *ip,
483 struct xfs_trans **tpp,
484 xfs_fileoff_t offset_fsb,
Christoph Hellwig3802a342017-03-07 16:45:58 -0800485 xfs_fileoff_t end_fsb,
486 bool cancel_real)
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700487{
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100488 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
Christoph Hellwigdf5ab1b2016-11-24 11:39:50 +1100489 struct xfs_bmbt_irec got, del;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700490 struct xfs_iext_cursor icur;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700491 xfs_fsblock_t firstfsb;
492 struct xfs_defer_ops dfops;
Christoph Hellwigdf5ab1b2016-11-24 11:39:50 +1100493 int error = 0;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700494
495 if (!xfs_is_reflink_inode(ip))
496 return 0;
Christoph Hellwig41caabd2017-11-03 10:34:44 -0700497 if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got))
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100498 return 0;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700499
Christoph Hellwig41caabd2017-11-03 10:34:44 -0700500 /* Walk backwards until we're out of the I/O range... */
501 while (got.br_startoff + got.br_blockcount > offset_fsb) {
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100502 del = got;
503 xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
Christoph Hellwig41caabd2017-11-03 10:34:44 -0700504
505 /* Extent delete may have bumped ext forward */
506 if (!del.br_blockcount) {
507 xfs_iext_prev(ifp, &icur);
508 goto next_extent;
509 }
510
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100511 trace_xfs_reflink_cancel_cow(ip, &del);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700512
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100513 if (isnullstartblock(del.br_startblock)) {
514 error = xfs_bmap_del_extent_delay(ip, XFS_COW_FORK,
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700515 &icur, &got, &del);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700516 if (error)
517 break;
Christoph Hellwig3802a342017-03-07 16:45:58 -0800518 } else if (del.br_state == XFS_EXT_UNWRITTEN || cancel_real) {
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700519 xfs_defer_init(&dfops, &firstfsb);
520
Darrick J. Wong174edb02016-10-03 09:11:39 -0700521 /* Free the CoW orphan record. */
522 error = xfs_refcount_free_cow_extent(ip->i_mount,
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100523 &dfops, del.br_startblock,
524 del.br_blockcount);
Darrick J. Wong174edb02016-10-03 09:11:39 -0700525 if (error)
526 break;
527
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700528 xfs_bmap_add_free(ip->i_mount, &dfops,
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100529 del.br_startblock, del.br_blockcount,
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700530 NULL);
531
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700532 /* Roll the transaction */
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700533 xfs_defer_ijoin(&dfops, ip);
534 error = xfs_defer_finish(tpp, &dfops);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700535 if (error) {
536 xfs_defer_cancel(&dfops);
537 break;
538 }
539
540 /* Remove the mapping from the CoW fork. */
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700541 xfs_bmap_del_extent_cow(ip, &icur, &got, &del);
Darrick J. Wong4b4c1322018-01-19 09:05:48 -0800542
543 /* Remove the quota reservation */
544 error = xfs_trans_reserve_quota_nblks(NULL, ip,
545 -(long)del.br_blockcount, 0,
546 XFS_QMOPT_RES_REGBLKS);
547 if (error)
548 break;
Darrick J. Wong9d40fba2017-12-10 18:03:55 -0800549 } else {
550 /* Didn't do anything, push cursor back. */
551 xfs_iext_prev(ifp, &icur);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700552 }
Christoph Hellwig41caabd2017-11-03 10:34:44 -0700553next_extent:
554 if (!xfs_iext_get_extent(ifp, &icur, &got))
Brian Fosterc17a8ef2016-10-24 14:21:08 +1100555 break;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700556 }
557
Brian Fosterc17a8ef2016-10-24 14:21:08 +1100558 /* clear tag if cow fork is emptied */
559 if (!ifp->if_bytes)
560 xfs_inode_clear_cowblocks_tag(ip);
561
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700562 return error;
563}
564
565/*
Christoph Hellwig3802a342017-03-07 16:45:58 -0800566 * Cancel CoW reservations for some byte range of an inode.
567 *
568 * If cancel_real is true this function cancels all COW fork extents for the
569 * inode; if cancel_real is false, real extents are not cleared.
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700570 */
571int
572xfs_reflink_cancel_cow_range(
573 struct xfs_inode *ip,
574 xfs_off_t offset,
Christoph Hellwig3802a342017-03-07 16:45:58 -0800575 xfs_off_t count,
576 bool cancel_real)
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700577{
578 struct xfs_trans *tp;
579 xfs_fileoff_t offset_fsb;
580 xfs_fileoff_t end_fsb;
581 int error;
582
583 trace_xfs_reflink_cancel_cow_range(ip, offset, count);
Darrick J. Wong63646fc2016-10-10 16:47:32 +1100584 ASSERT(xfs_is_reflink_inode(ip));
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700585
586 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
587 if (count == NULLFILEOFF)
588 end_fsb = NULLFILEOFF;
589 else
590 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
591
592 /* Start a rolling transaction to remove the mappings */
593 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
Dave Chinner4df0f7f2018-03-06 17:07:22 -0800594 0, 0, XFS_TRANS_NOFS, &tp);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700595 if (error)
596 goto out;
597
598 xfs_ilock(ip, XFS_ILOCK_EXCL);
599 xfs_trans_ijoin(tp, ip, 0);
600
601 /* Scrape out the old CoW reservations */
Christoph Hellwig3802a342017-03-07 16:45:58 -0800602 error = xfs_reflink_cancel_cow_blocks(ip, &tp, offset_fsb, end_fsb,
603 cancel_real);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700604 if (error)
605 goto out_cancel;
606
607 error = xfs_trans_commit(tp);
608
609 xfs_iunlock(ip, XFS_ILOCK_EXCL);
610 return error;
611
612out_cancel:
613 xfs_trans_cancel(tp);
614 xfs_iunlock(ip, XFS_ILOCK_EXCL);
615out:
616 trace_xfs_reflink_cancel_cow_range_error(ip, error, _RET_IP_);
617 return error;
618}
619
620/*
621 * Remap parts of a file's data fork after a successful CoW.
622 */
623int
624xfs_reflink_end_cow(
625 struct xfs_inode *ip,
626 xfs_off_t offset,
627 xfs_off_t count)
628{
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100629 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
Christoph Hellwig4ab86712016-11-24 11:39:50 +1100630 struct xfs_bmbt_irec got, del;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700631 struct xfs_trans *tp;
632 xfs_fileoff_t offset_fsb;
633 xfs_fileoff_t end_fsb;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700634 xfs_fsblock_t firstfsb;
635 struct xfs_defer_ops dfops;
Christoph Hellwig4ab86712016-11-24 11:39:50 +1100636 int error;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700637 unsigned int resblks;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700638 xfs_filblks_t rlen;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700639 struct xfs_iext_cursor icur;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700640
641 trace_xfs_reflink_end_cow(ip, offset, count);
642
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100643 /* No COW extents? That's easy! */
644 if (ifp->if_bytes == 0)
645 return 0;
646
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700647 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
648 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700649
Darrick J. Wongfe0be232017-04-12 12:26:07 -0700650 /*
651 * Start a rolling transaction to switch the mappings. We're
652 * unlikely ever to have to remap 16T worth of single-block
653 * extents, so just cap the worst case extent count to 2^32-1.
654 * Stick a warning in just in case, and avoid 64-bit division.
655 */
656 BUILD_BUG_ON(MAX_RW_COUNT > UINT_MAX);
657 if (end_fsb - offset_fsb > UINT_MAX) {
658 error = -EFSCORRUPTED;
659 xfs_force_shutdown(ip->i_mount, SHUTDOWN_CORRUPT_INCORE);
660 ASSERT(0);
661 goto out;
662 }
663 resblks = XFS_NEXTENTADD_SPACE_RES(ip->i_mount,
664 (unsigned int)(end_fsb - offset_fsb),
665 XFS_DATA_FORK);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700666 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
Dave Chinner4df0f7f2018-03-06 17:07:22 -0800667 resblks, 0, XFS_TRANS_RESERVE | XFS_TRANS_NOFS, &tp);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700668 if (error)
669 goto out;
670
671 xfs_ilock(ip, XFS_ILOCK_EXCL);
672 xfs_trans_ijoin(tp, ip, 0);
673
Christoph Hellwigdc560152017-10-23 16:32:39 -0700674 /*
675 * In case of racing, overlapping AIO writes no COW extents might be
676 * left by the time I/O completes for the loser of the race. In that
677 * case we are done.
678 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700679 if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got))
Christoph Hellwigdc560152017-10-23 16:32:39 -0700680 goto out_cancel;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700681
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100682 /* Walk backwards until we're out of the I/O range... */
683 while (got.br_startoff + got.br_blockcount > offset_fsb) {
684 del = got;
685 xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
686
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700687 /* Extent delete may have bumped ext forward */
Christoph Hellwigdf79b812018-03-13 23:15:33 -0700688 if (!del.br_blockcount)
689 goto prev_extent;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700690
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100691 ASSERT(!isnullstartblock(got.br_startblock));
692
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800693 /*
694 * Don't remap unwritten extents; these are
695 * speculatively preallocated CoW extents that have been
696 * allocated but have not yet been involved in a write.
697 */
Christoph Hellwigdf79b812018-03-13 23:15:33 -0700698 if (got.br_state == XFS_EXT_UNWRITTEN)
699 goto prev_extent;
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800700
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100701 /* Unmap the old blocks in the data fork. */
702 xfs_defer_init(&dfops, &firstfsb);
Brian Foster4bcfa612018-07-11 22:26:13 -0700703 tp->t_dfops = &dfops;
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100704 rlen = del.br_blockcount;
705 error = __xfs_bunmapi(tp, ip, del.br_startoff, &rlen, 0, 1,
Brian Fosterccd9d912018-07-11 22:26:13 -0700706 &firstfsb);
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100707 if (error)
708 goto out_defer;
709
710 /* Trim the extent to whatever got unmapped. */
711 if (rlen) {
712 xfs_trim_extent(&del, del.br_startoff + rlen,
713 del.br_blockcount - rlen);
714 }
715 trace_xfs_reflink_cow_remap(ip, &del);
716
717 /* Free the CoW orphan record. */
Brian Foster4bcfa612018-07-11 22:26:13 -0700718 error = xfs_refcount_free_cow_extent(tp->t_mountp, tp->t_dfops,
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100719 del.br_startblock, del.br_blockcount);
720 if (error)
721 goto out_defer;
722
723 /* Map the new blocks into the data fork. */
Brian Foster4bcfa612018-07-11 22:26:13 -0700724 error = xfs_bmap_map_extent(tp->t_mountp, tp->t_dfops, ip,
725 &del);
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100726 if (error)
727 goto out_defer;
728
Darrick J. Wong4b4c1322018-01-19 09:05:48 -0800729 /* Charge this new data fork mapping to the on-disk quota. */
730 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_DELBCOUNT,
731 (long)del.br_blockcount);
732
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100733 /* Remove the mapping from the CoW fork. */
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700734 xfs_bmap_del_extent_cow(ip, &icur, &got, &del);
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100735
Brian Foster4bcfa612018-07-11 22:26:13 -0700736 xfs_defer_ijoin(tp->t_dfops, ip);
737 error = xfs_defer_finish(&tp, tp->t_dfops);
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100738 if (error)
739 goto out_defer;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700740 if (!xfs_iext_get_extent(ifp, &icur, &got))
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100741 break;
Christoph Hellwigdf79b812018-03-13 23:15:33 -0700742 continue;
743prev_extent:
744 if (!xfs_iext_prev_extent(ifp, &icur, &got))
745 break;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700746 }
747
748 error = xfs_trans_commit(tp);
749 xfs_iunlock(ip, XFS_ILOCK_EXCL);
750 if (error)
751 goto out;
752 return 0;
753
754out_defer:
Brian Foster4bcfa612018-07-11 22:26:13 -0700755 xfs_defer_cancel(tp->t_dfops);
Christoph Hellwige12199f2017-10-03 08:58:33 -0700756out_cancel:
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700757 xfs_trans_cancel(tp);
758 xfs_iunlock(ip, XFS_ILOCK_EXCL);
759out:
760 trace_xfs_reflink_end_cow_error(ip, error, _RET_IP_);
761 return error;
762}
Darrick J. Wong174edb02016-10-03 09:11:39 -0700763
764/*
765 * Free leftover CoW reservations that didn't get cleaned out.
766 */
767int
768xfs_reflink_recover_cow(
769 struct xfs_mount *mp)
770{
771 xfs_agnumber_t agno;
772 int error = 0;
773
774 if (!xfs_sb_version_hasreflink(&mp->m_sb))
775 return 0;
776
777 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
778 error = xfs_refcount_recover_cow_leftovers(mp, agno);
779 if (error)
780 break;
781 }
782
783 return error;
784}
Darrick J. Wong862bb362016-10-03 09:11:40 -0700785
786/*
787 * Reflinking (Block) Ranges of Two Files Together
788 *
789 * First, ensure that the reflink flag is set on both inodes. The flag is an
790 * optimization to avoid unnecessary refcount btree lookups in the write path.
791 *
792 * Now we can iteratively remap the range of extents (and holes) in src to the
793 * corresponding ranges in dest. Let drange and srange denote the ranges of
794 * logical blocks in dest and src touched by the reflink operation.
795 *
796 * While the length of drange is greater than zero,
797 * - Read src's bmbt at the start of srange ("imap")
798 * - If imap doesn't exist, make imap appear to start at the end of srange
799 * with zero length.
800 * - If imap starts before srange, advance imap to start at srange.
801 * - If imap goes beyond srange, truncate imap to end at the end of srange.
802 * - Punch (imap start - srange start + imap len) blocks from dest at
803 * offset (drange start).
804 * - If imap points to a real range of pblks,
805 * > Increase the refcount of the imap's pblks
806 * > Map imap's pblks into dest at the offset
807 * (drange start + imap start - srange start)
808 * - Advance drange and srange by (imap start - srange start + imap len)
809 *
810 * Finally, if the reflink made dest longer, update both the in-core and
811 * on-disk file sizes.
812 *
813 * ASCII Art Demonstration:
814 *
815 * Let's say we want to reflink this source file:
816 *
817 * ----SSSSSSS-SSSSS----SSSSSS (src file)
818 * <-------------------->
819 *
820 * into this destination file:
821 *
822 * --DDDDDDDDDDDDDDDDDDD--DDD (dest file)
823 * <-------------------->
824 * '-' means a hole, and 'S' and 'D' are written blocks in the src and dest.
825 * Observe that the range has different logical offsets in either file.
826 *
827 * Consider that the first extent in the source file doesn't line up with our
828 * reflink range. Unmapping and remapping are separate operations, so we can
829 * unmap more blocks from the destination file than we remap.
830 *
831 * ----SSSSSSS-SSSSS----SSSSSS
832 * <------->
833 * --DDDDD---------DDDDD--DDD
834 * <------->
835 *
836 * Now remap the source extent into the destination file:
837 *
838 * ----SSSSSSS-SSSSS----SSSSSS
839 * <------->
840 * --DDDDD--SSSSSSSDDDDD--DDD
841 * <------->
842 *
843 * Do likewise with the second hole and extent in our range. Holes in the
844 * unmap range don't affect our operation.
845 *
846 * ----SSSSSSS-SSSSS----SSSSSS
847 * <---->
848 * --DDDDD--SSSSSSS-SSSSS-DDD
849 * <---->
850 *
851 * Finally, unmap and remap part of the third extent. This will increase the
852 * size of the destination file.
853 *
854 * ----SSSSSSS-SSSSS----SSSSSS
855 * <----->
856 * --DDDDD--SSSSSSS-SSSSS----SSS
857 * <----->
858 *
859 * Once we update the destination file's i_size, we're done.
860 */
861
862/*
863 * Ensure the reflink bit is set in both inodes.
864 */
865STATIC int
866xfs_reflink_set_inode_flag(
867 struct xfs_inode *src,
868 struct xfs_inode *dest)
869{
870 struct xfs_mount *mp = src->i_mount;
871 int error;
872 struct xfs_trans *tp;
873
874 if (xfs_is_reflink_inode(src) && xfs_is_reflink_inode(dest))
875 return 0;
876
877 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
878 if (error)
879 goto out_error;
880
881 /* Lock both files against IO */
882 if (src->i_ino == dest->i_ino)
883 xfs_ilock(src, XFS_ILOCK_EXCL);
884 else
Darrick J. Wong7c2d2382018-01-26 15:27:33 -0800885 xfs_lock_two_inodes(src, XFS_ILOCK_EXCL, dest, XFS_ILOCK_EXCL);
Darrick J. Wong862bb362016-10-03 09:11:40 -0700886
887 if (!xfs_is_reflink_inode(src)) {
888 trace_xfs_reflink_set_inode_flag(src);
889 xfs_trans_ijoin(tp, src, XFS_ILOCK_EXCL);
890 src->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
891 xfs_trans_log_inode(tp, src, XFS_ILOG_CORE);
892 xfs_ifork_init_cow(src);
893 } else
894 xfs_iunlock(src, XFS_ILOCK_EXCL);
895
896 if (src->i_ino == dest->i_ino)
897 goto commit_flags;
898
899 if (!xfs_is_reflink_inode(dest)) {
900 trace_xfs_reflink_set_inode_flag(dest);
901 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
902 dest->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
903 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
904 xfs_ifork_init_cow(dest);
905 } else
906 xfs_iunlock(dest, XFS_ILOCK_EXCL);
907
908commit_flags:
909 error = xfs_trans_commit(tp);
910 if (error)
911 goto out_error;
912 return error;
913
914out_error:
915 trace_xfs_reflink_set_inode_flag_error(dest, error, _RET_IP_);
916 return error;
917}
918
919/*
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700920 * Update destination inode size & cowextsize hint, if necessary.
Darrick J. Wong862bb362016-10-03 09:11:40 -0700921 */
922STATIC int
923xfs_reflink_update_dest(
924 struct xfs_inode *dest,
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700925 xfs_off_t newlen,
Christoph Hellwigc5ecb422017-02-06 17:45:51 -0800926 xfs_extlen_t cowextsize,
927 bool is_dedupe)
Darrick J. Wong862bb362016-10-03 09:11:40 -0700928{
929 struct xfs_mount *mp = dest->i_mount;
930 struct xfs_trans *tp;
931 int error;
932
Christoph Hellwigc5ecb422017-02-06 17:45:51 -0800933 if (is_dedupe && newlen <= i_size_read(VFS_I(dest)) && cowextsize == 0)
Darrick J. Wong862bb362016-10-03 09:11:40 -0700934 return 0;
935
936 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
937 if (error)
938 goto out_error;
939
940 xfs_ilock(dest, XFS_ILOCK_EXCL);
941 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
942
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700943 if (newlen > i_size_read(VFS_I(dest))) {
944 trace_xfs_reflink_update_inode_size(dest, newlen);
945 i_size_write(VFS_I(dest), newlen);
946 dest->i_d.di_size = newlen;
947 }
948
949 if (cowextsize) {
950 dest->i_d.di_cowextsize = cowextsize;
951 dest->i_d.di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
952 }
953
Christoph Hellwigc5ecb422017-02-06 17:45:51 -0800954 if (!is_dedupe) {
955 xfs_trans_ichgtime(tp, dest,
956 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
957 }
Darrick J. Wong862bb362016-10-03 09:11:40 -0700958 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
959
960 error = xfs_trans_commit(tp);
961 if (error)
962 goto out_error;
963 return error;
964
965out_error:
966 trace_xfs_reflink_update_inode_size_error(dest, error, _RET_IP_);
967 return error;
968}
969
970/*
Darrick J. Wong6fa164b2016-10-03 09:11:45 -0700971 * Do we have enough reserve in this AG to handle a reflink? The refcount
972 * btree already reserved all the space it needs, but the rmap btree can grow
973 * infinitely, so we won't allow more reflinks when the AG is down to the
974 * btree reserves.
975 */
976static int
977xfs_reflink_ag_has_free_space(
978 struct xfs_mount *mp,
979 xfs_agnumber_t agno)
980{
981 struct xfs_perag *pag;
982 int error = 0;
983
984 if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
985 return 0;
986
987 pag = xfs_perag_get(mp, agno);
Brian Foster21592862018-03-09 14:01:59 -0800988 if (xfs_ag_resv_critical(pag, XFS_AG_RESV_RMAPBT) ||
Darrick J. Wong6fa164b2016-10-03 09:11:45 -0700989 xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA))
990 error = -ENOSPC;
991 xfs_perag_put(pag);
992 return error;
993}
994
995/*
Darrick J. Wong862bb362016-10-03 09:11:40 -0700996 * Unmap a range of blocks from a file, then map other blocks into the hole.
997 * The range to unmap is (destoff : destoff + srcioff + irec->br_blockcount).
998 * The extent irec is mapped into dest at irec->br_startoff.
999 */
1000STATIC int
1001xfs_reflink_remap_extent(
1002 struct xfs_inode *ip,
1003 struct xfs_bmbt_irec *irec,
1004 xfs_fileoff_t destoff,
1005 xfs_off_t new_isize)
1006{
1007 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig9c4f29d2017-03-28 14:53:35 -07001008 bool real_extent = xfs_bmap_is_real_extent(irec);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001009 struct xfs_trans *tp;
1010 xfs_fsblock_t firstfsb;
1011 unsigned int resblks;
1012 struct xfs_defer_ops dfops;
1013 struct xfs_bmbt_irec uirec;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001014 xfs_filblks_t rlen;
1015 xfs_filblks_t unmap_len;
1016 xfs_off_t newlen;
1017 int error;
1018
1019 unmap_len = irec->br_startoff + irec->br_blockcount - destoff;
1020 trace_xfs_reflink_punch_range(ip, destoff, unmap_len);
1021
Darrick J. Wong6fa164b2016-10-03 09:11:45 -07001022 /* No reflinking if we're low on space */
1023 if (real_extent) {
1024 error = xfs_reflink_ag_has_free_space(mp,
1025 XFS_FSB_TO_AGNO(mp, irec->br_startblock));
1026 if (error)
1027 goto out;
1028 }
1029
Darrick J. Wong862bb362016-10-03 09:11:40 -07001030 /* Start a rolling transaction to switch the mappings */
1031 resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
1032 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
1033 if (error)
1034 goto out;
1035
1036 xfs_ilock(ip, XFS_ILOCK_EXCL);
1037 xfs_trans_ijoin(tp, ip, 0);
1038
1039 /* If we're not just clearing space, then do we have enough quota? */
1040 if (real_extent) {
1041 error = xfs_trans_reserve_quota_nblks(tp, ip,
1042 irec->br_blockcount, 0, XFS_QMOPT_RES_REGBLKS);
1043 if (error)
1044 goto out_cancel;
1045 }
1046
1047 trace_xfs_reflink_remap(ip, irec->br_startoff,
1048 irec->br_blockcount, irec->br_startblock);
1049
1050 /* Unmap the old blocks in the data fork. */
1051 rlen = unmap_len;
1052 while (rlen) {
1053 xfs_defer_init(&dfops, &firstfsb);
Brian Foster4bcfa612018-07-11 22:26:13 -07001054 tp->t_dfops = &dfops;
Brian Fosterccd9d912018-07-11 22:26:13 -07001055 error = __xfs_bunmapi(tp, ip, destoff, &rlen, 0, 1, &firstfsb);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001056 if (error)
1057 goto out_defer;
1058
1059 /*
1060 * Trim the extent to whatever got unmapped.
1061 * Remember, bunmapi works backwards.
1062 */
1063 uirec.br_startblock = irec->br_startblock + rlen;
1064 uirec.br_startoff = irec->br_startoff + rlen;
1065 uirec.br_blockcount = unmap_len - rlen;
1066 unmap_len = rlen;
1067
1068 /* If this isn't a real mapping, we're done. */
1069 if (!real_extent || uirec.br_blockcount == 0)
1070 goto next_extent;
1071
1072 trace_xfs_reflink_remap(ip, uirec.br_startoff,
1073 uirec.br_blockcount, uirec.br_startblock);
1074
1075 /* Update the refcount tree */
Brian Foster4bcfa612018-07-11 22:26:13 -07001076 error = xfs_refcount_increase_extent(mp, tp->t_dfops, &uirec);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001077 if (error)
1078 goto out_defer;
1079
1080 /* Map the new blocks into the data fork. */
Brian Foster4bcfa612018-07-11 22:26:13 -07001081 error = xfs_bmap_map_extent(mp, tp->t_dfops, ip, &uirec);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001082 if (error)
1083 goto out_defer;
1084
1085 /* Update quota accounting. */
1086 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT,
1087 uirec.br_blockcount);
1088
1089 /* Update dest isize if needed. */
1090 newlen = XFS_FSB_TO_B(mp,
1091 uirec.br_startoff + uirec.br_blockcount);
1092 newlen = min_t(xfs_off_t, newlen, new_isize);
1093 if (newlen > i_size_read(VFS_I(ip))) {
1094 trace_xfs_reflink_update_inode_size(ip, newlen);
1095 i_size_write(VFS_I(ip), newlen);
1096 ip->i_d.di_size = newlen;
1097 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1098 }
1099
1100next_extent:
1101 /* Process all the deferred stuff. */
Brian Foster4bcfa612018-07-11 22:26:13 -07001102 xfs_defer_ijoin(tp->t_dfops, ip);
1103 error = xfs_defer_finish(&tp, tp->t_dfops);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001104 if (error)
1105 goto out_defer;
1106 }
1107
1108 error = xfs_trans_commit(tp);
1109 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1110 if (error)
1111 goto out;
1112 return 0;
1113
1114out_defer:
Brian Foster4bcfa612018-07-11 22:26:13 -07001115 xfs_defer_cancel(tp->t_dfops);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001116out_cancel:
1117 xfs_trans_cancel(tp);
1118 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1119out:
1120 trace_xfs_reflink_remap_extent_error(ip, error, _RET_IP_);
1121 return error;
1122}
1123
1124/*
1125 * Iteratively remap one file's extents (and holes) to another's.
1126 */
1127STATIC int
1128xfs_reflink_remap_blocks(
1129 struct xfs_inode *src,
1130 xfs_fileoff_t srcoff,
1131 struct xfs_inode *dest,
1132 xfs_fileoff_t destoff,
1133 xfs_filblks_t len,
1134 xfs_off_t new_isize)
1135{
1136 struct xfs_bmbt_irec imap;
1137 int nimaps;
1138 int error = 0;
1139 xfs_filblks_t range_len;
1140
1141 /* drange = (destoff, destoff + len); srange = (srcoff, srcoff + len) */
1142 while (len) {
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001143 uint lock_mode;
1144
Darrick J. Wong862bb362016-10-03 09:11:40 -07001145 trace_xfs_reflink_remap_blocks_loop(src, srcoff, len,
1146 dest, destoff);
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001147
Darrick J. Wong862bb362016-10-03 09:11:40 -07001148 /* Read extent from the source file */
1149 nimaps = 1;
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001150 lock_mode = xfs_ilock_data_map_shared(src);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001151 error = xfs_bmapi_read(src, srcoff, len, &imap, &nimaps, 0);
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001152 xfs_iunlock(src, lock_mode);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001153 if (error)
1154 goto err;
1155 ASSERT(nimaps == 1);
1156
1157 trace_xfs_reflink_remap_imap(src, srcoff, len, XFS_IO_OVERWRITE,
1158 &imap);
1159
1160 /* Translate imap into the destination file. */
1161 range_len = imap.br_startoff + imap.br_blockcount - srcoff;
1162 imap.br_startoff += destoff - srcoff;
1163
1164 /* Clear dest from destoff to the end of imap and map it in. */
1165 error = xfs_reflink_remap_extent(dest, &imap, destoff,
1166 new_isize);
1167 if (error)
1168 goto err;
1169
1170 if (fatal_signal_pending(current)) {
1171 error = -EINTR;
1172 goto err;
1173 }
1174
1175 /* Advance drange/srange */
1176 srcoff += range_len;
1177 destoff += range_len;
1178 len -= range_len;
1179 }
1180
1181 return 0;
1182
1183err:
1184 trace_xfs_reflink_remap_blocks_error(dest, error, _RET_IP_);
1185 return error;
1186}
1187
1188/*
Darrick J. Wong1364b1d42018-01-18 13:55:20 -08001189 * Grab the exclusive iolock for a data copy from src to dest, making
1190 * sure to abide vfs locking order (lowest pointer value goes first) and
1191 * breaking the pnfs layout leases on dest before proceeding. The loop
1192 * is needed because we cannot call the blocking break_layout() with the
1193 * src iolock held, and therefore have to back out both locks.
1194 */
1195static int
1196xfs_iolock_two_inodes_and_break_layout(
1197 struct inode *src,
1198 struct inode *dest)
1199{
1200 int error;
1201
1202retry:
1203 if (src < dest) {
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001204 inode_lock_shared(src);
Darrick J. Wong1364b1d42018-01-18 13:55:20 -08001205 inode_lock_nested(dest, I_MUTEX_NONDIR2);
1206 } else {
1207 /* src >= dest */
1208 inode_lock(dest);
1209 }
1210
1211 error = break_layout(dest, false);
1212 if (error == -EWOULDBLOCK) {
1213 inode_unlock(dest);
1214 if (src < dest)
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001215 inode_unlock_shared(src);
Darrick J. Wong1364b1d42018-01-18 13:55:20 -08001216 error = break_layout(dest, true);
1217 if (error)
1218 return error;
1219 goto retry;
1220 }
1221 if (error) {
1222 inode_unlock(dest);
1223 if (src < dest)
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001224 inode_unlock_shared(src);
Darrick J. Wong1364b1d42018-01-18 13:55:20 -08001225 return error;
1226 }
1227 if (src > dest)
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001228 inode_lock_shared_nested(src, I_MUTEX_NONDIR2);
Darrick J. Wong1364b1d42018-01-18 13:55:20 -08001229 return 0;
1230}
1231
1232/*
Darrick J. Wong862bb362016-10-03 09:11:40 -07001233 * Link a range of blocks from one file to another.
1234 */
1235int
1236xfs_reflink_remap_range(
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001237 struct file *file_in,
1238 loff_t pos_in,
1239 struct file *file_out,
1240 loff_t pos_out,
1241 u64 len,
1242 bool is_dedupe)
Darrick J. Wong862bb362016-10-03 09:11:40 -07001243{
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001244 struct inode *inode_in = file_inode(file_in);
1245 struct xfs_inode *src = XFS_I(inode_in);
1246 struct inode *inode_out = file_inode(file_out);
1247 struct xfs_inode *dest = XFS_I(inode_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001248 struct xfs_mount *mp = src->i_mount;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001249 bool same_inode = (inode_in == inode_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001250 xfs_fileoff_t sfsbno, dfsbno;
1251 xfs_filblks_t fsblen;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001252 xfs_extlen_t cowextsize;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001253 ssize_t ret;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001254
1255 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1256 return -EOPNOTSUPP;
1257
1258 if (XFS_FORCED_SHUTDOWN(mp))
1259 return -EIO;
1260
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001261 /* Lock both files against IO */
Darrick J. Wong1364b1d42018-01-18 13:55:20 -08001262 ret = xfs_iolock_two_inodes_and_break_layout(inode_in, inode_out);
1263 if (ret)
1264 return ret;
Christoph Hellwig65523212016-11-30 14:33:25 +11001265 if (same_inode)
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001266 xfs_ilock(src, XFS_MMAPLOCK_EXCL);
Christoph Hellwig65523212016-11-30 14:33:25 +11001267 else
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001268 xfs_lock_two_inodes(src, XFS_MMAPLOCK_SHARED, dest,
Darrick J. Wong7c2d2382018-01-26 15:27:33 -08001269 XFS_MMAPLOCK_EXCL);
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001270
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001271 /* Check file eligibility and prepare for block sharing. */
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001272 ret = -EINVAL;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001273 /* Don't reflink realtime inodes */
1274 if (XFS_IS_REALTIME_INODE(src) || XFS_IS_REALTIME_INODE(dest))
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001275 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001276
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001277 /* Don't share DAX file data for now. */
1278 if (IS_DAX(inode_in) || IS_DAX(inode_out))
1279 goto out_unlock;
Darrick J. Wongcc714662016-10-03 09:11:41 -07001280
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001281 ret = vfs_clone_file_prep_inodes(inode_in, pos_in, inode_out, pos_out,
1282 &len, is_dedupe);
Darrick J. Wong22725ce2016-12-19 15:13:26 -08001283 if (ret <= 0)
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001284 goto out_unlock;
1285
Darrick J. Wong09ac8622018-01-19 08:56:04 -08001286 /* Attach dquots to dest inode before changing block map */
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -07001287 ret = xfs_qm_dqattach(dest);
Darrick J. Wong09ac8622018-01-19 08:56:04 -08001288 if (ret)
1289 goto out_unlock;
1290
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001291 trace_xfs_reflink_remap_range(src, pos_in, len, dest, pos_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001292
Darrick J. Wong5c989a02017-12-10 18:03:54 -08001293 /*
1294 * Clear out post-eof preallocations because we don't have page cache
1295 * backing the delayed allocations and they'll never get freed on
1296 * their own.
1297 */
1298 if (xfs_can_free_eofblocks(dest, true)) {
1299 ret = xfs_free_eofblocks(dest);
1300 if (ret)
1301 goto out_unlock;
1302 }
1303
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001304 /* Set flags and remap blocks. */
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001305 ret = xfs_reflink_set_inode_flag(src, dest);
1306 if (ret)
1307 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001308
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001309 dfsbno = XFS_B_TO_FSBT(mp, pos_out);
1310 sfsbno = XFS_B_TO_FSBT(mp, pos_in);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001311 fsblen = XFS_B_TO_FSB(mp, len);
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001312 ret = xfs_reflink_remap_blocks(src, sfsbno, dest, dfsbno, fsblen,
1313 pos_out + len);
1314 if (ret)
1315 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001316
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001317 /* Zap any page cache for the destination file's range. */
1318 truncate_inode_pages_range(&inode_out->i_data, pos_out,
1319 PAGE_ALIGN(pos_out + len) - 1);
1320
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001321 /*
1322 * Carry the cowextsize hint from src to dest if we're sharing the
1323 * entire source file to the entire destination file, the source file
1324 * has a cowextsize hint, and the destination file does not.
1325 */
1326 cowextsize = 0;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001327 if (pos_in == 0 && len == i_size_read(inode_in) &&
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001328 (src->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) &&
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001329 pos_out == 0 && len >= i_size_read(inode_out) &&
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001330 !(dest->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1331 cowextsize = src->i_d.di_cowextsize;
1332
Christoph Hellwigc5ecb422017-02-06 17:45:51 -08001333 ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize,
1334 is_dedupe);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001335
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001336out_unlock:
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001337 xfs_iunlock(dest, XFS_MMAPLOCK_EXCL);
Christoph Hellwig65523212016-11-30 14:33:25 +11001338 if (!same_inode)
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001339 xfs_iunlock(src, XFS_MMAPLOCK_SHARED);
1340 inode_unlock(inode_out);
1341 if (!same_inode)
1342 inode_unlock_shared(inode_in);
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001343 if (ret)
1344 trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_);
1345 return ret;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001346}
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001347
1348/*
1349 * The user wants to preemptively CoW all shared blocks in this file,
1350 * which enables us to turn off the reflink flag. Iterate all
1351 * extents which are not prealloc/delalloc to see which ranges are
1352 * mentioned in the refcount tree, then read those blocks into the
1353 * pagecache, dirty them, fsync them back out, and then we can update
1354 * the inode flag. What happens if we run out of memory? :)
1355 */
1356STATIC int
1357xfs_reflink_dirty_extents(
1358 struct xfs_inode *ip,
1359 xfs_fileoff_t fbno,
1360 xfs_filblks_t end,
1361 xfs_off_t isize)
1362{
1363 struct xfs_mount *mp = ip->i_mount;
1364 xfs_agnumber_t agno;
1365 xfs_agblock_t agbno;
1366 xfs_extlen_t aglen;
1367 xfs_agblock_t rbno;
1368 xfs_extlen_t rlen;
1369 xfs_off_t fpos;
1370 xfs_off_t flen;
1371 struct xfs_bmbt_irec map[2];
1372 int nmaps;
Darrick J. Wong9780643c2016-10-10 16:49:18 +11001373 int error = 0;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001374
1375 while (end - fbno > 0) {
1376 nmaps = 1;
1377 /*
1378 * Look for extents in the file. Skip holes, delalloc, or
1379 * unwritten extents; they can't be reflinked.
1380 */
1381 error = xfs_bmapi_read(ip, fbno, end - fbno, map, &nmaps, 0);
1382 if (error)
1383 goto out;
1384 if (nmaps == 0)
1385 break;
Christoph Hellwig9c4f29d2017-03-28 14:53:35 -07001386 if (!xfs_bmap_is_real_extent(&map[0]))
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001387 goto next;
1388
1389 map[1] = map[0];
1390 while (map[1].br_blockcount) {
1391 agno = XFS_FSB_TO_AGNO(mp, map[1].br_startblock);
1392 agbno = XFS_FSB_TO_AGBNO(mp, map[1].br_startblock);
1393 aglen = map[1].br_blockcount;
1394
Darrick J. Wong92ff7282017-06-16 11:00:10 -07001395 error = xfs_reflink_find_shared(mp, NULL, agno, agbno,
1396 aglen, &rbno, &rlen, true);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001397 if (error)
1398 goto out;
1399 if (rbno == NULLAGBLOCK)
1400 break;
1401
1402 /* Dirty the pages */
1403 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1404 fpos = XFS_FSB_TO_B(mp, map[1].br_startoff +
1405 (rbno - agbno));
1406 flen = XFS_FSB_TO_B(mp, rlen);
1407 if (fpos + flen > isize)
1408 flen = isize - fpos;
1409 error = iomap_file_dirty(VFS_I(ip), fpos, flen,
1410 &xfs_iomap_ops);
1411 xfs_ilock(ip, XFS_ILOCK_EXCL);
1412 if (error)
1413 goto out;
1414
1415 map[1].br_blockcount -= (rbno - agbno + rlen);
1416 map[1].br_startoff += (rbno - agbno + rlen);
1417 map[1].br_startblock += (rbno - agbno + rlen);
1418 }
1419
1420next:
1421 fbno = map[0].br_startoff + map[0].br_blockcount;
1422 }
1423out:
1424 return error;
1425}
1426
Darrick J. Wongea7cdd72017-06-16 11:00:11 -07001427/* Does this inode need the reflink flag? */
1428int
1429xfs_reflink_inode_has_shared_extents(
1430 struct xfs_trans *tp,
1431 struct xfs_inode *ip,
1432 bool *has_shared)
1433{
1434 struct xfs_bmbt_irec got;
1435 struct xfs_mount *mp = ip->i_mount;
1436 struct xfs_ifork *ifp;
1437 xfs_agnumber_t agno;
1438 xfs_agblock_t agbno;
1439 xfs_extlen_t aglen;
1440 xfs_agblock_t rbno;
1441 xfs_extlen_t rlen;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001442 struct xfs_iext_cursor icur;
Darrick J. Wongea7cdd72017-06-16 11:00:11 -07001443 bool found;
1444 int error;
1445
1446 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1447 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1448 error = xfs_iread_extents(tp, ip, XFS_DATA_FORK);
1449 if (error)
1450 return error;
1451 }
1452
1453 *has_shared = false;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001454 found = xfs_iext_lookup_extent(ip, ifp, 0, &icur, &got);
Darrick J. Wongea7cdd72017-06-16 11:00:11 -07001455 while (found) {
1456 if (isnullstartblock(got.br_startblock) ||
1457 got.br_state != XFS_EXT_NORM)
1458 goto next;
1459 agno = XFS_FSB_TO_AGNO(mp, got.br_startblock);
1460 agbno = XFS_FSB_TO_AGBNO(mp, got.br_startblock);
1461 aglen = got.br_blockcount;
1462
1463 error = xfs_reflink_find_shared(mp, tp, agno, agbno, aglen,
1464 &rbno, &rlen, false);
1465 if (error)
1466 return error;
1467 /* Is there still a shared block here? */
1468 if (rbno != NULLAGBLOCK) {
1469 *has_shared = true;
1470 return 0;
1471 }
1472next:
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001473 found = xfs_iext_next_extent(ifp, &icur, &got);
Darrick J. Wongea7cdd72017-06-16 11:00:11 -07001474 }
1475
1476 return 0;
1477}
1478
Dave Chinner844e5e72018-05-09 07:49:10 -07001479/*
1480 * Clear the inode reflink flag if there are no shared extents.
1481 *
1482 * The caller is responsible for joining the inode to the transaction passed in.
1483 * The inode will be joined to the transaction that is returned to the caller.
1484 */
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001485int
1486xfs_reflink_clear_inode_flag(
1487 struct xfs_inode *ip,
1488 struct xfs_trans **tpp)
1489{
Darrick J. Wongea7cdd72017-06-16 11:00:11 -07001490 bool needs_flag;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001491 int error = 0;
1492
Darrick J. Wong63646fc2016-10-10 16:47:32 +11001493 ASSERT(xfs_is_reflink_inode(ip));
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001494
Darrick J. Wongea7cdd72017-06-16 11:00:11 -07001495 error = xfs_reflink_inode_has_shared_extents(*tpp, ip, &needs_flag);
1496 if (error || needs_flag)
1497 return error;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001498
1499 /*
1500 * We didn't find any shared blocks so turn off the reflink flag.
1501 * First, get rid of any leftover CoW mappings.
1502 */
Christoph Hellwig3802a342017-03-07 16:45:58 -08001503 error = xfs_reflink_cancel_cow_blocks(ip, tpp, 0, NULLFILEOFF, true);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001504 if (error)
1505 return error;
1506
1507 /* Clear the inode flag. */
1508 trace_xfs_reflink_unset_inode_flag(ip);
1509 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
Darrick J. Wong83104d42016-10-03 09:11:46 -07001510 xfs_inode_clear_cowblocks_tag(ip);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001511 xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
1512
1513 return error;
1514}
1515
1516/*
1517 * Clear the inode reflink flag if there are no shared extents and the size
1518 * hasn't changed.
1519 */
1520STATIC int
1521xfs_reflink_try_clear_inode_flag(
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001522 struct xfs_inode *ip)
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001523{
1524 struct xfs_mount *mp = ip->i_mount;
1525 struct xfs_trans *tp;
1526 int error = 0;
1527
1528 /* Start a rolling transaction to remove the mappings */
1529 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
1530 if (error)
1531 return error;
1532
1533 xfs_ilock(ip, XFS_ILOCK_EXCL);
1534 xfs_trans_ijoin(tp, ip, 0);
1535
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001536 error = xfs_reflink_clear_inode_flag(ip, &tp);
1537 if (error)
1538 goto cancel;
1539
1540 error = xfs_trans_commit(tp);
1541 if (error)
1542 goto out;
1543
1544 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1545 return 0;
1546cancel:
1547 xfs_trans_cancel(tp);
1548out:
1549 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1550 return error;
1551}
1552
1553/*
1554 * Pre-COW all shared blocks within a given byte range of a file and turn off
1555 * the reflink flag if we unshare all of the file's blocks.
1556 */
1557int
1558xfs_reflink_unshare(
1559 struct xfs_inode *ip,
1560 xfs_off_t offset,
1561 xfs_off_t len)
1562{
1563 struct xfs_mount *mp = ip->i_mount;
1564 xfs_fileoff_t fbno;
1565 xfs_filblks_t end;
1566 xfs_off_t isize;
1567 int error;
1568
1569 if (!xfs_is_reflink_inode(ip))
1570 return 0;
1571
1572 trace_xfs_reflink_unshare(ip, offset, len);
1573
1574 inode_dio_wait(VFS_I(ip));
1575
1576 /* Try to CoW the selected ranges */
1577 xfs_ilock(ip, XFS_ILOCK_EXCL);
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001578 fbno = XFS_B_TO_FSBT(mp, offset);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001579 isize = i_size_read(VFS_I(ip));
1580 end = XFS_B_TO_FSB(mp, offset + len);
1581 error = xfs_reflink_dirty_extents(ip, fbno, end, isize);
1582 if (error)
1583 goto out_unlock;
1584 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1585
1586 /* Wait for the IO to finish */
1587 error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
1588 if (error)
1589 goto out;
1590
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001591 /* Turn off the reflink flag if possible. */
1592 error = xfs_reflink_try_clear_inode_flag(ip);
1593 if (error)
1594 goto out;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001595
1596 return 0;
1597
1598out_unlock:
1599 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1600out:
1601 trace_xfs_reflink_unshare_error(ip, error, _RET_IP_);
1602 return error;
1603}