Lines Matching +full:data +full:- +full:mapping

1 // SPDX-License-Identifier: GPL-2.0+
44 * through the use of a copy-on-write mechanism. At a high level, that
46 * block, write the data to the new block, and if that succeeds we map the
50 * of disk blocks to dirty-but-not-yet-mapped file blocks as long as
56 * create a delalloc mapping, which is a regular in-core extent, but without
58 * a flag that this is a delalloc mapping, and a worst-case estimate of how
59 * many blocks might be required to put the mapping into the BMBT.) delalloc
68 * D: --RRRRRRSSSRRRRRRRR--- (data fork)
69 * C: ------DDDDDDD--------- (CoW fork)
73 * allocating blocks and replacing the delalloc mapping with real ones.
74 * A delalloc mapping can be replaced by several unwritten ones if the
77 * D: --RRRRRRSSSRRRRRRRR---
78 * C: ------UUUUUUU---------
80 * We want to adapt the delalloc mechanism for copy-on-write, since the
84 * to disturb the mapping in the data fork until we're sure that the write
86 * mapping from the data fork and moving the new mapping from the CoW fork to
87 * the data fork. This will be discussed shortly.
90 * Block-aligned directio writes will use the same mechanism as buffered
98 * D: --RRRRRRSSSRRRRRRRR---
99 * C: ------UUrrUUU---------
101 * CoW remapping must be done after the data block write completes,
102 * because we don't want to destroy the old data fork map until we're sure
106 * unmap the corresponding range in the data fork, map the new range into
107 * the data fork, and remove the extent from the CoW fork. Because of
109 * only to remap the blocks that we've actually written out -- we must
114 * D: --RRRRRRrrSRRRRRRRR---
115 * C: ------UU--UUU---------
127 * Given a file mapping for the data device, find the lowest-numbered run of
128 * shared blocks within that mapping and return it in shared_offset/shared_len.
150 pag = xfs_perag_get(mp, XFS_FSB_TO_AGNO(mp, irec->br_startblock)); in xfs_reflink_find_shared()
151 orig_bno = XFS_FSB_TO_AGBNO(mp, irec->br_startblock); in xfs_reflink_find_shared()
158 error = xfs_refcount_find_shared(cur, orig_bno, irec->br_blockcount, in xfs_reflink_find_shared()
164 *shared_offset = found_bno - orig_bno; in xfs_reflink_find_shared()
171 * Given a file mapping for the rt device, find the lowest-numbered run of
172 * shared blocks within that mapping and return it in shared_offset/shared_len.
198 * xfs_refcount_find_shared is shared between the RT and data device in xfs_reflink_find_rtshared()
201 orig_bno = xfs_rtb_to_rgbno(mp, irec->br_startblock); in xfs_reflink_find_rtshared()
202 rtg = xfs_rtgroup_get(mp, xfs_rtb_to_rgno(mp, irec->br_startblock)); in xfs_reflink_find_rtshared()
206 error = xfs_refcount_find_shared(cur, orig_bno, irec->br_blockcount, in xfs_reflink_find_rtshared()
213 *shared_offset = found_bno - orig_bno; in xfs_reflink_find_rtshared()
218 * Trim the mapping to the next block where there's a change in the
220 * find the lowest-numbered extent of shared blocks that coincides with
221 * the given block mapping. If the shared extent overlaps the start of
222 * the mapping, trim the mapping to the end of the shared extent. If
223 * the shared region intersects the mapping, trim the mapping to the
233 struct xfs_mount *mp = ip->i_mount; in xfs_reflink_trim_around_shared()
259 * The start of this mapping points to shared space. Truncate in xfs_reflink_trim_around_shared()
260 * the mapping at the end of the shared region so that a in xfs_reflink_trim_around_shared()
264 irec->br_blockcount = shared_len; in xfs_reflink_trim_around_shared()
269 * of the mapping. Truncate the mapping at the start of the in xfs_reflink_trim_around_shared()
273 irec->br_blockcount = shared_offset; in xfs_reflink_trim_around_shared()
287 !isnullstartblock(imap->br_startblock)) { in xfs_bmap_trim_cow()
292 /* Trim the mapping to the nearest shared extent boundary. */ in xfs_bmap_trim_cow()
308 if (!xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &got)) in xfs_reflink_convert_cow_locked()
317 return -EIO; in xfs_reflink_convert_cow_locked()
329 } while (xfs_iext_next_extent(ip->i_cowfp, &icur, &got)); in xfs_reflink_convert_cow_locked()
341 struct xfs_mount *mp = ip->i_mount; in xfs_reflink_convert_cow()
344 xfs_filblks_t count_fsb = end_fsb - offset_fsb; in xfs_reflink_convert_cow()
368 xfs_fileoff_t offset_fsb = imap->br_startoff; in xfs_find_trim_cow_extent()
369 xfs_filblks_t count_fsb = imap->br_blockcount; in xfs_find_trim_cow_extent()
378 if (!xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, cmap)) in xfs_find_trim_cow_extent()
379 cmap->br_startoff = offset_fsb + count_fsb; in xfs_find_trim_cow_extent()
380 if (cmap->br_startoff > offset_fsb) { in xfs_find_trim_cow_extent()
381 xfs_trim_extent(imap, imap->br_startoff, in xfs_find_trim_cow_extent()
382 cmap->br_startoff - imap->br_startoff); in xfs_find_trim_cow_extent()
387 if (isnullstartblock(cmap->br_startblock)) { in xfs_find_trim_cow_extent()
388 xfs_trim_extent(imap, cmap->br_startoff, cmap->br_blockcount); in xfs_find_trim_cow_extent()
392 /* real extent found - no need to allocate */ in xfs_find_trim_cow_extent()
405 xfs_fileoff_t offset_fsb = imap->br_startoff; in xfs_reflink_convert_unwritten()
406 xfs_filblks_t count_fsb = imap->br_blockcount; in xfs_reflink_convert_unwritten()
417 * data and need the conversion, but for buffered writes we're done. in xfs_reflink_convert_unwritten()
419 if (!convert_now || cmap->br_state == XFS_EXT_NORM) in xfs_reflink_convert_unwritten()
426 cmap->br_state = XFS_EXT_NORM; in xfs_reflink_convert_unwritten()
440 struct xfs_mount *mp = ip->i_mount; in xfs_reflink_fill_cow_hole()
448 resaligned = xfs_aligned_fsb_count(imap->br_startoff, in xfs_reflink_fill_cow_hole()
449 imap->br_blockcount, xfs_get_cowextsz_hint(ip)); in xfs_reflink_fill_cow_hole()
461 error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, dblocks, in xfs_reflink_fill_cow_hole()
479 error = xfs_bmapi_write(tp, ip, imap->br_startoff, imap->br_blockcount, in xfs_reflink_fill_cow_hole()
507 struct xfs_mount *mp = ip->i_mount; in xfs_reflink_fill_delalloc()
517 error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, 0, 0, in xfs_reflink_fill_delalloc()
534 ASSERT(isnullstartblock(cmap->br_startblock) || in xfs_reflink_fill_delalloc()
535 cmap->br_startblock == DELAYSTARTBLOCK); in xfs_reflink_fill_delalloc()
541 error = xfs_bmapi_write(tp, ip, cmap->br_startoff, in xfs_reflink_fill_delalloc()
542 cmap->br_blockcount, in xfs_reflink_fill_delalloc()
552 } while (cmap->br_startoff + cmap->br_blockcount <= imap->br_startoff); in xfs_reflink_fill_delalloc()
575 if (!ip->i_cowfp) { in xfs_reflink_allocate_cow()
590 * CoW fork does not have an extent and data extent is shared. in xfs_reflink_allocate_cow()
593 if (cmap->br_startoff > imap->br_startoff) in xfs_reflink_allocate_cow()
599 * There may or may not be a data fork mapping. in xfs_reflink_allocate_cow()
601 if (isnullstartblock(cmap->br_startblock) || in xfs_reflink_allocate_cow()
602 cmap->br_startblock == DELAYSTARTBLOCK) in xfs_reflink_allocate_cow()
608 return -EFSCORRUPTED; in xfs_reflink_allocate_cow()
642 xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb); in xfs_reflink_cancel_cow_blocks()
656 ASSERT((*tpp)->t_highest_agno == NULLAGNUMBER); in xfs_reflink_cancel_cow_blocks()
674 /* Remove the mapping from the CoW fork. */ in xfs_reflink_cancel_cow_blocks()
689 if (!ifp->if_bytes) in xfs_reflink_cancel_cow_blocks()
713 ASSERT(ip->i_cowfp); in xfs_reflink_cancel_cow_range()
715 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset); in xfs_reflink_cancel_cow_range()
719 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count); in xfs_reflink_cancel_cow_range()
722 error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write, in xfs_reflink_cancel_cow_range()
752 * something from the CoW fork to the data fork, we must update the quota
753 * accounting for delayed allocations. For remapping from the data fork to the
754 * data fork, use regular block accounting.
779 * Remap part of the CoW fork into the data fork.
782 * into the data fork; this function will remap what it can (at the end of the
796 struct xfs_bmbt_irec got, del, data; in xfs_reflink_end_cow_extent_locked() local
814 * Only remap real extents that contain data. With AIO, speculative in xfs_reflink_end_cow_extent_locked()
817 * deletion; from now on @del represents the mapping that we're in xfs_reflink_end_cow_extent_locked()
828 xfs_trim_extent(&del, *offset_fsb, end_fsb - *offset_fsb); in xfs_reflink_end_cow_extent_locked()
835 /* Grab the corresponding mapping in the data fork. */ in xfs_reflink_end_cow_extent_locked()
837 error = xfs_bmapi_read(ip, del.br_startoff, del.br_blockcount, &data, in xfs_reflink_end_cow_extent_locked()
843 data.br_blockcount = min(data.br_blockcount, del.br_blockcount); in xfs_reflink_end_cow_extent_locked()
844 del.br_blockcount = data.br_blockcount; in xfs_reflink_end_cow_extent_locked()
847 trace_xfs_reflink_cow_remap_to(ip, &data); in xfs_reflink_end_cow_extent_locked()
849 if (xfs_bmap_is_real_extent(&data)) { in xfs_reflink_end_cow_extent_locked()
854 xfs_bmap_unmap_extent(tp, ip, XFS_DATA_FORK, &data); in xfs_reflink_end_cow_extent_locked()
855 xfs_refcount_decrease_extent(tp, isrt, &data); in xfs_reflink_end_cow_extent_locked()
856 xfs_reflink_update_quota(tp, ip, false, -data.br_blockcount); in xfs_reflink_end_cow_extent_locked()
857 } else if (data.br_startblock == DELAYSTARTBLOCK) { in xfs_reflink_end_cow_extent_locked()
866 error = xfs_bunmapi(NULL, ip, data.br_startoff, in xfs_reflink_end_cow_extent_locked()
867 data.br_blockcount, 0, 1, &done); in xfs_reflink_end_cow_extent_locked()
877 /* Map the new blocks into the data fork. */ in xfs_reflink_end_cow_extent_locked()
880 /* Charge this new data fork mapping to the on-disk quota. */ in xfs_reflink_end_cow_extent_locked()
883 /* Remove the mapping from the CoW fork. */ in xfs_reflink_end_cow_extent_locked()
892 * Remap part of the CoW fork into the data fork.
895 * into the data fork; this function will remap what it can (at the end of the
907 struct xfs_mount *mp = ip->i_mount; in xfs_reflink_end_cow_extent()
913 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, in xfs_reflink_end_cow_extent()
930 * Remap parts of a file's data fork after a successful CoW.
944 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset); in xfs_reflink_end_cow()
945 end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count); in xfs_reflink_end_cow()
960 * region. There are also have post-eof checks in the writeback in xfs_reflink_end_cow()
988 * Fully remap all of the file's data fork at once, which is the critical part
1002 struct xfs_mount *mp = ip->i_mount; in xfs_reflink_end_atomic_cow()
1015 resblks = (end_fsb - offset_fsb) * in xfs_reflink_end_atomic_cow()
1018 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_atomic_ioend, resblks, 0, in xfs_reflink_end_atomic_cow()
1053 * Atomic write limits must always be a power-of-2, according to in xfs_reflink_max_atomic_cow()
1106 * - Read src's bmbt at the start of srange ("imap")
1107 * - If imap doesn't exist, make imap appear to start at the end of srange
1109 * - If imap starts before srange, advance imap to start at srange.
1110 * - If imap goes beyond srange, truncate imap to end at the end of srange.
1111 * - Punch (imap start - srange start + imap len) blocks from dest at
1113 * - If imap points to a real range of pblks,
1116 * (drange start + imap start - srange start)
1117 * - Advance drange and srange by (imap start - srange start + imap len)
1119 * Finally, if the reflink made dest longer, update both the in-core and
1120 * on-disk file sizes.
1126 * ----SSSSSSS-SSSSS----SSSSSS (src file)
1127 * <-------------------->
1131 * --DDDDDDDDDDDDDDDDDDD--DDD (dest file)
1132 * <-------------------->
1133 * '-' means a hole, and 'S' and 'D' are written blocks in the src and dest.
1140 * ----SSSSSSS-SSSSS----SSSSSS
1141 * <------->
1142 * --DDDDD---------DDDDD--DDD
1143 * <------->
1147 * ----SSSSSSS-SSSSS----SSSSSS
1148 * <------->
1149 * --DDDDD--SSSSSSSDDDDD--DDD
1150 * <------->
1155 * ----SSSSSSS-SSSSS----SSSSSS
1156 * <---->
1157 * --DDDDD--SSSSSSS-SSSSS-DDD
1158 * <---->
1163 * ----SSSSSSS-SSSSS----SSSSSS
1164 * <----->
1165 * --DDDDD--SSSSSSS-SSSSS----SSS
1166 * <----->
1179 struct xfs_mount *mp = src->i_mount; in xfs_reflink_set_inode_flag()
1186 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp); in xfs_reflink_set_inode_flag()
1191 if (src->i_ino == dest->i_ino) in xfs_reflink_set_inode_flag()
1199 src->i_diflags2 |= XFS_DIFLAG2_REFLINK; in xfs_reflink_set_inode_flag()
1205 if (src->i_ino == dest->i_ino) in xfs_reflink_set_inode_flag()
1211 dest->i_diflags2 |= XFS_DIFLAG2_REFLINK; in xfs_reflink_set_inode_flag()
1238 struct xfs_mount *mp = dest->i_mount; in xfs_reflink_update_dest()
1245 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp); in xfs_reflink_update_dest()
1255 dest->i_disk_size = newlen; in xfs_reflink_update_dest()
1259 dest->i_cowextsize = cowextsize; in xfs_reflink_update_dest()
1260 dest->i_diflags2 |= XFS_DIFLAG2_COWEXTSIZE; in xfs_reflink_update_dest()
1295 return -ENOSPC; in xfs_reflink_ag_has_free_space()
1303 error = -ENOSPC; in xfs_reflink_ag_has_free_space()
1319 struct xfs_mount *mp = ip->i_mount; in xfs_reflink_remap_extent()
1343 * mapping dmap into a sparse part of the file plus the bmbt split. We in xfs_reflink_remap_extent()
1344 * haven't locked the inode or read the existing mapping yet, so we do in xfs_reflink_remap_extent()
1356 rblocks = dmap->br_blockcount; in xfs_reflink_remap_extent()
1358 dblocks = resblks + dmap->br_blockcount; in xfs_reflink_remap_extent()
1361 error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, in xfs_reflink_remap_extent()
1363 if (error == -EDQUOT || error == -ENOSPC) { in xfs_reflink_remap_extent()
1365 error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, in xfs_reflink_remap_extent()
1377 error = xfs_bmapi_read(ip, dmap->br_startoff, dmap->br_blockcount, in xfs_reflink_remap_extent()
1381 ASSERT(nimaps == 1 && smap.br_startoff == dmap->br_startoff); in xfs_reflink_remap_extent()
1388 dmap->br_blockcount = min(dmap->br_blockcount, smap.br_blockcount); in xfs_reflink_remap_extent()
1389 ASSERT(dmap->br_blockcount == smap.br_blockcount); in xfs_reflink_remap_extent()
1398 if (dmap->br_startblock == smap.br_startblock) { in xfs_reflink_remap_extent()
1399 if (dmap->br_state != smap.br_state) { in xfs_reflink_remap_extent()
1401 error = -EFSCORRUPTED; in xfs_reflink_remap_extent()
1407 if (dmap->br_state == XFS_EXT_UNWRITTEN && in xfs_reflink_remap_extent()
1411 /* No reflinking if the AG of the dest mapping is low on space. */ in xfs_reflink_remap_extent()
1414 dmap->br_startblock); in xfs_reflink_remap_extent()
1423 * If we are mapping a written extent into the file, we need to have in xfs_reflink_remap_extent()
1444 rblocks = dmap->br_blockcount; in xfs_reflink_remap_extent()
1446 dblocks = dmap->br_blockcount; in xfs_reflink_remap_extent()
1472 qdelta -= smap.br_blockcount; in xfs_reflink_remap_extent()
1496 qdelta += dmap->br_blockcount; in xfs_reflink_remap_extent()
1502 newlen = XFS_FSB_TO_B(mp, dmap->br_startoff + dmap->br_blockcount); in xfs_reflink_remap_extent()
1507 ip->i_disk_size = newlen; in xfs_reflink_remap_extent()
1536 struct xfs_mount *mp = src->i_mount; in xfs_reflink_remap_blocks()
1571 error = -EFSCORRUPTED; in xfs_reflink_remap_blocks()
1584 error = -EINTR; in xfs_reflink_remap_blocks()
1591 len -= imap.br_blockcount; in xfs_reflink_remap_blocks()
1599 XFS_FSB_TO_B(src->i_mount, remapped_len)); in xfs_reflink_remap_blocks()
1605 * zero any speculative post-EOF preallocations that sit between the old EOF
1618 trace_xfs_zero_eof(ip, isize, pos - isize); in xfs_reflink_zero_posteof()
1619 return xfs_zero_range(ip, isize, pos - isize, NULL, NULL); in xfs_reflink_zero_posteof()
1636 * of the destination file, then we can expose stale data from beyond the source
1648 * stale data in the destination file. Hence we reject these clone attempts with
1649 * -EINVAL in this case.
1672 ret = -EINVAL; in xfs_reflink_remap_prep()
1673 /* Can't reflink between data and rt volumes */ in xfs_reflink_remap_prep()
1677 /* Don't share DAX file data with non-DAX file. */ in xfs_reflink_remap_prep()
1696 * Zero existing post-eof speculative preallocations in the destination in xfs_reflink_remap_prep()
1714 loff_t flen = *len + (pos_out - XFS_ISIZE(dest)); in xfs_reflink_remap_prep()
1740 struct xfs_mount *mp = ip->i_mount; in xfs_reflink_inode_has_shared_extents()
1815 ip->i_diflags2 &= ~XFS_DIFLAG2_REFLINK; in xfs_reflink_clear_inode_flag()
1830 struct xfs_mount *mp = ip->i_mount; in xfs_reflink_try_clear_inode_flag()
1835 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp); in xfs_reflink_try_clear_inode_flag()
1860 * Pre-COW all shared blocks within a given byte range of a file and turn off
1889 error = filemap_write_and_wait_range(inode->i_mapping, offset, in xfs_reflink_unshare()
1890 offset + len - 1); in xfs_reflink_unshare()
1921 * because we would have to perform CoW-around for unaligned write in xfs_reflink_supports_rextsize()