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; 858016e29fSHarshad Shirwadkar struct ext4_group_info *grp; 868016e29fSHarshad Shirwadkar 878016e29fSHarshad Shirwadkar if (EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY) 888016e29fSHarshad Shirwadkar return 0; 898016e29fSHarshad Shirwadkar 908016e29fSHarshad Shirwadkar grp = ext4_get_group_info(sb, block_group); 919008a58eSDarrick J. Wong 929008a58eSDarrick J. Wong if (buffer_verified(bh)) 939008a58eSDarrick J. Wong return 0; 949008a58eSDarrick J. Wong if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) 959008a58eSDarrick J. Wong return -EFSCORRUPTED; 969008a58eSDarrick J. Wong 979008a58eSDarrick J. Wong ext4_lock_group(sb, block_group); 988d5a803cSTheodore Ts'o if (buffer_verified(bh)) 998d5a803cSTheodore Ts'o goto verified; 1009008a58eSDarrick J. Wong blk = ext4_inode_bitmap(sb, desc); 1019008a58eSDarrick J. Wong if (!ext4_inode_bitmap_csum_verify(sb, block_group, desc, bh, 10246f870d6STheodore Ts'o EXT4_INODES_PER_GROUP(sb) / 8) || 10346f870d6STheodore Ts'o ext4_simulate_fail(sb, EXT4_SIM_IBITMAP_CRC)) { 1049008a58eSDarrick J. Wong ext4_unlock_group(sb, block_group); 1059008a58eSDarrick J. Wong ext4_error(sb, "Corrupt inode bitmap - block_group = %u, " 1069008a58eSDarrick J. Wong "inode_bitmap = %llu", block_group, blk); 107db79e6d1SWang Shilong ext4_mark_group_bitmap_corrupted(sb, block_group, 108db79e6d1SWang Shilong EXT4_GROUP_INFO_IBITMAP_CORRUPT); 1099008a58eSDarrick J. Wong return -EFSBADCRC; 1109008a58eSDarrick J. Wong } 1119008a58eSDarrick J. Wong set_buffer_verified(bh); 1128d5a803cSTheodore Ts'o verified: 1139008a58eSDarrick J. Wong ext4_unlock_group(sb, block_group); 1149008a58eSDarrick J. Wong return 0; 1159008a58eSDarrick J. Wong } 1169008a58eSDarrick J. Wong 117ac27a0ecSDave Kleikamp /* 118ac27a0ecSDave Kleikamp * Read the inode allocation bitmap for a given block_group, reading 119ac27a0ecSDave Kleikamp * into the specified slot in the superblock's bitmap cache. 120ac27a0ecSDave Kleikamp * 1219033783cSJosh Triplett * Return buffer_head of bitmap on success, or an ERR_PTR on error. 122ac27a0ecSDave Kleikamp */ 123ac27a0ecSDave Kleikamp static struct buffer_head * 124e29d1cdeSEric Sandeen ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group) 125ac27a0ecSDave Kleikamp { 126617ba13bSMingming Cao struct ext4_group_desc *desc; 1277dac4a17STheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(sb); 128ac27a0ecSDave Kleikamp struct buffer_head *bh = NULL; 129e29d1cdeSEric Sandeen ext4_fsblk_t bitmap_blk; 1309008a58eSDarrick J. Wong int err; 131ac27a0ecSDave Kleikamp 132617ba13bSMingming Cao desc = ext4_get_group_desc(sb, block_group, NULL); 133ac27a0ecSDave Kleikamp if (!desc) 1349008a58eSDarrick J. Wong return ERR_PTR(-EFSCORRUPTED); 135bfff6873SLukas Czerner 136e29d1cdeSEric Sandeen bitmap_blk = ext4_inode_bitmap(sb, desc); 1377dac4a17STheodore Ts'o if ((bitmap_blk <= le32_to_cpu(sbi->s_es->s_first_data_block)) || 1387dac4a17STheodore Ts'o (bitmap_blk >= ext4_blocks_count(sbi->s_es))) { 1397dac4a17STheodore Ts'o ext4_error(sb, "Invalid inode bitmap blk %llu in " 1407dac4a17STheodore Ts'o "block_group %u", bitmap_blk, block_group); 141206f6d55SWang Shilong ext4_mark_group_bitmap_corrupted(sb, block_group, 142206f6d55SWang Shilong EXT4_GROUP_INFO_IBITMAP_CORRUPT); 1437dac4a17STheodore Ts'o return ERR_PTR(-EFSCORRUPTED); 1447dac4a17STheodore Ts'o } 145e29d1cdeSEric Sandeen bh = sb_getblk(sb, bitmap_blk); 146e29d1cdeSEric Sandeen if (unlikely(!bh)) { 1475ef2a699SWang Shilong ext4_warning(sb, "Cannot read inode bitmap - " 148a9df9a49STheodore Ts'o "block_group = %u, inode_bitmap = %llu", 149e29d1cdeSEric Sandeen block_group, bitmap_blk); 1500db9fdebSWang Shilong return ERR_PTR(-ENOMEM); 151e29d1cdeSEric Sandeen } 1522ccb5fb9SAneesh Kumar K.V if (bitmap_uptodate(bh)) 15341a246d1SDarrick J. Wong goto verify; 154e29d1cdeSEric Sandeen 155c806e68fSFrederic Bohe lock_buffer(bh); 1562ccb5fb9SAneesh Kumar K.V if (bitmap_uptodate(bh)) { 1572ccb5fb9SAneesh Kumar K.V unlock_buffer(bh); 15841a246d1SDarrick J. Wong goto verify; 1592ccb5fb9SAneesh Kumar K.V } 160bfff6873SLukas Czerner 161955ce5f5SAneesh Kumar K.V ext4_lock_group(sb, block_group); 1628844618dSTheodore Ts'o if (ext4_has_group_desc_csum(sb) && 1638844618dSTheodore Ts'o (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT))) { 1648844618dSTheodore Ts'o if (block_group == 0) { 1658844618dSTheodore Ts'o ext4_unlock_group(sb, block_group); 1668844618dSTheodore Ts'o unlock_buffer(bh); 1678844618dSTheodore Ts'o ext4_error(sb, "Inode bitmap for bg 0 marked " 1688844618dSTheodore Ts'o "uninitialized"); 1698844618dSTheodore Ts'o err = -EFSCORRUPTED; 1708844618dSTheodore Ts'o goto out; 1718844618dSTheodore Ts'o } 172044e6e3dSTheodore Ts'o memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8); 173044e6e3dSTheodore Ts'o ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), 174044e6e3dSTheodore Ts'o sb->s_blocksize * 8, bh->b_data); 1752ccb5fb9SAneesh Kumar K.V set_bitmap_uptodate(bh); 176e29d1cdeSEric Sandeen set_buffer_uptodate(bh); 17741a246d1SDarrick J. Wong set_buffer_verified(bh); 178955ce5f5SAneesh Kumar K.V ext4_unlock_group(sb, block_group); 1793300bedaSAneesh Kumar K.V unlock_buffer(bh); 180e29d1cdeSEric Sandeen return bh; 181e29d1cdeSEric Sandeen } 182955ce5f5SAneesh Kumar K.V ext4_unlock_group(sb, block_group); 183bfff6873SLukas Czerner 1842ccb5fb9SAneesh Kumar K.V if (buffer_uptodate(bh)) { 1852ccb5fb9SAneesh Kumar K.V /* 1862ccb5fb9SAneesh Kumar K.V * if not uninit if bh is uptodate, 1872ccb5fb9SAneesh Kumar K.V * bitmap is also uptodate 1882ccb5fb9SAneesh Kumar K.V */ 1892ccb5fb9SAneesh Kumar K.V set_bitmap_uptodate(bh); 1902ccb5fb9SAneesh Kumar K.V unlock_buffer(bh); 19141a246d1SDarrick J. Wong goto verify; 1922ccb5fb9SAneesh Kumar K.V } 1932ccb5fb9SAneesh Kumar K.V /* 194813e5727STheodore Ts'o * submit the buffer_head for reading 1952ccb5fb9SAneesh Kumar K.V */ 1960562e0baSJiaying Zhang trace_ext4_load_inode_bitmap(sb, block_group); 1972d069c08Szhangyi (F) ext4_read_bh(bh, REQ_META | REQ_PRIO, ext4_end_bitmap_read); 19846f870d6STheodore Ts'o ext4_simulate_fail_bh(sb, bh, EXT4_SIM_IBITMAP_EIO); 199813e5727STheodore Ts'o if (!buffer_uptodate(bh)) { 200e29d1cdeSEric Sandeen put_bh(bh); 20154d3adbcSTheodore Ts'o ext4_error_err(sb, EIO, "Cannot read inode bitmap - " 202a9df9a49STheodore Ts'o "block_group = %u, inode_bitmap = %llu", 203e29d1cdeSEric Sandeen block_group, bitmap_blk); 204206f6d55SWang Shilong ext4_mark_group_bitmap_corrupted(sb, block_group, 205206f6d55SWang Shilong EXT4_GROUP_INFO_IBITMAP_CORRUPT); 2069008a58eSDarrick J. Wong return ERR_PTR(-EIO); 207e29d1cdeSEric Sandeen } 20841a246d1SDarrick J. Wong 20941a246d1SDarrick J. Wong verify: 2109008a58eSDarrick J. Wong err = ext4_validate_inode_bitmap(sb, desc, block_group, bh); 2119008a58eSDarrick J. Wong if (err) 2129008a58eSDarrick J. Wong goto out; 213ac27a0ecSDave Kleikamp return bh; 2149008a58eSDarrick J. Wong out: 2159008a58eSDarrick J. Wong put_bh(bh); 2169008a58eSDarrick J. Wong return ERR_PTR(err); 217ac27a0ecSDave Kleikamp } 218ac27a0ecSDave Kleikamp 219ac27a0ecSDave Kleikamp /* 220ac27a0ecSDave Kleikamp * NOTE! When we get the inode, we're the only people 221ac27a0ecSDave Kleikamp * that have access to it, and as such there are no 222ac27a0ecSDave Kleikamp * race conditions we have to worry about. The inode 223ac27a0ecSDave Kleikamp * is not on the hash-lists, and it cannot be reached 224ac27a0ecSDave Kleikamp * through the filesystem because the directory entry 225ac27a0ecSDave Kleikamp * has been deleted earlier. 226ac27a0ecSDave Kleikamp * 227ac27a0ecSDave Kleikamp * HOWEVER: we must make sure that we get no aliases, 228ac27a0ecSDave Kleikamp * which means that we have to call "clear_inode()" 229ac27a0ecSDave Kleikamp * _before_ we mark the inode not in use in the inode 230ac27a0ecSDave Kleikamp * bitmaps. Otherwise a newly created file might use 231ac27a0ecSDave Kleikamp * the same inode number (not actually the same pointer 232ac27a0ecSDave Kleikamp * though), and then we'd have two inodes sharing the 233ac27a0ecSDave Kleikamp * same inode number and space on the harddisk. 234ac27a0ecSDave Kleikamp */ 235617ba13bSMingming Cao void ext4_free_inode(handle_t *handle, struct inode *inode) 236ac27a0ecSDave Kleikamp { 237ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 238ac27a0ecSDave Kleikamp int is_directory; 239ac27a0ecSDave Kleikamp unsigned long ino; 240ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh = NULL; 241ac27a0ecSDave Kleikamp struct buffer_head *bh2; 242fd2d4291SAvantika Mathur ext4_group_t block_group; 243ac27a0ecSDave Kleikamp unsigned long bit; 244617ba13bSMingming Cao struct ext4_group_desc *gdp; 245617ba13bSMingming Cao struct ext4_super_block *es; 246617ba13bSMingming Cao struct ext4_sb_info *sbi; 2477ce9d5d1SEric Sandeen int fatal = 0, err, count, cleared; 24887a39389SDarrick J. Wong struct ext4_group_info *grp; 249ac27a0ecSDave Kleikamp 25092b97816STheodore Ts'o if (!sb) { 25192b97816STheodore Ts'o printk(KERN_ERR "EXT4-fs: %s:%d: inode on " 25292b97816STheodore Ts'o "nonexistent device\n", __func__, __LINE__); 25392b97816STheodore Ts'o return; 25492b97816STheodore Ts'o } 255ac27a0ecSDave Kleikamp if (atomic_read(&inode->i_count) > 1) { 25692b97816STheodore Ts'o ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: count=%d", 25792b97816STheodore Ts'o __func__, __LINE__, inode->i_ino, 258ac27a0ecSDave Kleikamp atomic_read(&inode->i_count)); 259ac27a0ecSDave Kleikamp return; 260ac27a0ecSDave Kleikamp } 261ac27a0ecSDave Kleikamp if (inode->i_nlink) { 26292b97816STheodore Ts'o ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: nlink=%d\n", 26392b97816STheodore Ts'o __func__, __LINE__, inode->i_ino, inode->i_nlink); 264ac27a0ecSDave Kleikamp return; 265ac27a0ecSDave Kleikamp } 266617ba13bSMingming Cao sbi = EXT4_SB(sb); 267ac27a0ecSDave Kleikamp 268ac27a0ecSDave Kleikamp ino = inode->i_ino; 269617ba13bSMingming Cao ext4_debug("freeing inode %lu\n", ino); 2709bffad1eSTheodore Ts'o trace_ext4_free_inode(inode); 271ac27a0ecSDave Kleikamp 272871a2931SChristoph Hellwig dquot_initialize(inode); 27363936ddaSChristoph Hellwig dquot_free_inode(inode); 274ac27a0ecSDave Kleikamp 275ac27a0ecSDave Kleikamp is_directory = S_ISDIR(inode->i_mode); 276ac27a0ecSDave Kleikamp 277ac27a0ecSDave Kleikamp /* Do this BEFORE marking the inode not in use or returning an error */ 2780930fcc1SAl Viro ext4_clear_inode(inode); 279ac27a0ecSDave Kleikamp 28049598e04SJun Piao es = sbi->s_es; 281617ba13bSMingming Cao if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) { 28212062dddSEric Sandeen ext4_error(sb, "reserved or nonexistent inode %lu", ino); 283ac27a0ecSDave Kleikamp goto error_return; 284ac27a0ecSDave Kleikamp } 285617ba13bSMingming Cao block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); 286617ba13bSMingming Cao bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); 287e29d1cdeSEric Sandeen bitmap_bh = ext4_read_inode_bitmap(sb, block_group); 28887a39389SDarrick J. Wong /* Don't bother if the inode bitmap is corrupt. */ 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 } 2948016e29fSHarshad Shirwadkar if (!(sbi->s_mount_state & EXT4_FC_REPLAY)) { 2958016e29fSHarshad Shirwadkar grp = ext4_get_group_info(sb, block_group); 2969008a58eSDarrick J. Wong if (unlikely(EXT4_MB_GRP_IBITMAP_CORRUPT(grp))) { 2979008a58eSDarrick J. Wong fatal = -EFSCORRUPTED; 2989008a58eSDarrick J. Wong goto error_return; 2999008a58eSDarrick J. Wong } 3008016e29fSHarshad Shirwadkar } 301ac27a0ecSDave Kleikamp 302ac27a0ecSDave Kleikamp BUFFER_TRACE(bitmap_bh, "get_write_access"); 303188c299eSJan Kara fatal = ext4_journal_get_write_access(handle, sb, bitmap_bh, 304188c299eSJan Kara EXT4_JTR_NONE); 305ac27a0ecSDave Kleikamp if (fatal) 306ac27a0ecSDave Kleikamp goto error_return; 307ac27a0ecSDave Kleikamp 308d17413c0SDmitry Monakhov fatal = -ESRCH; 309617ba13bSMingming Cao gdp = ext4_get_group_desc(sb, block_group, &bh2); 310d17413c0SDmitry Monakhov if (gdp) { 311ac27a0ecSDave Kleikamp BUFFER_TRACE(bh2, "get_write_access"); 312188c299eSJan Kara fatal = ext4_journal_get_write_access(handle, sb, bh2, 313188c299eSJan Kara EXT4_JTR_NONE); 314d17413c0SDmitry Monakhov } 315955ce5f5SAneesh Kumar K.V ext4_lock_group(sb, block_group); 316597d508cSAkinobu Mita cleared = ext4_test_and_clear_bit(bit, bitmap_bh->b_data); 317d17413c0SDmitry Monakhov if (fatal || !cleared) { 318d17413c0SDmitry Monakhov ext4_unlock_group(sb, block_group); 319d17413c0SDmitry Monakhov goto out; 320d17413c0SDmitry Monakhov } 321d17413c0SDmitry Monakhov 322560671a0SAneesh Kumar K.V count = ext4_free_inodes_count(sb, gdp) + 1; 323560671a0SAneesh Kumar K.V ext4_free_inodes_set(sb, gdp, count); 324560671a0SAneesh Kumar K.V if (is_directory) { 325560671a0SAneesh Kumar K.V count = ext4_used_dirs_count(sb, gdp) - 1; 326560671a0SAneesh Kumar K.V ext4_used_dirs_set(sb, gdp, count); 327b45f189aSRitesh Harjani if (percpu_counter_initialized(&sbi->s_dirs_counter)) 328d17413c0SDmitry Monakhov percpu_counter_dec(&sbi->s_dirs_counter); 329d17413c0SDmitry Monakhov } 33041a246d1SDarrick J. Wong ext4_inode_bitmap_csum_set(sb, block_group, gdp, bitmap_bh, 33141a246d1SDarrick J. Wong EXT4_INODES_PER_GROUP(sb) / 8); 332feb0ab32SDarrick J. Wong ext4_group_desc_csum_set(sb, block_group, gdp); 333d17413c0SDmitry Monakhov ext4_unlock_group(sb, block_group); 3347d39db14STheodore Ts'o 335b45f189aSRitesh Harjani if (percpu_counter_initialized(&sbi->s_freeinodes_counter)) 336d17413c0SDmitry Monakhov percpu_counter_inc(&sbi->s_freeinodes_counter); 337d17413c0SDmitry Monakhov if (sbi->s_log_groups_per_flex) { 3387c990728SSuraj Jitindar Singh struct flex_groups *fg; 339d17413c0SDmitry Monakhov 3407c990728SSuraj Jitindar Singh fg = sbi_array_rcu_deref(sbi, s_flex_groups, 3417c990728SSuraj Jitindar Singh ext4_flex_group(sbi, block_group)); 3427c990728SSuraj Jitindar Singh atomic_inc(&fg->free_inodes); 343d17413c0SDmitry Monakhov if (is_directory) 3447c990728SSuraj Jitindar Singh atomic_dec(&fg->used_dirs); 3457d39db14STheodore Ts'o } 3460390131bSFrank Mayhar BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata"); 347d17413c0SDmitry Monakhov fatal = ext4_handle_dirty_metadata(handle, NULL, bh2); 348d17413c0SDmitry Monakhov out: 349d17413c0SDmitry Monakhov if (cleared) { 3500390131bSFrank Mayhar BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata"); 3510390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh); 352ac27a0ecSDave Kleikamp if (!fatal) 353ac27a0ecSDave Kleikamp fatal = err; 35487a39389SDarrick J. Wong } else { 355d17413c0SDmitry Monakhov ext4_error(sb, "bit already cleared for inode %lu", ino); 356db79e6d1SWang Shilong ext4_mark_group_bitmap_corrupted(sb, block_group, 357db79e6d1SWang Shilong EXT4_GROUP_INFO_IBITMAP_CORRUPT); 35887a39389SDarrick J. Wong } 359d17413c0SDmitry Monakhov 360ac27a0ecSDave Kleikamp error_return: 361ac27a0ecSDave Kleikamp brelse(bitmap_bh); 362617ba13bSMingming Cao ext4_std_error(sb, fatal); 363ac27a0ecSDave Kleikamp } 364ac27a0ecSDave Kleikamp 365a4912123STheodore Ts'o struct orlov_stats { 36690ba983fSTheodore Ts'o __u64 free_clusters; 367a4912123STheodore Ts'o __u32 free_inodes; 368a4912123STheodore Ts'o __u32 used_dirs; 369a4912123STheodore Ts'o }; 370a4912123STheodore Ts'o 371a4912123STheodore Ts'o /* 372a4912123STheodore Ts'o * Helper function for Orlov's allocator; returns critical information 373a4912123STheodore Ts'o * for a particular block group or flex_bg. If flex_size is 1, then g 374a4912123STheodore Ts'o * is a block group number; otherwise it is flex_bg number. 375a4912123STheodore Ts'o */ 3761f109d5aSTheodore Ts'o static void get_orlov_stats(struct super_block *sb, ext4_group_t g, 377a4912123STheodore Ts'o int flex_size, struct orlov_stats *stats) 378a4912123STheodore Ts'o { 379a4912123STheodore Ts'o struct ext4_group_desc *desc; 380a4912123STheodore Ts'o 3817d39db14STheodore Ts'o if (flex_size > 1) { 3827c990728SSuraj Jitindar Singh struct flex_groups *fg = sbi_array_rcu_deref(EXT4_SB(sb), 3837c990728SSuraj Jitindar Singh s_flex_groups, g); 3847c990728SSuraj Jitindar Singh stats->free_inodes = atomic_read(&fg->free_inodes); 3857c990728SSuraj Jitindar Singh stats->free_clusters = atomic64_read(&fg->free_clusters); 3867c990728SSuraj Jitindar Singh stats->used_dirs = atomic_read(&fg->used_dirs); 3877d39db14STheodore Ts'o return; 3887d39db14STheodore Ts'o } 3897d39db14STheodore Ts'o 3907d39db14STheodore Ts'o desc = ext4_get_group_desc(sb, g, NULL); 3917d39db14STheodore Ts'o if (desc) { 3927d39db14STheodore Ts'o stats->free_inodes = ext4_free_inodes_count(sb, desc); 393021b65bbSTheodore Ts'o stats->free_clusters = ext4_free_group_clusters(sb, desc); 3947d39db14STheodore Ts'o stats->used_dirs = ext4_used_dirs_count(sb, desc); 3957d39db14STheodore Ts'o } else { 396a4912123STheodore Ts'o stats->free_inodes = 0; 39724aaa8efSTheodore Ts'o stats->free_clusters = 0; 398a4912123STheodore Ts'o stats->used_dirs = 0; 399a4912123STheodore Ts'o } 400a4912123STheodore Ts'o } 401a4912123STheodore Ts'o 402ac27a0ecSDave Kleikamp /* 403ac27a0ecSDave Kleikamp * Orlov's allocator for directories. 404ac27a0ecSDave Kleikamp * 405ac27a0ecSDave Kleikamp * We always try to spread first-level directories. 406ac27a0ecSDave Kleikamp * 407c89849ccSPan Dong * If there are blockgroups with both free inodes and free clusters counts 408ac27a0ecSDave Kleikamp * not worse than average we return one with smallest directory count. 409ac27a0ecSDave Kleikamp * Otherwise we simply return a random group. 410ac27a0ecSDave Kleikamp * 411ac27a0ecSDave Kleikamp * For the rest rules look so: 412ac27a0ecSDave Kleikamp * 413ac27a0ecSDave Kleikamp * It's OK to put directory into a group unless 414ac27a0ecSDave Kleikamp * it has too many directories already (max_dirs) or 415ac27a0ecSDave Kleikamp * it has too few free inodes left (min_inodes) or 416c89849ccSPan Dong * it has too few free clusters left (min_clusters) or 4171cc8dcf5SBenoit Boissinot * Parent's group is preferred, if it doesn't satisfy these 418ac27a0ecSDave Kleikamp * conditions we search cyclically through the rest. If none 419ac27a0ecSDave Kleikamp * of the groups look good we just look for a group with more 420ac27a0ecSDave Kleikamp * free inodes than average (starting at parent's group). 421ac27a0ecSDave Kleikamp */ 422ac27a0ecSDave Kleikamp 4232aa9fc4cSAvantika Mathur static int find_group_orlov(struct super_block *sb, struct inode *parent, 424dcca3fecSAl Viro ext4_group_t *group, umode_t mode, 425f157a4aaSTheodore Ts'o const struct qstr *qstr) 426ac27a0ecSDave Kleikamp { 427fd2d4291SAvantika Mathur ext4_group_t parent_group = EXT4_I(parent)->i_block_group; 428617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 4298df9675fSTheodore Ts'o ext4_group_t real_ngroups = ext4_get_groups_count(sb); 430617ba13bSMingming Cao int inodes_per_group = EXT4_INODES_PER_GROUP(sb); 43114c83c9fSTheodore Ts'o unsigned int freei, avefreei, grp_free; 432c89849ccSPan Dong ext4_fsblk_t freec, avefreec; 433ac27a0ecSDave Kleikamp unsigned int ndirs; 434a4912123STheodore Ts'o int max_dirs, min_inodes; 43524aaa8efSTheodore Ts'o ext4_grpblk_t min_clusters; 4368df9675fSTheodore Ts'o ext4_group_t i, grp, g, ngroups; 437617ba13bSMingming Cao struct ext4_group_desc *desc; 438a4912123STheodore Ts'o struct orlov_stats stats; 439a4912123STheodore Ts'o int flex_size = ext4_flex_bg_size(sbi); 440f157a4aaSTheodore Ts'o struct dx_hash_info hinfo; 441a4912123STheodore Ts'o 4428df9675fSTheodore Ts'o ngroups = real_ngroups; 443a4912123STheodore Ts'o if (flex_size > 1) { 4448df9675fSTheodore Ts'o ngroups = (real_ngroups + flex_size - 1) >> 445a4912123STheodore Ts'o sbi->s_log_groups_per_flex; 446a4912123STheodore Ts'o parent_group >>= sbi->s_log_groups_per_flex; 447a4912123STheodore Ts'o } 448ac27a0ecSDave Kleikamp 449ac27a0ecSDave Kleikamp freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter); 450ac27a0ecSDave Kleikamp avefreei = freei / ngroups; 451c89849ccSPan Dong freec = percpu_counter_read_positive(&sbi->s_freeclusters_counter); 452c89849ccSPan Dong avefreec = freec; 45324aaa8efSTheodore Ts'o do_div(avefreec, ngroups); 454ac27a0ecSDave Kleikamp ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter); 455ac27a0ecSDave Kleikamp 456a4912123STheodore Ts'o if (S_ISDIR(mode) && 4572b0143b5SDavid Howells ((parent == d_inode(sb->s_root)) || 45812e9b892SDmitry Monakhov (ext4_test_inode_flag(parent, EXT4_INODE_TOPDIR)))) { 459ac27a0ecSDave Kleikamp int best_ndir = inodes_per_group; 4602aa9fc4cSAvantika Mathur int ret = -1; 461ac27a0ecSDave Kleikamp 462f157a4aaSTheodore Ts'o if (qstr) { 463f157a4aaSTheodore Ts'o hinfo.hash_version = DX_HASH_HALF_MD4; 464f157a4aaSTheodore Ts'o hinfo.seed = sbi->s_hash_seed; 465b886ee3eSGabriel Krisman Bertazi ext4fs_dirhash(parent, qstr->name, qstr->len, &hinfo); 4668b3ccbc1SJason A. Donenfeld parent_group = hinfo.hash % ngroups; 467f157a4aaSTheodore Ts'o } else 4688032bf12SJason A. Donenfeld parent_group = get_random_u32_below(ngroups); 469ac27a0ecSDave Kleikamp for (i = 0; i < ngroups; i++) { 470a4912123STheodore Ts'o g = (parent_group + i) % ngroups; 471a4912123STheodore Ts'o get_orlov_stats(sb, g, flex_size, &stats); 472a4912123STheodore Ts'o if (!stats.free_inodes) 473ac27a0ecSDave Kleikamp continue; 474a4912123STheodore Ts'o if (stats.used_dirs >= best_ndir) 475ac27a0ecSDave Kleikamp continue; 476a4912123STheodore Ts'o if (stats.free_inodes < avefreei) 477ac27a0ecSDave Kleikamp continue; 47824aaa8efSTheodore Ts'o if (stats.free_clusters < avefreec) 479ac27a0ecSDave Kleikamp continue; 480a4912123STheodore Ts'o grp = g; 4812aa9fc4cSAvantika Mathur ret = 0; 482a4912123STheodore Ts'o best_ndir = stats.used_dirs; 483ac27a0ecSDave Kleikamp } 484a4912123STheodore Ts'o if (ret) 485a4912123STheodore Ts'o goto fallback; 486a4912123STheodore Ts'o found_flex_bg: 487a4912123STheodore Ts'o if (flex_size == 1) { 488a4912123STheodore Ts'o *group = grp; 489a4912123STheodore Ts'o return 0; 490a4912123STheodore Ts'o } 491a4912123STheodore Ts'o 492a4912123STheodore Ts'o /* 493a4912123STheodore Ts'o * We pack inodes at the beginning of the flexgroup's 494a4912123STheodore Ts'o * inode tables. Block allocation decisions will do 495a4912123STheodore Ts'o * something similar, although regular files will 496a4912123STheodore Ts'o * start at 2nd block group of the flexgroup. See 497a4912123STheodore Ts'o * ext4_ext_find_goal() and ext4_find_near(). 498a4912123STheodore Ts'o */ 499a4912123STheodore Ts'o grp *= flex_size; 500a4912123STheodore Ts'o for (i = 0; i < flex_size; i++) { 5018df9675fSTheodore Ts'o if (grp+i >= real_ngroups) 502a4912123STheodore Ts'o break; 503a4912123STheodore Ts'o desc = ext4_get_group_desc(sb, grp+i, NULL); 504a4912123STheodore Ts'o if (desc && ext4_free_inodes_count(sb, desc)) { 505a4912123STheodore Ts'o *group = grp+i; 506a4912123STheodore Ts'o return 0; 507a4912123STheodore Ts'o } 508a4912123STheodore Ts'o } 509ac27a0ecSDave Kleikamp goto fallback; 510ac27a0ecSDave Kleikamp } 511ac27a0ecSDave Kleikamp 512613c5a85SJan Kara max_dirs = ndirs / ngroups + inodes_per_group*flex_size / 16; 513a4912123STheodore Ts'o min_inodes = avefreei - inodes_per_group*flex_size / 4; 514a4912123STheodore Ts'o if (min_inodes < 1) 515a4912123STheodore Ts'o min_inodes = 1; 51624aaa8efSTheodore Ts'o min_clusters = avefreec - EXT4_CLUSTERS_PER_GROUP(sb)*flex_size / 4; 517ac27a0ecSDave Kleikamp 518a4912123STheodore Ts'o /* 519a4912123STheodore Ts'o * Start looking in the flex group where we last allocated an 520a4912123STheodore Ts'o * inode for this parent directory 521a4912123STheodore Ts'o */ 522a4912123STheodore Ts'o if (EXT4_I(parent)->i_last_alloc_group != ~0) { 523a4912123STheodore Ts'o parent_group = EXT4_I(parent)->i_last_alloc_group; 524a4912123STheodore Ts'o if (flex_size > 1) 525a4912123STheodore Ts'o parent_group >>= sbi->s_log_groups_per_flex; 526a4912123STheodore Ts'o } 527ac27a0ecSDave Kleikamp 528ac27a0ecSDave Kleikamp for (i = 0; i < ngroups; i++) { 529a4912123STheodore Ts'o grp = (parent_group + i) % ngroups; 530a4912123STheodore Ts'o get_orlov_stats(sb, grp, flex_size, &stats); 531a4912123STheodore Ts'o if (stats.used_dirs >= max_dirs) 532ac27a0ecSDave Kleikamp continue; 533a4912123STheodore Ts'o if (stats.free_inodes < min_inodes) 534ac27a0ecSDave Kleikamp continue; 53524aaa8efSTheodore Ts'o if (stats.free_clusters < min_clusters) 536ac27a0ecSDave Kleikamp continue; 537a4912123STheodore Ts'o goto found_flex_bg; 538ac27a0ecSDave Kleikamp } 539ac27a0ecSDave Kleikamp 540ac27a0ecSDave Kleikamp fallback: 5418df9675fSTheodore Ts'o ngroups = real_ngroups; 542a4912123STheodore Ts'o avefreei = freei / ngroups; 543b5451f7bSTheodore Ts'o fallback_retry: 544a4912123STheodore Ts'o parent_group = EXT4_I(parent)->i_block_group; 545ac27a0ecSDave Kleikamp for (i = 0; i < ngroups; i++) { 546a4912123STheodore Ts'o grp = (parent_group + i) % ngroups; 547a4912123STheodore Ts'o desc = ext4_get_group_desc(sb, grp, NULL); 548bb3d132aSDan Carpenter if (desc) { 54914c83c9fSTheodore Ts'o grp_free = ext4_free_inodes_count(sb, desc); 550bb3d132aSDan Carpenter if (grp_free && grp_free >= avefreei) { 551a4912123STheodore Ts'o *group = grp; 5522aa9fc4cSAvantika Mathur return 0; 553ac27a0ecSDave Kleikamp } 554a4912123STheodore Ts'o } 555bb3d132aSDan Carpenter } 556ac27a0ecSDave Kleikamp 557ac27a0ecSDave Kleikamp if (avefreei) { 558ac27a0ecSDave Kleikamp /* 559ac27a0ecSDave Kleikamp * The free-inodes counter is approximate, and for really small 560ac27a0ecSDave Kleikamp * filesystems the above test can fail to find any blockgroups 561ac27a0ecSDave Kleikamp */ 562ac27a0ecSDave Kleikamp avefreei = 0; 563b5451f7bSTheodore Ts'o goto fallback_retry; 564ac27a0ecSDave Kleikamp } 565ac27a0ecSDave Kleikamp 566ac27a0ecSDave Kleikamp return -1; 567ac27a0ecSDave Kleikamp } 568ac27a0ecSDave Kleikamp 5692aa9fc4cSAvantika Mathur static int find_group_other(struct super_block *sb, struct inode *parent, 570dcca3fecSAl Viro ext4_group_t *group, umode_t mode) 571ac27a0ecSDave Kleikamp { 572fd2d4291SAvantika Mathur ext4_group_t parent_group = EXT4_I(parent)->i_block_group; 5738df9675fSTheodore Ts'o ext4_group_t i, last, ngroups = ext4_get_groups_count(sb); 574617ba13bSMingming Cao struct ext4_group_desc *desc; 575a4912123STheodore Ts'o int flex_size = ext4_flex_bg_size(EXT4_SB(sb)); 576a4912123STheodore Ts'o 577a4912123STheodore Ts'o /* 578a4912123STheodore Ts'o * Try to place the inode is the same flex group as its 579a4912123STheodore Ts'o * parent. If we can't find space, use the Orlov algorithm to 580a4912123STheodore Ts'o * find another flex group, and store that information in the 581a4912123STheodore Ts'o * parent directory's inode information so that use that flex 582a4912123STheodore Ts'o * group for future allocations. 583a4912123STheodore Ts'o */ 584a4912123STheodore Ts'o if (flex_size > 1) { 585a4912123STheodore Ts'o int retry = 0; 586a4912123STheodore Ts'o 587a4912123STheodore Ts'o try_again: 588a4912123STheodore Ts'o parent_group &= ~(flex_size-1); 589a4912123STheodore Ts'o last = parent_group + flex_size; 590a4912123STheodore Ts'o if (last > ngroups) 591a4912123STheodore Ts'o last = ngroups; 592a4912123STheodore Ts'o for (i = parent_group; i < last; i++) { 593a4912123STheodore Ts'o desc = ext4_get_group_desc(sb, i, NULL); 594a4912123STheodore Ts'o if (desc && ext4_free_inodes_count(sb, desc)) { 595a4912123STheodore Ts'o *group = i; 596a4912123STheodore Ts'o return 0; 597a4912123STheodore Ts'o } 598a4912123STheodore Ts'o } 599a4912123STheodore Ts'o if (!retry && EXT4_I(parent)->i_last_alloc_group != ~0) { 600a4912123STheodore Ts'o retry = 1; 601a4912123STheodore Ts'o parent_group = EXT4_I(parent)->i_last_alloc_group; 602a4912123STheodore Ts'o goto try_again; 603a4912123STheodore Ts'o } 604a4912123STheodore Ts'o /* 605a4912123STheodore Ts'o * If this didn't work, use the Orlov search algorithm 606a4912123STheodore Ts'o * to find a new flex group; we pass in the mode to 607a4912123STheodore Ts'o * avoid the topdir algorithms. 608a4912123STheodore Ts'o */ 609a4912123STheodore Ts'o *group = parent_group + flex_size; 610a4912123STheodore Ts'o if (*group > ngroups) 611a4912123STheodore Ts'o *group = 0; 6127dc57615SPeter Huewe return find_group_orlov(sb, parent, group, mode, NULL); 613a4912123STheodore Ts'o } 614ac27a0ecSDave Kleikamp 615ac27a0ecSDave Kleikamp /* 616ac27a0ecSDave Kleikamp * Try to place the inode in its parent directory 617ac27a0ecSDave Kleikamp */ 6182aa9fc4cSAvantika Mathur *group = parent_group; 6192aa9fc4cSAvantika Mathur desc = ext4_get_group_desc(sb, *group, NULL); 620560671a0SAneesh Kumar K.V if (desc && ext4_free_inodes_count(sb, desc) && 621021b65bbSTheodore Ts'o ext4_free_group_clusters(sb, desc)) 6222aa9fc4cSAvantika Mathur return 0; 623ac27a0ecSDave Kleikamp 624ac27a0ecSDave Kleikamp /* 625ac27a0ecSDave Kleikamp * We're going to place this inode in a different blockgroup from its 626ac27a0ecSDave Kleikamp * parent. We want to cause files in a common directory to all land in 627ac27a0ecSDave Kleikamp * the same blockgroup. But we want files which are in a different 628ac27a0ecSDave Kleikamp * directory which shares a blockgroup with our parent to land in a 629ac27a0ecSDave Kleikamp * different blockgroup. 630ac27a0ecSDave Kleikamp * 631ac27a0ecSDave Kleikamp * So add our directory's i_ino into the starting point for the hash. 632ac27a0ecSDave Kleikamp */ 6332aa9fc4cSAvantika Mathur *group = (*group + parent->i_ino) % ngroups; 634ac27a0ecSDave Kleikamp 635ac27a0ecSDave Kleikamp /* 636ac27a0ecSDave Kleikamp * Use a quadratic hash to find a group with a free inode and some free 637ac27a0ecSDave Kleikamp * blocks. 638ac27a0ecSDave Kleikamp */ 639ac27a0ecSDave Kleikamp for (i = 1; i < ngroups; i <<= 1) { 6402aa9fc4cSAvantika Mathur *group += i; 6412aa9fc4cSAvantika Mathur if (*group >= ngroups) 6422aa9fc4cSAvantika Mathur *group -= ngroups; 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 /* 650ac27a0ecSDave Kleikamp * That failed: try linear search for a free inode, even if that group 651ac27a0ecSDave Kleikamp * has no free blocks. 652ac27a0ecSDave Kleikamp */ 6532aa9fc4cSAvantika Mathur *group = parent_group; 654ac27a0ecSDave Kleikamp for (i = 0; i < ngroups; i++) { 6552aa9fc4cSAvantika Mathur if (++*group >= ngroups) 6562aa9fc4cSAvantika Mathur *group = 0; 6572aa9fc4cSAvantika Mathur desc = ext4_get_group_desc(sb, *group, NULL); 658560671a0SAneesh Kumar K.V if (desc && ext4_free_inodes_count(sb, desc)) 6592aa9fc4cSAvantika Mathur return 0; 660ac27a0ecSDave Kleikamp } 661ac27a0ecSDave Kleikamp 662ac27a0ecSDave Kleikamp return -1; 663ac27a0ecSDave Kleikamp } 664ac27a0ecSDave Kleikamp 665ac27a0ecSDave Kleikamp /* 66619883bd9STheodore Ts'o * In no journal mode, if an inode has recently been deleted, we want 66719883bd9STheodore Ts'o * to avoid reusing it until we're reasonably sure the inode table 66819883bd9STheodore Ts'o * block has been written back to disk. (Yes, these values are 66919883bd9STheodore Ts'o * somewhat arbitrary...) 67019883bd9STheodore Ts'o */ 671a17a9d93STheodore Ts'o #define RECENTCY_MIN 60 672b5f51573SAndreas Dilger #define RECENTCY_DIRTY 300 67319883bd9STheodore Ts'o 67419883bd9STheodore Ts'o static int recently_deleted(struct super_block *sb, ext4_group_t group, int ino) 67519883bd9STheodore Ts'o { 67619883bd9STheodore Ts'o struct ext4_group_desc *gdp; 67719883bd9STheodore Ts'o struct ext4_inode *raw_inode; 67819883bd9STheodore Ts'o struct buffer_head *bh; 67919883bd9STheodore Ts'o int inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 680b5f51573SAndreas Dilger int offset, ret = 0; 681b5f51573SAndreas Dilger int recentcy = RECENTCY_MIN; 682b5f51573SAndreas Dilger u32 dtime, now; 68319883bd9STheodore Ts'o 68419883bd9STheodore Ts'o gdp = ext4_get_group_desc(sb, group, NULL); 68519883bd9STheodore Ts'o if (unlikely(!gdp)) 68619883bd9STheodore Ts'o return 0; 68719883bd9STheodore Ts'o 6884f9d956dSJan Kara bh = sb_find_get_block(sb, ext4_inode_table(sb, gdp) + 68919883bd9STheodore Ts'o (ino / inodes_per_block)); 6904f9d956dSJan Kara if (!bh || !buffer_uptodate(bh)) 69119883bd9STheodore Ts'o /* 69219883bd9STheodore Ts'o * If the block is not in the buffer cache, then it 69319883bd9STheodore Ts'o * must have been written out. 69419883bd9STheodore Ts'o */ 69519883bd9STheodore Ts'o goto out; 69619883bd9STheodore Ts'o 69719883bd9STheodore Ts'o offset = (ino % inodes_per_block) * EXT4_INODE_SIZE(sb); 69819883bd9STheodore Ts'o raw_inode = (struct ext4_inode *) (bh->b_data + offset); 699b5f51573SAndreas Dilger 700b5f51573SAndreas Dilger /* i_dtime is only 32 bits on disk, but we only care about relative 701b5f51573SAndreas Dilger * times in the range of a few minutes (i.e. long enough to sync a 702b5f51573SAndreas Dilger * recently-deleted inode to disk), so using the low 32 bits of the 703b5f51573SAndreas Dilger * clock (a 68 year range) is enough, see time_before32() */ 70419883bd9STheodore Ts'o dtime = le32_to_cpu(raw_inode->i_dtime); 705b5f51573SAndreas Dilger now = ktime_get_real_seconds(); 70619883bd9STheodore Ts'o if (buffer_dirty(bh)) 70719883bd9STheodore Ts'o recentcy += RECENTCY_DIRTY; 70819883bd9STheodore Ts'o 709b5f51573SAndreas Dilger if (dtime && time_before32(dtime, now) && 710b5f51573SAndreas Dilger time_before32(now, dtime + recentcy)) 71119883bd9STheodore Ts'o ret = 1; 71219883bd9STheodore Ts'o out: 71319883bd9STheodore Ts'o brelse(bh); 71419883bd9STheodore Ts'o return ret; 71519883bd9STheodore Ts'o } 71619883bd9STheodore Ts'o 717901ed070SWang Shilong static int find_inode_bit(struct super_block *sb, ext4_group_t group, 718901ed070SWang Shilong struct buffer_head *bitmap, unsigned long *ino) 719901ed070SWang Shilong { 720d05466b2SJan Kara bool check_recently_deleted = EXT4_SB(sb)->s_journal == NULL; 721d05466b2SJan Kara unsigned long recently_deleted_ino = EXT4_INODES_PER_GROUP(sb); 722d05466b2SJan Kara 723901ed070SWang Shilong next: 724901ed070SWang Shilong *ino = ext4_find_next_zero_bit((unsigned long *) 725901ed070SWang Shilong bitmap->b_data, 726901ed070SWang Shilong EXT4_INODES_PER_GROUP(sb), *ino); 727901ed070SWang Shilong if (*ino >= EXT4_INODES_PER_GROUP(sb)) 728d05466b2SJan Kara goto not_found; 729901ed070SWang Shilong 730d05466b2SJan Kara if (check_recently_deleted && recently_deleted(sb, group, *ino)) { 731d05466b2SJan Kara recently_deleted_ino = *ino; 732901ed070SWang Shilong *ino = *ino + 1; 733901ed070SWang Shilong if (*ino < EXT4_INODES_PER_GROUP(sb)) 734901ed070SWang Shilong goto next; 735d05466b2SJan Kara goto not_found; 736901ed070SWang Shilong } 737d05466b2SJan Kara return 1; 738d05466b2SJan Kara not_found: 739d05466b2SJan Kara if (recently_deleted_ino >= EXT4_INODES_PER_GROUP(sb)) 740d05466b2SJan Kara return 0; 741d05466b2SJan Kara /* 742d05466b2SJan Kara * Not reusing recently deleted inodes is mostly a preference. We don't 743d05466b2SJan Kara * want to report ENOSPC or skew allocation patterns because of that. 744d05466b2SJan Kara * So return even recently deleted inode if we could find better in the 745d05466b2SJan Kara * given range. 746d05466b2SJan Kara */ 747d05466b2SJan Kara *ino = recently_deleted_ino; 748901ed070SWang Shilong return 1; 749901ed070SWang Shilong } 750901ed070SWang Shilong 7518016e29fSHarshad Shirwadkar int ext4_mark_inode_used(struct super_block *sb, int ino) 7528016e29fSHarshad Shirwadkar { 7538016e29fSHarshad Shirwadkar unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count); 7548016e29fSHarshad Shirwadkar struct buffer_head *inode_bitmap_bh = NULL, *group_desc_bh = NULL; 7558016e29fSHarshad Shirwadkar struct ext4_group_desc *gdp; 7568016e29fSHarshad Shirwadkar ext4_group_t group; 7578016e29fSHarshad Shirwadkar int bit; 7588016e29fSHarshad Shirwadkar int err = -EFSCORRUPTED; 7598016e29fSHarshad Shirwadkar 7608016e29fSHarshad Shirwadkar if (ino < EXT4_FIRST_INO(sb) || ino > max_ino) 7618016e29fSHarshad Shirwadkar goto out; 7628016e29fSHarshad Shirwadkar 7638016e29fSHarshad Shirwadkar group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); 7648016e29fSHarshad Shirwadkar bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); 7658016e29fSHarshad Shirwadkar inode_bitmap_bh = ext4_read_inode_bitmap(sb, group); 7668016e29fSHarshad Shirwadkar if (IS_ERR(inode_bitmap_bh)) 7678016e29fSHarshad Shirwadkar return PTR_ERR(inode_bitmap_bh); 7688016e29fSHarshad Shirwadkar 7698016e29fSHarshad Shirwadkar if (ext4_test_bit(bit, inode_bitmap_bh->b_data)) { 7708016e29fSHarshad Shirwadkar err = 0; 7718016e29fSHarshad Shirwadkar goto out; 7728016e29fSHarshad Shirwadkar } 7738016e29fSHarshad Shirwadkar 7748016e29fSHarshad Shirwadkar gdp = ext4_get_group_desc(sb, group, &group_desc_bh); 7758016e29fSHarshad Shirwadkar if (!gdp || !group_desc_bh) { 7768016e29fSHarshad Shirwadkar err = -EINVAL; 7778016e29fSHarshad Shirwadkar goto out; 7788016e29fSHarshad Shirwadkar } 7798016e29fSHarshad Shirwadkar 7808016e29fSHarshad Shirwadkar ext4_set_bit(bit, inode_bitmap_bh->b_data); 7818016e29fSHarshad Shirwadkar 7828016e29fSHarshad Shirwadkar BUFFER_TRACE(inode_bitmap_bh, "call ext4_handle_dirty_metadata"); 7838016e29fSHarshad Shirwadkar err = ext4_handle_dirty_metadata(NULL, NULL, inode_bitmap_bh); 7848016e29fSHarshad Shirwadkar if (err) { 7858016e29fSHarshad Shirwadkar ext4_std_error(sb, err); 7868016e29fSHarshad Shirwadkar goto out; 7878016e29fSHarshad Shirwadkar } 7888016e29fSHarshad Shirwadkar err = sync_dirty_buffer(inode_bitmap_bh); 7898016e29fSHarshad Shirwadkar if (err) { 7908016e29fSHarshad Shirwadkar ext4_std_error(sb, err); 7918016e29fSHarshad Shirwadkar goto out; 7928016e29fSHarshad Shirwadkar } 7938016e29fSHarshad Shirwadkar 7948016e29fSHarshad Shirwadkar /* We may have to initialize the block bitmap if it isn't already */ 7958016e29fSHarshad Shirwadkar if (ext4_has_group_desc_csum(sb) && 7968016e29fSHarshad Shirwadkar gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { 7978016e29fSHarshad Shirwadkar struct buffer_head *block_bitmap_bh; 7988016e29fSHarshad Shirwadkar 7998016e29fSHarshad Shirwadkar block_bitmap_bh = ext4_read_block_bitmap(sb, group); 8008016e29fSHarshad Shirwadkar if (IS_ERR(block_bitmap_bh)) { 8018016e29fSHarshad Shirwadkar err = PTR_ERR(block_bitmap_bh); 8028016e29fSHarshad Shirwadkar goto out; 8038016e29fSHarshad Shirwadkar } 8048016e29fSHarshad Shirwadkar 8058016e29fSHarshad Shirwadkar BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap"); 8068016e29fSHarshad Shirwadkar err = ext4_handle_dirty_metadata(NULL, NULL, block_bitmap_bh); 8078016e29fSHarshad Shirwadkar sync_dirty_buffer(block_bitmap_bh); 8088016e29fSHarshad Shirwadkar 8098016e29fSHarshad Shirwadkar /* recheck and clear flag under lock if we still need to */ 8108016e29fSHarshad Shirwadkar ext4_lock_group(sb, group); 8118016e29fSHarshad Shirwadkar if (ext4_has_group_desc_csum(sb) && 8128016e29fSHarshad Shirwadkar (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) { 8138016e29fSHarshad Shirwadkar gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT); 8148016e29fSHarshad Shirwadkar ext4_free_group_clusters_set(sb, gdp, 8158016e29fSHarshad Shirwadkar ext4_free_clusters_after_init(sb, group, gdp)); 8168016e29fSHarshad Shirwadkar ext4_block_bitmap_csum_set(sb, group, gdp, 8178016e29fSHarshad Shirwadkar block_bitmap_bh); 8188016e29fSHarshad Shirwadkar ext4_group_desc_csum_set(sb, group, gdp); 8198016e29fSHarshad Shirwadkar } 8208016e29fSHarshad Shirwadkar ext4_unlock_group(sb, group); 8218016e29fSHarshad Shirwadkar brelse(block_bitmap_bh); 8228016e29fSHarshad Shirwadkar 8238016e29fSHarshad Shirwadkar if (err) { 8248016e29fSHarshad Shirwadkar ext4_std_error(sb, err); 8258016e29fSHarshad Shirwadkar goto out; 8268016e29fSHarshad Shirwadkar } 8278016e29fSHarshad Shirwadkar } 8288016e29fSHarshad Shirwadkar 8298016e29fSHarshad Shirwadkar /* Update the relevant bg descriptor fields */ 8308016e29fSHarshad Shirwadkar if (ext4_has_group_desc_csum(sb)) { 8318016e29fSHarshad Shirwadkar int free; 8328016e29fSHarshad Shirwadkar 8338016e29fSHarshad Shirwadkar ext4_lock_group(sb, group); /* while we modify the bg desc */ 8348016e29fSHarshad Shirwadkar free = EXT4_INODES_PER_GROUP(sb) - 8358016e29fSHarshad Shirwadkar ext4_itable_unused_count(sb, gdp); 8368016e29fSHarshad Shirwadkar if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) { 8378016e29fSHarshad Shirwadkar gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT); 8388016e29fSHarshad Shirwadkar free = 0; 8398016e29fSHarshad Shirwadkar } 8408016e29fSHarshad Shirwadkar 8418016e29fSHarshad Shirwadkar /* 8428016e29fSHarshad Shirwadkar * Check the relative inode number against the last used 8438016e29fSHarshad Shirwadkar * relative inode number in this group. if it is greater 8448016e29fSHarshad Shirwadkar * we need to update the bg_itable_unused count 8458016e29fSHarshad Shirwadkar */ 8468016e29fSHarshad Shirwadkar if (bit >= free) 8478016e29fSHarshad Shirwadkar ext4_itable_unused_set(sb, gdp, 8488016e29fSHarshad Shirwadkar (EXT4_INODES_PER_GROUP(sb) - bit - 1)); 8498016e29fSHarshad Shirwadkar } else { 8508016e29fSHarshad Shirwadkar ext4_lock_group(sb, group); 8518016e29fSHarshad Shirwadkar } 8528016e29fSHarshad Shirwadkar 8538016e29fSHarshad Shirwadkar ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1); 8548016e29fSHarshad Shirwadkar if (ext4_has_group_desc_csum(sb)) { 8558016e29fSHarshad Shirwadkar ext4_inode_bitmap_csum_set(sb, group, gdp, inode_bitmap_bh, 8568016e29fSHarshad Shirwadkar EXT4_INODES_PER_GROUP(sb) / 8); 8578016e29fSHarshad Shirwadkar ext4_group_desc_csum_set(sb, group, gdp); 8588016e29fSHarshad Shirwadkar } 8598016e29fSHarshad Shirwadkar 8608016e29fSHarshad Shirwadkar ext4_unlock_group(sb, group); 8618016e29fSHarshad Shirwadkar err = ext4_handle_dirty_metadata(NULL, NULL, group_desc_bh); 8628016e29fSHarshad Shirwadkar sync_dirty_buffer(group_desc_bh); 8638016e29fSHarshad Shirwadkar out: 8648016e29fSHarshad Shirwadkar return err; 8658016e29fSHarshad Shirwadkar } 8668016e29fSHarshad Shirwadkar 867177cc0e7SEric Biggers static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode, 868177cc0e7SEric Biggers bool encrypt) 869177cc0e7SEric Biggers { 870177cc0e7SEric Biggers struct super_block *sb = dir->i_sb; 871177cc0e7SEric Biggers int nblocks = 0; 872177cc0e7SEric Biggers #ifdef CONFIG_EXT4_FS_POSIX_ACL 873cac2f8b8SChristian Brauner struct posix_acl *p = get_inode_acl(dir, ACL_TYPE_DEFAULT); 874177cc0e7SEric Biggers 875177cc0e7SEric Biggers if (IS_ERR(p)) 876177cc0e7SEric Biggers return PTR_ERR(p); 877177cc0e7SEric Biggers if (p) { 878177cc0e7SEric Biggers int acl_size = p->a_count * sizeof(ext4_acl_entry); 879177cc0e7SEric Biggers 880177cc0e7SEric Biggers nblocks += (S_ISDIR(mode) ? 2 : 1) * 881177cc0e7SEric Biggers __ext4_xattr_set_credits(sb, NULL /* inode */, 882177cc0e7SEric Biggers NULL /* block_bh */, acl_size, 883177cc0e7SEric Biggers true /* is_create */); 884177cc0e7SEric Biggers posix_acl_release(p); 885177cc0e7SEric Biggers } 886177cc0e7SEric Biggers #endif 887177cc0e7SEric Biggers 888177cc0e7SEric Biggers #ifdef CONFIG_SECURITY 889177cc0e7SEric Biggers { 890177cc0e7SEric Biggers int num_security_xattrs = 1; 891177cc0e7SEric Biggers 892177cc0e7SEric Biggers #ifdef CONFIG_INTEGRITY 893177cc0e7SEric Biggers num_security_xattrs++; 894177cc0e7SEric Biggers #endif 895177cc0e7SEric Biggers /* 896177cc0e7SEric Biggers * We assume that security xattrs are never more than 1k. 897177cc0e7SEric Biggers * In practice they are under 128 bytes. 898177cc0e7SEric Biggers */ 899177cc0e7SEric Biggers nblocks += num_security_xattrs * 900177cc0e7SEric Biggers __ext4_xattr_set_credits(sb, NULL /* inode */, 901177cc0e7SEric Biggers NULL /* block_bh */, 1024, 902177cc0e7SEric Biggers true /* is_create */); 903177cc0e7SEric Biggers } 904177cc0e7SEric Biggers #endif 905177cc0e7SEric Biggers if (encrypt) 906177cc0e7SEric Biggers nblocks += __ext4_xattr_set_credits(sb, 907177cc0e7SEric Biggers NULL /* inode */, 908177cc0e7SEric Biggers NULL /* block_bh */, 909177cc0e7SEric Biggers FSCRYPT_SET_CONTEXT_MAX_SIZE, 910177cc0e7SEric Biggers true /* is_create */); 911177cc0e7SEric Biggers return nblocks; 912177cc0e7SEric Biggers } 913177cc0e7SEric Biggers 91419883bd9STheodore Ts'o /* 915ac27a0ecSDave Kleikamp * There are two policies for allocating an inode. If the new inode is 916ac27a0ecSDave Kleikamp * a directory, then a forward search is made for a block group with both 917ac27a0ecSDave Kleikamp * free space and a low directory-to-inode ratio; if that fails, then of 918ac27a0ecSDave Kleikamp * the groups with above-average free space, that group with the fewest 919ac27a0ecSDave Kleikamp * directories already is chosen. 920ac27a0ecSDave Kleikamp * 921ac27a0ecSDave Kleikamp * For other inodes, search forward from the parent directory's block 922ac27a0ecSDave Kleikamp * group to find a free inode. 923ac27a0ecSDave Kleikamp */ 924f2d40141SChristian Brauner struct inode *__ext4_new_inode(struct mnt_idmap *idmap, 92514f3db55SChristian Brauner handle_t *handle, struct inode *dir, 9261139575aSTheodore Ts'o umode_t mode, const struct qstr *qstr, 9271b917ed8STahsin Erdogan __u32 goal, uid_t *owner, __u32 i_flags, 9281b917ed8STahsin Erdogan int handle_type, unsigned int line_no, 9291b917ed8STahsin Erdogan int nblocks) 930ac27a0ecSDave Kleikamp { 931ac27a0ecSDave Kleikamp struct super_block *sb; 9323300bedaSAneesh Kumar K.V struct buffer_head *inode_bitmap_bh = NULL; 9333300bedaSAneesh Kumar K.V struct buffer_head *group_desc_bh; 9348df9675fSTheodore Ts'o ext4_group_t ngroups, group = 0; 935ac27a0ecSDave Kleikamp unsigned long ino = 0; 936ac27a0ecSDave Kleikamp struct inode *inode; 937617ba13bSMingming Cao struct ext4_group_desc *gdp = NULL; 938617ba13bSMingming Cao struct ext4_inode_info *ei; 939617ba13bSMingming Cao struct ext4_sb_info *sbi; 940a7cdadeeSJan Kara int ret2, err; 941ac27a0ecSDave Kleikamp struct inode *ret; 9422aa9fc4cSAvantika Mathur ext4_group_t i; 943772cb7c8SJose R. Santos ext4_group_t flex_group; 9448016e29fSHarshad Shirwadkar struct ext4_group_info *grp = NULL; 94502ce5316SEric Biggers bool encrypt = false; 946ac27a0ecSDave Kleikamp 947ac27a0ecSDave Kleikamp /* Cannot create files in a deleted directory */ 948ac27a0ecSDave Kleikamp if (!dir || !dir->i_nlink) 949ac27a0ecSDave Kleikamp return ERR_PTR(-EPERM); 950ac27a0ecSDave Kleikamp 951af65207cSTahsin Erdogan sb = dir->i_sb; 952af65207cSTahsin Erdogan sbi = EXT4_SB(sb); 953af65207cSTahsin Erdogan 954af65207cSTahsin Erdogan if (unlikely(ext4_forced_shutdown(sbi))) 9550db1ff22STheodore Ts'o return ERR_PTR(-EIO); 9560db1ff22STheodore Ts'o 9578df9675fSTheodore Ts'o ngroups = ext4_get_groups_count(sb); 9589bffad1eSTheodore Ts'o trace_ext4_request_inode(dir, mode); 959ac27a0ecSDave Kleikamp inode = new_inode(sb); 960ac27a0ecSDave Kleikamp if (!inode) 961ac27a0ecSDave Kleikamp return ERR_PTR(-ENOMEM); 962617ba13bSMingming Cao ei = EXT4_I(inode); 963772cb7c8SJose R. Santos 964eb9cc7e1SJan Kara /* 965b8a07463SAdam Buchbinder * Initialize owners and quota early so that we don't have to account 966eb9cc7e1SJan Kara * for quota initialization worst case in standard inode creating 967eb9cc7e1SJan Kara * transaction 968eb9cc7e1SJan Kara */ 969eb9cc7e1SJan Kara if (owner) { 970eb9cc7e1SJan Kara inode->i_mode = mode; 971eb9cc7e1SJan Kara i_uid_write(inode, owner[0]); 972eb9cc7e1SJan Kara i_gid_write(inode, owner[1]); 973eb9cc7e1SJan Kara } else if (test_opt(sb, GRPID)) { 974eb9cc7e1SJan Kara inode->i_mode = mode; 975*c14329d3SChristian Brauner inode_fsuid_set(inode, idmap); 976eb9cc7e1SJan Kara inode->i_gid = dir->i_gid; 977eb9cc7e1SJan Kara } else 978f2d40141SChristian Brauner inode_init_owner(idmap, inode, dir, mode); 979040cb378SLi Xi 9800b7b7779SKaho Ng if (ext4_has_feature_project(sb) && 981040cb378SLi Xi ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT)) 982040cb378SLi Xi ei->i_projid = EXT4_I(dir)->i_projid; 983040cb378SLi Xi else 984040cb378SLi Xi ei->i_projid = make_kprojid(&init_user_ns, EXT4_DEF_PROJID); 985040cb378SLi Xi 98602ce5316SEric Biggers if (!(i_flags & EXT4_EA_INODE_FL)) { 98702ce5316SEric Biggers err = fscrypt_prepare_new_inode(dir, inode, &encrypt); 98802ce5316SEric Biggers if (err) 98902ce5316SEric Biggers goto out; 99002ce5316SEric Biggers } 99102ce5316SEric Biggers 992a7cdadeeSJan Kara err = dquot_initialize(inode); 993a7cdadeeSJan Kara if (err) 994a7cdadeeSJan Kara goto out; 995eb9cc7e1SJan Kara 99602ce5316SEric Biggers if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) { 99702ce5316SEric Biggers ret2 = ext4_xattr_credits_for_new_inode(dir, mode, encrypt); 99802ce5316SEric Biggers if (ret2 < 0) { 99902ce5316SEric Biggers err = ret2; 100002ce5316SEric Biggers goto out; 100102ce5316SEric Biggers } 100202ce5316SEric Biggers nblocks += ret2; 100302ce5316SEric Biggers } 100402ce5316SEric Biggers 100511013911SAndreas Dilger if (!goal) 100611013911SAndreas Dilger goal = sbi->s_inode_goal; 100711013911SAndreas Dilger 1008e6462869SJohann Lombardi if (goal && goal <= le32_to_cpu(sbi->s_es->s_inodes_count)) { 100911013911SAndreas Dilger group = (goal - 1) / EXT4_INODES_PER_GROUP(sb); 101011013911SAndreas Dilger ino = (goal - 1) % EXT4_INODES_PER_GROUP(sb); 101111013911SAndreas Dilger ret2 = 0; 101211013911SAndreas Dilger goto got_group; 101311013911SAndreas Dilger } 101411013911SAndreas Dilger 10154113c4caSLukas Czerner if (S_ISDIR(mode)) 1016f157a4aaSTheodore Ts'o ret2 = find_group_orlov(sb, dir, &group, mode, qstr); 10174113c4caSLukas Czerner else 1018a4912123STheodore Ts'o ret2 = find_group_other(sb, dir, &group, mode); 1019ac27a0ecSDave Kleikamp 1020772cb7c8SJose R. Santos got_group: 1021a4912123STheodore Ts'o EXT4_I(dir)->i_last_alloc_group = group; 1022ac27a0ecSDave Kleikamp err = -ENOSPC; 10232aa9fc4cSAvantika Mathur if (ret2 == -1) 1024ac27a0ecSDave Kleikamp goto out; 1025ac27a0ecSDave Kleikamp 1026119c0d44STheodore Ts'o /* 1027119c0d44STheodore Ts'o * Normally we will only go through one pass of this loop, 1028119c0d44STheodore Ts'o * unless we get unlucky and it turns out the group we selected 1029119c0d44STheodore Ts'o * had its last inode grabbed by someone else. 1030119c0d44STheodore Ts'o */ 103111013911SAndreas Dilger for (i = 0; i < ngroups; i++, ino = 0) { 1032ac27a0ecSDave Kleikamp err = -EIO; 1033ac27a0ecSDave Kleikamp 10343300bedaSAneesh Kumar K.V gdp = ext4_get_group_desc(sb, group, &group_desc_bh); 1035ac27a0ecSDave Kleikamp if (!gdp) 1036eb9cc7e1SJan Kara goto out; 1037ac27a0ecSDave Kleikamp 1038f2a09af6SYongqiang Yang /* 1039f2a09af6SYongqiang Yang * Check free inodes count before loading bitmap. 1040f2a09af6SYongqiang Yang */ 10412fe435d8SWang Shilong if (ext4_free_inodes_count(sb, gdp) == 0) 10422fe435d8SWang Shilong goto next_group; 1043f2a09af6SYongqiang Yang 10448016e29fSHarshad Shirwadkar if (!(sbi->s_mount_state & EXT4_FC_REPLAY)) { 104587a39389SDarrick J. Wong grp = ext4_get_group_info(sb, group); 10468016e29fSHarshad Shirwadkar /* 10478016e29fSHarshad Shirwadkar * Skip groups with already-known suspicious inode 10488016e29fSHarshad Shirwadkar * tables 10498016e29fSHarshad Shirwadkar */ 10502fe435d8SWang Shilong if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) 10512fe435d8SWang Shilong goto next_group; 10528016e29fSHarshad Shirwadkar } 105387a39389SDarrick J. Wong 10543300bedaSAneesh Kumar K.V brelse(inode_bitmap_bh); 10553300bedaSAneesh Kumar K.V inode_bitmap_bh = ext4_read_inode_bitmap(sb, group); 105687a39389SDarrick J. Wong /* Skip groups with suspicious inode tables */ 10578016e29fSHarshad Shirwadkar if (((!(sbi->s_mount_state & EXT4_FC_REPLAY)) 10588016e29fSHarshad Shirwadkar && EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) || 10599008a58eSDarrick J. Wong IS_ERR(inode_bitmap_bh)) { 10609008a58eSDarrick J. Wong inode_bitmap_bh = NULL; 10612fe435d8SWang Shilong goto next_group; 106287a39389SDarrick J. Wong } 1063ac27a0ecSDave Kleikamp 1064ac27a0ecSDave Kleikamp repeat_in_this_group: 1065901ed070SWang Shilong ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino); 1066901ed070SWang Shilong if (!ret2) 1067a34eb503STheodore Ts'o goto next_group; 1068901ed070SWang Shilong 1069119c0d44STheodore Ts'o if (group == 0 && (ino + 1) < EXT4_FIRST_INO(sb)) { 1070119c0d44STheodore Ts'o ext4_error(sb, "reserved inode found cleared - " 1071119c0d44STheodore Ts'o "inode=%lu", ino + 1); 1072206f6d55SWang Shilong ext4_mark_group_bitmap_corrupted(sb, group, 1073206f6d55SWang Shilong EXT4_GROUP_INFO_IBITMAP_CORRUPT); 10742fe435d8SWang Shilong goto next_group; 1075119c0d44STheodore Ts'o } 1076901ed070SWang Shilong 10778016e29fSHarshad Shirwadkar if ((!(sbi->s_mount_state & EXT4_FC_REPLAY)) && !handle) { 10781139575aSTheodore Ts'o BUG_ON(nblocks <= 0); 10795f3e2403Schangfengnan handle = __ext4_journal_start_sb(NULL, dir->i_sb, 10805f3e2403Schangfengnan line_no, handle_type, nblocks, 0, 1081a70fd5acSyangerkun ext4_trans_default_revoke_credits(sb)); 10821139575aSTheodore Ts'o if (IS_ERR(handle)) { 10831139575aSTheodore Ts'o err = PTR_ERR(handle); 1084eb9cc7e1SJan Kara ext4_std_error(sb, err); 1085eb9cc7e1SJan Kara goto out; 10861139575aSTheodore Ts'o } 10871139575aSTheodore Ts'o } 1088ffb5387eSEric Sandeen BUFFER_TRACE(inode_bitmap_bh, "get_write_access"); 1089188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, inode_bitmap_bh, 1090188c299eSJan Kara EXT4_JTR_NONE); 1091eb9cc7e1SJan Kara if (err) { 1092eb9cc7e1SJan Kara ext4_std_error(sb, err); 1093eb9cc7e1SJan Kara goto out; 1094eb9cc7e1SJan Kara } 1095119c0d44STheodore Ts'o ext4_lock_group(sb, group); 1096119c0d44STheodore Ts'o ret2 = ext4_test_and_set_bit(ino, inode_bitmap_bh->b_data); 1097901ed070SWang Shilong if (ret2) { 1098901ed070SWang Shilong /* Someone already took the bit. Repeat the search 1099901ed070SWang Shilong * with lock held. 1100901ed070SWang Shilong */ 1101901ed070SWang Shilong ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino); 1102901ed070SWang Shilong if (ret2) { 1103901ed070SWang Shilong ext4_set_bit(ino, inode_bitmap_bh->b_data); 1104901ed070SWang Shilong ret2 = 0; 1105901ed070SWang Shilong } else { 1106901ed070SWang Shilong ret2 = 1; /* we didn't grab the inode */ 1107901ed070SWang Shilong } 1108901ed070SWang Shilong } 1109119c0d44STheodore Ts'o ext4_unlock_group(sb, group); 1110119c0d44STheodore Ts'o ino++; /* the inode bitmap is zero-based */ 1111119c0d44STheodore Ts'o if (!ret2) 1112119c0d44STheodore Ts'o goto got; /* we grabbed the inode! */ 1113901ed070SWang Shilong 1114119c0d44STheodore Ts'o if (ino < EXT4_INODES_PER_GROUP(sb)) 1115119c0d44STheodore Ts'o goto repeat_in_this_group; 1116a34eb503STheodore Ts'o next_group: 1117a34eb503STheodore Ts'o if (++group == ngroups) 1118a34eb503STheodore Ts'o group = 0; 1119ac27a0ecSDave Kleikamp } 1120ac27a0ecSDave Kleikamp err = -ENOSPC; 1121ac27a0ecSDave Kleikamp goto out; 1122ac27a0ecSDave Kleikamp 1123ac27a0ecSDave Kleikamp got: 1124ffb5387eSEric Sandeen BUFFER_TRACE(inode_bitmap_bh, "call ext4_handle_dirty_metadata"); 1125ffb5387eSEric Sandeen err = ext4_handle_dirty_metadata(handle, NULL, inode_bitmap_bh); 1126eb9cc7e1SJan Kara if (err) { 1127eb9cc7e1SJan Kara ext4_std_error(sb, err); 1128eb9cc7e1SJan Kara goto out; 1129eb9cc7e1SJan Kara } 1130ffb5387eSEric Sandeen 113161c219f5STheodore Ts'o BUFFER_TRACE(group_desc_bh, "get_write_access"); 1132188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, group_desc_bh, 1133188c299eSJan Kara EXT4_JTR_NONE); 113461c219f5STheodore Ts'o if (err) { 113561c219f5STheodore Ts'o ext4_std_error(sb, err); 113661c219f5STheodore Ts'o goto out; 113761c219f5STheodore Ts'o } 113861c219f5STheodore Ts'o 1139717d50e4SAndreas Dilger /* We may have to initialize the block bitmap if it isn't already */ 1140feb0ab32SDarrick J. Wong if (ext4_has_group_desc_csum(sb) && 1141717d50e4SAndreas Dilger gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { 11423300bedaSAneesh Kumar K.V struct buffer_head *block_bitmap_bh; 1143717d50e4SAndreas Dilger 11443300bedaSAneesh Kumar K.V block_bitmap_bh = ext4_read_block_bitmap(sb, group); 11459008a58eSDarrick J. Wong if (IS_ERR(block_bitmap_bh)) { 11469008a58eSDarrick J. Wong err = PTR_ERR(block_bitmap_bh); 1147599a9b77SJan Kara goto out; 1148599a9b77SJan Kara } 11493300bedaSAneesh Kumar K.V BUFFER_TRACE(block_bitmap_bh, "get block bitmap access"); 1150188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, block_bitmap_bh, 1151188c299eSJan Kara EXT4_JTR_NONE); 1152717d50e4SAndreas Dilger if (err) { 11533300bedaSAneesh Kumar K.V brelse(block_bitmap_bh); 1154eb9cc7e1SJan Kara ext4_std_error(sb, err); 1155eb9cc7e1SJan Kara goto out; 1156717d50e4SAndreas Dilger } 1157717d50e4SAndreas Dilger 1158fd034a84STheodore Ts'o BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap"); 1159fd034a84STheodore Ts'o err = ext4_handle_dirty_metadata(handle, NULL, block_bitmap_bh); 1160fd034a84STheodore Ts'o 1161717d50e4SAndreas Dilger /* recheck and clear flag under lock if we still need to */ 1162fd034a84STheodore Ts'o ext4_lock_group(sb, group); 11638844618dSTheodore Ts'o if (ext4_has_group_desc_csum(sb) && 11648844618dSTheodore Ts'o (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) { 11653300bedaSAneesh Kumar K.V gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT); 1166021b65bbSTheodore Ts'o ext4_free_group_clusters_set(sb, gdp, 1167cff1dfd7STheodore Ts'o ext4_free_clusters_after_init(sb, group, gdp)); 1168fa77dcfaSDarrick J. Wong ext4_block_bitmap_csum_set(sb, group, gdp, 116979f1ba49STao Ma block_bitmap_bh); 1170feb0ab32SDarrick J. Wong ext4_group_desc_csum_set(sb, group, gdp); 1171717d50e4SAndreas Dilger } 1172955ce5f5SAneesh Kumar K.V ext4_unlock_group(sb, group); 1173aeb1e5d6STheodore Ts'o brelse(block_bitmap_bh); 1174717d50e4SAndreas Dilger 1175eb9cc7e1SJan Kara if (err) { 1176eb9cc7e1SJan Kara ext4_std_error(sb, err); 1177eb9cc7e1SJan Kara goto out; 1178eb9cc7e1SJan Kara } 1179717d50e4SAndreas Dilger } 1180119c0d44STheodore Ts'o 1181119c0d44STheodore Ts'o /* Update the relevant bg descriptor fields */ 118241a246d1SDarrick J. Wong if (ext4_has_group_desc_csum(sb)) { 1183119c0d44STheodore Ts'o int free; 11848016e29fSHarshad Shirwadkar struct ext4_group_info *grp = NULL; 1185119c0d44STheodore Ts'o 11868016e29fSHarshad Shirwadkar if (!(sbi->s_mount_state & EXT4_FC_REPLAY)) { 11878016e29fSHarshad Shirwadkar grp = ext4_get_group_info(sb, group); 11888016e29fSHarshad Shirwadkar down_read(&grp->alloc_sem); /* 11898016e29fSHarshad Shirwadkar * protect vs itable 11908016e29fSHarshad Shirwadkar * lazyinit 11918016e29fSHarshad Shirwadkar */ 11928016e29fSHarshad Shirwadkar } 1193119c0d44STheodore Ts'o ext4_lock_group(sb, group); /* while we modify the bg desc */ 1194119c0d44STheodore Ts'o free = EXT4_INODES_PER_GROUP(sb) - 1195119c0d44STheodore Ts'o ext4_itable_unused_count(sb, gdp); 1196119c0d44STheodore Ts'o if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) { 1197119c0d44STheodore Ts'o gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT); 1198119c0d44STheodore Ts'o free = 0; 1199119c0d44STheodore Ts'o } 1200119c0d44STheodore Ts'o /* 1201119c0d44STheodore Ts'o * Check the relative inode number against the last used 1202119c0d44STheodore Ts'o * relative inode number in this group. if it is greater 1203119c0d44STheodore Ts'o * we need to update the bg_itable_unused count 1204119c0d44STheodore Ts'o */ 1205119c0d44STheodore Ts'o if (ino > free) 1206119c0d44STheodore Ts'o ext4_itable_unused_set(sb, gdp, 1207119c0d44STheodore Ts'o (EXT4_INODES_PER_GROUP(sb) - ino)); 12088016e29fSHarshad Shirwadkar if (!(sbi->s_mount_state & EXT4_FC_REPLAY)) 1209119c0d44STheodore Ts'o up_read(&grp->alloc_sem); 12106f2e9f0eSTao Ma } else { 12116f2e9f0eSTao Ma ext4_lock_group(sb, group); 1212119c0d44STheodore Ts'o } 12136f2e9f0eSTao Ma 1214119c0d44STheodore Ts'o ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1); 1215119c0d44STheodore Ts'o if (S_ISDIR(mode)) { 1216119c0d44STheodore Ts'o ext4_used_dirs_set(sb, gdp, ext4_used_dirs_count(sb, gdp) + 1); 1217119c0d44STheodore Ts'o if (sbi->s_log_groups_per_flex) { 1218119c0d44STheodore Ts'o ext4_group_t f = ext4_flex_group(sbi, group); 1219119c0d44STheodore Ts'o 12207c990728SSuraj Jitindar Singh atomic_inc(&sbi_array_rcu_deref(sbi, s_flex_groups, 12217c990728SSuraj Jitindar Singh f)->used_dirs); 1222119c0d44STheodore Ts'o } 1223119c0d44STheodore Ts'o } 122441a246d1SDarrick J. Wong if (ext4_has_group_desc_csum(sb)) { 122541a246d1SDarrick J. Wong ext4_inode_bitmap_csum_set(sb, group, gdp, inode_bitmap_bh, 122641a246d1SDarrick J. Wong EXT4_INODES_PER_GROUP(sb) / 8); 1227feb0ab32SDarrick J. Wong ext4_group_desc_csum_set(sb, group, gdp); 1228119c0d44STheodore Ts'o } 12296f2e9f0eSTao Ma ext4_unlock_group(sb, group); 1230119c0d44STheodore Ts'o 12313300bedaSAneesh Kumar K.V BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata"); 12323300bedaSAneesh Kumar K.V err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh); 1233eb9cc7e1SJan Kara if (err) { 1234eb9cc7e1SJan Kara ext4_std_error(sb, err); 1235eb9cc7e1SJan Kara goto out; 1236eb9cc7e1SJan Kara } 1237ac27a0ecSDave Kleikamp 1238ac27a0ecSDave Kleikamp percpu_counter_dec(&sbi->s_freeinodes_counter); 1239ac27a0ecSDave Kleikamp if (S_ISDIR(mode)) 1240ac27a0ecSDave Kleikamp percpu_counter_inc(&sbi->s_dirs_counter); 1241ac27a0ecSDave Kleikamp 1242772cb7c8SJose R. Santos if (sbi->s_log_groups_per_flex) { 1243772cb7c8SJose R. Santos flex_group = ext4_flex_group(sbi, group); 12447c990728SSuraj Jitindar Singh atomic_dec(&sbi_array_rcu_deref(sbi, s_flex_groups, 12457c990728SSuraj Jitindar Singh flex_group)->free_inodes); 1246772cb7c8SJose R. Santos } 1247ac27a0ecSDave Kleikamp 1248717d50e4SAndreas Dilger inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb); 1249ac27a0ecSDave Kleikamp /* This is the optimal IO size (for stat), not the fs block size */ 1250ac27a0ecSDave Kleikamp inode->i_blocks = 0; 125195582b00SDeepa Dinamani inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode); 12527b62b293SArnd Bergmann ei->i_crtime = inode->i_mtime; 1253ac27a0ecSDave Kleikamp 1254ac27a0ecSDave Kleikamp memset(ei->i_data, 0, sizeof(ei->i_data)); 1255ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 1256ac27a0ecSDave Kleikamp ei->i_disksize = 0; 1257ac27a0ecSDave Kleikamp 12584af83508SEryu Guan /* Don't inherit extent flag from directory, amongst others. */ 12592dc6b0d4SDuane Griffin ei->i_flags = 12602dc6b0d4SDuane Griffin ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED); 12611b917ed8STahsin Erdogan ei->i_flags |= i_flags; 1262ac27a0ecSDave Kleikamp ei->i_file_acl = 0; 1263ac27a0ecSDave Kleikamp ei->i_dtime = 0; 1264ac27a0ecSDave Kleikamp ei->i_block_group = group; 1265a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 1266ac27a0ecSDave Kleikamp 1267043546e4SIra Weiny ext4_set_inode_flags(inode, true); 1268ac27a0ecSDave Kleikamp if (IS_DIRSYNC(inode)) 12690390131bSFrank Mayhar ext4_handle_sync(handle); 12706b38e842SAl Viro if (insert_inode_locked(inode) < 0) { 1271acd6ad83SJan Kara /* 1272acd6ad83SJan Kara * Likely a bitmap corruption causing inode to be allocated 1273acd6ad83SJan Kara * twice. 1274acd6ad83SJan Kara */ 1275acd6ad83SJan Kara err = -EIO; 1276eb9cc7e1SJan Kara ext4_error(sb, "failed to insert inode %lu: doubly allocated?", 1277eb9cc7e1SJan Kara inode->i_ino); 1278206f6d55SWang Shilong ext4_mark_group_bitmap_corrupted(sb, group, 1279206f6d55SWang Shilong EXT4_GROUP_INFO_IBITMAP_CORRUPT); 1280eb9cc7e1SJan Kara goto out; 12816b38e842SAl Viro } 1282a251c17aSJason A. Donenfeld inode->i_generation = get_random_u32(); 1283ac27a0ecSDave Kleikamp 1284814525f4SDarrick J. Wong /* Precompute checksum seed for inode metadata */ 12859aa5d32bSDmitry Monakhov if (ext4_has_metadata_csum(sb)) { 1286814525f4SDarrick J. Wong __u32 csum; 1287814525f4SDarrick J. Wong __le32 inum = cpu_to_le32(inode->i_ino); 1288814525f4SDarrick J. Wong __le32 gen = cpu_to_le32(inode->i_generation); 1289814525f4SDarrick J. Wong csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, 1290814525f4SDarrick J. Wong sizeof(inum)); 1291814525f4SDarrick J. Wong ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, 1292814525f4SDarrick J. Wong sizeof(gen)); 1293814525f4SDarrick J. Wong } 1294814525f4SDarrick J. Wong 1295353eb83cSTheodore Ts'o ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ 129619f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_NEW); 1297ef7f3835SKalpak Shah 129849598e04SJun Piao ei->i_extra_isize = sbi->s_want_extra_isize; 1299f08225d1STao Ma ei->i_inline_off = 0; 13004811d992STheodore Ts'o if (ext4_has_feature_inline_data(sb) && 13014811d992STheodore Ts'o (!(ei->i_flags & EXT4_DAX_FL) || S_ISDIR(mode))) 1302f08225d1STao Ma ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); 1303ac27a0ecSDave Kleikamp ret = inode; 130463936ddaSChristoph Hellwig err = dquot_alloc_inode(inode); 130563936ddaSChristoph Hellwig if (err) 1306ac27a0ecSDave Kleikamp goto fail_drop; 1307ac27a0ecSDave Kleikamp 1308aa1dca3bSEric Biggers /* 1309aa1dca3bSEric Biggers * Since the encryption xattr will always be unique, create it first so 1310aa1dca3bSEric Biggers * that it's less likely to end up in an external xattr block and 1311aa1dca3bSEric Biggers * prevent its deduplication. 1312aa1dca3bSEric Biggers */ 1313aa1dca3bSEric Biggers if (encrypt) { 131402ce5316SEric Biggers err = fscrypt_set_context(inode, handle); 1315aa1dca3bSEric Biggers if (err) 1316aa1dca3bSEric Biggers goto fail_free_drop; 1317aa1dca3bSEric Biggers } 1318aa1dca3bSEric Biggers 13191b917ed8STahsin Erdogan if (!(ei->i_flags & EXT4_EA_INODE_FL)) { 1320617ba13bSMingming Cao err = ext4_init_acl(handle, inode, dir); 1321ac27a0ecSDave Kleikamp if (err) 1322ac27a0ecSDave Kleikamp goto fail_free_drop; 1323ac27a0ecSDave Kleikamp 13242a7dba39SEric Paris err = ext4_init_security(handle, inode, dir, qstr); 1325ac27a0ecSDave Kleikamp if (err) 1326ac27a0ecSDave Kleikamp goto fail_free_drop; 1327ad47f953STahsin Erdogan } 1328ac27a0ecSDave Kleikamp 1329e2b911c5SDarrick J. Wong if (ext4_has_feature_extents(sb)) { 1330e4079a11SEric Sandeen /* set extent flag only for directory, file and normal symlink*/ 1331e65187e6SAneesh Kumar K.V if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) { 133212e9b892SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS); 1333a86c6181SAlex Tomas ext4_ext_tree_init(handle, inode); 1334a86c6181SAlex Tomas } 133542bf0383SAneesh Kumar K.V } 1336ac27a0ecSDave Kleikamp 1337688f869cSTheodore Ts'o if (ext4_handle_valid(handle)) { 1338688f869cSTheodore Ts'o ei->i_sync_tid = handle->h_transaction->t_tid; 1339688f869cSTheodore Ts'o ei->i_datasync_tid = handle->h_transaction->t_tid; 1340688f869cSTheodore Ts'o } 1341688f869cSTheodore Ts'o 13428753e88fSAneesh Kumar K.V err = ext4_mark_inode_dirty(handle, inode); 13438753e88fSAneesh Kumar K.V if (err) { 13448753e88fSAneesh Kumar K.V ext4_std_error(sb, err); 13458753e88fSAneesh Kumar K.V goto fail_free_drop; 13468753e88fSAneesh Kumar K.V } 13478753e88fSAneesh Kumar K.V 1348617ba13bSMingming Cao ext4_debug("allocating inode %lu\n", inode->i_ino); 13499bffad1eSTheodore Ts'o trace_ext4_allocate_inode(inode, dir, mode); 13503300bedaSAneesh Kumar K.V brelse(inode_bitmap_bh); 1351ac27a0ecSDave Kleikamp return ret; 1352ac27a0ecSDave Kleikamp 1353ac27a0ecSDave Kleikamp fail_free_drop: 135463936ddaSChristoph Hellwig dquot_free_inode(inode); 1355ac27a0ecSDave Kleikamp fail_drop: 13566d6b77f1SMiklos Szeredi clear_nlink(inode); 13576b38e842SAl Viro unlock_new_inode(inode); 1358eb9cc7e1SJan Kara out: 1359eb9cc7e1SJan Kara dquot_drop(inode); 1360eb9cc7e1SJan Kara inode->i_flags |= S_NOQUOTA; 1361ac27a0ecSDave Kleikamp iput(inode); 13623300bedaSAneesh Kumar K.V brelse(inode_bitmap_bh); 1363ac27a0ecSDave Kleikamp return ERR_PTR(err); 1364ac27a0ecSDave Kleikamp } 1365ac27a0ecSDave Kleikamp 1366ac27a0ecSDave Kleikamp /* Verify that we are loading a valid orphan from disk */ 1367617ba13bSMingming Cao struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino) 1368ac27a0ecSDave Kleikamp { 1369617ba13bSMingming Cao unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count); 1370fd2d4291SAvantika Mathur ext4_group_t block_group; 1371ac27a0ecSDave Kleikamp int bit; 13727827a7f6STheodore Ts'o struct buffer_head *bitmap_bh = NULL; 1373ac27a0ecSDave Kleikamp struct inode *inode = NULL; 13747827a7f6STheodore Ts'o int err = -EFSCORRUPTED; 1375ac27a0ecSDave Kleikamp 13767827a7f6STheodore Ts'o if (ino < EXT4_FIRST_INO(sb) || ino > max_ino) 13777827a7f6STheodore Ts'o goto bad_orphan; 1378ac27a0ecSDave Kleikamp 1379617ba13bSMingming Cao block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); 1380617ba13bSMingming Cao bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); 1381e29d1cdeSEric Sandeen bitmap_bh = ext4_read_inode_bitmap(sb, block_group); 1382206f6d55SWang Shilong if (IS_ERR(bitmap_bh)) 1383e884bce1SAl Viro return ERR_CAST(bitmap_bh); 1384ac27a0ecSDave Kleikamp 1385ac27a0ecSDave Kleikamp /* Having the inode bit set should be a 100% indicator that this 1386ac27a0ecSDave Kleikamp * is a valid orphan (no e2fsck run on fs). Orphans also include 1387ac27a0ecSDave Kleikamp * inodes that were being truncated, so we can't check i_nlink==0. 1388ac27a0ecSDave Kleikamp */ 13891d1fe1eeSDavid Howells if (!ext4_test_bit(bit, bitmap_bh->b_data)) 13901d1fe1eeSDavid Howells goto bad_orphan; 13911d1fe1eeSDavid Howells 13928a363970STheodore Ts'o inode = ext4_iget(sb, ino, EXT4_IGET_NORMAL); 13937827a7f6STheodore Ts'o if (IS_ERR(inode)) { 13947827a7f6STheodore Ts'o err = PTR_ERR(inode); 139554d3adbcSTheodore Ts'o ext4_error_err(sb, -err, 139654d3adbcSTheodore Ts'o "couldn't read orphan inode %lu (err %d)", 13977827a7f6STheodore Ts'o ino, err); 13983bbd0ef2SXiyu Yang brelse(bitmap_bh); 13997827a7f6STheodore Ts'o return inode; 14007827a7f6STheodore Ts'o } 14011d1fe1eeSDavid Howells 140291ef4cafSDuane Griffin /* 1403c9eb13a9STheodore Ts'o * If the orphans has i_nlinks > 0 then it should be able to 1404c9eb13a9STheodore Ts'o * be truncated, otherwise it won't be removed from the orphan 1405c9eb13a9STheodore Ts'o * list during processing and an infinite loop will result. 1406c9eb13a9STheodore Ts'o * Similarly, it must not be a bad inode. 140791ef4cafSDuane Griffin */ 1408c9eb13a9STheodore Ts'o if ((inode->i_nlink && !ext4_can_truncate(inode)) || 1409c9eb13a9STheodore Ts'o is_bad_inode(inode)) 141091ef4cafSDuane Griffin goto bad_orphan; 141191ef4cafSDuane Griffin 14121d1fe1eeSDavid Howells if (NEXT_ORPHAN(inode) > max_ino) 14131d1fe1eeSDavid Howells goto bad_orphan; 14141d1fe1eeSDavid Howells brelse(bitmap_bh); 14151d1fe1eeSDavid Howells return inode; 14161d1fe1eeSDavid Howells 14171d1fe1eeSDavid Howells bad_orphan: 14187827a7f6STheodore Ts'o ext4_error(sb, "bad orphan inode %lu", ino); 14197827a7f6STheodore Ts'o if (bitmap_bh) 14207827a7f6STheodore Ts'o printk(KERN_ERR "ext4_test_bit(bit=%d, block=%llu) = %d\n", 1421ac27a0ecSDave Kleikamp bit, (unsigned long long)bitmap_bh->b_blocknr, 1422617ba13bSMingming Cao ext4_test_bit(bit, bitmap_bh->b_data)); 1423ac27a0ecSDave Kleikamp if (inode) { 14247827a7f6STheodore Ts'o printk(KERN_ERR "is_bad_inode(inode)=%d\n", 1425ac27a0ecSDave Kleikamp is_bad_inode(inode)); 14267827a7f6STheodore Ts'o printk(KERN_ERR "NEXT_ORPHAN(inode)=%u\n", 1427ac27a0ecSDave Kleikamp NEXT_ORPHAN(inode)); 14287827a7f6STheodore Ts'o printk(KERN_ERR "max_ino=%lu\n", max_ino); 14297827a7f6STheodore Ts'o printk(KERN_ERR "i_nlink=%u\n", inode->i_nlink); 1430ac27a0ecSDave Kleikamp /* Avoid freeing blocks if we got a bad deleted inode */ 14311d1fe1eeSDavid Howells if (inode->i_nlink == 0) 1432ac27a0ecSDave Kleikamp inode->i_blocks = 0; 1433ac27a0ecSDave Kleikamp iput(inode); 1434ac27a0ecSDave Kleikamp } 1435ac27a0ecSDave Kleikamp brelse(bitmap_bh); 14361d1fe1eeSDavid Howells return ERR_PTR(err); 1437ac27a0ecSDave Kleikamp } 1438ac27a0ecSDave Kleikamp 1439617ba13bSMingming Cao unsigned long ext4_count_free_inodes(struct super_block *sb) 1440ac27a0ecSDave Kleikamp { 1441ac27a0ecSDave Kleikamp unsigned long desc_count; 1442617ba13bSMingming Cao struct ext4_group_desc *gdp; 14438df9675fSTheodore Ts'o ext4_group_t i, ngroups = ext4_get_groups_count(sb); 1444617ba13bSMingming Cao #ifdef EXT4FS_DEBUG 1445617ba13bSMingming Cao struct ext4_super_block *es; 1446ac27a0ecSDave Kleikamp unsigned long bitmap_count, x; 1447ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh = NULL; 1448ac27a0ecSDave Kleikamp 1449617ba13bSMingming Cao es = EXT4_SB(sb)->s_es; 1450ac27a0ecSDave Kleikamp desc_count = 0; 1451ac27a0ecSDave Kleikamp bitmap_count = 0; 1452ac27a0ecSDave Kleikamp gdp = NULL; 14538df9675fSTheodore Ts'o for (i = 0; i < ngroups; i++) { 1454617ba13bSMingming Cao gdp = ext4_get_group_desc(sb, i, NULL); 1455ac27a0ecSDave Kleikamp if (!gdp) 1456ac27a0ecSDave Kleikamp continue; 1457560671a0SAneesh Kumar K.V desc_count += ext4_free_inodes_count(sb, gdp); 1458ac27a0ecSDave Kleikamp brelse(bitmap_bh); 1459e29d1cdeSEric Sandeen bitmap_bh = ext4_read_inode_bitmap(sb, i); 14609008a58eSDarrick J. Wong if (IS_ERR(bitmap_bh)) { 14619008a58eSDarrick J. Wong bitmap_bh = NULL; 1462ac27a0ecSDave Kleikamp continue; 14639008a58eSDarrick J. Wong } 1464ac27a0ecSDave Kleikamp 1465f6fb99caSTheodore Ts'o x = ext4_count_free(bitmap_bh->b_data, 1466f6fb99caSTheodore Ts'o EXT4_INODES_PER_GROUP(sb) / 8); 1467c549a95dSEric Sandeen printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n", 1468785b4b3aSPeng Tao (unsigned long) i, ext4_free_inodes_count(sb, gdp), x); 1469ac27a0ecSDave Kleikamp bitmap_count += x; 1470ac27a0ecSDave Kleikamp } 1471ac27a0ecSDave Kleikamp brelse(bitmap_bh); 14724776004fSTheodore Ts'o printk(KERN_DEBUG "ext4_count_free_inodes: " 14734776004fSTheodore Ts'o "stored = %u, computed = %lu, %lu\n", 1474ac27a0ecSDave Kleikamp le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count); 1475ac27a0ecSDave Kleikamp return desc_count; 1476ac27a0ecSDave Kleikamp #else 1477ac27a0ecSDave Kleikamp desc_count = 0; 14788df9675fSTheodore Ts'o for (i = 0; i < ngroups; i++) { 1479617ba13bSMingming Cao gdp = ext4_get_group_desc(sb, i, NULL); 1480ac27a0ecSDave Kleikamp if (!gdp) 1481ac27a0ecSDave Kleikamp continue; 1482560671a0SAneesh Kumar K.V desc_count += ext4_free_inodes_count(sb, gdp); 1483ac27a0ecSDave Kleikamp cond_resched(); 1484ac27a0ecSDave Kleikamp } 1485ac27a0ecSDave Kleikamp return desc_count; 1486ac27a0ecSDave Kleikamp #endif 1487ac27a0ecSDave Kleikamp } 1488ac27a0ecSDave Kleikamp 1489ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */ 1490617ba13bSMingming Cao unsigned long ext4_count_dirs(struct super_block * sb) 1491ac27a0ecSDave Kleikamp { 1492ac27a0ecSDave Kleikamp unsigned long count = 0; 14938df9675fSTheodore Ts'o ext4_group_t i, ngroups = ext4_get_groups_count(sb); 1494ac27a0ecSDave Kleikamp 14958df9675fSTheodore Ts'o for (i = 0; i < ngroups; i++) { 1496617ba13bSMingming Cao struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL); 1497ac27a0ecSDave Kleikamp if (!gdp) 1498ac27a0ecSDave Kleikamp continue; 1499560671a0SAneesh Kumar K.V count += ext4_used_dirs_count(sb, gdp); 1500ac27a0ecSDave Kleikamp } 1501ac27a0ecSDave Kleikamp return count; 1502ac27a0ecSDave Kleikamp } 1503bfff6873SLukas Czerner 1504bfff6873SLukas Czerner /* 1505bfff6873SLukas Czerner * Zeroes not yet zeroed inode table - just write zeroes through the whole 1506bfff6873SLukas Czerner * inode table. Must be called without any spinlock held. The only place 1507bfff6873SLukas Czerner * where it is called from on active part of filesystem is ext4lazyinit 1508bfff6873SLukas Czerner * thread, so we do not need any special locks, however we have to prevent 1509bfff6873SLukas Czerner * inode allocation from the current group, so we take alloc_sem lock, to 1510119c0d44STheodore Ts'o * block ext4_new_inode() until we are finished. 1511bfff6873SLukas Czerner */ 1512e0cbee3eSH Hartley Sweeten int ext4_init_inode_table(struct super_block *sb, ext4_group_t group, 1513bfff6873SLukas Czerner int barrier) 1514bfff6873SLukas Czerner { 1515bfff6873SLukas Czerner struct ext4_group_info *grp = ext4_get_group_info(sb, group); 1516bfff6873SLukas Czerner struct ext4_sb_info *sbi = EXT4_SB(sb); 1517bfff6873SLukas Czerner struct ext4_group_desc *gdp = NULL; 1518bfff6873SLukas Czerner struct buffer_head *group_desc_bh; 1519bfff6873SLukas Czerner handle_t *handle; 1520bfff6873SLukas Czerner ext4_fsblk_t blk; 1521bfff6873SLukas Czerner int num, ret = 0, used_blks = 0; 1522a149d2a5SZhang Yi unsigned long used_inos = 0; 1523bfff6873SLukas Czerner 1524bfff6873SLukas Czerner /* This should not happen, but just to be sure check this */ 1525bc98a42cSDavid Howells if (sb_rdonly(sb)) { 1526bfff6873SLukas Czerner ret = 1; 1527bfff6873SLukas Czerner goto out; 1528bfff6873SLukas Czerner } 1529bfff6873SLukas Czerner 1530bfff6873SLukas Czerner gdp = ext4_get_group_desc(sb, group, &group_desc_bh); 1531bfff6873SLukas Czerner if (!gdp) 1532bfff6873SLukas Czerner goto out; 1533bfff6873SLukas Czerner 1534bfff6873SLukas Czerner /* 1535bfff6873SLukas Czerner * We do not need to lock this, because we are the only one 1536bfff6873SLukas Czerner * handling this flag. 1537bfff6873SLukas Czerner */ 1538bfff6873SLukas Czerner if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)) 1539bfff6873SLukas Czerner goto out; 1540bfff6873SLukas Czerner 15419924a92aSTheodore Ts'o handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1); 1542bfff6873SLukas Czerner if (IS_ERR(handle)) { 1543bfff6873SLukas Czerner ret = PTR_ERR(handle); 1544bfff6873SLukas Czerner goto out; 1545bfff6873SLukas Czerner } 1546bfff6873SLukas Czerner 1547bfff6873SLukas Czerner down_write(&grp->alloc_sem); 1548bfff6873SLukas Czerner /* 1549bfff6873SLukas Czerner * If inode bitmap was already initialized there may be some 1550bfff6873SLukas Czerner * used inodes so we need to skip blocks with used inodes in 1551bfff6873SLukas Czerner * inode table. 1552bfff6873SLukas Czerner */ 1553a149d2a5SZhang Yi if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT))) { 1554a149d2a5SZhang Yi used_inos = EXT4_INODES_PER_GROUP(sb) - 1555a149d2a5SZhang Yi ext4_itable_unused_count(sb, gdp); 1556a149d2a5SZhang Yi used_blks = DIV_ROUND_UP(used_inos, sbi->s_inodes_per_block); 1557bfff6873SLukas Czerner 1558a149d2a5SZhang Yi /* Bogus inode unused count? */ 1559a149d2a5SZhang Yi if (used_blks < 0 || used_blks > sbi->s_itb_per_group) { 15601084f252STheodore Ts'o ext4_error(sb, "Something is wrong with group %u: " 15611084f252STheodore Ts'o "used itable blocks: %d; " 15621084f252STheodore Ts'o "itable unused count: %u", 1563857ac889SLukas Czerner group, used_blks, 1564857ac889SLukas Czerner ext4_itable_unused_count(sb, gdp)); 1565857ac889SLukas Czerner ret = 1; 156633853a0dSYongqiang Yang goto err_out; 1567857ac889SLukas Czerner } 1568857ac889SLukas Czerner 1569a149d2a5SZhang Yi used_inos += group * EXT4_INODES_PER_GROUP(sb); 1570a149d2a5SZhang Yi /* 1571a149d2a5SZhang Yi * Are there some uninitialized inodes in the inode table 1572a149d2a5SZhang Yi * before the first normal inode? 1573a149d2a5SZhang Yi */ 1574a149d2a5SZhang Yi if ((used_blks != sbi->s_itb_per_group) && 1575a149d2a5SZhang Yi (used_inos < EXT4_FIRST_INO(sb))) { 1576a149d2a5SZhang Yi ext4_error(sb, "Something is wrong with group %u: " 1577a149d2a5SZhang Yi "itable unused count: %u; " 1578a149d2a5SZhang Yi "itables initialized count: %ld", 1579a149d2a5SZhang Yi group, ext4_itable_unused_count(sb, gdp), 1580a149d2a5SZhang Yi used_inos); 1581a149d2a5SZhang Yi ret = 1; 1582a149d2a5SZhang Yi goto err_out; 1583a149d2a5SZhang Yi } 1584a149d2a5SZhang Yi } 1585a149d2a5SZhang Yi 1586bfff6873SLukas Czerner blk = ext4_inode_table(sb, gdp) + used_blks; 1587bfff6873SLukas Czerner num = sbi->s_itb_per_group - used_blks; 1588bfff6873SLukas Czerner 1589bfff6873SLukas Czerner BUFFER_TRACE(group_desc_bh, "get_write_access"); 1590188c299eSJan Kara ret = ext4_journal_get_write_access(handle, sb, group_desc_bh, 1591188c299eSJan Kara EXT4_JTR_NONE); 1592bfff6873SLukas Czerner if (ret) 1593bfff6873SLukas Czerner goto err_out; 1594bfff6873SLukas Czerner 1595bfff6873SLukas Czerner /* 1596bfff6873SLukas Czerner * Skip zeroout if the inode table is full. But we set the ZEROED 1597bfff6873SLukas Czerner * flag anyway, because obviously, when it is full it does not need 1598bfff6873SLukas Czerner * further zeroing. 1599bfff6873SLukas Czerner */ 1600bfff6873SLukas Czerner if (unlikely(num == 0)) 1601bfff6873SLukas Czerner goto skip_zeroout; 1602bfff6873SLukas Czerner 1603bfff6873SLukas Czerner ext4_debug("going to zero out inode table in group %d\n", 1604bfff6873SLukas Czerner group); 1605a107e5a3STheodore Ts'o ret = sb_issue_zeroout(sb, blk, num, GFP_NOFS); 1606bfff6873SLukas Czerner if (ret < 0) 1607bfff6873SLukas Czerner goto err_out; 1608a107e5a3STheodore Ts'o if (barrier) 1609c6bf3f0eSChristoph Hellwig blkdev_issue_flush(sb->s_bdev); 1610bfff6873SLukas Czerner 1611bfff6873SLukas Czerner skip_zeroout: 1612bfff6873SLukas Czerner ext4_lock_group(sb, group); 1613bfff6873SLukas Czerner gdp->bg_flags |= cpu_to_le16(EXT4_BG_INODE_ZEROED); 1614feb0ab32SDarrick J. Wong ext4_group_desc_csum_set(sb, group, gdp); 1615bfff6873SLukas Czerner ext4_unlock_group(sb, group); 1616bfff6873SLukas Czerner 1617bfff6873SLukas Czerner BUFFER_TRACE(group_desc_bh, 1618bfff6873SLukas Czerner "call ext4_handle_dirty_metadata"); 1619bfff6873SLukas Czerner ret = ext4_handle_dirty_metadata(handle, NULL, 1620bfff6873SLukas Czerner group_desc_bh); 1621bfff6873SLukas Czerner 1622bfff6873SLukas Czerner err_out: 1623bfff6873SLukas Czerner up_write(&grp->alloc_sem); 1624bfff6873SLukas Czerner ext4_journal_stop(handle); 1625bfff6873SLukas Czerner out: 1626bfff6873SLukas Czerner return ret; 1627bfff6873SLukas Czerner } 1628