xref: /linux/fs/ext4/ialloc.c (revision 119c0d4460b001e44b41dcf73dc6ee794b98bd31)
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>
17dab291afSMingming Cao #include <linux/jbd2.h>
18ac27a0ecSDave Kleikamp #include <linux/stat.h>
19ac27a0ecSDave Kleikamp #include <linux/string.h>
20ac27a0ecSDave Kleikamp #include <linux/quotaops.h>
21ac27a0ecSDave Kleikamp #include <linux/buffer_head.h>
22ac27a0ecSDave Kleikamp #include <linux/random.h>
23ac27a0ecSDave Kleikamp #include <linux/bitops.h>
243a5b2ecdSMingming Cao #include <linux/blkdev.h>
25ac27a0ecSDave Kleikamp #include <asm/byteorder.h>
269bffad1eSTheodore Ts'o 
273dcf5451SChristoph Hellwig #include "ext4.h"
283dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
29ac27a0ecSDave Kleikamp #include "xattr.h"
30ac27a0ecSDave Kleikamp #include "acl.h"
31ac27a0ecSDave Kleikamp 
329bffad1eSTheodore Ts'o #include <trace/events/ext4.h>
339bffad1eSTheodore Ts'o 
34ac27a0ecSDave Kleikamp /*
35ac27a0ecSDave Kleikamp  * ialloc.c contains the inodes allocation and deallocation routines
36ac27a0ecSDave Kleikamp  */
37ac27a0ecSDave Kleikamp 
38ac27a0ecSDave Kleikamp /*
39ac27a0ecSDave Kleikamp  * The free inodes are managed by bitmaps.  A file system contains several
40ac27a0ecSDave Kleikamp  * blocks groups.  Each group contains 1 bitmap block for blocks, 1 bitmap
41ac27a0ecSDave Kleikamp  * block for inodes, N blocks for the inode table and data blocks.
42ac27a0ecSDave Kleikamp  *
43ac27a0ecSDave Kleikamp  * The file system contains group descriptors which are located after the
44ac27a0ecSDave Kleikamp  * super block.  Each descriptor contains the number of the bitmap block and
45ac27a0ecSDave Kleikamp  * the free blocks count in the block.
46ac27a0ecSDave Kleikamp  */
47ac27a0ecSDave Kleikamp 
48717d50e4SAndreas Dilger /*
49717d50e4SAndreas Dilger  * To avoid calling the atomic setbit hundreds or thousands of times, we only
50717d50e4SAndreas Dilger  * need to use it within a single byte (to ensure we get endianness right).
51717d50e4SAndreas Dilger  * We can use memset for the rest of the bitmap as there are no other users.
52717d50e4SAndreas Dilger  */
5361d08673STheodore Ts'o void ext4_mark_bitmap_end(int start_bit, int end_bit, char *bitmap)
54717d50e4SAndreas Dilger {
55717d50e4SAndreas Dilger 	int i;
56717d50e4SAndreas Dilger 
57717d50e4SAndreas Dilger 	if (start_bit >= end_bit)
58717d50e4SAndreas Dilger 		return;
59717d50e4SAndreas Dilger 
60717d50e4SAndreas Dilger 	ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit);
61717d50e4SAndreas Dilger 	for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
62717d50e4SAndreas Dilger 		ext4_set_bit(i, bitmap);
63717d50e4SAndreas Dilger 	if (i < end_bit)
64717d50e4SAndreas Dilger 		memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
65717d50e4SAndreas Dilger }
66717d50e4SAndreas Dilger 
67717d50e4SAndreas Dilger /* Initializes an uninitialized inode bitmap */
681f109d5aSTheodore Ts'o static unsigned ext4_init_inode_bitmap(struct super_block *sb,
691f109d5aSTheodore Ts'o 				       struct buffer_head *bh,
70fd2d4291SAvantika Mathur 				       ext4_group_t block_group,
71717d50e4SAndreas Dilger 				       struct ext4_group_desc *gdp)
72717d50e4SAndreas Dilger {
73717d50e4SAndreas Dilger 	struct ext4_sb_info *sbi = EXT4_SB(sb);
74717d50e4SAndreas Dilger 
75717d50e4SAndreas Dilger 	J_ASSERT_BH(bh, buffer_locked(bh));
76717d50e4SAndreas Dilger 
77717d50e4SAndreas Dilger 	/* If checksum is bad mark all blocks and inodes use to prevent
78717d50e4SAndreas Dilger 	 * allocation, essentially implementing a per-group read-only flag. */
79717d50e4SAndreas Dilger 	if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) {
8012062dddSEric Sandeen 		ext4_error(sb, "Checksum bad for group %u", block_group);
81021b65bbSTheodore Ts'o 		ext4_free_group_clusters_set(sb, gdp, 0);
82560671a0SAneesh Kumar K.V 		ext4_free_inodes_set(sb, gdp, 0);
83560671a0SAneesh Kumar K.V 		ext4_itable_unused_set(sb, gdp, 0);
84717d50e4SAndreas Dilger 		memset(bh->b_data, 0xff, sb->s_blocksize);
85717d50e4SAndreas Dilger 		return 0;
86717d50e4SAndreas Dilger 	}
87717d50e4SAndreas Dilger 
88717d50e4SAndreas Dilger 	memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8);
8961d08673STheodore Ts'o 	ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), sb->s_blocksize * 8,
90717d50e4SAndreas Dilger 			bh->b_data);
91717d50e4SAndreas Dilger 
92717d50e4SAndreas Dilger 	return EXT4_INODES_PER_GROUP(sb);
93717d50e4SAndreas Dilger }
94ac27a0ecSDave Kleikamp 
95ac27a0ecSDave Kleikamp /*
96ac27a0ecSDave Kleikamp  * Read the inode allocation bitmap for a given block_group, reading
97ac27a0ecSDave Kleikamp  * into the specified slot in the superblock's bitmap cache.
98ac27a0ecSDave Kleikamp  *
99ac27a0ecSDave Kleikamp  * Return buffer_head of bitmap on success or NULL.
100ac27a0ecSDave Kleikamp  */
101ac27a0ecSDave Kleikamp static struct buffer_head *
102e29d1cdeSEric Sandeen ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group)
103ac27a0ecSDave Kleikamp {
104617ba13bSMingming Cao 	struct ext4_group_desc *desc;
105ac27a0ecSDave Kleikamp 	struct buffer_head *bh = NULL;
106e29d1cdeSEric Sandeen 	ext4_fsblk_t bitmap_blk;
107ac27a0ecSDave Kleikamp 
108617ba13bSMingming Cao 	desc = ext4_get_group_desc(sb, block_group, NULL);
109ac27a0ecSDave Kleikamp 	if (!desc)
110e29d1cdeSEric Sandeen 		return NULL;
111bfff6873SLukas Czerner 
112e29d1cdeSEric Sandeen 	bitmap_blk = ext4_inode_bitmap(sb, desc);
113e29d1cdeSEric Sandeen 	bh = sb_getblk(sb, bitmap_blk);
114e29d1cdeSEric Sandeen 	if (unlikely(!bh)) {
11512062dddSEric Sandeen 		ext4_error(sb, "Cannot read inode bitmap - "
116a9df9a49STheodore Ts'o 			    "block_group = %u, inode_bitmap = %llu",
117e29d1cdeSEric Sandeen 			    block_group, bitmap_blk);
118e29d1cdeSEric Sandeen 		return NULL;
119e29d1cdeSEric Sandeen 	}
1202ccb5fb9SAneesh Kumar K.V 	if (bitmap_uptodate(bh))
121e29d1cdeSEric Sandeen 		return bh;
122e29d1cdeSEric Sandeen 
123c806e68fSFrederic Bohe 	lock_buffer(bh);
1242ccb5fb9SAneesh Kumar K.V 	if (bitmap_uptodate(bh)) {
1252ccb5fb9SAneesh Kumar K.V 		unlock_buffer(bh);
1262ccb5fb9SAneesh Kumar K.V 		return bh;
1272ccb5fb9SAneesh Kumar K.V 	}
128bfff6873SLukas Czerner 
129955ce5f5SAneesh Kumar K.V 	ext4_lock_group(sb, block_group);
130e29d1cdeSEric Sandeen 	if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
131e29d1cdeSEric Sandeen 		ext4_init_inode_bitmap(sb, bh, block_group, desc);
1322ccb5fb9SAneesh Kumar K.V 		set_bitmap_uptodate(bh);
133e29d1cdeSEric Sandeen 		set_buffer_uptodate(bh);
134955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, block_group);
1353300bedaSAneesh Kumar K.V 		unlock_buffer(bh);
136e29d1cdeSEric Sandeen 		return bh;
137e29d1cdeSEric Sandeen 	}
138955ce5f5SAneesh Kumar K.V 	ext4_unlock_group(sb, block_group);
139bfff6873SLukas Czerner 
1402ccb5fb9SAneesh Kumar K.V 	if (buffer_uptodate(bh)) {
1412ccb5fb9SAneesh Kumar K.V 		/*
1422ccb5fb9SAneesh Kumar K.V 		 * if not uninit if bh is uptodate,
1432ccb5fb9SAneesh Kumar K.V 		 * bitmap is also uptodate
1442ccb5fb9SAneesh Kumar K.V 		 */
1452ccb5fb9SAneesh Kumar K.V 		set_bitmap_uptodate(bh);
1462ccb5fb9SAneesh Kumar K.V 		unlock_buffer(bh);
1472ccb5fb9SAneesh Kumar K.V 		return bh;
1482ccb5fb9SAneesh Kumar K.V 	}
1492ccb5fb9SAneesh Kumar K.V 	/*
1502ccb5fb9SAneesh Kumar K.V 	 * submit the buffer_head for read. We can
1512ccb5fb9SAneesh Kumar K.V 	 * safely mark the bitmap as uptodate now.
1522ccb5fb9SAneesh Kumar K.V 	 * We do it here so the bitmap uptodate bit
1532ccb5fb9SAneesh Kumar K.V 	 * get set with buffer lock held.
1542ccb5fb9SAneesh Kumar K.V 	 */
1550562e0baSJiaying Zhang 	trace_ext4_load_inode_bitmap(sb, block_group);
1562ccb5fb9SAneesh Kumar K.V 	set_bitmap_uptodate(bh);
157e29d1cdeSEric Sandeen 	if (bh_submit_read(bh) < 0) {
158e29d1cdeSEric Sandeen 		put_bh(bh);
15912062dddSEric Sandeen 		ext4_error(sb, "Cannot read inode bitmap - "
160a9df9a49STheodore Ts'o 			    "block_group = %u, inode_bitmap = %llu",
161e29d1cdeSEric Sandeen 			    block_group, bitmap_blk);
162e29d1cdeSEric Sandeen 		return NULL;
163e29d1cdeSEric Sandeen 	}
164ac27a0ecSDave Kleikamp 	return bh;
165ac27a0ecSDave Kleikamp }
166ac27a0ecSDave Kleikamp 
167ac27a0ecSDave Kleikamp /*
168ac27a0ecSDave Kleikamp  * NOTE! When we get the inode, we're the only people
169ac27a0ecSDave Kleikamp  * that have access to it, and as such there are no
170ac27a0ecSDave Kleikamp  * race conditions we have to worry about. The inode
171ac27a0ecSDave Kleikamp  * is not on the hash-lists, and it cannot be reached
172ac27a0ecSDave Kleikamp  * through the filesystem because the directory entry
173ac27a0ecSDave Kleikamp  * has been deleted earlier.
174ac27a0ecSDave Kleikamp  *
175ac27a0ecSDave Kleikamp  * HOWEVER: we must make sure that we get no aliases,
176ac27a0ecSDave Kleikamp  * which means that we have to call "clear_inode()"
177ac27a0ecSDave Kleikamp  * _before_ we mark the inode not in use in the inode
178ac27a0ecSDave Kleikamp  * bitmaps. Otherwise a newly created file might use
179ac27a0ecSDave Kleikamp  * the same inode number (not actually the same pointer
180ac27a0ecSDave Kleikamp  * though), and then we'd have two inodes sharing the
181ac27a0ecSDave Kleikamp  * same inode number and space on the harddisk.
182ac27a0ecSDave Kleikamp  */
183617ba13bSMingming Cao void ext4_free_inode(handle_t *handle, struct inode *inode)
184ac27a0ecSDave Kleikamp {
185ac27a0ecSDave Kleikamp 	struct super_block *sb = inode->i_sb;
186ac27a0ecSDave Kleikamp 	int is_directory;
187ac27a0ecSDave Kleikamp 	unsigned long ino;
188ac27a0ecSDave Kleikamp 	struct buffer_head *bitmap_bh = NULL;
189ac27a0ecSDave Kleikamp 	struct buffer_head *bh2;
190fd2d4291SAvantika Mathur 	ext4_group_t block_group;
191ac27a0ecSDave Kleikamp 	unsigned long bit;
192617ba13bSMingming Cao 	struct ext4_group_desc *gdp;
193617ba13bSMingming Cao 	struct ext4_super_block *es;
194617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
1957ce9d5d1SEric Sandeen 	int fatal = 0, err, count, cleared;
196ac27a0ecSDave Kleikamp 
197ac27a0ecSDave Kleikamp 	if (atomic_read(&inode->i_count) > 1) {
1984776004fSTheodore Ts'o 		printk(KERN_ERR "ext4_free_inode: inode has count=%d\n",
199ac27a0ecSDave Kleikamp 		       atomic_read(&inode->i_count));
200ac27a0ecSDave Kleikamp 		return;
201ac27a0ecSDave Kleikamp 	}
202ac27a0ecSDave Kleikamp 	if (inode->i_nlink) {
2034776004fSTheodore Ts'o 		printk(KERN_ERR "ext4_free_inode: inode has nlink=%d\n",
204ac27a0ecSDave Kleikamp 		       inode->i_nlink);
205ac27a0ecSDave Kleikamp 		return;
206ac27a0ecSDave Kleikamp 	}
207ac27a0ecSDave Kleikamp 	if (!sb) {
2084776004fSTheodore Ts'o 		printk(KERN_ERR "ext4_free_inode: inode on "
2094776004fSTheodore Ts'o 		       "nonexistent device\n");
210ac27a0ecSDave Kleikamp 		return;
211ac27a0ecSDave Kleikamp 	}
212617ba13bSMingming Cao 	sbi = EXT4_SB(sb);
213ac27a0ecSDave Kleikamp 
214ac27a0ecSDave Kleikamp 	ino = inode->i_ino;
215617ba13bSMingming Cao 	ext4_debug("freeing inode %lu\n", ino);
2169bffad1eSTheodore Ts'o 	trace_ext4_free_inode(inode);
217ac27a0ecSDave Kleikamp 
218ac27a0ecSDave Kleikamp 	/*
219ac27a0ecSDave Kleikamp 	 * Note: we must free any quota before locking the superblock,
220ac27a0ecSDave Kleikamp 	 * as writing the quota to disk may need the lock as well.
221ac27a0ecSDave Kleikamp 	 */
222871a2931SChristoph Hellwig 	dquot_initialize(inode);
223617ba13bSMingming Cao 	ext4_xattr_delete_inode(handle, inode);
22463936ddaSChristoph Hellwig 	dquot_free_inode(inode);
2259f754758SChristoph Hellwig 	dquot_drop(inode);
226ac27a0ecSDave Kleikamp 
227ac27a0ecSDave Kleikamp 	is_directory = S_ISDIR(inode->i_mode);
228ac27a0ecSDave Kleikamp 
229ac27a0ecSDave Kleikamp 	/* Do this BEFORE marking the inode not in use or returning an error */
2300930fcc1SAl Viro 	ext4_clear_inode(inode);
231ac27a0ecSDave Kleikamp 
232617ba13bSMingming Cao 	es = EXT4_SB(sb)->s_es;
233617ba13bSMingming Cao 	if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
23412062dddSEric Sandeen 		ext4_error(sb, "reserved or nonexistent inode %lu", ino);
235ac27a0ecSDave Kleikamp 		goto error_return;
236ac27a0ecSDave Kleikamp 	}
237617ba13bSMingming Cao 	block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
238617ba13bSMingming Cao 	bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
239e29d1cdeSEric Sandeen 	bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
240ac27a0ecSDave Kleikamp 	if (!bitmap_bh)
241ac27a0ecSDave Kleikamp 		goto error_return;
242ac27a0ecSDave Kleikamp 
243ac27a0ecSDave Kleikamp 	BUFFER_TRACE(bitmap_bh, "get_write_access");
244617ba13bSMingming Cao 	fatal = ext4_journal_get_write_access(handle, bitmap_bh);
245ac27a0ecSDave Kleikamp 	if (fatal)
246ac27a0ecSDave Kleikamp 		goto error_return;
247ac27a0ecSDave Kleikamp 
248d17413c0SDmitry Monakhov 	fatal = -ESRCH;
249617ba13bSMingming Cao 	gdp = ext4_get_group_desc(sb, block_group, &bh2);
250d17413c0SDmitry Monakhov 	if (gdp) {
251ac27a0ecSDave Kleikamp 		BUFFER_TRACE(bh2, "get_write_access");
252617ba13bSMingming Cao 		fatal = ext4_journal_get_write_access(handle, bh2);
253d17413c0SDmitry Monakhov 	}
254955ce5f5SAneesh Kumar K.V 	ext4_lock_group(sb, block_group);
255597d508cSAkinobu Mita 	cleared = ext4_test_and_clear_bit(bit, bitmap_bh->b_data);
256d17413c0SDmitry Monakhov 	if (fatal || !cleared) {
257d17413c0SDmitry Monakhov 		ext4_unlock_group(sb, block_group);
258d17413c0SDmitry Monakhov 		goto out;
259d17413c0SDmitry Monakhov 	}
260d17413c0SDmitry Monakhov 
261560671a0SAneesh Kumar K.V 	count = ext4_free_inodes_count(sb, gdp) + 1;
262560671a0SAneesh Kumar K.V 	ext4_free_inodes_set(sb, gdp, count);
263560671a0SAneesh Kumar K.V 	if (is_directory) {
264560671a0SAneesh Kumar K.V 		count = ext4_used_dirs_count(sb, gdp) - 1;
265560671a0SAneesh Kumar K.V 		ext4_used_dirs_set(sb, gdp, count);
266d17413c0SDmitry Monakhov 		percpu_counter_dec(&sbi->s_dirs_counter);
267d17413c0SDmitry Monakhov 	}
268d17413c0SDmitry Monakhov 	gdp->bg_checksum = ext4_group_desc_csum(sbi, block_group, gdp);
269d17413c0SDmitry Monakhov 	ext4_unlock_group(sb, block_group);
2707d39db14STheodore Ts'o 
271d17413c0SDmitry Monakhov 	percpu_counter_inc(&sbi->s_freeinodes_counter);
272d17413c0SDmitry Monakhov 	if (sbi->s_log_groups_per_flex) {
273d17413c0SDmitry Monakhov 		ext4_group_t f = ext4_flex_group(sbi, block_group);
274d17413c0SDmitry Monakhov 
275d17413c0SDmitry Monakhov 		atomic_inc(&sbi->s_flex_groups[f].free_inodes);
276d17413c0SDmitry Monakhov 		if (is_directory)
277c4caae25SEric Sandeen 			atomic_dec(&sbi->s_flex_groups[f].used_dirs);
2787d39db14STheodore Ts'o 	}
2790390131bSFrank Mayhar 	BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata");
280d17413c0SDmitry Monakhov 	fatal = ext4_handle_dirty_metadata(handle, NULL, bh2);
281d17413c0SDmitry Monakhov out:
282d17413c0SDmitry Monakhov 	if (cleared) {
2830390131bSFrank Mayhar 		BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata");
2840390131bSFrank Mayhar 		err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
285ac27a0ecSDave Kleikamp 		if (!fatal)
286ac27a0ecSDave Kleikamp 			fatal = err;
287a0375156STheodore Ts'o 		ext4_mark_super_dirty(sb);
288d17413c0SDmitry Monakhov 	} else
289d17413c0SDmitry Monakhov 		ext4_error(sb, "bit already cleared for inode %lu", ino);
290d17413c0SDmitry Monakhov 
291ac27a0ecSDave Kleikamp error_return:
292ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
293617ba13bSMingming Cao 	ext4_std_error(sb, fatal);
294ac27a0ecSDave Kleikamp }
295ac27a0ecSDave Kleikamp 
296a4912123STheodore Ts'o struct orlov_stats {
297a4912123STheodore Ts'o 	__u32 free_inodes;
29824aaa8efSTheodore Ts'o 	__u32 free_clusters;
299a4912123STheodore Ts'o 	__u32 used_dirs;
300a4912123STheodore Ts'o };
301a4912123STheodore Ts'o 
302a4912123STheodore Ts'o /*
303a4912123STheodore Ts'o  * Helper function for Orlov's allocator; returns critical information
304a4912123STheodore Ts'o  * for a particular block group or flex_bg.  If flex_size is 1, then g
305a4912123STheodore Ts'o  * is a block group number; otherwise it is flex_bg number.
306a4912123STheodore Ts'o  */
3071f109d5aSTheodore Ts'o static void get_orlov_stats(struct super_block *sb, ext4_group_t g,
308a4912123STheodore Ts'o 			    int flex_size, struct orlov_stats *stats)
309a4912123STheodore Ts'o {
310a4912123STheodore Ts'o 	struct ext4_group_desc *desc;
3117d39db14STheodore Ts'o 	struct flex_groups *flex_group = EXT4_SB(sb)->s_flex_groups;
312a4912123STheodore Ts'o 
3137d39db14STheodore Ts'o 	if (flex_size > 1) {
3147d39db14STheodore Ts'o 		stats->free_inodes = atomic_read(&flex_group[g].free_inodes);
31524aaa8efSTheodore Ts'o 		stats->free_clusters = atomic_read(&flex_group[g].free_clusters);
3167d39db14STheodore Ts'o 		stats->used_dirs = atomic_read(&flex_group[g].used_dirs);
3177d39db14STheodore Ts'o 		return;
3187d39db14STheodore Ts'o 	}
3197d39db14STheodore Ts'o 
3207d39db14STheodore Ts'o 	desc = ext4_get_group_desc(sb, g, NULL);
3217d39db14STheodore Ts'o 	if (desc) {
3227d39db14STheodore Ts'o 		stats->free_inodes = ext4_free_inodes_count(sb, desc);
323021b65bbSTheodore Ts'o 		stats->free_clusters = ext4_free_group_clusters(sb, desc);
3247d39db14STheodore Ts'o 		stats->used_dirs = ext4_used_dirs_count(sb, desc);
3257d39db14STheodore Ts'o 	} else {
326a4912123STheodore Ts'o 		stats->free_inodes = 0;
32724aaa8efSTheodore Ts'o 		stats->free_clusters = 0;
328a4912123STheodore Ts'o 		stats->used_dirs = 0;
329a4912123STheodore Ts'o 	}
330a4912123STheodore Ts'o }
331a4912123STheodore Ts'o 
332ac27a0ecSDave Kleikamp /*
333ac27a0ecSDave Kleikamp  * Orlov's allocator for directories.
334ac27a0ecSDave Kleikamp  *
335ac27a0ecSDave Kleikamp  * We always try to spread first-level directories.
336ac27a0ecSDave Kleikamp  *
337ac27a0ecSDave Kleikamp  * If there are blockgroups with both free inodes and free blocks counts
338ac27a0ecSDave Kleikamp  * not worse than average we return one with smallest directory count.
339ac27a0ecSDave Kleikamp  * Otherwise we simply return a random group.
340ac27a0ecSDave Kleikamp  *
341ac27a0ecSDave Kleikamp  * For the rest rules look so:
342ac27a0ecSDave Kleikamp  *
343ac27a0ecSDave Kleikamp  * It's OK to put directory into a group unless
344ac27a0ecSDave Kleikamp  * it has too many directories already (max_dirs) or
345ac27a0ecSDave Kleikamp  * it has too few free inodes left (min_inodes) or
346ac27a0ecSDave Kleikamp  * it has too few free blocks left (min_blocks) or
3471cc8dcf5SBenoit Boissinot  * Parent's group is preferred, if it doesn't satisfy these
348ac27a0ecSDave Kleikamp  * conditions we search cyclically through the rest. If none
349ac27a0ecSDave Kleikamp  * of the groups look good we just look for a group with more
350ac27a0ecSDave Kleikamp  * free inodes than average (starting at parent's group).
351ac27a0ecSDave Kleikamp  */
352ac27a0ecSDave Kleikamp 
3532aa9fc4cSAvantika Mathur static int find_group_orlov(struct super_block *sb, struct inode *parent,
354dcca3fecSAl Viro 			    ext4_group_t *group, umode_t mode,
355f157a4aaSTheodore Ts'o 			    const struct qstr *qstr)
356ac27a0ecSDave Kleikamp {
357fd2d4291SAvantika Mathur 	ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
358617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3598df9675fSTheodore Ts'o 	ext4_group_t real_ngroups = ext4_get_groups_count(sb);
360617ba13bSMingming Cao 	int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
36114c83c9fSTheodore Ts'o 	unsigned int freei, avefreei, grp_free;
36224aaa8efSTheodore Ts'o 	ext4_fsblk_t freeb, avefreec;
363ac27a0ecSDave Kleikamp 	unsigned int ndirs;
364a4912123STheodore Ts'o 	int max_dirs, min_inodes;
36524aaa8efSTheodore Ts'o 	ext4_grpblk_t min_clusters;
3668df9675fSTheodore Ts'o 	ext4_group_t i, grp, g, ngroups;
367617ba13bSMingming Cao 	struct ext4_group_desc *desc;
368a4912123STheodore Ts'o 	struct orlov_stats stats;
369a4912123STheodore Ts'o 	int flex_size = ext4_flex_bg_size(sbi);
370f157a4aaSTheodore Ts'o 	struct dx_hash_info hinfo;
371a4912123STheodore Ts'o 
3728df9675fSTheodore Ts'o 	ngroups = real_ngroups;
373a4912123STheodore Ts'o 	if (flex_size > 1) {
3748df9675fSTheodore Ts'o 		ngroups = (real_ngroups + flex_size - 1) >>
375a4912123STheodore Ts'o 			sbi->s_log_groups_per_flex;
376a4912123STheodore Ts'o 		parent_group >>= sbi->s_log_groups_per_flex;
377a4912123STheodore Ts'o 	}
378ac27a0ecSDave Kleikamp 
379ac27a0ecSDave Kleikamp 	freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
380ac27a0ecSDave Kleikamp 	avefreei = freei / ngroups;
38157042651STheodore Ts'o 	freeb = EXT4_C2B(sbi,
38257042651STheodore Ts'o 		percpu_counter_read_positive(&sbi->s_freeclusters_counter));
38324aaa8efSTheodore Ts'o 	avefreec = freeb;
38424aaa8efSTheodore Ts'o 	do_div(avefreec, ngroups);
385ac27a0ecSDave Kleikamp 	ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
386ac27a0ecSDave Kleikamp 
387a4912123STheodore Ts'o 	if (S_ISDIR(mode) &&
388a4912123STheodore Ts'o 	    ((parent == sb->s_root->d_inode) ||
38912e9b892SDmitry Monakhov 	     (ext4_test_inode_flag(parent, EXT4_INODE_TOPDIR)))) {
390ac27a0ecSDave Kleikamp 		int best_ndir = inodes_per_group;
3912aa9fc4cSAvantika Mathur 		int ret = -1;
392ac27a0ecSDave Kleikamp 
393f157a4aaSTheodore Ts'o 		if (qstr) {
394f157a4aaSTheodore Ts'o 			hinfo.hash_version = DX_HASH_HALF_MD4;
395f157a4aaSTheodore Ts'o 			hinfo.seed = sbi->s_hash_seed;
396f157a4aaSTheodore Ts'o 			ext4fs_dirhash(qstr->name, qstr->len, &hinfo);
397f157a4aaSTheodore Ts'o 			grp = hinfo.hash;
398f157a4aaSTheodore Ts'o 		} else
3992aa9fc4cSAvantika Mathur 			get_random_bytes(&grp, sizeof(grp));
4002aa9fc4cSAvantika Mathur 		parent_group = (unsigned)grp % ngroups;
401ac27a0ecSDave Kleikamp 		for (i = 0; i < ngroups; i++) {
402a4912123STheodore Ts'o 			g = (parent_group + i) % ngroups;
403a4912123STheodore Ts'o 			get_orlov_stats(sb, g, flex_size, &stats);
404a4912123STheodore Ts'o 			if (!stats.free_inodes)
405ac27a0ecSDave Kleikamp 				continue;
406a4912123STheodore Ts'o 			if (stats.used_dirs >= best_ndir)
407ac27a0ecSDave Kleikamp 				continue;
408a4912123STheodore Ts'o 			if (stats.free_inodes < avefreei)
409ac27a0ecSDave Kleikamp 				continue;
41024aaa8efSTheodore Ts'o 			if (stats.free_clusters < avefreec)
411ac27a0ecSDave Kleikamp 				continue;
412a4912123STheodore Ts'o 			grp = g;
4132aa9fc4cSAvantika Mathur 			ret = 0;
414a4912123STheodore Ts'o 			best_ndir = stats.used_dirs;
415ac27a0ecSDave Kleikamp 		}
416a4912123STheodore Ts'o 		if (ret)
417a4912123STheodore Ts'o 			goto fallback;
418a4912123STheodore Ts'o 	found_flex_bg:
419a4912123STheodore Ts'o 		if (flex_size == 1) {
420a4912123STheodore Ts'o 			*group = grp;
421a4912123STheodore Ts'o 			return 0;
422a4912123STheodore Ts'o 		}
423a4912123STheodore Ts'o 
424a4912123STheodore Ts'o 		/*
425a4912123STheodore Ts'o 		 * We pack inodes at the beginning of the flexgroup's
426a4912123STheodore Ts'o 		 * inode tables.  Block allocation decisions will do
427a4912123STheodore Ts'o 		 * something similar, although regular files will
428a4912123STheodore Ts'o 		 * start at 2nd block group of the flexgroup.  See
429a4912123STheodore Ts'o 		 * ext4_ext_find_goal() and ext4_find_near().
430a4912123STheodore Ts'o 		 */
431a4912123STheodore Ts'o 		grp *= flex_size;
432a4912123STheodore Ts'o 		for (i = 0; i < flex_size; i++) {
4338df9675fSTheodore Ts'o 			if (grp+i >= real_ngroups)
434a4912123STheodore Ts'o 				break;
435a4912123STheodore Ts'o 			desc = ext4_get_group_desc(sb, grp+i, NULL);
436a4912123STheodore Ts'o 			if (desc && ext4_free_inodes_count(sb, desc)) {
437a4912123STheodore Ts'o 				*group = grp+i;
438a4912123STheodore Ts'o 				return 0;
439a4912123STheodore Ts'o 			}
440a4912123STheodore Ts'o 		}
441ac27a0ecSDave Kleikamp 		goto fallback;
442ac27a0ecSDave Kleikamp 	}
443ac27a0ecSDave Kleikamp 
444ac27a0ecSDave Kleikamp 	max_dirs = ndirs / ngroups + inodes_per_group / 16;
445a4912123STheodore Ts'o 	min_inodes = avefreei - inodes_per_group*flex_size / 4;
446a4912123STheodore Ts'o 	if (min_inodes < 1)
447a4912123STheodore Ts'o 		min_inodes = 1;
44824aaa8efSTheodore Ts'o 	min_clusters = avefreec - EXT4_CLUSTERS_PER_GROUP(sb)*flex_size / 4;
449ac27a0ecSDave Kleikamp 
450a4912123STheodore Ts'o 	/*
451a4912123STheodore Ts'o 	 * Start looking in the flex group where we last allocated an
452a4912123STheodore Ts'o 	 * inode for this parent directory
453a4912123STheodore Ts'o 	 */
454a4912123STheodore Ts'o 	if (EXT4_I(parent)->i_last_alloc_group != ~0) {
455a4912123STheodore Ts'o 		parent_group = EXT4_I(parent)->i_last_alloc_group;
456a4912123STheodore Ts'o 		if (flex_size > 1)
457a4912123STheodore Ts'o 			parent_group >>= sbi->s_log_groups_per_flex;
458a4912123STheodore Ts'o 	}
459ac27a0ecSDave Kleikamp 
460ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
461a4912123STheodore Ts'o 		grp = (parent_group + i) % ngroups;
462a4912123STheodore Ts'o 		get_orlov_stats(sb, grp, flex_size, &stats);
463a4912123STheodore Ts'o 		if (stats.used_dirs >= max_dirs)
464ac27a0ecSDave Kleikamp 			continue;
465a4912123STheodore Ts'o 		if (stats.free_inodes < min_inodes)
466ac27a0ecSDave Kleikamp 			continue;
46724aaa8efSTheodore Ts'o 		if (stats.free_clusters < min_clusters)
468ac27a0ecSDave Kleikamp 			continue;
469a4912123STheodore Ts'o 		goto found_flex_bg;
470ac27a0ecSDave Kleikamp 	}
471ac27a0ecSDave Kleikamp 
472ac27a0ecSDave Kleikamp fallback:
4738df9675fSTheodore Ts'o 	ngroups = real_ngroups;
474a4912123STheodore Ts'o 	avefreei = freei / ngroups;
475b5451f7bSTheodore Ts'o fallback_retry:
476a4912123STheodore Ts'o 	parent_group = EXT4_I(parent)->i_block_group;
477ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
478a4912123STheodore Ts'o 		grp = (parent_group + i) % ngroups;
479a4912123STheodore Ts'o 		desc = ext4_get_group_desc(sb, grp, NULL);
48014c83c9fSTheodore Ts'o 		grp_free = ext4_free_inodes_count(sb, desc);
48114c83c9fSTheodore Ts'o 		if (desc && grp_free && grp_free >= avefreei) {
482a4912123STheodore Ts'o 			*group = grp;
4832aa9fc4cSAvantika Mathur 			return 0;
484ac27a0ecSDave Kleikamp 		}
485a4912123STheodore Ts'o 	}
486ac27a0ecSDave Kleikamp 
487ac27a0ecSDave Kleikamp 	if (avefreei) {
488ac27a0ecSDave Kleikamp 		/*
489ac27a0ecSDave Kleikamp 		 * The free-inodes counter is approximate, and for really small
490ac27a0ecSDave Kleikamp 		 * filesystems the above test can fail to find any blockgroups
491ac27a0ecSDave Kleikamp 		 */
492ac27a0ecSDave Kleikamp 		avefreei = 0;
493b5451f7bSTheodore Ts'o 		goto fallback_retry;
494ac27a0ecSDave Kleikamp 	}
495ac27a0ecSDave Kleikamp 
496ac27a0ecSDave Kleikamp 	return -1;
497ac27a0ecSDave Kleikamp }
498ac27a0ecSDave Kleikamp 
4992aa9fc4cSAvantika Mathur static int find_group_other(struct super_block *sb, struct inode *parent,
500dcca3fecSAl Viro 			    ext4_group_t *group, umode_t mode)
501ac27a0ecSDave Kleikamp {
502fd2d4291SAvantika Mathur 	ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
5038df9675fSTheodore Ts'o 	ext4_group_t i, last, ngroups = ext4_get_groups_count(sb);
504617ba13bSMingming Cao 	struct ext4_group_desc *desc;
505a4912123STheodore Ts'o 	int flex_size = ext4_flex_bg_size(EXT4_SB(sb));
506a4912123STheodore Ts'o 
507a4912123STheodore Ts'o 	/*
508a4912123STheodore Ts'o 	 * Try to place the inode is the same flex group as its
509a4912123STheodore Ts'o 	 * parent.  If we can't find space, use the Orlov algorithm to
510a4912123STheodore Ts'o 	 * find another flex group, and store that information in the
511a4912123STheodore Ts'o 	 * parent directory's inode information so that use that flex
512a4912123STheodore Ts'o 	 * group for future allocations.
513a4912123STheodore Ts'o 	 */
514a4912123STheodore Ts'o 	if (flex_size > 1) {
515a4912123STheodore Ts'o 		int retry = 0;
516a4912123STheodore Ts'o 
517a4912123STheodore Ts'o 	try_again:
518a4912123STheodore Ts'o 		parent_group &= ~(flex_size-1);
519a4912123STheodore Ts'o 		last = parent_group + flex_size;
520a4912123STheodore Ts'o 		if (last > ngroups)
521a4912123STheodore Ts'o 			last = ngroups;
522a4912123STheodore Ts'o 		for  (i = parent_group; i < last; i++) {
523a4912123STheodore Ts'o 			desc = ext4_get_group_desc(sb, i, NULL);
524a4912123STheodore Ts'o 			if (desc && ext4_free_inodes_count(sb, desc)) {
525a4912123STheodore Ts'o 				*group = i;
526a4912123STheodore Ts'o 				return 0;
527a4912123STheodore Ts'o 			}
528a4912123STheodore Ts'o 		}
529a4912123STheodore Ts'o 		if (!retry && EXT4_I(parent)->i_last_alloc_group != ~0) {
530a4912123STheodore Ts'o 			retry = 1;
531a4912123STheodore Ts'o 			parent_group = EXT4_I(parent)->i_last_alloc_group;
532a4912123STheodore Ts'o 			goto try_again;
533a4912123STheodore Ts'o 		}
534a4912123STheodore Ts'o 		/*
535a4912123STheodore Ts'o 		 * If this didn't work, use the Orlov search algorithm
536a4912123STheodore Ts'o 		 * to find a new flex group; we pass in the mode to
537a4912123STheodore Ts'o 		 * avoid the topdir algorithms.
538a4912123STheodore Ts'o 		 */
539a4912123STheodore Ts'o 		*group = parent_group + flex_size;
540a4912123STheodore Ts'o 		if (*group > ngroups)
541a4912123STheodore Ts'o 			*group = 0;
5427dc57615SPeter Huewe 		return find_group_orlov(sb, parent, group, mode, NULL);
543a4912123STheodore Ts'o 	}
544ac27a0ecSDave Kleikamp 
545ac27a0ecSDave Kleikamp 	/*
546ac27a0ecSDave Kleikamp 	 * Try to place the inode in its parent directory
547ac27a0ecSDave Kleikamp 	 */
5482aa9fc4cSAvantika Mathur 	*group = parent_group;
5492aa9fc4cSAvantika Mathur 	desc = ext4_get_group_desc(sb, *group, NULL);
550560671a0SAneesh Kumar K.V 	if (desc && ext4_free_inodes_count(sb, desc) &&
551021b65bbSTheodore Ts'o 	    ext4_free_group_clusters(sb, desc))
5522aa9fc4cSAvantika Mathur 		return 0;
553ac27a0ecSDave Kleikamp 
554ac27a0ecSDave Kleikamp 	/*
555ac27a0ecSDave Kleikamp 	 * We're going to place this inode in a different blockgroup from its
556ac27a0ecSDave Kleikamp 	 * parent.  We want to cause files in a common directory to all land in
557ac27a0ecSDave Kleikamp 	 * the same blockgroup.  But we want files which are in a different
558ac27a0ecSDave Kleikamp 	 * directory which shares a blockgroup with our parent to land in a
559ac27a0ecSDave Kleikamp 	 * different blockgroup.
560ac27a0ecSDave Kleikamp 	 *
561ac27a0ecSDave Kleikamp 	 * So add our directory's i_ino into the starting point for the hash.
562ac27a0ecSDave Kleikamp 	 */
5632aa9fc4cSAvantika Mathur 	*group = (*group + parent->i_ino) % ngroups;
564ac27a0ecSDave Kleikamp 
565ac27a0ecSDave Kleikamp 	/*
566ac27a0ecSDave Kleikamp 	 * Use a quadratic hash to find a group with a free inode and some free
567ac27a0ecSDave Kleikamp 	 * blocks.
568ac27a0ecSDave Kleikamp 	 */
569ac27a0ecSDave Kleikamp 	for (i = 1; i < ngroups; i <<= 1) {
5702aa9fc4cSAvantika Mathur 		*group += i;
5712aa9fc4cSAvantika Mathur 		if (*group >= ngroups)
5722aa9fc4cSAvantika Mathur 			*group -= ngroups;
5732aa9fc4cSAvantika Mathur 		desc = ext4_get_group_desc(sb, *group, NULL);
574560671a0SAneesh Kumar K.V 		if (desc && ext4_free_inodes_count(sb, desc) &&
575021b65bbSTheodore Ts'o 		    ext4_free_group_clusters(sb, desc))
5762aa9fc4cSAvantika Mathur 			return 0;
577ac27a0ecSDave Kleikamp 	}
578ac27a0ecSDave Kleikamp 
579ac27a0ecSDave Kleikamp 	/*
580ac27a0ecSDave Kleikamp 	 * That failed: try linear search for a free inode, even if that group
581ac27a0ecSDave Kleikamp 	 * has no free blocks.
582ac27a0ecSDave Kleikamp 	 */
5832aa9fc4cSAvantika Mathur 	*group = parent_group;
584ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
5852aa9fc4cSAvantika Mathur 		if (++*group >= ngroups)
5862aa9fc4cSAvantika Mathur 			*group = 0;
5872aa9fc4cSAvantika Mathur 		desc = ext4_get_group_desc(sb, *group, NULL);
588560671a0SAneesh Kumar K.V 		if (desc && ext4_free_inodes_count(sb, desc))
5892aa9fc4cSAvantika Mathur 			return 0;
590ac27a0ecSDave Kleikamp 	}
591ac27a0ecSDave Kleikamp 
592ac27a0ecSDave Kleikamp 	return -1;
593ac27a0ecSDave Kleikamp }
594ac27a0ecSDave Kleikamp 
595ac27a0ecSDave Kleikamp /*
596ac27a0ecSDave Kleikamp  * There are two policies for allocating an inode.  If the new inode is
597ac27a0ecSDave Kleikamp  * a directory, then a forward search is made for a block group with both
598ac27a0ecSDave Kleikamp  * free space and a low directory-to-inode ratio; if that fails, then of
599ac27a0ecSDave Kleikamp  * the groups with above-average free space, that group with the fewest
600ac27a0ecSDave Kleikamp  * directories already is chosen.
601ac27a0ecSDave Kleikamp  *
602ac27a0ecSDave Kleikamp  * For other inodes, search forward from the parent directory's block
603ac27a0ecSDave Kleikamp  * group to find a free inode.
604ac27a0ecSDave Kleikamp  */
605dcca3fecSAl Viro struct inode *ext4_new_inode(handle_t *handle, struct inode *dir, umode_t mode,
6065cb81dabSDmitry Monakhov 			     const struct qstr *qstr, __u32 goal, uid_t *owner)
607ac27a0ecSDave Kleikamp {
608ac27a0ecSDave Kleikamp 	struct super_block *sb;
6093300bedaSAneesh Kumar K.V 	struct buffer_head *inode_bitmap_bh = NULL;
6103300bedaSAneesh Kumar K.V 	struct buffer_head *group_desc_bh;
6118df9675fSTheodore Ts'o 	ext4_group_t ngroups, group = 0;
612ac27a0ecSDave Kleikamp 	unsigned long ino = 0;
613ac27a0ecSDave Kleikamp 	struct inode *inode;
614617ba13bSMingming Cao 	struct ext4_group_desc *gdp = NULL;
615617ba13bSMingming Cao 	struct ext4_inode_info *ei;
616617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
61739341867SAneesh Kumar K.V 	int ret2, err = 0;
618ac27a0ecSDave Kleikamp 	struct inode *ret;
6192aa9fc4cSAvantika Mathur 	ext4_group_t i;
620772cb7c8SJose R. Santos 	ext4_group_t flex_group;
621ac27a0ecSDave Kleikamp 
622ac27a0ecSDave Kleikamp 	/* Cannot create files in a deleted directory */
623ac27a0ecSDave Kleikamp 	if (!dir || !dir->i_nlink)
624ac27a0ecSDave Kleikamp 		return ERR_PTR(-EPERM);
625ac27a0ecSDave Kleikamp 
626ac27a0ecSDave Kleikamp 	sb = dir->i_sb;
6278df9675fSTheodore Ts'o 	ngroups = ext4_get_groups_count(sb);
6289bffad1eSTheodore Ts'o 	trace_ext4_request_inode(dir, mode);
629ac27a0ecSDave Kleikamp 	inode = new_inode(sb);
630ac27a0ecSDave Kleikamp 	if (!inode)
631ac27a0ecSDave Kleikamp 		return ERR_PTR(-ENOMEM);
632617ba13bSMingming Cao 	ei = EXT4_I(inode);
633617ba13bSMingming Cao 	sbi = EXT4_SB(sb);
634772cb7c8SJose R. Santos 
63511013911SAndreas Dilger 	if (!goal)
63611013911SAndreas Dilger 		goal = sbi->s_inode_goal;
63711013911SAndreas Dilger 
638e6462869SJohann Lombardi 	if (goal && goal <= le32_to_cpu(sbi->s_es->s_inodes_count)) {
63911013911SAndreas Dilger 		group = (goal - 1) / EXT4_INODES_PER_GROUP(sb);
64011013911SAndreas Dilger 		ino = (goal - 1) % EXT4_INODES_PER_GROUP(sb);
64111013911SAndreas Dilger 		ret2 = 0;
64211013911SAndreas Dilger 		goto got_group;
64311013911SAndreas Dilger 	}
64411013911SAndreas Dilger 
6454113c4caSLukas Czerner 	if (S_ISDIR(mode))
646f157a4aaSTheodore Ts'o 		ret2 = find_group_orlov(sb, dir, &group, mode, qstr);
6474113c4caSLukas Czerner 	else
648a4912123STheodore Ts'o 		ret2 = find_group_other(sb, dir, &group, mode);
649ac27a0ecSDave Kleikamp 
650772cb7c8SJose R. Santos got_group:
651a4912123STheodore Ts'o 	EXT4_I(dir)->i_last_alloc_group = group;
652ac27a0ecSDave Kleikamp 	err = -ENOSPC;
6532aa9fc4cSAvantika Mathur 	if (ret2 == -1)
654ac27a0ecSDave Kleikamp 		goto out;
655ac27a0ecSDave Kleikamp 
656*119c0d44STheodore Ts'o 	/*
657*119c0d44STheodore Ts'o 	 * Normally we will only go through one pass of this loop,
658*119c0d44STheodore Ts'o 	 * unless we get unlucky and it turns out the group we selected
659*119c0d44STheodore Ts'o 	 * had its last inode grabbed by someone else.
660*119c0d44STheodore Ts'o 	 */
66111013911SAndreas Dilger 	for (i = 0; i < ngroups; i++, ino = 0) {
662ac27a0ecSDave Kleikamp 		err = -EIO;
663ac27a0ecSDave Kleikamp 
6643300bedaSAneesh Kumar K.V 		gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
665ac27a0ecSDave Kleikamp 		if (!gdp)
666ac27a0ecSDave Kleikamp 			goto fail;
667ac27a0ecSDave Kleikamp 
6683300bedaSAneesh Kumar K.V 		brelse(inode_bitmap_bh);
6693300bedaSAneesh Kumar K.V 		inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
6703300bedaSAneesh Kumar K.V 		if (!inode_bitmap_bh)
671ac27a0ecSDave Kleikamp 			goto fail;
672ac27a0ecSDave Kleikamp 
673ac27a0ecSDave Kleikamp repeat_in_this_group:
674617ba13bSMingming Cao 		ino = ext4_find_next_zero_bit((unsigned long *)
6753300bedaSAneesh Kumar K.V 					      inode_bitmap_bh->b_data,
6763300bedaSAneesh Kumar K.V 					      EXT4_INODES_PER_GROUP(sb), ino);
677*119c0d44STheodore Ts'o 		if (ino >= EXT4_INODES_PER_GROUP(sb)) {
6788df9675fSTheodore Ts'o 			if (++group == ngroups)
679ac27a0ecSDave Kleikamp 				group = 0;
680*119c0d44STheodore Ts'o 			continue;
681*119c0d44STheodore Ts'o 		}
682*119c0d44STheodore Ts'o 		if (group == 0 && (ino+1) < EXT4_FIRST_INO(sb)) {
683*119c0d44STheodore Ts'o 			ext4_error(sb, "reserved inode found cleared - "
684*119c0d44STheodore Ts'o 				   "inode=%lu", ino + 1);
685*119c0d44STheodore Ts'o 			continue;
686*119c0d44STheodore Ts'o 		}
687*119c0d44STheodore Ts'o 		ext4_lock_group(sb, group);
688*119c0d44STheodore Ts'o 		ret2 = ext4_test_and_set_bit(ino, inode_bitmap_bh->b_data);
689*119c0d44STheodore Ts'o 		ext4_unlock_group(sb, group);
690*119c0d44STheodore Ts'o 		ino++;		/* the inode bitmap is zero-based */
691*119c0d44STheodore Ts'o 		if (!ret2)
692*119c0d44STheodore Ts'o 			goto got; /* we grabbed the inode! */
693*119c0d44STheodore Ts'o 		if (ino < EXT4_INODES_PER_GROUP(sb))
694*119c0d44STheodore Ts'o 			goto repeat_in_this_group;
695ac27a0ecSDave Kleikamp 	}
696ac27a0ecSDave Kleikamp 	err = -ENOSPC;
697ac27a0ecSDave Kleikamp 	goto out;
698ac27a0ecSDave Kleikamp 
699ac27a0ecSDave Kleikamp got:
700717d50e4SAndreas Dilger 	/* We may have to initialize the block bitmap if it isn't already */
701717d50e4SAndreas Dilger 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
702717d50e4SAndreas Dilger 	    gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
7033300bedaSAneesh Kumar K.V 		struct buffer_head *block_bitmap_bh;
704717d50e4SAndreas Dilger 
7053300bedaSAneesh Kumar K.V 		block_bitmap_bh = ext4_read_block_bitmap(sb, group);
7063300bedaSAneesh Kumar K.V 		BUFFER_TRACE(block_bitmap_bh, "get block bitmap access");
7073300bedaSAneesh Kumar K.V 		err = ext4_journal_get_write_access(handle, block_bitmap_bh);
708717d50e4SAndreas Dilger 		if (err) {
7093300bedaSAneesh Kumar K.V 			brelse(block_bitmap_bh);
710717d50e4SAndreas Dilger 			goto fail;
711717d50e4SAndreas Dilger 		}
712717d50e4SAndreas Dilger 
713fd034a84STheodore Ts'o 		BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
714fd034a84STheodore Ts'o 		err = ext4_handle_dirty_metadata(handle, NULL, block_bitmap_bh);
715fd034a84STheodore Ts'o 		brelse(block_bitmap_bh);
716fd034a84STheodore Ts'o 
717717d50e4SAndreas Dilger 		/* recheck and clear flag under lock if we still need to */
718fd034a84STheodore Ts'o 		ext4_lock_group(sb, group);
719717d50e4SAndreas Dilger 		if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
7203300bedaSAneesh Kumar K.V 			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
721021b65bbSTheodore Ts'o 			ext4_free_group_clusters_set(sb, gdp,
722cff1dfd7STheodore Ts'o 				ext4_free_clusters_after_init(sb, group, gdp));
72323712a9cSFrederic Bohe 			gdp->bg_checksum = ext4_group_desc_csum(sbi, group,
72423712a9cSFrederic Bohe 								gdp);
725717d50e4SAndreas Dilger 		}
726955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, group);
727717d50e4SAndreas Dilger 
728717d50e4SAndreas Dilger 		if (err)
729717d50e4SAndreas Dilger 			goto fail;
730717d50e4SAndreas Dilger 	}
731*119c0d44STheodore Ts'o 
732*119c0d44STheodore Ts'o 	BUFFER_TRACE(inode_bitmap_bh, "get_write_access");
733*119c0d44STheodore Ts'o 	err = ext4_journal_get_write_access(handle, inode_bitmap_bh);
734*119c0d44STheodore Ts'o 	if (err)
735*119c0d44STheodore Ts'o 		goto fail;
736*119c0d44STheodore Ts'o 
737*119c0d44STheodore Ts'o 	BUFFER_TRACE(group_desc_bh, "get_write_access");
738*119c0d44STheodore Ts'o 	err = ext4_journal_get_write_access(handle, group_desc_bh);
739*119c0d44STheodore Ts'o 	if (err)
740*119c0d44STheodore Ts'o 		goto fail;
741*119c0d44STheodore Ts'o 
742*119c0d44STheodore Ts'o 	/* Update the relevant bg descriptor fields */
743*119c0d44STheodore Ts'o 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
744*119c0d44STheodore Ts'o 		int free;
745*119c0d44STheodore Ts'o 		struct ext4_group_info *grp = ext4_get_group_info(sb, group);
746*119c0d44STheodore Ts'o 
747*119c0d44STheodore Ts'o 		down_read(&grp->alloc_sem); /* protect vs itable lazyinit */
748*119c0d44STheodore Ts'o 		ext4_lock_group(sb, group); /* while we modify the bg desc */
749*119c0d44STheodore Ts'o 		free = EXT4_INODES_PER_GROUP(sb) -
750*119c0d44STheodore Ts'o 			ext4_itable_unused_count(sb, gdp);
751*119c0d44STheodore Ts'o 		if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
752*119c0d44STheodore Ts'o 			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
753*119c0d44STheodore Ts'o 			free = 0;
754*119c0d44STheodore Ts'o 		}
755*119c0d44STheodore Ts'o 		/*
756*119c0d44STheodore Ts'o 		 * Check the relative inode number against the last used
757*119c0d44STheodore Ts'o 		 * relative inode number in this group. if it is greater
758*119c0d44STheodore Ts'o 		 * we need to update the bg_itable_unused count
759*119c0d44STheodore Ts'o 		 */
760*119c0d44STheodore Ts'o 		if (ino > free)
761*119c0d44STheodore Ts'o 			ext4_itable_unused_set(sb, gdp,
762*119c0d44STheodore Ts'o 					(EXT4_INODES_PER_GROUP(sb) - ino));
763*119c0d44STheodore Ts'o 		up_read(&grp->alloc_sem);
764*119c0d44STheodore Ts'o 	}
765*119c0d44STheodore Ts'o 	ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1);
766*119c0d44STheodore Ts'o 	if (S_ISDIR(mode)) {
767*119c0d44STheodore Ts'o 		ext4_used_dirs_set(sb, gdp, ext4_used_dirs_count(sb, gdp) + 1);
768*119c0d44STheodore Ts'o 		if (sbi->s_log_groups_per_flex) {
769*119c0d44STheodore Ts'o 			ext4_group_t f = ext4_flex_group(sbi, group);
770*119c0d44STheodore Ts'o 
771*119c0d44STheodore Ts'o 			atomic_inc(&sbi->s_flex_groups[f].used_dirs);
772*119c0d44STheodore Ts'o 		}
773*119c0d44STheodore Ts'o 	}
774*119c0d44STheodore Ts'o 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
775*119c0d44STheodore Ts'o 		gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp);
776*119c0d44STheodore Ts'o 		ext4_unlock_group(sb, group);
777*119c0d44STheodore Ts'o 	}
778*119c0d44STheodore Ts'o 
779*119c0d44STheodore Ts'o 	BUFFER_TRACE(inode_bitmap_bh, "call ext4_handle_dirty_metadata");
780*119c0d44STheodore Ts'o 	err = ext4_handle_dirty_metadata(handle, NULL, inode_bitmap_bh);
781*119c0d44STheodore Ts'o 	if (err)
782*119c0d44STheodore Ts'o 		goto fail;
783*119c0d44STheodore Ts'o 
7843300bedaSAneesh Kumar K.V 	BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata");
7853300bedaSAneesh Kumar K.V 	err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh);
78639341867SAneesh Kumar K.V 	if (err)
78739341867SAneesh Kumar K.V 		goto fail;
788ac27a0ecSDave Kleikamp 
789ac27a0ecSDave Kleikamp 	percpu_counter_dec(&sbi->s_freeinodes_counter);
790ac27a0ecSDave Kleikamp 	if (S_ISDIR(mode))
791ac27a0ecSDave Kleikamp 		percpu_counter_inc(&sbi->s_dirs_counter);
792a0375156STheodore Ts'o 	ext4_mark_super_dirty(sb);
793ac27a0ecSDave Kleikamp 
794772cb7c8SJose R. Santos 	if (sbi->s_log_groups_per_flex) {
795772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, group);
7969f24e420STheodore Ts'o 		atomic_dec(&sbi->s_flex_groups[flex_group].free_inodes);
797772cb7c8SJose R. Santos 	}
7985cb81dabSDmitry Monakhov 	if (owner) {
7995cb81dabSDmitry Monakhov 		inode->i_mode = mode;
8005cb81dabSDmitry Monakhov 		inode->i_uid = owner[0];
8015cb81dabSDmitry Monakhov 		inode->i_gid = owner[1];
8025cb81dabSDmitry Monakhov 	} else if (test_opt(sb, GRPID)) {
803ac27a0ecSDave Kleikamp 		inode->i_mode = mode;
804b10b8520SDmitry Monakhov 		inode->i_uid = current_fsuid();
805b10b8520SDmitry Monakhov 		inode->i_gid = dir->i_gid;
806b10b8520SDmitry Monakhov 	} else
807b10b8520SDmitry Monakhov 		inode_init_owner(inode, dir, mode);
808ac27a0ecSDave Kleikamp 
809717d50e4SAndreas Dilger 	inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
810ac27a0ecSDave Kleikamp 	/* This is the optimal IO size (for stat), not the fs block size */
811ac27a0ecSDave Kleikamp 	inode->i_blocks = 0;
812ef7f3835SKalpak Shah 	inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
813ef7f3835SKalpak Shah 						       ext4_current_time(inode);
814ac27a0ecSDave Kleikamp 
815ac27a0ecSDave Kleikamp 	memset(ei->i_data, 0, sizeof(ei->i_data));
816ac27a0ecSDave Kleikamp 	ei->i_dir_start_lookup = 0;
817ac27a0ecSDave Kleikamp 	ei->i_disksize = 0;
818ac27a0ecSDave Kleikamp 
8194af83508SEryu Guan 	/* Don't inherit extent flag from directory, amongst others. */
8202dc6b0d4SDuane Griffin 	ei->i_flags =
8212dc6b0d4SDuane Griffin 		ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED);
822ac27a0ecSDave Kleikamp 	ei->i_file_acl = 0;
823ac27a0ecSDave Kleikamp 	ei->i_dtime = 0;
824ac27a0ecSDave Kleikamp 	ei->i_block_group = group;
825a4912123STheodore Ts'o 	ei->i_last_alloc_group = ~0;
826ac27a0ecSDave Kleikamp 
827617ba13bSMingming Cao 	ext4_set_inode_flags(inode);
828ac27a0ecSDave Kleikamp 	if (IS_DIRSYNC(inode))
8290390131bSFrank Mayhar 		ext4_handle_sync(handle);
8306b38e842SAl Viro 	if (insert_inode_locked(inode) < 0) {
831acd6ad83SJan Kara 		/*
832acd6ad83SJan Kara 		 * Likely a bitmap corruption causing inode to be allocated
833acd6ad83SJan Kara 		 * twice.
834acd6ad83SJan Kara 		 */
835acd6ad83SJan Kara 		err = -EIO;
836acd6ad83SJan Kara 		goto fail;
8376b38e842SAl Viro 	}
838ac27a0ecSDave Kleikamp 	spin_lock(&sbi->s_next_gen_lock);
839ac27a0ecSDave Kleikamp 	inode->i_generation = sbi->s_next_generation++;
840ac27a0ecSDave Kleikamp 	spin_unlock(&sbi->s_next_gen_lock);
841ac27a0ecSDave Kleikamp 
842353eb83cSTheodore Ts'o 	ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
84319f5fb7aSTheodore Ts'o 	ext4_set_inode_state(inode, EXT4_STATE_NEW);
844ef7f3835SKalpak Shah 
845ef7f3835SKalpak Shah 	ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize;
846ac27a0ecSDave Kleikamp 
847ac27a0ecSDave Kleikamp 	ret = inode;
848871a2931SChristoph Hellwig 	dquot_initialize(inode);
84963936ddaSChristoph Hellwig 	err = dquot_alloc_inode(inode);
85063936ddaSChristoph Hellwig 	if (err)
851ac27a0ecSDave Kleikamp 		goto fail_drop;
852ac27a0ecSDave Kleikamp 
853617ba13bSMingming Cao 	err = ext4_init_acl(handle, inode, dir);
854ac27a0ecSDave Kleikamp 	if (err)
855ac27a0ecSDave Kleikamp 		goto fail_free_drop;
856ac27a0ecSDave Kleikamp 
8572a7dba39SEric Paris 	err = ext4_init_security(handle, inode, dir, qstr);
858ac27a0ecSDave Kleikamp 	if (err)
859ac27a0ecSDave Kleikamp 		goto fail_free_drop;
860ac27a0ecSDave Kleikamp 
86183982b6fSTheodore Ts'o 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
862e4079a11SEric Sandeen 		/* set extent flag only for directory, file and normal symlink*/
863e65187e6SAneesh Kumar K.V 		if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
86412e9b892SDmitry Monakhov 			ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
865a86c6181SAlex Tomas 			ext4_ext_tree_init(handle, inode);
866a86c6181SAlex Tomas 		}
86742bf0383SAneesh Kumar K.V 	}
868ac27a0ecSDave Kleikamp 
869688f869cSTheodore Ts'o 	if (ext4_handle_valid(handle)) {
870688f869cSTheodore Ts'o 		ei->i_sync_tid = handle->h_transaction->t_tid;
871688f869cSTheodore Ts'o 		ei->i_datasync_tid = handle->h_transaction->t_tid;
872688f869cSTheodore Ts'o 	}
873688f869cSTheodore Ts'o 
8748753e88fSAneesh Kumar K.V 	err = ext4_mark_inode_dirty(handle, inode);
8758753e88fSAneesh Kumar K.V 	if (err) {
8768753e88fSAneesh Kumar K.V 		ext4_std_error(sb, err);
8778753e88fSAneesh Kumar K.V 		goto fail_free_drop;
8788753e88fSAneesh Kumar K.V 	}
8798753e88fSAneesh Kumar K.V 
880617ba13bSMingming Cao 	ext4_debug("allocating inode %lu\n", inode->i_ino);
8819bffad1eSTheodore Ts'o 	trace_ext4_allocate_inode(inode, dir, mode);
882ac27a0ecSDave Kleikamp 	goto really_out;
883ac27a0ecSDave Kleikamp fail:
884617ba13bSMingming Cao 	ext4_std_error(sb, err);
885ac27a0ecSDave Kleikamp out:
886ac27a0ecSDave Kleikamp 	iput(inode);
887ac27a0ecSDave Kleikamp 	ret = ERR_PTR(err);
888ac27a0ecSDave Kleikamp really_out:
8893300bedaSAneesh Kumar K.V 	brelse(inode_bitmap_bh);
890ac27a0ecSDave Kleikamp 	return ret;
891ac27a0ecSDave Kleikamp 
892ac27a0ecSDave Kleikamp fail_free_drop:
89363936ddaSChristoph Hellwig 	dquot_free_inode(inode);
894ac27a0ecSDave Kleikamp 
895ac27a0ecSDave Kleikamp fail_drop:
8969f754758SChristoph Hellwig 	dquot_drop(inode);
897ac27a0ecSDave Kleikamp 	inode->i_flags |= S_NOQUOTA;
8986d6b77f1SMiklos Szeredi 	clear_nlink(inode);
8996b38e842SAl Viro 	unlock_new_inode(inode);
900ac27a0ecSDave Kleikamp 	iput(inode);
9013300bedaSAneesh Kumar K.V 	brelse(inode_bitmap_bh);
902ac27a0ecSDave Kleikamp 	return ERR_PTR(err);
903ac27a0ecSDave Kleikamp }
904ac27a0ecSDave Kleikamp 
905ac27a0ecSDave Kleikamp /* Verify that we are loading a valid orphan from disk */
906617ba13bSMingming Cao struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
907ac27a0ecSDave Kleikamp {
908617ba13bSMingming Cao 	unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
909fd2d4291SAvantika Mathur 	ext4_group_t block_group;
910ac27a0ecSDave Kleikamp 	int bit;
9111d1fe1eeSDavid Howells 	struct buffer_head *bitmap_bh;
912ac27a0ecSDave Kleikamp 	struct inode *inode = NULL;
9131d1fe1eeSDavid Howells 	long err = -EIO;
914ac27a0ecSDave Kleikamp 
915ac27a0ecSDave Kleikamp 	/* Error cases - e2fsck has already cleaned up for us */
916ac27a0ecSDave Kleikamp 	if (ino > max_ino) {
91712062dddSEric Sandeen 		ext4_warning(sb, "bad orphan ino %lu!  e2fsck was run?", ino);
9181d1fe1eeSDavid Howells 		goto error;
919ac27a0ecSDave Kleikamp 	}
920ac27a0ecSDave Kleikamp 
921617ba13bSMingming Cao 	block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
922617ba13bSMingming Cao 	bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
923e29d1cdeSEric Sandeen 	bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
924ac27a0ecSDave Kleikamp 	if (!bitmap_bh) {
92512062dddSEric Sandeen 		ext4_warning(sb, "inode bitmap error for orphan %lu", ino);
9261d1fe1eeSDavid Howells 		goto error;
927ac27a0ecSDave Kleikamp 	}
928ac27a0ecSDave Kleikamp 
929ac27a0ecSDave Kleikamp 	/* Having the inode bit set should be a 100% indicator that this
930ac27a0ecSDave Kleikamp 	 * is a valid orphan (no e2fsck run on fs).  Orphans also include
931ac27a0ecSDave Kleikamp 	 * inodes that were being truncated, so we can't check i_nlink==0.
932ac27a0ecSDave Kleikamp 	 */
9331d1fe1eeSDavid Howells 	if (!ext4_test_bit(bit, bitmap_bh->b_data))
9341d1fe1eeSDavid Howells 		goto bad_orphan;
9351d1fe1eeSDavid Howells 
9361d1fe1eeSDavid Howells 	inode = ext4_iget(sb, ino);
9371d1fe1eeSDavid Howells 	if (IS_ERR(inode))
9381d1fe1eeSDavid Howells 		goto iget_failed;
9391d1fe1eeSDavid Howells 
94091ef4cafSDuane Griffin 	/*
94191ef4cafSDuane Griffin 	 * If the orphans has i_nlinks > 0 then it should be able to be
94291ef4cafSDuane Griffin 	 * truncated, otherwise it won't be removed from the orphan list
94391ef4cafSDuane Griffin 	 * during processing and an infinite loop will result.
94491ef4cafSDuane Griffin 	 */
94591ef4cafSDuane Griffin 	if (inode->i_nlink && !ext4_can_truncate(inode))
94691ef4cafSDuane Griffin 		goto bad_orphan;
94791ef4cafSDuane Griffin 
9481d1fe1eeSDavid Howells 	if (NEXT_ORPHAN(inode) > max_ino)
9491d1fe1eeSDavid Howells 		goto bad_orphan;
9501d1fe1eeSDavid Howells 	brelse(bitmap_bh);
9511d1fe1eeSDavid Howells 	return inode;
9521d1fe1eeSDavid Howells 
9531d1fe1eeSDavid Howells iget_failed:
9541d1fe1eeSDavid Howells 	err = PTR_ERR(inode);
9551d1fe1eeSDavid Howells 	inode = NULL;
9561d1fe1eeSDavid Howells bad_orphan:
95712062dddSEric Sandeen 	ext4_warning(sb, "bad orphan inode %lu!  e2fsck was run?", ino);
958617ba13bSMingming Cao 	printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n",
959ac27a0ecSDave Kleikamp 	       bit, (unsigned long long)bitmap_bh->b_blocknr,
960617ba13bSMingming Cao 	       ext4_test_bit(bit, bitmap_bh->b_data));
961ac27a0ecSDave Kleikamp 	printk(KERN_NOTICE "inode=%p\n", inode);
962ac27a0ecSDave Kleikamp 	if (inode) {
963ac27a0ecSDave Kleikamp 		printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
964ac27a0ecSDave Kleikamp 		       is_bad_inode(inode));
965ac27a0ecSDave Kleikamp 		printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
966ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
967ac27a0ecSDave Kleikamp 		printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
96891ef4cafSDuane Griffin 		printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink);
969ac27a0ecSDave Kleikamp 		/* Avoid freeing blocks if we got a bad deleted inode */
9701d1fe1eeSDavid Howells 		if (inode->i_nlink == 0)
971ac27a0ecSDave Kleikamp 			inode->i_blocks = 0;
972ac27a0ecSDave Kleikamp 		iput(inode);
973ac27a0ecSDave Kleikamp 	}
974ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
9751d1fe1eeSDavid Howells error:
9761d1fe1eeSDavid Howells 	return ERR_PTR(err);
977ac27a0ecSDave Kleikamp }
978ac27a0ecSDave Kleikamp 
979617ba13bSMingming Cao unsigned long ext4_count_free_inodes(struct super_block *sb)
980ac27a0ecSDave Kleikamp {
981ac27a0ecSDave Kleikamp 	unsigned long desc_count;
982617ba13bSMingming Cao 	struct ext4_group_desc *gdp;
9838df9675fSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
984617ba13bSMingming Cao #ifdef EXT4FS_DEBUG
985617ba13bSMingming Cao 	struct ext4_super_block *es;
986ac27a0ecSDave Kleikamp 	unsigned long bitmap_count, x;
987ac27a0ecSDave Kleikamp 	struct buffer_head *bitmap_bh = NULL;
988ac27a0ecSDave Kleikamp 
989617ba13bSMingming Cao 	es = EXT4_SB(sb)->s_es;
990ac27a0ecSDave Kleikamp 	desc_count = 0;
991ac27a0ecSDave Kleikamp 	bitmap_count = 0;
992ac27a0ecSDave Kleikamp 	gdp = NULL;
9938df9675fSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
994617ba13bSMingming Cao 		gdp = ext4_get_group_desc(sb, i, NULL);
995ac27a0ecSDave Kleikamp 		if (!gdp)
996ac27a0ecSDave Kleikamp 			continue;
997560671a0SAneesh Kumar K.V 		desc_count += ext4_free_inodes_count(sb, gdp);
998ac27a0ecSDave Kleikamp 		brelse(bitmap_bh);
999e29d1cdeSEric Sandeen 		bitmap_bh = ext4_read_inode_bitmap(sb, i);
1000ac27a0ecSDave Kleikamp 		if (!bitmap_bh)
1001ac27a0ecSDave Kleikamp 			continue;
1002ac27a0ecSDave Kleikamp 
1003617ba13bSMingming Cao 		x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8);
1004c549a95dSEric Sandeen 		printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
1005785b4b3aSPeng Tao 			(unsigned long) i, ext4_free_inodes_count(sb, gdp), x);
1006ac27a0ecSDave Kleikamp 		bitmap_count += x;
1007ac27a0ecSDave Kleikamp 	}
1008ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
10094776004fSTheodore Ts'o 	printk(KERN_DEBUG "ext4_count_free_inodes: "
10104776004fSTheodore Ts'o 	       "stored = %u, computed = %lu, %lu\n",
1011ac27a0ecSDave Kleikamp 	       le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
1012ac27a0ecSDave Kleikamp 	return desc_count;
1013ac27a0ecSDave Kleikamp #else
1014ac27a0ecSDave Kleikamp 	desc_count = 0;
10158df9675fSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
1016617ba13bSMingming Cao 		gdp = ext4_get_group_desc(sb, i, NULL);
1017ac27a0ecSDave Kleikamp 		if (!gdp)
1018ac27a0ecSDave Kleikamp 			continue;
1019560671a0SAneesh Kumar K.V 		desc_count += ext4_free_inodes_count(sb, gdp);
1020ac27a0ecSDave Kleikamp 		cond_resched();
1021ac27a0ecSDave Kleikamp 	}
1022ac27a0ecSDave Kleikamp 	return desc_count;
1023ac27a0ecSDave Kleikamp #endif
1024ac27a0ecSDave Kleikamp }
1025ac27a0ecSDave Kleikamp 
1026ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
1027617ba13bSMingming Cao unsigned long ext4_count_dirs(struct super_block * sb)
1028ac27a0ecSDave Kleikamp {
1029ac27a0ecSDave Kleikamp 	unsigned long count = 0;
10308df9675fSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
1031ac27a0ecSDave Kleikamp 
10328df9675fSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
1033617ba13bSMingming Cao 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1034ac27a0ecSDave Kleikamp 		if (!gdp)
1035ac27a0ecSDave Kleikamp 			continue;
1036560671a0SAneesh Kumar K.V 		count += ext4_used_dirs_count(sb, gdp);
1037ac27a0ecSDave Kleikamp 	}
1038ac27a0ecSDave Kleikamp 	return count;
1039ac27a0ecSDave Kleikamp }
1040bfff6873SLukas Czerner 
1041bfff6873SLukas Czerner /*
1042bfff6873SLukas Czerner  * Zeroes not yet zeroed inode table - just write zeroes through the whole
1043bfff6873SLukas Czerner  * inode table. Must be called without any spinlock held. The only place
1044bfff6873SLukas Czerner  * where it is called from on active part of filesystem is ext4lazyinit
1045bfff6873SLukas Czerner  * thread, so we do not need any special locks, however we have to prevent
1046bfff6873SLukas Czerner  * inode allocation from the current group, so we take alloc_sem lock, to
1047*119c0d44STheodore Ts'o  * block ext4_new_inode() until we are finished.
1048bfff6873SLukas Czerner  */
1049e0cbee3eSH Hartley Sweeten int ext4_init_inode_table(struct super_block *sb, ext4_group_t group,
1050bfff6873SLukas Czerner 				 int barrier)
1051bfff6873SLukas Czerner {
1052bfff6873SLukas Czerner 	struct ext4_group_info *grp = ext4_get_group_info(sb, group);
1053bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1054bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
1055bfff6873SLukas Czerner 	struct buffer_head *group_desc_bh;
1056bfff6873SLukas Czerner 	handle_t *handle;
1057bfff6873SLukas Czerner 	ext4_fsblk_t blk;
1058bfff6873SLukas Czerner 	int num, ret = 0, used_blks = 0;
1059bfff6873SLukas Czerner 
1060bfff6873SLukas Czerner 	/* This should not happen, but just to be sure check this */
1061bfff6873SLukas Czerner 	if (sb->s_flags & MS_RDONLY) {
1062bfff6873SLukas Czerner 		ret = 1;
1063bfff6873SLukas Czerner 		goto out;
1064bfff6873SLukas Czerner 	}
1065bfff6873SLukas Czerner 
1066bfff6873SLukas Czerner 	gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
1067bfff6873SLukas Czerner 	if (!gdp)
1068bfff6873SLukas Czerner 		goto out;
1069bfff6873SLukas Czerner 
1070bfff6873SLukas Czerner 	/*
1071bfff6873SLukas Czerner 	 * We do not need to lock this, because we are the only one
1072bfff6873SLukas Czerner 	 * handling this flag.
1073bfff6873SLukas Czerner 	 */
1074bfff6873SLukas Czerner 	if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED))
1075bfff6873SLukas Czerner 		goto out;
1076bfff6873SLukas Czerner 
1077bfff6873SLukas Czerner 	handle = ext4_journal_start_sb(sb, 1);
1078bfff6873SLukas Czerner 	if (IS_ERR(handle)) {
1079bfff6873SLukas Czerner 		ret = PTR_ERR(handle);
1080bfff6873SLukas Czerner 		goto out;
1081bfff6873SLukas Czerner 	}
1082bfff6873SLukas Czerner 
1083bfff6873SLukas Czerner 	down_write(&grp->alloc_sem);
1084bfff6873SLukas Czerner 	/*
1085bfff6873SLukas Czerner 	 * If inode bitmap was already initialized there may be some
1086bfff6873SLukas Czerner 	 * used inodes so we need to skip blocks with used inodes in
1087bfff6873SLukas Czerner 	 * inode table.
1088bfff6873SLukas Czerner 	 */
1089bfff6873SLukas Czerner 	if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)))
1090bfff6873SLukas Czerner 		used_blks = DIV_ROUND_UP((EXT4_INODES_PER_GROUP(sb) -
1091bfff6873SLukas Czerner 			    ext4_itable_unused_count(sb, gdp)),
1092bfff6873SLukas Czerner 			    sbi->s_inodes_per_block);
1093bfff6873SLukas Czerner 
1094857ac889SLukas Czerner 	if ((used_blks < 0) || (used_blks > sbi->s_itb_per_group)) {
1095857ac889SLukas Czerner 		ext4_error(sb, "Something is wrong with group %u\n"
1096857ac889SLukas Czerner 			   "Used itable blocks: %d"
1097857ac889SLukas Czerner 			   "itable unused count: %u\n",
1098857ac889SLukas Czerner 			   group, used_blks,
1099857ac889SLukas Czerner 			   ext4_itable_unused_count(sb, gdp));
1100857ac889SLukas Czerner 		ret = 1;
110133853a0dSYongqiang Yang 		goto err_out;
1102857ac889SLukas Czerner 	}
1103857ac889SLukas Czerner 
1104bfff6873SLukas Czerner 	blk = ext4_inode_table(sb, gdp) + used_blks;
1105bfff6873SLukas Czerner 	num = sbi->s_itb_per_group - used_blks;
1106bfff6873SLukas Czerner 
1107bfff6873SLukas Czerner 	BUFFER_TRACE(group_desc_bh, "get_write_access");
1108bfff6873SLukas Czerner 	ret = ext4_journal_get_write_access(handle,
1109bfff6873SLukas Czerner 					    group_desc_bh);
1110bfff6873SLukas Czerner 	if (ret)
1111bfff6873SLukas Czerner 		goto err_out;
1112bfff6873SLukas Czerner 
1113bfff6873SLukas Czerner 	/*
1114bfff6873SLukas Czerner 	 * Skip zeroout if the inode table is full. But we set the ZEROED
1115bfff6873SLukas Czerner 	 * flag anyway, because obviously, when it is full it does not need
1116bfff6873SLukas Czerner 	 * further zeroing.
1117bfff6873SLukas Czerner 	 */
1118bfff6873SLukas Czerner 	if (unlikely(num == 0))
1119bfff6873SLukas Czerner 		goto skip_zeroout;
1120bfff6873SLukas Czerner 
1121bfff6873SLukas Czerner 	ext4_debug("going to zero out inode table in group %d\n",
1122bfff6873SLukas Czerner 		   group);
1123a107e5a3STheodore Ts'o 	ret = sb_issue_zeroout(sb, blk, num, GFP_NOFS);
1124bfff6873SLukas Czerner 	if (ret < 0)
1125bfff6873SLukas Czerner 		goto err_out;
1126a107e5a3STheodore Ts'o 	if (barrier)
1127a107e5a3STheodore Ts'o 		blkdev_issue_flush(sb->s_bdev, GFP_NOFS, NULL);
1128bfff6873SLukas Czerner 
1129bfff6873SLukas Czerner skip_zeroout:
1130bfff6873SLukas Czerner 	ext4_lock_group(sb, group);
1131bfff6873SLukas Czerner 	gdp->bg_flags |= cpu_to_le16(EXT4_BG_INODE_ZEROED);
1132bfff6873SLukas Czerner 	gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp);
1133bfff6873SLukas Czerner 	ext4_unlock_group(sb, group);
1134bfff6873SLukas Czerner 
1135bfff6873SLukas Czerner 	BUFFER_TRACE(group_desc_bh,
1136bfff6873SLukas Czerner 		     "call ext4_handle_dirty_metadata");
1137bfff6873SLukas Czerner 	ret = ext4_handle_dirty_metadata(handle, NULL,
1138bfff6873SLukas Czerner 					 group_desc_bh);
1139bfff6873SLukas Czerner 
1140bfff6873SLukas Czerner err_out:
1141bfff6873SLukas Czerner 	up_write(&grp->alloc_sem);
1142bfff6873SLukas Czerner 	ext4_journal_stop(handle);
1143bfff6873SLukas Czerner out:
1144bfff6873SLukas Czerner 	return ret;
1145bfff6873SLukas Czerner }
1146