xref: /linux/fs/ext4/ialloc.c (revision f2d40141d5d90b882e2c35b226f9244a63b82b6e)
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;
858016e29fSHarshad Shirwadkar 	struct ext4_group_info *grp;
868016e29fSHarshad Shirwadkar 
878016e29fSHarshad Shirwadkar 	if (EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY)
888016e29fSHarshad Shirwadkar 		return 0;
898016e29fSHarshad Shirwadkar 
908016e29fSHarshad Shirwadkar 	grp = ext4_get_group_info(sb, block_group);
919008a58eSDarrick J. Wong 
929008a58eSDarrick J. Wong 	if (buffer_verified(bh))
939008a58eSDarrick J. Wong 		return 0;
949008a58eSDarrick J. Wong 	if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp))
959008a58eSDarrick J. Wong 		return -EFSCORRUPTED;
969008a58eSDarrick J. Wong 
979008a58eSDarrick J. Wong 	ext4_lock_group(sb, block_group);
988d5a803cSTheodore Ts'o 	if (buffer_verified(bh))
998d5a803cSTheodore Ts'o 		goto verified;
1009008a58eSDarrick J. Wong 	blk = ext4_inode_bitmap(sb, desc);
1019008a58eSDarrick J. Wong 	if (!ext4_inode_bitmap_csum_verify(sb, block_group, desc, bh,
10246f870d6STheodore Ts'o 					   EXT4_INODES_PER_GROUP(sb) / 8) ||
10346f870d6STheodore Ts'o 	    ext4_simulate_fail(sb, EXT4_SIM_IBITMAP_CRC)) {
1049008a58eSDarrick J. Wong 		ext4_unlock_group(sb, block_group);
1059008a58eSDarrick J. Wong 		ext4_error(sb, "Corrupt inode bitmap - block_group = %u, "
1069008a58eSDarrick J. Wong 			   "inode_bitmap = %llu", block_group, blk);
107db79e6d1SWang Shilong 		ext4_mark_group_bitmap_corrupted(sb, block_group,
108db79e6d1SWang Shilong 					EXT4_GROUP_INFO_IBITMAP_CORRUPT);
1099008a58eSDarrick J. Wong 		return -EFSBADCRC;
1109008a58eSDarrick J. Wong 	}
1119008a58eSDarrick J. Wong 	set_buffer_verified(bh);
1128d5a803cSTheodore Ts'o verified:
1139008a58eSDarrick J. Wong 	ext4_unlock_group(sb, block_group);
1149008a58eSDarrick J. Wong 	return 0;
1159008a58eSDarrick J. Wong }
1169008a58eSDarrick J. Wong 
117ac27a0ecSDave Kleikamp /*
118ac27a0ecSDave Kleikamp  * Read the inode allocation bitmap for a given block_group, reading
119ac27a0ecSDave Kleikamp  * into the specified slot in the superblock's bitmap cache.
120ac27a0ecSDave Kleikamp  *
1219033783cSJosh Triplett  * Return buffer_head of bitmap on success, or an ERR_PTR on error.
122ac27a0ecSDave Kleikamp  */
123ac27a0ecSDave Kleikamp static struct buffer_head *
124e29d1cdeSEric Sandeen ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group)
125ac27a0ecSDave Kleikamp {
126617ba13bSMingming Cao 	struct ext4_group_desc *desc;
1277dac4a17STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
128ac27a0ecSDave Kleikamp 	struct buffer_head *bh = NULL;
129e29d1cdeSEric Sandeen 	ext4_fsblk_t bitmap_blk;
1309008a58eSDarrick J. Wong 	int err;
131ac27a0ecSDave Kleikamp 
132617ba13bSMingming Cao 	desc = ext4_get_group_desc(sb, block_group, NULL);
133ac27a0ecSDave Kleikamp 	if (!desc)
1349008a58eSDarrick J. Wong 		return ERR_PTR(-EFSCORRUPTED);
135bfff6873SLukas Czerner 
136e29d1cdeSEric Sandeen 	bitmap_blk = ext4_inode_bitmap(sb, desc);
1377dac4a17STheodore Ts'o 	if ((bitmap_blk <= le32_to_cpu(sbi->s_es->s_first_data_block)) ||
1387dac4a17STheodore Ts'o 	    (bitmap_blk >= ext4_blocks_count(sbi->s_es))) {
1397dac4a17STheodore Ts'o 		ext4_error(sb, "Invalid inode bitmap blk %llu in "
1407dac4a17STheodore Ts'o 			   "block_group %u", bitmap_blk, block_group);
141206f6d55SWang Shilong 		ext4_mark_group_bitmap_corrupted(sb, block_group,
142206f6d55SWang Shilong 					EXT4_GROUP_INFO_IBITMAP_CORRUPT);
1437dac4a17STheodore Ts'o 		return ERR_PTR(-EFSCORRUPTED);
1447dac4a17STheodore Ts'o 	}
145e29d1cdeSEric Sandeen 	bh = sb_getblk(sb, bitmap_blk);
146e29d1cdeSEric Sandeen 	if (unlikely(!bh)) {
1475ef2a699SWang Shilong 		ext4_warning(sb, "Cannot read inode bitmap - "
148a9df9a49STheodore Ts'o 			     "block_group = %u, inode_bitmap = %llu",
149e29d1cdeSEric Sandeen 			     block_group, bitmap_blk);
1500db9fdebSWang Shilong 		return ERR_PTR(-ENOMEM);
151e29d1cdeSEric Sandeen 	}
1522ccb5fb9SAneesh Kumar K.V 	if (bitmap_uptodate(bh))
15341a246d1SDarrick J. Wong 		goto verify;
154e29d1cdeSEric Sandeen 
155c806e68fSFrederic Bohe 	lock_buffer(bh);
1562ccb5fb9SAneesh Kumar K.V 	if (bitmap_uptodate(bh)) {
1572ccb5fb9SAneesh Kumar K.V 		unlock_buffer(bh);
15841a246d1SDarrick J. Wong 		goto verify;
1592ccb5fb9SAneesh Kumar K.V 	}
160bfff6873SLukas Czerner 
161955ce5f5SAneesh Kumar K.V 	ext4_lock_group(sb, block_group);
1628844618dSTheodore Ts'o 	if (ext4_has_group_desc_csum(sb) &&
1638844618dSTheodore Ts'o 	    (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT))) {
1648844618dSTheodore Ts'o 		if (block_group == 0) {
1658844618dSTheodore Ts'o 			ext4_unlock_group(sb, block_group);
1668844618dSTheodore Ts'o 			unlock_buffer(bh);
1678844618dSTheodore Ts'o 			ext4_error(sb, "Inode bitmap for bg 0 marked "
1688844618dSTheodore Ts'o 				   "uninitialized");
1698844618dSTheodore Ts'o 			err = -EFSCORRUPTED;
1708844618dSTheodore Ts'o 			goto out;
1718844618dSTheodore Ts'o 		}
172044e6e3dSTheodore Ts'o 		memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8);
173044e6e3dSTheodore Ts'o 		ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb),
174044e6e3dSTheodore Ts'o 				     sb->s_blocksize * 8, bh->b_data);
1752ccb5fb9SAneesh Kumar K.V 		set_bitmap_uptodate(bh);
176e29d1cdeSEric Sandeen 		set_buffer_uptodate(bh);
17741a246d1SDarrick J. Wong 		set_buffer_verified(bh);
178955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, block_group);
1793300bedaSAneesh Kumar K.V 		unlock_buffer(bh);
180e29d1cdeSEric Sandeen 		return bh;
181e29d1cdeSEric Sandeen 	}
182955ce5f5SAneesh Kumar K.V 	ext4_unlock_group(sb, block_group);
183bfff6873SLukas Czerner 
1842ccb5fb9SAneesh Kumar K.V 	if (buffer_uptodate(bh)) {
1852ccb5fb9SAneesh Kumar K.V 		/*
1862ccb5fb9SAneesh Kumar K.V 		 * if not uninit if bh is uptodate,
1872ccb5fb9SAneesh Kumar K.V 		 * bitmap is also uptodate
1882ccb5fb9SAneesh Kumar K.V 		 */
1892ccb5fb9SAneesh Kumar K.V 		set_bitmap_uptodate(bh);
1902ccb5fb9SAneesh Kumar K.V 		unlock_buffer(bh);
19141a246d1SDarrick J. Wong 		goto verify;
1922ccb5fb9SAneesh Kumar K.V 	}
1932ccb5fb9SAneesh Kumar K.V 	/*
194813e5727STheodore Ts'o 	 * submit the buffer_head for reading
1952ccb5fb9SAneesh Kumar K.V 	 */
1960562e0baSJiaying Zhang 	trace_ext4_load_inode_bitmap(sb, block_group);
1972d069c08Szhangyi (F) 	ext4_read_bh(bh, REQ_META | REQ_PRIO, ext4_end_bitmap_read);
19846f870d6STheodore Ts'o 	ext4_simulate_fail_bh(sb, bh, EXT4_SIM_IBITMAP_EIO);
199813e5727STheodore Ts'o 	if (!buffer_uptodate(bh)) {
200e29d1cdeSEric Sandeen 		put_bh(bh);
20154d3adbcSTheodore Ts'o 		ext4_error_err(sb, EIO, "Cannot read inode bitmap - "
202a9df9a49STheodore Ts'o 			       "block_group = %u, inode_bitmap = %llu",
203e29d1cdeSEric Sandeen 			       block_group, bitmap_blk);
204206f6d55SWang Shilong 		ext4_mark_group_bitmap_corrupted(sb, block_group,
205206f6d55SWang Shilong 				EXT4_GROUP_INFO_IBITMAP_CORRUPT);
2069008a58eSDarrick J. Wong 		return ERR_PTR(-EIO);
207e29d1cdeSEric Sandeen 	}
20841a246d1SDarrick J. Wong 
20941a246d1SDarrick J. Wong verify:
2109008a58eSDarrick J. Wong 	err = ext4_validate_inode_bitmap(sb, desc, block_group, bh);
2119008a58eSDarrick J. Wong 	if (err)
2129008a58eSDarrick J. Wong 		goto out;
213ac27a0ecSDave Kleikamp 	return bh;
2149008a58eSDarrick J. Wong out:
2159008a58eSDarrick J. Wong 	put_bh(bh);
2169008a58eSDarrick J. Wong 	return ERR_PTR(err);
217ac27a0ecSDave Kleikamp }
218ac27a0ecSDave Kleikamp 
219ac27a0ecSDave Kleikamp /*
220ac27a0ecSDave Kleikamp  * NOTE! When we get the inode, we're the only people
221ac27a0ecSDave Kleikamp  * that have access to it, and as such there are no
222ac27a0ecSDave Kleikamp  * race conditions we have to worry about. The inode
223ac27a0ecSDave Kleikamp  * is not on the hash-lists, and it cannot be reached
224ac27a0ecSDave Kleikamp  * through the filesystem because the directory entry
225ac27a0ecSDave Kleikamp  * has been deleted earlier.
226ac27a0ecSDave Kleikamp  *
227ac27a0ecSDave Kleikamp  * HOWEVER: we must make sure that we get no aliases,
228ac27a0ecSDave Kleikamp  * which means that we have to call "clear_inode()"
229ac27a0ecSDave Kleikamp  * _before_ we mark the inode not in use in the inode
230ac27a0ecSDave Kleikamp  * bitmaps. Otherwise a newly created file might use
231ac27a0ecSDave Kleikamp  * the same inode number (not actually the same pointer
232ac27a0ecSDave Kleikamp  * though), and then we'd have two inodes sharing the
233ac27a0ecSDave Kleikamp  * same inode number and space on the harddisk.
234ac27a0ecSDave Kleikamp  */
235617ba13bSMingming Cao void ext4_free_inode(handle_t *handle, struct inode *inode)
236ac27a0ecSDave Kleikamp {
237ac27a0ecSDave Kleikamp 	struct super_block *sb = inode->i_sb;
238ac27a0ecSDave Kleikamp 	int is_directory;
239ac27a0ecSDave Kleikamp 	unsigned long ino;
240ac27a0ecSDave Kleikamp 	struct buffer_head *bitmap_bh = NULL;
241ac27a0ecSDave Kleikamp 	struct buffer_head *bh2;
242fd2d4291SAvantika Mathur 	ext4_group_t block_group;
243ac27a0ecSDave Kleikamp 	unsigned long bit;
244617ba13bSMingming Cao 	struct ext4_group_desc *gdp;
245617ba13bSMingming Cao 	struct ext4_super_block *es;
246617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
2477ce9d5d1SEric Sandeen 	int fatal = 0, err, count, cleared;
24887a39389SDarrick J. Wong 	struct ext4_group_info *grp;
249ac27a0ecSDave Kleikamp 
25092b97816STheodore Ts'o 	if (!sb) {
25192b97816STheodore Ts'o 		printk(KERN_ERR "EXT4-fs: %s:%d: inode on "
25292b97816STheodore Ts'o 		       "nonexistent device\n", __func__, __LINE__);
25392b97816STheodore Ts'o 		return;
25492b97816STheodore Ts'o 	}
255ac27a0ecSDave Kleikamp 	if (atomic_read(&inode->i_count) > 1) {
25692b97816STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: count=%d",
25792b97816STheodore Ts'o 			 __func__, __LINE__, inode->i_ino,
258ac27a0ecSDave Kleikamp 			 atomic_read(&inode->i_count));
259ac27a0ecSDave Kleikamp 		return;
260ac27a0ecSDave Kleikamp 	}
261ac27a0ecSDave Kleikamp 	if (inode->i_nlink) {
26292b97816STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: nlink=%d\n",
26392b97816STheodore Ts'o 			 __func__, __LINE__, inode->i_ino, inode->i_nlink);
264ac27a0ecSDave Kleikamp 		return;
265ac27a0ecSDave Kleikamp 	}
266617ba13bSMingming Cao 	sbi = EXT4_SB(sb);
267ac27a0ecSDave Kleikamp 
268ac27a0ecSDave Kleikamp 	ino = inode->i_ino;
269617ba13bSMingming Cao 	ext4_debug("freeing inode %lu\n", ino);
2709bffad1eSTheodore Ts'o 	trace_ext4_free_inode(inode);
271ac27a0ecSDave Kleikamp 
272871a2931SChristoph Hellwig 	dquot_initialize(inode);
27363936ddaSChristoph Hellwig 	dquot_free_inode(inode);
274ac27a0ecSDave Kleikamp 
275ac27a0ecSDave Kleikamp 	is_directory = S_ISDIR(inode->i_mode);
276ac27a0ecSDave Kleikamp 
277ac27a0ecSDave Kleikamp 	/* Do this BEFORE marking the inode not in use or returning an error */
2780930fcc1SAl Viro 	ext4_clear_inode(inode);
279ac27a0ecSDave Kleikamp 
28049598e04SJun Piao 	es = sbi->s_es;
281617ba13bSMingming Cao 	if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
28212062dddSEric Sandeen 		ext4_error(sb, "reserved or nonexistent inode %lu", ino);
283ac27a0ecSDave Kleikamp 		goto error_return;
284ac27a0ecSDave Kleikamp 	}
285617ba13bSMingming Cao 	block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
286617ba13bSMingming Cao 	bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
287e29d1cdeSEric Sandeen 	bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
28887a39389SDarrick J. Wong 	/* Don't bother if the inode bitmap is corrupt. */
2899008a58eSDarrick J. Wong 	if (IS_ERR(bitmap_bh)) {
2909008a58eSDarrick J. Wong 		fatal = PTR_ERR(bitmap_bh);
2919008a58eSDarrick J. Wong 		bitmap_bh = NULL;
292ac27a0ecSDave Kleikamp 		goto error_return;
2939008a58eSDarrick J. Wong 	}
2948016e29fSHarshad Shirwadkar 	if (!(sbi->s_mount_state & EXT4_FC_REPLAY)) {
2958016e29fSHarshad Shirwadkar 		grp = ext4_get_group_info(sb, block_group);
2969008a58eSDarrick J. Wong 		if (unlikely(EXT4_MB_GRP_IBITMAP_CORRUPT(grp))) {
2979008a58eSDarrick J. Wong 			fatal = -EFSCORRUPTED;
2989008a58eSDarrick J. Wong 			goto error_return;
2999008a58eSDarrick J. Wong 		}
3008016e29fSHarshad Shirwadkar 	}
301ac27a0ecSDave Kleikamp 
302ac27a0ecSDave Kleikamp 	BUFFER_TRACE(bitmap_bh, "get_write_access");
303188c299eSJan Kara 	fatal = ext4_journal_get_write_access(handle, sb, bitmap_bh,
304188c299eSJan Kara 					      EXT4_JTR_NONE);
305ac27a0ecSDave Kleikamp 	if (fatal)
306ac27a0ecSDave Kleikamp 		goto error_return;
307ac27a0ecSDave Kleikamp 
308d17413c0SDmitry Monakhov 	fatal = -ESRCH;
309617ba13bSMingming Cao 	gdp = ext4_get_group_desc(sb, block_group, &bh2);
310d17413c0SDmitry Monakhov 	if (gdp) {
311ac27a0ecSDave Kleikamp 		BUFFER_TRACE(bh2, "get_write_access");
312188c299eSJan Kara 		fatal = ext4_journal_get_write_access(handle, sb, bh2,
313188c299eSJan Kara 						      EXT4_JTR_NONE);
314d17413c0SDmitry Monakhov 	}
315955ce5f5SAneesh Kumar K.V 	ext4_lock_group(sb, block_group);
316597d508cSAkinobu Mita 	cleared = ext4_test_and_clear_bit(bit, bitmap_bh->b_data);
317d17413c0SDmitry Monakhov 	if (fatal || !cleared) {
318d17413c0SDmitry Monakhov 		ext4_unlock_group(sb, block_group);
319d17413c0SDmitry Monakhov 		goto out;
320d17413c0SDmitry Monakhov 	}
321d17413c0SDmitry Monakhov 
322560671a0SAneesh Kumar K.V 	count = ext4_free_inodes_count(sb, gdp) + 1;
323560671a0SAneesh Kumar K.V 	ext4_free_inodes_set(sb, gdp, count);
324560671a0SAneesh Kumar K.V 	if (is_directory) {
325560671a0SAneesh Kumar K.V 		count = ext4_used_dirs_count(sb, gdp) - 1;
326560671a0SAneesh Kumar K.V 		ext4_used_dirs_set(sb, gdp, count);
327b45f189aSRitesh Harjani 		if (percpu_counter_initialized(&sbi->s_dirs_counter))
328d17413c0SDmitry Monakhov 			percpu_counter_dec(&sbi->s_dirs_counter);
329d17413c0SDmitry Monakhov 	}
33041a246d1SDarrick J. Wong 	ext4_inode_bitmap_csum_set(sb, block_group, gdp, bitmap_bh,
33141a246d1SDarrick J. Wong 				   EXT4_INODES_PER_GROUP(sb) / 8);
332feb0ab32SDarrick J. Wong 	ext4_group_desc_csum_set(sb, block_group, gdp);
333d17413c0SDmitry Monakhov 	ext4_unlock_group(sb, block_group);
3347d39db14STheodore Ts'o 
335b45f189aSRitesh Harjani 	if (percpu_counter_initialized(&sbi->s_freeinodes_counter))
336d17413c0SDmitry Monakhov 		percpu_counter_inc(&sbi->s_freeinodes_counter);
337d17413c0SDmitry Monakhov 	if (sbi->s_log_groups_per_flex) {
3387c990728SSuraj Jitindar Singh 		struct flex_groups *fg;
339d17413c0SDmitry Monakhov 
3407c990728SSuraj Jitindar Singh 		fg = sbi_array_rcu_deref(sbi, s_flex_groups,
3417c990728SSuraj Jitindar Singh 					 ext4_flex_group(sbi, block_group));
3427c990728SSuraj Jitindar Singh 		atomic_inc(&fg->free_inodes);
343d17413c0SDmitry Monakhov 		if (is_directory)
3447c990728SSuraj Jitindar Singh 			atomic_dec(&fg->used_dirs);
3457d39db14STheodore Ts'o 	}
3460390131bSFrank Mayhar 	BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata");
347d17413c0SDmitry Monakhov 	fatal = ext4_handle_dirty_metadata(handle, NULL, bh2);
348d17413c0SDmitry Monakhov out:
349d17413c0SDmitry Monakhov 	if (cleared) {
3500390131bSFrank Mayhar 		BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata");
3510390131bSFrank Mayhar 		err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
352ac27a0ecSDave Kleikamp 		if (!fatal)
353ac27a0ecSDave Kleikamp 			fatal = err;
35487a39389SDarrick J. Wong 	} else {
355d17413c0SDmitry Monakhov 		ext4_error(sb, "bit already cleared for inode %lu", ino);
356db79e6d1SWang Shilong 		ext4_mark_group_bitmap_corrupted(sb, block_group,
357db79e6d1SWang Shilong 					EXT4_GROUP_INFO_IBITMAP_CORRUPT);
35887a39389SDarrick J. Wong 	}
359d17413c0SDmitry Monakhov 
360ac27a0ecSDave Kleikamp error_return:
361ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
362617ba13bSMingming Cao 	ext4_std_error(sb, fatal);
363ac27a0ecSDave Kleikamp }
364ac27a0ecSDave Kleikamp 
365a4912123STheodore Ts'o struct orlov_stats {
36690ba983fSTheodore Ts'o 	__u64 free_clusters;
367a4912123STheodore Ts'o 	__u32 free_inodes;
368a4912123STheodore Ts'o 	__u32 used_dirs;
369a4912123STheodore Ts'o };
370a4912123STheodore Ts'o 
371a4912123STheodore Ts'o /*
372a4912123STheodore Ts'o  * Helper function for Orlov's allocator; returns critical information
373a4912123STheodore Ts'o  * for a particular block group or flex_bg.  If flex_size is 1, then g
374a4912123STheodore Ts'o  * is a block group number; otherwise it is flex_bg number.
375a4912123STheodore Ts'o  */
3761f109d5aSTheodore Ts'o static void get_orlov_stats(struct super_block *sb, ext4_group_t g,
377a4912123STheodore Ts'o 			    int flex_size, struct orlov_stats *stats)
378a4912123STheodore Ts'o {
379a4912123STheodore Ts'o 	struct ext4_group_desc *desc;
380a4912123STheodore Ts'o 
3817d39db14STheodore Ts'o 	if (flex_size > 1) {
3827c990728SSuraj Jitindar Singh 		struct flex_groups *fg = sbi_array_rcu_deref(EXT4_SB(sb),
3837c990728SSuraj Jitindar Singh 							     s_flex_groups, g);
3847c990728SSuraj Jitindar Singh 		stats->free_inodes = atomic_read(&fg->free_inodes);
3857c990728SSuraj Jitindar Singh 		stats->free_clusters = atomic64_read(&fg->free_clusters);
3867c990728SSuraj Jitindar Singh 		stats->used_dirs = atomic_read(&fg->used_dirs);
3877d39db14STheodore Ts'o 		return;
3887d39db14STheodore Ts'o 	}
3897d39db14STheodore Ts'o 
3907d39db14STheodore Ts'o 	desc = ext4_get_group_desc(sb, g, NULL);
3917d39db14STheodore Ts'o 	if (desc) {
3927d39db14STheodore Ts'o 		stats->free_inodes = ext4_free_inodes_count(sb, desc);
393021b65bbSTheodore Ts'o 		stats->free_clusters = ext4_free_group_clusters(sb, desc);
3947d39db14STheodore Ts'o 		stats->used_dirs = ext4_used_dirs_count(sb, desc);
3957d39db14STheodore Ts'o 	} else {
396a4912123STheodore Ts'o 		stats->free_inodes = 0;
39724aaa8efSTheodore Ts'o 		stats->free_clusters = 0;
398a4912123STheodore Ts'o 		stats->used_dirs = 0;
399a4912123STheodore Ts'o 	}
400a4912123STheodore Ts'o }
401a4912123STheodore Ts'o 
402ac27a0ecSDave Kleikamp /*
403ac27a0ecSDave Kleikamp  * Orlov's allocator for directories.
404ac27a0ecSDave Kleikamp  *
405ac27a0ecSDave Kleikamp  * We always try to spread first-level directories.
406ac27a0ecSDave Kleikamp  *
407c89849ccSPan Dong  * If there are blockgroups with both free inodes and free clusters counts
408ac27a0ecSDave Kleikamp  * not worse than average we return one with smallest directory count.
409ac27a0ecSDave Kleikamp  * Otherwise we simply return a random group.
410ac27a0ecSDave Kleikamp  *
411ac27a0ecSDave Kleikamp  * For the rest rules look so:
412ac27a0ecSDave Kleikamp  *
413ac27a0ecSDave Kleikamp  * It's OK to put directory into a group unless
414ac27a0ecSDave Kleikamp  * it has too many directories already (max_dirs) or
415ac27a0ecSDave Kleikamp  * it has too few free inodes left (min_inodes) or
416c89849ccSPan Dong  * it has too few free clusters left (min_clusters) or
4171cc8dcf5SBenoit Boissinot  * Parent's group is preferred, if it doesn't satisfy these
418ac27a0ecSDave Kleikamp  * conditions we search cyclically through the rest. If none
419ac27a0ecSDave Kleikamp  * of the groups look good we just look for a group with more
420ac27a0ecSDave Kleikamp  * free inodes than average (starting at parent's group).
421ac27a0ecSDave Kleikamp  */
422ac27a0ecSDave Kleikamp 
4232aa9fc4cSAvantika Mathur static int find_group_orlov(struct super_block *sb, struct inode *parent,
424dcca3fecSAl Viro 			    ext4_group_t *group, umode_t mode,
425f157a4aaSTheodore Ts'o 			    const struct qstr *qstr)
426ac27a0ecSDave Kleikamp {
427fd2d4291SAvantika Mathur 	ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
428617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4298df9675fSTheodore Ts'o 	ext4_group_t real_ngroups = ext4_get_groups_count(sb);
430617ba13bSMingming Cao 	int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
43114c83c9fSTheodore Ts'o 	unsigned int freei, avefreei, grp_free;
432c89849ccSPan Dong 	ext4_fsblk_t freec, avefreec;
433ac27a0ecSDave Kleikamp 	unsigned int ndirs;
434a4912123STheodore Ts'o 	int max_dirs, min_inodes;
43524aaa8efSTheodore Ts'o 	ext4_grpblk_t min_clusters;
4368df9675fSTheodore Ts'o 	ext4_group_t i, grp, g, ngroups;
437617ba13bSMingming Cao 	struct ext4_group_desc *desc;
438a4912123STheodore Ts'o 	struct orlov_stats stats;
439a4912123STheodore Ts'o 	int flex_size = ext4_flex_bg_size(sbi);
440f157a4aaSTheodore Ts'o 	struct dx_hash_info hinfo;
441a4912123STheodore Ts'o 
4428df9675fSTheodore Ts'o 	ngroups = real_ngroups;
443a4912123STheodore Ts'o 	if (flex_size > 1) {
4448df9675fSTheodore Ts'o 		ngroups = (real_ngroups + flex_size - 1) >>
445a4912123STheodore Ts'o 			sbi->s_log_groups_per_flex;
446a4912123STheodore Ts'o 		parent_group >>= sbi->s_log_groups_per_flex;
447a4912123STheodore Ts'o 	}
448ac27a0ecSDave Kleikamp 
449ac27a0ecSDave Kleikamp 	freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
450ac27a0ecSDave Kleikamp 	avefreei = freei / ngroups;
451c89849ccSPan Dong 	freec = percpu_counter_read_positive(&sbi->s_freeclusters_counter);
452c89849ccSPan Dong 	avefreec = freec;
45324aaa8efSTheodore Ts'o 	do_div(avefreec, ngroups);
454ac27a0ecSDave Kleikamp 	ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
455ac27a0ecSDave Kleikamp 
456a4912123STheodore Ts'o 	if (S_ISDIR(mode) &&
4572b0143b5SDavid Howells 	    ((parent == d_inode(sb->s_root)) ||
45812e9b892SDmitry Monakhov 	     (ext4_test_inode_flag(parent, EXT4_INODE_TOPDIR)))) {
459ac27a0ecSDave Kleikamp 		int best_ndir = inodes_per_group;
4602aa9fc4cSAvantika Mathur 		int ret = -1;
461ac27a0ecSDave Kleikamp 
462f157a4aaSTheodore Ts'o 		if (qstr) {
463f157a4aaSTheodore Ts'o 			hinfo.hash_version = DX_HASH_HALF_MD4;
464f157a4aaSTheodore Ts'o 			hinfo.seed = sbi->s_hash_seed;
465b886ee3eSGabriel Krisman Bertazi 			ext4fs_dirhash(parent, qstr->name, qstr->len, &hinfo);
4668b3ccbc1SJason A. Donenfeld 			parent_group = hinfo.hash % ngroups;
467f157a4aaSTheodore Ts'o 		} else
4688032bf12SJason A. Donenfeld 			parent_group = get_random_u32_below(ngroups);
469ac27a0ecSDave Kleikamp 		for (i = 0; i < ngroups; i++) {
470a4912123STheodore Ts'o 			g = (parent_group + i) % ngroups;
471a4912123STheodore Ts'o 			get_orlov_stats(sb, g, flex_size, &stats);
472a4912123STheodore Ts'o 			if (!stats.free_inodes)
473ac27a0ecSDave Kleikamp 				continue;
474a4912123STheodore Ts'o 			if (stats.used_dirs >= best_ndir)
475ac27a0ecSDave Kleikamp 				continue;
476a4912123STheodore Ts'o 			if (stats.free_inodes < avefreei)
477ac27a0ecSDave Kleikamp 				continue;
47824aaa8efSTheodore Ts'o 			if (stats.free_clusters < avefreec)
479ac27a0ecSDave Kleikamp 				continue;
480a4912123STheodore Ts'o 			grp = g;
4812aa9fc4cSAvantika Mathur 			ret = 0;
482a4912123STheodore Ts'o 			best_ndir = stats.used_dirs;
483ac27a0ecSDave Kleikamp 		}
484a4912123STheodore Ts'o 		if (ret)
485a4912123STheodore Ts'o 			goto fallback;
486a4912123STheodore Ts'o 	found_flex_bg:
487a4912123STheodore Ts'o 		if (flex_size == 1) {
488a4912123STheodore Ts'o 			*group = grp;
489a4912123STheodore Ts'o 			return 0;
490a4912123STheodore Ts'o 		}
491a4912123STheodore Ts'o 
492a4912123STheodore Ts'o 		/*
493a4912123STheodore Ts'o 		 * We pack inodes at the beginning of the flexgroup's
494a4912123STheodore Ts'o 		 * inode tables.  Block allocation decisions will do
495a4912123STheodore Ts'o 		 * something similar, although regular files will
496a4912123STheodore Ts'o 		 * start at 2nd block group of the flexgroup.  See
497a4912123STheodore Ts'o 		 * ext4_ext_find_goal() and ext4_find_near().
498a4912123STheodore Ts'o 		 */
499a4912123STheodore Ts'o 		grp *= flex_size;
500a4912123STheodore Ts'o 		for (i = 0; i < flex_size; i++) {
5018df9675fSTheodore Ts'o 			if (grp+i >= real_ngroups)
502a4912123STheodore Ts'o 				break;
503a4912123STheodore Ts'o 			desc = ext4_get_group_desc(sb, grp+i, NULL);
504a4912123STheodore Ts'o 			if (desc && ext4_free_inodes_count(sb, desc)) {
505a4912123STheodore Ts'o 				*group = grp+i;
506a4912123STheodore Ts'o 				return 0;
507a4912123STheodore Ts'o 			}
508a4912123STheodore Ts'o 		}
509ac27a0ecSDave Kleikamp 		goto fallback;
510ac27a0ecSDave Kleikamp 	}
511ac27a0ecSDave Kleikamp 
512613c5a85SJan Kara 	max_dirs = ndirs / ngroups + inodes_per_group*flex_size / 16;
513a4912123STheodore Ts'o 	min_inodes = avefreei - inodes_per_group*flex_size / 4;
514a4912123STheodore Ts'o 	if (min_inodes < 1)
515a4912123STheodore Ts'o 		min_inodes = 1;
51624aaa8efSTheodore Ts'o 	min_clusters = avefreec - EXT4_CLUSTERS_PER_GROUP(sb)*flex_size / 4;
517ac27a0ecSDave Kleikamp 
518a4912123STheodore Ts'o 	/*
519a4912123STheodore Ts'o 	 * Start looking in the flex group where we last allocated an
520a4912123STheodore Ts'o 	 * inode for this parent directory
521a4912123STheodore Ts'o 	 */
522a4912123STheodore Ts'o 	if (EXT4_I(parent)->i_last_alloc_group != ~0) {
523a4912123STheodore Ts'o 		parent_group = EXT4_I(parent)->i_last_alloc_group;
524a4912123STheodore Ts'o 		if (flex_size > 1)
525a4912123STheodore Ts'o 			parent_group >>= sbi->s_log_groups_per_flex;
526a4912123STheodore Ts'o 	}
527ac27a0ecSDave Kleikamp 
528ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
529a4912123STheodore Ts'o 		grp = (parent_group + i) % ngroups;
530a4912123STheodore Ts'o 		get_orlov_stats(sb, grp, flex_size, &stats);
531a4912123STheodore Ts'o 		if (stats.used_dirs >= max_dirs)
532ac27a0ecSDave Kleikamp 			continue;
533a4912123STheodore Ts'o 		if (stats.free_inodes < min_inodes)
534ac27a0ecSDave Kleikamp 			continue;
53524aaa8efSTheodore Ts'o 		if (stats.free_clusters < min_clusters)
536ac27a0ecSDave Kleikamp 			continue;
537a4912123STheodore Ts'o 		goto found_flex_bg;
538ac27a0ecSDave Kleikamp 	}
539ac27a0ecSDave Kleikamp 
540ac27a0ecSDave Kleikamp fallback:
5418df9675fSTheodore Ts'o 	ngroups = real_ngroups;
542a4912123STheodore Ts'o 	avefreei = freei / ngroups;
543b5451f7bSTheodore Ts'o fallback_retry:
544a4912123STheodore Ts'o 	parent_group = EXT4_I(parent)->i_block_group;
545ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
546a4912123STheodore Ts'o 		grp = (parent_group + i) % ngroups;
547a4912123STheodore Ts'o 		desc = ext4_get_group_desc(sb, grp, NULL);
548bb3d132aSDan Carpenter 		if (desc) {
54914c83c9fSTheodore Ts'o 			grp_free = ext4_free_inodes_count(sb, desc);
550bb3d132aSDan Carpenter 			if (grp_free && grp_free >= avefreei) {
551a4912123STheodore Ts'o 				*group = grp;
5522aa9fc4cSAvantika Mathur 				return 0;
553ac27a0ecSDave Kleikamp 			}
554a4912123STheodore Ts'o 		}
555bb3d132aSDan Carpenter 	}
556ac27a0ecSDave Kleikamp 
557ac27a0ecSDave Kleikamp 	if (avefreei) {
558ac27a0ecSDave Kleikamp 		/*
559ac27a0ecSDave Kleikamp 		 * The free-inodes counter is approximate, and for really small
560ac27a0ecSDave Kleikamp 		 * filesystems the above test can fail to find any blockgroups
561ac27a0ecSDave Kleikamp 		 */
562ac27a0ecSDave Kleikamp 		avefreei = 0;
563b5451f7bSTheodore Ts'o 		goto fallback_retry;
564ac27a0ecSDave Kleikamp 	}
565ac27a0ecSDave Kleikamp 
566ac27a0ecSDave Kleikamp 	return -1;
567ac27a0ecSDave Kleikamp }
568ac27a0ecSDave Kleikamp 
5692aa9fc4cSAvantika Mathur static int find_group_other(struct super_block *sb, struct inode *parent,
570dcca3fecSAl Viro 			    ext4_group_t *group, umode_t mode)
571ac27a0ecSDave Kleikamp {
572fd2d4291SAvantika Mathur 	ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
5738df9675fSTheodore Ts'o 	ext4_group_t i, last, ngroups = ext4_get_groups_count(sb);
574617ba13bSMingming Cao 	struct ext4_group_desc *desc;
575a4912123STheodore Ts'o 	int flex_size = ext4_flex_bg_size(EXT4_SB(sb));
576a4912123STheodore Ts'o 
577a4912123STheodore Ts'o 	/*
578a4912123STheodore Ts'o 	 * Try to place the inode is the same flex group as its
579a4912123STheodore Ts'o 	 * parent.  If we can't find space, use the Orlov algorithm to
580a4912123STheodore Ts'o 	 * find another flex group, and store that information in the
581a4912123STheodore Ts'o 	 * parent directory's inode information so that use that flex
582a4912123STheodore Ts'o 	 * group for future allocations.
583a4912123STheodore Ts'o 	 */
584a4912123STheodore Ts'o 	if (flex_size > 1) {
585a4912123STheodore Ts'o 		int retry = 0;
586a4912123STheodore Ts'o 
587a4912123STheodore Ts'o 	try_again:
588a4912123STheodore Ts'o 		parent_group &= ~(flex_size-1);
589a4912123STheodore Ts'o 		last = parent_group + flex_size;
590a4912123STheodore Ts'o 		if (last > ngroups)
591a4912123STheodore Ts'o 			last = ngroups;
592a4912123STheodore Ts'o 		for  (i = parent_group; i < last; i++) {
593a4912123STheodore Ts'o 			desc = ext4_get_group_desc(sb, i, NULL);
594a4912123STheodore Ts'o 			if (desc && ext4_free_inodes_count(sb, desc)) {
595a4912123STheodore Ts'o 				*group = i;
596a4912123STheodore Ts'o 				return 0;
597a4912123STheodore Ts'o 			}
598a4912123STheodore Ts'o 		}
599a4912123STheodore Ts'o 		if (!retry && EXT4_I(parent)->i_last_alloc_group != ~0) {
600a4912123STheodore Ts'o 			retry = 1;
601a4912123STheodore Ts'o 			parent_group = EXT4_I(parent)->i_last_alloc_group;
602a4912123STheodore Ts'o 			goto try_again;
603a4912123STheodore Ts'o 		}
604a4912123STheodore Ts'o 		/*
605a4912123STheodore Ts'o 		 * If this didn't work, use the Orlov search algorithm
606a4912123STheodore Ts'o 		 * to find a new flex group; we pass in the mode to
607a4912123STheodore Ts'o 		 * avoid the topdir algorithms.
608a4912123STheodore Ts'o 		 */
609a4912123STheodore Ts'o 		*group = parent_group + flex_size;
610a4912123STheodore Ts'o 		if (*group > ngroups)
611a4912123STheodore Ts'o 			*group = 0;
6127dc57615SPeter Huewe 		return find_group_orlov(sb, parent, group, mode, NULL);
613a4912123STheodore Ts'o 	}
614ac27a0ecSDave Kleikamp 
615ac27a0ecSDave Kleikamp 	/*
616ac27a0ecSDave Kleikamp 	 * Try to place the inode in its parent directory
617ac27a0ecSDave Kleikamp 	 */
6182aa9fc4cSAvantika Mathur 	*group = parent_group;
6192aa9fc4cSAvantika Mathur 	desc = ext4_get_group_desc(sb, *group, NULL);
620560671a0SAneesh Kumar K.V 	if (desc && ext4_free_inodes_count(sb, desc) &&
621021b65bbSTheodore Ts'o 	    ext4_free_group_clusters(sb, desc))
6222aa9fc4cSAvantika Mathur 		return 0;
623ac27a0ecSDave Kleikamp 
624ac27a0ecSDave Kleikamp 	/*
625ac27a0ecSDave Kleikamp 	 * We're going to place this inode in a different blockgroup from its
626ac27a0ecSDave Kleikamp 	 * parent.  We want to cause files in a common directory to all land in
627ac27a0ecSDave Kleikamp 	 * the same blockgroup.  But we want files which are in a different
628ac27a0ecSDave Kleikamp 	 * directory which shares a blockgroup with our parent to land in a
629ac27a0ecSDave Kleikamp 	 * different blockgroup.
630ac27a0ecSDave Kleikamp 	 *
631ac27a0ecSDave Kleikamp 	 * So add our directory's i_ino into the starting point for the hash.
632ac27a0ecSDave Kleikamp 	 */
6332aa9fc4cSAvantika Mathur 	*group = (*group + parent->i_ino) % ngroups;
634ac27a0ecSDave Kleikamp 
635ac27a0ecSDave Kleikamp 	/*
636ac27a0ecSDave Kleikamp 	 * Use a quadratic hash to find a group with a free inode and some free
637ac27a0ecSDave Kleikamp 	 * blocks.
638ac27a0ecSDave Kleikamp 	 */
639ac27a0ecSDave Kleikamp 	for (i = 1; i < ngroups; i <<= 1) {
6402aa9fc4cSAvantika Mathur 		*group += i;
6412aa9fc4cSAvantika Mathur 		if (*group >= ngroups)
6422aa9fc4cSAvantika Mathur 			*group -= ngroups;
6432aa9fc4cSAvantika Mathur 		desc = ext4_get_group_desc(sb, *group, NULL);
644560671a0SAneesh Kumar K.V 		if (desc && ext4_free_inodes_count(sb, desc) &&
645021b65bbSTheodore Ts'o 		    ext4_free_group_clusters(sb, desc))
6462aa9fc4cSAvantika Mathur 			return 0;
647ac27a0ecSDave Kleikamp 	}
648ac27a0ecSDave Kleikamp 
649ac27a0ecSDave Kleikamp 	/*
650ac27a0ecSDave Kleikamp 	 * That failed: try linear search for a free inode, even if that group
651ac27a0ecSDave Kleikamp 	 * has no free blocks.
652ac27a0ecSDave Kleikamp 	 */
6532aa9fc4cSAvantika Mathur 	*group = parent_group;
654ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
6552aa9fc4cSAvantika Mathur 		if (++*group >= ngroups)
6562aa9fc4cSAvantika Mathur 			*group = 0;
6572aa9fc4cSAvantika Mathur 		desc = ext4_get_group_desc(sb, *group, NULL);
658560671a0SAneesh Kumar K.V 		if (desc && ext4_free_inodes_count(sb, desc))
6592aa9fc4cSAvantika Mathur 			return 0;
660ac27a0ecSDave Kleikamp 	}
661ac27a0ecSDave Kleikamp 
662ac27a0ecSDave Kleikamp 	return -1;
663ac27a0ecSDave Kleikamp }
664ac27a0ecSDave Kleikamp 
665ac27a0ecSDave Kleikamp /*
66619883bd9STheodore Ts'o  * In no journal mode, if an inode has recently been deleted, we want
66719883bd9STheodore Ts'o  * to avoid reusing it until we're reasonably sure the inode table
66819883bd9STheodore Ts'o  * block has been written back to disk.  (Yes, these values are
66919883bd9STheodore Ts'o  * somewhat arbitrary...)
67019883bd9STheodore Ts'o  */
671a17a9d93STheodore Ts'o #define RECENTCY_MIN	60
672b5f51573SAndreas Dilger #define RECENTCY_DIRTY	300
67319883bd9STheodore Ts'o 
67419883bd9STheodore Ts'o static int recently_deleted(struct super_block *sb, ext4_group_t group, int ino)
67519883bd9STheodore Ts'o {
67619883bd9STheodore Ts'o 	struct ext4_group_desc	*gdp;
67719883bd9STheodore Ts'o 	struct ext4_inode	*raw_inode;
67819883bd9STheodore Ts'o 	struct buffer_head	*bh;
67919883bd9STheodore Ts'o 	int inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
680b5f51573SAndreas Dilger 	int offset, ret = 0;
681b5f51573SAndreas Dilger 	int recentcy = RECENTCY_MIN;
682b5f51573SAndreas Dilger 	u32 dtime, now;
68319883bd9STheodore Ts'o 
68419883bd9STheodore Ts'o 	gdp = ext4_get_group_desc(sb, group, NULL);
68519883bd9STheodore Ts'o 	if (unlikely(!gdp))
68619883bd9STheodore Ts'o 		return 0;
68719883bd9STheodore Ts'o 
6884f9d956dSJan Kara 	bh = sb_find_get_block(sb, ext4_inode_table(sb, gdp) +
68919883bd9STheodore Ts'o 		       (ino / inodes_per_block));
6904f9d956dSJan Kara 	if (!bh || !buffer_uptodate(bh))
69119883bd9STheodore Ts'o 		/*
69219883bd9STheodore Ts'o 		 * If the block is not in the buffer cache, then it
69319883bd9STheodore Ts'o 		 * must have been written out.
69419883bd9STheodore Ts'o 		 */
69519883bd9STheodore Ts'o 		goto out;
69619883bd9STheodore Ts'o 
69719883bd9STheodore Ts'o 	offset = (ino % inodes_per_block) * EXT4_INODE_SIZE(sb);
69819883bd9STheodore Ts'o 	raw_inode = (struct ext4_inode *) (bh->b_data + offset);
699b5f51573SAndreas Dilger 
700b5f51573SAndreas Dilger 	/* i_dtime is only 32 bits on disk, but we only care about relative
701b5f51573SAndreas Dilger 	 * times in the range of a few minutes (i.e. long enough to sync a
702b5f51573SAndreas Dilger 	 * recently-deleted inode to disk), so using the low 32 bits of the
703b5f51573SAndreas Dilger 	 * clock (a 68 year range) is enough, see time_before32() */
70419883bd9STheodore Ts'o 	dtime = le32_to_cpu(raw_inode->i_dtime);
705b5f51573SAndreas Dilger 	now = ktime_get_real_seconds();
70619883bd9STheodore Ts'o 	if (buffer_dirty(bh))
70719883bd9STheodore Ts'o 		recentcy += RECENTCY_DIRTY;
70819883bd9STheodore Ts'o 
709b5f51573SAndreas Dilger 	if (dtime && time_before32(dtime, now) &&
710b5f51573SAndreas Dilger 	    time_before32(now, dtime + recentcy))
71119883bd9STheodore Ts'o 		ret = 1;
71219883bd9STheodore Ts'o out:
71319883bd9STheodore Ts'o 	brelse(bh);
71419883bd9STheodore Ts'o 	return ret;
71519883bd9STheodore Ts'o }
71619883bd9STheodore Ts'o 
717901ed070SWang Shilong static int find_inode_bit(struct super_block *sb, ext4_group_t group,
718901ed070SWang Shilong 			  struct buffer_head *bitmap, unsigned long *ino)
719901ed070SWang Shilong {
720d05466b2SJan Kara 	bool check_recently_deleted = EXT4_SB(sb)->s_journal == NULL;
721d05466b2SJan Kara 	unsigned long recently_deleted_ino = EXT4_INODES_PER_GROUP(sb);
722d05466b2SJan Kara 
723901ed070SWang Shilong next:
724901ed070SWang Shilong 	*ino = ext4_find_next_zero_bit((unsigned long *)
725901ed070SWang Shilong 				       bitmap->b_data,
726901ed070SWang Shilong 				       EXT4_INODES_PER_GROUP(sb), *ino);
727901ed070SWang Shilong 	if (*ino >= EXT4_INODES_PER_GROUP(sb))
728d05466b2SJan Kara 		goto not_found;
729901ed070SWang Shilong 
730d05466b2SJan Kara 	if (check_recently_deleted && recently_deleted(sb, group, *ino)) {
731d05466b2SJan Kara 		recently_deleted_ino = *ino;
732901ed070SWang Shilong 		*ino = *ino + 1;
733901ed070SWang Shilong 		if (*ino < EXT4_INODES_PER_GROUP(sb))
734901ed070SWang Shilong 			goto next;
735d05466b2SJan Kara 		goto not_found;
736901ed070SWang Shilong 	}
737d05466b2SJan Kara 	return 1;
738d05466b2SJan Kara not_found:
739d05466b2SJan Kara 	if (recently_deleted_ino >= EXT4_INODES_PER_GROUP(sb))
740d05466b2SJan Kara 		return 0;
741d05466b2SJan Kara 	/*
742d05466b2SJan Kara 	 * Not reusing recently deleted inodes is mostly a preference. We don't
743d05466b2SJan Kara 	 * want to report ENOSPC or skew allocation patterns because of that.
744d05466b2SJan Kara 	 * So return even recently deleted inode if we could find better in the
745d05466b2SJan Kara 	 * given range.
746d05466b2SJan Kara 	 */
747d05466b2SJan Kara 	*ino = recently_deleted_ino;
748901ed070SWang Shilong 	return 1;
749901ed070SWang Shilong }
750901ed070SWang Shilong 
7518016e29fSHarshad Shirwadkar int ext4_mark_inode_used(struct super_block *sb, int ino)
7528016e29fSHarshad Shirwadkar {
7538016e29fSHarshad Shirwadkar 	unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
7548016e29fSHarshad Shirwadkar 	struct buffer_head *inode_bitmap_bh = NULL, *group_desc_bh = NULL;
7558016e29fSHarshad Shirwadkar 	struct ext4_group_desc *gdp;
7568016e29fSHarshad Shirwadkar 	ext4_group_t group;
7578016e29fSHarshad Shirwadkar 	int bit;
7588016e29fSHarshad Shirwadkar 	int err = -EFSCORRUPTED;
7598016e29fSHarshad Shirwadkar 
7608016e29fSHarshad Shirwadkar 	if (ino < EXT4_FIRST_INO(sb) || ino > max_ino)
7618016e29fSHarshad Shirwadkar 		goto out;
7628016e29fSHarshad Shirwadkar 
7638016e29fSHarshad Shirwadkar 	group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
7648016e29fSHarshad Shirwadkar 	bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
7658016e29fSHarshad Shirwadkar 	inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
7668016e29fSHarshad Shirwadkar 	if (IS_ERR(inode_bitmap_bh))
7678016e29fSHarshad Shirwadkar 		return PTR_ERR(inode_bitmap_bh);
7688016e29fSHarshad Shirwadkar 
7698016e29fSHarshad Shirwadkar 	if (ext4_test_bit(bit, inode_bitmap_bh->b_data)) {
7708016e29fSHarshad Shirwadkar 		err = 0;
7718016e29fSHarshad Shirwadkar 		goto out;
7728016e29fSHarshad Shirwadkar 	}
7738016e29fSHarshad Shirwadkar 
7748016e29fSHarshad Shirwadkar 	gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
7758016e29fSHarshad Shirwadkar 	if (!gdp || !group_desc_bh) {
7768016e29fSHarshad Shirwadkar 		err = -EINVAL;
7778016e29fSHarshad Shirwadkar 		goto out;
7788016e29fSHarshad Shirwadkar 	}
7798016e29fSHarshad Shirwadkar 
7808016e29fSHarshad Shirwadkar 	ext4_set_bit(bit, inode_bitmap_bh->b_data);
7818016e29fSHarshad Shirwadkar 
7828016e29fSHarshad Shirwadkar 	BUFFER_TRACE(inode_bitmap_bh, "call ext4_handle_dirty_metadata");
7838016e29fSHarshad Shirwadkar 	err = ext4_handle_dirty_metadata(NULL, NULL, inode_bitmap_bh);
7848016e29fSHarshad Shirwadkar 	if (err) {
7858016e29fSHarshad Shirwadkar 		ext4_std_error(sb, err);
7868016e29fSHarshad Shirwadkar 		goto out;
7878016e29fSHarshad Shirwadkar 	}
7888016e29fSHarshad Shirwadkar 	err = sync_dirty_buffer(inode_bitmap_bh);
7898016e29fSHarshad Shirwadkar 	if (err) {
7908016e29fSHarshad Shirwadkar 		ext4_std_error(sb, err);
7918016e29fSHarshad Shirwadkar 		goto out;
7928016e29fSHarshad Shirwadkar 	}
7938016e29fSHarshad Shirwadkar 
7948016e29fSHarshad Shirwadkar 	/* We may have to initialize the block bitmap if it isn't already */
7958016e29fSHarshad Shirwadkar 	if (ext4_has_group_desc_csum(sb) &&
7968016e29fSHarshad Shirwadkar 	    gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
7978016e29fSHarshad Shirwadkar 		struct buffer_head *block_bitmap_bh;
7988016e29fSHarshad Shirwadkar 
7998016e29fSHarshad Shirwadkar 		block_bitmap_bh = ext4_read_block_bitmap(sb, group);
8008016e29fSHarshad Shirwadkar 		if (IS_ERR(block_bitmap_bh)) {
8018016e29fSHarshad Shirwadkar 			err = PTR_ERR(block_bitmap_bh);
8028016e29fSHarshad Shirwadkar 			goto out;
8038016e29fSHarshad Shirwadkar 		}
8048016e29fSHarshad Shirwadkar 
8058016e29fSHarshad Shirwadkar 		BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
8068016e29fSHarshad Shirwadkar 		err = ext4_handle_dirty_metadata(NULL, NULL, block_bitmap_bh);
8078016e29fSHarshad Shirwadkar 		sync_dirty_buffer(block_bitmap_bh);
8088016e29fSHarshad Shirwadkar 
8098016e29fSHarshad Shirwadkar 		/* recheck and clear flag under lock if we still need to */
8108016e29fSHarshad Shirwadkar 		ext4_lock_group(sb, group);
8118016e29fSHarshad Shirwadkar 		if (ext4_has_group_desc_csum(sb) &&
8128016e29fSHarshad Shirwadkar 		    (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
8138016e29fSHarshad Shirwadkar 			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
8148016e29fSHarshad Shirwadkar 			ext4_free_group_clusters_set(sb, gdp,
8158016e29fSHarshad Shirwadkar 				ext4_free_clusters_after_init(sb, group, gdp));
8168016e29fSHarshad Shirwadkar 			ext4_block_bitmap_csum_set(sb, group, gdp,
8178016e29fSHarshad Shirwadkar 						   block_bitmap_bh);
8188016e29fSHarshad Shirwadkar 			ext4_group_desc_csum_set(sb, group, gdp);
8198016e29fSHarshad Shirwadkar 		}
8208016e29fSHarshad Shirwadkar 		ext4_unlock_group(sb, group);
8218016e29fSHarshad Shirwadkar 		brelse(block_bitmap_bh);
8228016e29fSHarshad Shirwadkar 
8238016e29fSHarshad Shirwadkar 		if (err) {
8248016e29fSHarshad Shirwadkar 			ext4_std_error(sb, err);
8258016e29fSHarshad Shirwadkar 			goto out;
8268016e29fSHarshad Shirwadkar 		}
8278016e29fSHarshad Shirwadkar 	}
8288016e29fSHarshad Shirwadkar 
8298016e29fSHarshad Shirwadkar 	/* Update the relevant bg descriptor fields */
8308016e29fSHarshad Shirwadkar 	if (ext4_has_group_desc_csum(sb)) {
8318016e29fSHarshad Shirwadkar 		int free;
8328016e29fSHarshad Shirwadkar 
8338016e29fSHarshad Shirwadkar 		ext4_lock_group(sb, group); /* while we modify the bg desc */
8348016e29fSHarshad Shirwadkar 		free = EXT4_INODES_PER_GROUP(sb) -
8358016e29fSHarshad Shirwadkar 			ext4_itable_unused_count(sb, gdp);
8368016e29fSHarshad Shirwadkar 		if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
8378016e29fSHarshad Shirwadkar 			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
8388016e29fSHarshad Shirwadkar 			free = 0;
8398016e29fSHarshad Shirwadkar 		}
8408016e29fSHarshad Shirwadkar 
8418016e29fSHarshad Shirwadkar 		/*
8428016e29fSHarshad Shirwadkar 		 * Check the relative inode number against the last used
8438016e29fSHarshad Shirwadkar 		 * relative inode number in this group. if it is greater
8448016e29fSHarshad Shirwadkar 		 * we need to update the bg_itable_unused count
8458016e29fSHarshad Shirwadkar 		 */
8468016e29fSHarshad Shirwadkar 		if (bit >= free)
8478016e29fSHarshad Shirwadkar 			ext4_itable_unused_set(sb, gdp,
8488016e29fSHarshad Shirwadkar 					(EXT4_INODES_PER_GROUP(sb) - bit - 1));
8498016e29fSHarshad Shirwadkar 	} else {
8508016e29fSHarshad Shirwadkar 		ext4_lock_group(sb, group);
8518016e29fSHarshad Shirwadkar 	}
8528016e29fSHarshad Shirwadkar 
8538016e29fSHarshad Shirwadkar 	ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1);
8548016e29fSHarshad Shirwadkar 	if (ext4_has_group_desc_csum(sb)) {
8558016e29fSHarshad Shirwadkar 		ext4_inode_bitmap_csum_set(sb, group, gdp, inode_bitmap_bh,
8568016e29fSHarshad Shirwadkar 					   EXT4_INODES_PER_GROUP(sb) / 8);
8578016e29fSHarshad Shirwadkar 		ext4_group_desc_csum_set(sb, group, gdp);
8588016e29fSHarshad Shirwadkar 	}
8598016e29fSHarshad Shirwadkar 
8608016e29fSHarshad Shirwadkar 	ext4_unlock_group(sb, group);
8618016e29fSHarshad Shirwadkar 	err = ext4_handle_dirty_metadata(NULL, NULL, group_desc_bh);
8628016e29fSHarshad Shirwadkar 	sync_dirty_buffer(group_desc_bh);
8638016e29fSHarshad Shirwadkar out:
8648016e29fSHarshad Shirwadkar 	return err;
8658016e29fSHarshad Shirwadkar }
8668016e29fSHarshad Shirwadkar 
867177cc0e7SEric Biggers static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
868177cc0e7SEric Biggers 					    bool encrypt)
869177cc0e7SEric Biggers {
870177cc0e7SEric Biggers 	struct super_block *sb = dir->i_sb;
871177cc0e7SEric Biggers 	int nblocks = 0;
872177cc0e7SEric Biggers #ifdef CONFIG_EXT4_FS_POSIX_ACL
873cac2f8b8SChristian Brauner 	struct posix_acl *p = get_inode_acl(dir, ACL_TYPE_DEFAULT);
874177cc0e7SEric Biggers 
875177cc0e7SEric Biggers 	if (IS_ERR(p))
876177cc0e7SEric Biggers 		return PTR_ERR(p);
877177cc0e7SEric Biggers 	if (p) {
878177cc0e7SEric Biggers 		int acl_size = p->a_count * sizeof(ext4_acl_entry);
879177cc0e7SEric Biggers 
880177cc0e7SEric Biggers 		nblocks += (S_ISDIR(mode) ? 2 : 1) *
881177cc0e7SEric Biggers 			__ext4_xattr_set_credits(sb, NULL /* inode */,
882177cc0e7SEric Biggers 						 NULL /* block_bh */, acl_size,
883177cc0e7SEric Biggers 						 true /* is_create */);
884177cc0e7SEric Biggers 		posix_acl_release(p);
885177cc0e7SEric Biggers 	}
886177cc0e7SEric Biggers #endif
887177cc0e7SEric Biggers 
888177cc0e7SEric Biggers #ifdef CONFIG_SECURITY
889177cc0e7SEric Biggers 	{
890177cc0e7SEric Biggers 		int num_security_xattrs = 1;
891177cc0e7SEric Biggers 
892177cc0e7SEric Biggers #ifdef CONFIG_INTEGRITY
893177cc0e7SEric Biggers 		num_security_xattrs++;
894177cc0e7SEric Biggers #endif
895177cc0e7SEric Biggers 		/*
896177cc0e7SEric Biggers 		 * We assume that security xattrs are never more than 1k.
897177cc0e7SEric Biggers 		 * In practice they are under 128 bytes.
898177cc0e7SEric Biggers 		 */
899177cc0e7SEric Biggers 		nblocks += num_security_xattrs *
900177cc0e7SEric Biggers 			__ext4_xattr_set_credits(sb, NULL /* inode */,
901177cc0e7SEric Biggers 						 NULL /* block_bh */, 1024,
902177cc0e7SEric Biggers 						 true /* is_create */);
903177cc0e7SEric Biggers 	}
904177cc0e7SEric Biggers #endif
905177cc0e7SEric Biggers 	if (encrypt)
906177cc0e7SEric Biggers 		nblocks += __ext4_xattr_set_credits(sb,
907177cc0e7SEric Biggers 						    NULL /* inode */,
908177cc0e7SEric Biggers 						    NULL /* block_bh */,
909177cc0e7SEric Biggers 						    FSCRYPT_SET_CONTEXT_MAX_SIZE,
910177cc0e7SEric Biggers 						    true /* is_create */);
911177cc0e7SEric Biggers 	return nblocks;
912177cc0e7SEric Biggers }
913177cc0e7SEric Biggers 
91419883bd9STheodore Ts'o /*
915ac27a0ecSDave Kleikamp  * There are two policies for allocating an inode.  If the new inode is
916ac27a0ecSDave Kleikamp  * a directory, then a forward search is made for a block group with both
917ac27a0ecSDave Kleikamp  * free space and a low directory-to-inode ratio; if that fails, then of
918ac27a0ecSDave Kleikamp  * the groups with above-average free space, that group with the fewest
919ac27a0ecSDave Kleikamp  * directories already is chosen.
920ac27a0ecSDave Kleikamp  *
921ac27a0ecSDave Kleikamp  * For other inodes, search forward from the parent directory's block
922ac27a0ecSDave Kleikamp  * group to find a free inode.
923ac27a0ecSDave Kleikamp  */
924*f2d40141SChristian Brauner struct inode *__ext4_new_inode(struct mnt_idmap *idmap,
92514f3db55SChristian Brauner 			       handle_t *handle, struct inode *dir,
9261139575aSTheodore Ts'o 			       umode_t mode, const struct qstr *qstr,
9271b917ed8STahsin Erdogan 			       __u32 goal, uid_t *owner, __u32 i_flags,
9281b917ed8STahsin Erdogan 			       int handle_type, unsigned int line_no,
9291b917ed8STahsin Erdogan 			       int nblocks)
930ac27a0ecSDave Kleikamp {
931ac27a0ecSDave Kleikamp 	struct super_block *sb;
9323300bedaSAneesh Kumar K.V 	struct buffer_head *inode_bitmap_bh = NULL;
9333300bedaSAneesh Kumar K.V 	struct buffer_head *group_desc_bh;
9348df9675fSTheodore Ts'o 	ext4_group_t ngroups, group = 0;
935ac27a0ecSDave Kleikamp 	unsigned long ino = 0;
936ac27a0ecSDave Kleikamp 	struct inode *inode;
937617ba13bSMingming Cao 	struct ext4_group_desc *gdp = NULL;
938617ba13bSMingming Cao 	struct ext4_inode_info *ei;
939617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
940a7cdadeeSJan Kara 	int ret2, err;
941ac27a0ecSDave Kleikamp 	struct inode *ret;
9422aa9fc4cSAvantika Mathur 	ext4_group_t i;
943772cb7c8SJose R. Santos 	ext4_group_t flex_group;
9448016e29fSHarshad Shirwadkar 	struct ext4_group_info *grp = NULL;
94502ce5316SEric Biggers 	bool encrypt = false;
946*f2d40141SChristian Brauner 	struct user_namespace *mnt_userns = mnt_idmap_owner(idmap);
947ac27a0ecSDave Kleikamp 
948ac27a0ecSDave Kleikamp 	/* Cannot create files in a deleted directory */
949ac27a0ecSDave Kleikamp 	if (!dir || !dir->i_nlink)
950ac27a0ecSDave Kleikamp 		return ERR_PTR(-EPERM);
951ac27a0ecSDave Kleikamp 
952af65207cSTahsin Erdogan 	sb = dir->i_sb;
953af65207cSTahsin Erdogan 	sbi = EXT4_SB(sb);
954af65207cSTahsin Erdogan 
955af65207cSTahsin Erdogan 	if (unlikely(ext4_forced_shutdown(sbi)))
9560db1ff22STheodore Ts'o 		return ERR_PTR(-EIO);
9570db1ff22STheodore Ts'o 
9588df9675fSTheodore Ts'o 	ngroups = ext4_get_groups_count(sb);
9599bffad1eSTheodore Ts'o 	trace_ext4_request_inode(dir, mode);
960ac27a0ecSDave Kleikamp 	inode = new_inode(sb);
961ac27a0ecSDave Kleikamp 	if (!inode)
962ac27a0ecSDave Kleikamp 		return ERR_PTR(-ENOMEM);
963617ba13bSMingming Cao 	ei = EXT4_I(inode);
964772cb7c8SJose R. Santos 
965eb9cc7e1SJan Kara 	/*
966b8a07463SAdam Buchbinder 	 * Initialize owners and quota early so that we don't have to account
967eb9cc7e1SJan Kara 	 * for quota initialization worst case in standard inode creating
968eb9cc7e1SJan Kara 	 * transaction
969eb9cc7e1SJan Kara 	 */
970eb9cc7e1SJan Kara 	if (owner) {
971eb9cc7e1SJan Kara 		inode->i_mode = mode;
972eb9cc7e1SJan Kara 		i_uid_write(inode, owner[0]);
973eb9cc7e1SJan Kara 		i_gid_write(inode, owner[1]);
974eb9cc7e1SJan Kara 	} else if (test_opt(sb, GRPID)) {
975eb9cc7e1SJan Kara 		inode->i_mode = mode;
976db998553SChristian Brauner 		inode_fsuid_set(inode, mnt_userns);
977eb9cc7e1SJan Kara 		inode->i_gid = dir->i_gid;
978eb9cc7e1SJan Kara 	} else
979*f2d40141SChristian Brauner 		inode_init_owner(idmap, inode, dir, mode);
980040cb378SLi Xi 
9810b7b7779SKaho Ng 	if (ext4_has_feature_project(sb) &&
982040cb378SLi Xi 	    ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT))
983040cb378SLi Xi 		ei->i_projid = EXT4_I(dir)->i_projid;
984040cb378SLi Xi 	else
985040cb378SLi Xi 		ei->i_projid = make_kprojid(&init_user_ns, EXT4_DEF_PROJID);
986040cb378SLi Xi 
98702ce5316SEric Biggers 	if (!(i_flags & EXT4_EA_INODE_FL)) {
98802ce5316SEric Biggers 		err = fscrypt_prepare_new_inode(dir, inode, &encrypt);
98902ce5316SEric Biggers 		if (err)
99002ce5316SEric Biggers 			goto out;
99102ce5316SEric Biggers 	}
99202ce5316SEric Biggers 
993a7cdadeeSJan Kara 	err = dquot_initialize(inode);
994a7cdadeeSJan Kara 	if (err)
995a7cdadeeSJan Kara 		goto out;
996eb9cc7e1SJan Kara 
99702ce5316SEric Biggers 	if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) {
99802ce5316SEric Biggers 		ret2 = ext4_xattr_credits_for_new_inode(dir, mode, encrypt);
99902ce5316SEric Biggers 		if (ret2 < 0) {
100002ce5316SEric Biggers 			err = ret2;
100102ce5316SEric Biggers 			goto out;
100202ce5316SEric Biggers 		}
100302ce5316SEric Biggers 		nblocks += ret2;
100402ce5316SEric Biggers 	}
100502ce5316SEric Biggers 
100611013911SAndreas Dilger 	if (!goal)
100711013911SAndreas Dilger 		goal = sbi->s_inode_goal;
100811013911SAndreas Dilger 
1009e6462869SJohann Lombardi 	if (goal && goal <= le32_to_cpu(sbi->s_es->s_inodes_count)) {
101011013911SAndreas Dilger 		group = (goal - 1) / EXT4_INODES_PER_GROUP(sb);
101111013911SAndreas Dilger 		ino = (goal - 1) % EXT4_INODES_PER_GROUP(sb);
101211013911SAndreas Dilger 		ret2 = 0;
101311013911SAndreas Dilger 		goto got_group;
101411013911SAndreas Dilger 	}
101511013911SAndreas Dilger 
10164113c4caSLukas Czerner 	if (S_ISDIR(mode))
1017f157a4aaSTheodore Ts'o 		ret2 = find_group_orlov(sb, dir, &group, mode, qstr);
10184113c4caSLukas Czerner 	else
1019a4912123STheodore Ts'o 		ret2 = find_group_other(sb, dir, &group, mode);
1020ac27a0ecSDave Kleikamp 
1021772cb7c8SJose R. Santos got_group:
1022a4912123STheodore Ts'o 	EXT4_I(dir)->i_last_alloc_group = group;
1023ac27a0ecSDave Kleikamp 	err = -ENOSPC;
10242aa9fc4cSAvantika Mathur 	if (ret2 == -1)
1025ac27a0ecSDave Kleikamp 		goto out;
1026ac27a0ecSDave Kleikamp 
1027119c0d44STheodore Ts'o 	/*
1028119c0d44STheodore Ts'o 	 * Normally we will only go through one pass of this loop,
1029119c0d44STheodore Ts'o 	 * unless we get unlucky and it turns out the group we selected
1030119c0d44STheodore Ts'o 	 * had its last inode grabbed by someone else.
1031119c0d44STheodore Ts'o 	 */
103211013911SAndreas Dilger 	for (i = 0; i < ngroups; i++, ino = 0) {
1033ac27a0ecSDave Kleikamp 		err = -EIO;
1034ac27a0ecSDave Kleikamp 
10353300bedaSAneesh Kumar K.V 		gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
1036ac27a0ecSDave Kleikamp 		if (!gdp)
1037eb9cc7e1SJan Kara 			goto out;
1038ac27a0ecSDave Kleikamp 
1039f2a09af6SYongqiang Yang 		/*
1040f2a09af6SYongqiang Yang 		 * Check free inodes count before loading bitmap.
1041f2a09af6SYongqiang Yang 		 */
10422fe435d8SWang Shilong 		if (ext4_free_inodes_count(sb, gdp) == 0)
10432fe435d8SWang Shilong 			goto next_group;
1044f2a09af6SYongqiang Yang 
10458016e29fSHarshad Shirwadkar 		if (!(sbi->s_mount_state & EXT4_FC_REPLAY)) {
104687a39389SDarrick J. Wong 			grp = ext4_get_group_info(sb, group);
10478016e29fSHarshad Shirwadkar 			/*
10488016e29fSHarshad Shirwadkar 			 * Skip groups with already-known suspicious inode
10498016e29fSHarshad Shirwadkar 			 * tables
10508016e29fSHarshad Shirwadkar 			 */
10512fe435d8SWang Shilong 			if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp))
10522fe435d8SWang Shilong 				goto next_group;
10538016e29fSHarshad Shirwadkar 		}
105487a39389SDarrick J. Wong 
10553300bedaSAneesh Kumar K.V 		brelse(inode_bitmap_bh);
10563300bedaSAneesh Kumar K.V 		inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
105787a39389SDarrick J. Wong 		/* Skip groups with suspicious inode tables */
10588016e29fSHarshad Shirwadkar 		if (((!(sbi->s_mount_state & EXT4_FC_REPLAY))
10598016e29fSHarshad Shirwadkar 		     && EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) ||
10609008a58eSDarrick J. Wong 		    IS_ERR(inode_bitmap_bh)) {
10619008a58eSDarrick J. Wong 			inode_bitmap_bh = NULL;
10622fe435d8SWang Shilong 			goto next_group;
106387a39389SDarrick J. Wong 		}
1064ac27a0ecSDave Kleikamp 
1065ac27a0ecSDave Kleikamp repeat_in_this_group:
1066901ed070SWang Shilong 		ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino);
1067901ed070SWang Shilong 		if (!ret2)
1068a34eb503STheodore Ts'o 			goto next_group;
1069901ed070SWang Shilong 
1070119c0d44STheodore Ts'o 		if (group == 0 && (ino + 1) < EXT4_FIRST_INO(sb)) {
1071119c0d44STheodore Ts'o 			ext4_error(sb, "reserved inode found cleared - "
1072119c0d44STheodore Ts'o 				   "inode=%lu", ino + 1);
1073206f6d55SWang Shilong 			ext4_mark_group_bitmap_corrupted(sb, group,
1074206f6d55SWang Shilong 					EXT4_GROUP_INFO_IBITMAP_CORRUPT);
10752fe435d8SWang Shilong 			goto next_group;
1076119c0d44STheodore Ts'o 		}
1077901ed070SWang Shilong 
10788016e29fSHarshad Shirwadkar 		if ((!(sbi->s_mount_state & EXT4_FC_REPLAY)) && !handle) {
10791139575aSTheodore Ts'o 			BUG_ON(nblocks <= 0);
10805f3e2403Schangfengnan 			handle = __ext4_journal_start_sb(NULL, dir->i_sb,
10815f3e2403Schangfengnan 				 line_no, handle_type, nblocks, 0,
1082a70fd5acSyangerkun 				 ext4_trans_default_revoke_credits(sb));
10831139575aSTheodore Ts'o 			if (IS_ERR(handle)) {
10841139575aSTheodore Ts'o 				err = PTR_ERR(handle);
1085eb9cc7e1SJan Kara 				ext4_std_error(sb, err);
1086eb9cc7e1SJan Kara 				goto out;
10871139575aSTheodore Ts'o 			}
10881139575aSTheodore Ts'o 		}
1089ffb5387eSEric Sandeen 		BUFFER_TRACE(inode_bitmap_bh, "get_write_access");
1090188c299eSJan Kara 		err = ext4_journal_get_write_access(handle, sb, inode_bitmap_bh,
1091188c299eSJan Kara 						    EXT4_JTR_NONE);
1092eb9cc7e1SJan Kara 		if (err) {
1093eb9cc7e1SJan Kara 			ext4_std_error(sb, err);
1094eb9cc7e1SJan Kara 			goto out;
1095eb9cc7e1SJan Kara 		}
1096119c0d44STheodore Ts'o 		ext4_lock_group(sb, group);
1097119c0d44STheodore Ts'o 		ret2 = ext4_test_and_set_bit(ino, inode_bitmap_bh->b_data);
1098901ed070SWang Shilong 		if (ret2) {
1099901ed070SWang Shilong 			/* Someone already took the bit. Repeat the search
1100901ed070SWang Shilong 			 * with lock held.
1101901ed070SWang Shilong 			 */
1102901ed070SWang Shilong 			ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino);
1103901ed070SWang Shilong 			if (ret2) {
1104901ed070SWang Shilong 				ext4_set_bit(ino, inode_bitmap_bh->b_data);
1105901ed070SWang Shilong 				ret2 = 0;
1106901ed070SWang Shilong 			} else {
1107901ed070SWang Shilong 				ret2 = 1; /* we didn't grab the inode */
1108901ed070SWang Shilong 			}
1109901ed070SWang Shilong 		}
1110119c0d44STheodore Ts'o 		ext4_unlock_group(sb, group);
1111119c0d44STheodore Ts'o 		ino++;		/* the inode bitmap is zero-based */
1112119c0d44STheodore Ts'o 		if (!ret2)
1113119c0d44STheodore Ts'o 			goto got; /* we grabbed the inode! */
1114901ed070SWang Shilong 
1115119c0d44STheodore Ts'o 		if (ino < EXT4_INODES_PER_GROUP(sb))
1116119c0d44STheodore Ts'o 			goto repeat_in_this_group;
1117a34eb503STheodore Ts'o next_group:
1118a34eb503STheodore Ts'o 		if (++group == ngroups)
1119a34eb503STheodore Ts'o 			group = 0;
1120ac27a0ecSDave Kleikamp 	}
1121ac27a0ecSDave Kleikamp 	err = -ENOSPC;
1122ac27a0ecSDave Kleikamp 	goto out;
1123ac27a0ecSDave Kleikamp 
1124ac27a0ecSDave Kleikamp got:
1125ffb5387eSEric Sandeen 	BUFFER_TRACE(inode_bitmap_bh, "call ext4_handle_dirty_metadata");
1126ffb5387eSEric Sandeen 	err = ext4_handle_dirty_metadata(handle, NULL, inode_bitmap_bh);
1127eb9cc7e1SJan Kara 	if (err) {
1128eb9cc7e1SJan Kara 		ext4_std_error(sb, err);
1129eb9cc7e1SJan Kara 		goto out;
1130eb9cc7e1SJan Kara 	}
1131ffb5387eSEric Sandeen 
113261c219f5STheodore Ts'o 	BUFFER_TRACE(group_desc_bh, "get_write_access");
1133188c299eSJan Kara 	err = ext4_journal_get_write_access(handle, sb, group_desc_bh,
1134188c299eSJan Kara 					    EXT4_JTR_NONE);
113561c219f5STheodore Ts'o 	if (err) {
113661c219f5STheodore Ts'o 		ext4_std_error(sb, err);
113761c219f5STheodore Ts'o 		goto out;
113861c219f5STheodore Ts'o 	}
113961c219f5STheodore Ts'o 
1140717d50e4SAndreas Dilger 	/* We may have to initialize the block bitmap if it isn't already */
1141feb0ab32SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb) &&
1142717d50e4SAndreas Dilger 	    gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
11433300bedaSAneesh Kumar K.V 		struct buffer_head *block_bitmap_bh;
1144717d50e4SAndreas Dilger 
11453300bedaSAneesh Kumar K.V 		block_bitmap_bh = ext4_read_block_bitmap(sb, group);
11469008a58eSDarrick J. Wong 		if (IS_ERR(block_bitmap_bh)) {
11479008a58eSDarrick J. Wong 			err = PTR_ERR(block_bitmap_bh);
1148599a9b77SJan Kara 			goto out;
1149599a9b77SJan Kara 		}
11503300bedaSAneesh Kumar K.V 		BUFFER_TRACE(block_bitmap_bh, "get block bitmap access");
1151188c299eSJan Kara 		err = ext4_journal_get_write_access(handle, sb, block_bitmap_bh,
1152188c299eSJan Kara 						    EXT4_JTR_NONE);
1153717d50e4SAndreas Dilger 		if (err) {
11543300bedaSAneesh Kumar K.V 			brelse(block_bitmap_bh);
1155eb9cc7e1SJan Kara 			ext4_std_error(sb, err);
1156eb9cc7e1SJan Kara 			goto out;
1157717d50e4SAndreas Dilger 		}
1158717d50e4SAndreas Dilger 
1159fd034a84STheodore Ts'o 		BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
1160fd034a84STheodore Ts'o 		err = ext4_handle_dirty_metadata(handle, NULL, block_bitmap_bh);
1161fd034a84STheodore Ts'o 
1162717d50e4SAndreas Dilger 		/* recheck and clear flag under lock if we still need to */
1163fd034a84STheodore Ts'o 		ext4_lock_group(sb, group);
11648844618dSTheodore Ts'o 		if (ext4_has_group_desc_csum(sb) &&
11658844618dSTheodore Ts'o 		    (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
11663300bedaSAneesh Kumar K.V 			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
1167021b65bbSTheodore Ts'o 			ext4_free_group_clusters_set(sb, gdp,
1168cff1dfd7STheodore Ts'o 				ext4_free_clusters_after_init(sb, group, gdp));
1169fa77dcfaSDarrick J. Wong 			ext4_block_bitmap_csum_set(sb, group, gdp,
117079f1ba49STao Ma 						   block_bitmap_bh);
1171feb0ab32SDarrick J. Wong 			ext4_group_desc_csum_set(sb, group, gdp);
1172717d50e4SAndreas Dilger 		}
1173955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, group);
1174aeb1e5d6STheodore Ts'o 		brelse(block_bitmap_bh);
1175717d50e4SAndreas Dilger 
1176eb9cc7e1SJan Kara 		if (err) {
1177eb9cc7e1SJan Kara 			ext4_std_error(sb, err);
1178eb9cc7e1SJan Kara 			goto out;
1179eb9cc7e1SJan Kara 		}
1180717d50e4SAndreas Dilger 	}
1181119c0d44STheodore Ts'o 
1182119c0d44STheodore Ts'o 	/* Update the relevant bg descriptor fields */
118341a246d1SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb)) {
1184119c0d44STheodore Ts'o 		int free;
11858016e29fSHarshad Shirwadkar 		struct ext4_group_info *grp = NULL;
1186119c0d44STheodore Ts'o 
11878016e29fSHarshad Shirwadkar 		if (!(sbi->s_mount_state & EXT4_FC_REPLAY)) {
11888016e29fSHarshad Shirwadkar 			grp = ext4_get_group_info(sb, group);
11898016e29fSHarshad Shirwadkar 			down_read(&grp->alloc_sem); /*
11908016e29fSHarshad Shirwadkar 						     * protect vs itable
11918016e29fSHarshad Shirwadkar 						     * lazyinit
11928016e29fSHarshad Shirwadkar 						     */
11938016e29fSHarshad Shirwadkar 		}
1194119c0d44STheodore Ts'o 		ext4_lock_group(sb, group); /* while we modify the bg desc */
1195119c0d44STheodore Ts'o 		free = EXT4_INODES_PER_GROUP(sb) -
1196119c0d44STheodore Ts'o 			ext4_itable_unused_count(sb, gdp);
1197119c0d44STheodore Ts'o 		if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
1198119c0d44STheodore Ts'o 			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
1199119c0d44STheodore Ts'o 			free = 0;
1200119c0d44STheodore Ts'o 		}
1201119c0d44STheodore Ts'o 		/*
1202119c0d44STheodore Ts'o 		 * Check the relative inode number against the last used
1203119c0d44STheodore Ts'o 		 * relative inode number in this group. if it is greater
1204119c0d44STheodore Ts'o 		 * we need to update the bg_itable_unused count
1205119c0d44STheodore Ts'o 		 */
1206119c0d44STheodore Ts'o 		if (ino > free)
1207119c0d44STheodore Ts'o 			ext4_itable_unused_set(sb, gdp,
1208119c0d44STheodore Ts'o 					(EXT4_INODES_PER_GROUP(sb) - ino));
12098016e29fSHarshad Shirwadkar 		if (!(sbi->s_mount_state & EXT4_FC_REPLAY))
1210119c0d44STheodore Ts'o 			up_read(&grp->alloc_sem);
12116f2e9f0eSTao Ma 	} else {
12126f2e9f0eSTao Ma 		ext4_lock_group(sb, group);
1213119c0d44STheodore Ts'o 	}
12146f2e9f0eSTao Ma 
1215119c0d44STheodore Ts'o 	ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1);
1216119c0d44STheodore Ts'o 	if (S_ISDIR(mode)) {
1217119c0d44STheodore Ts'o 		ext4_used_dirs_set(sb, gdp, ext4_used_dirs_count(sb, gdp) + 1);
1218119c0d44STheodore Ts'o 		if (sbi->s_log_groups_per_flex) {
1219119c0d44STheodore Ts'o 			ext4_group_t f = ext4_flex_group(sbi, group);
1220119c0d44STheodore Ts'o 
12217c990728SSuraj Jitindar Singh 			atomic_inc(&sbi_array_rcu_deref(sbi, s_flex_groups,
12227c990728SSuraj Jitindar Singh 							f)->used_dirs);
1223119c0d44STheodore Ts'o 		}
1224119c0d44STheodore Ts'o 	}
122541a246d1SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb)) {
122641a246d1SDarrick J. Wong 		ext4_inode_bitmap_csum_set(sb, group, gdp, inode_bitmap_bh,
122741a246d1SDarrick J. Wong 					   EXT4_INODES_PER_GROUP(sb) / 8);
1228feb0ab32SDarrick J. Wong 		ext4_group_desc_csum_set(sb, group, gdp);
1229119c0d44STheodore Ts'o 	}
12306f2e9f0eSTao Ma 	ext4_unlock_group(sb, group);
1231119c0d44STheodore Ts'o 
12323300bedaSAneesh Kumar K.V 	BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata");
12333300bedaSAneesh Kumar K.V 	err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh);
1234eb9cc7e1SJan Kara 	if (err) {
1235eb9cc7e1SJan Kara 		ext4_std_error(sb, err);
1236eb9cc7e1SJan Kara 		goto out;
1237eb9cc7e1SJan Kara 	}
1238ac27a0ecSDave Kleikamp 
1239ac27a0ecSDave Kleikamp 	percpu_counter_dec(&sbi->s_freeinodes_counter);
1240ac27a0ecSDave Kleikamp 	if (S_ISDIR(mode))
1241ac27a0ecSDave Kleikamp 		percpu_counter_inc(&sbi->s_dirs_counter);
1242ac27a0ecSDave Kleikamp 
1243772cb7c8SJose R. Santos 	if (sbi->s_log_groups_per_flex) {
1244772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, group);
12457c990728SSuraj Jitindar Singh 		atomic_dec(&sbi_array_rcu_deref(sbi, s_flex_groups,
12467c990728SSuraj Jitindar Singh 						flex_group)->free_inodes);
1247772cb7c8SJose R. Santos 	}
1248ac27a0ecSDave Kleikamp 
1249717d50e4SAndreas Dilger 	inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
1250ac27a0ecSDave Kleikamp 	/* This is the optimal IO size (for stat), not the fs block size */
1251ac27a0ecSDave Kleikamp 	inode->i_blocks = 0;
125295582b00SDeepa Dinamani 	inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
12537b62b293SArnd Bergmann 	ei->i_crtime = inode->i_mtime;
1254ac27a0ecSDave Kleikamp 
1255ac27a0ecSDave Kleikamp 	memset(ei->i_data, 0, sizeof(ei->i_data));
1256ac27a0ecSDave Kleikamp 	ei->i_dir_start_lookup = 0;
1257ac27a0ecSDave Kleikamp 	ei->i_disksize = 0;
1258ac27a0ecSDave Kleikamp 
12594af83508SEryu Guan 	/* Don't inherit extent flag from directory, amongst others. */
12602dc6b0d4SDuane Griffin 	ei->i_flags =
12612dc6b0d4SDuane Griffin 		ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED);
12621b917ed8STahsin Erdogan 	ei->i_flags |= i_flags;
1263ac27a0ecSDave Kleikamp 	ei->i_file_acl = 0;
1264ac27a0ecSDave Kleikamp 	ei->i_dtime = 0;
1265ac27a0ecSDave Kleikamp 	ei->i_block_group = group;
1266a4912123STheodore Ts'o 	ei->i_last_alloc_group = ~0;
1267ac27a0ecSDave Kleikamp 
1268043546e4SIra Weiny 	ext4_set_inode_flags(inode, true);
1269ac27a0ecSDave Kleikamp 	if (IS_DIRSYNC(inode))
12700390131bSFrank Mayhar 		ext4_handle_sync(handle);
12716b38e842SAl Viro 	if (insert_inode_locked(inode) < 0) {
1272acd6ad83SJan Kara 		/*
1273acd6ad83SJan Kara 		 * Likely a bitmap corruption causing inode to be allocated
1274acd6ad83SJan Kara 		 * twice.
1275acd6ad83SJan Kara 		 */
1276acd6ad83SJan Kara 		err = -EIO;
1277eb9cc7e1SJan Kara 		ext4_error(sb, "failed to insert inode %lu: doubly allocated?",
1278eb9cc7e1SJan Kara 			   inode->i_ino);
1279206f6d55SWang Shilong 		ext4_mark_group_bitmap_corrupted(sb, group,
1280206f6d55SWang Shilong 					EXT4_GROUP_INFO_IBITMAP_CORRUPT);
1281eb9cc7e1SJan Kara 		goto out;
12826b38e842SAl Viro 	}
1283a251c17aSJason A. Donenfeld 	inode->i_generation = get_random_u32();
1284ac27a0ecSDave Kleikamp 
1285814525f4SDarrick J. Wong 	/* Precompute checksum seed for inode metadata */
12869aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sb)) {
1287814525f4SDarrick J. Wong 		__u32 csum;
1288814525f4SDarrick J. Wong 		__le32 inum = cpu_to_le32(inode->i_ino);
1289814525f4SDarrick J. Wong 		__le32 gen = cpu_to_le32(inode->i_generation);
1290814525f4SDarrick J. Wong 		csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
1291814525f4SDarrick J. Wong 				   sizeof(inum));
1292814525f4SDarrick J. Wong 		ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
1293814525f4SDarrick J. Wong 					      sizeof(gen));
1294814525f4SDarrick J. Wong 	}
1295814525f4SDarrick J. Wong 
1296353eb83cSTheodore Ts'o 	ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
129719f5fb7aSTheodore Ts'o 	ext4_set_inode_state(inode, EXT4_STATE_NEW);
1298ef7f3835SKalpak Shah 
129949598e04SJun Piao 	ei->i_extra_isize = sbi->s_want_extra_isize;
1300f08225d1STao Ma 	ei->i_inline_off = 0;
13014811d992STheodore Ts'o 	if (ext4_has_feature_inline_data(sb) &&
13024811d992STheodore Ts'o 	    (!(ei->i_flags & EXT4_DAX_FL) || S_ISDIR(mode)))
1303f08225d1STao Ma 		ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
1304ac27a0ecSDave Kleikamp 	ret = inode;
130563936ddaSChristoph Hellwig 	err = dquot_alloc_inode(inode);
130663936ddaSChristoph Hellwig 	if (err)
1307ac27a0ecSDave Kleikamp 		goto fail_drop;
1308ac27a0ecSDave Kleikamp 
1309aa1dca3bSEric Biggers 	/*
1310aa1dca3bSEric Biggers 	 * Since the encryption xattr will always be unique, create it first so
1311aa1dca3bSEric Biggers 	 * that it's less likely to end up in an external xattr block and
1312aa1dca3bSEric Biggers 	 * prevent its deduplication.
1313aa1dca3bSEric Biggers 	 */
1314aa1dca3bSEric Biggers 	if (encrypt) {
131502ce5316SEric Biggers 		err = fscrypt_set_context(inode, handle);
1316aa1dca3bSEric Biggers 		if (err)
1317aa1dca3bSEric Biggers 			goto fail_free_drop;
1318aa1dca3bSEric Biggers 	}
1319aa1dca3bSEric Biggers 
13201b917ed8STahsin Erdogan 	if (!(ei->i_flags & EXT4_EA_INODE_FL)) {
1321617ba13bSMingming Cao 		err = ext4_init_acl(handle, inode, dir);
1322ac27a0ecSDave Kleikamp 		if (err)
1323ac27a0ecSDave Kleikamp 			goto fail_free_drop;
1324ac27a0ecSDave Kleikamp 
13252a7dba39SEric Paris 		err = ext4_init_security(handle, inode, dir, qstr);
1326ac27a0ecSDave Kleikamp 		if (err)
1327ac27a0ecSDave Kleikamp 			goto fail_free_drop;
1328ad47f953STahsin Erdogan 	}
1329ac27a0ecSDave Kleikamp 
1330e2b911c5SDarrick J. Wong 	if (ext4_has_feature_extents(sb)) {
1331e4079a11SEric Sandeen 		/* set extent flag only for directory, file and normal symlink*/
1332e65187e6SAneesh Kumar K.V 		if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
133312e9b892SDmitry Monakhov 			ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
1334a86c6181SAlex Tomas 			ext4_ext_tree_init(handle, inode);
1335a86c6181SAlex Tomas 		}
133642bf0383SAneesh Kumar K.V 	}
1337ac27a0ecSDave Kleikamp 
1338688f869cSTheodore Ts'o 	if (ext4_handle_valid(handle)) {
1339688f869cSTheodore Ts'o 		ei->i_sync_tid = handle->h_transaction->t_tid;
1340688f869cSTheodore Ts'o 		ei->i_datasync_tid = handle->h_transaction->t_tid;
1341688f869cSTheodore Ts'o 	}
1342688f869cSTheodore Ts'o 
13438753e88fSAneesh Kumar K.V 	err = ext4_mark_inode_dirty(handle, inode);
13448753e88fSAneesh Kumar K.V 	if (err) {
13458753e88fSAneesh Kumar K.V 		ext4_std_error(sb, err);
13468753e88fSAneesh Kumar K.V 		goto fail_free_drop;
13478753e88fSAneesh Kumar K.V 	}
13488753e88fSAneesh Kumar K.V 
1349617ba13bSMingming Cao 	ext4_debug("allocating inode %lu\n", inode->i_ino);
13509bffad1eSTheodore Ts'o 	trace_ext4_allocate_inode(inode, dir, mode);
13513300bedaSAneesh Kumar K.V 	brelse(inode_bitmap_bh);
1352ac27a0ecSDave Kleikamp 	return ret;
1353ac27a0ecSDave Kleikamp 
1354ac27a0ecSDave Kleikamp fail_free_drop:
135563936ddaSChristoph Hellwig 	dquot_free_inode(inode);
1356ac27a0ecSDave Kleikamp fail_drop:
13576d6b77f1SMiklos Szeredi 	clear_nlink(inode);
13586b38e842SAl Viro 	unlock_new_inode(inode);
1359eb9cc7e1SJan Kara out:
1360eb9cc7e1SJan Kara 	dquot_drop(inode);
1361eb9cc7e1SJan Kara 	inode->i_flags |= S_NOQUOTA;
1362ac27a0ecSDave Kleikamp 	iput(inode);
13633300bedaSAneesh Kumar K.V 	brelse(inode_bitmap_bh);
1364ac27a0ecSDave Kleikamp 	return ERR_PTR(err);
1365ac27a0ecSDave Kleikamp }
1366ac27a0ecSDave Kleikamp 
1367ac27a0ecSDave Kleikamp /* Verify that we are loading a valid orphan from disk */
1368617ba13bSMingming Cao struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
1369ac27a0ecSDave Kleikamp {
1370617ba13bSMingming Cao 	unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
1371fd2d4291SAvantika Mathur 	ext4_group_t block_group;
1372ac27a0ecSDave Kleikamp 	int bit;
13737827a7f6STheodore Ts'o 	struct buffer_head *bitmap_bh = NULL;
1374ac27a0ecSDave Kleikamp 	struct inode *inode = NULL;
13757827a7f6STheodore Ts'o 	int err = -EFSCORRUPTED;
1376ac27a0ecSDave Kleikamp 
13777827a7f6STheodore Ts'o 	if (ino < EXT4_FIRST_INO(sb) || ino > max_ino)
13787827a7f6STheodore Ts'o 		goto bad_orphan;
1379ac27a0ecSDave Kleikamp 
1380617ba13bSMingming Cao 	block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
1381617ba13bSMingming Cao 	bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
1382e29d1cdeSEric Sandeen 	bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
1383206f6d55SWang Shilong 	if (IS_ERR(bitmap_bh))
1384e884bce1SAl Viro 		return ERR_CAST(bitmap_bh);
1385ac27a0ecSDave Kleikamp 
1386ac27a0ecSDave Kleikamp 	/* Having the inode bit set should be a 100% indicator that this
1387ac27a0ecSDave Kleikamp 	 * is a valid orphan (no e2fsck run on fs).  Orphans also include
1388ac27a0ecSDave Kleikamp 	 * inodes that were being truncated, so we can't check i_nlink==0.
1389ac27a0ecSDave Kleikamp 	 */
13901d1fe1eeSDavid Howells 	if (!ext4_test_bit(bit, bitmap_bh->b_data))
13911d1fe1eeSDavid Howells 		goto bad_orphan;
13921d1fe1eeSDavid Howells 
13938a363970STheodore Ts'o 	inode = ext4_iget(sb, ino, EXT4_IGET_NORMAL);
13947827a7f6STheodore Ts'o 	if (IS_ERR(inode)) {
13957827a7f6STheodore Ts'o 		err = PTR_ERR(inode);
139654d3adbcSTheodore Ts'o 		ext4_error_err(sb, -err,
139754d3adbcSTheodore Ts'o 			       "couldn't read orphan inode %lu (err %d)",
13987827a7f6STheodore Ts'o 			       ino, err);
13993bbd0ef2SXiyu Yang 		brelse(bitmap_bh);
14007827a7f6STheodore Ts'o 		return inode;
14017827a7f6STheodore Ts'o 	}
14021d1fe1eeSDavid Howells 
140391ef4cafSDuane Griffin 	/*
1404c9eb13a9STheodore Ts'o 	 * If the orphans has i_nlinks > 0 then it should be able to
1405c9eb13a9STheodore Ts'o 	 * be truncated, otherwise it won't be removed from the orphan
1406c9eb13a9STheodore Ts'o 	 * list during processing and an infinite loop will result.
1407c9eb13a9STheodore Ts'o 	 * Similarly, it must not be a bad inode.
140891ef4cafSDuane Griffin 	 */
1409c9eb13a9STheodore Ts'o 	if ((inode->i_nlink && !ext4_can_truncate(inode)) ||
1410c9eb13a9STheodore Ts'o 	    is_bad_inode(inode))
141191ef4cafSDuane Griffin 		goto bad_orphan;
141291ef4cafSDuane Griffin 
14131d1fe1eeSDavid Howells 	if (NEXT_ORPHAN(inode) > max_ino)
14141d1fe1eeSDavid Howells 		goto bad_orphan;
14151d1fe1eeSDavid Howells 	brelse(bitmap_bh);
14161d1fe1eeSDavid Howells 	return inode;
14171d1fe1eeSDavid Howells 
14181d1fe1eeSDavid Howells bad_orphan:
14197827a7f6STheodore Ts'o 	ext4_error(sb, "bad orphan inode %lu", ino);
14207827a7f6STheodore Ts'o 	if (bitmap_bh)
14217827a7f6STheodore Ts'o 		printk(KERN_ERR "ext4_test_bit(bit=%d, block=%llu) = %d\n",
1422ac27a0ecSDave Kleikamp 		       bit, (unsigned long long)bitmap_bh->b_blocknr,
1423617ba13bSMingming Cao 		       ext4_test_bit(bit, bitmap_bh->b_data));
1424ac27a0ecSDave Kleikamp 	if (inode) {
14257827a7f6STheodore Ts'o 		printk(KERN_ERR "is_bad_inode(inode)=%d\n",
1426ac27a0ecSDave Kleikamp 		       is_bad_inode(inode));
14277827a7f6STheodore Ts'o 		printk(KERN_ERR "NEXT_ORPHAN(inode)=%u\n",
1428ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
14297827a7f6STheodore Ts'o 		printk(KERN_ERR "max_ino=%lu\n", max_ino);
14307827a7f6STheodore Ts'o 		printk(KERN_ERR "i_nlink=%u\n", inode->i_nlink);
1431ac27a0ecSDave Kleikamp 		/* Avoid freeing blocks if we got a bad deleted inode */
14321d1fe1eeSDavid Howells 		if (inode->i_nlink == 0)
1433ac27a0ecSDave Kleikamp 			inode->i_blocks = 0;
1434ac27a0ecSDave Kleikamp 		iput(inode);
1435ac27a0ecSDave Kleikamp 	}
1436ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
14371d1fe1eeSDavid Howells 	return ERR_PTR(err);
1438ac27a0ecSDave Kleikamp }
1439ac27a0ecSDave Kleikamp 
1440617ba13bSMingming Cao unsigned long ext4_count_free_inodes(struct super_block *sb)
1441ac27a0ecSDave Kleikamp {
1442ac27a0ecSDave Kleikamp 	unsigned long desc_count;
1443617ba13bSMingming Cao 	struct ext4_group_desc *gdp;
14448df9675fSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
1445617ba13bSMingming Cao #ifdef EXT4FS_DEBUG
1446617ba13bSMingming Cao 	struct ext4_super_block *es;
1447ac27a0ecSDave Kleikamp 	unsigned long bitmap_count, x;
1448ac27a0ecSDave Kleikamp 	struct buffer_head *bitmap_bh = NULL;
1449ac27a0ecSDave Kleikamp 
1450617ba13bSMingming Cao 	es = EXT4_SB(sb)->s_es;
1451ac27a0ecSDave Kleikamp 	desc_count = 0;
1452ac27a0ecSDave Kleikamp 	bitmap_count = 0;
1453ac27a0ecSDave Kleikamp 	gdp = NULL;
14548df9675fSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
1455617ba13bSMingming Cao 		gdp = ext4_get_group_desc(sb, i, NULL);
1456ac27a0ecSDave Kleikamp 		if (!gdp)
1457ac27a0ecSDave Kleikamp 			continue;
1458560671a0SAneesh Kumar K.V 		desc_count += ext4_free_inodes_count(sb, gdp);
1459ac27a0ecSDave Kleikamp 		brelse(bitmap_bh);
1460e29d1cdeSEric Sandeen 		bitmap_bh = ext4_read_inode_bitmap(sb, i);
14619008a58eSDarrick J. Wong 		if (IS_ERR(bitmap_bh)) {
14629008a58eSDarrick J. Wong 			bitmap_bh = NULL;
1463ac27a0ecSDave Kleikamp 			continue;
14649008a58eSDarrick J. Wong 		}
1465ac27a0ecSDave Kleikamp 
1466f6fb99caSTheodore Ts'o 		x = ext4_count_free(bitmap_bh->b_data,
1467f6fb99caSTheodore Ts'o 				    EXT4_INODES_PER_GROUP(sb) / 8);
1468c549a95dSEric Sandeen 		printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
1469785b4b3aSPeng Tao 			(unsigned long) i, ext4_free_inodes_count(sb, gdp), x);
1470ac27a0ecSDave Kleikamp 		bitmap_count += x;
1471ac27a0ecSDave Kleikamp 	}
1472ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
14734776004fSTheodore Ts'o 	printk(KERN_DEBUG "ext4_count_free_inodes: "
14744776004fSTheodore Ts'o 	       "stored = %u, computed = %lu, %lu\n",
1475ac27a0ecSDave Kleikamp 	       le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
1476ac27a0ecSDave Kleikamp 	return desc_count;
1477ac27a0ecSDave Kleikamp #else
1478ac27a0ecSDave Kleikamp 	desc_count = 0;
14798df9675fSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
1480617ba13bSMingming Cao 		gdp = ext4_get_group_desc(sb, i, NULL);
1481ac27a0ecSDave Kleikamp 		if (!gdp)
1482ac27a0ecSDave Kleikamp 			continue;
1483560671a0SAneesh Kumar K.V 		desc_count += ext4_free_inodes_count(sb, gdp);
1484ac27a0ecSDave Kleikamp 		cond_resched();
1485ac27a0ecSDave Kleikamp 	}
1486ac27a0ecSDave Kleikamp 	return desc_count;
1487ac27a0ecSDave Kleikamp #endif
1488ac27a0ecSDave Kleikamp }
1489ac27a0ecSDave Kleikamp 
1490ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
1491617ba13bSMingming Cao unsigned long ext4_count_dirs(struct super_block * sb)
1492ac27a0ecSDave Kleikamp {
1493ac27a0ecSDave Kleikamp 	unsigned long count = 0;
14948df9675fSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
1495ac27a0ecSDave Kleikamp 
14968df9675fSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
1497617ba13bSMingming Cao 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1498ac27a0ecSDave Kleikamp 		if (!gdp)
1499ac27a0ecSDave Kleikamp 			continue;
1500560671a0SAneesh Kumar K.V 		count += ext4_used_dirs_count(sb, gdp);
1501ac27a0ecSDave Kleikamp 	}
1502ac27a0ecSDave Kleikamp 	return count;
1503ac27a0ecSDave Kleikamp }
1504bfff6873SLukas Czerner 
1505bfff6873SLukas Czerner /*
1506bfff6873SLukas Czerner  * Zeroes not yet zeroed inode table - just write zeroes through the whole
1507bfff6873SLukas Czerner  * inode table. Must be called without any spinlock held. The only place
1508bfff6873SLukas Czerner  * where it is called from on active part of filesystem is ext4lazyinit
1509bfff6873SLukas Czerner  * thread, so we do not need any special locks, however we have to prevent
1510bfff6873SLukas Czerner  * inode allocation from the current group, so we take alloc_sem lock, to
1511119c0d44STheodore Ts'o  * block ext4_new_inode() until we are finished.
1512bfff6873SLukas Czerner  */
1513e0cbee3eSH Hartley Sweeten int ext4_init_inode_table(struct super_block *sb, ext4_group_t group,
1514bfff6873SLukas Czerner 				 int barrier)
1515bfff6873SLukas Czerner {
1516bfff6873SLukas Czerner 	struct ext4_group_info *grp = ext4_get_group_info(sb, group);
1517bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1518bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
1519bfff6873SLukas Czerner 	struct buffer_head *group_desc_bh;
1520bfff6873SLukas Czerner 	handle_t *handle;
1521bfff6873SLukas Czerner 	ext4_fsblk_t blk;
1522bfff6873SLukas Czerner 	int num, ret = 0, used_blks = 0;
1523a149d2a5SZhang Yi 	unsigned long used_inos = 0;
1524bfff6873SLukas Czerner 
1525bfff6873SLukas Czerner 	/* This should not happen, but just to be sure check this */
1526bc98a42cSDavid Howells 	if (sb_rdonly(sb)) {
1527bfff6873SLukas Czerner 		ret = 1;
1528bfff6873SLukas Czerner 		goto out;
1529bfff6873SLukas Czerner 	}
1530bfff6873SLukas Czerner 
1531bfff6873SLukas Czerner 	gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
1532bfff6873SLukas Czerner 	if (!gdp)
1533bfff6873SLukas Czerner 		goto out;
1534bfff6873SLukas Czerner 
1535bfff6873SLukas Czerner 	/*
1536bfff6873SLukas Czerner 	 * We do not need to lock this, because we are the only one
1537bfff6873SLukas Czerner 	 * handling this flag.
1538bfff6873SLukas Czerner 	 */
1539bfff6873SLukas Czerner 	if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED))
1540bfff6873SLukas Czerner 		goto out;
1541bfff6873SLukas Czerner 
15429924a92aSTheodore Ts'o 	handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
1543bfff6873SLukas Czerner 	if (IS_ERR(handle)) {
1544bfff6873SLukas Czerner 		ret = PTR_ERR(handle);
1545bfff6873SLukas Czerner 		goto out;
1546bfff6873SLukas Czerner 	}
1547bfff6873SLukas Czerner 
1548bfff6873SLukas Czerner 	down_write(&grp->alloc_sem);
1549bfff6873SLukas Czerner 	/*
1550bfff6873SLukas Czerner 	 * If inode bitmap was already initialized there may be some
1551bfff6873SLukas Czerner 	 * used inodes so we need to skip blocks with used inodes in
1552bfff6873SLukas Czerner 	 * inode table.
1553bfff6873SLukas Czerner 	 */
1554a149d2a5SZhang Yi 	if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT))) {
1555a149d2a5SZhang Yi 		used_inos = EXT4_INODES_PER_GROUP(sb) -
1556a149d2a5SZhang Yi 			    ext4_itable_unused_count(sb, gdp);
1557a149d2a5SZhang Yi 		used_blks = DIV_ROUND_UP(used_inos, sbi->s_inodes_per_block);
1558bfff6873SLukas Czerner 
1559a149d2a5SZhang Yi 		/* Bogus inode unused count? */
1560a149d2a5SZhang Yi 		if (used_blks < 0 || used_blks > sbi->s_itb_per_group) {
15611084f252STheodore Ts'o 			ext4_error(sb, "Something is wrong with group %u: "
15621084f252STheodore Ts'o 				   "used itable blocks: %d; "
15631084f252STheodore Ts'o 				   "itable unused count: %u",
1564857ac889SLukas Czerner 				   group, used_blks,
1565857ac889SLukas Czerner 				   ext4_itable_unused_count(sb, gdp));
1566857ac889SLukas Czerner 			ret = 1;
156733853a0dSYongqiang Yang 			goto err_out;
1568857ac889SLukas Czerner 		}
1569857ac889SLukas Czerner 
1570a149d2a5SZhang Yi 		used_inos += group * EXT4_INODES_PER_GROUP(sb);
1571a149d2a5SZhang Yi 		/*
1572a149d2a5SZhang Yi 		 * Are there some uninitialized inodes in the inode table
1573a149d2a5SZhang Yi 		 * before the first normal inode?
1574a149d2a5SZhang Yi 		 */
1575a149d2a5SZhang Yi 		if ((used_blks != sbi->s_itb_per_group) &&
1576a149d2a5SZhang Yi 		     (used_inos < EXT4_FIRST_INO(sb))) {
1577a149d2a5SZhang Yi 			ext4_error(sb, "Something is wrong with group %u: "
1578a149d2a5SZhang Yi 				   "itable unused count: %u; "
1579a149d2a5SZhang Yi 				   "itables initialized count: %ld",
1580a149d2a5SZhang Yi 				   group, ext4_itable_unused_count(sb, gdp),
1581a149d2a5SZhang Yi 				   used_inos);
1582a149d2a5SZhang Yi 			ret = 1;
1583a149d2a5SZhang Yi 			goto err_out;
1584a149d2a5SZhang Yi 		}
1585a149d2a5SZhang Yi 	}
1586a149d2a5SZhang Yi 
1587bfff6873SLukas Czerner 	blk = ext4_inode_table(sb, gdp) + used_blks;
1588bfff6873SLukas Czerner 	num = sbi->s_itb_per_group - used_blks;
1589bfff6873SLukas Czerner 
1590bfff6873SLukas Czerner 	BUFFER_TRACE(group_desc_bh, "get_write_access");
1591188c299eSJan Kara 	ret = ext4_journal_get_write_access(handle, sb, group_desc_bh,
1592188c299eSJan Kara 					    EXT4_JTR_NONE);
1593bfff6873SLukas Czerner 	if (ret)
1594bfff6873SLukas Czerner 		goto err_out;
1595bfff6873SLukas Czerner 
1596bfff6873SLukas Czerner 	/*
1597bfff6873SLukas Czerner 	 * Skip zeroout if the inode table is full. But we set the ZEROED
1598bfff6873SLukas Czerner 	 * flag anyway, because obviously, when it is full it does not need
1599bfff6873SLukas Czerner 	 * further zeroing.
1600bfff6873SLukas Czerner 	 */
1601bfff6873SLukas Czerner 	if (unlikely(num == 0))
1602bfff6873SLukas Czerner 		goto skip_zeroout;
1603bfff6873SLukas Czerner 
1604bfff6873SLukas Czerner 	ext4_debug("going to zero out inode table in group %d\n",
1605bfff6873SLukas Czerner 		   group);
1606a107e5a3STheodore Ts'o 	ret = sb_issue_zeroout(sb, blk, num, GFP_NOFS);
1607bfff6873SLukas Czerner 	if (ret < 0)
1608bfff6873SLukas Czerner 		goto err_out;
1609a107e5a3STheodore Ts'o 	if (barrier)
1610c6bf3f0eSChristoph Hellwig 		blkdev_issue_flush(sb->s_bdev);
1611bfff6873SLukas Czerner 
1612bfff6873SLukas Czerner skip_zeroout:
1613bfff6873SLukas Czerner 	ext4_lock_group(sb, group);
1614bfff6873SLukas Czerner 	gdp->bg_flags |= cpu_to_le16(EXT4_BG_INODE_ZEROED);
1615feb0ab32SDarrick J. Wong 	ext4_group_desc_csum_set(sb, group, gdp);
1616bfff6873SLukas Czerner 	ext4_unlock_group(sb, group);
1617bfff6873SLukas Czerner 
1618bfff6873SLukas Czerner 	BUFFER_TRACE(group_desc_bh,
1619bfff6873SLukas Czerner 		     "call ext4_handle_dirty_metadata");
1620bfff6873SLukas Czerner 	ret = ext4_handle_dirty_metadata(handle, NULL,
1621bfff6873SLukas Czerner 					 group_desc_bh);
1622bfff6873SLukas Czerner 
1623bfff6873SLukas Czerner err_out:
1624bfff6873SLukas Czerner 	up_write(&grp->alloc_sem);
1625bfff6873SLukas Czerner 	ext4_journal_stop(handle);
1626bfff6873SLukas Czerner out:
1627bfff6873SLukas Czerner 	return ret;
1628bfff6873SLukas Czerner }
1629