xref: /linux/fs/ext4/ialloc.c (revision 36ec807b627b4c0a0a382f0ae48eac7187d14b2b)
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;
945354b2afSTheodore Ts'o 	if (!grp || 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);
101b83acc77SKemeng Shi 	if (!ext4_inode_bitmap_csum_verify(sb, 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);
2965354b2afSTheodore Ts'o 		if (!grp || 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 	}
3304fd873c8SKemeng Shi 	ext4_inode_bitmap_csum_set(sb, 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));
8161df9bde4SKemeng Shi 			ext4_block_bitmap_csum_set(sb, gdp, block_bitmap_bh);
8178016e29fSHarshad Shirwadkar 			ext4_group_desc_csum_set(sb, group, gdp);
8188016e29fSHarshad Shirwadkar 		}
8198016e29fSHarshad Shirwadkar 		ext4_unlock_group(sb, group);
8208016e29fSHarshad Shirwadkar 		brelse(block_bitmap_bh);
8218016e29fSHarshad Shirwadkar 
8228016e29fSHarshad Shirwadkar 		if (err) {
8238016e29fSHarshad Shirwadkar 			ext4_std_error(sb, err);
8248016e29fSHarshad Shirwadkar 			goto out;
8258016e29fSHarshad Shirwadkar 		}
8268016e29fSHarshad Shirwadkar 	}
8278016e29fSHarshad Shirwadkar 
8288016e29fSHarshad Shirwadkar 	/* Update the relevant bg descriptor fields */
8298016e29fSHarshad Shirwadkar 	if (ext4_has_group_desc_csum(sb)) {
8308016e29fSHarshad Shirwadkar 		int free;
8318016e29fSHarshad Shirwadkar 
8328016e29fSHarshad Shirwadkar 		ext4_lock_group(sb, group); /* while we modify the bg desc */
8338016e29fSHarshad Shirwadkar 		free = EXT4_INODES_PER_GROUP(sb) -
8348016e29fSHarshad Shirwadkar 			ext4_itable_unused_count(sb, gdp);
8358016e29fSHarshad Shirwadkar 		if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
8368016e29fSHarshad Shirwadkar 			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
8378016e29fSHarshad Shirwadkar 			free = 0;
8388016e29fSHarshad Shirwadkar 		}
8398016e29fSHarshad Shirwadkar 
8408016e29fSHarshad Shirwadkar 		/*
8418016e29fSHarshad Shirwadkar 		 * Check the relative inode number against the last used
8428016e29fSHarshad Shirwadkar 		 * relative inode number in this group. if it is greater
8438016e29fSHarshad Shirwadkar 		 * we need to update the bg_itable_unused count
8448016e29fSHarshad Shirwadkar 		 */
8458016e29fSHarshad Shirwadkar 		if (bit >= free)
8468016e29fSHarshad Shirwadkar 			ext4_itable_unused_set(sb, gdp,
8478016e29fSHarshad Shirwadkar 					(EXT4_INODES_PER_GROUP(sb) - bit - 1));
8488016e29fSHarshad Shirwadkar 	} else {
8498016e29fSHarshad Shirwadkar 		ext4_lock_group(sb, group);
8508016e29fSHarshad Shirwadkar 	}
8518016e29fSHarshad Shirwadkar 
8528016e29fSHarshad Shirwadkar 	ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1);
8538016e29fSHarshad Shirwadkar 	if (ext4_has_group_desc_csum(sb)) {
8544fd873c8SKemeng Shi 		ext4_inode_bitmap_csum_set(sb, gdp, inode_bitmap_bh,
8558016e29fSHarshad Shirwadkar 					   EXT4_INODES_PER_GROUP(sb) / 8);
8568016e29fSHarshad Shirwadkar 		ext4_group_desc_csum_set(sb, group, gdp);
8578016e29fSHarshad Shirwadkar 	}
8588016e29fSHarshad Shirwadkar 
8598016e29fSHarshad Shirwadkar 	ext4_unlock_group(sb, group);
8608016e29fSHarshad Shirwadkar 	err = ext4_handle_dirty_metadata(NULL, NULL, group_desc_bh);
8618016e29fSHarshad Shirwadkar 	sync_dirty_buffer(group_desc_bh);
8628016e29fSHarshad Shirwadkar out:
8638016e29fSHarshad Shirwadkar 	return err;
8648016e29fSHarshad Shirwadkar }
8658016e29fSHarshad Shirwadkar 
866177cc0e7SEric Biggers static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
867177cc0e7SEric Biggers 					    bool encrypt)
868177cc0e7SEric Biggers {
869177cc0e7SEric Biggers 	struct super_block *sb = dir->i_sb;
870177cc0e7SEric Biggers 	int nblocks = 0;
871177cc0e7SEric Biggers #ifdef CONFIG_EXT4_FS_POSIX_ACL
872cac2f8b8SChristian Brauner 	struct posix_acl *p = get_inode_acl(dir, ACL_TYPE_DEFAULT);
873177cc0e7SEric Biggers 
874177cc0e7SEric Biggers 	if (IS_ERR(p))
875177cc0e7SEric Biggers 		return PTR_ERR(p);
876177cc0e7SEric Biggers 	if (p) {
877177cc0e7SEric Biggers 		int acl_size = p->a_count * sizeof(ext4_acl_entry);
878177cc0e7SEric Biggers 
879177cc0e7SEric Biggers 		nblocks += (S_ISDIR(mode) ? 2 : 1) *
880177cc0e7SEric Biggers 			__ext4_xattr_set_credits(sb, NULL /* inode */,
881177cc0e7SEric Biggers 						 NULL /* block_bh */, acl_size,
882177cc0e7SEric Biggers 						 true /* is_create */);
883177cc0e7SEric Biggers 		posix_acl_release(p);
884177cc0e7SEric Biggers 	}
885177cc0e7SEric Biggers #endif
886177cc0e7SEric Biggers 
887177cc0e7SEric Biggers #ifdef CONFIG_SECURITY
888177cc0e7SEric Biggers 	{
889177cc0e7SEric Biggers 		int num_security_xattrs = 1;
890177cc0e7SEric Biggers 
891177cc0e7SEric Biggers #ifdef CONFIG_INTEGRITY
892177cc0e7SEric Biggers 		num_security_xattrs++;
893177cc0e7SEric Biggers #endif
894177cc0e7SEric Biggers 		/*
895177cc0e7SEric Biggers 		 * We assume that security xattrs are never more than 1k.
896177cc0e7SEric Biggers 		 * In practice they are under 128 bytes.
897177cc0e7SEric Biggers 		 */
898177cc0e7SEric Biggers 		nblocks += num_security_xattrs *
899177cc0e7SEric Biggers 			__ext4_xattr_set_credits(sb, NULL /* inode */,
900177cc0e7SEric Biggers 						 NULL /* block_bh */, 1024,
901177cc0e7SEric Biggers 						 true /* is_create */);
902177cc0e7SEric Biggers 	}
903177cc0e7SEric Biggers #endif
904177cc0e7SEric Biggers 	if (encrypt)
905177cc0e7SEric Biggers 		nblocks += __ext4_xattr_set_credits(sb,
906177cc0e7SEric Biggers 						    NULL /* inode */,
907177cc0e7SEric Biggers 						    NULL /* block_bh */,
908177cc0e7SEric Biggers 						    FSCRYPT_SET_CONTEXT_MAX_SIZE,
909177cc0e7SEric Biggers 						    true /* is_create */);
910177cc0e7SEric Biggers 	return nblocks;
911177cc0e7SEric Biggers }
912177cc0e7SEric Biggers 
91319883bd9STheodore Ts'o /*
914ac27a0ecSDave Kleikamp  * There are two policies for allocating an inode.  If the new inode is
915ac27a0ecSDave Kleikamp  * a directory, then a forward search is made for a block group with both
916ac27a0ecSDave Kleikamp  * free space and a low directory-to-inode ratio; if that fails, then of
917ac27a0ecSDave Kleikamp  * the groups with above-average free space, that group with the fewest
918ac27a0ecSDave Kleikamp  * directories already is chosen.
919ac27a0ecSDave Kleikamp  *
920ac27a0ecSDave Kleikamp  * For other inodes, search forward from the parent directory's block
921ac27a0ecSDave Kleikamp  * group to find a free inode.
922ac27a0ecSDave Kleikamp  */
923f2d40141SChristian Brauner struct inode *__ext4_new_inode(struct mnt_idmap *idmap,
92414f3db55SChristian Brauner 			       handle_t *handle, struct inode *dir,
9251139575aSTheodore Ts'o 			       umode_t mode, const struct qstr *qstr,
9261b917ed8STahsin Erdogan 			       __u32 goal, uid_t *owner, __u32 i_flags,
9271b917ed8STahsin Erdogan 			       int handle_type, unsigned int line_no,
9281b917ed8STahsin Erdogan 			       int nblocks)
929ac27a0ecSDave Kleikamp {
930ac27a0ecSDave Kleikamp 	struct super_block *sb;
9313300bedaSAneesh Kumar K.V 	struct buffer_head *inode_bitmap_bh = NULL;
9323300bedaSAneesh Kumar K.V 	struct buffer_head *group_desc_bh;
9338df9675fSTheodore Ts'o 	ext4_group_t ngroups, group = 0;
934ac27a0ecSDave Kleikamp 	unsigned long ino = 0;
935ac27a0ecSDave Kleikamp 	struct inode *inode;
936617ba13bSMingming Cao 	struct ext4_group_desc *gdp = NULL;
937617ba13bSMingming Cao 	struct ext4_inode_info *ei;
938617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
939a7cdadeeSJan Kara 	int ret2, err;
940ac27a0ecSDave Kleikamp 	struct inode *ret;
9412aa9fc4cSAvantika Mathur 	ext4_group_t i;
942772cb7c8SJose R. Santos 	ext4_group_t flex_group;
9438016e29fSHarshad Shirwadkar 	struct ext4_group_info *grp = NULL;
94402ce5316SEric Biggers 	bool encrypt = false;
945ac27a0ecSDave Kleikamp 
946ac27a0ecSDave Kleikamp 	/* Cannot create files in a deleted directory */
947ac27a0ecSDave Kleikamp 	if (!dir || !dir->i_nlink)
948ac27a0ecSDave Kleikamp 		return ERR_PTR(-EPERM);
949ac27a0ecSDave Kleikamp 
950af65207cSTahsin Erdogan 	sb = dir->i_sb;
951af65207cSTahsin Erdogan 	sbi = EXT4_SB(sb);
952af65207cSTahsin Erdogan 
953eb8ab444SJan Kara 	if (unlikely(ext4_forced_shutdown(sb)))
9540db1ff22STheodore Ts'o 		return ERR_PTR(-EIO);
9550db1ff22STheodore Ts'o 
9568df9675fSTheodore Ts'o 	ngroups = ext4_get_groups_count(sb);
9579bffad1eSTheodore Ts'o 	trace_ext4_request_inode(dir, mode);
958ac27a0ecSDave Kleikamp 	inode = new_inode(sb);
959ac27a0ecSDave Kleikamp 	if (!inode)
960ac27a0ecSDave Kleikamp 		return ERR_PTR(-ENOMEM);
961617ba13bSMingming Cao 	ei = EXT4_I(inode);
962772cb7c8SJose R. Santos 
963eb9cc7e1SJan Kara 	/*
964b8a07463SAdam Buchbinder 	 * Initialize owners and quota early so that we don't have to account
965eb9cc7e1SJan Kara 	 * for quota initialization worst case in standard inode creating
966eb9cc7e1SJan Kara 	 * transaction
967eb9cc7e1SJan Kara 	 */
968eb9cc7e1SJan Kara 	if (owner) {
969eb9cc7e1SJan Kara 		inode->i_mode = mode;
970eb9cc7e1SJan Kara 		i_uid_write(inode, owner[0]);
971eb9cc7e1SJan Kara 		i_gid_write(inode, owner[1]);
972eb9cc7e1SJan Kara 	} else if (test_opt(sb, GRPID)) {
973eb9cc7e1SJan Kara 		inode->i_mode = mode;
974c14329d3SChristian Brauner 		inode_fsuid_set(inode, idmap);
975eb9cc7e1SJan Kara 		inode->i_gid = dir->i_gid;
976eb9cc7e1SJan Kara 	} else
977f2d40141SChristian Brauner 		inode_init_owner(idmap, inode, dir, mode);
978040cb378SLi Xi 
9790b7b7779SKaho Ng 	if (ext4_has_feature_project(sb) &&
980040cb378SLi Xi 	    ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT))
981040cb378SLi Xi 		ei->i_projid = EXT4_I(dir)->i_projid;
982040cb378SLi Xi 	else
983040cb378SLi Xi 		ei->i_projid = make_kprojid(&init_user_ns, EXT4_DEF_PROJID);
984040cb378SLi Xi 
98502ce5316SEric Biggers 	if (!(i_flags & EXT4_EA_INODE_FL)) {
98602ce5316SEric Biggers 		err = fscrypt_prepare_new_inode(dir, inode, &encrypt);
98702ce5316SEric Biggers 		if (err)
98802ce5316SEric Biggers 			goto out;
98902ce5316SEric Biggers 	}
99002ce5316SEric Biggers 
991a7cdadeeSJan Kara 	err = dquot_initialize(inode);
992a7cdadeeSJan Kara 	if (err)
993a7cdadeeSJan Kara 		goto out;
994eb9cc7e1SJan Kara 
99502ce5316SEric Biggers 	if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) {
99602ce5316SEric Biggers 		ret2 = ext4_xattr_credits_for_new_inode(dir, mode, encrypt);
99702ce5316SEric Biggers 		if (ret2 < 0) {
99802ce5316SEric Biggers 			err = ret2;
99902ce5316SEric Biggers 			goto out;
100002ce5316SEric Biggers 		}
100102ce5316SEric Biggers 		nblocks += ret2;
100202ce5316SEric Biggers 	}
100302ce5316SEric Biggers 
100411013911SAndreas Dilger 	if (!goal)
100511013911SAndreas Dilger 		goal = sbi->s_inode_goal;
100611013911SAndreas Dilger 
1007e6462869SJohann Lombardi 	if (goal && goal <= le32_to_cpu(sbi->s_es->s_inodes_count)) {
100811013911SAndreas Dilger 		group = (goal - 1) / EXT4_INODES_PER_GROUP(sb);
100911013911SAndreas Dilger 		ino = (goal - 1) % EXT4_INODES_PER_GROUP(sb);
101011013911SAndreas Dilger 		ret2 = 0;
101111013911SAndreas Dilger 		goto got_group;
101211013911SAndreas Dilger 	}
101311013911SAndreas Dilger 
10144113c4caSLukas Czerner 	if (S_ISDIR(mode))
1015f157a4aaSTheodore Ts'o 		ret2 = find_group_orlov(sb, dir, &group, mode, qstr);
10164113c4caSLukas Czerner 	else
1017a4912123STheodore Ts'o 		ret2 = find_group_other(sb, dir, &group, mode);
1018ac27a0ecSDave Kleikamp 
1019772cb7c8SJose R. Santos got_group:
1020a4912123STheodore Ts'o 	EXT4_I(dir)->i_last_alloc_group = group;
1021ac27a0ecSDave Kleikamp 	err = -ENOSPC;
10222aa9fc4cSAvantika Mathur 	if (ret2 == -1)
1023ac27a0ecSDave Kleikamp 		goto out;
1024ac27a0ecSDave Kleikamp 
1025119c0d44STheodore Ts'o 	/*
1026119c0d44STheodore Ts'o 	 * Normally we will only go through one pass of this loop,
1027119c0d44STheodore Ts'o 	 * unless we get unlucky and it turns out the group we selected
1028119c0d44STheodore Ts'o 	 * had its last inode grabbed by someone else.
1029119c0d44STheodore Ts'o 	 */
103011013911SAndreas Dilger 	for (i = 0; i < ngroups; i++, ino = 0) {
1031ac27a0ecSDave Kleikamp 		err = -EIO;
1032ac27a0ecSDave Kleikamp 
10333300bedaSAneesh Kumar K.V 		gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
1034ac27a0ecSDave Kleikamp 		if (!gdp)
1035eb9cc7e1SJan Kara 			goto out;
1036ac27a0ecSDave Kleikamp 
1037f2a09af6SYongqiang Yang 		/*
1038f2a09af6SYongqiang Yang 		 * Check free inodes count before loading bitmap.
1039f2a09af6SYongqiang Yang 		 */
10402fe435d8SWang Shilong 		if (ext4_free_inodes_count(sb, gdp) == 0)
10412fe435d8SWang Shilong 			goto next_group;
1042f2a09af6SYongqiang Yang 
10438016e29fSHarshad Shirwadkar 		if (!(sbi->s_mount_state & EXT4_FC_REPLAY)) {
104487a39389SDarrick J. Wong 			grp = ext4_get_group_info(sb, group);
10458016e29fSHarshad Shirwadkar 			/*
10468016e29fSHarshad Shirwadkar 			 * Skip groups with already-known suspicious inode
10478016e29fSHarshad Shirwadkar 			 * tables
10488016e29fSHarshad Shirwadkar 			 */
10495354b2afSTheodore Ts'o 			if (!grp || EXT4_MB_GRP_IBITMAP_CORRUPT(grp))
10502fe435d8SWang Shilong 				goto next_group;
10518016e29fSHarshad Shirwadkar 		}
105287a39389SDarrick J. Wong 
10533300bedaSAneesh Kumar K.V 		brelse(inode_bitmap_bh);
10543300bedaSAneesh Kumar K.V 		inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
105587a39389SDarrick J. Wong 		/* Skip groups with suspicious inode tables */
10568016e29fSHarshad Shirwadkar 		if (((!(sbi->s_mount_state & EXT4_FC_REPLAY))
10578016e29fSHarshad Shirwadkar 		     && EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) ||
10589008a58eSDarrick J. Wong 		    IS_ERR(inode_bitmap_bh)) {
10599008a58eSDarrick J. Wong 			inode_bitmap_bh = NULL;
10602fe435d8SWang Shilong 			goto next_group;
106187a39389SDarrick J. Wong 		}
1062ac27a0ecSDave Kleikamp 
1063ac27a0ecSDave Kleikamp repeat_in_this_group:
1064901ed070SWang Shilong 		ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino);
1065901ed070SWang Shilong 		if (!ret2)
1066a34eb503STheodore Ts'o 			goto next_group;
1067901ed070SWang Shilong 
1068119c0d44STheodore Ts'o 		if (group == 0 && (ino + 1) < EXT4_FIRST_INO(sb)) {
1069119c0d44STheodore Ts'o 			ext4_error(sb, "reserved inode found cleared - "
1070119c0d44STheodore Ts'o 				   "inode=%lu", ino + 1);
1071206f6d55SWang Shilong 			ext4_mark_group_bitmap_corrupted(sb, group,
1072206f6d55SWang Shilong 					EXT4_GROUP_INFO_IBITMAP_CORRUPT);
10732fe435d8SWang Shilong 			goto next_group;
1074119c0d44STheodore Ts'o 		}
1075901ed070SWang Shilong 
10768016e29fSHarshad Shirwadkar 		if ((!(sbi->s_mount_state & EXT4_FC_REPLAY)) && !handle) {
10771139575aSTheodore Ts'o 			BUG_ON(nblocks <= 0);
10785f3e2403Schangfengnan 			handle = __ext4_journal_start_sb(NULL, dir->i_sb,
10795f3e2403Schangfengnan 				 line_no, handle_type, nblocks, 0,
1080a70fd5acSyangerkun 				 ext4_trans_default_revoke_credits(sb));
10811139575aSTheodore Ts'o 			if (IS_ERR(handle)) {
10821139575aSTheodore Ts'o 				err = PTR_ERR(handle);
1083eb9cc7e1SJan Kara 				ext4_std_error(sb, err);
1084eb9cc7e1SJan Kara 				goto out;
10851139575aSTheodore Ts'o 			}
10861139575aSTheodore Ts'o 		}
1087ffb5387eSEric Sandeen 		BUFFER_TRACE(inode_bitmap_bh, "get_write_access");
1088188c299eSJan Kara 		err = ext4_journal_get_write_access(handle, sb, inode_bitmap_bh,
1089188c299eSJan Kara 						    EXT4_JTR_NONE);
1090eb9cc7e1SJan Kara 		if (err) {
1091eb9cc7e1SJan Kara 			ext4_std_error(sb, err);
1092eb9cc7e1SJan Kara 			goto out;
1093eb9cc7e1SJan Kara 		}
1094119c0d44STheodore Ts'o 		ext4_lock_group(sb, group);
1095119c0d44STheodore Ts'o 		ret2 = ext4_test_and_set_bit(ino, inode_bitmap_bh->b_data);
1096901ed070SWang Shilong 		if (ret2) {
1097901ed070SWang Shilong 			/* Someone already took the bit. Repeat the search
1098901ed070SWang Shilong 			 * with lock held.
1099901ed070SWang Shilong 			 */
1100901ed070SWang Shilong 			ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino);
1101901ed070SWang Shilong 			if (ret2) {
1102901ed070SWang Shilong 				ext4_set_bit(ino, inode_bitmap_bh->b_data);
1103901ed070SWang Shilong 				ret2 = 0;
1104901ed070SWang Shilong 			} else {
1105901ed070SWang Shilong 				ret2 = 1; /* we didn't grab the inode */
1106901ed070SWang Shilong 			}
1107901ed070SWang Shilong 		}
1108119c0d44STheodore Ts'o 		ext4_unlock_group(sb, group);
1109119c0d44STheodore Ts'o 		ino++;		/* the inode bitmap is zero-based */
1110119c0d44STheodore Ts'o 		if (!ret2)
1111119c0d44STheodore Ts'o 			goto got; /* we grabbed the inode! */
1112901ed070SWang Shilong 
1113119c0d44STheodore Ts'o 		if (ino < EXT4_INODES_PER_GROUP(sb))
1114119c0d44STheodore Ts'o 			goto repeat_in_this_group;
1115a34eb503STheodore Ts'o next_group:
1116a34eb503STheodore Ts'o 		if (++group == ngroups)
1117a34eb503STheodore Ts'o 			group = 0;
1118ac27a0ecSDave Kleikamp 	}
1119ac27a0ecSDave Kleikamp 	err = -ENOSPC;
1120ac27a0ecSDave Kleikamp 	goto out;
1121ac27a0ecSDave Kleikamp 
1122ac27a0ecSDave Kleikamp got:
1123ffb5387eSEric Sandeen 	BUFFER_TRACE(inode_bitmap_bh, "call ext4_handle_dirty_metadata");
1124ffb5387eSEric Sandeen 	err = ext4_handle_dirty_metadata(handle, NULL, inode_bitmap_bh);
1125eb9cc7e1SJan Kara 	if (err) {
1126eb9cc7e1SJan Kara 		ext4_std_error(sb, err);
1127eb9cc7e1SJan Kara 		goto out;
1128eb9cc7e1SJan Kara 	}
1129ffb5387eSEric Sandeen 
113061c219f5STheodore Ts'o 	BUFFER_TRACE(group_desc_bh, "get_write_access");
1131188c299eSJan Kara 	err = ext4_journal_get_write_access(handle, sb, group_desc_bh,
1132188c299eSJan Kara 					    EXT4_JTR_NONE);
113361c219f5STheodore Ts'o 	if (err) {
113461c219f5STheodore Ts'o 		ext4_std_error(sb, err);
113561c219f5STheodore Ts'o 		goto out;
113661c219f5STheodore Ts'o 	}
113761c219f5STheodore Ts'o 
1138717d50e4SAndreas Dilger 	/* We may have to initialize the block bitmap if it isn't already */
1139feb0ab32SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb) &&
1140717d50e4SAndreas Dilger 	    gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
11413300bedaSAneesh Kumar K.V 		struct buffer_head *block_bitmap_bh;
1142717d50e4SAndreas Dilger 
11433300bedaSAneesh Kumar K.V 		block_bitmap_bh = ext4_read_block_bitmap(sb, group);
11449008a58eSDarrick J. Wong 		if (IS_ERR(block_bitmap_bh)) {
11459008a58eSDarrick J. Wong 			err = PTR_ERR(block_bitmap_bh);
1146599a9b77SJan Kara 			goto out;
1147599a9b77SJan Kara 		}
11483300bedaSAneesh Kumar K.V 		BUFFER_TRACE(block_bitmap_bh, "get block bitmap access");
1149188c299eSJan Kara 		err = ext4_journal_get_write_access(handle, sb, block_bitmap_bh,
1150188c299eSJan Kara 						    EXT4_JTR_NONE);
1151717d50e4SAndreas Dilger 		if (err) {
11523300bedaSAneesh Kumar K.V 			brelse(block_bitmap_bh);
1153eb9cc7e1SJan Kara 			ext4_std_error(sb, err);
1154eb9cc7e1SJan Kara 			goto out;
1155717d50e4SAndreas Dilger 		}
1156717d50e4SAndreas Dilger 
1157fd034a84STheodore Ts'o 		BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
1158fd034a84STheodore Ts'o 		err = ext4_handle_dirty_metadata(handle, NULL, block_bitmap_bh);
1159fd034a84STheodore Ts'o 
1160717d50e4SAndreas Dilger 		/* recheck and clear flag under lock if we still need to */
1161fd034a84STheodore Ts'o 		ext4_lock_group(sb, group);
11628844618dSTheodore Ts'o 		if (ext4_has_group_desc_csum(sb) &&
11638844618dSTheodore Ts'o 		    (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
11643300bedaSAneesh Kumar K.V 			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
1165021b65bbSTheodore Ts'o 			ext4_free_group_clusters_set(sb, gdp,
1166cff1dfd7STheodore Ts'o 				ext4_free_clusters_after_init(sb, group, gdp));
11671df9bde4SKemeng Shi 			ext4_block_bitmap_csum_set(sb, gdp, block_bitmap_bh);
1168feb0ab32SDarrick J. Wong 			ext4_group_desc_csum_set(sb, group, gdp);
1169717d50e4SAndreas Dilger 		}
1170955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, group);
1171aeb1e5d6STheodore Ts'o 		brelse(block_bitmap_bh);
1172717d50e4SAndreas Dilger 
1173eb9cc7e1SJan Kara 		if (err) {
1174eb9cc7e1SJan Kara 			ext4_std_error(sb, err);
1175eb9cc7e1SJan Kara 			goto out;
1176eb9cc7e1SJan Kara 		}
1177717d50e4SAndreas Dilger 	}
1178119c0d44STheodore Ts'o 
1179119c0d44STheodore Ts'o 	/* Update the relevant bg descriptor fields */
118041a246d1SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb)) {
1181119c0d44STheodore Ts'o 		int free;
11828016e29fSHarshad Shirwadkar 		struct ext4_group_info *grp = NULL;
1183119c0d44STheodore Ts'o 
11848016e29fSHarshad Shirwadkar 		if (!(sbi->s_mount_state & EXT4_FC_REPLAY)) {
11858016e29fSHarshad Shirwadkar 			grp = ext4_get_group_info(sb, group);
11865354b2afSTheodore Ts'o 			if (!grp) {
11875354b2afSTheodore Ts'o 				err = -EFSCORRUPTED;
11885354b2afSTheodore Ts'o 				goto out;
11895354b2afSTheodore Ts'o 			}
11908016e29fSHarshad Shirwadkar 			down_read(&grp->alloc_sem); /*
11918016e29fSHarshad Shirwadkar 						     * protect vs itable
11928016e29fSHarshad Shirwadkar 						     * lazyinit
11938016e29fSHarshad Shirwadkar 						     */
11948016e29fSHarshad Shirwadkar 		}
1195119c0d44STheodore Ts'o 		ext4_lock_group(sb, group); /* while we modify the bg desc */
1196119c0d44STheodore Ts'o 		free = EXT4_INODES_PER_GROUP(sb) -
1197119c0d44STheodore Ts'o 			ext4_itable_unused_count(sb, gdp);
1198119c0d44STheodore Ts'o 		if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
1199119c0d44STheodore Ts'o 			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
1200119c0d44STheodore Ts'o 			free = 0;
1201119c0d44STheodore Ts'o 		}
1202119c0d44STheodore Ts'o 		/*
1203119c0d44STheodore Ts'o 		 * Check the relative inode number against the last used
1204119c0d44STheodore Ts'o 		 * relative inode number in this group. if it is greater
1205119c0d44STheodore Ts'o 		 * we need to update the bg_itable_unused count
1206119c0d44STheodore Ts'o 		 */
1207119c0d44STheodore Ts'o 		if (ino > free)
1208119c0d44STheodore Ts'o 			ext4_itable_unused_set(sb, gdp,
1209119c0d44STheodore Ts'o 					(EXT4_INODES_PER_GROUP(sb) - ino));
12108016e29fSHarshad Shirwadkar 		if (!(sbi->s_mount_state & EXT4_FC_REPLAY))
1211119c0d44STheodore Ts'o 			up_read(&grp->alloc_sem);
12126f2e9f0eSTao Ma 	} else {
12136f2e9f0eSTao Ma 		ext4_lock_group(sb, group);
1214119c0d44STheodore Ts'o 	}
12156f2e9f0eSTao Ma 
1216119c0d44STheodore Ts'o 	ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1);
1217119c0d44STheodore Ts'o 	if (S_ISDIR(mode)) {
1218119c0d44STheodore Ts'o 		ext4_used_dirs_set(sb, gdp, ext4_used_dirs_count(sb, gdp) + 1);
1219119c0d44STheodore Ts'o 		if (sbi->s_log_groups_per_flex) {
1220119c0d44STheodore Ts'o 			ext4_group_t f = ext4_flex_group(sbi, group);
1221119c0d44STheodore Ts'o 
12227c990728SSuraj Jitindar Singh 			atomic_inc(&sbi_array_rcu_deref(sbi, s_flex_groups,
12237c990728SSuraj Jitindar Singh 							f)->used_dirs);
1224119c0d44STheodore Ts'o 		}
1225119c0d44STheodore Ts'o 	}
122641a246d1SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb)) {
12274fd873c8SKemeng Shi 		ext4_inode_bitmap_csum_set(sb, gdp, inode_bitmap_bh,
122841a246d1SDarrick J. Wong 					   EXT4_INODES_PER_GROUP(sb) / 8);
1229feb0ab32SDarrick J. Wong 		ext4_group_desc_csum_set(sb, group, gdp);
1230119c0d44STheodore Ts'o 	}
12316f2e9f0eSTao Ma 	ext4_unlock_group(sb, group);
1232119c0d44STheodore Ts'o 
12333300bedaSAneesh Kumar K.V 	BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata");
12343300bedaSAneesh Kumar K.V 	err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh);
1235eb9cc7e1SJan Kara 	if (err) {
1236eb9cc7e1SJan Kara 		ext4_std_error(sb, err);
1237eb9cc7e1SJan Kara 		goto out;
1238eb9cc7e1SJan Kara 	}
1239ac27a0ecSDave Kleikamp 
1240ac27a0ecSDave Kleikamp 	percpu_counter_dec(&sbi->s_freeinodes_counter);
1241ac27a0ecSDave Kleikamp 	if (S_ISDIR(mode))
1242ac27a0ecSDave Kleikamp 		percpu_counter_inc(&sbi->s_dirs_counter);
1243ac27a0ecSDave Kleikamp 
1244772cb7c8SJose R. Santos 	if (sbi->s_log_groups_per_flex) {
1245772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, group);
12467c990728SSuraj Jitindar Singh 		atomic_dec(&sbi_array_rcu_deref(sbi, s_flex_groups,
12477c990728SSuraj Jitindar Singh 						flex_group)->free_inodes);
1248772cb7c8SJose R. Santos 	}
1249ac27a0ecSDave Kleikamp 
1250717d50e4SAndreas Dilger 	inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
1251ac27a0ecSDave Kleikamp 	/* This is the optimal IO size (for stat), not the fs block size */
1252ac27a0ecSDave Kleikamp 	inode->i_blocks = 0;
1253b898ab23SJeff Layton 	simple_inode_init_ts(inode);
1254b898ab23SJeff Layton 	ei->i_crtime = inode_get_mtime(inode);
1255ac27a0ecSDave Kleikamp 
1256ac27a0ecSDave Kleikamp 	memset(ei->i_data, 0, sizeof(ei->i_data));
1257ac27a0ecSDave Kleikamp 	ei->i_dir_start_lookup = 0;
1258ac27a0ecSDave Kleikamp 	ei->i_disksize = 0;
1259ac27a0ecSDave Kleikamp 
12604af83508SEryu Guan 	/* Don't inherit extent flag from directory, amongst others. */
12612dc6b0d4SDuane Griffin 	ei->i_flags =
12622dc6b0d4SDuane Griffin 		ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED);
12631b917ed8STahsin Erdogan 	ei->i_flags |= i_flags;
1264ac27a0ecSDave Kleikamp 	ei->i_file_acl = 0;
1265ac27a0ecSDave Kleikamp 	ei->i_dtime = 0;
1266ac27a0ecSDave Kleikamp 	ei->i_block_group = group;
1267a4912123STheodore Ts'o 	ei->i_last_alloc_group = ~0;
1268ac27a0ecSDave Kleikamp 
1269043546e4SIra Weiny 	ext4_set_inode_flags(inode, true);
1270ac27a0ecSDave Kleikamp 	if (IS_DIRSYNC(inode))
12710390131bSFrank Mayhar 		ext4_handle_sync(handle);
12726b38e842SAl Viro 	if (insert_inode_locked(inode) < 0) {
1273acd6ad83SJan Kara 		/*
1274acd6ad83SJan Kara 		 * Likely a bitmap corruption causing inode to be allocated
1275acd6ad83SJan Kara 		 * twice.
1276acd6ad83SJan Kara 		 */
1277acd6ad83SJan Kara 		err = -EIO;
1278eb9cc7e1SJan Kara 		ext4_error(sb, "failed to insert inode %lu: doubly allocated?",
1279eb9cc7e1SJan Kara 			   inode->i_ino);
1280206f6d55SWang Shilong 		ext4_mark_group_bitmap_corrupted(sb, group,
1281206f6d55SWang Shilong 					EXT4_GROUP_INFO_IBITMAP_CORRUPT);
1282eb9cc7e1SJan Kara 		goto out;
12836b38e842SAl Viro 	}
1284a251c17aSJason A. Donenfeld 	inode->i_generation = get_random_u32();
1285ac27a0ecSDave Kleikamp 
1286814525f4SDarrick J. Wong 	/* Precompute checksum seed for inode metadata */
12879aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sb)) {
1288814525f4SDarrick J. Wong 		__u32 csum;
1289814525f4SDarrick J. Wong 		__le32 inum = cpu_to_le32(inode->i_ino);
1290814525f4SDarrick J. Wong 		__le32 gen = cpu_to_le32(inode->i_generation);
1291814525f4SDarrick J. Wong 		csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
1292814525f4SDarrick J. Wong 				   sizeof(inum));
1293814525f4SDarrick J. Wong 		ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
1294814525f4SDarrick J. Wong 					      sizeof(gen));
1295814525f4SDarrick J. Wong 	}
1296814525f4SDarrick J. Wong 
1297353eb83cSTheodore Ts'o 	ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
129819f5fb7aSTheodore Ts'o 	ext4_set_inode_state(inode, EXT4_STATE_NEW);
1299ef7f3835SKalpak Shah 
130049598e04SJun Piao 	ei->i_extra_isize = sbi->s_want_extra_isize;
1301f08225d1STao Ma 	ei->i_inline_off = 0;
13024811d992STheodore Ts'o 	if (ext4_has_feature_inline_data(sb) &&
13034811d992STheodore Ts'o 	    (!(ei->i_flags & EXT4_DAX_FL) || S_ISDIR(mode)))
1304f08225d1STao Ma 		ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
1305ac27a0ecSDave Kleikamp 	ret = inode;
130663936ddaSChristoph Hellwig 	err = dquot_alloc_inode(inode);
130763936ddaSChristoph Hellwig 	if (err)
1308ac27a0ecSDave Kleikamp 		goto fail_drop;
1309ac27a0ecSDave Kleikamp 
1310aa1dca3bSEric Biggers 	/*
1311aa1dca3bSEric Biggers 	 * Since the encryption xattr will always be unique, create it first so
1312aa1dca3bSEric Biggers 	 * that it's less likely to end up in an external xattr block and
1313aa1dca3bSEric Biggers 	 * prevent its deduplication.
1314aa1dca3bSEric Biggers 	 */
1315aa1dca3bSEric Biggers 	if (encrypt) {
131602ce5316SEric Biggers 		err = fscrypt_set_context(inode, handle);
1317aa1dca3bSEric Biggers 		if (err)
1318aa1dca3bSEric Biggers 			goto fail_free_drop;
1319aa1dca3bSEric Biggers 	}
1320aa1dca3bSEric Biggers 
13211b917ed8STahsin Erdogan 	if (!(ei->i_flags & EXT4_EA_INODE_FL)) {
1322617ba13bSMingming Cao 		err = ext4_init_acl(handle, inode, dir);
1323ac27a0ecSDave Kleikamp 		if (err)
1324ac27a0ecSDave Kleikamp 			goto fail_free_drop;
1325ac27a0ecSDave Kleikamp 
13262a7dba39SEric Paris 		err = ext4_init_security(handle, inode, dir, qstr);
1327ac27a0ecSDave Kleikamp 		if (err)
1328ac27a0ecSDave Kleikamp 			goto fail_free_drop;
1329ad47f953STahsin Erdogan 	}
1330ac27a0ecSDave Kleikamp 
1331e2b911c5SDarrick J. Wong 	if (ext4_has_feature_extents(sb)) {
1332e4079a11SEric Sandeen 		/* set extent flag only for directory, file and normal symlink*/
1333e65187e6SAneesh Kumar K.V 		if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
133412e9b892SDmitry Monakhov 			ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
1335a86c6181SAlex Tomas 			ext4_ext_tree_init(handle, inode);
1336a86c6181SAlex Tomas 		}
133742bf0383SAneesh Kumar K.V 	}
1338ac27a0ecSDave Kleikamp 
1339*2d4d6bdaSLuis Henriques (SUSE) 	ext4_update_inode_fsync_trans(handle, inode, 1);
1340688f869cSTheodore Ts'o 
13418753e88fSAneesh Kumar K.V 	err = ext4_mark_inode_dirty(handle, inode);
13428753e88fSAneesh Kumar K.V 	if (err) {
13438753e88fSAneesh Kumar K.V 		ext4_std_error(sb, err);
13448753e88fSAneesh Kumar K.V 		goto fail_free_drop;
13458753e88fSAneesh Kumar K.V 	}
13468753e88fSAneesh Kumar K.V 
1347617ba13bSMingming Cao 	ext4_debug("allocating inode %lu\n", inode->i_ino);
13489bffad1eSTheodore Ts'o 	trace_ext4_allocate_inode(inode, dir, mode);
13493300bedaSAneesh Kumar K.V 	brelse(inode_bitmap_bh);
1350ac27a0ecSDave Kleikamp 	return ret;
1351ac27a0ecSDave Kleikamp 
1352ac27a0ecSDave Kleikamp fail_free_drop:
135363936ddaSChristoph Hellwig 	dquot_free_inode(inode);
1354ac27a0ecSDave Kleikamp fail_drop:
13556d6b77f1SMiklos Szeredi 	clear_nlink(inode);
13566b38e842SAl Viro 	unlock_new_inode(inode);
1357eb9cc7e1SJan Kara out:
1358eb9cc7e1SJan Kara 	dquot_drop(inode);
1359eb9cc7e1SJan Kara 	inode->i_flags |= S_NOQUOTA;
1360ac27a0ecSDave Kleikamp 	iput(inode);
13613300bedaSAneesh Kumar K.V 	brelse(inode_bitmap_bh);
1362ac27a0ecSDave Kleikamp 	return ERR_PTR(err);
1363ac27a0ecSDave Kleikamp }
1364ac27a0ecSDave Kleikamp 
1365ac27a0ecSDave Kleikamp /* Verify that we are loading a valid orphan from disk */
1366617ba13bSMingming Cao struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
1367ac27a0ecSDave Kleikamp {
1368617ba13bSMingming Cao 	unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
1369fd2d4291SAvantika Mathur 	ext4_group_t block_group;
1370ac27a0ecSDave Kleikamp 	int bit;
13717827a7f6STheodore Ts'o 	struct buffer_head *bitmap_bh = NULL;
1372ac27a0ecSDave Kleikamp 	struct inode *inode = NULL;
13737827a7f6STheodore Ts'o 	int err = -EFSCORRUPTED;
1374ac27a0ecSDave Kleikamp 
13757827a7f6STheodore Ts'o 	if (ino < EXT4_FIRST_INO(sb) || ino > max_ino)
13767827a7f6STheodore Ts'o 		goto bad_orphan;
1377ac27a0ecSDave Kleikamp 
1378617ba13bSMingming Cao 	block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
1379617ba13bSMingming Cao 	bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
1380e29d1cdeSEric Sandeen 	bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
1381206f6d55SWang Shilong 	if (IS_ERR(bitmap_bh))
1382e884bce1SAl Viro 		return ERR_CAST(bitmap_bh);
1383ac27a0ecSDave Kleikamp 
1384ac27a0ecSDave Kleikamp 	/* Having the inode bit set should be a 100% indicator that this
1385ac27a0ecSDave Kleikamp 	 * is a valid orphan (no e2fsck run on fs).  Orphans also include
1386ac27a0ecSDave Kleikamp 	 * inodes that were being truncated, so we can't check i_nlink==0.
1387ac27a0ecSDave Kleikamp 	 */
13881d1fe1eeSDavid Howells 	if (!ext4_test_bit(bit, bitmap_bh->b_data))
13891d1fe1eeSDavid Howells 		goto bad_orphan;
13901d1fe1eeSDavid Howells 
13918a363970STheodore Ts'o 	inode = ext4_iget(sb, ino, EXT4_IGET_NORMAL);
13927827a7f6STheodore Ts'o 	if (IS_ERR(inode)) {
13937827a7f6STheodore Ts'o 		err = PTR_ERR(inode);
139454d3adbcSTheodore Ts'o 		ext4_error_err(sb, -err,
139554d3adbcSTheodore Ts'o 			       "couldn't read orphan inode %lu (err %d)",
13967827a7f6STheodore Ts'o 			       ino, err);
13973bbd0ef2SXiyu Yang 		brelse(bitmap_bh);
13987827a7f6STheodore Ts'o 		return inode;
13997827a7f6STheodore Ts'o 	}
14001d1fe1eeSDavid Howells 
140191ef4cafSDuane Griffin 	/*
1402c9eb13a9STheodore Ts'o 	 * If the orphans has i_nlinks > 0 then it should be able to
1403c9eb13a9STheodore Ts'o 	 * be truncated, otherwise it won't be removed from the orphan
1404c9eb13a9STheodore Ts'o 	 * list during processing and an infinite loop will result.
1405c9eb13a9STheodore Ts'o 	 * Similarly, it must not be a bad inode.
140691ef4cafSDuane Griffin 	 */
1407c9eb13a9STheodore Ts'o 	if ((inode->i_nlink && !ext4_can_truncate(inode)) ||
1408c9eb13a9STheodore Ts'o 	    is_bad_inode(inode))
140991ef4cafSDuane Griffin 		goto bad_orphan;
141091ef4cafSDuane Griffin 
14111d1fe1eeSDavid Howells 	if (NEXT_ORPHAN(inode) > max_ino)
14121d1fe1eeSDavid Howells 		goto bad_orphan;
14131d1fe1eeSDavid Howells 	brelse(bitmap_bh);
14141d1fe1eeSDavid Howells 	return inode;
14151d1fe1eeSDavid Howells 
14161d1fe1eeSDavid Howells bad_orphan:
14177827a7f6STheodore Ts'o 	ext4_error(sb, "bad orphan inode %lu", ino);
14187827a7f6STheodore Ts'o 	if (bitmap_bh)
14197827a7f6STheodore Ts'o 		printk(KERN_ERR "ext4_test_bit(bit=%d, block=%llu) = %d\n",
1420ac27a0ecSDave Kleikamp 		       bit, (unsigned long long)bitmap_bh->b_blocknr,
1421617ba13bSMingming Cao 		       ext4_test_bit(bit, bitmap_bh->b_data));
1422ac27a0ecSDave Kleikamp 	if (inode) {
14237827a7f6STheodore Ts'o 		printk(KERN_ERR "is_bad_inode(inode)=%d\n",
1424ac27a0ecSDave Kleikamp 		       is_bad_inode(inode));
14257827a7f6STheodore Ts'o 		printk(KERN_ERR "NEXT_ORPHAN(inode)=%u\n",
1426ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
14277827a7f6STheodore Ts'o 		printk(KERN_ERR "max_ino=%lu\n", max_ino);
14287827a7f6STheodore Ts'o 		printk(KERN_ERR "i_nlink=%u\n", inode->i_nlink);
1429ac27a0ecSDave Kleikamp 		/* Avoid freeing blocks if we got a bad deleted inode */
14301d1fe1eeSDavid Howells 		if (inode->i_nlink == 0)
1431ac27a0ecSDave Kleikamp 			inode->i_blocks = 0;
1432ac27a0ecSDave Kleikamp 		iput(inode);
1433ac27a0ecSDave Kleikamp 	}
1434ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
14351d1fe1eeSDavid Howells 	return ERR_PTR(err);
1436ac27a0ecSDave Kleikamp }
1437ac27a0ecSDave Kleikamp 
1438617ba13bSMingming Cao unsigned long ext4_count_free_inodes(struct super_block *sb)
1439ac27a0ecSDave Kleikamp {
1440ac27a0ecSDave Kleikamp 	unsigned long desc_count;
1441617ba13bSMingming Cao 	struct ext4_group_desc *gdp;
14428df9675fSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
1443617ba13bSMingming Cao #ifdef EXT4FS_DEBUG
1444617ba13bSMingming Cao 	struct ext4_super_block *es;
1445ac27a0ecSDave Kleikamp 	unsigned long bitmap_count, x;
1446ac27a0ecSDave Kleikamp 	struct buffer_head *bitmap_bh = NULL;
1447ac27a0ecSDave Kleikamp 
1448617ba13bSMingming Cao 	es = EXT4_SB(sb)->s_es;
1449ac27a0ecSDave Kleikamp 	desc_count = 0;
1450ac27a0ecSDave Kleikamp 	bitmap_count = 0;
1451ac27a0ecSDave Kleikamp 	gdp = NULL;
14528df9675fSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
1453617ba13bSMingming Cao 		gdp = ext4_get_group_desc(sb, i, NULL);
1454ac27a0ecSDave Kleikamp 		if (!gdp)
1455ac27a0ecSDave Kleikamp 			continue;
1456560671a0SAneesh Kumar K.V 		desc_count += ext4_free_inodes_count(sb, gdp);
1457ac27a0ecSDave Kleikamp 		brelse(bitmap_bh);
1458e29d1cdeSEric Sandeen 		bitmap_bh = ext4_read_inode_bitmap(sb, i);
14599008a58eSDarrick J. Wong 		if (IS_ERR(bitmap_bh)) {
14609008a58eSDarrick J. Wong 			bitmap_bh = NULL;
1461ac27a0ecSDave Kleikamp 			continue;
14629008a58eSDarrick J. Wong 		}
1463ac27a0ecSDave Kleikamp 
1464f6fb99caSTheodore Ts'o 		x = ext4_count_free(bitmap_bh->b_data,
1465f6fb99caSTheodore Ts'o 				    EXT4_INODES_PER_GROUP(sb) / 8);
1466c549a95dSEric Sandeen 		printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
1467785b4b3aSPeng Tao 			(unsigned long) i, ext4_free_inodes_count(sb, gdp), x);
1468ac27a0ecSDave Kleikamp 		bitmap_count += x;
1469ac27a0ecSDave Kleikamp 	}
1470ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
14714776004fSTheodore Ts'o 	printk(KERN_DEBUG "ext4_count_free_inodes: "
14724776004fSTheodore Ts'o 	       "stored = %u, computed = %lu, %lu\n",
1473ac27a0ecSDave Kleikamp 	       le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
1474ac27a0ecSDave Kleikamp 	return desc_count;
1475ac27a0ecSDave Kleikamp #else
1476ac27a0ecSDave Kleikamp 	desc_count = 0;
14778df9675fSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
1478617ba13bSMingming Cao 		gdp = ext4_get_group_desc(sb, i, NULL);
1479ac27a0ecSDave Kleikamp 		if (!gdp)
1480ac27a0ecSDave Kleikamp 			continue;
1481560671a0SAneesh Kumar K.V 		desc_count += ext4_free_inodes_count(sb, gdp);
1482ac27a0ecSDave Kleikamp 		cond_resched();
1483ac27a0ecSDave Kleikamp 	}
1484ac27a0ecSDave Kleikamp 	return desc_count;
1485ac27a0ecSDave Kleikamp #endif
1486ac27a0ecSDave Kleikamp }
1487ac27a0ecSDave Kleikamp 
1488ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
1489617ba13bSMingming Cao unsigned long ext4_count_dirs(struct super_block * sb)
1490ac27a0ecSDave Kleikamp {
1491ac27a0ecSDave Kleikamp 	unsigned long count = 0;
14928df9675fSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
1493ac27a0ecSDave Kleikamp 
14948df9675fSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
1495617ba13bSMingming Cao 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1496ac27a0ecSDave Kleikamp 		if (!gdp)
1497ac27a0ecSDave Kleikamp 			continue;
1498560671a0SAneesh Kumar K.V 		count += ext4_used_dirs_count(sb, gdp);
1499ac27a0ecSDave Kleikamp 	}
1500ac27a0ecSDave Kleikamp 	return count;
1501ac27a0ecSDave Kleikamp }
1502bfff6873SLukas Czerner 
1503bfff6873SLukas Czerner /*
1504bfff6873SLukas Czerner  * Zeroes not yet zeroed inode table - just write zeroes through the whole
1505bfff6873SLukas Czerner  * inode table. Must be called without any spinlock held. The only place
1506bfff6873SLukas Czerner  * where it is called from on active part of filesystem is ext4lazyinit
1507bfff6873SLukas Czerner  * thread, so we do not need any special locks, however we have to prevent
1508bfff6873SLukas Czerner  * inode allocation from the current group, so we take alloc_sem lock, to
1509119c0d44STheodore Ts'o  * block ext4_new_inode() until we are finished.
1510bfff6873SLukas Czerner  */
1511e0cbee3eSH Hartley Sweeten int ext4_init_inode_table(struct super_block *sb, ext4_group_t group,
1512bfff6873SLukas Czerner 				 int barrier)
1513bfff6873SLukas Czerner {
1514bfff6873SLukas Czerner 	struct ext4_group_info *grp = ext4_get_group_info(sb, group);
1515bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1516bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
1517bfff6873SLukas Czerner 	struct buffer_head *group_desc_bh;
1518bfff6873SLukas Czerner 	handle_t *handle;
1519bfff6873SLukas Czerner 	ext4_fsblk_t blk;
1520bfff6873SLukas Czerner 	int num, ret = 0, used_blks = 0;
1521a149d2a5SZhang Yi 	unsigned long used_inos = 0;
1522bfff6873SLukas Czerner 
1523bfff6873SLukas Czerner 	gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
15245354b2afSTheodore Ts'o 	if (!gdp || !grp)
1525bfff6873SLukas Czerner 		goto out;
1526bfff6873SLukas Czerner 
1527bfff6873SLukas Czerner 	/*
1528bfff6873SLukas Czerner 	 * We do not need to lock this, because we are the only one
1529bfff6873SLukas Czerner 	 * handling this flag.
1530bfff6873SLukas Czerner 	 */
1531bfff6873SLukas Czerner 	if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED))
1532bfff6873SLukas Czerner 		goto out;
1533bfff6873SLukas Czerner 
15349924a92aSTheodore Ts'o 	handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
1535bfff6873SLukas Czerner 	if (IS_ERR(handle)) {
1536bfff6873SLukas Czerner 		ret = PTR_ERR(handle);
1537bfff6873SLukas Czerner 		goto out;
1538bfff6873SLukas Czerner 	}
1539bfff6873SLukas Czerner 
1540bfff6873SLukas Czerner 	down_write(&grp->alloc_sem);
1541bfff6873SLukas Czerner 	/*
1542bfff6873SLukas Czerner 	 * If inode bitmap was already initialized there may be some
1543bfff6873SLukas Czerner 	 * used inodes so we need to skip blocks with used inodes in
1544bfff6873SLukas Czerner 	 * inode table.
1545bfff6873SLukas Czerner 	 */
1546a149d2a5SZhang Yi 	if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT))) {
1547a149d2a5SZhang Yi 		used_inos = EXT4_INODES_PER_GROUP(sb) -
1548a149d2a5SZhang Yi 			    ext4_itable_unused_count(sb, gdp);
1549a149d2a5SZhang Yi 		used_blks = DIV_ROUND_UP(used_inos, sbi->s_inodes_per_block);
1550bfff6873SLukas Czerner 
1551a149d2a5SZhang Yi 		/* Bogus inode unused count? */
1552a149d2a5SZhang Yi 		if (used_blks < 0 || used_blks > sbi->s_itb_per_group) {
15531084f252STheodore Ts'o 			ext4_error(sb, "Something is wrong with group %u: "
15541084f252STheodore Ts'o 				   "used itable blocks: %d; "
15551084f252STheodore Ts'o 				   "itable unused count: %u",
1556857ac889SLukas Czerner 				   group, used_blks,
1557857ac889SLukas Czerner 				   ext4_itable_unused_count(sb, gdp));
1558857ac889SLukas Czerner 			ret = 1;
155933853a0dSYongqiang Yang 			goto err_out;
1560857ac889SLukas Czerner 		}
1561857ac889SLukas Czerner 
1562a149d2a5SZhang Yi 		used_inos += group * EXT4_INODES_PER_GROUP(sb);
1563a149d2a5SZhang Yi 		/*
1564a149d2a5SZhang Yi 		 * Are there some uninitialized inodes in the inode table
1565a149d2a5SZhang Yi 		 * before the first normal inode?
1566a149d2a5SZhang Yi 		 */
1567a149d2a5SZhang Yi 		if ((used_blks != sbi->s_itb_per_group) &&
1568a149d2a5SZhang Yi 		     (used_inos < EXT4_FIRST_INO(sb))) {
1569a149d2a5SZhang Yi 			ext4_error(sb, "Something is wrong with group %u: "
1570a149d2a5SZhang Yi 				   "itable unused count: %u; "
1571a149d2a5SZhang Yi 				   "itables initialized count: %ld",
1572a149d2a5SZhang Yi 				   group, ext4_itable_unused_count(sb, gdp),
1573a149d2a5SZhang Yi 				   used_inos);
1574a149d2a5SZhang Yi 			ret = 1;
1575a149d2a5SZhang Yi 			goto err_out;
1576a149d2a5SZhang Yi 		}
1577a149d2a5SZhang Yi 	}
1578a149d2a5SZhang Yi 
1579bfff6873SLukas Czerner 	blk = ext4_inode_table(sb, gdp) + used_blks;
1580bfff6873SLukas Czerner 	num = sbi->s_itb_per_group - used_blks;
1581bfff6873SLukas Czerner 
1582bfff6873SLukas Czerner 	BUFFER_TRACE(group_desc_bh, "get_write_access");
1583188c299eSJan Kara 	ret = ext4_journal_get_write_access(handle, sb, group_desc_bh,
1584188c299eSJan Kara 					    EXT4_JTR_NONE);
1585bfff6873SLukas Czerner 	if (ret)
1586bfff6873SLukas Czerner 		goto err_out;
1587bfff6873SLukas Czerner 
1588bfff6873SLukas Czerner 	/*
1589bfff6873SLukas Czerner 	 * Skip zeroout if the inode table is full. But we set the ZEROED
1590bfff6873SLukas Czerner 	 * flag anyway, because obviously, when it is full it does not need
1591bfff6873SLukas Czerner 	 * further zeroing.
1592bfff6873SLukas Czerner 	 */
1593bfff6873SLukas Czerner 	if (unlikely(num == 0))
1594bfff6873SLukas Czerner 		goto skip_zeroout;
1595bfff6873SLukas Czerner 
1596bfff6873SLukas Czerner 	ext4_debug("going to zero out inode table in group %d\n",
1597bfff6873SLukas Czerner 		   group);
1598a107e5a3STheodore Ts'o 	ret = sb_issue_zeroout(sb, blk, num, GFP_NOFS);
1599bfff6873SLukas Czerner 	if (ret < 0)
1600bfff6873SLukas Czerner 		goto err_out;
1601a107e5a3STheodore Ts'o 	if (barrier)
1602c6bf3f0eSChristoph Hellwig 		blkdev_issue_flush(sb->s_bdev);
1603bfff6873SLukas Czerner 
1604bfff6873SLukas Czerner skip_zeroout:
1605bfff6873SLukas Czerner 	ext4_lock_group(sb, group);
1606bfff6873SLukas Czerner 	gdp->bg_flags |= cpu_to_le16(EXT4_BG_INODE_ZEROED);
1607feb0ab32SDarrick J. Wong 	ext4_group_desc_csum_set(sb, group, gdp);
1608bfff6873SLukas Czerner 	ext4_unlock_group(sb, group);
1609bfff6873SLukas Czerner 
1610bfff6873SLukas Czerner 	BUFFER_TRACE(group_desc_bh,
1611bfff6873SLukas Czerner 		     "call ext4_handle_dirty_metadata");
1612bfff6873SLukas Czerner 	ret = ext4_handle_dirty_metadata(handle, NULL,
1613bfff6873SLukas Czerner 					 group_desc_bh);
1614bfff6873SLukas Czerner 
1615bfff6873SLukas Czerner err_out:
1616bfff6873SLukas Czerner 	up_write(&grp->alloc_sem);
1617bfff6873SLukas Czerner 	ext4_journal_stop(handle);
1618bfff6873SLukas Czerner out:
1619bfff6873SLukas Czerner 	return ret;
1620bfff6873SLukas Czerner }
1621