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/ialloc.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)  *  BSD ufs-inspired inode and directory allocation by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  *  Stephen Tweedie (sct@redhat.com), 1993
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  *  Big-endian to little-endian byte-swapping/bitmaps by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  *        David S. Miller (davem@caip.rutgers.edu), 1995
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/time.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/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/quotaops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/buffer_head.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <linux/cred.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include <asm/byteorder.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include "ext4.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include "ext4_jbd2.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include "xattr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include "acl.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include <trace/events/ext4.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37)  * ialloc.c contains the inodes allocation and deallocation routines
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41)  * The free inodes are managed by bitmaps.  A file system contains several
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42)  * blocks groups.  Each group contains 1 bitmap block for blocks, 1 bitmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43)  * block for inodes, N blocks for the inode table and data blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45)  * The file system contains group descriptors which are located after the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46)  * super block.  Each descriptor contains the number of the bitmap block and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47)  * the free blocks count in the block.
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51)  * To avoid calling the atomic setbit hundreds or thousands of times, we only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52)  * need to use it within a single byte (to ensure we get endianness right).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53)  * We can use memset for the rest of the bitmap as there are no other users.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) void ext4_mark_bitmap_end(int start_bit, int end_bit, char *bitmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	if (start_bit >= end_bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 	ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 	for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 		ext4_set_bit(i, bitmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 	if (i < end_bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 		memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
^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) void ext4_end_bitmap_read(struct buffer_head *bh, int uptodate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	if (uptodate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 		set_buffer_uptodate(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 		set_bitmap_uptodate(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 	unlock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	put_bh(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) static int ext4_validate_inode_bitmap(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 				      struct ext4_group_desc *desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 				      ext4_group_t block_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 				      struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	ext4_fsblk_t	blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 	struct ext4_group_info *grp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	if (EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	grp = ext4_get_group_info(sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	if (buffer_verified(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 		return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	ext4_lock_group(sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	if (buffer_verified(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 		goto verified;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 	blk = ext4_inode_bitmap(sb, desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	if (!ext4_inode_bitmap_csum_verify(sb, block_group, desc, bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 					   EXT4_INODES_PER_GROUP(sb) / 8) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	    ext4_simulate_fail(sb, EXT4_SIM_IBITMAP_CRC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 		ext4_unlock_group(sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 		ext4_error(sb, "Corrupt inode bitmap - block_group = %u, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 			   "inode_bitmap = %llu", block_group, blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 		ext4_mark_group_bitmap_corrupted(sb, block_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 					EXT4_GROUP_INFO_IBITMAP_CORRUPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 		return -EFSBADCRC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	set_buffer_verified(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) verified:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	ext4_unlock_group(sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118)  * Read the inode allocation bitmap for a given block_group, reading
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119)  * into the specified slot in the superblock's bitmap cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121)  * Return buffer_head of bitmap on success, or an ERR_PTR on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) static struct buffer_head *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	struct ext4_group_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	struct ext4_sb_info *sbi = EXT4_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	struct buffer_head *bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	ext4_fsblk_t bitmap_blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	desc = ext4_get_group_desc(sb, block_group, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	if (!desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 		return ERR_PTR(-EFSCORRUPTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	bitmap_blk = ext4_inode_bitmap(sb, desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	if ((bitmap_blk <= le32_to_cpu(sbi->s_es->s_first_data_block)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	    (bitmap_blk >= ext4_blocks_count(sbi->s_es))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 		ext4_error(sb, "Invalid inode bitmap blk %llu in "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 			   "block_group %u", bitmap_blk, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 		ext4_mark_group_bitmap_corrupted(sb, block_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 					EXT4_GROUP_INFO_IBITMAP_CORRUPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 		return ERR_PTR(-EFSCORRUPTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	bh = sb_getblk(sb, bitmap_blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	if (unlikely(!bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 		ext4_warning(sb, "Cannot read inode bitmap - "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 			     "block_group = %u, inode_bitmap = %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 			     block_group, bitmap_blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	if (bitmap_uptodate(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 		goto verify;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	lock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	if (bitmap_uptodate(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 		unlock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 		goto verify;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	ext4_lock_group(sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	if (ext4_has_group_desc_csum(sb) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	    (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 		if (block_group == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 			ext4_unlock_group(sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 			unlock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 			ext4_error(sb, "Inode bitmap for bg 0 marked "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 				   "uninitialized");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 			err = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 		memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 		ext4_mark_bitmap_end(EXT4_INODES_PER_GROUP(sb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 				     sb->s_blocksize * 8, bh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 		set_bitmap_uptodate(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 		set_buffer_uptodate(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 		set_buffer_verified(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 		ext4_unlock_group(sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 		unlock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 		return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	ext4_unlock_group(sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	if (buffer_uptodate(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 		 * if not uninit if bh is uptodate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 		 * bitmap is also uptodate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 		set_bitmap_uptodate(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 		unlock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 		goto verify;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	 * submit the buffer_head for reading
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	trace_ext4_load_inode_bitmap(sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	ext4_read_bh(bh, REQ_META | REQ_PRIO, ext4_end_bitmap_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	ext4_simulate_fail_bh(sb, bh, EXT4_SIM_IBITMAP_EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	if (!buffer_uptodate(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 		put_bh(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 		ext4_error_err(sb, EIO, "Cannot read inode bitmap - "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 			       "block_group = %u, inode_bitmap = %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 			       block_group, bitmap_blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 		ext4_mark_group_bitmap_corrupted(sb, block_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 				EXT4_GROUP_INFO_IBITMAP_CORRUPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 		return ERR_PTR(-EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) verify:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	err = ext4_validate_inode_bitmap(sb, desc, block_group, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	put_bh(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220)  * NOTE! When we get the inode, we're the only people
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221)  * that have access to it, and as such there are no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222)  * race conditions we have to worry about. The inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223)  * is not on the hash-lists, and it cannot be reached
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224)  * through the filesystem because the directory entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225)  * has been deleted earlier.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227)  * HOWEVER: we must make sure that we get no aliases,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228)  * which means that we have to call "clear_inode()"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229)  * _before_ we mark the inode not in use in the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230)  * bitmaps. Otherwise a newly created file might use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231)  * the same inode number (not actually the same pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232)  * though), and then we'd have two inodes sharing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233)  * same inode number and space on the harddisk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) void ext4_free_inode(handle_t *handle, struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	int is_directory;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	unsigned long ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	struct buffer_head *bitmap_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	struct buffer_head *bh2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	ext4_group_t block_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	unsigned long bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	struct ext4_group_desc *gdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	struct ext4_super_block *es;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	struct ext4_sb_info *sbi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	int fatal = 0, err, count, cleared;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	struct ext4_group_info *grp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	if (!sb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 		printk(KERN_ERR "EXT4-fs: %s:%d: inode on "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 		       "nonexistent device\n", __func__, __LINE__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	if (atomic_read(&inode->i_count) > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 		ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: count=%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 			 __func__, __LINE__, inode->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 			 atomic_read(&inode->i_count));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	if (inode->i_nlink) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 		ext4_msg(sb, KERN_ERR, "%s:%d: inode #%lu: nlink=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 			 __func__, __LINE__, inode->i_ino, inode->i_nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	sbi = EXT4_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	ino = inode->i_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	ext4_debug("freeing inode %lu\n", ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	trace_ext4_free_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	dquot_initialize(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	dquot_free_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	is_directory = S_ISDIR(inode->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	/* Do this BEFORE marking the inode not in use or returning an error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	ext4_clear_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	es = sbi->s_es;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 	if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 		ext4_error(sb, "reserved or nonexistent inode %lu", ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 		goto error_return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	/* Don't bother if the inode bitmap is corrupt. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	if (IS_ERR(bitmap_bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 		fatal = PTR_ERR(bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 		bitmap_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 		goto error_return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	if (!(sbi->s_mount_state & EXT4_FC_REPLAY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 		grp = ext4_get_group_info(sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 		if (unlikely(EXT4_MB_GRP_IBITMAP_CORRUPT(grp))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 			fatal = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 			goto error_return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	BUFFER_TRACE(bitmap_bh, "get_write_access");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	fatal = ext4_journal_get_write_access(handle, bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	if (fatal)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 		goto error_return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	fatal = -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	gdp = ext4_get_group_desc(sb, block_group, &bh2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	if (gdp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 		BUFFER_TRACE(bh2, "get_write_access");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 		fatal = ext4_journal_get_write_access(handle, bh2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	ext4_lock_group(sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	cleared = ext4_test_and_clear_bit(bit, bitmap_bh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	if (fatal || !cleared) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 		ext4_unlock_group(sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	count = ext4_free_inodes_count(sb, gdp) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	ext4_free_inodes_set(sb, gdp, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	if (is_directory) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 		count = ext4_used_dirs_count(sb, gdp) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 		ext4_used_dirs_set(sb, gdp, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 		if (percpu_counter_initialized(&sbi->s_dirs_counter))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 			percpu_counter_dec(&sbi->s_dirs_counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	ext4_inode_bitmap_csum_set(sb, block_group, gdp, bitmap_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 				   EXT4_INODES_PER_GROUP(sb) / 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	ext4_group_desc_csum_set(sb, block_group, gdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	ext4_unlock_group(sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	if (percpu_counter_initialized(&sbi->s_freeinodes_counter))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 		percpu_counter_inc(&sbi->s_freeinodes_counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	if (sbi->s_log_groups_per_flex) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 		struct flex_groups *fg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 		fg = sbi_array_rcu_deref(sbi, s_flex_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 					 ext4_flex_group(sbi, block_group));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 		atomic_inc(&fg->free_inodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 		if (is_directory)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 			atomic_dec(&fg->used_dirs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	fatal = ext4_handle_dirty_metadata(handle, NULL, bh2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	if (cleared) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 		BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 		err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 		if (!fatal)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 			fatal = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 		ext4_error(sb, "bit already cleared for inode %lu", ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 		ext4_mark_group_bitmap_corrupted(sb, block_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 					EXT4_GROUP_INFO_IBITMAP_CORRUPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) error_return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	brelse(bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	ext4_std_error(sb, fatal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) struct orlov_stats {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	__u64 free_clusters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	__u32 free_inodes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	__u32 used_dirs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370)  * Helper function for Orlov's allocator; returns critical information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371)  * for a particular block group or flex_bg.  If flex_size is 1, then g
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372)  * is a block group number; otherwise it is flex_bg number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) static void get_orlov_stats(struct super_block *sb, ext4_group_t g,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 			    int flex_size, struct orlov_stats *stats)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	struct ext4_group_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	if (flex_size > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 		struct flex_groups *fg = sbi_array_rcu_deref(EXT4_SB(sb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 							     s_flex_groups, g);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 		stats->free_inodes = atomic_read(&fg->free_inodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 		stats->free_clusters = atomic64_read(&fg->free_clusters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 		stats->used_dirs = atomic_read(&fg->used_dirs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	desc = ext4_get_group_desc(sb, g, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	if (desc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 		stats->free_inodes = ext4_free_inodes_count(sb, desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 		stats->free_clusters = ext4_free_group_clusters(sb, desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 		stats->used_dirs = ext4_used_dirs_count(sb, desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 		stats->free_inodes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 		stats->free_clusters = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 		stats->used_dirs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401)  * Orlov's allocator for directories.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403)  * We always try to spread first-level directories.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405)  * If there are blockgroups with both free inodes and free clusters counts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406)  * not worse than average we return one with smallest directory count.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407)  * Otherwise we simply return a random group.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409)  * For the rest rules look so:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411)  * It's OK to put directory into a group unless
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412)  * it has too many directories already (max_dirs) or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413)  * it has too few free inodes left (min_inodes) or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414)  * it has too few free clusters left (min_clusters) or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415)  * Parent's group is preferred, if it doesn't satisfy these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416)  * conditions we search cyclically through the rest. If none
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417)  * of the groups look good we just look for a group with more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418)  * free inodes than average (starting at parent's group).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) static int find_group_orlov(struct super_block *sb, struct inode *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 			    ext4_group_t *group, umode_t mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 			    const struct qstr *qstr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	struct ext4_sb_info *sbi = EXT4_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	ext4_group_t real_ngroups = ext4_get_groups_count(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	int inodes_per_group = EXT4_INODES_PER_GROUP(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	unsigned int freei, avefreei, grp_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	ext4_fsblk_t freec, avefreec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	unsigned int ndirs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	int max_dirs, min_inodes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	ext4_grpblk_t min_clusters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	ext4_group_t i, grp, g, ngroups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	struct ext4_group_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	struct orlov_stats stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	int flex_size = ext4_flex_bg_size(sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	struct dx_hash_info hinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	ngroups = real_ngroups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	if (flex_size > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 		ngroups = (real_ngroups + flex_size - 1) >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 			sbi->s_log_groups_per_flex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 		parent_group >>= sbi->s_log_groups_per_flex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	avefreei = freei / ngroups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	freec = percpu_counter_read_positive(&sbi->s_freeclusters_counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	avefreec = freec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	do_div(avefreec, ngroups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	if (S_ISDIR(mode) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	    ((parent == d_inode(sb->s_root)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	     (ext4_test_inode_flag(parent, EXT4_INODE_TOPDIR)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 		int best_ndir = inodes_per_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 		int ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 		if (qstr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 			if (ext4_hash_in_dirent(parent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 				hinfo.hash_version = DX_HASH_SIPHASH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 				hinfo.hash_version = DX_HASH_HALF_MD4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 			hinfo.seed = sbi->s_hash_seed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 			ext4fs_dirhash(parent, qstr->name, qstr->len, &hinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 			grp = hinfo.hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 			grp = prandom_u32();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 		parent_group = (unsigned)grp % ngroups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 		for (i = 0; i < ngroups; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 			g = (parent_group + i) % ngroups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 			get_orlov_stats(sb, g, flex_size, &stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 			if (!stats.free_inodes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 			if (stats.used_dirs >= best_ndir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 			if (stats.free_inodes < avefreei)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 			if (stats.free_clusters < avefreec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 			grp = g;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 			ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 			best_ndir = stats.used_dirs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 			goto fallback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	found_flex_bg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 		if (flex_size == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 			*group = grp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 		 * We pack inodes at the beginning of the flexgroup's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 		 * inode tables.  Block allocation decisions will do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 		 * something similar, although regular files will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 		 * start at 2nd block group of the flexgroup.  See
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 		 * ext4_ext_find_goal() and ext4_find_near().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 		grp *= flex_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 		for (i = 0; i < flex_size; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 			if (grp+i >= real_ngroups)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 			desc = ext4_get_group_desc(sb, grp+i, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 			if (desc && ext4_free_inodes_count(sb, desc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 				*group = grp+i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 				return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 		goto fallback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	max_dirs = ndirs / ngroups + inodes_per_group / 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	min_inodes = avefreei - inodes_per_group*flex_size / 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 	if (min_inodes < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 		min_inodes = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	min_clusters = avefreec - EXT4_CLUSTERS_PER_GROUP(sb)*flex_size / 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	 * Start looking in the flex group where we last allocated an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	 * inode for this parent directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	if (EXT4_I(parent)->i_last_alloc_group != ~0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 		parent_group = EXT4_I(parent)->i_last_alloc_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 		if (flex_size > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 			parent_group >>= sbi->s_log_groups_per_flex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	for (i = 0; i < ngroups; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 		grp = (parent_group + i) % ngroups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 		get_orlov_stats(sb, grp, flex_size, &stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		if (stats.used_dirs >= max_dirs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 		if (stats.free_inodes < min_inodes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 		if (stats.free_clusters < min_clusters)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 		goto found_flex_bg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) fallback:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	ngroups = real_ngroups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	avefreei = freei / ngroups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) fallback_retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	parent_group = EXT4_I(parent)->i_block_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	for (i = 0; i < ngroups; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 		grp = (parent_group + i) % ngroups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 		desc = ext4_get_group_desc(sb, grp, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 		if (desc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 			grp_free = ext4_free_inodes_count(sb, desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 			if (grp_free && grp_free >= avefreei) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 				*group = grp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 				return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	if (avefreei) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 		 * The free-inodes counter is approximate, and for really small
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 		 * filesystems the above test can fail to find any blockgroups
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 		avefreei = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 		goto fallback_retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) static int find_group_other(struct super_block *sb, struct inode *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 			    ext4_group_t *group, umode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	ext4_group_t i, last, ngroups = ext4_get_groups_count(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	struct ext4_group_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	int flex_size = ext4_flex_bg_size(EXT4_SB(sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 	 * Try to place the inode is the same flex group as its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	 * parent.  If we can't find space, use the Orlov algorithm to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	 * find another flex group, and store that information in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	 * parent directory's inode information so that use that flex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	 * group for future allocations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	if (flex_size > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 		int retry = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	try_again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 		parent_group &= ~(flex_size-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 		last = parent_group + flex_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 		if (last > ngroups)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 			last = ngroups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 		for  (i = parent_group; i < last; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 			desc = ext4_get_group_desc(sb, i, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 			if (desc && ext4_free_inodes_count(sb, desc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 				*group = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 				return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 		if (!retry && EXT4_I(parent)->i_last_alloc_group != ~0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 			retry = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 			parent_group = EXT4_I(parent)->i_last_alloc_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 			goto try_again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 		 * If this didn't work, use the Orlov search algorithm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 		 * to find a new flex group; we pass in the mode to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 		 * avoid the topdir algorithms.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 		*group = parent_group + flex_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 		if (*group > ngroups)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 			*group = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 		return find_group_orlov(sb, parent, group, mode, NULL);
^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) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	 * Try to place the inode in its parent directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	*group = parent_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	desc = ext4_get_group_desc(sb, *group, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	if (desc && ext4_free_inodes_count(sb, desc) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	    ext4_free_group_clusters(sb, desc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 		return 0;
^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) 	 * We're going to place this inode in a different blockgroup from its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	 * parent.  We want to cause files in a common directory to all land in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	 * the same blockgroup.  But we want files which are in a different
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	 * directory which shares a blockgroup with our parent to land in a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	 * different blockgroup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	 * So add our directory's i_ino into the starting point for the hash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	*group = (*group + parent->i_ino) % ngroups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	 * Use a quadratic hash to find a group with a free inode and some free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	 * blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	for (i = 1; i < ngroups; i <<= 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 		*group += i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 		if (*group >= ngroups)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 			*group -= ngroups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 		desc = ext4_get_group_desc(sb, *group, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 		if (desc && ext4_free_inodes_count(sb, desc) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 		    ext4_free_group_clusters(sb, desc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 			return 0;
^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) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	 * That failed: try linear search for a free inode, even if that group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	 * has no free blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	*group = parent_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	for (i = 0; i < ngroups; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 		if (++*group >= ngroups)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 			*group = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 		desc = ext4_get_group_desc(sb, *group, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 		if (desc && ext4_free_inodes_count(sb, desc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 
^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)  * In no journal mode, if an inode has recently been deleted, we want
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669)  * to avoid reusing it until we're reasonably sure the inode table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670)  * block has been written back to disk.  (Yes, these values are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671)  * somewhat arbitrary...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) #define RECENTCY_MIN	60
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) #define RECENTCY_DIRTY	300
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) static int recently_deleted(struct super_block *sb, ext4_group_t group, int ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	struct ext4_group_desc	*gdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	struct ext4_inode	*raw_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	struct buffer_head	*bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	int inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	int offset, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	int recentcy = RECENTCY_MIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	u32 dtime, now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	gdp = ext4_get_group_desc(sb, group, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	if (unlikely(!gdp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	bh = sb_find_get_block(sb, ext4_inode_table(sb, gdp) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 		       (ino / inodes_per_block));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	if (!bh || !buffer_uptodate(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 		 * If the block is not in the buffer cache, then it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 		 * must have been written out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	offset = (ino % inodes_per_block) * EXT4_INODE_SIZE(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	raw_inode = (struct ext4_inode *) (bh->b_data + offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	/* i_dtime is only 32 bits on disk, but we only care about relative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	 * times in the range of a few minutes (i.e. long enough to sync a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	 * recently-deleted inode to disk), so using the low 32 bits of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	 * clock (a 68 year range) is enough, see time_before32() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	dtime = le32_to_cpu(raw_inode->i_dtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	now = ktime_get_real_seconds();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	if (buffer_dirty(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 		recentcy += RECENTCY_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	if (dtime && time_before32(dtime, now) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	    time_before32(now, dtime + recentcy))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 		ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) static int find_inode_bit(struct super_block *sb, ext4_group_t group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 			  struct buffer_head *bitmap, unsigned long *ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	bool check_recently_deleted = EXT4_SB(sb)->s_journal == NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	unsigned long recently_deleted_ino = EXT4_INODES_PER_GROUP(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	*ino = ext4_find_next_zero_bit((unsigned long *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 				       bitmap->b_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 				       EXT4_INODES_PER_GROUP(sb), *ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	if (*ino >= EXT4_INODES_PER_GROUP(sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 		goto not_found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	if (check_recently_deleted && recently_deleted(sb, group, *ino)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 		recently_deleted_ino = *ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 		*ino = *ino + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 		if (*ino < EXT4_INODES_PER_GROUP(sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 			goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 		goto not_found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) not_found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	if (recently_deleted_ino >= EXT4_INODES_PER_GROUP(sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	 * Not reusing recently deleted inodes is mostly a preference. We don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	 * want to report ENOSPC or skew allocation patterns because of that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	 * So return even recently deleted inode if we could find better in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	 * given range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 	*ino = recently_deleted_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) int ext4_mark_inode_used(struct super_block *sb, int ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	struct buffer_head *inode_bitmap_bh = NULL, *group_desc_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	struct ext4_group_desc *gdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	ext4_group_t group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	int bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	int err = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	if (ino < EXT4_FIRST_INO(sb) || ino > max_ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	if (IS_ERR(inode_bitmap_bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 		return PTR_ERR(inode_bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	if (ext4_test_bit(bit, inode_bitmap_bh->b_data)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 		err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 		goto out;
^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) 	gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	if (!gdp || !group_desc_bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	ext4_set_bit(bit, inode_bitmap_bh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	BUFFER_TRACE(inode_bitmap_bh, "call ext4_handle_dirty_metadata");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	err = ext4_handle_dirty_metadata(NULL, NULL, inode_bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 		ext4_std_error(sb, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	err = sync_dirty_buffer(inode_bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 		ext4_std_error(sb, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	/* We may have to initialize the block bitmap if it isn't already */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	if (ext4_has_group_desc_csum(sb) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	    gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 		struct buffer_head *block_bitmap_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 		block_bitmap_bh = ext4_read_block_bitmap(sb, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 		if (IS_ERR(block_bitmap_bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 			err = PTR_ERR(block_bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 		err = ext4_handle_dirty_metadata(NULL, NULL, block_bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 		sync_dirty_buffer(block_bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 		/* recheck and clear flag under lock if we still need to */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 		ext4_lock_group(sb, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 		if (ext4_has_group_desc_csum(sb) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 		    (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 			ext4_free_group_clusters_set(sb, gdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 				ext4_free_clusters_after_init(sb, group, gdp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 			ext4_block_bitmap_csum_set(sb, group, gdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 						   block_bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 			ext4_group_desc_csum_set(sb, group, gdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 		ext4_unlock_group(sb, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 		brelse(block_bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 			ext4_std_error(sb, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	/* Update the relevant bg descriptor fields */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	if (ext4_has_group_desc_csum(sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 		int free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 		ext4_lock_group(sb, group); /* while we modify the bg desc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 		free = EXT4_INODES_PER_GROUP(sb) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 			ext4_itable_unused_count(sb, gdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 		if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 			free = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 		}
^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) 		 * Check the relative inode number against the last used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 		 * relative inode number in this group. if it is greater
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 		 * we need to update the bg_itable_unused count
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 		if (bit >= free)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 			ext4_itable_unused_set(sb, gdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 					(EXT4_INODES_PER_GROUP(sb) - bit - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 		ext4_lock_group(sb, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	if (ext4_has_group_desc_csum(sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 		ext4_inode_bitmap_csum_set(sb, group, gdp, inode_bitmap_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 					   EXT4_INODES_PER_GROUP(sb) / 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 		ext4_group_desc_csum_set(sb, group, gdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	ext4_unlock_group(sb, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	err = ext4_handle_dirty_metadata(NULL, NULL, group_desc_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	sync_dirty_buffer(group_desc_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 					    bool encrypt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	struct super_block *sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	int nblocks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) #ifdef CONFIG_EXT4_FS_POSIX_ACL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	struct posix_acl *p = get_acl(dir, ACL_TYPE_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	if (IS_ERR(p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 		return PTR_ERR(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	if (p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		int acl_size = p->a_count * sizeof(ext4_acl_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 		nblocks += (S_ISDIR(mode) ? 2 : 1) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 			__ext4_xattr_set_credits(sb, NULL /* inode */,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 						 NULL /* block_bh */, acl_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 						 true /* is_create */);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 		posix_acl_release(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) #ifdef CONFIG_SECURITY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 		int num_security_xattrs = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) #ifdef CONFIG_INTEGRITY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 		num_security_xattrs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 		 * We assume that security xattrs are never more than 1k.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 		 * In practice they are under 128 bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 		nblocks += num_security_xattrs *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 			__ext4_xattr_set_credits(sb, NULL /* inode */,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 						 NULL /* block_bh */, 1024,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 						 true /* is_create */);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	if (encrypt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 		nblocks += __ext4_xattr_set_credits(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 						    NULL /* inode */,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 						    NULL /* block_bh */,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 						    FSCRYPT_SET_CONTEXT_MAX_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 						    true /* is_create */);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	return nblocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917)  * There are two policies for allocating an inode.  If the new inode is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918)  * a directory, then a forward search is made for a block group with both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919)  * free space and a low directory-to-inode ratio; if that fails, then of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920)  * the groups with above-average free space, that group with the fewest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921)  * directories already is chosen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923)  * For other inodes, search forward from the parent directory's block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924)  * group to find a free inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 			       umode_t mode, const struct qstr *qstr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 			       __u32 goal, uid_t *owner, __u32 i_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 			       int handle_type, unsigned int line_no,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 			       int nblocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	struct super_block *sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	struct buffer_head *inode_bitmap_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	struct buffer_head *group_desc_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	ext4_group_t ngroups, group = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	unsigned long ino = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	struct ext4_group_desc *gdp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	struct ext4_inode_info *ei;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 	struct ext4_sb_info *sbi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	int ret2, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	struct inode *ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	ext4_group_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	ext4_group_t flex_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	struct ext4_group_info *grp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	bool encrypt = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	/* Cannot create files in a deleted directory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	if (!dir || !dir->i_nlink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 		return ERR_PTR(-EPERM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	sbi = EXT4_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 	if (unlikely(ext4_forced_shutdown(sbi)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 		return ERR_PTR(-EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	ngroups = ext4_get_groups_count(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	trace_ext4_request_inode(dir, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	inode = new_inode(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	if (!inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	ei = EXT4_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	 * Initialize owners and quota early so that we don't have to account
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	 * for quota initialization worst case in standard inode creating
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	 * transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	if (owner) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 		inode->i_mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 		i_uid_write(inode, owner[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 		i_gid_write(inode, owner[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 	} else if (test_opt(sb, GRPID)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 		inode->i_mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		inode->i_uid = current_fsuid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 		inode->i_gid = dir->i_gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 		inode_init_owner(inode, dir, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	if (ext4_has_feature_project(sb) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	    ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 		ei->i_projid = EXT4_I(dir)->i_projid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 		ei->i_projid = make_kprojid(&init_user_ns, EXT4_DEF_PROJID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	if (!(i_flags & EXT4_EA_INODE_FL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 		err = fscrypt_prepare_new_inode(dir, inode, &encrypt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	err = dquot_initialize(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 		ret2 = ext4_xattr_credits_for_new_inode(dir, mode, encrypt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 		if (ret2 < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 			err = ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 		nblocks += ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	if (!goal)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 		goal = sbi->s_inode_goal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	if (goal && goal <= le32_to_cpu(sbi->s_es->s_inodes_count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 		group = (goal - 1) / EXT4_INODES_PER_GROUP(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 		ino = (goal - 1) % EXT4_INODES_PER_GROUP(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 		ret2 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 		goto got_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	if (S_ISDIR(mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 		ret2 = find_group_orlov(sb, dir, &group, mode, qstr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 		ret2 = find_group_other(sb, dir, &group, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) got_group:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	EXT4_I(dir)->i_last_alloc_group = group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	err = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	if (ret2 == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	 * Normally we will only go through one pass of this loop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	 * unless we get unlucky and it turns out the group we selected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	 * had its last inode grabbed by someone else.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	for (i = 0; i < ngroups; i++, ino = 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 		err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 		gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 		if (!gdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 		 * Check free inodes count before loading bitmap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 		if (ext4_free_inodes_count(sb, gdp) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 			goto next_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 		if (!(sbi->s_mount_state & EXT4_FC_REPLAY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 			grp = ext4_get_group_info(sb, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 			 * Skip groups with already-known suspicious inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 			 * tables
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 			if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 				goto next_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 		brelse(inode_bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 		inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 		/* Skip groups with suspicious inode tables */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 		if (((!(sbi->s_mount_state & EXT4_FC_REPLAY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 		     && EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 		    IS_ERR(inode_bitmap_bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 			inode_bitmap_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 			goto next_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) repeat_in_this_group:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 		ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 		if (!ret2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 			goto next_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 		if (group == 0 && (ino + 1) < EXT4_FIRST_INO(sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 			ext4_error(sb, "reserved inode found cleared - "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 				   "inode=%lu", ino + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 			ext4_mark_group_bitmap_corrupted(sb, group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 					EXT4_GROUP_INFO_IBITMAP_CORRUPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 			goto next_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 		if ((!(sbi->s_mount_state & EXT4_FC_REPLAY)) && !handle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 			BUG_ON(nblocks <= 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 			handle = __ext4_journal_start_sb(dir->i_sb, line_no,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 				 handle_type, nblocks, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 				 ext4_trans_default_revoke_credits(sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 			if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 				err = PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 				ext4_std_error(sb, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 		BUFFER_TRACE(inode_bitmap_bh, "get_write_access");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 		err = ext4_journal_get_write_access(handle, inode_bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 			ext4_std_error(sb, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 		ext4_lock_group(sb, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 		ret2 = ext4_test_and_set_bit(ino, inode_bitmap_bh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 		if (ret2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 			/* Someone already took the bit. Repeat the search
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 			 * with lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 			ret2 = find_inode_bit(sb, group, inode_bitmap_bh, &ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 			if (ret2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 				ext4_set_bit(ino, inode_bitmap_bh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 				ret2 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 				ret2 = 1; /* we didn't grab the inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 		ext4_unlock_group(sb, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 		ino++;		/* the inode bitmap is zero-based */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 		if (!ret2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 			goto got; /* we grabbed the inode! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 		if (ino < EXT4_INODES_PER_GROUP(sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 			goto repeat_in_this_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) next_group:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 		if (++group == ngroups)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 			group = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	err = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) got:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	BUFFER_TRACE(inode_bitmap_bh, "call ext4_handle_dirty_metadata");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	err = ext4_handle_dirty_metadata(handle, NULL, inode_bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 		ext4_std_error(sb, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	BUFFER_TRACE(group_desc_bh, "get_write_access");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	err = ext4_journal_get_write_access(handle, group_desc_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 		ext4_std_error(sb, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	/* We may have to initialize the block bitmap if it isn't already */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	if (ext4_has_group_desc_csum(sb) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	    gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 		struct buffer_head *block_bitmap_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 		block_bitmap_bh = ext4_read_block_bitmap(sb, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 		if (IS_ERR(block_bitmap_bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 			err = PTR_ERR(block_bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 		BUFFER_TRACE(block_bitmap_bh, "get block bitmap access");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 		err = ext4_journal_get_write_access(handle, block_bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 			brelse(block_bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 			ext4_std_error(sb, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 		BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 		err = ext4_handle_dirty_metadata(handle, NULL, block_bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 		/* recheck and clear flag under lock if we still need to */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 		ext4_lock_group(sb, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 		if (ext4_has_group_desc_csum(sb) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 		    (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 			ext4_free_group_clusters_set(sb, gdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 				ext4_free_clusters_after_init(sb, group, gdp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 			ext4_block_bitmap_csum_set(sb, group, gdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 						   block_bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 			ext4_group_desc_csum_set(sb, group, gdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 		ext4_unlock_group(sb, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 		brelse(block_bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 			ext4_std_error(sb, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 	/* Update the relevant bg descriptor fields */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 	if (ext4_has_group_desc_csum(sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 		int free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 		struct ext4_group_info *grp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 		if (!(sbi->s_mount_state & EXT4_FC_REPLAY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 			grp = ext4_get_group_info(sb, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 			down_read(&grp->alloc_sem); /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 						     * protect vs itable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 						     * lazyinit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 						     */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 		ext4_lock_group(sb, group); /* while we modify the bg desc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 		free = EXT4_INODES_PER_GROUP(sb) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 			ext4_itable_unused_count(sb, gdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 		if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 			gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 			free = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 		 * Check the relative inode number against the last used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 		 * relative inode number in this group. if it is greater
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 		 * we need to update the bg_itable_unused count
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 		if (ino > free)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 			ext4_itable_unused_set(sb, gdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 					(EXT4_INODES_PER_GROUP(sb) - ino));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 		if (!(sbi->s_mount_state & EXT4_FC_REPLAY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 			up_read(&grp->alloc_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 		ext4_lock_group(sb, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 	if (S_ISDIR(mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 		ext4_used_dirs_set(sb, gdp, ext4_used_dirs_count(sb, gdp) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 		if (sbi->s_log_groups_per_flex) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 			ext4_group_t f = ext4_flex_group(sbi, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 			atomic_inc(&sbi_array_rcu_deref(sbi, s_flex_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 							f)->used_dirs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	if (ext4_has_group_desc_csum(sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 		ext4_inode_bitmap_csum_set(sb, group, gdp, inode_bitmap_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 					   EXT4_INODES_PER_GROUP(sb) / 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 		ext4_group_desc_csum_set(sb, group, gdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 	ext4_unlock_group(sb, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 		ext4_std_error(sb, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 	percpu_counter_dec(&sbi->s_freeinodes_counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	if (S_ISDIR(mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 		percpu_counter_inc(&sbi->s_dirs_counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	if (sbi->s_log_groups_per_flex) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 		flex_group = ext4_flex_group(sbi, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 		atomic_dec(&sbi_array_rcu_deref(sbi, s_flex_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 						flex_group)->free_inodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 	/* This is the optimal IO size (for stat), not the fs block size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 	inode->i_blocks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 	inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	ei->i_crtime = inode->i_mtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 	memset(ei->i_data, 0, sizeof(ei->i_data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	ei->i_dir_start_lookup = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 	ei->i_disksize = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	/* Don't inherit extent flag from directory, amongst others. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	ei->i_flags =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 		ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 	ei->i_flags |= i_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 	ei->i_file_acl = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	ei->i_dtime = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	ei->i_block_group = group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 	ei->i_last_alloc_group = ~0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 	ext4_set_inode_flags(inode, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 	if (IS_DIRSYNC(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 		ext4_handle_sync(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	if (insert_inode_locked(inode) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 		 * Likely a bitmap corruption causing inode to be allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 		 * twice.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 		err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 		ext4_error(sb, "failed to insert inode %lu: doubly allocated?",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 			   inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 		ext4_mark_group_bitmap_corrupted(sb, group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 					EXT4_GROUP_INFO_IBITMAP_CORRUPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 	inode->i_generation = prandom_u32();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 	/* Precompute checksum seed for inode metadata */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	if (ext4_has_metadata_csum(sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 		__u32 csum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 		__le32 inum = cpu_to_le32(inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 		__le32 gen = cpu_to_le32(inode->i_generation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 		csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 				   sizeof(inum));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 		ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 					      sizeof(gen));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 	ext4_set_inode_state(inode, EXT4_STATE_NEW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 	ei->i_extra_isize = sbi->s_want_extra_isize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 	ei->i_inline_off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 	if (ext4_has_feature_inline_data(sb) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	    (!(ei->i_flags & EXT4_DAX_FL) || S_ISDIR(mode)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 		ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	ret = inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	err = dquot_alloc_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 		goto fail_drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	 * Since the encryption xattr will always be unique, create it first so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 	 * that it's less likely to end up in an external xattr block and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 	 * prevent its deduplication.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 	if (encrypt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 		err = fscrypt_set_context(inode, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 			goto fail_free_drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	if (!(ei->i_flags & EXT4_EA_INODE_FL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 		err = ext4_init_acl(handle, inode, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 			goto fail_free_drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 		err = ext4_init_security(handle, inode, dir, qstr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 			goto fail_free_drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 	if (ext4_has_feature_extents(sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 		/* set extent flag only for directory, file and normal symlink*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 		if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 			ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 			ext4_ext_tree_init(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 	if (ext4_handle_valid(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 		ei->i_sync_tid = handle->h_transaction->t_tid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 		ei->i_datasync_tid = handle->h_transaction->t_tid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 	err = ext4_mark_inode_dirty(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 		ext4_std_error(sb, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 		goto fail_free_drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 	ext4_debug("allocating inode %lu\n", inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 	trace_ext4_allocate_inode(inode, dir, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 	brelse(inode_bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) fail_free_drop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 	dquot_free_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) fail_drop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 	clear_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 	unlock_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 	dquot_drop(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 	inode->i_flags |= S_NOQUOTA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 	brelse(inode_bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 	return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) /* Verify that we are loading a valid orphan from disk */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 	unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 	ext4_group_t block_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 	int bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 	struct buffer_head *bitmap_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 	struct inode *inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 	int err = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	if (ino < EXT4_FIRST_INO(sb) || ino > max_ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 		goto bad_orphan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 	block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 	bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 	bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 	if (IS_ERR(bitmap_bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 		return ERR_CAST(bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 	/* Having the inode bit set should be a 100% indicator that this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 	 * is a valid orphan (no e2fsck run on fs).  Orphans also include
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 	 * inodes that were being truncated, so we can't check i_nlink==0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	if (!ext4_test_bit(bit, bitmap_bh->b_data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 		goto bad_orphan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 	inode = ext4_iget(sb, ino, EXT4_IGET_NORMAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 	if (IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 		err = PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 		ext4_error_err(sb, -err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 			       "couldn't read orphan inode %lu (err %d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 			       ino, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 		brelse(bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 		return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 	 * If the orphans has i_nlinks > 0 then it should be able to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 	 * be truncated, otherwise it won't be removed from the orphan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 	 * list during processing and an infinite loop will result.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 	 * Similarly, it must not be a bad inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 	if ((inode->i_nlink && !ext4_can_truncate(inode)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 	    is_bad_inode(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 		goto bad_orphan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	if (NEXT_ORPHAN(inode) > max_ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 		goto bad_orphan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 	brelse(bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 	return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) bad_orphan:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 	ext4_error(sb, "bad orphan inode %lu", ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 	if (bitmap_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 		printk(KERN_ERR "ext4_test_bit(bit=%d, block=%llu) = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 		       bit, (unsigned long long)bitmap_bh->b_blocknr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 		       ext4_test_bit(bit, bitmap_bh->b_data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 	if (inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 		printk(KERN_ERR "is_bad_inode(inode)=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 		       is_bad_inode(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 		printk(KERN_ERR "NEXT_ORPHAN(inode)=%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 		       NEXT_ORPHAN(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 		printk(KERN_ERR "max_ino=%lu\n", max_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 		printk(KERN_ERR "i_nlink=%u\n", inode->i_nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 		/* Avoid freeing blocks if we got a bad deleted inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 		if (inode->i_nlink == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 			inode->i_blocks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 		iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 	brelse(bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 	return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) unsigned long ext4_count_free_inodes(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 	unsigned long desc_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 	struct ext4_group_desc *gdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) #ifdef EXT4FS_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 	struct ext4_super_block *es;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 	unsigned long bitmap_count, x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 	struct buffer_head *bitmap_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 	es = EXT4_SB(sb)->s_es;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	desc_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 	bitmap_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 	gdp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	for (i = 0; i < ngroups; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 		gdp = ext4_get_group_desc(sb, i, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 		if (!gdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 		desc_count += ext4_free_inodes_count(sb, gdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 		brelse(bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 		bitmap_bh = ext4_read_inode_bitmap(sb, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 		if (IS_ERR(bitmap_bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 			bitmap_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 		x = ext4_count_free(bitmap_bh->b_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 				    EXT4_INODES_PER_GROUP(sb) / 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 		printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 			(unsigned long) i, ext4_free_inodes_count(sb, gdp), x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 		bitmap_count += x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 	brelse(bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 	printk(KERN_DEBUG "ext4_count_free_inodes: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 	       "stored = %u, computed = %lu, %lu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 	       le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 	return desc_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 	desc_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 	for (i = 0; i < ngroups; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 		gdp = ext4_get_group_desc(sb, i, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 		if (!gdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 		desc_count += ext4_free_inodes_count(sb, gdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 	return desc_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) /* Called at mount-time, super-block is locked */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) unsigned long ext4_count_dirs(struct super_block * sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 	unsigned long count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 	for (i = 0; i < ngroups; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 		if (!gdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 		count += ext4_used_dirs_count(sb, gdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503)  * Zeroes not yet zeroed inode table - just write zeroes through the whole
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504)  * inode table. Must be called without any spinlock held. The only place
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505)  * where it is called from on active part of filesystem is ext4lazyinit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506)  * thread, so we do not need any special locks, however we have to prevent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507)  * inode allocation from the current group, so we take alloc_sem lock, to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508)  * block ext4_new_inode() until we are finished.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) int ext4_init_inode_table(struct super_block *sb, ext4_group_t group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 				 int barrier)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 	struct ext4_group_info *grp = ext4_get_group_info(sb, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	struct ext4_sb_info *sbi = EXT4_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 	struct ext4_group_desc *gdp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 	struct buffer_head *group_desc_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 	handle_t *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 	ext4_fsblk_t blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 	int num, ret = 0, used_blks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 	unsigned long used_inos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 	/* This should not happen, but just to be sure check this */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 	if (sb_rdonly(sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 		ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 	gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 	if (!gdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 	 * We do not need to lock this, because we are the only one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	 * handling this flag.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 	if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 	handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 	if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 		ret = PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 	down_write(&grp->alloc_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 	 * If inode bitmap was already initialized there may be some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 	 * used inodes so we need to skip blocks with used inodes in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 	 * inode table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 	if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 		used_inos = EXT4_INODES_PER_GROUP(sb) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 			    ext4_itable_unused_count(sb, gdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 		used_blks = DIV_ROUND_UP(used_inos, sbi->s_inodes_per_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 		/* Bogus inode unused count? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 		if (used_blks < 0 || used_blks > sbi->s_itb_per_group) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 			ext4_error(sb, "Something is wrong with group %u: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 				   "used itable blocks: %d; "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 				   "itable unused count: %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 				   group, used_blks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 				   ext4_itable_unused_count(sb, gdp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 			ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 			goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 		used_inos += group * EXT4_INODES_PER_GROUP(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 		 * Are there some uninitialized inodes in the inode table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 		 * before the first normal inode?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 		if ((used_blks != sbi->s_itb_per_group) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 		     (used_inos < EXT4_FIRST_INO(sb))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 			ext4_error(sb, "Something is wrong with group %u: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 				   "itable unused count: %u; "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 				   "itables initialized count: %ld",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 				   group, ext4_itable_unused_count(sb, gdp),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 				   used_inos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 			ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 			goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 	blk = ext4_inode_table(sb, gdp) + used_blks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 	num = sbi->s_itb_per_group - used_blks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 	BUFFER_TRACE(group_desc_bh, "get_write_access");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 	ret = ext4_journal_get_write_access(handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 					    group_desc_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 	 * Skip zeroout if the inode table is full. But we set the ZEROED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 	 * flag anyway, because obviously, when it is full it does not need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 	 * further zeroing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 	if (unlikely(num == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 		goto skip_zeroout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 	ext4_debug("going to zero out inode table in group %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 		   group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 	ret = sb_issue_zeroout(sb, blk, num, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 	if (barrier)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 		blkdev_issue_flush(sb->s_bdev, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) skip_zeroout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 	ext4_lock_group(sb, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 	gdp->bg_flags |= cpu_to_le16(EXT4_BG_INODE_ZEROED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 	ext4_group_desc_csum_set(sb, group, gdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 	ext4_unlock_group(sb, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 	BUFFER_TRACE(group_desc_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 		     "call ext4_handle_dirty_metadata");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 	ret = ext4_handle_dirty_metadata(handle, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 					 group_desc_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 	up_write(&grp->alloc_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 	ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) }