Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 Oracle. All Rights Reserved. |
| 3 | * |
| 4 | * Author: Darrick J. Wong <darrick.wong@oracle.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version 2 |
| 9 | * of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it would be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write the Free Software Foundation, |
| 18 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | */ |
| 20 | #include "xfs.h" |
| 21 | #include "xfs_fs.h" |
| 22 | #include "xfs_shared.h" |
| 23 | #include "xfs_format.h" |
| 24 | #include "xfs_log_format.h" |
| 25 | #include "xfs_trans_resv.h" |
| 26 | #include "xfs_mount.h" |
| 27 | #include "xfs_defer.h" |
| 28 | #include "xfs_da_format.h" |
| 29 | #include "xfs_da_btree.h" |
| 30 | #include "xfs_inode.h" |
| 31 | #include "xfs_trans.h" |
| 32 | #include "xfs_inode_item.h" |
| 33 | #include "xfs_bmap.h" |
| 34 | #include "xfs_bmap_util.h" |
| 35 | #include "xfs_error.h" |
| 36 | #include "xfs_dir2.h" |
| 37 | #include "xfs_dir2_priv.h" |
| 38 | #include "xfs_ioctl.h" |
| 39 | #include "xfs_trace.h" |
| 40 | #include "xfs_log.h" |
| 41 | #include "xfs_icache.h" |
| 42 | #include "xfs_pnfs.h" |
Darrick J. Wong | 174edb0 | 2016-10-03 09:11:39 -0700 | [diff] [blame^] | 43 | #include "xfs_btree.h" |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 44 | #include "xfs_refcount_btree.h" |
| 45 | #include "xfs_refcount.h" |
| 46 | #include "xfs_bmap_btree.h" |
| 47 | #include "xfs_trans_space.h" |
| 48 | #include "xfs_bit.h" |
| 49 | #include "xfs_alloc.h" |
| 50 | #include "xfs_quota_defs.h" |
| 51 | #include "xfs_quota.h" |
| 52 | #include "xfs_btree.h" |
| 53 | #include "xfs_bmap_btree.h" |
| 54 | #include "xfs_reflink.h" |
Darrick J. Wong | 2a06705 | 2016-10-03 09:11:33 -0700 | [diff] [blame] | 55 | #include "xfs_iomap.h" |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 56 | #include "xfs_rmap_btree.h" |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 57 | |
| 58 | /* |
| 59 | * Copy on Write of Shared Blocks |
| 60 | * |
| 61 | * XFS must preserve "the usual" file semantics even when two files share |
| 62 | * the same physical blocks. This means that a write to one file must not |
| 63 | * alter the blocks in a different file; the way that we'll do that is |
| 64 | * through the use of a copy-on-write mechanism. At a high level, that |
| 65 | * means that when we want to write to a shared block, we allocate a new |
| 66 | * block, write the data to the new block, and if that succeeds we map the |
| 67 | * new block into the file. |
| 68 | * |
| 69 | * XFS provides a "delayed allocation" mechanism that defers the allocation |
| 70 | * of disk blocks to dirty-but-not-yet-mapped file blocks as long as |
| 71 | * possible. This reduces fragmentation by enabling the filesystem to ask |
| 72 | * for bigger chunks less often, which is exactly what we want for CoW. |
| 73 | * |
| 74 | * The delalloc mechanism begins when the kernel wants to make a block |
| 75 | * writable (write_begin or page_mkwrite). If the offset is not mapped, we |
| 76 | * create a delalloc mapping, which is a regular in-core extent, but without |
| 77 | * a real startblock. (For delalloc mappings, the startblock encodes both |
| 78 | * a flag that this is a delalloc mapping, and a worst-case estimate of how |
| 79 | * many blocks might be required to put the mapping into the BMBT.) delalloc |
| 80 | * mappings are a reservation against the free space in the filesystem; |
| 81 | * adjacent mappings can also be combined into fewer larger mappings. |
| 82 | * |
| 83 | * When dirty pages are being written out (typically in writepage), the |
| 84 | * delalloc reservations are converted into real mappings by allocating |
| 85 | * blocks and replacing the delalloc mapping with real ones. A delalloc |
| 86 | * mapping can be replaced by several real ones if the free space is |
| 87 | * fragmented. |
| 88 | * |
| 89 | * We want to adapt the delalloc mechanism for copy-on-write, since the |
| 90 | * write paths are similar. The first two steps (creating the reservation |
| 91 | * and allocating the blocks) are exactly the same as delalloc except that |
| 92 | * the mappings must be stored in a separate CoW fork because we do not want |
| 93 | * to disturb the mapping in the data fork until we're sure that the write |
| 94 | * succeeded. IO completion in this case is the process of removing the old |
| 95 | * mapping from the data fork and moving the new mapping from the CoW fork to |
| 96 | * the data fork. This will be discussed shortly. |
| 97 | * |
| 98 | * For now, unaligned directio writes will be bounced back to the page cache. |
| 99 | * Block-aligned directio writes will use the same mechanism as buffered |
| 100 | * writes. |
| 101 | * |
| 102 | * CoW remapping must be done after the data block write completes, |
| 103 | * because we don't want to destroy the old data fork map until we're sure |
| 104 | * the new block has been written. Since the new mappings are kept in a |
| 105 | * separate fork, we can simply iterate these mappings to find the ones |
| 106 | * that cover the file blocks that we just CoW'd. For each extent, simply |
| 107 | * unmap the corresponding range in the data fork, map the new range into |
| 108 | * the data fork, and remove the extent from the CoW fork. |
| 109 | * |
| 110 | * Since the remapping operation can be applied to an arbitrary file |
| 111 | * range, we record the need for the remap step as a flag in the ioend |
| 112 | * instead of declaring a new IO type. This is required for direct io |
| 113 | * because we only have ioend for the whole dio, and we have to be able to |
| 114 | * remember the presence of unwritten blocks and CoW blocks with a single |
| 115 | * ioend structure. Better yet, the more ground we can cover with one |
| 116 | * ioend, the better. |
| 117 | */ |
Darrick J. Wong | 2a06705 | 2016-10-03 09:11:33 -0700 | [diff] [blame] | 118 | |
| 119 | /* |
| 120 | * Given an AG extent, find the lowest-numbered run of shared blocks |
| 121 | * within that range and return the range in fbno/flen. If |
| 122 | * find_end_of_shared is true, return the longest contiguous extent of |
| 123 | * shared blocks. If there are no shared extents, fbno and flen will |
| 124 | * be set to NULLAGBLOCK and 0, respectively. |
| 125 | */ |
| 126 | int |
| 127 | xfs_reflink_find_shared( |
| 128 | struct xfs_mount *mp, |
| 129 | xfs_agnumber_t agno, |
| 130 | xfs_agblock_t agbno, |
| 131 | xfs_extlen_t aglen, |
| 132 | xfs_agblock_t *fbno, |
| 133 | xfs_extlen_t *flen, |
| 134 | bool find_end_of_shared) |
| 135 | { |
| 136 | struct xfs_buf *agbp; |
| 137 | struct xfs_btree_cur *cur; |
| 138 | int error; |
| 139 | |
| 140 | error = xfs_alloc_read_agf(mp, NULL, agno, 0, &agbp); |
| 141 | if (error) |
| 142 | return error; |
| 143 | |
| 144 | cur = xfs_refcountbt_init_cursor(mp, NULL, agbp, agno, NULL); |
| 145 | |
| 146 | error = xfs_refcount_find_shared(cur, agbno, aglen, fbno, flen, |
| 147 | find_end_of_shared); |
| 148 | |
| 149 | xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 150 | |
| 151 | xfs_buf_relse(agbp); |
| 152 | return error; |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * Trim the mapping to the next block where there's a change in the |
| 157 | * shared/unshared status. More specifically, this means that we |
| 158 | * find the lowest-numbered extent of shared blocks that coincides with |
| 159 | * the given block mapping. If the shared extent overlaps the start of |
| 160 | * the mapping, trim the mapping to the end of the shared extent. If |
| 161 | * the shared region intersects the mapping, trim the mapping to the |
| 162 | * start of the shared extent. If there are no shared regions that |
| 163 | * overlap, just return the original extent. |
| 164 | */ |
| 165 | int |
| 166 | xfs_reflink_trim_around_shared( |
| 167 | struct xfs_inode *ip, |
| 168 | struct xfs_bmbt_irec *irec, |
| 169 | bool *shared, |
| 170 | bool *trimmed) |
| 171 | { |
| 172 | xfs_agnumber_t agno; |
| 173 | xfs_agblock_t agbno; |
| 174 | xfs_extlen_t aglen; |
| 175 | xfs_agblock_t fbno; |
| 176 | xfs_extlen_t flen; |
| 177 | int error = 0; |
| 178 | |
| 179 | /* Holes, unwritten, and delalloc extents cannot be shared */ |
| 180 | if (!xfs_is_reflink_inode(ip) || |
| 181 | ISUNWRITTEN(irec) || |
| 182 | irec->br_startblock == HOLESTARTBLOCK || |
| 183 | irec->br_startblock == DELAYSTARTBLOCK) { |
| 184 | *shared = false; |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | trace_xfs_reflink_trim_around_shared(ip, irec); |
| 189 | |
| 190 | agno = XFS_FSB_TO_AGNO(ip->i_mount, irec->br_startblock); |
| 191 | agbno = XFS_FSB_TO_AGBNO(ip->i_mount, irec->br_startblock); |
| 192 | aglen = irec->br_blockcount; |
| 193 | |
| 194 | error = xfs_reflink_find_shared(ip->i_mount, agno, agbno, |
| 195 | aglen, &fbno, &flen, true); |
| 196 | if (error) |
| 197 | return error; |
| 198 | |
| 199 | *shared = *trimmed = false; |
| 200 | if (fbno == NULLAGBLOCK) { |
| 201 | /* No shared blocks at all. */ |
| 202 | return 0; |
| 203 | } else if (fbno == agbno) { |
| 204 | /* |
| 205 | * The start of this extent is shared. Truncate the |
| 206 | * mapping at the end of the shared region so that a |
| 207 | * subsequent iteration starts at the start of the |
| 208 | * unshared region. |
| 209 | */ |
| 210 | irec->br_blockcount = flen; |
| 211 | *shared = true; |
| 212 | if (flen != aglen) |
| 213 | *trimmed = true; |
| 214 | return 0; |
| 215 | } else { |
| 216 | /* |
| 217 | * There's a shared extent midway through this extent. |
| 218 | * Truncate the mapping at the start of the shared |
| 219 | * extent so that a subsequent iteration starts at the |
| 220 | * start of the shared region. |
| 221 | */ |
| 222 | irec->br_blockcount = fbno - agbno; |
| 223 | *trimmed = true; |
| 224 | return 0; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /* Create a CoW reservation for a range of blocks within a file. */ |
| 229 | static int |
| 230 | __xfs_reflink_reserve_cow( |
| 231 | struct xfs_inode *ip, |
| 232 | xfs_fileoff_t *offset_fsb, |
Darrick J. Wong | 0613f16 | 2016-10-03 09:11:37 -0700 | [diff] [blame] | 233 | xfs_fileoff_t end_fsb, |
| 234 | bool *skipped) |
Darrick J. Wong | 2a06705 | 2016-10-03 09:11:33 -0700 | [diff] [blame] | 235 | { |
| 236 | struct xfs_bmbt_irec got, prev, imap; |
| 237 | xfs_fileoff_t orig_end_fsb; |
| 238 | int nimaps, eof = 0, error = 0; |
| 239 | bool shared = false, trimmed = false; |
| 240 | xfs_extnum_t idx; |
| 241 | |
| 242 | /* Already reserved? Skip the refcount btree access. */ |
| 243 | xfs_bmap_search_extents(ip, *offset_fsb, XFS_COW_FORK, &eof, &idx, |
| 244 | &got, &prev); |
| 245 | if (!eof && got.br_startoff <= *offset_fsb) { |
| 246 | end_fsb = orig_end_fsb = got.br_startoff + got.br_blockcount; |
| 247 | trace_xfs_reflink_cow_found(ip, &got); |
| 248 | goto done; |
| 249 | } |
| 250 | |
| 251 | /* Read extent from the source file. */ |
| 252 | nimaps = 1; |
| 253 | error = xfs_bmapi_read(ip, *offset_fsb, end_fsb - *offset_fsb, |
| 254 | &imap, &nimaps, 0); |
| 255 | if (error) |
| 256 | goto out_unlock; |
| 257 | ASSERT(nimaps == 1); |
| 258 | |
| 259 | /* Trim the mapping to the nearest shared extent boundary. */ |
| 260 | error = xfs_reflink_trim_around_shared(ip, &imap, &shared, &trimmed); |
| 261 | if (error) |
| 262 | goto out_unlock; |
| 263 | |
| 264 | end_fsb = orig_end_fsb = imap.br_startoff + imap.br_blockcount; |
| 265 | |
| 266 | /* Not shared? Just report the (potentially capped) extent. */ |
Darrick J. Wong | 0613f16 | 2016-10-03 09:11:37 -0700 | [diff] [blame] | 267 | if (!shared) { |
| 268 | *skipped = true; |
Darrick J. Wong | 2a06705 | 2016-10-03 09:11:33 -0700 | [diff] [blame] | 269 | goto done; |
Darrick J. Wong | 0613f16 | 2016-10-03 09:11:37 -0700 | [diff] [blame] | 270 | } |
Darrick J. Wong | 2a06705 | 2016-10-03 09:11:33 -0700 | [diff] [blame] | 271 | |
| 272 | /* |
| 273 | * Fork all the shared blocks from our write offset until the end of |
| 274 | * the extent. |
| 275 | */ |
| 276 | error = xfs_qm_dqattach_locked(ip, 0); |
| 277 | if (error) |
| 278 | goto out_unlock; |
| 279 | |
| 280 | retry: |
| 281 | error = xfs_bmapi_reserve_delalloc(ip, XFS_COW_FORK, *offset_fsb, |
| 282 | end_fsb - *offset_fsb, &got, |
| 283 | &prev, &idx, eof); |
| 284 | switch (error) { |
| 285 | case 0: |
| 286 | break; |
| 287 | case -ENOSPC: |
| 288 | case -EDQUOT: |
| 289 | /* retry without any preallocation */ |
| 290 | trace_xfs_reflink_cow_enospc(ip, &imap); |
| 291 | if (end_fsb != orig_end_fsb) { |
| 292 | end_fsb = orig_end_fsb; |
| 293 | goto retry; |
| 294 | } |
| 295 | /*FALLTHRU*/ |
| 296 | default: |
| 297 | goto out_unlock; |
| 298 | } |
| 299 | |
| 300 | trace_xfs_reflink_cow_alloc(ip, &got); |
| 301 | done: |
| 302 | *offset_fsb = end_fsb; |
| 303 | out_unlock: |
| 304 | return error; |
| 305 | } |
| 306 | |
| 307 | /* Create a CoW reservation for part of a file. */ |
| 308 | int |
| 309 | xfs_reflink_reserve_cow_range( |
| 310 | struct xfs_inode *ip, |
| 311 | xfs_off_t offset, |
| 312 | xfs_off_t count) |
| 313 | { |
| 314 | struct xfs_mount *mp = ip->i_mount; |
| 315 | xfs_fileoff_t offset_fsb, end_fsb; |
Darrick J. Wong | 0613f16 | 2016-10-03 09:11:37 -0700 | [diff] [blame] | 316 | bool skipped = false; |
Darrick J. Wong | 2a06705 | 2016-10-03 09:11:33 -0700 | [diff] [blame] | 317 | int error; |
| 318 | |
| 319 | trace_xfs_reflink_reserve_cow_range(ip, offset, count); |
| 320 | |
| 321 | offset_fsb = XFS_B_TO_FSBT(mp, offset); |
| 322 | end_fsb = XFS_B_TO_FSB(mp, offset + count); |
| 323 | |
| 324 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 325 | while (offset_fsb < end_fsb) { |
Darrick J. Wong | 0613f16 | 2016-10-03 09:11:37 -0700 | [diff] [blame] | 326 | error = __xfs_reflink_reserve_cow(ip, &offset_fsb, end_fsb, |
| 327 | &skipped); |
Darrick J. Wong | 2a06705 | 2016-10-03 09:11:33 -0700 | [diff] [blame] | 328 | if (error) { |
| 329 | trace_xfs_reflink_reserve_cow_range_error(ip, error, |
| 330 | _RET_IP_); |
| 331 | break; |
| 332 | } |
| 333 | } |
| 334 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 335 | |
| 336 | return error; |
| 337 | } |
Darrick J. Wong | ef47366 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 338 | |
Darrick J. Wong | 0613f16 | 2016-10-03 09:11:37 -0700 | [diff] [blame] | 339 | /* Allocate all CoW reservations covering a range of blocks in a file. */ |
| 340 | static int |
| 341 | __xfs_reflink_allocate_cow( |
| 342 | struct xfs_inode *ip, |
| 343 | xfs_fileoff_t *offset_fsb, |
| 344 | xfs_fileoff_t end_fsb) |
| 345 | { |
| 346 | struct xfs_mount *mp = ip->i_mount; |
| 347 | struct xfs_bmbt_irec imap; |
| 348 | struct xfs_defer_ops dfops; |
| 349 | struct xfs_trans *tp; |
| 350 | xfs_fsblock_t first_block; |
| 351 | xfs_fileoff_t next_fsb; |
| 352 | int nimaps = 1, error; |
| 353 | bool skipped = false; |
| 354 | |
| 355 | xfs_defer_init(&dfops, &first_block); |
| 356 | |
| 357 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, |
| 358 | XFS_TRANS_RESERVE, &tp); |
| 359 | if (error) |
| 360 | return error; |
| 361 | |
| 362 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 363 | |
| 364 | next_fsb = *offset_fsb; |
| 365 | error = __xfs_reflink_reserve_cow(ip, &next_fsb, end_fsb, &skipped); |
| 366 | if (error) |
| 367 | goto out_trans_cancel; |
| 368 | |
| 369 | if (skipped) { |
| 370 | *offset_fsb = next_fsb; |
| 371 | goto out_trans_cancel; |
| 372 | } |
| 373 | |
| 374 | xfs_trans_ijoin(tp, ip, 0); |
| 375 | error = xfs_bmapi_write(tp, ip, *offset_fsb, next_fsb - *offset_fsb, |
| 376 | XFS_BMAPI_COWFORK, &first_block, |
| 377 | XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK), |
| 378 | &imap, &nimaps, &dfops); |
| 379 | if (error) |
| 380 | goto out_trans_cancel; |
| 381 | |
| 382 | /* We might not have been able to map the whole delalloc extent */ |
| 383 | *offset_fsb = min(*offset_fsb + imap.br_blockcount, next_fsb); |
| 384 | |
| 385 | error = xfs_defer_finish(&tp, &dfops, NULL); |
| 386 | if (error) |
| 387 | goto out_trans_cancel; |
| 388 | |
| 389 | error = xfs_trans_commit(tp); |
| 390 | |
| 391 | out_unlock: |
| 392 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 393 | return error; |
| 394 | out_trans_cancel: |
| 395 | xfs_defer_cancel(&dfops); |
| 396 | xfs_trans_cancel(tp); |
| 397 | goto out_unlock; |
| 398 | } |
| 399 | |
| 400 | /* Allocate all CoW reservations covering a part of a file. */ |
| 401 | int |
| 402 | xfs_reflink_allocate_cow_range( |
| 403 | struct xfs_inode *ip, |
| 404 | xfs_off_t offset, |
| 405 | xfs_off_t count) |
| 406 | { |
| 407 | struct xfs_mount *mp = ip->i_mount; |
| 408 | xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset); |
| 409 | xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count); |
| 410 | int error; |
| 411 | |
| 412 | ASSERT(xfs_is_reflink_inode(ip)); |
| 413 | |
| 414 | trace_xfs_reflink_allocate_cow_range(ip, offset, count); |
| 415 | |
| 416 | /* |
| 417 | * Make sure that the dquots are there. |
| 418 | */ |
| 419 | error = xfs_qm_dqattach(ip, 0); |
| 420 | if (error) |
| 421 | return error; |
| 422 | |
| 423 | while (offset_fsb < end_fsb) { |
| 424 | error = __xfs_reflink_allocate_cow(ip, &offset_fsb, end_fsb); |
| 425 | if (error) { |
| 426 | trace_xfs_reflink_allocate_cow_range_error(ip, error, |
| 427 | _RET_IP_); |
| 428 | break; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | return error; |
| 433 | } |
| 434 | |
Darrick J. Wong | ef47366 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 435 | /* |
| 436 | * Find the CoW reservation (and whether or not it needs block allocation) |
| 437 | * for a given byte offset of a file. |
| 438 | */ |
| 439 | bool |
| 440 | xfs_reflink_find_cow_mapping( |
| 441 | struct xfs_inode *ip, |
| 442 | xfs_off_t offset, |
| 443 | struct xfs_bmbt_irec *imap, |
| 444 | bool *need_alloc) |
| 445 | { |
| 446 | struct xfs_bmbt_irec irec; |
| 447 | struct xfs_ifork *ifp; |
| 448 | struct xfs_bmbt_rec_host *gotp; |
| 449 | xfs_fileoff_t bno; |
| 450 | xfs_extnum_t idx; |
| 451 | |
| 452 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED)); |
| 453 | ASSERT(xfs_is_reflink_inode(ip)); |
| 454 | |
| 455 | /* Find the extent in the CoW fork. */ |
| 456 | ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK); |
| 457 | bno = XFS_B_TO_FSBT(ip->i_mount, offset); |
| 458 | gotp = xfs_iext_bno_to_ext(ifp, bno, &idx); |
| 459 | if (!gotp) |
| 460 | return false; |
| 461 | |
| 462 | xfs_bmbt_get_all(gotp, &irec); |
| 463 | if (bno >= irec.br_startoff + irec.br_blockcount || |
| 464 | bno < irec.br_startoff) |
| 465 | return false; |
| 466 | |
| 467 | trace_xfs_reflink_find_cow_mapping(ip, offset, 1, XFS_IO_OVERWRITE, |
| 468 | &irec); |
| 469 | |
| 470 | /* If it's still delalloc, we must allocate later. */ |
| 471 | *imap = irec; |
| 472 | *need_alloc = !!(isnullstartblock(irec.br_startblock)); |
| 473 | |
| 474 | return true; |
| 475 | } |
| 476 | |
| 477 | /* |
| 478 | * Trim an extent to end at the next CoW reservation past offset_fsb. |
| 479 | */ |
| 480 | int |
| 481 | xfs_reflink_trim_irec_to_next_cow( |
| 482 | struct xfs_inode *ip, |
| 483 | xfs_fileoff_t offset_fsb, |
| 484 | struct xfs_bmbt_irec *imap) |
| 485 | { |
| 486 | struct xfs_bmbt_irec irec; |
| 487 | struct xfs_ifork *ifp; |
| 488 | struct xfs_bmbt_rec_host *gotp; |
| 489 | xfs_extnum_t idx; |
| 490 | |
| 491 | if (!xfs_is_reflink_inode(ip)) |
| 492 | return 0; |
| 493 | |
| 494 | /* Find the extent in the CoW fork. */ |
| 495 | ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK); |
| 496 | gotp = xfs_iext_bno_to_ext(ifp, offset_fsb, &idx); |
| 497 | if (!gotp) |
| 498 | return 0; |
| 499 | xfs_bmbt_get_all(gotp, &irec); |
| 500 | |
| 501 | /* This is the extent before; try sliding up one. */ |
| 502 | if (irec.br_startoff < offset_fsb) { |
| 503 | idx++; |
| 504 | if (idx >= ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) |
| 505 | return 0; |
| 506 | gotp = xfs_iext_get_ext(ifp, idx); |
| 507 | xfs_bmbt_get_all(gotp, &irec); |
| 508 | } |
| 509 | |
| 510 | if (irec.br_startoff >= imap->br_startoff + imap->br_blockcount) |
| 511 | return 0; |
| 512 | |
| 513 | imap->br_blockcount = irec.br_startoff - imap->br_startoff; |
| 514 | trace_xfs_reflink_trim_irec(ip, imap); |
| 515 | |
| 516 | return 0; |
| 517 | } |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 518 | |
| 519 | /* |
| 520 | * Cancel all pending CoW reservations for some block range of an inode. |
| 521 | */ |
| 522 | int |
| 523 | xfs_reflink_cancel_cow_blocks( |
| 524 | struct xfs_inode *ip, |
| 525 | struct xfs_trans **tpp, |
| 526 | xfs_fileoff_t offset_fsb, |
| 527 | xfs_fileoff_t end_fsb) |
| 528 | { |
| 529 | struct xfs_bmbt_irec irec; |
| 530 | xfs_filblks_t count_fsb; |
| 531 | xfs_fsblock_t firstfsb; |
| 532 | struct xfs_defer_ops dfops; |
| 533 | int error = 0; |
| 534 | int nimaps; |
| 535 | |
| 536 | if (!xfs_is_reflink_inode(ip)) |
| 537 | return 0; |
| 538 | |
| 539 | /* Go find the old extent in the CoW fork. */ |
| 540 | while (offset_fsb < end_fsb) { |
| 541 | nimaps = 1; |
| 542 | count_fsb = (xfs_filblks_t)(end_fsb - offset_fsb); |
| 543 | error = xfs_bmapi_read(ip, offset_fsb, count_fsb, &irec, |
| 544 | &nimaps, XFS_BMAPI_COWFORK); |
| 545 | if (error) |
| 546 | break; |
| 547 | ASSERT(nimaps == 1); |
| 548 | |
| 549 | trace_xfs_reflink_cancel_cow(ip, &irec); |
| 550 | |
| 551 | if (irec.br_startblock == DELAYSTARTBLOCK) { |
| 552 | /* Free a delayed allocation. */ |
| 553 | xfs_mod_fdblocks(ip->i_mount, irec.br_blockcount, |
| 554 | false); |
| 555 | ip->i_delayed_blks -= irec.br_blockcount; |
| 556 | |
| 557 | /* Remove the mapping from the CoW fork. */ |
| 558 | error = xfs_bunmapi_cow(ip, &irec); |
| 559 | if (error) |
| 560 | break; |
| 561 | } else if (irec.br_startblock == HOLESTARTBLOCK) { |
| 562 | /* empty */ |
| 563 | } else { |
| 564 | xfs_trans_ijoin(*tpp, ip, 0); |
| 565 | xfs_defer_init(&dfops, &firstfsb); |
| 566 | |
Darrick J. Wong | 174edb0 | 2016-10-03 09:11:39 -0700 | [diff] [blame^] | 567 | /* Free the CoW orphan record. */ |
| 568 | error = xfs_refcount_free_cow_extent(ip->i_mount, |
| 569 | &dfops, irec.br_startblock, |
| 570 | irec.br_blockcount); |
| 571 | if (error) |
| 572 | break; |
| 573 | |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 574 | xfs_bmap_add_free(ip->i_mount, &dfops, |
| 575 | irec.br_startblock, irec.br_blockcount, |
| 576 | NULL); |
| 577 | |
| 578 | /* Update quota accounting */ |
| 579 | xfs_trans_mod_dquot_byino(*tpp, ip, XFS_TRANS_DQ_BCOUNT, |
| 580 | -(long)irec.br_blockcount); |
| 581 | |
| 582 | /* Roll the transaction */ |
| 583 | error = xfs_defer_finish(tpp, &dfops, ip); |
| 584 | if (error) { |
| 585 | xfs_defer_cancel(&dfops); |
| 586 | break; |
| 587 | } |
| 588 | |
| 589 | /* Remove the mapping from the CoW fork. */ |
| 590 | error = xfs_bunmapi_cow(ip, &irec); |
| 591 | if (error) |
| 592 | break; |
| 593 | } |
| 594 | |
| 595 | /* Roll on... */ |
| 596 | offset_fsb = irec.br_startoff + irec.br_blockcount; |
| 597 | } |
| 598 | |
| 599 | return error; |
| 600 | } |
| 601 | |
| 602 | /* |
| 603 | * Cancel all pending CoW reservations for some byte range of an inode. |
| 604 | */ |
| 605 | int |
| 606 | xfs_reflink_cancel_cow_range( |
| 607 | struct xfs_inode *ip, |
| 608 | xfs_off_t offset, |
| 609 | xfs_off_t count) |
| 610 | { |
| 611 | struct xfs_trans *tp; |
| 612 | xfs_fileoff_t offset_fsb; |
| 613 | xfs_fileoff_t end_fsb; |
| 614 | int error; |
| 615 | |
| 616 | trace_xfs_reflink_cancel_cow_range(ip, offset, count); |
| 617 | |
| 618 | offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset); |
| 619 | if (count == NULLFILEOFF) |
| 620 | end_fsb = NULLFILEOFF; |
| 621 | else |
| 622 | end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count); |
| 623 | |
| 624 | /* Start a rolling transaction to remove the mappings */ |
| 625 | error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write, |
| 626 | 0, 0, 0, &tp); |
| 627 | if (error) |
| 628 | goto out; |
| 629 | |
| 630 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 631 | xfs_trans_ijoin(tp, ip, 0); |
| 632 | |
| 633 | /* Scrape out the old CoW reservations */ |
| 634 | error = xfs_reflink_cancel_cow_blocks(ip, &tp, offset_fsb, end_fsb); |
| 635 | if (error) |
| 636 | goto out_cancel; |
| 637 | |
| 638 | error = xfs_trans_commit(tp); |
| 639 | |
| 640 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 641 | return error; |
| 642 | |
| 643 | out_cancel: |
| 644 | xfs_trans_cancel(tp); |
| 645 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 646 | out: |
| 647 | trace_xfs_reflink_cancel_cow_range_error(ip, error, _RET_IP_); |
| 648 | return error; |
| 649 | } |
| 650 | |
| 651 | /* |
| 652 | * Remap parts of a file's data fork after a successful CoW. |
| 653 | */ |
| 654 | int |
| 655 | xfs_reflink_end_cow( |
| 656 | struct xfs_inode *ip, |
| 657 | xfs_off_t offset, |
| 658 | xfs_off_t count) |
| 659 | { |
| 660 | struct xfs_bmbt_irec irec; |
| 661 | struct xfs_bmbt_irec uirec; |
| 662 | struct xfs_trans *tp; |
| 663 | xfs_fileoff_t offset_fsb; |
| 664 | xfs_fileoff_t end_fsb; |
| 665 | xfs_filblks_t count_fsb; |
| 666 | xfs_fsblock_t firstfsb; |
| 667 | struct xfs_defer_ops dfops; |
| 668 | int error; |
| 669 | unsigned int resblks; |
| 670 | xfs_filblks_t ilen; |
| 671 | xfs_filblks_t rlen; |
| 672 | int nimaps; |
| 673 | |
| 674 | trace_xfs_reflink_end_cow(ip, offset, count); |
| 675 | |
| 676 | offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset); |
| 677 | end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count); |
| 678 | count_fsb = (xfs_filblks_t)(end_fsb - offset_fsb); |
| 679 | |
| 680 | /* Start a rolling transaction to switch the mappings */ |
| 681 | resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK); |
| 682 | error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write, |
| 683 | resblks, 0, 0, &tp); |
| 684 | if (error) |
| 685 | goto out; |
| 686 | |
| 687 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 688 | xfs_trans_ijoin(tp, ip, 0); |
| 689 | |
| 690 | /* Go find the old extent in the CoW fork. */ |
| 691 | while (offset_fsb < end_fsb) { |
| 692 | /* Read extent from the source file */ |
| 693 | nimaps = 1; |
| 694 | count_fsb = (xfs_filblks_t)(end_fsb - offset_fsb); |
| 695 | error = xfs_bmapi_read(ip, offset_fsb, count_fsb, &irec, |
| 696 | &nimaps, XFS_BMAPI_COWFORK); |
| 697 | if (error) |
| 698 | goto out_cancel; |
| 699 | ASSERT(nimaps == 1); |
| 700 | |
| 701 | ASSERT(irec.br_startblock != DELAYSTARTBLOCK); |
| 702 | trace_xfs_reflink_cow_remap(ip, &irec); |
| 703 | |
| 704 | /* |
| 705 | * We can have a hole in the CoW fork if part of a directio |
| 706 | * write is CoW but part of it isn't. |
| 707 | */ |
| 708 | rlen = ilen = irec.br_blockcount; |
| 709 | if (irec.br_startblock == HOLESTARTBLOCK) |
| 710 | goto next_extent; |
| 711 | |
| 712 | /* Unmap the old blocks in the data fork. */ |
| 713 | while (rlen) { |
| 714 | xfs_defer_init(&dfops, &firstfsb); |
| 715 | error = __xfs_bunmapi(tp, ip, irec.br_startoff, |
| 716 | &rlen, 0, 1, &firstfsb, &dfops); |
| 717 | if (error) |
| 718 | goto out_defer; |
| 719 | |
| 720 | /* |
| 721 | * Trim the extent to whatever got unmapped. |
| 722 | * Remember, bunmapi works backwards. |
| 723 | */ |
| 724 | uirec.br_startblock = irec.br_startblock + rlen; |
| 725 | uirec.br_startoff = irec.br_startoff + rlen; |
| 726 | uirec.br_blockcount = irec.br_blockcount - rlen; |
| 727 | irec.br_blockcount = rlen; |
| 728 | trace_xfs_reflink_cow_remap_piece(ip, &uirec); |
| 729 | |
Darrick J. Wong | 174edb0 | 2016-10-03 09:11:39 -0700 | [diff] [blame^] | 730 | /* Free the CoW orphan record. */ |
| 731 | error = xfs_refcount_free_cow_extent(tp->t_mountp, |
| 732 | &dfops, uirec.br_startblock, |
| 733 | uirec.br_blockcount); |
| 734 | if (error) |
| 735 | goto out_defer; |
| 736 | |
Darrick J. Wong | 43caeb1 | 2016-10-03 09:11:35 -0700 | [diff] [blame] | 737 | /* Map the new blocks into the data fork. */ |
| 738 | error = xfs_bmap_map_extent(tp->t_mountp, &dfops, |
| 739 | ip, &uirec); |
| 740 | if (error) |
| 741 | goto out_defer; |
| 742 | |
| 743 | /* Remove the mapping from the CoW fork. */ |
| 744 | error = xfs_bunmapi_cow(ip, &uirec); |
| 745 | if (error) |
| 746 | goto out_defer; |
| 747 | |
| 748 | error = xfs_defer_finish(&tp, &dfops, ip); |
| 749 | if (error) |
| 750 | goto out_defer; |
| 751 | } |
| 752 | |
| 753 | next_extent: |
| 754 | /* Roll on... */ |
| 755 | offset_fsb = irec.br_startoff + ilen; |
| 756 | } |
| 757 | |
| 758 | error = xfs_trans_commit(tp); |
| 759 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 760 | if (error) |
| 761 | goto out; |
| 762 | return 0; |
| 763 | |
| 764 | out_defer: |
| 765 | xfs_defer_cancel(&dfops); |
| 766 | out_cancel: |
| 767 | xfs_trans_cancel(tp); |
| 768 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 769 | out: |
| 770 | trace_xfs_reflink_end_cow_error(ip, error, _RET_IP_); |
| 771 | return error; |
| 772 | } |
Darrick J. Wong | 174edb0 | 2016-10-03 09:11:39 -0700 | [diff] [blame^] | 773 | |
| 774 | /* |
| 775 | * Free leftover CoW reservations that didn't get cleaned out. |
| 776 | */ |
| 777 | int |
| 778 | xfs_reflink_recover_cow( |
| 779 | struct xfs_mount *mp) |
| 780 | { |
| 781 | xfs_agnumber_t agno; |
| 782 | int error = 0; |
| 783 | |
| 784 | if (!xfs_sb_version_hasreflink(&mp->m_sb)) |
| 785 | return 0; |
| 786 | |
| 787 | for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) { |
| 788 | error = xfs_refcount_recover_cow_leftovers(mp, agno); |
| 789 | if (error) |
| 790 | break; |
| 791 | } |
| 792 | |
| 793 | return error; |
| 794 | } |