1ac27a0ecSDave Kleikamp /* 2617ba13bSMingming Cao * linux/fs/ext4/ialloc.c 3ac27a0ecSDave Kleikamp * 4ac27a0ecSDave Kleikamp * Copyright (C) 1992, 1993, 1994, 1995 5ac27a0ecSDave Kleikamp * Remy Card (card@masi.ibp.fr) 6ac27a0ecSDave Kleikamp * Laboratoire MASI - Institut Blaise Pascal 7ac27a0ecSDave Kleikamp * Universite Pierre et Marie Curie (Paris VI) 8ac27a0ecSDave Kleikamp * 9ac27a0ecSDave Kleikamp * BSD ufs-inspired inode and directory allocation by 10ac27a0ecSDave Kleikamp * Stephen Tweedie (sct@redhat.com), 1993 11ac27a0ecSDave Kleikamp * Big-endian to little-endian byte-swapping/bitmaps by 12ac27a0ecSDave Kleikamp * David S. Miller (davem@caip.rutgers.edu), 1995 13ac27a0ecSDave Kleikamp */ 14ac27a0ecSDave Kleikamp 15ac27a0ecSDave Kleikamp #include <linux/time.h> 16ac27a0ecSDave Kleikamp #include <linux/fs.h> 17ac27a0ecSDave Kleikamp #include <linux/stat.h> 18ac27a0ecSDave Kleikamp #include <linux/string.h> 19ac27a0ecSDave Kleikamp #include <linux/quotaops.h> 20ac27a0ecSDave Kleikamp #include <linux/buffer_head.h> 21ac27a0ecSDave Kleikamp #include <linux/random.h> 22ac27a0ecSDave Kleikamp #include <linux/bitops.h> 233a5b2ecdSMingming Cao #include <linux/blkdev.h> 24ac27a0ecSDave Kleikamp #include <asm/byteorder.h> 259bffad1eSTheodore Ts'o 263dcf5451SChristoph Hellwig #include "ext4.h" 273dcf5451SChristoph Hellwig #include "ext4_jbd2.h" 28ac27a0ecSDave Kleikamp #include "xattr.h" 29ac27a0ecSDave Kleikamp #include "acl.h" 30ac27a0ecSDave Kleikamp 319bffad1eSTheodore Ts'o #include <trace/events/ext4.h> 329bffad1eSTheodore Ts'o 33ac27a0ecSDave Kleikamp /* 34ac27a0ecSDave Kleikamp * ialloc.c contains the inodes allocation and deallocation routines 35ac27a0ecSDave Kleikamp */ 36ac27a0ecSDave Kleikamp 37ac27a0ecSDave Kleikamp /* 38ac27a0ecSDave Kleikamp * The free inodes are managed by bitmaps. A file system contains several 39ac27a0ecSDave Kleikamp * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap 40ac27a0ecSDave Kleikamp * block for inodes, N blocks for the inode table and data blocks. 41ac27a0ecSDave Kleikamp * 42ac27a0ecSDave Kleikamp * The file system contains group descriptors which are located after the 43ac27a0ecSDave Kleikamp * super block. Each descriptor contains the number of the bitmap block and 44ac27a0ecSDave Kleikamp * the free blocks count in the block. 45ac27a0ecSDave Kleikamp */ 46ac27a0ecSDave Kleikamp 47717d50e4SAndreas Dilger /* 48717d50e4SAndreas Dilger * To avoid calling the atomic setbit hundreds or thousands of times, we only 49717d50e4SAndreas Dilger * need to use it within a single byte (to ensure we get endianness right). 50717d50e4SAndreas Dilger * We can use memset for the rest of the bitmap as there are no other users. 51717d50e4SAndreas Dilger */ 5261d08673STheodore Ts'o void ext4_mark_bitmap_end(int start_bit, int end_bit, char *bitmap) 53717d50e4SAndreas Dilger { 54717d50e4SAndreas Dilger int i; 55717d50e4SAndreas Dilger 56717d50e4SAndreas Dilger if (start_bit >= end_bit) 57717d50e4SAndreas Dilger return; 58717d50e4SAndreas Dilger 59717d50e4SAndreas Dilger ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit); 60717d50e4SAndreas Dilger for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++) 61717d50e4SAndreas Dilger ext4_set_bit(i, bitmap); 62717d50e4SAndreas Dilger if (i < end_bit) 63717d50e4SAndreas Dilger memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3); 64717d50e4SAndreas Dilger } 65717d50e4SAndreas Dilger 66717d50e4SAndreas Dilger /* Initializes an uninitialized inode bitmap */ 671f109d5aSTheodore Ts'o static unsigned ext4_init_inode_bitmap(struct super_block *sb, 681f109d5aSTheodore Ts'o struct buffer_head *bh, 69fd2d4291SAvantika Mathur ext4_group_t block_group, 70717d50e4SAndreas Dilger struct ext4_group_desc *gdp) 71717d50e4SAndreas Dilger { 72bdfb6ff4SDarrick J. Wong struct ext4_group_info *grp; 73e43bb4e6SNamjae Jeon struct ext4_sb_info *sbi = EXT4_SB(sb); 74717d50e4SAndreas Dilger J_ASSERT_BH(bh, buffer_locked(bh)); 75717d50e4SAndreas Dilger 76717d50e4SAndreas Dilger /* If checksum is bad mark all blocks and inodes use to prevent 77717d50e4SAndreas Dilger * allocation, essentially implementing a per-group read-only flag. */ 78feb0ab32SDarrick J. Wong if (!ext4_group_desc_csum_verify(sb, block_group, gdp)) { 7912062dddSEric Sandeen ext4_error(sb, "Checksum bad for group %u", block_group); 80bdfb6ff4SDarrick J. Wong grp = ext4_get_group_info(sb, block_group); 81e43bb4e6SNamjae Jeon if (!EXT4_MB_GRP_BBITMAP_CORRUPT(grp)) 82e43bb4e6SNamjae Jeon percpu_counter_sub(&sbi->s_freeclusters_counter, 83e43bb4e6SNamjae Jeon grp->bb_free); 84bdfb6ff4SDarrick J. Wong set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT, &grp->bb_state); 85e43bb4e6SNamjae Jeon if (!EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) { 86e43bb4e6SNamjae Jeon int count; 87e43bb4e6SNamjae Jeon count = ext4_free_inodes_count(sb, gdp); 88e43bb4e6SNamjae Jeon percpu_counter_sub(&sbi->s_freeinodes_counter, 89e43bb4e6SNamjae Jeon count); 90e43bb4e6SNamjae Jeon } 91bdfb6ff4SDarrick J. Wong set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &grp->bb_state); 92717d50e4SAndreas Dilger return 0; 93717d50e4SAndreas Dilger } 94717d50e4SAndreas Dilger 95717d50e4SAndreas Dilger memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8); 9661d08673STheodore Ts'o ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), sb->s_blocksize * 8, 97717d50e4SAndreas Dilger bh->b_data); 9841a246d1SDarrick J. Wong ext4_inode_bitmap_csum_set(sb, block_group, gdp, bh, 9941a246d1SDarrick J. Wong EXT4_INODES_PER_GROUP(sb) / 8); 100feb0ab32SDarrick J. Wong ext4_group_desc_csum_set(sb, block_group, gdp); 101717d50e4SAndreas Dilger 102717d50e4SAndreas Dilger return EXT4_INODES_PER_GROUP(sb); 103717d50e4SAndreas Dilger } 104ac27a0ecSDave Kleikamp 105813e5727STheodore Ts'o void ext4_end_bitmap_read(struct buffer_head *bh, int uptodate) 106813e5727STheodore Ts'o { 107813e5727STheodore Ts'o if (uptodate) { 108813e5727STheodore Ts'o set_buffer_uptodate(bh); 109813e5727STheodore Ts'o set_bitmap_uptodate(bh); 110813e5727STheodore Ts'o } 111813e5727STheodore Ts'o unlock_buffer(bh); 112813e5727STheodore Ts'o put_bh(bh); 113813e5727STheodore Ts'o } 114813e5727STheodore Ts'o 115ac27a0ecSDave Kleikamp /* 116ac27a0ecSDave Kleikamp * Read the inode allocation bitmap for a given block_group, reading 117ac27a0ecSDave Kleikamp * into the specified slot in the superblock's bitmap cache. 118ac27a0ecSDave Kleikamp * 119ac27a0ecSDave Kleikamp * Return buffer_head of bitmap on success or NULL. 120ac27a0ecSDave Kleikamp */ 121ac27a0ecSDave Kleikamp static struct buffer_head * 122e29d1cdeSEric Sandeen ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group) 123ac27a0ecSDave Kleikamp { 124617ba13bSMingming Cao struct ext4_group_desc *desc; 125ac27a0ecSDave Kleikamp struct buffer_head *bh = NULL; 126e29d1cdeSEric Sandeen ext4_fsblk_t bitmap_blk; 12787a39389SDarrick J. Wong struct ext4_group_info *grp; 128e43bb4e6SNamjae Jeon struct ext4_sb_info *sbi = EXT4_SB(sb); 129ac27a0ecSDave Kleikamp 130617ba13bSMingming Cao desc = ext4_get_group_desc(sb, block_group, NULL); 131ac27a0ecSDave Kleikamp if (!desc) 132e29d1cdeSEric Sandeen return NULL; 133bfff6873SLukas Czerner 134e29d1cdeSEric Sandeen bitmap_blk = ext4_inode_bitmap(sb, desc); 135e29d1cdeSEric Sandeen bh = sb_getblk(sb, bitmap_blk); 136e29d1cdeSEric Sandeen if (unlikely(!bh)) { 13712062dddSEric Sandeen ext4_error(sb, "Cannot read inode bitmap - " 138a9df9a49STheodore Ts'o "block_group = %u, inode_bitmap = %llu", 139e29d1cdeSEric Sandeen block_group, bitmap_blk); 140e29d1cdeSEric Sandeen return NULL; 141e29d1cdeSEric Sandeen } 1422ccb5fb9SAneesh Kumar K.V if (bitmap_uptodate(bh)) 14341a246d1SDarrick J. Wong goto verify; 144e29d1cdeSEric Sandeen 145c806e68fSFrederic Bohe lock_buffer(bh); 1462ccb5fb9SAneesh Kumar K.V if (bitmap_uptodate(bh)) { 1472ccb5fb9SAneesh Kumar K.V unlock_buffer(bh); 14841a246d1SDarrick J. Wong goto verify; 1492ccb5fb9SAneesh Kumar K.V } 150bfff6873SLukas Czerner 151955ce5f5SAneesh Kumar K.V ext4_lock_group(sb, block_group); 152e29d1cdeSEric Sandeen if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) { 153e29d1cdeSEric Sandeen ext4_init_inode_bitmap(sb, bh, block_group, desc); 1542ccb5fb9SAneesh Kumar K.V set_bitmap_uptodate(bh); 155e29d1cdeSEric Sandeen set_buffer_uptodate(bh); 15641a246d1SDarrick J. Wong set_buffer_verified(bh); 157955ce5f5SAneesh Kumar K.V ext4_unlock_group(sb, block_group); 1583300bedaSAneesh Kumar K.V unlock_buffer(bh); 159e29d1cdeSEric Sandeen return bh; 160e29d1cdeSEric Sandeen } 161955ce5f5SAneesh Kumar K.V ext4_unlock_group(sb, block_group); 162bfff6873SLukas Czerner 1632ccb5fb9SAneesh Kumar K.V if (buffer_uptodate(bh)) { 1642ccb5fb9SAneesh Kumar K.V /* 1652ccb5fb9SAneesh Kumar K.V * if not uninit if bh is uptodate, 1662ccb5fb9SAneesh Kumar K.V * bitmap is also uptodate 1672ccb5fb9SAneesh Kumar K.V */ 1682ccb5fb9SAneesh Kumar K.V set_bitmap_uptodate(bh); 1692ccb5fb9SAneesh Kumar K.V unlock_buffer(bh); 17041a246d1SDarrick J. Wong goto verify; 1712ccb5fb9SAneesh Kumar K.V } 1722ccb5fb9SAneesh Kumar K.V /* 173813e5727STheodore Ts'o * submit the buffer_head for reading 1742ccb5fb9SAneesh Kumar K.V */ 1750562e0baSJiaying Zhang trace_ext4_load_inode_bitmap(sb, block_group); 176813e5727STheodore Ts'o bh->b_end_io = ext4_end_bitmap_read; 177813e5727STheodore Ts'o get_bh(bh); 1789f203507STheodore Ts'o submit_bh(READ | REQ_META | REQ_PRIO, bh); 179813e5727STheodore Ts'o wait_on_buffer(bh); 180813e5727STheodore Ts'o if (!buffer_uptodate(bh)) { 181e29d1cdeSEric Sandeen put_bh(bh); 18212062dddSEric Sandeen ext4_error(sb, "Cannot read inode bitmap - " 183a9df9a49STheodore Ts'o "block_group = %u, inode_bitmap = %llu", 184e29d1cdeSEric Sandeen block_group, bitmap_blk); 185e29d1cdeSEric Sandeen return NULL; 186e29d1cdeSEric Sandeen } 18741a246d1SDarrick J. Wong 18841a246d1SDarrick J. Wong verify: 18941a246d1SDarrick J. Wong ext4_lock_group(sb, block_group); 19041a246d1SDarrick J. Wong if (!buffer_verified(bh) && 19141a246d1SDarrick J. Wong !ext4_inode_bitmap_csum_verify(sb, block_group, desc, bh, 19241a246d1SDarrick J. Wong EXT4_INODES_PER_GROUP(sb) / 8)) { 19341a246d1SDarrick J. Wong ext4_unlock_group(sb, block_group); 19441a246d1SDarrick J. Wong put_bh(bh); 19541a246d1SDarrick J. Wong ext4_error(sb, "Corrupt inode bitmap - block_group = %u, " 19641a246d1SDarrick J. Wong "inode_bitmap = %llu", block_group, bitmap_blk); 19787a39389SDarrick J. Wong grp = ext4_get_group_info(sb, block_group); 198e43bb4e6SNamjae Jeon if (!EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) { 199e43bb4e6SNamjae Jeon int count; 200e43bb4e6SNamjae Jeon count = ext4_free_inodes_count(sb, desc); 201e43bb4e6SNamjae Jeon percpu_counter_sub(&sbi->s_freeinodes_counter, 202e43bb4e6SNamjae Jeon count); 203e43bb4e6SNamjae Jeon } 20487a39389SDarrick J. Wong set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &grp->bb_state); 20541a246d1SDarrick J. Wong return NULL; 20641a246d1SDarrick J. Wong } 20741a246d1SDarrick J. Wong ext4_unlock_group(sb, block_group); 20841a246d1SDarrick J. Wong set_buffer_verified(bh); 209ac27a0ecSDave Kleikamp return bh; 210ac27a0ecSDave Kleikamp } 211ac27a0ecSDave Kleikamp 212ac27a0ecSDave Kleikamp /* 213ac27a0ecSDave Kleikamp * NOTE! When we get the inode, we're the only people 214ac27a0ecSDave Kleikamp * that have access to it, and as such there are no 215ac27a0ecSDave Kleikamp * race conditions we have to worry about. The inode 216ac27a0ecSDave Kleikamp * is not on the hash-lists, and it cannot be reached 217ac27a0ecSDave Kleikamp * through the filesystem because the directory entry 218ac27a0ecSDave Kleikamp * has been deleted earlier. 219ac27a0ecSDave Kleikamp * 220ac27a0ecSDave Kleikamp * HOWEVER: we must make sure that we get no aliases, 221ac27a0ecSDave Kleikamp * which means that we have to call "clear_inode()" 222ac27a0ecSDave Kleikamp * _before_ we mark the inode not in use in the inode 223ac27a0ecSDave Kleikamp * bitmaps. Otherwise a newly created file might use 224ac27a0ecSDave Kleikamp * the same inode number (not actually the same pointer 225ac27a0ecSDave Kleikamp * though), and then we'd have two inodes sharing the 226ac27a0ecSDave Kleikamp * same inode number and space on the harddisk. 227ac27a0ecSDave Kleikamp */ 228617ba13bSMingming Cao void ext4_free_inode(handle_t *handle, struct inode *inode) 229ac27a0ecSDave Kleikamp { 230ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 231ac27a0ecSDave Kleikamp int is_directory; 232ac27a0ecSDave Kleikamp unsigned long ino; 233ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh = NULL; 234ac27a0ecSDave Kleikamp struct buffer_head *bh2; 235fd2d4291SAvantika Mathur ext4_group_t block_group; 236ac27a0ecSDave Kleikamp unsigned long bit; 237617ba13bSMingming Cao struct ext4_group_desc *gdp; 238617ba13bSMingming Cao struct ext4_super_block *es; 239617ba13bSMingming Cao struct ext4_sb_info *sbi; 2407ce9d5d1SEric Sandeen int fatal = 0, err, count, cleared; 24187a39389SDarrick J. Wong struct ext4_group_info *grp; 242ac27a0ecSDave Kleikamp 24392b97816STheodore Ts'o if (!sb) { 24492b97816STheodore Ts'o printk(KERN_ERR "EXT4-fs: %s:%d: inode on " 24592b97816STheodore Ts'o "nonexistent device\n", __func__, __LINE__); 24692b97816STheodore Ts'o return; 24792b97816STheodore Ts'o } 248ac27a0ecSDave Kleikamp if (atomic_read(&inode->i_count) > 1) { 24992b97816STheodore Ts'o ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: count=%d", 25092b97816STheodore Ts'o __func__, __LINE__, inode->i_ino, 251ac27a0ecSDave Kleikamp atomic_read(&inode->i_count)); 252ac27a0ecSDave Kleikamp return; 253ac27a0ecSDave Kleikamp } 254ac27a0ecSDave Kleikamp if (inode->i_nlink) { 25592b97816STheodore Ts'o ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: nlink=%d\n", 25692b97816STheodore Ts'o __func__, __LINE__, inode->i_ino, inode->i_nlink); 257ac27a0ecSDave Kleikamp return; 258ac27a0ecSDave Kleikamp } 259617ba13bSMingming Cao sbi = EXT4_SB(sb); 260ac27a0ecSDave Kleikamp 261ac27a0ecSDave Kleikamp ino = inode->i_ino; 262617ba13bSMingming Cao ext4_debug("freeing inode %lu\n", ino); 2639bffad1eSTheodore Ts'o trace_ext4_free_inode(inode); 264ac27a0ecSDave Kleikamp 265ac27a0ecSDave Kleikamp /* 266ac27a0ecSDave Kleikamp * Note: we must free any quota before locking the superblock, 267ac27a0ecSDave Kleikamp * as writing the quota to disk may need the lock as well. 268ac27a0ecSDave Kleikamp */ 269871a2931SChristoph Hellwig dquot_initialize(inode); 270617ba13bSMingming Cao ext4_xattr_delete_inode(handle, inode); 27163936ddaSChristoph Hellwig dquot_free_inode(inode); 2729f754758SChristoph Hellwig dquot_drop(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 279617ba13bSMingming Cao es = EXT4_SB(sb)->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); 28987a39389SDarrick J. Wong if (unlikely(EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) || !bitmap_bh) 290ac27a0ecSDave Kleikamp goto error_return; 291ac27a0ecSDave Kleikamp 292ac27a0ecSDave Kleikamp BUFFER_TRACE(bitmap_bh, "get_write_access"); 293617ba13bSMingming Cao fatal = ext4_journal_get_write_access(handle, bitmap_bh); 294ac27a0ecSDave Kleikamp if (fatal) 295ac27a0ecSDave Kleikamp goto error_return; 296ac27a0ecSDave Kleikamp 297d17413c0SDmitry Monakhov fatal = -ESRCH; 298617ba13bSMingming Cao gdp = ext4_get_group_desc(sb, block_group, &bh2); 299d17413c0SDmitry Monakhov if (gdp) { 300ac27a0ecSDave Kleikamp BUFFER_TRACE(bh2, "get_write_access"); 301617ba13bSMingming Cao fatal = ext4_journal_get_write_access(handle, bh2); 302d17413c0SDmitry Monakhov } 303955ce5f5SAneesh Kumar K.V ext4_lock_group(sb, block_group); 304597d508cSAkinobu Mita cleared = ext4_test_and_clear_bit(bit, bitmap_bh->b_data); 305d17413c0SDmitry Monakhov if (fatal || !cleared) { 306d17413c0SDmitry Monakhov ext4_unlock_group(sb, block_group); 307d17413c0SDmitry Monakhov goto out; 308d17413c0SDmitry Monakhov } 309d17413c0SDmitry Monakhov 310560671a0SAneesh Kumar K.V count = ext4_free_inodes_count(sb, gdp) + 1; 311560671a0SAneesh Kumar K.V ext4_free_inodes_set(sb, gdp, count); 312560671a0SAneesh Kumar K.V if (is_directory) { 313560671a0SAneesh Kumar K.V count = ext4_used_dirs_count(sb, gdp) - 1; 314560671a0SAneesh Kumar K.V ext4_used_dirs_set(sb, gdp, count); 315d17413c0SDmitry Monakhov percpu_counter_dec(&sbi->s_dirs_counter); 316d17413c0SDmitry Monakhov } 31741a246d1SDarrick J. Wong ext4_inode_bitmap_csum_set(sb, block_group, gdp, bitmap_bh, 31841a246d1SDarrick J. Wong EXT4_INODES_PER_GROUP(sb) / 8); 319feb0ab32SDarrick J. Wong ext4_group_desc_csum_set(sb, block_group, gdp); 320d17413c0SDmitry Monakhov ext4_unlock_group(sb, block_group); 3217d39db14STheodore Ts'o 322d17413c0SDmitry Monakhov percpu_counter_inc(&sbi->s_freeinodes_counter); 323d17413c0SDmitry Monakhov if (sbi->s_log_groups_per_flex) { 324d17413c0SDmitry Monakhov ext4_group_t f = ext4_flex_group(sbi, block_group); 325d17413c0SDmitry Monakhov 326d17413c0SDmitry Monakhov atomic_inc(&sbi->s_flex_groups[f].free_inodes); 327d17413c0SDmitry Monakhov if (is_directory) 328c4caae25SEric Sandeen atomic_dec(&sbi->s_flex_groups[f].used_dirs); 3297d39db14STheodore Ts'o } 3300390131bSFrank Mayhar BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata"); 331d17413c0SDmitry Monakhov fatal = ext4_handle_dirty_metadata(handle, NULL, bh2); 332d17413c0SDmitry Monakhov out: 333d17413c0SDmitry Monakhov if (cleared) { 3340390131bSFrank Mayhar BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata"); 3350390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh); 336ac27a0ecSDave Kleikamp if (!fatal) 337ac27a0ecSDave Kleikamp fatal = err; 33887a39389SDarrick J. Wong } else { 339d17413c0SDmitry Monakhov ext4_error(sb, "bit already cleared for inode %lu", ino); 340bf40c926SNamjae Jeon if (gdp && !EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) { 341e43bb4e6SNamjae Jeon int count; 342e43bb4e6SNamjae Jeon count = ext4_free_inodes_count(sb, gdp); 343e43bb4e6SNamjae Jeon percpu_counter_sub(&sbi->s_freeinodes_counter, 344e43bb4e6SNamjae Jeon count); 345e43bb4e6SNamjae Jeon } 34687a39389SDarrick J. Wong set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &grp->bb_state); 34787a39389SDarrick J. Wong } 348d17413c0SDmitry Monakhov 349ac27a0ecSDave Kleikamp error_return: 350ac27a0ecSDave Kleikamp brelse(bitmap_bh); 351617ba13bSMingming Cao ext4_std_error(sb, fatal); 352ac27a0ecSDave Kleikamp } 353ac27a0ecSDave Kleikamp 354a4912123STheodore Ts'o struct orlov_stats { 35590ba983fSTheodore Ts'o __u64 free_clusters; 356a4912123STheodore Ts'o __u32 free_inodes; 357a4912123STheodore Ts'o __u32 used_dirs; 358a4912123STheodore Ts'o }; 359a4912123STheodore Ts'o 360a4912123STheodore Ts'o /* 361a4912123STheodore Ts'o * Helper function for Orlov's allocator; returns critical information 362a4912123STheodore Ts'o * for a particular block group or flex_bg. If flex_size is 1, then g 363a4912123STheodore Ts'o * is a block group number; otherwise it is flex_bg number. 364a4912123STheodore Ts'o */ 3651f109d5aSTheodore Ts'o static void get_orlov_stats(struct super_block *sb, ext4_group_t g, 366a4912123STheodore Ts'o int flex_size, struct orlov_stats *stats) 367a4912123STheodore Ts'o { 368a4912123STheodore Ts'o struct ext4_group_desc *desc; 3697d39db14STheodore Ts'o struct flex_groups *flex_group = EXT4_SB(sb)->s_flex_groups; 370a4912123STheodore Ts'o 3717d39db14STheodore Ts'o if (flex_size > 1) { 3727d39db14STheodore Ts'o stats->free_inodes = atomic_read(&flex_group[g].free_inodes); 37390ba983fSTheodore Ts'o stats->free_clusters = atomic64_read(&flex_group[g].free_clusters); 3747d39db14STheodore Ts'o stats->used_dirs = atomic_read(&flex_group[g].used_dirs); 3757d39db14STheodore Ts'o return; 3767d39db14STheodore Ts'o } 3777d39db14STheodore Ts'o 3787d39db14STheodore Ts'o desc = ext4_get_group_desc(sb, g, NULL); 3797d39db14STheodore Ts'o if (desc) { 3807d39db14STheodore Ts'o stats->free_inodes = ext4_free_inodes_count(sb, desc); 381021b65bbSTheodore Ts'o stats->free_clusters = ext4_free_group_clusters(sb, desc); 3827d39db14STheodore Ts'o stats->used_dirs = ext4_used_dirs_count(sb, desc); 3837d39db14STheodore Ts'o } else { 384a4912123STheodore Ts'o stats->free_inodes = 0; 38524aaa8efSTheodore Ts'o stats->free_clusters = 0; 386a4912123STheodore Ts'o stats->used_dirs = 0; 387a4912123STheodore Ts'o } 388a4912123STheodore Ts'o } 389a4912123STheodore Ts'o 390ac27a0ecSDave Kleikamp /* 391ac27a0ecSDave Kleikamp * Orlov's allocator for directories. 392ac27a0ecSDave Kleikamp * 393ac27a0ecSDave Kleikamp * We always try to spread first-level directories. 394ac27a0ecSDave Kleikamp * 395ac27a0ecSDave Kleikamp * If there are blockgroups with both free inodes and free blocks counts 396ac27a0ecSDave Kleikamp * not worse than average we return one with smallest directory count. 397ac27a0ecSDave Kleikamp * Otherwise we simply return a random group. 398ac27a0ecSDave Kleikamp * 399ac27a0ecSDave Kleikamp * For the rest rules look so: 400ac27a0ecSDave Kleikamp * 401ac27a0ecSDave Kleikamp * It's OK to put directory into a group unless 402ac27a0ecSDave Kleikamp * it has too many directories already (max_dirs) or 403ac27a0ecSDave Kleikamp * it has too few free inodes left (min_inodes) or 404ac27a0ecSDave Kleikamp * it has too few free blocks left (min_blocks) or 4051cc8dcf5SBenoit Boissinot * Parent's group is preferred, if it doesn't satisfy these 406ac27a0ecSDave Kleikamp * conditions we search cyclically through the rest. If none 407ac27a0ecSDave Kleikamp * of the groups look good we just look for a group with more 408ac27a0ecSDave Kleikamp * free inodes than average (starting at parent's group). 409ac27a0ecSDave Kleikamp */ 410ac27a0ecSDave Kleikamp 4112aa9fc4cSAvantika Mathur static int find_group_orlov(struct super_block *sb, struct inode *parent, 412dcca3fecSAl Viro ext4_group_t *group, umode_t mode, 413f157a4aaSTheodore Ts'o const struct qstr *qstr) 414ac27a0ecSDave Kleikamp { 415fd2d4291SAvantika Mathur ext4_group_t parent_group = EXT4_I(parent)->i_block_group; 416617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 4178df9675fSTheodore Ts'o ext4_group_t real_ngroups = ext4_get_groups_count(sb); 418617ba13bSMingming Cao int inodes_per_group = EXT4_INODES_PER_GROUP(sb); 41914c83c9fSTheodore Ts'o unsigned int freei, avefreei, grp_free; 42024aaa8efSTheodore Ts'o ext4_fsblk_t freeb, avefreec; 421ac27a0ecSDave Kleikamp unsigned int ndirs; 422a4912123STheodore Ts'o int max_dirs, min_inodes; 42324aaa8efSTheodore Ts'o ext4_grpblk_t min_clusters; 4248df9675fSTheodore Ts'o ext4_group_t i, grp, g, ngroups; 425617ba13bSMingming Cao struct ext4_group_desc *desc; 426a4912123STheodore Ts'o struct orlov_stats stats; 427a4912123STheodore Ts'o int flex_size = ext4_flex_bg_size(sbi); 428f157a4aaSTheodore Ts'o struct dx_hash_info hinfo; 429a4912123STheodore Ts'o 4308df9675fSTheodore Ts'o ngroups = real_ngroups; 431a4912123STheodore Ts'o if (flex_size > 1) { 4328df9675fSTheodore Ts'o ngroups = (real_ngroups + flex_size - 1) >> 433a4912123STheodore Ts'o sbi->s_log_groups_per_flex; 434a4912123STheodore Ts'o parent_group >>= sbi->s_log_groups_per_flex; 435a4912123STheodore Ts'o } 436ac27a0ecSDave Kleikamp 437ac27a0ecSDave Kleikamp freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter); 438ac27a0ecSDave Kleikamp avefreei = freei / ngroups; 43957042651STheodore Ts'o freeb = EXT4_C2B(sbi, 44057042651STheodore Ts'o percpu_counter_read_positive(&sbi->s_freeclusters_counter)); 44124aaa8efSTheodore Ts'o avefreec = freeb; 44224aaa8efSTheodore Ts'o do_div(avefreec, ngroups); 443ac27a0ecSDave Kleikamp ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter); 444ac27a0ecSDave Kleikamp 445a4912123STheodore Ts'o if (S_ISDIR(mode) && 4462b0143b5SDavid Howells ((parent == d_inode(sb->s_root)) || 44712e9b892SDmitry Monakhov (ext4_test_inode_flag(parent, EXT4_INODE_TOPDIR)))) { 448ac27a0ecSDave Kleikamp int best_ndir = inodes_per_group; 4492aa9fc4cSAvantika Mathur int ret = -1; 450ac27a0ecSDave Kleikamp 451f157a4aaSTheodore Ts'o if (qstr) { 452f157a4aaSTheodore Ts'o hinfo.hash_version = DX_HASH_HALF_MD4; 453f157a4aaSTheodore Ts'o hinfo.seed = sbi->s_hash_seed; 454f157a4aaSTheodore Ts'o ext4fs_dirhash(qstr->name, qstr->len, &hinfo); 455f157a4aaSTheodore Ts'o grp = hinfo.hash; 456f157a4aaSTheodore Ts'o } else 457dd1f723bSTheodore Ts'o grp = prandom_u32(); 4582aa9fc4cSAvantika Mathur parent_group = (unsigned)grp % ngroups; 459ac27a0ecSDave Kleikamp for (i = 0; i < ngroups; i++) { 460a4912123STheodore Ts'o g = (parent_group + i) % ngroups; 461a4912123STheodore Ts'o get_orlov_stats(sb, g, flex_size, &stats); 462a4912123STheodore Ts'o if (!stats.free_inodes) 463ac27a0ecSDave Kleikamp continue; 464a4912123STheodore Ts'o if (stats.used_dirs >= best_ndir) 465ac27a0ecSDave Kleikamp continue; 466a4912123STheodore Ts'o if (stats.free_inodes < avefreei) 467ac27a0ecSDave Kleikamp continue; 46824aaa8efSTheodore Ts'o if (stats.free_clusters < avefreec) 469ac27a0ecSDave Kleikamp continue; 470a4912123STheodore Ts'o grp = g; 4712aa9fc4cSAvantika Mathur ret = 0; 472a4912123STheodore Ts'o best_ndir = stats.used_dirs; 473ac27a0ecSDave Kleikamp } 474a4912123STheodore Ts'o if (ret) 475a4912123STheodore Ts'o goto fallback; 476a4912123STheodore Ts'o found_flex_bg: 477a4912123STheodore Ts'o if (flex_size == 1) { 478a4912123STheodore Ts'o *group = grp; 479a4912123STheodore Ts'o return 0; 480a4912123STheodore Ts'o } 481a4912123STheodore Ts'o 482a4912123STheodore Ts'o /* 483a4912123STheodore Ts'o * We pack inodes at the beginning of the flexgroup's 484a4912123STheodore Ts'o * inode tables. Block allocation decisions will do 485a4912123STheodore Ts'o * something similar, although regular files will 486a4912123STheodore Ts'o * start at 2nd block group of the flexgroup. See 487a4912123STheodore Ts'o * ext4_ext_find_goal() and ext4_find_near(). 488a4912123STheodore Ts'o */ 489a4912123STheodore Ts'o grp *= flex_size; 490a4912123STheodore Ts'o for (i = 0; i < flex_size; i++) { 4918df9675fSTheodore Ts'o if (grp+i >= real_ngroups) 492a4912123STheodore Ts'o break; 493a4912123STheodore Ts'o desc = ext4_get_group_desc(sb, grp+i, NULL); 494a4912123STheodore Ts'o if (desc && ext4_free_inodes_count(sb, desc)) { 495a4912123STheodore Ts'o *group = grp+i; 496a4912123STheodore Ts'o return 0; 497a4912123STheodore Ts'o } 498a4912123STheodore Ts'o } 499ac27a0ecSDave Kleikamp goto fallback; 500ac27a0ecSDave Kleikamp } 501ac27a0ecSDave Kleikamp 502ac27a0ecSDave Kleikamp max_dirs = ndirs / ngroups + inodes_per_group / 16; 503a4912123STheodore Ts'o min_inodes = avefreei - inodes_per_group*flex_size / 4; 504a4912123STheodore Ts'o if (min_inodes < 1) 505a4912123STheodore Ts'o min_inodes = 1; 50624aaa8efSTheodore Ts'o min_clusters = avefreec - EXT4_CLUSTERS_PER_GROUP(sb)*flex_size / 4; 507ac27a0ecSDave Kleikamp 508a4912123STheodore Ts'o /* 509a4912123STheodore Ts'o * Start looking in the flex group where we last allocated an 510a4912123STheodore Ts'o * inode for this parent directory 511a4912123STheodore Ts'o */ 512a4912123STheodore Ts'o if (EXT4_I(parent)->i_last_alloc_group != ~0) { 513a4912123STheodore Ts'o parent_group = EXT4_I(parent)->i_last_alloc_group; 514a4912123STheodore Ts'o if (flex_size > 1) 515a4912123STheodore Ts'o parent_group >>= sbi->s_log_groups_per_flex; 516a4912123STheodore Ts'o } 517ac27a0ecSDave Kleikamp 518ac27a0ecSDave Kleikamp for (i = 0; i < ngroups; i++) { 519a4912123STheodore Ts'o grp = (parent_group + i) % ngroups; 520a4912123STheodore Ts'o get_orlov_stats(sb, grp, flex_size, &stats); 521a4912123STheodore Ts'o if (stats.used_dirs >= max_dirs) 522ac27a0ecSDave Kleikamp continue; 523a4912123STheodore Ts'o if (stats.free_inodes < min_inodes) 524ac27a0ecSDave Kleikamp continue; 52524aaa8efSTheodore Ts'o if (stats.free_clusters < min_clusters) 526ac27a0ecSDave Kleikamp continue; 527a4912123STheodore Ts'o goto found_flex_bg; 528ac27a0ecSDave Kleikamp } 529ac27a0ecSDave Kleikamp 530ac27a0ecSDave Kleikamp fallback: 5318df9675fSTheodore Ts'o ngroups = real_ngroups; 532a4912123STheodore Ts'o avefreei = freei / ngroups; 533b5451f7bSTheodore Ts'o fallback_retry: 534a4912123STheodore Ts'o parent_group = EXT4_I(parent)->i_block_group; 535ac27a0ecSDave Kleikamp for (i = 0; i < ngroups; i++) { 536a4912123STheodore Ts'o grp = (parent_group + i) % ngroups; 537a4912123STheodore Ts'o desc = ext4_get_group_desc(sb, grp, NULL); 538bb3d132aSDan Carpenter if (desc) { 53914c83c9fSTheodore Ts'o grp_free = ext4_free_inodes_count(sb, desc); 540bb3d132aSDan Carpenter if (grp_free && grp_free >= avefreei) { 541a4912123STheodore Ts'o *group = grp; 5422aa9fc4cSAvantika Mathur return 0; 543ac27a0ecSDave Kleikamp } 544a4912123STheodore Ts'o } 545bb3d132aSDan Carpenter } 546ac27a0ecSDave Kleikamp 547ac27a0ecSDave Kleikamp if (avefreei) { 548ac27a0ecSDave Kleikamp /* 549ac27a0ecSDave Kleikamp * The free-inodes counter is approximate, and for really small 550ac27a0ecSDave Kleikamp * filesystems the above test can fail to find any blockgroups 551ac27a0ecSDave Kleikamp */ 552ac27a0ecSDave Kleikamp avefreei = 0; 553b5451f7bSTheodore Ts'o goto fallback_retry; 554ac27a0ecSDave Kleikamp } 555ac27a0ecSDave Kleikamp 556ac27a0ecSDave Kleikamp return -1; 557ac27a0ecSDave Kleikamp } 558ac27a0ecSDave Kleikamp 5592aa9fc4cSAvantika Mathur static int find_group_other(struct super_block *sb, struct inode *parent, 560dcca3fecSAl Viro ext4_group_t *group, umode_t mode) 561ac27a0ecSDave Kleikamp { 562fd2d4291SAvantika Mathur ext4_group_t parent_group = EXT4_I(parent)->i_block_group; 5638df9675fSTheodore Ts'o ext4_group_t i, last, ngroups = ext4_get_groups_count(sb); 564617ba13bSMingming Cao struct ext4_group_desc *desc; 565a4912123STheodore Ts'o int flex_size = ext4_flex_bg_size(EXT4_SB(sb)); 566a4912123STheodore Ts'o 567a4912123STheodore Ts'o /* 568a4912123STheodore Ts'o * Try to place the inode is the same flex group as its 569a4912123STheodore Ts'o * parent. If we can't find space, use the Orlov algorithm to 570a4912123STheodore Ts'o * find another flex group, and store that information in the 571a4912123STheodore Ts'o * parent directory's inode information so that use that flex 572a4912123STheodore Ts'o * group for future allocations. 573a4912123STheodore Ts'o */ 574a4912123STheodore Ts'o if (flex_size > 1) { 575a4912123STheodore Ts'o int retry = 0; 576a4912123STheodore Ts'o 577a4912123STheodore Ts'o try_again: 578a4912123STheodore Ts'o parent_group &= ~(flex_size-1); 579a4912123STheodore Ts'o last = parent_group + flex_size; 580a4912123STheodore Ts'o if (last > ngroups) 581a4912123STheodore Ts'o last = ngroups; 582a4912123STheodore Ts'o for (i = parent_group; i < last; i++) { 583a4912123STheodore Ts'o desc = ext4_get_group_desc(sb, i, NULL); 584a4912123STheodore Ts'o if (desc && ext4_free_inodes_count(sb, desc)) { 585a4912123STheodore Ts'o *group = i; 586a4912123STheodore Ts'o return 0; 587a4912123STheodore Ts'o } 588a4912123STheodore Ts'o } 589a4912123STheodore Ts'o if (!retry && EXT4_I(parent)->i_last_alloc_group != ~0) { 590a4912123STheodore Ts'o retry = 1; 591a4912123STheodore Ts'o parent_group = EXT4_I(parent)->i_last_alloc_group; 592a4912123STheodore Ts'o goto try_again; 593a4912123STheodore Ts'o } 594a4912123STheodore Ts'o /* 595a4912123STheodore Ts'o * If this didn't work, use the Orlov search algorithm 596a4912123STheodore Ts'o * to find a new flex group; we pass in the mode to 597a4912123STheodore Ts'o * avoid the topdir algorithms. 598a4912123STheodore Ts'o */ 599a4912123STheodore Ts'o *group = parent_group + flex_size; 600a4912123STheodore Ts'o if (*group > ngroups) 601a4912123STheodore Ts'o *group = 0; 6027dc57615SPeter Huewe return find_group_orlov(sb, parent, group, mode, NULL); 603a4912123STheodore Ts'o } 604ac27a0ecSDave Kleikamp 605ac27a0ecSDave Kleikamp /* 606ac27a0ecSDave Kleikamp * Try to place the inode in its parent directory 607ac27a0ecSDave Kleikamp */ 6082aa9fc4cSAvantika Mathur *group = parent_group; 6092aa9fc4cSAvantika Mathur desc = ext4_get_group_desc(sb, *group, NULL); 610560671a0SAneesh Kumar K.V if (desc && ext4_free_inodes_count(sb, desc) && 611021b65bbSTheodore Ts'o ext4_free_group_clusters(sb, desc)) 6122aa9fc4cSAvantika Mathur return 0; 613ac27a0ecSDave Kleikamp 614ac27a0ecSDave Kleikamp /* 615ac27a0ecSDave Kleikamp * We're going to place this inode in a different blockgroup from its 616ac27a0ecSDave Kleikamp * parent. We want to cause files in a common directory to all land in 617ac27a0ecSDave Kleikamp * the same blockgroup. But we want files which are in a different 618ac27a0ecSDave Kleikamp * directory which shares a blockgroup with our parent to land in a 619ac27a0ecSDave Kleikamp * different blockgroup. 620ac27a0ecSDave Kleikamp * 621ac27a0ecSDave Kleikamp * So add our directory's i_ino into the starting point for the hash. 622ac27a0ecSDave Kleikamp */ 6232aa9fc4cSAvantika Mathur *group = (*group + parent->i_ino) % ngroups; 624ac27a0ecSDave Kleikamp 625ac27a0ecSDave Kleikamp /* 626ac27a0ecSDave Kleikamp * Use a quadratic hash to find a group with a free inode and some free 627ac27a0ecSDave Kleikamp * blocks. 628ac27a0ecSDave Kleikamp */ 629ac27a0ecSDave Kleikamp for (i = 1; i < ngroups; i <<= 1) { 6302aa9fc4cSAvantika Mathur *group += i; 6312aa9fc4cSAvantika Mathur if (*group >= ngroups) 6322aa9fc4cSAvantika Mathur *group -= ngroups; 6332aa9fc4cSAvantika Mathur desc = ext4_get_group_desc(sb, *group, NULL); 634560671a0SAneesh Kumar K.V if (desc && ext4_free_inodes_count(sb, desc) && 635021b65bbSTheodore Ts'o ext4_free_group_clusters(sb, desc)) 6362aa9fc4cSAvantika Mathur return 0; 637ac27a0ecSDave Kleikamp } 638ac27a0ecSDave Kleikamp 639ac27a0ecSDave Kleikamp /* 640ac27a0ecSDave Kleikamp * That failed: try linear search for a free inode, even if that group 641ac27a0ecSDave Kleikamp * has no free blocks. 642ac27a0ecSDave Kleikamp */ 6432aa9fc4cSAvantika Mathur *group = parent_group; 644ac27a0ecSDave Kleikamp for (i = 0; i < ngroups; i++) { 6452aa9fc4cSAvantika Mathur if (++*group >= ngroups) 6462aa9fc4cSAvantika Mathur *group = 0; 6472aa9fc4cSAvantika Mathur desc = ext4_get_group_desc(sb, *group, NULL); 648560671a0SAneesh Kumar K.V if (desc && ext4_free_inodes_count(sb, desc)) 6492aa9fc4cSAvantika Mathur return 0; 650ac27a0ecSDave Kleikamp } 651ac27a0ecSDave Kleikamp 652ac27a0ecSDave Kleikamp return -1; 653ac27a0ecSDave Kleikamp } 654ac27a0ecSDave Kleikamp 655ac27a0ecSDave Kleikamp /* 65619883bd9STheodore Ts'o * In no journal mode, if an inode has recently been deleted, we want 65719883bd9STheodore Ts'o * to avoid reusing it until we're reasonably sure the inode table 65819883bd9STheodore Ts'o * block has been written back to disk. (Yes, these values are 65919883bd9STheodore Ts'o * somewhat arbitrary...) 66019883bd9STheodore Ts'o */ 66119883bd9STheodore Ts'o #define RECENTCY_MIN 5 66219883bd9STheodore Ts'o #define RECENTCY_DIRTY 30 66319883bd9STheodore Ts'o 66419883bd9STheodore Ts'o static int recently_deleted(struct super_block *sb, ext4_group_t group, int ino) 66519883bd9STheodore Ts'o { 66619883bd9STheodore Ts'o struct ext4_group_desc *gdp; 66719883bd9STheodore Ts'o struct ext4_inode *raw_inode; 66819883bd9STheodore Ts'o struct buffer_head *bh; 66919883bd9STheodore Ts'o unsigned long dtime, now; 67019883bd9STheodore Ts'o int inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 67119883bd9STheodore Ts'o int offset, ret = 0, recentcy = RECENTCY_MIN; 67219883bd9STheodore Ts'o 67319883bd9STheodore Ts'o gdp = ext4_get_group_desc(sb, group, NULL); 67419883bd9STheodore Ts'o if (unlikely(!gdp)) 67519883bd9STheodore Ts'o return 0; 67619883bd9STheodore Ts'o 67719883bd9STheodore Ts'o bh = sb_getblk(sb, ext4_inode_table(sb, gdp) + 67819883bd9STheodore Ts'o (ino / inodes_per_block)); 67919883bd9STheodore Ts'o if (unlikely(!bh) || !buffer_uptodate(bh)) 68019883bd9STheodore Ts'o /* 68119883bd9STheodore Ts'o * If the block is not in the buffer cache, then it 68219883bd9STheodore Ts'o * must have been written out. 68319883bd9STheodore Ts'o */ 68419883bd9STheodore Ts'o goto out; 68519883bd9STheodore Ts'o 68619883bd9STheodore Ts'o offset = (ino % inodes_per_block) * EXT4_INODE_SIZE(sb); 68719883bd9STheodore Ts'o raw_inode = (struct ext4_inode *) (bh->b_data + offset); 68819883bd9STheodore Ts'o dtime = le32_to_cpu(raw_inode->i_dtime); 68919883bd9STheodore Ts'o now = get_seconds(); 69019883bd9STheodore Ts'o if (buffer_dirty(bh)) 69119883bd9STheodore Ts'o recentcy += RECENTCY_DIRTY; 69219883bd9STheodore Ts'o 69319883bd9STheodore Ts'o if (dtime && (dtime < now) && (now < dtime + recentcy)) 69419883bd9STheodore Ts'o ret = 1; 69519883bd9STheodore Ts'o out: 69619883bd9STheodore Ts'o brelse(bh); 69719883bd9STheodore Ts'o return ret; 69819883bd9STheodore Ts'o } 69919883bd9STheodore Ts'o 70019883bd9STheodore Ts'o /* 701ac27a0ecSDave Kleikamp * There are two policies for allocating an inode. If the new inode is 702ac27a0ecSDave Kleikamp * a directory, then a forward search is made for a block group with both 703ac27a0ecSDave Kleikamp * free space and a low directory-to-inode ratio; if that fails, then of 704ac27a0ecSDave Kleikamp * the groups with above-average free space, that group with the fewest 705ac27a0ecSDave Kleikamp * directories already is chosen. 706ac27a0ecSDave Kleikamp * 707ac27a0ecSDave Kleikamp * For other inodes, search forward from the parent directory's block 708ac27a0ecSDave Kleikamp * group to find a free inode. 709ac27a0ecSDave Kleikamp */ 7101139575aSTheodore Ts'o struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir, 7111139575aSTheodore Ts'o umode_t mode, const struct qstr *qstr, 7121139575aSTheodore Ts'o __u32 goal, uid_t *owner, int handle_type, 7131139575aSTheodore Ts'o unsigned int line_no, int nblocks) 714ac27a0ecSDave Kleikamp { 715ac27a0ecSDave Kleikamp struct super_block *sb; 7163300bedaSAneesh Kumar K.V struct buffer_head *inode_bitmap_bh = NULL; 7173300bedaSAneesh Kumar K.V struct buffer_head *group_desc_bh; 7188df9675fSTheodore Ts'o ext4_group_t ngroups, group = 0; 719ac27a0ecSDave Kleikamp unsigned long ino = 0; 720ac27a0ecSDave Kleikamp struct inode *inode; 721617ba13bSMingming Cao struct ext4_group_desc *gdp = NULL; 722617ba13bSMingming Cao struct ext4_inode_info *ei; 723617ba13bSMingming Cao struct ext4_sb_info *sbi; 724*a7cdadeeSJan Kara int ret2, err; 725ac27a0ecSDave Kleikamp struct inode *ret; 7262aa9fc4cSAvantika Mathur ext4_group_t i; 727772cb7c8SJose R. Santos ext4_group_t flex_group; 72887a39389SDarrick J. Wong struct ext4_group_info *grp; 729e709e9dfSTheodore Ts'o int encrypt = 0; 730ac27a0ecSDave Kleikamp 731ac27a0ecSDave Kleikamp /* Cannot create files in a deleted directory */ 732ac27a0ecSDave Kleikamp if (!dir || !dir->i_nlink) 733ac27a0ecSDave Kleikamp return ERR_PTR(-EPERM); 734ac27a0ecSDave Kleikamp 735e709e9dfSTheodore Ts'o if ((ext4_encrypted_inode(dir) || 736e709e9dfSTheodore Ts'o DUMMY_ENCRYPTION_ENABLED(EXT4_SB(dir->i_sb))) && 737e709e9dfSTheodore Ts'o (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { 738e709e9dfSTheodore Ts'o err = ext4_get_encryption_info(dir); 739e709e9dfSTheodore Ts'o if (err) 740e709e9dfSTheodore Ts'o return ERR_PTR(err); 741e709e9dfSTheodore Ts'o if (ext4_encryption_info(dir) == NULL) 742e709e9dfSTheodore Ts'o return ERR_PTR(-EPERM); 743e709e9dfSTheodore Ts'o if (!handle) 744e709e9dfSTheodore Ts'o nblocks += EXT4_DATA_TRANS_BLOCKS(dir->i_sb); 745e709e9dfSTheodore Ts'o encrypt = 1; 746e709e9dfSTheodore Ts'o } 747e709e9dfSTheodore Ts'o 748ac27a0ecSDave Kleikamp sb = dir->i_sb; 7498df9675fSTheodore Ts'o ngroups = ext4_get_groups_count(sb); 7509bffad1eSTheodore Ts'o trace_ext4_request_inode(dir, mode); 751ac27a0ecSDave Kleikamp inode = new_inode(sb); 752ac27a0ecSDave Kleikamp if (!inode) 753ac27a0ecSDave Kleikamp return ERR_PTR(-ENOMEM); 754617ba13bSMingming Cao ei = EXT4_I(inode); 755617ba13bSMingming Cao sbi = EXT4_SB(sb); 756772cb7c8SJose R. Santos 757eb9cc7e1SJan Kara /* 758eb9cc7e1SJan Kara * Initalize owners and quota early so that we don't have to account 759eb9cc7e1SJan Kara * for quota initialization worst case in standard inode creating 760eb9cc7e1SJan Kara * transaction 761eb9cc7e1SJan Kara */ 762eb9cc7e1SJan Kara if (owner) { 763eb9cc7e1SJan Kara inode->i_mode = mode; 764eb9cc7e1SJan Kara i_uid_write(inode, owner[0]); 765eb9cc7e1SJan Kara i_gid_write(inode, owner[1]); 766eb9cc7e1SJan Kara } else if (test_opt(sb, GRPID)) { 767eb9cc7e1SJan Kara inode->i_mode = mode; 768eb9cc7e1SJan Kara inode->i_uid = current_fsuid(); 769eb9cc7e1SJan Kara inode->i_gid = dir->i_gid; 770eb9cc7e1SJan Kara } else 771eb9cc7e1SJan Kara inode_init_owner(inode, dir, mode); 772*a7cdadeeSJan Kara err = dquot_initialize(inode); 773*a7cdadeeSJan Kara if (err) 774*a7cdadeeSJan Kara goto out; 775eb9cc7e1SJan Kara 77611013911SAndreas Dilger if (!goal) 77711013911SAndreas Dilger goal = sbi->s_inode_goal; 77811013911SAndreas Dilger 779e6462869SJohann Lombardi if (goal && goal <= le32_to_cpu(sbi->s_es->s_inodes_count)) { 78011013911SAndreas Dilger group = (goal - 1) / EXT4_INODES_PER_GROUP(sb); 78111013911SAndreas Dilger ino = (goal - 1) % EXT4_INODES_PER_GROUP(sb); 78211013911SAndreas Dilger ret2 = 0; 78311013911SAndreas Dilger goto got_group; 78411013911SAndreas Dilger } 78511013911SAndreas Dilger 7864113c4caSLukas Czerner if (S_ISDIR(mode)) 787f157a4aaSTheodore Ts'o ret2 = find_group_orlov(sb, dir, &group, mode, qstr); 7884113c4caSLukas Czerner else 789a4912123STheodore Ts'o ret2 = find_group_other(sb, dir, &group, mode); 790ac27a0ecSDave Kleikamp 791772cb7c8SJose R. Santos got_group: 792a4912123STheodore Ts'o EXT4_I(dir)->i_last_alloc_group = group; 793ac27a0ecSDave Kleikamp err = -ENOSPC; 7942aa9fc4cSAvantika Mathur if (ret2 == -1) 795ac27a0ecSDave Kleikamp goto out; 796ac27a0ecSDave Kleikamp 797119c0d44STheodore Ts'o /* 798119c0d44STheodore Ts'o * Normally we will only go through one pass of this loop, 799119c0d44STheodore Ts'o * unless we get unlucky and it turns out the group we selected 800119c0d44STheodore Ts'o * had its last inode grabbed by someone else. 801119c0d44STheodore Ts'o */ 80211013911SAndreas Dilger for (i = 0; i < ngroups; i++, ino = 0) { 803ac27a0ecSDave Kleikamp err = -EIO; 804ac27a0ecSDave Kleikamp 8053300bedaSAneesh Kumar K.V gdp = ext4_get_group_desc(sb, group, &group_desc_bh); 806ac27a0ecSDave Kleikamp if (!gdp) 807eb9cc7e1SJan Kara goto out; 808ac27a0ecSDave Kleikamp 809f2a09af6SYongqiang Yang /* 810f2a09af6SYongqiang Yang * Check free inodes count before loading bitmap. 811f2a09af6SYongqiang Yang */ 812f2a09af6SYongqiang Yang if (ext4_free_inodes_count(sb, gdp) == 0) { 813f2a09af6SYongqiang Yang if (++group == ngroups) 814f2a09af6SYongqiang Yang group = 0; 815f2a09af6SYongqiang Yang continue; 816f2a09af6SYongqiang Yang } 817f2a09af6SYongqiang Yang 81887a39389SDarrick J. Wong grp = ext4_get_group_info(sb, group); 81987a39389SDarrick J. Wong /* Skip groups with already-known suspicious inode tables */ 82087a39389SDarrick J. Wong if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) { 82187a39389SDarrick J. Wong if (++group == ngroups) 82287a39389SDarrick J. Wong group = 0; 82387a39389SDarrick J. Wong continue; 82487a39389SDarrick J. Wong } 82587a39389SDarrick J. Wong 8263300bedaSAneesh Kumar K.V brelse(inode_bitmap_bh); 8273300bedaSAneesh Kumar K.V inode_bitmap_bh = ext4_read_inode_bitmap(sb, group); 82887a39389SDarrick J. Wong /* Skip groups with suspicious inode tables */ 82987a39389SDarrick J. Wong if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp) || !inode_bitmap_bh) { 83087a39389SDarrick J. Wong if (++group == ngroups) 83187a39389SDarrick J. Wong group = 0; 83287a39389SDarrick J. Wong continue; 83387a39389SDarrick J. Wong } 834ac27a0ecSDave Kleikamp 835ac27a0ecSDave Kleikamp repeat_in_this_group: 836617ba13bSMingming Cao ino = ext4_find_next_zero_bit((unsigned long *) 8373300bedaSAneesh Kumar K.V inode_bitmap_bh->b_data, 8383300bedaSAneesh Kumar K.V EXT4_INODES_PER_GROUP(sb), ino); 839a34eb503STheodore Ts'o if (ino >= EXT4_INODES_PER_GROUP(sb)) 840a34eb503STheodore Ts'o goto next_group; 841119c0d44STheodore Ts'o if (group == 0 && (ino+1) < EXT4_FIRST_INO(sb)) { 842119c0d44STheodore Ts'o ext4_error(sb, "reserved inode found cleared - " 843119c0d44STheodore Ts'o "inode=%lu", ino + 1); 844119c0d44STheodore Ts'o continue; 845119c0d44STheodore Ts'o } 84619883bd9STheodore Ts'o if ((EXT4_SB(sb)->s_journal == NULL) && 84719883bd9STheodore Ts'o recently_deleted(sb, group, ino)) { 84819883bd9STheodore Ts'o ino++; 84919883bd9STheodore Ts'o goto next_inode; 85019883bd9STheodore Ts'o } 8511139575aSTheodore Ts'o if (!handle) { 8521139575aSTheodore Ts'o BUG_ON(nblocks <= 0); 8531139575aSTheodore Ts'o handle = __ext4_journal_start_sb(dir->i_sb, line_no, 8545fe2fe89SJan Kara handle_type, nblocks, 8555fe2fe89SJan Kara 0); 8561139575aSTheodore Ts'o if (IS_ERR(handle)) { 8571139575aSTheodore Ts'o err = PTR_ERR(handle); 858eb9cc7e1SJan Kara ext4_std_error(sb, err); 859eb9cc7e1SJan Kara goto out; 8601139575aSTheodore Ts'o } 8611139575aSTheodore Ts'o } 862ffb5387eSEric Sandeen BUFFER_TRACE(inode_bitmap_bh, "get_write_access"); 863ffb5387eSEric Sandeen err = ext4_journal_get_write_access(handle, inode_bitmap_bh); 864eb9cc7e1SJan Kara if (err) { 865eb9cc7e1SJan Kara ext4_std_error(sb, err); 866eb9cc7e1SJan Kara goto out; 867eb9cc7e1SJan Kara } 868119c0d44STheodore Ts'o ext4_lock_group(sb, group); 869119c0d44STheodore Ts'o ret2 = ext4_test_and_set_bit(ino, inode_bitmap_bh->b_data); 870119c0d44STheodore Ts'o ext4_unlock_group(sb, group); 871119c0d44STheodore Ts'o ino++; /* the inode bitmap is zero-based */ 872119c0d44STheodore Ts'o if (!ret2) 873119c0d44STheodore Ts'o goto got; /* we grabbed the inode! */ 87419883bd9STheodore Ts'o next_inode: 875119c0d44STheodore Ts'o if (ino < EXT4_INODES_PER_GROUP(sb)) 876119c0d44STheodore Ts'o goto repeat_in_this_group; 877a34eb503STheodore Ts'o next_group: 878a34eb503STheodore Ts'o if (++group == ngroups) 879a34eb503STheodore Ts'o group = 0; 880ac27a0ecSDave Kleikamp } 881ac27a0ecSDave Kleikamp err = -ENOSPC; 882ac27a0ecSDave Kleikamp goto out; 883ac27a0ecSDave Kleikamp 884ac27a0ecSDave Kleikamp got: 885ffb5387eSEric Sandeen BUFFER_TRACE(inode_bitmap_bh, "call ext4_handle_dirty_metadata"); 886ffb5387eSEric Sandeen err = ext4_handle_dirty_metadata(handle, NULL, inode_bitmap_bh); 887eb9cc7e1SJan Kara if (err) { 888eb9cc7e1SJan Kara ext4_std_error(sb, err); 889eb9cc7e1SJan Kara goto out; 890eb9cc7e1SJan Kara } 891ffb5387eSEric Sandeen 89261c219f5STheodore Ts'o BUFFER_TRACE(group_desc_bh, "get_write_access"); 89361c219f5STheodore Ts'o err = ext4_journal_get_write_access(handle, group_desc_bh); 89461c219f5STheodore Ts'o if (err) { 89561c219f5STheodore Ts'o ext4_std_error(sb, err); 89661c219f5STheodore Ts'o goto out; 89761c219f5STheodore Ts'o } 89861c219f5STheodore Ts'o 899717d50e4SAndreas Dilger /* We may have to initialize the block bitmap if it isn't already */ 900feb0ab32SDarrick J. Wong if (ext4_has_group_desc_csum(sb) && 901717d50e4SAndreas Dilger gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { 9023300bedaSAneesh Kumar K.V struct buffer_head *block_bitmap_bh; 903717d50e4SAndreas Dilger 9043300bedaSAneesh Kumar K.V block_bitmap_bh = ext4_read_block_bitmap(sb, group); 905599a9b77SJan Kara if (!block_bitmap_bh) { 906599a9b77SJan Kara err = -EIO; 907599a9b77SJan Kara goto out; 908599a9b77SJan Kara } 9093300bedaSAneesh Kumar K.V BUFFER_TRACE(block_bitmap_bh, "get block bitmap access"); 9103300bedaSAneesh Kumar K.V err = ext4_journal_get_write_access(handle, block_bitmap_bh); 911717d50e4SAndreas Dilger if (err) { 9123300bedaSAneesh Kumar K.V brelse(block_bitmap_bh); 913eb9cc7e1SJan Kara ext4_std_error(sb, err); 914eb9cc7e1SJan Kara goto out; 915717d50e4SAndreas Dilger } 916717d50e4SAndreas Dilger 917fd034a84STheodore Ts'o BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap"); 918fd034a84STheodore Ts'o err = ext4_handle_dirty_metadata(handle, NULL, block_bitmap_bh); 919fd034a84STheodore Ts'o 920717d50e4SAndreas Dilger /* recheck and clear flag under lock if we still need to */ 921fd034a84STheodore Ts'o ext4_lock_group(sb, group); 922717d50e4SAndreas Dilger if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { 9233300bedaSAneesh Kumar K.V gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT); 924021b65bbSTheodore Ts'o ext4_free_group_clusters_set(sb, gdp, 925cff1dfd7STheodore Ts'o ext4_free_clusters_after_init(sb, group, gdp)); 926fa77dcfaSDarrick J. Wong ext4_block_bitmap_csum_set(sb, group, gdp, 92779f1ba49STao Ma block_bitmap_bh); 928feb0ab32SDarrick J. Wong ext4_group_desc_csum_set(sb, group, gdp); 929717d50e4SAndreas Dilger } 930955ce5f5SAneesh Kumar K.V ext4_unlock_group(sb, group); 931aeb1e5d6STheodore Ts'o brelse(block_bitmap_bh); 932717d50e4SAndreas Dilger 933eb9cc7e1SJan Kara if (err) { 934eb9cc7e1SJan Kara ext4_std_error(sb, err); 935eb9cc7e1SJan Kara goto out; 936eb9cc7e1SJan Kara } 937717d50e4SAndreas Dilger } 938119c0d44STheodore Ts'o 939119c0d44STheodore Ts'o /* Update the relevant bg descriptor fields */ 94041a246d1SDarrick J. Wong if (ext4_has_group_desc_csum(sb)) { 941119c0d44STheodore Ts'o int free; 942119c0d44STheodore Ts'o struct ext4_group_info *grp = ext4_get_group_info(sb, group); 943119c0d44STheodore Ts'o 944119c0d44STheodore Ts'o down_read(&grp->alloc_sem); /* protect vs itable lazyinit */ 945119c0d44STheodore Ts'o ext4_lock_group(sb, group); /* while we modify the bg desc */ 946119c0d44STheodore Ts'o free = EXT4_INODES_PER_GROUP(sb) - 947119c0d44STheodore Ts'o ext4_itable_unused_count(sb, gdp); 948119c0d44STheodore Ts'o if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) { 949119c0d44STheodore Ts'o gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT); 950119c0d44STheodore Ts'o free = 0; 951119c0d44STheodore Ts'o } 952119c0d44STheodore Ts'o /* 953119c0d44STheodore Ts'o * Check the relative inode number against the last used 954119c0d44STheodore Ts'o * relative inode number in this group. if it is greater 955119c0d44STheodore Ts'o * we need to update the bg_itable_unused count 956119c0d44STheodore Ts'o */ 957119c0d44STheodore Ts'o if (ino > free) 958119c0d44STheodore Ts'o ext4_itable_unused_set(sb, gdp, 959119c0d44STheodore Ts'o (EXT4_INODES_PER_GROUP(sb) - ino)); 960119c0d44STheodore Ts'o up_read(&grp->alloc_sem); 9616f2e9f0eSTao Ma } else { 9626f2e9f0eSTao Ma ext4_lock_group(sb, group); 963119c0d44STheodore Ts'o } 9646f2e9f0eSTao Ma 965119c0d44STheodore Ts'o ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1); 966119c0d44STheodore Ts'o if (S_ISDIR(mode)) { 967119c0d44STheodore Ts'o ext4_used_dirs_set(sb, gdp, ext4_used_dirs_count(sb, gdp) + 1); 968119c0d44STheodore Ts'o if (sbi->s_log_groups_per_flex) { 969119c0d44STheodore Ts'o ext4_group_t f = ext4_flex_group(sbi, group); 970119c0d44STheodore Ts'o 971119c0d44STheodore Ts'o atomic_inc(&sbi->s_flex_groups[f].used_dirs); 972119c0d44STheodore Ts'o } 973119c0d44STheodore Ts'o } 97441a246d1SDarrick J. Wong if (ext4_has_group_desc_csum(sb)) { 97541a246d1SDarrick J. Wong ext4_inode_bitmap_csum_set(sb, group, gdp, inode_bitmap_bh, 97641a246d1SDarrick J. Wong EXT4_INODES_PER_GROUP(sb) / 8); 977feb0ab32SDarrick J. Wong ext4_group_desc_csum_set(sb, group, gdp); 978119c0d44STheodore Ts'o } 9796f2e9f0eSTao Ma ext4_unlock_group(sb, group); 980119c0d44STheodore Ts'o 9813300bedaSAneesh Kumar K.V BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata"); 9823300bedaSAneesh Kumar K.V err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh); 983eb9cc7e1SJan Kara if (err) { 984eb9cc7e1SJan Kara ext4_std_error(sb, err); 985eb9cc7e1SJan Kara goto out; 986eb9cc7e1SJan Kara } 987ac27a0ecSDave Kleikamp 988ac27a0ecSDave Kleikamp percpu_counter_dec(&sbi->s_freeinodes_counter); 989ac27a0ecSDave Kleikamp if (S_ISDIR(mode)) 990ac27a0ecSDave Kleikamp percpu_counter_inc(&sbi->s_dirs_counter); 991ac27a0ecSDave Kleikamp 992772cb7c8SJose R. Santos if (sbi->s_log_groups_per_flex) { 993772cb7c8SJose R. Santos flex_group = ext4_flex_group(sbi, group); 9949f24e420STheodore Ts'o atomic_dec(&sbi->s_flex_groups[flex_group].free_inodes); 995772cb7c8SJose R. Santos } 996ac27a0ecSDave Kleikamp 997717d50e4SAndreas Dilger inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb); 998ac27a0ecSDave Kleikamp /* This is the optimal IO size (for stat), not the fs block size */ 999ac27a0ecSDave Kleikamp inode->i_blocks = 0; 1000ef7f3835SKalpak Shah inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime = 1001ef7f3835SKalpak Shah ext4_current_time(inode); 1002ac27a0ecSDave Kleikamp 1003ac27a0ecSDave Kleikamp memset(ei->i_data, 0, sizeof(ei->i_data)); 1004ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 1005ac27a0ecSDave Kleikamp ei->i_disksize = 0; 1006ac27a0ecSDave Kleikamp 10074af83508SEryu Guan /* Don't inherit extent flag from directory, amongst others. */ 10082dc6b0d4SDuane Griffin ei->i_flags = 10092dc6b0d4SDuane Griffin ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED); 1010ac27a0ecSDave Kleikamp ei->i_file_acl = 0; 1011ac27a0ecSDave Kleikamp ei->i_dtime = 0; 1012ac27a0ecSDave Kleikamp ei->i_block_group = group; 1013a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 1014ac27a0ecSDave Kleikamp 1015617ba13bSMingming Cao ext4_set_inode_flags(inode); 1016ac27a0ecSDave Kleikamp if (IS_DIRSYNC(inode)) 10170390131bSFrank Mayhar ext4_handle_sync(handle); 10186b38e842SAl Viro if (insert_inode_locked(inode) < 0) { 1019acd6ad83SJan Kara /* 1020acd6ad83SJan Kara * Likely a bitmap corruption causing inode to be allocated 1021acd6ad83SJan Kara * twice. 1022acd6ad83SJan Kara */ 1023acd6ad83SJan Kara err = -EIO; 1024eb9cc7e1SJan Kara ext4_error(sb, "failed to insert inode %lu: doubly allocated?", 1025eb9cc7e1SJan Kara inode->i_ino); 1026eb9cc7e1SJan Kara goto out; 10276b38e842SAl Viro } 1028ac27a0ecSDave Kleikamp spin_lock(&sbi->s_next_gen_lock); 1029ac27a0ecSDave Kleikamp inode->i_generation = sbi->s_next_generation++; 1030ac27a0ecSDave Kleikamp spin_unlock(&sbi->s_next_gen_lock); 1031ac27a0ecSDave Kleikamp 1032814525f4SDarrick J. Wong /* Precompute checksum seed for inode metadata */ 10339aa5d32bSDmitry Monakhov if (ext4_has_metadata_csum(sb)) { 1034814525f4SDarrick J. Wong __u32 csum; 1035814525f4SDarrick J. Wong __le32 inum = cpu_to_le32(inode->i_ino); 1036814525f4SDarrick J. Wong __le32 gen = cpu_to_le32(inode->i_generation); 1037814525f4SDarrick J. Wong csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, 1038814525f4SDarrick J. Wong sizeof(inum)); 1039814525f4SDarrick J. Wong ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, 1040814525f4SDarrick J. Wong sizeof(gen)); 1041814525f4SDarrick J. Wong } 1042814525f4SDarrick J. Wong 1043353eb83cSTheodore Ts'o ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ 104419f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_NEW); 1045ef7f3835SKalpak Shah 1046ef7f3835SKalpak Shah ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize; 1047f08225d1STao Ma ei->i_inline_off = 0; 1048f08225d1STao Ma if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_INLINE_DATA)) 1049f08225d1STao Ma ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); 1050ac27a0ecSDave Kleikamp ret = inode; 105163936ddaSChristoph Hellwig err = dquot_alloc_inode(inode); 105263936ddaSChristoph Hellwig if (err) 1053ac27a0ecSDave Kleikamp goto fail_drop; 1054ac27a0ecSDave Kleikamp 1055617ba13bSMingming Cao err = ext4_init_acl(handle, inode, dir); 1056ac27a0ecSDave Kleikamp if (err) 1057ac27a0ecSDave Kleikamp goto fail_free_drop; 1058ac27a0ecSDave Kleikamp 10592a7dba39SEric Paris err = ext4_init_security(handle, inode, dir, qstr); 1060ac27a0ecSDave Kleikamp if (err) 1061ac27a0ecSDave Kleikamp goto fail_free_drop; 1062ac27a0ecSDave Kleikamp 106383982b6fSTheodore Ts'o if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) { 1064e4079a11SEric Sandeen /* set extent flag only for directory, file and normal symlink*/ 1065e65187e6SAneesh Kumar K.V if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) { 106612e9b892SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS); 1067a86c6181SAlex Tomas ext4_ext_tree_init(handle, inode); 1068a86c6181SAlex Tomas } 106942bf0383SAneesh Kumar K.V } 1070ac27a0ecSDave Kleikamp 1071688f869cSTheodore Ts'o if (ext4_handle_valid(handle)) { 1072688f869cSTheodore Ts'o ei->i_sync_tid = handle->h_transaction->t_tid; 1073688f869cSTheodore Ts'o ei->i_datasync_tid = handle->h_transaction->t_tid; 1074688f869cSTheodore Ts'o } 1075688f869cSTheodore Ts'o 1076e709e9dfSTheodore Ts'o if (encrypt) { 1077e709e9dfSTheodore Ts'o err = ext4_inherit_context(dir, inode); 1078e709e9dfSTheodore Ts'o if (err) 1079e709e9dfSTheodore Ts'o goto fail_free_drop; 1080e709e9dfSTheodore Ts'o } 1081e709e9dfSTheodore Ts'o 10828753e88fSAneesh Kumar K.V err = ext4_mark_inode_dirty(handle, inode); 10838753e88fSAneesh Kumar K.V if (err) { 10848753e88fSAneesh Kumar K.V ext4_std_error(sb, err); 10858753e88fSAneesh Kumar K.V goto fail_free_drop; 10868753e88fSAneesh Kumar K.V } 10878753e88fSAneesh Kumar K.V 1088617ba13bSMingming Cao ext4_debug("allocating inode %lu\n", inode->i_ino); 10899bffad1eSTheodore Ts'o trace_ext4_allocate_inode(inode, dir, mode); 10903300bedaSAneesh Kumar K.V brelse(inode_bitmap_bh); 1091ac27a0ecSDave Kleikamp return ret; 1092ac27a0ecSDave Kleikamp 1093ac27a0ecSDave Kleikamp fail_free_drop: 109463936ddaSChristoph Hellwig dquot_free_inode(inode); 1095ac27a0ecSDave Kleikamp fail_drop: 10966d6b77f1SMiklos Szeredi clear_nlink(inode); 10976b38e842SAl Viro unlock_new_inode(inode); 1098eb9cc7e1SJan Kara out: 1099eb9cc7e1SJan Kara dquot_drop(inode); 1100eb9cc7e1SJan Kara inode->i_flags |= S_NOQUOTA; 1101ac27a0ecSDave Kleikamp iput(inode); 11023300bedaSAneesh Kumar K.V brelse(inode_bitmap_bh); 1103ac27a0ecSDave Kleikamp return ERR_PTR(err); 1104ac27a0ecSDave Kleikamp } 1105ac27a0ecSDave Kleikamp 1106ac27a0ecSDave Kleikamp /* Verify that we are loading a valid orphan from disk */ 1107617ba13bSMingming Cao struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino) 1108ac27a0ecSDave Kleikamp { 1109617ba13bSMingming Cao unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count); 1110fd2d4291SAvantika Mathur ext4_group_t block_group; 1111ac27a0ecSDave Kleikamp int bit; 11121d1fe1eeSDavid Howells struct buffer_head *bitmap_bh; 1113ac27a0ecSDave Kleikamp struct inode *inode = NULL; 11141d1fe1eeSDavid Howells long err = -EIO; 1115ac27a0ecSDave Kleikamp 1116ac27a0ecSDave Kleikamp /* Error cases - e2fsck has already cleaned up for us */ 1117ac27a0ecSDave Kleikamp if (ino > max_ino) { 111812062dddSEric Sandeen ext4_warning(sb, "bad orphan ino %lu! e2fsck was run?", ino); 11191d1fe1eeSDavid Howells goto error; 1120ac27a0ecSDave Kleikamp } 1121ac27a0ecSDave Kleikamp 1122617ba13bSMingming Cao block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); 1123617ba13bSMingming Cao bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); 1124e29d1cdeSEric Sandeen bitmap_bh = ext4_read_inode_bitmap(sb, block_group); 1125ac27a0ecSDave Kleikamp if (!bitmap_bh) { 112612062dddSEric Sandeen ext4_warning(sb, "inode bitmap error for orphan %lu", ino); 11271d1fe1eeSDavid Howells goto error; 1128ac27a0ecSDave Kleikamp } 1129ac27a0ecSDave Kleikamp 1130ac27a0ecSDave Kleikamp /* Having the inode bit set should be a 100% indicator that this 1131ac27a0ecSDave Kleikamp * is a valid orphan (no e2fsck run on fs). Orphans also include 1132ac27a0ecSDave Kleikamp * inodes that were being truncated, so we can't check i_nlink==0. 1133ac27a0ecSDave Kleikamp */ 11341d1fe1eeSDavid Howells if (!ext4_test_bit(bit, bitmap_bh->b_data)) 11351d1fe1eeSDavid Howells goto bad_orphan; 11361d1fe1eeSDavid Howells 11371d1fe1eeSDavid Howells inode = ext4_iget(sb, ino); 11381d1fe1eeSDavid Howells if (IS_ERR(inode)) 11391d1fe1eeSDavid Howells goto iget_failed; 11401d1fe1eeSDavid Howells 114191ef4cafSDuane Griffin /* 114291ef4cafSDuane Griffin * If the orphans has i_nlinks > 0 then it should be able to be 114391ef4cafSDuane Griffin * truncated, otherwise it won't be removed from the orphan list 114491ef4cafSDuane Griffin * during processing and an infinite loop will result. 114591ef4cafSDuane Griffin */ 114691ef4cafSDuane Griffin if (inode->i_nlink && !ext4_can_truncate(inode)) 114791ef4cafSDuane Griffin goto bad_orphan; 114891ef4cafSDuane Griffin 11491d1fe1eeSDavid Howells if (NEXT_ORPHAN(inode) > max_ino) 11501d1fe1eeSDavid Howells goto bad_orphan; 11511d1fe1eeSDavid Howells brelse(bitmap_bh); 11521d1fe1eeSDavid Howells return inode; 11531d1fe1eeSDavid Howells 11541d1fe1eeSDavid Howells iget_failed: 11551d1fe1eeSDavid Howells err = PTR_ERR(inode); 11561d1fe1eeSDavid Howells inode = NULL; 11571d1fe1eeSDavid Howells bad_orphan: 115812062dddSEric Sandeen ext4_warning(sb, "bad orphan inode %lu! e2fsck was run?", ino); 11598de5c325STheodore Ts'o printk(KERN_WARNING "ext4_test_bit(bit=%d, block=%llu) = %d\n", 1160ac27a0ecSDave Kleikamp bit, (unsigned long long)bitmap_bh->b_blocknr, 1161617ba13bSMingming Cao ext4_test_bit(bit, bitmap_bh->b_data)); 11628de5c325STheodore Ts'o printk(KERN_WARNING "inode=%p\n", inode); 1163ac27a0ecSDave Kleikamp if (inode) { 11648de5c325STheodore Ts'o printk(KERN_WARNING "is_bad_inode(inode)=%d\n", 1165ac27a0ecSDave Kleikamp is_bad_inode(inode)); 11668de5c325STheodore Ts'o printk(KERN_WARNING "NEXT_ORPHAN(inode)=%u\n", 1167ac27a0ecSDave Kleikamp NEXT_ORPHAN(inode)); 11688de5c325STheodore Ts'o printk(KERN_WARNING "max_ino=%lu\n", max_ino); 11698de5c325STheodore Ts'o printk(KERN_WARNING "i_nlink=%u\n", inode->i_nlink); 1170ac27a0ecSDave Kleikamp /* Avoid freeing blocks if we got a bad deleted inode */ 11711d1fe1eeSDavid Howells if (inode->i_nlink == 0) 1172ac27a0ecSDave Kleikamp inode->i_blocks = 0; 1173ac27a0ecSDave Kleikamp iput(inode); 1174ac27a0ecSDave Kleikamp } 1175ac27a0ecSDave Kleikamp brelse(bitmap_bh); 11761d1fe1eeSDavid Howells error: 11771d1fe1eeSDavid Howells return ERR_PTR(err); 1178ac27a0ecSDave Kleikamp } 1179ac27a0ecSDave Kleikamp 1180617ba13bSMingming Cao unsigned long ext4_count_free_inodes(struct super_block *sb) 1181ac27a0ecSDave Kleikamp { 1182ac27a0ecSDave Kleikamp unsigned long desc_count; 1183617ba13bSMingming Cao struct ext4_group_desc *gdp; 11848df9675fSTheodore Ts'o ext4_group_t i, ngroups = ext4_get_groups_count(sb); 1185617ba13bSMingming Cao #ifdef EXT4FS_DEBUG 1186617ba13bSMingming Cao struct ext4_super_block *es; 1187ac27a0ecSDave Kleikamp unsigned long bitmap_count, x; 1188ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh = NULL; 1189ac27a0ecSDave Kleikamp 1190617ba13bSMingming Cao es = EXT4_SB(sb)->s_es; 1191ac27a0ecSDave Kleikamp desc_count = 0; 1192ac27a0ecSDave Kleikamp bitmap_count = 0; 1193ac27a0ecSDave Kleikamp gdp = NULL; 11948df9675fSTheodore Ts'o for (i = 0; i < ngroups; i++) { 1195617ba13bSMingming Cao gdp = ext4_get_group_desc(sb, i, NULL); 1196ac27a0ecSDave Kleikamp if (!gdp) 1197ac27a0ecSDave Kleikamp continue; 1198560671a0SAneesh Kumar K.V desc_count += ext4_free_inodes_count(sb, gdp); 1199ac27a0ecSDave Kleikamp brelse(bitmap_bh); 1200e29d1cdeSEric Sandeen bitmap_bh = ext4_read_inode_bitmap(sb, i); 1201ac27a0ecSDave Kleikamp if (!bitmap_bh) 1202ac27a0ecSDave Kleikamp continue; 1203ac27a0ecSDave Kleikamp 1204f6fb99caSTheodore Ts'o x = ext4_count_free(bitmap_bh->b_data, 1205f6fb99caSTheodore Ts'o EXT4_INODES_PER_GROUP(sb) / 8); 1206c549a95dSEric Sandeen printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n", 1207785b4b3aSPeng Tao (unsigned long) i, ext4_free_inodes_count(sb, gdp), x); 1208ac27a0ecSDave Kleikamp bitmap_count += x; 1209ac27a0ecSDave Kleikamp } 1210ac27a0ecSDave Kleikamp brelse(bitmap_bh); 12114776004fSTheodore Ts'o printk(KERN_DEBUG "ext4_count_free_inodes: " 12124776004fSTheodore Ts'o "stored = %u, computed = %lu, %lu\n", 1213ac27a0ecSDave Kleikamp le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count); 1214ac27a0ecSDave Kleikamp return desc_count; 1215ac27a0ecSDave Kleikamp #else 1216ac27a0ecSDave Kleikamp desc_count = 0; 12178df9675fSTheodore Ts'o for (i = 0; i < ngroups; i++) { 1218617ba13bSMingming Cao gdp = ext4_get_group_desc(sb, i, NULL); 1219ac27a0ecSDave Kleikamp if (!gdp) 1220ac27a0ecSDave Kleikamp continue; 1221560671a0SAneesh Kumar K.V desc_count += ext4_free_inodes_count(sb, gdp); 1222ac27a0ecSDave Kleikamp cond_resched(); 1223ac27a0ecSDave Kleikamp } 1224ac27a0ecSDave Kleikamp return desc_count; 1225ac27a0ecSDave Kleikamp #endif 1226ac27a0ecSDave Kleikamp } 1227ac27a0ecSDave Kleikamp 1228ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */ 1229617ba13bSMingming Cao unsigned long ext4_count_dirs(struct super_block * sb) 1230ac27a0ecSDave Kleikamp { 1231ac27a0ecSDave Kleikamp unsigned long count = 0; 12328df9675fSTheodore Ts'o ext4_group_t i, ngroups = ext4_get_groups_count(sb); 1233ac27a0ecSDave Kleikamp 12348df9675fSTheodore Ts'o for (i = 0; i < ngroups; i++) { 1235617ba13bSMingming Cao struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL); 1236ac27a0ecSDave Kleikamp if (!gdp) 1237ac27a0ecSDave Kleikamp continue; 1238560671a0SAneesh Kumar K.V count += ext4_used_dirs_count(sb, gdp); 1239ac27a0ecSDave Kleikamp } 1240ac27a0ecSDave Kleikamp return count; 1241ac27a0ecSDave Kleikamp } 1242bfff6873SLukas Czerner 1243bfff6873SLukas Czerner /* 1244bfff6873SLukas Czerner * Zeroes not yet zeroed inode table - just write zeroes through the whole 1245bfff6873SLukas Czerner * inode table. Must be called without any spinlock held. The only place 1246bfff6873SLukas Czerner * where it is called from on active part of filesystem is ext4lazyinit 1247bfff6873SLukas Czerner * thread, so we do not need any special locks, however we have to prevent 1248bfff6873SLukas Czerner * inode allocation from the current group, so we take alloc_sem lock, to 1249119c0d44STheodore Ts'o * block ext4_new_inode() until we are finished. 1250bfff6873SLukas Czerner */ 1251e0cbee3eSH Hartley Sweeten int ext4_init_inode_table(struct super_block *sb, ext4_group_t group, 1252bfff6873SLukas Czerner int barrier) 1253bfff6873SLukas Czerner { 1254bfff6873SLukas Czerner struct ext4_group_info *grp = ext4_get_group_info(sb, group); 1255bfff6873SLukas Czerner struct ext4_sb_info *sbi = EXT4_SB(sb); 1256bfff6873SLukas Czerner struct ext4_group_desc *gdp = NULL; 1257bfff6873SLukas Czerner struct buffer_head *group_desc_bh; 1258bfff6873SLukas Czerner handle_t *handle; 1259bfff6873SLukas Czerner ext4_fsblk_t blk; 1260bfff6873SLukas Czerner int num, ret = 0, used_blks = 0; 1261bfff6873SLukas Czerner 1262bfff6873SLukas Czerner /* This should not happen, but just to be sure check this */ 1263bfff6873SLukas Czerner if (sb->s_flags & MS_RDONLY) { 1264bfff6873SLukas Czerner ret = 1; 1265bfff6873SLukas Czerner goto out; 1266bfff6873SLukas Czerner } 1267bfff6873SLukas Czerner 1268bfff6873SLukas Czerner gdp = ext4_get_group_desc(sb, group, &group_desc_bh); 1269bfff6873SLukas Czerner if (!gdp) 1270bfff6873SLukas Czerner goto out; 1271bfff6873SLukas Czerner 1272bfff6873SLukas Czerner /* 1273bfff6873SLukas Czerner * We do not need to lock this, because we are the only one 1274bfff6873SLukas Czerner * handling this flag. 1275bfff6873SLukas Czerner */ 1276bfff6873SLukas Czerner if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)) 1277bfff6873SLukas Czerner goto out; 1278bfff6873SLukas Czerner 12799924a92aSTheodore Ts'o handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1); 1280bfff6873SLukas Czerner if (IS_ERR(handle)) { 1281bfff6873SLukas Czerner ret = PTR_ERR(handle); 1282bfff6873SLukas Czerner goto out; 1283bfff6873SLukas Czerner } 1284bfff6873SLukas Czerner 1285bfff6873SLukas Czerner down_write(&grp->alloc_sem); 1286bfff6873SLukas Czerner /* 1287bfff6873SLukas Czerner * If inode bitmap was already initialized there may be some 1288bfff6873SLukas Czerner * used inodes so we need to skip blocks with used inodes in 1289bfff6873SLukas Czerner * inode table. 1290bfff6873SLukas Czerner */ 1291bfff6873SLukas Czerner if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT))) 1292bfff6873SLukas Czerner used_blks = DIV_ROUND_UP((EXT4_INODES_PER_GROUP(sb) - 1293bfff6873SLukas Czerner ext4_itable_unused_count(sb, gdp)), 1294bfff6873SLukas Czerner sbi->s_inodes_per_block); 1295bfff6873SLukas Czerner 1296857ac889SLukas Czerner if ((used_blks < 0) || (used_blks > sbi->s_itb_per_group)) { 12971084f252STheodore Ts'o ext4_error(sb, "Something is wrong with group %u: " 12981084f252STheodore Ts'o "used itable blocks: %d; " 12991084f252STheodore Ts'o "itable unused count: %u", 1300857ac889SLukas Czerner group, used_blks, 1301857ac889SLukas Czerner ext4_itable_unused_count(sb, gdp)); 1302857ac889SLukas Czerner ret = 1; 130333853a0dSYongqiang Yang goto err_out; 1304857ac889SLukas Czerner } 1305857ac889SLukas Czerner 1306bfff6873SLukas Czerner blk = ext4_inode_table(sb, gdp) + used_blks; 1307bfff6873SLukas Czerner num = sbi->s_itb_per_group - used_blks; 1308bfff6873SLukas Czerner 1309bfff6873SLukas Czerner BUFFER_TRACE(group_desc_bh, "get_write_access"); 1310bfff6873SLukas Czerner ret = ext4_journal_get_write_access(handle, 1311bfff6873SLukas Czerner group_desc_bh); 1312bfff6873SLukas Czerner if (ret) 1313bfff6873SLukas Czerner goto err_out; 1314bfff6873SLukas Czerner 1315bfff6873SLukas Czerner /* 1316bfff6873SLukas Czerner * Skip zeroout if the inode table is full. But we set the ZEROED 1317bfff6873SLukas Czerner * flag anyway, because obviously, when it is full it does not need 1318bfff6873SLukas Czerner * further zeroing. 1319bfff6873SLukas Czerner */ 1320bfff6873SLukas Czerner if (unlikely(num == 0)) 1321bfff6873SLukas Czerner goto skip_zeroout; 1322bfff6873SLukas Czerner 1323bfff6873SLukas Czerner ext4_debug("going to zero out inode table in group %d\n", 1324bfff6873SLukas Czerner group); 1325a107e5a3STheodore Ts'o ret = sb_issue_zeroout(sb, blk, num, GFP_NOFS); 1326bfff6873SLukas Czerner if (ret < 0) 1327bfff6873SLukas Czerner goto err_out; 1328a107e5a3STheodore Ts'o if (barrier) 1329a107e5a3STheodore Ts'o blkdev_issue_flush(sb->s_bdev, GFP_NOFS, NULL); 1330bfff6873SLukas Czerner 1331bfff6873SLukas Czerner skip_zeroout: 1332bfff6873SLukas Czerner ext4_lock_group(sb, group); 1333bfff6873SLukas Czerner gdp->bg_flags |= cpu_to_le16(EXT4_BG_INODE_ZEROED); 1334feb0ab32SDarrick J. Wong ext4_group_desc_csum_set(sb, group, gdp); 1335bfff6873SLukas Czerner ext4_unlock_group(sb, group); 1336bfff6873SLukas Czerner 1337bfff6873SLukas Czerner BUFFER_TRACE(group_desc_bh, 1338bfff6873SLukas Czerner "call ext4_handle_dirty_metadata"); 1339bfff6873SLukas Czerner ret = ext4_handle_dirty_metadata(handle, NULL, 1340bfff6873SLukas Czerner group_desc_bh); 1341bfff6873SLukas Czerner 1342bfff6873SLukas Czerner err_out: 1343bfff6873SLukas Czerner up_write(&grp->alloc_sem); 1344bfff6873SLukas Czerner ext4_journal_stop(handle); 1345bfff6873SLukas Czerner out: 1346bfff6873SLukas Czerner return ret; 1347bfff6873SLukas Czerner } 1348