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> 18ac27a0ecSDave Kleikamp #include <linux/stat.h> 19ac27a0ecSDave Kleikamp #include <linux/string.h> 20ac27a0ecSDave Kleikamp #include <linux/quotaops.h> 21ac27a0ecSDave Kleikamp #include <linux/buffer_head.h> 22ac27a0ecSDave Kleikamp #include <linux/random.h> 23ac27a0ecSDave Kleikamp #include <linux/bitops.h> 243a5b2ecdSMingming Cao #include <linux/blkdev.h> 25ac27a0ecSDave Kleikamp #include <asm/byteorder.h> 263dcf5451SChristoph Hellwig #include "ext4.h" 273dcf5451SChristoph Hellwig #include "ext4_jbd2.h" 28ac27a0ecSDave Kleikamp #include "xattr.h" 29ac27a0ecSDave Kleikamp #include "acl.h" 30717d50e4SAndreas Dilger #include "group.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 46717d50e4SAndreas Dilger /* 47717d50e4SAndreas Dilger * To avoid calling the atomic setbit hundreds or thousands of times, we only 48717d50e4SAndreas Dilger * need to use it within a single byte (to ensure we get endianness right). 49717d50e4SAndreas Dilger * We can use memset for the rest of the bitmap as there are no other users. 50717d50e4SAndreas Dilger */ 51717d50e4SAndreas Dilger void mark_bitmap_end(int start_bit, int end_bit, char *bitmap) 52717d50e4SAndreas Dilger { 53717d50e4SAndreas Dilger int i; 54717d50e4SAndreas Dilger 55717d50e4SAndreas Dilger if (start_bit >= end_bit) 56717d50e4SAndreas Dilger return; 57717d50e4SAndreas Dilger 58717d50e4SAndreas Dilger ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit); 59717d50e4SAndreas Dilger for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++) 60717d50e4SAndreas Dilger ext4_set_bit(i, bitmap); 61717d50e4SAndreas Dilger if (i < end_bit) 62717d50e4SAndreas Dilger memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3); 63717d50e4SAndreas Dilger } 64717d50e4SAndreas Dilger 65717d50e4SAndreas Dilger /* Initializes an uninitialized inode bitmap */ 66fd2d4291SAvantika Mathur unsigned ext4_init_inode_bitmap(struct super_block *sb, struct buffer_head *bh, 67fd2d4291SAvantika Mathur ext4_group_t block_group, 68717d50e4SAndreas Dilger struct ext4_group_desc *gdp) 69717d50e4SAndreas Dilger { 70717d50e4SAndreas Dilger struct ext4_sb_info *sbi = EXT4_SB(sb); 71717d50e4SAndreas Dilger 72717d50e4SAndreas Dilger J_ASSERT_BH(bh, buffer_locked(bh)); 73717d50e4SAndreas Dilger 74717d50e4SAndreas Dilger /* If checksum is bad mark all blocks and inodes use to prevent 75717d50e4SAndreas Dilger * allocation, essentially implementing a per-group read-only flag. */ 76717d50e4SAndreas Dilger if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) { 77a9df9a49STheodore Ts'o ext4_error(sb, __func__, "Checksum bad for group %u", 78717d50e4SAndreas Dilger block_group); 79560671a0SAneesh Kumar K.V ext4_free_blks_set(sb, gdp, 0); 80560671a0SAneesh Kumar K.V ext4_free_inodes_set(sb, gdp, 0); 81560671a0SAneesh Kumar K.V ext4_itable_unused_set(sb, gdp, 0); 82717d50e4SAndreas Dilger memset(bh->b_data, 0xff, sb->s_blocksize); 83717d50e4SAndreas Dilger return 0; 84717d50e4SAndreas Dilger } 85717d50e4SAndreas Dilger 86717d50e4SAndreas Dilger memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8); 87717d50e4SAndreas Dilger mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), EXT4_BLOCKS_PER_GROUP(sb), 88717d50e4SAndreas Dilger bh->b_data); 89717d50e4SAndreas Dilger 90717d50e4SAndreas Dilger return EXT4_INODES_PER_GROUP(sb); 91717d50e4SAndreas Dilger } 92ac27a0ecSDave Kleikamp 93ac27a0ecSDave Kleikamp /* 94ac27a0ecSDave Kleikamp * Read the inode allocation bitmap for a given block_group, reading 95ac27a0ecSDave Kleikamp * into the specified slot in the superblock's bitmap cache. 96ac27a0ecSDave Kleikamp * 97ac27a0ecSDave Kleikamp * Return buffer_head of bitmap on success or NULL. 98ac27a0ecSDave Kleikamp */ 99ac27a0ecSDave Kleikamp static struct buffer_head * 100e29d1cdeSEric Sandeen ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group) 101ac27a0ecSDave Kleikamp { 102617ba13bSMingming Cao struct ext4_group_desc *desc; 103ac27a0ecSDave Kleikamp struct buffer_head *bh = NULL; 104e29d1cdeSEric Sandeen ext4_fsblk_t bitmap_blk; 105ac27a0ecSDave Kleikamp 106617ba13bSMingming Cao desc = ext4_get_group_desc(sb, block_group, NULL); 107ac27a0ecSDave Kleikamp if (!desc) 108e29d1cdeSEric Sandeen return NULL; 109e29d1cdeSEric Sandeen bitmap_blk = ext4_inode_bitmap(sb, desc); 110e29d1cdeSEric Sandeen bh = sb_getblk(sb, bitmap_blk); 111e29d1cdeSEric Sandeen if (unlikely(!bh)) { 112e29d1cdeSEric Sandeen ext4_error(sb, __func__, 113ac27a0ecSDave Kleikamp "Cannot read inode bitmap - " 114a9df9a49STheodore Ts'o "block_group = %u, inode_bitmap = %llu", 115e29d1cdeSEric Sandeen block_group, bitmap_blk); 116e29d1cdeSEric Sandeen return NULL; 117e29d1cdeSEric Sandeen } 118*2ccb5fb9SAneesh Kumar K.V if (bitmap_uptodate(bh)) 119e29d1cdeSEric Sandeen return bh; 120e29d1cdeSEric Sandeen 121c806e68fSFrederic Bohe lock_buffer(bh); 122*2ccb5fb9SAneesh Kumar K.V if (bitmap_uptodate(bh)) { 123*2ccb5fb9SAneesh Kumar K.V unlock_buffer(bh); 124*2ccb5fb9SAneesh Kumar K.V return bh; 125*2ccb5fb9SAneesh Kumar K.V } 126b5f10eedSEric Sandeen spin_lock(sb_bgl_lock(EXT4_SB(sb), block_group)); 127e29d1cdeSEric Sandeen if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) { 128e29d1cdeSEric Sandeen ext4_init_inode_bitmap(sb, bh, block_group, desc); 129*2ccb5fb9SAneesh Kumar K.V set_bitmap_uptodate(bh); 130e29d1cdeSEric Sandeen set_buffer_uptodate(bh); 131b5f10eedSEric Sandeen spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group)); 1323300bedaSAneesh Kumar K.V unlock_buffer(bh); 133e29d1cdeSEric Sandeen return bh; 134e29d1cdeSEric Sandeen } 135b5f10eedSEric Sandeen spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group)); 136*2ccb5fb9SAneesh Kumar K.V if (buffer_uptodate(bh)) { 137*2ccb5fb9SAneesh Kumar K.V /* 138*2ccb5fb9SAneesh Kumar K.V * if not uninit if bh is uptodate, 139*2ccb5fb9SAneesh Kumar K.V * bitmap is also uptodate 140*2ccb5fb9SAneesh Kumar K.V */ 141*2ccb5fb9SAneesh Kumar K.V set_bitmap_uptodate(bh); 142*2ccb5fb9SAneesh Kumar K.V unlock_buffer(bh); 143*2ccb5fb9SAneesh Kumar K.V return bh; 144*2ccb5fb9SAneesh Kumar K.V } 145*2ccb5fb9SAneesh Kumar K.V /* 146*2ccb5fb9SAneesh Kumar K.V * submit the buffer_head for read. We can 147*2ccb5fb9SAneesh Kumar K.V * safely mark the bitmap as uptodate now. 148*2ccb5fb9SAneesh Kumar K.V * We do it here so the bitmap uptodate bit 149*2ccb5fb9SAneesh Kumar K.V * get set with buffer lock held. 150*2ccb5fb9SAneesh Kumar K.V */ 151*2ccb5fb9SAneesh Kumar K.V set_bitmap_uptodate(bh); 152e29d1cdeSEric Sandeen if (bh_submit_read(bh) < 0) { 153e29d1cdeSEric Sandeen put_bh(bh); 154e29d1cdeSEric Sandeen ext4_error(sb, __func__, 155e29d1cdeSEric Sandeen "Cannot read inode bitmap - " 156a9df9a49STheodore Ts'o "block_group = %u, inode_bitmap = %llu", 157e29d1cdeSEric Sandeen block_group, bitmap_blk); 158e29d1cdeSEric Sandeen return NULL; 159e29d1cdeSEric Sandeen } 160ac27a0ecSDave Kleikamp return bh; 161ac27a0ecSDave Kleikamp } 162ac27a0ecSDave Kleikamp 163ac27a0ecSDave Kleikamp /* 164ac27a0ecSDave Kleikamp * NOTE! When we get the inode, we're the only people 165ac27a0ecSDave Kleikamp * that have access to it, and as such there are no 166ac27a0ecSDave Kleikamp * race conditions we have to worry about. The inode 167ac27a0ecSDave Kleikamp * is not on the hash-lists, and it cannot be reached 168ac27a0ecSDave Kleikamp * through the filesystem because the directory entry 169ac27a0ecSDave Kleikamp * has been deleted earlier. 170ac27a0ecSDave Kleikamp * 171ac27a0ecSDave Kleikamp * HOWEVER: we must make sure that we get no aliases, 172ac27a0ecSDave Kleikamp * which means that we have to call "clear_inode()" 173ac27a0ecSDave Kleikamp * _before_ we mark the inode not in use in the inode 174ac27a0ecSDave Kleikamp * bitmaps. Otherwise a newly created file might use 175ac27a0ecSDave Kleikamp * the same inode number (not actually the same pointer 176ac27a0ecSDave Kleikamp * though), and then we'd have two inodes sharing the 177ac27a0ecSDave Kleikamp * same inode number and space on the harddisk. 178ac27a0ecSDave Kleikamp */ 179617ba13bSMingming Cao void ext4_free_inode(handle_t *handle, struct inode *inode) 180ac27a0ecSDave Kleikamp { 181ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 182ac27a0ecSDave Kleikamp int is_directory; 183ac27a0ecSDave Kleikamp unsigned long ino; 184ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh = NULL; 185ac27a0ecSDave Kleikamp struct buffer_head *bh2; 186fd2d4291SAvantika Mathur ext4_group_t block_group; 187ac27a0ecSDave Kleikamp unsigned long bit; 188617ba13bSMingming Cao struct ext4_group_desc *gdp; 189617ba13bSMingming Cao struct ext4_super_block *es; 190617ba13bSMingming Cao struct ext4_sb_info *sbi; 191560671a0SAneesh Kumar K.V int fatal = 0, err, count; 192772cb7c8SJose R. Santos ext4_group_t flex_group; 193ac27a0ecSDave Kleikamp 194ac27a0ecSDave Kleikamp if (atomic_read(&inode->i_count) > 1) { 1954776004fSTheodore Ts'o printk(KERN_ERR "ext4_free_inode: inode has count=%d\n", 196ac27a0ecSDave Kleikamp atomic_read(&inode->i_count)); 197ac27a0ecSDave Kleikamp return; 198ac27a0ecSDave Kleikamp } 199ac27a0ecSDave Kleikamp if (inode->i_nlink) { 2004776004fSTheodore Ts'o printk(KERN_ERR "ext4_free_inode: inode has nlink=%d\n", 201ac27a0ecSDave Kleikamp inode->i_nlink); 202ac27a0ecSDave Kleikamp return; 203ac27a0ecSDave Kleikamp } 204ac27a0ecSDave Kleikamp if (!sb) { 2054776004fSTheodore Ts'o printk(KERN_ERR "ext4_free_inode: inode on " 2064776004fSTheodore Ts'o "nonexistent device\n"); 207ac27a0ecSDave Kleikamp return; 208ac27a0ecSDave Kleikamp } 209617ba13bSMingming Cao sbi = EXT4_SB(sb); 210ac27a0ecSDave Kleikamp 211ac27a0ecSDave Kleikamp ino = inode->i_ino; 212617ba13bSMingming Cao ext4_debug("freeing inode %lu\n", ino); 213ac27a0ecSDave Kleikamp 214ac27a0ecSDave Kleikamp /* 215ac27a0ecSDave Kleikamp * Note: we must free any quota before locking the superblock, 216ac27a0ecSDave Kleikamp * as writing the quota to disk may need the lock as well. 217ac27a0ecSDave Kleikamp */ 218ac27a0ecSDave Kleikamp DQUOT_INIT(inode); 219617ba13bSMingming Cao ext4_xattr_delete_inode(handle, inode); 220ac27a0ecSDave Kleikamp DQUOT_FREE_INODE(inode); 221ac27a0ecSDave Kleikamp DQUOT_DROP(inode); 222ac27a0ecSDave Kleikamp 223ac27a0ecSDave Kleikamp is_directory = S_ISDIR(inode->i_mode); 224ac27a0ecSDave Kleikamp 225ac27a0ecSDave Kleikamp /* Do this BEFORE marking the inode not in use or returning an error */ 226ac27a0ecSDave Kleikamp clear_inode(inode); 227ac27a0ecSDave Kleikamp 228617ba13bSMingming Cao es = EXT4_SB(sb)->s_es; 229617ba13bSMingming Cao if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) { 230617ba13bSMingming Cao ext4_error(sb, "ext4_free_inode", 231ac27a0ecSDave Kleikamp "reserved or nonexistent inode %lu", ino); 232ac27a0ecSDave Kleikamp goto error_return; 233ac27a0ecSDave Kleikamp } 234617ba13bSMingming Cao block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); 235617ba13bSMingming Cao bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); 236e29d1cdeSEric Sandeen bitmap_bh = ext4_read_inode_bitmap(sb, block_group); 237ac27a0ecSDave Kleikamp if (!bitmap_bh) 238ac27a0ecSDave Kleikamp goto error_return; 239ac27a0ecSDave Kleikamp 240ac27a0ecSDave Kleikamp BUFFER_TRACE(bitmap_bh, "get_write_access"); 241617ba13bSMingming Cao fatal = ext4_journal_get_write_access(handle, bitmap_bh); 242ac27a0ecSDave Kleikamp if (fatal) 243ac27a0ecSDave Kleikamp goto error_return; 244ac27a0ecSDave Kleikamp 245ac27a0ecSDave Kleikamp /* Ok, now we can actually update the inode bitmaps.. */ 246617ba13bSMingming Cao if (!ext4_clear_bit_atomic(sb_bgl_lock(sbi, block_group), 247ac27a0ecSDave Kleikamp bit, bitmap_bh->b_data)) 248617ba13bSMingming Cao ext4_error(sb, "ext4_free_inode", 249ac27a0ecSDave Kleikamp "bit already cleared for inode %lu", ino); 250ac27a0ecSDave Kleikamp else { 251617ba13bSMingming Cao gdp = ext4_get_group_desc(sb, block_group, &bh2); 252ac27a0ecSDave Kleikamp 253ac27a0ecSDave Kleikamp BUFFER_TRACE(bh2, "get_write_access"); 254617ba13bSMingming Cao fatal = ext4_journal_get_write_access(handle, bh2); 255ac27a0ecSDave Kleikamp if (fatal) goto error_return; 256ac27a0ecSDave Kleikamp 257ac27a0ecSDave Kleikamp if (gdp) { 258ac27a0ecSDave Kleikamp spin_lock(sb_bgl_lock(sbi, block_group)); 259560671a0SAneesh Kumar K.V count = ext4_free_inodes_count(sb, gdp) + 1; 260560671a0SAneesh Kumar K.V ext4_free_inodes_set(sb, gdp, count); 261560671a0SAneesh Kumar K.V if (is_directory) { 262560671a0SAneesh Kumar K.V count = ext4_used_dirs_count(sb, gdp) - 1; 263560671a0SAneesh Kumar K.V ext4_used_dirs_set(sb, gdp, count); 264560671a0SAneesh Kumar K.V } 265717d50e4SAndreas Dilger gdp->bg_checksum = ext4_group_desc_csum(sbi, 266717d50e4SAndreas Dilger block_group, gdp); 267ac27a0ecSDave Kleikamp spin_unlock(sb_bgl_lock(sbi, block_group)); 268ac27a0ecSDave Kleikamp percpu_counter_inc(&sbi->s_freeinodes_counter); 269ac27a0ecSDave Kleikamp if (is_directory) 270ac27a0ecSDave Kleikamp percpu_counter_dec(&sbi->s_dirs_counter); 271ac27a0ecSDave Kleikamp 272772cb7c8SJose R. Santos if (sbi->s_log_groups_per_flex) { 273772cb7c8SJose R. Santos flex_group = ext4_flex_group(sbi, block_group); 274772cb7c8SJose R. Santos spin_lock(sb_bgl_lock(sbi, flex_group)); 275772cb7c8SJose R. Santos sbi->s_flex_groups[flex_group].free_inodes++; 276772cb7c8SJose R. Santos spin_unlock(sb_bgl_lock(sbi, flex_group)); 277772cb7c8SJose R. Santos } 278ac27a0ecSDave Kleikamp } 2790390131bSFrank Mayhar BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata"); 2800390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, NULL, bh2); 281ac27a0ecSDave Kleikamp if (!fatal) fatal = err; 282ac27a0ecSDave Kleikamp } 2830390131bSFrank Mayhar BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata"); 2840390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh); 285ac27a0ecSDave Kleikamp if (!fatal) 286ac27a0ecSDave Kleikamp fatal = err; 287ac27a0ecSDave Kleikamp sb->s_dirt = 1; 288ac27a0ecSDave Kleikamp error_return: 289ac27a0ecSDave Kleikamp brelse(bitmap_bh); 290617ba13bSMingming Cao ext4_std_error(sb, fatal); 291ac27a0ecSDave Kleikamp } 292ac27a0ecSDave Kleikamp 293ac27a0ecSDave Kleikamp /* 294ac27a0ecSDave Kleikamp * There are two policies for allocating an inode. If the new inode is 295ac27a0ecSDave Kleikamp * a directory, then a forward search is made for a block group with both 296ac27a0ecSDave Kleikamp * free space and a low directory-to-inode ratio; if that fails, then of 297ac27a0ecSDave Kleikamp * the groups with above-average free space, that group with the fewest 298ac27a0ecSDave Kleikamp * directories already is chosen. 299ac27a0ecSDave Kleikamp * 300ac27a0ecSDave Kleikamp * For other inodes, search forward from the parent directory\'s block 301ac27a0ecSDave Kleikamp * group to find a free inode. 302ac27a0ecSDave Kleikamp */ 3032aa9fc4cSAvantika Mathur static int find_group_dir(struct super_block *sb, struct inode *parent, 3042aa9fc4cSAvantika Mathur ext4_group_t *best_group) 305ac27a0ecSDave Kleikamp { 306fd2d4291SAvantika Mathur ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count; 307ac27a0ecSDave Kleikamp unsigned int freei, avefreei; 308617ba13bSMingming Cao struct ext4_group_desc *desc, *best_desc = NULL; 3092aa9fc4cSAvantika Mathur ext4_group_t group; 3102aa9fc4cSAvantika Mathur int ret = -1; 311ac27a0ecSDave Kleikamp 312617ba13bSMingming Cao freei = percpu_counter_read_positive(&EXT4_SB(sb)->s_freeinodes_counter); 313ac27a0ecSDave Kleikamp avefreei = freei / ngroups; 314ac27a0ecSDave Kleikamp 315ac27a0ecSDave Kleikamp for (group = 0; group < ngroups; group++) { 316ef2fb679SEric Sandeen desc = ext4_get_group_desc(sb, group, NULL); 317560671a0SAneesh Kumar K.V if (!desc || !ext4_free_inodes_count(sb, desc)) 318ac27a0ecSDave Kleikamp continue; 319560671a0SAneesh Kumar K.V if (ext4_free_inodes_count(sb, desc) < avefreei) 320ac27a0ecSDave Kleikamp continue; 321ac27a0ecSDave Kleikamp if (!best_desc || 322560671a0SAneesh Kumar K.V (ext4_free_blks_count(sb, desc) > 323560671a0SAneesh Kumar K.V ext4_free_blks_count(sb, best_desc))) { 3242aa9fc4cSAvantika Mathur *best_group = group; 325ac27a0ecSDave Kleikamp best_desc = desc; 3262aa9fc4cSAvantika Mathur ret = 0; 327ac27a0ecSDave Kleikamp } 328ac27a0ecSDave Kleikamp } 3292aa9fc4cSAvantika Mathur return ret; 330ac27a0ecSDave Kleikamp } 331ac27a0ecSDave Kleikamp 332772cb7c8SJose R. Santos #define free_block_ratio 10 333772cb7c8SJose R. Santos 334772cb7c8SJose R. Santos static int find_group_flex(struct super_block *sb, struct inode *parent, 335772cb7c8SJose R. Santos ext4_group_t *best_group) 336772cb7c8SJose R. Santos { 337772cb7c8SJose R. Santos struct ext4_sb_info *sbi = EXT4_SB(sb); 338772cb7c8SJose R. Santos struct ext4_group_desc *desc; 339772cb7c8SJose R. Santos struct buffer_head *bh; 340772cb7c8SJose R. Santos struct flex_groups *flex_group = sbi->s_flex_groups; 341772cb7c8SJose R. Santos ext4_group_t parent_group = EXT4_I(parent)->i_block_group; 342772cb7c8SJose R. Santos ext4_group_t parent_fbg_group = ext4_flex_group(sbi, parent_group); 343772cb7c8SJose R. Santos ext4_group_t ngroups = sbi->s_groups_count; 344772cb7c8SJose R. Santos int flex_size = ext4_flex_bg_size(sbi); 345772cb7c8SJose R. Santos ext4_group_t best_flex = parent_fbg_group; 346772cb7c8SJose R. Santos int blocks_per_flex = sbi->s_blocks_per_group * flex_size; 347772cb7c8SJose R. Santos int flexbg_free_blocks; 348772cb7c8SJose R. Santos int flex_freeb_ratio; 349772cb7c8SJose R. Santos ext4_group_t n_fbg_groups; 350772cb7c8SJose R. Santos ext4_group_t i; 351772cb7c8SJose R. Santos 352772cb7c8SJose R. Santos n_fbg_groups = (sbi->s_groups_count + flex_size - 1) >> 353772cb7c8SJose R. Santos sbi->s_log_groups_per_flex; 354772cb7c8SJose R. Santos 355772cb7c8SJose R. Santos find_close_to_parent: 356772cb7c8SJose R. Santos flexbg_free_blocks = flex_group[best_flex].free_blocks; 357772cb7c8SJose R. Santos flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex; 358772cb7c8SJose R. Santos if (flex_group[best_flex].free_inodes && 359772cb7c8SJose R. Santos flex_freeb_ratio > free_block_ratio) 360772cb7c8SJose R. Santos goto found_flexbg; 361772cb7c8SJose R. Santos 362772cb7c8SJose R. Santos if (best_flex && best_flex == parent_fbg_group) { 363772cb7c8SJose R. Santos best_flex--; 364772cb7c8SJose R. Santos goto find_close_to_parent; 365772cb7c8SJose R. Santos } 366772cb7c8SJose R. Santos 367772cb7c8SJose R. Santos for (i = 0; i < n_fbg_groups; i++) { 368772cb7c8SJose R. Santos if (i == parent_fbg_group || i == parent_fbg_group - 1) 369772cb7c8SJose R. Santos continue; 370772cb7c8SJose R. Santos 371772cb7c8SJose R. Santos flexbg_free_blocks = flex_group[i].free_blocks; 372772cb7c8SJose R. Santos flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex; 373772cb7c8SJose R. Santos 374772cb7c8SJose R. Santos if (flex_freeb_ratio > free_block_ratio && 375772cb7c8SJose R. Santos flex_group[i].free_inodes) { 376772cb7c8SJose R. Santos best_flex = i; 377772cb7c8SJose R. Santos goto found_flexbg; 378772cb7c8SJose R. Santos } 379772cb7c8SJose R. Santos 380c001077fSEric Sandeen if (flex_group[best_flex].free_inodes == 0 || 381772cb7c8SJose R. Santos (flex_group[i].free_blocks > 382772cb7c8SJose R. Santos flex_group[best_flex].free_blocks && 383772cb7c8SJose R. Santos flex_group[i].free_inodes)) 384772cb7c8SJose R. Santos best_flex = i; 385772cb7c8SJose R. Santos } 386772cb7c8SJose R. Santos 387772cb7c8SJose R. Santos if (!flex_group[best_flex].free_inodes || 388772cb7c8SJose R. Santos !flex_group[best_flex].free_blocks) 389772cb7c8SJose R. Santos return -1; 390772cb7c8SJose R. Santos 391772cb7c8SJose R. Santos found_flexbg: 392772cb7c8SJose R. Santos for (i = best_flex * flex_size; i < ngroups && 393772cb7c8SJose R. Santos i < (best_flex + 1) * flex_size; i++) { 394772cb7c8SJose R. Santos desc = ext4_get_group_desc(sb, i, &bh); 395560671a0SAneesh Kumar K.V if (ext4_free_inodes_count(sb, desc)) { 396772cb7c8SJose R. Santos *best_group = i; 397772cb7c8SJose R. Santos goto out; 398772cb7c8SJose R. Santos } 399772cb7c8SJose R. Santos } 400772cb7c8SJose R. Santos 401772cb7c8SJose R. Santos return -1; 402772cb7c8SJose R. Santos out: 403772cb7c8SJose R. Santos return 0; 404772cb7c8SJose R. Santos } 405772cb7c8SJose R. Santos 406ac27a0ecSDave Kleikamp /* 407ac27a0ecSDave Kleikamp * Orlov's allocator for directories. 408ac27a0ecSDave Kleikamp * 409ac27a0ecSDave Kleikamp * We always try to spread first-level directories. 410ac27a0ecSDave Kleikamp * 411ac27a0ecSDave Kleikamp * If there are blockgroups with both free inodes and free blocks counts 412ac27a0ecSDave Kleikamp * not worse than average we return one with smallest directory count. 413ac27a0ecSDave Kleikamp * Otherwise we simply return a random group. 414ac27a0ecSDave Kleikamp * 415ac27a0ecSDave Kleikamp * For the rest rules look so: 416ac27a0ecSDave Kleikamp * 417ac27a0ecSDave Kleikamp * It's OK to put directory into a group unless 418ac27a0ecSDave Kleikamp * it has too many directories already (max_dirs) or 419ac27a0ecSDave Kleikamp * it has too few free inodes left (min_inodes) or 420ac27a0ecSDave Kleikamp * it has too few free blocks left (min_blocks) or 421ac27a0ecSDave Kleikamp * it's already running too large debt (max_debt). 4221cc8dcf5SBenoit Boissinot * Parent's group is preferred, if it doesn't satisfy these 423ac27a0ecSDave Kleikamp * conditions we search cyclically through the rest. If none 424ac27a0ecSDave Kleikamp * of the groups look good we just look for a group with more 425ac27a0ecSDave Kleikamp * free inodes than average (starting at parent's group). 426ac27a0ecSDave Kleikamp * 427ac27a0ecSDave Kleikamp * Debt is incremented each time we allocate a directory and decremented 428ac27a0ecSDave Kleikamp * when we allocate an inode, within 0--255. 429ac27a0ecSDave Kleikamp */ 430ac27a0ecSDave Kleikamp 431ac27a0ecSDave Kleikamp #define INODE_COST 64 432ac27a0ecSDave Kleikamp #define BLOCK_COST 256 433ac27a0ecSDave Kleikamp 4342aa9fc4cSAvantika Mathur static int find_group_orlov(struct super_block *sb, struct inode *parent, 4352aa9fc4cSAvantika Mathur ext4_group_t *group) 436ac27a0ecSDave Kleikamp { 437fd2d4291SAvantika Mathur ext4_group_t parent_group = EXT4_I(parent)->i_block_group; 438617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 439617ba13bSMingming Cao struct ext4_super_block *es = sbi->s_es; 440fd2d4291SAvantika Mathur ext4_group_t ngroups = sbi->s_groups_count; 441617ba13bSMingming Cao int inodes_per_group = EXT4_INODES_PER_GROUP(sb); 442ac27a0ecSDave Kleikamp unsigned int freei, avefreei; 443617ba13bSMingming Cao ext4_fsblk_t freeb, avefreeb; 444617ba13bSMingming Cao ext4_fsblk_t blocks_per_dir; 445ac27a0ecSDave Kleikamp unsigned int ndirs; 446ac27a0ecSDave Kleikamp int max_debt, max_dirs, min_inodes; 447617ba13bSMingming Cao ext4_grpblk_t min_blocks; 4482aa9fc4cSAvantika Mathur ext4_group_t i; 449617ba13bSMingming Cao struct ext4_group_desc *desc; 450ac27a0ecSDave Kleikamp 451ac27a0ecSDave Kleikamp freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter); 452ac27a0ecSDave Kleikamp avefreei = freei / ngroups; 453ac27a0ecSDave Kleikamp freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter); 4543a5b2ecdSMingming Cao avefreeb = freeb; 455f4e5bc24SAndrew Morton do_div(avefreeb, ngroups); 456ac27a0ecSDave Kleikamp ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter); 457ac27a0ecSDave Kleikamp 458ac27a0ecSDave Kleikamp if ((parent == sb->s_root->d_inode) || 459617ba13bSMingming Cao (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL)) { 460ac27a0ecSDave Kleikamp int best_ndir = inodes_per_group; 4612aa9fc4cSAvantika Mathur ext4_group_t grp; 4622aa9fc4cSAvantika Mathur int ret = -1; 463ac27a0ecSDave Kleikamp 4642aa9fc4cSAvantika Mathur get_random_bytes(&grp, sizeof(grp)); 4652aa9fc4cSAvantika Mathur parent_group = (unsigned)grp % ngroups; 466ac27a0ecSDave Kleikamp for (i = 0; i < ngroups; i++) { 4672aa9fc4cSAvantika Mathur grp = (parent_group + i) % ngroups; 4682aa9fc4cSAvantika Mathur desc = ext4_get_group_desc(sb, grp, NULL); 469560671a0SAneesh Kumar K.V if (!desc || !ext4_free_inodes_count(sb, desc)) 470ac27a0ecSDave Kleikamp continue; 471560671a0SAneesh Kumar K.V if (ext4_used_dirs_count(sb, desc) >= best_ndir) 472ac27a0ecSDave Kleikamp continue; 473560671a0SAneesh Kumar K.V if (ext4_free_inodes_count(sb, desc) < avefreei) 474ac27a0ecSDave Kleikamp continue; 475560671a0SAneesh Kumar K.V if (ext4_free_blks_count(sb, desc) < avefreeb) 476ac27a0ecSDave Kleikamp continue; 4772aa9fc4cSAvantika Mathur *group = grp; 4782aa9fc4cSAvantika Mathur ret = 0; 479560671a0SAneesh Kumar K.V best_ndir = ext4_used_dirs_count(sb, desc); 480ac27a0ecSDave Kleikamp } 4812aa9fc4cSAvantika Mathur if (ret == 0) 4822aa9fc4cSAvantika Mathur return ret; 483ac27a0ecSDave Kleikamp goto fallback; 484ac27a0ecSDave Kleikamp } 485ac27a0ecSDave Kleikamp 486bd81d8eeSLaurent Vivier blocks_per_dir = ext4_blocks_count(es) - freeb; 487f4e5bc24SAndrew Morton do_div(blocks_per_dir, ndirs); 488ac27a0ecSDave Kleikamp 489ac27a0ecSDave Kleikamp max_dirs = ndirs / ngroups + inodes_per_group / 16; 490ac27a0ecSDave Kleikamp min_inodes = avefreei - inodes_per_group / 4; 491617ba13bSMingming Cao min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb) / 4; 492ac27a0ecSDave Kleikamp 4933a5b2ecdSMingming Cao max_debt = EXT4_BLOCKS_PER_GROUP(sb); 494f4e5bc24SAndrew Morton max_debt /= max_t(int, blocks_per_dir, BLOCK_COST); 495ac27a0ecSDave Kleikamp if (max_debt * INODE_COST > inodes_per_group) 496ac27a0ecSDave Kleikamp max_debt = inodes_per_group / INODE_COST; 497ac27a0ecSDave Kleikamp if (max_debt > 255) 498ac27a0ecSDave Kleikamp max_debt = 255; 499ac27a0ecSDave Kleikamp if (max_debt == 0) 500ac27a0ecSDave Kleikamp max_debt = 1; 501ac27a0ecSDave Kleikamp 502ac27a0ecSDave Kleikamp for (i = 0; i < ngroups; i++) { 5032aa9fc4cSAvantika Mathur *group = (parent_group + i) % ngroups; 5042aa9fc4cSAvantika Mathur desc = ext4_get_group_desc(sb, *group, NULL); 505560671a0SAneesh Kumar K.V if (!desc || !ext4_free_inodes_count(sb, desc)) 506ac27a0ecSDave Kleikamp continue; 507560671a0SAneesh Kumar K.V if (ext4_used_dirs_count(sb, desc) >= max_dirs) 508ac27a0ecSDave Kleikamp continue; 509560671a0SAneesh Kumar K.V if (ext4_free_inodes_count(sb, desc) < min_inodes) 510ac27a0ecSDave Kleikamp continue; 511560671a0SAneesh Kumar K.V if (ext4_free_blks_count(sb, desc) < min_blocks) 512ac27a0ecSDave Kleikamp continue; 5132aa9fc4cSAvantika Mathur return 0; 514ac27a0ecSDave Kleikamp } 515ac27a0ecSDave Kleikamp 516ac27a0ecSDave Kleikamp fallback: 517ac27a0ecSDave Kleikamp for (i = 0; i < ngroups; i++) { 5182aa9fc4cSAvantika Mathur *group = (parent_group + i) % ngroups; 5192aa9fc4cSAvantika Mathur desc = ext4_get_group_desc(sb, *group, NULL); 520560671a0SAneesh Kumar K.V if (desc && ext4_free_inodes_count(sb, desc) && 521560671a0SAneesh Kumar K.V ext4_free_inodes_count(sb, desc) >= avefreei) 5222aa9fc4cSAvantika Mathur return 0; 523ac27a0ecSDave Kleikamp } 524ac27a0ecSDave Kleikamp 525ac27a0ecSDave Kleikamp if (avefreei) { 526ac27a0ecSDave Kleikamp /* 527ac27a0ecSDave Kleikamp * The free-inodes counter is approximate, and for really small 528ac27a0ecSDave Kleikamp * filesystems the above test can fail to find any blockgroups 529ac27a0ecSDave Kleikamp */ 530ac27a0ecSDave Kleikamp avefreei = 0; 531ac27a0ecSDave Kleikamp goto fallback; 532ac27a0ecSDave Kleikamp } 533ac27a0ecSDave Kleikamp 534ac27a0ecSDave Kleikamp return -1; 535ac27a0ecSDave Kleikamp } 536ac27a0ecSDave Kleikamp 5372aa9fc4cSAvantika Mathur static int find_group_other(struct super_block *sb, struct inode *parent, 5382aa9fc4cSAvantika Mathur ext4_group_t *group) 539ac27a0ecSDave Kleikamp { 540fd2d4291SAvantika Mathur ext4_group_t parent_group = EXT4_I(parent)->i_block_group; 541fd2d4291SAvantika Mathur ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count; 542617ba13bSMingming Cao struct ext4_group_desc *desc; 5432aa9fc4cSAvantika Mathur ext4_group_t i; 544ac27a0ecSDave Kleikamp 545ac27a0ecSDave Kleikamp /* 546ac27a0ecSDave Kleikamp * Try to place the inode in its parent directory 547ac27a0ecSDave Kleikamp */ 5482aa9fc4cSAvantika Mathur *group = parent_group; 5492aa9fc4cSAvantika Mathur desc = ext4_get_group_desc(sb, *group, NULL); 550560671a0SAneesh Kumar K.V if (desc && ext4_free_inodes_count(sb, desc) && 551560671a0SAneesh Kumar K.V ext4_free_blks_count(sb, desc)) 5522aa9fc4cSAvantika Mathur return 0; 553ac27a0ecSDave Kleikamp 554ac27a0ecSDave Kleikamp /* 555ac27a0ecSDave Kleikamp * We're going to place this inode in a different blockgroup from its 556ac27a0ecSDave Kleikamp * parent. We want to cause files in a common directory to all land in 557ac27a0ecSDave Kleikamp * the same blockgroup. But we want files which are in a different 558ac27a0ecSDave Kleikamp * directory which shares a blockgroup with our parent to land in a 559ac27a0ecSDave Kleikamp * different blockgroup. 560ac27a0ecSDave Kleikamp * 561ac27a0ecSDave Kleikamp * So add our directory's i_ino into the starting point for the hash. 562ac27a0ecSDave Kleikamp */ 5632aa9fc4cSAvantika Mathur *group = (*group + parent->i_ino) % ngroups; 564ac27a0ecSDave Kleikamp 565ac27a0ecSDave Kleikamp /* 566ac27a0ecSDave Kleikamp * Use a quadratic hash to find a group with a free inode and some free 567ac27a0ecSDave Kleikamp * blocks. 568ac27a0ecSDave Kleikamp */ 569ac27a0ecSDave Kleikamp for (i = 1; i < ngroups; i <<= 1) { 5702aa9fc4cSAvantika Mathur *group += i; 5712aa9fc4cSAvantika Mathur if (*group >= ngroups) 5722aa9fc4cSAvantika Mathur *group -= ngroups; 5732aa9fc4cSAvantika Mathur desc = ext4_get_group_desc(sb, *group, NULL); 574560671a0SAneesh Kumar K.V if (desc && ext4_free_inodes_count(sb, desc) && 575560671a0SAneesh Kumar K.V ext4_free_blks_count(sb, desc)) 5762aa9fc4cSAvantika Mathur return 0; 577ac27a0ecSDave Kleikamp } 578ac27a0ecSDave Kleikamp 579ac27a0ecSDave Kleikamp /* 580ac27a0ecSDave Kleikamp * That failed: try linear search for a free inode, even if that group 581ac27a0ecSDave Kleikamp * has no free blocks. 582ac27a0ecSDave Kleikamp */ 5832aa9fc4cSAvantika Mathur *group = parent_group; 584ac27a0ecSDave Kleikamp for (i = 0; i < ngroups; i++) { 5852aa9fc4cSAvantika Mathur if (++*group >= ngroups) 5862aa9fc4cSAvantika Mathur *group = 0; 5872aa9fc4cSAvantika Mathur desc = ext4_get_group_desc(sb, *group, NULL); 588560671a0SAneesh Kumar K.V if (desc && ext4_free_inodes_count(sb, desc)) 5892aa9fc4cSAvantika Mathur return 0; 590ac27a0ecSDave Kleikamp } 591ac27a0ecSDave Kleikamp 592ac27a0ecSDave Kleikamp return -1; 593ac27a0ecSDave Kleikamp } 594ac27a0ecSDave Kleikamp 595ac27a0ecSDave Kleikamp /* 59639341867SAneesh Kumar K.V * claim the inode from the inode bitmap. If the group 59739341867SAneesh Kumar K.V * is uninit we need to take the groups's sb_bgl_lock 59839341867SAneesh Kumar K.V * and clear the uninit flag. The inode bitmap update 59939341867SAneesh Kumar K.V * and group desc uninit flag clear should be done 60039341867SAneesh Kumar K.V * after holding sb_bgl_lock so that ext4_read_inode_bitmap 60139341867SAneesh Kumar K.V * doesn't race with the ext4_claim_inode 60239341867SAneesh Kumar K.V */ 60339341867SAneesh Kumar K.V static int ext4_claim_inode(struct super_block *sb, 60439341867SAneesh Kumar K.V struct buffer_head *inode_bitmap_bh, 60539341867SAneesh Kumar K.V unsigned long ino, ext4_group_t group, int mode) 60639341867SAneesh Kumar K.V { 60739341867SAneesh Kumar K.V int free = 0, retval = 0, count; 60839341867SAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(sb); 60939341867SAneesh Kumar K.V struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL); 61039341867SAneesh Kumar K.V 61139341867SAneesh Kumar K.V spin_lock(sb_bgl_lock(sbi, group)); 61239341867SAneesh Kumar K.V if (ext4_set_bit(ino, inode_bitmap_bh->b_data)) { 61339341867SAneesh Kumar K.V /* not a free inode */ 61439341867SAneesh Kumar K.V retval = 1; 61539341867SAneesh Kumar K.V goto err_ret; 61639341867SAneesh Kumar K.V } 61739341867SAneesh Kumar K.V ino++; 61839341867SAneesh Kumar K.V if ((group == 0 && ino < EXT4_FIRST_INO(sb)) || 61939341867SAneesh Kumar K.V ino > EXT4_INODES_PER_GROUP(sb)) { 62039341867SAneesh Kumar K.V spin_unlock(sb_bgl_lock(sbi, group)); 62139341867SAneesh Kumar K.V ext4_error(sb, __func__, 62239341867SAneesh Kumar K.V "reserved inode or inode > inodes count - " 62339341867SAneesh Kumar K.V "block_group = %u, inode=%lu", group, 62439341867SAneesh Kumar K.V ino + group * EXT4_INODES_PER_GROUP(sb)); 62539341867SAneesh Kumar K.V return 1; 62639341867SAneesh Kumar K.V } 62739341867SAneesh Kumar K.V /* If we didn't allocate from within the initialized part of the inode 62839341867SAneesh Kumar K.V * table then we need to initialize up to this inode. */ 62939341867SAneesh Kumar K.V if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) { 63039341867SAneesh Kumar K.V 63139341867SAneesh Kumar K.V if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) { 63239341867SAneesh Kumar K.V gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT); 63339341867SAneesh Kumar K.V /* When marking the block group with 63439341867SAneesh Kumar K.V * ~EXT4_BG_INODE_UNINIT we don't want to depend 63539341867SAneesh Kumar K.V * on the value of bg_itable_unused even though 63639341867SAneesh Kumar K.V * mke2fs could have initialized the same for us. 63739341867SAneesh Kumar K.V * Instead we calculated the value below 63839341867SAneesh Kumar K.V */ 63939341867SAneesh Kumar K.V 64039341867SAneesh Kumar K.V free = 0; 64139341867SAneesh Kumar K.V } else { 64239341867SAneesh Kumar K.V free = EXT4_INODES_PER_GROUP(sb) - 64339341867SAneesh Kumar K.V ext4_itable_unused_count(sb, gdp); 64439341867SAneesh Kumar K.V } 64539341867SAneesh Kumar K.V 64639341867SAneesh Kumar K.V /* 64739341867SAneesh Kumar K.V * Check the relative inode number against the last used 64839341867SAneesh Kumar K.V * relative inode number in this group. if it is greater 64939341867SAneesh Kumar K.V * we need to update the bg_itable_unused count 65039341867SAneesh Kumar K.V * 65139341867SAneesh Kumar K.V */ 65239341867SAneesh Kumar K.V if (ino > free) 65339341867SAneesh Kumar K.V ext4_itable_unused_set(sb, gdp, 65439341867SAneesh Kumar K.V (EXT4_INODES_PER_GROUP(sb) - ino)); 65539341867SAneesh Kumar K.V } 65639341867SAneesh Kumar K.V count = ext4_free_inodes_count(sb, gdp) - 1; 65739341867SAneesh Kumar K.V ext4_free_inodes_set(sb, gdp, count); 65839341867SAneesh Kumar K.V if (S_ISDIR(mode)) { 65939341867SAneesh Kumar K.V count = ext4_used_dirs_count(sb, gdp) + 1; 66039341867SAneesh Kumar K.V ext4_used_dirs_set(sb, gdp, count); 66139341867SAneesh Kumar K.V } 66239341867SAneesh Kumar K.V gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp); 66339341867SAneesh Kumar K.V err_ret: 66439341867SAneesh Kumar K.V spin_unlock(sb_bgl_lock(sbi, group)); 66539341867SAneesh Kumar K.V return retval; 66639341867SAneesh Kumar K.V } 66739341867SAneesh Kumar K.V 66839341867SAneesh Kumar K.V /* 669ac27a0ecSDave Kleikamp * There are two policies for allocating an inode. If the new inode is 670ac27a0ecSDave Kleikamp * a directory, then a forward search is made for a block group with both 671ac27a0ecSDave Kleikamp * free space and a low directory-to-inode ratio; if that fails, then of 672ac27a0ecSDave Kleikamp * the groups with above-average free space, that group with the fewest 673ac27a0ecSDave Kleikamp * directories already is chosen. 674ac27a0ecSDave Kleikamp * 675ac27a0ecSDave Kleikamp * For other inodes, search forward from the parent directory's block 676ac27a0ecSDave Kleikamp * group to find a free inode. 677ac27a0ecSDave Kleikamp */ 678617ba13bSMingming Cao struct inode *ext4_new_inode(handle_t *handle, struct inode *dir, int mode) 679ac27a0ecSDave Kleikamp { 680ac27a0ecSDave Kleikamp struct super_block *sb; 6813300bedaSAneesh Kumar K.V struct buffer_head *inode_bitmap_bh = NULL; 6823300bedaSAneesh Kumar K.V struct buffer_head *group_desc_bh; 6832aa9fc4cSAvantika Mathur ext4_group_t group = 0; 684ac27a0ecSDave Kleikamp unsigned long ino = 0; 685ac27a0ecSDave Kleikamp struct inode *inode; 686617ba13bSMingming Cao struct ext4_group_desc *gdp = NULL; 687617ba13bSMingming Cao struct ext4_super_block *es; 688617ba13bSMingming Cao struct ext4_inode_info *ei; 689617ba13bSMingming Cao struct ext4_sb_info *sbi; 69039341867SAneesh Kumar K.V int ret2, err = 0; 691ac27a0ecSDave Kleikamp struct inode *ret; 6922aa9fc4cSAvantika Mathur ext4_group_t i; 6932aa9fc4cSAvantika Mathur int free = 0; 694772cb7c8SJose R. Santos ext4_group_t flex_group; 695ac27a0ecSDave Kleikamp 696ac27a0ecSDave Kleikamp /* Cannot create files in a deleted directory */ 697ac27a0ecSDave Kleikamp if (!dir || !dir->i_nlink) 698ac27a0ecSDave Kleikamp return ERR_PTR(-EPERM); 699ac27a0ecSDave Kleikamp 700ac27a0ecSDave Kleikamp sb = dir->i_sb; 701ac27a0ecSDave Kleikamp inode = new_inode(sb); 702ac27a0ecSDave Kleikamp if (!inode) 703ac27a0ecSDave Kleikamp return ERR_PTR(-ENOMEM); 704617ba13bSMingming Cao ei = EXT4_I(inode); 705ac27a0ecSDave Kleikamp 706617ba13bSMingming Cao sbi = EXT4_SB(sb); 707ac27a0ecSDave Kleikamp es = sbi->s_es; 708772cb7c8SJose R. Santos 709772cb7c8SJose R. Santos if (sbi->s_log_groups_per_flex) { 710772cb7c8SJose R. Santos ret2 = find_group_flex(sb, dir, &group); 711772cb7c8SJose R. Santos goto got_group; 712772cb7c8SJose R. Santos } 713772cb7c8SJose R. Santos 714ac27a0ecSDave Kleikamp if (S_ISDIR(mode)) { 715ac27a0ecSDave Kleikamp if (test_opt(sb, OLDALLOC)) 7162aa9fc4cSAvantika Mathur ret2 = find_group_dir(sb, dir, &group); 717ac27a0ecSDave Kleikamp else 7182aa9fc4cSAvantika Mathur ret2 = find_group_orlov(sb, dir, &group); 719ac27a0ecSDave Kleikamp } else 7202aa9fc4cSAvantika Mathur ret2 = find_group_other(sb, dir, &group); 721ac27a0ecSDave Kleikamp 722772cb7c8SJose R. Santos got_group: 723ac27a0ecSDave Kleikamp err = -ENOSPC; 7242aa9fc4cSAvantika Mathur if (ret2 == -1) 725ac27a0ecSDave Kleikamp goto out; 726ac27a0ecSDave Kleikamp 727ac27a0ecSDave Kleikamp for (i = 0; i < sbi->s_groups_count; i++) { 728ac27a0ecSDave Kleikamp err = -EIO; 729ac27a0ecSDave Kleikamp 7303300bedaSAneesh Kumar K.V gdp = ext4_get_group_desc(sb, group, &group_desc_bh); 731ac27a0ecSDave Kleikamp if (!gdp) 732ac27a0ecSDave Kleikamp goto fail; 733ac27a0ecSDave Kleikamp 7343300bedaSAneesh Kumar K.V brelse(inode_bitmap_bh); 7353300bedaSAneesh Kumar K.V inode_bitmap_bh = ext4_read_inode_bitmap(sb, group); 7363300bedaSAneesh Kumar K.V if (!inode_bitmap_bh) 737ac27a0ecSDave Kleikamp goto fail; 738ac27a0ecSDave Kleikamp 739ac27a0ecSDave Kleikamp ino = 0; 740ac27a0ecSDave Kleikamp 741ac27a0ecSDave Kleikamp repeat_in_this_group: 742617ba13bSMingming Cao ino = ext4_find_next_zero_bit((unsigned long *) 7433300bedaSAneesh Kumar K.V inode_bitmap_bh->b_data, 7443300bedaSAneesh Kumar K.V EXT4_INODES_PER_GROUP(sb), ino); 7453300bedaSAneesh Kumar K.V 746617ba13bSMingming Cao if (ino < EXT4_INODES_PER_GROUP(sb)) { 747ac27a0ecSDave Kleikamp 7483300bedaSAneesh Kumar K.V BUFFER_TRACE(inode_bitmap_bh, "get_write_access"); 7493300bedaSAneesh Kumar K.V err = ext4_journal_get_write_access(handle, 7503300bedaSAneesh Kumar K.V inode_bitmap_bh); 751ac27a0ecSDave Kleikamp if (err) 752ac27a0ecSDave Kleikamp goto fail; 753ac27a0ecSDave Kleikamp 75439341867SAneesh Kumar K.V BUFFER_TRACE(group_desc_bh, "get_write_access"); 75539341867SAneesh Kumar K.V err = ext4_journal_get_write_access(handle, 75639341867SAneesh Kumar K.V group_desc_bh); 75739341867SAneesh Kumar K.V if (err) 75839341867SAneesh Kumar K.V goto fail; 75939341867SAneesh Kumar K.V if (!ext4_claim_inode(sb, inode_bitmap_bh, 76039341867SAneesh Kumar K.V ino, group, mode)) { 761ac27a0ecSDave Kleikamp /* we won it */ 7623300bedaSAneesh Kumar K.V BUFFER_TRACE(inode_bitmap_bh, 7630390131bSFrank Mayhar "call ext4_handle_dirty_metadata"); 7640390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, 7650390131bSFrank Mayhar inode, 7663300bedaSAneesh Kumar K.V inode_bitmap_bh); 767ac27a0ecSDave Kleikamp if (err) 768ac27a0ecSDave Kleikamp goto fail; 76939341867SAneesh Kumar K.V /* zero bit is inode number 1*/ 77039341867SAneesh Kumar K.V ino++; 771ac27a0ecSDave Kleikamp goto got; 772ac27a0ecSDave Kleikamp } 773ac27a0ecSDave Kleikamp /* we lost it */ 7743300bedaSAneesh Kumar K.V ext4_handle_release_buffer(handle, inode_bitmap_bh); 77539341867SAneesh Kumar K.V ext4_handle_release_buffer(handle, group_desc_bh); 776ac27a0ecSDave Kleikamp 777617ba13bSMingming Cao if (++ino < EXT4_INODES_PER_GROUP(sb)) 778ac27a0ecSDave Kleikamp goto repeat_in_this_group; 779ac27a0ecSDave Kleikamp } 780ac27a0ecSDave Kleikamp 781ac27a0ecSDave Kleikamp /* 782ac27a0ecSDave Kleikamp * This case is possible in concurrent environment. It is very 783ac27a0ecSDave Kleikamp * rare. We cannot repeat the find_group_xxx() call because 784ac27a0ecSDave Kleikamp * that will simply return the same blockgroup, because the 785ac27a0ecSDave Kleikamp * group descriptor metadata has not yet been updated. 786ac27a0ecSDave Kleikamp * So we just go onto the next blockgroup. 787ac27a0ecSDave Kleikamp */ 788ac27a0ecSDave Kleikamp if (++group == sbi->s_groups_count) 789ac27a0ecSDave Kleikamp group = 0; 790ac27a0ecSDave Kleikamp } 791ac27a0ecSDave Kleikamp err = -ENOSPC; 792ac27a0ecSDave Kleikamp goto out; 793ac27a0ecSDave Kleikamp 794ac27a0ecSDave Kleikamp got: 795717d50e4SAndreas Dilger /* We may have to initialize the block bitmap if it isn't already */ 796717d50e4SAndreas Dilger if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) && 797717d50e4SAndreas Dilger gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { 7983300bedaSAneesh Kumar K.V struct buffer_head *block_bitmap_bh; 799717d50e4SAndreas Dilger 8003300bedaSAneesh Kumar K.V block_bitmap_bh = ext4_read_block_bitmap(sb, group); 8013300bedaSAneesh Kumar K.V BUFFER_TRACE(block_bitmap_bh, "get block bitmap access"); 8023300bedaSAneesh Kumar K.V err = ext4_journal_get_write_access(handle, block_bitmap_bh); 803717d50e4SAndreas Dilger if (err) { 8043300bedaSAneesh Kumar K.V brelse(block_bitmap_bh); 805717d50e4SAndreas Dilger goto fail; 806717d50e4SAndreas Dilger } 807717d50e4SAndreas Dilger 808717d50e4SAndreas Dilger free = 0; 809ac27a0ecSDave Kleikamp spin_lock(sb_bgl_lock(sbi, group)); 810717d50e4SAndreas Dilger /* recheck and clear flag under lock if we still need to */ 811717d50e4SAndreas Dilger if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { 812717d50e4SAndreas Dilger free = ext4_free_blocks_after_init(sb, group, gdp); 8133300bedaSAneesh Kumar K.V gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT); 814560671a0SAneesh Kumar K.V ext4_free_blks_set(sb, gdp, free); 81523712a9cSFrederic Bohe gdp->bg_checksum = ext4_group_desc_csum(sbi, group, 81623712a9cSFrederic Bohe gdp); 817717d50e4SAndreas Dilger } 818717d50e4SAndreas Dilger spin_unlock(sb_bgl_lock(sbi, group)); 819717d50e4SAndreas Dilger 820717d50e4SAndreas Dilger /* Don't need to dirty bitmap block if we didn't change it */ 821717d50e4SAndreas Dilger if (free) { 8223300bedaSAneesh Kumar K.V BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap"); 8230390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, 8243300bedaSAneesh Kumar K.V NULL, block_bitmap_bh); 825717d50e4SAndreas Dilger } 826717d50e4SAndreas Dilger 8273300bedaSAneesh Kumar K.V brelse(block_bitmap_bh); 828717d50e4SAndreas Dilger if (err) 829717d50e4SAndreas Dilger goto fail; 830717d50e4SAndreas Dilger } 8313300bedaSAneesh Kumar K.V BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata"); 8323300bedaSAneesh Kumar K.V err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh); 83339341867SAneesh Kumar K.V if (err) 83439341867SAneesh Kumar K.V goto fail; 835ac27a0ecSDave Kleikamp 836ac27a0ecSDave Kleikamp percpu_counter_dec(&sbi->s_freeinodes_counter); 837ac27a0ecSDave Kleikamp if (S_ISDIR(mode)) 838ac27a0ecSDave Kleikamp percpu_counter_inc(&sbi->s_dirs_counter); 839ac27a0ecSDave Kleikamp sb->s_dirt = 1; 840ac27a0ecSDave Kleikamp 841772cb7c8SJose R. Santos if (sbi->s_log_groups_per_flex) { 842772cb7c8SJose R. Santos flex_group = ext4_flex_group(sbi, group); 843772cb7c8SJose R. Santos spin_lock(sb_bgl_lock(sbi, flex_group)); 844772cb7c8SJose R. Santos sbi->s_flex_groups[flex_group].free_inodes--; 845772cb7c8SJose R. Santos spin_unlock(sb_bgl_lock(sbi, flex_group)); 846772cb7c8SJose R. Santos } 847772cb7c8SJose R. Santos 8484c9c544eSDavid Howells inode->i_uid = current_fsuid(); 849ac27a0ecSDave Kleikamp if (test_opt(sb, GRPID)) 850ac27a0ecSDave Kleikamp inode->i_gid = dir->i_gid; 851ac27a0ecSDave Kleikamp else if (dir->i_mode & S_ISGID) { 852ac27a0ecSDave Kleikamp inode->i_gid = dir->i_gid; 853ac27a0ecSDave Kleikamp if (S_ISDIR(mode)) 854ac27a0ecSDave Kleikamp mode |= S_ISGID; 855ac27a0ecSDave Kleikamp } else 8564c9c544eSDavid Howells inode->i_gid = current_fsgid(); 857ac27a0ecSDave Kleikamp inode->i_mode = mode; 858ac27a0ecSDave Kleikamp 859717d50e4SAndreas Dilger inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb); 860ac27a0ecSDave Kleikamp /* This is the optimal IO size (for stat), not the fs block size */ 861ac27a0ecSDave Kleikamp inode->i_blocks = 0; 862ef7f3835SKalpak Shah inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime = 863ef7f3835SKalpak Shah ext4_current_time(inode); 864ac27a0ecSDave Kleikamp 865ac27a0ecSDave Kleikamp memset(ei->i_data, 0, sizeof(ei->i_data)); 866ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 867ac27a0ecSDave Kleikamp ei->i_disksize = 0; 868ac27a0ecSDave Kleikamp 86942bf0383SAneesh Kumar K.V /* 87042bf0383SAneesh Kumar K.V * Don't inherit extent flag from directory. We set extent flag on 87142bf0383SAneesh Kumar K.V * newly created directory and file only if -o extent mount option is 87242bf0383SAneesh Kumar K.V * specified 87342bf0383SAneesh Kumar K.V */ 87442bf0383SAneesh Kumar K.V ei->i_flags = EXT4_I(dir)->i_flags & ~(EXT4_INDEX_FL|EXT4_EXTENTS_FL); 875ac27a0ecSDave Kleikamp if (S_ISLNK(mode)) 876617ba13bSMingming Cao ei->i_flags &= ~(EXT4_IMMUTABLE_FL|EXT4_APPEND_FL); 877ac27a0ecSDave Kleikamp /* dirsync only applies to directories */ 878ac27a0ecSDave Kleikamp if (!S_ISDIR(mode)) 879617ba13bSMingming Cao ei->i_flags &= ~EXT4_DIRSYNC_FL; 880ac27a0ecSDave Kleikamp ei->i_file_acl = 0; 881ac27a0ecSDave Kleikamp ei->i_dtime = 0; 882ac27a0ecSDave Kleikamp ei->i_block_group = group; 883ac27a0ecSDave Kleikamp 884617ba13bSMingming Cao ext4_set_inode_flags(inode); 885ac27a0ecSDave Kleikamp if (IS_DIRSYNC(inode)) 8860390131bSFrank Mayhar ext4_handle_sync(handle); 8876b38e842SAl Viro if (insert_inode_locked(inode) < 0) { 8886b38e842SAl Viro err = -EINVAL; 8896b38e842SAl Viro goto fail_drop; 8906b38e842SAl Viro } 891ac27a0ecSDave Kleikamp spin_lock(&sbi->s_next_gen_lock); 892ac27a0ecSDave Kleikamp inode->i_generation = sbi->s_next_generation++; 893ac27a0ecSDave Kleikamp spin_unlock(&sbi->s_next_gen_lock); 894ac27a0ecSDave Kleikamp 895617ba13bSMingming Cao ei->i_state = EXT4_STATE_NEW; 896ef7f3835SKalpak Shah 897ef7f3835SKalpak Shah ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize; 898ac27a0ecSDave Kleikamp 899ac27a0ecSDave Kleikamp ret = inode; 900ac27a0ecSDave Kleikamp if (DQUOT_ALLOC_INODE(inode)) { 901ac27a0ecSDave Kleikamp err = -EDQUOT; 902ac27a0ecSDave Kleikamp goto fail_drop; 903ac27a0ecSDave Kleikamp } 904ac27a0ecSDave Kleikamp 905617ba13bSMingming Cao err = ext4_init_acl(handle, inode, dir); 906ac27a0ecSDave Kleikamp if (err) 907ac27a0ecSDave Kleikamp goto fail_free_drop; 908ac27a0ecSDave Kleikamp 909617ba13bSMingming Cao err = ext4_init_security(handle, inode, dir); 910ac27a0ecSDave Kleikamp if (err) 911ac27a0ecSDave Kleikamp goto fail_free_drop; 912ac27a0ecSDave Kleikamp 913a86c6181SAlex Tomas if (test_opt(sb, EXTENTS)) { 914e4079a11SEric Sandeen /* set extent flag only for directory, file and normal symlink*/ 915e65187e6SAneesh Kumar K.V if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) { 916a86c6181SAlex Tomas EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL; 917a86c6181SAlex Tomas ext4_ext_tree_init(handle, inode); 918a86c6181SAlex Tomas } 91942bf0383SAneesh Kumar K.V } 920ac27a0ecSDave Kleikamp 9218753e88fSAneesh Kumar K.V err = ext4_mark_inode_dirty(handle, inode); 9228753e88fSAneesh Kumar K.V if (err) { 9238753e88fSAneesh Kumar K.V ext4_std_error(sb, err); 9248753e88fSAneesh Kumar K.V goto fail_free_drop; 9258753e88fSAneesh Kumar K.V } 9268753e88fSAneesh Kumar K.V 927617ba13bSMingming Cao ext4_debug("allocating inode %lu\n", inode->i_ino); 928ac27a0ecSDave Kleikamp goto really_out; 929ac27a0ecSDave Kleikamp fail: 930617ba13bSMingming Cao ext4_std_error(sb, err); 931ac27a0ecSDave Kleikamp out: 932ac27a0ecSDave Kleikamp iput(inode); 933ac27a0ecSDave Kleikamp ret = ERR_PTR(err); 934ac27a0ecSDave Kleikamp really_out: 9353300bedaSAneesh Kumar K.V brelse(inode_bitmap_bh); 936ac27a0ecSDave Kleikamp return ret; 937ac27a0ecSDave Kleikamp 938ac27a0ecSDave Kleikamp fail_free_drop: 939ac27a0ecSDave Kleikamp DQUOT_FREE_INODE(inode); 940ac27a0ecSDave Kleikamp 941ac27a0ecSDave Kleikamp fail_drop: 942ac27a0ecSDave Kleikamp DQUOT_DROP(inode); 943ac27a0ecSDave Kleikamp inode->i_flags |= S_NOQUOTA; 944ac27a0ecSDave Kleikamp inode->i_nlink = 0; 9456b38e842SAl Viro unlock_new_inode(inode); 946ac27a0ecSDave Kleikamp iput(inode); 9473300bedaSAneesh Kumar K.V brelse(inode_bitmap_bh); 948ac27a0ecSDave Kleikamp return ERR_PTR(err); 949ac27a0ecSDave Kleikamp } 950ac27a0ecSDave Kleikamp 951ac27a0ecSDave Kleikamp /* Verify that we are loading a valid orphan from disk */ 952617ba13bSMingming Cao struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino) 953ac27a0ecSDave Kleikamp { 954617ba13bSMingming Cao unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count); 955fd2d4291SAvantika Mathur ext4_group_t block_group; 956ac27a0ecSDave Kleikamp int bit; 9571d1fe1eeSDavid Howells struct buffer_head *bitmap_bh; 958ac27a0ecSDave Kleikamp struct inode *inode = NULL; 9591d1fe1eeSDavid Howells long err = -EIO; 960ac27a0ecSDave Kleikamp 961ac27a0ecSDave Kleikamp /* Error cases - e2fsck has already cleaned up for us */ 962ac27a0ecSDave Kleikamp if (ino > max_ino) { 96346e665e9SHarvey Harrison ext4_warning(sb, __func__, 964ac27a0ecSDave Kleikamp "bad orphan ino %lu! e2fsck was run?", ino); 9651d1fe1eeSDavid Howells goto error; 966ac27a0ecSDave Kleikamp } 967ac27a0ecSDave Kleikamp 968617ba13bSMingming Cao block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); 969617ba13bSMingming Cao bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); 970e29d1cdeSEric Sandeen bitmap_bh = ext4_read_inode_bitmap(sb, block_group); 971ac27a0ecSDave Kleikamp if (!bitmap_bh) { 97246e665e9SHarvey Harrison ext4_warning(sb, __func__, 973ac27a0ecSDave Kleikamp "inode bitmap error for orphan %lu", ino); 9741d1fe1eeSDavid Howells goto error; 975ac27a0ecSDave Kleikamp } 976ac27a0ecSDave Kleikamp 977ac27a0ecSDave Kleikamp /* Having the inode bit set should be a 100% indicator that this 978ac27a0ecSDave Kleikamp * is a valid orphan (no e2fsck run on fs). Orphans also include 979ac27a0ecSDave Kleikamp * inodes that were being truncated, so we can't check i_nlink==0. 980ac27a0ecSDave Kleikamp */ 9811d1fe1eeSDavid Howells if (!ext4_test_bit(bit, bitmap_bh->b_data)) 9821d1fe1eeSDavid Howells goto bad_orphan; 9831d1fe1eeSDavid Howells 9841d1fe1eeSDavid Howells inode = ext4_iget(sb, ino); 9851d1fe1eeSDavid Howells if (IS_ERR(inode)) 9861d1fe1eeSDavid Howells goto iget_failed; 9871d1fe1eeSDavid Howells 98891ef4cafSDuane Griffin /* 98991ef4cafSDuane Griffin * If the orphans has i_nlinks > 0 then it should be able to be 99091ef4cafSDuane Griffin * truncated, otherwise it won't be removed from the orphan list 99191ef4cafSDuane Griffin * during processing and an infinite loop will result. 99291ef4cafSDuane Griffin */ 99391ef4cafSDuane Griffin if (inode->i_nlink && !ext4_can_truncate(inode)) 99491ef4cafSDuane Griffin goto bad_orphan; 99591ef4cafSDuane Griffin 9961d1fe1eeSDavid Howells if (NEXT_ORPHAN(inode) > max_ino) 9971d1fe1eeSDavid Howells goto bad_orphan; 9981d1fe1eeSDavid Howells brelse(bitmap_bh); 9991d1fe1eeSDavid Howells return inode; 10001d1fe1eeSDavid Howells 10011d1fe1eeSDavid Howells iget_failed: 10021d1fe1eeSDavid Howells err = PTR_ERR(inode); 10031d1fe1eeSDavid Howells inode = NULL; 10041d1fe1eeSDavid Howells bad_orphan: 100546e665e9SHarvey Harrison ext4_warning(sb, __func__, 1006ac27a0ecSDave Kleikamp "bad orphan inode %lu! e2fsck was run?", ino); 1007617ba13bSMingming Cao printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n", 1008ac27a0ecSDave Kleikamp bit, (unsigned long long)bitmap_bh->b_blocknr, 1009617ba13bSMingming Cao ext4_test_bit(bit, bitmap_bh->b_data)); 1010ac27a0ecSDave Kleikamp printk(KERN_NOTICE "inode=%p\n", inode); 1011ac27a0ecSDave Kleikamp if (inode) { 1012ac27a0ecSDave Kleikamp printk(KERN_NOTICE "is_bad_inode(inode)=%d\n", 1013ac27a0ecSDave Kleikamp is_bad_inode(inode)); 1014ac27a0ecSDave Kleikamp printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n", 1015ac27a0ecSDave Kleikamp NEXT_ORPHAN(inode)); 1016ac27a0ecSDave Kleikamp printk(KERN_NOTICE "max_ino=%lu\n", max_ino); 101791ef4cafSDuane Griffin printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink); 1018ac27a0ecSDave Kleikamp /* Avoid freeing blocks if we got a bad deleted inode */ 10191d1fe1eeSDavid Howells if (inode->i_nlink == 0) 1020ac27a0ecSDave Kleikamp inode->i_blocks = 0; 1021ac27a0ecSDave Kleikamp iput(inode); 1022ac27a0ecSDave Kleikamp } 1023ac27a0ecSDave Kleikamp brelse(bitmap_bh); 10241d1fe1eeSDavid Howells error: 10251d1fe1eeSDavid Howells return ERR_PTR(err); 1026ac27a0ecSDave Kleikamp } 1027ac27a0ecSDave Kleikamp 1028617ba13bSMingming Cao unsigned long ext4_count_free_inodes(struct super_block *sb) 1029ac27a0ecSDave Kleikamp { 1030ac27a0ecSDave Kleikamp unsigned long desc_count; 1031617ba13bSMingming Cao struct ext4_group_desc *gdp; 1032fd2d4291SAvantika Mathur ext4_group_t i; 1033617ba13bSMingming Cao #ifdef EXT4FS_DEBUG 1034617ba13bSMingming Cao struct ext4_super_block *es; 1035ac27a0ecSDave Kleikamp unsigned long bitmap_count, x; 1036ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh = NULL; 1037ac27a0ecSDave Kleikamp 1038617ba13bSMingming Cao es = EXT4_SB(sb)->s_es; 1039ac27a0ecSDave Kleikamp desc_count = 0; 1040ac27a0ecSDave Kleikamp bitmap_count = 0; 1041ac27a0ecSDave Kleikamp gdp = NULL; 1042617ba13bSMingming Cao for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) { 1043617ba13bSMingming Cao gdp = ext4_get_group_desc(sb, i, NULL); 1044ac27a0ecSDave Kleikamp if (!gdp) 1045ac27a0ecSDave Kleikamp continue; 1046560671a0SAneesh Kumar K.V desc_count += ext4_free_inodes_count(sb, gdp); 1047ac27a0ecSDave Kleikamp brelse(bitmap_bh); 1048e29d1cdeSEric Sandeen bitmap_bh = ext4_read_inode_bitmap(sb, i); 1049ac27a0ecSDave Kleikamp if (!bitmap_bh) 1050ac27a0ecSDave Kleikamp continue; 1051ac27a0ecSDave Kleikamp 1052617ba13bSMingming Cao x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8); 1053c549a95dSEric Sandeen printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n", 1054560671a0SAneesh Kumar K.V i, ext4_free_inodes_count(sb, gdp), x); 1055ac27a0ecSDave Kleikamp bitmap_count += x; 1056ac27a0ecSDave Kleikamp } 1057ac27a0ecSDave Kleikamp brelse(bitmap_bh); 10584776004fSTheodore Ts'o printk(KERN_DEBUG "ext4_count_free_inodes: " 10594776004fSTheodore Ts'o "stored = %u, computed = %lu, %lu\n", 1060ac27a0ecSDave Kleikamp le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count); 1061ac27a0ecSDave Kleikamp return desc_count; 1062ac27a0ecSDave Kleikamp #else 1063ac27a0ecSDave Kleikamp desc_count = 0; 1064617ba13bSMingming Cao for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) { 1065617ba13bSMingming Cao gdp = ext4_get_group_desc(sb, i, NULL); 1066ac27a0ecSDave Kleikamp if (!gdp) 1067ac27a0ecSDave Kleikamp continue; 1068560671a0SAneesh Kumar K.V desc_count += ext4_free_inodes_count(sb, gdp); 1069ac27a0ecSDave Kleikamp cond_resched(); 1070ac27a0ecSDave Kleikamp } 1071ac27a0ecSDave Kleikamp return desc_count; 1072ac27a0ecSDave Kleikamp #endif 1073ac27a0ecSDave Kleikamp } 1074ac27a0ecSDave Kleikamp 1075ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */ 1076617ba13bSMingming Cao unsigned long ext4_count_dirs(struct super_block * sb) 1077ac27a0ecSDave Kleikamp { 1078ac27a0ecSDave Kleikamp unsigned long count = 0; 1079fd2d4291SAvantika Mathur ext4_group_t i; 1080ac27a0ecSDave Kleikamp 1081617ba13bSMingming Cao for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) { 1082617ba13bSMingming Cao struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL); 1083ac27a0ecSDave Kleikamp if (!gdp) 1084ac27a0ecSDave Kleikamp continue; 1085560671a0SAneesh Kumar K.V count += ext4_used_dirs_count(sb, gdp); 1086ac27a0ecSDave Kleikamp } 1087ac27a0ecSDave Kleikamp return count; 1088ac27a0ecSDave Kleikamp } 1089