]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/xfs/xfs_reflink.c
xfs: implement swapext for rmap filesystems
[mirror_ubuntu-zesty-kernel.git] / fs / xfs / xfs_reflink.c
CommitLineData
3993baeb
DW
1/*
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"
174edb0e 43#include "xfs_btree.h"
3993baeb
DW
44#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"
2a06705c 55#include "xfs_iomap.h"
43caeb18 56#include "xfs_rmap_btree.h"
6fa164b8
DW
57#include "xfs_sb.h"
58#include "xfs_ag_resv.h"
3993baeb
DW
59
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 *
85 * When dirty pages are being written out (typically in writepage), the
86 * delalloc reservations are converted into real mappings by allocating
87 * blocks and replacing the delalloc mapping with real ones. A delalloc
88 * mapping can be replaced by several real ones if the free space is
89 * fragmented.
90 *
91 * We want to adapt the delalloc mechanism for copy-on-write, since the
92 * write paths are similar. The first two steps (creating the reservation
93 * and allocating the blocks) are exactly the same as delalloc except that
94 * the mappings must be stored in a separate CoW fork because we do not want
95 * to disturb the mapping in the data fork until we're sure that the write
96 * succeeded. IO completion in this case is the process of removing the old
97 * mapping from the data fork and moving the new mapping from the CoW fork to
98 * the data fork. This will be discussed shortly.
99 *
100 * For now, unaligned directio writes will be bounced back to the page cache.
101 * Block-aligned directio writes will use the same mechanism as buffered
102 * writes.
103 *
104 * CoW remapping must be done after the data block write completes,
105 * because we don't want to destroy the old data fork map until we're sure
106 * the new block has been written. Since the new mappings are kept in a
107 * separate fork, we can simply iterate these mappings to find the ones
108 * that cover the file blocks that we just CoW'd. For each extent, simply
109 * unmap the corresponding range in the data fork, map the new range into
110 * the data fork, and remove the extent from the CoW fork.
111 *
112 * Since the remapping operation can be applied to an arbitrary file
113 * range, we record the need for the remap step as a flag in the ioend
114 * instead of declaring a new IO type. This is required for direct io
115 * because we only have ioend for the whole dio, and we have to be able to
116 * remember the presence of unwritten blocks and CoW blocks with a single
117 * ioend structure. Better yet, the more ground we can cover with one
118 * ioend, the better.
119 */
2a06705c
DW
120
121/*
122 * Given an AG extent, find the lowest-numbered run of shared blocks
123 * within that range and return the range in fbno/flen. If
124 * find_end_of_shared is true, return the longest contiguous extent of
125 * shared blocks. If there are no shared extents, fbno and flen will
126 * be set to NULLAGBLOCK and 0, respectively.
127 */
128int
129xfs_reflink_find_shared(
130 struct xfs_mount *mp,
131 xfs_agnumber_t agno,
132 xfs_agblock_t agbno,
133 xfs_extlen_t aglen,
134 xfs_agblock_t *fbno,
135 xfs_extlen_t *flen,
136 bool find_end_of_shared)
137{
138 struct xfs_buf *agbp;
139 struct xfs_btree_cur *cur;
140 int error;
141
142 error = xfs_alloc_read_agf(mp, NULL, agno, 0, &agbp);
143 if (error)
144 return error;
145
146 cur = xfs_refcountbt_init_cursor(mp, NULL, agbp, agno, NULL);
147
148 error = xfs_refcount_find_shared(cur, agbno, aglen, fbno, flen,
149 find_end_of_shared);
150
151 xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
152
153 xfs_buf_relse(agbp);
154 return error;
155}
156
157/*
158 * Trim the mapping to the next block where there's a change in the
159 * shared/unshared status. More specifically, this means that we
160 * find the lowest-numbered extent of shared blocks that coincides with
161 * the given block mapping. If the shared extent overlaps the start of
162 * the mapping, trim the mapping to the end of the shared extent. If
163 * the shared region intersects the mapping, trim the mapping to the
164 * start of the shared extent. If there are no shared regions that
165 * overlap, just return the original extent.
166 */
167int
168xfs_reflink_trim_around_shared(
169 struct xfs_inode *ip,
170 struct xfs_bmbt_irec *irec,
171 bool *shared,
172 bool *trimmed)
173{
174 xfs_agnumber_t agno;
175 xfs_agblock_t agbno;
176 xfs_extlen_t aglen;
177 xfs_agblock_t fbno;
178 xfs_extlen_t flen;
179 int error = 0;
180
181 /* Holes, unwritten, and delalloc extents cannot be shared */
182 if (!xfs_is_reflink_inode(ip) ||
183 ISUNWRITTEN(irec) ||
184 irec->br_startblock == HOLESTARTBLOCK ||
185 irec->br_startblock == DELAYSTARTBLOCK) {
186 *shared = false;
187 return 0;
188 }
189
190 trace_xfs_reflink_trim_around_shared(ip, irec);
191
192 agno = XFS_FSB_TO_AGNO(ip->i_mount, irec->br_startblock);
193 agbno = XFS_FSB_TO_AGBNO(ip->i_mount, irec->br_startblock);
194 aglen = irec->br_blockcount;
195
196 error = xfs_reflink_find_shared(ip->i_mount, agno, agbno,
197 aglen, &fbno, &flen, true);
198 if (error)
199 return error;
200
201 *shared = *trimmed = false;
202 if (fbno == NULLAGBLOCK) {
203 /* No shared blocks at all. */
204 return 0;
205 } else if (fbno == agbno) {
206 /*
207 * The start of this extent is shared. Truncate the
208 * mapping at the end of the shared region so that a
209 * subsequent iteration starts at the start of the
210 * unshared region.
211 */
212 irec->br_blockcount = flen;
213 *shared = true;
214 if (flen != aglen)
215 *trimmed = true;
216 return 0;
217 } else {
218 /*
219 * There's a shared extent midway through this extent.
220 * Truncate the mapping at the start of the shared
221 * extent so that a subsequent iteration starts at the
222 * start of the shared region.
223 */
224 irec->br_blockcount = fbno - agbno;
225 *trimmed = true;
226 return 0;
227 }
228}
229
230/* Create a CoW reservation for a range of blocks within a file. */
231static int
232__xfs_reflink_reserve_cow(
233 struct xfs_inode *ip,
234 xfs_fileoff_t *offset_fsb,
0613f16c
DW
235 xfs_fileoff_t end_fsb,
236 bool *skipped)
2a06705c
DW
237{
238 struct xfs_bmbt_irec got, prev, imap;
239 xfs_fileoff_t orig_end_fsb;
240 int nimaps, eof = 0, error = 0;
241 bool shared = false, trimmed = false;
242 xfs_extnum_t idx;
f7ca3522 243 xfs_extlen_t align;
2a06705c
DW
244
245 /* Already reserved? Skip the refcount btree access. */
246 xfs_bmap_search_extents(ip, *offset_fsb, XFS_COW_FORK, &eof, &idx,
247 &got, &prev);
248 if (!eof && got.br_startoff <= *offset_fsb) {
249 end_fsb = orig_end_fsb = got.br_startoff + got.br_blockcount;
250 trace_xfs_reflink_cow_found(ip, &got);
251 goto done;
252 }
253
254 /* Read extent from the source file. */
255 nimaps = 1;
256 error = xfs_bmapi_read(ip, *offset_fsb, end_fsb - *offset_fsb,
257 &imap, &nimaps, 0);
258 if (error)
259 goto out_unlock;
260 ASSERT(nimaps == 1);
261
262 /* Trim the mapping to the nearest shared extent boundary. */
263 error = xfs_reflink_trim_around_shared(ip, &imap, &shared, &trimmed);
264 if (error)
265 goto out_unlock;
266
267 end_fsb = orig_end_fsb = imap.br_startoff + imap.br_blockcount;
268
269 /* Not shared? Just report the (potentially capped) extent. */
0613f16c
DW
270 if (!shared) {
271 *skipped = true;
2a06705c 272 goto done;
0613f16c 273 }
2a06705c
DW
274
275 /*
276 * Fork all the shared blocks from our write offset until the end of
277 * the extent.
278 */
279 error = xfs_qm_dqattach_locked(ip, 0);
280 if (error)
281 goto out_unlock;
282
f7ca3522
DW
283 align = xfs_eof_alignment(ip, xfs_get_cowextsz_hint(ip));
284 if (align)
285 end_fsb = roundup_64(end_fsb, align);
286
2a06705c
DW
287retry:
288 error = xfs_bmapi_reserve_delalloc(ip, XFS_COW_FORK, *offset_fsb,
289 end_fsb - *offset_fsb, &got,
290 &prev, &idx, eof);
291 switch (error) {
292 case 0:
293 break;
294 case -ENOSPC:
295 case -EDQUOT:
296 /* retry without any preallocation */
297 trace_xfs_reflink_cow_enospc(ip, &imap);
298 if (end_fsb != orig_end_fsb) {
299 end_fsb = orig_end_fsb;
300 goto retry;
301 }
302 /*FALLTHRU*/
303 default:
304 goto out_unlock;
305 }
306
83104d44
DW
307 if (end_fsb != orig_end_fsb)
308 xfs_inode_set_cowblocks_tag(ip);
309
2a06705c
DW
310 trace_xfs_reflink_cow_alloc(ip, &got);
311done:
312 *offset_fsb = end_fsb;
313out_unlock:
314 return error;
315}
316
317/* Create a CoW reservation for part of a file. */
318int
319xfs_reflink_reserve_cow_range(
320 struct xfs_inode *ip,
321 xfs_off_t offset,
322 xfs_off_t count)
323{
324 struct xfs_mount *mp = ip->i_mount;
325 xfs_fileoff_t offset_fsb, end_fsb;
0613f16c 326 bool skipped = false;
2a06705c
DW
327 int error;
328
329 trace_xfs_reflink_reserve_cow_range(ip, offset, count);
330
331 offset_fsb = XFS_B_TO_FSBT(mp, offset);
332 end_fsb = XFS_B_TO_FSB(mp, offset + count);
333
334 xfs_ilock(ip, XFS_ILOCK_EXCL);
335 while (offset_fsb < end_fsb) {
0613f16c
DW
336 error = __xfs_reflink_reserve_cow(ip, &offset_fsb, end_fsb,
337 &skipped);
2a06705c
DW
338 if (error) {
339 trace_xfs_reflink_reserve_cow_range_error(ip, error,
340 _RET_IP_);
341 break;
342 }
343 }
344 xfs_iunlock(ip, XFS_ILOCK_EXCL);
345
346 return error;
347}
ef473667 348
0613f16c
DW
349/* Allocate all CoW reservations covering a range of blocks in a file. */
350static int
351__xfs_reflink_allocate_cow(
352 struct xfs_inode *ip,
353 xfs_fileoff_t *offset_fsb,
354 xfs_fileoff_t end_fsb)
355{
356 struct xfs_mount *mp = ip->i_mount;
357 struct xfs_bmbt_irec imap;
358 struct xfs_defer_ops dfops;
359 struct xfs_trans *tp;
360 xfs_fsblock_t first_block;
361 xfs_fileoff_t next_fsb;
362 int nimaps = 1, error;
363 bool skipped = false;
364
365 xfs_defer_init(&dfops, &first_block);
366
367 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
368 XFS_TRANS_RESERVE, &tp);
369 if (error)
370 return error;
371
372 xfs_ilock(ip, XFS_ILOCK_EXCL);
373
374 next_fsb = *offset_fsb;
375 error = __xfs_reflink_reserve_cow(ip, &next_fsb, end_fsb, &skipped);
376 if (error)
377 goto out_trans_cancel;
378
379 if (skipped) {
380 *offset_fsb = next_fsb;
381 goto out_trans_cancel;
382 }
383
384 xfs_trans_ijoin(tp, ip, 0);
385 error = xfs_bmapi_write(tp, ip, *offset_fsb, next_fsb - *offset_fsb,
386 XFS_BMAPI_COWFORK, &first_block,
387 XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK),
388 &imap, &nimaps, &dfops);
389 if (error)
390 goto out_trans_cancel;
391
392 /* We might not have been able to map the whole delalloc extent */
393 *offset_fsb = min(*offset_fsb + imap.br_blockcount, next_fsb);
394
395 error = xfs_defer_finish(&tp, &dfops, NULL);
396 if (error)
397 goto out_trans_cancel;
398
399 error = xfs_trans_commit(tp);
400
401out_unlock:
402 xfs_iunlock(ip, XFS_ILOCK_EXCL);
403 return error;
404out_trans_cancel:
405 xfs_defer_cancel(&dfops);
406 xfs_trans_cancel(tp);
407 goto out_unlock;
408}
409
410/* Allocate all CoW reservations covering a part of a file. */
411int
412xfs_reflink_allocate_cow_range(
413 struct xfs_inode *ip,
414 xfs_off_t offset,
415 xfs_off_t count)
416{
417 struct xfs_mount *mp = ip->i_mount;
418 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
419 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count);
420 int error;
421
422 ASSERT(xfs_is_reflink_inode(ip));
423
424 trace_xfs_reflink_allocate_cow_range(ip, offset, count);
425
426 /*
427 * Make sure that the dquots are there.
428 */
429 error = xfs_qm_dqattach(ip, 0);
430 if (error)
431 return error;
432
433 while (offset_fsb < end_fsb) {
434 error = __xfs_reflink_allocate_cow(ip, &offset_fsb, end_fsb);
435 if (error) {
436 trace_xfs_reflink_allocate_cow_range_error(ip, error,
437 _RET_IP_);
438 break;
439 }
440 }
441
442 return error;
443}
444
ef473667
DW
445/*
446 * Find the CoW reservation (and whether or not it needs block allocation)
447 * for a given byte offset of a file.
448 */
449bool
450xfs_reflink_find_cow_mapping(
451 struct xfs_inode *ip,
452 xfs_off_t offset,
453 struct xfs_bmbt_irec *imap,
454 bool *need_alloc)
455{
456 struct xfs_bmbt_irec irec;
457 struct xfs_ifork *ifp;
458 struct xfs_bmbt_rec_host *gotp;
459 xfs_fileoff_t bno;
460 xfs_extnum_t idx;
461
462 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
463 ASSERT(xfs_is_reflink_inode(ip));
464
465 /* Find the extent in the CoW fork. */
466 ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
467 bno = XFS_B_TO_FSBT(ip->i_mount, offset);
468 gotp = xfs_iext_bno_to_ext(ifp, bno, &idx);
469 if (!gotp)
470 return false;
471
472 xfs_bmbt_get_all(gotp, &irec);
473 if (bno >= irec.br_startoff + irec.br_blockcount ||
474 bno < irec.br_startoff)
475 return false;
476
477 trace_xfs_reflink_find_cow_mapping(ip, offset, 1, XFS_IO_OVERWRITE,
478 &irec);
479
480 /* If it's still delalloc, we must allocate later. */
481 *imap = irec;
482 *need_alloc = !!(isnullstartblock(irec.br_startblock));
483
484 return true;
485}
486
487/*
488 * Trim an extent to end at the next CoW reservation past offset_fsb.
489 */
490int
491xfs_reflink_trim_irec_to_next_cow(
492 struct xfs_inode *ip,
493 xfs_fileoff_t offset_fsb,
494 struct xfs_bmbt_irec *imap)
495{
496 struct xfs_bmbt_irec irec;
497 struct xfs_ifork *ifp;
498 struct xfs_bmbt_rec_host *gotp;
499 xfs_extnum_t idx;
500
501 if (!xfs_is_reflink_inode(ip))
502 return 0;
503
504 /* Find the extent in the CoW fork. */
505 ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
506 gotp = xfs_iext_bno_to_ext(ifp, offset_fsb, &idx);
507 if (!gotp)
508 return 0;
509 xfs_bmbt_get_all(gotp, &irec);
510
511 /* This is the extent before; try sliding up one. */
512 if (irec.br_startoff < offset_fsb) {
513 idx++;
514 if (idx >= ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
515 return 0;
516 gotp = xfs_iext_get_ext(ifp, idx);
517 xfs_bmbt_get_all(gotp, &irec);
518 }
519
520 if (irec.br_startoff >= imap->br_startoff + imap->br_blockcount)
521 return 0;
522
523 imap->br_blockcount = irec.br_startoff - imap->br_startoff;
524 trace_xfs_reflink_trim_irec(ip, imap);
525
526 return 0;
527}
43caeb18
DW
528
529/*
530 * Cancel all pending CoW reservations for some block range of an inode.
531 */
532int
533xfs_reflink_cancel_cow_blocks(
534 struct xfs_inode *ip,
535 struct xfs_trans **tpp,
536 xfs_fileoff_t offset_fsb,
537 xfs_fileoff_t end_fsb)
538{
539 struct xfs_bmbt_irec irec;
540 xfs_filblks_t count_fsb;
541 xfs_fsblock_t firstfsb;
542 struct xfs_defer_ops dfops;
543 int error = 0;
544 int nimaps;
545
546 if (!xfs_is_reflink_inode(ip))
547 return 0;
548
549 /* Go find the old extent in the CoW fork. */
550 while (offset_fsb < end_fsb) {
551 nimaps = 1;
552 count_fsb = (xfs_filblks_t)(end_fsb - offset_fsb);
553 error = xfs_bmapi_read(ip, offset_fsb, count_fsb, &irec,
554 &nimaps, XFS_BMAPI_COWFORK);
555 if (error)
556 break;
557 ASSERT(nimaps == 1);
558
559 trace_xfs_reflink_cancel_cow(ip, &irec);
560
561 if (irec.br_startblock == DELAYSTARTBLOCK) {
562 /* Free a delayed allocation. */
563 xfs_mod_fdblocks(ip->i_mount, irec.br_blockcount,
564 false);
565 ip->i_delayed_blks -= irec.br_blockcount;
566
567 /* Remove the mapping from the CoW fork. */
568 error = xfs_bunmapi_cow(ip, &irec);
569 if (error)
570 break;
571 } else if (irec.br_startblock == HOLESTARTBLOCK) {
572 /* empty */
573 } else {
574 xfs_trans_ijoin(*tpp, ip, 0);
575 xfs_defer_init(&dfops, &firstfsb);
576
174edb0e
DW
577 /* Free the CoW orphan record. */
578 error = xfs_refcount_free_cow_extent(ip->i_mount,
579 &dfops, irec.br_startblock,
580 irec.br_blockcount);
581 if (error)
582 break;
583
43caeb18
DW
584 xfs_bmap_add_free(ip->i_mount, &dfops,
585 irec.br_startblock, irec.br_blockcount,
586 NULL);
587
588 /* Update quota accounting */
589 xfs_trans_mod_dquot_byino(*tpp, ip, XFS_TRANS_DQ_BCOUNT,
590 -(long)irec.br_blockcount);
591
592 /* Roll the transaction */
593 error = xfs_defer_finish(tpp, &dfops, ip);
594 if (error) {
595 xfs_defer_cancel(&dfops);
596 break;
597 }
598
599 /* Remove the mapping from the CoW fork. */
600 error = xfs_bunmapi_cow(ip, &irec);
601 if (error)
602 break;
603 }
604
605 /* Roll on... */
606 offset_fsb = irec.br_startoff + irec.br_blockcount;
607 }
608
609 return error;
610}
611
612/*
613 * Cancel all pending CoW reservations for some byte range of an inode.
614 */
615int
616xfs_reflink_cancel_cow_range(
617 struct xfs_inode *ip,
618 xfs_off_t offset,
619 xfs_off_t count)
620{
621 struct xfs_trans *tp;
622 xfs_fileoff_t offset_fsb;
623 xfs_fileoff_t end_fsb;
624 int error;
625
626 trace_xfs_reflink_cancel_cow_range(ip, offset, count);
627
628 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
629 if (count == NULLFILEOFF)
630 end_fsb = NULLFILEOFF;
631 else
632 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
633
634 /* Start a rolling transaction to remove the mappings */
635 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
636 0, 0, 0, &tp);
637 if (error)
638 goto out;
639
640 xfs_ilock(ip, XFS_ILOCK_EXCL);
641 xfs_trans_ijoin(tp, ip, 0);
642
643 /* Scrape out the old CoW reservations */
644 error = xfs_reflink_cancel_cow_blocks(ip, &tp, offset_fsb, end_fsb);
645 if (error)
646 goto out_cancel;
647
648 error = xfs_trans_commit(tp);
649
650 xfs_iunlock(ip, XFS_ILOCK_EXCL);
651 return error;
652
653out_cancel:
654 xfs_trans_cancel(tp);
655 xfs_iunlock(ip, XFS_ILOCK_EXCL);
656out:
657 trace_xfs_reflink_cancel_cow_range_error(ip, error, _RET_IP_);
658 return error;
659}
660
661/*
662 * Remap parts of a file's data fork after a successful CoW.
663 */
664int
665xfs_reflink_end_cow(
666 struct xfs_inode *ip,
667 xfs_off_t offset,
668 xfs_off_t count)
669{
670 struct xfs_bmbt_irec irec;
671 struct xfs_bmbt_irec uirec;
672 struct xfs_trans *tp;
673 xfs_fileoff_t offset_fsb;
674 xfs_fileoff_t end_fsb;
675 xfs_filblks_t count_fsb;
676 xfs_fsblock_t firstfsb;
677 struct xfs_defer_ops dfops;
678 int error;
679 unsigned int resblks;
680 xfs_filblks_t ilen;
681 xfs_filblks_t rlen;
682 int nimaps;
683
684 trace_xfs_reflink_end_cow(ip, offset, count);
685
686 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
687 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
688 count_fsb = (xfs_filblks_t)(end_fsb - offset_fsb);
689
690 /* Start a rolling transaction to switch the mappings */
691 resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
692 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
693 resblks, 0, 0, &tp);
694 if (error)
695 goto out;
696
697 xfs_ilock(ip, XFS_ILOCK_EXCL);
698 xfs_trans_ijoin(tp, ip, 0);
699
700 /* Go find the old extent in the CoW fork. */
701 while (offset_fsb < end_fsb) {
702 /* Read extent from the source file */
703 nimaps = 1;
704 count_fsb = (xfs_filblks_t)(end_fsb - offset_fsb);
705 error = xfs_bmapi_read(ip, offset_fsb, count_fsb, &irec,
706 &nimaps, XFS_BMAPI_COWFORK);
707 if (error)
708 goto out_cancel;
709 ASSERT(nimaps == 1);
710
711 ASSERT(irec.br_startblock != DELAYSTARTBLOCK);
712 trace_xfs_reflink_cow_remap(ip, &irec);
713
714 /*
715 * We can have a hole in the CoW fork if part of a directio
716 * write is CoW but part of it isn't.
717 */
718 rlen = ilen = irec.br_blockcount;
719 if (irec.br_startblock == HOLESTARTBLOCK)
720 goto next_extent;
721
722 /* Unmap the old blocks in the data fork. */
723 while (rlen) {
724 xfs_defer_init(&dfops, &firstfsb);
725 error = __xfs_bunmapi(tp, ip, irec.br_startoff,
726 &rlen, 0, 1, &firstfsb, &dfops);
727 if (error)
728 goto out_defer;
729
730 /*
731 * Trim the extent to whatever got unmapped.
732 * Remember, bunmapi works backwards.
733 */
734 uirec.br_startblock = irec.br_startblock + rlen;
735 uirec.br_startoff = irec.br_startoff + rlen;
736 uirec.br_blockcount = irec.br_blockcount - rlen;
737 irec.br_blockcount = rlen;
738 trace_xfs_reflink_cow_remap_piece(ip, &uirec);
739
174edb0e
DW
740 /* Free the CoW orphan record. */
741 error = xfs_refcount_free_cow_extent(tp->t_mountp,
742 &dfops, uirec.br_startblock,
743 uirec.br_blockcount);
744 if (error)
745 goto out_defer;
746
43caeb18
DW
747 /* Map the new blocks into the data fork. */
748 error = xfs_bmap_map_extent(tp->t_mountp, &dfops,
749 ip, &uirec);
750 if (error)
751 goto out_defer;
752
753 /* Remove the mapping from the CoW fork. */
754 error = xfs_bunmapi_cow(ip, &uirec);
755 if (error)
756 goto out_defer;
757
758 error = xfs_defer_finish(&tp, &dfops, ip);
759 if (error)
760 goto out_defer;
761 }
762
763next_extent:
764 /* Roll on... */
765 offset_fsb = irec.br_startoff + ilen;
766 }
767
768 error = xfs_trans_commit(tp);
769 xfs_iunlock(ip, XFS_ILOCK_EXCL);
770 if (error)
771 goto out;
772 return 0;
773
774out_defer:
775 xfs_defer_cancel(&dfops);
776out_cancel:
777 xfs_trans_cancel(tp);
778 xfs_iunlock(ip, XFS_ILOCK_EXCL);
779out:
780 trace_xfs_reflink_end_cow_error(ip, error, _RET_IP_);
781 return error;
782}
174edb0e
DW
783
784/*
785 * Free leftover CoW reservations that didn't get cleaned out.
786 */
787int
788xfs_reflink_recover_cow(
789 struct xfs_mount *mp)
790{
791 xfs_agnumber_t agno;
792 int error = 0;
793
794 if (!xfs_sb_version_hasreflink(&mp->m_sb))
795 return 0;
796
797 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
798 error = xfs_refcount_recover_cow_leftovers(mp, agno);
799 if (error)
800 break;
801 }
802
803 return error;
804}
862bb360
DW
805
806/*
807 * Reflinking (Block) Ranges of Two Files Together
808 *
809 * First, ensure that the reflink flag is set on both inodes. The flag is an
810 * optimization to avoid unnecessary refcount btree lookups in the write path.
811 *
812 * Now we can iteratively remap the range of extents (and holes) in src to the
813 * corresponding ranges in dest. Let drange and srange denote the ranges of
814 * logical blocks in dest and src touched by the reflink operation.
815 *
816 * While the length of drange is greater than zero,
817 * - Read src's bmbt at the start of srange ("imap")
818 * - If imap doesn't exist, make imap appear to start at the end of srange
819 * with zero length.
820 * - If imap starts before srange, advance imap to start at srange.
821 * - If imap goes beyond srange, truncate imap to end at the end of srange.
822 * - Punch (imap start - srange start + imap len) blocks from dest at
823 * offset (drange start).
824 * - If imap points to a real range of pblks,
825 * > Increase the refcount of the imap's pblks
826 * > Map imap's pblks into dest at the offset
827 * (drange start + imap start - srange start)
828 * - Advance drange and srange by (imap start - srange start + imap len)
829 *
830 * Finally, if the reflink made dest longer, update both the in-core and
831 * on-disk file sizes.
832 *
833 * ASCII Art Demonstration:
834 *
835 * Let's say we want to reflink this source file:
836 *
837 * ----SSSSSSS-SSSSS----SSSSSS (src file)
838 * <-------------------->
839 *
840 * into this destination file:
841 *
842 * --DDDDDDDDDDDDDDDDDDD--DDD (dest file)
843 * <-------------------->
844 * '-' means a hole, and 'S' and 'D' are written blocks in the src and dest.
845 * Observe that the range has different logical offsets in either file.
846 *
847 * Consider that the first extent in the source file doesn't line up with our
848 * reflink range. Unmapping and remapping are separate operations, so we can
849 * unmap more blocks from the destination file than we remap.
850 *
851 * ----SSSSSSS-SSSSS----SSSSSS
852 * <------->
853 * --DDDDD---------DDDDD--DDD
854 * <------->
855 *
856 * Now remap the source extent into the destination file:
857 *
858 * ----SSSSSSS-SSSSS----SSSSSS
859 * <------->
860 * --DDDDD--SSSSSSSDDDDD--DDD
861 * <------->
862 *
863 * Do likewise with the second hole and extent in our range. Holes in the
864 * unmap range don't affect our operation.
865 *
866 * ----SSSSSSS-SSSSS----SSSSSS
867 * <---->
868 * --DDDDD--SSSSSSS-SSSSS-DDD
869 * <---->
870 *
871 * Finally, unmap and remap part of the third extent. This will increase the
872 * size of the destination file.
873 *
874 * ----SSSSSSS-SSSSS----SSSSSS
875 * <----->
876 * --DDDDD--SSSSSSS-SSSSS----SSS
877 * <----->
878 *
879 * Once we update the destination file's i_size, we're done.
880 */
881
882/*
883 * Ensure the reflink bit is set in both inodes.
884 */
885STATIC int
886xfs_reflink_set_inode_flag(
887 struct xfs_inode *src,
888 struct xfs_inode *dest)
889{
890 struct xfs_mount *mp = src->i_mount;
891 int error;
892 struct xfs_trans *tp;
893
894 if (xfs_is_reflink_inode(src) && xfs_is_reflink_inode(dest))
895 return 0;
896
897 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
898 if (error)
899 goto out_error;
900
901 /* Lock both files against IO */
902 if (src->i_ino == dest->i_ino)
903 xfs_ilock(src, XFS_ILOCK_EXCL);
904 else
905 xfs_lock_two_inodes(src, dest, XFS_ILOCK_EXCL);
906
907 if (!xfs_is_reflink_inode(src)) {
908 trace_xfs_reflink_set_inode_flag(src);
909 xfs_trans_ijoin(tp, src, XFS_ILOCK_EXCL);
910 src->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
911 xfs_trans_log_inode(tp, src, XFS_ILOG_CORE);
912 xfs_ifork_init_cow(src);
913 } else
914 xfs_iunlock(src, XFS_ILOCK_EXCL);
915
916 if (src->i_ino == dest->i_ino)
917 goto commit_flags;
918
919 if (!xfs_is_reflink_inode(dest)) {
920 trace_xfs_reflink_set_inode_flag(dest);
921 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
922 dest->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
923 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
924 xfs_ifork_init_cow(dest);
925 } else
926 xfs_iunlock(dest, XFS_ILOCK_EXCL);
927
928commit_flags:
929 error = xfs_trans_commit(tp);
930 if (error)
931 goto out_error;
932 return error;
933
934out_error:
935 trace_xfs_reflink_set_inode_flag_error(dest, error, _RET_IP_);
936 return error;
937}
938
939/*
f7ca3522 940 * Update destination inode size & cowextsize hint, if necessary.
862bb360
DW
941 */
942STATIC int
943xfs_reflink_update_dest(
944 struct xfs_inode *dest,
f7ca3522
DW
945 xfs_off_t newlen,
946 xfs_extlen_t cowextsize)
862bb360
DW
947{
948 struct xfs_mount *mp = dest->i_mount;
949 struct xfs_trans *tp;
950 int error;
951
f7ca3522 952 if (newlen <= i_size_read(VFS_I(dest)) && cowextsize == 0)
862bb360
DW
953 return 0;
954
955 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
956 if (error)
957 goto out_error;
958
959 xfs_ilock(dest, XFS_ILOCK_EXCL);
960 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
961
f7ca3522
DW
962 if (newlen > i_size_read(VFS_I(dest))) {
963 trace_xfs_reflink_update_inode_size(dest, newlen);
964 i_size_write(VFS_I(dest), newlen);
965 dest->i_d.di_size = newlen;
966 }
967
968 if (cowextsize) {
969 dest->i_d.di_cowextsize = cowextsize;
970 dest->i_d.di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
971 }
972
862bb360
DW
973 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
974
975 error = xfs_trans_commit(tp);
976 if (error)
977 goto out_error;
978 return error;
979
980out_error:
981 trace_xfs_reflink_update_inode_size_error(dest, error, _RET_IP_);
982 return error;
983}
984
6fa164b8
DW
985/*
986 * Do we have enough reserve in this AG to handle a reflink? The refcount
987 * btree already reserved all the space it needs, but the rmap btree can grow
988 * infinitely, so we won't allow more reflinks when the AG is down to the
989 * btree reserves.
990 */
991static int
992xfs_reflink_ag_has_free_space(
993 struct xfs_mount *mp,
994 xfs_agnumber_t agno)
995{
996 struct xfs_perag *pag;
997 int error = 0;
998
999 if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
1000 return 0;
1001
1002 pag = xfs_perag_get(mp, agno);
1003 if (xfs_ag_resv_critical(pag, XFS_AG_RESV_AGFL) ||
1004 xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA))
1005 error = -ENOSPC;
1006 xfs_perag_put(pag);
1007 return error;
1008}
1009
862bb360
DW
1010/*
1011 * Unmap a range of blocks from a file, then map other blocks into the hole.
1012 * The range to unmap is (destoff : destoff + srcioff + irec->br_blockcount).
1013 * The extent irec is mapped into dest at irec->br_startoff.
1014 */
1015STATIC int
1016xfs_reflink_remap_extent(
1017 struct xfs_inode *ip,
1018 struct xfs_bmbt_irec *irec,
1019 xfs_fileoff_t destoff,
1020 xfs_off_t new_isize)
1021{
1022 struct xfs_mount *mp = ip->i_mount;
1023 struct xfs_trans *tp;
1024 xfs_fsblock_t firstfsb;
1025 unsigned int resblks;
1026 struct xfs_defer_ops dfops;
1027 struct xfs_bmbt_irec uirec;
1028 bool real_extent;
1029 xfs_filblks_t rlen;
1030 xfs_filblks_t unmap_len;
1031 xfs_off_t newlen;
1032 int error;
1033
1034 unmap_len = irec->br_startoff + irec->br_blockcount - destoff;
1035 trace_xfs_reflink_punch_range(ip, destoff, unmap_len);
1036
1037 /* Only remap normal extents. */
1038 real_extent = (irec->br_startblock != HOLESTARTBLOCK &&
1039 irec->br_startblock != DELAYSTARTBLOCK &&
1040 !ISUNWRITTEN(irec));
1041
6fa164b8
DW
1042 /* No reflinking if we're low on space */
1043 if (real_extent) {
1044 error = xfs_reflink_ag_has_free_space(mp,
1045 XFS_FSB_TO_AGNO(mp, irec->br_startblock));
1046 if (error)
1047 goto out;
1048 }
1049
862bb360
DW
1050 /* Start a rolling transaction to switch the mappings */
1051 resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
1052 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
1053 if (error)
1054 goto out;
1055
1056 xfs_ilock(ip, XFS_ILOCK_EXCL);
1057 xfs_trans_ijoin(tp, ip, 0);
1058
1059 /* If we're not just clearing space, then do we have enough quota? */
1060 if (real_extent) {
1061 error = xfs_trans_reserve_quota_nblks(tp, ip,
1062 irec->br_blockcount, 0, XFS_QMOPT_RES_REGBLKS);
1063 if (error)
1064 goto out_cancel;
1065 }
1066
1067 trace_xfs_reflink_remap(ip, irec->br_startoff,
1068 irec->br_blockcount, irec->br_startblock);
1069
1070 /* Unmap the old blocks in the data fork. */
1071 rlen = unmap_len;
1072 while (rlen) {
1073 xfs_defer_init(&dfops, &firstfsb);
1074 error = __xfs_bunmapi(tp, ip, destoff, &rlen, 0, 1,
1075 &firstfsb, &dfops);
1076 if (error)
1077 goto out_defer;
1078
1079 /*
1080 * Trim the extent to whatever got unmapped.
1081 * Remember, bunmapi works backwards.
1082 */
1083 uirec.br_startblock = irec->br_startblock + rlen;
1084 uirec.br_startoff = irec->br_startoff + rlen;
1085 uirec.br_blockcount = unmap_len - rlen;
1086 unmap_len = rlen;
1087
1088 /* If this isn't a real mapping, we're done. */
1089 if (!real_extent || uirec.br_blockcount == 0)
1090 goto next_extent;
1091
1092 trace_xfs_reflink_remap(ip, uirec.br_startoff,
1093 uirec.br_blockcount, uirec.br_startblock);
1094
1095 /* Update the refcount tree */
1096 error = xfs_refcount_increase_extent(mp, &dfops, &uirec);
1097 if (error)
1098 goto out_defer;
1099
1100 /* Map the new blocks into the data fork. */
1101 error = xfs_bmap_map_extent(mp, &dfops, ip, &uirec);
1102 if (error)
1103 goto out_defer;
1104
1105 /* Update quota accounting. */
1106 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT,
1107 uirec.br_blockcount);
1108
1109 /* Update dest isize if needed. */
1110 newlen = XFS_FSB_TO_B(mp,
1111 uirec.br_startoff + uirec.br_blockcount);
1112 newlen = min_t(xfs_off_t, newlen, new_isize);
1113 if (newlen > i_size_read(VFS_I(ip))) {
1114 trace_xfs_reflink_update_inode_size(ip, newlen);
1115 i_size_write(VFS_I(ip), newlen);
1116 ip->i_d.di_size = newlen;
1117 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1118 }
1119
1120next_extent:
1121 /* Process all the deferred stuff. */
1122 error = xfs_defer_finish(&tp, &dfops, ip);
1123 if (error)
1124 goto out_defer;
1125 }
1126
1127 error = xfs_trans_commit(tp);
1128 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1129 if (error)
1130 goto out;
1131 return 0;
1132
1133out_defer:
1134 xfs_defer_cancel(&dfops);
1135out_cancel:
1136 xfs_trans_cancel(tp);
1137 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1138out:
1139 trace_xfs_reflink_remap_extent_error(ip, error, _RET_IP_);
1140 return error;
1141}
1142
1143/*
1144 * Iteratively remap one file's extents (and holes) to another's.
1145 */
1146STATIC int
1147xfs_reflink_remap_blocks(
1148 struct xfs_inode *src,
1149 xfs_fileoff_t srcoff,
1150 struct xfs_inode *dest,
1151 xfs_fileoff_t destoff,
1152 xfs_filblks_t len,
1153 xfs_off_t new_isize)
1154{
1155 struct xfs_bmbt_irec imap;
1156 int nimaps;
1157 int error = 0;
1158 xfs_filblks_t range_len;
1159
1160 /* drange = (destoff, destoff + len); srange = (srcoff, srcoff + len) */
1161 while (len) {
1162 trace_xfs_reflink_remap_blocks_loop(src, srcoff, len,
1163 dest, destoff);
1164 /* Read extent from the source file */
1165 nimaps = 1;
1166 xfs_ilock(src, XFS_ILOCK_EXCL);
1167 error = xfs_bmapi_read(src, srcoff, len, &imap, &nimaps, 0);
1168 xfs_iunlock(src, XFS_ILOCK_EXCL);
1169 if (error)
1170 goto err;
1171 ASSERT(nimaps == 1);
1172
1173 trace_xfs_reflink_remap_imap(src, srcoff, len, XFS_IO_OVERWRITE,
1174 &imap);
1175
1176 /* Translate imap into the destination file. */
1177 range_len = imap.br_startoff + imap.br_blockcount - srcoff;
1178 imap.br_startoff += destoff - srcoff;
1179
1180 /* Clear dest from destoff to the end of imap and map it in. */
1181 error = xfs_reflink_remap_extent(dest, &imap, destoff,
1182 new_isize);
1183 if (error)
1184 goto err;
1185
1186 if (fatal_signal_pending(current)) {
1187 error = -EINTR;
1188 goto err;
1189 }
1190
1191 /* Advance drange/srange */
1192 srcoff += range_len;
1193 destoff += range_len;
1194 len -= range_len;
1195 }
1196
1197 return 0;
1198
1199err:
1200 trace_xfs_reflink_remap_blocks_error(dest, error, _RET_IP_);
1201 return error;
1202}
1203
cc714660
DW
1204/*
1205 * Read a page's worth of file data into the page cache. Return the page
1206 * locked.
1207 */
1208static struct page *
1209xfs_get_page(
1210 struct inode *inode,
1211 xfs_off_t offset)
1212{
1213 struct address_space *mapping;
1214 struct page *page;
1215 pgoff_t n;
1216
1217 n = offset >> PAGE_SHIFT;
1218 mapping = inode->i_mapping;
1219 page = read_mapping_page(mapping, n, NULL);
1220 if (IS_ERR(page))
1221 return page;
1222 if (!PageUptodate(page)) {
1223 put_page(page);
1224 return ERR_PTR(-EIO);
1225 }
1226 lock_page(page);
1227 return page;
1228}
1229
1230/*
1231 * Compare extents of two files to see if they are the same.
1232 */
1233static int
1234xfs_compare_extents(
1235 struct inode *src,
1236 xfs_off_t srcoff,
1237 struct inode *dest,
1238 xfs_off_t destoff,
1239 xfs_off_t len,
1240 bool *is_same)
1241{
1242 xfs_off_t src_poff;
1243 xfs_off_t dest_poff;
1244 void *src_addr;
1245 void *dest_addr;
1246 struct page *src_page;
1247 struct page *dest_page;
1248 xfs_off_t cmp_len;
1249 bool same;
1250 int error;
1251
1252 error = -EINVAL;
1253 same = true;
1254 while (len) {
1255 src_poff = srcoff & (PAGE_SIZE - 1);
1256 dest_poff = destoff & (PAGE_SIZE - 1);
1257 cmp_len = min(PAGE_SIZE - src_poff,
1258 PAGE_SIZE - dest_poff);
1259 cmp_len = min(cmp_len, len);
1260 ASSERT(cmp_len > 0);
1261
1262 trace_xfs_reflink_compare_extents(XFS_I(src), srcoff, cmp_len,
1263 XFS_I(dest), destoff);
1264
1265 src_page = xfs_get_page(src, srcoff);
1266 if (IS_ERR(src_page)) {
1267 error = PTR_ERR(src_page);
1268 goto out_error;
1269 }
1270 dest_page = xfs_get_page(dest, destoff);
1271 if (IS_ERR(dest_page)) {
1272 error = PTR_ERR(dest_page);
1273 unlock_page(src_page);
1274 put_page(src_page);
1275 goto out_error;
1276 }
1277 src_addr = kmap_atomic(src_page);
1278 dest_addr = kmap_atomic(dest_page);
1279
1280 flush_dcache_page(src_page);
1281 flush_dcache_page(dest_page);
1282
1283 if (memcmp(src_addr + src_poff, dest_addr + dest_poff, cmp_len))
1284 same = false;
1285
1286 kunmap_atomic(dest_addr);
1287 kunmap_atomic(src_addr);
1288 unlock_page(dest_page);
1289 unlock_page(src_page);
1290 put_page(dest_page);
1291 put_page(src_page);
1292
1293 if (!same)
1294 break;
1295
1296 srcoff += cmp_len;
1297 destoff += cmp_len;
1298 len -= cmp_len;
1299 }
1300
1301 *is_same = same;
1302 return 0;
1303
1304out_error:
1305 trace_xfs_reflink_compare_extents_error(XFS_I(dest), error, _RET_IP_);
1306 return error;
1307}
1308
862bb360
DW
1309/*
1310 * Link a range of blocks from one file to another.
1311 */
1312int
1313xfs_reflink_remap_range(
1314 struct xfs_inode *src,
1315 xfs_off_t srcoff,
1316 struct xfs_inode *dest,
1317 xfs_off_t destoff,
cc714660
DW
1318 xfs_off_t len,
1319 unsigned int flags)
862bb360
DW
1320{
1321 struct xfs_mount *mp = src->i_mount;
1322 xfs_fileoff_t sfsbno, dfsbno;
1323 xfs_filblks_t fsblen;
1324 int error;
f7ca3522 1325 xfs_extlen_t cowextsize;
cc714660 1326 bool is_same;
862bb360
DW
1327
1328 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1329 return -EOPNOTSUPP;
1330
1331 if (XFS_FORCED_SHUTDOWN(mp))
1332 return -EIO;
1333
1334 /* Don't reflink realtime inodes */
1335 if (XFS_IS_REALTIME_INODE(src) || XFS_IS_REALTIME_INODE(dest))
1336 return -EINVAL;
1337
cc714660
DW
1338 if (flags & ~XFS_REFLINK_ALL)
1339 return -EINVAL;
1340
862bb360
DW
1341 trace_xfs_reflink_remap_range(src, srcoff, len, dest, destoff);
1342
1343 /* Lock both files against IO */
1344 if (src->i_ino == dest->i_ino) {
1345 xfs_ilock(src, XFS_IOLOCK_EXCL);
1346 xfs_ilock(src, XFS_MMAPLOCK_EXCL);
1347 } else {
1348 xfs_lock_two_inodes(src, dest, XFS_IOLOCK_EXCL);
1349 xfs_lock_two_inodes(src, dest, XFS_MMAPLOCK_EXCL);
1350 }
1351
cc714660
DW
1352 /*
1353 * Check that the extents are the same.
1354 */
1355 if (flags & XFS_REFLINK_DEDUPE) {
1356 is_same = false;
1357 error = xfs_compare_extents(VFS_I(src), srcoff, VFS_I(dest),
1358 destoff, len, &is_same);
1359 if (error)
1360 goto out_error;
1361 if (!is_same) {
1362 error = -EBADE;
1363 goto out_error;
1364 }
1365 }
1366
862bb360
DW
1367 error = xfs_reflink_set_inode_flag(src, dest);
1368 if (error)
1369 goto out_error;
1370
1371 /*
1372 * Invalidate the page cache so that we can clear any CoW mappings
1373 * in the destination file.
1374 */
1375 truncate_inode_pages_range(&VFS_I(dest)->i_data, destoff,
1376 PAGE_ALIGN(destoff + len) - 1);
1377
1378 dfsbno = XFS_B_TO_FSBT(mp, destoff);
1379 sfsbno = XFS_B_TO_FSBT(mp, srcoff);
1380 fsblen = XFS_B_TO_FSB(mp, len);
1381 error = xfs_reflink_remap_blocks(src, sfsbno, dest, dfsbno, fsblen,
1382 destoff + len);
1383 if (error)
1384 goto out_error;
1385
f7ca3522
DW
1386 /*
1387 * Carry the cowextsize hint from src to dest if we're sharing the
1388 * entire source file to the entire destination file, the source file
1389 * has a cowextsize hint, and the destination file does not.
1390 */
1391 cowextsize = 0;
1392 if (srcoff == 0 && len == i_size_read(VFS_I(src)) &&
1393 (src->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) &&
1394 destoff == 0 && len >= i_size_read(VFS_I(dest)) &&
1395 !(dest->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1396 cowextsize = src->i_d.di_cowextsize;
1397
1398 error = xfs_reflink_update_dest(dest, destoff + len, cowextsize);
862bb360
DW
1399 if (error)
1400 goto out_error;
1401
1402out_error:
1403 xfs_iunlock(src, XFS_MMAPLOCK_EXCL);
1404 xfs_iunlock(src, XFS_IOLOCK_EXCL);
1405 if (src->i_ino != dest->i_ino) {
1406 xfs_iunlock(dest, XFS_MMAPLOCK_EXCL);
1407 xfs_iunlock(dest, XFS_IOLOCK_EXCL);
1408 }
1409 if (error)
1410 trace_xfs_reflink_remap_range_error(dest, error, _RET_IP_);
1411 return error;
1412}
98cc2db5
DW
1413
1414/*
1415 * The user wants to preemptively CoW all shared blocks in this file,
1416 * which enables us to turn off the reflink flag. Iterate all
1417 * extents which are not prealloc/delalloc to see which ranges are
1418 * mentioned in the refcount tree, then read those blocks into the
1419 * pagecache, dirty them, fsync them back out, and then we can update
1420 * the inode flag. What happens if we run out of memory? :)
1421 */
1422STATIC int
1423xfs_reflink_dirty_extents(
1424 struct xfs_inode *ip,
1425 xfs_fileoff_t fbno,
1426 xfs_filblks_t end,
1427 xfs_off_t isize)
1428{
1429 struct xfs_mount *mp = ip->i_mount;
1430 xfs_agnumber_t agno;
1431 xfs_agblock_t agbno;
1432 xfs_extlen_t aglen;
1433 xfs_agblock_t rbno;
1434 xfs_extlen_t rlen;
1435 xfs_off_t fpos;
1436 xfs_off_t flen;
1437 struct xfs_bmbt_irec map[2];
1438 int nmaps;
1439 int error;
1440
1441 while (end - fbno > 0) {
1442 nmaps = 1;
1443 /*
1444 * Look for extents in the file. Skip holes, delalloc, or
1445 * unwritten extents; they can't be reflinked.
1446 */
1447 error = xfs_bmapi_read(ip, fbno, end - fbno, map, &nmaps, 0);
1448 if (error)
1449 goto out;
1450 if (nmaps == 0)
1451 break;
1452 if (map[0].br_startblock == HOLESTARTBLOCK ||
1453 map[0].br_startblock == DELAYSTARTBLOCK ||
1454 ISUNWRITTEN(&map[0]))
1455 goto next;
1456
1457 map[1] = map[0];
1458 while (map[1].br_blockcount) {
1459 agno = XFS_FSB_TO_AGNO(mp, map[1].br_startblock);
1460 agbno = XFS_FSB_TO_AGBNO(mp, map[1].br_startblock);
1461 aglen = map[1].br_blockcount;
1462
1463 error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
1464 &rbno, &rlen, true);
1465 if (error)
1466 goto out;
1467 if (rbno == NULLAGBLOCK)
1468 break;
1469
1470 /* Dirty the pages */
1471 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1472 fpos = XFS_FSB_TO_B(mp, map[1].br_startoff +
1473 (rbno - agbno));
1474 flen = XFS_FSB_TO_B(mp, rlen);
1475 if (fpos + flen > isize)
1476 flen = isize - fpos;
1477 error = iomap_file_dirty(VFS_I(ip), fpos, flen,
1478 &xfs_iomap_ops);
1479 xfs_ilock(ip, XFS_ILOCK_EXCL);
1480 if (error)
1481 goto out;
1482
1483 map[1].br_blockcount -= (rbno - agbno + rlen);
1484 map[1].br_startoff += (rbno - agbno + rlen);
1485 map[1].br_startblock += (rbno - agbno + rlen);
1486 }
1487
1488next:
1489 fbno = map[0].br_startoff + map[0].br_blockcount;
1490 }
1491out:
1492 return error;
1493}
1494
1495/* Clear the inode reflink flag if there are no shared extents. */
1496int
1497xfs_reflink_clear_inode_flag(
1498 struct xfs_inode *ip,
1499 struct xfs_trans **tpp)
1500{
1501 struct xfs_mount *mp = ip->i_mount;
1502 xfs_fileoff_t fbno;
1503 xfs_filblks_t end;
1504 xfs_agnumber_t agno;
1505 xfs_agblock_t agbno;
1506 xfs_extlen_t aglen;
1507 xfs_agblock_t rbno;
1508 xfs_extlen_t rlen;
1509 struct xfs_bmbt_irec map[2];
1510 int nmaps;
1511 int error = 0;
1512
1513 if (!(ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK))
1514 return 0;
1515
1516 fbno = 0;
1517 end = XFS_B_TO_FSB(mp, i_size_read(VFS_I(ip)));
1518 while (end - fbno > 0) {
1519 nmaps = 1;
1520 /*
1521 * Look for extents in the file. Skip holes, delalloc, or
1522 * unwritten extents; they can't be reflinked.
1523 */
1524 error = xfs_bmapi_read(ip, fbno, end - fbno, map, &nmaps, 0);
1525 if (error)
1526 return error;
1527 if (nmaps == 0)
1528 break;
1529 if (map[0].br_startblock == HOLESTARTBLOCK ||
1530 map[0].br_startblock == DELAYSTARTBLOCK ||
1531 ISUNWRITTEN(&map[0]))
1532 goto next;
1533
1534 map[1] = map[0];
1535 while (map[1].br_blockcount) {
1536 agno = XFS_FSB_TO_AGNO(mp, map[1].br_startblock);
1537 agbno = XFS_FSB_TO_AGBNO(mp, map[1].br_startblock);
1538 aglen = map[1].br_blockcount;
1539
1540 error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
1541 &rbno, &rlen, false);
1542 if (error)
1543 return error;
1544 /* Is there still a shared block here? */
1545 if (rbno != NULLAGBLOCK)
1546 return 0;
1547
1548 map[1].br_blockcount -= aglen;
1549 map[1].br_startoff += aglen;
1550 map[1].br_startblock += aglen;
1551 }
1552
1553next:
1554 fbno = map[0].br_startoff + map[0].br_blockcount;
1555 }
1556
1557 /*
1558 * We didn't find any shared blocks so turn off the reflink flag.
1559 * First, get rid of any leftover CoW mappings.
1560 */
1561 error = xfs_reflink_cancel_cow_blocks(ip, tpp, 0, NULLFILEOFF);
1562 if (error)
1563 return error;
1564
1565 /* Clear the inode flag. */
1566 trace_xfs_reflink_unset_inode_flag(ip);
1567 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
83104d44 1568 xfs_inode_clear_cowblocks_tag(ip);
98cc2db5
DW
1569 xfs_trans_ijoin(*tpp, ip, 0);
1570 xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
1571
1572 return error;
1573}
1574
1575/*
1576 * Clear the inode reflink flag if there are no shared extents and the size
1577 * hasn't changed.
1578 */
1579STATIC int
1580xfs_reflink_try_clear_inode_flag(
1581 struct xfs_inode *ip,
1582 xfs_off_t old_isize)
1583{
1584 struct xfs_mount *mp = ip->i_mount;
1585 struct xfs_trans *tp;
1586 int error = 0;
1587
1588 /* Start a rolling transaction to remove the mappings */
1589 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
1590 if (error)
1591 return error;
1592
1593 xfs_ilock(ip, XFS_ILOCK_EXCL);
1594 xfs_trans_ijoin(tp, ip, 0);
1595
1596 if (old_isize != i_size_read(VFS_I(ip)))
1597 goto cancel;
1598
1599 error = xfs_reflink_clear_inode_flag(ip, &tp);
1600 if (error)
1601 goto cancel;
1602
1603 error = xfs_trans_commit(tp);
1604 if (error)
1605 goto out;
1606
1607 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1608 return 0;
1609cancel:
1610 xfs_trans_cancel(tp);
1611out:
1612 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1613 return error;
1614}
1615
1616/*
1617 * Pre-COW all shared blocks within a given byte range of a file and turn off
1618 * the reflink flag if we unshare all of the file's blocks.
1619 */
1620int
1621xfs_reflink_unshare(
1622 struct xfs_inode *ip,
1623 xfs_off_t offset,
1624 xfs_off_t len)
1625{
1626 struct xfs_mount *mp = ip->i_mount;
1627 xfs_fileoff_t fbno;
1628 xfs_filblks_t end;
1629 xfs_off_t isize;
1630 int error;
1631
1632 if (!xfs_is_reflink_inode(ip))
1633 return 0;
1634
1635 trace_xfs_reflink_unshare(ip, offset, len);
1636
1637 inode_dio_wait(VFS_I(ip));
1638
1639 /* Try to CoW the selected ranges */
1640 xfs_ilock(ip, XFS_ILOCK_EXCL);
1641 fbno = XFS_B_TO_FSB(mp, offset);
1642 isize = i_size_read(VFS_I(ip));
1643 end = XFS_B_TO_FSB(mp, offset + len);
1644 error = xfs_reflink_dirty_extents(ip, fbno, end, isize);
1645 if (error)
1646 goto out_unlock;
1647 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1648
1649 /* Wait for the IO to finish */
1650 error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
1651 if (error)
1652 goto out;
1653
1654 /* Turn off the reflink flag if we unshared the whole file */
1655 if (offset == 0 && len == isize) {
1656 error = xfs_reflink_try_clear_inode_flag(ip, isize);
1657 if (error)
1658 goto out;
1659 }
1660
1661 return 0;
1662
1663out_unlock:
1664 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1665out:
1666 trace_xfs_reflink_unshare_error(ip, error, _RET_IP_);
1667 return error;
1668}
83104d44
DW
1669
1670/*
1671 * Does this inode have any real CoW reservations?
1672 */
1673bool
1674xfs_reflink_has_real_cow_blocks(
1675 struct xfs_inode *ip)
1676{
1677 struct xfs_bmbt_irec irec;
1678 struct xfs_ifork *ifp;
1679 struct xfs_bmbt_rec_host *gotp;
1680 xfs_extnum_t idx;
1681
1682 if (!xfs_is_reflink_inode(ip))
1683 return false;
1684
1685 /* Go find the old extent in the CoW fork. */
1686 ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
1687 gotp = xfs_iext_bno_to_ext(ifp, 0, &idx);
1688 while (gotp) {
1689 xfs_bmbt_get_all(gotp, &irec);
1690
1691 if (!isnullstartblock(irec.br_startblock))
1692 return true;
1693
1694 /* Roll on... */
1695 idx++;
1696 if (idx >= ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
1697 break;
1698 gotp = xfs_iext_get_ext(ifp, idx);
1699 }
1700
1701 return false;
1702}