xref: /linux/fs/ext4/ialloc.c (revision 8753e88f1b4345677620ec68f847222a6301e2fd)
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)) {
7746e665e9SHarvey Harrison 		ext4_error(sb, __func__, "Checksum bad for group %lu\n",
78717d50e4SAndreas Dilger 			   block_group);
79717d50e4SAndreas Dilger 		gdp->bg_free_blocks_count = 0;
80717d50e4SAndreas Dilger 		gdp->bg_free_inodes_count = 0;
81717d50e4SAndreas Dilger 		gdp->bg_itable_unused = 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 *
100fd2d4291SAvantika Mathur 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;
104ac27a0ecSDave Kleikamp 
105617ba13bSMingming Cao 	desc = ext4_get_group_desc(sb, block_group, NULL);
106ac27a0ecSDave Kleikamp 	if (!desc)
107ac27a0ecSDave Kleikamp 		goto error_out;
108717d50e4SAndreas Dilger 	if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
109717d50e4SAndreas Dilger 		bh = sb_getblk(sb, ext4_inode_bitmap(sb, desc));
110717d50e4SAndreas Dilger 		if (!buffer_uptodate(bh)) {
111717d50e4SAndreas Dilger 			lock_buffer(bh);
112717d50e4SAndreas Dilger 			if (!buffer_uptodate(bh)) {
113717d50e4SAndreas Dilger 				ext4_init_inode_bitmap(sb, bh, block_group,
114717d50e4SAndreas Dilger 						       desc);
115717d50e4SAndreas Dilger 				set_buffer_uptodate(bh);
116717d50e4SAndreas Dilger 			}
117717d50e4SAndreas Dilger 			unlock_buffer(bh);
118717d50e4SAndreas Dilger 		}
119717d50e4SAndreas Dilger 	} else {
1208fadc143SAlexandre Ratchov 		bh = sb_bread(sb, ext4_inode_bitmap(sb, desc));
121717d50e4SAndreas Dilger 	}
122ac27a0ecSDave Kleikamp 	if (!bh)
123617ba13bSMingming Cao 		ext4_error(sb, "read_inode_bitmap",
124ac27a0ecSDave Kleikamp 			    "Cannot read inode bitmap - "
125bd81d8eeSLaurent Vivier 			    "block_group = %lu, inode_bitmap = %llu",
1268fadc143SAlexandre Ratchov 			    block_group, ext4_inode_bitmap(sb, desc));
127ac27a0ecSDave Kleikamp error_out:
128ac27a0ecSDave Kleikamp 	return bh;
129ac27a0ecSDave Kleikamp }
130ac27a0ecSDave Kleikamp 
131ac27a0ecSDave Kleikamp /*
132ac27a0ecSDave Kleikamp  * NOTE! When we get the inode, we're the only people
133ac27a0ecSDave Kleikamp  * that have access to it, and as such there are no
134ac27a0ecSDave Kleikamp  * race conditions we have to worry about. The inode
135ac27a0ecSDave Kleikamp  * is not on the hash-lists, and it cannot be reached
136ac27a0ecSDave Kleikamp  * through the filesystem because the directory entry
137ac27a0ecSDave Kleikamp  * has been deleted earlier.
138ac27a0ecSDave Kleikamp  *
139ac27a0ecSDave Kleikamp  * HOWEVER: we must make sure that we get no aliases,
140ac27a0ecSDave Kleikamp  * which means that we have to call "clear_inode()"
141ac27a0ecSDave Kleikamp  * _before_ we mark the inode not in use in the inode
142ac27a0ecSDave Kleikamp  * bitmaps. Otherwise a newly created file might use
143ac27a0ecSDave Kleikamp  * the same inode number (not actually the same pointer
144ac27a0ecSDave Kleikamp  * though), and then we'd have two inodes sharing the
145ac27a0ecSDave Kleikamp  * same inode number and space on the harddisk.
146ac27a0ecSDave Kleikamp  */
147617ba13bSMingming Cao void ext4_free_inode (handle_t *handle, struct inode * inode)
148ac27a0ecSDave Kleikamp {
149ac27a0ecSDave Kleikamp 	struct super_block * sb = inode->i_sb;
150ac27a0ecSDave Kleikamp 	int is_directory;
151ac27a0ecSDave Kleikamp 	unsigned long ino;
152ac27a0ecSDave Kleikamp 	struct buffer_head *bitmap_bh = NULL;
153ac27a0ecSDave Kleikamp 	struct buffer_head *bh2;
154fd2d4291SAvantika Mathur 	ext4_group_t block_group;
155ac27a0ecSDave Kleikamp 	unsigned long bit;
156617ba13bSMingming Cao 	struct ext4_group_desc * gdp;
157617ba13bSMingming Cao 	struct ext4_super_block * es;
158617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
159ac27a0ecSDave Kleikamp 	int fatal = 0, err;
160ac27a0ecSDave Kleikamp 
161ac27a0ecSDave Kleikamp 	if (atomic_read(&inode->i_count) > 1) {
162617ba13bSMingming Cao 		printk ("ext4_free_inode: inode has count=%d\n",
163ac27a0ecSDave Kleikamp 					atomic_read(&inode->i_count));
164ac27a0ecSDave Kleikamp 		return;
165ac27a0ecSDave Kleikamp 	}
166ac27a0ecSDave Kleikamp 	if (inode->i_nlink) {
167617ba13bSMingming Cao 		printk ("ext4_free_inode: inode has nlink=%d\n",
168ac27a0ecSDave Kleikamp 			inode->i_nlink);
169ac27a0ecSDave Kleikamp 		return;
170ac27a0ecSDave Kleikamp 	}
171ac27a0ecSDave Kleikamp 	if (!sb) {
172617ba13bSMingming Cao 		printk("ext4_free_inode: inode on nonexistent device\n");
173ac27a0ecSDave Kleikamp 		return;
174ac27a0ecSDave Kleikamp 	}
175617ba13bSMingming Cao 	sbi = EXT4_SB(sb);
176ac27a0ecSDave Kleikamp 
177ac27a0ecSDave Kleikamp 	ino = inode->i_ino;
178617ba13bSMingming Cao 	ext4_debug ("freeing inode %lu\n", ino);
179ac27a0ecSDave Kleikamp 
180ac27a0ecSDave Kleikamp 	/*
181ac27a0ecSDave Kleikamp 	 * Note: we must free any quota before locking the superblock,
182ac27a0ecSDave Kleikamp 	 * as writing the quota to disk may need the lock as well.
183ac27a0ecSDave Kleikamp 	 */
184ac27a0ecSDave Kleikamp 	DQUOT_INIT(inode);
185617ba13bSMingming Cao 	ext4_xattr_delete_inode(handle, inode);
186ac27a0ecSDave Kleikamp 	DQUOT_FREE_INODE(inode);
187ac27a0ecSDave Kleikamp 	DQUOT_DROP(inode);
188ac27a0ecSDave Kleikamp 
189ac27a0ecSDave Kleikamp 	is_directory = S_ISDIR(inode->i_mode);
190ac27a0ecSDave Kleikamp 
191ac27a0ecSDave Kleikamp 	/* Do this BEFORE marking the inode not in use or returning an error */
192ac27a0ecSDave Kleikamp 	clear_inode (inode);
193ac27a0ecSDave Kleikamp 
194617ba13bSMingming Cao 	es = EXT4_SB(sb)->s_es;
195617ba13bSMingming Cao 	if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
196617ba13bSMingming Cao 		ext4_error (sb, "ext4_free_inode",
197ac27a0ecSDave Kleikamp 			    "reserved or nonexistent inode %lu", ino);
198ac27a0ecSDave Kleikamp 		goto error_return;
199ac27a0ecSDave Kleikamp 	}
200617ba13bSMingming Cao 	block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
201617ba13bSMingming Cao 	bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
202ac27a0ecSDave Kleikamp 	bitmap_bh = read_inode_bitmap(sb, block_group);
203ac27a0ecSDave Kleikamp 	if (!bitmap_bh)
204ac27a0ecSDave Kleikamp 		goto error_return;
205ac27a0ecSDave Kleikamp 
206ac27a0ecSDave Kleikamp 	BUFFER_TRACE(bitmap_bh, "get_write_access");
207617ba13bSMingming Cao 	fatal = ext4_journal_get_write_access(handle, bitmap_bh);
208ac27a0ecSDave Kleikamp 	if (fatal)
209ac27a0ecSDave Kleikamp 		goto error_return;
210ac27a0ecSDave Kleikamp 
211ac27a0ecSDave Kleikamp 	/* Ok, now we can actually update the inode bitmaps.. */
212617ba13bSMingming Cao 	if (!ext4_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
213ac27a0ecSDave Kleikamp 					bit, bitmap_bh->b_data))
214617ba13bSMingming Cao 		ext4_error (sb, "ext4_free_inode",
215ac27a0ecSDave Kleikamp 			      "bit already cleared for inode %lu", ino);
216ac27a0ecSDave Kleikamp 	else {
217617ba13bSMingming Cao 		gdp = ext4_get_group_desc (sb, block_group, &bh2);
218ac27a0ecSDave Kleikamp 
219ac27a0ecSDave Kleikamp 		BUFFER_TRACE(bh2, "get_write_access");
220617ba13bSMingming Cao 		fatal = ext4_journal_get_write_access(handle, bh2);
221ac27a0ecSDave Kleikamp 		if (fatal) goto error_return;
222ac27a0ecSDave Kleikamp 
223ac27a0ecSDave Kleikamp 		if (gdp) {
224ac27a0ecSDave Kleikamp 			spin_lock(sb_bgl_lock(sbi, block_group));
225e8546d06SMarcin Slusarz 			le16_add_cpu(&gdp->bg_free_inodes_count, 1);
226ac27a0ecSDave Kleikamp 			if (is_directory)
227e8546d06SMarcin Slusarz 				le16_add_cpu(&gdp->bg_used_dirs_count, -1);
228717d50e4SAndreas Dilger 			gdp->bg_checksum = ext4_group_desc_csum(sbi,
229717d50e4SAndreas Dilger 							block_group, gdp);
230ac27a0ecSDave Kleikamp 			spin_unlock(sb_bgl_lock(sbi, block_group));
231ac27a0ecSDave Kleikamp 			percpu_counter_inc(&sbi->s_freeinodes_counter);
232ac27a0ecSDave Kleikamp 			if (is_directory)
233ac27a0ecSDave Kleikamp 				percpu_counter_dec(&sbi->s_dirs_counter);
234ac27a0ecSDave Kleikamp 
235ac27a0ecSDave Kleikamp 		}
236617ba13bSMingming Cao 		BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata");
237617ba13bSMingming Cao 		err = ext4_journal_dirty_metadata(handle, bh2);
238ac27a0ecSDave Kleikamp 		if (!fatal) fatal = err;
239ac27a0ecSDave Kleikamp 	}
240617ba13bSMingming Cao 	BUFFER_TRACE(bitmap_bh, "call ext4_journal_dirty_metadata");
241617ba13bSMingming Cao 	err = ext4_journal_dirty_metadata(handle, bitmap_bh);
242ac27a0ecSDave Kleikamp 	if (!fatal)
243ac27a0ecSDave Kleikamp 		fatal = err;
244ac27a0ecSDave Kleikamp 	sb->s_dirt = 1;
245ac27a0ecSDave Kleikamp error_return:
246ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
247617ba13bSMingming Cao 	ext4_std_error(sb, fatal);
248ac27a0ecSDave Kleikamp }
249ac27a0ecSDave Kleikamp 
250ac27a0ecSDave Kleikamp /*
251ac27a0ecSDave Kleikamp  * There are two policies for allocating an inode.  If the new inode is
252ac27a0ecSDave Kleikamp  * a directory, then a forward search is made for a block group with both
253ac27a0ecSDave Kleikamp  * free space and a low directory-to-inode ratio; if that fails, then of
254ac27a0ecSDave Kleikamp  * the groups with above-average free space, that group with the fewest
255ac27a0ecSDave Kleikamp  * directories already is chosen.
256ac27a0ecSDave Kleikamp  *
257ac27a0ecSDave Kleikamp  * For other inodes, search forward from the parent directory\'s block
258ac27a0ecSDave Kleikamp  * group to find a free inode.
259ac27a0ecSDave Kleikamp  */
2602aa9fc4cSAvantika Mathur static int find_group_dir(struct super_block *sb, struct inode *parent,
2612aa9fc4cSAvantika Mathur 				ext4_group_t *best_group)
262ac27a0ecSDave Kleikamp {
263fd2d4291SAvantika Mathur 	ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
264ac27a0ecSDave Kleikamp 	unsigned int freei, avefreei;
265617ba13bSMingming Cao 	struct ext4_group_desc *desc, *best_desc = NULL;
2662aa9fc4cSAvantika Mathur 	ext4_group_t group;
2672aa9fc4cSAvantika Mathur 	int ret = -1;
268ac27a0ecSDave Kleikamp 
269617ba13bSMingming Cao 	freei = percpu_counter_read_positive(&EXT4_SB(sb)->s_freeinodes_counter);
270ac27a0ecSDave Kleikamp 	avefreei = freei / ngroups;
271ac27a0ecSDave Kleikamp 
272ac27a0ecSDave Kleikamp 	for (group = 0; group < ngroups; group++) {
273ef2fb679SEric Sandeen 		desc = ext4_get_group_desc (sb, group, NULL);
274ac27a0ecSDave Kleikamp 		if (!desc || !desc->bg_free_inodes_count)
275ac27a0ecSDave Kleikamp 			continue;
276ac27a0ecSDave Kleikamp 		if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
277ac27a0ecSDave Kleikamp 			continue;
278ac27a0ecSDave Kleikamp 		if (!best_desc ||
279ac27a0ecSDave Kleikamp 		    (le16_to_cpu(desc->bg_free_blocks_count) >
280ac27a0ecSDave Kleikamp 		     le16_to_cpu(best_desc->bg_free_blocks_count))) {
2812aa9fc4cSAvantika Mathur 			*best_group = group;
282ac27a0ecSDave Kleikamp 			best_desc = desc;
2832aa9fc4cSAvantika Mathur 			ret = 0;
284ac27a0ecSDave Kleikamp 		}
285ac27a0ecSDave Kleikamp 	}
2862aa9fc4cSAvantika Mathur 	return ret;
287ac27a0ecSDave Kleikamp }
288ac27a0ecSDave Kleikamp 
289ac27a0ecSDave Kleikamp /*
290ac27a0ecSDave Kleikamp  * Orlov's allocator for directories.
291ac27a0ecSDave Kleikamp  *
292ac27a0ecSDave Kleikamp  * We always try to spread first-level directories.
293ac27a0ecSDave Kleikamp  *
294ac27a0ecSDave Kleikamp  * If there are blockgroups with both free inodes and free blocks counts
295ac27a0ecSDave Kleikamp  * not worse than average we return one with smallest directory count.
296ac27a0ecSDave Kleikamp  * Otherwise we simply return a random group.
297ac27a0ecSDave Kleikamp  *
298ac27a0ecSDave Kleikamp  * For the rest rules look so:
299ac27a0ecSDave Kleikamp  *
300ac27a0ecSDave Kleikamp  * It's OK to put directory into a group unless
301ac27a0ecSDave Kleikamp  * it has too many directories already (max_dirs) or
302ac27a0ecSDave Kleikamp  * it has too few free inodes left (min_inodes) or
303ac27a0ecSDave Kleikamp  * it has too few free blocks left (min_blocks) or
304ac27a0ecSDave Kleikamp  * it's already running too large debt (max_debt).
3051cc8dcf5SBenoit Boissinot  * Parent's group is preferred, if it doesn't satisfy these
306ac27a0ecSDave Kleikamp  * conditions we search cyclically through the rest. If none
307ac27a0ecSDave Kleikamp  * of the groups look good we just look for a group with more
308ac27a0ecSDave Kleikamp  * free inodes than average (starting at parent's group).
309ac27a0ecSDave Kleikamp  *
310ac27a0ecSDave Kleikamp  * Debt is incremented each time we allocate a directory and decremented
311ac27a0ecSDave Kleikamp  * when we allocate an inode, within 0--255.
312ac27a0ecSDave Kleikamp  */
313ac27a0ecSDave Kleikamp 
314ac27a0ecSDave Kleikamp #define INODE_COST 64
315ac27a0ecSDave Kleikamp #define BLOCK_COST 256
316ac27a0ecSDave Kleikamp 
3172aa9fc4cSAvantika Mathur static int find_group_orlov(struct super_block *sb, struct inode *parent,
3182aa9fc4cSAvantika Mathur 				ext4_group_t *group)
319ac27a0ecSDave Kleikamp {
320fd2d4291SAvantika Mathur 	ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
321617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
322617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
323fd2d4291SAvantika Mathur 	ext4_group_t ngroups = sbi->s_groups_count;
324617ba13bSMingming Cao 	int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
325ac27a0ecSDave Kleikamp 	unsigned int freei, avefreei;
326617ba13bSMingming Cao 	ext4_fsblk_t freeb, avefreeb;
327617ba13bSMingming Cao 	ext4_fsblk_t blocks_per_dir;
328ac27a0ecSDave Kleikamp 	unsigned int ndirs;
329ac27a0ecSDave Kleikamp 	int max_debt, max_dirs, min_inodes;
330617ba13bSMingming Cao 	ext4_grpblk_t min_blocks;
3312aa9fc4cSAvantika Mathur 	ext4_group_t i;
332617ba13bSMingming Cao 	struct ext4_group_desc *desc;
333ac27a0ecSDave Kleikamp 
334ac27a0ecSDave Kleikamp 	freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
335ac27a0ecSDave Kleikamp 	avefreei = freei / ngroups;
336ac27a0ecSDave Kleikamp 	freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
3373a5b2ecdSMingming Cao 	avefreeb = freeb;
338f4e5bc24SAndrew Morton 	do_div(avefreeb, ngroups);
339ac27a0ecSDave Kleikamp 	ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
340ac27a0ecSDave Kleikamp 
341ac27a0ecSDave Kleikamp 	if ((parent == sb->s_root->d_inode) ||
342617ba13bSMingming Cao 	    (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL)) {
343ac27a0ecSDave Kleikamp 		int best_ndir = inodes_per_group;
3442aa9fc4cSAvantika Mathur 		ext4_group_t grp;
3452aa9fc4cSAvantika Mathur 		int ret = -1;
346ac27a0ecSDave Kleikamp 
3472aa9fc4cSAvantika Mathur 		get_random_bytes(&grp, sizeof(grp));
3482aa9fc4cSAvantika Mathur 		parent_group = (unsigned)grp % ngroups;
349ac27a0ecSDave Kleikamp 		for (i = 0; i < ngroups; i++) {
3502aa9fc4cSAvantika Mathur 			grp = (parent_group + i) % ngroups;
3512aa9fc4cSAvantika Mathur 			desc = ext4_get_group_desc(sb, grp, NULL);
352ac27a0ecSDave Kleikamp 			if (!desc || !desc->bg_free_inodes_count)
353ac27a0ecSDave Kleikamp 				continue;
354ac27a0ecSDave Kleikamp 			if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
355ac27a0ecSDave Kleikamp 				continue;
356ac27a0ecSDave Kleikamp 			if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
357ac27a0ecSDave Kleikamp 				continue;
358ac27a0ecSDave Kleikamp 			if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
359ac27a0ecSDave Kleikamp 				continue;
3602aa9fc4cSAvantika Mathur 			*group = grp;
3612aa9fc4cSAvantika Mathur 			ret = 0;
362ac27a0ecSDave Kleikamp 			best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
363ac27a0ecSDave Kleikamp 		}
3642aa9fc4cSAvantika Mathur 		if (ret == 0)
3652aa9fc4cSAvantika Mathur 			return ret;
366ac27a0ecSDave Kleikamp 		goto fallback;
367ac27a0ecSDave Kleikamp 	}
368ac27a0ecSDave Kleikamp 
369bd81d8eeSLaurent Vivier 	blocks_per_dir = ext4_blocks_count(es) - freeb;
370f4e5bc24SAndrew Morton 	do_div(blocks_per_dir, ndirs);
371ac27a0ecSDave Kleikamp 
372ac27a0ecSDave Kleikamp 	max_dirs = ndirs / ngroups + inodes_per_group / 16;
373ac27a0ecSDave Kleikamp 	min_inodes = avefreei - inodes_per_group / 4;
374617ba13bSMingming Cao 	min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb) / 4;
375ac27a0ecSDave Kleikamp 
3763a5b2ecdSMingming Cao 	max_debt = EXT4_BLOCKS_PER_GROUP(sb);
377f4e5bc24SAndrew Morton 	max_debt /= max_t(int, blocks_per_dir, BLOCK_COST);
378ac27a0ecSDave Kleikamp 	if (max_debt * INODE_COST > inodes_per_group)
379ac27a0ecSDave Kleikamp 		max_debt = inodes_per_group / INODE_COST;
380ac27a0ecSDave Kleikamp 	if (max_debt > 255)
381ac27a0ecSDave Kleikamp 		max_debt = 255;
382ac27a0ecSDave Kleikamp 	if (max_debt == 0)
383ac27a0ecSDave Kleikamp 		max_debt = 1;
384ac27a0ecSDave Kleikamp 
385ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
3862aa9fc4cSAvantika Mathur 		*group = (parent_group + i) % ngroups;
3872aa9fc4cSAvantika Mathur 		desc = ext4_get_group_desc(sb, *group, NULL);
388ac27a0ecSDave Kleikamp 		if (!desc || !desc->bg_free_inodes_count)
389ac27a0ecSDave Kleikamp 			continue;
390ac27a0ecSDave Kleikamp 		if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
391ac27a0ecSDave Kleikamp 			continue;
392ac27a0ecSDave Kleikamp 		if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
393ac27a0ecSDave Kleikamp 			continue;
394ac27a0ecSDave Kleikamp 		if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
395ac27a0ecSDave Kleikamp 			continue;
3962aa9fc4cSAvantika Mathur 		return 0;
397ac27a0ecSDave Kleikamp 	}
398ac27a0ecSDave Kleikamp 
399ac27a0ecSDave Kleikamp fallback:
400ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
4012aa9fc4cSAvantika Mathur 		*group = (parent_group + i) % ngroups;
4022aa9fc4cSAvantika Mathur 		desc = ext4_get_group_desc(sb, *group, NULL);
4032aa9fc4cSAvantika Mathur 		if (desc && desc->bg_free_inodes_count &&
4042aa9fc4cSAvantika Mathur 			le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
4052aa9fc4cSAvantika Mathur 			return 0;
406ac27a0ecSDave Kleikamp 	}
407ac27a0ecSDave Kleikamp 
408ac27a0ecSDave Kleikamp 	if (avefreei) {
409ac27a0ecSDave Kleikamp 		/*
410ac27a0ecSDave Kleikamp 		 * The free-inodes counter is approximate, and for really small
411ac27a0ecSDave Kleikamp 		 * filesystems the above test can fail to find any blockgroups
412ac27a0ecSDave Kleikamp 		 */
413ac27a0ecSDave Kleikamp 		avefreei = 0;
414ac27a0ecSDave Kleikamp 		goto fallback;
415ac27a0ecSDave Kleikamp 	}
416ac27a0ecSDave Kleikamp 
417ac27a0ecSDave Kleikamp 	return -1;
418ac27a0ecSDave Kleikamp }
419ac27a0ecSDave Kleikamp 
4202aa9fc4cSAvantika Mathur static int find_group_other(struct super_block *sb, struct inode *parent,
4212aa9fc4cSAvantika Mathur 				ext4_group_t *group)
422ac27a0ecSDave Kleikamp {
423fd2d4291SAvantika Mathur 	ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
424fd2d4291SAvantika Mathur 	ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
425617ba13bSMingming Cao 	struct ext4_group_desc *desc;
4262aa9fc4cSAvantika Mathur 	ext4_group_t i;
427ac27a0ecSDave Kleikamp 
428ac27a0ecSDave Kleikamp 	/*
429ac27a0ecSDave Kleikamp 	 * Try to place the inode in its parent directory
430ac27a0ecSDave Kleikamp 	 */
4312aa9fc4cSAvantika Mathur 	*group = parent_group;
4322aa9fc4cSAvantika Mathur 	desc = ext4_get_group_desc(sb, *group, NULL);
433ac27a0ecSDave Kleikamp 	if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
434ac27a0ecSDave Kleikamp 			le16_to_cpu(desc->bg_free_blocks_count))
4352aa9fc4cSAvantika Mathur 		return 0;
436ac27a0ecSDave Kleikamp 
437ac27a0ecSDave Kleikamp 	/*
438ac27a0ecSDave Kleikamp 	 * We're going to place this inode in a different blockgroup from its
439ac27a0ecSDave Kleikamp 	 * parent.  We want to cause files in a common directory to all land in
440ac27a0ecSDave Kleikamp 	 * the same blockgroup.  But we want files which are in a different
441ac27a0ecSDave Kleikamp 	 * directory which shares a blockgroup with our parent to land in a
442ac27a0ecSDave Kleikamp 	 * different blockgroup.
443ac27a0ecSDave Kleikamp 	 *
444ac27a0ecSDave Kleikamp 	 * So add our directory's i_ino into the starting point for the hash.
445ac27a0ecSDave Kleikamp 	 */
4462aa9fc4cSAvantika Mathur 	*group = (*group + parent->i_ino) % ngroups;
447ac27a0ecSDave Kleikamp 
448ac27a0ecSDave Kleikamp 	/*
449ac27a0ecSDave Kleikamp 	 * Use a quadratic hash to find a group with a free inode and some free
450ac27a0ecSDave Kleikamp 	 * blocks.
451ac27a0ecSDave Kleikamp 	 */
452ac27a0ecSDave Kleikamp 	for (i = 1; i < ngroups; i <<= 1) {
4532aa9fc4cSAvantika Mathur 		*group += i;
4542aa9fc4cSAvantika Mathur 		if (*group >= ngroups)
4552aa9fc4cSAvantika Mathur 			*group -= ngroups;
4562aa9fc4cSAvantika Mathur 		desc = ext4_get_group_desc(sb, *group, NULL);
457ac27a0ecSDave Kleikamp 		if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
458ac27a0ecSDave Kleikamp 				le16_to_cpu(desc->bg_free_blocks_count))
4592aa9fc4cSAvantika Mathur 			return 0;
460ac27a0ecSDave Kleikamp 	}
461ac27a0ecSDave Kleikamp 
462ac27a0ecSDave Kleikamp 	/*
463ac27a0ecSDave Kleikamp 	 * That failed: try linear search for a free inode, even if that group
464ac27a0ecSDave Kleikamp 	 * has no free blocks.
465ac27a0ecSDave Kleikamp 	 */
4662aa9fc4cSAvantika Mathur 	*group = parent_group;
467ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
4682aa9fc4cSAvantika Mathur 		if (++*group >= ngroups)
4692aa9fc4cSAvantika Mathur 			*group = 0;
4702aa9fc4cSAvantika Mathur 		desc = ext4_get_group_desc(sb, *group, NULL);
471ac27a0ecSDave Kleikamp 		if (desc && le16_to_cpu(desc->bg_free_inodes_count))
4722aa9fc4cSAvantika Mathur 			return 0;
473ac27a0ecSDave Kleikamp 	}
474ac27a0ecSDave Kleikamp 
475ac27a0ecSDave Kleikamp 	return -1;
476ac27a0ecSDave Kleikamp }
477ac27a0ecSDave Kleikamp 
478ac27a0ecSDave Kleikamp /*
479ac27a0ecSDave Kleikamp  * There are two policies for allocating an inode.  If the new inode is
480ac27a0ecSDave Kleikamp  * a directory, then a forward search is made for a block group with both
481ac27a0ecSDave Kleikamp  * free space and a low directory-to-inode ratio; if that fails, then of
482ac27a0ecSDave Kleikamp  * the groups with above-average free space, that group with the fewest
483ac27a0ecSDave Kleikamp  * directories already is chosen.
484ac27a0ecSDave Kleikamp  *
485ac27a0ecSDave Kleikamp  * For other inodes, search forward from the parent directory's block
486ac27a0ecSDave Kleikamp  * group to find a free inode.
487ac27a0ecSDave Kleikamp  */
488617ba13bSMingming Cao struct inode *ext4_new_inode(handle_t *handle, struct inode * dir, int mode)
489ac27a0ecSDave Kleikamp {
490ac27a0ecSDave Kleikamp 	struct super_block *sb;
491ac27a0ecSDave Kleikamp 	struct buffer_head *bitmap_bh = NULL;
492ac27a0ecSDave Kleikamp 	struct buffer_head *bh2;
4932aa9fc4cSAvantika Mathur 	ext4_group_t group = 0;
494ac27a0ecSDave Kleikamp 	unsigned long ino = 0;
495ac27a0ecSDave Kleikamp 	struct inode * inode;
496617ba13bSMingming Cao 	struct ext4_group_desc * gdp = NULL;
497617ba13bSMingming Cao 	struct ext4_super_block * es;
498617ba13bSMingming Cao 	struct ext4_inode_info *ei;
499617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
5002aa9fc4cSAvantika Mathur 	int ret2, err = 0;
501ac27a0ecSDave Kleikamp 	struct inode *ret;
5022aa9fc4cSAvantika Mathur 	ext4_group_t i;
5032aa9fc4cSAvantika Mathur 	int free = 0;
504ac27a0ecSDave Kleikamp 
505ac27a0ecSDave Kleikamp 	/* Cannot create files in a deleted directory */
506ac27a0ecSDave Kleikamp 	if (!dir || !dir->i_nlink)
507ac27a0ecSDave Kleikamp 		return ERR_PTR(-EPERM);
508ac27a0ecSDave Kleikamp 
509ac27a0ecSDave Kleikamp 	sb = dir->i_sb;
510ac27a0ecSDave Kleikamp 	inode = new_inode(sb);
511ac27a0ecSDave Kleikamp 	if (!inode)
512ac27a0ecSDave Kleikamp 		return ERR_PTR(-ENOMEM);
513617ba13bSMingming Cao 	ei = EXT4_I(inode);
514ac27a0ecSDave Kleikamp 
515617ba13bSMingming Cao 	sbi = EXT4_SB(sb);
516ac27a0ecSDave Kleikamp 	es = sbi->s_es;
517ac27a0ecSDave Kleikamp 	if (S_ISDIR(mode)) {
518ac27a0ecSDave Kleikamp 		if (test_opt (sb, OLDALLOC))
5192aa9fc4cSAvantika Mathur 			ret2 = find_group_dir(sb, dir, &group);
520ac27a0ecSDave Kleikamp 		else
5212aa9fc4cSAvantika Mathur 			ret2 = find_group_orlov(sb, dir, &group);
522ac27a0ecSDave Kleikamp 	} else
5232aa9fc4cSAvantika Mathur 		ret2 = find_group_other(sb, dir, &group);
524ac27a0ecSDave Kleikamp 
525ac27a0ecSDave Kleikamp 	err = -ENOSPC;
5262aa9fc4cSAvantika Mathur 	if (ret2 == -1)
527ac27a0ecSDave Kleikamp 		goto out;
528ac27a0ecSDave Kleikamp 
529ac27a0ecSDave Kleikamp 	for (i = 0; i < sbi->s_groups_count; i++) {
530ac27a0ecSDave Kleikamp 		err = -EIO;
531ac27a0ecSDave Kleikamp 
532617ba13bSMingming Cao 		gdp = ext4_get_group_desc(sb, group, &bh2);
533ac27a0ecSDave Kleikamp 		if (!gdp)
534ac27a0ecSDave Kleikamp 			goto fail;
535ac27a0ecSDave Kleikamp 
536ac27a0ecSDave Kleikamp 		brelse(bitmap_bh);
537ac27a0ecSDave Kleikamp 		bitmap_bh = read_inode_bitmap(sb, group);
538ac27a0ecSDave Kleikamp 		if (!bitmap_bh)
539ac27a0ecSDave Kleikamp 			goto fail;
540ac27a0ecSDave Kleikamp 
541ac27a0ecSDave Kleikamp 		ino = 0;
542ac27a0ecSDave Kleikamp 
543ac27a0ecSDave Kleikamp repeat_in_this_group:
544617ba13bSMingming Cao 		ino = ext4_find_next_zero_bit((unsigned long *)
545617ba13bSMingming Cao 				bitmap_bh->b_data, EXT4_INODES_PER_GROUP(sb), ino);
546617ba13bSMingming Cao 		if (ino < EXT4_INODES_PER_GROUP(sb)) {
547ac27a0ecSDave Kleikamp 
548ac27a0ecSDave Kleikamp 			BUFFER_TRACE(bitmap_bh, "get_write_access");
549617ba13bSMingming Cao 			err = ext4_journal_get_write_access(handle, bitmap_bh);
550ac27a0ecSDave Kleikamp 			if (err)
551ac27a0ecSDave Kleikamp 				goto fail;
552ac27a0ecSDave Kleikamp 
553617ba13bSMingming Cao 			if (!ext4_set_bit_atomic(sb_bgl_lock(sbi, group),
554ac27a0ecSDave Kleikamp 						ino, bitmap_bh->b_data)) {
555ac27a0ecSDave Kleikamp 				/* we won it */
556ac27a0ecSDave Kleikamp 				BUFFER_TRACE(bitmap_bh,
557617ba13bSMingming Cao 					"call ext4_journal_dirty_metadata");
558617ba13bSMingming Cao 				err = ext4_journal_dirty_metadata(handle,
559ac27a0ecSDave Kleikamp 								bitmap_bh);
560ac27a0ecSDave Kleikamp 				if (err)
561ac27a0ecSDave Kleikamp 					goto fail;
562ac27a0ecSDave Kleikamp 				goto got;
563ac27a0ecSDave Kleikamp 			}
564ac27a0ecSDave Kleikamp 			/* we lost it */
565dab291afSMingming Cao 			jbd2_journal_release_buffer(handle, bitmap_bh);
566ac27a0ecSDave Kleikamp 
567617ba13bSMingming Cao 			if (++ino < EXT4_INODES_PER_GROUP(sb))
568ac27a0ecSDave Kleikamp 				goto repeat_in_this_group;
569ac27a0ecSDave Kleikamp 		}
570ac27a0ecSDave Kleikamp 
571ac27a0ecSDave Kleikamp 		/*
572ac27a0ecSDave Kleikamp 		 * This case is possible in concurrent environment.  It is very
573ac27a0ecSDave Kleikamp 		 * rare.  We cannot repeat the find_group_xxx() call because
574ac27a0ecSDave Kleikamp 		 * that will simply return the same blockgroup, because the
575ac27a0ecSDave Kleikamp 		 * group descriptor metadata has not yet been updated.
576ac27a0ecSDave Kleikamp 		 * So we just go onto the next blockgroup.
577ac27a0ecSDave Kleikamp 		 */
578ac27a0ecSDave Kleikamp 		if (++group == sbi->s_groups_count)
579ac27a0ecSDave Kleikamp 			group = 0;
580ac27a0ecSDave Kleikamp 	}
581ac27a0ecSDave Kleikamp 	err = -ENOSPC;
582ac27a0ecSDave Kleikamp 	goto out;
583ac27a0ecSDave Kleikamp 
584ac27a0ecSDave Kleikamp got:
585717d50e4SAndreas Dilger 	ino++;
586717d50e4SAndreas Dilger 	if ((group == 0 && ino < EXT4_FIRST_INO(sb)) ||
587717d50e4SAndreas Dilger 	    ino > EXT4_INODES_PER_GROUP(sb)) {
58846e665e9SHarvey Harrison 		ext4_error(sb, __func__,
589ac27a0ecSDave Kleikamp 			   "reserved inode or inode > inodes count - "
590fd2d4291SAvantika Mathur 			   "block_group = %lu, inode=%lu", group,
591717d50e4SAndreas Dilger 			   ino + group * EXT4_INODES_PER_GROUP(sb));
592ac27a0ecSDave Kleikamp 		err = -EIO;
593ac27a0ecSDave Kleikamp 		goto fail;
594ac27a0ecSDave Kleikamp 	}
595ac27a0ecSDave Kleikamp 
596ac27a0ecSDave Kleikamp 	BUFFER_TRACE(bh2, "get_write_access");
597617ba13bSMingming Cao 	err = ext4_journal_get_write_access(handle, bh2);
598ac27a0ecSDave Kleikamp 	if (err) goto fail;
599717d50e4SAndreas Dilger 
600717d50e4SAndreas Dilger 	/* We may have to initialize the block bitmap if it isn't already */
601717d50e4SAndreas Dilger 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
602717d50e4SAndreas Dilger 	    gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
603717d50e4SAndreas Dilger 		struct buffer_head *block_bh = read_block_bitmap(sb, group);
604717d50e4SAndreas Dilger 
605717d50e4SAndreas Dilger 		BUFFER_TRACE(block_bh, "get block bitmap access");
606717d50e4SAndreas Dilger 		err = ext4_journal_get_write_access(handle, block_bh);
607717d50e4SAndreas Dilger 		if (err) {
608717d50e4SAndreas Dilger 			brelse(block_bh);
609717d50e4SAndreas Dilger 			goto fail;
610717d50e4SAndreas Dilger 		}
611717d50e4SAndreas Dilger 
612717d50e4SAndreas Dilger 		free = 0;
613ac27a0ecSDave Kleikamp 		spin_lock(sb_bgl_lock(sbi, group));
614717d50e4SAndreas Dilger 		/* recheck and clear flag under lock if we still need to */
615717d50e4SAndreas Dilger 		if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
616717d50e4SAndreas Dilger 			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
617717d50e4SAndreas Dilger 			free = ext4_free_blocks_after_init(sb, group, gdp);
618717d50e4SAndreas Dilger 			gdp->bg_free_blocks_count = cpu_to_le16(free);
619717d50e4SAndreas Dilger 		}
620717d50e4SAndreas Dilger 		spin_unlock(sb_bgl_lock(sbi, group));
621717d50e4SAndreas Dilger 
622717d50e4SAndreas Dilger 		/* Don't need to dirty bitmap block if we didn't change it */
623717d50e4SAndreas Dilger 		if (free) {
624717d50e4SAndreas Dilger 			BUFFER_TRACE(block_bh, "dirty block bitmap");
625717d50e4SAndreas Dilger 			err = ext4_journal_dirty_metadata(handle, block_bh);
626717d50e4SAndreas Dilger 		}
627717d50e4SAndreas Dilger 
628717d50e4SAndreas Dilger 		brelse(block_bh);
629717d50e4SAndreas Dilger 		if (err)
630717d50e4SAndreas Dilger 			goto fail;
631717d50e4SAndreas Dilger 	}
632717d50e4SAndreas Dilger 
633717d50e4SAndreas Dilger 	spin_lock(sb_bgl_lock(sbi, group));
634717d50e4SAndreas Dilger 	/* If we didn't allocate from within the initialized part of the inode
635717d50e4SAndreas Dilger 	 * table then we need to initialize up to this inode. */
636717d50e4SAndreas Dilger 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
637717d50e4SAndreas Dilger 		if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
638717d50e4SAndreas Dilger 			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
639717d50e4SAndreas Dilger 
640717d50e4SAndreas Dilger 			/* When marking the block group with
641717d50e4SAndreas Dilger 			 * ~EXT4_BG_INODE_UNINIT we don't want to depend
642717d50e4SAndreas Dilger 			 * on the value of bg_itable_unsed even though
643717d50e4SAndreas Dilger 			 * mke2fs could have initialized the same for us.
644717d50e4SAndreas Dilger 			 * Instead we calculated the value below
645717d50e4SAndreas Dilger 			 */
646717d50e4SAndreas Dilger 
647717d50e4SAndreas Dilger 			free = 0;
648717d50e4SAndreas Dilger 		} else {
649717d50e4SAndreas Dilger 			free = EXT4_INODES_PER_GROUP(sb) -
650717d50e4SAndreas Dilger 				le16_to_cpu(gdp->bg_itable_unused);
651717d50e4SAndreas Dilger 		}
652717d50e4SAndreas Dilger 
653717d50e4SAndreas Dilger 		/*
654717d50e4SAndreas Dilger 		 * Check the relative inode number against the last used
655717d50e4SAndreas Dilger 		 * relative inode number in this group. if it is greater
656717d50e4SAndreas Dilger 		 * we need to  update the bg_itable_unused count
657717d50e4SAndreas Dilger 		 *
658717d50e4SAndreas Dilger 		 */
659717d50e4SAndreas Dilger 		if (ino > free)
660717d50e4SAndreas Dilger 			gdp->bg_itable_unused =
661717d50e4SAndreas Dilger 				cpu_to_le16(EXT4_INODES_PER_GROUP(sb) - ino);
662717d50e4SAndreas Dilger 	}
663717d50e4SAndreas Dilger 
664e8546d06SMarcin Slusarz 	le16_add_cpu(&gdp->bg_free_inodes_count, -1);
665ac27a0ecSDave Kleikamp 	if (S_ISDIR(mode)) {
666e8546d06SMarcin Slusarz 		le16_add_cpu(&gdp->bg_used_dirs_count, 1);
667ac27a0ecSDave Kleikamp 	}
668717d50e4SAndreas Dilger 	gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp);
669ac27a0ecSDave Kleikamp 	spin_unlock(sb_bgl_lock(sbi, group));
670617ba13bSMingming Cao 	BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata");
671617ba13bSMingming Cao 	err = ext4_journal_dirty_metadata(handle, bh2);
672ac27a0ecSDave Kleikamp 	if (err) goto fail;
673ac27a0ecSDave Kleikamp 
674ac27a0ecSDave Kleikamp 	percpu_counter_dec(&sbi->s_freeinodes_counter);
675ac27a0ecSDave Kleikamp 	if (S_ISDIR(mode))
676ac27a0ecSDave Kleikamp 		percpu_counter_inc(&sbi->s_dirs_counter);
677ac27a0ecSDave Kleikamp 	sb->s_dirt = 1;
678ac27a0ecSDave Kleikamp 
679ac27a0ecSDave Kleikamp 	inode->i_uid = current->fsuid;
680ac27a0ecSDave Kleikamp 	if (test_opt (sb, GRPID))
681ac27a0ecSDave Kleikamp 		inode->i_gid = dir->i_gid;
682ac27a0ecSDave Kleikamp 	else if (dir->i_mode & S_ISGID) {
683ac27a0ecSDave Kleikamp 		inode->i_gid = dir->i_gid;
684ac27a0ecSDave Kleikamp 		if (S_ISDIR(mode))
685ac27a0ecSDave Kleikamp 			mode |= S_ISGID;
686ac27a0ecSDave Kleikamp 	} else
687ac27a0ecSDave Kleikamp 		inode->i_gid = current->fsgid;
688ac27a0ecSDave Kleikamp 	inode->i_mode = mode;
689ac27a0ecSDave Kleikamp 
690717d50e4SAndreas Dilger 	inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
691ac27a0ecSDave Kleikamp 	/* This is the optimal IO size (for stat), not the fs block size */
692ac27a0ecSDave Kleikamp 	inode->i_blocks = 0;
693ef7f3835SKalpak Shah 	inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
694ef7f3835SKalpak Shah 						       ext4_current_time(inode);
695ac27a0ecSDave Kleikamp 
696ac27a0ecSDave Kleikamp 	memset(ei->i_data, 0, sizeof(ei->i_data));
697ac27a0ecSDave Kleikamp 	ei->i_dir_start_lookup = 0;
698ac27a0ecSDave Kleikamp 	ei->i_disksize = 0;
699ac27a0ecSDave Kleikamp 
70042bf0383SAneesh Kumar K.V 	/*
70142bf0383SAneesh Kumar K.V 	 * Don't inherit extent flag from directory. We set extent flag on
70242bf0383SAneesh Kumar K.V 	 * newly created directory and file only if -o extent mount option is
70342bf0383SAneesh Kumar K.V 	 * specified
70442bf0383SAneesh Kumar K.V 	 */
70542bf0383SAneesh Kumar K.V 	ei->i_flags = EXT4_I(dir)->i_flags & ~(EXT4_INDEX_FL|EXT4_EXTENTS_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 
742a86c6181SAlex Tomas 	if (test_opt(sb, EXTENTS)) {
743e65187e6SAneesh Kumar K.V 		/* set extent flag only for diretory, file and normal symlink*/
744e65187e6SAneesh Kumar K.V 		if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
745a86c6181SAlex Tomas 			EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL;
746a86c6181SAlex Tomas 			ext4_ext_tree_init(handle, inode);
74799e6f829SAneesh Kumar K.V 			err = ext4_update_incompat_feature(handle, sb,
74899e6f829SAneesh Kumar K.V 					EXT4_FEATURE_INCOMPAT_EXTENTS);
74999e6f829SAneesh Kumar K.V 			if (err)
750*8753e88fSAneesh Kumar K.V 				goto fail_free_drop;
751a86c6181SAlex Tomas 		}
75242bf0383SAneesh Kumar K.V 	}
753ac27a0ecSDave Kleikamp 
754*8753e88fSAneesh Kumar K.V 	err = ext4_mark_inode_dirty(handle, inode);
755*8753e88fSAneesh Kumar K.V 	if (err) {
756*8753e88fSAneesh Kumar K.V 		ext4_std_error(sb, err);
757*8753e88fSAneesh Kumar K.V 		goto fail_free_drop;
758*8753e88fSAneesh Kumar K.V 	}
759*8753e88fSAneesh Kumar K.V 
760617ba13bSMingming Cao 	ext4_debug("allocating inode %lu\n", inode->i_ino);
761ac27a0ecSDave Kleikamp 	goto really_out;
762ac27a0ecSDave Kleikamp fail:
763617ba13bSMingming Cao 	ext4_std_error(sb, err);
764ac27a0ecSDave Kleikamp out:
765ac27a0ecSDave Kleikamp 	iput(inode);
766ac27a0ecSDave Kleikamp 	ret = ERR_PTR(err);
767ac27a0ecSDave Kleikamp really_out:
768ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
769ac27a0ecSDave Kleikamp 	return ret;
770ac27a0ecSDave Kleikamp 
771ac27a0ecSDave Kleikamp fail_free_drop:
772ac27a0ecSDave Kleikamp 	DQUOT_FREE_INODE(inode);
773ac27a0ecSDave Kleikamp 
774ac27a0ecSDave Kleikamp fail_drop:
775ac27a0ecSDave Kleikamp 	DQUOT_DROP(inode);
776ac27a0ecSDave Kleikamp 	inode->i_flags |= S_NOQUOTA;
777ac27a0ecSDave Kleikamp 	inode->i_nlink = 0;
778ac27a0ecSDave Kleikamp 	iput(inode);
779ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
780ac27a0ecSDave Kleikamp 	return ERR_PTR(err);
781ac27a0ecSDave Kleikamp }
782ac27a0ecSDave Kleikamp 
783ac27a0ecSDave Kleikamp /* Verify that we are loading a valid orphan from disk */
784617ba13bSMingming Cao struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
785ac27a0ecSDave Kleikamp {
786617ba13bSMingming Cao 	unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
787fd2d4291SAvantika Mathur 	ext4_group_t block_group;
788ac27a0ecSDave Kleikamp 	int bit;
7891d1fe1eeSDavid Howells 	struct buffer_head *bitmap_bh;
790ac27a0ecSDave Kleikamp 	struct inode *inode = NULL;
7911d1fe1eeSDavid Howells 	long err = -EIO;
792ac27a0ecSDave Kleikamp 
793ac27a0ecSDave Kleikamp 	/* Error cases - e2fsck has already cleaned up for us */
794ac27a0ecSDave Kleikamp 	if (ino > max_ino) {
79546e665e9SHarvey Harrison 		ext4_warning(sb, __func__,
796ac27a0ecSDave Kleikamp 			     "bad orphan ino %lu!  e2fsck was run?", ino);
7971d1fe1eeSDavid Howells 		goto error;
798ac27a0ecSDave Kleikamp 	}
799ac27a0ecSDave Kleikamp 
800617ba13bSMingming Cao 	block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
801617ba13bSMingming Cao 	bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
802ac27a0ecSDave Kleikamp 	bitmap_bh = read_inode_bitmap(sb, block_group);
803ac27a0ecSDave Kleikamp 	if (!bitmap_bh) {
80446e665e9SHarvey Harrison 		ext4_warning(sb, __func__,
805ac27a0ecSDave Kleikamp 			     "inode bitmap error for orphan %lu", ino);
8061d1fe1eeSDavid Howells 		goto error;
807ac27a0ecSDave Kleikamp 	}
808ac27a0ecSDave Kleikamp 
809ac27a0ecSDave Kleikamp 	/* Having the inode bit set should be a 100% indicator that this
810ac27a0ecSDave Kleikamp 	 * is a valid orphan (no e2fsck run on fs).  Orphans also include
811ac27a0ecSDave Kleikamp 	 * inodes that were being truncated, so we can't check i_nlink==0.
812ac27a0ecSDave Kleikamp 	 */
8131d1fe1eeSDavid Howells 	if (!ext4_test_bit(bit, bitmap_bh->b_data))
8141d1fe1eeSDavid Howells 		goto bad_orphan;
8151d1fe1eeSDavid Howells 
8161d1fe1eeSDavid Howells 	inode = ext4_iget(sb, ino);
8171d1fe1eeSDavid Howells 	if (IS_ERR(inode))
8181d1fe1eeSDavid Howells 		goto iget_failed;
8191d1fe1eeSDavid Howells 
8201d1fe1eeSDavid Howells 	if (NEXT_ORPHAN(inode) > max_ino)
8211d1fe1eeSDavid Howells 		goto bad_orphan;
8221d1fe1eeSDavid Howells 	brelse(bitmap_bh);
8231d1fe1eeSDavid Howells 	return inode;
8241d1fe1eeSDavid Howells 
8251d1fe1eeSDavid Howells iget_failed:
8261d1fe1eeSDavid Howells 	err = PTR_ERR(inode);
8271d1fe1eeSDavid Howells 	inode = NULL;
8281d1fe1eeSDavid Howells bad_orphan:
82946e665e9SHarvey Harrison 	ext4_warning(sb, __func__,
830ac27a0ecSDave Kleikamp 		     "bad orphan inode %lu!  e2fsck was run?", ino);
831617ba13bSMingming Cao 	printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n",
832ac27a0ecSDave Kleikamp 	       bit, (unsigned long long)bitmap_bh->b_blocknr,
833617ba13bSMingming Cao 	       ext4_test_bit(bit, bitmap_bh->b_data));
834ac27a0ecSDave Kleikamp 	printk(KERN_NOTICE "inode=%p\n", inode);
835ac27a0ecSDave Kleikamp 	if (inode) {
836ac27a0ecSDave Kleikamp 		printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
837ac27a0ecSDave Kleikamp 		       is_bad_inode(inode));
838ac27a0ecSDave Kleikamp 		printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
839ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
840ac27a0ecSDave Kleikamp 		printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
841ac27a0ecSDave Kleikamp 		/* Avoid freeing blocks if we got a bad deleted inode */
8421d1fe1eeSDavid Howells 		if (inode->i_nlink == 0)
843ac27a0ecSDave Kleikamp 			inode->i_blocks = 0;
844ac27a0ecSDave Kleikamp 		iput(inode);
845ac27a0ecSDave Kleikamp 	}
846ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
8471d1fe1eeSDavid Howells error:
8481d1fe1eeSDavid Howells 	return ERR_PTR(err);
849ac27a0ecSDave Kleikamp }
850ac27a0ecSDave Kleikamp 
851617ba13bSMingming Cao unsigned long ext4_count_free_inodes (struct super_block * sb)
852ac27a0ecSDave Kleikamp {
853ac27a0ecSDave Kleikamp 	unsigned long desc_count;
854617ba13bSMingming Cao 	struct ext4_group_desc *gdp;
855fd2d4291SAvantika Mathur 	ext4_group_t i;
856617ba13bSMingming Cao #ifdef EXT4FS_DEBUG
857617ba13bSMingming Cao 	struct ext4_super_block *es;
858ac27a0ecSDave Kleikamp 	unsigned long bitmap_count, x;
859ac27a0ecSDave Kleikamp 	struct buffer_head *bitmap_bh = NULL;
860ac27a0ecSDave Kleikamp 
861617ba13bSMingming Cao 	es = EXT4_SB(sb)->s_es;
862ac27a0ecSDave Kleikamp 	desc_count = 0;
863ac27a0ecSDave Kleikamp 	bitmap_count = 0;
864ac27a0ecSDave Kleikamp 	gdp = NULL;
865617ba13bSMingming Cao 	for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
866617ba13bSMingming Cao 		gdp = ext4_get_group_desc (sb, i, NULL);
867ac27a0ecSDave Kleikamp 		if (!gdp)
868ac27a0ecSDave Kleikamp 			continue;
869ac27a0ecSDave Kleikamp 		desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
870ac27a0ecSDave Kleikamp 		brelse(bitmap_bh);
871ac27a0ecSDave Kleikamp 		bitmap_bh = read_inode_bitmap(sb, i);
872ac27a0ecSDave Kleikamp 		if (!bitmap_bh)
873ac27a0ecSDave Kleikamp 			continue;
874ac27a0ecSDave Kleikamp 
875617ba13bSMingming Cao 		x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8);
876c549a95dSEric Sandeen 		printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
877ac27a0ecSDave Kleikamp 			i, le16_to_cpu(gdp->bg_free_inodes_count), x);
878ac27a0ecSDave Kleikamp 		bitmap_count += x;
879ac27a0ecSDave Kleikamp 	}
880ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
881617ba13bSMingming Cao 	printk("ext4_count_free_inodes: stored = %u, computed = %lu, %lu\n",
882ac27a0ecSDave Kleikamp 		le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
883ac27a0ecSDave Kleikamp 	return desc_count;
884ac27a0ecSDave Kleikamp #else
885ac27a0ecSDave Kleikamp 	desc_count = 0;
886617ba13bSMingming Cao 	for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
887617ba13bSMingming Cao 		gdp = ext4_get_group_desc (sb, i, NULL);
888ac27a0ecSDave Kleikamp 		if (!gdp)
889ac27a0ecSDave Kleikamp 			continue;
890ac27a0ecSDave Kleikamp 		desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
891ac27a0ecSDave Kleikamp 		cond_resched();
892ac27a0ecSDave Kleikamp 	}
893ac27a0ecSDave Kleikamp 	return desc_count;
894ac27a0ecSDave Kleikamp #endif
895ac27a0ecSDave Kleikamp }
896ac27a0ecSDave Kleikamp 
897ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
898617ba13bSMingming Cao unsigned long ext4_count_dirs (struct super_block * sb)
899ac27a0ecSDave Kleikamp {
900ac27a0ecSDave Kleikamp 	unsigned long count = 0;
901fd2d4291SAvantika Mathur 	ext4_group_t i;
902ac27a0ecSDave Kleikamp 
903617ba13bSMingming Cao 	for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
904617ba13bSMingming Cao 		struct ext4_group_desc *gdp = ext4_get_group_desc (sb, i, NULL);
905ac27a0ecSDave Kleikamp 		if (!gdp)
906ac27a0ecSDave Kleikamp 			continue;
907ac27a0ecSDave Kleikamp 		count += le16_to_cpu(gdp->bg_used_dirs_count);
908ac27a0ecSDave Kleikamp 	}
909ac27a0ecSDave Kleikamp 	return count;
910ac27a0ecSDave Kleikamp }
911ac27a0ecSDave Kleikamp 
912