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