xref: /linux/fs/ext4/ialloc.c (revision 785b4b3a5a992eeb8b495a89b6f690b383010aba)
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  */
53717d50e4SAndreas Dilger void 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 */
68fd2d4291SAvantika Mathur unsigned ext4_init_inode_bitmap(struct super_block *sb, struct buffer_head *bh,
69fd2d4291SAvantika Mathur 				ext4_group_t block_group,
70717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
71717d50e4SAndreas Dilger {
72717d50e4SAndreas Dilger 	struct ext4_sb_info *sbi = EXT4_SB(sb);
73717d50e4SAndreas Dilger 
74717d50e4SAndreas Dilger 	J_ASSERT_BH(bh, buffer_locked(bh));
75717d50e4SAndreas Dilger 
76717d50e4SAndreas Dilger 	/* If checksum is bad mark all blocks and inodes use to prevent
77717d50e4SAndreas Dilger 	 * allocation, essentially implementing a per-group read-only flag. */
78717d50e4SAndreas Dilger 	if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) {
79a9df9a49STheodore Ts'o 		ext4_error(sb, __func__, "Checksum bad for group %u",
80717d50e4SAndreas Dilger 			   block_group);
81560671a0SAneesh Kumar K.V 		ext4_free_blks_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);
89648f5879SAneesh Kumar K.V 	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;
111e29d1cdeSEric Sandeen 	bitmap_blk = ext4_inode_bitmap(sb, desc);
112e29d1cdeSEric Sandeen 	bh = sb_getblk(sb, bitmap_blk);
113e29d1cdeSEric Sandeen 	if (unlikely(!bh)) {
114e29d1cdeSEric Sandeen 		ext4_error(sb, __func__,
115ac27a0ecSDave Kleikamp 			    "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 	}
128955ce5f5SAneesh Kumar K.V 	ext4_lock_group(sb, block_group);
129e29d1cdeSEric Sandeen 	if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
130e29d1cdeSEric Sandeen 		ext4_init_inode_bitmap(sb, bh, block_group, desc);
1312ccb5fb9SAneesh Kumar K.V 		set_bitmap_uptodate(bh);
132e29d1cdeSEric Sandeen 		set_buffer_uptodate(bh);
133955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, block_group);
1343300bedaSAneesh Kumar K.V 		unlock_buffer(bh);
135e29d1cdeSEric Sandeen 		return bh;
136e29d1cdeSEric Sandeen 	}
137955ce5f5SAneesh Kumar K.V 	ext4_unlock_group(sb, block_group);
1382ccb5fb9SAneesh Kumar K.V 	if (buffer_uptodate(bh)) {
1392ccb5fb9SAneesh Kumar K.V 		/*
1402ccb5fb9SAneesh Kumar K.V 		 * if not uninit if bh is uptodate,
1412ccb5fb9SAneesh Kumar K.V 		 * bitmap is also uptodate
1422ccb5fb9SAneesh Kumar K.V 		 */
1432ccb5fb9SAneesh Kumar K.V 		set_bitmap_uptodate(bh);
1442ccb5fb9SAneesh Kumar K.V 		unlock_buffer(bh);
1452ccb5fb9SAneesh Kumar K.V 		return bh;
1462ccb5fb9SAneesh Kumar K.V 	}
1472ccb5fb9SAneesh Kumar K.V 	/*
1482ccb5fb9SAneesh Kumar K.V 	 * submit the buffer_head for read. We can
1492ccb5fb9SAneesh Kumar K.V 	 * safely mark the bitmap as uptodate now.
1502ccb5fb9SAneesh Kumar K.V 	 * We do it here so the bitmap uptodate bit
1512ccb5fb9SAneesh Kumar K.V 	 * get set with buffer lock held.
1522ccb5fb9SAneesh Kumar K.V 	 */
1532ccb5fb9SAneesh Kumar K.V 	set_bitmap_uptodate(bh);
154e29d1cdeSEric Sandeen 	if (bh_submit_read(bh) < 0) {
155e29d1cdeSEric Sandeen 		put_bh(bh);
156e29d1cdeSEric Sandeen 		ext4_error(sb, __func__,
157e29d1cdeSEric Sandeen 			    "Cannot read inode bitmap - "
158a9df9a49STheodore Ts'o 			    "block_group = %u, inode_bitmap = %llu",
159e29d1cdeSEric Sandeen 			    block_group, bitmap_blk);
160e29d1cdeSEric Sandeen 		return NULL;
161e29d1cdeSEric Sandeen 	}
162ac27a0ecSDave Kleikamp 	return bh;
163ac27a0ecSDave Kleikamp }
164ac27a0ecSDave Kleikamp 
165ac27a0ecSDave Kleikamp /*
166ac27a0ecSDave Kleikamp  * NOTE! When we get the inode, we're the only people
167ac27a0ecSDave Kleikamp  * that have access to it, and as such there are no
168ac27a0ecSDave Kleikamp  * race conditions we have to worry about. The inode
169ac27a0ecSDave Kleikamp  * is not on the hash-lists, and it cannot be reached
170ac27a0ecSDave Kleikamp  * through the filesystem because the directory entry
171ac27a0ecSDave Kleikamp  * has been deleted earlier.
172ac27a0ecSDave Kleikamp  *
173ac27a0ecSDave Kleikamp  * HOWEVER: we must make sure that we get no aliases,
174ac27a0ecSDave Kleikamp  * which means that we have to call "clear_inode()"
175ac27a0ecSDave Kleikamp  * _before_ we mark the inode not in use in the inode
176ac27a0ecSDave Kleikamp  * bitmaps. Otherwise a newly created file might use
177ac27a0ecSDave Kleikamp  * the same inode number (not actually the same pointer
178ac27a0ecSDave Kleikamp  * though), and then we'd have two inodes sharing the
179ac27a0ecSDave Kleikamp  * same inode number and space on the harddisk.
180ac27a0ecSDave Kleikamp  */
181617ba13bSMingming Cao void ext4_free_inode(handle_t *handle, struct inode *inode)
182ac27a0ecSDave Kleikamp {
183ac27a0ecSDave Kleikamp 	struct super_block *sb = inode->i_sb;
184ac27a0ecSDave Kleikamp 	int is_directory;
185ac27a0ecSDave Kleikamp 	unsigned long ino;
186ac27a0ecSDave Kleikamp 	struct buffer_head *bitmap_bh = NULL;
187ac27a0ecSDave Kleikamp 	struct buffer_head *bh2;
188fd2d4291SAvantika Mathur 	ext4_group_t block_group;
189ac27a0ecSDave Kleikamp 	unsigned long bit;
190617ba13bSMingming Cao 	struct ext4_group_desc *gdp;
191617ba13bSMingming Cao 	struct ext4_super_block *es;
192617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
1937ce9d5d1SEric Sandeen 	int fatal = 0, err, count, cleared;
194ac27a0ecSDave Kleikamp 
195ac27a0ecSDave Kleikamp 	if (atomic_read(&inode->i_count) > 1) {
1964776004fSTheodore Ts'o 		printk(KERN_ERR "ext4_free_inode: inode has count=%d\n",
197ac27a0ecSDave Kleikamp 		       atomic_read(&inode->i_count));
198ac27a0ecSDave Kleikamp 		return;
199ac27a0ecSDave Kleikamp 	}
200ac27a0ecSDave Kleikamp 	if (inode->i_nlink) {
2014776004fSTheodore Ts'o 		printk(KERN_ERR "ext4_free_inode: inode has nlink=%d\n",
202ac27a0ecSDave Kleikamp 		       inode->i_nlink);
203ac27a0ecSDave Kleikamp 		return;
204ac27a0ecSDave Kleikamp 	}
205ac27a0ecSDave Kleikamp 	if (!sb) {
2064776004fSTheodore Ts'o 		printk(KERN_ERR "ext4_free_inode: inode on "
2074776004fSTheodore Ts'o 		       "nonexistent device\n");
208ac27a0ecSDave Kleikamp 		return;
209ac27a0ecSDave Kleikamp 	}
210617ba13bSMingming Cao 	sbi = EXT4_SB(sb);
211ac27a0ecSDave Kleikamp 
212ac27a0ecSDave Kleikamp 	ino = inode->i_ino;
213617ba13bSMingming Cao 	ext4_debug("freeing inode %lu\n", ino);
2149bffad1eSTheodore Ts'o 	trace_ext4_free_inode(inode);
215ac27a0ecSDave Kleikamp 
216ac27a0ecSDave Kleikamp 	/*
217ac27a0ecSDave Kleikamp 	 * Note: we must free any quota before locking the superblock,
218ac27a0ecSDave Kleikamp 	 * as writing the quota to disk may need the lock as well.
219ac27a0ecSDave Kleikamp 	 */
220a269eb18SJan Kara 	vfs_dq_init(inode);
221617ba13bSMingming Cao 	ext4_xattr_delete_inode(handle, inode);
222a269eb18SJan Kara 	vfs_dq_free_inode(inode);
223a269eb18SJan Kara 	vfs_dq_drop(inode);
224ac27a0ecSDave Kleikamp 
225ac27a0ecSDave Kleikamp 	is_directory = S_ISDIR(inode->i_mode);
226ac27a0ecSDave Kleikamp 
227ac27a0ecSDave Kleikamp 	/* Do this BEFORE marking the inode not in use or returning an error */
228ac27a0ecSDave Kleikamp 	clear_inode(inode);
229ac27a0ecSDave Kleikamp 
230617ba13bSMingming Cao 	es = EXT4_SB(sb)->s_es;
231617ba13bSMingming Cao 	if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
232617ba13bSMingming Cao 		ext4_error(sb, "ext4_free_inode",
233ac27a0ecSDave Kleikamp 			   "reserved or nonexistent inode %lu", ino);
234ac27a0ecSDave Kleikamp 		goto error_return;
235ac27a0ecSDave Kleikamp 	}
236617ba13bSMingming Cao 	block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
237617ba13bSMingming Cao 	bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
238e29d1cdeSEric Sandeen 	bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
239ac27a0ecSDave Kleikamp 	if (!bitmap_bh)
240ac27a0ecSDave Kleikamp 		goto error_return;
241ac27a0ecSDave Kleikamp 
242ac27a0ecSDave Kleikamp 	BUFFER_TRACE(bitmap_bh, "get_write_access");
243617ba13bSMingming Cao 	fatal = ext4_journal_get_write_access(handle, bitmap_bh);
244ac27a0ecSDave Kleikamp 	if (fatal)
245ac27a0ecSDave Kleikamp 		goto error_return;
246ac27a0ecSDave Kleikamp 
247ac27a0ecSDave Kleikamp 	/* Ok, now we can actually update the inode bitmaps.. */
248955ce5f5SAneesh Kumar K.V 	cleared = ext4_clear_bit_atomic(ext4_group_lock_ptr(sb, block_group),
249955ce5f5SAneesh Kumar K.V 					bit, bitmap_bh->b_data);
2507ce9d5d1SEric Sandeen 	if (!cleared)
251617ba13bSMingming Cao 		ext4_error(sb, "ext4_free_inode",
252ac27a0ecSDave Kleikamp 			   "bit already cleared for inode %lu", ino);
253ac27a0ecSDave Kleikamp 	else {
254617ba13bSMingming Cao 		gdp = ext4_get_group_desc(sb, block_group, &bh2);
255ac27a0ecSDave Kleikamp 
256ac27a0ecSDave Kleikamp 		BUFFER_TRACE(bh2, "get_write_access");
257617ba13bSMingming Cao 		fatal = ext4_journal_get_write_access(handle, bh2);
258ac27a0ecSDave Kleikamp 		if (fatal) goto error_return;
259ac27a0ecSDave Kleikamp 
260ac27a0ecSDave Kleikamp 		if (gdp) {
261955ce5f5SAneesh Kumar K.V 			ext4_lock_group(sb, block_group);
262560671a0SAneesh Kumar K.V 			count = ext4_free_inodes_count(sb, gdp) + 1;
263560671a0SAneesh Kumar K.V 			ext4_free_inodes_set(sb, gdp, count);
264560671a0SAneesh Kumar K.V 			if (is_directory) {
265560671a0SAneesh Kumar K.V 				count = ext4_used_dirs_count(sb, gdp) - 1;
266560671a0SAneesh Kumar K.V 				ext4_used_dirs_set(sb, gdp, count);
2677d39db14STheodore Ts'o 				if (sbi->s_log_groups_per_flex) {
2687d39db14STheodore Ts'o 					ext4_group_t f;
2697d39db14STheodore Ts'o 
2707d39db14STheodore Ts'o 					f = ext4_flex_group(sbi, block_group);
2717d39db14STheodore Ts'o 					atomic_dec(&sbi->s_flex_groups[f].free_inodes);
2727d39db14STheodore Ts'o 				}
2737d39db14STheodore Ts'o 
274560671a0SAneesh Kumar K.V 			}
275717d50e4SAndreas Dilger 			gdp->bg_checksum = ext4_group_desc_csum(sbi,
276717d50e4SAndreas Dilger 							block_group, gdp);
277955ce5f5SAneesh Kumar K.V 			ext4_unlock_group(sb, block_group);
278ac27a0ecSDave Kleikamp 			percpu_counter_inc(&sbi->s_freeinodes_counter);
279ac27a0ecSDave Kleikamp 			if (is_directory)
280ac27a0ecSDave Kleikamp 				percpu_counter_dec(&sbi->s_dirs_counter);
281ac27a0ecSDave Kleikamp 
282772cb7c8SJose R. Santos 			if (sbi->s_log_groups_per_flex) {
2839f24e420STheodore Ts'o 				ext4_group_t f;
2849f24e420STheodore Ts'o 
2859f24e420STheodore Ts'o 				f = ext4_flex_group(sbi, block_group);
2869f24e420STheodore Ts'o 				atomic_inc(&sbi->s_flex_groups[f].free_inodes);
287772cb7c8SJose R. Santos 			}
288ac27a0ecSDave Kleikamp 		}
2890390131bSFrank Mayhar 		BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata");
2900390131bSFrank Mayhar 		err = ext4_handle_dirty_metadata(handle, NULL, bh2);
291ac27a0ecSDave Kleikamp 		if (!fatal) fatal = err;
292ac27a0ecSDave Kleikamp 	}
2930390131bSFrank Mayhar 	BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata");
2940390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
295ac27a0ecSDave Kleikamp 	if (!fatal)
296ac27a0ecSDave Kleikamp 		fatal = err;
297ac27a0ecSDave Kleikamp 	sb->s_dirt = 1;
298ac27a0ecSDave Kleikamp error_return:
299ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
300617ba13bSMingming Cao 	ext4_std_error(sb, fatal);
301ac27a0ecSDave Kleikamp }
302ac27a0ecSDave Kleikamp 
303ac27a0ecSDave Kleikamp /*
304ac27a0ecSDave Kleikamp  * There are two policies for allocating an inode.  If the new inode is
305ac27a0ecSDave Kleikamp  * a directory, then a forward search is made for a block group with both
306ac27a0ecSDave Kleikamp  * free space and a low directory-to-inode ratio; if that fails, then of
307ac27a0ecSDave Kleikamp  * the groups with above-average free space, that group with the fewest
308ac27a0ecSDave Kleikamp  * directories already is chosen.
309ac27a0ecSDave Kleikamp  *
310ac27a0ecSDave Kleikamp  * For other inodes, search forward from the parent directory\'s block
311ac27a0ecSDave Kleikamp  * group to find a free inode.
312ac27a0ecSDave Kleikamp  */
3132aa9fc4cSAvantika Mathur static int find_group_dir(struct super_block *sb, struct inode *parent,
3142aa9fc4cSAvantika Mathur 				ext4_group_t *best_group)
315ac27a0ecSDave Kleikamp {
3168df9675fSTheodore Ts'o 	ext4_group_t ngroups = ext4_get_groups_count(sb);
317ac27a0ecSDave Kleikamp 	unsigned int freei, avefreei;
318617ba13bSMingming Cao 	struct ext4_group_desc *desc, *best_desc = NULL;
3192aa9fc4cSAvantika Mathur 	ext4_group_t group;
3202aa9fc4cSAvantika Mathur 	int ret = -1;
321ac27a0ecSDave Kleikamp 
322617ba13bSMingming Cao 	freei = percpu_counter_read_positive(&EXT4_SB(sb)->s_freeinodes_counter);
323ac27a0ecSDave Kleikamp 	avefreei = freei / ngroups;
324ac27a0ecSDave Kleikamp 
325ac27a0ecSDave Kleikamp 	for (group = 0; group < ngroups; group++) {
326ef2fb679SEric Sandeen 		desc = ext4_get_group_desc(sb, group, NULL);
327560671a0SAneesh Kumar K.V 		if (!desc || !ext4_free_inodes_count(sb, desc))
328ac27a0ecSDave Kleikamp 			continue;
329560671a0SAneesh Kumar K.V 		if (ext4_free_inodes_count(sb, desc) < avefreei)
330ac27a0ecSDave Kleikamp 			continue;
331ac27a0ecSDave Kleikamp 		if (!best_desc ||
332560671a0SAneesh Kumar K.V 		    (ext4_free_blks_count(sb, desc) >
333560671a0SAneesh Kumar K.V 		     ext4_free_blks_count(sb, best_desc))) {
3342aa9fc4cSAvantika Mathur 			*best_group = group;
335ac27a0ecSDave Kleikamp 			best_desc = desc;
3362aa9fc4cSAvantika Mathur 			ret = 0;
337ac27a0ecSDave Kleikamp 		}
338ac27a0ecSDave Kleikamp 	}
3392aa9fc4cSAvantika Mathur 	return ret;
340ac27a0ecSDave Kleikamp }
341ac27a0ecSDave Kleikamp 
342772cb7c8SJose R. Santos #define free_block_ratio 10
343772cb7c8SJose R. Santos 
344772cb7c8SJose R. Santos static int find_group_flex(struct super_block *sb, struct inode *parent,
345772cb7c8SJose R. Santos 			   ext4_group_t *best_group)
346772cb7c8SJose R. Santos {
347772cb7c8SJose R. Santos 	struct ext4_sb_info *sbi = EXT4_SB(sb);
348772cb7c8SJose R. Santos 	struct ext4_group_desc *desc;
349772cb7c8SJose R. Santos 	struct flex_groups *flex_group = sbi->s_flex_groups;
350772cb7c8SJose R. Santos 	ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
351772cb7c8SJose R. Santos 	ext4_group_t parent_fbg_group = ext4_flex_group(sbi, parent_group);
3528df9675fSTheodore Ts'o 	ext4_group_t ngroups = ext4_get_groups_count(sb);
353772cb7c8SJose R. Santos 	int flex_size = ext4_flex_bg_size(sbi);
354772cb7c8SJose R. Santos 	ext4_group_t best_flex = parent_fbg_group;
355772cb7c8SJose R. Santos 	int blocks_per_flex = sbi->s_blocks_per_group * flex_size;
356772cb7c8SJose R. Santos 	int flexbg_free_blocks;
357772cb7c8SJose R. Santos 	int flex_freeb_ratio;
358772cb7c8SJose R. Santos 	ext4_group_t n_fbg_groups;
359772cb7c8SJose R. Santos 	ext4_group_t i;
360772cb7c8SJose R. Santos 
3618df9675fSTheodore Ts'o 	n_fbg_groups = (ngroups + flex_size - 1) >>
362772cb7c8SJose R. Santos 		sbi->s_log_groups_per_flex;
363772cb7c8SJose R. Santos 
364772cb7c8SJose R. Santos find_close_to_parent:
3659f24e420STheodore Ts'o 	flexbg_free_blocks = atomic_read(&flex_group[best_flex].free_blocks);
366772cb7c8SJose R. Santos 	flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
3679f24e420STheodore Ts'o 	if (atomic_read(&flex_group[best_flex].free_inodes) &&
368772cb7c8SJose R. Santos 	    flex_freeb_ratio > free_block_ratio)
369772cb7c8SJose R. Santos 		goto found_flexbg;
370772cb7c8SJose R. Santos 
371772cb7c8SJose R. Santos 	if (best_flex && best_flex == parent_fbg_group) {
372772cb7c8SJose R. Santos 		best_flex--;
373772cb7c8SJose R. Santos 		goto find_close_to_parent;
374772cb7c8SJose R. Santos 	}
375772cb7c8SJose R. Santos 
376772cb7c8SJose R. Santos 	for (i = 0; i < n_fbg_groups; i++) {
377772cb7c8SJose R. Santos 		if (i == parent_fbg_group || i == parent_fbg_group - 1)
378772cb7c8SJose R. Santos 			continue;
379772cb7c8SJose R. Santos 
3809f24e420STheodore Ts'o 		flexbg_free_blocks = atomic_read(&flex_group[i].free_blocks);
381772cb7c8SJose R. Santos 		flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
382772cb7c8SJose R. Santos 
383772cb7c8SJose R. Santos 		if (flex_freeb_ratio > free_block_ratio &&
3849f24e420STheodore Ts'o 		    (atomic_read(&flex_group[i].free_inodes))) {
385772cb7c8SJose R. Santos 			best_flex = i;
386772cb7c8SJose R. Santos 			goto found_flexbg;
387772cb7c8SJose R. Santos 		}
388772cb7c8SJose R. Santos 
3899f24e420STheodore Ts'o 		if ((atomic_read(&flex_group[best_flex].free_inodes) == 0) ||
3909f24e420STheodore Ts'o 		    ((atomic_read(&flex_group[i].free_blocks) >
3919f24e420STheodore Ts'o 		      atomic_read(&flex_group[best_flex].free_blocks)) &&
3929f24e420STheodore Ts'o 		     atomic_read(&flex_group[i].free_inodes)))
393772cb7c8SJose R. Santos 			best_flex = i;
394772cb7c8SJose R. Santos 	}
395772cb7c8SJose R. Santos 
3969f24e420STheodore Ts'o 	if (!atomic_read(&flex_group[best_flex].free_inodes) ||
3979f24e420STheodore Ts'o 	    !atomic_read(&flex_group[best_flex].free_blocks))
398772cb7c8SJose R. Santos 		return -1;
399772cb7c8SJose R. Santos 
400772cb7c8SJose R. Santos found_flexbg:
401772cb7c8SJose R. Santos 	for (i = best_flex * flex_size; i < ngroups &&
402772cb7c8SJose R. Santos 		     i < (best_flex + 1) * flex_size; i++) {
40388b6edd1STheodore Ts'o 		desc = ext4_get_group_desc(sb, i, NULL);
404560671a0SAneesh Kumar K.V 		if (ext4_free_inodes_count(sb, desc)) {
405772cb7c8SJose R. Santos 			*best_group = i;
406772cb7c8SJose R. Santos 			goto out;
407772cb7c8SJose R. Santos 		}
408772cb7c8SJose R. Santos 	}
409772cb7c8SJose R. Santos 
410772cb7c8SJose R. Santos 	return -1;
411772cb7c8SJose R. Santos out:
412772cb7c8SJose R. Santos 	return 0;
413772cb7c8SJose R. Santos }
414772cb7c8SJose R. Santos 
415a4912123STheodore Ts'o struct orlov_stats {
416a4912123STheodore Ts'o 	__u32 free_inodes;
417a4912123STheodore Ts'o 	__u32 free_blocks;
418a4912123STheodore Ts'o 	__u32 used_dirs;
419a4912123STheodore Ts'o };
420a4912123STheodore Ts'o 
421a4912123STheodore Ts'o /*
422a4912123STheodore Ts'o  * Helper function for Orlov's allocator; returns critical information
423a4912123STheodore Ts'o  * for a particular block group or flex_bg.  If flex_size is 1, then g
424a4912123STheodore Ts'o  * is a block group number; otherwise it is flex_bg number.
425a4912123STheodore Ts'o  */
426a4912123STheodore Ts'o void get_orlov_stats(struct super_block *sb, ext4_group_t g,
427a4912123STheodore Ts'o 		       int flex_size, struct orlov_stats *stats)
428a4912123STheodore Ts'o {
429a4912123STheodore Ts'o 	struct ext4_group_desc *desc;
4307d39db14STheodore Ts'o 	struct flex_groups *flex_group = EXT4_SB(sb)->s_flex_groups;
431a4912123STheodore Ts'o 
4327d39db14STheodore Ts'o 	if (flex_size > 1) {
4337d39db14STheodore Ts'o 		stats->free_inodes = atomic_read(&flex_group[g].free_inodes);
4347d39db14STheodore Ts'o 		stats->free_blocks = atomic_read(&flex_group[g].free_blocks);
4357d39db14STheodore Ts'o 		stats->used_dirs = atomic_read(&flex_group[g].used_dirs);
4367d39db14STheodore Ts'o 		return;
4377d39db14STheodore Ts'o 	}
4387d39db14STheodore Ts'o 
4397d39db14STheodore Ts'o 	desc = ext4_get_group_desc(sb, g, NULL);
4407d39db14STheodore Ts'o 	if (desc) {
4417d39db14STheodore Ts'o 		stats->free_inodes = ext4_free_inodes_count(sb, desc);
4427d39db14STheodore Ts'o 		stats->free_blocks = ext4_free_blks_count(sb, desc);
4437d39db14STheodore Ts'o 		stats->used_dirs = ext4_used_dirs_count(sb, desc);
4447d39db14STheodore Ts'o 	} else {
445a4912123STheodore Ts'o 		stats->free_inodes = 0;
446a4912123STheodore Ts'o 		stats->free_blocks = 0;
447a4912123STheodore Ts'o 		stats->used_dirs = 0;
448a4912123STheodore Ts'o 	}
449a4912123STheodore Ts'o }
450a4912123STheodore Ts'o 
451ac27a0ecSDave Kleikamp /*
452ac27a0ecSDave Kleikamp  * Orlov's allocator for directories.
453ac27a0ecSDave Kleikamp  *
454ac27a0ecSDave Kleikamp  * We always try to spread first-level directories.
455ac27a0ecSDave Kleikamp  *
456ac27a0ecSDave Kleikamp  * If there are blockgroups with both free inodes and free blocks counts
457ac27a0ecSDave Kleikamp  * not worse than average we return one with smallest directory count.
458ac27a0ecSDave Kleikamp  * Otherwise we simply return a random group.
459ac27a0ecSDave Kleikamp  *
460ac27a0ecSDave Kleikamp  * For the rest rules look so:
461ac27a0ecSDave Kleikamp  *
462ac27a0ecSDave Kleikamp  * It's OK to put directory into a group unless
463ac27a0ecSDave Kleikamp  * it has too many directories already (max_dirs) or
464ac27a0ecSDave Kleikamp  * it has too few free inodes left (min_inodes) or
465ac27a0ecSDave Kleikamp  * it has too few free blocks left (min_blocks) or
4661cc8dcf5SBenoit Boissinot  * Parent's group is preferred, if it doesn't satisfy these
467ac27a0ecSDave Kleikamp  * conditions we search cyclically through the rest. If none
468ac27a0ecSDave Kleikamp  * of the groups look good we just look for a group with more
469ac27a0ecSDave Kleikamp  * free inodes than average (starting at parent's group).
470ac27a0ecSDave Kleikamp  */
471ac27a0ecSDave Kleikamp 
4722aa9fc4cSAvantika Mathur static int find_group_orlov(struct super_block *sb, struct inode *parent,
473f157a4aaSTheodore Ts'o 			    ext4_group_t *group, int mode,
474f157a4aaSTheodore Ts'o 			    const struct qstr *qstr)
475ac27a0ecSDave Kleikamp {
476fd2d4291SAvantika Mathur 	ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
477617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4788df9675fSTheodore Ts'o 	ext4_group_t real_ngroups = ext4_get_groups_count(sb);
479617ba13bSMingming Cao 	int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
480ac27a0ecSDave Kleikamp 	unsigned int freei, avefreei;
481617ba13bSMingming Cao 	ext4_fsblk_t freeb, avefreeb;
482ac27a0ecSDave Kleikamp 	unsigned int ndirs;
483a4912123STheodore Ts'o 	int max_dirs, min_inodes;
484617ba13bSMingming Cao 	ext4_grpblk_t min_blocks;
4858df9675fSTheodore Ts'o 	ext4_group_t i, grp, g, ngroups;
486617ba13bSMingming Cao 	struct ext4_group_desc *desc;
487a4912123STheodore Ts'o 	struct orlov_stats stats;
488a4912123STheodore Ts'o 	int flex_size = ext4_flex_bg_size(sbi);
489f157a4aaSTheodore Ts'o 	struct dx_hash_info hinfo;
490a4912123STheodore Ts'o 
4918df9675fSTheodore Ts'o 	ngroups = real_ngroups;
492a4912123STheodore Ts'o 	if (flex_size > 1) {
4938df9675fSTheodore Ts'o 		ngroups = (real_ngroups + flex_size - 1) >>
494a4912123STheodore Ts'o 			sbi->s_log_groups_per_flex;
495a4912123STheodore Ts'o 		parent_group >>= sbi->s_log_groups_per_flex;
496a4912123STheodore Ts'o 	}
497ac27a0ecSDave Kleikamp 
498ac27a0ecSDave Kleikamp 	freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
499ac27a0ecSDave Kleikamp 	avefreei = freei / ngroups;
500ac27a0ecSDave Kleikamp 	freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
5013a5b2ecdSMingming Cao 	avefreeb = freeb;
502f4e5bc24SAndrew Morton 	do_div(avefreeb, ngroups);
503ac27a0ecSDave Kleikamp 	ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
504ac27a0ecSDave Kleikamp 
505a4912123STheodore Ts'o 	if (S_ISDIR(mode) &&
506a4912123STheodore Ts'o 	    ((parent == sb->s_root->d_inode) ||
507a4912123STheodore Ts'o 	     (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL))) {
508ac27a0ecSDave Kleikamp 		int best_ndir = inodes_per_group;
5092aa9fc4cSAvantika Mathur 		int ret = -1;
510ac27a0ecSDave Kleikamp 
511f157a4aaSTheodore Ts'o 		if (qstr) {
512f157a4aaSTheodore Ts'o 			hinfo.hash_version = DX_HASH_HALF_MD4;
513f157a4aaSTheodore Ts'o 			hinfo.seed = sbi->s_hash_seed;
514f157a4aaSTheodore Ts'o 			ext4fs_dirhash(qstr->name, qstr->len, &hinfo);
515f157a4aaSTheodore Ts'o 			grp = hinfo.hash;
516f157a4aaSTheodore Ts'o 		} else
5172aa9fc4cSAvantika Mathur 			get_random_bytes(&grp, sizeof(grp));
5182aa9fc4cSAvantika Mathur 		parent_group = (unsigned)grp % ngroups;
519ac27a0ecSDave Kleikamp 		for (i = 0; i < ngroups; i++) {
520a4912123STheodore Ts'o 			g = (parent_group + i) % ngroups;
521a4912123STheodore Ts'o 			get_orlov_stats(sb, g, flex_size, &stats);
522a4912123STheodore Ts'o 			if (!stats.free_inodes)
523ac27a0ecSDave Kleikamp 				continue;
524a4912123STheodore Ts'o 			if (stats.used_dirs >= best_ndir)
525ac27a0ecSDave Kleikamp 				continue;
526a4912123STheodore Ts'o 			if (stats.free_inodes < avefreei)
527ac27a0ecSDave Kleikamp 				continue;
528a4912123STheodore Ts'o 			if (stats.free_blocks < avefreeb)
529ac27a0ecSDave Kleikamp 				continue;
530a4912123STheodore Ts'o 			grp = g;
5312aa9fc4cSAvantika Mathur 			ret = 0;
532a4912123STheodore Ts'o 			best_ndir = stats.used_dirs;
533ac27a0ecSDave Kleikamp 		}
534a4912123STheodore Ts'o 		if (ret)
535a4912123STheodore Ts'o 			goto fallback;
536a4912123STheodore Ts'o 	found_flex_bg:
537a4912123STheodore Ts'o 		if (flex_size == 1) {
538a4912123STheodore Ts'o 			*group = grp;
539a4912123STheodore Ts'o 			return 0;
540a4912123STheodore Ts'o 		}
541a4912123STheodore Ts'o 
542a4912123STheodore Ts'o 		/*
543a4912123STheodore Ts'o 		 * We pack inodes at the beginning of the flexgroup's
544a4912123STheodore Ts'o 		 * inode tables.  Block allocation decisions will do
545a4912123STheodore Ts'o 		 * something similar, although regular files will
546a4912123STheodore Ts'o 		 * start at 2nd block group of the flexgroup.  See
547a4912123STheodore Ts'o 		 * ext4_ext_find_goal() and ext4_find_near().
548a4912123STheodore Ts'o 		 */
549a4912123STheodore Ts'o 		grp *= flex_size;
550a4912123STheodore Ts'o 		for (i = 0; i < flex_size; i++) {
5518df9675fSTheodore Ts'o 			if (grp+i >= real_ngroups)
552a4912123STheodore Ts'o 				break;
553a4912123STheodore Ts'o 			desc = ext4_get_group_desc(sb, grp+i, NULL);
554a4912123STheodore Ts'o 			if (desc && ext4_free_inodes_count(sb, desc)) {
555a4912123STheodore Ts'o 				*group = grp+i;
556a4912123STheodore Ts'o 				return 0;
557a4912123STheodore Ts'o 			}
558a4912123STheodore Ts'o 		}
559ac27a0ecSDave Kleikamp 		goto fallback;
560ac27a0ecSDave Kleikamp 	}
561ac27a0ecSDave Kleikamp 
562ac27a0ecSDave Kleikamp 	max_dirs = ndirs / ngroups + inodes_per_group / 16;
563a4912123STheodore Ts'o 	min_inodes = avefreei - inodes_per_group*flex_size / 4;
564a4912123STheodore Ts'o 	if (min_inodes < 1)
565a4912123STheodore Ts'o 		min_inodes = 1;
566a4912123STheodore Ts'o 	min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb)*flex_size / 4;
567ac27a0ecSDave Kleikamp 
568a4912123STheodore Ts'o 	/*
569a4912123STheodore Ts'o 	 * Start looking in the flex group where we last allocated an
570a4912123STheodore Ts'o 	 * inode for this parent directory
571a4912123STheodore Ts'o 	 */
572a4912123STheodore Ts'o 	if (EXT4_I(parent)->i_last_alloc_group != ~0) {
573a4912123STheodore Ts'o 		parent_group = EXT4_I(parent)->i_last_alloc_group;
574a4912123STheodore Ts'o 		if (flex_size > 1)
575a4912123STheodore Ts'o 			parent_group >>= sbi->s_log_groups_per_flex;
576a4912123STheodore Ts'o 	}
577ac27a0ecSDave Kleikamp 
578ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
579a4912123STheodore Ts'o 		grp = (parent_group + i) % ngroups;
580a4912123STheodore Ts'o 		get_orlov_stats(sb, grp, flex_size, &stats);
581a4912123STheodore Ts'o 		if (stats.used_dirs >= max_dirs)
582ac27a0ecSDave Kleikamp 			continue;
583a4912123STheodore Ts'o 		if (stats.free_inodes < min_inodes)
584ac27a0ecSDave Kleikamp 			continue;
585a4912123STheodore Ts'o 		if (stats.free_blocks < min_blocks)
586ac27a0ecSDave Kleikamp 			continue;
587a4912123STheodore Ts'o 		goto found_flex_bg;
588ac27a0ecSDave Kleikamp 	}
589ac27a0ecSDave Kleikamp 
590ac27a0ecSDave Kleikamp fallback:
5918df9675fSTheodore Ts'o 	ngroups = real_ngroups;
592a4912123STheodore Ts'o 	avefreei = freei / ngroups;
593b5451f7bSTheodore Ts'o fallback_retry:
594a4912123STheodore Ts'o 	parent_group = EXT4_I(parent)->i_block_group;
595ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
596a4912123STheodore Ts'o 		grp = (parent_group + i) % ngroups;
597a4912123STheodore Ts'o 		desc = ext4_get_group_desc(sb, grp, NULL);
598560671a0SAneesh Kumar K.V 		if (desc && ext4_free_inodes_count(sb, desc) &&
599a4912123STheodore Ts'o 		    ext4_free_inodes_count(sb, desc) >= avefreei) {
600a4912123STheodore Ts'o 			*group = grp;
6012aa9fc4cSAvantika Mathur 			return 0;
602ac27a0ecSDave Kleikamp 		}
603a4912123STheodore Ts'o 	}
604ac27a0ecSDave Kleikamp 
605ac27a0ecSDave Kleikamp 	if (avefreei) {
606ac27a0ecSDave Kleikamp 		/*
607ac27a0ecSDave Kleikamp 		 * The free-inodes counter is approximate, and for really small
608ac27a0ecSDave Kleikamp 		 * filesystems the above test can fail to find any blockgroups
609ac27a0ecSDave Kleikamp 		 */
610ac27a0ecSDave Kleikamp 		avefreei = 0;
611b5451f7bSTheodore Ts'o 		goto fallback_retry;
612ac27a0ecSDave Kleikamp 	}
613ac27a0ecSDave Kleikamp 
614ac27a0ecSDave Kleikamp 	return -1;
615ac27a0ecSDave Kleikamp }
616ac27a0ecSDave Kleikamp 
6172aa9fc4cSAvantika Mathur static int find_group_other(struct super_block *sb, struct inode *parent,
618a4912123STheodore Ts'o 			    ext4_group_t *group, int mode)
619ac27a0ecSDave Kleikamp {
620fd2d4291SAvantika Mathur 	ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
6218df9675fSTheodore Ts'o 	ext4_group_t i, last, ngroups = ext4_get_groups_count(sb);
622617ba13bSMingming Cao 	struct ext4_group_desc *desc;
623a4912123STheodore Ts'o 	int flex_size = ext4_flex_bg_size(EXT4_SB(sb));
624a4912123STheodore Ts'o 
625a4912123STheodore Ts'o 	/*
626a4912123STheodore Ts'o 	 * Try to place the inode is the same flex group as its
627a4912123STheodore Ts'o 	 * parent.  If we can't find space, use the Orlov algorithm to
628a4912123STheodore Ts'o 	 * find another flex group, and store that information in the
629a4912123STheodore Ts'o 	 * parent directory's inode information so that use that flex
630a4912123STheodore Ts'o 	 * group for future allocations.
631a4912123STheodore Ts'o 	 */
632a4912123STheodore Ts'o 	if (flex_size > 1) {
633a4912123STheodore Ts'o 		int retry = 0;
634a4912123STheodore Ts'o 
635a4912123STheodore Ts'o 	try_again:
636a4912123STheodore Ts'o 		parent_group &= ~(flex_size-1);
637a4912123STheodore Ts'o 		last = parent_group + flex_size;
638a4912123STheodore Ts'o 		if (last > ngroups)
639a4912123STheodore Ts'o 			last = ngroups;
640a4912123STheodore Ts'o 		for  (i = parent_group; i < last; i++) {
641a4912123STheodore Ts'o 			desc = ext4_get_group_desc(sb, i, NULL);
642a4912123STheodore Ts'o 			if (desc && ext4_free_inodes_count(sb, desc)) {
643a4912123STheodore Ts'o 				*group = i;
644a4912123STheodore Ts'o 				return 0;
645a4912123STheodore Ts'o 			}
646a4912123STheodore Ts'o 		}
647a4912123STheodore Ts'o 		if (!retry && EXT4_I(parent)->i_last_alloc_group != ~0) {
648a4912123STheodore Ts'o 			retry = 1;
649a4912123STheodore Ts'o 			parent_group = EXT4_I(parent)->i_last_alloc_group;
650a4912123STheodore Ts'o 			goto try_again;
651a4912123STheodore Ts'o 		}
652a4912123STheodore Ts'o 		/*
653a4912123STheodore Ts'o 		 * If this didn't work, use the Orlov search algorithm
654a4912123STheodore Ts'o 		 * to find a new flex group; we pass in the mode to
655a4912123STheodore Ts'o 		 * avoid the topdir algorithms.
656a4912123STheodore Ts'o 		 */
657a4912123STheodore Ts'o 		*group = parent_group + flex_size;
658a4912123STheodore Ts'o 		if (*group > ngroups)
659a4912123STheodore Ts'o 			*group = 0;
660f157a4aaSTheodore Ts'o 		return find_group_orlov(sb, parent, group, mode, 0);
661a4912123STheodore Ts'o 	}
662ac27a0ecSDave Kleikamp 
663ac27a0ecSDave Kleikamp 	/*
664ac27a0ecSDave Kleikamp 	 * Try to place the inode in its parent directory
665ac27a0ecSDave Kleikamp 	 */
6662aa9fc4cSAvantika Mathur 	*group = parent_group;
6672aa9fc4cSAvantika Mathur 	desc = ext4_get_group_desc(sb, *group, NULL);
668560671a0SAneesh Kumar K.V 	if (desc && ext4_free_inodes_count(sb, desc) &&
669560671a0SAneesh Kumar K.V 			ext4_free_blks_count(sb, desc))
6702aa9fc4cSAvantika Mathur 		return 0;
671ac27a0ecSDave Kleikamp 
672ac27a0ecSDave Kleikamp 	/*
673ac27a0ecSDave Kleikamp 	 * We're going to place this inode in a different blockgroup from its
674ac27a0ecSDave Kleikamp 	 * parent.  We want to cause files in a common directory to all land in
675ac27a0ecSDave Kleikamp 	 * the same blockgroup.  But we want files which are in a different
676ac27a0ecSDave Kleikamp 	 * directory which shares a blockgroup with our parent to land in a
677ac27a0ecSDave Kleikamp 	 * different blockgroup.
678ac27a0ecSDave Kleikamp 	 *
679ac27a0ecSDave Kleikamp 	 * So add our directory's i_ino into the starting point for the hash.
680ac27a0ecSDave Kleikamp 	 */
6812aa9fc4cSAvantika Mathur 	*group = (*group + parent->i_ino) % ngroups;
682ac27a0ecSDave Kleikamp 
683ac27a0ecSDave Kleikamp 	/*
684ac27a0ecSDave Kleikamp 	 * Use a quadratic hash to find a group with a free inode and some free
685ac27a0ecSDave Kleikamp 	 * blocks.
686ac27a0ecSDave Kleikamp 	 */
687ac27a0ecSDave Kleikamp 	for (i = 1; i < ngroups; i <<= 1) {
6882aa9fc4cSAvantika Mathur 		*group += i;
6892aa9fc4cSAvantika Mathur 		if (*group >= ngroups)
6902aa9fc4cSAvantika Mathur 			*group -= ngroups;
6912aa9fc4cSAvantika Mathur 		desc = ext4_get_group_desc(sb, *group, NULL);
692560671a0SAneesh Kumar K.V 		if (desc && ext4_free_inodes_count(sb, desc) &&
693560671a0SAneesh Kumar K.V 				ext4_free_blks_count(sb, desc))
6942aa9fc4cSAvantika Mathur 			return 0;
695ac27a0ecSDave Kleikamp 	}
696ac27a0ecSDave Kleikamp 
697ac27a0ecSDave Kleikamp 	/*
698ac27a0ecSDave Kleikamp 	 * That failed: try linear search for a free inode, even if that group
699ac27a0ecSDave Kleikamp 	 * has no free blocks.
700ac27a0ecSDave Kleikamp 	 */
7012aa9fc4cSAvantika Mathur 	*group = parent_group;
702ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
7032aa9fc4cSAvantika Mathur 		if (++*group >= ngroups)
7042aa9fc4cSAvantika Mathur 			*group = 0;
7052aa9fc4cSAvantika Mathur 		desc = ext4_get_group_desc(sb, *group, NULL);
706560671a0SAneesh Kumar K.V 		if (desc && ext4_free_inodes_count(sb, desc))
7072aa9fc4cSAvantika Mathur 			return 0;
708ac27a0ecSDave Kleikamp 	}
709ac27a0ecSDave Kleikamp 
710ac27a0ecSDave Kleikamp 	return -1;
711ac27a0ecSDave Kleikamp }
712ac27a0ecSDave Kleikamp 
713ac27a0ecSDave Kleikamp /*
71439341867SAneesh Kumar K.V  * claim the inode from the inode bitmap. If the group
715955ce5f5SAneesh Kumar K.V  * is uninit we need to take the groups's ext4_group_lock
71639341867SAneesh Kumar K.V  * and clear the uninit flag. The inode bitmap update
71739341867SAneesh Kumar K.V  * and group desc uninit flag clear should be done
718955ce5f5SAneesh Kumar K.V  * after holding ext4_group_lock so that ext4_read_inode_bitmap
71939341867SAneesh Kumar K.V  * doesn't race with the ext4_claim_inode
72039341867SAneesh Kumar K.V  */
72139341867SAneesh Kumar K.V static int ext4_claim_inode(struct super_block *sb,
72239341867SAneesh Kumar K.V 			struct buffer_head *inode_bitmap_bh,
72339341867SAneesh Kumar K.V 			unsigned long ino, ext4_group_t group, int mode)
72439341867SAneesh Kumar K.V {
72539341867SAneesh Kumar K.V 	int free = 0, retval = 0, count;
72639341867SAneesh Kumar K.V 	struct ext4_sb_info *sbi = EXT4_SB(sb);
72739341867SAneesh Kumar K.V 	struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL);
72839341867SAneesh Kumar K.V 
729955ce5f5SAneesh Kumar K.V 	ext4_lock_group(sb, group);
73039341867SAneesh Kumar K.V 	if (ext4_set_bit(ino, inode_bitmap_bh->b_data)) {
73139341867SAneesh Kumar K.V 		/* not a free inode */
73239341867SAneesh Kumar K.V 		retval = 1;
73339341867SAneesh Kumar K.V 		goto err_ret;
73439341867SAneesh Kumar K.V 	}
73539341867SAneesh Kumar K.V 	ino++;
73639341867SAneesh Kumar K.V 	if ((group == 0 && ino < EXT4_FIRST_INO(sb)) ||
73739341867SAneesh Kumar K.V 			ino > EXT4_INODES_PER_GROUP(sb)) {
738955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, group);
73939341867SAneesh Kumar K.V 		ext4_error(sb, __func__,
74039341867SAneesh Kumar K.V 			   "reserved inode or inode > inodes count - "
74139341867SAneesh Kumar K.V 			   "block_group = %u, inode=%lu", group,
74239341867SAneesh Kumar K.V 			   ino + group * EXT4_INODES_PER_GROUP(sb));
74339341867SAneesh Kumar K.V 		return 1;
74439341867SAneesh Kumar K.V 	}
74539341867SAneesh Kumar K.V 	/* If we didn't allocate from within the initialized part of the inode
74639341867SAneesh Kumar K.V 	 * table then we need to initialize up to this inode. */
74739341867SAneesh Kumar K.V 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
74839341867SAneesh Kumar K.V 
74939341867SAneesh Kumar K.V 		if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
75039341867SAneesh Kumar K.V 			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
75139341867SAneesh Kumar K.V 			/* When marking the block group with
75239341867SAneesh Kumar K.V 			 * ~EXT4_BG_INODE_UNINIT we don't want to depend
75339341867SAneesh Kumar K.V 			 * on the value of bg_itable_unused even though
75439341867SAneesh Kumar K.V 			 * mke2fs could have initialized the same for us.
75539341867SAneesh Kumar K.V 			 * Instead we calculated the value below
75639341867SAneesh Kumar K.V 			 */
75739341867SAneesh Kumar K.V 
75839341867SAneesh Kumar K.V 			free = 0;
75939341867SAneesh Kumar K.V 		} else {
76039341867SAneesh Kumar K.V 			free = EXT4_INODES_PER_GROUP(sb) -
76139341867SAneesh Kumar K.V 				ext4_itable_unused_count(sb, gdp);
76239341867SAneesh Kumar K.V 		}
76339341867SAneesh Kumar K.V 
76439341867SAneesh Kumar K.V 		/*
76539341867SAneesh Kumar K.V 		 * Check the relative inode number against the last used
76639341867SAneesh Kumar K.V 		 * relative inode number in this group. if it is greater
76739341867SAneesh Kumar K.V 		 * we need to  update the bg_itable_unused count
76839341867SAneesh Kumar K.V 		 *
76939341867SAneesh Kumar K.V 		 */
77039341867SAneesh Kumar K.V 		if (ino > free)
77139341867SAneesh Kumar K.V 			ext4_itable_unused_set(sb, gdp,
77239341867SAneesh Kumar K.V 					(EXT4_INODES_PER_GROUP(sb) - ino));
77339341867SAneesh Kumar K.V 	}
77439341867SAneesh Kumar K.V 	count = ext4_free_inodes_count(sb, gdp) - 1;
77539341867SAneesh Kumar K.V 	ext4_free_inodes_set(sb, gdp, count);
77639341867SAneesh Kumar K.V 	if (S_ISDIR(mode)) {
77739341867SAneesh Kumar K.V 		count = ext4_used_dirs_count(sb, gdp) + 1;
77839341867SAneesh Kumar K.V 		ext4_used_dirs_set(sb, gdp, count);
7797d39db14STheodore Ts'o 		if (sbi->s_log_groups_per_flex) {
7807d39db14STheodore Ts'o 			ext4_group_t f = ext4_flex_group(sbi, group);
7817d39db14STheodore Ts'o 
7827d39db14STheodore Ts'o 			atomic_inc(&sbi->s_flex_groups[f].free_inodes);
7837d39db14STheodore Ts'o 		}
78439341867SAneesh Kumar K.V 	}
78539341867SAneesh Kumar K.V 	gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp);
78639341867SAneesh Kumar K.V err_ret:
787955ce5f5SAneesh Kumar K.V 	ext4_unlock_group(sb, group);
78839341867SAneesh Kumar K.V 	return retval;
78939341867SAneesh Kumar K.V }
79039341867SAneesh Kumar K.V 
79139341867SAneesh Kumar K.V /*
792ac27a0ecSDave Kleikamp  * There are two policies for allocating an inode.  If the new inode is
793ac27a0ecSDave Kleikamp  * a directory, then a forward search is made for a block group with both
794ac27a0ecSDave Kleikamp  * free space and a low directory-to-inode ratio; if that fails, then of
795ac27a0ecSDave Kleikamp  * the groups with above-average free space, that group with the fewest
796ac27a0ecSDave Kleikamp  * directories already is chosen.
797ac27a0ecSDave Kleikamp  *
798ac27a0ecSDave Kleikamp  * For other inodes, search forward from the parent directory's block
799ac27a0ecSDave Kleikamp  * group to find a free inode.
800ac27a0ecSDave Kleikamp  */
801f157a4aaSTheodore Ts'o struct inode *ext4_new_inode(handle_t *handle, struct inode *dir, int mode,
80211013911SAndreas Dilger 			     const struct qstr *qstr, __u32 goal)
803ac27a0ecSDave Kleikamp {
804ac27a0ecSDave Kleikamp 	struct super_block *sb;
8053300bedaSAneesh Kumar K.V 	struct buffer_head *inode_bitmap_bh = NULL;
8063300bedaSAneesh Kumar K.V 	struct buffer_head *group_desc_bh;
8078df9675fSTheodore Ts'o 	ext4_group_t ngroups, group = 0;
808ac27a0ecSDave Kleikamp 	unsigned long ino = 0;
809ac27a0ecSDave Kleikamp 	struct inode *inode;
810617ba13bSMingming Cao 	struct ext4_group_desc *gdp = NULL;
811617ba13bSMingming Cao 	struct ext4_inode_info *ei;
812617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
81339341867SAneesh Kumar K.V 	int ret2, err = 0;
814ac27a0ecSDave Kleikamp 	struct inode *ret;
8152aa9fc4cSAvantika Mathur 	ext4_group_t i;
8162aa9fc4cSAvantika Mathur 	int free = 0;
8172842c3b5STheodore Ts'o 	static int once = 1;
818772cb7c8SJose R. Santos 	ext4_group_t flex_group;
819ac27a0ecSDave Kleikamp 
820ac27a0ecSDave Kleikamp 	/* Cannot create files in a deleted directory */
821ac27a0ecSDave Kleikamp 	if (!dir || !dir->i_nlink)
822ac27a0ecSDave Kleikamp 		return ERR_PTR(-EPERM);
823ac27a0ecSDave Kleikamp 
824ac27a0ecSDave Kleikamp 	sb = dir->i_sb;
8258df9675fSTheodore Ts'o 	ngroups = ext4_get_groups_count(sb);
8269bffad1eSTheodore Ts'o 	trace_ext4_request_inode(dir, mode);
827ac27a0ecSDave Kleikamp 	inode = new_inode(sb);
828ac27a0ecSDave Kleikamp 	if (!inode)
829ac27a0ecSDave Kleikamp 		return ERR_PTR(-ENOMEM);
830617ba13bSMingming Cao 	ei = EXT4_I(inode);
831617ba13bSMingming Cao 	sbi = EXT4_SB(sb);
832772cb7c8SJose R. Santos 
83311013911SAndreas Dilger 	if (!goal)
83411013911SAndreas Dilger 		goal = sbi->s_inode_goal;
83511013911SAndreas Dilger 
836e6462869SJohann Lombardi 	if (goal && goal <= le32_to_cpu(sbi->s_es->s_inodes_count)) {
83711013911SAndreas Dilger 		group = (goal - 1) / EXT4_INODES_PER_GROUP(sb);
83811013911SAndreas Dilger 		ino = (goal - 1) % EXT4_INODES_PER_GROUP(sb);
83911013911SAndreas Dilger 		ret2 = 0;
84011013911SAndreas Dilger 		goto got_group;
84111013911SAndreas Dilger 	}
84211013911SAndreas Dilger 
843a4912123STheodore Ts'o 	if (sbi->s_log_groups_per_flex && test_opt(sb, OLDALLOC)) {
844772cb7c8SJose R. Santos 		ret2 = find_group_flex(sb, dir, &group);
84505bf9e83STheodore Ts'o 		if (ret2 == -1) {
846a4912123STheodore Ts'o 			ret2 = find_group_other(sb, dir, &group, mode);
8476b82f3cbSChuck Ebbert 			if (ret2 == 0 && once) {
8482842c3b5STheodore Ts'o 				once = 0;
84905bf9e83STheodore Ts'o 				printk(KERN_NOTICE "ext4: find_group_flex "
85005bf9e83STheodore Ts'o 				       "failed, fallback succeeded dir %lu\n",
85105bf9e83STheodore Ts'o 				       dir->i_ino);
85205bf9e83STheodore Ts'o 			}
8536b82f3cbSChuck Ebbert 		}
854772cb7c8SJose R. Santos 		goto got_group;
855772cb7c8SJose R. Santos 	}
856772cb7c8SJose R. Santos 
857ac27a0ecSDave Kleikamp 	if (S_ISDIR(mode)) {
858ac27a0ecSDave Kleikamp 		if (test_opt(sb, OLDALLOC))
8592aa9fc4cSAvantika Mathur 			ret2 = find_group_dir(sb, dir, &group);
860ac27a0ecSDave Kleikamp 		else
861f157a4aaSTheodore Ts'o 			ret2 = find_group_orlov(sb, dir, &group, mode, qstr);
862ac27a0ecSDave Kleikamp 	} else
863a4912123STheodore Ts'o 		ret2 = find_group_other(sb, dir, &group, mode);
864ac27a0ecSDave Kleikamp 
865772cb7c8SJose R. Santos got_group:
866a4912123STheodore Ts'o 	EXT4_I(dir)->i_last_alloc_group = group;
867ac27a0ecSDave Kleikamp 	err = -ENOSPC;
8682aa9fc4cSAvantika Mathur 	if (ret2 == -1)
869ac27a0ecSDave Kleikamp 		goto out;
870ac27a0ecSDave Kleikamp 
87111013911SAndreas Dilger 	for (i = 0; i < ngroups; i++, ino = 0) {
872ac27a0ecSDave Kleikamp 		err = -EIO;
873ac27a0ecSDave Kleikamp 
8743300bedaSAneesh Kumar K.V 		gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
875ac27a0ecSDave Kleikamp 		if (!gdp)
876ac27a0ecSDave Kleikamp 			goto fail;
877ac27a0ecSDave Kleikamp 
8783300bedaSAneesh Kumar K.V 		brelse(inode_bitmap_bh);
8793300bedaSAneesh Kumar K.V 		inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
8803300bedaSAneesh Kumar K.V 		if (!inode_bitmap_bh)
881ac27a0ecSDave Kleikamp 			goto fail;
882ac27a0ecSDave Kleikamp 
883ac27a0ecSDave Kleikamp repeat_in_this_group:
884617ba13bSMingming Cao 		ino = ext4_find_next_zero_bit((unsigned long *)
8853300bedaSAneesh Kumar K.V 					      inode_bitmap_bh->b_data,
8863300bedaSAneesh Kumar K.V 					      EXT4_INODES_PER_GROUP(sb), ino);
8873300bedaSAneesh Kumar K.V 
888617ba13bSMingming Cao 		if (ino < EXT4_INODES_PER_GROUP(sb)) {
889ac27a0ecSDave Kleikamp 
8903300bedaSAneesh Kumar K.V 			BUFFER_TRACE(inode_bitmap_bh, "get_write_access");
8913300bedaSAneesh Kumar K.V 			err = ext4_journal_get_write_access(handle,
8923300bedaSAneesh Kumar K.V 							    inode_bitmap_bh);
893ac27a0ecSDave Kleikamp 			if (err)
894ac27a0ecSDave Kleikamp 				goto fail;
895ac27a0ecSDave Kleikamp 
89639341867SAneesh Kumar K.V 			BUFFER_TRACE(group_desc_bh, "get_write_access");
89739341867SAneesh Kumar K.V 			err = ext4_journal_get_write_access(handle,
89839341867SAneesh Kumar K.V 								group_desc_bh);
89939341867SAneesh Kumar K.V 			if (err)
90039341867SAneesh Kumar K.V 				goto fail;
90139341867SAneesh Kumar K.V 			if (!ext4_claim_inode(sb, inode_bitmap_bh,
90239341867SAneesh Kumar K.V 						ino, group, mode)) {
903ac27a0ecSDave Kleikamp 				/* we won it */
9043300bedaSAneesh Kumar K.V 				BUFFER_TRACE(inode_bitmap_bh,
9050390131bSFrank Mayhar 					"call ext4_handle_dirty_metadata");
9060390131bSFrank Mayhar 				err = ext4_handle_dirty_metadata(handle,
9070390131bSFrank Mayhar 								 inode,
9083300bedaSAneesh Kumar K.V 							inode_bitmap_bh);
909ac27a0ecSDave Kleikamp 				if (err)
910ac27a0ecSDave Kleikamp 					goto fail;
91139341867SAneesh Kumar K.V 				/* zero bit is inode number 1*/
91239341867SAneesh Kumar K.V 				ino++;
913ac27a0ecSDave Kleikamp 				goto got;
914ac27a0ecSDave Kleikamp 			}
915ac27a0ecSDave Kleikamp 			/* we lost it */
9163300bedaSAneesh Kumar K.V 			ext4_handle_release_buffer(handle, inode_bitmap_bh);
91739341867SAneesh Kumar K.V 			ext4_handle_release_buffer(handle, group_desc_bh);
918ac27a0ecSDave Kleikamp 
919617ba13bSMingming Cao 			if (++ino < EXT4_INODES_PER_GROUP(sb))
920ac27a0ecSDave Kleikamp 				goto repeat_in_this_group;
921ac27a0ecSDave Kleikamp 		}
922ac27a0ecSDave Kleikamp 
923ac27a0ecSDave Kleikamp 		/*
924ac27a0ecSDave Kleikamp 		 * This case is possible in concurrent environment.  It is very
925ac27a0ecSDave Kleikamp 		 * rare.  We cannot repeat the find_group_xxx() call because
926ac27a0ecSDave Kleikamp 		 * that will simply return the same blockgroup, because the
927ac27a0ecSDave Kleikamp 		 * group descriptor metadata has not yet been updated.
928ac27a0ecSDave Kleikamp 		 * So we just go onto the next blockgroup.
929ac27a0ecSDave Kleikamp 		 */
9308df9675fSTheodore Ts'o 		if (++group == ngroups)
931ac27a0ecSDave Kleikamp 			group = 0;
932ac27a0ecSDave Kleikamp 	}
933ac27a0ecSDave Kleikamp 	err = -ENOSPC;
934ac27a0ecSDave Kleikamp 	goto out;
935ac27a0ecSDave Kleikamp 
936ac27a0ecSDave Kleikamp got:
937717d50e4SAndreas Dilger 	/* We may have to initialize the block bitmap if it isn't already */
938717d50e4SAndreas Dilger 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
939717d50e4SAndreas Dilger 	    gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
9403300bedaSAneesh Kumar K.V 		struct buffer_head *block_bitmap_bh;
941717d50e4SAndreas Dilger 
9423300bedaSAneesh Kumar K.V 		block_bitmap_bh = ext4_read_block_bitmap(sb, group);
9433300bedaSAneesh Kumar K.V 		BUFFER_TRACE(block_bitmap_bh, "get block bitmap access");
9443300bedaSAneesh Kumar K.V 		err = ext4_journal_get_write_access(handle, block_bitmap_bh);
945717d50e4SAndreas Dilger 		if (err) {
9463300bedaSAneesh Kumar K.V 			brelse(block_bitmap_bh);
947717d50e4SAndreas Dilger 			goto fail;
948717d50e4SAndreas Dilger 		}
949717d50e4SAndreas Dilger 
950717d50e4SAndreas Dilger 		free = 0;
951955ce5f5SAneesh Kumar K.V 		ext4_lock_group(sb, group);
952717d50e4SAndreas Dilger 		/* recheck and clear flag under lock if we still need to */
953717d50e4SAndreas Dilger 		if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
954717d50e4SAndreas Dilger 			free = ext4_free_blocks_after_init(sb, group, gdp);
9553300bedaSAneesh Kumar K.V 			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
956560671a0SAneesh Kumar K.V 			ext4_free_blks_set(sb, gdp, free);
95723712a9cSFrederic Bohe 			gdp->bg_checksum = ext4_group_desc_csum(sbi, group,
95823712a9cSFrederic Bohe 								gdp);
959717d50e4SAndreas Dilger 		}
960955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, group);
961717d50e4SAndreas Dilger 
962717d50e4SAndreas Dilger 		/* Don't need to dirty bitmap block if we didn't change it */
963717d50e4SAndreas Dilger 		if (free) {
9643300bedaSAneesh Kumar K.V 			BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
9650390131bSFrank Mayhar 			err = ext4_handle_dirty_metadata(handle,
9663300bedaSAneesh Kumar K.V 							NULL, block_bitmap_bh);
967717d50e4SAndreas Dilger 		}
968717d50e4SAndreas Dilger 
9693300bedaSAneesh Kumar K.V 		brelse(block_bitmap_bh);
970717d50e4SAndreas Dilger 		if (err)
971717d50e4SAndreas Dilger 			goto fail;
972717d50e4SAndreas Dilger 	}
9733300bedaSAneesh Kumar K.V 	BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata");
9743300bedaSAneesh Kumar K.V 	err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh);
97539341867SAneesh Kumar K.V 	if (err)
97639341867SAneesh Kumar K.V 		goto fail;
977ac27a0ecSDave Kleikamp 
978ac27a0ecSDave Kleikamp 	percpu_counter_dec(&sbi->s_freeinodes_counter);
979ac27a0ecSDave Kleikamp 	if (S_ISDIR(mode))
980ac27a0ecSDave Kleikamp 		percpu_counter_inc(&sbi->s_dirs_counter);
981ac27a0ecSDave Kleikamp 	sb->s_dirt = 1;
982ac27a0ecSDave Kleikamp 
983772cb7c8SJose R. Santos 	if (sbi->s_log_groups_per_flex) {
984772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, group);
9859f24e420STheodore Ts'o 		atomic_dec(&sbi->s_flex_groups[flex_group].free_inodes);
986772cb7c8SJose R. Santos 	}
987772cb7c8SJose R. Santos 
9884c9c544eSDavid Howells 	inode->i_uid = current_fsuid();
989ac27a0ecSDave Kleikamp 	if (test_opt(sb, GRPID))
990ac27a0ecSDave Kleikamp 		inode->i_gid = dir->i_gid;
991ac27a0ecSDave Kleikamp 	else if (dir->i_mode & S_ISGID) {
992ac27a0ecSDave Kleikamp 		inode->i_gid = dir->i_gid;
993ac27a0ecSDave Kleikamp 		if (S_ISDIR(mode))
994ac27a0ecSDave Kleikamp 			mode |= S_ISGID;
995ac27a0ecSDave Kleikamp 	} else
9964c9c544eSDavid Howells 		inode->i_gid = current_fsgid();
997ac27a0ecSDave Kleikamp 	inode->i_mode = mode;
998ac27a0ecSDave Kleikamp 
999717d50e4SAndreas Dilger 	inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
1000ac27a0ecSDave Kleikamp 	/* This is the optimal IO size (for stat), not the fs block size */
1001ac27a0ecSDave Kleikamp 	inode->i_blocks = 0;
1002ef7f3835SKalpak Shah 	inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
1003ef7f3835SKalpak Shah 						       ext4_current_time(inode);
1004ac27a0ecSDave Kleikamp 
1005ac27a0ecSDave Kleikamp 	memset(ei->i_data, 0, sizeof(ei->i_data));
1006ac27a0ecSDave Kleikamp 	ei->i_dir_start_lookup = 0;
1007ac27a0ecSDave Kleikamp 	ei->i_disksize = 0;
1008ac27a0ecSDave Kleikamp 
100942bf0383SAneesh Kumar K.V 	/*
10102dc6b0d4SDuane Griffin 	 * Don't inherit extent flag from directory, amongst others. We set
10112dc6b0d4SDuane Griffin 	 * extent flag on newly created directory and file only if -o extent
10122dc6b0d4SDuane Griffin 	 * mount option is specified
101342bf0383SAneesh Kumar K.V 	 */
10142dc6b0d4SDuane Griffin 	ei->i_flags =
10152dc6b0d4SDuane Griffin 		ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED);
1016ac27a0ecSDave Kleikamp 	ei->i_file_acl = 0;
1017ac27a0ecSDave Kleikamp 	ei->i_dtime = 0;
1018ac27a0ecSDave Kleikamp 	ei->i_block_group = group;
1019a4912123STheodore Ts'o 	ei->i_last_alloc_group = ~0;
1020ac27a0ecSDave Kleikamp 
1021617ba13bSMingming Cao 	ext4_set_inode_flags(inode);
1022ac27a0ecSDave Kleikamp 	if (IS_DIRSYNC(inode))
10230390131bSFrank Mayhar 		ext4_handle_sync(handle);
10246b38e842SAl Viro 	if (insert_inode_locked(inode) < 0) {
10256b38e842SAl Viro 		err = -EINVAL;
10266b38e842SAl Viro 		goto fail_drop;
10276b38e842SAl Viro 	}
1028ac27a0ecSDave Kleikamp 	spin_lock(&sbi->s_next_gen_lock);
1029ac27a0ecSDave Kleikamp 	inode->i_generation = sbi->s_next_generation++;
1030ac27a0ecSDave Kleikamp 	spin_unlock(&sbi->s_next_gen_lock);
1031ac27a0ecSDave Kleikamp 
1032617ba13bSMingming Cao 	ei->i_state = EXT4_STATE_NEW;
1033ef7f3835SKalpak Shah 
1034ef7f3835SKalpak Shah 	ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize;
1035ac27a0ecSDave Kleikamp 
1036ac27a0ecSDave Kleikamp 	ret = inode;
1037a269eb18SJan Kara 	if (vfs_dq_alloc_inode(inode)) {
1038ac27a0ecSDave Kleikamp 		err = -EDQUOT;
1039ac27a0ecSDave Kleikamp 		goto fail_drop;
1040ac27a0ecSDave Kleikamp 	}
1041ac27a0ecSDave Kleikamp 
1042617ba13bSMingming Cao 	err = ext4_init_acl(handle, inode, dir);
1043ac27a0ecSDave Kleikamp 	if (err)
1044ac27a0ecSDave Kleikamp 		goto fail_free_drop;
1045ac27a0ecSDave Kleikamp 
1046617ba13bSMingming Cao 	err = ext4_init_security(handle, inode, dir);
1047ac27a0ecSDave Kleikamp 	if (err)
1048ac27a0ecSDave Kleikamp 		goto fail_free_drop;
1049ac27a0ecSDave Kleikamp 
105083982b6fSTheodore Ts'o 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
1051e4079a11SEric Sandeen 		/* set extent flag only for directory, file and normal symlink*/
1052e65187e6SAneesh Kumar K.V 		if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
1053a86c6181SAlex Tomas 			EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL;
1054a86c6181SAlex Tomas 			ext4_ext_tree_init(handle, inode);
1055a86c6181SAlex Tomas 		}
105642bf0383SAneesh Kumar K.V 	}
1057ac27a0ecSDave Kleikamp 
10588753e88fSAneesh Kumar K.V 	err = ext4_mark_inode_dirty(handle, inode);
10598753e88fSAneesh Kumar K.V 	if (err) {
10608753e88fSAneesh Kumar K.V 		ext4_std_error(sb, err);
10618753e88fSAneesh Kumar K.V 		goto fail_free_drop;
10628753e88fSAneesh Kumar K.V 	}
10638753e88fSAneesh Kumar K.V 
1064617ba13bSMingming Cao 	ext4_debug("allocating inode %lu\n", inode->i_ino);
10659bffad1eSTheodore Ts'o 	trace_ext4_allocate_inode(inode, dir, mode);
1066ac27a0ecSDave Kleikamp 	goto really_out;
1067ac27a0ecSDave Kleikamp fail:
1068617ba13bSMingming Cao 	ext4_std_error(sb, err);
1069ac27a0ecSDave Kleikamp out:
1070ac27a0ecSDave Kleikamp 	iput(inode);
1071ac27a0ecSDave Kleikamp 	ret = ERR_PTR(err);
1072ac27a0ecSDave Kleikamp really_out:
10733300bedaSAneesh Kumar K.V 	brelse(inode_bitmap_bh);
1074ac27a0ecSDave Kleikamp 	return ret;
1075ac27a0ecSDave Kleikamp 
1076ac27a0ecSDave Kleikamp fail_free_drop:
1077a269eb18SJan Kara 	vfs_dq_free_inode(inode);
1078ac27a0ecSDave Kleikamp 
1079ac27a0ecSDave Kleikamp fail_drop:
1080a269eb18SJan Kara 	vfs_dq_drop(inode);
1081ac27a0ecSDave Kleikamp 	inode->i_flags |= S_NOQUOTA;
1082ac27a0ecSDave Kleikamp 	inode->i_nlink = 0;
10836b38e842SAl Viro 	unlock_new_inode(inode);
1084ac27a0ecSDave Kleikamp 	iput(inode);
10853300bedaSAneesh Kumar K.V 	brelse(inode_bitmap_bh);
1086ac27a0ecSDave Kleikamp 	return ERR_PTR(err);
1087ac27a0ecSDave Kleikamp }
1088ac27a0ecSDave Kleikamp 
1089ac27a0ecSDave Kleikamp /* Verify that we are loading a valid orphan from disk */
1090617ba13bSMingming Cao struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
1091ac27a0ecSDave Kleikamp {
1092617ba13bSMingming Cao 	unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
1093fd2d4291SAvantika Mathur 	ext4_group_t block_group;
1094ac27a0ecSDave Kleikamp 	int bit;
10951d1fe1eeSDavid Howells 	struct buffer_head *bitmap_bh;
1096ac27a0ecSDave Kleikamp 	struct inode *inode = NULL;
10971d1fe1eeSDavid Howells 	long err = -EIO;
1098ac27a0ecSDave Kleikamp 
1099ac27a0ecSDave Kleikamp 	/* Error cases - e2fsck has already cleaned up for us */
1100ac27a0ecSDave Kleikamp 	if (ino > max_ino) {
110146e665e9SHarvey Harrison 		ext4_warning(sb, __func__,
1102ac27a0ecSDave Kleikamp 			     "bad orphan ino %lu!  e2fsck was run?", ino);
11031d1fe1eeSDavid Howells 		goto error;
1104ac27a0ecSDave Kleikamp 	}
1105ac27a0ecSDave Kleikamp 
1106617ba13bSMingming Cao 	block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
1107617ba13bSMingming Cao 	bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
1108e29d1cdeSEric Sandeen 	bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
1109ac27a0ecSDave Kleikamp 	if (!bitmap_bh) {
111046e665e9SHarvey Harrison 		ext4_warning(sb, __func__,
1111ac27a0ecSDave Kleikamp 			     "inode bitmap error for orphan %lu", ino);
11121d1fe1eeSDavid Howells 		goto error;
1113ac27a0ecSDave Kleikamp 	}
1114ac27a0ecSDave Kleikamp 
1115ac27a0ecSDave Kleikamp 	/* Having the inode bit set should be a 100% indicator that this
1116ac27a0ecSDave Kleikamp 	 * is a valid orphan (no e2fsck run on fs).  Orphans also include
1117ac27a0ecSDave Kleikamp 	 * inodes that were being truncated, so we can't check i_nlink==0.
1118ac27a0ecSDave Kleikamp 	 */
11191d1fe1eeSDavid Howells 	if (!ext4_test_bit(bit, bitmap_bh->b_data))
11201d1fe1eeSDavid Howells 		goto bad_orphan;
11211d1fe1eeSDavid Howells 
11221d1fe1eeSDavid Howells 	inode = ext4_iget(sb, ino);
11231d1fe1eeSDavid Howells 	if (IS_ERR(inode))
11241d1fe1eeSDavid Howells 		goto iget_failed;
11251d1fe1eeSDavid Howells 
112691ef4cafSDuane Griffin 	/*
112791ef4cafSDuane Griffin 	 * If the orphans has i_nlinks > 0 then it should be able to be
112891ef4cafSDuane Griffin 	 * truncated, otherwise it won't be removed from the orphan list
112991ef4cafSDuane Griffin 	 * during processing and an infinite loop will result.
113091ef4cafSDuane Griffin 	 */
113191ef4cafSDuane Griffin 	if (inode->i_nlink && !ext4_can_truncate(inode))
113291ef4cafSDuane Griffin 		goto bad_orphan;
113391ef4cafSDuane Griffin 
11341d1fe1eeSDavid Howells 	if (NEXT_ORPHAN(inode) > max_ino)
11351d1fe1eeSDavid Howells 		goto bad_orphan;
11361d1fe1eeSDavid Howells 	brelse(bitmap_bh);
11371d1fe1eeSDavid Howells 	return inode;
11381d1fe1eeSDavid Howells 
11391d1fe1eeSDavid Howells iget_failed:
11401d1fe1eeSDavid Howells 	err = PTR_ERR(inode);
11411d1fe1eeSDavid Howells 	inode = NULL;
11421d1fe1eeSDavid Howells bad_orphan:
114346e665e9SHarvey Harrison 	ext4_warning(sb, __func__,
1144ac27a0ecSDave Kleikamp 		     "bad orphan inode %lu!  e2fsck was run?", ino);
1145617ba13bSMingming Cao 	printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n",
1146ac27a0ecSDave Kleikamp 	       bit, (unsigned long long)bitmap_bh->b_blocknr,
1147617ba13bSMingming Cao 	       ext4_test_bit(bit, bitmap_bh->b_data));
1148ac27a0ecSDave Kleikamp 	printk(KERN_NOTICE "inode=%p\n", inode);
1149ac27a0ecSDave Kleikamp 	if (inode) {
1150ac27a0ecSDave Kleikamp 		printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
1151ac27a0ecSDave Kleikamp 		       is_bad_inode(inode));
1152ac27a0ecSDave Kleikamp 		printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
1153ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
1154ac27a0ecSDave Kleikamp 		printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
115591ef4cafSDuane Griffin 		printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink);
1156ac27a0ecSDave Kleikamp 		/* Avoid freeing blocks if we got a bad deleted inode */
11571d1fe1eeSDavid Howells 		if (inode->i_nlink == 0)
1158ac27a0ecSDave Kleikamp 			inode->i_blocks = 0;
1159ac27a0ecSDave Kleikamp 		iput(inode);
1160ac27a0ecSDave Kleikamp 	}
1161ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
11621d1fe1eeSDavid Howells error:
11631d1fe1eeSDavid Howells 	return ERR_PTR(err);
1164ac27a0ecSDave Kleikamp }
1165ac27a0ecSDave Kleikamp 
1166617ba13bSMingming Cao unsigned long ext4_count_free_inodes(struct super_block *sb)
1167ac27a0ecSDave Kleikamp {
1168ac27a0ecSDave Kleikamp 	unsigned long desc_count;
1169617ba13bSMingming Cao 	struct ext4_group_desc *gdp;
11708df9675fSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
1171617ba13bSMingming Cao #ifdef EXT4FS_DEBUG
1172617ba13bSMingming Cao 	struct ext4_super_block *es;
1173ac27a0ecSDave Kleikamp 	unsigned long bitmap_count, x;
1174ac27a0ecSDave Kleikamp 	struct buffer_head *bitmap_bh = NULL;
1175ac27a0ecSDave Kleikamp 
1176617ba13bSMingming Cao 	es = EXT4_SB(sb)->s_es;
1177ac27a0ecSDave Kleikamp 	desc_count = 0;
1178ac27a0ecSDave Kleikamp 	bitmap_count = 0;
1179ac27a0ecSDave Kleikamp 	gdp = NULL;
11808df9675fSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
1181617ba13bSMingming Cao 		gdp = ext4_get_group_desc(sb, i, NULL);
1182ac27a0ecSDave Kleikamp 		if (!gdp)
1183ac27a0ecSDave Kleikamp 			continue;
1184560671a0SAneesh Kumar K.V 		desc_count += ext4_free_inodes_count(sb, gdp);
1185ac27a0ecSDave Kleikamp 		brelse(bitmap_bh);
1186e29d1cdeSEric Sandeen 		bitmap_bh = ext4_read_inode_bitmap(sb, i);
1187ac27a0ecSDave Kleikamp 		if (!bitmap_bh)
1188ac27a0ecSDave Kleikamp 			continue;
1189ac27a0ecSDave Kleikamp 
1190617ba13bSMingming Cao 		x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8);
1191c549a95dSEric Sandeen 		printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
1192*785b4b3aSPeng Tao 			(unsigned long) i, ext4_free_inodes_count(sb, gdp), x);
1193ac27a0ecSDave Kleikamp 		bitmap_count += x;
1194ac27a0ecSDave Kleikamp 	}
1195ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
11964776004fSTheodore Ts'o 	printk(KERN_DEBUG "ext4_count_free_inodes: "
11974776004fSTheodore Ts'o 	       "stored = %u, computed = %lu, %lu\n",
1198ac27a0ecSDave Kleikamp 	       le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
1199ac27a0ecSDave Kleikamp 	return desc_count;
1200ac27a0ecSDave Kleikamp #else
1201ac27a0ecSDave Kleikamp 	desc_count = 0;
12028df9675fSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
1203617ba13bSMingming Cao 		gdp = ext4_get_group_desc(sb, i, NULL);
1204ac27a0ecSDave Kleikamp 		if (!gdp)
1205ac27a0ecSDave Kleikamp 			continue;
1206560671a0SAneesh Kumar K.V 		desc_count += ext4_free_inodes_count(sb, gdp);
1207ac27a0ecSDave Kleikamp 		cond_resched();
1208ac27a0ecSDave Kleikamp 	}
1209ac27a0ecSDave Kleikamp 	return desc_count;
1210ac27a0ecSDave Kleikamp #endif
1211ac27a0ecSDave Kleikamp }
1212ac27a0ecSDave Kleikamp 
1213ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
1214617ba13bSMingming Cao unsigned long ext4_count_dirs(struct super_block * sb)
1215ac27a0ecSDave Kleikamp {
1216ac27a0ecSDave Kleikamp 	unsigned long count = 0;
12178df9675fSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
1218ac27a0ecSDave Kleikamp 
12198df9675fSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
1220617ba13bSMingming Cao 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1221ac27a0ecSDave Kleikamp 		if (!gdp)
1222ac27a0ecSDave Kleikamp 			continue;
1223560671a0SAneesh Kumar K.V 		count += ext4_used_dirs_count(sb, gdp);
1224ac27a0ecSDave Kleikamp 	}
1225ac27a0ecSDave Kleikamp 	return count;
1226ac27a0ecSDave Kleikamp }
1227