1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * linux/fs/ext4/ialloc.c 4 * 5 * Copyright (C) 1992, 1993, 1994, 1995 6 * Remy Card (card@masi.ibp.fr) 7 * Laboratoire MASI - Institut Blaise Pascal 8 * Universite Pierre et Marie Curie (Paris VI) 9 * 10 * BSD ufs-inspired inode and directory allocation by 11 * Stephen Tweedie (sct@redhat.com), 1993 12 * Big-endian to little-endian byte-swapping/bitmaps by 13 * David S. Miller (davem@caip.rutgers.edu), 1995 14 */ 15 16 #include <linux/time.h> 17 #include <linux/fs.h> 18 #include <linux/stat.h> 19 #include <linux/string.h> 20 #include <linux/quotaops.h> 21 #include <linux/buffer_head.h> 22 #include <linux/random.h> 23 #include <linux/bitops.h> 24 #include <linux/blkdev.h> 25 #include <linux/cred.h> 26 27 #include <asm/byteorder.h> 28 29 #include "ext4.h" 30 #include "ext4_jbd2.h" 31 #include "xattr.h" 32 #include "acl.h" 33 34 #include <trace/events/ext4.h> 35 36 /* 37 * ialloc.c contains the inodes allocation and deallocation routines 38 */ 39 40 /* 41 * The free inodes are managed by bitmaps. A file system contains several 42 * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap 43 * block for inodes, N blocks for the inode table and data blocks. 44 * 45 * The file system contains group descriptors which are located after the 46 * super block. Each descriptor contains the number of the bitmap block and 47 * the free blocks count in the block. 48 */ 49 50 /* 51 * To avoid calling the atomic setbit hundreds or thousands of times, we only 52 * need to use it within a single byte (to ensure we get endianness right). 53 * We can use memset for the rest of the bitmap as there are no other users. 54 */ 55 void ext4_mark_bitmap_end(int start_bit, int end_bit, char *bitmap) 56 { 57 int i; 58 59 if (start_bit >= end_bit) 60 return; 61 62 ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit); 63 for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++) 64 ext4_set_bit(i, bitmap); 65 if (i < end_bit) 66 memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3); 67 } 68 69 /* Initializes an uninitialized inode bitmap */ 70 static int ext4_init_inode_bitmap(struct super_block *sb, 71 struct buffer_head *bh, 72 ext4_group_t block_group, 73 struct ext4_group_desc *gdp) 74 { 75 struct ext4_group_info *grp; 76 struct ext4_sb_info *sbi = EXT4_SB(sb); 77 J_ASSERT_BH(bh, buffer_locked(bh)); 78 79 /* If checksum is bad mark all blocks and inodes use to prevent 80 * allocation, essentially implementing a per-group read-only flag. */ 81 if (!ext4_group_desc_csum_verify(sb, block_group, gdp)) { 82 grp = ext4_get_group_info(sb, block_group); 83 if (!EXT4_MB_GRP_BBITMAP_CORRUPT(grp)) 84 percpu_counter_sub(&sbi->s_freeclusters_counter, 85 grp->bb_free); 86 set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT, &grp->bb_state); 87 if (!EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) { 88 int count; 89 count = ext4_free_inodes_count(sb, gdp); 90 percpu_counter_sub(&sbi->s_freeinodes_counter, 91 count); 92 } 93 set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &grp->bb_state); 94 return -EFSBADCRC; 95 } 96 97 memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8); 98 ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), sb->s_blocksize * 8, 99 bh->b_data); 100 ext4_inode_bitmap_csum_set(sb, block_group, gdp, bh, 101 EXT4_INODES_PER_GROUP(sb) / 8); 102 ext4_group_desc_csum_set(sb, block_group, gdp); 103 104 return 0; 105 } 106 107 void ext4_end_bitmap_read(struct buffer_head *bh, int uptodate) 108 { 109 if (uptodate) { 110 set_buffer_uptodate(bh); 111 set_bitmap_uptodate(bh); 112 } 113 unlock_buffer(bh); 114 put_bh(bh); 115 } 116 117 static int ext4_validate_inode_bitmap(struct super_block *sb, 118 struct ext4_group_desc *desc, 119 ext4_group_t block_group, 120 struct buffer_head *bh) 121 { 122 ext4_fsblk_t blk; 123 struct ext4_group_info *grp = ext4_get_group_info(sb, block_group); 124 struct ext4_sb_info *sbi = EXT4_SB(sb); 125 126 if (buffer_verified(bh)) 127 return 0; 128 if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) 129 return -EFSCORRUPTED; 130 131 ext4_lock_group(sb, block_group); 132 blk = ext4_inode_bitmap(sb, desc); 133 if (!ext4_inode_bitmap_csum_verify(sb, block_group, desc, bh, 134 EXT4_INODES_PER_GROUP(sb) / 8)) { 135 ext4_unlock_group(sb, block_group); 136 ext4_error(sb, "Corrupt inode bitmap - block_group = %u, " 137 "inode_bitmap = %llu", block_group, blk); 138 grp = ext4_get_group_info(sb, block_group); 139 if (!EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) { 140 int count; 141 count = ext4_free_inodes_count(sb, desc); 142 percpu_counter_sub(&sbi->s_freeinodes_counter, 143 count); 144 } 145 set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &grp->bb_state); 146 return -EFSBADCRC; 147 } 148 set_buffer_verified(bh); 149 ext4_unlock_group(sb, block_group); 150 return 0; 151 } 152 153 /* 154 * Read the inode allocation bitmap for a given block_group, reading 155 * into the specified slot in the superblock's bitmap cache. 156 * 157 * Return buffer_head of bitmap on success or NULL. 158 */ 159 static struct buffer_head * 160 ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group) 161 { 162 struct ext4_group_desc *desc; 163 struct buffer_head *bh = NULL; 164 ext4_fsblk_t bitmap_blk; 165 int err; 166 167 desc = ext4_get_group_desc(sb, block_group, NULL); 168 if (!desc) 169 return ERR_PTR(-EFSCORRUPTED); 170 171 bitmap_blk = ext4_inode_bitmap(sb, desc); 172 bh = sb_getblk(sb, bitmap_blk); 173 if (unlikely(!bh)) { 174 ext4_error(sb, "Cannot read inode bitmap - " 175 "block_group = %u, inode_bitmap = %llu", 176 block_group, bitmap_blk); 177 return ERR_PTR(-EIO); 178 } 179 if (bitmap_uptodate(bh)) 180 goto verify; 181 182 lock_buffer(bh); 183 if (bitmap_uptodate(bh)) { 184 unlock_buffer(bh); 185 goto verify; 186 } 187 188 ext4_lock_group(sb, block_group); 189 if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) { 190 err = ext4_init_inode_bitmap(sb, bh, block_group, desc); 191 set_bitmap_uptodate(bh); 192 set_buffer_uptodate(bh); 193 set_buffer_verified(bh); 194 ext4_unlock_group(sb, block_group); 195 unlock_buffer(bh); 196 if (err) { 197 ext4_error(sb, "Failed to init inode bitmap for group " 198 "%u: %d", block_group, err); 199 goto out; 200 } 201 return bh; 202 } 203 ext4_unlock_group(sb, block_group); 204 205 if (buffer_uptodate(bh)) { 206 /* 207 * if not uninit if bh is uptodate, 208 * bitmap is also uptodate 209 */ 210 set_bitmap_uptodate(bh); 211 unlock_buffer(bh); 212 goto verify; 213 } 214 /* 215 * submit the buffer_head for reading 216 */ 217 trace_ext4_load_inode_bitmap(sb, block_group); 218 bh->b_end_io = ext4_end_bitmap_read; 219 get_bh(bh); 220 submit_bh(REQ_OP_READ, REQ_META | REQ_PRIO, bh); 221 wait_on_buffer(bh); 222 if (!buffer_uptodate(bh)) { 223 put_bh(bh); 224 ext4_error(sb, "Cannot read inode bitmap - " 225 "block_group = %u, inode_bitmap = %llu", 226 block_group, bitmap_blk); 227 return ERR_PTR(-EIO); 228 } 229 230 verify: 231 err = ext4_validate_inode_bitmap(sb, desc, block_group, bh); 232 if (err) 233 goto out; 234 return bh; 235 out: 236 put_bh(bh); 237 return ERR_PTR(err); 238 } 239 240 /* 241 * NOTE! When we get the inode, we're the only people 242 * that have access to it, and as such there are no 243 * race conditions we have to worry about. The inode 244 * is not on the hash-lists, and it cannot be reached 245 * through the filesystem because the directory entry 246 * has been deleted earlier. 247 * 248 * HOWEVER: we must make sure that we get no aliases, 249 * which means that we have to call "clear_inode()" 250 * _before_ we mark the inode not in use in the inode 251 * bitmaps. Otherwise a newly created file might use 252 * the same inode number (not actually the same pointer 253 * though), and then we'd have two inodes sharing the 254 * same inode number and space on the harddisk. 255 */ 256 void ext4_free_inode(handle_t *handle, struct inode *inode) 257 { 258 struct super_block *sb = inode->i_sb; 259 int is_directory; 260 unsigned long ino; 261 struct buffer_head *bitmap_bh = NULL; 262 struct buffer_head *bh2; 263 ext4_group_t block_group; 264 unsigned long bit; 265 struct ext4_group_desc *gdp; 266 struct ext4_super_block *es; 267 struct ext4_sb_info *sbi; 268 int fatal = 0, err, count, cleared; 269 struct ext4_group_info *grp; 270 271 if (!sb) { 272 printk(KERN_ERR "EXT4-fs: %s:%d: inode on " 273 "nonexistent device\n", __func__, __LINE__); 274 return; 275 } 276 if (atomic_read(&inode->i_count) > 1) { 277 ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: count=%d", 278 __func__, __LINE__, inode->i_ino, 279 atomic_read(&inode->i_count)); 280 return; 281 } 282 if (inode->i_nlink) { 283 ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: nlink=%d\n", 284 __func__, __LINE__, inode->i_ino, inode->i_nlink); 285 return; 286 } 287 sbi = EXT4_SB(sb); 288 289 ino = inode->i_ino; 290 ext4_debug("freeing inode %lu\n", ino); 291 trace_ext4_free_inode(inode); 292 293 /* 294 * Note: we must free any quota before locking the superblock, 295 * as writing the quota to disk may need the lock as well. 296 */ 297 dquot_initialize(inode); 298 dquot_free_inode(inode); 299 dquot_drop(inode); 300 301 is_directory = S_ISDIR(inode->i_mode); 302 303 /* Do this BEFORE marking the inode not in use or returning an error */ 304 ext4_clear_inode(inode); 305 306 es = sbi->s_es; 307 if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) { 308 ext4_error(sb, "reserved or nonexistent inode %lu", ino); 309 goto error_return; 310 } 311 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); 312 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); 313 bitmap_bh = ext4_read_inode_bitmap(sb, block_group); 314 /* Don't bother if the inode bitmap is corrupt. */ 315 grp = ext4_get_group_info(sb, block_group); 316 if (IS_ERR(bitmap_bh)) { 317 fatal = PTR_ERR(bitmap_bh); 318 bitmap_bh = NULL; 319 goto error_return; 320 } 321 if (unlikely(EXT4_MB_GRP_IBITMAP_CORRUPT(grp))) { 322 fatal = -EFSCORRUPTED; 323 goto error_return; 324 } 325 326 BUFFER_TRACE(bitmap_bh, "get_write_access"); 327 fatal = ext4_journal_get_write_access(handle, bitmap_bh); 328 if (fatal) 329 goto error_return; 330 331 fatal = -ESRCH; 332 gdp = ext4_get_group_desc(sb, block_group, &bh2); 333 if (gdp) { 334 BUFFER_TRACE(bh2, "get_write_access"); 335 fatal = ext4_journal_get_write_access(handle, bh2); 336 } 337 ext4_lock_group(sb, block_group); 338 cleared = ext4_test_and_clear_bit(bit, bitmap_bh->b_data); 339 if (fatal || !cleared) { 340 ext4_unlock_group(sb, block_group); 341 goto out; 342 } 343 344 count = ext4_free_inodes_count(sb, gdp) + 1; 345 ext4_free_inodes_set(sb, gdp, count); 346 if (is_directory) { 347 count = ext4_used_dirs_count(sb, gdp) - 1; 348 ext4_used_dirs_set(sb, gdp, count); 349 percpu_counter_dec(&sbi->s_dirs_counter); 350 } 351 ext4_inode_bitmap_csum_set(sb, block_group, gdp, bitmap_bh, 352 EXT4_INODES_PER_GROUP(sb) / 8); 353 ext4_group_desc_csum_set(sb, block_group, gdp); 354 ext4_unlock_group(sb, block_group); 355 356 percpu_counter_inc(&sbi->s_freeinodes_counter); 357 if (sbi->s_log_groups_per_flex) { 358 ext4_group_t f = ext4_flex_group(sbi, block_group); 359 360 atomic_inc(&sbi->s_flex_groups[f].free_inodes); 361 if (is_directory) 362 atomic_dec(&sbi->s_flex_groups[f].used_dirs); 363 } 364 BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata"); 365 fatal = ext4_handle_dirty_metadata(handle, NULL, bh2); 366 out: 367 if (cleared) { 368 BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata"); 369 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh); 370 if (!fatal) 371 fatal = err; 372 } else { 373 ext4_error(sb, "bit already cleared for inode %lu", ino); 374 if (gdp && !EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) { 375 int count; 376 count = ext4_free_inodes_count(sb, gdp); 377 percpu_counter_sub(&sbi->s_freeinodes_counter, 378 count); 379 } 380 set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &grp->bb_state); 381 } 382 383 error_return: 384 brelse(bitmap_bh); 385 ext4_std_error(sb, fatal); 386 } 387 388 struct orlov_stats { 389 __u64 free_clusters; 390 __u32 free_inodes; 391 __u32 used_dirs; 392 }; 393 394 /* 395 * Helper function for Orlov's allocator; returns critical information 396 * for a particular block group or flex_bg. If flex_size is 1, then g 397 * is a block group number; otherwise it is flex_bg number. 398 */ 399 static void get_orlov_stats(struct super_block *sb, ext4_group_t g, 400 int flex_size, struct orlov_stats *stats) 401 { 402 struct ext4_group_desc *desc; 403 struct flex_groups *flex_group = EXT4_SB(sb)->s_flex_groups; 404 405 if (flex_size > 1) { 406 stats->free_inodes = atomic_read(&flex_group[g].free_inodes); 407 stats->free_clusters = atomic64_read(&flex_group[g].free_clusters); 408 stats->used_dirs = atomic_read(&flex_group[g].used_dirs); 409 return; 410 } 411 412 desc = ext4_get_group_desc(sb, g, NULL); 413 if (desc) { 414 stats->free_inodes = ext4_free_inodes_count(sb, desc); 415 stats->free_clusters = ext4_free_group_clusters(sb, desc); 416 stats->used_dirs = ext4_used_dirs_count(sb, desc); 417 } else { 418 stats->free_inodes = 0; 419 stats->free_clusters = 0; 420 stats->used_dirs = 0; 421 } 422 } 423 424 /* 425 * Orlov's allocator for directories. 426 * 427 * We always try to spread first-level directories. 428 * 429 * If there are blockgroups with both free inodes and free blocks counts 430 * not worse than average we return one with smallest directory count. 431 * Otherwise we simply return a random group. 432 * 433 * For the rest rules look so: 434 * 435 * It's OK to put directory into a group unless 436 * it has too many directories already (max_dirs) or 437 * it has too few free inodes left (min_inodes) or 438 * it has too few free blocks left (min_blocks) or 439 * Parent's group is preferred, if it doesn't satisfy these 440 * conditions we search cyclically through the rest. If none 441 * of the groups look good we just look for a group with more 442 * free inodes than average (starting at parent's group). 443 */ 444 445 static int find_group_orlov(struct super_block *sb, struct inode *parent, 446 ext4_group_t *group, umode_t mode, 447 const struct qstr *qstr) 448 { 449 ext4_group_t parent_group = EXT4_I(parent)->i_block_group; 450 struct ext4_sb_info *sbi = EXT4_SB(sb); 451 ext4_group_t real_ngroups = ext4_get_groups_count(sb); 452 int inodes_per_group = EXT4_INODES_PER_GROUP(sb); 453 unsigned int freei, avefreei, grp_free; 454 ext4_fsblk_t freeb, avefreec; 455 unsigned int ndirs; 456 int max_dirs, min_inodes; 457 ext4_grpblk_t min_clusters; 458 ext4_group_t i, grp, g, ngroups; 459 struct ext4_group_desc *desc; 460 struct orlov_stats stats; 461 int flex_size = ext4_flex_bg_size(sbi); 462 struct dx_hash_info hinfo; 463 464 ngroups = real_ngroups; 465 if (flex_size > 1) { 466 ngroups = (real_ngroups + flex_size - 1) >> 467 sbi->s_log_groups_per_flex; 468 parent_group >>= sbi->s_log_groups_per_flex; 469 } 470 471 freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter); 472 avefreei = freei / ngroups; 473 freeb = EXT4_C2B(sbi, 474 percpu_counter_read_positive(&sbi->s_freeclusters_counter)); 475 avefreec = freeb; 476 do_div(avefreec, ngroups); 477 ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter); 478 479 if (S_ISDIR(mode) && 480 ((parent == d_inode(sb->s_root)) || 481 (ext4_test_inode_flag(parent, EXT4_INODE_TOPDIR)))) { 482 int best_ndir = inodes_per_group; 483 int ret = -1; 484 485 if (qstr) { 486 hinfo.hash_version = DX_HASH_HALF_MD4; 487 hinfo.seed = sbi->s_hash_seed; 488 ext4fs_dirhash(qstr->name, qstr->len, &hinfo); 489 grp = hinfo.hash; 490 } else 491 grp = prandom_u32(); 492 parent_group = (unsigned)grp % ngroups; 493 for (i = 0; i < ngroups; i++) { 494 g = (parent_group + i) % ngroups; 495 get_orlov_stats(sb, g, flex_size, &stats); 496 if (!stats.free_inodes) 497 continue; 498 if (stats.used_dirs >= best_ndir) 499 continue; 500 if (stats.free_inodes < avefreei) 501 continue; 502 if (stats.free_clusters < avefreec) 503 continue; 504 grp = g; 505 ret = 0; 506 best_ndir = stats.used_dirs; 507 } 508 if (ret) 509 goto fallback; 510 found_flex_bg: 511 if (flex_size == 1) { 512 *group = grp; 513 return 0; 514 } 515 516 /* 517 * We pack inodes at the beginning of the flexgroup's 518 * inode tables. Block allocation decisions will do 519 * something similar, although regular files will 520 * start at 2nd block group of the flexgroup. See 521 * ext4_ext_find_goal() and ext4_find_near(). 522 */ 523 grp *= flex_size; 524 for (i = 0; i < flex_size; i++) { 525 if (grp+i >= real_ngroups) 526 break; 527 desc = ext4_get_group_desc(sb, grp+i, NULL); 528 if (desc && ext4_free_inodes_count(sb, desc)) { 529 *group = grp+i; 530 return 0; 531 } 532 } 533 goto fallback; 534 } 535 536 max_dirs = ndirs / ngroups + inodes_per_group / 16; 537 min_inodes = avefreei - inodes_per_group*flex_size / 4; 538 if (min_inodes < 1) 539 min_inodes = 1; 540 min_clusters = avefreec - EXT4_CLUSTERS_PER_GROUP(sb)*flex_size / 4; 541 542 /* 543 * Start looking in the flex group where we last allocated an 544 * inode for this parent directory 545 */ 546 if (EXT4_I(parent)->i_last_alloc_group != ~0) { 547 parent_group = EXT4_I(parent)->i_last_alloc_group; 548 if (flex_size > 1) 549 parent_group >>= sbi->s_log_groups_per_flex; 550 } 551 552 for (i = 0; i < ngroups; i++) { 553 grp = (parent_group + i) % ngroups; 554 get_orlov_stats(sb, grp, flex_size, &stats); 555 if (stats.used_dirs >= max_dirs) 556 continue; 557 if (stats.free_inodes < min_inodes) 558 continue; 559 if (stats.free_clusters < min_clusters) 560 continue; 561 goto found_flex_bg; 562 } 563 564 fallback: 565 ngroups = real_ngroups; 566 avefreei = freei / ngroups; 567 fallback_retry: 568 parent_group = EXT4_I(parent)->i_block_group; 569 for (i = 0; i < ngroups; i++) { 570 grp = (parent_group + i) % ngroups; 571 desc = ext4_get_group_desc(sb, grp, NULL); 572 if (desc) { 573 grp_free = ext4_free_inodes_count(sb, desc); 574 if (grp_free && grp_free >= avefreei) { 575 *group = grp; 576 return 0; 577 } 578 } 579 } 580 581 if (avefreei) { 582 /* 583 * The free-inodes counter is approximate, and for really small 584 * filesystems the above test can fail to find any blockgroups 585 */ 586 avefreei = 0; 587 goto fallback_retry; 588 } 589 590 return -1; 591 } 592 593 static int find_group_other(struct super_block *sb, struct inode *parent, 594 ext4_group_t *group, umode_t mode) 595 { 596 ext4_group_t parent_group = EXT4_I(parent)->i_block_group; 597 ext4_group_t i, last, ngroups = ext4_get_groups_count(sb); 598 struct ext4_group_desc *desc; 599 int flex_size = ext4_flex_bg_size(EXT4_SB(sb)); 600 601 /* 602 * Try to place the inode is the same flex group as its 603 * parent. If we can't find space, use the Orlov algorithm to 604 * find another flex group, and store that information in the 605 * parent directory's inode information so that use that flex 606 * group for future allocations. 607 */ 608 if (flex_size > 1) { 609 int retry = 0; 610 611 try_again: 612 parent_group &= ~(flex_size-1); 613 last = parent_group + flex_size; 614 if (last > ngroups) 615 last = ngroups; 616 for (i = parent_group; i < last; i++) { 617 desc = ext4_get_group_desc(sb, i, NULL); 618 if (desc && ext4_free_inodes_count(sb, desc)) { 619 *group = i; 620 return 0; 621 } 622 } 623 if (!retry && EXT4_I(parent)->i_last_alloc_group != ~0) { 624 retry = 1; 625 parent_group = EXT4_I(parent)->i_last_alloc_group; 626 goto try_again; 627 } 628 /* 629 * If this didn't work, use the Orlov search algorithm 630 * to find a new flex group; we pass in the mode to 631 * avoid the topdir algorithms. 632 */ 633 *group = parent_group + flex_size; 634 if (*group > ngroups) 635 *group = 0; 636 return find_group_orlov(sb, parent, group, mode, NULL); 637 } 638 639 /* 640 * Try to place the inode in its parent directory 641 */ 642 *group = parent_group; 643 desc = ext4_get_group_desc(sb, *group, NULL); 644 if (desc && ext4_free_inodes_count(sb, desc) && 645 ext4_free_group_clusters(sb, desc)) 646 return 0; 647 648 /* 649 * We're going to place this inode in a different blockgroup from its 650 * parent. We want to cause files in a common directory to all land in 651 * the same blockgroup. But we want files which are in a different 652 * directory which shares a blockgroup with our parent to land in a 653 * different blockgroup. 654 * 655 * So add our directory's i_ino into the starting point for the hash. 656 */ 657 *group = (*group + parent->i_ino) % ngroups; 658 659 /* 660 * Use a quadratic hash to find a group with a free inode and some free 661 * blocks. 662 */ 663 for (i = 1; i < ngroups; i <<= 1) { 664 *group += i; 665 if (*group >= ngroups) 666 *group -= ngroups; 667 desc = ext4_get_group_desc(sb, *group, NULL); 668 if (desc && ext4_free_inodes_count(sb, desc) && 669 ext4_free_group_clusters(sb, desc)) 670 return 0; 671 } 672 673 /* 674 * That failed: try linear search for a free inode, even if that group 675 * has no free blocks. 676 */ 677 *group = parent_group; 678 for (i = 0; i < ngroups; i++) { 679 if (++*group >= ngroups) 680 *group = 0; 681 desc = ext4_get_group_desc(sb, *group, NULL); 682 if (desc && ext4_free_inodes_count(sb, desc)) 683 return 0; 684 } 685 686 return -1; 687 } 688 689 /* 690 * In no journal mode, if an inode has recently been deleted, we want 691 * to avoid reusing it until we're reasonably sure the inode table 692 * block has been written back to disk. (Yes, these values are 693 * somewhat arbitrary...) 694 */ 695 #define RECENTCY_MIN 5 696 #define RECENTCY_DIRTY 300 697 698 static int recently_deleted(struct super_block *sb, ext4_group_t group, int ino) 699 { 700 struct ext4_group_desc *gdp; 701 struct ext4_inode *raw_inode; 702 struct buffer_head *bh; 703 int inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 704 int offset, ret = 0; 705 int recentcy = RECENTCY_MIN; 706 u32 dtime, now; 707 708 gdp = ext4_get_group_desc(sb, group, NULL); 709 if (unlikely(!gdp)) 710 return 0; 711 712 bh = sb_find_get_block(sb, ext4_inode_table(sb, gdp) + 713 (ino / inodes_per_block)); 714 if (!bh || !buffer_uptodate(bh)) 715 /* 716 * If the block is not in the buffer cache, then it 717 * must have been written out. 718 */ 719 goto out; 720 721 offset = (ino % inodes_per_block) * EXT4_INODE_SIZE(sb); 722 raw_inode = (struct ext4_inode *) (bh->b_data + offset); 723 724 /* i_dtime is only 32 bits on disk, but we only care about relative 725 * times in the range of a few minutes (i.e. long enough to sync a 726 * recently-deleted inode to disk), so using the low 32 bits of the 727 * clock (a 68 year range) is enough, see time_before32() */ 728 dtime = le32_to_cpu(raw_inode->i_dtime); 729 now = ktime_get_real_seconds(); 730 if (buffer_dirty(bh)) 731 recentcy += RECENTCY_DIRTY; 732 733 if (dtime && time_before32(dtime, now) && 734 time_before32(now, dtime + recentcy)) 735 ret = 1; 736 out: 737 brelse(bh); 738 return ret; 739 } 740 741 static int find_inode_bit(struct super_block *sb, ext4_group_t group, 742 struct buffer_head *bitmap, unsigned long *ino) 743 { 744 next: 745 *ino = ext4_find_next_zero_bit((unsigned long *) 746 bitmap->b_data, 747 EXT4_INODES_PER_GROUP(sb), *ino); 748 if (*ino >= EXT4_INODES_PER_GROUP(sb)) 749 return 0; 750 751 if ((EXT4_SB(sb)->s_journal == NULL) && 752 recently_deleted(sb, group, *ino)) { 753 *ino = *ino + 1; 754 if (*ino < EXT4_INODES_PER_GROUP(sb)) 755 goto next; 756 return 0; 757 } 758 759 return 1; 760 } 761 762 /* 763 * There are two policies for allocating an inode. If the new inode is 764 * a directory, then a forward search is made for a block group with both 765 * free space and a low directory-to-inode ratio; if that fails, then of 766 * the groups with above-average free space, that group with the fewest 767 * directories already is chosen. 768 * 769 * For other inodes, search forward from the parent directory's block 770 * group to find a free inode. 771 */ 772 struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir, 773 umode_t mode, const struct qstr *qstr, 774 __u32 goal, uid_t *owner, __u32 i_flags, 775 int handle_type, unsigned int line_no, 776 int nblocks) 777 { 778 struct super_block *sb; 779 struct buffer_head *inode_bitmap_bh = NULL; 780 struct buffer_head *group_desc_bh; 781 ext4_group_t ngroups, group = 0; 782 unsigned long ino = 0; 783 struct inode *inode; 784 struct ext4_group_desc *gdp = NULL; 785 struct ext4_inode_info *ei; 786 struct ext4_sb_info *sbi; 787 int ret2, err; 788 struct inode *ret; 789 ext4_group_t i; 790 ext4_group_t flex_group; 791 struct ext4_group_info *grp; 792 int encrypt = 0; 793 794 /* Cannot create files in a deleted directory */ 795 if (!dir || !dir->i_nlink) 796 return ERR_PTR(-EPERM); 797 798 sb = dir->i_sb; 799 sbi = EXT4_SB(sb); 800 801 if (unlikely(ext4_forced_shutdown(sbi))) 802 return ERR_PTR(-EIO); 803 804 if ((ext4_encrypted_inode(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) && 805 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) && 806 !(i_flags & EXT4_EA_INODE_FL)) { 807 err = fscrypt_get_encryption_info(dir); 808 if (err) 809 return ERR_PTR(err); 810 if (!fscrypt_has_encryption_key(dir)) 811 return ERR_PTR(-ENOKEY); 812 encrypt = 1; 813 } 814 815 if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) { 816 #ifdef CONFIG_EXT4_FS_POSIX_ACL 817 struct posix_acl *p = get_acl(dir, ACL_TYPE_DEFAULT); 818 819 if (IS_ERR(p)) 820 return ERR_CAST(p); 821 if (p) { 822 int acl_size = p->a_count * sizeof(ext4_acl_entry); 823 824 nblocks += (S_ISDIR(mode) ? 2 : 1) * 825 __ext4_xattr_set_credits(sb, NULL /* inode */, 826 NULL /* block_bh */, acl_size, 827 true /* is_create */); 828 posix_acl_release(p); 829 } 830 #endif 831 832 #ifdef CONFIG_SECURITY 833 { 834 int num_security_xattrs = 1; 835 836 #ifdef CONFIG_INTEGRITY 837 num_security_xattrs++; 838 #endif 839 /* 840 * We assume that security xattrs are never 841 * more than 1k. In practice they are under 842 * 128 bytes. 843 */ 844 nblocks += num_security_xattrs * 845 __ext4_xattr_set_credits(sb, NULL /* inode */, 846 NULL /* block_bh */, 1024, 847 true /* is_create */); 848 } 849 #endif 850 if (encrypt) 851 nblocks += __ext4_xattr_set_credits(sb, 852 NULL /* inode */, NULL /* block_bh */, 853 FSCRYPT_SET_CONTEXT_MAX_SIZE, 854 true /* is_create */); 855 } 856 857 ngroups = ext4_get_groups_count(sb); 858 trace_ext4_request_inode(dir, mode); 859 inode = new_inode(sb); 860 if (!inode) 861 return ERR_PTR(-ENOMEM); 862 ei = EXT4_I(inode); 863 864 /* 865 * Initialize owners and quota early so that we don't have to account 866 * for quota initialization worst case in standard inode creating 867 * transaction 868 */ 869 if (owner) { 870 inode->i_mode = mode; 871 i_uid_write(inode, owner[0]); 872 i_gid_write(inode, owner[1]); 873 } else if (test_opt(sb, GRPID)) { 874 inode->i_mode = mode; 875 inode->i_uid = current_fsuid(); 876 inode->i_gid = dir->i_gid; 877 } else 878 inode_init_owner(inode, dir, mode); 879 880 if (ext4_has_feature_project(sb) && 881 ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT)) 882 ei->i_projid = EXT4_I(dir)->i_projid; 883 else 884 ei->i_projid = make_kprojid(&init_user_ns, EXT4_DEF_PROJID); 885 886 err = dquot_initialize(inode); 887 if (err) 888 goto out; 889 890 if (!goal) 891 goal = sbi->s_inode_goal; 892 893 if (goal && goal <= le32_to_cpu(sbi->s_es->s_inodes_count)) { 894 group = (goal - 1) / EXT4_INODES_PER_GROUP(sb); 895 ino = (goal - 1) % EXT4_INODES_PER_GROUP(sb); 896 ret2 = 0; 897 goto got_group; 898 } 899 900 if (S_ISDIR(mode)) 901 ret2 = find_group_orlov(sb, dir, &group, mode, qstr); 902 else 903 ret2 = find_group_other(sb, dir, &group, mode); 904 905 got_group: 906 EXT4_I(dir)->i_last_alloc_group = group; 907 err = -ENOSPC; 908 if (ret2 == -1) 909 goto out; 910 911 /* 912 * Normally we will only go through one pass of this loop, 913 * unless we get unlucky and it turns out the group we selected 914 * had its last inode grabbed by someone else. 915 */ 916 for (i = 0; i < ngroups; i++, ino = 0) { 917 err = -EIO; 918 919 gdp = ext4_get_group_desc(sb, group, &group_desc_bh); 920 if (!gdp) 921 goto out; 922 923 /* 924 * Check free inodes count before loading bitmap. 925 */ 926 if (ext4_free_inodes_count(sb, gdp) == 0) 927 goto next_group; 928 929 grp = ext4_get_group_info(sb, group); 930 /* Skip groups with already-known suspicious inode tables */ 931 if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) 932 goto next_group; 933 934 brelse(inode_bitmap_bh); 935 inode_bitmap_bh = ext4_read_inode_bitmap(sb, group); 936 /* Skip groups with suspicious inode tables */ 937 if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp) || 938 IS_ERR(inode_bitmap_bh)) { 939 inode_bitmap_bh = NULL; 940 goto next_group; 941 } 942 943 repeat_in_this_group: 944 ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino); 945 if (!ret2) 946 goto next_group; 947 948 if (group == 0 && (ino + 1) < EXT4_FIRST_INO(sb)) { 949 ext4_error(sb, "reserved inode found cleared - " 950 "inode=%lu", ino + 1); 951 goto next_group; 952 } 953 954 if (!handle) { 955 BUG_ON(nblocks <= 0); 956 handle = __ext4_journal_start_sb(dir->i_sb, line_no, 957 handle_type, nblocks, 958 0); 959 if (IS_ERR(handle)) { 960 err = PTR_ERR(handle); 961 ext4_std_error(sb, err); 962 goto out; 963 } 964 } 965 BUFFER_TRACE(inode_bitmap_bh, "get_write_access"); 966 err = ext4_journal_get_write_access(handle, inode_bitmap_bh); 967 if (err) { 968 ext4_std_error(sb, err); 969 goto out; 970 } 971 ext4_lock_group(sb, group); 972 ret2 = ext4_test_and_set_bit(ino, inode_bitmap_bh->b_data); 973 if (ret2) { 974 /* Someone already took the bit. Repeat the search 975 * with lock held. 976 */ 977 ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino); 978 if (ret2) { 979 ext4_set_bit(ino, inode_bitmap_bh->b_data); 980 ret2 = 0; 981 } else { 982 ret2 = 1; /* we didn't grab the inode */ 983 } 984 } 985 ext4_unlock_group(sb, group); 986 ino++; /* the inode bitmap is zero-based */ 987 if (!ret2) 988 goto got; /* we grabbed the inode! */ 989 990 if (ino < EXT4_INODES_PER_GROUP(sb)) 991 goto repeat_in_this_group; 992 next_group: 993 if (++group == ngroups) 994 group = 0; 995 } 996 err = -ENOSPC; 997 goto out; 998 999 got: 1000 BUFFER_TRACE(inode_bitmap_bh, "call ext4_handle_dirty_metadata"); 1001 err = ext4_handle_dirty_metadata(handle, NULL, inode_bitmap_bh); 1002 if (err) { 1003 ext4_std_error(sb, err); 1004 goto out; 1005 } 1006 1007 BUFFER_TRACE(group_desc_bh, "get_write_access"); 1008 err = ext4_journal_get_write_access(handle, group_desc_bh); 1009 if (err) { 1010 ext4_std_error(sb, err); 1011 goto out; 1012 } 1013 1014 /* We may have to initialize the block bitmap if it isn't already */ 1015 if (ext4_has_group_desc_csum(sb) && 1016 gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { 1017 struct buffer_head *block_bitmap_bh; 1018 1019 block_bitmap_bh = ext4_read_block_bitmap(sb, group); 1020 if (IS_ERR(block_bitmap_bh)) { 1021 err = PTR_ERR(block_bitmap_bh); 1022 goto out; 1023 } 1024 BUFFER_TRACE(block_bitmap_bh, "get block bitmap access"); 1025 err = ext4_journal_get_write_access(handle, block_bitmap_bh); 1026 if (err) { 1027 brelse(block_bitmap_bh); 1028 ext4_std_error(sb, err); 1029 goto out; 1030 } 1031 1032 BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap"); 1033 err = ext4_handle_dirty_metadata(handle, NULL, block_bitmap_bh); 1034 1035 /* recheck and clear flag under lock if we still need to */ 1036 ext4_lock_group(sb, group); 1037 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { 1038 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT); 1039 ext4_free_group_clusters_set(sb, gdp, 1040 ext4_free_clusters_after_init(sb, group, gdp)); 1041 ext4_block_bitmap_csum_set(sb, group, gdp, 1042 block_bitmap_bh); 1043 ext4_group_desc_csum_set(sb, group, gdp); 1044 } 1045 ext4_unlock_group(sb, group); 1046 brelse(block_bitmap_bh); 1047 1048 if (err) { 1049 ext4_std_error(sb, err); 1050 goto out; 1051 } 1052 } 1053 1054 /* Update the relevant bg descriptor fields */ 1055 if (ext4_has_group_desc_csum(sb)) { 1056 int free; 1057 struct ext4_group_info *grp = ext4_get_group_info(sb, group); 1058 1059 down_read(&grp->alloc_sem); /* protect vs itable lazyinit */ 1060 ext4_lock_group(sb, group); /* while we modify the bg desc */ 1061 free = EXT4_INODES_PER_GROUP(sb) - 1062 ext4_itable_unused_count(sb, gdp); 1063 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) { 1064 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT); 1065 free = 0; 1066 } 1067 /* 1068 * Check the relative inode number against the last used 1069 * relative inode number in this group. if it is greater 1070 * we need to update the bg_itable_unused count 1071 */ 1072 if (ino > free) 1073 ext4_itable_unused_set(sb, gdp, 1074 (EXT4_INODES_PER_GROUP(sb) - ino)); 1075 up_read(&grp->alloc_sem); 1076 } else { 1077 ext4_lock_group(sb, group); 1078 } 1079 1080 ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1); 1081 if (S_ISDIR(mode)) { 1082 ext4_used_dirs_set(sb, gdp, ext4_used_dirs_count(sb, gdp) + 1); 1083 if (sbi->s_log_groups_per_flex) { 1084 ext4_group_t f = ext4_flex_group(sbi, group); 1085 1086 atomic_inc(&sbi->s_flex_groups[f].used_dirs); 1087 } 1088 } 1089 if (ext4_has_group_desc_csum(sb)) { 1090 ext4_inode_bitmap_csum_set(sb, group, gdp, inode_bitmap_bh, 1091 EXT4_INODES_PER_GROUP(sb) / 8); 1092 ext4_group_desc_csum_set(sb, group, gdp); 1093 } 1094 ext4_unlock_group(sb, group); 1095 1096 BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata"); 1097 err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh); 1098 if (err) { 1099 ext4_std_error(sb, err); 1100 goto out; 1101 } 1102 1103 percpu_counter_dec(&sbi->s_freeinodes_counter); 1104 if (S_ISDIR(mode)) 1105 percpu_counter_inc(&sbi->s_dirs_counter); 1106 1107 if (sbi->s_log_groups_per_flex) { 1108 flex_group = ext4_flex_group(sbi, group); 1109 atomic_dec(&sbi->s_flex_groups[flex_group].free_inodes); 1110 } 1111 1112 inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb); 1113 /* This is the optimal IO size (for stat), not the fs block size */ 1114 inode->i_blocks = 0; 1115 inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime = 1116 current_time(inode); 1117 1118 memset(ei->i_data, 0, sizeof(ei->i_data)); 1119 ei->i_dir_start_lookup = 0; 1120 ei->i_disksize = 0; 1121 1122 /* Don't inherit extent flag from directory, amongst others. */ 1123 ei->i_flags = 1124 ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED); 1125 ei->i_flags |= i_flags; 1126 ei->i_file_acl = 0; 1127 ei->i_dtime = 0; 1128 ei->i_block_group = group; 1129 ei->i_last_alloc_group = ~0; 1130 1131 ext4_set_inode_flags(inode); 1132 if (IS_DIRSYNC(inode)) 1133 ext4_handle_sync(handle); 1134 if (insert_inode_locked(inode) < 0) { 1135 /* 1136 * Likely a bitmap corruption causing inode to be allocated 1137 * twice. 1138 */ 1139 err = -EIO; 1140 ext4_error(sb, "failed to insert inode %lu: doubly allocated?", 1141 inode->i_ino); 1142 goto out; 1143 } 1144 inode->i_generation = prandom_u32(); 1145 1146 /* Precompute checksum seed for inode metadata */ 1147 if (ext4_has_metadata_csum(sb)) { 1148 __u32 csum; 1149 __le32 inum = cpu_to_le32(inode->i_ino); 1150 __le32 gen = cpu_to_le32(inode->i_generation); 1151 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, 1152 sizeof(inum)); 1153 ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, 1154 sizeof(gen)); 1155 } 1156 1157 ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ 1158 ext4_set_inode_state(inode, EXT4_STATE_NEW); 1159 1160 ei->i_extra_isize = sbi->s_want_extra_isize; 1161 ei->i_inline_off = 0; 1162 if (ext4_has_feature_inline_data(sb)) 1163 ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); 1164 ret = inode; 1165 err = dquot_alloc_inode(inode); 1166 if (err) 1167 goto fail_drop; 1168 1169 /* 1170 * Since the encryption xattr will always be unique, create it first so 1171 * that it's less likely to end up in an external xattr block and 1172 * prevent its deduplication. 1173 */ 1174 if (encrypt) { 1175 err = fscrypt_inherit_context(dir, inode, handle, true); 1176 if (err) 1177 goto fail_free_drop; 1178 } 1179 1180 if (!(ei->i_flags & EXT4_EA_INODE_FL)) { 1181 err = ext4_init_acl(handle, inode, dir); 1182 if (err) 1183 goto fail_free_drop; 1184 1185 err = ext4_init_security(handle, inode, dir, qstr); 1186 if (err) 1187 goto fail_free_drop; 1188 } 1189 1190 if (ext4_has_feature_extents(sb)) { 1191 /* set extent flag only for directory, file and normal symlink*/ 1192 if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) { 1193 ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS); 1194 ext4_ext_tree_init(handle, inode); 1195 } 1196 } 1197 1198 if (ext4_handle_valid(handle)) { 1199 ei->i_sync_tid = handle->h_transaction->t_tid; 1200 ei->i_datasync_tid = handle->h_transaction->t_tid; 1201 } 1202 1203 err = ext4_mark_inode_dirty(handle, inode); 1204 if (err) { 1205 ext4_std_error(sb, err); 1206 goto fail_free_drop; 1207 } 1208 1209 ext4_debug("allocating inode %lu\n", inode->i_ino); 1210 trace_ext4_allocate_inode(inode, dir, mode); 1211 brelse(inode_bitmap_bh); 1212 return ret; 1213 1214 fail_free_drop: 1215 dquot_free_inode(inode); 1216 fail_drop: 1217 clear_nlink(inode); 1218 unlock_new_inode(inode); 1219 out: 1220 dquot_drop(inode); 1221 inode->i_flags |= S_NOQUOTA; 1222 iput(inode); 1223 brelse(inode_bitmap_bh); 1224 return ERR_PTR(err); 1225 } 1226 1227 /* Verify that we are loading a valid orphan from disk */ 1228 struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino) 1229 { 1230 unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count); 1231 ext4_group_t block_group; 1232 int bit; 1233 struct buffer_head *bitmap_bh = NULL; 1234 struct inode *inode = NULL; 1235 int err = -EFSCORRUPTED; 1236 1237 if (ino < EXT4_FIRST_INO(sb) || ino > max_ino) 1238 goto bad_orphan; 1239 1240 block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); 1241 bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); 1242 bitmap_bh = ext4_read_inode_bitmap(sb, block_group); 1243 if (IS_ERR(bitmap_bh)) { 1244 ext4_error(sb, "inode bitmap error %ld for orphan %lu", 1245 ino, PTR_ERR(bitmap_bh)); 1246 return (struct inode *) bitmap_bh; 1247 } 1248 1249 /* Having the inode bit set should be a 100% indicator that this 1250 * is a valid orphan (no e2fsck run on fs). Orphans also include 1251 * inodes that were being truncated, so we can't check i_nlink==0. 1252 */ 1253 if (!ext4_test_bit(bit, bitmap_bh->b_data)) 1254 goto bad_orphan; 1255 1256 inode = ext4_iget(sb, ino); 1257 if (IS_ERR(inode)) { 1258 err = PTR_ERR(inode); 1259 ext4_error(sb, "couldn't read orphan inode %lu (err %d)", 1260 ino, err); 1261 return inode; 1262 } 1263 1264 /* 1265 * If the orphans has i_nlinks > 0 then it should be able to 1266 * be truncated, otherwise it won't be removed from the orphan 1267 * list during processing and an infinite loop will result. 1268 * Similarly, it must not be a bad inode. 1269 */ 1270 if ((inode->i_nlink && !ext4_can_truncate(inode)) || 1271 is_bad_inode(inode)) 1272 goto bad_orphan; 1273 1274 if (NEXT_ORPHAN(inode) > max_ino) 1275 goto bad_orphan; 1276 brelse(bitmap_bh); 1277 return inode; 1278 1279 bad_orphan: 1280 ext4_error(sb, "bad orphan inode %lu", ino); 1281 if (bitmap_bh) 1282 printk(KERN_ERR "ext4_test_bit(bit=%d, block=%llu) = %d\n", 1283 bit, (unsigned long long)bitmap_bh->b_blocknr, 1284 ext4_test_bit(bit, bitmap_bh->b_data)); 1285 if (inode) { 1286 printk(KERN_ERR "is_bad_inode(inode)=%d\n", 1287 is_bad_inode(inode)); 1288 printk(KERN_ERR "NEXT_ORPHAN(inode)=%u\n", 1289 NEXT_ORPHAN(inode)); 1290 printk(KERN_ERR "max_ino=%lu\n", max_ino); 1291 printk(KERN_ERR "i_nlink=%u\n", inode->i_nlink); 1292 /* Avoid freeing blocks if we got a bad deleted inode */ 1293 if (inode->i_nlink == 0) 1294 inode->i_blocks = 0; 1295 iput(inode); 1296 } 1297 brelse(bitmap_bh); 1298 return ERR_PTR(err); 1299 } 1300 1301 unsigned long ext4_count_free_inodes(struct super_block *sb) 1302 { 1303 unsigned long desc_count; 1304 struct ext4_group_desc *gdp; 1305 ext4_group_t i, ngroups = ext4_get_groups_count(sb); 1306 #ifdef EXT4FS_DEBUG 1307 struct ext4_super_block *es; 1308 unsigned long bitmap_count, x; 1309 struct buffer_head *bitmap_bh = NULL; 1310 1311 es = EXT4_SB(sb)->s_es; 1312 desc_count = 0; 1313 bitmap_count = 0; 1314 gdp = NULL; 1315 for (i = 0; i < ngroups; i++) { 1316 gdp = ext4_get_group_desc(sb, i, NULL); 1317 if (!gdp) 1318 continue; 1319 desc_count += ext4_free_inodes_count(sb, gdp); 1320 brelse(bitmap_bh); 1321 bitmap_bh = ext4_read_inode_bitmap(sb, i); 1322 if (IS_ERR(bitmap_bh)) { 1323 bitmap_bh = NULL; 1324 continue; 1325 } 1326 1327 x = ext4_count_free(bitmap_bh->b_data, 1328 EXT4_INODES_PER_GROUP(sb) / 8); 1329 printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n", 1330 (unsigned long) i, ext4_free_inodes_count(sb, gdp), x); 1331 bitmap_count += x; 1332 } 1333 brelse(bitmap_bh); 1334 printk(KERN_DEBUG "ext4_count_free_inodes: " 1335 "stored = %u, computed = %lu, %lu\n", 1336 le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count); 1337 return desc_count; 1338 #else 1339 desc_count = 0; 1340 for (i = 0; i < ngroups; i++) { 1341 gdp = ext4_get_group_desc(sb, i, NULL); 1342 if (!gdp) 1343 continue; 1344 desc_count += ext4_free_inodes_count(sb, gdp); 1345 cond_resched(); 1346 } 1347 return desc_count; 1348 #endif 1349 } 1350 1351 /* Called at mount-time, super-block is locked */ 1352 unsigned long ext4_count_dirs(struct super_block * sb) 1353 { 1354 unsigned long count = 0; 1355 ext4_group_t i, ngroups = ext4_get_groups_count(sb); 1356 1357 for (i = 0; i < ngroups; i++) { 1358 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL); 1359 if (!gdp) 1360 continue; 1361 count += ext4_used_dirs_count(sb, gdp); 1362 } 1363 return count; 1364 } 1365 1366 /* 1367 * Zeroes not yet zeroed inode table - just write zeroes through the whole 1368 * inode table. Must be called without any spinlock held. The only place 1369 * where it is called from on active part of filesystem is ext4lazyinit 1370 * thread, so we do not need any special locks, however we have to prevent 1371 * inode allocation from the current group, so we take alloc_sem lock, to 1372 * block ext4_new_inode() until we are finished. 1373 */ 1374 int ext4_init_inode_table(struct super_block *sb, ext4_group_t group, 1375 int barrier) 1376 { 1377 struct ext4_group_info *grp = ext4_get_group_info(sb, group); 1378 struct ext4_sb_info *sbi = EXT4_SB(sb); 1379 struct ext4_group_desc *gdp = NULL; 1380 struct buffer_head *group_desc_bh; 1381 handle_t *handle; 1382 ext4_fsblk_t blk; 1383 int num, ret = 0, used_blks = 0; 1384 1385 /* This should not happen, but just to be sure check this */ 1386 if (sb_rdonly(sb)) { 1387 ret = 1; 1388 goto out; 1389 } 1390 1391 gdp = ext4_get_group_desc(sb, group, &group_desc_bh); 1392 if (!gdp) 1393 goto out; 1394 1395 /* 1396 * We do not need to lock this, because we are the only one 1397 * handling this flag. 1398 */ 1399 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)) 1400 goto out; 1401 1402 handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1); 1403 if (IS_ERR(handle)) { 1404 ret = PTR_ERR(handle); 1405 goto out; 1406 } 1407 1408 down_write(&grp->alloc_sem); 1409 /* 1410 * If inode bitmap was already initialized there may be some 1411 * used inodes so we need to skip blocks with used inodes in 1412 * inode table. 1413 */ 1414 if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT))) 1415 used_blks = DIV_ROUND_UP((EXT4_INODES_PER_GROUP(sb) - 1416 ext4_itable_unused_count(sb, gdp)), 1417 sbi->s_inodes_per_block); 1418 1419 if ((used_blks < 0) || (used_blks > sbi->s_itb_per_group)) { 1420 ext4_error(sb, "Something is wrong with group %u: " 1421 "used itable blocks: %d; " 1422 "itable unused count: %u", 1423 group, used_blks, 1424 ext4_itable_unused_count(sb, gdp)); 1425 ret = 1; 1426 goto err_out; 1427 } 1428 1429 blk = ext4_inode_table(sb, gdp) + used_blks; 1430 num = sbi->s_itb_per_group - used_blks; 1431 1432 BUFFER_TRACE(group_desc_bh, "get_write_access"); 1433 ret = ext4_journal_get_write_access(handle, 1434 group_desc_bh); 1435 if (ret) 1436 goto err_out; 1437 1438 /* 1439 * Skip zeroout if the inode table is full. But we set the ZEROED 1440 * flag anyway, because obviously, when it is full it does not need 1441 * further zeroing. 1442 */ 1443 if (unlikely(num == 0)) 1444 goto skip_zeroout; 1445 1446 ext4_debug("going to zero out inode table in group %d\n", 1447 group); 1448 ret = sb_issue_zeroout(sb, blk, num, GFP_NOFS); 1449 if (ret < 0) 1450 goto err_out; 1451 if (barrier) 1452 blkdev_issue_flush(sb->s_bdev, GFP_NOFS, NULL); 1453 1454 skip_zeroout: 1455 ext4_lock_group(sb, group); 1456 gdp->bg_flags |= cpu_to_le16(EXT4_BG_INODE_ZEROED); 1457 ext4_group_desc_csum_set(sb, group, gdp); 1458 ext4_unlock_group(sb, group); 1459 1460 BUFFER_TRACE(group_desc_bh, 1461 "call ext4_handle_dirty_metadata"); 1462 ret = ext4_handle_dirty_metadata(handle, NULL, 1463 group_desc_bh); 1464 1465 err_out: 1466 up_write(&grp->alloc_sem); 1467 ext4_journal_stop(handle); 1468 out: 1469 return ret; 1470 } 1471