From: Artem Blagodarenko dx_root_limit() assumed that '.' and '..' take 12 bytes each, so the dx_entry array of the root always started 32 bytes into the block. With dirdata, '.' and '..' can be longer. Pass the offset of the dx_entry array to dx_root_limit() instead. Both callers already have the array: dx_probe() found it through dx_get_dx_info(), and make_indexed_dir() has just placed it. The root is not looked up a second time, so the limit always matches the array that was written or read. For a root without dirdata the offset is offsetof(struct dx_root, entries), and the limit is unchanged. Also mask the dirdata flags out of the file type in get_dtype(), so an entry that carries dirdata still reports its type to readdir. Signed-off-by: Artem Blagodarenko Assisted-by: Claude:claude-opus-5-5 --- fs/ext4/ext4.h | 8 +++++--- fs/ext4/namei-test.c | 12 ++++++------ fs/ext4/namei.c | 12 ++++++------ fs/ext4/namei.h | 19 +++++++++++++------ 4 files changed, 30 insertions(+), 21 deletions(-) diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h index aa45735a9bb7..32401150503e 100644 --- a/fs/ext4/ext4.h +++ b/fs/ext4/ext4.h @@ -3108,12 +3108,14 @@ static const unsigned char ext4_filetype_table[] = { DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK }; -static inline unsigned char get_dtype(struct super_block *sb, int filetype) +static inline unsigned char get_dtype(struct super_block *sb, int filetype) { - if (!ext4_has_feature_filetype(sb) || filetype >= EXT4_FT_MAX) + unsigned char fl_index = filetype & EXT4_FT_MASK; + + if (!ext4_has_feature_filetype(sb) || fl_index >= EXT4_FT_MAX) return DT_UNKNOWN; - return ext4_filetype_table[filetype]; + return ext4_filetype_table[fl_index]; } extern int ext4_check_all_de(struct inode *dir, struct buffer_head *bh, void *buf, int buf_size); diff --git a/fs/ext4/namei-test.c b/fs/ext4/namei-test.c index 2a918fd12b05..bc10fdb13928 100644 --- a/fs/ext4/namei-test.c +++ b/fs/ext4/namei-test.c @@ -629,7 +629,7 @@ static void test_namei_dx_helpers_and_csum(struct kunit *test) /* Check limits without METADATA_CSUM (1024B block) */ /* Root: (1024 - 12 - 12 - 8) / 8 = 124 */ KUNIT_EXPECT_EQ(test, - dx_root_limit(dir, sizeof(struct dx_root_info)), 124U); + dx_root_limit(dir, offsetof(struct dx_root, entries)), 124U); /* Node: (1024 - 8) / 8 = 127 */ KUNIT_EXPECT_EQ(test, dx_node_limit(dir), 127U); @@ -637,7 +637,7 @@ static void test_namei_dx_helpers_and_csum(struct kunit *test) ctx->es.s_feature_ro_compat |= cpu_to_le32(EXT4_FEATURE_RO_COMPAT_METADATA_CSUM); KUNIT_EXPECT_EQ(test, - dx_root_limit(dir, sizeof(struct dx_root_info)), 123U); + dx_root_limit(dir, offsetof(struct dx_root, entries)), 123U); KUNIT_EXPECT_EQ(test, dx_node_limit(dir), 126U); /* Set up a valid dx_root in root_bh */ @@ -658,7 +658,7 @@ static void test_namei_dx_helpers_and_csum(struct kunit *test) root->info.info_length = sizeof(struct dx_root_info); root->info.hash_version = DX_HASH_HALF_MD4; entries = root->entries; - dx_set_limit(entries, dx_root_limit(dir, sizeof(struct dx_root_info))); + dx_set_limit(entries, dx_root_limit(dir, offsetof(struct dx_root, entries))); dx_set_count(entries, 2); dx_set_block(entries + 0, 0xf0000001U); /* high 4 bits masked */ dx_set_hash(entries + 1, 0x80001234U); @@ -858,7 +858,7 @@ static void test_namei_read_dirblock(struct kunit *test) root->info.info_length = sizeof(struct dx_root_info); root->info.hash_version = DX_HASH_HALF_MD4; dx_set_limit(root->entries, - dx_root_limit(dir, sizeof(struct dx_root_info))); + dx_root_limit(dir, offsetof(struct dx_root, entries))); dx_set_count(root->entries, 1); dx_set_block(root->entries, 1); ext4_dx_csum_set_test(dir, (struct ext4_dir_entry_2 *)bh->b_data); @@ -1368,7 +1368,7 @@ static void setup_valid_dx_root(struct inode *dir, struct buffer_head *bh) root->info.info_length = sizeof(struct dx_root_info); root->info.hash_version = DX_HASH_HALF_MD4; dx_set_limit(root->entries, - dx_root_limit(dir, sizeof(struct dx_root_info))); + dx_root_limit(dir, offsetof(struct dx_root, entries))); dx_set_count(root->entries, 2); dx_set_block(root->entries + 0, 1); dx_set_hash(root->entries + 1, 0x80000000U); @@ -1626,7 +1626,7 @@ static void test_namei_dx_index_level_growth_and_fallback(struct kunit *test) setup_valid_dx_root(dir, bh0); root = (struct dx_root *)bh0->b_data; - root_limit = dx_root_limit(dir, sizeof(struct dx_root_info)); + root_limit = dx_root_limit(dir, offsetof(struct dx_root, entries)); dx_set_limit(root->entries, root_limit); dx_set_count(root->entries, root_limit); /* Point every index entry at leaf block 1 in ascending hash order */ diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c index edbe033b623e..78ee4936e6cc 100644 --- a/fs/ext4/namei.c +++ b/fs/ext4/namei.c @@ -666,7 +666,7 @@ static struct dx_frame * dx_probe(struct ext4_filename *fname, struct inode *dir, struct dx_hash_info *hinfo, struct dx_frame *frame_in) { - unsigned count, indirect, level, i; + unsigned count, indirect, level, i, limit; struct dx_entry *at, *entries, *p, *q, *m; struct dx_root_info *info; struct dx_frame *frame = frame_in; @@ -749,10 +749,10 @@ dx_probe(struct ext4_filename *fname, struct inode *dir, entries = (struct dx_entry *)(((char *)info) + info->info_length); - if (dx_get_limit(entries) != dx_root_limit(dir, info->info_length)) { + limit = dx_root_limit(dir, (char *)entries - frame->bh->b_data); + if (dx_get_limit(entries) != limit) { ext4_warning_inode(dir, "dx entry: limit %u != root limit %u", - dx_get_limit(entries), - dx_root_limit(dir, info->info_length)); + dx_get_limit(entries), limit); goto fail; } @@ -2224,10 +2224,10 @@ static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname, dx_info->hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version; - entries = (void *)dx_info + sizeof(*dx_info); + entries = (void *)dx_info + dx_info->info_length; dx_set_block(entries, 1); dx_set_count(entries, 1); - dx_set_limit(entries, dx_root_limit(dir, sizeof(*dx_info))); + dx_set_limit(entries, dx_root_limit(dir, (char *)entries - (char *)dot_de)); /* Initialize as for dx_probe */ fname->hinfo.hash_version = dx_info->hash_version; diff --git a/fs/ext4/namei.h b/fs/ext4/namei.h index e66c5d1c2aae..569409c52f58 100644 --- a/fs/ext4/namei.h +++ b/fs/ext4/namei.h @@ -145,15 +145,22 @@ static inline void dx_set_limit(struct dx_entry *entries, unsigned int value) ((struct dx_countlimit *)entries)->limit = cpu_to_le16(value); } -static inline unsigned int dx_root_limit(struct inode *dir, unsigned int infosize) +/* + * Number of dx_entry slots in an htree root block whose dx_entry array starts + * @entries_offset bytes into the block. The offset depends on the length of + * '.' and '..', which can carry dirdata; for a root without dirdata it is + * offsetof(struct dx_root, entries). + */ +static inline unsigned int dx_root_limit(struct inode *dir, + unsigned int entries_offset) { - unsigned int entry_space = dir->i_sb->s_blocksize - - ext4_dir_rec_len(1, NULL) - - ext4_dir_rec_len(2, NULL) - infosize; + unsigned int used = entries_offset; if (ext4_has_feature_metadata_csum(dir->i_sb)) - entry_space -= sizeof(struct dx_tail); - return entry_space / sizeof(struct dx_entry); + used += sizeof(struct dx_tail); + if (used >= dir->i_sb->s_blocksize) + return 0; + return (dir->i_sb->s_blocksize - used) / sizeof(struct dx_entry); } static inline unsigned int __dx_node_limit(struct inode *dir, -- 2.52.0