blob: 33845009bec5dc1342195da8dbe151807a8c513d [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
Darrick J. Wong92ff7282017-06-16 11:00:10 -0700160 cur = xfs_refcountbt_init_cursor(mp, tp, agbp, agno, NULL);
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 Foster8a749382018-07-11 22:26:06 -0700329 0, imap, &nimaps, NULL);
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 |
352 XFS_BMAPI_CONVERT_ONLY, &first_block, 0, &imap, &nimaps,
Brian Foster8a749382018-07-11 22:26:06 -0700353 NULL);
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);
432 nimaps = 1;
433
434 /* Allocate the entire reservation as unwritten blocks. */
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800435 error = xfs_bmapi_write(tp, ip, imap->br_startoff, imap->br_blockcount,
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800436 XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC, &first_block,
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800437 resblks, imap, &nimaps, &dfops);
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800438 if (error)
439 goto out_bmap_cancel;
440
Darrick J. Wong86d692b2017-12-14 15:46:06 -0800441 xfs_inode_set_cowblocks_tag(ip);
442
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800443 /* Finish up. */
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700444 error = xfs_defer_finish(&tp, &dfops);
Darrick J. Wong0613f162016-10-03 09:11:37 -0700445 if (error)
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800446 goto out_bmap_cancel;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700447
448 error = xfs_trans_commit(tp);
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800449 if (error)
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800450 return error;
Darrick J. Wong9f37bd12018-01-26 11:37:44 -0800451
452 /*
453 * Allocation succeeded but the requested range was not even partially
454 * satisfied? Bail out!
455 */
456 if (nimaps == 0)
457 return -ENOSPC;
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800458convert:
Brian Foster8a749382018-07-11 22:26:06 -0700459 return xfs_reflink_convert_cow_extent(ip, imap, offset_fsb, count_fsb);
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800460out_bmap_cancel:
Darrick J. Wong0613f162016-10-03 09:11:37 -0700461 xfs_defer_cancel(&dfops);
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800462 xfs_trans_unreserve_quota_nblks(tp, ip, (long)resblks, 0,
463 XFS_QMOPT_RES_REGBLKS);
Christoph Hellwig3c68d442017-02-06 10:51:03 -0800464out:
465 if (tp)
466 xfs_trans_cancel(tp);
467 return error;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700468}
469
Darrick J. Wongef473662016-10-03 09:11:34 -0700470/*
Christoph Hellwig3802a342017-03-07 16:45:58 -0800471 * Cancel CoW reservations for some block range of an inode.
472 *
473 * If cancel_real is true this function cancels all COW fork extents for the
474 * inode; if cancel_real is false, real extents are not cleared.
Dave Chinnerc5295c62018-05-09 07:49:09 -0700475 *
476 * Caller must have already joined the inode to the current transaction. The
477 * inode will be joined to the transaction returned to the caller.
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700478 */
479int
480xfs_reflink_cancel_cow_blocks(
481 struct xfs_inode *ip,
482 struct xfs_trans **tpp,
483 xfs_fileoff_t offset_fsb,
Christoph Hellwig3802a342017-03-07 16:45:58 -0800484 xfs_fileoff_t end_fsb,
485 bool cancel_real)
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700486{
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100487 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
Christoph Hellwigdf5ab1b2016-11-24 11:39:50 +1100488 struct xfs_bmbt_irec got, del;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700489 struct xfs_iext_cursor icur;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700490 xfs_fsblock_t firstfsb;
491 struct xfs_defer_ops dfops;
Christoph Hellwigdf5ab1b2016-11-24 11:39:50 +1100492 int error = 0;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700493
494 if (!xfs_is_reflink_inode(ip))
495 return 0;
Christoph Hellwig41caabd2017-11-03 10:34:44 -0700496 if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got))
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100497 return 0;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700498
Christoph Hellwig41caabd2017-11-03 10:34:44 -0700499 /* Walk backwards until we're out of the I/O range... */
500 while (got.br_startoff + got.br_blockcount > offset_fsb) {
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100501 del = got;
502 xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
Christoph Hellwig41caabd2017-11-03 10:34:44 -0700503
504 /* Extent delete may have bumped ext forward */
505 if (!del.br_blockcount) {
506 xfs_iext_prev(ifp, &icur);
507 goto next_extent;
508 }
509
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100510 trace_xfs_reflink_cancel_cow(ip, &del);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700511
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100512 if (isnullstartblock(del.br_startblock)) {
513 error = xfs_bmap_del_extent_delay(ip, XFS_COW_FORK,
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700514 &icur, &got, &del);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700515 if (error)
516 break;
Christoph Hellwig3802a342017-03-07 16:45:58 -0800517 } else if (del.br_state == XFS_EXT_UNWRITTEN || cancel_real) {
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700518 xfs_defer_init(&dfops, &firstfsb);
519
Darrick J. Wong174edb02016-10-03 09:11:39 -0700520 /* Free the CoW orphan record. */
521 error = xfs_refcount_free_cow_extent(ip->i_mount,
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100522 &dfops, del.br_startblock,
523 del.br_blockcount);
Darrick J. Wong174edb02016-10-03 09:11:39 -0700524 if (error)
525 break;
526
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700527 xfs_bmap_add_free(ip->i_mount, &dfops,
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100528 del.br_startblock, del.br_blockcount,
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700529 NULL);
530
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700531 /* Roll the transaction */
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700532 xfs_defer_ijoin(&dfops, ip);
533 error = xfs_defer_finish(tpp, &dfops);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700534 if (error) {
535 xfs_defer_cancel(&dfops);
536 break;
537 }
538
539 /* Remove the mapping from the CoW fork. */
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700540 xfs_bmap_del_extent_cow(ip, &icur, &got, &del);
Darrick J. Wong4b4c1322018-01-19 09:05:48 -0800541
542 /* Remove the quota reservation */
543 error = xfs_trans_reserve_quota_nblks(NULL, ip,
544 -(long)del.br_blockcount, 0,
545 XFS_QMOPT_RES_REGBLKS);
546 if (error)
547 break;
Darrick J. Wong9d40fba2017-12-10 18:03:55 -0800548 } else {
549 /* Didn't do anything, push cursor back. */
550 xfs_iext_prev(ifp, &icur);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700551 }
Christoph Hellwig41caabd2017-11-03 10:34:44 -0700552next_extent:
553 if (!xfs_iext_get_extent(ifp, &icur, &got))
Brian Fosterc17a8ef2016-10-24 14:21:08 +1100554 break;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700555 }
556
Brian Fosterc17a8ef2016-10-24 14:21:08 +1100557 /* clear tag if cow fork is emptied */
558 if (!ifp->if_bytes)
559 xfs_inode_clear_cowblocks_tag(ip);
560
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700561 return error;
562}
563
564/*
Christoph Hellwig3802a342017-03-07 16:45:58 -0800565 * Cancel CoW reservations for some byte range of an inode.
566 *
567 * If cancel_real is true this function cancels all COW fork extents for the
568 * inode; if cancel_real is false, real extents are not cleared.
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700569 */
570int
571xfs_reflink_cancel_cow_range(
572 struct xfs_inode *ip,
573 xfs_off_t offset,
Christoph Hellwig3802a342017-03-07 16:45:58 -0800574 xfs_off_t count,
575 bool cancel_real)
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700576{
577 struct xfs_trans *tp;
578 xfs_fileoff_t offset_fsb;
579 xfs_fileoff_t end_fsb;
580 int error;
581
582 trace_xfs_reflink_cancel_cow_range(ip, offset, count);
Darrick J. Wong63646fc2016-10-10 16:47:32 +1100583 ASSERT(xfs_is_reflink_inode(ip));
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700584
585 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
586 if (count == NULLFILEOFF)
587 end_fsb = NULLFILEOFF;
588 else
589 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
590
591 /* Start a rolling transaction to remove the mappings */
592 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
Dave Chinner4df0f7f2018-03-06 17:07:22 -0800593 0, 0, XFS_TRANS_NOFS, &tp);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700594 if (error)
595 goto out;
596
597 xfs_ilock(ip, XFS_ILOCK_EXCL);
598 xfs_trans_ijoin(tp, ip, 0);
599
600 /* Scrape out the old CoW reservations */
Christoph Hellwig3802a342017-03-07 16:45:58 -0800601 error = xfs_reflink_cancel_cow_blocks(ip, &tp, offset_fsb, end_fsb,
602 cancel_real);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700603 if (error)
604 goto out_cancel;
605
606 error = xfs_trans_commit(tp);
607
608 xfs_iunlock(ip, XFS_ILOCK_EXCL);
609 return error;
610
611out_cancel:
612 xfs_trans_cancel(tp);
613 xfs_iunlock(ip, XFS_ILOCK_EXCL);
614out:
615 trace_xfs_reflink_cancel_cow_range_error(ip, error, _RET_IP_);
616 return error;
617}
618
619/*
620 * Remap parts of a file's data fork after a successful CoW.
621 */
622int
623xfs_reflink_end_cow(
624 struct xfs_inode *ip,
625 xfs_off_t offset,
626 xfs_off_t count)
627{
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100628 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
Christoph Hellwig4ab86712016-11-24 11:39:50 +1100629 struct xfs_bmbt_irec got, del;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700630 struct xfs_trans *tp;
631 xfs_fileoff_t offset_fsb;
632 xfs_fileoff_t end_fsb;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700633 xfs_fsblock_t firstfsb;
634 struct xfs_defer_ops dfops;
Christoph Hellwig4ab86712016-11-24 11:39:50 +1100635 int error;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700636 unsigned int resblks;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700637 xfs_filblks_t rlen;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700638 struct xfs_iext_cursor icur;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700639
640 trace_xfs_reflink_end_cow(ip, offset, count);
641
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100642 /* No COW extents? That's easy! */
643 if (ifp->if_bytes == 0)
644 return 0;
645
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700646 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
647 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700648
Darrick J. Wongfe0be232017-04-12 12:26:07 -0700649 /*
650 * Start a rolling transaction to switch the mappings. We're
651 * unlikely ever to have to remap 16T worth of single-block
652 * extents, so just cap the worst case extent count to 2^32-1.
653 * Stick a warning in just in case, and avoid 64-bit division.
654 */
655 BUILD_BUG_ON(MAX_RW_COUNT > UINT_MAX);
656 if (end_fsb - offset_fsb > UINT_MAX) {
657 error = -EFSCORRUPTED;
658 xfs_force_shutdown(ip->i_mount, SHUTDOWN_CORRUPT_INCORE);
659 ASSERT(0);
660 goto out;
661 }
662 resblks = XFS_NEXTENTADD_SPACE_RES(ip->i_mount,
663 (unsigned int)(end_fsb - offset_fsb),
664 XFS_DATA_FORK);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700665 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
Dave Chinner4df0f7f2018-03-06 17:07:22 -0800666 resblks, 0, XFS_TRANS_RESERVE | XFS_TRANS_NOFS, &tp);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700667 if (error)
668 goto out;
669
670 xfs_ilock(ip, XFS_ILOCK_EXCL);
671 xfs_trans_ijoin(tp, ip, 0);
672
Christoph Hellwigdc560152017-10-23 16:32:39 -0700673 /*
674 * In case of racing, overlapping AIO writes no COW extents might be
675 * left by the time I/O completes for the loser of the race. In that
676 * case we are done.
677 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700678 if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got))
Christoph Hellwigdc560152017-10-23 16:32:39 -0700679 goto out_cancel;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700680
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100681 /* Walk backwards until we're out of the I/O range... */
682 while (got.br_startoff + got.br_blockcount > offset_fsb) {
683 del = got;
684 xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
685
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700686 /* Extent delete may have bumped ext forward */
Christoph Hellwigdf79b812018-03-13 23:15:33 -0700687 if (!del.br_blockcount)
688 goto prev_extent;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700689
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100690 ASSERT(!isnullstartblock(got.br_startblock));
691
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800692 /*
693 * Don't remap unwritten extents; these are
694 * speculatively preallocated CoW extents that have been
695 * allocated but have not yet been involved in a write.
696 */
Christoph Hellwigdf79b812018-03-13 23:15:33 -0700697 if (got.br_state == XFS_EXT_UNWRITTEN)
698 goto prev_extent;
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800699
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100700 /* Unmap the old blocks in the data fork. */
701 xfs_defer_init(&dfops, &firstfsb);
702 rlen = del.br_blockcount;
703 error = __xfs_bunmapi(tp, ip, del.br_startoff, &rlen, 0, 1,
704 &firstfsb, &dfops);
705 if (error)
706 goto out_defer;
707
708 /* Trim the extent to whatever got unmapped. */
709 if (rlen) {
710 xfs_trim_extent(&del, del.br_startoff + rlen,
711 del.br_blockcount - rlen);
712 }
713 trace_xfs_reflink_cow_remap(ip, &del);
714
715 /* Free the CoW orphan record. */
716 error = xfs_refcount_free_cow_extent(tp->t_mountp, &dfops,
717 del.br_startblock, del.br_blockcount);
718 if (error)
719 goto out_defer;
720
721 /* Map the new blocks into the data fork. */
722 error = xfs_bmap_map_extent(tp->t_mountp, &dfops, ip, &del);
723 if (error)
724 goto out_defer;
725
Darrick J. Wong4b4c1322018-01-19 09:05:48 -0800726 /* Charge this new data fork mapping to the on-disk quota. */
727 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_DELBCOUNT,
728 (long)del.br_blockcount);
729
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100730 /* Remove the mapping from the CoW fork. */
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700731 xfs_bmap_del_extent_cow(ip, &icur, &got, &del);
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100732
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700733 xfs_defer_ijoin(&dfops, ip);
734 error = xfs_defer_finish(&tp, &dfops);
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100735 if (error)
736 goto out_defer;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700737 if (!xfs_iext_get_extent(ifp, &icur, &got))
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100738 break;
Christoph Hellwigdf79b812018-03-13 23:15:33 -0700739 continue;
740prev_extent:
741 if (!xfs_iext_prev_extent(ifp, &icur, &got))
742 break;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700743 }
744
745 error = xfs_trans_commit(tp);
746 xfs_iunlock(ip, XFS_ILOCK_EXCL);
747 if (error)
748 goto out;
749 return 0;
750
751out_defer:
752 xfs_defer_cancel(&dfops);
Christoph Hellwige12199f2017-10-03 08:58:33 -0700753out_cancel:
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700754 xfs_trans_cancel(tp);
755 xfs_iunlock(ip, XFS_ILOCK_EXCL);
756out:
757 trace_xfs_reflink_end_cow_error(ip, error, _RET_IP_);
758 return error;
759}
Darrick J. Wong174edb02016-10-03 09:11:39 -0700760
761/*
762 * Free leftover CoW reservations that didn't get cleaned out.
763 */
764int
765xfs_reflink_recover_cow(
766 struct xfs_mount *mp)
767{
768 xfs_agnumber_t agno;
769 int error = 0;
770
771 if (!xfs_sb_version_hasreflink(&mp->m_sb))
772 return 0;
773
774 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
775 error = xfs_refcount_recover_cow_leftovers(mp, agno);
776 if (error)
777 break;
778 }
779
780 return error;
781}
Darrick J. Wong862bb362016-10-03 09:11:40 -0700782
783/*
784 * Reflinking (Block) Ranges of Two Files Together
785 *
786 * First, ensure that the reflink flag is set on both inodes. The flag is an
787 * optimization to avoid unnecessary refcount btree lookups in the write path.
788 *
789 * Now we can iteratively remap the range of extents (and holes) in src to the
790 * corresponding ranges in dest. Let drange and srange denote the ranges of
791 * logical blocks in dest and src touched by the reflink operation.
792 *
793 * While the length of drange is greater than zero,
794 * - Read src's bmbt at the start of srange ("imap")
795 * - If imap doesn't exist, make imap appear to start at the end of srange
796 * with zero length.
797 * - If imap starts before srange, advance imap to start at srange.
798 * - If imap goes beyond srange, truncate imap to end at the end of srange.
799 * - Punch (imap start - srange start + imap len) blocks from dest at
800 * offset (drange start).
801 * - If imap points to a real range of pblks,
802 * > Increase the refcount of the imap's pblks
803 * > Map imap's pblks into dest at the offset
804 * (drange start + imap start - srange start)
805 * - Advance drange and srange by (imap start - srange start + imap len)
806 *
807 * Finally, if the reflink made dest longer, update both the in-core and
808 * on-disk file sizes.
809 *
810 * ASCII Art Demonstration:
811 *
812 * Let's say we want to reflink this source file:
813 *
814 * ----SSSSSSS-SSSSS----SSSSSS (src file)
815 * <-------------------->
816 *
817 * into this destination file:
818 *
819 * --DDDDDDDDDDDDDDDDDDD--DDD (dest file)
820 * <-------------------->
821 * '-' means a hole, and 'S' and 'D' are written blocks in the src and dest.
822 * Observe that the range has different logical offsets in either file.
823 *
824 * Consider that the first extent in the source file doesn't line up with our
825 * reflink range. Unmapping and remapping are separate operations, so we can
826 * unmap more blocks from the destination file than we remap.
827 *
828 * ----SSSSSSS-SSSSS----SSSSSS
829 * <------->
830 * --DDDDD---------DDDDD--DDD
831 * <------->
832 *
833 * Now remap the source extent into the destination file:
834 *
835 * ----SSSSSSS-SSSSS----SSSSSS
836 * <------->
837 * --DDDDD--SSSSSSSDDDDD--DDD
838 * <------->
839 *
840 * Do likewise with the second hole and extent in our range. Holes in the
841 * unmap range don't affect our operation.
842 *
843 * ----SSSSSSS-SSSSS----SSSSSS
844 * <---->
845 * --DDDDD--SSSSSSS-SSSSS-DDD
846 * <---->
847 *
848 * Finally, unmap and remap part of the third extent. This will increase the
849 * size of the destination file.
850 *
851 * ----SSSSSSS-SSSSS----SSSSSS
852 * <----->
853 * --DDDDD--SSSSSSS-SSSSS----SSS
854 * <----->
855 *
856 * Once we update the destination file's i_size, we're done.
857 */
858
859/*
860 * Ensure the reflink bit is set in both inodes.
861 */
862STATIC int
863xfs_reflink_set_inode_flag(
864 struct xfs_inode *src,
865 struct xfs_inode *dest)
866{
867 struct xfs_mount *mp = src->i_mount;
868 int error;
869 struct xfs_trans *tp;
870
871 if (xfs_is_reflink_inode(src) && xfs_is_reflink_inode(dest))
872 return 0;
873
874 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
875 if (error)
876 goto out_error;
877
878 /* Lock both files against IO */
879 if (src->i_ino == dest->i_ino)
880 xfs_ilock(src, XFS_ILOCK_EXCL);
881 else
Darrick J. Wong7c2d2382018-01-26 15:27:33 -0800882 xfs_lock_two_inodes(src, XFS_ILOCK_EXCL, dest, XFS_ILOCK_EXCL);
Darrick J. Wong862bb362016-10-03 09:11:40 -0700883
884 if (!xfs_is_reflink_inode(src)) {
885 trace_xfs_reflink_set_inode_flag(src);
886 xfs_trans_ijoin(tp, src, XFS_ILOCK_EXCL);
887 src->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
888 xfs_trans_log_inode(tp, src, XFS_ILOG_CORE);
889 xfs_ifork_init_cow(src);
890 } else
891 xfs_iunlock(src, XFS_ILOCK_EXCL);
892
893 if (src->i_ino == dest->i_ino)
894 goto commit_flags;
895
896 if (!xfs_is_reflink_inode(dest)) {
897 trace_xfs_reflink_set_inode_flag(dest);
898 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
899 dest->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
900 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
901 xfs_ifork_init_cow(dest);
902 } else
903 xfs_iunlock(dest, XFS_ILOCK_EXCL);
904
905commit_flags:
906 error = xfs_trans_commit(tp);
907 if (error)
908 goto out_error;
909 return error;
910
911out_error:
912 trace_xfs_reflink_set_inode_flag_error(dest, error, _RET_IP_);
913 return error;
914}
915
916/*
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700917 * Update destination inode size & cowextsize hint, if necessary.
Darrick J. Wong862bb362016-10-03 09:11:40 -0700918 */
919STATIC int
920xfs_reflink_update_dest(
921 struct xfs_inode *dest,
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700922 xfs_off_t newlen,
Christoph Hellwigc5ecb422017-02-06 17:45:51 -0800923 xfs_extlen_t cowextsize,
924 bool is_dedupe)
Darrick J. Wong862bb362016-10-03 09:11:40 -0700925{
926 struct xfs_mount *mp = dest->i_mount;
927 struct xfs_trans *tp;
928 int error;
929
Christoph Hellwigc5ecb422017-02-06 17:45:51 -0800930 if (is_dedupe && newlen <= i_size_read(VFS_I(dest)) && cowextsize == 0)
Darrick J. Wong862bb362016-10-03 09:11:40 -0700931 return 0;
932
933 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
934 if (error)
935 goto out_error;
936
937 xfs_ilock(dest, XFS_ILOCK_EXCL);
938 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
939
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700940 if (newlen > i_size_read(VFS_I(dest))) {
941 trace_xfs_reflink_update_inode_size(dest, newlen);
942 i_size_write(VFS_I(dest), newlen);
943 dest->i_d.di_size = newlen;
944 }
945
946 if (cowextsize) {
947 dest->i_d.di_cowextsize = cowextsize;
948 dest->i_d.di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
949 }
950
Christoph Hellwigc5ecb422017-02-06 17:45:51 -0800951 if (!is_dedupe) {
952 xfs_trans_ichgtime(tp, dest,
953 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
954 }
Darrick J. Wong862bb362016-10-03 09:11:40 -0700955 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
956
957 error = xfs_trans_commit(tp);
958 if (error)
959 goto out_error;
960 return error;
961
962out_error:
963 trace_xfs_reflink_update_inode_size_error(dest, error, _RET_IP_);
964 return error;
965}
966
967/*
Darrick J. Wong6fa164b2016-10-03 09:11:45 -0700968 * Do we have enough reserve in this AG to handle a reflink? The refcount
969 * btree already reserved all the space it needs, but the rmap btree can grow
970 * infinitely, so we won't allow more reflinks when the AG is down to the
971 * btree reserves.
972 */
973static int
974xfs_reflink_ag_has_free_space(
975 struct xfs_mount *mp,
976 xfs_agnumber_t agno)
977{
978 struct xfs_perag *pag;
979 int error = 0;
980
981 if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
982 return 0;
983
984 pag = xfs_perag_get(mp, agno);
Brian Foster21592862018-03-09 14:01:59 -0800985 if (xfs_ag_resv_critical(pag, XFS_AG_RESV_RMAPBT) ||
Darrick J. Wong6fa164b2016-10-03 09:11:45 -0700986 xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA))
987 error = -ENOSPC;
988 xfs_perag_put(pag);
989 return error;
990}
991
992/*
Darrick J. Wong862bb362016-10-03 09:11:40 -0700993 * Unmap a range of blocks from a file, then map other blocks into the hole.
994 * The range to unmap is (destoff : destoff + srcioff + irec->br_blockcount).
995 * The extent irec is mapped into dest at irec->br_startoff.
996 */
997STATIC int
998xfs_reflink_remap_extent(
999 struct xfs_inode *ip,
1000 struct xfs_bmbt_irec *irec,
1001 xfs_fileoff_t destoff,
1002 xfs_off_t new_isize)
1003{
1004 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig9c4f29d2017-03-28 14:53:35 -07001005 bool real_extent = xfs_bmap_is_real_extent(irec);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001006 struct xfs_trans *tp;
1007 xfs_fsblock_t firstfsb;
1008 unsigned int resblks;
1009 struct xfs_defer_ops dfops;
1010 struct xfs_bmbt_irec uirec;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001011 xfs_filblks_t rlen;
1012 xfs_filblks_t unmap_len;
1013 xfs_off_t newlen;
1014 int error;
1015
1016 unmap_len = irec->br_startoff + irec->br_blockcount - destoff;
1017 trace_xfs_reflink_punch_range(ip, destoff, unmap_len);
1018
Darrick J. Wong6fa164b2016-10-03 09:11:45 -07001019 /* No reflinking if we're low on space */
1020 if (real_extent) {
1021 error = xfs_reflink_ag_has_free_space(mp,
1022 XFS_FSB_TO_AGNO(mp, irec->br_startblock));
1023 if (error)
1024 goto out;
1025 }
1026
Darrick J. Wong862bb362016-10-03 09:11:40 -07001027 /* Start a rolling transaction to switch the mappings */
1028 resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
1029 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
1030 if (error)
1031 goto out;
1032
1033 xfs_ilock(ip, XFS_ILOCK_EXCL);
1034 xfs_trans_ijoin(tp, ip, 0);
1035
1036 /* If we're not just clearing space, then do we have enough quota? */
1037 if (real_extent) {
1038 error = xfs_trans_reserve_quota_nblks(tp, ip,
1039 irec->br_blockcount, 0, XFS_QMOPT_RES_REGBLKS);
1040 if (error)
1041 goto out_cancel;
1042 }
1043
1044 trace_xfs_reflink_remap(ip, irec->br_startoff,
1045 irec->br_blockcount, irec->br_startblock);
1046
1047 /* Unmap the old blocks in the data fork. */
1048 rlen = unmap_len;
1049 while (rlen) {
1050 xfs_defer_init(&dfops, &firstfsb);
1051 error = __xfs_bunmapi(tp, ip, destoff, &rlen, 0, 1,
1052 &firstfsb, &dfops);
1053 if (error)
1054 goto out_defer;
1055
1056 /*
1057 * Trim the extent to whatever got unmapped.
1058 * Remember, bunmapi works backwards.
1059 */
1060 uirec.br_startblock = irec->br_startblock + rlen;
1061 uirec.br_startoff = irec->br_startoff + rlen;
1062 uirec.br_blockcount = unmap_len - rlen;
1063 unmap_len = rlen;
1064
1065 /* If this isn't a real mapping, we're done. */
1066 if (!real_extent || uirec.br_blockcount == 0)
1067 goto next_extent;
1068
1069 trace_xfs_reflink_remap(ip, uirec.br_startoff,
1070 uirec.br_blockcount, uirec.br_startblock);
1071
1072 /* Update the refcount tree */
1073 error = xfs_refcount_increase_extent(mp, &dfops, &uirec);
1074 if (error)
1075 goto out_defer;
1076
1077 /* Map the new blocks into the data fork. */
1078 error = xfs_bmap_map_extent(mp, &dfops, ip, &uirec);
1079 if (error)
1080 goto out_defer;
1081
1082 /* Update quota accounting. */
1083 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT,
1084 uirec.br_blockcount);
1085
1086 /* Update dest isize if needed. */
1087 newlen = XFS_FSB_TO_B(mp,
1088 uirec.br_startoff + uirec.br_blockcount);
1089 newlen = min_t(xfs_off_t, newlen, new_isize);
1090 if (newlen > i_size_read(VFS_I(ip))) {
1091 trace_xfs_reflink_update_inode_size(ip, newlen);
1092 i_size_write(VFS_I(ip), newlen);
1093 ip->i_d.di_size = newlen;
1094 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1095 }
1096
1097next_extent:
1098 /* Process all the deferred stuff. */
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001099 xfs_defer_ijoin(&dfops, ip);
1100 error = xfs_defer_finish(&tp, &dfops);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001101 if (error)
1102 goto out_defer;
1103 }
1104
1105 error = xfs_trans_commit(tp);
1106 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1107 if (error)
1108 goto out;
1109 return 0;
1110
1111out_defer:
1112 xfs_defer_cancel(&dfops);
1113out_cancel:
1114 xfs_trans_cancel(tp);
1115 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1116out:
1117 trace_xfs_reflink_remap_extent_error(ip, error, _RET_IP_);
1118 return error;
1119}
1120
1121/*
1122 * Iteratively remap one file's extents (and holes) to another's.
1123 */
1124STATIC int
1125xfs_reflink_remap_blocks(
1126 struct xfs_inode *src,
1127 xfs_fileoff_t srcoff,
1128 struct xfs_inode *dest,
1129 xfs_fileoff_t destoff,
1130 xfs_filblks_t len,
1131 xfs_off_t new_isize)
1132{
1133 struct xfs_bmbt_irec imap;
1134 int nimaps;
1135 int error = 0;
1136 xfs_filblks_t range_len;
1137
1138 /* drange = (destoff, destoff + len); srange = (srcoff, srcoff + len) */
1139 while (len) {
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001140 uint lock_mode;
1141
Darrick J. Wong862bb362016-10-03 09:11:40 -07001142 trace_xfs_reflink_remap_blocks_loop(src, srcoff, len,
1143 dest, destoff);
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001144
Darrick J. Wong862bb362016-10-03 09:11:40 -07001145 /* Read extent from the source file */
1146 nimaps = 1;
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001147 lock_mode = xfs_ilock_data_map_shared(src);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001148 error = xfs_bmapi_read(src, srcoff, len, &imap, &nimaps, 0);
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001149 xfs_iunlock(src, lock_mode);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001150 if (error)
1151 goto err;
1152 ASSERT(nimaps == 1);
1153
1154 trace_xfs_reflink_remap_imap(src, srcoff, len, XFS_IO_OVERWRITE,
1155 &imap);
1156
1157 /* Translate imap into the destination file. */
1158 range_len = imap.br_startoff + imap.br_blockcount - srcoff;
1159 imap.br_startoff += destoff - srcoff;
1160
1161 /* Clear dest from destoff to the end of imap and map it in. */
1162 error = xfs_reflink_remap_extent(dest, &imap, destoff,
1163 new_isize);
1164 if (error)
1165 goto err;
1166
1167 if (fatal_signal_pending(current)) {
1168 error = -EINTR;
1169 goto err;
1170 }
1171
1172 /* Advance drange/srange */
1173 srcoff += range_len;
1174 destoff += range_len;
1175 len -= range_len;
1176 }
1177
1178 return 0;
1179
1180err:
1181 trace_xfs_reflink_remap_blocks_error(dest, error, _RET_IP_);
1182 return error;
1183}
1184
1185/*
Darrick J. Wong1364b1d42018-01-18 13:55:20 -08001186 * Grab the exclusive iolock for a data copy from src to dest, making
1187 * sure to abide vfs locking order (lowest pointer value goes first) and
1188 * breaking the pnfs layout leases on dest before proceeding. The loop
1189 * is needed because we cannot call the blocking break_layout() with the
1190 * src iolock held, and therefore have to back out both locks.
1191 */
1192static int
1193xfs_iolock_two_inodes_and_break_layout(
1194 struct inode *src,
1195 struct inode *dest)
1196{
1197 int error;
1198
1199retry:
1200 if (src < dest) {
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001201 inode_lock_shared(src);
Darrick J. Wong1364b1d42018-01-18 13:55:20 -08001202 inode_lock_nested(dest, I_MUTEX_NONDIR2);
1203 } else {
1204 /* src >= dest */
1205 inode_lock(dest);
1206 }
1207
1208 error = break_layout(dest, false);
1209 if (error == -EWOULDBLOCK) {
1210 inode_unlock(dest);
1211 if (src < dest)
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001212 inode_unlock_shared(src);
Darrick J. Wong1364b1d42018-01-18 13:55:20 -08001213 error = break_layout(dest, true);
1214 if (error)
1215 return error;
1216 goto retry;
1217 }
1218 if (error) {
1219 inode_unlock(dest);
1220 if (src < dest)
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001221 inode_unlock_shared(src);
Darrick J. Wong1364b1d42018-01-18 13:55:20 -08001222 return error;
1223 }
1224 if (src > dest)
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001225 inode_lock_shared_nested(src, I_MUTEX_NONDIR2);
Darrick J. Wong1364b1d42018-01-18 13:55:20 -08001226 return 0;
1227}
1228
1229/*
Darrick J. Wong862bb362016-10-03 09:11:40 -07001230 * Link a range of blocks from one file to another.
1231 */
1232int
1233xfs_reflink_remap_range(
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001234 struct file *file_in,
1235 loff_t pos_in,
1236 struct file *file_out,
1237 loff_t pos_out,
1238 u64 len,
1239 bool is_dedupe)
Darrick J. Wong862bb362016-10-03 09:11:40 -07001240{
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001241 struct inode *inode_in = file_inode(file_in);
1242 struct xfs_inode *src = XFS_I(inode_in);
1243 struct inode *inode_out = file_inode(file_out);
1244 struct xfs_inode *dest = XFS_I(inode_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001245 struct xfs_mount *mp = src->i_mount;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001246 bool same_inode = (inode_in == inode_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001247 xfs_fileoff_t sfsbno, dfsbno;
1248 xfs_filblks_t fsblen;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001249 xfs_extlen_t cowextsize;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001250 ssize_t ret;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001251
1252 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1253 return -EOPNOTSUPP;
1254
1255 if (XFS_FORCED_SHUTDOWN(mp))
1256 return -EIO;
1257
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001258 /* Lock both files against IO */
Darrick J. Wong1364b1d42018-01-18 13:55:20 -08001259 ret = xfs_iolock_two_inodes_and_break_layout(inode_in, inode_out);
1260 if (ret)
1261 return ret;
Christoph Hellwig65523212016-11-30 14:33:25 +11001262 if (same_inode)
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001263 xfs_ilock(src, XFS_MMAPLOCK_EXCL);
Christoph Hellwig65523212016-11-30 14:33:25 +11001264 else
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001265 xfs_lock_two_inodes(src, XFS_MMAPLOCK_SHARED, dest,
Darrick J. Wong7c2d2382018-01-26 15:27:33 -08001266 XFS_MMAPLOCK_EXCL);
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001267
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001268 /* Check file eligibility and prepare for block sharing. */
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001269 ret = -EINVAL;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001270 /* Don't reflink realtime inodes */
1271 if (XFS_IS_REALTIME_INODE(src) || XFS_IS_REALTIME_INODE(dest))
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001272 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001273
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001274 /* Don't share DAX file data for now. */
1275 if (IS_DAX(inode_in) || IS_DAX(inode_out))
1276 goto out_unlock;
Darrick J. Wongcc714662016-10-03 09:11:41 -07001277
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001278 ret = vfs_clone_file_prep_inodes(inode_in, pos_in, inode_out, pos_out,
1279 &len, is_dedupe);
Darrick J. Wong22725ce2016-12-19 15:13:26 -08001280 if (ret <= 0)
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001281 goto out_unlock;
1282
Darrick J. Wong09ac8622018-01-19 08:56:04 -08001283 /* Attach dquots to dest inode before changing block map */
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -07001284 ret = xfs_qm_dqattach(dest);
Darrick J. Wong09ac8622018-01-19 08:56:04 -08001285 if (ret)
1286 goto out_unlock;
1287
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001288 trace_xfs_reflink_remap_range(src, pos_in, len, dest, pos_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001289
Darrick J. Wong5c989a02017-12-10 18:03:54 -08001290 /*
1291 * Clear out post-eof preallocations because we don't have page cache
1292 * backing the delayed allocations and they'll never get freed on
1293 * their own.
1294 */
1295 if (xfs_can_free_eofblocks(dest, true)) {
1296 ret = xfs_free_eofblocks(dest);
1297 if (ret)
1298 goto out_unlock;
1299 }
1300
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001301 /* Set flags and remap blocks. */
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001302 ret = xfs_reflink_set_inode_flag(src, dest);
1303 if (ret)
1304 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001305
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001306 dfsbno = XFS_B_TO_FSBT(mp, pos_out);
1307 sfsbno = XFS_B_TO_FSBT(mp, pos_in);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001308 fsblen = XFS_B_TO_FSB(mp, len);
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001309 ret = xfs_reflink_remap_blocks(src, sfsbno, dest, dfsbno, fsblen,
1310 pos_out + len);
1311 if (ret)
1312 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001313
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001314 /* Zap any page cache for the destination file's range. */
1315 truncate_inode_pages_range(&inode_out->i_data, pos_out,
1316 PAGE_ALIGN(pos_out + len) - 1);
1317
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001318 /*
1319 * Carry the cowextsize hint from src to dest if we're sharing the
1320 * entire source file to the entire destination file, the source file
1321 * has a cowextsize hint, and the destination file does not.
1322 */
1323 cowextsize = 0;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001324 if (pos_in == 0 && len == i_size_read(inode_in) &&
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001325 (src->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) &&
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001326 pos_out == 0 && len >= i_size_read(inode_out) &&
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001327 !(dest->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1328 cowextsize = src->i_d.di_cowextsize;
1329
Christoph Hellwigc5ecb422017-02-06 17:45:51 -08001330 ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize,
1331 is_dedupe);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001332
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001333out_unlock:
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001334 xfs_iunlock(dest, XFS_MMAPLOCK_EXCL);
Christoph Hellwig65523212016-11-30 14:33:25 +11001335 if (!same_inode)
Darrick J. Wong01c2e132018-01-18 14:07:53 -08001336 xfs_iunlock(src, XFS_MMAPLOCK_SHARED);
1337 inode_unlock(inode_out);
1338 if (!same_inode)
1339 inode_unlock_shared(inode_in);
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001340 if (ret)
1341 trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_);
1342 return ret;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001343}
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001344
1345/*
1346 * The user wants to preemptively CoW all shared blocks in this file,
1347 * which enables us to turn off the reflink flag. Iterate all
1348 * extents which are not prealloc/delalloc to see which ranges are
1349 * mentioned in the refcount tree, then read those blocks into the
1350 * pagecache, dirty them, fsync them back out, and then we can update
1351 * the inode flag. What happens if we run out of memory? :)
1352 */
1353STATIC int
1354xfs_reflink_dirty_extents(
1355 struct xfs_inode *ip,
1356 xfs_fileoff_t fbno,
1357 xfs_filblks_t end,
1358 xfs_off_t isize)
1359{
1360 struct xfs_mount *mp = ip->i_mount;
1361 xfs_agnumber_t agno;
1362 xfs_agblock_t agbno;
1363 xfs_extlen_t aglen;
1364 xfs_agblock_t rbno;
1365 xfs_extlen_t rlen;
1366 xfs_off_t fpos;
1367 xfs_off_t flen;
1368 struct xfs_bmbt_irec map[2];
1369 int nmaps;
Darrick J. Wong9780643c2016-10-10 16:49:18 +11001370 int error = 0;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001371
1372 while (end - fbno > 0) {
1373 nmaps = 1;
1374 /*
1375 * Look for extents in the file. Skip holes, delalloc, or
1376 * unwritten extents; they can't be reflinked.
1377 */
1378 error = xfs_bmapi_read(ip, fbno, end - fbno, map, &nmaps, 0);
1379 if (error)
1380 goto out;
1381 if (nmaps == 0)
1382 break;
Christoph Hellwig9c4f29d2017-03-28 14:53:35 -07001383 if (!xfs_bmap_is_real_extent(&map[0]))
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001384 goto next;
1385
1386 map[1] = map[0];
1387 while (map[1].br_blockcount) {
1388 agno = XFS_FSB_TO_AGNO(mp, map[1].br_startblock);
1389 agbno = XFS_FSB_TO_AGBNO(mp, map[1].br_startblock);
1390 aglen = map[1].br_blockcount;
1391
Darrick J. Wong92ff7282017-06-16 11:00:10 -07001392 error = xfs_reflink_find_shared(mp, NULL, agno, agbno,
1393 aglen, &rbno, &rlen, true);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001394 if (error)
1395 goto out;
1396 if (rbno == NULLAGBLOCK)
1397 break;
1398
1399 /* Dirty the pages */
1400 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1401 fpos = XFS_FSB_TO_B(mp, map[1].br_startoff +
1402 (rbno - agbno));
1403 flen = XFS_FSB_TO_B(mp, rlen);
1404 if (fpos + flen > isize)
1405 flen = isize - fpos;
1406 error = iomap_file_dirty(VFS_I(ip), fpos, flen,
1407 &xfs_iomap_ops);
1408 xfs_ilock(ip, XFS_ILOCK_EXCL);
1409 if (error)
1410 goto out;
1411
1412 map[1].br_blockcount -= (rbno - agbno + rlen);
1413 map[1].br_startoff += (rbno - agbno + rlen);
1414 map[1].br_startblock += (rbno - agbno + rlen);
1415 }
1416
1417next:
1418 fbno = map[0].br_startoff + map[0].br_blockcount;
1419 }
1420out:
1421 return error;
1422}
1423
Darrick J. Wongea7cdd72017-06-16 11:00:11 -07001424/* Does this inode need the reflink flag? */
1425int
1426xfs_reflink_inode_has_shared_extents(
1427 struct xfs_trans *tp,
1428 struct xfs_inode *ip,
1429 bool *has_shared)
1430{
1431 struct xfs_bmbt_irec got;
1432 struct xfs_mount *mp = ip->i_mount;
1433 struct xfs_ifork *ifp;
1434 xfs_agnumber_t agno;
1435 xfs_agblock_t agbno;
1436 xfs_extlen_t aglen;
1437 xfs_agblock_t rbno;
1438 xfs_extlen_t rlen;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001439 struct xfs_iext_cursor icur;
Darrick J. Wongea7cdd72017-06-16 11:00:11 -07001440 bool found;
1441 int error;
1442
1443 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1444 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1445 error = xfs_iread_extents(tp, ip, XFS_DATA_FORK);
1446 if (error)
1447 return error;
1448 }
1449
1450 *has_shared = false;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001451 found = xfs_iext_lookup_extent(ip, ifp, 0, &icur, &got);
Darrick J. Wongea7cdd72017-06-16 11:00:11 -07001452 while (found) {
1453 if (isnullstartblock(got.br_startblock) ||
1454 got.br_state != XFS_EXT_NORM)
1455 goto next;
1456 agno = XFS_FSB_TO_AGNO(mp, got.br_startblock);
1457 agbno = XFS_FSB_TO_AGBNO(mp, got.br_startblock);
1458 aglen = got.br_blockcount;
1459
1460 error = xfs_reflink_find_shared(mp, tp, agno, agbno, aglen,
1461 &rbno, &rlen, false);
1462 if (error)
1463 return error;
1464 /* Is there still a shared block here? */
1465 if (rbno != NULLAGBLOCK) {
1466 *has_shared = true;
1467 return 0;
1468 }
1469next:
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001470 found = xfs_iext_next_extent(ifp, &icur, &got);
Darrick J. Wongea7cdd72017-06-16 11:00:11 -07001471 }
1472
1473 return 0;
1474}
1475
Dave Chinner844e5e72018-05-09 07:49:10 -07001476/*
1477 * Clear the inode reflink flag if there are no shared extents.
1478 *
1479 * The caller is responsible for joining the inode to the transaction passed in.
1480 * The inode will be joined to the transaction that is returned to the caller.
1481 */
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001482int
1483xfs_reflink_clear_inode_flag(
1484 struct xfs_inode *ip,
1485 struct xfs_trans **tpp)
1486{
Darrick J. Wongea7cdd72017-06-16 11:00:11 -07001487 bool needs_flag;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001488 int error = 0;
1489
Darrick J. Wong63646fc2016-10-10 16:47:32 +11001490 ASSERT(xfs_is_reflink_inode(ip));
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001491
Darrick J. Wongea7cdd72017-06-16 11:00:11 -07001492 error = xfs_reflink_inode_has_shared_extents(*tpp, ip, &needs_flag);
1493 if (error || needs_flag)
1494 return error;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001495
1496 /*
1497 * We didn't find any shared blocks so turn off the reflink flag.
1498 * First, get rid of any leftover CoW mappings.
1499 */
Christoph Hellwig3802a342017-03-07 16:45:58 -08001500 error = xfs_reflink_cancel_cow_blocks(ip, tpp, 0, NULLFILEOFF, true);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001501 if (error)
1502 return error;
1503
1504 /* Clear the inode flag. */
1505 trace_xfs_reflink_unset_inode_flag(ip);
1506 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
Darrick J. Wong83104d42016-10-03 09:11:46 -07001507 xfs_inode_clear_cowblocks_tag(ip);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001508 xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
1509
1510 return error;
1511}
1512
1513/*
1514 * Clear the inode reflink flag if there are no shared extents and the size
1515 * hasn't changed.
1516 */
1517STATIC int
1518xfs_reflink_try_clear_inode_flag(
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001519 struct xfs_inode *ip)
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001520{
1521 struct xfs_mount *mp = ip->i_mount;
1522 struct xfs_trans *tp;
1523 int error = 0;
1524
1525 /* Start a rolling transaction to remove the mappings */
1526 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
1527 if (error)
1528 return error;
1529
1530 xfs_ilock(ip, XFS_ILOCK_EXCL);
1531 xfs_trans_ijoin(tp, ip, 0);
1532
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001533 error = xfs_reflink_clear_inode_flag(ip, &tp);
1534 if (error)
1535 goto cancel;
1536
1537 error = xfs_trans_commit(tp);
1538 if (error)
1539 goto out;
1540
1541 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1542 return 0;
1543cancel:
1544 xfs_trans_cancel(tp);
1545out:
1546 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1547 return error;
1548}
1549
1550/*
1551 * Pre-COW all shared blocks within a given byte range of a file and turn off
1552 * the reflink flag if we unshare all of the file's blocks.
1553 */
1554int
1555xfs_reflink_unshare(
1556 struct xfs_inode *ip,
1557 xfs_off_t offset,
1558 xfs_off_t len)
1559{
1560 struct xfs_mount *mp = ip->i_mount;
1561 xfs_fileoff_t fbno;
1562 xfs_filblks_t end;
1563 xfs_off_t isize;
1564 int error;
1565
1566 if (!xfs_is_reflink_inode(ip))
1567 return 0;
1568
1569 trace_xfs_reflink_unshare(ip, offset, len);
1570
1571 inode_dio_wait(VFS_I(ip));
1572
1573 /* Try to CoW the selected ranges */
1574 xfs_ilock(ip, XFS_ILOCK_EXCL);
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001575 fbno = XFS_B_TO_FSBT(mp, offset);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001576 isize = i_size_read(VFS_I(ip));
1577 end = XFS_B_TO_FSB(mp, offset + len);
1578 error = xfs_reflink_dirty_extents(ip, fbno, end, isize);
1579 if (error)
1580 goto out_unlock;
1581 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1582
1583 /* Wait for the IO to finish */
1584 error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
1585 if (error)
1586 goto out;
1587
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001588 /* Turn off the reflink flag if possible. */
1589 error = xfs_reflink_try_clear_inode_flag(ip);
1590 if (error)
1591 goto out;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001592
1593 return 0;
1594
1595out_unlock:
1596 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1597out:
1598 trace_xfs_reflink_unshare_error(ip, error, _RET_IP_);
1599 return error;
1600}