1b2441318SGreg 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 69813e5727STheodore Ts'o void ext4_end_bitmap_read(struct buffer_head *bh, int uptodate) 70813e5727STheodore Ts'o { 71813e5727STheodore Ts'o if (uptodate) { 72813e5727STheodore Ts'o set_buffer_uptodate(bh); 73813e5727STheodore Ts'o set_bitmap_uptodate(bh); 74813e5727STheodore Ts'o } 75813e5727STheodore Ts'o unlock_buffer(bh); 76813e5727STheodore Ts'o put_bh(bh); 77813e5727STheodore Ts'o } 78813e5727STheodore Ts'o 799008a58eSDarrick J. Wong static int ext4_validate_inode_bitmap(struct super_block *sb, 809008a58eSDarrick J. Wong struct ext4_group_desc *desc, 819008a58eSDarrick J. Wong ext4_group_t block_group, 829008a58eSDarrick J. Wong struct buffer_head *bh) 839008a58eSDarrick J. Wong { 849008a58eSDarrick J. Wong ext4_fsblk_t blk; 859008a58eSDarrick J. Wong struct ext4_group_info *grp = ext4_get_group_info(sb, block_group); 869008a58eSDarrick J. Wong 879008a58eSDarrick J. Wong if (buffer_verified(bh)) 889008a58eSDarrick J. Wong return 0; 899008a58eSDarrick J. Wong if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) 909008a58eSDarrick J. Wong return -EFSCORRUPTED; 919008a58eSDarrick J. Wong 929008a58eSDarrick J. Wong ext4_lock_group(sb, block_group); 938d5a803cSTheodore Ts'o if (buffer_verified(bh)) 948d5a803cSTheodore Ts'o goto verified; 959008a58eSDarrick J. Wong blk = ext4_inode_bitmap(sb, desc); 969008a58eSDarrick J. Wong if (!ext4_inode_bitmap_csum_verify(sb, block_group, desc, bh, 97*46f870d6STheodore Ts'o EXT4_INODES_PER_GROUP(sb) / 8) || 98*46f870d6STheodore Ts'o ext4_simulate_fail(sb, EXT4_SIM_IBITMAP_CRC)) { 999008a58eSDarrick J. Wong ext4_unlock_group(sb, block_group); 1009008a58eSDarrick J. Wong ext4_error(sb, "Corrupt inode bitmap - block_group = %u, " 1019008a58eSDarrick J. Wong "inode_bitmap = %llu", block_group, blk); 102db79e6d1SWang Shilong ext4_mark_group_bitmap_corrupted(sb, block_group, 103db79e6d1SWang Shilong EXT4_GROUP_INFO_IBITMAP_CORRUPT); 1049008a58eSDarrick J. Wong return -EFSBADCRC; 1059008a58eSDarrick J. Wong } 1069008a58eSDarrick J. Wong set_buffer_verified(bh); 1078d5a803cSTheodore Ts'o verified: 1089008a58eSDarrick J. Wong ext4_unlock_group(sb, block_group); 1099008a58eSDarrick J. Wong return 0; 1109008a58eSDarrick J. Wong } 1119008a58eSDarrick J. Wong 112ac27a0ecSDave Kleikamp /* 113ac27a0ecSDave Kleikamp * Read the inode allocation bitmap for a given block_group, reading 114ac27a0ecSDave Kleikamp * into the specified slot in the superblock's bitmap cache. 115ac27a0ecSDave Kleikamp * 116ac27a0ecSDave Kleikamp * Return buffer_head of bitmap on success or NULL. 117ac27a0ecSDave Kleikamp */ 118ac27a0ecSDave Kleikamp static struct buffer_head * 119e29d1cdeSEric Sandeen ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group) 120ac27a0ecSDave Kleikamp { 121617ba13bSMingming Cao struct ext4_group_desc *desc; 1227dac4a17STheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(sb); 123ac27a0ecSDave Kleikamp struct buffer_head *bh = NULL; 124e29d1cdeSEric Sandeen ext4_fsblk_t bitmap_blk; 1259008a58eSDarrick J. Wong int err; 126ac27a0ecSDave Kleikamp 127617ba13bSMingming Cao desc = ext4_get_group_desc(sb, block_group, NULL); 128ac27a0ecSDave Kleikamp if (!desc) 1299008a58eSDarrick J. Wong return ERR_PTR(-EFSCORRUPTED); 130bfff6873SLukas Czerner 131e29d1cdeSEric Sandeen bitmap_blk = ext4_inode_bitmap(sb, desc); 1327dac4a17STheodore Ts'o if ((bitmap_blk <= le32_to_cpu(sbi->s_es->s_first_data_block)) || 1337dac4a17STheodore Ts'o (bitmap_blk >= ext4_blocks_count(sbi->s_es))) { 1347dac4a17STheodore Ts'o ext4_error(sb, "Invalid inode bitmap blk %llu in " 1357dac4a17STheodore Ts'o "block_group %u", bitmap_blk, block_group); 136206f6d55SWang Shilong ext4_mark_group_bitmap_corrupted(sb, block_group, 137206f6d55SWang Shilong EXT4_GROUP_INFO_IBITMAP_CORRUPT); 1387dac4a17STheodore Ts'o return ERR_PTR(-EFSCORRUPTED); 1397dac4a17STheodore Ts'o } 140e29d1cdeSEric Sandeen bh = sb_getblk(sb, bitmap_blk); 141e29d1cdeSEric Sandeen if (unlikely(!bh)) { 1425ef2a699SWang Shilong ext4_warning(sb, "Cannot read inode bitmap - " 143a9df9a49STheodore Ts'o "block_group = %u, inode_bitmap = %llu", 144e29d1cdeSEric Sandeen block_group, bitmap_blk); 1450db9fdebSWang Shilong return ERR_PTR(-ENOMEM); 146e29d1cdeSEric Sandeen } 1472ccb5fb9SAneesh Kumar K.V if (bitmap_uptodate(bh)) 14841a246d1SDarrick J. Wong goto verify; 149e29d1cdeSEric Sandeen 150c806e68fSFrederic Bohe lock_buffer(bh); 1512ccb5fb9SAneesh Kumar K.V if (bitmap_uptodate(bh)) { 1522ccb5fb9SAneesh Kumar K.V unlock_buffer(bh); 15341a246d1SDarrick J. Wong goto verify; 1542ccb5fb9SAneesh Kumar K.V } 155bfff6873SLukas Czerner 156955ce5f5SAneesh Kumar K.V ext4_lock_group(sb, block_group); 1578844618dSTheodore Ts'o if (ext4_has_group_desc_csum(sb) && 1588844618dSTheodore Ts'o (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT))) { 1598844618dSTheodore Ts'o if (block_group == 0) { 1608844618dSTheodore Ts'o ext4_unlock_group(sb, block_group); 1618844618dSTheodore Ts'o unlock_buffer(bh); 1628844618dSTheodore Ts'o ext4_error(sb, "Inode bitmap for bg 0 marked " 1638844618dSTheodore Ts'o "uninitialized"); 1648844618dSTheodore Ts'o err = -EFSCORRUPTED; 1658844618dSTheodore Ts'o goto out; 1668844618dSTheodore Ts'o } 167044e6e3dSTheodore Ts'o memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8); 168044e6e3dSTheodore Ts'o ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), 169044e6e3dSTheodore Ts'o sb->s_blocksize * 8, bh->b_data); 1702ccb5fb9SAneesh Kumar K.V set_bitmap_uptodate(bh); 171e29d1cdeSEric Sandeen set_buffer_uptodate(bh); 17241a246d1SDarrick J. Wong set_buffer_verified(bh); 173955ce5f5SAneesh Kumar K.V ext4_unlock_group(sb, block_group); 1743300bedaSAneesh Kumar K.V unlock_buffer(bh); 175e29d1cdeSEric Sandeen return bh; 176e29d1cdeSEric Sandeen } 177955ce5f5SAneesh Kumar K.V ext4_unlock_group(sb, block_group); 178bfff6873SLukas Czerner 1792ccb5fb9SAneesh Kumar K.V if (buffer_uptodate(bh)) { 1802ccb5fb9SAneesh Kumar K.V /* 1812ccb5fb9SAneesh Kumar K.V * if not uninit if bh is uptodate, 1822ccb5fb9SAneesh Kumar K.V * bitmap is also uptodate 1832ccb5fb9SAneesh Kumar K.V */ 1842ccb5fb9SAneesh Kumar K.V set_bitmap_uptodate(bh); 1852ccb5fb9SAneesh Kumar K.V unlock_buffer(bh); 18641a246d1SDarrick J. Wong goto verify; 1872ccb5fb9SAneesh Kumar K.V } 1882ccb5fb9SAneesh Kumar K.V /* 189813e5727STheodore Ts'o * submit the buffer_head for reading 1902ccb5fb9SAneesh Kumar K.V */ 1910562e0baSJiaying Zhang trace_ext4_load_inode_bitmap(sb, block_group); 192813e5727STheodore Ts'o bh->b_end_io = ext4_end_bitmap_read; 193813e5727STheodore Ts'o get_bh(bh); 1942a222ca9SMike Christie submit_bh(REQ_OP_READ, REQ_META | REQ_PRIO, bh); 195813e5727STheodore Ts'o wait_on_buffer(bh); 196*46f870d6STheodore Ts'o ext4_simulate_fail_bh(sb, bh, EXT4_SIM_IBITMAP_EIO); 197813e5727STheodore Ts'o if (!buffer_uptodate(bh)) { 198e29d1cdeSEric Sandeen put_bh(bh); 199878520acSTheodore Ts'o ext4_set_errno(sb, EIO); 20012062dddSEric Sandeen ext4_error(sb, "Cannot read inode bitmap - " 201a9df9a49STheodore Ts'o "block_group = %u, inode_bitmap = %llu", 202e29d1cdeSEric Sandeen block_group, bitmap_blk); 203206f6d55SWang Shilong ext4_mark_group_bitmap_corrupted(sb, block_group, 204206f6d55SWang Shilong EXT4_GROUP_INFO_IBITMAP_CORRUPT); 2059008a58eSDarrick J. Wong return ERR_PTR(-EIO); 206e29d1cdeSEric Sandeen } 20741a246d1SDarrick J. Wong 20841a246d1SDarrick J. Wong verify: 2099008a58eSDarrick J. Wong err = ext4_validate_inode_bitmap(sb, desc, block_group, bh); 2109008a58eSDarrick J. Wong if (err) 2119008a58eSDarrick J. Wong goto out; 212ac27a0ecSDave Kleikamp return bh; 2139008a58eSDarrick J. Wong out: 2149008a58eSDarrick J. Wong put_bh(bh); 2159008a58eSDarrick J. Wong return ERR_PTR(err); 216ac27a0ecSDave Kleikamp } 217ac27a0ecSDave Kleikamp 218ac27a0ecSDave Kleikamp /* 219ac27a0ecSDave Kleikamp * NOTE! When we get the inode, we're the only people 220ac27a0ecSDave Kleikamp * that have access to it, and as such there are no 221ac27a0ecSDave Kleikamp * race conditions we have to worry about. The inode 222ac27a0ecSDave Kleikamp * is not on the hash-lists, and it cannot be reached 223ac27a0ecSDave Kleikamp * through the filesystem because the directory entry 224ac27a0ecSDave Kleikamp * has been deleted earlier. 225ac27a0ecSDave Kleikamp * 226ac27a0ecSDave Kleikamp * HOWEVER: we must make sure that we get no aliases, 227ac27a0ecSDave Kleikamp * which means that we have to call "clear_inode()" 228ac27a0ecSDave Kleikamp * _before_ we mark the inode not in use in the inode 229ac27a0ecSDave Kleikamp * bitmaps. Otherwise a newly created file might use 230ac27a0ecSDave Kleikamp * the same inode number (not actually the same pointer 231ac27a0ecSDave Kleikamp * though), and then we'd have two inodes sharing the 232ac27a0ecSDave Kleikamp * same inode number and space on the harddisk. 233ac27a0ecSDave Kleikamp */ 234617ba13bSMingming Cao void ext4_free_inode(handle_t *handle, struct inode *inode) 235ac27a0ecSDave Kleikamp { 236ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 237ac27a0ecSDave Kleikamp int is_directory; 238ac27a0ecSDave Kleikamp unsigned long ino; 239ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh = NULL; 240ac27a0ecSDave Kleikamp struct buffer_head *bh2; 241fd2d4291SAvantika Mathur ext4_group_t block_group; 242ac27a0ecSDave Kleikamp unsigned long bit; 243617ba13bSMingming Cao struct ext4_group_desc *gdp; 244617ba13bSMingming Cao struct ext4_super_block *es; 245617ba13bSMingming Cao struct ext4_sb_info *sbi; 2467ce9d5d1SEric Sandeen int fatal = 0, err, count, cleared; 24787a39389SDarrick J. Wong struct ext4_group_info *grp; 248ac27a0ecSDave Kleikamp 24992b97816STheodore Ts'o if (!sb) { 25092b97816STheodore Ts'o printk(KERN_ERR "EXT4-fs: %s:%d: inode on " 25192b97816STheodore Ts'o "nonexistent device\n", __func__, __LINE__); 25292b97816STheodore Ts'o return; 25392b97816STheodore Ts'o } 254ac27a0ecSDave Kleikamp if (atomic_read(&inode->i_count) > 1) { 25592b97816STheodore Ts'o ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: count=%d", 25692b97816STheodore Ts'o __func__, __LINE__, inode->i_ino, 257ac27a0ecSDave Kleikamp atomic_read(&inode->i_count)); 258ac27a0ecSDave Kleikamp return; 259ac27a0ecSDave Kleikamp } 260ac27a0ecSDave Kleikamp if (inode->i_nlink) { 26192b97816STheodore Ts'o ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: nlink=%d\n", 26292b97816STheodore Ts'o __func__, __LINE__, inode->i_ino, inode->i_nlink); 263ac27a0ecSDave Kleikamp return; 264ac27a0ecSDave Kleikamp } 265617ba13bSMingming Cao sbi = EXT4_SB(sb); 266ac27a0ecSDave Kleikamp 267ac27a0ecSDave Kleikamp ino = inode->i_ino; 268617ba13bSMingming Cao ext4_debug("freeing inode %lu\n", ino); 2699bffad1eSTheodore Ts'o trace_ext4_free_inode(inode); 270ac27a0ecSDave Kleikamp 271871a2931SChristoph Hellwig dquot_initialize(inode); 27263936ddaSChristoph Hellwig dquot_free_inode(inode); 273ac27a0ecSDave Kleikamp 274ac27a0ecSDave Kleikamp is_directory = S_ISDIR(inode->i_mode); 275ac27a0ecSDave Kleikamp 276ac27a0ecSDave Kleikamp /* Do this BEFORE marking the inode not in use or returning an error */ 2770930fcc1SAl Viro ext4_clear_inode(inode); 278ac27a0ecSDave Kleikamp 27949598e04SJun Piao es = sbi->s_es; 280617ba13bSMingming Cao if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) { 28112062dddSEric Sandeen ext4_error(sb, "reserved or nonexistent inode %lu", ino); 282ac27a0ecSDave Kleikamp goto error_return; 283ac27a0ecSDave Kleikamp } 284617ba13bSMingming Cao block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); 285617ba13bSMingming Cao bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); 286e29d1cdeSEric Sandeen bitmap_bh = ext4_read_inode_bitmap(sb, block_group); 28787a39389SDarrick J. Wong /* Don't bother if the inode bitmap is corrupt. */ 28887a39389SDarrick J. Wong grp = ext4_get_group_info(sb, block_group); 2899008a58eSDarrick J. Wong if (IS_ERR(bitmap_bh)) { 2909008a58eSDarrick J. Wong fatal = PTR_ERR(bitmap_bh); 2919008a58eSDarrick J. Wong bitmap_bh = NULL; 292ac27a0ecSDave Kleikamp goto error_return; 2939008a58eSDarrick J. Wong } 2949008a58eSDarrick J. Wong if (unlikely(EXT4_MB_GRP_IBITMAP_CORRUPT(grp))) { 2959008a58eSDarrick J. Wong fatal = -EFSCORRUPTED; 2969008a58eSDarrick J. Wong goto error_return; 2979008a58eSDarrick J. Wong } 298ac27a0ecSDave Kleikamp 299ac27a0ecSDave Kleikamp BUFFER_TRACE(bitmap_bh, "get_write_access"); 300617ba13bSMingming Cao fatal = ext4_journal_get_write_access(handle, bitmap_bh); 301ac27a0ecSDave Kleikamp if (fatal) 302ac27a0ecSDave Kleikamp goto error_return; 303ac27a0ecSDave Kleikamp 304d17413c0SDmitry Monakhov fatal = -ESRCH; 305617ba13bSMingming Cao gdp = ext4_get_group_desc(sb, block_group, &bh2); 306d17413c0SDmitry Monakhov if (gdp) { 307ac27a0ecSDave Kleikamp BUFFER_TRACE(bh2, "get_write_access"); 308617ba13bSMingming Cao fatal = ext4_journal_get_write_access(handle, bh2); 309d17413c0SDmitry Monakhov } 310955ce5f5SAneesh Kumar K.V ext4_lock_group(sb, block_group); 311597d508cSAkinobu Mita cleared = ext4_test_and_clear_bit(bit, bitmap_bh->b_data); 312d17413c0SDmitry Monakhov if (fatal || !cleared) { 313d17413c0SDmitry Monakhov ext4_unlock_group(sb, block_group); 314d17413c0SDmitry Monakhov goto out; 315d17413c0SDmitry Monakhov } 316d17413c0SDmitry Monakhov 317560671a0SAneesh Kumar K.V count = ext4_free_inodes_count(sb, gdp) + 1; 318560671a0SAneesh Kumar K.V ext4_free_inodes_set(sb, gdp, count); 319560671a0SAneesh Kumar K.V if (is_directory) { 320560671a0SAneesh Kumar K.V count = ext4_used_dirs_count(sb, gdp) - 1; 321560671a0SAneesh Kumar K.V ext4_used_dirs_set(sb, gdp, count); 322d17413c0SDmitry Monakhov percpu_counter_dec(&sbi->s_dirs_counter); 323d17413c0SDmitry Monakhov } 32441a246d1SDarrick J. Wong ext4_inode_bitmap_csum_set(sb, block_group, gdp, bitmap_bh, 32541a246d1SDarrick J. Wong EXT4_INODES_PER_GROUP(sb) / 8); 326feb0ab32SDarrick J. Wong ext4_group_desc_csum_set(sb, block_group, gdp); 327d17413c0SDmitry Monakhov ext4_unlock_group(sb, block_group); 3287d39db14STheodore Ts'o 329d17413c0SDmitry Monakhov percpu_counter_inc(&sbi->s_freeinodes_counter); 330d17413c0SDmitry Monakhov if (sbi->s_log_groups_per_flex) { 331d17413c0SDmitry Monakhov ext4_group_t f = ext4_flex_group(sbi, block_group); 332d17413c0SDmitry Monakhov 333d17413c0SDmitry Monakhov atomic_inc(&sbi->s_flex_groups[f].free_inodes); 334d17413c0SDmitry Monakhov if (is_directory) 335c4caae25SEric Sandeen atomic_dec(&sbi->s_flex_groups[f].used_dirs); 3367d39db14STheodore Ts'o } 3370390131bSFrank Mayhar BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata"); 338d17413c0SDmitry Monakhov fatal = ext4_handle_dirty_metadata(handle, NULL, bh2); 339d17413c0SDmitry Monakhov out: 340d17413c0SDmitry Monakhov if (cleared) { 3410390131bSFrank Mayhar BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata"); 3420390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh); 343ac27a0ecSDave Kleikamp if (!fatal) 344ac27a0ecSDave Kleikamp fatal = err; 34587a39389SDarrick J. Wong } else { 346d17413c0SDmitry Monakhov ext4_error(sb, "bit already cleared for inode %lu", ino); 347db79e6d1SWang Shilong ext4_mark_group_bitmap_corrupted(sb, block_group, 348db79e6d1SWang Shilong EXT4_GROUP_INFO_IBITMAP_CORRUPT); 34987a39389SDarrick J. Wong } 350d17413c0SDmitry Monakhov 351ac27a0ecSDave Kleikamp error_return: 352ac27a0ecSDave Kleikamp brelse(bitmap_bh); 353617ba13bSMingming Cao ext4_std_error(sb, fatal); 354ac27a0ecSDave Kleikamp } 355ac27a0ecSDave Kleikamp 356a4912123STheodore Ts'o struct orlov_stats { 35790ba983fSTheodore Ts'o __u64 free_clusters; 358a4912123STheodore Ts'o __u32 free_inodes; 359a4912123STheodore Ts'o __u32 used_dirs; 360a4912123STheodore Ts'o }; 361a4912123STheodore Ts'o 362a4912123STheodore Ts'o /* 363a4912123STheodore Ts'o * Helper function for Orlov's allocator; returns critical information 364a4912123STheodore Ts'o * for a particular block group or flex_bg. If flex_size is 1, then g 365a4912123STheodore Ts'o * is a block group number; otherwise it is flex_bg number. 366a4912123STheodore Ts'o */ 3671f109d5aSTheodore Ts'o static void get_orlov_stats(struct super_block *sb, ext4_group_t g, 368a4912123STheodore Ts'o int flex_size, struct orlov_stats *stats) 369a4912123STheodore Ts'o { 370a4912123STheodore Ts'o struct ext4_group_desc *desc; 3717d39db14STheodore Ts'o struct flex_groups *flex_group = EXT4_SB(sb)->s_flex_groups; 372a4912123STheodore Ts'o 3737d39db14STheodore Ts'o if (flex_size > 1) { 3747d39db14STheodore Ts'o stats->free_inodes = atomic_read(&flex_group[g].free_inodes); 37590ba983fSTheodore Ts'o stats->free_clusters = atomic64_read(&flex_group[g].free_clusters); 3767d39db14STheodore Ts'o stats->used_dirs = atomic_read(&flex_group[g].used_dirs); 3777d39db14STheodore Ts'o return; 3787d39db14STheodore Ts'o } 3797d39db14STheodore Ts'o 3807d39db14STheodore Ts'o desc = ext4_get_group_desc(sb, g, NULL); 3817d39db14STheodore Ts'o if (desc) { 3827d39db14STheodore Ts'o stats->free_inodes = ext4_free_inodes_count(sb, desc); 383021b65bbSTheodore Ts'o stats->free_clusters = ext4_free_group_clusters(sb, desc); 3847d39db14STheodore Ts'o stats->used_dirs = ext4_used_dirs_count(sb, desc); 3857d39db14STheodore Ts'o } else { 386a4912123STheodore Ts'o stats->free_inodes = 0; 38724aaa8efSTheodore Ts'o stats->free_clusters = 0; 388a4912123STheodore Ts'o stats->used_dirs = 0; 389a4912123STheodore Ts'o } 390a4912123STheodore Ts'o } 391a4912123STheodore Ts'o 392ac27a0ecSDave Kleikamp /* 393ac27a0ecSDave Kleikamp * Orlov's allocator for directories. 394ac27a0ecSDave Kleikamp * 395ac27a0ecSDave Kleikamp * We always try to spread first-level directories. 396ac27a0ecSDave Kleikamp * 397ac27a0ecSDave Kleikamp * If there are blockgroups with both free inodes and free blocks counts 398ac27a0ecSDave Kleikamp * not worse than average we return one with smallest directory count. 399ac27a0ecSDave Kleikamp * Otherwise we simply return a random group. 400ac27a0ecSDave Kleikamp * 401ac27a0ecSDave Kleikamp * For the rest rules look so: 402ac27a0ecSDave Kleikamp * 403ac27a0ecSDave Kleikamp * It's OK to put directory into a group unless 404ac27a0ecSDave Kleikamp * it has too many directories already (max_dirs) or 405ac27a0ecSDave Kleikamp * it has too few free inodes left (min_inodes) or 406ac27a0ecSDave Kleikamp * it has too few free blocks left (min_blocks) or 4071cc8dcf5SBenoit Boissinot * Parent's group is preferred, if it doesn't satisfy these 408ac27a0ecSDave Kleikamp * conditions we search cyclically through the rest. If none 409ac27a0ecSDave Kleikamp * of the groups look good we just look for a group with more 410ac27a0ecSDave Kleikamp * free inodes than average (starting at parent's group). 411ac27a0ecSDave Kleikamp */ 412ac27a0ecSDave Kleikamp 4132aa9fc4cSAvantika Mathur static int find_group_orlov(struct super_block *sb, struct inode *parent, 414dcca3fecSAl Viro ext4_group_t *group, umode_t mode, 415f157a4aaSTheodore Ts'o const struct qstr *qstr) 416ac27a0ecSDave Kleikamp { 417fd2d4291SAvantika Mathur ext4_group_t parent_group = EXT4_I(parent)->i_block_group; 418617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 4198df9675fSTheodore Ts'o ext4_group_t real_ngroups = ext4_get_groups_count(sb); 420617ba13bSMingming Cao int inodes_per_group = EXT4_INODES_PER_GROUP(sb); 42114c83c9fSTheodore Ts'o unsigned int freei, avefreei, grp_free; 42224aaa8efSTheodore Ts'o ext4_fsblk_t freeb, avefreec; 423ac27a0ecSDave Kleikamp unsigned int ndirs; 424a4912123STheodore Ts'o int max_dirs, min_inodes; 42524aaa8efSTheodore Ts'o ext4_grpblk_t min_clusters; 4268df9675fSTheodore Ts'o ext4_group_t i, grp, g, ngroups; 427617ba13bSMingming Cao struct ext4_group_desc *desc; 428a4912123STheodore Ts'o struct orlov_stats stats; 429a4912123STheodore Ts'o int flex_size = ext4_flex_bg_size(sbi); 430f157a4aaSTheodore Ts'o struct dx_hash_info hinfo; 431a4912123STheodore Ts'o 4328df9675fSTheodore Ts'o ngroups = real_ngroups; 433a4912123STheodore Ts'o if (flex_size > 1) { 4348df9675fSTheodore Ts'o ngroups = (real_ngroups + flex_size - 1) >> 435a4912123STheodore Ts'o sbi->s_log_groups_per_flex; 436a4912123STheodore Ts'o parent_group >>= sbi->s_log_groups_per_flex; 437a4912123STheodore Ts'o } 438ac27a0ecSDave Kleikamp 439ac27a0ecSDave Kleikamp freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter); 440ac27a0ecSDave Kleikamp avefreei = freei / ngroups; 44157042651STheodore Ts'o freeb = EXT4_C2B(sbi, 44257042651STheodore Ts'o percpu_counter_read_positive(&sbi->s_freeclusters_counter)); 44324aaa8efSTheodore Ts'o avefreec = freeb; 44424aaa8efSTheodore Ts'o do_div(avefreec, ngroups); 445ac27a0ecSDave Kleikamp ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter); 446ac27a0ecSDave Kleikamp 447a4912123STheodore Ts'o if (S_ISDIR(mode) && 4482b0143b5SDavid Howells ((parent == d_inode(sb->s_root)) || 44912e9b892SDmitry Monakhov (ext4_test_inode_flag(parent, EXT4_INODE_TOPDIR)))) { 450ac27a0ecSDave Kleikamp int best_ndir = inodes_per_group; 4512aa9fc4cSAvantika Mathur int ret = -1; 452ac27a0ecSDave Kleikamp 453f157a4aaSTheodore Ts'o if (qstr) { 454f157a4aaSTheodore Ts'o hinfo.hash_version = DX_HASH_HALF_MD4; 455f157a4aaSTheodore Ts'o hinfo.seed = sbi->s_hash_seed; 456b886ee3eSGabriel Krisman Bertazi ext4fs_dirhash(parent, qstr->name, qstr->len, &hinfo); 457f157a4aaSTheodore Ts'o grp = hinfo.hash; 458f157a4aaSTheodore Ts'o } else 459dd1f723bSTheodore Ts'o grp = prandom_u32(); 4602aa9fc4cSAvantika Mathur parent_group = (unsigned)grp % ngroups; 461ac27a0ecSDave Kleikamp for (i = 0; i < ngroups; i++) { 462a4912123STheodore Ts'o g = (parent_group + i) % ngroups; 463a4912123STheodore Ts'o get_orlov_stats(sb, g, flex_size, &stats); 464a4912123STheodore Ts'o if (!stats.free_inodes) 465ac27a0ecSDave Kleikamp continue; 466a4912123STheodore Ts'o if (stats.used_dirs >= best_ndir) 467ac27a0ecSDave Kleikamp continue; 468a4912123STheodore Ts'o if (stats.free_inodes < avefreei) 469ac27a0ecSDave Kleikamp continue; 47024aaa8efSTheodore Ts'o if (stats.free_clusters < avefreec) 471ac27a0ecSDave Kleikamp continue; 472a4912123STheodore Ts'o grp = g; 4732aa9fc4cSAvantika Mathur ret = 0; 474a4912123STheodore Ts'o best_ndir = stats.used_dirs; 475ac27a0ecSDave Kleikamp } 476a4912123STheodore Ts'o if (ret) 477a4912123STheodore Ts'o goto fallback; 478a4912123STheodore Ts'o found_flex_bg: 479a4912123STheodore Ts'o if (flex_size == 1) { 480a4912123STheodore Ts'o *group = grp; 481a4912123STheodore Ts'o return 0; 482a4912123STheodore Ts'o } 483a4912123STheodore Ts'o 484a4912123STheodore Ts'o /* 485a4912123STheodore Ts'o * We pack inodes at the beginning of the flexgroup's 486a4912123STheodore Ts'o * inode tables. Block allocation decisions will do 487a4912123STheodore Ts'o * something similar, although regular files will 488a4912123STheodore Ts'o * start at 2nd block group of the flexgroup. See 489a4912123STheodore Ts'o * ext4_ext_find_goal() and ext4_find_near(). 490a4912123STheodore Ts'o */ 491a4912123STheodore Ts'o grp *= flex_size; 492a4912123STheodore Ts'o for (i = 0; i < flex_size; i++) { 4938df9675fSTheodore Ts'o if (grp+i >= real_ngroups) 494a4912123STheodore Ts'o break; 495a4912123STheodore Ts'o desc = ext4_get_group_desc(sb, grp+i, NULL); 496a4912123STheodore Ts'o if (desc && ext4_free_inodes_count(sb, desc)) { 497a4912123STheodore Ts'o *group = grp+i; 498a4912123STheodore Ts'o return 0; 499a4912123STheodore Ts'o } 500a4912123STheodore Ts'o } 501ac27a0ecSDave Kleikamp goto fallback; 502ac27a0ecSDave Kleikamp } 503ac27a0ecSDave Kleikamp 504ac27a0ecSDave Kleikamp max_dirs = ndirs / ngroups + inodes_per_group / 16; 505a4912123STheodore Ts'o min_inodes = avefreei - inodes_per_group*flex_size / 4; 506a4912123STheodore Ts'o if (min_inodes < 1) 507a4912123STheodore Ts'o min_inodes = 1; 50824aaa8efSTheodore Ts'o min_clusters = avefreec - EXT4_CLUSTERS_PER_GROUP(sb)*flex_size / 4; 509ac27a0ecSDave Kleikamp 510a4912123STheodore Ts'o /* 511a4912123STheodore Ts'o * Start looking in the flex group where we last allocated an 512a4912123STheodore Ts'o * inode for this parent directory 513a4912123STheodore Ts'o */ 514a4912123STheodore Ts'o if (EXT4_I(parent)->i_last_alloc_group != ~0) { 515a4912123STheodore Ts'o parent_group = EXT4_I(parent)->i_last_alloc_group; 516a4912123STheodore Ts'o if (flex_size > 1) 517a4912123STheodore Ts'o parent_group >>= sbi->s_log_groups_per_flex; 518a4912123STheodore Ts'o } 519ac27a0ecSDave Kleikamp 520ac27a0ecSDave Kleikamp for (i = 0; i < ngroups; i++) { 521a4912123STheodore Ts'o grp = (parent_group + i) % ngroups; 522a4912123STheodore Ts'o get_orlov_stats(sb, grp, flex_size, &stats); 523a4912123STheodore Ts'o if (stats.used_dirs >= max_dirs) 524ac27a0ecSDave Kleikamp continue; 525a4912123STheodore Ts'o if (stats.free_inodes < min_inodes) 526ac27a0ecSDave Kleikamp continue; 52724aaa8efSTheodore Ts'o if (stats.free_clusters < min_clusters) 528ac27a0ecSDave Kleikamp continue; 529a4912123STheodore Ts'o goto found_flex_bg; 530ac27a0ecSDave Kleikamp } 531ac27a0ecSDave Kleikamp 532ac27a0ecSDave Kleikamp fallback: 5338df9675fSTheodore Ts'o ngroups = real_ngroups; 534a4912123STheodore Ts'o avefreei = freei / ngroups; 535b5451f7bSTheodore Ts'o fallback_retry: 536a4912123STheodore Ts'o parent_group = EXT4_I(parent)->i_block_group; 537ac27a0ecSDave Kleikamp for (i = 0; i < ngroups; i++) { 538a4912123STheodore Ts'o grp = (parent_group + i) % ngroups; 539a4912123STheodore Ts'o desc = ext4_get_group_desc(sb, grp, NULL); 540bb3d132aSDan Carpenter if (desc) { 54114c83c9fSTheodore Ts'o grp_free = ext4_free_inodes_count(sb, desc); 542bb3d132aSDan Carpenter if (grp_free && grp_free >= avefreei) { 543a4912123STheodore Ts'o *group = grp; 5442aa9fc4cSAvantika Mathur return 0; 545ac27a0ecSDave Kleikamp } 546a4912123STheodore Ts'o } 547bb3d132aSDan Carpenter } 548ac27a0ecSDave Kleikamp 549ac27a0ecSDave Kleikamp if (avefreei) { 550ac27a0ecSDave Kleikamp /* 551ac27a0ecSDave Kleikamp * The free-inodes counter is approximate, and for really small 552ac27a0ecSDave Kleikamp * filesystems the above test can fail to find any blockgroups 553ac27a0ecSDave Kleikamp */ 554ac27a0ecSDave Kleikamp avefreei = 0; 555b5451f7bSTheodore Ts'o goto fallback_retry; 556ac27a0ecSDave Kleikamp } 557ac27a0ecSDave Kleikamp 558ac27a0ecSDave Kleikamp return -1; 559ac27a0ecSDave Kleikamp } 560ac27a0ecSDave Kleikamp 5612aa9fc4cSAvantika Mathur static int find_group_other(struct super_block *sb, struct inode *parent, 562dcca3fecSAl Viro ext4_group_t *group, umode_t mode) 563ac27a0ecSDave Kleikamp { 564fd2d4291SAvantika Mathur ext4_group_t parent_group = EXT4_I(parent)->i_block_group; 5658df9675fSTheodore Ts'o ext4_group_t i, last, ngroups = ext4_get_groups_count(sb); 566617ba13bSMingming Cao struct ext4_group_desc *desc; 567a4912123STheodore Ts'o int flex_size = ext4_flex_bg_size(EXT4_SB(sb)); 568a4912123STheodore Ts'o 569a4912123STheodore Ts'o /* 570a4912123STheodore Ts'o * Try to place the inode is the same flex group as its 571a4912123STheodore Ts'o * parent. If we can't find space, use the Orlov algorithm to 572a4912123STheodore Ts'o * find another flex group, and store that information in the 573a4912123STheodore Ts'o * parent directory's inode information so that use that flex 574a4912123STheodore Ts'o * group for future allocations. 575a4912123STheodore Ts'o */ 576a4912123STheodore Ts'o if (flex_size > 1) { 577a4912123STheodore Ts'o int retry = 0; 578a4912123STheodore Ts'o 579a4912123STheodore Ts'o try_again: 580a4912123STheodore Ts'o parent_group &= ~(flex_size-1); 581a4912123STheodore Ts'o last = parent_group + flex_size; 582a4912123STheodore Ts'o if (last > ngroups) 583a4912123STheodore Ts'o last = ngroups; 584a4912123STheodore Ts'o for (i = parent_group; i < last; i++) { 585a4912123STheodore Ts'o desc = ext4_get_group_desc(sb, i, NULL); 586a4912123STheodore Ts'o if (desc && ext4_free_inodes_count(sb, desc)) { 587a4912123STheodore Ts'o *group = i; 588a4912123STheodore Ts'o return 0; 589a4912123STheodore Ts'o } 590a4912123STheodore Ts'o } 591a4912123STheodore Ts'o if (!retry && EXT4_I(parent)->i_last_alloc_group != ~0) { 592a4912123STheodore Ts'o retry = 1; 593a4912123STheodore Ts'o parent_group = EXT4_I(parent)->i_last_alloc_group; 594a4912123STheodore Ts'o goto try_again; 595a4912123STheodore Ts'o } 596a4912123STheodore Ts'o /* 597a4912123STheodore Ts'o * If this didn't work, use the Orlov search algorithm 598a4912123STheodore Ts'o * to find a new flex group; we pass in the mode to 599a4912123STheodore Ts'o * avoid the topdir algorithms. 600a4912123STheodore Ts'o */ 601a4912123STheodore Ts'o *group = parent_group + flex_size; 602a4912123STheodore Ts'o if (*group > ngroups) 603a4912123STheodore Ts'o *group = 0; 6047dc57615SPeter Huewe return find_group_orlov(sb, parent, group, mode, NULL); 605a4912123STheodore Ts'o } 606ac27a0ecSDave Kleikamp 607ac27a0ecSDave Kleikamp /* 608ac27a0ecSDave Kleikamp * Try to place the inode in its parent directory 609ac27a0ecSDave Kleikamp */ 6102aa9fc4cSAvantika Mathur *group = parent_group; 6112aa9fc4cSAvantika Mathur desc = ext4_get_group_desc(sb, *group, NULL); 612560671a0SAneesh Kumar K.V if (desc && ext4_free_inodes_count(sb, desc) && 613021b65bbSTheodore Ts'o ext4_free_group_clusters(sb, desc)) 6142aa9fc4cSAvantika Mathur return 0; 615ac27a0ecSDave Kleikamp 616ac27a0ecSDave Kleikamp /* 617ac27a0ecSDave Kleikamp * We're going to place this inode in a different blockgroup from its 618ac27a0ecSDave Kleikamp * parent. We want to cause files in a common directory to all land in 619ac27a0ecSDave Kleikamp * the same blockgroup. But we want files which are in a different 620ac27a0ecSDave Kleikamp * directory which shares a blockgroup with our parent to land in a 621ac27a0ecSDave Kleikamp * different blockgroup. 622ac27a0ecSDave Kleikamp * 623ac27a0ecSDave Kleikamp * So add our directory's i_ino into the starting point for the hash. 624ac27a0ecSDave Kleikamp */ 6252aa9fc4cSAvantika Mathur *group = (*group + parent->i_ino) % ngroups; 626ac27a0ecSDave Kleikamp 627ac27a0ecSDave Kleikamp /* 628ac27a0ecSDave Kleikamp * Use a quadratic hash to find a group with a free inode and some free 629ac27a0ecSDave Kleikamp * blocks. 630ac27a0ecSDave Kleikamp */ 631ac27a0ecSDave Kleikamp for (i = 1; i < ngroups; i <<= 1) { 6322aa9fc4cSAvantika Mathur *group += i; 6332aa9fc4cSAvantika Mathur if (*group >= ngroups) 6342aa9fc4cSAvantika Mathur *group -= ngroups; 6352aa9fc4cSAvantika Mathur desc = ext4_get_group_desc(sb, *group, NULL); 636560671a0SAneesh Kumar K.V if (desc && ext4_free_inodes_count(sb, desc) && 637021b65bbSTheodore Ts'o ext4_free_group_clusters(sb, desc)) 6382aa9fc4cSAvantika Mathur return 0; 639ac27a0ecSDave Kleikamp } 640ac27a0ecSDave Kleikamp 641ac27a0ecSDave Kleikamp /* 642ac27a0ecSDave Kleikamp * That failed: try linear search for a free inode, even if that group 643ac27a0ecSDave Kleikamp * has no free blocks. 644ac27a0ecSDave Kleikamp */ 6452aa9fc4cSAvantika Mathur *group = parent_group; 646ac27a0ecSDave Kleikamp for (i = 0; i < ngroups; i++) { 6472aa9fc4cSAvantika Mathur if (++*group >= ngroups) 6482aa9fc4cSAvantika Mathur *group = 0; 6492aa9fc4cSAvantika Mathur desc = ext4_get_group_desc(sb, *group, NULL); 650560671a0SAneesh Kumar K.V if (desc && ext4_free_inodes_count(sb, desc)) 6512aa9fc4cSAvantika Mathur return 0; 652ac27a0ecSDave Kleikamp } 653ac27a0ecSDave Kleikamp 654ac27a0ecSDave Kleikamp return -1; 655ac27a0ecSDave Kleikamp } 656ac27a0ecSDave Kleikamp 657ac27a0ecSDave Kleikamp /* 65819883bd9STheodore Ts'o * In no journal mode, if an inode has recently been deleted, we want 65919883bd9STheodore Ts'o * to avoid reusing it until we're reasonably sure the inode table 66019883bd9STheodore Ts'o * block has been written back to disk. (Yes, these values are 66119883bd9STheodore Ts'o * somewhat arbitrary...) 66219883bd9STheodore Ts'o */ 66319883bd9STheodore Ts'o #define RECENTCY_MIN 5 664b5f51573SAndreas Dilger #define RECENTCY_DIRTY 300 66519883bd9STheodore Ts'o 66619883bd9STheodore Ts'o static int recently_deleted(struct super_block *sb, ext4_group_t group, int ino) 66719883bd9STheodore Ts'o { 66819883bd9STheodore Ts'o struct ext4_group_desc *gdp; 66919883bd9STheodore Ts'o struct ext4_inode *raw_inode; 67019883bd9STheodore Ts'o struct buffer_head *bh; 67119883bd9STheodore Ts'o int inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 672b5f51573SAndreas Dilger int offset, ret = 0; 673b5f51573SAndreas Dilger int recentcy = RECENTCY_MIN; 674b5f51573SAndreas Dilger u32 dtime, now; 67519883bd9STheodore Ts'o 67619883bd9STheodore Ts'o gdp = ext4_get_group_desc(sb, group, NULL); 67719883bd9STheodore Ts'o if (unlikely(!gdp)) 67819883bd9STheodore Ts'o return 0; 67919883bd9STheodore Ts'o 6804f9d956dSJan Kara bh = sb_find_get_block(sb, ext4_inode_table(sb, gdp) + 68119883bd9STheodore Ts'o (ino / inodes_per_block)); 6824f9d956dSJan Kara if (!bh || !buffer_uptodate(bh)) 68319883bd9STheodore Ts'o /* 68419883bd9STheodore Ts'o * If the block is not in the buffer cache, then it 68519883bd9STheodore Ts'o * must have been written out. 68619883bd9STheodore Ts'o */ 68719883bd9STheodore Ts'o goto out; 68819883bd9STheodore Ts'o 68919883bd9STheodore Ts'o offset = (ino % inodes_per_block) * EXT4_INODE_SIZE(sb); 69019883bd9STheodore Ts'o raw_inode = (struct ext4_inode *) (bh->b_data + offset); 691b5f51573SAndreas Dilger 692b5f51573SAndreas Dilger /* i_dtime is only 32 bits on disk, but we only care about relative 693b5f51573SAndreas Dilger * times in the range of a few minutes (i.e. long enough to sync a 694b5f51573SAndreas Dilger * recently-deleted inode to disk), so using the low 32 bits of the 695b5f51573SAndreas Dilger * clock (a 68 year range) is enough, see time_before32() */ 69619883bd9STheodore Ts'o dtime = le32_to_cpu(raw_inode->i_dtime); 697b5f51573SAndreas Dilger now = ktime_get_real_seconds(); 69819883bd9STheodore Ts'o if (buffer_dirty(bh)) 69919883bd9STheodore Ts'o recentcy += RECENTCY_DIRTY; 70019883bd9STheodore Ts'o 701b5f51573SAndreas Dilger if (dtime && time_before32(dtime, now) && 702b5f51573SAndreas Dilger time_before32(now, dtime + recentcy)) 70319883bd9STheodore Ts'o ret = 1; 70419883bd9STheodore Ts'o out: 70519883bd9STheodore Ts'o brelse(bh); 70619883bd9STheodore Ts'o return ret; 70719883bd9STheodore Ts'o } 70819883bd9STheodore Ts'o 709901ed070SWang Shilong static int find_inode_bit(struct super_block *sb, ext4_group_t group, 710901ed070SWang Shilong struct buffer_head *bitmap, unsigned long *ino) 711901ed070SWang Shilong { 712901ed070SWang Shilong next: 713901ed070SWang Shilong *ino = ext4_find_next_zero_bit((unsigned long *) 714901ed070SWang Shilong bitmap->b_data, 715901ed070SWang Shilong EXT4_INODES_PER_GROUP(sb), *ino); 716901ed070SWang Shilong if (*ino >= EXT4_INODES_PER_GROUP(sb)) 717901ed070SWang Shilong return 0; 718901ed070SWang Shilong 719901ed070SWang Shilong if ((EXT4_SB(sb)->s_journal == NULL) && 720901ed070SWang Shilong recently_deleted(sb, group, *ino)) { 721901ed070SWang Shilong *ino = *ino + 1; 722901ed070SWang Shilong if (*ino < EXT4_INODES_PER_GROUP(sb)) 723901ed070SWang Shilong goto next; 724901ed070SWang Shilong return 0; 725901ed070SWang Shilong } 726901ed070SWang Shilong 727901ed070SWang Shilong return 1; 728901ed070SWang Shilong } 729901ed070SWang Shilong 73019883bd9STheodore Ts'o /* 731ac27a0ecSDave Kleikamp * There are two policies for allocating an inode. If the new inode is 732ac27a0ecSDave Kleikamp * a directory, then a forward search is made for a block group with both 733ac27a0ecSDave Kleikamp * free space and a low directory-to-inode ratio; if that fails, then of 734ac27a0ecSDave Kleikamp * the groups with above-average free space, that group with the fewest 735ac27a0ecSDave Kleikamp * directories already is chosen. 736ac27a0ecSDave Kleikamp * 737ac27a0ecSDave Kleikamp * For other inodes, search forward from the parent directory's block 738ac27a0ecSDave Kleikamp * group to find a free inode. 739ac27a0ecSDave Kleikamp */ 7401139575aSTheodore Ts'o struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir, 7411139575aSTheodore Ts'o umode_t mode, const struct qstr *qstr, 7421b917ed8STahsin Erdogan __u32 goal, uid_t *owner, __u32 i_flags, 7431b917ed8STahsin Erdogan int handle_type, unsigned int line_no, 7441b917ed8STahsin Erdogan int nblocks) 745ac27a0ecSDave Kleikamp { 746ac27a0ecSDave Kleikamp struct super_block *sb; 7473300bedaSAneesh Kumar K.V struct buffer_head *inode_bitmap_bh = NULL; 7483300bedaSAneesh Kumar K.V struct buffer_head *group_desc_bh; 7498df9675fSTheodore Ts'o ext4_group_t ngroups, group = 0; 750ac27a0ecSDave Kleikamp unsigned long ino = 0; 751ac27a0ecSDave Kleikamp struct inode *inode; 752617ba13bSMingming Cao struct ext4_group_desc *gdp = NULL; 753617ba13bSMingming Cao struct ext4_inode_info *ei; 754617ba13bSMingming Cao struct ext4_sb_info *sbi; 755a7cdadeeSJan Kara int ret2, err; 756ac27a0ecSDave Kleikamp struct inode *ret; 7572aa9fc4cSAvantika Mathur ext4_group_t i; 758772cb7c8SJose R. Santos ext4_group_t flex_group; 75987a39389SDarrick J. Wong struct ext4_group_info *grp; 760e709e9dfSTheodore Ts'o int encrypt = 0; 761ac27a0ecSDave Kleikamp 762ac27a0ecSDave Kleikamp /* Cannot create files in a deleted directory */ 763ac27a0ecSDave Kleikamp if (!dir || !dir->i_nlink) 764ac27a0ecSDave Kleikamp return ERR_PTR(-EPERM); 765ac27a0ecSDave Kleikamp 766af65207cSTahsin Erdogan sb = dir->i_sb; 767af65207cSTahsin Erdogan sbi = EXT4_SB(sb); 768af65207cSTahsin Erdogan 769af65207cSTahsin Erdogan if (unlikely(ext4_forced_shutdown(sbi))) 7700db1ff22STheodore Ts'o return ERR_PTR(-EIO); 7710db1ff22STheodore Ts'o 772592ddec7SChandan Rajendra if ((IS_ENCRYPTED(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) && 773ad47f953STahsin Erdogan (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) && 774ad47f953STahsin Erdogan !(i_flags & EXT4_EA_INODE_FL)) { 775a7550b30SJaegeuk Kim err = fscrypt_get_encryption_info(dir); 776e709e9dfSTheodore Ts'o if (err) 777e709e9dfSTheodore Ts'o return ERR_PTR(err); 778a7550b30SJaegeuk Kim if (!fscrypt_has_encryption_key(dir)) 77954475f53SEric Biggers return ERR_PTR(-ENOKEY); 780e709e9dfSTheodore Ts'o encrypt = 1; 781e709e9dfSTheodore Ts'o } 782e709e9dfSTheodore Ts'o 783af65207cSTahsin Erdogan if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) { 784af65207cSTahsin Erdogan #ifdef CONFIG_EXT4_FS_POSIX_ACL 785af65207cSTahsin Erdogan struct posix_acl *p = get_acl(dir, ACL_TYPE_DEFAULT); 786af65207cSTahsin Erdogan 787996fc447STheodore Ts'o if (IS_ERR(p)) 788996fc447STheodore Ts'o return ERR_CAST(p); 789af65207cSTahsin Erdogan if (p) { 790af65207cSTahsin Erdogan int acl_size = p->a_count * sizeof(ext4_acl_entry); 791af65207cSTahsin Erdogan 792af65207cSTahsin Erdogan nblocks += (S_ISDIR(mode) ? 2 : 1) * 793af65207cSTahsin Erdogan __ext4_xattr_set_credits(sb, NULL /* inode */, 794af65207cSTahsin Erdogan NULL /* block_bh */, acl_size, 795af65207cSTahsin Erdogan true /* is_create */); 796af65207cSTahsin Erdogan posix_acl_release(p); 797af65207cSTahsin Erdogan } 798af65207cSTahsin Erdogan #endif 799af65207cSTahsin Erdogan 800af65207cSTahsin Erdogan #ifdef CONFIG_SECURITY 801af65207cSTahsin Erdogan { 802af65207cSTahsin Erdogan int num_security_xattrs = 1; 803af65207cSTahsin Erdogan 804af65207cSTahsin Erdogan #ifdef CONFIG_INTEGRITY 805af65207cSTahsin Erdogan num_security_xattrs++; 806af65207cSTahsin Erdogan #endif 807af65207cSTahsin Erdogan /* 808af65207cSTahsin Erdogan * We assume that security xattrs are never 809af65207cSTahsin Erdogan * more than 1k. In practice they are under 810af65207cSTahsin Erdogan * 128 bytes. 811af65207cSTahsin Erdogan */ 812af65207cSTahsin Erdogan nblocks += num_security_xattrs * 813af65207cSTahsin Erdogan __ext4_xattr_set_credits(sb, NULL /* inode */, 814af65207cSTahsin Erdogan NULL /* block_bh */, 1024, 815af65207cSTahsin Erdogan true /* is_create */); 816af65207cSTahsin Erdogan } 817af65207cSTahsin Erdogan #endif 818af65207cSTahsin Erdogan if (encrypt) 819af65207cSTahsin Erdogan nblocks += __ext4_xattr_set_credits(sb, 820af65207cSTahsin Erdogan NULL /* inode */, NULL /* block_bh */, 821af65207cSTahsin Erdogan FSCRYPT_SET_CONTEXT_MAX_SIZE, 822af65207cSTahsin Erdogan true /* is_create */); 823af65207cSTahsin Erdogan } 824af65207cSTahsin Erdogan 8258df9675fSTheodore Ts'o ngroups = ext4_get_groups_count(sb); 8269bffad1eSTheodore Ts'o trace_ext4_request_inode(dir, mode); 827ac27a0ecSDave Kleikamp inode = new_inode(sb); 828ac27a0ecSDave Kleikamp if (!inode) 829ac27a0ecSDave Kleikamp return ERR_PTR(-ENOMEM); 830617ba13bSMingming Cao ei = EXT4_I(inode); 831772cb7c8SJose R. Santos 832eb9cc7e1SJan Kara /* 833b8a07463SAdam Buchbinder * Initialize owners and quota early so that we don't have to account 834eb9cc7e1SJan Kara * for quota initialization worst case in standard inode creating 835eb9cc7e1SJan Kara * transaction 836eb9cc7e1SJan Kara */ 837eb9cc7e1SJan Kara if (owner) { 838eb9cc7e1SJan Kara inode->i_mode = mode; 839eb9cc7e1SJan Kara i_uid_write(inode, owner[0]); 840eb9cc7e1SJan Kara i_gid_write(inode, owner[1]); 841eb9cc7e1SJan Kara } else if (test_opt(sb, GRPID)) { 842eb9cc7e1SJan Kara inode->i_mode = mode; 843eb9cc7e1SJan Kara inode->i_uid = current_fsuid(); 844eb9cc7e1SJan Kara inode->i_gid = dir->i_gid; 845eb9cc7e1SJan Kara } else 846eb9cc7e1SJan Kara inode_init_owner(inode, dir, mode); 847040cb378SLi Xi 8480b7b7779SKaho Ng if (ext4_has_feature_project(sb) && 849040cb378SLi Xi ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT)) 850040cb378SLi Xi ei->i_projid = EXT4_I(dir)->i_projid; 851040cb378SLi Xi else 852040cb378SLi Xi ei->i_projid = make_kprojid(&init_user_ns, EXT4_DEF_PROJID); 853040cb378SLi Xi 854a7cdadeeSJan Kara err = dquot_initialize(inode); 855a7cdadeeSJan Kara if (err) 856a7cdadeeSJan Kara goto out; 857eb9cc7e1SJan Kara 85811013911SAndreas Dilger if (!goal) 85911013911SAndreas Dilger goal = sbi->s_inode_goal; 86011013911SAndreas Dilger 861e6462869SJohann Lombardi if (goal && goal <= le32_to_cpu(sbi->s_es->s_inodes_count)) { 86211013911SAndreas Dilger group = (goal - 1) / EXT4_INODES_PER_GROUP(sb); 86311013911SAndreas Dilger ino = (goal - 1) % EXT4_INODES_PER_GROUP(sb); 86411013911SAndreas Dilger ret2 = 0; 86511013911SAndreas Dilger goto got_group; 86611013911SAndreas Dilger } 86711013911SAndreas Dilger 8684113c4caSLukas Czerner if (S_ISDIR(mode)) 869f157a4aaSTheodore Ts'o ret2 = find_group_orlov(sb, dir, &group, mode, qstr); 8704113c4caSLukas Czerner else 871a4912123STheodore Ts'o ret2 = find_group_other(sb, dir, &group, mode); 872ac27a0ecSDave Kleikamp 873772cb7c8SJose R. Santos got_group: 874a4912123STheodore Ts'o EXT4_I(dir)->i_last_alloc_group = group; 875ac27a0ecSDave Kleikamp err = -ENOSPC; 8762aa9fc4cSAvantika Mathur if (ret2 == -1) 877ac27a0ecSDave Kleikamp goto out; 878ac27a0ecSDave Kleikamp 879119c0d44STheodore Ts'o /* 880119c0d44STheodore Ts'o * Normally we will only go through one pass of this loop, 881119c0d44STheodore Ts'o * unless we get unlucky and it turns out the group we selected 882119c0d44STheodore Ts'o * had its last inode grabbed by someone else. 883119c0d44STheodore Ts'o */ 88411013911SAndreas Dilger for (i = 0; i < ngroups; i++, ino = 0) { 885ac27a0ecSDave Kleikamp err = -EIO; 886ac27a0ecSDave Kleikamp 8873300bedaSAneesh Kumar K.V gdp = ext4_get_group_desc(sb, group, &group_desc_bh); 888ac27a0ecSDave Kleikamp if (!gdp) 889eb9cc7e1SJan Kara goto out; 890ac27a0ecSDave Kleikamp 891f2a09af6SYongqiang Yang /* 892f2a09af6SYongqiang Yang * Check free inodes count before loading bitmap. 893f2a09af6SYongqiang Yang */ 8942fe435d8SWang Shilong if (ext4_free_inodes_count(sb, gdp) == 0) 8952fe435d8SWang Shilong goto next_group; 896f2a09af6SYongqiang Yang 89787a39389SDarrick J. Wong grp = ext4_get_group_info(sb, group); 89887a39389SDarrick J. Wong /* Skip groups with already-known suspicious inode tables */ 8992fe435d8SWang Shilong if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) 9002fe435d8SWang Shilong goto next_group; 90187a39389SDarrick J. Wong 9023300bedaSAneesh Kumar K.V brelse(inode_bitmap_bh); 9033300bedaSAneesh Kumar K.V inode_bitmap_bh = ext4_read_inode_bitmap(sb, group); 90487a39389SDarrick J. Wong /* Skip groups with suspicious inode tables */ 9059008a58eSDarrick J. Wong if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp) || 9069008a58eSDarrick J. Wong IS_ERR(inode_bitmap_bh)) { 9079008a58eSDarrick J. Wong inode_bitmap_bh = NULL; 9082fe435d8SWang Shilong goto next_group; 90987a39389SDarrick J. Wong } 910ac27a0ecSDave Kleikamp 911ac27a0ecSDave Kleikamp repeat_in_this_group: 912901ed070SWang Shilong ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino); 913901ed070SWang Shilong if (!ret2) 914a34eb503STheodore Ts'o goto next_group; 915901ed070SWang Shilong 916119c0d44STheodore Ts'o if (group == 0 && (ino + 1) < EXT4_FIRST_INO(sb)) { 917119c0d44STheodore Ts'o ext4_error(sb, "reserved inode found cleared - " 918119c0d44STheodore Ts'o "inode=%lu", ino + 1); 919206f6d55SWang Shilong ext4_mark_group_bitmap_corrupted(sb, group, 920206f6d55SWang Shilong EXT4_GROUP_INFO_IBITMAP_CORRUPT); 9212fe435d8SWang Shilong goto next_group; 922119c0d44STheodore Ts'o } 923901ed070SWang Shilong 9241139575aSTheodore Ts'o if (!handle) { 9251139575aSTheodore Ts'o BUG_ON(nblocks <= 0); 9261139575aSTheodore Ts'o handle = __ext4_journal_start_sb(dir->i_sb, line_no, 927a70fd5acSyangerkun handle_type, nblocks, 0, 928a70fd5acSyangerkun ext4_trans_default_revoke_credits(sb)); 9291139575aSTheodore Ts'o if (IS_ERR(handle)) { 9301139575aSTheodore Ts'o err = PTR_ERR(handle); 931eb9cc7e1SJan Kara ext4_std_error(sb, err); 932eb9cc7e1SJan Kara goto out; 9331139575aSTheodore Ts'o } 9341139575aSTheodore Ts'o } 935ffb5387eSEric Sandeen BUFFER_TRACE(inode_bitmap_bh, "get_write_access"); 936ffb5387eSEric Sandeen err = ext4_journal_get_write_access(handle, inode_bitmap_bh); 937eb9cc7e1SJan Kara if (err) { 938eb9cc7e1SJan Kara ext4_std_error(sb, err); 939eb9cc7e1SJan Kara goto out; 940eb9cc7e1SJan Kara } 941119c0d44STheodore Ts'o ext4_lock_group(sb, group); 942119c0d44STheodore Ts'o ret2 = ext4_test_and_set_bit(ino, inode_bitmap_bh->b_data); 943901ed070SWang Shilong if (ret2) { 944901ed070SWang Shilong /* Someone already took the bit. Repeat the search 945901ed070SWang Shilong * with lock held. 946901ed070SWang Shilong */ 947901ed070SWang Shilong ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino); 948901ed070SWang Shilong if (ret2) { 949901ed070SWang Shilong ext4_set_bit(ino, inode_bitmap_bh->b_data); 950901ed070SWang Shilong ret2 = 0; 951901ed070SWang Shilong } else { 952901ed070SWang Shilong ret2 = 1; /* we didn't grab the inode */ 953901ed070SWang Shilong } 954901ed070SWang Shilong } 955119c0d44STheodore Ts'o ext4_unlock_group(sb, group); 956119c0d44STheodore Ts'o ino++; /* the inode bitmap is zero-based */ 957119c0d44STheodore Ts'o if (!ret2) 958119c0d44STheodore Ts'o goto got; /* we grabbed the inode! */ 959901ed070SWang Shilong 960119c0d44STheodore Ts'o if (ino < EXT4_INODES_PER_GROUP(sb)) 961119c0d44STheodore Ts'o goto repeat_in_this_group; 962a34eb503STheodore Ts'o next_group: 963a34eb503STheodore Ts'o if (++group == ngroups) 964a34eb503STheodore Ts'o group = 0; 965ac27a0ecSDave Kleikamp } 966ac27a0ecSDave Kleikamp err = -ENOSPC; 967ac27a0ecSDave Kleikamp goto out; 968ac27a0ecSDave Kleikamp 969ac27a0ecSDave Kleikamp got: 970ffb5387eSEric Sandeen BUFFER_TRACE(inode_bitmap_bh, "call ext4_handle_dirty_metadata"); 971ffb5387eSEric Sandeen err = ext4_handle_dirty_metadata(handle, NULL, inode_bitmap_bh); 972eb9cc7e1SJan Kara if (err) { 973eb9cc7e1SJan Kara ext4_std_error(sb, err); 974eb9cc7e1SJan Kara goto out; 975eb9cc7e1SJan Kara } 976ffb5387eSEric Sandeen 97761c219f5STheodore Ts'o BUFFER_TRACE(group_desc_bh, "get_write_access"); 97861c219f5STheodore Ts'o err = ext4_journal_get_write_access(handle, group_desc_bh); 97961c219f5STheodore Ts'o if (err) { 98061c219f5STheodore Ts'o ext4_std_error(sb, err); 98161c219f5STheodore Ts'o goto out; 98261c219f5STheodore Ts'o } 98361c219f5STheodore Ts'o 984717d50e4SAndreas Dilger /* We may have to initialize the block bitmap if it isn't already */ 985feb0ab32SDarrick J. Wong if (ext4_has_group_desc_csum(sb) && 986717d50e4SAndreas Dilger gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { 9873300bedaSAneesh Kumar K.V struct buffer_head *block_bitmap_bh; 988717d50e4SAndreas Dilger 9893300bedaSAneesh Kumar K.V block_bitmap_bh = ext4_read_block_bitmap(sb, group); 9909008a58eSDarrick J. Wong if (IS_ERR(block_bitmap_bh)) { 9919008a58eSDarrick J. Wong err = PTR_ERR(block_bitmap_bh); 992599a9b77SJan Kara goto out; 993599a9b77SJan Kara } 9943300bedaSAneesh Kumar K.V BUFFER_TRACE(block_bitmap_bh, "get block bitmap access"); 9953300bedaSAneesh Kumar K.V err = ext4_journal_get_write_access(handle, block_bitmap_bh); 996717d50e4SAndreas Dilger if (err) { 9973300bedaSAneesh Kumar K.V brelse(block_bitmap_bh); 998eb9cc7e1SJan Kara ext4_std_error(sb, err); 999eb9cc7e1SJan Kara goto out; 1000717d50e4SAndreas Dilger } 1001717d50e4SAndreas Dilger 1002fd034a84STheodore Ts'o BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap"); 1003fd034a84STheodore Ts'o err = ext4_handle_dirty_metadata(handle, NULL, block_bitmap_bh); 1004fd034a84STheodore Ts'o 1005717d50e4SAndreas Dilger /* recheck and clear flag under lock if we still need to */ 1006fd034a84STheodore Ts'o ext4_lock_group(sb, group); 10078844618dSTheodore Ts'o if (ext4_has_group_desc_csum(sb) && 10088844618dSTheodore Ts'o (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) { 10093300bedaSAneesh Kumar K.V gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT); 1010021b65bbSTheodore Ts'o ext4_free_group_clusters_set(sb, gdp, 1011cff1dfd7STheodore Ts'o ext4_free_clusters_after_init(sb, group, gdp)); 1012fa77dcfaSDarrick J. Wong ext4_block_bitmap_csum_set(sb, group, gdp, 101379f1ba49STao Ma block_bitmap_bh); 1014feb0ab32SDarrick J. Wong ext4_group_desc_csum_set(sb, group, gdp); 1015717d50e4SAndreas Dilger } 1016955ce5f5SAneesh Kumar K.V ext4_unlock_group(sb, group); 1017aeb1e5d6STheodore Ts'o brelse(block_bitmap_bh); 1018717d50e4SAndreas Dilger 1019eb9cc7e1SJan Kara if (err) { 1020eb9cc7e1SJan Kara ext4_std_error(sb, err); 1021eb9cc7e1SJan Kara goto out; 1022eb9cc7e1SJan Kara } 1023717d50e4SAndreas Dilger } 1024119c0d44STheodore Ts'o 1025119c0d44STheodore Ts'o /* Update the relevant bg descriptor fields */ 102641a246d1SDarrick J. Wong if (ext4_has_group_desc_csum(sb)) { 1027119c0d44STheodore Ts'o int free; 1028119c0d44STheodore Ts'o struct ext4_group_info *grp = ext4_get_group_info(sb, group); 1029119c0d44STheodore Ts'o 1030119c0d44STheodore Ts'o down_read(&grp->alloc_sem); /* protect vs itable lazyinit */ 1031119c0d44STheodore Ts'o ext4_lock_group(sb, group); /* while we modify the bg desc */ 1032119c0d44STheodore Ts'o free = EXT4_INODES_PER_GROUP(sb) - 1033119c0d44STheodore Ts'o ext4_itable_unused_count(sb, gdp); 1034119c0d44STheodore Ts'o if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) { 1035119c0d44STheodore Ts'o gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT); 1036119c0d44STheodore Ts'o free = 0; 1037119c0d44STheodore Ts'o } 1038119c0d44STheodore Ts'o /* 1039119c0d44STheodore Ts'o * Check the relative inode number against the last used 1040119c0d44STheodore Ts'o * relative inode number in this group. if it is greater 1041119c0d44STheodore Ts'o * we need to update the bg_itable_unused count 1042119c0d44STheodore Ts'o */ 1043119c0d44STheodore Ts'o if (ino > free) 1044119c0d44STheodore Ts'o ext4_itable_unused_set(sb, gdp, 1045119c0d44STheodore Ts'o (EXT4_INODES_PER_GROUP(sb) - ino)); 1046119c0d44STheodore Ts'o up_read(&grp->alloc_sem); 10476f2e9f0eSTao Ma } else { 10486f2e9f0eSTao Ma ext4_lock_group(sb, group); 1049119c0d44STheodore Ts'o } 10506f2e9f0eSTao Ma 1051119c0d44STheodore Ts'o ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1); 1052119c0d44STheodore Ts'o if (S_ISDIR(mode)) { 1053119c0d44STheodore Ts'o ext4_used_dirs_set(sb, gdp, ext4_used_dirs_count(sb, gdp) + 1); 1054119c0d44STheodore Ts'o if (sbi->s_log_groups_per_flex) { 1055119c0d44STheodore Ts'o ext4_group_t f = ext4_flex_group(sbi, group); 1056119c0d44STheodore Ts'o 1057119c0d44STheodore Ts'o atomic_inc(&sbi->s_flex_groups[f].used_dirs); 1058119c0d44STheodore Ts'o } 1059119c0d44STheodore Ts'o } 106041a246d1SDarrick J. Wong if (ext4_has_group_desc_csum(sb)) { 106141a246d1SDarrick J. Wong ext4_inode_bitmap_csum_set(sb, group, gdp, inode_bitmap_bh, 106241a246d1SDarrick J. Wong EXT4_INODES_PER_GROUP(sb) / 8); 1063feb0ab32SDarrick J. Wong ext4_group_desc_csum_set(sb, group, gdp); 1064119c0d44STheodore Ts'o } 10656f2e9f0eSTao Ma ext4_unlock_group(sb, group); 1066119c0d44STheodore Ts'o 10673300bedaSAneesh Kumar K.V BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata"); 10683300bedaSAneesh Kumar K.V err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh); 1069eb9cc7e1SJan Kara if (err) { 1070eb9cc7e1SJan Kara ext4_std_error(sb, err); 1071eb9cc7e1SJan Kara goto out; 1072eb9cc7e1SJan Kara } 1073ac27a0ecSDave Kleikamp 1074ac27a0ecSDave Kleikamp percpu_counter_dec(&sbi->s_freeinodes_counter); 1075ac27a0ecSDave Kleikamp if (S_ISDIR(mode)) 1076ac27a0ecSDave Kleikamp percpu_counter_inc(&sbi->s_dirs_counter); 1077ac27a0ecSDave Kleikamp 1078772cb7c8SJose R. Santos if (sbi->s_log_groups_per_flex) { 1079772cb7c8SJose R. Santos flex_group = ext4_flex_group(sbi, group); 10809f24e420STheodore Ts'o atomic_dec(&sbi->s_flex_groups[flex_group].free_inodes); 1081772cb7c8SJose R. Santos } 1082ac27a0ecSDave Kleikamp 1083717d50e4SAndreas Dilger inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb); 1084ac27a0ecSDave Kleikamp /* This is the optimal IO size (for stat), not the fs block size */ 1085ac27a0ecSDave Kleikamp inode->i_blocks = 0; 108695582b00SDeepa Dinamani inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode); 10877b62b293SArnd Bergmann ei->i_crtime = inode->i_mtime; 1088ac27a0ecSDave Kleikamp 1089ac27a0ecSDave Kleikamp memset(ei->i_data, 0, sizeof(ei->i_data)); 1090ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 1091ac27a0ecSDave Kleikamp ei->i_disksize = 0; 1092ac27a0ecSDave Kleikamp 10934af83508SEryu Guan /* Don't inherit extent flag from directory, amongst others. */ 10942dc6b0d4SDuane Griffin ei->i_flags = 10952dc6b0d4SDuane Griffin ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED); 10961b917ed8STahsin Erdogan ei->i_flags |= i_flags; 1097ac27a0ecSDave Kleikamp ei->i_file_acl = 0; 1098ac27a0ecSDave Kleikamp ei->i_dtime = 0; 1099ac27a0ecSDave Kleikamp ei->i_block_group = group; 1100a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 1101ac27a0ecSDave Kleikamp 1102617ba13bSMingming Cao ext4_set_inode_flags(inode); 1103ac27a0ecSDave Kleikamp if (IS_DIRSYNC(inode)) 11040390131bSFrank Mayhar ext4_handle_sync(handle); 11056b38e842SAl Viro if (insert_inode_locked(inode) < 0) { 1106acd6ad83SJan Kara /* 1107acd6ad83SJan Kara * Likely a bitmap corruption causing inode to be allocated 1108acd6ad83SJan Kara * twice. 1109acd6ad83SJan Kara */ 1110acd6ad83SJan Kara err = -EIO; 1111eb9cc7e1SJan Kara ext4_error(sb, "failed to insert inode %lu: doubly allocated?", 1112eb9cc7e1SJan Kara inode->i_ino); 1113206f6d55SWang Shilong ext4_mark_group_bitmap_corrupted(sb, group, 1114206f6d55SWang Shilong EXT4_GROUP_INFO_IBITMAP_CORRUPT); 1115eb9cc7e1SJan Kara goto out; 11166b38e842SAl Viro } 111723253068STheodore Ts'o inode->i_generation = prandom_u32(); 1118ac27a0ecSDave Kleikamp 1119814525f4SDarrick J. Wong /* Precompute checksum seed for inode metadata */ 11209aa5d32bSDmitry Monakhov if (ext4_has_metadata_csum(sb)) { 1121814525f4SDarrick J. Wong __u32 csum; 1122814525f4SDarrick J. Wong __le32 inum = cpu_to_le32(inode->i_ino); 1123814525f4SDarrick J. Wong __le32 gen = cpu_to_le32(inode->i_generation); 1124814525f4SDarrick J. Wong csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, 1125814525f4SDarrick J. Wong sizeof(inum)); 1126814525f4SDarrick J. Wong ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, 1127814525f4SDarrick J. Wong sizeof(gen)); 1128814525f4SDarrick J. Wong } 1129814525f4SDarrick J. Wong 1130353eb83cSTheodore Ts'o ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ 113119f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_NEW); 1132ef7f3835SKalpak Shah 113349598e04SJun Piao ei->i_extra_isize = sbi->s_want_extra_isize; 1134f08225d1STao Ma ei->i_inline_off = 0; 1135e2b911c5SDarrick J. Wong if (ext4_has_feature_inline_data(sb)) 1136f08225d1STao Ma ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); 1137ac27a0ecSDave Kleikamp ret = inode; 113863936ddaSChristoph Hellwig err = dquot_alloc_inode(inode); 113963936ddaSChristoph Hellwig if (err) 1140ac27a0ecSDave Kleikamp goto fail_drop; 1141ac27a0ecSDave Kleikamp 1142aa1dca3bSEric Biggers /* 1143aa1dca3bSEric Biggers * Since the encryption xattr will always be unique, create it first so 1144aa1dca3bSEric Biggers * that it's less likely to end up in an external xattr block and 1145aa1dca3bSEric Biggers * prevent its deduplication. 1146aa1dca3bSEric Biggers */ 1147aa1dca3bSEric Biggers if (encrypt) { 1148aa1dca3bSEric Biggers err = fscrypt_inherit_context(dir, inode, handle, true); 1149aa1dca3bSEric Biggers if (err) 1150aa1dca3bSEric Biggers goto fail_free_drop; 1151aa1dca3bSEric Biggers } 1152aa1dca3bSEric Biggers 11531b917ed8STahsin Erdogan if (!(ei->i_flags & EXT4_EA_INODE_FL)) { 1154617ba13bSMingming Cao err = ext4_init_acl(handle, inode, dir); 1155ac27a0ecSDave Kleikamp if (err) 1156ac27a0ecSDave Kleikamp goto fail_free_drop; 1157ac27a0ecSDave Kleikamp 11582a7dba39SEric Paris err = ext4_init_security(handle, inode, dir, qstr); 1159ac27a0ecSDave Kleikamp if (err) 1160ac27a0ecSDave Kleikamp goto fail_free_drop; 1161ad47f953STahsin Erdogan } 1162ac27a0ecSDave Kleikamp 1163e2b911c5SDarrick J. Wong if (ext4_has_feature_extents(sb)) { 1164e4079a11SEric Sandeen /* set extent flag only for directory, file and normal symlink*/ 1165e65187e6SAneesh Kumar K.V if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) { 116612e9b892SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS); 1167a86c6181SAlex Tomas ext4_ext_tree_init(handle, inode); 1168a86c6181SAlex Tomas } 116942bf0383SAneesh Kumar K.V } 1170ac27a0ecSDave Kleikamp 1171688f869cSTheodore Ts'o if (ext4_handle_valid(handle)) { 1172688f869cSTheodore Ts'o ei->i_sync_tid = handle->h_transaction->t_tid; 1173688f869cSTheodore Ts'o ei->i_datasync_tid = handle->h_transaction->t_tid; 1174688f869cSTheodore Ts'o } 1175688f869cSTheodore Ts'o 11768753e88fSAneesh Kumar K.V err = ext4_mark_inode_dirty(handle, inode); 11778753e88fSAneesh Kumar K.V if (err) { 11788753e88fSAneesh Kumar K.V ext4_std_error(sb, err); 11798753e88fSAneesh Kumar K.V goto fail_free_drop; 11808753e88fSAneesh Kumar K.V } 11818753e88fSAneesh Kumar K.V 1182617ba13bSMingming Cao ext4_debug("allocating inode %lu\n", inode->i_ino); 11839bffad1eSTheodore Ts'o trace_ext4_allocate_inode(inode, dir, mode); 11843300bedaSAneesh Kumar K.V brelse(inode_bitmap_bh); 1185ac27a0ecSDave Kleikamp return ret; 1186ac27a0ecSDave Kleikamp 1187ac27a0ecSDave Kleikamp fail_free_drop: 118863936ddaSChristoph Hellwig dquot_free_inode(inode); 1189ac27a0ecSDave Kleikamp fail_drop: 11906d6b77f1SMiklos Szeredi clear_nlink(inode); 11916b38e842SAl Viro unlock_new_inode(inode); 1192eb9cc7e1SJan Kara out: 1193eb9cc7e1SJan Kara dquot_drop(inode); 1194eb9cc7e1SJan Kara inode->i_flags |= S_NOQUOTA; 1195ac27a0ecSDave Kleikamp iput(inode); 11963300bedaSAneesh Kumar K.V brelse(inode_bitmap_bh); 1197ac27a0ecSDave Kleikamp return ERR_PTR(err); 1198ac27a0ecSDave Kleikamp } 1199ac27a0ecSDave Kleikamp 1200ac27a0ecSDave Kleikamp /* Verify that we are loading a valid orphan from disk */ 1201617ba13bSMingming Cao struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino) 1202ac27a0ecSDave Kleikamp { 1203617ba13bSMingming Cao unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count); 1204fd2d4291SAvantika Mathur ext4_group_t block_group; 1205ac27a0ecSDave Kleikamp int bit; 12067827a7f6STheodore Ts'o struct buffer_head *bitmap_bh = NULL; 1207ac27a0ecSDave Kleikamp struct inode *inode = NULL; 12087827a7f6STheodore Ts'o int err = -EFSCORRUPTED; 1209ac27a0ecSDave Kleikamp 12107827a7f6STheodore Ts'o if (ino < EXT4_FIRST_INO(sb) || ino > max_ino) 12117827a7f6STheodore Ts'o goto bad_orphan; 1212ac27a0ecSDave Kleikamp 1213617ba13bSMingming Cao block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); 1214617ba13bSMingming Cao bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); 1215e29d1cdeSEric Sandeen bitmap_bh = ext4_read_inode_bitmap(sb, block_group); 1216206f6d55SWang Shilong if (IS_ERR(bitmap_bh)) 1217e884bce1SAl Viro return ERR_CAST(bitmap_bh); 1218ac27a0ecSDave Kleikamp 1219ac27a0ecSDave Kleikamp /* Having the inode bit set should be a 100% indicator that this 1220ac27a0ecSDave Kleikamp * is a valid orphan (no e2fsck run on fs). Orphans also include 1221ac27a0ecSDave Kleikamp * inodes that were being truncated, so we can't check i_nlink==0. 1222ac27a0ecSDave Kleikamp */ 12231d1fe1eeSDavid Howells if (!ext4_test_bit(bit, bitmap_bh->b_data)) 12241d1fe1eeSDavid Howells goto bad_orphan; 12251d1fe1eeSDavid Howells 12268a363970STheodore Ts'o inode = ext4_iget(sb, ino, EXT4_IGET_NORMAL); 12277827a7f6STheodore Ts'o if (IS_ERR(inode)) { 12287827a7f6STheodore Ts'o err = PTR_ERR(inode); 1229878520acSTheodore Ts'o ext4_set_errno(sb, -err); 12307827a7f6STheodore Ts'o ext4_error(sb, "couldn't read orphan inode %lu (err %d)", 12317827a7f6STheodore Ts'o ino, err); 12327827a7f6STheodore Ts'o return inode; 12337827a7f6STheodore Ts'o } 12341d1fe1eeSDavid Howells 123591ef4cafSDuane Griffin /* 1236c9eb13a9STheodore Ts'o * If the orphans has i_nlinks > 0 then it should be able to 1237c9eb13a9STheodore Ts'o * be truncated, otherwise it won't be removed from the orphan 1238c9eb13a9STheodore Ts'o * list during processing and an infinite loop will result. 1239c9eb13a9STheodore Ts'o * Similarly, it must not be a bad inode. 124091ef4cafSDuane Griffin */ 1241c9eb13a9STheodore Ts'o if ((inode->i_nlink && !ext4_can_truncate(inode)) || 1242c9eb13a9STheodore Ts'o is_bad_inode(inode)) 124391ef4cafSDuane Griffin goto bad_orphan; 124491ef4cafSDuane Griffin 12451d1fe1eeSDavid Howells if (NEXT_ORPHAN(inode) > max_ino) 12461d1fe1eeSDavid Howells goto bad_orphan; 12471d1fe1eeSDavid Howells brelse(bitmap_bh); 12481d1fe1eeSDavid Howells return inode; 12491d1fe1eeSDavid Howells 12501d1fe1eeSDavid Howells bad_orphan: 12517827a7f6STheodore Ts'o ext4_error(sb, "bad orphan inode %lu", ino); 12527827a7f6STheodore Ts'o if (bitmap_bh) 12537827a7f6STheodore Ts'o printk(KERN_ERR "ext4_test_bit(bit=%d, block=%llu) = %d\n", 1254ac27a0ecSDave Kleikamp bit, (unsigned long long)bitmap_bh->b_blocknr, 1255617ba13bSMingming Cao ext4_test_bit(bit, bitmap_bh->b_data)); 1256ac27a0ecSDave Kleikamp if (inode) { 12577827a7f6STheodore Ts'o printk(KERN_ERR "is_bad_inode(inode)=%d\n", 1258ac27a0ecSDave Kleikamp is_bad_inode(inode)); 12597827a7f6STheodore Ts'o printk(KERN_ERR "NEXT_ORPHAN(inode)=%u\n", 1260ac27a0ecSDave Kleikamp NEXT_ORPHAN(inode)); 12617827a7f6STheodore Ts'o printk(KERN_ERR "max_ino=%lu\n", max_ino); 12627827a7f6STheodore Ts'o printk(KERN_ERR "i_nlink=%u\n", inode->i_nlink); 1263ac27a0ecSDave Kleikamp /* Avoid freeing blocks if we got a bad deleted inode */ 12641d1fe1eeSDavid Howells if (inode->i_nlink == 0) 1265ac27a0ecSDave Kleikamp inode->i_blocks = 0; 1266ac27a0ecSDave Kleikamp iput(inode); 1267ac27a0ecSDave Kleikamp } 1268ac27a0ecSDave Kleikamp brelse(bitmap_bh); 12691d1fe1eeSDavid Howells return ERR_PTR(err); 1270ac27a0ecSDave Kleikamp } 1271ac27a0ecSDave Kleikamp 1272617ba13bSMingming Cao unsigned long ext4_count_free_inodes(struct super_block *sb) 1273ac27a0ecSDave Kleikamp { 1274ac27a0ecSDave Kleikamp unsigned long desc_count; 1275617ba13bSMingming Cao struct ext4_group_desc *gdp; 12768df9675fSTheodore Ts'o ext4_group_t i, ngroups = ext4_get_groups_count(sb); 1277617ba13bSMingming Cao #ifdef EXT4FS_DEBUG 1278617ba13bSMingming Cao struct ext4_super_block *es; 1279ac27a0ecSDave Kleikamp unsigned long bitmap_count, x; 1280ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh = NULL; 1281ac27a0ecSDave Kleikamp 1282617ba13bSMingming Cao es = EXT4_SB(sb)->s_es; 1283ac27a0ecSDave Kleikamp desc_count = 0; 1284ac27a0ecSDave Kleikamp bitmap_count = 0; 1285ac27a0ecSDave Kleikamp gdp = NULL; 12868df9675fSTheodore Ts'o for (i = 0; i < ngroups; i++) { 1287617ba13bSMingming Cao gdp = ext4_get_group_desc(sb, i, NULL); 1288ac27a0ecSDave Kleikamp if (!gdp) 1289ac27a0ecSDave Kleikamp continue; 1290560671a0SAneesh Kumar K.V desc_count += ext4_free_inodes_count(sb, gdp); 1291ac27a0ecSDave Kleikamp brelse(bitmap_bh); 1292e29d1cdeSEric Sandeen bitmap_bh = ext4_read_inode_bitmap(sb, i); 12939008a58eSDarrick J. Wong if (IS_ERR(bitmap_bh)) { 12949008a58eSDarrick J. Wong bitmap_bh = NULL; 1295ac27a0ecSDave Kleikamp continue; 12969008a58eSDarrick J. Wong } 1297ac27a0ecSDave Kleikamp 1298f6fb99caSTheodore Ts'o x = ext4_count_free(bitmap_bh->b_data, 1299f6fb99caSTheodore Ts'o EXT4_INODES_PER_GROUP(sb) / 8); 1300c549a95dSEric Sandeen printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n", 1301785b4b3aSPeng Tao (unsigned long) i, ext4_free_inodes_count(sb, gdp), x); 1302ac27a0ecSDave Kleikamp bitmap_count += x; 1303ac27a0ecSDave Kleikamp } 1304ac27a0ecSDave Kleikamp brelse(bitmap_bh); 13054776004fSTheodore Ts'o printk(KERN_DEBUG "ext4_count_free_inodes: " 13064776004fSTheodore Ts'o "stored = %u, computed = %lu, %lu\n", 1307ac27a0ecSDave Kleikamp le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count); 1308ac27a0ecSDave Kleikamp return desc_count; 1309ac27a0ecSDave Kleikamp #else 1310ac27a0ecSDave Kleikamp desc_count = 0; 13118df9675fSTheodore Ts'o for (i = 0; i < ngroups; i++) { 1312617ba13bSMingming Cao gdp = ext4_get_group_desc(sb, i, NULL); 1313ac27a0ecSDave Kleikamp if (!gdp) 1314ac27a0ecSDave Kleikamp continue; 1315560671a0SAneesh Kumar K.V desc_count += ext4_free_inodes_count(sb, gdp); 1316ac27a0ecSDave Kleikamp cond_resched(); 1317ac27a0ecSDave Kleikamp } 1318ac27a0ecSDave Kleikamp return desc_count; 1319ac27a0ecSDave Kleikamp #endif 1320ac27a0ecSDave Kleikamp } 1321ac27a0ecSDave Kleikamp 1322ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */ 1323617ba13bSMingming Cao unsigned long ext4_count_dirs(struct super_block * sb) 1324ac27a0ecSDave Kleikamp { 1325ac27a0ecSDave Kleikamp unsigned long count = 0; 13268df9675fSTheodore Ts'o ext4_group_t i, ngroups = ext4_get_groups_count(sb); 1327ac27a0ecSDave Kleikamp 13288df9675fSTheodore Ts'o for (i = 0; i < ngroups; i++) { 1329617ba13bSMingming Cao struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL); 1330ac27a0ecSDave Kleikamp if (!gdp) 1331ac27a0ecSDave Kleikamp continue; 1332560671a0SAneesh Kumar K.V count += ext4_used_dirs_count(sb, gdp); 1333ac27a0ecSDave Kleikamp } 1334ac27a0ecSDave Kleikamp return count; 1335ac27a0ecSDave Kleikamp } 1336bfff6873SLukas Czerner 1337bfff6873SLukas Czerner /* 1338bfff6873SLukas Czerner * Zeroes not yet zeroed inode table - just write zeroes through the whole 1339bfff6873SLukas Czerner * inode table. Must be called without any spinlock held. The only place 1340bfff6873SLukas Czerner * where it is called from on active part of filesystem is ext4lazyinit 1341bfff6873SLukas Czerner * thread, so we do not need any special locks, however we have to prevent 1342bfff6873SLukas Czerner * inode allocation from the current group, so we take alloc_sem lock, to 1343119c0d44STheodore Ts'o * block ext4_new_inode() until we are finished. 1344bfff6873SLukas Czerner */ 1345e0cbee3eSH Hartley Sweeten int ext4_init_inode_table(struct super_block *sb, ext4_group_t group, 1346bfff6873SLukas Czerner int barrier) 1347bfff6873SLukas Czerner { 1348bfff6873SLukas Czerner struct ext4_group_info *grp = ext4_get_group_info(sb, group); 1349bfff6873SLukas Czerner struct ext4_sb_info *sbi = EXT4_SB(sb); 1350bfff6873SLukas Czerner struct ext4_group_desc *gdp = NULL; 1351bfff6873SLukas Czerner struct buffer_head *group_desc_bh; 1352bfff6873SLukas Czerner handle_t *handle; 1353bfff6873SLukas Czerner ext4_fsblk_t blk; 1354bfff6873SLukas Czerner int num, ret = 0, used_blks = 0; 1355bfff6873SLukas Czerner 1356bfff6873SLukas Czerner /* This should not happen, but just to be sure check this */ 1357bc98a42cSDavid Howells if (sb_rdonly(sb)) { 1358bfff6873SLukas Czerner ret = 1; 1359bfff6873SLukas Czerner goto out; 1360bfff6873SLukas Czerner } 1361bfff6873SLukas Czerner 1362bfff6873SLukas Czerner gdp = ext4_get_group_desc(sb, group, &group_desc_bh); 1363bfff6873SLukas Czerner if (!gdp) 1364bfff6873SLukas Czerner goto out; 1365bfff6873SLukas Czerner 1366bfff6873SLukas Czerner /* 1367bfff6873SLukas Czerner * We do not need to lock this, because we are the only one 1368bfff6873SLukas Czerner * handling this flag. 1369bfff6873SLukas Czerner */ 1370bfff6873SLukas Czerner if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)) 1371bfff6873SLukas Czerner goto out; 1372bfff6873SLukas Czerner 13739924a92aSTheodore Ts'o handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1); 1374bfff6873SLukas Czerner if (IS_ERR(handle)) { 1375bfff6873SLukas Czerner ret = PTR_ERR(handle); 1376bfff6873SLukas Czerner goto out; 1377bfff6873SLukas Czerner } 1378bfff6873SLukas Czerner 1379bfff6873SLukas Czerner down_write(&grp->alloc_sem); 1380bfff6873SLukas Czerner /* 1381bfff6873SLukas Czerner * If inode bitmap was already initialized there may be some 1382bfff6873SLukas Czerner * used inodes so we need to skip blocks with used inodes in 1383bfff6873SLukas Czerner * inode table. 1384bfff6873SLukas Czerner */ 1385bfff6873SLukas Czerner if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT))) 1386bfff6873SLukas Czerner used_blks = DIV_ROUND_UP((EXT4_INODES_PER_GROUP(sb) - 1387bfff6873SLukas Czerner ext4_itable_unused_count(sb, gdp)), 1388bfff6873SLukas Czerner sbi->s_inodes_per_block); 1389bfff6873SLukas Czerner 139050122847STheodore Ts'o if ((used_blks < 0) || (used_blks > sbi->s_itb_per_group) || 139150122847STheodore Ts'o ((group == 0) && ((EXT4_INODES_PER_GROUP(sb) - 139250122847STheodore Ts'o ext4_itable_unused_count(sb, gdp)) < 139350122847STheodore Ts'o EXT4_FIRST_INO(sb)))) { 13941084f252STheodore Ts'o ext4_error(sb, "Something is wrong with group %u: " 13951084f252STheodore Ts'o "used itable blocks: %d; " 13961084f252STheodore Ts'o "itable unused count: %u", 1397857ac889SLukas Czerner group, used_blks, 1398857ac889SLukas Czerner ext4_itable_unused_count(sb, gdp)); 1399857ac889SLukas Czerner ret = 1; 140033853a0dSYongqiang Yang goto err_out; 1401857ac889SLukas Czerner } 1402857ac889SLukas Czerner 1403bfff6873SLukas Czerner blk = ext4_inode_table(sb, gdp) + used_blks; 1404bfff6873SLukas Czerner num = sbi->s_itb_per_group - used_blks; 1405bfff6873SLukas Czerner 1406bfff6873SLukas Czerner BUFFER_TRACE(group_desc_bh, "get_write_access"); 1407bfff6873SLukas Czerner ret = ext4_journal_get_write_access(handle, 1408bfff6873SLukas Czerner group_desc_bh); 1409bfff6873SLukas Czerner if (ret) 1410bfff6873SLukas Czerner goto err_out; 1411bfff6873SLukas Czerner 1412bfff6873SLukas Czerner /* 1413bfff6873SLukas Czerner * Skip zeroout if the inode table is full. But we set the ZEROED 1414bfff6873SLukas Czerner * flag anyway, because obviously, when it is full it does not need 1415bfff6873SLukas Czerner * further zeroing. 1416bfff6873SLukas Czerner */ 1417bfff6873SLukas Czerner if (unlikely(num == 0)) 1418bfff6873SLukas Czerner goto skip_zeroout; 1419bfff6873SLukas Czerner 1420bfff6873SLukas Czerner ext4_debug("going to zero out inode table in group %d\n", 1421bfff6873SLukas Czerner group); 1422a107e5a3STheodore Ts'o ret = sb_issue_zeroout(sb, blk, num, GFP_NOFS); 1423bfff6873SLukas Czerner if (ret < 0) 1424bfff6873SLukas Czerner goto err_out; 1425a107e5a3STheodore Ts'o if (barrier) 1426a107e5a3STheodore Ts'o blkdev_issue_flush(sb->s_bdev, GFP_NOFS, NULL); 1427bfff6873SLukas Czerner 1428bfff6873SLukas Czerner skip_zeroout: 1429bfff6873SLukas Czerner ext4_lock_group(sb, group); 1430bfff6873SLukas Czerner gdp->bg_flags |= cpu_to_le16(EXT4_BG_INODE_ZEROED); 1431feb0ab32SDarrick J. Wong ext4_group_desc_csum_set(sb, group, gdp); 1432bfff6873SLukas Czerner ext4_unlock_group(sb, group); 1433bfff6873SLukas Czerner 1434bfff6873SLukas Czerner BUFFER_TRACE(group_desc_bh, 1435bfff6873SLukas Czerner "call ext4_handle_dirty_metadata"); 1436bfff6873SLukas Czerner ret = ext4_handle_dirty_metadata(handle, NULL, 1437bfff6873SLukas Czerner group_desc_bh); 1438bfff6873SLukas Czerner 1439bfff6873SLukas Czerner err_out: 1440bfff6873SLukas Czerner up_write(&grp->alloc_sem); 1441bfff6873SLukas Czerner ext4_journal_stop(handle); 1442bfff6873SLukas Czerner out: 1443bfff6873SLukas Czerner return ret; 1444bfff6873SLukas Czerner } 1445