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