Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *  linux/fs/ext4/balloc.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 1992, 1993, 1994, 1995
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Remy Card (card@masi.ibp.fr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Laboratoire MASI - Institut Blaise Pascal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Universite Pierre et Marie Curie (Paris VI)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *  Enhanced block allocation by Stephen Tweedie (sct@redhat.com), 1993
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *  Big-endian to little-endian byte-swapping/bitmaps by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *        David S. Miller (davem@caip.rutgers.edu), 1995
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/quotaops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/buffer_head.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include "ext4.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include "ext4_jbd2.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include "mballoc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <trace/events/ext4.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static unsigned ext4_num_base_meta_clusters(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 					    ext4_group_t block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * balloc.c contains the blocks allocation and deallocation routines
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * Calculate block group number for a given block number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) ext4_group_t ext4_get_group_number(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 				   ext4_fsblk_t block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	ext4_group_t group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	if (test_opt2(sb, STD_GROUP_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		group = (block -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 			 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 			(EXT4_BLOCK_SIZE_BITS(sb) + EXT4_CLUSTER_BITS(sb) + 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		ext4_get_group_no_and_offset(sb, block, &group, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	return group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  * Calculate the block group number and offset into the block/cluster
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  * allocation bitmap, given a block number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) void ext4_get_group_no_and_offset(struct super_block *sb, ext4_fsblk_t blocknr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		ext4_group_t *blockgrpp, ext4_grpblk_t *offsetp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	ext4_grpblk_t offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	blocknr = blocknr - le32_to_cpu(es->s_first_data_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	offset = do_div(blocknr, EXT4_BLOCKS_PER_GROUP(sb)) >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		EXT4_SB(sb)->s_cluster_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	if (offsetp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		*offsetp = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	if (blockgrpp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		*blockgrpp = blocknr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70)  * Check whether the 'block' lives within the 'block_group'. Returns 1 if so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71)  * and 0 otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) static inline int ext4_block_in_group(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 				      ext4_fsblk_t block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 				      ext4_group_t block_group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	ext4_group_t actual_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	actual_group = ext4_get_group_number(sb, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	return (actual_group == block_group) ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) /* Return the number of clusters used for file system metadata; this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84)  * represents the overhead needed by the file system.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) static unsigned ext4_num_overhead_clusters(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 					   ext4_group_t block_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 					   struct ext4_group_desc *gdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	unsigned num_clusters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	int block_cluster = -1, inode_cluster = -1, itbl_cluster = -1, i, c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	ext4_fsblk_t start = ext4_group_first_block_no(sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	ext4_fsblk_t itbl_blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	struct ext4_sb_info *sbi = EXT4_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	/* This is the number of clusters used by the superblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	 * block group descriptors, and reserved block group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	 * descriptor blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	num_clusters = ext4_num_base_meta_clusters(sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	 * For the allocation bitmaps and inode table, we first need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	 * to check to see if the block is in the block group.  If it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	 * is, then check to see if the cluster is already accounted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	 * for in the clusters used for the base metadata cluster, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	 * if we can increment the base metadata cluster to include
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	 * that block.  Otherwise, we will have to track the cluster
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	 * used for the allocation bitmap or inode table explicitly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	 * Normally all of these blocks are contiguous, so the special
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	 * case handling shouldn't be necessary except for *very*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	 * unusual file system layouts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	if (ext4_block_in_group(sb, ext4_block_bitmap(sb, gdp), block_group)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		block_cluster = EXT4_B2C(sbi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 					 ext4_block_bitmap(sb, gdp) - start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		if (block_cluster < num_clusters)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 			block_cluster = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		else if (block_cluster == num_clusters) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 			num_clusters++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 			block_cluster = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	if (ext4_block_in_group(sb, ext4_inode_bitmap(sb, gdp), block_group)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		inode_cluster = EXT4_B2C(sbi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 					 ext4_inode_bitmap(sb, gdp) - start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		if (inode_cluster < num_clusters)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 			inode_cluster = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		else if (inode_cluster == num_clusters) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 			num_clusters++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 			inode_cluster = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	itbl_blk = ext4_inode_table(sb, gdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	for (i = 0; i < sbi->s_itb_per_group; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		if (ext4_block_in_group(sb, itbl_blk + i, block_group)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 			c = EXT4_B2C(sbi, itbl_blk + i - start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 			if ((c < num_clusters) || (c == inode_cluster) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 			    (c == block_cluster) || (c == itbl_cluster))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			if (c == num_clusters) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 				num_clusters++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			num_clusters++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			itbl_cluster = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	if (block_cluster != -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		num_clusters++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	if (inode_cluster != -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		num_clusters++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	return num_clusters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static unsigned int num_clusters_in_group(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 					  ext4_group_t block_group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	unsigned int blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	if (block_group == ext4_get_groups_count(sb) - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		 * Even though mke2fs always initializes the first and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		 * last group, just in case some other tool was used,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		 * we need to make sure we calculate the right free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		 * blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		blocks = ext4_blocks_count(EXT4_SB(sb)->s_es) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 			ext4_group_first_block_no(sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		blocks = EXT4_BLOCKS_PER_GROUP(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	return EXT4_NUM_B2C(EXT4_SB(sb), blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) /* Initializes an uninitialized block bitmap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) static int ext4_init_block_bitmap(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 				   struct buffer_head *bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 				   ext4_group_t block_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 				   struct ext4_group_desc *gdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	unsigned int bit, bit_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	struct ext4_sb_info *sbi = EXT4_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	ext4_fsblk_t start, tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	J_ASSERT_BH(bh, buffer_locked(bh));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	/* If checksum is bad mark all blocks used to prevent allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	 * essentially implementing a per-group read-only flag. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	if (!ext4_group_desc_csum_verify(sb, block_group, gdp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		ext4_mark_group_bitmap_corrupted(sb, block_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 					EXT4_GROUP_INFO_BBITMAP_CORRUPT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 					EXT4_GROUP_INFO_IBITMAP_CORRUPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		return -EFSBADCRC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	memset(bh->b_data, 0, sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	bit_max = ext4_num_base_meta_clusters(sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	if ((bit_max >> 3) >= bh->b_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	for (bit = 0; bit < bit_max; bit++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		ext4_set_bit(bit, bh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	start = ext4_group_first_block_no(sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	/* Set bits for block and inode bitmaps, and inode table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	tmp = ext4_block_bitmap(sb, gdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	if (ext4_block_in_group(sb, tmp, block_group))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		ext4_set_bit(EXT4_B2C(sbi, tmp - start), bh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	tmp = ext4_inode_bitmap(sb, gdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	if (ext4_block_in_group(sb, tmp, block_group))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		ext4_set_bit(EXT4_B2C(sbi, tmp - start), bh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	tmp = ext4_inode_table(sb, gdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	for (; tmp < ext4_inode_table(sb, gdp) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		     sbi->s_itb_per_group; tmp++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		if (ext4_block_in_group(sb, tmp, block_group))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 			ext4_set_bit(EXT4_B2C(sbi, tmp - start), bh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	 * Also if the number of blocks within the group is less than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	 * the blocksize * 8 ( which is the size of bitmap ), set rest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	 * of the block bitmap to 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	ext4_mark_bitmap_end(num_clusters_in_group(sb, block_group),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 			     sb->s_blocksize * 8, bh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) /* Return the number of free blocks in a block group.  It is used when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)  * the block bitmap is uninitialized, so we can't just count the bits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)  * in the bitmap. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) unsigned ext4_free_clusters_after_init(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 				       ext4_group_t block_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 				       struct ext4_group_desc *gdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	return num_clusters_in_group(sb, block_group) - 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		ext4_num_overhead_clusters(sb, block_group, gdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)  * The free blocks are managed by bitmaps.  A file system contains several
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)  * blocks groups.  Each group contains 1 bitmap block for blocks, 1 bitmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)  * block for inodes, N blocks for the inode table and data blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)  * The file system contains group descriptors which are located after the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)  * super block.  Each descriptor contains the number of the bitmap block and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)  * the free blocks count in the block.  The descriptors are loaded in memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)  * when a file system is mounted (see ext4_fill_super).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)  * ext4_get_group_desc() -- load group descriptor from disk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)  * @sb:			super block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)  * @block_group:	given block group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)  * @bh:			pointer to the buffer head to store the block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)  *			group descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) struct ext4_group_desc * ext4_get_group_desc(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 					     ext4_group_t block_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 					     struct buffer_head **bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	unsigned int group_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	unsigned int offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	ext4_group_t ngroups = ext4_get_groups_count(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	struct ext4_group_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	struct ext4_sb_info *sbi = EXT4_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	struct buffer_head *bh_p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	if (block_group >= ngroups) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		ext4_error(sb, "block_group >= groups_count - block_group = %u,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 			   " groups_count = %u", block_group, ngroups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	group_desc = block_group >> EXT4_DESC_PER_BLOCK_BITS(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	offset = block_group & (EXT4_DESC_PER_BLOCK(sb) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	bh_p = sbi_array_rcu_deref(sbi, s_group_desc, group_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	 * sbi_array_rcu_deref returns with rcu unlocked, this is ok since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	 * the pointer being dereferenced won't be dereferenced again. By
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	 * looking at the usage in add_new_gdb() the value isn't modified,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	 * just the pointer, and so it remains valid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	if (!bh_p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		ext4_error(sb, "Group descriptor not loaded - "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 			   "block_group = %u, group_desc = %u, desc = %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 			   block_group, group_desc, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	desc = (struct ext4_group_desc *)(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		(__u8 *)bh_p->b_data +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		offset * EXT4_DESC_SIZE(sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	if (bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		*bh = bh_p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	return desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)  * Return the block number which was discovered to be invalid, or 0 if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)  * the block bitmap is valid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) static ext4_fsblk_t ext4_valid_block_bitmap(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 					    struct ext4_group_desc *desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 					    ext4_group_t block_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 					    struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	struct ext4_sb_info *sbi = EXT4_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	ext4_grpblk_t offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	ext4_grpblk_t next_zero_bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	ext4_grpblk_t max_bit = EXT4_CLUSTERS_PER_GROUP(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	ext4_fsblk_t blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	ext4_fsblk_t group_first_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	if (ext4_has_feature_flex_bg(sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		/* with FLEX_BG, the inode/block bitmaps and itable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		 * blocks may not be in the group at all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		 * so the bitmap validation will be skipped for those groups
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		 * or it has to also read the block group where the bitmaps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		 * are located to verify they are set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	group_first_block = ext4_group_first_block_no(sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	/* check whether block bitmap block number is set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	blk = ext4_block_bitmap(sb, desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	offset = blk - group_first_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	if (offset < 0 || EXT4_B2C(sbi, offset) >= max_bit ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	    !ext4_test_bit(EXT4_B2C(sbi, offset), bh->b_data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		/* bad block bitmap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		return blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	/* check whether the inode bitmap block number is set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	blk = ext4_inode_bitmap(sb, desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	offset = blk - group_first_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	if (offset < 0 || EXT4_B2C(sbi, offset) >= max_bit ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	    !ext4_test_bit(EXT4_B2C(sbi, offset), bh->b_data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		/* bad block bitmap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		return blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	/* check whether the inode table block number is set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	blk = ext4_inode_table(sb, desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	offset = blk - group_first_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	if (offset < 0 || EXT4_B2C(sbi, offset) >= max_bit ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	    EXT4_B2C(sbi, offset + sbi->s_itb_per_group) >= max_bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		return blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	next_zero_bit = ext4_find_next_zero_bit(bh->b_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 			EXT4_B2C(sbi, offset + sbi->s_itb_per_group),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 			EXT4_B2C(sbi, offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	if (next_zero_bit <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	    EXT4_B2C(sbi, offset + sbi->s_itb_per_group))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		/* bad bitmap for inode tables */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		return blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) static int ext4_validate_block_bitmap(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 				      struct ext4_group_desc *desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 				      ext4_group_t block_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 				      struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	ext4_fsblk_t	blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	struct ext4_group_info *grp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	if (EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	grp = ext4_get_group_info(sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	if (buffer_verified(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	if (EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	ext4_lock_group(sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	if (buffer_verified(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		goto verified;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	if (unlikely(!ext4_block_bitmap_csum_verify(sb, block_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 						    desc, bh) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		     ext4_simulate_fail(sb, EXT4_SIM_BBITMAP_CRC))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		ext4_unlock_group(sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		ext4_error(sb, "bg %u: bad block bitmap checksum", block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		ext4_mark_group_bitmap_corrupted(sb, block_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 					EXT4_GROUP_INFO_BBITMAP_CORRUPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		return -EFSBADCRC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	blk = ext4_valid_block_bitmap(sb, desc, block_group, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	if (unlikely(blk != 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		ext4_unlock_group(sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		ext4_error(sb, "bg %u: block %llu: invalid block bitmap",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 			   block_group, blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		ext4_mark_group_bitmap_corrupted(sb, block_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 					EXT4_GROUP_INFO_BBITMAP_CORRUPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	set_buffer_verified(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) verified:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	ext4_unlock_group(sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)  * ext4_read_block_bitmap_nowait()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)  * @sb:			super block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)  * @block_group:	given block group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)  * Read the bitmap for a given block_group,and validate the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)  * bits for block/inode/inode tables are set in the bitmaps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)  * Return buffer_head on success or an ERR_PTR in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) struct buffer_head *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) ext4_read_block_bitmap_nowait(struct super_block *sb, ext4_group_t block_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 			      bool ignore_locked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	struct ext4_group_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	struct ext4_sb_info *sbi = EXT4_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	ext4_fsblk_t bitmap_blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	desc = ext4_get_group_desc(sb, block_group, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	if (!desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 		return ERR_PTR(-EFSCORRUPTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	bitmap_blk = ext4_block_bitmap(sb, desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	if ((bitmap_blk <= le32_to_cpu(sbi->s_es->s_first_data_block)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	    (bitmap_blk >= ext4_blocks_count(sbi->s_es))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		ext4_error(sb, "Invalid block bitmap block %llu in "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 			   "block_group %u", bitmap_blk, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		ext4_mark_group_bitmap_corrupted(sb, block_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 					EXT4_GROUP_INFO_BBITMAP_CORRUPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 		return ERR_PTR(-EFSCORRUPTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	bh = sb_getblk(sb, bitmap_blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	if (unlikely(!bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		ext4_warning(sb, "Cannot get buffer for block bitmap - "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 			     "block_group = %u, block_bitmap = %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 			     block_group, bitmap_blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	if (ignore_locked && buffer_locked(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		/* buffer under IO already, return if called for prefetching */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		put_bh(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	if (bitmap_uptodate(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 		goto verify;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	lock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	if (bitmap_uptodate(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		unlock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 		goto verify;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	ext4_lock_group(sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	if (ext4_has_group_desc_csum(sb) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	    (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		if (block_group == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 			ext4_unlock_group(sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 			unlock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 			ext4_error(sb, "Block bitmap for bg 0 marked "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 				   "uninitialized");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 			err = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		err = ext4_init_block_bitmap(sb, bh, block_group, desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		set_bitmap_uptodate(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		set_buffer_uptodate(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		set_buffer_verified(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		ext4_unlock_group(sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		unlock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 			ext4_error(sb, "Failed to init block bitmap for group "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 				   "%u: %d", block_group, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 		goto verify;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	ext4_unlock_group(sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	if (buffer_uptodate(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		 * if not uninit if bh is uptodate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 		 * bitmap is also uptodate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 		set_bitmap_uptodate(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 		unlock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		goto verify;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	 * submit the buffer_head for reading
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	set_buffer_new(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	trace_ext4_read_block_bitmap_load(sb, block_group, ignore_locked);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	ext4_read_bh_nowait(bh, REQ_META | REQ_PRIO |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 			    (ignore_locked ? REQ_RAHEAD : 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 			    ext4_end_bitmap_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) verify:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	err = ext4_validate_block_bitmap(sb, desc, block_group, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	put_bh(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) /* Returns 0 on success, -errno on error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) int ext4_wait_block_bitmap(struct super_block *sb, ext4_group_t block_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 			   struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	struct ext4_group_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	if (!buffer_new(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	desc = ext4_get_group_desc(sb, block_group, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	if (!desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	wait_on_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	ext4_simulate_fail_bh(sb, bh, EXT4_SIM_BBITMAP_EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	if (!buffer_uptodate(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 		ext4_error_err(sb, EIO, "Cannot read block bitmap - "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 			       "block_group = %u, block_bitmap = %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 			       block_group, (unsigned long long) bh->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 		ext4_mark_group_bitmap_corrupted(sb, block_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 					EXT4_GROUP_INFO_BBITMAP_CORRUPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	clear_buffer_new(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	/* Panic or remount fs read-only if block bitmap is invalid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	return ext4_validate_block_bitmap(sb, desc, block_group, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) struct buffer_head *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) ext4_read_block_bitmap(struct super_block *sb, ext4_group_t block_group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	bh = ext4_read_block_bitmap_nowait(sb, block_group, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	if (IS_ERR(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 		return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	err = ext4_wait_block_bitmap(sb, block_group, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 		put_bh(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 		return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)  * ext4_has_free_clusters()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)  * @sbi:	in-core super block structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)  * @nclusters:	number of needed blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)  * @flags:	flags from ext4_mb_new_blocks()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566)  * Check if filesystem has nclusters free & available for allocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)  * On success return 1, return 0 on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) static int ext4_has_free_clusters(struct ext4_sb_info *sbi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 				  s64 nclusters, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	s64 free_clusters, dirty_clusters, rsv, resv_clusters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	struct percpu_counter *fcc = &sbi->s_freeclusters_counter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	struct percpu_counter *dcc = &sbi->s_dirtyclusters_counter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	free_clusters  = percpu_counter_read_positive(fcc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	dirty_clusters = percpu_counter_read_positive(dcc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	resv_clusters = atomic64_read(&sbi->s_resv_clusters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	 * r_blocks_count should always be multiple of the cluster ratio so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	 * we are safe to do a plane bit shift only.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	rsv = (ext4_r_blocks_count(sbi->s_es) >> sbi->s_cluster_bits) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 	      resv_clusters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 	if (free_clusters - (nclusters + rsv + dirty_clusters) <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 					EXT4_FREECLUSTERS_WATERMARK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 		free_clusters  = percpu_counter_sum_positive(fcc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 		dirty_clusters = percpu_counter_sum_positive(dcc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	/* Check whether we have space after accounting for current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	 * dirty clusters & root reserved clusters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	if (free_clusters >= (rsv + nclusters + dirty_clusters))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	/* Hm, nope.  Are (enough) root reserved clusters available? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 	if (uid_eq(sbi->s_resuid, current_fsuid()) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	    (!gid_eq(sbi->s_resgid, GLOBAL_ROOT_GID) && in_group_p(sbi->s_resgid)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	    capable(CAP_SYS_RESOURCE) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	    (flags & EXT4_MB_USE_ROOT_BLOCKS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 		if (free_clusters >= (nclusters + dirty_clusters +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 				      resv_clusters))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 	/* No free blocks. Let's see if we can dip into reserved pool */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	if (flags & EXT4_MB_USE_RESERVED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 		if (free_clusters >= (nclusters + dirty_clusters))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) int ext4_claim_free_clusters(struct ext4_sb_info *sbi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 			     s64 nclusters, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 	if (ext4_has_free_clusters(sbi, nclusters, flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 		percpu_counter_add(&sbi->s_dirtyclusters_counter, nclusters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 		return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628)  * ext4_should_retry_alloc() - check if a block allocation should be retried
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629)  * @sb:			superblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)  * @retries:		number of retry attempts made so far
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)  * ext4_should_retry_alloc() is called when ENOSPC is returned while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)  * attempting to allocate blocks.  If there's an indication that a pending
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)  * journal transaction might free some space and allow another attempt to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635)  * succeed, this function will wait for the current or committing transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636)  * to complete and then return TRUE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) int ext4_should_retry_alloc(struct super_block *sb, int *retries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 	struct ext4_sb_info *sbi = EXT4_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 	if (!sbi->s_journal)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 	if (++(*retries) > 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 		percpu_counter_inc(&sbi->s_sra_exceeded_retry_limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 	 * if there's no indication that blocks are about to be freed it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 	 * possible we just missed a transaction commit that did so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 	smp_mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	if (sbi->s_mb_free_pending == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 		return ext4_has_free_clusters(sbi, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 	 * it's possible we've just missed a transaction commit here,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 	 * so ignore the returned status
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 	jbd_debug(1, "%s: retrying operation after ENOSPC\n", sb->s_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 	(void) jbd2_journal_force_commit_nested(sbi->s_journal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668)  * ext4_new_meta_blocks() -- allocate block for meta data (indexing) blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670)  * @handle:             handle to this transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671)  * @inode:              file inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)  * @goal:               given target block(filesystem wide)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673)  * @count:		pointer to total number of clusters needed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674)  * @errp:               error code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676)  * Return 1st allocated block number on success, *count stores total account
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677)  * error stores in errp pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) ext4_fsblk_t ext4_new_meta_blocks(handle_t *handle, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 				  ext4_fsblk_t goal, unsigned int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 				  unsigned long *count, int *errp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 	struct ext4_allocation_request ar;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 	ext4_fsblk_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 	memset(&ar, 0, sizeof(ar));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 	/* Fill with neighbour allocated blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	ar.inode = inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 	ar.goal = goal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	ar.len = count ? *count : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 	ar.flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 	ret = ext4_mb_new_blocks(handle, &ar, errp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 	if (count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 		*count = ar.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 	 * Account for the allocated meta blocks.  We will never
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 	 * fail EDQUOT for metdata, but we do account for it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 	if (!(*errp) && (flags & EXT4_MB_DELALLOC_RESERVED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 		dquot_alloc_block_nofail(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 				EXT4_C2B(EXT4_SB(inode->i_sb), ar.len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708)  * ext4_count_free_clusters() -- count filesystem free clusters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709)  * @sb:		superblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711)  * Adds up the number of free clusters from each block group.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) ext4_fsblk_t ext4_count_free_clusters(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 	ext4_fsblk_t desc_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 	struct ext4_group_desc *gdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 	ext4_group_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 	ext4_group_t ngroups = ext4_get_groups_count(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 	struct ext4_group_info *grp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) #ifdef EXT4FS_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 	struct ext4_super_block *es;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 	ext4_fsblk_t bitmap_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 	unsigned int x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 	struct buffer_head *bitmap_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 	es = EXT4_SB(sb)->s_es;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 	desc_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 	bitmap_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 	gdp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 	for (i = 0; i < ngroups; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 		gdp = ext4_get_group_desc(sb, i, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 		if (!gdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 		grp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 		if (EXT4_SB(sb)->s_group_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 			grp = ext4_get_group_info(sb, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 		if (!grp || !EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 			desc_count += ext4_free_group_clusters(sb, gdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 		brelse(bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 		bitmap_bh = ext4_read_block_bitmap(sb, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 		if (IS_ERR(bitmap_bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 			bitmap_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 		x = ext4_count_free(bitmap_bh->b_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 				    EXT4_CLUSTERS_PER_GROUP(sb) / 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 		printk(KERN_DEBUG "group %u: stored = %d, counted = %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 			i, ext4_free_group_clusters(sb, gdp), x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 		bitmap_count += x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 	brelse(bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 	printk(KERN_DEBUG "ext4_count_free_clusters: stored = %llu"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 	       ", computed = %llu, %llu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 	       EXT4_NUM_B2C(EXT4_SB(sb), ext4_free_blocks_count(es)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 	       desc_count, bitmap_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 	return bitmap_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 	desc_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 	for (i = 0; i < ngroups; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 		gdp = ext4_get_group_desc(sb, i, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 		if (!gdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 		grp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 		if (EXT4_SB(sb)->s_group_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 			grp = ext4_get_group_info(sb, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 		if (!grp || !EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 			desc_count += ext4_free_group_clusters(sb, gdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 	return desc_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) static inline int test_root(ext4_group_t a, int b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 		if (a < b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 		if (a == b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 		if ((a % b) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 		a = a / b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790)  *	ext4_bg_has_super - number of blocks used by the superblock in group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791)  *	@sb: superblock for filesystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792)  *	@group: group number to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794)  *	Return the number of blocks used by the superblock (primary or backup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795)  *	in this group.  Currently this will be only 0 or 1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) int ext4_bg_has_super(struct super_block *sb, ext4_group_t group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) 	if (group == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) 	if (ext4_has_feature_sparse_super2(sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) 		if (group == le32_to_cpu(es->s_backup_bgs[0]) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) 		    group == le32_to_cpu(es->s_backup_bgs[1]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) 	if ((group <= 1) || !ext4_has_feature_sparse_super(sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) 	if (!(group & 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) 	if (test_root(group, 3) || (test_root(group, 5)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) 	    test_root(group, 7))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) static unsigned long ext4_bg_num_gdb_meta(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) 					ext4_group_t group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) 	unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) 	ext4_group_t first = metagroup * EXT4_DESC_PER_BLOCK(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) 	ext4_group_t last = first + EXT4_DESC_PER_BLOCK(sb) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) 	if (group == first || group == first + 1 || group == last)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) static unsigned long ext4_bg_num_gdb_nometa(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) 					ext4_group_t group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) 	if (!ext4_bg_has_super(sb, group))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) 	if (ext4_has_feature_meta_bg(sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) 		return le32_to_cpu(EXT4_SB(sb)->s_es->s_first_meta_bg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) 		return EXT4_SB(sb)->s_gdb_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845)  *	ext4_bg_num_gdb - number of blocks used by the group table in group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846)  *	@sb: superblock for filesystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847)  *	@group: group number to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849)  *	Return the number of blocks used by the group descriptor table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850)  *	(primary or backup) in this group.  In the future there may be a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851)  *	different number of descriptor blocks in each group.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) unsigned long ext4_bg_num_gdb(struct super_block *sb, ext4_group_t group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) 	unsigned long first_meta_bg =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) 			le32_to_cpu(EXT4_SB(sb)->s_es->s_first_meta_bg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) 	unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) 	if (!ext4_has_feature_meta_bg(sb) || metagroup < first_meta_bg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) 		return ext4_bg_num_gdb_nometa(sb, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) 	return ext4_bg_num_gdb_meta(sb,group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867)  * This function returns the number of file system metadata clusters at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868)  * the beginning of a block group, including the reserved gdt blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) static unsigned ext4_num_base_meta_clusters(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) 				     ext4_group_t block_group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) 	struct ext4_sb_info *sbi = EXT4_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) 	unsigned num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) 	/* Check for superblock and gdt backups in this group */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) 	num = ext4_bg_has_super(sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) 	if (!ext4_has_feature_meta_bg(sb) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) 	    block_group < le32_to_cpu(sbi->s_es->s_first_meta_bg) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) 			  sbi->s_desc_per_block) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) 		if (num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) 			num += ext4_bg_num_gdb(sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) 			num += le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) 	} else { /* For META_BG_BLOCK_GROUPS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) 		num += ext4_bg_num_gdb(sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) 	return EXT4_NUM_B2C(sbi, num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892)  *	ext4_inode_to_goal_block - return a hint for block allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893)  *	@inode: inode for block allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895)  *	Return the ideal location to start allocating blocks for a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896)  *	newly created inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) ext4_fsblk_t ext4_inode_to_goal_block(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) 	struct ext4_inode_info *ei = EXT4_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) 	ext4_group_t block_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) 	ext4_grpblk_t colour;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) 	int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) 	ext4_fsblk_t bg_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) 	ext4_fsblk_t last_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) 	block_group = ei->i_block_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) 	if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) 		 * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) 		 * block groups per flexgroup, reserve the first block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) 		 * group for directories and special files.  Regular
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) 		 * files will start at the second block group.  This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) 		 * tends to speed up directory access and improves
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) 		 * fsck times.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) 		block_group &= ~(flex_size-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) 		if (S_ISREG(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) 			block_group++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) 	bg_start = ext4_group_first_block_no(inode->i_sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) 	last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) 	 * If we are doing delayed allocation, we don't need take
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) 	 * colour into account.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) 	if (test_opt(inode->i_sb, DELALLOC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) 		return bg_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) 	if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) 		colour = (task_pid_nr(current) % 16) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) 			(EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) 		colour = (task_pid_nr(current) % 16) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) 			((last_block - bg_start) / 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) 	return bg_start + colour;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939)