1ac27a0ecSDave Kleikamp /* 2617ba13bSMingming Cao * linux/fs/ext4/ialloc.c 3ac27a0ecSDave Kleikamp * 4ac27a0ecSDave Kleikamp * Copyright (C) 1992, 1993, 1994, 1995 5ac27a0ecSDave Kleikamp * Remy Card (card@masi.ibp.fr) 6ac27a0ecSDave Kleikamp * Laboratoire MASI - Institut Blaise Pascal 7ac27a0ecSDave Kleikamp * Universite Pierre et Marie Curie (Paris VI) 8ac27a0ecSDave Kleikamp * 9ac27a0ecSDave Kleikamp * BSD ufs-inspired inode and directory allocation by 10ac27a0ecSDave Kleikamp * Stephen Tweedie (sct@redhat.com), 1993 11ac27a0ecSDave Kleikamp * Big-endian to little-endian byte-swapping/bitmaps by 12ac27a0ecSDave Kleikamp * David S. Miller (davem@caip.rutgers.edu), 1995 13ac27a0ecSDave Kleikamp */ 14ac27a0ecSDave Kleikamp 15ac27a0ecSDave Kleikamp #include <linux/time.h> 16ac27a0ecSDave Kleikamp #include <linux/fs.h> 17dab291afSMingming Cao #include <linux/jbd2.h> 18617ba13bSMingming Cao #include <linux/ext4_fs.h> 19dab291afSMingming Cao #include <linux/ext4_jbd2.h> 20ac27a0ecSDave Kleikamp #include <linux/stat.h> 21ac27a0ecSDave Kleikamp #include <linux/string.h> 22ac27a0ecSDave Kleikamp #include <linux/quotaops.h> 23ac27a0ecSDave Kleikamp #include <linux/buffer_head.h> 24ac27a0ecSDave Kleikamp #include <linux/random.h> 25ac27a0ecSDave Kleikamp #include <linux/bitops.h> 26*3a5b2ecdSMingming Cao #include <linux/blkdev.h> 27ac27a0ecSDave Kleikamp #include <asm/byteorder.h> 28ac27a0ecSDave Kleikamp 29ac27a0ecSDave Kleikamp #include "xattr.h" 30ac27a0ecSDave Kleikamp #include "acl.h" 31ac27a0ecSDave Kleikamp 32ac27a0ecSDave Kleikamp /* 33ac27a0ecSDave Kleikamp * ialloc.c contains the inodes allocation and deallocation routines 34ac27a0ecSDave Kleikamp */ 35ac27a0ecSDave Kleikamp 36ac27a0ecSDave Kleikamp /* 37ac27a0ecSDave Kleikamp * The free inodes are managed by bitmaps. A file system contains several 38ac27a0ecSDave Kleikamp * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap 39ac27a0ecSDave Kleikamp * block for inodes, N blocks for the inode table and data blocks. 40ac27a0ecSDave Kleikamp * 41ac27a0ecSDave Kleikamp * The file system contains group descriptors which are located after the 42ac27a0ecSDave Kleikamp * super block. Each descriptor contains the number of the bitmap block and 43ac27a0ecSDave Kleikamp * the free blocks count in the block. 44ac27a0ecSDave Kleikamp */ 45ac27a0ecSDave Kleikamp 46ac27a0ecSDave Kleikamp 47ac27a0ecSDave Kleikamp /* 48ac27a0ecSDave Kleikamp * Read the inode allocation bitmap for a given block_group, reading 49ac27a0ecSDave Kleikamp * into the specified slot in the superblock's bitmap cache. 50ac27a0ecSDave Kleikamp * 51ac27a0ecSDave Kleikamp * Return buffer_head of bitmap on success or NULL. 52ac27a0ecSDave Kleikamp */ 53ac27a0ecSDave Kleikamp static struct buffer_head * 54ac27a0ecSDave Kleikamp read_inode_bitmap(struct super_block * sb, unsigned long block_group) 55ac27a0ecSDave Kleikamp { 56617ba13bSMingming Cao struct ext4_group_desc *desc; 57ac27a0ecSDave Kleikamp struct buffer_head *bh = NULL; 58ac27a0ecSDave Kleikamp 59617ba13bSMingming Cao desc = ext4_get_group_desc(sb, block_group, NULL); 60ac27a0ecSDave Kleikamp if (!desc) 61ac27a0ecSDave Kleikamp goto error_out; 62ac27a0ecSDave Kleikamp 63ac27a0ecSDave Kleikamp bh = sb_bread(sb, le32_to_cpu(desc->bg_inode_bitmap)); 64ac27a0ecSDave Kleikamp if (!bh) 65617ba13bSMingming Cao ext4_error(sb, "read_inode_bitmap", 66ac27a0ecSDave Kleikamp "Cannot read inode bitmap - " 67ac27a0ecSDave Kleikamp "block_group = %lu, inode_bitmap = %u", 68ac27a0ecSDave Kleikamp block_group, le32_to_cpu(desc->bg_inode_bitmap)); 69ac27a0ecSDave Kleikamp error_out: 70ac27a0ecSDave Kleikamp return bh; 71ac27a0ecSDave Kleikamp } 72ac27a0ecSDave Kleikamp 73ac27a0ecSDave Kleikamp /* 74ac27a0ecSDave Kleikamp * NOTE! When we get the inode, we're the only people 75ac27a0ecSDave Kleikamp * that have access to it, and as such there are no 76ac27a0ecSDave Kleikamp * race conditions we have to worry about. The inode 77ac27a0ecSDave Kleikamp * is not on the hash-lists, and it cannot be reached 78ac27a0ecSDave Kleikamp * through the filesystem because the directory entry 79ac27a0ecSDave Kleikamp * has been deleted earlier. 80ac27a0ecSDave Kleikamp * 81ac27a0ecSDave Kleikamp * HOWEVER: we must make sure that we get no aliases, 82ac27a0ecSDave Kleikamp * which means that we have to call "clear_inode()" 83ac27a0ecSDave Kleikamp * _before_ we mark the inode not in use in the inode 84ac27a0ecSDave Kleikamp * bitmaps. Otherwise a newly created file might use 85ac27a0ecSDave Kleikamp * the same inode number (not actually the same pointer 86ac27a0ecSDave Kleikamp * though), and then we'd have two inodes sharing the 87ac27a0ecSDave Kleikamp * same inode number and space on the harddisk. 88ac27a0ecSDave Kleikamp */ 89617ba13bSMingming Cao void ext4_free_inode (handle_t *handle, struct inode * inode) 90ac27a0ecSDave Kleikamp { 91ac27a0ecSDave Kleikamp struct super_block * sb = inode->i_sb; 92ac27a0ecSDave Kleikamp int is_directory; 93ac27a0ecSDave Kleikamp unsigned long ino; 94ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh = NULL; 95ac27a0ecSDave Kleikamp struct buffer_head *bh2; 96ac27a0ecSDave Kleikamp unsigned long block_group; 97ac27a0ecSDave Kleikamp unsigned long bit; 98617ba13bSMingming Cao struct ext4_group_desc * gdp; 99617ba13bSMingming Cao struct ext4_super_block * es; 100617ba13bSMingming Cao struct ext4_sb_info *sbi; 101ac27a0ecSDave Kleikamp int fatal = 0, err; 102ac27a0ecSDave Kleikamp 103ac27a0ecSDave Kleikamp if (atomic_read(&inode->i_count) > 1) { 104617ba13bSMingming Cao printk ("ext4_free_inode: inode has count=%d\n", 105ac27a0ecSDave Kleikamp atomic_read(&inode->i_count)); 106ac27a0ecSDave Kleikamp return; 107ac27a0ecSDave Kleikamp } 108ac27a0ecSDave Kleikamp if (inode->i_nlink) { 109617ba13bSMingming Cao printk ("ext4_free_inode: inode has nlink=%d\n", 110ac27a0ecSDave Kleikamp inode->i_nlink); 111ac27a0ecSDave Kleikamp return; 112ac27a0ecSDave Kleikamp } 113ac27a0ecSDave Kleikamp if (!sb) { 114617ba13bSMingming Cao printk("ext4_free_inode: inode on nonexistent device\n"); 115ac27a0ecSDave Kleikamp return; 116ac27a0ecSDave Kleikamp } 117617ba13bSMingming Cao sbi = EXT4_SB(sb); 118ac27a0ecSDave Kleikamp 119ac27a0ecSDave Kleikamp ino = inode->i_ino; 120617ba13bSMingming Cao ext4_debug ("freeing inode %lu\n", ino); 121ac27a0ecSDave Kleikamp 122ac27a0ecSDave Kleikamp /* 123ac27a0ecSDave Kleikamp * Note: we must free any quota before locking the superblock, 124ac27a0ecSDave Kleikamp * as writing the quota to disk may need the lock as well. 125ac27a0ecSDave Kleikamp */ 126ac27a0ecSDave Kleikamp DQUOT_INIT(inode); 127617ba13bSMingming Cao ext4_xattr_delete_inode(handle, inode); 128ac27a0ecSDave Kleikamp DQUOT_FREE_INODE(inode); 129ac27a0ecSDave Kleikamp DQUOT_DROP(inode); 130ac27a0ecSDave Kleikamp 131ac27a0ecSDave Kleikamp is_directory = S_ISDIR(inode->i_mode); 132ac27a0ecSDave Kleikamp 133ac27a0ecSDave Kleikamp /* Do this BEFORE marking the inode not in use or returning an error */ 134ac27a0ecSDave Kleikamp clear_inode (inode); 135ac27a0ecSDave Kleikamp 136617ba13bSMingming Cao es = EXT4_SB(sb)->s_es; 137617ba13bSMingming Cao if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) { 138617ba13bSMingming Cao ext4_error (sb, "ext4_free_inode", 139ac27a0ecSDave Kleikamp "reserved or nonexistent inode %lu", ino); 140ac27a0ecSDave Kleikamp goto error_return; 141ac27a0ecSDave Kleikamp } 142617ba13bSMingming Cao block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); 143617ba13bSMingming Cao bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); 144ac27a0ecSDave Kleikamp bitmap_bh = read_inode_bitmap(sb, block_group); 145ac27a0ecSDave Kleikamp if (!bitmap_bh) 146ac27a0ecSDave Kleikamp goto error_return; 147ac27a0ecSDave Kleikamp 148ac27a0ecSDave Kleikamp BUFFER_TRACE(bitmap_bh, "get_write_access"); 149617ba13bSMingming Cao fatal = ext4_journal_get_write_access(handle, bitmap_bh); 150ac27a0ecSDave Kleikamp if (fatal) 151ac27a0ecSDave Kleikamp goto error_return; 152ac27a0ecSDave Kleikamp 153ac27a0ecSDave Kleikamp /* Ok, now we can actually update the inode bitmaps.. */ 154617ba13bSMingming Cao if (!ext4_clear_bit_atomic(sb_bgl_lock(sbi, block_group), 155ac27a0ecSDave Kleikamp bit, bitmap_bh->b_data)) 156617ba13bSMingming Cao ext4_error (sb, "ext4_free_inode", 157ac27a0ecSDave Kleikamp "bit already cleared for inode %lu", ino); 158ac27a0ecSDave Kleikamp else { 159617ba13bSMingming Cao gdp = ext4_get_group_desc (sb, block_group, &bh2); 160ac27a0ecSDave Kleikamp 161ac27a0ecSDave Kleikamp BUFFER_TRACE(bh2, "get_write_access"); 162617ba13bSMingming Cao fatal = ext4_journal_get_write_access(handle, bh2); 163ac27a0ecSDave Kleikamp if (fatal) goto error_return; 164ac27a0ecSDave Kleikamp 165ac27a0ecSDave Kleikamp if (gdp) { 166ac27a0ecSDave Kleikamp spin_lock(sb_bgl_lock(sbi, block_group)); 167ac27a0ecSDave Kleikamp gdp->bg_free_inodes_count = cpu_to_le16( 168ac27a0ecSDave Kleikamp le16_to_cpu(gdp->bg_free_inodes_count) + 1); 169ac27a0ecSDave Kleikamp if (is_directory) 170ac27a0ecSDave Kleikamp gdp->bg_used_dirs_count = cpu_to_le16( 171ac27a0ecSDave Kleikamp le16_to_cpu(gdp->bg_used_dirs_count) - 1); 172ac27a0ecSDave Kleikamp spin_unlock(sb_bgl_lock(sbi, block_group)); 173ac27a0ecSDave Kleikamp percpu_counter_inc(&sbi->s_freeinodes_counter); 174ac27a0ecSDave Kleikamp if (is_directory) 175ac27a0ecSDave Kleikamp percpu_counter_dec(&sbi->s_dirs_counter); 176ac27a0ecSDave Kleikamp 177ac27a0ecSDave Kleikamp } 178617ba13bSMingming Cao BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata"); 179617ba13bSMingming Cao err = ext4_journal_dirty_metadata(handle, bh2); 180ac27a0ecSDave Kleikamp if (!fatal) fatal = err; 181ac27a0ecSDave Kleikamp } 182617ba13bSMingming Cao BUFFER_TRACE(bitmap_bh, "call ext4_journal_dirty_metadata"); 183617ba13bSMingming Cao err = ext4_journal_dirty_metadata(handle, bitmap_bh); 184ac27a0ecSDave Kleikamp if (!fatal) 185ac27a0ecSDave Kleikamp fatal = err; 186ac27a0ecSDave Kleikamp sb->s_dirt = 1; 187ac27a0ecSDave Kleikamp error_return: 188ac27a0ecSDave Kleikamp brelse(bitmap_bh); 189617ba13bSMingming Cao ext4_std_error(sb, fatal); 190ac27a0ecSDave Kleikamp } 191ac27a0ecSDave Kleikamp 192ac27a0ecSDave Kleikamp /* 193ac27a0ecSDave Kleikamp * There are two policies for allocating an inode. If the new inode is 194ac27a0ecSDave Kleikamp * a directory, then a forward search is made for a block group with both 195ac27a0ecSDave Kleikamp * free space and a low directory-to-inode ratio; if that fails, then of 196ac27a0ecSDave Kleikamp * the groups with above-average free space, that group with the fewest 197ac27a0ecSDave Kleikamp * directories already is chosen. 198ac27a0ecSDave Kleikamp * 199ac27a0ecSDave Kleikamp * For other inodes, search forward from the parent directory\'s block 200ac27a0ecSDave Kleikamp * group to find a free inode. 201ac27a0ecSDave Kleikamp */ 202ac27a0ecSDave Kleikamp static int find_group_dir(struct super_block *sb, struct inode *parent) 203ac27a0ecSDave Kleikamp { 204617ba13bSMingming Cao int ngroups = EXT4_SB(sb)->s_groups_count; 205ac27a0ecSDave Kleikamp unsigned int freei, avefreei; 206617ba13bSMingming Cao struct ext4_group_desc *desc, *best_desc = NULL; 207ac27a0ecSDave Kleikamp struct buffer_head *bh; 208ac27a0ecSDave Kleikamp int group, best_group = -1; 209ac27a0ecSDave Kleikamp 210617ba13bSMingming Cao freei = percpu_counter_read_positive(&EXT4_SB(sb)->s_freeinodes_counter); 211ac27a0ecSDave Kleikamp avefreei = freei / ngroups; 212ac27a0ecSDave Kleikamp 213ac27a0ecSDave Kleikamp for (group = 0; group < ngroups; group++) { 214617ba13bSMingming Cao desc = ext4_get_group_desc (sb, group, &bh); 215ac27a0ecSDave Kleikamp if (!desc || !desc->bg_free_inodes_count) 216ac27a0ecSDave Kleikamp continue; 217ac27a0ecSDave Kleikamp if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei) 218ac27a0ecSDave Kleikamp continue; 219ac27a0ecSDave Kleikamp if (!best_desc || 220ac27a0ecSDave Kleikamp (le16_to_cpu(desc->bg_free_blocks_count) > 221ac27a0ecSDave Kleikamp le16_to_cpu(best_desc->bg_free_blocks_count))) { 222ac27a0ecSDave Kleikamp best_group = group; 223ac27a0ecSDave Kleikamp best_desc = desc; 224ac27a0ecSDave Kleikamp } 225ac27a0ecSDave Kleikamp } 226ac27a0ecSDave Kleikamp return best_group; 227ac27a0ecSDave Kleikamp } 228ac27a0ecSDave Kleikamp 229ac27a0ecSDave Kleikamp /* 230ac27a0ecSDave Kleikamp * Orlov's allocator for directories. 231ac27a0ecSDave Kleikamp * 232ac27a0ecSDave Kleikamp * We always try to spread first-level directories. 233ac27a0ecSDave Kleikamp * 234ac27a0ecSDave Kleikamp * If there are blockgroups with both free inodes and free blocks counts 235ac27a0ecSDave Kleikamp * not worse than average we return one with smallest directory count. 236ac27a0ecSDave Kleikamp * Otherwise we simply return a random group. 237ac27a0ecSDave Kleikamp * 238ac27a0ecSDave Kleikamp * For the rest rules look so: 239ac27a0ecSDave Kleikamp * 240ac27a0ecSDave Kleikamp * It's OK to put directory into a group unless 241ac27a0ecSDave Kleikamp * it has too many directories already (max_dirs) or 242ac27a0ecSDave Kleikamp * it has too few free inodes left (min_inodes) or 243ac27a0ecSDave Kleikamp * it has too few free blocks left (min_blocks) or 244ac27a0ecSDave Kleikamp * it's already running too large debt (max_debt). 245ac27a0ecSDave Kleikamp * Parent's group is prefered, if it doesn't satisfy these 246ac27a0ecSDave Kleikamp * conditions we search cyclically through the rest. If none 247ac27a0ecSDave Kleikamp * of the groups look good we just look for a group with more 248ac27a0ecSDave Kleikamp * free inodes than average (starting at parent's group). 249ac27a0ecSDave Kleikamp * 250ac27a0ecSDave Kleikamp * Debt is incremented each time we allocate a directory and decremented 251ac27a0ecSDave Kleikamp * when we allocate an inode, within 0--255. 252ac27a0ecSDave Kleikamp */ 253ac27a0ecSDave Kleikamp 254ac27a0ecSDave Kleikamp #define INODE_COST 64 255ac27a0ecSDave Kleikamp #define BLOCK_COST 256 256ac27a0ecSDave Kleikamp 257ac27a0ecSDave Kleikamp static int find_group_orlov(struct super_block *sb, struct inode *parent) 258ac27a0ecSDave Kleikamp { 259617ba13bSMingming Cao int parent_group = EXT4_I(parent)->i_block_group; 260617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 261617ba13bSMingming Cao struct ext4_super_block *es = sbi->s_es; 262ac27a0ecSDave Kleikamp int ngroups = sbi->s_groups_count; 263617ba13bSMingming Cao int inodes_per_group = EXT4_INODES_PER_GROUP(sb); 264ac27a0ecSDave Kleikamp unsigned int freei, avefreei; 265617ba13bSMingming Cao ext4_fsblk_t freeb, avefreeb; 266617ba13bSMingming Cao ext4_fsblk_t blocks_per_dir; 267ac27a0ecSDave Kleikamp unsigned int ndirs; 268ac27a0ecSDave Kleikamp int max_debt, max_dirs, min_inodes; 269617ba13bSMingming Cao ext4_grpblk_t min_blocks; 270ac27a0ecSDave Kleikamp int group = -1, i; 271617ba13bSMingming Cao struct ext4_group_desc *desc; 272ac27a0ecSDave Kleikamp struct buffer_head *bh; 273ac27a0ecSDave Kleikamp 274ac27a0ecSDave Kleikamp freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter); 275ac27a0ecSDave Kleikamp avefreei = freei / ngroups; 276ac27a0ecSDave Kleikamp freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter); 277*3a5b2ecdSMingming Cao avefreeb = freeb; 278*3a5b2ecdSMingming Cao sector_div(avefreeb, ngroups); 279ac27a0ecSDave Kleikamp ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter); 280ac27a0ecSDave Kleikamp 281ac27a0ecSDave Kleikamp if ((parent == sb->s_root->d_inode) || 282617ba13bSMingming Cao (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL)) { 283ac27a0ecSDave Kleikamp int best_ndir = inodes_per_group; 284ac27a0ecSDave Kleikamp int best_group = -1; 285ac27a0ecSDave Kleikamp 286ac27a0ecSDave Kleikamp get_random_bytes(&group, sizeof(group)); 287ac27a0ecSDave Kleikamp parent_group = (unsigned)group % ngroups; 288ac27a0ecSDave Kleikamp for (i = 0; i < ngroups; i++) { 289ac27a0ecSDave Kleikamp group = (parent_group + i) % ngroups; 290617ba13bSMingming Cao desc = ext4_get_group_desc (sb, group, &bh); 291ac27a0ecSDave Kleikamp if (!desc || !desc->bg_free_inodes_count) 292ac27a0ecSDave Kleikamp continue; 293ac27a0ecSDave Kleikamp if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir) 294ac27a0ecSDave Kleikamp continue; 295ac27a0ecSDave Kleikamp if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei) 296ac27a0ecSDave Kleikamp continue; 297ac27a0ecSDave Kleikamp if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb) 298ac27a0ecSDave Kleikamp continue; 299ac27a0ecSDave Kleikamp best_group = group; 300ac27a0ecSDave Kleikamp best_ndir = le16_to_cpu(desc->bg_used_dirs_count); 301ac27a0ecSDave Kleikamp } 302ac27a0ecSDave Kleikamp if (best_group >= 0) 303ac27a0ecSDave Kleikamp return best_group; 304ac27a0ecSDave Kleikamp goto fallback; 305ac27a0ecSDave Kleikamp } 306ac27a0ecSDave Kleikamp 307*3a5b2ecdSMingming Cao blocks_per_dir = le32_to_cpu(es->s_blocks_count) - freeb; 308*3a5b2ecdSMingming Cao sector_div(blocks_per_dir, ndirs); 309ac27a0ecSDave Kleikamp 310ac27a0ecSDave Kleikamp max_dirs = ndirs / ngroups + inodes_per_group / 16; 311ac27a0ecSDave Kleikamp min_inodes = avefreei - inodes_per_group / 4; 312617ba13bSMingming Cao min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb) / 4; 313ac27a0ecSDave Kleikamp 314*3a5b2ecdSMingming Cao max_debt = EXT4_BLOCKS_PER_GROUP(sb); 315*3a5b2ecdSMingming Cao sector_div(max_debt, max(blocks_per_dir, (ext4_fsblk_t)BLOCK_COST)); 316ac27a0ecSDave Kleikamp if (max_debt * INODE_COST > inodes_per_group) 317ac27a0ecSDave Kleikamp max_debt = inodes_per_group / INODE_COST; 318ac27a0ecSDave Kleikamp if (max_debt > 255) 319ac27a0ecSDave Kleikamp max_debt = 255; 320ac27a0ecSDave Kleikamp if (max_debt == 0) 321ac27a0ecSDave Kleikamp max_debt = 1; 322ac27a0ecSDave Kleikamp 323ac27a0ecSDave Kleikamp for (i = 0; i < ngroups; i++) { 324ac27a0ecSDave Kleikamp group = (parent_group + i) % ngroups; 325617ba13bSMingming Cao desc = ext4_get_group_desc (sb, group, &bh); 326ac27a0ecSDave Kleikamp if (!desc || !desc->bg_free_inodes_count) 327ac27a0ecSDave Kleikamp continue; 328ac27a0ecSDave Kleikamp if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs) 329ac27a0ecSDave Kleikamp continue; 330ac27a0ecSDave Kleikamp if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes) 331ac27a0ecSDave Kleikamp continue; 332ac27a0ecSDave Kleikamp if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks) 333ac27a0ecSDave Kleikamp continue; 334ac27a0ecSDave Kleikamp return group; 335ac27a0ecSDave Kleikamp } 336ac27a0ecSDave Kleikamp 337ac27a0ecSDave Kleikamp fallback: 338ac27a0ecSDave Kleikamp for (i = 0; i < ngroups; i++) { 339ac27a0ecSDave Kleikamp group = (parent_group + i) % ngroups; 340617ba13bSMingming Cao desc = ext4_get_group_desc (sb, group, &bh); 341ac27a0ecSDave Kleikamp if (!desc || !desc->bg_free_inodes_count) 342ac27a0ecSDave Kleikamp continue; 343ac27a0ecSDave Kleikamp if (le16_to_cpu(desc->bg_free_inodes_count) >= avefreei) 344ac27a0ecSDave Kleikamp return group; 345ac27a0ecSDave Kleikamp } 346ac27a0ecSDave Kleikamp 347ac27a0ecSDave Kleikamp if (avefreei) { 348ac27a0ecSDave Kleikamp /* 349ac27a0ecSDave Kleikamp * The free-inodes counter is approximate, and for really small 350ac27a0ecSDave Kleikamp * filesystems the above test can fail to find any blockgroups 351ac27a0ecSDave Kleikamp */ 352ac27a0ecSDave Kleikamp avefreei = 0; 353ac27a0ecSDave Kleikamp goto fallback; 354ac27a0ecSDave Kleikamp } 355ac27a0ecSDave Kleikamp 356ac27a0ecSDave Kleikamp return -1; 357ac27a0ecSDave Kleikamp } 358ac27a0ecSDave Kleikamp 359ac27a0ecSDave Kleikamp static int find_group_other(struct super_block *sb, struct inode *parent) 360ac27a0ecSDave Kleikamp { 361617ba13bSMingming Cao int parent_group = EXT4_I(parent)->i_block_group; 362617ba13bSMingming Cao int ngroups = EXT4_SB(sb)->s_groups_count; 363617ba13bSMingming Cao struct ext4_group_desc *desc; 364ac27a0ecSDave Kleikamp struct buffer_head *bh; 365ac27a0ecSDave Kleikamp int group, i; 366ac27a0ecSDave Kleikamp 367ac27a0ecSDave Kleikamp /* 368ac27a0ecSDave Kleikamp * Try to place the inode in its parent directory 369ac27a0ecSDave Kleikamp */ 370ac27a0ecSDave Kleikamp group = parent_group; 371617ba13bSMingming Cao desc = ext4_get_group_desc (sb, group, &bh); 372ac27a0ecSDave Kleikamp if (desc && le16_to_cpu(desc->bg_free_inodes_count) && 373ac27a0ecSDave Kleikamp le16_to_cpu(desc->bg_free_blocks_count)) 374ac27a0ecSDave Kleikamp return group; 375ac27a0ecSDave Kleikamp 376ac27a0ecSDave Kleikamp /* 377ac27a0ecSDave Kleikamp * We're going to place this inode in a different blockgroup from its 378ac27a0ecSDave Kleikamp * parent. We want to cause files in a common directory to all land in 379ac27a0ecSDave Kleikamp * the same blockgroup. But we want files which are in a different 380ac27a0ecSDave Kleikamp * directory which shares a blockgroup with our parent to land in a 381ac27a0ecSDave Kleikamp * different blockgroup. 382ac27a0ecSDave Kleikamp * 383ac27a0ecSDave Kleikamp * So add our directory's i_ino into the starting point for the hash. 384ac27a0ecSDave Kleikamp */ 385ac27a0ecSDave Kleikamp group = (group + parent->i_ino) % ngroups; 386ac27a0ecSDave Kleikamp 387ac27a0ecSDave Kleikamp /* 388ac27a0ecSDave Kleikamp * Use a quadratic hash to find a group with a free inode and some free 389ac27a0ecSDave Kleikamp * blocks. 390ac27a0ecSDave Kleikamp */ 391ac27a0ecSDave Kleikamp for (i = 1; i < ngroups; i <<= 1) { 392ac27a0ecSDave Kleikamp group += i; 393ac27a0ecSDave Kleikamp if (group >= ngroups) 394ac27a0ecSDave Kleikamp group -= ngroups; 395617ba13bSMingming Cao desc = ext4_get_group_desc (sb, group, &bh); 396ac27a0ecSDave Kleikamp if (desc && le16_to_cpu(desc->bg_free_inodes_count) && 397ac27a0ecSDave Kleikamp le16_to_cpu(desc->bg_free_blocks_count)) 398ac27a0ecSDave Kleikamp return group; 399ac27a0ecSDave Kleikamp } 400ac27a0ecSDave Kleikamp 401ac27a0ecSDave Kleikamp /* 402ac27a0ecSDave Kleikamp * That failed: try linear search for a free inode, even if that group 403ac27a0ecSDave Kleikamp * has no free blocks. 404ac27a0ecSDave Kleikamp */ 405ac27a0ecSDave Kleikamp group = parent_group; 406ac27a0ecSDave Kleikamp for (i = 0; i < ngroups; i++) { 407ac27a0ecSDave Kleikamp if (++group >= ngroups) 408ac27a0ecSDave Kleikamp group = 0; 409617ba13bSMingming Cao desc = ext4_get_group_desc (sb, group, &bh); 410ac27a0ecSDave Kleikamp if (desc && le16_to_cpu(desc->bg_free_inodes_count)) 411ac27a0ecSDave Kleikamp return group; 412ac27a0ecSDave Kleikamp } 413ac27a0ecSDave Kleikamp 414ac27a0ecSDave Kleikamp return -1; 415ac27a0ecSDave Kleikamp } 416ac27a0ecSDave Kleikamp 417ac27a0ecSDave Kleikamp /* 418ac27a0ecSDave Kleikamp * There are two policies for allocating an inode. If the new inode is 419ac27a0ecSDave Kleikamp * a directory, then a forward search is made for a block group with both 420ac27a0ecSDave Kleikamp * free space and a low directory-to-inode ratio; if that fails, then of 421ac27a0ecSDave Kleikamp * the groups with above-average free space, that group with the fewest 422ac27a0ecSDave Kleikamp * directories already is chosen. 423ac27a0ecSDave Kleikamp * 424ac27a0ecSDave Kleikamp * For other inodes, search forward from the parent directory's block 425ac27a0ecSDave Kleikamp * group to find a free inode. 426ac27a0ecSDave Kleikamp */ 427617ba13bSMingming Cao struct inode *ext4_new_inode(handle_t *handle, struct inode * dir, int mode) 428ac27a0ecSDave Kleikamp { 429ac27a0ecSDave Kleikamp struct super_block *sb; 430ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh = NULL; 431ac27a0ecSDave Kleikamp struct buffer_head *bh2; 432ac27a0ecSDave Kleikamp int group; 433ac27a0ecSDave Kleikamp unsigned long ino = 0; 434ac27a0ecSDave Kleikamp struct inode * inode; 435617ba13bSMingming Cao struct ext4_group_desc * gdp = NULL; 436617ba13bSMingming Cao struct ext4_super_block * es; 437617ba13bSMingming Cao struct ext4_inode_info *ei; 438617ba13bSMingming Cao struct ext4_sb_info *sbi; 439ac27a0ecSDave Kleikamp int err = 0; 440ac27a0ecSDave Kleikamp struct inode *ret; 441ac27a0ecSDave Kleikamp int i; 442ac27a0ecSDave Kleikamp 443ac27a0ecSDave Kleikamp /* Cannot create files in a deleted directory */ 444ac27a0ecSDave Kleikamp if (!dir || !dir->i_nlink) 445ac27a0ecSDave Kleikamp return ERR_PTR(-EPERM); 446ac27a0ecSDave Kleikamp 447ac27a0ecSDave Kleikamp sb = dir->i_sb; 448ac27a0ecSDave Kleikamp inode = new_inode(sb); 449ac27a0ecSDave Kleikamp if (!inode) 450ac27a0ecSDave Kleikamp return ERR_PTR(-ENOMEM); 451617ba13bSMingming Cao ei = EXT4_I(inode); 452ac27a0ecSDave Kleikamp 453617ba13bSMingming Cao sbi = EXT4_SB(sb); 454ac27a0ecSDave Kleikamp es = sbi->s_es; 455ac27a0ecSDave Kleikamp if (S_ISDIR(mode)) { 456ac27a0ecSDave Kleikamp if (test_opt (sb, OLDALLOC)) 457ac27a0ecSDave Kleikamp group = find_group_dir(sb, dir); 458ac27a0ecSDave Kleikamp else 459ac27a0ecSDave Kleikamp group = find_group_orlov(sb, dir); 460ac27a0ecSDave Kleikamp } else 461ac27a0ecSDave Kleikamp group = find_group_other(sb, dir); 462ac27a0ecSDave Kleikamp 463ac27a0ecSDave Kleikamp err = -ENOSPC; 464ac27a0ecSDave Kleikamp if (group == -1) 465ac27a0ecSDave Kleikamp goto out; 466ac27a0ecSDave Kleikamp 467ac27a0ecSDave Kleikamp for (i = 0; i < sbi->s_groups_count; i++) { 468ac27a0ecSDave Kleikamp err = -EIO; 469ac27a0ecSDave Kleikamp 470617ba13bSMingming Cao gdp = ext4_get_group_desc(sb, group, &bh2); 471ac27a0ecSDave Kleikamp if (!gdp) 472ac27a0ecSDave Kleikamp goto fail; 473ac27a0ecSDave Kleikamp 474ac27a0ecSDave Kleikamp brelse(bitmap_bh); 475ac27a0ecSDave Kleikamp bitmap_bh = read_inode_bitmap(sb, group); 476ac27a0ecSDave Kleikamp if (!bitmap_bh) 477ac27a0ecSDave Kleikamp goto fail; 478ac27a0ecSDave Kleikamp 479ac27a0ecSDave Kleikamp ino = 0; 480ac27a0ecSDave Kleikamp 481ac27a0ecSDave Kleikamp repeat_in_this_group: 482617ba13bSMingming Cao ino = ext4_find_next_zero_bit((unsigned long *) 483617ba13bSMingming Cao bitmap_bh->b_data, EXT4_INODES_PER_GROUP(sb), ino); 484617ba13bSMingming Cao if (ino < EXT4_INODES_PER_GROUP(sb)) { 485ac27a0ecSDave Kleikamp 486ac27a0ecSDave Kleikamp BUFFER_TRACE(bitmap_bh, "get_write_access"); 487617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, bitmap_bh); 488ac27a0ecSDave Kleikamp if (err) 489ac27a0ecSDave Kleikamp goto fail; 490ac27a0ecSDave Kleikamp 491617ba13bSMingming Cao if (!ext4_set_bit_atomic(sb_bgl_lock(sbi, group), 492ac27a0ecSDave Kleikamp ino, bitmap_bh->b_data)) { 493ac27a0ecSDave Kleikamp /* we won it */ 494ac27a0ecSDave Kleikamp BUFFER_TRACE(bitmap_bh, 495617ba13bSMingming Cao "call ext4_journal_dirty_metadata"); 496617ba13bSMingming Cao err = ext4_journal_dirty_metadata(handle, 497ac27a0ecSDave Kleikamp bitmap_bh); 498ac27a0ecSDave Kleikamp if (err) 499ac27a0ecSDave Kleikamp goto fail; 500ac27a0ecSDave Kleikamp goto got; 501ac27a0ecSDave Kleikamp } 502ac27a0ecSDave Kleikamp /* we lost it */ 503dab291afSMingming Cao jbd2_journal_release_buffer(handle, bitmap_bh); 504ac27a0ecSDave Kleikamp 505617ba13bSMingming Cao if (++ino < EXT4_INODES_PER_GROUP(sb)) 506ac27a0ecSDave Kleikamp goto repeat_in_this_group; 507ac27a0ecSDave Kleikamp } 508ac27a0ecSDave Kleikamp 509ac27a0ecSDave Kleikamp /* 510ac27a0ecSDave Kleikamp * This case is possible in concurrent environment. It is very 511ac27a0ecSDave Kleikamp * rare. We cannot repeat the find_group_xxx() call because 512ac27a0ecSDave Kleikamp * that will simply return the same blockgroup, because the 513ac27a0ecSDave Kleikamp * group descriptor metadata has not yet been updated. 514ac27a0ecSDave Kleikamp * So we just go onto the next blockgroup. 515ac27a0ecSDave Kleikamp */ 516ac27a0ecSDave Kleikamp if (++group == sbi->s_groups_count) 517ac27a0ecSDave Kleikamp group = 0; 518ac27a0ecSDave Kleikamp } 519ac27a0ecSDave Kleikamp err = -ENOSPC; 520ac27a0ecSDave Kleikamp goto out; 521ac27a0ecSDave Kleikamp 522ac27a0ecSDave Kleikamp got: 523617ba13bSMingming Cao ino += group * EXT4_INODES_PER_GROUP(sb) + 1; 524617ba13bSMingming Cao if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) { 525617ba13bSMingming Cao ext4_error (sb, "ext4_new_inode", 526ac27a0ecSDave Kleikamp "reserved inode or inode > inodes count - " 527ac27a0ecSDave Kleikamp "block_group = %d, inode=%lu", group, ino); 528ac27a0ecSDave Kleikamp err = -EIO; 529ac27a0ecSDave Kleikamp goto fail; 530ac27a0ecSDave Kleikamp } 531ac27a0ecSDave Kleikamp 532ac27a0ecSDave Kleikamp BUFFER_TRACE(bh2, "get_write_access"); 533617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, bh2); 534ac27a0ecSDave Kleikamp if (err) goto fail; 535ac27a0ecSDave Kleikamp spin_lock(sb_bgl_lock(sbi, group)); 536ac27a0ecSDave Kleikamp gdp->bg_free_inodes_count = 537ac27a0ecSDave Kleikamp cpu_to_le16(le16_to_cpu(gdp->bg_free_inodes_count) - 1); 538ac27a0ecSDave Kleikamp if (S_ISDIR(mode)) { 539ac27a0ecSDave Kleikamp gdp->bg_used_dirs_count = 540ac27a0ecSDave Kleikamp cpu_to_le16(le16_to_cpu(gdp->bg_used_dirs_count) + 1); 541ac27a0ecSDave Kleikamp } 542ac27a0ecSDave Kleikamp spin_unlock(sb_bgl_lock(sbi, group)); 543617ba13bSMingming Cao BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata"); 544617ba13bSMingming Cao err = ext4_journal_dirty_metadata(handle, bh2); 545ac27a0ecSDave Kleikamp if (err) goto fail; 546ac27a0ecSDave Kleikamp 547ac27a0ecSDave Kleikamp percpu_counter_dec(&sbi->s_freeinodes_counter); 548ac27a0ecSDave Kleikamp if (S_ISDIR(mode)) 549ac27a0ecSDave Kleikamp percpu_counter_inc(&sbi->s_dirs_counter); 550ac27a0ecSDave Kleikamp sb->s_dirt = 1; 551ac27a0ecSDave Kleikamp 552ac27a0ecSDave Kleikamp inode->i_uid = current->fsuid; 553ac27a0ecSDave Kleikamp if (test_opt (sb, GRPID)) 554ac27a0ecSDave Kleikamp inode->i_gid = dir->i_gid; 555ac27a0ecSDave Kleikamp else if (dir->i_mode & S_ISGID) { 556ac27a0ecSDave Kleikamp inode->i_gid = dir->i_gid; 557ac27a0ecSDave Kleikamp if (S_ISDIR(mode)) 558ac27a0ecSDave Kleikamp mode |= S_ISGID; 559ac27a0ecSDave Kleikamp } else 560ac27a0ecSDave Kleikamp inode->i_gid = current->fsgid; 561ac27a0ecSDave Kleikamp inode->i_mode = mode; 562ac27a0ecSDave Kleikamp 563ac27a0ecSDave Kleikamp inode->i_ino = ino; 564ac27a0ecSDave Kleikamp /* This is the optimal IO size (for stat), not the fs block size */ 565ac27a0ecSDave Kleikamp inode->i_blocks = 0; 566ac27a0ecSDave Kleikamp inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC; 567ac27a0ecSDave Kleikamp 568ac27a0ecSDave Kleikamp memset(ei->i_data, 0, sizeof(ei->i_data)); 569ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 570ac27a0ecSDave Kleikamp ei->i_disksize = 0; 571ac27a0ecSDave Kleikamp 572617ba13bSMingming Cao ei->i_flags = EXT4_I(dir)->i_flags & ~EXT4_INDEX_FL; 573ac27a0ecSDave Kleikamp if (S_ISLNK(mode)) 574617ba13bSMingming Cao ei->i_flags &= ~(EXT4_IMMUTABLE_FL|EXT4_APPEND_FL); 575ac27a0ecSDave Kleikamp /* dirsync only applies to directories */ 576ac27a0ecSDave Kleikamp if (!S_ISDIR(mode)) 577617ba13bSMingming Cao ei->i_flags &= ~EXT4_DIRSYNC_FL; 578617ba13bSMingming Cao #ifdef EXT4_FRAGMENTS 579ac27a0ecSDave Kleikamp ei->i_faddr = 0; 580ac27a0ecSDave Kleikamp ei->i_frag_no = 0; 581ac27a0ecSDave Kleikamp ei->i_frag_size = 0; 582ac27a0ecSDave Kleikamp #endif 583ac27a0ecSDave Kleikamp ei->i_file_acl = 0; 584ac27a0ecSDave Kleikamp ei->i_dir_acl = 0; 585ac27a0ecSDave Kleikamp ei->i_dtime = 0; 586ac27a0ecSDave Kleikamp ei->i_block_alloc_info = NULL; 587ac27a0ecSDave Kleikamp ei->i_block_group = group; 588ac27a0ecSDave Kleikamp 589617ba13bSMingming Cao ext4_set_inode_flags(inode); 590ac27a0ecSDave Kleikamp if (IS_DIRSYNC(inode)) 591ac27a0ecSDave Kleikamp handle->h_sync = 1; 592ac27a0ecSDave Kleikamp insert_inode_hash(inode); 593ac27a0ecSDave Kleikamp spin_lock(&sbi->s_next_gen_lock); 594ac27a0ecSDave Kleikamp inode->i_generation = sbi->s_next_generation++; 595ac27a0ecSDave Kleikamp spin_unlock(&sbi->s_next_gen_lock); 596ac27a0ecSDave Kleikamp 597617ba13bSMingming Cao ei->i_state = EXT4_STATE_NEW; 598ac27a0ecSDave Kleikamp ei->i_extra_isize = 599617ba13bSMingming Cao (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) ? 600617ba13bSMingming Cao sizeof(struct ext4_inode) - EXT4_GOOD_OLD_INODE_SIZE : 0; 601ac27a0ecSDave Kleikamp 602ac27a0ecSDave Kleikamp ret = inode; 603ac27a0ecSDave Kleikamp if(DQUOT_ALLOC_INODE(inode)) { 604ac27a0ecSDave Kleikamp err = -EDQUOT; 605ac27a0ecSDave Kleikamp goto fail_drop; 606ac27a0ecSDave Kleikamp } 607ac27a0ecSDave Kleikamp 608617ba13bSMingming Cao err = ext4_init_acl(handle, inode, dir); 609ac27a0ecSDave Kleikamp if (err) 610ac27a0ecSDave Kleikamp goto fail_free_drop; 611ac27a0ecSDave Kleikamp 612617ba13bSMingming Cao err = ext4_init_security(handle,inode, dir); 613ac27a0ecSDave Kleikamp if (err) 614ac27a0ecSDave Kleikamp goto fail_free_drop; 615ac27a0ecSDave Kleikamp 616617ba13bSMingming Cao err = ext4_mark_inode_dirty(handle, inode); 617ac27a0ecSDave Kleikamp if (err) { 618617ba13bSMingming Cao ext4_std_error(sb, err); 619ac27a0ecSDave Kleikamp goto fail_free_drop; 620ac27a0ecSDave Kleikamp } 621a86c6181SAlex Tomas if (test_opt(sb, EXTENTS)) { 622a86c6181SAlex Tomas EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL; 623a86c6181SAlex Tomas ext4_ext_tree_init(handle, inode); 624a86c6181SAlex Tomas if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) { 625a86c6181SAlex Tomas err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh); 626a86c6181SAlex Tomas if (err) goto fail; 627a86c6181SAlex Tomas EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS); 628a86c6181SAlex Tomas BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "call ext4_journal_dirty_metadata"); 629a86c6181SAlex Tomas err = ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh); 630a86c6181SAlex Tomas } 631a86c6181SAlex Tomas } 632ac27a0ecSDave Kleikamp 633617ba13bSMingming Cao ext4_debug("allocating inode %lu\n", inode->i_ino); 634ac27a0ecSDave Kleikamp goto really_out; 635ac27a0ecSDave Kleikamp fail: 636617ba13bSMingming Cao ext4_std_error(sb, err); 637ac27a0ecSDave Kleikamp out: 638ac27a0ecSDave Kleikamp iput(inode); 639ac27a0ecSDave Kleikamp ret = ERR_PTR(err); 640ac27a0ecSDave Kleikamp really_out: 641ac27a0ecSDave Kleikamp brelse(bitmap_bh); 642ac27a0ecSDave Kleikamp return ret; 643ac27a0ecSDave Kleikamp 644ac27a0ecSDave Kleikamp fail_free_drop: 645ac27a0ecSDave Kleikamp DQUOT_FREE_INODE(inode); 646ac27a0ecSDave Kleikamp 647ac27a0ecSDave Kleikamp fail_drop: 648ac27a0ecSDave Kleikamp DQUOT_DROP(inode); 649ac27a0ecSDave Kleikamp inode->i_flags |= S_NOQUOTA; 650ac27a0ecSDave Kleikamp inode->i_nlink = 0; 651ac27a0ecSDave Kleikamp iput(inode); 652ac27a0ecSDave Kleikamp brelse(bitmap_bh); 653ac27a0ecSDave Kleikamp return ERR_PTR(err); 654ac27a0ecSDave Kleikamp } 655ac27a0ecSDave Kleikamp 656ac27a0ecSDave Kleikamp /* Verify that we are loading a valid orphan from disk */ 657617ba13bSMingming Cao struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino) 658ac27a0ecSDave Kleikamp { 659617ba13bSMingming Cao unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count); 660ac27a0ecSDave Kleikamp unsigned long block_group; 661ac27a0ecSDave Kleikamp int bit; 662ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh = NULL; 663ac27a0ecSDave Kleikamp struct inode *inode = NULL; 664ac27a0ecSDave Kleikamp 665ac27a0ecSDave Kleikamp /* Error cases - e2fsck has already cleaned up for us */ 666ac27a0ecSDave Kleikamp if (ino > max_ino) { 667617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, 668ac27a0ecSDave Kleikamp "bad orphan ino %lu! e2fsck was run?", ino); 669ac27a0ecSDave Kleikamp goto out; 670ac27a0ecSDave Kleikamp } 671ac27a0ecSDave Kleikamp 672617ba13bSMingming Cao block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); 673617ba13bSMingming Cao bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); 674ac27a0ecSDave Kleikamp bitmap_bh = read_inode_bitmap(sb, block_group); 675ac27a0ecSDave Kleikamp if (!bitmap_bh) { 676617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, 677ac27a0ecSDave Kleikamp "inode bitmap error for orphan %lu", ino); 678ac27a0ecSDave Kleikamp goto out; 679ac27a0ecSDave Kleikamp } 680ac27a0ecSDave Kleikamp 681ac27a0ecSDave Kleikamp /* Having the inode bit set should be a 100% indicator that this 682ac27a0ecSDave Kleikamp * is a valid orphan (no e2fsck run on fs). Orphans also include 683ac27a0ecSDave Kleikamp * inodes that were being truncated, so we can't check i_nlink==0. 684ac27a0ecSDave Kleikamp */ 685617ba13bSMingming Cao if (!ext4_test_bit(bit, bitmap_bh->b_data) || 686ac27a0ecSDave Kleikamp !(inode = iget(sb, ino)) || is_bad_inode(inode) || 687ac27a0ecSDave Kleikamp NEXT_ORPHAN(inode) > max_ino) { 688617ba13bSMingming Cao ext4_warning(sb, __FUNCTION__, 689ac27a0ecSDave Kleikamp "bad orphan inode %lu! e2fsck was run?", ino); 690617ba13bSMingming Cao printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n", 691ac27a0ecSDave Kleikamp bit, (unsigned long long)bitmap_bh->b_blocknr, 692617ba13bSMingming Cao ext4_test_bit(bit, bitmap_bh->b_data)); 693ac27a0ecSDave Kleikamp printk(KERN_NOTICE "inode=%p\n", inode); 694ac27a0ecSDave Kleikamp if (inode) { 695ac27a0ecSDave Kleikamp printk(KERN_NOTICE "is_bad_inode(inode)=%d\n", 696ac27a0ecSDave Kleikamp is_bad_inode(inode)); 697ac27a0ecSDave Kleikamp printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n", 698ac27a0ecSDave Kleikamp NEXT_ORPHAN(inode)); 699ac27a0ecSDave Kleikamp printk(KERN_NOTICE "max_ino=%lu\n", max_ino); 700ac27a0ecSDave Kleikamp } 701ac27a0ecSDave Kleikamp /* Avoid freeing blocks if we got a bad deleted inode */ 702ac27a0ecSDave Kleikamp if (inode && inode->i_nlink == 0) 703ac27a0ecSDave Kleikamp inode->i_blocks = 0; 704ac27a0ecSDave Kleikamp iput(inode); 705ac27a0ecSDave Kleikamp inode = NULL; 706ac27a0ecSDave Kleikamp } 707ac27a0ecSDave Kleikamp out: 708ac27a0ecSDave Kleikamp brelse(bitmap_bh); 709ac27a0ecSDave Kleikamp return inode; 710ac27a0ecSDave Kleikamp } 711ac27a0ecSDave Kleikamp 712617ba13bSMingming Cao unsigned long ext4_count_free_inodes (struct super_block * sb) 713ac27a0ecSDave Kleikamp { 714ac27a0ecSDave Kleikamp unsigned long desc_count; 715617ba13bSMingming Cao struct ext4_group_desc *gdp; 716ac27a0ecSDave Kleikamp int i; 717617ba13bSMingming Cao #ifdef EXT4FS_DEBUG 718617ba13bSMingming Cao struct ext4_super_block *es; 719ac27a0ecSDave Kleikamp unsigned long bitmap_count, x; 720ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh = NULL; 721ac27a0ecSDave Kleikamp 722617ba13bSMingming Cao es = EXT4_SB(sb)->s_es; 723ac27a0ecSDave Kleikamp desc_count = 0; 724ac27a0ecSDave Kleikamp bitmap_count = 0; 725ac27a0ecSDave Kleikamp gdp = NULL; 726617ba13bSMingming Cao for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) { 727617ba13bSMingming Cao gdp = ext4_get_group_desc (sb, i, NULL); 728ac27a0ecSDave Kleikamp if (!gdp) 729ac27a0ecSDave Kleikamp continue; 730ac27a0ecSDave Kleikamp desc_count += le16_to_cpu(gdp->bg_free_inodes_count); 731ac27a0ecSDave Kleikamp brelse(bitmap_bh); 732ac27a0ecSDave Kleikamp bitmap_bh = read_inode_bitmap(sb, i); 733ac27a0ecSDave Kleikamp if (!bitmap_bh) 734ac27a0ecSDave Kleikamp continue; 735ac27a0ecSDave Kleikamp 736617ba13bSMingming Cao x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8); 737ac27a0ecSDave Kleikamp printk("group %d: stored = %d, counted = %lu\n", 738ac27a0ecSDave Kleikamp i, le16_to_cpu(gdp->bg_free_inodes_count), x); 739ac27a0ecSDave Kleikamp bitmap_count += x; 740ac27a0ecSDave Kleikamp } 741ac27a0ecSDave Kleikamp brelse(bitmap_bh); 742617ba13bSMingming Cao printk("ext4_count_free_inodes: stored = %u, computed = %lu, %lu\n", 743ac27a0ecSDave Kleikamp le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count); 744ac27a0ecSDave Kleikamp return desc_count; 745ac27a0ecSDave Kleikamp #else 746ac27a0ecSDave Kleikamp desc_count = 0; 747617ba13bSMingming Cao for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) { 748617ba13bSMingming Cao gdp = ext4_get_group_desc (sb, i, NULL); 749ac27a0ecSDave Kleikamp if (!gdp) 750ac27a0ecSDave Kleikamp continue; 751ac27a0ecSDave Kleikamp desc_count += le16_to_cpu(gdp->bg_free_inodes_count); 752ac27a0ecSDave Kleikamp cond_resched(); 753ac27a0ecSDave Kleikamp } 754ac27a0ecSDave Kleikamp return desc_count; 755ac27a0ecSDave Kleikamp #endif 756ac27a0ecSDave Kleikamp } 757ac27a0ecSDave Kleikamp 758ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */ 759617ba13bSMingming Cao unsigned long ext4_count_dirs (struct super_block * sb) 760ac27a0ecSDave Kleikamp { 761ac27a0ecSDave Kleikamp unsigned long count = 0; 762ac27a0ecSDave Kleikamp int i; 763ac27a0ecSDave Kleikamp 764617ba13bSMingming Cao for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) { 765617ba13bSMingming Cao struct ext4_group_desc *gdp = ext4_get_group_desc (sb, i, NULL); 766ac27a0ecSDave Kleikamp if (!gdp) 767ac27a0ecSDave Kleikamp continue; 768ac27a0ecSDave Kleikamp count += le16_to_cpu(gdp->bg_used_dirs_count); 769ac27a0ecSDave Kleikamp } 770ac27a0ecSDave Kleikamp return count; 771ac27a0ecSDave Kleikamp } 772ac27a0ecSDave Kleikamp 773