1*b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2ac27a0ecSDave Kleikamp /* 3617ba13bSMingming Cao * linux/fs/ext4/ialloc.c 4ac27a0ecSDave Kleikamp * 5ac27a0ecSDave Kleikamp * Copyright (C) 1992, 1993, 1994, 1995 6ac27a0ecSDave Kleikamp * Remy Card (card@masi.ibp.fr) 7ac27a0ecSDave Kleikamp * Laboratoire MASI - Institut Blaise Pascal 8ac27a0ecSDave Kleikamp * Universite Pierre et Marie Curie (Paris VI) 9ac27a0ecSDave Kleikamp * 10ac27a0ecSDave Kleikamp * BSD ufs-inspired inode and directory allocation by 11ac27a0ecSDave Kleikamp * Stephen Tweedie (sct@redhat.com), 1993 12ac27a0ecSDave Kleikamp * Big-endian to little-endian byte-swapping/bitmaps by 13ac27a0ecSDave Kleikamp * David S. Miller (davem@caip.rutgers.edu), 1995 14ac27a0ecSDave Kleikamp */ 15ac27a0ecSDave Kleikamp 16ac27a0ecSDave Kleikamp #include <linux/time.h> 17ac27a0ecSDave Kleikamp #include <linux/fs.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> 255b825c3aSIngo Molnar #include <linux/cred.h> 265b825c3aSIngo Molnar 27ac27a0ecSDave Kleikamp #include <asm/byteorder.h> 289bffad1eSTheodore Ts'o 293dcf5451SChristoph Hellwig #include "ext4.h" 303dcf5451SChristoph Hellwig #include "ext4_jbd2.h" 31ac27a0ecSDave Kleikamp #include "xattr.h" 32ac27a0ecSDave Kleikamp #include "acl.h" 33ac27a0ecSDave Kleikamp 349bffad1eSTheodore Ts'o #include <trace/events/ext4.h> 359bffad1eSTheodore Ts'o 36ac27a0ecSDave Kleikamp /* 37ac27a0ecSDave Kleikamp * ialloc.c contains the inodes allocation and deallocation routines 38ac27a0ecSDave Kleikamp */ 39ac27a0ecSDave Kleikamp 40ac27a0ecSDave Kleikamp /* 41ac27a0ecSDave Kleikamp * The free inodes are managed by bitmaps. A file system contains several 42ac27a0ecSDave Kleikamp * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap 43ac27a0ecSDave Kleikamp * block for inodes, N blocks for the inode table and data blocks. 44ac27a0ecSDave Kleikamp * 45ac27a0ecSDave Kleikamp * The file system contains group descriptors which are located after the 46ac27a0ecSDave Kleikamp * super block. Each descriptor contains the number of the bitmap block and 47ac27a0ecSDave Kleikamp * the free blocks count in the block. 48ac27a0ecSDave Kleikamp */ 49ac27a0ecSDave Kleikamp 50717d50e4SAndreas Dilger /* 51717d50e4SAndreas Dilger * To avoid calling the atomic setbit hundreds or thousands of times, we only 52717d50e4SAndreas Dilger * need to use it within a single byte (to ensure we get endianness right). 53717d50e4SAndreas Dilger * We can use memset for the rest of the bitmap as there are no other users. 54717d50e4SAndreas Dilger */ 5561d08673STheodore Ts'o void ext4_mark_bitmap_end(int start_bit, int end_bit, char *bitmap) 56717d50e4SAndreas Dilger { 57717d50e4SAndreas Dilger int i; 58717d50e4SAndreas Dilger 59717d50e4SAndreas Dilger if (start_bit >= end_bit) 60717d50e4SAndreas Dilger return; 61717d50e4SAndreas Dilger 62717d50e4SAndreas Dilger ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit); 63717d50e4SAndreas Dilger for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++) 64717d50e4SAndreas Dilger ext4_set_bit(i, bitmap); 65717d50e4SAndreas Dilger if (i < end_bit) 66717d50e4SAndreas Dilger memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3); 67717d50e4SAndreas Dilger } 68717d50e4SAndreas Dilger 69717d50e4SAndreas Dilger /* Initializes an uninitialized inode bitmap */ 709008a58eSDarrick J. Wong static int ext4_init_inode_bitmap(struct super_block *sb, 711f109d5aSTheodore Ts'o struct buffer_head *bh, 72fd2d4291SAvantika Mathur ext4_group_t block_group, 73717d50e4SAndreas Dilger struct ext4_group_desc *gdp) 74717d50e4SAndreas Dilger { 75bdfb6ff4SDarrick J. Wong struct ext4_group_info *grp; 76e43bb4e6SNamjae Jeon struct ext4_sb_info *sbi = EXT4_SB(sb); 77717d50e4SAndreas Dilger J_ASSERT_BH(bh, buffer_locked(bh)); 78717d50e4SAndreas Dilger 79717d50e4SAndreas Dilger /* If checksum is bad mark all blocks and inodes use to prevent 80717d50e4SAndreas Dilger * allocation, essentially implementing a per-group read-only flag. */ 81feb0ab32SDarrick J. Wong if (!ext4_group_desc_csum_verify(sb, block_group, gdp)) { 82bdfb6ff4SDarrick J. Wong grp = ext4_get_group_info(sb, block_group); 83e43bb4e6SNamjae Jeon if (!EXT4_MB_GRP_BBITMAP_CORRUPT(grp)) 84e43bb4e6SNamjae Jeon percpu_counter_sub(&sbi->s_freeclusters_counter, 85e43bb4e6SNamjae Jeon grp->bb_free); 86bdfb6ff4SDarrick J. Wong set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT, &grp->bb_state); 87e43bb4e6SNamjae Jeon if (!EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) { 88e43bb4e6SNamjae Jeon int count; 89e43bb4e6SNamjae Jeon count = ext4_free_inodes_count(sb, gdp); 90e43bb4e6SNamjae Jeon percpu_counter_sub(&sbi->s_freeinodes_counter, 91e43bb4e6SNamjae Jeon count); 92e43bb4e6SNamjae Jeon } 93bdfb6ff4SDarrick J. Wong set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &grp->bb_state); 949008a58eSDarrick J. Wong return -EFSBADCRC; 95717d50e4SAndreas Dilger } 96717d50e4SAndreas Dilger 97717d50e4SAndreas Dilger memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8); 9861d08673STheodore Ts'o ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), sb->s_blocksize * 8, 99717d50e4SAndreas Dilger bh->b_data); 10041a246d1SDarrick J. Wong ext4_inode_bitmap_csum_set(sb, block_group, gdp, bh, 10141a246d1SDarrick J. Wong EXT4_INODES_PER_GROUP(sb) / 8); 102feb0ab32SDarrick J. Wong ext4_group_desc_csum_set(sb, block_group, gdp); 103717d50e4SAndreas Dilger 1049008a58eSDarrick J. Wong return 0; 105717d50e4SAndreas Dilger } 106ac27a0ecSDave Kleikamp 107813e5727STheodore Ts'o void ext4_end_bitmap_read(struct buffer_head *bh, int uptodate) 108813e5727STheodore Ts'o { 109813e5727STheodore Ts'o if (uptodate) { 110813e5727STheodore Ts'o set_buffer_uptodate(bh); 111813e5727STheodore Ts'o set_bitmap_uptodate(bh); 112813e5727STheodore Ts'o } 113813e5727STheodore Ts'o unlock_buffer(bh); 114813e5727STheodore Ts'o put_bh(bh); 115813e5727STheodore Ts'o } 116813e5727STheodore Ts'o 1179008a58eSDarrick J. Wong static int ext4_validate_inode_bitmap(struct super_block *sb, 1189008a58eSDarrick J. Wong struct ext4_group_desc *desc, 1199008a58eSDarrick J. Wong ext4_group_t block_group, 1209008a58eSDarrick J. Wong struct buffer_head *bh) 1219008a58eSDarrick J. Wong { 1229008a58eSDarrick J. Wong ext4_fsblk_t blk; 1239008a58eSDarrick J. Wong struct ext4_group_info *grp = ext4_get_group_info(sb, block_group); 1249008a58eSDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(sb); 1259008a58eSDarrick J. Wong 1269008a58eSDarrick J. Wong if (buffer_verified(bh)) 1279008a58eSDarrick J. Wong return 0; 1289008a58eSDarrick J. Wong if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) 1299008a58eSDarrick J. Wong return -EFSCORRUPTED; 1309008a58eSDarrick J. Wong 1319008a58eSDarrick J. Wong ext4_lock_group(sb, block_group); 1329008a58eSDarrick J. Wong blk = ext4_inode_bitmap(sb, desc); 1339008a58eSDarrick J. Wong if (!ext4_inode_bitmap_csum_verify(sb, block_group, desc, bh, 1349008a58eSDarrick J. Wong EXT4_INODES_PER_GROUP(sb) / 8)) { 1359008a58eSDarrick J. Wong ext4_unlock_group(sb, block_group); 1369008a58eSDarrick J. Wong ext4_error(sb, "Corrupt inode bitmap - block_group = %u, " 1379008a58eSDarrick J. Wong "inode_bitmap = %llu", block_group, blk); 1389008a58eSDarrick J. Wong grp = ext4_get_group_info(sb, block_group); 1399008a58eSDarrick J. Wong if (!EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) { 1409008a58eSDarrick J. Wong int count; 1419008a58eSDarrick J. Wong count = ext4_free_inodes_count(sb, desc); 1429008a58eSDarrick J. Wong percpu_counter_sub(&sbi->s_freeinodes_counter, 1439008a58eSDarrick J. Wong count); 1449008a58eSDarrick J. Wong } 1459008a58eSDarrick J. Wong set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &grp->bb_state); 1469008a58eSDarrick J. Wong return -EFSBADCRC; 1479008a58eSDarrick J. Wong } 1489008a58eSDarrick J. Wong set_buffer_verified(bh); 1499008a58eSDarrick J. Wong ext4_unlock_group(sb, block_group); 1509008a58eSDarrick J. Wong return 0; 1519008a58eSDarrick J. Wong } 1529008a58eSDarrick J. Wong 153ac27a0ecSDave Kleikamp /* 154ac27a0ecSDave Kleikamp * Read the inode allocation bitmap for a given block_group, reading 155ac27a0ecSDave Kleikamp * into the specified slot in the superblock's bitmap cache. 156ac27a0ecSDave Kleikamp * 157ac27a0ecSDave Kleikamp * Return buffer_head of bitmap on success or NULL. 158ac27a0ecSDave Kleikamp */ 159ac27a0ecSDave Kleikamp static struct buffer_head * 160e29d1cdeSEric Sandeen ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group) 161ac27a0ecSDave Kleikamp { 162617ba13bSMingming Cao struct ext4_group_desc *desc; 163ac27a0ecSDave Kleikamp struct buffer_head *bh = NULL; 164e29d1cdeSEric Sandeen ext4_fsblk_t bitmap_blk; 1659008a58eSDarrick J. Wong int err; 166ac27a0ecSDave Kleikamp 167617ba13bSMingming Cao desc = ext4_get_group_desc(sb, block_group, NULL); 168ac27a0ecSDave Kleikamp if (!desc) 1699008a58eSDarrick J. Wong return ERR_PTR(-EFSCORRUPTED); 170bfff6873SLukas Czerner 171e29d1cdeSEric Sandeen bitmap_blk = ext4_inode_bitmap(sb, desc); 172e29d1cdeSEric Sandeen bh = sb_getblk(sb, bitmap_blk); 173e29d1cdeSEric Sandeen if (unlikely(!bh)) { 17412062dddSEric Sandeen ext4_error(sb, "Cannot read inode bitmap - " 175a9df9a49STheodore Ts'o "block_group = %u, inode_bitmap = %llu", 176e29d1cdeSEric Sandeen block_group, bitmap_blk); 1779008a58eSDarrick J. Wong return ERR_PTR(-EIO); 178e29d1cdeSEric Sandeen } 1792ccb5fb9SAneesh Kumar K.V if (bitmap_uptodate(bh)) 18041a246d1SDarrick J. Wong goto verify; 181e29d1cdeSEric Sandeen 182c806e68fSFrederic Bohe lock_buffer(bh); 1832ccb5fb9SAneesh Kumar K.V if (bitmap_uptodate(bh)) { 1842ccb5fb9SAneesh Kumar K.V unlock_buffer(bh); 18541a246d1SDarrick J. Wong goto verify; 1862ccb5fb9SAneesh Kumar K.V } 187bfff6873SLukas Czerner 188955ce5f5SAneesh Kumar K.V ext4_lock_group(sb, block_group); 189e29d1cdeSEric Sandeen if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) { 1909008a58eSDarrick J. Wong err = ext4_init_inode_bitmap(sb, bh, block_group, desc); 1912ccb5fb9SAneesh Kumar K.V set_bitmap_uptodate(bh); 192e29d1cdeSEric Sandeen set_buffer_uptodate(bh); 19341a246d1SDarrick J. Wong set_buffer_verified(bh); 194955ce5f5SAneesh Kumar K.V ext4_unlock_group(sb, block_group); 1953300bedaSAneesh Kumar K.V unlock_buffer(bh); 19605145bd7SJan Kara if (err) { 19705145bd7SJan Kara ext4_error(sb, "Failed to init inode bitmap for group " 19805145bd7SJan Kara "%u: %d", block_group, err); 1999008a58eSDarrick J. Wong goto out; 20005145bd7SJan Kara } 201e29d1cdeSEric Sandeen return bh; 202e29d1cdeSEric Sandeen } 203955ce5f5SAneesh Kumar K.V ext4_unlock_group(sb, block_group); 204bfff6873SLukas Czerner 2052ccb5fb9SAneesh Kumar K.V if (buffer_uptodate(bh)) { 2062ccb5fb9SAneesh Kumar K.V /* 2072ccb5fb9SAneesh Kumar K.V * if not uninit if bh is uptodate, 2082ccb5fb9SAneesh Kumar K.V * bitmap is also uptodate 2092ccb5fb9SAneesh Kumar K.V */ 2102ccb5fb9SAneesh Kumar K.V set_bitmap_uptodate(bh); 2112ccb5fb9SAneesh Kumar K.V unlock_buffer(bh); 21241a246d1SDarrick J. Wong goto verify; 2132ccb5fb9SAneesh Kumar K.V } 2142ccb5fb9SAneesh Kumar K.V /* 215813e5727STheodore Ts'o * submit the buffer_head for reading 2162ccb5fb9SAneesh Kumar K.V */ 2170562e0baSJiaying Zhang trace_ext4_load_inode_bitmap(sb, block_group); 218813e5727STheodore Ts'o bh->b_end_io = ext4_end_bitmap_read; 219813e5727STheodore Ts'o get_bh(bh); 2202a222ca9SMike Christie submit_bh(REQ_OP_READ, REQ_META | REQ_PRIO, bh); 221813e5727STheodore Ts'o wait_on_buffer(bh); 222813e5727STheodore Ts'o if (!buffer_uptodate(bh)) { 223e29d1cdeSEric Sandeen put_bh(bh); 22412062dddSEric Sandeen ext4_error(sb, "Cannot read inode bitmap - " 225a9df9a49STheodore Ts'o "block_group = %u, inode_bitmap = %llu", 226e29d1cdeSEric Sandeen block_group, bitmap_blk); 2279008a58eSDarrick J. Wong return ERR_PTR(-EIO); 228e29d1cdeSEric Sandeen } 22941a246d1SDarrick J. Wong 23041a246d1SDarrick J. Wong verify: 2319008a58eSDarrick J. Wong err = ext4_validate_inode_bitmap(sb, desc, block_group, bh); 2329008a58eSDarrick J. Wong if (err) 2339008a58eSDarrick J. Wong goto out; 234ac27a0ecSDave Kleikamp return bh; 2359008a58eSDarrick J. Wong out: 2369008a58eSDarrick J. Wong put_bh(bh); 2379008a58eSDarrick J. Wong return ERR_PTR(err); 238ac27a0ecSDave Kleikamp } 239ac27a0ecSDave Kleikamp 240ac27a0ecSDave Kleikamp /* 241ac27a0ecSDave Kleikamp * NOTE! When we get the inode, we're the only people 242ac27a0ecSDave Kleikamp * that have access to it, and as such there are no 243ac27a0ecSDave Kleikamp * race conditions we have to worry about. The inode 244ac27a0ecSDave Kleikamp * is not on the hash-lists, and it cannot be reached 245ac27a0ecSDave Kleikamp * through the filesystem because the directory entry 246ac27a0ecSDave Kleikamp * has been deleted earlier. 247ac27a0ecSDave Kleikamp * 248ac27a0ecSDave Kleikamp * HOWEVER: we must make sure that we get no aliases, 249ac27a0ecSDave Kleikamp * which means that we have to call "clear_inode()" 250ac27a0ecSDave Kleikamp * _before_ we mark the inode not in use in the inode 251ac27a0ecSDave Kleikamp * bitmaps. Otherwise a newly created file might use 252ac27a0ecSDave Kleikamp * the same inode number (not actually the same pointer 253ac27a0ecSDave Kleikamp * though), and then we'd have two inodes sharing the 254ac27a0ecSDave Kleikamp * same inode number and space on the harddisk. 255ac27a0ecSDave Kleikamp */ 256617ba13bSMingming Cao void ext4_free_inode(handle_t *handle, struct inode *inode) 257ac27a0ecSDave Kleikamp { 258ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 259ac27a0ecSDave Kleikamp int is_directory; 260ac27a0ecSDave Kleikamp unsigned long ino; 261ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh = NULL; 262ac27a0ecSDave Kleikamp struct buffer_head *bh2; 263fd2d4291SAvantika Mathur ext4_group_t block_group; 264ac27a0ecSDave Kleikamp unsigned long bit; 265617ba13bSMingming Cao struct ext4_group_desc *gdp; 266617ba13bSMingming Cao struct ext4_super_block *es; 267617ba13bSMingming Cao struct ext4_sb_info *sbi; 2687ce9d5d1SEric Sandeen int fatal = 0, err, count, cleared; 26987a39389SDarrick J. Wong struct ext4_group_info *grp; 270ac27a0ecSDave Kleikamp 27192b97816STheodore Ts'o if (!sb) { 27292b97816STheodore Ts'o printk(KERN_ERR "EXT4-fs: %s:%d: inode on " 27392b97816STheodore Ts'o "nonexistent device\n", __func__, __LINE__); 27492b97816STheodore Ts'o return; 27592b97816STheodore Ts'o } 276ac27a0ecSDave Kleikamp if (atomic_read(&inode->i_count) > 1) { 27792b97816STheodore Ts'o ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: count=%d", 27892b97816STheodore Ts'o __func__, __LINE__, inode->i_ino, 279ac27a0ecSDave Kleikamp atomic_read(&inode->i_count)); 280ac27a0ecSDave Kleikamp return; 281ac27a0ecSDave Kleikamp } 282ac27a0ecSDave Kleikamp if (inode->i_nlink) { 28392b97816STheodore Ts'o ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: nlink=%d\n", 28492b97816STheodore Ts'o __func__, __LINE__, inode->i_ino, inode->i_nlink); 285ac27a0ecSDave Kleikamp return; 286ac27a0ecSDave Kleikamp } 287617ba13bSMingming Cao sbi = EXT4_SB(sb); 288ac27a0ecSDave Kleikamp 289ac27a0ecSDave Kleikamp ino = inode->i_ino; 290617ba13bSMingming Cao ext4_debug("freeing inode %lu\n", ino); 2919bffad1eSTheodore Ts'o trace_ext4_free_inode(inode); 292ac27a0ecSDave Kleikamp 293ac27a0ecSDave Kleikamp /* 294ac27a0ecSDave Kleikamp * Note: we must free any quota before locking the superblock, 295ac27a0ecSDave Kleikamp * as writing the quota to disk may need the lock as well. 296ac27a0ecSDave Kleikamp */ 297871a2931SChristoph Hellwig dquot_initialize(inode); 29863936ddaSChristoph Hellwig dquot_free_inode(inode); 2999f754758SChristoph Hellwig dquot_drop(inode); 300ac27a0ecSDave Kleikamp 301ac27a0ecSDave Kleikamp is_directory = S_ISDIR(inode->i_mode); 302ac27a0ecSDave Kleikamp 303ac27a0ecSDave Kleikamp /* Do this BEFORE marking the inode not in use or returning an error */ 3040930fcc1SAl Viro ext4_clear_inode(inode); 305ac27a0ecSDave Kleikamp 306617ba13bSMingming Cao es = EXT4_SB(sb)->s_es; 307617ba13bSMingming Cao if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) { 30812062dddSEric Sandeen ext4_error(sb, "reserved or nonexistent inode %lu", ino); 309ac27a0ecSDave Kleikamp goto error_return; 310ac27a0ecSDave Kleikamp } 311617ba13bSMingming Cao block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); 312617ba13bSMingming Cao bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); 313e29d1cdeSEric Sandeen bitmap_bh = ext4_read_inode_bitmap(sb, block_group); 31487a39389SDarrick J. Wong /* Don't bother if the inode bitmap is corrupt. */ 31587a39389SDarrick J. Wong grp = ext4_get_group_info(sb, block_group); 3169008a58eSDarrick J. Wong if (IS_ERR(bitmap_bh)) { 3179008a58eSDarrick J. Wong fatal = PTR_ERR(bitmap_bh); 3189008a58eSDarrick J. Wong bitmap_bh = NULL; 319ac27a0ecSDave Kleikamp goto error_return; 3209008a58eSDarrick J. Wong } 3219008a58eSDarrick J. Wong if (unlikely(EXT4_MB_GRP_IBITMAP_CORRUPT(grp))) { 3229008a58eSDarrick J. Wong fatal = -EFSCORRUPTED; 3239008a58eSDarrick J. Wong goto error_return; 3249008a58eSDarrick J. Wong } 325ac27a0ecSDave Kleikamp 326ac27a0ecSDave Kleikamp BUFFER_TRACE(bitmap_bh, "get_write_access"); 327617ba13bSMingming Cao fatal = ext4_journal_get_write_access(handle, bitmap_bh); 328ac27a0ecSDave Kleikamp if (fatal) 329ac27a0ecSDave Kleikamp goto error_return; 330ac27a0ecSDave Kleikamp 331d17413c0SDmitry Monakhov fatal = -ESRCH; 332617ba13bSMingming Cao gdp = ext4_get_group_desc(sb, block_group, &bh2); 333d17413c0SDmitry Monakhov if (gdp) { 334ac27a0ecSDave Kleikamp BUFFER_TRACE(bh2, "get_write_access"); 335617ba13bSMingming Cao fatal = ext4_journal_get_write_access(handle, bh2); 336d17413c0SDmitry Monakhov } 337955ce5f5SAneesh Kumar K.V ext4_lock_group(sb, block_group); 338597d508cSAkinobu Mita cleared = ext4_test_and_clear_bit(bit, bitmap_bh->b_data); 339d17413c0SDmitry Monakhov if (fatal || !cleared) { 340d17413c0SDmitry Monakhov ext4_unlock_group(sb, block_group); 341d17413c0SDmitry Monakhov goto out; 342d17413c0SDmitry Monakhov } 343d17413c0SDmitry Monakhov 344560671a0SAneesh Kumar K.V count = ext4_free_inodes_count(sb, gdp) + 1; 345560671a0SAneesh Kumar K.V ext4_free_inodes_set(sb, gdp, count); 346560671a0SAneesh Kumar K.V if (is_directory) { 347560671a0SAneesh Kumar K.V count = ext4_used_dirs_count(sb, gdp) - 1; 348560671a0SAneesh Kumar K.V ext4_used_dirs_set(sb, gdp, count); 349d17413c0SDmitry Monakhov percpu_counter_dec(&sbi->s_dirs_counter); 350d17413c0SDmitry Monakhov } 35141a246d1SDarrick J. Wong ext4_inode_bitmap_csum_set(sb, block_group, gdp, bitmap_bh, 35241a246d1SDarrick J. Wong EXT4_INODES_PER_GROUP(sb) / 8); 353feb0ab32SDarrick J. Wong ext4_group_desc_csum_set(sb, block_group, gdp); 354d17413c0SDmitry Monakhov ext4_unlock_group(sb, block_group); 3557d39db14STheodore Ts'o 356d17413c0SDmitry Monakhov percpu_counter_inc(&sbi->s_freeinodes_counter); 357d17413c0SDmitry Monakhov if (sbi->s_log_groups_per_flex) { 358d17413c0SDmitry Monakhov ext4_group_t f = ext4_flex_group(sbi, block_group); 359d17413c0SDmitry Monakhov 360d17413c0SDmitry Monakhov atomic_inc(&sbi->s_flex_groups[f].free_inodes); 361d17413c0SDmitry Monakhov if (is_directory) 362c4caae25SEric Sandeen atomic_dec(&sbi->s_flex_groups[f].used_dirs); 3637d39db14STheodore Ts'o } 3640390131bSFrank Mayhar BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata"); 365d17413c0SDmitry Monakhov fatal = ext4_handle_dirty_metadata(handle, NULL, bh2); 366d17413c0SDmitry Monakhov out: 367d17413c0SDmitry Monakhov if (cleared) { 3680390131bSFrank Mayhar BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata"); 3690390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh); 370ac27a0ecSDave Kleikamp if (!fatal) 371ac27a0ecSDave Kleikamp fatal = err; 37287a39389SDarrick J. Wong } else { 373d17413c0SDmitry Monakhov ext4_error(sb, "bit already cleared for inode %lu", ino); 374bf40c926SNamjae Jeon if (gdp && !EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) { 375e43bb4e6SNamjae Jeon int count; 376e43bb4e6SNamjae Jeon count = ext4_free_inodes_count(sb, gdp); 377e43bb4e6SNamjae Jeon percpu_counter_sub(&sbi->s_freeinodes_counter, 378e43bb4e6SNamjae Jeon count); 379e43bb4e6SNamjae Jeon } 38087a39389SDarrick J. Wong set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &grp->bb_state); 38187a39389SDarrick J. Wong } 382d17413c0SDmitry Monakhov 383ac27a0ecSDave Kleikamp error_return: 384ac27a0ecSDave Kleikamp brelse(bitmap_bh); 385617ba13bSMingming Cao ext4_std_error(sb, fatal); 386ac27a0ecSDave Kleikamp } 387ac27a0ecSDave Kleikamp 388a4912123STheodore Ts'o struct orlov_stats { 38990ba983fSTheodore Ts'o __u64 free_clusters; 390a4912123STheodore Ts'o __u32 free_inodes; 391a4912123STheodore Ts'o __u32 used_dirs; 392a4912123STheodore Ts'o }; 393a4912123STheodore Ts'o 394a4912123STheodore Ts'o /* 395a4912123STheodore Ts'o * Helper function for Orlov's allocator; returns critical information 396a4912123STheodore Ts'o * for a particular block group or flex_bg. If flex_size is 1, then g 397a4912123STheodore Ts'o * is a block group number; otherwise it is flex_bg number. 398a4912123STheodore Ts'o */ 3991f109d5aSTheodore Ts'o static void get_orlov_stats(struct super_block *sb, ext4_group_t g, 400a4912123STheodore Ts'o int flex_size, struct orlov_stats *stats) 401a4912123STheodore Ts'o { 402a4912123STheodore Ts'o struct ext4_group_desc *desc; 4037d39db14STheodore Ts'o struct flex_groups *flex_group = EXT4_SB(sb)->s_flex_groups; 404a4912123STheodore Ts'o 4057d39db14STheodore Ts'o if (flex_size > 1) { 4067d39db14STheodore Ts'o stats->free_inodes = atomic_read(&flex_group[g].free_inodes); 40790ba983fSTheodore Ts'o stats->free_clusters = atomic64_read(&flex_group[g].free_clusters); 4087d39db14STheodore Ts'o stats->used_dirs = atomic_read(&flex_group[g].used_dirs); 4097d39db14STheodore Ts'o return; 4107d39db14STheodore Ts'o } 4117d39db14STheodore Ts'o 4127d39db14STheodore Ts'o desc = ext4_get_group_desc(sb, g, NULL); 4137d39db14STheodore Ts'o if (desc) { 4147d39db14STheodore Ts'o stats->free_inodes = ext4_free_inodes_count(sb, desc); 415021b65bbSTheodore Ts'o stats->free_clusters = ext4_free_group_clusters(sb, desc); 4167d39db14STheodore Ts'o stats->used_dirs = ext4_used_dirs_count(sb, desc); 4177d39db14STheodore Ts'o } else { 418a4912123STheodore Ts'o stats->free_inodes = 0; 41924aaa8efSTheodore Ts'o stats->free_clusters = 0; 420a4912123STheodore Ts'o stats->used_dirs = 0; 421a4912123STheodore Ts'o } 422a4912123STheodore Ts'o } 423a4912123STheodore Ts'o 424ac27a0ecSDave Kleikamp /* 425ac27a0ecSDave Kleikamp * Orlov's allocator for directories. 426ac27a0ecSDave Kleikamp * 427ac27a0ecSDave Kleikamp * We always try to spread first-level directories. 428ac27a0ecSDave Kleikamp * 429ac27a0ecSDave Kleikamp * If there are blockgroups with both free inodes and free blocks counts 430ac27a0ecSDave Kleikamp * not worse than average we return one with smallest directory count. 431ac27a0ecSDave Kleikamp * Otherwise we simply return a random group. 432ac27a0ecSDave Kleikamp * 433ac27a0ecSDave Kleikamp * For the rest rules look so: 434ac27a0ecSDave Kleikamp * 435ac27a0ecSDave Kleikamp * It's OK to put directory into a group unless 436ac27a0ecSDave Kleikamp * it has too many directories already (max_dirs) or 437ac27a0ecSDave Kleikamp * it has too few free inodes left (min_inodes) or 438ac27a0ecSDave Kleikamp * it has too few free blocks left (min_blocks) or 4391cc8dcf5SBenoit Boissinot * Parent's group is preferred, if it doesn't satisfy these 440ac27a0ecSDave Kleikamp * conditions we search cyclically through the rest. If none 441ac27a0ecSDave Kleikamp * of the groups look good we just look for a group with more 442ac27a0ecSDave Kleikamp * free inodes than average (starting at parent's group). 443ac27a0ecSDave Kleikamp */ 444ac27a0ecSDave Kleikamp 4452aa9fc4cSAvantika Mathur static int find_group_orlov(struct super_block *sb, struct inode *parent, 446dcca3fecSAl Viro ext4_group_t *group, umode_t mode, 447f157a4aaSTheodore Ts'o const struct qstr *qstr) 448ac27a0ecSDave Kleikamp { 449fd2d4291SAvantika Mathur ext4_group_t parent_group = EXT4_I(parent)->i_block_group; 450617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 4518df9675fSTheodore Ts'o ext4_group_t real_ngroups = ext4_get_groups_count(sb); 452617ba13bSMingming Cao int inodes_per_group = EXT4_INODES_PER_GROUP(sb); 45314c83c9fSTheodore Ts'o unsigned int freei, avefreei, grp_free; 45424aaa8efSTheodore Ts'o ext4_fsblk_t freeb, avefreec; 455ac27a0ecSDave Kleikamp unsigned int ndirs; 456a4912123STheodore Ts'o int max_dirs, min_inodes; 45724aaa8efSTheodore Ts'o ext4_grpblk_t min_clusters; 4588df9675fSTheodore Ts'o ext4_group_t i, grp, g, ngroups; 459617ba13bSMingming Cao struct ext4_group_desc *desc; 460a4912123STheodore Ts'o struct orlov_stats stats; 461a4912123STheodore Ts'o int flex_size = ext4_flex_bg_size(sbi); 462f157a4aaSTheodore Ts'o struct dx_hash_info hinfo; 463a4912123STheodore Ts'o 4648df9675fSTheodore Ts'o ngroups = real_ngroups; 465a4912123STheodore Ts'o if (flex_size > 1) { 4668df9675fSTheodore Ts'o ngroups = (real_ngroups + flex_size - 1) >> 467a4912123STheodore Ts'o sbi->s_log_groups_per_flex; 468a4912123STheodore Ts'o parent_group >>= sbi->s_log_groups_per_flex; 469a4912123STheodore Ts'o } 470ac27a0ecSDave Kleikamp 471ac27a0ecSDave Kleikamp freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter); 472ac27a0ecSDave Kleikamp avefreei = freei / ngroups; 47357042651STheodore Ts'o freeb = EXT4_C2B(sbi, 47457042651STheodore Ts'o percpu_counter_read_positive(&sbi->s_freeclusters_counter)); 47524aaa8efSTheodore Ts'o avefreec = freeb; 47624aaa8efSTheodore Ts'o do_div(avefreec, ngroups); 477ac27a0ecSDave Kleikamp ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter); 478ac27a0ecSDave Kleikamp 479a4912123STheodore Ts'o if (S_ISDIR(mode) && 4802b0143b5SDavid Howells ((parent == d_inode(sb->s_root)) || 48112e9b892SDmitry Monakhov (ext4_test_inode_flag(parent, EXT4_INODE_TOPDIR)))) { 482ac27a0ecSDave Kleikamp int best_ndir = inodes_per_group; 4832aa9fc4cSAvantika Mathur int ret = -1; 484ac27a0ecSDave Kleikamp 485f157a4aaSTheodore Ts'o if (qstr) { 486f157a4aaSTheodore Ts'o hinfo.hash_version = DX_HASH_HALF_MD4; 487f157a4aaSTheodore Ts'o hinfo.seed = sbi->s_hash_seed; 488f157a4aaSTheodore Ts'o ext4fs_dirhash(qstr->name, qstr->len, &hinfo); 489f157a4aaSTheodore Ts'o grp = hinfo.hash; 490f157a4aaSTheodore Ts'o } else 491dd1f723bSTheodore Ts'o grp = prandom_u32(); 4922aa9fc4cSAvantika Mathur parent_group = (unsigned)grp % ngroups; 493ac27a0ecSDave Kleikamp for (i = 0; i < ngroups; i++) { 494a4912123STheodore Ts'o g = (parent_group + i) % ngroups; 495a4912123STheodore Ts'o get_orlov_stats(sb, g, flex_size, &stats); 496a4912123STheodore Ts'o if (!stats.free_inodes) 497ac27a0ecSDave Kleikamp continue; 498a4912123STheodore Ts'o if (stats.used_dirs >= best_ndir) 499ac27a0ecSDave Kleikamp continue; 500a4912123STheodore Ts'o if (stats.free_inodes < avefreei) 501ac27a0ecSDave Kleikamp continue; 50224aaa8efSTheodore Ts'o if (stats.free_clusters < avefreec) 503ac27a0ecSDave Kleikamp continue; 504a4912123STheodore Ts'o grp = g; 5052aa9fc4cSAvantika Mathur ret = 0; 506a4912123STheodore Ts'o best_ndir = stats.used_dirs; 507ac27a0ecSDave Kleikamp } 508a4912123STheodore Ts'o if (ret) 509a4912123STheodore Ts'o goto fallback; 510a4912123STheodore Ts'o found_flex_bg: 511a4912123STheodore Ts'o if (flex_size == 1) { 512a4912123STheodore Ts'o *group = grp; 513a4912123STheodore Ts'o return 0; 514a4912123STheodore Ts'o } 515a4912123STheodore Ts'o 516a4912123STheodore Ts'o /* 517a4912123STheodore Ts'o * We pack inodes at the beginning of the flexgroup's 518a4912123STheodore Ts'o * inode tables. Block allocation decisions will do 519a4912123STheodore Ts'o * something similar, although regular files will 520a4912123STheodore Ts'o * start at 2nd block group of the flexgroup. See 521a4912123STheodore Ts'o * ext4_ext_find_goal() and ext4_find_near(). 522a4912123STheodore Ts'o */ 523a4912123STheodore Ts'o grp *= flex_size; 524a4912123STheodore Ts'o for (i = 0; i < flex_size; i++) { 5258df9675fSTheodore Ts'o if (grp+i >= real_ngroups) 526a4912123STheodore Ts'o break; 527a4912123STheodore Ts'o desc = ext4_get_group_desc(sb, grp+i, NULL); 528a4912123STheodore Ts'o if (desc && ext4_free_inodes_count(sb, desc)) { 529a4912123STheodore Ts'o *group = grp+i; 530a4912123STheodore Ts'o return 0; 531a4912123STheodore Ts'o } 532a4912123STheodore Ts'o } 533ac27a0ecSDave Kleikamp goto fallback; 534ac27a0ecSDave Kleikamp } 535ac27a0ecSDave Kleikamp 536ac27a0ecSDave Kleikamp max_dirs = ndirs / ngroups + inodes_per_group / 16; 537a4912123STheodore Ts'o min_inodes = avefreei - inodes_per_group*flex_size / 4; 538a4912123STheodore Ts'o if (min_inodes < 1) 539a4912123STheodore Ts'o min_inodes = 1; 54024aaa8efSTheodore Ts'o min_clusters = avefreec - EXT4_CLUSTERS_PER_GROUP(sb)*flex_size / 4; 541ac27a0ecSDave Kleikamp 542a4912123STheodore Ts'o /* 543a4912123STheodore Ts'o * Start looking in the flex group where we last allocated an 544a4912123STheodore Ts'o * inode for this parent directory 545a4912123STheodore Ts'o */ 546a4912123STheodore Ts'o if (EXT4_I(parent)->i_last_alloc_group != ~0) { 547a4912123STheodore Ts'o parent_group = EXT4_I(parent)->i_last_alloc_group; 548a4912123STheodore Ts'o if (flex_size > 1) 549a4912123STheodore Ts'o parent_group >>= sbi->s_log_groups_per_flex; 550a4912123STheodore Ts'o } 551ac27a0ecSDave Kleikamp 552ac27a0ecSDave Kleikamp for (i = 0; i < ngroups; i++) { 553a4912123STheodore Ts'o grp = (parent_group + i) % ngroups; 554a4912123STheodore Ts'o get_orlov_stats(sb, grp, flex_size, &stats); 555a4912123STheodore Ts'o if (stats.used_dirs >= max_dirs) 556ac27a0ecSDave Kleikamp continue; 557a4912123STheodore Ts'o if (stats.free_inodes < min_inodes) 558ac27a0ecSDave Kleikamp continue; 55924aaa8efSTheodore Ts'o if (stats.free_clusters < min_clusters) 560ac27a0ecSDave Kleikamp continue; 561a4912123STheodore Ts'o goto found_flex_bg; 562ac27a0ecSDave Kleikamp } 563ac27a0ecSDave Kleikamp 564ac27a0ecSDave Kleikamp fallback: 5658df9675fSTheodore Ts'o ngroups = real_ngroups; 566a4912123STheodore Ts'o avefreei = freei / ngroups; 567b5451f7bSTheodore Ts'o fallback_retry: 568a4912123STheodore Ts'o parent_group = EXT4_I(parent)->i_block_group; 569ac27a0ecSDave Kleikamp for (i = 0; i < ngroups; i++) { 570a4912123STheodore Ts'o grp = (parent_group + i) % ngroups; 571a4912123STheodore Ts'o desc = ext4_get_group_desc(sb, grp, NULL); 572bb3d132aSDan Carpenter if (desc) { 57314c83c9fSTheodore Ts'o grp_free = ext4_free_inodes_count(sb, desc); 574bb3d132aSDan Carpenter if (grp_free && grp_free >= avefreei) { 575a4912123STheodore Ts'o *group = grp; 5762aa9fc4cSAvantika Mathur return 0; 577ac27a0ecSDave Kleikamp } 578a4912123STheodore Ts'o } 579bb3d132aSDan Carpenter } 580ac27a0ecSDave Kleikamp 581ac27a0ecSDave Kleikamp if (avefreei) { 582ac27a0ecSDave Kleikamp /* 583ac27a0ecSDave Kleikamp * The free-inodes counter is approximate, and for really small 584ac27a0ecSDave Kleikamp * filesystems the above test can fail to find any blockgroups 585ac27a0ecSDave Kleikamp */ 586ac27a0ecSDave Kleikamp avefreei = 0; 587b5451f7bSTheodore Ts'o goto fallback_retry; 588ac27a0ecSDave Kleikamp } 589ac27a0ecSDave Kleikamp 590ac27a0ecSDave Kleikamp return -1; 591ac27a0ecSDave Kleikamp } 592ac27a0ecSDave Kleikamp 5932aa9fc4cSAvantika Mathur static int find_group_other(struct super_block *sb, struct inode *parent, 594dcca3fecSAl Viro ext4_group_t *group, umode_t mode) 595ac27a0ecSDave Kleikamp { 596fd2d4291SAvantika Mathur ext4_group_t parent_group = EXT4_I(parent)->i_block_group; 5978df9675fSTheodore Ts'o ext4_group_t i, last, ngroups = ext4_get_groups_count(sb); 598617ba13bSMingming Cao struct ext4_group_desc *desc; 599a4912123STheodore Ts'o int flex_size = ext4_flex_bg_size(EXT4_SB(sb)); 600a4912123STheodore Ts'o 601a4912123STheodore Ts'o /* 602a4912123STheodore Ts'o * Try to place the inode is the same flex group as its 603a4912123STheodore Ts'o * parent. If we can't find space, use the Orlov algorithm to 604a4912123STheodore Ts'o * find another flex group, and store that information in the 605a4912123STheodore Ts'o * parent directory's inode information so that use that flex 606a4912123STheodore Ts'o * group for future allocations. 607a4912123STheodore Ts'o */ 608a4912123STheodore Ts'o if (flex_size > 1) { 609a4912123STheodore Ts'o int retry = 0; 610a4912123STheodore Ts'o 611a4912123STheodore Ts'o try_again: 612a4912123STheodore Ts'o parent_group &= ~(flex_size-1); 613a4912123STheodore Ts'o last = parent_group + flex_size; 614a4912123STheodore Ts'o if (last > ngroups) 615a4912123STheodore Ts'o last = ngroups; 616a4912123STheodore Ts'o for (i = parent_group; i < last; i++) { 617a4912123STheodore Ts'o desc = ext4_get_group_desc(sb, i, NULL); 618a4912123STheodore Ts'o if (desc && ext4_free_inodes_count(sb, desc)) { 619a4912123STheodore Ts'o *group = i; 620a4912123STheodore Ts'o return 0; 621a4912123STheodore Ts'o } 622a4912123STheodore Ts'o } 623a4912123STheodore Ts'o if (!retry && EXT4_I(parent)->i_last_alloc_group != ~0) { 624a4912123STheodore Ts'o retry = 1; 625a4912123STheodore Ts'o parent_group = EXT4_I(parent)->i_last_alloc_group; 626a4912123STheodore Ts'o goto try_again; 627a4912123STheodore Ts'o } 628a4912123STheodore Ts'o /* 629a4912123STheodore Ts'o * If this didn't work, use the Orlov search algorithm 630a4912123STheodore Ts'o * to find a new flex group; we pass in the mode to 631a4912123STheodore Ts'o * avoid the topdir algorithms. 632a4912123STheodore Ts'o */ 633a4912123STheodore Ts'o *group = parent_group + flex_size; 634a4912123STheodore Ts'o if (*group > ngroups) 635a4912123STheodore Ts'o *group = 0; 6367dc57615SPeter Huewe return find_group_orlov(sb, parent, group, mode, NULL); 637a4912123STheodore Ts'o } 638ac27a0ecSDave Kleikamp 639ac27a0ecSDave Kleikamp /* 640ac27a0ecSDave Kleikamp * Try to place the inode in its parent directory 641ac27a0ecSDave Kleikamp */ 6422aa9fc4cSAvantika Mathur *group = parent_group; 6432aa9fc4cSAvantika Mathur desc = ext4_get_group_desc(sb, *group, NULL); 644560671a0SAneesh Kumar K.V if (desc && ext4_free_inodes_count(sb, desc) && 645021b65bbSTheodore Ts'o ext4_free_group_clusters(sb, desc)) 6462aa9fc4cSAvantika Mathur return 0; 647ac27a0ecSDave Kleikamp 648ac27a0ecSDave Kleikamp /* 649ac27a0ecSDave Kleikamp * We're going to place this inode in a different blockgroup from its 650ac27a0ecSDave Kleikamp * parent. We want to cause files in a common directory to all land in 651ac27a0ecSDave Kleikamp * the same blockgroup. But we want files which are in a different 652ac27a0ecSDave Kleikamp * directory which shares a blockgroup with our parent to land in a 653ac27a0ecSDave Kleikamp * different blockgroup. 654ac27a0ecSDave Kleikamp * 655ac27a0ecSDave Kleikamp * So add our directory's i_ino into the starting point for the hash. 656ac27a0ecSDave Kleikamp */ 6572aa9fc4cSAvantika Mathur *group = (*group + parent->i_ino) % ngroups; 658ac27a0ecSDave Kleikamp 659ac27a0ecSDave Kleikamp /* 660ac27a0ecSDave Kleikamp * Use a quadratic hash to find a group with a free inode and some free 661ac27a0ecSDave Kleikamp * blocks. 662ac27a0ecSDave Kleikamp */ 663ac27a0ecSDave Kleikamp for (i = 1; i < ngroups; i <<= 1) { 6642aa9fc4cSAvantika Mathur *group += i; 6652aa9fc4cSAvantika Mathur if (*group >= ngroups) 6662aa9fc4cSAvantika Mathur *group -= ngroups; 6672aa9fc4cSAvantika Mathur desc = ext4_get_group_desc(sb, *group, NULL); 668560671a0SAneesh Kumar K.V if (desc && ext4_free_inodes_count(sb, desc) && 669021b65bbSTheodore Ts'o ext4_free_group_clusters(sb, desc)) 6702aa9fc4cSAvantika Mathur return 0; 671ac27a0ecSDave Kleikamp } 672ac27a0ecSDave Kleikamp 673ac27a0ecSDave Kleikamp /* 674ac27a0ecSDave Kleikamp * That failed: try linear search for a free inode, even if that group 675ac27a0ecSDave Kleikamp * has no free blocks. 676ac27a0ecSDave Kleikamp */ 6772aa9fc4cSAvantika Mathur *group = parent_group; 678ac27a0ecSDave Kleikamp for (i = 0; i < ngroups; i++) { 6792aa9fc4cSAvantika Mathur if (++*group >= ngroups) 6802aa9fc4cSAvantika Mathur *group = 0; 6812aa9fc4cSAvantika Mathur desc = ext4_get_group_desc(sb, *group, NULL); 682560671a0SAneesh Kumar K.V if (desc && ext4_free_inodes_count(sb, desc)) 6832aa9fc4cSAvantika Mathur return 0; 684ac27a0ecSDave Kleikamp } 685ac27a0ecSDave Kleikamp 686ac27a0ecSDave Kleikamp return -1; 687ac27a0ecSDave Kleikamp } 688ac27a0ecSDave Kleikamp 689ac27a0ecSDave Kleikamp /* 69019883bd9STheodore Ts'o * In no journal mode, if an inode has recently been deleted, we want 69119883bd9STheodore Ts'o * to avoid reusing it until we're reasonably sure the inode table 69219883bd9STheodore Ts'o * block has been written back to disk. (Yes, these values are 69319883bd9STheodore Ts'o * somewhat arbitrary...) 69419883bd9STheodore Ts'o */ 69519883bd9STheodore Ts'o #define RECENTCY_MIN 5 696b5f51573SAndreas Dilger #define RECENTCY_DIRTY 300 69719883bd9STheodore Ts'o 69819883bd9STheodore Ts'o static int recently_deleted(struct super_block *sb, ext4_group_t group, int ino) 69919883bd9STheodore Ts'o { 70019883bd9STheodore Ts'o struct ext4_group_desc *gdp; 70119883bd9STheodore Ts'o struct ext4_inode *raw_inode; 70219883bd9STheodore Ts'o struct buffer_head *bh; 70319883bd9STheodore Ts'o int inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 704b5f51573SAndreas Dilger int offset, ret = 0; 705b5f51573SAndreas Dilger int recentcy = RECENTCY_MIN; 706b5f51573SAndreas Dilger u32 dtime, now; 70719883bd9STheodore Ts'o 70819883bd9STheodore Ts'o gdp = ext4_get_group_desc(sb, group, NULL); 70919883bd9STheodore Ts'o if (unlikely(!gdp)) 71019883bd9STheodore Ts'o return 0; 71119883bd9STheodore Ts'o 7124f9d956dSJan Kara bh = sb_find_get_block(sb, ext4_inode_table(sb, gdp) + 71319883bd9STheodore Ts'o (ino / inodes_per_block)); 7144f9d956dSJan Kara if (!bh || !buffer_uptodate(bh)) 71519883bd9STheodore Ts'o /* 71619883bd9STheodore Ts'o * If the block is not in the buffer cache, then it 71719883bd9STheodore Ts'o * must have been written out. 71819883bd9STheodore Ts'o */ 71919883bd9STheodore Ts'o goto out; 72019883bd9STheodore Ts'o 72119883bd9STheodore Ts'o offset = (ino % inodes_per_block) * EXT4_INODE_SIZE(sb); 72219883bd9STheodore Ts'o raw_inode = (struct ext4_inode *) (bh->b_data + offset); 723b5f51573SAndreas Dilger 724b5f51573SAndreas Dilger /* i_dtime is only 32 bits on disk, but we only care about relative 725b5f51573SAndreas Dilger * times in the range of a few minutes (i.e. long enough to sync a 726b5f51573SAndreas Dilger * recently-deleted inode to disk), so using the low 32 bits of the 727b5f51573SAndreas Dilger * clock (a 68 year range) is enough, see time_before32() */ 72819883bd9STheodore Ts'o dtime = le32_to_cpu(raw_inode->i_dtime); 729b5f51573SAndreas Dilger now = ktime_get_real_seconds(); 73019883bd9STheodore Ts'o if (buffer_dirty(bh)) 73119883bd9STheodore Ts'o recentcy += RECENTCY_DIRTY; 73219883bd9STheodore Ts'o 733b5f51573SAndreas Dilger if (dtime && time_before32(dtime, now) && 734b5f51573SAndreas Dilger time_before32(now, dtime + recentcy)) 73519883bd9STheodore Ts'o ret = 1; 73619883bd9STheodore Ts'o out: 73719883bd9STheodore Ts'o brelse(bh); 73819883bd9STheodore Ts'o return ret; 73919883bd9STheodore Ts'o } 74019883bd9STheodore Ts'o 741901ed070SWang Shilong static int find_inode_bit(struct super_block *sb, ext4_group_t group, 742901ed070SWang Shilong struct buffer_head *bitmap, unsigned long *ino) 743901ed070SWang Shilong { 744901ed070SWang Shilong next: 745901ed070SWang Shilong *ino = ext4_find_next_zero_bit((unsigned long *) 746901ed070SWang Shilong bitmap->b_data, 747901ed070SWang Shilong EXT4_INODES_PER_GROUP(sb), *ino); 748901ed070SWang Shilong if (*ino >= EXT4_INODES_PER_GROUP(sb)) 749901ed070SWang Shilong return 0; 750901ed070SWang Shilong 751901ed070SWang Shilong if ((EXT4_SB(sb)->s_journal == NULL) && 752901ed070SWang Shilong recently_deleted(sb, group, *ino)) { 753901ed070SWang Shilong *ino = *ino + 1; 754901ed070SWang Shilong if (*ino < EXT4_INODES_PER_GROUP(sb)) 755901ed070SWang Shilong goto next; 756901ed070SWang Shilong return 0; 757901ed070SWang Shilong } 758901ed070SWang Shilong 759901ed070SWang Shilong return 1; 760901ed070SWang Shilong } 761901ed070SWang Shilong 76219883bd9STheodore Ts'o /* 763ac27a0ecSDave Kleikamp * There are two policies for allocating an inode. If the new inode is 764ac27a0ecSDave Kleikamp * a directory, then a forward search is made for a block group with both 765ac27a0ecSDave Kleikamp * free space and a low directory-to-inode ratio; if that fails, then of 766ac27a0ecSDave Kleikamp * the groups with above-average free space, that group with the fewest 767ac27a0ecSDave Kleikamp * directories already is chosen. 768ac27a0ecSDave Kleikamp * 769ac27a0ecSDave Kleikamp * For other inodes, search forward from the parent directory's block 770ac27a0ecSDave Kleikamp * group to find a free inode. 771ac27a0ecSDave Kleikamp */ 7721139575aSTheodore Ts'o struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir, 7731139575aSTheodore Ts'o umode_t mode, const struct qstr *qstr, 7741b917ed8STahsin Erdogan __u32 goal, uid_t *owner, __u32 i_flags, 7751b917ed8STahsin Erdogan int handle_type, unsigned int line_no, 7761b917ed8STahsin Erdogan int nblocks) 777ac27a0ecSDave Kleikamp { 778ac27a0ecSDave Kleikamp struct super_block *sb; 7793300bedaSAneesh Kumar K.V struct buffer_head *inode_bitmap_bh = NULL; 7803300bedaSAneesh Kumar K.V struct buffer_head *group_desc_bh; 7818df9675fSTheodore Ts'o ext4_group_t ngroups, group = 0; 782ac27a0ecSDave Kleikamp unsigned long ino = 0; 783ac27a0ecSDave Kleikamp struct inode *inode; 784617ba13bSMingming Cao struct ext4_group_desc *gdp = NULL; 785617ba13bSMingming Cao struct ext4_inode_info *ei; 786617ba13bSMingming Cao struct ext4_sb_info *sbi; 787a7cdadeeSJan Kara int ret2, err; 788ac27a0ecSDave Kleikamp struct inode *ret; 7892aa9fc4cSAvantika Mathur ext4_group_t i; 790772cb7c8SJose R. Santos ext4_group_t flex_group; 79187a39389SDarrick J. Wong struct ext4_group_info *grp; 792e709e9dfSTheodore Ts'o int encrypt = 0; 793ac27a0ecSDave Kleikamp 794ac27a0ecSDave Kleikamp /* Cannot create files in a deleted directory */ 795ac27a0ecSDave Kleikamp if (!dir || !dir->i_nlink) 796ac27a0ecSDave Kleikamp return ERR_PTR(-EPERM); 797ac27a0ecSDave Kleikamp 798af65207cSTahsin Erdogan sb = dir->i_sb; 799af65207cSTahsin Erdogan sbi = EXT4_SB(sb); 800af65207cSTahsin Erdogan 801af65207cSTahsin Erdogan if (unlikely(ext4_forced_shutdown(sbi))) 8020db1ff22STheodore Ts'o return ERR_PTR(-EIO); 8030db1ff22STheodore Ts'o 804af65207cSTahsin Erdogan if ((ext4_encrypted_inode(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) && 805ad47f953STahsin Erdogan (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) && 806ad47f953STahsin Erdogan !(i_flags & EXT4_EA_INODE_FL)) { 807a7550b30SJaegeuk Kim err = fscrypt_get_encryption_info(dir); 808e709e9dfSTheodore Ts'o if (err) 809e709e9dfSTheodore Ts'o return ERR_PTR(err); 810a7550b30SJaegeuk Kim if (!fscrypt_has_encryption_key(dir)) 81154475f53SEric Biggers return ERR_PTR(-ENOKEY); 812e709e9dfSTheodore Ts'o encrypt = 1; 813e709e9dfSTheodore Ts'o } 814e709e9dfSTheodore Ts'o 815af65207cSTahsin Erdogan if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) { 816af65207cSTahsin Erdogan #ifdef CONFIG_EXT4_FS_POSIX_ACL 817af65207cSTahsin Erdogan struct posix_acl *p = get_acl(dir, ACL_TYPE_DEFAULT); 818af65207cSTahsin Erdogan 819af65207cSTahsin Erdogan if (p) { 820af65207cSTahsin Erdogan int acl_size = p->a_count * sizeof(ext4_acl_entry); 821af65207cSTahsin Erdogan 822af65207cSTahsin Erdogan nblocks += (S_ISDIR(mode) ? 2 : 1) * 823af65207cSTahsin Erdogan __ext4_xattr_set_credits(sb, NULL /* inode */, 824af65207cSTahsin Erdogan NULL /* block_bh */, acl_size, 825af65207cSTahsin Erdogan true /* is_create */); 826af65207cSTahsin Erdogan posix_acl_release(p); 827af65207cSTahsin Erdogan } 828af65207cSTahsin Erdogan #endif 829af65207cSTahsin Erdogan 830af65207cSTahsin Erdogan #ifdef CONFIG_SECURITY 831af65207cSTahsin Erdogan { 832af65207cSTahsin Erdogan int num_security_xattrs = 1; 833af65207cSTahsin Erdogan 834af65207cSTahsin Erdogan #ifdef CONFIG_INTEGRITY 835af65207cSTahsin Erdogan num_security_xattrs++; 836af65207cSTahsin Erdogan #endif 837af65207cSTahsin Erdogan /* 838af65207cSTahsin Erdogan * We assume that security xattrs are never 839af65207cSTahsin Erdogan * more than 1k. In practice they are under 840af65207cSTahsin Erdogan * 128 bytes. 841af65207cSTahsin Erdogan */ 842af65207cSTahsin Erdogan nblocks += num_security_xattrs * 843af65207cSTahsin Erdogan __ext4_xattr_set_credits(sb, NULL /* inode */, 844af65207cSTahsin Erdogan NULL /* block_bh */, 1024, 845af65207cSTahsin Erdogan true /* is_create */); 846af65207cSTahsin Erdogan } 847af65207cSTahsin Erdogan #endif 848af65207cSTahsin Erdogan if (encrypt) 849af65207cSTahsin Erdogan nblocks += __ext4_xattr_set_credits(sb, 850af65207cSTahsin Erdogan NULL /* inode */, NULL /* block_bh */, 851af65207cSTahsin Erdogan FSCRYPT_SET_CONTEXT_MAX_SIZE, 852af65207cSTahsin Erdogan true /* is_create */); 853af65207cSTahsin Erdogan } 854af65207cSTahsin Erdogan 8558df9675fSTheodore Ts'o ngroups = ext4_get_groups_count(sb); 8569bffad1eSTheodore Ts'o trace_ext4_request_inode(dir, mode); 857ac27a0ecSDave Kleikamp inode = new_inode(sb); 858ac27a0ecSDave Kleikamp if (!inode) 859ac27a0ecSDave Kleikamp return ERR_PTR(-ENOMEM); 860617ba13bSMingming Cao ei = EXT4_I(inode); 861772cb7c8SJose R. Santos 862eb9cc7e1SJan Kara /* 863b8a07463SAdam Buchbinder * Initialize owners and quota early so that we don't have to account 864eb9cc7e1SJan Kara * for quota initialization worst case in standard inode creating 865eb9cc7e1SJan Kara * transaction 866eb9cc7e1SJan Kara */ 867eb9cc7e1SJan Kara if (owner) { 868eb9cc7e1SJan Kara inode->i_mode = mode; 869eb9cc7e1SJan Kara i_uid_write(inode, owner[0]); 870eb9cc7e1SJan Kara i_gid_write(inode, owner[1]); 871eb9cc7e1SJan Kara } else if (test_opt(sb, GRPID)) { 872eb9cc7e1SJan Kara inode->i_mode = mode; 873eb9cc7e1SJan Kara inode->i_uid = current_fsuid(); 874eb9cc7e1SJan Kara inode->i_gid = dir->i_gid; 875eb9cc7e1SJan Kara } else 876eb9cc7e1SJan Kara inode_init_owner(inode, dir, mode); 877040cb378SLi Xi 8780b7b7779SKaho Ng if (ext4_has_feature_project(sb) && 879040cb378SLi Xi ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT)) 880040cb378SLi Xi ei->i_projid = EXT4_I(dir)->i_projid; 881040cb378SLi Xi else 882040cb378SLi Xi ei->i_projid = make_kprojid(&init_user_ns, EXT4_DEF_PROJID); 883040cb378SLi Xi 884a7cdadeeSJan Kara err = dquot_initialize(inode); 885a7cdadeeSJan Kara if (err) 886a7cdadeeSJan Kara goto out; 887eb9cc7e1SJan Kara 88811013911SAndreas Dilger if (!goal) 88911013911SAndreas Dilger goal = sbi->s_inode_goal; 89011013911SAndreas Dilger 891e6462869SJohann Lombardi if (goal && goal <= le32_to_cpu(sbi->s_es->s_inodes_count)) { 89211013911SAndreas Dilger group = (goal - 1) / EXT4_INODES_PER_GROUP(sb); 89311013911SAndreas Dilger ino = (goal - 1) % EXT4_INODES_PER_GROUP(sb); 89411013911SAndreas Dilger ret2 = 0; 89511013911SAndreas Dilger goto got_group; 89611013911SAndreas Dilger } 89711013911SAndreas Dilger 8984113c4caSLukas Czerner if (S_ISDIR(mode)) 899f157a4aaSTheodore Ts'o ret2 = find_group_orlov(sb, dir, &group, mode, qstr); 9004113c4caSLukas Czerner else 901a4912123STheodore Ts'o ret2 = find_group_other(sb, dir, &group, mode); 902ac27a0ecSDave Kleikamp 903772cb7c8SJose R. Santos got_group: 904a4912123STheodore Ts'o EXT4_I(dir)->i_last_alloc_group = group; 905ac27a0ecSDave Kleikamp err = -ENOSPC; 9062aa9fc4cSAvantika Mathur if (ret2 == -1) 907ac27a0ecSDave Kleikamp goto out; 908ac27a0ecSDave Kleikamp 909119c0d44STheodore Ts'o /* 910119c0d44STheodore Ts'o * Normally we will only go through one pass of this loop, 911119c0d44STheodore Ts'o * unless we get unlucky and it turns out the group we selected 912119c0d44STheodore Ts'o * had its last inode grabbed by someone else. 913119c0d44STheodore Ts'o */ 91411013911SAndreas Dilger for (i = 0; i < ngroups; i++, ino = 0) { 915ac27a0ecSDave Kleikamp err = -EIO; 916ac27a0ecSDave Kleikamp 9173300bedaSAneesh Kumar K.V gdp = ext4_get_group_desc(sb, group, &group_desc_bh); 918ac27a0ecSDave Kleikamp if (!gdp) 919eb9cc7e1SJan Kara goto out; 920ac27a0ecSDave Kleikamp 921f2a09af6SYongqiang Yang /* 922f2a09af6SYongqiang Yang * Check free inodes count before loading bitmap. 923f2a09af6SYongqiang Yang */ 9242fe435d8SWang Shilong if (ext4_free_inodes_count(sb, gdp) == 0) 9252fe435d8SWang Shilong goto next_group; 926f2a09af6SYongqiang Yang 92787a39389SDarrick J. Wong grp = ext4_get_group_info(sb, group); 92887a39389SDarrick J. Wong /* Skip groups with already-known suspicious inode tables */ 9292fe435d8SWang Shilong if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) 9302fe435d8SWang Shilong goto next_group; 93187a39389SDarrick J. Wong 9323300bedaSAneesh Kumar K.V brelse(inode_bitmap_bh); 9333300bedaSAneesh Kumar K.V inode_bitmap_bh = ext4_read_inode_bitmap(sb, group); 93487a39389SDarrick J. Wong /* Skip groups with suspicious inode tables */ 9359008a58eSDarrick J. Wong if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp) || 9369008a58eSDarrick J. Wong IS_ERR(inode_bitmap_bh)) { 9379008a58eSDarrick J. Wong inode_bitmap_bh = NULL; 9382fe435d8SWang Shilong goto next_group; 93987a39389SDarrick J. Wong } 940ac27a0ecSDave Kleikamp 941ac27a0ecSDave Kleikamp repeat_in_this_group: 942901ed070SWang Shilong ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino); 943901ed070SWang Shilong if (!ret2) 944a34eb503STheodore Ts'o goto next_group; 945901ed070SWang Shilong 946119c0d44STheodore Ts'o if (group == 0 && (ino + 1) < EXT4_FIRST_INO(sb)) { 947119c0d44STheodore Ts'o ext4_error(sb, "reserved inode found cleared - " 948119c0d44STheodore Ts'o "inode=%lu", ino + 1); 9492fe435d8SWang Shilong goto next_group; 950119c0d44STheodore Ts'o } 951901ed070SWang Shilong 9521139575aSTheodore Ts'o if (!handle) { 9531139575aSTheodore Ts'o BUG_ON(nblocks <= 0); 9541139575aSTheodore Ts'o handle = __ext4_journal_start_sb(dir->i_sb, line_no, 9555fe2fe89SJan Kara handle_type, nblocks, 9565fe2fe89SJan Kara 0); 9571139575aSTheodore Ts'o if (IS_ERR(handle)) { 9581139575aSTheodore Ts'o err = PTR_ERR(handle); 959eb9cc7e1SJan Kara ext4_std_error(sb, err); 960eb9cc7e1SJan Kara goto out; 9611139575aSTheodore Ts'o } 9621139575aSTheodore Ts'o } 963ffb5387eSEric Sandeen BUFFER_TRACE(inode_bitmap_bh, "get_write_access"); 964ffb5387eSEric Sandeen err = ext4_journal_get_write_access(handle, inode_bitmap_bh); 965eb9cc7e1SJan Kara if (err) { 966eb9cc7e1SJan Kara ext4_std_error(sb, err); 967eb9cc7e1SJan Kara goto out; 968eb9cc7e1SJan Kara } 969119c0d44STheodore Ts'o ext4_lock_group(sb, group); 970119c0d44STheodore Ts'o ret2 = ext4_test_and_set_bit(ino, inode_bitmap_bh->b_data); 971901ed070SWang Shilong if (ret2) { 972901ed070SWang Shilong /* Someone already took the bit. Repeat the search 973901ed070SWang Shilong * with lock held. 974901ed070SWang Shilong */ 975901ed070SWang Shilong ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino); 976901ed070SWang Shilong if (ret2) { 977901ed070SWang Shilong ext4_set_bit(ino, inode_bitmap_bh->b_data); 978901ed070SWang Shilong ret2 = 0; 979901ed070SWang Shilong } else { 980901ed070SWang Shilong ret2 = 1; /* we didn't grab the inode */ 981901ed070SWang Shilong } 982901ed070SWang Shilong } 983119c0d44STheodore Ts'o ext4_unlock_group(sb, group); 984119c0d44STheodore Ts'o ino++; /* the inode bitmap is zero-based */ 985119c0d44STheodore Ts'o if (!ret2) 986119c0d44STheodore Ts'o goto got; /* we grabbed the inode! */ 987901ed070SWang Shilong 988119c0d44STheodore Ts'o if (ino < EXT4_INODES_PER_GROUP(sb)) 989119c0d44STheodore Ts'o goto repeat_in_this_group; 990a34eb503STheodore Ts'o next_group: 991a34eb503STheodore Ts'o if (++group == ngroups) 992a34eb503STheodore Ts'o group = 0; 993ac27a0ecSDave Kleikamp } 994ac27a0ecSDave Kleikamp err = -ENOSPC; 995ac27a0ecSDave Kleikamp goto out; 996ac27a0ecSDave Kleikamp 997ac27a0ecSDave Kleikamp got: 998ffb5387eSEric Sandeen BUFFER_TRACE(inode_bitmap_bh, "call ext4_handle_dirty_metadata"); 999ffb5387eSEric Sandeen err = ext4_handle_dirty_metadata(handle, NULL, inode_bitmap_bh); 1000eb9cc7e1SJan Kara if (err) { 1001eb9cc7e1SJan Kara ext4_std_error(sb, err); 1002eb9cc7e1SJan Kara goto out; 1003eb9cc7e1SJan Kara } 1004ffb5387eSEric Sandeen 100561c219f5STheodore Ts'o BUFFER_TRACE(group_desc_bh, "get_write_access"); 100661c219f5STheodore Ts'o err = ext4_journal_get_write_access(handle, group_desc_bh); 100761c219f5STheodore Ts'o if (err) { 100861c219f5STheodore Ts'o ext4_std_error(sb, err); 100961c219f5STheodore Ts'o goto out; 101061c219f5STheodore Ts'o } 101161c219f5STheodore Ts'o 1012717d50e4SAndreas Dilger /* We may have to initialize the block bitmap if it isn't already */ 1013feb0ab32SDarrick J. Wong if (ext4_has_group_desc_csum(sb) && 1014717d50e4SAndreas Dilger gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { 10153300bedaSAneesh Kumar K.V struct buffer_head *block_bitmap_bh; 1016717d50e4SAndreas Dilger 10173300bedaSAneesh Kumar K.V block_bitmap_bh = ext4_read_block_bitmap(sb, group); 10189008a58eSDarrick J. Wong if (IS_ERR(block_bitmap_bh)) { 10199008a58eSDarrick J. Wong err = PTR_ERR(block_bitmap_bh); 1020599a9b77SJan Kara goto out; 1021599a9b77SJan Kara } 10223300bedaSAneesh Kumar K.V BUFFER_TRACE(block_bitmap_bh, "get block bitmap access"); 10233300bedaSAneesh Kumar K.V err = ext4_journal_get_write_access(handle, block_bitmap_bh); 1024717d50e4SAndreas Dilger if (err) { 10253300bedaSAneesh Kumar K.V brelse(block_bitmap_bh); 1026eb9cc7e1SJan Kara ext4_std_error(sb, err); 1027eb9cc7e1SJan Kara goto out; 1028717d50e4SAndreas Dilger } 1029717d50e4SAndreas Dilger 1030fd034a84STheodore Ts'o BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap"); 1031fd034a84STheodore Ts'o err = ext4_handle_dirty_metadata(handle, NULL, block_bitmap_bh); 1032fd034a84STheodore Ts'o 1033717d50e4SAndreas Dilger /* recheck and clear flag under lock if we still need to */ 1034fd034a84STheodore Ts'o ext4_lock_group(sb, group); 1035717d50e4SAndreas Dilger if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { 10363300bedaSAneesh Kumar K.V gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT); 1037021b65bbSTheodore Ts'o ext4_free_group_clusters_set(sb, gdp, 1038cff1dfd7STheodore Ts'o ext4_free_clusters_after_init(sb, group, gdp)); 1039fa77dcfaSDarrick J. Wong ext4_block_bitmap_csum_set(sb, group, gdp, 104079f1ba49STao Ma block_bitmap_bh); 1041feb0ab32SDarrick J. Wong ext4_group_desc_csum_set(sb, group, gdp); 1042717d50e4SAndreas Dilger } 1043955ce5f5SAneesh Kumar K.V ext4_unlock_group(sb, group); 1044aeb1e5d6STheodore Ts'o brelse(block_bitmap_bh); 1045717d50e4SAndreas Dilger 1046eb9cc7e1SJan Kara if (err) { 1047eb9cc7e1SJan Kara ext4_std_error(sb, err); 1048eb9cc7e1SJan Kara goto out; 1049eb9cc7e1SJan Kara } 1050717d50e4SAndreas Dilger } 1051119c0d44STheodore Ts'o 1052119c0d44STheodore Ts'o /* Update the relevant bg descriptor fields */ 105341a246d1SDarrick J. Wong if (ext4_has_group_desc_csum(sb)) { 1054119c0d44STheodore Ts'o int free; 1055119c0d44STheodore Ts'o struct ext4_group_info *grp = ext4_get_group_info(sb, group); 1056119c0d44STheodore Ts'o 1057119c0d44STheodore Ts'o down_read(&grp->alloc_sem); /* protect vs itable lazyinit */ 1058119c0d44STheodore Ts'o ext4_lock_group(sb, group); /* while we modify the bg desc */ 1059119c0d44STheodore Ts'o free = EXT4_INODES_PER_GROUP(sb) - 1060119c0d44STheodore Ts'o ext4_itable_unused_count(sb, gdp); 1061119c0d44STheodore Ts'o if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) { 1062119c0d44STheodore Ts'o gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT); 1063119c0d44STheodore Ts'o free = 0; 1064119c0d44STheodore Ts'o } 1065119c0d44STheodore Ts'o /* 1066119c0d44STheodore Ts'o * Check the relative inode number against the last used 1067119c0d44STheodore Ts'o * relative inode number in this group. if it is greater 1068119c0d44STheodore Ts'o * we need to update the bg_itable_unused count 1069119c0d44STheodore Ts'o */ 1070119c0d44STheodore Ts'o if (ino > free) 1071119c0d44STheodore Ts'o ext4_itable_unused_set(sb, gdp, 1072119c0d44STheodore Ts'o (EXT4_INODES_PER_GROUP(sb) - ino)); 1073119c0d44STheodore Ts'o up_read(&grp->alloc_sem); 10746f2e9f0eSTao Ma } else { 10756f2e9f0eSTao Ma ext4_lock_group(sb, group); 1076119c0d44STheodore Ts'o } 10776f2e9f0eSTao Ma 1078119c0d44STheodore Ts'o ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1); 1079119c0d44STheodore Ts'o if (S_ISDIR(mode)) { 1080119c0d44STheodore Ts'o ext4_used_dirs_set(sb, gdp, ext4_used_dirs_count(sb, gdp) + 1); 1081119c0d44STheodore Ts'o if (sbi->s_log_groups_per_flex) { 1082119c0d44STheodore Ts'o ext4_group_t f = ext4_flex_group(sbi, group); 1083119c0d44STheodore Ts'o 1084119c0d44STheodore Ts'o atomic_inc(&sbi->s_flex_groups[f].used_dirs); 1085119c0d44STheodore Ts'o } 1086119c0d44STheodore Ts'o } 108741a246d1SDarrick J. Wong if (ext4_has_group_desc_csum(sb)) { 108841a246d1SDarrick J. Wong ext4_inode_bitmap_csum_set(sb, group, gdp, inode_bitmap_bh, 108941a246d1SDarrick J. Wong EXT4_INODES_PER_GROUP(sb) / 8); 1090feb0ab32SDarrick J. Wong ext4_group_desc_csum_set(sb, group, gdp); 1091119c0d44STheodore Ts'o } 10926f2e9f0eSTao Ma ext4_unlock_group(sb, group); 1093119c0d44STheodore Ts'o 10943300bedaSAneesh Kumar K.V BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata"); 10953300bedaSAneesh Kumar K.V err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh); 1096eb9cc7e1SJan Kara if (err) { 1097eb9cc7e1SJan Kara ext4_std_error(sb, err); 1098eb9cc7e1SJan Kara goto out; 1099eb9cc7e1SJan Kara } 1100ac27a0ecSDave Kleikamp 1101ac27a0ecSDave Kleikamp percpu_counter_dec(&sbi->s_freeinodes_counter); 1102ac27a0ecSDave Kleikamp if (S_ISDIR(mode)) 1103ac27a0ecSDave Kleikamp percpu_counter_inc(&sbi->s_dirs_counter); 1104ac27a0ecSDave Kleikamp 1105772cb7c8SJose R. Santos if (sbi->s_log_groups_per_flex) { 1106772cb7c8SJose R. Santos flex_group = ext4_flex_group(sbi, group); 11079f24e420STheodore Ts'o atomic_dec(&sbi->s_flex_groups[flex_group].free_inodes); 1108772cb7c8SJose R. Santos } 1109ac27a0ecSDave Kleikamp 1110717d50e4SAndreas Dilger inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb); 1111ac27a0ecSDave Kleikamp /* This is the optimal IO size (for stat), not the fs block size */ 1112ac27a0ecSDave Kleikamp inode->i_blocks = 0; 1113ef7f3835SKalpak Shah inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime = 1114eeca7ea1SDeepa Dinamani current_time(inode); 1115ac27a0ecSDave Kleikamp 1116ac27a0ecSDave Kleikamp memset(ei->i_data, 0, sizeof(ei->i_data)); 1117ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 1118ac27a0ecSDave Kleikamp ei->i_disksize = 0; 1119ac27a0ecSDave Kleikamp 11204af83508SEryu Guan /* Don't inherit extent flag from directory, amongst others. */ 11212dc6b0d4SDuane Griffin ei->i_flags = 11222dc6b0d4SDuane Griffin ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED); 11231b917ed8STahsin Erdogan ei->i_flags |= i_flags; 1124ac27a0ecSDave Kleikamp ei->i_file_acl = 0; 1125ac27a0ecSDave Kleikamp ei->i_dtime = 0; 1126ac27a0ecSDave Kleikamp ei->i_block_group = group; 1127a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 1128ac27a0ecSDave Kleikamp 1129617ba13bSMingming Cao ext4_set_inode_flags(inode); 1130ac27a0ecSDave Kleikamp if (IS_DIRSYNC(inode)) 11310390131bSFrank Mayhar ext4_handle_sync(handle); 11326b38e842SAl Viro if (insert_inode_locked(inode) < 0) { 1133acd6ad83SJan Kara /* 1134acd6ad83SJan Kara * Likely a bitmap corruption causing inode to be allocated 1135acd6ad83SJan Kara * twice. 1136acd6ad83SJan Kara */ 1137acd6ad83SJan Kara err = -EIO; 1138eb9cc7e1SJan Kara ext4_error(sb, "failed to insert inode %lu: doubly allocated?", 1139eb9cc7e1SJan Kara inode->i_ino); 1140eb9cc7e1SJan Kara goto out; 11416b38e842SAl Viro } 1142ac27a0ecSDave Kleikamp spin_lock(&sbi->s_next_gen_lock); 1143ac27a0ecSDave Kleikamp inode->i_generation = sbi->s_next_generation++; 1144ac27a0ecSDave Kleikamp spin_unlock(&sbi->s_next_gen_lock); 1145ac27a0ecSDave Kleikamp 1146814525f4SDarrick J. Wong /* Precompute checksum seed for inode metadata */ 11479aa5d32bSDmitry Monakhov if (ext4_has_metadata_csum(sb)) { 1148814525f4SDarrick J. Wong __u32 csum; 1149814525f4SDarrick J. Wong __le32 inum = cpu_to_le32(inode->i_ino); 1150814525f4SDarrick J. Wong __le32 gen = cpu_to_le32(inode->i_generation); 1151814525f4SDarrick J. Wong csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, 1152814525f4SDarrick J. Wong sizeof(inum)); 1153814525f4SDarrick J. Wong ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, 1154814525f4SDarrick J. Wong sizeof(gen)); 1155814525f4SDarrick J. Wong } 1156814525f4SDarrick J. Wong 1157353eb83cSTheodore Ts'o ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ 115819f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_NEW); 1159ef7f3835SKalpak Shah 1160ef7f3835SKalpak Shah ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize; 1161f08225d1STao Ma ei->i_inline_off = 0; 1162e2b911c5SDarrick J. Wong if (ext4_has_feature_inline_data(sb)) 1163f08225d1STao Ma ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); 1164ac27a0ecSDave Kleikamp ret = inode; 116563936ddaSChristoph Hellwig err = dquot_alloc_inode(inode); 116663936ddaSChristoph Hellwig if (err) 1167ac27a0ecSDave Kleikamp goto fail_drop; 1168ac27a0ecSDave Kleikamp 1169aa1dca3bSEric Biggers /* 1170aa1dca3bSEric Biggers * Since the encryption xattr will always be unique, create it first so 1171aa1dca3bSEric Biggers * that it's less likely to end up in an external xattr block and 1172aa1dca3bSEric Biggers * prevent its deduplication. 1173aa1dca3bSEric Biggers */ 1174aa1dca3bSEric Biggers if (encrypt) { 1175aa1dca3bSEric Biggers err = fscrypt_inherit_context(dir, inode, handle, true); 1176aa1dca3bSEric Biggers if (err) 1177aa1dca3bSEric Biggers goto fail_free_drop; 1178aa1dca3bSEric Biggers } 1179aa1dca3bSEric Biggers 11801b917ed8STahsin Erdogan if (!(ei->i_flags & EXT4_EA_INODE_FL)) { 1181617ba13bSMingming Cao err = ext4_init_acl(handle, inode, dir); 1182ac27a0ecSDave Kleikamp if (err) 1183ac27a0ecSDave Kleikamp goto fail_free_drop; 1184ac27a0ecSDave Kleikamp 11852a7dba39SEric Paris err = ext4_init_security(handle, inode, dir, qstr); 1186ac27a0ecSDave Kleikamp if (err) 1187ac27a0ecSDave Kleikamp goto fail_free_drop; 1188ad47f953STahsin Erdogan } 1189ac27a0ecSDave Kleikamp 1190e2b911c5SDarrick J. Wong if (ext4_has_feature_extents(sb)) { 1191e4079a11SEric Sandeen /* set extent flag only for directory, file and normal symlink*/ 1192e65187e6SAneesh Kumar K.V if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) { 119312e9b892SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS); 1194a86c6181SAlex Tomas ext4_ext_tree_init(handle, inode); 1195a86c6181SAlex Tomas } 119642bf0383SAneesh Kumar K.V } 1197ac27a0ecSDave Kleikamp 1198688f869cSTheodore Ts'o if (ext4_handle_valid(handle)) { 1199688f869cSTheodore Ts'o ei->i_sync_tid = handle->h_transaction->t_tid; 1200688f869cSTheodore Ts'o ei->i_datasync_tid = handle->h_transaction->t_tid; 1201688f869cSTheodore Ts'o } 1202688f869cSTheodore Ts'o 12038753e88fSAneesh Kumar K.V err = ext4_mark_inode_dirty(handle, inode); 12048753e88fSAneesh Kumar K.V if (err) { 12058753e88fSAneesh Kumar K.V ext4_std_error(sb, err); 12068753e88fSAneesh Kumar K.V goto fail_free_drop; 12078753e88fSAneesh Kumar K.V } 12088753e88fSAneesh Kumar K.V 1209617ba13bSMingming Cao ext4_debug("allocating inode %lu\n", inode->i_ino); 12109bffad1eSTheodore Ts'o trace_ext4_allocate_inode(inode, dir, mode); 12113300bedaSAneesh Kumar K.V brelse(inode_bitmap_bh); 1212ac27a0ecSDave Kleikamp return ret; 1213ac27a0ecSDave Kleikamp 1214ac27a0ecSDave Kleikamp fail_free_drop: 121563936ddaSChristoph Hellwig dquot_free_inode(inode); 1216ac27a0ecSDave Kleikamp fail_drop: 12176d6b77f1SMiklos Szeredi clear_nlink(inode); 12186b38e842SAl Viro unlock_new_inode(inode); 1219eb9cc7e1SJan Kara out: 1220eb9cc7e1SJan Kara dquot_drop(inode); 1221eb9cc7e1SJan Kara inode->i_flags |= S_NOQUOTA; 1222ac27a0ecSDave Kleikamp iput(inode); 12233300bedaSAneesh Kumar K.V brelse(inode_bitmap_bh); 1224ac27a0ecSDave Kleikamp return ERR_PTR(err); 1225ac27a0ecSDave Kleikamp } 1226ac27a0ecSDave Kleikamp 1227ac27a0ecSDave Kleikamp /* Verify that we are loading a valid orphan from disk */ 1228617ba13bSMingming Cao struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino) 1229ac27a0ecSDave Kleikamp { 1230617ba13bSMingming Cao unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count); 1231fd2d4291SAvantika Mathur ext4_group_t block_group; 1232ac27a0ecSDave Kleikamp int bit; 12337827a7f6STheodore Ts'o struct buffer_head *bitmap_bh = NULL; 1234ac27a0ecSDave Kleikamp struct inode *inode = NULL; 12357827a7f6STheodore Ts'o int err = -EFSCORRUPTED; 1236ac27a0ecSDave Kleikamp 12377827a7f6STheodore Ts'o if (ino < EXT4_FIRST_INO(sb) || ino > max_ino) 12387827a7f6STheodore Ts'o goto bad_orphan; 1239ac27a0ecSDave Kleikamp 1240617ba13bSMingming Cao block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); 1241617ba13bSMingming Cao bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); 1242e29d1cdeSEric Sandeen bitmap_bh = ext4_read_inode_bitmap(sb, block_group); 12439008a58eSDarrick J. Wong if (IS_ERR(bitmap_bh)) { 12447827a7f6STheodore Ts'o ext4_error(sb, "inode bitmap error %ld for orphan %lu", 12457827a7f6STheodore Ts'o ino, PTR_ERR(bitmap_bh)); 12467827a7f6STheodore Ts'o return (struct inode *) bitmap_bh; 1247ac27a0ecSDave Kleikamp } 1248ac27a0ecSDave Kleikamp 1249ac27a0ecSDave Kleikamp /* Having the inode bit set should be a 100% indicator that this 1250ac27a0ecSDave Kleikamp * is a valid orphan (no e2fsck run on fs). Orphans also include 1251ac27a0ecSDave Kleikamp * inodes that were being truncated, so we can't check i_nlink==0. 1252ac27a0ecSDave Kleikamp */ 12531d1fe1eeSDavid Howells if (!ext4_test_bit(bit, bitmap_bh->b_data)) 12541d1fe1eeSDavid Howells goto bad_orphan; 12551d1fe1eeSDavid Howells 12561d1fe1eeSDavid Howells inode = ext4_iget(sb, ino); 12577827a7f6STheodore Ts'o if (IS_ERR(inode)) { 12587827a7f6STheodore Ts'o err = PTR_ERR(inode); 12597827a7f6STheodore Ts'o ext4_error(sb, "couldn't read orphan inode %lu (err %d)", 12607827a7f6STheodore Ts'o ino, err); 12617827a7f6STheodore Ts'o return inode; 12627827a7f6STheodore Ts'o } 12631d1fe1eeSDavid Howells 126491ef4cafSDuane Griffin /* 1265c9eb13a9STheodore Ts'o * If the orphans has i_nlinks > 0 then it should be able to 1266c9eb13a9STheodore Ts'o * be truncated, otherwise it won't be removed from the orphan 1267c9eb13a9STheodore Ts'o * list during processing and an infinite loop will result. 1268c9eb13a9STheodore Ts'o * Similarly, it must not be a bad inode. 126991ef4cafSDuane Griffin */ 1270c9eb13a9STheodore Ts'o if ((inode->i_nlink && !ext4_can_truncate(inode)) || 1271c9eb13a9STheodore Ts'o is_bad_inode(inode)) 127291ef4cafSDuane Griffin goto bad_orphan; 127391ef4cafSDuane Griffin 12741d1fe1eeSDavid Howells if (NEXT_ORPHAN(inode) > max_ino) 12751d1fe1eeSDavid Howells goto bad_orphan; 12761d1fe1eeSDavid Howells brelse(bitmap_bh); 12771d1fe1eeSDavid Howells return inode; 12781d1fe1eeSDavid Howells 12791d1fe1eeSDavid Howells bad_orphan: 12807827a7f6STheodore Ts'o ext4_error(sb, "bad orphan inode %lu", ino); 12817827a7f6STheodore Ts'o if (bitmap_bh) 12827827a7f6STheodore Ts'o printk(KERN_ERR "ext4_test_bit(bit=%d, block=%llu) = %d\n", 1283ac27a0ecSDave Kleikamp bit, (unsigned long long)bitmap_bh->b_blocknr, 1284617ba13bSMingming Cao ext4_test_bit(bit, bitmap_bh->b_data)); 1285ac27a0ecSDave Kleikamp if (inode) { 12867827a7f6STheodore Ts'o printk(KERN_ERR "is_bad_inode(inode)=%d\n", 1287ac27a0ecSDave Kleikamp is_bad_inode(inode)); 12887827a7f6STheodore Ts'o printk(KERN_ERR "NEXT_ORPHAN(inode)=%u\n", 1289ac27a0ecSDave Kleikamp NEXT_ORPHAN(inode)); 12907827a7f6STheodore Ts'o printk(KERN_ERR "max_ino=%lu\n", max_ino); 12917827a7f6STheodore Ts'o printk(KERN_ERR "i_nlink=%u\n", inode->i_nlink); 1292ac27a0ecSDave Kleikamp /* Avoid freeing blocks if we got a bad deleted inode */ 12931d1fe1eeSDavid Howells if (inode->i_nlink == 0) 1294ac27a0ecSDave Kleikamp inode->i_blocks = 0; 1295ac27a0ecSDave Kleikamp iput(inode); 1296ac27a0ecSDave Kleikamp } 1297ac27a0ecSDave Kleikamp brelse(bitmap_bh); 12981d1fe1eeSDavid Howells return ERR_PTR(err); 1299ac27a0ecSDave Kleikamp } 1300ac27a0ecSDave Kleikamp 1301617ba13bSMingming Cao unsigned long ext4_count_free_inodes(struct super_block *sb) 1302ac27a0ecSDave Kleikamp { 1303ac27a0ecSDave Kleikamp unsigned long desc_count; 1304617ba13bSMingming Cao struct ext4_group_desc *gdp; 13058df9675fSTheodore Ts'o ext4_group_t i, ngroups = ext4_get_groups_count(sb); 1306617ba13bSMingming Cao #ifdef EXT4FS_DEBUG 1307617ba13bSMingming Cao struct ext4_super_block *es; 1308ac27a0ecSDave Kleikamp unsigned long bitmap_count, x; 1309ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh = NULL; 1310ac27a0ecSDave Kleikamp 1311617ba13bSMingming Cao es = EXT4_SB(sb)->s_es; 1312ac27a0ecSDave Kleikamp desc_count = 0; 1313ac27a0ecSDave Kleikamp bitmap_count = 0; 1314ac27a0ecSDave Kleikamp gdp = NULL; 13158df9675fSTheodore Ts'o for (i = 0; i < ngroups; i++) { 1316617ba13bSMingming Cao gdp = ext4_get_group_desc(sb, i, NULL); 1317ac27a0ecSDave Kleikamp if (!gdp) 1318ac27a0ecSDave Kleikamp continue; 1319560671a0SAneesh Kumar K.V desc_count += ext4_free_inodes_count(sb, gdp); 1320ac27a0ecSDave Kleikamp brelse(bitmap_bh); 1321e29d1cdeSEric Sandeen bitmap_bh = ext4_read_inode_bitmap(sb, i); 13229008a58eSDarrick J. Wong if (IS_ERR(bitmap_bh)) { 13239008a58eSDarrick J. Wong bitmap_bh = NULL; 1324ac27a0ecSDave Kleikamp continue; 13259008a58eSDarrick J. Wong } 1326ac27a0ecSDave Kleikamp 1327f6fb99caSTheodore Ts'o x = ext4_count_free(bitmap_bh->b_data, 1328f6fb99caSTheodore Ts'o EXT4_INODES_PER_GROUP(sb) / 8); 1329c549a95dSEric Sandeen printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n", 1330785b4b3aSPeng Tao (unsigned long) i, ext4_free_inodes_count(sb, gdp), x); 1331ac27a0ecSDave Kleikamp bitmap_count += x; 1332ac27a0ecSDave Kleikamp } 1333ac27a0ecSDave Kleikamp brelse(bitmap_bh); 13344776004fSTheodore Ts'o printk(KERN_DEBUG "ext4_count_free_inodes: " 13354776004fSTheodore Ts'o "stored = %u, computed = %lu, %lu\n", 1336ac27a0ecSDave Kleikamp le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count); 1337ac27a0ecSDave Kleikamp return desc_count; 1338ac27a0ecSDave Kleikamp #else 1339ac27a0ecSDave Kleikamp desc_count = 0; 13408df9675fSTheodore Ts'o for (i = 0; i < ngroups; i++) { 1341617ba13bSMingming Cao gdp = ext4_get_group_desc(sb, i, NULL); 1342ac27a0ecSDave Kleikamp if (!gdp) 1343ac27a0ecSDave Kleikamp continue; 1344560671a0SAneesh Kumar K.V desc_count += ext4_free_inodes_count(sb, gdp); 1345ac27a0ecSDave Kleikamp cond_resched(); 1346ac27a0ecSDave Kleikamp } 1347ac27a0ecSDave Kleikamp return desc_count; 1348ac27a0ecSDave Kleikamp #endif 1349ac27a0ecSDave Kleikamp } 1350ac27a0ecSDave Kleikamp 1351ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */ 1352617ba13bSMingming Cao unsigned long ext4_count_dirs(struct super_block * sb) 1353ac27a0ecSDave Kleikamp { 1354ac27a0ecSDave Kleikamp unsigned long count = 0; 13558df9675fSTheodore Ts'o ext4_group_t i, ngroups = ext4_get_groups_count(sb); 1356ac27a0ecSDave Kleikamp 13578df9675fSTheodore Ts'o for (i = 0; i < ngroups; i++) { 1358617ba13bSMingming Cao struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL); 1359ac27a0ecSDave Kleikamp if (!gdp) 1360ac27a0ecSDave Kleikamp continue; 1361560671a0SAneesh Kumar K.V count += ext4_used_dirs_count(sb, gdp); 1362ac27a0ecSDave Kleikamp } 1363ac27a0ecSDave Kleikamp return count; 1364ac27a0ecSDave Kleikamp } 1365bfff6873SLukas Czerner 1366bfff6873SLukas Czerner /* 1367bfff6873SLukas Czerner * Zeroes not yet zeroed inode table - just write zeroes through the whole 1368bfff6873SLukas Czerner * inode table. Must be called without any spinlock held. The only place 1369bfff6873SLukas Czerner * where it is called from on active part of filesystem is ext4lazyinit 1370bfff6873SLukas Czerner * thread, so we do not need any special locks, however we have to prevent 1371bfff6873SLukas Czerner * inode allocation from the current group, so we take alloc_sem lock, to 1372119c0d44STheodore Ts'o * block ext4_new_inode() until we are finished. 1373bfff6873SLukas Czerner */ 1374e0cbee3eSH Hartley Sweeten int ext4_init_inode_table(struct super_block *sb, ext4_group_t group, 1375bfff6873SLukas Czerner int barrier) 1376bfff6873SLukas Czerner { 1377bfff6873SLukas Czerner struct ext4_group_info *grp = ext4_get_group_info(sb, group); 1378bfff6873SLukas Czerner struct ext4_sb_info *sbi = EXT4_SB(sb); 1379bfff6873SLukas Czerner struct ext4_group_desc *gdp = NULL; 1380bfff6873SLukas Czerner struct buffer_head *group_desc_bh; 1381bfff6873SLukas Czerner handle_t *handle; 1382bfff6873SLukas Czerner ext4_fsblk_t blk; 1383bfff6873SLukas Czerner int num, ret = 0, used_blks = 0; 1384bfff6873SLukas Czerner 1385bfff6873SLukas Czerner /* This should not happen, but just to be sure check this */ 1386bc98a42cSDavid Howells if (sb_rdonly(sb)) { 1387bfff6873SLukas Czerner ret = 1; 1388bfff6873SLukas Czerner goto out; 1389bfff6873SLukas Czerner } 1390bfff6873SLukas Czerner 1391bfff6873SLukas Czerner gdp = ext4_get_group_desc(sb, group, &group_desc_bh); 1392bfff6873SLukas Czerner if (!gdp) 1393bfff6873SLukas Czerner goto out; 1394bfff6873SLukas Czerner 1395bfff6873SLukas Czerner /* 1396bfff6873SLukas Czerner * We do not need to lock this, because we are the only one 1397bfff6873SLukas Czerner * handling this flag. 1398bfff6873SLukas Czerner */ 1399bfff6873SLukas Czerner if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)) 1400bfff6873SLukas Czerner goto out; 1401bfff6873SLukas Czerner 14029924a92aSTheodore Ts'o handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1); 1403bfff6873SLukas Czerner if (IS_ERR(handle)) { 1404bfff6873SLukas Czerner ret = PTR_ERR(handle); 1405bfff6873SLukas Czerner goto out; 1406bfff6873SLukas Czerner } 1407bfff6873SLukas Czerner 1408bfff6873SLukas Czerner down_write(&grp->alloc_sem); 1409bfff6873SLukas Czerner /* 1410bfff6873SLukas Czerner * If inode bitmap was already initialized there may be some 1411bfff6873SLukas Czerner * used inodes so we need to skip blocks with used inodes in 1412bfff6873SLukas Czerner * inode table. 1413bfff6873SLukas Czerner */ 1414bfff6873SLukas Czerner if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT))) 1415bfff6873SLukas Czerner used_blks = DIV_ROUND_UP((EXT4_INODES_PER_GROUP(sb) - 1416bfff6873SLukas Czerner ext4_itable_unused_count(sb, gdp)), 1417bfff6873SLukas Czerner sbi->s_inodes_per_block); 1418bfff6873SLukas Czerner 1419857ac889SLukas Czerner if ((used_blks < 0) || (used_blks > sbi->s_itb_per_group)) { 14201084f252STheodore Ts'o ext4_error(sb, "Something is wrong with group %u: " 14211084f252STheodore Ts'o "used itable blocks: %d; " 14221084f252STheodore Ts'o "itable unused count: %u", 1423857ac889SLukas Czerner group, used_blks, 1424857ac889SLukas Czerner ext4_itable_unused_count(sb, gdp)); 1425857ac889SLukas Czerner ret = 1; 142633853a0dSYongqiang Yang goto err_out; 1427857ac889SLukas Czerner } 1428857ac889SLukas Czerner 1429bfff6873SLukas Czerner blk = ext4_inode_table(sb, gdp) + used_blks; 1430bfff6873SLukas Czerner num = sbi->s_itb_per_group - used_blks; 1431bfff6873SLukas Czerner 1432bfff6873SLukas Czerner BUFFER_TRACE(group_desc_bh, "get_write_access"); 1433bfff6873SLukas Czerner ret = ext4_journal_get_write_access(handle, 1434bfff6873SLukas Czerner group_desc_bh); 1435bfff6873SLukas Czerner if (ret) 1436bfff6873SLukas Czerner goto err_out; 1437bfff6873SLukas Czerner 1438bfff6873SLukas Czerner /* 1439bfff6873SLukas Czerner * Skip zeroout if the inode table is full. But we set the ZEROED 1440bfff6873SLukas Czerner * flag anyway, because obviously, when it is full it does not need 1441bfff6873SLukas Czerner * further zeroing. 1442bfff6873SLukas Czerner */ 1443bfff6873SLukas Czerner if (unlikely(num == 0)) 1444bfff6873SLukas Czerner goto skip_zeroout; 1445bfff6873SLukas Czerner 1446bfff6873SLukas Czerner ext4_debug("going to zero out inode table in group %d\n", 1447bfff6873SLukas Czerner group); 1448a107e5a3STheodore Ts'o ret = sb_issue_zeroout(sb, blk, num, GFP_NOFS); 1449bfff6873SLukas Czerner if (ret < 0) 1450bfff6873SLukas Czerner goto err_out; 1451a107e5a3STheodore Ts'o if (barrier) 1452a107e5a3STheodore Ts'o blkdev_issue_flush(sb->s_bdev, GFP_NOFS, NULL); 1453bfff6873SLukas Czerner 1454bfff6873SLukas Czerner skip_zeroout: 1455bfff6873SLukas Czerner ext4_lock_group(sb, group); 1456bfff6873SLukas Czerner gdp->bg_flags |= cpu_to_le16(EXT4_BG_INODE_ZEROED); 1457feb0ab32SDarrick J. Wong ext4_group_desc_csum_set(sb, group, gdp); 1458bfff6873SLukas Czerner ext4_unlock_group(sb, group); 1459bfff6873SLukas Czerner 1460bfff6873SLukas Czerner BUFFER_TRACE(group_desc_bh, 1461bfff6873SLukas Czerner "call ext4_handle_dirty_metadata"); 1462bfff6873SLukas Czerner ret = ext4_handle_dirty_metadata(handle, NULL, 1463bfff6873SLukas Czerner group_desc_bh); 1464bfff6873SLukas Czerner 1465bfff6873SLukas Czerner err_out: 1466bfff6873SLukas Czerner up_write(&grp->alloc_sem); 1467bfff6873SLukas Czerner ext4_journal_stop(handle); 1468bfff6873SLukas Czerner out: 1469bfff6873SLukas Czerner return ret; 1470bfff6873SLukas Czerner } 1471