xref: /linux/fs/ext4/ialloc.c (revision 044e6e3d74a3d7103a0c8a9305dfd94d64000660)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2ac27a0ecSDave Kleikamp /*
3617ba13bSMingming Cao  *  linux/fs/ext4/ialloc.c
4ac27a0ecSDave Kleikamp  *
5ac27a0ecSDave Kleikamp  * Copyright (C) 1992, 1993, 1994, 1995
6ac27a0ecSDave Kleikamp  * Remy Card (card@masi.ibp.fr)
7ac27a0ecSDave Kleikamp  * Laboratoire MASI - Institut Blaise Pascal
8ac27a0ecSDave Kleikamp  * Universite Pierre et Marie Curie (Paris VI)
9ac27a0ecSDave Kleikamp  *
10ac27a0ecSDave Kleikamp  *  BSD ufs-inspired inode and directory allocation by
11ac27a0ecSDave Kleikamp  *  Stephen Tweedie (sct@redhat.com), 1993
12ac27a0ecSDave Kleikamp  *  Big-endian to little-endian byte-swapping/bitmaps by
13ac27a0ecSDave Kleikamp  *        David S. Miller (davem@caip.rutgers.edu), 1995
14ac27a0ecSDave Kleikamp  */
15ac27a0ecSDave Kleikamp 
16ac27a0ecSDave Kleikamp #include <linux/time.h>
17ac27a0ecSDave Kleikamp #include <linux/fs.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>
255b825c3aSIngo Molnar #include <linux/cred.h>
265b825c3aSIngo Molnar 
27ac27a0ecSDave Kleikamp #include <asm/byteorder.h>
289bffad1eSTheodore Ts'o 
293dcf5451SChristoph Hellwig #include "ext4.h"
303dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
31ac27a0ecSDave Kleikamp #include "xattr.h"
32ac27a0ecSDave Kleikamp #include "acl.h"
33ac27a0ecSDave Kleikamp 
349bffad1eSTheodore Ts'o #include <trace/events/ext4.h>
359bffad1eSTheodore Ts'o 
36ac27a0ecSDave Kleikamp /*
37ac27a0ecSDave Kleikamp  * ialloc.c contains the inodes allocation and deallocation routines
38ac27a0ecSDave Kleikamp  */
39ac27a0ecSDave Kleikamp 
40ac27a0ecSDave Kleikamp /*
41ac27a0ecSDave Kleikamp  * The free inodes are managed by bitmaps.  A file system contains several
42ac27a0ecSDave Kleikamp  * blocks groups.  Each group contains 1 bitmap block for blocks, 1 bitmap
43ac27a0ecSDave Kleikamp  * block for inodes, N blocks for the inode table and data blocks.
44ac27a0ecSDave Kleikamp  *
45ac27a0ecSDave Kleikamp  * The file system contains group descriptors which are located after the
46ac27a0ecSDave Kleikamp  * super block.  Each descriptor contains the number of the bitmap block and
47ac27a0ecSDave Kleikamp  * the free blocks count in the block.
48ac27a0ecSDave Kleikamp  */
49ac27a0ecSDave Kleikamp 
50717d50e4SAndreas Dilger /*
51717d50e4SAndreas Dilger  * To avoid calling the atomic setbit hundreds or thousands of times, we only
52717d50e4SAndreas Dilger  * need to use it within a single byte (to ensure we get endianness right).
53717d50e4SAndreas Dilger  * We can use memset for the rest of the bitmap as there are no other users.
54717d50e4SAndreas Dilger  */
5561d08673STheodore Ts'o void ext4_mark_bitmap_end(int start_bit, int end_bit, char *bitmap)
56717d50e4SAndreas Dilger {
57717d50e4SAndreas Dilger 	int i;
58717d50e4SAndreas Dilger 
59717d50e4SAndreas Dilger 	if (start_bit >= end_bit)
60717d50e4SAndreas Dilger 		return;
61717d50e4SAndreas Dilger 
62717d50e4SAndreas Dilger 	ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit);
63717d50e4SAndreas Dilger 	for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
64717d50e4SAndreas Dilger 		ext4_set_bit(i, bitmap);
65717d50e4SAndreas Dilger 	if (i < end_bit)
66717d50e4SAndreas Dilger 		memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
67717d50e4SAndreas Dilger }
68717d50e4SAndreas Dilger 
69813e5727STheodore Ts'o void ext4_end_bitmap_read(struct buffer_head *bh, int uptodate)
70813e5727STheodore Ts'o {
71813e5727STheodore Ts'o 	if (uptodate) {
72813e5727STheodore Ts'o 		set_buffer_uptodate(bh);
73813e5727STheodore Ts'o 		set_bitmap_uptodate(bh);
74813e5727STheodore Ts'o 	}
75813e5727STheodore Ts'o 	unlock_buffer(bh);
76813e5727STheodore Ts'o 	put_bh(bh);
77813e5727STheodore Ts'o }
78813e5727STheodore Ts'o 
799008a58eSDarrick J. Wong static int ext4_validate_inode_bitmap(struct super_block *sb,
809008a58eSDarrick J. Wong 				      struct ext4_group_desc *desc,
819008a58eSDarrick J. Wong 				      ext4_group_t block_group,
829008a58eSDarrick J. Wong 				      struct buffer_head *bh)
839008a58eSDarrick J. Wong {
849008a58eSDarrick J. Wong 	ext4_fsblk_t	blk;
859008a58eSDarrick J. Wong 	struct ext4_group_info *grp = ext4_get_group_info(sb, block_group);
869008a58eSDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
879008a58eSDarrick J. Wong 
889008a58eSDarrick J. Wong 	if (buffer_verified(bh))
899008a58eSDarrick J. Wong 		return 0;
909008a58eSDarrick J. Wong 	if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp))
919008a58eSDarrick J. Wong 		return -EFSCORRUPTED;
929008a58eSDarrick J. Wong 
939008a58eSDarrick J. Wong 	ext4_lock_group(sb, block_group);
949008a58eSDarrick J. Wong 	blk = ext4_inode_bitmap(sb, desc);
959008a58eSDarrick J. Wong 	if (!ext4_inode_bitmap_csum_verify(sb, block_group, desc, bh,
969008a58eSDarrick J. Wong 					   EXT4_INODES_PER_GROUP(sb) / 8)) {
979008a58eSDarrick J. Wong 		ext4_unlock_group(sb, block_group);
989008a58eSDarrick J. Wong 		ext4_error(sb, "Corrupt inode bitmap - block_group = %u, "
999008a58eSDarrick J. Wong 			   "inode_bitmap = %llu", block_group, blk);
1009008a58eSDarrick J. Wong 		grp = ext4_get_group_info(sb, block_group);
1019008a58eSDarrick J. Wong 		if (!EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) {
1029008a58eSDarrick J. Wong 			int count;
1039008a58eSDarrick J. Wong 			count = ext4_free_inodes_count(sb, desc);
1049008a58eSDarrick J. Wong 			percpu_counter_sub(&sbi->s_freeinodes_counter,
1059008a58eSDarrick J. Wong 					   count);
1069008a58eSDarrick J. Wong 		}
1079008a58eSDarrick J. Wong 		set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &grp->bb_state);
1089008a58eSDarrick J. Wong 		return -EFSBADCRC;
1099008a58eSDarrick J. Wong 	}
1109008a58eSDarrick J. Wong 	set_buffer_verified(bh);
1119008a58eSDarrick J. Wong 	ext4_unlock_group(sb, block_group);
1129008a58eSDarrick J. Wong 	return 0;
1139008a58eSDarrick J. Wong }
1149008a58eSDarrick J. Wong 
115ac27a0ecSDave Kleikamp /*
116ac27a0ecSDave Kleikamp  * Read the inode allocation bitmap for a given block_group, reading
117ac27a0ecSDave Kleikamp  * into the specified slot in the superblock's bitmap cache.
118ac27a0ecSDave Kleikamp  *
119ac27a0ecSDave Kleikamp  * Return buffer_head of bitmap on success or NULL.
120ac27a0ecSDave Kleikamp  */
121ac27a0ecSDave Kleikamp static struct buffer_head *
122e29d1cdeSEric Sandeen ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group)
123ac27a0ecSDave Kleikamp {
124617ba13bSMingming Cao 	struct ext4_group_desc *desc;
125ac27a0ecSDave Kleikamp 	struct buffer_head *bh = NULL;
126e29d1cdeSEric Sandeen 	ext4_fsblk_t bitmap_blk;
1279008a58eSDarrick J. Wong 	int err;
128ac27a0ecSDave Kleikamp 
129617ba13bSMingming Cao 	desc = ext4_get_group_desc(sb, block_group, NULL);
130ac27a0ecSDave Kleikamp 	if (!desc)
1319008a58eSDarrick J. Wong 		return ERR_PTR(-EFSCORRUPTED);
132bfff6873SLukas Czerner 
133e29d1cdeSEric Sandeen 	bitmap_blk = ext4_inode_bitmap(sb, desc);
134e29d1cdeSEric Sandeen 	bh = sb_getblk(sb, bitmap_blk);
135e29d1cdeSEric Sandeen 	if (unlikely(!bh)) {
13612062dddSEric Sandeen 		ext4_error(sb, "Cannot read inode bitmap - "
137a9df9a49STheodore Ts'o 			    "block_group = %u, inode_bitmap = %llu",
138e29d1cdeSEric Sandeen 			    block_group, bitmap_blk);
1399008a58eSDarrick J. Wong 		return ERR_PTR(-EIO);
140e29d1cdeSEric Sandeen 	}
1412ccb5fb9SAneesh Kumar K.V 	if (bitmap_uptodate(bh))
14241a246d1SDarrick J. Wong 		goto verify;
143e29d1cdeSEric Sandeen 
144c806e68fSFrederic Bohe 	lock_buffer(bh);
1452ccb5fb9SAneesh Kumar K.V 	if (bitmap_uptodate(bh)) {
1462ccb5fb9SAneesh Kumar K.V 		unlock_buffer(bh);
14741a246d1SDarrick J. Wong 		goto verify;
1482ccb5fb9SAneesh Kumar K.V 	}
149bfff6873SLukas Czerner 
150955ce5f5SAneesh Kumar K.V 	ext4_lock_group(sb, block_group);
151e29d1cdeSEric Sandeen 	if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
152*044e6e3dSTheodore Ts'o 		memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8);
153*044e6e3dSTheodore Ts'o 		ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb),
154*044e6e3dSTheodore Ts'o 				     sb->s_blocksize * 8, bh->b_data);
1552ccb5fb9SAneesh Kumar K.V 		set_bitmap_uptodate(bh);
156e29d1cdeSEric Sandeen 		set_buffer_uptodate(bh);
15741a246d1SDarrick J. Wong 		set_buffer_verified(bh);
158955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, block_group);
1593300bedaSAneesh Kumar K.V 		unlock_buffer(bh);
160e29d1cdeSEric Sandeen 		return bh;
161e29d1cdeSEric Sandeen 	}
162955ce5f5SAneesh Kumar K.V 	ext4_unlock_group(sb, block_group);
163bfff6873SLukas Czerner 
1642ccb5fb9SAneesh Kumar K.V 	if (buffer_uptodate(bh)) {
1652ccb5fb9SAneesh Kumar K.V 		/*
1662ccb5fb9SAneesh Kumar K.V 		 * if not uninit if bh is uptodate,
1672ccb5fb9SAneesh Kumar K.V 		 * bitmap is also uptodate
1682ccb5fb9SAneesh Kumar K.V 		 */
1692ccb5fb9SAneesh Kumar K.V 		set_bitmap_uptodate(bh);
1702ccb5fb9SAneesh Kumar K.V 		unlock_buffer(bh);
17141a246d1SDarrick J. Wong 		goto verify;
1722ccb5fb9SAneesh Kumar K.V 	}
1732ccb5fb9SAneesh Kumar K.V 	/*
174813e5727STheodore Ts'o 	 * submit the buffer_head for reading
1752ccb5fb9SAneesh Kumar K.V 	 */
1760562e0baSJiaying Zhang 	trace_ext4_load_inode_bitmap(sb, block_group);
177813e5727STheodore Ts'o 	bh->b_end_io = ext4_end_bitmap_read;
178813e5727STheodore Ts'o 	get_bh(bh);
1792a222ca9SMike Christie 	submit_bh(REQ_OP_READ, REQ_META | REQ_PRIO, bh);
180813e5727STheodore Ts'o 	wait_on_buffer(bh);
181813e5727STheodore Ts'o 	if (!buffer_uptodate(bh)) {
182e29d1cdeSEric Sandeen 		put_bh(bh);
18312062dddSEric Sandeen 		ext4_error(sb, "Cannot read inode bitmap - "
184a9df9a49STheodore Ts'o 			   "block_group = %u, inode_bitmap = %llu",
185e29d1cdeSEric Sandeen 			   block_group, bitmap_blk);
1869008a58eSDarrick J. Wong 		return ERR_PTR(-EIO);
187e29d1cdeSEric Sandeen 	}
18841a246d1SDarrick J. Wong 
18941a246d1SDarrick J. Wong verify:
1909008a58eSDarrick J. Wong 	err = ext4_validate_inode_bitmap(sb, desc, block_group, bh);
1919008a58eSDarrick J. Wong 	if (err)
1929008a58eSDarrick J. Wong 		goto out;
193ac27a0ecSDave Kleikamp 	return bh;
1949008a58eSDarrick J. Wong out:
1959008a58eSDarrick J. Wong 	put_bh(bh);
1969008a58eSDarrick J. Wong 	return ERR_PTR(err);
197ac27a0ecSDave Kleikamp }
198ac27a0ecSDave Kleikamp 
199ac27a0ecSDave Kleikamp /*
200ac27a0ecSDave Kleikamp  * NOTE! When we get the inode, we're the only people
201ac27a0ecSDave Kleikamp  * that have access to it, and as such there are no
202ac27a0ecSDave Kleikamp  * race conditions we have to worry about. The inode
203ac27a0ecSDave Kleikamp  * is not on the hash-lists, and it cannot be reached
204ac27a0ecSDave Kleikamp  * through the filesystem because the directory entry
205ac27a0ecSDave Kleikamp  * has been deleted earlier.
206ac27a0ecSDave Kleikamp  *
207ac27a0ecSDave Kleikamp  * HOWEVER: we must make sure that we get no aliases,
208ac27a0ecSDave Kleikamp  * which means that we have to call "clear_inode()"
209ac27a0ecSDave Kleikamp  * _before_ we mark the inode not in use in the inode
210ac27a0ecSDave Kleikamp  * bitmaps. Otherwise a newly created file might use
211ac27a0ecSDave Kleikamp  * the same inode number (not actually the same pointer
212ac27a0ecSDave Kleikamp  * though), and then we'd have two inodes sharing the
213ac27a0ecSDave Kleikamp  * same inode number and space on the harddisk.
214ac27a0ecSDave Kleikamp  */
215617ba13bSMingming Cao void ext4_free_inode(handle_t *handle, struct inode *inode)
216ac27a0ecSDave Kleikamp {
217ac27a0ecSDave Kleikamp 	struct super_block *sb = inode->i_sb;
218ac27a0ecSDave Kleikamp 	int is_directory;
219ac27a0ecSDave Kleikamp 	unsigned long ino;
220ac27a0ecSDave Kleikamp 	struct buffer_head *bitmap_bh = NULL;
221ac27a0ecSDave Kleikamp 	struct buffer_head *bh2;
222fd2d4291SAvantika Mathur 	ext4_group_t block_group;
223ac27a0ecSDave Kleikamp 	unsigned long bit;
224617ba13bSMingming Cao 	struct ext4_group_desc *gdp;
225617ba13bSMingming Cao 	struct ext4_super_block *es;
226617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
2277ce9d5d1SEric Sandeen 	int fatal = 0, err, count, cleared;
22887a39389SDarrick J. Wong 	struct ext4_group_info *grp;
229ac27a0ecSDave Kleikamp 
23092b97816STheodore Ts'o 	if (!sb) {
23192b97816STheodore Ts'o 		printk(KERN_ERR "EXT4-fs: %s:%d: inode on "
23292b97816STheodore Ts'o 		       "nonexistent device\n", __func__, __LINE__);
23392b97816STheodore Ts'o 		return;
23492b97816STheodore Ts'o 	}
235ac27a0ecSDave Kleikamp 	if (atomic_read(&inode->i_count) > 1) {
23692b97816STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: count=%d",
23792b97816STheodore Ts'o 			 __func__, __LINE__, inode->i_ino,
238ac27a0ecSDave Kleikamp 			 atomic_read(&inode->i_count));
239ac27a0ecSDave Kleikamp 		return;
240ac27a0ecSDave Kleikamp 	}
241ac27a0ecSDave Kleikamp 	if (inode->i_nlink) {
24292b97816STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: nlink=%d\n",
24392b97816STheodore Ts'o 			 __func__, __LINE__, inode->i_ino, inode->i_nlink);
244ac27a0ecSDave Kleikamp 		return;
245ac27a0ecSDave Kleikamp 	}
246617ba13bSMingming Cao 	sbi = EXT4_SB(sb);
247ac27a0ecSDave Kleikamp 
248ac27a0ecSDave Kleikamp 	ino = inode->i_ino;
249617ba13bSMingming Cao 	ext4_debug("freeing inode %lu\n", ino);
2509bffad1eSTheodore Ts'o 	trace_ext4_free_inode(inode);
251ac27a0ecSDave Kleikamp 
252ac27a0ecSDave Kleikamp 	/*
253ac27a0ecSDave Kleikamp 	 * Note: we must free any quota before locking the superblock,
254ac27a0ecSDave Kleikamp 	 * as writing the quota to disk may need the lock as well.
255ac27a0ecSDave Kleikamp 	 */
256871a2931SChristoph Hellwig 	dquot_initialize(inode);
25763936ddaSChristoph Hellwig 	dquot_free_inode(inode);
2589f754758SChristoph Hellwig 	dquot_drop(inode);
259ac27a0ecSDave Kleikamp 
260ac27a0ecSDave Kleikamp 	is_directory = S_ISDIR(inode->i_mode);
261ac27a0ecSDave Kleikamp 
262ac27a0ecSDave Kleikamp 	/* Do this BEFORE marking the inode not in use or returning an error */
2630930fcc1SAl Viro 	ext4_clear_inode(inode);
264ac27a0ecSDave Kleikamp 
26549598e04SJun Piao 	es = sbi->s_es;
266617ba13bSMingming Cao 	if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
26712062dddSEric Sandeen 		ext4_error(sb, "reserved or nonexistent inode %lu", ino);
268ac27a0ecSDave Kleikamp 		goto error_return;
269ac27a0ecSDave Kleikamp 	}
270617ba13bSMingming Cao 	block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
271617ba13bSMingming Cao 	bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
272e29d1cdeSEric Sandeen 	bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
27387a39389SDarrick J. Wong 	/* Don't bother if the inode bitmap is corrupt. */
27487a39389SDarrick J. Wong 	grp = ext4_get_group_info(sb, block_group);
2759008a58eSDarrick J. Wong 	if (IS_ERR(bitmap_bh)) {
2769008a58eSDarrick J. Wong 		fatal = PTR_ERR(bitmap_bh);
2779008a58eSDarrick J. Wong 		bitmap_bh = NULL;
278ac27a0ecSDave Kleikamp 		goto error_return;
2799008a58eSDarrick J. Wong 	}
2809008a58eSDarrick J. Wong 	if (unlikely(EXT4_MB_GRP_IBITMAP_CORRUPT(grp))) {
2819008a58eSDarrick J. Wong 		fatal = -EFSCORRUPTED;
2829008a58eSDarrick J. Wong 		goto error_return;
2839008a58eSDarrick J. Wong 	}
284ac27a0ecSDave Kleikamp 
285ac27a0ecSDave Kleikamp 	BUFFER_TRACE(bitmap_bh, "get_write_access");
286617ba13bSMingming Cao 	fatal = ext4_journal_get_write_access(handle, bitmap_bh);
287ac27a0ecSDave Kleikamp 	if (fatal)
288ac27a0ecSDave Kleikamp 		goto error_return;
289ac27a0ecSDave Kleikamp 
290d17413c0SDmitry Monakhov 	fatal = -ESRCH;
291617ba13bSMingming Cao 	gdp = ext4_get_group_desc(sb, block_group, &bh2);
292d17413c0SDmitry Monakhov 	if (gdp) {
293ac27a0ecSDave Kleikamp 		BUFFER_TRACE(bh2, "get_write_access");
294617ba13bSMingming Cao 		fatal = ext4_journal_get_write_access(handle, bh2);
295d17413c0SDmitry Monakhov 	}
296955ce5f5SAneesh Kumar K.V 	ext4_lock_group(sb, block_group);
297597d508cSAkinobu Mita 	cleared = ext4_test_and_clear_bit(bit, bitmap_bh->b_data);
298d17413c0SDmitry Monakhov 	if (fatal || !cleared) {
299d17413c0SDmitry Monakhov 		ext4_unlock_group(sb, block_group);
300d17413c0SDmitry Monakhov 		goto out;
301d17413c0SDmitry Monakhov 	}
302d17413c0SDmitry Monakhov 
303560671a0SAneesh Kumar K.V 	count = ext4_free_inodes_count(sb, gdp) + 1;
304560671a0SAneesh Kumar K.V 	ext4_free_inodes_set(sb, gdp, count);
305560671a0SAneesh Kumar K.V 	if (is_directory) {
306560671a0SAneesh Kumar K.V 		count = ext4_used_dirs_count(sb, gdp) - 1;
307560671a0SAneesh Kumar K.V 		ext4_used_dirs_set(sb, gdp, count);
308d17413c0SDmitry Monakhov 		percpu_counter_dec(&sbi->s_dirs_counter);
309d17413c0SDmitry Monakhov 	}
31041a246d1SDarrick J. Wong 	ext4_inode_bitmap_csum_set(sb, block_group, gdp, bitmap_bh,
31141a246d1SDarrick J. Wong 				   EXT4_INODES_PER_GROUP(sb) / 8);
312feb0ab32SDarrick J. Wong 	ext4_group_desc_csum_set(sb, block_group, gdp);
313d17413c0SDmitry Monakhov 	ext4_unlock_group(sb, block_group);
3147d39db14STheodore Ts'o 
315d17413c0SDmitry Monakhov 	percpu_counter_inc(&sbi->s_freeinodes_counter);
316d17413c0SDmitry Monakhov 	if (sbi->s_log_groups_per_flex) {
317d17413c0SDmitry Monakhov 		ext4_group_t f = ext4_flex_group(sbi, block_group);
318d17413c0SDmitry Monakhov 
319d17413c0SDmitry Monakhov 		atomic_inc(&sbi->s_flex_groups[f].free_inodes);
320d17413c0SDmitry Monakhov 		if (is_directory)
321c4caae25SEric Sandeen 			atomic_dec(&sbi->s_flex_groups[f].used_dirs);
3227d39db14STheodore Ts'o 	}
3230390131bSFrank Mayhar 	BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata");
324d17413c0SDmitry Monakhov 	fatal = ext4_handle_dirty_metadata(handle, NULL, bh2);
325d17413c0SDmitry Monakhov out:
326d17413c0SDmitry Monakhov 	if (cleared) {
3270390131bSFrank Mayhar 		BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata");
3280390131bSFrank Mayhar 		err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
329ac27a0ecSDave Kleikamp 		if (!fatal)
330ac27a0ecSDave Kleikamp 			fatal = err;
33187a39389SDarrick J. Wong 	} else {
332d17413c0SDmitry Monakhov 		ext4_error(sb, "bit already cleared for inode %lu", ino);
333bf40c926SNamjae Jeon 		if (gdp && !EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) {
334e43bb4e6SNamjae Jeon 			int count;
335e43bb4e6SNamjae Jeon 			count = ext4_free_inodes_count(sb, gdp);
336e43bb4e6SNamjae Jeon 			percpu_counter_sub(&sbi->s_freeinodes_counter,
337e43bb4e6SNamjae Jeon 					   count);
338e43bb4e6SNamjae Jeon 		}
33987a39389SDarrick J. Wong 		set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &grp->bb_state);
34087a39389SDarrick J. Wong 	}
341d17413c0SDmitry Monakhov 
342ac27a0ecSDave Kleikamp error_return:
343ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
344617ba13bSMingming Cao 	ext4_std_error(sb, fatal);
345ac27a0ecSDave Kleikamp }
346ac27a0ecSDave Kleikamp 
347a4912123STheodore Ts'o struct orlov_stats {
34890ba983fSTheodore Ts'o 	__u64 free_clusters;
349a4912123STheodore Ts'o 	__u32 free_inodes;
350a4912123STheodore Ts'o 	__u32 used_dirs;
351a4912123STheodore Ts'o };
352a4912123STheodore Ts'o 
353a4912123STheodore Ts'o /*
354a4912123STheodore Ts'o  * Helper function for Orlov's allocator; returns critical information
355a4912123STheodore Ts'o  * for a particular block group or flex_bg.  If flex_size is 1, then g
356a4912123STheodore Ts'o  * is a block group number; otherwise it is flex_bg number.
357a4912123STheodore Ts'o  */
3581f109d5aSTheodore Ts'o static void get_orlov_stats(struct super_block *sb, ext4_group_t g,
359a4912123STheodore Ts'o 			    int flex_size, struct orlov_stats *stats)
360a4912123STheodore Ts'o {
361a4912123STheodore Ts'o 	struct ext4_group_desc *desc;
3627d39db14STheodore Ts'o 	struct flex_groups *flex_group = EXT4_SB(sb)->s_flex_groups;
363a4912123STheodore Ts'o 
3647d39db14STheodore Ts'o 	if (flex_size > 1) {
3657d39db14STheodore Ts'o 		stats->free_inodes = atomic_read(&flex_group[g].free_inodes);
36690ba983fSTheodore Ts'o 		stats->free_clusters = atomic64_read(&flex_group[g].free_clusters);
3677d39db14STheodore Ts'o 		stats->used_dirs = atomic_read(&flex_group[g].used_dirs);
3687d39db14STheodore Ts'o 		return;
3697d39db14STheodore Ts'o 	}
3707d39db14STheodore Ts'o 
3717d39db14STheodore Ts'o 	desc = ext4_get_group_desc(sb, g, NULL);
3727d39db14STheodore Ts'o 	if (desc) {
3737d39db14STheodore Ts'o 		stats->free_inodes = ext4_free_inodes_count(sb, desc);
374021b65bbSTheodore Ts'o 		stats->free_clusters = ext4_free_group_clusters(sb, desc);
3757d39db14STheodore Ts'o 		stats->used_dirs = ext4_used_dirs_count(sb, desc);
3767d39db14STheodore Ts'o 	} else {
377a4912123STheodore Ts'o 		stats->free_inodes = 0;
37824aaa8efSTheodore Ts'o 		stats->free_clusters = 0;
379a4912123STheodore Ts'o 		stats->used_dirs = 0;
380a4912123STheodore Ts'o 	}
381a4912123STheodore Ts'o }
382a4912123STheodore Ts'o 
383ac27a0ecSDave Kleikamp /*
384ac27a0ecSDave Kleikamp  * Orlov's allocator for directories.
385ac27a0ecSDave Kleikamp  *
386ac27a0ecSDave Kleikamp  * We always try to spread first-level directories.
387ac27a0ecSDave Kleikamp  *
388ac27a0ecSDave Kleikamp  * If there are blockgroups with both free inodes and free blocks counts
389ac27a0ecSDave Kleikamp  * not worse than average we return one with smallest directory count.
390ac27a0ecSDave Kleikamp  * Otherwise we simply return a random group.
391ac27a0ecSDave Kleikamp  *
392ac27a0ecSDave Kleikamp  * For the rest rules look so:
393ac27a0ecSDave Kleikamp  *
394ac27a0ecSDave Kleikamp  * It's OK to put directory into a group unless
395ac27a0ecSDave Kleikamp  * it has too many directories already (max_dirs) or
396ac27a0ecSDave Kleikamp  * it has too few free inodes left (min_inodes) or
397ac27a0ecSDave Kleikamp  * it has too few free blocks left (min_blocks) or
3981cc8dcf5SBenoit Boissinot  * Parent's group is preferred, if it doesn't satisfy these
399ac27a0ecSDave Kleikamp  * conditions we search cyclically through the rest. If none
400ac27a0ecSDave Kleikamp  * of the groups look good we just look for a group with more
401ac27a0ecSDave Kleikamp  * free inodes than average (starting at parent's group).
402ac27a0ecSDave Kleikamp  */
403ac27a0ecSDave Kleikamp 
4042aa9fc4cSAvantika Mathur static int find_group_orlov(struct super_block *sb, struct inode *parent,
405dcca3fecSAl Viro 			    ext4_group_t *group, umode_t mode,
406f157a4aaSTheodore Ts'o 			    const struct qstr *qstr)
407ac27a0ecSDave Kleikamp {
408fd2d4291SAvantika Mathur 	ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
409617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4108df9675fSTheodore Ts'o 	ext4_group_t real_ngroups = ext4_get_groups_count(sb);
411617ba13bSMingming Cao 	int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
41214c83c9fSTheodore Ts'o 	unsigned int freei, avefreei, grp_free;
41324aaa8efSTheodore Ts'o 	ext4_fsblk_t freeb, avefreec;
414ac27a0ecSDave Kleikamp 	unsigned int ndirs;
415a4912123STheodore Ts'o 	int max_dirs, min_inodes;
41624aaa8efSTheodore Ts'o 	ext4_grpblk_t min_clusters;
4178df9675fSTheodore Ts'o 	ext4_group_t i, grp, g, ngroups;
418617ba13bSMingming Cao 	struct ext4_group_desc *desc;
419a4912123STheodore Ts'o 	struct orlov_stats stats;
420a4912123STheodore Ts'o 	int flex_size = ext4_flex_bg_size(sbi);
421f157a4aaSTheodore Ts'o 	struct dx_hash_info hinfo;
422a4912123STheodore Ts'o 
4238df9675fSTheodore Ts'o 	ngroups = real_ngroups;
424a4912123STheodore Ts'o 	if (flex_size > 1) {
4258df9675fSTheodore Ts'o 		ngroups = (real_ngroups + flex_size - 1) >>
426a4912123STheodore Ts'o 			sbi->s_log_groups_per_flex;
427a4912123STheodore Ts'o 		parent_group >>= sbi->s_log_groups_per_flex;
428a4912123STheodore Ts'o 	}
429ac27a0ecSDave Kleikamp 
430ac27a0ecSDave Kleikamp 	freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
431ac27a0ecSDave Kleikamp 	avefreei = freei / ngroups;
43257042651STheodore Ts'o 	freeb = EXT4_C2B(sbi,
43357042651STheodore Ts'o 		percpu_counter_read_positive(&sbi->s_freeclusters_counter));
43424aaa8efSTheodore Ts'o 	avefreec = freeb;
43524aaa8efSTheodore Ts'o 	do_div(avefreec, ngroups);
436ac27a0ecSDave Kleikamp 	ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
437ac27a0ecSDave Kleikamp 
438a4912123STheodore Ts'o 	if (S_ISDIR(mode) &&
4392b0143b5SDavid Howells 	    ((parent == d_inode(sb->s_root)) ||
44012e9b892SDmitry Monakhov 	     (ext4_test_inode_flag(parent, EXT4_INODE_TOPDIR)))) {
441ac27a0ecSDave Kleikamp 		int best_ndir = inodes_per_group;
4422aa9fc4cSAvantika Mathur 		int ret = -1;
443ac27a0ecSDave Kleikamp 
444f157a4aaSTheodore Ts'o 		if (qstr) {
445f157a4aaSTheodore Ts'o 			hinfo.hash_version = DX_HASH_HALF_MD4;
446f157a4aaSTheodore Ts'o 			hinfo.seed = sbi->s_hash_seed;
447f157a4aaSTheodore Ts'o 			ext4fs_dirhash(qstr->name, qstr->len, &hinfo);
448f157a4aaSTheodore Ts'o 			grp = hinfo.hash;
449f157a4aaSTheodore Ts'o 		} else
450dd1f723bSTheodore Ts'o 			grp = prandom_u32();
4512aa9fc4cSAvantika Mathur 		parent_group = (unsigned)grp % ngroups;
452ac27a0ecSDave Kleikamp 		for (i = 0; i < ngroups; i++) {
453a4912123STheodore Ts'o 			g = (parent_group + i) % ngroups;
454a4912123STheodore Ts'o 			get_orlov_stats(sb, g, flex_size, &stats);
455a4912123STheodore Ts'o 			if (!stats.free_inodes)
456ac27a0ecSDave Kleikamp 				continue;
457a4912123STheodore Ts'o 			if (stats.used_dirs >= best_ndir)
458ac27a0ecSDave Kleikamp 				continue;
459a4912123STheodore Ts'o 			if (stats.free_inodes < avefreei)
460ac27a0ecSDave Kleikamp 				continue;
46124aaa8efSTheodore Ts'o 			if (stats.free_clusters < avefreec)
462ac27a0ecSDave Kleikamp 				continue;
463a4912123STheodore Ts'o 			grp = g;
4642aa9fc4cSAvantika Mathur 			ret = 0;
465a4912123STheodore Ts'o 			best_ndir = stats.used_dirs;
466ac27a0ecSDave Kleikamp 		}
467a4912123STheodore Ts'o 		if (ret)
468a4912123STheodore Ts'o 			goto fallback;
469a4912123STheodore Ts'o 	found_flex_bg:
470a4912123STheodore Ts'o 		if (flex_size == 1) {
471a4912123STheodore Ts'o 			*group = grp;
472a4912123STheodore Ts'o 			return 0;
473a4912123STheodore Ts'o 		}
474a4912123STheodore Ts'o 
475a4912123STheodore Ts'o 		/*
476a4912123STheodore Ts'o 		 * We pack inodes at the beginning of the flexgroup's
477a4912123STheodore Ts'o 		 * inode tables.  Block allocation decisions will do
478a4912123STheodore Ts'o 		 * something similar, although regular files will
479a4912123STheodore Ts'o 		 * start at 2nd block group of the flexgroup.  See
480a4912123STheodore Ts'o 		 * ext4_ext_find_goal() and ext4_find_near().
481a4912123STheodore Ts'o 		 */
482a4912123STheodore Ts'o 		grp *= flex_size;
483a4912123STheodore Ts'o 		for (i = 0; i < flex_size; i++) {
4848df9675fSTheodore Ts'o 			if (grp+i >= real_ngroups)
485a4912123STheodore Ts'o 				break;
486a4912123STheodore Ts'o 			desc = ext4_get_group_desc(sb, grp+i, NULL);
487a4912123STheodore Ts'o 			if (desc && ext4_free_inodes_count(sb, desc)) {
488a4912123STheodore Ts'o 				*group = grp+i;
489a4912123STheodore Ts'o 				return 0;
490a4912123STheodore Ts'o 			}
491a4912123STheodore Ts'o 		}
492ac27a0ecSDave Kleikamp 		goto fallback;
493ac27a0ecSDave Kleikamp 	}
494ac27a0ecSDave Kleikamp 
495ac27a0ecSDave Kleikamp 	max_dirs = ndirs / ngroups + inodes_per_group / 16;
496a4912123STheodore Ts'o 	min_inodes = avefreei - inodes_per_group*flex_size / 4;
497a4912123STheodore Ts'o 	if (min_inodes < 1)
498a4912123STheodore Ts'o 		min_inodes = 1;
49924aaa8efSTheodore Ts'o 	min_clusters = avefreec - EXT4_CLUSTERS_PER_GROUP(sb)*flex_size / 4;
500ac27a0ecSDave Kleikamp 
501a4912123STheodore Ts'o 	/*
502a4912123STheodore Ts'o 	 * Start looking in the flex group where we last allocated an
503a4912123STheodore Ts'o 	 * inode for this parent directory
504a4912123STheodore Ts'o 	 */
505a4912123STheodore Ts'o 	if (EXT4_I(parent)->i_last_alloc_group != ~0) {
506a4912123STheodore Ts'o 		parent_group = EXT4_I(parent)->i_last_alloc_group;
507a4912123STheodore Ts'o 		if (flex_size > 1)
508a4912123STheodore Ts'o 			parent_group >>= sbi->s_log_groups_per_flex;
509a4912123STheodore Ts'o 	}
510ac27a0ecSDave Kleikamp 
511ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
512a4912123STheodore Ts'o 		grp = (parent_group + i) % ngroups;
513a4912123STheodore Ts'o 		get_orlov_stats(sb, grp, flex_size, &stats);
514a4912123STheodore Ts'o 		if (stats.used_dirs >= max_dirs)
515ac27a0ecSDave Kleikamp 			continue;
516a4912123STheodore Ts'o 		if (stats.free_inodes < min_inodes)
517ac27a0ecSDave Kleikamp 			continue;
51824aaa8efSTheodore Ts'o 		if (stats.free_clusters < min_clusters)
519ac27a0ecSDave Kleikamp 			continue;
520a4912123STheodore Ts'o 		goto found_flex_bg;
521ac27a0ecSDave Kleikamp 	}
522ac27a0ecSDave Kleikamp 
523ac27a0ecSDave Kleikamp fallback:
5248df9675fSTheodore Ts'o 	ngroups = real_ngroups;
525a4912123STheodore Ts'o 	avefreei = freei / ngroups;
526b5451f7bSTheodore Ts'o fallback_retry:
527a4912123STheodore Ts'o 	parent_group = EXT4_I(parent)->i_block_group;
528ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
529a4912123STheodore Ts'o 		grp = (parent_group + i) % ngroups;
530a4912123STheodore Ts'o 		desc = ext4_get_group_desc(sb, grp, NULL);
531bb3d132aSDan Carpenter 		if (desc) {
53214c83c9fSTheodore Ts'o 			grp_free = ext4_free_inodes_count(sb, desc);
533bb3d132aSDan Carpenter 			if (grp_free && grp_free >= avefreei) {
534a4912123STheodore Ts'o 				*group = grp;
5352aa9fc4cSAvantika Mathur 				return 0;
536ac27a0ecSDave Kleikamp 			}
537a4912123STheodore Ts'o 		}
538bb3d132aSDan Carpenter 	}
539ac27a0ecSDave Kleikamp 
540ac27a0ecSDave Kleikamp 	if (avefreei) {
541ac27a0ecSDave Kleikamp 		/*
542ac27a0ecSDave Kleikamp 		 * The free-inodes counter is approximate, and for really small
543ac27a0ecSDave Kleikamp 		 * filesystems the above test can fail to find any blockgroups
544ac27a0ecSDave Kleikamp 		 */
545ac27a0ecSDave Kleikamp 		avefreei = 0;
546b5451f7bSTheodore Ts'o 		goto fallback_retry;
547ac27a0ecSDave Kleikamp 	}
548ac27a0ecSDave Kleikamp 
549ac27a0ecSDave Kleikamp 	return -1;
550ac27a0ecSDave Kleikamp }
551ac27a0ecSDave Kleikamp 
5522aa9fc4cSAvantika Mathur static int find_group_other(struct super_block *sb, struct inode *parent,
553dcca3fecSAl Viro 			    ext4_group_t *group, umode_t mode)
554ac27a0ecSDave Kleikamp {
555fd2d4291SAvantika Mathur 	ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
5568df9675fSTheodore Ts'o 	ext4_group_t i, last, ngroups = ext4_get_groups_count(sb);
557617ba13bSMingming Cao 	struct ext4_group_desc *desc;
558a4912123STheodore Ts'o 	int flex_size = ext4_flex_bg_size(EXT4_SB(sb));
559a4912123STheodore Ts'o 
560a4912123STheodore Ts'o 	/*
561a4912123STheodore Ts'o 	 * Try to place the inode is the same flex group as its
562a4912123STheodore Ts'o 	 * parent.  If we can't find space, use the Orlov algorithm to
563a4912123STheodore Ts'o 	 * find another flex group, and store that information in the
564a4912123STheodore Ts'o 	 * parent directory's inode information so that use that flex
565a4912123STheodore Ts'o 	 * group for future allocations.
566a4912123STheodore Ts'o 	 */
567a4912123STheodore Ts'o 	if (flex_size > 1) {
568a4912123STheodore Ts'o 		int retry = 0;
569a4912123STheodore Ts'o 
570a4912123STheodore Ts'o 	try_again:
571a4912123STheodore Ts'o 		parent_group &= ~(flex_size-1);
572a4912123STheodore Ts'o 		last = parent_group + flex_size;
573a4912123STheodore Ts'o 		if (last > ngroups)
574a4912123STheodore Ts'o 			last = ngroups;
575a4912123STheodore Ts'o 		for  (i = parent_group; i < last; i++) {
576a4912123STheodore Ts'o 			desc = ext4_get_group_desc(sb, i, NULL);
577a4912123STheodore Ts'o 			if (desc && ext4_free_inodes_count(sb, desc)) {
578a4912123STheodore Ts'o 				*group = i;
579a4912123STheodore Ts'o 				return 0;
580a4912123STheodore Ts'o 			}
581a4912123STheodore Ts'o 		}
582a4912123STheodore Ts'o 		if (!retry && EXT4_I(parent)->i_last_alloc_group != ~0) {
583a4912123STheodore Ts'o 			retry = 1;
584a4912123STheodore Ts'o 			parent_group = EXT4_I(parent)->i_last_alloc_group;
585a4912123STheodore Ts'o 			goto try_again;
586a4912123STheodore Ts'o 		}
587a4912123STheodore Ts'o 		/*
588a4912123STheodore Ts'o 		 * If this didn't work, use the Orlov search algorithm
589a4912123STheodore Ts'o 		 * to find a new flex group; we pass in the mode to
590a4912123STheodore Ts'o 		 * avoid the topdir algorithms.
591a4912123STheodore Ts'o 		 */
592a4912123STheodore Ts'o 		*group = parent_group + flex_size;
593a4912123STheodore Ts'o 		if (*group > ngroups)
594a4912123STheodore Ts'o 			*group = 0;
5957dc57615SPeter Huewe 		return find_group_orlov(sb, parent, group, mode, NULL);
596a4912123STheodore Ts'o 	}
597ac27a0ecSDave Kleikamp 
598ac27a0ecSDave Kleikamp 	/*
599ac27a0ecSDave Kleikamp 	 * Try to place the inode in its parent directory
600ac27a0ecSDave Kleikamp 	 */
6012aa9fc4cSAvantika Mathur 	*group = parent_group;
6022aa9fc4cSAvantika Mathur 	desc = ext4_get_group_desc(sb, *group, NULL);
603560671a0SAneesh Kumar K.V 	if (desc && ext4_free_inodes_count(sb, desc) &&
604021b65bbSTheodore Ts'o 	    ext4_free_group_clusters(sb, desc))
6052aa9fc4cSAvantika Mathur 		return 0;
606ac27a0ecSDave Kleikamp 
607ac27a0ecSDave Kleikamp 	/*
608ac27a0ecSDave Kleikamp 	 * We're going to place this inode in a different blockgroup from its
609ac27a0ecSDave Kleikamp 	 * parent.  We want to cause files in a common directory to all land in
610ac27a0ecSDave Kleikamp 	 * the same blockgroup.  But we want files which are in a different
611ac27a0ecSDave Kleikamp 	 * directory which shares a blockgroup with our parent to land in a
612ac27a0ecSDave Kleikamp 	 * different blockgroup.
613ac27a0ecSDave Kleikamp 	 *
614ac27a0ecSDave Kleikamp 	 * So add our directory's i_ino into the starting point for the hash.
615ac27a0ecSDave Kleikamp 	 */
6162aa9fc4cSAvantika Mathur 	*group = (*group + parent->i_ino) % ngroups;
617ac27a0ecSDave Kleikamp 
618ac27a0ecSDave Kleikamp 	/*
619ac27a0ecSDave Kleikamp 	 * Use a quadratic hash to find a group with a free inode and some free
620ac27a0ecSDave Kleikamp 	 * blocks.
621ac27a0ecSDave Kleikamp 	 */
622ac27a0ecSDave Kleikamp 	for (i = 1; i < ngroups; i <<= 1) {
6232aa9fc4cSAvantika Mathur 		*group += i;
6242aa9fc4cSAvantika Mathur 		if (*group >= ngroups)
6252aa9fc4cSAvantika Mathur 			*group -= ngroups;
6262aa9fc4cSAvantika Mathur 		desc = ext4_get_group_desc(sb, *group, NULL);
627560671a0SAneesh Kumar K.V 		if (desc && ext4_free_inodes_count(sb, desc) &&
628021b65bbSTheodore Ts'o 		    ext4_free_group_clusters(sb, desc))
6292aa9fc4cSAvantika Mathur 			return 0;
630ac27a0ecSDave Kleikamp 	}
631ac27a0ecSDave Kleikamp 
632ac27a0ecSDave Kleikamp 	/*
633ac27a0ecSDave Kleikamp 	 * That failed: try linear search for a free inode, even if that group
634ac27a0ecSDave Kleikamp 	 * has no free blocks.
635ac27a0ecSDave Kleikamp 	 */
6362aa9fc4cSAvantika Mathur 	*group = parent_group;
637ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
6382aa9fc4cSAvantika Mathur 		if (++*group >= ngroups)
6392aa9fc4cSAvantika Mathur 			*group = 0;
6402aa9fc4cSAvantika Mathur 		desc = ext4_get_group_desc(sb, *group, NULL);
641560671a0SAneesh Kumar K.V 		if (desc && ext4_free_inodes_count(sb, desc))
6422aa9fc4cSAvantika Mathur 			return 0;
643ac27a0ecSDave Kleikamp 	}
644ac27a0ecSDave Kleikamp 
645ac27a0ecSDave Kleikamp 	return -1;
646ac27a0ecSDave Kleikamp }
647ac27a0ecSDave Kleikamp 
648ac27a0ecSDave Kleikamp /*
64919883bd9STheodore Ts'o  * In no journal mode, if an inode has recently been deleted, we want
65019883bd9STheodore Ts'o  * to avoid reusing it until we're reasonably sure the inode table
65119883bd9STheodore Ts'o  * block has been written back to disk.  (Yes, these values are
65219883bd9STheodore Ts'o  * somewhat arbitrary...)
65319883bd9STheodore Ts'o  */
65419883bd9STheodore Ts'o #define RECENTCY_MIN	5
655b5f51573SAndreas Dilger #define RECENTCY_DIRTY	300
65619883bd9STheodore Ts'o 
65719883bd9STheodore Ts'o static int recently_deleted(struct super_block *sb, ext4_group_t group, int ino)
65819883bd9STheodore Ts'o {
65919883bd9STheodore Ts'o 	struct ext4_group_desc	*gdp;
66019883bd9STheodore Ts'o 	struct ext4_inode	*raw_inode;
66119883bd9STheodore Ts'o 	struct buffer_head	*bh;
66219883bd9STheodore Ts'o 	int inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
663b5f51573SAndreas Dilger 	int offset, ret = 0;
664b5f51573SAndreas Dilger 	int recentcy = RECENTCY_MIN;
665b5f51573SAndreas Dilger 	u32 dtime, now;
66619883bd9STheodore Ts'o 
66719883bd9STheodore Ts'o 	gdp = ext4_get_group_desc(sb, group, NULL);
66819883bd9STheodore Ts'o 	if (unlikely(!gdp))
66919883bd9STheodore Ts'o 		return 0;
67019883bd9STheodore Ts'o 
6714f9d956dSJan Kara 	bh = sb_find_get_block(sb, ext4_inode_table(sb, gdp) +
67219883bd9STheodore Ts'o 		       (ino / inodes_per_block));
6734f9d956dSJan Kara 	if (!bh || !buffer_uptodate(bh))
67419883bd9STheodore Ts'o 		/*
67519883bd9STheodore Ts'o 		 * If the block is not in the buffer cache, then it
67619883bd9STheodore Ts'o 		 * must have been written out.
67719883bd9STheodore Ts'o 		 */
67819883bd9STheodore Ts'o 		goto out;
67919883bd9STheodore Ts'o 
68019883bd9STheodore Ts'o 	offset = (ino % inodes_per_block) * EXT4_INODE_SIZE(sb);
68119883bd9STheodore Ts'o 	raw_inode = (struct ext4_inode *) (bh->b_data + offset);
682b5f51573SAndreas Dilger 
683b5f51573SAndreas Dilger 	/* i_dtime is only 32 bits on disk, but we only care about relative
684b5f51573SAndreas Dilger 	 * times in the range of a few minutes (i.e. long enough to sync a
685b5f51573SAndreas Dilger 	 * recently-deleted inode to disk), so using the low 32 bits of the
686b5f51573SAndreas Dilger 	 * clock (a 68 year range) is enough, see time_before32() */
68719883bd9STheodore Ts'o 	dtime = le32_to_cpu(raw_inode->i_dtime);
688b5f51573SAndreas Dilger 	now = ktime_get_real_seconds();
68919883bd9STheodore Ts'o 	if (buffer_dirty(bh))
69019883bd9STheodore Ts'o 		recentcy += RECENTCY_DIRTY;
69119883bd9STheodore Ts'o 
692b5f51573SAndreas Dilger 	if (dtime && time_before32(dtime, now) &&
693b5f51573SAndreas Dilger 	    time_before32(now, dtime + recentcy))
69419883bd9STheodore Ts'o 		ret = 1;
69519883bd9STheodore Ts'o out:
69619883bd9STheodore Ts'o 	brelse(bh);
69719883bd9STheodore Ts'o 	return ret;
69819883bd9STheodore Ts'o }
69919883bd9STheodore Ts'o 
700901ed070SWang Shilong static int find_inode_bit(struct super_block *sb, ext4_group_t group,
701901ed070SWang Shilong 			  struct buffer_head *bitmap, unsigned long *ino)
702901ed070SWang Shilong {
703901ed070SWang Shilong next:
704901ed070SWang Shilong 	*ino = ext4_find_next_zero_bit((unsigned long *)
705901ed070SWang Shilong 				       bitmap->b_data,
706901ed070SWang Shilong 				       EXT4_INODES_PER_GROUP(sb), *ino);
707901ed070SWang Shilong 	if (*ino >= EXT4_INODES_PER_GROUP(sb))
708901ed070SWang Shilong 		return 0;
709901ed070SWang Shilong 
710901ed070SWang Shilong 	if ((EXT4_SB(sb)->s_journal == NULL) &&
711901ed070SWang Shilong 	    recently_deleted(sb, group, *ino)) {
712901ed070SWang Shilong 		*ino = *ino + 1;
713901ed070SWang Shilong 		if (*ino < EXT4_INODES_PER_GROUP(sb))
714901ed070SWang Shilong 			goto next;
715901ed070SWang Shilong 		return 0;
716901ed070SWang Shilong 	}
717901ed070SWang Shilong 
718901ed070SWang Shilong 	return 1;
719901ed070SWang Shilong }
720901ed070SWang Shilong 
72119883bd9STheodore Ts'o /*
722ac27a0ecSDave Kleikamp  * There are two policies for allocating an inode.  If the new inode is
723ac27a0ecSDave Kleikamp  * a directory, then a forward search is made for a block group with both
724ac27a0ecSDave Kleikamp  * free space and a low directory-to-inode ratio; if that fails, then of
725ac27a0ecSDave Kleikamp  * the groups with above-average free space, that group with the fewest
726ac27a0ecSDave Kleikamp  * directories already is chosen.
727ac27a0ecSDave Kleikamp  *
728ac27a0ecSDave Kleikamp  * For other inodes, search forward from the parent directory's block
729ac27a0ecSDave Kleikamp  * group to find a free inode.
730ac27a0ecSDave Kleikamp  */
7311139575aSTheodore Ts'o struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
7321139575aSTheodore Ts'o 			       umode_t mode, const struct qstr *qstr,
7331b917ed8STahsin Erdogan 			       __u32 goal, uid_t *owner, __u32 i_flags,
7341b917ed8STahsin Erdogan 			       int handle_type, unsigned int line_no,
7351b917ed8STahsin Erdogan 			       int nblocks)
736ac27a0ecSDave Kleikamp {
737ac27a0ecSDave Kleikamp 	struct super_block *sb;
7383300bedaSAneesh Kumar K.V 	struct buffer_head *inode_bitmap_bh = NULL;
7393300bedaSAneesh Kumar K.V 	struct buffer_head *group_desc_bh;
7408df9675fSTheodore Ts'o 	ext4_group_t ngroups, group = 0;
741ac27a0ecSDave Kleikamp 	unsigned long ino = 0;
742ac27a0ecSDave Kleikamp 	struct inode *inode;
743617ba13bSMingming Cao 	struct ext4_group_desc *gdp = NULL;
744617ba13bSMingming Cao 	struct ext4_inode_info *ei;
745617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
746a7cdadeeSJan Kara 	int ret2, err;
747ac27a0ecSDave Kleikamp 	struct inode *ret;
7482aa9fc4cSAvantika Mathur 	ext4_group_t i;
749772cb7c8SJose R. Santos 	ext4_group_t flex_group;
75087a39389SDarrick J. Wong 	struct ext4_group_info *grp;
751e709e9dfSTheodore Ts'o 	int encrypt = 0;
752ac27a0ecSDave Kleikamp 
753ac27a0ecSDave Kleikamp 	/* Cannot create files in a deleted directory */
754ac27a0ecSDave Kleikamp 	if (!dir || !dir->i_nlink)
755ac27a0ecSDave Kleikamp 		return ERR_PTR(-EPERM);
756ac27a0ecSDave Kleikamp 
757af65207cSTahsin Erdogan 	sb = dir->i_sb;
758af65207cSTahsin Erdogan 	sbi = EXT4_SB(sb);
759af65207cSTahsin Erdogan 
760af65207cSTahsin Erdogan 	if (unlikely(ext4_forced_shutdown(sbi)))
7610db1ff22STheodore Ts'o 		return ERR_PTR(-EIO);
7620db1ff22STheodore Ts'o 
763af65207cSTahsin Erdogan 	if ((ext4_encrypted_inode(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) &&
764ad47f953STahsin Erdogan 	    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) &&
765ad47f953STahsin Erdogan 	    !(i_flags & EXT4_EA_INODE_FL)) {
766a7550b30SJaegeuk Kim 		err = fscrypt_get_encryption_info(dir);
767e709e9dfSTheodore Ts'o 		if (err)
768e709e9dfSTheodore Ts'o 			return ERR_PTR(err);
769a7550b30SJaegeuk Kim 		if (!fscrypt_has_encryption_key(dir))
77054475f53SEric Biggers 			return ERR_PTR(-ENOKEY);
771e709e9dfSTheodore Ts'o 		encrypt = 1;
772e709e9dfSTheodore Ts'o 	}
773e709e9dfSTheodore Ts'o 
774af65207cSTahsin Erdogan 	if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) {
775af65207cSTahsin Erdogan #ifdef CONFIG_EXT4_FS_POSIX_ACL
776af65207cSTahsin Erdogan 		struct posix_acl *p = get_acl(dir, ACL_TYPE_DEFAULT);
777af65207cSTahsin Erdogan 
778996fc447STheodore Ts'o 		if (IS_ERR(p))
779996fc447STheodore Ts'o 			return ERR_CAST(p);
780af65207cSTahsin Erdogan 		if (p) {
781af65207cSTahsin Erdogan 			int acl_size = p->a_count * sizeof(ext4_acl_entry);
782af65207cSTahsin Erdogan 
783af65207cSTahsin Erdogan 			nblocks += (S_ISDIR(mode) ? 2 : 1) *
784af65207cSTahsin Erdogan 				__ext4_xattr_set_credits(sb, NULL /* inode */,
785af65207cSTahsin Erdogan 					NULL /* block_bh */, acl_size,
786af65207cSTahsin Erdogan 					true /* is_create */);
787af65207cSTahsin Erdogan 			posix_acl_release(p);
788af65207cSTahsin Erdogan 		}
789af65207cSTahsin Erdogan #endif
790af65207cSTahsin Erdogan 
791af65207cSTahsin Erdogan #ifdef CONFIG_SECURITY
792af65207cSTahsin Erdogan 		{
793af65207cSTahsin Erdogan 			int num_security_xattrs = 1;
794af65207cSTahsin Erdogan 
795af65207cSTahsin Erdogan #ifdef CONFIG_INTEGRITY
796af65207cSTahsin Erdogan 			num_security_xattrs++;
797af65207cSTahsin Erdogan #endif
798af65207cSTahsin Erdogan 			/*
799af65207cSTahsin Erdogan 			 * We assume that security xattrs are never
800af65207cSTahsin Erdogan 			 * more than 1k.  In practice they are under
801af65207cSTahsin Erdogan 			 * 128 bytes.
802af65207cSTahsin Erdogan 			 */
803af65207cSTahsin Erdogan 			nblocks += num_security_xattrs *
804af65207cSTahsin Erdogan 				__ext4_xattr_set_credits(sb, NULL /* inode */,
805af65207cSTahsin Erdogan 					NULL /* block_bh */, 1024,
806af65207cSTahsin Erdogan 					true /* is_create */);
807af65207cSTahsin Erdogan 		}
808af65207cSTahsin Erdogan #endif
809af65207cSTahsin Erdogan 		if (encrypt)
810af65207cSTahsin Erdogan 			nblocks += __ext4_xattr_set_credits(sb,
811af65207cSTahsin Erdogan 					NULL /* inode */, NULL /* block_bh */,
812af65207cSTahsin Erdogan 					FSCRYPT_SET_CONTEXT_MAX_SIZE,
813af65207cSTahsin Erdogan 					true /* is_create */);
814af65207cSTahsin Erdogan 	}
815af65207cSTahsin Erdogan 
8168df9675fSTheodore Ts'o 	ngroups = ext4_get_groups_count(sb);
8179bffad1eSTheodore Ts'o 	trace_ext4_request_inode(dir, mode);
818ac27a0ecSDave Kleikamp 	inode = new_inode(sb);
819ac27a0ecSDave Kleikamp 	if (!inode)
820ac27a0ecSDave Kleikamp 		return ERR_PTR(-ENOMEM);
821617ba13bSMingming Cao 	ei = EXT4_I(inode);
822772cb7c8SJose R. Santos 
823eb9cc7e1SJan Kara 	/*
824b8a07463SAdam Buchbinder 	 * Initialize owners and quota early so that we don't have to account
825eb9cc7e1SJan Kara 	 * for quota initialization worst case in standard inode creating
826eb9cc7e1SJan Kara 	 * transaction
827eb9cc7e1SJan Kara 	 */
828eb9cc7e1SJan Kara 	if (owner) {
829eb9cc7e1SJan Kara 		inode->i_mode = mode;
830eb9cc7e1SJan Kara 		i_uid_write(inode, owner[0]);
831eb9cc7e1SJan Kara 		i_gid_write(inode, owner[1]);
832eb9cc7e1SJan Kara 	} else if (test_opt(sb, GRPID)) {
833eb9cc7e1SJan Kara 		inode->i_mode = mode;
834eb9cc7e1SJan Kara 		inode->i_uid = current_fsuid();
835eb9cc7e1SJan Kara 		inode->i_gid = dir->i_gid;
836eb9cc7e1SJan Kara 	} else
837eb9cc7e1SJan Kara 		inode_init_owner(inode, dir, mode);
838040cb378SLi Xi 
8390b7b7779SKaho Ng 	if (ext4_has_feature_project(sb) &&
840040cb378SLi Xi 	    ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT))
841040cb378SLi Xi 		ei->i_projid = EXT4_I(dir)->i_projid;
842040cb378SLi Xi 	else
843040cb378SLi Xi 		ei->i_projid = make_kprojid(&init_user_ns, EXT4_DEF_PROJID);
844040cb378SLi Xi 
845a7cdadeeSJan Kara 	err = dquot_initialize(inode);
846a7cdadeeSJan Kara 	if (err)
847a7cdadeeSJan Kara 		goto out;
848eb9cc7e1SJan Kara 
84911013911SAndreas Dilger 	if (!goal)
85011013911SAndreas Dilger 		goal = sbi->s_inode_goal;
85111013911SAndreas Dilger 
852e6462869SJohann Lombardi 	if (goal && goal <= le32_to_cpu(sbi->s_es->s_inodes_count)) {
85311013911SAndreas Dilger 		group = (goal - 1) / EXT4_INODES_PER_GROUP(sb);
85411013911SAndreas Dilger 		ino = (goal - 1) % EXT4_INODES_PER_GROUP(sb);
85511013911SAndreas Dilger 		ret2 = 0;
85611013911SAndreas Dilger 		goto got_group;
85711013911SAndreas Dilger 	}
85811013911SAndreas Dilger 
8594113c4caSLukas Czerner 	if (S_ISDIR(mode))
860f157a4aaSTheodore Ts'o 		ret2 = find_group_orlov(sb, dir, &group, mode, qstr);
8614113c4caSLukas Czerner 	else
862a4912123STheodore Ts'o 		ret2 = find_group_other(sb, dir, &group, mode);
863ac27a0ecSDave Kleikamp 
864772cb7c8SJose R. Santos got_group:
865a4912123STheodore Ts'o 	EXT4_I(dir)->i_last_alloc_group = group;
866ac27a0ecSDave Kleikamp 	err = -ENOSPC;
8672aa9fc4cSAvantika Mathur 	if (ret2 == -1)
868ac27a0ecSDave Kleikamp 		goto out;
869ac27a0ecSDave Kleikamp 
870119c0d44STheodore Ts'o 	/*
871119c0d44STheodore Ts'o 	 * Normally we will only go through one pass of this loop,
872119c0d44STheodore Ts'o 	 * unless we get unlucky and it turns out the group we selected
873119c0d44STheodore Ts'o 	 * had its last inode grabbed by someone else.
874119c0d44STheodore Ts'o 	 */
87511013911SAndreas Dilger 	for (i = 0; i < ngroups; i++, ino = 0) {
876ac27a0ecSDave Kleikamp 		err = -EIO;
877ac27a0ecSDave Kleikamp 
8783300bedaSAneesh Kumar K.V 		gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
879ac27a0ecSDave Kleikamp 		if (!gdp)
880eb9cc7e1SJan Kara 			goto out;
881ac27a0ecSDave Kleikamp 
882f2a09af6SYongqiang Yang 		/*
883f2a09af6SYongqiang Yang 		 * Check free inodes count before loading bitmap.
884f2a09af6SYongqiang Yang 		 */
8852fe435d8SWang Shilong 		if (ext4_free_inodes_count(sb, gdp) == 0)
8862fe435d8SWang Shilong 			goto next_group;
887f2a09af6SYongqiang Yang 
88887a39389SDarrick J. Wong 		grp = ext4_get_group_info(sb, group);
88987a39389SDarrick J. Wong 		/* Skip groups with already-known suspicious inode tables */
8902fe435d8SWang Shilong 		if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp))
8912fe435d8SWang Shilong 			goto next_group;
89287a39389SDarrick J. Wong 
8933300bedaSAneesh Kumar K.V 		brelse(inode_bitmap_bh);
8943300bedaSAneesh Kumar K.V 		inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
89587a39389SDarrick J. Wong 		/* Skip groups with suspicious inode tables */
8969008a58eSDarrick J. Wong 		if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp) ||
8979008a58eSDarrick J. Wong 		    IS_ERR(inode_bitmap_bh)) {
8989008a58eSDarrick J. Wong 			inode_bitmap_bh = NULL;
8992fe435d8SWang Shilong 			goto next_group;
90087a39389SDarrick J. Wong 		}
901ac27a0ecSDave Kleikamp 
902ac27a0ecSDave Kleikamp repeat_in_this_group:
903901ed070SWang Shilong 		ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino);
904901ed070SWang Shilong 		if (!ret2)
905a34eb503STheodore Ts'o 			goto next_group;
906901ed070SWang Shilong 
907119c0d44STheodore Ts'o 		if (group == 0 && (ino + 1) < EXT4_FIRST_INO(sb)) {
908119c0d44STheodore Ts'o 			ext4_error(sb, "reserved inode found cleared - "
909119c0d44STheodore Ts'o 				   "inode=%lu", ino + 1);
9102fe435d8SWang Shilong 			goto next_group;
911119c0d44STheodore Ts'o 		}
912901ed070SWang Shilong 
9131139575aSTheodore Ts'o 		if (!handle) {
9141139575aSTheodore Ts'o 			BUG_ON(nblocks <= 0);
9151139575aSTheodore Ts'o 			handle = __ext4_journal_start_sb(dir->i_sb, line_no,
9165fe2fe89SJan Kara 							 handle_type, nblocks,
9175fe2fe89SJan Kara 							 0);
9181139575aSTheodore Ts'o 			if (IS_ERR(handle)) {
9191139575aSTheodore Ts'o 				err = PTR_ERR(handle);
920eb9cc7e1SJan Kara 				ext4_std_error(sb, err);
921eb9cc7e1SJan Kara 				goto out;
9221139575aSTheodore Ts'o 			}
9231139575aSTheodore Ts'o 		}
924ffb5387eSEric Sandeen 		BUFFER_TRACE(inode_bitmap_bh, "get_write_access");
925ffb5387eSEric Sandeen 		err = ext4_journal_get_write_access(handle, inode_bitmap_bh);
926eb9cc7e1SJan Kara 		if (err) {
927eb9cc7e1SJan Kara 			ext4_std_error(sb, err);
928eb9cc7e1SJan Kara 			goto out;
929eb9cc7e1SJan Kara 		}
930119c0d44STheodore Ts'o 		ext4_lock_group(sb, group);
931119c0d44STheodore Ts'o 		ret2 = ext4_test_and_set_bit(ino, inode_bitmap_bh->b_data);
932901ed070SWang Shilong 		if (ret2) {
933901ed070SWang Shilong 			/* Someone already took the bit. Repeat the search
934901ed070SWang Shilong 			 * with lock held.
935901ed070SWang Shilong 			 */
936901ed070SWang Shilong 			ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino);
937901ed070SWang Shilong 			if (ret2) {
938901ed070SWang Shilong 				ext4_set_bit(ino, inode_bitmap_bh->b_data);
939901ed070SWang Shilong 				ret2 = 0;
940901ed070SWang Shilong 			} else {
941901ed070SWang Shilong 				ret2 = 1; /* we didn't grab the inode */
942901ed070SWang Shilong 			}
943901ed070SWang Shilong 		}
944119c0d44STheodore Ts'o 		ext4_unlock_group(sb, group);
945119c0d44STheodore Ts'o 		ino++;		/* the inode bitmap is zero-based */
946119c0d44STheodore Ts'o 		if (!ret2)
947119c0d44STheodore Ts'o 			goto got; /* we grabbed the inode! */
948901ed070SWang Shilong 
949119c0d44STheodore Ts'o 		if (ino < EXT4_INODES_PER_GROUP(sb))
950119c0d44STheodore Ts'o 			goto repeat_in_this_group;
951a34eb503STheodore Ts'o next_group:
952a34eb503STheodore Ts'o 		if (++group == ngroups)
953a34eb503STheodore Ts'o 			group = 0;
954ac27a0ecSDave Kleikamp 	}
955ac27a0ecSDave Kleikamp 	err = -ENOSPC;
956ac27a0ecSDave Kleikamp 	goto out;
957ac27a0ecSDave Kleikamp 
958ac27a0ecSDave Kleikamp got:
959ffb5387eSEric Sandeen 	BUFFER_TRACE(inode_bitmap_bh, "call ext4_handle_dirty_metadata");
960ffb5387eSEric Sandeen 	err = ext4_handle_dirty_metadata(handle, NULL, inode_bitmap_bh);
961eb9cc7e1SJan Kara 	if (err) {
962eb9cc7e1SJan Kara 		ext4_std_error(sb, err);
963eb9cc7e1SJan Kara 		goto out;
964eb9cc7e1SJan Kara 	}
965ffb5387eSEric Sandeen 
96661c219f5STheodore Ts'o 	BUFFER_TRACE(group_desc_bh, "get_write_access");
96761c219f5STheodore Ts'o 	err = ext4_journal_get_write_access(handle, group_desc_bh);
96861c219f5STheodore Ts'o 	if (err) {
96961c219f5STheodore Ts'o 		ext4_std_error(sb, err);
97061c219f5STheodore Ts'o 		goto out;
97161c219f5STheodore Ts'o 	}
97261c219f5STheodore Ts'o 
973717d50e4SAndreas Dilger 	/* We may have to initialize the block bitmap if it isn't already */
974feb0ab32SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb) &&
975717d50e4SAndreas Dilger 	    gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
9763300bedaSAneesh Kumar K.V 		struct buffer_head *block_bitmap_bh;
977717d50e4SAndreas Dilger 
9783300bedaSAneesh Kumar K.V 		block_bitmap_bh = ext4_read_block_bitmap(sb, group);
9799008a58eSDarrick J. Wong 		if (IS_ERR(block_bitmap_bh)) {
9809008a58eSDarrick J. Wong 			err = PTR_ERR(block_bitmap_bh);
981599a9b77SJan Kara 			goto out;
982599a9b77SJan Kara 		}
9833300bedaSAneesh Kumar K.V 		BUFFER_TRACE(block_bitmap_bh, "get block bitmap access");
9843300bedaSAneesh Kumar K.V 		err = ext4_journal_get_write_access(handle, block_bitmap_bh);
985717d50e4SAndreas Dilger 		if (err) {
9863300bedaSAneesh Kumar K.V 			brelse(block_bitmap_bh);
987eb9cc7e1SJan Kara 			ext4_std_error(sb, err);
988eb9cc7e1SJan Kara 			goto out;
989717d50e4SAndreas Dilger 		}
990717d50e4SAndreas Dilger 
991fd034a84STheodore Ts'o 		BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
992fd034a84STheodore Ts'o 		err = ext4_handle_dirty_metadata(handle, NULL, block_bitmap_bh);
993fd034a84STheodore Ts'o 
994717d50e4SAndreas Dilger 		/* recheck and clear flag under lock if we still need to */
995fd034a84STheodore Ts'o 		ext4_lock_group(sb, group);
996717d50e4SAndreas Dilger 		if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
9973300bedaSAneesh Kumar K.V 			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
998021b65bbSTheodore Ts'o 			ext4_free_group_clusters_set(sb, gdp,
999cff1dfd7STheodore Ts'o 				ext4_free_clusters_after_init(sb, group, gdp));
1000fa77dcfaSDarrick J. Wong 			ext4_block_bitmap_csum_set(sb, group, gdp,
100179f1ba49STao Ma 						   block_bitmap_bh);
1002feb0ab32SDarrick J. Wong 			ext4_group_desc_csum_set(sb, group, gdp);
1003717d50e4SAndreas Dilger 		}
1004955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, group);
1005aeb1e5d6STheodore Ts'o 		brelse(block_bitmap_bh);
1006717d50e4SAndreas Dilger 
1007eb9cc7e1SJan Kara 		if (err) {
1008eb9cc7e1SJan Kara 			ext4_std_error(sb, err);
1009eb9cc7e1SJan Kara 			goto out;
1010eb9cc7e1SJan Kara 		}
1011717d50e4SAndreas Dilger 	}
1012119c0d44STheodore Ts'o 
1013119c0d44STheodore Ts'o 	/* Update the relevant bg descriptor fields */
101441a246d1SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb)) {
1015119c0d44STheodore Ts'o 		int free;
1016119c0d44STheodore Ts'o 		struct ext4_group_info *grp = ext4_get_group_info(sb, group);
1017119c0d44STheodore Ts'o 
1018119c0d44STheodore Ts'o 		down_read(&grp->alloc_sem); /* protect vs itable lazyinit */
1019119c0d44STheodore Ts'o 		ext4_lock_group(sb, group); /* while we modify the bg desc */
1020119c0d44STheodore Ts'o 		free = EXT4_INODES_PER_GROUP(sb) -
1021119c0d44STheodore Ts'o 			ext4_itable_unused_count(sb, gdp);
1022119c0d44STheodore Ts'o 		if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
1023119c0d44STheodore Ts'o 			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
1024119c0d44STheodore Ts'o 			free = 0;
1025119c0d44STheodore Ts'o 		}
1026119c0d44STheodore Ts'o 		/*
1027119c0d44STheodore Ts'o 		 * Check the relative inode number against the last used
1028119c0d44STheodore Ts'o 		 * relative inode number in this group. if it is greater
1029119c0d44STheodore Ts'o 		 * we need to update the bg_itable_unused count
1030119c0d44STheodore Ts'o 		 */
1031119c0d44STheodore Ts'o 		if (ino > free)
1032119c0d44STheodore Ts'o 			ext4_itable_unused_set(sb, gdp,
1033119c0d44STheodore Ts'o 					(EXT4_INODES_PER_GROUP(sb) - ino));
1034119c0d44STheodore Ts'o 		up_read(&grp->alloc_sem);
10356f2e9f0eSTao Ma 	} else {
10366f2e9f0eSTao Ma 		ext4_lock_group(sb, group);
1037119c0d44STheodore Ts'o 	}
10386f2e9f0eSTao Ma 
1039119c0d44STheodore Ts'o 	ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1);
1040119c0d44STheodore Ts'o 	if (S_ISDIR(mode)) {
1041119c0d44STheodore Ts'o 		ext4_used_dirs_set(sb, gdp, ext4_used_dirs_count(sb, gdp) + 1);
1042119c0d44STheodore Ts'o 		if (sbi->s_log_groups_per_flex) {
1043119c0d44STheodore Ts'o 			ext4_group_t f = ext4_flex_group(sbi, group);
1044119c0d44STheodore Ts'o 
1045119c0d44STheodore Ts'o 			atomic_inc(&sbi->s_flex_groups[f].used_dirs);
1046119c0d44STheodore Ts'o 		}
1047119c0d44STheodore Ts'o 	}
104841a246d1SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb)) {
104941a246d1SDarrick J. Wong 		ext4_inode_bitmap_csum_set(sb, group, gdp, inode_bitmap_bh,
105041a246d1SDarrick J. Wong 					   EXT4_INODES_PER_GROUP(sb) / 8);
1051feb0ab32SDarrick J. Wong 		ext4_group_desc_csum_set(sb, group, gdp);
1052119c0d44STheodore Ts'o 	}
10536f2e9f0eSTao Ma 	ext4_unlock_group(sb, group);
1054119c0d44STheodore Ts'o 
10553300bedaSAneesh Kumar K.V 	BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata");
10563300bedaSAneesh Kumar K.V 	err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh);
1057eb9cc7e1SJan Kara 	if (err) {
1058eb9cc7e1SJan Kara 		ext4_std_error(sb, err);
1059eb9cc7e1SJan Kara 		goto out;
1060eb9cc7e1SJan Kara 	}
1061ac27a0ecSDave Kleikamp 
1062ac27a0ecSDave Kleikamp 	percpu_counter_dec(&sbi->s_freeinodes_counter);
1063ac27a0ecSDave Kleikamp 	if (S_ISDIR(mode))
1064ac27a0ecSDave Kleikamp 		percpu_counter_inc(&sbi->s_dirs_counter);
1065ac27a0ecSDave Kleikamp 
1066772cb7c8SJose R. Santos 	if (sbi->s_log_groups_per_flex) {
1067772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, group);
10689f24e420STheodore Ts'o 		atomic_dec(&sbi->s_flex_groups[flex_group].free_inodes);
1069772cb7c8SJose R. Santos 	}
1070ac27a0ecSDave Kleikamp 
1071717d50e4SAndreas Dilger 	inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
1072ac27a0ecSDave Kleikamp 	/* This is the optimal IO size (for stat), not the fs block size */
1073ac27a0ecSDave Kleikamp 	inode->i_blocks = 0;
1074ef7f3835SKalpak Shah 	inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
1075eeca7ea1SDeepa Dinamani 						       current_time(inode);
1076ac27a0ecSDave Kleikamp 
1077ac27a0ecSDave Kleikamp 	memset(ei->i_data, 0, sizeof(ei->i_data));
1078ac27a0ecSDave Kleikamp 	ei->i_dir_start_lookup = 0;
1079ac27a0ecSDave Kleikamp 	ei->i_disksize = 0;
1080ac27a0ecSDave Kleikamp 
10814af83508SEryu Guan 	/* Don't inherit extent flag from directory, amongst others. */
10822dc6b0d4SDuane Griffin 	ei->i_flags =
10832dc6b0d4SDuane Griffin 		ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED);
10841b917ed8STahsin Erdogan 	ei->i_flags |= i_flags;
1085ac27a0ecSDave Kleikamp 	ei->i_file_acl = 0;
1086ac27a0ecSDave Kleikamp 	ei->i_dtime = 0;
1087ac27a0ecSDave Kleikamp 	ei->i_block_group = group;
1088a4912123STheodore Ts'o 	ei->i_last_alloc_group = ~0;
1089ac27a0ecSDave Kleikamp 
1090617ba13bSMingming Cao 	ext4_set_inode_flags(inode);
1091ac27a0ecSDave Kleikamp 	if (IS_DIRSYNC(inode))
10920390131bSFrank Mayhar 		ext4_handle_sync(handle);
10936b38e842SAl Viro 	if (insert_inode_locked(inode) < 0) {
1094acd6ad83SJan Kara 		/*
1095acd6ad83SJan Kara 		 * Likely a bitmap corruption causing inode to be allocated
1096acd6ad83SJan Kara 		 * twice.
1097acd6ad83SJan Kara 		 */
1098acd6ad83SJan Kara 		err = -EIO;
1099eb9cc7e1SJan Kara 		ext4_error(sb, "failed to insert inode %lu: doubly allocated?",
1100eb9cc7e1SJan Kara 			   inode->i_ino);
1101eb9cc7e1SJan Kara 		goto out;
11026b38e842SAl Viro 	}
110323253068STheodore Ts'o 	inode->i_generation = prandom_u32();
1104ac27a0ecSDave Kleikamp 
1105814525f4SDarrick J. Wong 	/* Precompute checksum seed for inode metadata */
11069aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sb)) {
1107814525f4SDarrick J. Wong 		__u32 csum;
1108814525f4SDarrick J. Wong 		__le32 inum = cpu_to_le32(inode->i_ino);
1109814525f4SDarrick J. Wong 		__le32 gen = cpu_to_le32(inode->i_generation);
1110814525f4SDarrick J. Wong 		csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
1111814525f4SDarrick J. Wong 				   sizeof(inum));
1112814525f4SDarrick J. Wong 		ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
1113814525f4SDarrick J. Wong 					      sizeof(gen));
1114814525f4SDarrick J. Wong 	}
1115814525f4SDarrick J. Wong 
1116353eb83cSTheodore Ts'o 	ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
111719f5fb7aSTheodore Ts'o 	ext4_set_inode_state(inode, EXT4_STATE_NEW);
1118ef7f3835SKalpak Shah 
111949598e04SJun Piao 	ei->i_extra_isize = sbi->s_want_extra_isize;
1120f08225d1STao Ma 	ei->i_inline_off = 0;
1121e2b911c5SDarrick J. Wong 	if (ext4_has_feature_inline_data(sb))
1122f08225d1STao Ma 		ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
1123ac27a0ecSDave Kleikamp 	ret = inode;
112463936ddaSChristoph Hellwig 	err = dquot_alloc_inode(inode);
112563936ddaSChristoph Hellwig 	if (err)
1126ac27a0ecSDave Kleikamp 		goto fail_drop;
1127ac27a0ecSDave Kleikamp 
1128aa1dca3bSEric Biggers 	/*
1129aa1dca3bSEric Biggers 	 * Since the encryption xattr will always be unique, create it first so
1130aa1dca3bSEric Biggers 	 * that it's less likely to end up in an external xattr block and
1131aa1dca3bSEric Biggers 	 * prevent its deduplication.
1132aa1dca3bSEric Biggers 	 */
1133aa1dca3bSEric Biggers 	if (encrypt) {
1134aa1dca3bSEric Biggers 		err = fscrypt_inherit_context(dir, inode, handle, true);
1135aa1dca3bSEric Biggers 		if (err)
1136aa1dca3bSEric Biggers 			goto fail_free_drop;
1137aa1dca3bSEric Biggers 	}
1138aa1dca3bSEric Biggers 
11391b917ed8STahsin Erdogan 	if (!(ei->i_flags & EXT4_EA_INODE_FL)) {
1140617ba13bSMingming Cao 		err = ext4_init_acl(handle, inode, dir);
1141ac27a0ecSDave Kleikamp 		if (err)
1142ac27a0ecSDave Kleikamp 			goto fail_free_drop;
1143ac27a0ecSDave Kleikamp 
11442a7dba39SEric Paris 		err = ext4_init_security(handle, inode, dir, qstr);
1145ac27a0ecSDave Kleikamp 		if (err)
1146ac27a0ecSDave Kleikamp 			goto fail_free_drop;
1147ad47f953STahsin Erdogan 	}
1148ac27a0ecSDave Kleikamp 
1149e2b911c5SDarrick J. Wong 	if (ext4_has_feature_extents(sb)) {
1150e4079a11SEric Sandeen 		/* set extent flag only for directory, file and normal symlink*/
1151e65187e6SAneesh Kumar K.V 		if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
115212e9b892SDmitry Monakhov 			ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
1153a86c6181SAlex Tomas 			ext4_ext_tree_init(handle, inode);
1154a86c6181SAlex Tomas 		}
115542bf0383SAneesh Kumar K.V 	}
1156ac27a0ecSDave Kleikamp 
1157688f869cSTheodore Ts'o 	if (ext4_handle_valid(handle)) {
1158688f869cSTheodore Ts'o 		ei->i_sync_tid = handle->h_transaction->t_tid;
1159688f869cSTheodore Ts'o 		ei->i_datasync_tid = handle->h_transaction->t_tid;
1160688f869cSTheodore Ts'o 	}
1161688f869cSTheodore Ts'o 
11628753e88fSAneesh Kumar K.V 	err = ext4_mark_inode_dirty(handle, inode);
11638753e88fSAneesh Kumar K.V 	if (err) {
11648753e88fSAneesh Kumar K.V 		ext4_std_error(sb, err);
11658753e88fSAneesh Kumar K.V 		goto fail_free_drop;
11668753e88fSAneesh Kumar K.V 	}
11678753e88fSAneesh Kumar K.V 
1168617ba13bSMingming Cao 	ext4_debug("allocating inode %lu\n", inode->i_ino);
11699bffad1eSTheodore Ts'o 	trace_ext4_allocate_inode(inode, dir, mode);
11703300bedaSAneesh Kumar K.V 	brelse(inode_bitmap_bh);
1171ac27a0ecSDave Kleikamp 	return ret;
1172ac27a0ecSDave Kleikamp 
1173ac27a0ecSDave Kleikamp fail_free_drop:
117463936ddaSChristoph Hellwig 	dquot_free_inode(inode);
1175ac27a0ecSDave Kleikamp fail_drop:
11766d6b77f1SMiklos Szeredi 	clear_nlink(inode);
11776b38e842SAl Viro 	unlock_new_inode(inode);
1178eb9cc7e1SJan Kara out:
1179eb9cc7e1SJan Kara 	dquot_drop(inode);
1180eb9cc7e1SJan Kara 	inode->i_flags |= S_NOQUOTA;
1181ac27a0ecSDave Kleikamp 	iput(inode);
11823300bedaSAneesh Kumar K.V 	brelse(inode_bitmap_bh);
1183ac27a0ecSDave Kleikamp 	return ERR_PTR(err);
1184ac27a0ecSDave Kleikamp }
1185ac27a0ecSDave Kleikamp 
1186ac27a0ecSDave Kleikamp /* Verify that we are loading a valid orphan from disk */
1187617ba13bSMingming Cao struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
1188ac27a0ecSDave Kleikamp {
1189617ba13bSMingming Cao 	unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
1190fd2d4291SAvantika Mathur 	ext4_group_t block_group;
1191ac27a0ecSDave Kleikamp 	int bit;
11927827a7f6STheodore Ts'o 	struct buffer_head *bitmap_bh = NULL;
1193ac27a0ecSDave Kleikamp 	struct inode *inode = NULL;
11947827a7f6STheodore Ts'o 	int err = -EFSCORRUPTED;
1195ac27a0ecSDave Kleikamp 
11967827a7f6STheodore Ts'o 	if (ino < EXT4_FIRST_INO(sb) || ino > max_ino)
11977827a7f6STheodore Ts'o 		goto bad_orphan;
1198ac27a0ecSDave Kleikamp 
1199617ba13bSMingming Cao 	block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
1200617ba13bSMingming Cao 	bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
1201e29d1cdeSEric Sandeen 	bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
12029008a58eSDarrick J. Wong 	if (IS_ERR(bitmap_bh)) {
12037827a7f6STheodore Ts'o 		ext4_error(sb, "inode bitmap error %ld for orphan %lu",
12047827a7f6STheodore Ts'o 			   ino, PTR_ERR(bitmap_bh));
12057827a7f6STheodore Ts'o 		return (struct inode *) bitmap_bh;
1206ac27a0ecSDave Kleikamp 	}
1207ac27a0ecSDave Kleikamp 
1208ac27a0ecSDave Kleikamp 	/* Having the inode bit set should be a 100% indicator that this
1209ac27a0ecSDave Kleikamp 	 * is a valid orphan (no e2fsck run on fs).  Orphans also include
1210ac27a0ecSDave Kleikamp 	 * inodes that were being truncated, so we can't check i_nlink==0.
1211ac27a0ecSDave Kleikamp 	 */
12121d1fe1eeSDavid Howells 	if (!ext4_test_bit(bit, bitmap_bh->b_data))
12131d1fe1eeSDavid Howells 		goto bad_orphan;
12141d1fe1eeSDavid Howells 
12151d1fe1eeSDavid Howells 	inode = ext4_iget(sb, ino);
12167827a7f6STheodore Ts'o 	if (IS_ERR(inode)) {
12177827a7f6STheodore Ts'o 		err = PTR_ERR(inode);
12187827a7f6STheodore Ts'o 		ext4_error(sb, "couldn't read orphan inode %lu (err %d)",
12197827a7f6STheodore Ts'o 			   ino, err);
12207827a7f6STheodore Ts'o 		return inode;
12217827a7f6STheodore Ts'o 	}
12221d1fe1eeSDavid Howells 
122391ef4cafSDuane Griffin 	/*
1224c9eb13a9STheodore Ts'o 	 * If the orphans has i_nlinks > 0 then it should be able to
1225c9eb13a9STheodore Ts'o 	 * be truncated, otherwise it won't be removed from the orphan
1226c9eb13a9STheodore Ts'o 	 * list during processing and an infinite loop will result.
1227c9eb13a9STheodore Ts'o 	 * Similarly, it must not be a bad inode.
122891ef4cafSDuane Griffin 	 */
1229c9eb13a9STheodore Ts'o 	if ((inode->i_nlink && !ext4_can_truncate(inode)) ||
1230c9eb13a9STheodore Ts'o 	    is_bad_inode(inode))
123191ef4cafSDuane Griffin 		goto bad_orphan;
123291ef4cafSDuane Griffin 
12331d1fe1eeSDavid Howells 	if (NEXT_ORPHAN(inode) > max_ino)
12341d1fe1eeSDavid Howells 		goto bad_orphan;
12351d1fe1eeSDavid Howells 	brelse(bitmap_bh);
12361d1fe1eeSDavid Howells 	return inode;
12371d1fe1eeSDavid Howells 
12381d1fe1eeSDavid Howells bad_orphan:
12397827a7f6STheodore Ts'o 	ext4_error(sb, "bad orphan inode %lu", ino);
12407827a7f6STheodore Ts'o 	if (bitmap_bh)
12417827a7f6STheodore Ts'o 		printk(KERN_ERR "ext4_test_bit(bit=%d, block=%llu) = %d\n",
1242ac27a0ecSDave Kleikamp 		       bit, (unsigned long long)bitmap_bh->b_blocknr,
1243617ba13bSMingming Cao 		       ext4_test_bit(bit, bitmap_bh->b_data));
1244ac27a0ecSDave Kleikamp 	if (inode) {
12457827a7f6STheodore Ts'o 		printk(KERN_ERR "is_bad_inode(inode)=%d\n",
1246ac27a0ecSDave Kleikamp 		       is_bad_inode(inode));
12477827a7f6STheodore Ts'o 		printk(KERN_ERR "NEXT_ORPHAN(inode)=%u\n",
1248ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
12497827a7f6STheodore Ts'o 		printk(KERN_ERR "max_ino=%lu\n", max_ino);
12507827a7f6STheodore Ts'o 		printk(KERN_ERR "i_nlink=%u\n", inode->i_nlink);
1251ac27a0ecSDave Kleikamp 		/* Avoid freeing blocks if we got a bad deleted inode */
12521d1fe1eeSDavid Howells 		if (inode->i_nlink == 0)
1253ac27a0ecSDave Kleikamp 			inode->i_blocks = 0;
1254ac27a0ecSDave Kleikamp 		iput(inode);
1255ac27a0ecSDave Kleikamp 	}
1256ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
12571d1fe1eeSDavid Howells 	return ERR_PTR(err);
1258ac27a0ecSDave Kleikamp }
1259ac27a0ecSDave Kleikamp 
1260617ba13bSMingming Cao unsigned long ext4_count_free_inodes(struct super_block *sb)
1261ac27a0ecSDave Kleikamp {
1262ac27a0ecSDave Kleikamp 	unsigned long desc_count;
1263617ba13bSMingming Cao 	struct ext4_group_desc *gdp;
12648df9675fSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
1265617ba13bSMingming Cao #ifdef EXT4FS_DEBUG
1266617ba13bSMingming Cao 	struct ext4_super_block *es;
1267ac27a0ecSDave Kleikamp 	unsigned long bitmap_count, x;
1268ac27a0ecSDave Kleikamp 	struct buffer_head *bitmap_bh = NULL;
1269ac27a0ecSDave Kleikamp 
1270617ba13bSMingming Cao 	es = EXT4_SB(sb)->s_es;
1271ac27a0ecSDave Kleikamp 	desc_count = 0;
1272ac27a0ecSDave Kleikamp 	bitmap_count = 0;
1273ac27a0ecSDave Kleikamp 	gdp = NULL;
12748df9675fSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
1275617ba13bSMingming Cao 		gdp = ext4_get_group_desc(sb, i, NULL);
1276ac27a0ecSDave Kleikamp 		if (!gdp)
1277ac27a0ecSDave Kleikamp 			continue;
1278560671a0SAneesh Kumar K.V 		desc_count += ext4_free_inodes_count(sb, gdp);
1279ac27a0ecSDave Kleikamp 		brelse(bitmap_bh);
1280e29d1cdeSEric Sandeen 		bitmap_bh = ext4_read_inode_bitmap(sb, i);
12819008a58eSDarrick J. Wong 		if (IS_ERR(bitmap_bh)) {
12829008a58eSDarrick J. Wong 			bitmap_bh = NULL;
1283ac27a0ecSDave Kleikamp 			continue;
12849008a58eSDarrick J. Wong 		}
1285ac27a0ecSDave Kleikamp 
1286f6fb99caSTheodore Ts'o 		x = ext4_count_free(bitmap_bh->b_data,
1287f6fb99caSTheodore Ts'o 				    EXT4_INODES_PER_GROUP(sb) / 8);
1288c549a95dSEric Sandeen 		printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
1289785b4b3aSPeng Tao 			(unsigned long) i, ext4_free_inodes_count(sb, gdp), x);
1290ac27a0ecSDave Kleikamp 		bitmap_count += x;
1291ac27a0ecSDave Kleikamp 	}
1292ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
12934776004fSTheodore Ts'o 	printk(KERN_DEBUG "ext4_count_free_inodes: "
12944776004fSTheodore Ts'o 	       "stored = %u, computed = %lu, %lu\n",
1295ac27a0ecSDave Kleikamp 	       le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
1296ac27a0ecSDave Kleikamp 	return desc_count;
1297ac27a0ecSDave Kleikamp #else
1298ac27a0ecSDave Kleikamp 	desc_count = 0;
12998df9675fSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
1300617ba13bSMingming Cao 		gdp = ext4_get_group_desc(sb, i, NULL);
1301ac27a0ecSDave Kleikamp 		if (!gdp)
1302ac27a0ecSDave Kleikamp 			continue;
1303560671a0SAneesh Kumar K.V 		desc_count += ext4_free_inodes_count(sb, gdp);
1304ac27a0ecSDave Kleikamp 		cond_resched();
1305ac27a0ecSDave Kleikamp 	}
1306ac27a0ecSDave Kleikamp 	return desc_count;
1307ac27a0ecSDave Kleikamp #endif
1308ac27a0ecSDave Kleikamp }
1309ac27a0ecSDave Kleikamp 
1310ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
1311617ba13bSMingming Cao unsigned long ext4_count_dirs(struct super_block * sb)
1312ac27a0ecSDave Kleikamp {
1313ac27a0ecSDave Kleikamp 	unsigned long count = 0;
13148df9675fSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
1315ac27a0ecSDave Kleikamp 
13168df9675fSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
1317617ba13bSMingming Cao 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1318ac27a0ecSDave Kleikamp 		if (!gdp)
1319ac27a0ecSDave Kleikamp 			continue;
1320560671a0SAneesh Kumar K.V 		count += ext4_used_dirs_count(sb, gdp);
1321ac27a0ecSDave Kleikamp 	}
1322ac27a0ecSDave Kleikamp 	return count;
1323ac27a0ecSDave Kleikamp }
1324bfff6873SLukas Czerner 
1325bfff6873SLukas Czerner /*
1326bfff6873SLukas Czerner  * Zeroes not yet zeroed inode table - just write zeroes through the whole
1327bfff6873SLukas Czerner  * inode table. Must be called without any spinlock held. The only place
1328bfff6873SLukas Czerner  * where it is called from on active part of filesystem is ext4lazyinit
1329bfff6873SLukas Czerner  * thread, so we do not need any special locks, however we have to prevent
1330bfff6873SLukas Czerner  * inode allocation from the current group, so we take alloc_sem lock, to
1331119c0d44STheodore Ts'o  * block ext4_new_inode() until we are finished.
1332bfff6873SLukas Czerner  */
1333e0cbee3eSH Hartley Sweeten int ext4_init_inode_table(struct super_block *sb, ext4_group_t group,
1334bfff6873SLukas Czerner 				 int barrier)
1335bfff6873SLukas Czerner {
1336bfff6873SLukas Czerner 	struct ext4_group_info *grp = ext4_get_group_info(sb, group);
1337bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1338bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
1339bfff6873SLukas Czerner 	struct buffer_head *group_desc_bh;
1340bfff6873SLukas Czerner 	handle_t *handle;
1341bfff6873SLukas Czerner 	ext4_fsblk_t blk;
1342bfff6873SLukas Czerner 	int num, ret = 0, used_blks = 0;
1343bfff6873SLukas Czerner 
1344bfff6873SLukas Czerner 	/* This should not happen, but just to be sure check this */
1345bc98a42cSDavid Howells 	if (sb_rdonly(sb)) {
1346bfff6873SLukas Czerner 		ret = 1;
1347bfff6873SLukas Czerner 		goto out;
1348bfff6873SLukas Czerner 	}
1349bfff6873SLukas Czerner 
1350bfff6873SLukas Czerner 	gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
1351bfff6873SLukas Czerner 	if (!gdp)
1352bfff6873SLukas Czerner 		goto out;
1353bfff6873SLukas Czerner 
1354bfff6873SLukas Czerner 	/*
1355bfff6873SLukas Czerner 	 * We do not need to lock this, because we are the only one
1356bfff6873SLukas Czerner 	 * handling this flag.
1357bfff6873SLukas Czerner 	 */
1358bfff6873SLukas Czerner 	if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED))
1359bfff6873SLukas Czerner 		goto out;
1360bfff6873SLukas Czerner 
13619924a92aSTheodore Ts'o 	handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
1362bfff6873SLukas Czerner 	if (IS_ERR(handle)) {
1363bfff6873SLukas Czerner 		ret = PTR_ERR(handle);
1364bfff6873SLukas Czerner 		goto out;
1365bfff6873SLukas Czerner 	}
1366bfff6873SLukas Czerner 
1367bfff6873SLukas Czerner 	down_write(&grp->alloc_sem);
1368bfff6873SLukas Czerner 	/*
1369bfff6873SLukas Czerner 	 * If inode bitmap was already initialized there may be some
1370bfff6873SLukas Czerner 	 * used inodes so we need to skip blocks with used inodes in
1371bfff6873SLukas Czerner 	 * inode table.
1372bfff6873SLukas Czerner 	 */
1373bfff6873SLukas Czerner 	if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)))
1374bfff6873SLukas Czerner 		used_blks = DIV_ROUND_UP((EXT4_INODES_PER_GROUP(sb) -
1375bfff6873SLukas Czerner 			    ext4_itable_unused_count(sb, gdp)),
1376bfff6873SLukas Czerner 			    sbi->s_inodes_per_block);
1377bfff6873SLukas Czerner 
1378857ac889SLukas Czerner 	if ((used_blks < 0) || (used_blks > sbi->s_itb_per_group)) {
13791084f252STheodore Ts'o 		ext4_error(sb, "Something is wrong with group %u: "
13801084f252STheodore Ts'o 			   "used itable blocks: %d; "
13811084f252STheodore Ts'o 			   "itable unused count: %u",
1382857ac889SLukas Czerner 			   group, used_blks,
1383857ac889SLukas Czerner 			   ext4_itable_unused_count(sb, gdp));
1384857ac889SLukas Czerner 		ret = 1;
138533853a0dSYongqiang Yang 		goto err_out;
1386857ac889SLukas Czerner 	}
1387857ac889SLukas Czerner 
1388bfff6873SLukas Czerner 	blk = ext4_inode_table(sb, gdp) + used_blks;
1389bfff6873SLukas Czerner 	num = sbi->s_itb_per_group - used_blks;
1390bfff6873SLukas Czerner 
1391bfff6873SLukas Czerner 	BUFFER_TRACE(group_desc_bh, "get_write_access");
1392bfff6873SLukas Czerner 	ret = ext4_journal_get_write_access(handle,
1393bfff6873SLukas Czerner 					    group_desc_bh);
1394bfff6873SLukas Czerner 	if (ret)
1395bfff6873SLukas Czerner 		goto err_out;
1396bfff6873SLukas Czerner 
1397bfff6873SLukas Czerner 	/*
1398bfff6873SLukas Czerner 	 * Skip zeroout if the inode table is full. But we set the ZEROED
1399bfff6873SLukas Czerner 	 * flag anyway, because obviously, when it is full it does not need
1400bfff6873SLukas Czerner 	 * further zeroing.
1401bfff6873SLukas Czerner 	 */
1402bfff6873SLukas Czerner 	if (unlikely(num == 0))
1403bfff6873SLukas Czerner 		goto skip_zeroout;
1404bfff6873SLukas Czerner 
1405bfff6873SLukas Czerner 	ext4_debug("going to zero out inode table in group %d\n",
1406bfff6873SLukas Czerner 		   group);
1407a107e5a3STheodore Ts'o 	ret = sb_issue_zeroout(sb, blk, num, GFP_NOFS);
1408bfff6873SLukas Czerner 	if (ret < 0)
1409bfff6873SLukas Czerner 		goto err_out;
1410a107e5a3STheodore Ts'o 	if (barrier)
1411a107e5a3STheodore Ts'o 		blkdev_issue_flush(sb->s_bdev, GFP_NOFS, NULL);
1412bfff6873SLukas Czerner 
1413bfff6873SLukas Czerner skip_zeroout:
1414bfff6873SLukas Czerner 	ext4_lock_group(sb, group);
1415bfff6873SLukas Czerner 	gdp->bg_flags |= cpu_to_le16(EXT4_BG_INODE_ZEROED);
1416feb0ab32SDarrick J. Wong 	ext4_group_desc_csum_set(sb, group, gdp);
1417bfff6873SLukas Czerner 	ext4_unlock_group(sb, group);
1418bfff6873SLukas Czerner 
1419bfff6873SLukas Czerner 	BUFFER_TRACE(group_desc_bh,
1420bfff6873SLukas Czerner 		     "call ext4_handle_dirty_metadata");
1421bfff6873SLukas Czerner 	ret = ext4_handle_dirty_metadata(handle, NULL,
1422bfff6873SLukas Czerner 					 group_desc_bh);
1423bfff6873SLukas Czerner 
1424bfff6873SLukas Czerner err_out:
1425bfff6873SLukas Czerner 	up_write(&grp->alloc_sem);
1426bfff6873SLukas Czerner 	ext4_journal_stop(handle);
1427bfff6873SLukas Czerner out:
1428bfff6873SLukas Czerner 	return ret;
1429bfff6873SLukas Czerner }
1430