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"
58 #include "xfs_ag_resv.h"
61 * Copy on Write of Shared Blocks
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.
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.
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.
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
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.
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
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.
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
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.
129 xfs_reflink_find_shared(
130 struct xfs_mount
*mp
,
136 bool find_end_of_shared
)
138 struct xfs_buf
*agbp
;
139 struct xfs_btree_cur
*cur
;
142 error
= xfs_alloc_read_agf(mp
, NULL
, agno
, 0, &agbp
);
146 cur
= xfs_refcountbt_init_cursor(mp
, NULL
, agbp
, agno
, NULL
);
148 error
= xfs_refcount_find_shared(cur
, agbno
, aglen
, fbno
, flen
,
151 xfs_btree_del_cursor(cur
, error
? XFS_BTREE_ERROR
: XFS_BTREE_NOERROR
);
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.
168 xfs_reflink_trim_around_shared(
169 struct xfs_inode
*ip
,
170 struct xfs_bmbt_irec
*irec
,
181 /* Holes, unwritten, and delalloc extents cannot be shared */
182 if (!xfs_is_reflink_inode(ip
) ||
184 irec
->br_startblock
== HOLESTARTBLOCK
||
185 irec
->br_startblock
== DELAYSTARTBLOCK
||
186 isnullstartblock(irec
->br_startblock
)) {
191 trace_xfs_reflink_trim_around_shared(ip
, irec
);
193 agno
= XFS_FSB_TO_AGNO(ip
->i_mount
, irec
->br_startblock
);
194 agbno
= XFS_FSB_TO_AGBNO(ip
->i_mount
, irec
->br_startblock
);
195 aglen
= irec
->br_blockcount
;
197 error
= xfs_reflink_find_shared(ip
->i_mount
, agno
, agbno
,
198 aglen
, &fbno
, &flen
, true);
202 *shared
= *trimmed
= false;
203 if (fbno
== NULLAGBLOCK
) {
204 /* No shared blocks at all. */
206 } else if (fbno
== agbno
) {
208 * The start of this extent is shared. Truncate the
209 * mapping at the end of the shared region so that a
210 * subsequent iteration starts at the start of the
213 irec
->br_blockcount
= flen
;
220 * There's a shared extent midway through this extent.
221 * Truncate the mapping at the start of the shared
222 * extent so that a subsequent iteration starts at the
223 * start of the shared region.
225 irec
->br_blockcount
= fbno
- agbno
;
232 * Trim the passed in imap to the next shared/unshared extent boundary, and
233 * if imap->br_startoff points to a shared extent reserve space for it in the
234 * COW fork. In this case *shared is set to true, else to false.
236 * Note that imap will always contain the block numbers for the existing blocks
237 * in the data fork, as the upper layers need them for read-modify-write
241 xfs_reflink_reserve_cow(
242 struct xfs_inode
*ip
,
243 struct xfs_bmbt_irec
*imap
,
246 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_COW_FORK
);
247 struct xfs_bmbt_irec got
;
249 bool eof
= false, trimmed
;
253 * Search the COW fork extent list first. This serves two purposes:
254 * first this implement the speculative preallocation using cowextisze,
255 * so that we also unshared block adjacent to shared blocks instead
256 * of just the shared blocks themselves. Second the lookup in the
257 * extent list is generally faster than going out to the shared extent
261 if (!xfs_iext_lookup_extent(ip
, ifp
, imap
->br_startoff
, &idx
, &got
))
263 if (!eof
&& got
.br_startoff
<= imap
->br_startoff
) {
264 trace_xfs_reflink_cow_found(ip
, imap
);
265 xfs_trim_extent(imap
, got
.br_startoff
, got
.br_blockcount
);
271 /* Trim the mapping to the nearest shared extent boundary. */
272 error
= xfs_reflink_trim_around_shared(ip
, imap
, shared
, &trimmed
);
276 /* Not shared? Just report the (potentially capped) extent. */
281 * Fork all the shared blocks from our write offset until the end of
284 error
= xfs_qm_dqattach_locked(ip
, 0);
288 error
= xfs_bmapi_reserve_delalloc(ip
, XFS_COW_FORK
, imap
->br_startoff
,
289 imap
->br_blockcount
, 0, &got
, &idx
, eof
);
290 if (error
== -ENOSPC
|| error
== -EDQUOT
)
291 trace_xfs_reflink_cow_enospc(ip
, imap
);
295 trace_xfs_reflink_cow_alloc(ip
, &got
);
299 /* Allocate all CoW reservations covering a range of blocks in a file. */
301 __xfs_reflink_allocate_cow(
302 struct xfs_inode
*ip
,
303 xfs_fileoff_t
*offset_fsb
,
304 xfs_fileoff_t end_fsb
)
306 struct xfs_mount
*mp
= ip
->i_mount
;
307 struct xfs_bmbt_irec imap
;
308 struct xfs_defer_ops dfops
;
309 struct xfs_trans
*tp
;
310 xfs_fsblock_t first_block
;
311 int nimaps
= 1, error
;
314 xfs_defer_init(&dfops
, &first_block
);
316 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
, 0, 0,
317 XFS_TRANS_RESERVE
, &tp
);
321 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
323 /* Read extent from the source file. */
325 error
= xfs_bmapi_read(ip
, *offset_fsb
, end_fsb
- *offset_fsb
,
331 error
= xfs_reflink_reserve_cow(ip
, &imap
, &shared
);
333 goto out_trans_cancel
;
336 *offset_fsb
= imap
.br_startoff
+ imap
.br_blockcount
;
337 goto out_trans_cancel
;
340 xfs_trans_ijoin(tp
, ip
, 0);
341 error
= xfs_bmapi_write(tp
, ip
, imap
.br_startoff
, imap
.br_blockcount
,
342 XFS_BMAPI_COWFORK
, &first_block
,
343 XFS_EXTENTADD_SPACE_RES(mp
, XFS_DATA_FORK
),
344 &imap
, &nimaps
, &dfops
);
346 goto out_trans_cancel
;
348 error
= xfs_defer_finish(&tp
, &dfops
, NULL
);
350 goto out_trans_cancel
;
352 error
= xfs_trans_commit(tp
);
354 *offset_fsb
= imap
.br_startoff
+ imap
.br_blockcount
;
356 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
359 xfs_defer_cancel(&dfops
);
360 xfs_trans_cancel(tp
);
364 /* Allocate all CoW reservations covering a part of a file. */
366 xfs_reflink_allocate_cow_range(
367 struct xfs_inode
*ip
,
371 struct xfs_mount
*mp
= ip
->i_mount
;
372 xfs_fileoff_t offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
373 xfs_fileoff_t end_fsb
= XFS_B_TO_FSB(mp
, offset
+ count
);
376 ASSERT(xfs_is_reflink_inode(ip
));
378 trace_xfs_reflink_allocate_cow_range(ip
, offset
, count
);
381 * Make sure that the dquots are there.
383 error
= xfs_qm_dqattach(ip
, 0);
387 while (offset_fsb
< end_fsb
) {
388 error
= __xfs_reflink_allocate_cow(ip
, &offset_fsb
, end_fsb
);
390 trace_xfs_reflink_allocate_cow_range_error(ip
, error
,
400 * Find the CoW reservation for a given byte offset of a file.
403 xfs_reflink_find_cow_mapping(
404 struct xfs_inode
*ip
,
406 struct xfs_bmbt_irec
*imap
)
408 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_COW_FORK
);
409 xfs_fileoff_t offset_fsb
;
410 struct xfs_bmbt_irec got
;
413 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
| XFS_ILOCK_SHARED
));
414 ASSERT(xfs_is_reflink_inode(ip
));
416 offset_fsb
= XFS_B_TO_FSBT(ip
->i_mount
, offset
);
417 if (!xfs_iext_lookup_extent(ip
, ifp
, offset_fsb
, &idx
, &got
))
419 if (got
.br_startoff
> offset_fsb
)
422 trace_xfs_reflink_find_cow_mapping(ip
, offset
, 1, XFS_IO_OVERWRITE
,
429 * Trim an extent to end at the next CoW reservation past offset_fsb.
432 xfs_reflink_trim_irec_to_next_cow(
433 struct xfs_inode
*ip
,
434 xfs_fileoff_t offset_fsb
,
435 struct xfs_bmbt_irec
*imap
)
437 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_COW_FORK
);
438 struct xfs_bmbt_irec got
;
441 if (!xfs_is_reflink_inode(ip
))
444 /* Find the extent in the CoW fork. */
445 if (!xfs_iext_lookup_extent(ip
, ifp
, offset_fsb
, &idx
, &got
))
448 /* This is the extent before; try sliding up one. */
449 if (got
.br_startoff
< offset_fsb
) {
450 if (!xfs_iext_get_extent(ifp
, idx
+ 1, &got
))
454 if (got
.br_startoff
>= imap
->br_startoff
+ imap
->br_blockcount
)
457 imap
->br_blockcount
= got
.br_startoff
- imap
->br_startoff
;
458 trace_xfs_reflink_trim_irec(ip
, imap
);
462 * Cancel all pending CoW reservations for some block range of an inode.
465 xfs_reflink_cancel_cow_blocks(
466 struct xfs_inode
*ip
,
467 struct xfs_trans
**tpp
,
468 xfs_fileoff_t offset_fsb
,
469 xfs_fileoff_t end_fsb
)
471 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_COW_FORK
);
472 struct xfs_bmbt_irec got
, del
;
474 xfs_fsblock_t firstfsb
;
475 struct xfs_defer_ops dfops
;
478 if (!xfs_is_reflink_inode(ip
))
480 if (!xfs_iext_lookup_extent(ip
, ifp
, offset_fsb
, &idx
, &got
))
483 while (got
.br_startoff
< end_fsb
) {
485 xfs_trim_extent(&del
, offset_fsb
, end_fsb
- offset_fsb
);
486 trace_xfs_reflink_cancel_cow(ip
, &del
);
488 if (isnullstartblock(del
.br_startblock
)) {
489 error
= xfs_bmap_del_extent_delay(ip
, XFS_COW_FORK
,
494 xfs_trans_ijoin(*tpp
, ip
, 0);
495 xfs_defer_init(&dfops
, &firstfsb
);
497 /* Free the CoW orphan record. */
498 error
= xfs_refcount_free_cow_extent(ip
->i_mount
,
499 &dfops
, del
.br_startblock
,
504 xfs_bmap_add_free(ip
->i_mount
, &dfops
,
505 del
.br_startblock
, del
.br_blockcount
,
508 /* Update quota accounting */
509 xfs_trans_mod_dquot_byino(*tpp
, ip
, XFS_TRANS_DQ_BCOUNT
,
510 -(long)del
.br_blockcount
);
512 /* Roll the transaction */
513 error
= xfs_defer_finish(tpp
, &dfops
, ip
);
515 xfs_defer_cancel(&dfops
);
519 /* Remove the mapping from the CoW fork. */
520 xfs_bmap_del_extent_cow(ip
, &idx
, &got
, &del
);
523 if (!xfs_iext_get_extent(ifp
, ++idx
, &got
))
527 /* clear tag if cow fork is emptied */
529 xfs_inode_clear_cowblocks_tag(ip
);
535 * Cancel all pending CoW reservations for some byte range of an inode.
538 xfs_reflink_cancel_cow_range(
539 struct xfs_inode
*ip
,
543 struct xfs_trans
*tp
;
544 xfs_fileoff_t offset_fsb
;
545 xfs_fileoff_t end_fsb
;
548 trace_xfs_reflink_cancel_cow_range(ip
, offset
, count
);
549 ASSERT(xfs_is_reflink_inode(ip
));
551 offset_fsb
= XFS_B_TO_FSBT(ip
->i_mount
, offset
);
552 if (count
== NULLFILEOFF
)
553 end_fsb
= NULLFILEOFF
;
555 end_fsb
= XFS_B_TO_FSB(ip
->i_mount
, offset
+ count
);
557 /* Start a rolling transaction to remove the mappings */
558 error
= xfs_trans_alloc(ip
->i_mount
, &M_RES(ip
->i_mount
)->tr_write
,
563 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
564 xfs_trans_ijoin(tp
, ip
, 0);
566 /* Scrape out the old CoW reservations */
567 error
= xfs_reflink_cancel_cow_blocks(ip
, &tp
, offset_fsb
, end_fsb
);
571 error
= xfs_trans_commit(tp
);
573 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
577 xfs_trans_cancel(tp
);
578 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
580 trace_xfs_reflink_cancel_cow_range_error(ip
, error
, _RET_IP_
);
585 * Remap parts of a file's data fork after a successful CoW.
589 struct xfs_inode
*ip
,
593 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_COW_FORK
);
594 struct xfs_bmbt_irec got
, del
;
595 struct xfs_trans
*tp
;
596 xfs_fileoff_t offset_fsb
;
597 xfs_fileoff_t end_fsb
;
598 xfs_fsblock_t firstfsb
;
599 struct xfs_defer_ops dfops
;
601 unsigned int resblks
;
605 trace_xfs_reflink_end_cow(ip
, offset
, count
);
607 /* No COW extents? That's easy! */
608 if (ifp
->if_bytes
== 0)
611 offset_fsb
= XFS_B_TO_FSBT(ip
->i_mount
, offset
);
612 end_fsb
= XFS_B_TO_FSB(ip
->i_mount
, offset
+ count
);
614 /* Start a rolling transaction to switch the mappings */
615 resblks
= XFS_EXTENTADD_SPACE_RES(ip
->i_mount
, XFS_DATA_FORK
);
616 error
= xfs_trans_alloc(ip
->i_mount
, &M_RES(ip
->i_mount
)->tr_write
,
621 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
622 xfs_trans_ijoin(tp
, ip
, 0);
624 /* If there is a hole at end_fsb - 1 go to the previous extent */
625 if (!xfs_iext_lookup_extent(ip
, ifp
, end_fsb
- 1, &idx
, &got
) ||
626 got
.br_startoff
> end_fsb
) {
628 xfs_iext_get_extent(ifp
, --idx
, &got
);
631 /* Walk backwards until we're out of the I/O range... */
632 while (got
.br_startoff
+ got
.br_blockcount
> offset_fsb
) {
634 xfs_trim_extent(&del
, offset_fsb
, end_fsb
- offset_fsb
);
636 /* Extent delete may have bumped idx forward */
637 if (!del
.br_blockcount
) {
642 ASSERT(!isnullstartblock(got
.br_startblock
));
644 /* Unmap the old blocks in the data fork. */
645 xfs_defer_init(&dfops
, &firstfsb
);
646 rlen
= del
.br_blockcount
;
647 error
= __xfs_bunmapi(tp
, ip
, del
.br_startoff
, &rlen
, 0, 1,
652 /* Trim the extent to whatever got unmapped. */
654 xfs_trim_extent(&del
, del
.br_startoff
+ rlen
,
655 del
.br_blockcount
- rlen
);
657 trace_xfs_reflink_cow_remap(ip
, &del
);
659 /* Free the CoW orphan record. */
660 error
= xfs_refcount_free_cow_extent(tp
->t_mountp
, &dfops
,
661 del
.br_startblock
, del
.br_blockcount
);
665 /* Map the new blocks into the data fork. */
666 error
= xfs_bmap_map_extent(tp
->t_mountp
, &dfops
, ip
, &del
);
670 /* Remove the mapping from the CoW fork. */
671 xfs_bmap_del_extent_cow(ip
, &idx
, &got
, &del
);
673 error
= xfs_defer_finish(&tp
, &dfops
, ip
);
677 if (!xfs_iext_get_extent(ifp
, idx
, &got
))
681 error
= xfs_trans_commit(tp
);
682 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
688 xfs_defer_cancel(&dfops
);
689 xfs_trans_cancel(tp
);
690 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
692 trace_xfs_reflink_end_cow_error(ip
, error
, _RET_IP_
);
697 * Free leftover CoW reservations that didn't get cleaned out.
700 xfs_reflink_recover_cow(
701 struct xfs_mount
*mp
)
706 if (!xfs_sb_version_hasreflink(&mp
->m_sb
))
709 for (agno
= 0; agno
< mp
->m_sb
.sb_agcount
; agno
++) {
710 error
= xfs_refcount_recover_cow_leftovers(mp
, agno
);
719 * Reflinking (Block) Ranges of Two Files Together
721 * First, ensure that the reflink flag is set on both inodes. The flag is an
722 * optimization to avoid unnecessary refcount btree lookups in the write path.
724 * Now we can iteratively remap the range of extents (and holes) in src to the
725 * corresponding ranges in dest. Let drange and srange denote the ranges of
726 * logical blocks in dest and src touched by the reflink operation.
728 * While the length of drange is greater than zero,
729 * - Read src's bmbt at the start of srange ("imap")
730 * - If imap doesn't exist, make imap appear to start at the end of srange
732 * - If imap starts before srange, advance imap to start at srange.
733 * - If imap goes beyond srange, truncate imap to end at the end of srange.
734 * - Punch (imap start - srange start + imap len) blocks from dest at
735 * offset (drange start).
736 * - If imap points to a real range of pblks,
737 * > Increase the refcount of the imap's pblks
738 * > Map imap's pblks into dest at the offset
739 * (drange start + imap start - srange start)
740 * - Advance drange and srange by (imap start - srange start + imap len)
742 * Finally, if the reflink made dest longer, update both the in-core and
743 * on-disk file sizes.
745 * ASCII Art Demonstration:
747 * Let's say we want to reflink this source file:
749 * ----SSSSSSS-SSSSS----SSSSSS (src file)
750 * <-------------------->
752 * into this destination file:
754 * --DDDDDDDDDDDDDDDDDDD--DDD (dest file)
755 * <-------------------->
756 * '-' means a hole, and 'S' and 'D' are written blocks in the src and dest.
757 * Observe that the range has different logical offsets in either file.
759 * Consider that the first extent in the source file doesn't line up with our
760 * reflink range. Unmapping and remapping are separate operations, so we can
761 * unmap more blocks from the destination file than we remap.
763 * ----SSSSSSS-SSSSS----SSSSSS
765 * --DDDDD---------DDDDD--DDD
768 * Now remap the source extent into the destination file:
770 * ----SSSSSSS-SSSSS----SSSSSS
772 * --DDDDD--SSSSSSSDDDDD--DDD
775 * Do likewise with the second hole and extent in our range. Holes in the
776 * unmap range don't affect our operation.
778 * ----SSSSSSS-SSSSS----SSSSSS
780 * --DDDDD--SSSSSSS-SSSSS-DDD
783 * Finally, unmap and remap part of the third extent. This will increase the
784 * size of the destination file.
786 * ----SSSSSSS-SSSSS----SSSSSS
788 * --DDDDD--SSSSSSS-SSSSS----SSS
791 * Once we update the destination file's i_size, we're done.
795 * Ensure the reflink bit is set in both inodes.
798 xfs_reflink_set_inode_flag(
799 struct xfs_inode
*src
,
800 struct xfs_inode
*dest
)
802 struct xfs_mount
*mp
= src
->i_mount
;
804 struct xfs_trans
*tp
;
806 if (xfs_is_reflink_inode(src
) && xfs_is_reflink_inode(dest
))
809 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_ichange
, 0, 0, 0, &tp
);
813 /* Lock both files against IO */
814 if (src
->i_ino
== dest
->i_ino
)
815 xfs_ilock(src
, XFS_ILOCK_EXCL
);
817 xfs_lock_two_inodes(src
, dest
, XFS_ILOCK_EXCL
);
819 if (!xfs_is_reflink_inode(src
)) {
820 trace_xfs_reflink_set_inode_flag(src
);
821 xfs_trans_ijoin(tp
, src
, XFS_ILOCK_EXCL
);
822 src
->i_d
.di_flags2
|= XFS_DIFLAG2_REFLINK
;
823 xfs_trans_log_inode(tp
, src
, XFS_ILOG_CORE
);
824 xfs_ifork_init_cow(src
);
826 xfs_iunlock(src
, XFS_ILOCK_EXCL
);
828 if (src
->i_ino
== dest
->i_ino
)
831 if (!xfs_is_reflink_inode(dest
)) {
832 trace_xfs_reflink_set_inode_flag(dest
);
833 xfs_trans_ijoin(tp
, dest
, XFS_ILOCK_EXCL
);
834 dest
->i_d
.di_flags2
|= XFS_DIFLAG2_REFLINK
;
835 xfs_trans_log_inode(tp
, dest
, XFS_ILOG_CORE
);
836 xfs_ifork_init_cow(dest
);
838 xfs_iunlock(dest
, XFS_ILOCK_EXCL
);
841 error
= xfs_trans_commit(tp
);
847 trace_xfs_reflink_set_inode_flag_error(dest
, error
, _RET_IP_
);
852 * Update destination inode size & cowextsize hint, if necessary.
855 xfs_reflink_update_dest(
856 struct xfs_inode
*dest
,
858 xfs_extlen_t cowextsize
)
860 struct xfs_mount
*mp
= dest
->i_mount
;
861 struct xfs_trans
*tp
;
864 if (newlen
<= i_size_read(VFS_I(dest
)) && cowextsize
== 0)
867 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_ichange
, 0, 0, 0, &tp
);
871 xfs_ilock(dest
, XFS_ILOCK_EXCL
);
872 xfs_trans_ijoin(tp
, dest
, XFS_ILOCK_EXCL
);
874 if (newlen
> i_size_read(VFS_I(dest
))) {
875 trace_xfs_reflink_update_inode_size(dest
, newlen
);
876 i_size_write(VFS_I(dest
), newlen
);
877 dest
->i_d
.di_size
= newlen
;
881 dest
->i_d
.di_cowextsize
= cowextsize
;
882 dest
->i_d
.di_flags2
|= XFS_DIFLAG2_COWEXTSIZE
;
885 xfs_trans_log_inode(tp
, dest
, XFS_ILOG_CORE
);
887 error
= xfs_trans_commit(tp
);
893 trace_xfs_reflink_update_inode_size_error(dest
, error
, _RET_IP_
);
898 * Do we have enough reserve in this AG to handle a reflink? The refcount
899 * btree already reserved all the space it needs, but the rmap btree can grow
900 * infinitely, so we won't allow more reflinks when the AG is down to the
904 xfs_reflink_ag_has_free_space(
905 struct xfs_mount
*mp
,
908 struct xfs_perag
*pag
;
911 if (!xfs_sb_version_hasrmapbt(&mp
->m_sb
))
914 pag
= xfs_perag_get(mp
, agno
);
915 if (xfs_ag_resv_critical(pag
, XFS_AG_RESV_AGFL
) ||
916 xfs_ag_resv_critical(pag
, XFS_AG_RESV_METADATA
))
923 * Unmap a range of blocks from a file, then map other blocks into the hole.
924 * The range to unmap is (destoff : destoff + srcioff + irec->br_blockcount).
925 * The extent irec is mapped into dest at irec->br_startoff.
928 xfs_reflink_remap_extent(
929 struct xfs_inode
*ip
,
930 struct xfs_bmbt_irec
*irec
,
931 xfs_fileoff_t destoff
,
934 struct xfs_mount
*mp
= ip
->i_mount
;
935 struct xfs_trans
*tp
;
936 xfs_fsblock_t firstfsb
;
937 unsigned int resblks
;
938 struct xfs_defer_ops dfops
;
939 struct xfs_bmbt_irec uirec
;
942 xfs_filblks_t unmap_len
;
946 unmap_len
= irec
->br_startoff
+ irec
->br_blockcount
- destoff
;
947 trace_xfs_reflink_punch_range(ip
, destoff
, unmap_len
);
949 /* Only remap normal extents. */
950 real_extent
= (irec
->br_startblock
!= HOLESTARTBLOCK
&&
951 irec
->br_startblock
!= DELAYSTARTBLOCK
&&
954 /* No reflinking if we're low on space */
956 error
= xfs_reflink_ag_has_free_space(mp
,
957 XFS_FSB_TO_AGNO(mp
, irec
->br_startblock
));
962 /* Start a rolling transaction to switch the mappings */
963 resblks
= XFS_EXTENTADD_SPACE_RES(ip
->i_mount
, XFS_DATA_FORK
);
964 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
, resblks
, 0, 0, &tp
);
968 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
969 xfs_trans_ijoin(tp
, ip
, 0);
971 /* If we're not just clearing space, then do we have enough quota? */
973 error
= xfs_trans_reserve_quota_nblks(tp
, ip
,
974 irec
->br_blockcount
, 0, XFS_QMOPT_RES_REGBLKS
);
979 trace_xfs_reflink_remap(ip
, irec
->br_startoff
,
980 irec
->br_blockcount
, irec
->br_startblock
);
982 /* Unmap the old blocks in the data fork. */
985 xfs_defer_init(&dfops
, &firstfsb
);
986 error
= __xfs_bunmapi(tp
, ip
, destoff
, &rlen
, 0, 1,
992 * Trim the extent to whatever got unmapped.
993 * Remember, bunmapi works backwards.
995 uirec
.br_startblock
= irec
->br_startblock
+ rlen
;
996 uirec
.br_startoff
= irec
->br_startoff
+ rlen
;
997 uirec
.br_blockcount
= unmap_len
- rlen
;
1000 /* If this isn't a real mapping, we're done. */
1001 if (!real_extent
|| uirec
.br_blockcount
== 0)
1004 trace_xfs_reflink_remap(ip
, uirec
.br_startoff
,
1005 uirec
.br_blockcount
, uirec
.br_startblock
);
1007 /* Update the refcount tree */
1008 error
= xfs_refcount_increase_extent(mp
, &dfops
, &uirec
);
1012 /* Map the new blocks into the data fork. */
1013 error
= xfs_bmap_map_extent(mp
, &dfops
, ip
, &uirec
);
1017 /* Update quota accounting. */
1018 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_BCOUNT
,
1019 uirec
.br_blockcount
);
1021 /* Update dest isize if needed. */
1022 newlen
= XFS_FSB_TO_B(mp
,
1023 uirec
.br_startoff
+ uirec
.br_blockcount
);
1024 newlen
= min_t(xfs_off_t
, newlen
, new_isize
);
1025 if (newlen
> i_size_read(VFS_I(ip
))) {
1026 trace_xfs_reflink_update_inode_size(ip
, newlen
);
1027 i_size_write(VFS_I(ip
), newlen
);
1028 ip
->i_d
.di_size
= newlen
;
1029 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1033 /* Process all the deferred stuff. */
1034 error
= xfs_defer_finish(&tp
, &dfops
, ip
);
1039 error
= xfs_trans_commit(tp
);
1040 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1046 xfs_defer_cancel(&dfops
);
1048 xfs_trans_cancel(tp
);
1049 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1051 trace_xfs_reflink_remap_extent_error(ip
, error
, _RET_IP_
);
1056 * Iteratively remap one file's extents (and holes) to another's.
1059 xfs_reflink_remap_blocks(
1060 struct xfs_inode
*src
,
1061 xfs_fileoff_t srcoff
,
1062 struct xfs_inode
*dest
,
1063 xfs_fileoff_t destoff
,
1065 xfs_off_t new_isize
)
1067 struct xfs_bmbt_irec imap
;
1070 xfs_filblks_t range_len
;
1072 /* drange = (destoff, destoff + len); srange = (srcoff, srcoff + len) */
1074 trace_xfs_reflink_remap_blocks_loop(src
, srcoff
, len
,
1076 /* Read extent from the source file */
1078 xfs_ilock(src
, XFS_ILOCK_EXCL
);
1079 error
= xfs_bmapi_read(src
, srcoff
, len
, &imap
, &nimaps
, 0);
1080 xfs_iunlock(src
, XFS_ILOCK_EXCL
);
1083 ASSERT(nimaps
== 1);
1085 trace_xfs_reflink_remap_imap(src
, srcoff
, len
, XFS_IO_OVERWRITE
,
1088 /* Translate imap into the destination file. */
1089 range_len
= imap
.br_startoff
+ imap
.br_blockcount
- srcoff
;
1090 imap
.br_startoff
+= destoff
- srcoff
;
1092 /* Clear dest from destoff to the end of imap and map it in. */
1093 error
= xfs_reflink_remap_extent(dest
, &imap
, destoff
,
1098 if (fatal_signal_pending(current
)) {
1103 /* Advance drange/srange */
1104 srcoff
+= range_len
;
1105 destoff
+= range_len
;
1112 trace_xfs_reflink_remap_blocks_error(dest
, error
, _RET_IP_
);
1117 * Link a range of blocks from one file to another.
1120 xfs_reflink_remap_range(
1121 struct file
*file_in
,
1123 struct file
*file_out
,
1128 struct inode
*inode_in
= file_inode(file_in
);
1129 struct xfs_inode
*src
= XFS_I(inode_in
);
1130 struct inode
*inode_out
= file_inode(file_out
);
1131 struct xfs_inode
*dest
= XFS_I(inode_out
);
1132 struct xfs_mount
*mp
= src
->i_mount
;
1133 bool same_inode
= (inode_in
== inode_out
);
1134 xfs_fileoff_t sfsbno
, dfsbno
;
1135 xfs_filblks_t fsblen
;
1136 xfs_extlen_t cowextsize
;
1139 if (!xfs_sb_version_hasreflink(&mp
->m_sb
))
1142 if (XFS_FORCED_SHUTDOWN(mp
))
1145 /* Lock both files against IO */
1146 lock_two_nondirectories(inode_in
, inode_out
);
1148 xfs_ilock(src
, XFS_MMAPLOCK_EXCL
);
1150 xfs_lock_two_inodes(src
, dest
, XFS_MMAPLOCK_EXCL
);
1152 /* Check file eligibility and prepare for block sharing. */
1154 /* Don't reflink realtime inodes */
1155 if (XFS_IS_REALTIME_INODE(src
) || XFS_IS_REALTIME_INODE(dest
))
1158 /* Don't share DAX file data for now. */
1159 if (IS_DAX(inode_in
) || IS_DAX(inode_out
))
1162 ret
= vfs_clone_file_prep_inodes(inode_in
, pos_in
, inode_out
, pos_out
,
1167 trace_xfs_reflink_remap_range(src
, pos_in
, len
, dest
, pos_out
);
1169 /* Set flags and remap blocks. */
1170 ret
= xfs_reflink_set_inode_flag(src
, dest
);
1174 dfsbno
= XFS_B_TO_FSBT(mp
, pos_out
);
1175 sfsbno
= XFS_B_TO_FSBT(mp
, pos_in
);
1176 fsblen
= XFS_B_TO_FSB(mp
, len
);
1177 ret
= xfs_reflink_remap_blocks(src
, sfsbno
, dest
, dfsbno
, fsblen
,
1182 /* Zap any page cache for the destination file's range. */
1183 truncate_inode_pages_range(&inode_out
->i_data
, pos_out
,
1184 PAGE_ALIGN(pos_out
+ len
) - 1);
1187 * Carry the cowextsize hint from src to dest if we're sharing the
1188 * entire source file to the entire destination file, the source file
1189 * has a cowextsize hint, and the destination file does not.
1192 if (pos_in
== 0 && len
== i_size_read(inode_in
) &&
1193 (src
->i_d
.di_flags2
& XFS_DIFLAG2_COWEXTSIZE
) &&
1194 pos_out
== 0 && len
>= i_size_read(inode_out
) &&
1195 !(dest
->i_d
.di_flags2
& XFS_DIFLAG2_COWEXTSIZE
))
1196 cowextsize
= src
->i_d
.di_cowextsize
;
1198 ret
= xfs_reflink_update_dest(dest
, pos_out
+ len
, cowextsize
);
1201 xfs_iunlock(src
, XFS_MMAPLOCK_EXCL
);
1203 xfs_iunlock(dest
, XFS_MMAPLOCK_EXCL
);
1204 unlock_two_nondirectories(inode_in
, inode_out
);
1206 trace_xfs_reflink_remap_range_error(dest
, ret
, _RET_IP_
);
1211 * The user wants to preemptively CoW all shared blocks in this file,
1212 * which enables us to turn off the reflink flag. Iterate all
1213 * extents which are not prealloc/delalloc to see which ranges are
1214 * mentioned in the refcount tree, then read those blocks into the
1215 * pagecache, dirty them, fsync them back out, and then we can update
1216 * the inode flag. What happens if we run out of memory? :)
1219 xfs_reflink_dirty_extents(
1220 struct xfs_inode
*ip
,
1225 struct xfs_mount
*mp
= ip
->i_mount
;
1226 xfs_agnumber_t agno
;
1227 xfs_agblock_t agbno
;
1233 struct xfs_bmbt_irec map
[2];
1237 while (end
- fbno
> 0) {
1240 * Look for extents in the file. Skip holes, delalloc, or
1241 * unwritten extents; they can't be reflinked.
1243 error
= xfs_bmapi_read(ip
, fbno
, end
- fbno
, map
, &nmaps
, 0);
1248 if (map
[0].br_startblock
== HOLESTARTBLOCK
||
1249 map
[0].br_startblock
== DELAYSTARTBLOCK
||
1250 ISUNWRITTEN(&map
[0]))
1254 while (map
[1].br_blockcount
) {
1255 agno
= XFS_FSB_TO_AGNO(mp
, map
[1].br_startblock
);
1256 agbno
= XFS_FSB_TO_AGBNO(mp
, map
[1].br_startblock
);
1257 aglen
= map
[1].br_blockcount
;
1259 error
= xfs_reflink_find_shared(mp
, agno
, agbno
, aglen
,
1260 &rbno
, &rlen
, true);
1263 if (rbno
== NULLAGBLOCK
)
1266 /* Dirty the pages */
1267 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1268 fpos
= XFS_FSB_TO_B(mp
, map
[1].br_startoff
+
1270 flen
= XFS_FSB_TO_B(mp
, rlen
);
1271 if (fpos
+ flen
> isize
)
1272 flen
= isize
- fpos
;
1273 error
= iomap_file_dirty(VFS_I(ip
), fpos
, flen
,
1275 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1279 map
[1].br_blockcount
-= (rbno
- agbno
+ rlen
);
1280 map
[1].br_startoff
+= (rbno
- agbno
+ rlen
);
1281 map
[1].br_startblock
+= (rbno
- agbno
+ rlen
);
1285 fbno
= map
[0].br_startoff
+ map
[0].br_blockcount
;
1291 /* Clear the inode reflink flag if there are no shared extents. */
1293 xfs_reflink_clear_inode_flag(
1294 struct xfs_inode
*ip
,
1295 struct xfs_trans
**tpp
)
1297 struct xfs_mount
*mp
= ip
->i_mount
;
1300 xfs_agnumber_t agno
;
1301 xfs_agblock_t agbno
;
1305 struct xfs_bmbt_irec map
;
1309 ASSERT(xfs_is_reflink_inode(ip
));
1312 end
= XFS_B_TO_FSB(mp
, i_size_read(VFS_I(ip
)));
1313 while (end
- fbno
> 0) {
1316 * Look for extents in the file. Skip holes, delalloc, or
1317 * unwritten extents; they can't be reflinked.
1319 error
= xfs_bmapi_read(ip
, fbno
, end
- fbno
, &map
, &nmaps
, 0);
1324 if (map
.br_startblock
== HOLESTARTBLOCK
||
1325 map
.br_startblock
== DELAYSTARTBLOCK
||
1329 agno
= XFS_FSB_TO_AGNO(mp
, map
.br_startblock
);
1330 agbno
= XFS_FSB_TO_AGBNO(mp
, map
.br_startblock
);
1331 aglen
= map
.br_blockcount
;
1333 error
= xfs_reflink_find_shared(mp
, agno
, agbno
, aglen
,
1334 &rbno
, &rlen
, false);
1337 /* Is there still a shared block here? */
1338 if (rbno
!= NULLAGBLOCK
)
1341 fbno
= map
.br_startoff
+ map
.br_blockcount
;
1345 * We didn't find any shared blocks so turn off the reflink flag.
1346 * First, get rid of any leftover CoW mappings.
1348 error
= xfs_reflink_cancel_cow_blocks(ip
, tpp
, 0, NULLFILEOFF
);
1352 /* Clear the inode flag. */
1353 trace_xfs_reflink_unset_inode_flag(ip
);
1354 ip
->i_d
.di_flags2
&= ~XFS_DIFLAG2_REFLINK
;
1355 xfs_inode_clear_cowblocks_tag(ip
);
1356 xfs_trans_ijoin(*tpp
, ip
, 0);
1357 xfs_trans_log_inode(*tpp
, ip
, XFS_ILOG_CORE
);
1363 * Clear the inode reflink flag if there are no shared extents and the size
1367 xfs_reflink_try_clear_inode_flag(
1368 struct xfs_inode
*ip
)
1370 struct xfs_mount
*mp
= ip
->i_mount
;
1371 struct xfs_trans
*tp
;
1374 /* Start a rolling transaction to remove the mappings */
1375 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
, 0, 0, 0, &tp
);
1379 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1380 xfs_trans_ijoin(tp
, ip
, 0);
1382 error
= xfs_reflink_clear_inode_flag(ip
, &tp
);
1386 error
= xfs_trans_commit(tp
);
1390 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1393 xfs_trans_cancel(tp
);
1395 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1400 * Pre-COW all shared blocks within a given byte range of a file and turn off
1401 * the reflink flag if we unshare all of the file's blocks.
1404 xfs_reflink_unshare(
1405 struct xfs_inode
*ip
,
1409 struct xfs_mount
*mp
= ip
->i_mount
;
1415 if (!xfs_is_reflink_inode(ip
))
1418 trace_xfs_reflink_unshare(ip
, offset
, len
);
1420 inode_dio_wait(VFS_I(ip
));
1422 /* Try to CoW the selected ranges */
1423 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1424 fbno
= XFS_B_TO_FSBT(mp
, offset
);
1425 isize
= i_size_read(VFS_I(ip
));
1426 end
= XFS_B_TO_FSB(mp
, offset
+ len
);
1427 error
= xfs_reflink_dirty_extents(ip
, fbno
, end
, isize
);
1430 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1432 /* Wait for the IO to finish */
1433 error
= filemap_write_and_wait(VFS_I(ip
)->i_mapping
);
1437 /* Turn off the reflink flag if possible. */
1438 error
= xfs_reflink_try_clear_inode_flag(ip
);
1445 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1447 trace_xfs_reflink_unshare_error(ip
, error
, _RET_IP_
);