xref: /openbmc/linux/fs/ext4/balloc.c (revision ecc23d0a422a3118fcf6e4f0a46e17a6c2047b02)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2ac27a0ecSDave Kleikamp /*
3617ba13bSMingming Cao  *  linux/fs/ext4/balloc.c
4ac27a0ecSDave Kleikamp  *
5ac27a0ecSDave Kleikamp  * Copyright (C) 1992, 1993, 1994, 1995
6ac27a0ecSDave Kleikamp  * Remy Card (card@masi.ibp.fr)
7ac27a0ecSDave Kleikamp  * Laboratoire MASI - Institut Blaise Pascal
8ac27a0ecSDave Kleikamp  * Universite Pierre et Marie Curie (Paris VI)
9ac27a0ecSDave Kleikamp  *
10ac27a0ecSDave Kleikamp  *  Enhanced block allocation by 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/capability.h>
17ac27a0ecSDave Kleikamp #include <linux/fs.h>
18ac27a0ecSDave Kleikamp #include <linux/quotaops.h>
19ac27a0ecSDave Kleikamp #include <linux/buffer_head.h>
203dcf5451SChristoph Hellwig #include "ext4.h"
213dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
22e21675d4SAneesh Kumar K.V #include "mballoc.h"
233dcf5451SChristoph Hellwig 
240562e0baSJiaying Zhang #include <trace/events/ext4.h>
250562e0baSJiaying Zhang 
265f163cc7SEric Sandeen static unsigned ext4_num_base_meta_clusters(struct super_block *sb,
275f163cc7SEric Sandeen 					    ext4_group_t block_group);
28ac27a0ecSDave Kleikamp /*
29ac27a0ecSDave Kleikamp  * balloc.c contains the blocks allocation and deallocation routines
30ac27a0ecSDave Kleikamp  */
31ac27a0ecSDave Kleikamp 
32ac27a0ecSDave Kleikamp /*
33bd86298eSLukas Czerner  * Calculate block group number for a given block number
34bd86298eSLukas Czerner  */
ext4_get_group_number(struct super_block * sb,ext4_fsblk_t block)35bd86298eSLukas Czerner ext4_group_t ext4_get_group_number(struct super_block *sb,
36bd86298eSLukas Czerner 				   ext4_fsblk_t block)
37bd86298eSLukas Czerner {
38bd86298eSLukas Czerner 	ext4_group_t group;
39bd86298eSLukas Czerner 
40bd86298eSLukas Czerner 	if (test_opt2(sb, STD_GROUP_SIZE))
41960fd856STheodore Ts'o 		group = (block -
42960fd856STheodore Ts'o 			 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) >>
43bd86298eSLukas Czerner 			(EXT4_BLOCK_SIZE_BITS(sb) + EXT4_CLUSTER_BITS(sb) + 3);
44bd86298eSLukas Czerner 	else
45bd86298eSLukas Czerner 		ext4_get_group_no_and_offset(sb, block, &group, NULL);
46bd86298eSLukas Czerner 	return group;
47bd86298eSLukas Czerner }
48bd86298eSLukas Czerner 
49bd86298eSLukas Czerner /*
503212a80aSTheodore Ts'o  * Calculate the block group number and offset into the block/cluster
513212a80aSTheodore Ts'o  * allocation bitmap, given a block number
5272b64b59SAndrew Morton  */
ext4_get_group_no_and_offset(struct super_block * sb,ext4_fsblk_t blocknr,ext4_group_t * blockgrpp,ext4_grpblk_t * offsetp)5372b64b59SAndrew Morton void ext4_get_group_no_and_offset(struct super_block *sb, ext4_fsblk_t blocknr,
54fd2d4291SAvantika Mathur 		ext4_group_t *blockgrpp, ext4_grpblk_t *offsetp)
5572b64b59SAndrew Morton {
5672b64b59SAndrew Morton 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
5772b64b59SAndrew Morton 	ext4_grpblk_t offset;
5872b64b59SAndrew Morton 
5972b64b59SAndrew Morton 	blocknr = blocknr - le32_to_cpu(es->s_first_data_block);
603212a80aSTheodore Ts'o 	offset = do_div(blocknr, EXT4_BLOCKS_PER_GROUP(sb)) >>
613212a80aSTheodore Ts'o 		EXT4_SB(sb)->s_cluster_bits;
6272b64b59SAndrew Morton 	if (offsetp)
6372b64b59SAndrew Morton 		*offsetp = offset;
6472b64b59SAndrew Morton 	if (blockgrpp)
6572b64b59SAndrew Morton 		*blockgrpp = blocknr;
6672b64b59SAndrew Morton 
6772b64b59SAndrew Morton }
6872b64b59SAndrew Morton 
6968911009SLukas Czerner /*
7068911009SLukas Czerner  * Check whether the 'block' lives within the 'block_group'. Returns 1 if so
7168911009SLukas Czerner  * and 0 otherwise.
7268911009SLukas Czerner  */
ext4_block_in_group(struct super_block * sb,ext4_fsblk_t block,ext4_group_t block_group)7368911009SLukas Czerner static inline int ext4_block_in_group(struct super_block *sb,
7468911009SLukas Czerner 				      ext4_fsblk_t block,
750bf7e837SJose R. Santos 				      ext4_group_t block_group)
760bf7e837SJose R. Santos {
770bf7e837SJose R. Santos 	ext4_group_t actual_group;
7868911009SLukas Czerner 
79bd86298eSLukas Czerner 	actual_group = ext4_get_group_number(sb, block);
8068911009SLukas Czerner 	return (actual_group == block_group) ? 1 : 0;
810bf7e837SJose R. Santos }
820bf7e837SJose R. Santos 
8368e294dcSKemeng Shi /*
8468e294dcSKemeng Shi  * Return the number of clusters used for file system metadata; this
85d5b8f310STheodore Ts'o  * represents the overhead needed by the file system.
86d5b8f310STheodore Ts'o  */
ext4_num_overhead_clusters(struct super_block * sb,ext4_group_t block_group,struct ext4_group_desc * gdp)87c197855eSStephen Hemminger static unsigned ext4_num_overhead_clusters(struct super_block *sb,
88e187c658STheodore Ts'o 					   ext4_group_t block_group,
89e187c658STheodore Ts'o 					   struct ext4_group_desc *gdp)
900bf7e837SJose R. Santos {
9168e294dcSKemeng Shi 	unsigned base_clusters, num_clusters;
9268e294dcSKemeng Shi 	int block_cluster = -1, inode_cluster;
9368e294dcSKemeng Shi 	int itbl_cluster_start = -1, itbl_cluster_end = -1;
94d5b8f310STheodore Ts'o 	ext4_fsblk_t start = ext4_group_first_block_no(sb, block_group);
9568e294dcSKemeng Shi 	ext4_fsblk_t end = start + EXT4_BLOCKS_PER_GROUP(sb) - 1;
9668e294dcSKemeng Shi 	ext4_fsblk_t itbl_blk_start, itbl_blk_end;
970bf7e837SJose R. Santos 	struct ext4_sb_info *sbi = EXT4_SB(sb);
980bf7e837SJose R. Santos 
99d5b8f310STheodore Ts'o 	/* This is the number of clusters used by the superblock,
100d5b8f310STheodore Ts'o 	 * block group descriptors, and reserved block group
101d5b8f310STheodore Ts'o 	 * descriptor blocks */
10268e294dcSKemeng Shi 	base_clusters = ext4_num_base_meta_clusters(sb, block_group);
10368e294dcSKemeng Shi 	num_clusters = base_clusters;
1040bf7e837SJose R. Santos 
105d5b8f310STheodore Ts'o 	/*
10668e294dcSKemeng Shi 	 * Account and record inode table clusters if any cluster
10768e294dcSKemeng Shi 	 * is in the block group, or inode table cluster range is
10868e294dcSKemeng Shi 	 * [-1, -1] and won't overlap with block/inode bitmap cluster
10968e294dcSKemeng Shi 	 * accounted below.
11068e294dcSKemeng Shi 	 */
11168e294dcSKemeng Shi 	itbl_blk_start = ext4_inode_table(sb, gdp);
11268e294dcSKemeng Shi 	itbl_blk_end = itbl_blk_start + sbi->s_itb_per_group - 1;
11368e294dcSKemeng Shi 	if (itbl_blk_start <= end && itbl_blk_end >= start) {
11468e294dcSKemeng Shi 		itbl_blk_start = itbl_blk_start >= start ?
11568e294dcSKemeng Shi 			itbl_blk_start : start;
11668e294dcSKemeng Shi 		itbl_blk_end = itbl_blk_end <= end ?
11768e294dcSKemeng Shi 			itbl_blk_end : end;
11868e294dcSKemeng Shi 
11968e294dcSKemeng Shi 		itbl_cluster_start = EXT4_B2C(sbi, itbl_blk_start - start);
12068e294dcSKemeng Shi 		itbl_cluster_end = EXT4_B2C(sbi, itbl_blk_end - start);
12168e294dcSKemeng Shi 
12268e294dcSKemeng Shi 		num_clusters += itbl_cluster_end - itbl_cluster_start + 1;
12368e294dcSKemeng Shi 		/* check if border cluster is overlapped */
12468e294dcSKemeng Shi 		if (itbl_cluster_start == base_clusters - 1)
12568e294dcSKemeng Shi 			num_clusters--;
12668e294dcSKemeng Shi 	}
12768e294dcSKemeng Shi 
12868e294dcSKemeng Shi 	/*
12968e294dcSKemeng Shi 	 * For the allocation bitmaps, we first need to check to see
13068e294dcSKemeng Shi 	 * if the block is in the block group.  If it is, then check
13168e294dcSKemeng Shi 	 * to see if the cluster is already accounted for in the clusters
13268e294dcSKemeng Shi 	 * used for the base metadata cluster and inode tables cluster.
133d5b8f310STheodore Ts'o 	 * Normally all of these blocks are contiguous, so the special
134d5b8f310STheodore Ts'o 	 * case handling shouldn't be necessary except for *very*
135d5b8f310STheodore Ts'o 	 * unusual file system layouts.
136d5b8f310STheodore Ts'o 	 */
137d5b8f310STheodore Ts'o 	if (ext4_block_in_group(sb, ext4_block_bitmap(sb, gdp), block_group)) {
138b0dd6b70STheodore Ts'o 		block_cluster = EXT4_B2C(sbi,
139b0dd6b70STheodore Ts'o 					 ext4_block_bitmap(sb, gdp) - start);
14068e294dcSKemeng Shi 		if (block_cluster >= base_clusters &&
14168e294dcSKemeng Shi 		    (block_cluster < itbl_cluster_start ||
14268e294dcSKemeng Shi 		    block_cluster > itbl_cluster_end))
143d5b8f310STheodore Ts'o 			num_clusters++;
1440bf7e837SJose R. Santos 	}
145d5b8f310STheodore Ts'o 
146d5b8f310STheodore Ts'o 	if (ext4_block_in_group(sb, ext4_inode_bitmap(sb, gdp), block_group)) {
147d5b8f310STheodore Ts'o 		inode_cluster = EXT4_B2C(sbi,
148b0dd6b70STheodore Ts'o 					 ext4_inode_bitmap(sb, gdp) - start);
14968e294dcSKemeng Shi 		/*
15068e294dcSKemeng Shi 		 * Additional check if inode bitmap is in just accounted
15168e294dcSKemeng Shi 		 * block_cluster
15268e294dcSKemeng Shi 		 */
15368e294dcSKemeng Shi 		if (inode_cluster != block_cluster &&
15468e294dcSKemeng Shi 		    inode_cluster >= base_clusters &&
15568e294dcSKemeng Shi 		    (inode_cluster < itbl_cluster_start ||
15668e294dcSKemeng Shi 		    inode_cluster > itbl_cluster_end))
157d5b8f310STheodore Ts'o 			num_clusters++;
158d5b8f310STheodore Ts'o 	}
159d5b8f310STheodore Ts'o 
160d5b8f310STheodore Ts'o 	return num_clusters;
161d5b8f310STheodore Ts'o }
162d5b8f310STheodore Ts'o 
num_clusters_in_group(struct super_block * sb,ext4_group_t block_group)163d5b8f310STheodore Ts'o static unsigned int num_clusters_in_group(struct super_block *sb,
16449f7f9afSTheodore Ts'o 					  ext4_group_t block_group)
16549f7f9afSTheodore Ts'o {
166d5b8f310STheodore Ts'o 	unsigned int blocks;
167d5b8f310STheodore Ts'o 
16849f7f9afSTheodore Ts'o 	if (block_group == ext4_get_groups_count(sb) - 1) {
16949f7f9afSTheodore Ts'o 		/*
17049f7f9afSTheodore Ts'o 		 * Even though mke2fs always initializes the first and
17149f7f9afSTheodore Ts'o 		 * last group, just in case some other tool was used,
17249f7f9afSTheodore Ts'o 		 * we need to make sure we calculate the right free
17349f7f9afSTheodore Ts'o 		 * blocks.
17449f7f9afSTheodore Ts'o 		 */
175d5b8f310STheodore Ts'o 		blocks = ext4_blocks_count(EXT4_SB(sb)->s_es) -
17649f7f9afSTheodore Ts'o 			ext4_group_first_block_no(sb, block_group);
17749f7f9afSTheodore Ts'o 	} else
178d5b8f310STheodore Ts'o 		blocks = EXT4_BLOCKS_PER_GROUP(sb);
179d5b8f310STheodore Ts'o 	return EXT4_NUM_B2C(EXT4_SB(sb), blocks);
18049f7f9afSTheodore Ts'o }
18149f7f9afSTheodore Ts'o 
182fd034a84STheodore Ts'o /* Initializes an uninitialized block bitmap */
ext4_init_block_bitmap(struct super_block * sb,struct buffer_head * bh,ext4_group_t block_group,struct ext4_group_desc * gdp)183aef4885aSDmitry Monakhov static int ext4_init_block_bitmap(struct super_block *sb,
184c197855eSStephen Hemminger 				   struct buffer_head *bh,
185fd034a84STheodore Ts'o 				   ext4_group_t block_group,
186fd034a84STheodore Ts'o 				   struct ext4_group_desc *gdp)
187717d50e4SAndreas Dilger {
188d5b8f310STheodore Ts'o 	unsigned int bit, bit_max;
189717d50e4SAndreas Dilger 	struct ext4_sb_info *sbi = EXT4_SB(sb);
190fd034a84STheodore Ts'o 	ext4_fsblk_t start, tmp;
191717d50e4SAndreas Dilger 
192837c23fbSChunguang Xu 	ASSERT(buffer_locked(bh));
193717d50e4SAndreas Dilger 
194feb0ab32SDarrick J. Wong 	if (!ext4_group_desc_csum_verify(sb, block_group, gdp)) {
195db79e6d1SWang Shilong 		ext4_mark_group_bitmap_corrupted(sb, block_group,
196db79e6d1SWang Shilong 					EXT4_GROUP_INFO_BBITMAP_CORRUPT |
197db79e6d1SWang Shilong 					EXT4_GROUP_INFO_IBITMAP_CORRUPT);
1986a797d27SDarrick J. Wong 		return -EFSBADCRC;
199717d50e4SAndreas Dilger 	}
200717d50e4SAndreas Dilger 	memset(bh->b_data, 0, sb->s_blocksize);
201d00a6d7bSAkinobu Mita 
202d5b8f310STheodore Ts'o 	bit_max = ext4_num_base_meta_clusters(sb, block_group);
2035b9554dcSTheodore Ts'o 	if ((bit_max >> 3) >= bh->b_size)
2045b9554dcSTheodore Ts'o 		return -EFSCORRUPTED;
2055b9554dcSTheodore Ts'o 
206717d50e4SAndreas Dilger 	for (bit = 0; bit < bit_max; bit++)
207717d50e4SAndreas Dilger 		ext4_set_bit(bit, bh->b_data);
208717d50e4SAndreas Dilger 
209d00a6d7bSAkinobu Mita 	start = ext4_group_first_block_no(sb, block_group);
210717d50e4SAndreas Dilger 
2110bf7e837SJose R. Santos 	/* Set bits for block and inode bitmaps, and inode table */
2120bf7e837SJose R. Santos 	tmp = ext4_block_bitmap(sb, gdp);
213819b23f1STheodore Ts'o 	if (ext4_block_in_group(sb, tmp, block_group))
214d5b8f310STheodore Ts'o 		ext4_set_bit(EXT4_B2C(sbi, tmp - start), bh->b_data);
2150bf7e837SJose R. Santos 
2160bf7e837SJose R. Santos 	tmp = ext4_inode_bitmap(sb, gdp);
217819b23f1STheodore Ts'o 	if (ext4_block_in_group(sb, tmp, block_group))
218d5b8f310STheodore Ts'o 		ext4_set_bit(EXT4_B2C(sbi, tmp - start), bh->b_data);
2190bf7e837SJose R. Santos 
2200bf7e837SJose R. Santos 	tmp = ext4_inode_table(sb, gdp);
2210bf7e837SJose R. Santos 	for (; tmp < ext4_inode_table(sb, gdp) +
2220bf7e837SJose R. Santos 		     sbi->s_itb_per_group; tmp++) {
223819b23f1STheodore Ts'o 		if (ext4_block_in_group(sb, tmp, block_group))
224d5b8f310STheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, tmp - start), bh->b_data);
2250bf7e837SJose R. Santos 	}
226d5b8f310STheodore Ts'o 
227717d50e4SAndreas Dilger 	/*
228fd034a84STheodore Ts'o 	 * Also if the number of blocks within the group is less than
229fd034a84STheodore Ts'o 	 * the blocksize * 8 ( which is the size of bitmap ), set rest
230fd034a84STheodore Ts'o 	 * of the block bitmap to 1
231717d50e4SAndreas Dilger 	 */
232d5b8f310STheodore Ts'o 	ext4_mark_bitmap_end(num_clusters_in_group(sb, block_group),
233fd034a84STheodore Ts'o 			     sb->s_blocksize * 8, bh->b_data);
234aef4885aSDmitry Monakhov 	return 0;
235717d50e4SAndreas Dilger }
236717d50e4SAndreas Dilger 
237fd034a84STheodore Ts'o /* Return the number of free blocks in a block group.  It is used when
238fd034a84STheodore Ts'o  * the block bitmap is uninitialized, so we can't just count the bits
239fd034a84STheodore Ts'o  * in the bitmap. */
ext4_free_clusters_after_init(struct super_block * sb,ext4_group_t block_group,struct ext4_group_desc * gdp)240cff1dfd7STheodore Ts'o unsigned ext4_free_clusters_after_init(struct super_block *sb,
241fd034a84STheodore Ts'o 				       ext4_group_t block_group,
242fd034a84STheodore Ts'o 				       struct ext4_group_desc *gdp)
243fd034a84STheodore Ts'o {
244d5b8f310STheodore Ts'o 	return num_clusters_in_group(sb, block_group) -
245d5b8f310STheodore Ts'o 		ext4_num_overhead_clusters(sb, block_group, gdp);
246fd034a84STheodore Ts'o }
247717d50e4SAndreas Dilger 
24872b64b59SAndrew Morton /*
249ac27a0ecSDave Kleikamp  * The free blocks are managed by bitmaps.  A file system contains several
250ac27a0ecSDave Kleikamp  * blocks groups.  Each group contains 1 bitmap block for blocks, 1 bitmap
251ac27a0ecSDave Kleikamp  * block for inodes, N blocks for the inode table and data blocks.
252ac27a0ecSDave Kleikamp  *
253ac27a0ecSDave Kleikamp  * The file system contains group descriptors which are located after the
254ac27a0ecSDave Kleikamp  * super block.  Each descriptor contains the number of the bitmap block and
255ac27a0ecSDave Kleikamp  * the free blocks count in the block.  The descriptors are loaded in memory
256e627432cSAneesh Kumar K.V  * when a file system is mounted (see ext4_fill_super).
257ac27a0ecSDave Kleikamp  */
258ac27a0ecSDave Kleikamp 
259ac27a0ecSDave Kleikamp /**
260617ba13bSMingming Cao  * ext4_get_group_desc() -- load group descriptor from disk
261ac27a0ecSDave Kleikamp  * @sb:			super block
262ac27a0ecSDave Kleikamp  * @block_group:	given block group
263ac27a0ecSDave Kleikamp  * @bh:			pointer to the buffer head to store the block
264ac27a0ecSDave Kleikamp  *			group descriptor
265ac27a0ecSDave Kleikamp  */
ext4_get_group_desc(struct super_block * sb,ext4_group_t block_group,struct buffer_head ** bh)266617ba13bSMingming Cao struct ext4_group_desc * ext4_get_group_desc(struct super_block *sb,
267fd2d4291SAvantika Mathur 					     ext4_group_t block_group,
268ac27a0ecSDave Kleikamp 					     struct buffer_head **bh)
269ac27a0ecSDave Kleikamp {
270498e5f24STheodore Ts'o 	unsigned int group_desc;
271498e5f24STheodore Ts'o 	unsigned int offset;
2728df9675fSTheodore Ts'o 	ext4_group_t ngroups = ext4_get_groups_count(sb);
273617ba13bSMingming Cao 	struct ext4_group_desc *desc;
274617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2751d0c3924STheodore Ts'o 	struct buffer_head *bh_p;
276ac27a0ecSDave Kleikamp 
2778df9675fSTheodore Ts'o 	if (block_group >= ngroups) {
27812062dddSEric Sandeen 		ext4_error(sb, "block_group >= groups_count - block_group = %u,"
27912062dddSEric Sandeen 			   " groups_count = %u", block_group, ngroups);
280ac27a0ecSDave Kleikamp 
281ac27a0ecSDave Kleikamp 		return NULL;
282ac27a0ecSDave Kleikamp 	}
283ac27a0ecSDave Kleikamp 
284617ba13bSMingming Cao 	group_desc = block_group >> EXT4_DESC_PER_BLOCK_BITS(sb);
285617ba13bSMingming Cao 	offset = block_group & (EXT4_DESC_PER_BLOCK(sb) - 1);
2861d0c3924STheodore Ts'o 	bh_p = sbi_array_rcu_deref(sbi, s_group_desc, group_desc);
2871d0c3924STheodore Ts'o 	/*
2881d0c3924STheodore Ts'o 	 * sbi_array_rcu_deref returns with rcu unlocked, this is ok since
2891d0c3924STheodore Ts'o 	 * the pointer being dereferenced won't be dereferenced again. By
2901d0c3924STheodore Ts'o 	 * looking at the usage in add_new_gdb() the value isn't modified,
2911d0c3924STheodore Ts'o 	 * just the pointer, and so it remains valid.
2921d0c3924STheodore Ts'o 	 */
2931d0c3924STheodore Ts'o 	if (!bh_p) {
29412062dddSEric Sandeen 		ext4_error(sb, "Group descriptor not loaded - "
295498e5f24STheodore Ts'o 			   "block_group = %u, group_desc = %u, desc = %u",
296ac27a0ecSDave Kleikamp 			   block_group, group_desc, offset);
297ac27a0ecSDave Kleikamp 		return NULL;
298ac27a0ecSDave Kleikamp 	}
299ac27a0ecSDave Kleikamp 
3000d1ee42fSAlexandre Ratchov 	desc = (struct ext4_group_desc *)(
3011d0c3924STheodore Ts'o 		(__u8 *)bh_p->b_data +
3020d1ee42fSAlexandre Ratchov 		offset * EXT4_DESC_SIZE(sb));
303ac27a0ecSDave Kleikamp 	if (bh)
3041d0c3924STheodore Ts'o 		*bh = bh_p;
3050d1ee42fSAlexandre Ratchov 	return desc;
306ac27a0ecSDave Kleikamp }
307ac27a0ecSDave Kleikamp 
ext4_valid_block_bitmap_padding(struct super_block * sb,ext4_group_t block_group,struct buffer_head * bh)308fa08a7b6SYe Bin static ext4_fsblk_t ext4_valid_block_bitmap_padding(struct super_block *sb,
309fa08a7b6SYe Bin 						    ext4_group_t block_group,
310fa08a7b6SYe Bin 						    struct buffer_head *bh)
311fa08a7b6SYe Bin {
312fa08a7b6SYe Bin 	ext4_grpblk_t next_zero_bit;
313fa08a7b6SYe Bin 	unsigned long bitmap_size = sb->s_blocksize * 8;
314fa08a7b6SYe Bin 	unsigned int offset = num_clusters_in_group(sb, block_group);
315fa08a7b6SYe Bin 
316fa08a7b6SYe Bin 	if (bitmap_size <= offset)
317fa08a7b6SYe Bin 		return 0;
318fa08a7b6SYe Bin 
319fa08a7b6SYe Bin 	next_zero_bit = ext4_find_next_zero_bit(bh->b_data, bitmap_size, offset);
320fa08a7b6SYe Bin 
321fa08a7b6SYe Bin 	return (next_zero_bit < bitmap_size ? next_zero_bit : 0);
322fa08a7b6SYe Bin }
323fa08a7b6SYe Bin 
ext4_get_group_info(struct super_block * sb,ext4_group_t group)3245354b2afSTheodore Ts'o struct ext4_group_info *ext4_get_group_info(struct super_block *sb,
3255354b2afSTheodore Ts'o 					    ext4_group_t group)
3265354b2afSTheodore Ts'o {
3275354b2afSTheodore Ts'o 	struct ext4_group_info **grp_info;
3285354b2afSTheodore Ts'o 	long indexv, indexh;
3295354b2afSTheodore Ts'o 
330f451fd97SFabio M. De Francesco 	if (unlikely(group >= EXT4_SB(sb)->s_groups_count))
3315354b2afSTheodore Ts'o 		return NULL;
3325354b2afSTheodore Ts'o 	indexv = group >> (EXT4_DESC_PER_BLOCK_BITS(sb));
3335354b2afSTheodore Ts'o 	indexh = group & ((EXT4_DESC_PER_BLOCK(sb)) - 1);
3345354b2afSTheodore Ts'o 	grp_info = sbi_array_rcu_deref(EXT4_SB(sb), s_group_info, indexv);
3355354b2afSTheodore Ts'o 	return grp_info[indexh];
3365354b2afSTheodore Ts'o }
3375354b2afSTheodore Ts'o 
3387a4c5de2STheodore Ts'o /*
3397a4c5de2STheodore Ts'o  * Return the block number which was discovered to be invalid, or 0 if
3407a4c5de2STheodore Ts'o  * the block bitmap is valid.
3417a4c5de2STheodore Ts'o  */
ext4_valid_block_bitmap(struct super_block * sb,struct ext4_group_desc * desc,ext4_group_t block_group,struct buffer_head * bh)3427a4c5de2STheodore Ts'o static ext4_fsblk_t ext4_valid_block_bitmap(struct super_block *sb,
343abcb2947SAneesh Kumar K.V 					    struct ext4_group_desc *desc,
344dbde0abeSDarrick J. Wong 					    ext4_group_t block_group,
345abcb2947SAneesh Kumar K.V 					    struct buffer_head *bh)
346abcb2947SAneesh Kumar K.V {
347e674e5cbSDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
348abcb2947SAneesh Kumar K.V 	ext4_grpblk_t offset;
349abcb2947SAneesh Kumar K.V 	ext4_grpblk_t next_zero_bit;
35022be37acSLukas Czerner 	ext4_grpblk_t max_bit = EXT4_CLUSTERS_PER_GROUP(sb);
3517a4c5de2STheodore Ts'o 	ext4_fsblk_t blk;
352abcb2947SAneesh Kumar K.V 	ext4_fsblk_t group_first_block;
353abcb2947SAneesh Kumar K.V 
354e2b911c5SDarrick J. Wong 	if (ext4_has_feature_flex_bg(sb)) {
355abcb2947SAneesh Kumar K.V 		/* with FLEX_BG, the inode/block bitmaps and itable
356abcb2947SAneesh Kumar K.V 		 * blocks may not be in the group at all
357abcb2947SAneesh Kumar K.V 		 * so the bitmap validation will be skipped for those groups
358abcb2947SAneesh Kumar K.V 		 * or it has to also read the block group where the bitmaps
359abcb2947SAneesh Kumar K.V 		 * are located to verify they are set.
360abcb2947SAneesh Kumar K.V 		 */
3617a4c5de2STheodore Ts'o 		return 0;
362abcb2947SAneesh Kumar K.V 	}
363abcb2947SAneesh Kumar K.V 	group_first_block = ext4_group_first_block_no(sb, block_group);
364abcb2947SAneesh Kumar K.V 
365abcb2947SAneesh Kumar K.V 	/* check whether block bitmap block number is set */
3667a4c5de2STheodore Ts'o 	blk = ext4_block_bitmap(sb, desc);
3677a4c5de2STheodore Ts'o 	offset = blk - group_first_block;
36822be37acSLukas Czerner 	if (offset < 0 || EXT4_B2C(sbi, offset) >= max_bit ||
3697dac4a17STheodore Ts'o 	    !ext4_test_bit(EXT4_B2C(sbi, offset), bh->b_data))
370abcb2947SAneesh Kumar K.V 		/* bad block bitmap */
3717a4c5de2STheodore Ts'o 		return blk;
372abcb2947SAneesh Kumar K.V 
373abcb2947SAneesh Kumar K.V 	/* check whether the inode bitmap block number is set */
3747a4c5de2STheodore Ts'o 	blk = ext4_inode_bitmap(sb, desc);
3757a4c5de2STheodore Ts'o 	offset = blk - group_first_block;
37622be37acSLukas Czerner 	if (offset < 0 || EXT4_B2C(sbi, offset) >= max_bit ||
3777dac4a17STheodore Ts'o 	    !ext4_test_bit(EXT4_B2C(sbi, offset), bh->b_data))
378abcb2947SAneesh Kumar K.V 		/* bad block bitmap */
3797a4c5de2STheodore Ts'o 		return blk;
380abcb2947SAneesh Kumar K.V 
381abcb2947SAneesh Kumar K.V 	/* check whether the inode table block number is set */
3827a4c5de2STheodore Ts'o 	blk = ext4_inode_table(sb, desc);
3837a4c5de2STheodore Ts'o 	offset = blk - group_first_block;
38422be37acSLukas Czerner 	if (offset < 0 || EXT4_B2C(sbi, offset) >= max_bit ||
3853d61ef10SKemeng Shi 	    EXT4_B2C(sbi, offset + sbi->s_itb_per_group - 1) >= max_bit)
3867dac4a17STheodore Ts'o 		return blk;
387abcb2947SAneesh Kumar K.V 	next_zero_bit = ext4_find_next_zero_bit(bh->b_data,
3883d61ef10SKemeng Shi 			EXT4_B2C(sbi, offset + sbi->s_itb_per_group - 1) + 1,
389e674e5cbSDarrick J. Wong 			EXT4_B2C(sbi, offset));
390e674e5cbSDarrick J. Wong 	if (next_zero_bit <
3913d61ef10SKemeng Shi 	    EXT4_B2C(sbi, offset + sbi->s_itb_per_group - 1) + 1)
3927a4c5de2STheodore Ts'o 		/* bad bitmap for inode tables */
3937a4c5de2STheodore Ts'o 		return blk;
394abcb2947SAneesh Kumar K.V 	return 0;
395abcb2947SAneesh Kumar K.V }
396fa77dcfaSDarrick J. Wong 
ext4_validate_block_bitmap(struct super_block * sb,struct ext4_group_desc * desc,ext4_group_t block_group,struct buffer_head * bh)3979008a58eSDarrick J. Wong static int ext4_validate_block_bitmap(struct super_block *sb,
398fa77dcfaSDarrick J. Wong 				      struct ext4_group_desc *desc,
399dbde0abeSDarrick J. Wong 				      ext4_group_t block_group,
400fa77dcfaSDarrick J. Wong 				      struct buffer_head *bh)
401fa77dcfaSDarrick J. Wong {
4027a4c5de2STheodore Ts'o 	ext4_fsblk_t	blk;
4038016e29fSHarshad Shirwadkar 	struct ext4_group_info *grp;
4048016e29fSHarshad Shirwadkar 
4058016e29fSHarshad Shirwadkar 	if (EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY)
4068016e29fSHarshad Shirwadkar 		return 0;
4078016e29fSHarshad Shirwadkar 
4088016e29fSHarshad Shirwadkar 	grp = ext4_get_group_info(sb, block_group);
4097a4c5de2STheodore Ts'o 
4109008a58eSDarrick J. Wong 	if (buffer_verified(bh))
4119008a58eSDarrick J. Wong 		return 0;
4125354b2afSTheodore Ts'o 	if (!grp || EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
4139008a58eSDarrick J. Wong 		return -EFSCORRUPTED;
414fa77dcfaSDarrick J. Wong 
415fa77dcfaSDarrick J. Wong 	ext4_lock_group(sb, block_group);
4168d5a803cSTheodore Ts'o 	if (buffer_verified(bh))
4178d5a803cSTheodore Ts'o 		goto verified;
41882483dfeSKemeng Shi 	if (unlikely(!ext4_block_bitmap_csum_verify(sb, desc, bh) ||
41946f870d6STheodore Ts'o 		     ext4_simulate_fail(sb, EXT4_SIM_BBITMAP_CRC))) {
4209008a58eSDarrick J. Wong 		ext4_unlock_group(sb, block_group);
4219008a58eSDarrick J. Wong 		ext4_error(sb, "bg %u: bad block bitmap checksum", block_group);
422db79e6d1SWang Shilong 		ext4_mark_group_bitmap_corrupted(sb, block_group,
423db79e6d1SWang Shilong 					EXT4_GROUP_INFO_BBITMAP_CORRUPT);
4249008a58eSDarrick J. Wong 		return -EFSBADCRC;
4259008a58eSDarrick J. Wong 	}
4267a4c5de2STheodore Ts'o 	blk = ext4_valid_block_bitmap(sb, desc, block_group, bh);
4277a4c5de2STheodore Ts'o 	if (unlikely(blk != 0)) {
4287a4c5de2STheodore Ts'o 		ext4_unlock_group(sb, block_group);
4297a4c5de2STheodore Ts'o 		ext4_error(sb, "bg %u: block %llu: invalid block bitmap",
4307a4c5de2STheodore Ts'o 			   block_group, blk);
431db79e6d1SWang Shilong 		ext4_mark_group_bitmap_corrupted(sb, block_group,
432db79e6d1SWang Shilong 					EXT4_GROUP_INFO_BBITMAP_CORRUPT);
4339008a58eSDarrick J. Wong 		return -EFSCORRUPTED;
4347a4c5de2STheodore Ts'o 	}
435fa08a7b6SYe Bin 	blk = ext4_valid_block_bitmap_padding(sb, block_group, bh);
436fa08a7b6SYe Bin 	if (unlikely(blk != 0)) {
437fa08a7b6SYe Bin 		ext4_unlock_group(sb, block_group);
438fa08a7b6SYe Bin 		ext4_error(sb, "bg %u: block %llu: padding at end of block bitmap is not set",
439fa08a7b6SYe Bin 			   block_group, blk);
440fa08a7b6SYe Bin 		ext4_mark_group_bitmap_corrupted(sb, block_group,
441fa08a7b6SYe Bin 						 EXT4_GROUP_INFO_BBITMAP_CORRUPT);
442fa08a7b6SYe Bin 		return -EFSCORRUPTED;
443fa08a7b6SYe Bin 	}
444fa77dcfaSDarrick J. Wong 	set_buffer_verified(bh);
4458d5a803cSTheodore Ts'o verified:
446fa77dcfaSDarrick J. Wong 	ext4_unlock_group(sb, block_group);
4479008a58eSDarrick J. Wong 	return 0;
448fa77dcfaSDarrick J. Wong }
449fa77dcfaSDarrick J. Wong 
450ac27a0ecSDave Kleikamp /**
45115b49132SEryu Guan  * ext4_read_block_bitmap_nowait()
452ac27a0ecSDave Kleikamp  * @sb:			super block
453ac27a0ecSDave Kleikamp  * @block_group:	given block group
454919adbfeSTheodore Ts'o  * @ignore_locked:	ignore locked buffers
455ac27a0ecSDave Kleikamp  *
456abcb2947SAneesh Kumar K.V  * Read the bitmap for a given block_group,and validate the
457abcb2947SAneesh Kumar K.V  * bits for block/inode/inode tables are set in the bitmaps
458ac27a0ecSDave Kleikamp  *
4599033783cSJosh Triplett  * Return buffer_head on success or an ERR_PTR in case of failure.
460ac27a0ecSDave Kleikamp  */
461717d50e4SAndreas Dilger struct buffer_head *
ext4_read_block_bitmap_nowait(struct super_block * sb,ext4_group_t block_group,bool ignore_locked)462cfd73237SAlex Zhuravlev ext4_read_block_bitmap_nowait(struct super_block *sb, ext4_group_t block_group,
463cfd73237SAlex Zhuravlev 			      bool ignore_locked)
464ac27a0ecSDave Kleikamp {
465617ba13bSMingming Cao 	struct ext4_group_desc *desc;
4667dac4a17STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
467813e5727STheodore Ts'o 	struct buffer_head *bh;
4687c9e69faSAneesh Kumar K.V 	ext4_fsblk_t bitmap_blk;
4699008a58eSDarrick J. Wong 	int err;
470ac27a0ecSDave Kleikamp 
471617ba13bSMingming Cao 	desc = ext4_get_group_desc(sb, block_group, NULL);
472ac27a0ecSDave Kleikamp 	if (!desc)
4739008a58eSDarrick J. Wong 		return ERR_PTR(-EFSCORRUPTED);
4747c9e69faSAneesh Kumar K.V 	bitmap_blk = ext4_block_bitmap(sb, desc);
4757dac4a17STheodore Ts'o 	if ((bitmap_blk <= le32_to_cpu(sbi->s_es->s_first_data_block)) ||
4767dac4a17STheodore Ts'o 	    (bitmap_blk >= ext4_blocks_count(sbi->s_es))) {
4777dac4a17STheodore Ts'o 		ext4_error(sb, "Invalid block bitmap block %llu in "
4787dac4a17STheodore Ts'o 			   "block_group %u", bitmap_blk, block_group);
479736dedbbSWang Shilong 		ext4_mark_group_bitmap_corrupted(sb, block_group,
480736dedbbSWang Shilong 					EXT4_GROUP_INFO_BBITMAP_CORRUPT);
4817dac4a17STheodore Ts'o 		return ERR_PTR(-EFSCORRUPTED);
4827dac4a17STheodore Ts'o 	}
483717d50e4SAndreas Dilger 	bh = sb_getblk(sb, bitmap_blk);
484abcb2947SAneesh Kumar K.V 	if (unlikely(!bh)) {
4855ef2a699SWang Shilong 		ext4_warning(sb, "Cannot get buffer for block bitmap - "
486a9df9a49STheodore Ts'o 			     "block_group = %u, block_bitmap = %llu",
487e29d1cdeSEric Sandeen 			     block_group, bitmap_blk);
4889008a58eSDarrick J. Wong 		return ERR_PTR(-ENOMEM);
489abcb2947SAneesh Kumar K.V 	}
4902ccb5fb9SAneesh Kumar K.V 
491cfd73237SAlex Zhuravlev 	if (ignore_locked && buffer_locked(bh)) {
492cfd73237SAlex Zhuravlev 		/* buffer under IO already, return if called for prefetching */
493cfd73237SAlex Zhuravlev 		put_bh(bh);
494cfd73237SAlex Zhuravlev 		return NULL;
495cfd73237SAlex Zhuravlev 	}
496cfd73237SAlex Zhuravlev 
4972ccb5fb9SAneesh Kumar K.V 	if (bitmap_uptodate(bh))
498fa77dcfaSDarrick J. Wong 		goto verify;
499abcb2947SAneesh Kumar K.V 
500c806e68fSFrederic Bohe 	lock_buffer(bh);
5012ccb5fb9SAneesh Kumar K.V 	if (bitmap_uptodate(bh)) {
5022ccb5fb9SAneesh Kumar K.V 		unlock_buffer(bh);
503fa77dcfaSDarrick J. Wong 		goto verify;
5042ccb5fb9SAneesh Kumar K.V 	}
505955ce5f5SAneesh Kumar K.V 	ext4_lock_group(sb, block_group);
5068844618dSTheodore Ts'o 	if (ext4_has_group_desc_csum(sb) &&
5078844618dSTheodore Ts'o 	    (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
5088844618dSTheodore Ts'o 		if (block_group == 0) {
5098844618dSTheodore Ts'o 			ext4_unlock_group(sb, block_group);
5108844618dSTheodore Ts'o 			unlock_buffer(bh);
5118844618dSTheodore Ts'o 			ext4_error(sb, "Block bitmap for bg 0 marked "
5128844618dSTheodore Ts'o 				   "uninitialized");
5138844618dSTheodore Ts'o 			err = -EFSCORRUPTED;
5148844618dSTheodore Ts'o 			goto out;
5158844618dSTheodore Ts'o 		}
516aef4885aSDmitry Monakhov 		err = ext4_init_block_bitmap(sb, bh, block_group, desc);
517b5aa06bfSKemeng Shi 		if (err) {
518b5aa06bfSKemeng Shi 			ext4_unlock_group(sb, block_group);
519b5aa06bfSKemeng Shi 			unlock_buffer(bh);
520b5aa06bfSKemeng Shi 			ext4_error(sb, "Failed to init block bitmap for group "
521b5aa06bfSKemeng Shi 				   "%u: %d", block_group, err);
522b5aa06bfSKemeng Shi 			goto out;
523b5aa06bfSKemeng Shi 		}
5242ccb5fb9SAneesh Kumar K.V 		set_bitmap_uptodate(bh);
525abcb2947SAneesh Kumar K.V 		set_buffer_uptodate(bh);
526044e6e3dSTheodore Ts'o 		set_buffer_verified(bh);
527955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, block_group);
5283300bedaSAneesh Kumar K.V 		unlock_buffer(bh);
529cefa74d0SKemeng Shi 		return bh;
530abcb2947SAneesh Kumar K.V 	}
531955ce5f5SAneesh Kumar K.V 	ext4_unlock_group(sb, block_group);
5322ccb5fb9SAneesh Kumar K.V 	if (buffer_uptodate(bh)) {
5332ccb5fb9SAneesh Kumar K.V 		/*
5342ccb5fb9SAneesh Kumar K.V 		 * if not uninit if bh is uptodate,
5352ccb5fb9SAneesh Kumar K.V 		 * bitmap is also uptodate
5362ccb5fb9SAneesh Kumar K.V 		 */
5372ccb5fb9SAneesh Kumar K.V 		set_bitmap_uptodate(bh);
5382ccb5fb9SAneesh Kumar K.V 		unlock_buffer(bh);
539fa77dcfaSDarrick J. Wong 		goto verify;
5402ccb5fb9SAneesh Kumar K.V 	}
5412ccb5fb9SAneesh Kumar K.V 	/*
542813e5727STheodore Ts'o 	 * submit the buffer_head for reading
5432ccb5fb9SAneesh Kumar K.V 	 */
544813e5727STheodore Ts'o 	set_buffer_new(bh);
545ab74c7b2STheodore Ts'o 	trace_ext4_read_block_bitmap_load(sb, block_group, ignore_locked);
5462d069c08Szhangyi (F) 	ext4_read_bh_nowait(bh, REQ_META | REQ_PRIO |
5472d069c08Szhangyi (F) 			    (ignore_locked ? REQ_RAHEAD : 0),
548*77035e4dSLong Li 			    ext4_end_bitmap_read,
549*77035e4dSLong Li 			    ext4_simulate_fail(sb, EXT4_SIM_BBITMAP_EIO));
550813e5727STheodore Ts'o 	return bh;
551fa77dcfaSDarrick J. Wong verify:
5529008a58eSDarrick J. Wong 	err = ext4_validate_block_bitmap(sb, desc, block_group, bh);
5539008a58eSDarrick J. Wong 	if (err)
5549008a58eSDarrick J. Wong 		goto out;
555fa77dcfaSDarrick J. Wong 	return bh;
5569008a58eSDarrick J. Wong out:
55748d9eb97SDarrick J. Wong 	put_bh(bh);
5589008a58eSDarrick J. Wong 	return ERR_PTR(err);
559813e5727STheodore Ts'o }
560813e5727STheodore Ts'o 
5619033783cSJosh Triplett /* Returns 0 on success, -errno on error */
ext4_wait_block_bitmap(struct super_block * sb,ext4_group_t block_group,struct buffer_head * bh)562813e5727STheodore Ts'o int ext4_wait_block_bitmap(struct super_block *sb, ext4_group_t block_group,
563813e5727STheodore Ts'o 			   struct buffer_head *bh)
564813e5727STheodore Ts'o {
565813e5727STheodore Ts'o 	struct ext4_group_desc *desc;
566813e5727STheodore Ts'o 
567813e5727STheodore Ts'o 	if (!buffer_new(bh))
568813e5727STheodore Ts'o 		return 0;
569813e5727STheodore Ts'o 	desc = ext4_get_group_desc(sb, block_group, NULL);
570813e5727STheodore Ts'o 	if (!desc)
5719008a58eSDarrick J. Wong 		return -EFSCORRUPTED;
572813e5727STheodore Ts'o 	wait_on_buffer(bh);
573813e5727STheodore Ts'o 	if (!buffer_uptodate(bh)) {
57454d3adbcSTheodore Ts'o 		ext4_error_err(sb, EIO, "Cannot read block bitmap - "
575a9df9a49STheodore Ts'o 			       "block_group = %u, block_bitmap = %llu",
576d4dc462fSHeiko Carstens 			       block_group, (unsigned long long) bh->b_blocknr);
577736dedbbSWang Shilong 		ext4_mark_group_bitmap_corrupted(sb, block_group,
578736dedbbSWang Shilong 					EXT4_GROUP_INFO_BBITMAP_CORRUPT);
5799008a58eSDarrick J. Wong 		return -EIO;
580813e5727STheodore Ts'o 	}
581813e5727STheodore Ts'o 	clear_buffer_new(bh);
582813e5727STheodore Ts'o 	/* Panic or remount fs read-only if block bitmap is invalid */
5839008a58eSDarrick J. Wong 	return ext4_validate_block_bitmap(sb, desc, block_group, bh);
584813e5727STheodore Ts'o }
585813e5727STheodore Ts'o 
586813e5727STheodore Ts'o struct buffer_head *
ext4_read_block_bitmap(struct super_block * sb,ext4_group_t block_group)587813e5727STheodore Ts'o ext4_read_block_bitmap(struct super_block *sb, ext4_group_t block_group)
588813e5727STheodore Ts'o {
589813e5727STheodore Ts'o 	struct buffer_head *bh;
5909008a58eSDarrick J. Wong 	int err;
591813e5727STheodore Ts'o 
592cfd73237SAlex Zhuravlev 	bh = ext4_read_block_bitmap_nowait(sb, block_group, false);
5939008a58eSDarrick J. Wong 	if (IS_ERR(bh))
5949008a58eSDarrick J. Wong 		return bh;
5959008a58eSDarrick J. Wong 	err = ext4_wait_block_bitmap(sb, block_group, bh);
5969008a58eSDarrick J. Wong 	if (err) {
597813e5727STheodore Ts'o 		put_bh(bh);
5989008a58eSDarrick J. Wong 		return ERR_PTR(err);
599abcb2947SAneesh Kumar K.V 	}
600ac27a0ecSDave Kleikamp 	return bh;
601ac27a0ecSDave Kleikamp }
602ac27a0ecSDave Kleikamp 
603ac27a0ecSDave Kleikamp /**
604df55c99dSTheodore Ts'o  * ext4_has_free_clusters()
6058c3bf8a0SEric Sandeen  * @sbi:	in-core super block structure.
606df55c99dSTheodore Ts'o  * @nclusters:	number of needed blocks
607df55c99dSTheodore Ts'o  * @flags:	flags from ext4_mb_new_blocks()
6088c3bf8a0SEric Sandeen  *
609df55c99dSTheodore Ts'o  * Check if filesystem has nclusters free & available for allocation.
6108c3bf8a0SEric Sandeen  * On success return 1, return 0 on failure.
6118c3bf8a0SEric Sandeen  */
ext4_has_free_clusters(struct ext4_sb_info * sbi,s64 nclusters,unsigned int flags)612df55c99dSTheodore Ts'o static int ext4_has_free_clusters(struct ext4_sb_info *sbi,
613df55c99dSTheodore Ts'o 				  s64 nclusters, unsigned int flags)
614a30d542aSAneesh Kumar K.V {
61527dd4385SLukas Czerner 	s64 free_clusters, dirty_clusters, rsv, resv_clusters;
61657042651STheodore Ts'o 	struct percpu_counter *fcc = &sbi->s_freeclusters_counter;
617df55c99dSTheodore Ts'o 	struct percpu_counter *dcc = &sbi->s_dirtyclusters_counter;
618a30d542aSAneesh Kumar K.V 
619df55c99dSTheodore Ts'o 	free_clusters  = percpu_counter_read_positive(fcc);
620df55c99dSTheodore Ts'o 	dirty_clusters = percpu_counter_read_positive(dcc);
62127dd4385SLukas Czerner 	resv_clusters = atomic64_read(&sbi->s_resv_clusters);
622304e220fSLukas Czerner 
623304e220fSLukas Czerner 	/*
624304e220fSLukas Czerner 	 * r_blocks_count should always be multiple of the cluster ratio so
625304e220fSLukas Czerner 	 * we are safe to do a plane bit shift only.
626304e220fSLukas Czerner 	 */
62727dd4385SLukas Czerner 	rsv = (ext4_r_blocks_count(sbi->s_es) >> sbi->s_cluster_bits) +
62827dd4385SLukas Czerner 	      resv_clusters;
629a30d542aSAneesh Kumar K.V 
63027dd4385SLukas Czerner 	if (free_clusters - (nclusters + rsv + dirty_clusters) <
631df55c99dSTheodore Ts'o 					EXT4_FREECLUSTERS_WATERMARK) {
632304e220fSLukas Czerner 		free_clusters  = percpu_counter_sum_positive(fcc);
633df55c99dSTheodore Ts'o 		dirty_clusters = percpu_counter_sum_positive(dcc);
6346bc6e63fSAneesh Kumar K.V 	}
635df55c99dSTheodore Ts'o 	/* Check whether we have space after accounting for current
636df55c99dSTheodore Ts'o 	 * dirty clusters & root reserved clusters.
6376bc6e63fSAneesh Kumar K.V 	 */
63827dd4385SLukas Czerner 	if (free_clusters >= (rsv + nclusters + dirty_clusters))
6398c3bf8a0SEric Sandeen 		return 1;
640a996031cSEric Sandeen 
641df55c99dSTheodore Ts'o 	/* Hm, nope.  Are (enough) root reserved clusters available? */
64208cefc7aSEric W. Biederman 	if (uid_eq(sbi->s_resuid, current_fsuid()) ||
64308cefc7aSEric W. Biederman 	    (!gid_eq(sbi->s_resgid, GLOBAL_ROOT_GID) && in_group_p(sbi->s_resgid)) ||
64455f020dbSAllison Henderson 	    capable(CAP_SYS_RESOURCE) ||
64555f020dbSAllison Henderson 	    (flags & EXT4_MB_USE_ROOT_BLOCKS)) {
64655f020dbSAllison Henderson 
64727dd4385SLukas Czerner 		if (free_clusters >= (nclusters + dirty_clusters +
64827dd4385SLukas Czerner 				      resv_clusters))
64927dd4385SLukas Czerner 			return 1;
65027dd4385SLukas Czerner 	}
65127dd4385SLukas Czerner 	/* No free blocks. Let's see if we can dip into reserved pool */
65227dd4385SLukas Czerner 	if (flags & EXT4_MB_USE_RESERVED) {
653df55c99dSTheodore Ts'o 		if (free_clusters >= (nclusters + dirty_clusters))
654a996031cSEric Sandeen 			return 1;
655a996031cSEric Sandeen 	}
656a996031cSEric Sandeen 
657a996031cSEric Sandeen 	return 0;
658a30d542aSAneesh Kumar K.V }
659a30d542aSAneesh Kumar K.V 
ext4_claim_free_clusters(struct ext4_sb_info * sbi,s64 nclusters,unsigned int flags)660e7d5f315STheodore Ts'o int ext4_claim_free_clusters(struct ext4_sb_info *sbi,
661e7d5f315STheodore Ts'o 			     s64 nclusters, unsigned int flags)
662ac27a0ecSDave Kleikamp {
663df55c99dSTheodore Ts'o 	if (ext4_has_free_clusters(sbi, nclusters, flags)) {
664e7d5f315STheodore Ts'o 		percpu_counter_add(&sbi->s_dirtyclusters_counter, nclusters);
66516eb7295SAneesh Kumar K.V 		return 0;
6668c3bf8a0SEric Sandeen 	} else
6678c3bf8a0SEric Sandeen 		return -ENOSPC;
668ac27a0ecSDave Kleikamp }
66907031431SMingming Cao 
670ac27a0ecSDave Kleikamp /**
671c60990b3STheodore Ts'o  * ext4_should_retry_alloc() - check if a block allocation should be retried
672ac27a0ecSDave Kleikamp  * @sb:			superblock
673efc61345SEric Whitney  * @retries:		number of retry attempts made so far
674ac27a0ecSDave Kleikamp  *
675efc61345SEric Whitney  * ext4_should_retry_alloc() is called when ENOSPC is returned while
676efc61345SEric Whitney  * attempting to allocate blocks.  If there's an indication that a pending
677efc61345SEric Whitney  * journal transaction might free some space and allow another attempt to
678efc61345SEric Whitney  * succeed, this function will wait for the current or committing transaction
679efc61345SEric Whitney  * to complete and then return TRUE.
680ac27a0ecSDave Kleikamp  */
ext4_should_retry_alloc(struct super_block * sb,int * retries)681617ba13bSMingming Cao int ext4_should_retry_alloc(struct super_block *sb, int *retries)
682ac27a0ecSDave Kleikamp {
683efc61345SEric Whitney 	struct ext4_sb_info *sbi = EXT4_SB(sb);
684efc61345SEric Whitney 
685efc61345SEric Whitney 	if (!sbi->s_journal)
686ac27a0ecSDave Kleikamp 		return 0;
687ac27a0ecSDave Kleikamp 
688efc61345SEric Whitney 	if (++(*retries) > 3) {
689efc61345SEric Whitney 		percpu_counter_inc(&sbi->s_sra_exceeded_retry_limit);
690efc61345SEric Whitney 		return 0;
691efc61345SEric Whitney 	}
692efc61345SEric Whitney 
693efc61345SEric Whitney 	/*
694efc61345SEric Whitney 	 * if there's no indication that blocks are about to be freed it's
695efc61345SEric Whitney 	 * possible we just missed a transaction commit that did so
696efc61345SEric Whitney 	 */
697d08854f5STheodore Ts'o 	smp_mb();
6985036ab8dSWang Jianchao 	if (sbi->s_mb_free_pending == 0) {
6995036ab8dSWang Jianchao 		if (test_opt(sb, DISCARD)) {
7005036ab8dSWang Jianchao 			atomic_inc(&sbi->s_retry_alloc_pending);
7015036ab8dSWang Jianchao 			flush_work(&sbi->s_discard_work);
7025036ab8dSWang Jianchao 			atomic_dec(&sbi->s_retry_alloc_pending);
7035036ab8dSWang Jianchao 		}
704efc61345SEric Whitney 		return ext4_has_free_clusters(sbi, 1, 0);
7055036ab8dSWang Jianchao 	}
70668fd9750STheodore Ts'o 
707efc61345SEric Whitney 	/*
708efc61345SEric Whitney 	 * it's possible we've just missed a transaction commit here,
709efc61345SEric Whitney 	 * so ignore the returned status
710efc61345SEric Whitney 	 */
7114978c659SJan Kara 	ext4_debug("%s: retrying operation after ENOSPC\n", sb->s_id);
712efc61345SEric Whitney 	(void) jbd2_journal_force_commit_nested(sbi->s_journal);
713dbc427ceSJan Kara 	return 1;
714ac27a0ecSDave Kleikamp }
715ac27a0ecSDave Kleikamp 
716654b4908SAneesh Kumar K.V /*
717654b4908SAneesh Kumar K.V  * ext4_new_meta_blocks() -- allocate block for meta data (indexing) blocks
718654b4908SAneesh Kumar K.V  *
719654b4908SAneesh Kumar K.V  * @handle:             handle to this transaction
720654b4908SAneesh Kumar K.V  * @inode:              file inode
721654b4908SAneesh Kumar K.V  * @goal:               given target block(filesystem wide)
7227b415bf6SAditya Kali  * @count:		pointer to total number of clusters needed
723654b4908SAneesh Kumar K.V  * @errp:               error code
724654b4908SAneesh Kumar K.V  *
725654b4908SAneesh Kumar K.V  * Return 1st allocated block number on success, *count stores total account
726654b4908SAneesh Kumar K.V  * error stores in errp pointer
727654b4908SAneesh Kumar K.V  */
ext4_new_meta_blocks(handle_t * handle,struct inode * inode,ext4_fsblk_t goal,unsigned int flags,unsigned long * count,int * errp)728654b4908SAneesh Kumar K.V ext4_fsblk_t ext4_new_meta_blocks(handle_t *handle, struct inode *inode,
72955f020dbSAllison Henderson 				  ext4_fsblk_t goal, unsigned int flags,
73055f020dbSAllison Henderson 				  unsigned long *count, int *errp)
731654b4908SAneesh Kumar K.V {
73297df5d15STheodore Ts'o 	struct ext4_allocation_request ar;
733d2a17637SMingming Cao 	ext4_fsblk_t ret;
73497df5d15STheodore Ts'o 
73597df5d15STheodore Ts'o 	memset(&ar, 0, sizeof(ar));
73697df5d15STheodore Ts'o 	/* Fill with neighbour allocated blocks */
73797df5d15STheodore Ts'o 	ar.inode = inode;
73897df5d15STheodore Ts'o 	ar.goal = goal;
73997df5d15STheodore Ts'o 	ar.len = count ? *count : 1;
74055f020dbSAllison Henderson 	ar.flags = flags;
74197df5d15STheodore Ts'o 
74297df5d15STheodore Ts'o 	ret = ext4_mb_new_blocks(handle, &ar, errp);
74397df5d15STheodore Ts'o 	if (count)
74497df5d15STheodore Ts'o 		*count = ar.len;
745d2a17637SMingming Cao 	/*
74672b8ab9dSEric Sandeen 	 * Account for the allocated meta blocks.  We will never
74772b8ab9dSEric Sandeen 	 * fail EDQUOT for metdata, but we do account for it.
748d2a17637SMingming Cao 	 */
749e3cf5d5dSTheodore Ts'o 	if (!(*errp) && (flags & EXT4_MB_DELALLOC_RESERVED)) {
7507b415bf6SAditya Kali 		dquot_alloc_block_nofail(inode,
7517b415bf6SAditya Kali 				EXT4_C2B(EXT4_SB(inode->i_sb), ar.len));
752d2a17637SMingming Cao 	}
753d2a17637SMingming Cao 	return ret;
754d2a17637SMingming Cao }
755d2a17637SMingming Cao 
756ac27a0ecSDave Kleikamp /**
7575dee5437STheodore Ts'o  * ext4_count_free_clusters() -- count filesystem free clusters
758ac27a0ecSDave Kleikamp  * @sb:		superblock
759ac27a0ecSDave Kleikamp  *
7605dee5437STheodore Ts'o  * Adds up the number of free clusters from each block group.
761ac27a0ecSDave Kleikamp  */
ext4_count_free_clusters(struct super_block * sb)7625dee5437STheodore Ts'o ext4_fsblk_t ext4_count_free_clusters(struct super_block *sb)
763ac27a0ecSDave Kleikamp {
764617ba13bSMingming Cao 	ext4_fsblk_t desc_count;
765617ba13bSMingming Cao 	struct ext4_group_desc *gdp;
766fd2d4291SAvantika Mathur 	ext4_group_t i;
7678df9675fSTheodore Ts'o 	ext4_group_t ngroups = ext4_get_groups_count(sb);
7682746f7a1SDarrick J. Wong 	struct ext4_group_info *grp;
769617ba13bSMingming Cao #ifdef EXT4FS_DEBUG
770617ba13bSMingming Cao 	struct ext4_super_block *es;
771617ba13bSMingming Cao 	ext4_fsblk_t bitmap_count;
772498e5f24STheodore Ts'o 	unsigned int x;
773ac27a0ecSDave Kleikamp 	struct buffer_head *bitmap_bh = NULL;
774ac27a0ecSDave Kleikamp 
775617ba13bSMingming Cao 	es = EXT4_SB(sb)->s_es;
776ac27a0ecSDave Kleikamp 	desc_count = 0;
777ac27a0ecSDave Kleikamp 	bitmap_count = 0;
778ac27a0ecSDave Kleikamp 	gdp = NULL;
779ac27a0ecSDave Kleikamp 
780ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
781617ba13bSMingming Cao 		gdp = ext4_get_group_desc(sb, i, NULL);
782ac27a0ecSDave Kleikamp 		if (!gdp)
783ac27a0ecSDave Kleikamp 			continue;
7842746f7a1SDarrick J. Wong 		grp = NULL;
7852746f7a1SDarrick J. Wong 		if (EXT4_SB(sb)->s_group_info)
7862746f7a1SDarrick J. Wong 			grp = ext4_get_group_info(sb, i);
7872746f7a1SDarrick J. Wong 		if (!grp || !EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
788021b65bbSTheodore Ts'o 			desc_count += ext4_free_group_clusters(sb, gdp);
789ac27a0ecSDave Kleikamp 		brelse(bitmap_bh);
790574ca174STheodore Ts'o 		bitmap_bh = ext4_read_block_bitmap(sb, i);
7919008a58eSDarrick J. Wong 		if (IS_ERR(bitmap_bh)) {
7929008a58eSDarrick J. Wong 			bitmap_bh = NULL;
793ac27a0ecSDave Kleikamp 			continue;
7949008a58eSDarrick J. Wong 		}
795ac27a0ecSDave Kleikamp 
796f6fb99caSTheodore Ts'o 		x = ext4_count_free(bitmap_bh->b_data,
797036acea2SAzat Khuzhin 				    EXT4_CLUSTERS_PER_GROUP(sb) / 8);
7989fd9784cSThadeu Lima de Souza Cascardo 		printk(KERN_DEBUG "group %u: stored = %d, counted = %u\n",
799021b65bbSTheodore Ts'o 			i, ext4_free_group_clusters(sb, gdp), x);
800ac27a0ecSDave Kleikamp 		bitmap_count += x;
801ac27a0ecSDave Kleikamp 	}
802ac27a0ecSDave Kleikamp 	brelse(bitmap_bh);
8035dee5437STheodore Ts'o 	printk(KERN_DEBUG "ext4_count_free_clusters: stored = %llu"
8045dee5437STheodore Ts'o 	       ", computed = %llu, %llu\n",
805810da240SLukas Czerner 	       EXT4_NUM_B2C(EXT4_SB(sb), ext4_free_blocks_count(es)),
806ac27a0ecSDave Kleikamp 	       desc_count, bitmap_count);
807ac27a0ecSDave Kleikamp 	return bitmap_count;
808ac27a0ecSDave Kleikamp #else
809ac27a0ecSDave Kleikamp 	desc_count = 0;
810ac27a0ecSDave Kleikamp 	for (i = 0; i < ngroups; i++) {
811617ba13bSMingming Cao 		gdp = ext4_get_group_desc(sb, i, NULL);
812ac27a0ecSDave Kleikamp 		if (!gdp)
813ac27a0ecSDave Kleikamp 			continue;
8142746f7a1SDarrick J. Wong 		grp = NULL;
8152746f7a1SDarrick J. Wong 		if (EXT4_SB(sb)->s_group_info)
8162746f7a1SDarrick J. Wong 			grp = ext4_get_group_info(sb, i);
8172746f7a1SDarrick J. Wong 		if (!grp || !EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
818021b65bbSTheodore Ts'o 			desc_count += ext4_free_group_clusters(sb, gdp);
819ac27a0ecSDave Kleikamp 	}
820ac27a0ecSDave Kleikamp 
821ac27a0ecSDave Kleikamp 	return desc_count;
822ac27a0ecSDave Kleikamp #endif
823ac27a0ecSDave Kleikamp }
824ac27a0ecSDave Kleikamp 
test_root(ext4_group_t a,int b)825fd2d4291SAvantika Mathur static inline int test_root(ext4_group_t a, int b)
826ac27a0ecSDave Kleikamp {
827f4afb4f4STheodore Ts'o 	while (1) {
828f4afb4f4STheodore Ts'o 		if (a < b)
829f4afb4f4STheodore Ts'o 			return 0;
830f4afb4f4STheodore Ts'o 		if (a == b)
831f4afb4f4STheodore Ts'o 			return 1;
832f4afb4f4STheodore Ts'o 		if ((a % b) != 0)
833f4afb4f4STheodore Ts'o 			return 0;
834f4afb4f4STheodore Ts'o 		a = a / b;
835f4afb4f4STheodore Ts'o 	}
836ac27a0ecSDave Kleikamp }
837ac27a0ecSDave Kleikamp 
838ac27a0ecSDave Kleikamp /**
839617ba13bSMingming Cao  *	ext4_bg_has_super - number of blocks used by the superblock in group
840ac27a0ecSDave Kleikamp  *	@sb: superblock for filesystem
841ac27a0ecSDave Kleikamp  *	@group: group number to check
842ac27a0ecSDave Kleikamp  *
843ac27a0ecSDave Kleikamp  *	Return the number of blocks used by the superblock (primary or backup)
844ac27a0ecSDave Kleikamp  *	in this group.  Currently this will be only 0 or 1.
845ac27a0ecSDave Kleikamp  */
ext4_bg_has_super(struct super_block * sb,ext4_group_t group)846fd2d4291SAvantika Mathur int ext4_bg_has_super(struct super_block *sb, ext4_group_t group)
847ac27a0ecSDave Kleikamp {
8481beeef1bSDarrick J. Wong 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
8491beeef1bSDarrick J. Wong 
8501beeef1bSDarrick J. Wong 	if (group == 0)
851ac27a0ecSDave Kleikamp 		return 1;
852e2b911c5SDarrick J. Wong 	if (ext4_has_feature_sparse_super2(sb)) {
8531beeef1bSDarrick J. Wong 		if (group == le32_to_cpu(es->s_backup_bgs[0]) ||
8541beeef1bSDarrick J. Wong 		    group == le32_to_cpu(es->s_backup_bgs[1]))
8551beeef1bSDarrick J. Wong 			return 1;
8561beeef1bSDarrick J. Wong 		return 0;
8571beeef1bSDarrick J. Wong 	}
858e2b911c5SDarrick J. Wong 	if ((group <= 1) || !ext4_has_feature_sparse_super(sb))
8591beeef1bSDarrick J. Wong 		return 1;
8601beeef1bSDarrick J. Wong 	if (!(group & 1))
8611beeef1bSDarrick J. Wong 		return 0;
8621beeef1bSDarrick J. Wong 	if (test_root(group, 3) || (test_root(group, 5)) ||
8631beeef1bSDarrick J. Wong 	    test_root(group, 7))
8641beeef1bSDarrick J. Wong 		return 1;
8651beeef1bSDarrick J. Wong 
8661beeef1bSDarrick J. Wong 	return 0;
867ac27a0ecSDave Kleikamp }
868ac27a0ecSDave Kleikamp 
ext4_bg_num_gdb_meta(struct super_block * sb,ext4_group_t group)869fd2d4291SAvantika Mathur static unsigned long ext4_bg_num_gdb_meta(struct super_block *sb,
870fd2d4291SAvantika Mathur 					ext4_group_t group)
871ac27a0ecSDave Kleikamp {
872617ba13bSMingming Cao 	unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb);
873fd2d4291SAvantika Mathur 	ext4_group_t first = metagroup * EXT4_DESC_PER_BLOCK(sb);
874fd2d4291SAvantika Mathur 	ext4_group_t last = first + EXT4_DESC_PER_BLOCK(sb) - 1;
875ac27a0ecSDave Kleikamp 
876ac27a0ecSDave Kleikamp 	if (group == first || group == first + 1 || group == last)
877ac27a0ecSDave Kleikamp 		return 1;
878ac27a0ecSDave Kleikamp 	return 0;
879ac27a0ecSDave Kleikamp }
880ac27a0ecSDave Kleikamp 
ext4_bg_num_gdb_nometa(struct super_block * sb,ext4_group_t group)881fd2d4291SAvantika Mathur static unsigned long ext4_bg_num_gdb_nometa(struct super_block *sb,
882fd2d4291SAvantika Mathur 					ext4_group_t group)
883ac27a0ecSDave Kleikamp {
8848dadb198STheodore Ts'o 	if (!ext4_bg_has_super(sb, group))
8858dadb198STheodore Ts'o 		return 0;
8868dadb198STheodore Ts'o 
88719482792SKemeng Shi 	if (ext4_has_feature_meta_bg(sb))
88819482792SKemeng Shi 		return le32_to_cpu(EXT4_SB(sb)->s_es->s_first_meta_bg);
88919482792SKemeng Shi 	else
8908dadb198STheodore Ts'o 		return EXT4_SB(sb)->s_gdb_count;
891ac27a0ecSDave Kleikamp }
892ac27a0ecSDave Kleikamp 
893ac27a0ecSDave Kleikamp /**
894617ba13bSMingming Cao  *	ext4_bg_num_gdb - number of blocks used by the group table in group
895ac27a0ecSDave Kleikamp  *	@sb: superblock for filesystem
896ac27a0ecSDave Kleikamp  *	@group: group number to check
897ac27a0ecSDave Kleikamp  *
898ac27a0ecSDave Kleikamp  *	Return the number of blocks used by the group descriptor table
899ac27a0ecSDave Kleikamp  *	(primary or backup) in this group.  In the future there may be a
900ac27a0ecSDave Kleikamp  *	different number of descriptor blocks in each group.
901ac27a0ecSDave Kleikamp  */
ext4_bg_num_gdb(struct super_block * sb,ext4_group_t group)902fd2d4291SAvantika Mathur unsigned long ext4_bg_num_gdb(struct super_block *sb, ext4_group_t group)
903ac27a0ecSDave Kleikamp {
904ac27a0ecSDave Kleikamp 	unsigned long first_meta_bg =
905617ba13bSMingming Cao 			le32_to_cpu(EXT4_SB(sb)->s_es->s_first_meta_bg);
906617ba13bSMingming Cao 	unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb);
907ac27a0ecSDave Kleikamp 
908e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_meta_bg(sb) || metagroup < first_meta_bg)
909617ba13bSMingming Cao 		return ext4_bg_num_gdb_nometa(sb, group);
910ac27a0ecSDave Kleikamp 
911617ba13bSMingming Cao 	return ext4_bg_num_gdb_meta(sb,group);
912ac27a0ecSDave Kleikamp 
913ac27a0ecSDave Kleikamp }
914c2ea3fdeSTheodore Ts'o 
91549f7f9afSTheodore Ts'o /*
91668228da5SWang Jianjian  * This function returns the number of file system metadata blocks at
91749f7f9afSTheodore Ts'o  * the beginning of a block group, including the reserved gdt blocks.
91849f7f9afSTheodore Ts'o  */
ext4_num_base_meta_blocks(struct super_block * sb,ext4_group_t block_group)91968228da5SWang Jianjian unsigned int ext4_num_base_meta_blocks(struct super_block *sb,
92049f7f9afSTheodore Ts'o 				       ext4_group_t block_group)
92149f7f9afSTheodore Ts'o {
92249f7f9afSTheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
923d5b8f310STheodore Ts'o 	unsigned num;
92449f7f9afSTheodore Ts'o 
92549f7f9afSTheodore Ts'o 	/* Check for superblock and gdt backups in this group */
92649f7f9afSTheodore Ts'o 	num = ext4_bg_has_super(sb, block_group);
92749f7f9afSTheodore Ts'o 
928e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_meta_bg(sb) ||
92949f7f9afSTheodore Ts'o 	    block_group < le32_to_cpu(sbi->s_es->s_first_meta_bg) *
93049f7f9afSTheodore Ts'o 			  sbi->s_desc_per_block) {
93149f7f9afSTheodore Ts'o 		if (num) {
932a38627f1SKemeng Shi 			num += ext4_bg_num_gdb_nometa(sb, block_group);
93349f7f9afSTheodore Ts'o 			num += le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks);
93449f7f9afSTheodore Ts'o 		}
93549f7f9afSTheodore Ts'o 	} else { /* For META_BG_BLOCK_GROUPS */
936a38627f1SKemeng Shi 		num += ext4_bg_num_gdb_meta(sb, block_group);
93749f7f9afSTheodore Ts'o 	}
93868228da5SWang Jianjian 	return num;
93949f7f9afSTheodore Ts'o }
94068228da5SWang Jianjian 
ext4_num_base_meta_clusters(struct super_block * sb,ext4_group_t block_group)94168228da5SWang Jianjian static unsigned int ext4_num_base_meta_clusters(struct super_block *sb,
94268228da5SWang Jianjian 						ext4_group_t block_group)
94368228da5SWang Jianjian {
94468228da5SWang Jianjian 	return EXT4_NUM_B2C(EXT4_SB(sb), ext4_num_base_meta_blocks(sb, block_group));
94568228da5SWang Jianjian }
94668228da5SWang Jianjian 
947f86186b4SEric Sandeen /**
948f86186b4SEric Sandeen  *	ext4_inode_to_goal_block - return a hint for block allocation
949f86186b4SEric Sandeen  *	@inode: inode for block allocation
950f86186b4SEric Sandeen  *
951f86186b4SEric Sandeen  *	Return the ideal location to start allocating blocks for a
952f86186b4SEric Sandeen  *	newly created inode.
953f86186b4SEric Sandeen  */
ext4_inode_to_goal_block(struct inode * inode)954f86186b4SEric Sandeen ext4_fsblk_t ext4_inode_to_goal_block(struct inode *inode)
955f86186b4SEric Sandeen {
956f86186b4SEric Sandeen 	struct ext4_inode_info *ei = EXT4_I(inode);
957f86186b4SEric Sandeen 	ext4_group_t block_group;
958f86186b4SEric Sandeen 	ext4_grpblk_t colour;
959f86186b4SEric Sandeen 	int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
960f86186b4SEric Sandeen 	ext4_fsblk_t bg_start;
961f86186b4SEric Sandeen 	ext4_fsblk_t last_block;
962f86186b4SEric Sandeen 
963f86186b4SEric Sandeen 	block_group = ei->i_block_group;
964f86186b4SEric Sandeen 	if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
965f86186b4SEric Sandeen 		/*
966f86186b4SEric Sandeen 		 * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
967f86186b4SEric Sandeen 		 * block groups per flexgroup, reserve the first block
968f86186b4SEric Sandeen 		 * group for directories and special files.  Regular
969f86186b4SEric Sandeen 		 * files will start at the second block group.  This
970f86186b4SEric Sandeen 		 * tends to speed up directory access and improves
971f86186b4SEric Sandeen 		 * fsck times.
972f86186b4SEric Sandeen 		 */
973f86186b4SEric Sandeen 		block_group &= ~(flex_size-1);
974f86186b4SEric Sandeen 		if (S_ISREG(inode->i_mode))
975f86186b4SEric Sandeen 			block_group++;
976f86186b4SEric Sandeen 	}
977f86186b4SEric Sandeen 	bg_start = ext4_group_first_block_no(inode->i_sb, block_group);
978f86186b4SEric Sandeen 	last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
979f86186b4SEric Sandeen 
980f86186b4SEric Sandeen 	/*
981f86186b4SEric Sandeen 	 * If we are doing delayed allocation, we don't need take
982f86186b4SEric Sandeen 	 * colour into account.
983f86186b4SEric Sandeen 	 */
984f86186b4SEric Sandeen 	if (test_opt(inode->i_sb, DELALLOC))
985f86186b4SEric Sandeen 		return bg_start;
986f86186b4SEric Sandeen 
987f86186b4SEric Sandeen 	if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
9889bee5779SRitesh Harjani 		colour = (task_pid_nr(current) % 16) *
989f86186b4SEric Sandeen 			(EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
990f86186b4SEric Sandeen 	else
9919bee5779SRitesh Harjani 		colour = (task_pid_nr(current) % 16) *
9929bee5779SRitesh Harjani 			((last_block - bg_start) / 16);
993f86186b4SEric Sandeen 	return bg_start + colour;
994f86186b4SEric Sandeen }
995f86186b4SEric Sandeen 
996