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> 269bffad1eSTheodore Ts'o 273dcf5451SChristoph Hellwig #include "ext4.h" 283dcf5451SChristoph Hellwig #include "ext4_jbd2.h" 29ac27a0ecSDave Kleikamp #include "xattr.h" 30ac27a0ecSDave Kleikamp #include "acl.h" 31ac27a0ecSDave Kleikamp 329bffad1eSTheodore Ts'o #include <trace/events/ext4.h> 339bffad1eSTheodore Ts'o 34ac27a0ecSDave Kleikamp /* 35ac27a0ecSDave Kleikamp * ialloc.c contains the inodes allocation and deallocation routines 36ac27a0ecSDave Kleikamp */ 37ac27a0ecSDave Kleikamp 38ac27a0ecSDave Kleikamp /* 39ac27a0ecSDave Kleikamp * The free inodes are managed by bitmaps. A file system contains several 40ac27a0ecSDave Kleikamp * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap 41ac27a0ecSDave Kleikamp * block for inodes, N blocks for the inode table and data blocks. 42ac27a0ecSDave Kleikamp * 43ac27a0ecSDave Kleikamp * The file system contains group descriptors which are located after the 44ac27a0ecSDave Kleikamp * super block. Each descriptor contains the number of the bitmap block and 45ac27a0ecSDave Kleikamp * the free blocks count in the block. 46ac27a0ecSDave Kleikamp */ 47ac27a0ecSDave Kleikamp 48717d50e4SAndreas Dilger /* 49717d50e4SAndreas Dilger * To avoid calling the atomic setbit hundreds or thousands of times, we only 50717d50e4SAndreas Dilger * need to use it within a single byte (to ensure we get endianness right). 51717d50e4SAndreas Dilger * We can use memset for the rest of the bitmap as there are no other users. 52717d50e4SAndreas Dilger */ 53717d50e4SAndreas Dilger void mark_bitmap_end(int start_bit, int end_bit, char *bitmap) 54717d50e4SAndreas Dilger { 55717d50e4SAndreas Dilger int i; 56717d50e4SAndreas Dilger 57717d50e4SAndreas Dilger if (start_bit >= end_bit) 58717d50e4SAndreas Dilger return; 59717d50e4SAndreas Dilger 60717d50e4SAndreas Dilger ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit); 61717d50e4SAndreas Dilger for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++) 62717d50e4SAndreas Dilger ext4_set_bit(i, bitmap); 63717d50e4SAndreas Dilger if (i < end_bit) 64717d50e4SAndreas Dilger memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3); 65717d50e4SAndreas Dilger } 66717d50e4SAndreas Dilger 67717d50e4SAndreas Dilger /* Initializes an uninitialized inode bitmap */ 68fd2d4291SAvantika Mathur unsigned ext4_init_inode_bitmap(struct super_block *sb, struct buffer_head *bh, 69fd2d4291SAvantika Mathur ext4_group_t block_group, 70717d50e4SAndreas Dilger struct ext4_group_desc *gdp) 71717d50e4SAndreas Dilger { 72717d50e4SAndreas Dilger struct ext4_sb_info *sbi = EXT4_SB(sb); 73717d50e4SAndreas Dilger 74717d50e4SAndreas Dilger J_ASSERT_BH(bh, buffer_locked(bh)); 75717d50e4SAndreas Dilger 76717d50e4SAndreas Dilger /* If checksum is bad mark all blocks and inodes use to prevent 77717d50e4SAndreas Dilger * allocation, essentially implementing a per-group read-only flag. */ 78717d50e4SAndreas Dilger if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) { 79*12062dddSEric Sandeen ext4_error(sb, "Checksum bad for group %u", block_group); 80560671a0SAneesh Kumar K.V ext4_free_blks_set(sb, gdp, 0); 81560671a0SAneesh Kumar K.V ext4_free_inodes_set(sb, gdp, 0); 82560671a0SAneesh Kumar K.V ext4_itable_unused_set(sb, gdp, 0); 83717d50e4SAndreas Dilger memset(bh->b_data, 0xff, sb->s_blocksize); 84717d50e4SAndreas Dilger return 0; 85717d50e4SAndreas Dilger } 86717d50e4SAndreas Dilger 87717d50e4SAndreas Dilger memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8); 88648f5879SAneesh Kumar K.V mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), sb->s_blocksize * 8, 89717d50e4SAndreas Dilger bh->b_data); 90717d50e4SAndreas Dilger 91717d50e4SAndreas Dilger return EXT4_INODES_PER_GROUP(sb); 92717d50e4SAndreas Dilger } 93ac27a0ecSDave Kleikamp 94ac27a0ecSDave Kleikamp /* 95ac27a0ecSDave Kleikamp * Read the inode allocation bitmap for a given block_group, reading 96ac27a0ecSDave Kleikamp * into the specified slot in the superblock's bitmap cache. 97ac27a0ecSDave Kleikamp * 98ac27a0ecSDave Kleikamp * Return buffer_head of bitmap on success or NULL. 99ac27a0ecSDave Kleikamp */ 100ac27a0ecSDave Kleikamp static struct buffer_head * 101e29d1cdeSEric Sandeen ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group) 102ac27a0ecSDave Kleikamp { 103617ba13bSMingming Cao struct ext4_group_desc *desc; 104ac27a0ecSDave Kleikamp struct buffer_head *bh = NULL; 105e29d1cdeSEric Sandeen ext4_fsblk_t bitmap_blk; 106ac27a0ecSDave Kleikamp 107617ba13bSMingming Cao desc = ext4_get_group_desc(sb, block_group, NULL); 108ac27a0ecSDave Kleikamp if (!desc) 109e29d1cdeSEric Sandeen return NULL; 110e29d1cdeSEric Sandeen bitmap_blk = ext4_inode_bitmap(sb, desc); 111e29d1cdeSEric Sandeen bh = sb_getblk(sb, bitmap_blk); 112e29d1cdeSEric Sandeen if (unlikely(!bh)) { 113*12062dddSEric Sandeen ext4_error(sb, "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 } 1182ccb5fb9SAneesh Kumar K.V if (bitmap_uptodate(bh)) 119e29d1cdeSEric Sandeen return bh; 120e29d1cdeSEric Sandeen 121c806e68fSFrederic Bohe lock_buffer(bh); 1222ccb5fb9SAneesh Kumar K.V if (bitmap_uptodate(bh)) { 1232ccb5fb9SAneesh Kumar K.V unlock_buffer(bh); 1242ccb5fb9SAneesh Kumar K.V return bh; 1252ccb5fb9SAneesh Kumar K.V } 126955ce5f5SAneesh Kumar K.V ext4_lock_group(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); 1292ccb5fb9SAneesh Kumar K.V set_bitmap_uptodate(bh); 130e29d1cdeSEric Sandeen set_buffer_uptodate(bh); 131955ce5f5SAneesh Kumar K.V ext4_unlock_group(sb, block_group); 1323300bedaSAneesh Kumar K.V unlock_buffer(bh); 133e29d1cdeSEric Sandeen return bh; 134e29d1cdeSEric Sandeen } 135955ce5f5SAneesh Kumar K.V ext4_unlock_group(sb, block_group); 1362ccb5fb9SAneesh Kumar K.V if (buffer_uptodate(bh)) { 1372ccb5fb9SAneesh Kumar K.V /* 1382ccb5fb9SAneesh Kumar K.V * if not uninit if bh is uptodate, 1392ccb5fb9SAneesh Kumar K.V * bitmap is also uptodate 1402ccb5fb9SAneesh Kumar K.V */ 1412ccb5fb9SAneesh Kumar K.V set_bitmap_uptodate(bh); 1422ccb5fb9SAneesh Kumar K.V unlock_buffer(bh); 1432ccb5fb9SAneesh Kumar K.V return bh; 1442ccb5fb9SAneesh Kumar K.V } 1452ccb5fb9SAneesh Kumar K.V /* 1462ccb5fb9SAneesh Kumar K.V * submit the buffer_head for read. We can 1472ccb5fb9SAneesh Kumar K.V * safely mark the bitmap as uptodate now. 1482ccb5fb9SAneesh Kumar K.V * We do it here so the bitmap uptodate bit 1492ccb5fb9SAneesh Kumar K.V * get set with buffer lock held. 1502ccb5fb9SAneesh Kumar K.V */ 1512ccb5fb9SAneesh Kumar K.V set_bitmap_uptodate(bh); 152e29d1cdeSEric Sandeen if (bh_submit_read(bh) < 0) { 153e29d1cdeSEric Sandeen put_bh(bh); 154*12062dddSEric Sandeen ext4_error(sb, "Cannot read inode bitmap - " 155a9df9a49STheodore Ts'o "block_group = %u, inode_bitmap = %llu", 156e29d1cdeSEric Sandeen block_group, bitmap_blk); 157e29d1cdeSEric Sandeen return NULL; 158e29d1cdeSEric Sandeen } 159ac27a0ecSDave Kleikamp return bh; 160ac27a0ecSDave Kleikamp } 161ac27a0ecSDave Kleikamp 162ac27a0ecSDave Kleikamp /* 163ac27a0ecSDave Kleikamp * NOTE! When we get the inode, we're the only people 164ac27a0ecSDave Kleikamp * that have access to it, and as such there are no 165ac27a0ecSDave Kleikamp * race conditions we have to worry about. The inode 166ac27a0ecSDave Kleikamp * is not on the hash-lists, and it cannot be reached 167ac27a0ecSDave Kleikamp * through the filesystem because the directory entry 168ac27a0ecSDave Kleikamp * has been deleted earlier. 169ac27a0ecSDave Kleikamp * 170ac27a0ecSDave Kleikamp * HOWEVER: we must make sure that we get no aliases, 171ac27a0ecSDave Kleikamp * which means that we have to call "clear_inode()" 172ac27a0ecSDave Kleikamp * _before_ we mark the inode not in use in the inode 173ac27a0ecSDave Kleikamp * bitmaps. Otherwise a newly created file might use 174ac27a0ecSDave Kleikamp * the same inode number (not actually the same pointer 175ac27a0ecSDave Kleikamp * though), and then we'd have two inodes sharing the 176ac27a0ecSDave Kleikamp * same inode number and space on the harddisk. 177ac27a0ecSDave Kleikamp */ 178617ba13bSMingming Cao void ext4_free_inode(handle_t *handle, struct inode *inode) 179ac27a0ecSDave Kleikamp { 180ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 181ac27a0ecSDave Kleikamp int is_directory; 182ac27a0ecSDave Kleikamp unsigned long ino; 183ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh = NULL; 184ac27a0ecSDave Kleikamp struct buffer_head *bh2; 185fd2d4291SAvantika Mathur ext4_group_t block_group; 186ac27a0ecSDave Kleikamp unsigned long bit; 187617ba13bSMingming Cao struct ext4_group_desc *gdp; 188617ba13bSMingming Cao struct ext4_super_block *es; 189617ba13bSMingming Cao struct ext4_sb_info *sbi; 1907ce9d5d1SEric Sandeen int fatal = 0, err, count, cleared; 191ac27a0ecSDave Kleikamp 192ac27a0ecSDave Kleikamp if (atomic_read(&inode->i_count) > 1) { 1934776004fSTheodore Ts'o printk(KERN_ERR "ext4_free_inode: inode has count=%d\n", 194ac27a0ecSDave Kleikamp atomic_read(&inode->i_count)); 195ac27a0ecSDave Kleikamp return; 196ac27a0ecSDave Kleikamp } 197ac27a0ecSDave Kleikamp if (inode->i_nlink) { 1984776004fSTheodore Ts'o printk(KERN_ERR "ext4_free_inode: inode has nlink=%d\n", 199ac27a0ecSDave Kleikamp inode->i_nlink); 200ac27a0ecSDave Kleikamp return; 201ac27a0ecSDave Kleikamp } 202ac27a0ecSDave Kleikamp if (!sb) { 2034776004fSTheodore Ts'o printk(KERN_ERR "ext4_free_inode: inode on " 2044776004fSTheodore Ts'o "nonexistent device\n"); 205ac27a0ecSDave Kleikamp return; 206ac27a0ecSDave Kleikamp } 207617ba13bSMingming Cao sbi = EXT4_SB(sb); 208ac27a0ecSDave Kleikamp 209ac27a0ecSDave Kleikamp ino = inode->i_ino; 210617ba13bSMingming Cao ext4_debug("freeing inode %lu\n", ino); 2119bffad1eSTheodore Ts'o trace_ext4_free_inode(inode); 212ac27a0ecSDave Kleikamp 213ac27a0ecSDave Kleikamp /* 214ac27a0ecSDave Kleikamp * Note: we must free any quota before locking the superblock, 215ac27a0ecSDave Kleikamp * as writing the quota to disk may need the lock as well. 216ac27a0ecSDave Kleikamp */ 217a269eb18SJan Kara vfs_dq_init(inode); 218617ba13bSMingming Cao ext4_xattr_delete_inode(handle, inode); 219a269eb18SJan Kara vfs_dq_free_inode(inode); 220a269eb18SJan Kara vfs_dq_drop(inode); 221ac27a0ecSDave Kleikamp 222ac27a0ecSDave Kleikamp is_directory = S_ISDIR(inode->i_mode); 223ac27a0ecSDave Kleikamp 224ac27a0ecSDave Kleikamp /* Do this BEFORE marking the inode not in use or returning an error */ 225ac27a0ecSDave Kleikamp clear_inode(inode); 226ac27a0ecSDave Kleikamp 227617ba13bSMingming Cao es = EXT4_SB(sb)->s_es; 228617ba13bSMingming Cao if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) { 229*12062dddSEric Sandeen ext4_error(sb, "reserved or nonexistent inode %lu", ino); 230ac27a0ecSDave Kleikamp goto error_return; 231ac27a0ecSDave Kleikamp } 232617ba13bSMingming Cao block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); 233617ba13bSMingming Cao bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); 234e29d1cdeSEric Sandeen bitmap_bh = ext4_read_inode_bitmap(sb, block_group); 235ac27a0ecSDave Kleikamp if (!bitmap_bh) 236ac27a0ecSDave Kleikamp goto error_return; 237ac27a0ecSDave Kleikamp 238ac27a0ecSDave Kleikamp BUFFER_TRACE(bitmap_bh, "get_write_access"); 239617ba13bSMingming Cao fatal = ext4_journal_get_write_access(handle, bitmap_bh); 240ac27a0ecSDave Kleikamp if (fatal) 241ac27a0ecSDave Kleikamp goto error_return; 242ac27a0ecSDave Kleikamp 243ac27a0ecSDave Kleikamp /* Ok, now we can actually update the inode bitmaps.. */ 244955ce5f5SAneesh Kumar K.V cleared = ext4_clear_bit_atomic(ext4_group_lock_ptr(sb, block_group), 245955ce5f5SAneesh Kumar K.V bit, bitmap_bh->b_data); 2467ce9d5d1SEric Sandeen if (!cleared) 247*12062dddSEric Sandeen ext4_error(sb, "bit already cleared for inode %lu", ino); 248ac27a0ecSDave Kleikamp else { 249617ba13bSMingming Cao gdp = ext4_get_group_desc(sb, block_group, &bh2); 250ac27a0ecSDave Kleikamp 251ac27a0ecSDave Kleikamp BUFFER_TRACE(bh2, "get_write_access"); 252617ba13bSMingming Cao fatal = ext4_journal_get_write_access(handle, bh2); 253ac27a0ecSDave Kleikamp if (fatal) goto error_return; 254ac27a0ecSDave Kleikamp 255ac27a0ecSDave Kleikamp if (gdp) { 256955ce5f5SAneesh Kumar K.V ext4_lock_group(sb, block_group); 257560671a0SAneesh Kumar K.V count = ext4_free_inodes_count(sb, gdp) + 1; 258560671a0SAneesh Kumar K.V ext4_free_inodes_set(sb, gdp, count); 259560671a0SAneesh Kumar K.V if (is_directory) { 260560671a0SAneesh Kumar K.V count = ext4_used_dirs_count(sb, gdp) - 1; 261560671a0SAneesh Kumar K.V ext4_used_dirs_set(sb, gdp, count); 2627d39db14STheodore Ts'o if (sbi->s_log_groups_per_flex) { 2637d39db14STheodore Ts'o ext4_group_t f; 2647d39db14STheodore Ts'o 2657d39db14STheodore Ts'o f = ext4_flex_group(sbi, block_group); 2667d39db14STheodore Ts'o atomic_dec(&sbi->s_flex_groups[f].free_inodes); 2677d39db14STheodore Ts'o } 2687d39db14STheodore Ts'o 269560671a0SAneesh Kumar K.V } 270717d50e4SAndreas Dilger gdp->bg_checksum = ext4_group_desc_csum(sbi, 271717d50e4SAndreas Dilger block_group, gdp); 272955ce5f5SAneesh Kumar K.V ext4_unlock_group(sb, block_group); 273ac27a0ecSDave Kleikamp percpu_counter_inc(&sbi->s_freeinodes_counter); 274ac27a0ecSDave Kleikamp if (is_directory) 275ac27a0ecSDave Kleikamp percpu_counter_dec(&sbi->s_dirs_counter); 276ac27a0ecSDave Kleikamp 277772cb7c8SJose R. Santos if (sbi->s_log_groups_per_flex) { 2789f24e420STheodore Ts'o ext4_group_t f; 2799f24e420STheodore Ts'o 2809f24e420STheodore Ts'o f = ext4_flex_group(sbi, block_group); 2819f24e420STheodore Ts'o atomic_inc(&sbi->s_flex_groups[f].free_inodes); 282772cb7c8SJose R. Santos } 283ac27a0ecSDave Kleikamp } 2840390131bSFrank Mayhar BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata"); 2850390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, NULL, bh2); 286ac27a0ecSDave Kleikamp if (!fatal) fatal = err; 287ac27a0ecSDave Kleikamp } 2880390131bSFrank Mayhar BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata"); 2890390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh); 290ac27a0ecSDave Kleikamp if (!fatal) 291ac27a0ecSDave Kleikamp fatal = err; 292ac27a0ecSDave Kleikamp sb->s_dirt = 1; 293ac27a0ecSDave Kleikamp error_return: 294ac27a0ecSDave Kleikamp brelse(bitmap_bh); 295617ba13bSMingming Cao ext4_std_error(sb, fatal); 296ac27a0ecSDave Kleikamp } 297ac27a0ecSDave Kleikamp 298ac27a0ecSDave Kleikamp /* 299ac27a0ecSDave Kleikamp * There are two policies for allocating an inode. If the new inode is 300ac27a0ecSDave Kleikamp * a directory, then a forward search is made for a block group with both 301ac27a0ecSDave Kleikamp * free space and a low directory-to-inode ratio; if that fails, then of 302ac27a0ecSDave Kleikamp * the groups with above-average free space, that group with the fewest 303ac27a0ecSDave Kleikamp * directories already is chosen. 304ac27a0ecSDave Kleikamp * 305ac27a0ecSDave Kleikamp * For other inodes, search forward from the parent directory\'s block 306ac27a0ecSDave Kleikamp * group to find a free inode. 307ac27a0ecSDave Kleikamp */ 3082aa9fc4cSAvantika Mathur static int find_group_dir(struct super_block *sb, struct inode *parent, 3092aa9fc4cSAvantika Mathur ext4_group_t *best_group) 310ac27a0ecSDave Kleikamp { 3118df9675fSTheodore Ts'o ext4_group_t ngroups = ext4_get_groups_count(sb); 312ac27a0ecSDave Kleikamp unsigned int freei, avefreei; 313617ba13bSMingming Cao struct ext4_group_desc *desc, *best_desc = NULL; 3142aa9fc4cSAvantika Mathur ext4_group_t group; 3152aa9fc4cSAvantika Mathur int ret = -1; 316ac27a0ecSDave Kleikamp 317617ba13bSMingming Cao freei = percpu_counter_read_positive(&EXT4_SB(sb)->s_freeinodes_counter); 318ac27a0ecSDave Kleikamp avefreei = freei / ngroups; 319ac27a0ecSDave Kleikamp 320ac27a0ecSDave Kleikamp for (group = 0; group < ngroups; group++) { 321ef2fb679SEric Sandeen desc = ext4_get_group_desc(sb, group, NULL); 322560671a0SAneesh Kumar K.V if (!desc || !ext4_free_inodes_count(sb, desc)) 323ac27a0ecSDave Kleikamp continue; 324560671a0SAneesh Kumar K.V if (ext4_free_inodes_count(sb, desc) < avefreei) 325ac27a0ecSDave Kleikamp continue; 326ac27a0ecSDave Kleikamp if (!best_desc || 327560671a0SAneesh Kumar K.V (ext4_free_blks_count(sb, desc) > 328560671a0SAneesh Kumar K.V ext4_free_blks_count(sb, best_desc))) { 3292aa9fc4cSAvantika Mathur *best_group = group; 330ac27a0ecSDave Kleikamp best_desc = desc; 3312aa9fc4cSAvantika Mathur ret = 0; 332ac27a0ecSDave Kleikamp } 333ac27a0ecSDave Kleikamp } 3342aa9fc4cSAvantika Mathur return ret; 335ac27a0ecSDave Kleikamp } 336ac27a0ecSDave Kleikamp 337772cb7c8SJose R. Santos #define free_block_ratio 10 338772cb7c8SJose R. Santos 339772cb7c8SJose R. Santos static int find_group_flex(struct super_block *sb, struct inode *parent, 340772cb7c8SJose R. Santos ext4_group_t *best_group) 341772cb7c8SJose R. Santos { 342772cb7c8SJose R. Santos struct ext4_sb_info *sbi = EXT4_SB(sb); 343772cb7c8SJose R. Santos struct ext4_group_desc *desc; 344772cb7c8SJose R. Santos struct flex_groups *flex_group = sbi->s_flex_groups; 345772cb7c8SJose R. Santos ext4_group_t parent_group = EXT4_I(parent)->i_block_group; 346772cb7c8SJose R. Santos ext4_group_t parent_fbg_group = ext4_flex_group(sbi, parent_group); 3478df9675fSTheodore Ts'o ext4_group_t ngroups = ext4_get_groups_count(sb); 348772cb7c8SJose R. Santos int flex_size = ext4_flex_bg_size(sbi); 349772cb7c8SJose R. Santos ext4_group_t best_flex = parent_fbg_group; 350772cb7c8SJose R. Santos int blocks_per_flex = sbi->s_blocks_per_group * flex_size; 351772cb7c8SJose R. Santos int flexbg_free_blocks; 352772cb7c8SJose R. Santos int flex_freeb_ratio; 353772cb7c8SJose R. Santos ext4_group_t n_fbg_groups; 354772cb7c8SJose R. Santos ext4_group_t i; 355772cb7c8SJose R. Santos 3568df9675fSTheodore Ts'o n_fbg_groups = (ngroups + flex_size - 1) >> 357772cb7c8SJose R. Santos sbi->s_log_groups_per_flex; 358772cb7c8SJose R. Santos 359772cb7c8SJose R. Santos find_close_to_parent: 3609f24e420STheodore Ts'o flexbg_free_blocks = atomic_read(&flex_group[best_flex].free_blocks); 361772cb7c8SJose R. Santos flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex; 3629f24e420STheodore Ts'o if (atomic_read(&flex_group[best_flex].free_inodes) && 363772cb7c8SJose R. Santos flex_freeb_ratio > free_block_ratio) 364772cb7c8SJose R. Santos goto found_flexbg; 365772cb7c8SJose R. Santos 366772cb7c8SJose R. Santos if (best_flex && best_flex == parent_fbg_group) { 367772cb7c8SJose R. Santos best_flex--; 368772cb7c8SJose R. Santos goto find_close_to_parent; 369772cb7c8SJose R. Santos } 370772cb7c8SJose R. Santos 371772cb7c8SJose R. Santos for (i = 0; i < n_fbg_groups; i++) { 372772cb7c8SJose R. Santos if (i == parent_fbg_group || i == parent_fbg_group - 1) 373772cb7c8SJose R. Santos continue; 374772cb7c8SJose R. Santos 3759f24e420STheodore Ts'o flexbg_free_blocks = atomic_read(&flex_group[i].free_blocks); 376772cb7c8SJose R. Santos flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex; 377772cb7c8SJose R. Santos 378772cb7c8SJose R. Santos if (flex_freeb_ratio > free_block_ratio && 3799f24e420STheodore Ts'o (atomic_read(&flex_group[i].free_inodes))) { 380772cb7c8SJose R. Santos best_flex = i; 381772cb7c8SJose R. Santos goto found_flexbg; 382772cb7c8SJose R. Santos } 383772cb7c8SJose R. Santos 3849f24e420STheodore Ts'o if ((atomic_read(&flex_group[best_flex].free_inodes) == 0) || 3859f24e420STheodore Ts'o ((atomic_read(&flex_group[i].free_blocks) > 3869f24e420STheodore Ts'o atomic_read(&flex_group[best_flex].free_blocks)) && 3879f24e420STheodore Ts'o atomic_read(&flex_group[i].free_inodes))) 388772cb7c8SJose R. Santos best_flex = i; 389772cb7c8SJose R. Santos } 390772cb7c8SJose R. Santos 3919f24e420STheodore Ts'o if (!atomic_read(&flex_group[best_flex].free_inodes) || 3929f24e420STheodore Ts'o !atomic_read(&flex_group[best_flex].free_blocks)) 393772cb7c8SJose R. Santos return -1; 394772cb7c8SJose R. Santos 395772cb7c8SJose R. Santos found_flexbg: 396772cb7c8SJose R. Santos for (i = best_flex * flex_size; i < ngroups && 397772cb7c8SJose R. Santos i < (best_flex + 1) * flex_size; i++) { 39888b6edd1STheodore Ts'o desc = ext4_get_group_desc(sb, i, NULL); 399560671a0SAneesh Kumar K.V if (ext4_free_inodes_count(sb, desc)) { 400772cb7c8SJose R. Santos *best_group = i; 401772cb7c8SJose R. Santos goto out; 402772cb7c8SJose R. Santos } 403772cb7c8SJose R. Santos } 404772cb7c8SJose R. Santos 405772cb7c8SJose R. Santos return -1; 406772cb7c8SJose R. Santos out: 407772cb7c8SJose R. Santos return 0; 408772cb7c8SJose R. Santos } 409772cb7c8SJose R. Santos 410a4912123STheodore Ts'o struct orlov_stats { 411a4912123STheodore Ts'o __u32 free_inodes; 412a4912123STheodore Ts'o __u32 free_blocks; 413a4912123STheodore Ts'o __u32 used_dirs; 414a4912123STheodore Ts'o }; 415a4912123STheodore Ts'o 416a4912123STheodore Ts'o /* 417a4912123STheodore Ts'o * Helper function for Orlov's allocator; returns critical information 418a4912123STheodore Ts'o * for a particular block group or flex_bg. If flex_size is 1, then g 419a4912123STheodore Ts'o * is a block group number; otherwise it is flex_bg number. 420a4912123STheodore Ts'o */ 421a4912123STheodore Ts'o void get_orlov_stats(struct super_block *sb, ext4_group_t g, 422a4912123STheodore Ts'o int flex_size, struct orlov_stats *stats) 423a4912123STheodore Ts'o { 424a4912123STheodore Ts'o struct ext4_group_desc *desc; 4257d39db14STheodore Ts'o struct flex_groups *flex_group = EXT4_SB(sb)->s_flex_groups; 426a4912123STheodore Ts'o 4277d39db14STheodore Ts'o if (flex_size > 1) { 4287d39db14STheodore Ts'o stats->free_inodes = atomic_read(&flex_group[g].free_inodes); 4297d39db14STheodore Ts'o stats->free_blocks = atomic_read(&flex_group[g].free_blocks); 4307d39db14STheodore Ts'o stats->used_dirs = atomic_read(&flex_group[g].used_dirs); 4317d39db14STheodore Ts'o return; 4327d39db14STheodore Ts'o } 4337d39db14STheodore Ts'o 4347d39db14STheodore Ts'o desc = ext4_get_group_desc(sb, g, NULL); 4357d39db14STheodore Ts'o if (desc) { 4367d39db14STheodore Ts'o stats->free_inodes = ext4_free_inodes_count(sb, desc); 4377d39db14STheodore Ts'o stats->free_blocks = ext4_free_blks_count(sb, desc); 4387d39db14STheodore Ts'o stats->used_dirs = ext4_used_dirs_count(sb, desc); 4397d39db14STheodore Ts'o } else { 440a4912123STheodore Ts'o stats->free_inodes = 0; 441a4912123STheodore Ts'o stats->free_blocks = 0; 442a4912123STheodore Ts'o stats->used_dirs = 0; 443a4912123STheodore Ts'o } 444a4912123STheodore Ts'o } 445a4912123STheodore Ts'o 446ac27a0ecSDave Kleikamp /* 447ac27a0ecSDave Kleikamp * Orlov's allocator for directories. 448ac27a0ecSDave Kleikamp * 449ac27a0ecSDave Kleikamp * We always try to spread first-level directories. 450ac27a0ecSDave Kleikamp * 451ac27a0ecSDave Kleikamp * If there are blockgroups with both free inodes and free blocks counts 452ac27a0ecSDave Kleikamp * not worse than average we return one with smallest directory count. 453ac27a0ecSDave Kleikamp * Otherwise we simply return a random group. 454ac27a0ecSDave Kleikamp * 455ac27a0ecSDave Kleikamp * For the rest rules look so: 456ac27a0ecSDave Kleikamp * 457ac27a0ecSDave Kleikamp * It's OK to put directory into a group unless 458ac27a0ecSDave Kleikamp * it has too many directories already (max_dirs) or 459ac27a0ecSDave Kleikamp * it has too few free inodes left (min_inodes) or 460ac27a0ecSDave Kleikamp * it has too few free blocks left (min_blocks) or 4611cc8dcf5SBenoit Boissinot * Parent's group is preferred, if it doesn't satisfy these 462ac27a0ecSDave Kleikamp * conditions we search cyclically through the rest. If none 463ac27a0ecSDave Kleikamp * of the groups look good we just look for a group with more 464ac27a0ecSDave Kleikamp * free inodes than average (starting at parent's group). 465ac27a0ecSDave Kleikamp */ 466ac27a0ecSDave Kleikamp 4672aa9fc4cSAvantika Mathur static int find_group_orlov(struct super_block *sb, struct inode *parent, 468f157a4aaSTheodore Ts'o ext4_group_t *group, int mode, 469f157a4aaSTheodore Ts'o const struct qstr *qstr) 470ac27a0ecSDave Kleikamp { 471fd2d4291SAvantika Mathur ext4_group_t parent_group = EXT4_I(parent)->i_block_group; 472617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 4738df9675fSTheodore Ts'o ext4_group_t real_ngroups = ext4_get_groups_count(sb); 474617ba13bSMingming Cao int inodes_per_group = EXT4_INODES_PER_GROUP(sb); 475ac27a0ecSDave Kleikamp unsigned int freei, avefreei; 476617ba13bSMingming Cao ext4_fsblk_t freeb, avefreeb; 477ac27a0ecSDave Kleikamp unsigned int ndirs; 478a4912123STheodore Ts'o int max_dirs, min_inodes; 479617ba13bSMingming Cao ext4_grpblk_t min_blocks; 4808df9675fSTheodore Ts'o ext4_group_t i, grp, g, ngroups; 481617ba13bSMingming Cao struct ext4_group_desc *desc; 482a4912123STheodore Ts'o struct orlov_stats stats; 483a4912123STheodore Ts'o int flex_size = ext4_flex_bg_size(sbi); 484f157a4aaSTheodore Ts'o struct dx_hash_info hinfo; 485a4912123STheodore Ts'o 4868df9675fSTheodore Ts'o ngroups = real_ngroups; 487a4912123STheodore Ts'o if (flex_size > 1) { 4888df9675fSTheodore Ts'o ngroups = (real_ngroups + flex_size - 1) >> 489a4912123STheodore Ts'o sbi->s_log_groups_per_flex; 490a4912123STheodore Ts'o parent_group >>= sbi->s_log_groups_per_flex; 491a4912123STheodore Ts'o } 492ac27a0ecSDave Kleikamp 493ac27a0ecSDave Kleikamp freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter); 494ac27a0ecSDave Kleikamp avefreei = freei / ngroups; 495ac27a0ecSDave Kleikamp freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter); 4963a5b2ecdSMingming Cao avefreeb = freeb; 497f4e5bc24SAndrew Morton do_div(avefreeb, ngroups); 498ac27a0ecSDave Kleikamp ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter); 499ac27a0ecSDave Kleikamp 500a4912123STheodore Ts'o if (S_ISDIR(mode) && 501a4912123STheodore Ts'o ((parent == sb->s_root->d_inode) || 502a4912123STheodore Ts'o (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL))) { 503ac27a0ecSDave Kleikamp int best_ndir = inodes_per_group; 5042aa9fc4cSAvantika Mathur int ret = -1; 505ac27a0ecSDave Kleikamp 506f157a4aaSTheodore Ts'o if (qstr) { 507f157a4aaSTheodore Ts'o hinfo.hash_version = DX_HASH_HALF_MD4; 508f157a4aaSTheodore Ts'o hinfo.seed = sbi->s_hash_seed; 509f157a4aaSTheodore Ts'o ext4fs_dirhash(qstr->name, qstr->len, &hinfo); 510f157a4aaSTheodore Ts'o grp = hinfo.hash; 511f157a4aaSTheodore Ts'o } else 5122aa9fc4cSAvantika Mathur get_random_bytes(&grp, sizeof(grp)); 5132aa9fc4cSAvantika Mathur parent_group = (unsigned)grp % ngroups; 514ac27a0ecSDave Kleikamp for (i = 0; i < ngroups; i++) { 515a4912123STheodore Ts'o g = (parent_group + i) % ngroups; 516a4912123STheodore Ts'o get_orlov_stats(sb, g, flex_size, &stats); 517a4912123STheodore Ts'o if (!stats.free_inodes) 518ac27a0ecSDave Kleikamp continue; 519a4912123STheodore Ts'o if (stats.used_dirs >= best_ndir) 520ac27a0ecSDave Kleikamp continue; 521a4912123STheodore Ts'o if (stats.free_inodes < avefreei) 522ac27a0ecSDave Kleikamp continue; 523a4912123STheodore Ts'o if (stats.free_blocks < avefreeb) 524ac27a0ecSDave Kleikamp continue; 525a4912123STheodore Ts'o grp = g; 5262aa9fc4cSAvantika Mathur ret = 0; 527a4912123STheodore Ts'o best_ndir = stats.used_dirs; 528ac27a0ecSDave Kleikamp } 529a4912123STheodore Ts'o if (ret) 530a4912123STheodore Ts'o goto fallback; 531a4912123STheodore Ts'o found_flex_bg: 532a4912123STheodore Ts'o if (flex_size == 1) { 533a4912123STheodore Ts'o *group = grp; 534a4912123STheodore Ts'o return 0; 535a4912123STheodore Ts'o } 536a4912123STheodore Ts'o 537a4912123STheodore Ts'o /* 538a4912123STheodore Ts'o * We pack inodes at the beginning of the flexgroup's 539a4912123STheodore Ts'o * inode tables. Block allocation decisions will do 540a4912123STheodore Ts'o * something similar, although regular files will 541a4912123STheodore Ts'o * start at 2nd block group of the flexgroup. See 542a4912123STheodore Ts'o * ext4_ext_find_goal() and ext4_find_near(). 543a4912123STheodore Ts'o */ 544a4912123STheodore Ts'o grp *= flex_size; 545a4912123STheodore Ts'o for (i = 0; i < flex_size; i++) { 5468df9675fSTheodore Ts'o if (grp+i >= real_ngroups) 547a4912123STheodore Ts'o break; 548a4912123STheodore Ts'o desc = ext4_get_group_desc(sb, grp+i, NULL); 549a4912123STheodore Ts'o if (desc && ext4_free_inodes_count(sb, desc)) { 550a4912123STheodore Ts'o *group = grp+i; 551a4912123STheodore Ts'o return 0; 552a4912123STheodore Ts'o } 553a4912123STheodore Ts'o } 554ac27a0ecSDave Kleikamp goto fallback; 555ac27a0ecSDave Kleikamp } 556ac27a0ecSDave Kleikamp 557ac27a0ecSDave Kleikamp max_dirs = ndirs / ngroups + inodes_per_group / 16; 558a4912123STheodore Ts'o min_inodes = avefreei - inodes_per_group*flex_size / 4; 559a4912123STheodore Ts'o if (min_inodes < 1) 560a4912123STheodore Ts'o min_inodes = 1; 561a4912123STheodore Ts'o min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb)*flex_size / 4; 562ac27a0ecSDave Kleikamp 563a4912123STheodore Ts'o /* 564a4912123STheodore Ts'o * Start looking in the flex group where we last allocated an 565a4912123STheodore Ts'o * inode for this parent directory 566a4912123STheodore Ts'o */ 567a4912123STheodore Ts'o if (EXT4_I(parent)->i_last_alloc_group != ~0) { 568a4912123STheodore Ts'o parent_group = EXT4_I(parent)->i_last_alloc_group; 569a4912123STheodore Ts'o if (flex_size > 1) 570a4912123STheodore Ts'o parent_group >>= sbi->s_log_groups_per_flex; 571a4912123STheodore Ts'o } 572ac27a0ecSDave Kleikamp 573ac27a0ecSDave Kleikamp for (i = 0; i < ngroups; i++) { 574a4912123STheodore Ts'o grp = (parent_group + i) % ngroups; 575a4912123STheodore Ts'o get_orlov_stats(sb, grp, flex_size, &stats); 576a4912123STheodore Ts'o if (stats.used_dirs >= max_dirs) 577ac27a0ecSDave Kleikamp continue; 578a4912123STheodore Ts'o if (stats.free_inodes < min_inodes) 579ac27a0ecSDave Kleikamp continue; 580a4912123STheodore Ts'o if (stats.free_blocks < min_blocks) 581ac27a0ecSDave Kleikamp continue; 582a4912123STheodore Ts'o goto found_flex_bg; 583ac27a0ecSDave Kleikamp } 584ac27a0ecSDave Kleikamp 585ac27a0ecSDave Kleikamp fallback: 5868df9675fSTheodore Ts'o ngroups = real_ngroups; 587a4912123STheodore Ts'o avefreei = freei / ngroups; 588b5451f7bSTheodore Ts'o fallback_retry: 589a4912123STheodore Ts'o parent_group = EXT4_I(parent)->i_block_group; 590ac27a0ecSDave Kleikamp for (i = 0; i < ngroups; i++) { 591a4912123STheodore Ts'o grp = (parent_group + i) % ngroups; 592a4912123STheodore Ts'o desc = ext4_get_group_desc(sb, grp, NULL); 593560671a0SAneesh Kumar K.V if (desc && ext4_free_inodes_count(sb, desc) && 594a4912123STheodore Ts'o ext4_free_inodes_count(sb, desc) >= avefreei) { 595a4912123STheodore Ts'o *group = grp; 5962aa9fc4cSAvantika Mathur return 0; 597ac27a0ecSDave Kleikamp } 598a4912123STheodore Ts'o } 599ac27a0ecSDave Kleikamp 600ac27a0ecSDave Kleikamp if (avefreei) { 601ac27a0ecSDave Kleikamp /* 602ac27a0ecSDave Kleikamp * The free-inodes counter is approximate, and for really small 603ac27a0ecSDave Kleikamp * filesystems the above test can fail to find any blockgroups 604ac27a0ecSDave Kleikamp */ 605ac27a0ecSDave Kleikamp avefreei = 0; 606b5451f7bSTheodore Ts'o goto fallback_retry; 607ac27a0ecSDave Kleikamp } 608ac27a0ecSDave Kleikamp 609ac27a0ecSDave Kleikamp return -1; 610ac27a0ecSDave Kleikamp } 611ac27a0ecSDave Kleikamp 6122aa9fc4cSAvantika Mathur static int find_group_other(struct super_block *sb, struct inode *parent, 613a4912123STheodore Ts'o ext4_group_t *group, int mode) 614ac27a0ecSDave Kleikamp { 615fd2d4291SAvantika Mathur ext4_group_t parent_group = EXT4_I(parent)->i_block_group; 6168df9675fSTheodore Ts'o ext4_group_t i, last, ngroups = ext4_get_groups_count(sb); 617617ba13bSMingming Cao struct ext4_group_desc *desc; 618a4912123STheodore Ts'o int flex_size = ext4_flex_bg_size(EXT4_SB(sb)); 619a4912123STheodore Ts'o 620a4912123STheodore Ts'o /* 621a4912123STheodore Ts'o * Try to place the inode is the same flex group as its 622a4912123STheodore Ts'o * parent. If we can't find space, use the Orlov algorithm to 623a4912123STheodore Ts'o * find another flex group, and store that information in the 624a4912123STheodore Ts'o * parent directory's inode information so that use that flex 625a4912123STheodore Ts'o * group for future allocations. 626a4912123STheodore Ts'o */ 627a4912123STheodore Ts'o if (flex_size > 1) { 628a4912123STheodore Ts'o int retry = 0; 629a4912123STheodore Ts'o 630a4912123STheodore Ts'o try_again: 631a4912123STheodore Ts'o parent_group &= ~(flex_size-1); 632a4912123STheodore Ts'o last = parent_group + flex_size; 633a4912123STheodore Ts'o if (last > ngroups) 634a4912123STheodore Ts'o last = ngroups; 635a4912123STheodore Ts'o for (i = parent_group; i < last; i++) { 636a4912123STheodore Ts'o desc = ext4_get_group_desc(sb, i, NULL); 637a4912123STheodore Ts'o if (desc && ext4_free_inodes_count(sb, desc)) { 638a4912123STheodore Ts'o *group = i; 639a4912123STheodore Ts'o return 0; 640a4912123STheodore Ts'o } 641a4912123STheodore Ts'o } 642a4912123STheodore Ts'o if (!retry && EXT4_I(parent)->i_last_alloc_group != ~0) { 643a4912123STheodore Ts'o retry = 1; 644a4912123STheodore Ts'o parent_group = EXT4_I(parent)->i_last_alloc_group; 645a4912123STheodore Ts'o goto try_again; 646a4912123STheodore Ts'o } 647a4912123STheodore Ts'o /* 648a4912123STheodore Ts'o * If this didn't work, use the Orlov search algorithm 649a4912123STheodore Ts'o * to find a new flex group; we pass in the mode to 650a4912123STheodore Ts'o * avoid the topdir algorithms. 651a4912123STheodore Ts'o */ 652a4912123STheodore Ts'o *group = parent_group + flex_size; 653a4912123STheodore Ts'o if (*group > ngroups) 654a4912123STheodore Ts'o *group = 0; 655f157a4aaSTheodore Ts'o return find_group_orlov(sb, parent, group, mode, 0); 656a4912123STheodore Ts'o } 657ac27a0ecSDave Kleikamp 658ac27a0ecSDave Kleikamp /* 659ac27a0ecSDave Kleikamp * Try to place the inode in its parent directory 660ac27a0ecSDave Kleikamp */ 6612aa9fc4cSAvantika Mathur *group = parent_group; 6622aa9fc4cSAvantika Mathur desc = ext4_get_group_desc(sb, *group, NULL); 663560671a0SAneesh Kumar K.V if (desc && ext4_free_inodes_count(sb, desc) && 664560671a0SAneesh Kumar K.V ext4_free_blks_count(sb, desc)) 6652aa9fc4cSAvantika Mathur return 0; 666ac27a0ecSDave Kleikamp 667ac27a0ecSDave Kleikamp /* 668ac27a0ecSDave Kleikamp * We're going to place this inode in a different blockgroup from its 669ac27a0ecSDave Kleikamp * parent. We want to cause files in a common directory to all land in 670ac27a0ecSDave Kleikamp * the same blockgroup. But we want files which are in a different 671ac27a0ecSDave Kleikamp * directory which shares a blockgroup with our parent to land in a 672ac27a0ecSDave Kleikamp * different blockgroup. 673ac27a0ecSDave Kleikamp * 674ac27a0ecSDave Kleikamp * So add our directory's i_ino into the starting point for the hash. 675ac27a0ecSDave Kleikamp */ 6762aa9fc4cSAvantika Mathur *group = (*group + parent->i_ino) % ngroups; 677ac27a0ecSDave Kleikamp 678ac27a0ecSDave Kleikamp /* 679ac27a0ecSDave Kleikamp * Use a quadratic hash to find a group with a free inode and some free 680ac27a0ecSDave Kleikamp * blocks. 681ac27a0ecSDave Kleikamp */ 682ac27a0ecSDave Kleikamp for (i = 1; i < ngroups; i <<= 1) { 6832aa9fc4cSAvantika Mathur *group += i; 6842aa9fc4cSAvantika Mathur if (*group >= ngroups) 6852aa9fc4cSAvantika Mathur *group -= ngroups; 6862aa9fc4cSAvantika Mathur desc = ext4_get_group_desc(sb, *group, NULL); 687560671a0SAneesh Kumar K.V if (desc && ext4_free_inodes_count(sb, desc) && 688560671a0SAneesh Kumar K.V ext4_free_blks_count(sb, desc)) 6892aa9fc4cSAvantika Mathur return 0; 690ac27a0ecSDave Kleikamp } 691ac27a0ecSDave Kleikamp 692ac27a0ecSDave Kleikamp /* 693ac27a0ecSDave Kleikamp * That failed: try linear search for a free inode, even if that group 694ac27a0ecSDave Kleikamp * has no free blocks. 695ac27a0ecSDave Kleikamp */ 6962aa9fc4cSAvantika Mathur *group = parent_group; 697ac27a0ecSDave Kleikamp for (i = 0; i < ngroups; i++) { 6982aa9fc4cSAvantika Mathur if (++*group >= ngroups) 6992aa9fc4cSAvantika Mathur *group = 0; 7002aa9fc4cSAvantika Mathur desc = ext4_get_group_desc(sb, *group, NULL); 701560671a0SAneesh Kumar K.V if (desc && ext4_free_inodes_count(sb, desc)) 7022aa9fc4cSAvantika Mathur return 0; 703ac27a0ecSDave Kleikamp } 704ac27a0ecSDave Kleikamp 705ac27a0ecSDave Kleikamp return -1; 706ac27a0ecSDave Kleikamp } 707ac27a0ecSDave Kleikamp 708ac27a0ecSDave Kleikamp /* 70939341867SAneesh Kumar K.V * claim the inode from the inode bitmap. If the group 710955ce5f5SAneesh Kumar K.V * is uninit we need to take the groups's ext4_group_lock 71139341867SAneesh Kumar K.V * and clear the uninit flag. The inode bitmap update 71239341867SAneesh Kumar K.V * and group desc uninit flag clear should be done 713955ce5f5SAneesh Kumar K.V * after holding ext4_group_lock so that ext4_read_inode_bitmap 71439341867SAneesh Kumar K.V * doesn't race with the ext4_claim_inode 71539341867SAneesh Kumar K.V */ 71639341867SAneesh Kumar K.V static int ext4_claim_inode(struct super_block *sb, 71739341867SAneesh Kumar K.V struct buffer_head *inode_bitmap_bh, 71839341867SAneesh Kumar K.V unsigned long ino, ext4_group_t group, int mode) 71939341867SAneesh Kumar K.V { 72039341867SAneesh Kumar K.V int free = 0, retval = 0, count; 72139341867SAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(sb); 72239341867SAneesh Kumar K.V struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL); 72339341867SAneesh Kumar K.V 724955ce5f5SAneesh Kumar K.V ext4_lock_group(sb, group); 72539341867SAneesh Kumar K.V if (ext4_set_bit(ino, inode_bitmap_bh->b_data)) { 72639341867SAneesh Kumar K.V /* not a free inode */ 72739341867SAneesh Kumar K.V retval = 1; 72839341867SAneesh Kumar K.V goto err_ret; 72939341867SAneesh Kumar K.V } 73039341867SAneesh Kumar K.V ino++; 73139341867SAneesh Kumar K.V if ((group == 0 && ino < EXT4_FIRST_INO(sb)) || 73239341867SAneesh Kumar K.V ino > EXT4_INODES_PER_GROUP(sb)) { 733955ce5f5SAneesh Kumar K.V ext4_unlock_group(sb, group); 734*12062dddSEric Sandeen ext4_error(sb, "reserved inode or inode > inodes count - " 73539341867SAneesh Kumar K.V "block_group = %u, inode=%lu", group, 73639341867SAneesh Kumar K.V ino + group * EXT4_INODES_PER_GROUP(sb)); 73739341867SAneesh Kumar K.V return 1; 73839341867SAneesh Kumar K.V } 73939341867SAneesh Kumar K.V /* If we didn't allocate from within the initialized part of the inode 74039341867SAneesh Kumar K.V * table then we need to initialize up to this inode. */ 74139341867SAneesh Kumar K.V if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) { 74239341867SAneesh Kumar K.V 74339341867SAneesh Kumar K.V if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) { 74439341867SAneesh Kumar K.V gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT); 74539341867SAneesh Kumar K.V /* When marking the block group with 74639341867SAneesh Kumar K.V * ~EXT4_BG_INODE_UNINIT we don't want to depend 74739341867SAneesh Kumar K.V * on the value of bg_itable_unused even though 74839341867SAneesh Kumar K.V * mke2fs could have initialized the same for us. 74939341867SAneesh Kumar K.V * Instead we calculated the value below 75039341867SAneesh Kumar K.V */ 75139341867SAneesh Kumar K.V 75239341867SAneesh Kumar K.V free = 0; 75339341867SAneesh Kumar K.V } else { 75439341867SAneesh Kumar K.V free = EXT4_INODES_PER_GROUP(sb) - 75539341867SAneesh Kumar K.V ext4_itable_unused_count(sb, gdp); 75639341867SAneesh Kumar K.V } 75739341867SAneesh Kumar K.V 75839341867SAneesh Kumar K.V /* 75939341867SAneesh Kumar K.V * Check the relative inode number against the last used 76039341867SAneesh Kumar K.V * relative inode number in this group. if it is greater 76139341867SAneesh Kumar K.V * we need to update the bg_itable_unused count 76239341867SAneesh Kumar K.V * 76339341867SAneesh Kumar K.V */ 76439341867SAneesh Kumar K.V if (ino > free) 76539341867SAneesh Kumar K.V ext4_itable_unused_set(sb, gdp, 76639341867SAneesh Kumar K.V (EXT4_INODES_PER_GROUP(sb) - ino)); 76739341867SAneesh Kumar K.V } 76839341867SAneesh Kumar K.V count = ext4_free_inodes_count(sb, gdp) - 1; 76939341867SAneesh Kumar K.V ext4_free_inodes_set(sb, gdp, count); 77039341867SAneesh Kumar K.V if (S_ISDIR(mode)) { 77139341867SAneesh Kumar K.V count = ext4_used_dirs_count(sb, gdp) + 1; 77239341867SAneesh Kumar K.V ext4_used_dirs_set(sb, gdp, count); 7737d39db14STheodore Ts'o if (sbi->s_log_groups_per_flex) { 7747d39db14STheodore Ts'o ext4_group_t f = ext4_flex_group(sbi, group); 7757d39db14STheodore Ts'o 7767d39db14STheodore Ts'o atomic_inc(&sbi->s_flex_groups[f].free_inodes); 7777d39db14STheodore Ts'o } 77839341867SAneesh Kumar K.V } 77939341867SAneesh Kumar K.V gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp); 78039341867SAneesh Kumar K.V err_ret: 781955ce5f5SAneesh Kumar K.V ext4_unlock_group(sb, group); 78239341867SAneesh Kumar K.V return retval; 78339341867SAneesh Kumar K.V } 78439341867SAneesh Kumar K.V 78539341867SAneesh Kumar K.V /* 786ac27a0ecSDave Kleikamp * There are two policies for allocating an inode. If the new inode is 787ac27a0ecSDave Kleikamp * a directory, then a forward search is made for a block group with both 788ac27a0ecSDave Kleikamp * free space and a low directory-to-inode ratio; if that fails, then of 789ac27a0ecSDave Kleikamp * the groups with above-average free space, that group with the fewest 790ac27a0ecSDave Kleikamp * directories already is chosen. 791ac27a0ecSDave Kleikamp * 792ac27a0ecSDave Kleikamp * For other inodes, search forward from the parent directory's block 793ac27a0ecSDave Kleikamp * group to find a free inode. 794ac27a0ecSDave Kleikamp */ 795f157a4aaSTheodore Ts'o struct inode *ext4_new_inode(handle_t *handle, struct inode *dir, int mode, 79611013911SAndreas Dilger const struct qstr *qstr, __u32 goal) 797ac27a0ecSDave Kleikamp { 798ac27a0ecSDave Kleikamp struct super_block *sb; 7993300bedaSAneesh Kumar K.V struct buffer_head *inode_bitmap_bh = NULL; 8003300bedaSAneesh Kumar K.V struct buffer_head *group_desc_bh; 8018df9675fSTheodore Ts'o ext4_group_t ngroups, group = 0; 802ac27a0ecSDave Kleikamp unsigned long ino = 0; 803ac27a0ecSDave Kleikamp struct inode *inode; 804617ba13bSMingming Cao struct ext4_group_desc *gdp = NULL; 805617ba13bSMingming Cao struct ext4_inode_info *ei; 806617ba13bSMingming Cao struct ext4_sb_info *sbi; 80739341867SAneesh Kumar K.V int ret2, err = 0; 808ac27a0ecSDave Kleikamp struct inode *ret; 8092aa9fc4cSAvantika Mathur ext4_group_t i; 8102aa9fc4cSAvantika Mathur int free = 0; 8112842c3b5STheodore Ts'o static int once = 1; 812772cb7c8SJose R. Santos ext4_group_t flex_group; 813ac27a0ecSDave Kleikamp 814ac27a0ecSDave Kleikamp /* Cannot create files in a deleted directory */ 815ac27a0ecSDave Kleikamp if (!dir || !dir->i_nlink) 816ac27a0ecSDave Kleikamp return ERR_PTR(-EPERM); 817ac27a0ecSDave Kleikamp 818ac27a0ecSDave Kleikamp sb = dir->i_sb; 8198df9675fSTheodore Ts'o ngroups = ext4_get_groups_count(sb); 8209bffad1eSTheodore Ts'o trace_ext4_request_inode(dir, mode); 821ac27a0ecSDave Kleikamp inode = new_inode(sb); 822ac27a0ecSDave Kleikamp if (!inode) 823ac27a0ecSDave Kleikamp return ERR_PTR(-ENOMEM); 824617ba13bSMingming Cao ei = EXT4_I(inode); 825617ba13bSMingming Cao sbi = EXT4_SB(sb); 826772cb7c8SJose R. Santos 82711013911SAndreas Dilger if (!goal) 82811013911SAndreas Dilger goal = sbi->s_inode_goal; 82911013911SAndreas Dilger 830e6462869SJohann Lombardi if (goal && goal <= le32_to_cpu(sbi->s_es->s_inodes_count)) { 83111013911SAndreas Dilger group = (goal - 1) / EXT4_INODES_PER_GROUP(sb); 83211013911SAndreas Dilger ino = (goal - 1) % EXT4_INODES_PER_GROUP(sb); 83311013911SAndreas Dilger ret2 = 0; 83411013911SAndreas Dilger goto got_group; 83511013911SAndreas Dilger } 83611013911SAndreas Dilger 837a4912123STheodore Ts'o if (sbi->s_log_groups_per_flex && test_opt(sb, OLDALLOC)) { 838772cb7c8SJose R. Santos ret2 = find_group_flex(sb, dir, &group); 83905bf9e83STheodore Ts'o if (ret2 == -1) { 840a4912123STheodore Ts'o ret2 = find_group_other(sb, dir, &group, mode); 8416b82f3cbSChuck Ebbert if (ret2 == 0 && once) { 8422842c3b5STheodore Ts'o once = 0; 84305bf9e83STheodore Ts'o printk(KERN_NOTICE "ext4: find_group_flex " 84405bf9e83STheodore Ts'o "failed, fallback succeeded dir %lu\n", 84505bf9e83STheodore Ts'o dir->i_ino); 84605bf9e83STheodore Ts'o } 8476b82f3cbSChuck Ebbert } 848772cb7c8SJose R. Santos goto got_group; 849772cb7c8SJose R. Santos } 850772cb7c8SJose R. Santos 851ac27a0ecSDave Kleikamp if (S_ISDIR(mode)) { 852ac27a0ecSDave Kleikamp if (test_opt(sb, OLDALLOC)) 8532aa9fc4cSAvantika Mathur ret2 = find_group_dir(sb, dir, &group); 854ac27a0ecSDave Kleikamp else 855f157a4aaSTheodore Ts'o ret2 = find_group_orlov(sb, dir, &group, mode, qstr); 856ac27a0ecSDave Kleikamp } else 857a4912123STheodore Ts'o ret2 = find_group_other(sb, dir, &group, mode); 858ac27a0ecSDave Kleikamp 859772cb7c8SJose R. Santos got_group: 860a4912123STheodore Ts'o EXT4_I(dir)->i_last_alloc_group = group; 861ac27a0ecSDave Kleikamp err = -ENOSPC; 8622aa9fc4cSAvantika Mathur if (ret2 == -1) 863ac27a0ecSDave Kleikamp goto out; 864ac27a0ecSDave Kleikamp 86511013911SAndreas Dilger for (i = 0; i < ngroups; i++, ino = 0) { 866ac27a0ecSDave Kleikamp err = -EIO; 867ac27a0ecSDave Kleikamp 8683300bedaSAneesh Kumar K.V gdp = ext4_get_group_desc(sb, group, &group_desc_bh); 869ac27a0ecSDave Kleikamp if (!gdp) 870ac27a0ecSDave Kleikamp goto fail; 871ac27a0ecSDave Kleikamp 8723300bedaSAneesh Kumar K.V brelse(inode_bitmap_bh); 8733300bedaSAneesh Kumar K.V inode_bitmap_bh = ext4_read_inode_bitmap(sb, group); 8743300bedaSAneesh Kumar K.V if (!inode_bitmap_bh) 875ac27a0ecSDave Kleikamp goto fail; 876ac27a0ecSDave Kleikamp 877ac27a0ecSDave Kleikamp repeat_in_this_group: 878617ba13bSMingming Cao ino = ext4_find_next_zero_bit((unsigned long *) 8793300bedaSAneesh Kumar K.V inode_bitmap_bh->b_data, 8803300bedaSAneesh Kumar K.V EXT4_INODES_PER_GROUP(sb), ino); 8813300bedaSAneesh Kumar K.V 882617ba13bSMingming Cao if (ino < EXT4_INODES_PER_GROUP(sb)) { 883ac27a0ecSDave Kleikamp 8843300bedaSAneesh Kumar K.V BUFFER_TRACE(inode_bitmap_bh, "get_write_access"); 8853300bedaSAneesh Kumar K.V err = ext4_journal_get_write_access(handle, 8863300bedaSAneesh Kumar K.V inode_bitmap_bh); 887ac27a0ecSDave Kleikamp if (err) 888ac27a0ecSDave Kleikamp goto fail; 889ac27a0ecSDave Kleikamp 89039341867SAneesh Kumar K.V BUFFER_TRACE(group_desc_bh, "get_write_access"); 89139341867SAneesh Kumar K.V err = ext4_journal_get_write_access(handle, 89239341867SAneesh Kumar K.V group_desc_bh); 89339341867SAneesh Kumar K.V if (err) 89439341867SAneesh Kumar K.V goto fail; 89539341867SAneesh Kumar K.V if (!ext4_claim_inode(sb, inode_bitmap_bh, 89639341867SAneesh Kumar K.V ino, group, mode)) { 897ac27a0ecSDave Kleikamp /* we won it */ 8983300bedaSAneesh Kumar K.V BUFFER_TRACE(inode_bitmap_bh, 8990390131bSFrank Mayhar "call ext4_handle_dirty_metadata"); 9000390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, 9010390131bSFrank Mayhar inode, 9023300bedaSAneesh Kumar K.V inode_bitmap_bh); 903ac27a0ecSDave Kleikamp if (err) 904ac27a0ecSDave Kleikamp goto fail; 90539341867SAneesh Kumar K.V /* zero bit is inode number 1*/ 90639341867SAneesh Kumar K.V ino++; 907ac27a0ecSDave Kleikamp goto got; 908ac27a0ecSDave Kleikamp } 909ac27a0ecSDave Kleikamp /* we lost it */ 9103300bedaSAneesh Kumar K.V ext4_handle_release_buffer(handle, inode_bitmap_bh); 91139341867SAneesh Kumar K.V ext4_handle_release_buffer(handle, group_desc_bh); 912ac27a0ecSDave Kleikamp 913617ba13bSMingming Cao if (++ino < EXT4_INODES_PER_GROUP(sb)) 914ac27a0ecSDave Kleikamp goto repeat_in_this_group; 915ac27a0ecSDave Kleikamp } 916ac27a0ecSDave Kleikamp 917ac27a0ecSDave Kleikamp /* 918ac27a0ecSDave Kleikamp * This case is possible in concurrent environment. It is very 919ac27a0ecSDave Kleikamp * rare. We cannot repeat the find_group_xxx() call because 920ac27a0ecSDave Kleikamp * that will simply return the same blockgroup, because the 921ac27a0ecSDave Kleikamp * group descriptor metadata has not yet been updated. 922ac27a0ecSDave Kleikamp * So we just go onto the next blockgroup. 923ac27a0ecSDave Kleikamp */ 9248df9675fSTheodore Ts'o if (++group == ngroups) 925ac27a0ecSDave Kleikamp group = 0; 926ac27a0ecSDave Kleikamp } 927ac27a0ecSDave Kleikamp err = -ENOSPC; 928ac27a0ecSDave Kleikamp goto out; 929ac27a0ecSDave Kleikamp 930ac27a0ecSDave Kleikamp got: 931717d50e4SAndreas Dilger /* We may have to initialize the block bitmap if it isn't already */ 932717d50e4SAndreas Dilger if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) && 933717d50e4SAndreas Dilger gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { 9343300bedaSAneesh Kumar K.V struct buffer_head *block_bitmap_bh; 935717d50e4SAndreas Dilger 9363300bedaSAneesh Kumar K.V block_bitmap_bh = ext4_read_block_bitmap(sb, group); 9373300bedaSAneesh Kumar K.V BUFFER_TRACE(block_bitmap_bh, "get block bitmap access"); 9383300bedaSAneesh Kumar K.V err = ext4_journal_get_write_access(handle, block_bitmap_bh); 939717d50e4SAndreas Dilger if (err) { 9403300bedaSAneesh Kumar K.V brelse(block_bitmap_bh); 941717d50e4SAndreas Dilger goto fail; 942717d50e4SAndreas Dilger } 943717d50e4SAndreas Dilger 944717d50e4SAndreas Dilger free = 0; 945955ce5f5SAneesh Kumar K.V ext4_lock_group(sb, group); 946717d50e4SAndreas Dilger /* recheck and clear flag under lock if we still need to */ 947717d50e4SAndreas Dilger if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { 948717d50e4SAndreas Dilger free = ext4_free_blocks_after_init(sb, group, gdp); 9493300bedaSAneesh Kumar K.V gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT); 950560671a0SAneesh Kumar K.V ext4_free_blks_set(sb, gdp, free); 95123712a9cSFrederic Bohe gdp->bg_checksum = ext4_group_desc_csum(sbi, group, 95223712a9cSFrederic Bohe gdp); 953717d50e4SAndreas Dilger } 954955ce5f5SAneesh Kumar K.V ext4_unlock_group(sb, group); 955717d50e4SAndreas Dilger 956717d50e4SAndreas Dilger /* Don't need to dirty bitmap block if we didn't change it */ 957717d50e4SAndreas Dilger if (free) { 9583300bedaSAneesh Kumar K.V BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap"); 9590390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, 9603300bedaSAneesh Kumar K.V NULL, block_bitmap_bh); 961717d50e4SAndreas Dilger } 962717d50e4SAndreas Dilger 9633300bedaSAneesh Kumar K.V brelse(block_bitmap_bh); 964717d50e4SAndreas Dilger if (err) 965717d50e4SAndreas Dilger goto fail; 966717d50e4SAndreas Dilger } 9673300bedaSAneesh Kumar K.V BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata"); 9683300bedaSAneesh Kumar K.V err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh); 96939341867SAneesh Kumar K.V if (err) 97039341867SAneesh Kumar K.V goto fail; 971ac27a0ecSDave Kleikamp 972ac27a0ecSDave Kleikamp percpu_counter_dec(&sbi->s_freeinodes_counter); 973ac27a0ecSDave Kleikamp if (S_ISDIR(mode)) 974ac27a0ecSDave Kleikamp percpu_counter_inc(&sbi->s_dirs_counter); 975ac27a0ecSDave Kleikamp sb->s_dirt = 1; 976ac27a0ecSDave Kleikamp 977772cb7c8SJose R. Santos if (sbi->s_log_groups_per_flex) { 978772cb7c8SJose R. Santos flex_group = ext4_flex_group(sbi, group); 9799f24e420STheodore Ts'o atomic_dec(&sbi->s_flex_groups[flex_group].free_inodes); 980772cb7c8SJose R. Santos } 981772cb7c8SJose R. Santos 9824c9c544eSDavid Howells inode->i_uid = current_fsuid(); 983ac27a0ecSDave Kleikamp if (test_opt(sb, GRPID)) 984ac27a0ecSDave Kleikamp inode->i_gid = dir->i_gid; 985ac27a0ecSDave Kleikamp else if (dir->i_mode & S_ISGID) { 986ac27a0ecSDave Kleikamp inode->i_gid = dir->i_gid; 987ac27a0ecSDave Kleikamp if (S_ISDIR(mode)) 988ac27a0ecSDave Kleikamp mode |= S_ISGID; 989ac27a0ecSDave Kleikamp } else 9904c9c544eSDavid Howells inode->i_gid = current_fsgid(); 991ac27a0ecSDave Kleikamp inode->i_mode = mode; 992ac27a0ecSDave Kleikamp 993717d50e4SAndreas Dilger inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb); 994ac27a0ecSDave Kleikamp /* This is the optimal IO size (for stat), not the fs block size */ 995ac27a0ecSDave Kleikamp inode->i_blocks = 0; 996ef7f3835SKalpak Shah inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime = 997ef7f3835SKalpak Shah ext4_current_time(inode); 998ac27a0ecSDave Kleikamp 999ac27a0ecSDave Kleikamp memset(ei->i_data, 0, sizeof(ei->i_data)); 1000ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 1001ac27a0ecSDave Kleikamp ei->i_disksize = 0; 1002ac27a0ecSDave Kleikamp 100342bf0383SAneesh Kumar K.V /* 10042dc6b0d4SDuane Griffin * Don't inherit extent flag from directory, amongst others. We set 10052dc6b0d4SDuane Griffin * extent flag on newly created directory and file only if -o extent 10062dc6b0d4SDuane Griffin * mount option is specified 100742bf0383SAneesh Kumar K.V */ 10082dc6b0d4SDuane Griffin ei->i_flags = 10092dc6b0d4SDuane Griffin ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED); 1010ac27a0ecSDave Kleikamp ei->i_file_acl = 0; 1011ac27a0ecSDave Kleikamp ei->i_dtime = 0; 1012ac27a0ecSDave Kleikamp ei->i_block_group = group; 1013a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 1014ac27a0ecSDave Kleikamp 1015617ba13bSMingming Cao ext4_set_inode_flags(inode); 1016ac27a0ecSDave Kleikamp if (IS_DIRSYNC(inode)) 10170390131bSFrank Mayhar ext4_handle_sync(handle); 10186b38e842SAl Viro if (insert_inode_locked(inode) < 0) { 10196b38e842SAl Viro err = -EINVAL; 10206b38e842SAl Viro goto fail_drop; 10216b38e842SAl Viro } 1022ac27a0ecSDave Kleikamp spin_lock(&sbi->s_next_gen_lock); 1023ac27a0ecSDave Kleikamp inode->i_generation = sbi->s_next_generation++; 1024ac27a0ecSDave Kleikamp spin_unlock(&sbi->s_next_gen_lock); 1025ac27a0ecSDave Kleikamp 102619f5fb7aSTheodore Ts'o ei->i_state_flags = 0; 102719f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_NEW); 1028ef7f3835SKalpak Shah 1029ef7f3835SKalpak Shah ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize; 1030ac27a0ecSDave Kleikamp 1031ac27a0ecSDave Kleikamp ret = inode; 1032a269eb18SJan Kara if (vfs_dq_alloc_inode(inode)) { 1033ac27a0ecSDave Kleikamp err = -EDQUOT; 1034ac27a0ecSDave Kleikamp goto fail_drop; 1035ac27a0ecSDave Kleikamp } 1036ac27a0ecSDave Kleikamp 1037617ba13bSMingming Cao err = ext4_init_acl(handle, inode, dir); 1038ac27a0ecSDave Kleikamp if (err) 1039ac27a0ecSDave Kleikamp goto fail_free_drop; 1040ac27a0ecSDave Kleikamp 1041617ba13bSMingming Cao err = ext4_init_security(handle, inode, dir); 1042ac27a0ecSDave Kleikamp if (err) 1043ac27a0ecSDave Kleikamp goto fail_free_drop; 1044ac27a0ecSDave Kleikamp 104583982b6fSTheodore Ts'o if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) { 1046e4079a11SEric Sandeen /* set extent flag only for directory, file and normal symlink*/ 1047e65187e6SAneesh Kumar K.V if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) { 1048a86c6181SAlex Tomas EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL; 1049a86c6181SAlex Tomas ext4_ext_tree_init(handle, inode); 1050a86c6181SAlex Tomas } 105142bf0383SAneesh Kumar K.V } 1052ac27a0ecSDave Kleikamp 10538753e88fSAneesh Kumar K.V err = ext4_mark_inode_dirty(handle, inode); 10548753e88fSAneesh Kumar K.V if (err) { 10558753e88fSAneesh Kumar K.V ext4_std_error(sb, err); 10568753e88fSAneesh Kumar K.V goto fail_free_drop; 10578753e88fSAneesh Kumar K.V } 10588753e88fSAneesh Kumar K.V 1059617ba13bSMingming Cao ext4_debug("allocating inode %lu\n", inode->i_ino); 10609bffad1eSTheodore Ts'o trace_ext4_allocate_inode(inode, dir, mode); 1061ac27a0ecSDave Kleikamp goto really_out; 1062ac27a0ecSDave Kleikamp fail: 1063617ba13bSMingming Cao ext4_std_error(sb, err); 1064ac27a0ecSDave Kleikamp out: 1065ac27a0ecSDave Kleikamp iput(inode); 1066ac27a0ecSDave Kleikamp ret = ERR_PTR(err); 1067ac27a0ecSDave Kleikamp really_out: 10683300bedaSAneesh Kumar K.V brelse(inode_bitmap_bh); 1069ac27a0ecSDave Kleikamp return ret; 1070ac27a0ecSDave Kleikamp 1071ac27a0ecSDave Kleikamp fail_free_drop: 1072a269eb18SJan Kara vfs_dq_free_inode(inode); 1073ac27a0ecSDave Kleikamp 1074ac27a0ecSDave Kleikamp fail_drop: 1075a269eb18SJan Kara vfs_dq_drop(inode); 1076ac27a0ecSDave Kleikamp inode->i_flags |= S_NOQUOTA; 1077ac27a0ecSDave Kleikamp inode->i_nlink = 0; 10786b38e842SAl Viro unlock_new_inode(inode); 1079ac27a0ecSDave Kleikamp iput(inode); 10803300bedaSAneesh Kumar K.V brelse(inode_bitmap_bh); 1081ac27a0ecSDave Kleikamp return ERR_PTR(err); 1082ac27a0ecSDave Kleikamp } 1083ac27a0ecSDave Kleikamp 1084ac27a0ecSDave Kleikamp /* Verify that we are loading a valid orphan from disk */ 1085617ba13bSMingming Cao struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino) 1086ac27a0ecSDave Kleikamp { 1087617ba13bSMingming Cao unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count); 1088fd2d4291SAvantika Mathur ext4_group_t block_group; 1089ac27a0ecSDave Kleikamp int bit; 10901d1fe1eeSDavid Howells struct buffer_head *bitmap_bh; 1091ac27a0ecSDave Kleikamp struct inode *inode = NULL; 10921d1fe1eeSDavid Howells long err = -EIO; 1093ac27a0ecSDave Kleikamp 1094ac27a0ecSDave Kleikamp /* Error cases - e2fsck has already cleaned up for us */ 1095ac27a0ecSDave Kleikamp if (ino > max_ino) { 1096*12062dddSEric Sandeen ext4_warning(sb, "bad orphan ino %lu! e2fsck was run?", ino); 10971d1fe1eeSDavid Howells goto error; 1098ac27a0ecSDave Kleikamp } 1099ac27a0ecSDave Kleikamp 1100617ba13bSMingming Cao block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); 1101617ba13bSMingming Cao bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); 1102e29d1cdeSEric Sandeen bitmap_bh = ext4_read_inode_bitmap(sb, block_group); 1103ac27a0ecSDave Kleikamp if (!bitmap_bh) { 1104*12062dddSEric Sandeen ext4_warning(sb, "inode bitmap error for orphan %lu", ino); 11051d1fe1eeSDavid Howells goto error; 1106ac27a0ecSDave Kleikamp } 1107ac27a0ecSDave Kleikamp 1108ac27a0ecSDave Kleikamp /* Having the inode bit set should be a 100% indicator that this 1109ac27a0ecSDave Kleikamp * is a valid orphan (no e2fsck run on fs). Orphans also include 1110ac27a0ecSDave Kleikamp * inodes that were being truncated, so we can't check i_nlink==0. 1111ac27a0ecSDave Kleikamp */ 11121d1fe1eeSDavid Howells if (!ext4_test_bit(bit, bitmap_bh->b_data)) 11131d1fe1eeSDavid Howells goto bad_orphan; 11141d1fe1eeSDavid Howells 11151d1fe1eeSDavid Howells inode = ext4_iget(sb, ino); 11161d1fe1eeSDavid Howells if (IS_ERR(inode)) 11171d1fe1eeSDavid Howells goto iget_failed; 11181d1fe1eeSDavid Howells 111991ef4cafSDuane Griffin /* 112091ef4cafSDuane Griffin * If the orphans has i_nlinks > 0 then it should be able to be 112191ef4cafSDuane Griffin * truncated, otherwise it won't be removed from the orphan list 112291ef4cafSDuane Griffin * during processing and an infinite loop will result. 112391ef4cafSDuane Griffin */ 112491ef4cafSDuane Griffin if (inode->i_nlink && !ext4_can_truncate(inode)) 112591ef4cafSDuane Griffin goto bad_orphan; 112691ef4cafSDuane Griffin 11271d1fe1eeSDavid Howells if (NEXT_ORPHAN(inode) > max_ino) 11281d1fe1eeSDavid Howells goto bad_orphan; 11291d1fe1eeSDavid Howells brelse(bitmap_bh); 11301d1fe1eeSDavid Howells return inode; 11311d1fe1eeSDavid Howells 11321d1fe1eeSDavid Howells iget_failed: 11331d1fe1eeSDavid Howells err = PTR_ERR(inode); 11341d1fe1eeSDavid Howells inode = NULL; 11351d1fe1eeSDavid Howells bad_orphan: 1136*12062dddSEric Sandeen ext4_warning(sb, "bad orphan inode %lu! e2fsck was run?", ino); 1137617ba13bSMingming Cao printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n", 1138ac27a0ecSDave Kleikamp bit, (unsigned long long)bitmap_bh->b_blocknr, 1139617ba13bSMingming Cao ext4_test_bit(bit, bitmap_bh->b_data)); 1140ac27a0ecSDave Kleikamp printk(KERN_NOTICE "inode=%p\n", inode); 1141ac27a0ecSDave Kleikamp if (inode) { 1142ac27a0ecSDave Kleikamp printk(KERN_NOTICE "is_bad_inode(inode)=%d\n", 1143ac27a0ecSDave Kleikamp is_bad_inode(inode)); 1144ac27a0ecSDave Kleikamp printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n", 1145ac27a0ecSDave Kleikamp NEXT_ORPHAN(inode)); 1146ac27a0ecSDave Kleikamp printk(KERN_NOTICE "max_ino=%lu\n", max_ino); 114791ef4cafSDuane Griffin printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink); 1148ac27a0ecSDave Kleikamp /* Avoid freeing blocks if we got a bad deleted inode */ 11491d1fe1eeSDavid Howells if (inode->i_nlink == 0) 1150ac27a0ecSDave Kleikamp inode->i_blocks = 0; 1151ac27a0ecSDave Kleikamp iput(inode); 1152ac27a0ecSDave Kleikamp } 1153ac27a0ecSDave Kleikamp brelse(bitmap_bh); 11541d1fe1eeSDavid Howells error: 11551d1fe1eeSDavid Howells return ERR_PTR(err); 1156ac27a0ecSDave Kleikamp } 1157ac27a0ecSDave Kleikamp 1158617ba13bSMingming Cao unsigned long ext4_count_free_inodes(struct super_block *sb) 1159ac27a0ecSDave Kleikamp { 1160ac27a0ecSDave Kleikamp unsigned long desc_count; 1161617ba13bSMingming Cao struct ext4_group_desc *gdp; 11628df9675fSTheodore Ts'o ext4_group_t i, ngroups = ext4_get_groups_count(sb); 1163617ba13bSMingming Cao #ifdef EXT4FS_DEBUG 1164617ba13bSMingming Cao struct ext4_super_block *es; 1165ac27a0ecSDave Kleikamp unsigned long bitmap_count, x; 1166ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh = NULL; 1167ac27a0ecSDave Kleikamp 1168617ba13bSMingming Cao es = EXT4_SB(sb)->s_es; 1169ac27a0ecSDave Kleikamp desc_count = 0; 1170ac27a0ecSDave Kleikamp bitmap_count = 0; 1171ac27a0ecSDave Kleikamp gdp = NULL; 11728df9675fSTheodore Ts'o for (i = 0; i < ngroups; i++) { 1173617ba13bSMingming Cao gdp = ext4_get_group_desc(sb, i, NULL); 1174ac27a0ecSDave Kleikamp if (!gdp) 1175ac27a0ecSDave Kleikamp continue; 1176560671a0SAneesh Kumar K.V desc_count += ext4_free_inodes_count(sb, gdp); 1177ac27a0ecSDave Kleikamp brelse(bitmap_bh); 1178e29d1cdeSEric Sandeen bitmap_bh = ext4_read_inode_bitmap(sb, i); 1179ac27a0ecSDave Kleikamp if (!bitmap_bh) 1180ac27a0ecSDave Kleikamp continue; 1181ac27a0ecSDave Kleikamp 1182617ba13bSMingming Cao x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8); 1183c549a95dSEric Sandeen printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n", 1184785b4b3aSPeng Tao (unsigned long) i, ext4_free_inodes_count(sb, gdp), x); 1185ac27a0ecSDave Kleikamp bitmap_count += x; 1186ac27a0ecSDave Kleikamp } 1187ac27a0ecSDave Kleikamp brelse(bitmap_bh); 11884776004fSTheodore Ts'o printk(KERN_DEBUG "ext4_count_free_inodes: " 11894776004fSTheodore Ts'o "stored = %u, computed = %lu, %lu\n", 1190ac27a0ecSDave Kleikamp le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count); 1191ac27a0ecSDave Kleikamp return desc_count; 1192ac27a0ecSDave Kleikamp #else 1193ac27a0ecSDave Kleikamp desc_count = 0; 11948df9675fSTheodore Ts'o for (i = 0; i < ngroups; i++) { 1195617ba13bSMingming Cao gdp = ext4_get_group_desc(sb, i, NULL); 1196ac27a0ecSDave Kleikamp if (!gdp) 1197ac27a0ecSDave Kleikamp continue; 1198560671a0SAneesh Kumar K.V desc_count += ext4_free_inodes_count(sb, gdp); 1199ac27a0ecSDave Kleikamp cond_resched(); 1200ac27a0ecSDave Kleikamp } 1201ac27a0ecSDave Kleikamp return desc_count; 1202ac27a0ecSDave Kleikamp #endif 1203ac27a0ecSDave Kleikamp } 1204ac27a0ecSDave Kleikamp 1205ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */ 1206617ba13bSMingming Cao unsigned long ext4_count_dirs(struct super_block * sb) 1207ac27a0ecSDave Kleikamp { 1208ac27a0ecSDave Kleikamp unsigned long count = 0; 12098df9675fSTheodore Ts'o ext4_group_t i, ngroups = ext4_get_groups_count(sb); 1210ac27a0ecSDave Kleikamp 12118df9675fSTheodore Ts'o for (i = 0; i < ngroups; i++) { 1212617ba13bSMingming Cao struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL); 1213ac27a0ecSDave Kleikamp if (!gdp) 1214ac27a0ecSDave Kleikamp continue; 1215560671a0SAneesh Kumar K.V count += ext4_used_dirs_count(sb, gdp); 1216ac27a0ecSDave Kleikamp } 1217ac27a0ecSDave Kleikamp return count; 1218ac27a0ecSDave Kleikamp } 1219