Dave Chinner | 0b61f8a | 2018-06-05 19:42:14 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 Oracle. All Rights Reserved. |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4 | * Author: Darrick J. Wong <darrick.wong@oracle.com> |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 5 | */ |
| 6 | #include "xfs.h" |
| 7 | #include "xfs_fs.h" |
| 8 | #include "xfs_shared.h" |
| 9 | #include "xfs_format.h" |
| 10 | #include "xfs_log_format.h" |
| 11 | #include "xfs_trans_resv.h" |
| 12 | #include "xfs_mount.h" |
| 13 | #include "xfs_defer.h" |
| 14 | #include "xfs_da_format.h" |
| 15 | #include "xfs_da_btree.h" |
| 16 | #include "xfs_inode.h" |
| 17 | #include "xfs_trans.h" |
| 18 | #include "xfs_inode_item.h" |
| 19 | #include "xfs_bmap.h" |
| 20 | #include "xfs_bmap_util.h" |
| 21 | #include "xfs_error.h" |
| 22 | #include "xfs_dir2.h" |
| 23 | #include "xfs_dir2_priv.h" |
| 24 | #include "xfs_ioctl.h" |
| 25 | #include "xfs_trace.h" |
| 26 | #include "xfs_log.h" |
| 27 | #include "xfs_icache.h" |
| 28 | #include "xfs_pnfs.h" |
Darrick J. Wong | 174edb0 | 2016-10-03 09:11:39 -0700 | [diff] [blame] | 29 | #include "xfs_btree.h" |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 30 | #include "xfs_refcount_btree.h" |
| 31 | #include "xfs_refcount.h" |
| 32 | #include "xfs_bmap_btree.h" |
| 33 | #include "xfs_trans_space.h" |
| 34 | #include "xfs_bit.h" |
| 35 | #include "xfs_alloc.h" |
| 36 | #include "xfs_quota_defs.h" |
| 37 | #include "xfs_quota.h" |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 38 | #include "xfs_reflink.h" |
Darrick J. Wong | 2a06705 | 2016-10-03 09:11:33 -0700 | [diff] [blame] | 39 | #include "xfs_iomap.h" |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 40 | #include "xfs_rmap_btree.h" |
Darrick J. Wong | 6fa164b | 2016-10-03 09:11:45 -0700 | [diff] [blame] | 41 | #include "xfs_sb.h" |
| 42 | #include "xfs_ag_resv.h" |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 43 | |
| 44 | /* |
| 45 | * Copy on Write of Shared Blocks |
| 46 | * |
| 47 | * XFS must preserve "the usual" file semantics even when two files share |
| 48 | * the same physical blocks. This means that a write to one file must not |
| 49 | * alter the blocks in a different file; the way that we'll do that is |
| 50 | * through the use of a copy-on-write mechanism. At a high level, that |
| 51 | * means that when we want to write to a shared block, we allocate a new |
| 52 | * block, write the data to the new block, and if that succeeds we map the |
| 53 | * new block into the file. |
| 54 | * |
| 55 | * XFS provides a "delayed allocation" mechanism that defers the allocation |
| 56 | * of disk blocks to dirty-but-not-yet-mapped file blocks as long as |
| 57 | * possible. This reduces fragmentation by enabling the filesystem to ask |
| 58 | * for bigger chunks less often, which is exactly what we want for CoW. |
| 59 | * |
| 60 | * The delalloc mechanism begins when the kernel wants to make a block |
| 61 | * writable (write_begin or page_mkwrite). If the offset is not mapped, we |
| 62 | * create a delalloc mapping, which is a regular in-core extent, but without |
| 63 | * a real startblock. (For delalloc mappings, the startblock encodes both |
| 64 | * a flag that this is a delalloc mapping, and a worst-case estimate of how |
| 65 | * many blocks might be required to put the mapping into the BMBT.) delalloc |
| 66 | * mappings are a reservation against the free space in the filesystem; |
| 67 | * adjacent mappings can also be combined into fewer larger mappings. |
| 68 | * |
Darrick J. Wong | 5eda430 | 2017-02-02 15:14:02 -0800 | [diff] [blame] | 69 | * As an optimization, the CoW extent size hint (cowextsz) creates |
| 70 | * outsized aligned delalloc reservations in the hope of landing out of |
| 71 | * order nearby CoW writes in a single extent on disk, thereby reducing |
| 72 | * fragmentation and improving future performance. |
| 73 | * |
| 74 | * D: --RRRRRRSSSRRRRRRRR--- (data fork) |
| 75 | * C: ------DDDDDDD--------- (CoW fork) |
| 76 | * |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 77 | * When dirty pages are being written out (typically in writepage), the |
Darrick J. Wong | 5eda430 | 2017-02-02 15:14:02 -0800 | [diff] [blame] | 78 | * delalloc reservations are converted into unwritten mappings by |
| 79 | * allocating blocks and replacing the delalloc mapping with real ones. |
| 80 | * A delalloc mapping can be replaced by several unwritten ones if the |
| 81 | * free space is fragmented. |
| 82 | * |
| 83 | * D: --RRRRRRSSSRRRRRRRR--- |
| 84 | * C: ------UUUUUUU--------- |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 85 | * |
| 86 | * We want to adapt the delalloc mechanism for copy-on-write, since the |
| 87 | * write paths are similar. The first two steps (creating the reservation |
| 88 | * and allocating the blocks) are exactly the same as delalloc except that |
| 89 | * the mappings must be stored in a separate CoW fork because we do not want |
| 90 | * to disturb the mapping in the data fork until we're sure that the write |
| 91 | * succeeded. IO completion in this case is the process of removing the old |
| 92 | * mapping from the data fork and moving the new mapping from the CoW fork to |
| 93 | * the data fork. This will be discussed shortly. |
| 94 | * |
| 95 | * For now, unaligned directio writes will be bounced back to the page cache. |
| 96 | * Block-aligned directio writes will use the same mechanism as buffered |
| 97 | * writes. |
| 98 | * |
Darrick J. Wong | 5eda430 | 2017-02-02 15:14:02 -0800 | [diff] [blame] | 99 | * Just prior to submitting the actual disk write requests, we convert |
| 100 | * the extents representing the range of the file actually being written |
| 101 | * (as opposed to extra pieces created for the cowextsize hint) to real |
| 102 | * extents. This will become important in the next step: |
| 103 | * |
| 104 | * D: --RRRRRRSSSRRRRRRRR--- |
| 105 | * C: ------UUrrUUU--------- |
| 106 | * |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 107 | * CoW remapping must be done after the data block write completes, |
| 108 | * because we don't want to destroy the old data fork map until we're sure |
| 109 | * the new block has been written. Since the new mappings are kept in a |
| 110 | * separate fork, we can simply iterate these mappings to find the ones |
| 111 | * that cover the file blocks that we just CoW'd. For each extent, simply |
| 112 | * unmap the corresponding range in the data fork, map the new range into |
Darrick J. Wong | 5eda430 | 2017-02-02 15:14:02 -0800 | [diff] [blame] | 113 | * the data fork, and remove the extent from the CoW fork. Because of |
| 114 | * the presence of the cowextsize hint, however, we must be careful |
| 115 | * only to remap the blocks that we've actually written out -- we must |
| 116 | * never remap delalloc reservations nor CoW staging blocks that have |
| 117 | * yet to be written. This corresponds exactly to the real extents in |
| 118 | * the CoW fork: |
| 119 | * |
| 120 | * D: --RRRRRRrrSRRRRRRRR--- |
| 121 | * C: ------UU--UUU--------- |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 122 | * |
| 123 | * Since the remapping operation can be applied to an arbitrary file |
| 124 | * range, we record the need for the remap step as a flag in the ioend |
| 125 | * instead of declaring a new IO type. This is required for direct io |
| 126 | * because we only have ioend for the whole dio, and we have to be able to |
| 127 | * remember the presence of unwritten blocks and CoW blocks with a single |
| 128 | * ioend structure. Better yet, the more ground we can cover with one |
| 129 | * ioend, the better. |
| 130 | */ |
Darrick J. Wong | 2a06705 | 2016-10-03 09:11:33 -0700 | [diff] [blame] | 131 | |
| 132 | /* |
| 133 | * Given an AG extent, find the lowest-numbered run of shared blocks |
| 134 | * within that range and return the range in fbno/flen. If |
| 135 | * find_end_of_shared is true, return the longest contiguous extent of |
| 136 | * shared blocks. If there are no shared extents, fbno and flen will |
| 137 | * be set to NULLAGBLOCK and 0, respectively. |
| 138 | */ |
| 139 | int |
| 140 | xfs_reflink_find_shared( |
| 141 | struct xfs_mount *mp, |
Darrick J. Wong | 92ff728 | 2017-06-16 11:00:10 -0700 | [diff] [blame] | 142 | struct xfs_trans *tp, |
Darrick J. Wong | 2a06705 | 2016-10-03 09:11:33 -0700 | [diff] [blame] | 143 | xfs_agnumber_t agno, |
| 144 | xfs_agblock_t agbno, |
| 145 | xfs_extlen_t aglen, |
| 146 | xfs_agblock_t *fbno, |
| 147 | xfs_extlen_t *flen, |
| 148 | bool find_end_of_shared) |
| 149 | { |
| 150 | struct xfs_buf *agbp; |
| 151 | struct xfs_btree_cur *cur; |
| 152 | int error; |
| 153 | |
Darrick J. Wong | 92ff728 | 2017-06-16 11:00:10 -0700 | [diff] [blame] | 154 | error = xfs_alloc_read_agf(mp, tp, agno, 0, &agbp); |
Darrick J. Wong | 2a06705 | 2016-10-03 09:11:33 -0700 | [diff] [blame] | 155 | if (error) |
| 156 | return error; |
Darrick J. Wong | 10479e2 | 2017-07-17 14:30:57 -0700 | [diff] [blame] | 157 | if (!agbp) |
| 158 | return -ENOMEM; |
Darrick J. Wong | 2a06705 | 2016-10-03 09:11:33 -0700 | [diff] [blame] | 159 | |
Brian Foster | ed7ef8e | 2018-07-11 22:26:17 -0700 | [diff] [blame] | 160 | cur = xfs_refcountbt_init_cursor(mp, tp, agbp, agno); |
Darrick J. Wong | 2a06705 | 2016-10-03 09:11:33 -0700 | [diff] [blame] | 161 | |
| 162 | error = xfs_refcount_find_shared(cur, agbno, aglen, fbno, flen, |
| 163 | find_end_of_shared); |
| 164 | |
| 165 | xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 166 | |
Darrick J. Wong | 92ff728 | 2017-06-16 11:00:10 -0700 | [diff] [blame] | 167 | xfs_trans_brelse(tp, agbp); |
Darrick J. Wong | 2a06705 | 2016-10-03 09:11:33 -0700 | [diff] [blame] | 168 | return error; |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * Trim the mapping to the next block where there's a change in the |
| 173 | * shared/unshared status. More specifically, this means that we |
| 174 | * find the lowest-numbered extent of shared blocks that coincides with |
| 175 | * the given block mapping. If the shared extent overlaps the start of |
| 176 | * the mapping, trim the mapping to the end of the shared extent. If |
| 177 | * the shared region intersects the mapping, trim the mapping to the |
| 178 | * start of the shared extent. If there are no shared regions that |
| 179 | * overlap, just return the original extent. |
| 180 | */ |
| 181 | int |
| 182 | xfs_reflink_trim_around_shared( |
| 183 | struct xfs_inode *ip, |
| 184 | struct xfs_bmbt_irec *irec, |
| 185 | bool *shared, |
| 186 | bool *trimmed) |
| 187 | { |
| 188 | xfs_agnumber_t agno; |
| 189 | xfs_agblock_t agbno; |
| 190 | xfs_extlen_t aglen; |
| 191 | xfs_agblock_t fbno; |
| 192 | xfs_extlen_t flen; |
| 193 | int error = 0; |
| 194 | |
| 195 | /* Holes, unwritten, and delalloc extents cannot be shared */ |
Christoph Hellwig | 9c4f29d | 2017-03-28 14:53:35 -0700 | [diff] [blame] | 196 | if (!xfs_is_reflink_inode(ip) || !xfs_bmap_is_real_extent(irec)) { |
Darrick J. Wong | 2a06705 | 2016-10-03 09:11:33 -0700 | [diff] [blame] | 197 | *shared = false; |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | trace_xfs_reflink_trim_around_shared(ip, irec); |
| 202 | |
| 203 | agno = XFS_FSB_TO_AGNO(ip->i_mount, irec->br_startblock); |
| 204 | agbno = XFS_FSB_TO_AGBNO(ip->i_mount, irec->br_startblock); |
| 205 | aglen = irec->br_blockcount; |
| 206 | |
Darrick J. Wong | 92ff728 | 2017-06-16 11:00:10 -0700 | [diff] [blame] | 207 | error = xfs_reflink_find_shared(ip->i_mount, NULL, agno, agbno, |
Darrick J. Wong | 2a06705 | 2016-10-03 09:11:33 -0700 | [diff] [blame] | 208 | aglen, &fbno, &flen, true); |
| 209 | if (error) |
| 210 | return error; |
| 211 | |
| 212 | *shared = *trimmed = false; |
| 213 | if (fbno == NULLAGBLOCK) { |
| 214 | /* No shared blocks at all. */ |
| 215 | return 0; |
| 216 | } else if (fbno == agbno) { |
| 217 | /* |
| 218 | * The start of this extent is shared. Truncate the |
| 219 | * mapping at the end of the shared region so that a |
| 220 | * subsequent iteration starts at the start of the |
| 221 | * unshared region. |
| 222 | */ |
| 223 | irec->br_blockcount = flen; |
| 224 | *shared = true; |
| 225 | if (flen != aglen) |
| 226 | *trimmed = true; |
| 227 | return 0; |
| 228 | } else { |
| 229 | /* |
| 230 | * There's a shared extent midway through this extent. |
| 231 | * Truncate the mapping at the start of the shared |
| 232 | * extent so that a subsequent iteration starts at the |
| 233 | * start of the shared region. |
| 234 | */ |
| 235 | irec->br_blockcount = fbno - agbno; |
| 236 | *trimmed = true; |
| 237 | return 0; |
| 238 | } |
| 239 | } |
| 240 | |
Christoph Hellwig | 3ba020b | 2016-10-20 15:53:50 +1100 | [diff] [blame] | 241 | /* |
| 242 | * Trim the passed in imap to the next shared/unshared extent boundary, and |
| 243 | * if imap->br_startoff points to a shared extent reserve space for it in the |
| 244 | * COW fork. In this case *shared is set to true, else to false. |
| 245 | * |
| 246 | * Note that imap will always contain the block numbers for the existing blocks |
| 247 | * in the data fork, as the upper layers need them for read-modify-write |
| 248 | * operations. |
| 249 | */ |
| 250 | int |
| 251 | xfs_reflink_reserve_cow( |
Darrick J. Wong | 2a06705 | 2016-10-03 09:11:33 -0700 | [diff] [blame] | 252 | struct xfs_inode *ip, |
Christoph Hellwig | 3ba020b | 2016-10-20 15:53:50 +1100 | [diff] [blame] | 253 | struct xfs_bmbt_irec *imap, |
| 254 | bool *shared) |
Darrick J. Wong | 2a06705 | 2016-10-03 09:11:33 -0700 | [diff] [blame] | 255 | { |
Christoph Hellwig | 2755fc44 | 2016-11-24 11:39:49 +1100 | [diff] [blame] | 256 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK); |
| 257 | struct xfs_bmbt_irec got; |
Christoph Hellwig | 2755fc44 | 2016-11-24 11:39:49 +1100 | [diff] [blame] | 258 | int error = 0; |
| 259 | bool eof = false, trimmed; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 260 | struct xfs_iext_cursor icur; |
Darrick J. Wong | 2a06705 | 2016-10-03 09:11:33 -0700 | [diff] [blame] | 261 | |
Christoph Hellwig | 3ba020b | 2016-10-20 15:53:50 +1100 | [diff] [blame] | 262 | /* |
| 263 | * Search the COW fork extent list first. This serves two purposes: |
| 264 | * first this implement the speculative preallocation using cowextisze, |
| 265 | * so that we also unshared block adjacent to shared blocks instead |
| 266 | * of just the shared blocks themselves. Second the lookup in the |
| 267 | * extent list is generally faster than going out to the shared extent |
| 268 | * tree. |
| 269 | */ |
Christoph Hellwig | 2755fc44 | 2016-11-24 11:39:49 +1100 | [diff] [blame] | 270 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 271 | if (!xfs_iext_lookup_extent(ip, ifp, imap->br_startoff, &icur, &got)) |
Christoph Hellwig | 2755fc44 | 2016-11-24 11:39:49 +1100 | [diff] [blame] | 272 | eof = true; |
Christoph Hellwig | 3ba020b | 2016-10-20 15:53:50 +1100 | [diff] [blame] | 273 | if (!eof && got.br_startoff <= imap->br_startoff) { |
| 274 | trace_xfs_reflink_cow_found(ip, imap); |
| 275 | xfs_trim_extent(imap, got.br_startoff, got.br_blockcount); |
Darrick J. Wong | 2a06705 | 2016-10-03 09:11:33 -0700 | [diff] [blame] | 276 | |
Christoph Hellwig | 3ba020b | 2016-10-20 15:53:50 +1100 | [diff] [blame] | 277 | *shared = true; |
| 278 | return 0; |
| 279 | } |
Darrick J. Wong | 2a06705 | 2016-10-03 09:11:33 -0700 | [diff] [blame] | 280 | |
| 281 | /* Trim the mapping to the nearest shared extent boundary. */ |
Christoph Hellwig | 3ba020b | 2016-10-20 15:53:50 +1100 | [diff] [blame] | 282 | error = xfs_reflink_trim_around_shared(ip, imap, shared, &trimmed); |
Darrick J. Wong | 2a06705 | 2016-10-03 09:11:33 -0700 | [diff] [blame] | 283 | if (error) |
Christoph Hellwig | 3ba020b | 2016-10-20 15:53:50 +1100 | [diff] [blame] | 284 | return error; |
Darrick J. Wong | 2a06705 | 2016-10-03 09:11:33 -0700 | [diff] [blame] | 285 | |
| 286 | /* Not shared? Just report the (potentially capped) extent. */ |
Christoph Hellwig | 3ba020b | 2016-10-20 15:53:50 +1100 | [diff] [blame] | 287 | if (!*shared) |
| 288 | return 0; |
Darrick J. Wong | 2a06705 | 2016-10-03 09:11:33 -0700 | [diff] [blame] | 289 | |
| 290 | /* |
| 291 | * Fork all the shared blocks from our write offset until the end of |
| 292 | * the extent. |
| 293 | */ |
Darrick J. Wong | 4882c19 | 2018-05-04 15:30:22 -0700 | [diff] [blame] | 294 | error = xfs_qm_dqattach_locked(ip, false); |
Darrick J. Wong | 2a06705 | 2016-10-03 09:11:33 -0700 | [diff] [blame] | 295 | if (error) |
Christoph Hellwig | 3ba020b | 2016-10-20 15:53:50 +1100 | [diff] [blame] | 296 | return error; |
| 297 | |
Christoph Hellwig | 3ba020b | 2016-10-20 15:53:50 +1100 | [diff] [blame] | 298 | error = xfs_bmapi_reserve_delalloc(ip, XFS_COW_FORK, imap->br_startoff, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 299 | imap->br_blockcount, 0, &got, &icur, eof); |
Brian Foster | 0260d8f | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 300 | if (error == -ENOSPC || error == -EDQUOT) |
Christoph Hellwig | 3ba020b | 2016-10-20 15:53:50 +1100 | [diff] [blame] | 301 | trace_xfs_reflink_cow_enospc(ip, imap); |
Brian Foster | 0260d8f | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 302 | if (error) |
Christoph Hellwig | 3ba020b | 2016-10-20 15:53:50 +1100 | [diff] [blame] | 303 | return error; |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 304 | |
Darrick J. Wong | 2a06705 | 2016-10-03 09:11:33 -0700 | [diff] [blame] | 305 | trace_xfs_reflink_cow_alloc(ip, &got); |
Christoph Hellwig | 3ba020b | 2016-10-20 15:53:50 +1100 | [diff] [blame] | 306 | return 0; |
Darrick J. Wong | 2a06705 | 2016-10-03 09:11:33 -0700 | [diff] [blame] | 307 | } |
Darrick J. Wong | ef47366 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 308 | |
Darrick J. Wong | 5eda430 | 2017-02-02 15:14:02 -0800 | [diff] [blame] | 309 | /* Convert part of an unwritten CoW extent to a real one. */ |
| 310 | STATIC int |
| 311 | xfs_reflink_convert_cow_extent( |
| 312 | struct xfs_inode *ip, |
| 313 | struct xfs_bmbt_irec *imap, |
| 314 | xfs_fileoff_t offset_fsb, |
Brian Foster | 8a74938 | 2018-07-11 22:26:06 -0700 | [diff] [blame] | 315 | xfs_filblks_t count_fsb) |
Darrick J. Wong | 5eda430 | 2017-02-02 15:14:02 -0800 | [diff] [blame] | 316 | { |
Darrick J. Wong | 5eda430 | 2017-02-02 15:14:02 -0800 | [diff] [blame] | 317 | int nimaps = 1; |
| 318 | |
| 319 | if (imap->br_state == XFS_EXT_NORM) |
| 320 | return 0; |
| 321 | |
Christoph Hellwig | dcf9585 | 2017-02-06 10:46:01 -0800 | [diff] [blame] | 322 | xfs_trim_extent(imap, offset_fsb, count_fsb); |
| 323 | trace_xfs_reflink_convert_cow(ip, imap); |
| 324 | if (imap->br_blockcount == 0) |
Darrick J. Wong | 5eda430 | 2017-02-02 15:14:02 -0800 | [diff] [blame] | 325 | return 0; |
Christoph Hellwig | dcf9585 | 2017-02-06 10:46:01 -0800 | [diff] [blame] | 326 | return xfs_bmapi_write(NULL, ip, imap->br_startoff, imap->br_blockcount, |
Brian Foster | 3ae2d89 | 2018-07-11 22:26:19 -0700 | [diff] [blame^] | 327 | XFS_BMAPI_COWFORK | XFS_BMAPI_CONVERT, NULL, 0, imap, |
| 328 | &nimaps); |
Darrick J. Wong | 5eda430 | 2017-02-02 15:14:02 -0800 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /* Convert all of the unwritten CoW extents in a file's range to real ones. */ |
| 332 | int |
| 333 | xfs_reflink_convert_cow( |
| 334 | struct xfs_inode *ip, |
| 335 | xfs_off_t offset, |
| 336 | xfs_off_t count) |
| 337 | { |
Darrick J. Wong | 5eda430 | 2017-02-02 15:14:02 -0800 | [diff] [blame] | 338 | struct xfs_mount *mp = ip->i_mount; |
Darrick J. Wong | 5eda430 | 2017-02-02 15:14:02 -0800 | [diff] [blame] | 339 | xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset); |
| 340 | xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count); |
Christoph Hellwig | b121459 | 2017-11-03 10:34:44 -0700 | [diff] [blame] | 341 | xfs_filblks_t count_fsb = end_fsb - offset_fsb; |
| 342 | struct xfs_bmbt_irec imap; |
Christoph Hellwig | b121459 | 2017-11-03 10:34:44 -0700 | [diff] [blame] | 343 | int nimaps = 1, error = 0; |
| 344 | |
| 345 | ASSERT(count != 0); |
Darrick J. Wong | 5eda430 | 2017-02-02 15:14:02 -0800 | [diff] [blame] | 346 | |
| 347 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | b121459 | 2017-11-03 10:34:44 -0700 | [diff] [blame] | 348 | error = xfs_bmapi_write(NULL, ip, offset_fsb, count_fsb, |
| 349 | XFS_BMAPI_COWFORK | XFS_BMAPI_CONVERT | |
Brian Foster | 3ae2d89 | 2018-07-11 22:26:19 -0700 | [diff] [blame^] | 350 | XFS_BMAPI_CONVERT_ONLY, NULL, 0, &imap, &nimaps); |
Darrick J. Wong | 5eda430 | 2017-02-02 15:14:02 -0800 | [diff] [blame] | 351 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 352 | return error; |
| 353 | } |
| 354 | |
Darrick J. Wong | 0613f16 | 2016-10-03 09:11:37 -0700 | [diff] [blame] | 355 | /* Allocate all CoW reservations covering a range of blocks in a file. */ |
Christoph Hellwig | 3c68d44 | 2017-02-06 10:51:03 -0800 | [diff] [blame] | 356 | int |
| 357 | xfs_reflink_allocate_cow( |
Darrick J. Wong | 0613f16 | 2016-10-03 09:11:37 -0700 | [diff] [blame] | 358 | struct xfs_inode *ip, |
Christoph Hellwig | 3c68d44 | 2017-02-06 10:51:03 -0800 | [diff] [blame] | 359 | struct xfs_bmbt_irec *imap, |
| 360 | bool *shared, |
| 361 | uint *lockmode) |
Darrick J. Wong | 0613f16 | 2016-10-03 09:11:37 -0700 | [diff] [blame] | 362 | { |
| 363 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | 3c68d44 | 2017-02-06 10:51:03 -0800 | [diff] [blame] | 364 | xfs_fileoff_t offset_fsb = imap->br_startoff; |
| 365 | xfs_filblks_t count_fsb = imap->br_blockcount; |
| 366 | struct xfs_bmbt_irec got; |
Darrick J. Wong | 0613f16 | 2016-10-03 09:11:37 -0700 | [diff] [blame] | 367 | struct xfs_defer_ops dfops; |
Christoph Hellwig | 3c68d44 | 2017-02-06 10:51:03 -0800 | [diff] [blame] | 368 | struct xfs_trans *tp = NULL; |
Darrick J. Wong | 0613f16 | 2016-10-03 09:11:37 -0700 | [diff] [blame] | 369 | xfs_fsblock_t first_block; |
Christoph Hellwig | 3c68d44 | 2017-02-06 10:51:03 -0800 | [diff] [blame] | 370 | int nimaps, error = 0; |
| 371 | bool trimmed; |
Christoph Hellwig | a14234c | 2017-02-06 10:50:49 -0800 | [diff] [blame] | 372 | xfs_filblks_t resaligned; |
Christoph Hellwig | 3c68d44 | 2017-02-06 10:51:03 -0800 | [diff] [blame] | 373 | xfs_extlen_t resblks = 0; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 374 | struct xfs_iext_cursor icur; |
Darrick J. Wong | 0613f16 | 2016-10-03 09:11:37 -0700 | [diff] [blame] | 375 | |
Christoph Hellwig | 3c68d44 | 2017-02-06 10:51:03 -0800 | [diff] [blame] | 376 | retry: |
| 377 | ASSERT(xfs_is_reflink_inode(ip)); |
Christoph Hellwig | c7dbe3f | 2018-03-13 23:15:31 -0700 | [diff] [blame] | 378 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Darrick J. Wong | 0613f16 | 2016-10-03 09:11:37 -0700 | [diff] [blame] | 379 | |
Christoph Hellwig | a14234c | 2017-02-06 10:50:49 -0800 | [diff] [blame] | 380 | /* |
| 381 | * Even if the extent is not shared we might have a preallocation for |
| 382 | * it in the COW fork. If so use it. |
| 383 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 384 | if (xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &got) && |
Christoph Hellwig | 3c68d44 | 2017-02-06 10:51:03 -0800 | [diff] [blame] | 385 | got.br_startoff <= offset_fsb) { |
| 386 | *shared = true; |
| 387 | |
Christoph Hellwig | a14234c | 2017-02-06 10:50:49 -0800 | [diff] [blame] | 388 | /* If we have a real allocation in the COW fork we're done. */ |
| 389 | if (!isnullstartblock(got.br_startblock)) { |
Christoph Hellwig | 3c68d44 | 2017-02-06 10:51:03 -0800 | [diff] [blame] | 390 | xfs_trim_extent(&got, offset_fsb, count_fsb); |
| 391 | *imap = got; |
| 392 | goto convert; |
Christoph Hellwig | a14234c | 2017-02-06 10:50:49 -0800 | [diff] [blame] | 393 | } |
Christoph Hellwig | 3c68d44 | 2017-02-06 10:51:03 -0800 | [diff] [blame] | 394 | |
| 395 | xfs_trim_extent(imap, got.br_startoff, got.br_blockcount); |
Christoph Hellwig | a14234c | 2017-02-06 10:50:49 -0800 | [diff] [blame] | 396 | } else { |
Christoph Hellwig | 3c68d44 | 2017-02-06 10:51:03 -0800 | [diff] [blame] | 397 | error = xfs_reflink_trim_around_shared(ip, imap, shared, &trimmed); |
| 398 | if (error || !*shared) |
| 399 | goto out; |
| 400 | } |
| 401 | |
| 402 | if (!tp) { |
| 403 | resaligned = xfs_aligned_fsb_count(imap->br_startoff, |
| 404 | imap->br_blockcount, xfs_get_cowextsz_hint(ip)); |
| 405 | resblks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned); |
| 406 | |
| 407 | xfs_iunlock(ip, *lockmode); |
| 408 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp); |
| 409 | *lockmode = XFS_ILOCK_EXCL; |
| 410 | xfs_ilock(ip, *lockmode); |
| 411 | |
Christoph Hellwig | a14234c | 2017-02-06 10:50:49 -0800 | [diff] [blame] | 412 | if (error) |
Christoph Hellwig | 3c68d44 | 2017-02-06 10:51:03 -0800 | [diff] [blame] | 413 | return error; |
Christoph Hellwig | 3ba020b | 2016-10-20 15:53:50 +1100 | [diff] [blame] | 414 | |
Darrick J. Wong | 4882c19 | 2018-05-04 15:30:22 -0700 | [diff] [blame] | 415 | error = xfs_qm_dqattach_locked(ip, false); |
Christoph Hellwig | a14234c | 2017-02-06 10:50:49 -0800 | [diff] [blame] | 416 | if (error) |
Christoph Hellwig | 3c68d44 | 2017-02-06 10:51:03 -0800 | [diff] [blame] | 417 | goto out; |
| 418 | goto retry; |
Darrick J. Wong | 0613f16 | 2016-10-03 09:11:37 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Christoph Hellwig | a14234c | 2017-02-06 10:50:49 -0800 | [diff] [blame] | 421 | error = xfs_trans_reserve_quota_nblks(tp, ip, resblks, 0, |
| 422 | XFS_QMOPT_RES_REGBLKS); |
Darrick J. Wong | 0613f16 | 2016-10-03 09:11:37 -0700 | [diff] [blame] | 423 | if (error) |
Christoph Hellwig | 3c68d44 | 2017-02-06 10:51:03 -0800 | [diff] [blame] | 424 | goto out; |
Darrick J. Wong | 0613f16 | 2016-10-03 09:11:37 -0700 | [diff] [blame] | 425 | |
Christoph Hellwig | a14234c | 2017-02-06 10:50:49 -0800 | [diff] [blame] | 426 | xfs_trans_ijoin(tp, ip, 0); |
| 427 | |
Brian Foster | bcd2c9f | 2018-07-11 22:26:19 -0700 | [diff] [blame] | 428 | xfs_defer_init(tp, &dfops, &first_block); |
Christoph Hellwig | a14234c | 2017-02-06 10:50:49 -0800 | [diff] [blame] | 429 | nimaps = 1; |
| 430 | |
| 431 | /* Allocate the entire reservation as unwritten blocks. */ |
Christoph Hellwig | 3c68d44 | 2017-02-06 10:51:03 -0800 | [diff] [blame] | 432 | error = xfs_bmapi_write(tp, ip, imap->br_startoff, imap->br_blockcount, |
Christoph Hellwig | a14234c | 2017-02-06 10:50:49 -0800 | [diff] [blame] | 433 | XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC, &first_block, |
Brian Foster | 6e702a5 | 2018-07-11 22:26:12 -0700 | [diff] [blame] | 434 | resblks, imap, &nimaps); |
Christoph Hellwig | a14234c | 2017-02-06 10:50:49 -0800 | [diff] [blame] | 435 | if (error) |
| 436 | goto out_bmap_cancel; |
| 437 | |
Darrick J. Wong | 86d692b | 2017-12-14 15:46:06 -0800 | [diff] [blame] | 438 | xfs_inode_set_cowblocks_tag(ip); |
| 439 | |
Darrick J. Wong | 5eda430 | 2017-02-02 15:14:02 -0800 | [diff] [blame] | 440 | /* Finish up. */ |
Brian Foster | 175d1a0 | 2018-07-11 22:26:12 -0700 | [diff] [blame] | 441 | error = xfs_defer_finish(&tp, tp->t_dfops); |
Darrick J. Wong | 0613f16 | 2016-10-03 09:11:37 -0700 | [diff] [blame] | 442 | if (error) |
Christoph Hellwig | a14234c | 2017-02-06 10:50:49 -0800 | [diff] [blame] | 443 | goto out_bmap_cancel; |
Darrick J. Wong | 0613f16 | 2016-10-03 09:11:37 -0700 | [diff] [blame] | 444 | |
| 445 | error = xfs_trans_commit(tp); |
Christoph Hellwig | a14234c | 2017-02-06 10:50:49 -0800 | [diff] [blame] | 446 | if (error) |
Christoph Hellwig | 3c68d44 | 2017-02-06 10:51:03 -0800 | [diff] [blame] | 447 | return error; |
Darrick J. Wong | 9f37bd1 | 2018-01-26 11:37:44 -0800 | [diff] [blame] | 448 | |
| 449 | /* |
| 450 | * Allocation succeeded but the requested range was not even partially |
| 451 | * satisfied? Bail out! |
| 452 | */ |
| 453 | if (nimaps == 0) |
| 454 | return -ENOSPC; |
Christoph Hellwig | 3c68d44 | 2017-02-06 10:51:03 -0800 | [diff] [blame] | 455 | convert: |
Brian Foster | 8a74938 | 2018-07-11 22:26:06 -0700 | [diff] [blame] | 456 | return xfs_reflink_convert_cow_extent(ip, imap, offset_fsb, count_fsb); |
Christoph Hellwig | a14234c | 2017-02-06 10:50:49 -0800 | [diff] [blame] | 457 | out_bmap_cancel: |
Brian Foster | 175d1a0 | 2018-07-11 22:26:12 -0700 | [diff] [blame] | 458 | xfs_defer_cancel(tp->t_dfops); |
Christoph Hellwig | a14234c | 2017-02-06 10:50:49 -0800 | [diff] [blame] | 459 | xfs_trans_unreserve_quota_nblks(tp, ip, (long)resblks, 0, |
| 460 | XFS_QMOPT_RES_REGBLKS); |
Christoph Hellwig | 3c68d44 | 2017-02-06 10:51:03 -0800 | [diff] [blame] | 461 | out: |
| 462 | if (tp) |
| 463 | xfs_trans_cancel(tp); |
| 464 | return error; |
Darrick J. Wong | 0613f16 | 2016-10-03 09:11:37 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Darrick J. Wong | ef47366 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 467 | /* |
Christoph Hellwig | 3802a34 | 2017-03-07 16:45:58 -0800 | [diff] [blame] | 468 | * Cancel CoW reservations for some block range of an inode. |
| 469 | * |
| 470 | * If cancel_real is true this function cancels all COW fork extents for the |
| 471 | * inode; if cancel_real is false, real extents are not cleared. |
Dave Chinner | c5295c6 | 2018-05-09 07:49:09 -0700 | [diff] [blame] | 472 | * |
| 473 | * Caller must have already joined the inode to the current transaction. The |
| 474 | * inode will be joined to the transaction returned to the caller. |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 475 | */ |
| 476 | int |
| 477 | xfs_reflink_cancel_cow_blocks( |
| 478 | struct xfs_inode *ip, |
| 479 | struct xfs_trans **tpp, |
| 480 | xfs_fileoff_t offset_fsb, |
Christoph Hellwig | 3802a34 | 2017-03-07 16:45:58 -0800 | [diff] [blame] | 481 | xfs_fileoff_t end_fsb, |
| 482 | bool cancel_real) |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 483 | { |
Christoph Hellwig | 3e0ee78 | 2016-10-20 15:54:31 +1100 | [diff] [blame] | 484 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK); |
Christoph Hellwig | df5ab1b | 2016-11-24 11:39:50 +1100 | [diff] [blame] | 485 | struct xfs_bmbt_irec got, del; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 486 | struct xfs_iext_cursor icur; |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 487 | xfs_fsblock_t firstfsb; |
| 488 | struct xfs_defer_ops dfops; |
Brian Foster | 27356a0 | 2018-07-11 22:26:18 -0700 | [diff] [blame] | 489 | struct xfs_defer_ops *odfops = (*tpp)->t_dfops; |
Christoph Hellwig | df5ab1b | 2016-11-24 11:39:50 +1100 | [diff] [blame] | 490 | int error = 0; |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 491 | |
| 492 | if (!xfs_is_reflink_inode(ip)) |
| 493 | return 0; |
Christoph Hellwig | 41caabd | 2017-11-03 10:34:44 -0700 | [diff] [blame] | 494 | if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got)) |
Christoph Hellwig | 3e0ee78 | 2016-10-20 15:54:31 +1100 | [diff] [blame] | 495 | return 0; |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 496 | |
Christoph Hellwig | 41caabd | 2017-11-03 10:34:44 -0700 | [diff] [blame] | 497 | /* Walk backwards until we're out of the I/O range... */ |
| 498 | while (got.br_startoff + got.br_blockcount > offset_fsb) { |
Christoph Hellwig | 3e0ee78 | 2016-10-20 15:54:31 +1100 | [diff] [blame] | 499 | del = got; |
| 500 | xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb); |
Christoph Hellwig | 41caabd | 2017-11-03 10:34:44 -0700 | [diff] [blame] | 501 | |
| 502 | /* Extent delete may have bumped ext forward */ |
| 503 | if (!del.br_blockcount) { |
| 504 | xfs_iext_prev(ifp, &icur); |
| 505 | goto next_extent; |
| 506 | } |
| 507 | |
Christoph Hellwig | 3e0ee78 | 2016-10-20 15:54:31 +1100 | [diff] [blame] | 508 | trace_xfs_reflink_cancel_cow(ip, &del); |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 509 | |
Christoph Hellwig | 3e0ee78 | 2016-10-20 15:54:31 +1100 | [diff] [blame] | 510 | if (isnullstartblock(del.br_startblock)) { |
| 511 | error = xfs_bmap_del_extent_delay(ip, XFS_COW_FORK, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 512 | &icur, &got, &del); |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 513 | if (error) |
| 514 | break; |
Christoph Hellwig | 3802a34 | 2017-03-07 16:45:58 -0800 | [diff] [blame] | 515 | } else if (del.br_state == XFS_EXT_UNWRITTEN || cancel_real) { |
Brian Foster | bcd2c9f | 2018-07-11 22:26:19 -0700 | [diff] [blame] | 516 | xfs_defer_init(*tpp, &dfops, &firstfsb); |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 517 | |
Darrick J. Wong | 174edb0 | 2016-10-03 09:11:39 -0700 | [diff] [blame] | 518 | /* Free the CoW orphan record. */ |
| 519 | error = xfs_refcount_free_cow_extent(ip->i_mount, |
Brian Foster | 27356a0 | 2018-07-11 22:26:18 -0700 | [diff] [blame] | 520 | (*tpp)->t_dfops, del.br_startblock, |
Christoph Hellwig | 3e0ee78 | 2016-10-20 15:54:31 +1100 | [diff] [blame] | 521 | del.br_blockcount); |
Darrick J. Wong | 174edb0 | 2016-10-03 09:11:39 -0700 | [diff] [blame] | 522 | if (error) |
| 523 | break; |
| 524 | |
Brian Foster | 27356a0 | 2018-07-11 22:26:18 -0700 | [diff] [blame] | 525 | xfs_bmap_add_free(ip->i_mount, (*tpp)->t_dfops, |
Christoph Hellwig | 3e0ee78 | 2016-10-20 15:54:31 +1100 | [diff] [blame] | 526 | del.br_startblock, del.br_blockcount, |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 527 | NULL); |
| 528 | |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 529 | /* Roll the transaction */ |
Brian Foster | 27356a0 | 2018-07-11 22:26:18 -0700 | [diff] [blame] | 530 | xfs_defer_ijoin((*tpp)->t_dfops, ip); |
| 531 | error = xfs_defer_finish(tpp, (*tpp)->t_dfops); |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 532 | if (error) { |
Brian Foster | 27356a0 | 2018-07-11 22:26:18 -0700 | [diff] [blame] | 533 | xfs_defer_cancel((*tpp)->t_dfops); |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 534 | break; |
| 535 | } |
| 536 | |
| 537 | /* Remove the mapping from the CoW fork. */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 538 | xfs_bmap_del_extent_cow(ip, &icur, &got, &del); |
Darrick J. Wong | 4b4c132 | 2018-01-19 09:05:48 -0800 | [diff] [blame] | 539 | |
| 540 | /* Remove the quota reservation */ |
| 541 | error = xfs_trans_reserve_quota_nblks(NULL, ip, |
| 542 | -(long)del.br_blockcount, 0, |
| 543 | XFS_QMOPT_RES_REGBLKS); |
| 544 | if (error) |
| 545 | break; |
Darrick J. Wong | 9d40fba | 2017-12-10 18:03:55 -0800 | [diff] [blame] | 546 | } else { |
| 547 | /* Didn't do anything, push cursor back. */ |
| 548 | xfs_iext_prev(ifp, &icur); |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 549 | } |
Christoph Hellwig | 41caabd | 2017-11-03 10:34:44 -0700 | [diff] [blame] | 550 | next_extent: |
| 551 | if (!xfs_iext_get_extent(ifp, &icur, &got)) |
Brian Foster | c17a8ef | 2016-10-24 14:21:08 +1100 | [diff] [blame] | 552 | break; |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Brian Foster | c17a8ef | 2016-10-24 14:21:08 +1100 | [diff] [blame] | 555 | /* clear tag if cow fork is emptied */ |
| 556 | if (!ifp->if_bytes) |
| 557 | xfs_inode_clear_cowblocks_tag(ip); |
Brian Foster | 27356a0 | 2018-07-11 22:26:18 -0700 | [diff] [blame] | 558 | (*tpp)->t_dfops = odfops; |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 559 | return error; |
| 560 | } |
| 561 | |
| 562 | /* |
Christoph Hellwig | 3802a34 | 2017-03-07 16:45:58 -0800 | [diff] [blame] | 563 | * Cancel CoW reservations for some byte range of an inode. |
| 564 | * |
| 565 | * If cancel_real is true this function cancels all COW fork extents for the |
| 566 | * inode; if cancel_real is false, real extents are not cleared. |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 567 | */ |
| 568 | int |
| 569 | xfs_reflink_cancel_cow_range( |
| 570 | struct xfs_inode *ip, |
| 571 | xfs_off_t offset, |
Christoph Hellwig | 3802a34 | 2017-03-07 16:45:58 -0800 | [diff] [blame] | 572 | xfs_off_t count, |
| 573 | bool cancel_real) |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 574 | { |
| 575 | struct xfs_trans *tp; |
| 576 | xfs_fileoff_t offset_fsb; |
| 577 | xfs_fileoff_t end_fsb; |
| 578 | int error; |
| 579 | |
| 580 | trace_xfs_reflink_cancel_cow_range(ip, offset, count); |
Darrick J. Wong | 63646fc | 2016-10-10 16:47:32 +1100 | [diff] [blame] | 581 | ASSERT(xfs_is_reflink_inode(ip)); |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 582 | |
| 583 | offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset); |
| 584 | if (count == NULLFILEOFF) |
| 585 | end_fsb = NULLFILEOFF; |
| 586 | else |
| 587 | end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count); |
| 588 | |
| 589 | /* Start a rolling transaction to remove the mappings */ |
| 590 | error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write, |
Dave Chinner | 4df0f7f | 2018-03-06 17:07:22 -0800 | [diff] [blame] | 591 | 0, 0, XFS_TRANS_NOFS, &tp); |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 592 | if (error) |
| 593 | goto out; |
| 594 | |
| 595 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 596 | xfs_trans_ijoin(tp, ip, 0); |
| 597 | |
| 598 | /* Scrape out the old CoW reservations */ |
Christoph Hellwig | 3802a34 | 2017-03-07 16:45:58 -0800 | [diff] [blame] | 599 | error = xfs_reflink_cancel_cow_blocks(ip, &tp, offset_fsb, end_fsb, |
| 600 | cancel_real); |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 601 | if (error) |
| 602 | goto out_cancel; |
| 603 | |
| 604 | error = xfs_trans_commit(tp); |
| 605 | |
| 606 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 607 | return error; |
| 608 | |
| 609 | out_cancel: |
| 610 | xfs_trans_cancel(tp); |
| 611 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 612 | out: |
| 613 | trace_xfs_reflink_cancel_cow_range_error(ip, error, _RET_IP_); |
| 614 | return error; |
| 615 | } |
| 616 | |
| 617 | /* |
| 618 | * Remap parts of a file's data fork after a successful CoW. |
| 619 | */ |
| 620 | int |
| 621 | xfs_reflink_end_cow( |
| 622 | struct xfs_inode *ip, |
| 623 | xfs_off_t offset, |
| 624 | xfs_off_t count) |
| 625 | { |
Christoph Hellwig | c1112b6 | 2016-10-20 15:54:45 +1100 | [diff] [blame] | 626 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK); |
Christoph Hellwig | 4ab8671 | 2016-11-24 11:39:50 +1100 | [diff] [blame] | 627 | struct xfs_bmbt_irec got, del; |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 628 | struct xfs_trans *tp; |
| 629 | xfs_fileoff_t offset_fsb; |
| 630 | xfs_fileoff_t end_fsb; |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 631 | xfs_fsblock_t firstfsb; |
| 632 | struct xfs_defer_ops dfops; |
Christoph Hellwig | 4ab8671 | 2016-11-24 11:39:50 +1100 | [diff] [blame] | 633 | int error; |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 634 | unsigned int resblks; |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 635 | xfs_filblks_t rlen; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 636 | struct xfs_iext_cursor icur; |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 637 | |
| 638 | trace_xfs_reflink_end_cow(ip, offset, count); |
| 639 | |
Christoph Hellwig | c1112b6 | 2016-10-20 15:54:45 +1100 | [diff] [blame] | 640 | /* No COW extents? That's easy! */ |
| 641 | if (ifp->if_bytes == 0) |
| 642 | return 0; |
| 643 | |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 644 | offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset); |
| 645 | end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count); |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 646 | |
Darrick J. Wong | fe0be23 | 2017-04-12 12:26:07 -0700 | [diff] [blame] | 647 | /* |
| 648 | * Start a rolling transaction to switch the mappings. We're |
| 649 | * unlikely ever to have to remap 16T worth of single-block |
| 650 | * extents, so just cap the worst case extent count to 2^32-1. |
| 651 | * Stick a warning in just in case, and avoid 64-bit division. |
| 652 | */ |
| 653 | BUILD_BUG_ON(MAX_RW_COUNT > UINT_MAX); |
| 654 | if (end_fsb - offset_fsb > UINT_MAX) { |
| 655 | error = -EFSCORRUPTED; |
| 656 | xfs_force_shutdown(ip->i_mount, SHUTDOWN_CORRUPT_INCORE); |
| 657 | ASSERT(0); |
| 658 | goto out; |
| 659 | } |
| 660 | resblks = XFS_NEXTENTADD_SPACE_RES(ip->i_mount, |
| 661 | (unsigned int)(end_fsb - offset_fsb), |
| 662 | XFS_DATA_FORK); |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 663 | error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write, |
Dave Chinner | 4df0f7f | 2018-03-06 17:07:22 -0800 | [diff] [blame] | 664 | resblks, 0, XFS_TRANS_RESERVE | XFS_TRANS_NOFS, &tp); |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 665 | if (error) |
| 666 | goto out; |
| 667 | |
| 668 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 669 | xfs_trans_ijoin(tp, ip, 0); |
| 670 | |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 671 | /* |
| 672 | * In case of racing, overlapping AIO writes no COW extents might be |
| 673 | * left by the time I/O completes for the loser of the race. In that |
| 674 | * case we are done. |
| 675 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 676 | if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got)) |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 677 | goto out_cancel; |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 678 | |
Christoph Hellwig | c1112b6 | 2016-10-20 15:54:45 +1100 | [diff] [blame] | 679 | /* Walk backwards until we're out of the I/O range... */ |
| 680 | while (got.br_startoff + got.br_blockcount > offset_fsb) { |
| 681 | del = got; |
| 682 | xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb); |
| 683 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 684 | /* Extent delete may have bumped ext forward */ |
Christoph Hellwig | df79b81 | 2018-03-13 23:15:33 -0700 | [diff] [blame] | 685 | if (!del.br_blockcount) |
| 686 | goto prev_extent; |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 687 | |
Christoph Hellwig | c1112b6 | 2016-10-20 15:54:45 +1100 | [diff] [blame] | 688 | ASSERT(!isnullstartblock(got.br_startblock)); |
| 689 | |
Darrick J. Wong | 5eda430 | 2017-02-02 15:14:02 -0800 | [diff] [blame] | 690 | /* |
| 691 | * Don't remap unwritten extents; these are |
| 692 | * speculatively preallocated CoW extents that have been |
| 693 | * allocated but have not yet been involved in a write. |
| 694 | */ |
Christoph Hellwig | df79b81 | 2018-03-13 23:15:33 -0700 | [diff] [blame] | 695 | if (got.br_state == XFS_EXT_UNWRITTEN) |
| 696 | goto prev_extent; |
Darrick J. Wong | 5eda430 | 2017-02-02 15:14:02 -0800 | [diff] [blame] | 697 | |
Christoph Hellwig | c1112b6 | 2016-10-20 15:54:45 +1100 | [diff] [blame] | 698 | /* Unmap the old blocks in the data fork. */ |
Brian Foster | bcd2c9f | 2018-07-11 22:26:19 -0700 | [diff] [blame] | 699 | xfs_defer_init(tp, &dfops, &firstfsb); |
Christoph Hellwig | c1112b6 | 2016-10-20 15:54:45 +1100 | [diff] [blame] | 700 | rlen = del.br_blockcount; |
| 701 | error = __xfs_bunmapi(tp, ip, del.br_startoff, &rlen, 0, 1, |
Brian Foster | ccd9d91 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 702 | &firstfsb); |
Christoph Hellwig | c1112b6 | 2016-10-20 15:54:45 +1100 | [diff] [blame] | 703 | if (error) |
| 704 | goto out_defer; |
| 705 | |
| 706 | /* Trim the extent to whatever got unmapped. */ |
| 707 | if (rlen) { |
| 708 | xfs_trim_extent(&del, del.br_startoff + rlen, |
| 709 | del.br_blockcount - rlen); |
| 710 | } |
| 711 | trace_xfs_reflink_cow_remap(ip, &del); |
| 712 | |
| 713 | /* Free the CoW orphan record. */ |
Brian Foster | 4bcfa61 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 714 | error = xfs_refcount_free_cow_extent(tp->t_mountp, tp->t_dfops, |
Christoph Hellwig | c1112b6 | 2016-10-20 15:54:45 +1100 | [diff] [blame] | 715 | del.br_startblock, del.br_blockcount); |
| 716 | if (error) |
| 717 | goto out_defer; |
| 718 | |
| 719 | /* Map the new blocks into the data fork. */ |
Brian Foster | 4bcfa61 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 720 | error = xfs_bmap_map_extent(tp->t_mountp, tp->t_dfops, ip, |
| 721 | &del); |
Christoph Hellwig | c1112b6 | 2016-10-20 15:54:45 +1100 | [diff] [blame] | 722 | if (error) |
| 723 | goto out_defer; |
| 724 | |
Darrick J. Wong | 4b4c132 | 2018-01-19 09:05:48 -0800 | [diff] [blame] | 725 | /* Charge this new data fork mapping to the on-disk quota. */ |
| 726 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_DELBCOUNT, |
| 727 | (long)del.br_blockcount); |
| 728 | |
Christoph Hellwig | c1112b6 | 2016-10-20 15:54:45 +1100 | [diff] [blame] | 729 | /* Remove the mapping from the CoW fork. */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 730 | xfs_bmap_del_extent_cow(ip, &icur, &got, &del); |
Christoph Hellwig | c1112b6 | 2016-10-20 15:54:45 +1100 | [diff] [blame] | 731 | |
Brian Foster | 4bcfa61 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 732 | xfs_defer_ijoin(tp->t_dfops, ip); |
| 733 | error = xfs_defer_finish(&tp, tp->t_dfops); |
Christoph Hellwig | c1112b6 | 2016-10-20 15:54:45 +1100 | [diff] [blame] | 734 | if (error) |
| 735 | goto out_defer; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 736 | if (!xfs_iext_get_extent(ifp, &icur, &got)) |
Christoph Hellwig | c1112b6 | 2016-10-20 15:54:45 +1100 | [diff] [blame] | 737 | break; |
Christoph Hellwig | df79b81 | 2018-03-13 23:15:33 -0700 | [diff] [blame] | 738 | continue; |
| 739 | prev_extent: |
| 740 | if (!xfs_iext_prev_extent(ifp, &icur, &got)) |
| 741 | break; |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | error = xfs_trans_commit(tp); |
| 745 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 746 | if (error) |
| 747 | goto out; |
| 748 | return 0; |
| 749 | |
| 750 | out_defer: |
Brian Foster | 4bcfa61 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 751 | xfs_defer_cancel(tp->t_dfops); |
Christoph Hellwig | e12199f | 2017-10-03 08:58:33 -0700 | [diff] [blame] | 752 | out_cancel: |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 753 | xfs_trans_cancel(tp); |
| 754 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 755 | out: |
| 756 | trace_xfs_reflink_end_cow_error(ip, error, _RET_IP_); |
| 757 | return error; |
| 758 | } |
Darrick J. Wong | 174edb0 | 2016-10-03 09:11:39 -0700 | [diff] [blame] | 759 | |
| 760 | /* |
| 761 | * Free leftover CoW reservations that didn't get cleaned out. |
| 762 | */ |
| 763 | int |
| 764 | xfs_reflink_recover_cow( |
| 765 | struct xfs_mount *mp) |
| 766 | { |
| 767 | xfs_agnumber_t agno; |
| 768 | int error = 0; |
| 769 | |
| 770 | if (!xfs_sb_version_hasreflink(&mp->m_sb)) |
| 771 | return 0; |
| 772 | |
| 773 | for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) { |
| 774 | error = xfs_refcount_recover_cow_leftovers(mp, agno); |
| 775 | if (error) |
| 776 | break; |
| 777 | } |
| 778 | |
| 779 | return error; |
| 780 | } |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 781 | |
| 782 | /* |
| 783 | * Reflinking (Block) Ranges of Two Files Together |
| 784 | * |
| 785 | * First, ensure that the reflink flag is set on both inodes. The flag is an |
| 786 | * optimization to avoid unnecessary refcount btree lookups in the write path. |
| 787 | * |
| 788 | * Now we can iteratively remap the range of extents (and holes) in src to the |
| 789 | * corresponding ranges in dest. Let drange and srange denote the ranges of |
| 790 | * logical blocks in dest and src touched by the reflink operation. |
| 791 | * |
| 792 | * While the length of drange is greater than zero, |
| 793 | * - Read src's bmbt at the start of srange ("imap") |
| 794 | * - If imap doesn't exist, make imap appear to start at the end of srange |
| 795 | * with zero length. |
| 796 | * - If imap starts before srange, advance imap to start at srange. |
| 797 | * - If imap goes beyond srange, truncate imap to end at the end of srange. |
| 798 | * - Punch (imap start - srange start + imap len) blocks from dest at |
| 799 | * offset (drange start). |
| 800 | * - If imap points to a real range of pblks, |
| 801 | * > Increase the refcount of the imap's pblks |
| 802 | * > Map imap's pblks into dest at the offset |
| 803 | * (drange start + imap start - srange start) |
| 804 | * - Advance drange and srange by (imap start - srange start + imap len) |
| 805 | * |
| 806 | * Finally, if the reflink made dest longer, update both the in-core and |
| 807 | * on-disk file sizes. |
| 808 | * |
| 809 | * ASCII Art Demonstration: |
| 810 | * |
| 811 | * Let's say we want to reflink this source file: |
| 812 | * |
| 813 | * ----SSSSSSS-SSSSS----SSSSSS (src file) |
| 814 | * <--------------------> |
| 815 | * |
| 816 | * into this destination file: |
| 817 | * |
| 818 | * --DDDDDDDDDDDDDDDDDDD--DDD (dest file) |
| 819 | * <--------------------> |
| 820 | * '-' means a hole, and 'S' and 'D' are written blocks in the src and dest. |
| 821 | * Observe that the range has different logical offsets in either file. |
| 822 | * |
| 823 | * Consider that the first extent in the source file doesn't line up with our |
| 824 | * reflink range. Unmapping and remapping are separate operations, so we can |
| 825 | * unmap more blocks from the destination file than we remap. |
| 826 | * |
| 827 | * ----SSSSSSS-SSSSS----SSSSSS |
| 828 | * <-------> |
| 829 | * --DDDDD---------DDDDD--DDD |
| 830 | * <-------> |
| 831 | * |
| 832 | * Now remap the source extent into the destination file: |
| 833 | * |
| 834 | * ----SSSSSSS-SSSSS----SSSSSS |
| 835 | * <-------> |
| 836 | * --DDDDD--SSSSSSSDDDDD--DDD |
| 837 | * <-------> |
| 838 | * |
| 839 | * Do likewise with the second hole and extent in our range. Holes in the |
| 840 | * unmap range don't affect our operation. |
| 841 | * |
| 842 | * ----SSSSSSS-SSSSS----SSSSSS |
| 843 | * <----> |
| 844 | * --DDDDD--SSSSSSS-SSSSS-DDD |
| 845 | * <----> |
| 846 | * |
| 847 | * Finally, unmap and remap part of the third extent. This will increase the |
| 848 | * size of the destination file. |
| 849 | * |
| 850 | * ----SSSSSSS-SSSSS----SSSSSS |
| 851 | * <-----> |
| 852 | * --DDDDD--SSSSSSS-SSSSS----SSS |
| 853 | * <-----> |
| 854 | * |
| 855 | * Once we update the destination file's i_size, we're done. |
| 856 | */ |
| 857 | |
| 858 | /* |
| 859 | * Ensure the reflink bit is set in both inodes. |
| 860 | */ |
| 861 | STATIC int |
| 862 | xfs_reflink_set_inode_flag( |
| 863 | struct xfs_inode *src, |
| 864 | struct xfs_inode *dest) |
| 865 | { |
| 866 | struct xfs_mount *mp = src->i_mount; |
| 867 | int error; |
| 868 | struct xfs_trans *tp; |
| 869 | |
| 870 | if (xfs_is_reflink_inode(src) && xfs_is_reflink_inode(dest)) |
| 871 | return 0; |
| 872 | |
| 873 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp); |
| 874 | if (error) |
| 875 | goto out_error; |
| 876 | |
| 877 | /* Lock both files against IO */ |
| 878 | if (src->i_ino == dest->i_ino) |
| 879 | xfs_ilock(src, XFS_ILOCK_EXCL); |
| 880 | else |
Darrick J. Wong | 7c2d238 | 2018-01-26 15:27:33 -0800 | [diff] [blame] | 881 | xfs_lock_two_inodes(src, XFS_ILOCK_EXCL, dest, XFS_ILOCK_EXCL); |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 882 | |
| 883 | if (!xfs_is_reflink_inode(src)) { |
| 884 | trace_xfs_reflink_set_inode_flag(src); |
| 885 | xfs_trans_ijoin(tp, src, XFS_ILOCK_EXCL); |
| 886 | src->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK; |
| 887 | xfs_trans_log_inode(tp, src, XFS_ILOG_CORE); |
| 888 | xfs_ifork_init_cow(src); |
| 889 | } else |
| 890 | xfs_iunlock(src, XFS_ILOCK_EXCL); |
| 891 | |
| 892 | if (src->i_ino == dest->i_ino) |
| 893 | goto commit_flags; |
| 894 | |
| 895 | if (!xfs_is_reflink_inode(dest)) { |
| 896 | trace_xfs_reflink_set_inode_flag(dest); |
| 897 | xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL); |
| 898 | dest->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK; |
| 899 | xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE); |
| 900 | xfs_ifork_init_cow(dest); |
| 901 | } else |
| 902 | xfs_iunlock(dest, XFS_ILOCK_EXCL); |
| 903 | |
| 904 | commit_flags: |
| 905 | error = xfs_trans_commit(tp); |
| 906 | if (error) |
| 907 | goto out_error; |
| 908 | return error; |
| 909 | |
| 910 | out_error: |
| 911 | trace_xfs_reflink_set_inode_flag_error(dest, error, _RET_IP_); |
| 912 | return error; |
| 913 | } |
| 914 | |
| 915 | /* |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 916 | * Update destination inode size & cowextsize hint, if necessary. |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 917 | */ |
| 918 | STATIC int |
| 919 | xfs_reflink_update_dest( |
| 920 | struct xfs_inode *dest, |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 921 | xfs_off_t newlen, |
Christoph Hellwig | c5ecb42 | 2017-02-06 17:45:51 -0800 | [diff] [blame] | 922 | xfs_extlen_t cowextsize, |
| 923 | bool is_dedupe) |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 924 | { |
| 925 | struct xfs_mount *mp = dest->i_mount; |
| 926 | struct xfs_trans *tp; |
| 927 | int error; |
| 928 | |
Christoph Hellwig | c5ecb42 | 2017-02-06 17:45:51 -0800 | [diff] [blame] | 929 | if (is_dedupe && newlen <= i_size_read(VFS_I(dest)) && cowextsize == 0) |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 930 | return 0; |
| 931 | |
| 932 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp); |
| 933 | if (error) |
| 934 | goto out_error; |
| 935 | |
| 936 | xfs_ilock(dest, XFS_ILOCK_EXCL); |
| 937 | xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL); |
| 938 | |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 939 | if (newlen > i_size_read(VFS_I(dest))) { |
| 940 | trace_xfs_reflink_update_inode_size(dest, newlen); |
| 941 | i_size_write(VFS_I(dest), newlen); |
| 942 | dest->i_d.di_size = newlen; |
| 943 | } |
| 944 | |
| 945 | if (cowextsize) { |
| 946 | dest->i_d.di_cowextsize = cowextsize; |
| 947 | dest->i_d.di_flags2 |= XFS_DIFLAG2_COWEXTSIZE; |
| 948 | } |
| 949 | |
Christoph Hellwig | c5ecb42 | 2017-02-06 17:45:51 -0800 | [diff] [blame] | 950 | if (!is_dedupe) { |
| 951 | xfs_trans_ichgtime(tp, dest, |
| 952 | XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
| 953 | } |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 954 | xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE); |
| 955 | |
| 956 | error = xfs_trans_commit(tp); |
| 957 | if (error) |
| 958 | goto out_error; |
| 959 | return error; |
| 960 | |
| 961 | out_error: |
| 962 | trace_xfs_reflink_update_inode_size_error(dest, error, _RET_IP_); |
| 963 | return error; |
| 964 | } |
| 965 | |
| 966 | /* |
Darrick J. Wong | 6fa164b | 2016-10-03 09:11:45 -0700 | [diff] [blame] | 967 | * Do we have enough reserve in this AG to handle a reflink? The refcount |
| 968 | * btree already reserved all the space it needs, but the rmap btree can grow |
| 969 | * infinitely, so we won't allow more reflinks when the AG is down to the |
| 970 | * btree reserves. |
| 971 | */ |
| 972 | static int |
| 973 | xfs_reflink_ag_has_free_space( |
| 974 | struct xfs_mount *mp, |
| 975 | xfs_agnumber_t agno) |
| 976 | { |
| 977 | struct xfs_perag *pag; |
| 978 | int error = 0; |
| 979 | |
| 980 | if (!xfs_sb_version_hasrmapbt(&mp->m_sb)) |
| 981 | return 0; |
| 982 | |
| 983 | pag = xfs_perag_get(mp, agno); |
Brian Foster | 2159286 | 2018-03-09 14:01:59 -0800 | [diff] [blame] | 984 | if (xfs_ag_resv_critical(pag, XFS_AG_RESV_RMAPBT) || |
Darrick J. Wong | 6fa164b | 2016-10-03 09:11:45 -0700 | [diff] [blame] | 985 | xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA)) |
| 986 | error = -ENOSPC; |
| 987 | xfs_perag_put(pag); |
| 988 | return error; |
| 989 | } |
| 990 | |
| 991 | /* |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 992 | * Unmap a range of blocks from a file, then map other blocks into the hole. |
| 993 | * The range to unmap is (destoff : destoff + srcioff + irec->br_blockcount). |
| 994 | * The extent irec is mapped into dest at irec->br_startoff. |
| 995 | */ |
| 996 | STATIC int |
| 997 | xfs_reflink_remap_extent( |
| 998 | struct xfs_inode *ip, |
| 999 | struct xfs_bmbt_irec *irec, |
| 1000 | xfs_fileoff_t destoff, |
| 1001 | xfs_off_t new_isize) |
| 1002 | { |
| 1003 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | 9c4f29d | 2017-03-28 14:53:35 -0700 | [diff] [blame] | 1004 | bool real_extent = xfs_bmap_is_real_extent(irec); |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1005 | struct xfs_trans *tp; |
| 1006 | xfs_fsblock_t firstfsb; |
| 1007 | unsigned int resblks; |
| 1008 | struct xfs_defer_ops dfops; |
| 1009 | struct xfs_bmbt_irec uirec; |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1010 | xfs_filblks_t rlen; |
| 1011 | xfs_filblks_t unmap_len; |
| 1012 | xfs_off_t newlen; |
| 1013 | int error; |
| 1014 | |
| 1015 | unmap_len = irec->br_startoff + irec->br_blockcount - destoff; |
| 1016 | trace_xfs_reflink_punch_range(ip, destoff, unmap_len); |
| 1017 | |
Darrick J. Wong | 6fa164b | 2016-10-03 09:11:45 -0700 | [diff] [blame] | 1018 | /* No reflinking if we're low on space */ |
| 1019 | if (real_extent) { |
| 1020 | error = xfs_reflink_ag_has_free_space(mp, |
| 1021 | XFS_FSB_TO_AGNO(mp, irec->br_startblock)); |
| 1022 | if (error) |
| 1023 | goto out; |
| 1024 | } |
| 1025 | |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1026 | /* Start a rolling transaction to switch the mappings */ |
| 1027 | resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK); |
| 1028 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp); |
| 1029 | if (error) |
| 1030 | goto out; |
| 1031 | |
| 1032 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1033 | xfs_trans_ijoin(tp, ip, 0); |
| 1034 | |
| 1035 | /* If we're not just clearing space, then do we have enough quota? */ |
| 1036 | if (real_extent) { |
| 1037 | error = xfs_trans_reserve_quota_nblks(tp, ip, |
| 1038 | irec->br_blockcount, 0, XFS_QMOPT_RES_REGBLKS); |
| 1039 | if (error) |
| 1040 | goto out_cancel; |
| 1041 | } |
| 1042 | |
| 1043 | trace_xfs_reflink_remap(ip, irec->br_startoff, |
| 1044 | irec->br_blockcount, irec->br_startblock); |
| 1045 | |
| 1046 | /* Unmap the old blocks in the data fork. */ |
| 1047 | rlen = unmap_len; |
| 1048 | while (rlen) { |
Brian Foster | bcd2c9f | 2018-07-11 22:26:19 -0700 | [diff] [blame] | 1049 | xfs_defer_init(tp, &dfops, &firstfsb); |
Brian Foster | ccd9d91 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 1050 | error = __xfs_bunmapi(tp, ip, destoff, &rlen, 0, 1, &firstfsb); |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1051 | if (error) |
| 1052 | goto out_defer; |
| 1053 | |
| 1054 | /* |
| 1055 | * Trim the extent to whatever got unmapped. |
| 1056 | * Remember, bunmapi works backwards. |
| 1057 | */ |
| 1058 | uirec.br_startblock = irec->br_startblock + rlen; |
| 1059 | uirec.br_startoff = irec->br_startoff + rlen; |
| 1060 | uirec.br_blockcount = unmap_len - rlen; |
| 1061 | unmap_len = rlen; |
| 1062 | |
| 1063 | /* If this isn't a real mapping, we're done. */ |
| 1064 | if (!real_extent || uirec.br_blockcount == 0) |
| 1065 | goto next_extent; |
| 1066 | |
| 1067 | trace_xfs_reflink_remap(ip, uirec.br_startoff, |
| 1068 | uirec.br_blockcount, uirec.br_startblock); |
| 1069 | |
| 1070 | /* Update the refcount tree */ |
Brian Foster | 4bcfa61 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 1071 | error = xfs_refcount_increase_extent(mp, tp->t_dfops, &uirec); |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1072 | if (error) |
| 1073 | goto out_defer; |
| 1074 | |
| 1075 | /* Map the new blocks into the data fork. */ |
Brian Foster | 4bcfa61 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 1076 | error = xfs_bmap_map_extent(mp, tp->t_dfops, ip, &uirec); |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1077 | if (error) |
| 1078 | goto out_defer; |
| 1079 | |
| 1080 | /* Update quota accounting. */ |
| 1081 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, |
| 1082 | uirec.br_blockcount); |
| 1083 | |
| 1084 | /* Update dest isize if needed. */ |
| 1085 | newlen = XFS_FSB_TO_B(mp, |
| 1086 | uirec.br_startoff + uirec.br_blockcount); |
| 1087 | newlen = min_t(xfs_off_t, newlen, new_isize); |
| 1088 | if (newlen > i_size_read(VFS_I(ip))) { |
| 1089 | trace_xfs_reflink_update_inode_size(ip, newlen); |
| 1090 | i_size_write(VFS_I(ip), newlen); |
| 1091 | ip->i_d.di_size = newlen; |
| 1092 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1093 | } |
| 1094 | |
| 1095 | next_extent: |
| 1096 | /* Process all the deferred stuff. */ |
Brian Foster | 4bcfa61 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 1097 | xfs_defer_ijoin(tp->t_dfops, ip); |
| 1098 | error = xfs_defer_finish(&tp, tp->t_dfops); |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1099 | if (error) |
| 1100 | goto out_defer; |
| 1101 | } |
| 1102 | |
| 1103 | error = xfs_trans_commit(tp); |
| 1104 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1105 | if (error) |
| 1106 | goto out; |
| 1107 | return 0; |
| 1108 | |
| 1109 | out_defer: |
Brian Foster | 4bcfa61 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 1110 | xfs_defer_cancel(tp->t_dfops); |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1111 | out_cancel: |
| 1112 | xfs_trans_cancel(tp); |
| 1113 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1114 | out: |
| 1115 | trace_xfs_reflink_remap_extent_error(ip, error, _RET_IP_); |
| 1116 | return error; |
| 1117 | } |
| 1118 | |
| 1119 | /* |
| 1120 | * Iteratively remap one file's extents (and holes) to another's. |
| 1121 | */ |
| 1122 | STATIC int |
| 1123 | xfs_reflink_remap_blocks( |
| 1124 | struct xfs_inode *src, |
| 1125 | xfs_fileoff_t srcoff, |
| 1126 | struct xfs_inode *dest, |
| 1127 | xfs_fileoff_t destoff, |
| 1128 | xfs_filblks_t len, |
| 1129 | xfs_off_t new_isize) |
| 1130 | { |
| 1131 | struct xfs_bmbt_irec imap; |
| 1132 | int nimaps; |
| 1133 | int error = 0; |
| 1134 | xfs_filblks_t range_len; |
| 1135 | |
| 1136 | /* drange = (destoff, destoff + len); srange = (srcoff, srcoff + len) */ |
| 1137 | while (len) { |
Darrick J. Wong | 01c2e13 | 2018-01-18 14:07:53 -0800 | [diff] [blame] | 1138 | uint lock_mode; |
| 1139 | |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1140 | trace_xfs_reflink_remap_blocks_loop(src, srcoff, len, |
| 1141 | dest, destoff); |
Darrick J. Wong | 01c2e13 | 2018-01-18 14:07:53 -0800 | [diff] [blame] | 1142 | |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1143 | /* Read extent from the source file */ |
| 1144 | nimaps = 1; |
Darrick J. Wong | 01c2e13 | 2018-01-18 14:07:53 -0800 | [diff] [blame] | 1145 | lock_mode = xfs_ilock_data_map_shared(src); |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1146 | error = xfs_bmapi_read(src, srcoff, len, &imap, &nimaps, 0); |
Darrick J. Wong | 01c2e13 | 2018-01-18 14:07:53 -0800 | [diff] [blame] | 1147 | xfs_iunlock(src, lock_mode); |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1148 | if (error) |
| 1149 | goto err; |
| 1150 | ASSERT(nimaps == 1); |
| 1151 | |
| 1152 | trace_xfs_reflink_remap_imap(src, srcoff, len, XFS_IO_OVERWRITE, |
| 1153 | &imap); |
| 1154 | |
| 1155 | /* Translate imap into the destination file. */ |
| 1156 | range_len = imap.br_startoff + imap.br_blockcount - srcoff; |
| 1157 | imap.br_startoff += destoff - srcoff; |
| 1158 | |
| 1159 | /* Clear dest from destoff to the end of imap and map it in. */ |
| 1160 | error = xfs_reflink_remap_extent(dest, &imap, destoff, |
| 1161 | new_isize); |
| 1162 | if (error) |
| 1163 | goto err; |
| 1164 | |
| 1165 | if (fatal_signal_pending(current)) { |
| 1166 | error = -EINTR; |
| 1167 | goto err; |
| 1168 | } |
| 1169 | |
| 1170 | /* Advance drange/srange */ |
| 1171 | srcoff += range_len; |
| 1172 | destoff += range_len; |
| 1173 | len -= range_len; |
| 1174 | } |
| 1175 | |
| 1176 | return 0; |
| 1177 | |
| 1178 | err: |
| 1179 | trace_xfs_reflink_remap_blocks_error(dest, error, _RET_IP_); |
| 1180 | return error; |
| 1181 | } |
| 1182 | |
| 1183 | /* |
Darrick J. Wong | 1364b1d4 | 2018-01-18 13:55:20 -0800 | [diff] [blame] | 1184 | * Grab the exclusive iolock for a data copy from src to dest, making |
| 1185 | * sure to abide vfs locking order (lowest pointer value goes first) and |
| 1186 | * breaking the pnfs layout leases on dest before proceeding. The loop |
| 1187 | * is needed because we cannot call the blocking break_layout() with the |
| 1188 | * src iolock held, and therefore have to back out both locks. |
| 1189 | */ |
| 1190 | static int |
| 1191 | xfs_iolock_two_inodes_and_break_layout( |
| 1192 | struct inode *src, |
| 1193 | struct inode *dest) |
| 1194 | { |
| 1195 | int error; |
| 1196 | |
| 1197 | retry: |
| 1198 | if (src < dest) { |
Darrick J. Wong | 01c2e13 | 2018-01-18 14:07:53 -0800 | [diff] [blame] | 1199 | inode_lock_shared(src); |
Darrick J. Wong | 1364b1d4 | 2018-01-18 13:55:20 -0800 | [diff] [blame] | 1200 | inode_lock_nested(dest, I_MUTEX_NONDIR2); |
| 1201 | } else { |
| 1202 | /* src >= dest */ |
| 1203 | inode_lock(dest); |
| 1204 | } |
| 1205 | |
| 1206 | error = break_layout(dest, false); |
| 1207 | if (error == -EWOULDBLOCK) { |
| 1208 | inode_unlock(dest); |
| 1209 | if (src < dest) |
Darrick J. Wong | 01c2e13 | 2018-01-18 14:07:53 -0800 | [diff] [blame] | 1210 | inode_unlock_shared(src); |
Darrick J. Wong | 1364b1d4 | 2018-01-18 13:55:20 -0800 | [diff] [blame] | 1211 | error = break_layout(dest, true); |
| 1212 | if (error) |
| 1213 | return error; |
| 1214 | goto retry; |
| 1215 | } |
| 1216 | if (error) { |
| 1217 | inode_unlock(dest); |
| 1218 | if (src < dest) |
Darrick J. Wong | 01c2e13 | 2018-01-18 14:07:53 -0800 | [diff] [blame] | 1219 | inode_unlock_shared(src); |
Darrick J. Wong | 1364b1d4 | 2018-01-18 13:55:20 -0800 | [diff] [blame] | 1220 | return error; |
| 1221 | } |
| 1222 | if (src > dest) |
Darrick J. Wong | 01c2e13 | 2018-01-18 14:07:53 -0800 | [diff] [blame] | 1223 | inode_lock_shared_nested(src, I_MUTEX_NONDIR2); |
Darrick J. Wong | 1364b1d4 | 2018-01-18 13:55:20 -0800 | [diff] [blame] | 1224 | return 0; |
| 1225 | } |
| 1226 | |
| 1227 | /* |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1228 | * Link a range of blocks from one file to another. |
| 1229 | */ |
| 1230 | int |
| 1231 | xfs_reflink_remap_range( |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1232 | struct file *file_in, |
| 1233 | loff_t pos_in, |
| 1234 | struct file *file_out, |
| 1235 | loff_t pos_out, |
| 1236 | u64 len, |
| 1237 | bool is_dedupe) |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1238 | { |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1239 | struct inode *inode_in = file_inode(file_in); |
| 1240 | struct xfs_inode *src = XFS_I(inode_in); |
| 1241 | struct inode *inode_out = file_inode(file_out); |
| 1242 | struct xfs_inode *dest = XFS_I(inode_out); |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1243 | struct xfs_mount *mp = src->i_mount; |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1244 | bool same_inode = (inode_in == inode_out); |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1245 | xfs_fileoff_t sfsbno, dfsbno; |
| 1246 | xfs_filblks_t fsblen; |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1247 | xfs_extlen_t cowextsize; |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1248 | ssize_t ret; |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1249 | |
| 1250 | if (!xfs_sb_version_hasreflink(&mp->m_sb)) |
| 1251 | return -EOPNOTSUPP; |
| 1252 | |
| 1253 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 1254 | return -EIO; |
| 1255 | |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1256 | /* Lock both files against IO */ |
Darrick J. Wong | 1364b1d4 | 2018-01-18 13:55:20 -0800 | [diff] [blame] | 1257 | ret = xfs_iolock_two_inodes_and_break_layout(inode_in, inode_out); |
| 1258 | if (ret) |
| 1259 | return ret; |
Christoph Hellwig | 6552321 | 2016-11-30 14:33:25 +1100 | [diff] [blame] | 1260 | if (same_inode) |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1261 | xfs_ilock(src, XFS_MMAPLOCK_EXCL); |
Christoph Hellwig | 6552321 | 2016-11-30 14:33:25 +1100 | [diff] [blame] | 1262 | else |
Darrick J. Wong | 01c2e13 | 2018-01-18 14:07:53 -0800 | [diff] [blame] | 1263 | xfs_lock_two_inodes(src, XFS_MMAPLOCK_SHARED, dest, |
Darrick J. Wong | 7c2d238 | 2018-01-26 15:27:33 -0800 | [diff] [blame] | 1264 | XFS_MMAPLOCK_EXCL); |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1265 | |
Darrick J. Wong | 876bec6f | 2016-12-09 16:18:30 -0800 | [diff] [blame] | 1266 | /* Check file eligibility and prepare for block sharing. */ |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1267 | ret = -EINVAL; |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1268 | /* Don't reflink realtime inodes */ |
| 1269 | if (XFS_IS_REALTIME_INODE(src) || XFS_IS_REALTIME_INODE(dest)) |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1270 | goto out_unlock; |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1271 | |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1272 | /* Don't share DAX file data for now. */ |
| 1273 | if (IS_DAX(inode_in) || IS_DAX(inode_out)) |
| 1274 | goto out_unlock; |
Darrick J. Wong | cc71466 | 2016-10-03 09:11:41 -0700 | [diff] [blame] | 1275 | |
Darrick J. Wong | 876bec6f | 2016-12-09 16:18:30 -0800 | [diff] [blame] | 1276 | ret = vfs_clone_file_prep_inodes(inode_in, pos_in, inode_out, pos_out, |
| 1277 | &len, is_dedupe); |
Darrick J. Wong | 22725ce | 2016-12-19 15:13:26 -0800 | [diff] [blame] | 1278 | if (ret <= 0) |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1279 | goto out_unlock; |
| 1280 | |
Darrick J. Wong | 09ac862 | 2018-01-19 08:56:04 -0800 | [diff] [blame] | 1281 | /* Attach dquots to dest inode before changing block map */ |
Darrick J. Wong | c14cfcc | 2018-05-04 15:30:21 -0700 | [diff] [blame] | 1282 | ret = xfs_qm_dqattach(dest); |
Darrick J. Wong | 09ac862 | 2018-01-19 08:56:04 -0800 | [diff] [blame] | 1283 | if (ret) |
| 1284 | goto out_unlock; |
| 1285 | |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1286 | trace_xfs_reflink_remap_range(src, pos_in, len, dest, pos_out); |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1287 | |
Darrick J. Wong | 5c989a0 | 2017-12-10 18:03:54 -0800 | [diff] [blame] | 1288 | /* |
| 1289 | * Clear out post-eof preallocations because we don't have page cache |
| 1290 | * backing the delayed allocations and they'll never get freed on |
| 1291 | * their own. |
| 1292 | */ |
| 1293 | if (xfs_can_free_eofblocks(dest, true)) { |
| 1294 | ret = xfs_free_eofblocks(dest); |
| 1295 | if (ret) |
| 1296 | goto out_unlock; |
| 1297 | } |
| 1298 | |
Darrick J. Wong | 876bec6f | 2016-12-09 16:18:30 -0800 | [diff] [blame] | 1299 | /* Set flags and remap blocks. */ |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1300 | ret = xfs_reflink_set_inode_flag(src, dest); |
| 1301 | if (ret) |
| 1302 | goto out_unlock; |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1303 | |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1304 | dfsbno = XFS_B_TO_FSBT(mp, pos_out); |
| 1305 | sfsbno = XFS_B_TO_FSBT(mp, pos_in); |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1306 | fsblen = XFS_B_TO_FSB(mp, len); |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1307 | ret = xfs_reflink_remap_blocks(src, sfsbno, dest, dfsbno, fsblen, |
| 1308 | pos_out + len); |
| 1309 | if (ret) |
| 1310 | goto out_unlock; |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1311 | |
Darrick J. Wong | 876bec6f | 2016-12-09 16:18:30 -0800 | [diff] [blame] | 1312 | /* Zap any page cache for the destination file's range. */ |
| 1313 | truncate_inode_pages_range(&inode_out->i_data, pos_out, |
| 1314 | PAGE_ALIGN(pos_out + len) - 1); |
| 1315 | |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1316 | /* |
| 1317 | * Carry the cowextsize hint from src to dest if we're sharing the |
| 1318 | * entire source file to the entire destination file, the source file |
| 1319 | * has a cowextsize hint, and the destination file does not. |
| 1320 | */ |
| 1321 | cowextsize = 0; |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1322 | if (pos_in == 0 && len == i_size_read(inode_in) && |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1323 | (src->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) && |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1324 | pos_out == 0 && len >= i_size_read(inode_out) && |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1325 | !(dest->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE)) |
| 1326 | cowextsize = src->i_d.di_cowextsize; |
| 1327 | |
Christoph Hellwig | c5ecb42 | 2017-02-06 17:45:51 -0800 | [diff] [blame] | 1328 | ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize, |
| 1329 | is_dedupe); |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1330 | |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1331 | out_unlock: |
Darrick J. Wong | 01c2e13 | 2018-01-18 14:07:53 -0800 | [diff] [blame] | 1332 | xfs_iunlock(dest, XFS_MMAPLOCK_EXCL); |
Christoph Hellwig | 6552321 | 2016-11-30 14:33:25 +1100 | [diff] [blame] | 1333 | if (!same_inode) |
Darrick J. Wong | 01c2e13 | 2018-01-18 14:07:53 -0800 | [diff] [blame] | 1334 | xfs_iunlock(src, XFS_MMAPLOCK_SHARED); |
| 1335 | inode_unlock(inode_out); |
| 1336 | if (!same_inode) |
| 1337 | inode_unlock_shared(inode_in); |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1338 | if (ret) |
| 1339 | trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_); |
| 1340 | return ret; |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1341 | } |
Darrick J. Wong | 98cc2db | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1342 | |
| 1343 | /* |
| 1344 | * The user wants to preemptively CoW all shared blocks in this file, |
| 1345 | * which enables us to turn off the reflink flag. Iterate all |
| 1346 | * extents which are not prealloc/delalloc to see which ranges are |
| 1347 | * mentioned in the refcount tree, then read those blocks into the |
| 1348 | * pagecache, dirty them, fsync them back out, and then we can update |
| 1349 | * the inode flag. What happens if we run out of memory? :) |
| 1350 | */ |
| 1351 | STATIC int |
| 1352 | xfs_reflink_dirty_extents( |
| 1353 | struct xfs_inode *ip, |
| 1354 | xfs_fileoff_t fbno, |
| 1355 | xfs_filblks_t end, |
| 1356 | xfs_off_t isize) |
| 1357 | { |
| 1358 | struct xfs_mount *mp = ip->i_mount; |
| 1359 | xfs_agnumber_t agno; |
| 1360 | xfs_agblock_t agbno; |
| 1361 | xfs_extlen_t aglen; |
| 1362 | xfs_agblock_t rbno; |
| 1363 | xfs_extlen_t rlen; |
| 1364 | xfs_off_t fpos; |
| 1365 | xfs_off_t flen; |
| 1366 | struct xfs_bmbt_irec map[2]; |
| 1367 | int nmaps; |
Darrick J. Wong | 9780643c | 2016-10-10 16:49:18 +1100 | [diff] [blame] | 1368 | int error = 0; |
Darrick J. Wong | 98cc2db | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1369 | |
| 1370 | while (end - fbno > 0) { |
| 1371 | nmaps = 1; |
| 1372 | /* |
| 1373 | * Look for extents in the file. Skip holes, delalloc, or |
| 1374 | * unwritten extents; they can't be reflinked. |
| 1375 | */ |
| 1376 | error = xfs_bmapi_read(ip, fbno, end - fbno, map, &nmaps, 0); |
| 1377 | if (error) |
| 1378 | goto out; |
| 1379 | if (nmaps == 0) |
| 1380 | break; |
Christoph Hellwig | 9c4f29d | 2017-03-28 14:53:35 -0700 | [diff] [blame] | 1381 | if (!xfs_bmap_is_real_extent(&map[0])) |
Darrick J. Wong | 98cc2db | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1382 | goto next; |
| 1383 | |
| 1384 | map[1] = map[0]; |
| 1385 | while (map[1].br_blockcount) { |
| 1386 | agno = XFS_FSB_TO_AGNO(mp, map[1].br_startblock); |
| 1387 | agbno = XFS_FSB_TO_AGBNO(mp, map[1].br_startblock); |
| 1388 | aglen = map[1].br_blockcount; |
| 1389 | |
Darrick J. Wong | 92ff728 | 2017-06-16 11:00:10 -0700 | [diff] [blame] | 1390 | error = xfs_reflink_find_shared(mp, NULL, agno, agbno, |
| 1391 | aglen, &rbno, &rlen, true); |
Darrick J. Wong | 98cc2db | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1392 | if (error) |
| 1393 | goto out; |
| 1394 | if (rbno == NULLAGBLOCK) |
| 1395 | break; |
| 1396 | |
| 1397 | /* Dirty the pages */ |
| 1398 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1399 | fpos = XFS_FSB_TO_B(mp, map[1].br_startoff + |
| 1400 | (rbno - agbno)); |
| 1401 | flen = XFS_FSB_TO_B(mp, rlen); |
| 1402 | if (fpos + flen > isize) |
| 1403 | flen = isize - fpos; |
| 1404 | error = iomap_file_dirty(VFS_I(ip), fpos, flen, |
| 1405 | &xfs_iomap_ops); |
| 1406 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1407 | if (error) |
| 1408 | goto out; |
| 1409 | |
| 1410 | map[1].br_blockcount -= (rbno - agbno + rlen); |
| 1411 | map[1].br_startoff += (rbno - agbno + rlen); |
| 1412 | map[1].br_startblock += (rbno - agbno + rlen); |
| 1413 | } |
| 1414 | |
| 1415 | next: |
| 1416 | fbno = map[0].br_startoff + map[0].br_blockcount; |
| 1417 | } |
| 1418 | out: |
| 1419 | return error; |
| 1420 | } |
| 1421 | |
Darrick J. Wong | ea7cdd7 | 2017-06-16 11:00:11 -0700 | [diff] [blame] | 1422 | /* Does this inode need the reflink flag? */ |
| 1423 | int |
| 1424 | xfs_reflink_inode_has_shared_extents( |
| 1425 | struct xfs_trans *tp, |
| 1426 | struct xfs_inode *ip, |
| 1427 | bool *has_shared) |
| 1428 | { |
| 1429 | struct xfs_bmbt_irec got; |
| 1430 | struct xfs_mount *mp = ip->i_mount; |
| 1431 | struct xfs_ifork *ifp; |
| 1432 | xfs_agnumber_t agno; |
| 1433 | xfs_agblock_t agbno; |
| 1434 | xfs_extlen_t aglen; |
| 1435 | xfs_agblock_t rbno; |
| 1436 | xfs_extlen_t rlen; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1437 | struct xfs_iext_cursor icur; |
Darrick J. Wong | ea7cdd7 | 2017-06-16 11:00:11 -0700 | [diff] [blame] | 1438 | bool found; |
| 1439 | int error; |
| 1440 | |
| 1441 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 1442 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1443 | error = xfs_iread_extents(tp, ip, XFS_DATA_FORK); |
| 1444 | if (error) |
| 1445 | return error; |
| 1446 | } |
| 1447 | |
| 1448 | *has_shared = false; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1449 | found = xfs_iext_lookup_extent(ip, ifp, 0, &icur, &got); |
Darrick J. Wong | ea7cdd7 | 2017-06-16 11:00:11 -0700 | [diff] [blame] | 1450 | while (found) { |
| 1451 | if (isnullstartblock(got.br_startblock) || |
| 1452 | got.br_state != XFS_EXT_NORM) |
| 1453 | goto next; |
| 1454 | agno = XFS_FSB_TO_AGNO(mp, got.br_startblock); |
| 1455 | agbno = XFS_FSB_TO_AGBNO(mp, got.br_startblock); |
| 1456 | aglen = got.br_blockcount; |
| 1457 | |
| 1458 | error = xfs_reflink_find_shared(mp, tp, agno, agbno, aglen, |
| 1459 | &rbno, &rlen, false); |
| 1460 | if (error) |
| 1461 | return error; |
| 1462 | /* Is there still a shared block here? */ |
| 1463 | if (rbno != NULLAGBLOCK) { |
| 1464 | *has_shared = true; |
| 1465 | return 0; |
| 1466 | } |
| 1467 | next: |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1468 | found = xfs_iext_next_extent(ifp, &icur, &got); |
Darrick J. Wong | ea7cdd7 | 2017-06-16 11:00:11 -0700 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | return 0; |
| 1472 | } |
| 1473 | |
Dave Chinner | 844e5e7 | 2018-05-09 07:49:10 -0700 | [diff] [blame] | 1474 | /* |
| 1475 | * Clear the inode reflink flag if there are no shared extents. |
| 1476 | * |
| 1477 | * The caller is responsible for joining the inode to the transaction passed in. |
| 1478 | * The inode will be joined to the transaction that is returned to the caller. |
| 1479 | */ |
Darrick J. Wong | 98cc2db | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1480 | int |
| 1481 | xfs_reflink_clear_inode_flag( |
| 1482 | struct xfs_inode *ip, |
| 1483 | struct xfs_trans **tpp) |
| 1484 | { |
Darrick J. Wong | ea7cdd7 | 2017-06-16 11:00:11 -0700 | [diff] [blame] | 1485 | bool needs_flag; |
Darrick J. Wong | 98cc2db | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1486 | int error = 0; |
| 1487 | |
Darrick J. Wong | 63646fc | 2016-10-10 16:47:32 +1100 | [diff] [blame] | 1488 | ASSERT(xfs_is_reflink_inode(ip)); |
Darrick J. Wong | 98cc2db | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1489 | |
Darrick J. Wong | ea7cdd7 | 2017-06-16 11:00:11 -0700 | [diff] [blame] | 1490 | error = xfs_reflink_inode_has_shared_extents(*tpp, ip, &needs_flag); |
| 1491 | if (error || needs_flag) |
| 1492 | return error; |
Darrick J. Wong | 98cc2db | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1493 | |
| 1494 | /* |
| 1495 | * We didn't find any shared blocks so turn off the reflink flag. |
| 1496 | * First, get rid of any leftover CoW mappings. |
| 1497 | */ |
Christoph Hellwig | 3802a34 | 2017-03-07 16:45:58 -0800 | [diff] [blame] | 1498 | error = xfs_reflink_cancel_cow_blocks(ip, tpp, 0, NULLFILEOFF, true); |
Darrick J. Wong | 98cc2db | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1499 | if (error) |
| 1500 | return error; |
| 1501 | |
| 1502 | /* Clear the inode flag. */ |
| 1503 | trace_xfs_reflink_unset_inode_flag(ip); |
| 1504 | ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK; |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1505 | xfs_inode_clear_cowblocks_tag(ip); |
Darrick J. Wong | 98cc2db | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1506 | xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE); |
| 1507 | |
| 1508 | return error; |
| 1509 | } |
| 1510 | |
| 1511 | /* |
| 1512 | * Clear the inode reflink flag if there are no shared extents and the size |
| 1513 | * hasn't changed. |
| 1514 | */ |
| 1515 | STATIC int |
| 1516 | xfs_reflink_try_clear_inode_flag( |
Darrick J. Wong | 97a1b87 | 2016-10-10 16:49:01 +1100 | [diff] [blame] | 1517 | struct xfs_inode *ip) |
Darrick J. Wong | 98cc2db | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1518 | { |
| 1519 | struct xfs_mount *mp = ip->i_mount; |
| 1520 | struct xfs_trans *tp; |
| 1521 | int error = 0; |
| 1522 | |
| 1523 | /* Start a rolling transaction to remove the mappings */ |
| 1524 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp); |
| 1525 | if (error) |
| 1526 | return error; |
| 1527 | |
| 1528 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1529 | xfs_trans_ijoin(tp, ip, 0); |
| 1530 | |
Darrick J. Wong | 98cc2db | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1531 | error = xfs_reflink_clear_inode_flag(ip, &tp); |
| 1532 | if (error) |
| 1533 | goto cancel; |
| 1534 | |
| 1535 | error = xfs_trans_commit(tp); |
| 1536 | if (error) |
| 1537 | goto out; |
| 1538 | |
| 1539 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1540 | return 0; |
| 1541 | cancel: |
| 1542 | xfs_trans_cancel(tp); |
| 1543 | out: |
| 1544 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1545 | return error; |
| 1546 | } |
| 1547 | |
| 1548 | /* |
| 1549 | * Pre-COW all shared blocks within a given byte range of a file and turn off |
| 1550 | * the reflink flag if we unshare all of the file's blocks. |
| 1551 | */ |
| 1552 | int |
| 1553 | xfs_reflink_unshare( |
| 1554 | struct xfs_inode *ip, |
| 1555 | xfs_off_t offset, |
| 1556 | xfs_off_t len) |
| 1557 | { |
| 1558 | struct xfs_mount *mp = ip->i_mount; |
| 1559 | xfs_fileoff_t fbno; |
| 1560 | xfs_filblks_t end; |
| 1561 | xfs_off_t isize; |
| 1562 | int error; |
| 1563 | |
| 1564 | if (!xfs_is_reflink_inode(ip)) |
| 1565 | return 0; |
| 1566 | |
| 1567 | trace_xfs_reflink_unshare(ip, offset, len); |
| 1568 | |
| 1569 | inode_dio_wait(VFS_I(ip)); |
| 1570 | |
| 1571 | /* Try to CoW the selected ranges */ |
| 1572 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Darrick J. Wong | 97a1b87 | 2016-10-10 16:49:01 +1100 | [diff] [blame] | 1573 | fbno = XFS_B_TO_FSBT(mp, offset); |
Darrick J. Wong | 98cc2db | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1574 | isize = i_size_read(VFS_I(ip)); |
| 1575 | end = XFS_B_TO_FSB(mp, offset + len); |
| 1576 | error = xfs_reflink_dirty_extents(ip, fbno, end, isize); |
| 1577 | if (error) |
| 1578 | goto out_unlock; |
| 1579 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1580 | |
| 1581 | /* Wait for the IO to finish */ |
| 1582 | error = filemap_write_and_wait(VFS_I(ip)->i_mapping); |
| 1583 | if (error) |
| 1584 | goto out; |
| 1585 | |
Darrick J. Wong | 97a1b87 | 2016-10-10 16:49:01 +1100 | [diff] [blame] | 1586 | /* Turn off the reflink flag if possible. */ |
| 1587 | error = xfs_reflink_try_clear_inode_flag(ip); |
| 1588 | if (error) |
| 1589 | goto out; |
Darrick J. Wong | 98cc2db | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1590 | |
| 1591 | return 0; |
| 1592 | |
| 1593 | out_unlock: |
| 1594 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1595 | out: |
| 1596 | trace_xfs_reflink_unshare_error(ip, error, _RET_IP_); |
| 1597 | return error; |
| 1598 | } |