xref: /linux/fs/ext4/ialloc.c (revision 1d1fe1ee02b9ac2660995b10e35dd41448fef011)
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>
18617ba13bSMingming Cao #include <linux/ext4_fs.h>
19dab291afSMingming Cao #include <linux/ext4_jbd2.h>
20ac27a0ecSDave Kleikamp #include <linux/stat.h>
21ac27a0ecSDave Kleikamp #include <linux/string.h>
22ac27a0ecSDave Kleikamp #include <linux/quotaops.h>
23ac27a0ecSDave Kleikamp #include <linux/buffer_head.h>
24ac27a0ecSDave Kleikamp #include <linux/random.h>
25ac27a0ecSDave Kleikamp #include <linux/bitops.h>
263a5b2ecdSMingming Cao #include <linux/blkdev.h>
27ac27a0ecSDave Kleikamp #include <asm/byteorder.h>
28ac27a0ecSDave Kleikamp 
29ac27a0ecSDave Kleikamp #include "xattr.h"
30ac27a0ecSDave Kleikamp #include "acl.h"
31717d50e4SAndreas Dilger #include "group.h"
32ac27a0ecSDave Kleikamp 
33ac27a0ecSDave Kleikamp /*
34ac27a0ecSDave Kleikamp  * ialloc.c contains the inodes allocation and deallocation routines
35ac27a0ecSDave Kleikamp  */
36ac27a0ecSDave Kleikamp 
37ac27a0ecSDave Kleikamp /*
38ac27a0ecSDave Kleikamp  * The free inodes are managed by bitmaps.  A file system contains several
39ac27a0ecSDave Kleikamp  * blocks groups.  Each group contains 1 bitmap block for blocks, 1 bitmap
40ac27a0ecSDave Kleikamp  * block for inodes, N blocks for the inode table and data blocks.
41ac27a0ecSDave Kleikamp  *
42ac27a0ecSDave Kleikamp  * The file system contains group descriptors which are located after the
43ac27a0ecSDave Kleikamp  * super block.  Each descriptor contains the number of the bitmap block and
44ac27a0ecSDave Kleikamp  * the free blocks count in the block.
45ac27a0ecSDave Kleikamp  */
46ac27a0ecSDave Kleikamp 
47717d50e4SAndreas Dilger /*
48717d50e4SAndreas Dilger  * To avoid calling the atomic setbit hundreds or thousands of times, we only
49717d50e4SAndreas Dilger  * need to use it within a single byte (to ensure we get endianness right).
50717d50e4SAndreas Dilger  * We can use memset for the rest of the bitmap as there are no other users.
51717d50e4SAndreas Dilger  */
52717d50e4SAndreas Dilger void mark_bitmap_end(int start_bit, int end_bit, char *bitmap)
53717d50e4SAndreas Dilger {
54717d50e4SAndreas Dilger 	int i;
55717d50e4SAndreas Dilger 
56717d50e4SAndreas Dilger 	if (start_bit >= end_bit)
57717d50e4SAndreas Dilger 		return;
58717d50e4SAndreas Dilger 
59717d50e4SAndreas Dilger 	ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit);
60717d50e4SAndreas Dilger 	for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
61717d50e4SAndreas Dilger 		ext4_set_bit(i, bitmap);
62717d50e4SAndreas Dilger 	if (i < end_bit)
63717d50e4SAndreas Dilger 		memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
64717d50e4SAndreas Dilger }
65717d50e4SAndreas Dilger 
66717d50e4SAndreas Dilger /* Initializes an uninitialized inode bitmap */
67fd2d4291SAvantika Mathur unsigned ext4_init_inode_bitmap(struct super_block *sb, struct buffer_head *bh,
68fd2d4291SAvantika Mathur 				ext4_group_t block_group,
69717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
70717d50e4SAndreas Dilger {
71717d50e4SAndreas Dilger 	struct ext4_sb_info *sbi = EXT4_SB(sb);
72717d50e4SAndreas Dilger 
73717d50e4SAndreas Dilger 	J_ASSERT_BH(bh, buffer_locked(bh));
74717d50e4SAndreas Dilger 
75717d50e4SAndreas Dilger 	/* If checksum is bad mark all blocks and inodes use to prevent
76717d50e4SAndreas Dilger 	 * allocation, essentially implementing a per-group read-only flag. */
77717d50e4SAndreas Dilger 	if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) {
78fd2d4291SAvantika Mathur 		ext4_error(sb, __FUNCTION__, "Checksum bad for group %lu\n",
79717d50e4SAndreas Dilger 			   block_group);
80717d50e4SAndreas Dilger 		gdp->bg_free_blocks_count = 0;
81717d50e4SAndreas Dilger 		gdp->bg_free_inodes_count = 0;
82717d50e4SAndreas Dilger 		gdp->bg_itable_unused = 0;
83717d50e4SAndreas Dilger 		memset(bh->b_data, 0xff, sb->s_blocksize);
84717d50e4SAndreas Dilger 		return 0;
85717d50e4SAndreas Dilger 	}
86717d50e4SAndreas Dilger 
87717d50e4SAndreas Dilger 	memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8);
88717d50e4SAndreas Dilger 	mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), EXT4_BLOCKS_PER_GROUP(sb),
89717d50e4SAndreas Dilger 			bh->b_data);
90717d50e4SAndreas Dilger 
91717d50e4SAndreas Dilger 	return EXT4_INODES_PER_GROUP(sb);
92717d50e4SAndreas Dilger }
93ac27a0ecSDave Kleikamp 
94ac27a0ecSDave Kleikamp /*
95ac27a0ecSDave Kleikamp  * Read the inode allocation bitmap for a given block_group, reading
96ac27a0ecSDave Kleikamp  * into the specified slot in the superblock's bitmap cache.
97ac27a0ecSDave Kleikamp  *
98ac27a0ecSDave Kleikamp  * Return buffer_head of bitmap on success or NULL.
99ac27a0ecSDave Kleikamp  */
100ac27a0ecSDave Kleikamp static struct buffer_head *
101fd2d4291SAvantika Mathur read_inode_bitmap(struct super_block *sb, ext4_group_t block_group)
102ac27a0ecSDave Kleikamp {
103617ba13bSMingming Cao 	struct ext4_group_desc *desc;
104ac27a0ecSDave Kleikamp 	struct buffer_head *bh = NULL;
105ac27a0ecSDave Kleikamp 
106617ba13bSMingming Cao 	desc = ext4_get_group_desc(sb, block_group, NULL);
107ac27a0ecSDave Kleikamp 	if (!desc)
108ac27a0ecSDave Kleikamp 		goto error_out;
109717d50e4SAndreas Dilger 	if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
110717d50e4SAndreas Dilger 		bh = sb_getblk(sb, ext4_inode_bitmap(sb, desc));
111717d50e4SAndreas Dilger 		if (!buffer_uptodate(bh)) {
112717d50e4SAndreas Dilger 			lock_buffer(bh);
113717d50e4SAndreas Dilger 			if (!buffer_uptodate(bh)) {
114717d50e4SAndreas Dilger 				ext4_init_inode_bitmap(sb, bh, block_group,
115717d50e4SAndreas Dilger 						       desc);
116717d50e4SAndreas Dilger 				set_buffer_uptodate(bh);
117717d50e4SAndreas Dilger 			}
118717d50e4SAndreas Dilger 			unlock_buffer(bh);
119717d50e4SAndreas Dilger 		}
120717d50e4SAndreas Dilger 	} else {
1218fadc143SAlexandre Ratchov 		bh = sb_bread(sb, ext4_inode_bitmap(sb, desc));
122717d50e4SAndreas Dilger 	}
123ac27a0ecSDave Kleikamp 	if (!bh)
124617ba13bSMingming Cao 		ext4_error(sb, "read_inode_bitmap",
125ac27a0ecSDave Kleikamp 			    "Cannot read inode bitmap - "
126bd81d8eeSLaurent Vivier 			    "block_group = %lu, inode_bitmap = %llu",
1278fadc143SAlexandre Ratchov 			    block_group, ext4_inode_bitmap(sb, desc));
128ac27a0ecSDave Kleikamp error_out:
129ac27a0ecSDave Kleikamp 	return bh;
130ac27a0ecSDave Kleikamp }
131ac27a0ecSDave Kleikamp 
132ac27a0ecSDave Kleikamp /*
133ac27a0ecSDave Kleikamp  * NOTE! When we get the inode, we're the only people
134ac27a0ecSDave Kleikamp  * that have access to it, and as such there are no
135ac27a0ecSDave Kleikamp  * race conditions we have to worry about. The inode
136ac27a0ecSDave Kleikamp  * is not on the hash-lists, and it cannot be reached
137ac27a0ecSDave Kleikamp  * through the filesystem because the directory entry
138ac27a0ecSDave Kleikamp  * has been deleted earlier.
139ac27a0ecSDave Kleikamp  *
140ac27a0ecSDave Kleikamp  * HOWEVER: we must make sure that we get no aliases,
141ac27a0ecSDave Kleikamp  * which means that we have to call "clear_inode()"
142ac27a0ecSDave Kleikamp  * _before_ we mark the inode not in use in the inode
143ac27a0ecSDave Kleikamp  * bitmaps. Otherwise a newly created file might use
144ac27a0ecSDave Kleikamp  * the same inode number (not actually the same pointer
145ac27a0ecSDave Kleikamp  * though), and then we'd have two inodes sharing the
146ac27a0ecSDave Kleikamp  * same inode number and space on the harddisk.
147ac27a0ecSDave Kleikamp  */
148617ba13bSMingming Cao void ext4_free_inode (handle_t *handle, struct inode * inode)
149ac27a0ecSDave Kleikamp {
150ac27a0ecSDave Kleikamp 	struct super_block * sb = inode->i_sb;
151ac27a0ecSDave Kleikamp 	int is_directory;
152ac27a0ecSDave Kleikamp 	unsigned long ino;
153ac27a0ecSDave Kleikamp 	struct buffer_head *bitmap_bh = NULL;
154ac27a0ecSDave Kleikamp 	struct buffer_head *bh2;
155fd2d4291SAvantika Mathur 	ext4_group_t block_group;
156ac27a0ecSDave Kleikamp 	unsigned long bit;
157617ba13bSMingming Cao 	struct ext4_group_desc * gdp;
158617ba13bSMingming Cao 	struct ext4_super_block * es;
159617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
160ac27a0ecSDave Kleikamp 	int fatal = 0, err;
161ac27a0ecSDave Kleikamp 
162ac27a0ecSDave Kleikamp 	if (atomic_read(&inode->i_count) > 1) {
163617ba13bSMingming Cao 		printk ("ext4_free_inode: inode has count=%d\n",
164ac27a0ecSDave Kleikamp 					atomic_read(&inode->i_count));
165ac27a0ecSDave Kleikamp 		return;
166ac27a0ecSDave Kleikamp 	}
167ac27a0ecSDave Kleikamp 	if (inode->i_nlink) {
168617ba13bSMingming Cao 		printk ("ext4_free_inode: inode has nlink=%d\n",
169ac27a0ecSDave Kleikamp 			inode->i_nlink);
170ac27a0ecSDave Kleikamp 		return;
171ac27a0ecSDave Kleikamp 	}
172ac27a0ecSDave Kleikamp 	if (!sb) {
173617ba13bSMingming Cao 		printk("ext4_free_inode: inode on nonexistent device\n");
174ac27a0ecSDave Kleikamp 		return;
175ac27a0ecSDave Kleikamp 	}
176617ba13bSMingming Cao 	sbi = EXT4_SB(sb);
177ac27a0ecSDave Kleikamp 
178ac27a0ecSDave Kleikamp 	ino = inode->i_ino;
179617ba13bSMingming Cao 	ext4_debug ("freeing inode %lu\n", ino);
180ac27a0ecSDave Kleikamp 
181ac27a0ecSDave Kleikamp 	/*
182ac27a0ecSDave Kleikamp 	 * Note: we must free any quota before locking the superblock,
183ac27a0ecSDave Kleikamp 	 * as writing the quota to disk may need the lock as well.
184ac27a0ecSDave Kleikamp 	 */
185ac27a0ecSDave Kleikamp 	DQUOT_INIT(inode);
186617ba13bSMingming Cao 	ext4_xattr_delete_inode(handle, inode);
187ac27a0ecSDave Kleikamp 	DQUOT_FREE_INODE(inode);
188ac27a0ecSDave Kleikamp 	DQUOT_DROP(inode);
189ac27a0ecSDave Kleikamp 
190ac27a0ecSDave Kleikamp 	is_directory = S_ISDIR(inode->i_mode);
191ac27a0ecSDave Kleikamp 
192ac27a0ecSDave Kleikamp 	/* Do this BEFORE marking the inode not in use or returning an error */
193ac27a0ecSDave Kleikamp 	clear_inode (inode);
194ac27a0ecSDave Kleikamp 
195617ba13bSMingming Cao 	es = EXT4_SB(sb)->s_es;
196617ba13bSMingming Cao 	if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
197617ba13bSMingming Cao 		ext4_error (sb, "ext4_free_inode",
198ac27a0ecSDave Kleikamp 			    "reserved or nonexistent inode %lu", ino);
199ac27a0ecSDave Kleikamp 		goto error_return;
200ac27a0ecSDave Kleikamp 	}
201617ba13bSMingming Cao 	block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
202617ba13bSMingming Cao 	bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
203ac27a0ecSDave Kleikamp 	bitmap_bh = read_inode_bitmap(sb, block_group);
204ac27a0ecSDave Kleikamp 	if (!bitmap_bh)
205ac27a0ecSDave Kleikamp 		goto error_return;
206ac27a0ecSDave Kleikamp 
207ac27a0ecSDave Kleikamp 	BUFFER_TRACE(bitmap_bh, "get_write_access");
208617ba13bSMingming Cao 	fatal = ext4_journal_get_write_access(handle, bitmap_bh);
209ac27a0ecSDave Kleikamp 	if (fatal)
210ac27a0ecSDave Kleikamp 		goto error_return;
211ac27a0ecSDave Kleikamp 
212ac27a0ecSDave Kleikamp 	/* Ok, now we can actually update the inode bitmaps.. */
213617ba13bSMingming Cao 	if (!ext4_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
214ac27a0ecSDave Kleikamp 					bit, bitmap_bh->b_data))
215617ba13bSMingming Cao 		ext4_error (sb, "ext4_free_inode",
216ac27a0ecSDave Kleikamp 			      "bit already cleared for inode %lu", ino);
217ac27a0ecSDave Kleikamp 	else {
218617ba13bSMingming Cao 		gdp = ext4_get_group_desc (sb, block_group, &bh2);
219ac27a0ecSDave Kleikamp 
220ac27a0ecSDave Kleikamp 		BUFFER_TRACE(bh2, "get_write_access");
221617ba13bSMingming Cao 		fatal = ext4_journal_get_write_access(handle, bh2);
222ac27a0ecSDave Kleikamp 		if (fatal) goto error_return;
223ac27a0ecSDave Kleikamp 
224ac27a0ecSDave Kleikamp 		if (gdp) {
225ac27a0ecSDave Kleikamp 			spin_lock(sb_bgl_lock(sbi, block_group));
226ac27a0ecSDave Kleikamp 			gdp->bg_free_inodes_count = cpu_to_le16(
227ac27a0ecSDave Kleikamp 				le16_to_cpu(gdp->bg_free_inodes_count) + 1);
228ac27a0ecSDave Kleikamp 			if (is_directory)
229ac27a0ecSDave Kleikamp 				gdp->bg_used_dirs_count = cpu_to_le16(
230ac27a0ecSDave Kleikamp 				  le16_to_cpu(gdp->bg_used_dirs_count) - 1);
231717d50e4SAndreas Dilger 			gdp->bg_checksum = ext4_group_desc_csum(sbi,
232717d50e4SAndreas Dilger 							block_group, gdp);
233ac27a0ecSDave Kleikamp 			spin_unlock(sb_bgl_lock(sbi, block_group));
234ac27a0ecSDave Kleikamp 			percpu_counter_inc(&sbi->s_freeinodes_counter);
235ac27a0ecSDave Kleikamp 			if (is_directory)
236ac27a0ecSDave Kleikamp 				percpu_counter_dec(&sbi->s_dirs_counter);
237ac27a0ecSDave Kleikamp 
238ac27a0ecSDave Kleikamp 		}
239617ba13bSMingming Cao 		BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata");
240617ba13bSMingming Cao 		err = ext4_journal_dirty_metadata(handle, bh2);
241ac27a0ecSDave Kleikamp 		if (!fatal) fatal = err;
242ac27a0ecSDave Kleikamp 	}
243617ba13bSMingming Cao 	BUFFER_TRACE(bitmap_bh, "call ext4_journal_dirty_metadata");
244617ba13bSMingming Cao 	err = ext4_journal_dirty_metadata(handle, bitmap_bh);
245ac27a0ecSDave Kleikamp 	if (!fatal)
246ac27a0ecSDave Kleikamp 		fatal = err;
247ac27a0ecSDave Kleikamp 	sb->s_dirt = 1;
248ac27a0ecSDave Kleikamp error_return:
249ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
250617ba13bSMingming Cao 	ext4_std_error(sb, fatal);
251ac27a0ecSDave Kleikamp }
252ac27a0ecSDave Kleikamp 
253ac27a0ecSDave Kleikamp /*
254ac27a0ecSDave Kleikamp  * There are two policies for allocating an inode.  If the new inode is
255ac27a0ecSDave Kleikamp  * a directory, then a forward search is made for a block group with both
256ac27a0ecSDave Kleikamp  * free space and a low directory-to-inode ratio; if that fails, then of
257ac27a0ecSDave Kleikamp  * the groups with above-average free space, that group with the fewest
258ac27a0ecSDave Kleikamp  * directories already is chosen.
259ac27a0ecSDave Kleikamp  *
260ac27a0ecSDave Kleikamp  * For other inodes, search forward from the parent directory\'s block
261ac27a0ecSDave Kleikamp  * group to find a free inode.
262ac27a0ecSDave Kleikamp  */
2632aa9fc4cSAvantika Mathur static int find_group_dir(struct super_block *sb, struct inode *parent,
2642aa9fc4cSAvantika Mathur 				ext4_group_t *best_group)
265ac27a0ecSDave Kleikamp {
266fd2d4291SAvantika Mathur 	ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
267ac27a0ecSDave Kleikamp 	unsigned int freei, avefreei;
268617ba13bSMingming Cao 	struct ext4_group_desc *desc, *best_desc = NULL;
2692aa9fc4cSAvantika Mathur 	ext4_group_t group;
2702aa9fc4cSAvantika Mathur 	int ret = -1;
271ac27a0ecSDave Kleikamp 
272617ba13bSMingming Cao 	freei = percpu_counter_read_positive(&EXT4_SB(sb)->s_freeinodes_counter);
273ac27a0ecSDave Kleikamp 	avefreei = freei / ngroups;
274ac27a0ecSDave Kleikamp 
275ac27a0ecSDave Kleikamp 	for (group = 0; group < ngroups; group++) {
276ef2fb679SEric Sandeen 		desc = ext4_get_group_desc (sb, group, NULL);
277ac27a0ecSDave Kleikamp 		if (!desc || !desc->bg_free_inodes_count)
278ac27a0ecSDave Kleikamp 			continue;
279ac27a0ecSDave Kleikamp 		if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
280ac27a0ecSDave Kleikamp 			continue;
281ac27a0ecSDave Kleikamp 		if (!best_desc ||
282ac27a0ecSDave Kleikamp 		    (le16_to_cpu(desc->bg_free_blocks_count) >
283ac27a0ecSDave Kleikamp 		     le16_to_cpu(best_desc->bg_free_blocks_count))) {
2842aa9fc4cSAvantika Mathur 			*best_group = group;
285ac27a0ecSDave Kleikamp 			best_desc = desc;
2862aa9fc4cSAvantika Mathur 			ret = 0;
287ac27a0ecSDave Kleikamp 		}
288ac27a0ecSDave Kleikamp 	}
2892aa9fc4cSAvantika Mathur 	return ret;
290ac27a0ecSDave Kleikamp }
291ac27a0ecSDave Kleikamp 
292ac27a0ecSDave Kleikamp /*
293ac27a0ecSDave Kleikamp  * Orlov's allocator for directories.
294ac27a0ecSDave Kleikamp  *
295ac27a0ecSDave Kleikamp  * We always try to spread first-level directories.
296ac27a0ecSDave Kleikamp  *
297ac27a0ecSDave Kleikamp  * If there are blockgroups with both free inodes and free blocks counts
298ac27a0ecSDave Kleikamp  * not worse than average we return one with smallest directory count.
299ac27a0ecSDave Kleikamp  * Otherwise we simply return a random group.
300ac27a0ecSDave Kleikamp  *
301ac27a0ecSDave Kleikamp  * For the rest rules look so:
302ac27a0ecSDave Kleikamp  *
303ac27a0ecSDave Kleikamp  * It's OK to put directory into a group unless
304ac27a0ecSDave Kleikamp  * it has too many directories already (max_dirs) or
305ac27a0ecSDave Kleikamp  * it has too few free inodes left (min_inodes) or
306ac27a0ecSDave Kleikamp  * it has too few free blocks left (min_blocks) or
307ac27a0ecSDave Kleikamp  * it's already running too large debt (max_debt).
308ac27a0ecSDave Kleikamp  * Parent's group is prefered, if it doesn't satisfy these
309ac27a0ecSDave Kleikamp  * conditions we search cyclically through the rest. If none
310ac27a0ecSDave Kleikamp  * of the groups look good we just look for a group with more
311ac27a0ecSDave Kleikamp  * free inodes than average (starting at parent's group).
312ac27a0ecSDave Kleikamp  *
313ac27a0ecSDave Kleikamp  * Debt is incremented each time we allocate a directory and decremented
314ac27a0ecSDave Kleikamp  * when we allocate an inode, within 0--255.
315ac27a0ecSDave Kleikamp  */
316ac27a0ecSDave Kleikamp 
317ac27a0ecSDave Kleikamp #define INODE_COST 64
318ac27a0ecSDave Kleikamp #define BLOCK_COST 256
319ac27a0ecSDave Kleikamp 
3202aa9fc4cSAvantika Mathur static int find_group_orlov(struct super_block *sb, struct inode *parent,
3212aa9fc4cSAvantika Mathur 				ext4_group_t *group)
322ac27a0ecSDave Kleikamp {
323fd2d4291SAvantika Mathur 	ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
324617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
325617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
326fd2d4291SAvantika Mathur 	ext4_group_t ngroups = sbi->s_groups_count;
327617ba13bSMingming Cao 	int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
328ac27a0ecSDave Kleikamp 	unsigned int freei, avefreei;
329617ba13bSMingming Cao 	ext4_fsblk_t freeb, avefreeb;
330617ba13bSMingming Cao 	ext4_fsblk_t blocks_per_dir;
331ac27a0ecSDave Kleikamp 	unsigned int ndirs;
332ac27a0ecSDave Kleikamp 	int max_debt, max_dirs, min_inodes;
333617ba13bSMingming Cao 	ext4_grpblk_t min_blocks;
3342aa9fc4cSAvantika Mathur 	ext4_group_t i;
335617ba13bSMingming Cao 	struct ext4_group_desc *desc;
336ac27a0ecSDave Kleikamp 
337ac27a0ecSDave Kleikamp 	freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
338ac27a0ecSDave Kleikamp 	avefreei = freei / ngroups;
339ac27a0ecSDave Kleikamp 	freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
3403a5b2ecdSMingming Cao 	avefreeb = freeb;
341f4e5bc24SAndrew Morton 	do_div(avefreeb, ngroups);
342ac27a0ecSDave Kleikamp 	ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
343ac27a0ecSDave Kleikamp 
344ac27a0ecSDave Kleikamp 	if ((parent == sb->s_root->d_inode) ||
345617ba13bSMingming Cao 	    (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL)) {
346ac27a0ecSDave Kleikamp 		int best_ndir = inodes_per_group;
3472aa9fc4cSAvantika Mathur 		ext4_group_t grp;
3482aa9fc4cSAvantika Mathur 		int ret = -1;
349ac27a0ecSDave Kleikamp 
3502aa9fc4cSAvantika Mathur 		get_random_bytes(&grp, sizeof(grp));
3512aa9fc4cSAvantika Mathur 		parent_group = (unsigned)grp % ngroups;
352ac27a0ecSDave Kleikamp 		for (i = 0; i < ngroups; i++) {
3532aa9fc4cSAvantika Mathur 			grp = (parent_group + i) % ngroups;
3542aa9fc4cSAvantika Mathur 			desc = ext4_get_group_desc(sb, grp, NULL);
355ac27a0ecSDave Kleikamp 			if (!desc || !desc->bg_free_inodes_count)
356ac27a0ecSDave Kleikamp 				continue;
357ac27a0ecSDave Kleikamp 			if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
358ac27a0ecSDave Kleikamp 				continue;
359ac27a0ecSDave Kleikamp 			if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
360ac27a0ecSDave Kleikamp 				continue;
361ac27a0ecSDave Kleikamp 			if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
362ac27a0ecSDave Kleikamp 				continue;
3632aa9fc4cSAvantika Mathur 			*group = grp;
3642aa9fc4cSAvantika Mathur 			ret = 0;
365ac27a0ecSDave Kleikamp 			best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
366ac27a0ecSDave Kleikamp 		}
3672aa9fc4cSAvantika Mathur 		if (ret == 0)
3682aa9fc4cSAvantika Mathur 			return ret;
369ac27a0ecSDave Kleikamp 		goto fallback;
370ac27a0ecSDave Kleikamp 	}
371ac27a0ecSDave Kleikamp 
372bd81d8eeSLaurent Vivier 	blocks_per_dir = ext4_blocks_count(es) - freeb;
373f4e5bc24SAndrew Morton 	do_div(blocks_per_dir, ndirs);
374ac27a0ecSDave Kleikamp 
375ac27a0ecSDave Kleikamp 	max_dirs = ndirs / ngroups + inodes_per_group / 16;
376ac27a0ecSDave Kleikamp 	min_inodes = avefreei - inodes_per_group / 4;
377617ba13bSMingming Cao 	min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb) / 4;
378ac27a0ecSDave Kleikamp 
3793a5b2ecdSMingming Cao 	max_debt = EXT4_BLOCKS_PER_GROUP(sb);
380f4e5bc24SAndrew Morton 	max_debt /= max_t(int, blocks_per_dir, BLOCK_COST);
381ac27a0ecSDave Kleikamp 	if (max_debt * INODE_COST > inodes_per_group)
382ac27a0ecSDave Kleikamp 		max_debt = inodes_per_group / INODE_COST;
383ac27a0ecSDave Kleikamp 	if (max_debt > 255)
384ac27a0ecSDave Kleikamp 		max_debt = 255;
385ac27a0ecSDave Kleikamp 	if (max_debt == 0)
386ac27a0ecSDave Kleikamp 		max_debt = 1;
387ac27a0ecSDave Kleikamp 
388ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
3892aa9fc4cSAvantika Mathur 		*group = (parent_group + i) % ngroups;
3902aa9fc4cSAvantika Mathur 		desc = ext4_get_group_desc(sb, *group, NULL);
391ac27a0ecSDave Kleikamp 		if (!desc || !desc->bg_free_inodes_count)
392ac27a0ecSDave Kleikamp 			continue;
393ac27a0ecSDave Kleikamp 		if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
394ac27a0ecSDave Kleikamp 			continue;
395ac27a0ecSDave Kleikamp 		if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
396ac27a0ecSDave Kleikamp 			continue;
397ac27a0ecSDave Kleikamp 		if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
398ac27a0ecSDave Kleikamp 			continue;
3992aa9fc4cSAvantika Mathur 		return 0;
400ac27a0ecSDave Kleikamp 	}
401ac27a0ecSDave Kleikamp 
402ac27a0ecSDave Kleikamp fallback:
403ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
4042aa9fc4cSAvantika Mathur 		*group = (parent_group + i) % ngroups;
4052aa9fc4cSAvantika Mathur 		desc = ext4_get_group_desc(sb, *group, NULL);
4062aa9fc4cSAvantika Mathur 		if (desc && desc->bg_free_inodes_count &&
4072aa9fc4cSAvantika Mathur 			le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
4082aa9fc4cSAvantika Mathur 			return 0;
409ac27a0ecSDave Kleikamp 	}
410ac27a0ecSDave Kleikamp 
411ac27a0ecSDave Kleikamp 	if (avefreei) {
412ac27a0ecSDave Kleikamp 		/*
413ac27a0ecSDave Kleikamp 		 * The free-inodes counter is approximate, and for really small
414ac27a0ecSDave Kleikamp 		 * filesystems the above test can fail to find any blockgroups
415ac27a0ecSDave Kleikamp 		 */
416ac27a0ecSDave Kleikamp 		avefreei = 0;
417ac27a0ecSDave Kleikamp 		goto fallback;
418ac27a0ecSDave Kleikamp 	}
419ac27a0ecSDave Kleikamp 
420ac27a0ecSDave Kleikamp 	return -1;
421ac27a0ecSDave Kleikamp }
422ac27a0ecSDave Kleikamp 
4232aa9fc4cSAvantika Mathur static int find_group_other(struct super_block *sb, struct inode *parent,
4242aa9fc4cSAvantika Mathur 				ext4_group_t *group)
425ac27a0ecSDave Kleikamp {
426fd2d4291SAvantika Mathur 	ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
427fd2d4291SAvantika Mathur 	ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
428617ba13bSMingming Cao 	struct ext4_group_desc *desc;
4292aa9fc4cSAvantika Mathur 	ext4_group_t i;
430ac27a0ecSDave Kleikamp 
431ac27a0ecSDave Kleikamp 	/*
432ac27a0ecSDave Kleikamp 	 * Try to place the inode in its parent directory
433ac27a0ecSDave Kleikamp 	 */
4342aa9fc4cSAvantika Mathur 	*group = parent_group;
4352aa9fc4cSAvantika Mathur 	desc = ext4_get_group_desc(sb, *group, NULL);
436ac27a0ecSDave Kleikamp 	if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
437ac27a0ecSDave Kleikamp 			le16_to_cpu(desc->bg_free_blocks_count))
4382aa9fc4cSAvantika Mathur 		return 0;
439ac27a0ecSDave Kleikamp 
440ac27a0ecSDave Kleikamp 	/*
441ac27a0ecSDave Kleikamp 	 * We're going to place this inode in a different blockgroup from its
442ac27a0ecSDave Kleikamp 	 * parent.  We want to cause files in a common directory to all land in
443ac27a0ecSDave Kleikamp 	 * the same blockgroup.  But we want files which are in a different
444ac27a0ecSDave Kleikamp 	 * directory which shares a blockgroup with our parent to land in a
445ac27a0ecSDave Kleikamp 	 * different blockgroup.
446ac27a0ecSDave Kleikamp 	 *
447ac27a0ecSDave Kleikamp 	 * So add our directory's i_ino into the starting point for the hash.
448ac27a0ecSDave Kleikamp 	 */
4492aa9fc4cSAvantika Mathur 	*group = (*group + parent->i_ino) % ngroups;
450ac27a0ecSDave Kleikamp 
451ac27a0ecSDave Kleikamp 	/*
452ac27a0ecSDave Kleikamp 	 * Use a quadratic hash to find a group with a free inode and some free
453ac27a0ecSDave Kleikamp 	 * blocks.
454ac27a0ecSDave Kleikamp 	 */
455ac27a0ecSDave Kleikamp 	for (i = 1; i < ngroups; i <<= 1) {
4562aa9fc4cSAvantika Mathur 		*group += i;
4572aa9fc4cSAvantika Mathur 		if (*group >= ngroups)
4582aa9fc4cSAvantika Mathur 			*group -= ngroups;
4592aa9fc4cSAvantika Mathur 		desc = ext4_get_group_desc(sb, *group, NULL);
460ac27a0ecSDave Kleikamp 		if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
461ac27a0ecSDave Kleikamp 				le16_to_cpu(desc->bg_free_blocks_count))
4622aa9fc4cSAvantika Mathur 			return 0;
463ac27a0ecSDave Kleikamp 	}
464ac27a0ecSDave Kleikamp 
465ac27a0ecSDave Kleikamp 	/*
466ac27a0ecSDave Kleikamp 	 * That failed: try linear search for a free inode, even if that group
467ac27a0ecSDave Kleikamp 	 * has no free blocks.
468ac27a0ecSDave Kleikamp 	 */
4692aa9fc4cSAvantika Mathur 	*group = parent_group;
470ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
4712aa9fc4cSAvantika Mathur 		if (++*group >= ngroups)
4722aa9fc4cSAvantika Mathur 			*group = 0;
4732aa9fc4cSAvantika Mathur 		desc = ext4_get_group_desc(sb, *group, NULL);
474ac27a0ecSDave Kleikamp 		if (desc && le16_to_cpu(desc->bg_free_inodes_count))
4752aa9fc4cSAvantika Mathur 			return 0;
476ac27a0ecSDave Kleikamp 	}
477ac27a0ecSDave Kleikamp 
478ac27a0ecSDave Kleikamp 	return -1;
479ac27a0ecSDave Kleikamp }
480ac27a0ecSDave Kleikamp 
481ac27a0ecSDave Kleikamp /*
482ac27a0ecSDave Kleikamp  * There are two policies for allocating an inode.  If the new inode is
483ac27a0ecSDave Kleikamp  * a directory, then a forward search is made for a block group with both
484ac27a0ecSDave Kleikamp  * free space and a low directory-to-inode ratio; if that fails, then of
485ac27a0ecSDave Kleikamp  * the groups with above-average free space, that group with the fewest
486ac27a0ecSDave Kleikamp  * directories already is chosen.
487ac27a0ecSDave Kleikamp  *
488ac27a0ecSDave Kleikamp  * For other inodes, search forward from the parent directory's block
489ac27a0ecSDave Kleikamp  * group to find a free inode.
490ac27a0ecSDave Kleikamp  */
491617ba13bSMingming Cao struct inode *ext4_new_inode(handle_t *handle, struct inode * dir, int mode)
492ac27a0ecSDave Kleikamp {
493ac27a0ecSDave Kleikamp 	struct super_block *sb;
494ac27a0ecSDave Kleikamp 	struct buffer_head *bitmap_bh = NULL;
495ac27a0ecSDave Kleikamp 	struct buffer_head *bh2;
4962aa9fc4cSAvantika Mathur 	ext4_group_t group = 0;
497ac27a0ecSDave Kleikamp 	unsigned long ino = 0;
498ac27a0ecSDave Kleikamp 	struct inode * inode;
499617ba13bSMingming Cao 	struct ext4_group_desc * gdp = NULL;
500617ba13bSMingming Cao 	struct ext4_super_block * es;
501617ba13bSMingming Cao 	struct ext4_inode_info *ei;
502617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
5032aa9fc4cSAvantika Mathur 	int ret2, err = 0;
504ac27a0ecSDave Kleikamp 	struct inode *ret;
5052aa9fc4cSAvantika Mathur 	ext4_group_t i;
5062aa9fc4cSAvantika Mathur 	int free = 0;
507ac27a0ecSDave Kleikamp 
508ac27a0ecSDave Kleikamp 	/* Cannot create files in a deleted directory */
509ac27a0ecSDave Kleikamp 	if (!dir || !dir->i_nlink)
510ac27a0ecSDave Kleikamp 		return ERR_PTR(-EPERM);
511ac27a0ecSDave Kleikamp 
512ac27a0ecSDave Kleikamp 	sb = dir->i_sb;
513ac27a0ecSDave Kleikamp 	inode = new_inode(sb);
514ac27a0ecSDave Kleikamp 	if (!inode)
515ac27a0ecSDave Kleikamp 		return ERR_PTR(-ENOMEM);
516617ba13bSMingming Cao 	ei = EXT4_I(inode);
517ac27a0ecSDave Kleikamp 
518617ba13bSMingming Cao 	sbi = EXT4_SB(sb);
519ac27a0ecSDave Kleikamp 	es = sbi->s_es;
520ac27a0ecSDave Kleikamp 	if (S_ISDIR(mode)) {
521ac27a0ecSDave Kleikamp 		if (test_opt (sb, OLDALLOC))
5222aa9fc4cSAvantika Mathur 			ret2 = find_group_dir(sb, dir, &group);
523ac27a0ecSDave Kleikamp 		else
5242aa9fc4cSAvantika Mathur 			ret2 = find_group_orlov(sb, dir, &group);
525ac27a0ecSDave Kleikamp 	} else
5262aa9fc4cSAvantika Mathur 		ret2 = find_group_other(sb, dir, &group);
527ac27a0ecSDave Kleikamp 
528ac27a0ecSDave Kleikamp 	err = -ENOSPC;
5292aa9fc4cSAvantika Mathur 	if (ret2 == -1)
530ac27a0ecSDave Kleikamp 		goto out;
531ac27a0ecSDave Kleikamp 
532ac27a0ecSDave Kleikamp 	for (i = 0; i < sbi->s_groups_count; i++) {
533ac27a0ecSDave Kleikamp 		err = -EIO;
534ac27a0ecSDave Kleikamp 
535617ba13bSMingming Cao 		gdp = ext4_get_group_desc(sb, group, &bh2);
536ac27a0ecSDave Kleikamp 		if (!gdp)
537ac27a0ecSDave Kleikamp 			goto fail;
538ac27a0ecSDave Kleikamp 
539ac27a0ecSDave Kleikamp 		brelse(bitmap_bh);
540ac27a0ecSDave Kleikamp 		bitmap_bh = read_inode_bitmap(sb, group);
541ac27a0ecSDave Kleikamp 		if (!bitmap_bh)
542ac27a0ecSDave Kleikamp 			goto fail;
543ac27a0ecSDave Kleikamp 
544ac27a0ecSDave Kleikamp 		ino = 0;
545ac27a0ecSDave Kleikamp 
546ac27a0ecSDave Kleikamp repeat_in_this_group:
547617ba13bSMingming Cao 		ino = ext4_find_next_zero_bit((unsigned long *)
548617ba13bSMingming Cao 				bitmap_bh->b_data, EXT4_INODES_PER_GROUP(sb), ino);
549617ba13bSMingming Cao 		if (ino < EXT4_INODES_PER_GROUP(sb)) {
550ac27a0ecSDave Kleikamp 
551ac27a0ecSDave Kleikamp 			BUFFER_TRACE(bitmap_bh, "get_write_access");
552617ba13bSMingming Cao 			err = ext4_journal_get_write_access(handle, bitmap_bh);
553ac27a0ecSDave Kleikamp 			if (err)
554ac27a0ecSDave Kleikamp 				goto fail;
555ac27a0ecSDave Kleikamp 
556617ba13bSMingming Cao 			if (!ext4_set_bit_atomic(sb_bgl_lock(sbi, group),
557ac27a0ecSDave Kleikamp 						ino, bitmap_bh->b_data)) {
558ac27a0ecSDave Kleikamp 				/* we won it */
559ac27a0ecSDave Kleikamp 				BUFFER_TRACE(bitmap_bh,
560617ba13bSMingming Cao 					"call ext4_journal_dirty_metadata");
561617ba13bSMingming Cao 				err = ext4_journal_dirty_metadata(handle,
562ac27a0ecSDave Kleikamp 								bitmap_bh);
563ac27a0ecSDave Kleikamp 				if (err)
564ac27a0ecSDave Kleikamp 					goto fail;
565ac27a0ecSDave Kleikamp 				goto got;
566ac27a0ecSDave Kleikamp 			}
567ac27a0ecSDave Kleikamp 			/* we lost it */
568dab291afSMingming Cao 			jbd2_journal_release_buffer(handle, bitmap_bh);
569ac27a0ecSDave Kleikamp 
570617ba13bSMingming Cao 			if (++ino < EXT4_INODES_PER_GROUP(sb))
571ac27a0ecSDave Kleikamp 				goto repeat_in_this_group;
572ac27a0ecSDave Kleikamp 		}
573ac27a0ecSDave Kleikamp 
574ac27a0ecSDave Kleikamp 		/*
575ac27a0ecSDave Kleikamp 		 * This case is possible in concurrent environment.  It is very
576ac27a0ecSDave Kleikamp 		 * rare.  We cannot repeat the find_group_xxx() call because
577ac27a0ecSDave Kleikamp 		 * that will simply return the same blockgroup, because the
578ac27a0ecSDave Kleikamp 		 * group descriptor metadata has not yet been updated.
579ac27a0ecSDave Kleikamp 		 * So we just go onto the next blockgroup.
580ac27a0ecSDave Kleikamp 		 */
581ac27a0ecSDave Kleikamp 		if (++group == sbi->s_groups_count)
582ac27a0ecSDave Kleikamp 			group = 0;
583ac27a0ecSDave Kleikamp 	}
584ac27a0ecSDave Kleikamp 	err = -ENOSPC;
585ac27a0ecSDave Kleikamp 	goto out;
586ac27a0ecSDave Kleikamp 
587ac27a0ecSDave Kleikamp got:
588717d50e4SAndreas Dilger 	ino++;
589717d50e4SAndreas Dilger 	if ((group == 0 && ino < EXT4_FIRST_INO(sb)) ||
590717d50e4SAndreas Dilger 	    ino > EXT4_INODES_PER_GROUP(sb)) {
591717d50e4SAndreas Dilger 		ext4_error(sb, __FUNCTION__,
592ac27a0ecSDave Kleikamp 			   "reserved inode or inode > inodes count - "
593fd2d4291SAvantika Mathur 			   "block_group = %lu, inode=%lu", group,
594717d50e4SAndreas Dilger 			   ino + group * EXT4_INODES_PER_GROUP(sb));
595ac27a0ecSDave Kleikamp 		err = -EIO;
596ac27a0ecSDave Kleikamp 		goto fail;
597ac27a0ecSDave Kleikamp 	}
598ac27a0ecSDave Kleikamp 
599ac27a0ecSDave Kleikamp 	BUFFER_TRACE(bh2, "get_write_access");
600617ba13bSMingming Cao 	err = ext4_journal_get_write_access(handle, bh2);
601ac27a0ecSDave Kleikamp 	if (err) goto fail;
602717d50e4SAndreas Dilger 
603717d50e4SAndreas Dilger 	/* We may have to initialize the block bitmap if it isn't already */
604717d50e4SAndreas Dilger 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
605717d50e4SAndreas Dilger 	    gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
606717d50e4SAndreas Dilger 		struct buffer_head *block_bh = read_block_bitmap(sb, group);
607717d50e4SAndreas Dilger 
608717d50e4SAndreas Dilger 		BUFFER_TRACE(block_bh, "get block bitmap access");
609717d50e4SAndreas Dilger 		err = ext4_journal_get_write_access(handle, block_bh);
610717d50e4SAndreas Dilger 		if (err) {
611717d50e4SAndreas Dilger 			brelse(block_bh);
612717d50e4SAndreas Dilger 			goto fail;
613717d50e4SAndreas Dilger 		}
614717d50e4SAndreas Dilger 
615717d50e4SAndreas Dilger 		free = 0;
616ac27a0ecSDave Kleikamp 		spin_lock(sb_bgl_lock(sbi, group));
617717d50e4SAndreas Dilger 		/* recheck and clear flag under lock if we still need to */
618717d50e4SAndreas Dilger 		if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
619717d50e4SAndreas Dilger 			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
620717d50e4SAndreas Dilger 			free = ext4_free_blocks_after_init(sb, group, gdp);
621717d50e4SAndreas Dilger 			gdp->bg_free_blocks_count = cpu_to_le16(free);
622717d50e4SAndreas Dilger 		}
623717d50e4SAndreas Dilger 		spin_unlock(sb_bgl_lock(sbi, group));
624717d50e4SAndreas Dilger 
625717d50e4SAndreas Dilger 		/* Don't need to dirty bitmap block if we didn't change it */
626717d50e4SAndreas Dilger 		if (free) {
627717d50e4SAndreas Dilger 			BUFFER_TRACE(block_bh, "dirty block bitmap");
628717d50e4SAndreas Dilger 			err = ext4_journal_dirty_metadata(handle, block_bh);
629717d50e4SAndreas Dilger 		}
630717d50e4SAndreas Dilger 
631717d50e4SAndreas Dilger 		brelse(block_bh);
632717d50e4SAndreas Dilger 		if (err)
633717d50e4SAndreas Dilger 			goto fail;
634717d50e4SAndreas Dilger 	}
635717d50e4SAndreas Dilger 
636717d50e4SAndreas Dilger 	spin_lock(sb_bgl_lock(sbi, group));
637717d50e4SAndreas Dilger 	/* If we didn't allocate from within the initialized part of the inode
638717d50e4SAndreas Dilger 	 * table then we need to initialize up to this inode. */
639717d50e4SAndreas Dilger 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
640717d50e4SAndreas Dilger 		if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
641717d50e4SAndreas Dilger 			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
642717d50e4SAndreas Dilger 
643717d50e4SAndreas Dilger 			/* When marking the block group with
644717d50e4SAndreas Dilger 			 * ~EXT4_BG_INODE_UNINIT we don't want to depend
645717d50e4SAndreas Dilger 			 * on the value of bg_itable_unsed even though
646717d50e4SAndreas Dilger 			 * mke2fs could have initialized the same for us.
647717d50e4SAndreas Dilger 			 * Instead we calculated the value below
648717d50e4SAndreas Dilger 			 */
649717d50e4SAndreas Dilger 
650717d50e4SAndreas Dilger 			free = 0;
651717d50e4SAndreas Dilger 		} else {
652717d50e4SAndreas Dilger 			free = EXT4_INODES_PER_GROUP(sb) -
653717d50e4SAndreas Dilger 				le16_to_cpu(gdp->bg_itable_unused);
654717d50e4SAndreas Dilger 		}
655717d50e4SAndreas Dilger 
656717d50e4SAndreas Dilger 		/*
657717d50e4SAndreas Dilger 		 * Check the relative inode number against the last used
658717d50e4SAndreas Dilger 		 * relative inode number in this group. if it is greater
659717d50e4SAndreas Dilger 		 * we need to  update the bg_itable_unused count
660717d50e4SAndreas Dilger 		 *
661717d50e4SAndreas Dilger 		 */
662717d50e4SAndreas Dilger 		if (ino > free)
663717d50e4SAndreas Dilger 			gdp->bg_itable_unused =
664717d50e4SAndreas Dilger 				cpu_to_le16(EXT4_INODES_PER_GROUP(sb) - ino);
665717d50e4SAndreas Dilger 	}
666717d50e4SAndreas Dilger 
667ac27a0ecSDave Kleikamp 	gdp->bg_free_inodes_count =
668ac27a0ecSDave Kleikamp 		cpu_to_le16(le16_to_cpu(gdp->bg_free_inodes_count) - 1);
669ac27a0ecSDave Kleikamp 	if (S_ISDIR(mode)) {
670ac27a0ecSDave Kleikamp 		gdp->bg_used_dirs_count =
671ac27a0ecSDave Kleikamp 			cpu_to_le16(le16_to_cpu(gdp->bg_used_dirs_count) + 1);
672ac27a0ecSDave Kleikamp 	}
673717d50e4SAndreas Dilger 	gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp);
674ac27a0ecSDave Kleikamp 	spin_unlock(sb_bgl_lock(sbi, group));
675617ba13bSMingming Cao 	BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata");
676617ba13bSMingming Cao 	err = ext4_journal_dirty_metadata(handle, bh2);
677ac27a0ecSDave Kleikamp 	if (err) goto fail;
678ac27a0ecSDave Kleikamp 
679ac27a0ecSDave Kleikamp 	percpu_counter_dec(&sbi->s_freeinodes_counter);
680ac27a0ecSDave Kleikamp 	if (S_ISDIR(mode))
681ac27a0ecSDave Kleikamp 		percpu_counter_inc(&sbi->s_dirs_counter);
682ac27a0ecSDave Kleikamp 	sb->s_dirt = 1;
683ac27a0ecSDave Kleikamp 
684ac27a0ecSDave Kleikamp 	inode->i_uid = current->fsuid;
685ac27a0ecSDave Kleikamp 	if (test_opt (sb, GRPID))
686ac27a0ecSDave Kleikamp 		inode->i_gid = dir->i_gid;
687ac27a0ecSDave Kleikamp 	else if (dir->i_mode & S_ISGID) {
688ac27a0ecSDave Kleikamp 		inode->i_gid = dir->i_gid;
689ac27a0ecSDave Kleikamp 		if (S_ISDIR(mode))
690ac27a0ecSDave Kleikamp 			mode |= S_ISGID;
691ac27a0ecSDave Kleikamp 	} else
692ac27a0ecSDave Kleikamp 		inode->i_gid = current->fsgid;
693ac27a0ecSDave Kleikamp 	inode->i_mode = mode;
694ac27a0ecSDave Kleikamp 
695717d50e4SAndreas Dilger 	inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
696ac27a0ecSDave Kleikamp 	/* This is the optimal IO size (for stat), not the fs block size */
697ac27a0ecSDave Kleikamp 	inode->i_blocks = 0;
698ef7f3835SKalpak Shah 	inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
699ef7f3835SKalpak Shah 						       ext4_current_time(inode);
700ac27a0ecSDave Kleikamp 
701ac27a0ecSDave Kleikamp 	memset(ei->i_data, 0, sizeof(ei->i_data));
702ac27a0ecSDave Kleikamp 	ei->i_dir_start_lookup = 0;
703ac27a0ecSDave Kleikamp 	ei->i_disksize = 0;
704ac27a0ecSDave Kleikamp 
705617ba13bSMingming Cao 	ei->i_flags = EXT4_I(dir)->i_flags & ~EXT4_INDEX_FL;
706ac27a0ecSDave Kleikamp 	if (S_ISLNK(mode))
707617ba13bSMingming Cao 		ei->i_flags &= ~(EXT4_IMMUTABLE_FL|EXT4_APPEND_FL);
708ac27a0ecSDave Kleikamp 	/* dirsync only applies to directories */
709ac27a0ecSDave Kleikamp 	if (!S_ISDIR(mode))
710617ba13bSMingming Cao 		ei->i_flags &= ~EXT4_DIRSYNC_FL;
711ac27a0ecSDave Kleikamp 	ei->i_file_acl = 0;
712ac27a0ecSDave Kleikamp 	ei->i_dtime = 0;
713ac27a0ecSDave Kleikamp 	ei->i_block_alloc_info = NULL;
714ac27a0ecSDave Kleikamp 	ei->i_block_group = group;
715ac27a0ecSDave Kleikamp 
716617ba13bSMingming Cao 	ext4_set_inode_flags(inode);
717ac27a0ecSDave Kleikamp 	if (IS_DIRSYNC(inode))
718ac27a0ecSDave Kleikamp 		handle->h_sync = 1;
719ac27a0ecSDave Kleikamp 	insert_inode_hash(inode);
720ac27a0ecSDave Kleikamp 	spin_lock(&sbi->s_next_gen_lock);
721ac27a0ecSDave Kleikamp 	inode->i_generation = sbi->s_next_generation++;
722ac27a0ecSDave Kleikamp 	spin_unlock(&sbi->s_next_gen_lock);
723ac27a0ecSDave Kleikamp 
724617ba13bSMingming Cao 	ei->i_state = EXT4_STATE_NEW;
725ef7f3835SKalpak Shah 
726ef7f3835SKalpak Shah 	ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize;
727ac27a0ecSDave Kleikamp 
728ac27a0ecSDave Kleikamp 	ret = inode;
729ac27a0ecSDave Kleikamp 	if(DQUOT_ALLOC_INODE(inode)) {
730ac27a0ecSDave Kleikamp 		err = -EDQUOT;
731ac27a0ecSDave Kleikamp 		goto fail_drop;
732ac27a0ecSDave Kleikamp 	}
733ac27a0ecSDave Kleikamp 
734617ba13bSMingming Cao 	err = ext4_init_acl(handle, inode, dir);
735ac27a0ecSDave Kleikamp 	if (err)
736ac27a0ecSDave Kleikamp 		goto fail_free_drop;
737ac27a0ecSDave Kleikamp 
738617ba13bSMingming Cao 	err = ext4_init_security(handle,inode, dir);
739ac27a0ecSDave Kleikamp 	if (err)
740ac27a0ecSDave Kleikamp 		goto fail_free_drop;
741ac27a0ecSDave Kleikamp 
742617ba13bSMingming Cao 	err = ext4_mark_inode_dirty(handle, inode);
743ac27a0ecSDave Kleikamp 	if (err) {
744617ba13bSMingming Cao 		ext4_std_error(sb, err);
745ac27a0ecSDave Kleikamp 		goto fail_free_drop;
746ac27a0ecSDave Kleikamp 	}
747a86c6181SAlex Tomas 	if (test_opt(sb, EXTENTS)) {
748a86c6181SAlex Tomas 		EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL;
749a86c6181SAlex Tomas 		ext4_ext_tree_init(handle, inode);
75099e6f829SAneesh Kumar K.V 		err = ext4_update_incompat_feature(handle, sb,
75199e6f829SAneesh Kumar K.V 						EXT4_FEATURE_INCOMPAT_EXTENTS);
75299e6f829SAneesh Kumar K.V 		if (err)
75399e6f829SAneesh Kumar K.V 			goto fail;
754a86c6181SAlex Tomas 	}
755ac27a0ecSDave Kleikamp 
756617ba13bSMingming Cao 	ext4_debug("allocating inode %lu\n", inode->i_ino);
757ac27a0ecSDave Kleikamp 	goto really_out;
758ac27a0ecSDave Kleikamp fail:
759617ba13bSMingming Cao 	ext4_std_error(sb, err);
760ac27a0ecSDave Kleikamp out:
761ac27a0ecSDave Kleikamp 	iput(inode);
762ac27a0ecSDave Kleikamp 	ret = ERR_PTR(err);
763ac27a0ecSDave Kleikamp really_out:
764ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
765ac27a0ecSDave Kleikamp 	return ret;
766ac27a0ecSDave Kleikamp 
767ac27a0ecSDave Kleikamp fail_free_drop:
768ac27a0ecSDave Kleikamp 	DQUOT_FREE_INODE(inode);
769ac27a0ecSDave Kleikamp 
770ac27a0ecSDave Kleikamp fail_drop:
771ac27a0ecSDave Kleikamp 	DQUOT_DROP(inode);
772ac27a0ecSDave Kleikamp 	inode->i_flags |= S_NOQUOTA;
773ac27a0ecSDave Kleikamp 	inode->i_nlink = 0;
774ac27a0ecSDave Kleikamp 	iput(inode);
775ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
776ac27a0ecSDave Kleikamp 	return ERR_PTR(err);
777ac27a0ecSDave Kleikamp }
778ac27a0ecSDave Kleikamp 
779ac27a0ecSDave Kleikamp /* Verify that we are loading a valid orphan from disk */
780617ba13bSMingming Cao struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
781ac27a0ecSDave Kleikamp {
782617ba13bSMingming Cao 	unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
783fd2d4291SAvantika Mathur 	ext4_group_t block_group;
784ac27a0ecSDave Kleikamp 	int bit;
785*1d1fe1eeSDavid Howells 	struct buffer_head *bitmap_bh;
786ac27a0ecSDave Kleikamp 	struct inode *inode = NULL;
787*1d1fe1eeSDavid Howells 	long err = -EIO;
788ac27a0ecSDave Kleikamp 
789ac27a0ecSDave Kleikamp 	/* Error cases - e2fsck has already cleaned up for us */
790ac27a0ecSDave Kleikamp 	if (ino > max_ino) {
791617ba13bSMingming Cao 		ext4_warning(sb, __FUNCTION__,
792ac27a0ecSDave Kleikamp 			     "bad orphan ino %lu!  e2fsck was run?", ino);
793*1d1fe1eeSDavid Howells 		goto error;
794ac27a0ecSDave Kleikamp 	}
795ac27a0ecSDave Kleikamp 
796617ba13bSMingming Cao 	block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
797617ba13bSMingming Cao 	bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
798ac27a0ecSDave Kleikamp 	bitmap_bh = read_inode_bitmap(sb, block_group);
799ac27a0ecSDave Kleikamp 	if (!bitmap_bh) {
800617ba13bSMingming Cao 		ext4_warning(sb, __FUNCTION__,
801ac27a0ecSDave Kleikamp 			     "inode bitmap error for orphan %lu", ino);
802*1d1fe1eeSDavid Howells 		goto error;
803ac27a0ecSDave Kleikamp 	}
804ac27a0ecSDave Kleikamp 
805ac27a0ecSDave Kleikamp 	/* Having the inode bit set should be a 100% indicator that this
806ac27a0ecSDave Kleikamp 	 * is a valid orphan (no e2fsck run on fs).  Orphans also include
807ac27a0ecSDave Kleikamp 	 * inodes that were being truncated, so we can't check i_nlink==0.
808ac27a0ecSDave Kleikamp 	 */
809*1d1fe1eeSDavid Howells 	if (!ext4_test_bit(bit, bitmap_bh->b_data))
810*1d1fe1eeSDavid Howells 		goto bad_orphan;
811*1d1fe1eeSDavid Howells 
812*1d1fe1eeSDavid Howells 	inode = ext4_iget(sb, ino);
813*1d1fe1eeSDavid Howells 	if (IS_ERR(inode))
814*1d1fe1eeSDavid Howells 		goto iget_failed;
815*1d1fe1eeSDavid Howells 
816*1d1fe1eeSDavid Howells 	if (NEXT_ORPHAN(inode) > max_ino)
817*1d1fe1eeSDavid Howells 		goto bad_orphan;
818*1d1fe1eeSDavid Howells 	brelse(bitmap_bh);
819*1d1fe1eeSDavid Howells 	return inode;
820*1d1fe1eeSDavid Howells 
821*1d1fe1eeSDavid Howells iget_failed:
822*1d1fe1eeSDavid Howells 	err = PTR_ERR(inode);
823*1d1fe1eeSDavid Howells 	inode = NULL;
824*1d1fe1eeSDavid Howells bad_orphan:
825617ba13bSMingming Cao 	ext4_warning(sb, __FUNCTION__,
826ac27a0ecSDave Kleikamp 		     "bad orphan inode %lu!  e2fsck was run?", ino);
827617ba13bSMingming Cao 	printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n",
828ac27a0ecSDave Kleikamp 	       bit, (unsigned long long)bitmap_bh->b_blocknr,
829617ba13bSMingming Cao 	       ext4_test_bit(bit, bitmap_bh->b_data));
830ac27a0ecSDave Kleikamp 	printk(KERN_NOTICE "inode=%p\n", inode);
831ac27a0ecSDave Kleikamp 	if (inode) {
832ac27a0ecSDave Kleikamp 		printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
833ac27a0ecSDave Kleikamp 		       is_bad_inode(inode));
834ac27a0ecSDave Kleikamp 		printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
835ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
836ac27a0ecSDave Kleikamp 		printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
837ac27a0ecSDave Kleikamp 		/* Avoid freeing blocks if we got a bad deleted inode */
838*1d1fe1eeSDavid Howells 		if (inode->i_nlink == 0)
839ac27a0ecSDave Kleikamp 			inode->i_blocks = 0;
840ac27a0ecSDave Kleikamp 		iput(inode);
841ac27a0ecSDave Kleikamp 	}
842ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
843*1d1fe1eeSDavid Howells error:
844*1d1fe1eeSDavid Howells 	return ERR_PTR(err);
845ac27a0ecSDave Kleikamp }
846ac27a0ecSDave Kleikamp 
847617ba13bSMingming Cao unsigned long ext4_count_free_inodes (struct super_block * sb)
848ac27a0ecSDave Kleikamp {
849ac27a0ecSDave Kleikamp 	unsigned long desc_count;
850617ba13bSMingming Cao 	struct ext4_group_desc *gdp;
851fd2d4291SAvantika Mathur 	ext4_group_t i;
852617ba13bSMingming Cao #ifdef EXT4FS_DEBUG
853617ba13bSMingming Cao 	struct ext4_super_block *es;
854ac27a0ecSDave Kleikamp 	unsigned long bitmap_count, x;
855ac27a0ecSDave Kleikamp 	struct buffer_head *bitmap_bh = NULL;
856ac27a0ecSDave Kleikamp 
857617ba13bSMingming Cao 	es = EXT4_SB(sb)->s_es;
858ac27a0ecSDave Kleikamp 	desc_count = 0;
859ac27a0ecSDave Kleikamp 	bitmap_count = 0;
860ac27a0ecSDave Kleikamp 	gdp = NULL;
861617ba13bSMingming Cao 	for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
862617ba13bSMingming Cao 		gdp = ext4_get_group_desc (sb, i, NULL);
863ac27a0ecSDave Kleikamp 		if (!gdp)
864ac27a0ecSDave Kleikamp 			continue;
865ac27a0ecSDave Kleikamp 		desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
866ac27a0ecSDave Kleikamp 		brelse(bitmap_bh);
867ac27a0ecSDave Kleikamp 		bitmap_bh = read_inode_bitmap(sb, i);
868ac27a0ecSDave Kleikamp 		if (!bitmap_bh)
869ac27a0ecSDave Kleikamp 			continue;
870ac27a0ecSDave Kleikamp 
871617ba13bSMingming Cao 		x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8);
872c549a95dSEric Sandeen 		printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
873ac27a0ecSDave Kleikamp 			i, le16_to_cpu(gdp->bg_free_inodes_count), x);
874ac27a0ecSDave Kleikamp 		bitmap_count += x;
875ac27a0ecSDave Kleikamp 	}
876ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
877617ba13bSMingming Cao 	printk("ext4_count_free_inodes: stored = %u, computed = %lu, %lu\n",
878ac27a0ecSDave Kleikamp 		le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
879ac27a0ecSDave Kleikamp 	return desc_count;
880ac27a0ecSDave Kleikamp #else
881ac27a0ecSDave Kleikamp 	desc_count = 0;
882617ba13bSMingming Cao 	for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
883617ba13bSMingming Cao 		gdp = ext4_get_group_desc (sb, i, NULL);
884ac27a0ecSDave Kleikamp 		if (!gdp)
885ac27a0ecSDave Kleikamp 			continue;
886ac27a0ecSDave Kleikamp 		desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
887ac27a0ecSDave Kleikamp 		cond_resched();
888ac27a0ecSDave Kleikamp 	}
889ac27a0ecSDave Kleikamp 	return desc_count;
890ac27a0ecSDave Kleikamp #endif
891ac27a0ecSDave Kleikamp }
892ac27a0ecSDave Kleikamp 
893ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
894617ba13bSMingming Cao unsigned long ext4_count_dirs (struct super_block * sb)
895ac27a0ecSDave Kleikamp {
896ac27a0ecSDave Kleikamp 	unsigned long count = 0;
897fd2d4291SAvantika Mathur 	ext4_group_t i;
898ac27a0ecSDave Kleikamp 
899617ba13bSMingming Cao 	for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
900617ba13bSMingming Cao 		struct ext4_group_desc *gdp = ext4_get_group_desc (sb, i, NULL);
901ac27a0ecSDave Kleikamp 		if (!gdp)
902ac27a0ecSDave Kleikamp 			continue;
903ac27a0ecSDave Kleikamp 		count += le16_to_cpu(gdp->bg_used_dirs_count);
904ac27a0ecSDave Kleikamp 	}
905ac27a0ecSDave Kleikamp 	return count;
906ac27a0ecSDave Kleikamp }
907ac27a0ecSDave Kleikamp 
908