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