xref: /linux/fs/ext4/ialloc.c (revision 560671a0d3c9ad2d647fa6d09375a262e1f19c4f)
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>
263dcf5451SChristoph Hellwig #include "ext4.h"
273dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
28ac27a0ecSDave Kleikamp #include "xattr.h"
29ac27a0ecSDave Kleikamp #include "acl.h"
30717d50e4SAndreas Dilger #include "group.h"
31ac27a0ecSDave Kleikamp 
32ac27a0ecSDave Kleikamp /*
33ac27a0ecSDave Kleikamp  * ialloc.c contains the inodes allocation and deallocation routines
34ac27a0ecSDave Kleikamp  */
35ac27a0ecSDave Kleikamp 
36ac27a0ecSDave Kleikamp /*
37ac27a0ecSDave Kleikamp  * The free inodes are managed by bitmaps.  A file system contains several
38ac27a0ecSDave Kleikamp  * blocks groups.  Each group contains 1 bitmap block for blocks, 1 bitmap
39ac27a0ecSDave Kleikamp  * block for inodes, N blocks for the inode table and data blocks.
40ac27a0ecSDave Kleikamp  *
41ac27a0ecSDave Kleikamp  * The file system contains group descriptors which are located after the
42ac27a0ecSDave Kleikamp  * super block.  Each descriptor contains the number of the bitmap block and
43ac27a0ecSDave Kleikamp  * the free blocks count in the block.
44ac27a0ecSDave Kleikamp  */
45ac27a0ecSDave Kleikamp 
46717d50e4SAndreas Dilger /*
47717d50e4SAndreas Dilger  * To avoid calling the atomic setbit hundreds or thousands of times, we only
48717d50e4SAndreas Dilger  * need to use it within a single byte (to ensure we get endianness right).
49717d50e4SAndreas Dilger  * We can use memset for the rest of the bitmap as there are no other users.
50717d50e4SAndreas Dilger  */
51717d50e4SAndreas Dilger void mark_bitmap_end(int start_bit, int end_bit, char *bitmap)
52717d50e4SAndreas Dilger {
53717d50e4SAndreas Dilger 	int i;
54717d50e4SAndreas Dilger 
55717d50e4SAndreas Dilger 	if (start_bit >= end_bit)
56717d50e4SAndreas Dilger 		return;
57717d50e4SAndreas Dilger 
58717d50e4SAndreas Dilger 	ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit);
59717d50e4SAndreas Dilger 	for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
60717d50e4SAndreas Dilger 		ext4_set_bit(i, bitmap);
61717d50e4SAndreas Dilger 	if (i < end_bit)
62717d50e4SAndreas Dilger 		memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
63717d50e4SAndreas Dilger }
64717d50e4SAndreas Dilger 
65717d50e4SAndreas Dilger /* Initializes an uninitialized inode bitmap */
66fd2d4291SAvantika Mathur unsigned ext4_init_inode_bitmap(struct super_block *sb, struct buffer_head *bh,
67fd2d4291SAvantika Mathur 				ext4_group_t block_group,
68717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
69717d50e4SAndreas Dilger {
70717d50e4SAndreas Dilger 	struct ext4_sb_info *sbi = EXT4_SB(sb);
71717d50e4SAndreas Dilger 
72717d50e4SAndreas Dilger 	J_ASSERT_BH(bh, buffer_locked(bh));
73717d50e4SAndreas Dilger 
74717d50e4SAndreas Dilger 	/* If checksum is bad mark all blocks and inodes use to prevent
75717d50e4SAndreas Dilger 	 * allocation, essentially implementing a per-group read-only flag. */
76717d50e4SAndreas Dilger 	if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) {
77a9df9a49STheodore Ts'o 		ext4_error(sb, __func__, "Checksum bad for group %u",
78717d50e4SAndreas Dilger 			   block_group);
79*560671a0SAneesh Kumar K.V 		ext4_free_blks_set(sb, gdp, 0);
80*560671a0SAneesh Kumar K.V 		ext4_free_inodes_set(sb, gdp, 0);
81*560671a0SAneesh Kumar K.V 		ext4_itable_unused_set(sb, gdp, 0);
82717d50e4SAndreas Dilger 		memset(bh->b_data, 0xff, sb->s_blocksize);
83717d50e4SAndreas Dilger 		return 0;
84717d50e4SAndreas Dilger 	}
85717d50e4SAndreas Dilger 
86717d50e4SAndreas Dilger 	memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8);
87717d50e4SAndreas Dilger 	mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), EXT4_BLOCKS_PER_GROUP(sb),
88717d50e4SAndreas Dilger 			bh->b_data);
89717d50e4SAndreas Dilger 
90717d50e4SAndreas Dilger 	return EXT4_INODES_PER_GROUP(sb);
91717d50e4SAndreas Dilger }
92ac27a0ecSDave Kleikamp 
93ac27a0ecSDave Kleikamp /*
94ac27a0ecSDave Kleikamp  * Read the inode allocation bitmap for a given block_group, reading
95ac27a0ecSDave Kleikamp  * into the specified slot in the superblock's bitmap cache.
96ac27a0ecSDave Kleikamp  *
97ac27a0ecSDave Kleikamp  * Return buffer_head of bitmap on success or NULL.
98ac27a0ecSDave Kleikamp  */
99ac27a0ecSDave Kleikamp static struct buffer_head *
100e29d1cdeSEric Sandeen ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group)
101ac27a0ecSDave Kleikamp {
102617ba13bSMingming Cao 	struct ext4_group_desc *desc;
103ac27a0ecSDave Kleikamp 	struct buffer_head *bh = NULL;
104e29d1cdeSEric Sandeen 	ext4_fsblk_t bitmap_blk;
105ac27a0ecSDave Kleikamp 
106617ba13bSMingming Cao 	desc = ext4_get_group_desc(sb, block_group, NULL);
107ac27a0ecSDave Kleikamp 	if (!desc)
108e29d1cdeSEric Sandeen 		return NULL;
109e29d1cdeSEric Sandeen 	bitmap_blk = ext4_inode_bitmap(sb, desc);
110e29d1cdeSEric Sandeen 	bh = sb_getblk(sb, bitmap_blk);
111e29d1cdeSEric Sandeen 	if (unlikely(!bh)) {
112e29d1cdeSEric Sandeen 		ext4_error(sb, __func__,
113ac27a0ecSDave Kleikamp 			    "Cannot read inode bitmap - "
114a9df9a49STheodore Ts'o 			    "block_group = %u, inode_bitmap = %llu",
115e29d1cdeSEric Sandeen 			    block_group, bitmap_blk);
116e29d1cdeSEric Sandeen 		return NULL;
117e29d1cdeSEric Sandeen 	}
118c806e68fSFrederic Bohe 	if (buffer_uptodate(bh) &&
119c806e68fSFrederic Bohe 	    !(desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)))
120e29d1cdeSEric Sandeen 		return bh;
121e29d1cdeSEric Sandeen 
122c806e68fSFrederic Bohe 	lock_buffer(bh);
123b5f10eedSEric Sandeen 	spin_lock(sb_bgl_lock(EXT4_SB(sb), block_group));
124e29d1cdeSEric Sandeen 	if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
125e29d1cdeSEric Sandeen 		ext4_init_inode_bitmap(sb, bh, block_group, desc);
126e29d1cdeSEric Sandeen 		set_buffer_uptodate(bh);
127e29d1cdeSEric Sandeen 		unlock_buffer(bh);
128b5f10eedSEric Sandeen 		spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group));
129e29d1cdeSEric Sandeen 		return bh;
130e29d1cdeSEric Sandeen 	}
131b5f10eedSEric Sandeen 	spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group));
132e29d1cdeSEric Sandeen 	if (bh_submit_read(bh) < 0) {
133e29d1cdeSEric Sandeen 		put_bh(bh);
134e29d1cdeSEric Sandeen 		ext4_error(sb, __func__,
135e29d1cdeSEric Sandeen 			    "Cannot read inode bitmap - "
136a9df9a49STheodore Ts'o 			    "block_group = %u, inode_bitmap = %llu",
137e29d1cdeSEric Sandeen 			    block_group, bitmap_blk);
138e29d1cdeSEric Sandeen 		return NULL;
139e29d1cdeSEric Sandeen 	}
140ac27a0ecSDave Kleikamp 	return bh;
141ac27a0ecSDave Kleikamp }
142ac27a0ecSDave Kleikamp 
143ac27a0ecSDave Kleikamp /*
144ac27a0ecSDave Kleikamp  * NOTE! When we get the inode, we're the only people
145ac27a0ecSDave Kleikamp  * that have access to it, and as such there are no
146ac27a0ecSDave Kleikamp  * race conditions we have to worry about. The inode
147ac27a0ecSDave Kleikamp  * is not on the hash-lists, and it cannot be reached
148ac27a0ecSDave Kleikamp  * through the filesystem because the directory entry
149ac27a0ecSDave Kleikamp  * has been deleted earlier.
150ac27a0ecSDave Kleikamp  *
151ac27a0ecSDave Kleikamp  * HOWEVER: we must make sure that we get no aliases,
152ac27a0ecSDave Kleikamp  * which means that we have to call "clear_inode()"
153ac27a0ecSDave Kleikamp  * _before_ we mark the inode not in use in the inode
154ac27a0ecSDave Kleikamp  * bitmaps. Otherwise a newly created file might use
155ac27a0ecSDave Kleikamp  * the same inode number (not actually the same pointer
156ac27a0ecSDave Kleikamp  * though), and then we'd have two inodes sharing the
157ac27a0ecSDave Kleikamp  * same inode number and space on the harddisk.
158ac27a0ecSDave Kleikamp  */
159617ba13bSMingming Cao void ext4_free_inode(handle_t *handle, struct inode *inode)
160ac27a0ecSDave Kleikamp {
161ac27a0ecSDave Kleikamp 	struct super_block *sb = inode->i_sb;
162ac27a0ecSDave Kleikamp 	int is_directory;
163ac27a0ecSDave Kleikamp 	unsigned long ino;
164ac27a0ecSDave Kleikamp 	struct buffer_head *bitmap_bh = NULL;
165ac27a0ecSDave Kleikamp 	struct buffer_head *bh2;
166fd2d4291SAvantika Mathur 	ext4_group_t block_group;
167ac27a0ecSDave Kleikamp 	unsigned long bit;
168617ba13bSMingming Cao 	struct ext4_group_desc *gdp;
169617ba13bSMingming Cao 	struct ext4_super_block *es;
170617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
171*560671a0SAneesh Kumar K.V 	int fatal = 0, err, count;
172772cb7c8SJose R. Santos 	ext4_group_t flex_group;
173ac27a0ecSDave Kleikamp 
174ac27a0ecSDave Kleikamp 	if (atomic_read(&inode->i_count) > 1) {
1754776004fSTheodore Ts'o 		printk(KERN_ERR "ext4_free_inode: inode has count=%d\n",
176ac27a0ecSDave Kleikamp 		       atomic_read(&inode->i_count));
177ac27a0ecSDave Kleikamp 		return;
178ac27a0ecSDave Kleikamp 	}
179ac27a0ecSDave Kleikamp 	if (inode->i_nlink) {
1804776004fSTheodore Ts'o 		printk(KERN_ERR "ext4_free_inode: inode has nlink=%d\n",
181ac27a0ecSDave Kleikamp 		       inode->i_nlink);
182ac27a0ecSDave Kleikamp 		return;
183ac27a0ecSDave Kleikamp 	}
184ac27a0ecSDave Kleikamp 	if (!sb) {
1854776004fSTheodore Ts'o 		printk(KERN_ERR "ext4_free_inode: inode on "
1864776004fSTheodore Ts'o 		       "nonexistent device\n");
187ac27a0ecSDave Kleikamp 		return;
188ac27a0ecSDave Kleikamp 	}
189617ba13bSMingming Cao 	sbi = EXT4_SB(sb);
190ac27a0ecSDave Kleikamp 
191ac27a0ecSDave Kleikamp 	ino = inode->i_ino;
192617ba13bSMingming Cao 	ext4_debug("freeing inode %lu\n", ino);
193ac27a0ecSDave Kleikamp 
194ac27a0ecSDave Kleikamp 	/*
195ac27a0ecSDave Kleikamp 	 * Note: we must free any quota before locking the superblock,
196ac27a0ecSDave Kleikamp 	 * as writing the quota to disk may need the lock as well.
197ac27a0ecSDave Kleikamp 	 */
198ac27a0ecSDave Kleikamp 	DQUOT_INIT(inode);
199617ba13bSMingming Cao 	ext4_xattr_delete_inode(handle, inode);
200ac27a0ecSDave Kleikamp 	DQUOT_FREE_INODE(inode);
201ac27a0ecSDave Kleikamp 	DQUOT_DROP(inode);
202ac27a0ecSDave Kleikamp 
203ac27a0ecSDave Kleikamp 	is_directory = S_ISDIR(inode->i_mode);
204ac27a0ecSDave Kleikamp 
205ac27a0ecSDave Kleikamp 	/* Do this BEFORE marking the inode not in use or returning an error */
206ac27a0ecSDave Kleikamp 	clear_inode(inode);
207ac27a0ecSDave Kleikamp 
208617ba13bSMingming Cao 	es = EXT4_SB(sb)->s_es;
209617ba13bSMingming Cao 	if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
210617ba13bSMingming Cao 		ext4_error(sb, "ext4_free_inode",
211ac27a0ecSDave Kleikamp 			   "reserved or nonexistent inode %lu", ino);
212ac27a0ecSDave Kleikamp 		goto error_return;
213ac27a0ecSDave Kleikamp 	}
214617ba13bSMingming Cao 	block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
215617ba13bSMingming Cao 	bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
216e29d1cdeSEric Sandeen 	bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
217ac27a0ecSDave Kleikamp 	if (!bitmap_bh)
218ac27a0ecSDave Kleikamp 		goto error_return;
219ac27a0ecSDave Kleikamp 
220ac27a0ecSDave Kleikamp 	BUFFER_TRACE(bitmap_bh, "get_write_access");
221617ba13bSMingming Cao 	fatal = ext4_journal_get_write_access(handle, bitmap_bh);
222ac27a0ecSDave Kleikamp 	if (fatal)
223ac27a0ecSDave Kleikamp 		goto error_return;
224ac27a0ecSDave Kleikamp 
225ac27a0ecSDave Kleikamp 	/* Ok, now we can actually update the inode bitmaps.. */
226617ba13bSMingming Cao 	if (!ext4_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
227ac27a0ecSDave Kleikamp 					bit, bitmap_bh->b_data))
228617ba13bSMingming Cao 		ext4_error(sb, "ext4_free_inode",
229ac27a0ecSDave Kleikamp 			   "bit already cleared for inode %lu", ino);
230ac27a0ecSDave Kleikamp 	else {
231617ba13bSMingming Cao 		gdp = ext4_get_group_desc(sb, block_group, &bh2);
232ac27a0ecSDave Kleikamp 
233ac27a0ecSDave Kleikamp 		BUFFER_TRACE(bh2, "get_write_access");
234617ba13bSMingming Cao 		fatal = ext4_journal_get_write_access(handle, bh2);
235ac27a0ecSDave Kleikamp 		if (fatal) goto error_return;
236ac27a0ecSDave Kleikamp 
237ac27a0ecSDave Kleikamp 		if (gdp) {
238ac27a0ecSDave Kleikamp 			spin_lock(sb_bgl_lock(sbi, block_group));
239*560671a0SAneesh Kumar K.V 			count = ext4_free_inodes_count(sb, gdp) + 1;
240*560671a0SAneesh Kumar K.V 			ext4_free_inodes_set(sb, gdp, count);
241*560671a0SAneesh Kumar K.V 			if (is_directory) {
242*560671a0SAneesh Kumar K.V 				count = ext4_used_dirs_count(sb, gdp) - 1;
243*560671a0SAneesh Kumar K.V 				ext4_used_dirs_set(sb, gdp, count);
244*560671a0SAneesh Kumar K.V 			}
245717d50e4SAndreas Dilger 			gdp->bg_checksum = ext4_group_desc_csum(sbi,
246717d50e4SAndreas Dilger 							block_group, gdp);
247ac27a0ecSDave Kleikamp 			spin_unlock(sb_bgl_lock(sbi, block_group));
248ac27a0ecSDave Kleikamp 			percpu_counter_inc(&sbi->s_freeinodes_counter);
249ac27a0ecSDave Kleikamp 			if (is_directory)
250ac27a0ecSDave Kleikamp 				percpu_counter_dec(&sbi->s_dirs_counter);
251ac27a0ecSDave Kleikamp 
252772cb7c8SJose R. Santos 			if (sbi->s_log_groups_per_flex) {
253772cb7c8SJose R. Santos 				flex_group = ext4_flex_group(sbi, block_group);
254772cb7c8SJose R. Santos 				spin_lock(sb_bgl_lock(sbi, flex_group));
255772cb7c8SJose R. Santos 				sbi->s_flex_groups[flex_group].free_inodes++;
256772cb7c8SJose R. Santos 				spin_unlock(sb_bgl_lock(sbi, flex_group));
257772cb7c8SJose R. Santos 			}
258ac27a0ecSDave Kleikamp 		}
2590390131bSFrank Mayhar 		BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata");
2600390131bSFrank Mayhar 		err = ext4_handle_dirty_metadata(handle, NULL, bh2);
261ac27a0ecSDave Kleikamp 		if (!fatal) fatal = err;
262ac27a0ecSDave Kleikamp 	}
2630390131bSFrank Mayhar 	BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata");
2640390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
265ac27a0ecSDave Kleikamp 	if (!fatal)
266ac27a0ecSDave Kleikamp 		fatal = err;
267ac27a0ecSDave Kleikamp 	sb->s_dirt = 1;
268ac27a0ecSDave Kleikamp error_return:
269ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
270617ba13bSMingming Cao 	ext4_std_error(sb, fatal);
271ac27a0ecSDave Kleikamp }
272ac27a0ecSDave Kleikamp 
273ac27a0ecSDave Kleikamp /*
274ac27a0ecSDave Kleikamp  * There are two policies for allocating an inode.  If the new inode is
275ac27a0ecSDave Kleikamp  * a directory, then a forward search is made for a block group with both
276ac27a0ecSDave Kleikamp  * free space and a low directory-to-inode ratio; if that fails, then of
277ac27a0ecSDave Kleikamp  * the groups with above-average free space, that group with the fewest
278ac27a0ecSDave Kleikamp  * directories already is chosen.
279ac27a0ecSDave Kleikamp  *
280ac27a0ecSDave Kleikamp  * For other inodes, search forward from the parent directory\'s block
281ac27a0ecSDave Kleikamp  * group to find a free inode.
282ac27a0ecSDave Kleikamp  */
2832aa9fc4cSAvantika Mathur static int find_group_dir(struct super_block *sb, struct inode *parent,
2842aa9fc4cSAvantika Mathur 				ext4_group_t *best_group)
285ac27a0ecSDave Kleikamp {
286fd2d4291SAvantika Mathur 	ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
287ac27a0ecSDave Kleikamp 	unsigned int freei, avefreei;
288617ba13bSMingming Cao 	struct ext4_group_desc *desc, *best_desc = NULL;
2892aa9fc4cSAvantika Mathur 	ext4_group_t group;
2902aa9fc4cSAvantika Mathur 	int ret = -1;
291ac27a0ecSDave Kleikamp 
292617ba13bSMingming Cao 	freei = percpu_counter_read_positive(&EXT4_SB(sb)->s_freeinodes_counter);
293ac27a0ecSDave Kleikamp 	avefreei = freei / ngroups;
294ac27a0ecSDave Kleikamp 
295ac27a0ecSDave Kleikamp 	for (group = 0; group < ngroups; group++) {
296ef2fb679SEric Sandeen 		desc = ext4_get_group_desc(sb, group, NULL);
297*560671a0SAneesh Kumar K.V 		if (!desc || !ext4_free_inodes_count(sb, desc))
298ac27a0ecSDave Kleikamp 			continue;
299*560671a0SAneesh Kumar K.V 		if (ext4_free_inodes_count(sb, desc) < avefreei)
300ac27a0ecSDave Kleikamp 			continue;
301ac27a0ecSDave Kleikamp 		if (!best_desc ||
302*560671a0SAneesh Kumar K.V 		    (ext4_free_blks_count(sb, desc) >
303*560671a0SAneesh Kumar K.V 		     ext4_free_blks_count(sb, best_desc))) {
3042aa9fc4cSAvantika Mathur 			*best_group = group;
305ac27a0ecSDave Kleikamp 			best_desc = desc;
3062aa9fc4cSAvantika Mathur 			ret = 0;
307ac27a0ecSDave Kleikamp 		}
308ac27a0ecSDave Kleikamp 	}
3092aa9fc4cSAvantika Mathur 	return ret;
310ac27a0ecSDave Kleikamp }
311ac27a0ecSDave Kleikamp 
312772cb7c8SJose R. Santos #define free_block_ratio 10
313772cb7c8SJose R. Santos 
314772cb7c8SJose R. Santos static int find_group_flex(struct super_block *sb, struct inode *parent,
315772cb7c8SJose R. Santos 			   ext4_group_t *best_group)
316772cb7c8SJose R. Santos {
317772cb7c8SJose R. Santos 	struct ext4_sb_info *sbi = EXT4_SB(sb);
318772cb7c8SJose R. Santos 	struct ext4_group_desc *desc;
319772cb7c8SJose R. Santos 	struct buffer_head *bh;
320772cb7c8SJose R. Santos 	struct flex_groups *flex_group = sbi->s_flex_groups;
321772cb7c8SJose R. Santos 	ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
322772cb7c8SJose R. Santos 	ext4_group_t parent_fbg_group = ext4_flex_group(sbi, parent_group);
323772cb7c8SJose R. Santos 	ext4_group_t ngroups = sbi->s_groups_count;
324772cb7c8SJose R. Santos 	int flex_size = ext4_flex_bg_size(sbi);
325772cb7c8SJose R. Santos 	ext4_group_t best_flex = parent_fbg_group;
326772cb7c8SJose R. Santos 	int blocks_per_flex = sbi->s_blocks_per_group * flex_size;
327772cb7c8SJose R. Santos 	int flexbg_free_blocks;
328772cb7c8SJose R. Santos 	int flex_freeb_ratio;
329772cb7c8SJose R. Santos 	ext4_group_t n_fbg_groups;
330772cb7c8SJose R. Santos 	ext4_group_t i;
331772cb7c8SJose R. Santos 
332772cb7c8SJose R. Santos 	n_fbg_groups = (sbi->s_groups_count + flex_size - 1) >>
333772cb7c8SJose R. Santos 		sbi->s_log_groups_per_flex;
334772cb7c8SJose R. Santos 
335772cb7c8SJose R. Santos find_close_to_parent:
336772cb7c8SJose R. Santos 	flexbg_free_blocks = flex_group[best_flex].free_blocks;
337772cb7c8SJose R. Santos 	flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
338772cb7c8SJose R. Santos 	if (flex_group[best_flex].free_inodes &&
339772cb7c8SJose R. Santos 	    flex_freeb_ratio > free_block_ratio)
340772cb7c8SJose R. Santos 		goto found_flexbg;
341772cb7c8SJose R. Santos 
342772cb7c8SJose R. Santos 	if (best_flex && best_flex == parent_fbg_group) {
343772cb7c8SJose R. Santos 		best_flex--;
344772cb7c8SJose R. Santos 		goto find_close_to_parent;
345772cb7c8SJose R. Santos 	}
346772cb7c8SJose R. Santos 
347772cb7c8SJose R. Santos 	for (i = 0; i < n_fbg_groups; i++) {
348772cb7c8SJose R. Santos 		if (i == parent_fbg_group || i == parent_fbg_group - 1)
349772cb7c8SJose R. Santos 			continue;
350772cb7c8SJose R. Santos 
351772cb7c8SJose R. Santos 		flexbg_free_blocks = flex_group[i].free_blocks;
352772cb7c8SJose R. Santos 		flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
353772cb7c8SJose R. Santos 
354772cb7c8SJose R. Santos 		if (flex_freeb_ratio > free_block_ratio &&
355772cb7c8SJose R. Santos 		    flex_group[i].free_inodes) {
356772cb7c8SJose R. Santos 			best_flex = i;
357772cb7c8SJose R. Santos 			goto found_flexbg;
358772cb7c8SJose R. Santos 		}
359772cb7c8SJose R. Santos 
360c001077fSEric Sandeen 		if (flex_group[best_flex].free_inodes == 0 ||
361772cb7c8SJose R. Santos 		    (flex_group[i].free_blocks >
362772cb7c8SJose R. Santos 		     flex_group[best_flex].free_blocks &&
363772cb7c8SJose R. Santos 		     flex_group[i].free_inodes))
364772cb7c8SJose R. Santos 			best_flex = i;
365772cb7c8SJose R. Santos 	}
366772cb7c8SJose R. Santos 
367772cb7c8SJose R. Santos 	if (!flex_group[best_flex].free_inodes ||
368772cb7c8SJose R. Santos 	    !flex_group[best_flex].free_blocks)
369772cb7c8SJose R. Santos 		return -1;
370772cb7c8SJose R. Santos 
371772cb7c8SJose R. Santos found_flexbg:
372772cb7c8SJose R. Santos 	for (i = best_flex * flex_size; i < ngroups &&
373772cb7c8SJose R. Santos 		     i < (best_flex + 1) * flex_size; i++) {
374772cb7c8SJose R. Santos 		desc = ext4_get_group_desc(sb, i, &bh);
375*560671a0SAneesh Kumar K.V 		if (ext4_free_inodes_count(sb, desc)) {
376772cb7c8SJose R. Santos 			*best_group = i;
377772cb7c8SJose R. Santos 			goto out;
378772cb7c8SJose R. Santos 		}
379772cb7c8SJose R. Santos 	}
380772cb7c8SJose R. Santos 
381772cb7c8SJose R. Santos 	return -1;
382772cb7c8SJose R. Santos out:
383772cb7c8SJose R. Santos 	return 0;
384772cb7c8SJose R. Santos }
385772cb7c8SJose R. Santos 
386ac27a0ecSDave Kleikamp /*
387ac27a0ecSDave Kleikamp  * Orlov's allocator for directories.
388ac27a0ecSDave Kleikamp  *
389ac27a0ecSDave Kleikamp  * We always try to spread first-level directories.
390ac27a0ecSDave Kleikamp  *
391ac27a0ecSDave Kleikamp  * If there are blockgroups with both free inodes and free blocks counts
392ac27a0ecSDave Kleikamp  * not worse than average we return one with smallest directory count.
393ac27a0ecSDave Kleikamp  * Otherwise we simply return a random group.
394ac27a0ecSDave Kleikamp  *
395ac27a0ecSDave Kleikamp  * For the rest rules look so:
396ac27a0ecSDave Kleikamp  *
397ac27a0ecSDave Kleikamp  * It's OK to put directory into a group unless
398ac27a0ecSDave Kleikamp  * it has too many directories already (max_dirs) or
399ac27a0ecSDave Kleikamp  * it has too few free inodes left (min_inodes) or
400ac27a0ecSDave Kleikamp  * it has too few free blocks left (min_blocks) or
401ac27a0ecSDave Kleikamp  * it's already running too large debt (max_debt).
4021cc8dcf5SBenoit Boissinot  * Parent's group is preferred, if it doesn't satisfy these
403ac27a0ecSDave Kleikamp  * conditions we search cyclically through the rest. If none
404ac27a0ecSDave Kleikamp  * of the groups look good we just look for a group with more
405ac27a0ecSDave Kleikamp  * free inodes than average (starting at parent's group).
406ac27a0ecSDave Kleikamp  *
407ac27a0ecSDave Kleikamp  * Debt is incremented each time we allocate a directory and decremented
408ac27a0ecSDave Kleikamp  * when we allocate an inode, within 0--255.
409ac27a0ecSDave Kleikamp  */
410ac27a0ecSDave Kleikamp 
411ac27a0ecSDave Kleikamp #define INODE_COST 64
412ac27a0ecSDave Kleikamp #define BLOCK_COST 256
413ac27a0ecSDave Kleikamp 
4142aa9fc4cSAvantika Mathur static int find_group_orlov(struct super_block *sb, struct inode *parent,
4152aa9fc4cSAvantika Mathur 				ext4_group_t *group)
416ac27a0ecSDave Kleikamp {
417fd2d4291SAvantika Mathur 	ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
418617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
419617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
420fd2d4291SAvantika Mathur 	ext4_group_t ngroups = sbi->s_groups_count;
421617ba13bSMingming Cao 	int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
422ac27a0ecSDave Kleikamp 	unsigned int freei, avefreei;
423617ba13bSMingming Cao 	ext4_fsblk_t freeb, avefreeb;
424617ba13bSMingming Cao 	ext4_fsblk_t blocks_per_dir;
425ac27a0ecSDave Kleikamp 	unsigned int ndirs;
426ac27a0ecSDave Kleikamp 	int max_debt, max_dirs, min_inodes;
427617ba13bSMingming Cao 	ext4_grpblk_t min_blocks;
4282aa9fc4cSAvantika Mathur 	ext4_group_t i;
429617ba13bSMingming Cao 	struct ext4_group_desc *desc;
430ac27a0ecSDave Kleikamp 
431ac27a0ecSDave Kleikamp 	freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
432ac27a0ecSDave Kleikamp 	avefreei = freei / ngroups;
433ac27a0ecSDave Kleikamp 	freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
4343a5b2ecdSMingming Cao 	avefreeb = freeb;
435f4e5bc24SAndrew Morton 	do_div(avefreeb, ngroups);
436ac27a0ecSDave Kleikamp 	ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
437ac27a0ecSDave Kleikamp 
438ac27a0ecSDave Kleikamp 	if ((parent == sb->s_root->d_inode) ||
439617ba13bSMingming Cao 	    (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL)) {
440ac27a0ecSDave Kleikamp 		int best_ndir = inodes_per_group;
4412aa9fc4cSAvantika Mathur 		ext4_group_t grp;
4422aa9fc4cSAvantika Mathur 		int ret = -1;
443ac27a0ecSDave Kleikamp 
4442aa9fc4cSAvantika Mathur 		get_random_bytes(&grp, sizeof(grp));
4452aa9fc4cSAvantika Mathur 		parent_group = (unsigned)grp % ngroups;
446ac27a0ecSDave Kleikamp 		for (i = 0; i < ngroups; i++) {
4472aa9fc4cSAvantika Mathur 			grp = (parent_group + i) % ngroups;
4482aa9fc4cSAvantika Mathur 			desc = ext4_get_group_desc(sb, grp, NULL);
449*560671a0SAneesh Kumar K.V 			if (!desc || !ext4_free_inodes_count(sb, desc))
450ac27a0ecSDave Kleikamp 				continue;
451*560671a0SAneesh Kumar K.V 			if (ext4_used_dirs_count(sb, desc) >= best_ndir)
452ac27a0ecSDave Kleikamp 				continue;
453*560671a0SAneesh Kumar K.V 			if (ext4_free_inodes_count(sb, desc) < avefreei)
454ac27a0ecSDave Kleikamp 				continue;
455*560671a0SAneesh Kumar K.V 			if (ext4_free_blks_count(sb, desc) < avefreeb)
456ac27a0ecSDave Kleikamp 				continue;
4572aa9fc4cSAvantika Mathur 			*group = grp;
4582aa9fc4cSAvantika Mathur 			ret = 0;
459*560671a0SAneesh Kumar K.V 			best_ndir = ext4_used_dirs_count(sb, desc);
460ac27a0ecSDave Kleikamp 		}
4612aa9fc4cSAvantika Mathur 		if (ret == 0)
4622aa9fc4cSAvantika Mathur 			return ret;
463ac27a0ecSDave Kleikamp 		goto fallback;
464ac27a0ecSDave Kleikamp 	}
465ac27a0ecSDave Kleikamp 
466bd81d8eeSLaurent Vivier 	blocks_per_dir = ext4_blocks_count(es) - freeb;
467f4e5bc24SAndrew Morton 	do_div(blocks_per_dir, ndirs);
468ac27a0ecSDave Kleikamp 
469ac27a0ecSDave Kleikamp 	max_dirs = ndirs / ngroups + inodes_per_group / 16;
470ac27a0ecSDave Kleikamp 	min_inodes = avefreei - inodes_per_group / 4;
471617ba13bSMingming Cao 	min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb) / 4;
472ac27a0ecSDave Kleikamp 
4733a5b2ecdSMingming Cao 	max_debt = EXT4_BLOCKS_PER_GROUP(sb);
474f4e5bc24SAndrew Morton 	max_debt /= max_t(int, blocks_per_dir, BLOCK_COST);
475ac27a0ecSDave Kleikamp 	if (max_debt * INODE_COST > inodes_per_group)
476ac27a0ecSDave Kleikamp 		max_debt = inodes_per_group / INODE_COST;
477ac27a0ecSDave Kleikamp 	if (max_debt > 255)
478ac27a0ecSDave Kleikamp 		max_debt = 255;
479ac27a0ecSDave Kleikamp 	if (max_debt == 0)
480ac27a0ecSDave Kleikamp 		max_debt = 1;
481ac27a0ecSDave Kleikamp 
482ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
4832aa9fc4cSAvantika Mathur 		*group = (parent_group + i) % ngroups;
4842aa9fc4cSAvantika Mathur 		desc = ext4_get_group_desc(sb, *group, NULL);
485*560671a0SAneesh Kumar K.V 		if (!desc || !ext4_free_inodes_count(sb, desc))
486ac27a0ecSDave Kleikamp 			continue;
487*560671a0SAneesh Kumar K.V 		if (ext4_used_dirs_count(sb, desc) >= max_dirs)
488ac27a0ecSDave Kleikamp 			continue;
489*560671a0SAneesh Kumar K.V 		if (ext4_free_inodes_count(sb, desc) < min_inodes)
490ac27a0ecSDave Kleikamp 			continue;
491*560671a0SAneesh Kumar K.V 		if (ext4_free_blks_count(sb, desc) < min_blocks)
492ac27a0ecSDave Kleikamp 			continue;
4932aa9fc4cSAvantika Mathur 		return 0;
494ac27a0ecSDave Kleikamp 	}
495ac27a0ecSDave Kleikamp 
496ac27a0ecSDave Kleikamp fallback:
497ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
4982aa9fc4cSAvantika Mathur 		*group = (parent_group + i) % ngroups;
4992aa9fc4cSAvantika Mathur 		desc = ext4_get_group_desc(sb, *group, NULL);
500*560671a0SAneesh Kumar K.V 		if (desc && ext4_free_inodes_count(sb, desc) &&
501*560671a0SAneesh Kumar K.V 			ext4_free_inodes_count(sb, desc) >= avefreei)
5022aa9fc4cSAvantika Mathur 			return 0;
503ac27a0ecSDave Kleikamp 	}
504ac27a0ecSDave Kleikamp 
505ac27a0ecSDave Kleikamp 	if (avefreei) {
506ac27a0ecSDave Kleikamp 		/*
507ac27a0ecSDave Kleikamp 		 * The free-inodes counter is approximate, and for really small
508ac27a0ecSDave Kleikamp 		 * filesystems the above test can fail to find any blockgroups
509ac27a0ecSDave Kleikamp 		 */
510ac27a0ecSDave Kleikamp 		avefreei = 0;
511ac27a0ecSDave Kleikamp 		goto fallback;
512ac27a0ecSDave Kleikamp 	}
513ac27a0ecSDave Kleikamp 
514ac27a0ecSDave Kleikamp 	return -1;
515ac27a0ecSDave Kleikamp }
516ac27a0ecSDave Kleikamp 
5172aa9fc4cSAvantika Mathur static int find_group_other(struct super_block *sb, struct inode *parent,
5182aa9fc4cSAvantika Mathur 				ext4_group_t *group)
519ac27a0ecSDave Kleikamp {
520fd2d4291SAvantika Mathur 	ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
521fd2d4291SAvantika Mathur 	ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
522617ba13bSMingming Cao 	struct ext4_group_desc *desc;
5232aa9fc4cSAvantika Mathur 	ext4_group_t i;
524ac27a0ecSDave Kleikamp 
525ac27a0ecSDave Kleikamp 	/*
526ac27a0ecSDave Kleikamp 	 * Try to place the inode in its parent directory
527ac27a0ecSDave Kleikamp 	 */
5282aa9fc4cSAvantika Mathur 	*group = parent_group;
5292aa9fc4cSAvantika Mathur 	desc = ext4_get_group_desc(sb, *group, NULL);
530*560671a0SAneesh Kumar K.V 	if (desc && ext4_free_inodes_count(sb, desc) &&
531*560671a0SAneesh Kumar K.V 			ext4_free_blks_count(sb, desc))
5322aa9fc4cSAvantika Mathur 		return 0;
533ac27a0ecSDave Kleikamp 
534ac27a0ecSDave Kleikamp 	/*
535ac27a0ecSDave Kleikamp 	 * We're going to place this inode in a different blockgroup from its
536ac27a0ecSDave Kleikamp 	 * parent.  We want to cause files in a common directory to all land in
537ac27a0ecSDave Kleikamp 	 * the same blockgroup.  But we want files which are in a different
538ac27a0ecSDave Kleikamp 	 * directory which shares a blockgroup with our parent to land in a
539ac27a0ecSDave Kleikamp 	 * different blockgroup.
540ac27a0ecSDave Kleikamp 	 *
541ac27a0ecSDave Kleikamp 	 * So add our directory's i_ino into the starting point for the hash.
542ac27a0ecSDave Kleikamp 	 */
5432aa9fc4cSAvantika Mathur 	*group = (*group + parent->i_ino) % ngroups;
544ac27a0ecSDave Kleikamp 
545ac27a0ecSDave Kleikamp 	/*
546ac27a0ecSDave Kleikamp 	 * Use a quadratic hash to find a group with a free inode and some free
547ac27a0ecSDave Kleikamp 	 * blocks.
548ac27a0ecSDave Kleikamp 	 */
549ac27a0ecSDave Kleikamp 	for (i = 1; i < ngroups; i <<= 1) {
5502aa9fc4cSAvantika Mathur 		*group += i;
5512aa9fc4cSAvantika Mathur 		if (*group >= ngroups)
5522aa9fc4cSAvantika Mathur 			*group -= ngroups;
5532aa9fc4cSAvantika Mathur 		desc = ext4_get_group_desc(sb, *group, NULL);
554*560671a0SAneesh Kumar K.V 		if (desc && ext4_free_inodes_count(sb, desc) &&
555*560671a0SAneesh Kumar K.V 				ext4_free_blks_count(sb, desc))
5562aa9fc4cSAvantika Mathur 			return 0;
557ac27a0ecSDave Kleikamp 	}
558ac27a0ecSDave Kleikamp 
559ac27a0ecSDave Kleikamp 	/*
560ac27a0ecSDave Kleikamp 	 * That failed: try linear search for a free inode, even if that group
561ac27a0ecSDave Kleikamp 	 * has no free blocks.
562ac27a0ecSDave Kleikamp 	 */
5632aa9fc4cSAvantika Mathur 	*group = parent_group;
564ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
5652aa9fc4cSAvantika Mathur 		if (++*group >= ngroups)
5662aa9fc4cSAvantika Mathur 			*group = 0;
5672aa9fc4cSAvantika Mathur 		desc = ext4_get_group_desc(sb, *group, NULL);
568*560671a0SAneesh Kumar K.V 		if (desc && ext4_free_inodes_count(sb, desc))
5692aa9fc4cSAvantika Mathur 			return 0;
570ac27a0ecSDave Kleikamp 	}
571ac27a0ecSDave Kleikamp 
572ac27a0ecSDave Kleikamp 	return -1;
573ac27a0ecSDave Kleikamp }
574ac27a0ecSDave Kleikamp 
575ac27a0ecSDave Kleikamp /*
576ac27a0ecSDave Kleikamp  * There are two policies for allocating an inode.  If the new inode is
577ac27a0ecSDave Kleikamp  * a directory, then a forward search is made for a block group with both
578ac27a0ecSDave Kleikamp  * free space and a low directory-to-inode ratio; if that fails, then of
579ac27a0ecSDave Kleikamp  * the groups with above-average free space, that group with the fewest
580ac27a0ecSDave Kleikamp  * directories already is chosen.
581ac27a0ecSDave Kleikamp  *
582ac27a0ecSDave Kleikamp  * For other inodes, search forward from the parent directory's block
583ac27a0ecSDave Kleikamp  * group to find a free inode.
584ac27a0ecSDave Kleikamp  */
585617ba13bSMingming Cao struct inode *ext4_new_inode(handle_t *handle, struct inode *dir, int mode)
586ac27a0ecSDave Kleikamp {
587ac27a0ecSDave Kleikamp 	struct super_block *sb;
588ac27a0ecSDave Kleikamp 	struct buffer_head *bitmap_bh = NULL;
589ac27a0ecSDave Kleikamp 	struct buffer_head *bh2;
5902aa9fc4cSAvantika Mathur 	ext4_group_t group = 0;
591ac27a0ecSDave Kleikamp 	unsigned long ino = 0;
592ac27a0ecSDave Kleikamp 	struct inode *inode;
593617ba13bSMingming Cao 	struct ext4_group_desc *gdp = NULL;
594617ba13bSMingming Cao 	struct ext4_super_block *es;
595617ba13bSMingming Cao 	struct ext4_inode_info *ei;
596617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
597*560671a0SAneesh Kumar K.V 	int ret2, err = 0, count;
598ac27a0ecSDave Kleikamp 	struct inode *ret;
5992aa9fc4cSAvantika Mathur 	ext4_group_t i;
6002aa9fc4cSAvantika Mathur 	int free = 0;
601772cb7c8SJose R. Santos 	ext4_group_t flex_group;
602ac27a0ecSDave Kleikamp 
603ac27a0ecSDave Kleikamp 	/* Cannot create files in a deleted directory */
604ac27a0ecSDave Kleikamp 	if (!dir || !dir->i_nlink)
605ac27a0ecSDave Kleikamp 		return ERR_PTR(-EPERM);
606ac27a0ecSDave Kleikamp 
607ac27a0ecSDave Kleikamp 	sb = dir->i_sb;
608ac27a0ecSDave Kleikamp 	inode = new_inode(sb);
609ac27a0ecSDave Kleikamp 	if (!inode)
610ac27a0ecSDave Kleikamp 		return ERR_PTR(-ENOMEM);
611617ba13bSMingming Cao 	ei = EXT4_I(inode);
612ac27a0ecSDave Kleikamp 
613617ba13bSMingming Cao 	sbi = EXT4_SB(sb);
614ac27a0ecSDave Kleikamp 	es = sbi->s_es;
615772cb7c8SJose R. Santos 
616772cb7c8SJose R. Santos 	if (sbi->s_log_groups_per_flex) {
617772cb7c8SJose R. Santos 		ret2 = find_group_flex(sb, dir, &group);
618772cb7c8SJose R. Santos 		goto got_group;
619772cb7c8SJose R. Santos 	}
620772cb7c8SJose R. Santos 
621ac27a0ecSDave Kleikamp 	if (S_ISDIR(mode)) {
622ac27a0ecSDave Kleikamp 		if (test_opt(sb, OLDALLOC))
6232aa9fc4cSAvantika Mathur 			ret2 = find_group_dir(sb, dir, &group);
624ac27a0ecSDave Kleikamp 		else
6252aa9fc4cSAvantika Mathur 			ret2 = find_group_orlov(sb, dir, &group);
626ac27a0ecSDave Kleikamp 	} else
6272aa9fc4cSAvantika Mathur 		ret2 = find_group_other(sb, dir, &group);
628ac27a0ecSDave Kleikamp 
629772cb7c8SJose R. Santos got_group:
630ac27a0ecSDave Kleikamp 	err = -ENOSPC;
6312aa9fc4cSAvantika Mathur 	if (ret2 == -1)
632ac27a0ecSDave Kleikamp 		goto out;
633ac27a0ecSDave Kleikamp 
634ac27a0ecSDave Kleikamp 	for (i = 0; i < sbi->s_groups_count; i++) {
635ac27a0ecSDave Kleikamp 		err = -EIO;
636ac27a0ecSDave Kleikamp 
637617ba13bSMingming Cao 		gdp = ext4_get_group_desc(sb, group, &bh2);
638ac27a0ecSDave Kleikamp 		if (!gdp)
639ac27a0ecSDave Kleikamp 			goto fail;
640ac27a0ecSDave Kleikamp 
641ac27a0ecSDave Kleikamp 		brelse(bitmap_bh);
642e29d1cdeSEric Sandeen 		bitmap_bh = ext4_read_inode_bitmap(sb, group);
643ac27a0ecSDave Kleikamp 		if (!bitmap_bh)
644ac27a0ecSDave Kleikamp 			goto fail;
645ac27a0ecSDave Kleikamp 
646ac27a0ecSDave Kleikamp 		ino = 0;
647ac27a0ecSDave Kleikamp 
648ac27a0ecSDave Kleikamp repeat_in_this_group:
649617ba13bSMingming Cao 		ino = ext4_find_next_zero_bit((unsigned long *)
650617ba13bSMingming Cao 				bitmap_bh->b_data, EXT4_INODES_PER_GROUP(sb), ino);
651617ba13bSMingming Cao 		if (ino < EXT4_INODES_PER_GROUP(sb)) {
652ac27a0ecSDave Kleikamp 
653ac27a0ecSDave Kleikamp 			BUFFER_TRACE(bitmap_bh, "get_write_access");
654617ba13bSMingming Cao 			err = ext4_journal_get_write_access(handle, bitmap_bh);
655ac27a0ecSDave Kleikamp 			if (err)
656ac27a0ecSDave Kleikamp 				goto fail;
657ac27a0ecSDave Kleikamp 
658617ba13bSMingming Cao 			if (!ext4_set_bit_atomic(sb_bgl_lock(sbi, group),
659ac27a0ecSDave Kleikamp 						ino, bitmap_bh->b_data)) {
660ac27a0ecSDave Kleikamp 				/* we won it */
661ac27a0ecSDave Kleikamp 				BUFFER_TRACE(bitmap_bh,
6620390131bSFrank Mayhar 					"call ext4_handle_dirty_metadata");
6630390131bSFrank Mayhar 				err = ext4_handle_dirty_metadata(handle,
6640390131bSFrank Mayhar 								inode,
665ac27a0ecSDave Kleikamp 								bitmap_bh);
666ac27a0ecSDave Kleikamp 				if (err)
667ac27a0ecSDave Kleikamp 					goto fail;
668ac27a0ecSDave Kleikamp 				goto got;
669ac27a0ecSDave Kleikamp 			}
670ac27a0ecSDave Kleikamp 			/* we lost it */
6710390131bSFrank Mayhar 			ext4_handle_release_buffer(handle, bitmap_bh);
672ac27a0ecSDave Kleikamp 
673617ba13bSMingming Cao 			if (++ino < EXT4_INODES_PER_GROUP(sb))
674ac27a0ecSDave Kleikamp 				goto repeat_in_this_group;
675ac27a0ecSDave Kleikamp 		}
676ac27a0ecSDave Kleikamp 
677ac27a0ecSDave Kleikamp 		/*
678ac27a0ecSDave Kleikamp 		 * This case is possible in concurrent environment.  It is very
679ac27a0ecSDave Kleikamp 		 * rare.  We cannot repeat the find_group_xxx() call because
680ac27a0ecSDave Kleikamp 		 * that will simply return the same blockgroup, because the
681ac27a0ecSDave Kleikamp 		 * group descriptor metadata has not yet been updated.
682ac27a0ecSDave Kleikamp 		 * So we just go onto the next blockgroup.
683ac27a0ecSDave Kleikamp 		 */
684ac27a0ecSDave Kleikamp 		if (++group == sbi->s_groups_count)
685ac27a0ecSDave Kleikamp 			group = 0;
686ac27a0ecSDave Kleikamp 	}
687ac27a0ecSDave Kleikamp 	err = -ENOSPC;
688ac27a0ecSDave Kleikamp 	goto out;
689ac27a0ecSDave Kleikamp 
690ac27a0ecSDave Kleikamp got:
691717d50e4SAndreas Dilger 	ino++;
692717d50e4SAndreas Dilger 	if ((group == 0 && ino < EXT4_FIRST_INO(sb)) ||
693717d50e4SAndreas Dilger 	    ino > EXT4_INODES_PER_GROUP(sb)) {
69446e665e9SHarvey Harrison 		ext4_error(sb, __func__,
695ac27a0ecSDave Kleikamp 			   "reserved inode or inode > inodes count - "
696a9df9a49STheodore Ts'o 			   "block_group = %u, inode=%lu", group,
697717d50e4SAndreas Dilger 			   ino + group * EXT4_INODES_PER_GROUP(sb));
698ac27a0ecSDave Kleikamp 		err = -EIO;
699ac27a0ecSDave Kleikamp 		goto fail;
700ac27a0ecSDave Kleikamp 	}
701ac27a0ecSDave Kleikamp 
702ac27a0ecSDave Kleikamp 	BUFFER_TRACE(bh2, "get_write_access");
703617ba13bSMingming Cao 	err = ext4_journal_get_write_access(handle, bh2);
704ac27a0ecSDave Kleikamp 	if (err) goto fail;
705717d50e4SAndreas Dilger 
706717d50e4SAndreas Dilger 	/* We may have to initialize the block bitmap if it isn't already */
707717d50e4SAndreas Dilger 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
708717d50e4SAndreas Dilger 	    gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
709574ca174STheodore Ts'o 		struct buffer_head *block_bh = ext4_read_block_bitmap(sb, group);
710717d50e4SAndreas Dilger 
711717d50e4SAndreas Dilger 		BUFFER_TRACE(block_bh, "get block bitmap access");
712717d50e4SAndreas Dilger 		err = ext4_journal_get_write_access(handle, block_bh);
713717d50e4SAndreas Dilger 		if (err) {
714717d50e4SAndreas Dilger 			brelse(block_bh);
715717d50e4SAndreas Dilger 			goto fail;
716717d50e4SAndreas Dilger 		}
717717d50e4SAndreas Dilger 
718717d50e4SAndreas Dilger 		free = 0;
719ac27a0ecSDave Kleikamp 		spin_lock(sb_bgl_lock(sbi, group));
720717d50e4SAndreas Dilger 		/* recheck and clear flag under lock if we still need to */
721717d50e4SAndreas Dilger 		if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
722717d50e4SAndreas Dilger 			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
723717d50e4SAndreas Dilger 			free = ext4_free_blocks_after_init(sb, group, gdp);
724*560671a0SAneesh Kumar K.V 			ext4_free_blks_set(sb, gdp, free);
72523712a9cSFrederic Bohe 			gdp->bg_checksum = ext4_group_desc_csum(sbi, group,
72623712a9cSFrederic Bohe 								gdp);
727717d50e4SAndreas Dilger 		}
728717d50e4SAndreas Dilger 		spin_unlock(sb_bgl_lock(sbi, group));
729717d50e4SAndreas Dilger 
730717d50e4SAndreas Dilger 		/* Don't need to dirty bitmap block if we didn't change it */
731717d50e4SAndreas Dilger 		if (free) {
732717d50e4SAndreas Dilger 			BUFFER_TRACE(block_bh, "dirty block bitmap");
7330390131bSFrank Mayhar 			err = ext4_handle_dirty_metadata(handle,
7340390131bSFrank Mayhar 							NULL, block_bh);
735717d50e4SAndreas Dilger 		}
736717d50e4SAndreas Dilger 
737717d50e4SAndreas Dilger 		brelse(block_bh);
738717d50e4SAndreas Dilger 		if (err)
739717d50e4SAndreas Dilger 			goto fail;
740717d50e4SAndreas Dilger 	}
741717d50e4SAndreas Dilger 
742717d50e4SAndreas Dilger 	spin_lock(sb_bgl_lock(sbi, group));
743717d50e4SAndreas Dilger 	/* If we didn't allocate from within the initialized part of the inode
744717d50e4SAndreas Dilger 	 * table then we need to initialize up to this inode. */
745717d50e4SAndreas Dilger 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
746717d50e4SAndreas Dilger 		if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
747717d50e4SAndreas Dilger 			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
748717d50e4SAndreas Dilger 
749717d50e4SAndreas Dilger 			/* When marking the block group with
750717d50e4SAndreas Dilger 			 * ~EXT4_BG_INODE_UNINIT we don't want to depend
751b5f10eedSEric Sandeen 			 * on the value of bg_itable_unused even though
752717d50e4SAndreas Dilger 			 * mke2fs could have initialized the same for us.
753717d50e4SAndreas Dilger 			 * Instead we calculated the value below
754717d50e4SAndreas Dilger 			 */
755717d50e4SAndreas Dilger 
756717d50e4SAndreas Dilger 			free = 0;
757717d50e4SAndreas Dilger 		} else {
758717d50e4SAndreas Dilger 			free = EXT4_INODES_PER_GROUP(sb) -
759*560671a0SAneesh Kumar K.V 				ext4_itable_unused_count(sb, gdp);
760717d50e4SAndreas Dilger 		}
761717d50e4SAndreas Dilger 
762717d50e4SAndreas Dilger 		/*
763717d50e4SAndreas Dilger 		 * Check the relative inode number against the last used
764717d50e4SAndreas Dilger 		 * relative inode number in this group. if it is greater
765717d50e4SAndreas Dilger 		 * we need to  update the bg_itable_unused count
766717d50e4SAndreas Dilger 		 *
767717d50e4SAndreas Dilger 		 */
768717d50e4SAndreas Dilger 		if (ino > free)
769*560671a0SAneesh Kumar K.V 			ext4_itable_unused_set(sb, gdp,
770*560671a0SAneesh Kumar K.V 					(EXT4_INODES_PER_GROUP(sb) - ino));
771717d50e4SAndreas Dilger 	}
772717d50e4SAndreas Dilger 
773*560671a0SAneesh Kumar K.V 	count = ext4_free_inodes_count(sb, gdp) - 1;
774*560671a0SAneesh Kumar K.V 	ext4_free_inodes_set(sb, gdp, count);
775ac27a0ecSDave Kleikamp 	if (S_ISDIR(mode)) {
776*560671a0SAneesh Kumar K.V 		count = ext4_used_dirs_count(sb, gdp) + 1;
777*560671a0SAneesh Kumar K.V 		ext4_used_dirs_set(sb, gdp, count);
778ac27a0ecSDave Kleikamp 	}
779717d50e4SAndreas Dilger 	gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp);
780ac27a0ecSDave Kleikamp 	spin_unlock(sb_bgl_lock(sbi, group));
7810390131bSFrank Mayhar 	BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata");
7820390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, NULL, bh2);
783ac27a0ecSDave Kleikamp 	if (err) goto fail;
784ac27a0ecSDave Kleikamp 
785ac27a0ecSDave Kleikamp 	percpu_counter_dec(&sbi->s_freeinodes_counter);
786ac27a0ecSDave Kleikamp 	if (S_ISDIR(mode))
787ac27a0ecSDave Kleikamp 		percpu_counter_inc(&sbi->s_dirs_counter);
788ac27a0ecSDave Kleikamp 	sb->s_dirt = 1;
789ac27a0ecSDave Kleikamp 
790772cb7c8SJose R. Santos 	if (sbi->s_log_groups_per_flex) {
791772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, group);
792772cb7c8SJose R. Santos 		spin_lock(sb_bgl_lock(sbi, flex_group));
793772cb7c8SJose R. Santos 		sbi->s_flex_groups[flex_group].free_inodes--;
794772cb7c8SJose R. Santos 		spin_unlock(sb_bgl_lock(sbi, flex_group));
795772cb7c8SJose R. Santos 	}
796772cb7c8SJose R. Santos 
7974c9c544eSDavid Howells 	inode->i_uid = current_fsuid();
798ac27a0ecSDave Kleikamp 	if (test_opt(sb, GRPID))
799ac27a0ecSDave Kleikamp 		inode->i_gid = dir->i_gid;
800ac27a0ecSDave Kleikamp 	else if (dir->i_mode & S_ISGID) {
801ac27a0ecSDave Kleikamp 		inode->i_gid = dir->i_gid;
802ac27a0ecSDave Kleikamp 		if (S_ISDIR(mode))
803ac27a0ecSDave Kleikamp 			mode |= S_ISGID;
804ac27a0ecSDave Kleikamp 	} else
8054c9c544eSDavid Howells 		inode->i_gid = current_fsgid();
806ac27a0ecSDave Kleikamp 	inode->i_mode = mode;
807ac27a0ecSDave Kleikamp 
808717d50e4SAndreas Dilger 	inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
809ac27a0ecSDave Kleikamp 	/* This is the optimal IO size (for stat), not the fs block size */
810ac27a0ecSDave Kleikamp 	inode->i_blocks = 0;
811ef7f3835SKalpak Shah 	inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
812ef7f3835SKalpak Shah 						       ext4_current_time(inode);
813ac27a0ecSDave Kleikamp 
814ac27a0ecSDave Kleikamp 	memset(ei->i_data, 0, sizeof(ei->i_data));
815ac27a0ecSDave Kleikamp 	ei->i_dir_start_lookup = 0;
816ac27a0ecSDave Kleikamp 	ei->i_disksize = 0;
817ac27a0ecSDave Kleikamp 
81842bf0383SAneesh Kumar K.V 	/*
81942bf0383SAneesh Kumar K.V 	 * Don't inherit extent flag from directory. We set extent flag on
82042bf0383SAneesh Kumar K.V 	 * newly created directory and file only if -o extent mount option is
82142bf0383SAneesh Kumar K.V 	 * specified
82242bf0383SAneesh Kumar K.V 	 */
82342bf0383SAneesh Kumar K.V 	ei->i_flags = EXT4_I(dir)->i_flags & ~(EXT4_INDEX_FL|EXT4_EXTENTS_FL);
824ac27a0ecSDave Kleikamp 	if (S_ISLNK(mode))
825617ba13bSMingming Cao 		ei->i_flags &= ~(EXT4_IMMUTABLE_FL|EXT4_APPEND_FL);
826ac27a0ecSDave Kleikamp 	/* dirsync only applies to directories */
827ac27a0ecSDave Kleikamp 	if (!S_ISDIR(mode))
828617ba13bSMingming Cao 		ei->i_flags &= ~EXT4_DIRSYNC_FL;
829ac27a0ecSDave Kleikamp 	ei->i_file_acl = 0;
830ac27a0ecSDave Kleikamp 	ei->i_dtime = 0;
831ac27a0ecSDave Kleikamp 	ei->i_block_group = group;
832ac27a0ecSDave Kleikamp 
833617ba13bSMingming Cao 	ext4_set_inode_flags(inode);
834ac27a0ecSDave Kleikamp 	if (IS_DIRSYNC(inode))
8350390131bSFrank Mayhar 		ext4_handle_sync(handle);
8366b38e842SAl Viro 	if (insert_inode_locked(inode) < 0) {
8376b38e842SAl Viro 		err = -EINVAL;
8386b38e842SAl Viro 		goto fail_drop;
8396b38e842SAl Viro 	}
840ac27a0ecSDave Kleikamp 	spin_lock(&sbi->s_next_gen_lock);
841ac27a0ecSDave Kleikamp 	inode->i_generation = sbi->s_next_generation++;
842ac27a0ecSDave Kleikamp 	spin_unlock(&sbi->s_next_gen_lock);
843ac27a0ecSDave Kleikamp 
844617ba13bSMingming Cao 	ei->i_state = EXT4_STATE_NEW;
845ef7f3835SKalpak Shah 
846ef7f3835SKalpak Shah 	ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize;
847ac27a0ecSDave Kleikamp 
848ac27a0ecSDave Kleikamp 	ret = inode;
849ac27a0ecSDave Kleikamp 	if (DQUOT_ALLOC_INODE(inode)) {
850ac27a0ecSDave Kleikamp 		err = -EDQUOT;
851ac27a0ecSDave Kleikamp 		goto fail_drop;
852ac27a0ecSDave Kleikamp 	}
853ac27a0ecSDave Kleikamp 
854617ba13bSMingming Cao 	err = ext4_init_acl(handle, inode, dir);
855ac27a0ecSDave Kleikamp 	if (err)
856ac27a0ecSDave Kleikamp 		goto fail_free_drop;
857ac27a0ecSDave Kleikamp 
858617ba13bSMingming Cao 	err = ext4_init_security(handle, inode, dir);
859ac27a0ecSDave Kleikamp 	if (err)
860ac27a0ecSDave Kleikamp 		goto fail_free_drop;
861ac27a0ecSDave Kleikamp 
862a86c6181SAlex Tomas 	if (test_opt(sb, EXTENTS)) {
863e4079a11SEric Sandeen 		/* set extent flag only for directory, file and normal symlink*/
864e65187e6SAneesh Kumar K.V 		if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
865a86c6181SAlex Tomas 			EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL;
866a86c6181SAlex Tomas 			ext4_ext_tree_init(handle, inode);
867a86c6181SAlex Tomas 		}
86842bf0383SAneesh Kumar K.V 	}
869ac27a0ecSDave Kleikamp 
8708753e88fSAneesh Kumar K.V 	err = ext4_mark_inode_dirty(handle, inode);
8718753e88fSAneesh Kumar K.V 	if (err) {
8728753e88fSAneesh Kumar K.V 		ext4_std_error(sb, err);
8738753e88fSAneesh Kumar K.V 		goto fail_free_drop;
8748753e88fSAneesh Kumar K.V 	}
8758753e88fSAneesh Kumar K.V 
876617ba13bSMingming Cao 	ext4_debug("allocating inode %lu\n", inode->i_ino);
877ac27a0ecSDave Kleikamp 	goto really_out;
878ac27a0ecSDave Kleikamp fail:
879617ba13bSMingming Cao 	ext4_std_error(sb, err);
880ac27a0ecSDave Kleikamp out:
881ac27a0ecSDave Kleikamp 	iput(inode);
882ac27a0ecSDave Kleikamp 	ret = ERR_PTR(err);
883ac27a0ecSDave Kleikamp really_out:
884ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
885ac27a0ecSDave Kleikamp 	return ret;
886ac27a0ecSDave Kleikamp 
887ac27a0ecSDave Kleikamp fail_free_drop:
888ac27a0ecSDave Kleikamp 	DQUOT_FREE_INODE(inode);
889ac27a0ecSDave Kleikamp 
890ac27a0ecSDave Kleikamp fail_drop:
891ac27a0ecSDave Kleikamp 	DQUOT_DROP(inode);
892ac27a0ecSDave Kleikamp 	inode->i_flags |= S_NOQUOTA;
893ac27a0ecSDave Kleikamp 	inode->i_nlink = 0;
8946b38e842SAl Viro 	unlock_new_inode(inode);
895ac27a0ecSDave Kleikamp 	iput(inode);
896ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
897ac27a0ecSDave Kleikamp 	return ERR_PTR(err);
898ac27a0ecSDave Kleikamp }
899ac27a0ecSDave Kleikamp 
900ac27a0ecSDave Kleikamp /* Verify that we are loading a valid orphan from disk */
901617ba13bSMingming Cao struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
902ac27a0ecSDave Kleikamp {
903617ba13bSMingming Cao 	unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
904fd2d4291SAvantika Mathur 	ext4_group_t block_group;
905ac27a0ecSDave Kleikamp 	int bit;
9061d1fe1eeSDavid Howells 	struct buffer_head *bitmap_bh;
907ac27a0ecSDave Kleikamp 	struct inode *inode = NULL;
9081d1fe1eeSDavid Howells 	long err = -EIO;
909ac27a0ecSDave Kleikamp 
910ac27a0ecSDave Kleikamp 	/* Error cases - e2fsck has already cleaned up for us */
911ac27a0ecSDave Kleikamp 	if (ino > max_ino) {
91246e665e9SHarvey Harrison 		ext4_warning(sb, __func__,
913ac27a0ecSDave Kleikamp 			     "bad orphan ino %lu!  e2fsck was run?", ino);
9141d1fe1eeSDavid Howells 		goto error;
915ac27a0ecSDave Kleikamp 	}
916ac27a0ecSDave Kleikamp 
917617ba13bSMingming Cao 	block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
918617ba13bSMingming Cao 	bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
919e29d1cdeSEric Sandeen 	bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
920ac27a0ecSDave Kleikamp 	if (!bitmap_bh) {
92146e665e9SHarvey Harrison 		ext4_warning(sb, __func__,
922ac27a0ecSDave Kleikamp 			     "inode bitmap error for orphan %lu", ino);
9231d1fe1eeSDavid Howells 		goto error;
924ac27a0ecSDave Kleikamp 	}
925ac27a0ecSDave Kleikamp 
926ac27a0ecSDave Kleikamp 	/* Having the inode bit set should be a 100% indicator that this
927ac27a0ecSDave Kleikamp 	 * is a valid orphan (no e2fsck run on fs).  Orphans also include
928ac27a0ecSDave Kleikamp 	 * inodes that were being truncated, so we can't check i_nlink==0.
929ac27a0ecSDave Kleikamp 	 */
9301d1fe1eeSDavid Howells 	if (!ext4_test_bit(bit, bitmap_bh->b_data))
9311d1fe1eeSDavid Howells 		goto bad_orphan;
9321d1fe1eeSDavid Howells 
9331d1fe1eeSDavid Howells 	inode = ext4_iget(sb, ino);
9341d1fe1eeSDavid Howells 	if (IS_ERR(inode))
9351d1fe1eeSDavid Howells 		goto iget_failed;
9361d1fe1eeSDavid Howells 
93791ef4cafSDuane Griffin 	/*
93891ef4cafSDuane Griffin 	 * If the orphans has i_nlinks > 0 then it should be able to be
93991ef4cafSDuane Griffin 	 * truncated, otherwise it won't be removed from the orphan list
94091ef4cafSDuane Griffin 	 * during processing and an infinite loop will result.
94191ef4cafSDuane Griffin 	 */
94291ef4cafSDuane Griffin 	if (inode->i_nlink && !ext4_can_truncate(inode))
94391ef4cafSDuane Griffin 		goto bad_orphan;
94491ef4cafSDuane Griffin 
9451d1fe1eeSDavid Howells 	if (NEXT_ORPHAN(inode) > max_ino)
9461d1fe1eeSDavid Howells 		goto bad_orphan;
9471d1fe1eeSDavid Howells 	brelse(bitmap_bh);
9481d1fe1eeSDavid Howells 	return inode;
9491d1fe1eeSDavid Howells 
9501d1fe1eeSDavid Howells iget_failed:
9511d1fe1eeSDavid Howells 	err = PTR_ERR(inode);
9521d1fe1eeSDavid Howells 	inode = NULL;
9531d1fe1eeSDavid Howells bad_orphan:
95446e665e9SHarvey Harrison 	ext4_warning(sb, __func__,
955ac27a0ecSDave Kleikamp 		     "bad orphan inode %lu!  e2fsck was run?", ino);
956617ba13bSMingming Cao 	printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n",
957ac27a0ecSDave Kleikamp 	       bit, (unsigned long long)bitmap_bh->b_blocknr,
958617ba13bSMingming Cao 	       ext4_test_bit(bit, bitmap_bh->b_data));
959ac27a0ecSDave Kleikamp 	printk(KERN_NOTICE "inode=%p\n", inode);
960ac27a0ecSDave Kleikamp 	if (inode) {
961ac27a0ecSDave Kleikamp 		printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
962ac27a0ecSDave Kleikamp 		       is_bad_inode(inode));
963ac27a0ecSDave Kleikamp 		printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
964ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
965ac27a0ecSDave Kleikamp 		printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
96691ef4cafSDuane Griffin 		printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink);
967ac27a0ecSDave Kleikamp 		/* Avoid freeing blocks if we got a bad deleted inode */
9681d1fe1eeSDavid Howells 		if (inode->i_nlink == 0)
969ac27a0ecSDave Kleikamp 			inode->i_blocks = 0;
970ac27a0ecSDave Kleikamp 		iput(inode);
971ac27a0ecSDave Kleikamp 	}
972ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
9731d1fe1eeSDavid Howells error:
9741d1fe1eeSDavid Howells 	return ERR_PTR(err);
975ac27a0ecSDave Kleikamp }
976ac27a0ecSDave Kleikamp 
977617ba13bSMingming Cao unsigned long ext4_count_free_inodes(struct super_block *sb)
978ac27a0ecSDave Kleikamp {
979ac27a0ecSDave Kleikamp 	unsigned long desc_count;
980617ba13bSMingming Cao 	struct ext4_group_desc *gdp;
981fd2d4291SAvantika Mathur 	ext4_group_t i;
982617ba13bSMingming Cao #ifdef EXT4FS_DEBUG
983617ba13bSMingming Cao 	struct ext4_super_block *es;
984ac27a0ecSDave Kleikamp 	unsigned long bitmap_count, x;
985ac27a0ecSDave Kleikamp 	struct buffer_head *bitmap_bh = NULL;
986ac27a0ecSDave Kleikamp 
987617ba13bSMingming Cao 	es = EXT4_SB(sb)->s_es;
988ac27a0ecSDave Kleikamp 	desc_count = 0;
989ac27a0ecSDave Kleikamp 	bitmap_count = 0;
990ac27a0ecSDave Kleikamp 	gdp = NULL;
991617ba13bSMingming Cao 	for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
992617ba13bSMingming Cao 		gdp = ext4_get_group_desc(sb, i, NULL);
993ac27a0ecSDave Kleikamp 		if (!gdp)
994ac27a0ecSDave Kleikamp 			continue;
995*560671a0SAneesh Kumar K.V 		desc_count += ext4_free_inodes_count(sb, gdp);
996ac27a0ecSDave Kleikamp 		brelse(bitmap_bh);
997e29d1cdeSEric Sandeen 		bitmap_bh = ext4_read_inode_bitmap(sb, i);
998ac27a0ecSDave Kleikamp 		if (!bitmap_bh)
999ac27a0ecSDave Kleikamp 			continue;
1000ac27a0ecSDave Kleikamp 
1001617ba13bSMingming Cao 		x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8);
1002c549a95dSEric Sandeen 		printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
1003*560671a0SAneesh Kumar K.V 			i, ext4_free_inodes_count(sb, gdp), x);
1004ac27a0ecSDave Kleikamp 		bitmap_count += x;
1005ac27a0ecSDave Kleikamp 	}
1006ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
10074776004fSTheodore Ts'o 	printk(KERN_DEBUG "ext4_count_free_inodes: "
10084776004fSTheodore Ts'o 	       "stored = %u, computed = %lu, %lu\n",
1009ac27a0ecSDave Kleikamp 	       le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
1010ac27a0ecSDave Kleikamp 	return desc_count;
1011ac27a0ecSDave Kleikamp #else
1012ac27a0ecSDave Kleikamp 	desc_count = 0;
1013617ba13bSMingming Cao 	for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
1014617ba13bSMingming Cao 		gdp = ext4_get_group_desc(sb, i, NULL);
1015ac27a0ecSDave Kleikamp 		if (!gdp)
1016ac27a0ecSDave Kleikamp 			continue;
1017*560671a0SAneesh Kumar K.V 		desc_count += ext4_free_inodes_count(sb, gdp);
1018ac27a0ecSDave Kleikamp 		cond_resched();
1019ac27a0ecSDave Kleikamp 	}
1020ac27a0ecSDave Kleikamp 	return desc_count;
1021ac27a0ecSDave Kleikamp #endif
1022ac27a0ecSDave Kleikamp }
1023ac27a0ecSDave Kleikamp 
1024ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
1025617ba13bSMingming Cao unsigned long ext4_count_dirs(struct super_block * sb)
1026ac27a0ecSDave Kleikamp {
1027ac27a0ecSDave Kleikamp 	unsigned long count = 0;
1028fd2d4291SAvantika Mathur 	ext4_group_t i;
1029ac27a0ecSDave Kleikamp 
1030617ba13bSMingming Cao 	for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
1031617ba13bSMingming Cao 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1032ac27a0ecSDave Kleikamp 		if (!gdp)
1033ac27a0ecSDave Kleikamp 			continue;
1034*560671a0SAneesh Kumar K.V 		count += ext4_used_dirs_count(sb, gdp);
1035ac27a0ecSDave Kleikamp 	}
1036ac27a0ecSDave Kleikamp 	return count;
1037ac27a0ecSDave Kleikamp }
1038