xref: /linux/fs/ext4/ialloc.c (revision b5f515735bea4ae71c248aea3e049073f8852889)
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>
245b825c3aSIngo Molnar #include <linux/cred.h>
255b825c3aSIngo Molnar 
26ac27a0ecSDave Kleikamp #include <asm/byteorder.h>
279bffad1eSTheodore Ts'o 
283dcf5451SChristoph Hellwig #include "ext4.h"
293dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
30ac27a0ecSDave Kleikamp #include "xattr.h"
31ac27a0ecSDave Kleikamp #include "acl.h"
32ac27a0ecSDave Kleikamp 
339bffad1eSTheodore Ts'o #include <trace/events/ext4.h>
349bffad1eSTheodore Ts'o 
35ac27a0ecSDave Kleikamp /*
36ac27a0ecSDave Kleikamp  * ialloc.c contains the inodes allocation and deallocation routines
37ac27a0ecSDave Kleikamp  */
38ac27a0ecSDave Kleikamp 
39ac27a0ecSDave Kleikamp /*
40ac27a0ecSDave Kleikamp  * The free inodes are managed by bitmaps.  A file system contains several
41ac27a0ecSDave Kleikamp  * blocks groups.  Each group contains 1 bitmap block for blocks, 1 bitmap
42ac27a0ecSDave Kleikamp  * block for inodes, N blocks for the inode table and data blocks.
43ac27a0ecSDave Kleikamp  *
44ac27a0ecSDave Kleikamp  * The file system contains group descriptors which are located after the
45ac27a0ecSDave Kleikamp  * super block.  Each descriptor contains the number of the bitmap block and
46ac27a0ecSDave Kleikamp  * the free blocks count in the block.
47ac27a0ecSDave Kleikamp  */
48ac27a0ecSDave Kleikamp 
49717d50e4SAndreas Dilger /*
50717d50e4SAndreas Dilger  * To avoid calling the atomic setbit hundreds or thousands of times, we only
51717d50e4SAndreas Dilger  * need to use it within a single byte (to ensure we get endianness right).
52717d50e4SAndreas Dilger  * We can use memset for the rest of the bitmap as there are no other users.
53717d50e4SAndreas Dilger  */
5461d08673STheodore Ts'o void ext4_mark_bitmap_end(int start_bit, int end_bit, char *bitmap)
55717d50e4SAndreas Dilger {
56717d50e4SAndreas Dilger 	int i;
57717d50e4SAndreas Dilger 
58717d50e4SAndreas Dilger 	if (start_bit >= end_bit)
59717d50e4SAndreas Dilger 		return;
60717d50e4SAndreas Dilger 
61717d50e4SAndreas Dilger 	ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit);
62717d50e4SAndreas Dilger 	for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
63717d50e4SAndreas Dilger 		ext4_set_bit(i, bitmap);
64717d50e4SAndreas Dilger 	if (i < end_bit)
65717d50e4SAndreas Dilger 		memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
66717d50e4SAndreas Dilger }
67717d50e4SAndreas Dilger 
68717d50e4SAndreas Dilger /* Initializes an uninitialized inode bitmap */
699008a58eSDarrick J. Wong static int ext4_init_inode_bitmap(struct super_block *sb,
701f109d5aSTheodore Ts'o 				       struct buffer_head *bh,
71fd2d4291SAvantika Mathur 				       ext4_group_t block_group,
72717d50e4SAndreas Dilger 				       struct ext4_group_desc *gdp)
73717d50e4SAndreas Dilger {
74bdfb6ff4SDarrick J. Wong 	struct ext4_group_info *grp;
75e43bb4e6SNamjae Jeon 	struct ext4_sb_info *sbi = EXT4_SB(sb);
76717d50e4SAndreas Dilger 	J_ASSERT_BH(bh, buffer_locked(bh));
77717d50e4SAndreas Dilger 
78717d50e4SAndreas Dilger 	/* If checksum is bad mark all blocks and inodes use to prevent
79717d50e4SAndreas Dilger 	 * allocation, essentially implementing a per-group read-only flag. */
80feb0ab32SDarrick J. Wong 	if (!ext4_group_desc_csum_verify(sb, block_group, gdp)) {
81bdfb6ff4SDarrick J. Wong 		grp = ext4_get_group_info(sb, block_group);
82e43bb4e6SNamjae Jeon 		if (!EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
83e43bb4e6SNamjae Jeon 			percpu_counter_sub(&sbi->s_freeclusters_counter,
84e43bb4e6SNamjae Jeon 					   grp->bb_free);
85bdfb6ff4SDarrick J. Wong 		set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT, &grp->bb_state);
86e43bb4e6SNamjae Jeon 		if (!EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) {
87e43bb4e6SNamjae Jeon 			int count;
88e43bb4e6SNamjae Jeon 			count = ext4_free_inodes_count(sb, gdp);
89e43bb4e6SNamjae Jeon 			percpu_counter_sub(&sbi->s_freeinodes_counter,
90e43bb4e6SNamjae Jeon 					   count);
91e43bb4e6SNamjae Jeon 		}
92bdfb6ff4SDarrick J. Wong 		set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &grp->bb_state);
939008a58eSDarrick J. Wong 		return -EFSBADCRC;
94717d50e4SAndreas Dilger 	}
95717d50e4SAndreas Dilger 
96717d50e4SAndreas Dilger 	memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8);
9761d08673STheodore Ts'o 	ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), sb->s_blocksize * 8,
98717d50e4SAndreas Dilger 			bh->b_data);
9941a246d1SDarrick J. Wong 	ext4_inode_bitmap_csum_set(sb, block_group, gdp, bh,
10041a246d1SDarrick J. Wong 				   EXT4_INODES_PER_GROUP(sb) / 8);
101feb0ab32SDarrick J. Wong 	ext4_group_desc_csum_set(sb, block_group, gdp);
102717d50e4SAndreas Dilger 
1039008a58eSDarrick J. Wong 	return 0;
104717d50e4SAndreas Dilger }
105ac27a0ecSDave Kleikamp 
106813e5727STheodore Ts'o void ext4_end_bitmap_read(struct buffer_head *bh, int uptodate)
107813e5727STheodore Ts'o {
108813e5727STheodore Ts'o 	if (uptodate) {
109813e5727STheodore Ts'o 		set_buffer_uptodate(bh);
110813e5727STheodore Ts'o 		set_bitmap_uptodate(bh);
111813e5727STheodore Ts'o 	}
112813e5727STheodore Ts'o 	unlock_buffer(bh);
113813e5727STheodore Ts'o 	put_bh(bh);
114813e5727STheodore Ts'o }
115813e5727STheodore Ts'o 
1169008a58eSDarrick J. Wong static int ext4_validate_inode_bitmap(struct super_block *sb,
1179008a58eSDarrick J. Wong 				      struct ext4_group_desc *desc,
1189008a58eSDarrick J. Wong 				      ext4_group_t block_group,
1199008a58eSDarrick J. Wong 				      struct buffer_head *bh)
1209008a58eSDarrick J. Wong {
1219008a58eSDarrick J. Wong 	ext4_fsblk_t	blk;
1229008a58eSDarrick J. Wong 	struct ext4_group_info *grp = ext4_get_group_info(sb, block_group);
1239008a58eSDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1249008a58eSDarrick J. Wong 
1259008a58eSDarrick J. Wong 	if (buffer_verified(bh))
1269008a58eSDarrick J. Wong 		return 0;
1279008a58eSDarrick J. Wong 	if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp))
1289008a58eSDarrick J. Wong 		return -EFSCORRUPTED;
1299008a58eSDarrick J. Wong 
1309008a58eSDarrick J. Wong 	ext4_lock_group(sb, block_group);
1319008a58eSDarrick J. Wong 	blk = ext4_inode_bitmap(sb, desc);
1329008a58eSDarrick J. Wong 	if (!ext4_inode_bitmap_csum_verify(sb, block_group, desc, bh,
1339008a58eSDarrick J. Wong 					   EXT4_INODES_PER_GROUP(sb) / 8)) {
1349008a58eSDarrick J. Wong 		ext4_unlock_group(sb, block_group);
1359008a58eSDarrick J. Wong 		ext4_error(sb, "Corrupt inode bitmap - block_group = %u, "
1369008a58eSDarrick J. Wong 			   "inode_bitmap = %llu", block_group, blk);
1379008a58eSDarrick J. Wong 		grp = ext4_get_group_info(sb, block_group);
1389008a58eSDarrick J. Wong 		if (!EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) {
1399008a58eSDarrick J. Wong 			int count;
1409008a58eSDarrick J. Wong 			count = ext4_free_inodes_count(sb, desc);
1419008a58eSDarrick J. Wong 			percpu_counter_sub(&sbi->s_freeinodes_counter,
1429008a58eSDarrick J. Wong 					   count);
1439008a58eSDarrick J. Wong 		}
1449008a58eSDarrick J. Wong 		set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &grp->bb_state);
1459008a58eSDarrick J. Wong 		return -EFSBADCRC;
1469008a58eSDarrick J. Wong 	}
1479008a58eSDarrick J. Wong 	set_buffer_verified(bh);
1489008a58eSDarrick J. Wong 	ext4_unlock_group(sb, block_group);
1499008a58eSDarrick J. Wong 	return 0;
1509008a58eSDarrick J. Wong }
1519008a58eSDarrick J. Wong 
152ac27a0ecSDave Kleikamp /*
153ac27a0ecSDave Kleikamp  * Read the inode allocation bitmap for a given block_group, reading
154ac27a0ecSDave Kleikamp  * into the specified slot in the superblock's bitmap cache.
155ac27a0ecSDave Kleikamp  *
156ac27a0ecSDave Kleikamp  * Return buffer_head of bitmap on success or NULL.
157ac27a0ecSDave Kleikamp  */
158ac27a0ecSDave Kleikamp static struct buffer_head *
159e29d1cdeSEric Sandeen ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group)
160ac27a0ecSDave Kleikamp {
161617ba13bSMingming Cao 	struct ext4_group_desc *desc;
162ac27a0ecSDave Kleikamp 	struct buffer_head *bh = NULL;
163e29d1cdeSEric Sandeen 	ext4_fsblk_t bitmap_blk;
1649008a58eSDarrick J. Wong 	int err;
165ac27a0ecSDave Kleikamp 
166617ba13bSMingming Cao 	desc = ext4_get_group_desc(sb, block_group, NULL);
167ac27a0ecSDave Kleikamp 	if (!desc)
1689008a58eSDarrick J. Wong 		return ERR_PTR(-EFSCORRUPTED);
169bfff6873SLukas Czerner 
170e29d1cdeSEric Sandeen 	bitmap_blk = ext4_inode_bitmap(sb, desc);
171e29d1cdeSEric Sandeen 	bh = sb_getblk(sb, bitmap_blk);
172e29d1cdeSEric Sandeen 	if (unlikely(!bh)) {
17312062dddSEric Sandeen 		ext4_error(sb, "Cannot read inode bitmap - "
174a9df9a49STheodore Ts'o 			    "block_group = %u, inode_bitmap = %llu",
175e29d1cdeSEric Sandeen 			    block_group, bitmap_blk);
1769008a58eSDarrick J. Wong 		return ERR_PTR(-EIO);
177e29d1cdeSEric Sandeen 	}
1782ccb5fb9SAneesh Kumar K.V 	if (bitmap_uptodate(bh))
17941a246d1SDarrick J. Wong 		goto verify;
180e29d1cdeSEric Sandeen 
181c806e68fSFrederic Bohe 	lock_buffer(bh);
1822ccb5fb9SAneesh Kumar K.V 	if (bitmap_uptodate(bh)) {
1832ccb5fb9SAneesh Kumar K.V 		unlock_buffer(bh);
18441a246d1SDarrick J. Wong 		goto verify;
1852ccb5fb9SAneesh Kumar K.V 	}
186bfff6873SLukas Czerner 
187955ce5f5SAneesh Kumar K.V 	ext4_lock_group(sb, block_group);
188e29d1cdeSEric Sandeen 	if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
1899008a58eSDarrick J. Wong 		err = ext4_init_inode_bitmap(sb, bh, block_group, desc);
1902ccb5fb9SAneesh Kumar K.V 		set_bitmap_uptodate(bh);
191e29d1cdeSEric Sandeen 		set_buffer_uptodate(bh);
19241a246d1SDarrick J. Wong 		set_buffer_verified(bh);
193955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, block_group);
1943300bedaSAneesh Kumar K.V 		unlock_buffer(bh);
19505145bd7SJan Kara 		if (err) {
19605145bd7SJan Kara 			ext4_error(sb, "Failed to init inode bitmap for group "
19705145bd7SJan Kara 				   "%u: %d", block_group, err);
1989008a58eSDarrick J. Wong 			goto out;
19905145bd7SJan Kara 		}
200e29d1cdeSEric Sandeen 		return bh;
201e29d1cdeSEric Sandeen 	}
202955ce5f5SAneesh Kumar K.V 	ext4_unlock_group(sb, block_group);
203bfff6873SLukas Czerner 
2042ccb5fb9SAneesh Kumar K.V 	if (buffer_uptodate(bh)) {
2052ccb5fb9SAneesh Kumar K.V 		/*
2062ccb5fb9SAneesh Kumar K.V 		 * if not uninit if bh is uptodate,
2072ccb5fb9SAneesh Kumar K.V 		 * bitmap is also uptodate
2082ccb5fb9SAneesh Kumar K.V 		 */
2092ccb5fb9SAneesh Kumar K.V 		set_bitmap_uptodate(bh);
2102ccb5fb9SAneesh Kumar K.V 		unlock_buffer(bh);
21141a246d1SDarrick J. Wong 		goto verify;
2122ccb5fb9SAneesh Kumar K.V 	}
2132ccb5fb9SAneesh Kumar K.V 	/*
214813e5727STheodore Ts'o 	 * submit the buffer_head for reading
2152ccb5fb9SAneesh Kumar K.V 	 */
2160562e0baSJiaying Zhang 	trace_ext4_load_inode_bitmap(sb, block_group);
217813e5727STheodore Ts'o 	bh->b_end_io = ext4_end_bitmap_read;
218813e5727STheodore Ts'o 	get_bh(bh);
2192a222ca9SMike Christie 	submit_bh(REQ_OP_READ, REQ_META | REQ_PRIO, bh);
220813e5727STheodore Ts'o 	wait_on_buffer(bh);
221813e5727STheodore Ts'o 	if (!buffer_uptodate(bh)) {
222e29d1cdeSEric Sandeen 		put_bh(bh);
22312062dddSEric Sandeen 		ext4_error(sb, "Cannot read inode bitmap - "
224a9df9a49STheodore Ts'o 			   "block_group = %u, inode_bitmap = %llu",
225e29d1cdeSEric Sandeen 			   block_group, bitmap_blk);
2269008a58eSDarrick J. Wong 		return ERR_PTR(-EIO);
227e29d1cdeSEric Sandeen 	}
22841a246d1SDarrick J. Wong 
22941a246d1SDarrick J. Wong verify:
2309008a58eSDarrick J. Wong 	err = ext4_validate_inode_bitmap(sb, desc, block_group, bh);
2319008a58eSDarrick J. Wong 	if (err)
2329008a58eSDarrick J. Wong 		goto out;
233ac27a0ecSDave Kleikamp 	return bh;
2349008a58eSDarrick J. Wong out:
2359008a58eSDarrick J. Wong 	put_bh(bh);
2369008a58eSDarrick J. Wong 	return ERR_PTR(err);
237ac27a0ecSDave Kleikamp }
238ac27a0ecSDave Kleikamp 
239ac27a0ecSDave Kleikamp /*
240ac27a0ecSDave Kleikamp  * NOTE! When we get the inode, we're the only people
241ac27a0ecSDave Kleikamp  * that have access to it, and as such there are no
242ac27a0ecSDave Kleikamp  * race conditions we have to worry about. The inode
243ac27a0ecSDave Kleikamp  * is not on the hash-lists, and it cannot be reached
244ac27a0ecSDave Kleikamp  * through the filesystem because the directory entry
245ac27a0ecSDave Kleikamp  * has been deleted earlier.
246ac27a0ecSDave Kleikamp  *
247ac27a0ecSDave Kleikamp  * HOWEVER: we must make sure that we get no aliases,
248ac27a0ecSDave Kleikamp  * which means that we have to call "clear_inode()"
249ac27a0ecSDave Kleikamp  * _before_ we mark the inode not in use in the inode
250ac27a0ecSDave Kleikamp  * bitmaps. Otherwise a newly created file might use
251ac27a0ecSDave Kleikamp  * the same inode number (not actually the same pointer
252ac27a0ecSDave Kleikamp  * though), and then we'd have two inodes sharing the
253ac27a0ecSDave Kleikamp  * same inode number and space on the harddisk.
254ac27a0ecSDave Kleikamp  */
255617ba13bSMingming Cao void ext4_free_inode(handle_t *handle, struct inode *inode)
256ac27a0ecSDave Kleikamp {
257ac27a0ecSDave Kleikamp 	struct super_block *sb = inode->i_sb;
258ac27a0ecSDave Kleikamp 	int is_directory;
259ac27a0ecSDave Kleikamp 	unsigned long ino;
260ac27a0ecSDave Kleikamp 	struct buffer_head *bitmap_bh = NULL;
261ac27a0ecSDave Kleikamp 	struct buffer_head *bh2;
262fd2d4291SAvantika Mathur 	ext4_group_t block_group;
263ac27a0ecSDave Kleikamp 	unsigned long bit;
264617ba13bSMingming Cao 	struct ext4_group_desc *gdp;
265617ba13bSMingming Cao 	struct ext4_super_block *es;
266617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
2677ce9d5d1SEric Sandeen 	int fatal = 0, err, count, cleared;
26887a39389SDarrick J. Wong 	struct ext4_group_info *grp;
269ac27a0ecSDave Kleikamp 
27092b97816STheodore Ts'o 	if (!sb) {
27192b97816STheodore Ts'o 		printk(KERN_ERR "EXT4-fs: %s:%d: inode on "
27292b97816STheodore Ts'o 		       "nonexistent device\n", __func__, __LINE__);
27392b97816STheodore Ts'o 		return;
27492b97816STheodore Ts'o 	}
275ac27a0ecSDave Kleikamp 	if (atomic_read(&inode->i_count) > 1) {
27692b97816STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: count=%d",
27792b97816STheodore Ts'o 			 __func__, __LINE__, inode->i_ino,
278ac27a0ecSDave Kleikamp 			 atomic_read(&inode->i_count));
279ac27a0ecSDave Kleikamp 		return;
280ac27a0ecSDave Kleikamp 	}
281ac27a0ecSDave Kleikamp 	if (inode->i_nlink) {
28292b97816STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: nlink=%d\n",
28392b97816STheodore Ts'o 			 __func__, __LINE__, inode->i_ino, inode->i_nlink);
284ac27a0ecSDave Kleikamp 		return;
285ac27a0ecSDave Kleikamp 	}
286617ba13bSMingming Cao 	sbi = EXT4_SB(sb);
287ac27a0ecSDave Kleikamp 
288ac27a0ecSDave Kleikamp 	ino = inode->i_ino;
289617ba13bSMingming Cao 	ext4_debug("freeing inode %lu\n", ino);
2909bffad1eSTheodore Ts'o 	trace_ext4_free_inode(inode);
291ac27a0ecSDave Kleikamp 
292ac27a0ecSDave Kleikamp 	/*
293ac27a0ecSDave Kleikamp 	 * Note: we must free any quota before locking the superblock,
294ac27a0ecSDave Kleikamp 	 * as writing the quota to disk may need the lock as well.
295ac27a0ecSDave Kleikamp 	 */
296871a2931SChristoph Hellwig 	dquot_initialize(inode);
29763936ddaSChristoph Hellwig 	dquot_free_inode(inode);
2989f754758SChristoph Hellwig 	dquot_drop(inode);
299ac27a0ecSDave Kleikamp 
300ac27a0ecSDave Kleikamp 	is_directory = S_ISDIR(inode->i_mode);
301ac27a0ecSDave Kleikamp 
302ac27a0ecSDave Kleikamp 	/* Do this BEFORE marking the inode not in use or returning an error */
3030930fcc1SAl Viro 	ext4_clear_inode(inode);
304ac27a0ecSDave Kleikamp 
305617ba13bSMingming Cao 	es = EXT4_SB(sb)->s_es;
306617ba13bSMingming Cao 	if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
30712062dddSEric Sandeen 		ext4_error(sb, "reserved or nonexistent inode %lu", ino);
308ac27a0ecSDave Kleikamp 		goto error_return;
309ac27a0ecSDave Kleikamp 	}
310617ba13bSMingming Cao 	block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
311617ba13bSMingming Cao 	bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
312e29d1cdeSEric Sandeen 	bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
31387a39389SDarrick J. Wong 	/* Don't bother if the inode bitmap is corrupt. */
31487a39389SDarrick J. Wong 	grp = ext4_get_group_info(sb, block_group);
3159008a58eSDarrick J. Wong 	if (IS_ERR(bitmap_bh)) {
3169008a58eSDarrick J. Wong 		fatal = PTR_ERR(bitmap_bh);
3179008a58eSDarrick J. Wong 		bitmap_bh = NULL;
318ac27a0ecSDave Kleikamp 		goto error_return;
3199008a58eSDarrick J. Wong 	}
3209008a58eSDarrick J. Wong 	if (unlikely(EXT4_MB_GRP_IBITMAP_CORRUPT(grp))) {
3219008a58eSDarrick J. Wong 		fatal = -EFSCORRUPTED;
3229008a58eSDarrick J. Wong 		goto error_return;
3239008a58eSDarrick J. Wong 	}
324ac27a0ecSDave Kleikamp 
325ac27a0ecSDave Kleikamp 	BUFFER_TRACE(bitmap_bh, "get_write_access");
326617ba13bSMingming Cao 	fatal = ext4_journal_get_write_access(handle, bitmap_bh);
327ac27a0ecSDave Kleikamp 	if (fatal)
328ac27a0ecSDave Kleikamp 		goto error_return;
329ac27a0ecSDave Kleikamp 
330d17413c0SDmitry Monakhov 	fatal = -ESRCH;
331617ba13bSMingming Cao 	gdp = ext4_get_group_desc(sb, block_group, &bh2);
332d17413c0SDmitry Monakhov 	if (gdp) {
333ac27a0ecSDave Kleikamp 		BUFFER_TRACE(bh2, "get_write_access");
334617ba13bSMingming Cao 		fatal = ext4_journal_get_write_access(handle, bh2);
335d17413c0SDmitry Monakhov 	}
336955ce5f5SAneesh Kumar K.V 	ext4_lock_group(sb, block_group);
337597d508cSAkinobu Mita 	cleared = ext4_test_and_clear_bit(bit, bitmap_bh->b_data);
338d17413c0SDmitry Monakhov 	if (fatal || !cleared) {
339d17413c0SDmitry Monakhov 		ext4_unlock_group(sb, block_group);
340d17413c0SDmitry Monakhov 		goto out;
341d17413c0SDmitry Monakhov 	}
342d17413c0SDmitry Monakhov 
343560671a0SAneesh Kumar K.V 	count = ext4_free_inodes_count(sb, gdp) + 1;
344560671a0SAneesh Kumar K.V 	ext4_free_inodes_set(sb, gdp, count);
345560671a0SAneesh Kumar K.V 	if (is_directory) {
346560671a0SAneesh Kumar K.V 		count = ext4_used_dirs_count(sb, gdp) - 1;
347560671a0SAneesh Kumar K.V 		ext4_used_dirs_set(sb, gdp, count);
348d17413c0SDmitry Monakhov 		percpu_counter_dec(&sbi->s_dirs_counter);
349d17413c0SDmitry Monakhov 	}
35041a246d1SDarrick J. Wong 	ext4_inode_bitmap_csum_set(sb, block_group, gdp, bitmap_bh,
35141a246d1SDarrick J. Wong 				   EXT4_INODES_PER_GROUP(sb) / 8);
352feb0ab32SDarrick J. Wong 	ext4_group_desc_csum_set(sb, block_group, gdp);
353d17413c0SDmitry Monakhov 	ext4_unlock_group(sb, block_group);
3547d39db14STheodore Ts'o 
355d17413c0SDmitry Monakhov 	percpu_counter_inc(&sbi->s_freeinodes_counter);
356d17413c0SDmitry Monakhov 	if (sbi->s_log_groups_per_flex) {
357d17413c0SDmitry Monakhov 		ext4_group_t f = ext4_flex_group(sbi, block_group);
358d17413c0SDmitry Monakhov 
359d17413c0SDmitry Monakhov 		atomic_inc(&sbi->s_flex_groups[f].free_inodes);
360d17413c0SDmitry Monakhov 		if (is_directory)
361c4caae25SEric Sandeen 			atomic_dec(&sbi->s_flex_groups[f].used_dirs);
3627d39db14STheodore Ts'o 	}
3630390131bSFrank Mayhar 	BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata");
364d17413c0SDmitry Monakhov 	fatal = ext4_handle_dirty_metadata(handle, NULL, bh2);
365d17413c0SDmitry Monakhov out:
366d17413c0SDmitry Monakhov 	if (cleared) {
3670390131bSFrank Mayhar 		BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata");
3680390131bSFrank Mayhar 		err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
369ac27a0ecSDave Kleikamp 		if (!fatal)
370ac27a0ecSDave Kleikamp 			fatal = err;
37187a39389SDarrick J. Wong 	} else {
372d17413c0SDmitry Monakhov 		ext4_error(sb, "bit already cleared for inode %lu", ino);
373bf40c926SNamjae Jeon 		if (gdp && !EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) {
374e43bb4e6SNamjae Jeon 			int count;
375e43bb4e6SNamjae Jeon 			count = ext4_free_inodes_count(sb, gdp);
376e43bb4e6SNamjae Jeon 			percpu_counter_sub(&sbi->s_freeinodes_counter,
377e43bb4e6SNamjae Jeon 					   count);
378e43bb4e6SNamjae Jeon 		}
37987a39389SDarrick J. Wong 		set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &grp->bb_state);
38087a39389SDarrick J. Wong 	}
381d17413c0SDmitry Monakhov 
382ac27a0ecSDave Kleikamp error_return:
383ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
384617ba13bSMingming Cao 	ext4_std_error(sb, fatal);
385ac27a0ecSDave Kleikamp }
386ac27a0ecSDave Kleikamp 
387a4912123STheodore Ts'o struct orlov_stats {
38890ba983fSTheodore Ts'o 	__u64 free_clusters;
389a4912123STheodore Ts'o 	__u32 free_inodes;
390a4912123STheodore Ts'o 	__u32 used_dirs;
391a4912123STheodore Ts'o };
392a4912123STheodore Ts'o 
393a4912123STheodore Ts'o /*
394a4912123STheodore Ts'o  * Helper function for Orlov's allocator; returns critical information
395a4912123STheodore Ts'o  * for a particular block group or flex_bg.  If flex_size is 1, then g
396a4912123STheodore Ts'o  * is a block group number; otherwise it is flex_bg number.
397a4912123STheodore Ts'o  */
3981f109d5aSTheodore Ts'o static void get_orlov_stats(struct super_block *sb, ext4_group_t g,
399a4912123STheodore Ts'o 			    int flex_size, struct orlov_stats *stats)
400a4912123STheodore Ts'o {
401a4912123STheodore Ts'o 	struct ext4_group_desc *desc;
4027d39db14STheodore Ts'o 	struct flex_groups *flex_group = EXT4_SB(sb)->s_flex_groups;
403a4912123STheodore Ts'o 
4047d39db14STheodore Ts'o 	if (flex_size > 1) {
4057d39db14STheodore Ts'o 		stats->free_inodes = atomic_read(&flex_group[g].free_inodes);
40690ba983fSTheodore Ts'o 		stats->free_clusters = atomic64_read(&flex_group[g].free_clusters);
4077d39db14STheodore Ts'o 		stats->used_dirs = atomic_read(&flex_group[g].used_dirs);
4087d39db14STheodore Ts'o 		return;
4097d39db14STheodore Ts'o 	}
4107d39db14STheodore Ts'o 
4117d39db14STheodore Ts'o 	desc = ext4_get_group_desc(sb, g, NULL);
4127d39db14STheodore Ts'o 	if (desc) {
4137d39db14STheodore Ts'o 		stats->free_inodes = ext4_free_inodes_count(sb, desc);
414021b65bbSTheodore Ts'o 		stats->free_clusters = ext4_free_group_clusters(sb, desc);
4157d39db14STheodore Ts'o 		stats->used_dirs = ext4_used_dirs_count(sb, desc);
4167d39db14STheodore Ts'o 	} else {
417a4912123STheodore Ts'o 		stats->free_inodes = 0;
41824aaa8efSTheodore Ts'o 		stats->free_clusters = 0;
419a4912123STheodore Ts'o 		stats->used_dirs = 0;
420a4912123STheodore Ts'o 	}
421a4912123STheodore Ts'o }
422a4912123STheodore Ts'o 
423ac27a0ecSDave Kleikamp /*
424ac27a0ecSDave Kleikamp  * Orlov's allocator for directories.
425ac27a0ecSDave Kleikamp  *
426ac27a0ecSDave Kleikamp  * We always try to spread first-level directories.
427ac27a0ecSDave Kleikamp  *
428ac27a0ecSDave Kleikamp  * If there are blockgroups with both free inodes and free blocks counts
429ac27a0ecSDave Kleikamp  * not worse than average we return one with smallest directory count.
430ac27a0ecSDave Kleikamp  * Otherwise we simply return a random group.
431ac27a0ecSDave Kleikamp  *
432ac27a0ecSDave Kleikamp  * For the rest rules look so:
433ac27a0ecSDave Kleikamp  *
434ac27a0ecSDave Kleikamp  * It's OK to put directory into a group unless
435ac27a0ecSDave Kleikamp  * it has too many directories already (max_dirs) or
436ac27a0ecSDave Kleikamp  * it has too few free inodes left (min_inodes) or
437ac27a0ecSDave Kleikamp  * it has too few free blocks left (min_blocks) or
4381cc8dcf5SBenoit Boissinot  * Parent's group is preferred, if it doesn't satisfy these
439ac27a0ecSDave Kleikamp  * conditions we search cyclically through the rest. If none
440ac27a0ecSDave Kleikamp  * of the groups look good we just look for a group with more
441ac27a0ecSDave Kleikamp  * free inodes than average (starting at parent's group).
442ac27a0ecSDave Kleikamp  */
443ac27a0ecSDave Kleikamp 
4442aa9fc4cSAvantika Mathur static int find_group_orlov(struct super_block *sb, struct inode *parent,
445dcca3fecSAl Viro 			    ext4_group_t *group, umode_t mode,
446f157a4aaSTheodore Ts'o 			    const struct qstr *qstr)
447ac27a0ecSDave Kleikamp {
448fd2d4291SAvantika Mathur 	ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
449617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4508df9675fSTheodore Ts'o 	ext4_group_t real_ngroups = ext4_get_groups_count(sb);
451617ba13bSMingming Cao 	int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
45214c83c9fSTheodore Ts'o 	unsigned int freei, avefreei, grp_free;
45324aaa8efSTheodore Ts'o 	ext4_fsblk_t freeb, avefreec;
454ac27a0ecSDave Kleikamp 	unsigned int ndirs;
455a4912123STheodore Ts'o 	int max_dirs, min_inodes;
45624aaa8efSTheodore Ts'o 	ext4_grpblk_t min_clusters;
4578df9675fSTheodore Ts'o 	ext4_group_t i, grp, g, ngroups;
458617ba13bSMingming Cao 	struct ext4_group_desc *desc;
459a4912123STheodore Ts'o 	struct orlov_stats stats;
460a4912123STheodore Ts'o 	int flex_size = ext4_flex_bg_size(sbi);
461f157a4aaSTheodore Ts'o 	struct dx_hash_info hinfo;
462a4912123STheodore Ts'o 
4638df9675fSTheodore Ts'o 	ngroups = real_ngroups;
464a4912123STheodore Ts'o 	if (flex_size > 1) {
4658df9675fSTheodore Ts'o 		ngroups = (real_ngroups + flex_size - 1) >>
466a4912123STheodore Ts'o 			sbi->s_log_groups_per_flex;
467a4912123STheodore Ts'o 		parent_group >>= sbi->s_log_groups_per_flex;
468a4912123STheodore Ts'o 	}
469ac27a0ecSDave Kleikamp 
470ac27a0ecSDave Kleikamp 	freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
471ac27a0ecSDave Kleikamp 	avefreei = freei / ngroups;
47257042651STheodore Ts'o 	freeb = EXT4_C2B(sbi,
47357042651STheodore Ts'o 		percpu_counter_read_positive(&sbi->s_freeclusters_counter));
47424aaa8efSTheodore Ts'o 	avefreec = freeb;
47524aaa8efSTheodore Ts'o 	do_div(avefreec, ngroups);
476ac27a0ecSDave Kleikamp 	ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
477ac27a0ecSDave Kleikamp 
478a4912123STheodore Ts'o 	if (S_ISDIR(mode) &&
4792b0143b5SDavid Howells 	    ((parent == d_inode(sb->s_root)) ||
48012e9b892SDmitry Monakhov 	     (ext4_test_inode_flag(parent, EXT4_INODE_TOPDIR)))) {
481ac27a0ecSDave Kleikamp 		int best_ndir = inodes_per_group;
4822aa9fc4cSAvantika Mathur 		int ret = -1;
483ac27a0ecSDave Kleikamp 
484f157a4aaSTheodore Ts'o 		if (qstr) {
485f157a4aaSTheodore Ts'o 			hinfo.hash_version = DX_HASH_HALF_MD4;
486f157a4aaSTheodore Ts'o 			hinfo.seed = sbi->s_hash_seed;
487f157a4aaSTheodore Ts'o 			ext4fs_dirhash(qstr->name, qstr->len, &hinfo);
488f157a4aaSTheodore Ts'o 			grp = hinfo.hash;
489f157a4aaSTheodore Ts'o 		} else
490dd1f723bSTheodore Ts'o 			grp = prandom_u32();
4912aa9fc4cSAvantika Mathur 		parent_group = (unsigned)grp % ngroups;
492ac27a0ecSDave Kleikamp 		for (i = 0; i < ngroups; i++) {
493a4912123STheodore Ts'o 			g = (parent_group + i) % ngroups;
494a4912123STheodore Ts'o 			get_orlov_stats(sb, g, flex_size, &stats);
495a4912123STheodore Ts'o 			if (!stats.free_inodes)
496ac27a0ecSDave Kleikamp 				continue;
497a4912123STheodore Ts'o 			if (stats.used_dirs >= best_ndir)
498ac27a0ecSDave Kleikamp 				continue;
499a4912123STheodore Ts'o 			if (stats.free_inodes < avefreei)
500ac27a0ecSDave Kleikamp 				continue;
50124aaa8efSTheodore Ts'o 			if (stats.free_clusters < avefreec)
502ac27a0ecSDave Kleikamp 				continue;
503a4912123STheodore Ts'o 			grp = g;
5042aa9fc4cSAvantika Mathur 			ret = 0;
505a4912123STheodore Ts'o 			best_ndir = stats.used_dirs;
506ac27a0ecSDave Kleikamp 		}
507a4912123STheodore Ts'o 		if (ret)
508a4912123STheodore Ts'o 			goto fallback;
509a4912123STheodore Ts'o 	found_flex_bg:
510a4912123STheodore Ts'o 		if (flex_size == 1) {
511a4912123STheodore Ts'o 			*group = grp;
512a4912123STheodore Ts'o 			return 0;
513a4912123STheodore Ts'o 		}
514a4912123STheodore Ts'o 
515a4912123STheodore Ts'o 		/*
516a4912123STheodore Ts'o 		 * We pack inodes at the beginning of the flexgroup's
517a4912123STheodore Ts'o 		 * inode tables.  Block allocation decisions will do
518a4912123STheodore Ts'o 		 * something similar, although regular files will
519a4912123STheodore Ts'o 		 * start at 2nd block group of the flexgroup.  See
520a4912123STheodore Ts'o 		 * ext4_ext_find_goal() and ext4_find_near().
521a4912123STheodore Ts'o 		 */
522a4912123STheodore Ts'o 		grp *= flex_size;
523a4912123STheodore Ts'o 		for (i = 0; i < flex_size; i++) {
5248df9675fSTheodore Ts'o 			if (grp+i >= real_ngroups)
525a4912123STheodore Ts'o 				break;
526a4912123STheodore Ts'o 			desc = ext4_get_group_desc(sb, grp+i, NULL);
527a4912123STheodore Ts'o 			if (desc && ext4_free_inodes_count(sb, desc)) {
528a4912123STheodore Ts'o 				*group = grp+i;
529a4912123STheodore Ts'o 				return 0;
530a4912123STheodore Ts'o 			}
531a4912123STheodore Ts'o 		}
532ac27a0ecSDave Kleikamp 		goto fallback;
533ac27a0ecSDave Kleikamp 	}
534ac27a0ecSDave Kleikamp 
535ac27a0ecSDave Kleikamp 	max_dirs = ndirs / ngroups + inodes_per_group / 16;
536a4912123STheodore Ts'o 	min_inodes = avefreei - inodes_per_group*flex_size / 4;
537a4912123STheodore Ts'o 	if (min_inodes < 1)
538a4912123STheodore Ts'o 		min_inodes = 1;
53924aaa8efSTheodore Ts'o 	min_clusters = avefreec - EXT4_CLUSTERS_PER_GROUP(sb)*flex_size / 4;
540ac27a0ecSDave Kleikamp 
541a4912123STheodore Ts'o 	/*
542a4912123STheodore Ts'o 	 * Start looking in the flex group where we last allocated an
543a4912123STheodore Ts'o 	 * inode for this parent directory
544a4912123STheodore Ts'o 	 */
545a4912123STheodore Ts'o 	if (EXT4_I(parent)->i_last_alloc_group != ~0) {
546a4912123STheodore Ts'o 		parent_group = EXT4_I(parent)->i_last_alloc_group;
547a4912123STheodore Ts'o 		if (flex_size > 1)
548a4912123STheodore Ts'o 			parent_group >>= sbi->s_log_groups_per_flex;
549a4912123STheodore Ts'o 	}
550ac27a0ecSDave Kleikamp 
551ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
552a4912123STheodore Ts'o 		grp = (parent_group + i) % ngroups;
553a4912123STheodore Ts'o 		get_orlov_stats(sb, grp, flex_size, &stats);
554a4912123STheodore Ts'o 		if (stats.used_dirs >= max_dirs)
555ac27a0ecSDave Kleikamp 			continue;
556a4912123STheodore Ts'o 		if (stats.free_inodes < min_inodes)
557ac27a0ecSDave Kleikamp 			continue;
55824aaa8efSTheodore Ts'o 		if (stats.free_clusters < min_clusters)
559ac27a0ecSDave Kleikamp 			continue;
560a4912123STheodore Ts'o 		goto found_flex_bg;
561ac27a0ecSDave Kleikamp 	}
562ac27a0ecSDave Kleikamp 
563ac27a0ecSDave Kleikamp fallback:
5648df9675fSTheodore Ts'o 	ngroups = real_ngroups;
565a4912123STheodore Ts'o 	avefreei = freei / ngroups;
566b5451f7bSTheodore Ts'o fallback_retry:
567a4912123STheodore Ts'o 	parent_group = EXT4_I(parent)->i_block_group;
568ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
569a4912123STheodore Ts'o 		grp = (parent_group + i) % ngroups;
570a4912123STheodore Ts'o 		desc = ext4_get_group_desc(sb, grp, NULL);
571bb3d132aSDan Carpenter 		if (desc) {
57214c83c9fSTheodore Ts'o 			grp_free = ext4_free_inodes_count(sb, desc);
573bb3d132aSDan Carpenter 			if (grp_free && grp_free >= avefreei) {
574a4912123STheodore Ts'o 				*group = grp;
5752aa9fc4cSAvantika Mathur 				return 0;
576ac27a0ecSDave Kleikamp 			}
577a4912123STheodore Ts'o 		}
578bb3d132aSDan Carpenter 	}
579ac27a0ecSDave Kleikamp 
580ac27a0ecSDave Kleikamp 	if (avefreei) {
581ac27a0ecSDave Kleikamp 		/*
582ac27a0ecSDave Kleikamp 		 * The free-inodes counter is approximate, and for really small
583ac27a0ecSDave Kleikamp 		 * filesystems the above test can fail to find any blockgroups
584ac27a0ecSDave Kleikamp 		 */
585ac27a0ecSDave Kleikamp 		avefreei = 0;
586b5451f7bSTheodore Ts'o 		goto fallback_retry;
587ac27a0ecSDave Kleikamp 	}
588ac27a0ecSDave Kleikamp 
589ac27a0ecSDave Kleikamp 	return -1;
590ac27a0ecSDave Kleikamp }
591ac27a0ecSDave Kleikamp 
5922aa9fc4cSAvantika Mathur static int find_group_other(struct super_block *sb, struct inode *parent,
593dcca3fecSAl Viro 			    ext4_group_t *group, umode_t mode)
594ac27a0ecSDave Kleikamp {
595fd2d4291SAvantika Mathur 	ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
5968df9675fSTheodore Ts'o 	ext4_group_t i, last, ngroups = ext4_get_groups_count(sb);
597617ba13bSMingming Cao 	struct ext4_group_desc *desc;
598a4912123STheodore Ts'o 	int flex_size = ext4_flex_bg_size(EXT4_SB(sb));
599a4912123STheodore Ts'o 
600a4912123STheodore Ts'o 	/*
601a4912123STheodore Ts'o 	 * Try to place the inode is the same flex group as its
602a4912123STheodore Ts'o 	 * parent.  If we can't find space, use the Orlov algorithm to
603a4912123STheodore Ts'o 	 * find another flex group, and store that information in the
604a4912123STheodore Ts'o 	 * parent directory's inode information so that use that flex
605a4912123STheodore Ts'o 	 * group for future allocations.
606a4912123STheodore Ts'o 	 */
607a4912123STheodore Ts'o 	if (flex_size > 1) {
608a4912123STheodore Ts'o 		int retry = 0;
609a4912123STheodore Ts'o 
610a4912123STheodore Ts'o 	try_again:
611a4912123STheodore Ts'o 		parent_group &= ~(flex_size-1);
612a4912123STheodore Ts'o 		last = parent_group + flex_size;
613a4912123STheodore Ts'o 		if (last > ngroups)
614a4912123STheodore Ts'o 			last = ngroups;
615a4912123STheodore Ts'o 		for  (i = parent_group; i < last; i++) {
616a4912123STheodore Ts'o 			desc = ext4_get_group_desc(sb, i, NULL);
617a4912123STheodore Ts'o 			if (desc && ext4_free_inodes_count(sb, desc)) {
618a4912123STheodore Ts'o 				*group = i;
619a4912123STheodore Ts'o 				return 0;
620a4912123STheodore Ts'o 			}
621a4912123STheodore Ts'o 		}
622a4912123STheodore Ts'o 		if (!retry && EXT4_I(parent)->i_last_alloc_group != ~0) {
623a4912123STheodore Ts'o 			retry = 1;
624a4912123STheodore Ts'o 			parent_group = EXT4_I(parent)->i_last_alloc_group;
625a4912123STheodore Ts'o 			goto try_again;
626a4912123STheodore Ts'o 		}
627a4912123STheodore Ts'o 		/*
628a4912123STheodore Ts'o 		 * If this didn't work, use the Orlov search algorithm
629a4912123STheodore Ts'o 		 * to find a new flex group; we pass in the mode to
630a4912123STheodore Ts'o 		 * avoid the topdir algorithms.
631a4912123STheodore Ts'o 		 */
632a4912123STheodore Ts'o 		*group = parent_group + flex_size;
633a4912123STheodore Ts'o 		if (*group > ngroups)
634a4912123STheodore Ts'o 			*group = 0;
6357dc57615SPeter Huewe 		return find_group_orlov(sb, parent, group, mode, NULL);
636a4912123STheodore Ts'o 	}
637ac27a0ecSDave Kleikamp 
638ac27a0ecSDave Kleikamp 	/*
639ac27a0ecSDave Kleikamp 	 * Try to place the inode in its parent directory
640ac27a0ecSDave Kleikamp 	 */
6412aa9fc4cSAvantika Mathur 	*group = parent_group;
6422aa9fc4cSAvantika Mathur 	desc = ext4_get_group_desc(sb, *group, NULL);
643560671a0SAneesh Kumar K.V 	if (desc && ext4_free_inodes_count(sb, desc) &&
644021b65bbSTheodore Ts'o 	    ext4_free_group_clusters(sb, desc))
6452aa9fc4cSAvantika Mathur 		return 0;
646ac27a0ecSDave Kleikamp 
647ac27a0ecSDave Kleikamp 	/*
648ac27a0ecSDave Kleikamp 	 * We're going to place this inode in a different blockgroup from its
649ac27a0ecSDave Kleikamp 	 * parent.  We want to cause files in a common directory to all land in
650ac27a0ecSDave Kleikamp 	 * the same blockgroup.  But we want files which are in a different
651ac27a0ecSDave Kleikamp 	 * directory which shares a blockgroup with our parent to land in a
652ac27a0ecSDave Kleikamp 	 * different blockgroup.
653ac27a0ecSDave Kleikamp 	 *
654ac27a0ecSDave Kleikamp 	 * So add our directory's i_ino into the starting point for the hash.
655ac27a0ecSDave Kleikamp 	 */
6562aa9fc4cSAvantika Mathur 	*group = (*group + parent->i_ino) % ngroups;
657ac27a0ecSDave Kleikamp 
658ac27a0ecSDave Kleikamp 	/*
659ac27a0ecSDave Kleikamp 	 * Use a quadratic hash to find a group with a free inode and some free
660ac27a0ecSDave Kleikamp 	 * blocks.
661ac27a0ecSDave Kleikamp 	 */
662ac27a0ecSDave Kleikamp 	for (i = 1; i < ngroups; i <<= 1) {
6632aa9fc4cSAvantika Mathur 		*group += i;
6642aa9fc4cSAvantika Mathur 		if (*group >= ngroups)
6652aa9fc4cSAvantika Mathur 			*group -= ngroups;
6662aa9fc4cSAvantika Mathur 		desc = ext4_get_group_desc(sb, *group, NULL);
667560671a0SAneesh Kumar K.V 		if (desc && ext4_free_inodes_count(sb, desc) &&
668021b65bbSTheodore Ts'o 		    ext4_free_group_clusters(sb, desc))
6692aa9fc4cSAvantika Mathur 			return 0;
670ac27a0ecSDave Kleikamp 	}
671ac27a0ecSDave Kleikamp 
672ac27a0ecSDave Kleikamp 	/*
673ac27a0ecSDave Kleikamp 	 * That failed: try linear search for a free inode, even if that group
674ac27a0ecSDave Kleikamp 	 * has no free blocks.
675ac27a0ecSDave Kleikamp 	 */
6762aa9fc4cSAvantika Mathur 	*group = parent_group;
677ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
6782aa9fc4cSAvantika Mathur 		if (++*group >= ngroups)
6792aa9fc4cSAvantika Mathur 			*group = 0;
6802aa9fc4cSAvantika Mathur 		desc = ext4_get_group_desc(sb, *group, NULL);
681560671a0SAneesh Kumar K.V 		if (desc && ext4_free_inodes_count(sb, desc))
6822aa9fc4cSAvantika Mathur 			return 0;
683ac27a0ecSDave Kleikamp 	}
684ac27a0ecSDave Kleikamp 
685ac27a0ecSDave Kleikamp 	return -1;
686ac27a0ecSDave Kleikamp }
687ac27a0ecSDave Kleikamp 
688ac27a0ecSDave Kleikamp /*
68919883bd9STheodore Ts'o  * In no journal mode, if an inode has recently been deleted, we want
69019883bd9STheodore Ts'o  * to avoid reusing it until we're reasonably sure the inode table
69119883bd9STheodore Ts'o  * block has been written back to disk.  (Yes, these values are
69219883bd9STheodore Ts'o  * somewhat arbitrary...)
69319883bd9STheodore Ts'o  */
69419883bd9STheodore Ts'o #define RECENTCY_MIN	5
695*b5f51573SAndreas Dilger #define RECENTCY_DIRTY	300
69619883bd9STheodore Ts'o 
69719883bd9STheodore Ts'o static int recently_deleted(struct super_block *sb, ext4_group_t group, int ino)
69819883bd9STheodore Ts'o {
69919883bd9STheodore Ts'o 	struct ext4_group_desc	*gdp;
70019883bd9STheodore Ts'o 	struct ext4_inode	*raw_inode;
70119883bd9STheodore Ts'o 	struct buffer_head	*bh;
70219883bd9STheodore Ts'o 	int inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
703*b5f51573SAndreas Dilger 	int offset, ret = 0;
704*b5f51573SAndreas Dilger 	int recentcy = RECENTCY_MIN;
705*b5f51573SAndreas Dilger 	u32 dtime, now;
70619883bd9STheodore Ts'o 
70719883bd9STheodore Ts'o 	gdp = ext4_get_group_desc(sb, group, NULL);
70819883bd9STheodore Ts'o 	if (unlikely(!gdp))
70919883bd9STheodore Ts'o 		return 0;
71019883bd9STheodore Ts'o 
7114f9d956dSJan Kara 	bh = sb_find_get_block(sb, ext4_inode_table(sb, gdp) +
71219883bd9STheodore Ts'o 		       (ino / inodes_per_block));
7134f9d956dSJan Kara 	if (!bh || !buffer_uptodate(bh))
71419883bd9STheodore Ts'o 		/*
71519883bd9STheodore Ts'o 		 * If the block is not in the buffer cache, then it
71619883bd9STheodore Ts'o 		 * must have been written out.
71719883bd9STheodore Ts'o 		 */
71819883bd9STheodore Ts'o 		goto out;
71919883bd9STheodore Ts'o 
72019883bd9STheodore Ts'o 	offset = (ino % inodes_per_block) * EXT4_INODE_SIZE(sb);
72119883bd9STheodore Ts'o 	raw_inode = (struct ext4_inode *) (bh->b_data + offset);
722*b5f51573SAndreas Dilger 
723*b5f51573SAndreas Dilger 	/* i_dtime is only 32 bits on disk, but we only care about relative
724*b5f51573SAndreas Dilger 	 * times in the range of a few minutes (i.e. long enough to sync a
725*b5f51573SAndreas Dilger 	 * recently-deleted inode to disk), so using the low 32 bits of the
726*b5f51573SAndreas Dilger 	 * clock (a 68 year range) is enough, see time_before32() */
72719883bd9STheodore Ts'o 	dtime = le32_to_cpu(raw_inode->i_dtime);
728*b5f51573SAndreas Dilger 	now = ktime_get_real_seconds();
72919883bd9STheodore Ts'o 	if (buffer_dirty(bh))
73019883bd9STheodore Ts'o 		recentcy += RECENTCY_DIRTY;
73119883bd9STheodore Ts'o 
732*b5f51573SAndreas Dilger 	if (dtime && time_before32(dtime, now) &&
733*b5f51573SAndreas Dilger 	    time_before32(now, dtime + recentcy))
73419883bd9STheodore Ts'o 		ret = 1;
73519883bd9STheodore Ts'o out:
73619883bd9STheodore Ts'o 	brelse(bh);
73719883bd9STheodore Ts'o 	return ret;
73819883bd9STheodore Ts'o }
73919883bd9STheodore Ts'o 
740901ed070SWang Shilong static int find_inode_bit(struct super_block *sb, ext4_group_t group,
741901ed070SWang Shilong 			  struct buffer_head *bitmap, unsigned long *ino)
742901ed070SWang Shilong {
743901ed070SWang Shilong next:
744901ed070SWang Shilong 	*ino = ext4_find_next_zero_bit((unsigned long *)
745901ed070SWang Shilong 				       bitmap->b_data,
746901ed070SWang Shilong 				       EXT4_INODES_PER_GROUP(sb), *ino);
747901ed070SWang Shilong 	if (*ino >= EXT4_INODES_PER_GROUP(sb))
748901ed070SWang Shilong 		return 0;
749901ed070SWang Shilong 
750901ed070SWang Shilong 	if ((EXT4_SB(sb)->s_journal == NULL) &&
751901ed070SWang Shilong 	    recently_deleted(sb, group, *ino)) {
752901ed070SWang Shilong 		*ino = *ino + 1;
753901ed070SWang Shilong 		if (*ino < EXT4_INODES_PER_GROUP(sb))
754901ed070SWang Shilong 			goto next;
755901ed070SWang Shilong 		return 0;
756901ed070SWang Shilong 	}
757901ed070SWang Shilong 
758901ed070SWang Shilong 	return 1;
759901ed070SWang Shilong }
760901ed070SWang Shilong 
76119883bd9STheodore Ts'o /*
762ac27a0ecSDave Kleikamp  * There are two policies for allocating an inode.  If the new inode is
763ac27a0ecSDave Kleikamp  * a directory, then a forward search is made for a block group with both
764ac27a0ecSDave Kleikamp  * free space and a low directory-to-inode ratio; if that fails, then of
765ac27a0ecSDave Kleikamp  * the groups with above-average free space, that group with the fewest
766ac27a0ecSDave Kleikamp  * directories already is chosen.
767ac27a0ecSDave Kleikamp  *
768ac27a0ecSDave Kleikamp  * For other inodes, search forward from the parent directory's block
769ac27a0ecSDave Kleikamp  * group to find a free inode.
770ac27a0ecSDave Kleikamp  */
7711139575aSTheodore Ts'o struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
7721139575aSTheodore Ts'o 			       umode_t mode, const struct qstr *qstr,
7731b917ed8STahsin Erdogan 			       __u32 goal, uid_t *owner, __u32 i_flags,
7741b917ed8STahsin Erdogan 			       int handle_type, unsigned int line_no,
7751b917ed8STahsin Erdogan 			       int nblocks)
776ac27a0ecSDave Kleikamp {
777ac27a0ecSDave Kleikamp 	struct super_block *sb;
7783300bedaSAneesh Kumar K.V 	struct buffer_head *inode_bitmap_bh = NULL;
7793300bedaSAneesh Kumar K.V 	struct buffer_head *group_desc_bh;
7808df9675fSTheodore Ts'o 	ext4_group_t ngroups, group = 0;
781ac27a0ecSDave Kleikamp 	unsigned long ino = 0;
782ac27a0ecSDave Kleikamp 	struct inode *inode;
783617ba13bSMingming Cao 	struct ext4_group_desc *gdp = NULL;
784617ba13bSMingming Cao 	struct ext4_inode_info *ei;
785617ba13bSMingming Cao 	struct ext4_sb_info *sbi;
786a7cdadeeSJan Kara 	int ret2, err;
787ac27a0ecSDave Kleikamp 	struct inode *ret;
7882aa9fc4cSAvantika Mathur 	ext4_group_t i;
789772cb7c8SJose R. Santos 	ext4_group_t flex_group;
79087a39389SDarrick J. Wong 	struct ext4_group_info *grp;
791e709e9dfSTheodore Ts'o 	int encrypt = 0;
792ac27a0ecSDave Kleikamp 
793ac27a0ecSDave Kleikamp 	/* Cannot create files in a deleted directory */
794ac27a0ecSDave Kleikamp 	if (!dir || !dir->i_nlink)
795ac27a0ecSDave Kleikamp 		return ERR_PTR(-EPERM);
796ac27a0ecSDave Kleikamp 
797af65207cSTahsin Erdogan 	sb = dir->i_sb;
798af65207cSTahsin Erdogan 	sbi = EXT4_SB(sb);
799af65207cSTahsin Erdogan 
800af65207cSTahsin Erdogan 	if (unlikely(ext4_forced_shutdown(sbi)))
8010db1ff22STheodore Ts'o 		return ERR_PTR(-EIO);
8020db1ff22STheodore Ts'o 
803af65207cSTahsin Erdogan 	if ((ext4_encrypted_inode(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) &&
804ad47f953STahsin Erdogan 	    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) &&
805ad47f953STahsin Erdogan 	    !(i_flags & EXT4_EA_INODE_FL)) {
806a7550b30SJaegeuk Kim 		err = fscrypt_get_encryption_info(dir);
807e709e9dfSTheodore Ts'o 		if (err)
808e709e9dfSTheodore Ts'o 			return ERR_PTR(err);
809a7550b30SJaegeuk Kim 		if (!fscrypt_has_encryption_key(dir))
81054475f53SEric Biggers 			return ERR_PTR(-ENOKEY);
811e709e9dfSTheodore Ts'o 		encrypt = 1;
812e709e9dfSTheodore Ts'o 	}
813e709e9dfSTheodore Ts'o 
814af65207cSTahsin Erdogan 	if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) {
815af65207cSTahsin Erdogan #ifdef CONFIG_EXT4_FS_POSIX_ACL
816af65207cSTahsin Erdogan 		struct posix_acl *p = get_acl(dir, ACL_TYPE_DEFAULT);
817af65207cSTahsin Erdogan 
818af65207cSTahsin Erdogan 		if (p) {
819af65207cSTahsin Erdogan 			int acl_size = p->a_count * sizeof(ext4_acl_entry);
820af65207cSTahsin Erdogan 
821af65207cSTahsin Erdogan 			nblocks += (S_ISDIR(mode) ? 2 : 1) *
822af65207cSTahsin Erdogan 				__ext4_xattr_set_credits(sb, NULL /* inode */,
823af65207cSTahsin Erdogan 					NULL /* block_bh */, acl_size,
824af65207cSTahsin Erdogan 					true /* is_create */);
825af65207cSTahsin Erdogan 			posix_acl_release(p);
826af65207cSTahsin Erdogan 		}
827af65207cSTahsin Erdogan #endif
828af65207cSTahsin Erdogan 
829af65207cSTahsin Erdogan #ifdef CONFIG_SECURITY
830af65207cSTahsin Erdogan 		{
831af65207cSTahsin Erdogan 			int num_security_xattrs = 1;
832af65207cSTahsin Erdogan 
833af65207cSTahsin Erdogan #ifdef CONFIG_INTEGRITY
834af65207cSTahsin Erdogan 			num_security_xattrs++;
835af65207cSTahsin Erdogan #endif
836af65207cSTahsin Erdogan 			/*
837af65207cSTahsin Erdogan 			 * We assume that security xattrs are never
838af65207cSTahsin Erdogan 			 * more than 1k.  In practice they are under
839af65207cSTahsin Erdogan 			 * 128 bytes.
840af65207cSTahsin Erdogan 			 */
841af65207cSTahsin Erdogan 			nblocks += num_security_xattrs *
842af65207cSTahsin Erdogan 				__ext4_xattr_set_credits(sb, NULL /* inode */,
843af65207cSTahsin Erdogan 					NULL /* block_bh */, 1024,
844af65207cSTahsin Erdogan 					true /* is_create */);
845af65207cSTahsin Erdogan 		}
846af65207cSTahsin Erdogan #endif
847af65207cSTahsin Erdogan 		if (encrypt)
848af65207cSTahsin Erdogan 			nblocks += __ext4_xattr_set_credits(sb,
849af65207cSTahsin Erdogan 					NULL /* inode */, NULL /* block_bh */,
850af65207cSTahsin Erdogan 					FSCRYPT_SET_CONTEXT_MAX_SIZE,
851af65207cSTahsin Erdogan 					true /* is_create */);
852af65207cSTahsin Erdogan 	}
853af65207cSTahsin Erdogan 
8548df9675fSTheodore Ts'o 	ngroups = ext4_get_groups_count(sb);
8559bffad1eSTheodore Ts'o 	trace_ext4_request_inode(dir, mode);
856ac27a0ecSDave Kleikamp 	inode = new_inode(sb);
857ac27a0ecSDave Kleikamp 	if (!inode)
858ac27a0ecSDave Kleikamp 		return ERR_PTR(-ENOMEM);
859617ba13bSMingming Cao 	ei = EXT4_I(inode);
860772cb7c8SJose R. Santos 
861eb9cc7e1SJan Kara 	/*
862b8a07463SAdam Buchbinder 	 * Initialize owners and quota early so that we don't have to account
863eb9cc7e1SJan Kara 	 * for quota initialization worst case in standard inode creating
864eb9cc7e1SJan Kara 	 * transaction
865eb9cc7e1SJan Kara 	 */
866eb9cc7e1SJan Kara 	if (owner) {
867eb9cc7e1SJan Kara 		inode->i_mode = mode;
868eb9cc7e1SJan Kara 		i_uid_write(inode, owner[0]);
869eb9cc7e1SJan Kara 		i_gid_write(inode, owner[1]);
870eb9cc7e1SJan Kara 	} else if (test_opt(sb, GRPID)) {
871eb9cc7e1SJan Kara 		inode->i_mode = mode;
872eb9cc7e1SJan Kara 		inode->i_uid = current_fsuid();
873eb9cc7e1SJan Kara 		inode->i_gid = dir->i_gid;
874eb9cc7e1SJan Kara 	} else
875eb9cc7e1SJan Kara 		inode_init_owner(inode, dir, mode);
876040cb378SLi Xi 
8770b7b7779SKaho Ng 	if (ext4_has_feature_project(sb) &&
878040cb378SLi Xi 	    ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT))
879040cb378SLi Xi 		ei->i_projid = EXT4_I(dir)->i_projid;
880040cb378SLi Xi 	else
881040cb378SLi Xi 		ei->i_projid = make_kprojid(&init_user_ns, EXT4_DEF_PROJID);
882040cb378SLi Xi 
883a7cdadeeSJan Kara 	err = dquot_initialize(inode);
884a7cdadeeSJan Kara 	if (err)
885a7cdadeeSJan Kara 		goto out;
886eb9cc7e1SJan Kara 
88711013911SAndreas Dilger 	if (!goal)
88811013911SAndreas Dilger 		goal = sbi->s_inode_goal;
88911013911SAndreas Dilger 
890e6462869SJohann Lombardi 	if (goal && goal <= le32_to_cpu(sbi->s_es->s_inodes_count)) {
89111013911SAndreas Dilger 		group = (goal - 1) / EXT4_INODES_PER_GROUP(sb);
89211013911SAndreas Dilger 		ino = (goal - 1) % EXT4_INODES_PER_GROUP(sb);
89311013911SAndreas Dilger 		ret2 = 0;
89411013911SAndreas Dilger 		goto got_group;
89511013911SAndreas Dilger 	}
89611013911SAndreas Dilger 
8974113c4caSLukas Czerner 	if (S_ISDIR(mode))
898f157a4aaSTheodore Ts'o 		ret2 = find_group_orlov(sb, dir, &group, mode, qstr);
8994113c4caSLukas Czerner 	else
900a4912123STheodore Ts'o 		ret2 = find_group_other(sb, dir, &group, mode);
901ac27a0ecSDave Kleikamp 
902772cb7c8SJose R. Santos got_group:
903a4912123STheodore Ts'o 	EXT4_I(dir)->i_last_alloc_group = group;
904ac27a0ecSDave Kleikamp 	err = -ENOSPC;
9052aa9fc4cSAvantika Mathur 	if (ret2 == -1)
906ac27a0ecSDave Kleikamp 		goto out;
907ac27a0ecSDave Kleikamp 
908119c0d44STheodore Ts'o 	/*
909119c0d44STheodore Ts'o 	 * Normally we will only go through one pass of this loop,
910119c0d44STheodore Ts'o 	 * unless we get unlucky and it turns out the group we selected
911119c0d44STheodore Ts'o 	 * had its last inode grabbed by someone else.
912119c0d44STheodore Ts'o 	 */
91311013911SAndreas Dilger 	for (i = 0; i < ngroups; i++, ino = 0) {
914ac27a0ecSDave Kleikamp 		err = -EIO;
915ac27a0ecSDave Kleikamp 
9163300bedaSAneesh Kumar K.V 		gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
917ac27a0ecSDave Kleikamp 		if (!gdp)
918eb9cc7e1SJan Kara 			goto out;
919ac27a0ecSDave Kleikamp 
920f2a09af6SYongqiang Yang 		/*
921f2a09af6SYongqiang Yang 		 * Check free inodes count before loading bitmap.
922f2a09af6SYongqiang Yang 		 */
9232fe435d8SWang Shilong 		if (ext4_free_inodes_count(sb, gdp) == 0)
9242fe435d8SWang Shilong 			goto next_group;
925f2a09af6SYongqiang Yang 
92687a39389SDarrick J. Wong 		grp = ext4_get_group_info(sb, group);
92787a39389SDarrick J. Wong 		/* Skip groups with already-known suspicious inode tables */
9282fe435d8SWang Shilong 		if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp))
9292fe435d8SWang Shilong 			goto next_group;
93087a39389SDarrick J. Wong 
9313300bedaSAneesh Kumar K.V 		brelse(inode_bitmap_bh);
9323300bedaSAneesh Kumar K.V 		inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
93387a39389SDarrick J. Wong 		/* Skip groups with suspicious inode tables */
9349008a58eSDarrick J. Wong 		if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp) ||
9359008a58eSDarrick J. Wong 		    IS_ERR(inode_bitmap_bh)) {
9369008a58eSDarrick J. Wong 			inode_bitmap_bh = NULL;
9372fe435d8SWang Shilong 			goto next_group;
93887a39389SDarrick J. Wong 		}
939ac27a0ecSDave Kleikamp 
940ac27a0ecSDave Kleikamp repeat_in_this_group:
941901ed070SWang Shilong 		ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino);
942901ed070SWang Shilong 		if (!ret2)
943a34eb503STheodore Ts'o 			goto next_group;
944901ed070SWang Shilong 
945119c0d44STheodore Ts'o 		if (group == 0 && (ino + 1) < EXT4_FIRST_INO(sb)) {
946119c0d44STheodore Ts'o 			ext4_error(sb, "reserved inode found cleared - "
947119c0d44STheodore Ts'o 				   "inode=%lu", ino + 1);
9482fe435d8SWang Shilong 			goto next_group;
949119c0d44STheodore Ts'o 		}
950901ed070SWang Shilong 
9511139575aSTheodore Ts'o 		if (!handle) {
9521139575aSTheodore Ts'o 			BUG_ON(nblocks <= 0);
9531139575aSTheodore Ts'o 			handle = __ext4_journal_start_sb(dir->i_sb, line_no,
9545fe2fe89SJan Kara 							 handle_type, nblocks,
9555fe2fe89SJan Kara 							 0);
9561139575aSTheodore Ts'o 			if (IS_ERR(handle)) {
9571139575aSTheodore Ts'o 				err = PTR_ERR(handle);
958eb9cc7e1SJan Kara 				ext4_std_error(sb, err);
959eb9cc7e1SJan Kara 				goto out;
9601139575aSTheodore Ts'o 			}
9611139575aSTheodore Ts'o 		}
962ffb5387eSEric Sandeen 		BUFFER_TRACE(inode_bitmap_bh, "get_write_access");
963ffb5387eSEric Sandeen 		err = ext4_journal_get_write_access(handle, inode_bitmap_bh);
964eb9cc7e1SJan Kara 		if (err) {
965eb9cc7e1SJan Kara 			ext4_std_error(sb, err);
966eb9cc7e1SJan Kara 			goto out;
967eb9cc7e1SJan Kara 		}
968119c0d44STheodore Ts'o 		ext4_lock_group(sb, group);
969119c0d44STheodore Ts'o 		ret2 = ext4_test_and_set_bit(ino, inode_bitmap_bh->b_data);
970901ed070SWang Shilong 		if (ret2) {
971901ed070SWang Shilong 			/* Someone already took the bit. Repeat the search
972901ed070SWang Shilong 			 * with lock held.
973901ed070SWang Shilong 			 */
974901ed070SWang Shilong 			ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino);
975901ed070SWang Shilong 			if (ret2) {
976901ed070SWang Shilong 				ext4_set_bit(ino, inode_bitmap_bh->b_data);
977901ed070SWang Shilong 				ret2 = 0;
978901ed070SWang Shilong 			} else {
979901ed070SWang Shilong 				ret2 = 1; /* we didn't grab the inode */
980901ed070SWang Shilong 			}
981901ed070SWang Shilong 		}
982119c0d44STheodore Ts'o 		ext4_unlock_group(sb, group);
983119c0d44STheodore Ts'o 		ino++;		/* the inode bitmap is zero-based */
984119c0d44STheodore Ts'o 		if (!ret2)
985119c0d44STheodore Ts'o 			goto got; /* we grabbed the inode! */
986901ed070SWang Shilong 
987119c0d44STheodore Ts'o 		if (ino < EXT4_INODES_PER_GROUP(sb))
988119c0d44STheodore Ts'o 			goto repeat_in_this_group;
989a34eb503STheodore Ts'o next_group:
990a34eb503STheodore Ts'o 		if (++group == ngroups)
991a34eb503STheodore Ts'o 			group = 0;
992ac27a0ecSDave Kleikamp 	}
993ac27a0ecSDave Kleikamp 	err = -ENOSPC;
994ac27a0ecSDave Kleikamp 	goto out;
995ac27a0ecSDave Kleikamp 
996ac27a0ecSDave Kleikamp got:
997ffb5387eSEric Sandeen 	BUFFER_TRACE(inode_bitmap_bh, "call ext4_handle_dirty_metadata");
998ffb5387eSEric Sandeen 	err = ext4_handle_dirty_metadata(handle, NULL, inode_bitmap_bh);
999eb9cc7e1SJan Kara 	if (err) {
1000eb9cc7e1SJan Kara 		ext4_std_error(sb, err);
1001eb9cc7e1SJan Kara 		goto out;
1002eb9cc7e1SJan Kara 	}
1003ffb5387eSEric Sandeen 
100461c219f5STheodore Ts'o 	BUFFER_TRACE(group_desc_bh, "get_write_access");
100561c219f5STheodore Ts'o 	err = ext4_journal_get_write_access(handle, group_desc_bh);
100661c219f5STheodore Ts'o 	if (err) {
100761c219f5STheodore Ts'o 		ext4_std_error(sb, err);
100861c219f5STheodore Ts'o 		goto out;
100961c219f5STheodore Ts'o 	}
101061c219f5STheodore Ts'o 
1011717d50e4SAndreas Dilger 	/* We may have to initialize the block bitmap if it isn't already */
1012feb0ab32SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb) &&
1013717d50e4SAndreas Dilger 	    gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
10143300bedaSAneesh Kumar K.V 		struct buffer_head *block_bitmap_bh;
1015717d50e4SAndreas Dilger 
10163300bedaSAneesh Kumar K.V 		block_bitmap_bh = ext4_read_block_bitmap(sb, group);
10179008a58eSDarrick J. Wong 		if (IS_ERR(block_bitmap_bh)) {
10189008a58eSDarrick J. Wong 			err = PTR_ERR(block_bitmap_bh);
1019599a9b77SJan Kara 			goto out;
1020599a9b77SJan Kara 		}
10213300bedaSAneesh Kumar K.V 		BUFFER_TRACE(block_bitmap_bh, "get block bitmap access");
10223300bedaSAneesh Kumar K.V 		err = ext4_journal_get_write_access(handle, block_bitmap_bh);
1023717d50e4SAndreas Dilger 		if (err) {
10243300bedaSAneesh Kumar K.V 			brelse(block_bitmap_bh);
1025eb9cc7e1SJan Kara 			ext4_std_error(sb, err);
1026eb9cc7e1SJan Kara 			goto out;
1027717d50e4SAndreas Dilger 		}
1028717d50e4SAndreas Dilger 
1029fd034a84STheodore Ts'o 		BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
1030fd034a84STheodore Ts'o 		err = ext4_handle_dirty_metadata(handle, NULL, block_bitmap_bh);
1031fd034a84STheodore Ts'o 
1032717d50e4SAndreas Dilger 		/* recheck and clear flag under lock if we still need to */
1033fd034a84STheodore Ts'o 		ext4_lock_group(sb, group);
1034717d50e4SAndreas Dilger 		if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
10353300bedaSAneesh Kumar K.V 			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
1036021b65bbSTheodore Ts'o 			ext4_free_group_clusters_set(sb, gdp,
1037cff1dfd7STheodore Ts'o 				ext4_free_clusters_after_init(sb, group, gdp));
1038fa77dcfaSDarrick J. Wong 			ext4_block_bitmap_csum_set(sb, group, gdp,
103979f1ba49STao Ma 						   block_bitmap_bh);
1040feb0ab32SDarrick J. Wong 			ext4_group_desc_csum_set(sb, group, gdp);
1041717d50e4SAndreas Dilger 		}
1042955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, group);
1043aeb1e5d6STheodore Ts'o 		brelse(block_bitmap_bh);
1044717d50e4SAndreas Dilger 
1045eb9cc7e1SJan Kara 		if (err) {
1046eb9cc7e1SJan Kara 			ext4_std_error(sb, err);
1047eb9cc7e1SJan Kara 			goto out;
1048eb9cc7e1SJan Kara 		}
1049717d50e4SAndreas Dilger 	}
1050119c0d44STheodore Ts'o 
1051119c0d44STheodore Ts'o 	/* Update the relevant bg descriptor fields */
105241a246d1SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb)) {
1053119c0d44STheodore Ts'o 		int free;
1054119c0d44STheodore Ts'o 		struct ext4_group_info *grp = ext4_get_group_info(sb, group);
1055119c0d44STheodore Ts'o 
1056119c0d44STheodore Ts'o 		down_read(&grp->alloc_sem); /* protect vs itable lazyinit */
1057119c0d44STheodore Ts'o 		ext4_lock_group(sb, group); /* while we modify the bg desc */
1058119c0d44STheodore Ts'o 		free = EXT4_INODES_PER_GROUP(sb) -
1059119c0d44STheodore Ts'o 			ext4_itable_unused_count(sb, gdp);
1060119c0d44STheodore Ts'o 		if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
1061119c0d44STheodore Ts'o 			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
1062119c0d44STheodore Ts'o 			free = 0;
1063119c0d44STheodore Ts'o 		}
1064119c0d44STheodore Ts'o 		/*
1065119c0d44STheodore Ts'o 		 * Check the relative inode number against the last used
1066119c0d44STheodore Ts'o 		 * relative inode number in this group. if it is greater
1067119c0d44STheodore Ts'o 		 * we need to update the bg_itable_unused count
1068119c0d44STheodore Ts'o 		 */
1069119c0d44STheodore Ts'o 		if (ino > free)
1070119c0d44STheodore Ts'o 			ext4_itable_unused_set(sb, gdp,
1071119c0d44STheodore Ts'o 					(EXT4_INODES_PER_GROUP(sb) - ino));
1072119c0d44STheodore Ts'o 		up_read(&grp->alloc_sem);
10736f2e9f0eSTao Ma 	} else {
10746f2e9f0eSTao Ma 		ext4_lock_group(sb, group);
1075119c0d44STheodore Ts'o 	}
10766f2e9f0eSTao Ma 
1077119c0d44STheodore Ts'o 	ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1);
1078119c0d44STheodore Ts'o 	if (S_ISDIR(mode)) {
1079119c0d44STheodore Ts'o 		ext4_used_dirs_set(sb, gdp, ext4_used_dirs_count(sb, gdp) + 1);
1080119c0d44STheodore Ts'o 		if (sbi->s_log_groups_per_flex) {
1081119c0d44STheodore Ts'o 			ext4_group_t f = ext4_flex_group(sbi, group);
1082119c0d44STheodore Ts'o 
1083119c0d44STheodore Ts'o 			atomic_inc(&sbi->s_flex_groups[f].used_dirs);
1084119c0d44STheodore Ts'o 		}
1085119c0d44STheodore Ts'o 	}
108641a246d1SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb)) {
108741a246d1SDarrick J. Wong 		ext4_inode_bitmap_csum_set(sb, group, gdp, inode_bitmap_bh,
108841a246d1SDarrick J. Wong 					   EXT4_INODES_PER_GROUP(sb) / 8);
1089feb0ab32SDarrick J. Wong 		ext4_group_desc_csum_set(sb, group, gdp);
1090119c0d44STheodore Ts'o 	}
10916f2e9f0eSTao Ma 	ext4_unlock_group(sb, group);
1092119c0d44STheodore Ts'o 
10933300bedaSAneesh Kumar K.V 	BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata");
10943300bedaSAneesh Kumar K.V 	err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh);
1095eb9cc7e1SJan Kara 	if (err) {
1096eb9cc7e1SJan Kara 		ext4_std_error(sb, err);
1097eb9cc7e1SJan Kara 		goto out;
1098eb9cc7e1SJan Kara 	}
1099ac27a0ecSDave Kleikamp 
1100ac27a0ecSDave Kleikamp 	percpu_counter_dec(&sbi->s_freeinodes_counter);
1101ac27a0ecSDave Kleikamp 	if (S_ISDIR(mode))
1102ac27a0ecSDave Kleikamp 		percpu_counter_inc(&sbi->s_dirs_counter);
1103ac27a0ecSDave Kleikamp 
1104772cb7c8SJose R. Santos 	if (sbi->s_log_groups_per_flex) {
1105772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, group);
11069f24e420STheodore Ts'o 		atomic_dec(&sbi->s_flex_groups[flex_group].free_inodes);
1107772cb7c8SJose R. Santos 	}
1108ac27a0ecSDave Kleikamp 
1109717d50e4SAndreas Dilger 	inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
1110ac27a0ecSDave Kleikamp 	/* This is the optimal IO size (for stat), not the fs block size */
1111ac27a0ecSDave Kleikamp 	inode->i_blocks = 0;
1112ef7f3835SKalpak Shah 	inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
1113eeca7ea1SDeepa Dinamani 						       current_time(inode);
1114ac27a0ecSDave Kleikamp 
1115ac27a0ecSDave Kleikamp 	memset(ei->i_data, 0, sizeof(ei->i_data));
1116ac27a0ecSDave Kleikamp 	ei->i_dir_start_lookup = 0;
1117ac27a0ecSDave Kleikamp 	ei->i_disksize = 0;
1118ac27a0ecSDave Kleikamp 
11194af83508SEryu Guan 	/* Don't inherit extent flag from directory, amongst others. */
11202dc6b0d4SDuane Griffin 	ei->i_flags =
11212dc6b0d4SDuane Griffin 		ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED);
11221b917ed8STahsin Erdogan 	ei->i_flags |= i_flags;
1123ac27a0ecSDave Kleikamp 	ei->i_file_acl = 0;
1124ac27a0ecSDave Kleikamp 	ei->i_dtime = 0;
1125ac27a0ecSDave Kleikamp 	ei->i_block_group = group;
1126a4912123STheodore Ts'o 	ei->i_last_alloc_group = ~0;
1127ac27a0ecSDave Kleikamp 
1128617ba13bSMingming Cao 	ext4_set_inode_flags(inode);
1129ac27a0ecSDave Kleikamp 	if (IS_DIRSYNC(inode))
11300390131bSFrank Mayhar 		ext4_handle_sync(handle);
11316b38e842SAl Viro 	if (insert_inode_locked(inode) < 0) {
1132acd6ad83SJan Kara 		/*
1133acd6ad83SJan Kara 		 * Likely a bitmap corruption causing inode to be allocated
1134acd6ad83SJan Kara 		 * twice.
1135acd6ad83SJan Kara 		 */
1136acd6ad83SJan Kara 		err = -EIO;
1137eb9cc7e1SJan Kara 		ext4_error(sb, "failed to insert inode %lu: doubly allocated?",
1138eb9cc7e1SJan Kara 			   inode->i_ino);
1139eb9cc7e1SJan Kara 		goto out;
11406b38e842SAl Viro 	}
1141ac27a0ecSDave Kleikamp 	spin_lock(&sbi->s_next_gen_lock);
1142ac27a0ecSDave Kleikamp 	inode->i_generation = sbi->s_next_generation++;
1143ac27a0ecSDave Kleikamp 	spin_unlock(&sbi->s_next_gen_lock);
1144ac27a0ecSDave Kleikamp 
1145814525f4SDarrick J. Wong 	/* Precompute checksum seed for inode metadata */
11469aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sb)) {
1147814525f4SDarrick J. Wong 		__u32 csum;
1148814525f4SDarrick J. Wong 		__le32 inum = cpu_to_le32(inode->i_ino);
1149814525f4SDarrick J. Wong 		__le32 gen = cpu_to_le32(inode->i_generation);
1150814525f4SDarrick J. Wong 		csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
1151814525f4SDarrick J. Wong 				   sizeof(inum));
1152814525f4SDarrick J. Wong 		ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
1153814525f4SDarrick J. Wong 					      sizeof(gen));
1154814525f4SDarrick J. Wong 	}
1155814525f4SDarrick J. Wong 
1156353eb83cSTheodore Ts'o 	ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
115719f5fb7aSTheodore Ts'o 	ext4_set_inode_state(inode, EXT4_STATE_NEW);
1158ef7f3835SKalpak Shah 
1159ef7f3835SKalpak Shah 	ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize;
1160f08225d1STao Ma 	ei->i_inline_off = 0;
1161e2b911c5SDarrick J. Wong 	if (ext4_has_feature_inline_data(sb))
1162f08225d1STao Ma 		ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
1163ac27a0ecSDave Kleikamp 	ret = inode;
116463936ddaSChristoph Hellwig 	err = dquot_alloc_inode(inode);
116563936ddaSChristoph Hellwig 	if (err)
1166ac27a0ecSDave Kleikamp 		goto fail_drop;
1167ac27a0ecSDave Kleikamp 
1168aa1dca3bSEric Biggers 	/*
1169aa1dca3bSEric Biggers 	 * Since the encryption xattr will always be unique, create it first so
1170aa1dca3bSEric Biggers 	 * that it's less likely to end up in an external xattr block and
1171aa1dca3bSEric Biggers 	 * prevent its deduplication.
1172aa1dca3bSEric Biggers 	 */
1173aa1dca3bSEric Biggers 	if (encrypt) {
1174aa1dca3bSEric Biggers 		err = fscrypt_inherit_context(dir, inode, handle, true);
1175aa1dca3bSEric Biggers 		if (err)
1176aa1dca3bSEric Biggers 			goto fail_free_drop;
1177aa1dca3bSEric Biggers 	}
1178aa1dca3bSEric Biggers 
11791b917ed8STahsin Erdogan 	if (!(ei->i_flags & EXT4_EA_INODE_FL)) {
1180617ba13bSMingming Cao 		err = ext4_init_acl(handle, inode, dir);
1181ac27a0ecSDave Kleikamp 		if (err)
1182ac27a0ecSDave Kleikamp 			goto fail_free_drop;
1183ac27a0ecSDave Kleikamp 
11842a7dba39SEric Paris 		err = ext4_init_security(handle, inode, dir, qstr);
1185ac27a0ecSDave Kleikamp 		if (err)
1186ac27a0ecSDave Kleikamp 			goto fail_free_drop;
1187ad47f953STahsin Erdogan 	}
1188ac27a0ecSDave Kleikamp 
1189e2b911c5SDarrick J. Wong 	if (ext4_has_feature_extents(sb)) {
1190e4079a11SEric Sandeen 		/* set extent flag only for directory, file and normal symlink*/
1191e65187e6SAneesh Kumar K.V 		if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
119212e9b892SDmitry Monakhov 			ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
1193a86c6181SAlex Tomas 			ext4_ext_tree_init(handle, inode);
1194a86c6181SAlex Tomas 		}
119542bf0383SAneesh Kumar K.V 	}
1196ac27a0ecSDave Kleikamp 
1197688f869cSTheodore Ts'o 	if (ext4_handle_valid(handle)) {
1198688f869cSTheodore Ts'o 		ei->i_sync_tid = handle->h_transaction->t_tid;
1199688f869cSTheodore Ts'o 		ei->i_datasync_tid = handle->h_transaction->t_tid;
1200688f869cSTheodore Ts'o 	}
1201688f869cSTheodore Ts'o 
12028753e88fSAneesh Kumar K.V 	err = ext4_mark_inode_dirty(handle, inode);
12038753e88fSAneesh Kumar K.V 	if (err) {
12048753e88fSAneesh Kumar K.V 		ext4_std_error(sb, err);
12058753e88fSAneesh Kumar K.V 		goto fail_free_drop;
12068753e88fSAneesh Kumar K.V 	}
12078753e88fSAneesh Kumar K.V 
1208617ba13bSMingming Cao 	ext4_debug("allocating inode %lu\n", inode->i_ino);
12099bffad1eSTheodore Ts'o 	trace_ext4_allocate_inode(inode, dir, mode);
12103300bedaSAneesh Kumar K.V 	brelse(inode_bitmap_bh);
1211ac27a0ecSDave Kleikamp 	return ret;
1212ac27a0ecSDave Kleikamp 
1213ac27a0ecSDave Kleikamp fail_free_drop:
121463936ddaSChristoph Hellwig 	dquot_free_inode(inode);
1215ac27a0ecSDave Kleikamp fail_drop:
12166d6b77f1SMiklos Szeredi 	clear_nlink(inode);
12176b38e842SAl Viro 	unlock_new_inode(inode);
1218eb9cc7e1SJan Kara out:
1219eb9cc7e1SJan Kara 	dquot_drop(inode);
1220eb9cc7e1SJan Kara 	inode->i_flags |= S_NOQUOTA;
1221ac27a0ecSDave Kleikamp 	iput(inode);
12223300bedaSAneesh Kumar K.V 	brelse(inode_bitmap_bh);
1223ac27a0ecSDave Kleikamp 	return ERR_PTR(err);
1224ac27a0ecSDave Kleikamp }
1225ac27a0ecSDave Kleikamp 
1226ac27a0ecSDave Kleikamp /* Verify that we are loading a valid orphan from disk */
1227617ba13bSMingming Cao struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
1228ac27a0ecSDave Kleikamp {
1229617ba13bSMingming Cao 	unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
1230fd2d4291SAvantika Mathur 	ext4_group_t block_group;
1231ac27a0ecSDave Kleikamp 	int bit;
12327827a7f6STheodore Ts'o 	struct buffer_head *bitmap_bh = NULL;
1233ac27a0ecSDave Kleikamp 	struct inode *inode = NULL;
12347827a7f6STheodore Ts'o 	int err = -EFSCORRUPTED;
1235ac27a0ecSDave Kleikamp 
12367827a7f6STheodore Ts'o 	if (ino < EXT4_FIRST_INO(sb) || ino > max_ino)
12377827a7f6STheodore Ts'o 		goto bad_orphan;
1238ac27a0ecSDave Kleikamp 
1239617ba13bSMingming Cao 	block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
1240617ba13bSMingming Cao 	bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
1241e29d1cdeSEric Sandeen 	bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
12429008a58eSDarrick J. Wong 	if (IS_ERR(bitmap_bh)) {
12437827a7f6STheodore Ts'o 		ext4_error(sb, "inode bitmap error %ld for orphan %lu",
12447827a7f6STheodore Ts'o 			   ino, PTR_ERR(bitmap_bh));
12457827a7f6STheodore Ts'o 		return (struct inode *) bitmap_bh;
1246ac27a0ecSDave Kleikamp 	}
1247ac27a0ecSDave Kleikamp 
1248ac27a0ecSDave Kleikamp 	/* Having the inode bit set should be a 100% indicator that this
1249ac27a0ecSDave Kleikamp 	 * is a valid orphan (no e2fsck run on fs).  Orphans also include
1250ac27a0ecSDave Kleikamp 	 * inodes that were being truncated, so we can't check i_nlink==0.
1251ac27a0ecSDave Kleikamp 	 */
12521d1fe1eeSDavid Howells 	if (!ext4_test_bit(bit, bitmap_bh->b_data))
12531d1fe1eeSDavid Howells 		goto bad_orphan;
12541d1fe1eeSDavid Howells 
12551d1fe1eeSDavid Howells 	inode = ext4_iget(sb, ino);
12567827a7f6STheodore Ts'o 	if (IS_ERR(inode)) {
12577827a7f6STheodore Ts'o 		err = PTR_ERR(inode);
12587827a7f6STheodore Ts'o 		ext4_error(sb, "couldn't read orphan inode %lu (err %d)",
12597827a7f6STheodore Ts'o 			   ino, err);
12607827a7f6STheodore Ts'o 		return inode;
12617827a7f6STheodore Ts'o 	}
12621d1fe1eeSDavid Howells 
126391ef4cafSDuane Griffin 	/*
1264c9eb13a9STheodore Ts'o 	 * If the orphans has i_nlinks > 0 then it should be able to
1265c9eb13a9STheodore Ts'o 	 * be truncated, otherwise it won't be removed from the orphan
1266c9eb13a9STheodore Ts'o 	 * list during processing and an infinite loop will result.
1267c9eb13a9STheodore Ts'o 	 * Similarly, it must not be a bad inode.
126891ef4cafSDuane Griffin 	 */
1269c9eb13a9STheodore Ts'o 	if ((inode->i_nlink && !ext4_can_truncate(inode)) ||
1270c9eb13a9STheodore Ts'o 	    is_bad_inode(inode))
127191ef4cafSDuane Griffin 		goto bad_orphan;
127291ef4cafSDuane Griffin 
12731d1fe1eeSDavid Howells 	if (NEXT_ORPHAN(inode) > max_ino)
12741d1fe1eeSDavid Howells 		goto bad_orphan;
12751d1fe1eeSDavid Howells 	brelse(bitmap_bh);
12761d1fe1eeSDavid Howells 	return inode;
12771d1fe1eeSDavid Howells 
12781d1fe1eeSDavid Howells bad_orphan:
12797827a7f6STheodore Ts'o 	ext4_error(sb, "bad orphan inode %lu", ino);
12807827a7f6STheodore Ts'o 	if (bitmap_bh)
12817827a7f6STheodore Ts'o 		printk(KERN_ERR "ext4_test_bit(bit=%d, block=%llu) = %d\n",
1282ac27a0ecSDave Kleikamp 		       bit, (unsigned long long)bitmap_bh->b_blocknr,
1283617ba13bSMingming Cao 		       ext4_test_bit(bit, bitmap_bh->b_data));
1284ac27a0ecSDave Kleikamp 	if (inode) {
12857827a7f6STheodore Ts'o 		printk(KERN_ERR "is_bad_inode(inode)=%d\n",
1286ac27a0ecSDave Kleikamp 		       is_bad_inode(inode));
12877827a7f6STheodore Ts'o 		printk(KERN_ERR "NEXT_ORPHAN(inode)=%u\n",
1288ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
12897827a7f6STheodore Ts'o 		printk(KERN_ERR "max_ino=%lu\n", max_ino);
12907827a7f6STheodore Ts'o 		printk(KERN_ERR "i_nlink=%u\n", inode->i_nlink);
1291ac27a0ecSDave Kleikamp 		/* Avoid freeing blocks if we got a bad deleted inode */
12921d1fe1eeSDavid Howells 		if (inode->i_nlink == 0)
1293ac27a0ecSDave Kleikamp 			inode->i_blocks = 0;
1294ac27a0ecSDave Kleikamp 		iput(inode);
1295ac27a0ecSDave Kleikamp 	}
1296ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
12971d1fe1eeSDavid Howells 	return ERR_PTR(err);
1298ac27a0ecSDave Kleikamp }
1299ac27a0ecSDave Kleikamp 
1300617ba13bSMingming Cao unsigned long ext4_count_free_inodes(struct super_block *sb)
1301ac27a0ecSDave Kleikamp {
1302ac27a0ecSDave Kleikamp 	unsigned long desc_count;
1303617ba13bSMingming Cao 	struct ext4_group_desc *gdp;
13048df9675fSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
1305617ba13bSMingming Cao #ifdef EXT4FS_DEBUG
1306617ba13bSMingming Cao 	struct ext4_super_block *es;
1307ac27a0ecSDave Kleikamp 	unsigned long bitmap_count, x;
1308ac27a0ecSDave Kleikamp 	struct buffer_head *bitmap_bh = NULL;
1309ac27a0ecSDave Kleikamp 
1310617ba13bSMingming Cao 	es = EXT4_SB(sb)->s_es;
1311ac27a0ecSDave Kleikamp 	desc_count = 0;
1312ac27a0ecSDave Kleikamp 	bitmap_count = 0;
1313ac27a0ecSDave Kleikamp 	gdp = NULL;
13148df9675fSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
1315617ba13bSMingming Cao 		gdp = ext4_get_group_desc(sb, i, NULL);
1316ac27a0ecSDave Kleikamp 		if (!gdp)
1317ac27a0ecSDave Kleikamp 			continue;
1318560671a0SAneesh Kumar K.V 		desc_count += ext4_free_inodes_count(sb, gdp);
1319ac27a0ecSDave Kleikamp 		brelse(bitmap_bh);
1320e29d1cdeSEric Sandeen 		bitmap_bh = ext4_read_inode_bitmap(sb, i);
13219008a58eSDarrick J. Wong 		if (IS_ERR(bitmap_bh)) {
13229008a58eSDarrick J. Wong 			bitmap_bh = NULL;
1323ac27a0ecSDave Kleikamp 			continue;
13249008a58eSDarrick J. Wong 		}
1325ac27a0ecSDave Kleikamp 
1326f6fb99caSTheodore Ts'o 		x = ext4_count_free(bitmap_bh->b_data,
1327f6fb99caSTheodore Ts'o 				    EXT4_INODES_PER_GROUP(sb) / 8);
1328c549a95dSEric Sandeen 		printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
1329785b4b3aSPeng Tao 			(unsigned long) i, ext4_free_inodes_count(sb, gdp), x);
1330ac27a0ecSDave Kleikamp 		bitmap_count += x;
1331ac27a0ecSDave Kleikamp 	}
1332ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
13334776004fSTheodore Ts'o 	printk(KERN_DEBUG "ext4_count_free_inodes: "
13344776004fSTheodore Ts'o 	       "stored = %u, computed = %lu, %lu\n",
1335ac27a0ecSDave Kleikamp 	       le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
1336ac27a0ecSDave Kleikamp 	return desc_count;
1337ac27a0ecSDave Kleikamp #else
1338ac27a0ecSDave Kleikamp 	desc_count = 0;
13398df9675fSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
1340617ba13bSMingming Cao 		gdp = ext4_get_group_desc(sb, i, NULL);
1341ac27a0ecSDave Kleikamp 		if (!gdp)
1342ac27a0ecSDave Kleikamp 			continue;
1343560671a0SAneesh Kumar K.V 		desc_count += ext4_free_inodes_count(sb, gdp);
1344ac27a0ecSDave Kleikamp 		cond_resched();
1345ac27a0ecSDave Kleikamp 	}
1346ac27a0ecSDave Kleikamp 	return desc_count;
1347ac27a0ecSDave Kleikamp #endif
1348ac27a0ecSDave Kleikamp }
1349ac27a0ecSDave Kleikamp 
1350ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
1351617ba13bSMingming Cao unsigned long ext4_count_dirs(struct super_block * sb)
1352ac27a0ecSDave Kleikamp {
1353ac27a0ecSDave Kleikamp 	unsigned long count = 0;
13548df9675fSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
1355ac27a0ecSDave Kleikamp 
13568df9675fSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
1357617ba13bSMingming Cao 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1358ac27a0ecSDave Kleikamp 		if (!gdp)
1359ac27a0ecSDave Kleikamp 			continue;
1360560671a0SAneesh Kumar K.V 		count += ext4_used_dirs_count(sb, gdp);
1361ac27a0ecSDave Kleikamp 	}
1362ac27a0ecSDave Kleikamp 	return count;
1363ac27a0ecSDave Kleikamp }
1364bfff6873SLukas Czerner 
1365bfff6873SLukas Czerner /*
1366bfff6873SLukas Czerner  * Zeroes not yet zeroed inode table - just write zeroes through the whole
1367bfff6873SLukas Czerner  * inode table. Must be called without any spinlock held. The only place
1368bfff6873SLukas Czerner  * where it is called from on active part of filesystem is ext4lazyinit
1369bfff6873SLukas Czerner  * thread, so we do not need any special locks, however we have to prevent
1370bfff6873SLukas Czerner  * inode allocation from the current group, so we take alloc_sem lock, to
1371119c0d44STheodore Ts'o  * block ext4_new_inode() until we are finished.
1372bfff6873SLukas Czerner  */
1373e0cbee3eSH Hartley Sweeten int ext4_init_inode_table(struct super_block *sb, ext4_group_t group,
1374bfff6873SLukas Czerner 				 int barrier)
1375bfff6873SLukas Czerner {
1376bfff6873SLukas Czerner 	struct ext4_group_info *grp = ext4_get_group_info(sb, group);
1377bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1378bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
1379bfff6873SLukas Czerner 	struct buffer_head *group_desc_bh;
1380bfff6873SLukas Czerner 	handle_t *handle;
1381bfff6873SLukas Czerner 	ext4_fsblk_t blk;
1382bfff6873SLukas Czerner 	int num, ret = 0, used_blks = 0;
1383bfff6873SLukas Czerner 
1384bfff6873SLukas Czerner 	/* This should not happen, but just to be sure check this */
1385bfff6873SLukas Czerner 	if (sb->s_flags & MS_RDONLY) {
1386bfff6873SLukas Czerner 		ret = 1;
1387bfff6873SLukas Czerner 		goto out;
1388bfff6873SLukas Czerner 	}
1389bfff6873SLukas Czerner 
1390bfff6873SLukas Czerner 	gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
1391bfff6873SLukas Czerner 	if (!gdp)
1392bfff6873SLukas Czerner 		goto out;
1393bfff6873SLukas Czerner 
1394bfff6873SLukas Czerner 	/*
1395bfff6873SLukas Czerner 	 * We do not need to lock this, because we are the only one
1396bfff6873SLukas Czerner 	 * handling this flag.
1397bfff6873SLukas Czerner 	 */
1398bfff6873SLukas Czerner 	if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED))
1399bfff6873SLukas Czerner 		goto out;
1400bfff6873SLukas Czerner 
14019924a92aSTheodore Ts'o 	handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
1402bfff6873SLukas Czerner 	if (IS_ERR(handle)) {
1403bfff6873SLukas Czerner 		ret = PTR_ERR(handle);
1404bfff6873SLukas Czerner 		goto out;
1405bfff6873SLukas Czerner 	}
1406bfff6873SLukas Czerner 
1407bfff6873SLukas Czerner 	down_write(&grp->alloc_sem);
1408bfff6873SLukas Czerner 	/*
1409bfff6873SLukas Czerner 	 * If inode bitmap was already initialized there may be some
1410bfff6873SLukas Czerner 	 * used inodes so we need to skip blocks with used inodes in
1411bfff6873SLukas Czerner 	 * inode table.
1412bfff6873SLukas Czerner 	 */
1413bfff6873SLukas Czerner 	if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)))
1414bfff6873SLukas Czerner 		used_blks = DIV_ROUND_UP((EXT4_INODES_PER_GROUP(sb) -
1415bfff6873SLukas Czerner 			    ext4_itable_unused_count(sb, gdp)),
1416bfff6873SLukas Czerner 			    sbi->s_inodes_per_block);
1417bfff6873SLukas Czerner 
1418857ac889SLukas Czerner 	if ((used_blks < 0) || (used_blks > sbi->s_itb_per_group)) {
14191084f252STheodore Ts'o 		ext4_error(sb, "Something is wrong with group %u: "
14201084f252STheodore Ts'o 			   "used itable blocks: %d; "
14211084f252STheodore Ts'o 			   "itable unused count: %u",
1422857ac889SLukas Czerner 			   group, used_blks,
1423857ac889SLukas Czerner 			   ext4_itable_unused_count(sb, gdp));
1424857ac889SLukas Czerner 		ret = 1;
142533853a0dSYongqiang Yang 		goto err_out;
1426857ac889SLukas Czerner 	}
1427857ac889SLukas Czerner 
1428bfff6873SLukas Czerner 	blk = ext4_inode_table(sb, gdp) + used_blks;
1429bfff6873SLukas Czerner 	num = sbi->s_itb_per_group - used_blks;
1430bfff6873SLukas Czerner 
1431bfff6873SLukas Czerner 	BUFFER_TRACE(group_desc_bh, "get_write_access");
1432bfff6873SLukas Czerner 	ret = ext4_journal_get_write_access(handle,
1433bfff6873SLukas Czerner 					    group_desc_bh);
1434bfff6873SLukas Czerner 	if (ret)
1435bfff6873SLukas Czerner 		goto err_out;
1436bfff6873SLukas Czerner 
1437bfff6873SLukas Czerner 	/*
1438bfff6873SLukas Czerner 	 * Skip zeroout if the inode table is full. But we set the ZEROED
1439bfff6873SLukas Czerner 	 * flag anyway, because obviously, when it is full it does not need
1440bfff6873SLukas Czerner 	 * further zeroing.
1441bfff6873SLukas Czerner 	 */
1442bfff6873SLukas Czerner 	if (unlikely(num == 0))
1443bfff6873SLukas Czerner 		goto skip_zeroout;
1444bfff6873SLukas Czerner 
1445bfff6873SLukas Czerner 	ext4_debug("going to zero out inode table in group %d\n",
1446bfff6873SLukas Czerner 		   group);
1447a107e5a3STheodore Ts'o 	ret = sb_issue_zeroout(sb, blk, num, GFP_NOFS);
1448bfff6873SLukas Czerner 	if (ret < 0)
1449bfff6873SLukas Czerner 		goto err_out;
1450a107e5a3STheodore Ts'o 	if (barrier)
1451a107e5a3STheodore Ts'o 		blkdev_issue_flush(sb->s_bdev, GFP_NOFS, NULL);
1452bfff6873SLukas Czerner 
1453bfff6873SLukas Czerner skip_zeroout:
1454bfff6873SLukas Czerner 	ext4_lock_group(sb, group);
1455bfff6873SLukas Czerner 	gdp->bg_flags |= cpu_to_le16(EXT4_BG_INODE_ZEROED);
1456feb0ab32SDarrick J. Wong 	ext4_group_desc_csum_set(sb, group, gdp);
1457bfff6873SLukas Czerner 	ext4_unlock_group(sb, group);
1458bfff6873SLukas Czerner 
1459bfff6873SLukas Czerner 	BUFFER_TRACE(group_desc_bh,
1460bfff6873SLukas Czerner 		     "call ext4_handle_dirty_metadata");
1461bfff6873SLukas Czerner 	ret = ext4_handle_dirty_metadata(handle, NULL,
1462bfff6873SLukas Czerner 					 group_desc_bh);
1463bfff6873SLukas Czerner 
1464bfff6873SLukas Czerner err_out:
1465bfff6873SLukas Czerner 	up_write(&grp->alloc_sem);
1466bfff6873SLukas Czerner 	ext4_journal_stop(handle);
1467bfff6873SLukas Czerner out:
1468bfff6873SLukas Czerner 	return ret;
1469bfff6873SLukas Czerner }
1470