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 | |
Darrick J. Wong | 0b04b6b8 | 2018-07-19 12:26:31 -0700 | [diff] [blame] | 165 | xfs_btree_del_cursor(cur, error); |
Darrick J. Wong | 2a06705 | 2016-10-03 09:11:33 -0700 | [diff] [blame] | 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 | a7beabe | 2018-07-11 22:26:25 -0700 | [diff] [blame] | 327 | XFS_BMAPI_COWFORK | XFS_BMAPI_CONVERT, 0, imap, |
Brian Foster | 3ae2d89 | 2018-07-11 22:26:19 -0700 | [diff] [blame] | 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 | a7beabe | 2018-07-11 22:26:25 -0700 | [diff] [blame] | 350 | XFS_BMAPI_CONVERT_ONLY, 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; |
Christoph Hellwig | 3c68d44 | 2017-02-06 10:51:03 -0800 | [diff] [blame] | 367 | struct xfs_trans *tp = NULL; |
Christoph Hellwig | 3c68d44 | 2017-02-06 10:51:03 -0800 | [diff] [blame] | 368 | int nimaps, error = 0; |
| 369 | bool trimmed; |
Christoph Hellwig | a14234c | 2017-02-06 10:50:49 -0800 | [diff] [blame] | 370 | xfs_filblks_t resaligned; |
Christoph Hellwig | 3c68d44 | 2017-02-06 10:51:03 -0800 | [diff] [blame] | 371 | xfs_extlen_t resblks = 0; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 372 | struct xfs_iext_cursor icur; |
Darrick J. Wong | 0613f16 | 2016-10-03 09:11:37 -0700 | [diff] [blame] | 373 | |
Christoph Hellwig | 3c68d44 | 2017-02-06 10:51:03 -0800 | [diff] [blame] | 374 | retry: |
| 375 | ASSERT(xfs_is_reflink_inode(ip)); |
Christoph Hellwig | c7dbe3f | 2018-03-13 23:15:31 -0700 | [diff] [blame] | 376 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Darrick J. Wong | 0613f16 | 2016-10-03 09:11:37 -0700 | [diff] [blame] | 377 | |
Christoph Hellwig | a14234c | 2017-02-06 10:50:49 -0800 | [diff] [blame] | 378 | /* |
| 379 | * Even if the extent is not shared we might have a preallocation for |
| 380 | * it in the COW fork. If so use it. |
| 381 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 382 | 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] | 383 | got.br_startoff <= offset_fsb) { |
| 384 | *shared = true; |
| 385 | |
Christoph Hellwig | a14234c | 2017-02-06 10:50:49 -0800 | [diff] [blame] | 386 | /* If we have a real allocation in the COW fork we're done. */ |
| 387 | if (!isnullstartblock(got.br_startblock)) { |
Christoph Hellwig | 3c68d44 | 2017-02-06 10:51:03 -0800 | [diff] [blame] | 388 | xfs_trim_extent(&got, offset_fsb, count_fsb); |
| 389 | *imap = got; |
| 390 | goto convert; |
Christoph Hellwig | a14234c | 2017-02-06 10:50:49 -0800 | [diff] [blame] | 391 | } |
Christoph Hellwig | 3c68d44 | 2017-02-06 10:51:03 -0800 | [diff] [blame] | 392 | |
| 393 | xfs_trim_extent(imap, got.br_startoff, got.br_blockcount); |
Christoph Hellwig | a14234c | 2017-02-06 10:50:49 -0800 | [diff] [blame] | 394 | } else { |
Christoph Hellwig | 3c68d44 | 2017-02-06 10:51:03 -0800 | [diff] [blame] | 395 | error = xfs_reflink_trim_around_shared(ip, imap, shared, &trimmed); |
| 396 | if (error || !*shared) |
| 397 | goto out; |
| 398 | } |
| 399 | |
| 400 | if (!tp) { |
| 401 | resaligned = xfs_aligned_fsb_count(imap->br_startoff, |
| 402 | imap->br_blockcount, xfs_get_cowextsz_hint(ip)); |
| 403 | resblks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned); |
| 404 | |
| 405 | xfs_iunlock(ip, *lockmode); |
| 406 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp); |
| 407 | *lockmode = XFS_ILOCK_EXCL; |
| 408 | xfs_ilock(ip, *lockmode); |
| 409 | |
Christoph Hellwig | a14234c | 2017-02-06 10:50:49 -0800 | [diff] [blame] | 410 | if (error) |
Christoph Hellwig | 3c68d44 | 2017-02-06 10:51:03 -0800 | [diff] [blame] | 411 | return error; |
Christoph Hellwig | 3ba020b | 2016-10-20 15:53:50 +1100 | [diff] [blame] | 412 | |
Darrick J. Wong | 4882c19 | 2018-05-04 15:30:22 -0700 | [diff] [blame] | 413 | error = xfs_qm_dqattach_locked(ip, false); |
Christoph Hellwig | a14234c | 2017-02-06 10:50:49 -0800 | [diff] [blame] | 414 | if (error) |
Christoph Hellwig | 3c68d44 | 2017-02-06 10:51:03 -0800 | [diff] [blame] | 415 | goto out; |
| 416 | goto retry; |
Darrick J. Wong | 0613f16 | 2016-10-03 09:11:37 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Christoph Hellwig | a14234c | 2017-02-06 10:50:49 -0800 | [diff] [blame] | 419 | error = xfs_trans_reserve_quota_nblks(tp, ip, resblks, 0, |
| 420 | XFS_QMOPT_RES_REGBLKS); |
Darrick J. Wong | 0613f16 | 2016-10-03 09:11:37 -0700 | [diff] [blame] | 421 | if (error) |
Christoph Hellwig | 3c68d44 | 2017-02-06 10:51:03 -0800 | [diff] [blame] | 422 | goto out; |
Darrick J. Wong | 0613f16 | 2016-10-03 09:11:37 -0700 | [diff] [blame] | 423 | |
Christoph Hellwig | a14234c | 2017-02-06 10:50:49 -0800 | [diff] [blame] | 424 | xfs_trans_ijoin(tp, ip, 0); |
| 425 | |
Christoph Hellwig | a14234c | 2017-02-06 10:50:49 -0800 | [diff] [blame] | 426 | nimaps = 1; |
| 427 | |
| 428 | /* Allocate the entire reservation as unwritten blocks. */ |
Christoph Hellwig | 3c68d44 | 2017-02-06 10:51:03 -0800 | [diff] [blame] | 429 | error = xfs_bmapi_write(tp, ip, imap->br_startoff, imap->br_blockcount, |
Brian Foster | 650919f | 2018-07-11 22:26:23 -0700 | [diff] [blame] | 430 | XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC, |
Brian Foster | a7beabe | 2018-07-11 22:26:25 -0700 | [diff] [blame] | 431 | resblks, imap, &nimaps); |
Christoph Hellwig | a14234c | 2017-02-06 10:50:49 -0800 | [diff] [blame] | 432 | if (error) |
Brian Foster | c8eac49 | 2018-07-24 13:43:13 -0700 | [diff] [blame] | 433 | goto out_trans_cancel; |
Christoph Hellwig | a14234c | 2017-02-06 10:50:49 -0800 | [diff] [blame] | 434 | |
Darrick J. Wong | 86d692b | 2017-12-14 15:46:06 -0800 | [diff] [blame] | 435 | xfs_inode_set_cowblocks_tag(ip); |
| 436 | |
Darrick J. Wong | 5eda430 | 2017-02-02 15:14:02 -0800 | [diff] [blame] | 437 | /* Finish up. */ |
Darrick J. Wong | 0613f16 | 2016-10-03 09:11:37 -0700 | [diff] [blame] | 438 | error = xfs_trans_commit(tp); |
Christoph Hellwig | a14234c | 2017-02-06 10:50:49 -0800 | [diff] [blame] | 439 | if (error) |
Christoph Hellwig | 3c68d44 | 2017-02-06 10:51:03 -0800 | [diff] [blame] | 440 | return error; |
Darrick J. Wong | 9f37bd1 | 2018-01-26 11:37:44 -0800 | [diff] [blame] | 441 | |
| 442 | /* |
| 443 | * Allocation succeeded but the requested range was not even partially |
| 444 | * satisfied? Bail out! |
| 445 | */ |
| 446 | if (nimaps == 0) |
| 447 | return -ENOSPC; |
Christoph Hellwig | 3c68d44 | 2017-02-06 10:51:03 -0800 | [diff] [blame] | 448 | convert: |
Brian Foster | 8a74938 | 2018-07-11 22:26:06 -0700 | [diff] [blame] | 449 | return xfs_reflink_convert_cow_extent(ip, imap, offset_fsb, count_fsb); |
Brian Foster | c8eac49 | 2018-07-24 13:43:13 -0700 | [diff] [blame] | 450 | out_trans_cancel: |
Christoph Hellwig | a14234c | 2017-02-06 10:50:49 -0800 | [diff] [blame] | 451 | xfs_trans_unreserve_quota_nblks(tp, ip, (long)resblks, 0, |
| 452 | XFS_QMOPT_RES_REGBLKS); |
Christoph Hellwig | 3c68d44 | 2017-02-06 10:51:03 -0800 | [diff] [blame] | 453 | out: |
| 454 | if (tp) |
| 455 | xfs_trans_cancel(tp); |
| 456 | return error; |
Darrick J. Wong | 0613f16 | 2016-10-03 09:11:37 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Darrick J. Wong | ef47366 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 459 | /* |
Christoph Hellwig | 3802a34 | 2017-03-07 16:45:58 -0800 | [diff] [blame] | 460 | * Cancel CoW reservations for some block range of an inode. |
| 461 | * |
| 462 | * If cancel_real is true this function cancels all COW fork extents for the |
| 463 | * inode; if cancel_real is false, real extents are not cleared. |
Dave Chinner | c5295c6 | 2018-05-09 07:49:09 -0700 | [diff] [blame] | 464 | * |
| 465 | * Caller must have already joined the inode to the current transaction. The |
| 466 | * inode will be joined to the transaction returned to the caller. |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 467 | */ |
| 468 | int |
| 469 | xfs_reflink_cancel_cow_blocks( |
| 470 | struct xfs_inode *ip, |
| 471 | struct xfs_trans **tpp, |
| 472 | xfs_fileoff_t offset_fsb, |
Christoph Hellwig | 3802a34 | 2017-03-07 16:45:58 -0800 | [diff] [blame] | 473 | xfs_fileoff_t end_fsb, |
| 474 | bool cancel_real) |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 475 | { |
Christoph Hellwig | 3e0ee78 | 2016-10-20 15:54:31 +1100 | [diff] [blame] | 476 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK); |
Christoph Hellwig | df5ab1b | 2016-11-24 11:39:50 +1100 | [diff] [blame] | 477 | struct xfs_bmbt_irec got, del; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 478 | struct xfs_iext_cursor icur; |
Christoph Hellwig | df5ab1b | 2016-11-24 11:39:50 +1100 | [diff] [blame] | 479 | int error = 0; |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 480 | |
Christoph Hellwig | 51d6269 | 2018-07-17 16:51:51 -0700 | [diff] [blame^] | 481 | if (!xfs_inode_has_cow_data(ip)) |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 482 | return 0; |
Christoph Hellwig | 41caabd | 2017-11-03 10:34:44 -0700 | [diff] [blame] | 483 | if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got)) |
Christoph Hellwig | 3e0ee78 | 2016-10-20 15:54:31 +1100 | [diff] [blame] | 484 | return 0; |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 485 | |
Christoph Hellwig | 41caabd | 2017-11-03 10:34:44 -0700 | [diff] [blame] | 486 | /* Walk backwards until we're out of the I/O range... */ |
| 487 | while (got.br_startoff + got.br_blockcount > offset_fsb) { |
Christoph Hellwig | 3e0ee78 | 2016-10-20 15:54:31 +1100 | [diff] [blame] | 488 | del = got; |
| 489 | xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb); |
Christoph Hellwig | 41caabd | 2017-11-03 10:34:44 -0700 | [diff] [blame] | 490 | |
| 491 | /* Extent delete may have bumped ext forward */ |
| 492 | if (!del.br_blockcount) { |
| 493 | xfs_iext_prev(ifp, &icur); |
| 494 | goto next_extent; |
| 495 | } |
| 496 | |
Christoph Hellwig | 3e0ee78 | 2016-10-20 15:54:31 +1100 | [diff] [blame] | 497 | trace_xfs_reflink_cancel_cow(ip, &del); |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 498 | |
Christoph Hellwig | 3e0ee78 | 2016-10-20 15:54:31 +1100 | [diff] [blame] | 499 | if (isnullstartblock(del.br_startblock)) { |
| 500 | error = xfs_bmap_del_extent_delay(ip, XFS_COW_FORK, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 501 | &icur, &got, &del); |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 502 | if (error) |
| 503 | break; |
Christoph Hellwig | 3802a34 | 2017-03-07 16:45:58 -0800 | [diff] [blame] | 504 | } else if (del.br_state == XFS_EXT_UNWRITTEN || cancel_real) { |
Brian Foster | 1e5ae19 | 2018-07-24 13:43:12 -0700 | [diff] [blame] | 505 | ASSERT((*tpp)->t_dfops); |
| 506 | ASSERT((*tpp)->t_firstblock == NULLFSBLOCK); |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 507 | |
Darrick J. Wong | 174edb0 | 2016-10-03 09:11:39 -0700 | [diff] [blame] | 508 | /* Free the CoW orphan record. */ |
| 509 | error = xfs_refcount_free_cow_extent(ip->i_mount, |
Brian Foster | 27356a0 | 2018-07-11 22:26:18 -0700 | [diff] [blame] | 510 | (*tpp)->t_dfops, del.br_startblock, |
Christoph Hellwig | 3e0ee78 | 2016-10-20 15:54:31 +1100 | [diff] [blame] | 511 | del.br_blockcount); |
Darrick J. Wong | 174edb0 | 2016-10-03 09:11:39 -0700 | [diff] [blame] | 512 | if (error) |
| 513 | break; |
| 514 | |
Brian Foster | 27356a0 | 2018-07-11 22:26:18 -0700 | [diff] [blame] | 515 | xfs_bmap_add_free(ip->i_mount, (*tpp)->t_dfops, |
Christoph Hellwig | 3e0ee78 | 2016-10-20 15:54:31 +1100 | [diff] [blame] | 516 | del.br_startblock, del.br_blockcount, |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 517 | NULL); |
| 518 | |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 519 | /* Roll the transaction */ |
Brian Foster | 27356a0 | 2018-07-11 22:26:18 -0700 | [diff] [blame] | 520 | xfs_defer_ijoin((*tpp)->t_dfops, ip); |
Brian Foster | 9e28a24 | 2018-07-24 13:43:15 -0700 | [diff] [blame] | 521 | error = xfs_defer_finish(tpp); |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 522 | if (error) { |
Brian Foster | 9e28a24 | 2018-07-24 13:43:15 -0700 | [diff] [blame] | 523 | xfs_defer_cancel(*tpp); |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 524 | break; |
| 525 | } |
| 526 | |
| 527 | /* Remove the mapping from the CoW fork. */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 528 | xfs_bmap_del_extent_cow(ip, &icur, &got, &del); |
Darrick J. Wong | 4b4c132 | 2018-01-19 09:05:48 -0800 | [diff] [blame] | 529 | |
| 530 | /* Remove the quota reservation */ |
| 531 | error = xfs_trans_reserve_quota_nblks(NULL, ip, |
| 532 | -(long)del.br_blockcount, 0, |
| 533 | XFS_QMOPT_RES_REGBLKS); |
| 534 | if (error) |
| 535 | break; |
Darrick J. Wong | 9d40fba | 2017-12-10 18:03:55 -0800 | [diff] [blame] | 536 | } else { |
| 537 | /* Didn't do anything, push cursor back. */ |
| 538 | xfs_iext_prev(ifp, &icur); |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 539 | } |
Christoph Hellwig | 41caabd | 2017-11-03 10:34:44 -0700 | [diff] [blame] | 540 | next_extent: |
| 541 | if (!xfs_iext_get_extent(ifp, &icur, &got)) |
Brian Foster | c17a8ef | 2016-10-24 14:21:08 +1100 | [diff] [blame] | 542 | break; |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Brian Foster | c17a8ef | 2016-10-24 14:21:08 +1100 | [diff] [blame] | 545 | /* clear tag if cow fork is emptied */ |
| 546 | if (!ifp->if_bytes) |
| 547 | xfs_inode_clear_cowblocks_tag(ip); |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 548 | return error; |
| 549 | } |
| 550 | |
| 551 | /* |
Christoph Hellwig | 3802a34 | 2017-03-07 16:45:58 -0800 | [diff] [blame] | 552 | * Cancel CoW reservations for some byte range of an inode. |
| 553 | * |
| 554 | * If cancel_real is true this function cancels all COW fork extents for the |
| 555 | * inode; if cancel_real is false, real extents are not cleared. |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 556 | */ |
| 557 | int |
| 558 | xfs_reflink_cancel_cow_range( |
| 559 | struct xfs_inode *ip, |
| 560 | xfs_off_t offset, |
Christoph Hellwig | 3802a34 | 2017-03-07 16:45:58 -0800 | [diff] [blame] | 561 | xfs_off_t count, |
| 562 | bool cancel_real) |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 563 | { |
| 564 | struct xfs_trans *tp; |
| 565 | xfs_fileoff_t offset_fsb; |
| 566 | xfs_fileoff_t end_fsb; |
| 567 | int error; |
| 568 | |
| 569 | trace_xfs_reflink_cancel_cow_range(ip, offset, count); |
Darrick J. Wong | 63646fc | 2016-10-10 16:47:32 +1100 | [diff] [blame] | 570 | ASSERT(xfs_is_reflink_inode(ip)); |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 571 | |
| 572 | offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset); |
| 573 | if (count == NULLFILEOFF) |
| 574 | end_fsb = NULLFILEOFF; |
| 575 | else |
| 576 | end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count); |
| 577 | |
| 578 | /* Start a rolling transaction to remove the mappings */ |
| 579 | 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] | 580 | 0, 0, XFS_TRANS_NOFS, &tp); |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 581 | if (error) |
| 582 | goto out; |
| 583 | |
| 584 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 585 | xfs_trans_ijoin(tp, ip, 0); |
| 586 | |
| 587 | /* Scrape out the old CoW reservations */ |
Christoph Hellwig | 3802a34 | 2017-03-07 16:45:58 -0800 | [diff] [blame] | 588 | error = xfs_reflink_cancel_cow_blocks(ip, &tp, offset_fsb, end_fsb, |
| 589 | cancel_real); |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 590 | if (error) |
| 591 | goto out_cancel; |
| 592 | |
| 593 | error = xfs_trans_commit(tp); |
| 594 | |
| 595 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 596 | return error; |
| 597 | |
| 598 | out_cancel: |
| 599 | xfs_trans_cancel(tp); |
| 600 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 601 | out: |
| 602 | trace_xfs_reflink_cancel_cow_range_error(ip, error, _RET_IP_); |
| 603 | return error; |
| 604 | } |
| 605 | |
| 606 | /* |
| 607 | * Remap parts of a file's data fork after a successful CoW. |
| 608 | */ |
| 609 | int |
| 610 | xfs_reflink_end_cow( |
| 611 | struct xfs_inode *ip, |
| 612 | xfs_off_t offset, |
| 613 | xfs_off_t count) |
| 614 | { |
Christoph Hellwig | c1112b6 | 2016-10-20 15:54:45 +1100 | [diff] [blame] | 615 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK); |
Christoph Hellwig | 4ab8671 | 2016-11-24 11:39:50 +1100 | [diff] [blame] | 616 | struct xfs_bmbt_irec got, del; |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 617 | struct xfs_trans *tp; |
| 618 | xfs_fileoff_t offset_fsb; |
| 619 | xfs_fileoff_t end_fsb; |
Christoph Hellwig | 4ab8671 | 2016-11-24 11:39:50 +1100 | [diff] [blame] | 620 | int error; |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 621 | unsigned int resblks; |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 622 | xfs_filblks_t rlen; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 623 | struct xfs_iext_cursor icur; |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 624 | |
| 625 | trace_xfs_reflink_end_cow(ip, offset, count); |
| 626 | |
Christoph Hellwig | c1112b6 | 2016-10-20 15:54:45 +1100 | [diff] [blame] | 627 | /* No COW extents? That's easy! */ |
| 628 | if (ifp->if_bytes == 0) |
| 629 | return 0; |
| 630 | |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 631 | offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset); |
| 632 | end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count); |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 633 | |
Darrick J. Wong | fe0be23 | 2017-04-12 12:26:07 -0700 | [diff] [blame] | 634 | /* |
| 635 | * Start a rolling transaction to switch the mappings. We're |
| 636 | * unlikely ever to have to remap 16T worth of single-block |
| 637 | * extents, so just cap the worst case extent count to 2^32-1. |
| 638 | * Stick a warning in just in case, and avoid 64-bit division. |
| 639 | */ |
| 640 | BUILD_BUG_ON(MAX_RW_COUNT > UINT_MAX); |
| 641 | if (end_fsb - offset_fsb > UINT_MAX) { |
| 642 | error = -EFSCORRUPTED; |
| 643 | xfs_force_shutdown(ip->i_mount, SHUTDOWN_CORRUPT_INCORE); |
| 644 | ASSERT(0); |
| 645 | goto out; |
| 646 | } |
| 647 | resblks = XFS_NEXTENTADD_SPACE_RES(ip->i_mount, |
| 648 | (unsigned int)(end_fsb - offset_fsb), |
| 649 | XFS_DATA_FORK); |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 650 | 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] | 651 | resblks, 0, XFS_TRANS_RESERVE | XFS_TRANS_NOFS, &tp); |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 652 | if (error) |
| 653 | goto out; |
| 654 | |
| 655 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 656 | xfs_trans_ijoin(tp, ip, 0); |
| 657 | |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 658 | /* |
| 659 | * In case of racing, overlapping AIO writes no COW extents might be |
| 660 | * left by the time I/O completes for the loser of the race. In that |
| 661 | * case we are done. |
| 662 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 663 | if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got)) |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 664 | goto out_cancel; |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 665 | |
Christoph Hellwig | c1112b6 | 2016-10-20 15:54:45 +1100 | [diff] [blame] | 666 | /* Walk backwards until we're out of the I/O range... */ |
| 667 | while (got.br_startoff + got.br_blockcount > offset_fsb) { |
| 668 | del = got; |
| 669 | xfs_trim_extent(&del, offset_fsb, end_fsb - offset_fsb); |
| 670 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 671 | /* Extent delete may have bumped ext forward */ |
Christoph Hellwig | df79b81 | 2018-03-13 23:15:33 -0700 | [diff] [blame] | 672 | if (!del.br_blockcount) |
| 673 | goto prev_extent; |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 674 | |
Christoph Hellwig | c1112b6 | 2016-10-20 15:54:45 +1100 | [diff] [blame] | 675 | ASSERT(!isnullstartblock(got.br_startblock)); |
| 676 | |
Darrick J. Wong | 5eda430 | 2017-02-02 15:14:02 -0800 | [diff] [blame] | 677 | /* |
| 678 | * Don't remap unwritten extents; these are |
| 679 | * speculatively preallocated CoW extents that have been |
| 680 | * allocated but have not yet been involved in a write. |
| 681 | */ |
Christoph Hellwig | df79b81 | 2018-03-13 23:15:33 -0700 | [diff] [blame] | 682 | if (got.br_state == XFS_EXT_UNWRITTEN) |
| 683 | goto prev_extent; |
Darrick J. Wong | 5eda430 | 2017-02-02 15:14:02 -0800 | [diff] [blame] | 684 | |
Christoph Hellwig | c1112b6 | 2016-10-20 15:54:45 +1100 | [diff] [blame] | 685 | /* Unmap the old blocks in the data fork. */ |
Brian Foster | c8eac49 | 2018-07-24 13:43:13 -0700 | [diff] [blame] | 686 | ASSERT(tp->t_dfops && tp->t_firstblock == NULLFSBLOCK); |
Christoph Hellwig | c1112b6 | 2016-10-20 15:54:45 +1100 | [diff] [blame] | 687 | rlen = del.br_blockcount; |
Brian Foster | 2af5284 | 2018-07-11 22:26:25 -0700 | [diff] [blame] | 688 | error = __xfs_bunmapi(tp, ip, del.br_startoff, &rlen, 0, 1); |
Christoph Hellwig | c1112b6 | 2016-10-20 15:54:45 +1100 | [diff] [blame] | 689 | if (error) |
Brian Foster | c8eac49 | 2018-07-24 13:43:13 -0700 | [diff] [blame] | 690 | goto out_cancel; |
Christoph Hellwig | c1112b6 | 2016-10-20 15:54:45 +1100 | [diff] [blame] | 691 | |
| 692 | /* Trim the extent to whatever got unmapped. */ |
| 693 | if (rlen) { |
| 694 | xfs_trim_extent(&del, del.br_startoff + rlen, |
| 695 | del.br_blockcount - rlen); |
| 696 | } |
| 697 | trace_xfs_reflink_cow_remap(ip, &del); |
| 698 | |
| 699 | /* Free the CoW orphan record. */ |
Brian Foster | 4bcfa61 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 700 | error = xfs_refcount_free_cow_extent(tp->t_mountp, tp->t_dfops, |
Christoph Hellwig | c1112b6 | 2016-10-20 15:54:45 +1100 | [diff] [blame] | 701 | del.br_startblock, del.br_blockcount); |
| 702 | if (error) |
Brian Foster | c8eac49 | 2018-07-24 13:43:13 -0700 | [diff] [blame] | 703 | goto out_cancel; |
Christoph Hellwig | c1112b6 | 2016-10-20 15:54:45 +1100 | [diff] [blame] | 704 | |
| 705 | /* Map the new blocks into the data fork. */ |
Brian Foster | 4bcfa61 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 706 | error = xfs_bmap_map_extent(tp->t_mountp, tp->t_dfops, ip, |
| 707 | &del); |
Christoph Hellwig | c1112b6 | 2016-10-20 15:54:45 +1100 | [diff] [blame] | 708 | if (error) |
Brian Foster | c8eac49 | 2018-07-24 13:43:13 -0700 | [diff] [blame] | 709 | goto out_cancel; |
Christoph Hellwig | c1112b6 | 2016-10-20 15:54:45 +1100 | [diff] [blame] | 710 | |
Darrick J. Wong | 4b4c132 | 2018-01-19 09:05:48 -0800 | [diff] [blame] | 711 | /* Charge this new data fork mapping to the on-disk quota. */ |
| 712 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_DELBCOUNT, |
| 713 | (long)del.br_blockcount); |
| 714 | |
Christoph Hellwig | c1112b6 | 2016-10-20 15:54:45 +1100 | [diff] [blame] | 715 | /* Remove the mapping from the CoW fork. */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 716 | xfs_bmap_del_extent_cow(ip, &icur, &got, &del); |
Christoph Hellwig | c1112b6 | 2016-10-20 15:54:45 +1100 | [diff] [blame] | 717 | |
Brian Foster | 4bcfa61 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 718 | xfs_defer_ijoin(tp->t_dfops, ip); |
Brian Foster | 9e28a24 | 2018-07-24 13:43:15 -0700 | [diff] [blame] | 719 | error = xfs_defer_finish(&tp); |
Christoph Hellwig | c1112b6 | 2016-10-20 15:54:45 +1100 | [diff] [blame] | 720 | if (error) |
Brian Foster | c8eac49 | 2018-07-24 13:43:13 -0700 | [diff] [blame] | 721 | goto out_cancel; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 722 | if (!xfs_iext_get_extent(ifp, &icur, &got)) |
Christoph Hellwig | c1112b6 | 2016-10-20 15:54:45 +1100 | [diff] [blame] | 723 | break; |
Christoph Hellwig | df79b81 | 2018-03-13 23:15:33 -0700 | [diff] [blame] | 724 | continue; |
| 725 | prev_extent: |
| 726 | if (!xfs_iext_prev_extent(ifp, &icur, &got)) |
| 727 | break; |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | error = xfs_trans_commit(tp); |
| 731 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 732 | if (error) |
| 733 | goto out; |
| 734 | return 0; |
| 735 | |
Christoph Hellwig | e12199f | 2017-10-03 08:58:33 -0700 | [diff] [blame] | 736 | out_cancel: |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 737 | xfs_trans_cancel(tp); |
| 738 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 739 | out: |
| 740 | trace_xfs_reflink_end_cow_error(ip, error, _RET_IP_); |
| 741 | return error; |
| 742 | } |
Darrick J. Wong | 174edb0 | 2016-10-03 09:11:39 -0700 | [diff] [blame] | 743 | |
| 744 | /* |
| 745 | * Free leftover CoW reservations that didn't get cleaned out. |
| 746 | */ |
| 747 | int |
| 748 | xfs_reflink_recover_cow( |
| 749 | struct xfs_mount *mp) |
| 750 | { |
| 751 | xfs_agnumber_t agno; |
| 752 | int error = 0; |
| 753 | |
| 754 | if (!xfs_sb_version_hasreflink(&mp->m_sb)) |
| 755 | return 0; |
| 756 | |
| 757 | for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) { |
| 758 | error = xfs_refcount_recover_cow_leftovers(mp, agno); |
| 759 | if (error) |
| 760 | break; |
| 761 | } |
| 762 | |
| 763 | return error; |
| 764 | } |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 765 | |
| 766 | /* |
| 767 | * Reflinking (Block) Ranges of Two Files Together |
| 768 | * |
| 769 | * First, ensure that the reflink flag is set on both inodes. The flag is an |
| 770 | * optimization to avoid unnecessary refcount btree lookups in the write path. |
| 771 | * |
| 772 | * Now we can iteratively remap the range of extents (and holes) in src to the |
| 773 | * corresponding ranges in dest. Let drange and srange denote the ranges of |
| 774 | * logical blocks in dest and src touched by the reflink operation. |
| 775 | * |
| 776 | * While the length of drange is greater than zero, |
| 777 | * - Read src's bmbt at the start of srange ("imap") |
| 778 | * - If imap doesn't exist, make imap appear to start at the end of srange |
| 779 | * with zero length. |
| 780 | * - If imap starts before srange, advance imap to start at srange. |
| 781 | * - If imap goes beyond srange, truncate imap to end at the end of srange. |
| 782 | * - Punch (imap start - srange start + imap len) blocks from dest at |
| 783 | * offset (drange start). |
| 784 | * - If imap points to a real range of pblks, |
| 785 | * > Increase the refcount of the imap's pblks |
| 786 | * > Map imap's pblks into dest at the offset |
| 787 | * (drange start + imap start - srange start) |
| 788 | * - Advance drange and srange by (imap start - srange start + imap len) |
| 789 | * |
| 790 | * Finally, if the reflink made dest longer, update both the in-core and |
| 791 | * on-disk file sizes. |
| 792 | * |
| 793 | * ASCII Art Demonstration: |
| 794 | * |
| 795 | * Let's say we want to reflink this source file: |
| 796 | * |
| 797 | * ----SSSSSSS-SSSSS----SSSSSS (src file) |
| 798 | * <--------------------> |
| 799 | * |
| 800 | * into this destination file: |
| 801 | * |
| 802 | * --DDDDDDDDDDDDDDDDDDD--DDD (dest file) |
| 803 | * <--------------------> |
| 804 | * '-' means a hole, and 'S' and 'D' are written blocks in the src and dest. |
| 805 | * Observe that the range has different logical offsets in either file. |
| 806 | * |
| 807 | * Consider that the first extent in the source file doesn't line up with our |
| 808 | * reflink range. Unmapping and remapping are separate operations, so we can |
| 809 | * unmap more blocks from the destination file than we remap. |
| 810 | * |
| 811 | * ----SSSSSSS-SSSSS----SSSSSS |
| 812 | * <-------> |
| 813 | * --DDDDD---------DDDDD--DDD |
| 814 | * <-------> |
| 815 | * |
| 816 | * Now remap the source extent into the destination file: |
| 817 | * |
| 818 | * ----SSSSSSS-SSSSS----SSSSSS |
| 819 | * <-------> |
| 820 | * --DDDDD--SSSSSSSDDDDD--DDD |
| 821 | * <-------> |
| 822 | * |
| 823 | * Do likewise with the second hole and extent in our range. Holes in the |
| 824 | * unmap range don't affect our operation. |
| 825 | * |
| 826 | * ----SSSSSSS-SSSSS----SSSSSS |
| 827 | * <----> |
| 828 | * --DDDDD--SSSSSSS-SSSSS-DDD |
| 829 | * <----> |
| 830 | * |
| 831 | * Finally, unmap and remap part of the third extent. This will increase the |
| 832 | * size of the destination file. |
| 833 | * |
| 834 | * ----SSSSSSS-SSSSS----SSSSSS |
| 835 | * <-----> |
| 836 | * --DDDDD--SSSSSSS-SSSSS----SSS |
| 837 | * <-----> |
| 838 | * |
| 839 | * Once we update the destination file's i_size, we're done. |
| 840 | */ |
| 841 | |
| 842 | /* |
| 843 | * Ensure the reflink bit is set in both inodes. |
| 844 | */ |
| 845 | STATIC int |
| 846 | xfs_reflink_set_inode_flag( |
| 847 | struct xfs_inode *src, |
| 848 | struct xfs_inode *dest) |
| 849 | { |
| 850 | struct xfs_mount *mp = src->i_mount; |
| 851 | int error; |
| 852 | struct xfs_trans *tp; |
| 853 | |
| 854 | if (xfs_is_reflink_inode(src) && xfs_is_reflink_inode(dest)) |
| 855 | return 0; |
| 856 | |
| 857 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp); |
| 858 | if (error) |
| 859 | goto out_error; |
| 860 | |
| 861 | /* Lock both files against IO */ |
| 862 | if (src->i_ino == dest->i_ino) |
| 863 | xfs_ilock(src, XFS_ILOCK_EXCL); |
| 864 | else |
Darrick J. Wong | 7c2d238 | 2018-01-26 15:27:33 -0800 | [diff] [blame] | 865 | 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] | 866 | |
| 867 | if (!xfs_is_reflink_inode(src)) { |
| 868 | trace_xfs_reflink_set_inode_flag(src); |
| 869 | xfs_trans_ijoin(tp, src, XFS_ILOCK_EXCL); |
| 870 | src->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK; |
| 871 | xfs_trans_log_inode(tp, src, XFS_ILOG_CORE); |
| 872 | xfs_ifork_init_cow(src); |
| 873 | } else |
| 874 | xfs_iunlock(src, XFS_ILOCK_EXCL); |
| 875 | |
| 876 | if (src->i_ino == dest->i_ino) |
| 877 | goto commit_flags; |
| 878 | |
| 879 | if (!xfs_is_reflink_inode(dest)) { |
| 880 | trace_xfs_reflink_set_inode_flag(dest); |
| 881 | xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL); |
| 882 | dest->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK; |
| 883 | xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE); |
| 884 | xfs_ifork_init_cow(dest); |
| 885 | } else |
| 886 | xfs_iunlock(dest, XFS_ILOCK_EXCL); |
| 887 | |
| 888 | commit_flags: |
| 889 | error = xfs_trans_commit(tp); |
| 890 | if (error) |
| 891 | goto out_error; |
| 892 | return error; |
| 893 | |
| 894 | out_error: |
| 895 | trace_xfs_reflink_set_inode_flag_error(dest, error, _RET_IP_); |
| 896 | return error; |
| 897 | } |
| 898 | |
| 899 | /* |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 900 | * Update destination inode size & cowextsize hint, if necessary. |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 901 | */ |
| 902 | STATIC int |
| 903 | xfs_reflink_update_dest( |
| 904 | struct xfs_inode *dest, |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 905 | xfs_off_t newlen, |
Christoph Hellwig | c5ecb42 | 2017-02-06 17:45:51 -0800 | [diff] [blame] | 906 | xfs_extlen_t cowextsize, |
| 907 | bool is_dedupe) |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 908 | { |
| 909 | struct xfs_mount *mp = dest->i_mount; |
| 910 | struct xfs_trans *tp; |
| 911 | int error; |
| 912 | |
Christoph Hellwig | c5ecb42 | 2017-02-06 17:45:51 -0800 | [diff] [blame] | 913 | 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] | 914 | return 0; |
| 915 | |
| 916 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp); |
| 917 | if (error) |
| 918 | goto out_error; |
| 919 | |
| 920 | xfs_ilock(dest, XFS_ILOCK_EXCL); |
| 921 | xfs_trans_ijoin(tp, dest, XFS_ILOCK_EXCL); |
| 922 | |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 923 | if (newlen > i_size_read(VFS_I(dest))) { |
| 924 | trace_xfs_reflink_update_inode_size(dest, newlen); |
| 925 | i_size_write(VFS_I(dest), newlen); |
| 926 | dest->i_d.di_size = newlen; |
| 927 | } |
| 928 | |
| 929 | if (cowextsize) { |
| 930 | dest->i_d.di_cowextsize = cowextsize; |
| 931 | dest->i_d.di_flags2 |= XFS_DIFLAG2_COWEXTSIZE; |
| 932 | } |
| 933 | |
Christoph Hellwig | c5ecb42 | 2017-02-06 17:45:51 -0800 | [diff] [blame] | 934 | if (!is_dedupe) { |
| 935 | xfs_trans_ichgtime(tp, dest, |
| 936 | XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
| 937 | } |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 938 | xfs_trans_log_inode(tp, dest, XFS_ILOG_CORE); |
| 939 | |
| 940 | error = xfs_trans_commit(tp); |
| 941 | if (error) |
| 942 | goto out_error; |
| 943 | return error; |
| 944 | |
| 945 | out_error: |
| 946 | trace_xfs_reflink_update_inode_size_error(dest, error, _RET_IP_); |
| 947 | return error; |
| 948 | } |
| 949 | |
| 950 | /* |
Darrick J. Wong | 6fa164b | 2016-10-03 09:11:45 -0700 | [diff] [blame] | 951 | * Do we have enough reserve in this AG to handle a reflink? The refcount |
| 952 | * btree already reserved all the space it needs, but the rmap btree can grow |
| 953 | * infinitely, so we won't allow more reflinks when the AG is down to the |
| 954 | * btree reserves. |
| 955 | */ |
| 956 | static int |
| 957 | xfs_reflink_ag_has_free_space( |
| 958 | struct xfs_mount *mp, |
| 959 | xfs_agnumber_t agno) |
| 960 | { |
| 961 | struct xfs_perag *pag; |
| 962 | int error = 0; |
| 963 | |
| 964 | if (!xfs_sb_version_hasrmapbt(&mp->m_sb)) |
| 965 | return 0; |
| 966 | |
| 967 | pag = xfs_perag_get(mp, agno); |
Brian Foster | 2159286 | 2018-03-09 14:01:59 -0800 | [diff] [blame] | 968 | if (xfs_ag_resv_critical(pag, XFS_AG_RESV_RMAPBT) || |
Darrick J. Wong | 6fa164b | 2016-10-03 09:11:45 -0700 | [diff] [blame] | 969 | xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA)) |
| 970 | error = -ENOSPC; |
| 971 | xfs_perag_put(pag); |
| 972 | return error; |
| 973 | } |
| 974 | |
| 975 | /* |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 976 | * Unmap a range of blocks from a file, then map other blocks into the hole. |
| 977 | * The range to unmap is (destoff : destoff + srcioff + irec->br_blockcount). |
| 978 | * The extent irec is mapped into dest at irec->br_startoff. |
| 979 | */ |
| 980 | STATIC int |
| 981 | xfs_reflink_remap_extent( |
| 982 | struct xfs_inode *ip, |
| 983 | struct xfs_bmbt_irec *irec, |
| 984 | xfs_fileoff_t destoff, |
| 985 | xfs_off_t new_isize) |
| 986 | { |
| 987 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | 9c4f29d | 2017-03-28 14:53:35 -0700 | [diff] [blame] | 988 | bool real_extent = xfs_bmap_is_real_extent(irec); |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 989 | struct xfs_trans *tp; |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 990 | unsigned int resblks; |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 991 | struct xfs_bmbt_irec uirec; |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 992 | xfs_filblks_t rlen; |
| 993 | xfs_filblks_t unmap_len; |
| 994 | xfs_off_t newlen; |
| 995 | int error; |
| 996 | |
| 997 | unmap_len = irec->br_startoff + irec->br_blockcount - destoff; |
| 998 | trace_xfs_reflink_punch_range(ip, destoff, unmap_len); |
| 999 | |
Darrick J. Wong | 6fa164b | 2016-10-03 09:11:45 -0700 | [diff] [blame] | 1000 | /* No reflinking if we're low on space */ |
| 1001 | if (real_extent) { |
| 1002 | error = xfs_reflink_ag_has_free_space(mp, |
| 1003 | XFS_FSB_TO_AGNO(mp, irec->br_startblock)); |
| 1004 | if (error) |
| 1005 | goto out; |
| 1006 | } |
| 1007 | |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1008 | /* Start a rolling transaction to switch the mappings */ |
| 1009 | resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK); |
| 1010 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp); |
| 1011 | if (error) |
| 1012 | goto out; |
| 1013 | |
| 1014 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1015 | xfs_trans_ijoin(tp, ip, 0); |
| 1016 | |
| 1017 | /* If we're not just clearing space, then do we have enough quota? */ |
| 1018 | if (real_extent) { |
| 1019 | error = xfs_trans_reserve_quota_nblks(tp, ip, |
| 1020 | irec->br_blockcount, 0, XFS_QMOPT_RES_REGBLKS); |
| 1021 | if (error) |
| 1022 | goto out_cancel; |
| 1023 | } |
| 1024 | |
| 1025 | trace_xfs_reflink_remap(ip, irec->br_startoff, |
| 1026 | irec->br_blockcount, irec->br_startblock); |
| 1027 | |
| 1028 | /* Unmap the old blocks in the data fork. */ |
| 1029 | rlen = unmap_len; |
| 1030 | while (rlen) { |
Brian Foster | c8eac49 | 2018-07-24 13:43:13 -0700 | [diff] [blame] | 1031 | ASSERT(tp->t_dfops && tp->t_firstblock == NULLFSBLOCK); |
Brian Foster | 2af5284 | 2018-07-11 22:26:25 -0700 | [diff] [blame] | 1032 | error = __xfs_bunmapi(tp, ip, destoff, &rlen, 0, 1); |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1033 | if (error) |
Brian Foster | c8eac49 | 2018-07-24 13:43:13 -0700 | [diff] [blame] | 1034 | goto out_cancel; |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1035 | |
| 1036 | /* |
| 1037 | * Trim the extent to whatever got unmapped. |
| 1038 | * Remember, bunmapi works backwards. |
| 1039 | */ |
| 1040 | uirec.br_startblock = irec->br_startblock + rlen; |
| 1041 | uirec.br_startoff = irec->br_startoff + rlen; |
| 1042 | uirec.br_blockcount = unmap_len - rlen; |
| 1043 | unmap_len = rlen; |
| 1044 | |
| 1045 | /* If this isn't a real mapping, we're done. */ |
| 1046 | if (!real_extent || uirec.br_blockcount == 0) |
| 1047 | goto next_extent; |
| 1048 | |
| 1049 | trace_xfs_reflink_remap(ip, uirec.br_startoff, |
| 1050 | uirec.br_blockcount, uirec.br_startblock); |
| 1051 | |
| 1052 | /* Update the refcount tree */ |
Brian Foster | 4bcfa61 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 1053 | error = xfs_refcount_increase_extent(mp, tp->t_dfops, &uirec); |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1054 | if (error) |
Brian Foster | c8eac49 | 2018-07-24 13:43:13 -0700 | [diff] [blame] | 1055 | goto out_cancel; |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1056 | |
| 1057 | /* Map the new blocks into the data fork. */ |
Brian Foster | 4bcfa61 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 1058 | error = xfs_bmap_map_extent(mp, tp->t_dfops, ip, &uirec); |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1059 | if (error) |
Brian Foster | c8eac49 | 2018-07-24 13:43:13 -0700 | [diff] [blame] | 1060 | goto out_cancel; |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1061 | |
| 1062 | /* Update quota accounting. */ |
| 1063 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, |
| 1064 | uirec.br_blockcount); |
| 1065 | |
| 1066 | /* Update dest isize if needed. */ |
| 1067 | newlen = XFS_FSB_TO_B(mp, |
| 1068 | uirec.br_startoff + uirec.br_blockcount); |
| 1069 | newlen = min_t(xfs_off_t, newlen, new_isize); |
| 1070 | if (newlen > i_size_read(VFS_I(ip))) { |
| 1071 | trace_xfs_reflink_update_inode_size(ip, newlen); |
| 1072 | i_size_write(VFS_I(ip), newlen); |
| 1073 | ip->i_d.di_size = newlen; |
| 1074 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1075 | } |
| 1076 | |
| 1077 | next_extent: |
| 1078 | /* Process all the deferred stuff. */ |
Brian Foster | 4bcfa61 | 2018-07-11 22:26:13 -0700 | [diff] [blame] | 1079 | xfs_defer_ijoin(tp->t_dfops, ip); |
Brian Foster | 9e28a24 | 2018-07-24 13:43:15 -0700 | [diff] [blame] | 1080 | error = xfs_defer_finish(&tp); |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1081 | if (error) |
Brian Foster | c8eac49 | 2018-07-24 13:43:13 -0700 | [diff] [blame] | 1082 | goto out_cancel; |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | error = xfs_trans_commit(tp); |
| 1086 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1087 | if (error) |
| 1088 | goto out; |
| 1089 | return 0; |
| 1090 | |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1091 | out_cancel: |
| 1092 | xfs_trans_cancel(tp); |
| 1093 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1094 | out: |
| 1095 | trace_xfs_reflink_remap_extent_error(ip, error, _RET_IP_); |
| 1096 | return error; |
| 1097 | } |
| 1098 | |
| 1099 | /* |
| 1100 | * Iteratively remap one file's extents (and holes) to another's. |
| 1101 | */ |
| 1102 | STATIC int |
| 1103 | xfs_reflink_remap_blocks( |
| 1104 | struct xfs_inode *src, |
| 1105 | xfs_fileoff_t srcoff, |
| 1106 | struct xfs_inode *dest, |
| 1107 | xfs_fileoff_t destoff, |
| 1108 | xfs_filblks_t len, |
| 1109 | xfs_off_t new_isize) |
| 1110 | { |
| 1111 | struct xfs_bmbt_irec imap; |
| 1112 | int nimaps; |
| 1113 | int error = 0; |
| 1114 | xfs_filblks_t range_len; |
| 1115 | |
| 1116 | /* drange = (destoff, destoff + len); srange = (srcoff, srcoff + len) */ |
| 1117 | while (len) { |
Darrick J. Wong | 01c2e13 | 2018-01-18 14:07:53 -0800 | [diff] [blame] | 1118 | uint lock_mode; |
| 1119 | |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1120 | trace_xfs_reflink_remap_blocks_loop(src, srcoff, len, |
| 1121 | dest, destoff); |
Darrick J. Wong | 01c2e13 | 2018-01-18 14:07:53 -0800 | [diff] [blame] | 1122 | |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1123 | /* Read extent from the source file */ |
| 1124 | nimaps = 1; |
Darrick J. Wong | 01c2e13 | 2018-01-18 14:07:53 -0800 | [diff] [blame] | 1125 | lock_mode = xfs_ilock_data_map_shared(src); |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1126 | error = xfs_bmapi_read(src, srcoff, len, &imap, &nimaps, 0); |
Darrick J. Wong | 01c2e13 | 2018-01-18 14:07:53 -0800 | [diff] [blame] | 1127 | xfs_iunlock(src, lock_mode); |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1128 | if (error) |
| 1129 | goto err; |
| 1130 | ASSERT(nimaps == 1); |
| 1131 | |
| 1132 | trace_xfs_reflink_remap_imap(src, srcoff, len, XFS_IO_OVERWRITE, |
| 1133 | &imap); |
| 1134 | |
| 1135 | /* Translate imap into the destination file. */ |
| 1136 | range_len = imap.br_startoff + imap.br_blockcount - srcoff; |
| 1137 | imap.br_startoff += destoff - srcoff; |
| 1138 | |
| 1139 | /* Clear dest from destoff to the end of imap and map it in. */ |
| 1140 | error = xfs_reflink_remap_extent(dest, &imap, destoff, |
| 1141 | new_isize); |
| 1142 | if (error) |
| 1143 | goto err; |
| 1144 | |
| 1145 | if (fatal_signal_pending(current)) { |
| 1146 | error = -EINTR; |
| 1147 | goto err; |
| 1148 | } |
| 1149 | |
| 1150 | /* Advance drange/srange */ |
| 1151 | srcoff += range_len; |
| 1152 | destoff += range_len; |
| 1153 | len -= range_len; |
| 1154 | } |
| 1155 | |
| 1156 | return 0; |
| 1157 | |
| 1158 | err: |
| 1159 | trace_xfs_reflink_remap_blocks_error(dest, error, _RET_IP_); |
| 1160 | return error; |
| 1161 | } |
| 1162 | |
| 1163 | /* |
Darrick J. Wong | 1364b1d4 | 2018-01-18 13:55:20 -0800 | [diff] [blame] | 1164 | * Grab the exclusive iolock for a data copy from src to dest, making |
| 1165 | * sure to abide vfs locking order (lowest pointer value goes first) and |
| 1166 | * breaking the pnfs layout leases on dest before proceeding. The loop |
| 1167 | * is needed because we cannot call the blocking break_layout() with the |
| 1168 | * src iolock held, and therefore have to back out both locks. |
| 1169 | */ |
| 1170 | static int |
| 1171 | xfs_iolock_two_inodes_and_break_layout( |
| 1172 | struct inode *src, |
| 1173 | struct inode *dest) |
| 1174 | { |
| 1175 | int error; |
| 1176 | |
| 1177 | retry: |
| 1178 | if (src < dest) { |
Darrick J. Wong | 01c2e13 | 2018-01-18 14:07:53 -0800 | [diff] [blame] | 1179 | inode_lock_shared(src); |
Darrick J. Wong | 1364b1d4 | 2018-01-18 13:55:20 -0800 | [diff] [blame] | 1180 | inode_lock_nested(dest, I_MUTEX_NONDIR2); |
| 1181 | } else { |
| 1182 | /* src >= dest */ |
| 1183 | inode_lock(dest); |
| 1184 | } |
| 1185 | |
| 1186 | error = break_layout(dest, false); |
| 1187 | if (error == -EWOULDBLOCK) { |
| 1188 | inode_unlock(dest); |
| 1189 | if (src < dest) |
Darrick J. Wong | 01c2e13 | 2018-01-18 14:07:53 -0800 | [diff] [blame] | 1190 | inode_unlock_shared(src); |
Darrick J. Wong | 1364b1d4 | 2018-01-18 13:55:20 -0800 | [diff] [blame] | 1191 | error = break_layout(dest, true); |
| 1192 | if (error) |
| 1193 | return error; |
| 1194 | goto retry; |
| 1195 | } |
| 1196 | if (error) { |
| 1197 | inode_unlock(dest); |
| 1198 | if (src < dest) |
Darrick J. Wong | 01c2e13 | 2018-01-18 14:07:53 -0800 | [diff] [blame] | 1199 | inode_unlock_shared(src); |
Darrick J. Wong | 1364b1d4 | 2018-01-18 13:55:20 -0800 | [diff] [blame] | 1200 | return error; |
| 1201 | } |
| 1202 | if (src > dest) |
Darrick J. Wong | 01c2e13 | 2018-01-18 14:07:53 -0800 | [diff] [blame] | 1203 | inode_lock_shared_nested(src, I_MUTEX_NONDIR2); |
Darrick J. Wong | 1364b1d4 | 2018-01-18 13:55:20 -0800 | [diff] [blame] | 1204 | return 0; |
| 1205 | } |
| 1206 | |
| 1207 | /* |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1208 | * Link a range of blocks from one file to another. |
| 1209 | */ |
| 1210 | int |
| 1211 | xfs_reflink_remap_range( |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1212 | struct file *file_in, |
| 1213 | loff_t pos_in, |
| 1214 | struct file *file_out, |
| 1215 | loff_t pos_out, |
| 1216 | u64 len, |
| 1217 | bool is_dedupe) |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1218 | { |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1219 | struct inode *inode_in = file_inode(file_in); |
| 1220 | struct xfs_inode *src = XFS_I(inode_in); |
| 1221 | struct inode *inode_out = file_inode(file_out); |
| 1222 | struct xfs_inode *dest = XFS_I(inode_out); |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1223 | struct xfs_mount *mp = src->i_mount; |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1224 | bool same_inode = (inode_in == inode_out); |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1225 | xfs_fileoff_t sfsbno, dfsbno; |
| 1226 | xfs_filblks_t fsblen; |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1227 | xfs_extlen_t cowextsize; |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1228 | ssize_t ret; |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1229 | |
| 1230 | if (!xfs_sb_version_hasreflink(&mp->m_sb)) |
| 1231 | return -EOPNOTSUPP; |
| 1232 | |
| 1233 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 1234 | return -EIO; |
| 1235 | |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1236 | /* Lock both files against IO */ |
Darrick J. Wong | 1364b1d4 | 2018-01-18 13:55:20 -0800 | [diff] [blame] | 1237 | ret = xfs_iolock_two_inodes_and_break_layout(inode_in, inode_out); |
| 1238 | if (ret) |
| 1239 | return ret; |
Christoph Hellwig | 6552321 | 2016-11-30 14:33:25 +1100 | [diff] [blame] | 1240 | if (same_inode) |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1241 | xfs_ilock(src, XFS_MMAPLOCK_EXCL); |
Christoph Hellwig | 6552321 | 2016-11-30 14:33:25 +1100 | [diff] [blame] | 1242 | else |
Darrick J. Wong | 01c2e13 | 2018-01-18 14:07:53 -0800 | [diff] [blame] | 1243 | xfs_lock_two_inodes(src, XFS_MMAPLOCK_SHARED, dest, |
Darrick J. Wong | 7c2d238 | 2018-01-26 15:27:33 -0800 | [diff] [blame] | 1244 | XFS_MMAPLOCK_EXCL); |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1245 | |
Darrick J. Wong | 876bec6f | 2016-12-09 16:18:30 -0800 | [diff] [blame] | 1246 | /* Check file eligibility and prepare for block sharing. */ |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1247 | ret = -EINVAL; |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1248 | /* Don't reflink realtime inodes */ |
| 1249 | if (XFS_IS_REALTIME_INODE(src) || XFS_IS_REALTIME_INODE(dest)) |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1250 | goto out_unlock; |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1251 | |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1252 | /* Don't share DAX file data for now. */ |
| 1253 | if (IS_DAX(inode_in) || IS_DAX(inode_out)) |
| 1254 | goto out_unlock; |
Darrick J. Wong | cc71466 | 2016-10-03 09:11:41 -0700 | [diff] [blame] | 1255 | |
Darrick J. Wong | 876bec6f | 2016-12-09 16:18:30 -0800 | [diff] [blame] | 1256 | ret = vfs_clone_file_prep_inodes(inode_in, pos_in, inode_out, pos_out, |
| 1257 | &len, is_dedupe); |
Darrick J. Wong | 22725ce | 2016-12-19 15:13:26 -0800 | [diff] [blame] | 1258 | if (ret <= 0) |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1259 | goto out_unlock; |
| 1260 | |
Darrick J. Wong | 09ac862 | 2018-01-19 08:56:04 -0800 | [diff] [blame] | 1261 | /* Attach dquots to dest inode before changing block map */ |
Darrick J. Wong | c14cfcc | 2018-05-04 15:30:21 -0700 | [diff] [blame] | 1262 | ret = xfs_qm_dqattach(dest); |
Darrick J. Wong | 09ac862 | 2018-01-19 08:56:04 -0800 | [diff] [blame] | 1263 | if (ret) |
| 1264 | goto out_unlock; |
| 1265 | |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1266 | 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] | 1267 | |
Darrick J. Wong | 5c989a0 | 2017-12-10 18:03:54 -0800 | [diff] [blame] | 1268 | /* |
| 1269 | * Clear out post-eof preallocations because we don't have page cache |
| 1270 | * backing the delayed allocations and they'll never get freed on |
| 1271 | * their own. |
| 1272 | */ |
| 1273 | if (xfs_can_free_eofblocks(dest, true)) { |
| 1274 | ret = xfs_free_eofblocks(dest); |
| 1275 | if (ret) |
| 1276 | goto out_unlock; |
| 1277 | } |
| 1278 | |
Darrick J. Wong | 876bec6f | 2016-12-09 16:18:30 -0800 | [diff] [blame] | 1279 | /* Set flags and remap blocks. */ |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1280 | ret = xfs_reflink_set_inode_flag(src, dest); |
| 1281 | if (ret) |
| 1282 | goto out_unlock; |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1283 | |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1284 | dfsbno = XFS_B_TO_FSBT(mp, pos_out); |
| 1285 | sfsbno = XFS_B_TO_FSBT(mp, pos_in); |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1286 | fsblen = XFS_B_TO_FSB(mp, len); |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1287 | ret = xfs_reflink_remap_blocks(src, sfsbno, dest, dfsbno, fsblen, |
| 1288 | pos_out + len); |
| 1289 | if (ret) |
| 1290 | goto out_unlock; |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1291 | |
Darrick J. Wong | 876bec6f | 2016-12-09 16:18:30 -0800 | [diff] [blame] | 1292 | /* Zap any page cache for the destination file's range. */ |
| 1293 | truncate_inode_pages_range(&inode_out->i_data, pos_out, |
| 1294 | PAGE_ALIGN(pos_out + len) - 1); |
| 1295 | |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1296 | /* |
| 1297 | * Carry the cowextsize hint from src to dest if we're sharing the |
| 1298 | * entire source file to the entire destination file, the source file |
| 1299 | * has a cowextsize hint, and the destination file does not. |
| 1300 | */ |
| 1301 | cowextsize = 0; |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1302 | if (pos_in == 0 && len == i_size_read(inode_in) && |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1303 | (src->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE) && |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1304 | pos_out == 0 && len >= i_size_read(inode_out) && |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1305 | !(dest->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE)) |
| 1306 | cowextsize = src->i_d.di_cowextsize; |
| 1307 | |
Christoph Hellwig | c5ecb42 | 2017-02-06 17:45:51 -0800 | [diff] [blame] | 1308 | ret = xfs_reflink_update_dest(dest, pos_out + len, cowextsize, |
| 1309 | is_dedupe); |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1310 | |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1311 | out_unlock: |
Darrick J. Wong | 01c2e13 | 2018-01-18 14:07:53 -0800 | [diff] [blame] | 1312 | xfs_iunlock(dest, XFS_MMAPLOCK_EXCL); |
Christoph Hellwig | 6552321 | 2016-11-30 14:33:25 +1100 | [diff] [blame] | 1313 | if (!same_inode) |
Darrick J. Wong | 01c2e13 | 2018-01-18 14:07:53 -0800 | [diff] [blame] | 1314 | xfs_iunlock(src, XFS_MMAPLOCK_SHARED); |
| 1315 | inode_unlock(inode_out); |
| 1316 | if (!same_inode) |
| 1317 | inode_unlock_shared(inode_in); |
Christoph Hellwig | 5faaf4f | 2016-10-20 15:50:07 +1100 | [diff] [blame] | 1318 | if (ret) |
| 1319 | trace_xfs_reflink_remap_range_error(dest, ret, _RET_IP_); |
| 1320 | return ret; |
Darrick J. Wong | 862bb36 | 2016-10-03 09:11:40 -0700 | [diff] [blame] | 1321 | } |
Darrick J. Wong | 98cc2db | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1322 | |
| 1323 | /* |
| 1324 | * The user wants to preemptively CoW all shared blocks in this file, |
| 1325 | * which enables us to turn off the reflink flag. Iterate all |
| 1326 | * extents which are not prealloc/delalloc to see which ranges are |
| 1327 | * mentioned in the refcount tree, then read those blocks into the |
| 1328 | * pagecache, dirty them, fsync them back out, and then we can update |
| 1329 | * the inode flag. What happens if we run out of memory? :) |
| 1330 | */ |
| 1331 | STATIC int |
| 1332 | xfs_reflink_dirty_extents( |
| 1333 | struct xfs_inode *ip, |
| 1334 | xfs_fileoff_t fbno, |
| 1335 | xfs_filblks_t end, |
| 1336 | xfs_off_t isize) |
| 1337 | { |
| 1338 | struct xfs_mount *mp = ip->i_mount; |
| 1339 | xfs_agnumber_t agno; |
| 1340 | xfs_agblock_t agbno; |
| 1341 | xfs_extlen_t aglen; |
| 1342 | xfs_agblock_t rbno; |
| 1343 | xfs_extlen_t rlen; |
| 1344 | xfs_off_t fpos; |
| 1345 | xfs_off_t flen; |
| 1346 | struct xfs_bmbt_irec map[2]; |
| 1347 | int nmaps; |
Darrick J. Wong | 9780643c | 2016-10-10 16:49:18 +1100 | [diff] [blame] | 1348 | int error = 0; |
Darrick J. Wong | 98cc2db | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1349 | |
| 1350 | while (end - fbno > 0) { |
| 1351 | nmaps = 1; |
| 1352 | /* |
| 1353 | * Look for extents in the file. Skip holes, delalloc, or |
| 1354 | * unwritten extents; they can't be reflinked. |
| 1355 | */ |
| 1356 | error = xfs_bmapi_read(ip, fbno, end - fbno, map, &nmaps, 0); |
| 1357 | if (error) |
| 1358 | goto out; |
| 1359 | if (nmaps == 0) |
| 1360 | break; |
Christoph Hellwig | 9c4f29d | 2017-03-28 14:53:35 -0700 | [diff] [blame] | 1361 | if (!xfs_bmap_is_real_extent(&map[0])) |
Darrick J. Wong | 98cc2db | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1362 | goto next; |
| 1363 | |
| 1364 | map[1] = map[0]; |
| 1365 | while (map[1].br_blockcount) { |
| 1366 | agno = XFS_FSB_TO_AGNO(mp, map[1].br_startblock); |
| 1367 | agbno = XFS_FSB_TO_AGBNO(mp, map[1].br_startblock); |
| 1368 | aglen = map[1].br_blockcount; |
| 1369 | |
Darrick J. Wong | 92ff728 | 2017-06-16 11:00:10 -0700 | [diff] [blame] | 1370 | error = xfs_reflink_find_shared(mp, NULL, agno, agbno, |
| 1371 | aglen, &rbno, &rlen, true); |
Darrick J. Wong | 98cc2db | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1372 | if (error) |
| 1373 | goto out; |
| 1374 | if (rbno == NULLAGBLOCK) |
| 1375 | break; |
| 1376 | |
| 1377 | /* Dirty the pages */ |
| 1378 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1379 | fpos = XFS_FSB_TO_B(mp, map[1].br_startoff + |
| 1380 | (rbno - agbno)); |
| 1381 | flen = XFS_FSB_TO_B(mp, rlen); |
| 1382 | if (fpos + flen > isize) |
| 1383 | flen = isize - fpos; |
| 1384 | error = iomap_file_dirty(VFS_I(ip), fpos, flen, |
| 1385 | &xfs_iomap_ops); |
| 1386 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1387 | if (error) |
| 1388 | goto out; |
| 1389 | |
| 1390 | map[1].br_blockcount -= (rbno - agbno + rlen); |
| 1391 | map[1].br_startoff += (rbno - agbno + rlen); |
| 1392 | map[1].br_startblock += (rbno - agbno + rlen); |
| 1393 | } |
| 1394 | |
| 1395 | next: |
| 1396 | fbno = map[0].br_startoff + map[0].br_blockcount; |
| 1397 | } |
| 1398 | out: |
| 1399 | return error; |
| 1400 | } |
| 1401 | |
Darrick J. Wong | ea7cdd7 | 2017-06-16 11:00:11 -0700 | [diff] [blame] | 1402 | /* Does this inode need the reflink flag? */ |
| 1403 | int |
| 1404 | xfs_reflink_inode_has_shared_extents( |
| 1405 | struct xfs_trans *tp, |
| 1406 | struct xfs_inode *ip, |
| 1407 | bool *has_shared) |
| 1408 | { |
| 1409 | struct xfs_bmbt_irec got; |
| 1410 | struct xfs_mount *mp = ip->i_mount; |
| 1411 | struct xfs_ifork *ifp; |
| 1412 | xfs_agnumber_t agno; |
| 1413 | xfs_agblock_t agbno; |
| 1414 | xfs_extlen_t aglen; |
| 1415 | xfs_agblock_t rbno; |
| 1416 | xfs_extlen_t rlen; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1417 | struct xfs_iext_cursor icur; |
Darrick J. Wong | ea7cdd7 | 2017-06-16 11:00:11 -0700 | [diff] [blame] | 1418 | bool found; |
| 1419 | int error; |
| 1420 | |
| 1421 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 1422 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1423 | error = xfs_iread_extents(tp, ip, XFS_DATA_FORK); |
| 1424 | if (error) |
| 1425 | return error; |
| 1426 | } |
| 1427 | |
| 1428 | *has_shared = false; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1429 | found = xfs_iext_lookup_extent(ip, ifp, 0, &icur, &got); |
Darrick J. Wong | ea7cdd7 | 2017-06-16 11:00:11 -0700 | [diff] [blame] | 1430 | while (found) { |
| 1431 | if (isnullstartblock(got.br_startblock) || |
| 1432 | got.br_state != XFS_EXT_NORM) |
| 1433 | goto next; |
| 1434 | agno = XFS_FSB_TO_AGNO(mp, got.br_startblock); |
| 1435 | agbno = XFS_FSB_TO_AGBNO(mp, got.br_startblock); |
| 1436 | aglen = got.br_blockcount; |
| 1437 | |
| 1438 | error = xfs_reflink_find_shared(mp, tp, agno, agbno, aglen, |
| 1439 | &rbno, &rlen, false); |
| 1440 | if (error) |
| 1441 | return error; |
| 1442 | /* Is there still a shared block here? */ |
| 1443 | if (rbno != NULLAGBLOCK) { |
| 1444 | *has_shared = true; |
| 1445 | return 0; |
| 1446 | } |
| 1447 | next: |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1448 | found = xfs_iext_next_extent(ifp, &icur, &got); |
Darrick J. Wong | ea7cdd7 | 2017-06-16 11:00:11 -0700 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | return 0; |
| 1452 | } |
| 1453 | |
Dave Chinner | 844e5e7 | 2018-05-09 07:49:10 -0700 | [diff] [blame] | 1454 | /* |
| 1455 | * Clear the inode reflink flag if there are no shared extents. |
| 1456 | * |
| 1457 | * The caller is responsible for joining the inode to the transaction passed in. |
| 1458 | * The inode will be joined to the transaction that is returned to the caller. |
| 1459 | */ |
Darrick J. Wong | 98cc2db | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1460 | int |
| 1461 | xfs_reflink_clear_inode_flag( |
| 1462 | struct xfs_inode *ip, |
| 1463 | struct xfs_trans **tpp) |
| 1464 | { |
Darrick J. Wong | ea7cdd7 | 2017-06-16 11:00:11 -0700 | [diff] [blame] | 1465 | bool needs_flag; |
Darrick J. Wong | 98cc2db | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1466 | int error = 0; |
| 1467 | |
Darrick J. Wong | 63646fc | 2016-10-10 16:47:32 +1100 | [diff] [blame] | 1468 | ASSERT(xfs_is_reflink_inode(ip)); |
Darrick J. Wong | 98cc2db | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1469 | |
Darrick J. Wong | ea7cdd7 | 2017-06-16 11:00:11 -0700 | [diff] [blame] | 1470 | error = xfs_reflink_inode_has_shared_extents(*tpp, ip, &needs_flag); |
| 1471 | if (error || needs_flag) |
| 1472 | return error; |
Darrick J. Wong | 98cc2db | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1473 | |
| 1474 | /* |
| 1475 | * We didn't find any shared blocks so turn off the reflink flag. |
| 1476 | * First, get rid of any leftover CoW mappings. |
| 1477 | */ |
Christoph Hellwig | 3802a34 | 2017-03-07 16:45:58 -0800 | [diff] [blame] | 1478 | error = xfs_reflink_cancel_cow_blocks(ip, tpp, 0, NULLFILEOFF, true); |
Darrick J. Wong | 98cc2db | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1479 | if (error) |
| 1480 | return error; |
| 1481 | |
| 1482 | /* Clear the inode flag. */ |
| 1483 | trace_xfs_reflink_unset_inode_flag(ip); |
| 1484 | ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK; |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1485 | xfs_inode_clear_cowblocks_tag(ip); |
Darrick J. Wong | 98cc2db | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1486 | xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE); |
| 1487 | |
| 1488 | return error; |
| 1489 | } |
| 1490 | |
| 1491 | /* |
| 1492 | * Clear the inode reflink flag if there are no shared extents and the size |
| 1493 | * hasn't changed. |
| 1494 | */ |
| 1495 | STATIC int |
| 1496 | xfs_reflink_try_clear_inode_flag( |
Darrick J. Wong | 97a1b87 | 2016-10-10 16:49:01 +1100 | [diff] [blame] | 1497 | struct xfs_inode *ip) |
Darrick J. Wong | 98cc2db | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1498 | { |
| 1499 | struct xfs_mount *mp = ip->i_mount; |
| 1500 | struct xfs_trans *tp; |
| 1501 | int error = 0; |
| 1502 | |
| 1503 | /* Start a rolling transaction to remove the mappings */ |
| 1504 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp); |
| 1505 | if (error) |
| 1506 | return error; |
| 1507 | |
| 1508 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1509 | xfs_trans_ijoin(tp, ip, 0); |
| 1510 | |
Darrick J. Wong | 98cc2db | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1511 | error = xfs_reflink_clear_inode_flag(ip, &tp); |
| 1512 | if (error) |
| 1513 | goto cancel; |
| 1514 | |
| 1515 | error = xfs_trans_commit(tp); |
| 1516 | if (error) |
| 1517 | goto out; |
| 1518 | |
| 1519 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1520 | return 0; |
| 1521 | cancel: |
| 1522 | xfs_trans_cancel(tp); |
| 1523 | out: |
| 1524 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1525 | return error; |
| 1526 | } |
| 1527 | |
| 1528 | /* |
| 1529 | * Pre-COW all shared blocks within a given byte range of a file and turn off |
| 1530 | * the reflink flag if we unshare all of the file's blocks. |
| 1531 | */ |
| 1532 | int |
| 1533 | xfs_reflink_unshare( |
| 1534 | struct xfs_inode *ip, |
| 1535 | xfs_off_t offset, |
| 1536 | xfs_off_t len) |
| 1537 | { |
| 1538 | struct xfs_mount *mp = ip->i_mount; |
| 1539 | xfs_fileoff_t fbno; |
| 1540 | xfs_filblks_t end; |
| 1541 | xfs_off_t isize; |
| 1542 | int error; |
| 1543 | |
| 1544 | if (!xfs_is_reflink_inode(ip)) |
| 1545 | return 0; |
| 1546 | |
| 1547 | trace_xfs_reflink_unshare(ip, offset, len); |
| 1548 | |
| 1549 | inode_dio_wait(VFS_I(ip)); |
| 1550 | |
| 1551 | /* Try to CoW the selected ranges */ |
| 1552 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Darrick J. Wong | 97a1b87 | 2016-10-10 16:49:01 +1100 | [diff] [blame] | 1553 | fbno = XFS_B_TO_FSBT(mp, offset); |
Darrick J. Wong | 98cc2db | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1554 | isize = i_size_read(VFS_I(ip)); |
| 1555 | end = XFS_B_TO_FSB(mp, offset + len); |
| 1556 | error = xfs_reflink_dirty_extents(ip, fbno, end, isize); |
| 1557 | if (error) |
| 1558 | goto out_unlock; |
| 1559 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1560 | |
| 1561 | /* Wait for the IO to finish */ |
| 1562 | error = filemap_write_and_wait(VFS_I(ip)->i_mapping); |
| 1563 | if (error) |
| 1564 | goto out; |
| 1565 | |
Darrick J. Wong | 97a1b87 | 2016-10-10 16:49:01 +1100 | [diff] [blame] | 1566 | /* Turn off the reflink flag if possible. */ |
| 1567 | error = xfs_reflink_try_clear_inode_flag(ip); |
| 1568 | if (error) |
| 1569 | goto out; |
Darrick J. Wong | 98cc2db | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 1570 | |
| 1571 | return 0; |
| 1572 | |
| 1573 | out_unlock: |
| 1574 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1575 | out: |
| 1576 | trace_xfs_reflink_unshare_error(ip, error, _RET_IP_); |
| 1577 | return error; |
| 1578 | } |