xref: /linux/fs/ext4/ialloc.c (revision 901ed070df3c2c19e3083a734eafc82599fe991b)
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>
245b825c3aSIngo Molnar #include <linux/cred.h>
255b825c3aSIngo Molnar 
26ac27a0ecSDave Kleikamp #include <asm/byteorder.h>
279bffad1eSTheodore Ts'o 
283dcf5451SChristoph Hellwig #include "ext4.h"
293dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
30ac27a0ecSDave Kleikamp #include "xattr.h"
31ac27a0ecSDave Kleikamp #include "acl.h"
32ac27a0ecSDave Kleikamp 
339bffad1eSTheodore Ts'o #include <trace/events/ext4.h>
349bffad1eSTheodore Ts'o 
35ac27a0ecSDave Kleikamp /*
36ac27a0ecSDave Kleikamp  * ialloc.c contains the inodes allocation and deallocation routines
37ac27a0ecSDave Kleikamp  */
38ac27a0ecSDave Kleikamp 
39ac27a0ecSDave Kleikamp /*
40ac27a0ecSDave Kleikamp  * The free inodes are managed by bitmaps.  A file system contains several
41ac27a0ecSDave Kleikamp  * blocks groups.  Each group contains 1 bitmap block for blocks, 1 bitmap
42ac27a0ecSDave Kleikamp  * block for inodes, N blocks for the inode table and data blocks.
43ac27a0ecSDave Kleikamp  *
44ac27a0ecSDave Kleikamp  * The file system contains group descriptors which are located after the
45ac27a0ecSDave Kleikamp  * super block.  Each descriptor contains the number of the bitmap block and
46ac27a0ecSDave Kleikamp  * the free blocks count in the block.
47ac27a0ecSDave Kleikamp  */
48ac27a0ecSDave Kleikamp 
49717d50e4SAndreas Dilger /*
50717d50e4SAndreas Dilger  * To avoid calling the atomic setbit hundreds or thousands of times, we only
51717d50e4SAndreas Dilger  * need to use it within a single byte (to ensure we get endianness right).
52717d50e4SAndreas Dilger  * We can use memset for the rest of the bitmap as there are no other users.
53717d50e4SAndreas Dilger  */
5461d08673STheodore Ts'o void ext4_mark_bitmap_end(int start_bit, int end_bit, char *bitmap)
55717d50e4SAndreas Dilger {
56717d50e4SAndreas Dilger 	int i;
57717d50e4SAndreas Dilger 
58717d50e4SAndreas Dilger 	if (start_bit >= end_bit)
59717d50e4SAndreas Dilger 		return;
60717d50e4SAndreas Dilger 
61717d50e4SAndreas Dilger 	ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit);
62717d50e4SAndreas Dilger 	for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
63717d50e4SAndreas Dilger 		ext4_set_bit(i, bitmap);
64717d50e4SAndreas Dilger 	if (i < end_bit)
65717d50e4SAndreas Dilger 		memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
66717d50e4SAndreas Dilger }
67717d50e4SAndreas Dilger 
68717d50e4SAndreas Dilger /* Initializes an uninitialized inode bitmap */
699008a58eSDarrick J. Wong static int ext4_init_inode_bitmap(struct super_block *sb,
701f109d5aSTheodore Ts'o 				       struct buffer_head *bh,
71fd2d4291SAvantika Mathur 				       ext4_group_t block_group,
72717d50e4SAndreas Dilger 				       struct ext4_group_desc *gdp)
73717d50e4SAndreas Dilger {
74bdfb6ff4SDarrick J. Wong 	struct ext4_group_info *grp;
75e43bb4e6SNamjae Jeon 	struct ext4_sb_info *sbi = EXT4_SB(sb);
76717d50e4SAndreas Dilger 	J_ASSERT_BH(bh, buffer_locked(bh));
77717d50e4SAndreas Dilger 
78717d50e4SAndreas Dilger 	/* If checksum is bad mark all blocks and inodes use to prevent
79717d50e4SAndreas Dilger 	 * allocation, essentially implementing a per-group read-only flag. */
80feb0ab32SDarrick J. Wong 	if (!ext4_group_desc_csum_verify(sb, block_group, gdp)) {
81bdfb6ff4SDarrick J. Wong 		grp = ext4_get_group_info(sb, block_group);
82e43bb4e6SNamjae Jeon 		if (!EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
83e43bb4e6SNamjae Jeon 			percpu_counter_sub(&sbi->s_freeclusters_counter,
84e43bb4e6SNamjae Jeon 					   grp->bb_free);
85bdfb6ff4SDarrick J. Wong 		set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT, &grp->bb_state);
86e43bb4e6SNamjae Jeon 		if (!EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) {
87e43bb4e6SNamjae Jeon 			int count;
88e43bb4e6SNamjae Jeon 			count = ext4_free_inodes_count(sb, gdp);
89e43bb4e6SNamjae Jeon 			percpu_counter_sub(&sbi->s_freeinodes_counter,
90e43bb4e6SNamjae Jeon 					   count);
91e43bb4e6SNamjae Jeon 		}
92bdfb6ff4SDarrick J. Wong 		set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &grp->bb_state);
939008a58eSDarrick J. Wong 		return -EFSBADCRC;
94717d50e4SAndreas Dilger 	}
95717d50e4SAndreas Dilger 
96717d50e4SAndreas Dilger 	memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8);
9761d08673STheodore Ts'o 	ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), sb->s_blocksize * 8,
98717d50e4SAndreas Dilger 			bh->b_data);
9941a246d1SDarrick J. Wong 	ext4_inode_bitmap_csum_set(sb, block_group, gdp, bh,
10041a246d1SDarrick J. Wong 				   EXT4_INODES_PER_GROUP(sb) / 8);
101feb0ab32SDarrick J. Wong 	ext4_group_desc_csum_set(sb, block_group, gdp);
102717d50e4SAndreas Dilger 
1039008a58eSDarrick J. Wong 	return 0;
104717d50e4SAndreas Dilger }
105ac27a0ecSDave Kleikamp 
106813e5727STheodore Ts'o void ext4_end_bitmap_read(struct buffer_head *bh, int uptodate)
107813e5727STheodore Ts'o {
108813e5727STheodore Ts'o 	if (uptodate) {
109813e5727STheodore Ts'o 		set_buffer_uptodate(bh);
110813e5727STheodore Ts'o 		set_bitmap_uptodate(bh);
111813e5727STheodore Ts'o 	}
112813e5727STheodore Ts'o 	unlock_buffer(bh);
113813e5727STheodore Ts'o 	put_bh(bh);
114813e5727STheodore Ts'o }
115813e5727STheodore Ts'o 
1169008a58eSDarrick J. Wong static int ext4_validate_inode_bitmap(struct super_block *sb,
1179008a58eSDarrick J. Wong 				      struct ext4_group_desc *desc,
1189008a58eSDarrick J. Wong 				      ext4_group_t block_group,
1199008a58eSDarrick J. Wong 				      struct buffer_head *bh)
1209008a58eSDarrick J. Wong {
1219008a58eSDarrick J. Wong 	ext4_fsblk_t	blk;
1229008a58eSDarrick J. Wong 	struct ext4_group_info *grp = ext4_get_group_info(sb, block_group);
1239008a58eSDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1249008a58eSDarrick J. Wong 
1259008a58eSDarrick J. Wong 	if (buffer_verified(bh))
1269008a58eSDarrick J. Wong 		return 0;
1279008a58eSDarrick J. Wong 	if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp))
1289008a58eSDarrick J. Wong 		return -EFSCORRUPTED;
1299008a58eSDarrick J. Wong 
1309008a58eSDarrick J. Wong 	ext4_lock_group(sb, block_group);
1319008a58eSDarrick J. Wong 	blk = ext4_inode_bitmap(sb, desc);
1329008a58eSDarrick J. Wong 	if (!ext4_inode_bitmap_csum_verify(sb, block_group, desc, bh,
1339008a58eSDarrick J. Wong 					   EXT4_INODES_PER_GROUP(sb) / 8)) {
1349008a58eSDarrick J. Wong 		ext4_unlock_group(sb, block_group);
1359008a58eSDarrick J. Wong 		ext4_error(sb, "Corrupt inode bitmap - block_group = %u, "
1369008a58eSDarrick J. Wong 			   "inode_bitmap = %llu", block_group, blk);
1379008a58eSDarrick J. Wong 		grp = ext4_get_group_info(sb, block_group);
1389008a58eSDarrick J. Wong 		if (!EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) {
1399008a58eSDarrick J. Wong 			int count;
1409008a58eSDarrick J. Wong 			count = ext4_free_inodes_count(sb, desc);
1419008a58eSDarrick J. Wong 			percpu_counter_sub(&sbi->s_freeinodes_counter,
1429008a58eSDarrick J. Wong 					   count);
1439008a58eSDarrick J. Wong 		}
1449008a58eSDarrick J. Wong 		set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &grp->bb_state);
1459008a58eSDarrick J. Wong 		return -EFSBADCRC;
1469008a58eSDarrick J. Wong 	}
1479008a58eSDarrick J. Wong 	set_buffer_verified(bh);
1489008a58eSDarrick J. Wong 	ext4_unlock_group(sb, block_group);
1499008a58eSDarrick J. Wong 	return 0;
1509008a58eSDarrick J. Wong }
1519008a58eSDarrick J. Wong 
152ac27a0ecSDave Kleikamp /*
153ac27a0ecSDave Kleikamp  * Read the inode allocation bitmap for a given block_group, reading
154ac27a0ecSDave Kleikamp  * into the specified slot in the superblock's bitmap cache.
155ac27a0ecSDave Kleikamp  *
156ac27a0ecSDave Kleikamp  * Return buffer_head of bitmap on success or NULL.
157ac27a0ecSDave Kleikamp  */
158ac27a0ecSDave Kleikamp static struct buffer_head *
159e29d1cdeSEric Sandeen ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group)
160ac27a0ecSDave Kleikamp {
161617ba13bSMingming Cao 	struct ext4_group_desc *desc;
162ac27a0ecSDave Kleikamp 	struct buffer_head *bh = NULL;
163e29d1cdeSEric Sandeen 	ext4_fsblk_t bitmap_blk;
1649008a58eSDarrick J. Wong 	int err;
165ac27a0ecSDave Kleikamp 
166617ba13bSMingming Cao 	desc = ext4_get_group_desc(sb, block_group, NULL);
167ac27a0ecSDave Kleikamp 	if (!desc)
1689008a58eSDarrick J. Wong 		return ERR_PTR(-EFSCORRUPTED);
169bfff6873SLukas Czerner 
170e29d1cdeSEric Sandeen 	bitmap_blk = ext4_inode_bitmap(sb, desc);
171e29d1cdeSEric Sandeen 	bh = sb_getblk(sb, bitmap_blk);
172e29d1cdeSEric Sandeen 	if (unlikely(!bh)) {
17312062dddSEric Sandeen 		ext4_error(sb, "Cannot read inode bitmap - "
174a9df9a49STheodore Ts'o 			    "block_group = %u, inode_bitmap = %llu",
175e29d1cdeSEric Sandeen 			    block_group, bitmap_blk);
1769008a58eSDarrick J. Wong 		return ERR_PTR(-EIO);
177e29d1cdeSEric Sandeen 	}
1782ccb5fb9SAneesh Kumar K.V 	if (bitmap_uptodate(bh))
17941a246d1SDarrick J. Wong 		goto verify;
180e29d1cdeSEric Sandeen 
181c806e68fSFrederic Bohe 	lock_buffer(bh);
1822ccb5fb9SAneesh Kumar K.V 	if (bitmap_uptodate(bh)) {
1832ccb5fb9SAneesh Kumar K.V 		unlock_buffer(bh);
18441a246d1SDarrick J. Wong 		goto verify;
1852ccb5fb9SAneesh Kumar K.V 	}
186bfff6873SLukas Czerner 
187955ce5f5SAneesh Kumar K.V 	ext4_lock_group(sb, block_group);
188e29d1cdeSEric Sandeen 	if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
1899008a58eSDarrick J. Wong 		err = ext4_init_inode_bitmap(sb, bh, block_group, desc);
1902ccb5fb9SAneesh Kumar K.V 		set_bitmap_uptodate(bh);
191e29d1cdeSEric Sandeen 		set_buffer_uptodate(bh);
19241a246d1SDarrick J. Wong 		set_buffer_verified(bh);
193955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, block_group);
1943300bedaSAneesh Kumar K.V 		unlock_buffer(bh);
19505145bd7SJan Kara 		if (err) {
19605145bd7SJan Kara 			ext4_error(sb, "Failed to init inode bitmap for group "
19705145bd7SJan Kara 				   "%u: %d", block_group, err);
1989008a58eSDarrick J. Wong 			goto out;
19905145bd7SJan Kara 		}
200e29d1cdeSEric Sandeen 		return bh;
201e29d1cdeSEric Sandeen 	}
202955ce5f5SAneesh Kumar K.V 	ext4_unlock_group(sb, block_group);
203bfff6873SLukas Czerner 
2042ccb5fb9SAneesh Kumar K.V 	if (buffer_uptodate(bh)) {
2052ccb5fb9SAneesh Kumar K.V 		/*
2062ccb5fb9SAneesh Kumar K.V 		 * if not uninit if bh is uptodate,
2072ccb5fb9SAneesh Kumar K.V 		 * bitmap is also uptodate
2082ccb5fb9SAneesh Kumar K.V 		 */
2092ccb5fb9SAneesh Kumar K.V 		set_bitmap_uptodate(bh);
2102ccb5fb9SAneesh Kumar K.V 		unlock_buffer(bh);
21141a246d1SDarrick J. Wong 		goto verify;
2122ccb5fb9SAneesh Kumar K.V 	}
2132ccb5fb9SAneesh Kumar K.V 	/*
214813e5727STheodore Ts'o 	 * submit the buffer_head for reading
2152ccb5fb9SAneesh Kumar K.V 	 */
2160562e0baSJiaying Zhang 	trace_ext4_load_inode_bitmap(sb, block_group);
217813e5727STheodore Ts'o 	bh->b_end_io = ext4_end_bitmap_read;
218813e5727STheodore Ts'o 	get_bh(bh);
2192a222ca9SMike Christie 	submit_bh(REQ_OP_READ, REQ_META | REQ_PRIO, bh);
220813e5727STheodore Ts'o 	wait_on_buffer(bh);
221813e5727STheodore Ts'o 	if (!buffer_uptodate(bh)) {
222e29d1cdeSEric Sandeen 		put_bh(bh);
22312062dddSEric Sandeen 		ext4_error(sb, "Cannot read inode bitmap - "
224a9df9a49STheodore Ts'o 			   "block_group = %u, inode_bitmap = %llu",
225e29d1cdeSEric Sandeen 			   block_group, bitmap_blk);
2269008a58eSDarrick J. Wong 		return ERR_PTR(-EIO);
227e29d1cdeSEric Sandeen 	}
22841a246d1SDarrick J. Wong 
22941a246d1SDarrick J. Wong verify:
2309008a58eSDarrick J. Wong 	err = ext4_validate_inode_bitmap(sb, desc, block_group, bh);
2319008a58eSDarrick J. Wong 	if (err)
2329008a58eSDarrick J. Wong 		goto out;
233ac27a0ecSDave Kleikamp 	return bh;
2349008a58eSDarrick J. Wong out:
2359008a58eSDarrick J. Wong 	put_bh(bh);
2369008a58eSDarrick J. Wong 	return ERR_PTR(err);
237ac27a0ecSDave Kleikamp }
238ac27a0ecSDave Kleikamp 
239ac27a0ecSDave Kleikamp /*
240ac27a0ecSDave Kleikamp  * NOTE! When we get the inode, we're the only people
241ac27a0ecSDave Kleikamp  * that have access to it, and as such there are no
242ac27a0ecSDave Kleikamp  * race conditions we have to worry about. The inode
243ac27a0ecSDave Kleikamp  * is not on the hash-lists, and it cannot be reached
244ac27a0ecSDave Kleikamp  * through the filesystem because the directory entry
245ac27a0ecSDave Kleikamp  * has been deleted earlier.
246ac27a0ecSDave Kleikamp  *
247ac27a0ecSDave Kleikamp  * HOWEVER: we must make sure that we get no aliases,
248ac27a0ecSDave Kleikamp  * which means that we have to call "clear_inode()"
249ac27a0ecSDave Kleikamp  * _before_ we mark the inode not in use in the inode
250ac27a0ecSDave Kleikamp  * bitmaps. Otherwise a newly created file might use
251ac27a0ecSDave Kleikamp  * the same inode number (not actually the same pointer
252ac27a0ecSDave Kleikamp  * though), and then we'd have two inodes sharing the
253ac27a0ecSDave Kleikamp  * same inode number and space on the harddisk.
254ac27a0ecSDave Kleikamp  */
255617ba13bSMingming Cao void ext4_free_inode(handle_t *handle, struct inode *inode)
256ac27a0ecSDave Kleikamp {
257ac27a0ecSDave Kleikamp 	struct super_block *sb = inode->i_sb;
258ac27a0ecSDave Kleikamp 	int is_directory;
259ac27a0ecSDave Kleikamp 	unsigned long ino;
260ac27a0ecSDave Kleikamp 	struct buffer_head *bitmap_bh = NULL;
261ac27a0ecSDave Kleikamp 	struct buffer_head *bh2;
262fd2d4291SAvantika Mathur 	ext4_group_t block_group;
263ac27a0ecSDave Kleikamp 	unsigned long bit;
264617ba13bSMingming Cao 	struct ext4_group_desc *gdp;
265617ba13bSMingming Cao 	struct ext4_super_block *es;
266617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
2677ce9d5d1SEric Sandeen 	int fatal = 0, err, count, cleared;
26887a39389SDarrick J. Wong 	struct ext4_group_info *grp;
269ac27a0ecSDave Kleikamp 
27092b97816STheodore Ts'o 	if (!sb) {
27192b97816STheodore Ts'o 		printk(KERN_ERR "EXT4-fs: %s:%d: inode on "
27292b97816STheodore Ts'o 		       "nonexistent device\n", __func__, __LINE__);
27392b97816STheodore Ts'o 		return;
27492b97816STheodore Ts'o 	}
275ac27a0ecSDave Kleikamp 	if (atomic_read(&inode->i_count) > 1) {
27692b97816STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: count=%d",
27792b97816STheodore Ts'o 			 __func__, __LINE__, inode->i_ino,
278ac27a0ecSDave Kleikamp 			 atomic_read(&inode->i_count));
279ac27a0ecSDave Kleikamp 		return;
280ac27a0ecSDave Kleikamp 	}
281ac27a0ecSDave Kleikamp 	if (inode->i_nlink) {
28292b97816STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: nlink=%d\n",
28392b97816STheodore Ts'o 			 __func__, __LINE__, inode->i_ino, inode->i_nlink);
284ac27a0ecSDave Kleikamp 		return;
285ac27a0ecSDave Kleikamp 	}
286617ba13bSMingming Cao 	sbi = EXT4_SB(sb);
287ac27a0ecSDave Kleikamp 
288ac27a0ecSDave Kleikamp 	ino = inode->i_ino;
289617ba13bSMingming Cao 	ext4_debug("freeing inode %lu\n", ino);
2909bffad1eSTheodore Ts'o 	trace_ext4_free_inode(inode);
291ac27a0ecSDave Kleikamp 
292ac27a0ecSDave Kleikamp 	/*
293ac27a0ecSDave Kleikamp 	 * Note: we must free any quota before locking the superblock,
294ac27a0ecSDave Kleikamp 	 * as writing the quota to disk may need the lock as well.
295ac27a0ecSDave Kleikamp 	 */
296871a2931SChristoph Hellwig 	dquot_initialize(inode);
29763936ddaSChristoph Hellwig 	dquot_free_inode(inode);
2989f754758SChristoph Hellwig 	dquot_drop(inode);
299ac27a0ecSDave Kleikamp 
300ac27a0ecSDave Kleikamp 	is_directory = S_ISDIR(inode->i_mode);
301ac27a0ecSDave Kleikamp 
302ac27a0ecSDave Kleikamp 	/* Do this BEFORE marking the inode not in use or returning an error */
3030930fcc1SAl Viro 	ext4_clear_inode(inode);
304ac27a0ecSDave Kleikamp 
305617ba13bSMingming Cao 	es = EXT4_SB(sb)->s_es;
306617ba13bSMingming Cao 	if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
30712062dddSEric Sandeen 		ext4_error(sb, "reserved or nonexistent inode %lu", ino);
308ac27a0ecSDave Kleikamp 		goto error_return;
309ac27a0ecSDave Kleikamp 	}
310617ba13bSMingming Cao 	block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
311617ba13bSMingming Cao 	bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
312e29d1cdeSEric Sandeen 	bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
31387a39389SDarrick J. Wong 	/* Don't bother if the inode bitmap is corrupt. */
31487a39389SDarrick J. Wong 	grp = ext4_get_group_info(sb, block_group);
3159008a58eSDarrick J. Wong 	if (IS_ERR(bitmap_bh)) {
3169008a58eSDarrick J. Wong 		fatal = PTR_ERR(bitmap_bh);
3179008a58eSDarrick J. Wong 		bitmap_bh = NULL;
318ac27a0ecSDave Kleikamp 		goto error_return;
3199008a58eSDarrick J. Wong 	}
3209008a58eSDarrick J. Wong 	if (unlikely(EXT4_MB_GRP_IBITMAP_CORRUPT(grp))) {
3219008a58eSDarrick J. Wong 		fatal = -EFSCORRUPTED;
3229008a58eSDarrick J. Wong 		goto error_return;
3239008a58eSDarrick J. Wong 	}
324ac27a0ecSDave Kleikamp 
325ac27a0ecSDave Kleikamp 	BUFFER_TRACE(bitmap_bh, "get_write_access");
326617ba13bSMingming Cao 	fatal = ext4_journal_get_write_access(handle, bitmap_bh);
327ac27a0ecSDave Kleikamp 	if (fatal)
328ac27a0ecSDave Kleikamp 		goto error_return;
329ac27a0ecSDave Kleikamp 
330d17413c0SDmitry Monakhov 	fatal = -ESRCH;
331617ba13bSMingming Cao 	gdp = ext4_get_group_desc(sb, block_group, &bh2);
332d17413c0SDmitry Monakhov 	if (gdp) {
333ac27a0ecSDave Kleikamp 		BUFFER_TRACE(bh2, "get_write_access");
334617ba13bSMingming Cao 		fatal = ext4_journal_get_write_access(handle, bh2);
335d17413c0SDmitry Monakhov 	}
336955ce5f5SAneesh Kumar K.V 	ext4_lock_group(sb, block_group);
337597d508cSAkinobu Mita 	cleared = ext4_test_and_clear_bit(bit, bitmap_bh->b_data);
338d17413c0SDmitry Monakhov 	if (fatal || !cleared) {
339d17413c0SDmitry Monakhov 		ext4_unlock_group(sb, block_group);
340d17413c0SDmitry Monakhov 		goto out;
341d17413c0SDmitry Monakhov 	}
342d17413c0SDmitry Monakhov 
343560671a0SAneesh Kumar K.V 	count = ext4_free_inodes_count(sb, gdp) + 1;
344560671a0SAneesh Kumar K.V 	ext4_free_inodes_set(sb, gdp, count);
345560671a0SAneesh Kumar K.V 	if (is_directory) {
346560671a0SAneesh Kumar K.V 		count = ext4_used_dirs_count(sb, gdp) - 1;
347560671a0SAneesh Kumar K.V 		ext4_used_dirs_set(sb, gdp, count);
348d17413c0SDmitry Monakhov 		percpu_counter_dec(&sbi->s_dirs_counter);
349d17413c0SDmitry Monakhov 	}
35041a246d1SDarrick J. Wong 	ext4_inode_bitmap_csum_set(sb, block_group, gdp, bitmap_bh,
35141a246d1SDarrick J. Wong 				   EXT4_INODES_PER_GROUP(sb) / 8);
352feb0ab32SDarrick J. Wong 	ext4_group_desc_csum_set(sb, block_group, gdp);
353d17413c0SDmitry Monakhov 	ext4_unlock_group(sb, block_group);
3547d39db14STheodore Ts'o 
355d17413c0SDmitry Monakhov 	percpu_counter_inc(&sbi->s_freeinodes_counter);
356d17413c0SDmitry Monakhov 	if (sbi->s_log_groups_per_flex) {
357d17413c0SDmitry Monakhov 		ext4_group_t f = ext4_flex_group(sbi, block_group);
358d17413c0SDmitry Monakhov 
359d17413c0SDmitry Monakhov 		atomic_inc(&sbi->s_flex_groups[f].free_inodes);
360d17413c0SDmitry Monakhov 		if (is_directory)
361c4caae25SEric Sandeen 			atomic_dec(&sbi->s_flex_groups[f].used_dirs);
3627d39db14STheodore Ts'o 	}
3630390131bSFrank Mayhar 	BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata");
364d17413c0SDmitry Monakhov 	fatal = ext4_handle_dirty_metadata(handle, NULL, bh2);
365d17413c0SDmitry Monakhov out:
366d17413c0SDmitry Monakhov 	if (cleared) {
3670390131bSFrank Mayhar 		BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata");
3680390131bSFrank Mayhar 		err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
369ac27a0ecSDave Kleikamp 		if (!fatal)
370ac27a0ecSDave Kleikamp 			fatal = err;
37187a39389SDarrick J. Wong 	} else {
372d17413c0SDmitry Monakhov 		ext4_error(sb, "bit already cleared for inode %lu", ino);
373bf40c926SNamjae Jeon 		if (gdp && !EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) {
374e43bb4e6SNamjae Jeon 			int count;
375e43bb4e6SNamjae Jeon 			count = ext4_free_inodes_count(sb, gdp);
376e43bb4e6SNamjae Jeon 			percpu_counter_sub(&sbi->s_freeinodes_counter,
377e43bb4e6SNamjae Jeon 					   count);
378e43bb4e6SNamjae Jeon 		}
37987a39389SDarrick J. Wong 		set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &grp->bb_state);
38087a39389SDarrick J. Wong 	}
381d17413c0SDmitry Monakhov 
382ac27a0ecSDave Kleikamp error_return:
383ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
384617ba13bSMingming Cao 	ext4_std_error(sb, fatal);
385ac27a0ecSDave Kleikamp }
386ac27a0ecSDave Kleikamp 
387a4912123STheodore Ts'o struct orlov_stats {
38890ba983fSTheodore Ts'o 	__u64 free_clusters;
389a4912123STheodore Ts'o 	__u32 free_inodes;
390a4912123STheodore Ts'o 	__u32 used_dirs;
391a4912123STheodore Ts'o };
392a4912123STheodore Ts'o 
393a4912123STheodore Ts'o /*
394a4912123STheodore Ts'o  * Helper function for Orlov's allocator; returns critical information
395a4912123STheodore Ts'o  * for a particular block group or flex_bg.  If flex_size is 1, then g
396a4912123STheodore Ts'o  * is a block group number; otherwise it is flex_bg number.
397a4912123STheodore Ts'o  */
3981f109d5aSTheodore Ts'o static void get_orlov_stats(struct super_block *sb, ext4_group_t g,
399a4912123STheodore Ts'o 			    int flex_size, struct orlov_stats *stats)
400a4912123STheodore Ts'o {
401a4912123STheodore Ts'o 	struct ext4_group_desc *desc;
4027d39db14STheodore Ts'o 	struct flex_groups *flex_group = EXT4_SB(sb)->s_flex_groups;
403a4912123STheodore Ts'o 
4047d39db14STheodore Ts'o 	if (flex_size > 1) {
4057d39db14STheodore Ts'o 		stats->free_inodes = atomic_read(&flex_group[g].free_inodes);
40690ba983fSTheodore Ts'o 		stats->free_clusters = atomic64_read(&flex_group[g].free_clusters);
4077d39db14STheodore Ts'o 		stats->used_dirs = atomic_read(&flex_group[g].used_dirs);
4087d39db14STheodore Ts'o 		return;
4097d39db14STheodore Ts'o 	}
4107d39db14STheodore Ts'o 
4117d39db14STheodore Ts'o 	desc = ext4_get_group_desc(sb, g, NULL);
4127d39db14STheodore Ts'o 	if (desc) {
4137d39db14STheodore Ts'o 		stats->free_inodes = ext4_free_inodes_count(sb, desc);
414021b65bbSTheodore Ts'o 		stats->free_clusters = ext4_free_group_clusters(sb, desc);
4157d39db14STheodore Ts'o 		stats->used_dirs = ext4_used_dirs_count(sb, desc);
4167d39db14STheodore Ts'o 	} else {
417a4912123STheodore Ts'o 		stats->free_inodes = 0;
41824aaa8efSTheodore Ts'o 		stats->free_clusters = 0;
419a4912123STheodore Ts'o 		stats->used_dirs = 0;
420a4912123STheodore Ts'o 	}
421a4912123STheodore Ts'o }
422a4912123STheodore Ts'o 
423ac27a0ecSDave Kleikamp /*
424ac27a0ecSDave Kleikamp  * Orlov's allocator for directories.
425ac27a0ecSDave Kleikamp  *
426ac27a0ecSDave Kleikamp  * We always try to spread first-level directories.
427ac27a0ecSDave Kleikamp  *
428ac27a0ecSDave Kleikamp  * If there are blockgroups with both free inodes and free blocks counts
429ac27a0ecSDave Kleikamp  * not worse than average we return one with smallest directory count.
430ac27a0ecSDave Kleikamp  * Otherwise we simply return a random group.
431ac27a0ecSDave Kleikamp  *
432ac27a0ecSDave Kleikamp  * For the rest rules look so:
433ac27a0ecSDave Kleikamp  *
434ac27a0ecSDave Kleikamp  * It's OK to put directory into a group unless
435ac27a0ecSDave Kleikamp  * it has too many directories already (max_dirs) or
436ac27a0ecSDave Kleikamp  * it has too few free inodes left (min_inodes) or
437ac27a0ecSDave Kleikamp  * it has too few free blocks left (min_blocks) or
4381cc8dcf5SBenoit Boissinot  * Parent's group is preferred, if it doesn't satisfy these
439ac27a0ecSDave Kleikamp  * conditions we search cyclically through the rest. If none
440ac27a0ecSDave Kleikamp  * of the groups look good we just look for a group with more
441ac27a0ecSDave Kleikamp  * free inodes than average (starting at parent's group).
442ac27a0ecSDave Kleikamp  */
443ac27a0ecSDave Kleikamp 
4442aa9fc4cSAvantika Mathur static int find_group_orlov(struct super_block *sb, struct inode *parent,
445dcca3fecSAl Viro 			    ext4_group_t *group, umode_t mode,
446f157a4aaSTheodore Ts'o 			    const struct qstr *qstr)
447ac27a0ecSDave Kleikamp {
448fd2d4291SAvantika Mathur 	ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
449617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4508df9675fSTheodore Ts'o 	ext4_group_t real_ngroups = ext4_get_groups_count(sb);
451617ba13bSMingming Cao 	int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
45214c83c9fSTheodore Ts'o 	unsigned int freei, avefreei, grp_free;
45324aaa8efSTheodore Ts'o 	ext4_fsblk_t freeb, avefreec;
454ac27a0ecSDave Kleikamp 	unsigned int ndirs;
455a4912123STheodore Ts'o 	int max_dirs, min_inodes;
45624aaa8efSTheodore Ts'o 	ext4_grpblk_t min_clusters;
4578df9675fSTheodore Ts'o 	ext4_group_t i, grp, g, ngroups;
458617ba13bSMingming Cao 	struct ext4_group_desc *desc;
459a4912123STheodore Ts'o 	struct orlov_stats stats;
460a4912123STheodore Ts'o 	int flex_size = ext4_flex_bg_size(sbi);
461f157a4aaSTheodore Ts'o 	struct dx_hash_info hinfo;
462a4912123STheodore Ts'o 
4638df9675fSTheodore Ts'o 	ngroups = real_ngroups;
464a4912123STheodore Ts'o 	if (flex_size > 1) {
4658df9675fSTheodore Ts'o 		ngroups = (real_ngroups + flex_size - 1) >>
466a4912123STheodore Ts'o 			sbi->s_log_groups_per_flex;
467a4912123STheodore Ts'o 		parent_group >>= sbi->s_log_groups_per_flex;
468a4912123STheodore Ts'o 	}
469ac27a0ecSDave Kleikamp 
470ac27a0ecSDave Kleikamp 	freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
471ac27a0ecSDave Kleikamp 	avefreei = freei / ngroups;
47257042651STheodore Ts'o 	freeb = EXT4_C2B(sbi,
47357042651STheodore Ts'o 		percpu_counter_read_positive(&sbi->s_freeclusters_counter));
47424aaa8efSTheodore Ts'o 	avefreec = freeb;
47524aaa8efSTheodore Ts'o 	do_div(avefreec, ngroups);
476ac27a0ecSDave Kleikamp 	ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
477ac27a0ecSDave Kleikamp 
478a4912123STheodore Ts'o 	if (S_ISDIR(mode) &&
4792b0143b5SDavid Howells 	    ((parent == d_inode(sb->s_root)) ||
48012e9b892SDmitry Monakhov 	     (ext4_test_inode_flag(parent, EXT4_INODE_TOPDIR)))) {
481ac27a0ecSDave Kleikamp 		int best_ndir = inodes_per_group;
4822aa9fc4cSAvantika Mathur 		int ret = -1;
483ac27a0ecSDave Kleikamp 
484f157a4aaSTheodore Ts'o 		if (qstr) {
485f157a4aaSTheodore Ts'o 			hinfo.hash_version = DX_HASH_HALF_MD4;
486f157a4aaSTheodore Ts'o 			hinfo.seed = sbi->s_hash_seed;
487f157a4aaSTheodore Ts'o 			ext4fs_dirhash(qstr->name, qstr->len, &hinfo);
488f157a4aaSTheodore Ts'o 			grp = hinfo.hash;
489f157a4aaSTheodore Ts'o 		} else
490dd1f723bSTheodore Ts'o 			grp = prandom_u32();
4912aa9fc4cSAvantika Mathur 		parent_group = (unsigned)grp % ngroups;
492ac27a0ecSDave Kleikamp 		for (i = 0; i < ngroups; i++) {
493a4912123STheodore Ts'o 			g = (parent_group + i) % ngroups;
494a4912123STheodore Ts'o 			get_orlov_stats(sb, g, flex_size, &stats);
495a4912123STheodore Ts'o 			if (!stats.free_inodes)
496ac27a0ecSDave Kleikamp 				continue;
497a4912123STheodore Ts'o 			if (stats.used_dirs >= best_ndir)
498ac27a0ecSDave Kleikamp 				continue;
499a4912123STheodore Ts'o 			if (stats.free_inodes < avefreei)
500ac27a0ecSDave Kleikamp 				continue;
50124aaa8efSTheodore Ts'o 			if (stats.free_clusters < avefreec)
502ac27a0ecSDave Kleikamp 				continue;
503a4912123STheodore Ts'o 			grp = g;
5042aa9fc4cSAvantika Mathur 			ret = 0;
505a4912123STheodore Ts'o 			best_ndir = stats.used_dirs;
506ac27a0ecSDave Kleikamp 		}
507a4912123STheodore Ts'o 		if (ret)
508a4912123STheodore Ts'o 			goto fallback;
509a4912123STheodore Ts'o 	found_flex_bg:
510a4912123STheodore Ts'o 		if (flex_size == 1) {
511a4912123STheodore Ts'o 			*group = grp;
512a4912123STheodore Ts'o 			return 0;
513a4912123STheodore Ts'o 		}
514a4912123STheodore Ts'o 
515a4912123STheodore Ts'o 		/*
516a4912123STheodore Ts'o 		 * We pack inodes at the beginning of the flexgroup's
517a4912123STheodore Ts'o 		 * inode tables.  Block allocation decisions will do
518a4912123STheodore Ts'o 		 * something similar, although regular files will
519a4912123STheodore Ts'o 		 * start at 2nd block group of the flexgroup.  See
520a4912123STheodore Ts'o 		 * ext4_ext_find_goal() and ext4_find_near().
521a4912123STheodore Ts'o 		 */
522a4912123STheodore Ts'o 		grp *= flex_size;
523a4912123STheodore Ts'o 		for (i = 0; i < flex_size; i++) {
5248df9675fSTheodore Ts'o 			if (grp+i >= real_ngroups)
525a4912123STheodore Ts'o 				break;
526a4912123STheodore Ts'o 			desc = ext4_get_group_desc(sb, grp+i, NULL);
527a4912123STheodore Ts'o 			if (desc && ext4_free_inodes_count(sb, desc)) {
528a4912123STheodore Ts'o 				*group = grp+i;
529a4912123STheodore Ts'o 				return 0;
530a4912123STheodore Ts'o 			}
531a4912123STheodore Ts'o 		}
532ac27a0ecSDave Kleikamp 		goto fallback;
533ac27a0ecSDave Kleikamp 	}
534ac27a0ecSDave Kleikamp 
535ac27a0ecSDave Kleikamp 	max_dirs = ndirs / ngroups + inodes_per_group / 16;
536a4912123STheodore Ts'o 	min_inodes = avefreei - inodes_per_group*flex_size / 4;
537a4912123STheodore Ts'o 	if (min_inodes < 1)
538a4912123STheodore Ts'o 		min_inodes = 1;
53924aaa8efSTheodore Ts'o 	min_clusters = avefreec - EXT4_CLUSTERS_PER_GROUP(sb)*flex_size / 4;
540ac27a0ecSDave Kleikamp 
541a4912123STheodore Ts'o 	/*
542a4912123STheodore Ts'o 	 * Start looking in the flex group where we last allocated an
543a4912123STheodore Ts'o 	 * inode for this parent directory
544a4912123STheodore Ts'o 	 */
545a4912123STheodore Ts'o 	if (EXT4_I(parent)->i_last_alloc_group != ~0) {
546a4912123STheodore Ts'o 		parent_group = EXT4_I(parent)->i_last_alloc_group;
547a4912123STheodore Ts'o 		if (flex_size > 1)
548a4912123STheodore Ts'o 			parent_group >>= sbi->s_log_groups_per_flex;
549a4912123STheodore Ts'o 	}
550ac27a0ecSDave Kleikamp 
551ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
552a4912123STheodore Ts'o 		grp = (parent_group + i) % ngroups;
553a4912123STheodore Ts'o 		get_orlov_stats(sb, grp, flex_size, &stats);
554a4912123STheodore Ts'o 		if (stats.used_dirs >= max_dirs)
555ac27a0ecSDave Kleikamp 			continue;
556a4912123STheodore Ts'o 		if (stats.free_inodes < min_inodes)
557ac27a0ecSDave Kleikamp 			continue;
55824aaa8efSTheodore Ts'o 		if (stats.free_clusters < min_clusters)
559ac27a0ecSDave Kleikamp 			continue;
560a4912123STheodore Ts'o 		goto found_flex_bg;
561ac27a0ecSDave Kleikamp 	}
562ac27a0ecSDave Kleikamp 
563ac27a0ecSDave Kleikamp fallback:
5648df9675fSTheodore Ts'o 	ngroups = real_ngroups;
565a4912123STheodore Ts'o 	avefreei = freei / ngroups;
566b5451f7bSTheodore Ts'o fallback_retry:
567a4912123STheodore Ts'o 	parent_group = EXT4_I(parent)->i_block_group;
568ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
569a4912123STheodore Ts'o 		grp = (parent_group + i) % ngroups;
570a4912123STheodore Ts'o 		desc = ext4_get_group_desc(sb, grp, NULL);
571bb3d132aSDan Carpenter 		if (desc) {
57214c83c9fSTheodore Ts'o 			grp_free = ext4_free_inodes_count(sb, desc);
573bb3d132aSDan Carpenter 			if (grp_free && grp_free >= avefreei) {
574a4912123STheodore Ts'o 				*group = grp;
5752aa9fc4cSAvantika Mathur 				return 0;
576ac27a0ecSDave Kleikamp 			}
577a4912123STheodore Ts'o 		}
578bb3d132aSDan Carpenter 	}
579ac27a0ecSDave Kleikamp 
580ac27a0ecSDave Kleikamp 	if (avefreei) {
581ac27a0ecSDave Kleikamp 		/*
582ac27a0ecSDave Kleikamp 		 * The free-inodes counter is approximate, and for really small
583ac27a0ecSDave Kleikamp 		 * filesystems the above test can fail to find any blockgroups
584ac27a0ecSDave Kleikamp 		 */
585ac27a0ecSDave Kleikamp 		avefreei = 0;
586b5451f7bSTheodore Ts'o 		goto fallback_retry;
587ac27a0ecSDave Kleikamp 	}
588ac27a0ecSDave Kleikamp 
589ac27a0ecSDave Kleikamp 	return -1;
590ac27a0ecSDave Kleikamp }
591ac27a0ecSDave Kleikamp 
5922aa9fc4cSAvantika Mathur static int find_group_other(struct super_block *sb, struct inode *parent,
593dcca3fecSAl Viro 			    ext4_group_t *group, umode_t mode)
594ac27a0ecSDave Kleikamp {
595fd2d4291SAvantika Mathur 	ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
5968df9675fSTheodore Ts'o 	ext4_group_t i, last, ngroups = ext4_get_groups_count(sb);
597617ba13bSMingming Cao 	struct ext4_group_desc *desc;
598a4912123STheodore Ts'o 	int flex_size = ext4_flex_bg_size(EXT4_SB(sb));
599a4912123STheodore Ts'o 
600a4912123STheodore Ts'o 	/*
601a4912123STheodore Ts'o 	 * Try to place the inode is the same flex group as its
602a4912123STheodore Ts'o 	 * parent.  If we can't find space, use the Orlov algorithm to
603a4912123STheodore Ts'o 	 * find another flex group, and store that information in the
604a4912123STheodore Ts'o 	 * parent directory's inode information so that use that flex
605a4912123STheodore Ts'o 	 * group for future allocations.
606a4912123STheodore Ts'o 	 */
607a4912123STheodore Ts'o 	if (flex_size > 1) {
608a4912123STheodore Ts'o 		int retry = 0;
609a4912123STheodore Ts'o 
610a4912123STheodore Ts'o 	try_again:
611a4912123STheodore Ts'o 		parent_group &= ~(flex_size-1);
612a4912123STheodore Ts'o 		last = parent_group + flex_size;
613a4912123STheodore Ts'o 		if (last > ngroups)
614a4912123STheodore Ts'o 			last = ngroups;
615a4912123STheodore Ts'o 		for  (i = parent_group; i < last; i++) {
616a4912123STheodore Ts'o 			desc = ext4_get_group_desc(sb, i, NULL);
617a4912123STheodore Ts'o 			if (desc && ext4_free_inodes_count(sb, desc)) {
618a4912123STheodore Ts'o 				*group = i;
619a4912123STheodore Ts'o 				return 0;
620a4912123STheodore Ts'o 			}
621a4912123STheodore Ts'o 		}
622a4912123STheodore Ts'o 		if (!retry && EXT4_I(parent)->i_last_alloc_group != ~0) {
623a4912123STheodore Ts'o 			retry = 1;
624a4912123STheodore Ts'o 			parent_group = EXT4_I(parent)->i_last_alloc_group;
625a4912123STheodore Ts'o 			goto try_again;
626a4912123STheodore Ts'o 		}
627a4912123STheodore Ts'o 		/*
628a4912123STheodore Ts'o 		 * If this didn't work, use the Orlov search algorithm
629a4912123STheodore Ts'o 		 * to find a new flex group; we pass in the mode to
630a4912123STheodore Ts'o 		 * avoid the topdir algorithms.
631a4912123STheodore Ts'o 		 */
632a4912123STheodore Ts'o 		*group = parent_group + flex_size;
633a4912123STheodore Ts'o 		if (*group > ngroups)
634a4912123STheodore Ts'o 			*group = 0;
6357dc57615SPeter Huewe 		return find_group_orlov(sb, parent, group, mode, NULL);
636a4912123STheodore Ts'o 	}
637ac27a0ecSDave Kleikamp 
638ac27a0ecSDave Kleikamp 	/*
639ac27a0ecSDave Kleikamp 	 * Try to place the inode in its parent directory
640ac27a0ecSDave Kleikamp 	 */
6412aa9fc4cSAvantika Mathur 	*group = parent_group;
6422aa9fc4cSAvantika Mathur 	desc = ext4_get_group_desc(sb, *group, NULL);
643560671a0SAneesh Kumar K.V 	if (desc && ext4_free_inodes_count(sb, desc) &&
644021b65bbSTheodore Ts'o 	    ext4_free_group_clusters(sb, desc))
6452aa9fc4cSAvantika Mathur 		return 0;
646ac27a0ecSDave Kleikamp 
647ac27a0ecSDave Kleikamp 	/*
648ac27a0ecSDave Kleikamp 	 * We're going to place this inode in a different blockgroup from its
649ac27a0ecSDave Kleikamp 	 * parent.  We want to cause files in a common directory to all land in
650ac27a0ecSDave Kleikamp 	 * the same blockgroup.  But we want files which are in a different
651ac27a0ecSDave Kleikamp 	 * directory which shares a blockgroup with our parent to land in a
652ac27a0ecSDave Kleikamp 	 * different blockgroup.
653ac27a0ecSDave Kleikamp 	 *
654ac27a0ecSDave Kleikamp 	 * So add our directory's i_ino into the starting point for the hash.
655ac27a0ecSDave Kleikamp 	 */
6562aa9fc4cSAvantika Mathur 	*group = (*group + parent->i_ino) % ngroups;
657ac27a0ecSDave Kleikamp 
658ac27a0ecSDave Kleikamp 	/*
659ac27a0ecSDave Kleikamp 	 * Use a quadratic hash to find a group with a free inode and some free
660ac27a0ecSDave Kleikamp 	 * blocks.
661ac27a0ecSDave Kleikamp 	 */
662ac27a0ecSDave Kleikamp 	for (i = 1; i < ngroups; i <<= 1) {
6632aa9fc4cSAvantika Mathur 		*group += i;
6642aa9fc4cSAvantika Mathur 		if (*group >= ngroups)
6652aa9fc4cSAvantika Mathur 			*group -= ngroups;
6662aa9fc4cSAvantika Mathur 		desc = ext4_get_group_desc(sb, *group, NULL);
667560671a0SAneesh Kumar K.V 		if (desc && ext4_free_inodes_count(sb, desc) &&
668021b65bbSTheodore Ts'o 		    ext4_free_group_clusters(sb, desc))
6692aa9fc4cSAvantika Mathur 			return 0;
670ac27a0ecSDave Kleikamp 	}
671ac27a0ecSDave Kleikamp 
672ac27a0ecSDave Kleikamp 	/*
673ac27a0ecSDave Kleikamp 	 * That failed: try linear search for a free inode, even if that group
674ac27a0ecSDave Kleikamp 	 * has no free blocks.
675ac27a0ecSDave Kleikamp 	 */
6762aa9fc4cSAvantika Mathur 	*group = parent_group;
677ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
6782aa9fc4cSAvantika Mathur 		if (++*group >= ngroups)
6792aa9fc4cSAvantika Mathur 			*group = 0;
6802aa9fc4cSAvantika Mathur 		desc = ext4_get_group_desc(sb, *group, NULL);
681560671a0SAneesh Kumar K.V 		if (desc && ext4_free_inodes_count(sb, desc))
6822aa9fc4cSAvantika Mathur 			return 0;
683ac27a0ecSDave Kleikamp 	}
684ac27a0ecSDave Kleikamp 
685ac27a0ecSDave Kleikamp 	return -1;
686ac27a0ecSDave Kleikamp }
687ac27a0ecSDave Kleikamp 
688ac27a0ecSDave Kleikamp /*
68919883bd9STheodore Ts'o  * In no journal mode, if an inode has recently been deleted, we want
69019883bd9STheodore Ts'o  * to avoid reusing it until we're reasonably sure the inode table
69119883bd9STheodore Ts'o  * block has been written back to disk.  (Yes, these values are
69219883bd9STheodore Ts'o  * somewhat arbitrary...)
69319883bd9STheodore Ts'o  */
69419883bd9STheodore Ts'o #define RECENTCY_MIN	5
69519883bd9STheodore Ts'o #define RECENTCY_DIRTY	30
69619883bd9STheodore Ts'o 
69719883bd9STheodore Ts'o static int recently_deleted(struct super_block *sb, ext4_group_t group, int ino)
69819883bd9STheodore Ts'o {
69919883bd9STheodore Ts'o 	struct ext4_group_desc	*gdp;
70019883bd9STheodore Ts'o 	struct ext4_inode	*raw_inode;
70119883bd9STheodore Ts'o 	struct buffer_head	*bh;
70219883bd9STheodore Ts'o 	unsigned long		dtime, now;
70319883bd9STheodore Ts'o 	int	inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
70419883bd9STheodore Ts'o 	int	offset, ret = 0, recentcy = RECENTCY_MIN;
70519883bd9STheodore Ts'o 
70619883bd9STheodore Ts'o 	gdp = ext4_get_group_desc(sb, group, NULL);
70719883bd9STheodore Ts'o 	if (unlikely(!gdp))
70819883bd9STheodore Ts'o 		return 0;
70919883bd9STheodore Ts'o 
7104f9d956dSJan Kara 	bh = sb_find_get_block(sb, ext4_inode_table(sb, gdp) +
71119883bd9STheodore Ts'o 		       (ino / inodes_per_block));
7124f9d956dSJan Kara 	if (!bh || !buffer_uptodate(bh))
71319883bd9STheodore Ts'o 		/*
71419883bd9STheodore Ts'o 		 * If the block is not in the buffer cache, then it
71519883bd9STheodore Ts'o 		 * must have been written out.
71619883bd9STheodore Ts'o 		 */
71719883bd9STheodore Ts'o 		goto out;
71819883bd9STheodore Ts'o 
71919883bd9STheodore Ts'o 	offset = (ino % inodes_per_block) * EXT4_INODE_SIZE(sb);
72019883bd9STheodore Ts'o 	raw_inode = (struct ext4_inode *) (bh->b_data + offset);
72119883bd9STheodore Ts'o 	dtime = le32_to_cpu(raw_inode->i_dtime);
72219883bd9STheodore Ts'o 	now = get_seconds();
72319883bd9STheodore Ts'o 	if (buffer_dirty(bh))
72419883bd9STheodore Ts'o 		recentcy += RECENTCY_DIRTY;
72519883bd9STheodore Ts'o 
72619883bd9STheodore Ts'o 	if (dtime && (dtime < now) && (now < dtime + recentcy))
72719883bd9STheodore Ts'o 		ret = 1;
72819883bd9STheodore Ts'o out:
72919883bd9STheodore Ts'o 	brelse(bh);
73019883bd9STheodore Ts'o 	return ret;
73119883bd9STheodore Ts'o }
73219883bd9STheodore Ts'o 
733*901ed070SWang Shilong static int find_inode_bit(struct super_block *sb, ext4_group_t group,
734*901ed070SWang Shilong 			  struct buffer_head *bitmap, unsigned long *ino)
735*901ed070SWang Shilong {
736*901ed070SWang Shilong next:
737*901ed070SWang Shilong 	*ino = ext4_find_next_zero_bit((unsigned long *)
738*901ed070SWang Shilong 				       bitmap->b_data,
739*901ed070SWang Shilong 				       EXT4_INODES_PER_GROUP(sb), *ino);
740*901ed070SWang Shilong 	if (*ino >= EXT4_INODES_PER_GROUP(sb))
741*901ed070SWang Shilong 		return 0;
742*901ed070SWang Shilong 
743*901ed070SWang Shilong 	if ((EXT4_SB(sb)->s_journal == NULL) &&
744*901ed070SWang Shilong 	    recently_deleted(sb, group, *ino)) {
745*901ed070SWang Shilong 		*ino = *ino + 1;
746*901ed070SWang Shilong 		if (*ino < EXT4_INODES_PER_GROUP(sb))
747*901ed070SWang Shilong 			goto next;
748*901ed070SWang Shilong 		return 0;
749*901ed070SWang Shilong 	}
750*901ed070SWang Shilong 
751*901ed070SWang Shilong 	return 1;
752*901ed070SWang Shilong }
753*901ed070SWang Shilong 
75419883bd9STheodore Ts'o /*
755ac27a0ecSDave Kleikamp  * There are two policies for allocating an inode.  If the new inode is
756ac27a0ecSDave Kleikamp  * a directory, then a forward search is made for a block group with both
757ac27a0ecSDave Kleikamp  * free space and a low directory-to-inode ratio; if that fails, then of
758ac27a0ecSDave Kleikamp  * the groups with above-average free space, that group with the fewest
759ac27a0ecSDave Kleikamp  * directories already is chosen.
760ac27a0ecSDave Kleikamp  *
761ac27a0ecSDave Kleikamp  * For other inodes, search forward from the parent directory's block
762ac27a0ecSDave Kleikamp  * group to find a free inode.
763ac27a0ecSDave Kleikamp  */
7641139575aSTheodore Ts'o struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
7651139575aSTheodore Ts'o 			       umode_t mode, const struct qstr *qstr,
7661b917ed8STahsin Erdogan 			       __u32 goal, uid_t *owner, __u32 i_flags,
7671b917ed8STahsin Erdogan 			       int handle_type, unsigned int line_no,
7681b917ed8STahsin Erdogan 			       int nblocks)
769ac27a0ecSDave Kleikamp {
770ac27a0ecSDave Kleikamp 	struct super_block *sb;
7713300bedaSAneesh Kumar K.V 	struct buffer_head *inode_bitmap_bh = NULL;
7723300bedaSAneesh Kumar K.V 	struct buffer_head *group_desc_bh;
7738df9675fSTheodore Ts'o 	ext4_group_t ngroups, group = 0;
774ac27a0ecSDave Kleikamp 	unsigned long ino = 0;
775ac27a0ecSDave Kleikamp 	struct inode *inode;
776617ba13bSMingming Cao 	struct ext4_group_desc *gdp = NULL;
777617ba13bSMingming Cao 	struct ext4_inode_info *ei;
778617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
779a7cdadeeSJan Kara 	int ret2, err;
780ac27a0ecSDave Kleikamp 	struct inode *ret;
7812aa9fc4cSAvantika Mathur 	ext4_group_t i;
782772cb7c8SJose R. Santos 	ext4_group_t flex_group;
78387a39389SDarrick J. Wong 	struct ext4_group_info *grp;
784e709e9dfSTheodore Ts'o 	int encrypt = 0;
785ac27a0ecSDave Kleikamp 
786ac27a0ecSDave Kleikamp 	/* Cannot create files in a deleted directory */
787ac27a0ecSDave Kleikamp 	if (!dir || !dir->i_nlink)
788ac27a0ecSDave Kleikamp 		return ERR_PTR(-EPERM);
789ac27a0ecSDave Kleikamp 
790af65207cSTahsin Erdogan 	sb = dir->i_sb;
791af65207cSTahsin Erdogan 	sbi = EXT4_SB(sb);
792af65207cSTahsin Erdogan 
793af65207cSTahsin Erdogan 	if (unlikely(ext4_forced_shutdown(sbi)))
7940db1ff22STheodore Ts'o 		return ERR_PTR(-EIO);
7950db1ff22STheodore Ts'o 
796af65207cSTahsin Erdogan 	if ((ext4_encrypted_inode(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) &&
797ad47f953STahsin Erdogan 	    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) &&
798ad47f953STahsin Erdogan 	    !(i_flags & EXT4_EA_INODE_FL)) {
799a7550b30SJaegeuk Kim 		err = fscrypt_get_encryption_info(dir);
800e709e9dfSTheodore Ts'o 		if (err)
801e709e9dfSTheodore Ts'o 			return ERR_PTR(err);
802a7550b30SJaegeuk Kim 		if (!fscrypt_has_encryption_key(dir))
80354475f53SEric Biggers 			return ERR_PTR(-ENOKEY);
804e709e9dfSTheodore Ts'o 		encrypt = 1;
805e709e9dfSTheodore Ts'o 	}
806e709e9dfSTheodore Ts'o 
807af65207cSTahsin Erdogan 	if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) {
808af65207cSTahsin Erdogan #ifdef CONFIG_EXT4_FS_POSIX_ACL
809af65207cSTahsin Erdogan 		struct posix_acl *p = get_acl(dir, ACL_TYPE_DEFAULT);
810af65207cSTahsin Erdogan 
811af65207cSTahsin Erdogan 		if (p) {
812af65207cSTahsin Erdogan 			int acl_size = p->a_count * sizeof(ext4_acl_entry);
813af65207cSTahsin Erdogan 
814af65207cSTahsin Erdogan 			nblocks += (S_ISDIR(mode) ? 2 : 1) *
815af65207cSTahsin Erdogan 				__ext4_xattr_set_credits(sb, NULL /* inode */,
816af65207cSTahsin Erdogan 					NULL /* block_bh */, acl_size,
817af65207cSTahsin Erdogan 					true /* is_create */);
818af65207cSTahsin Erdogan 			posix_acl_release(p);
819af65207cSTahsin Erdogan 		}
820af65207cSTahsin Erdogan #endif
821af65207cSTahsin Erdogan 
822af65207cSTahsin Erdogan #ifdef CONFIG_SECURITY
823af65207cSTahsin Erdogan 		{
824af65207cSTahsin Erdogan 			int num_security_xattrs = 1;
825af65207cSTahsin Erdogan 
826af65207cSTahsin Erdogan #ifdef CONFIG_INTEGRITY
827af65207cSTahsin Erdogan 			num_security_xattrs++;
828af65207cSTahsin Erdogan #endif
829af65207cSTahsin Erdogan 			/*
830af65207cSTahsin Erdogan 			 * We assume that security xattrs are never
831af65207cSTahsin Erdogan 			 * more than 1k.  In practice they are under
832af65207cSTahsin Erdogan 			 * 128 bytes.
833af65207cSTahsin Erdogan 			 */
834af65207cSTahsin Erdogan 			nblocks += num_security_xattrs *
835af65207cSTahsin Erdogan 				__ext4_xattr_set_credits(sb, NULL /* inode */,
836af65207cSTahsin Erdogan 					NULL /* block_bh */, 1024,
837af65207cSTahsin Erdogan 					true /* is_create */);
838af65207cSTahsin Erdogan 		}
839af65207cSTahsin Erdogan #endif
840af65207cSTahsin Erdogan 		if (encrypt)
841af65207cSTahsin Erdogan 			nblocks += __ext4_xattr_set_credits(sb,
842af65207cSTahsin Erdogan 					NULL /* inode */, NULL /* block_bh */,
843af65207cSTahsin Erdogan 					FSCRYPT_SET_CONTEXT_MAX_SIZE,
844af65207cSTahsin Erdogan 					true /* is_create */);
845af65207cSTahsin Erdogan 	}
846af65207cSTahsin Erdogan 
8478df9675fSTheodore Ts'o 	ngroups = ext4_get_groups_count(sb);
8489bffad1eSTheodore Ts'o 	trace_ext4_request_inode(dir, mode);
849ac27a0ecSDave Kleikamp 	inode = new_inode(sb);
850ac27a0ecSDave Kleikamp 	if (!inode)
851ac27a0ecSDave Kleikamp 		return ERR_PTR(-ENOMEM);
852617ba13bSMingming Cao 	ei = EXT4_I(inode);
853772cb7c8SJose R. Santos 
854eb9cc7e1SJan Kara 	/*
855b8a07463SAdam Buchbinder 	 * Initialize owners and quota early so that we don't have to account
856eb9cc7e1SJan Kara 	 * for quota initialization worst case in standard inode creating
857eb9cc7e1SJan Kara 	 * transaction
858eb9cc7e1SJan Kara 	 */
859eb9cc7e1SJan Kara 	if (owner) {
860eb9cc7e1SJan Kara 		inode->i_mode = mode;
861eb9cc7e1SJan Kara 		i_uid_write(inode, owner[0]);
862eb9cc7e1SJan Kara 		i_gid_write(inode, owner[1]);
863eb9cc7e1SJan Kara 	} else if (test_opt(sb, GRPID)) {
864eb9cc7e1SJan Kara 		inode->i_mode = mode;
865eb9cc7e1SJan Kara 		inode->i_uid = current_fsuid();
866eb9cc7e1SJan Kara 		inode->i_gid = dir->i_gid;
867eb9cc7e1SJan Kara 	} else
868eb9cc7e1SJan Kara 		inode_init_owner(inode, dir, mode);
869040cb378SLi Xi 
8700b7b7779SKaho Ng 	if (ext4_has_feature_project(sb) &&
871040cb378SLi Xi 	    ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT))
872040cb378SLi Xi 		ei->i_projid = EXT4_I(dir)->i_projid;
873040cb378SLi Xi 	else
874040cb378SLi Xi 		ei->i_projid = make_kprojid(&init_user_ns, EXT4_DEF_PROJID);
875040cb378SLi Xi 
876a7cdadeeSJan Kara 	err = dquot_initialize(inode);
877a7cdadeeSJan Kara 	if (err)
878a7cdadeeSJan Kara 		goto out;
879eb9cc7e1SJan Kara 
88011013911SAndreas Dilger 	if (!goal)
88111013911SAndreas Dilger 		goal = sbi->s_inode_goal;
88211013911SAndreas Dilger 
883e6462869SJohann Lombardi 	if (goal && goal <= le32_to_cpu(sbi->s_es->s_inodes_count)) {
88411013911SAndreas Dilger 		group = (goal - 1) / EXT4_INODES_PER_GROUP(sb);
88511013911SAndreas Dilger 		ino = (goal - 1) % EXT4_INODES_PER_GROUP(sb);
88611013911SAndreas Dilger 		ret2 = 0;
88711013911SAndreas Dilger 		goto got_group;
88811013911SAndreas Dilger 	}
88911013911SAndreas Dilger 
8904113c4caSLukas Czerner 	if (S_ISDIR(mode))
891f157a4aaSTheodore Ts'o 		ret2 = find_group_orlov(sb, dir, &group, mode, qstr);
8924113c4caSLukas Czerner 	else
893a4912123STheodore Ts'o 		ret2 = find_group_other(sb, dir, &group, mode);
894ac27a0ecSDave Kleikamp 
895772cb7c8SJose R. Santos got_group:
896a4912123STheodore Ts'o 	EXT4_I(dir)->i_last_alloc_group = group;
897ac27a0ecSDave Kleikamp 	err = -ENOSPC;
8982aa9fc4cSAvantika Mathur 	if (ret2 == -1)
899ac27a0ecSDave Kleikamp 		goto out;
900ac27a0ecSDave Kleikamp 
901119c0d44STheodore Ts'o 	/*
902119c0d44STheodore Ts'o 	 * Normally we will only go through one pass of this loop,
903119c0d44STheodore Ts'o 	 * unless we get unlucky and it turns out the group we selected
904119c0d44STheodore Ts'o 	 * had its last inode grabbed by someone else.
905119c0d44STheodore Ts'o 	 */
90611013911SAndreas Dilger 	for (i = 0; i < ngroups; i++, ino = 0) {
907ac27a0ecSDave Kleikamp 		err = -EIO;
908ac27a0ecSDave Kleikamp 
9093300bedaSAneesh Kumar K.V 		gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
910ac27a0ecSDave Kleikamp 		if (!gdp)
911eb9cc7e1SJan Kara 			goto out;
912ac27a0ecSDave Kleikamp 
913f2a09af6SYongqiang Yang 		/*
914f2a09af6SYongqiang Yang 		 * Check free inodes count before loading bitmap.
915f2a09af6SYongqiang Yang 		 */
9162fe435d8SWang Shilong 		if (ext4_free_inodes_count(sb, gdp) == 0)
9172fe435d8SWang Shilong 			goto next_group;
918f2a09af6SYongqiang Yang 
91987a39389SDarrick J. Wong 		grp = ext4_get_group_info(sb, group);
92087a39389SDarrick J. Wong 		/* Skip groups with already-known suspicious inode tables */
9212fe435d8SWang Shilong 		if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp))
9222fe435d8SWang Shilong 			goto next_group;
92387a39389SDarrick J. Wong 
9243300bedaSAneesh Kumar K.V 		brelse(inode_bitmap_bh);
9253300bedaSAneesh Kumar K.V 		inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
92687a39389SDarrick J. Wong 		/* Skip groups with suspicious inode tables */
9279008a58eSDarrick J. Wong 		if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp) ||
9289008a58eSDarrick J. Wong 		    IS_ERR(inode_bitmap_bh)) {
9299008a58eSDarrick J. Wong 			inode_bitmap_bh = NULL;
9302fe435d8SWang Shilong 			goto next_group;
93187a39389SDarrick J. Wong 		}
932ac27a0ecSDave Kleikamp 
933ac27a0ecSDave Kleikamp repeat_in_this_group:
934*901ed070SWang Shilong 		ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino);
935*901ed070SWang Shilong 		if (!ret2)
936a34eb503STheodore Ts'o 			goto next_group;
937*901ed070SWang Shilong 
938119c0d44STheodore Ts'o 		if (group == 0 && (ino + 1) < EXT4_FIRST_INO(sb)) {
939119c0d44STheodore Ts'o 			ext4_error(sb, "reserved inode found cleared - "
940119c0d44STheodore Ts'o 				   "inode=%lu", ino + 1);
9412fe435d8SWang Shilong 			goto next_group;
942119c0d44STheodore Ts'o 		}
943*901ed070SWang Shilong 
9441139575aSTheodore Ts'o 		if (!handle) {
9451139575aSTheodore Ts'o 			BUG_ON(nblocks <= 0);
9461139575aSTheodore Ts'o 			handle = __ext4_journal_start_sb(dir->i_sb, line_no,
9475fe2fe89SJan Kara 							 handle_type, nblocks,
9485fe2fe89SJan Kara 							 0);
9491139575aSTheodore Ts'o 			if (IS_ERR(handle)) {
9501139575aSTheodore Ts'o 				err = PTR_ERR(handle);
951eb9cc7e1SJan Kara 				ext4_std_error(sb, err);
952eb9cc7e1SJan Kara 				goto out;
9531139575aSTheodore Ts'o 			}
9541139575aSTheodore Ts'o 		}
955ffb5387eSEric Sandeen 		BUFFER_TRACE(inode_bitmap_bh, "get_write_access");
956ffb5387eSEric Sandeen 		err = ext4_journal_get_write_access(handle, inode_bitmap_bh);
957eb9cc7e1SJan Kara 		if (err) {
958eb9cc7e1SJan Kara 			ext4_std_error(sb, err);
959eb9cc7e1SJan Kara 			goto out;
960eb9cc7e1SJan Kara 		}
961119c0d44STheodore Ts'o 		ext4_lock_group(sb, group);
962119c0d44STheodore Ts'o 		ret2 = ext4_test_and_set_bit(ino, inode_bitmap_bh->b_data);
963*901ed070SWang Shilong 		if (ret2) {
964*901ed070SWang Shilong 			/* Someone already took the bit. Repeat the search
965*901ed070SWang Shilong 			 * with lock held.
966*901ed070SWang Shilong 			 */
967*901ed070SWang Shilong 			ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino);
968*901ed070SWang Shilong 			if (ret2) {
969*901ed070SWang Shilong 				ext4_set_bit(ino, inode_bitmap_bh->b_data);
970*901ed070SWang Shilong 				ret2 = 0;
971*901ed070SWang Shilong 			} else {
972*901ed070SWang Shilong 				ret2 = 1; /* we didn't grab the inode */
973*901ed070SWang Shilong 			}
974*901ed070SWang Shilong 		}
975119c0d44STheodore Ts'o 		ext4_unlock_group(sb, group);
976119c0d44STheodore Ts'o 		ino++;		/* the inode bitmap is zero-based */
977119c0d44STheodore Ts'o 		if (!ret2)
978119c0d44STheodore Ts'o 			goto got; /* we grabbed the inode! */
979*901ed070SWang Shilong 
980119c0d44STheodore Ts'o 		if (ino < EXT4_INODES_PER_GROUP(sb))
981119c0d44STheodore Ts'o 			goto repeat_in_this_group;
982a34eb503STheodore Ts'o next_group:
983a34eb503STheodore Ts'o 		if (++group == ngroups)
984a34eb503STheodore Ts'o 			group = 0;
985ac27a0ecSDave Kleikamp 	}
986ac27a0ecSDave Kleikamp 	err = -ENOSPC;
987ac27a0ecSDave Kleikamp 	goto out;
988ac27a0ecSDave Kleikamp 
989ac27a0ecSDave Kleikamp got:
990ffb5387eSEric Sandeen 	BUFFER_TRACE(inode_bitmap_bh, "call ext4_handle_dirty_metadata");
991ffb5387eSEric Sandeen 	err = ext4_handle_dirty_metadata(handle, NULL, inode_bitmap_bh);
992eb9cc7e1SJan Kara 	if (err) {
993eb9cc7e1SJan Kara 		ext4_std_error(sb, err);
994eb9cc7e1SJan Kara 		goto out;
995eb9cc7e1SJan Kara 	}
996ffb5387eSEric Sandeen 
99761c219f5STheodore Ts'o 	BUFFER_TRACE(group_desc_bh, "get_write_access");
99861c219f5STheodore Ts'o 	err = ext4_journal_get_write_access(handle, group_desc_bh);
99961c219f5STheodore Ts'o 	if (err) {
100061c219f5STheodore Ts'o 		ext4_std_error(sb, err);
100161c219f5STheodore Ts'o 		goto out;
100261c219f5STheodore Ts'o 	}
100361c219f5STheodore Ts'o 
1004717d50e4SAndreas Dilger 	/* We may have to initialize the block bitmap if it isn't already */
1005feb0ab32SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb) &&
1006717d50e4SAndreas Dilger 	    gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
10073300bedaSAneesh Kumar K.V 		struct buffer_head *block_bitmap_bh;
1008717d50e4SAndreas Dilger 
10093300bedaSAneesh Kumar K.V 		block_bitmap_bh = ext4_read_block_bitmap(sb, group);
10109008a58eSDarrick J. Wong 		if (IS_ERR(block_bitmap_bh)) {
10119008a58eSDarrick J. Wong 			err = PTR_ERR(block_bitmap_bh);
1012599a9b77SJan Kara 			goto out;
1013599a9b77SJan Kara 		}
10143300bedaSAneesh Kumar K.V 		BUFFER_TRACE(block_bitmap_bh, "get block bitmap access");
10153300bedaSAneesh Kumar K.V 		err = ext4_journal_get_write_access(handle, block_bitmap_bh);
1016717d50e4SAndreas Dilger 		if (err) {
10173300bedaSAneesh Kumar K.V 			brelse(block_bitmap_bh);
1018eb9cc7e1SJan Kara 			ext4_std_error(sb, err);
1019eb9cc7e1SJan Kara 			goto out;
1020717d50e4SAndreas Dilger 		}
1021717d50e4SAndreas Dilger 
1022fd034a84STheodore Ts'o 		BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
1023fd034a84STheodore Ts'o 		err = ext4_handle_dirty_metadata(handle, NULL, block_bitmap_bh);
1024fd034a84STheodore Ts'o 
1025717d50e4SAndreas Dilger 		/* recheck and clear flag under lock if we still need to */
1026fd034a84STheodore Ts'o 		ext4_lock_group(sb, group);
1027717d50e4SAndreas Dilger 		if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
10283300bedaSAneesh Kumar K.V 			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
1029021b65bbSTheodore Ts'o 			ext4_free_group_clusters_set(sb, gdp,
1030cff1dfd7STheodore Ts'o 				ext4_free_clusters_after_init(sb, group, gdp));
1031fa77dcfaSDarrick J. Wong 			ext4_block_bitmap_csum_set(sb, group, gdp,
103279f1ba49STao Ma 						   block_bitmap_bh);
1033feb0ab32SDarrick J. Wong 			ext4_group_desc_csum_set(sb, group, gdp);
1034717d50e4SAndreas Dilger 		}
1035955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, group);
1036aeb1e5d6STheodore Ts'o 		brelse(block_bitmap_bh);
1037717d50e4SAndreas Dilger 
1038eb9cc7e1SJan Kara 		if (err) {
1039eb9cc7e1SJan Kara 			ext4_std_error(sb, err);
1040eb9cc7e1SJan Kara 			goto out;
1041eb9cc7e1SJan Kara 		}
1042717d50e4SAndreas Dilger 	}
1043119c0d44STheodore Ts'o 
1044119c0d44STheodore Ts'o 	/* Update the relevant bg descriptor fields */
104541a246d1SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb)) {
1046119c0d44STheodore Ts'o 		int free;
1047119c0d44STheodore Ts'o 		struct ext4_group_info *grp = ext4_get_group_info(sb, group);
1048119c0d44STheodore Ts'o 
1049119c0d44STheodore Ts'o 		down_read(&grp->alloc_sem); /* protect vs itable lazyinit */
1050119c0d44STheodore Ts'o 		ext4_lock_group(sb, group); /* while we modify the bg desc */
1051119c0d44STheodore Ts'o 		free = EXT4_INODES_PER_GROUP(sb) -
1052119c0d44STheodore Ts'o 			ext4_itable_unused_count(sb, gdp);
1053119c0d44STheodore Ts'o 		if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
1054119c0d44STheodore Ts'o 			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
1055119c0d44STheodore Ts'o 			free = 0;
1056119c0d44STheodore Ts'o 		}
1057119c0d44STheodore Ts'o 		/*
1058119c0d44STheodore Ts'o 		 * Check the relative inode number against the last used
1059119c0d44STheodore Ts'o 		 * relative inode number in this group. if it is greater
1060119c0d44STheodore Ts'o 		 * we need to update the bg_itable_unused count
1061119c0d44STheodore Ts'o 		 */
1062119c0d44STheodore Ts'o 		if (ino > free)
1063119c0d44STheodore Ts'o 			ext4_itable_unused_set(sb, gdp,
1064119c0d44STheodore Ts'o 					(EXT4_INODES_PER_GROUP(sb) - ino));
1065119c0d44STheodore Ts'o 		up_read(&grp->alloc_sem);
10666f2e9f0eSTao Ma 	} else {
10676f2e9f0eSTao Ma 		ext4_lock_group(sb, group);
1068119c0d44STheodore Ts'o 	}
10696f2e9f0eSTao Ma 
1070119c0d44STheodore Ts'o 	ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1);
1071119c0d44STheodore Ts'o 	if (S_ISDIR(mode)) {
1072119c0d44STheodore Ts'o 		ext4_used_dirs_set(sb, gdp, ext4_used_dirs_count(sb, gdp) + 1);
1073119c0d44STheodore Ts'o 		if (sbi->s_log_groups_per_flex) {
1074119c0d44STheodore Ts'o 			ext4_group_t f = ext4_flex_group(sbi, group);
1075119c0d44STheodore Ts'o 
1076119c0d44STheodore Ts'o 			atomic_inc(&sbi->s_flex_groups[f].used_dirs);
1077119c0d44STheodore Ts'o 		}
1078119c0d44STheodore Ts'o 	}
107941a246d1SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb)) {
108041a246d1SDarrick J. Wong 		ext4_inode_bitmap_csum_set(sb, group, gdp, inode_bitmap_bh,
108141a246d1SDarrick J. Wong 					   EXT4_INODES_PER_GROUP(sb) / 8);
1082feb0ab32SDarrick J. Wong 		ext4_group_desc_csum_set(sb, group, gdp);
1083119c0d44STheodore Ts'o 	}
10846f2e9f0eSTao Ma 	ext4_unlock_group(sb, group);
1085119c0d44STheodore Ts'o 
10863300bedaSAneesh Kumar K.V 	BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata");
10873300bedaSAneesh Kumar K.V 	err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh);
1088eb9cc7e1SJan Kara 	if (err) {
1089eb9cc7e1SJan Kara 		ext4_std_error(sb, err);
1090eb9cc7e1SJan Kara 		goto out;
1091eb9cc7e1SJan Kara 	}
1092ac27a0ecSDave Kleikamp 
1093ac27a0ecSDave Kleikamp 	percpu_counter_dec(&sbi->s_freeinodes_counter);
1094ac27a0ecSDave Kleikamp 	if (S_ISDIR(mode))
1095ac27a0ecSDave Kleikamp 		percpu_counter_inc(&sbi->s_dirs_counter);
1096ac27a0ecSDave Kleikamp 
1097772cb7c8SJose R. Santos 	if (sbi->s_log_groups_per_flex) {
1098772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, group);
10999f24e420STheodore Ts'o 		atomic_dec(&sbi->s_flex_groups[flex_group].free_inodes);
1100772cb7c8SJose R. Santos 	}
1101ac27a0ecSDave Kleikamp 
1102717d50e4SAndreas Dilger 	inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
1103ac27a0ecSDave Kleikamp 	/* This is the optimal IO size (for stat), not the fs block size */
1104ac27a0ecSDave Kleikamp 	inode->i_blocks = 0;
1105ef7f3835SKalpak Shah 	inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
1106eeca7ea1SDeepa Dinamani 						       current_time(inode);
1107ac27a0ecSDave Kleikamp 
1108ac27a0ecSDave Kleikamp 	memset(ei->i_data, 0, sizeof(ei->i_data));
1109ac27a0ecSDave Kleikamp 	ei->i_dir_start_lookup = 0;
1110ac27a0ecSDave Kleikamp 	ei->i_disksize = 0;
1111ac27a0ecSDave Kleikamp 
11124af83508SEryu Guan 	/* Don't inherit extent flag from directory, amongst others. */
11132dc6b0d4SDuane Griffin 	ei->i_flags =
11142dc6b0d4SDuane Griffin 		ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED);
11151b917ed8STahsin Erdogan 	ei->i_flags |= i_flags;
1116ac27a0ecSDave Kleikamp 	ei->i_file_acl = 0;
1117ac27a0ecSDave Kleikamp 	ei->i_dtime = 0;
1118ac27a0ecSDave Kleikamp 	ei->i_block_group = group;
1119a4912123STheodore Ts'o 	ei->i_last_alloc_group = ~0;
1120ac27a0ecSDave Kleikamp 
1121617ba13bSMingming Cao 	ext4_set_inode_flags(inode);
1122ac27a0ecSDave Kleikamp 	if (IS_DIRSYNC(inode))
11230390131bSFrank Mayhar 		ext4_handle_sync(handle);
11246b38e842SAl Viro 	if (insert_inode_locked(inode) < 0) {
1125acd6ad83SJan Kara 		/*
1126acd6ad83SJan Kara 		 * Likely a bitmap corruption causing inode to be allocated
1127acd6ad83SJan Kara 		 * twice.
1128acd6ad83SJan Kara 		 */
1129acd6ad83SJan Kara 		err = -EIO;
1130eb9cc7e1SJan Kara 		ext4_error(sb, "failed to insert inode %lu: doubly allocated?",
1131eb9cc7e1SJan Kara 			   inode->i_ino);
1132eb9cc7e1SJan Kara 		goto out;
11336b38e842SAl Viro 	}
1134ac27a0ecSDave Kleikamp 	spin_lock(&sbi->s_next_gen_lock);
1135ac27a0ecSDave Kleikamp 	inode->i_generation = sbi->s_next_generation++;
1136ac27a0ecSDave Kleikamp 	spin_unlock(&sbi->s_next_gen_lock);
1137ac27a0ecSDave Kleikamp 
1138814525f4SDarrick J. Wong 	/* Precompute checksum seed for inode metadata */
11399aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sb)) {
1140814525f4SDarrick J. Wong 		__u32 csum;
1141814525f4SDarrick J. Wong 		__le32 inum = cpu_to_le32(inode->i_ino);
1142814525f4SDarrick J. Wong 		__le32 gen = cpu_to_le32(inode->i_generation);
1143814525f4SDarrick J. Wong 		csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
1144814525f4SDarrick J. Wong 				   sizeof(inum));
1145814525f4SDarrick J. Wong 		ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
1146814525f4SDarrick J. Wong 					      sizeof(gen));
1147814525f4SDarrick J. Wong 	}
1148814525f4SDarrick J. Wong 
1149353eb83cSTheodore Ts'o 	ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
115019f5fb7aSTheodore Ts'o 	ext4_set_inode_state(inode, EXT4_STATE_NEW);
1151ef7f3835SKalpak Shah 
1152ef7f3835SKalpak Shah 	ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize;
1153f08225d1STao Ma 	ei->i_inline_off = 0;
1154e2b911c5SDarrick J. Wong 	if (ext4_has_feature_inline_data(sb))
1155f08225d1STao Ma 		ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
1156ac27a0ecSDave Kleikamp 	ret = inode;
115763936ddaSChristoph Hellwig 	err = dquot_alloc_inode(inode);
115863936ddaSChristoph Hellwig 	if (err)
1159ac27a0ecSDave Kleikamp 		goto fail_drop;
1160ac27a0ecSDave Kleikamp 
1161aa1dca3bSEric Biggers 	/*
1162aa1dca3bSEric Biggers 	 * Since the encryption xattr will always be unique, create it first so
1163aa1dca3bSEric Biggers 	 * that it's less likely to end up in an external xattr block and
1164aa1dca3bSEric Biggers 	 * prevent its deduplication.
1165aa1dca3bSEric Biggers 	 */
1166aa1dca3bSEric Biggers 	if (encrypt) {
1167aa1dca3bSEric Biggers 		err = fscrypt_inherit_context(dir, inode, handle, true);
1168aa1dca3bSEric Biggers 		if (err)
1169aa1dca3bSEric Biggers 			goto fail_free_drop;
1170aa1dca3bSEric Biggers 	}
1171aa1dca3bSEric Biggers 
11721b917ed8STahsin Erdogan 	if (!(ei->i_flags & EXT4_EA_INODE_FL)) {
1173617ba13bSMingming Cao 		err = ext4_init_acl(handle, inode, dir);
1174ac27a0ecSDave Kleikamp 		if (err)
1175ac27a0ecSDave Kleikamp 			goto fail_free_drop;
1176ac27a0ecSDave Kleikamp 
11772a7dba39SEric Paris 		err = ext4_init_security(handle, inode, dir, qstr);
1178ac27a0ecSDave Kleikamp 		if (err)
1179ac27a0ecSDave Kleikamp 			goto fail_free_drop;
1180ad47f953STahsin Erdogan 	}
1181ac27a0ecSDave Kleikamp 
1182e2b911c5SDarrick J. Wong 	if (ext4_has_feature_extents(sb)) {
1183e4079a11SEric Sandeen 		/* set extent flag only for directory, file and normal symlink*/
1184e65187e6SAneesh Kumar K.V 		if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
118512e9b892SDmitry Monakhov 			ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
1186a86c6181SAlex Tomas 			ext4_ext_tree_init(handle, inode);
1187a86c6181SAlex Tomas 		}
118842bf0383SAneesh Kumar K.V 	}
1189ac27a0ecSDave Kleikamp 
1190688f869cSTheodore Ts'o 	if (ext4_handle_valid(handle)) {
1191688f869cSTheodore Ts'o 		ei->i_sync_tid = handle->h_transaction->t_tid;
1192688f869cSTheodore Ts'o 		ei->i_datasync_tid = handle->h_transaction->t_tid;
1193688f869cSTheodore Ts'o 	}
1194688f869cSTheodore Ts'o 
11958753e88fSAneesh Kumar K.V 	err = ext4_mark_inode_dirty(handle, inode);
11968753e88fSAneesh Kumar K.V 	if (err) {
11978753e88fSAneesh Kumar K.V 		ext4_std_error(sb, err);
11988753e88fSAneesh Kumar K.V 		goto fail_free_drop;
11998753e88fSAneesh Kumar K.V 	}
12008753e88fSAneesh Kumar K.V 
1201617ba13bSMingming Cao 	ext4_debug("allocating inode %lu\n", inode->i_ino);
12029bffad1eSTheodore Ts'o 	trace_ext4_allocate_inode(inode, dir, mode);
12033300bedaSAneesh Kumar K.V 	brelse(inode_bitmap_bh);
1204ac27a0ecSDave Kleikamp 	return ret;
1205ac27a0ecSDave Kleikamp 
1206ac27a0ecSDave Kleikamp fail_free_drop:
120763936ddaSChristoph Hellwig 	dquot_free_inode(inode);
1208ac27a0ecSDave Kleikamp fail_drop:
12096d6b77f1SMiklos Szeredi 	clear_nlink(inode);
12106b38e842SAl Viro 	unlock_new_inode(inode);
1211eb9cc7e1SJan Kara out:
1212eb9cc7e1SJan Kara 	dquot_drop(inode);
1213eb9cc7e1SJan Kara 	inode->i_flags |= S_NOQUOTA;
1214ac27a0ecSDave Kleikamp 	iput(inode);
12153300bedaSAneesh Kumar K.V 	brelse(inode_bitmap_bh);
1216ac27a0ecSDave Kleikamp 	return ERR_PTR(err);
1217ac27a0ecSDave Kleikamp }
1218ac27a0ecSDave Kleikamp 
1219ac27a0ecSDave Kleikamp /* Verify that we are loading a valid orphan from disk */
1220617ba13bSMingming Cao struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
1221ac27a0ecSDave Kleikamp {
1222617ba13bSMingming Cao 	unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
1223fd2d4291SAvantika Mathur 	ext4_group_t block_group;
1224ac27a0ecSDave Kleikamp 	int bit;
12257827a7f6STheodore Ts'o 	struct buffer_head *bitmap_bh = NULL;
1226ac27a0ecSDave Kleikamp 	struct inode *inode = NULL;
12277827a7f6STheodore Ts'o 	int err = -EFSCORRUPTED;
1228ac27a0ecSDave Kleikamp 
12297827a7f6STheodore Ts'o 	if (ino < EXT4_FIRST_INO(sb) || ino > max_ino)
12307827a7f6STheodore Ts'o 		goto bad_orphan;
1231ac27a0ecSDave Kleikamp 
1232617ba13bSMingming Cao 	block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
1233617ba13bSMingming Cao 	bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
1234e29d1cdeSEric Sandeen 	bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
12359008a58eSDarrick J. Wong 	if (IS_ERR(bitmap_bh)) {
12367827a7f6STheodore Ts'o 		ext4_error(sb, "inode bitmap error %ld for orphan %lu",
12377827a7f6STheodore Ts'o 			   ino, PTR_ERR(bitmap_bh));
12387827a7f6STheodore Ts'o 		return (struct inode *) bitmap_bh;
1239ac27a0ecSDave Kleikamp 	}
1240ac27a0ecSDave Kleikamp 
1241ac27a0ecSDave Kleikamp 	/* Having the inode bit set should be a 100% indicator that this
1242ac27a0ecSDave Kleikamp 	 * is a valid orphan (no e2fsck run on fs).  Orphans also include
1243ac27a0ecSDave Kleikamp 	 * inodes that were being truncated, so we can't check i_nlink==0.
1244ac27a0ecSDave Kleikamp 	 */
12451d1fe1eeSDavid Howells 	if (!ext4_test_bit(bit, bitmap_bh->b_data))
12461d1fe1eeSDavid Howells 		goto bad_orphan;
12471d1fe1eeSDavid Howells 
12481d1fe1eeSDavid Howells 	inode = ext4_iget(sb, ino);
12497827a7f6STheodore Ts'o 	if (IS_ERR(inode)) {
12507827a7f6STheodore Ts'o 		err = PTR_ERR(inode);
12517827a7f6STheodore Ts'o 		ext4_error(sb, "couldn't read orphan inode %lu (err %d)",
12527827a7f6STheodore Ts'o 			   ino, err);
12537827a7f6STheodore Ts'o 		return inode;
12547827a7f6STheodore Ts'o 	}
12551d1fe1eeSDavid Howells 
125691ef4cafSDuane Griffin 	/*
1257c9eb13a9STheodore Ts'o 	 * If the orphans has i_nlinks > 0 then it should be able to
1258c9eb13a9STheodore Ts'o 	 * be truncated, otherwise it won't be removed from the orphan
1259c9eb13a9STheodore Ts'o 	 * list during processing and an infinite loop will result.
1260c9eb13a9STheodore Ts'o 	 * Similarly, it must not be a bad inode.
126191ef4cafSDuane Griffin 	 */
1262c9eb13a9STheodore Ts'o 	if ((inode->i_nlink && !ext4_can_truncate(inode)) ||
1263c9eb13a9STheodore Ts'o 	    is_bad_inode(inode))
126491ef4cafSDuane Griffin 		goto bad_orphan;
126591ef4cafSDuane Griffin 
12661d1fe1eeSDavid Howells 	if (NEXT_ORPHAN(inode) > max_ino)
12671d1fe1eeSDavid Howells 		goto bad_orphan;
12681d1fe1eeSDavid Howells 	brelse(bitmap_bh);
12691d1fe1eeSDavid Howells 	return inode;
12701d1fe1eeSDavid Howells 
12711d1fe1eeSDavid Howells bad_orphan:
12727827a7f6STheodore Ts'o 	ext4_error(sb, "bad orphan inode %lu", ino);
12737827a7f6STheodore Ts'o 	if (bitmap_bh)
12747827a7f6STheodore Ts'o 		printk(KERN_ERR "ext4_test_bit(bit=%d, block=%llu) = %d\n",
1275ac27a0ecSDave Kleikamp 		       bit, (unsigned long long)bitmap_bh->b_blocknr,
1276617ba13bSMingming Cao 		       ext4_test_bit(bit, bitmap_bh->b_data));
1277ac27a0ecSDave Kleikamp 	if (inode) {
12787827a7f6STheodore Ts'o 		printk(KERN_ERR "is_bad_inode(inode)=%d\n",
1279ac27a0ecSDave Kleikamp 		       is_bad_inode(inode));
12807827a7f6STheodore Ts'o 		printk(KERN_ERR "NEXT_ORPHAN(inode)=%u\n",
1281ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
12827827a7f6STheodore Ts'o 		printk(KERN_ERR "max_ino=%lu\n", max_ino);
12837827a7f6STheodore Ts'o 		printk(KERN_ERR "i_nlink=%u\n", inode->i_nlink);
1284ac27a0ecSDave Kleikamp 		/* Avoid freeing blocks if we got a bad deleted inode */
12851d1fe1eeSDavid Howells 		if (inode->i_nlink == 0)
1286ac27a0ecSDave Kleikamp 			inode->i_blocks = 0;
1287ac27a0ecSDave Kleikamp 		iput(inode);
1288ac27a0ecSDave Kleikamp 	}
1289ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
12901d1fe1eeSDavid Howells 	return ERR_PTR(err);
1291ac27a0ecSDave Kleikamp }
1292ac27a0ecSDave Kleikamp 
1293617ba13bSMingming Cao unsigned long ext4_count_free_inodes(struct super_block *sb)
1294ac27a0ecSDave Kleikamp {
1295ac27a0ecSDave Kleikamp 	unsigned long desc_count;
1296617ba13bSMingming Cao 	struct ext4_group_desc *gdp;
12978df9675fSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
1298617ba13bSMingming Cao #ifdef EXT4FS_DEBUG
1299617ba13bSMingming Cao 	struct ext4_super_block *es;
1300ac27a0ecSDave Kleikamp 	unsigned long bitmap_count, x;
1301ac27a0ecSDave Kleikamp 	struct buffer_head *bitmap_bh = NULL;
1302ac27a0ecSDave Kleikamp 
1303617ba13bSMingming Cao 	es = EXT4_SB(sb)->s_es;
1304ac27a0ecSDave Kleikamp 	desc_count = 0;
1305ac27a0ecSDave Kleikamp 	bitmap_count = 0;
1306ac27a0ecSDave Kleikamp 	gdp = NULL;
13078df9675fSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
1308617ba13bSMingming Cao 		gdp = ext4_get_group_desc(sb, i, NULL);
1309ac27a0ecSDave Kleikamp 		if (!gdp)
1310ac27a0ecSDave Kleikamp 			continue;
1311560671a0SAneesh Kumar K.V 		desc_count += ext4_free_inodes_count(sb, gdp);
1312ac27a0ecSDave Kleikamp 		brelse(bitmap_bh);
1313e29d1cdeSEric Sandeen 		bitmap_bh = ext4_read_inode_bitmap(sb, i);
13149008a58eSDarrick J. Wong 		if (IS_ERR(bitmap_bh)) {
13159008a58eSDarrick J. Wong 			bitmap_bh = NULL;
1316ac27a0ecSDave Kleikamp 			continue;
13179008a58eSDarrick J. Wong 		}
1318ac27a0ecSDave Kleikamp 
1319f6fb99caSTheodore Ts'o 		x = ext4_count_free(bitmap_bh->b_data,
1320f6fb99caSTheodore Ts'o 				    EXT4_INODES_PER_GROUP(sb) / 8);
1321c549a95dSEric Sandeen 		printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
1322785b4b3aSPeng Tao 			(unsigned long) i, ext4_free_inodes_count(sb, gdp), x);
1323ac27a0ecSDave Kleikamp 		bitmap_count += x;
1324ac27a0ecSDave Kleikamp 	}
1325ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
13264776004fSTheodore Ts'o 	printk(KERN_DEBUG "ext4_count_free_inodes: "
13274776004fSTheodore Ts'o 	       "stored = %u, computed = %lu, %lu\n",
1328ac27a0ecSDave Kleikamp 	       le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
1329ac27a0ecSDave Kleikamp 	return desc_count;
1330ac27a0ecSDave Kleikamp #else
1331ac27a0ecSDave Kleikamp 	desc_count = 0;
13328df9675fSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
1333617ba13bSMingming Cao 		gdp = ext4_get_group_desc(sb, i, NULL);
1334ac27a0ecSDave Kleikamp 		if (!gdp)
1335ac27a0ecSDave Kleikamp 			continue;
1336560671a0SAneesh Kumar K.V 		desc_count += ext4_free_inodes_count(sb, gdp);
1337ac27a0ecSDave Kleikamp 		cond_resched();
1338ac27a0ecSDave Kleikamp 	}
1339ac27a0ecSDave Kleikamp 	return desc_count;
1340ac27a0ecSDave Kleikamp #endif
1341ac27a0ecSDave Kleikamp }
1342ac27a0ecSDave Kleikamp 
1343ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
1344617ba13bSMingming Cao unsigned long ext4_count_dirs(struct super_block * sb)
1345ac27a0ecSDave Kleikamp {
1346ac27a0ecSDave Kleikamp 	unsigned long count = 0;
13478df9675fSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
1348ac27a0ecSDave Kleikamp 
13498df9675fSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
1350617ba13bSMingming Cao 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1351ac27a0ecSDave Kleikamp 		if (!gdp)
1352ac27a0ecSDave Kleikamp 			continue;
1353560671a0SAneesh Kumar K.V 		count += ext4_used_dirs_count(sb, gdp);
1354ac27a0ecSDave Kleikamp 	}
1355ac27a0ecSDave Kleikamp 	return count;
1356ac27a0ecSDave Kleikamp }
1357bfff6873SLukas Czerner 
1358bfff6873SLukas Czerner /*
1359bfff6873SLukas Czerner  * Zeroes not yet zeroed inode table - just write zeroes through the whole
1360bfff6873SLukas Czerner  * inode table. Must be called without any spinlock held. The only place
1361bfff6873SLukas Czerner  * where it is called from on active part of filesystem is ext4lazyinit
1362bfff6873SLukas Czerner  * thread, so we do not need any special locks, however we have to prevent
1363bfff6873SLukas Czerner  * inode allocation from the current group, so we take alloc_sem lock, to
1364119c0d44STheodore Ts'o  * block ext4_new_inode() until we are finished.
1365bfff6873SLukas Czerner  */
1366e0cbee3eSH Hartley Sweeten int ext4_init_inode_table(struct super_block *sb, ext4_group_t group,
1367bfff6873SLukas Czerner 				 int barrier)
1368bfff6873SLukas Czerner {
1369bfff6873SLukas Czerner 	struct ext4_group_info *grp = ext4_get_group_info(sb, group);
1370bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1371bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
1372bfff6873SLukas Czerner 	struct buffer_head *group_desc_bh;
1373bfff6873SLukas Czerner 	handle_t *handle;
1374bfff6873SLukas Czerner 	ext4_fsblk_t blk;
1375bfff6873SLukas Czerner 	int num, ret = 0, used_blks = 0;
1376bfff6873SLukas Czerner 
1377bfff6873SLukas Czerner 	/* This should not happen, but just to be sure check this */
1378bfff6873SLukas Czerner 	if (sb->s_flags & MS_RDONLY) {
1379bfff6873SLukas Czerner 		ret = 1;
1380bfff6873SLukas Czerner 		goto out;
1381bfff6873SLukas Czerner 	}
1382bfff6873SLukas Czerner 
1383bfff6873SLukas Czerner 	gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
1384bfff6873SLukas Czerner 	if (!gdp)
1385bfff6873SLukas Czerner 		goto out;
1386bfff6873SLukas Czerner 
1387bfff6873SLukas Czerner 	/*
1388bfff6873SLukas Czerner 	 * We do not need to lock this, because we are the only one
1389bfff6873SLukas Czerner 	 * handling this flag.
1390bfff6873SLukas Czerner 	 */
1391bfff6873SLukas Czerner 	if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED))
1392bfff6873SLukas Czerner 		goto out;
1393bfff6873SLukas Czerner 
13949924a92aSTheodore Ts'o 	handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
1395bfff6873SLukas Czerner 	if (IS_ERR(handle)) {
1396bfff6873SLukas Czerner 		ret = PTR_ERR(handle);
1397bfff6873SLukas Czerner 		goto out;
1398bfff6873SLukas Czerner 	}
1399bfff6873SLukas Czerner 
1400bfff6873SLukas Czerner 	down_write(&grp->alloc_sem);
1401bfff6873SLukas Czerner 	/*
1402bfff6873SLukas Czerner 	 * If inode bitmap was already initialized there may be some
1403bfff6873SLukas Czerner 	 * used inodes so we need to skip blocks with used inodes in
1404bfff6873SLukas Czerner 	 * inode table.
1405bfff6873SLukas Czerner 	 */
1406bfff6873SLukas Czerner 	if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)))
1407bfff6873SLukas Czerner 		used_blks = DIV_ROUND_UP((EXT4_INODES_PER_GROUP(sb) -
1408bfff6873SLukas Czerner 			    ext4_itable_unused_count(sb, gdp)),
1409bfff6873SLukas Czerner 			    sbi->s_inodes_per_block);
1410bfff6873SLukas Czerner 
1411857ac889SLukas Czerner 	if ((used_blks < 0) || (used_blks > sbi->s_itb_per_group)) {
14121084f252STheodore Ts'o 		ext4_error(sb, "Something is wrong with group %u: "
14131084f252STheodore Ts'o 			   "used itable blocks: %d; "
14141084f252STheodore Ts'o 			   "itable unused count: %u",
1415857ac889SLukas Czerner 			   group, used_blks,
1416857ac889SLukas Czerner 			   ext4_itable_unused_count(sb, gdp));
1417857ac889SLukas Czerner 		ret = 1;
141833853a0dSYongqiang Yang 		goto err_out;
1419857ac889SLukas Czerner 	}
1420857ac889SLukas Czerner 
1421bfff6873SLukas Czerner 	blk = ext4_inode_table(sb, gdp) + used_blks;
1422bfff6873SLukas Czerner 	num = sbi->s_itb_per_group - used_blks;
1423bfff6873SLukas Czerner 
1424bfff6873SLukas Czerner 	BUFFER_TRACE(group_desc_bh, "get_write_access");
1425bfff6873SLukas Czerner 	ret = ext4_journal_get_write_access(handle,
1426bfff6873SLukas Czerner 					    group_desc_bh);
1427bfff6873SLukas Czerner 	if (ret)
1428bfff6873SLukas Czerner 		goto err_out;
1429bfff6873SLukas Czerner 
1430bfff6873SLukas Czerner 	/*
1431bfff6873SLukas Czerner 	 * Skip zeroout if the inode table is full. But we set the ZEROED
1432bfff6873SLukas Czerner 	 * flag anyway, because obviously, when it is full it does not need
1433bfff6873SLukas Czerner 	 * further zeroing.
1434bfff6873SLukas Czerner 	 */
1435bfff6873SLukas Czerner 	if (unlikely(num == 0))
1436bfff6873SLukas Czerner 		goto skip_zeroout;
1437bfff6873SLukas Czerner 
1438bfff6873SLukas Czerner 	ext4_debug("going to zero out inode table in group %d\n",
1439bfff6873SLukas Czerner 		   group);
1440a107e5a3STheodore Ts'o 	ret = sb_issue_zeroout(sb, blk, num, GFP_NOFS);
1441bfff6873SLukas Czerner 	if (ret < 0)
1442bfff6873SLukas Czerner 		goto err_out;
1443a107e5a3STheodore Ts'o 	if (barrier)
1444a107e5a3STheodore Ts'o 		blkdev_issue_flush(sb->s_bdev, GFP_NOFS, NULL);
1445bfff6873SLukas Czerner 
1446bfff6873SLukas Czerner skip_zeroout:
1447bfff6873SLukas Czerner 	ext4_lock_group(sb, group);
1448bfff6873SLukas Czerner 	gdp->bg_flags |= cpu_to_le16(EXT4_BG_INODE_ZEROED);
1449feb0ab32SDarrick J. Wong 	ext4_group_desc_csum_set(sb, group, gdp);
1450bfff6873SLukas Czerner 	ext4_unlock_group(sb, group);
1451bfff6873SLukas Czerner 
1452bfff6873SLukas Czerner 	BUFFER_TRACE(group_desc_bh,
1453bfff6873SLukas Czerner 		     "call ext4_handle_dirty_metadata");
1454bfff6873SLukas Czerner 	ret = ext4_handle_dirty_metadata(handle, NULL,
1455bfff6873SLukas Czerner 					 group_desc_bh);
1456bfff6873SLukas Czerner 
1457bfff6873SLukas Czerner err_out:
1458bfff6873SLukas Czerner 	up_write(&grp->alloc_sem);
1459bfff6873SLukas Czerner 	ext4_journal_stop(handle);
1460bfff6873SLukas Czerner out:
1461bfff6873SLukas Czerner 	return ret;
1462bfff6873SLukas Czerner }
1463