xref: /linux/fs/ext4/ialloc.c (revision c9eb13a9105e2e418f72e46a2b6da3f49e696902)
1ac27a0ecSDave Kleikamp /*
2617ba13bSMingming Cao  *  linux/fs/ext4/ialloc.c
3ac27a0ecSDave Kleikamp  *
4ac27a0ecSDave Kleikamp  * Copyright (C) 1992, 1993, 1994, 1995
5ac27a0ecSDave Kleikamp  * Remy Card (card@masi.ibp.fr)
6ac27a0ecSDave Kleikamp  * Laboratoire MASI - Institut Blaise Pascal
7ac27a0ecSDave Kleikamp  * Universite Pierre et Marie Curie (Paris VI)
8ac27a0ecSDave Kleikamp  *
9ac27a0ecSDave Kleikamp  *  BSD ufs-inspired inode and directory allocation by
10ac27a0ecSDave Kleikamp  *  Stephen Tweedie (sct@redhat.com), 1993
11ac27a0ecSDave Kleikamp  *  Big-endian to little-endian byte-swapping/bitmaps by
12ac27a0ecSDave Kleikamp  *        David S. Miller (davem@caip.rutgers.edu), 1995
13ac27a0ecSDave Kleikamp  */
14ac27a0ecSDave Kleikamp 
15ac27a0ecSDave Kleikamp #include <linux/time.h>
16ac27a0ecSDave Kleikamp #include <linux/fs.h>
17ac27a0ecSDave Kleikamp #include <linux/stat.h>
18ac27a0ecSDave Kleikamp #include <linux/string.h>
19ac27a0ecSDave Kleikamp #include <linux/quotaops.h>
20ac27a0ecSDave Kleikamp #include <linux/buffer_head.h>
21ac27a0ecSDave Kleikamp #include <linux/random.h>
22ac27a0ecSDave Kleikamp #include <linux/bitops.h>
233a5b2ecdSMingming Cao #include <linux/blkdev.h>
24ac27a0ecSDave Kleikamp #include <asm/byteorder.h>
259bffad1eSTheodore Ts'o 
263dcf5451SChristoph Hellwig #include "ext4.h"
273dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
28ac27a0ecSDave Kleikamp #include "xattr.h"
29ac27a0ecSDave Kleikamp #include "acl.h"
30ac27a0ecSDave Kleikamp 
319bffad1eSTheodore Ts'o #include <trace/events/ext4.h>
329bffad1eSTheodore Ts'o 
33ac27a0ecSDave Kleikamp /*
34ac27a0ecSDave Kleikamp  * ialloc.c contains the inodes allocation and deallocation routines
35ac27a0ecSDave Kleikamp  */
36ac27a0ecSDave Kleikamp 
37ac27a0ecSDave Kleikamp /*
38ac27a0ecSDave Kleikamp  * The free inodes are managed by bitmaps.  A file system contains several
39ac27a0ecSDave Kleikamp  * blocks groups.  Each group contains 1 bitmap block for blocks, 1 bitmap
40ac27a0ecSDave Kleikamp  * block for inodes, N blocks for the inode table and data blocks.
41ac27a0ecSDave Kleikamp  *
42ac27a0ecSDave Kleikamp  * The file system contains group descriptors which are located after the
43ac27a0ecSDave Kleikamp  * super block.  Each descriptor contains the number of the bitmap block and
44ac27a0ecSDave Kleikamp  * the free blocks count in the block.
45ac27a0ecSDave Kleikamp  */
46ac27a0ecSDave Kleikamp 
47717d50e4SAndreas Dilger /*
48717d50e4SAndreas Dilger  * To avoid calling the atomic setbit hundreds or thousands of times, we only
49717d50e4SAndreas Dilger  * need to use it within a single byte (to ensure we get endianness right).
50717d50e4SAndreas Dilger  * We can use memset for the rest of the bitmap as there are no other users.
51717d50e4SAndreas Dilger  */
5261d08673STheodore Ts'o void ext4_mark_bitmap_end(int start_bit, int end_bit, char *bitmap)
53717d50e4SAndreas Dilger {
54717d50e4SAndreas Dilger 	int i;
55717d50e4SAndreas Dilger 
56717d50e4SAndreas Dilger 	if (start_bit >= end_bit)
57717d50e4SAndreas Dilger 		return;
58717d50e4SAndreas Dilger 
59717d50e4SAndreas Dilger 	ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit);
60717d50e4SAndreas Dilger 	for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
61717d50e4SAndreas Dilger 		ext4_set_bit(i, bitmap);
62717d50e4SAndreas Dilger 	if (i < end_bit)
63717d50e4SAndreas Dilger 		memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
64717d50e4SAndreas Dilger }
65717d50e4SAndreas Dilger 
66717d50e4SAndreas Dilger /* Initializes an uninitialized inode bitmap */
679008a58eSDarrick J. Wong static int ext4_init_inode_bitmap(struct super_block *sb,
681f109d5aSTheodore Ts'o 				       struct buffer_head *bh,
69fd2d4291SAvantika Mathur 				       ext4_group_t block_group,
70717d50e4SAndreas Dilger 				       struct ext4_group_desc *gdp)
71717d50e4SAndreas Dilger {
72bdfb6ff4SDarrick J. Wong 	struct ext4_group_info *grp;
73e43bb4e6SNamjae Jeon 	struct ext4_sb_info *sbi = EXT4_SB(sb);
74717d50e4SAndreas Dilger 	J_ASSERT_BH(bh, buffer_locked(bh));
75717d50e4SAndreas Dilger 
76717d50e4SAndreas Dilger 	/* If checksum is bad mark all blocks and inodes use to prevent
77717d50e4SAndreas Dilger 	 * allocation, essentially implementing a per-group read-only flag. */
78feb0ab32SDarrick J. Wong 	if (!ext4_group_desc_csum_verify(sb, block_group, gdp)) {
79bdfb6ff4SDarrick J. Wong 		grp = ext4_get_group_info(sb, block_group);
80e43bb4e6SNamjae Jeon 		if (!EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
81e43bb4e6SNamjae Jeon 			percpu_counter_sub(&sbi->s_freeclusters_counter,
82e43bb4e6SNamjae Jeon 					   grp->bb_free);
83bdfb6ff4SDarrick J. Wong 		set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT, &grp->bb_state);
84e43bb4e6SNamjae Jeon 		if (!EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) {
85e43bb4e6SNamjae Jeon 			int count;
86e43bb4e6SNamjae Jeon 			count = ext4_free_inodes_count(sb, gdp);
87e43bb4e6SNamjae Jeon 			percpu_counter_sub(&sbi->s_freeinodes_counter,
88e43bb4e6SNamjae Jeon 					   count);
89e43bb4e6SNamjae Jeon 		}
90bdfb6ff4SDarrick J. Wong 		set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &grp->bb_state);
919008a58eSDarrick J. Wong 		return -EFSBADCRC;
92717d50e4SAndreas Dilger 	}
93717d50e4SAndreas Dilger 
94717d50e4SAndreas Dilger 	memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8);
9561d08673STheodore Ts'o 	ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), sb->s_blocksize * 8,
96717d50e4SAndreas Dilger 			bh->b_data);
9741a246d1SDarrick J. Wong 	ext4_inode_bitmap_csum_set(sb, block_group, gdp, bh,
9841a246d1SDarrick J. Wong 				   EXT4_INODES_PER_GROUP(sb) / 8);
99feb0ab32SDarrick J. Wong 	ext4_group_desc_csum_set(sb, block_group, gdp);
100717d50e4SAndreas Dilger 
1019008a58eSDarrick J. Wong 	return 0;
102717d50e4SAndreas Dilger }
103ac27a0ecSDave Kleikamp 
104813e5727STheodore Ts'o void ext4_end_bitmap_read(struct buffer_head *bh, int uptodate)
105813e5727STheodore Ts'o {
106813e5727STheodore Ts'o 	if (uptodate) {
107813e5727STheodore Ts'o 		set_buffer_uptodate(bh);
108813e5727STheodore Ts'o 		set_bitmap_uptodate(bh);
109813e5727STheodore Ts'o 	}
110813e5727STheodore Ts'o 	unlock_buffer(bh);
111813e5727STheodore Ts'o 	put_bh(bh);
112813e5727STheodore Ts'o }
113813e5727STheodore Ts'o 
1149008a58eSDarrick J. Wong static int ext4_validate_inode_bitmap(struct super_block *sb,
1159008a58eSDarrick J. Wong 				      struct ext4_group_desc *desc,
1169008a58eSDarrick J. Wong 				      ext4_group_t block_group,
1179008a58eSDarrick J. Wong 				      struct buffer_head *bh)
1189008a58eSDarrick J. Wong {
1199008a58eSDarrick J. Wong 	ext4_fsblk_t	blk;
1209008a58eSDarrick J. Wong 	struct ext4_group_info *grp = ext4_get_group_info(sb, block_group);
1219008a58eSDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1229008a58eSDarrick J. Wong 
1239008a58eSDarrick J. Wong 	if (buffer_verified(bh))
1249008a58eSDarrick J. Wong 		return 0;
1259008a58eSDarrick J. Wong 	if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp))
1269008a58eSDarrick J. Wong 		return -EFSCORRUPTED;
1279008a58eSDarrick J. Wong 
1289008a58eSDarrick J. Wong 	ext4_lock_group(sb, block_group);
1299008a58eSDarrick J. Wong 	blk = ext4_inode_bitmap(sb, desc);
1309008a58eSDarrick J. Wong 	if (!ext4_inode_bitmap_csum_verify(sb, block_group, desc, bh,
1319008a58eSDarrick J. Wong 					   EXT4_INODES_PER_GROUP(sb) / 8)) {
1329008a58eSDarrick J. Wong 		ext4_unlock_group(sb, block_group);
1339008a58eSDarrick J. Wong 		ext4_error(sb, "Corrupt inode bitmap - block_group = %u, "
1349008a58eSDarrick J. Wong 			   "inode_bitmap = %llu", block_group, blk);
1359008a58eSDarrick J. Wong 		grp = ext4_get_group_info(sb, block_group);
1369008a58eSDarrick J. Wong 		if (!EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) {
1379008a58eSDarrick J. Wong 			int count;
1389008a58eSDarrick J. Wong 			count = ext4_free_inodes_count(sb, desc);
1399008a58eSDarrick J. Wong 			percpu_counter_sub(&sbi->s_freeinodes_counter,
1409008a58eSDarrick J. Wong 					   count);
1419008a58eSDarrick J. Wong 		}
1429008a58eSDarrick J. Wong 		set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &grp->bb_state);
1439008a58eSDarrick J. Wong 		return -EFSBADCRC;
1449008a58eSDarrick J. Wong 	}
1459008a58eSDarrick J. Wong 	set_buffer_verified(bh);
1469008a58eSDarrick J. Wong 	ext4_unlock_group(sb, block_group);
1479008a58eSDarrick J. Wong 	return 0;
1489008a58eSDarrick J. Wong }
1499008a58eSDarrick J. Wong 
150ac27a0ecSDave Kleikamp /*
151ac27a0ecSDave Kleikamp  * Read the inode allocation bitmap for a given block_group, reading
152ac27a0ecSDave Kleikamp  * into the specified slot in the superblock's bitmap cache.
153ac27a0ecSDave Kleikamp  *
154ac27a0ecSDave Kleikamp  * Return buffer_head of bitmap on success or NULL.
155ac27a0ecSDave Kleikamp  */
156ac27a0ecSDave Kleikamp static struct buffer_head *
157e29d1cdeSEric Sandeen ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group)
158ac27a0ecSDave Kleikamp {
159617ba13bSMingming Cao 	struct ext4_group_desc *desc;
160ac27a0ecSDave Kleikamp 	struct buffer_head *bh = NULL;
161e29d1cdeSEric Sandeen 	ext4_fsblk_t bitmap_blk;
1629008a58eSDarrick J. Wong 	int err;
163ac27a0ecSDave Kleikamp 
164617ba13bSMingming Cao 	desc = ext4_get_group_desc(sb, block_group, NULL);
165ac27a0ecSDave Kleikamp 	if (!desc)
1669008a58eSDarrick J. Wong 		return ERR_PTR(-EFSCORRUPTED);
167bfff6873SLukas Czerner 
168e29d1cdeSEric Sandeen 	bitmap_blk = ext4_inode_bitmap(sb, desc);
169e29d1cdeSEric Sandeen 	bh = sb_getblk(sb, bitmap_blk);
170e29d1cdeSEric Sandeen 	if (unlikely(!bh)) {
17112062dddSEric Sandeen 		ext4_error(sb, "Cannot read inode bitmap - "
172a9df9a49STheodore Ts'o 			    "block_group = %u, inode_bitmap = %llu",
173e29d1cdeSEric Sandeen 			    block_group, bitmap_blk);
1749008a58eSDarrick J. Wong 		return ERR_PTR(-EIO);
175e29d1cdeSEric Sandeen 	}
1762ccb5fb9SAneesh Kumar K.V 	if (bitmap_uptodate(bh))
17741a246d1SDarrick J. Wong 		goto verify;
178e29d1cdeSEric Sandeen 
179c806e68fSFrederic Bohe 	lock_buffer(bh);
1802ccb5fb9SAneesh Kumar K.V 	if (bitmap_uptodate(bh)) {
1812ccb5fb9SAneesh Kumar K.V 		unlock_buffer(bh);
18241a246d1SDarrick J. Wong 		goto verify;
1832ccb5fb9SAneesh Kumar K.V 	}
184bfff6873SLukas Czerner 
185955ce5f5SAneesh Kumar K.V 	ext4_lock_group(sb, block_group);
186e29d1cdeSEric Sandeen 	if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
1879008a58eSDarrick J. Wong 		err = ext4_init_inode_bitmap(sb, bh, block_group, desc);
1882ccb5fb9SAneesh Kumar K.V 		set_bitmap_uptodate(bh);
189e29d1cdeSEric Sandeen 		set_buffer_uptodate(bh);
19041a246d1SDarrick J. Wong 		set_buffer_verified(bh);
191955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, block_group);
1923300bedaSAneesh Kumar K.V 		unlock_buffer(bh);
19305145bd7SJan Kara 		if (err) {
19405145bd7SJan Kara 			ext4_error(sb, "Failed to init inode bitmap for group "
19505145bd7SJan Kara 				   "%u: %d", block_group, err);
1969008a58eSDarrick J. Wong 			goto out;
19705145bd7SJan Kara 		}
198e29d1cdeSEric Sandeen 		return bh;
199e29d1cdeSEric Sandeen 	}
200955ce5f5SAneesh Kumar K.V 	ext4_unlock_group(sb, block_group);
201bfff6873SLukas Czerner 
2022ccb5fb9SAneesh Kumar K.V 	if (buffer_uptodate(bh)) {
2032ccb5fb9SAneesh Kumar K.V 		/*
2042ccb5fb9SAneesh Kumar K.V 		 * if not uninit if bh is uptodate,
2052ccb5fb9SAneesh Kumar K.V 		 * bitmap is also uptodate
2062ccb5fb9SAneesh Kumar K.V 		 */
2072ccb5fb9SAneesh Kumar K.V 		set_bitmap_uptodate(bh);
2082ccb5fb9SAneesh Kumar K.V 		unlock_buffer(bh);
20941a246d1SDarrick J. Wong 		goto verify;
2102ccb5fb9SAneesh Kumar K.V 	}
2112ccb5fb9SAneesh Kumar K.V 	/*
212813e5727STheodore Ts'o 	 * submit the buffer_head for reading
2132ccb5fb9SAneesh Kumar K.V 	 */
2140562e0baSJiaying Zhang 	trace_ext4_load_inode_bitmap(sb, block_group);
215813e5727STheodore Ts'o 	bh->b_end_io = ext4_end_bitmap_read;
216813e5727STheodore Ts'o 	get_bh(bh);
2179f203507STheodore Ts'o 	submit_bh(READ | REQ_META | REQ_PRIO, bh);
218813e5727STheodore Ts'o 	wait_on_buffer(bh);
219813e5727STheodore Ts'o 	if (!buffer_uptodate(bh)) {
220e29d1cdeSEric Sandeen 		put_bh(bh);
22112062dddSEric Sandeen 		ext4_error(sb, "Cannot read inode bitmap - "
222a9df9a49STheodore Ts'o 			   "block_group = %u, inode_bitmap = %llu",
223e29d1cdeSEric Sandeen 			   block_group, bitmap_blk);
2249008a58eSDarrick J. Wong 		return ERR_PTR(-EIO);
225e29d1cdeSEric Sandeen 	}
22641a246d1SDarrick J. Wong 
22741a246d1SDarrick J. Wong verify:
2289008a58eSDarrick J. Wong 	err = ext4_validate_inode_bitmap(sb, desc, block_group, bh);
2299008a58eSDarrick J. Wong 	if (err)
2309008a58eSDarrick J. Wong 		goto out;
231ac27a0ecSDave Kleikamp 	return bh;
2329008a58eSDarrick J. Wong out:
2339008a58eSDarrick J. Wong 	put_bh(bh);
2349008a58eSDarrick J. Wong 	return ERR_PTR(err);
235ac27a0ecSDave Kleikamp }
236ac27a0ecSDave Kleikamp 
237ac27a0ecSDave Kleikamp /*
238ac27a0ecSDave Kleikamp  * NOTE! When we get the inode, we're the only people
239ac27a0ecSDave Kleikamp  * that have access to it, and as such there are no
240ac27a0ecSDave Kleikamp  * race conditions we have to worry about. The inode
241ac27a0ecSDave Kleikamp  * is not on the hash-lists, and it cannot be reached
242ac27a0ecSDave Kleikamp  * through the filesystem because the directory entry
243ac27a0ecSDave Kleikamp  * has been deleted earlier.
244ac27a0ecSDave Kleikamp  *
245ac27a0ecSDave Kleikamp  * HOWEVER: we must make sure that we get no aliases,
246ac27a0ecSDave Kleikamp  * which means that we have to call "clear_inode()"
247ac27a0ecSDave Kleikamp  * _before_ we mark the inode not in use in the inode
248ac27a0ecSDave Kleikamp  * bitmaps. Otherwise a newly created file might use
249ac27a0ecSDave Kleikamp  * the same inode number (not actually the same pointer
250ac27a0ecSDave Kleikamp  * though), and then we'd have two inodes sharing the
251ac27a0ecSDave Kleikamp  * same inode number and space on the harddisk.
252ac27a0ecSDave Kleikamp  */
253617ba13bSMingming Cao void ext4_free_inode(handle_t *handle, struct inode *inode)
254ac27a0ecSDave Kleikamp {
255ac27a0ecSDave Kleikamp 	struct super_block *sb = inode->i_sb;
256ac27a0ecSDave Kleikamp 	int is_directory;
257ac27a0ecSDave Kleikamp 	unsigned long ino;
258ac27a0ecSDave Kleikamp 	struct buffer_head *bitmap_bh = NULL;
259ac27a0ecSDave Kleikamp 	struct buffer_head *bh2;
260fd2d4291SAvantika Mathur 	ext4_group_t block_group;
261ac27a0ecSDave Kleikamp 	unsigned long bit;
262617ba13bSMingming Cao 	struct ext4_group_desc *gdp;
263617ba13bSMingming Cao 	struct ext4_super_block *es;
264617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
2657ce9d5d1SEric Sandeen 	int fatal = 0, err, count, cleared;
26687a39389SDarrick J. Wong 	struct ext4_group_info *grp;
267ac27a0ecSDave Kleikamp 
26892b97816STheodore Ts'o 	if (!sb) {
26992b97816STheodore Ts'o 		printk(KERN_ERR "EXT4-fs: %s:%d: inode on "
27092b97816STheodore Ts'o 		       "nonexistent device\n", __func__, __LINE__);
27192b97816STheodore Ts'o 		return;
27292b97816STheodore Ts'o 	}
273ac27a0ecSDave Kleikamp 	if (atomic_read(&inode->i_count) > 1) {
27492b97816STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: count=%d",
27592b97816STheodore Ts'o 			 __func__, __LINE__, inode->i_ino,
276ac27a0ecSDave Kleikamp 			 atomic_read(&inode->i_count));
277ac27a0ecSDave Kleikamp 		return;
278ac27a0ecSDave Kleikamp 	}
279ac27a0ecSDave Kleikamp 	if (inode->i_nlink) {
28092b97816STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: nlink=%d\n",
28192b97816STheodore Ts'o 			 __func__, __LINE__, inode->i_ino, inode->i_nlink);
282ac27a0ecSDave Kleikamp 		return;
283ac27a0ecSDave Kleikamp 	}
284617ba13bSMingming Cao 	sbi = EXT4_SB(sb);
285ac27a0ecSDave Kleikamp 
286ac27a0ecSDave Kleikamp 	ino = inode->i_ino;
287617ba13bSMingming Cao 	ext4_debug("freeing inode %lu\n", ino);
2889bffad1eSTheodore Ts'o 	trace_ext4_free_inode(inode);
289ac27a0ecSDave Kleikamp 
290ac27a0ecSDave Kleikamp 	/*
291ac27a0ecSDave Kleikamp 	 * Note: we must free any quota before locking the superblock,
292ac27a0ecSDave Kleikamp 	 * as writing the quota to disk may need the lock as well.
293ac27a0ecSDave Kleikamp 	 */
294871a2931SChristoph Hellwig 	dquot_initialize(inode);
295617ba13bSMingming Cao 	ext4_xattr_delete_inode(handle, inode);
29663936ddaSChristoph Hellwig 	dquot_free_inode(inode);
2979f754758SChristoph Hellwig 	dquot_drop(inode);
298ac27a0ecSDave Kleikamp 
299ac27a0ecSDave Kleikamp 	is_directory = S_ISDIR(inode->i_mode);
300ac27a0ecSDave Kleikamp 
301ac27a0ecSDave Kleikamp 	/* Do this BEFORE marking the inode not in use or returning an error */
3020930fcc1SAl Viro 	ext4_clear_inode(inode);
303ac27a0ecSDave Kleikamp 
304617ba13bSMingming Cao 	es = EXT4_SB(sb)->s_es;
305617ba13bSMingming Cao 	if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
30612062dddSEric Sandeen 		ext4_error(sb, "reserved or nonexistent inode %lu", ino);
307ac27a0ecSDave Kleikamp 		goto error_return;
308ac27a0ecSDave Kleikamp 	}
309617ba13bSMingming Cao 	block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
310617ba13bSMingming Cao 	bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
311e29d1cdeSEric Sandeen 	bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
31287a39389SDarrick J. Wong 	/* Don't bother if the inode bitmap is corrupt. */
31387a39389SDarrick J. Wong 	grp = ext4_get_group_info(sb, block_group);
3149008a58eSDarrick J. Wong 	if (IS_ERR(bitmap_bh)) {
3159008a58eSDarrick J. Wong 		fatal = PTR_ERR(bitmap_bh);
3169008a58eSDarrick J. Wong 		bitmap_bh = NULL;
317ac27a0ecSDave Kleikamp 		goto error_return;
3189008a58eSDarrick J. Wong 	}
3199008a58eSDarrick J. Wong 	if (unlikely(EXT4_MB_GRP_IBITMAP_CORRUPT(grp))) {
3209008a58eSDarrick J. Wong 		fatal = -EFSCORRUPTED;
3219008a58eSDarrick J. Wong 		goto error_return;
3229008a58eSDarrick J. Wong 	}
323ac27a0ecSDave Kleikamp 
324ac27a0ecSDave Kleikamp 	BUFFER_TRACE(bitmap_bh, "get_write_access");
325617ba13bSMingming Cao 	fatal = ext4_journal_get_write_access(handle, bitmap_bh);
326ac27a0ecSDave Kleikamp 	if (fatal)
327ac27a0ecSDave Kleikamp 		goto error_return;
328ac27a0ecSDave Kleikamp 
329d17413c0SDmitry Monakhov 	fatal = -ESRCH;
330617ba13bSMingming Cao 	gdp = ext4_get_group_desc(sb, block_group, &bh2);
331d17413c0SDmitry Monakhov 	if (gdp) {
332ac27a0ecSDave Kleikamp 		BUFFER_TRACE(bh2, "get_write_access");
333617ba13bSMingming Cao 		fatal = ext4_journal_get_write_access(handle, bh2);
334d17413c0SDmitry Monakhov 	}
335955ce5f5SAneesh Kumar K.V 	ext4_lock_group(sb, block_group);
336597d508cSAkinobu Mita 	cleared = ext4_test_and_clear_bit(bit, bitmap_bh->b_data);
337d17413c0SDmitry Monakhov 	if (fatal || !cleared) {
338d17413c0SDmitry Monakhov 		ext4_unlock_group(sb, block_group);
339d17413c0SDmitry Monakhov 		goto out;
340d17413c0SDmitry Monakhov 	}
341d17413c0SDmitry Monakhov 
342560671a0SAneesh Kumar K.V 	count = ext4_free_inodes_count(sb, gdp) + 1;
343560671a0SAneesh Kumar K.V 	ext4_free_inodes_set(sb, gdp, count);
344560671a0SAneesh Kumar K.V 	if (is_directory) {
345560671a0SAneesh Kumar K.V 		count = ext4_used_dirs_count(sb, gdp) - 1;
346560671a0SAneesh Kumar K.V 		ext4_used_dirs_set(sb, gdp, count);
347d17413c0SDmitry Monakhov 		percpu_counter_dec(&sbi->s_dirs_counter);
348d17413c0SDmitry Monakhov 	}
34941a246d1SDarrick J. Wong 	ext4_inode_bitmap_csum_set(sb, block_group, gdp, bitmap_bh,
35041a246d1SDarrick J. Wong 				   EXT4_INODES_PER_GROUP(sb) / 8);
351feb0ab32SDarrick J. Wong 	ext4_group_desc_csum_set(sb, block_group, gdp);
352d17413c0SDmitry Monakhov 	ext4_unlock_group(sb, block_group);
3537d39db14STheodore Ts'o 
354d17413c0SDmitry Monakhov 	percpu_counter_inc(&sbi->s_freeinodes_counter);
355d17413c0SDmitry Monakhov 	if (sbi->s_log_groups_per_flex) {
356d17413c0SDmitry Monakhov 		ext4_group_t f = ext4_flex_group(sbi, block_group);
357d17413c0SDmitry Monakhov 
358d17413c0SDmitry Monakhov 		atomic_inc(&sbi->s_flex_groups[f].free_inodes);
359d17413c0SDmitry Monakhov 		if (is_directory)
360c4caae25SEric Sandeen 			atomic_dec(&sbi->s_flex_groups[f].used_dirs);
3617d39db14STheodore Ts'o 	}
3620390131bSFrank Mayhar 	BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata");
363d17413c0SDmitry Monakhov 	fatal = ext4_handle_dirty_metadata(handle, NULL, bh2);
364d17413c0SDmitry Monakhov out:
365d17413c0SDmitry Monakhov 	if (cleared) {
3660390131bSFrank Mayhar 		BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata");
3670390131bSFrank Mayhar 		err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
368ac27a0ecSDave Kleikamp 		if (!fatal)
369ac27a0ecSDave Kleikamp 			fatal = err;
37087a39389SDarrick J. Wong 	} else {
371d17413c0SDmitry Monakhov 		ext4_error(sb, "bit already cleared for inode %lu", ino);
372bf40c926SNamjae Jeon 		if (gdp && !EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) {
373e43bb4e6SNamjae Jeon 			int count;
374e43bb4e6SNamjae Jeon 			count = ext4_free_inodes_count(sb, gdp);
375e43bb4e6SNamjae Jeon 			percpu_counter_sub(&sbi->s_freeinodes_counter,
376e43bb4e6SNamjae Jeon 					   count);
377e43bb4e6SNamjae Jeon 		}
37887a39389SDarrick J. Wong 		set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &grp->bb_state);
37987a39389SDarrick J. Wong 	}
380d17413c0SDmitry Monakhov 
381ac27a0ecSDave Kleikamp error_return:
382ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
383617ba13bSMingming Cao 	ext4_std_error(sb, fatal);
384ac27a0ecSDave Kleikamp }
385ac27a0ecSDave Kleikamp 
386a4912123STheodore Ts'o struct orlov_stats {
38790ba983fSTheodore Ts'o 	__u64 free_clusters;
388a4912123STheodore Ts'o 	__u32 free_inodes;
389a4912123STheodore Ts'o 	__u32 used_dirs;
390a4912123STheodore Ts'o };
391a4912123STheodore Ts'o 
392a4912123STheodore Ts'o /*
393a4912123STheodore Ts'o  * Helper function for Orlov's allocator; returns critical information
394a4912123STheodore Ts'o  * for a particular block group or flex_bg.  If flex_size is 1, then g
395a4912123STheodore Ts'o  * is a block group number; otherwise it is flex_bg number.
396a4912123STheodore Ts'o  */
3971f109d5aSTheodore Ts'o static void get_orlov_stats(struct super_block *sb, ext4_group_t g,
398a4912123STheodore Ts'o 			    int flex_size, struct orlov_stats *stats)
399a4912123STheodore Ts'o {
400a4912123STheodore Ts'o 	struct ext4_group_desc *desc;
4017d39db14STheodore Ts'o 	struct flex_groups *flex_group = EXT4_SB(sb)->s_flex_groups;
402a4912123STheodore Ts'o 
4037d39db14STheodore Ts'o 	if (flex_size > 1) {
4047d39db14STheodore Ts'o 		stats->free_inodes = atomic_read(&flex_group[g].free_inodes);
40590ba983fSTheodore Ts'o 		stats->free_clusters = atomic64_read(&flex_group[g].free_clusters);
4067d39db14STheodore Ts'o 		stats->used_dirs = atomic_read(&flex_group[g].used_dirs);
4077d39db14STheodore Ts'o 		return;
4087d39db14STheodore Ts'o 	}
4097d39db14STheodore Ts'o 
4107d39db14STheodore Ts'o 	desc = ext4_get_group_desc(sb, g, NULL);
4117d39db14STheodore Ts'o 	if (desc) {
4127d39db14STheodore Ts'o 		stats->free_inodes = ext4_free_inodes_count(sb, desc);
413021b65bbSTheodore Ts'o 		stats->free_clusters = ext4_free_group_clusters(sb, desc);
4147d39db14STheodore Ts'o 		stats->used_dirs = ext4_used_dirs_count(sb, desc);
4157d39db14STheodore Ts'o 	} else {
416a4912123STheodore Ts'o 		stats->free_inodes = 0;
41724aaa8efSTheodore Ts'o 		stats->free_clusters = 0;
418a4912123STheodore Ts'o 		stats->used_dirs = 0;
419a4912123STheodore Ts'o 	}
420a4912123STheodore Ts'o }
421a4912123STheodore Ts'o 
422ac27a0ecSDave Kleikamp /*
423ac27a0ecSDave Kleikamp  * Orlov's allocator for directories.
424ac27a0ecSDave Kleikamp  *
425ac27a0ecSDave Kleikamp  * We always try to spread first-level directories.
426ac27a0ecSDave Kleikamp  *
427ac27a0ecSDave Kleikamp  * If there are blockgroups with both free inodes and free blocks counts
428ac27a0ecSDave Kleikamp  * not worse than average we return one with smallest directory count.
429ac27a0ecSDave Kleikamp  * Otherwise we simply return a random group.
430ac27a0ecSDave Kleikamp  *
431ac27a0ecSDave Kleikamp  * For the rest rules look so:
432ac27a0ecSDave Kleikamp  *
433ac27a0ecSDave Kleikamp  * It's OK to put directory into a group unless
434ac27a0ecSDave Kleikamp  * it has too many directories already (max_dirs) or
435ac27a0ecSDave Kleikamp  * it has too few free inodes left (min_inodes) or
436ac27a0ecSDave Kleikamp  * it has too few free blocks left (min_blocks) or
4371cc8dcf5SBenoit Boissinot  * Parent's group is preferred, if it doesn't satisfy these
438ac27a0ecSDave Kleikamp  * conditions we search cyclically through the rest. If none
439ac27a0ecSDave Kleikamp  * of the groups look good we just look for a group with more
440ac27a0ecSDave Kleikamp  * free inodes than average (starting at parent's group).
441ac27a0ecSDave Kleikamp  */
442ac27a0ecSDave Kleikamp 
4432aa9fc4cSAvantika Mathur static int find_group_orlov(struct super_block *sb, struct inode *parent,
444dcca3fecSAl Viro 			    ext4_group_t *group, umode_t mode,
445f157a4aaSTheodore Ts'o 			    const struct qstr *qstr)
446ac27a0ecSDave Kleikamp {
447fd2d4291SAvantika Mathur 	ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
448617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4498df9675fSTheodore Ts'o 	ext4_group_t real_ngroups = ext4_get_groups_count(sb);
450617ba13bSMingming Cao 	int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
45114c83c9fSTheodore Ts'o 	unsigned int freei, avefreei, grp_free;
45224aaa8efSTheodore Ts'o 	ext4_fsblk_t freeb, avefreec;
453ac27a0ecSDave Kleikamp 	unsigned int ndirs;
454a4912123STheodore Ts'o 	int max_dirs, min_inodes;
45524aaa8efSTheodore Ts'o 	ext4_grpblk_t min_clusters;
4568df9675fSTheodore Ts'o 	ext4_group_t i, grp, g, ngroups;
457617ba13bSMingming Cao 	struct ext4_group_desc *desc;
458a4912123STheodore Ts'o 	struct orlov_stats stats;
459a4912123STheodore Ts'o 	int flex_size = ext4_flex_bg_size(sbi);
460f157a4aaSTheodore Ts'o 	struct dx_hash_info hinfo;
461a4912123STheodore Ts'o 
4628df9675fSTheodore Ts'o 	ngroups = real_ngroups;
463a4912123STheodore Ts'o 	if (flex_size > 1) {
4648df9675fSTheodore Ts'o 		ngroups = (real_ngroups + flex_size - 1) >>
465a4912123STheodore Ts'o 			sbi->s_log_groups_per_flex;
466a4912123STheodore Ts'o 		parent_group >>= sbi->s_log_groups_per_flex;
467a4912123STheodore Ts'o 	}
468ac27a0ecSDave Kleikamp 
469ac27a0ecSDave Kleikamp 	freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
470ac27a0ecSDave Kleikamp 	avefreei = freei / ngroups;
47157042651STheodore Ts'o 	freeb = EXT4_C2B(sbi,
47257042651STheodore Ts'o 		percpu_counter_read_positive(&sbi->s_freeclusters_counter));
47324aaa8efSTheodore Ts'o 	avefreec = freeb;
47424aaa8efSTheodore Ts'o 	do_div(avefreec, ngroups);
475ac27a0ecSDave Kleikamp 	ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
476ac27a0ecSDave Kleikamp 
477a4912123STheodore Ts'o 	if (S_ISDIR(mode) &&
4782b0143b5SDavid Howells 	    ((parent == d_inode(sb->s_root)) ||
47912e9b892SDmitry Monakhov 	     (ext4_test_inode_flag(parent, EXT4_INODE_TOPDIR)))) {
480ac27a0ecSDave Kleikamp 		int best_ndir = inodes_per_group;
4812aa9fc4cSAvantika Mathur 		int ret = -1;
482ac27a0ecSDave Kleikamp 
483f157a4aaSTheodore Ts'o 		if (qstr) {
484f157a4aaSTheodore Ts'o 			hinfo.hash_version = DX_HASH_HALF_MD4;
485f157a4aaSTheodore Ts'o 			hinfo.seed = sbi->s_hash_seed;
486f157a4aaSTheodore Ts'o 			ext4fs_dirhash(qstr->name, qstr->len, &hinfo);
487f157a4aaSTheodore Ts'o 			grp = hinfo.hash;
488f157a4aaSTheodore Ts'o 		} else
489dd1f723bSTheodore Ts'o 			grp = prandom_u32();
4902aa9fc4cSAvantika Mathur 		parent_group = (unsigned)grp % ngroups;
491ac27a0ecSDave Kleikamp 		for (i = 0; i < ngroups; i++) {
492a4912123STheodore Ts'o 			g = (parent_group + i) % ngroups;
493a4912123STheodore Ts'o 			get_orlov_stats(sb, g, flex_size, &stats);
494a4912123STheodore Ts'o 			if (!stats.free_inodes)
495ac27a0ecSDave Kleikamp 				continue;
496a4912123STheodore Ts'o 			if (stats.used_dirs >= best_ndir)
497ac27a0ecSDave Kleikamp 				continue;
498a4912123STheodore Ts'o 			if (stats.free_inodes < avefreei)
499ac27a0ecSDave Kleikamp 				continue;
50024aaa8efSTheodore Ts'o 			if (stats.free_clusters < avefreec)
501ac27a0ecSDave Kleikamp 				continue;
502a4912123STheodore Ts'o 			grp = g;
5032aa9fc4cSAvantika Mathur 			ret = 0;
504a4912123STheodore Ts'o 			best_ndir = stats.used_dirs;
505ac27a0ecSDave Kleikamp 		}
506a4912123STheodore Ts'o 		if (ret)
507a4912123STheodore Ts'o 			goto fallback;
508a4912123STheodore Ts'o 	found_flex_bg:
509a4912123STheodore Ts'o 		if (flex_size == 1) {
510a4912123STheodore Ts'o 			*group = grp;
511a4912123STheodore Ts'o 			return 0;
512a4912123STheodore Ts'o 		}
513a4912123STheodore Ts'o 
514a4912123STheodore Ts'o 		/*
515a4912123STheodore Ts'o 		 * We pack inodes at the beginning of the flexgroup's
516a4912123STheodore Ts'o 		 * inode tables.  Block allocation decisions will do
517a4912123STheodore Ts'o 		 * something similar, although regular files will
518a4912123STheodore Ts'o 		 * start at 2nd block group of the flexgroup.  See
519a4912123STheodore Ts'o 		 * ext4_ext_find_goal() and ext4_find_near().
520a4912123STheodore Ts'o 		 */
521a4912123STheodore Ts'o 		grp *= flex_size;
522a4912123STheodore Ts'o 		for (i = 0; i < flex_size; i++) {
5238df9675fSTheodore Ts'o 			if (grp+i >= real_ngroups)
524a4912123STheodore Ts'o 				break;
525a4912123STheodore Ts'o 			desc = ext4_get_group_desc(sb, grp+i, NULL);
526a4912123STheodore Ts'o 			if (desc && ext4_free_inodes_count(sb, desc)) {
527a4912123STheodore Ts'o 				*group = grp+i;
528a4912123STheodore Ts'o 				return 0;
529a4912123STheodore Ts'o 			}
530a4912123STheodore Ts'o 		}
531ac27a0ecSDave Kleikamp 		goto fallback;
532ac27a0ecSDave Kleikamp 	}
533ac27a0ecSDave Kleikamp 
534ac27a0ecSDave Kleikamp 	max_dirs = ndirs / ngroups + inodes_per_group / 16;
535a4912123STheodore Ts'o 	min_inodes = avefreei - inodes_per_group*flex_size / 4;
536a4912123STheodore Ts'o 	if (min_inodes < 1)
537a4912123STheodore Ts'o 		min_inodes = 1;
53824aaa8efSTheodore Ts'o 	min_clusters = avefreec - EXT4_CLUSTERS_PER_GROUP(sb)*flex_size / 4;
539ac27a0ecSDave Kleikamp 
540a4912123STheodore Ts'o 	/*
541a4912123STheodore Ts'o 	 * Start looking in the flex group where we last allocated an
542a4912123STheodore Ts'o 	 * inode for this parent directory
543a4912123STheodore Ts'o 	 */
544a4912123STheodore Ts'o 	if (EXT4_I(parent)->i_last_alloc_group != ~0) {
545a4912123STheodore Ts'o 		parent_group = EXT4_I(parent)->i_last_alloc_group;
546a4912123STheodore Ts'o 		if (flex_size > 1)
547a4912123STheodore Ts'o 			parent_group >>= sbi->s_log_groups_per_flex;
548a4912123STheodore Ts'o 	}
549ac27a0ecSDave Kleikamp 
550ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
551a4912123STheodore Ts'o 		grp = (parent_group + i) % ngroups;
552a4912123STheodore Ts'o 		get_orlov_stats(sb, grp, flex_size, &stats);
553a4912123STheodore Ts'o 		if (stats.used_dirs >= max_dirs)
554ac27a0ecSDave Kleikamp 			continue;
555a4912123STheodore Ts'o 		if (stats.free_inodes < min_inodes)
556ac27a0ecSDave Kleikamp 			continue;
55724aaa8efSTheodore Ts'o 		if (stats.free_clusters < min_clusters)
558ac27a0ecSDave Kleikamp 			continue;
559a4912123STheodore Ts'o 		goto found_flex_bg;
560ac27a0ecSDave Kleikamp 	}
561ac27a0ecSDave Kleikamp 
562ac27a0ecSDave Kleikamp fallback:
5638df9675fSTheodore Ts'o 	ngroups = real_ngroups;
564a4912123STheodore Ts'o 	avefreei = freei / ngroups;
565b5451f7bSTheodore Ts'o fallback_retry:
566a4912123STheodore Ts'o 	parent_group = EXT4_I(parent)->i_block_group;
567ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
568a4912123STheodore Ts'o 		grp = (parent_group + i) % ngroups;
569a4912123STheodore Ts'o 		desc = ext4_get_group_desc(sb, grp, NULL);
570bb3d132aSDan Carpenter 		if (desc) {
57114c83c9fSTheodore Ts'o 			grp_free = ext4_free_inodes_count(sb, desc);
572bb3d132aSDan Carpenter 			if (grp_free && grp_free >= avefreei) {
573a4912123STheodore Ts'o 				*group = grp;
5742aa9fc4cSAvantika Mathur 				return 0;
575ac27a0ecSDave Kleikamp 			}
576a4912123STheodore Ts'o 		}
577bb3d132aSDan Carpenter 	}
578ac27a0ecSDave Kleikamp 
579ac27a0ecSDave Kleikamp 	if (avefreei) {
580ac27a0ecSDave Kleikamp 		/*
581ac27a0ecSDave Kleikamp 		 * The free-inodes counter is approximate, and for really small
582ac27a0ecSDave Kleikamp 		 * filesystems the above test can fail to find any blockgroups
583ac27a0ecSDave Kleikamp 		 */
584ac27a0ecSDave Kleikamp 		avefreei = 0;
585b5451f7bSTheodore Ts'o 		goto fallback_retry;
586ac27a0ecSDave Kleikamp 	}
587ac27a0ecSDave Kleikamp 
588ac27a0ecSDave Kleikamp 	return -1;
589ac27a0ecSDave Kleikamp }
590ac27a0ecSDave Kleikamp 
5912aa9fc4cSAvantika Mathur static int find_group_other(struct super_block *sb, struct inode *parent,
592dcca3fecSAl Viro 			    ext4_group_t *group, umode_t mode)
593ac27a0ecSDave Kleikamp {
594fd2d4291SAvantika Mathur 	ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
5958df9675fSTheodore Ts'o 	ext4_group_t i, last, ngroups = ext4_get_groups_count(sb);
596617ba13bSMingming Cao 	struct ext4_group_desc *desc;
597a4912123STheodore Ts'o 	int flex_size = ext4_flex_bg_size(EXT4_SB(sb));
598a4912123STheodore Ts'o 
599a4912123STheodore Ts'o 	/*
600a4912123STheodore Ts'o 	 * Try to place the inode is the same flex group as its
601a4912123STheodore Ts'o 	 * parent.  If we can't find space, use the Orlov algorithm to
602a4912123STheodore Ts'o 	 * find another flex group, and store that information in the
603a4912123STheodore Ts'o 	 * parent directory's inode information so that use that flex
604a4912123STheodore Ts'o 	 * group for future allocations.
605a4912123STheodore Ts'o 	 */
606a4912123STheodore Ts'o 	if (flex_size > 1) {
607a4912123STheodore Ts'o 		int retry = 0;
608a4912123STheodore Ts'o 
609a4912123STheodore Ts'o 	try_again:
610a4912123STheodore Ts'o 		parent_group &= ~(flex_size-1);
611a4912123STheodore Ts'o 		last = parent_group + flex_size;
612a4912123STheodore Ts'o 		if (last > ngroups)
613a4912123STheodore Ts'o 			last = ngroups;
614a4912123STheodore Ts'o 		for  (i = parent_group; i < last; i++) {
615a4912123STheodore Ts'o 			desc = ext4_get_group_desc(sb, i, NULL);
616a4912123STheodore Ts'o 			if (desc && ext4_free_inodes_count(sb, desc)) {
617a4912123STheodore Ts'o 				*group = i;
618a4912123STheodore Ts'o 				return 0;
619a4912123STheodore Ts'o 			}
620a4912123STheodore Ts'o 		}
621a4912123STheodore Ts'o 		if (!retry && EXT4_I(parent)->i_last_alloc_group != ~0) {
622a4912123STheodore Ts'o 			retry = 1;
623a4912123STheodore Ts'o 			parent_group = EXT4_I(parent)->i_last_alloc_group;
624a4912123STheodore Ts'o 			goto try_again;
625a4912123STheodore Ts'o 		}
626a4912123STheodore Ts'o 		/*
627a4912123STheodore Ts'o 		 * If this didn't work, use the Orlov search algorithm
628a4912123STheodore Ts'o 		 * to find a new flex group; we pass in the mode to
629a4912123STheodore Ts'o 		 * avoid the topdir algorithms.
630a4912123STheodore Ts'o 		 */
631a4912123STheodore Ts'o 		*group = parent_group + flex_size;
632a4912123STheodore Ts'o 		if (*group > ngroups)
633a4912123STheodore Ts'o 			*group = 0;
6347dc57615SPeter Huewe 		return find_group_orlov(sb, parent, group, mode, NULL);
635a4912123STheodore Ts'o 	}
636ac27a0ecSDave Kleikamp 
637ac27a0ecSDave Kleikamp 	/*
638ac27a0ecSDave Kleikamp 	 * Try to place the inode in its parent directory
639ac27a0ecSDave Kleikamp 	 */
6402aa9fc4cSAvantika Mathur 	*group = parent_group;
6412aa9fc4cSAvantika Mathur 	desc = ext4_get_group_desc(sb, *group, NULL);
642560671a0SAneesh Kumar K.V 	if (desc && ext4_free_inodes_count(sb, desc) &&
643021b65bbSTheodore Ts'o 	    ext4_free_group_clusters(sb, desc))
6442aa9fc4cSAvantika Mathur 		return 0;
645ac27a0ecSDave Kleikamp 
646ac27a0ecSDave Kleikamp 	/*
647ac27a0ecSDave Kleikamp 	 * We're going to place this inode in a different blockgroup from its
648ac27a0ecSDave Kleikamp 	 * parent.  We want to cause files in a common directory to all land in
649ac27a0ecSDave Kleikamp 	 * the same blockgroup.  But we want files which are in a different
650ac27a0ecSDave Kleikamp 	 * directory which shares a blockgroup with our parent to land in a
651ac27a0ecSDave Kleikamp 	 * different blockgroup.
652ac27a0ecSDave Kleikamp 	 *
653ac27a0ecSDave Kleikamp 	 * So add our directory's i_ino into the starting point for the hash.
654ac27a0ecSDave Kleikamp 	 */
6552aa9fc4cSAvantika Mathur 	*group = (*group + parent->i_ino) % ngroups;
656ac27a0ecSDave Kleikamp 
657ac27a0ecSDave Kleikamp 	/*
658ac27a0ecSDave Kleikamp 	 * Use a quadratic hash to find a group with a free inode and some free
659ac27a0ecSDave Kleikamp 	 * blocks.
660ac27a0ecSDave Kleikamp 	 */
661ac27a0ecSDave Kleikamp 	for (i = 1; i < ngroups; i <<= 1) {
6622aa9fc4cSAvantika Mathur 		*group += i;
6632aa9fc4cSAvantika Mathur 		if (*group >= ngroups)
6642aa9fc4cSAvantika Mathur 			*group -= ngroups;
6652aa9fc4cSAvantika Mathur 		desc = ext4_get_group_desc(sb, *group, NULL);
666560671a0SAneesh Kumar K.V 		if (desc && ext4_free_inodes_count(sb, desc) &&
667021b65bbSTheodore Ts'o 		    ext4_free_group_clusters(sb, desc))
6682aa9fc4cSAvantika Mathur 			return 0;
669ac27a0ecSDave Kleikamp 	}
670ac27a0ecSDave Kleikamp 
671ac27a0ecSDave Kleikamp 	/*
672ac27a0ecSDave Kleikamp 	 * That failed: try linear search for a free inode, even if that group
673ac27a0ecSDave Kleikamp 	 * has no free blocks.
674ac27a0ecSDave Kleikamp 	 */
6752aa9fc4cSAvantika Mathur 	*group = parent_group;
676ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
6772aa9fc4cSAvantika Mathur 		if (++*group >= ngroups)
6782aa9fc4cSAvantika Mathur 			*group = 0;
6792aa9fc4cSAvantika Mathur 		desc = ext4_get_group_desc(sb, *group, NULL);
680560671a0SAneesh Kumar K.V 		if (desc && ext4_free_inodes_count(sb, desc))
6812aa9fc4cSAvantika Mathur 			return 0;
682ac27a0ecSDave Kleikamp 	}
683ac27a0ecSDave Kleikamp 
684ac27a0ecSDave Kleikamp 	return -1;
685ac27a0ecSDave Kleikamp }
686ac27a0ecSDave Kleikamp 
687ac27a0ecSDave Kleikamp /*
68819883bd9STheodore Ts'o  * In no journal mode, if an inode has recently been deleted, we want
68919883bd9STheodore Ts'o  * to avoid reusing it until we're reasonably sure the inode table
69019883bd9STheodore Ts'o  * block has been written back to disk.  (Yes, these values are
69119883bd9STheodore Ts'o  * somewhat arbitrary...)
69219883bd9STheodore Ts'o  */
69319883bd9STheodore Ts'o #define RECENTCY_MIN	5
69419883bd9STheodore Ts'o #define RECENTCY_DIRTY	30
69519883bd9STheodore Ts'o 
69619883bd9STheodore Ts'o static int recently_deleted(struct super_block *sb, ext4_group_t group, int ino)
69719883bd9STheodore Ts'o {
69819883bd9STheodore Ts'o 	struct ext4_group_desc	*gdp;
69919883bd9STheodore Ts'o 	struct ext4_inode	*raw_inode;
70019883bd9STheodore Ts'o 	struct buffer_head	*bh;
70119883bd9STheodore Ts'o 	unsigned long		dtime, now;
70219883bd9STheodore Ts'o 	int	inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
70319883bd9STheodore Ts'o 	int	offset, ret = 0, recentcy = RECENTCY_MIN;
70419883bd9STheodore Ts'o 
70519883bd9STheodore Ts'o 	gdp = ext4_get_group_desc(sb, group, NULL);
70619883bd9STheodore Ts'o 	if (unlikely(!gdp))
70719883bd9STheodore Ts'o 		return 0;
70819883bd9STheodore Ts'o 
70919883bd9STheodore Ts'o 	bh = sb_getblk(sb, ext4_inode_table(sb, gdp) +
71019883bd9STheodore Ts'o 		       (ino / inodes_per_block));
71119883bd9STheodore Ts'o 	if (unlikely(!bh) || !buffer_uptodate(bh))
71219883bd9STheodore Ts'o 		/*
71319883bd9STheodore Ts'o 		 * If the block is not in the buffer cache, then it
71419883bd9STheodore Ts'o 		 * must have been written out.
71519883bd9STheodore Ts'o 		 */
71619883bd9STheodore Ts'o 		goto out;
71719883bd9STheodore Ts'o 
71819883bd9STheodore Ts'o 	offset = (ino % inodes_per_block) * EXT4_INODE_SIZE(sb);
71919883bd9STheodore Ts'o 	raw_inode = (struct ext4_inode *) (bh->b_data + offset);
72019883bd9STheodore Ts'o 	dtime = le32_to_cpu(raw_inode->i_dtime);
72119883bd9STheodore Ts'o 	now = get_seconds();
72219883bd9STheodore Ts'o 	if (buffer_dirty(bh))
72319883bd9STheodore Ts'o 		recentcy += RECENTCY_DIRTY;
72419883bd9STheodore Ts'o 
72519883bd9STheodore Ts'o 	if (dtime && (dtime < now) && (now < dtime + recentcy))
72619883bd9STheodore Ts'o 		ret = 1;
72719883bd9STheodore Ts'o out:
72819883bd9STheodore Ts'o 	brelse(bh);
72919883bd9STheodore Ts'o 	return ret;
73019883bd9STheodore Ts'o }
73119883bd9STheodore Ts'o 
73219883bd9STheodore Ts'o /*
733ac27a0ecSDave Kleikamp  * There are two policies for allocating an inode.  If the new inode is
734ac27a0ecSDave Kleikamp  * a directory, then a forward search is made for a block group with both
735ac27a0ecSDave Kleikamp  * free space and a low directory-to-inode ratio; if that fails, then of
736ac27a0ecSDave Kleikamp  * the groups with above-average free space, that group with the fewest
737ac27a0ecSDave Kleikamp  * directories already is chosen.
738ac27a0ecSDave Kleikamp  *
739ac27a0ecSDave Kleikamp  * For other inodes, search forward from the parent directory's block
740ac27a0ecSDave Kleikamp  * group to find a free inode.
741ac27a0ecSDave Kleikamp  */
7421139575aSTheodore Ts'o struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
7431139575aSTheodore Ts'o 			       umode_t mode, const struct qstr *qstr,
7441139575aSTheodore Ts'o 			       __u32 goal, uid_t *owner, int handle_type,
7451139575aSTheodore Ts'o 			       unsigned int line_no, int nblocks)
746ac27a0ecSDave Kleikamp {
747ac27a0ecSDave Kleikamp 	struct super_block *sb;
7483300bedaSAneesh Kumar K.V 	struct buffer_head *inode_bitmap_bh = NULL;
7493300bedaSAneesh Kumar K.V 	struct buffer_head *group_desc_bh;
7508df9675fSTheodore Ts'o 	ext4_group_t ngroups, group = 0;
751ac27a0ecSDave Kleikamp 	unsigned long ino = 0;
752ac27a0ecSDave Kleikamp 	struct inode *inode;
753617ba13bSMingming Cao 	struct ext4_group_desc *gdp = NULL;
754617ba13bSMingming Cao 	struct ext4_inode_info *ei;
755617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
756a7cdadeeSJan Kara 	int ret2, err;
757ac27a0ecSDave Kleikamp 	struct inode *ret;
7582aa9fc4cSAvantika Mathur 	ext4_group_t i;
759772cb7c8SJose R. Santos 	ext4_group_t flex_group;
76087a39389SDarrick J. Wong 	struct ext4_group_info *grp;
761e709e9dfSTheodore Ts'o 	int encrypt = 0;
762ac27a0ecSDave Kleikamp 
763ac27a0ecSDave Kleikamp 	/* Cannot create files in a deleted directory */
764ac27a0ecSDave Kleikamp 	if (!dir || !dir->i_nlink)
765ac27a0ecSDave Kleikamp 		return ERR_PTR(-EPERM);
766ac27a0ecSDave Kleikamp 
767e709e9dfSTheodore Ts'o 	if ((ext4_encrypted_inode(dir) ||
768e709e9dfSTheodore Ts'o 	     DUMMY_ENCRYPTION_ENABLED(EXT4_SB(dir->i_sb))) &&
769e709e9dfSTheodore Ts'o 	    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
770e709e9dfSTheodore Ts'o 		err = ext4_get_encryption_info(dir);
771e709e9dfSTheodore Ts'o 		if (err)
772e709e9dfSTheodore Ts'o 			return ERR_PTR(err);
773e709e9dfSTheodore Ts'o 		if (ext4_encryption_info(dir) == NULL)
774e709e9dfSTheodore Ts'o 			return ERR_PTR(-EPERM);
775e709e9dfSTheodore Ts'o 		if (!handle)
776e709e9dfSTheodore Ts'o 			nblocks += EXT4_DATA_TRANS_BLOCKS(dir->i_sb);
777e709e9dfSTheodore Ts'o 		encrypt = 1;
778e709e9dfSTheodore Ts'o 	}
779e709e9dfSTheodore Ts'o 
780ac27a0ecSDave Kleikamp 	sb = dir->i_sb;
7818df9675fSTheodore Ts'o 	ngroups = ext4_get_groups_count(sb);
7829bffad1eSTheodore Ts'o 	trace_ext4_request_inode(dir, mode);
783ac27a0ecSDave Kleikamp 	inode = new_inode(sb);
784ac27a0ecSDave Kleikamp 	if (!inode)
785ac27a0ecSDave Kleikamp 		return ERR_PTR(-ENOMEM);
786617ba13bSMingming Cao 	ei = EXT4_I(inode);
787617ba13bSMingming Cao 	sbi = EXT4_SB(sb);
788772cb7c8SJose R. Santos 
789eb9cc7e1SJan Kara 	/*
790b8a07463SAdam Buchbinder 	 * Initialize owners and quota early so that we don't have to account
791eb9cc7e1SJan Kara 	 * for quota initialization worst case in standard inode creating
792eb9cc7e1SJan Kara 	 * transaction
793eb9cc7e1SJan Kara 	 */
794eb9cc7e1SJan Kara 	if (owner) {
795eb9cc7e1SJan Kara 		inode->i_mode = mode;
796eb9cc7e1SJan Kara 		i_uid_write(inode, owner[0]);
797eb9cc7e1SJan Kara 		i_gid_write(inode, owner[1]);
798eb9cc7e1SJan Kara 	} else if (test_opt(sb, GRPID)) {
799eb9cc7e1SJan Kara 		inode->i_mode = mode;
800eb9cc7e1SJan Kara 		inode->i_uid = current_fsuid();
801eb9cc7e1SJan Kara 		inode->i_gid = dir->i_gid;
802eb9cc7e1SJan Kara 	} else
803eb9cc7e1SJan Kara 		inode_init_owner(inode, dir, mode);
804040cb378SLi Xi 
805040cb378SLi Xi 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_PROJECT) &&
806040cb378SLi Xi 	    ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT))
807040cb378SLi Xi 		ei->i_projid = EXT4_I(dir)->i_projid;
808040cb378SLi Xi 	else
809040cb378SLi Xi 		ei->i_projid = make_kprojid(&init_user_ns, EXT4_DEF_PROJID);
810040cb378SLi Xi 
811a7cdadeeSJan Kara 	err = dquot_initialize(inode);
812a7cdadeeSJan Kara 	if (err)
813a7cdadeeSJan Kara 		goto out;
814eb9cc7e1SJan Kara 
81511013911SAndreas Dilger 	if (!goal)
81611013911SAndreas Dilger 		goal = sbi->s_inode_goal;
81711013911SAndreas Dilger 
818e6462869SJohann Lombardi 	if (goal && goal <= le32_to_cpu(sbi->s_es->s_inodes_count)) {
81911013911SAndreas Dilger 		group = (goal - 1) / EXT4_INODES_PER_GROUP(sb);
82011013911SAndreas Dilger 		ino = (goal - 1) % EXT4_INODES_PER_GROUP(sb);
82111013911SAndreas Dilger 		ret2 = 0;
82211013911SAndreas Dilger 		goto got_group;
82311013911SAndreas Dilger 	}
82411013911SAndreas Dilger 
8254113c4caSLukas Czerner 	if (S_ISDIR(mode))
826f157a4aaSTheodore Ts'o 		ret2 = find_group_orlov(sb, dir, &group, mode, qstr);
8274113c4caSLukas Czerner 	else
828a4912123STheodore Ts'o 		ret2 = find_group_other(sb, dir, &group, mode);
829ac27a0ecSDave Kleikamp 
830772cb7c8SJose R. Santos got_group:
831a4912123STheodore Ts'o 	EXT4_I(dir)->i_last_alloc_group = group;
832ac27a0ecSDave Kleikamp 	err = -ENOSPC;
8332aa9fc4cSAvantika Mathur 	if (ret2 == -1)
834ac27a0ecSDave Kleikamp 		goto out;
835ac27a0ecSDave Kleikamp 
836119c0d44STheodore Ts'o 	/*
837119c0d44STheodore Ts'o 	 * Normally we will only go through one pass of this loop,
838119c0d44STheodore Ts'o 	 * unless we get unlucky and it turns out the group we selected
839119c0d44STheodore Ts'o 	 * had its last inode grabbed by someone else.
840119c0d44STheodore Ts'o 	 */
84111013911SAndreas Dilger 	for (i = 0; i < ngroups; i++, ino = 0) {
842ac27a0ecSDave Kleikamp 		err = -EIO;
843ac27a0ecSDave Kleikamp 
8443300bedaSAneesh Kumar K.V 		gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
845ac27a0ecSDave Kleikamp 		if (!gdp)
846eb9cc7e1SJan Kara 			goto out;
847ac27a0ecSDave Kleikamp 
848f2a09af6SYongqiang Yang 		/*
849f2a09af6SYongqiang Yang 		 * Check free inodes count before loading bitmap.
850f2a09af6SYongqiang Yang 		 */
851f2a09af6SYongqiang Yang 		if (ext4_free_inodes_count(sb, gdp) == 0) {
852f2a09af6SYongqiang Yang 			if (++group == ngroups)
853f2a09af6SYongqiang Yang 				group = 0;
854f2a09af6SYongqiang Yang 			continue;
855f2a09af6SYongqiang Yang 		}
856f2a09af6SYongqiang Yang 
85787a39389SDarrick J. Wong 		grp = ext4_get_group_info(sb, group);
85887a39389SDarrick J. Wong 		/* Skip groups with already-known suspicious inode tables */
85987a39389SDarrick J. Wong 		if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) {
86087a39389SDarrick J. Wong 			if (++group == ngroups)
86187a39389SDarrick J. Wong 				group = 0;
86287a39389SDarrick J. Wong 			continue;
86387a39389SDarrick J. Wong 		}
86487a39389SDarrick J. Wong 
8653300bedaSAneesh Kumar K.V 		brelse(inode_bitmap_bh);
8663300bedaSAneesh Kumar K.V 		inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
86787a39389SDarrick J. Wong 		/* Skip groups with suspicious inode tables */
8689008a58eSDarrick J. Wong 		if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp) ||
8699008a58eSDarrick J. Wong 		    IS_ERR(inode_bitmap_bh)) {
8709008a58eSDarrick J. Wong 			inode_bitmap_bh = NULL;
87187a39389SDarrick J. Wong 			if (++group == ngroups)
87287a39389SDarrick J. Wong 				group = 0;
87387a39389SDarrick J. Wong 			continue;
87487a39389SDarrick J. Wong 		}
875ac27a0ecSDave Kleikamp 
876ac27a0ecSDave Kleikamp repeat_in_this_group:
877617ba13bSMingming Cao 		ino = ext4_find_next_zero_bit((unsigned long *)
8783300bedaSAneesh Kumar K.V 					      inode_bitmap_bh->b_data,
8793300bedaSAneesh Kumar K.V 					      EXT4_INODES_PER_GROUP(sb), ino);
880a34eb503STheodore Ts'o 		if (ino >= EXT4_INODES_PER_GROUP(sb))
881a34eb503STheodore Ts'o 			goto next_group;
882119c0d44STheodore Ts'o 		if (group == 0 && (ino+1) < EXT4_FIRST_INO(sb)) {
883119c0d44STheodore Ts'o 			ext4_error(sb, "reserved inode found cleared - "
884119c0d44STheodore Ts'o 				   "inode=%lu", ino + 1);
885119c0d44STheodore Ts'o 			continue;
886119c0d44STheodore Ts'o 		}
88719883bd9STheodore Ts'o 		if ((EXT4_SB(sb)->s_journal == NULL) &&
88819883bd9STheodore Ts'o 		    recently_deleted(sb, group, ino)) {
88919883bd9STheodore Ts'o 			ino++;
89019883bd9STheodore Ts'o 			goto next_inode;
89119883bd9STheodore Ts'o 		}
8921139575aSTheodore Ts'o 		if (!handle) {
8931139575aSTheodore Ts'o 			BUG_ON(nblocks <= 0);
8941139575aSTheodore Ts'o 			handle = __ext4_journal_start_sb(dir->i_sb, line_no,
8955fe2fe89SJan Kara 							 handle_type, nblocks,
8965fe2fe89SJan Kara 							 0);
8971139575aSTheodore Ts'o 			if (IS_ERR(handle)) {
8981139575aSTheodore Ts'o 				err = PTR_ERR(handle);
899eb9cc7e1SJan Kara 				ext4_std_error(sb, err);
900eb9cc7e1SJan Kara 				goto out;
9011139575aSTheodore Ts'o 			}
9021139575aSTheodore Ts'o 		}
903ffb5387eSEric Sandeen 		BUFFER_TRACE(inode_bitmap_bh, "get_write_access");
904ffb5387eSEric Sandeen 		err = ext4_journal_get_write_access(handle, inode_bitmap_bh);
905eb9cc7e1SJan Kara 		if (err) {
906eb9cc7e1SJan Kara 			ext4_std_error(sb, err);
907eb9cc7e1SJan Kara 			goto out;
908eb9cc7e1SJan Kara 		}
909119c0d44STheodore Ts'o 		ext4_lock_group(sb, group);
910119c0d44STheodore Ts'o 		ret2 = ext4_test_and_set_bit(ino, inode_bitmap_bh->b_data);
911119c0d44STheodore Ts'o 		ext4_unlock_group(sb, group);
912119c0d44STheodore Ts'o 		ino++;		/* the inode bitmap is zero-based */
913119c0d44STheodore Ts'o 		if (!ret2)
914119c0d44STheodore Ts'o 			goto got; /* we grabbed the inode! */
91519883bd9STheodore Ts'o next_inode:
916119c0d44STheodore Ts'o 		if (ino < EXT4_INODES_PER_GROUP(sb))
917119c0d44STheodore Ts'o 			goto repeat_in_this_group;
918a34eb503STheodore Ts'o next_group:
919a34eb503STheodore Ts'o 		if (++group == ngroups)
920a34eb503STheodore Ts'o 			group = 0;
921ac27a0ecSDave Kleikamp 	}
922ac27a0ecSDave Kleikamp 	err = -ENOSPC;
923ac27a0ecSDave Kleikamp 	goto out;
924ac27a0ecSDave Kleikamp 
925ac27a0ecSDave Kleikamp got:
926ffb5387eSEric Sandeen 	BUFFER_TRACE(inode_bitmap_bh, "call ext4_handle_dirty_metadata");
927ffb5387eSEric Sandeen 	err = ext4_handle_dirty_metadata(handle, NULL, inode_bitmap_bh);
928eb9cc7e1SJan Kara 	if (err) {
929eb9cc7e1SJan Kara 		ext4_std_error(sb, err);
930eb9cc7e1SJan Kara 		goto out;
931eb9cc7e1SJan Kara 	}
932ffb5387eSEric Sandeen 
93361c219f5STheodore Ts'o 	BUFFER_TRACE(group_desc_bh, "get_write_access");
93461c219f5STheodore Ts'o 	err = ext4_journal_get_write_access(handle, group_desc_bh);
93561c219f5STheodore Ts'o 	if (err) {
93661c219f5STheodore Ts'o 		ext4_std_error(sb, err);
93761c219f5STheodore Ts'o 		goto out;
93861c219f5STheodore Ts'o 	}
93961c219f5STheodore Ts'o 
940717d50e4SAndreas Dilger 	/* We may have to initialize the block bitmap if it isn't already */
941feb0ab32SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb) &&
942717d50e4SAndreas Dilger 	    gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
9433300bedaSAneesh Kumar K.V 		struct buffer_head *block_bitmap_bh;
944717d50e4SAndreas Dilger 
9453300bedaSAneesh Kumar K.V 		block_bitmap_bh = ext4_read_block_bitmap(sb, group);
9469008a58eSDarrick J. Wong 		if (IS_ERR(block_bitmap_bh)) {
9479008a58eSDarrick J. Wong 			err = PTR_ERR(block_bitmap_bh);
948599a9b77SJan Kara 			goto out;
949599a9b77SJan Kara 		}
9503300bedaSAneesh Kumar K.V 		BUFFER_TRACE(block_bitmap_bh, "get block bitmap access");
9513300bedaSAneesh Kumar K.V 		err = ext4_journal_get_write_access(handle, block_bitmap_bh);
952717d50e4SAndreas Dilger 		if (err) {
9533300bedaSAneesh Kumar K.V 			brelse(block_bitmap_bh);
954eb9cc7e1SJan Kara 			ext4_std_error(sb, err);
955eb9cc7e1SJan Kara 			goto out;
956717d50e4SAndreas Dilger 		}
957717d50e4SAndreas Dilger 
958fd034a84STheodore Ts'o 		BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
959fd034a84STheodore Ts'o 		err = ext4_handle_dirty_metadata(handle, NULL, block_bitmap_bh);
960fd034a84STheodore Ts'o 
961717d50e4SAndreas Dilger 		/* recheck and clear flag under lock if we still need to */
962fd034a84STheodore Ts'o 		ext4_lock_group(sb, group);
963717d50e4SAndreas Dilger 		if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
9643300bedaSAneesh Kumar K.V 			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
965021b65bbSTheodore Ts'o 			ext4_free_group_clusters_set(sb, gdp,
966cff1dfd7STheodore Ts'o 				ext4_free_clusters_after_init(sb, group, gdp));
967fa77dcfaSDarrick J. Wong 			ext4_block_bitmap_csum_set(sb, group, gdp,
96879f1ba49STao Ma 						   block_bitmap_bh);
969feb0ab32SDarrick J. Wong 			ext4_group_desc_csum_set(sb, group, gdp);
970717d50e4SAndreas Dilger 		}
971955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, group);
972aeb1e5d6STheodore Ts'o 		brelse(block_bitmap_bh);
973717d50e4SAndreas Dilger 
974eb9cc7e1SJan Kara 		if (err) {
975eb9cc7e1SJan Kara 			ext4_std_error(sb, err);
976eb9cc7e1SJan Kara 			goto out;
977eb9cc7e1SJan Kara 		}
978717d50e4SAndreas Dilger 	}
979119c0d44STheodore Ts'o 
980119c0d44STheodore Ts'o 	/* Update the relevant bg descriptor fields */
98141a246d1SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb)) {
982119c0d44STheodore Ts'o 		int free;
983119c0d44STheodore Ts'o 		struct ext4_group_info *grp = ext4_get_group_info(sb, group);
984119c0d44STheodore Ts'o 
985119c0d44STheodore Ts'o 		down_read(&grp->alloc_sem); /* protect vs itable lazyinit */
986119c0d44STheodore Ts'o 		ext4_lock_group(sb, group); /* while we modify the bg desc */
987119c0d44STheodore Ts'o 		free = EXT4_INODES_PER_GROUP(sb) -
988119c0d44STheodore Ts'o 			ext4_itable_unused_count(sb, gdp);
989119c0d44STheodore Ts'o 		if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
990119c0d44STheodore Ts'o 			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
991119c0d44STheodore Ts'o 			free = 0;
992119c0d44STheodore Ts'o 		}
993119c0d44STheodore Ts'o 		/*
994119c0d44STheodore Ts'o 		 * Check the relative inode number against the last used
995119c0d44STheodore Ts'o 		 * relative inode number in this group. if it is greater
996119c0d44STheodore Ts'o 		 * we need to update the bg_itable_unused count
997119c0d44STheodore Ts'o 		 */
998119c0d44STheodore Ts'o 		if (ino > free)
999119c0d44STheodore Ts'o 			ext4_itable_unused_set(sb, gdp,
1000119c0d44STheodore Ts'o 					(EXT4_INODES_PER_GROUP(sb) - ino));
1001119c0d44STheodore Ts'o 		up_read(&grp->alloc_sem);
10026f2e9f0eSTao Ma 	} else {
10036f2e9f0eSTao Ma 		ext4_lock_group(sb, group);
1004119c0d44STheodore Ts'o 	}
10056f2e9f0eSTao Ma 
1006119c0d44STheodore Ts'o 	ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1);
1007119c0d44STheodore Ts'o 	if (S_ISDIR(mode)) {
1008119c0d44STheodore Ts'o 		ext4_used_dirs_set(sb, gdp, ext4_used_dirs_count(sb, gdp) + 1);
1009119c0d44STheodore Ts'o 		if (sbi->s_log_groups_per_flex) {
1010119c0d44STheodore Ts'o 			ext4_group_t f = ext4_flex_group(sbi, group);
1011119c0d44STheodore Ts'o 
1012119c0d44STheodore Ts'o 			atomic_inc(&sbi->s_flex_groups[f].used_dirs);
1013119c0d44STheodore Ts'o 		}
1014119c0d44STheodore Ts'o 	}
101541a246d1SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb)) {
101641a246d1SDarrick J. Wong 		ext4_inode_bitmap_csum_set(sb, group, gdp, inode_bitmap_bh,
101741a246d1SDarrick J. Wong 					   EXT4_INODES_PER_GROUP(sb) / 8);
1018feb0ab32SDarrick J. Wong 		ext4_group_desc_csum_set(sb, group, gdp);
1019119c0d44STheodore Ts'o 	}
10206f2e9f0eSTao Ma 	ext4_unlock_group(sb, group);
1021119c0d44STheodore Ts'o 
10223300bedaSAneesh Kumar K.V 	BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata");
10233300bedaSAneesh Kumar K.V 	err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh);
1024eb9cc7e1SJan Kara 	if (err) {
1025eb9cc7e1SJan Kara 		ext4_std_error(sb, err);
1026eb9cc7e1SJan Kara 		goto out;
1027eb9cc7e1SJan Kara 	}
1028ac27a0ecSDave Kleikamp 
1029ac27a0ecSDave Kleikamp 	percpu_counter_dec(&sbi->s_freeinodes_counter);
1030ac27a0ecSDave Kleikamp 	if (S_ISDIR(mode))
1031ac27a0ecSDave Kleikamp 		percpu_counter_inc(&sbi->s_dirs_counter);
1032ac27a0ecSDave Kleikamp 
1033772cb7c8SJose R. Santos 	if (sbi->s_log_groups_per_flex) {
1034772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, group);
10359f24e420STheodore Ts'o 		atomic_dec(&sbi->s_flex_groups[flex_group].free_inodes);
1036772cb7c8SJose R. Santos 	}
1037ac27a0ecSDave Kleikamp 
1038717d50e4SAndreas Dilger 	inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
1039ac27a0ecSDave Kleikamp 	/* This is the optimal IO size (for stat), not the fs block size */
1040ac27a0ecSDave Kleikamp 	inode->i_blocks = 0;
1041ef7f3835SKalpak Shah 	inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
1042ef7f3835SKalpak Shah 						       ext4_current_time(inode);
1043ac27a0ecSDave Kleikamp 
1044ac27a0ecSDave Kleikamp 	memset(ei->i_data, 0, sizeof(ei->i_data));
1045ac27a0ecSDave Kleikamp 	ei->i_dir_start_lookup = 0;
1046ac27a0ecSDave Kleikamp 	ei->i_disksize = 0;
1047ac27a0ecSDave Kleikamp 
10484af83508SEryu Guan 	/* Don't inherit extent flag from directory, amongst others. */
10492dc6b0d4SDuane Griffin 	ei->i_flags =
10502dc6b0d4SDuane Griffin 		ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED);
1051ac27a0ecSDave Kleikamp 	ei->i_file_acl = 0;
1052ac27a0ecSDave Kleikamp 	ei->i_dtime = 0;
1053ac27a0ecSDave Kleikamp 	ei->i_block_group = group;
1054a4912123STheodore Ts'o 	ei->i_last_alloc_group = ~0;
1055ac27a0ecSDave Kleikamp 
1056617ba13bSMingming Cao 	ext4_set_inode_flags(inode);
1057ac27a0ecSDave Kleikamp 	if (IS_DIRSYNC(inode))
10580390131bSFrank Mayhar 		ext4_handle_sync(handle);
10596b38e842SAl Viro 	if (insert_inode_locked(inode) < 0) {
1060acd6ad83SJan Kara 		/*
1061acd6ad83SJan Kara 		 * Likely a bitmap corruption causing inode to be allocated
1062acd6ad83SJan Kara 		 * twice.
1063acd6ad83SJan Kara 		 */
1064acd6ad83SJan Kara 		err = -EIO;
1065eb9cc7e1SJan Kara 		ext4_error(sb, "failed to insert inode %lu: doubly allocated?",
1066eb9cc7e1SJan Kara 			   inode->i_ino);
1067eb9cc7e1SJan Kara 		goto out;
10686b38e842SAl Viro 	}
1069ac27a0ecSDave Kleikamp 	spin_lock(&sbi->s_next_gen_lock);
1070ac27a0ecSDave Kleikamp 	inode->i_generation = sbi->s_next_generation++;
1071ac27a0ecSDave Kleikamp 	spin_unlock(&sbi->s_next_gen_lock);
1072ac27a0ecSDave Kleikamp 
1073814525f4SDarrick J. Wong 	/* Precompute checksum seed for inode metadata */
10749aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sb)) {
1075814525f4SDarrick J. Wong 		__u32 csum;
1076814525f4SDarrick J. Wong 		__le32 inum = cpu_to_le32(inode->i_ino);
1077814525f4SDarrick J. Wong 		__le32 gen = cpu_to_le32(inode->i_generation);
1078814525f4SDarrick J. Wong 		csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
1079814525f4SDarrick J. Wong 				   sizeof(inum));
1080814525f4SDarrick J. Wong 		ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
1081814525f4SDarrick J. Wong 					      sizeof(gen));
1082814525f4SDarrick J. Wong 	}
1083814525f4SDarrick J. Wong 
1084353eb83cSTheodore Ts'o 	ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
108519f5fb7aSTheodore Ts'o 	ext4_set_inode_state(inode, EXT4_STATE_NEW);
1086ef7f3835SKalpak Shah 
1087ef7f3835SKalpak Shah 	ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize;
1088f08225d1STao Ma 	ei->i_inline_off = 0;
1089e2b911c5SDarrick J. Wong 	if (ext4_has_feature_inline_data(sb))
1090f08225d1STao Ma 		ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
1091ac27a0ecSDave Kleikamp 	ret = inode;
109263936ddaSChristoph Hellwig 	err = dquot_alloc_inode(inode);
109363936ddaSChristoph Hellwig 	if (err)
1094ac27a0ecSDave Kleikamp 		goto fail_drop;
1095ac27a0ecSDave Kleikamp 
1096617ba13bSMingming Cao 	err = ext4_init_acl(handle, inode, dir);
1097ac27a0ecSDave Kleikamp 	if (err)
1098ac27a0ecSDave Kleikamp 		goto fail_free_drop;
1099ac27a0ecSDave Kleikamp 
11002a7dba39SEric Paris 	err = ext4_init_security(handle, inode, dir, qstr);
1101ac27a0ecSDave Kleikamp 	if (err)
1102ac27a0ecSDave Kleikamp 		goto fail_free_drop;
1103ac27a0ecSDave Kleikamp 
1104e2b911c5SDarrick J. Wong 	if (ext4_has_feature_extents(sb)) {
1105e4079a11SEric Sandeen 		/* set extent flag only for directory, file and normal symlink*/
1106e65187e6SAneesh Kumar K.V 		if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
110712e9b892SDmitry Monakhov 			ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
1108a86c6181SAlex Tomas 			ext4_ext_tree_init(handle, inode);
1109a86c6181SAlex Tomas 		}
111042bf0383SAneesh Kumar K.V 	}
1111ac27a0ecSDave Kleikamp 
1112688f869cSTheodore Ts'o 	if (ext4_handle_valid(handle)) {
1113688f869cSTheodore Ts'o 		ei->i_sync_tid = handle->h_transaction->t_tid;
1114688f869cSTheodore Ts'o 		ei->i_datasync_tid = handle->h_transaction->t_tid;
1115688f869cSTheodore Ts'o 	}
1116688f869cSTheodore Ts'o 
1117e709e9dfSTheodore Ts'o 	if (encrypt) {
1118e709e9dfSTheodore Ts'o 		err = ext4_inherit_context(dir, inode);
1119e709e9dfSTheodore Ts'o 		if (err)
1120e709e9dfSTheodore Ts'o 			goto fail_free_drop;
1121e709e9dfSTheodore Ts'o 	}
1122e709e9dfSTheodore Ts'o 
11238753e88fSAneesh Kumar K.V 	err = ext4_mark_inode_dirty(handle, inode);
11248753e88fSAneesh Kumar K.V 	if (err) {
11258753e88fSAneesh Kumar K.V 		ext4_std_error(sb, err);
11268753e88fSAneesh Kumar K.V 		goto fail_free_drop;
11278753e88fSAneesh Kumar K.V 	}
11288753e88fSAneesh Kumar K.V 
1129617ba13bSMingming Cao 	ext4_debug("allocating inode %lu\n", inode->i_ino);
11309bffad1eSTheodore Ts'o 	trace_ext4_allocate_inode(inode, dir, mode);
11313300bedaSAneesh Kumar K.V 	brelse(inode_bitmap_bh);
1132ac27a0ecSDave Kleikamp 	return ret;
1133ac27a0ecSDave Kleikamp 
1134ac27a0ecSDave Kleikamp fail_free_drop:
113563936ddaSChristoph Hellwig 	dquot_free_inode(inode);
1136ac27a0ecSDave Kleikamp fail_drop:
11376d6b77f1SMiklos Szeredi 	clear_nlink(inode);
11386b38e842SAl Viro 	unlock_new_inode(inode);
1139eb9cc7e1SJan Kara out:
1140eb9cc7e1SJan Kara 	dquot_drop(inode);
1141eb9cc7e1SJan Kara 	inode->i_flags |= S_NOQUOTA;
1142ac27a0ecSDave Kleikamp 	iput(inode);
11433300bedaSAneesh Kumar K.V 	brelse(inode_bitmap_bh);
1144ac27a0ecSDave Kleikamp 	return ERR_PTR(err);
1145ac27a0ecSDave Kleikamp }
1146ac27a0ecSDave Kleikamp 
1147ac27a0ecSDave Kleikamp /* Verify that we are loading a valid orphan from disk */
1148617ba13bSMingming Cao struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
1149ac27a0ecSDave Kleikamp {
1150617ba13bSMingming Cao 	unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
1151fd2d4291SAvantika Mathur 	ext4_group_t block_group;
1152ac27a0ecSDave Kleikamp 	int bit;
11531d1fe1eeSDavid Howells 	struct buffer_head *bitmap_bh;
1154ac27a0ecSDave Kleikamp 	struct inode *inode = NULL;
11551d1fe1eeSDavid Howells 	long err = -EIO;
1156ac27a0ecSDave Kleikamp 
1157ac27a0ecSDave Kleikamp 	/* Error cases - e2fsck has already cleaned up for us */
1158ac27a0ecSDave Kleikamp 	if (ino > max_ino) {
115912062dddSEric Sandeen 		ext4_warning(sb, "bad orphan ino %lu!  e2fsck was run?", ino);
11606a797d27SDarrick J. Wong 		err = -EFSCORRUPTED;
11611d1fe1eeSDavid Howells 		goto error;
1162ac27a0ecSDave Kleikamp 	}
1163ac27a0ecSDave Kleikamp 
1164617ba13bSMingming Cao 	block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
1165617ba13bSMingming Cao 	bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
1166e29d1cdeSEric Sandeen 	bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
11679008a58eSDarrick J. Wong 	if (IS_ERR(bitmap_bh)) {
11689008a58eSDarrick J. Wong 		err = PTR_ERR(bitmap_bh);
11699008a58eSDarrick J. Wong 		ext4_warning(sb, "inode bitmap error %ld for orphan %lu",
11709008a58eSDarrick J. Wong 			     ino, err);
11711d1fe1eeSDavid Howells 		goto error;
1172ac27a0ecSDave Kleikamp 	}
1173ac27a0ecSDave Kleikamp 
1174ac27a0ecSDave Kleikamp 	/* Having the inode bit set should be a 100% indicator that this
1175ac27a0ecSDave Kleikamp 	 * is a valid orphan (no e2fsck run on fs).  Orphans also include
1176ac27a0ecSDave Kleikamp 	 * inodes that were being truncated, so we can't check i_nlink==0.
1177ac27a0ecSDave Kleikamp 	 */
11781d1fe1eeSDavid Howells 	if (!ext4_test_bit(bit, bitmap_bh->b_data))
11791d1fe1eeSDavid Howells 		goto bad_orphan;
11801d1fe1eeSDavid Howells 
11811d1fe1eeSDavid Howells 	inode = ext4_iget(sb, ino);
11821d1fe1eeSDavid Howells 	if (IS_ERR(inode))
11831d1fe1eeSDavid Howells 		goto iget_failed;
11841d1fe1eeSDavid Howells 
118591ef4cafSDuane Griffin 	/*
1186*c9eb13a9STheodore Ts'o 	 * If the orphans has i_nlinks > 0 then it should be able to
1187*c9eb13a9STheodore Ts'o 	 * be truncated, otherwise it won't be removed from the orphan
1188*c9eb13a9STheodore Ts'o 	 * list during processing and an infinite loop will result.
1189*c9eb13a9STheodore Ts'o 	 * Similarly, it must not be a bad inode.
119091ef4cafSDuane Griffin 	 */
1191*c9eb13a9STheodore Ts'o 	if ((inode->i_nlink && !ext4_can_truncate(inode)) ||
1192*c9eb13a9STheodore Ts'o 	    is_bad_inode(inode))
119391ef4cafSDuane Griffin 		goto bad_orphan;
119491ef4cafSDuane Griffin 
11951d1fe1eeSDavid Howells 	if (NEXT_ORPHAN(inode) > max_ino)
11961d1fe1eeSDavid Howells 		goto bad_orphan;
11971d1fe1eeSDavid Howells 	brelse(bitmap_bh);
11981d1fe1eeSDavid Howells 	return inode;
11991d1fe1eeSDavid Howells 
12001d1fe1eeSDavid Howells iget_failed:
12011d1fe1eeSDavid Howells 	err = PTR_ERR(inode);
12021d1fe1eeSDavid Howells 	inode = NULL;
12031d1fe1eeSDavid Howells bad_orphan:
120412062dddSEric Sandeen 	ext4_warning(sb, "bad orphan inode %lu!  e2fsck was run?", ino);
12058de5c325STheodore Ts'o 	printk(KERN_WARNING "ext4_test_bit(bit=%d, block=%llu) = %d\n",
1206ac27a0ecSDave Kleikamp 	       bit, (unsigned long long)bitmap_bh->b_blocknr,
1207617ba13bSMingming Cao 	       ext4_test_bit(bit, bitmap_bh->b_data));
12088de5c325STheodore Ts'o 	printk(KERN_WARNING "inode=%p\n", inode);
1209ac27a0ecSDave Kleikamp 	if (inode) {
12108de5c325STheodore Ts'o 		printk(KERN_WARNING "is_bad_inode(inode)=%d\n",
1211ac27a0ecSDave Kleikamp 		       is_bad_inode(inode));
12128de5c325STheodore Ts'o 		printk(KERN_WARNING "NEXT_ORPHAN(inode)=%u\n",
1213ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
12148de5c325STheodore Ts'o 		printk(KERN_WARNING "max_ino=%lu\n", max_ino);
12158de5c325STheodore Ts'o 		printk(KERN_WARNING "i_nlink=%u\n", inode->i_nlink);
1216ac27a0ecSDave Kleikamp 		/* Avoid freeing blocks if we got a bad deleted inode */
12171d1fe1eeSDavid Howells 		if (inode->i_nlink == 0)
1218ac27a0ecSDave Kleikamp 			inode->i_blocks = 0;
1219ac27a0ecSDave Kleikamp 		iput(inode);
1220ac27a0ecSDave Kleikamp 	}
1221ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
12221d1fe1eeSDavid Howells error:
12231d1fe1eeSDavid Howells 	return ERR_PTR(err);
1224ac27a0ecSDave Kleikamp }
1225ac27a0ecSDave Kleikamp 
1226617ba13bSMingming Cao unsigned long ext4_count_free_inodes(struct super_block *sb)
1227ac27a0ecSDave Kleikamp {
1228ac27a0ecSDave Kleikamp 	unsigned long desc_count;
1229617ba13bSMingming Cao 	struct ext4_group_desc *gdp;
12308df9675fSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
1231617ba13bSMingming Cao #ifdef EXT4FS_DEBUG
1232617ba13bSMingming Cao 	struct ext4_super_block *es;
1233ac27a0ecSDave Kleikamp 	unsigned long bitmap_count, x;
1234ac27a0ecSDave Kleikamp 	struct buffer_head *bitmap_bh = NULL;
1235ac27a0ecSDave Kleikamp 
1236617ba13bSMingming Cao 	es = EXT4_SB(sb)->s_es;
1237ac27a0ecSDave Kleikamp 	desc_count = 0;
1238ac27a0ecSDave Kleikamp 	bitmap_count = 0;
1239ac27a0ecSDave Kleikamp 	gdp = NULL;
12408df9675fSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
1241617ba13bSMingming Cao 		gdp = ext4_get_group_desc(sb, i, NULL);
1242ac27a0ecSDave Kleikamp 		if (!gdp)
1243ac27a0ecSDave Kleikamp 			continue;
1244560671a0SAneesh Kumar K.V 		desc_count += ext4_free_inodes_count(sb, gdp);
1245ac27a0ecSDave Kleikamp 		brelse(bitmap_bh);
1246e29d1cdeSEric Sandeen 		bitmap_bh = ext4_read_inode_bitmap(sb, i);
12479008a58eSDarrick J. Wong 		if (IS_ERR(bitmap_bh)) {
12489008a58eSDarrick J. Wong 			bitmap_bh = NULL;
1249ac27a0ecSDave Kleikamp 			continue;
12509008a58eSDarrick J. Wong 		}
1251ac27a0ecSDave Kleikamp 
1252f6fb99caSTheodore Ts'o 		x = ext4_count_free(bitmap_bh->b_data,
1253f6fb99caSTheodore Ts'o 				    EXT4_INODES_PER_GROUP(sb) / 8);
1254c549a95dSEric Sandeen 		printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
1255785b4b3aSPeng Tao 			(unsigned long) i, ext4_free_inodes_count(sb, gdp), x);
1256ac27a0ecSDave Kleikamp 		bitmap_count += x;
1257ac27a0ecSDave Kleikamp 	}
1258ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
12594776004fSTheodore Ts'o 	printk(KERN_DEBUG "ext4_count_free_inodes: "
12604776004fSTheodore Ts'o 	       "stored = %u, computed = %lu, %lu\n",
1261ac27a0ecSDave Kleikamp 	       le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
1262ac27a0ecSDave Kleikamp 	return desc_count;
1263ac27a0ecSDave Kleikamp #else
1264ac27a0ecSDave Kleikamp 	desc_count = 0;
12658df9675fSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
1266617ba13bSMingming Cao 		gdp = ext4_get_group_desc(sb, i, NULL);
1267ac27a0ecSDave Kleikamp 		if (!gdp)
1268ac27a0ecSDave Kleikamp 			continue;
1269560671a0SAneesh Kumar K.V 		desc_count += ext4_free_inodes_count(sb, gdp);
1270ac27a0ecSDave Kleikamp 		cond_resched();
1271ac27a0ecSDave Kleikamp 	}
1272ac27a0ecSDave Kleikamp 	return desc_count;
1273ac27a0ecSDave Kleikamp #endif
1274ac27a0ecSDave Kleikamp }
1275ac27a0ecSDave Kleikamp 
1276ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
1277617ba13bSMingming Cao unsigned long ext4_count_dirs(struct super_block * sb)
1278ac27a0ecSDave Kleikamp {
1279ac27a0ecSDave Kleikamp 	unsigned long count = 0;
12808df9675fSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
1281ac27a0ecSDave Kleikamp 
12828df9675fSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
1283617ba13bSMingming Cao 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1284ac27a0ecSDave Kleikamp 		if (!gdp)
1285ac27a0ecSDave Kleikamp 			continue;
1286560671a0SAneesh Kumar K.V 		count += ext4_used_dirs_count(sb, gdp);
1287ac27a0ecSDave Kleikamp 	}
1288ac27a0ecSDave Kleikamp 	return count;
1289ac27a0ecSDave Kleikamp }
1290bfff6873SLukas Czerner 
1291bfff6873SLukas Czerner /*
1292bfff6873SLukas Czerner  * Zeroes not yet zeroed inode table - just write zeroes through the whole
1293bfff6873SLukas Czerner  * inode table. Must be called without any spinlock held. The only place
1294bfff6873SLukas Czerner  * where it is called from on active part of filesystem is ext4lazyinit
1295bfff6873SLukas Czerner  * thread, so we do not need any special locks, however we have to prevent
1296bfff6873SLukas Czerner  * inode allocation from the current group, so we take alloc_sem lock, to
1297119c0d44STheodore Ts'o  * block ext4_new_inode() until we are finished.
1298bfff6873SLukas Czerner  */
1299e0cbee3eSH Hartley Sweeten int ext4_init_inode_table(struct super_block *sb, ext4_group_t group,
1300bfff6873SLukas Czerner 				 int barrier)
1301bfff6873SLukas Czerner {
1302bfff6873SLukas Czerner 	struct ext4_group_info *grp = ext4_get_group_info(sb, group);
1303bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1304bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
1305bfff6873SLukas Czerner 	struct buffer_head *group_desc_bh;
1306bfff6873SLukas Czerner 	handle_t *handle;
1307bfff6873SLukas Czerner 	ext4_fsblk_t blk;
1308bfff6873SLukas Czerner 	int num, ret = 0, used_blks = 0;
1309bfff6873SLukas Czerner 
1310bfff6873SLukas Czerner 	/* This should not happen, but just to be sure check this */
1311bfff6873SLukas Czerner 	if (sb->s_flags & MS_RDONLY) {
1312bfff6873SLukas Czerner 		ret = 1;
1313bfff6873SLukas Czerner 		goto out;
1314bfff6873SLukas Czerner 	}
1315bfff6873SLukas Czerner 
1316bfff6873SLukas Czerner 	gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
1317bfff6873SLukas Czerner 	if (!gdp)
1318bfff6873SLukas Czerner 		goto out;
1319bfff6873SLukas Czerner 
1320bfff6873SLukas Czerner 	/*
1321bfff6873SLukas Czerner 	 * We do not need to lock this, because we are the only one
1322bfff6873SLukas Czerner 	 * handling this flag.
1323bfff6873SLukas Czerner 	 */
1324bfff6873SLukas Czerner 	if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED))
1325bfff6873SLukas Czerner 		goto out;
1326bfff6873SLukas Czerner 
13279924a92aSTheodore Ts'o 	handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
1328bfff6873SLukas Czerner 	if (IS_ERR(handle)) {
1329bfff6873SLukas Czerner 		ret = PTR_ERR(handle);
1330bfff6873SLukas Czerner 		goto out;
1331bfff6873SLukas Czerner 	}
1332bfff6873SLukas Czerner 
1333bfff6873SLukas Czerner 	down_write(&grp->alloc_sem);
1334bfff6873SLukas Czerner 	/*
1335bfff6873SLukas Czerner 	 * If inode bitmap was already initialized there may be some
1336bfff6873SLukas Czerner 	 * used inodes so we need to skip blocks with used inodes in
1337bfff6873SLukas Czerner 	 * inode table.
1338bfff6873SLukas Czerner 	 */
1339bfff6873SLukas Czerner 	if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)))
1340bfff6873SLukas Czerner 		used_blks = DIV_ROUND_UP((EXT4_INODES_PER_GROUP(sb) -
1341bfff6873SLukas Czerner 			    ext4_itable_unused_count(sb, gdp)),
1342bfff6873SLukas Czerner 			    sbi->s_inodes_per_block);
1343bfff6873SLukas Czerner 
1344857ac889SLukas Czerner 	if ((used_blks < 0) || (used_blks > sbi->s_itb_per_group)) {
13451084f252STheodore Ts'o 		ext4_error(sb, "Something is wrong with group %u: "
13461084f252STheodore Ts'o 			   "used itable blocks: %d; "
13471084f252STheodore Ts'o 			   "itable unused count: %u",
1348857ac889SLukas Czerner 			   group, used_blks,
1349857ac889SLukas Czerner 			   ext4_itable_unused_count(sb, gdp));
1350857ac889SLukas Czerner 		ret = 1;
135133853a0dSYongqiang Yang 		goto err_out;
1352857ac889SLukas Czerner 	}
1353857ac889SLukas Czerner 
1354bfff6873SLukas Czerner 	blk = ext4_inode_table(sb, gdp) + used_blks;
1355bfff6873SLukas Czerner 	num = sbi->s_itb_per_group - used_blks;
1356bfff6873SLukas Czerner 
1357bfff6873SLukas Czerner 	BUFFER_TRACE(group_desc_bh, "get_write_access");
1358bfff6873SLukas Czerner 	ret = ext4_journal_get_write_access(handle,
1359bfff6873SLukas Czerner 					    group_desc_bh);
1360bfff6873SLukas Czerner 	if (ret)
1361bfff6873SLukas Czerner 		goto err_out;
1362bfff6873SLukas Czerner 
1363bfff6873SLukas Czerner 	/*
1364bfff6873SLukas Czerner 	 * Skip zeroout if the inode table is full. But we set the ZEROED
1365bfff6873SLukas Czerner 	 * flag anyway, because obviously, when it is full it does not need
1366bfff6873SLukas Czerner 	 * further zeroing.
1367bfff6873SLukas Czerner 	 */
1368bfff6873SLukas Czerner 	if (unlikely(num == 0))
1369bfff6873SLukas Czerner 		goto skip_zeroout;
1370bfff6873SLukas Czerner 
1371bfff6873SLukas Czerner 	ext4_debug("going to zero out inode table in group %d\n",
1372bfff6873SLukas Czerner 		   group);
1373a107e5a3STheodore Ts'o 	ret = sb_issue_zeroout(sb, blk, num, GFP_NOFS);
1374bfff6873SLukas Czerner 	if (ret < 0)
1375bfff6873SLukas Czerner 		goto err_out;
1376a107e5a3STheodore Ts'o 	if (barrier)
1377a107e5a3STheodore Ts'o 		blkdev_issue_flush(sb->s_bdev, GFP_NOFS, NULL);
1378bfff6873SLukas Czerner 
1379bfff6873SLukas Czerner skip_zeroout:
1380bfff6873SLukas Czerner 	ext4_lock_group(sb, group);
1381bfff6873SLukas Czerner 	gdp->bg_flags |= cpu_to_le16(EXT4_BG_INODE_ZEROED);
1382feb0ab32SDarrick J. Wong 	ext4_group_desc_csum_set(sb, group, gdp);
1383bfff6873SLukas Czerner 	ext4_unlock_group(sb, group);
1384bfff6873SLukas Czerner 
1385bfff6873SLukas Czerner 	BUFFER_TRACE(group_desc_bh,
1386bfff6873SLukas Czerner 		     "call ext4_handle_dirty_metadata");
1387bfff6873SLukas Czerner 	ret = ext4_handle_dirty_metadata(handle, NULL,
1388bfff6873SLukas Czerner 					 group_desc_bh);
1389bfff6873SLukas Czerner 
1390bfff6873SLukas Czerner err_out:
1391bfff6873SLukas Czerner 	up_write(&grp->alloc_sem);
1392bfff6873SLukas Czerner 	ext4_journal_stop(handle);
1393bfff6873SLukas Czerner out:
1394bfff6873SLukas Czerner 	return ret;
1395bfff6873SLukas Czerner }
1396