1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc. 4 * All Rights Reserved. 5 */ 6 #include "xfs.h" 7 #include "xfs_fs.h" 8 #include "xfs_shared.h" 9 #include "xfs_format.h" 10 #include "xfs_log_format.h" 11 #include "xfs_trans_resv.h" 12 #include "xfs_bit.h" 13 #include "xfs_mount.h" 14 #include "xfs_inode.h" 15 #include "xfs_btree.h" 16 #include "xfs_ialloc.h" 17 #include "xfs_ialloc_btree.h" 18 #include "xfs_alloc.h" 19 #include "xfs_errortag.h" 20 #include "xfs_error.h" 21 #include "xfs_bmap.h" 22 #include "xfs_trans.h" 23 #include "xfs_buf_item.h" 24 #include "xfs_icreate_item.h" 25 #include "xfs_icache.h" 26 #include "xfs_trace.h" 27 #include "xfs_log.h" 28 #include "xfs_rmap.h" 29 #include "xfs_ag.h" 30 #include "xfs_health.h" 31 32 /* 33 * Lookup a record by ino in the btree given by cur. 34 */ 35 int /* error */ 36 xfs_inobt_lookup( 37 struct xfs_btree_cur *cur, /* btree cursor */ 38 xfs_agino_t ino, /* starting inode of chunk */ 39 xfs_lookup_t dir, /* <=, >=, == */ 40 int *stat) /* success/failure */ 41 { 42 cur->bc_rec.i.ir_startino = ino; 43 cur->bc_rec.i.ir_holemask = 0; 44 cur->bc_rec.i.ir_count = 0; 45 cur->bc_rec.i.ir_freecount = 0; 46 cur->bc_rec.i.ir_free = 0; 47 return xfs_btree_lookup(cur, dir, stat); 48 } 49 50 /* 51 * Update the record referred to by cur to the value given. 52 * This either works (return 0) or gets an EFSCORRUPTED error. 53 */ 54 STATIC int /* error */ 55 xfs_inobt_update( 56 struct xfs_btree_cur *cur, /* btree cursor */ 57 xfs_inobt_rec_incore_t *irec) /* btree record */ 58 { 59 union xfs_btree_rec rec; 60 61 rec.inobt.ir_startino = cpu_to_be32(irec->ir_startino); 62 if (xfs_has_sparseinodes(cur->bc_mp)) { 63 rec.inobt.ir_u.sp.ir_holemask = cpu_to_be16(irec->ir_holemask); 64 rec.inobt.ir_u.sp.ir_count = irec->ir_count; 65 rec.inobt.ir_u.sp.ir_freecount = irec->ir_freecount; 66 } else { 67 /* ir_holemask/ir_count not supported on-disk */ 68 rec.inobt.ir_u.f.ir_freecount = cpu_to_be32(irec->ir_freecount); 69 } 70 rec.inobt.ir_free = cpu_to_be64(irec->ir_free); 71 return xfs_btree_update(cur, &rec); 72 } 73 74 /* Convert on-disk btree record to incore inobt record. */ 75 void 76 xfs_inobt_btrec_to_irec( 77 struct xfs_mount *mp, 78 const union xfs_btree_rec *rec, 79 struct xfs_inobt_rec_incore *irec) 80 { 81 irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino); 82 if (xfs_has_sparseinodes(mp)) { 83 irec->ir_holemask = be16_to_cpu(rec->inobt.ir_u.sp.ir_holemask); 84 irec->ir_count = rec->inobt.ir_u.sp.ir_count; 85 irec->ir_freecount = rec->inobt.ir_u.sp.ir_freecount; 86 } else { 87 /* 88 * ir_holemask/ir_count not supported on-disk. Fill in hardcoded 89 * values for full inode chunks. 90 */ 91 irec->ir_holemask = XFS_INOBT_HOLEMASK_FULL; 92 irec->ir_count = XFS_INODES_PER_CHUNK; 93 irec->ir_freecount = 94 be32_to_cpu(rec->inobt.ir_u.f.ir_freecount); 95 } 96 irec->ir_free = be64_to_cpu(rec->inobt.ir_free); 97 } 98 99 /* Compute the freecount of an incore inode record. */ 100 uint8_t 101 xfs_inobt_rec_freecount( 102 const struct xfs_inobt_rec_incore *irec) 103 { 104 uint64_t realfree = irec->ir_free; 105 106 if (xfs_inobt_issparse(irec->ir_holemask)) 107 realfree &= xfs_inobt_irec_to_allocmask(irec); 108 return hweight64(realfree); 109 } 110 111 /* Simple checks for inode records. */ 112 xfs_failaddr_t 113 xfs_inobt_check_irec( 114 struct xfs_perag *pag, 115 const struct xfs_inobt_rec_incore *irec) 116 { 117 /* Record has to be properly aligned within the AG. */ 118 if (!xfs_verify_agino(pag, irec->ir_startino)) 119 return __this_address; 120 if (!xfs_verify_agino(pag, 121 irec->ir_startino + XFS_INODES_PER_CHUNK - 1)) 122 return __this_address; 123 if (irec->ir_count < XFS_INODES_PER_HOLEMASK_BIT || 124 irec->ir_count > XFS_INODES_PER_CHUNK) 125 return __this_address; 126 if (irec->ir_freecount > XFS_INODES_PER_CHUNK) 127 return __this_address; 128 129 if (xfs_inobt_rec_freecount(irec) != irec->ir_freecount) 130 return __this_address; 131 132 return NULL; 133 } 134 135 static inline int 136 xfs_inobt_complain_bad_rec( 137 struct xfs_btree_cur *cur, 138 xfs_failaddr_t fa, 139 const struct xfs_inobt_rec_incore *irec) 140 { 141 struct xfs_mount *mp = cur->bc_mp; 142 143 xfs_warn(mp, 144 "%sbt record corruption in AG %d detected at %pS!", 145 cur->bc_ops->name, cur->bc_group->xg_gno, fa); 146 xfs_warn(mp, 147 "start inode 0x%x, count 0x%x, free 0x%x freemask 0x%llx, holemask 0x%x", 148 irec->ir_startino, irec->ir_count, irec->ir_freecount, 149 irec->ir_free, irec->ir_holemask); 150 xfs_btree_mark_sick(cur); 151 return -EFSCORRUPTED; 152 } 153 154 /* 155 * Get the data from the pointed-to record. 156 */ 157 int 158 xfs_inobt_get_rec( 159 struct xfs_btree_cur *cur, 160 struct xfs_inobt_rec_incore *irec, 161 int *stat) 162 { 163 struct xfs_mount *mp = cur->bc_mp; 164 union xfs_btree_rec *rec; 165 xfs_failaddr_t fa; 166 int error; 167 168 error = xfs_btree_get_rec(cur, &rec, stat); 169 if (error || *stat == 0) 170 return error; 171 172 xfs_inobt_btrec_to_irec(mp, rec, irec); 173 fa = xfs_inobt_check_irec(to_perag(cur->bc_group), irec); 174 if (fa) 175 return xfs_inobt_complain_bad_rec(cur, fa, irec); 176 177 return 0; 178 } 179 180 /* 181 * Insert a single inobt record. Cursor must already point to desired location. 182 */ 183 int 184 xfs_inobt_insert_rec( 185 struct xfs_btree_cur *cur, 186 uint16_t holemask, 187 uint8_t count, 188 int32_t freecount, 189 xfs_inofree_t free, 190 int *stat) 191 { 192 cur->bc_rec.i.ir_holemask = holemask; 193 cur->bc_rec.i.ir_count = count; 194 cur->bc_rec.i.ir_freecount = freecount; 195 cur->bc_rec.i.ir_free = free; 196 return xfs_btree_insert(cur, stat); 197 } 198 199 /* 200 * Insert records describing a newly allocated inode chunk into the inobt. 201 */ 202 STATIC int 203 xfs_inobt_insert( 204 struct xfs_perag *pag, 205 struct xfs_trans *tp, 206 struct xfs_buf *agbp, 207 xfs_agino_t newino, 208 xfs_agino_t newlen, 209 bool is_finobt) 210 { 211 struct xfs_btree_cur *cur; 212 xfs_agino_t thisino; 213 int i; 214 int error; 215 216 if (is_finobt) 217 cur = xfs_finobt_init_cursor(pag, tp, agbp); 218 else 219 cur = xfs_inobt_init_cursor(pag, tp, agbp); 220 221 for (thisino = newino; 222 thisino < newino + newlen; 223 thisino += XFS_INODES_PER_CHUNK) { 224 error = xfs_inobt_lookup(cur, thisino, XFS_LOOKUP_EQ, &i); 225 if (error) { 226 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 227 return error; 228 } 229 ASSERT(i == 0); 230 231 error = xfs_inobt_insert_rec(cur, XFS_INOBT_HOLEMASK_FULL, 232 XFS_INODES_PER_CHUNK, 233 XFS_INODES_PER_CHUNK, 234 XFS_INOBT_ALL_FREE, &i); 235 if (error) { 236 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 237 return error; 238 } 239 ASSERT(i == 1); 240 } 241 242 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 243 244 return 0; 245 } 246 247 /* 248 * Verify that the number of free inodes in the AGI is correct. 249 */ 250 #ifdef DEBUG 251 static int 252 xfs_check_agi_freecount( 253 struct xfs_btree_cur *cur) 254 { 255 if (cur->bc_nlevels == 1) { 256 xfs_inobt_rec_incore_t rec; 257 int freecount = 0; 258 int error; 259 int i; 260 261 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i); 262 if (error) 263 return error; 264 265 do { 266 error = xfs_inobt_get_rec(cur, &rec, &i); 267 if (error) 268 return error; 269 270 if (i) { 271 freecount += rec.ir_freecount; 272 error = xfs_btree_increment(cur, 0, &i); 273 if (error) 274 return error; 275 } 276 } while (i == 1); 277 278 if (!xfs_is_shutdown(cur->bc_mp)) { 279 ASSERT(freecount == 280 to_perag(cur->bc_group)->pagi_freecount); 281 } 282 } 283 return 0; 284 } 285 #else 286 #define xfs_check_agi_freecount(cur) 0 287 #endif 288 289 /* 290 * Initialise a new set of inodes. When called without a transaction context 291 * (e.g. from recovery) we initiate a delayed write of the inode buffers rather 292 * than logging them (which in a transaction context puts them into the AIL 293 * for writeback rather than the xfsbufd queue). 294 */ 295 int 296 xfs_ialloc_inode_init( 297 struct xfs_mount *mp, 298 struct xfs_trans *tp, 299 struct list_head *buffer_list, 300 int icount, 301 xfs_agnumber_t agno, 302 xfs_agblock_t agbno, 303 xfs_agblock_t length, 304 unsigned int gen) 305 { 306 struct xfs_buf *fbuf; 307 struct xfs_dinode *free; 308 int nbufs; 309 int version; 310 int i, j; 311 xfs_daddr_t d; 312 xfs_ino_t ino = 0; 313 int error; 314 315 /* 316 * Loop over the new block(s), filling in the inodes. For small block 317 * sizes, manipulate the inodes in buffers which are multiples of the 318 * blocks size. 319 */ 320 nbufs = length / M_IGEO(mp)->blocks_per_cluster; 321 322 /* 323 * Figure out what version number to use in the inodes we create. If 324 * the superblock version has caught up to the one that supports the new 325 * inode format, then use the new inode version. Otherwise use the old 326 * version so that old kernels will continue to be able to use the file 327 * system. 328 * 329 * For v3 inodes, we also need to write the inode number into the inode, 330 * so calculate the first inode number of the chunk here as 331 * XFS_AGB_TO_AGINO() only works within a filesystem block, not 332 * across multiple filesystem blocks (such as a cluster) and so cannot 333 * be used in the cluster buffer loop below. 334 * 335 * Further, because we are writing the inode directly into the buffer 336 * and calculating a CRC on the entire inode, we have ot log the entire 337 * inode so that the entire range the CRC covers is present in the log. 338 * That means for v3 inode we log the entire buffer rather than just the 339 * inode cores. 340 */ 341 if (xfs_has_v3inodes(mp)) { 342 version = 3; 343 ino = XFS_AGINO_TO_INO(mp, agno, XFS_AGB_TO_AGINO(mp, agbno)); 344 345 /* 346 * log the initialisation that is about to take place as an 347 * logical operation. This means the transaction does not 348 * need to log the physical changes to the inode buffers as log 349 * recovery will know what initialisation is actually needed. 350 * Hence we only need to log the buffers as "ordered" buffers so 351 * they track in the AIL as if they were physically logged. 352 */ 353 if (tp) 354 xfs_icreate_log(tp, agno, agbno, icount, 355 mp->m_sb.sb_inodesize, length, gen); 356 } else 357 version = 2; 358 359 for (j = 0; j < nbufs; j++) { 360 /* 361 * Get the block. 362 */ 363 d = XFS_AGB_TO_DADDR(mp, agno, agbno + 364 (j * M_IGEO(mp)->blocks_per_cluster)); 365 error = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, 366 mp->m_bsize * M_IGEO(mp)->blocks_per_cluster, 367 XBF_UNMAPPED, &fbuf); 368 if (error) 369 return error; 370 371 /* Initialize the inode buffers and log them appropriately. */ 372 fbuf->b_ops = &xfs_inode_buf_ops; 373 xfs_buf_zero(fbuf, 0, BBTOB(fbuf->b_length)); 374 for (i = 0; i < M_IGEO(mp)->inodes_per_cluster; i++) { 375 int ioffset = i << mp->m_sb.sb_inodelog; 376 377 free = xfs_make_iptr(mp, fbuf, i); 378 free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC); 379 free->di_version = version; 380 free->di_gen = cpu_to_be32(gen); 381 free->di_next_unlinked = cpu_to_be32(NULLAGINO); 382 383 if (version == 3) { 384 free->di_ino = cpu_to_be64(ino); 385 ino++; 386 uuid_copy(&free->di_uuid, 387 &mp->m_sb.sb_meta_uuid); 388 xfs_dinode_calc_crc(mp, free); 389 } else if (tp) { 390 /* just log the inode core */ 391 xfs_trans_log_buf(tp, fbuf, ioffset, 392 ioffset + XFS_DINODE_SIZE(mp) - 1); 393 } 394 } 395 396 if (tp) { 397 /* 398 * Mark the buffer as an inode allocation buffer so it 399 * sticks in AIL at the point of this allocation 400 * transaction. This ensures the they are on disk before 401 * the tail of the log can be moved past this 402 * transaction (i.e. by preventing relogging from moving 403 * it forward in the log). 404 */ 405 xfs_trans_inode_alloc_buf(tp, fbuf); 406 if (version == 3) { 407 /* 408 * Mark the buffer as ordered so that they are 409 * not physically logged in the transaction but 410 * still tracked in the AIL as part of the 411 * transaction and pin the log appropriately. 412 */ 413 xfs_trans_ordered_buf(tp, fbuf); 414 } 415 } else { 416 fbuf->b_flags |= XBF_DONE; 417 xfs_buf_delwri_queue(fbuf, buffer_list); 418 xfs_buf_relse(fbuf); 419 } 420 } 421 return 0; 422 } 423 424 /* 425 * Align startino and allocmask for a recently allocated sparse chunk such that 426 * they are fit for insertion (or merge) into the on-disk inode btrees. 427 * 428 * Background: 429 * 430 * When enabled, sparse inode support increases the inode alignment from cluster 431 * size to inode chunk size. This means that the minimum range between two 432 * non-adjacent inode records in the inobt is large enough for a full inode 433 * record. This allows for cluster sized, cluster aligned block allocation 434 * without need to worry about whether the resulting inode record overlaps with 435 * another record in the tree. Without this basic rule, we would have to deal 436 * with the consequences of overlap by potentially undoing recent allocations in 437 * the inode allocation codepath. 438 * 439 * Because of this alignment rule (which is enforced on mount), there are two 440 * inobt possibilities for newly allocated sparse chunks. One is that the 441 * aligned inode record for the chunk covers a range of inodes not already 442 * covered in the inobt (i.e., it is safe to insert a new sparse record). The 443 * other is that a record already exists at the aligned startino that considers 444 * the newly allocated range as sparse. In the latter case, record content is 445 * merged in hope that sparse inode chunks fill to full chunks over time. 446 */ 447 STATIC void 448 xfs_align_sparse_ino( 449 struct xfs_mount *mp, 450 xfs_agino_t *startino, 451 uint16_t *allocmask) 452 { 453 xfs_agblock_t agbno; 454 xfs_agblock_t mod; 455 int offset; 456 457 agbno = XFS_AGINO_TO_AGBNO(mp, *startino); 458 mod = agbno % mp->m_sb.sb_inoalignmt; 459 if (!mod) 460 return; 461 462 /* calculate the inode offset and align startino */ 463 offset = XFS_AGB_TO_AGINO(mp, mod); 464 *startino -= offset; 465 466 /* 467 * Since startino has been aligned down, left shift allocmask such that 468 * it continues to represent the same physical inodes relative to the 469 * new startino. 470 */ 471 *allocmask <<= offset / XFS_INODES_PER_HOLEMASK_BIT; 472 } 473 474 /* 475 * Determine whether the source inode record can merge into the target. Both 476 * records must be sparse, the inode ranges must match and there must be no 477 * allocation overlap between the records. 478 */ 479 STATIC bool 480 __xfs_inobt_can_merge( 481 struct xfs_inobt_rec_incore *trec, /* tgt record */ 482 struct xfs_inobt_rec_incore *srec) /* src record */ 483 { 484 uint64_t talloc; 485 uint64_t salloc; 486 487 /* records must cover the same inode range */ 488 if (trec->ir_startino != srec->ir_startino) 489 return false; 490 491 /* both records must be sparse */ 492 if (!xfs_inobt_issparse(trec->ir_holemask) || 493 !xfs_inobt_issparse(srec->ir_holemask)) 494 return false; 495 496 /* both records must track some inodes */ 497 if (!trec->ir_count || !srec->ir_count) 498 return false; 499 500 /* can't exceed capacity of a full record */ 501 if (trec->ir_count + srec->ir_count > XFS_INODES_PER_CHUNK) 502 return false; 503 504 /* verify there is no allocation overlap */ 505 talloc = xfs_inobt_irec_to_allocmask(trec); 506 salloc = xfs_inobt_irec_to_allocmask(srec); 507 if (talloc & salloc) 508 return false; 509 510 return true; 511 } 512 513 /* 514 * Merge the source inode record into the target. The caller must call 515 * __xfs_inobt_can_merge() to ensure the merge is valid. 516 */ 517 STATIC void 518 __xfs_inobt_rec_merge( 519 struct xfs_inobt_rec_incore *trec, /* target */ 520 struct xfs_inobt_rec_incore *srec) /* src */ 521 { 522 ASSERT(trec->ir_startino == srec->ir_startino); 523 524 /* combine the counts */ 525 trec->ir_count += srec->ir_count; 526 trec->ir_freecount += srec->ir_freecount; 527 528 /* 529 * Merge the holemask and free mask. For both fields, 0 bits refer to 530 * allocated inodes. We combine the allocated ranges with bitwise AND. 531 */ 532 trec->ir_holemask &= srec->ir_holemask; 533 trec->ir_free &= srec->ir_free; 534 } 535 536 /* 537 * Insert a new sparse inode chunk into the associated inode allocation btree. 538 * The inode record for the sparse chunk is pre-aligned to a startino that 539 * should match any pre-existing sparse inode record in the tree. This allows 540 * sparse chunks to fill over time. 541 * 542 * If no preexisting record exists, the provided record is inserted. 543 * If there is a preexisting record, the provided record is merged with the 544 * existing record and updated in place. The merged record is returned in nrec. 545 * 546 * It is considered corruption if a merge is requested and not possible. Given 547 * the sparse inode alignment constraints, this should never happen. 548 */ 549 STATIC int 550 xfs_inobt_insert_sprec( 551 struct xfs_perag *pag, 552 struct xfs_trans *tp, 553 struct xfs_buf *agbp, 554 struct xfs_inobt_rec_incore *nrec) /* in/out: new/merged rec. */ 555 { 556 struct xfs_mount *mp = pag_mount(pag); 557 struct xfs_btree_cur *cur; 558 int error; 559 int i; 560 struct xfs_inobt_rec_incore rec; 561 562 cur = xfs_inobt_init_cursor(pag, tp, agbp); 563 564 /* the new record is pre-aligned so we know where to look */ 565 error = xfs_inobt_lookup(cur, nrec->ir_startino, XFS_LOOKUP_EQ, &i); 566 if (error) 567 goto error; 568 /* if nothing there, insert a new record and return */ 569 if (i == 0) { 570 error = xfs_inobt_insert_rec(cur, nrec->ir_holemask, 571 nrec->ir_count, nrec->ir_freecount, 572 nrec->ir_free, &i); 573 if (error) 574 goto error; 575 if (XFS_IS_CORRUPT(mp, i != 1)) { 576 xfs_btree_mark_sick(cur); 577 error = -EFSCORRUPTED; 578 goto error; 579 } 580 581 goto out; 582 } 583 584 /* 585 * A record exists at this startino. Merge the records. 586 */ 587 error = xfs_inobt_get_rec(cur, &rec, &i); 588 if (error) 589 goto error; 590 if (XFS_IS_CORRUPT(mp, i != 1)) { 591 xfs_btree_mark_sick(cur); 592 error = -EFSCORRUPTED; 593 goto error; 594 } 595 if (XFS_IS_CORRUPT(mp, rec.ir_startino != nrec->ir_startino)) { 596 xfs_btree_mark_sick(cur); 597 error = -EFSCORRUPTED; 598 goto error; 599 } 600 601 /* 602 * This should never fail. If we have coexisting records that 603 * cannot merge, something is seriously wrong. 604 */ 605 if (XFS_IS_CORRUPT(mp, !__xfs_inobt_can_merge(nrec, &rec))) { 606 xfs_btree_mark_sick(cur); 607 error = -EFSCORRUPTED; 608 goto error; 609 } 610 611 trace_xfs_irec_merge_pre(pag, &rec, nrec); 612 613 /* merge to nrec to output the updated record */ 614 __xfs_inobt_rec_merge(nrec, &rec); 615 616 trace_xfs_irec_merge_post(pag, nrec); 617 618 error = xfs_inobt_rec_check_count(mp, nrec); 619 if (error) 620 goto error; 621 622 error = xfs_inobt_update(cur, nrec); 623 if (error) 624 goto error; 625 626 out: 627 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 628 return 0; 629 error: 630 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 631 return error; 632 } 633 634 /* 635 * Insert a new sparse inode chunk into the free inode btree. The inode 636 * record for the sparse chunk is pre-aligned to a startino that should match 637 * any pre-existing sparse inode record in the tree. This allows sparse chunks 638 * to fill over time. 639 * 640 * The new record is always inserted, overwriting a pre-existing record if 641 * there is one. 642 */ 643 STATIC int 644 xfs_finobt_insert_sprec( 645 struct xfs_perag *pag, 646 struct xfs_trans *tp, 647 struct xfs_buf *agbp, 648 struct xfs_inobt_rec_incore *nrec) /* in/out: new rec. */ 649 { 650 struct xfs_mount *mp = pag_mount(pag); 651 struct xfs_btree_cur *cur; 652 int error; 653 int i; 654 655 cur = xfs_finobt_init_cursor(pag, tp, agbp); 656 657 /* the new record is pre-aligned so we know where to look */ 658 error = xfs_inobt_lookup(cur, nrec->ir_startino, XFS_LOOKUP_EQ, &i); 659 if (error) 660 goto error; 661 /* if nothing there, insert a new record and return */ 662 if (i == 0) { 663 error = xfs_inobt_insert_rec(cur, nrec->ir_holemask, 664 nrec->ir_count, nrec->ir_freecount, 665 nrec->ir_free, &i); 666 if (error) 667 goto error; 668 if (XFS_IS_CORRUPT(mp, i != 1)) { 669 xfs_btree_mark_sick(cur); 670 error = -EFSCORRUPTED; 671 goto error; 672 } 673 } else { 674 error = xfs_inobt_update(cur, nrec); 675 if (error) 676 goto error; 677 } 678 679 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 680 return 0; 681 error: 682 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 683 return error; 684 } 685 686 687 /* 688 * Allocate new inodes in the allocation group specified by agbp. Returns 0 if 689 * inodes were allocated in this AG; -EAGAIN if there was no space in this AG so 690 * the caller knows it can try another AG, a hard -ENOSPC when over the maximum 691 * inode count threshold, or the usual negative error code for other errors. 692 */ 693 STATIC int 694 xfs_ialloc_ag_alloc( 695 struct xfs_perag *pag, 696 struct xfs_trans *tp, 697 struct xfs_buf *agbp) 698 { 699 struct xfs_agi *agi; 700 struct xfs_alloc_arg args; 701 int error; 702 xfs_agino_t newino; /* new first inode's number */ 703 xfs_agino_t newlen; /* new number of inodes */ 704 int isaligned = 0; /* inode allocation at stripe */ 705 /* unit boundary */ 706 /* init. to full chunk */ 707 struct xfs_inobt_rec_incore rec; 708 struct xfs_ino_geometry *igeo = M_IGEO(tp->t_mountp); 709 uint16_t allocmask = (uint16_t) -1; 710 int do_sparse = 0; 711 712 memset(&args, 0, sizeof(args)); 713 args.tp = tp; 714 args.mp = tp->t_mountp; 715 args.fsbno = NULLFSBLOCK; 716 args.oinfo = XFS_RMAP_OINFO_INODES; 717 args.pag = pag; 718 719 #ifdef DEBUG 720 /* randomly do sparse inode allocations */ 721 if (xfs_has_sparseinodes(tp->t_mountp) && 722 igeo->ialloc_min_blks < igeo->ialloc_blks) 723 do_sparse = get_random_u32_below(2); 724 #endif 725 726 /* 727 * Locking will ensure that we don't have two callers in here 728 * at one time. 729 */ 730 newlen = igeo->ialloc_inos; 731 if (igeo->maxicount && 732 percpu_counter_read_positive(&args.mp->m_icount) + newlen > 733 igeo->maxicount) 734 return -ENOSPC; 735 args.minlen = args.maxlen = igeo->ialloc_blks; 736 /* 737 * First try to allocate inodes contiguous with the last-allocated 738 * chunk of inodes. If the filesystem is striped, this will fill 739 * an entire stripe unit with inodes. 740 */ 741 agi = agbp->b_addr; 742 newino = be32_to_cpu(agi->agi_newino); 743 args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) + 744 igeo->ialloc_blks; 745 if (do_sparse) 746 goto sparse_alloc; 747 if (likely(newino != NULLAGINO && 748 (args.agbno < be32_to_cpu(agi->agi_length)))) { 749 args.prod = 1; 750 751 /* 752 * We need to take into account alignment here to ensure that 753 * we don't modify the free list if we fail to have an exact 754 * block. If we don't have an exact match, and every oher 755 * attempt allocation attempt fails, we'll end up cancelling 756 * a dirty transaction and shutting down. 757 * 758 * For an exact allocation, alignment must be 1, 759 * however we need to take cluster alignment into account when 760 * fixing up the freelist. Use the minalignslop field to 761 * indicate that extra blocks might be required for alignment, 762 * but not to use them in the actual exact allocation. 763 */ 764 args.alignment = 1; 765 args.minalignslop = igeo->cluster_align - 1; 766 767 /* Allow space for the inode btree to split. */ 768 args.minleft = igeo->inobt_maxlevels; 769 error = xfs_alloc_vextent_exact_bno(&args, 770 xfs_agbno_to_fsb(pag, args.agbno)); 771 if (error) 772 return error; 773 774 /* 775 * This request might have dirtied the transaction if the AG can 776 * satisfy the request, but the exact block was not available. 777 * If the allocation did fail, subsequent requests will relax 778 * the exact agbno requirement and increase the alignment 779 * instead. It is critical that the total size of the request 780 * (len + alignment + slop) does not increase from this point 781 * on, so reset minalignslop to ensure it is not included in 782 * subsequent requests. 783 */ 784 args.minalignslop = 0; 785 } 786 787 if (unlikely(args.fsbno == NULLFSBLOCK)) { 788 /* 789 * Set the alignment for the allocation. 790 * If stripe alignment is turned on then align at stripe unit 791 * boundary. 792 * If the cluster size is smaller than a filesystem block 793 * then we're doing I/O for inodes in filesystem block size 794 * pieces, so don't need alignment anyway. 795 */ 796 isaligned = 0; 797 if (igeo->ialloc_align) { 798 ASSERT(!xfs_has_noalign(args.mp)); 799 args.alignment = args.mp->m_dalign; 800 isaligned = 1; 801 } else 802 args.alignment = igeo->cluster_align; 803 /* 804 * Allocate a fixed-size extent of inodes. 805 */ 806 args.prod = 1; 807 /* 808 * Allow space for the inode btree to split. 809 */ 810 args.minleft = igeo->inobt_maxlevels; 811 error = xfs_alloc_vextent_near_bno(&args, 812 xfs_agbno_to_fsb(pag, 813 be32_to_cpu(agi->agi_root))); 814 if (error) 815 return error; 816 } 817 818 /* 819 * If stripe alignment is turned on, then try again with cluster 820 * alignment. 821 */ 822 if (isaligned && args.fsbno == NULLFSBLOCK) { 823 args.alignment = igeo->cluster_align; 824 error = xfs_alloc_vextent_near_bno(&args, 825 xfs_agbno_to_fsb(pag, 826 be32_to_cpu(agi->agi_root))); 827 if (error) 828 return error; 829 } 830 831 /* 832 * Finally, try a sparse allocation if the filesystem supports it and 833 * the sparse allocation length is smaller than a full chunk. 834 */ 835 if (xfs_has_sparseinodes(args.mp) && 836 igeo->ialloc_min_blks < igeo->ialloc_blks && 837 args.fsbno == NULLFSBLOCK) { 838 sparse_alloc: 839 args.alignment = args.mp->m_sb.sb_spino_align; 840 args.prod = 1; 841 842 args.minlen = igeo->ialloc_min_blks; 843 args.maxlen = args.minlen; 844 845 /* 846 * The inode record will be aligned to full chunk size. We must 847 * prevent sparse allocation from AG boundaries that result in 848 * invalid inode records, such as records that start at agbno 0 849 * or extend beyond the AG. 850 * 851 * Set min agbno to the first aligned, non-zero agbno and max to 852 * the last aligned agbno that is at least one full chunk from 853 * the end of the AG. 854 */ 855 args.min_agbno = args.mp->m_sb.sb_inoalignmt; 856 args.max_agbno = round_down(args.mp->m_sb.sb_agblocks, 857 args.mp->m_sb.sb_inoalignmt) - 858 igeo->ialloc_blks; 859 860 error = xfs_alloc_vextent_near_bno(&args, 861 xfs_agbno_to_fsb(pag, 862 be32_to_cpu(agi->agi_root))); 863 if (error) 864 return error; 865 866 newlen = XFS_AGB_TO_AGINO(args.mp, args.len); 867 ASSERT(newlen <= XFS_INODES_PER_CHUNK); 868 allocmask = (1 << (newlen / XFS_INODES_PER_HOLEMASK_BIT)) - 1; 869 } 870 871 if (args.fsbno == NULLFSBLOCK) 872 return -EAGAIN; 873 874 ASSERT(args.len == args.minlen); 875 876 /* 877 * Stamp and write the inode buffers. 878 * 879 * Seed the new inode cluster with a random generation number. This 880 * prevents short-term reuse of generation numbers if a chunk is 881 * freed and then immediately reallocated. We use random numbers 882 * rather than a linear progression to prevent the next generation 883 * number from being easily guessable. 884 */ 885 error = xfs_ialloc_inode_init(args.mp, tp, NULL, newlen, pag_agno(pag), 886 args.agbno, args.len, get_random_u32()); 887 888 if (error) 889 return error; 890 /* 891 * Convert the results. 892 */ 893 newino = XFS_AGB_TO_AGINO(args.mp, args.agbno); 894 895 if (xfs_inobt_issparse(~allocmask)) { 896 /* 897 * We've allocated a sparse chunk. Align the startino and mask. 898 */ 899 xfs_align_sparse_ino(args.mp, &newino, &allocmask); 900 901 rec.ir_startino = newino; 902 rec.ir_holemask = ~allocmask; 903 rec.ir_count = newlen; 904 rec.ir_freecount = newlen; 905 rec.ir_free = XFS_INOBT_ALL_FREE; 906 907 /* 908 * Insert the sparse record into the inobt and allow for a merge 909 * if necessary. If a merge does occur, rec is updated to the 910 * merged record. 911 */ 912 error = xfs_inobt_insert_sprec(pag, tp, agbp, &rec); 913 if (error == -EFSCORRUPTED) { 914 xfs_alert(args.mp, 915 "invalid sparse inode record: ino 0x%llx holemask 0x%x count %u", 916 xfs_agino_to_ino(pag, rec.ir_startino), 917 rec.ir_holemask, rec.ir_count); 918 xfs_force_shutdown(args.mp, SHUTDOWN_CORRUPT_INCORE); 919 } 920 if (error) 921 return error; 922 923 /* 924 * We can't merge the part we've just allocated as for the inobt 925 * due to finobt semantics. The original record may or may not 926 * exist independent of whether physical inodes exist in this 927 * sparse chunk. 928 * 929 * We must update the finobt record based on the inobt record. 930 * rec contains the fully merged and up to date inobt record 931 * from the previous call. Set merge false to replace any 932 * existing record with this one. 933 */ 934 if (xfs_has_finobt(args.mp)) { 935 error = xfs_finobt_insert_sprec(pag, tp, agbp, &rec); 936 if (error) 937 return error; 938 } 939 } else { 940 /* full chunk - insert new records to both btrees */ 941 error = xfs_inobt_insert(pag, tp, agbp, newino, newlen, false); 942 if (error) 943 return error; 944 945 if (xfs_has_finobt(args.mp)) { 946 error = xfs_inobt_insert(pag, tp, agbp, newino, 947 newlen, true); 948 if (error) 949 return error; 950 } 951 } 952 953 /* 954 * Update AGI counts and newino. 955 */ 956 be32_add_cpu(&agi->agi_count, newlen); 957 be32_add_cpu(&agi->agi_freecount, newlen); 958 pag->pagi_freecount += newlen; 959 pag->pagi_count += newlen; 960 agi->agi_newino = cpu_to_be32(newino); 961 962 /* 963 * Log allocation group header fields 964 */ 965 xfs_ialloc_log_agi(tp, agbp, 966 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO); 967 /* 968 * Modify/log superblock values for inode count and inode free count. 969 */ 970 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen); 971 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen); 972 return 0; 973 } 974 975 /* 976 * Try to retrieve the next record to the left/right from the current one. 977 */ 978 STATIC int 979 xfs_ialloc_next_rec( 980 struct xfs_btree_cur *cur, 981 xfs_inobt_rec_incore_t *rec, 982 int *done, 983 int left) 984 { 985 int error; 986 int i; 987 988 if (left) 989 error = xfs_btree_decrement(cur, 0, &i); 990 else 991 error = xfs_btree_increment(cur, 0, &i); 992 993 if (error) 994 return error; 995 *done = !i; 996 if (i) { 997 error = xfs_inobt_get_rec(cur, rec, &i); 998 if (error) 999 return error; 1000 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) { 1001 xfs_btree_mark_sick(cur); 1002 return -EFSCORRUPTED; 1003 } 1004 } 1005 1006 return 0; 1007 } 1008 1009 STATIC int 1010 xfs_ialloc_get_rec( 1011 struct xfs_btree_cur *cur, 1012 xfs_agino_t agino, 1013 xfs_inobt_rec_incore_t *rec, 1014 int *done) 1015 { 1016 int error; 1017 int i; 1018 1019 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i); 1020 if (error) 1021 return error; 1022 *done = !i; 1023 if (i) { 1024 error = xfs_inobt_get_rec(cur, rec, &i); 1025 if (error) 1026 return error; 1027 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) { 1028 xfs_btree_mark_sick(cur); 1029 return -EFSCORRUPTED; 1030 } 1031 } 1032 1033 return 0; 1034 } 1035 1036 /* 1037 * Return the offset of the first free inode in the record. If the inode chunk 1038 * is sparsely allocated, we convert the record holemask to inode granularity 1039 * and mask off the unallocated regions from the inode free mask. 1040 */ 1041 STATIC int 1042 xfs_inobt_first_free_inode( 1043 struct xfs_inobt_rec_incore *rec) 1044 { 1045 xfs_inofree_t realfree; 1046 1047 /* if there are no holes, return the first available offset */ 1048 if (!xfs_inobt_issparse(rec->ir_holemask)) 1049 return xfs_lowbit64(rec->ir_free); 1050 1051 realfree = xfs_inobt_irec_to_allocmask(rec); 1052 realfree &= rec->ir_free; 1053 1054 return xfs_lowbit64(realfree); 1055 } 1056 1057 /* 1058 * If this AG has corrupt inodes, check if allocating this inode would fail 1059 * with corruption errors. Returns 0 if we're clear, or EAGAIN to try again 1060 * somewhere else. 1061 */ 1062 static int 1063 xfs_dialloc_check_ino( 1064 struct xfs_perag *pag, 1065 struct xfs_trans *tp, 1066 xfs_ino_t ino) 1067 { 1068 struct xfs_imap imap; 1069 struct xfs_buf *bp; 1070 int error; 1071 1072 error = xfs_imap(pag, tp, ino, &imap, 0); 1073 if (error) 1074 return -EAGAIN; 1075 1076 error = xfs_imap_to_bp(pag_mount(pag), tp, &imap, &bp); 1077 if (error) 1078 return -EAGAIN; 1079 1080 xfs_trans_brelse(tp, bp); 1081 return 0; 1082 } 1083 1084 /* 1085 * Allocate an inode using the inobt-only algorithm. 1086 */ 1087 STATIC int 1088 xfs_dialloc_ag_inobt( 1089 struct xfs_perag *pag, 1090 struct xfs_trans *tp, 1091 struct xfs_buf *agbp, 1092 xfs_ino_t parent, 1093 xfs_ino_t *inop) 1094 { 1095 struct xfs_mount *mp = tp->t_mountp; 1096 struct xfs_agi *agi = agbp->b_addr; 1097 xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent); 1098 xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent); 1099 struct xfs_btree_cur *cur, *tcur; 1100 struct xfs_inobt_rec_incore rec, trec; 1101 xfs_ino_t ino; 1102 int error; 1103 int offset; 1104 int i, j; 1105 int searchdistance = 10; 1106 1107 ASSERT(xfs_perag_initialised_agi(pag)); 1108 ASSERT(xfs_perag_allows_inodes(pag)); 1109 ASSERT(pag->pagi_freecount > 0); 1110 1111 restart_pagno: 1112 cur = xfs_inobt_init_cursor(pag, tp, agbp); 1113 /* 1114 * If pagino is 0 (this is the root inode allocation) use newino. 1115 * This must work because we've just allocated some. 1116 */ 1117 if (!pagino) 1118 pagino = be32_to_cpu(agi->agi_newino); 1119 1120 error = xfs_check_agi_freecount(cur); 1121 if (error) 1122 goto error0; 1123 1124 /* 1125 * If in the same AG as the parent, try to get near the parent. 1126 */ 1127 if (pagno == pag_agno(pag)) { 1128 int doneleft; /* done, to the left */ 1129 int doneright; /* done, to the right */ 1130 1131 error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i); 1132 if (error) 1133 goto error0; 1134 if (XFS_IS_CORRUPT(mp, i != 1)) { 1135 xfs_btree_mark_sick(cur); 1136 error = -EFSCORRUPTED; 1137 goto error0; 1138 } 1139 1140 error = xfs_inobt_get_rec(cur, &rec, &j); 1141 if (error) 1142 goto error0; 1143 if (XFS_IS_CORRUPT(mp, j != 1)) { 1144 xfs_btree_mark_sick(cur); 1145 error = -EFSCORRUPTED; 1146 goto error0; 1147 } 1148 1149 if (rec.ir_freecount > 0) { 1150 /* 1151 * Found a free inode in the same chunk 1152 * as the parent, done. 1153 */ 1154 goto alloc_inode; 1155 } 1156 1157 1158 /* 1159 * In the same AG as parent, but parent's chunk is full. 1160 */ 1161 1162 /* duplicate the cursor, search left & right simultaneously */ 1163 error = xfs_btree_dup_cursor(cur, &tcur); 1164 if (error) 1165 goto error0; 1166 1167 /* 1168 * Skip to last blocks looked up if same parent inode. 1169 */ 1170 if (pagino != NULLAGINO && 1171 pag->pagl_pagino == pagino && 1172 pag->pagl_leftrec != NULLAGINO && 1173 pag->pagl_rightrec != NULLAGINO) { 1174 error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec, 1175 &trec, &doneleft); 1176 if (error) 1177 goto error1; 1178 1179 error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec, 1180 &rec, &doneright); 1181 if (error) 1182 goto error1; 1183 } else { 1184 /* search left with tcur, back up 1 record */ 1185 error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1); 1186 if (error) 1187 goto error1; 1188 1189 /* search right with cur, go forward 1 record. */ 1190 error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0); 1191 if (error) 1192 goto error1; 1193 } 1194 1195 /* 1196 * Loop until we find an inode chunk with a free inode. 1197 */ 1198 while (--searchdistance > 0 && (!doneleft || !doneright)) { 1199 int useleft; /* using left inode chunk this time */ 1200 1201 /* figure out the closer block if both are valid. */ 1202 if (!doneleft && !doneright) { 1203 useleft = pagino - 1204 (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) < 1205 rec.ir_startino - pagino; 1206 } else { 1207 useleft = !doneleft; 1208 } 1209 1210 /* free inodes to the left? */ 1211 if (useleft && trec.ir_freecount) { 1212 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 1213 cur = tcur; 1214 1215 pag->pagl_leftrec = trec.ir_startino; 1216 pag->pagl_rightrec = rec.ir_startino; 1217 pag->pagl_pagino = pagino; 1218 rec = trec; 1219 goto alloc_inode; 1220 } 1221 1222 /* free inodes to the right? */ 1223 if (!useleft && rec.ir_freecount) { 1224 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); 1225 1226 pag->pagl_leftrec = trec.ir_startino; 1227 pag->pagl_rightrec = rec.ir_startino; 1228 pag->pagl_pagino = pagino; 1229 goto alloc_inode; 1230 } 1231 1232 /* get next record to check */ 1233 if (useleft) { 1234 error = xfs_ialloc_next_rec(tcur, &trec, 1235 &doneleft, 1); 1236 } else { 1237 error = xfs_ialloc_next_rec(cur, &rec, 1238 &doneright, 0); 1239 } 1240 if (error) 1241 goto error1; 1242 } 1243 1244 if (searchdistance <= 0) { 1245 /* 1246 * Not in range - save last search 1247 * location and allocate a new inode 1248 */ 1249 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); 1250 pag->pagl_leftrec = trec.ir_startino; 1251 pag->pagl_rightrec = rec.ir_startino; 1252 pag->pagl_pagino = pagino; 1253 1254 } else { 1255 /* 1256 * We've reached the end of the btree. because 1257 * we are only searching a small chunk of the 1258 * btree each search, there is obviously free 1259 * inodes closer to the parent inode than we 1260 * are now. restart the search again. 1261 */ 1262 pag->pagl_pagino = NULLAGINO; 1263 pag->pagl_leftrec = NULLAGINO; 1264 pag->pagl_rightrec = NULLAGINO; 1265 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); 1266 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 1267 goto restart_pagno; 1268 } 1269 } 1270 1271 /* 1272 * In a different AG from the parent. 1273 * See if the most recently allocated block has any free. 1274 */ 1275 if (agi->agi_newino != cpu_to_be32(NULLAGINO)) { 1276 error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino), 1277 XFS_LOOKUP_EQ, &i); 1278 if (error) 1279 goto error0; 1280 1281 if (i == 1) { 1282 error = xfs_inobt_get_rec(cur, &rec, &j); 1283 if (error) 1284 goto error0; 1285 1286 if (j == 1 && rec.ir_freecount > 0) { 1287 /* 1288 * The last chunk allocated in the group 1289 * still has a free inode. 1290 */ 1291 goto alloc_inode; 1292 } 1293 } 1294 } 1295 1296 /* 1297 * None left in the last group, search the whole AG 1298 */ 1299 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i); 1300 if (error) 1301 goto error0; 1302 if (XFS_IS_CORRUPT(mp, i != 1)) { 1303 xfs_btree_mark_sick(cur); 1304 error = -EFSCORRUPTED; 1305 goto error0; 1306 } 1307 1308 for (;;) { 1309 error = xfs_inobt_get_rec(cur, &rec, &i); 1310 if (error) 1311 goto error0; 1312 if (XFS_IS_CORRUPT(mp, i != 1)) { 1313 xfs_btree_mark_sick(cur); 1314 error = -EFSCORRUPTED; 1315 goto error0; 1316 } 1317 if (rec.ir_freecount > 0) 1318 break; 1319 error = xfs_btree_increment(cur, 0, &i); 1320 if (error) 1321 goto error0; 1322 if (XFS_IS_CORRUPT(mp, i != 1)) { 1323 xfs_btree_mark_sick(cur); 1324 error = -EFSCORRUPTED; 1325 goto error0; 1326 } 1327 } 1328 1329 alloc_inode: 1330 offset = xfs_inobt_first_free_inode(&rec); 1331 ASSERT(offset >= 0); 1332 ASSERT(offset < XFS_INODES_PER_CHUNK); 1333 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) % 1334 XFS_INODES_PER_CHUNK) == 0); 1335 ino = xfs_agino_to_ino(pag, rec.ir_startino + offset); 1336 1337 if (xfs_ag_has_sickness(pag, XFS_SICK_AG_INODES)) { 1338 error = xfs_dialloc_check_ino(pag, tp, ino); 1339 if (error) 1340 goto error0; 1341 } 1342 1343 rec.ir_free &= ~XFS_INOBT_MASK(offset); 1344 rec.ir_freecount--; 1345 error = xfs_inobt_update(cur, &rec); 1346 if (error) 1347 goto error0; 1348 be32_add_cpu(&agi->agi_freecount, -1); 1349 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); 1350 pag->pagi_freecount--; 1351 1352 error = xfs_check_agi_freecount(cur); 1353 if (error) 1354 goto error0; 1355 1356 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 1357 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1); 1358 *inop = ino; 1359 return 0; 1360 error1: 1361 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR); 1362 error0: 1363 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 1364 return error; 1365 } 1366 1367 /* 1368 * Use the free inode btree to allocate an inode based on distance from the 1369 * parent. Note that the provided cursor may be deleted and replaced. 1370 */ 1371 STATIC int 1372 xfs_dialloc_ag_finobt_near( 1373 xfs_agino_t pagino, 1374 struct xfs_btree_cur **ocur, 1375 struct xfs_inobt_rec_incore *rec) 1376 { 1377 struct xfs_btree_cur *lcur = *ocur; /* left search cursor */ 1378 struct xfs_btree_cur *rcur; /* right search cursor */ 1379 struct xfs_inobt_rec_incore rrec; 1380 int error; 1381 int i, j; 1382 1383 error = xfs_inobt_lookup(lcur, pagino, XFS_LOOKUP_LE, &i); 1384 if (error) 1385 return error; 1386 1387 if (i == 1) { 1388 error = xfs_inobt_get_rec(lcur, rec, &i); 1389 if (error) 1390 return error; 1391 if (XFS_IS_CORRUPT(lcur->bc_mp, i != 1)) { 1392 xfs_btree_mark_sick(lcur); 1393 return -EFSCORRUPTED; 1394 } 1395 1396 /* 1397 * See if we've landed in the parent inode record. The finobt 1398 * only tracks chunks with at least one free inode, so record 1399 * existence is enough. 1400 */ 1401 if (pagino >= rec->ir_startino && 1402 pagino < (rec->ir_startino + XFS_INODES_PER_CHUNK)) 1403 return 0; 1404 } 1405 1406 error = xfs_btree_dup_cursor(lcur, &rcur); 1407 if (error) 1408 return error; 1409 1410 error = xfs_inobt_lookup(rcur, pagino, XFS_LOOKUP_GE, &j); 1411 if (error) 1412 goto error_rcur; 1413 if (j == 1) { 1414 error = xfs_inobt_get_rec(rcur, &rrec, &j); 1415 if (error) 1416 goto error_rcur; 1417 if (XFS_IS_CORRUPT(lcur->bc_mp, j != 1)) { 1418 xfs_btree_mark_sick(lcur); 1419 error = -EFSCORRUPTED; 1420 goto error_rcur; 1421 } 1422 } 1423 1424 if (XFS_IS_CORRUPT(lcur->bc_mp, i != 1 && j != 1)) { 1425 xfs_btree_mark_sick(lcur); 1426 error = -EFSCORRUPTED; 1427 goto error_rcur; 1428 } 1429 if (i == 1 && j == 1) { 1430 /* 1431 * Both the left and right records are valid. Choose the closer 1432 * inode chunk to the target. 1433 */ 1434 if ((pagino - rec->ir_startino + XFS_INODES_PER_CHUNK - 1) > 1435 (rrec.ir_startino - pagino)) { 1436 *rec = rrec; 1437 xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR); 1438 *ocur = rcur; 1439 } else { 1440 xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR); 1441 } 1442 } else if (j == 1) { 1443 /* only the right record is valid */ 1444 *rec = rrec; 1445 xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR); 1446 *ocur = rcur; 1447 } else if (i == 1) { 1448 /* only the left record is valid */ 1449 xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR); 1450 } 1451 1452 return 0; 1453 1454 error_rcur: 1455 xfs_btree_del_cursor(rcur, XFS_BTREE_ERROR); 1456 return error; 1457 } 1458 1459 /* 1460 * Use the free inode btree to find a free inode based on a newino hint. If 1461 * the hint is NULL, find the first free inode in the AG. 1462 */ 1463 STATIC int 1464 xfs_dialloc_ag_finobt_newino( 1465 struct xfs_agi *agi, 1466 struct xfs_btree_cur *cur, 1467 struct xfs_inobt_rec_incore *rec) 1468 { 1469 int error; 1470 int i; 1471 1472 if (agi->agi_newino != cpu_to_be32(NULLAGINO)) { 1473 error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino), 1474 XFS_LOOKUP_EQ, &i); 1475 if (error) 1476 return error; 1477 if (i == 1) { 1478 error = xfs_inobt_get_rec(cur, rec, &i); 1479 if (error) 1480 return error; 1481 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) { 1482 xfs_btree_mark_sick(cur); 1483 return -EFSCORRUPTED; 1484 } 1485 return 0; 1486 } 1487 } 1488 1489 /* 1490 * Find the first inode available in the AG. 1491 */ 1492 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i); 1493 if (error) 1494 return error; 1495 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) { 1496 xfs_btree_mark_sick(cur); 1497 return -EFSCORRUPTED; 1498 } 1499 1500 error = xfs_inobt_get_rec(cur, rec, &i); 1501 if (error) 1502 return error; 1503 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) { 1504 xfs_btree_mark_sick(cur); 1505 return -EFSCORRUPTED; 1506 } 1507 1508 return 0; 1509 } 1510 1511 /* 1512 * Update the inobt based on a modification made to the finobt. Also ensure that 1513 * the records from both trees are equivalent post-modification. 1514 */ 1515 STATIC int 1516 xfs_dialloc_ag_update_inobt( 1517 struct xfs_btree_cur *cur, /* inobt cursor */ 1518 struct xfs_inobt_rec_incore *frec, /* finobt record */ 1519 int offset) /* inode offset */ 1520 { 1521 struct xfs_inobt_rec_incore rec; 1522 int error; 1523 int i; 1524 1525 error = xfs_inobt_lookup(cur, frec->ir_startino, XFS_LOOKUP_EQ, &i); 1526 if (error) 1527 return error; 1528 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) { 1529 xfs_btree_mark_sick(cur); 1530 return -EFSCORRUPTED; 1531 } 1532 1533 error = xfs_inobt_get_rec(cur, &rec, &i); 1534 if (error) 1535 return error; 1536 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) { 1537 xfs_btree_mark_sick(cur); 1538 return -EFSCORRUPTED; 1539 } 1540 ASSERT((XFS_AGINO_TO_OFFSET(cur->bc_mp, rec.ir_startino) % 1541 XFS_INODES_PER_CHUNK) == 0); 1542 1543 rec.ir_free &= ~XFS_INOBT_MASK(offset); 1544 rec.ir_freecount--; 1545 1546 if (XFS_IS_CORRUPT(cur->bc_mp, 1547 rec.ir_free != frec->ir_free || 1548 rec.ir_freecount != frec->ir_freecount)) { 1549 xfs_btree_mark_sick(cur); 1550 return -EFSCORRUPTED; 1551 } 1552 1553 return xfs_inobt_update(cur, &rec); 1554 } 1555 1556 /* 1557 * Allocate an inode using the free inode btree, if available. Otherwise, fall 1558 * back to the inobt search algorithm. 1559 * 1560 * The caller selected an AG for us, and made sure that free inodes are 1561 * available. 1562 */ 1563 static int 1564 xfs_dialloc_ag( 1565 struct xfs_perag *pag, 1566 struct xfs_trans *tp, 1567 struct xfs_buf *agbp, 1568 xfs_ino_t parent, 1569 xfs_ino_t *inop) 1570 { 1571 struct xfs_mount *mp = tp->t_mountp; 1572 struct xfs_agi *agi = agbp->b_addr; 1573 xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent); 1574 xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent); 1575 struct xfs_btree_cur *cur; /* finobt cursor */ 1576 struct xfs_btree_cur *icur; /* inobt cursor */ 1577 struct xfs_inobt_rec_incore rec; 1578 xfs_ino_t ino; 1579 int error; 1580 int offset; 1581 int i; 1582 1583 if (!xfs_has_finobt(mp)) 1584 return xfs_dialloc_ag_inobt(pag, tp, agbp, parent, inop); 1585 1586 /* 1587 * If pagino is 0 (this is the root inode allocation) use newino. 1588 * This must work because we've just allocated some. 1589 */ 1590 if (!pagino) 1591 pagino = be32_to_cpu(agi->agi_newino); 1592 1593 cur = xfs_finobt_init_cursor(pag, tp, agbp); 1594 1595 error = xfs_check_agi_freecount(cur); 1596 if (error) 1597 goto error_cur; 1598 1599 /* 1600 * The search algorithm depends on whether we're in the same AG as the 1601 * parent. If so, find the closest available inode to the parent. If 1602 * not, consider the agi hint or find the first free inode in the AG. 1603 */ 1604 if (pag_agno(pag) == pagno) 1605 error = xfs_dialloc_ag_finobt_near(pagino, &cur, &rec); 1606 else 1607 error = xfs_dialloc_ag_finobt_newino(agi, cur, &rec); 1608 if (error) 1609 goto error_cur; 1610 1611 offset = xfs_inobt_first_free_inode(&rec); 1612 ASSERT(offset >= 0); 1613 ASSERT(offset < XFS_INODES_PER_CHUNK); 1614 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) % 1615 XFS_INODES_PER_CHUNK) == 0); 1616 ino = xfs_agino_to_ino(pag, rec.ir_startino + offset); 1617 1618 if (xfs_ag_has_sickness(pag, XFS_SICK_AG_INODES)) { 1619 error = xfs_dialloc_check_ino(pag, tp, ino); 1620 if (error) 1621 goto error_cur; 1622 } 1623 1624 /* 1625 * Modify or remove the finobt record. 1626 */ 1627 rec.ir_free &= ~XFS_INOBT_MASK(offset); 1628 rec.ir_freecount--; 1629 if (rec.ir_freecount) 1630 error = xfs_inobt_update(cur, &rec); 1631 else 1632 error = xfs_btree_delete(cur, &i); 1633 if (error) 1634 goto error_cur; 1635 1636 /* 1637 * The finobt has now been updated appropriately. We haven't updated the 1638 * agi and superblock yet, so we can create an inobt cursor and validate 1639 * the original freecount. If all is well, make the equivalent update to 1640 * the inobt using the finobt record and offset information. 1641 */ 1642 icur = xfs_inobt_init_cursor(pag, tp, agbp); 1643 1644 error = xfs_check_agi_freecount(icur); 1645 if (error) 1646 goto error_icur; 1647 1648 error = xfs_dialloc_ag_update_inobt(icur, &rec, offset); 1649 if (error) 1650 goto error_icur; 1651 1652 /* 1653 * Both trees have now been updated. We must update the perag and 1654 * superblock before we can check the freecount for each btree. 1655 */ 1656 be32_add_cpu(&agi->agi_freecount, -1); 1657 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); 1658 pag->pagi_freecount--; 1659 1660 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1); 1661 1662 error = xfs_check_agi_freecount(icur); 1663 if (error) 1664 goto error_icur; 1665 error = xfs_check_agi_freecount(cur); 1666 if (error) 1667 goto error_icur; 1668 1669 xfs_btree_del_cursor(icur, XFS_BTREE_NOERROR); 1670 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 1671 *inop = ino; 1672 return 0; 1673 1674 error_icur: 1675 xfs_btree_del_cursor(icur, XFS_BTREE_ERROR); 1676 error_cur: 1677 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 1678 return error; 1679 } 1680 1681 static int 1682 xfs_dialloc_roll( 1683 struct xfs_trans **tpp, 1684 struct xfs_buf *agibp) 1685 { 1686 struct xfs_trans *tp = *tpp; 1687 struct xfs_dquot_acct *dqinfo; 1688 int error; 1689 1690 /* 1691 * Hold to on to the agibp across the commit so no other allocation can 1692 * come in and take the free inodes we just allocated for our caller. 1693 */ 1694 xfs_trans_bhold(tp, agibp); 1695 1696 /* 1697 * We want the quota changes to be associated with the next transaction, 1698 * NOT this one. So, detach the dqinfo from this and attach it to the 1699 * next transaction. 1700 */ 1701 dqinfo = tp->t_dqinfo; 1702 tp->t_dqinfo = NULL; 1703 1704 error = xfs_trans_roll(&tp); 1705 1706 /* Re-attach the quota info that we detached from prev trx. */ 1707 tp->t_dqinfo = dqinfo; 1708 1709 /* 1710 * Join the buffer even on commit error so that the buffer is released 1711 * when the caller cancels the transaction and doesn't have to handle 1712 * this error case specially. 1713 */ 1714 xfs_trans_bjoin(tp, agibp); 1715 *tpp = tp; 1716 return error; 1717 } 1718 1719 static bool 1720 xfs_dialloc_good_ag( 1721 struct xfs_perag *pag, 1722 struct xfs_trans *tp, 1723 umode_t mode, 1724 int flags, 1725 bool ok_alloc) 1726 { 1727 struct xfs_mount *mp = tp->t_mountp; 1728 xfs_extlen_t ineed; 1729 xfs_extlen_t longest = 0; 1730 int needspace; 1731 int error; 1732 1733 if (!pag) 1734 return false; 1735 if (!xfs_perag_allows_inodes(pag)) 1736 return false; 1737 1738 if (!xfs_perag_initialised_agi(pag)) { 1739 error = xfs_ialloc_read_agi(pag, tp, 0, NULL); 1740 if (error) 1741 return false; 1742 } 1743 1744 if (pag->pagi_freecount) 1745 return true; 1746 if (!ok_alloc) 1747 return false; 1748 1749 if (!xfs_perag_initialised_agf(pag)) { 1750 error = xfs_alloc_read_agf(pag, tp, flags, NULL); 1751 if (error) 1752 return false; 1753 } 1754 1755 /* 1756 * Check that there is enough free space for the file plus a chunk of 1757 * inodes if we need to allocate some. If this is the first pass across 1758 * the AGs, take into account the potential space needed for alignment 1759 * of inode chunks when checking the longest contiguous free space in 1760 * the AG - this prevents us from getting ENOSPC because we have free 1761 * space larger than ialloc_blks but alignment constraints prevent us 1762 * from using it. 1763 * 1764 * If we can't find an AG with space for full alignment slack to be 1765 * taken into account, we must be near ENOSPC in all AGs. Hence we 1766 * don't include alignment for the second pass and so if we fail 1767 * allocation due to alignment issues then it is most likely a real 1768 * ENOSPC condition. 1769 * 1770 * XXX(dgc): this calculation is now bogus thanks to the per-ag 1771 * reservations that xfs_alloc_fix_freelist() now does via 1772 * xfs_alloc_space_available(). When the AG fills up, pagf_freeblks will 1773 * be more than large enough for the check below to succeed, but 1774 * xfs_alloc_space_available() will fail because of the non-zero 1775 * metadata reservation and hence we won't actually be able to allocate 1776 * more inodes in this AG. We do soooo much unnecessary work near ENOSPC 1777 * because of this. 1778 */ 1779 ineed = M_IGEO(mp)->ialloc_min_blks; 1780 if (flags && ineed > 1) 1781 ineed += M_IGEO(mp)->cluster_align; 1782 longest = pag->pagf_longest; 1783 if (!longest) 1784 longest = pag->pagf_flcount > 0; 1785 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode); 1786 1787 if (pag->pagf_freeblks < needspace + ineed || longest < ineed) 1788 return false; 1789 return true; 1790 } 1791 1792 static int 1793 xfs_dialloc_try_ag( 1794 struct xfs_perag *pag, 1795 struct xfs_trans **tpp, 1796 xfs_ino_t parent, 1797 xfs_ino_t *new_ino, 1798 bool ok_alloc) 1799 { 1800 struct xfs_buf *agbp; 1801 xfs_ino_t ino; 1802 int error; 1803 1804 /* 1805 * Then read in the AGI buffer and recheck with the AGI buffer 1806 * lock held. 1807 */ 1808 error = xfs_ialloc_read_agi(pag, *tpp, 0, &agbp); 1809 if (error) 1810 return error; 1811 1812 if (!pag->pagi_freecount) { 1813 if (!ok_alloc) { 1814 error = -EAGAIN; 1815 goto out_release; 1816 } 1817 1818 error = xfs_ialloc_ag_alloc(pag, *tpp, agbp); 1819 if (error < 0) 1820 goto out_release; 1821 1822 /* 1823 * We successfully allocated space for an inode cluster in this 1824 * AG. Roll the transaction so that we can allocate one of the 1825 * new inodes. 1826 */ 1827 ASSERT(pag->pagi_freecount > 0); 1828 error = xfs_dialloc_roll(tpp, agbp); 1829 if (error) 1830 goto out_release; 1831 } 1832 1833 /* Allocate an inode in the found AG */ 1834 error = xfs_dialloc_ag(pag, *tpp, agbp, parent, &ino); 1835 if (!error) 1836 *new_ino = ino; 1837 return error; 1838 1839 out_release: 1840 xfs_trans_brelse(*tpp, agbp); 1841 return error; 1842 } 1843 1844 /* 1845 * Pick an AG for the new inode. 1846 * 1847 * Directories, symlinks, and regular files frequently allocate at least one 1848 * block, so factor that potential expansion when we examine whether an AG has 1849 * enough space for file creation. Try to keep metadata files all in the same 1850 * AG. 1851 */ 1852 static inline xfs_agnumber_t 1853 xfs_dialloc_pick_ag( 1854 struct xfs_mount *mp, 1855 struct xfs_inode *dp, 1856 umode_t mode) 1857 { 1858 xfs_agnumber_t start_agno; 1859 1860 if (!dp) 1861 return 0; 1862 if (xfs_is_metadir_inode(dp)) { 1863 if (mp->m_sb.sb_logstart) 1864 return XFS_FSB_TO_AGNO(mp, mp->m_sb.sb_logstart); 1865 return 0; 1866 } 1867 1868 if (S_ISDIR(mode)) 1869 return (atomic_inc_return(&mp->m_agirotor) - 1) % mp->m_maxagi; 1870 1871 start_agno = XFS_INO_TO_AGNO(mp, dp->i_ino); 1872 if (start_agno >= mp->m_maxagi) 1873 start_agno = 0; 1874 1875 return start_agno; 1876 } 1877 1878 /* 1879 * Allocate an on-disk inode. 1880 * 1881 * Mode is used to tell whether the new inode is a directory and hence where to 1882 * locate it. The on-disk inode that is allocated will be returned in @new_ino 1883 * on success, otherwise an error will be set to indicate the failure (e.g. 1884 * -ENOSPC). 1885 */ 1886 int 1887 xfs_dialloc( 1888 struct xfs_trans **tpp, 1889 const struct xfs_icreate_args *args, 1890 xfs_ino_t *new_ino) 1891 { 1892 struct xfs_mount *mp = (*tpp)->t_mountp; 1893 struct xfs_perag *pag; 1894 struct xfs_ino_geometry *igeo = M_IGEO(mp); 1895 xfs_ino_t ino = NULLFSINO; 1896 xfs_ino_t parent = args->pip ? args->pip->i_ino : 0; 1897 xfs_agnumber_t agno; 1898 xfs_agnumber_t start_agno; 1899 umode_t mode = args->mode & S_IFMT; 1900 bool ok_alloc = true; 1901 bool low_space = false; 1902 int flags; 1903 int error = 0; 1904 1905 start_agno = xfs_dialloc_pick_ag(mp, args->pip, mode); 1906 1907 /* 1908 * If we have already hit the ceiling of inode blocks then clear 1909 * ok_alloc so we scan all available agi structures for a free 1910 * inode. 1911 * 1912 * Read rough value of mp->m_icount by percpu_counter_read_positive, 1913 * which will sacrifice the preciseness but improve the performance. 1914 */ 1915 if (igeo->maxicount && 1916 percpu_counter_read_positive(&mp->m_icount) + igeo->ialloc_inos 1917 > igeo->maxicount) { 1918 ok_alloc = false; 1919 } 1920 1921 /* 1922 * If we are near to ENOSPC, we want to prefer allocation from AGs that 1923 * have free inodes in them rather than use up free space allocating new 1924 * inode chunks. Hence we turn off allocation for the first non-blocking 1925 * pass through the AGs if we are near ENOSPC to consume free inodes 1926 * that we can immediately allocate, but then we allow allocation on the 1927 * second pass if we fail to find an AG with free inodes in it. 1928 */ 1929 if (percpu_counter_read_positive(&mp->m_fdblocks) < 1930 mp->m_low_space[XFS_LOWSP_1_PCNT]) { 1931 ok_alloc = false; 1932 low_space = true; 1933 } 1934 1935 /* 1936 * Loop until we find an allocation group that either has free inodes 1937 * or in which we can allocate some inodes. Iterate through the 1938 * allocation groups upward, wrapping at the end. 1939 */ 1940 flags = XFS_ALLOC_FLAG_TRYLOCK; 1941 retry: 1942 for_each_perag_wrap_at(mp, start_agno, mp->m_maxagi, agno, pag) { 1943 if (xfs_dialloc_good_ag(pag, *tpp, mode, flags, ok_alloc)) { 1944 error = xfs_dialloc_try_ag(pag, tpp, parent, 1945 &ino, ok_alloc); 1946 if (error != -EAGAIN) 1947 break; 1948 error = 0; 1949 } 1950 1951 if (xfs_is_shutdown(mp)) { 1952 error = -EFSCORRUPTED; 1953 break; 1954 } 1955 } 1956 if (pag) 1957 xfs_perag_rele(pag); 1958 if (error) 1959 return error; 1960 if (ino == NULLFSINO) { 1961 if (flags) { 1962 flags = 0; 1963 if (low_space) 1964 ok_alloc = true; 1965 goto retry; 1966 } 1967 return -ENOSPC; 1968 } 1969 1970 /* 1971 * Protect against obviously corrupt allocation btree records. Later 1972 * xfs_iget checks will catch re-allocation of other active in-memory 1973 * and on-disk inodes. If we don't catch reallocating the parent inode 1974 * here we will deadlock in xfs_iget() so we have to do these checks 1975 * first. 1976 */ 1977 if (ino == parent || !xfs_verify_dir_ino(mp, ino)) { 1978 xfs_alert(mp, "Allocated a known in-use inode 0x%llx!", ino); 1979 xfs_agno_mark_sick(mp, XFS_INO_TO_AGNO(mp, ino), 1980 XFS_SICK_AG_INOBT); 1981 return -EFSCORRUPTED; 1982 } 1983 1984 *new_ino = ino; 1985 return 0; 1986 } 1987 1988 /* 1989 * Free the blocks of an inode chunk. We must consider that the inode chunk 1990 * might be sparse and only free the regions that are allocated as part of the 1991 * chunk. 1992 */ 1993 static int 1994 xfs_difree_inode_chunk( 1995 struct xfs_trans *tp, 1996 struct xfs_perag *pag, 1997 struct xfs_inobt_rec_incore *rec) 1998 { 1999 struct xfs_mount *mp = tp->t_mountp; 2000 xfs_agblock_t sagbno = XFS_AGINO_TO_AGBNO(mp, 2001 rec->ir_startino); 2002 int startidx, endidx; 2003 int nextbit; 2004 xfs_agblock_t agbno; 2005 int contigblk; 2006 DECLARE_BITMAP(holemask, XFS_INOBT_HOLEMASK_BITS); 2007 2008 if (!xfs_inobt_issparse(rec->ir_holemask)) { 2009 /* not sparse, calculate extent info directly */ 2010 return xfs_free_extent_later(tp, xfs_agbno_to_fsb(pag, sagbno), 2011 M_IGEO(mp)->ialloc_blks, &XFS_RMAP_OINFO_INODES, 2012 XFS_AG_RESV_NONE, 0); 2013 } 2014 2015 /* holemask is only 16-bits (fits in an unsigned long) */ 2016 ASSERT(sizeof(rec->ir_holemask) <= sizeof(holemask[0])); 2017 holemask[0] = rec->ir_holemask; 2018 2019 /* 2020 * Find contiguous ranges of zeroes (i.e., allocated regions) in the 2021 * holemask and convert the start/end index of each range to an extent. 2022 * We start with the start and end index both pointing at the first 0 in 2023 * the mask. 2024 */ 2025 startidx = endidx = find_first_zero_bit(holemask, 2026 XFS_INOBT_HOLEMASK_BITS); 2027 nextbit = startidx + 1; 2028 while (startidx < XFS_INOBT_HOLEMASK_BITS) { 2029 int error; 2030 2031 nextbit = find_next_zero_bit(holemask, XFS_INOBT_HOLEMASK_BITS, 2032 nextbit); 2033 /* 2034 * If the next zero bit is contiguous, update the end index of 2035 * the current range and continue. 2036 */ 2037 if (nextbit != XFS_INOBT_HOLEMASK_BITS && 2038 nextbit == endidx + 1) { 2039 endidx = nextbit; 2040 goto next; 2041 } 2042 2043 /* 2044 * nextbit is not contiguous with the current end index. Convert 2045 * the current start/end to an extent and add it to the free 2046 * list. 2047 */ 2048 agbno = sagbno + (startidx * XFS_INODES_PER_HOLEMASK_BIT) / 2049 mp->m_sb.sb_inopblock; 2050 contigblk = ((endidx - startidx + 1) * 2051 XFS_INODES_PER_HOLEMASK_BIT) / 2052 mp->m_sb.sb_inopblock; 2053 2054 ASSERT(agbno % mp->m_sb.sb_spino_align == 0); 2055 ASSERT(contigblk % mp->m_sb.sb_spino_align == 0); 2056 error = xfs_free_extent_later(tp, xfs_agbno_to_fsb(pag, agbno), 2057 contigblk, &XFS_RMAP_OINFO_INODES, 2058 XFS_AG_RESV_NONE, 0); 2059 if (error) 2060 return error; 2061 2062 /* reset range to current bit and carry on... */ 2063 startidx = endidx = nextbit; 2064 2065 next: 2066 nextbit++; 2067 } 2068 return 0; 2069 } 2070 2071 STATIC int 2072 xfs_difree_inobt( 2073 struct xfs_perag *pag, 2074 struct xfs_trans *tp, 2075 struct xfs_buf *agbp, 2076 xfs_agino_t agino, 2077 struct xfs_icluster *xic, 2078 struct xfs_inobt_rec_incore *orec) 2079 { 2080 struct xfs_mount *mp = pag_mount(pag); 2081 struct xfs_agi *agi = agbp->b_addr; 2082 struct xfs_btree_cur *cur; 2083 struct xfs_inobt_rec_incore rec; 2084 int ilen; 2085 int error; 2086 int i; 2087 int off; 2088 2089 ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC)); 2090 ASSERT(XFS_AGINO_TO_AGBNO(mp, agino) < be32_to_cpu(agi->agi_length)); 2091 2092 /* 2093 * Initialize the cursor. 2094 */ 2095 cur = xfs_inobt_init_cursor(pag, tp, agbp); 2096 2097 error = xfs_check_agi_freecount(cur); 2098 if (error) 2099 goto error0; 2100 2101 /* 2102 * Look for the entry describing this inode. 2103 */ 2104 if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) { 2105 xfs_warn(mp, "%s: xfs_inobt_lookup() returned error %d.", 2106 __func__, error); 2107 goto error0; 2108 } 2109 if (XFS_IS_CORRUPT(mp, i != 1)) { 2110 xfs_btree_mark_sick(cur); 2111 error = -EFSCORRUPTED; 2112 goto error0; 2113 } 2114 error = xfs_inobt_get_rec(cur, &rec, &i); 2115 if (error) { 2116 xfs_warn(mp, "%s: xfs_inobt_get_rec() returned error %d.", 2117 __func__, error); 2118 goto error0; 2119 } 2120 if (XFS_IS_CORRUPT(mp, i != 1)) { 2121 xfs_btree_mark_sick(cur); 2122 error = -EFSCORRUPTED; 2123 goto error0; 2124 } 2125 /* 2126 * Get the offset in the inode chunk. 2127 */ 2128 off = agino - rec.ir_startino; 2129 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK); 2130 ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off))); 2131 /* 2132 * Mark the inode free & increment the count. 2133 */ 2134 rec.ir_free |= XFS_INOBT_MASK(off); 2135 rec.ir_freecount++; 2136 2137 /* 2138 * When an inode chunk is free, it becomes eligible for removal. Don't 2139 * remove the chunk if the block size is large enough for multiple inode 2140 * chunks (that might not be free). 2141 */ 2142 if (!xfs_has_ikeep(mp) && rec.ir_free == XFS_INOBT_ALL_FREE && 2143 mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK) { 2144 xic->deleted = true; 2145 xic->first_ino = xfs_agino_to_ino(pag, rec.ir_startino); 2146 xic->alloc = xfs_inobt_irec_to_allocmask(&rec); 2147 2148 /* 2149 * Remove the inode cluster from the AGI B+Tree, adjust the 2150 * AGI and Superblock inode counts, and mark the disk space 2151 * to be freed when the transaction is committed. 2152 */ 2153 ilen = rec.ir_freecount; 2154 be32_add_cpu(&agi->agi_count, -ilen); 2155 be32_add_cpu(&agi->agi_freecount, -(ilen - 1)); 2156 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT); 2157 pag->pagi_freecount -= ilen - 1; 2158 pag->pagi_count -= ilen; 2159 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen); 2160 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1)); 2161 2162 if ((error = xfs_btree_delete(cur, &i))) { 2163 xfs_warn(mp, "%s: xfs_btree_delete returned error %d.", 2164 __func__, error); 2165 goto error0; 2166 } 2167 2168 error = xfs_difree_inode_chunk(tp, pag, &rec); 2169 if (error) 2170 goto error0; 2171 } else { 2172 xic->deleted = false; 2173 2174 error = xfs_inobt_update(cur, &rec); 2175 if (error) { 2176 xfs_warn(mp, "%s: xfs_inobt_update returned error %d.", 2177 __func__, error); 2178 goto error0; 2179 } 2180 2181 /* 2182 * Change the inode free counts and log the ag/sb changes. 2183 */ 2184 be32_add_cpu(&agi->agi_freecount, 1); 2185 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); 2186 pag->pagi_freecount++; 2187 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1); 2188 } 2189 2190 error = xfs_check_agi_freecount(cur); 2191 if (error) 2192 goto error0; 2193 2194 *orec = rec; 2195 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 2196 return 0; 2197 2198 error0: 2199 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 2200 return error; 2201 } 2202 2203 /* 2204 * Free an inode in the free inode btree. 2205 */ 2206 STATIC int 2207 xfs_difree_finobt( 2208 struct xfs_perag *pag, 2209 struct xfs_trans *tp, 2210 struct xfs_buf *agbp, 2211 xfs_agino_t agino, 2212 struct xfs_inobt_rec_incore *ibtrec) /* inobt record */ 2213 { 2214 struct xfs_mount *mp = pag_mount(pag); 2215 struct xfs_btree_cur *cur; 2216 struct xfs_inobt_rec_incore rec; 2217 int offset = agino - ibtrec->ir_startino; 2218 int error; 2219 int i; 2220 2221 cur = xfs_finobt_init_cursor(pag, tp, agbp); 2222 2223 error = xfs_inobt_lookup(cur, ibtrec->ir_startino, XFS_LOOKUP_EQ, &i); 2224 if (error) 2225 goto error; 2226 if (i == 0) { 2227 /* 2228 * If the record does not exist in the finobt, we must have just 2229 * freed an inode in a previously fully allocated chunk. If not, 2230 * something is out of sync. 2231 */ 2232 if (XFS_IS_CORRUPT(mp, ibtrec->ir_freecount != 1)) { 2233 xfs_btree_mark_sick(cur); 2234 error = -EFSCORRUPTED; 2235 goto error; 2236 } 2237 2238 error = xfs_inobt_insert_rec(cur, ibtrec->ir_holemask, 2239 ibtrec->ir_count, 2240 ibtrec->ir_freecount, 2241 ibtrec->ir_free, &i); 2242 if (error) 2243 goto error; 2244 ASSERT(i == 1); 2245 2246 goto out; 2247 } 2248 2249 /* 2250 * Read and update the existing record. We could just copy the ibtrec 2251 * across here, but that would defeat the purpose of having redundant 2252 * metadata. By making the modifications independently, we can catch 2253 * corruptions that we wouldn't see if we just copied from one record 2254 * to another. 2255 */ 2256 error = xfs_inobt_get_rec(cur, &rec, &i); 2257 if (error) 2258 goto error; 2259 if (XFS_IS_CORRUPT(mp, i != 1)) { 2260 xfs_btree_mark_sick(cur); 2261 error = -EFSCORRUPTED; 2262 goto error; 2263 } 2264 2265 rec.ir_free |= XFS_INOBT_MASK(offset); 2266 rec.ir_freecount++; 2267 2268 if (XFS_IS_CORRUPT(mp, 2269 rec.ir_free != ibtrec->ir_free || 2270 rec.ir_freecount != ibtrec->ir_freecount)) { 2271 xfs_btree_mark_sick(cur); 2272 error = -EFSCORRUPTED; 2273 goto error; 2274 } 2275 2276 /* 2277 * The content of inobt records should always match between the inobt 2278 * and finobt. The lifecycle of records in the finobt is different from 2279 * the inobt in that the finobt only tracks records with at least one 2280 * free inode. Hence, if all of the inodes are free and we aren't 2281 * keeping inode chunks permanently on disk, remove the record. 2282 * Otherwise, update the record with the new information. 2283 * 2284 * Note that we currently can't free chunks when the block size is large 2285 * enough for multiple chunks. Leave the finobt record to remain in sync 2286 * with the inobt. 2287 */ 2288 if (!xfs_has_ikeep(mp) && rec.ir_free == XFS_INOBT_ALL_FREE && 2289 mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK) { 2290 error = xfs_btree_delete(cur, &i); 2291 if (error) 2292 goto error; 2293 ASSERT(i == 1); 2294 } else { 2295 error = xfs_inobt_update(cur, &rec); 2296 if (error) 2297 goto error; 2298 } 2299 2300 out: 2301 error = xfs_check_agi_freecount(cur); 2302 if (error) 2303 goto error; 2304 2305 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); 2306 return 0; 2307 2308 error: 2309 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); 2310 return error; 2311 } 2312 2313 /* 2314 * Free disk inode. Carefully avoids touching the incore inode, all 2315 * manipulations incore are the caller's responsibility. 2316 * The on-disk inode is not changed by this operation, only the 2317 * btree (free inode mask) is changed. 2318 */ 2319 int 2320 xfs_difree( 2321 struct xfs_trans *tp, 2322 struct xfs_perag *pag, 2323 xfs_ino_t inode, 2324 struct xfs_icluster *xic) 2325 { 2326 /* REFERENCED */ 2327 xfs_agblock_t agbno; /* block number containing inode */ 2328 struct xfs_buf *agbp; /* buffer for allocation group header */ 2329 xfs_agino_t agino; /* allocation group inode number */ 2330 int error; /* error return value */ 2331 struct xfs_mount *mp = tp->t_mountp; 2332 struct xfs_inobt_rec_incore rec;/* btree record */ 2333 2334 /* 2335 * Break up inode number into its components. 2336 */ 2337 if (pag_agno(pag) != XFS_INO_TO_AGNO(mp, inode)) { 2338 xfs_warn(mp, "%s: agno != pag_agno(pag) (%d != %d).", 2339 __func__, XFS_INO_TO_AGNO(mp, inode), pag_agno(pag)); 2340 ASSERT(0); 2341 return -EINVAL; 2342 } 2343 agino = XFS_INO_TO_AGINO(mp, inode); 2344 if (inode != xfs_agino_to_ino(pag, agino)) { 2345 xfs_warn(mp, "%s: inode != xfs_agino_to_ino() (%llu != %llu).", 2346 __func__, (unsigned long long)inode, 2347 (unsigned long long)xfs_agino_to_ino(pag, agino)); 2348 ASSERT(0); 2349 return -EINVAL; 2350 } 2351 agbno = XFS_AGINO_TO_AGBNO(mp, agino); 2352 if (agbno >= mp->m_sb.sb_agblocks) { 2353 xfs_warn(mp, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).", 2354 __func__, agbno, mp->m_sb.sb_agblocks); 2355 ASSERT(0); 2356 return -EINVAL; 2357 } 2358 /* 2359 * Get the allocation group header. 2360 */ 2361 error = xfs_ialloc_read_agi(pag, tp, 0, &agbp); 2362 if (error) { 2363 xfs_warn(mp, "%s: xfs_ialloc_read_agi() returned error %d.", 2364 __func__, error); 2365 return error; 2366 } 2367 2368 /* 2369 * Fix up the inode allocation btree. 2370 */ 2371 error = xfs_difree_inobt(pag, tp, agbp, agino, xic, &rec); 2372 if (error) 2373 goto error0; 2374 2375 /* 2376 * Fix up the free inode btree. 2377 */ 2378 if (xfs_has_finobt(mp)) { 2379 error = xfs_difree_finobt(pag, tp, agbp, agino, &rec); 2380 if (error) 2381 goto error0; 2382 } 2383 2384 return 0; 2385 2386 error0: 2387 return error; 2388 } 2389 2390 STATIC int 2391 xfs_imap_lookup( 2392 struct xfs_perag *pag, 2393 struct xfs_trans *tp, 2394 xfs_agino_t agino, 2395 xfs_agblock_t agbno, 2396 xfs_agblock_t *chunk_agbno, 2397 xfs_agblock_t *offset_agbno, 2398 int flags) 2399 { 2400 struct xfs_mount *mp = pag_mount(pag); 2401 struct xfs_inobt_rec_incore rec; 2402 struct xfs_btree_cur *cur; 2403 struct xfs_buf *agbp; 2404 int error; 2405 int i; 2406 2407 error = xfs_ialloc_read_agi(pag, tp, 0, &agbp); 2408 if (error) { 2409 xfs_alert(mp, 2410 "%s: xfs_ialloc_read_agi() returned error %d, agno %d", 2411 __func__, error, pag_agno(pag)); 2412 return error; 2413 } 2414 2415 /* 2416 * Lookup the inode record for the given agino. If the record cannot be 2417 * found, then it's an invalid inode number and we should abort. Once 2418 * we have a record, we need to ensure it contains the inode number 2419 * we are looking up. 2420 */ 2421 cur = xfs_inobt_init_cursor(pag, tp, agbp); 2422 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i); 2423 if (!error) { 2424 if (i) 2425 error = xfs_inobt_get_rec(cur, &rec, &i); 2426 if (!error && i == 0) 2427 error = -EINVAL; 2428 } 2429 2430 xfs_trans_brelse(tp, agbp); 2431 xfs_btree_del_cursor(cur, error); 2432 if (error) 2433 return error; 2434 2435 /* check that the returned record contains the required inode */ 2436 if (rec.ir_startino > agino || 2437 rec.ir_startino + M_IGEO(mp)->ialloc_inos <= agino) 2438 return -EINVAL; 2439 2440 /* for untrusted inodes check it is allocated first */ 2441 if ((flags & XFS_IGET_UNTRUSTED) && 2442 (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino))) 2443 return -EINVAL; 2444 2445 *chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino); 2446 *offset_agbno = agbno - *chunk_agbno; 2447 return 0; 2448 } 2449 2450 /* 2451 * Return the location of the inode in imap, for mapping it into a buffer. 2452 */ 2453 int 2454 xfs_imap( 2455 struct xfs_perag *pag, 2456 struct xfs_trans *tp, 2457 xfs_ino_t ino, /* inode to locate */ 2458 struct xfs_imap *imap, /* location map structure */ 2459 uint flags) /* flags for inode btree lookup */ 2460 { 2461 struct xfs_mount *mp = pag_mount(pag); 2462 xfs_agblock_t agbno; /* block number of inode in the alloc group */ 2463 xfs_agino_t agino; /* inode number within alloc group */ 2464 xfs_agblock_t chunk_agbno; /* first block in inode chunk */ 2465 xfs_agblock_t cluster_agbno; /* first block in inode cluster */ 2466 int error; /* error code */ 2467 int offset; /* index of inode in its buffer */ 2468 xfs_agblock_t offset_agbno; /* blks from chunk start to inode */ 2469 2470 ASSERT(ino != NULLFSINO); 2471 2472 /* 2473 * Split up the inode number into its parts. 2474 */ 2475 agino = XFS_INO_TO_AGINO(mp, ino); 2476 agbno = XFS_AGINO_TO_AGBNO(mp, agino); 2477 if (agbno >= mp->m_sb.sb_agblocks || 2478 ino != xfs_agino_to_ino(pag, agino)) { 2479 error = -EINVAL; 2480 #ifdef DEBUG 2481 /* 2482 * Don't output diagnostic information for untrusted inodes 2483 * as they can be invalid without implying corruption. 2484 */ 2485 if (flags & XFS_IGET_UNTRUSTED) 2486 return error; 2487 if (agbno >= mp->m_sb.sb_agblocks) { 2488 xfs_alert(mp, 2489 "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)", 2490 __func__, (unsigned long long)agbno, 2491 (unsigned long)mp->m_sb.sb_agblocks); 2492 } 2493 if (ino != xfs_agino_to_ino(pag, agino)) { 2494 xfs_alert(mp, 2495 "%s: ino (0x%llx) != xfs_agino_to_ino() (0x%llx)", 2496 __func__, ino, 2497 xfs_agino_to_ino(pag, agino)); 2498 } 2499 xfs_stack_trace(); 2500 #endif /* DEBUG */ 2501 return error; 2502 } 2503 2504 /* 2505 * For bulkstat and handle lookups, we have an untrusted inode number 2506 * that we have to verify is valid. We cannot do this just by reading 2507 * the inode buffer as it may have been unlinked and removed leaving 2508 * inodes in stale state on disk. Hence we have to do a btree lookup 2509 * in all cases where an untrusted inode number is passed. 2510 */ 2511 if (flags & XFS_IGET_UNTRUSTED) { 2512 error = xfs_imap_lookup(pag, tp, agino, agbno, 2513 &chunk_agbno, &offset_agbno, flags); 2514 if (error) 2515 return error; 2516 goto out_map; 2517 } 2518 2519 /* 2520 * If the inode cluster size is the same as the blocksize or 2521 * smaller we get to the buffer by simple arithmetics. 2522 */ 2523 if (M_IGEO(mp)->blocks_per_cluster == 1) { 2524 offset = XFS_INO_TO_OFFSET(mp, ino); 2525 ASSERT(offset < mp->m_sb.sb_inopblock); 2526 2527 imap->im_blkno = xfs_agbno_to_daddr(pag, agbno); 2528 imap->im_len = XFS_FSB_TO_BB(mp, 1); 2529 imap->im_boffset = (unsigned short)(offset << 2530 mp->m_sb.sb_inodelog); 2531 return 0; 2532 } 2533 2534 /* 2535 * If the inode chunks are aligned then use simple maths to 2536 * find the location. Otherwise we have to do a btree 2537 * lookup to find the location. 2538 */ 2539 if (M_IGEO(mp)->inoalign_mask) { 2540 offset_agbno = agbno & M_IGEO(mp)->inoalign_mask; 2541 chunk_agbno = agbno - offset_agbno; 2542 } else { 2543 error = xfs_imap_lookup(pag, tp, agino, agbno, 2544 &chunk_agbno, &offset_agbno, flags); 2545 if (error) 2546 return error; 2547 } 2548 2549 out_map: 2550 ASSERT(agbno >= chunk_agbno); 2551 cluster_agbno = chunk_agbno + 2552 ((offset_agbno / M_IGEO(mp)->blocks_per_cluster) * 2553 M_IGEO(mp)->blocks_per_cluster); 2554 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) + 2555 XFS_INO_TO_OFFSET(mp, ino); 2556 2557 imap->im_blkno = xfs_agbno_to_daddr(pag, cluster_agbno); 2558 imap->im_len = XFS_FSB_TO_BB(mp, M_IGEO(mp)->blocks_per_cluster); 2559 imap->im_boffset = (unsigned short)(offset << mp->m_sb.sb_inodelog); 2560 2561 /* 2562 * If the inode number maps to a block outside the bounds 2563 * of the file system then return NULL rather than calling 2564 * read_buf and panicing when we get an error from the 2565 * driver. 2566 */ 2567 if ((imap->im_blkno + imap->im_len) > 2568 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) { 2569 xfs_alert(mp, 2570 "%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)", 2571 __func__, (unsigned long long) imap->im_blkno, 2572 (unsigned long long) imap->im_len, 2573 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)); 2574 return -EINVAL; 2575 } 2576 return 0; 2577 } 2578 2579 /* 2580 * Log specified fields for the ag hdr (inode section). The growth of the agi 2581 * structure over time requires that we interpret the buffer as two logical 2582 * regions delineated by the end of the unlinked list. This is due to the size 2583 * of the hash table and its location in the middle of the agi. 2584 * 2585 * For example, a request to log a field before agi_unlinked and a field after 2586 * agi_unlinked could cause us to log the entire hash table and use an excessive 2587 * amount of log space. To avoid this behavior, log the region up through 2588 * agi_unlinked in one call and the region after agi_unlinked through the end of 2589 * the structure in another. 2590 */ 2591 void 2592 xfs_ialloc_log_agi( 2593 struct xfs_trans *tp, 2594 struct xfs_buf *bp, 2595 uint32_t fields) 2596 { 2597 int first; /* first byte number */ 2598 int last; /* last byte number */ 2599 static const short offsets[] = { /* field starting offsets */ 2600 /* keep in sync with bit definitions */ 2601 offsetof(xfs_agi_t, agi_magicnum), 2602 offsetof(xfs_agi_t, agi_versionnum), 2603 offsetof(xfs_agi_t, agi_seqno), 2604 offsetof(xfs_agi_t, agi_length), 2605 offsetof(xfs_agi_t, agi_count), 2606 offsetof(xfs_agi_t, agi_root), 2607 offsetof(xfs_agi_t, agi_level), 2608 offsetof(xfs_agi_t, agi_freecount), 2609 offsetof(xfs_agi_t, agi_newino), 2610 offsetof(xfs_agi_t, agi_dirino), 2611 offsetof(xfs_agi_t, agi_unlinked), 2612 offsetof(xfs_agi_t, agi_free_root), 2613 offsetof(xfs_agi_t, agi_free_level), 2614 offsetof(xfs_agi_t, agi_iblocks), 2615 sizeof(xfs_agi_t) 2616 }; 2617 #ifdef DEBUG 2618 struct xfs_agi *agi = bp->b_addr; 2619 2620 ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC)); 2621 #endif 2622 2623 /* 2624 * Compute byte offsets for the first and last fields in the first 2625 * region and log the agi buffer. This only logs up through 2626 * agi_unlinked. 2627 */ 2628 if (fields & XFS_AGI_ALL_BITS_R1) { 2629 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R1, 2630 &first, &last); 2631 xfs_trans_log_buf(tp, bp, first, last); 2632 } 2633 2634 /* 2635 * Mask off the bits in the first region and calculate the first and 2636 * last field offsets for any bits in the second region. 2637 */ 2638 fields &= ~XFS_AGI_ALL_BITS_R1; 2639 if (fields) { 2640 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R2, 2641 &first, &last); 2642 xfs_trans_log_buf(tp, bp, first, last); 2643 } 2644 } 2645 2646 static xfs_failaddr_t 2647 xfs_agi_verify( 2648 struct xfs_buf *bp) 2649 { 2650 struct xfs_mount *mp = bp->b_mount; 2651 struct xfs_agi *agi = bp->b_addr; 2652 xfs_failaddr_t fa; 2653 uint32_t agi_seqno = be32_to_cpu(agi->agi_seqno); 2654 uint32_t agi_length = be32_to_cpu(agi->agi_length); 2655 int i; 2656 2657 if (xfs_has_crc(mp)) { 2658 if (!uuid_equal(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid)) 2659 return __this_address; 2660 if (!xfs_log_check_lsn(mp, be64_to_cpu(agi->agi_lsn))) 2661 return __this_address; 2662 } 2663 2664 /* 2665 * Validate the magic number of the agi block. 2666 */ 2667 if (!xfs_verify_magic(bp, agi->agi_magicnum)) 2668 return __this_address; 2669 if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum))) 2670 return __this_address; 2671 2672 fa = xfs_validate_ag_length(bp, agi_seqno, agi_length); 2673 if (fa) 2674 return fa; 2675 2676 if (be32_to_cpu(agi->agi_level) < 1 || 2677 be32_to_cpu(agi->agi_level) > M_IGEO(mp)->inobt_maxlevels) 2678 return __this_address; 2679 2680 if (xfs_has_finobt(mp) && 2681 (be32_to_cpu(agi->agi_free_level) < 1 || 2682 be32_to_cpu(agi->agi_free_level) > M_IGEO(mp)->inobt_maxlevels)) 2683 return __this_address; 2684 2685 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) { 2686 if (agi->agi_unlinked[i] == cpu_to_be32(NULLAGINO)) 2687 continue; 2688 if (!xfs_verify_ino(mp, be32_to_cpu(agi->agi_unlinked[i]))) 2689 return __this_address; 2690 } 2691 2692 return NULL; 2693 } 2694 2695 static void 2696 xfs_agi_read_verify( 2697 struct xfs_buf *bp) 2698 { 2699 struct xfs_mount *mp = bp->b_mount; 2700 xfs_failaddr_t fa; 2701 2702 if (xfs_has_crc(mp) && 2703 !xfs_buf_verify_cksum(bp, XFS_AGI_CRC_OFF)) 2704 xfs_verifier_error(bp, -EFSBADCRC, __this_address); 2705 else { 2706 fa = xfs_agi_verify(bp); 2707 if (XFS_TEST_ERROR(fa, mp, XFS_ERRTAG_IALLOC_READ_AGI)) 2708 xfs_verifier_error(bp, -EFSCORRUPTED, fa); 2709 } 2710 } 2711 2712 static void 2713 xfs_agi_write_verify( 2714 struct xfs_buf *bp) 2715 { 2716 struct xfs_mount *mp = bp->b_mount; 2717 struct xfs_buf_log_item *bip = bp->b_log_item; 2718 struct xfs_agi *agi = bp->b_addr; 2719 xfs_failaddr_t fa; 2720 2721 fa = xfs_agi_verify(bp); 2722 if (fa) { 2723 xfs_verifier_error(bp, -EFSCORRUPTED, fa); 2724 return; 2725 } 2726 2727 if (!xfs_has_crc(mp)) 2728 return; 2729 2730 if (bip) 2731 agi->agi_lsn = cpu_to_be64(bip->bli_item.li_lsn); 2732 xfs_buf_update_cksum(bp, XFS_AGI_CRC_OFF); 2733 } 2734 2735 const struct xfs_buf_ops xfs_agi_buf_ops = { 2736 .name = "xfs_agi", 2737 .magic = { cpu_to_be32(XFS_AGI_MAGIC), cpu_to_be32(XFS_AGI_MAGIC) }, 2738 .verify_read = xfs_agi_read_verify, 2739 .verify_write = xfs_agi_write_verify, 2740 .verify_struct = xfs_agi_verify, 2741 }; 2742 2743 /* 2744 * Read in the allocation group header (inode allocation section) 2745 */ 2746 int 2747 xfs_read_agi( 2748 struct xfs_perag *pag, 2749 struct xfs_trans *tp, 2750 xfs_buf_flags_t flags, 2751 struct xfs_buf **agibpp) 2752 { 2753 struct xfs_mount *mp = pag_mount(pag); 2754 int error; 2755 2756 trace_xfs_read_agi(pag); 2757 2758 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, 2759 XFS_AG_DADDR(mp, pag_agno(pag), XFS_AGI_DADDR(mp)), 2760 XFS_FSS_TO_BB(mp, 1), flags, agibpp, &xfs_agi_buf_ops); 2761 if (xfs_metadata_is_sick(error)) 2762 xfs_ag_mark_sick(pag, XFS_SICK_AG_AGI); 2763 if (error) 2764 return error; 2765 if (tp) 2766 xfs_trans_buf_set_type(tp, *agibpp, XFS_BLFT_AGI_BUF); 2767 2768 xfs_buf_set_ref(*agibpp, XFS_AGI_REF); 2769 return 0; 2770 } 2771 2772 /* 2773 * Read in the agi and initialise the per-ag data. If the caller supplies a 2774 * @agibpp, return the locked AGI buffer to them, otherwise release it. 2775 */ 2776 int 2777 xfs_ialloc_read_agi( 2778 struct xfs_perag *pag, 2779 struct xfs_trans *tp, 2780 int flags, 2781 struct xfs_buf **agibpp) 2782 { 2783 struct xfs_buf *agibp; 2784 struct xfs_agi *agi; 2785 int error; 2786 2787 trace_xfs_ialloc_read_agi(pag); 2788 2789 error = xfs_read_agi(pag, tp, 2790 (flags & XFS_IALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0, 2791 &agibp); 2792 if (error) 2793 return error; 2794 2795 agi = agibp->b_addr; 2796 if (!xfs_perag_initialised_agi(pag)) { 2797 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount); 2798 pag->pagi_count = be32_to_cpu(agi->agi_count); 2799 set_bit(XFS_AGSTATE_AGI_INIT, &pag->pag_opstate); 2800 } 2801 2802 /* 2803 * It's possible for these to be out of sync if 2804 * we are in the middle of a forced shutdown. 2805 */ 2806 ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) || 2807 xfs_is_shutdown(pag_mount(pag))); 2808 if (agibpp) 2809 *agibpp = agibp; 2810 else 2811 xfs_trans_brelse(tp, agibp); 2812 return 0; 2813 } 2814 2815 /* How many inodes are backed by inode clusters ondisk? */ 2816 STATIC int 2817 xfs_ialloc_count_ondisk( 2818 struct xfs_btree_cur *cur, 2819 xfs_agino_t low, 2820 xfs_agino_t high, 2821 unsigned int *allocated) 2822 { 2823 struct xfs_inobt_rec_incore irec; 2824 unsigned int ret = 0; 2825 int has_record; 2826 int error; 2827 2828 error = xfs_inobt_lookup(cur, low, XFS_LOOKUP_LE, &has_record); 2829 if (error) 2830 return error; 2831 2832 while (has_record) { 2833 unsigned int i, hole_idx; 2834 2835 error = xfs_inobt_get_rec(cur, &irec, &has_record); 2836 if (error) 2837 return error; 2838 if (irec.ir_startino > high) 2839 break; 2840 2841 for (i = 0; i < XFS_INODES_PER_CHUNK; i++) { 2842 if (irec.ir_startino + i < low) 2843 continue; 2844 if (irec.ir_startino + i > high) 2845 break; 2846 2847 hole_idx = i / XFS_INODES_PER_HOLEMASK_BIT; 2848 if (!(irec.ir_holemask & (1U << hole_idx))) 2849 ret++; 2850 } 2851 2852 error = xfs_btree_increment(cur, 0, &has_record); 2853 if (error) 2854 return error; 2855 } 2856 2857 *allocated = ret; 2858 return 0; 2859 } 2860 2861 /* Is there an inode record covering a given extent? */ 2862 int 2863 xfs_ialloc_has_inodes_at_extent( 2864 struct xfs_btree_cur *cur, 2865 xfs_agblock_t bno, 2866 xfs_extlen_t len, 2867 enum xbtree_recpacking *outcome) 2868 { 2869 xfs_agino_t agino; 2870 xfs_agino_t last_agino; 2871 unsigned int allocated; 2872 int error; 2873 2874 agino = XFS_AGB_TO_AGINO(cur->bc_mp, bno); 2875 last_agino = XFS_AGB_TO_AGINO(cur->bc_mp, bno + len) - 1; 2876 2877 error = xfs_ialloc_count_ondisk(cur, agino, last_agino, &allocated); 2878 if (error) 2879 return error; 2880 2881 if (allocated == 0) 2882 *outcome = XBTREE_RECPACKING_EMPTY; 2883 else if (allocated == last_agino - agino + 1) 2884 *outcome = XBTREE_RECPACKING_FULL; 2885 else 2886 *outcome = XBTREE_RECPACKING_SPARSE; 2887 return 0; 2888 } 2889 2890 struct xfs_ialloc_count_inodes { 2891 xfs_agino_t count; 2892 xfs_agino_t freecount; 2893 }; 2894 2895 /* Record inode counts across all inobt records. */ 2896 STATIC int 2897 xfs_ialloc_count_inodes_rec( 2898 struct xfs_btree_cur *cur, 2899 const union xfs_btree_rec *rec, 2900 void *priv) 2901 { 2902 struct xfs_inobt_rec_incore irec; 2903 struct xfs_ialloc_count_inodes *ci = priv; 2904 xfs_failaddr_t fa; 2905 2906 xfs_inobt_btrec_to_irec(cur->bc_mp, rec, &irec); 2907 fa = xfs_inobt_check_irec(to_perag(cur->bc_group), &irec); 2908 if (fa) 2909 return xfs_inobt_complain_bad_rec(cur, fa, &irec); 2910 2911 ci->count += irec.ir_count; 2912 ci->freecount += irec.ir_freecount; 2913 2914 return 0; 2915 } 2916 2917 /* Count allocated and free inodes under an inobt. */ 2918 int 2919 xfs_ialloc_count_inodes( 2920 struct xfs_btree_cur *cur, 2921 xfs_agino_t *count, 2922 xfs_agino_t *freecount) 2923 { 2924 struct xfs_ialloc_count_inodes ci = {0}; 2925 int error; 2926 2927 ASSERT(xfs_btree_is_ino(cur->bc_ops)); 2928 error = xfs_btree_query_all(cur, xfs_ialloc_count_inodes_rec, &ci); 2929 if (error) 2930 return error; 2931 2932 *count = ci.count; 2933 *freecount = ci.freecount; 2934 return 0; 2935 } 2936 2937 /* 2938 * Initialize inode-related geometry information. 2939 * 2940 * Compute the inode btree min and max levels and set maxicount. 2941 * 2942 * Set the inode cluster size. This may still be overridden by the file 2943 * system block size if it is larger than the chosen cluster size. 2944 * 2945 * For v5 filesystems, scale the cluster size with the inode size to keep a 2946 * constant ratio of inode per cluster buffer, but only if mkfs has set the 2947 * inode alignment value appropriately for larger cluster sizes. 2948 * 2949 * Then compute the inode cluster alignment information. 2950 */ 2951 void 2952 xfs_ialloc_setup_geometry( 2953 struct xfs_mount *mp) 2954 { 2955 struct xfs_sb *sbp = &mp->m_sb; 2956 struct xfs_ino_geometry *igeo = M_IGEO(mp); 2957 uint64_t icount; 2958 uint inodes; 2959 2960 igeo->new_diflags2 = 0; 2961 if (xfs_has_bigtime(mp)) 2962 igeo->new_diflags2 |= XFS_DIFLAG2_BIGTIME; 2963 if (xfs_has_large_extent_counts(mp)) 2964 igeo->new_diflags2 |= XFS_DIFLAG2_NREXT64; 2965 2966 /* Compute inode btree geometry. */ 2967 igeo->agino_log = sbp->sb_inopblog + sbp->sb_agblklog; 2968 igeo->inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, true); 2969 igeo->inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, false); 2970 igeo->inobt_mnr[0] = igeo->inobt_mxr[0] / 2; 2971 igeo->inobt_mnr[1] = igeo->inobt_mxr[1] / 2; 2972 2973 igeo->ialloc_inos = max_t(uint16_t, XFS_INODES_PER_CHUNK, 2974 sbp->sb_inopblock); 2975 igeo->ialloc_blks = igeo->ialloc_inos >> sbp->sb_inopblog; 2976 2977 if (sbp->sb_spino_align) 2978 igeo->ialloc_min_blks = sbp->sb_spino_align; 2979 else 2980 igeo->ialloc_min_blks = igeo->ialloc_blks; 2981 2982 /* Compute and fill in value of m_ino_geo.inobt_maxlevels. */ 2983 inodes = (1LL << XFS_INO_AGINO_BITS(mp)) >> XFS_INODES_PER_CHUNK_LOG; 2984 igeo->inobt_maxlevels = xfs_btree_compute_maxlevels(igeo->inobt_mnr, 2985 inodes); 2986 ASSERT(igeo->inobt_maxlevels <= xfs_iallocbt_maxlevels_ondisk()); 2987 2988 /* 2989 * Set the maximum inode count for this filesystem, being careful not 2990 * to use obviously garbage sb_inopblog/sb_inopblock values. Regular 2991 * users should never get here due to failing sb verification, but 2992 * certain users (xfs_db) need to be usable even with corrupt metadata. 2993 */ 2994 if (sbp->sb_imax_pct && igeo->ialloc_blks) { 2995 /* 2996 * Make sure the maximum inode count is a multiple 2997 * of the units we allocate inodes in. 2998 */ 2999 icount = sbp->sb_dblocks * sbp->sb_imax_pct; 3000 do_div(icount, 100); 3001 do_div(icount, igeo->ialloc_blks); 3002 igeo->maxicount = XFS_FSB_TO_INO(mp, 3003 icount * igeo->ialloc_blks); 3004 } else { 3005 igeo->maxicount = 0; 3006 } 3007 3008 /* 3009 * Compute the desired size of an inode cluster buffer size, which 3010 * starts at 8K and (on v5 filesystems) scales up with larger inode 3011 * sizes. 3012 * 3013 * Preserve the desired inode cluster size because the sparse inodes 3014 * feature uses that desired size (not the actual size) to compute the 3015 * sparse inode alignment. The mount code validates this value, so we 3016 * cannot change the behavior. 3017 */ 3018 igeo->inode_cluster_size_raw = XFS_INODE_BIG_CLUSTER_SIZE; 3019 if (xfs_has_v3inodes(mp)) { 3020 int new_size = igeo->inode_cluster_size_raw; 3021 3022 new_size *= mp->m_sb.sb_inodesize / XFS_DINODE_MIN_SIZE; 3023 if (mp->m_sb.sb_inoalignmt >= XFS_B_TO_FSBT(mp, new_size)) 3024 igeo->inode_cluster_size_raw = new_size; 3025 } 3026 3027 /* Calculate inode cluster ratios. */ 3028 if (igeo->inode_cluster_size_raw > mp->m_sb.sb_blocksize) 3029 igeo->blocks_per_cluster = XFS_B_TO_FSBT(mp, 3030 igeo->inode_cluster_size_raw); 3031 else 3032 igeo->blocks_per_cluster = 1; 3033 igeo->inode_cluster_size = XFS_FSB_TO_B(mp, igeo->blocks_per_cluster); 3034 igeo->inodes_per_cluster = XFS_FSB_TO_INO(mp, igeo->blocks_per_cluster); 3035 3036 /* Calculate inode cluster alignment. */ 3037 if (xfs_has_align(mp) && 3038 mp->m_sb.sb_inoalignmt >= igeo->blocks_per_cluster) 3039 igeo->cluster_align = mp->m_sb.sb_inoalignmt; 3040 else 3041 igeo->cluster_align = 1; 3042 igeo->inoalign_mask = igeo->cluster_align - 1; 3043 igeo->cluster_align_inodes = XFS_FSB_TO_INO(mp, igeo->cluster_align); 3044 3045 /* 3046 * If we are using stripe alignment, check whether 3047 * the stripe unit is a multiple of the inode alignment 3048 */ 3049 if (mp->m_dalign && igeo->inoalign_mask && 3050 !(mp->m_dalign & igeo->inoalign_mask)) 3051 igeo->ialloc_align = mp->m_dalign; 3052 else 3053 igeo->ialloc_align = 0; 3054 3055 if (mp->m_sb.sb_blocksize > PAGE_SIZE) 3056 igeo->min_folio_order = mp->m_sb.sb_blocklog - PAGE_SHIFT; 3057 else 3058 igeo->min_folio_order = 0; 3059 } 3060 3061 /* Compute the location of the root directory inode that is laid out by mkfs. */ 3062 xfs_ino_t 3063 xfs_ialloc_calc_rootino( 3064 struct xfs_mount *mp, 3065 int sunit) 3066 { 3067 struct xfs_ino_geometry *igeo = M_IGEO(mp); 3068 xfs_agblock_t first_bno; 3069 3070 /* 3071 * Pre-calculate the geometry of AG 0. We know what it looks like 3072 * because libxfs knows how to create allocation groups now. 3073 * 3074 * first_bno is the first block in which mkfs could possibly have 3075 * allocated the root directory inode, once we factor in the metadata 3076 * that mkfs formats before it. Namely, the four AG headers... 3077 */ 3078 first_bno = howmany(4 * mp->m_sb.sb_sectsize, mp->m_sb.sb_blocksize); 3079 3080 /* ...the two free space btree roots... */ 3081 first_bno += 2; 3082 3083 /* ...the inode btree root... */ 3084 first_bno += 1; 3085 3086 /* ...the initial AGFL... */ 3087 first_bno += xfs_alloc_min_freelist(mp, NULL); 3088 3089 /* ...the free inode btree root... */ 3090 if (xfs_has_finobt(mp)) 3091 first_bno++; 3092 3093 /* ...the reverse mapping btree root... */ 3094 if (xfs_has_rmapbt(mp)) 3095 first_bno++; 3096 3097 /* ...the reference count btree... */ 3098 if (xfs_has_reflink(mp)) 3099 first_bno++; 3100 3101 /* 3102 * ...and the log, if it is allocated in the first allocation group. 3103 * 3104 * This can happen with filesystems that only have a single 3105 * allocation group, or very odd geometries created by old mkfs 3106 * versions on very small filesystems. 3107 */ 3108 if (xfs_ag_contains_log(mp, 0)) 3109 first_bno += mp->m_sb.sb_logblocks; 3110 3111 /* 3112 * Now round first_bno up to whatever allocation alignment is given 3113 * by the filesystem or was passed in. 3114 */ 3115 if (xfs_has_dalign(mp) && igeo->ialloc_align > 0) 3116 first_bno = roundup(first_bno, sunit); 3117 else if (xfs_has_align(mp) && 3118 mp->m_sb.sb_inoalignmt > 1) 3119 first_bno = roundup(first_bno, mp->m_sb.sb_inoalignmt); 3120 3121 return XFS_AGINO_TO_INO(mp, 0, XFS_AGB_TO_AGINO(mp, first_bno)); 3122 } 3123 3124 /* 3125 * Ensure there are not sparse inode clusters that cross the new EOAG. 3126 * 3127 * This is a no-op for non-spinode filesystems since clusters are always fully 3128 * allocated and checking the bnobt suffices. However, a spinode filesystem 3129 * could have a record where the upper inodes are free blocks. If those blocks 3130 * were removed from the filesystem, the inode record would extend beyond EOAG, 3131 * which will be flagged as corruption. 3132 */ 3133 int 3134 xfs_ialloc_check_shrink( 3135 struct xfs_perag *pag, 3136 struct xfs_trans *tp, 3137 struct xfs_buf *agibp, 3138 xfs_agblock_t new_length) 3139 { 3140 struct xfs_inobt_rec_incore rec; 3141 struct xfs_btree_cur *cur; 3142 xfs_agino_t agino; 3143 int has; 3144 int error; 3145 3146 if (!xfs_has_sparseinodes(pag_mount(pag))) 3147 return 0; 3148 3149 cur = xfs_inobt_init_cursor(pag, tp, agibp); 3150 3151 /* Look up the inobt record that would correspond to the new EOFS. */ 3152 agino = XFS_AGB_TO_AGINO(pag_mount(pag), new_length); 3153 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &has); 3154 if (error || !has) 3155 goto out; 3156 3157 error = xfs_inobt_get_rec(cur, &rec, &has); 3158 if (error) 3159 goto out; 3160 3161 if (!has) { 3162 xfs_ag_mark_sick(pag, XFS_SICK_AG_INOBT); 3163 error = -EFSCORRUPTED; 3164 goto out; 3165 } 3166 3167 /* If the record covers inodes that would be beyond EOFS, bail out. */ 3168 if (rec.ir_startino + XFS_INODES_PER_CHUNK > agino) { 3169 error = -ENOSPC; 3170 goto out; 3171 } 3172 out: 3173 xfs_btree_del_cursor(cur, error); 3174 return error; 3175 } 3176