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_bmbt_irec got, prev;
247 xfs_fileoff_t end_fsb, orig_end_fsb;
248 int eof = 0, error = 0;
254 * Search the COW fork extent list first. This serves two purposes:
255 * first this implement the speculative preallocation using cowextisze,
256 * so that we also unshared block adjacent to shared blocks instead
257 * of just the shared blocks themselves. Second the lookup in the
258 * extent list is generally faster than going out to the shared extent
261 xfs_bmap_search_extents(ip, imap->br_startoff, XFS_COW_FORK, &eof, &idx,
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 end_fsb = orig_end_fsb = imap->br_startoff + imap->br_blockcount;
290 align = xfs_eof_alignment(ip, xfs_get_cowextsz_hint(ip));
292 end_fsb = roundup_64(end_fsb, align);
295 error = xfs_bmapi_reserve_delalloc(ip, XFS_COW_FORK, imap->br_startoff,
296 end_fsb - imap->br_startoff, &got, &prev, &idx, eof);
302 /* retry without any preallocation */
303 trace_xfs_reflink_cow_enospc(ip, imap);
304 if (end_fsb != orig_end_fsb) {
305 end_fsb = orig_end_fsb;
313 if (end_fsb != orig_end_fsb)
314 xfs_inode_set_cowblocks_tag(ip);
316 trace_xfs_reflink_cow_alloc(ip, &got);
320 /* Allocate all CoW reservations covering a range of blocks in a file. */
322 __xfs_reflink_allocate_cow(
323 struct xfs_inode *ip,
324 xfs_fileoff_t *offset_fsb,
325 xfs_fileoff_t end_fsb)
327 struct xfs_mount *mp = ip->i_mount;
328 struct xfs_bmbt_irec imap;
329 struct xfs_defer_ops dfops;
330 struct xfs_trans *tp;
331 xfs_fsblock_t first_block;
332 int nimaps = 1, error;
335 xfs_defer_init(&dfops, &first_block);
337 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
338 XFS_TRANS_RESERVE, &tp);
342 xfs_ilock(ip, XFS_ILOCK_EXCL);
344 /* Read extent from the source file. */
346 error = xfs_bmapi_read(ip, *offset_fsb, end_fsb - *offset_fsb,
352 error = xfs_reflink_reserve_cow(ip, &imap, &shared);
354 goto out_trans_cancel;
357 *offset_fsb = imap.br_startoff + imap.br_blockcount;
358 goto out_trans_cancel;
361 xfs_trans_ijoin(tp, ip, 0);
362 error = xfs_bmapi_write(tp, ip, imap.br_startoff, imap.br_blockcount,
363 XFS_BMAPI_COWFORK, &first_block,
364 XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK),
365 &imap, &nimaps, &dfops);
367 goto out_trans_cancel;
369 error = xfs_defer_finish(&tp, &dfops, NULL);
371 goto out_trans_cancel;
373 error = xfs_trans_commit(tp);
375 *offset_fsb = imap.br_startoff + imap.br_blockcount;
377 xfs_iunlock(ip, XFS_ILOCK_EXCL);
380 xfs_defer_cancel(&dfops);
381 xfs_trans_cancel(tp);
385 /* Allocate all CoW reservations covering a part of a file. */
387 xfs_reflink_allocate_cow_range(
388 struct xfs_inode *ip,
392 struct xfs_mount *mp = ip->i_mount;
393 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
394 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count);
397 ASSERT(xfs_is_reflink_inode(ip));
399 trace_xfs_reflink_allocate_cow_range(ip, offset, count);
402 * Make sure that the dquots are there.
404 error = xfs_qm_dqattach(ip, 0);
408 while (offset_fsb < end_fsb) {
409 error = __xfs_reflink_allocate_cow(ip, &offset_fsb, end_fsb);
411 trace_xfs_reflink_allocate_cow_range_error(ip, error,
421 * Find the CoW reservation (and whether or not it needs block allocation)
422 * for a given byte offset of a file.
425 xfs_reflink_find_cow_mapping(
426 struct xfs_inode *ip,
428 struct xfs_bmbt_irec *imap,
431 struct xfs_bmbt_irec irec;
432 struct xfs_ifork *ifp;
433 struct xfs_bmbt_rec_host *gotp;
437 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
438 ASSERT(xfs_is_reflink_inode(ip));
440 /* Find the extent in the CoW fork. */
441 ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
442 bno = XFS_B_TO_FSBT(ip->i_mount, offset);
443 gotp = xfs_iext_bno_to_ext(ifp, bno, &idx);
447 xfs_bmbt_get_all(gotp, &irec);
448 if (bno >= irec.br_startoff + irec.br_blockcount ||
449 bno < irec.br_startoff)
452 trace_xfs_reflink_find_cow_mapping(ip, offset, 1, XFS_IO_OVERWRITE,
455 /* If it's still delalloc, we must allocate later. */
457 *need_alloc = !!(isnullstartblock(irec.br_startblock));
463 * Trim an extent to end at the next CoW reservation past offset_fsb.
466 xfs_reflink_trim_irec_to_next_cow(
467 struct xfs_inode *ip,
468 xfs_fileoff_t offset_fsb,
469 struct xfs_bmbt_irec *imap)
471 struct xfs_bmbt_irec irec;
472 struct xfs_ifork *ifp;
473 struct xfs_bmbt_rec_host *gotp;
476 if (!xfs_is_reflink_inode(ip))
479 /* Find the extent in the CoW fork. */
480 ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
481 gotp = xfs_iext_bno_to_ext(ifp, offset_fsb, &idx);
484 xfs_bmbt_get_all(gotp, &irec);
486 /* This is the extent before; try sliding up one. */
487 if (irec.br_startoff < offset_fsb) {
489 if (idx >= ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
491 gotp = xfs_iext_get_ext(ifp, idx);
492 xfs_bmbt_get_all(gotp, &irec);
495 if (irec.br_startoff >= imap->br_startoff + imap->br_blockcount)
498 imap->br_blockcount = irec.br_startoff - imap->br_startoff;
499 trace_xfs_reflink_trim_irec(ip, imap);
505 * Cancel all pending CoW reservations for some block range of an inode.
508 xfs_reflink_cancel_cow_blocks(
509 struct xfs_inode *ip,
510 struct xfs_trans **tpp,
511 xfs_fileoff_t offset_fsb,
512 xfs_fileoff_t end_fsb)
514 struct xfs_bmbt_irec irec;
515 xfs_filblks_t count_fsb;
516 xfs_fsblock_t firstfsb;
517 struct xfs_defer_ops dfops;
521 if (!xfs_is_reflink_inode(ip))
524 /* Go find the old extent in the CoW fork. */
525 while (offset_fsb < end_fsb) {
527 count_fsb = (xfs_filblks_t)(end_fsb - offset_fsb);
528 error = xfs_bmapi_read(ip, offset_fsb, count_fsb, &irec,
529 &nimaps, XFS_BMAPI_COWFORK);
534 trace_xfs_reflink_cancel_cow(ip, &irec);
536 if (irec.br_startblock == DELAYSTARTBLOCK) {
537 /* Remove the mapping from the CoW fork. */
538 error = xfs_bunmapi_cow(ip, &irec);
541 } else if (irec.br_startblock == HOLESTARTBLOCK) {
544 xfs_trans_ijoin(*tpp, ip, 0);
545 xfs_defer_init(&dfops, &firstfsb);
547 /* Free the CoW orphan record. */
548 error = xfs_refcount_free_cow_extent(ip->i_mount,
549 &dfops, irec.br_startblock,
554 xfs_bmap_add_free(ip->i_mount, &dfops,
555 irec.br_startblock, irec.br_blockcount,
558 /* Update quota accounting */
559 xfs_trans_mod_dquot_byino(*tpp, ip, XFS_TRANS_DQ_BCOUNT,
560 -(long)irec.br_blockcount);
562 /* Roll the transaction */
563 error = xfs_defer_finish(tpp, &dfops, ip);
565 xfs_defer_cancel(&dfops);
569 /* Remove the mapping from the CoW fork. */
570 error = xfs_bunmapi_cow(ip, &irec);
576 offset_fsb = irec.br_startoff + irec.br_blockcount;
583 * Cancel all pending CoW reservations for some byte range of an inode.
586 xfs_reflink_cancel_cow_range(
587 struct xfs_inode *ip,
591 struct xfs_trans *tp;
592 xfs_fileoff_t offset_fsb;
593 xfs_fileoff_t end_fsb;
596 trace_xfs_reflink_cancel_cow_range(ip, offset, count);
597 ASSERT(xfs_is_reflink_inode(ip));
599 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
600 if (count == NULLFILEOFF)
601 end_fsb = NULLFILEOFF;
603 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
605 /* Start a rolling transaction to remove the mappings */
606 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
611 xfs_ilock(ip, XFS_ILOCK_EXCL);
612 xfs_trans_ijoin(tp, ip, 0);
614 /* Scrape out the old CoW reservations */
615 error = xfs_reflink_cancel_cow_blocks(ip, &tp, offset_fsb, end_fsb);
619 error = xfs_trans_commit(tp);
621 xfs_iunlock(ip, XFS_ILOCK_EXCL);
625 xfs_trans_cancel(tp);
626 xfs_iunlock(ip, XFS_ILOCK_EXCL);
628 trace_xfs_reflink_cancel_cow_range_error(ip, error, _RET_IP_);
633 * Remap parts of a file's data fork after a successful CoW.
637 struct xfs_inode *ip,
641 struct xfs_bmbt_irec irec;
642 struct xfs_bmbt_irec uirec;
643 struct xfs_trans *tp;
644 xfs_fileoff_t offset_fsb;
645 xfs_fileoff_t end_fsb;
646 xfs_filblks_t count_fsb;
647 xfs_fsblock_t firstfsb;
648 struct xfs_defer_ops dfops;
650 unsigned int resblks;
655 trace_xfs_reflink_end_cow(ip, offset, count);
657 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
658 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
659 count_fsb = (xfs_filblks_t)(end_fsb - offset_fsb);
661 /* Start a rolling transaction to switch the mappings */
662 resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
663 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
668 xfs_ilock(ip, XFS_ILOCK_EXCL);
669 xfs_trans_ijoin(tp, ip, 0);
671 /* Go find the old extent in the CoW fork. */
672 while (offset_fsb < end_fsb) {
673 /* Read extent from the source file */
675 count_fsb = (xfs_filblks_t)(end_fsb - offset_fsb);
676 error = xfs_bmapi_read(ip, offset_fsb, count_fsb, &irec,
677 &nimaps, XFS_BMAPI_COWFORK);
682 ASSERT(irec.br_startblock != DELAYSTARTBLOCK);
683 trace_xfs_reflink_cow_remap(ip, &irec);
686 * We can have a hole in the CoW fork if part of a directio
687 * write is CoW but part of it isn't.
689 rlen = ilen = irec.br_blockcount;
690 if (irec.br_startblock == HOLESTARTBLOCK)
693 /* Unmap the old blocks in the data fork. */
695 xfs_defer_init(&dfops, &firstfsb);
696 error = __xfs_bunmapi(tp, ip, irec.br_startoff,
697 &rlen, 0, 1, &firstfsb, &dfops);
702 * Trim the extent to whatever got unmapped.
703 * Remember, bunmapi works backwards.
705 uirec.br_startblock = irec.br_startblock + rlen;
706 uirec.br_startoff = irec.br_startoff + rlen;
707 uirec.br_blockcount = irec.br_blockcount - rlen;
708 irec.br_blockcount = rlen;
709 trace_xfs_reflink_cow_remap_piece(ip, &uirec);
711 /* Free the CoW orphan record. */
712 error = xfs_refcount_free_cow_extent(tp->t_mountp,
713 &dfops, uirec.br_startblock,
714 uirec.br_blockcount);
718 /* Map the new blocks into the data fork. */
719 error = xfs_bmap_map_extent(tp->t_mountp, &dfops,
724 /* Remove the mapping from the CoW fork. */
725 error = xfs_bunmapi_cow(ip, &uirec);
729 error = xfs_defer_finish(&tp, &dfops, ip);
736 offset_fsb = irec.br_startoff + ilen;
739 error = xfs_trans_commit(tp);
740 xfs_iunlock(ip, XFS_ILOCK_EXCL);
746 xfs_defer_cancel(&dfops);
748 xfs_trans_cancel(tp);
749 xfs_iunlock(ip, XFS_ILOCK_EXCL);
751 trace_xfs_reflink_end_cow_error(ip, error, _RET_IP_);
756 * Free leftover CoW reservations that didn't get cleaned out.
759 xfs_reflink_recover_cow(
760 struct xfs_mount *mp)
765 if (!xfs_sb_version_hasreflink(&mp->m_sb))
768 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
769 error = xfs_refcount_recover_cow_leftovers(mp, agno);
778 * Reflinking (Block) Ranges of Two Files Together
780 * First, ensure that the reflink flag is set on both inodes. The flag is an
781 * optimization to avoid unnecessary refcount btree lookups in the write path.
783 * Now we can iteratively remap the range of extents (and holes) in src to the
784 * corresponding ranges in dest. Let drange and srange denote the ranges of
785 * logical blocks in dest and src touched by the reflink operation.
787 * While the length of drange is greater than zero,
788 * - Read src's bmbt at the start of srange ("imap")
789 * - If imap doesn't exist, make imap appear to start at the end of srange
791 * - If imap starts before srange, advance imap to start at srange.
792 * - If imap goes beyond srange, truncate imap to end at the end of srange.
793 * - Punch (imap start - srange start + imap len) blocks from dest at
794 * offset (drange start).
795 * - If imap points to a real range of pblks,
796 * > Increase the refcount of the imap's pblks
797 * > Map imap's pblks into dest at the offset
798 * (drange start + imap start - srange start)
799 * - Advance drange and srange by (imap start - srange start + imap len)
801 * Finally, if the reflink made dest longer, update both the in-core and
802 * on-disk file sizes.
804 * ASCII Art Demonstration:
806 * Let's say we want to reflink this source file:
808 * ----SSSSSSS-SSSSS----SSSSSS (src file)
809 * <-------------------->
811 * into this destination file:
813 * --DDDDDDDDDDDDDDDDDDD--DDD (dest file)
814 * <-------------------->
815 * '-' means a hole, and 'S' and 'D' are written blocks in the src and dest.
816 * Observe that the range has different logical offsets in either file.
818 * Consider that the first extent in the source file doesn't line up with our
819 * reflink range. Unmapping and remapping are separate operations, so we can
820 * unmap more blocks from the destination file than we remap.
822 * ----SSSSSSS-SSSSS----SSSSSS
824 * --DDDDD---------DDDDD--DDD
827 * Now remap the source extent into the destination file:
829 * ----SSSSSSS-SSSSS----SSSSSS
831 * --DDDDD--SSSSSSSDDDDD--DDD
834 * Do likewise with the second hole and extent in our range. Holes in the
835 * unmap range don't affect our operation.
837 * ----SSSSSSS-SSSSS----SSSSSS
839 * --DDDDD--SSSSSSS-SSSSS-DDD
842 * Finally, unmap and remap part of the third extent. This will increase the
843 * size of the destination file.
845 * ----SSSSSSS-SSSSS----SSSSSS
847 * --DDDDD--SSSSSSS-SSSSS----SSS
850 * Once we update the destination file's i_size, we're done.
854 * Ensure the reflink bit is set in both inodes.
857 xfs_reflink_set_inode_flag(
858 struct xfs_inode *src,
859 struct xfs_inode *dest)
861 struct xfs_mount *mp = src->i_mount;
863 struct xfs_trans *tp;
865 if (xfs_is_reflink_inode(src) && xfs_is_reflink_inode(dest))
868 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
872 /* Lock both files against IO */
873 if (src->i_ino == dest->i_ino)
874 xfs_ilock(src, XFS_ILOCK_EXCL);
876 xfs_lock_two_inodes(src, dest, XFS_ILOCK_EXCL);
878 if (!xfs_is_reflink_inode(src)) {
879 trace_xfs_reflink_set_inode_flag(src);
880 xfs_trans_ijoin(tp, src, XFS_ILOCK_EXCL);
881 src->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
882 xfs_trans_log_inode(tp, src, XFS_ILOG_CORE);
883 xfs_ifork_init_cow(src);
885 xfs_iunlock(src, XFS_ILOCK_EXCL);
887 if (src->i_ino == dest->i_ino)
890 if (!xfs_is_reflink_inode(dest)) {
891 trace_xfs_reflink_set_inode_flag(dest);
892 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
893 dest->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
894 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
895 xfs_ifork_init_cow(dest);
897 xfs_iunlock(dest, XFS_ILOCK_EXCL);
900 error = xfs_trans_commit(tp);
906 trace_xfs_reflink_set_inode_flag_error(dest, error, _RET_IP_);
911 * Update destination inode size & cowextsize hint, if necessary.
914 xfs_reflink_update_dest(
915 struct xfs_inode *dest,
917 xfs_extlen_t cowextsize)
919 struct xfs_mount *mp = dest->i_mount;
920 struct xfs_trans *tp;
923 if (newlen <= i_size_read(VFS_I(dest)) && cowextsize == 0)
926 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
930 xfs_ilock(dest, XFS_ILOCK_EXCL);
931 xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL);
933 if (newlen > i_size_read(VFS_I(dest))) {
934 trace_xfs_reflink_update_inode_size(dest, newlen);
935 i_size_write(VFS_I(dest), newlen);
936 dest->i_d.di_size = newlen;
940 dest->i_d.di_cowextsize = cowextsize;
941 dest->i_d.di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
944 xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE);
946 error = xfs_trans_commit(tp);
952 trace_xfs_reflink_update_inode_size_error(dest, error, _RET_IP_);
957 * Do we have enough reserve in this AG to handle a reflink? The refcount
958 * btree already reserved all the space it needs, but the rmap btree can grow
959 * infinitely, so we won't allow more reflinks when the AG is down to the
963 xfs_reflink_ag_has_free_space(
964 struct xfs_mount *mp,
967 struct xfs_perag *pag;
970 if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
973 pag = xfs_perag_get(mp, agno);
974 if (xfs_ag_resv_critical(pag, XFS_AG_RESV_AGFL) ||
975 xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA))
982 * Unmap a range of blocks from a file, then map other blocks into the hole.
983 * The range to unmap is (destoff : destoff + srcioff + irec->br_blockcount).
984 * The extent irec is mapped into dest at irec->br_startoff.
987 xfs_reflink_remap_extent(
988 struct xfs_inode *ip,
989 struct xfs_bmbt_irec *irec,
990 xfs_fileoff_t destoff,
993 struct xfs_mount *mp = ip->i_mount;
994 struct xfs_trans *tp;
995 xfs_fsblock_t firstfsb;
996 unsigned int resblks;
997 struct xfs_defer_ops dfops;
998 struct xfs_bmbt_irec uirec;
1001 xfs_filblks_t unmap_len;
1005 unmap_len = irec->br_startoff + irec->br_blockcount - destoff;
1006 trace_xfs_reflink_punch_range(ip, destoff, unmap_len);
1008 /* Only remap normal extents. */
1009 real_extent = (irec->br_startblock != HOLESTARTBLOCK &&
1010 irec->br_startblock != DELAYSTARTBLOCK &&
1011 !ISUNWRITTEN(irec));
1013 /* No reflinking if we're low on space */
1015 error = xfs_reflink_ag_has_free_space(mp,
1016 XFS_FSB_TO_AGNO(mp, irec->br_startblock));
1021 /* Start a rolling transaction to switch the mappings */
1022 resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
1023 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
1027 xfs_ilock(ip, XFS_ILOCK_EXCL);
1028 xfs_trans_ijoin(tp, ip, 0);
1030 /* If we're not just clearing space, then do we have enough quota? */
1032 error = xfs_trans_reserve_quota_nblks(tp, ip,
1033 irec->br_blockcount, 0, XFS_QMOPT_RES_REGBLKS);
1038 trace_xfs_reflink_remap(ip, irec->br_startoff,
1039 irec->br_blockcount, irec->br_startblock);
1041 /* Unmap the old blocks in the data fork. */
1044 xfs_defer_init(&dfops, &firstfsb);
1045 error = __xfs_bunmapi(tp, ip, destoff, &rlen, 0, 1,
1051 * Trim the extent to whatever got unmapped.
1052 * Remember, bunmapi works backwards.
1054 uirec.br_startblock = irec->br_startblock + rlen;
1055 uirec.br_startoff = irec->br_startoff + rlen;
1056 uirec.br_blockcount = unmap_len - rlen;
1059 /* If this isn't a real mapping, we're done. */
1060 if (!real_extent || uirec.br_blockcount == 0)
1063 trace_xfs_reflink_remap(ip, uirec.br_startoff,
1064 uirec.br_blockcount, uirec.br_startblock);
1066 /* Update the refcount tree */
1067 error = xfs_refcount_increase_extent(mp, &dfops, &uirec);
1071 /* Map the new blocks into the data fork. */
1072 error = xfs_bmap_map_extent(mp, &dfops, ip, &uirec);
1076 /* Update quota accounting. */
1077 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT,
1078 uirec.br_blockcount);
1080 /* Update dest isize if needed. */
1081 newlen = XFS_FSB_TO_B(mp,
1082 uirec.br_startoff + uirec.br_blockcount);
1083 newlen = min_t(xfs_off_t, newlen, new_isize);
1084 if (newlen > i_size_read(VFS_I(ip))) {
1085 trace_xfs_reflink_update_inode_size(ip, newlen);
1086 i_size_write(VFS_I(ip), newlen);
1087 ip->i_d.di_size = newlen;
1088 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1092 /* Process all the deferred stuff. */
1093 error = xfs_defer_finish(&tp, &dfops, ip);
1098 error = xfs_trans_commit(tp);
1099 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1105 xfs_defer_cancel(&dfops);
1107 xfs_trans_cancel(tp);
1108 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1110 trace_xfs_reflink_remap_extent_error(ip, error, _RET_IP_);
1115 * Iteratively remap one file's extents (and holes) to another's.
1118 xfs_reflink_remap_blocks(
1119 struct xfs_inode *src,
1120 xfs_fileoff_t srcoff,
1121 struct xfs_inode *dest,
1122 xfs_fileoff_t destoff,
1124 xfs_off_t new_isize)
1126 struct xfs_bmbt_irec imap;
1129 xfs_filblks_t range_len;
1131 /* drange = (destoff, destoff + len); srange = (srcoff, srcoff + len) */
1133 trace_xfs_reflink_remap_blocks_loop(src, srcoff, len,
1135 /* Read extent from the source file */
1137 xfs_ilock(src, XFS_ILOCK_EXCL);
1138 error = xfs_bmapi_read(src, srcoff, len, &imap, &nimaps, 0);
1139 xfs_iunlock(src, XFS_ILOCK_EXCL);
1142 ASSERT(nimaps == 1);
1144 trace_xfs_reflink_remap_imap(src, srcoff, len, XFS_IO_OVERWRITE,
1147 /* Translate imap into the destination file. */
1148 range_len = imap.br_startoff + imap.br_blockcount - srcoff;
1149 imap.br_startoff += destoff - srcoff;
1151 /* Clear dest from destoff to the end of imap and map it in. */
1152 error = xfs_reflink_remap_extent(dest, &imap, destoff,
1157 if (fatal_signal_pending(current)) {
1162 /* Advance drange/srange */
1163 srcoff += range_len;
1164 destoff += range_len;
1171 trace_xfs_reflink_remap_blocks_error(dest, error, _RET_IP_);
1176 * Read a page's worth of file data into the page cache. Return the page
1179 static struct page *
1181 struct inode *inode,
1184 struct address_space *mapping;
1188 n = offset >> PAGE_SHIFT;
1189 mapping = inode->i_mapping;
1190 page = read_mapping_page(mapping, n, NULL);
1193 if (!PageUptodate(page)) {
1195 return ERR_PTR(-EIO);
1202 * Compare extents of two files to see if they are the same.
1205 xfs_compare_extents(
1214 xfs_off_t dest_poff;
1217 struct page *src_page;
1218 struct page *dest_page;
1226 src_poff = srcoff & (PAGE_SIZE - 1);
1227 dest_poff = destoff & (PAGE_SIZE - 1);
1228 cmp_len = min(PAGE_SIZE - src_poff,
1229 PAGE_SIZE - dest_poff);
1230 cmp_len = min(cmp_len, len);
1231 ASSERT(cmp_len > 0);
1233 trace_xfs_reflink_compare_extents(XFS_I(src), srcoff, cmp_len,
1234 XFS_I(dest), destoff);
1236 src_page = xfs_get_page(src, srcoff);
1237 if (IS_ERR(src_page)) {
1238 error = PTR_ERR(src_page);
1241 dest_page = xfs_get_page(dest, destoff);
1242 if (IS_ERR(dest_page)) {
1243 error = PTR_ERR(dest_page);
1244 unlock_page(src_page);
1248 src_addr = kmap_atomic(src_page);
1249 dest_addr = kmap_atomic(dest_page);
1251 flush_dcache_page(src_page);
1252 flush_dcache_page(dest_page);
1254 if (memcmp(src_addr + src_poff, dest_addr + dest_poff, cmp_len))
1257 kunmap_atomic(dest_addr);
1258 kunmap_atomic(src_addr);
1259 unlock_page(dest_page);
1260 unlock_page(src_page);
1261 put_page(dest_page);
1276 trace_xfs_reflink_compare_extents_error(XFS_I(dest), error, _RET_IP_);
1281 * Link a range of blocks from one file to another.
1284 xfs_reflink_remap_range(
1285 struct file *file_in,
1287 struct file *file_out,
1292 struct inode *inode_in = file_inode(file_in);
1293 struct xfs_inode *src = XFS_I(inode_in);
1294 struct inode *inode_out = file_inode(file_out);
1295 struct xfs_inode *dest = XFS_I(inode_out);
1296 struct xfs_mount *mp = src->i_mount;
1297 loff_t bs = inode_out->i_sb->s_blocksize;
1298 bool same_inode = (inode_in == inode_out);
1299 xfs_fileoff_t sfsbno, dfsbno;
1300 xfs_filblks_t fsblen;
1301 xfs_extlen_t cowextsize;
1306 if (!xfs_sb_version_hasreflink(&mp->m_sb))
1309 if (XFS_FORCED_SHUTDOWN(mp))
1312 /* Lock both files against IO */
1314 xfs_ilock(src, XFS_IOLOCK_EXCL);
1315 xfs_ilock(src, XFS_MMAPLOCK_EXCL);
1317 xfs_lock_two_inodes(src, dest, XFS_IOLOCK_EXCL);
1318 xfs_lock_two_inodes(src, dest, XFS_MMAPLOCK_EXCL);
1321 /* Don't touch certain kinds of inodes */
1323 if (IS_IMMUTABLE(inode_out))
1327 if (IS_SWAPFILE(inode_in) || IS_SWAPFILE(inode_out))
1331 /* Don't reflink dirs, pipes, sockets... */
1333 if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
1336 if (S_ISFIFO(inode_in->i_mode) || S_ISFIFO(inode_out->i_mode))
1338 if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
1341 /* Don't reflink realtime inodes */
1342 if (XFS_IS_REALTIME_INODE(src) || XFS_IS_REALTIME_INODE(dest))
1345 /* Don't share DAX file data for now. */
1346 if (IS_DAX(inode_in) || IS_DAX(inode_out))
1349 /* Are we going all the way to the end? */
1350 isize = i_size_read(inode_in);
1357 len = isize - pos_in;
1359 /* Ensure offsets don't wrap and the input is inside i_size */
1360 if (pos_in + len < pos_in || pos_out + len < pos_out ||
1361 pos_in + len > isize)
1364 /* Don't allow dedupe past EOF in the dest file */
1368 disize = i_size_read(inode_out);
1369 if (pos_out >= disize || pos_out + len > disize)
1373 /* If we're linking to EOF, continue to the block boundary. */
1374 if (pos_in + len == isize)
1375 blen = ALIGN(isize, bs) - pos_in;
1379 /* Only reflink if we're aligned to block boundaries */
1380 if (!IS_ALIGNED(pos_in, bs) || !IS_ALIGNED(pos_in + blen, bs) ||
1381 !IS_ALIGNED(pos_out, bs) || !IS_ALIGNED(pos_out + blen, bs))
1384 /* Don't allow overlapped reflink within the same file */
1386 if (pos_out + blen > pos_in && pos_out < pos_in + blen)
1390 /* Wait for the completion of any pending IOs on both files */
1391 inode_dio_wait(inode_in);
1393 inode_dio_wait(inode_out);
1395 ret = filemap_write_and_wait_range(inode_in->i_mapping,
1396 pos_in, pos_in + len - 1);
1400 ret = filemap_write_and_wait_range(inode_out->i_mapping,
1401 pos_out, pos_out + len - 1);
1405 trace_xfs_reflink_remap_range(src, pos_in, len, dest, pos_out);
1408 * Check that the extents are the same.
1411 bool is_same = false;
1413 ret = xfs_compare_extents(inode_in, pos_in, inode_out, pos_out,
1423 ret = xfs_reflink_set_inode_flag(src, dest);
1428 * Invalidate the page cache so that we can clear any CoW mappings
1429 * in the destination file.
1431 truncate_inode_pages_range(&inode_out->i_data, pos_out,
1432 PAGE_ALIGN(pos_out + len) - 1);
1434 dfsbno = XFS_B_TO_FSBT(mp, pos_out);
1435 sfsbno = XFS_B_TO_FSBT(mp, pos_in);
1436 fsblen = XFS_B_TO_FSB(mp, len);
1437 ret = xfs_reflink_remap_blocks(src, sfsbno, dest, dfsbno, fsblen,
1443 * Carry the cowextsize hint from src to dest if we're sharing the
1444 * entire source file to the entire destination file, the source file
1445 * has a cowextsize hint, and the destination file does not.
1448 if (pos_in == 0 && len == i_size_read(inode_in) &&
1449 (src->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) &&
1450 pos_out == 0 && len >= i_size_read(inode_out) &&
1451 !(dest->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
1452 cowextsize = src->i_d.di_cowextsize;
1454 ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize);
1457 xfs_iunlock(src, XFS_MMAPLOCK_EXCL);
1458 xfs_iunlock(src, XFS_IOLOCK_EXCL);
1459 if (src->i_ino != dest->i_ino) {
1460 xfs_iunlock(dest, XFS_MMAPLOCK_EXCL);
1461 xfs_iunlock(dest, XFS_IOLOCK_EXCL);
1464 trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_);
1469 * The user wants to preemptively CoW all shared blocks in this file,
1470 * which enables us to turn off the reflink flag. Iterate all
1471 * extents which are not prealloc/delalloc to see which ranges are
1472 * mentioned in the refcount tree, then read those blocks into the
1473 * pagecache, dirty them, fsync them back out, and then we can update
1474 * the inode flag. What happens if we run out of memory? :)
1477 xfs_reflink_dirty_extents(
1478 struct xfs_inode *ip,
1483 struct xfs_mount *mp = ip->i_mount;
1484 xfs_agnumber_t agno;
1485 xfs_agblock_t agbno;
1491 struct xfs_bmbt_irec map[2];
1495 while (end - fbno > 0) {
1498 * Look for extents in the file. Skip holes, delalloc, or
1499 * unwritten extents; they can't be reflinked.
1501 error = xfs_bmapi_read(ip, fbno, end - fbno, map, &nmaps, 0);
1506 if (map[0].br_startblock == HOLESTARTBLOCK ||
1507 map[0].br_startblock == DELAYSTARTBLOCK ||
1508 ISUNWRITTEN(&map[0]))
1512 while (map[1].br_blockcount) {
1513 agno = XFS_FSB_TO_AGNO(mp, map[1].br_startblock);
1514 agbno = XFS_FSB_TO_AGBNO(mp, map[1].br_startblock);
1515 aglen = map[1].br_blockcount;
1517 error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
1518 &rbno, &rlen, true);
1521 if (rbno == NULLAGBLOCK)
1524 /* Dirty the pages */
1525 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1526 fpos = XFS_FSB_TO_B(mp, map[1].br_startoff +
1528 flen = XFS_FSB_TO_B(mp, rlen);
1529 if (fpos + flen > isize)
1530 flen = isize - fpos;
1531 error = iomap_file_dirty(VFS_I(ip), fpos, flen,
1533 xfs_ilock(ip, XFS_ILOCK_EXCL);
1537 map[1].br_blockcount -= (rbno - agbno + rlen);
1538 map[1].br_startoff += (rbno - agbno + rlen);
1539 map[1].br_startblock += (rbno - agbno + rlen);
1543 fbno = map[0].br_startoff + map[0].br_blockcount;
1549 /* Clear the inode reflink flag if there are no shared extents. */
1551 xfs_reflink_clear_inode_flag(
1552 struct xfs_inode *ip,
1553 struct xfs_trans **tpp)
1555 struct xfs_mount *mp = ip->i_mount;
1558 xfs_agnumber_t agno;
1559 xfs_agblock_t agbno;
1563 struct xfs_bmbt_irec map;
1567 ASSERT(xfs_is_reflink_inode(ip));
1570 end = XFS_B_TO_FSB(mp, i_size_read(VFS_I(ip)));
1571 while (end - fbno > 0) {
1574 * Look for extents in the file. Skip holes, delalloc, or
1575 * unwritten extents; they can't be reflinked.
1577 error = xfs_bmapi_read(ip, fbno, end - fbno, &map, &nmaps, 0);
1582 if (map.br_startblock == HOLESTARTBLOCK ||
1583 map.br_startblock == DELAYSTARTBLOCK ||
1587 agno = XFS_FSB_TO_AGNO(mp, map.br_startblock);
1588 agbno = XFS_FSB_TO_AGBNO(mp, map.br_startblock);
1589 aglen = map.br_blockcount;
1591 error = xfs_reflink_find_shared(mp, agno, agbno, aglen,
1592 &rbno, &rlen, false);
1595 /* Is there still a shared block here? */
1596 if (rbno != NULLAGBLOCK)
1599 fbno = map.br_startoff + map.br_blockcount;
1603 * We didn't find any shared blocks so turn off the reflink flag.
1604 * First, get rid of any leftover CoW mappings.
1606 error = xfs_reflink_cancel_cow_blocks(ip, tpp, 0, NULLFILEOFF);
1610 /* Clear the inode flag. */
1611 trace_xfs_reflink_unset_inode_flag(ip);
1612 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
1613 xfs_inode_clear_cowblocks_tag(ip);
1614 xfs_trans_ijoin(*tpp, ip, 0);
1615 xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
1621 * Clear the inode reflink flag if there are no shared extents and the size
1625 xfs_reflink_try_clear_inode_flag(
1626 struct xfs_inode *ip)
1628 struct xfs_mount *mp = ip->i_mount;
1629 struct xfs_trans *tp;
1632 /* Start a rolling transaction to remove the mappings */
1633 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
1637 xfs_ilock(ip, XFS_ILOCK_EXCL);
1638 xfs_trans_ijoin(tp, ip, 0);
1640 error = xfs_reflink_clear_inode_flag(ip, &tp);
1644 error = xfs_trans_commit(tp);
1648 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1651 xfs_trans_cancel(tp);
1653 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1658 * Pre-COW all shared blocks within a given byte range of a file and turn off
1659 * the reflink flag if we unshare all of the file's blocks.
1662 xfs_reflink_unshare(
1663 struct xfs_inode *ip,
1667 struct xfs_mount *mp = ip->i_mount;
1673 if (!xfs_is_reflink_inode(ip))
1676 trace_xfs_reflink_unshare(ip, offset, len);
1678 inode_dio_wait(VFS_I(ip));
1680 /* Try to CoW the selected ranges */
1681 xfs_ilock(ip, XFS_ILOCK_EXCL);
1682 fbno = XFS_B_TO_FSBT(mp, offset);
1683 isize = i_size_read(VFS_I(ip));
1684 end = XFS_B_TO_FSB(mp, offset + len);
1685 error = xfs_reflink_dirty_extents(ip, fbno, end, isize);
1688 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1690 /* Wait for the IO to finish */
1691 error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
1695 /* Turn off the reflink flag if possible. */
1696 error = xfs_reflink_try_clear_inode_flag(ip);
1703 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1705 trace_xfs_reflink_unshare_error(ip, error, _RET_IP_);
1710 * Does this inode have any real CoW reservations?
1713 xfs_reflink_has_real_cow_blocks(
1714 struct xfs_inode *ip)
1716 struct xfs_bmbt_irec irec;
1717 struct xfs_ifork *ifp;
1718 struct xfs_bmbt_rec_host *gotp;
1721 if (!xfs_is_reflink_inode(ip))
1724 /* Go find the old extent in the CoW fork. */
1725 ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
1726 gotp = xfs_iext_bno_to_ext(ifp, 0, &idx);
1728 xfs_bmbt_get_all(gotp, &irec);
1730 if (!isnullstartblock(irec.br_startblock))
1735 if (idx >= ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
1737 gotp = xfs_iext_get_ext(ifp, idx);