blob: 9bba084c1436ffa32c9fe735c61dcebd05b4bf96 [file] [log] [blame]
Darrick J. Wong3993bae2016-10-03 09:11:32 -07001/*
2 * Copyright (C) 2016 Oracle. All Rights Reserved.
3 *
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it would be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20#include "xfs.h"
21#include "xfs_fs.h"
22#include "xfs_shared.h"
23#include "xfs_format.h"
24#include "xfs_log_format.h"
25#include "xfs_trans_resv.h"
26#include "xfs_mount.h"
27#include "xfs_defer.h"
28#include "xfs_da_format.h"
29#include "xfs_da_btree.h"
30#include "xfs_inode.h"
31#include "xfs_trans.h"
32#include "xfs_inode_item.h"
33#include "xfs_bmap.h"
34#include "xfs_bmap_util.h"
35#include "xfs_error.h"
36#include "xfs_dir2.h"
37#include "xfs_dir2_priv.h"
38#include "xfs_ioctl.h"
39#include "xfs_trace.h"
40#include "xfs_log.h"
41#include "xfs_icache.h"
42#include "xfs_pnfs.h"
Darrick J. Wong174edb02016-10-03 09:11:39 -070043#include "xfs_btree.h"
Darrick J. Wong3993bae2016-10-03 09:11:32 -070044#include "xfs_refcount_btree.h"
45#include "xfs_refcount.h"
46#include "xfs_bmap_btree.h"
47#include "xfs_trans_space.h"
48#include "xfs_bit.h"
49#include "xfs_alloc.h"
50#include "xfs_quota_defs.h"
51#include "xfs_quota.h"
52#include "xfs_btree.h"
53#include "xfs_bmap_btree.h"
54#include "xfs_reflink.h"
Darrick J. Wong2a067052016-10-03 09:11:33 -070055#include "xfs_iomap.h"
Darrick J. Wong43caeb12016-10-03 09:11:35 -070056#include "xfs_rmap_btree.h"
Darrick J. Wong6fa164b2016-10-03 09:11:45 -070057#include "xfs_sb.h"
58#include "xfs_ag_resv.h"
Darrick J. Wong3993bae2016-10-03 09:11:32 -070059
60/*
61 * Copy on Write of Shared Blocks
62 *
63 * XFS must preserve "the usual" file semantics even when two files share
64 * the same physical blocks. This means that a write to one file must not
65 * alter the blocks in a different file; the way that we'll do that is
66 * through the use of a copy-on-write mechanism. At a high level, that
67 * means that when we want to write to a shared block, we allocate a new
68 * block, write the data to the new block, and if that succeeds we map the
69 * new block into the file.
70 *
71 * XFS provides a "delayed allocation" mechanism that defers the allocation
72 * of disk blocks to dirty-but-not-yet-mapped file blocks as long as
73 * possible. This reduces fragmentation by enabling the filesystem to ask
74 * for bigger chunks less often, which is exactly what we want for CoW.
75 *
76 * The delalloc mechanism begins when the kernel wants to make a block
77 * writable (write_begin or page_mkwrite). If the offset is not mapped, we
78 * create a delalloc mapping, which is a regular in-core extent, but without
79 * a real startblock. (For delalloc mappings, the startblock encodes both
80 * a flag that this is a delalloc mapping, and a worst-case estimate of how
81 * many blocks might be required to put the mapping into the BMBT.) delalloc
82 * mappings are a reservation against the free space in the filesystem;
83 * adjacent mappings can also be combined into fewer larger mappings.
84 *
Darrick J. Wong5eda4302017-02-02 15:14:02 -080085 * As an optimization, the CoW extent size hint (cowextsz) creates
86 * outsized aligned delalloc reservations in the hope of landing out of
87 * order nearby CoW writes in a single extent on disk, thereby reducing
88 * fragmentation and improving future performance.
89 *
90 * D: --RRRRRRSSSRRRRRRRR--- (data fork)
91 * C: ------DDDDDDD--------- (CoW fork)
92 *
Darrick J. Wong3993bae2016-10-03 09:11:32 -070093 * When dirty pages are being written out (typically in writepage), the
Darrick J. Wong5eda4302017-02-02 15:14:02 -080094 * delalloc reservations are converted into unwritten mappings by
95 * allocating blocks and replacing the delalloc mapping with real ones.
96 * A delalloc mapping can be replaced by several unwritten ones if the
97 * free space is fragmented.
98 *
99 * D: --RRRRRRSSSRRRRRRRR---
100 * C: ------UUUUUUU---------
Darrick J. Wong3993bae2016-10-03 09:11:32 -0700101 *
102 * We want to adapt the delalloc mechanism for copy-on-write, since the
103 * write paths are similar. The first two steps (creating the reservation
104 * and allocating the blocks) are exactly the same as delalloc except that
105 * the mappings must be stored in a separate CoW fork because we do not want
106 * to disturb the mapping in the data fork until we're sure that the write
107 * succeeded. IO completion in this case is the process of removing the old
108 * mapping from the data fork and moving the new mapping from the CoW fork to
109 * the data fork. This will be discussed shortly.
110 *
111 * For now, unaligned directio writes will be bounced back to the page cache.
112 * Block-aligned directio writes will use the same mechanism as buffered
113 * writes.
114 *
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800115 * Just prior to submitting the actual disk write requests, we convert
116 * the extents representing the range of the file actually being written
117 * (as opposed to extra pieces created for the cowextsize hint) to real
118 * extents. This will become important in the next step:
119 *
120 * D: --RRRRRRSSSRRRRRRRR---
121 * C: ------UUrrUUU---------
122 *
Darrick J. Wong3993bae2016-10-03 09:11:32 -0700123 * CoW remapping must be done after the data block write completes,
124 * because we don't want to destroy the old data fork map until we're sure
125 * the new block has been written. Since the new mappings are kept in a
126 * separate fork, we can simply iterate these mappings to find the ones
127 * that cover the file blocks that we just CoW'd. For each extent, simply
128 * unmap the corresponding range in the data fork, map the new range into
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800129 * the data fork, and remove the extent from the CoW fork. Because of
130 * the presence of the cowextsize hint, however, we must be careful
131 * only to remap the blocks that we've actually written out -- we must
132 * never remap delalloc reservations nor CoW staging blocks that have
133 * yet to be written. This corresponds exactly to the real extents in
134 * the CoW fork:
135 *
136 * D: --RRRRRRrrSRRRRRRRR---
137 * C: ------UU--UUU---------
Darrick J. Wong3993bae2016-10-03 09:11:32 -0700138 *
139 * Since the remapping operation can be applied to an arbitrary file
140 * range, we record the need for the remap step as a flag in the ioend
141 * instead of declaring a new IO type. This is required for direct io
142 * because we only have ioend for the whole dio, and we have to be able to
143 * remember the presence of unwritten blocks and CoW blocks with a single
144 * ioend structure. Better yet, the more ground we can cover with one
145 * ioend, the better.
146 */
Darrick J. Wong2a067052016-10-03 09:11:33 -0700147
148/*
149 * Given an AG extent, find the lowest-numbered run of shared blocks
150 * within that range and return the range in fbno/flen. If
151 * find_end_of_shared is true, return the longest contiguous extent of
152 * shared blocks. If there are no shared extents, fbno and flen will
153 * be set to NULLAGBLOCK and 0, respectively.
154 */
155int
156xfs_reflink_find_shared(
157 struct xfs_mount *mp,
158 xfs_agnumber_t agno,
159 xfs_agblock_t agbno,
160 xfs_extlen_t aglen,
161 xfs_agblock_t *fbno,
162 xfs_extlen_t *flen,
163 bool find_end_of_shared)
164{
165 struct xfs_buf *agbp;
166 struct xfs_btree_cur *cur;
167 int error;
168
169 error = xfs_alloc_read_agf(mp, NULL, agno, 0, &agbp);
170 if (error)
171 return error;
172
173 cur = xfs_refcountbt_init_cursor(mp, NULL, agbp, agno, NULL);
174
175 error = xfs_refcount_find_shared(cur, agbno, aglen, fbno, flen,
176 find_end_of_shared);
177
178 xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
179
180 xfs_buf_relse(agbp);
181 return error;
182}
183
184/*
185 * Trim the mapping to the next block where there's a change in the
186 * shared/unshared status. More specifically, this means that we
187 * find the lowest-numbered extent of shared blocks that coincides with
188 * the given block mapping. If the shared extent overlaps the start of
189 * the mapping, trim the mapping to the end of the shared extent. If
190 * the shared region intersects the mapping, trim the mapping to the
191 * start of the shared extent. If there are no shared regions that
192 * overlap, just return the original extent.
193 */
194int
195xfs_reflink_trim_around_shared(
196 struct xfs_inode *ip,
197 struct xfs_bmbt_irec *irec,
198 bool *shared,
199 bool *trimmed)
200{
201 xfs_agnumber_t agno;
202 xfs_agblock_t agbno;
203 xfs_extlen_t aglen;
204 xfs_agblock_t fbno;
205 xfs_extlen_t flen;
206 int error = 0;
207
208 /* Holes, unwritten, and delalloc extents cannot be shared */
209 if (!xfs_is_reflink_inode(ip) ||
210 ISUNWRITTEN(irec) ||
211 irec->br_startblock == HOLESTARTBLOCK ||
Christoph Hellwig62c5ac82016-10-20 15:52:00 +1100212 irec->br_startblock == DELAYSTARTBLOCK ||
213 isnullstartblock(irec->br_startblock)) {
Darrick J. Wong2a067052016-10-03 09:11:33 -0700214 *shared = false;
215 return 0;
216 }
217
218 trace_xfs_reflink_trim_around_shared(ip, irec);
219
220 agno = XFS_FSB_TO_AGNO(ip->i_mount, irec->br_startblock);
221 agbno = XFS_FSB_TO_AGBNO(ip->i_mount, irec->br_startblock);
222 aglen = irec->br_blockcount;
223
224 error = xfs_reflink_find_shared(ip->i_mount, agno, agbno,
225 aglen, &fbno, &flen, true);
226 if (error)
227 return error;
228
229 *shared = *trimmed = false;
230 if (fbno == NULLAGBLOCK) {
231 /* No shared blocks at all. */
232 return 0;
233 } else if (fbno == agbno) {
234 /*
235 * The start of this extent is shared. Truncate the
236 * mapping at the end of the shared region so that a
237 * subsequent iteration starts at the start of the
238 * unshared region.
239 */
240 irec->br_blockcount = flen;
241 *shared = true;
242 if (flen != aglen)
243 *trimmed = true;
244 return 0;
245 } else {
246 /*
247 * There's a shared extent midway through this extent.
248 * Truncate the mapping at the start of the shared
249 * extent so that a subsequent iteration starts at the
250 * start of the shared region.
251 */
252 irec->br_blockcount = fbno - agbno;
253 *trimmed = true;
254 return 0;
255 }
256}
257
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100258/*
259 * Trim the passed in imap to the next shared/unshared extent boundary, and
260 * if imap->br_startoff points to a shared extent reserve space for it in the
261 * COW fork. In this case *shared is set to true, else to false.
262 *
263 * Note that imap will always contain the block numbers for the existing blocks
264 * in the data fork, as the upper layers need them for read-modify-write
265 * operations.
266 */
267int
268xfs_reflink_reserve_cow(
Darrick J. Wong2a067052016-10-03 09:11:33 -0700269 struct xfs_inode *ip,
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100270 struct xfs_bmbt_irec *imap,
271 bool *shared)
Darrick J. Wong2a067052016-10-03 09:11:33 -0700272{
Christoph Hellwig2755fc442016-11-24 11:39:49 +1100273 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
274 struct xfs_bmbt_irec got;
Christoph Hellwig2755fc442016-11-24 11:39:49 +1100275 int error = 0;
276 bool eof = false, trimmed;
Darrick J. Wong2a067052016-10-03 09:11:33 -0700277 xfs_extnum_t idx;
278
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100279 /*
280 * Search the COW fork extent list first. This serves two purposes:
281 * first this implement the speculative preallocation using cowextisze,
282 * so that we also unshared block adjacent to shared blocks instead
283 * of just the shared blocks themselves. Second the lookup in the
284 * extent list is generally faster than going out to the shared extent
285 * tree.
286 */
Christoph Hellwig2755fc442016-11-24 11:39:49 +1100287
288 if (!xfs_iext_lookup_extent(ip, ifp, imap->br_startoff, &idx, &got))
289 eof = true;
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100290 if (!eof && got.br_startoff <= imap->br_startoff) {
291 trace_xfs_reflink_cow_found(ip, imap);
292 xfs_trim_extent(imap, got.br_startoff, got.br_blockcount);
Darrick J. Wong2a067052016-10-03 09:11:33 -0700293
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100294 *shared = true;
295 return 0;
296 }
Darrick J. Wong2a067052016-10-03 09:11:33 -0700297
298 /* Trim the mapping to the nearest shared extent boundary. */
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100299 error = xfs_reflink_trim_around_shared(ip, imap, shared, &trimmed);
Darrick J. Wong2a067052016-10-03 09:11:33 -0700300 if (error)
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100301 return error;
Darrick J. Wong2a067052016-10-03 09:11:33 -0700302
303 /* Not shared? Just report the (potentially capped) extent. */
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100304 if (!*shared)
305 return 0;
Darrick J. Wong2a067052016-10-03 09:11:33 -0700306
307 /*
308 * Fork all the shared blocks from our write offset until the end of
309 * the extent.
310 */
311 error = xfs_qm_dqattach_locked(ip, 0);
312 if (error)
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100313 return error;
314
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100315 error = xfs_bmapi_reserve_delalloc(ip, XFS_COW_FORK, imap->br_startoff,
Brian Foster0260d8f2016-11-28 14:57:42 +1100316 imap->br_blockcount, 0, &got, &idx, eof);
317 if (error == -ENOSPC || error == -EDQUOT)
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100318 trace_xfs_reflink_cow_enospc(ip, imap);
Brian Foster0260d8f2016-11-28 14:57:42 +1100319 if (error)
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100320 return error;
Darrick J. Wong83104d42016-10-03 09:11:46 -0700321
Darrick J. Wong2a067052016-10-03 09:11:33 -0700322 trace_xfs_reflink_cow_alloc(ip, &got);
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100323 return 0;
Darrick J. Wong2a067052016-10-03 09:11:33 -0700324}
Darrick J. Wongef473662016-10-03 09:11:34 -0700325
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800326/* Convert part of an unwritten CoW extent to a real one. */
327STATIC int
328xfs_reflink_convert_cow_extent(
329 struct xfs_inode *ip,
330 struct xfs_bmbt_irec *imap,
331 xfs_fileoff_t offset_fsb,
332 xfs_filblks_t count_fsb,
333 struct xfs_defer_ops *dfops)
334{
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800335 xfs_fsblock_t first_block;
336 int nimaps = 1;
337
338 if (imap->br_state == XFS_EXT_NORM)
339 return 0;
340
Christoph Hellwigdcf95852017-02-06 10:46:01 -0800341 xfs_trim_extent(imap, offset_fsb, count_fsb);
342 trace_xfs_reflink_convert_cow(ip, imap);
343 if (imap->br_blockcount == 0)
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800344 return 0;
Christoph Hellwigdcf95852017-02-06 10:46:01 -0800345 return xfs_bmapi_write(NULL, ip, imap->br_startoff, imap->br_blockcount,
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800346 XFS_BMAPI_COWFORK | XFS_BMAPI_CONVERT, &first_block,
Christoph Hellwigdcf95852017-02-06 10:46:01 -0800347 0, imap, &nimaps, dfops);
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800348}
349
350/* Convert all of the unwritten CoW extents in a file's range to real ones. */
351int
352xfs_reflink_convert_cow(
353 struct xfs_inode *ip,
354 xfs_off_t offset,
355 xfs_off_t count)
356{
357 struct xfs_bmbt_irec got;
358 struct xfs_defer_ops dfops;
359 struct xfs_mount *mp = ip->i_mount;
360 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
361 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
362 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count);
363 xfs_extnum_t idx;
364 bool found;
365 int error;
366
367 xfs_ilock(ip, XFS_ILOCK_EXCL);
368
369 /* Convert all the extents to real from unwritten. */
370 for (found = xfs_iext_lookup_extent(ip, ifp, offset_fsb, &idx, &got);
371 found && got.br_startoff < end_fsb;
372 found = xfs_iext_get_extent(ifp, ++idx, &got)) {
373 error = xfs_reflink_convert_cow_extent(ip, &got, offset_fsb,
374 end_fsb - offset_fsb, &dfops);
375 if (error)
376 break;
377 }
378
379 /* Finish up. */
380 xfs_iunlock(ip, XFS_ILOCK_EXCL);
381 return error;
382}
383
Darrick J. Wong0613f162016-10-03 09:11:37 -0700384/* Allocate all CoW reservations covering a range of blocks in a file. */
385static int
386__xfs_reflink_allocate_cow(
387 struct xfs_inode *ip,
388 xfs_fileoff_t *offset_fsb,
389 xfs_fileoff_t end_fsb)
390{
391 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800392 struct xfs_bmbt_irec imap, got;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700393 struct xfs_defer_ops dfops;
394 struct xfs_trans *tp;
395 xfs_fsblock_t first_block;
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800396 int nimaps, error, lockmode;
397 bool shared, trimmed;
398 xfs_filblks_t resaligned;
399 xfs_extlen_t resblks;
400 xfs_extnum_t idx;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700401
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800402 resaligned = xfs_aligned_fsb_count(*offset_fsb, end_fsb - *offset_fsb,
403 xfs_get_cowextsz_hint(ip));
404 resblks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
Darrick J. Wong0613f162016-10-03 09:11:37 -0700405
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800406 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
Darrick J. Wong0613f162016-10-03 09:11:37 -0700407 if (error)
408 return error;
409
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800410 lockmode = XFS_ILOCK_EXCL;
411 xfs_ilock(ip, lockmode);
Darrick J. Wong0613f162016-10-03 09:11:37 -0700412
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800413 /*
414 * Even if the extent is not shared we might have a preallocation for
415 * it in the COW fork. If so use it.
416 */
417 if (xfs_iext_lookup_extent(ip, ip->i_cowfp, *offset_fsb, &idx, &got) &&
418 got.br_startoff <= *offset_fsb) {
419 /* If we have a real allocation in the COW fork we're done. */
420 if (!isnullstartblock(got.br_startblock)) {
421 xfs_trim_extent(&got, *offset_fsb,
422 end_fsb - *offset_fsb);
423 *offset_fsb = got.br_startoff + got.br_blockcount;
424 goto out_trans_cancel;
425 }
426 } else {
427 nimaps = 1;
428 error = xfs_bmapi_read(ip, *offset_fsb, end_fsb - *offset_fsb,
429 &imap, &nimaps, 0);
430 if (error)
431 goto out_trans_cancel;
432 ASSERT(nimaps == 1);
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100433
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800434 /* Trim the mapping to the nearest shared extent boundary. */
435 error = xfs_reflink_trim_around_shared(ip, &imap, &shared,
436 &trimmed);
437 if (error)
438 goto out_trans_cancel;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700439
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800440 if (!shared) {
441 *offset_fsb = imap.br_startoff + imap.br_blockcount;
442 goto out_trans_cancel;
443 }
444
445 *offset_fsb = imap.br_startoff;
446 end_fsb = imap.br_startoff + imap.br_blockcount;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700447 }
448
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800449 error = xfs_trans_reserve_quota_nblks(tp, ip, resblks, 0,
450 XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong0613f162016-10-03 09:11:37 -0700451 if (error)
452 goto out_trans_cancel;
453
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800454 xfs_trans_ijoin(tp, ip, 0);
455
456 xfs_defer_init(&dfops, &first_block);
457 nimaps = 1;
458
459 /* Allocate the entire reservation as unwritten blocks. */
460 error = xfs_bmapi_write(tp, ip, *offset_fsb, end_fsb - *offset_fsb,
461 XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC, &first_block,
462 resblks, &imap, &nimaps, &dfops);
463 if (error)
464 goto out_bmap_cancel;
465
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800466 /* Finish up. */
Darrick J. Wong0613f162016-10-03 09:11:37 -0700467 error = xfs_defer_finish(&tp, &dfops, NULL);
468 if (error)
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800469 goto out_bmap_cancel;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700470
471 error = xfs_trans_commit(tp);
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800472 if (error)
473 goto out_unlock;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700474
Christoph Hellwig3ba020b2016-10-20 15:53:50 +1100475 *offset_fsb = imap.br_startoff + imap.br_blockcount;
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800476
Darrick J. Wong0613f162016-10-03 09:11:37 -0700477out_unlock:
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800478 xfs_iunlock(ip, lockmode);
Darrick J. Wong0613f162016-10-03 09:11:37 -0700479 return error;
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800480
481out_bmap_cancel:
Darrick J. Wong0613f162016-10-03 09:11:37 -0700482 xfs_defer_cancel(&dfops);
Christoph Hellwiga14234c2017-02-06 10:50:49 -0800483 xfs_trans_unreserve_quota_nblks(tp, ip, (long)resblks, 0,
484 XFS_QMOPT_RES_REGBLKS);
485out_trans_cancel:
Darrick J. Wong0613f162016-10-03 09:11:37 -0700486 xfs_trans_cancel(tp);
487 goto out_unlock;
488}
489
490/* Allocate all CoW reservations covering a part of a file. */
491int
492xfs_reflink_allocate_cow_range(
493 struct xfs_inode *ip,
494 xfs_off_t offset,
495 xfs_off_t count)
496{
497 struct xfs_mount *mp = ip->i_mount;
498 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
499 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count);
500 int error;
501
502 ASSERT(xfs_is_reflink_inode(ip));
503
504 trace_xfs_reflink_allocate_cow_range(ip, offset, count);
505
506 /*
507 * Make sure that the dquots are there.
508 */
509 error = xfs_qm_dqattach(ip, 0);
510 if (error)
511 return error;
512
513 while (offset_fsb < end_fsb) {
514 error = __xfs_reflink_allocate_cow(ip, &offset_fsb, end_fsb);
515 if (error) {
516 trace_xfs_reflink_allocate_cow_range_error(ip, error,
517 _RET_IP_);
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800518 return error;
Darrick J. Wong0613f162016-10-03 09:11:37 -0700519 }
520 }
521
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800522 /* Convert the CoW extents to regular. */
523 return xfs_reflink_convert_cow(ip, offset, count);
Darrick J. Wong0613f162016-10-03 09:11:37 -0700524}
525
Darrick J. Wongef473662016-10-03 09:11:34 -0700526/*
Christoph Hellwig092d5d92016-11-24 11:39:49 +1100527 * Find the CoW reservation for a given byte offset of a file.
Darrick J. Wongef473662016-10-03 09:11:34 -0700528 */
529bool
530xfs_reflink_find_cow_mapping(
531 struct xfs_inode *ip,
532 xfs_off_t offset,
Christoph Hellwig092d5d92016-11-24 11:39:49 +1100533 struct xfs_bmbt_irec *imap)
Darrick J. Wongef473662016-10-03 09:11:34 -0700534{
Christoph Hellwig092d5d92016-11-24 11:39:49 +1100535 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
536 xfs_fileoff_t offset_fsb;
537 struct xfs_bmbt_irec got;
Darrick J. Wongef473662016-10-03 09:11:34 -0700538 xfs_extnum_t idx;
539
540 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
541 ASSERT(xfs_is_reflink_inode(ip));
542
Christoph Hellwig092d5d92016-11-24 11:39:49 +1100543 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
544 if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &idx, &got))
Darrick J. Wongef473662016-10-03 09:11:34 -0700545 return false;
Christoph Hellwig092d5d92016-11-24 11:39:49 +1100546 if (got.br_startoff > offset_fsb)
Darrick J. Wongef473662016-10-03 09:11:34 -0700547 return false;
548
549 trace_xfs_reflink_find_cow_mapping(ip, offset, 1, XFS_IO_OVERWRITE,
Christoph Hellwig092d5d92016-11-24 11:39:49 +1100550 &got);
551 *imap = got;
Darrick J. Wongef473662016-10-03 09:11:34 -0700552 return true;
553}
554
555/*
556 * Trim an extent to end at the next CoW reservation past offset_fsb.
557 */
Christoph Hellwig86f12ab2016-11-24 11:39:50 +1100558void
Darrick J. Wongef473662016-10-03 09:11:34 -0700559xfs_reflink_trim_irec_to_next_cow(
560 struct xfs_inode *ip,
561 xfs_fileoff_t offset_fsb,
562 struct xfs_bmbt_irec *imap)
563{
Christoph Hellwig86f12ab2016-11-24 11:39:50 +1100564 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
565 struct xfs_bmbt_irec got;
Darrick J. Wongef473662016-10-03 09:11:34 -0700566 xfs_extnum_t idx;
567
568 if (!xfs_is_reflink_inode(ip))
Christoph Hellwig86f12ab2016-11-24 11:39:50 +1100569 return;
Darrick J. Wongef473662016-10-03 09:11:34 -0700570
571 /* Find the extent in the CoW fork. */
Christoph Hellwig86f12ab2016-11-24 11:39:50 +1100572 if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &idx, &got))
573 return;
Darrick J. Wongef473662016-10-03 09:11:34 -0700574
575 /* This is the extent before; try sliding up one. */
Christoph Hellwig86f12ab2016-11-24 11:39:50 +1100576 if (got.br_startoff < offset_fsb) {
577 if (!xfs_iext_get_extent(ifp, idx + 1, &got))
578 return;
Darrick J. Wongef473662016-10-03 09:11:34 -0700579 }
580
Christoph Hellwig86f12ab2016-11-24 11:39:50 +1100581 if (got.br_startoff >= imap->br_startoff + imap->br_blockcount)
582 return;
Darrick J. Wongef473662016-10-03 09:11:34 -0700583
Christoph Hellwig86f12ab2016-11-24 11:39:50 +1100584 imap->br_blockcount = got.br_startoff - imap->br_startoff;
Darrick J. Wongef473662016-10-03 09:11:34 -0700585 trace_xfs_reflink_trim_irec(ip, imap);
Darrick J. Wongef473662016-10-03 09:11:34 -0700586}
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700587
588/*
589 * Cancel all pending CoW reservations for some block range of an inode.
590 */
591int
592xfs_reflink_cancel_cow_blocks(
593 struct xfs_inode *ip,
594 struct xfs_trans **tpp,
595 xfs_fileoff_t offset_fsb,
596 xfs_fileoff_t end_fsb)
597{
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100598 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
Christoph Hellwigdf5ab1b2016-11-24 11:39:50 +1100599 struct xfs_bmbt_irec got, del;
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100600 xfs_extnum_t idx;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700601 xfs_fsblock_t firstfsb;
602 struct xfs_defer_ops dfops;
Christoph Hellwigdf5ab1b2016-11-24 11:39:50 +1100603 int error = 0;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700604
605 if (!xfs_is_reflink_inode(ip))
606 return 0;
Christoph Hellwigdf5ab1b2016-11-24 11:39:50 +1100607 if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &idx, &got))
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100608 return 0;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700609
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100610 while (got.br_startoff < end_fsb) {
611 del = got;
612 xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
613 trace_xfs_reflink_cancel_cow(ip, &del);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700614
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100615 if (isnullstartblock(del.br_startblock)) {
616 error = xfs_bmap_del_extent_delay(ip, XFS_COW_FORK,
617 &idx, &got, &del);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700618 if (error)
619 break;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700620 } else {
621 xfs_trans_ijoin(*tpp, ip, 0);
622 xfs_defer_init(&dfops, &firstfsb);
623
Darrick J. Wong174edb02016-10-03 09:11:39 -0700624 /* Free the CoW orphan record. */
625 error = xfs_refcount_free_cow_extent(ip->i_mount,
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100626 &dfops, del.br_startblock,
627 del.br_blockcount);
Darrick J. Wong174edb02016-10-03 09:11:39 -0700628 if (error)
629 break;
630
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700631 xfs_bmap_add_free(ip->i_mount, &dfops,
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100632 del.br_startblock, del.br_blockcount,
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700633 NULL);
634
635 /* Update quota accounting */
636 xfs_trans_mod_dquot_byino(*tpp, ip, XFS_TRANS_DQ_BCOUNT,
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100637 -(long)del.br_blockcount);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700638
639 /* Roll the transaction */
640 error = xfs_defer_finish(tpp, &dfops, ip);
641 if (error) {
642 xfs_defer_cancel(&dfops);
643 break;
644 }
645
646 /* Remove the mapping from the CoW fork. */
Christoph Hellwig3e0ee782016-10-20 15:54:31 +1100647 xfs_bmap_del_extent_cow(ip, &idx, &got, &del);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700648 }
649
Christoph Hellwigdf5ab1b2016-11-24 11:39:50 +1100650 if (!xfs_iext_get_extent(ifp, ++idx, &got))
Brian Fosterc17a8ef2016-10-24 14:21:08 +1100651 break;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700652 }
653
Brian Fosterc17a8ef2016-10-24 14:21:08 +1100654 /* clear tag if cow fork is emptied */
655 if (!ifp->if_bytes)
656 xfs_inode_clear_cowblocks_tag(ip);
657
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700658 return error;
659}
660
661/*
662 * Cancel all pending CoW reservations for some byte range of an inode.
663 */
664int
665xfs_reflink_cancel_cow_range(
666 struct xfs_inode *ip,
667 xfs_off_t offset,
668 xfs_off_t count)
669{
670 struct xfs_trans *tp;
671 xfs_fileoff_t offset_fsb;
672 xfs_fileoff_t end_fsb;
673 int error;
674
675 trace_xfs_reflink_cancel_cow_range(ip, offset, count);
Darrick J. Wong63646fc2016-10-10 16:47:32 +1100676 ASSERT(xfs_is_reflink_inode(ip));
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700677
678 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
679 if (count == NULLFILEOFF)
680 end_fsb = NULLFILEOFF;
681 else
682 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
683
684 /* Start a rolling transaction to remove the mappings */
685 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
686 0, 0, 0, &tp);
687 if (error)
688 goto out;
689
690 xfs_ilock(ip, XFS_ILOCK_EXCL);
691 xfs_trans_ijoin(tp, ip, 0);
692
693 /* Scrape out the old CoW reservations */
694 error = xfs_reflink_cancel_cow_blocks(ip, &tp, offset_fsb, end_fsb);
695 if (error)
696 goto out_cancel;
697
698 error = xfs_trans_commit(tp);
699
700 xfs_iunlock(ip, XFS_ILOCK_EXCL);
701 return error;
702
703out_cancel:
704 xfs_trans_cancel(tp);
705 xfs_iunlock(ip, XFS_ILOCK_EXCL);
706out:
707 trace_xfs_reflink_cancel_cow_range_error(ip, error, _RET_IP_);
708 return error;
709}
710
711/*
712 * Remap parts of a file's data fork after a successful CoW.
713 */
714int
715xfs_reflink_end_cow(
716 struct xfs_inode *ip,
717 xfs_off_t offset,
718 xfs_off_t count)
719{
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100720 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
Christoph Hellwig4ab86712016-11-24 11:39:50 +1100721 struct xfs_bmbt_irec got, del;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700722 struct xfs_trans *tp;
723 xfs_fileoff_t offset_fsb;
724 xfs_fileoff_t end_fsb;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700725 xfs_fsblock_t firstfsb;
726 struct xfs_defer_ops dfops;
Christoph Hellwig4ab86712016-11-24 11:39:50 +1100727 int error;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700728 unsigned int resblks;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700729 xfs_filblks_t rlen;
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100730 xfs_extnum_t idx;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700731
732 trace_xfs_reflink_end_cow(ip, offset, count);
733
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100734 /* No COW extents? That's easy! */
735 if (ifp->if_bytes == 0)
736 return 0;
737
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700738 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
739 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700740
741 /* Start a rolling transaction to switch the mappings */
742 resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
743 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
744 resblks, 0, 0, &tp);
745 if (error)
746 goto out;
747
748 xfs_ilock(ip, XFS_ILOCK_EXCL);
749 xfs_trans_ijoin(tp, ip, 0);
750
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100751 /* If there is a hole at end_fsb - 1 go to the previous extent */
Christoph Hellwig4ab86712016-11-24 11:39:50 +1100752 if (!xfs_iext_lookup_extent(ip, ifp, end_fsb - 1, &idx, &got) ||
753 got.br_startoff > end_fsb) {
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100754 ASSERT(idx > 0);
Christoph Hellwig4ab86712016-11-24 11:39:50 +1100755 xfs_iext_get_extent(ifp, --idx, &got);
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100756 }
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700757
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100758 /* Walk backwards until we're out of the I/O range... */
759 while (got.br_startoff + got.br_blockcount > offset_fsb) {
760 del = got;
761 xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb);
762
763 /* Extent delete may have bumped idx forward */
764 if (!del.br_blockcount) {
765 idx--;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700766 goto next_extent;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700767 }
768
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100769 ASSERT(!isnullstartblock(got.br_startblock));
770
Darrick J. Wong5eda4302017-02-02 15:14:02 -0800771 /*
772 * Don't remap unwritten extents; these are
773 * speculatively preallocated CoW extents that have been
774 * allocated but have not yet been involved in a write.
775 */
776 if (got.br_state == XFS_EXT_UNWRITTEN) {
777 idx--;
778 goto next_extent;
779 }
780
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100781 /* Unmap the old blocks in the data fork. */
782 xfs_defer_init(&dfops, &firstfsb);
783 rlen = del.br_blockcount;
784 error = __xfs_bunmapi(tp, ip, del.br_startoff, &rlen, 0, 1,
785 &firstfsb, &dfops);
786 if (error)
787 goto out_defer;
788
789 /* Trim the extent to whatever got unmapped. */
790 if (rlen) {
791 xfs_trim_extent(&del, del.br_startoff + rlen,
792 del.br_blockcount - rlen);
793 }
794 trace_xfs_reflink_cow_remap(ip, &del);
795
796 /* Free the CoW orphan record. */
797 error = xfs_refcount_free_cow_extent(tp->t_mountp, &dfops,
798 del.br_startblock, del.br_blockcount);
799 if (error)
800 goto out_defer;
801
802 /* Map the new blocks into the data fork. */
803 error = xfs_bmap_map_extent(tp->t_mountp, &dfops, ip, &del);
804 if (error)
805 goto out_defer;
806
807 /* Remove the mapping from the CoW fork. */
808 xfs_bmap_del_extent_cow(ip, &idx, &got, &del);
809
810 error = xfs_defer_finish(&tp, &dfops, ip);
811 if (error)
812 goto out_defer;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700813next_extent:
Christoph Hellwig4ab86712016-11-24 11:39:50 +1100814 if (!xfs_iext_get_extent(ifp, idx, &got))
Christoph Hellwigc1112b62016-10-20 15:54:45 +1100815 break;
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700816 }
817
818 error = xfs_trans_commit(tp);
819 xfs_iunlock(ip, XFS_ILOCK_EXCL);
820 if (error)
821 goto out;
822 return 0;
823
824out_defer:
825 xfs_defer_cancel(&dfops);
Darrick J. Wong43caeb12016-10-03 09:11:35 -0700826 xfs_trans_cancel(tp);
827 xfs_iunlock(ip, XFS_ILOCK_EXCL);
828out:
829 trace_xfs_reflink_end_cow_error(ip, error, _RET_IP_);
830 return error;
831}
Darrick J. Wong174edb02016-10-03 09:11:39 -0700832
833/*
834 * Free leftover CoW reservations that didn't get cleaned out.
835 */
836int
837xfs_reflink_recover_cow(
838 struct xfs_mount *mp)
839{
840 xfs_agnumber_t agno;
841 int error = 0;
842
843 if (!xfs_sb_version_hasreflink(&mp->m_sb))
844 return 0;
845
846 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
847 error = xfs_refcount_recover_cow_leftovers(mp, agno);
848 if (error)
849 break;
850 }
851
852 return error;
853}
Darrick J. Wong862bb362016-10-03 09:11:40 -0700854
855/*
856 * Reflinking (Block) Ranges of Two Files Together
857 *
858 * First, ensure that the reflink flag is set on both inodes. The flag is an
859 * optimization to avoid unnecessary refcount btree lookups in the write path.
860 *
861 * Now we can iteratively remap the range of extents (and holes) in src to the
862 * corresponding ranges in dest. Let drange and srange denote the ranges of
863 * logical blocks in dest and src touched by the reflink operation.
864 *
865 * While the length of drange is greater than zero,
866 * - Read src's bmbt at the start of srange ("imap")
867 * - If imap doesn't exist, make imap appear to start at the end of srange
868 * with zero length.
869 * - If imap starts before srange, advance imap to start at srange.
870 * - If imap goes beyond srange, truncate imap to end at the end of srange.
871 * - Punch (imap start - srange start + imap len) blocks from dest at
872 * offset (drange start).
873 * - If imap points to a real range of pblks,
874 * > Increase the refcount of the imap's pblks
875 * > Map imap's pblks into dest at the offset
876 * (drange start + imap start - srange start)
877 * - Advance drange and srange by (imap start - srange start + imap len)
878 *
879 * Finally, if the reflink made dest longer, update both the in-core and
880 * on-disk file sizes.
881 *
882 * ASCII Art Demonstration:
883 *
884 * Let's say we want to reflink this source file:
885 *
886 * ----SSSSSSS-SSSSS----SSSSSS (src file)
887 * <-------------------->
888 *
889 * into this destination file:
890 *
891 * --DDDDDDDDDDDDDDDDDDD--DDD (dest file)
892 * <-------------------->
893 * '-' means a hole, and 'S' and 'D' are written blocks in the src and dest.
894 * Observe that the range has different logical offsets in either file.
895 *
896 * Consider that the first extent in the source file doesn't line up with our
897 * reflink range. Unmapping and remapping are separate operations, so we can
898 * unmap more blocks from the destination file than we remap.
899 *
900 * ----SSSSSSS-SSSSS----SSSSSS
901 * <------->
902 * --DDDDD---------DDDDD--DDD
903 * <------->
904 *
905 * Now remap the source extent into the destination file:
906 *
907 * ----SSSSSSS-SSSSS----SSSSSS
908 * <------->
909 * --DDDDD--SSSSSSSDDDDD--DDD
910 * <------->
911 *
912 * Do likewise with the second hole and extent in our range. Holes in the
913 * unmap range don't affect our operation.
914 *
915 * ----SSSSSSS-SSSSS----SSSSSS
916 * <---->
917 * --DDDDD--SSSSSSS-SSSSS-DDD
918 * <---->
919 *
920 * Finally, unmap and remap part of the third extent. This will increase the
921 * size of the destination file.
922 *
923 * ----SSSSSSS-SSSSS----SSSSSS
924 * <----->
925 * --DDDDD--SSSSSSS-SSSSS----SSS
926 * <----->
927 *
928 * Once we update the destination file's i_size, we're done.
929 */
930
931/*
932 * Ensure the reflink bit is set in both inodes.
933 */
934STATIC int
935xfs_reflink_set_inode_flag(
936 struct xfs_inode *src,
937 struct xfs_inode *dest)
938{
939 struct xfs_mount *mp = src->i_mount;
940 int error;
941 struct xfs_trans *tp;
942
943 if (xfs_is_reflink_inode(src) && xfs_is_reflink_inode(dest))
944 return 0;
945
946 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
947 if (error)
948 goto out_error;
949
950 /* Lock both files against IO */
951 if (src->i_ino == dest->i_ino)
952 xfs_ilock(src, XFS_ILOCK_EXCL);
953 else
954 xfs_lock_two_inodes(src, dest, XFS_ILOCK_EXCL);
955
956 if (!xfs_is_reflink_inode(src)) {
957 trace_xfs_reflink_set_inode_flag(src);
958 xfs_trans_ijoin(tp, src, XFS_ILOCK_EXCL);
959 src->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
960 xfs_trans_log_inode(tp, src, XFS_ILOG_CORE);
961 xfs_ifork_init_cow(src);
962 } else
963 xfs_iunlock(src, XFS_ILOCK_EXCL);
964
965 if (src->i_ino == dest->i_ino)
966 goto commit_flags;
967
968 if (!xfs_is_reflink_inode(dest)) {
969 trace_xfs_reflink_set_inode_flag(dest);
970 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
971 dest->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
972 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
973 xfs_ifork_init_cow(dest);
974 } else
975 xfs_iunlock(dest, XFS_ILOCK_EXCL);
976
977commit_flags:
978 error = xfs_trans_commit(tp);
979 if (error)
980 goto out_error;
981 return error;
982
983out_error:
984 trace_xfs_reflink_set_inode_flag_error(dest, error, _RET_IP_);
985 return error;
986}
987
988/*
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700989 * Update destination inode size & cowextsize hint, if necessary.
Darrick J. Wong862bb362016-10-03 09:11:40 -0700990 */
991STATIC int
992xfs_reflink_update_dest(
993 struct xfs_inode *dest,
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700994 xfs_off_t newlen,
995 xfs_extlen_t cowextsize)
Darrick J. Wong862bb362016-10-03 09:11:40 -0700996{
997 struct xfs_mount *mp = dest->i_mount;
998 struct xfs_trans *tp;
999 int error;
1000
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001001 if (newlen <= i_size_read(VFS_I(dest)) && cowextsize == 0)
Darrick J. Wong862bb362016-10-03 09:11:40 -07001002 return 0;
1003
1004 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
1005 if (error)
1006 goto out_error;
1007
1008 xfs_ilock(dest, XFS_ILOCK_EXCL);
1009 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
1010
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001011 if (newlen > i_size_read(VFS_I(dest))) {
1012 trace_xfs_reflink_update_inode_size(dest, newlen);
1013 i_size_write(VFS_I(dest), newlen);
1014 dest->i_d.di_size = newlen;
1015 }
1016
1017 if (cowextsize) {
1018 dest->i_d.di_cowextsize = cowextsize;
1019 dest->i_d.di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
1020 }
1021
Darrick J. Wong862bb362016-10-03 09:11:40 -07001022 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
1023
1024 error = xfs_trans_commit(tp);
1025 if (error)
1026 goto out_error;
1027 return error;
1028
1029out_error:
1030 trace_xfs_reflink_update_inode_size_error(dest, error, _RET_IP_);
1031 return error;
1032}
1033
1034/*
Darrick J. Wong6fa164b2016-10-03 09:11:45 -07001035 * Do we have enough reserve in this AG to handle a reflink? The refcount
1036 * btree already reserved all the space it needs, but the rmap btree can grow
1037 * infinitely, so we won't allow more reflinks when the AG is down to the
1038 * btree reserves.
1039 */
1040static int
1041xfs_reflink_ag_has_free_space(
1042 struct xfs_mount *mp,
1043 xfs_agnumber_t agno)
1044{
1045 struct xfs_perag *pag;
1046 int error = 0;
1047
1048 if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
1049 return 0;
1050
1051 pag = xfs_perag_get(mp, agno);
1052 if (xfs_ag_resv_critical(pag, XFS_AG_RESV_AGFL) ||
1053 xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA))
1054 error = -ENOSPC;
1055 xfs_perag_put(pag);
1056 return error;
1057}
1058
1059/*
Darrick J. Wong862bb362016-10-03 09:11:40 -07001060 * Unmap a range of blocks from a file, then map other blocks into the hole.
1061 * The range to unmap is (destoff : destoff + srcioff + irec->br_blockcount).
1062 * The extent irec is mapped into dest at irec->br_startoff.
1063 */
1064STATIC int
1065xfs_reflink_remap_extent(
1066 struct xfs_inode *ip,
1067 struct xfs_bmbt_irec *irec,
1068 xfs_fileoff_t destoff,
1069 xfs_off_t new_isize)
1070{
1071 struct xfs_mount *mp = ip->i_mount;
1072 struct xfs_trans *tp;
1073 xfs_fsblock_t firstfsb;
1074 unsigned int resblks;
1075 struct xfs_defer_ops dfops;
1076 struct xfs_bmbt_irec uirec;
1077 bool real_extent;
1078 xfs_filblks_t rlen;
1079 xfs_filblks_t unmap_len;
1080 xfs_off_t newlen;
1081 int error;
1082
1083 unmap_len = irec->br_startoff + irec->br_blockcount - destoff;
1084 trace_xfs_reflink_punch_range(ip, destoff, unmap_len);
1085
1086 /* Only remap normal extents. */
1087 real_extent = (irec->br_startblock != HOLESTARTBLOCK &&
1088 irec->br_startblock != DELAYSTARTBLOCK &&
1089 !ISUNWRITTEN(irec));
1090
Darrick J. Wong6fa164b2016-10-03 09:11:45 -07001091 /* No reflinking if we're low on space */
1092 if (real_extent) {
1093 error = xfs_reflink_ag_has_free_space(mp,
1094 XFS_FSB_TO_AGNO(mp, irec->br_startblock));
1095 if (error)
1096 goto out;
1097 }
1098
Darrick J. Wong862bb362016-10-03 09:11:40 -07001099 /* Start a rolling transaction to switch the mappings */
1100 resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
1101 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
1102 if (error)
1103 goto out;
1104
1105 xfs_ilock(ip, XFS_ILOCK_EXCL);
1106 xfs_trans_ijoin(tp, ip, 0);
1107
1108 /* If we're not just clearing space, then do we have enough quota? */
1109 if (real_extent) {
1110 error = xfs_trans_reserve_quota_nblks(tp, ip,
1111 irec->br_blockcount, 0, XFS_QMOPT_RES_REGBLKS);
1112 if (error)
1113 goto out_cancel;
1114 }
1115
1116 trace_xfs_reflink_remap(ip, irec->br_startoff,
1117 irec->br_blockcount, irec->br_startblock);
1118
1119 /* Unmap the old blocks in the data fork. */
1120 rlen = unmap_len;
1121 while (rlen) {
1122 xfs_defer_init(&dfops, &firstfsb);
1123 error = __xfs_bunmapi(tp, ip, destoff, &rlen, 0, 1,
1124 &firstfsb, &dfops);
1125 if (error)
1126 goto out_defer;
1127
1128 /*
1129 * Trim the extent to whatever got unmapped.
1130 * Remember, bunmapi works backwards.
1131 */
1132 uirec.br_startblock = irec->br_startblock + rlen;
1133 uirec.br_startoff = irec->br_startoff + rlen;
1134 uirec.br_blockcount = unmap_len - rlen;
1135 unmap_len = rlen;
1136
1137 /* If this isn't a real mapping, we're done. */
1138 if (!real_extent || uirec.br_blockcount == 0)
1139 goto next_extent;
1140
1141 trace_xfs_reflink_remap(ip, uirec.br_startoff,
1142 uirec.br_blockcount, uirec.br_startblock);
1143
1144 /* Update the refcount tree */
1145 error = xfs_refcount_increase_extent(mp, &dfops, &uirec);
1146 if (error)
1147 goto out_defer;
1148
1149 /* Map the new blocks into the data fork. */
1150 error = xfs_bmap_map_extent(mp, &dfops, ip, &uirec);
1151 if (error)
1152 goto out_defer;
1153
1154 /* Update quota accounting. */
1155 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT,
1156 uirec.br_blockcount);
1157
1158 /* Update dest isize if needed. */
1159 newlen = XFS_FSB_TO_B(mp,
1160 uirec.br_startoff + uirec.br_blockcount);
1161 newlen = min_t(xfs_off_t, newlen, new_isize);
1162 if (newlen > i_size_read(VFS_I(ip))) {
1163 trace_xfs_reflink_update_inode_size(ip, newlen);
1164 i_size_write(VFS_I(ip), newlen);
1165 ip->i_d.di_size = newlen;
1166 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1167 }
1168
1169next_extent:
1170 /* Process all the deferred stuff. */
1171 error = xfs_defer_finish(&tp, &dfops, ip);
1172 if (error)
1173 goto out_defer;
1174 }
1175
1176 error = xfs_trans_commit(tp);
1177 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1178 if (error)
1179 goto out;
1180 return 0;
1181
1182out_defer:
1183 xfs_defer_cancel(&dfops);
1184out_cancel:
1185 xfs_trans_cancel(tp);
1186 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1187out:
1188 trace_xfs_reflink_remap_extent_error(ip, error, _RET_IP_);
1189 return error;
1190}
1191
1192/*
1193 * Iteratively remap one file's extents (and holes) to another's.
1194 */
1195STATIC int
1196xfs_reflink_remap_blocks(
1197 struct xfs_inode *src,
1198 xfs_fileoff_t srcoff,
1199 struct xfs_inode *dest,
1200 xfs_fileoff_t destoff,
1201 xfs_filblks_t len,
1202 xfs_off_t new_isize)
1203{
1204 struct xfs_bmbt_irec imap;
1205 int nimaps;
1206 int error = 0;
1207 xfs_filblks_t range_len;
1208
1209 /* drange = (destoff, destoff + len); srange = (srcoff, srcoff + len) */
1210 while (len) {
1211 trace_xfs_reflink_remap_blocks_loop(src, srcoff, len,
1212 dest, destoff);
1213 /* Read extent from the source file */
1214 nimaps = 1;
1215 xfs_ilock(src, XFS_ILOCK_EXCL);
1216 error = xfs_bmapi_read(src, srcoff, len, &imap, &nimaps, 0);
1217 xfs_iunlock(src, XFS_ILOCK_EXCL);
1218 if (error)
1219 goto err;
1220 ASSERT(nimaps == 1);
1221
1222 trace_xfs_reflink_remap_imap(src, srcoff, len, XFS_IO_OVERWRITE,
1223 &imap);
1224
1225 /* Translate imap into the destination file. */
1226 range_len = imap.br_startoff + imap.br_blockcount - srcoff;
1227 imap.br_startoff += destoff - srcoff;
1228
1229 /* Clear dest from destoff to the end of imap and map it in. */
1230 error = xfs_reflink_remap_extent(dest, &imap, destoff,
1231 new_isize);
1232 if (error)
1233 goto err;
1234
1235 if (fatal_signal_pending(current)) {
1236 error = -EINTR;
1237 goto err;
1238 }
1239
1240 /* Advance drange/srange */
1241 srcoff += range_len;
1242 destoff += range_len;
1243 len -= range_len;
1244 }
1245
1246 return 0;
1247
1248err:
1249 trace_xfs_reflink_remap_blocks_error(dest, error, _RET_IP_);
1250 return error;
1251}
1252
1253/*
1254 * Link a range of blocks from one file to another.
1255 */
1256int
1257xfs_reflink_remap_range(
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001258 struct file *file_in,
1259 loff_t pos_in,
1260 struct file *file_out,
1261 loff_t pos_out,
1262 u64 len,
1263 bool is_dedupe)
Darrick J. Wong862bb362016-10-03 09:11:40 -07001264{
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001265 struct inode *inode_in = file_inode(file_in);
1266 struct xfs_inode *src = XFS_I(inode_in);
1267 struct inode *inode_out = file_inode(file_out);
1268 struct xfs_inode *dest = XFS_I(inode_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001269 struct xfs_mount *mp = src->i_mount;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001270 bool same_inode = (inode_in == inode_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001271 xfs_fileoff_t sfsbno, dfsbno;
1272 xfs_filblks_t fsblen;
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001273 xfs_extlen_t cowextsize;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001274 ssize_t ret;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001275
1276 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1277 return -EOPNOTSUPP;
1278
1279 if (XFS_FORCED_SHUTDOWN(mp))
1280 return -EIO;
1281
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001282 /* Lock both files against IO */
Christoph Hellwig65523212016-11-30 14:33:25 +11001283 lock_two_nondirectories(inode_in, inode_out);
1284 if (same_inode)
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001285 xfs_ilock(src, XFS_MMAPLOCK_EXCL);
Christoph Hellwig65523212016-11-30 14:33:25 +11001286 else
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001287 xfs_lock_two_inodes(src, dest, XFS_MMAPLOCK_EXCL);
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001288
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001289 /* Check file eligibility and prepare for block sharing. */
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001290 ret = -EINVAL;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001291 /* Don't reflink realtime inodes */
1292 if (XFS_IS_REALTIME_INODE(src) || XFS_IS_REALTIME_INODE(dest))
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001293 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001294
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001295 /* Don't share DAX file data for now. */
1296 if (IS_DAX(inode_in) || IS_DAX(inode_out))
1297 goto out_unlock;
Darrick J. Wongcc714662016-10-03 09:11:41 -07001298
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001299 ret = vfs_clone_file_prep_inodes(inode_in, pos_in, inode_out, pos_out,
1300 &len, is_dedupe);
Darrick J. Wong22725ce2016-12-19 15:13:26 -08001301 if (ret <= 0)
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001302 goto out_unlock;
1303
1304 trace_xfs_reflink_remap_range(src, pos_in, len, dest, pos_out);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001305
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001306 /* Set flags and remap blocks. */
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001307 ret = xfs_reflink_set_inode_flag(src, dest);
1308 if (ret)
1309 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001310
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001311 dfsbno = XFS_B_TO_FSBT(mp, pos_out);
1312 sfsbno = XFS_B_TO_FSBT(mp, pos_in);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001313 fsblen = XFS_B_TO_FSB(mp, len);
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001314 ret = xfs_reflink_remap_blocks(src, sfsbno, dest, dfsbno, fsblen,
1315 pos_out + len);
1316 if (ret)
1317 goto out_unlock;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001318
Darrick J. Wong876bec6f2016-12-09 16:18:30 -08001319 /* Zap any page cache for the destination file's range. */
1320 truncate_inode_pages_range(&inode_out->i_data, pos_out,
1321 PAGE_ALIGN(pos_out + len) - 1);
1322
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001323 /*
1324 * Carry the cowextsize hint from src to dest if we're sharing the
1325 * entire source file to the entire destination file, the source file
1326 * has a cowextsize hint, and the destination file does not.
1327 */
1328 cowextsize = 0;
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001329 if (pos_in == 0 && len == i_size_read(inode_in) &&
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001330 (src->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) &&
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001331 pos_out == 0 && len >= i_size_read(inode_out) &&
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07001332 !(dest->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1333 cowextsize = src->i_d.di_cowextsize;
1334
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001335 ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize);
Darrick J. Wong862bb362016-10-03 09:11:40 -07001336
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001337out_unlock:
1338 xfs_iunlock(src, XFS_MMAPLOCK_EXCL);
Christoph Hellwig65523212016-11-30 14:33:25 +11001339 if (!same_inode)
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001340 xfs_iunlock(dest, XFS_MMAPLOCK_EXCL);
Christoph Hellwig65523212016-11-30 14:33:25 +11001341 unlock_two_nondirectories(inode_in, inode_out);
Christoph Hellwig5faaf4f2016-10-20 15:50:07 +11001342 if (ret)
1343 trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_);
1344 return ret;
Darrick J. Wong862bb362016-10-03 09:11:40 -07001345}
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001346
1347/*
1348 * The user wants to preemptively CoW all shared blocks in this file,
1349 * which enables us to turn off the reflink flag. Iterate all
1350 * extents which are not prealloc/delalloc to see which ranges are
1351 * mentioned in the refcount tree, then read those blocks into the
1352 * pagecache, dirty them, fsync them back out, and then we can update
1353 * the inode flag. What happens if we run out of memory? :)
1354 */
1355STATIC int
1356xfs_reflink_dirty_extents(
1357 struct xfs_inode *ip,
1358 xfs_fileoff_t fbno,
1359 xfs_filblks_t end,
1360 xfs_off_t isize)
1361{
1362 struct xfs_mount *mp = ip->i_mount;
1363 xfs_agnumber_t agno;
1364 xfs_agblock_t agbno;
1365 xfs_extlen_t aglen;
1366 xfs_agblock_t rbno;
1367 xfs_extlen_t rlen;
1368 xfs_off_t fpos;
1369 xfs_off_t flen;
1370 struct xfs_bmbt_irec map[2];
1371 int nmaps;
Darrick J. Wong9780643c2016-10-10 16:49:18 +11001372 int error = 0;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001373
1374 while (end - fbno > 0) {
1375 nmaps = 1;
1376 /*
1377 * Look for extents in the file. Skip holes, delalloc, or
1378 * unwritten extents; they can't be reflinked.
1379 */
1380 error = xfs_bmapi_read(ip, fbno, end - fbno, map, &nmaps, 0);
1381 if (error)
1382 goto out;
1383 if (nmaps == 0)
1384 break;
1385 if (map[0].br_startblock == HOLESTARTBLOCK ||
1386 map[0].br_startblock == DELAYSTARTBLOCK ||
1387 ISUNWRITTEN(&map[0]))
1388 goto next;
1389
1390 map[1] = map[0];
1391 while (map[1].br_blockcount) {
1392 agno = XFS_FSB_TO_AGNO(mp, map[1].br_startblock);
1393 agbno = XFS_FSB_TO_AGBNO(mp, map[1].br_startblock);
1394 aglen = map[1].br_blockcount;
1395
1396 error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
1397 &rbno, &rlen, true);
1398 if (error)
1399 goto out;
1400 if (rbno == NULLAGBLOCK)
1401 break;
1402
1403 /* Dirty the pages */
1404 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1405 fpos = XFS_FSB_TO_B(mp, map[1].br_startoff +
1406 (rbno - agbno));
1407 flen = XFS_FSB_TO_B(mp, rlen);
1408 if (fpos + flen > isize)
1409 flen = isize - fpos;
1410 error = iomap_file_dirty(VFS_I(ip), fpos, flen,
1411 &xfs_iomap_ops);
1412 xfs_ilock(ip, XFS_ILOCK_EXCL);
1413 if (error)
1414 goto out;
1415
1416 map[1].br_blockcount -= (rbno - agbno + rlen);
1417 map[1].br_startoff += (rbno - agbno + rlen);
1418 map[1].br_startblock += (rbno - agbno + rlen);
1419 }
1420
1421next:
1422 fbno = map[0].br_startoff + map[0].br_blockcount;
1423 }
1424out:
1425 return error;
1426}
1427
1428/* Clear the inode reflink flag if there are no shared extents. */
1429int
1430xfs_reflink_clear_inode_flag(
1431 struct xfs_inode *ip,
1432 struct xfs_trans **tpp)
1433{
1434 struct xfs_mount *mp = ip->i_mount;
1435 xfs_fileoff_t fbno;
1436 xfs_filblks_t end;
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;
Darrick J. Wong024adf42016-10-10 16:47:40 +11001442 struct xfs_bmbt_irec map;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001443 int nmaps;
1444 int error = 0;
1445
Darrick J. Wong63646fc2016-10-10 16:47:32 +11001446 ASSERT(xfs_is_reflink_inode(ip));
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001447
1448 fbno = 0;
1449 end = XFS_B_TO_FSB(mp, i_size_read(VFS_I(ip)));
1450 while (end - fbno > 0) {
1451 nmaps = 1;
1452 /*
1453 * Look for extents in the file. Skip holes, delalloc, or
1454 * unwritten extents; they can't be reflinked.
1455 */
Darrick J. Wong024adf42016-10-10 16:47:40 +11001456 error = xfs_bmapi_read(ip, fbno, end - fbno, &map, &nmaps, 0);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001457 if (error)
1458 return error;
1459 if (nmaps == 0)
1460 break;
Darrick J. Wong024adf42016-10-10 16:47:40 +11001461 if (map.br_startblock == HOLESTARTBLOCK ||
1462 map.br_startblock == DELAYSTARTBLOCK ||
1463 ISUNWRITTEN(&map))
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001464 goto next;
1465
Darrick J. Wong024adf42016-10-10 16:47:40 +11001466 agno = XFS_FSB_TO_AGNO(mp, map.br_startblock);
1467 agbno = XFS_FSB_TO_AGBNO(mp, map.br_startblock);
1468 aglen = map.br_blockcount;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001469
Darrick J. Wong024adf42016-10-10 16:47:40 +11001470 error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
1471 &rbno, &rlen, false);
1472 if (error)
1473 return error;
1474 /* Is there still a shared block here? */
1475 if (rbno != NULLAGBLOCK)
1476 return 0;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001477next:
Darrick J. Wong024adf42016-10-10 16:47:40 +11001478 fbno = map.br_startoff + map.br_blockcount;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001479 }
1480
1481 /*
1482 * We didn't find any shared blocks so turn off the reflink flag.
1483 * First, get rid of any leftover CoW mappings.
1484 */
1485 error = xfs_reflink_cancel_cow_blocks(ip, tpp, 0, NULLFILEOFF);
1486 if (error)
1487 return error;
1488
1489 /* Clear the inode flag. */
1490 trace_xfs_reflink_unset_inode_flag(ip);
1491 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
Darrick J. Wong83104d42016-10-03 09:11:46 -07001492 xfs_inode_clear_cowblocks_tag(ip);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001493 xfs_trans_ijoin(*tpp, ip, 0);
1494 xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
1495
1496 return error;
1497}
1498
1499/*
1500 * Clear the inode reflink flag if there are no shared extents and the size
1501 * hasn't changed.
1502 */
1503STATIC int
1504xfs_reflink_try_clear_inode_flag(
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001505 struct xfs_inode *ip)
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001506{
1507 struct xfs_mount *mp = ip->i_mount;
1508 struct xfs_trans *tp;
1509 int error = 0;
1510
1511 /* Start a rolling transaction to remove the mappings */
1512 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
1513 if (error)
1514 return error;
1515
1516 xfs_ilock(ip, XFS_ILOCK_EXCL);
1517 xfs_trans_ijoin(tp, ip, 0);
1518
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001519 error = xfs_reflink_clear_inode_flag(ip, &tp);
1520 if (error)
1521 goto cancel;
1522
1523 error = xfs_trans_commit(tp);
1524 if (error)
1525 goto out;
1526
1527 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1528 return 0;
1529cancel:
1530 xfs_trans_cancel(tp);
1531out:
1532 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1533 return error;
1534}
1535
1536/*
1537 * Pre-COW all shared blocks within a given byte range of a file and turn off
1538 * the reflink flag if we unshare all of the file's blocks.
1539 */
1540int
1541xfs_reflink_unshare(
1542 struct xfs_inode *ip,
1543 xfs_off_t offset,
1544 xfs_off_t len)
1545{
1546 struct xfs_mount *mp = ip->i_mount;
1547 xfs_fileoff_t fbno;
1548 xfs_filblks_t end;
1549 xfs_off_t isize;
1550 int error;
1551
1552 if (!xfs_is_reflink_inode(ip))
1553 return 0;
1554
1555 trace_xfs_reflink_unshare(ip, offset, len);
1556
1557 inode_dio_wait(VFS_I(ip));
1558
1559 /* Try to CoW the selected ranges */
1560 xfs_ilock(ip, XFS_ILOCK_EXCL);
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001561 fbno = XFS_B_TO_FSBT(mp, offset);
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001562 isize = i_size_read(VFS_I(ip));
1563 end = XFS_B_TO_FSB(mp, offset + len);
1564 error = xfs_reflink_dirty_extents(ip, fbno, end, isize);
1565 if (error)
1566 goto out_unlock;
1567 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1568
1569 /* Wait for the IO to finish */
1570 error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
1571 if (error)
1572 goto out;
1573
Darrick J. Wong97a1b872016-10-10 16:49:01 +11001574 /* Turn off the reflink flag if possible. */
1575 error = xfs_reflink_try_clear_inode_flag(ip);
1576 if (error)
1577 goto out;
Darrick J. Wong98cc2db2016-10-03 09:11:43 -07001578
1579 return 0;
1580
1581out_unlock:
1582 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1583out:
1584 trace_xfs_reflink_unshare_error(ip, error, _RET_IP_);
1585 return error;
1586}