1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2000-2006 Silicon Graphics, Inc. 4 * All Rights Reserved. 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_bit.h" 13 #include "xfs_sb.h" 14 #include "xfs_mount.h" 15 #include "xfs_defer.h" 16 #include "xfs_dir2.h" 17 #include "xfs_inode.h" 18 #include "xfs_btree.h" 19 #include "xfs_trans.h" 20 #include "xfs_alloc.h" 21 #include "xfs_bmap.h" 22 #include "xfs_bmap_util.h" 23 #include "xfs_bmap_btree.h" 24 #include "xfs_rtbitmap.h" 25 #include "xfs_errortag.h" 26 #include "xfs_error.h" 27 #include "xfs_quota.h" 28 #include "xfs_trans_space.h" 29 #include "xfs_buf_item.h" 30 #include "xfs_trace.h" 31 #include "xfs_attr_leaf.h" 32 #include "xfs_filestream.h" 33 #include "xfs_rmap.h" 34 #include "xfs_ag.h" 35 #include "xfs_ag_resv.h" 36 #include "xfs_refcount.h" 37 #include "xfs_iomap.h" 38 #include "xfs_health.h" 39 #include "xfs_bmap_item.h" 40 #include "xfs_symlink_remote.h" 41 #include "xfs_inode_util.h" 42 #include "xfs_rtgroup.h" 43 #include "xfs_zone_alloc.h" 44 45 struct kmem_cache *xfs_bmap_intent_cache; 46 47 /* 48 * Miscellaneous helper functions 49 */ 50 51 /* 52 * Compute and fill in the value of the maximum depth of a bmap btree 53 * in this filesystem. Done once, during mount. 54 */ 55 void 56 xfs_bmap_compute_maxlevels( 57 xfs_mount_t *mp, /* file system mount structure */ 58 int whichfork) /* data or attr fork */ 59 { 60 uint64_t maxblocks; /* max blocks at this level */ 61 xfs_extnum_t maxleafents; /* max leaf entries possible */ 62 int level; /* btree level */ 63 int maxrootrecs; /* max records in root block */ 64 int minleafrecs; /* min records in leaf block */ 65 int minnoderecs; /* min records in node block */ 66 int sz; /* root block size */ 67 68 /* 69 * The maximum number of extents in a fork, hence the maximum number of 70 * leaf entries, is controlled by the size of the on-disk extent count. 71 * 72 * Note that we can no longer assume that if we are in ATTR1 that the 73 * fork offset of all the inodes will be 74 * (xfs_default_attroffset(ip) >> 3) because we could have mounted with 75 * ATTR2 and then mounted back with ATTR1, keeping the i_forkoff's fixed 76 * but probably at various positions. Therefore, for both ATTR1 and 77 * ATTR2 we have to assume the worst case scenario of a minimum size 78 * available. 79 */ 80 maxleafents = xfs_iext_max_nextents(xfs_has_large_extent_counts(mp), 81 whichfork); 82 if (whichfork == XFS_DATA_FORK) 83 sz = xfs_bmdr_space_calc(MINDBTPTRS); 84 else 85 sz = xfs_bmdr_space_calc(MINABTPTRS); 86 87 maxrootrecs = xfs_bmdr_maxrecs(sz, 0); 88 minleafrecs = mp->m_bmap_dmnr[0]; 89 minnoderecs = mp->m_bmap_dmnr[1]; 90 maxblocks = howmany_64(maxleafents, minleafrecs); 91 for (level = 1; maxblocks > 1; level++) { 92 if (maxblocks <= maxrootrecs) 93 maxblocks = 1; 94 else 95 maxblocks = howmany_64(maxblocks, minnoderecs); 96 } 97 mp->m_bm_maxlevels[whichfork] = level; 98 ASSERT(mp->m_bm_maxlevels[whichfork] <= xfs_bmbt_maxlevels_ondisk()); 99 } 100 101 unsigned int 102 xfs_bmap_compute_attr_offset( 103 struct xfs_mount *mp) 104 { 105 if (mp->m_sb.sb_inodesize == 256) 106 return XFS_LITINO(mp) - xfs_bmdr_space_calc(MINABTPTRS); 107 return xfs_bmdr_space_calc(6 * MINABTPTRS); 108 } 109 110 STATIC int /* error */ 111 xfs_bmbt_lookup_eq( 112 struct xfs_btree_cur *cur, 113 struct xfs_bmbt_irec *irec, 114 int *stat) /* success/failure */ 115 { 116 cur->bc_rec.b = *irec; 117 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat); 118 } 119 120 STATIC int /* error */ 121 xfs_bmbt_lookup_first( 122 struct xfs_btree_cur *cur, 123 int *stat) /* success/failure */ 124 { 125 cur->bc_rec.b.br_startoff = 0; 126 cur->bc_rec.b.br_startblock = 0; 127 cur->bc_rec.b.br_blockcount = 0; 128 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat); 129 } 130 131 /* 132 * Check if the inode needs to be converted to btree format. 133 */ 134 static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork) 135 { 136 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); 137 138 return whichfork != XFS_COW_FORK && 139 ifp->if_format == XFS_DINODE_FMT_EXTENTS && 140 ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork); 141 } 142 143 /* 144 * Check if the inode should be converted to extent format. 145 */ 146 static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork) 147 { 148 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); 149 150 return whichfork != XFS_COW_FORK && 151 ifp->if_format == XFS_DINODE_FMT_BTREE && 152 ifp->if_nextents <= XFS_IFORK_MAXEXT(ip, whichfork); 153 } 154 155 /* 156 * Update the record referred to by cur to the value given by irec 157 * This either works (return 0) or gets an EFSCORRUPTED error. 158 */ 159 STATIC int 160 xfs_bmbt_update( 161 struct xfs_btree_cur *cur, 162 struct xfs_bmbt_irec *irec) 163 { 164 union xfs_btree_rec rec; 165 166 xfs_bmbt_disk_set_all(&rec.bmbt, irec); 167 return xfs_btree_update(cur, &rec); 168 } 169 170 /* 171 * Compute the worst-case number of indirect blocks that will be used 172 * for ip's delayed extent of length "len". 173 */ 174 xfs_filblks_t 175 xfs_bmap_worst_indlen( 176 struct xfs_inode *ip, /* incore inode pointer */ 177 xfs_filblks_t len) /* delayed extent length */ 178 { 179 struct xfs_mount *mp = ip->i_mount; 180 int maxrecs = mp->m_bmap_dmxr[0]; 181 int level; 182 xfs_filblks_t rval; 183 184 for (level = 0, rval = 0; 185 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK); 186 level++) { 187 len += maxrecs - 1; 188 do_div(len, maxrecs); 189 rval += len; 190 if (len == 1) 191 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) - 192 level - 1; 193 if (level == 0) 194 maxrecs = mp->m_bmap_dmxr[1]; 195 } 196 return rval; 197 } 198 199 /* 200 * Calculate the default attribute fork offset for newly created inodes. 201 */ 202 uint 203 xfs_default_attroffset( 204 struct xfs_inode *ip) 205 { 206 if (ip->i_df.if_format == XFS_DINODE_FMT_DEV) 207 return roundup(sizeof(xfs_dev_t), 8); 208 return M_IGEO(ip->i_mount)->attr_fork_offset; 209 } 210 211 /* 212 * Helper routine to reset inode i_forkoff field when switching attribute fork 213 * from local to extent format - we reset it where possible to make space 214 * available for inline data fork extents. 215 */ 216 STATIC void 217 xfs_bmap_forkoff_reset( 218 xfs_inode_t *ip, 219 int whichfork) 220 { 221 if (whichfork == XFS_ATTR_FORK && 222 ip->i_df.if_format != XFS_DINODE_FMT_DEV && 223 ip->i_df.if_format != XFS_DINODE_FMT_BTREE) { 224 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3; 225 226 if (dfl_forkoff > ip->i_forkoff) 227 ip->i_forkoff = dfl_forkoff; 228 } 229 } 230 231 static int 232 xfs_bmap_read_buf( 233 struct xfs_mount *mp, /* file system mount point */ 234 struct xfs_trans *tp, /* transaction pointer */ 235 xfs_fsblock_t fsbno, /* file system block number */ 236 struct xfs_buf **bpp) /* buffer for fsbno */ 237 { 238 struct xfs_buf *bp; /* return value */ 239 int error; 240 241 if (!xfs_verify_fsbno(mp, fsbno)) 242 return -EFSCORRUPTED; 243 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, 244 XFS_FSB_TO_DADDR(mp, fsbno), mp->m_bsize, 0, &bp, 245 &xfs_bmbt_buf_ops); 246 if (!error) { 247 xfs_buf_set_ref(bp, XFS_BMAP_BTREE_REF); 248 *bpp = bp; 249 } 250 return error; 251 } 252 253 #ifdef DEBUG 254 STATIC struct xfs_buf * 255 xfs_bmap_get_bp( 256 struct xfs_btree_cur *cur, 257 xfs_fsblock_t bno) 258 { 259 struct xfs_log_item *lip; 260 int i; 261 262 if (!cur) 263 return NULL; 264 265 for (i = 0; i < cur->bc_maxlevels; i++) { 266 if (!cur->bc_levels[i].bp) 267 break; 268 if (xfs_buf_daddr(cur->bc_levels[i].bp) == bno) 269 return cur->bc_levels[i].bp; 270 } 271 272 /* Chase down all the log items to see if the bp is there */ 273 list_for_each_entry(lip, &cur->bc_tp->t_items, li_trans) { 274 struct xfs_buf_log_item *bip = (struct xfs_buf_log_item *)lip; 275 276 if (bip->bli_item.li_type == XFS_LI_BUF && 277 xfs_buf_daddr(bip->bli_buf) == bno) 278 return bip->bli_buf; 279 } 280 281 return NULL; 282 } 283 284 STATIC void 285 xfs_check_block( 286 struct xfs_btree_block *block, 287 xfs_mount_t *mp, 288 int root, 289 short sz) 290 { 291 int i, j, dmxr; 292 __be64 *pp, *thispa; /* pointer to block address */ 293 xfs_bmbt_key_t *prevp, *keyp; 294 295 ASSERT(be16_to_cpu(block->bb_level) > 0); 296 297 prevp = NULL; 298 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) { 299 dmxr = mp->m_bmap_dmxr[0]; 300 keyp = xfs_bmbt_key_addr(mp, block, i); 301 302 if (prevp) { 303 ASSERT(be64_to_cpu(prevp->br_startoff) < 304 be64_to_cpu(keyp->br_startoff)); 305 } 306 prevp = keyp; 307 308 /* 309 * Compare the block numbers to see if there are dups. 310 */ 311 if (root) 312 pp = xfs_bmap_broot_ptr_addr(mp, block, i, sz); 313 else 314 pp = xfs_bmbt_ptr_addr(mp, block, i, dmxr); 315 316 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) { 317 if (root) 318 thispa = xfs_bmap_broot_ptr_addr(mp, block, j, sz); 319 else 320 thispa = xfs_bmbt_ptr_addr(mp, block, j, dmxr); 321 if (*thispa == *pp) { 322 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %lld", 323 __func__, j, i, 324 (unsigned long long)be64_to_cpu(*thispa)); 325 xfs_err(mp, "%s: ptrs are equal in node\n", 326 __func__); 327 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); 328 } 329 } 330 } 331 } 332 333 /* 334 * Check that the extents for the inode ip are in the right order in all 335 * btree leaves. THis becomes prohibitively expensive for large extent count 336 * files, so don't bother with inodes that have more than 10,000 extents in 337 * them. The btree record ordering checks will still be done, so for such large 338 * bmapbt constructs that is going to catch most corruptions. 339 */ 340 STATIC void 341 xfs_bmap_check_leaf_extents( 342 struct xfs_btree_cur *cur, /* btree cursor or null */ 343 xfs_inode_t *ip, /* incore inode pointer */ 344 int whichfork) /* data or attr fork */ 345 { 346 struct xfs_mount *mp = ip->i_mount; 347 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); 348 struct xfs_btree_block *block; /* current btree block */ 349 xfs_fsblock_t bno; /* block # of "block" */ 350 struct xfs_buf *bp; /* buffer for "block" */ 351 int error; /* error return value */ 352 xfs_extnum_t i=0, j; /* index into the extents list */ 353 int level; /* btree level, for checking */ 354 __be64 *pp; /* pointer to block address */ 355 xfs_bmbt_rec_t *ep; /* pointer to current extent */ 356 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */ 357 xfs_bmbt_rec_t *nextp; /* pointer to next extent */ 358 int bp_release = 0; 359 360 if (ifp->if_format != XFS_DINODE_FMT_BTREE) 361 return; 362 363 /* skip large extent count inodes */ 364 if (ip->i_df.if_nextents > 10000) 365 return; 366 367 bno = NULLFSBLOCK; 368 block = ifp->if_broot; 369 /* 370 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. 371 */ 372 level = be16_to_cpu(block->bb_level); 373 ASSERT(level > 0); 374 xfs_check_block(block, mp, 1, ifp->if_broot_bytes); 375 pp = xfs_bmap_broot_ptr_addr(mp, block, 1, ifp->if_broot_bytes); 376 bno = be64_to_cpu(*pp); 377 378 ASSERT(bno != NULLFSBLOCK); 379 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); 380 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); 381 382 /* 383 * Go down the tree until leaf level is reached, following the first 384 * pointer (leftmost) at each level. 385 */ 386 while (level-- > 0) { 387 /* See if buf is in cur first */ 388 bp_release = 0; 389 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); 390 if (!bp) { 391 bp_release = 1; 392 error = xfs_bmap_read_buf(mp, NULL, bno, &bp); 393 if (xfs_metadata_is_sick(error)) 394 xfs_btree_mark_sick(cur); 395 if (error) 396 goto error_norelse; 397 } 398 block = XFS_BUF_TO_BLOCK(bp); 399 if (level == 0) 400 break; 401 402 /* 403 * Check this block for basic sanity (increasing keys and 404 * no duplicate blocks). 405 */ 406 407 xfs_check_block(block, mp, 0, 0); 408 pp = xfs_bmbt_ptr_addr(mp, block, 1, mp->m_bmap_dmxr[1]); 409 bno = be64_to_cpu(*pp); 410 if (XFS_IS_CORRUPT(mp, !xfs_verify_fsbno(mp, bno))) { 411 xfs_btree_mark_sick(cur); 412 error = -EFSCORRUPTED; 413 goto error0; 414 } 415 if (bp_release) { 416 bp_release = 0; 417 xfs_trans_brelse(NULL, bp); 418 } 419 } 420 421 /* 422 * Here with bp and block set to the leftmost leaf node in the tree. 423 */ 424 i = 0; 425 426 /* 427 * Loop over all leaf nodes checking that all extents are in the right order. 428 */ 429 for (;;) { 430 xfs_fsblock_t nextbno; 431 xfs_extnum_t num_recs; 432 433 434 num_recs = xfs_btree_get_numrecs(block); 435 436 /* 437 * Read-ahead the next leaf block, if any. 438 */ 439 440 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); 441 442 /* 443 * Check all the extents to make sure they are OK. 444 * If we had a previous block, the last entry should 445 * conform with the first entry in this one. 446 */ 447 448 ep = xfs_bmbt_rec_addr(mp, block, 1); 449 if (i) { 450 ASSERT(xfs_bmbt_disk_get_startoff(&last) + 451 xfs_bmbt_disk_get_blockcount(&last) <= 452 xfs_bmbt_disk_get_startoff(ep)); 453 } 454 for (j = 1; j < num_recs; j++) { 455 nextp = xfs_bmbt_rec_addr(mp, block, j + 1); 456 ASSERT(xfs_bmbt_disk_get_startoff(ep) + 457 xfs_bmbt_disk_get_blockcount(ep) <= 458 xfs_bmbt_disk_get_startoff(nextp)); 459 ep = nextp; 460 } 461 462 last = *ep; 463 i += num_recs; 464 if (bp_release) { 465 bp_release = 0; 466 xfs_trans_brelse(NULL, bp); 467 } 468 bno = nextbno; 469 /* 470 * If we've reached the end, stop. 471 */ 472 if (bno == NULLFSBLOCK) 473 break; 474 475 bp_release = 0; 476 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); 477 if (!bp) { 478 bp_release = 1; 479 error = xfs_bmap_read_buf(mp, NULL, bno, &bp); 480 if (xfs_metadata_is_sick(error)) 481 xfs_btree_mark_sick(cur); 482 if (error) 483 goto error_norelse; 484 } 485 block = XFS_BUF_TO_BLOCK(bp); 486 } 487 488 return; 489 490 error0: 491 xfs_warn(mp, "%s: at error0", __func__); 492 if (bp_release) 493 xfs_trans_brelse(NULL, bp); 494 error_norelse: 495 xfs_warn(mp, "%s: BAD after btree leaves for %llu extents", 496 __func__, i); 497 xfs_err(mp, "%s: CORRUPTED BTREE OR SOMETHING", __func__); 498 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); 499 return; 500 } 501 502 /* 503 * Validate that the bmbt_irecs being returned from bmapi are valid 504 * given the caller's original parameters. Specifically check the 505 * ranges of the returned irecs to ensure that they only extend beyond 506 * the given parameters if the XFS_BMAPI_ENTIRE flag was set. 507 */ 508 STATIC void 509 xfs_bmap_validate_ret( 510 xfs_fileoff_t bno, 511 xfs_filblks_t len, 512 uint32_t flags, 513 xfs_bmbt_irec_t *mval, 514 int nmap, 515 int ret_nmap) 516 { 517 int i; /* index to map values */ 518 519 ASSERT(ret_nmap <= nmap); 520 521 for (i = 0; i < ret_nmap; i++) { 522 ASSERT(mval[i].br_blockcount > 0); 523 if (!(flags & XFS_BMAPI_ENTIRE)) { 524 ASSERT(mval[i].br_startoff >= bno); 525 ASSERT(mval[i].br_blockcount <= len); 526 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <= 527 bno + len); 528 } else { 529 ASSERT(mval[i].br_startoff < bno + len); 530 ASSERT(mval[i].br_startoff + mval[i].br_blockcount > 531 bno); 532 } 533 ASSERT(i == 0 || 534 mval[i - 1].br_startoff + mval[i - 1].br_blockcount == 535 mval[i].br_startoff); 536 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK && 537 mval[i].br_startblock != HOLESTARTBLOCK); 538 ASSERT(mval[i].br_state == XFS_EXT_NORM || 539 mval[i].br_state == XFS_EXT_UNWRITTEN); 540 } 541 } 542 543 #else 544 #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0) 545 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0) 546 #endif /* DEBUG */ 547 548 /* 549 * Inode fork format manipulation functions 550 */ 551 552 /* 553 * Convert the inode format to extent format if it currently is in btree format, 554 * but the extent list is small enough that it fits into the extent format. 555 * 556 * Since the extents are already in-core, all we have to do is give up the space 557 * for the btree root and pitch the leaf block. 558 */ 559 STATIC int /* error */ 560 xfs_bmap_btree_to_extents( 561 struct xfs_trans *tp, /* transaction pointer */ 562 struct xfs_inode *ip, /* incore inode pointer */ 563 struct xfs_btree_cur *cur, /* btree cursor */ 564 int *logflagsp, /* inode logging flags */ 565 int whichfork) /* data or attr fork */ 566 { 567 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); 568 struct xfs_mount *mp = ip->i_mount; 569 struct xfs_btree_block *rblock = ifp->if_broot; 570 struct xfs_btree_block *cblock;/* child btree block */ 571 xfs_fsblock_t cbno; /* child block number */ 572 struct xfs_buf *cbp; /* child block's buffer */ 573 int error; /* error return value */ 574 __be64 *pp; /* ptr to block address */ 575 struct xfs_owner_info oinfo; 576 577 /* check if we actually need the extent format first: */ 578 if (!xfs_bmap_wants_extents(ip, whichfork)) 579 return 0; 580 581 ASSERT(cur); 582 ASSERT(whichfork != XFS_COW_FORK); 583 ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE); 584 ASSERT(be16_to_cpu(rblock->bb_level) == 1); 585 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1); 586 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, false) == 1); 587 588 pp = xfs_bmap_broot_ptr_addr(mp, rblock, 1, ifp->if_broot_bytes); 589 cbno = be64_to_cpu(*pp); 590 #ifdef DEBUG 591 if (XFS_IS_CORRUPT(cur->bc_mp, !xfs_verify_fsbno(mp, cbno))) { 592 xfs_btree_mark_sick(cur); 593 return -EFSCORRUPTED; 594 } 595 #endif 596 error = xfs_bmap_read_buf(mp, tp, cbno, &cbp); 597 if (xfs_metadata_is_sick(error)) 598 xfs_btree_mark_sick(cur); 599 if (error) 600 return error; 601 cblock = XFS_BUF_TO_BLOCK(cbp); 602 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp))) 603 return error; 604 605 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork); 606 error = xfs_free_extent_later(cur->bc_tp, cbno, 1, &oinfo, 607 XFS_AG_RESV_NONE, 0); 608 if (error) 609 return error; 610 611 ip->i_nblocks--; 612 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); 613 xfs_trans_binval(tp, cbp); 614 if (cur->bc_levels[0].bp == cbp) 615 cur->bc_levels[0].bp = NULL; 616 xfs_bmap_broot_realloc(ip, whichfork, 0); 617 ASSERT(ifp->if_broot == NULL); 618 ifp->if_format = XFS_DINODE_FMT_EXTENTS; 619 *logflagsp |= XFS_ILOG_CORE | xfs_ilog_fext(whichfork); 620 return 0; 621 } 622 623 /* 624 * Convert an extents-format file into a btree-format file. 625 * The new file will have a root block (in the inode) and a single child block. 626 */ 627 STATIC int /* error */ 628 xfs_bmap_extents_to_btree( 629 struct xfs_trans *tp, /* transaction pointer */ 630 struct xfs_inode *ip, /* incore inode pointer */ 631 struct xfs_btree_cur **curp, /* cursor returned to caller */ 632 int wasdel, /* converting a delayed alloc */ 633 int *logflagsp, /* inode logging flags */ 634 int whichfork) /* data or attr fork */ 635 { 636 struct xfs_btree_block *ablock; /* allocated (child) bt block */ 637 struct xfs_buf *abp; /* buffer for ablock */ 638 struct xfs_alloc_arg args; /* allocation arguments */ 639 struct xfs_bmbt_rec *arp; /* child record pointer */ 640 struct xfs_btree_block *block; /* btree root block */ 641 struct xfs_btree_cur *cur; /* bmap btree cursor */ 642 int error; /* error return value */ 643 struct xfs_ifork *ifp; /* inode fork pointer */ 644 struct xfs_bmbt_key *kp; /* root block key pointer */ 645 struct xfs_mount *mp; /* mount structure */ 646 xfs_bmbt_ptr_t *pp; /* root block address pointer */ 647 struct xfs_iext_cursor icur; 648 struct xfs_bmbt_irec rec; 649 xfs_extnum_t cnt = 0; 650 651 mp = ip->i_mount; 652 ASSERT(whichfork != XFS_COW_FORK); 653 ifp = xfs_ifork_ptr(ip, whichfork); 654 ASSERT(ifp->if_format == XFS_DINODE_FMT_EXTENTS); 655 656 /* 657 * Make space in the inode incore. This needs to be undone if we fail 658 * to expand the root. 659 */ 660 block = xfs_bmap_broot_realloc(ip, whichfork, 1); 661 662 /* 663 * Fill in the root. 664 */ 665 xfs_bmbt_init_block(ip, block, NULL, 1, 1); 666 /* 667 * Need a cursor. Can't allocate until bb_level is filled in. 668 */ 669 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); 670 if (wasdel) 671 cur->bc_flags |= XFS_BTREE_BMBT_WASDEL; 672 /* 673 * Convert to a btree with two levels, one record in root. 674 */ 675 ifp->if_format = XFS_DINODE_FMT_BTREE; 676 memset(&args, 0, sizeof(args)); 677 args.tp = tp; 678 args.mp = mp; 679 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork); 680 681 args.minlen = args.maxlen = args.prod = 1; 682 args.wasdel = wasdel; 683 *logflagsp = 0; 684 error = xfs_alloc_vextent_start_ag(&args, 685 XFS_INO_TO_FSB(mp, ip->i_ino)); 686 if (error) 687 goto out_root_realloc; 688 689 /* 690 * Allocation can't fail, the space was reserved. 691 */ 692 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) { 693 error = -ENOSPC; 694 goto out_root_realloc; 695 } 696 697 cur->bc_bmap.allocated++; 698 ip->i_nblocks++; 699 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L); 700 error = xfs_trans_get_buf(tp, mp->m_ddev_targp, 701 XFS_FSB_TO_DADDR(mp, args.fsbno), 702 mp->m_bsize, 0, &abp); 703 if (error) 704 goto out_unreserve_dquot; 705 706 /* 707 * Fill in the child block. 708 */ 709 ablock = XFS_BUF_TO_BLOCK(abp); 710 xfs_bmbt_init_block(ip, ablock, abp, 0, 0); 711 712 for_each_xfs_iext(ifp, &icur, &rec) { 713 if (isnullstartblock(rec.br_startblock)) 714 continue; 715 arp = xfs_bmbt_rec_addr(mp, ablock, 1 + cnt); 716 xfs_bmbt_disk_set_all(arp, &rec); 717 cnt++; 718 } 719 ASSERT(cnt == ifp->if_nextents); 720 xfs_btree_set_numrecs(ablock, cnt); 721 722 /* 723 * Fill in the root key and pointer. 724 */ 725 kp = xfs_bmbt_key_addr(mp, block, 1); 726 arp = xfs_bmbt_rec_addr(mp, ablock, 1); 727 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp)); 728 pp = xfs_bmbt_ptr_addr(mp, block, 1, xfs_bmbt_get_maxrecs(cur, 729 be16_to_cpu(block->bb_level))); 730 *pp = cpu_to_be64(args.fsbno); 731 732 /* 733 * Do all this logging at the end so that 734 * the root is at the right level. 735 */ 736 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS); 737 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs)); 738 ASSERT(*curp == NULL); 739 *curp = cur; 740 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork); 741 return 0; 742 743 out_unreserve_dquot: 744 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); 745 out_root_realloc: 746 xfs_bmap_broot_realloc(ip, whichfork, 0); 747 ifp->if_format = XFS_DINODE_FMT_EXTENTS; 748 ASSERT(ifp->if_broot == NULL); 749 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 750 751 return error; 752 } 753 754 /* 755 * Convert a local file to an extents file. 756 * This code is out of bounds for data forks of regular files, 757 * since the file data needs to get logged so things will stay consistent. 758 * (The bmap-level manipulations are ok, though). 759 */ 760 void 761 xfs_bmap_local_to_extents_empty( 762 struct xfs_trans *tp, 763 struct xfs_inode *ip, 764 int whichfork) 765 { 766 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); 767 768 ASSERT(whichfork != XFS_COW_FORK); 769 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL); 770 ASSERT(ifp->if_bytes == 0); 771 ASSERT(ifp->if_nextents == 0); 772 773 xfs_bmap_forkoff_reset(ip, whichfork); 774 ifp->if_data = NULL; 775 ifp->if_height = 0; 776 ifp->if_format = XFS_DINODE_FMT_EXTENTS; 777 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 778 } 779 780 781 int /* error */ 782 xfs_bmap_local_to_extents( 783 xfs_trans_t *tp, /* transaction pointer */ 784 xfs_inode_t *ip, /* incore inode pointer */ 785 xfs_extlen_t total, /* total blocks needed by transaction */ 786 int *logflagsp, /* inode logging flags */ 787 int whichfork, 788 void (*init_fn)(struct xfs_trans *tp, 789 struct xfs_buf *bp, 790 struct xfs_inode *ip, 791 struct xfs_ifork *ifp, void *priv), 792 void *priv) 793 { 794 int error = 0; 795 int flags; /* logging flags returned */ 796 struct xfs_ifork *ifp; /* inode fork pointer */ 797 xfs_alloc_arg_t args; /* allocation arguments */ 798 struct xfs_buf *bp; /* buffer for extent block */ 799 struct xfs_bmbt_irec rec; 800 struct xfs_iext_cursor icur; 801 802 /* 803 * We don't want to deal with the case of keeping inode data inline yet. 804 * So sending the data fork of a regular inode is invalid. 805 */ 806 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK)); 807 ifp = xfs_ifork_ptr(ip, whichfork); 808 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL); 809 810 if (!ifp->if_bytes) { 811 xfs_bmap_local_to_extents_empty(tp, ip, whichfork); 812 flags = XFS_ILOG_CORE; 813 goto done; 814 } 815 816 flags = 0; 817 error = 0; 818 memset(&args, 0, sizeof(args)); 819 args.tp = tp; 820 args.mp = ip->i_mount; 821 args.total = total; 822 args.minlen = args.maxlen = args.prod = 1; 823 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0); 824 825 /* 826 * Allocate a block. We know we need only one, since the 827 * file currently fits in an inode. 828 */ 829 args.total = total; 830 args.minlen = args.maxlen = args.prod = 1; 831 error = xfs_alloc_vextent_start_ag(&args, 832 XFS_INO_TO_FSB(args.mp, ip->i_ino)); 833 if (error) 834 goto done; 835 836 /* Can't fail, the space was reserved. */ 837 ASSERT(args.fsbno != NULLFSBLOCK); 838 ASSERT(args.len == 1); 839 error = xfs_trans_get_buf(tp, args.mp->m_ddev_targp, 840 XFS_FSB_TO_DADDR(args.mp, args.fsbno), 841 args.mp->m_bsize, 0, &bp); 842 if (error) 843 goto done; 844 845 /* 846 * Initialize the block, copy the data and log the remote buffer. 847 * 848 * The callout is responsible for logging because the remote format 849 * might differ from the local format and thus we don't know how much to 850 * log here. Note that init_fn must also set the buffer log item type 851 * correctly. 852 */ 853 init_fn(tp, bp, ip, ifp, priv); 854 855 /* account for the change in fork size */ 856 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork); 857 xfs_bmap_local_to_extents_empty(tp, ip, whichfork); 858 flags |= XFS_ILOG_CORE; 859 860 ifp->if_data = NULL; 861 ifp->if_height = 0; 862 863 rec.br_startoff = 0; 864 rec.br_startblock = args.fsbno; 865 rec.br_blockcount = 1; 866 rec.br_state = XFS_EXT_NORM; 867 xfs_iext_first(ifp, &icur); 868 xfs_iext_insert(ip, &icur, &rec, 0); 869 870 ifp->if_nextents = 1; 871 ip->i_nblocks = 1; 872 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L); 873 flags |= xfs_ilog_fext(whichfork); 874 875 done: 876 *logflagsp = flags; 877 return error; 878 } 879 880 /* 881 * Called from xfs_bmap_add_attrfork to handle btree format files. 882 */ 883 STATIC int /* error */ 884 xfs_bmap_add_attrfork_btree( 885 xfs_trans_t *tp, /* transaction pointer */ 886 xfs_inode_t *ip, /* incore inode pointer */ 887 int *flags) /* inode logging flags */ 888 { 889 struct xfs_btree_block *block = ip->i_df.if_broot; 890 struct xfs_btree_cur *cur; /* btree cursor */ 891 int error; /* error return value */ 892 xfs_mount_t *mp; /* file system mount struct */ 893 int stat; /* newroot status */ 894 895 mp = ip->i_mount; 896 897 if (xfs_bmap_bmdr_space(block) <= xfs_inode_data_fork_size(ip)) 898 *flags |= XFS_ILOG_DBROOT; 899 else { 900 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); 901 error = xfs_bmbt_lookup_first(cur, &stat); 902 if (error) 903 goto error0; 904 /* must be at least one entry */ 905 if (XFS_IS_CORRUPT(mp, stat != 1)) { 906 xfs_btree_mark_sick(cur); 907 error = -EFSCORRUPTED; 908 goto error0; 909 } 910 if ((error = xfs_btree_new_iroot(cur, flags, &stat))) 911 goto error0; 912 if (stat == 0) { 913 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 914 return -ENOSPC; 915 } 916 cur->bc_bmap.allocated = 0; 917 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 918 } 919 return 0; 920 error0: 921 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 922 return error; 923 } 924 925 /* 926 * Called from xfs_bmap_add_attrfork to handle extents format files. 927 */ 928 STATIC int /* error */ 929 xfs_bmap_add_attrfork_extents( 930 struct xfs_trans *tp, /* transaction pointer */ 931 struct xfs_inode *ip, /* incore inode pointer */ 932 int *flags) /* inode logging flags */ 933 { 934 struct xfs_btree_cur *cur; /* bmap btree cursor */ 935 int error; /* error return value */ 936 937 if (ip->i_df.if_nextents * sizeof(struct xfs_bmbt_rec) <= 938 xfs_inode_data_fork_size(ip)) 939 return 0; 940 cur = NULL; 941 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, flags, 942 XFS_DATA_FORK); 943 if (cur) { 944 cur->bc_bmap.allocated = 0; 945 xfs_btree_del_cursor(cur, error); 946 } 947 return error; 948 } 949 950 /* 951 * Called from xfs_bmap_add_attrfork to handle local format files. Each 952 * different data fork content type needs a different callout to do the 953 * conversion. Some are basic and only require special block initialisation 954 * callouts for the data formating, others (directories) are so specialised they 955 * handle everything themselves. 956 * 957 * XXX (dgc): investigate whether directory conversion can use the generic 958 * formatting callout. It should be possible - it's just a very complex 959 * formatter. 960 */ 961 STATIC int /* error */ 962 xfs_bmap_add_attrfork_local( 963 struct xfs_trans *tp, /* transaction pointer */ 964 struct xfs_inode *ip, /* incore inode pointer */ 965 int *flags) /* inode logging flags */ 966 { 967 struct xfs_da_args dargs; /* args for dir/attr code */ 968 969 if (ip->i_df.if_bytes <= xfs_inode_data_fork_size(ip)) 970 return 0; 971 972 if (S_ISDIR(VFS_I(ip)->i_mode)) { 973 memset(&dargs, 0, sizeof(dargs)); 974 dargs.geo = ip->i_mount->m_dir_geo; 975 dargs.dp = ip; 976 dargs.total = dargs.geo->fsbcount; 977 dargs.whichfork = XFS_DATA_FORK; 978 dargs.trans = tp; 979 dargs.owner = ip->i_ino; 980 return xfs_dir2_sf_to_block(&dargs); 981 } 982 983 if (S_ISLNK(VFS_I(ip)->i_mode)) 984 return xfs_bmap_local_to_extents(tp, ip, 1, flags, 985 XFS_DATA_FORK, xfs_symlink_local_to_remote, 986 NULL); 987 988 /* should only be called for types that support local format data */ 989 ASSERT(0); 990 xfs_bmap_mark_sick(ip, XFS_ATTR_FORK); 991 return -EFSCORRUPTED; 992 } 993 994 /* 995 * Set an inode attr fork offset based on the format of the data fork. 996 */ 997 static int 998 xfs_bmap_set_attrforkoff( 999 struct xfs_inode *ip, 1000 int size, 1001 int *version) 1002 { 1003 int default_size = xfs_default_attroffset(ip) >> 3; 1004 1005 switch (ip->i_df.if_format) { 1006 case XFS_DINODE_FMT_DEV: 1007 ip->i_forkoff = default_size; 1008 break; 1009 case XFS_DINODE_FMT_LOCAL: 1010 case XFS_DINODE_FMT_EXTENTS: 1011 case XFS_DINODE_FMT_BTREE: 1012 ip->i_forkoff = xfs_attr_shortform_bytesfit(ip, size); 1013 if (!ip->i_forkoff) 1014 ip->i_forkoff = default_size; 1015 else if (xfs_has_attr2(ip->i_mount) && version) 1016 *version = 2; 1017 break; 1018 default: 1019 ASSERT(0); 1020 return -EINVAL; 1021 } 1022 1023 return 0; 1024 } 1025 1026 /* 1027 * Convert inode from non-attributed to attributed. Caller must hold the 1028 * ILOCK_EXCL and the file cannot have an attr fork. 1029 */ 1030 int /* error code */ 1031 xfs_bmap_add_attrfork( 1032 struct xfs_trans *tp, 1033 struct xfs_inode *ip, /* incore inode pointer */ 1034 int size, /* space new attribute needs */ 1035 int rsvd) /* xact may use reserved blks */ 1036 { 1037 struct xfs_mount *mp = tp->t_mountp; 1038 int version = 1; /* superblock attr version */ 1039 int logflags; /* logging flags */ 1040 int error; /* error return value */ 1041 1042 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL); 1043 if (!xfs_is_metadir_inode(ip)) 1044 ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); 1045 ASSERT(!xfs_inode_has_attr_fork(ip)); 1046 1047 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 1048 error = xfs_bmap_set_attrforkoff(ip, size, &version); 1049 if (error) 1050 return error; 1051 1052 xfs_ifork_init_attr(ip, XFS_DINODE_FMT_EXTENTS, 0); 1053 logflags = 0; 1054 switch (ip->i_df.if_format) { 1055 case XFS_DINODE_FMT_LOCAL: 1056 error = xfs_bmap_add_attrfork_local(tp, ip, &logflags); 1057 break; 1058 case XFS_DINODE_FMT_EXTENTS: 1059 error = xfs_bmap_add_attrfork_extents(tp, ip, &logflags); 1060 break; 1061 case XFS_DINODE_FMT_BTREE: 1062 error = xfs_bmap_add_attrfork_btree(tp, ip, &logflags); 1063 break; 1064 default: 1065 error = 0; 1066 break; 1067 } 1068 if (logflags) 1069 xfs_trans_log_inode(tp, ip, logflags); 1070 if (error) 1071 return error; 1072 if (!xfs_has_attr(mp) || 1073 (!xfs_has_attr2(mp) && version == 2)) { 1074 bool log_sb = false; 1075 1076 spin_lock(&mp->m_sb_lock); 1077 if (!xfs_has_attr(mp)) { 1078 xfs_add_attr(mp); 1079 log_sb = true; 1080 } 1081 if (!xfs_has_attr2(mp) && version == 2) { 1082 xfs_add_attr2(mp); 1083 log_sb = true; 1084 } 1085 spin_unlock(&mp->m_sb_lock); 1086 if (log_sb) 1087 xfs_log_sb(tp); 1088 } 1089 1090 return 0; 1091 } 1092 1093 /* 1094 * Internal and external extent tree search functions. 1095 */ 1096 1097 struct xfs_iread_state { 1098 struct xfs_iext_cursor icur; 1099 xfs_extnum_t loaded; 1100 }; 1101 1102 int 1103 xfs_bmap_complain_bad_rec( 1104 struct xfs_inode *ip, 1105 int whichfork, 1106 xfs_failaddr_t fa, 1107 const struct xfs_bmbt_irec *irec) 1108 { 1109 struct xfs_mount *mp = ip->i_mount; 1110 const char *forkname; 1111 1112 switch (whichfork) { 1113 case XFS_DATA_FORK: forkname = "data"; break; 1114 case XFS_ATTR_FORK: forkname = "attr"; break; 1115 case XFS_COW_FORK: forkname = "CoW"; break; 1116 default: forkname = "???"; break; 1117 } 1118 1119 xfs_warn(mp, 1120 "Bmap BTree record corruption in inode 0x%llx %s fork detected at %pS!", 1121 ip->i_ino, forkname, fa); 1122 xfs_warn(mp, 1123 "Offset 0x%llx, start block 0x%llx, block count 0x%llx state 0x%x", 1124 irec->br_startoff, irec->br_startblock, irec->br_blockcount, 1125 irec->br_state); 1126 1127 return -EFSCORRUPTED; 1128 } 1129 1130 /* Stuff every bmbt record from this block into the incore extent map. */ 1131 static int 1132 xfs_iread_bmbt_block( 1133 struct xfs_btree_cur *cur, 1134 int level, 1135 void *priv) 1136 { 1137 struct xfs_iread_state *ir = priv; 1138 struct xfs_mount *mp = cur->bc_mp; 1139 struct xfs_inode *ip = cur->bc_ino.ip; 1140 struct xfs_btree_block *block; 1141 struct xfs_buf *bp; 1142 struct xfs_bmbt_rec *frp; 1143 xfs_extnum_t num_recs; 1144 xfs_extnum_t j; 1145 int whichfork = cur->bc_ino.whichfork; 1146 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); 1147 1148 block = xfs_btree_get_block(cur, level, &bp); 1149 1150 /* Abort if we find more records than nextents. */ 1151 num_recs = xfs_btree_get_numrecs(block); 1152 if (unlikely(ir->loaded + num_recs > ifp->if_nextents)) { 1153 xfs_warn(ip->i_mount, "corrupt dinode %llu, (btree extents).", 1154 (unsigned long long)ip->i_ino); 1155 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, block, 1156 sizeof(*block), __this_address); 1157 xfs_bmap_mark_sick(ip, whichfork); 1158 return -EFSCORRUPTED; 1159 } 1160 1161 /* Copy records into the incore cache. */ 1162 frp = xfs_bmbt_rec_addr(mp, block, 1); 1163 for (j = 0; j < num_recs; j++, frp++, ir->loaded++) { 1164 struct xfs_bmbt_irec new; 1165 xfs_failaddr_t fa; 1166 1167 xfs_bmbt_disk_get_all(frp, &new); 1168 fa = xfs_bmap_validate_extent(ip, whichfork, &new); 1169 if (fa) { 1170 xfs_inode_verifier_error(ip, -EFSCORRUPTED, 1171 "xfs_iread_extents(2)", frp, 1172 sizeof(*frp), fa); 1173 xfs_bmap_mark_sick(ip, whichfork); 1174 return xfs_bmap_complain_bad_rec(ip, whichfork, fa, 1175 &new); 1176 } 1177 xfs_iext_insert(ip, &ir->icur, &new, 1178 xfs_bmap_fork_to_state(whichfork)); 1179 trace_xfs_read_extent(ip, &ir->icur, 1180 xfs_bmap_fork_to_state(whichfork), _THIS_IP_); 1181 xfs_iext_next(ifp, &ir->icur); 1182 } 1183 1184 return 0; 1185 } 1186 1187 /* 1188 * Read in extents from a btree-format inode. 1189 */ 1190 int 1191 xfs_iread_extents( 1192 struct xfs_trans *tp, 1193 struct xfs_inode *ip, 1194 int whichfork) 1195 { 1196 struct xfs_iread_state ir; 1197 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); 1198 struct xfs_mount *mp = ip->i_mount; 1199 struct xfs_btree_cur *cur; 1200 int error; 1201 1202 if (!xfs_need_iread_extents(ifp)) 1203 return 0; 1204 1205 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL); 1206 1207 ir.loaded = 0; 1208 xfs_iext_first(ifp, &ir.icur); 1209 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); 1210 error = xfs_btree_visit_blocks(cur, xfs_iread_bmbt_block, 1211 XFS_BTREE_VISIT_RECORDS, &ir); 1212 xfs_btree_del_cursor(cur, error); 1213 if (error) 1214 goto out; 1215 1216 if (XFS_IS_CORRUPT(mp, ir.loaded != ifp->if_nextents)) { 1217 xfs_bmap_mark_sick(ip, whichfork); 1218 error = -EFSCORRUPTED; 1219 goto out; 1220 } 1221 ASSERT(ir.loaded == xfs_iext_count(ifp)); 1222 /* 1223 * Use release semantics so that we can use acquire semantics in 1224 * xfs_need_iread_extents and be guaranteed to see a valid mapping tree 1225 * after that load. 1226 */ 1227 smp_store_release(&ifp->if_needextents, 0); 1228 return 0; 1229 out: 1230 if (xfs_metadata_is_sick(error)) 1231 xfs_bmap_mark_sick(ip, whichfork); 1232 xfs_iext_destroy(ifp); 1233 return error; 1234 } 1235 1236 /* 1237 * Returns the relative block number of the first unused block(s) in the given 1238 * fork with at least "len" logically contiguous blocks free. This is the 1239 * lowest-address hole if the fork has holes, else the first block past the end 1240 * of fork. Return 0 if the fork is currently local (in-inode). 1241 */ 1242 int /* error */ 1243 xfs_bmap_first_unused( 1244 struct xfs_trans *tp, /* transaction pointer */ 1245 struct xfs_inode *ip, /* incore inode */ 1246 xfs_extlen_t len, /* size of hole to find */ 1247 xfs_fileoff_t *first_unused, /* unused block */ 1248 int whichfork) /* data or attr fork */ 1249 { 1250 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); 1251 struct xfs_bmbt_irec got; 1252 struct xfs_iext_cursor icur; 1253 xfs_fileoff_t lastaddr = 0; 1254 xfs_fileoff_t lowest, max; 1255 int error; 1256 1257 if (ifp->if_format == XFS_DINODE_FMT_LOCAL) { 1258 *first_unused = 0; 1259 return 0; 1260 } 1261 1262 ASSERT(xfs_ifork_has_extents(ifp)); 1263 1264 error = xfs_iread_extents(tp, ip, whichfork); 1265 if (error) 1266 return error; 1267 1268 lowest = max = *first_unused; 1269 for_each_xfs_iext(ifp, &icur, &got) { 1270 /* 1271 * See if the hole before this extent will work. 1272 */ 1273 if (got.br_startoff >= lowest + len && 1274 got.br_startoff - max >= len) 1275 break; 1276 lastaddr = got.br_startoff + got.br_blockcount; 1277 max = XFS_FILEOFF_MAX(lastaddr, lowest); 1278 } 1279 1280 *first_unused = max; 1281 return 0; 1282 } 1283 1284 /* 1285 * Returns the file-relative block number of the last block - 1 before 1286 * last_block (input value) in the file. 1287 * This is not based on i_size, it is based on the extent records. 1288 * Returns 0 for local files, as they do not have extent records. 1289 */ 1290 int /* error */ 1291 xfs_bmap_last_before( 1292 struct xfs_trans *tp, /* transaction pointer */ 1293 struct xfs_inode *ip, /* incore inode */ 1294 xfs_fileoff_t *last_block, /* last block */ 1295 int whichfork) /* data or attr fork */ 1296 { 1297 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); 1298 struct xfs_bmbt_irec got; 1299 struct xfs_iext_cursor icur; 1300 int error; 1301 1302 switch (ifp->if_format) { 1303 case XFS_DINODE_FMT_LOCAL: 1304 *last_block = 0; 1305 return 0; 1306 case XFS_DINODE_FMT_BTREE: 1307 case XFS_DINODE_FMT_EXTENTS: 1308 break; 1309 default: 1310 ASSERT(0); 1311 xfs_bmap_mark_sick(ip, whichfork); 1312 return -EFSCORRUPTED; 1313 } 1314 1315 error = xfs_iread_extents(tp, ip, whichfork); 1316 if (error) 1317 return error; 1318 1319 if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got)) 1320 *last_block = 0; 1321 return 0; 1322 } 1323 1324 int 1325 xfs_bmap_last_extent( 1326 struct xfs_trans *tp, 1327 struct xfs_inode *ip, 1328 int whichfork, 1329 struct xfs_bmbt_irec *rec, 1330 int *is_empty) 1331 { 1332 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); 1333 struct xfs_iext_cursor icur; 1334 int error; 1335 1336 error = xfs_iread_extents(tp, ip, whichfork); 1337 if (error) 1338 return error; 1339 1340 xfs_iext_last(ifp, &icur); 1341 if (!xfs_iext_get_extent(ifp, &icur, rec)) 1342 *is_empty = 1; 1343 else 1344 *is_empty = 0; 1345 return 0; 1346 } 1347 1348 /* 1349 * Check the last inode extent to determine whether this allocation will result 1350 * in blocks being allocated at the end of the file. When we allocate new data 1351 * blocks at the end of the file which do not start at the previous data block, 1352 * we will try to align the new blocks at stripe unit boundaries. 1353 * 1354 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be 1355 * at, or past the EOF. 1356 */ 1357 STATIC int 1358 xfs_bmap_isaeof( 1359 struct xfs_bmalloca *bma, 1360 int whichfork) 1361 { 1362 struct xfs_bmbt_irec rec; 1363 int is_empty; 1364 int error; 1365 1366 bma->aeof = false; 1367 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec, 1368 &is_empty); 1369 if (error) 1370 return error; 1371 1372 if (is_empty) { 1373 bma->aeof = true; 1374 return 0; 1375 } 1376 1377 /* 1378 * Check if we are allocation or past the last extent, or at least into 1379 * the last delayed allocated extent. 1380 */ 1381 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount || 1382 (bma->offset >= rec.br_startoff && 1383 isnullstartblock(rec.br_startblock)); 1384 return 0; 1385 } 1386 1387 /* 1388 * Returns the file-relative block number of the first block past eof in 1389 * the file. This is not based on i_size, it is based on the extent records. 1390 * Returns 0 for local files, as they do not have extent records. 1391 */ 1392 int 1393 xfs_bmap_last_offset( 1394 struct xfs_inode *ip, 1395 xfs_fileoff_t *last_block, 1396 int whichfork) 1397 { 1398 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); 1399 struct xfs_bmbt_irec rec; 1400 int is_empty; 1401 int error; 1402 1403 *last_block = 0; 1404 1405 if (ifp->if_format == XFS_DINODE_FMT_LOCAL) 1406 return 0; 1407 1408 if (XFS_IS_CORRUPT(ip->i_mount, !xfs_ifork_has_extents(ifp))) { 1409 xfs_bmap_mark_sick(ip, whichfork); 1410 return -EFSCORRUPTED; 1411 } 1412 1413 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty); 1414 if (error || is_empty) 1415 return error; 1416 1417 *last_block = rec.br_startoff + rec.br_blockcount; 1418 return 0; 1419 } 1420 1421 /* 1422 * Extent tree manipulation functions used during allocation. 1423 */ 1424 1425 static inline bool 1426 xfs_bmap_same_rtgroup( 1427 struct xfs_inode *ip, 1428 int whichfork, 1429 struct xfs_bmbt_irec *left, 1430 struct xfs_bmbt_irec *right) 1431 { 1432 struct xfs_mount *mp = ip->i_mount; 1433 1434 if (xfs_ifork_is_realtime(ip, whichfork) && xfs_has_rtgroups(mp)) { 1435 if (xfs_rtb_to_rgno(mp, left->br_startblock) != 1436 xfs_rtb_to_rgno(mp, right->br_startblock)) 1437 return false; 1438 } 1439 1440 return true; 1441 } 1442 1443 /* 1444 * Convert a delayed allocation to a real allocation. 1445 */ 1446 STATIC int /* error */ 1447 xfs_bmap_add_extent_delay_real( 1448 struct xfs_bmalloca *bma, 1449 int whichfork) 1450 { 1451 struct xfs_mount *mp = bma->ip->i_mount; 1452 struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork); 1453 struct xfs_bmbt_irec *new = &bma->got; 1454 int error; /* error return value */ 1455 int i; /* temp state */ 1456 xfs_fileoff_t new_endoff; /* end offset of new entry */ 1457 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ 1458 /* left is 0, right is 1, prev is 2 */ 1459 int rval=0; /* return value (logging flags) */ 1460 uint32_t state = xfs_bmap_fork_to_state(whichfork); 1461 xfs_filblks_t da_new; /* new count del alloc blocks used */ 1462 xfs_filblks_t da_old; /* old count del alloc blocks used */ 1463 xfs_filblks_t temp=0; /* value for da_new calculations */ 1464 int tmp_rval; /* partial logging flags */ 1465 struct xfs_bmbt_irec old; 1466 1467 ASSERT(whichfork != XFS_ATTR_FORK); 1468 ASSERT(!isnullstartblock(new->br_startblock)); 1469 ASSERT(!bma->cur || (bma->cur->bc_flags & XFS_BTREE_BMBT_WASDEL)); 1470 1471 XFS_STATS_INC(mp, xs_add_exlist); 1472 1473 #define LEFT r[0] 1474 #define RIGHT r[1] 1475 #define PREV r[2] 1476 1477 /* 1478 * Set up a bunch of variables to make the tests simpler. 1479 */ 1480 xfs_iext_get_extent(ifp, &bma->icur, &PREV); 1481 new_endoff = new->br_startoff + new->br_blockcount; 1482 ASSERT(isnullstartblock(PREV.br_startblock)); 1483 ASSERT(PREV.br_startoff <= new->br_startoff); 1484 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); 1485 1486 da_old = startblockval(PREV.br_startblock); 1487 da_new = 0; 1488 1489 /* 1490 * Set flags determining what part of the previous delayed allocation 1491 * extent is being replaced by a real allocation. 1492 */ 1493 if (PREV.br_startoff == new->br_startoff) 1494 state |= BMAP_LEFT_FILLING; 1495 if (PREV.br_startoff + PREV.br_blockcount == new_endoff) 1496 state |= BMAP_RIGHT_FILLING; 1497 1498 /* 1499 * Check and set flags if this segment has a left neighbor. 1500 * Don't set contiguous if the combined extent would be too large. 1501 */ 1502 if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) { 1503 state |= BMAP_LEFT_VALID; 1504 if (isnullstartblock(LEFT.br_startblock)) 1505 state |= BMAP_LEFT_DELAY; 1506 } 1507 1508 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && 1509 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && 1510 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && 1511 LEFT.br_state == new->br_state && 1512 LEFT.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN && 1513 xfs_bmap_same_rtgroup(bma->ip, whichfork, &LEFT, new)) 1514 state |= BMAP_LEFT_CONTIG; 1515 1516 /* 1517 * Check and set flags if this segment has a right neighbor. 1518 * Don't set contiguous if the combined extent would be too large. 1519 * Also check for all-three-contiguous being too large. 1520 */ 1521 if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) { 1522 state |= BMAP_RIGHT_VALID; 1523 if (isnullstartblock(RIGHT.br_startblock)) 1524 state |= BMAP_RIGHT_DELAY; 1525 } 1526 1527 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && 1528 new_endoff == RIGHT.br_startoff && 1529 new->br_startblock + new->br_blockcount == RIGHT.br_startblock && 1530 new->br_state == RIGHT.br_state && 1531 new->br_blockcount + RIGHT.br_blockcount <= XFS_MAX_BMBT_EXTLEN && 1532 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | 1533 BMAP_RIGHT_FILLING)) != 1534 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | 1535 BMAP_RIGHT_FILLING) || 1536 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount 1537 <= XFS_MAX_BMBT_EXTLEN) && 1538 xfs_bmap_same_rtgroup(bma->ip, whichfork, new, &RIGHT)) 1539 state |= BMAP_RIGHT_CONTIG; 1540 1541 error = 0; 1542 /* 1543 * Switch out based on the FILLING and CONTIG state bits. 1544 */ 1545 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | 1546 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { 1547 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | 1548 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: 1549 /* 1550 * Filling in all of a previously delayed allocation extent. 1551 * The left and right neighbors are both contiguous with new. 1552 */ 1553 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount; 1554 1555 xfs_iext_remove(bma->ip, &bma->icur, state); 1556 xfs_iext_remove(bma->ip, &bma->icur, state); 1557 xfs_iext_prev(ifp, &bma->icur); 1558 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT); 1559 ifp->if_nextents--; 1560 1561 if (bma->cur == NULL) 1562 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; 1563 else { 1564 rval = XFS_ILOG_CORE; 1565 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i); 1566 if (error) 1567 goto done; 1568 if (XFS_IS_CORRUPT(mp, i != 1)) { 1569 xfs_btree_mark_sick(bma->cur); 1570 error = -EFSCORRUPTED; 1571 goto done; 1572 } 1573 error = xfs_btree_delete(bma->cur, &i); 1574 if (error) 1575 goto done; 1576 if (XFS_IS_CORRUPT(mp, i != 1)) { 1577 xfs_btree_mark_sick(bma->cur); 1578 error = -EFSCORRUPTED; 1579 goto done; 1580 } 1581 error = xfs_btree_decrement(bma->cur, 0, &i); 1582 if (error) 1583 goto done; 1584 if (XFS_IS_CORRUPT(mp, i != 1)) { 1585 xfs_btree_mark_sick(bma->cur); 1586 error = -EFSCORRUPTED; 1587 goto done; 1588 } 1589 error = xfs_bmbt_update(bma->cur, &LEFT); 1590 if (error) 1591 goto done; 1592 } 1593 ASSERT(da_new <= da_old); 1594 break; 1595 1596 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: 1597 /* 1598 * Filling in all of a previously delayed allocation extent. 1599 * The left neighbor is contiguous, the right is not. 1600 */ 1601 old = LEFT; 1602 LEFT.br_blockcount += PREV.br_blockcount; 1603 1604 xfs_iext_remove(bma->ip, &bma->icur, state); 1605 xfs_iext_prev(ifp, &bma->icur); 1606 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT); 1607 1608 if (bma->cur == NULL) 1609 rval = XFS_ILOG_DEXT; 1610 else { 1611 rval = 0; 1612 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); 1613 if (error) 1614 goto done; 1615 if (XFS_IS_CORRUPT(mp, i != 1)) { 1616 xfs_btree_mark_sick(bma->cur); 1617 error = -EFSCORRUPTED; 1618 goto done; 1619 } 1620 error = xfs_bmbt_update(bma->cur, &LEFT); 1621 if (error) 1622 goto done; 1623 } 1624 ASSERT(da_new <= da_old); 1625 break; 1626 1627 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: 1628 /* 1629 * Filling in all of a previously delayed allocation extent. 1630 * The right neighbor is contiguous, the left is not. Take care 1631 * with delay -> unwritten extent allocation here because the 1632 * delalloc record we are overwriting is always written. 1633 */ 1634 PREV.br_startblock = new->br_startblock; 1635 PREV.br_blockcount += RIGHT.br_blockcount; 1636 PREV.br_state = new->br_state; 1637 1638 xfs_iext_next(ifp, &bma->icur); 1639 xfs_iext_remove(bma->ip, &bma->icur, state); 1640 xfs_iext_prev(ifp, &bma->icur); 1641 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); 1642 1643 if (bma->cur == NULL) 1644 rval = XFS_ILOG_DEXT; 1645 else { 1646 rval = 0; 1647 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i); 1648 if (error) 1649 goto done; 1650 if (XFS_IS_CORRUPT(mp, i != 1)) { 1651 xfs_btree_mark_sick(bma->cur); 1652 error = -EFSCORRUPTED; 1653 goto done; 1654 } 1655 error = xfs_bmbt_update(bma->cur, &PREV); 1656 if (error) 1657 goto done; 1658 } 1659 ASSERT(da_new <= da_old); 1660 break; 1661 1662 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: 1663 /* 1664 * Filling in all of a previously delayed allocation extent. 1665 * Neither the left nor right neighbors are contiguous with 1666 * the new one. 1667 */ 1668 PREV.br_startblock = new->br_startblock; 1669 PREV.br_state = new->br_state; 1670 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); 1671 ifp->if_nextents++; 1672 1673 if (bma->cur == NULL) 1674 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; 1675 else { 1676 rval = XFS_ILOG_CORE; 1677 error = xfs_bmbt_lookup_eq(bma->cur, new, &i); 1678 if (error) 1679 goto done; 1680 if (XFS_IS_CORRUPT(mp, i != 0)) { 1681 xfs_btree_mark_sick(bma->cur); 1682 error = -EFSCORRUPTED; 1683 goto done; 1684 } 1685 error = xfs_btree_insert(bma->cur, &i); 1686 if (error) 1687 goto done; 1688 if (XFS_IS_CORRUPT(mp, i != 1)) { 1689 xfs_btree_mark_sick(bma->cur); 1690 error = -EFSCORRUPTED; 1691 goto done; 1692 } 1693 } 1694 ASSERT(da_new <= da_old); 1695 break; 1696 1697 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: 1698 /* 1699 * Filling in the first part of a previous delayed allocation. 1700 * The left neighbor is contiguous. 1701 */ 1702 old = LEFT; 1703 temp = PREV.br_blockcount - new->br_blockcount; 1704 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), 1705 startblockval(PREV.br_startblock)); 1706 1707 LEFT.br_blockcount += new->br_blockcount; 1708 1709 PREV.br_blockcount = temp; 1710 PREV.br_startoff += new->br_blockcount; 1711 PREV.br_startblock = nullstartblock(da_new); 1712 1713 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); 1714 xfs_iext_prev(ifp, &bma->icur); 1715 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT); 1716 1717 if (bma->cur == NULL) 1718 rval = XFS_ILOG_DEXT; 1719 else { 1720 rval = 0; 1721 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); 1722 if (error) 1723 goto done; 1724 if (XFS_IS_CORRUPT(mp, i != 1)) { 1725 xfs_btree_mark_sick(bma->cur); 1726 error = -EFSCORRUPTED; 1727 goto done; 1728 } 1729 error = xfs_bmbt_update(bma->cur, &LEFT); 1730 if (error) 1731 goto done; 1732 } 1733 ASSERT(da_new <= da_old); 1734 break; 1735 1736 case BMAP_LEFT_FILLING: 1737 /* 1738 * Filling in the first part of a previous delayed allocation. 1739 * The left neighbor is not contiguous. 1740 */ 1741 xfs_iext_update_extent(bma->ip, state, &bma->icur, new); 1742 ifp->if_nextents++; 1743 1744 if (bma->cur == NULL) 1745 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; 1746 else { 1747 rval = XFS_ILOG_CORE; 1748 error = xfs_bmbt_lookup_eq(bma->cur, new, &i); 1749 if (error) 1750 goto done; 1751 if (XFS_IS_CORRUPT(mp, i != 0)) { 1752 xfs_btree_mark_sick(bma->cur); 1753 error = -EFSCORRUPTED; 1754 goto done; 1755 } 1756 error = xfs_btree_insert(bma->cur, &i); 1757 if (error) 1758 goto done; 1759 if (XFS_IS_CORRUPT(mp, i != 1)) { 1760 xfs_btree_mark_sick(bma->cur); 1761 error = -EFSCORRUPTED; 1762 goto done; 1763 } 1764 } 1765 1766 if (xfs_bmap_needs_btree(bma->ip, whichfork)) { 1767 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, 1768 &bma->cur, 1, &tmp_rval, whichfork); 1769 rval |= tmp_rval; 1770 if (error) 1771 goto done; 1772 } 1773 1774 temp = PREV.br_blockcount - new->br_blockcount; 1775 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), 1776 startblockval(PREV.br_startblock) - 1777 (bma->cur ? bma->cur->bc_bmap.allocated : 0)); 1778 1779 PREV.br_startoff = new_endoff; 1780 PREV.br_blockcount = temp; 1781 PREV.br_startblock = nullstartblock(da_new); 1782 xfs_iext_next(ifp, &bma->icur); 1783 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state); 1784 xfs_iext_prev(ifp, &bma->icur); 1785 break; 1786 1787 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: 1788 /* 1789 * Filling in the last part of a previous delayed allocation. 1790 * The right neighbor is contiguous with the new allocation. 1791 */ 1792 old = RIGHT; 1793 RIGHT.br_startoff = new->br_startoff; 1794 RIGHT.br_startblock = new->br_startblock; 1795 RIGHT.br_blockcount += new->br_blockcount; 1796 1797 if (bma->cur == NULL) 1798 rval = XFS_ILOG_DEXT; 1799 else { 1800 rval = 0; 1801 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); 1802 if (error) 1803 goto done; 1804 if (XFS_IS_CORRUPT(mp, i != 1)) { 1805 xfs_btree_mark_sick(bma->cur); 1806 error = -EFSCORRUPTED; 1807 goto done; 1808 } 1809 error = xfs_bmbt_update(bma->cur, &RIGHT); 1810 if (error) 1811 goto done; 1812 } 1813 1814 temp = PREV.br_blockcount - new->br_blockcount; 1815 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), 1816 startblockval(PREV.br_startblock)); 1817 1818 PREV.br_blockcount = temp; 1819 PREV.br_startblock = nullstartblock(da_new); 1820 1821 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); 1822 xfs_iext_next(ifp, &bma->icur); 1823 xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT); 1824 ASSERT(da_new <= da_old); 1825 break; 1826 1827 case BMAP_RIGHT_FILLING: 1828 /* 1829 * Filling in the last part of a previous delayed allocation. 1830 * The right neighbor is not contiguous. 1831 */ 1832 xfs_iext_update_extent(bma->ip, state, &bma->icur, new); 1833 ifp->if_nextents++; 1834 1835 if (bma->cur == NULL) 1836 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; 1837 else { 1838 rval = XFS_ILOG_CORE; 1839 error = xfs_bmbt_lookup_eq(bma->cur, new, &i); 1840 if (error) 1841 goto done; 1842 if (XFS_IS_CORRUPT(mp, i != 0)) { 1843 xfs_btree_mark_sick(bma->cur); 1844 error = -EFSCORRUPTED; 1845 goto done; 1846 } 1847 error = xfs_btree_insert(bma->cur, &i); 1848 if (error) 1849 goto done; 1850 if (XFS_IS_CORRUPT(mp, i != 1)) { 1851 xfs_btree_mark_sick(bma->cur); 1852 error = -EFSCORRUPTED; 1853 goto done; 1854 } 1855 } 1856 1857 if (xfs_bmap_needs_btree(bma->ip, whichfork)) { 1858 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, 1859 &bma->cur, 1, &tmp_rval, whichfork); 1860 rval |= tmp_rval; 1861 if (error) 1862 goto done; 1863 } 1864 1865 temp = PREV.br_blockcount - new->br_blockcount; 1866 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), 1867 startblockval(PREV.br_startblock) - 1868 (bma->cur ? bma->cur->bc_bmap.allocated : 0)); 1869 1870 PREV.br_startblock = nullstartblock(da_new); 1871 PREV.br_blockcount = temp; 1872 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state); 1873 xfs_iext_next(ifp, &bma->icur); 1874 ASSERT(da_new <= da_old); 1875 break; 1876 1877 case 0: 1878 /* 1879 * Filling in the middle part of a previous delayed allocation. 1880 * Contiguity is impossible here. 1881 * This case is avoided almost all the time. 1882 * 1883 * We start with a delayed allocation: 1884 * 1885 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+ 1886 * PREV @ idx 1887 * 1888 * and we are allocating: 1889 * +rrrrrrrrrrrrrrrrr+ 1890 * new 1891 * 1892 * and we set it up for insertion as: 1893 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+ 1894 * new 1895 * PREV @ idx LEFT RIGHT 1896 * inserted at idx + 1 1897 */ 1898 old = PREV; 1899 1900 /* LEFT is the new middle */ 1901 LEFT = *new; 1902 1903 /* RIGHT is the new right */ 1904 RIGHT.br_state = PREV.br_state; 1905 RIGHT.br_startoff = new_endoff; 1906 RIGHT.br_blockcount = 1907 PREV.br_startoff + PREV.br_blockcount - new_endoff; 1908 RIGHT.br_startblock = 1909 nullstartblock(xfs_bmap_worst_indlen(bma->ip, 1910 RIGHT.br_blockcount)); 1911 1912 /* truncate PREV */ 1913 PREV.br_blockcount = new->br_startoff - PREV.br_startoff; 1914 PREV.br_startblock = 1915 nullstartblock(xfs_bmap_worst_indlen(bma->ip, 1916 PREV.br_blockcount)); 1917 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); 1918 1919 xfs_iext_next(ifp, &bma->icur); 1920 xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state); 1921 xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state); 1922 ifp->if_nextents++; 1923 1924 if (bma->cur == NULL) 1925 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; 1926 else { 1927 rval = XFS_ILOG_CORE; 1928 error = xfs_bmbt_lookup_eq(bma->cur, new, &i); 1929 if (error) 1930 goto done; 1931 if (XFS_IS_CORRUPT(mp, i != 0)) { 1932 xfs_btree_mark_sick(bma->cur); 1933 error = -EFSCORRUPTED; 1934 goto done; 1935 } 1936 error = xfs_btree_insert(bma->cur, &i); 1937 if (error) 1938 goto done; 1939 if (XFS_IS_CORRUPT(mp, i != 1)) { 1940 xfs_btree_mark_sick(bma->cur); 1941 error = -EFSCORRUPTED; 1942 goto done; 1943 } 1944 } 1945 1946 if (xfs_bmap_needs_btree(bma->ip, whichfork)) { 1947 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, 1948 &bma->cur, 1, &tmp_rval, whichfork); 1949 rval |= tmp_rval; 1950 if (error) 1951 goto done; 1952 } 1953 1954 da_new = startblockval(PREV.br_startblock) + 1955 startblockval(RIGHT.br_startblock); 1956 break; 1957 1958 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: 1959 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: 1960 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: 1961 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: 1962 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: 1963 case BMAP_LEFT_CONTIG: 1964 case BMAP_RIGHT_CONTIG: 1965 /* 1966 * These cases are all impossible. 1967 */ 1968 ASSERT(0); 1969 } 1970 1971 /* add reverse mapping unless caller opted out */ 1972 if (!(bma->flags & XFS_BMAPI_NORMAP)) 1973 xfs_rmap_map_extent(bma->tp, bma->ip, whichfork, new); 1974 1975 /* convert to a btree if necessary */ 1976 if (xfs_bmap_needs_btree(bma->ip, whichfork)) { 1977 int tmp_logflags; /* partial log flag return val */ 1978 1979 ASSERT(bma->cur == NULL); 1980 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, 1981 &bma->cur, da_old > 0, &tmp_logflags, 1982 whichfork); 1983 bma->logflags |= tmp_logflags; 1984 if (error) 1985 goto done; 1986 } 1987 1988 if (da_new != da_old) 1989 xfs_mod_delalloc(bma->ip, 0, (int64_t)da_new - da_old); 1990 1991 if (bma->cur) { 1992 da_new += bma->cur->bc_bmap.allocated; 1993 bma->cur->bc_bmap.allocated = 0; 1994 } 1995 1996 /* adjust for changes in reserved delayed indirect blocks */ 1997 if (da_new < da_old) 1998 xfs_add_fdblocks(mp, da_old - da_new); 1999 else if (da_new > da_old) 2000 error = xfs_dec_fdblocks(mp, da_new - da_old, true); 2001 2002 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork); 2003 done: 2004 if (whichfork != XFS_COW_FORK) 2005 bma->logflags |= rval; 2006 return error; 2007 #undef LEFT 2008 #undef RIGHT 2009 #undef PREV 2010 } 2011 2012 /* 2013 * Convert an unwritten allocation to a real allocation or vice versa. 2014 */ 2015 int /* error */ 2016 xfs_bmap_add_extent_unwritten_real( 2017 struct xfs_trans *tp, 2018 xfs_inode_t *ip, /* incore inode pointer */ 2019 int whichfork, 2020 struct xfs_iext_cursor *icur, 2021 struct xfs_btree_cur **curp, /* if *curp is null, not a btree */ 2022 xfs_bmbt_irec_t *new, /* new data to add to file extents */ 2023 int *logflagsp) /* inode logging flags */ 2024 { 2025 struct xfs_btree_cur *cur; /* btree cursor */ 2026 int error; /* error return value */ 2027 int i; /* temp state */ 2028 struct xfs_ifork *ifp; /* inode fork pointer */ 2029 xfs_fileoff_t new_endoff; /* end offset of new entry */ 2030 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ 2031 /* left is 0, right is 1, prev is 2 */ 2032 int rval=0; /* return value (logging flags) */ 2033 uint32_t state = xfs_bmap_fork_to_state(whichfork); 2034 struct xfs_mount *mp = ip->i_mount; 2035 struct xfs_bmbt_irec old; 2036 2037 *logflagsp = 0; 2038 2039 cur = *curp; 2040 ifp = xfs_ifork_ptr(ip, whichfork); 2041 2042 ASSERT(!isnullstartblock(new->br_startblock)); 2043 2044 XFS_STATS_INC(mp, xs_add_exlist); 2045 2046 #define LEFT r[0] 2047 #define RIGHT r[1] 2048 #define PREV r[2] 2049 2050 /* 2051 * Set up a bunch of variables to make the tests simpler. 2052 */ 2053 error = 0; 2054 xfs_iext_get_extent(ifp, icur, &PREV); 2055 ASSERT(new->br_state != PREV.br_state); 2056 new_endoff = new->br_startoff + new->br_blockcount; 2057 ASSERT(PREV.br_startoff <= new->br_startoff); 2058 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); 2059 2060 /* 2061 * Set flags determining what part of the previous oldext allocation 2062 * extent is being replaced by a newext allocation. 2063 */ 2064 if (PREV.br_startoff == new->br_startoff) 2065 state |= BMAP_LEFT_FILLING; 2066 if (PREV.br_startoff + PREV.br_blockcount == new_endoff) 2067 state |= BMAP_RIGHT_FILLING; 2068 2069 /* 2070 * Check and set flags if this segment has a left neighbor. 2071 * Don't set contiguous if the combined extent would be too large. 2072 */ 2073 if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) { 2074 state |= BMAP_LEFT_VALID; 2075 if (isnullstartblock(LEFT.br_startblock)) 2076 state |= BMAP_LEFT_DELAY; 2077 } 2078 2079 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && 2080 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && 2081 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && 2082 LEFT.br_state == new->br_state && 2083 LEFT.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN && 2084 xfs_bmap_same_rtgroup(ip, whichfork, &LEFT, new)) 2085 state |= BMAP_LEFT_CONTIG; 2086 2087 /* 2088 * Check and set flags if this segment has a right neighbor. 2089 * Don't set contiguous if the combined extent would be too large. 2090 * Also check for all-three-contiguous being too large. 2091 */ 2092 if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) { 2093 state |= BMAP_RIGHT_VALID; 2094 if (isnullstartblock(RIGHT.br_startblock)) 2095 state |= BMAP_RIGHT_DELAY; 2096 } 2097 2098 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && 2099 new_endoff == RIGHT.br_startoff && 2100 new->br_startblock + new->br_blockcount == RIGHT.br_startblock && 2101 new->br_state == RIGHT.br_state && 2102 new->br_blockcount + RIGHT.br_blockcount <= XFS_MAX_BMBT_EXTLEN && 2103 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | 2104 BMAP_RIGHT_FILLING)) != 2105 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | 2106 BMAP_RIGHT_FILLING) || 2107 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount 2108 <= XFS_MAX_BMBT_EXTLEN) && 2109 xfs_bmap_same_rtgroup(ip, whichfork, new, &RIGHT)) 2110 state |= BMAP_RIGHT_CONTIG; 2111 2112 /* 2113 * Switch out based on the FILLING and CONTIG state bits. 2114 */ 2115 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | 2116 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { 2117 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | 2118 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: 2119 /* 2120 * Setting all of a previous oldext extent to newext. 2121 * The left and right neighbors are both contiguous with new. 2122 */ 2123 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount; 2124 2125 xfs_iext_remove(ip, icur, state); 2126 xfs_iext_remove(ip, icur, state); 2127 xfs_iext_prev(ifp, icur); 2128 xfs_iext_update_extent(ip, state, icur, &LEFT); 2129 ifp->if_nextents -= 2; 2130 if (cur == NULL) 2131 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; 2132 else { 2133 rval = XFS_ILOG_CORE; 2134 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i); 2135 if (error) 2136 goto done; 2137 if (XFS_IS_CORRUPT(mp, i != 1)) { 2138 xfs_btree_mark_sick(cur); 2139 error = -EFSCORRUPTED; 2140 goto done; 2141 } 2142 if ((error = xfs_btree_delete(cur, &i))) 2143 goto done; 2144 if (XFS_IS_CORRUPT(mp, i != 1)) { 2145 xfs_btree_mark_sick(cur); 2146 error = -EFSCORRUPTED; 2147 goto done; 2148 } 2149 if ((error = xfs_btree_decrement(cur, 0, &i))) 2150 goto done; 2151 if (XFS_IS_CORRUPT(mp, i != 1)) { 2152 xfs_btree_mark_sick(cur); 2153 error = -EFSCORRUPTED; 2154 goto done; 2155 } 2156 if ((error = xfs_btree_delete(cur, &i))) 2157 goto done; 2158 if (XFS_IS_CORRUPT(mp, i != 1)) { 2159 xfs_btree_mark_sick(cur); 2160 error = -EFSCORRUPTED; 2161 goto done; 2162 } 2163 if ((error = xfs_btree_decrement(cur, 0, &i))) 2164 goto done; 2165 if (XFS_IS_CORRUPT(mp, i != 1)) { 2166 xfs_btree_mark_sick(cur); 2167 error = -EFSCORRUPTED; 2168 goto done; 2169 } 2170 error = xfs_bmbt_update(cur, &LEFT); 2171 if (error) 2172 goto done; 2173 } 2174 break; 2175 2176 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: 2177 /* 2178 * Setting all of a previous oldext extent to newext. 2179 * The left neighbor is contiguous, the right is not. 2180 */ 2181 LEFT.br_blockcount += PREV.br_blockcount; 2182 2183 xfs_iext_remove(ip, icur, state); 2184 xfs_iext_prev(ifp, icur); 2185 xfs_iext_update_extent(ip, state, icur, &LEFT); 2186 ifp->if_nextents--; 2187 if (cur == NULL) 2188 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; 2189 else { 2190 rval = XFS_ILOG_CORE; 2191 error = xfs_bmbt_lookup_eq(cur, &PREV, &i); 2192 if (error) 2193 goto done; 2194 if (XFS_IS_CORRUPT(mp, i != 1)) { 2195 xfs_btree_mark_sick(cur); 2196 error = -EFSCORRUPTED; 2197 goto done; 2198 } 2199 if ((error = xfs_btree_delete(cur, &i))) 2200 goto done; 2201 if (XFS_IS_CORRUPT(mp, i != 1)) { 2202 xfs_btree_mark_sick(cur); 2203 error = -EFSCORRUPTED; 2204 goto done; 2205 } 2206 if ((error = xfs_btree_decrement(cur, 0, &i))) 2207 goto done; 2208 if (XFS_IS_CORRUPT(mp, i != 1)) { 2209 xfs_btree_mark_sick(cur); 2210 error = -EFSCORRUPTED; 2211 goto done; 2212 } 2213 error = xfs_bmbt_update(cur, &LEFT); 2214 if (error) 2215 goto done; 2216 } 2217 break; 2218 2219 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: 2220 /* 2221 * Setting all of a previous oldext extent to newext. 2222 * The right neighbor is contiguous, the left is not. 2223 */ 2224 PREV.br_blockcount += RIGHT.br_blockcount; 2225 PREV.br_state = new->br_state; 2226 2227 xfs_iext_next(ifp, icur); 2228 xfs_iext_remove(ip, icur, state); 2229 xfs_iext_prev(ifp, icur); 2230 xfs_iext_update_extent(ip, state, icur, &PREV); 2231 ifp->if_nextents--; 2232 2233 if (cur == NULL) 2234 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; 2235 else { 2236 rval = XFS_ILOG_CORE; 2237 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i); 2238 if (error) 2239 goto done; 2240 if (XFS_IS_CORRUPT(mp, i != 1)) { 2241 xfs_btree_mark_sick(cur); 2242 error = -EFSCORRUPTED; 2243 goto done; 2244 } 2245 if ((error = xfs_btree_delete(cur, &i))) 2246 goto done; 2247 if (XFS_IS_CORRUPT(mp, i != 1)) { 2248 xfs_btree_mark_sick(cur); 2249 error = -EFSCORRUPTED; 2250 goto done; 2251 } 2252 if ((error = xfs_btree_decrement(cur, 0, &i))) 2253 goto done; 2254 if (XFS_IS_CORRUPT(mp, i != 1)) { 2255 xfs_btree_mark_sick(cur); 2256 error = -EFSCORRUPTED; 2257 goto done; 2258 } 2259 error = xfs_bmbt_update(cur, &PREV); 2260 if (error) 2261 goto done; 2262 } 2263 break; 2264 2265 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: 2266 /* 2267 * Setting all of a previous oldext extent to newext. 2268 * Neither the left nor right neighbors are contiguous with 2269 * the new one. 2270 */ 2271 PREV.br_state = new->br_state; 2272 xfs_iext_update_extent(ip, state, icur, &PREV); 2273 2274 if (cur == NULL) 2275 rval = XFS_ILOG_DEXT; 2276 else { 2277 rval = 0; 2278 error = xfs_bmbt_lookup_eq(cur, new, &i); 2279 if (error) 2280 goto done; 2281 if (XFS_IS_CORRUPT(mp, i != 1)) { 2282 xfs_btree_mark_sick(cur); 2283 error = -EFSCORRUPTED; 2284 goto done; 2285 } 2286 error = xfs_bmbt_update(cur, &PREV); 2287 if (error) 2288 goto done; 2289 } 2290 break; 2291 2292 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: 2293 /* 2294 * Setting the first part of a previous oldext extent to newext. 2295 * The left neighbor is contiguous. 2296 */ 2297 LEFT.br_blockcount += new->br_blockcount; 2298 2299 old = PREV; 2300 PREV.br_startoff += new->br_blockcount; 2301 PREV.br_startblock += new->br_blockcount; 2302 PREV.br_blockcount -= new->br_blockcount; 2303 2304 xfs_iext_update_extent(ip, state, icur, &PREV); 2305 xfs_iext_prev(ifp, icur); 2306 xfs_iext_update_extent(ip, state, icur, &LEFT); 2307 2308 if (cur == NULL) 2309 rval = XFS_ILOG_DEXT; 2310 else { 2311 rval = 0; 2312 error = xfs_bmbt_lookup_eq(cur, &old, &i); 2313 if (error) 2314 goto done; 2315 if (XFS_IS_CORRUPT(mp, i != 1)) { 2316 xfs_btree_mark_sick(cur); 2317 error = -EFSCORRUPTED; 2318 goto done; 2319 } 2320 error = xfs_bmbt_update(cur, &PREV); 2321 if (error) 2322 goto done; 2323 error = xfs_btree_decrement(cur, 0, &i); 2324 if (error) 2325 goto done; 2326 error = xfs_bmbt_update(cur, &LEFT); 2327 if (error) 2328 goto done; 2329 } 2330 break; 2331 2332 case BMAP_LEFT_FILLING: 2333 /* 2334 * Setting the first part of a previous oldext extent to newext. 2335 * The left neighbor is not contiguous. 2336 */ 2337 old = PREV; 2338 PREV.br_startoff += new->br_blockcount; 2339 PREV.br_startblock += new->br_blockcount; 2340 PREV.br_blockcount -= new->br_blockcount; 2341 2342 xfs_iext_update_extent(ip, state, icur, &PREV); 2343 xfs_iext_insert(ip, icur, new, state); 2344 ifp->if_nextents++; 2345 2346 if (cur == NULL) 2347 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; 2348 else { 2349 rval = XFS_ILOG_CORE; 2350 error = xfs_bmbt_lookup_eq(cur, &old, &i); 2351 if (error) 2352 goto done; 2353 if (XFS_IS_CORRUPT(mp, i != 1)) { 2354 xfs_btree_mark_sick(cur); 2355 error = -EFSCORRUPTED; 2356 goto done; 2357 } 2358 error = xfs_bmbt_update(cur, &PREV); 2359 if (error) 2360 goto done; 2361 cur->bc_rec.b = *new; 2362 if ((error = xfs_btree_insert(cur, &i))) 2363 goto done; 2364 if (XFS_IS_CORRUPT(mp, i != 1)) { 2365 xfs_btree_mark_sick(cur); 2366 error = -EFSCORRUPTED; 2367 goto done; 2368 } 2369 } 2370 break; 2371 2372 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: 2373 /* 2374 * Setting the last part of a previous oldext extent to newext. 2375 * The right neighbor is contiguous with the new allocation. 2376 */ 2377 old = PREV; 2378 PREV.br_blockcount -= new->br_blockcount; 2379 2380 RIGHT.br_startoff = new->br_startoff; 2381 RIGHT.br_startblock = new->br_startblock; 2382 RIGHT.br_blockcount += new->br_blockcount; 2383 2384 xfs_iext_update_extent(ip, state, icur, &PREV); 2385 xfs_iext_next(ifp, icur); 2386 xfs_iext_update_extent(ip, state, icur, &RIGHT); 2387 2388 if (cur == NULL) 2389 rval = XFS_ILOG_DEXT; 2390 else { 2391 rval = 0; 2392 error = xfs_bmbt_lookup_eq(cur, &old, &i); 2393 if (error) 2394 goto done; 2395 if (XFS_IS_CORRUPT(mp, i != 1)) { 2396 xfs_btree_mark_sick(cur); 2397 error = -EFSCORRUPTED; 2398 goto done; 2399 } 2400 error = xfs_bmbt_update(cur, &PREV); 2401 if (error) 2402 goto done; 2403 error = xfs_btree_increment(cur, 0, &i); 2404 if (error) 2405 goto done; 2406 error = xfs_bmbt_update(cur, &RIGHT); 2407 if (error) 2408 goto done; 2409 } 2410 break; 2411 2412 case BMAP_RIGHT_FILLING: 2413 /* 2414 * Setting the last part of a previous oldext extent to newext. 2415 * The right neighbor is not contiguous. 2416 */ 2417 old = PREV; 2418 PREV.br_blockcount -= new->br_blockcount; 2419 2420 xfs_iext_update_extent(ip, state, icur, &PREV); 2421 xfs_iext_next(ifp, icur); 2422 xfs_iext_insert(ip, icur, new, state); 2423 ifp->if_nextents++; 2424 2425 if (cur == NULL) 2426 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; 2427 else { 2428 rval = XFS_ILOG_CORE; 2429 error = xfs_bmbt_lookup_eq(cur, &old, &i); 2430 if (error) 2431 goto done; 2432 if (XFS_IS_CORRUPT(mp, i != 1)) { 2433 xfs_btree_mark_sick(cur); 2434 error = -EFSCORRUPTED; 2435 goto done; 2436 } 2437 error = xfs_bmbt_update(cur, &PREV); 2438 if (error) 2439 goto done; 2440 error = xfs_bmbt_lookup_eq(cur, new, &i); 2441 if (error) 2442 goto done; 2443 if (XFS_IS_CORRUPT(mp, i != 0)) { 2444 xfs_btree_mark_sick(cur); 2445 error = -EFSCORRUPTED; 2446 goto done; 2447 } 2448 if ((error = xfs_btree_insert(cur, &i))) 2449 goto done; 2450 if (XFS_IS_CORRUPT(mp, i != 1)) { 2451 xfs_btree_mark_sick(cur); 2452 error = -EFSCORRUPTED; 2453 goto done; 2454 } 2455 } 2456 break; 2457 2458 case 0: 2459 /* 2460 * Setting the middle part of a previous oldext extent to 2461 * newext. Contiguity is impossible here. 2462 * One extent becomes three extents. 2463 */ 2464 old = PREV; 2465 PREV.br_blockcount = new->br_startoff - PREV.br_startoff; 2466 2467 r[0] = *new; 2468 r[1].br_startoff = new_endoff; 2469 r[1].br_blockcount = 2470 old.br_startoff + old.br_blockcount - new_endoff; 2471 r[1].br_startblock = new->br_startblock + new->br_blockcount; 2472 r[1].br_state = PREV.br_state; 2473 2474 xfs_iext_update_extent(ip, state, icur, &PREV); 2475 xfs_iext_next(ifp, icur); 2476 xfs_iext_insert(ip, icur, &r[1], state); 2477 xfs_iext_insert(ip, icur, &r[0], state); 2478 ifp->if_nextents += 2; 2479 2480 if (cur == NULL) 2481 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; 2482 else { 2483 rval = XFS_ILOG_CORE; 2484 error = xfs_bmbt_lookup_eq(cur, &old, &i); 2485 if (error) 2486 goto done; 2487 if (XFS_IS_CORRUPT(mp, i != 1)) { 2488 xfs_btree_mark_sick(cur); 2489 error = -EFSCORRUPTED; 2490 goto done; 2491 } 2492 /* new right extent - oldext */ 2493 error = xfs_bmbt_update(cur, &r[1]); 2494 if (error) 2495 goto done; 2496 /* new left extent - oldext */ 2497 cur->bc_rec.b = PREV; 2498 if ((error = xfs_btree_insert(cur, &i))) 2499 goto done; 2500 if (XFS_IS_CORRUPT(mp, i != 1)) { 2501 xfs_btree_mark_sick(cur); 2502 error = -EFSCORRUPTED; 2503 goto done; 2504 } 2505 /* 2506 * Reset the cursor to the position of the new extent 2507 * we are about to insert as we can't trust it after 2508 * the previous insert. 2509 */ 2510 error = xfs_bmbt_lookup_eq(cur, new, &i); 2511 if (error) 2512 goto done; 2513 if (XFS_IS_CORRUPT(mp, i != 0)) { 2514 xfs_btree_mark_sick(cur); 2515 error = -EFSCORRUPTED; 2516 goto done; 2517 } 2518 /* new middle extent - newext */ 2519 if ((error = xfs_btree_insert(cur, &i))) 2520 goto done; 2521 if (XFS_IS_CORRUPT(mp, i != 1)) { 2522 xfs_btree_mark_sick(cur); 2523 error = -EFSCORRUPTED; 2524 goto done; 2525 } 2526 } 2527 break; 2528 2529 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: 2530 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: 2531 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: 2532 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: 2533 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: 2534 case BMAP_LEFT_CONTIG: 2535 case BMAP_RIGHT_CONTIG: 2536 /* 2537 * These cases are all impossible. 2538 */ 2539 ASSERT(0); 2540 } 2541 2542 /* update reverse mappings */ 2543 xfs_rmap_convert_extent(mp, tp, ip, whichfork, new); 2544 2545 /* convert to a btree if necessary */ 2546 if (xfs_bmap_needs_btree(ip, whichfork)) { 2547 int tmp_logflags; /* partial log flag return val */ 2548 2549 ASSERT(cur == NULL); 2550 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, 2551 &tmp_logflags, whichfork); 2552 *logflagsp |= tmp_logflags; 2553 if (error) 2554 goto done; 2555 } 2556 2557 /* clear out the allocated field, done with it now in any case. */ 2558 if (cur) { 2559 cur->bc_bmap.allocated = 0; 2560 *curp = cur; 2561 } 2562 2563 xfs_bmap_check_leaf_extents(*curp, ip, whichfork); 2564 done: 2565 *logflagsp |= rval; 2566 return error; 2567 #undef LEFT 2568 #undef RIGHT 2569 #undef PREV 2570 } 2571 2572 /* 2573 * Convert a hole to a real allocation. 2574 */ 2575 STATIC int /* error */ 2576 xfs_bmap_add_extent_hole_real( 2577 struct xfs_trans *tp, 2578 struct xfs_inode *ip, 2579 int whichfork, 2580 struct xfs_iext_cursor *icur, 2581 struct xfs_btree_cur **curp, 2582 struct xfs_bmbt_irec *new, 2583 int *logflagsp, 2584 uint32_t flags) 2585 { 2586 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); 2587 struct xfs_mount *mp = ip->i_mount; 2588 struct xfs_btree_cur *cur = *curp; 2589 int error; /* error return value */ 2590 int i; /* temp state */ 2591 xfs_bmbt_irec_t left; /* left neighbor extent entry */ 2592 xfs_bmbt_irec_t right; /* right neighbor extent entry */ 2593 int rval=0; /* return value (logging flags) */ 2594 uint32_t state = xfs_bmap_fork_to_state(whichfork); 2595 struct xfs_bmbt_irec old; 2596 2597 ASSERT(!isnullstartblock(new->br_startblock)); 2598 ASSERT(!cur || !(cur->bc_flags & XFS_BTREE_BMBT_WASDEL)); 2599 2600 XFS_STATS_INC(mp, xs_add_exlist); 2601 2602 /* 2603 * Check and set flags if this segment has a left neighbor. 2604 */ 2605 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) { 2606 state |= BMAP_LEFT_VALID; 2607 if (isnullstartblock(left.br_startblock)) 2608 state |= BMAP_LEFT_DELAY; 2609 } 2610 2611 /* 2612 * Check and set flags if this segment has a current value. 2613 * Not true if we're inserting into the "hole" at eof. 2614 */ 2615 if (xfs_iext_get_extent(ifp, icur, &right)) { 2616 state |= BMAP_RIGHT_VALID; 2617 if (isnullstartblock(right.br_startblock)) 2618 state |= BMAP_RIGHT_DELAY; 2619 } 2620 2621 /* 2622 * We're inserting a real allocation between "left" and "right". 2623 * Set the contiguity flags. Don't let extents get too large. 2624 */ 2625 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && 2626 left.br_startoff + left.br_blockcount == new->br_startoff && 2627 left.br_startblock + left.br_blockcount == new->br_startblock && 2628 left.br_state == new->br_state && 2629 left.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN && 2630 xfs_bmap_same_rtgroup(ip, whichfork, &left, new)) 2631 state |= BMAP_LEFT_CONTIG; 2632 2633 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && 2634 new->br_startoff + new->br_blockcount == right.br_startoff && 2635 new->br_startblock + new->br_blockcount == right.br_startblock && 2636 new->br_state == right.br_state && 2637 new->br_blockcount + right.br_blockcount <= XFS_MAX_BMBT_EXTLEN && 2638 (!(state & BMAP_LEFT_CONTIG) || 2639 left.br_blockcount + new->br_blockcount + 2640 right.br_blockcount <= XFS_MAX_BMBT_EXTLEN) && 2641 xfs_bmap_same_rtgroup(ip, whichfork, new, &right)) 2642 state |= BMAP_RIGHT_CONTIG; 2643 2644 error = 0; 2645 /* 2646 * Select which case we're in here, and implement it. 2647 */ 2648 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { 2649 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: 2650 /* 2651 * New allocation is contiguous with real allocations on the 2652 * left and on the right. 2653 * Merge all three into a single extent record. 2654 */ 2655 left.br_blockcount += new->br_blockcount + right.br_blockcount; 2656 2657 xfs_iext_remove(ip, icur, state); 2658 xfs_iext_prev(ifp, icur); 2659 xfs_iext_update_extent(ip, state, icur, &left); 2660 ifp->if_nextents--; 2661 2662 if (cur == NULL) { 2663 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); 2664 } else { 2665 rval = XFS_ILOG_CORE; 2666 error = xfs_bmbt_lookup_eq(cur, &right, &i); 2667 if (error) 2668 goto done; 2669 if (XFS_IS_CORRUPT(mp, i != 1)) { 2670 xfs_btree_mark_sick(cur); 2671 error = -EFSCORRUPTED; 2672 goto done; 2673 } 2674 error = xfs_btree_delete(cur, &i); 2675 if (error) 2676 goto done; 2677 if (XFS_IS_CORRUPT(mp, i != 1)) { 2678 xfs_btree_mark_sick(cur); 2679 error = -EFSCORRUPTED; 2680 goto done; 2681 } 2682 error = xfs_btree_decrement(cur, 0, &i); 2683 if (error) 2684 goto done; 2685 if (XFS_IS_CORRUPT(mp, i != 1)) { 2686 xfs_btree_mark_sick(cur); 2687 error = -EFSCORRUPTED; 2688 goto done; 2689 } 2690 error = xfs_bmbt_update(cur, &left); 2691 if (error) 2692 goto done; 2693 } 2694 break; 2695 2696 case BMAP_LEFT_CONTIG: 2697 /* 2698 * New allocation is contiguous with a real allocation 2699 * on the left. 2700 * Merge the new allocation with the left neighbor. 2701 */ 2702 old = left; 2703 left.br_blockcount += new->br_blockcount; 2704 2705 xfs_iext_prev(ifp, icur); 2706 xfs_iext_update_extent(ip, state, icur, &left); 2707 2708 if (cur == NULL) { 2709 rval = xfs_ilog_fext(whichfork); 2710 } else { 2711 rval = 0; 2712 error = xfs_bmbt_lookup_eq(cur, &old, &i); 2713 if (error) 2714 goto done; 2715 if (XFS_IS_CORRUPT(mp, i != 1)) { 2716 xfs_btree_mark_sick(cur); 2717 error = -EFSCORRUPTED; 2718 goto done; 2719 } 2720 error = xfs_bmbt_update(cur, &left); 2721 if (error) 2722 goto done; 2723 } 2724 break; 2725 2726 case BMAP_RIGHT_CONTIG: 2727 /* 2728 * New allocation is contiguous with a real allocation 2729 * on the right. 2730 * Merge the new allocation with the right neighbor. 2731 */ 2732 old = right; 2733 2734 right.br_startoff = new->br_startoff; 2735 right.br_startblock = new->br_startblock; 2736 right.br_blockcount += new->br_blockcount; 2737 xfs_iext_update_extent(ip, state, icur, &right); 2738 2739 if (cur == NULL) { 2740 rval = xfs_ilog_fext(whichfork); 2741 } else { 2742 rval = 0; 2743 error = xfs_bmbt_lookup_eq(cur, &old, &i); 2744 if (error) 2745 goto done; 2746 if (XFS_IS_CORRUPT(mp, i != 1)) { 2747 xfs_btree_mark_sick(cur); 2748 error = -EFSCORRUPTED; 2749 goto done; 2750 } 2751 error = xfs_bmbt_update(cur, &right); 2752 if (error) 2753 goto done; 2754 } 2755 break; 2756 2757 case 0: 2758 /* 2759 * New allocation is not contiguous with another 2760 * real allocation. 2761 * Insert a new entry. 2762 */ 2763 xfs_iext_insert(ip, icur, new, state); 2764 ifp->if_nextents++; 2765 2766 if (cur == NULL) { 2767 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); 2768 } else { 2769 rval = XFS_ILOG_CORE; 2770 error = xfs_bmbt_lookup_eq(cur, new, &i); 2771 if (error) 2772 goto done; 2773 if (XFS_IS_CORRUPT(mp, i != 0)) { 2774 xfs_btree_mark_sick(cur); 2775 error = -EFSCORRUPTED; 2776 goto done; 2777 } 2778 error = xfs_btree_insert(cur, &i); 2779 if (error) 2780 goto done; 2781 if (XFS_IS_CORRUPT(mp, i != 1)) { 2782 xfs_btree_mark_sick(cur); 2783 error = -EFSCORRUPTED; 2784 goto done; 2785 } 2786 } 2787 break; 2788 } 2789 2790 /* add reverse mapping unless caller opted out */ 2791 if (!(flags & XFS_BMAPI_NORMAP)) 2792 xfs_rmap_map_extent(tp, ip, whichfork, new); 2793 2794 /* convert to a btree if necessary */ 2795 if (xfs_bmap_needs_btree(ip, whichfork)) { 2796 int tmp_logflags; /* partial log flag return val */ 2797 2798 ASSERT(cur == NULL); 2799 error = xfs_bmap_extents_to_btree(tp, ip, curp, 0, 2800 &tmp_logflags, whichfork); 2801 *logflagsp |= tmp_logflags; 2802 cur = *curp; 2803 if (error) 2804 goto done; 2805 } 2806 2807 /* clear out the allocated field, done with it now in any case. */ 2808 if (cur) 2809 cur->bc_bmap.allocated = 0; 2810 2811 xfs_bmap_check_leaf_extents(cur, ip, whichfork); 2812 done: 2813 *logflagsp |= rval; 2814 return error; 2815 } 2816 2817 /* 2818 * Functions used in the extent read, allocate and remove paths 2819 */ 2820 2821 /* 2822 * Adjust the size of the new extent based on i_extsize and rt extsize. 2823 */ 2824 int 2825 xfs_bmap_extsize_align( 2826 xfs_mount_t *mp, 2827 xfs_bmbt_irec_t *gotp, /* next extent pointer */ 2828 xfs_bmbt_irec_t *prevp, /* previous extent pointer */ 2829 xfs_extlen_t extsz, /* align to this extent size */ 2830 int rt, /* is this a realtime inode? */ 2831 int eof, /* is extent at end-of-file? */ 2832 int delay, /* creating delalloc extent? */ 2833 int convert, /* overwriting unwritten extent? */ 2834 xfs_fileoff_t *offp, /* in/out: aligned offset */ 2835 xfs_extlen_t *lenp) /* in/out: aligned length */ 2836 { 2837 xfs_fileoff_t orig_off; /* original offset */ 2838 xfs_extlen_t orig_alen; /* original length */ 2839 xfs_fileoff_t orig_end; /* original off+len */ 2840 xfs_fileoff_t nexto; /* next file offset */ 2841 xfs_fileoff_t prevo; /* previous file offset */ 2842 xfs_fileoff_t align_off; /* temp for offset */ 2843 xfs_extlen_t align_alen; /* temp for length */ 2844 xfs_extlen_t temp; /* temp for calculations */ 2845 2846 if (convert) 2847 return 0; 2848 2849 orig_off = align_off = *offp; 2850 orig_alen = align_alen = *lenp; 2851 orig_end = orig_off + orig_alen; 2852 2853 /* 2854 * If this request overlaps an existing extent, then don't 2855 * attempt to perform any additional alignment. 2856 */ 2857 if (!delay && !eof && 2858 (orig_off >= gotp->br_startoff) && 2859 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) { 2860 return 0; 2861 } 2862 2863 /* 2864 * If the file offset is unaligned vs. the extent size 2865 * we need to align it. This will be possible unless 2866 * the file was previously written with a kernel that didn't 2867 * perform this alignment, or if a truncate shot us in the 2868 * foot. 2869 */ 2870 div_u64_rem(orig_off, extsz, &temp); 2871 if (temp) { 2872 align_alen += temp; 2873 align_off -= temp; 2874 } 2875 2876 /* Same adjustment for the end of the requested area. */ 2877 temp = (align_alen % extsz); 2878 if (temp) 2879 align_alen += extsz - temp; 2880 2881 /* 2882 * For large extent hint sizes, the aligned extent might be larger than 2883 * XFS_BMBT_MAX_EXTLEN. In that case, reduce the size by an extsz so 2884 * that it pulls the length back under XFS_BMBT_MAX_EXTLEN. The outer 2885 * allocation loops handle short allocation just fine, so it is safe to 2886 * do this. We only want to do it when we are forced to, though, because 2887 * it means more allocation operations are required. 2888 */ 2889 while (align_alen > XFS_MAX_BMBT_EXTLEN) 2890 align_alen -= extsz; 2891 ASSERT(align_alen <= XFS_MAX_BMBT_EXTLEN); 2892 2893 /* 2894 * If the previous block overlaps with this proposed allocation 2895 * then move the start forward without adjusting the length. 2896 */ 2897 if (prevp->br_startoff != NULLFILEOFF) { 2898 if (prevp->br_startblock == HOLESTARTBLOCK) 2899 prevo = prevp->br_startoff; 2900 else 2901 prevo = prevp->br_startoff + prevp->br_blockcount; 2902 } else 2903 prevo = 0; 2904 if (align_off != orig_off && align_off < prevo) 2905 align_off = prevo; 2906 /* 2907 * If the next block overlaps with this proposed allocation 2908 * then move the start back without adjusting the length, 2909 * but not before offset 0. 2910 * This may of course make the start overlap previous block, 2911 * and if we hit the offset 0 limit then the next block 2912 * can still overlap too. 2913 */ 2914 if (!eof && gotp->br_startoff != NULLFILEOFF) { 2915 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) || 2916 (!delay && gotp->br_startblock == DELAYSTARTBLOCK)) 2917 nexto = gotp->br_startoff + gotp->br_blockcount; 2918 else 2919 nexto = gotp->br_startoff; 2920 } else 2921 nexto = NULLFILEOFF; 2922 if (!eof && 2923 align_off + align_alen != orig_end && 2924 align_off + align_alen > nexto) 2925 align_off = nexto > align_alen ? nexto - align_alen : 0; 2926 /* 2927 * If we're now overlapping the next or previous extent that 2928 * means we can't fit an extsz piece in this hole. Just move 2929 * the start forward to the first valid spot and set 2930 * the length so we hit the end. 2931 */ 2932 if (align_off != orig_off && align_off < prevo) 2933 align_off = prevo; 2934 if (align_off + align_alen != orig_end && 2935 align_off + align_alen > nexto && 2936 nexto != NULLFILEOFF) { 2937 ASSERT(nexto > prevo); 2938 align_alen = nexto - align_off; 2939 } 2940 2941 /* 2942 * If realtime, and the result isn't a multiple of the realtime 2943 * extent size we need to remove blocks until it is. 2944 */ 2945 if (rt && (temp = xfs_extlen_to_rtxmod(mp, align_alen))) { 2946 /* 2947 * We're not covering the original request, or 2948 * we won't be able to once we fix the length. 2949 */ 2950 if (orig_off < align_off || 2951 orig_end > align_off + align_alen || 2952 align_alen - temp < orig_alen) 2953 return -EINVAL; 2954 /* 2955 * Try to fix it by moving the start up. 2956 */ 2957 if (align_off + temp <= orig_off) { 2958 align_alen -= temp; 2959 align_off += temp; 2960 } 2961 /* 2962 * Try to fix it by moving the end in. 2963 */ 2964 else if (align_off + align_alen - temp >= orig_end) 2965 align_alen -= temp; 2966 /* 2967 * Set the start to the minimum then trim the length. 2968 */ 2969 else { 2970 align_alen -= orig_off - align_off; 2971 align_off = orig_off; 2972 align_alen -= xfs_extlen_to_rtxmod(mp, align_alen); 2973 } 2974 /* 2975 * Result doesn't cover the request, fail it. 2976 */ 2977 if (orig_off < align_off || orig_end > align_off + align_alen) 2978 return -EINVAL; 2979 } else { 2980 ASSERT(orig_off >= align_off); 2981 /* see XFS_BMBT_MAX_EXTLEN handling above */ 2982 ASSERT(orig_end <= align_off + align_alen || 2983 align_alen + extsz > XFS_MAX_BMBT_EXTLEN); 2984 } 2985 2986 #ifdef DEBUG 2987 if (!eof && gotp->br_startoff != NULLFILEOFF) 2988 ASSERT(align_off + align_alen <= gotp->br_startoff); 2989 if (prevp->br_startoff != NULLFILEOFF) 2990 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount); 2991 #endif 2992 2993 *lenp = align_alen; 2994 *offp = align_off; 2995 return 0; 2996 } 2997 2998 static inline bool 2999 xfs_bmap_adjacent_valid( 3000 struct xfs_bmalloca *ap, 3001 xfs_fsblock_t x, 3002 xfs_fsblock_t y) 3003 { 3004 struct xfs_mount *mp = ap->ip->i_mount; 3005 3006 if (XFS_IS_REALTIME_INODE(ap->ip) && 3007 (ap->datatype & XFS_ALLOC_USERDATA)) { 3008 if (!xfs_has_rtgroups(mp)) 3009 return x < mp->m_sb.sb_rblocks; 3010 3011 return xfs_rtb_to_rgno(mp, x) == xfs_rtb_to_rgno(mp, y) && 3012 xfs_rtb_to_rgno(mp, x) < mp->m_sb.sb_rgcount && 3013 xfs_rtb_to_rtx(mp, x) < mp->m_sb.sb_rgextents; 3014 3015 } 3016 3017 return XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && 3018 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && 3019 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks; 3020 } 3021 3022 #define XFS_ALLOC_GAP_UNITS 4 3023 3024 /* returns true if ap->blkno was modified */ 3025 bool 3026 xfs_bmap_adjacent( 3027 struct xfs_bmalloca *ap) /* bmap alloc argument struct */ 3028 { 3029 xfs_fsblock_t adjust; /* adjustment to block numbers */ 3030 3031 /* 3032 * If allocating at eof, and there's a previous real block, 3033 * try to use its last block as our starting point. 3034 */ 3035 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF && 3036 !isnullstartblock(ap->prev.br_startblock) && 3037 xfs_bmap_adjacent_valid(ap, 3038 ap->prev.br_startblock + ap->prev.br_blockcount, 3039 ap->prev.br_startblock)) { 3040 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount; 3041 /* 3042 * Adjust for the gap between prevp and us. 3043 */ 3044 adjust = ap->offset - 3045 (ap->prev.br_startoff + ap->prev.br_blockcount); 3046 if (adjust && xfs_bmap_adjacent_valid(ap, ap->blkno + adjust, 3047 ap->prev.br_startblock)) 3048 ap->blkno += adjust; 3049 return true; 3050 } 3051 /* 3052 * If not at eof, then compare the two neighbor blocks. 3053 * Figure out whether either one gives us a good starting point, 3054 * and pick the better one. 3055 */ 3056 if (!ap->eof) { 3057 xfs_fsblock_t gotbno; /* right side block number */ 3058 xfs_fsblock_t gotdiff=0; /* right side difference */ 3059 xfs_fsblock_t prevbno; /* left side block number */ 3060 xfs_fsblock_t prevdiff=0; /* left side difference */ 3061 3062 /* 3063 * If there's a previous (left) block, select a requested 3064 * start block based on it. 3065 */ 3066 if (ap->prev.br_startoff != NULLFILEOFF && 3067 !isnullstartblock(ap->prev.br_startblock) && 3068 (prevbno = ap->prev.br_startblock + 3069 ap->prev.br_blockcount) && 3070 xfs_bmap_adjacent_valid(ap, prevbno, 3071 ap->prev.br_startblock)) { 3072 /* 3073 * Calculate gap to end of previous block. 3074 */ 3075 adjust = prevdiff = ap->offset - 3076 (ap->prev.br_startoff + 3077 ap->prev.br_blockcount); 3078 /* 3079 * Figure the startblock based on the previous block's 3080 * end and the gap size. 3081 * Heuristic! 3082 * If the gap is large relative to the piece we're 3083 * allocating, or using it gives us an invalid block 3084 * number, then just use the end of the previous block. 3085 */ 3086 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length && 3087 xfs_bmap_adjacent_valid(ap, prevbno + prevdiff, 3088 ap->prev.br_startblock)) 3089 prevbno += adjust; 3090 else 3091 prevdiff += adjust; 3092 } 3093 /* 3094 * No previous block or can't follow it, just default. 3095 */ 3096 else 3097 prevbno = NULLFSBLOCK; 3098 /* 3099 * If there's a following (right) block, select a requested 3100 * start block based on it. 3101 */ 3102 if (!isnullstartblock(ap->got.br_startblock)) { 3103 /* 3104 * Calculate gap to start of next block. 3105 */ 3106 adjust = gotdiff = ap->got.br_startoff - ap->offset; 3107 /* 3108 * Figure the startblock based on the next block's 3109 * start and the gap size. 3110 */ 3111 gotbno = ap->got.br_startblock; 3112 /* 3113 * Heuristic! 3114 * If the gap is large relative to the piece we're 3115 * allocating, or using it gives us an invalid block 3116 * number, then just use the start of the next block 3117 * offset by our length. 3118 */ 3119 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length && 3120 xfs_bmap_adjacent_valid(ap, gotbno - gotdiff, 3121 gotbno)) 3122 gotbno -= adjust; 3123 else if (xfs_bmap_adjacent_valid(ap, gotbno - ap->length, 3124 gotbno)) { 3125 gotbno -= ap->length; 3126 gotdiff += adjust - ap->length; 3127 } else 3128 gotdiff += adjust; 3129 } 3130 /* 3131 * No next block, just default. 3132 */ 3133 else 3134 gotbno = NULLFSBLOCK; 3135 /* 3136 * If both valid, pick the better one, else the only good 3137 * one, else ap->blkno is already set (to 0 or the inode block). 3138 */ 3139 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) { 3140 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno; 3141 return true; 3142 } 3143 if (prevbno != NULLFSBLOCK) { 3144 ap->blkno = prevbno; 3145 return true; 3146 } 3147 if (gotbno != NULLFSBLOCK) { 3148 ap->blkno = gotbno; 3149 return true; 3150 } 3151 } 3152 3153 return false; 3154 } 3155 3156 int 3157 xfs_bmap_longest_free_extent( 3158 struct xfs_perag *pag, 3159 struct xfs_trans *tp, 3160 xfs_extlen_t *blen) 3161 { 3162 xfs_extlen_t longest; 3163 int error = 0; 3164 3165 if (!xfs_perag_initialised_agf(pag)) { 3166 error = xfs_alloc_read_agf(pag, tp, XFS_ALLOC_FLAG_TRYLOCK, 3167 NULL); 3168 if (error) 3169 return error; 3170 } 3171 3172 longest = xfs_alloc_longest_free_extent(pag, 3173 xfs_alloc_min_freelist(pag_mount(pag), pag), 3174 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE)); 3175 if (*blen < longest) 3176 *blen = longest; 3177 3178 return 0; 3179 } 3180 3181 static xfs_extlen_t 3182 xfs_bmap_select_minlen( 3183 struct xfs_bmalloca *ap, 3184 struct xfs_alloc_arg *args, 3185 xfs_extlen_t blen) 3186 { 3187 3188 /* 3189 * Since we used XFS_ALLOC_FLAG_TRYLOCK in _longest_free_extent(), it is 3190 * possible that there is enough contiguous free space for this request. 3191 */ 3192 if (blen < ap->minlen) 3193 return ap->minlen; 3194 3195 /* 3196 * If the best seen length is less than the request length, 3197 * use the best as the minimum, otherwise we've got the maxlen we 3198 * were asked for. 3199 */ 3200 if (blen < args->maxlen) 3201 return blen; 3202 return args->maxlen; 3203 } 3204 3205 static int 3206 xfs_bmap_btalloc_select_lengths( 3207 struct xfs_bmalloca *ap, 3208 struct xfs_alloc_arg *args, 3209 xfs_extlen_t *blen) 3210 { 3211 struct xfs_mount *mp = args->mp; 3212 struct xfs_perag *pag; 3213 xfs_agnumber_t agno, startag; 3214 int error = 0; 3215 3216 if (ap->tp->t_flags & XFS_TRANS_LOWMODE) { 3217 args->total = ap->minlen; 3218 args->minlen = ap->minlen; 3219 return 0; 3220 } 3221 3222 args->total = ap->total; 3223 startag = XFS_FSB_TO_AGNO(mp, ap->blkno); 3224 if (startag == NULLAGNUMBER) 3225 startag = 0; 3226 3227 *blen = 0; 3228 for_each_perag_wrap(mp, startag, agno, pag) { 3229 error = xfs_bmap_longest_free_extent(pag, args->tp, blen); 3230 if (error && error != -EAGAIN) 3231 break; 3232 error = 0; 3233 if (*blen >= args->maxlen) 3234 break; 3235 } 3236 if (pag) 3237 xfs_perag_rele(pag); 3238 3239 args->minlen = xfs_bmap_select_minlen(ap, args, *blen); 3240 return error; 3241 } 3242 3243 /* Update all inode and quota accounting for the allocation we just did. */ 3244 void 3245 xfs_bmap_alloc_account( 3246 struct xfs_bmalloca *ap) 3247 { 3248 bool isrt = XFS_IS_REALTIME_INODE(ap->ip) && 3249 !(ap->flags & XFS_BMAPI_ATTRFORK); 3250 uint fld; 3251 3252 if (ap->flags & XFS_BMAPI_COWFORK) { 3253 /* 3254 * COW fork blocks are in-core only and thus are treated as 3255 * in-core quota reservation (like delalloc blocks) even when 3256 * converted to real blocks. The quota reservation is not 3257 * accounted to disk until blocks are remapped to the data 3258 * fork. So if these blocks were previously delalloc, we 3259 * already have quota reservation and there's nothing to do 3260 * yet. 3261 */ 3262 if (ap->wasdel) { 3263 xfs_mod_delalloc(ap->ip, -(int64_t)ap->length, 0); 3264 return; 3265 } 3266 3267 /* 3268 * Otherwise, we've allocated blocks in a hole. The transaction 3269 * has acquired in-core quota reservation for this extent. 3270 * Rather than account these as real blocks, however, we reduce 3271 * the transaction quota reservation based on the allocation. 3272 * This essentially transfers the transaction quota reservation 3273 * to that of a delalloc extent. 3274 */ 3275 ap->ip->i_delayed_blks += ap->length; 3276 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, isrt ? 3277 XFS_TRANS_DQ_RES_RTBLKS : XFS_TRANS_DQ_RES_BLKS, 3278 -(long)ap->length); 3279 return; 3280 } 3281 3282 /* data/attr fork only */ 3283 ap->ip->i_nblocks += ap->length; 3284 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); 3285 if (ap->wasdel) { 3286 ap->ip->i_delayed_blks -= ap->length; 3287 xfs_mod_delalloc(ap->ip, -(int64_t)ap->length, 0); 3288 fld = isrt ? XFS_TRANS_DQ_DELRTBCOUNT : XFS_TRANS_DQ_DELBCOUNT; 3289 } else { 3290 fld = isrt ? XFS_TRANS_DQ_RTBCOUNT : XFS_TRANS_DQ_BCOUNT; 3291 } 3292 3293 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, fld, ap->length); 3294 } 3295 3296 static int 3297 xfs_bmap_compute_alignments( 3298 struct xfs_bmalloca *ap, 3299 struct xfs_alloc_arg *args) 3300 { 3301 struct xfs_mount *mp = args->mp; 3302 xfs_extlen_t align = 0; /* minimum allocation alignment */ 3303 int stripe_align = 0; 3304 3305 /* stripe alignment for allocation is determined by mount parameters */ 3306 if (mp->m_swidth && xfs_has_swalloc(mp)) 3307 stripe_align = mp->m_swidth; 3308 else if (mp->m_dalign) 3309 stripe_align = mp->m_dalign; 3310 3311 if (ap->flags & XFS_BMAPI_COWFORK) 3312 align = xfs_get_cowextsz_hint(ap->ip); 3313 else if (ap->datatype & XFS_ALLOC_USERDATA) 3314 align = xfs_get_extsz_hint(ap->ip); 3315 3316 /* Try to align start block to any minimum allocation alignment */ 3317 if (align > 1 && (ap->flags & XFS_BMAPI_EXTSZALIGN)) 3318 args->alignment = align; 3319 3320 if (align) { 3321 if (xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, align, 0, 3322 ap->eof, 0, ap->conv, &ap->offset, 3323 &ap->length)) 3324 ASSERT(0); 3325 ASSERT(ap->length); 3326 } 3327 3328 /* apply extent size hints if obtained earlier */ 3329 if (align) { 3330 args->prod = align; 3331 div_u64_rem(ap->offset, args->prod, &args->mod); 3332 if (args->mod) 3333 args->mod = args->prod - args->mod; 3334 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) { 3335 args->prod = 1; 3336 args->mod = 0; 3337 } else { 3338 args->prod = PAGE_SIZE >> mp->m_sb.sb_blocklog; 3339 div_u64_rem(ap->offset, args->prod, &args->mod); 3340 if (args->mod) 3341 args->mod = args->prod - args->mod; 3342 } 3343 3344 return stripe_align; 3345 } 3346 3347 static void 3348 xfs_bmap_process_allocated_extent( 3349 struct xfs_bmalloca *ap, 3350 struct xfs_alloc_arg *args, 3351 xfs_fileoff_t orig_offset, 3352 xfs_extlen_t orig_length) 3353 { 3354 ap->blkno = args->fsbno; 3355 ap->length = args->len; 3356 /* 3357 * If the extent size hint is active, we tried to round the 3358 * caller's allocation request offset down to extsz and the 3359 * length up to another extsz boundary. If we found a free 3360 * extent we mapped it in starting at this new offset. If the 3361 * newly mapped space isn't long enough to cover any of the 3362 * range of offsets that was originally requested, move the 3363 * mapping up so that we can fill as much of the caller's 3364 * original request as possible. Free space is apparently 3365 * very fragmented so we're unlikely to be able to satisfy the 3366 * hints anyway. 3367 */ 3368 if (ap->length <= orig_length) 3369 ap->offset = orig_offset; 3370 else if (ap->offset + ap->length < orig_offset + orig_length) 3371 ap->offset = orig_offset + orig_length - ap->length; 3372 xfs_bmap_alloc_account(ap); 3373 } 3374 3375 static int 3376 xfs_bmap_exact_minlen_extent_alloc( 3377 struct xfs_bmalloca *ap, 3378 struct xfs_alloc_arg *args) 3379 { 3380 if (ap->minlen != 1) { 3381 args->fsbno = NULLFSBLOCK; 3382 return 0; 3383 } 3384 3385 args->alloc_minlen_only = 1; 3386 args->minlen = args->maxlen = ap->minlen; 3387 args->total = ap->total; 3388 3389 /* 3390 * Unlike the longest extent available in an AG, we don't track 3391 * the length of an AG's shortest extent. 3392 * XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT is a debug only knob and 3393 * hence we can afford to start traversing from the 0th AG since 3394 * we need not be concerned about a drop in performance in 3395 * "debug only" code paths. 3396 */ 3397 ap->blkno = XFS_AGB_TO_FSB(ap->ip->i_mount, 0, 0); 3398 3399 /* 3400 * Call xfs_bmap_btalloc_low_space here as it first does a "normal" AG 3401 * iteration and then drops args->total to args->minlen, which might be 3402 * required to find an allocation for the transaction reservation when 3403 * the file system is very full. 3404 */ 3405 return xfs_bmap_btalloc_low_space(ap, args); 3406 } 3407 3408 /* 3409 * If we are not low on available data blocks and we are allocating at 3410 * EOF, optimise allocation for contiguous file extension and/or stripe 3411 * alignment of the new extent. 3412 * 3413 * NOTE: ap->aeof is only set if the allocation length is >= the 3414 * stripe unit and the allocation offset is at the end of file. 3415 */ 3416 static int 3417 xfs_bmap_btalloc_at_eof( 3418 struct xfs_bmalloca *ap, 3419 struct xfs_alloc_arg *args, 3420 xfs_extlen_t blen, 3421 int stripe_align, 3422 bool ag_only) 3423 { 3424 struct xfs_mount *mp = args->mp; 3425 struct xfs_perag *caller_pag = args->pag; 3426 int error; 3427 3428 /* 3429 * If there are already extents in the file, and xfs_bmap_adjacent() has 3430 * given a better blkno, try an exact EOF block allocation to extend the 3431 * file as a contiguous extent. If that fails, or it's the first 3432 * allocation in a file, just try for a stripe aligned allocation. 3433 */ 3434 if (ap->eof) { 3435 xfs_extlen_t nextminlen = 0; 3436 3437 /* 3438 * Compute the minlen+alignment for the next case. Set slop so 3439 * that the value of minlen+alignment+slop doesn't go up between 3440 * the calls. 3441 */ 3442 args->alignment = 1; 3443 if (blen > stripe_align && blen <= args->maxlen) 3444 nextminlen = blen - stripe_align; 3445 else 3446 nextminlen = args->minlen; 3447 if (nextminlen + stripe_align > args->minlen + 1) 3448 args->minalignslop = nextminlen + stripe_align - 3449 args->minlen - 1; 3450 else 3451 args->minalignslop = 0; 3452 3453 if (!caller_pag) 3454 args->pag = xfs_perag_get(mp, XFS_FSB_TO_AGNO(mp, ap->blkno)); 3455 error = xfs_alloc_vextent_exact_bno(args, ap->blkno); 3456 if (!caller_pag) { 3457 xfs_perag_put(args->pag); 3458 args->pag = NULL; 3459 } 3460 if (error) 3461 return error; 3462 3463 if (args->fsbno != NULLFSBLOCK) 3464 return 0; 3465 /* 3466 * Exact allocation failed. Reset to try an aligned allocation 3467 * according to the original allocation specification. 3468 */ 3469 args->alignment = stripe_align; 3470 args->minlen = nextminlen; 3471 args->minalignslop = 0; 3472 } else { 3473 /* 3474 * Adjust minlen to try and preserve alignment if we 3475 * can't guarantee an aligned maxlen extent. 3476 */ 3477 args->alignment = stripe_align; 3478 if (blen > args->alignment && 3479 blen <= args->maxlen + args->alignment) 3480 args->minlen = blen - args->alignment; 3481 args->minalignslop = 0; 3482 } 3483 3484 if (ag_only) { 3485 error = xfs_alloc_vextent_near_bno(args, ap->blkno); 3486 } else { 3487 args->pag = NULL; 3488 error = xfs_alloc_vextent_start_ag(args, ap->blkno); 3489 ASSERT(args->pag == NULL); 3490 args->pag = caller_pag; 3491 } 3492 if (error) 3493 return error; 3494 3495 if (args->fsbno != NULLFSBLOCK) 3496 return 0; 3497 3498 /* 3499 * Allocation failed, so turn return the allocation args to their 3500 * original non-aligned state so the caller can proceed on allocation 3501 * failure as if this function was never called. 3502 */ 3503 args->alignment = 1; 3504 return 0; 3505 } 3506 3507 /* 3508 * We have failed multiple allocation attempts so now are in a low space 3509 * allocation situation. Try a locality first full filesystem minimum length 3510 * allocation whilst still maintaining necessary total block reservation 3511 * requirements. 3512 * 3513 * If that fails, we are now critically low on space, so perform a last resort 3514 * allocation attempt: no reserve, no locality, blocking, minimum length, full 3515 * filesystem free space scan. We also indicate to future allocations in this 3516 * transaction that we are critically low on space so they don't waste time on 3517 * allocation modes that are unlikely to succeed. 3518 */ 3519 int 3520 xfs_bmap_btalloc_low_space( 3521 struct xfs_bmalloca *ap, 3522 struct xfs_alloc_arg *args) 3523 { 3524 int error; 3525 3526 if (args->minlen > ap->minlen) { 3527 args->minlen = ap->minlen; 3528 error = xfs_alloc_vextent_start_ag(args, ap->blkno); 3529 if (error || args->fsbno != NULLFSBLOCK) 3530 return error; 3531 } 3532 3533 /* Last ditch attempt before failure is declared. */ 3534 args->total = ap->minlen; 3535 error = xfs_alloc_vextent_first_ag(args, 0); 3536 if (error) 3537 return error; 3538 ap->tp->t_flags |= XFS_TRANS_LOWMODE; 3539 return 0; 3540 } 3541 3542 static int 3543 xfs_bmap_btalloc_filestreams( 3544 struct xfs_bmalloca *ap, 3545 struct xfs_alloc_arg *args, 3546 int stripe_align) 3547 { 3548 xfs_extlen_t blen = 0; 3549 int error = 0; 3550 3551 3552 error = xfs_filestream_select_ag(ap, args, &blen); 3553 if (error) 3554 return error; 3555 ASSERT(args->pag); 3556 3557 /* 3558 * If we are in low space mode, then optimal allocation will fail so 3559 * prepare for minimal allocation and jump to the low space algorithm 3560 * immediately. 3561 */ 3562 if (ap->tp->t_flags & XFS_TRANS_LOWMODE) { 3563 args->minlen = ap->minlen; 3564 ASSERT(args->fsbno == NULLFSBLOCK); 3565 goto out_low_space; 3566 } 3567 3568 args->minlen = xfs_bmap_select_minlen(ap, args, blen); 3569 if (ap->aeof) 3570 error = xfs_bmap_btalloc_at_eof(ap, args, blen, stripe_align, 3571 true); 3572 3573 if (!error && args->fsbno == NULLFSBLOCK) 3574 error = xfs_alloc_vextent_near_bno(args, ap->blkno); 3575 3576 out_low_space: 3577 /* 3578 * We are now done with the perag reference for the filestreams 3579 * association provided by xfs_filestream_select_ag(). Release it now as 3580 * we've either succeeded, had a fatal error or we are out of space and 3581 * need to do a full filesystem scan for free space which will take it's 3582 * own references. 3583 */ 3584 xfs_perag_rele(args->pag); 3585 args->pag = NULL; 3586 if (error || args->fsbno != NULLFSBLOCK) 3587 return error; 3588 3589 return xfs_bmap_btalloc_low_space(ap, args); 3590 } 3591 3592 static int 3593 xfs_bmap_btalloc_best_length( 3594 struct xfs_bmalloca *ap, 3595 struct xfs_alloc_arg *args, 3596 int stripe_align) 3597 { 3598 xfs_extlen_t blen = 0; 3599 int error; 3600 3601 ap->blkno = XFS_INO_TO_FSB(args->mp, ap->ip->i_ino); 3602 if (!xfs_bmap_adjacent(ap)) 3603 ap->eof = false; 3604 3605 /* 3606 * Search for an allocation group with a single extent large enough for 3607 * the request. If one isn't found, then adjust the minimum allocation 3608 * size to the largest space found. 3609 */ 3610 error = xfs_bmap_btalloc_select_lengths(ap, args, &blen); 3611 if (error) 3612 return error; 3613 3614 /* 3615 * Don't attempt optimal EOF allocation if previous allocations barely 3616 * succeeded due to being near ENOSPC. It is highly unlikely we'll get 3617 * optimal or even aligned allocations in this case, so don't waste time 3618 * trying. 3619 */ 3620 if (ap->aeof && !(ap->tp->t_flags & XFS_TRANS_LOWMODE)) { 3621 error = xfs_bmap_btalloc_at_eof(ap, args, blen, stripe_align, 3622 false); 3623 if (error || args->fsbno != NULLFSBLOCK) 3624 return error; 3625 } 3626 3627 error = xfs_alloc_vextent_start_ag(args, ap->blkno); 3628 if (error || args->fsbno != NULLFSBLOCK) 3629 return error; 3630 3631 return xfs_bmap_btalloc_low_space(ap, args); 3632 } 3633 3634 static int 3635 xfs_bmap_btalloc( 3636 struct xfs_bmalloca *ap) 3637 { 3638 struct xfs_mount *mp = ap->ip->i_mount; 3639 struct xfs_alloc_arg args = { 3640 .tp = ap->tp, 3641 .mp = mp, 3642 .fsbno = NULLFSBLOCK, 3643 .oinfo = XFS_RMAP_OINFO_SKIP_UPDATE, 3644 .minleft = ap->minleft, 3645 .wasdel = ap->wasdel, 3646 .resv = XFS_AG_RESV_NONE, 3647 .datatype = ap->datatype, 3648 .alignment = 1, 3649 .minalignslop = 0, 3650 }; 3651 xfs_fileoff_t orig_offset; 3652 xfs_extlen_t orig_length; 3653 int error; 3654 int stripe_align; 3655 3656 ASSERT(ap->length); 3657 orig_offset = ap->offset; 3658 orig_length = ap->length; 3659 3660 stripe_align = xfs_bmap_compute_alignments(ap, &args); 3661 3662 /* Trim the allocation back to the maximum an AG can fit. */ 3663 args.maxlen = min(ap->length, mp->m_ag_max_usable); 3664 3665 if (unlikely(XFS_TEST_ERROR(false, mp, 3666 XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT))) 3667 error = xfs_bmap_exact_minlen_extent_alloc(ap, &args); 3668 else if ((ap->datatype & XFS_ALLOC_USERDATA) && 3669 xfs_inode_is_filestream(ap->ip)) 3670 error = xfs_bmap_btalloc_filestreams(ap, &args, stripe_align); 3671 else 3672 error = xfs_bmap_btalloc_best_length(ap, &args, stripe_align); 3673 if (error) 3674 return error; 3675 3676 if (args.fsbno != NULLFSBLOCK) { 3677 xfs_bmap_process_allocated_extent(ap, &args, orig_offset, 3678 orig_length); 3679 } else { 3680 ap->blkno = NULLFSBLOCK; 3681 ap->length = 0; 3682 } 3683 return 0; 3684 } 3685 3686 /* Trim extent to fit a logical block range. */ 3687 void 3688 xfs_trim_extent( 3689 struct xfs_bmbt_irec *irec, 3690 xfs_fileoff_t bno, 3691 xfs_filblks_t len) 3692 { 3693 xfs_fileoff_t distance; 3694 xfs_fileoff_t end = bno + len; 3695 3696 if (irec->br_startoff + irec->br_blockcount <= bno || 3697 irec->br_startoff >= end) { 3698 irec->br_blockcount = 0; 3699 return; 3700 } 3701 3702 if (irec->br_startoff < bno) { 3703 distance = bno - irec->br_startoff; 3704 if (isnullstartblock(irec->br_startblock)) 3705 irec->br_startblock = DELAYSTARTBLOCK; 3706 if (irec->br_startblock != DELAYSTARTBLOCK && 3707 irec->br_startblock != HOLESTARTBLOCK) 3708 irec->br_startblock += distance; 3709 irec->br_startoff += distance; 3710 irec->br_blockcount -= distance; 3711 } 3712 3713 if (end < irec->br_startoff + irec->br_blockcount) { 3714 distance = irec->br_startoff + irec->br_blockcount - end; 3715 irec->br_blockcount -= distance; 3716 } 3717 } 3718 3719 /* 3720 * Trim the returned map to the required bounds 3721 */ 3722 STATIC void 3723 xfs_bmapi_trim_map( 3724 struct xfs_bmbt_irec *mval, 3725 struct xfs_bmbt_irec *got, 3726 xfs_fileoff_t *bno, 3727 xfs_filblks_t len, 3728 xfs_fileoff_t obno, 3729 xfs_fileoff_t end, 3730 int n, 3731 uint32_t flags) 3732 { 3733 if ((flags & XFS_BMAPI_ENTIRE) || 3734 got->br_startoff + got->br_blockcount <= obno) { 3735 *mval = *got; 3736 if (isnullstartblock(got->br_startblock)) 3737 mval->br_startblock = DELAYSTARTBLOCK; 3738 return; 3739 } 3740 3741 if (obno > *bno) 3742 *bno = obno; 3743 ASSERT((*bno >= obno) || (n == 0)); 3744 ASSERT(*bno < end); 3745 mval->br_startoff = *bno; 3746 if (isnullstartblock(got->br_startblock)) 3747 mval->br_startblock = DELAYSTARTBLOCK; 3748 else 3749 mval->br_startblock = got->br_startblock + 3750 (*bno - got->br_startoff); 3751 /* 3752 * Return the minimum of what we got and what we asked for for 3753 * the length. We can use the len variable here because it is 3754 * modified below and we could have been there before coming 3755 * here if the first part of the allocation didn't overlap what 3756 * was asked for. 3757 */ 3758 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno, 3759 got->br_blockcount - (*bno - got->br_startoff)); 3760 mval->br_state = got->br_state; 3761 ASSERT(mval->br_blockcount <= len); 3762 return; 3763 } 3764 3765 /* 3766 * Update and validate the extent map to return 3767 */ 3768 STATIC void 3769 xfs_bmapi_update_map( 3770 struct xfs_bmbt_irec **map, 3771 xfs_fileoff_t *bno, 3772 xfs_filblks_t *len, 3773 xfs_fileoff_t obno, 3774 xfs_fileoff_t end, 3775 int *n, 3776 uint32_t flags) 3777 { 3778 xfs_bmbt_irec_t *mval = *map; 3779 3780 ASSERT((flags & XFS_BMAPI_ENTIRE) || 3781 ((mval->br_startoff + mval->br_blockcount) <= end)); 3782 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) || 3783 (mval->br_startoff < obno)); 3784 3785 *bno = mval->br_startoff + mval->br_blockcount; 3786 *len = end - *bno; 3787 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) { 3788 /* update previous map with new information */ 3789 ASSERT(mval->br_startblock == mval[-1].br_startblock); 3790 ASSERT(mval->br_blockcount > mval[-1].br_blockcount); 3791 ASSERT(mval->br_state == mval[-1].br_state); 3792 mval[-1].br_blockcount = mval->br_blockcount; 3793 mval[-1].br_state = mval->br_state; 3794 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK && 3795 mval[-1].br_startblock != DELAYSTARTBLOCK && 3796 mval[-1].br_startblock != HOLESTARTBLOCK && 3797 mval->br_startblock == mval[-1].br_startblock + 3798 mval[-1].br_blockcount && 3799 mval[-1].br_state == mval->br_state) { 3800 ASSERT(mval->br_startoff == 3801 mval[-1].br_startoff + mval[-1].br_blockcount); 3802 mval[-1].br_blockcount += mval->br_blockcount; 3803 } else if (*n > 0 && 3804 mval->br_startblock == DELAYSTARTBLOCK && 3805 mval[-1].br_startblock == DELAYSTARTBLOCK && 3806 mval->br_startoff == 3807 mval[-1].br_startoff + mval[-1].br_blockcount) { 3808 mval[-1].br_blockcount += mval->br_blockcount; 3809 mval[-1].br_state = mval->br_state; 3810 } else if (!((*n == 0) && 3811 ((mval->br_startoff + mval->br_blockcount) <= 3812 obno))) { 3813 mval++; 3814 (*n)++; 3815 } 3816 *map = mval; 3817 } 3818 3819 /* 3820 * Map file blocks to filesystem blocks without allocation. 3821 */ 3822 int 3823 xfs_bmapi_read( 3824 struct xfs_inode *ip, 3825 xfs_fileoff_t bno, 3826 xfs_filblks_t len, 3827 struct xfs_bmbt_irec *mval, 3828 int *nmap, 3829 uint32_t flags) 3830 { 3831 struct xfs_mount *mp = ip->i_mount; 3832 int whichfork = xfs_bmapi_whichfork(flags); 3833 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); 3834 struct xfs_bmbt_irec got; 3835 xfs_fileoff_t obno; 3836 xfs_fileoff_t end; 3837 struct xfs_iext_cursor icur; 3838 int error; 3839 bool eof = false; 3840 int n = 0; 3841 3842 ASSERT(*nmap >= 1); 3843 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_ENTIRE))); 3844 xfs_assert_ilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL); 3845 3846 if (WARN_ON_ONCE(!ifp)) { 3847 xfs_bmap_mark_sick(ip, whichfork); 3848 return -EFSCORRUPTED; 3849 } 3850 3851 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) || 3852 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) { 3853 xfs_bmap_mark_sick(ip, whichfork); 3854 return -EFSCORRUPTED; 3855 } 3856 3857 if (xfs_is_shutdown(mp)) 3858 return -EIO; 3859 3860 XFS_STATS_INC(mp, xs_blk_mapr); 3861 3862 error = xfs_iread_extents(NULL, ip, whichfork); 3863 if (error) 3864 return error; 3865 3866 if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) 3867 eof = true; 3868 end = bno + len; 3869 obno = bno; 3870 3871 while (bno < end && n < *nmap) { 3872 /* Reading past eof, act as though there's a hole up to end. */ 3873 if (eof) 3874 got.br_startoff = end; 3875 if (got.br_startoff > bno) { 3876 /* Reading in a hole. */ 3877 mval->br_startoff = bno; 3878 mval->br_startblock = HOLESTARTBLOCK; 3879 mval->br_blockcount = 3880 XFS_FILBLKS_MIN(len, got.br_startoff - bno); 3881 mval->br_state = XFS_EXT_NORM; 3882 bno += mval->br_blockcount; 3883 len -= mval->br_blockcount; 3884 mval++; 3885 n++; 3886 continue; 3887 } 3888 3889 /* set up the extent map to return. */ 3890 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); 3891 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); 3892 3893 /* If we're done, stop now. */ 3894 if (bno >= end || n >= *nmap) 3895 break; 3896 3897 /* Else go on to the next record. */ 3898 if (!xfs_iext_next_extent(ifp, &icur, &got)) 3899 eof = true; 3900 } 3901 *nmap = n; 3902 return 0; 3903 } 3904 3905 static int 3906 xfs_bmapi_allocate( 3907 struct xfs_bmalloca *bma) 3908 { 3909 struct xfs_mount *mp = bma->ip->i_mount; 3910 int whichfork = xfs_bmapi_whichfork(bma->flags); 3911 struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork); 3912 int error; 3913 3914 ASSERT(bma->length > 0); 3915 ASSERT(bma->length <= XFS_MAX_BMBT_EXTLEN); 3916 3917 if (bma->flags & XFS_BMAPI_CONTIG) 3918 bma->minlen = bma->length; 3919 else 3920 bma->minlen = 1; 3921 3922 if (!(bma->flags & XFS_BMAPI_METADATA)) { 3923 /* 3924 * For the data and COW fork, the first data in the file is 3925 * treated differently to all other allocations. For the 3926 * attribute fork, we only need to ensure the allocated range 3927 * is not on the busy list. 3928 */ 3929 bma->datatype = XFS_ALLOC_NOBUSY; 3930 if (whichfork == XFS_DATA_FORK || whichfork == XFS_COW_FORK) { 3931 bma->datatype |= XFS_ALLOC_USERDATA; 3932 if (bma->offset == 0) 3933 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA; 3934 3935 if (mp->m_dalign && bma->length >= mp->m_dalign) { 3936 error = xfs_bmap_isaeof(bma, whichfork); 3937 if (error) 3938 return error; 3939 } 3940 } 3941 } 3942 3943 if ((bma->datatype & XFS_ALLOC_USERDATA) && 3944 XFS_IS_REALTIME_INODE(bma->ip)) 3945 error = xfs_bmap_rtalloc(bma); 3946 else 3947 error = xfs_bmap_btalloc(bma); 3948 if (error) 3949 return error; 3950 if (bma->blkno == NULLFSBLOCK) 3951 return -ENOSPC; 3952 3953 if (WARN_ON_ONCE(!xfs_valid_startblock(bma->ip, bma->blkno))) { 3954 xfs_bmap_mark_sick(bma->ip, whichfork); 3955 return -EFSCORRUPTED; 3956 } 3957 3958 if (bma->flags & XFS_BMAPI_ZERO) { 3959 error = xfs_zero_extent(bma->ip, bma->blkno, bma->length); 3960 if (error) 3961 return error; 3962 } 3963 3964 if (ifp->if_format == XFS_DINODE_FMT_BTREE && !bma->cur) 3965 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork); 3966 /* 3967 * Bump the number of extents we've allocated 3968 * in this call. 3969 */ 3970 bma->nallocs++; 3971 3972 if (bma->cur && bma->wasdel) 3973 bma->cur->bc_flags |= XFS_BTREE_BMBT_WASDEL; 3974 3975 bma->got.br_startoff = bma->offset; 3976 bma->got.br_startblock = bma->blkno; 3977 bma->got.br_blockcount = bma->length; 3978 bma->got.br_state = XFS_EXT_NORM; 3979 3980 if (bma->flags & XFS_BMAPI_PREALLOC) 3981 bma->got.br_state = XFS_EXT_UNWRITTEN; 3982 3983 if (bma->wasdel) 3984 error = xfs_bmap_add_extent_delay_real(bma, whichfork); 3985 else 3986 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip, 3987 whichfork, &bma->icur, &bma->cur, &bma->got, 3988 &bma->logflags, bma->flags); 3989 if (error) 3990 return error; 3991 3992 /* 3993 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real 3994 * or xfs_bmap_add_extent_hole_real might have merged it into one of 3995 * the neighbouring ones. 3996 */ 3997 xfs_iext_get_extent(ifp, &bma->icur, &bma->got); 3998 3999 ASSERT(bma->got.br_startoff <= bma->offset); 4000 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >= 4001 bma->offset + bma->length); 4002 ASSERT(bma->got.br_state == XFS_EXT_NORM || 4003 bma->got.br_state == XFS_EXT_UNWRITTEN); 4004 return 0; 4005 } 4006 4007 STATIC int 4008 xfs_bmapi_convert_unwritten( 4009 struct xfs_bmalloca *bma, 4010 struct xfs_bmbt_irec *mval, 4011 xfs_filblks_t len, 4012 uint32_t flags) 4013 { 4014 int whichfork = xfs_bmapi_whichfork(flags); 4015 struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork); 4016 int tmp_logflags = 0; 4017 int error; 4018 4019 /* check if we need to do unwritten->real conversion */ 4020 if (mval->br_state == XFS_EXT_UNWRITTEN && 4021 (flags & XFS_BMAPI_PREALLOC)) 4022 return 0; 4023 4024 /* check if we need to do real->unwritten conversion */ 4025 if (mval->br_state == XFS_EXT_NORM && 4026 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) != 4027 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) 4028 return 0; 4029 4030 /* 4031 * Modify (by adding) the state flag, if writing. 4032 */ 4033 ASSERT(mval->br_blockcount <= len); 4034 if (ifp->if_format == XFS_DINODE_FMT_BTREE && !bma->cur) { 4035 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp, 4036 bma->ip, whichfork); 4037 } 4038 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN) 4039 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN; 4040 4041 /* 4042 * Before insertion into the bmbt, zero the range being converted 4043 * if required. 4044 */ 4045 if (flags & XFS_BMAPI_ZERO) { 4046 error = xfs_zero_extent(bma->ip, mval->br_startblock, 4047 mval->br_blockcount); 4048 if (error) 4049 return error; 4050 } 4051 4052 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork, 4053 &bma->icur, &bma->cur, mval, &tmp_logflags); 4054 /* 4055 * Log the inode core unconditionally in the unwritten extent conversion 4056 * path because the conversion might not have done so (e.g., if the 4057 * extent count hasn't changed). We need to make sure the inode is dirty 4058 * in the transaction for the sake of fsync(), even if nothing has 4059 * changed, because fsync() will not force the log for this transaction 4060 * unless it sees the inode pinned. 4061 * 4062 * Note: If we're only converting cow fork extents, there aren't 4063 * any on-disk updates to make, so we don't need to log anything. 4064 */ 4065 if (whichfork != XFS_COW_FORK) 4066 bma->logflags |= tmp_logflags | XFS_ILOG_CORE; 4067 if (error) 4068 return error; 4069 4070 /* 4071 * Update our extent pointer, given that 4072 * xfs_bmap_add_extent_unwritten_real might have merged it into one 4073 * of the neighbouring ones. 4074 */ 4075 xfs_iext_get_extent(ifp, &bma->icur, &bma->got); 4076 4077 /* 4078 * We may have combined previously unwritten space with written space, 4079 * so generate another request. 4080 */ 4081 if (mval->br_blockcount < len) 4082 return -EAGAIN; 4083 return 0; 4084 } 4085 4086 xfs_extlen_t 4087 xfs_bmapi_minleft( 4088 struct xfs_trans *tp, 4089 struct xfs_inode *ip, 4090 int fork) 4091 { 4092 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, fork); 4093 4094 if (tp && tp->t_highest_agno != NULLAGNUMBER) 4095 return 0; 4096 if (ifp->if_format != XFS_DINODE_FMT_BTREE) 4097 return 1; 4098 return be16_to_cpu(ifp->if_broot->bb_level) + 1; 4099 } 4100 4101 /* 4102 * Log whatever the flags say, even if error. Otherwise we might miss detecting 4103 * a case where the data is changed, there's an error, and it's not logged so we 4104 * don't shutdown when we should. Don't bother logging extents/btree changes if 4105 * we converted to the other format. 4106 */ 4107 static void 4108 xfs_bmapi_finish( 4109 struct xfs_bmalloca *bma, 4110 int whichfork, 4111 int error) 4112 { 4113 struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork); 4114 4115 if ((bma->logflags & xfs_ilog_fext(whichfork)) && 4116 ifp->if_format != XFS_DINODE_FMT_EXTENTS) 4117 bma->logflags &= ~xfs_ilog_fext(whichfork); 4118 else if ((bma->logflags & xfs_ilog_fbroot(whichfork)) && 4119 ifp->if_format != XFS_DINODE_FMT_BTREE) 4120 bma->logflags &= ~xfs_ilog_fbroot(whichfork); 4121 4122 if (bma->logflags) 4123 xfs_trans_log_inode(bma->tp, bma->ip, bma->logflags); 4124 if (bma->cur) 4125 xfs_btree_del_cursor(bma->cur, error); 4126 } 4127 4128 /* 4129 * Map file blocks to filesystem blocks, and allocate blocks or convert the 4130 * extent state if necessary. Details behaviour is controlled by the flags 4131 * parameter. Only allocates blocks from a single allocation group, to avoid 4132 * locking problems. 4133 * 4134 * Returns 0 on success and places the extent mappings in mval. nmaps is used 4135 * as an input/output parameter where the caller specifies the maximum number 4136 * of mappings that may be returned and xfs_bmapi_write passes back the number 4137 * of mappings (including existing mappings) it found. 4138 * 4139 * Returns a negative error code on failure, including -ENOSPC when it could not 4140 * allocate any blocks and -ENOSR when it did allocate blocks to convert a 4141 * delalloc range, but those blocks were before the passed in range. 4142 */ 4143 int 4144 xfs_bmapi_write( 4145 struct xfs_trans *tp, /* transaction pointer */ 4146 struct xfs_inode *ip, /* incore inode */ 4147 xfs_fileoff_t bno, /* starting file offs. mapped */ 4148 xfs_filblks_t len, /* length to map in file */ 4149 uint32_t flags, /* XFS_BMAPI_... */ 4150 xfs_extlen_t total, /* total blocks needed */ 4151 struct xfs_bmbt_irec *mval, /* output: map values */ 4152 int *nmap) /* i/o: mval size/count */ 4153 { 4154 struct xfs_bmalloca bma = { 4155 .tp = tp, 4156 .ip = ip, 4157 .total = total, 4158 }; 4159 struct xfs_mount *mp = ip->i_mount; 4160 int whichfork = xfs_bmapi_whichfork(flags); 4161 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); 4162 xfs_fileoff_t end; /* end of mapped file region */ 4163 bool eof = false; /* after the end of extents */ 4164 int error; /* error return */ 4165 int n; /* current extent index */ 4166 xfs_fileoff_t obno; /* old block number (offset) */ 4167 4168 #ifdef DEBUG 4169 xfs_fileoff_t orig_bno; /* original block number value */ 4170 int orig_flags; /* original flags arg value */ 4171 xfs_filblks_t orig_len; /* original value of len arg */ 4172 struct xfs_bmbt_irec *orig_mval; /* original value of mval */ 4173 int orig_nmap; /* original value of *nmap */ 4174 4175 orig_bno = bno; 4176 orig_len = len; 4177 orig_flags = flags; 4178 orig_mval = mval; 4179 orig_nmap = *nmap; 4180 #endif 4181 4182 ASSERT(*nmap >= 1); 4183 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); 4184 ASSERT(tp != NULL); 4185 ASSERT(len > 0); 4186 ASSERT(ifp->if_format != XFS_DINODE_FMT_LOCAL); 4187 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL); 4188 ASSERT(!(flags & XFS_BMAPI_REMAP)); 4189 4190 /* zeroing is for currently only for data extents, not metadata */ 4191 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) != 4192 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)); 4193 /* 4194 * we can allocate unwritten extents or pre-zero allocated blocks, 4195 * but it makes no sense to do both at once. This would result in 4196 * zeroing the unwritten extent twice, but it still being an 4197 * unwritten extent.... 4198 */ 4199 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) != 4200 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)); 4201 4202 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) || 4203 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) { 4204 xfs_bmap_mark_sick(ip, whichfork); 4205 return -EFSCORRUPTED; 4206 } 4207 4208 if (xfs_is_shutdown(mp)) 4209 return -EIO; 4210 4211 XFS_STATS_INC(mp, xs_blk_mapw); 4212 4213 error = xfs_iread_extents(tp, ip, whichfork); 4214 if (error) 4215 goto error0; 4216 4217 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got)) 4218 eof = true; 4219 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev)) 4220 bma.prev.br_startoff = NULLFILEOFF; 4221 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork); 4222 4223 n = 0; 4224 end = bno + len; 4225 obno = bno; 4226 while (bno < end && n < *nmap) { 4227 bool need_alloc = false, wasdelay = false; 4228 4229 /* in hole or beyond EOF? */ 4230 if (eof || bma.got.br_startoff > bno) { 4231 /* 4232 * CoW fork conversions should /never/ hit EOF or 4233 * holes. There should always be something for us 4234 * to work on. 4235 */ 4236 ASSERT(!((flags & XFS_BMAPI_CONVERT) && 4237 (flags & XFS_BMAPI_COWFORK))); 4238 4239 need_alloc = true; 4240 } else if (isnullstartblock(bma.got.br_startblock)) { 4241 wasdelay = true; 4242 } 4243 4244 /* 4245 * First, deal with the hole before the allocated space 4246 * that we found, if any. 4247 */ 4248 if (need_alloc || wasdelay) { 4249 bma.eof = eof; 4250 bma.conv = !!(flags & XFS_BMAPI_CONVERT); 4251 bma.wasdel = wasdelay; 4252 bma.offset = bno; 4253 bma.flags = flags; 4254 4255 /* 4256 * There's a 32/64 bit type mismatch between the 4257 * allocation length request (which can be 64 bits in 4258 * length) and the bma length request, which is 4259 * xfs_extlen_t and therefore 32 bits. Hence we have to 4260 * be careful and do the min() using the larger type to 4261 * avoid overflows. 4262 */ 4263 bma.length = XFS_FILBLKS_MIN(len, XFS_MAX_BMBT_EXTLEN); 4264 4265 if (wasdelay) { 4266 bma.length = XFS_FILBLKS_MIN(bma.length, 4267 bma.got.br_blockcount - 4268 (bno - bma.got.br_startoff)); 4269 } else { 4270 if (!eof) 4271 bma.length = XFS_FILBLKS_MIN(bma.length, 4272 bma.got.br_startoff - bno); 4273 } 4274 4275 ASSERT(bma.length > 0); 4276 error = xfs_bmapi_allocate(&bma); 4277 if (error) { 4278 /* 4279 * If we already allocated space in a previous 4280 * iteration return what we go so far when 4281 * running out of space. 4282 */ 4283 if (error == -ENOSPC && bma.nallocs) 4284 break; 4285 goto error0; 4286 } 4287 4288 /* 4289 * If this is a CoW allocation, record the data in 4290 * the refcount btree for orphan recovery. 4291 */ 4292 if (whichfork == XFS_COW_FORK) 4293 xfs_refcount_alloc_cow_extent(tp, 4294 XFS_IS_REALTIME_INODE(ip), 4295 bma.blkno, bma.length); 4296 } 4297 4298 /* Deal with the allocated space we found. */ 4299 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno, 4300 end, n, flags); 4301 4302 /* Execute unwritten extent conversion if necessary */ 4303 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags); 4304 if (error == -EAGAIN) 4305 continue; 4306 if (error) 4307 goto error0; 4308 4309 /* update the extent map to return */ 4310 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); 4311 4312 /* 4313 * If we're done, stop now. Stop when we've allocated 4314 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise 4315 * the transaction may get too big. 4316 */ 4317 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap) 4318 break; 4319 4320 /* Else go on to the next record. */ 4321 bma.prev = bma.got; 4322 if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got)) 4323 eof = true; 4324 } 4325 4326 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags, 4327 whichfork); 4328 if (error) 4329 goto error0; 4330 4331 ASSERT(ifp->if_format != XFS_DINODE_FMT_BTREE || 4332 ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork)); 4333 xfs_bmapi_finish(&bma, whichfork, 0); 4334 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval, 4335 orig_nmap, n); 4336 4337 /* 4338 * When converting delayed allocations, xfs_bmapi_allocate ignores 4339 * the passed in bno and always converts from the start of the found 4340 * delalloc extent. 4341 * 4342 * To avoid a successful return with *nmap set to 0, return the magic 4343 * -ENOSR error code for this particular case so that the caller can 4344 * handle it. 4345 */ 4346 if (!n) { 4347 ASSERT(bma.nallocs >= *nmap); 4348 return -ENOSR; 4349 } 4350 *nmap = n; 4351 return 0; 4352 error0: 4353 xfs_bmapi_finish(&bma, whichfork, error); 4354 return error; 4355 } 4356 4357 /* 4358 * Convert an existing delalloc extent to real blocks based on file offset. This 4359 * attempts to allocate the entire delalloc extent and may require multiple 4360 * invocations to allocate the target offset if a large enough physical extent 4361 * is not available. 4362 */ 4363 static int 4364 xfs_bmapi_convert_one_delalloc( 4365 struct xfs_inode *ip, 4366 int whichfork, 4367 xfs_off_t offset, 4368 struct iomap *iomap, 4369 unsigned int *seq) 4370 { 4371 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); 4372 struct xfs_mount *mp = ip->i_mount; 4373 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset); 4374 struct xfs_bmalloca bma = { NULL }; 4375 uint16_t flags = 0; 4376 struct xfs_trans *tp; 4377 int error; 4378 4379 if (whichfork == XFS_COW_FORK) 4380 flags |= IOMAP_F_SHARED; 4381 4382 /* 4383 * Space for the extent and indirect blocks was reserved when the 4384 * delalloc extent was created so there's no need to do so here. 4385 */ 4386 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 4387 XFS_TRANS_RESERVE, &tp); 4388 if (error) 4389 return error; 4390 4391 xfs_ilock(ip, XFS_ILOCK_EXCL); 4392 xfs_trans_ijoin(tp, ip, 0); 4393 4394 error = xfs_iext_count_extend(tp, ip, whichfork, 4395 XFS_IEXT_ADD_NOSPLIT_CNT); 4396 if (error) 4397 goto out_trans_cancel; 4398 4399 if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &bma.icur, &bma.got) || 4400 bma.got.br_startoff > offset_fsb) { 4401 /* 4402 * No extent found in the range we are trying to convert. This 4403 * should only happen for the COW fork, where another thread 4404 * might have moved the extent to the data fork in the meantime. 4405 */ 4406 WARN_ON_ONCE(whichfork != XFS_COW_FORK); 4407 error = -EAGAIN; 4408 goto out_trans_cancel; 4409 } 4410 4411 /* 4412 * If we find a real extent here we raced with another thread converting 4413 * the extent. Just return the real extent at this offset. 4414 */ 4415 if (!isnullstartblock(bma.got.br_startblock)) { 4416 xfs_bmbt_to_iomap(ip, iomap, &bma.got, 0, flags, 4417 xfs_iomap_inode_sequence(ip, flags)); 4418 if (seq) 4419 *seq = READ_ONCE(ifp->if_seq); 4420 goto out_trans_cancel; 4421 } 4422 4423 bma.tp = tp; 4424 bma.ip = ip; 4425 bma.wasdel = true; 4426 bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork); 4427 4428 /* 4429 * Always allocate convert from the start of the delalloc extent even if 4430 * that is outside the passed in range to create large contiguous 4431 * extents on disk. 4432 */ 4433 bma.offset = bma.got.br_startoff; 4434 bma.length = bma.got.br_blockcount; 4435 4436 /* 4437 * When we're converting the delalloc reservations backing dirty pages 4438 * in the page cache, we must be careful about how we create the new 4439 * extents: 4440 * 4441 * New CoW fork extents are created unwritten, turned into real extents 4442 * when we're about to write the data to disk, and mapped into the data 4443 * fork after the write finishes. End of story. 4444 * 4445 * New data fork extents must be mapped in as unwritten and converted 4446 * to real extents after the write succeeds to avoid exposing stale 4447 * disk contents if we crash. 4448 */ 4449 bma.flags = XFS_BMAPI_PREALLOC; 4450 if (whichfork == XFS_COW_FORK) 4451 bma.flags |= XFS_BMAPI_COWFORK; 4452 4453 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev)) 4454 bma.prev.br_startoff = NULLFILEOFF; 4455 4456 error = xfs_bmapi_allocate(&bma); 4457 if (error) 4458 goto out_finish; 4459 4460 XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, bma.length)); 4461 XFS_STATS_INC(mp, xs_xstrat_quick); 4462 4463 ASSERT(!isnullstartblock(bma.got.br_startblock)); 4464 xfs_bmbt_to_iomap(ip, iomap, &bma.got, 0, flags, 4465 xfs_iomap_inode_sequence(ip, flags)); 4466 if (seq) 4467 *seq = READ_ONCE(ifp->if_seq); 4468 4469 if (whichfork == XFS_COW_FORK) 4470 xfs_refcount_alloc_cow_extent(tp, XFS_IS_REALTIME_INODE(ip), 4471 bma.blkno, bma.length); 4472 4473 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags, 4474 whichfork); 4475 if (error) 4476 goto out_finish; 4477 4478 xfs_bmapi_finish(&bma, whichfork, 0); 4479 error = xfs_trans_commit(tp); 4480 xfs_iunlock(ip, XFS_ILOCK_EXCL); 4481 return error; 4482 4483 out_finish: 4484 xfs_bmapi_finish(&bma, whichfork, error); 4485 out_trans_cancel: 4486 xfs_trans_cancel(tp); 4487 xfs_iunlock(ip, XFS_ILOCK_EXCL); 4488 return error; 4489 } 4490 4491 /* 4492 * Pass in a dellalloc extent and convert it to real extents, return the real 4493 * extent that maps offset_fsb in iomap. 4494 */ 4495 int 4496 xfs_bmapi_convert_delalloc( 4497 struct xfs_inode *ip, 4498 int whichfork, 4499 loff_t offset, 4500 struct iomap *iomap, 4501 unsigned int *seq) 4502 { 4503 int error; 4504 4505 /* 4506 * Attempt to allocate whatever delalloc extent currently backs offset 4507 * and put the result into iomap. Allocate in a loop because it may 4508 * take several attempts to allocate real blocks for a contiguous 4509 * delalloc extent if free space is sufficiently fragmented. 4510 */ 4511 do { 4512 error = xfs_bmapi_convert_one_delalloc(ip, whichfork, offset, 4513 iomap, seq); 4514 if (error) 4515 return error; 4516 } while (iomap->offset + iomap->length <= offset); 4517 4518 return 0; 4519 } 4520 4521 int 4522 xfs_bmapi_remap( 4523 struct xfs_trans *tp, 4524 struct xfs_inode *ip, 4525 xfs_fileoff_t bno, 4526 xfs_filblks_t len, 4527 xfs_fsblock_t startblock, 4528 uint32_t flags) 4529 { 4530 struct xfs_mount *mp = ip->i_mount; 4531 struct xfs_ifork *ifp; 4532 struct xfs_btree_cur *cur = NULL; 4533 struct xfs_bmbt_irec got; 4534 struct xfs_iext_cursor icur; 4535 int whichfork = xfs_bmapi_whichfork(flags); 4536 int logflags = 0, error; 4537 4538 ifp = xfs_ifork_ptr(ip, whichfork); 4539 ASSERT(len > 0); 4540 ASSERT(len <= (xfs_filblks_t)XFS_MAX_BMBT_EXTLEN); 4541 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL); 4542 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC | 4543 XFS_BMAPI_NORMAP))); 4544 ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) != 4545 (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)); 4546 4547 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) || 4548 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) { 4549 xfs_bmap_mark_sick(ip, whichfork); 4550 return -EFSCORRUPTED; 4551 } 4552 4553 if (xfs_is_shutdown(mp)) 4554 return -EIO; 4555 4556 error = xfs_iread_extents(tp, ip, whichfork); 4557 if (error) 4558 return error; 4559 4560 if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) { 4561 /* make sure we only reflink into a hole. */ 4562 ASSERT(got.br_startoff > bno); 4563 ASSERT(got.br_startoff - bno >= len); 4564 } 4565 4566 ip->i_nblocks += len; 4567 ip->i_delayed_blks -= len; /* see xfs_bmap_defer_add */ 4568 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 4569 4570 if (ifp->if_format == XFS_DINODE_FMT_BTREE) 4571 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); 4572 4573 got.br_startoff = bno; 4574 got.br_startblock = startblock; 4575 got.br_blockcount = len; 4576 if (flags & XFS_BMAPI_PREALLOC) 4577 got.br_state = XFS_EXT_UNWRITTEN; 4578 else 4579 got.br_state = XFS_EXT_NORM; 4580 4581 error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur, 4582 &cur, &got, &logflags, flags); 4583 if (error) 4584 goto error0; 4585 4586 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, whichfork); 4587 4588 error0: 4589 if (ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS) 4590 logflags &= ~XFS_ILOG_DEXT; 4591 else if (ip->i_df.if_format != XFS_DINODE_FMT_BTREE) 4592 logflags &= ~XFS_ILOG_DBROOT; 4593 4594 if (logflags) 4595 xfs_trans_log_inode(tp, ip, logflags); 4596 if (cur) 4597 xfs_btree_del_cursor(cur, error); 4598 return error; 4599 } 4600 4601 /* 4602 * When a delalloc extent is split (e.g., due to a hole punch), the original 4603 * indlen reservation must be shared across the two new extents that are left 4604 * behind. 4605 * 4606 * Given the original reservation and the worst case indlen for the two new 4607 * extents (as calculated by xfs_bmap_worst_indlen()), split the original 4608 * reservation fairly across the two new extents. If necessary, steal available 4609 * blocks from a deleted extent to make up a reservation deficiency (e.g., if 4610 * ores == 1). The number of stolen blocks is returned. The availability and 4611 * subsequent accounting of stolen blocks is the responsibility of the caller. 4612 */ 4613 static void 4614 xfs_bmap_split_indlen( 4615 xfs_filblks_t ores, /* original res. */ 4616 xfs_filblks_t *indlen1, /* ext1 worst indlen */ 4617 xfs_filblks_t *indlen2) /* ext2 worst indlen */ 4618 { 4619 xfs_filblks_t len1 = *indlen1; 4620 xfs_filblks_t len2 = *indlen2; 4621 xfs_filblks_t nres = len1 + len2; /* new total res. */ 4622 xfs_filblks_t resfactor; 4623 4624 /* 4625 * We can't meet the total required reservation for the two extents. 4626 * Calculate the percent of the overall shortage between both extents 4627 * and apply this percentage to each of the requested indlen values. 4628 * This distributes the shortage fairly and reduces the chances that one 4629 * of the two extents is left with nothing when extents are repeatedly 4630 * split. 4631 */ 4632 resfactor = (ores * 100); 4633 do_div(resfactor, nres); 4634 len1 *= resfactor; 4635 do_div(len1, 100); 4636 len2 *= resfactor; 4637 do_div(len2, 100); 4638 ASSERT(len1 + len2 <= ores); 4639 ASSERT(len1 < *indlen1 && len2 < *indlen2); 4640 4641 /* 4642 * Hand out the remainder to each extent. If one of the two reservations 4643 * is zero, we want to make sure that one gets a block first. The loop 4644 * below starts with len1, so hand len2 a block right off the bat if it 4645 * is zero. 4646 */ 4647 ores -= (len1 + len2); 4648 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores); 4649 if (ores && !len2 && *indlen2) { 4650 len2++; 4651 ores--; 4652 } 4653 while (ores) { 4654 if (len1 < *indlen1) { 4655 len1++; 4656 ores--; 4657 } 4658 if (!ores) 4659 break; 4660 if (len2 < *indlen2) { 4661 len2++; 4662 ores--; 4663 } 4664 } 4665 4666 *indlen1 = len1; 4667 *indlen2 = len2; 4668 } 4669 4670 void 4671 xfs_bmap_del_extent_delay( 4672 struct xfs_inode *ip, 4673 int whichfork, 4674 struct xfs_iext_cursor *icur, 4675 struct xfs_bmbt_irec *got, 4676 struct xfs_bmbt_irec *del, 4677 uint32_t bflags) /* bmapi flags */ 4678 { 4679 struct xfs_mount *mp = ip->i_mount; 4680 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); 4681 struct xfs_bmbt_irec new; 4682 int64_t da_old, da_new, da_diff = 0; 4683 xfs_fileoff_t del_endoff, got_endoff; 4684 xfs_filblks_t got_indlen, new_indlen, stolen = 0; 4685 uint32_t state = xfs_bmap_fork_to_state(whichfork); 4686 uint64_t fdblocks; 4687 bool isrt; 4688 4689 XFS_STATS_INC(mp, xs_del_exlist); 4690 4691 isrt = xfs_ifork_is_realtime(ip, whichfork); 4692 del_endoff = del->br_startoff + del->br_blockcount; 4693 got_endoff = got->br_startoff + got->br_blockcount; 4694 da_old = startblockval(got->br_startblock); 4695 da_new = 0; 4696 4697 ASSERT(del->br_blockcount > 0); 4698 ASSERT(got->br_startoff <= del->br_startoff); 4699 ASSERT(got_endoff >= del_endoff); 4700 4701 /* 4702 * Update the inode delalloc counter now and wait to update the 4703 * sb counters as we might have to borrow some blocks for the 4704 * indirect block accounting. 4705 */ 4706 xfs_quota_unreserve_blkres(ip, del->br_blockcount); 4707 ip->i_delayed_blks -= del->br_blockcount; 4708 4709 if (got->br_startoff == del->br_startoff) 4710 state |= BMAP_LEFT_FILLING; 4711 if (got_endoff == del_endoff) 4712 state |= BMAP_RIGHT_FILLING; 4713 4714 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { 4715 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: 4716 /* 4717 * Matches the whole extent. Delete the entry. 4718 */ 4719 xfs_iext_remove(ip, icur, state); 4720 xfs_iext_prev(ifp, icur); 4721 break; 4722 case BMAP_LEFT_FILLING: 4723 /* 4724 * Deleting the first part of the extent. 4725 */ 4726 got->br_startoff = del_endoff; 4727 got->br_blockcount -= del->br_blockcount; 4728 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, 4729 got->br_blockcount), da_old); 4730 got->br_startblock = nullstartblock((int)da_new); 4731 xfs_iext_update_extent(ip, state, icur, got); 4732 break; 4733 case BMAP_RIGHT_FILLING: 4734 /* 4735 * Deleting the last part of the extent. 4736 */ 4737 got->br_blockcount = got->br_blockcount - del->br_blockcount; 4738 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, 4739 got->br_blockcount), da_old); 4740 got->br_startblock = nullstartblock((int)da_new); 4741 xfs_iext_update_extent(ip, state, icur, got); 4742 break; 4743 case 0: 4744 /* 4745 * Deleting the middle of the extent. 4746 * 4747 * Distribute the original indlen reservation across the two new 4748 * extents. Steal blocks from the deleted extent if necessary. 4749 * Stealing blocks simply fudges the fdblocks accounting below. 4750 * Warn if either of the new indlen reservations is zero as this 4751 * can lead to delalloc problems. 4752 */ 4753 got->br_blockcount = del->br_startoff - got->br_startoff; 4754 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount); 4755 4756 new.br_blockcount = got_endoff - del_endoff; 4757 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount); 4758 4759 WARN_ON_ONCE(!got_indlen || !new_indlen); 4760 /* 4761 * Steal as many blocks as we can to try and satisfy the worst 4762 * case indlen for both new extents. 4763 * 4764 * However, we can't just steal reservations from the data 4765 * blocks if this is an RT inodes as the data and metadata 4766 * blocks come from different pools. We'll have to live with 4767 * under-filled indirect reservation in this case. 4768 */ 4769 da_new = got_indlen + new_indlen; 4770 if (da_new > da_old && !isrt) { 4771 stolen = XFS_FILBLKS_MIN(da_new - da_old, 4772 del->br_blockcount); 4773 da_old += stolen; 4774 } 4775 if (da_new > da_old) 4776 xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen); 4777 da_new = got_indlen + new_indlen; 4778 4779 got->br_startblock = nullstartblock((int)got_indlen); 4780 4781 new.br_startoff = del_endoff; 4782 new.br_state = got->br_state; 4783 new.br_startblock = nullstartblock((int)new_indlen); 4784 4785 xfs_iext_update_extent(ip, state, icur, got); 4786 xfs_iext_next(ifp, icur); 4787 xfs_iext_insert(ip, icur, &new, state); 4788 4789 del->br_blockcount -= stolen; 4790 break; 4791 } 4792 4793 ASSERT(da_old >= da_new); 4794 da_diff = da_old - da_new; 4795 fdblocks = da_diff; 4796 4797 if (bflags & XFS_BMAPI_REMAP) { 4798 ; 4799 } else if (isrt) { 4800 xfs_rtbxlen_t rtxlen; 4801 4802 rtxlen = xfs_blen_to_rtbxlen(mp, del->br_blockcount); 4803 if (xfs_is_zoned_inode(ip)) 4804 xfs_zoned_add_available(mp, rtxlen); 4805 xfs_add_frextents(mp, rtxlen); 4806 } else { 4807 fdblocks += del->br_blockcount; 4808 } 4809 4810 xfs_add_fdblocks(mp, fdblocks); 4811 xfs_mod_delalloc(ip, -(int64_t)del->br_blockcount, -da_diff); 4812 } 4813 4814 void 4815 xfs_bmap_del_extent_cow( 4816 struct xfs_inode *ip, 4817 struct xfs_iext_cursor *icur, 4818 struct xfs_bmbt_irec *got, 4819 struct xfs_bmbt_irec *del) 4820 { 4821 struct xfs_mount *mp = ip->i_mount; 4822 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_COW_FORK); 4823 struct xfs_bmbt_irec new; 4824 xfs_fileoff_t del_endoff, got_endoff; 4825 uint32_t state = BMAP_COWFORK; 4826 4827 XFS_STATS_INC(mp, xs_del_exlist); 4828 4829 del_endoff = del->br_startoff + del->br_blockcount; 4830 got_endoff = got->br_startoff + got->br_blockcount; 4831 4832 ASSERT(del->br_blockcount > 0); 4833 ASSERT(got->br_startoff <= del->br_startoff); 4834 ASSERT(got_endoff >= del_endoff); 4835 ASSERT(!isnullstartblock(got->br_startblock)); 4836 4837 if (got->br_startoff == del->br_startoff) 4838 state |= BMAP_LEFT_FILLING; 4839 if (got_endoff == del_endoff) 4840 state |= BMAP_RIGHT_FILLING; 4841 4842 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { 4843 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: 4844 /* 4845 * Matches the whole extent. Delete the entry. 4846 */ 4847 xfs_iext_remove(ip, icur, state); 4848 xfs_iext_prev(ifp, icur); 4849 break; 4850 case BMAP_LEFT_FILLING: 4851 /* 4852 * Deleting the first part of the extent. 4853 */ 4854 got->br_startoff = del_endoff; 4855 got->br_blockcount -= del->br_blockcount; 4856 got->br_startblock = del->br_startblock + del->br_blockcount; 4857 xfs_iext_update_extent(ip, state, icur, got); 4858 break; 4859 case BMAP_RIGHT_FILLING: 4860 /* 4861 * Deleting the last part of the extent. 4862 */ 4863 got->br_blockcount -= del->br_blockcount; 4864 xfs_iext_update_extent(ip, state, icur, got); 4865 break; 4866 case 0: 4867 /* 4868 * Deleting the middle of the extent. 4869 */ 4870 got->br_blockcount = del->br_startoff - got->br_startoff; 4871 4872 new.br_startoff = del_endoff; 4873 new.br_blockcount = got_endoff - del_endoff; 4874 new.br_state = got->br_state; 4875 new.br_startblock = del->br_startblock + del->br_blockcount; 4876 4877 xfs_iext_update_extent(ip, state, icur, got); 4878 xfs_iext_next(ifp, icur); 4879 xfs_iext_insert(ip, icur, &new, state); 4880 break; 4881 } 4882 ip->i_delayed_blks -= del->br_blockcount; 4883 } 4884 4885 static int 4886 xfs_bmap_free_rtblocks( 4887 struct xfs_trans *tp, 4888 struct xfs_bmbt_irec *del) 4889 { 4890 struct xfs_rtgroup *rtg; 4891 int error; 4892 4893 rtg = xfs_rtgroup_grab(tp->t_mountp, 0); 4894 if (!rtg) 4895 return -EIO; 4896 4897 /* 4898 * Ensure the bitmap and summary inodes are locked and joined to the 4899 * transaction before modifying them. 4900 */ 4901 if (!(tp->t_flags & XFS_TRANS_RTBITMAP_LOCKED)) { 4902 tp->t_flags |= XFS_TRANS_RTBITMAP_LOCKED; 4903 xfs_rtgroup_lock(rtg, XFS_RTGLOCK_BITMAP); 4904 xfs_rtgroup_trans_join(tp, rtg, XFS_RTGLOCK_BITMAP); 4905 } 4906 4907 error = xfs_rtfree_blocks(tp, rtg, del->br_startblock, 4908 del->br_blockcount); 4909 xfs_rtgroup_rele(rtg); 4910 return error; 4911 } 4912 4913 /* 4914 * Called by xfs_bmapi to update file extent records and the btree 4915 * after removing space. 4916 */ 4917 STATIC int /* error */ 4918 xfs_bmap_del_extent_real( 4919 xfs_inode_t *ip, /* incore inode pointer */ 4920 xfs_trans_t *tp, /* current transaction pointer */ 4921 struct xfs_iext_cursor *icur, 4922 struct xfs_btree_cur *cur, /* if null, not a btree */ 4923 xfs_bmbt_irec_t *del, /* data to remove from extents */ 4924 int *logflagsp, /* inode logging flags */ 4925 int whichfork, /* data or attr fork */ 4926 uint32_t bflags) /* bmapi flags */ 4927 { 4928 xfs_fsblock_t del_endblock=0; /* first block past del */ 4929 xfs_fileoff_t del_endoff; /* first offset past del */ 4930 int error = 0; /* error return value */ 4931 struct xfs_bmbt_irec got; /* current extent entry */ 4932 xfs_fileoff_t got_endoff; /* first offset past got */ 4933 int i; /* temp state */ 4934 struct xfs_ifork *ifp; /* inode fork pointer */ 4935 xfs_mount_t *mp; /* mount structure */ 4936 xfs_filblks_t nblks; /* quota/sb block count */ 4937 xfs_bmbt_irec_t new; /* new record to be inserted */ 4938 /* REFERENCED */ 4939 uint qfield; /* quota field to update */ 4940 uint32_t state = xfs_bmap_fork_to_state(whichfork); 4941 struct xfs_bmbt_irec old; 4942 4943 *logflagsp = 0; 4944 4945 mp = ip->i_mount; 4946 XFS_STATS_INC(mp, xs_del_exlist); 4947 4948 ifp = xfs_ifork_ptr(ip, whichfork); 4949 ASSERT(del->br_blockcount > 0); 4950 xfs_iext_get_extent(ifp, icur, &got); 4951 ASSERT(got.br_startoff <= del->br_startoff); 4952 del_endoff = del->br_startoff + del->br_blockcount; 4953 got_endoff = got.br_startoff + got.br_blockcount; 4954 ASSERT(got_endoff >= del_endoff); 4955 ASSERT(!isnullstartblock(got.br_startblock)); 4956 qfield = 0; 4957 4958 /* 4959 * If it's the case where the directory code is running with no block 4960 * reservation, and the deleted block is in the middle of its extent, 4961 * and the resulting insert of an extent would cause transformation to 4962 * btree format, then reject it. The calling code will then swap blocks 4963 * around instead. We have to do this now, rather than waiting for the 4964 * conversion to btree format, since the transaction will be dirty then. 4965 */ 4966 if (tp->t_blk_res == 0 && 4967 ifp->if_format == XFS_DINODE_FMT_EXTENTS && 4968 ifp->if_nextents >= XFS_IFORK_MAXEXT(ip, whichfork) && 4969 del->br_startoff > got.br_startoff && del_endoff < got_endoff) 4970 return -ENOSPC; 4971 4972 *logflagsp = XFS_ILOG_CORE; 4973 if (xfs_ifork_is_realtime(ip, whichfork)) 4974 qfield = XFS_TRANS_DQ_RTBCOUNT; 4975 else 4976 qfield = XFS_TRANS_DQ_BCOUNT; 4977 nblks = del->br_blockcount; 4978 4979 del_endblock = del->br_startblock + del->br_blockcount; 4980 if (cur) { 4981 error = xfs_bmbt_lookup_eq(cur, &got, &i); 4982 if (error) 4983 return error; 4984 if (XFS_IS_CORRUPT(mp, i != 1)) { 4985 xfs_btree_mark_sick(cur); 4986 return -EFSCORRUPTED; 4987 } 4988 } 4989 4990 if (got.br_startoff == del->br_startoff) 4991 state |= BMAP_LEFT_FILLING; 4992 if (got_endoff == del_endoff) 4993 state |= BMAP_RIGHT_FILLING; 4994 4995 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { 4996 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: 4997 /* 4998 * Matches the whole extent. Delete the entry. 4999 */ 5000 xfs_iext_remove(ip, icur, state); 5001 xfs_iext_prev(ifp, icur); 5002 ifp->if_nextents--; 5003 5004 *logflagsp |= XFS_ILOG_CORE; 5005 if (!cur) { 5006 *logflagsp |= xfs_ilog_fext(whichfork); 5007 break; 5008 } 5009 if ((error = xfs_btree_delete(cur, &i))) 5010 return error; 5011 if (XFS_IS_CORRUPT(mp, i != 1)) { 5012 xfs_btree_mark_sick(cur); 5013 return -EFSCORRUPTED; 5014 } 5015 break; 5016 case BMAP_LEFT_FILLING: 5017 /* 5018 * Deleting the first part of the extent. 5019 */ 5020 got.br_startoff = del_endoff; 5021 got.br_startblock = del_endblock; 5022 got.br_blockcount -= del->br_blockcount; 5023 xfs_iext_update_extent(ip, state, icur, &got); 5024 if (!cur) { 5025 *logflagsp |= xfs_ilog_fext(whichfork); 5026 break; 5027 } 5028 error = xfs_bmbt_update(cur, &got); 5029 if (error) 5030 return error; 5031 break; 5032 case BMAP_RIGHT_FILLING: 5033 /* 5034 * Deleting the last part of the extent. 5035 */ 5036 got.br_blockcount -= del->br_blockcount; 5037 xfs_iext_update_extent(ip, state, icur, &got); 5038 if (!cur) { 5039 *logflagsp |= xfs_ilog_fext(whichfork); 5040 break; 5041 } 5042 error = xfs_bmbt_update(cur, &got); 5043 if (error) 5044 return error; 5045 break; 5046 case 0: 5047 /* 5048 * Deleting the middle of the extent. 5049 */ 5050 5051 old = got; 5052 5053 got.br_blockcount = del->br_startoff - got.br_startoff; 5054 xfs_iext_update_extent(ip, state, icur, &got); 5055 5056 new.br_startoff = del_endoff; 5057 new.br_blockcount = got_endoff - del_endoff; 5058 new.br_state = got.br_state; 5059 new.br_startblock = del_endblock; 5060 5061 *logflagsp |= XFS_ILOG_CORE; 5062 if (cur) { 5063 error = xfs_bmbt_update(cur, &got); 5064 if (error) 5065 return error; 5066 error = xfs_btree_increment(cur, 0, &i); 5067 if (error) 5068 return error; 5069 cur->bc_rec.b = new; 5070 error = xfs_btree_insert(cur, &i); 5071 if (error && error != -ENOSPC) 5072 return error; 5073 /* 5074 * If get no-space back from btree insert, it tried a 5075 * split, and we have a zero block reservation. Fix up 5076 * our state and return the error. 5077 */ 5078 if (error == -ENOSPC) { 5079 /* 5080 * Reset the cursor, don't trust it after any 5081 * insert operation. 5082 */ 5083 error = xfs_bmbt_lookup_eq(cur, &got, &i); 5084 if (error) 5085 return error; 5086 if (XFS_IS_CORRUPT(mp, i != 1)) { 5087 xfs_btree_mark_sick(cur); 5088 return -EFSCORRUPTED; 5089 } 5090 /* 5091 * Update the btree record back 5092 * to the original value. 5093 */ 5094 error = xfs_bmbt_update(cur, &old); 5095 if (error) 5096 return error; 5097 /* 5098 * Reset the extent record back 5099 * to the original value. 5100 */ 5101 xfs_iext_update_extent(ip, state, icur, &old); 5102 *logflagsp = 0; 5103 return -ENOSPC; 5104 } 5105 if (XFS_IS_CORRUPT(mp, i != 1)) { 5106 xfs_btree_mark_sick(cur); 5107 return -EFSCORRUPTED; 5108 } 5109 } else 5110 *logflagsp |= xfs_ilog_fext(whichfork); 5111 5112 ifp->if_nextents++; 5113 xfs_iext_next(ifp, icur); 5114 xfs_iext_insert(ip, icur, &new, state); 5115 break; 5116 } 5117 5118 /* remove reverse mapping */ 5119 xfs_rmap_unmap_extent(tp, ip, whichfork, del); 5120 5121 /* 5122 * If we need to, add to list of extents to delete. 5123 */ 5124 if (!(bflags & XFS_BMAPI_REMAP)) { 5125 bool isrt = xfs_ifork_is_realtime(ip, whichfork); 5126 5127 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) { 5128 xfs_refcount_decrease_extent(tp, isrt, del); 5129 } else if (isrt && !xfs_has_rtgroups(mp)) { 5130 error = xfs_bmap_free_rtblocks(tp, del); 5131 } else { 5132 unsigned int efi_flags = 0; 5133 5134 if ((bflags & XFS_BMAPI_NODISCARD) || 5135 del->br_state == XFS_EXT_UNWRITTEN) 5136 efi_flags |= XFS_FREE_EXTENT_SKIP_DISCARD; 5137 5138 /* 5139 * Historically, we did not use EFIs to free realtime 5140 * extents. However, when reverse mapping is enabled, 5141 * we must maintain the same order of operations as the 5142 * data device, which is: Remove the file mapping, 5143 * remove the reverse mapping, and then free the 5144 * blocks. Reflink for realtime volumes requires the 5145 * same sort of ordering. Both features rely on 5146 * rtgroups, so let's gate rt EFI usage on rtgroups. 5147 */ 5148 if (isrt) 5149 efi_flags |= XFS_FREE_EXTENT_REALTIME; 5150 5151 error = xfs_free_extent_later(tp, del->br_startblock, 5152 del->br_blockcount, NULL, 5153 XFS_AG_RESV_NONE, efi_flags); 5154 } 5155 if (error) 5156 return error; 5157 } 5158 5159 /* 5160 * Adjust inode # blocks in the file. 5161 */ 5162 if (nblks) 5163 ip->i_nblocks -= nblks; 5164 /* 5165 * Adjust quota data. 5166 */ 5167 if (qfield && !(bflags & XFS_BMAPI_REMAP)) 5168 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks); 5169 5170 return 0; 5171 } 5172 5173 /* 5174 * Unmap (remove) blocks from a file. 5175 * If nexts is nonzero then the number of extents to remove is limited to 5176 * that value. If not all extents in the block range can be removed then 5177 * *done is set. 5178 */ 5179 static int 5180 __xfs_bunmapi( 5181 struct xfs_trans *tp, /* transaction pointer */ 5182 struct xfs_inode *ip, /* incore inode */ 5183 xfs_fileoff_t start, /* first file offset deleted */ 5184 xfs_filblks_t *rlen, /* i/o: amount remaining */ 5185 uint32_t flags, /* misc flags */ 5186 xfs_extnum_t nexts) /* number of extents max */ 5187 { 5188 struct xfs_btree_cur *cur; /* bmap btree cursor */ 5189 struct xfs_bmbt_irec del; /* extent being deleted */ 5190 int error; /* error return value */ 5191 xfs_extnum_t extno; /* extent number in list */ 5192 struct xfs_bmbt_irec got; /* current extent record */ 5193 struct xfs_ifork *ifp; /* inode fork pointer */ 5194 int isrt; /* freeing in rt area */ 5195 int logflags; /* transaction logging flags */ 5196 xfs_extlen_t mod; /* rt extent offset */ 5197 struct xfs_mount *mp = ip->i_mount; 5198 int tmp_logflags; /* partial logging flags */ 5199 int wasdel; /* was a delayed alloc extent */ 5200 int whichfork; /* data or attribute fork */ 5201 xfs_filblks_t len = *rlen; /* length to unmap in file */ 5202 xfs_fileoff_t end; 5203 struct xfs_iext_cursor icur; 5204 bool done = false; 5205 5206 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_); 5207 5208 whichfork = xfs_bmapi_whichfork(flags); 5209 ASSERT(whichfork != XFS_COW_FORK); 5210 ifp = xfs_ifork_ptr(ip, whichfork); 5211 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp))) { 5212 xfs_bmap_mark_sick(ip, whichfork); 5213 return -EFSCORRUPTED; 5214 } 5215 if (xfs_is_shutdown(mp)) 5216 return -EIO; 5217 5218 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL); 5219 ASSERT(len > 0); 5220 ASSERT(nexts >= 0); 5221 5222 error = xfs_iread_extents(tp, ip, whichfork); 5223 if (error) 5224 return error; 5225 5226 if (xfs_iext_count(ifp) == 0) { 5227 *rlen = 0; 5228 return 0; 5229 } 5230 XFS_STATS_INC(mp, xs_blk_unmap); 5231 isrt = xfs_ifork_is_realtime(ip, whichfork); 5232 end = start + len; 5233 5234 if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) { 5235 *rlen = 0; 5236 return 0; 5237 } 5238 end--; 5239 5240 logflags = 0; 5241 if (ifp->if_format == XFS_DINODE_FMT_BTREE) { 5242 ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE); 5243 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); 5244 } else 5245 cur = NULL; 5246 5247 extno = 0; 5248 while (end != (xfs_fileoff_t)-1 && end >= start && 5249 (nexts == 0 || extno < nexts)) { 5250 /* 5251 * Is the found extent after a hole in which end lives? 5252 * Just back up to the previous extent, if so. 5253 */ 5254 if (got.br_startoff > end && 5255 !xfs_iext_prev_extent(ifp, &icur, &got)) { 5256 done = true; 5257 break; 5258 } 5259 /* 5260 * Is the last block of this extent before the range 5261 * we're supposed to delete? If so, we're done. 5262 */ 5263 end = XFS_FILEOFF_MIN(end, 5264 got.br_startoff + got.br_blockcount - 1); 5265 if (end < start) 5266 break; 5267 /* 5268 * Then deal with the (possibly delayed) allocated space 5269 * we found. 5270 */ 5271 del = got; 5272 wasdel = isnullstartblock(del.br_startblock); 5273 5274 if (got.br_startoff < start) { 5275 del.br_startoff = start; 5276 del.br_blockcount -= start - got.br_startoff; 5277 if (!wasdel) 5278 del.br_startblock += start - got.br_startoff; 5279 } 5280 if (del.br_startoff + del.br_blockcount > end + 1) 5281 del.br_blockcount = end + 1 - del.br_startoff; 5282 5283 if (!isrt || (flags & XFS_BMAPI_REMAP)) 5284 goto delete; 5285 5286 mod = xfs_rtb_to_rtxoff(mp, 5287 del.br_startblock + del.br_blockcount); 5288 if (mod) { 5289 /* 5290 * Realtime extent not lined up at the end. 5291 * The extent could have been split into written 5292 * and unwritten pieces, or we could just be 5293 * unmapping part of it. But we can't really 5294 * get rid of part of a realtime extent. 5295 */ 5296 if (del.br_state == XFS_EXT_UNWRITTEN) { 5297 /* 5298 * This piece is unwritten, or we're not 5299 * using unwritten extents. Skip over it. 5300 */ 5301 ASSERT((flags & XFS_BMAPI_REMAP) || end >= mod); 5302 end -= mod > del.br_blockcount ? 5303 del.br_blockcount : mod; 5304 if (end < got.br_startoff && 5305 !xfs_iext_prev_extent(ifp, &icur, &got)) { 5306 done = true; 5307 break; 5308 } 5309 continue; 5310 } 5311 /* 5312 * It's written, turn it unwritten. 5313 * This is better than zeroing it. 5314 */ 5315 ASSERT(del.br_state == XFS_EXT_NORM); 5316 ASSERT(tp->t_blk_res > 0); 5317 /* 5318 * If this spans a realtime extent boundary, 5319 * chop it back to the start of the one we end at. 5320 */ 5321 if (del.br_blockcount > mod) { 5322 del.br_startoff += del.br_blockcount - mod; 5323 del.br_startblock += del.br_blockcount - mod; 5324 del.br_blockcount = mod; 5325 } 5326 del.br_state = XFS_EXT_UNWRITTEN; 5327 error = xfs_bmap_add_extent_unwritten_real(tp, ip, 5328 whichfork, &icur, &cur, &del, 5329 &logflags); 5330 if (error) 5331 goto error0; 5332 goto nodelete; 5333 } 5334 5335 mod = xfs_rtb_to_rtxoff(mp, del.br_startblock); 5336 if (mod) { 5337 xfs_extlen_t off = mp->m_sb.sb_rextsize - mod; 5338 5339 /* 5340 * Realtime extent is lined up at the end but not 5341 * at the front. We'll get rid of full extents if 5342 * we can. 5343 */ 5344 if (del.br_blockcount > off) { 5345 del.br_blockcount -= off; 5346 del.br_startoff += off; 5347 del.br_startblock += off; 5348 } else if (del.br_startoff == start && 5349 (del.br_state == XFS_EXT_UNWRITTEN || 5350 tp->t_blk_res == 0)) { 5351 /* 5352 * Can't make it unwritten. There isn't 5353 * a full extent here so just skip it. 5354 */ 5355 ASSERT(end >= del.br_blockcount); 5356 end -= del.br_blockcount; 5357 if (got.br_startoff > end && 5358 !xfs_iext_prev_extent(ifp, &icur, &got)) { 5359 done = true; 5360 break; 5361 } 5362 continue; 5363 } else if (del.br_state == XFS_EXT_UNWRITTEN) { 5364 struct xfs_bmbt_irec prev; 5365 xfs_fileoff_t unwrite_start; 5366 5367 /* 5368 * This one is already unwritten. 5369 * It must have a written left neighbor. 5370 * Unwrite the killed part of that one and 5371 * try again. 5372 */ 5373 if (!xfs_iext_prev_extent(ifp, &icur, &prev)) 5374 ASSERT(0); 5375 ASSERT(prev.br_state == XFS_EXT_NORM); 5376 ASSERT(!isnullstartblock(prev.br_startblock)); 5377 ASSERT(del.br_startblock == 5378 prev.br_startblock + prev.br_blockcount); 5379 unwrite_start = max3(start, 5380 del.br_startoff - mod, 5381 prev.br_startoff); 5382 mod = unwrite_start - prev.br_startoff; 5383 prev.br_startoff = unwrite_start; 5384 prev.br_startblock += mod; 5385 prev.br_blockcount -= mod; 5386 prev.br_state = XFS_EXT_UNWRITTEN; 5387 error = xfs_bmap_add_extent_unwritten_real(tp, 5388 ip, whichfork, &icur, &cur, 5389 &prev, &logflags); 5390 if (error) 5391 goto error0; 5392 goto nodelete; 5393 } else { 5394 ASSERT(del.br_state == XFS_EXT_NORM); 5395 del.br_state = XFS_EXT_UNWRITTEN; 5396 error = xfs_bmap_add_extent_unwritten_real(tp, 5397 ip, whichfork, &icur, &cur, 5398 &del, &logflags); 5399 if (error) 5400 goto error0; 5401 goto nodelete; 5402 } 5403 } 5404 5405 delete: 5406 if (wasdel) { 5407 xfs_bmap_del_extent_delay(ip, whichfork, &icur, &got, 5408 &del, flags); 5409 } else { 5410 error = xfs_bmap_del_extent_real(ip, tp, &icur, cur, 5411 &del, &tmp_logflags, whichfork, 5412 flags); 5413 logflags |= tmp_logflags; 5414 if (error) 5415 goto error0; 5416 } 5417 5418 end = del.br_startoff - 1; 5419 nodelete: 5420 /* 5421 * If not done go on to the next (previous) record. 5422 */ 5423 if (end != (xfs_fileoff_t)-1 && end >= start) { 5424 if (!xfs_iext_get_extent(ifp, &icur, &got) || 5425 (got.br_startoff > end && 5426 !xfs_iext_prev_extent(ifp, &icur, &got))) { 5427 done = true; 5428 break; 5429 } 5430 extno++; 5431 } 5432 } 5433 if (done || end == (xfs_fileoff_t)-1 || end < start) 5434 *rlen = 0; 5435 else 5436 *rlen = end - start + 1; 5437 5438 /* 5439 * Convert to a btree if necessary. 5440 */ 5441 if (xfs_bmap_needs_btree(ip, whichfork)) { 5442 ASSERT(cur == NULL); 5443 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, 5444 &tmp_logflags, whichfork); 5445 logflags |= tmp_logflags; 5446 } else { 5447 error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, 5448 whichfork); 5449 } 5450 5451 error0: 5452 /* 5453 * Log everything. Do this after conversion, there's no point in 5454 * logging the extent records if we've converted to btree format. 5455 */ 5456 if ((logflags & xfs_ilog_fext(whichfork)) && 5457 ifp->if_format != XFS_DINODE_FMT_EXTENTS) 5458 logflags &= ~xfs_ilog_fext(whichfork); 5459 else if ((logflags & xfs_ilog_fbroot(whichfork)) && 5460 ifp->if_format != XFS_DINODE_FMT_BTREE) 5461 logflags &= ~xfs_ilog_fbroot(whichfork); 5462 /* 5463 * Log inode even in the error case, if the transaction 5464 * is dirty we'll need to shut down the filesystem. 5465 */ 5466 if (logflags) 5467 xfs_trans_log_inode(tp, ip, logflags); 5468 if (cur) { 5469 if (!error) 5470 cur->bc_bmap.allocated = 0; 5471 xfs_btree_del_cursor(cur, error); 5472 } 5473 return error; 5474 } 5475 5476 /* Unmap a range of a file. */ 5477 int 5478 xfs_bunmapi( 5479 xfs_trans_t *tp, 5480 struct xfs_inode *ip, 5481 xfs_fileoff_t bno, 5482 xfs_filblks_t len, 5483 uint32_t flags, 5484 xfs_extnum_t nexts, 5485 int *done) 5486 { 5487 int error; 5488 5489 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts); 5490 *done = (len == 0); 5491 return error; 5492 } 5493 5494 /* 5495 * Determine whether an extent shift can be accomplished by a merge with the 5496 * extent that precedes the target hole of the shift. 5497 */ 5498 STATIC bool 5499 xfs_bmse_can_merge( 5500 struct xfs_inode *ip, 5501 int whichfork, 5502 struct xfs_bmbt_irec *left, /* preceding extent */ 5503 struct xfs_bmbt_irec *got, /* current extent to shift */ 5504 xfs_fileoff_t shift) /* shift fsb */ 5505 { 5506 xfs_fileoff_t startoff; 5507 5508 startoff = got->br_startoff - shift; 5509 5510 /* 5511 * The extent, once shifted, must be adjacent in-file and on-disk with 5512 * the preceding extent. 5513 */ 5514 if ((left->br_startoff + left->br_blockcount != startoff) || 5515 (left->br_startblock + left->br_blockcount != got->br_startblock) || 5516 (left->br_state != got->br_state) || 5517 (left->br_blockcount + got->br_blockcount > XFS_MAX_BMBT_EXTLEN) || 5518 !xfs_bmap_same_rtgroup(ip, whichfork, left, got)) 5519 return false; 5520 5521 return true; 5522 } 5523 5524 /* 5525 * A bmap extent shift adjusts the file offset of an extent to fill a preceding 5526 * hole in the file. If an extent shift would result in the extent being fully 5527 * adjacent to the extent that currently precedes the hole, we can merge with 5528 * the preceding extent rather than do the shift. 5529 * 5530 * This function assumes the caller has verified a shift-by-merge is possible 5531 * with the provided extents via xfs_bmse_can_merge(). 5532 */ 5533 STATIC int 5534 xfs_bmse_merge( 5535 struct xfs_trans *tp, 5536 struct xfs_inode *ip, 5537 int whichfork, 5538 xfs_fileoff_t shift, /* shift fsb */ 5539 struct xfs_iext_cursor *icur, 5540 struct xfs_bmbt_irec *got, /* extent to shift */ 5541 struct xfs_bmbt_irec *left, /* preceding extent */ 5542 struct xfs_btree_cur *cur, 5543 int *logflags) /* output */ 5544 { 5545 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); 5546 struct xfs_bmbt_irec new; 5547 xfs_filblks_t blockcount; 5548 int error, i; 5549 struct xfs_mount *mp = ip->i_mount; 5550 5551 blockcount = left->br_blockcount + got->br_blockcount; 5552 5553 xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL); 5554 ASSERT(xfs_bmse_can_merge(ip, whichfork, left, got, shift)); 5555 5556 new = *left; 5557 new.br_blockcount = blockcount; 5558 5559 /* 5560 * Update the on-disk extent count, the btree if necessary and log the 5561 * inode. 5562 */ 5563 ifp->if_nextents--; 5564 *logflags |= XFS_ILOG_CORE; 5565 if (!cur) { 5566 *logflags |= XFS_ILOG_DEXT; 5567 goto done; 5568 } 5569 5570 /* lookup and remove the extent to merge */ 5571 error = xfs_bmbt_lookup_eq(cur, got, &i); 5572 if (error) 5573 return error; 5574 if (XFS_IS_CORRUPT(mp, i != 1)) { 5575 xfs_btree_mark_sick(cur); 5576 return -EFSCORRUPTED; 5577 } 5578 5579 error = xfs_btree_delete(cur, &i); 5580 if (error) 5581 return error; 5582 if (XFS_IS_CORRUPT(mp, i != 1)) { 5583 xfs_btree_mark_sick(cur); 5584 return -EFSCORRUPTED; 5585 } 5586 5587 /* lookup and update size of the previous extent */ 5588 error = xfs_bmbt_lookup_eq(cur, left, &i); 5589 if (error) 5590 return error; 5591 if (XFS_IS_CORRUPT(mp, i != 1)) { 5592 xfs_btree_mark_sick(cur); 5593 return -EFSCORRUPTED; 5594 } 5595 5596 error = xfs_bmbt_update(cur, &new); 5597 if (error) 5598 return error; 5599 5600 /* change to extent format if required after extent removal */ 5601 error = xfs_bmap_btree_to_extents(tp, ip, cur, logflags, whichfork); 5602 if (error) 5603 return error; 5604 5605 done: 5606 xfs_iext_remove(ip, icur, 0); 5607 xfs_iext_prev(ifp, icur); 5608 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur, 5609 &new); 5610 5611 /* update reverse mapping. rmap functions merge the rmaps for us */ 5612 xfs_rmap_unmap_extent(tp, ip, whichfork, got); 5613 memcpy(&new, got, sizeof(new)); 5614 new.br_startoff = left->br_startoff + left->br_blockcount; 5615 xfs_rmap_map_extent(tp, ip, whichfork, &new); 5616 return 0; 5617 } 5618 5619 static int 5620 xfs_bmap_shift_update_extent( 5621 struct xfs_trans *tp, 5622 struct xfs_inode *ip, 5623 int whichfork, 5624 struct xfs_iext_cursor *icur, 5625 struct xfs_bmbt_irec *got, 5626 struct xfs_btree_cur *cur, 5627 int *logflags, 5628 xfs_fileoff_t startoff) 5629 { 5630 struct xfs_mount *mp = ip->i_mount; 5631 struct xfs_bmbt_irec prev = *got; 5632 int error, i; 5633 5634 *logflags |= XFS_ILOG_CORE; 5635 5636 got->br_startoff = startoff; 5637 5638 if (cur) { 5639 error = xfs_bmbt_lookup_eq(cur, &prev, &i); 5640 if (error) 5641 return error; 5642 if (XFS_IS_CORRUPT(mp, i != 1)) { 5643 xfs_btree_mark_sick(cur); 5644 return -EFSCORRUPTED; 5645 } 5646 5647 error = xfs_bmbt_update(cur, got); 5648 if (error) 5649 return error; 5650 } else { 5651 *logflags |= XFS_ILOG_DEXT; 5652 } 5653 5654 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur, 5655 got); 5656 5657 /* update reverse mapping */ 5658 xfs_rmap_unmap_extent(tp, ip, whichfork, &prev); 5659 xfs_rmap_map_extent(tp, ip, whichfork, got); 5660 return 0; 5661 } 5662 5663 int 5664 xfs_bmap_collapse_extents( 5665 struct xfs_trans *tp, 5666 struct xfs_inode *ip, 5667 xfs_fileoff_t *next_fsb, 5668 xfs_fileoff_t offset_shift_fsb, 5669 bool *done) 5670 { 5671 int whichfork = XFS_DATA_FORK; 5672 struct xfs_mount *mp = ip->i_mount; 5673 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); 5674 struct xfs_btree_cur *cur = NULL; 5675 struct xfs_bmbt_irec got, prev; 5676 struct xfs_iext_cursor icur; 5677 xfs_fileoff_t new_startoff; 5678 int error = 0; 5679 int logflags = 0; 5680 5681 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) || 5682 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) { 5683 xfs_bmap_mark_sick(ip, whichfork); 5684 return -EFSCORRUPTED; 5685 } 5686 5687 if (xfs_is_shutdown(mp)) 5688 return -EIO; 5689 5690 xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL); 5691 5692 error = xfs_iread_extents(tp, ip, whichfork); 5693 if (error) 5694 return error; 5695 5696 if (ifp->if_format == XFS_DINODE_FMT_BTREE) 5697 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); 5698 5699 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) { 5700 *done = true; 5701 goto del_cursor; 5702 } 5703 if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) { 5704 xfs_bmap_mark_sick(ip, whichfork); 5705 error = -EFSCORRUPTED; 5706 goto del_cursor; 5707 } 5708 5709 new_startoff = got.br_startoff - offset_shift_fsb; 5710 if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) { 5711 if (new_startoff < prev.br_startoff + prev.br_blockcount) { 5712 error = -EINVAL; 5713 goto del_cursor; 5714 } 5715 5716 if (xfs_bmse_can_merge(ip, whichfork, &prev, &got, 5717 offset_shift_fsb)) { 5718 error = xfs_bmse_merge(tp, ip, whichfork, 5719 offset_shift_fsb, &icur, &got, &prev, 5720 cur, &logflags); 5721 if (error) 5722 goto del_cursor; 5723 goto done; 5724 } 5725 } else { 5726 if (got.br_startoff < offset_shift_fsb) { 5727 error = -EINVAL; 5728 goto del_cursor; 5729 } 5730 } 5731 5732 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got, 5733 cur, &logflags, new_startoff); 5734 if (error) 5735 goto del_cursor; 5736 5737 done: 5738 if (!xfs_iext_next_extent(ifp, &icur, &got)) { 5739 *done = true; 5740 goto del_cursor; 5741 } 5742 5743 *next_fsb = got.br_startoff; 5744 del_cursor: 5745 if (cur) 5746 xfs_btree_del_cursor(cur, error); 5747 if (logflags) 5748 xfs_trans_log_inode(tp, ip, logflags); 5749 return error; 5750 } 5751 5752 /* Make sure we won't be right-shifting an extent past the maximum bound. */ 5753 int 5754 xfs_bmap_can_insert_extents( 5755 struct xfs_inode *ip, 5756 xfs_fileoff_t off, 5757 xfs_fileoff_t shift) 5758 { 5759 struct xfs_bmbt_irec got; 5760 int is_empty; 5761 int error = 0; 5762 5763 xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL); 5764 5765 if (xfs_is_shutdown(ip->i_mount)) 5766 return -EIO; 5767 5768 xfs_ilock(ip, XFS_ILOCK_EXCL); 5769 error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty); 5770 if (!error && !is_empty && got.br_startoff >= off && 5771 ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff) 5772 error = -EINVAL; 5773 xfs_iunlock(ip, XFS_ILOCK_EXCL); 5774 5775 return error; 5776 } 5777 5778 int 5779 xfs_bmap_insert_extents( 5780 struct xfs_trans *tp, 5781 struct xfs_inode *ip, 5782 xfs_fileoff_t *next_fsb, 5783 xfs_fileoff_t offset_shift_fsb, 5784 bool *done, 5785 xfs_fileoff_t stop_fsb) 5786 { 5787 int whichfork = XFS_DATA_FORK; 5788 struct xfs_mount *mp = ip->i_mount; 5789 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); 5790 struct xfs_btree_cur *cur = NULL; 5791 struct xfs_bmbt_irec got, next; 5792 struct xfs_iext_cursor icur; 5793 xfs_fileoff_t new_startoff; 5794 int error = 0; 5795 int logflags = 0; 5796 5797 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) || 5798 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) { 5799 xfs_bmap_mark_sick(ip, whichfork); 5800 return -EFSCORRUPTED; 5801 } 5802 5803 if (xfs_is_shutdown(mp)) 5804 return -EIO; 5805 5806 xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL); 5807 5808 error = xfs_iread_extents(tp, ip, whichfork); 5809 if (error) 5810 return error; 5811 5812 if (ifp->if_format == XFS_DINODE_FMT_BTREE) 5813 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); 5814 5815 if (*next_fsb == NULLFSBLOCK) { 5816 xfs_iext_last(ifp, &icur); 5817 if (!xfs_iext_get_extent(ifp, &icur, &got) || 5818 stop_fsb > got.br_startoff) { 5819 *done = true; 5820 goto del_cursor; 5821 } 5822 } else { 5823 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) { 5824 *done = true; 5825 goto del_cursor; 5826 } 5827 } 5828 if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) { 5829 xfs_bmap_mark_sick(ip, whichfork); 5830 error = -EFSCORRUPTED; 5831 goto del_cursor; 5832 } 5833 5834 if (XFS_IS_CORRUPT(mp, stop_fsb > got.br_startoff)) { 5835 xfs_bmap_mark_sick(ip, whichfork); 5836 error = -EFSCORRUPTED; 5837 goto del_cursor; 5838 } 5839 5840 new_startoff = got.br_startoff + offset_shift_fsb; 5841 if (xfs_iext_peek_next_extent(ifp, &icur, &next)) { 5842 if (new_startoff + got.br_blockcount > next.br_startoff) { 5843 error = -EINVAL; 5844 goto del_cursor; 5845 } 5846 5847 /* 5848 * Unlike a left shift (which involves a hole punch), a right 5849 * shift does not modify extent neighbors in any way. We should 5850 * never find mergeable extents in this scenario. Check anyways 5851 * and warn if we encounter two extents that could be one. 5852 */ 5853 if (xfs_bmse_can_merge(ip, whichfork, &got, &next, 5854 offset_shift_fsb)) 5855 WARN_ON_ONCE(1); 5856 } 5857 5858 error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got, 5859 cur, &logflags, new_startoff); 5860 if (error) 5861 goto del_cursor; 5862 5863 if (!xfs_iext_prev_extent(ifp, &icur, &got) || 5864 stop_fsb >= got.br_startoff + got.br_blockcount) { 5865 *done = true; 5866 goto del_cursor; 5867 } 5868 5869 *next_fsb = got.br_startoff; 5870 del_cursor: 5871 if (cur) 5872 xfs_btree_del_cursor(cur, error); 5873 if (logflags) 5874 xfs_trans_log_inode(tp, ip, logflags); 5875 return error; 5876 } 5877 5878 /* 5879 * Splits an extent into two extents at split_fsb block such that it is the 5880 * first block of the current_ext. @ext is a target extent to be split. 5881 * @split_fsb is a block where the extents is split. If split_fsb lies in a 5882 * hole or the first block of extents, just return 0. 5883 */ 5884 int 5885 xfs_bmap_split_extent( 5886 struct xfs_trans *tp, 5887 struct xfs_inode *ip, 5888 xfs_fileoff_t split_fsb) 5889 { 5890 int whichfork = XFS_DATA_FORK; 5891 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); 5892 struct xfs_btree_cur *cur = NULL; 5893 struct xfs_bmbt_irec got; 5894 struct xfs_bmbt_irec new; /* split extent */ 5895 struct xfs_mount *mp = ip->i_mount; 5896 xfs_fsblock_t gotblkcnt; /* new block count for got */ 5897 struct xfs_iext_cursor icur; 5898 int error = 0; 5899 int logflags = 0; 5900 int i = 0; 5901 5902 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) || 5903 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) { 5904 xfs_bmap_mark_sick(ip, whichfork); 5905 return -EFSCORRUPTED; 5906 } 5907 5908 if (xfs_is_shutdown(mp)) 5909 return -EIO; 5910 5911 /* Read in all the extents */ 5912 error = xfs_iread_extents(tp, ip, whichfork); 5913 if (error) 5914 return error; 5915 5916 /* 5917 * If there are not extents, or split_fsb lies in a hole we are done. 5918 */ 5919 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) || 5920 got.br_startoff >= split_fsb) 5921 return 0; 5922 5923 gotblkcnt = split_fsb - got.br_startoff; 5924 new.br_startoff = split_fsb; 5925 new.br_startblock = got.br_startblock + gotblkcnt; 5926 new.br_blockcount = got.br_blockcount - gotblkcnt; 5927 new.br_state = got.br_state; 5928 5929 if (ifp->if_format == XFS_DINODE_FMT_BTREE) { 5930 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); 5931 error = xfs_bmbt_lookup_eq(cur, &got, &i); 5932 if (error) 5933 goto del_cursor; 5934 if (XFS_IS_CORRUPT(mp, i != 1)) { 5935 xfs_btree_mark_sick(cur); 5936 error = -EFSCORRUPTED; 5937 goto del_cursor; 5938 } 5939 } 5940 5941 got.br_blockcount = gotblkcnt; 5942 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur, 5943 &got); 5944 5945 logflags = XFS_ILOG_CORE; 5946 if (cur) { 5947 error = xfs_bmbt_update(cur, &got); 5948 if (error) 5949 goto del_cursor; 5950 } else 5951 logflags |= XFS_ILOG_DEXT; 5952 5953 /* Add new extent */ 5954 xfs_iext_next(ifp, &icur); 5955 xfs_iext_insert(ip, &icur, &new, 0); 5956 ifp->if_nextents++; 5957 5958 if (cur) { 5959 error = xfs_bmbt_lookup_eq(cur, &new, &i); 5960 if (error) 5961 goto del_cursor; 5962 if (XFS_IS_CORRUPT(mp, i != 0)) { 5963 xfs_btree_mark_sick(cur); 5964 error = -EFSCORRUPTED; 5965 goto del_cursor; 5966 } 5967 error = xfs_btree_insert(cur, &i); 5968 if (error) 5969 goto del_cursor; 5970 if (XFS_IS_CORRUPT(mp, i != 1)) { 5971 xfs_btree_mark_sick(cur); 5972 error = -EFSCORRUPTED; 5973 goto del_cursor; 5974 } 5975 } 5976 5977 /* 5978 * Convert to a btree if necessary. 5979 */ 5980 if (xfs_bmap_needs_btree(ip, whichfork)) { 5981 int tmp_logflags; /* partial log flag return val */ 5982 5983 ASSERT(cur == NULL); 5984 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, 5985 &tmp_logflags, whichfork); 5986 logflags |= tmp_logflags; 5987 } 5988 5989 del_cursor: 5990 if (cur) { 5991 cur->bc_bmap.allocated = 0; 5992 xfs_btree_del_cursor(cur, error); 5993 } 5994 5995 if (logflags) 5996 xfs_trans_log_inode(tp, ip, logflags); 5997 return error; 5998 } 5999 6000 /* Record a bmap intent. */ 6001 static inline void 6002 __xfs_bmap_add( 6003 struct xfs_trans *tp, 6004 enum xfs_bmap_intent_type type, 6005 struct xfs_inode *ip, 6006 int whichfork, 6007 struct xfs_bmbt_irec *bmap) 6008 { 6009 struct xfs_bmap_intent *bi; 6010 6011 if ((whichfork != XFS_DATA_FORK && whichfork != XFS_ATTR_FORK) || 6012 bmap->br_startblock == HOLESTARTBLOCK || 6013 bmap->br_startblock == DELAYSTARTBLOCK) 6014 return; 6015 6016 bi = kmem_cache_alloc(xfs_bmap_intent_cache, GFP_KERNEL | __GFP_NOFAIL); 6017 INIT_LIST_HEAD(&bi->bi_list); 6018 bi->bi_type = type; 6019 bi->bi_owner = ip; 6020 bi->bi_whichfork = whichfork; 6021 bi->bi_bmap = *bmap; 6022 6023 xfs_bmap_defer_add(tp, bi); 6024 } 6025 6026 /* Map an extent into a file. */ 6027 void 6028 xfs_bmap_map_extent( 6029 struct xfs_trans *tp, 6030 struct xfs_inode *ip, 6031 int whichfork, 6032 struct xfs_bmbt_irec *PREV) 6033 { 6034 __xfs_bmap_add(tp, XFS_BMAP_MAP, ip, whichfork, PREV); 6035 } 6036 6037 /* Unmap an extent out of a file. */ 6038 void 6039 xfs_bmap_unmap_extent( 6040 struct xfs_trans *tp, 6041 struct xfs_inode *ip, 6042 int whichfork, 6043 struct xfs_bmbt_irec *PREV) 6044 { 6045 __xfs_bmap_add(tp, XFS_BMAP_UNMAP, ip, whichfork, PREV); 6046 } 6047 6048 /* 6049 * Process one of the deferred bmap operations. We pass back the 6050 * btree cursor to maintain our lock on the bmapbt between calls. 6051 */ 6052 int 6053 xfs_bmap_finish_one( 6054 struct xfs_trans *tp, 6055 struct xfs_bmap_intent *bi) 6056 { 6057 struct xfs_bmbt_irec *bmap = &bi->bi_bmap; 6058 int error = 0; 6059 int flags = 0; 6060 6061 if (bi->bi_whichfork == XFS_ATTR_FORK) 6062 flags |= XFS_BMAPI_ATTRFORK; 6063 6064 ASSERT(tp->t_highest_agno == NULLAGNUMBER); 6065 6066 trace_xfs_bmap_deferred(bi); 6067 6068 if (XFS_TEST_ERROR(false, tp->t_mountp, XFS_ERRTAG_BMAP_FINISH_ONE)) 6069 return -EIO; 6070 6071 switch (bi->bi_type) { 6072 case XFS_BMAP_MAP: 6073 if (bi->bi_bmap.br_state == XFS_EXT_UNWRITTEN) 6074 flags |= XFS_BMAPI_PREALLOC; 6075 error = xfs_bmapi_remap(tp, bi->bi_owner, bmap->br_startoff, 6076 bmap->br_blockcount, bmap->br_startblock, 6077 flags); 6078 bmap->br_blockcount = 0; 6079 break; 6080 case XFS_BMAP_UNMAP: 6081 error = __xfs_bunmapi(tp, bi->bi_owner, bmap->br_startoff, 6082 &bmap->br_blockcount, flags | XFS_BMAPI_REMAP, 6083 1); 6084 break; 6085 default: 6086 ASSERT(0); 6087 xfs_bmap_mark_sick(bi->bi_owner, bi->bi_whichfork); 6088 error = -EFSCORRUPTED; 6089 } 6090 6091 return error; 6092 } 6093 6094 /* Check that an extent does not have invalid flags or bad ranges. */ 6095 xfs_failaddr_t 6096 xfs_bmap_validate_extent_raw( 6097 struct xfs_mount *mp, 6098 bool rtfile, 6099 int whichfork, 6100 struct xfs_bmbt_irec *irec) 6101 { 6102 if (!xfs_verify_fileext(mp, irec->br_startoff, irec->br_blockcount)) 6103 return __this_address; 6104 6105 if (rtfile && whichfork == XFS_DATA_FORK) { 6106 if (!xfs_verify_rtbext(mp, irec->br_startblock, 6107 irec->br_blockcount)) 6108 return __this_address; 6109 } else { 6110 if (!xfs_verify_fsbext(mp, irec->br_startblock, 6111 irec->br_blockcount)) 6112 return __this_address; 6113 } 6114 if (irec->br_state != XFS_EXT_NORM && whichfork != XFS_DATA_FORK) 6115 return __this_address; 6116 return NULL; 6117 } 6118 6119 int __init 6120 xfs_bmap_intent_init_cache(void) 6121 { 6122 xfs_bmap_intent_cache = kmem_cache_create("xfs_bmap_intent", 6123 sizeof(struct xfs_bmap_intent), 6124 0, 0, NULL); 6125 6126 return xfs_bmap_intent_cache != NULL ? 0 : -ENOMEM; 6127 } 6128 6129 void 6130 xfs_bmap_intent_destroy_cache(void) 6131 { 6132 kmem_cache_destroy(xfs_bmap_intent_cache); 6133 xfs_bmap_intent_cache = NULL; 6134 } 6135 6136 /* Check that an inode's extent does not have invalid flags or bad ranges. */ 6137 xfs_failaddr_t 6138 xfs_bmap_validate_extent( 6139 struct xfs_inode *ip, 6140 int whichfork, 6141 struct xfs_bmbt_irec *irec) 6142 { 6143 return xfs_bmap_validate_extent_raw(ip->i_mount, 6144 XFS_IS_REALTIME_INODE(ip), whichfork, irec); 6145 } 6146 6147 /* 6148 * Used in xfs_itruncate_extents(). This is the maximum number of extents 6149 * freed from a file in a single transaction. 6150 */ 6151 #define XFS_ITRUNC_MAX_EXTENTS 2 6152 6153 /* 6154 * Unmap every extent in part of an inode's fork. We don't do any higher level 6155 * invalidation work at all. 6156 */ 6157 int 6158 xfs_bunmapi_range( 6159 struct xfs_trans **tpp, 6160 struct xfs_inode *ip, 6161 uint32_t flags, 6162 xfs_fileoff_t startoff, 6163 xfs_fileoff_t endoff) 6164 { 6165 xfs_filblks_t unmap_len = endoff - startoff + 1; 6166 int error = 0; 6167 6168 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL); 6169 6170 while (unmap_len > 0) { 6171 ASSERT((*tpp)->t_highest_agno == NULLAGNUMBER); 6172 error = __xfs_bunmapi(*tpp, ip, startoff, &unmap_len, flags, 6173 XFS_ITRUNC_MAX_EXTENTS); 6174 if (error) 6175 goto out; 6176 6177 /* free the just unmapped extents */ 6178 error = xfs_defer_finish(tpp); 6179 if (error) 6180 goto out; 6181 cond_resched(); 6182 } 6183 out: 6184 return error; 6185 } 6186 6187 struct xfs_bmap_query_range { 6188 xfs_bmap_query_range_fn fn; 6189 void *priv; 6190 }; 6191 6192 /* Format btree record and pass to our callback. */ 6193 STATIC int 6194 xfs_bmap_query_range_helper( 6195 struct xfs_btree_cur *cur, 6196 const union xfs_btree_rec *rec, 6197 void *priv) 6198 { 6199 struct xfs_bmap_query_range *query = priv; 6200 struct xfs_bmbt_irec irec; 6201 xfs_failaddr_t fa; 6202 6203 xfs_bmbt_disk_get_all(&rec->bmbt, &irec); 6204 fa = xfs_bmap_validate_extent(cur->bc_ino.ip, cur->bc_ino.whichfork, 6205 &irec); 6206 if (fa) { 6207 xfs_btree_mark_sick(cur); 6208 return xfs_bmap_complain_bad_rec(cur->bc_ino.ip, 6209 cur->bc_ino.whichfork, fa, &irec); 6210 } 6211 6212 return query->fn(cur, &irec, query->priv); 6213 } 6214 6215 /* Find all bmaps. */ 6216 int 6217 xfs_bmap_query_all( 6218 struct xfs_btree_cur *cur, 6219 xfs_bmap_query_range_fn fn, 6220 void *priv) 6221 { 6222 struct xfs_bmap_query_range query = { 6223 .priv = priv, 6224 .fn = fn, 6225 }; 6226 6227 return xfs_btree_query_all(cur, xfs_bmap_query_range_helper, &query); 6228 } 6229 6230 /* Helper function to extract extent size hint from inode */ 6231 xfs_extlen_t 6232 xfs_get_extsz_hint( 6233 struct xfs_inode *ip) 6234 { 6235 /* 6236 * No point in aligning allocations if we need to COW to actually 6237 * write to them. 6238 */ 6239 if (!xfs_is_always_cow_inode(ip) && 6240 (ip->i_diflags & XFS_DIFLAG_EXTSIZE) && ip->i_extsize) 6241 return ip->i_extsize; 6242 if (XFS_IS_REALTIME_INODE(ip) && 6243 ip->i_mount->m_sb.sb_rextsize > 1) 6244 return ip->i_mount->m_sb.sb_rextsize; 6245 return 0; 6246 } 6247 6248 /* 6249 * Helper function to extract CoW extent size hint from inode. 6250 * Between the extent size hint and the CoW extent size hint, we 6251 * return the greater of the two. If the value is zero (automatic), 6252 * use the default size. 6253 */ 6254 xfs_extlen_t 6255 xfs_get_cowextsz_hint( 6256 struct xfs_inode *ip) 6257 { 6258 xfs_extlen_t a, b; 6259 6260 a = 0; 6261 if (ip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE) 6262 a = ip->i_cowextsize; 6263 if (XFS_IS_REALTIME_INODE(ip)) { 6264 b = 0; 6265 if (ip->i_diflags & XFS_DIFLAG_EXTSIZE) 6266 b = ip->i_extsize; 6267 } else { 6268 b = xfs_get_extsz_hint(ip); 6269 } 6270 6271 a = max(a, b); 6272 if (a == 0) 6273 return XFS_DEFAULT_COWEXTSZ_HINT; 6274 return a; 6275 } 6276