xref: /linux/fs/ext4/ialloc.c (revision 2dc6b0d48ca0599837df21b14bb8393d0804af57)
1ac27a0ecSDave Kleikamp /*
2617ba13bSMingming Cao  *  linux/fs/ext4/ialloc.c
3ac27a0ecSDave Kleikamp  *
4ac27a0ecSDave Kleikamp  * Copyright (C) 1992, 1993, 1994, 1995
5ac27a0ecSDave Kleikamp  * Remy Card (card@masi.ibp.fr)
6ac27a0ecSDave Kleikamp  * Laboratoire MASI - Institut Blaise Pascal
7ac27a0ecSDave Kleikamp  * Universite Pierre et Marie Curie (Paris VI)
8ac27a0ecSDave Kleikamp  *
9ac27a0ecSDave Kleikamp  *  BSD ufs-inspired inode and directory allocation by
10ac27a0ecSDave Kleikamp  *  Stephen Tweedie (sct@redhat.com), 1993
11ac27a0ecSDave Kleikamp  *  Big-endian to little-endian byte-swapping/bitmaps by
12ac27a0ecSDave Kleikamp  *        David S. Miller (davem@caip.rutgers.edu), 1995
13ac27a0ecSDave Kleikamp  */
14ac27a0ecSDave Kleikamp 
15ac27a0ecSDave Kleikamp #include <linux/time.h>
16ac27a0ecSDave Kleikamp #include <linux/fs.h>
17dab291afSMingming Cao #include <linux/jbd2.h>
18ac27a0ecSDave Kleikamp #include <linux/stat.h>
19ac27a0ecSDave Kleikamp #include <linux/string.h>
20ac27a0ecSDave Kleikamp #include <linux/quotaops.h>
21ac27a0ecSDave Kleikamp #include <linux/buffer_head.h>
22ac27a0ecSDave Kleikamp #include <linux/random.h>
23ac27a0ecSDave Kleikamp #include <linux/bitops.h>
243a5b2ecdSMingming Cao #include <linux/blkdev.h>
25ac27a0ecSDave Kleikamp #include <asm/byteorder.h>
263dcf5451SChristoph Hellwig #include "ext4.h"
273dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
28ac27a0ecSDave Kleikamp #include "xattr.h"
29ac27a0ecSDave Kleikamp #include "acl.h"
30717d50e4SAndreas Dilger #include "group.h"
31ac27a0ecSDave Kleikamp 
32ac27a0ecSDave Kleikamp /*
33ac27a0ecSDave Kleikamp  * ialloc.c contains the inodes allocation and deallocation routines
34ac27a0ecSDave Kleikamp  */
35ac27a0ecSDave Kleikamp 
36ac27a0ecSDave Kleikamp /*
37ac27a0ecSDave Kleikamp  * The free inodes are managed by bitmaps.  A file system contains several
38ac27a0ecSDave Kleikamp  * blocks groups.  Each group contains 1 bitmap block for blocks, 1 bitmap
39ac27a0ecSDave Kleikamp  * block for inodes, N blocks for the inode table and data blocks.
40ac27a0ecSDave Kleikamp  *
41ac27a0ecSDave Kleikamp  * The file system contains group descriptors which are located after the
42ac27a0ecSDave Kleikamp  * super block.  Each descriptor contains the number of the bitmap block and
43ac27a0ecSDave Kleikamp  * the free blocks count in the block.
44ac27a0ecSDave Kleikamp  */
45ac27a0ecSDave Kleikamp 
46717d50e4SAndreas Dilger /*
47717d50e4SAndreas Dilger  * To avoid calling the atomic setbit hundreds or thousands of times, we only
48717d50e4SAndreas Dilger  * need to use it within a single byte (to ensure we get endianness right).
49717d50e4SAndreas Dilger  * We can use memset for the rest of the bitmap as there are no other users.
50717d50e4SAndreas Dilger  */
51717d50e4SAndreas Dilger void mark_bitmap_end(int start_bit, int end_bit, char *bitmap)
52717d50e4SAndreas Dilger {
53717d50e4SAndreas Dilger 	int i;
54717d50e4SAndreas Dilger 
55717d50e4SAndreas Dilger 	if (start_bit >= end_bit)
56717d50e4SAndreas Dilger 		return;
57717d50e4SAndreas Dilger 
58717d50e4SAndreas Dilger 	ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit);
59717d50e4SAndreas Dilger 	for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
60717d50e4SAndreas Dilger 		ext4_set_bit(i, bitmap);
61717d50e4SAndreas Dilger 	if (i < end_bit)
62717d50e4SAndreas Dilger 		memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
63717d50e4SAndreas Dilger }
64717d50e4SAndreas Dilger 
65717d50e4SAndreas Dilger /* Initializes an uninitialized inode bitmap */
66fd2d4291SAvantika Mathur unsigned ext4_init_inode_bitmap(struct super_block *sb, struct buffer_head *bh,
67fd2d4291SAvantika Mathur 				ext4_group_t block_group,
68717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
69717d50e4SAndreas Dilger {
70717d50e4SAndreas Dilger 	struct ext4_sb_info *sbi = EXT4_SB(sb);
71717d50e4SAndreas Dilger 
72717d50e4SAndreas Dilger 	J_ASSERT_BH(bh, buffer_locked(bh));
73717d50e4SAndreas Dilger 
74717d50e4SAndreas Dilger 	/* If checksum is bad mark all blocks and inodes use to prevent
75717d50e4SAndreas Dilger 	 * allocation, essentially implementing a per-group read-only flag. */
76717d50e4SAndreas Dilger 	if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) {
77a9df9a49STheodore Ts'o 		ext4_error(sb, __func__, "Checksum bad for group %u",
78717d50e4SAndreas Dilger 			   block_group);
79560671a0SAneesh Kumar K.V 		ext4_free_blks_set(sb, gdp, 0);
80560671a0SAneesh Kumar K.V 		ext4_free_inodes_set(sb, gdp, 0);
81560671a0SAneesh Kumar K.V 		ext4_itable_unused_set(sb, gdp, 0);
82717d50e4SAndreas Dilger 		memset(bh->b_data, 0xff, sb->s_blocksize);
83717d50e4SAndreas Dilger 		return 0;
84717d50e4SAndreas Dilger 	}
85717d50e4SAndreas Dilger 
86717d50e4SAndreas Dilger 	memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8);
87648f5879SAneesh Kumar K.V 	mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), sb->s_blocksize * 8,
88717d50e4SAndreas Dilger 			bh->b_data);
89717d50e4SAndreas Dilger 
90717d50e4SAndreas Dilger 	return EXT4_INODES_PER_GROUP(sb);
91717d50e4SAndreas Dilger }
92ac27a0ecSDave Kleikamp 
93ac27a0ecSDave Kleikamp /*
94ac27a0ecSDave Kleikamp  * Read the inode allocation bitmap for a given block_group, reading
95ac27a0ecSDave Kleikamp  * into the specified slot in the superblock's bitmap cache.
96ac27a0ecSDave Kleikamp  *
97ac27a0ecSDave Kleikamp  * Return buffer_head of bitmap on success or NULL.
98ac27a0ecSDave Kleikamp  */
99ac27a0ecSDave Kleikamp static struct buffer_head *
100e29d1cdeSEric Sandeen ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group)
101ac27a0ecSDave Kleikamp {
102617ba13bSMingming Cao 	struct ext4_group_desc *desc;
103ac27a0ecSDave Kleikamp 	struct buffer_head *bh = NULL;
104e29d1cdeSEric Sandeen 	ext4_fsblk_t bitmap_blk;
105ac27a0ecSDave Kleikamp 
106617ba13bSMingming Cao 	desc = ext4_get_group_desc(sb, block_group, NULL);
107ac27a0ecSDave Kleikamp 	if (!desc)
108e29d1cdeSEric Sandeen 		return NULL;
109e29d1cdeSEric Sandeen 	bitmap_blk = ext4_inode_bitmap(sb, desc);
110e29d1cdeSEric Sandeen 	bh = sb_getblk(sb, bitmap_blk);
111e29d1cdeSEric Sandeen 	if (unlikely(!bh)) {
112e29d1cdeSEric Sandeen 		ext4_error(sb, __func__,
113ac27a0ecSDave Kleikamp 			    "Cannot read inode bitmap - "
114a9df9a49STheodore Ts'o 			    "block_group = %u, inode_bitmap = %llu",
115e29d1cdeSEric Sandeen 			    block_group, bitmap_blk);
116e29d1cdeSEric Sandeen 		return NULL;
117e29d1cdeSEric Sandeen 	}
1182ccb5fb9SAneesh Kumar K.V 	if (bitmap_uptodate(bh))
119e29d1cdeSEric Sandeen 		return bh;
120e29d1cdeSEric Sandeen 
121c806e68fSFrederic Bohe 	lock_buffer(bh);
1222ccb5fb9SAneesh Kumar K.V 	if (bitmap_uptodate(bh)) {
1232ccb5fb9SAneesh Kumar K.V 		unlock_buffer(bh);
1242ccb5fb9SAneesh Kumar K.V 		return bh;
1252ccb5fb9SAneesh Kumar K.V 	}
126b5f10eedSEric Sandeen 	spin_lock(sb_bgl_lock(EXT4_SB(sb), block_group));
127e29d1cdeSEric Sandeen 	if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
128e29d1cdeSEric Sandeen 		ext4_init_inode_bitmap(sb, bh, block_group, desc);
1292ccb5fb9SAneesh Kumar K.V 		set_bitmap_uptodate(bh);
130e29d1cdeSEric Sandeen 		set_buffer_uptodate(bh);
131b5f10eedSEric Sandeen 		spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group));
1323300bedaSAneesh Kumar K.V 		unlock_buffer(bh);
133e29d1cdeSEric Sandeen 		return bh;
134e29d1cdeSEric Sandeen 	}
135b5f10eedSEric Sandeen 	spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group));
1362ccb5fb9SAneesh Kumar K.V 	if (buffer_uptodate(bh)) {
1372ccb5fb9SAneesh Kumar K.V 		/*
1382ccb5fb9SAneesh Kumar K.V 		 * if not uninit if bh is uptodate,
1392ccb5fb9SAneesh Kumar K.V 		 * bitmap is also uptodate
1402ccb5fb9SAneesh Kumar K.V 		 */
1412ccb5fb9SAneesh Kumar K.V 		set_bitmap_uptodate(bh);
1422ccb5fb9SAneesh Kumar K.V 		unlock_buffer(bh);
1432ccb5fb9SAneesh Kumar K.V 		return bh;
1442ccb5fb9SAneesh Kumar K.V 	}
1452ccb5fb9SAneesh Kumar K.V 	/*
1462ccb5fb9SAneesh Kumar K.V 	 * submit the buffer_head for read. We can
1472ccb5fb9SAneesh Kumar K.V 	 * safely mark the bitmap as uptodate now.
1482ccb5fb9SAneesh Kumar K.V 	 * We do it here so the bitmap uptodate bit
1492ccb5fb9SAneesh Kumar K.V 	 * get set with buffer lock held.
1502ccb5fb9SAneesh Kumar K.V 	 */
1512ccb5fb9SAneesh Kumar K.V 	set_bitmap_uptodate(bh);
152e29d1cdeSEric Sandeen 	if (bh_submit_read(bh) < 0) {
153e29d1cdeSEric Sandeen 		put_bh(bh);
154e29d1cdeSEric Sandeen 		ext4_error(sb, __func__,
155e29d1cdeSEric Sandeen 			    "Cannot read inode bitmap - "
156a9df9a49STheodore Ts'o 			    "block_group = %u, inode_bitmap = %llu",
157e29d1cdeSEric Sandeen 			    block_group, bitmap_blk);
158e29d1cdeSEric Sandeen 		return NULL;
159e29d1cdeSEric Sandeen 	}
160ac27a0ecSDave Kleikamp 	return bh;
161ac27a0ecSDave Kleikamp }
162ac27a0ecSDave Kleikamp 
163ac27a0ecSDave Kleikamp /*
164ac27a0ecSDave Kleikamp  * NOTE! When we get the inode, we're the only people
165ac27a0ecSDave Kleikamp  * that have access to it, and as such there are no
166ac27a0ecSDave Kleikamp  * race conditions we have to worry about. The inode
167ac27a0ecSDave Kleikamp  * is not on the hash-lists, and it cannot be reached
168ac27a0ecSDave Kleikamp  * through the filesystem because the directory entry
169ac27a0ecSDave Kleikamp  * has been deleted earlier.
170ac27a0ecSDave Kleikamp  *
171ac27a0ecSDave Kleikamp  * HOWEVER: we must make sure that we get no aliases,
172ac27a0ecSDave Kleikamp  * which means that we have to call "clear_inode()"
173ac27a0ecSDave Kleikamp  * _before_ we mark the inode not in use in the inode
174ac27a0ecSDave Kleikamp  * bitmaps. Otherwise a newly created file might use
175ac27a0ecSDave Kleikamp  * the same inode number (not actually the same pointer
176ac27a0ecSDave Kleikamp  * though), and then we'd have two inodes sharing the
177ac27a0ecSDave Kleikamp  * same inode number and space on the harddisk.
178ac27a0ecSDave Kleikamp  */
179617ba13bSMingming Cao void ext4_free_inode(handle_t *handle, struct inode *inode)
180ac27a0ecSDave Kleikamp {
181ac27a0ecSDave Kleikamp 	struct super_block *sb = inode->i_sb;
182ac27a0ecSDave Kleikamp 	int is_directory;
183ac27a0ecSDave Kleikamp 	unsigned long ino;
184ac27a0ecSDave Kleikamp 	struct buffer_head *bitmap_bh = NULL;
185ac27a0ecSDave Kleikamp 	struct buffer_head *bh2;
186fd2d4291SAvantika Mathur 	ext4_group_t block_group;
187ac27a0ecSDave Kleikamp 	unsigned long bit;
188617ba13bSMingming Cao 	struct ext4_group_desc *gdp;
189617ba13bSMingming Cao 	struct ext4_super_block *es;
190617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
1917ce9d5d1SEric Sandeen 	int fatal = 0, err, count, cleared;
192772cb7c8SJose R. Santos 	ext4_group_t flex_group;
193ac27a0ecSDave Kleikamp 
194ac27a0ecSDave Kleikamp 	if (atomic_read(&inode->i_count) > 1) {
1954776004fSTheodore Ts'o 		printk(KERN_ERR "ext4_free_inode: inode has count=%d\n",
196ac27a0ecSDave Kleikamp 		       atomic_read(&inode->i_count));
197ac27a0ecSDave Kleikamp 		return;
198ac27a0ecSDave Kleikamp 	}
199ac27a0ecSDave Kleikamp 	if (inode->i_nlink) {
2004776004fSTheodore Ts'o 		printk(KERN_ERR "ext4_free_inode: inode has nlink=%d\n",
201ac27a0ecSDave Kleikamp 		       inode->i_nlink);
202ac27a0ecSDave Kleikamp 		return;
203ac27a0ecSDave Kleikamp 	}
204ac27a0ecSDave Kleikamp 	if (!sb) {
2054776004fSTheodore Ts'o 		printk(KERN_ERR "ext4_free_inode: inode on "
2064776004fSTheodore Ts'o 		       "nonexistent device\n");
207ac27a0ecSDave Kleikamp 		return;
208ac27a0ecSDave Kleikamp 	}
209617ba13bSMingming Cao 	sbi = EXT4_SB(sb);
210ac27a0ecSDave Kleikamp 
211ac27a0ecSDave Kleikamp 	ino = inode->i_ino;
212617ba13bSMingming Cao 	ext4_debug("freeing inode %lu\n", ino);
213ba80b101STheodore Ts'o 	trace_mark(ext4_free_inode,
214ba80b101STheodore Ts'o 		   "dev %s ino %lu mode %d uid %lu gid %lu bocks %llu",
215ba80b101STheodore Ts'o 		   sb->s_id, inode->i_ino, inode->i_mode,
216ba80b101STheodore Ts'o 		   (unsigned long) inode->i_uid, (unsigned long) inode->i_gid,
217ba80b101STheodore Ts'o 		   (unsigned long long) inode->i_blocks);
218ac27a0ecSDave Kleikamp 
219ac27a0ecSDave Kleikamp 	/*
220ac27a0ecSDave Kleikamp 	 * Note: we must free any quota before locking the superblock,
221ac27a0ecSDave Kleikamp 	 * as writing the quota to disk may need the lock as well.
222ac27a0ecSDave Kleikamp 	 */
223*a269eb18SJan Kara 	vfs_dq_init(inode);
224617ba13bSMingming Cao 	ext4_xattr_delete_inode(handle, inode);
225*a269eb18SJan Kara 	vfs_dq_free_inode(inode);
226*a269eb18SJan Kara 	vfs_dq_drop(inode);
227ac27a0ecSDave Kleikamp 
228ac27a0ecSDave Kleikamp 	is_directory = S_ISDIR(inode->i_mode);
229ac27a0ecSDave Kleikamp 
230ac27a0ecSDave Kleikamp 	/* Do this BEFORE marking the inode not in use or returning an error */
231ac27a0ecSDave Kleikamp 	clear_inode(inode);
232ac27a0ecSDave Kleikamp 
233617ba13bSMingming Cao 	es = EXT4_SB(sb)->s_es;
234617ba13bSMingming Cao 	if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
235617ba13bSMingming Cao 		ext4_error(sb, "ext4_free_inode",
236ac27a0ecSDave Kleikamp 			   "reserved or nonexistent inode %lu", ino);
237ac27a0ecSDave Kleikamp 		goto error_return;
238ac27a0ecSDave Kleikamp 	}
239617ba13bSMingming Cao 	block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
240617ba13bSMingming Cao 	bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
241e29d1cdeSEric Sandeen 	bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
242ac27a0ecSDave Kleikamp 	if (!bitmap_bh)
243ac27a0ecSDave Kleikamp 		goto error_return;
244ac27a0ecSDave Kleikamp 
245ac27a0ecSDave Kleikamp 	BUFFER_TRACE(bitmap_bh, "get_write_access");
246617ba13bSMingming Cao 	fatal = ext4_journal_get_write_access(handle, bitmap_bh);
247ac27a0ecSDave Kleikamp 	if (fatal)
248ac27a0ecSDave Kleikamp 		goto error_return;
249ac27a0ecSDave Kleikamp 
250ac27a0ecSDave Kleikamp 	/* Ok, now we can actually update the inode bitmaps.. */
2517ce9d5d1SEric Sandeen 	spin_lock(sb_bgl_lock(sbi, block_group));
2527ce9d5d1SEric Sandeen 	cleared = ext4_clear_bit(bit, bitmap_bh->b_data);
2537ce9d5d1SEric Sandeen 	spin_unlock(sb_bgl_lock(sbi, block_group));
2547ce9d5d1SEric Sandeen 	if (!cleared)
255617ba13bSMingming Cao 		ext4_error(sb, "ext4_free_inode",
256ac27a0ecSDave Kleikamp 			   "bit already cleared for inode %lu", ino);
257ac27a0ecSDave Kleikamp 	else {
258617ba13bSMingming Cao 		gdp = ext4_get_group_desc(sb, block_group, &bh2);
259ac27a0ecSDave Kleikamp 
260ac27a0ecSDave Kleikamp 		BUFFER_TRACE(bh2, "get_write_access");
261617ba13bSMingming Cao 		fatal = ext4_journal_get_write_access(handle, bh2);
262ac27a0ecSDave Kleikamp 		if (fatal) goto error_return;
263ac27a0ecSDave Kleikamp 
264ac27a0ecSDave Kleikamp 		if (gdp) {
265ac27a0ecSDave Kleikamp 			spin_lock(sb_bgl_lock(sbi, block_group));
266560671a0SAneesh Kumar K.V 			count = ext4_free_inodes_count(sb, gdp) + 1;
267560671a0SAneesh Kumar K.V 			ext4_free_inodes_set(sb, gdp, count);
268560671a0SAneesh Kumar K.V 			if (is_directory) {
269560671a0SAneesh Kumar K.V 				count = ext4_used_dirs_count(sb, gdp) - 1;
270560671a0SAneesh Kumar K.V 				ext4_used_dirs_set(sb, gdp, count);
271560671a0SAneesh Kumar K.V 			}
272717d50e4SAndreas Dilger 			gdp->bg_checksum = ext4_group_desc_csum(sbi,
273717d50e4SAndreas Dilger 							block_group, gdp);
274ac27a0ecSDave Kleikamp 			spin_unlock(sb_bgl_lock(sbi, block_group));
275ac27a0ecSDave Kleikamp 			percpu_counter_inc(&sbi->s_freeinodes_counter);
276ac27a0ecSDave Kleikamp 			if (is_directory)
277ac27a0ecSDave Kleikamp 				percpu_counter_dec(&sbi->s_dirs_counter);
278ac27a0ecSDave Kleikamp 
279772cb7c8SJose R. Santos 			if (sbi->s_log_groups_per_flex) {
280772cb7c8SJose R. Santos 				flex_group = ext4_flex_group(sbi, block_group);
281772cb7c8SJose R. Santos 				spin_lock(sb_bgl_lock(sbi, flex_group));
282772cb7c8SJose R. Santos 				sbi->s_flex_groups[flex_group].free_inodes++;
283772cb7c8SJose R. Santos 				spin_unlock(sb_bgl_lock(sbi, flex_group));
284772cb7c8SJose R. Santos 			}
285ac27a0ecSDave Kleikamp 		}
2860390131bSFrank Mayhar 		BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata");
2870390131bSFrank Mayhar 		err = ext4_handle_dirty_metadata(handle, NULL, bh2);
288ac27a0ecSDave Kleikamp 		if (!fatal) fatal = err;
289ac27a0ecSDave Kleikamp 	}
2900390131bSFrank Mayhar 	BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata");
2910390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
292ac27a0ecSDave Kleikamp 	if (!fatal)
293ac27a0ecSDave Kleikamp 		fatal = err;
294ac27a0ecSDave Kleikamp 	sb->s_dirt = 1;
295ac27a0ecSDave Kleikamp error_return:
296ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
297617ba13bSMingming Cao 	ext4_std_error(sb, fatal);
298ac27a0ecSDave Kleikamp }
299ac27a0ecSDave Kleikamp 
300ac27a0ecSDave Kleikamp /*
301ac27a0ecSDave Kleikamp  * There are two policies for allocating an inode.  If the new inode is
302ac27a0ecSDave Kleikamp  * a directory, then a forward search is made for a block group with both
303ac27a0ecSDave Kleikamp  * free space and a low directory-to-inode ratio; if that fails, then of
304ac27a0ecSDave Kleikamp  * the groups with above-average free space, that group with the fewest
305ac27a0ecSDave Kleikamp  * directories already is chosen.
306ac27a0ecSDave Kleikamp  *
307ac27a0ecSDave Kleikamp  * For other inodes, search forward from the parent directory\'s block
308ac27a0ecSDave Kleikamp  * group to find a free inode.
309ac27a0ecSDave Kleikamp  */
3102aa9fc4cSAvantika Mathur static int find_group_dir(struct super_block *sb, struct inode *parent,
3112aa9fc4cSAvantika Mathur 				ext4_group_t *best_group)
312ac27a0ecSDave Kleikamp {
313fd2d4291SAvantika Mathur 	ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
314ac27a0ecSDave Kleikamp 	unsigned int freei, avefreei;
315617ba13bSMingming Cao 	struct ext4_group_desc *desc, *best_desc = NULL;
3162aa9fc4cSAvantika Mathur 	ext4_group_t group;
3172aa9fc4cSAvantika Mathur 	int ret = -1;
318ac27a0ecSDave Kleikamp 
319617ba13bSMingming Cao 	freei = percpu_counter_read_positive(&EXT4_SB(sb)->s_freeinodes_counter);
320ac27a0ecSDave Kleikamp 	avefreei = freei / ngroups;
321ac27a0ecSDave Kleikamp 
322ac27a0ecSDave Kleikamp 	for (group = 0; group < ngroups; group++) {
323ef2fb679SEric Sandeen 		desc = ext4_get_group_desc(sb, group, NULL);
324560671a0SAneesh Kumar K.V 		if (!desc || !ext4_free_inodes_count(sb, desc))
325ac27a0ecSDave Kleikamp 			continue;
326560671a0SAneesh Kumar K.V 		if (ext4_free_inodes_count(sb, desc) < avefreei)
327ac27a0ecSDave Kleikamp 			continue;
328ac27a0ecSDave Kleikamp 		if (!best_desc ||
329560671a0SAneesh Kumar K.V 		    (ext4_free_blks_count(sb, desc) >
330560671a0SAneesh Kumar K.V 		     ext4_free_blks_count(sb, best_desc))) {
3312aa9fc4cSAvantika Mathur 			*best_group = group;
332ac27a0ecSDave Kleikamp 			best_desc = desc;
3332aa9fc4cSAvantika Mathur 			ret = 0;
334ac27a0ecSDave Kleikamp 		}
335ac27a0ecSDave Kleikamp 	}
3362aa9fc4cSAvantika Mathur 	return ret;
337ac27a0ecSDave Kleikamp }
338ac27a0ecSDave Kleikamp 
339772cb7c8SJose R. Santos #define free_block_ratio 10
340772cb7c8SJose R. Santos 
341772cb7c8SJose R. Santos static int find_group_flex(struct super_block *sb, struct inode *parent,
342772cb7c8SJose R. Santos 			   ext4_group_t *best_group)
343772cb7c8SJose R. Santos {
344772cb7c8SJose R. Santos 	struct ext4_sb_info *sbi = EXT4_SB(sb);
345772cb7c8SJose R. Santos 	struct ext4_group_desc *desc;
346772cb7c8SJose R. Santos 	struct buffer_head *bh;
347772cb7c8SJose R. Santos 	struct flex_groups *flex_group = sbi->s_flex_groups;
348772cb7c8SJose R. Santos 	ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
349772cb7c8SJose R. Santos 	ext4_group_t parent_fbg_group = ext4_flex_group(sbi, parent_group);
350772cb7c8SJose R. Santos 	ext4_group_t ngroups = sbi->s_groups_count;
351772cb7c8SJose R. Santos 	int flex_size = ext4_flex_bg_size(sbi);
352772cb7c8SJose R. Santos 	ext4_group_t best_flex = parent_fbg_group;
353772cb7c8SJose R. Santos 	int blocks_per_flex = sbi->s_blocks_per_group * flex_size;
354772cb7c8SJose R. Santos 	int flexbg_free_blocks;
355772cb7c8SJose R. Santos 	int flex_freeb_ratio;
356772cb7c8SJose R. Santos 	ext4_group_t n_fbg_groups;
357772cb7c8SJose R. Santos 	ext4_group_t i;
358772cb7c8SJose R. Santos 
359772cb7c8SJose R. Santos 	n_fbg_groups = (sbi->s_groups_count + flex_size - 1) >>
360772cb7c8SJose R. Santos 		sbi->s_log_groups_per_flex;
361772cb7c8SJose R. Santos 
362772cb7c8SJose R. Santos find_close_to_parent:
363772cb7c8SJose R. Santos 	flexbg_free_blocks = flex_group[best_flex].free_blocks;
364772cb7c8SJose R. Santos 	flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
365772cb7c8SJose R. Santos 	if (flex_group[best_flex].free_inodes &&
366772cb7c8SJose R. Santos 	    flex_freeb_ratio > free_block_ratio)
367772cb7c8SJose R. Santos 		goto found_flexbg;
368772cb7c8SJose R. Santos 
369772cb7c8SJose R. Santos 	if (best_flex && best_flex == parent_fbg_group) {
370772cb7c8SJose R. Santos 		best_flex--;
371772cb7c8SJose R. Santos 		goto find_close_to_parent;
372772cb7c8SJose R. Santos 	}
373772cb7c8SJose R. Santos 
374772cb7c8SJose R. Santos 	for (i = 0; i < n_fbg_groups; i++) {
375772cb7c8SJose R. Santos 		if (i == parent_fbg_group || i == parent_fbg_group - 1)
376772cb7c8SJose R. Santos 			continue;
377772cb7c8SJose R. Santos 
378772cb7c8SJose R. Santos 		flexbg_free_blocks = flex_group[i].free_blocks;
379772cb7c8SJose R. Santos 		flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
380772cb7c8SJose R. Santos 
381772cb7c8SJose R. Santos 		if (flex_freeb_ratio > free_block_ratio &&
382772cb7c8SJose R. Santos 		    flex_group[i].free_inodes) {
383772cb7c8SJose R. Santos 			best_flex = i;
384772cb7c8SJose R. Santos 			goto found_flexbg;
385772cb7c8SJose R. Santos 		}
386772cb7c8SJose R. Santos 
387c001077fSEric Sandeen 		if (flex_group[best_flex].free_inodes == 0 ||
388772cb7c8SJose R. Santos 		    (flex_group[i].free_blocks >
389772cb7c8SJose R. Santos 		     flex_group[best_flex].free_blocks &&
390772cb7c8SJose R. Santos 		     flex_group[i].free_inodes))
391772cb7c8SJose R. Santos 			best_flex = i;
392772cb7c8SJose R. Santos 	}
393772cb7c8SJose R. Santos 
394772cb7c8SJose R. Santos 	if (!flex_group[best_flex].free_inodes ||
395772cb7c8SJose R. Santos 	    !flex_group[best_flex].free_blocks)
396772cb7c8SJose R. Santos 		return -1;
397772cb7c8SJose R. Santos 
398772cb7c8SJose R. Santos found_flexbg:
399772cb7c8SJose R. Santos 	for (i = best_flex * flex_size; i < ngroups &&
400772cb7c8SJose R. Santos 		     i < (best_flex + 1) * flex_size; i++) {
401772cb7c8SJose R. Santos 		desc = ext4_get_group_desc(sb, i, &bh);
402560671a0SAneesh Kumar K.V 		if (ext4_free_inodes_count(sb, desc)) {
403772cb7c8SJose R. Santos 			*best_group = i;
404772cb7c8SJose R. Santos 			goto out;
405772cb7c8SJose R. Santos 		}
406772cb7c8SJose R. Santos 	}
407772cb7c8SJose R. Santos 
408772cb7c8SJose R. Santos 	return -1;
409772cb7c8SJose R. Santos out:
410772cb7c8SJose R. Santos 	return 0;
411772cb7c8SJose R. Santos }
412772cb7c8SJose R. Santos 
413ac27a0ecSDave Kleikamp /*
414ac27a0ecSDave Kleikamp  * Orlov's allocator for directories.
415ac27a0ecSDave Kleikamp  *
416ac27a0ecSDave Kleikamp  * We always try to spread first-level directories.
417ac27a0ecSDave Kleikamp  *
418ac27a0ecSDave Kleikamp  * If there are blockgroups with both free inodes and free blocks counts
419ac27a0ecSDave Kleikamp  * not worse than average we return one with smallest directory count.
420ac27a0ecSDave Kleikamp  * Otherwise we simply return a random group.
421ac27a0ecSDave Kleikamp  *
422ac27a0ecSDave Kleikamp  * For the rest rules look so:
423ac27a0ecSDave Kleikamp  *
424ac27a0ecSDave Kleikamp  * It's OK to put directory into a group unless
425ac27a0ecSDave Kleikamp  * it has too many directories already (max_dirs) or
426ac27a0ecSDave Kleikamp  * it has too few free inodes left (min_inodes) or
427ac27a0ecSDave Kleikamp  * it has too few free blocks left (min_blocks) or
428ac27a0ecSDave Kleikamp  * it's already running too large debt (max_debt).
4291cc8dcf5SBenoit Boissinot  * Parent's group is preferred, if it doesn't satisfy these
430ac27a0ecSDave Kleikamp  * conditions we search cyclically through the rest. If none
431ac27a0ecSDave Kleikamp  * of the groups look good we just look for a group with more
432ac27a0ecSDave Kleikamp  * free inodes than average (starting at parent's group).
433ac27a0ecSDave Kleikamp  *
434ac27a0ecSDave Kleikamp  * Debt is incremented each time we allocate a directory and decremented
435ac27a0ecSDave Kleikamp  * when we allocate an inode, within 0--255.
436ac27a0ecSDave Kleikamp  */
437ac27a0ecSDave Kleikamp 
438ac27a0ecSDave Kleikamp #define INODE_COST 64
439ac27a0ecSDave Kleikamp #define BLOCK_COST 256
440ac27a0ecSDave Kleikamp 
4412aa9fc4cSAvantika Mathur static int find_group_orlov(struct super_block *sb, struct inode *parent,
4422aa9fc4cSAvantika Mathur 				ext4_group_t *group)
443ac27a0ecSDave Kleikamp {
444fd2d4291SAvantika Mathur 	ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
445617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
446617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
447fd2d4291SAvantika Mathur 	ext4_group_t ngroups = sbi->s_groups_count;
448617ba13bSMingming Cao 	int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
449ac27a0ecSDave Kleikamp 	unsigned int freei, avefreei;
450617ba13bSMingming Cao 	ext4_fsblk_t freeb, avefreeb;
451617ba13bSMingming Cao 	ext4_fsblk_t blocks_per_dir;
452ac27a0ecSDave Kleikamp 	unsigned int ndirs;
453ac27a0ecSDave Kleikamp 	int max_debt, max_dirs, min_inodes;
454617ba13bSMingming Cao 	ext4_grpblk_t min_blocks;
4552aa9fc4cSAvantika Mathur 	ext4_group_t i;
456617ba13bSMingming Cao 	struct ext4_group_desc *desc;
457ac27a0ecSDave Kleikamp 
458ac27a0ecSDave Kleikamp 	freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
459ac27a0ecSDave Kleikamp 	avefreei = freei / ngroups;
460ac27a0ecSDave Kleikamp 	freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
4613a5b2ecdSMingming Cao 	avefreeb = freeb;
462f4e5bc24SAndrew Morton 	do_div(avefreeb, ngroups);
463ac27a0ecSDave Kleikamp 	ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
464ac27a0ecSDave Kleikamp 
465ac27a0ecSDave Kleikamp 	if ((parent == sb->s_root->d_inode) ||
466617ba13bSMingming Cao 	    (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL)) {
467ac27a0ecSDave Kleikamp 		int best_ndir = inodes_per_group;
4682aa9fc4cSAvantika Mathur 		ext4_group_t grp;
4692aa9fc4cSAvantika Mathur 		int ret = -1;
470ac27a0ecSDave Kleikamp 
4712aa9fc4cSAvantika Mathur 		get_random_bytes(&grp, sizeof(grp));
4722aa9fc4cSAvantika Mathur 		parent_group = (unsigned)grp % ngroups;
473ac27a0ecSDave Kleikamp 		for (i = 0; i < ngroups; i++) {
4742aa9fc4cSAvantika Mathur 			grp = (parent_group + i) % ngroups;
4752aa9fc4cSAvantika Mathur 			desc = ext4_get_group_desc(sb, grp, NULL);
476560671a0SAneesh Kumar K.V 			if (!desc || !ext4_free_inodes_count(sb, desc))
477ac27a0ecSDave Kleikamp 				continue;
478560671a0SAneesh Kumar K.V 			if (ext4_used_dirs_count(sb, desc) >= best_ndir)
479ac27a0ecSDave Kleikamp 				continue;
480560671a0SAneesh Kumar K.V 			if (ext4_free_inodes_count(sb, desc) < avefreei)
481ac27a0ecSDave Kleikamp 				continue;
482560671a0SAneesh Kumar K.V 			if (ext4_free_blks_count(sb, desc) < avefreeb)
483ac27a0ecSDave Kleikamp 				continue;
4842aa9fc4cSAvantika Mathur 			*group = grp;
4852aa9fc4cSAvantika Mathur 			ret = 0;
486560671a0SAneesh Kumar K.V 			best_ndir = ext4_used_dirs_count(sb, desc);
487ac27a0ecSDave Kleikamp 		}
4882aa9fc4cSAvantika Mathur 		if (ret == 0)
4892aa9fc4cSAvantika Mathur 			return ret;
490ac27a0ecSDave Kleikamp 		goto fallback;
491ac27a0ecSDave Kleikamp 	}
492ac27a0ecSDave Kleikamp 
493bd81d8eeSLaurent Vivier 	blocks_per_dir = ext4_blocks_count(es) - freeb;
494f4e5bc24SAndrew Morton 	do_div(blocks_per_dir, ndirs);
495ac27a0ecSDave Kleikamp 
496ac27a0ecSDave Kleikamp 	max_dirs = ndirs / ngroups + inodes_per_group / 16;
497ac27a0ecSDave Kleikamp 	min_inodes = avefreei - inodes_per_group / 4;
498617ba13bSMingming Cao 	min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb) / 4;
499ac27a0ecSDave Kleikamp 
5003a5b2ecdSMingming Cao 	max_debt = EXT4_BLOCKS_PER_GROUP(sb);
501f4e5bc24SAndrew Morton 	max_debt /= max_t(int, blocks_per_dir, BLOCK_COST);
502ac27a0ecSDave Kleikamp 	if (max_debt * INODE_COST > inodes_per_group)
503ac27a0ecSDave Kleikamp 		max_debt = inodes_per_group / INODE_COST;
504ac27a0ecSDave Kleikamp 	if (max_debt > 255)
505ac27a0ecSDave Kleikamp 		max_debt = 255;
506ac27a0ecSDave Kleikamp 	if (max_debt == 0)
507ac27a0ecSDave Kleikamp 		max_debt = 1;
508ac27a0ecSDave Kleikamp 
509ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
5102aa9fc4cSAvantika Mathur 		*group = (parent_group + i) % ngroups;
5112aa9fc4cSAvantika Mathur 		desc = ext4_get_group_desc(sb, *group, NULL);
512560671a0SAneesh Kumar K.V 		if (!desc || !ext4_free_inodes_count(sb, desc))
513ac27a0ecSDave Kleikamp 			continue;
514560671a0SAneesh Kumar K.V 		if (ext4_used_dirs_count(sb, desc) >= max_dirs)
515ac27a0ecSDave Kleikamp 			continue;
516560671a0SAneesh Kumar K.V 		if (ext4_free_inodes_count(sb, desc) < min_inodes)
517ac27a0ecSDave Kleikamp 			continue;
518560671a0SAneesh Kumar K.V 		if (ext4_free_blks_count(sb, desc) < min_blocks)
519ac27a0ecSDave Kleikamp 			continue;
5202aa9fc4cSAvantika Mathur 		return 0;
521ac27a0ecSDave Kleikamp 	}
522ac27a0ecSDave Kleikamp 
523ac27a0ecSDave Kleikamp fallback:
524ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
5252aa9fc4cSAvantika Mathur 		*group = (parent_group + i) % ngroups;
5262aa9fc4cSAvantika Mathur 		desc = ext4_get_group_desc(sb, *group, NULL);
527560671a0SAneesh Kumar K.V 		if (desc && ext4_free_inodes_count(sb, desc) &&
528560671a0SAneesh Kumar K.V 			ext4_free_inodes_count(sb, desc) >= avefreei)
5292aa9fc4cSAvantika Mathur 			return 0;
530ac27a0ecSDave Kleikamp 	}
531ac27a0ecSDave Kleikamp 
532ac27a0ecSDave Kleikamp 	if (avefreei) {
533ac27a0ecSDave Kleikamp 		/*
534ac27a0ecSDave Kleikamp 		 * The free-inodes counter is approximate, and for really small
535ac27a0ecSDave Kleikamp 		 * filesystems the above test can fail to find any blockgroups
536ac27a0ecSDave Kleikamp 		 */
537ac27a0ecSDave Kleikamp 		avefreei = 0;
538ac27a0ecSDave Kleikamp 		goto fallback;
539ac27a0ecSDave Kleikamp 	}
540ac27a0ecSDave Kleikamp 
541ac27a0ecSDave Kleikamp 	return -1;
542ac27a0ecSDave Kleikamp }
543ac27a0ecSDave Kleikamp 
5442aa9fc4cSAvantika Mathur static int find_group_other(struct super_block *sb, struct inode *parent,
5452aa9fc4cSAvantika Mathur 				ext4_group_t *group)
546ac27a0ecSDave Kleikamp {
547fd2d4291SAvantika Mathur 	ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
548fd2d4291SAvantika Mathur 	ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
549617ba13bSMingming Cao 	struct ext4_group_desc *desc;
5502aa9fc4cSAvantika Mathur 	ext4_group_t i;
551ac27a0ecSDave Kleikamp 
552ac27a0ecSDave Kleikamp 	/*
553ac27a0ecSDave Kleikamp 	 * Try to place the inode in its parent directory
554ac27a0ecSDave Kleikamp 	 */
5552aa9fc4cSAvantika Mathur 	*group = parent_group;
5562aa9fc4cSAvantika Mathur 	desc = ext4_get_group_desc(sb, *group, NULL);
557560671a0SAneesh Kumar K.V 	if (desc && ext4_free_inodes_count(sb, desc) &&
558560671a0SAneesh Kumar K.V 			ext4_free_blks_count(sb, desc))
5592aa9fc4cSAvantika Mathur 		return 0;
560ac27a0ecSDave Kleikamp 
561ac27a0ecSDave Kleikamp 	/*
562ac27a0ecSDave Kleikamp 	 * We're going to place this inode in a different blockgroup from its
563ac27a0ecSDave Kleikamp 	 * parent.  We want to cause files in a common directory to all land in
564ac27a0ecSDave Kleikamp 	 * the same blockgroup.  But we want files which are in a different
565ac27a0ecSDave Kleikamp 	 * directory which shares a blockgroup with our parent to land in a
566ac27a0ecSDave Kleikamp 	 * different blockgroup.
567ac27a0ecSDave Kleikamp 	 *
568ac27a0ecSDave Kleikamp 	 * So add our directory's i_ino into the starting point for the hash.
569ac27a0ecSDave Kleikamp 	 */
5702aa9fc4cSAvantika Mathur 	*group = (*group + parent->i_ino) % ngroups;
571ac27a0ecSDave Kleikamp 
572ac27a0ecSDave Kleikamp 	/*
573ac27a0ecSDave Kleikamp 	 * Use a quadratic hash to find a group with a free inode and some free
574ac27a0ecSDave Kleikamp 	 * blocks.
575ac27a0ecSDave Kleikamp 	 */
576ac27a0ecSDave Kleikamp 	for (i = 1; i < ngroups; i <<= 1) {
5772aa9fc4cSAvantika Mathur 		*group += i;
5782aa9fc4cSAvantika Mathur 		if (*group >= ngroups)
5792aa9fc4cSAvantika Mathur 			*group -= ngroups;
5802aa9fc4cSAvantika Mathur 		desc = ext4_get_group_desc(sb, *group, NULL);
581560671a0SAneesh Kumar K.V 		if (desc && ext4_free_inodes_count(sb, desc) &&
582560671a0SAneesh Kumar K.V 				ext4_free_blks_count(sb, desc))
5832aa9fc4cSAvantika Mathur 			return 0;
584ac27a0ecSDave Kleikamp 	}
585ac27a0ecSDave Kleikamp 
586ac27a0ecSDave Kleikamp 	/*
587ac27a0ecSDave Kleikamp 	 * That failed: try linear search for a free inode, even if that group
588ac27a0ecSDave Kleikamp 	 * has no free blocks.
589ac27a0ecSDave Kleikamp 	 */
5902aa9fc4cSAvantika Mathur 	*group = parent_group;
591ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
5922aa9fc4cSAvantika Mathur 		if (++*group >= ngroups)
5932aa9fc4cSAvantika Mathur 			*group = 0;
5942aa9fc4cSAvantika Mathur 		desc = ext4_get_group_desc(sb, *group, NULL);
595560671a0SAneesh Kumar K.V 		if (desc && ext4_free_inodes_count(sb, desc))
5962aa9fc4cSAvantika Mathur 			return 0;
597ac27a0ecSDave Kleikamp 	}
598ac27a0ecSDave Kleikamp 
599ac27a0ecSDave Kleikamp 	return -1;
600ac27a0ecSDave Kleikamp }
601ac27a0ecSDave Kleikamp 
602ac27a0ecSDave Kleikamp /*
60339341867SAneesh Kumar K.V  * claim the inode from the inode bitmap. If the group
60439341867SAneesh Kumar K.V  * is uninit we need to take the groups's sb_bgl_lock
60539341867SAneesh Kumar K.V  * and clear the uninit flag. The inode bitmap update
60639341867SAneesh Kumar K.V  * and group desc uninit flag clear should be done
60739341867SAneesh Kumar K.V  * after holding sb_bgl_lock so that ext4_read_inode_bitmap
60839341867SAneesh Kumar K.V  * doesn't race with the ext4_claim_inode
60939341867SAneesh Kumar K.V  */
61039341867SAneesh Kumar K.V static int ext4_claim_inode(struct super_block *sb,
61139341867SAneesh Kumar K.V 			struct buffer_head *inode_bitmap_bh,
61239341867SAneesh Kumar K.V 			unsigned long ino, ext4_group_t group, int mode)
61339341867SAneesh Kumar K.V {
61439341867SAneesh Kumar K.V 	int free = 0, retval = 0, count;
61539341867SAneesh Kumar K.V 	struct ext4_sb_info *sbi = EXT4_SB(sb);
61639341867SAneesh Kumar K.V 	struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL);
61739341867SAneesh Kumar K.V 
61839341867SAneesh Kumar K.V 	spin_lock(sb_bgl_lock(sbi, group));
61939341867SAneesh Kumar K.V 	if (ext4_set_bit(ino, inode_bitmap_bh->b_data)) {
62039341867SAneesh Kumar K.V 		/* not a free inode */
62139341867SAneesh Kumar K.V 		retval = 1;
62239341867SAneesh Kumar K.V 		goto err_ret;
62339341867SAneesh Kumar K.V 	}
62439341867SAneesh Kumar K.V 	ino++;
62539341867SAneesh Kumar K.V 	if ((group == 0 && ino < EXT4_FIRST_INO(sb)) ||
62639341867SAneesh Kumar K.V 			ino > EXT4_INODES_PER_GROUP(sb)) {
62739341867SAneesh Kumar K.V 		spin_unlock(sb_bgl_lock(sbi, group));
62839341867SAneesh Kumar K.V 		ext4_error(sb, __func__,
62939341867SAneesh Kumar K.V 			   "reserved inode or inode > inodes count - "
63039341867SAneesh Kumar K.V 			   "block_group = %u, inode=%lu", group,
63139341867SAneesh Kumar K.V 			   ino + group * EXT4_INODES_PER_GROUP(sb));
63239341867SAneesh Kumar K.V 		return 1;
63339341867SAneesh Kumar K.V 	}
63439341867SAneesh Kumar K.V 	/* If we didn't allocate from within the initialized part of the inode
63539341867SAneesh Kumar K.V 	 * table then we need to initialize up to this inode. */
63639341867SAneesh Kumar K.V 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
63739341867SAneesh Kumar K.V 
63839341867SAneesh Kumar K.V 		if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
63939341867SAneesh Kumar K.V 			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
64039341867SAneesh Kumar K.V 			/* When marking the block group with
64139341867SAneesh Kumar K.V 			 * ~EXT4_BG_INODE_UNINIT we don't want to depend
64239341867SAneesh Kumar K.V 			 * on the value of bg_itable_unused even though
64339341867SAneesh Kumar K.V 			 * mke2fs could have initialized the same for us.
64439341867SAneesh Kumar K.V 			 * Instead we calculated the value below
64539341867SAneesh Kumar K.V 			 */
64639341867SAneesh Kumar K.V 
64739341867SAneesh Kumar K.V 			free = 0;
64839341867SAneesh Kumar K.V 		} else {
64939341867SAneesh Kumar K.V 			free = EXT4_INODES_PER_GROUP(sb) -
65039341867SAneesh Kumar K.V 				ext4_itable_unused_count(sb, gdp);
65139341867SAneesh Kumar K.V 		}
65239341867SAneesh Kumar K.V 
65339341867SAneesh Kumar K.V 		/*
65439341867SAneesh Kumar K.V 		 * Check the relative inode number against the last used
65539341867SAneesh Kumar K.V 		 * relative inode number in this group. if it is greater
65639341867SAneesh Kumar K.V 		 * we need to  update the bg_itable_unused count
65739341867SAneesh Kumar K.V 		 *
65839341867SAneesh Kumar K.V 		 */
65939341867SAneesh Kumar K.V 		if (ino > free)
66039341867SAneesh Kumar K.V 			ext4_itable_unused_set(sb, gdp,
66139341867SAneesh Kumar K.V 					(EXT4_INODES_PER_GROUP(sb) - ino));
66239341867SAneesh Kumar K.V 	}
66339341867SAneesh Kumar K.V 	count = ext4_free_inodes_count(sb, gdp) - 1;
66439341867SAneesh Kumar K.V 	ext4_free_inodes_set(sb, gdp, count);
66539341867SAneesh Kumar K.V 	if (S_ISDIR(mode)) {
66639341867SAneesh Kumar K.V 		count = ext4_used_dirs_count(sb, gdp) + 1;
66739341867SAneesh Kumar K.V 		ext4_used_dirs_set(sb, gdp, count);
66839341867SAneesh Kumar K.V 	}
66939341867SAneesh Kumar K.V 	gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp);
67039341867SAneesh Kumar K.V err_ret:
67139341867SAneesh Kumar K.V 	spin_unlock(sb_bgl_lock(sbi, group));
67239341867SAneesh Kumar K.V 	return retval;
67339341867SAneesh Kumar K.V }
67439341867SAneesh Kumar K.V 
67539341867SAneesh Kumar K.V /*
676ac27a0ecSDave Kleikamp  * There are two policies for allocating an inode.  If the new inode is
677ac27a0ecSDave Kleikamp  * a directory, then a forward search is made for a block group with both
678ac27a0ecSDave Kleikamp  * free space and a low directory-to-inode ratio; if that fails, then of
679ac27a0ecSDave Kleikamp  * the groups with above-average free space, that group with the fewest
680ac27a0ecSDave Kleikamp  * directories already is chosen.
681ac27a0ecSDave Kleikamp  *
682ac27a0ecSDave Kleikamp  * For other inodes, search forward from the parent directory's block
683ac27a0ecSDave Kleikamp  * group to find a free inode.
684ac27a0ecSDave Kleikamp  */
685617ba13bSMingming Cao struct inode *ext4_new_inode(handle_t *handle, struct inode *dir, int mode)
686ac27a0ecSDave Kleikamp {
687ac27a0ecSDave Kleikamp 	struct super_block *sb;
6883300bedaSAneesh Kumar K.V 	struct buffer_head *inode_bitmap_bh = NULL;
6893300bedaSAneesh Kumar K.V 	struct buffer_head *group_desc_bh;
6902aa9fc4cSAvantika Mathur 	ext4_group_t group = 0;
691ac27a0ecSDave Kleikamp 	unsigned long ino = 0;
692ac27a0ecSDave Kleikamp 	struct inode *inode;
693617ba13bSMingming Cao 	struct ext4_group_desc *gdp = NULL;
694617ba13bSMingming Cao 	struct ext4_super_block *es;
695617ba13bSMingming Cao 	struct ext4_inode_info *ei;
696617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
69739341867SAneesh Kumar K.V 	int ret2, err = 0;
698ac27a0ecSDave Kleikamp 	struct inode *ret;
6992aa9fc4cSAvantika Mathur 	ext4_group_t i;
7002aa9fc4cSAvantika Mathur 	int free = 0;
7012842c3b5STheodore Ts'o 	static int once = 1;
702772cb7c8SJose R. Santos 	ext4_group_t flex_group;
703ac27a0ecSDave Kleikamp 
704ac27a0ecSDave Kleikamp 	/* Cannot create files in a deleted directory */
705ac27a0ecSDave Kleikamp 	if (!dir || !dir->i_nlink)
706ac27a0ecSDave Kleikamp 		return ERR_PTR(-EPERM);
707ac27a0ecSDave Kleikamp 
708ac27a0ecSDave Kleikamp 	sb = dir->i_sb;
709ba80b101STheodore Ts'o 	trace_mark(ext4_request_inode, "dev %s dir %lu mode %d", sb->s_id,
710ba80b101STheodore Ts'o 		   dir->i_ino, mode);
711ac27a0ecSDave Kleikamp 	inode = new_inode(sb);
712ac27a0ecSDave Kleikamp 	if (!inode)
713ac27a0ecSDave Kleikamp 		return ERR_PTR(-ENOMEM);
714617ba13bSMingming Cao 	ei = EXT4_I(inode);
715ac27a0ecSDave Kleikamp 
716617ba13bSMingming Cao 	sbi = EXT4_SB(sb);
717ac27a0ecSDave Kleikamp 	es = sbi->s_es;
718772cb7c8SJose R. Santos 
719772cb7c8SJose R. Santos 	if (sbi->s_log_groups_per_flex) {
720772cb7c8SJose R. Santos 		ret2 = find_group_flex(sb, dir, &group);
72105bf9e83STheodore Ts'o 		if (ret2 == -1) {
72205bf9e83STheodore Ts'o 			ret2 = find_group_other(sb, dir, &group);
7232842c3b5STheodore Ts'o 			if (ret2 == 0 && once)
7242842c3b5STheodore Ts'o 				once = 0;
72505bf9e83STheodore Ts'o 				printk(KERN_NOTICE "ext4: find_group_flex "
72605bf9e83STheodore Ts'o 				       "failed, fallback succeeded dir %lu\n",
72705bf9e83STheodore Ts'o 				       dir->i_ino);
72805bf9e83STheodore Ts'o 		}
729772cb7c8SJose R. Santos 		goto got_group;
730772cb7c8SJose R. Santos 	}
731772cb7c8SJose R. Santos 
732ac27a0ecSDave Kleikamp 	if (S_ISDIR(mode)) {
733ac27a0ecSDave Kleikamp 		if (test_opt(sb, OLDALLOC))
7342aa9fc4cSAvantika Mathur 			ret2 = find_group_dir(sb, dir, &group);
735ac27a0ecSDave Kleikamp 		else
7362aa9fc4cSAvantika Mathur 			ret2 = find_group_orlov(sb, dir, &group);
737ac27a0ecSDave Kleikamp 	} else
7382aa9fc4cSAvantika Mathur 		ret2 = find_group_other(sb, dir, &group);
739ac27a0ecSDave Kleikamp 
740772cb7c8SJose R. Santos got_group:
741ac27a0ecSDave Kleikamp 	err = -ENOSPC;
7422aa9fc4cSAvantika Mathur 	if (ret2 == -1)
743ac27a0ecSDave Kleikamp 		goto out;
744ac27a0ecSDave Kleikamp 
745ac27a0ecSDave Kleikamp 	for (i = 0; i < sbi->s_groups_count; i++) {
746ac27a0ecSDave Kleikamp 		err = -EIO;
747ac27a0ecSDave Kleikamp 
7483300bedaSAneesh Kumar K.V 		gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
749ac27a0ecSDave Kleikamp 		if (!gdp)
750ac27a0ecSDave Kleikamp 			goto fail;
751ac27a0ecSDave Kleikamp 
7523300bedaSAneesh Kumar K.V 		brelse(inode_bitmap_bh);
7533300bedaSAneesh Kumar K.V 		inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
7543300bedaSAneesh Kumar K.V 		if (!inode_bitmap_bh)
755ac27a0ecSDave Kleikamp 			goto fail;
756ac27a0ecSDave Kleikamp 
757ac27a0ecSDave Kleikamp 		ino = 0;
758ac27a0ecSDave Kleikamp 
759ac27a0ecSDave Kleikamp repeat_in_this_group:
760617ba13bSMingming Cao 		ino = ext4_find_next_zero_bit((unsigned long *)
7613300bedaSAneesh Kumar K.V 					      inode_bitmap_bh->b_data,
7623300bedaSAneesh Kumar K.V 					      EXT4_INODES_PER_GROUP(sb), ino);
7633300bedaSAneesh Kumar K.V 
764617ba13bSMingming Cao 		if (ino < EXT4_INODES_PER_GROUP(sb)) {
765ac27a0ecSDave Kleikamp 
7663300bedaSAneesh Kumar K.V 			BUFFER_TRACE(inode_bitmap_bh, "get_write_access");
7673300bedaSAneesh Kumar K.V 			err = ext4_journal_get_write_access(handle,
7683300bedaSAneesh Kumar K.V 							    inode_bitmap_bh);
769ac27a0ecSDave Kleikamp 			if (err)
770ac27a0ecSDave Kleikamp 				goto fail;
771ac27a0ecSDave Kleikamp 
77239341867SAneesh Kumar K.V 			BUFFER_TRACE(group_desc_bh, "get_write_access");
77339341867SAneesh Kumar K.V 			err = ext4_journal_get_write_access(handle,
77439341867SAneesh Kumar K.V 								group_desc_bh);
77539341867SAneesh Kumar K.V 			if (err)
77639341867SAneesh Kumar K.V 				goto fail;
77739341867SAneesh Kumar K.V 			if (!ext4_claim_inode(sb, inode_bitmap_bh,
77839341867SAneesh Kumar K.V 						ino, group, mode)) {
779ac27a0ecSDave Kleikamp 				/* we won it */
7803300bedaSAneesh Kumar K.V 				BUFFER_TRACE(inode_bitmap_bh,
7810390131bSFrank Mayhar 					"call ext4_handle_dirty_metadata");
7820390131bSFrank Mayhar 				err = ext4_handle_dirty_metadata(handle,
7830390131bSFrank Mayhar 								 inode,
7843300bedaSAneesh Kumar K.V 							inode_bitmap_bh);
785ac27a0ecSDave Kleikamp 				if (err)
786ac27a0ecSDave Kleikamp 					goto fail;
78739341867SAneesh Kumar K.V 				/* zero bit is inode number 1*/
78839341867SAneesh Kumar K.V 				ino++;
789ac27a0ecSDave Kleikamp 				goto got;
790ac27a0ecSDave Kleikamp 			}
791ac27a0ecSDave Kleikamp 			/* we lost it */
7923300bedaSAneesh Kumar K.V 			ext4_handle_release_buffer(handle, inode_bitmap_bh);
79339341867SAneesh Kumar K.V 			ext4_handle_release_buffer(handle, group_desc_bh);
794ac27a0ecSDave Kleikamp 
795617ba13bSMingming Cao 			if (++ino < EXT4_INODES_PER_GROUP(sb))
796ac27a0ecSDave Kleikamp 				goto repeat_in_this_group;
797ac27a0ecSDave Kleikamp 		}
798ac27a0ecSDave Kleikamp 
799ac27a0ecSDave Kleikamp 		/*
800ac27a0ecSDave Kleikamp 		 * This case is possible in concurrent environment.  It is very
801ac27a0ecSDave Kleikamp 		 * rare.  We cannot repeat the find_group_xxx() call because
802ac27a0ecSDave Kleikamp 		 * that will simply return the same blockgroup, because the
803ac27a0ecSDave Kleikamp 		 * group descriptor metadata has not yet been updated.
804ac27a0ecSDave Kleikamp 		 * So we just go onto the next blockgroup.
805ac27a0ecSDave Kleikamp 		 */
806ac27a0ecSDave Kleikamp 		if (++group == sbi->s_groups_count)
807ac27a0ecSDave Kleikamp 			group = 0;
808ac27a0ecSDave Kleikamp 	}
809ac27a0ecSDave Kleikamp 	err = -ENOSPC;
810ac27a0ecSDave Kleikamp 	goto out;
811ac27a0ecSDave Kleikamp 
812ac27a0ecSDave Kleikamp got:
813717d50e4SAndreas Dilger 	/* We may have to initialize the block bitmap if it isn't already */
814717d50e4SAndreas Dilger 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
815717d50e4SAndreas Dilger 	    gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
8163300bedaSAneesh Kumar K.V 		struct buffer_head *block_bitmap_bh;
817717d50e4SAndreas Dilger 
8183300bedaSAneesh Kumar K.V 		block_bitmap_bh = ext4_read_block_bitmap(sb, group);
8193300bedaSAneesh Kumar K.V 		BUFFER_TRACE(block_bitmap_bh, "get block bitmap access");
8203300bedaSAneesh Kumar K.V 		err = ext4_journal_get_write_access(handle, block_bitmap_bh);
821717d50e4SAndreas Dilger 		if (err) {
8223300bedaSAneesh Kumar K.V 			brelse(block_bitmap_bh);
823717d50e4SAndreas Dilger 			goto fail;
824717d50e4SAndreas Dilger 		}
825717d50e4SAndreas Dilger 
826717d50e4SAndreas Dilger 		free = 0;
827ac27a0ecSDave Kleikamp 		spin_lock(sb_bgl_lock(sbi, group));
828717d50e4SAndreas Dilger 		/* recheck and clear flag under lock if we still need to */
829717d50e4SAndreas Dilger 		if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
830717d50e4SAndreas Dilger 			free = ext4_free_blocks_after_init(sb, group, gdp);
8313300bedaSAneesh Kumar K.V 			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
832560671a0SAneesh Kumar K.V 			ext4_free_blks_set(sb, gdp, free);
83323712a9cSFrederic Bohe 			gdp->bg_checksum = ext4_group_desc_csum(sbi, group,
83423712a9cSFrederic Bohe 								gdp);
835717d50e4SAndreas Dilger 		}
836717d50e4SAndreas Dilger 		spin_unlock(sb_bgl_lock(sbi, group));
837717d50e4SAndreas Dilger 
838717d50e4SAndreas Dilger 		/* Don't need to dirty bitmap block if we didn't change it */
839717d50e4SAndreas Dilger 		if (free) {
8403300bedaSAneesh Kumar K.V 			BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
8410390131bSFrank Mayhar 			err = ext4_handle_dirty_metadata(handle,
8423300bedaSAneesh Kumar K.V 							NULL, block_bitmap_bh);
843717d50e4SAndreas Dilger 		}
844717d50e4SAndreas Dilger 
8453300bedaSAneesh Kumar K.V 		brelse(block_bitmap_bh);
846717d50e4SAndreas Dilger 		if (err)
847717d50e4SAndreas Dilger 			goto fail;
848717d50e4SAndreas Dilger 	}
8493300bedaSAneesh Kumar K.V 	BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata");
8503300bedaSAneesh Kumar K.V 	err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh);
85139341867SAneesh Kumar K.V 	if (err)
85239341867SAneesh Kumar K.V 		goto fail;
853ac27a0ecSDave Kleikamp 
854ac27a0ecSDave Kleikamp 	percpu_counter_dec(&sbi->s_freeinodes_counter);
855ac27a0ecSDave Kleikamp 	if (S_ISDIR(mode))
856ac27a0ecSDave Kleikamp 		percpu_counter_inc(&sbi->s_dirs_counter);
857ac27a0ecSDave Kleikamp 	sb->s_dirt = 1;
858ac27a0ecSDave Kleikamp 
859772cb7c8SJose R. Santos 	if (sbi->s_log_groups_per_flex) {
860772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, group);
861772cb7c8SJose R. Santos 		spin_lock(sb_bgl_lock(sbi, flex_group));
862772cb7c8SJose R. Santos 		sbi->s_flex_groups[flex_group].free_inodes--;
863772cb7c8SJose R. Santos 		spin_unlock(sb_bgl_lock(sbi, flex_group));
864772cb7c8SJose R. Santos 	}
865772cb7c8SJose R. Santos 
8664c9c544eSDavid Howells 	inode->i_uid = current_fsuid();
867ac27a0ecSDave Kleikamp 	if (test_opt(sb, GRPID))
868ac27a0ecSDave Kleikamp 		inode->i_gid = dir->i_gid;
869ac27a0ecSDave Kleikamp 	else if (dir->i_mode & S_ISGID) {
870ac27a0ecSDave Kleikamp 		inode->i_gid = dir->i_gid;
871ac27a0ecSDave Kleikamp 		if (S_ISDIR(mode))
872ac27a0ecSDave Kleikamp 			mode |= S_ISGID;
873ac27a0ecSDave Kleikamp 	} else
8744c9c544eSDavid Howells 		inode->i_gid = current_fsgid();
875ac27a0ecSDave Kleikamp 	inode->i_mode = mode;
876ac27a0ecSDave Kleikamp 
877717d50e4SAndreas Dilger 	inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
878ac27a0ecSDave Kleikamp 	/* This is the optimal IO size (for stat), not the fs block size */
879ac27a0ecSDave Kleikamp 	inode->i_blocks = 0;
880ef7f3835SKalpak Shah 	inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
881ef7f3835SKalpak Shah 						       ext4_current_time(inode);
882ac27a0ecSDave Kleikamp 
883ac27a0ecSDave Kleikamp 	memset(ei->i_data, 0, sizeof(ei->i_data));
884ac27a0ecSDave Kleikamp 	ei->i_dir_start_lookup = 0;
885ac27a0ecSDave Kleikamp 	ei->i_disksize = 0;
886ac27a0ecSDave Kleikamp 
88742bf0383SAneesh Kumar K.V 	/*
8882dc6b0d4SDuane Griffin 	 * Don't inherit extent flag from directory, amongst others. We set
8892dc6b0d4SDuane Griffin 	 * extent flag on newly created directory and file only if -o extent
8902dc6b0d4SDuane Griffin 	 * mount option is specified
89142bf0383SAneesh Kumar K.V 	 */
8922dc6b0d4SDuane Griffin 	ei->i_flags =
8932dc6b0d4SDuane Griffin 		ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED);
894ac27a0ecSDave Kleikamp 	ei->i_file_acl = 0;
895ac27a0ecSDave Kleikamp 	ei->i_dtime = 0;
896ac27a0ecSDave Kleikamp 	ei->i_block_group = group;
897ac27a0ecSDave Kleikamp 
898617ba13bSMingming Cao 	ext4_set_inode_flags(inode);
899ac27a0ecSDave Kleikamp 	if (IS_DIRSYNC(inode))
9000390131bSFrank Mayhar 		ext4_handle_sync(handle);
9016b38e842SAl Viro 	if (insert_inode_locked(inode) < 0) {
9026b38e842SAl Viro 		err = -EINVAL;
9036b38e842SAl Viro 		goto fail_drop;
9046b38e842SAl Viro 	}
905ac27a0ecSDave Kleikamp 	spin_lock(&sbi->s_next_gen_lock);
906ac27a0ecSDave Kleikamp 	inode->i_generation = sbi->s_next_generation++;
907ac27a0ecSDave Kleikamp 	spin_unlock(&sbi->s_next_gen_lock);
908ac27a0ecSDave Kleikamp 
909617ba13bSMingming Cao 	ei->i_state = EXT4_STATE_NEW;
910ef7f3835SKalpak Shah 
911ef7f3835SKalpak Shah 	ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize;
912ac27a0ecSDave Kleikamp 
913ac27a0ecSDave Kleikamp 	ret = inode;
914*a269eb18SJan Kara 	if (vfs_dq_alloc_inode(inode)) {
915ac27a0ecSDave Kleikamp 		err = -EDQUOT;
916ac27a0ecSDave Kleikamp 		goto fail_drop;
917ac27a0ecSDave Kleikamp 	}
918ac27a0ecSDave Kleikamp 
919617ba13bSMingming Cao 	err = ext4_init_acl(handle, inode, dir);
920ac27a0ecSDave Kleikamp 	if (err)
921ac27a0ecSDave Kleikamp 		goto fail_free_drop;
922ac27a0ecSDave Kleikamp 
923617ba13bSMingming Cao 	err = ext4_init_security(handle, inode, dir);
924ac27a0ecSDave Kleikamp 	if (err)
925ac27a0ecSDave Kleikamp 		goto fail_free_drop;
926ac27a0ecSDave Kleikamp 
92783982b6fSTheodore Ts'o 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
928e4079a11SEric Sandeen 		/* set extent flag only for directory, file and normal symlink*/
929e65187e6SAneesh Kumar K.V 		if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
930a86c6181SAlex Tomas 			EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL;
931a86c6181SAlex Tomas 			ext4_ext_tree_init(handle, inode);
932a86c6181SAlex Tomas 		}
93342bf0383SAneesh Kumar K.V 	}
934ac27a0ecSDave Kleikamp 
9358753e88fSAneesh Kumar K.V 	err = ext4_mark_inode_dirty(handle, inode);
9368753e88fSAneesh Kumar K.V 	if (err) {
9378753e88fSAneesh Kumar K.V 		ext4_std_error(sb, err);
9388753e88fSAneesh Kumar K.V 		goto fail_free_drop;
9398753e88fSAneesh Kumar K.V 	}
9408753e88fSAneesh Kumar K.V 
941617ba13bSMingming Cao 	ext4_debug("allocating inode %lu\n", inode->i_ino);
942ba80b101STheodore Ts'o 	trace_mark(ext4_allocate_inode, "dev %s ino %lu dir %lu mode %d",
943ba80b101STheodore Ts'o 		   sb->s_id, inode->i_ino, dir->i_ino, mode);
944ac27a0ecSDave Kleikamp 	goto really_out;
945ac27a0ecSDave Kleikamp fail:
946617ba13bSMingming Cao 	ext4_std_error(sb, err);
947ac27a0ecSDave Kleikamp out:
948ac27a0ecSDave Kleikamp 	iput(inode);
949ac27a0ecSDave Kleikamp 	ret = ERR_PTR(err);
950ac27a0ecSDave Kleikamp really_out:
9513300bedaSAneesh Kumar K.V 	brelse(inode_bitmap_bh);
952ac27a0ecSDave Kleikamp 	return ret;
953ac27a0ecSDave Kleikamp 
954ac27a0ecSDave Kleikamp fail_free_drop:
955*a269eb18SJan Kara 	vfs_dq_free_inode(inode);
956ac27a0ecSDave Kleikamp 
957ac27a0ecSDave Kleikamp fail_drop:
958*a269eb18SJan Kara 	vfs_dq_drop(inode);
959ac27a0ecSDave Kleikamp 	inode->i_flags |= S_NOQUOTA;
960ac27a0ecSDave Kleikamp 	inode->i_nlink = 0;
9616b38e842SAl Viro 	unlock_new_inode(inode);
962ac27a0ecSDave Kleikamp 	iput(inode);
9633300bedaSAneesh Kumar K.V 	brelse(inode_bitmap_bh);
964ac27a0ecSDave Kleikamp 	return ERR_PTR(err);
965ac27a0ecSDave Kleikamp }
966ac27a0ecSDave Kleikamp 
967ac27a0ecSDave Kleikamp /* Verify that we are loading a valid orphan from disk */
968617ba13bSMingming Cao struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
969ac27a0ecSDave Kleikamp {
970617ba13bSMingming Cao 	unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
971fd2d4291SAvantika Mathur 	ext4_group_t block_group;
972ac27a0ecSDave Kleikamp 	int bit;
9731d1fe1eeSDavid Howells 	struct buffer_head *bitmap_bh;
974ac27a0ecSDave Kleikamp 	struct inode *inode = NULL;
9751d1fe1eeSDavid Howells 	long err = -EIO;
976ac27a0ecSDave Kleikamp 
977ac27a0ecSDave Kleikamp 	/* Error cases - e2fsck has already cleaned up for us */
978ac27a0ecSDave Kleikamp 	if (ino > max_ino) {
97946e665e9SHarvey Harrison 		ext4_warning(sb, __func__,
980ac27a0ecSDave Kleikamp 			     "bad orphan ino %lu!  e2fsck was run?", ino);
9811d1fe1eeSDavid Howells 		goto error;
982ac27a0ecSDave Kleikamp 	}
983ac27a0ecSDave Kleikamp 
984617ba13bSMingming Cao 	block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
985617ba13bSMingming Cao 	bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
986e29d1cdeSEric Sandeen 	bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
987ac27a0ecSDave Kleikamp 	if (!bitmap_bh) {
98846e665e9SHarvey Harrison 		ext4_warning(sb, __func__,
989ac27a0ecSDave Kleikamp 			     "inode bitmap error for orphan %lu", ino);
9901d1fe1eeSDavid Howells 		goto error;
991ac27a0ecSDave Kleikamp 	}
992ac27a0ecSDave Kleikamp 
993ac27a0ecSDave Kleikamp 	/* Having the inode bit set should be a 100% indicator that this
994ac27a0ecSDave Kleikamp 	 * is a valid orphan (no e2fsck run on fs).  Orphans also include
995ac27a0ecSDave Kleikamp 	 * inodes that were being truncated, so we can't check i_nlink==0.
996ac27a0ecSDave Kleikamp 	 */
9971d1fe1eeSDavid Howells 	if (!ext4_test_bit(bit, bitmap_bh->b_data))
9981d1fe1eeSDavid Howells 		goto bad_orphan;
9991d1fe1eeSDavid Howells 
10001d1fe1eeSDavid Howells 	inode = ext4_iget(sb, ino);
10011d1fe1eeSDavid Howells 	if (IS_ERR(inode))
10021d1fe1eeSDavid Howells 		goto iget_failed;
10031d1fe1eeSDavid Howells 
100491ef4cafSDuane Griffin 	/*
100591ef4cafSDuane Griffin 	 * If the orphans has i_nlinks > 0 then it should be able to be
100691ef4cafSDuane Griffin 	 * truncated, otherwise it won't be removed from the orphan list
100791ef4cafSDuane Griffin 	 * during processing and an infinite loop will result.
100891ef4cafSDuane Griffin 	 */
100991ef4cafSDuane Griffin 	if (inode->i_nlink && !ext4_can_truncate(inode))
101091ef4cafSDuane Griffin 		goto bad_orphan;
101191ef4cafSDuane Griffin 
10121d1fe1eeSDavid Howells 	if (NEXT_ORPHAN(inode) > max_ino)
10131d1fe1eeSDavid Howells 		goto bad_orphan;
10141d1fe1eeSDavid Howells 	brelse(bitmap_bh);
10151d1fe1eeSDavid Howells 	return inode;
10161d1fe1eeSDavid Howells 
10171d1fe1eeSDavid Howells iget_failed:
10181d1fe1eeSDavid Howells 	err = PTR_ERR(inode);
10191d1fe1eeSDavid Howells 	inode = NULL;
10201d1fe1eeSDavid Howells bad_orphan:
102146e665e9SHarvey Harrison 	ext4_warning(sb, __func__,
1022ac27a0ecSDave Kleikamp 		     "bad orphan inode %lu!  e2fsck was run?", ino);
1023617ba13bSMingming Cao 	printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n",
1024ac27a0ecSDave Kleikamp 	       bit, (unsigned long long)bitmap_bh->b_blocknr,
1025617ba13bSMingming Cao 	       ext4_test_bit(bit, bitmap_bh->b_data));
1026ac27a0ecSDave Kleikamp 	printk(KERN_NOTICE "inode=%p\n", inode);
1027ac27a0ecSDave Kleikamp 	if (inode) {
1028ac27a0ecSDave Kleikamp 		printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
1029ac27a0ecSDave Kleikamp 		       is_bad_inode(inode));
1030ac27a0ecSDave Kleikamp 		printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
1031ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
1032ac27a0ecSDave Kleikamp 		printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
103391ef4cafSDuane Griffin 		printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink);
1034ac27a0ecSDave Kleikamp 		/* Avoid freeing blocks if we got a bad deleted inode */
10351d1fe1eeSDavid Howells 		if (inode->i_nlink == 0)
1036ac27a0ecSDave Kleikamp 			inode->i_blocks = 0;
1037ac27a0ecSDave Kleikamp 		iput(inode);
1038ac27a0ecSDave Kleikamp 	}
1039ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
10401d1fe1eeSDavid Howells error:
10411d1fe1eeSDavid Howells 	return ERR_PTR(err);
1042ac27a0ecSDave Kleikamp }
1043ac27a0ecSDave Kleikamp 
1044617ba13bSMingming Cao unsigned long ext4_count_free_inodes(struct super_block *sb)
1045ac27a0ecSDave Kleikamp {
1046ac27a0ecSDave Kleikamp 	unsigned long desc_count;
1047617ba13bSMingming Cao 	struct ext4_group_desc *gdp;
1048fd2d4291SAvantika Mathur 	ext4_group_t i;
1049617ba13bSMingming Cao #ifdef EXT4FS_DEBUG
1050617ba13bSMingming Cao 	struct ext4_super_block *es;
1051ac27a0ecSDave Kleikamp 	unsigned long bitmap_count, x;
1052ac27a0ecSDave Kleikamp 	struct buffer_head *bitmap_bh = NULL;
1053ac27a0ecSDave Kleikamp 
1054617ba13bSMingming Cao 	es = EXT4_SB(sb)->s_es;
1055ac27a0ecSDave Kleikamp 	desc_count = 0;
1056ac27a0ecSDave Kleikamp 	bitmap_count = 0;
1057ac27a0ecSDave Kleikamp 	gdp = NULL;
1058617ba13bSMingming Cao 	for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
1059617ba13bSMingming Cao 		gdp = ext4_get_group_desc(sb, i, NULL);
1060ac27a0ecSDave Kleikamp 		if (!gdp)
1061ac27a0ecSDave Kleikamp 			continue;
1062560671a0SAneesh Kumar K.V 		desc_count += ext4_free_inodes_count(sb, gdp);
1063ac27a0ecSDave Kleikamp 		brelse(bitmap_bh);
1064e29d1cdeSEric Sandeen 		bitmap_bh = ext4_read_inode_bitmap(sb, i);
1065ac27a0ecSDave Kleikamp 		if (!bitmap_bh)
1066ac27a0ecSDave Kleikamp 			continue;
1067ac27a0ecSDave Kleikamp 
1068617ba13bSMingming Cao 		x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8);
1069c549a95dSEric Sandeen 		printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
1070560671a0SAneesh Kumar K.V 			i, ext4_free_inodes_count(sb, gdp), x);
1071ac27a0ecSDave Kleikamp 		bitmap_count += x;
1072ac27a0ecSDave Kleikamp 	}
1073ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
10744776004fSTheodore Ts'o 	printk(KERN_DEBUG "ext4_count_free_inodes: "
10754776004fSTheodore Ts'o 	       "stored = %u, computed = %lu, %lu\n",
1076ac27a0ecSDave Kleikamp 	       le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
1077ac27a0ecSDave Kleikamp 	return desc_count;
1078ac27a0ecSDave Kleikamp #else
1079ac27a0ecSDave Kleikamp 	desc_count = 0;
1080617ba13bSMingming Cao 	for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
1081617ba13bSMingming Cao 		gdp = ext4_get_group_desc(sb, i, NULL);
1082ac27a0ecSDave Kleikamp 		if (!gdp)
1083ac27a0ecSDave Kleikamp 			continue;
1084560671a0SAneesh Kumar K.V 		desc_count += ext4_free_inodes_count(sb, gdp);
1085ac27a0ecSDave Kleikamp 		cond_resched();
1086ac27a0ecSDave Kleikamp 	}
1087ac27a0ecSDave Kleikamp 	return desc_count;
1088ac27a0ecSDave Kleikamp #endif
1089ac27a0ecSDave Kleikamp }
1090ac27a0ecSDave Kleikamp 
1091ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
1092617ba13bSMingming Cao unsigned long ext4_count_dirs(struct super_block * sb)
1093ac27a0ecSDave Kleikamp {
1094ac27a0ecSDave Kleikamp 	unsigned long count = 0;
1095fd2d4291SAvantika Mathur 	ext4_group_t i;
1096ac27a0ecSDave Kleikamp 
1097617ba13bSMingming Cao 	for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
1098617ba13bSMingming Cao 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1099ac27a0ecSDave Kleikamp 		if (!gdp)
1100ac27a0ecSDave Kleikamp 			continue;
1101560671a0SAneesh Kumar K.V 		count += ext4_used_dirs_count(sb, gdp);
1102ac27a0ecSDave Kleikamp 	}
1103ac27a0ecSDave Kleikamp 	return count;
1104ac27a0ecSDave Kleikamp }
1105