2 * Copyright (C) 2016 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
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.
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.
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.
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"
34 #include "xfs_bmap_util.h"
35 #include "xfs_error.h"
37 #include "xfs_dir2_priv.h"
38 #include "xfs_ioctl.h"
39 #include "xfs_trace.h"
41 #include "xfs_icache.h"
43 #include "xfs_btree.h"
44 #include "xfs_refcount_btree.h"
45 #include "xfs_refcount.h"
46 #include "xfs_bmap_btree.h"
47 #include "xfs_trans_space.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"
55 #include "xfs_iomap.h"
56 #include "xfs_rmap_btree.h"
59 * Copy on Write of Shared Blocks
61 * XFS must preserve "the usual" file semantics even when two files share
62 * the same physical blocks. This means that a write to one file must not
63 * alter the blocks in a different file; the way that we'll do that is
64 * through the use of a copy-on-write mechanism. At a high level, that
65 * means that when we want to write to a shared block, we allocate a new
66 * block, write the data to the new block, and if that succeeds we map the
67 * new block into the file.
69 * XFS provides a "delayed allocation" mechanism that defers the allocation
70 * of disk blocks to dirty-but-not-yet-mapped file blocks as long as
71 * possible. This reduces fragmentation by enabling the filesystem to ask
72 * for bigger chunks less often, which is exactly what we want for CoW.
74 * The delalloc mechanism begins when the kernel wants to make a block
75 * writable (write_begin or page_mkwrite). If the offset is not mapped, we
76 * create a delalloc mapping, which is a regular in-core extent, but without
77 * a real startblock. (For delalloc mappings, the startblock encodes both
78 * a flag that this is a delalloc mapping, and a worst-case estimate of how
79 * many blocks might be required to put the mapping into the BMBT.) delalloc
80 * mappings are a reservation against the free space in the filesystem;
81 * adjacent mappings can also be combined into fewer larger mappings.
83 * When dirty pages are being written out (typically in writepage), the
84 * delalloc reservations are converted into real mappings by allocating
85 * blocks and replacing the delalloc mapping with real ones. A delalloc
86 * mapping can be replaced by several real ones if the free space is
89 * We want to adapt the delalloc mechanism for copy-on-write, since the
90 * write paths are similar. The first two steps (creating the reservation
91 * and allocating the blocks) are exactly the same as delalloc except that
92 * the mappings must be stored in a separate CoW fork because we do not want
93 * to disturb the mapping in the data fork until we're sure that the write
94 * succeeded. IO completion in this case is the process of removing the old
95 * mapping from the data fork and moving the new mapping from the CoW fork to
96 * the data fork. This will be discussed shortly.
98 * For now, unaligned directio writes will be bounced back to the page cache.
99 * Block-aligned directio writes will use the same mechanism as buffered
102 * CoW remapping must be done after the data block write completes,
103 * because we don't want to destroy the old data fork map until we're sure
104 * the new block has been written. Since the new mappings are kept in a
105 * separate fork, we can simply iterate these mappings to find the ones
106 * that cover the file blocks that we just CoW'd. For each extent, simply
107 * unmap the corresponding range in the data fork, map the new range into
108 * the data fork, and remove the extent from the CoW fork.
110 * Since the remapping operation can be applied to an arbitrary file
111 * range, we record the need for the remap step as a flag in the ioend
112 * instead of declaring a new IO type. This is required for direct io
113 * because we only have ioend for the whole dio, and we have to be able to
114 * remember the presence of unwritten blocks and CoW blocks with a single
115 * ioend structure. Better yet, the more ground we can cover with one
120 * Given an AG extent, find the lowest-numbered run of shared blocks
121 * within that range and return the range in fbno/flen. If
122 * find_end_of_shared is true, return the longest contiguous extent of
123 * shared blocks. If there are no shared extents, fbno and flen will
124 * be set to NULLAGBLOCK and 0, respectively.
127 xfs_reflink_find_shared(
128 struct xfs_mount
*mp
,
134 bool find_end_of_shared
)
136 struct xfs_buf
*agbp
;
137 struct xfs_btree_cur
*cur
;
140 error
= xfs_alloc_read_agf(mp
, NULL
, agno
, 0, &agbp
);
144 cur
= xfs_refcountbt_init_cursor(mp
, NULL
, agbp
, agno
, NULL
);
146 error
= xfs_refcount_find_shared(cur
, agbno
, aglen
, fbno
, flen
,
149 xfs_btree_del_cursor(cur
, error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
156 * Trim the mapping to the next block where there's a change in the
157 * shared/unshared status. More specifically, this means that we
158 * find the lowest-numbered extent of shared blocks that coincides with
159 * the given block mapping. If the shared extent overlaps the start of
160 * the mapping, trim the mapping to the end of the shared extent. If
161 * the shared region intersects the mapping, trim the mapping to the
162 * start of the shared extent. If there are no shared regions that
163 * overlap, just return the original extent.
166 xfs_reflink_trim_around_shared(
167 struct xfs_inode
*ip
,
168 struct xfs_bmbt_irec
*irec
,
179 /* Holes, unwritten, and delalloc extents cannot be shared */
180 if (!xfs_is_reflink_inode(ip
) ||
182 irec
->br_startblock
== HOLESTARTBLOCK
||
183 irec
->br_startblock
== DELAYSTARTBLOCK
) {
188 trace_xfs_reflink_trim_around_shared(ip
, irec
);
190 agno
= XFS_FSB_TO_AGNO(ip
->i_mount
, irec
->br_startblock
);
191 agbno
= XFS_FSB_TO_AGBNO(ip
->i_mount
, irec
->br_startblock
);
192 aglen
= irec
->br_blockcount
;
194 error
= xfs_reflink_find_shared(ip
->i_mount
, agno
, agbno
,
195 aglen
, &fbno
, &flen
, true);
199 *shared
= *trimmed
= false;
200 if (fbno
== NULLAGBLOCK
) {
201 /* No shared blocks at all. */
203 } else if (fbno
== agbno
) {
205 * The start of this extent is shared. Truncate the
206 * mapping at the end of the shared region so that a
207 * subsequent iteration starts at the start of the
210 irec
->br_blockcount
= flen
;
217 * There's a shared extent midway through this extent.
218 * Truncate the mapping at the start of the shared
219 * extent so that a subsequent iteration starts at the
220 * start of the shared region.
222 irec
->br_blockcount
= fbno
- agbno
;
228 /* Create a CoW reservation for a range of blocks within a file. */
230 __xfs_reflink_reserve_cow(
231 struct xfs_inode
*ip
,
232 xfs_fileoff_t
*offset_fsb
,
233 xfs_fileoff_t end_fsb
,
236 struct xfs_bmbt_irec got
, prev
, imap
;
237 xfs_fileoff_t orig_end_fsb
;
238 int nimaps
, eof
= 0, error
= 0;
239 bool shared
= false, trimmed
= false;
242 /* Already reserved? Skip the refcount btree access. */
243 xfs_bmap_search_extents(ip
, *offset_fsb
, XFS_COW_FORK
, &eof
, &idx
,
245 if (!eof
&& got
.br_startoff
<= *offset_fsb
) {
246 end_fsb
= orig_end_fsb
= got
.br_startoff
+ got
.br_blockcount
;
247 trace_xfs_reflink_cow_found(ip
, &got
);
251 /* Read extent from the source file. */
253 error
= xfs_bmapi_read(ip
, *offset_fsb
, end_fsb
- *offset_fsb
,
259 /* Trim the mapping to the nearest shared extent boundary. */
260 error
= xfs_reflink_trim_around_shared(ip
, &imap
, &shared
, &trimmed
);
264 end_fsb
= orig_end_fsb
= imap
.br_startoff
+ imap
.br_blockcount
;
266 /* Not shared? Just report the (potentially capped) extent. */
273 * Fork all the shared blocks from our write offset until the end of
276 error
= xfs_qm_dqattach_locked(ip
, 0);
281 error
= xfs_bmapi_reserve_delalloc(ip
, XFS_COW_FORK
, *offset_fsb
,
282 end_fsb
- *offset_fsb
, &got
,
289 /* retry without any preallocation */
290 trace_xfs_reflink_cow_enospc(ip
, &imap
);
291 if (end_fsb
!= orig_end_fsb
) {
292 end_fsb
= orig_end_fsb
;
300 trace_xfs_reflink_cow_alloc(ip
, &got
);
302 *offset_fsb
= end_fsb
;
307 /* Create a CoW reservation for part of a file. */
309 xfs_reflink_reserve_cow_range(
310 struct xfs_inode
*ip
,
314 struct xfs_mount
*mp
= ip
->i_mount
;
315 xfs_fileoff_t offset_fsb
, end_fsb
;
316 bool skipped
= false;
319 trace_xfs_reflink_reserve_cow_range(ip
, offset
, count
);
321 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
322 end_fsb
= XFS_B_TO_FSB(mp
, offset
+ count
);
324 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
325 while (offset_fsb
< end_fsb
) {
326 error
= __xfs_reflink_reserve_cow(ip
, &offset_fsb
, end_fsb
,
329 trace_xfs_reflink_reserve_cow_range_error(ip
, error
,
334 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
339 /* Allocate all CoW reservations covering a range of blocks in a file. */
341 __xfs_reflink_allocate_cow(
342 struct xfs_inode
*ip
,
343 xfs_fileoff_t
*offset_fsb
,
344 xfs_fileoff_t end_fsb
)
346 struct xfs_mount
*mp
= ip
->i_mount
;
347 struct xfs_bmbt_irec imap
;
348 struct xfs_defer_ops dfops
;
349 struct xfs_trans
*tp
;
350 xfs_fsblock_t first_block
;
351 xfs_fileoff_t next_fsb
;
352 int nimaps
= 1, error
;
353 bool skipped
= false;
355 xfs_defer_init(&dfops
, &first_block
);
357 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
, 0, 0,
358 XFS_TRANS_RESERVE
, &tp
);
362 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
364 next_fsb
= *offset_fsb
;
365 error
= __xfs_reflink_reserve_cow(ip
, &next_fsb
, end_fsb
, &skipped
);
367 goto out_trans_cancel
;
370 *offset_fsb
= next_fsb
;
371 goto out_trans_cancel
;
374 xfs_trans_ijoin(tp
, ip
, 0);
375 error
= xfs_bmapi_write(tp
, ip
, *offset_fsb
, next_fsb
- *offset_fsb
,
376 XFS_BMAPI_COWFORK
, &first_block
,
377 XFS_EXTENTADD_SPACE_RES(mp
, XFS_DATA_FORK
),
378 &imap
, &nimaps
, &dfops
);
380 goto out_trans_cancel
;
382 /* We might not have been able to map the whole delalloc extent */
383 *offset_fsb
= min(*offset_fsb
+ imap
.br_blockcount
, next_fsb
);
385 error
= xfs_defer_finish(&tp
, &dfops
, NULL
);
387 goto out_trans_cancel
;
389 error
= xfs_trans_commit(tp
);
392 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
395 xfs_defer_cancel(&dfops
);
396 xfs_trans_cancel(tp
);
400 /* Allocate all CoW reservations covering a part of a file. */
402 xfs_reflink_allocate_cow_range(
403 struct xfs_inode
*ip
,
407 struct xfs_mount
*mp
= ip
->i_mount
;
408 xfs_fileoff_t offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
409 xfs_fileoff_t end_fsb
= XFS_B_TO_FSB(mp
, offset
+ count
);
412 ASSERT(xfs_is_reflink_inode(ip
));
414 trace_xfs_reflink_allocate_cow_range(ip
, offset
, count
);
417 * Make sure that the dquots are there.
419 error
= xfs_qm_dqattach(ip
, 0);
423 while (offset_fsb
< end_fsb
) {
424 error
= __xfs_reflink_allocate_cow(ip
, &offset_fsb
, end_fsb
);
426 trace_xfs_reflink_allocate_cow_range_error(ip
, error
,
436 * Find the CoW reservation (and whether or not it needs block allocation)
437 * for a given byte offset of a file.
440 xfs_reflink_find_cow_mapping(
441 struct xfs_inode
*ip
,
443 struct xfs_bmbt_irec
*imap
,
446 struct xfs_bmbt_irec irec
;
447 struct xfs_ifork
*ifp
;
448 struct xfs_bmbt_rec_host
*gotp
;
452 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
| XFS_ILOCK_SHARED
));
453 ASSERT(xfs_is_reflink_inode(ip
));
455 /* Find the extent in the CoW fork. */
456 ifp
= XFS_IFORK_PTR(ip
, XFS_COW_FORK
);
457 bno
= XFS_B_TO_FSBT(ip
->i_mount
, offset
);
458 gotp
= xfs_iext_bno_to_ext(ifp
, bno
, &idx
);
462 xfs_bmbt_get_all(gotp
, &irec
);
463 if (bno
>= irec
.br_startoff
+ irec
.br_blockcount
||
464 bno
< irec
.br_startoff
)
467 trace_xfs_reflink_find_cow_mapping(ip
, offset
, 1, XFS_IO_OVERWRITE
,
470 /* If it's still delalloc, we must allocate later. */
472 *need_alloc
= !!(isnullstartblock(irec
.br_startblock
));
478 * Trim an extent to end at the next CoW reservation past offset_fsb.
481 xfs_reflink_trim_irec_to_next_cow(
482 struct xfs_inode
*ip
,
483 xfs_fileoff_t offset_fsb
,
484 struct xfs_bmbt_irec
*imap
)
486 struct xfs_bmbt_irec irec
;
487 struct xfs_ifork
*ifp
;
488 struct xfs_bmbt_rec_host
*gotp
;
491 if (!xfs_is_reflink_inode(ip
))
494 /* Find the extent in the CoW fork. */
495 ifp
= XFS_IFORK_PTR(ip
, XFS_COW_FORK
);
496 gotp
= xfs_iext_bno_to_ext(ifp
, offset_fsb
, &idx
);
499 xfs_bmbt_get_all(gotp
, &irec
);
501 /* This is the extent before; try sliding up one. */
502 if (irec
.br_startoff
< offset_fsb
) {
504 if (idx
>= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
))
506 gotp
= xfs_iext_get_ext(ifp
, idx
);
507 xfs_bmbt_get_all(gotp
, &irec
);
510 if (irec
.br_startoff
>= imap
->br_startoff
+ imap
->br_blockcount
)
513 imap
->br_blockcount
= irec
.br_startoff
- imap
->br_startoff
;
514 trace_xfs_reflink_trim_irec(ip
, imap
);
520 * Cancel all pending CoW reservations for some block range of an inode.
523 xfs_reflink_cancel_cow_blocks(
524 struct xfs_inode
*ip
,
525 struct xfs_trans
**tpp
,
526 xfs_fileoff_t offset_fsb
,
527 xfs_fileoff_t end_fsb
)
529 struct xfs_bmbt_irec irec
;
530 xfs_filblks_t count_fsb
;
531 xfs_fsblock_t firstfsb
;
532 struct xfs_defer_ops dfops
;
536 if (!xfs_is_reflink_inode(ip
))
539 /* Go find the old extent in the CoW fork. */
540 while (offset_fsb
< end_fsb
) {
542 count_fsb
= (xfs_filblks_t
)(end_fsb
- offset_fsb
);
543 error
= xfs_bmapi_read(ip
, offset_fsb
, count_fsb
, &irec
,
544 &nimaps
, XFS_BMAPI_COWFORK
);
549 trace_xfs_reflink_cancel_cow(ip
, &irec
);
551 if (irec
.br_startblock
== DELAYSTARTBLOCK
) {
552 /* Free a delayed allocation. */
553 xfs_mod_fdblocks(ip
->i_mount
, irec
.br_blockcount
,
555 ip
->i_delayed_blks
-= irec
.br_blockcount
;
557 /* Remove the mapping from the CoW fork. */
558 error
= xfs_bunmapi_cow(ip
, &irec
);
561 } else if (irec
.br_startblock
== HOLESTARTBLOCK
) {
564 xfs_trans_ijoin(*tpp
, ip
, 0);
565 xfs_defer_init(&dfops
, &firstfsb
);
567 /* Free the CoW orphan record. */
568 error
= xfs_refcount_free_cow_extent(ip
->i_mount
,
569 &dfops
, irec
.br_startblock
,
574 xfs_bmap_add_free(ip
->i_mount
, &dfops
,
575 irec
.br_startblock
, irec
.br_blockcount
,
578 /* Update quota accounting */
579 xfs_trans_mod_dquot_byino(*tpp
, ip
, XFS_TRANS_DQ_BCOUNT
,
580 -(long)irec
.br_blockcount
);
582 /* Roll the transaction */
583 error
= xfs_defer_finish(tpp
, &dfops
, ip
);
585 xfs_defer_cancel(&dfops
);
589 /* Remove the mapping from the CoW fork. */
590 error
= xfs_bunmapi_cow(ip
, &irec
);
596 offset_fsb
= irec
.br_startoff
+ irec
.br_blockcount
;
603 * Cancel all pending CoW reservations for some byte range of an inode.
606 xfs_reflink_cancel_cow_range(
607 struct xfs_inode
*ip
,
611 struct xfs_trans
*tp
;
612 xfs_fileoff_t offset_fsb
;
613 xfs_fileoff_t end_fsb
;
616 trace_xfs_reflink_cancel_cow_range(ip
, offset
, count
);
618 offset_fsb
= XFS_B_TO_FSBT(ip
->i_mount
, offset
);
619 if (count
== NULLFILEOFF
)
620 end_fsb
= NULLFILEOFF
;
622 end_fsb
= XFS_B_TO_FSB(ip
->i_mount
, offset
+ count
);
624 /* Start a rolling transaction to remove the mappings */
625 error
= xfs_trans_alloc(ip
->i_mount
, &M_RES(ip
->i_mount
)->tr_write
,
630 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
631 xfs_trans_ijoin(tp
, ip
, 0);
633 /* Scrape out the old CoW reservations */
634 error
= xfs_reflink_cancel_cow_blocks(ip
, &tp
, offset_fsb
, end_fsb
);
638 error
= xfs_trans_commit(tp
);
640 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
644 xfs_trans_cancel(tp
);
645 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
647 trace_xfs_reflink_cancel_cow_range_error(ip
, error
, _RET_IP_
);
652 * Remap parts of a file's data fork after a successful CoW.
656 struct xfs_inode
*ip
,
660 struct xfs_bmbt_irec irec
;
661 struct xfs_bmbt_irec uirec
;
662 struct xfs_trans
*tp
;
663 xfs_fileoff_t offset_fsb
;
664 xfs_fileoff_t end_fsb
;
665 xfs_filblks_t count_fsb
;
666 xfs_fsblock_t firstfsb
;
667 struct xfs_defer_ops dfops
;
669 unsigned int resblks
;
674 trace_xfs_reflink_end_cow(ip
, offset
, count
);
676 offset_fsb
= XFS_B_TO_FSBT(ip
->i_mount
, offset
);
677 end_fsb
= XFS_B_TO_FSB(ip
->i_mount
, offset
+ count
);
678 count_fsb
= (xfs_filblks_t
)(end_fsb
- offset_fsb
);
680 /* Start a rolling transaction to switch the mappings */
681 resblks
= XFS_EXTENTADD_SPACE_RES(ip
->i_mount
, XFS_DATA_FORK
);
682 error
= xfs_trans_alloc(ip
->i_mount
, &M_RES(ip
->i_mount
)->tr_write
,
687 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
688 xfs_trans_ijoin(tp
, ip
, 0);
690 /* Go find the old extent in the CoW fork. */
691 while (offset_fsb
< end_fsb
) {
692 /* Read extent from the source file */
694 count_fsb
= (xfs_filblks_t
)(end_fsb
- offset_fsb
);
695 error
= xfs_bmapi_read(ip
, offset_fsb
, count_fsb
, &irec
,
696 &nimaps
, XFS_BMAPI_COWFORK
);
701 ASSERT(irec
.br_startblock
!= DELAYSTARTBLOCK
);
702 trace_xfs_reflink_cow_remap(ip
, &irec
);
705 * We can have a hole in the CoW fork if part of a directio
706 * write is CoW but part of it isn't.
708 rlen
= ilen
= irec
.br_blockcount
;
709 if (irec
.br_startblock
== HOLESTARTBLOCK
)
712 /* Unmap the old blocks in the data fork. */
714 xfs_defer_init(&dfops
, &firstfsb
);
715 error
= __xfs_bunmapi(tp
, ip
, irec
.br_startoff
,
716 &rlen
, 0, 1, &firstfsb
, &dfops
);
721 * Trim the extent to whatever got unmapped.
722 * Remember, bunmapi works backwards.
724 uirec
.br_startblock
= irec
.br_startblock
+ rlen
;
725 uirec
.br_startoff
= irec
.br_startoff
+ rlen
;
726 uirec
.br_blockcount
= irec
.br_blockcount
- rlen
;
727 irec
.br_blockcount
= rlen
;
728 trace_xfs_reflink_cow_remap_piece(ip
, &uirec
);
730 /* Free the CoW orphan record. */
731 error
= xfs_refcount_free_cow_extent(tp
->t_mountp
,
732 &dfops
, uirec
.br_startblock
,
733 uirec
.br_blockcount
);
737 /* Map the new blocks into the data fork. */
738 error
= xfs_bmap_map_extent(tp
->t_mountp
, &dfops
,
743 /* Remove the mapping from the CoW fork. */
744 error
= xfs_bunmapi_cow(ip
, &uirec
);
748 error
= xfs_defer_finish(&tp
, &dfops
, ip
);
755 offset_fsb
= irec
.br_startoff
+ ilen
;
758 error
= xfs_trans_commit(tp
);
759 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
765 xfs_defer_cancel(&dfops
);
767 xfs_trans_cancel(tp
);
768 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
770 trace_xfs_reflink_end_cow_error(ip
, error
, _RET_IP_
);
775 * Free leftover CoW reservations that didn't get cleaned out.
778 xfs_reflink_recover_cow(
779 struct xfs_mount
*mp
)
784 if (!xfs_sb_version_hasreflink(&mp
->m_sb
))
787 for (agno
= 0; agno
< mp
->m_sb
.sb_agcount
; agno
++) {
788 error
= xfs_refcount_recover_cow_leftovers(mp
, agno
);
797 * Reflinking (Block) Ranges of Two Files Together
799 * First, ensure that the reflink flag is set on both inodes. The flag is an
800 * optimization to avoid unnecessary refcount btree lookups in the write path.
802 * Now we can iteratively remap the range of extents (and holes) in src to the
803 * corresponding ranges in dest. Let drange and srange denote the ranges of
804 * logical blocks in dest and src touched by the reflink operation.
806 * While the length of drange is greater than zero,
807 * - Read src's bmbt at the start of srange ("imap")
808 * - If imap doesn't exist, make imap appear to start at the end of srange
810 * - If imap starts before srange, advance imap to start at srange.
811 * - If imap goes beyond srange, truncate imap to end at the end of srange.
812 * - Punch (imap start - srange start + imap len) blocks from dest at
813 * offset (drange start).
814 * - If imap points to a real range of pblks,
815 * > Increase the refcount of the imap's pblks
816 * > Map imap's pblks into dest at the offset
817 * (drange start + imap start - srange start)
818 * - Advance drange and srange by (imap start - srange start + imap len)
820 * Finally, if the reflink made dest longer, update both the in-core and
821 * on-disk file sizes.
823 * ASCII Art Demonstration:
825 * Let's say we want to reflink this source file:
827 * ----SSSSSSS-SSSSS----SSSSSS (src file)
828 * <-------------------->
830 * into this destination file:
832 * --DDDDDDDDDDDDDDDDDDD--DDD (dest file)
833 * <-------------------->
834 * '-' means a hole, and 'S' and 'D' are written blocks in the src and dest.
835 * Observe that the range has different logical offsets in either file.
837 * Consider that the first extent in the source file doesn't line up with our
838 * reflink range. Unmapping and remapping are separate operations, so we can
839 * unmap more blocks from the destination file than we remap.
841 * ----SSSSSSS-SSSSS----SSSSSS
843 * --DDDDD---------DDDDD--DDD
846 * Now remap the source extent into the destination file:
848 * ----SSSSSSS-SSSSS----SSSSSS
850 * --DDDDD--SSSSSSSDDDDD--DDD
853 * Do likewise with the second hole and extent in our range. Holes in the
854 * unmap range don't affect our operation.
856 * ----SSSSSSS-SSSSS----SSSSSS
858 * --DDDDD--SSSSSSS-SSSSS-DDD
861 * Finally, unmap and remap part of the third extent. This will increase the
862 * size of the destination file.
864 * ----SSSSSSS-SSSSS----SSSSSS
866 * --DDDDD--SSSSSSS-SSSSS----SSS
869 * Once we update the destination file's i_size, we're done.
873 * Ensure the reflink bit is set in both inodes.
876 xfs_reflink_set_inode_flag(
877 struct xfs_inode
*src
,
878 struct xfs_inode
*dest
)
880 struct xfs_mount
*mp
= src
->i_mount
;
882 struct xfs_trans
*tp
;
884 if (xfs_is_reflink_inode(src
) && xfs_is_reflink_inode(dest
))
887 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_ichange
, 0, 0, 0, &tp
);
891 /* Lock both files against IO */
892 if (src
->i_ino
== dest
->i_ino
)
893 xfs_ilock(src
, XFS_ILOCK_EXCL
);
895 xfs_lock_two_inodes(src
, dest
, XFS_ILOCK_EXCL
);
897 if (!xfs_is_reflink_inode(src
)) {
898 trace_xfs_reflink_set_inode_flag(src
);
899 xfs_trans_ijoin(tp
, src
, XFS_ILOCK_EXCL
);
900 src
->i_d
.di_flags2
|= XFS_DIFLAG2_REFLINK
;
901 xfs_trans_log_inode(tp
, src
, XFS_ILOG_CORE
);
902 xfs_ifork_init_cow(src
);
904 xfs_iunlock(src
, XFS_ILOCK_EXCL
);
906 if (src
->i_ino
== dest
->i_ino
)
909 if (!xfs_is_reflink_inode(dest
)) {
910 trace_xfs_reflink_set_inode_flag(dest
);
911 xfs_trans_ijoin(tp
, dest
, XFS_ILOCK_EXCL
);
912 dest
->i_d
.di_flags2
|= XFS_DIFLAG2_REFLINK
;
913 xfs_trans_log_inode(tp
, dest
, XFS_ILOG_CORE
);
914 xfs_ifork_init_cow(dest
);
916 xfs_iunlock(dest
, XFS_ILOCK_EXCL
);
919 error
= xfs_trans_commit(tp
);
925 trace_xfs_reflink_set_inode_flag_error(dest
, error
, _RET_IP_
);
930 * Update destination inode size, if necessary.
933 xfs_reflink_update_dest(
934 struct xfs_inode
*dest
,
937 struct xfs_mount
*mp
= dest
->i_mount
;
938 struct xfs_trans
*tp
;
941 if (newlen
<= i_size_read(VFS_I(dest
)))
944 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_ichange
, 0, 0, 0, &tp
);
948 xfs_ilock(dest
, XFS_ILOCK_EXCL
);
949 xfs_trans_ijoin(tp
, dest
, XFS_ILOCK_EXCL
);
951 trace_xfs_reflink_update_inode_size(dest
, newlen
);
952 i_size_write(VFS_I(dest
), newlen
);
953 dest
->i_d
.di_size
= newlen
;
954 xfs_trans_log_inode(tp
, dest
, XFS_ILOG_CORE
);
956 error
= xfs_trans_commit(tp
);
962 trace_xfs_reflink_update_inode_size_error(dest
, error
, _RET_IP_
);
967 * Unmap a range of blocks from a file, then map other blocks into the hole.
968 * The range to unmap is (destoff : destoff + srcioff + irec->br_blockcount).
969 * The extent irec is mapped into dest at irec->br_startoff.
972 xfs_reflink_remap_extent(
973 struct xfs_inode
*ip
,
974 struct xfs_bmbt_irec
*irec
,
975 xfs_fileoff_t destoff
,
978 struct xfs_mount
*mp
= ip
->i_mount
;
979 struct xfs_trans
*tp
;
980 xfs_fsblock_t firstfsb
;
981 unsigned int resblks
;
982 struct xfs_defer_ops dfops
;
983 struct xfs_bmbt_irec uirec
;
986 xfs_filblks_t unmap_len
;
990 unmap_len
= irec
->br_startoff
+ irec
->br_blockcount
- destoff
;
991 trace_xfs_reflink_punch_range(ip
, destoff
, unmap_len
);
993 /* Only remap normal extents. */
994 real_extent
= (irec
->br_startblock
!= HOLESTARTBLOCK
&&
995 irec
->br_startblock
!= DELAYSTARTBLOCK
&&
998 /* Start a rolling transaction to switch the mappings */
999 resblks
= XFS_EXTENTADD_SPACE_RES(ip
->i_mount
, XFS_DATA_FORK
);
1000 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
, resblks
, 0, 0, &tp
);
1004 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1005 xfs_trans_ijoin(tp
, ip
, 0);
1007 /* If we're not just clearing space, then do we have enough quota? */
1009 error
= xfs_trans_reserve_quota_nblks(tp
, ip
,
1010 irec
->br_blockcount
, 0, XFS_QMOPT_RES_REGBLKS
);
1015 trace_xfs_reflink_remap(ip
, irec
->br_startoff
,
1016 irec
->br_blockcount
, irec
->br_startblock
);
1018 /* Unmap the old blocks in the data fork. */
1021 xfs_defer_init(&dfops
, &firstfsb
);
1022 error
= __xfs_bunmapi(tp
, ip
, destoff
, &rlen
, 0, 1,
1028 * Trim the extent to whatever got unmapped.
1029 * Remember, bunmapi works backwards.
1031 uirec
.br_startblock
= irec
->br_startblock
+ rlen
;
1032 uirec
.br_startoff
= irec
->br_startoff
+ rlen
;
1033 uirec
.br_blockcount
= unmap_len
- rlen
;
1036 /* If this isn't a real mapping, we're done. */
1037 if (!real_extent
|| uirec
.br_blockcount
== 0)
1040 trace_xfs_reflink_remap(ip
, uirec
.br_startoff
,
1041 uirec
.br_blockcount
, uirec
.br_startblock
);
1043 /* Update the refcount tree */
1044 error
= xfs_refcount_increase_extent(mp
, &dfops
, &uirec
);
1048 /* Map the new blocks into the data fork. */
1049 error
= xfs_bmap_map_extent(mp
, &dfops
, ip
, &uirec
);
1053 /* Update quota accounting. */
1054 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
,
1055 uirec
.br_blockcount
);
1057 /* Update dest isize if needed. */
1058 newlen
= XFS_FSB_TO_B(mp
,
1059 uirec
.br_startoff
+ uirec
.br_blockcount
);
1060 newlen
= min_t(xfs_off_t
, newlen
, new_isize
);
1061 if (newlen
> i_size_read(VFS_I(ip
))) {
1062 trace_xfs_reflink_update_inode_size(ip
, newlen
);
1063 i_size_write(VFS_I(ip
), newlen
);
1064 ip
->i_d
.di_size
= newlen
;
1065 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1069 /* Process all the deferred stuff. */
1070 error
= xfs_defer_finish(&tp
, &dfops
, ip
);
1075 error
= xfs_trans_commit(tp
);
1076 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1082 xfs_defer_cancel(&dfops
);
1084 xfs_trans_cancel(tp
);
1085 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1087 trace_xfs_reflink_remap_extent_error(ip
, error
, _RET_IP_
);
1092 * Iteratively remap one file's extents (and holes) to another's.
1095 xfs_reflink_remap_blocks(
1096 struct xfs_inode
*src
,
1097 xfs_fileoff_t srcoff
,
1098 struct xfs_inode
*dest
,
1099 xfs_fileoff_t destoff
,
1101 xfs_off_t new_isize
)
1103 struct xfs_bmbt_irec imap
;
1106 xfs_filblks_t range_len
;
1108 /* drange = (destoff, destoff + len); srange = (srcoff, srcoff + len) */
1110 trace_xfs_reflink_remap_blocks_loop(src
, srcoff
, len
,
1112 /* Read extent from the source file */
1114 xfs_ilock(src
, XFS_ILOCK_EXCL
);
1115 error
= xfs_bmapi_read(src
, srcoff
, len
, &imap
, &nimaps
, 0);
1116 xfs_iunlock(src
, XFS_ILOCK_EXCL
);
1119 ASSERT(nimaps
== 1);
1121 trace_xfs_reflink_remap_imap(src
, srcoff
, len
, XFS_IO_OVERWRITE
,
1124 /* Translate imap into the destination file. */
1125 range_len
= imap
.br_startoff
+ imap
.br_blockcount
- srcoff
;
1126 imap
.br_startoff
+= destoff
- srcoff
;
1128 /* Clear dest from destoff to the end of imap and map it in. */
1129 error
= xfs_reflink_remap_extent(dest
, &imap
, destoff
,
1134 if (fatal_signal_pending(current
)) {
1139 /* Advance drange/srange */
1140 srcoff
+= range_len
;
1141 destoff
+= range_len
;
1148 trace_xfs_reflink_remap_blocks_error(dest
, error
, _RET_IP_
);
1153 * Link a range of blocks from one file to another.
1156 xfs_reflink_remap_range(
1157 struct xfs_inode
*src
,
1159 struct xfs_inode
*dest
,
1163 struct xfs_mount
*mp
= src
->i_mount
;
1164 xfs_fileoff_t sfsbno
, dfsbno
;
1165 xfs_filblks_t fsblen
;
1168 if (!xfs_sb_version_hasreflink(&mp
->m_sb
))
1171 if (XFS_FORCED_SHUTDOWN(mp
))
1174 /* Don't reflink realtime inodes */
1175 if (XFS_IS_REALTIME_INODE(src
) || XFS_IS_REALTIME_INODE(dest
))
1178 trace_xfs_reflink_remap_range(src
, srcoff
, len
, dest
, destoff
);
1180 /* Lock both files against IO */
1181 if (src
->i_ino
== dest
->i_ino
) {
1182 xfs_ilock(src
, XFS_IOLOCK_EXCL
);
1183 xfs_ilock(src
, XFS_MMAPLOCK_EXCL
);
1185 xfs_lock_two_inodes(src
, dest
, XFS_IOLOCK_EXCL
);
1186 xfs_lock_two_inodes(src
, dest
, XFS_MMAPLOCK_EXCL
);
1189 error
= xfs_reflink_set_inode_flag(src
, dest
);
1194 * Invalidate the page cache so that we can clear any CoW mappings
1195 * in the destination file.
1197 truncate_inode_pages_range(&VFS_I(dest
)->i_data
, destoff
,
1198 PAGE_ALIGN(destoff
+ len
) - 1);
1200 dfsbno
= XFS_B_TO_FSBT(mp
, destoff
);
1201 sfsbno
= XFS_B_TO_FSBT(mp
, srcoff
);
1202 fsblen
= XFS_B_TO_FSB(mp
, len
);
1203 error
= xfs_reflink_remap_blocks(src
, sfsbno
, dest
, dfsbno
, fsblen
,
1208 error
= xfs_reflink_update_dest(dest
, destoff
+ len
);
1213 xfs_iunlock(src
, XFS_MMAPLOCK_EXCL
);
1214 xfs_iunlock(src
, XFS_IOLOCK_EXCL
);
1215 if (src
->i_ino
!= dest
->i_ino
) {
1216 xfs_iunlock(dest
, XFS_MMAPLOCK_EXCL
);
1217 xfs_iunlock(dest
, XFS_IOLOCK_EXCL
);
1220 trace_xfs_reflink_remap_range_error(dest
, error
, _RET_IP_
);