* [PATCH v15 00/13] Data in direntry (dirdata) feature, with KUnit tests
@ 2026-12-03 18:49 Artem Blagodarenko
2026-12-03 18:49 ` [PATCH v15 01/13] ext4: validate count against limit in ext4_dx_csum_verify/_set Artem Blagodarenko
` (13 more replies)
0 siblings, 14 replies; 26+ messages in thread
From: Artem Blagodarenko @ 2026-12-03 18:49 UTC (permalink / raw)
To: linux-ext4; +Cc: tytso, jack, adilger.kernel, ablagodarenko
The dirdata feature adds a small, self-describing area after the name in
struct ext4_dir_entry_2. The unused high 4 bits of file_type say which
records follow the NUL after the name; each record starts with a 1-byte
length, so a reader can skip records it does not know.
It is for per-name data that has to come back from readdir without
reading the child inode, and has to stay with the name across rename.
An xattr on the child can do neither without an inode lookup per entry,
which is what very large directories cannot afford.
The on-disk format is not new. INCOMPAT_DIRDATA was reserved in ext4 in
2010 (f710b4b96ba2), e2fsprogs knows the feature name, and Lustre servers
have written this format for over 15 years through ldiskfs, a patched
ext4. The series lets upstream ext4 and e2fsck read and write it.
ext4 already keeps one such item after the name: the casefold+fscrypt
hash. This series gives it a proper record (CFHASH) so it can sit next
to others. The first new record is LUFID, a 128-bit file identifier
that must survive rename and be visible to readdir.
Lustre is not meant to stay out of tree: the upstreaming project is
under way [1][2], with the client and server expected to be posted for
review around 2027Q1 [3]. The first server submission only has the
in-memory backend; an ext4 backend needs ext4 changes, which are being
prepared separately so they can be reviewed on their own. dirdata is
one of them, and one of the reasons the Lustre server needs ldiskfs
today.
Patches 01-04 are fixes and cleanups that do not depend on the feature
and can be taken on their own; 02 fixes a bug in current upstream for
casefolded+encrypted directories. Patches 05-12 are the feature, and
13 adds KUnit tests for it.
The KUnit tests are new in this version. They reuse the fixture of the
namei.c tests now in ext4 dev, so the dirdata on-disk format is checked
without a disk image: record parsing and corruption checks, htree roots
with dirdata on '.' and '..', make_indexed_dir() and leaf splits with a
LUFID on every entry, rename and RENAME_EXCHANGE, EXT4_IOC_SET_LUFID,
the casefold+fscrypt hash and the index node limits from patch 02.
They run under UML with
./tools/testing/kunit/kunit.py run --kunitconfig=fs/ext4
e2fsprogs support is in
[PATCH v4 00/13] e2fsprogs: Add support for dirdata feature
and fstests coverage in
[RFC PATCH v6 0/8] ext4: tests for the dirdata feature
Both are unchanged since they were posted with v14.
[1] https://lwn.net/Articles/1025268/
[2] https://wiki.lustre.org/Lustre_Upstreaming_to_Linux_Kernel
[3] "[RFC] Lustre Upstreaming Project Update", linux-fsdevel, 2026-02-23
Changes in v15 (no human review of v14 yet; this addresses the Sashiko
review of v14):
- New patch 13: KUnit tests for dirdata (9 tests), as a second suite,
ext4_dirdata_test, in namei-test.c next to ext4_namei_test.
- Rebased onto ext4.git dev (aceadbccd428), which moved the htree
structures and helpers from namei.c into namei.h (b82144c319f9) and
added KUnit tests for namei.c and dir.c. The series now changes
namei.h, and the tests follow the changed signatures of
ext4_check_dx_root(), dx_release(), dx_root_limit() and
ext4_find_dest_de(). struct dx_root stays in namei.h as the layout of
a root without dirdata, and struct dx_root_info is now a top-level
struct.
- Patches 01 and 04: the count and limit in the "dir seems corrupt"
messages are added in 01, where the messages are introduced, instead
of in 04, which is about ext4_dir_entry_is_tail() (Sashiko). The two
messages now have the same spacing.
- Patch 06: ext4_check_dx_root() checks '.' as well as '..', and bounds
the entry after '..' by the checksum tail, so the length of the
entries make_indexed_dir() moves out cannot underflow (Sashiko on
08/09).
- Patch 07 (was "ext4: add ext4_dir_entry_len()"): dx_root_limit()
takes the offset of the dx_entry array, which both callers already
have, instead of looking the root up again. New title and message,
which now also mention the get_dtype() change (Sashiko).
- Patch 08: when do_split() hands over a half with no room for the new
entry, that half is still dirtied before the error is returned
(Sashiko).
- Patch 09:
* a dirdata record that does not fit in its entry is reported as
corruption by __ext4_check_dir_entry() and dx_get_dx_info(),
instead of reading as "no dirdata"; the length of an entry then
never depends on its current rec_len (Sashiko on 06-09);
* ext4_inlinedir_to_tree() lets ext4_check_dir_entry() report a bad
entry instead of skipping it silently (Sashiko);
* the dirdata copy into struct fname in ext4_htree_store_dirent() is
gone; nothing read it (Sashiko).
* ext4_insert_dentry() and ext4_init_new_dir() are now inline
wrappers, so the test exports name ext4_insert_dentry_data() and
ext4_init_new_dir_data() instead; with CONFIG_EXT4_KUNIT_TESTS=m
modpost failed on the rebased v14.
- Patch 10: a RENAME_WHITEOUT rename that fails before the old entry is
replaced no longer rewrites that entry, which dropped its LUFID
(Sashiko).
- Patch 11: the five readers of the casefold+fscrypt hash go through
one helper, ext4_dirent_hash_get(). It reads the CFHASH record when
there is one, and the fixed place after the name only when the entry
is long enough and carries no other record there. Previously '.'
(12 bytes) and corrupted entries were read 8 bytes past their end
(Sashiko).
- Patch 12 (Sashiko):
* a failed re-add puts the old entry back into the block it was
deleted from, not into whichever block a lookup by name picks;
* fscrypt_prepare_readdir() is called before checking for the key;
* check_sticky() is mapped to -EPERM;
* the inode is checked as ext4_lookup() checks it;
* the idmap is const, following the VFS.
Tested on ext4.git dev:
- every patch builds with W=1, and links vmlinux and modules with the
ext4 KUnit tests as a module; the tip also builds with DX_DEBUG;
- KUnit (./tools/testing/kunit/kunit.py run --kunitconfig=fs/ext4):
571/571 on UML, and the ext4-test module passes on x86_64; each of
three regressions put back by hand (no corruption check in
dx_get_dx_info(), no bound on the legacy hash read, the fixed root
limit) fails its dirdata test;
- in a VM, fstests ext4/024, ext4/064-069 and the casefold and encrypt
groups pass with and without -O dirdata (34 tests each, 4 not run for
missing hardware or tools); 1k-block directories with 250-byte names
and 48-byte LUFIDs split cleanly; RENAME_WHITEOUT keeps the LUFID,
also when it fails with ENOSPC; a multi-level casefolded+encrypted
directory (12000 entries, with and without dirdata) passes e2fsck v4
before and after e2fsck -fD and after more kernel inserts.
v14: https://lore.kernel.org/linux-ext4/20261103134921.610047-1-ablagodarenko@thelustrecollective.com/
v13: https://lore.kernel.org/linux-ext4/20261029122413.230409-1-ablagodarenko@thelustrecollective.com/
v12: https://lore.kernel.org/linux-ext4/20260801181227.5198-1-ablagodarenko@thelustrecollective.com/
Artem Blagodarenko (13):
ext4: validate count against limit in ext4_dx_csum_verify/_set
ext4: use the e2fsck limit for htree index nodes
ext4: replace ext4_dir_entry with ext4_dir_entry_2
ext4: add ext4_dir_entry_is_tail()
ext4: add dirdata format definitions and access helpers
ext4: refactor dx_root to support variable dirent sizes
ext4: size the htree root from where its dx_entry array starts
ext4: rename ext4_dir_rec_len() and clarify dirdata usage
ext4: dirdata feature
ext4: add dirdata LUFID support for directory entry rename
ext4: add dirdata set/get helpers
ext4: Add EXT4_IOC_SET_LUFID ioctl for setting LUFID on directory
entries
ext4: add KUnit tests for dirdata
fs/ext4/dir.c | 25 +-
fs/ext4/ext4.h | 318 ++++++++-
fs/ext4/fast_commit.c | 1 +
fs/ext4/fast_commit.h | 1 +
fs/ext4/inline.c | 47 +-
fs/ext4/ioctl.c | 87 +++
fs/ext4/namei-test.c | 808 +++++++++++++++++++++-
fs/ext4/namei.c | 1326 +++++++++++++++++++++++++++++++------
fs/ext4/namei.h | 72 +-
fs/ext4/sysfs.c | 2 +
include/uapi/linux/ext4.h | 14 +
11 files changed, 2415 insertions(+), 286 deletions(-)
base-commit: aceadbccd428fb8e6593b27a3091d961f80225d1
--
2.52.0
^ permalink raw reply [flat|nested] 26+ messages in thread
* [PATCH v15 01/13] ext4: validate count against limit in ext4_dx_csum_verify/_set
2026-12-03 18:49 [PATCH v15 00/13] Data in direntry (dirdata) feature, with KUnit tests Artem Blagodarenko
@ 2026-12-03 18:49 ` Artem Blagodarenko
2026-12-03 19:03 ` sashiko-bot
2026-12-03 18:49 ` [PATCH v15 02/13] ext4: use the e2fsck limit for htree index nodes Artem Blagodarenko
` (12 subsequent siblings)
13 siblings, 1 reply; 26+ messages in thread
From: Artem Blagodarenko @ 2026-12-03 18:49 UTC (permalink / raw)
To: linux-ext4
Cc: tytso, jack, adilger.kernel, ablagodarenko, xiaowu.417,
Artem Blagodarenko
dx_countlimit's count field was read from disk and used directly to
compute the checksummed range (count_offset + count * sizeof(dx_entry))
without ever being checked against limit -- only limit itself was
bounds-checked against the block size. A corrupted or maliciously
crafted filesystem image that sets count to a large value (e.g. 65535)
makes ext4_chksum() read far past the end of the directory block
buffer, hitting adjacent slab objects.
Reported-by: xiaowu.417@qq.com
Signed-off-by: Artem Blagodarenko <artem.blagodarenko@gmail.com>
Assisted-by: Claude:claude-opus-5-5
---
fs/ext4/namei.c | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index b2753daad49f..1d3b71cd51d6 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -395,6 +395,11 @@ static int ext4_dx_csum_verify(struct inode *inode,
warn_no_space_for_csum(inode);
return 0;
}
+ if (count > limit) {
+ EXT4_ERROR_INODE(inode, "dir seems corrupt (%i > %i)? "
+ "Run e2fsck -D.", count, limit);
+ return 0;
+ }
t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
if (t->dt_checksum != ext4_dx_csum(inode, dirent, count_offset,
@@ -424,6 +429,11 @@ static void ext4_dx_csum_set(struct inode *inode, struct ext4_dir_entry *dirent)
warn_no_space_for_csum(inode);
return;
}
+ if (count > limit) {
+ EXT4_ERROR_INODE(inode, "dir seems corrupt (%i > %i)? "
+ "Run e2fsck -D.", count, limit);
+ return;
+ }
t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
t->dt_checksum = ext4_dx_csum(inode, dirent, count_offset, count, t);
--
2.52.0
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v15 02/13] ext4: use the e2fsck limit for htree index nodes
2026-12-03 18:49 [PATCH v15 00/13] Data in direntry (dirdata) feature, with KUnit tests Artem Blagodarenko
2026-12-03 18:49 ` [PATCH v15 01/13] ext4: validate count against limit in ext4_dx_csum_verify/_set Artem Blagodarenko
@ 2026-12-03 18:49 ` Artem Blagodarenko
2026-12-03 18:49 ` [PATCH v15 03/13] ext4: replace ext4_dir_entry with ext4_dir_entry_2 Artem Blagodarenko
` (11 subsequent siblings)
13 siblings, 0 replies; 26+ messages in thread
From: Artem Blagodarenko @ 2026-12-03 18:49 UTC (permalink / raw)
To: linux-ext4; +Cc: tytso, jack, adilger.kernel, ablagodarenko, Artem Blagodarenko
From: Artem Blagodarenko <artem.blagodarenko@gmail.com>
Since commit 471fbbea7ff7 ("ext4: handle casefolding with encryption"),
dx_node_limit() passes the directory to ext4_dir_rec_len(), which for a
casefolded+encrypted directory reserves 8 bytes for a hash in the fake
dirent at the start of an index node. That dirent never holds a name or
a hash, and e2fsck computes the limit without it. e2fsck also only
treats a block as an index node when its limit has that value.
As a result, on a casefolded+encrypted directory deep enough to have
index nodes, e2fsck reports the index nodes the kernel wrote as corrupt
leaf blocks ("directory corrupted"), and the kernel refuses the index
nodes that e2fsck -D wrote ("dx entry: limit 510 != node limit 509",
4k blocks with metadata_csum).
Compute the limit without the hash, as e2fsck does, and keep accepting
the old value so that directories written by older kernels can still be
read. Older kernels already reject index nodes written by e2fsck -D;
they will reject those written by this kernel in the same way.
Fixes: 471fbbea7ff7 ("ext4: handle casefolding with encryption")
Signed-off-by: Artem Blagodarenko <artem.blagodarenko@gmail.com>
Assisted-by: Claude:claude-opus-5-5
---
fs/ext4/namei.c | 2 +-
fs/ext4/namei.h | 25 +++++++++++++++++++++++--
2 files changed, 24 insertions(+), 3 deletions(-)
diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index 1d3b71cd51d6..1caa041b4286 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -768,7 +768,7 @@ dx_probe(struct ext4_filename *fname, struct inode *dir,
entries = ((struct dx_node *) frame->bh->b_data)->entries;
- if (dx_get_limit(entries) != dx_node_limit(dir)) {
+ if (!dx_node_limit_ok(dir, dx_get_limit(entries))) {
ext4_warning_inode(dir,
"dx entry: limit %u != node limit %u",
dx_get_limit(entries), dx_node_limit(dir));
diff --git a/fs/ext4/namei.h b/fs/ext4/namei.h
index 2539ed588966..41a12410433a 100644
--- a/fs/ext4/namei.h
+++ b/fs/ext4/namei.h
@@ -149,16 +149,37 @@ static inline unsigned int dx_root_limit(struct inode *dir, unsigned int infosiz
return entry_space / sizeof(struct dx_entry);
}
-static inline unsigned int dx_node_limit(struct inode *dir)
+static inline unsigned int __dx_node_limit(struct inode *dir,
+ const struct inode *hash_dir)
{
unsigned int entry_space = dir->i_sb->s_blocksize -
- ext4_dir_rec_len(0, dir);
+ ext4_dir_rec_len(0, hash_dir);
if (ext4_has_feature_metadata_csum(dir->i_sb))
entry_space -= sizeof(struct dx_tail);
return entry_space / sizeof(struct dx_entry);
}
+/*
+ * The fake dirent at the start of an index node never holds a name, so no
+ * room is reserved for a casefold+fscrypt hash in it. This is the limit
+ * e2fsck computes and expects.
+ */
+static inline unsigned int dx_node_limit(struct inode *dir)
+{
+ return __dx_node_limit(dir, NULL);
+}
+
+/*
+ * Older kernels did reserve the hash in casefolded+encrypted directories,
+ * so their index nodes have a limit one smaller. Accept those too.
+ */
+static inline bool dx_node_limit_ok(struct inode *dir, unsigned int limit)
+{
+ return limit == dx_node_limit(dir) ||
+ limit == __dx_node_limit(dir, dir);
+}
+
#if IS_ENABLED(CONFIG_EXT4_KUNIT_TESTS)
struct buffer_head *ext4_append_test(handle_t *handle, struct inode *inode,
ext4_lblk_t *block);
--
2.52.0
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v15 03/13] ext4: replace ext4_dir_entry with ext4_dir_entry_2
2026-12-03 18:49 [PATCH v15 00/13] Data in direntry (dirdata) feature, with KUnit tests Artem Blagodarenko
2026-12-03 18:49 ` [PATCH v15 01/13] ext4: validate count against limit in ext4_dx_csum_verify/_set Artem Blagodarenko
2026-12-03 18:49 ` [PATCH v15 02/13] ext4: use the e2fsck limit for htree index nodes Artem Blagodarenko
@ 2026-12-03 18:49 ` Artem Blagodarenko
2026-12-03 19:33 ` sashiko-bot
2026-12-03 18:49 ` [PATCH v15 04/13] ext4: add ext4_dir_entry_is_tail() Artem Blagodarenko
` (10 subsequent siblings)
13 siblings, 1 reply; 26+ messages in thread
From: Artem Blagodarenko @ 2026-12-03 18:49 UTC (permalink / raw)
To: linux-ext4
Cc: tytso, jack, adilger.kernel, ablagodarenko, Artem Blagodarenko,
Andreas Dilger
From: Artem Blagodarenko <artem.blagodarenko@gmail.com>
Replace remaining uses of struct ext4_dir_entry in namei.c
with struct ext4_dir_entry_2.
The code paths affected by this change already depend on the
filetype feature, so using struct ext4_dir_entry_2 is
appropriate and avoids mixing the two directory entry types
unnecessarily.
This change does not affect support for 16-bit rec_len.
Signed-off-by: Artem Blagodarenko <artem.blagodarenko@gmail.com>
Reviewed-by: Andreas Dilger <adilger@dilger.ca>
---
fs/ext4/ext4.h | 6 ++----
fs/ext4/namei-test.c | 28 ++++++++++++++--------------
fs/ext4/namei.c | 42 +++++++++++++++++++++---------------------
fs/ext4/namei.h | 4 ++--
4 files changed, 39 insertions(+), 41 deletions(-)
diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index dc2c181337a1..ca727888425a 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -2523,10 +2523,8 @@ static inline int ext4_emergency_state(struct super_block *sb)
* Structure of a directory entry
*/
#define EXT4_NAME_LEN 255
-/*
- * Base length of the ext4 directory entry excluding the name length
- */
-#define EXT4_BASE_DIR_LEN (sizeof(struct ext4_dir_entry_2) - EXT4_NAME_LEN)
+/* offsetof avoids sizeof()+padding giving 9 instead of the correct on-disk 8 */
+#define EXT4_BASE_DIR_LEN offsetof(struct ext4_dir_entry_2, name)
struct ext4_dir_entry {
__le32 inode; /* Inode number */
diff --git a/fs/ext4/namei-test.c b/fs/ext4/namei-test.c
index f0192135fcf2..30cba531a621 100644
--- a/fs/ext4/namei-test.c
+++ b/fs/ext4/namei-test.c
@@ -670,26 +670,26 @@ static void test_namei_dx_helpers_and_csum(struct kunit *test)
KUNIT_EXPECT_EQ(test, dx_get_hash(entries + 1), 0x80001234U);
KUNIT_EXPECT_EQ(test, dx_get_block(entries + 1), 2U);
- ext4_dx_csum_set_test(dir, (struct ext4_dir_entry *)root_bh->b_data);
+ ext4_dx_csum_set_test(dir, (struct ext4_dir_entry_2 *)root_bh->b_data);
KUNIT_EXPECT_EQ(test,
ext4_dx_csum_verify_test(dir,
- (struct ext4_dir_entry *)root_bh->b_data), 1);
+ (struct ext4_dir_entry_2 *)root_bh->b_data), 1);
/* Corrupt hash -> csum verify fails */
dx_set_hash(entries + 1, 0x80001235U);
KUNIT_EXPECT_EQ(test,
ext4_dx_csum_verify_test(dir,
- (struct ext4_dir_entry *)root_bh->b_data), 0);
+ (struct ext4_dir_entry_2 *)root_bh->b_data), 0);
dx_set_hash(entries + 1, 0x80001234U);
KUNIT_EXPECT_EQ(test,
ext4_dx_csum_verify_test(dir,
- (struct ext4_dir_entry *)root_bh->b_data), 1);
+ (struct ext4_dir_entry_2 *)root_bh->b_data), 1);
/* Overflow limit -> no space for csum fails */
dx_set_limit(entries, 200);
KUNIT_EXPECT_EQ(test,
ext4_dx_csum_verify_test(dir,
- (struct ext4_dir_entry *)root_bh->b_data), 0);
+ (struct ext4_dir_entry_2 *)root_bh->b_data), 0);
/* Set up a valid dx_node in node_bh */
node = (struct dx_node *)node_bh->b_data;
@@ -700,16 +700,16 @@ static void test_namei_dx_helpers_and_csum(struct kunit *test)
dx_set_hash(node->entries + 1, 0x40000000U);
dx_set_block(node->entries + 1, 4);
- ext4_dx_csum_set_test(dir, (struct ext4_dir_entry *)node_bh->b_data);
+ ext4_dx_csum_set_test(dir, (struct ext4_dir_entry_2 *)node_bh->b_data);
KUNIT_EXPECT_EQ(test,
ext4_dx_csum_verify_test(dir,
- (struct ext4_dir_entry *)node_bh->b_data), 1);
+ (struct ext4_dir_entry_2 *)node_bh->b_data), 1);
/* Corrupt fake.rec_len -> get_dx_countlimit returns NULL */
node->fake.rec_len = ext4_rec_len_to_disk(12, TEST_BLK_SIZE);
KUNIT_EXPECT_EQ(test,
ext4_dx_csum_verify_test(dir,
- (struct ext4_dir_entry *)node_bh->b_data), 0);
+ (struct ext4_dir_entry_2 *)node_bh->b_data), 0);
}
/*
@@ -861,7 +861,7 @@ static void test_namei_read_dirblock(struct kunit *test)
dx_root_limit(dir, sizeof(struct dx_root_info)));
dx_set_count(root->entries, 1);
dx_set_block(root->entries, 1);
- ext4_dx_csum_set_test(dir, (struct ext4_dir_entry *)bh->b_data);
+ ext4_dx_csum_set_test(dir, (struct ext4_dir_entry_2 *)bh->b_data);
if (p->id == RDB_BAD_INDEX_CSUM)
dx_set_block(root->entries, 99);
clear_buffer_verified(bh);
@@ -1635,7 +1635,7 @@ static void test_namei_dx_index_level_growth_and_fallback(struct kunit *test)
dx_set_hash(root->entries + i, (u32)i * 0x02000000U);
dx_set_block(root->entries + i, 1);
}
- ext4_dx_csum_set_test(dir, (struct ext4_dir_entry *)bh0->b_data);
+ ext4_dx_csum_set_test(dir, (struct ext4_dir_entry_2 *)bh0->b_data);
/* Fill leaf block 1 completely so adding one more entry splits it */
KUNIT_ASSERT_EQ(test,
@@ -1703,7 +1703,7 @@ static void test_namei_dx_index_level_growth_and_fallback(struct kunit *test)
dx_set_block(node->entries + i, 1);
}
ext4_dx_csum_set_test(dir,
- (struct ext4_dir_entry *)bh2->b_data);
+ (struct ext4_dir_entry_2 *)bh2->b_data);
/* Refill leaf block 1 to capacity */
for (i = 60; i < 120; i++) {
@@ -1744,7 +1744,7 @@ static void test_namei_dx_index_level_growth_and_fallback(struct kunit *test)
dx_set_block(root->entries + i, 2);
}
ext4_dx_csum_set_test(dir,
- (struct ext4_dir_entry *)bh0->b_data);
+ (struct ext4_dir_entry_2 *)bh0->b_data);
init_mock_dentry(&child_dentry, &parent_dentry,
"max_level_enospc", file1, &ctx->sb);
KUNIT_EXPECT_EQ(test,
@@ -1759,7 +1759,7 @@ static void test_namei_dx_index_level_growth_and_fallback(struct kunit *test)
dx_set_count(root->entries, 1);
dx_set_block(root->entries + 0, 2);
ext4_dx_csum_set_test(dir,
- (struct ext4_dir_entry *)bh0->b_data);
+ (struct ext4_dir_entry_2 *)bh0->b_data);
init_mock_dentry(&child_dentry, &parent_dentry,
"interior_node_split", file1, &ctx->sb);
KUNIT_ASSERT_EQ(test,
@@ -1775,7 +1775,7 @@ static void test_namei_dx_index_level_growth_and_fallback(struct kunit *test)
* EXT4_INODE_INDEX and returns -EFSCORRUPTED.
*/
root->info.hash_version = 99;
- ext4_dx_csum_set_test(dir, (struct ext4_dir_entry *)bh0->b_data);
+ ext4_dx_csum_set_test(dir, (struct ext4_dir_entry_2 *)bh0->b_data);
init_mock_dentry(&child_dentry, &parent_dentry, "after_corrupt",
file1, &ctx->sb);
KUNIT_EXPECT_EQ(test,
diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index 1caa041b4286..8fc3cdbbfd64 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -104,7 +104,7 @@ static struct buffer_head *ext4_append(handle_t *handle,
}
static int ext4_dx_csum_verify(struct inode *inode,
- struct ext4_dir_entry *dirent);
+ struct ext4_dir_entry_2 *dirent);
#define ext4_read_dirblock(inode, block, type) \
__ext4_read_dirblock((inode), (block), (type), __func__, __LINE__)
@@ -116,7 +116,7 @@ static struct buffer_head *__ext4_read_dirblock(struct inode *inode,
unsigned int line)
{
struct buffer_head *bh;
- struct ext4_dir_entry *dirent;
+ struct ext4_dir_entry_2 *dirent;
int is_dx_block = 0;
if (block >= inode->i_size >> inode->i_blkbits) {
@@ -148,7 +148,7 @@ static struct buffer_head *__ext4_read_dirblock(struct inode *inode,
}
if (!bh)
return NULL;
- dirent = (struct ext4_dir_entry *) bh->b_data;
+ dirent = (struct ext4_dir_entry_2 *) bh->b_data;
/* Determine whether or not we have an index block */
if (is_dx(inode)) {
if (block == 0)
@@ -233,13 +233,13 @@ static struct ext4_dir_entry_tail *get_dirent_tail(struct inode *inode,
int blocksize = EXT4_BLOCK_SIZE(inode->i_sb);
#ifdef PARANOID
- struct ext4_dir_entry *d, *top;
+ struct ext4_dir_entry_2 *d, *top;
- d = (struct ext4_dir_entry *)bh->b_data;
- top = (struct ext4_dir_entry *)(bh->b_data +
+ d = (struct ext4_dir_entry_2 *)bh->b_data;
+ top = (struct ext4_dir_entry_2 *)(bh->b_data +
(blocksize - sizeof(struct ext4_dir_entry_tail)));
while (d < top && ext4_rec_len_from_disk(d->rec_len, blocksize))
- d = (struct ext4_dir_entry *)(((void *)d) +
+ d = (struct ext4_dir_entry_2 *)(((void *)d) +
ext4_rec_len_from_disk(d->rec_len, blocksize));
if (d != top)
@@ -328,22 +328,22 @@ int ext4_handle_dirty_dirblock(handle_t *handle,
EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_handle_dirty_dirblock);
static struct dx_countlimit *get_dx_countlimit(struct inode *inode,
- struct ext4_dir_entry *dirent,
+ struct ext4_dir_entry_2 *dirent,
int *offset)
{
- struct ext4_dir_entry *dp;
+ struct ext4_dir_entry_2 *de;
struct dx_root_info *root;
int count_offset;
int blocksize = EXT4_BLOCK_SIZE(inode->i_sb);
unsigned int rlen = ext4_rec_len_from_disk(dirent->rec_len, blocksize);
if (rlen == blocksize)
- count_offset = 8;
+ count_offset = sizeof(struct dx_node);
else if (rlen == 12) {
- dp = (struct ext4_dir_entry *)(((void *)dirent) + 12);
- if (ext4_rec_len_from_disk(dp->rec_len, blocksize) != blocksize - 12)
+ de = (struct ext4_dir_entry_2 *)(((void *)dirent) + 12);
+ if (ext4_rec_len_from_disk(de->rec_len, blocksize) != blocksize - 12)
return NULL;
- root = (struct dx_root_info *)(((void *)dp + 12));
+ root = (struct dx_root_info *)(((void *)de + 12));
if (root->reserved_zero ||
root->info_length != sizeof(struct dx_root_info))
return NULL;
@@ -356,7 +356,7 @@ static struct dx_countlimit *get_dx_countlimit(struct inode *inode,
return (struct dx_countlimit *)(((void *)dirent) + count_offset);
}
-static __le32 ext4_dx_csum(struct inode *inode, struct ext4_dir_entry *dirent,
+static __le32 ext4_dx_csum(struct inode *inode, struct ext4_dir_entry_2 *dirent,
int count_offset, int count, struct dx_tail *t)
{
struct ext4_inode_info *ei = EXT4_I(inode);
@@ -374,7 +374,7 @@ static __le32 ext4_dx_csum(struct inode *inode, struct ext4_dir_entry *dirent,
}
static int ext4_dx_csum_verify(struct inode *inode,
- struct ext4_dir_entry *dirent)
+ struct ext4_dir_entry_2 *dirent)
{
struct dx_countlimit *c;
struct dx_tail *t;
@@ -408,7 +408,7 @@ static int ext4_dx_csum_verify(struct inode *inode,
return 1;
}
-static void ext4_dx_csum_set(struct inode *inode, struct ext4_dir_entry *dirent)
+static void ext4_dx_csum_set(struct inode *inode, struct ext4_dir_entry_2 *dirent)
{
struct dx_countlimit *c;
struct dx_tail *t;
@@ -443,7 +443,7 @@ static inline int ext4_handle_dirty_dx_node(handle_t *handle,
struct inode *inode,
struct buffer_head *bh)
{
- ext4_dx_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
+ ext4_dx_csum_set(inode, (struct ext4_dir_entry_2 *)bh->b_data);
return ext4_handle_dirty_metadata(handle, inode, bh);
}
@@ -1348,7 +1348,7 @@ int ext4_search_dir(struct buffer_head *bh, char *search_buf, int buf_size,
EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_search_dir);
static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,
- struct ext4_dir_entry *de)
+ struct ext4_dir_entry_2 *de)
{
struct super_block *sb = dir->i_sb;
@@ -1479,7 +1479,7 @@ static struct buffer_head *__ext4_find_entry(struct inode *dir,
}
if (!buffer_verified(bh) &&
!is_dx_internal_node(dir, block,
- (struct ext4_dir_entry *)bh->b_data) &&
+ (struct ext4_dir_entry_2 *)bh->b_data) &&
!ext4_dirblock_csum_verify(dir, bh)) {
EXT4_ERROR_INODE_ERR(dir, EFSBADCRC,
"checksumming directory "
@@ -4136,14 +4136,14 @@ struct buffer_head *ext4_read_dirblock_test(struct inode *inode,
}
EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_read_dirblock_test);
-void ext4_dx_csum_set_test(struct inode *inode, struct ext4_dir_entry *dirent)
+void ext4_dx_csum_set_test(struct inode *inode, struct ext4_dir_entry_2 *dirent)
{
ext4_dx_csum_set(inode, dirent);
}
EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_dx_csum_set_test);
int ext4_dx_csum_verify_test(struct inode *inode,
- struct ext4_dir_entry *dirent)
+ struct ext4_dir_entry_2 *dirent)
{
return ext4_dx_csum_verify(inode, dirent);
}
diff --git a/fs/ext4/namei.h b/fs/ext4/namei.h
index 41a12410433a..d5c261d0faf3 100644
--- a/fs/ext4/namei.h
+++ b/fs/ext4/namei.h
@@ -186,9 +186,9 @@ struct buffer_head *ext4_append_test(handle_t *handle, struct inode *inode,
struct buffer_head *ext4_read_dirblock_test(struct inode *inode,
ext4_lblk_t block,
enum dirblock_type type);
-void ext4_dx_csum_set_test(struct inode *inode, struct ext4_dir_entry *dirent);
+void ext4_dx_csum_set_test(struct inode *inode, struct ext4_dir_entry_2 *dirent);
int ext4_dx_csum_verify_test(struct inode *inode,
- struct ext4_dir_entry *dirent);
+ struct ext4_dir_entry_2 *dirent);
struct dx_frame *dx_probe_test(struct ext4_filename *fname, struct inode *dir,
struct dx_hash_info *hinfo,
struct dx_frame *frame_in);
--
2.52.0
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v15 04/13] ext4: add ext4_dir_entry_is_tail()
2026-12-03 18:49 [PATCH v15 00/13] Data in direntry (dirdata) feature, with KUnit tests Artem Blagodarenko
` (2 preceding siblings ...)
2026-12-03 18:49 ` [PATCH v15 03/13] ext4: replace ext4_dir_entry with ext4_dir_entry_2 Artem Blagodarenko
@ 2026-12-03 18:49 ` Artem Blagodarenko
2026-12-03 18:49 ` [PATCH v15 05/13] ext4: add dirdata format definitions and access helpers Artem Blagodarenko
` (9 subsequent siblings)
13 siblings, 0 replies; 26+ messages in thread
From: Artem Blagodarenko @ 2026-12-03 18:49 UTC (permalink / raw)
To: linux-ext4
Cc: tytso, jack, adilger.kernel, ablagodarenko, Artem Blagodarenko,
Andreas Dilger
From: Artem Blagodarenko <artem.blagodarenko@gmail.com>
Replace open-coded checks for directory tail entries with a call
to ext4_dir_entry_is_tail(). This helper will also be used by
upcoming changes.
Signed-off-by: Artem Blagodarenko <artem.blagodarenko@gmail.com>
Reviewed-by: Andreas Dilger <adilger@dilger.ca>
---
fs/ext4/ext4.h | 16 ++++++++++++++++
fs/ext4/namei.c | 8 ++------
2 files changed, 18 insertions(+), 6 deletions(-)
diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index ca727888425a..a7fc2c2c98e1 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -4362,6 +4362,22 @@ static inline void ext4_clear_io_unwritten_flag(ext4_io_end_t *io_end)
io_end->flag &= ~EXT4_IO_END_UNWRITTEN;
}
+/*
+ * ext4_dir_entry_is_tail() - Check if a directory entry is a tail entry.
+ * @de: directory entry to check
+ *
+ * Returns true if @de is a directory block tail entry (checksum record).
+ */
+static inline bool ext4_dir_entry_is_tail(struct ext4_dir_entry_2 *de)
+{
+ struct ext4_dir_entry_tail *t = (struct ext4_dir_entry_tail *)de;
+
+ return !t->det_reserved_zero1 &&
+ le16_to_cpu(t->det_rec_len) == sizeof(*t) &&
+ !t->det_reserved_zero2 &&
+ t->det_reserved_ft == EXT4_FT_DIR_CSUM;
+}
+
DECLARE_IOMAP_ITER_NEXT(ext4_iomap_next);
DECLARE_IOMAP_ITER_NEXT(ext4_iomap_next_report);
DECLARE_IOMAP_ITER_NEXT(ext4_iomap_buffered_write_next);
diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index 8fc3cdbbfd64..8fc39c91b6ef 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -247,14 +247,10 @@ static struct ext4_dir_entry_tail *get_dirent_tail(struct inode *inode,
t = (struct ext4_dir_entry_tail *)d;
#else
- t = EXT4_DIRENT_TAIL(bh->b_data, EXT4_BLOCK_SIZE(inode->i_sb));
+ t = EXT4_DIRENT_TAIL(bh->b_data, blocksize);
#endif
- if (t->det_reserved_zero1 ||
- (ext4_rec_len_from_disk(t->det_rec_len, blocksize) !=
- sizeof(struct ext4_dir_entry_tail)) ||
- t->det_reserved_zero2 ||
- t->det_reserved_ft != EXT4_FT_DIR_CSUM)
+ if (!ext4_dir_entry_is_tail((struct ext4_dir_entry_2 *)t))
return NULL;
return t;
--
2.52.0
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v15 05/13] ext4: add dirdata format definitions and access helpers
2026-12-03 18:49 [PATCH v15 00/13] Data in direntry (dirdata) feature, with KUnit tests Artem Blagodarenko
` (3 preceding siblings ...)
2026-12-03 18:49 ` [PATCH v15 04/13] ext4: add ext4_dir_entry_is_tail() Artem Blagodarenko
@ 2026-12-03 18:49 ` Artem Blagodarenko
2026-12-03 19:59 ` sashiko-bot
2026-12-03 18:49 ` [PATCH v15 06/13] ext4: refactor dx_root to support variable dirent sizes Artem Blagodarenko
` (8 subsequent siblings)
13 siblings, 1 reply; 26+ messages in thread
From: Artem Blagodarenko @ 2026-12-03 18:49 UTC (permalink / raw)
To: linux-ext4
Cc: tytso, jack, adilger.kernel, ablagodarenko, Pravin Shelar,
Artem Blagodarenko
Define the on-disk format for ext4 directory entry extension data.
The upper four bits of de->file_type indicate the presence of
optional data stored after the filename NUL terminator. This patch
defines flags for LUFID, 64-bit inode numbers, and casefold hash
data stored in that area.
Add struct ext4_dirent_data_header to describe variable-length
extension records and struct ext4_dirent_hash for hash storage used
by casefold and fscrypt.
Provide ext4_dirdata_next() to advance to the next extension record
and ext4_dirent_get_data_len() to compute the total extension data
length associated with a directory entry.
No functional changes.
Signed-off-by: Pravin Shelar <pravin.shelar@sun.com>
Signed-off-by: Artem Blagodarenko <artem.blagodarenko@gmail.com>
Assisted-by: Claude:claude-opus-5-5
---
fs/ext4/ext4.h | 123 +++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 123 insertions(+)
diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index a7fc2c2c98e1..a75a8cedd52f 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -2605,6 +2605,55 @@ struct ext4_dir_entry_tail {
#define EXT4_FT_SYMLINK 7
#define EXT4_FT_MAX 8
+#define EXT4_FT_MASK 0xf
+
+#if EXT4_FT_MAX > EXT4_FT_MASK
+#error "conflicting EXT4_FT_MAX and EXT4_FT_MASK"
+#endif
+
+/*
+ * d_type has 4 unused bits, so it can hold four types of data. These different
+ * types of data (e.g. fscypt hash, high 32 bits of 64-bit inode number) can be
+ * stored, in flag order, after file-name in ext4 dirent.
+ *
+ * These flags are added to d_type if ext4 dirent has extra data after
+ * filename. This data length is variable and length is stored in first byte
+ * of data. Data starts after filename NUL byte.
+ */
+#define EXT4_DIRENT_LUFID 0x10
+#define EXT4_DIRENT_INO64 0x20
+#define EXT4_DIRENT_CFHASH 0x40
+
+struct ext4_fid {
+ char fid[16]; /* 128-bit unique file identifier */
+};
+
+struct ext4_dirent_data_header {
+ /* length of this header + the whole data blob */
+ __u8 ddh_length;
+} __packed;
+
+struct ext4_dirent_fid {
+ struct ext4_dirent_data_header df_header;
+ struct ext4_fid df_fid[];
+};
+
+#define EXT4_LUFID_MAGIC 0xAD200907UL
+struct ext4_dentry_param {
+ __u32 edp_magic; /* EXT4_LUFID_MAGIC */
+ __u8 edp_dfid[]; /* struct ext4_dirent_fid payload */
+};
+
+/*
+ * The hash words are declared as scalars rather than as a nested
+ * struct ext4_dir_entry_hash: a 4-byte aligned struct inside a packed
+ * struct makes clang warn about unaligned access (-Wunaligned-access).
+ */
+struct ext4_dirent_hash {
+ struct ext4_dirent_data_header dh_header;
+ __le32 dh_hash;
+ __le32 dh_minor_hash;
+} __packed;
#define EXT4_FT_DIR_CSUM 0xDE
@@ -4378,6 +4427,80 @@ static inline bool ext4_dir_entry_is_tail(struct ext4_dir_entry_2 *de)
t->det_reserved_ft == EXT4_FT_DIR_CSUM;
}
+/*
+ * Advance to the next dirdata record header starting from @ddh.
+ *
+ * Uses READ_ONCE() on ddh_length to prevent the compiler from re-fetching this
+ * on-disk field and to guard against a concurrent write racing with any bounds
+ * check the caller performs on the returned pointer.
+ */
+static inline struct ext4_dirent_data_header *
+ext4_dirdata_next(struct ext4_dirent_data_header *ddh)
+{
+ return (struct ext4_dirent_data_header *)
+ ((char *)ddh + READ_ONCE(ddh->ddh_length));
+}
+
+/*
+ * ext4_dirent_get_data_len() - Compute the total dirdata length for an entry.
+ * @de: directory entry
+ * @rec_len: the record length of the directory entry (decoded)
+ *
+ * Computes the length of optional data stored after the filename (and its
+ * implicit NUL terminator). Each extension is indicated by a bit in the
+ * high 4 bits of de->file_type; the first byte of each extension is its
+ * length (including that length byte itself).
+ *
+ * Returns 0 for tail entries and for entries with no dirdata.
+ */
+static inline int ext4_dirent_get_data_len(struct ext4_dir_entry_2 *de,
+ unsigned int rec_len)
+{
+ __u8 extra_data_flags;
+ struct ext4_dirent_data_header *ddh;
+ int dlen = 0;
+ unsigned int offset;
+
+ if (ext4_dir_entry_is_tail(de))
+ return 0;
+
+ extra_data_flags = (de->file_type & ~EXT4_FT_MASK) >> 4;
+ /* offset from start of entry to after filename + NUL */
+ offset = EXT4_BASE_DIR_LEN + de->name_len + 1;
+
+ /* bounds check: ensure we start reading within the entry */
+ if (offset >= rec_len)
+ return 0;
+
+ ddh = (struct ext4_dirent_data_header *)((char *)de + offset);
+
+ while (extra_data_flags) {
+ if (extra_data_flags & 1) {
+ /* bounds check before reading ddh_length */
+ if (offset + sizeof(*ddh) > rec_len)
+ return 0;
+
+ /* Use READ_ONCE to prevent the compiler from re-fetching
+ * this disk field and to guard against a concurrent
+ * write racing with our bounds check below (TOCTOU). */
+ {
+ u8 ddh_len = READ_ONCE(ddh->ddh_length);
+
+ if (ddh_len == 0 || ddh_len > rec_len - offset)
+ return 0;
+ dlen += ddh_len + (dlen == 0);
+ offset += ddh_len;
+ /* advance using the already-validated local copy
+ * to avoid re-fetching ddh->ddh_length */
+ ddh = (struct ext4_dirent_data_header *)
+ ((char *)ddh + ddh_len);
+ }
+ }
+ extra_data_flags >>= 1;
+ }
+ return dlen;
+}
+
DECLARE_IOMAP_ITER_NEXT(ext4_iomap_next);
DECLARE_IOMAP_ITER_NEXT(ext4_iomap_next_report);
DECLARE_IOMAP_ITER_NEXT(ext4_iomap_buffered_write_next);
--
2.52.0
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v15 06/13] ext4: refactor dx_root to support variable dirent sizes
2026-12-03 18:49 [PATCH v15 00/13] Data in direntry (dirdata) feature, with KUnit tests Artem Blagodarenko
` (4 preceding siblings ...)
2026-12-03 18:49 ` [PATCH v15 05/13] ext4: add dirdata format definitions and access helpers Artem Blagodarenko
@ 2026-12-03 18:49 ` Artem Blagodarenko
2026-12-03 20:21 ` sashiko-bot
2026-12-03 18:49 ` [PATCH v15 07/13] ext4: size the htree root from where its dx_entry array starts Artem Blagodarenko
` (7 subsequent siblings)
13 siblings, 1 reply; 26+ messages in thread
From: Artem Blagodarenko @ 2026-12-03 18:49 UTC (permalink / raw)
To: linux-ext4
Cc: tytso, jack, adilger.kernel, ablagodarenko, Artem Blagodarenko,
Pravin Shelar
From: Artem Blagodarenko <artem.blagodarenko@gmail.com>
Split monolithic definition of dx_root struct to separate dx_root_info
from fake struct ext4_dir_entry2 for improved code readability.
This allows "." and ".." dirents to have different sizes if necessary,
since we can't assume the rec_len 12 if dx_root dirents have dirdata.
Adds dx_get_dx_info() accessor instead of complex typecast at callers.
Also add the ext4_dir_entry_len() helper, which dx_get_dx_info() uses
to step over the ".." entry now that it may carry dirdata.
dx_get_dx_info() finds ".." through the rec_len of ".", as the other
users of the root block do, and checks that the root has room for
dx_root_info, two dx_entry slots and the dx_tail. make_indexed_dir()
calls it before it changes the directory, so a failure leaves the
directory as it was.
Signed-off-by: Pravin Shelar <pravin.shelar@sun.com>
Signed-off-by: Artem Blagodarenko <artem.blagodarenko@gmail.com>
Assisted-by: Claude:claude-opus-5-5
---
fs/ext4/ext4.h | 31 ++++++
fs/ext4/namei-test.c | 2 +-
fs/ext4/namei.c | 219 ++++++++++++++++++++++++++++++-------------
fs/ext4/namei.h | 23 +++--
4 files changed, 199 insertions(+), 76 deletions(-)
diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index a75a8cedd52f..aa45735a9bb7 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -4501,6 +4501,37 @@ static inline int ext4_dirent_get_data_len(struct ext4_dir_entry_2 *de,
return dlen;
}
+/*
+ * ext4_dir_entry_len() - Compute the required rec_len for a directory entry.
+ * @de: directory entry (used to read name_len and any dirdata length)
+ * @blocksize: size of the buffer @de lives in (the real directory block
+ * size, or the smaller inline-data buffer size for inline
+ * directories) -- used only to decode @de->rec_len's "0/65535
+ * means rest of buffer" sentinel correctly.
+ * @dir: directory inode (may be NULL for '.' and '..' entries, which
+ * never carry the casefold+fscrypt hash regardless of the
+ * directory's feature flags)
+ *
+ * Returns the minimum record length needed to hold @de, rounded up to the
+ * directory alignment and including room for the casefold+fscrypt hash if
+ * the directory requires it. An entry that carries the hash as a CFHASH
+ * record has it counted in its dirdata length. Any other entry in such a
+ * directory may hold the hash at the fixed offset after the name, which is
+ * where readers look when there is no valid CFHASH record, so the result
+ * always covers that too.
+ */
+static inline unsigned int ext4_dir_entry_len(struct ext4_dir_entry_2 *de,
+ unsigned int blocksize,
+ const struct inode *dir)
+{
+ unsigned int rec_len = ext4_rec_len_from_disk(de->rec_len, blocksize);
+ unsigned int dirdata = ext4_dirent_get_data_len(de, rec_len);
+
+ return max(ext4_dir_rec_len(de->name_len + dirdata, NULL),
+ ext4_dir_rec_len(de->name_len, dir));
+}
+
+
DECLARE_IOMAP_ITER_NEXT(ext4_iomap_next);
DECLARE_IOMAP_ITER_NEXT(ext4_iomap_next_report);
DECLARE_IOMAP_ITER_NEXT(ext4_iomap_buffered_write_next);
diff --git a/fs/ext4/namei-test.c b/fs/ext4/namei-test.c
index 30cba531a621..2a918fd12b05 100644
--- a/fs/ext4/namei-test.c
+++ b/fs/ext4/namei-test.c
@@ -1493,7 +1493,7 @@ static void test_namei_check_dx_root_and_probe(struct kunit *test)
ext4_htree_next_block_test(dir, 0x40000000U, frame,
frames, NULL), 0);
}
- dx_release_test(frames);
+ dx_release_test(dir, frames);
}
/*
diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index 8fc39c91b6ef..edbe033b623e 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -443,6 +443,46 @@ static inline int ext4_handle_dirty_dx_node(handle_t *handle,
return ext4_handle_dirty_metadata(handle, inode, bh);
}
+/*
+ * dx_get_dx_info() - locate the dx_root_info in an htree root block
+ * @dir: directory inode
+ * @de_buf: start of the root block (the '.' entry)
+ *
+ * '..' is found through the rec_len of '.', as every other user of the
+ * root block does. The rec_len of '..' covers the rest of the block, so
+ * dx_root_info starts at the real length of '..'. '.' and '..' never carry
+ * the casefold+fscrypt hash, so their length is computed with a NULL @dir.
+ * The root must have room for dx_root_info, two dx_entry slots (the first
+ * split needs them) and, with metadata_csum, the dx_tail.
+ */
+static struct dx_root_info *dx_get_dx_info(struct inode *dir, void *de_buf)
+{
+ unsigned int blocksize = dir->i_sb->s_blocksize;
+ struct ext4_dir_entry_2 *de = de_buf;
+ unsigned int off, rlen, need;
+
+ rlen = ext4_rec_len_from_disk(de->rec_len, blocksize);
+ if (rlen < EXT4_BASE_DIR_LEN || rlen + EXT4_BASE_DIR_LEN > blocksize ||
+ ext4_dir_entry_len(de, blocksize, NULL) > rlen)
+ return ERR_PTR(-EFSCORRUPTED);
+ off = rlen;
+
+ de = (struct ext4_dir_entry_2 *)((char *)de_buf + off);
+ rlen = ext4_rec_len_from_disk(de->rec_len, blocksize);
+ if (rlen < EXT4_BASE_DIR_LEN || off + rlen > blocksize ||
+ ext4_dir_entry_len(de, blocksize, NULL) > rlen)
+ return ERR_PTR(-EFSCORRUPTED);
+ off += ext4_dir_entry_len(de, blocksize, NULL);
+
+ need = sizeof(struct dx_root_info) + 2 * sizeof(struct dx_entry);
+ if (ext4_has_feature_metadata_csum(dir->i_sb))
+ need += sizeof(struct dx_tail);
+ if (off + need > blocksize)
+ return ERR_PTR(-EFSCORRUPTED);
+
+ return (struct dx_root_info *)((char *)de_buf + off);
+}
+
/*
* Debug
*/
@@ -628,7 +668,7 @@ dx_probe(struct ext4_filename *fname, struct inode *dir,
{
unsigned count, indirect, level, i;
struct dx_entry *at, *entries, *p, *q, *m;
- struct dx_root *root;
+ struct dx_root_info *info;
struct dx_frame *frame = frame_in;
struct dx_frame *ret_err = ERR_PTR(ERR_BAD_DX_DIR);
u32 hash;
@@ -640,23 +680,31 @@ dx_probe(struct ext4_filename *fname, struct inode *dir,
if (IS_ERR(frame->bh))
return (struct dx_frame *) frame->bh;
- root = (struct dx_root *) frame->bh->b_data;
- if (root->info.hash_version != DX_HASH_TEA &&
- root->info.hash_version != DX_HASH_HALF_MD4 &&
- root->info.hash_version != DX_HASH_LEGACY &&
- root->info.hash_version != DX_HASH_SIPHASH) {
- ext4_warning_inode(dir, "Unrecognised inode hash code %u",
- root->info.hash_version);
+ info = dx_get_dx_info(dir, (struct ext4_dir_entry_2 *)frame->bh->b_data);
+ if (IS_ERR(info))
+ goto fail;
+ if (info->info_length != sizeof(struct dx_root_info)) {
+ ext4_warning_inode(dir, "corrupted htree: info_length %u",
+ info->info_length);
+ goto fail;
+ }
+ if (info->hash_version != DX_HASH_TEA &&
+ info->hash_version != DX_HASH_HALF_MD4 &&
+ info->hash_version != DX_HASH_LEGACY &&
+ info->hash_version != DX_HASH_SIPHASH) {
+ ext4_warning(dir->i_sb,
+ "Unrecognised inode hash code %d for directory #%llu",
+ info->hash_version, dir->i_ino);
goto fail;
}
if (ext4_hash_in_dirent(dir)) {
- if (root->info.hash_version != DX_HASH_SIPHASH) {
+ if (info->hash_version != DX_HASH_SIPHASH) {
ext4_warning_inode(dir,
"Hash in dirent, but hash is not SIPHASH");
goto fail;
}
} else {
- if (root->info.hash_version == DX_HASH_SIPHASH) {
+ if (info->hash_version == DX_HASH_SIPHASH) {
ext4_warning_inode(dir,
"Hash code is SIPHASH, but hash not in dirent");
goto fail;
@@ -664,7 +712,7 @@ dx_probe(struct ext4_filename *fname, struct inode *dir,
}
if (fname)
hinfo = &fname->hinfo;
- hinfo->hash_version = root->info.hash_version;
+ hinfo->hash_version = info->hash_version;
if (hinfo->hash_version <= DX_HASH_TEA)
hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
@@ -680,13 +728,13 @@ dx_probe(struct ext4_filename *fname, struct inode *dir,
}
hash = hinfo->hash;
- if (root->info.unused_flags & 1) {
+ if (info->unused_flags & 1) {
ext4_warning_inode(dir, "Unimplemented hash flags: %#06x",
- root->info.unused_flags);
+ info->unused_flags);
goto fail;
}
- indirect = root->info.indirect_levels;
+ indirect = info->indirect_levels;
if (indirect >= ext4_dir_htree_level(dir->i_sb)) {
ext4_warning(dir->i_sb,
"Directory (ino: %llu) htree depth %#06x exceed"
@@ -699,14 +747,12 @@ dx_probe(struct ext4_filename *fname, struct inode *dir,
goto fail;
}
- entries = (struct dx_entry *)(((char *)&root->info) +
- root->info.info_length);
+ entries = (struct dx_entry *)(((char *)info) + info->info_length);
- if (dx_get_limit(entries) != dx_root_limit(dir,
- root->info.info_length)) {
+ if (dx_get_limit(entries) != dx_root_limit(dir, info->info_length)) {
ext4_warning_inode(dir, "dx entry: limit %u != root limit %u",
dx_get_limit(entries),
- dx_root_limit(dir, root->info.info_length));
+ dx_root_limit(dir, info->info_length));
goto fail;
}
@@ -783,7 +829,7 @@ dx_probe(struct ext4_filename *fname, struct inode *dir,
return ret_err;
}
-static void dx_release(struct dx_frame *frames)
+static void dx_release(struct inode *dir, struct dx_frame *frames)
{
struct dx_root_info *info;
int i;
@@ -792,7 +838,18 @@ static void dx_release(struct dx_frame *frames)
if (frames[0].bh == NULL)
return;
- info = &((struct dx_root *)frames[0].bh->b_data)->info;
+ info = dx_get_dx_info(dir, (struct ext4_dir_entry_2 *)frames[0].bh->b_data);
+ if (IS_ERR(info)) {
+ /* Cannot determine indirect_levels; release all frame buffers
+ * that may have been loaded to avoid leaking them. */
+ for (i = 0; i < EXT4_HTREE_LEVEL; i++) {
+ if (!frames[i].bh)
+ break;
+ brelse(frames[i].bh);
+ frames[i].bh = NULL;
+ }
+ return;
+ }
/* save local copy, "info" may be freed after brelse() */
indirect_levels = info->indirect_levels;
for (i = 0; i <= indirect_levels; i++) {
@@ -1101,12 +1158,12 @@ int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
(count && ((hashval & 1) == 0)))
break;
}
- dx_release(frames);
+ dx_release(dir, frames);
dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
"next hash: %x\n", count, *next_hash));
return count;
errout:
- dx_release(frames);
+ dx_release(dir, frames);
return (err);
}
EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_htree_fill_tree);
@@ -1609,7 +1666,7 @@ static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
errout:
dxtrace(printk(KERN_DEBUG "%s not found\n", fname->usr_fname->name));
success:
- dx_release(frames);
+ dx_release(dir, frames);
return bh;
}
@@ -2015,44 +2072,57 @@ static int add_dirent_to_buf(handle_t *handle, struct ext4_filename *fname,
return err ? err : err2;
}
-static bool ext4_check_dx_root(struct inode *dir, struct dx_root *root)
+/*
+ * Check '.' and '..' at the start of a one-block directory that is about to
+ * become an htree root. On success *@dotdot is '..' and *@entry is the first
+ * entry after it, which lies before the checksum tail of the block.
+ */
+static bool ext4_check_dx_root(struct inode *dir,
+ struct ext4_dir_entry_2 *dot_de,
+ struct ext4_dir_entry_2 **dotdot,
+ struct ext4_dir_entry_2 **entry)
{
- struct fake_dirent *fde;
const char *error_msg;
- unsigned int rlen;
unsigned int blocksize = dir->i_sb->s_blocksize;
- char *blockend = (char *)root + dir->i_sb->s_blocksize;
+ char *end = (char *)dot_de + blocksize;
+ struct ext4_dir_entry_2 *dotdot_de, *de;
+ unsigned int rlen;
+
+ if (ext4_has_feature_metadata_csum(dir->i_sb))
+ end -= sizeof(struct ext4_dir_entry_tail);
- fde = &root->dot;
- if (unlikely(fde->name_len != 1)) {
+ if (unlikely(dot_de->name_len != 1)) {
error_msg = "invalid name_len for '.'";
goto corrupted;
}
- if (unlikely(strncmp(root->dot_name, ".", fde->name_len))) {
+ if (unlikely(strncmp(dot_de->name, ".", dot_de->name_len))) {
error_msg = "invalid name for '.'";
goto corrupted;
}
- rlen = ext4_rec_len_from_disk(fde->rec_len, blocksize);
- if (unlikely((char *)fde + rlen >= blockend)) {
+ rlen = ext4_rec_len_from_disk(dot_de->rec_len, blocksize);
+ if (unlikely(rlen < EXT4_BASE_DIR_LEN ||
+ (char *)dot_de + rlen + EXT4_BASE_DIR_LEN > end)) {
error_msg = "invalid rec_len for '.'";
goto corrupted;
}
- fde = &root->dotdot;
- if (unlikely(fde->name_len != 2)) {
+ dotdot_de = ext4_next_entry(dot_de, blocksize);
+ if (unlikely(dotdot_de->name_len != 2)) {
error_msg = "invalid name_len for '..'";
goto corrupted;
}
- if (unlikely(strncmp(root->dotdot_name, "..", fde->name_len))) {
+ if (unlikely(strncmp(dotdot_de->name, "..", dotdot_de->name_len))) {
error_msg = "invalid name for '..'";
goto corrupted;
}
- rlen = ext4_rec_len_from_disk(fde->rec_len, blocksize);
- if (unlikely((char *)fde + rlen >= blockend)) {
+ rlen = ext4_rec_len_from_disk(dotdot_de->rec_len, blocksize);
+ de = ext4_next_entry(dotdot_de, blocksize);
+ if (unlikely(rlen < EXT4_BASE_DIR_LEN || (char *)de >= end)) {
error_msg = "invalid rec_len for '..'";
goto corrupted;
}
-
+ *dotdot = dotdot_de;
+ *entry = de;
return true;
corrupted:
@@ -2070,16 +2140,15 @@ static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname,
struct inode *inode, struct buffer_head *bh)
{
struct buffer_head *bh2;
- struct dx_root *root;
struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
struct dx_entry *entries;
- struct ext4_dir_entry_2 *de, *de2;
+ struct ext4_dir_entry_2 *de, *de2, *dot_de, *dotdot_de;
char *data2, *top;
unsigned len;
int retval;
unsigned blocksize;
ext4_lblk_t block;
- struct fake_dirent *fde;
+ struct dx_root_info *dx_info;
int csum_size = 0;
if (ext4_has_feature_metadata_csum(inode->i_sb))
@@ -2096,17 +2165,22 @@ static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname,
return retval;
}
- root = (struct dx_root *) bh->b_data;
- if (!ext4_check_dx_root(dir, root)) {
+ dot_de = (struct ext4_dir_entry_2 *)bh->b_data;
+ if (!ext4_check_dx_root(dir, dot_de, &dotdot_de, &de)) {
brelse(bh);
return -EFSCORRUPTED;
}
+ /* Find room for the hashing info before the directory is changed */
+ dx_info = dx_get_dx_info(dir, dot_de);
+ if (IS_ERR(dx_info)) {
+ EXT4_ERROR_INODE(dir, "no room for htree root info");
+ brelse(bh);
+ return PTR_ERR(dx_info);
+ }
+
/* The 0th block becomes the root, move the dirents out */
- fde = &root->dotdot;
- de = (struct ext4_dir_entry_2 *)((char *)fde +
- ext4_rec_len_from_disk(fde->rec_len, blocksize));
- len = ((char *) root) + (blocksize - csum_size) - (char *) de;
+ len = ((char *)dot_de) + (blocksize - csum_size) - (char *)de;
/* Allocate new block for the 0th block's dirents */
bh2 = ext4_append(handle, dir, &block);
@@ -2137,24 +2211,26 @@ static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname,
ext4_initialize_dirent_tail(bh2, blocksize);
/* Initialize the root; the dot dirents already exist */
- de = (struct ext4_dir_entry_2 *) (&root->dotdot);
- de->rec_len = ext4_rec_len_to_disk(
- blocksize - ext4_dir_rec_len(2, NULL), blocksize);
- memset (&root->info, 0, sizeof(root->info));
- root->info.info_length = sizeof(root->info);
+ dotdot_de->rec_len =
+ ext4_rec_len_to_disk(blocksize - le16_to_cpu(dot_de->rec_len),
+ blocksize);
+
+ /* initialize hashing info */
+ memset(dx_info, 0, sizeof(*dx_info));
+ dx_info->info_length = sizeof(*dx_info);
if (ext4_hash_in_dirent(dir))
- root->info.hash_version = DX_HASH_SIPHASH;
+ dx_info->hash_version = DX_HASH_SIPHASH;
else
- root->info.hash_version =
+ dx_info->hash_version =
EXT4_SB(dir->i_sb)->s_def_hash_version;
- entries = root->entries;
+ entries = (void *)dx_info + sizeof(*dx_info);
dx_set_block(entries, 1);
dx_set_count(entries, 1);
- dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
+ dx_set_limit(entries, dx_root_limit(dir, sizeof(*dx_info)));
/* Initialize as for dx_probe */
- fname->hinfo.hash_version = root->info.hash_version;
+ fname->hinfo.hash_version = dx_info->hash_version;
if (fname->hinfo.hash_version <= DX_HASH_TEA)
fname->hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
fname->hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
@@ -2197,7 +2273,7 @@ static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname,
*/
if (retval)
ext4_mark_inode_dirty(handle, dir);
- dx_release(frames);
+ dx_release(dir, frames);
brelse(bh2);
return retval;
}
@@ -2464,7 +2540,7 @@ static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
if (restart || err)
goto journal_error;
} else {
- struct dx_root *dxroot;
+ struct dx_root_info *info;
memcpy((char *) entries2, (char *) entries,
icount * sizeof(struct dx_entry));
dx_set_limit(entries2, dx_node_limit(dir));
@@ -2472,11 +2548,17 @@ static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
/* Set up root */
dx_set_count(entries, 1);
dx_set_block(entries + 0, newblock);
- dxroot = (struct dx_root *)frames[0].bh->b_data;
- dxroot->info.indirect_levels += 1;
+ info = dx_get_dx_info(dir, (struct ext4_dir_entry_2 *)
+ frames[0].bh->b_data);
+ if (IS_ERR(info)) {
+ err = PTR_ERR(info);
+ brelse(bh2);
+ goto journal_error;
+ }
+ info->indirect_levels += 1;
dxtrace(printk(KERN_DEBUG
"Creating %d level index...\n",
- dxroot->info.indirect_levels));
+ info->indirect_levels));
err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
if (err) {
brelse(bh2);
@@ -2500,7 +2582,7 @@ static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
ext4_std_error(dir->i_sb, err); /* this is a no-op if err == 0 */
cleanup:
brelse(bh);
- dx_release(frames);
+ dx_release(dir, frames);
/* @restart is true means htree-path has been changed, we need to
* repeat dx_probe() to find out valid htree-path
*/
@@ -4153,9 +4235,9 @@ struct dx_frame *dx_probe_test(struct ext4_filename *fname, struct inode *dir,
}
EXPORT_SYMBOL_FOR_EXT4_TEST(dx_probe_test);
-void dx_release_test(struct dx_frame *frames)
+void dx_release_test(struct inode *dir, struct dx_frame *frames)
{
- dx_release(frames);
+ dx_release(dir, frames);
}
EXPORT_SYMBOL_FOR_EXT4_TEST(dx_release_test);
@@ -4215,7 +4297,10 @@ EXPORT_SYMBOL_FOR_EXT4_TEST(do_split_test);
bool ext4_check_dx_root_test(struct inode *dir, struct dx_root *root)
{
- return ext4_check_dx_root(dir, root);
+ struct ext4_dir_entry_2 *dotdot_de, *de;
+
+ return ext4_check_dx_root(dir, (struct ext4_dir_entry_2 *)root,
+ &dotdot_de, &de);
}
EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_check_dx_root_test);
diff --git a/fs/ext4/namei.h b/fs/ext4/namei.h
index d5c261d0faf3..e66c5d1c2aae 100644
--- a/fs/ext4/namei.h
+++ b/fs/ext4/namei.h
@@ -43,19 +43,26 @@ struct dx_entry {
* dx_root_info is laid out so that if it should somehow get overlaid by a
* dirent the two low bits of the hash version will be zero. Therefore, the
* hash version mod 4 should never be 0. Sincerely, the paranoia department.
+ *
+ * It follows the '..' entry of the htree root block. With dirdata, '.' and
+ * '..' can be longer than the 12 bytes struct dx_root assumes, so the kernel
+ * finds it with dx_get_dx_info() and never through struct dx_root.
*/
+struct dx_root_info {
+ __le32 reserved_zero;
+ u8 hash_version;
+ u8 info_length; /* 8 */
+ u8 indirect_levels;
+ u8 unused_flags;
+};
+
+/* Layout of an htree root block whose '.' and '..' carry no dirdata. */
struct dx_root {
struct fake_dirent dot;
char dot_name[4];
struct fake_dirent dotdot;
char dotdot_name[4];
- struct dx_root_info {
- __le32 reserved_zero;
- u8 hash_version;
- u8 info_length; /* 8 */
- u8 indirect_levels;
- u8 unused_flags;
- } info;
+ struct dx_root_info info;
struct dx_entry entries[];
};
@@ -192,7 +199,7 @@ int ext4_dx_csum_verify_test(struct inode *inode,
struct dx_frame *dx_probe_test(struct ext4_filename *fname, struct inode *dir,
struct dx_hash_info *hinfo,
struct dx_frame *frame_in);
-void dx_release_test(struct dx_frame *frames);
+void dx_release_test(struct inode *dir, struct dx_frame *frames);
int ext4_htree_next_block_test(struct inode *dir, __u32 hash,
struct dx_frame *frame,
struct dx_frame *frames, __u32 *start_hash);
--
2.52.0
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v15 07/13] ext4: size the htree root from where its dx_entry array starts
2026-12-03 18:49 [PATCH v15 00/13] Data in direntry (dirdata) feature, with KUnit tests Artem Blagodarenko
` (5 preceding siblings ...)
2026-12-03 18:49 ` [PATCH v15 06/13] ext4: refactor dx_root to support variable dirent sizes Artem Blagodarenko
@ 2026-12-03 18:49 ` Artem Blagodarenko
2026-12-03 20:37 ` sashiko-bot
2026-12-03 18:49 ` [PATCH v15 08/13] ext4: rename ext4_dir_rec_len() and clarify dirdata usage Artem Blagodarenko
` (6 subsequent siblings)
13 siblings, 1 reply; 26+ messages in thread
From: Artem Blagodarenko @ 2026-12-03 18:49 UTC (permalink / raw)
To: linux-ext4; +Cc: tytso, jack, adilger.kernel, ablagodarenko, Artem Blagodarenko
From: Artem Blagodarenko <artem.blagodarenko@gmail.com>
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 <artem.blagodarenko@gmail.com>
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
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v15 08/13] ext4: rename ext4_dir_rec_len() and clarify dirdata usage
2026-12-03 18:49 [PATCH v15 00/13] Data in direntry (dirdata) feature, with KUnit tests Artem Blagodarenko
` (6 preceding siblings ...)
2026-12-03 18:49 ` [PATCH v15 07/13] ext4: size the htree root from where its dx_entry array starts Artem Blagodarenko
@ 2026-12-03 18:49 ` Artem Blagodarenko
2026-12-03 21:05 ` sashiko-bot
2026-12-03 18:49 ` [PATCH v15 09/13] ext4: dirdata feature Artem Blagodarenko
` (5 subsequent siblings)
13 siblings, 1 reply; 26+ messages in thread
From: Artem Blagodarenko @ 2026-12-03 18:49 UTC (permalink / raw)
To: linux-ext4; +Cc: tytso, jack, adilger.kernel, ablagodarenko, Artem Blagodarenko
From: Artem Blagodarenko <artem.blagodarenko@gmail.com>
Rename ext4_dir_rec_len() to ext4_dirent_rec_len() to better
reflect that it computes the record length for a directory
entry based on the provided name length.
Update the comment to clarify handling of dirdata-enabled
directories and document the use of ext4_dir_entry_len()
when dirdata is present.
ext4_dir_entry_len() reads the dirdata of an entry, so the length of
the entry that do_split() hands to add_dirent_to_buf() now depends on
on-disk data. add_dirent_to_buf() checks that this entry has room for
the new one before anything is written, instead of overrunning the
block when the directory is corrupted.
Signed-off-by: Artem Blagodarenko <artem.blagodarenko@gmail.com>
Assisted-by: Claude:claude-opus-5-5
---
fs/ext4/dir.c | 15 ++++++-------
fs/ext4/ext4.h | 16 +++++++++-----
fs/ext4/inline.c | 21 ++++++++++--------
fs/ext4/namei-test.c | 12 +++++------
fs/ext4/namei.c | 51 +++++++++++++++++++++++++++++++-------------
fs/ext4/namei.h | 2 +-
6 files changed, 73 insertions(+), 44 deletions(-)
diff --git a/fs/ext4/dir.c b/fs/ext4/dir.c
index 43f4f2d451aa..06d8d2eb52ea 100644
--- a/fs/ext4/dir.c
+++ b/fs/ext4/dir.c
@@ -89,16 +89,15 @@ int __ext4_check_dir_entry(const char *function, unsigned int line,
bool fake = is_fake_dir_entry(de);
bool has_csum = ext4_has_feature_metadata_csum(dir->i_sb);
- if (unlikely(rlen < ext4_dir_rec_len(1, fake ? NULL : dir)))
+ if (unlikely(rlen < ext4_dirent_rec_len(1, fake ? NULL : dir)))
error_msg = "rec_len is smaller than minimal";
else if (unlikely(rlen % 4 != 0))
error_msg = "rec_len % 4 != 0";
- else if (unlikely(rlen < ext4_dir_rec_len(de->name_len,
- fake ? NULL : dir)))
- error_msg = "rec_len is too small for name_len";
else if (unlikely(next_offset > size))
error_msg = "directory entry overrun";
- else if (unlikely(next_offset > size - ext4_dir_rec_len(1,
+ else if (unlikely(rlen < ext4_dir_entry_len(de, size, fake ? NULL : dir)))
+ error_msg = "rec_len is too small for name_len";
+ else if (unlikely(next_offset > size - ext4_dirent_rec_len(1,
has_csum ? NULL : dir) &&
next_offset != size))
error_msg = "directory entry too close to block end";
@@ -239,7 +238,7 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)
if (!inode_eq_iversion(inode, info->cookie)) {
for (i = 0;
i <= sb->s_blocksize -
- ext4_dir_rec_len(1, has_csum ? NULL : inode) &&
+ ext4_dirent_rec_len(1, has_csum ? NULL : inode) &&
i < offset;) {
de = (struct ext4_dir_entry_2 *)
(bh->b_data + i);
@@ -250,7 +249,7 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)
* failure will be detected in the
* dirent test below. */
if (ext4_rec_len_from_disk(de->rec_len,
- sb->s_blocksize) < ext4_dir_rec_len(1,
+ sb->s_blocksize) < ext4_dirent_rec_len(1,
inode))
break;
i += ext4_rec_len_from_disk(de->rec_len,
@@ -264,7 +263,7 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)
if (unlikely(offset < sb->s_blocksize &&
offset > sb->s_blocksize -
- ext4_dir_rec_len(1, has_csum ? NULL : inode))) {
+ ext4_dirent_rec_len(1, has_csum ? NULL : inode))) {
EXT4_ERROR_FILE(file, bh->b_blocknr,
"bad entry in directory: %s - offset=%u, size=%lu",
"directory entry too close to block end",
diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 32401150503e..e34d91cd63ec 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -2671,11 +2671,16 @@ struct ext4_dirent_hash {
* casefolded and encrypted need to store the hash as well, so we add room for
* ext4_extended_dir_entry_2. For all entries related to '.' or '..' you should
* pass NULL for dir, as those entries do not use the extra fields.
+ *
+ * For directories with the dirdata feature, extra data may follow the filename.
+ * Use ext4_dir_entry_len() to compute the length of a directory entry
+ * including any dirdata, or ext4_dirent_rec_len() directly when the total
+ * name_len (including dirdata length) is already known.
*/
-static inline unsigned int ext4_dir_rec_len(__u8 name_len,
+static inline unsigned int ext4_dirent_rec_len(unsigned int name_len,
const struct inode *dir)
{
- int rec_len = (name_len + 8 + EXT4_DIR_ROUND);
+ unsigned int rec_len = (name_len + 8 + EXT4_DIR_ROUND);
if (dir && ext4_hash_in_dirent(dir))
rec_len += sizeof(struct ext4_dir_entry_hash);
@@ -3090,7 +3095,8 @@ extern void ext4_htree_free_dir_info(struct dir_private_info *p);
extern int ext4_find_dest_de(struct inode *dir, struct buffer_head *bh,
void *buf, int buf_size,
struct ext4_filename *fname,
- struct ext4_dir_entry_2 **dest_de);
+ struct ext4_dir_entry_2 **dest_de,
+ int dlen);
void ext4_insert_dentry(struct inode *dir, struct inode *inode,
struct ext4_dir_entry_2 *de,
int buf_size,
@@ -4529,8 +4535,8 @@ static inline unsigned int ext4_dir_entry_len(struct ext4_dir_entry_2 *de,
unsigned int rec_len = ext4_rec_len_from_disk(de->rec_len, blocksize);
unsigned int dirdata = ext4_dirent_get_data_len(de, rec_len);
- return max(ext4_dir_rec_len(de->name_len + dirdata, NULL),
- ext4_dir_rec_len(de->name_len, dir));
+ return max(ext4_dirent_rec_len(de->name_len + dirdata, NULL),
+ ext4_dirent_rec_len(de->name_len, dir));
}
diff --git a/fs/ext4/inline.c b/fs/ext4/inline.c
index acdc65ce4796..e3d3853f6f27 100644
--- a/fs/ext4/inline.c
+++ b/fs/ext4/inline.c
@@ -1002,8 +1002,11 @@ static int ext4_add_dirent_to_inline(handle_t *handle,
int err;
struct ext4_dir_entry_2 *de;
+ /* 'dlen' reserves space for dirdata extensions (LUFID, CFHASH) in the
+ * slot. The dirdata types are defined in the next patch; until then
+ * dlen stays 0 and ext4_find_dest_de() ignores dirdata overhead. */
err = ext4_find_dest_de(dir, iloc->bh, inline_start,
- inline_size, fname, &de);
+ inline_size, fname, &de, 0);
if (err)
return err;
@@ -1081,7 +1084,7 @@ static int ext4_update_inline_dir(handle_t *handle, struct inode *dir,
int old_size = EXT4_I(dir)->i_inline_size - EXT4_MIN_INLINE_DATA_SIZE;
int new_size = get_max_inline_xattr_value_size(dir, iloc);
- if (new_size - old_size <= ext4_dir_rec_len(1, NULL))
+ if (new_size - old_size <= ext4_dirent_rec_len(1, NULL))
return -ENOSPC;
ret = ext4_update_inline_data(handle, dir,
@@ -1342,7 +1345,7 @@ int ext4_inlinedir_to_tree(struct file *dir_file,
fake.name_len = 1;
memcpy(fake.name, ".", 2);
fake.rec_len = ext4_rec_len_to_disk(
- ext4_dir_rec_len(fake.name_len, NULL),
+ ext4_dirent_rec_len(fake.name_len, NULL),
inline_size);
ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
de = &fake;
@@ -1352,7 +1355,7 @@ int ext4_inlinedir_to_tree(struct file *dir_file,
fake.name_len = 2;
memcpy(fake.name, "..", 3);
fake.rec_len = ext4_rec_len_to_disk(
- ext4_dir_rec_len(fake.name_len, NULL),
+ ext4_dirent_rec_len(fake.name_len, NULL),
inline_size);
ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
de = &fake;
@@ -1460,8 +1463,8 @@ int ext4_read_inline_dir(struct file *file,
* So we will use extra_offset and extra_size to indicate them
* during the inline dir iteration.
*/
- dotdot_offset = ext4_dir_rec_len(1, NULL);
- dotdot_size = dotdot_offset + ext4_dir_rec_len(2, NULL);
+ dotdot_offset = ext4_dirent_rec_len(1, NULL);
+ dotdot_size = dotdot_offset + ext4_dirent_rec_len(2, NULL);
extra_offset = dotdot_size - EXT4_INLINE_DOTDOT_SIZE;
extra_size = extra_offset + inline_size;
@@ -1487,7 +1490,7 @@ int ext4_read_inline_dir(struct file *file,
/* for other entry, the real offset in
* the buf has to be tuned accordingly.
*/
- if (i + ext4_dir_rec_len(1, NULL) > extra_size)
+ if (i + ext4_dirent_rec_len(1, NULL) > extra_size)
break;
de = (struct ext4_dir_entry_2 *)
(dir_buf + i - extra_offset);
@@ -1498,7 +1501,7 @@ int ext4_read_inline_dir(struct file *file,
* failure will be detected in the
* dirent test below. */
if (ext4_rec_len_from_disk(de->rec_len, extra_size)
- < ext4_dir_rec_len(1, NULL))
+ < ext4_dirent_rec_len(1, NULL))
break;
i += ext4_rec_len_from_disk(de->rec_len,
extra_size);
@@ -1528,7 +1531,7 @@ int ext4_read_inline_dir(struct file *file,
* kmalloc(inline_size) buffer. Make sure its header fits before
* ext4_check_dir_entry() dereferences de->rec_len.
*/
- if (ctx->pos + ext4_dir_rec_len(1, NULL) > extra_size)
+ if (ctx->pos + ext4_dirent_rec_len(1, NULL) > extra_size)
goto out;
de = (struct ext4_dir_entry_2 *)
(dir_buf + ctx->pos - extra_offset);
diff --git a/fs/ext4/namei-test.c b/fs/ext4/namei-test.c
index bc10fdb13928..b45f5ad3b7af 100644
--- a/fs/ext4/namei-test.c
+++ b/fs/ext4/namei-test.c
@@ -1015,18 +1015,18 @@ static void test_namei_search_insert_delete_dentry(struct kunit *test)
KUNIT_ASSERT_EQ(test, ext4_fname_setup_filename(dir, &q2, 0, &fname2), 0);
/* Find space and insert alpha.txt */
- err = ext4_find_dest_de(dir, bh, bh->b_data, buf_size, &fname1, &de);
+ err = ext4_find_dest_de(dir, bh, bh->b_data, buf_size, &fname1, &de, 0);
KUNIT_ASSERT_EQ(test, err, 0);
KUNIT_ASSERT_NOT_NULL(test, de);
ext4_insert_dentry(dir, file1, de, TEST_BLK_SIZE, &fname1);
KUNIT_ASSERT_EQ(test, ext4_handle_dirty_dirblock(NULL, dir, bh), 0);
/* Duplicate insert of alpha.txt must return -EEXIST */
- err = ext4_find_dest_de(dir, bh, bh->b_data, buf_size, &fname1, &de);
+ err = ext4_find_dest_de(dir, bh, bh->b_data, buf_size, &fname1, &de, 0);
KUNIT_EXPECT_EQ(test, err, -EEXIST);
/* Insert beta.txt */
- err = ext4_find_dest_de(dir, bh, bh->b_data, buf_size, &fname2, &de);
+ err = ext4_find_dest_de(dir, bh, bh->b_data, buf_size, &fname2, &de, 0);
KUNIT_ASSERT_EQ(test, err, 0);
ext4_insert_dentry(dir, file2, de, TEST_BLK_SIZE, &fname2);
KUNIT_ASSERT_EQ(test, ext4_handle_dirty_dirblock(NULL, dir, bh), 0);
@@ -1181,7 +1181,7 @@ static void test_namei_dx_map_pack_and_move(struct kunit *test)
ext4_fname_setup_filename(dir, &q, 0, &fname), 0);
KUNIT_ASSERT_EQ(test,
ext4_find_dest_de(dir, bh, bh->b_data, TEST_BLK_SIZE,
- &fname, &de), 0);
+ &fname, &de, 0), 0);
ext4_insert_dentry(dir, file1, de, TEST_BLK_SIZE, &fname);
ext4_fname_free_filename(&fname);
}
@@ -1653,7 +1653,7 @@ static void test_namei_dx_index_level_growth_and_fallback(struct kunit *test)
err = ext4_find_dest_de(dir, bh1, bh1->b_data,
TEST_BLK_SIZE -
sizeof(struct ext4_dir_entry_tail),
- &fname, &de);
+ &fname, &de, 0);
if (err == 0)
ext4_insert_dentry(dir, file1, de, TEST_BLK_SIZE,
&fname);
@@ -1720,7 +1720,7 @@ static void test_namei_dx_index_level_growth_and_fallback(struct kunit *test)
err = ext4_find_dest_de(dir, bh1, bh1->b_data,
TEST_BLK_SIZE -
sizeof(struct ext4_dir_entry_tail),
- &fname, &de);
+ &fname, &de, 0);
if (err == 0)
ext4_insert_dentry(dir, file1, de,
TEST_BLK_SIZE, &fname);
diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index 78ee4936e6cc..6050dbfd9f78 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -586,7 +586,7 @@ static struct stats dx_show_leaf(struct inode *dir,
(unsigned) ((char *) de - base));
#endif
}
- space += ext4_dir_rec_len(de->name_len, dir);
+ space += ext4_dirent_rec_len(de->name_len, dir);
names++;
}
de = ext4_next_entry(de, size);
@@ -961,7 +961,7 @@ static int htree_dirblock_to_tree(struct file *dir_file,
/* csum entries are not larger in the casefolded encrypted case */
top = (struct ext4_dir_entry_2 *) ((char *) de +
dir->i_sb->s_blocksize -
- ext4_dir_rec_len(0,
+ ext4_dirent_rec_len(0,
csum ? NULL : dir));
/* Check if the directory is encrypted */
if (IS_ENCRYPTED(dir)) {
@@ -1378,7 +1378,7 @@ int ext4_search_dir(struct buffer_head *bh, char *search_buf, int buf_size,
/* do minimal checking `by hand' */
if (de->name + de->name_len <= dlimit &&
(!ext4_hash_in_dirent(dir) ||
- (char *)de + ext4_dir_rec_len(de->name_len, dir) <= dlimit) &&
+ (char *)de + ext4_dirent_rec_len(de->name_len, dir) <= dlimit) &&
ext4_match(dir, fname, de)) {
/* found a match - just to be sure, do
* a full check */
@@ -1764,7 +1764,7 @@ dx_move_dirents(struct inode *dir, char *from, char *to,
while (count--) {
struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
(from + (map->offs<<2));
- rec_len = ext4_dir_rec_len(de->name_len, dir);
+ rec_len = ext4_dir_entry_len(de, blocksize, dir);
memcpy (to, de, rec_len);
((struct ext4_dir_entry_2 *) to)->rec_len =
@@ -1797,7 +1797,7 @@ static struct ext4_dir_entry_2 *dx_pack_dirents(struct inode *dir, char *base,
while ((char*)de < base + blocksize) {
next = ext4_next_entry(de, blocksize);
if (de->inode && de->name_len) {
- rec_len = ext4_dir_rec_len(de->name_len, dir);
+ rec_len = ext4_dir_entry_len(de, blocksize, dir);
if (de > to)
memmove(to, de, rec_len);
to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
@@ -1949,10 +1949,11 @@ static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
int ext4_find_dest_de(struct inode *dir, struct buffer_head *bh,
void *buf, int buf_size,
struct ext4_filename *fname,
- struct ext4_dir_entry_2 **dest_de)
+ struct ext4_dir_entry_2 **dest_de,
+ int dlen)
{
struct ext4_dir_entry_2 *de;
- unsigned short reclen = ext4_dir_rec_len(fname_len(fname), dir);
+ unsigned short reclen = ext4_dirent_rec_len(fname_len(fname) + dlen, dir);
int nlen, rlen;
unsigned int offset = 0;
char *top;
@@ -1965,7 +1966,7 @@ int ext4_find_dest_de(struct inode *dir, struct buffer_head *bh,
return -EFSCORRUPTED;
if (ext4_match(dir, fname, de))
return -EEXIST;
- nlen = ext4_dir_rec_len(de->name_len, dir);
+ nlen = ext4_dir_entry_len(de, buf_size, dir);
rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
if ((de->inode ? rlen - nlen : rlen) >= reclen)
break;
@@ -1989,7 +1990,7 @@ void ext4_insert_dentry(struct inode *dir,
int nlen, rlen;
- nlen = ext4_dir_rec_len(de->name_len, dir);
+ nlen = ext4_dir_entry_len(de, buf_size, dir);
rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
if (de->inode) {
struct ext4_dir_entry_2 *de1 =
@@ -2035,9 +2036,29 @@ static int add_dirent_to_buf(handle_t *handle, struct ext4_filename *fname,
if (!de) {
err = ext4_find_dest_de(dir, bh, bh->b_data,
- blocksize - csum_size, fname, &de);
+ blocksize - csum_size, fname, &de, 0);
if (err)
return err;
+ } else {
+ /*
+ * do_split() hands over the last entry of a half. Its length
+ * comes from on-disk dirdata, so check that the new entry fits
+ * before it is written. do_split() has already packed this
+ * half and left it to us to dirty, so dirty it on failure too.
+ */
+ unsigned int nlen = de->inode ?
+ ext4_dir_entry_len(de, blocksize, dir) : 0;
+ unsigned int rlen = ext4_rec_len_from_disk(de->rec_len,
+ blocksize);
+
+ if (nlen > rlen ||
+ rlen - nlen < ext4_dirent_rec_len(fname_len(fname), dir)) {
+ EXT4_ERROR_INODE(dir,
+ "no room for new entry in split block %llu",
+ (unsigned long long)bh->b_blocknr);
+ ext4_handle_dirty_dirblock(handle, dir, bh);
+ return -EFSCORRUPTED;
+ }
}
BUFFER_TRACE(bh, "get_write_access");
err = ext4_journal_get_write_access(handle, dir->i_sb, bh,
@@ -2875,7 +2896,7 @@ int ext4_init_dirblock(handle_t *handle, struct inode *inode,
de->inode = cpu_to_le32(inode->i_ino);
de->name_len = 1;
- de->rec_len = ext4_rec_len_to_disk(ext4_dir_rec_len(de->name_len, NULL),
+ de->rec_len = ext4_rec_len_to_disk(ext4_dirent_rec_len(de->name_len, NULL),
blocksize);
memcpy(de->name, ".", 2);
ext4_set_de_type(inode->i_sb, de, S_IFDIR);
@@ -2887,7 +2908,7 @@ int ext4_init_dirblock(handle_t *handle, struct inode *inode,
ext4_set_de_type(inode->i_sb, de, S_IFDIR);
if (inline_buf) {
de->rec_len = ext4_rec_len_to_disk(
- ext4_dir_rec_len(de->name_len, NULL),
+ ext4_dirent_rec_len(de->name_len, NULL),
blocksize);
de = ext4_next_entry(de, blocksize);
header_size = (char *)de - bh->b_data;
@@ -2896,7 +2917,7 @@ int ext4_init_dirblock(handle_t *handle, struct inode *inode,
blocksize - csum_size);
} else {
de->rec_len = ext4_rec_len_to_disk(blocksize -
- (csum_size + ext4_dir_rec_len(1, NULL)),
+ (csum_size + ext4_dirent_rec_len(1, NULL)),
blocksize);
}
@@ -3021,8 +3042,8 @@ bool ext4_empty_dir(struct inode *inode)
}
sb = inode->i_sb;
- if (inode->i_size < ext4_dir_rec_len(1, NULL) +
- ext4_dir_rec_len(2, NULL)) {
+ if (inode->i_size < ext4_dirent_rec_len(1, NULL) +
+ ext4_dirent_rec_len(2, NULL)) {
EXT4_ERROR_INODE(inode, "invalid size");
return false;
}
diff --git a/fs/ext4/namei.h b/fs/ext4/namei.h
index 569409c52f58..946a5f705f4d 100644
--- a/fs/ext4/namei.h
+++ b/fs/ext4/namei.h
@@ -167,7 +167,7 @@ static inline unsigned int __dx_node_limit(struct inode *dir,
const struct inode *hash_dir)
{
unsigned int entry_space = dir->i_sb->s_blocksize -
- ext4_dir_rec_len(0, hash_dir);
+ ext4_dirent_rec_len(0, hash_dir);
if (ext4_has_feature_metadata_csum(dir->i_sb))
entry_space -= sizeof(struct dx_tail);
--
2.52.0
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v15 09/13] ext4: dirdata feature
2026-12-03 18:49 [PATCH v15 00/13] Data in direntry (dirdata) feature, with KUnit tests Artem Blagodarenko
` (7 preceding siblings ...)
2026-12-03 18:49 ` [PATCH v15 08/13] ext4: rename ext4_dir_rec_len() and clarify dirdata usage Artem Blagodarenko
@ 2026-12-03 18:49 ` Artem Blagodarenko
2026-12-03 21:37 ` sashiko-bot
2026-12-03 18:49 ` [PATCH v15 10/13] ext4: add dirdata LUFID support for directory entry rename Artem Blagodarenko
` (4 subsequent siblings)
13 siblings, 1 reply; 26+ messages in thread
From: Artem Blagodarenko @ 2026-12-03 18:49 UTC (permalink / raw)
To: linux-ext4
Cc: tytso, jack, adilger.kernel, ablagodarenko, Artem Blagodarenko,
Pravin Shelar
From: Artem Blagodarenko <artem.blagodarenko@gmail.com>
When fscrypt and casefold are enabled together for a directory,
all ext4_dir_entry[_2] in that directory store a n 8-byte hash
of the filename after 'name' between 'name_len' and 'rec_len'.
However, there is no clear indication there is important data
stored in these bytes, which are only for padding and alignment
in other directory entries. This adds complexity to code handling
the on-disk directory entries, and there is no provision for other
metadata to be stored in each dir entry after 'name'.
The dirdata feature adds a mechanism to store multiple metadata
entries in each dir entry after 'name' (including the fchash).
The unused high 4 bits of 'file_type' are used to indicate whether
additional data fields are stored after 'name'. If a bit is set,
the corresponding dirdata record is present, starting after a NUL
filename terminator. If present, a record starts with a 1-byte
length (including the length byte itself) and the data immediately
follows the length byte without any alignment.
This allows up to four different dirdata records to be stored in
each entry, and allows unhandled record bytes to be skipped without
having to process the contents, providing forward compatibility.
If and when the fourth and last dirdata record is needed, it is
recommended to further subdivide it into sub-records, with
the first byte being the total length, and then there being a
second byte that gives the sub-record length, etc. as long as
the total record length is less than 255 bytes. However, this
would not affect compatibility with the current code since the
record length would allow it to be skipped without processing.
Signed-off-by: Pravin Shelar <pravin.shelar@sun.com>
Signed-off-by: Artem Blagodarenko <artem.blagodarenko@gmail.com>
Assisted-by: Claude:claude-opus-5-5
---
fs/ext4/dir.c | 2 +
fs/ext4/ext4.h | 99 ++++++++++++++++++++++++++++++++++++++++---
fs/ext4/fast_commit.c | 1 +
fs/ext4/fast_commit.h | 1 +
fs/ext4/inline.c | 22 +++++++---
fs/ext4/namei.c | 96 ++++++++++++++++++++++++++++-------------
fs/ext4/sysfs.c | 2 +
7 files changed, 181 insertions(+), 42 deletions(-)
diff --git a/fs/ext4/dir.c b/fs/ext4/dir.c
index 06d8d2eb52ea..4372e23abb81 100644
--- a/fs/ext4/dir.c
+++ b/fs/ext4/dir.c
@@ -95,6 +95,8 @@ int __ext4_check_dir_entry(const char *function, unsigned int line,
error_msg = "rec_len % 4 != 0";
else if (unlikely(next_offset > size))
error_msg = "directory entry overrun";
+ else if (unlikely(ext4_dirent_data_corrupt(dir->i_sb, de, rlen)))
+ error_msg = "dirdata record overruns entry";
else if (unlikely(rlen < ext4_dir_entry_len(de, size, fake ? NULL : dir)))
error_msg = "rec_len is too small for name_len";
else if (unlikely(next_offset > size - ext4_dirent_rec_len(1,
diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index e34d91cd63ec..98014749e5d4 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -2385,6 +2385,7 @@ EXT4_FEATURE_INCOMPAT_FUNCS(casefold, CASEFOLD)
EXT4_FEATURE_INCOMPAT_FLEX_BG| \
EXT4_FEATURE_INCOMPAT_EA_INODE| \
EXT4_FEATURE_INCOMPAT_MMP | \
+ EXT4_FEATURE_INCOMPAT_DIRDATA | \
EXT4_FEATURE_INCOMPAT_INLINE_DATA | \
EXT4_FEATURE_INCOMPAT_ENCRYPT | \
EXT4_FEATURE_INCOMPAT_CASEFOLD | \
@@ -2655,6 +2656,18 @@ struct ext4_dirent_hash {
__le32 dh_minor_hash;
} __packed;
+static inline
+struct ext4_dirent_fid *ext4_dentry_get_fid(struct super_block *sb,
+ struct ext4_dentry_param *p)
+{
+ if (!ext4_has_feature_dirdata(sb))
+ return NULL;
+ if (p && p->edp_magic == EXT4_LUFID_MAGIC)
+ return (struct ext4_dirent_fid *)p->edp_dfid;
+
+ return NULL;
+}
+
#define EXT4_FT_DIR_CSUM 0xDE
/*
@@ -2777,6 +2790,8 @@ struct ext4_filename {
#if IS_ENABLED(CONFIG_UNICODE)
struct qstr cf_name;
#endif
+ /* LUFID/dirdata payload to embed in the new directory entry, or NULL */
+ struct ext4_dirent_fid *dfid;
};
#define fname_name(p) ((p)->disk_name.name)
@@ -3097,10 +3112,18 @@ extern int ext4_find_dest_de(struct inode *dir, struct buffer_head *bh,
struct ext4_filename *fname,
struct ext4_dir_entry_2 **dest_de,
int dlen);
-void ext4_insert_dentry(struct inode *dir, struct inode *inode,
- struct ext4_dir_entry_2 *de,
- int buf_size,
- struct ext4_filename *fname);
+void ext4_insert_dentry_data(struct inode *dir, struct inode *inode,
+ struct ext4_dir_entry_2 *de,
+ int buf_size,
+ struct ext4_filename *fname,
+ void *data);
+static inline void ext4_insert_dentry(struct inode *dir, struct inode *inode,
+ struct ext4_dir_entry_2 *de,
+ int buf_size,
+ struct ext4_filename *fname)
+{
+ ext4_insert_dentry_data(dir, inode, de, buf_size, fname, NULL);
+}
static inline void ext4_update_dx_flag(struct inode *inode)
{
if (!ext4_has_feature_dir_index(inode->i_sb) &&
@@ -3494,8 +3517,14 @@ extern int ext4_ext_swap_inode_data_test(handle_t *handle, struct inode *inode,
#endif
/* namei.c */
-extern int ext4_init_new_dir(handle_t *handle, struct inode *dir,
- struct inode *inode);
+extern int ext4_init_new_dir_data(handle_t *handle, struct inode *dir,
+ struct inode *inode,
+ const void *data1, const void *data2);
+static inline int ext4_init_new_dir(handle_t *handle, struct inode *dir,
+ struct inode *inode)
+{
+ return ext4_init_new_dir_data(handle, dir, inode, NULL, NULL);
+}
extern int ext4_dirblock_csum_verify(struct inode *inode,
struct buffer_head *bh);
extern int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
@@ -4509,6 +4538,28 @@ static inline int ext4_dirent_get_data_len(struct ext4_dir_entry_2 *de,
return dlen;
}
+/*
+ * ext4_dirent_data_corrupt() - check the dirdata records of an entry
+ * @sb: super block
+ * @de: directory entry
+ * @rec_len: the record length of the directory entry (decoded)
+ *
+ * On a dirdata filesystem every record flagged in the high bits of
+ * de->file_type must fit inside the entry. ext4_dirent_get_data_len()
+ * returns 0 when one does not, which would make the length of the entry
+ * depend on its current rec_len; callers treat that as corruption
+ * instead. Without the feature the flags are not interpreted.
+ */
+static inline bool ext4_dirent_data_corrupt(struct super_block *sb,
+ struct ext4_dir_entry_2 *de,
+ unsigned int rec_len)
+{
+ if (!ext4_has_feature_dirdata(sb) || ext4_dir_entry_is_tail(de) ||
+ !(de->file_type & ~EXT4_FT_MASK))
+ return false;
+ return ext4_dirent_get_data_len(de, rec_len) == 0;
+}
+
/*
* ext4_dir_entry_len() - Compute the required rec_len for a directory entry.
* @de: directory entry (used to read name_len and any dirdata length)
@@ -4539,6 +4590,42 @@ static inline unsigned int ext4_dir_entry_len(struct ext4_dir_entry_2 *de,
ext4_dirent_rec_len(de->name_len, dir));
}
+/*
+ * ext4_dirent_new_data_len() - dirdata length for a new entry in @dir
+ * @dir: directory inode
+ * @dfid: LUFID record to store, or NULL
+ *
+ * Returns the bytes of dirdata records (without the NUL byte after the
+ * name) that ext4_dirdata_set() will write: the LUFID record, and on a
+ * dirdata filesystem the casefold+fscrypt hash as a CFHASH record.
+ */
+static inline unsigned int
+ext4_dirent_new_data_len(const struct inode *dir,
+ const struct ext4_dirent_fid *dfid)
+{
+ unsigned int dlen = dfid ? dfid->df_header.ddh_length : 0;
+
+ if (ext4_hash_in_dirent(dir) && ext4_has_feature_dirdata(dir->i_sb))
+ dlen += sizeof(struct ext4_dirent_hash);
+ return dlen;
+}
+
+/*
+ * ext4_dirent_new_rec_len() - record length for a new entry in @dir
+ * @dir: directory inode
+ * @name_len: length of the name
+ * @dlen: dirdata length from ext4_dirent_new_data_len()
+ */
+static inline unsigned int ext4_dirent_new_rec_len(const struct inode *dir,
+ unsigned int name_len,
+ unsigned int dlen)
+{
+ /* With dirdata the hash, if any, is a record counted in @dlen */
+ if (dlen)
+ return ext4_dirent_rec_len(name_len + 1 + dlen, NULL);
+ return ext4_dirent_rec_len(name_len, dir);
+}
+
DECLARE_IOMAP_ITER_NEXT(ext4_iomap_next);
DECLARE_IOMAP_ITER_NEXT(ext4_iomap_next_report);
diff --git a/fs/ext4/fast_commit.c b/fs/ext4/fast_commit.c
index 9beb423b208f..6af083956b63 100644
--- a/fs/ext4/fast_commit.c
+++ b/fs/ext4/fast_commit.c
@@ -2726,6 +2726,7 @@ static const char * const fc_ineligible_reasons[] = {
[EXT4_FC_REASON_MIGRATE] = "Inode format migration",
[EXT4_FC_REASON_VERITY] = "fs-verity enable",
[EXT4_FC_REASON_MOVE_EXT] = "Move extents",
+ [EXT4_FC_REASON_DIRDATA] = "Dirdata LUFID (not recorded by fast-commit)",
};
int ext4_fc_info_show(struct seq_file *seq, void *v)
diff --git a/fs/ext4/fast_commit.h b/fs/ext4/fast_commit.h
index 2f77a37fb101..899faeb4e6e0 100644
--- a/fs/ext4/fast_commit.h
+++ b/fs/ext4/fast_commit.h
@@ -100,6 +100,7 @@ enum {
EXT4_FC_REASON_MIGRATE,
EXT4_FC_REASON_VERITY,
EXT4_FC_REASON_MOVE_EXT,
+ EXT4_FC_REASON_DIRDATA,
EXT4_FC_REASON_MAX
};
diff --git a/fs/ext4/inline.c b/fs/ext4/inline.c
index e3d3853f6f27..d43d9d03d0a1 100644
--- a/fs/ext4/inline.c
+++ b/fs/ext4/inline.c
@@ -1001,12 +1001,12 @@ static int ext4_add_dirent_to_inline(handle_t *handle,
{
int err;
struct ext4_dir_entry_2 *de;
+ /* fname->dfid was set in ext4_add_entry() */
+ struct ext4_dirent_fid *dfid = fname->dfid;
+ unsigned int dlen = ext4_dirent_new_data_len(dir, dfid);
- /* 'dlen' reserves space for dirdata extensions (LUFID, CFHASH) in the
- * slot. The dirdata types are defined in the next patch; until then
- * dlen stays 0 and ext4_find_dest_de() ignores dirdata overhead. */
err = ext4_find_dest_de(dir, iloc->bh, inline_start,
- inline_size, fname, &de, 0);
+ inline_size, fname, &de, dlen);
if (err)
return err;
@@ -1015,7 +1015,7 @@ static int ext4_add_dirent_to_inline(handle_t *handle,
EXT4_JTR_NONE);
if (err)
return err;
- ext4_insert_dentry(dir, inode, de, inline_size, fname);
+ ext4_insert_dentry_data(dir, inode, de, inline_size, fname, dfid);
ext4_show_inline_dir(dir, iloc->bh, inline_start, inline_size);
@@ -1300,12 +1300,14 @@ int ext4_inlinedir_to_tree(struct file *dir_file,
int err = 0, count = 0;
unsigned int parent_ino;
int pos;
+ unsigned int de_len;
struct ext4_dir_entry_2 *de;
struct inode *inode = file_inode(dir_file);
int ret, inline_size = 0;
struct ext4_iloc iloc;
void *dir_buf = NULL;
- struct ext4_dir_entry_2 fake;
+ /* zeroed: without the filetype feature, file_type is never set */
+ struct ext4_dir_entry_2 fake = {};
struct fscrypt_str tmp_str;
ret = ext4_get_inode_loc(inode, &iloc);
@@ -1361,14 +1363,20 @@ int ext4_inlinedir_to_tree(struct file *dir_file,
de = &fake;
pos = EXT4_INLINE_DOTDOT_SIZE;
} else {
+ if (pos + EXT4_BASE_DIR_LEN > inline_size) {
+ ret = count;
+ goto out;
+ }
de = (struct ext4_dir_entry_2 *)(dir_buf + pos);
- pos += ext4_rec_len_from_disk(de->rec_len, inline_size);
+ de_len = ext4_rec_len_from_disk(de->rec_len, inline_size);
+ /* reports rec_len below the minimum and overruns */
if (ext4_check_dir_entry(inode, dir_file, de,
iloc.bh, dir_buf,
inline_size, pos)) {
ret = count;
goto out;
}
+ pos += de_len;
}
if (ext4_hash_in_dirent(dir)) {
diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index 6050dbfd9f78..12ffd34cf46d 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -329,23 +329,33 @@ static struct dx_countlimit *get_dx_countlimit(struct inode *inode,
{
struct ext4_dir_entry_2 *de;
struct dx_root_info *root;
- int count_offset;
+ int count_offset, dotdot_rec_len;
int blocksize = EXT4_BLOCK_SIZE(inode->i_sb);
unsigned int rlen = ext4_rec_len_from_disk(dirent->rec_len, blocksize);
- if (rlen == blocksize)
+ if (rlen == blocksize) {
count_offset = sizeof(struct dx_node);
- else if (rlen == 12) {
- de = (struct ext4_dir_entry_2 *)(((void *)dirent) + 12);
- if (ext4_rec_len_from_disk(de->rec_len, blocksize) != blocksize - 12)
+ } else {
+ if (rlen < EXT4_BASE_DIR_LEN ||
+ rlen + EXT4_BASE_DIR_LEN > blocksize)
+ return NULL;
+ de = (struct ext4_dir_entry_2 *)(((char *)dirent) + rlen);
+ if (ext4_rec_len_from_disk(de->rec_len, blocksize) !=
+ (unsigned int)(blocksize - rlen))
+ return NULL;
+ /* de->rec_len covers whole dx_root block, calculate actual length.
+ * This is the '..' entry, which never carries the casefold+fscrypt
+ * hash, so pass NULL for dir regardless of the directory's flags */
+ dotdot_rec_len = ext4_dir_entry_len(de, blocksize, NULL);
+ if (rlen + dotdot_rec_len + sizeof(struct dx_root_info) +
+ sizeof(struct dx_countlimit) > blocksize)
return NULL;
- root = (struct dx_root_info *)(((void *)de + 12));
+ root = (struct dx_root_info *)(((char *)de + dotdot_rec_len));
if (root->reserved_zero ||
root->info_length != sizeof(struct dx_root_info))
return NULL;
- count_offset = 32;
- } else
- return NULL;
+ count_offset = root->info_length + rlen + dotdot_rec_len;
+ }
if (offset)
*offset = count_offset;
@@ -463,6 +473,7 @@ static struct dx_root_info *dx_get_dx_info(struct inode *dir, void *de_buf)
rlen = ext4_rec_len_from_disk(de->rec_len, blocksize);
if (rlen < EXT4_BASE_DIR_LEN || rlen + EXT4_BASE_DIR_LEN > blocksize ||
+ ext4_dirent_data_corrupt(dir->i_sb, de, rlen) ||
ext4_dir_entry_len(de, blocksize, NULL) > rlen)
return ERR_PTR(-EFSCORRUPTED);
off = rlen;
@@ -470,6 +481,7 @@ static struct dx_root_info *dx_get_dx_info(struct inode *dir, void *de_buf)
de = (struct ext4_dir_entry_2 *)((char *)de_buf + off);
rlen = ext4_rec_len_from_disk(de->rec_len, blocksize);
if (rlen < EXT4_BASE_DIR_LEN || off + rlen > blocksize ||
+ ext4_dirent_data_corrupt(dir->i_sb, de, rlen) ||
ext4_dir_entry_len(de, blocksize, NULL) > rlen)
return ERR_PTR(-EFSCORRUPTED);
off += ext4_dir_entry_len(de, blocksize, NULL);
@@ -586,7 +598,9 @@ static struct stats dx_show_leaf(struct inode *dir,
(unsigned) ((char *) de - base));
#endif
}
- space += ext4_dirent_rec_len(de->name_len, dir);
+ if ((char *)de + EXT4_BASE_DIR_LEN > base + size)
+ break;
+ space += ext4_dir_entry_len(de, size, dir);
names++;
}
de = ext4_next_entry(de, size);
@@ -1953,7 +1967,8 @@ int ext4_find_dest_de(struct inode *dir, struct buffer_head *bh,
int dlen)
{
struct ext4_dir_entry_2 *de;
- unsigned short reclen = ext4_dirent_rec_len(fname_len(fname) + dlen, dir);
+ unsigned short reclen = ext4_dirent_new_rec_len(dir, fname_len(fname),
+ dlen);
int nlen, rlen;
unsigned int offset = 0;
char *top;
@@ -1981,13 +1996,10 @@ int ext4_find_dest_de(struct inode *dir, struct buffer_head *bh,
}
EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_find_dest_de);
-void ext4_insert_dentry(struct inode *dir,
- struct inode *inode,
- struct ext4_dir_entry_2 *de,
- int buf_size,
- struct ext4_filename *fname)
+void ext4_insert_dentry_data(struct inode *dir, struct inode *inode,
+ struct ext4_dir_entry_2 *de, int buf_size,
+ struct ext4_filename *fname, void *data)
{
-
int nlen, rlen;
nlen = ext4_dir_entry_len(de, buf_size, dir);
@@ -2004,6 +2016,10 @@ void ext4_insert_dentry(struct inode *dir,
ext4_set_de_type(inode->i_sb, de, inode->i_mode);
de->name_len = fname_len(fname);
memcpy(de->name, fname_name(fname), fname_len(fname));
+ /* 'data' (the LUFID payload) and the CFHASH are written by
+ * ext4_dirdata_set(), which is introduced in the next patch that adds
+ * the dirdata set/get helpers. Until then, only the casefold hash is
+ * stored at the legacy fixed offset for encrypted+casefolded dirs. */
if (ext4_hash_in_dirent(dir)) {
struct dx_hash_info *hinfo = &fname->hinfo;
@@ -2012,7 +2028,7 @@ void ext4_insert_dentry(struct inode *dir,
cpu_to_le32(hinfo->minor_hash);
}
}
-EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_insert_dentry);
+EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_insert_dentry_data);
/*
* Add a new entry into a directory (leaf) block. If de is non-NULL,
@@ -2029,14 +2045,20 @@ static int add_dirent_to_buf(handle_t *handle, struct ext4_filename *fname,
{
unsigned int blocksize = dir->i_sb->s_blocksize;
int csum_size = 0;
- int err, err2;
+ int err, err2, dlen = 0;
+ struct ext4_dirent_fid *dfid;
if (ext4_has_feature_metadata_csum(inode->i_sb))
csum_size = sizeof(struct ext4_dir_entry_tail);
+ /* dfid is resolved once by ext4_add_entry() and stored in fname so
+ * that all add_dirent_to_buf() calls within the same add operation
+ * use a consistent value without touching the shared i_dirdata field. */
+ dfid = fname->dfid;
+ dlen = ext4_dirent_new_data_len(dir, dfid);
if (!de) {
err = ext4_find_dest_de(dir, bh, bh->b_data,
- blocksize - csum_size, fname, &de, 0);
+ blocksize - csum_size, fname, &de, dlen);
if (err)
return err;
} else {
@@ -2051,8 +2073,8 @@ static int add_dirent_to_buf(handle_t *handle, struct ext4_filename *fname,
unsigned int rlen = ext4_rec_len_from_disk(de->rec_len,
blocksize);
- if (nlen > rlen ||
- rlen - nlen < ext4_dirent_rec_len(fname_len(fname), dir)) {
+ if (nlen > rlen || rlen - nlen <
+ ext4_dirent_new_rec_len(dir, fname_len(fname), dlen)) {
EXT4_ERROR_INODE(dir,
"no room for new entry in split block %llu",
(unsigned long long)bh->b_blocknr);
@@ -2069,7 +2091,7 @@ static int add_dirent_to_buf(handle_t *handle, struct ext4_filename *fname,
}
/* By now the buffer is marked for journaling */
- ext4_insert_dentry(dir, inode, de, blocksize, fname);
+ ext4_insert_dentry_data(dir, inode, de, blocksize, fname, dfid);
/*
* XXX shouldn't update any times until successful
@@ -2311,7 +2333,8 @@ static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname,
*/
static int __ext4_add_entry(handle_t *handle, struct inode *dir,
const struct qstr *d_name,
- struct inode *inode)
+ struct inode *inode,
+ struct ext4_dentry_param *edp)
{
struct buffer_head *bh = NULL;
struct ext4_dir_entry_2 *de;
@@ -2336,6 +2359,13 @@ static int __ext4_add_entry(handle_t *handle, struct inode *dir,
if (retval)
return retval;
+ /* Resolve the LUFID payload once here and store it in fname so every
+ * add_dirent_to_buf() call in this add operation uses the same value
+ * without touching the shared EXT4_I(inode)->i_dirdata field. */
+ fname.dfid = ext4_dentry_get_fid(sb, edp);
+ if (fname.dfid)
+ ext4_fc_mark_ineligible(sb, EXT4_FC_REASON_DIRDATA, handle);
+
if (ext4_has_inline_data(dir)) {
retval = ext4_try_add_inline_entry(handle, &fname, dir, inode);
if (retval < 0)
@@ -2420,7 +2450,8 @@ static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
if (fscrypt_is_nokey_name(dentry))
return -ENOKEY;
- return __ext4_add_entry(handle, dir, &dentry->d_name, inode);
+ return __ext4_add_entry(handle, dir, &dentry->d_name, inode,
+ dentry->d_fsdata);
}
/*
@@ -2930,8 +2961,9 @@ int ext4_init_dirblock(handle_t *handle, struct inode *inode,
}
EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_init_dirblock);
-int ext4_init_new_dir(handle_t *handle, struct inode *dir,
- struct inode *inode)
+int ext4_init_new_dir_data(handle_t *handle, struct inode *dir,
+ struct inode *inode,
+ const void *data1, const void *data2)
{
struct buffer_head *dir_block = NULL;
ext4_lblk_t block = 0;
@@ -2955,7 +2987,7 @@ int ext4_init_new_dir(handle_t *handle, struct inode *dir,
brelse(dir_block);
return err;
}
-EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_init_new_dir);
+EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_init_new_dir_data);
static struct dentry *ext4_mkdir(const struct mnt_idmap *idmap, struct inode *dir,
struct dentry *dentry, umode_t mode)
@@ -3435,10 +3467,16 @@ int __ext4_link(struct inode *dir, struct inode *inode,
if (IS_DIRSYNC(dir))
ext4_handle_sync(handle);
+ /* Fast-commit does not record dirdata in its journal format; force a
+ * full journal commit so the LUFID survives crash+replay. */
+ if (ext4_dentry_get_fid(dir->i_sb, dentry ? dentry->d_fsdata : NULL))
+ ext4_fc_mark_ineligible(dir->i_sb, EXT4_FC_REASON_DIRDATA, handle);
+
inode_set_ctime_current(inode);
ext4_inc_count(inode);
- err = __ext4_add_entry(handle, dir, d_name, inode);
+ err = __ext4_add_entry(handle, dir, d_name, inode,
+ dentry ? dentry->d_fsdata : NULL);
if (!err) {
err = ext4_mark_inode_dirty(handle, inode);
/* this can happen only for tmpfile being
diff --git a/fs/ext4/sysfs.c b/fs/ext4/sysfs.c
index faa76f021099..a433418a8182 100644
--- a/fs/ext4/sysfs.c
+++ b/fs/ext4/sysfs.c
@@ -362,6 +362,7 @@ EXT4_ATTR_FEATURE(verity);
#endif
EXT4_ATTR_FEATURE(metadata_csum_seed);
EXT4_ATTR_FEATURE(fast_commit);
+EXT4_ATTR_FEATURE(dirdata);
#if IS_ENABLED(CONFIG_UNICODE) && defined(CONFIG_FS_ENCRYPTION)
EXT4_ATTR_FEATURE(encrypted_casefold);
#endif
@@ -385,6 +386,7 @@ static struct attribute *ext4_feat_attrs[] = {
#endif
ATTR_LIST(metadata_csum_seed),
ATTR_LIST(fast_commit),
+ ATTR_LIST(dirdata),
#if IS_ENABLED(CONFIG_UNICODE) && defined(CONFIG_FS_ENCRYPTION)
ATTR_LIST(encrypted_casefold),
#endif
--
2.52.0
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v15 10/13] ext4: add dirdata LUFID support for directory entry rename
2026-12-03 18:49 [PATCH v15 00/13] Data in direntry (dirdata) feature, with KUnit tests Artem Blagodarenko
` (8 preceding siblings ...)
2026-12-03 18:49 ` [PATCH v15 09/13] ext4: dirdata feature Artem Blagodarenko
@ 2026-12-03 18:49 ` Artem Blagodarenko
2026-12-03 22:02 ` sashiko-bot
2026-12-03 18:49 ` [PATCH v15 11/13] ext4: add dirdata set/get helpers Artem Blagodarenko
` (3 subsequent siblings)
13 siblings, 1 reply; 26+ messages in thread
From: Artem Blagodarenko @ 2026-12-03 18:49 UTC (permalink / raw)
To: linux-ext4; +Cc: tytso, jack, adilger.kernel, ablagodarenko, Artem Blagodarenko
ext4_setent() updates the inode number and file_type in an existing
directory entry but did not handle the dirdata extension payload.
Extend it with a src_fid parameter so that the source LUFID travels
with the inode through rename.
When the destination entry has a LUFID record of the same size, the
source record is copied over it. Otherwise the LUFID record of the
destination is removed, moving any records that follow it down, and
the source record is added in place if the entry has room for it. If
it has not, the entry is left without a LUFID.
CFHASH is a property of the destination name, not of the inode, so it
is kept.
Both ext4_rename() and ext4_cross_rename() snapshot the source LUFID
into a stack buffer before any setent() call, so that a cross-directory
swap reads both sides before either is overwritten. When a
RENAME_WHITEOUT rename fails after the old entry became the whiteout,
ext4_resetent() puts the snapshot back into the old entry.
Signed-off-by: Artem Blagodarenko <artem.blagodarenko@gmail.com>
Assisted-by: Claude:claude-opus-5-5
---
fs/ext4/namei.c | 261 ++++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 243 insertions(+), 18 deletions(-)
diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index 12ffd34cf46d..8a1c292ddd2f 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -3467,11 +3467,6 @@ int __ext4_link(struct inode *dir, struct inode *inode,
if (IS_DIRSYNC(dir))
ext4_handle_sync(handle);
- /* Fast-commit does not record dirdata in its journal format; force a
- * full journal commit so the LUFID survives crash+replay. */
- if (ext4_dentry_get_fid(dir->i_sb, dentry ? dentry->d_fsdata : NULL))
- ext4_fc_mark_ineligible(dir->i_sb, EXT4_FC_REASON_DIRDATA, handle);
-
inode_set_ctime_current(inode);
ext4_inc_count(inode);
@@ -3645,8 +3640,81 @@ static int ext4_rename_dir_finish(handle_t *handle, struct ext4_renament *ent,
return 0;
}
+/*
+ * ext4_dirent_drop_lufid() - remove the LUFID record from a directory entry
+ * @de: directory entry
+ * @rec_len: decoded rec_len of @de
+ *
+ * The records that follow LUFID (such as CFHASH) are moved down so they
+ * stay where the parser expects them. If the records cannot be parsed,
+ * none of them can be trusted and all dirdata flags are cleared.
+ */
+static void ext4_dirent_drop_lufid(struct ext4_dir_entry_2 *de,
+ unsigned int rec_len)
+{
+ unsigned int off = EXT4_BASE_DIR_LEN + de->name_len + 1;
+ unsigned int dlen, end, llen;
+
+ if (!(de->file_type & EXT4_DIRENT_LUFID))
+ return;
+ dlen = ext4_dirent_get_data_len(de, rec_len);
+ if (!dlen) {
+ de->file_type &= EXT4_FT_MASK;
+ return;
+ }
+ /* ext4_dirent_get_data_len() checked every record against rec_len */
+ llen = *((u8 *)de + off);
+ end = EXT4_BASE_DIR_LEN + de->name_len + dlen;
+ memmove((char *)de + off, (char *)de + off + llen, end - off - llen);
+ de->file_type &= ~EXT4_DIRENT_LUFID;
+}
+
+/*
+ * ext4_dirent_add_lufid() - add a LUFID record to a directory entry in place
+ * @dir: directory inode
+ * @de: directory entry without a LUFID record
+ * @rec_len: decoded rec_len of @de
+ * @fid: LUFID record to add
+ *
+ * LUFID is the first record, so any records already present are moved up.
+ * Returns false, leaving @de untouched, when the entry has no room.
+ */
+static bool ext4_dirent_add_lufid(struct inode *dir,
+ struct ext4_dir_entry_2 *de,
+ unsigned int rec_len,
+ const struct ext4_dirent_fid *fid)
+{
+ unsigned int off = EXT4_BASE_DIR_LEN + de->name_len + 1;
+ unsigned int flen = fid->df_header.ddh_length;
+ unsigned int dlen = ext4_dirent_get_data_len(de, rec_len);
+ unsigned int end;
+
+ if ((de->file_type & ~EXT4_FT_MASK) && !dlen)
+ return false;
+ /* without a CFHASH record the hash sits right after the name */
+ if (ext4_hash_in_dirent(dir) && !(de->file_type & EXT4_DIRENT_CFHASH))
+ return false;
+ end = dlen ? EXT4_BASE_DIR_LEN + de->name_len + dlen : off;
+ if (end + flen > rec_len)
+ return false;
+ memmove((char *)de + off + flen, (char *)de + off, end - off);
+ *((char *)de + EXT4_BASE_DIR_LEN + de->name_len) = 0;
+ memcpy((char *)de + off, fid, flen);
+ de->file_type |= EXT4_DIRENT_LUFID;
+ return true;
+}
+
+/*
+ * ext4_setent() - point a directory entry at another inode
+ * @src_fid: LUFID record of that inode, or NULL
+ *
+ * LUFID is inode-specific, so the record of @ent is replaced by @src_fid.
+ * Other records (CFHASH) belong to the name and are kept. If the entry
+ * has no room for @src_fid, the entry is left without a LUFID.
+ */
static int ext4_setent(handle_t *handle, struct ext4_renament *ent,
- unsigned ino, unsigned file_type)
+ unsigned ino, unsigned file_type,
+ const struct ext4_dirent_fid *src_fid)
{
int retval, retval2;
@@ -3656,8 +3724,33 @@ static int ext4_setent(handle_t *handle, struct ext4_renament *ent,
if (retval)
return retval;
ent->de->inode = cpu_to_le32(ino);
- if (ext4_has_feature_filetype(ent->dir->i_sb))
- ent->de->file_type = file_type;
+ if (ext4_has_feature_filetype(ent->dir->i_sb)) {
+ if (ext4_has_feature_dirdata(ent->dir->i_sb)) {
+ unsigned int rec_len =
+ ext4_rec_len_from_disk(ent->de->rec_len,
+ ent->dir->i_sb->s_blocksize);
+ unsigned int off =
+ EXT4_BASE_DIR_LEN + ent->de->name_len + 1;
+ struct ext4_dirent_fid *fid =
+ (struct ext4_dirent_fid *)((char *)ent->de + off);
+
+ if (src_fid &&
+ (ent->de->file_type & EXT4_DIRENT_LUFID) &&
+ ext4_dirent_get_data_len(ent->de, rec_len) &&
+ fid->df_header.ddh_length ==
+ src_fid->df_header.ddh_length) {
+ memcpy(fid, src_fid,
+ src_fid->df_header.ddh_length);
+ } else {
+ ext4_dirent_drop_lufid(ent->de, rec_len);
+ if (src_fid)
+ ext4_dirent_add_lufid(ent->dir, ent->de,
+ rec_len, src_fid);
+ }
+ }
+ ent->de->file_type = (file_type & EXT4_FT_MASK) |
+ (ent->de->file_type & ~EXT4_FT_MASK);
+ }
inode_inc_iversion(ent->dir);
inode_set_mtime_to_ts(ent->dir, inode_set_ctime_current(ent->dir));
retval = ext4_mark_inode_dirty(handle, ent->dir);
@@ -3673,7 +3766,8 @@ static int ext4_setent(handle_t *handle, struct ext4_renament *ent,
}
static void ext4_resetent(handle_t *handle, struct ext4_renament *ent,
- unsigned ino, unsigned file_type)
+ unsigned ino, unsigned file_type,
+ const struct ext4_dirent_fid *fid)
{
struct ext4_renament old = *ent;
int retval = 0;
@@ -3694,7 +3788,7 @@ static void ext4_resetent(handle_t *handle, struct ext4_renament *ent,
return;
}
- ext4_setent(handle, &old, ino, file_type);
+ ext4_setent(handle, &old, ino, file_type, fid);
brelse(old.bh);
}
@@ -3793,6 +3887,69 @@ static struct inode *ext4_whiteout_for_rename(const struct mnt_idmap *idmap,
return wh;
}
+/*
+ * Stack buffer sized for 1-byte header + 4 × 16-byte FIDs, covering virtually
+ * all real-world LUFID payloads without a heap allocation. ddh_length is u8
+ * so the absolute maximum is 255 bytes; larger payloads fall back to kmalloc.
+ */
+#define EXT4_LUFID_INLINE_SIZE 65
+
+struct ext4_lufid_snap {
+ struct ext4_dirent_fid *fid; /* NULL or valid; never ERR_PTR */
+ bool on_heap;
+ u8 buf[EXT4_LUFID_INLINE_SIZE];
+};
+
+/*
+ * ext4_lufid_snapshot - copy the LUFID record from a directory entry
+ *
+ * Fills @snap with a copy of the on-disk LUFID record. For payloads up to
+ * EXT4_LUFID_INLINE_SIZE bytes the inline buffer is used; larger payloads
+ * fall back to a heap allocation. Returns 0 on success (snap->fid is NULL
+ * when no LUFID is present), or -ENOMEM when a heap allocation fails.
+ * Call ext4_lufid_snap_release() when done.
+ */
+static int ext4_lufid_snapshot(struct ext4_dir_entry_2 *de,
+ unsigned int blocksize,
+ struct ext4_lufid_snap *snap)
+{
+ unsigned int ddh_off = EXT4_BASE_DIR_LEN + de->name_len + 1;
+ unsigned int rec_len = ext4_rec_len_from_disk(de->rec_len, blocksize);
+ struct ext4_dirent_fid *disk_fid;
+ unsigned int dlen;
+
+ snap->fid = NULL;
+ snap->on_heap = false;
+
+ if (!(de->file_type & EXT4_DIRENT_LUFID))
+ return 0;
+ if (ddh_off + sizeof(disk_fid->df_header) > rec_len)
+ return 0;
+ disk_fid = (struct ext4_dirent_fid *)((char *)de + ddh_off);
+ dlen = READ_ONCE(disk_fid->df_header.ddh_length);
+ if (dlen < sizeof(disk_fid->df_header) || ddh_off + dlen > rec_len)
+ return 0;
+
+ if (dlen <= sizeof(snap->buf)) {
+ memcpy(snap->buf, disk_fid, dlen);
+ snap->fid = (struct ext4_dirent_fid *)snap->buf;
+ } else {
+ snap->fid = kmemdup(disk_fid, dlen, GFP_NOFS);
+ if (!snap->fid)
+ return -ENOMEM;
+ snap->on_heap = true;
+ }
+ /* the length byte may have changed under us: keep the checked one */
+ snap->fid->df_header.ddh_length = dlen;
+ return 0;
+}
+
+static void ext4_lufid_snap_release(struct ext4_lufid_snap *snap)
+{
+ if (snap->on_heap)
+ kfree(snap->fid);
+}
+
/*
* Anybody can rename anything with this: the permission checks are left to the
* higher-level routines.
@@ -3819,8 +3976,11 @@ static int ext4_rename(const struct mnt_idmap *idmap, struct inode *old_dir,
int force_reread;
int retval;
struct inode *whiteout = NULL;
+ struct ext4_dentry_param *edp = NULL;
int credits;
u8 old_file_type;
+ struct ext4_lufid_snap old_snap = {};
+ bool old_replaced = false;
if (new.inode && new.inode->i_nlink == 0) {
EXT4_ERROR_INODE(new.inode,
@@ -3926,29 +4086,67 @@ static int ext4_rename(const struct mnt_idmap *idmap, struct inode *old_dir,
force_reread = (new.dir->i_ino == old.dir->i_ino &&
ext4_test_inode_flag(new.dir, EXT4_INODE_INLINE_DATA));
+ if (ext4_has_feature_dirdata(old.dir->i_sb)) {
+ /* Snapshot before whiteout's ext4_setent() clears the LUFID
+ * flag in old.de; the snapshot is also needed for the !new.bh
+ * path below where we must carry the LUFID to the new entry. */
+ retval = ext4_lufid_snapshot(old.de,
+ old.dir->i_sb->s_blocksize,
+ &old_snap);
+ if (retval)
+ goto end_rename;
+ if (!old_snap.fid)
+ old_file_type &= ~EXT4_DIRENT_LUFID;
+ }
+
+ /* Allocate the LUFID wrapper for ext4_add_entry() before any
+ * destructive changes to the old entry. An OOM failure after
+ * ext4_setent() has already compacted the extensions would trigger
+ * the lossy resetent path; pre-allocating here avoids that. */
+ if (!new.bh && old_snap.fid) {
+ unsigned fid_size = old_snap.fid->df_header.ddh_length;
+
+ edp = kmalloc(offsetof(struct ext4_dentry_param,
+ edp_dfid) + fid_size, GFP_NOFS);
+ if (!edp) {
+ retval = -ENOMEM;
+ goto end_rename;
+ }
+ edp->edp_magic = EXT4_LUFID_MAGIC;
+ memcpy(&edp->edp_dfid, old_snap.fid, fid_size);
+ }
+
if (whiteout) {
/*
* Do this before adding a new entry, so the old entry is sure
* to be still pointing to the valid old entry.
*/
+ /* from here on, a failure must put the old entry back */
+ old_replaced = true;
retval = ext4_setent(handle, &old, whiteout->i_ino,
- EXT4_FT_CHRDEV);
+ EXT4_FT_CHRDEV, NULL);
if (retval)
goto end_rename;
retval = ext4_mark_inode_dirty(handle, whiteout);
if (unlikely(retval))
goto end_rename;
-
}
if (!new.bh) {
+ new.dentry->d_fsdata = edp;
retval = ext4_add_entry(handle, new.dentry, old.inode);
+ new.dentry->d_fsdata = NULL;
if (retval)
goto end_rename;
} else {
retval = ext4_setent(handle, &new,
- old.inode->i_ino, old_file_type);
+ old.inode->i_ino, old_file_type, old_snap.fid);
if (retval)
goto end_rename;
+ /* ext4_setent() writes the LUFID in-place without going through
+ * __ext4_add_entry(), so mark ineligible here if needed. */
+ if (old_snap.fid)
+ ext4_fc_mark_ineligible(old.dir->i_sb,
+ EXT4_FC_REASON_DIRDATA, handle);
}
if (force_reread)
force_reread = !ext4_test_inode_flag(new.dir,
@@ -4033,10 +4231,12 @@ static int ext4_rename(const struct mnt_idmap *idmap, struct inode *old_dir,
retval = 0;
end_rename:
+ kfree(edp);
if (whiteout) {
if (retval) {
- ext4_resetent(handle, &old,
- old.inode->i_ino, old_file_type);
+ if (old_replaced)
+ ext4_resetent(handle, &old, old.inode->i_ino,
+ old_file_type, old_snap.fid);
drop_nlink(whiteout);
ext4_mark_inode_dirty(handle, whiteout);
ext4_orphan_add(handle, whiteout);
@@ -4048,6 +4248,7 @@ static int ext4_rename(const struct mnt_idmap *idmap, struct inode *old_dir,
ext4_journal_stop(handle);
}
release_bh:
+ ext4_lufid_snap_release(&old_snap);
brelse(old.dir_bh);
brelse(old.bh);
brelse(new.bh);
@@ -4069,8 +4270,9 @@ static int ext4_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
.dentry = new_dentry,
.inode = d_inode(new_dentry),
};
- u8 new_file_type;
+ u8 new_file_type, old_de_file_type;
int retval;
+ struct ext4_lufid_snap old_snap = {}, new_snap = {};
if ((ext4_test_inode_flag(new_dir, EXT4_INODE_PROJINHERIT) &&
!projid_eq(EXT4_I(new_dir)->i_projid,
@@ -4149,12 +4351,33 @@ static int ext4_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
goto end_rename;
}
+ if (ext4_has_feature_dirdata(old.dir->i_sb)) {
+ retval = ext4_lufid_snapshot(old.de, old.dir->i_sb->s_blocksize,
+ &old_snap);
+ if (retval)
+ goto end_rename;
+ retval = ext4_lufid_snapshot(new.de, new.dir->i_sb->s_blocksize,
+ &new_snap);
+ if (retval)
+ goto end_rename;
+ if (old_snap.fid || new_snap.fid)
+ ext4_fc_mark_ineligible(old.dir->i_sb,
+ EXT4_FC_REASON_DIRDATA, handle);
+ }
+
+ old_de_file_type = old.de->file_type;
+ if (!old_snap.fid)
+ old_de_file_type &= ~EXT4_DIRENT_LUFID;
new_file_type = new.de->file_type;
- retval = ext4_setent(handle, &new, old.inode->i_ino, old.de->file_type);
+ if (!new_snap.fid)
+ new_file_type &= ~EXT4_DIRENT_LUFID;
+ retval = ext4_setent(handle, &new, old.inode->i_ino, old_de_file_type,
+ old_snap.fid);
if (retval)
goto end_rename;
- retval = ext4_setent(handle, &old, new.inode->i_ino, new_file_type);
+ retval = ext4_setent(handle, &old, new.inode->i_ino, new_file_type,
+ new_snap.fid);
if (retval)
goto end_rename;
@@ -4187,6 +4410,8 @@ static int ext4_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
retval = 0;
end_rename:
+ ext4_lufid_snap_release(&old_snap);
+ ext4_lufid_snap_release(&new_snap);
brelse(old.dir_bh);
brelse(new.dir_bh);
brelse(old.bh);
--
2.52.0
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v15 11/13] ext4: add dirdata set/get helpers
2026-12-03 18:49 [PATCH v15 00/13] Data in direntry (dirdata) feature, with KUnit tests Artem Blagodarenko
` (9 preceding siblings ...)
2026-12-03 18:49 ` [PATCH v15 10/13] ext4: add dirdata LUFID support for directory entry rename Artem Blagodarenko
@ 2026-12-03 18:49 ` Artem Blagodarenko
2026-12-03 22:30 ` sashiko-bot
2026-12-03 18:49 ` [PATCH v15 12/13] ext4: Add EXT4_IOC_SET_LUFID ioctl for setting LUFID on directory entries Artem Blagodarenko
` (2 subsequent siblings)
13 siblings, 1 reply; 26+ messages in thread
From: Artem Blagodarenko @ 2026-12-03 18:49 UTC (permalink / raw)
To: linux-ext4; +Cc: tytso, jack, adilger.kernel, ablagodarenko, Artem Blagodarenko
From: Artem Blagodarenko <artem.blagodarenko@gmail.com>
Add helpers to set and retrieve dirdata payload and hook them up at
the appropriate call sites.
Enable dirdata for casefold+encryption hashes and storing unique
128-bit file identifier in the directory entry for testing.
Signed-off-by: Artem Blagodarenko <artem.blagodarenko@gmail.com>
Assisted-by: Claude:claude-opus-5-5
---
fs/ext4/dir.c | 8 +-
fs/ext4/ext4.h | 7 ++
fs/ext4/inline.c | 10 +-
fs/ext4/namei.c | 319 +++++++++++++++++++++++++++++++++++++++++------
4 files changed, 298 insertions(+), 46 deletions(-)
diff --git a/fs/ext4/dir.c b/fs/ext4/dir.c
index 4372e23abb81..896edb80b3fb 100644
--- a/fs/ext4/dir.c
+++ b/fs/ext4/dir.c
@@ -304,9 +304,13 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)
u32 hash;
u32 minor_hash;
+ struct dx_hash_info hi = {};
+
if (IS_CASEFOLDED(inode)) {
- hash = EXT4_DIRENT_HASH(de);
- minor_hash = EXT4_DIRENT_MINOR_HASH(de);
+ ext4_dirent_hash_get(de, inode,
+ sb->s_blocksize, &hi);
+ hash = hi.hash;
+ minor_hash = hi.minor_hash;
} else {
hash = 0;
minor_hash = 0;
diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 98014749e5d4..0083606e99d0 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -4206,6 +4206,13 @@ extern int __ext4_unlink(struct inode *dir, const struct qstr *d_name,
struct inode *inode, struct dentry *dentry);
extern int __ext4_link(struct inode *dir, struct inode *inode,
const struct qstr *d_name, struct dentry *dentry);
+extern bool ext4_dirent_hash_get(struct ext4_dir_entry_2 *de,
+ struct inode *dir, int buf_size,
+ struct dx_hash_info *hinfo);
+extern unsigned char ext4_dirdata_get(struct ext4_dir_entry_2 *de,
+ struct inode *dir, int buf_size,
+ struct ext4_dirent_fid *lufid,
+ struct dx_hash_info *hinfo);
#define S_SHIFT 12
static const unsigned char ext4_type_by_mode[(S_IFMT >> S_SHIFT) + 1] = {
diff --git a/fs/ext4/inline.c b/fs/ext4/inline.c
index d43d9d03d0a1..13309768528d 100644
--- a/fs/ext4/inline.c
+++ b/fs/ext4/inline.c
@@ -1380,10 +1380,14 @@ int ext4_inlinedir_to_tree(struct file *dir_file,
}
if (ext4_hash_in_dirent(dir)) {
- hinfo->hash = EXT4_DIRENT_HASH(de);
- hinfo->minor_hash = EXT4_DIRENT_MINOR_HASH(de);
+ if (!ext4_dirent_hash_get(de, dir, inline_size,
+ hinfo)) {
+ hinfo->hash = 0;
+ hinfo->minor_hash = 0;
+ }
} else {
- err = ext4fs_dirhash(dir, de->name, de->name_len, hinfo);
+ err = ext4fs_dirhash(dir, de->name, de->name_len,
+ hinfo);
if (err) {
ret = err;
goto out;
diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index 8a1c292ddd2f..ae58ce5120e1 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -1000,22 +1000,26 @@ static int htree_dirblock_to_tree(struct file *dir_file,
/* silently ignore the rest of the block */
break;
}
- if (ext4_hash_in_dirent(dir)) {
- if (de->name_len && de->inode) {
- hinfo->hash = EXT4_DIRENT_HASH(de);
- hinfo->minor_hash = EXT4_DIRENT_MINOR_HASH(de);
+ if (de->name_len && de->inode) {
+ if (ext4_hash_in_dirent(dir)) {
+ if (!ext4_dirent_hash_get(de, dir,
+ dir->i_sb->s_blocksize, hinfo)) {
+ hinfo->hash = 0;
+ hinfo->minor_hash = 0;
+ }
} else {
- hinfo->hash = 0;
- hinfo->minor_hash = 0;
+ err = ext4fs_dirhash(dir, de->name,
+ de->name_len, hinfo);
+ if (err < 0) {
+ count = err;
+ goto errout;
+ }
}
} else {
- err = ext4fs_dirhash(dir, de->name,
- de->name_len, hinfo);
- if (err < 0) {
- count = err;
- goto errout;
- }
+ hinfo->hash = 0;
+ hinfo->minor_hash = 0;
}
+
if ((hinfo->hash < start_hash) ||
((hinfo->hash == start_hash) &&
(hinfo->minor_hash < start_minor_hash)))
@@ -1196,6 +1200,239 @@ static inline int search_dirblock(struct buffer_head *bh,
* Directory block splitting, compacting
*/
+/*
+ * ext4_dirdata_get() - Read dirdata fields from a directory entry.
+ * @de: directory entry
+ * @dir: directory inode (used for fscrypt+casefold hash fallback)
+ * @buf_size: size of the buffer holding @de
+ * @dfid: if non-NULL and EXT4_DIRENT_LUFID is set, LUFID data is
+ * copied here
+ * @hinfo: if non-NULL, receives the casefold hash and minor hash
+ *
+ * Reads any dirdata stored in @de. If the dirdata feature is not enabled,
+ * falls back to reading the hash stored inline after the filename (for
+ * compatibility with the older casefold+fscrypt format).
+ *
+ * Returns a bitmask of EXT4_DIRENT_* flags indicating which fields were read.
+ *
+ * Compatibility note: enabling EXT4_FEATURE_INCOMPAT_DIRDATA on a filesystem
+ * that already has casefolded+encrypted directories is NOT safe without a
+ * prior migration pass. Before dirdata, the casefold+fscrypt hash was stored
+ * as a raw 8 bytes immediately after the filename with no flag in file_type.
+ * After dirdata is enabled, this function expects the hash to be present only
+ * when EXT4_DIRENT_CFHASH (0x40) is set in file_type; existing entries that
+ * carry the raw hash are silently misread as having no hash at all, breaking
+ * directory lookups. e2fsck must be run to convert all affected entries to
+ * the EXT4_DIRENT_CFHASH extension format before the feature flag is set with
+ * tune2fs. Detection heuristic: in a casefold+encrypted directory, an entry
+ * with rec_len >= round_up(name_len, 4) + 8 and no EXT4_DIRENT_CFHASH bit
+ * carries a raw pre-dirdata hash that must be migrated.
+ */
+unsigned char ext4_dirdata_get(struct ext4_dir_entry_2 *de, struct inode *dir,
+ int buf_size,
+ struct ext4_dirent_fid *dfid,
+ struct dx_hash_info *hinfo)
+{
+ unsigned char ret = 0;
+ unsigned int data_offset = de->name_len + 1;
+ unsigned int rec_len = ext4_rec_len_from_disk(de->rec_len, buf_size);
+
+ /* data_offset is relative to de->name, which itself starts
+ * EXT4_BASE_DIR_LEN bytes into the entry -- rec_len is relative to
+ * the start of the entry, so add the header size before comparing,
+ * or this lets reads run EXT4_BASE_DIR_LEN bytes past the entry. */
+ if (EXT4_BASE_DIR_LEN + data_offset > rec_len)
+ return ret;
+
+ /* compatibility: hash stored inline after filename (no dirdata) */
+ if (hinfo && !ext4_has_feature_dirdata(dir->i_sb) &&
+ ext4_hash_in_dirent(dir)) {
+ if (ext4_dirent_rec_len(de->name_len, dir) > rec_len)
+ return ret;
+ hinfo->hash = EXT4_DIRENT_HASH(de);
+ hinfo->minor_hash = EXT4_DIRENT_MINOR_HASH(de);
+ ret |= EXT4_DIRENT_CFHASH;
+ return ret;
+ }
+
+ /* EXT4_DIRENT_* bits are only meaningful when the feature is enabled */
+ if (!ext4_has_feature_dirdata(dir->i_sb))
+ return ret;
+
+ if (de->file_type & EXT4_DIRENT_LUFID) {
+ struct ext4_dirent_fid *disk_fid =
+ (struct ext4_dirent_fid *)((char *)de +
+ EXT4_BASE_DIR_LEN + data_offset);
+ unsigned int dlen;
+ /* struct ext4_fid df_fid[] does not provide the array size.
+ * First, verify that the header lies within the valid area, then
+ * verify that the entire record fits within it. */
+ if (EXT4_BASE_DIR_LEN + data_offset +
+ sizeof(disk_fid->df_header) > rec_len)
+ return ret;
+
+ dlen = disk_fid->df_header.ddh_length;
+ if (dlen == 0 ||
+ EXT4_BASE_DIR_LEN + data_offset + dlen > rec_len)
+ return ret;
+
+ if (dfid) {
+ memcpy(dfid, disk_fid, dlen);
+ ret |= EXT4_DIRENT_LUFID;
+ }
+ data_offset += dlen;
+ }
+
+ /* Skip INO64 for now*/
+ if (de->file_type & EXT4_DIRENT_INO64) {
+ struct ext4_dirent_data_header *ddh =
+ (struct ext4_dirent_data_header *)((char *)de +
+ EXT4_BASE_DIR_LEN + data_offset);
+ unsigned int dlen;
+
+ if (EXT4_BASE_DIR_LEN + data_offset + sizeof(*ddh) > rec_len)
+ return ret;
+
+ dlen = ddh->ddh_length;
+ if (dlen < sizeof(*ddh) ||
+ EXT4_BASE_DIR_LEN + data_offset + dlen > rec_len)
+ return ret;
+
+ data_offset += dlen;
+ }
+
+ if (!hinfo)
+ return ret;
+
+ if (de->file_type & EXT4_DIRENT_CFHASH) {
+ struct ext4_dirent_hash *dh =
+ (struct ext4_dirent_hash *)((char *)de + EXT4_BASE_DIR_LEN + data_offset);
+ unsigned int dlen;
+
+ if (EXT4_BASE_DIR_LEN + data_offset + sizeof(*dh) > rec_len)
+ return ret;
+ dlen = dh->dh_header.ddh_length;
+ if (dlen < sizeof(*dh) ||
+ EXT4_BASE_DIR_LEN + data_offset + dlen > rec_len)
+ return ret;
+
+ hinfo->hash = le32_to_cpu(dh->dh_hash);
+ hinfo->minor_hash = le32_to_cpu(dh->dh_minor_hash);
+ ret |= EXT4_DIRENT_CFHASH;
+ }
+
+ return ret;
+}
+
+/*
+ * ext4_dirent_hash_get() - read the hash stored in a casefold+fscrypt entry
+ * @de: directory entry, already checked by ext4_check_dir_entry()
+ * @dir: directory inode, for which ext4_hash_in_dirent() is true
+ * @buf_size: size of the buffer holding @de
+ * @hinfo: receives the hash and minor hash
+ *
+ * The hash is in the CFHASH record when the entry has one, and otherwise
+ * at the fixed offset after the name, as written before dirdata. That
+ * offset is only read when the entry is long enough to hold the hash and
+ * carries no other dirdata record, which would sit at the same offset.
+ * '.' and '..' are such short entries.
+ *
+ * Return: %true if @hinfo was filled in, %false if @de holds no hash.
+ */
+bool ext4_dirent_hash_get(struct ext4_dir_entry_2 *de, struct inode *dir,
+ int buf_size, struct dx_hash_info *hinfo)
+{
+ unsigned int rec_len = ext4_rec_len_from_disk(de->rec_len, buf_size);
+
+ if (ext4_has_feature_dirdata(dir->i_sb) &&
+ (de->file_type & ~EXT4_FT_MASK))
+ return ext4_dirdata_get(de, dir, buf_size, NULL, hinfo) &
+ EXT4_DIRENT_CFHASH;
+
+ if (ext4_dirent_rec_len(de->name_len, dir) > rec_len)
+ return false;
+ hinfo->hash = EXT4_DIRENT_HASH(de);
+ hinfo->minor_hash = EXT4_DIRENT_MINOR_HASH(de);
+ return true;
+}
+
+/*
+ * ext4_dirdata_set() - Write dirdata fields into a directory entry.
+ * @de: directory entry (name must already be set)
+ * @dir: directory inode
+ * @data: LUFID data to store (or NULL)
+ * @fname: filename info carrying the casefold hash
+ *
+ * Writes any required dirdata into @de after the filename. If the dirdata
+ * feature is not enabled, falls back to writing the hash inline after the
+ * filename (for compatibility with the older casefold+fscrypt format).
+ *
+ * See ext4_dirdata_get() for the compatibility constraint: enabling the
+ * dirdata feature on a filesystem with existing casefolded+encrypted
+ * directories requires an e2fsck migration pass before tune2fs sets the
+ * EXT4_FEATURE_INCOMPAT_DIRDATA superblock flag.
+ */
+static void ext4_dirdata_set(struct ext4_dir_entry_2 *de, struct inode *dir,
+ struct ext4_dirent_fid *dfid,
+ struct ext4_filename *fname)
+{
+ struct dx_hash_info *hinfo = &fname->hinfo;
+ unsigned int data_offset = de->name_len + 1;
+ unsigned int rec_len = ext4_rec_len_from_disk(de->rec_len,
+ dir->i_sb->s_blocksize);
+
+ /* Clear the gap byte between the filename and the first dirdata
+ * extension to avoid leaking stale memory to disk. Use pointer
+ * arithmetic rather than de->name[name_len] to stay within the
+ * declared name[] array bounds under FORTIFY_SOURCE. Only write it
+ * when there is actually room (entries that exactly fill their slot
+ * have rec_len == EXT4_BASE_DIR_LEN + name_len with no gap). */
+ if (EXT4_BASE_DIR_LEN + data_offset <= rec_len)
+ *((char *)de + EXT4_BASE_DIR_LEN + de->name_len) = 0;
+
+ if (dfid) {
+ unsigned int dlen = dfid->df_header.ddh_length;
+
+ if (EXT4_BASE_DIR_LEN + data_offset + dlen > rec_len) {
+ EXT4_ERROR_INODE(dir, "Can not insert FID");
+ return;
+ }
+
+ memcpy((char *)de + EXT4_BASE_DIR_LEN + data_offset, dfid, dlen);
+ de->file_type |= EXT4_DIRENT_LUFID;
+ data_offset += dlen;
+ }
+
+ if (ext4_hash_in_dirent(dir)) {
+ if (ext4_has_feature_dirdata(dir->i_sb)) {
+ struct ext4_dirent_hash *dh =
+ (struct ext4_dirent_hash *)((char *)de +
+ EXT4_BASE_DIR_LEN + data_offset);
+
+ if (EXT4_BASE_DIR_LEN + data_offset + sizeof(*dh) > rec_len) {
+ EXT4_ERROR_INODE(dir, "Can not insert dhash dirdata");
+ return;
+ }
+
+ dh->dh_header.ddh_length = sizeof(*dh);
+ dh->dh_hash = cpu_to_le32(hinfo->hash);
+ dh->dh_minor_hash = cpu_to_le32(hinfo->minor_hash);
+ de->file_type |= EXT4_DIRENT_CFHASH;
+ } else {
+ /* Compatibility: store hash inline after filename */
+ if (EXT4_BASE_DIR_LEN + data_offset +
+ sizeof(struct ext4_dir_entry_hash) > rec_len) {
+ EXT4_ERROR_INODE(dir, "Can not insert dhash");
+ return;
+ }
+
+ EXT4_DIRENT_HASHES(de)->hash = cpu_to_le32(hinfo->hash);
+ EXT4_DIRENT_HASHES(de)->minor_hash =
+ cpu_to_le32(hinfo->minor_hash);
+ }
+ }
+}
+
/*
* Create map of hash values, offsets, and sizes, stored at end of block.
* Returns number of entries mapped.
@@ -1219,11 +1456,13 @@ static int dx_make_map(struct inode *dir, struct buffer_head *bh,
((char *)de) - base))
return -EFSCORRUPTED;
if (de->name_len && de->inode) {
- if (ext4_hash_in_dirent(dir))
- h.hash = EXT4_DIRENT_HASH(de);
- else {
+ if (ext4_hash_in_dirent(dir)) {
+ if (!ext4_dirent_hash_get(de, dir, blocksize,
+ &h))
+ return -EFSCORRUPTED;
+ } else {
int err = ext4fs_dirhash(dir, de->name,
- de->name_len, &h);
+ de->name_len, &h);
if (err < 0)
return err;
}
@@ -1327,8 +1566,9 @@ EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_fname_setup_ci_filename);
* Return: %true if the directory entry matches, otherwise %false.
*/
static bool ext4_match(struct inode *parent,
- const struct ext4_filename *fname,
- struct ext4_dir_entry_2 *de)
+ const struct ext4_filename *fname,
+ struct ext4_dir_entry_2 *de,
+ int buf_size)
{
struct fscrypt_name f;
@@ -1355,10 +1595,15 @@ static bool ext4_match(struct inode *parent,
* considering the calculated hash.
*/
if (sb_no_casefold_compat_fallback(parent->i_sb) &&
- IS_ENCRYPTED(parent) && fname->cf_name.name &&
- (fname->hinfo.hash != EXT4_DIRENT_HASH(de) ||
- fname->hinfo.minor_hash != EXT4_DIRENT_MINOR_HASH(de)))
- return false;
+ IS_ENCRYPTED(parent) && fname->cf_name.name) {
+ struct dx_hash_info de_hinfo;
+
+ if (!ext4_dirent_hash_get(de, parent, buf_size,
+ &de_hinfo) ||
+ fname->hinfo.hash != de_hinfo.hash ||
+ fname->hinfo.minor_hash != de_hinfo.minor_hash)
+ return false;
+ }
/*
* Treat comparison errors as not a match. The
* only case where it happens is on a disk
@@ -1389,11 +1634,18 @@ int ext4_search_dir(struct buffer_head *bh, char *search_buf, int buf_size,
dlimit = search_buf + buf_size;
while ((char *) de < dlimit - EXT4_BASE_DIR_LEN) {
/* this code is executed quadratically often */
- /* do minimal checking `by hand' */
+ /* Decode rec_len before ext4_match() so that ext4_dirdata_get()
+ * inside ext4_match() never uses an unvalidated rec_len as a
+ * read bound. A crafted entry with rec_len > buf_size would
+ * let ext4_dirdata_get() read past the block buffer. */
+ de_len = ext4_rec_len_from_disk(de->rec_len,
+ dir->i_sb->s_blocksize);
+ if (de_len <= 0 || (char *)de + de_len > dlimit)
+ return -EFSCORRUPTED;
if (de->name + de->name_len <= dlimit &&
(!ext4_hash_in_dirent(dir) ||
(char *)de + ext4_dirent_rec_len(de->name_len, dir) <= dlimit) &&
- ext4_match(dir, fname, de)) {
+ ext4_match(dir, fname, de, buf_size)) {
/* found a match - just to be sure, do
* a full check */
if (ext4_check_dir_entry(dir, NULL, de, bh, search_buf,
@@ -1402,11 +1654,6 @@ int ext4_search_dir(struct buffer_head *bh, char *search_buf, int buf_size,
*res_dir = de;
return 1;
}
- /* prevent looping on a bad block */
- de_len = ext4_rec_len_from_disk(de->rec_len,
- dir->i_sb->s_blocksize);
- if (de_len <= 0)
- return -EFSCORRUPTED;
offset += de_len;
de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
}
@@ -1979,7 +2226,7 @@ int ext4_find_dest_de(struct inode *dir, struct buffer_head *bh,
if (ext4_check_dir_entry(dir, NULL, de, bh,
buf, buf_size, offset))
return -EFSCORRUPTED;
- if (ext4_match(dir, fname, de))
+ if (ext4_match(dir, fname, de, buf_size))
return -EEXIST;
nlen = ext4_dir_entry_len(de, buf_size, dir);
rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
@@ -2016,17 +2263,7 @@ void ext4_insert_dentry_data(struct inode *dir, struct inode *inode,
ext4_set_de_type(inode->i_sb, de, inode->i_mode);
de->name_len = fname_len(fname);
memcpy(de->name, fname_name(fname), fname_len(fname));
- /* 'data' (the LUFID payload) and the CFHASH are written by
- * ext4_dirdata_set(), which is introduced in the next patch that adds
- * the dirdata set/get helpers. Until then, only the casefold hash is
- * stored at the legacy fixed offset for encrypted+casefolded dirs. */
- if (ext4_hash_in_dirent(dir)) {
- struct dx_hash_info *hinfo = &fname->hinfo;
-
- EXT4_DIRENT_HASHES(de)->hash = cpu_to_le32(hinfo->hash);
- EXT4_DIRENT_HASHES(de)->minor_hash =
- cpu_to_le32(hinfo->minor_hash);
- }
+ ext4_dirdata_set(de, dir, data, fname);
}
EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_insert_dentry_data);
--
2.52.0
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v15 12/13] ext4: Add EXT4_IOC_SET_LUFID ioctl for setting LUFID on directory entries
2026-12-03 18:49 [PATCH v15 00/13] Data in direntry (dirdata) feature, with KUnit tests Artem Blagodarenko
` (10 preceding siblings ...)
2026-12-03 18:49 ` [PATCH v15 11/13] ext4: add dirdata set/get helpers Artem Blagodarenko
@ 2026-12-03 18:49 ` Artem Blagodarenko
2026-12-03 23:01 ` sashiko-bot
2026-12-03 18:49 ` [PATCH v15 13/13] ext4: add KUnit tests for dirdata Artem Blagodarenko
2026-12-03 23:15 ` [PATCH v15 00/13] Data in direntry (dirdata) feature, with KUnit tests sashiko-bot
13 siblings, 1 reply; 26+ messages in thread
From: Artem Blagodarenko @ 2026-12-03 18:49 UTC (permalink / raw)
To: linux-ext4; +Cc: tytso, jack, adilger.kernel, ablagodarenko, Artem Blagodarenko
From: Artem Blagodarenko <artem.blagodarenko@gmail.com>
Add a new ioctl command that allows setting LUFID (Locally Unique File ID)
data on existing directory entries. This includes:
- ext4_ioctl_set_lufid(): ioctl handler that validates parameters and
calls the underlying implementation
- ext4_dirdata_set_lufid(): Core function that performs the operation by:
* Looking up the target directory entry
* Retrieving the associated inode
* Deleting the old entry and re-creating it with LUFID data attached
The LUFID payload is limited to 48 bytes (three FIDs), so that a
directory block split always has room for the new entry, even with
1k blocks, a 255-byte name and a CFHASH record.
The entry is re-created under the name it is stored with, which in a
casefolded directory may differ in case from the name passed in; in an
encrypted directory that name is decrypted first, so the key is needed.
Both names are set up before the old entry is deleted, so a failed
re-add can put the old entry back without allocating memory. The
directory is locked for the whole operation, its quota is initialized
before the transaction starts, and DIRSYNC is honoured.
This implementation requires the dirdata feature to be enabled on the
filesystem.
Signed-off-by: Artem Blagodarenko <artem.blagodarenko@gmail.com>
Assisted-by: Claude:claude-opus-5-5
---
fs/ext4/ext4.h | 36 +++-
fs/ext4/inline.c | 4 +-
fs/ext4/ioctl.c | 87 +++++++++
fs/ext4/namei.c | 393 +++++++++++++++++++++++++++++++++-----
include/uapi/linux/ext4.h | 14 ++
5 files changed, 480 insertions(+), 54 deletions(-)
diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 0083606e99d0..1eaf22b4b1f0 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -2640,6 +2640,14 @@ struct ext4_dirent_fid {
};
#define EXT4_LUFID_MAGIC 0xAD200907UL
+
+/*
+ * Largest LUFID payload accepted from EXT4_IOC_SET_LUFID. do_split() needs
+ * every entry to fit in the free part of either half of a split block; with
+ * a 255-byte name, the NUL byte, a CFHASH record and 1k blocks that limits a
+ * LUFID record to about 57 bytes. Three FIDs leave some headroom.
+ */
+#define EXT4_LUFID_MAX_DATA_LEN (3 * sizeof(struct ext4_fid))
struct ext4_dentry_param {
__u32 edp_magic; /* EXT4_LUFID_MAGIC */
__u8 edp_dfid[]; /* struct ext4_dirent_fid payload */
@@ -2695,6 +2703,11 @@ static inline unsigned int ext4_dirent_rec_len(unsigned int name_len,
{
unsigned int rec_len = (name_len + 8 + EXT4_DIR_ROUND);
+ /*
+ * Reserve the casefold+fscrypt hash at its fixed position after the
+ * name. Callers whose length already includes the hash as a CFHASH
+ * dirdata record pass a NULL @dir (see ext4_dir_entry_len()).
+ */
if (dir && ext4_hash_in_dirent(dir))
rec_len += sizeof(struct ext4_dir_entry_hash);
return (rec_len & ~EXT4_DIR_ROUND);
@@ -3112,17 +3125,17 @@ extern int ext4_find_dest_de(struct inode *dir, struct buffer_head *bh,
struct ext4_filename *fname,
struct ext4_dir_entry_2 **dest_de,
int dlen);
-void ext4_insert_dentry_data(struct inode *dir, struct inode *inode,
- struct ext4_dir_entry_2 *de,
- int buf_size,
- struct ext4_filename *fname,
- void *data);
-static inline void ext4_insert_dentry(struct inode *dir, struct inode *inode,
- struct ext4_dir_entry_2 *de,
- int buf_size,
- struct ext4_filename *fname)
+int ext4_insert_dentry_data(struct inode *dir, struct inode *inode,
+ struct ext4_dir_entry_2 *de,
+ int buf_size,
+ struct ext4_filename *fname,
+ void *data);
+static inline int ext4_insert_dentry(struct inode *dir, struct inode *inode,
+ struct ext4_dir_entry_2 *de,
+ int buf_size,
+ struct ext4_filename *fname)
{
- ext4_insert_dentry_data(dir, inode, de, buf_size, fname, NULL);
+ return ext4_insert_dentry_data(dir, inode, de, buf_size, fname, NULL);
}
static inline void ext4_update_dx_flag(struct inode *inode)
{
@@ -3527,6 +3540,9 @@ static inline int ext4_init_new_dir(handle_t *handle, struct inode *dir,
}
extern int ext4_dirblock_csum_verify(struct inode *inode,
struct buffer_head *bh);
+extern int ext4_dirdata_set_lufid(const struct mnt_idmap *idmap, struct inode *dir,
+ const char *filename, int namelen,
+ struct ext4_dentry_param *edp);
extern int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
__u32 start_minor_hash, __u32 *next_hash);
extern int ext4_search_dir(struct buffer_head *bh,
diff --git a/fs/ext4/inline.c b/fs/ext4/inline.c
index 13309768528d..5028c81ffdf6 100644
--- a/fs/ext4/inline.c
+++ b/fs/ext4/inline.c
@@ -1015,7 +1015,9 @@ static int ext4_add_dirent_to_inline(handle_t *handle,
EXT4_JTR_NONE);
if (err)
return err;
- ext4_insert_dentry_data(dir, inode, de, inline_size, fname, dfid);
+ err = ext4_insert_dentry_data(dir, inode, de, inline_size, fname, dfid);
+ if (err)
+ return err;
ext4_show_inline_dir(dir, iloc->bh, inline_start, inline_size);
diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c
index 83630cd40712..a6baba5968f4 100644
--- a/fs/ext4/ioctl.c
+++ b/fs/ext4/ioctl.c
@@ -1563,6 +1563,90 @@ static int ext4_ioctl_set_tune_sb(struct file *filp,
return ret;
}
+/*
+ * ext4_ioctl_set_lufid() - Set LUFID on a directory entry
+ * @filp: file pointer (parent directory)
+ * @arg: pointer to ext4_set_lufid structure with filename and LUFID data
+ *
+ * This ioctl allows setting LUFID data on an existing
+ * directory entry. It is called on the parent directory with a filename and
+ * LUFID data.
+ */
+static long ext4_ioctl_set_lufid(struct file *filp, unsigned long arg)
+{
+ struct inode *dir = file_inode(filp);
+ const struct mnt_idmap *idmap = file_mnt_idmap(filp);
+ struct ext4_set_lufid lufid_args;
+ struct {
+ __u32 edp_magic;
+ struct ext4_dirent_data_header df_header;
+ char df_fid[EXT4_LUFID_MAX_DATA_LEN];
+ } edp;
+ int err;
+
+ /* Check if parent is a directory */
+ if (!S_ISDIR(dir->i_mode))
+ return -ENOTDIR;
+
+ /* This ioctl mutates directory entries; merely having the directory
+ * open (which only ever requires read access) is not enough.
+ * MAY_EXEC is required for entry lookup; MAY_WRITE for modification. */
+ err = inode_permission(idmap, dir, MAY_WRITE | MAY_EXEC);
+ if (err)
+ return err;
+
+ /* Copy arguments from user space */
+ if (copy_from_user(&lufid_args, (struct ext4_set_lufid __user *)arg,
+ sizeof(lufid_args)))
+ return -EFAULT;
+
+ /* Validate parameters. esl_name_len is NUL-excluded length (1-255). */
+ if (lufid_args.esl_name_len == 0 || lufid_args.esl_name_len > EXT4_NAME_LEN)
+ return -EINVAL;
+
+ /*
+ * The payload is capped so that a directory block split always has
+ * room for the new entry, see EXT4_LUFID_MAX_DATA_LEN
+ */
+ if (lufid_args.esl_data_len == 0 ||
+ lufid_args.esl_data_len > EXT4_LUFID_MAX_DATA_LEN)
+ return -EINVAL;
+
+ /* Ensure filename is NUL-terminated at exactly esl_name_len */
+ if (lufid_args.esl_name[lufid_args.esl_name_len] != '\0')
+ return -EINVAL;
+
+ /* '.' and '..' are not ordinary entries -- they must stay the first
+ * two entries in the directory's first block, so they can't go
+ * through the general delete+re-add path this ioctl uses */
+ if (!strcmp(lufid_args.esl_name, ".") || !strcmp(lufid_args.esl_name, ".."))
+ return -EINVAL;
+
+ /* Prepare the dentry param struct with LUFID data. ddh_length is
+ * documented (see struct ext4_dirent_data_header) as the length of
+ * the header plus the whole data blob -- include the header here so
+ * every dirdata reader/writer that takes ddh_length at face value
+ * (e.g. ext4_dirdata_set()'s memcpy) copies the full LUFID payload
+ * instead of silently dropping its last byte. */
+ edp.edp_magic = EXT4_LUFID_MAGIC;
+ edp.df_header.ddh_length = lufid_args.esl_data_len +
+ sizeof(edp.df_header);
+ memcpy(edp.df_fid, lufid_args.esl_data, lufid_args.esl_data_len);
+
+ /* Want write access */
+ err = mnt_want_write_file(filp);
+ if (err)
+ return err;
+
+ /* Call the helper function to do the actual work */
+ err = ext4_dirdata_set_lufid(idmap, dir, lufid_args.esl_name,
+ lufid_args.esl_name_len,
+ (struct ext4_dentry_param *)&edp);
+
+ mnt_drop_write_file(filp);
+ return err;
+}
+
static long __ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
struct inode *inode = file_inode(filp);
@@ -1949,6 +2033,8 @@ static long __ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
(void __user *)arg);
case EXT4_IOC_SET_TUNE_SB_PARAM:
return ext4_ioctl_set_tune_sb(filp, (void __user *)arg);
+ case EXT4_IOC_SET_LUFID:
+ return ext4_ioctl_set_lufid(filp, arg);
default:
return -ENOTTY;
}
@@ -2029,6 +2115,7 @@ long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
case FS_IOC_SETFSLABEL:
case EXT4_IOC_GETFSUUID:
case EXT4_IOC_SETFSUUID:
+ case EXT4_IOC_SET_LUFID:
break;
default:
return -ENOIOCTLCMD;
diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index ae58ce5120e1..f9250407cedb 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -1372,9 +1372,9 @@ bool ext4_dirent_hash_get(struct ext4_dir_entry_2 *de, struct inode *dir,
* directories requires an e2fsck migration pass before tune2fs sets the
* EXT4_FEATURE_INCOMPAT_DIRDATA superblock flag.
*/
-static void ext4_dirdata_set(struct ext4_dir_entry_2 *de, struct inode *dir,
- struct ext4_dirent_fid *dfid,
- struct ext4_filename *fname)
+static int ext4_dirdata_set(struct ext4_dir_entry_2 *de, struct inode *dir,
+ struct ext4_dirent_fid *dfid,
+ struct ext4_filename *fname)
{
struct dx_hash_info *hinfo = &fname->hinfo;
unsigned int data_offset = de->name_len + 1;
@@ -1395,7 +1395,7 @@ static void ext4_dirdata_set(struct ext4_dir_entry_2 *de, struct inode *dir,
if (EXT4_BASE_DIR_LEN + data_offset + dlen > rec_len) {
EXT4_ERROR_INODE(dir, "Can not insert FID");
- return;
+ return -EIO;
}
memcpy((char *)de + EXT4_BASE_DIR_LEN + data_offset, dfid, dlen);
@@ -1411,7 +1411,7 @@ static void ext4_dirdata_set(struct ext4_dir_entry_2 *de, struct inode *dir,
if (EXT4_BASE_DIR_LEN + data_offset + sizeof(*dh) > rec_len) {
EXT4_ERROR_INODE(dir, "Can not insert dhash dirdata");
- return;
+ return -EIO;
}
dh->dh_header.ddh_length = sizeof(*dh);
@@ -1419,11 +1419,18 @@ static void ext4_dirdata_set(struct ext4_dir_entry_2 *de, struct inode *dir,
dh->dh_minor_hash = cpu_to_le32(hinfo->minor_hash);
de->file_type |= EXT4_DIRENT_CFHASH;
} else {
- /* Compatibility: store hash inline after filename */
- if (EXT4_BASE_DIR_LEN + data_offset +
- sizeof(struct ext4_dir_entry_hash) > rec_len) {
+ /* Compatibility: store hash inline after filename.
+ * EXT4_DIRENT_HASHES places the hash at the
+ * 4-byte-aligned offset (8 + name_len + 3) & ~3, which
+ * may be less than EXT4_BASE_DIR_LEN + data_offset when
+ * name_len is a multiple of 4 (gap byte counted twice).
+ * Use the same aligned offset for the bounds check. */
+ unsigned int hash_off =
+ (EXT4_BASE_DIR_LEN + de->name_len + 3) & ~3;
+
+ if (hash_off + sizeof(struct ext4_dir_entry_hash) > rec_len) {
EXT4_ERROR_INODE(dir, "Can not insert dhash");
- return;
+ return -EIO;
}
EXT4_DIRENT_HASHES(de)->hash = cpu_to_le32(hinfo->hash);
@@ -1431,6 +1438,7 @@ static void ext4_dirdata_set(struct ext4_dir_entry_2 *de, struct inode *dir,
cpu_to_le32(hinfo->minor_hash);
}
}
+ return 0;
}
/*
@@ -2243,9 +2251,9 @@ int ext4_find_dest_de(struct inode *dir, struct buffer_head *bh,
}
EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_find_dest_de);
-void ext4_insert_dentry_data(struct inode *dir, struct inode *inode,
- struct ext4_dir_entry_2 *de, int buf_size,
- struct ext4_filename *fname, void *data)
+int ext4_insert_dentry_data(struct inode *dir, struct inode *inode,
+ struct ext4_dir_entry_2 *de, int buf_size,
+ struct ext4_filename *fname, void *data)
{
int nlen, rlen;
@@ -2263,7 +2271,7 @@ void ext4_insert_dentry_data(struct inode *dir, struct inode *inode,
ext4_set_de_type(inode->i_sb, de, inode->i_mode);
de->name_len = fname_len(fname);
memcpy(de->name, fname_name(fname), fname_len(fname));
- ext4_dirdata_set(de, dir, data, fname);
+ return ext4_dirdata_set(de, dir, data, fname);
}
EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_insert_dentry_data);
@@ -2328,7 +2336,11 @@ static int add_dirent_to_buf(handle_t *handle, struct ext4_filename *fname,
}
/* By now the buffer is marked for journaling */
- ext4_insert_dentry_data(dir, inode, de, blocksize, fname, dfid);
+ err = ext4_insert_dentry_data(dir, inode, de, blocksize, fname, dfid);
+ if (err) {
+ ext4_std_error(dir->i_sb, err);
+ return err;
+ }
/*
* XXX shouldn't update any times until successful
@@ -2568,15 +2580,17 @@ static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname,
* may not sleep between calling this and putting something into
* the entry, as someone else might have used it while you slept.
*/
-static int __ext4_add_entry(handle_t *handle, struct inode *dir,
- const struct qstr *d_name,
- struct inode *inode,
- struct ext4_dentry_param *edp)
+/*
+ * ext4_add_fname() - add an entry for a name that is already set up
+ *
+ * @fname->dfid is the LUFID record to store with the entry, or NULL.
+ */
+static int ext4_add_fname(handle_t *handle, struct inode *dir,
+ struct ext4_filename *fname, struct inode *inode)
{
struct buffer_head *bh = NULL;
struct ext4_dir_entry_2 *de;
struct super_block *sb;
- struct ext4_filename fname;
int retval;
int dx_fallback=0;
unsigned blocksize;
@@ -2589,22 +2603,11 @@ static int __ext4_add_entry(handle_t *handle, struct inode *dir,
sb = dir->i_sb;
blocksize = sb->s_blocksize;
- if (!generic_ci_validate_strict_name(dir, d_name))
- return -EINVAL;
-
- retval = ext4_fname_setup_filename(dir, d_name, 0, &fname);
- if (retval)
- return retval;
-
- /* Resolve the LUFID payload once here and store it in fname so every
- * add_dirent_to_buf() call in this add operation uses the same value
- * without touching the shared EXT4_I(inode)->i_dirdata field. */
- fname.dfid = ext4_dentry_get_fid(sb, edp);
- if (fname.dfid)
+ if (fname->dfid)
ext4_fc_mark_ineligible(sb, EXT4_FC_REASON_DIRDATA, handle);
if (ext4_has_inline_data(dir)) {
- retval = ext4_try_add_inline_entry(handle, &fname, dir, inode);
+ retval = ext4_try_add_inline_entry(handle, fname, dir, inode);
if (retval < 0)
goto out;
if (retval == 1) {
@@ -2614,7 +2617,7 @@ static int __ext4_add_entry(handle_t *handle, struct inode *dir,
}
if (is_dx(dir)) {
- retval = ext4_dx_add_entry(handle, &fname, dir, inode);
+ retval = ext4_dx_add_entry(handle, fname, dir, inode);
if (!retval || (retval != ERR_BAD_DX_DIR))
goto out;
/* Can we just ignore htree data? */
@@ -2643,14 +2646,14 @@ static int __ext4_add_entry(handle_t *handle, struct inode *dir,
bh = NULL;
goto out;
}
- retval = add_dirent_to_buf(handle, &fname, dir, inode,
+ retval = add_dirent_to_buf(handle, fname, dir, inode,
NULL, bh);
if (retval != -ENOSPC)
goto out;
if (blocks == 1 && !dx_fallback &&
ext4_has_feature_dir_index(sb)) {
- retval = make_indexed_dir(handle, &fname, dir,
+ retval = make_indexed_dir(handle, fname, dir,
inode, bh);
bh = NULL; /* make_indexed_dir releases bh */
goto out;
@@ -2671,15 +2674,39 @@ static int __ext4_add_entry(handle_t *handle, struct inode *dir,
if (csum_size)
ext4_initialize_dirent_tail(bh, blocksize);
- retval = add_dirent_to_buf(handle, &fname, dir, inode, de, bh);
+ retval = add_dirent_to_buf(handle, fname, dir, inode, de, bh);
out:
- ext4_fname_free_filename(&fname);
brelse(bh);
if (retval == 0)
ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
return retval;
}
+static int __ext4_add_entry(handle_t *handle, struct inode *dir,
+ const struct qstr *d_name,
+ struct inode *inode,
+ struct ext4_dentry_param *edp)
+{
+ struct ext4_filename fname;
+ int retval;
+
+ if (!generic_ci_validate_strict_name(dir, d_name))
+ return -EINVAL;
+
+ retval = ext4_fname_setup_filename(dir, d_name, 0, &fname);
+ if (retval)
+ return retval;
+
+ /* Resolve the LUFID payload once here and store it in fname so every
+ * add_dirent_to_buf() call in this add operation uses the same value
+ * without touching the shared EXT4_I(inode)->i_dirdata field.
+ */
+ fname.dfid = ext4_dentry_get_fid(dir->i_sb, edp);
+ retval = ext4_add_fname(handle, dir, &fname, inode);
+ ext4_fname_free_filename(&fname);
+ return retval;
+}
+
static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
struct inode *inode)
{
@@ -3158,14 +3185,15 @@ int ext4_init_dirblock(handle_t *handle, struct inode *inode,
struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *) bh->b_data;
size_t blocksize = bh->b_size;
int csum_size = 0, header_size;
+ unsigned int dot_rec_len;
if (ext4_has_feature_metadata_csum(inode->i_sb))
csum_size = sizeof(struct ext4_dir_entry_tail);
de->inode = cpu_to_le32(inode->i_ino);
de->name_len = 1;
- de->rec_len = ext4_rec_len_to_disk(ext4_dirent_rec_len(de->name_len, NULL),
- blocksize);
+ dot_rec_len = ext4_dirent_rec_len(de->name_len, NULL);
+ de->rec_len = ext4_rec_len_to_disk(dot_rec_len, blocksize);
memcpy(de->name, ".", 2);
ext4_set_de_type(inode->i_sb, de, S_IFDIR);
@@ -3185,7 +3213,7 @@ int ext4_init_dirblock(handle_t *handle, struct inode *inode,
blocksize - csum_size);
} else {
de->rec_len = ext4_rec_len_to_disk(blocksize -
- (csum_size + ext4_dirent_rec_len(1, NULL)),
+ (csum_size + dot_rec_len),
blocksize);
}
@@ -3219,6 +3247,12 @@ int ext4_init_new_dir_data(handle_t *handle, struct inode *dir,
dir_block = ext4_append(handle, inode, &block);
if (IS_ERR(dir_block))
return PTR_ERR(dir_block);
+ /*
+ * data1 and data2 are reserved for callers that need to embed
+ * dirdata into the '.' and '..' entries of a new directory.
+ * That path is not yet implemented here; a caller requiring it
+ * must write the dirdata entries after this function returns.
+ */
err = ext4_init_dirblock(handle, inode, dir_block, dir->i_ino, NULL, 0);
out:
brelse(dir_block);
@@ -4343,14 +4377,13 @@ static int ext4_rename(const struct mnt_idmap *idmap, struct inode *old_dir,
if (!new.bh && old_snap.fid) {
unsigned fid_size = old_snap.fid->df_header.ddh_length;
- edp = kmalloc(offsetof(struct ext4_dentry_param,
- edp_dfid) + fid_size, GFP_NOFS);
+ edp = kmalloc(sizeof(*edp) + fid_size, GFP_NOFS);
if (!edp) {
retval = -ENOMEM;
goto end_rename;
}
edp->edp_magic = EXT4_LUFID_MAGIC;
- memcpy(&edp->edp_dfid, old_snap.fid, fid_size);
+ memcpy(edp->edp_dfid, old_snap.fid, fid_size);
}
if (whiteout) {
@@ -4685,6 +4718,280 @@ static int ext4_rename2(const struct mnt_idmap *idmap,
return ext4_rename(idmap, old_dir, old_dentry, new_dir, new_dentry, flags);
}
+/*
+ * ext4_dirdata_set_lufid() - Set LUFID data on an existing directory entry
+ * @dir: parent directory inode
+ * @filename: name of the file in the directory
+ * @namelen: length of filename
+ * @edp: pointer to initialized dentry param with LUFID data
+ *
+ * This function finds an existing directory entry, deletes it, and re-creates it
+ * with LUFID data attached. Used by the EXT4_IOC_SET_LUFID ioctl.
+ *
+ * Returns 0 on success, negative error code on failure.
+ */
+int ext4_dirdata_set_lufid(const struct mnt_idmap *idmap, struct inode *dir,
+ const char *filename, int namelen,
+ struct ext4_dentry_param *edp)
+{
+ struct super_block *sb = dir->i_sb;
+ /* zero-init: safe to free on any path */
+ struct ext4_filename new_fname = {}, old_fname = {};
+ struct ext4_dir_entry_2 *de = NULL;
+ struct buffer_head *bh = NULL;
+ struct inode *inode = NULL;
+ handle_t *handle = NULL;
+ struct ext4_lufid_snap old_snap = {};
+ struct qstr d_name = QSTR_INIT(filename, namelen);
+ /* the name as stored on disk, for casefolded directories */
+ struct fscrypt_str stored = FSTR_INIT(NULL, 0);
+ struct qstr stored_name;
+ const struct qstr *add_name = &d_name;
+ __u32 ino;
+ int inlined = 0;
+ int err, err2;
+
+ if (!ext4_has_feature_dirdata(sb))
+ return -EOPNOTSUPP;
+
+ if (namelen > EXT4_NAME_LEN)
+ return -ENAMETOOLONG;
+ if (namelen != strnlen(filename, namelen + 1))
+ return -EINVAL;
+
+ /* the entry is re-created from its plaintext name */
+ if (IS_ENCRYPTED(dir)) {
+ err = fscrypt_prepare_readdir(dir);
+ if (err)
+ return err;
+ if (!fscrypt_has_encryption_key(dir))
+ return -ENOKEY;
+ }
+
+ err = dquot_initialize(dir);
+ if (err)
+ return err;
+
+ /*
+ * The lookup and the change must both be under the directory lock.
+ * The entry is deleted and re-added in the same directory, so the
+ * target inode itself is not changed and is not locked.
+ */
+ inode_lock_nested(dir, I_MUTEX_PARENT);
+
+ /*
+ * Look up the entry first to get the inode for the permission
+ * checks, without holding a journal handle.
+ */
+ bh = ext4_find_entry(dir, &d_name, &de, NULL);
+ if (IS_ERR(bh)) {
+ err = PTR_ERR(bh);
+ bh = NULL;
+ goto out_unlock_dir;
+ }
+ if (!bh) {
+ err = -ENOENT;
+ goto out_unlock_dir;
+ }
+ ino = le32_to_cpu(de->inode);
+ brelse(bh);
+ bh = NULL;
+ de = NULL;
+
+ /* the same checks as ext4_lookup() */
+ if (!ext4_valid_inum(sb, ino)) {
+ EXT4_ERROR_INODE(dir, "bad inode number: %u", ino);
+ err = -EFSCORRUPTED;
+ goto out_unlock_dir;
+ }
+ if (unlikely(ino == dir->i_ino)) {
+ EXT4_ERROR_INODE(dir, "'%.*s' linked to parent dir",
+ namelen, filename);
+ err = -EFSCORRUPTED;
+ goto out_unlock_dir;
+ }
+ inode = ext4_iget(sb, ino, EXT4_IGET_NORMAL);
+ if (IS_ERR(inode)) {
+ err = PTR_ERR(inode);
+ inode = NULL;
+ if (err == -ESTALE) {
+ EXT4_ERROR_INODE(dir, "deleted inode referenced: %u",
+ ino);
+ err = -EFSCORRUPTED;
+ }
+ goto out_unlock_dir;
+ }
+ if (IS_ENCRYPTED(dir) &&
+ (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
+ !fscrypt_has_permitted_context(dir, inode)) {
+ ext4_warning(sb, "Inconsistent encryption contexts: %llu/%llu",
+ dir->i_ino, inode->i_ino);
+ err = -EPERM;
+ goto out_iput;
+ }
+
+ /* Enforce sticky-bit restriction: on a sticky directory only the
+ * directory owner, the file owner, or a privileged process may
+ * modify entries. inode_permission(MAY_WRITE) alone does not cover
+ * this, so check it explicitly here as may_delete() does. */
+ if (check_sticky(idmap, dir, inode)) {
+ err = -EPERM;
+ goto out_iput;
+ }
+
+ /* The ioctl calls ext4_delete_entry() on the parent directory, which
+ * bypasses the VFS may_delete() path. inode_permission(MAY_WRITE)
+ * above blocks IS_IMMUTABLE(dir) but does NOT block IS_APPEND(dir):
+ * append-only directories allow new entries but forbid deletion, and
+ * that restriction lives only in may_delete(), not in
+ * inode_permission(). Enforce it explicitly here. */
+ if (IS_APPEND(dir)) {
+ err = -EPERM;
+ goto out_iput;
+ }
+
+ /* The ioctl deletes and re-creates the directory entry, which
+ * effectively modifies the target file's namespace presence.
+ * Refuse if the target itself is immutable or append-only. */
+ if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) {
+ err = -EPERM;
+ goto out_iput;
+ }
+
+ handle = ext4_journal_start(dir, EXT4_HT_DIR,
+ 3 * EXT4_DATA_TRANS_BLOCKS(sb) +
+ 2 * EXT4_INDEX_EXTRA_TRANS_BLOCKS);
+ if (IS_ERR(handle)) {
+ err = PTR_ERR(handle);
+ handle = NULL;
+ goto out_iput;
+ }
+ if (IS_DIRSYNC(dir))
+ ext4_handle_sync(handle);
+
+ /*
+ * Look up the entry again inside the handle and check that it still
+ * refers to the same inode (an unlink and create could have run in
+ * between).
+ */
+ bh = ext4_find_entry(dir, &d_name, &de, &inlined);
+ if (IS_ERR(bh)) {
+ err = PTR_ERR(bh);
+ bh = NULL;
+ goto out_journal;
+ }
+ if (!bh) {
+ err = -ENOENT;
+ goto out_journal;
+ }
+ if (le32_to_cpu(de->inode) != ino) {
+ err = -ENOENT;
+ goto out_brelse;
+ }
+
+ /*
+ * In a casefolded directory the lookup ignores case, so the caller's
+ * name may differ from the stored one. Re-add the stored name, or
+ * the entry would be renamed. In an encrypted directory the stored
+ * name is ciphertext, so decrypt it. Elsewhere the names match.
+ */
+ if (IS_CASEFOLDED(dir)) {
+ if (IS_ENCRYPTED(dir)) {
+ struct fscrypt_str disk = FSTR_INIT(de->name,
+ de->name_len);
+
+ err = fscrypt_fname_alloc_buffer(EXT4_NAME_LEN, &stored);
+ if (err)
+ goto out_brelse;
+ err = fscrypt_fname_disk_to_usr(dir, 0, 0, &disk,
+ &stored);
+ if (err)
+ goto out_brelse;
+ } else {
+ stored.name = kmemdup(de->name, de->name_len, GFP_NOFS);
+ if (!stored.name) {
+ err = -ENOMEM;
+ goto out_brelse;
+ }
+ stored.len = de->name_len;
+ }
+ stored_name = (struct qstr)QSTR_INIT(stored.name, stored.len);
+ add_name = &stored_name;
+ }
+
+ /* The add checks this too, but only after the entry is gone */
+ if (!generic_ci_validate_strict_name(dir, add_name)) {
+ err = -EINVAL;
+ goto out_brelse;
+ }
+
+ /* Copy the old LUFID so a failed re-add can restore it */
+ err = ext4_lufid_snapshot(de, sb->s_blocksize, &old_snap);
+ if (err)
+ goto out_brelse;
+
+ /*
+ * Set up both names before the entry is deleted, so neither the
+ * re-add nor the rollback has to allocate memory afterwards.
+ */
+ err = ext4_fname_setup_filename(dir, add_name, 0, &new_fname);
+ if (err)
+ goto out_brelse;
+ new_fname.dfid = ext4_dentry_get_fid(sb, edp);
+ err = ext4_fname_setup_filename(dir, add_name, 0, &old_fname);
+ if (err)
+ goto out_brelse;
+ old_fname.dfid = old_snap.fid;
+
+ err = ext4_delete_entry(handle, dir, de, bh);
+ if (err)
+ goto out_brelse;
+
+ err = ext4_add_fname(handle, dir, &new_fname, inode);
+ if (err) {
+ /*
+ * Put the old entry back into the space it was deleted from.
+ * A new lookup by name could pick another block of the same
+ * hash, which may be full. An inline directory has only the
+ * one buffer, and the space is there.
+ */
+ if (inlined)
+ err2 = ext4_add_fname(handle, dir, &old_fname, inode);
+ else
+ err2 = add_dirent_to_buf(handle, &old_fname, dir,
+ inode, NULL, bh);
+ if (err2)
+ EXT4_ERROR_INODE(dir,
+ "Failed to set LUFID on '%.*s' (err=%d) and failed to restore the original directory entry (err=%d); inode %llu may be orphaned",
+ namelen, filename, err, err2,
+ (unsigned long long)inode->i_ino);
+ goto out_brelse;
+ }
+
+ inode_set_ctime_current(dir);
+ inode_inc_iversion(dir);
+ ext4_mark_inode_dirty(handle, dir);
+
+out_brelse:
+ brelse(bh);
+out_journal:
+ ext4_fname_free_filename(&new_fname);
+ ext4_fname_free_filename(&old_fname);
+ kfree(stored.name);
+ ext4_lufid_snap_release(&old_snap);
+ if (handle) {
+ err2 = ext4_journal_stop(handle);
+ if (!err)
+ err = err2;
+ }
+out_iput:
+ iput(inode);
+out_unlock_dir:
+ inode_unlock(dir);
+
+ return err;
+}
+
/*
* directories can handle most operations...
*/
diff --git a/include/uapi/linux/ext4.h b/include/uapi/linux/ext4.h
index 9c683991c32f..5d5c8c3df2a4 100644
--- a/include/uapi/linux/ext4.h
+++ b/include/uapi/linux/ext4.h
@@ -35,6 +35,7 @@
#define EXT4_IOC_SETFSUUID _IOW('f', 44, struct fsuuid)
#define EXT4_IOC_GET_TUNE_SB_PARAM _IOR('f', 45, struct ext4_tune_sb_params)
#define EXT4_IOC_SET_TUNE_SB_PARAM _IOW('f', 46, struct ext4_tune_sb_params)
+#define EXT4_IOC_SET_LUFID _IOW('f', 47, struct ext4_set_lufid)
#define EXT4_IOC_SHUTDOWN _IOR('X', 125, __u32)
@@ -92,6 +93,19 @@ struct move_extent {
__u64 moved_len; /* moved block length */
};
+/*
+ * Structure for EXT4_IOC_SET_LUFID
+ * Sets LUFID on a directory entry
+ * Called on parent directory with filename and LUFID data as arguments
+ */
+struct ext4_set_lufid {
+ __u8 esl_name_len; /* length of filename, NOT including NUL terminator
+ * (valid range: 1-255, matching EXT4_NAME_LEN) */
+ char esl_name[255 + 1]; /* filename (NUL-terminated) */
+ __u8 esl_data_len; /* length of LUFID data (1-48) */
+ char esl_data[255]; /* LUFID data (raw bytes) */
+};
+
/*
* Flags used by EXT4_IOC_SHUTDOWN
*/
--
2.52.0
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v15 13/13] ext4: add KUnit tests for dirdata
2026-12-03 18:49 [PATCH v15 00/13] Data in direntry (dirdata) feature, with KUnit tests Artem Blagodarenko
` (11 preceding siblings ...)
2026-12-03 18:49 ` [PATCH v15 12/13] ext4: Add EXT4_IOC_SET_LUFID ioctl for setting LUFID on directory entries Artem Blagodarenko
@ 2026-12-03 18:49 ` Artem Blagodarenko
2026-12-03 23:15 ` sashiko-bot
2026-12-03 23:15 ` [PATCH v15 00/13] Data in direntry (dirdata) feature, with KUnit tests sashiko-bot
13 siblings, 1 reply; 26+ messages in thread
From: Artem Blagodarenko @ 2026-12-03 18:49 UTC (permalink / raw)
To: linux-ext4; +Cc: tytso, jack, adilger.kernel, ablagodarenko, Artem Blagodarenko
Add an ext4_dirdata_test suite next to ext4_namei_test in
fs/ext4/namei-test.c. It uses the same mock superblock, inodes and
directory blocks, and covers:
- record lengths: ext4_dirent_get_data_len(), ext4_dir_entry_len(),
ext4_dirent_data_corrupt() for records that run past rec_len or
have length 0, and get_dtype() with the dirdata flags set;
- __ext4_check_dir_entry() on entries whose rec_len does not cover
their records;
- dx_get_dx_info() with dirdata on '.' and '..', malformed '..'
records, and dx_root_limit() for the resulting offsets;
- adding entries with and without a LUFID, and without the feature;
- converting a directory whose '..' carries a LUFID into an htree and
splitting leaves whose entries all carry LUFIDs, then finding every
entry and its LUFID again and reading the directory back;
- rename within and across directories and RENAME_EXCHANGE, which
keep each LUFID with its inode;
- ext4_dirdata_set_lufid(), behind EXT4_IOC_SET_LUFID;
- the casefold+fscrypt hash from a CFHASH record, from the fixed
place used before dirdata, and from entries too short to hold it;
- the two htree index node limits accepted for casefolded and
encrypted directories.
dx_get_dx_info() gets a test wrapper, and ext4_dirdata_get(),
ext4_dirent_hash_get() and ext4_dirdata_set_lufid() are exported to
the test module.
Run with:
./tools/testing/kunit/kunit.py run --kunitconfig=fs/ext4
Signed-off-by: Artem Blagodarenko <artem.blagodarenko@gmail.com>
Assisted-by: Claude:claude-opus-5-5
---
fs/ext4/namei-test.c | 754 ++++++++++++++++++++++++++++++++++++++++++-
fs/ext4/namei.c | 9 +
fs/ext4/namei.h | 1 +
3 files changed, 763 insertions(+), 1 deletion(-)
diff --git a/fs/ext4/namei-test.c b/fs/ext4/namei-test.c
index b45f5ad3b7af..ba1bb52d2442 100644
--- a/fs/ext4/namei-test.c
+++ b/fs/ext4/namei-test.c
@@ -2315,6 +2315,737 @@ static void test_namei_casefold_lookup_and_strict(struct kunit *test)
}
#endif /* CONFIG_UNICODE */
+/*
+ * Dirdata tests.
+ *
+ * With the dirdata feature the high four bits of file_type flag records
+ * stored after the name and its NUL byte: a LUFID (a 128-bit file
+ * identifier, used by Lustre), a reserved INO64 record and CFHASH, the
+ * casefold+fscrypt hash. Each record starts with its own length byte.
+ */
+
+#define TEST_LUFID_LEN (sizeof(struct ext4_dirent_data_header) + \
+ sizeof(struct ext4_fid))
+#define TEST_CFHASH_LEN sizeof(struct ext4_dirent_hash)
+
+static void dirdata_enable(struct namei_test_ctx *ctx)
+{
+ ctx->es.s_feature_incompat |=
+ cpu_to_le32(EXT4_FEATURE_INCOMPAT_DIRDATA);
+}
+
+static void dirdata_fill_fid(u8 *rec, u8 seed)
+{
+ int i;
+
+ rec[0] = TEST_LUFID_LEN;
+ for (i = 0; i < sizeof(struct ext4_fid); i++)
+ rec[1 + i] = seed + i;
+}
+
+/*
+ * Write a directory entry at @buf: @name, its NUL byte, then a LUFID
+ * record made from @fid_seed (if @lufid) and a CFHASH record (if
+ * @cfhash). Returns the number of bytes used, before rounding.
+ */
+static unsigned int dirdata_make_de(char *buf, u32 ino, const char *name,
+ u8 ft, bool lufid, u8 fid_seed,
+ bool cfhash, u32 hash, u32 minor_hash,
+ unsigned int rec_len)
+{
+ struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)buf;
+ unsigned int len = strlen(name);
+ unsigned int off = EXT4_BASE_DIR_LEN + len + 1;
+
+ memset(buf, 0, rec_len);
+ de->inode = cpu_to_le32(ino);
+ de->rec_len = ext4_rec_len_to_disk(rec_len, TEST_BLK_SIZE);
+ de->name_len = len;
+ de->file_type = ft;
+ memcpy(de->name, name, len);
+ if (lufid) {
+ dirdata_fill_fid((u8 *)buf + off, fid_seed);
+ de->file_type |= EXT4_DIRENT_LUFID;
+ off += TEST_LUFID_LEN;
+ }
+ if (cfhash) {
+ struct ext4_dirent_hash *dh = (void *)(buf + off);
+
+ dh->dh_header.ddh_length = TEST_CFHASH_LEN;
+ dh->dh_hash = cpu_to_le32(hash);
+ dh->dh_minor_hash = cpu_to_le32(minor_hash);
+ de->file_type |= EXT4_DIRENT_CFHASH;
+ off += TEST_CFHASH_LEN;
+ }
+ return (lufid || cfhash) ? off : EXT4_BASE_DIR_LEN + len;
+}
+
+/* Return true if @de carries the LUFID made from @seed */
+static bool dirdata_has_fid(struct inode *dir, struct ext4_dir_entry_2 *de,
+ u8 seed)
+{
+ u8 want[TEST_LUFID_LEN];
+ union {
+ struct ext4_dirent_fid fid;
+ u8 raw[TEST_LUFID_LEN + EXT4_LUFID_MAX_DATA_LEN];
+ } got = {};
+
+ if (!(ext4_dirdata_get(de, dir, TEST_BLK_SIZE, &got.fid, NULL) &
+ EXT4_DIRENT_LUFID))
+ return false;
+ dirdata_fill_fid(want, seed);
+ return !memcmp(got.raw, want, TEST_LUFID_LEN);
+}
+
+static struct ext4_dentry_param *dirdata_make_edp(struct kunit *test, u8 seed)
+{
+ struct ext4_dentry_param *edp;
+
+ edp = kunit_kzalloc(test, sizeof(*edp) + TEST_LUFID_LEN, GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, edp);
+ edp->edp_magic = EXT4_LUFID_MAGIC;
+ dirdata_fill_fid(edp->edp_dfid, seed);
+ return edp;
+}
+
+/* Find @name in @dir; the caller releases the returned buffer */
+static struct buffer_head *dirdata_find(struct kunit *test, struct inode *dir,
+ const char *name,
+ struct ext4_dir_entry_2 **de)
+{
+ struct qstr q = QSTR_INIT(name, strlen(name));
+ struct buffer_head *bh;
+
+ *de = NULL;
+ bh = ext4_find_entry_test(dir, &q, de, NULL);
+ KUNIT_ASSERT_FALSE_MSG(test, IS_ERR_OR_NULL(bh), "missing %s", name);
+ KUNIT_ASSERT_NOT_NULL(test, *de);
+ return bh;
+}
+
+/*
+ * Rewrite '..' of a freshly initialized directory block so that it carries
+ * a LUFID, as Lustre does. '..' keeps the rest of the block.
+ */
+static void dirdata_set_dotdot_fid(struct kunit *test, struct inode *dir,
+ struct buffer_head *bh, u8 seed)
+{
+ struct ext4_dir_entry_2 *dot = (struct ext4_dir_entry_2 *)bh->b_data;
+ struct ext4_dir_entry_2 *dotdot;
+ unsigned int rlen;
+ char *rec;
+
+ dotdot = ext4_next_entry(dot, TEST_BLK_SIZE);
+ KUNIT_ASSERT_EQ(test, dotdot->name_len, 2);
+ rlen = ext4_rec_len_from_disk(dotdot->rec_len, TEST_BLK_SIZE);
+ KUNIT_ASSERT_GE(test, rlen, 64U);
+ rec = (char *)dotdot + EXT4_BASE_DIR_LEN + 2;
+ rec[0] = 0;
+ dirdata_fill_fid((u8 *)rec + 1, seed);
+ dotdot->file_type |= EXT4_DIRENT_LUFID;
+ KUNIT_ASSERT_EQ(test, ext4_handle_dirty_dirblock(NULL, dir, bh), 0);
+}
+
+/*
+ * Dirdata 1: record lengths (ext4_dirent_get_data_len, ext4_dir_entry_len),
+ * records that do not fit (ext4_dirent_data_corrupt) and the file type
+ * reported to readdir (get_dtype).
+ */
+static void test_dirdata_record_lengths(struct kunit *test)
+{
+ struct namei_test_ctx *ctx = test->priv;
+ struct inode *dir = get_mock_inode(ctx, TEST_DIR_INO);
+ struct ext4_dir_entry_2 *de;
+ struct buffer_head *bh;
+ char *buf;
+
+ buf = kunit_kzalloc(test, TEST_BLK_SIZE, GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, buf);
+ de = (struct ext4_dir_entry_2 *)buf;
+ dirdata_enable(ctx);
+
+ /* No dirdata: 8 + 3 rounded up */
+ dirdata_make_de(buf, TEST_FILE1_INO, "abc", EXT4_FT_REG_FILE,
+ false, 0, false, 0, 0, 64);
+ KUNIT_EXPECT_EQ(test, ext4_dirent_get_data_len(de, 64), 0);
+ KUNIT_EXPECT_EQ(test, ext4_dir_entry_len(de, TEST_BLK_SIZE, NULL), 12U);
+ KUNIT_EXPECT_FALSE(test, ext4_dirent_data_corrupt(&ctx->sb, de, 64));
+
+ /* LUFID: the NUL byte and the 17-byte record */
+ dirdata_make_de(buf, TEST_FILE1_INO, "abc", EXT4_FT_REG_FILE,
+ true, 1, false, 0, 0, 64);
+ KUNIT_EXPECT_EQ(test, ext4_dirent_get_data_len(de, 64),
+ (int)(1 + TEST_LUFID_LEN));
+ KUNIT_EXPECT_EQ(test, ext4_dir_entry_len(de, TEST_BLK_SIZE, NULL),
+ round_up(EXT4_BASE_DIR_LEN + 3 + 1 + TEST_LUFID_LEN,
+ 4));
+ KUNIT_EXPECT_FALSE(test, ext4_dirent_data_corrupt(&ctx->sb, de, 64));
+
+ /* LUFID and CFHASH: the NUL byte is counted once */
+ dirdata_make_de(buf, TEST_FILE1_INO, "abc", EXT4_FT_REG_FILE,
+ true, 1, true, 0x1234, 0x5678, 64);
+ KUNIT_EXPECT_EQ(test, ext4_dirent_get_data_len(de, 64),
+ (int)(1 + TEST_LUFID_LEN + TEST_CFHASH_LEN));
+ KUNIT_EXPECT_EQ(test, ext4_dir_entry_len(de, TEST_BLK_SIZE, NULL), 40U);
+
+ /* The flags do not change the type readdir reports */
+ KUNIT_EXPECT_EQ(test, get_dtype(&ctx->sb, de->file_type),
+ (unsigned char)DT_REG);
+
+ /* A LUFID record that runs past rec_len is corruption */
+ dirdata_make_de(buf, TEST_FILE1_INO, "abc", EXT4_FT_REG_FILE,
+ true, 1, false, 0, 0, 24);
+ KUNIT_EXPECT_EQ(test, ext4_dirent_get_data_len(de, 24), 0);
+ KUNIT_EXPECT_TRUE(test, ext4_dirent_data_corrupt(&ctx->sb, de, 24));
+
+ /* So is a record of length 0 */
+ dirdata_make_de(buf, TEST_FILE1_INO, "abc", EXT4_FT_REG_FILE,
+ true, 1, false, 0, 0, 64);
+ buf[EXT4_BASE_DIR_LEN + 3 + 1] = 0;
+ KUNIT_EXPECT_TRUE(test, ext4_dirent_data_corrupt(&ctx->sb, de, 64));
+
+ /* Without the feature the flags are not interpreted */
+ ctx->es.s_feature_incompat &=
+ ~cpu_to_le32(EXT4_FEATURE_INCOMPAT_DIRDATA);
+ KUNIT_EXPECT_FALSE(test, ext4_dirent_data_corrupt(&ctx->sb, de, 64));
+ dirdata_enable(ctx);
+
+ /* The checksum tail is never parsed for dirdata */
+ bh = map_and_get_dir_bh(ctx, dir, 0);
+ KUNIT_ASSERT_NOT_NULL(test, bh);
+ ext4_initialize_dirent_tail(bh, TEST_BLK_SIZE);
+ de = (struct ext4_dir_entry_2 *)EXT4_DIRENT_TAIL(bh->b_data,
+ TEST_BLK_SIZE);
+ KUNIT_EXPECT_TRUE(test, ext4_dir_entry_is_tail(de));
+ KUNIT_EXPECT_EQ(test, ext4_dirent_get_data_len(de, 12), 0);
+ KUNIT_EXPECT_FALSE(test, ext4_dirent_data_corrupt(&ctx->sb, de, 12));
+}
+
+/*
+ * Dirdata 2: __ext4_check_dir_entry() accepts an entry with well-formed
+ * records and rejects one whose record overruns it or whose rec_len does
+ * not cover its records.
+ */
+static void test_dirdata_check_dir_entry(struct kunit *test)
+{
+ struct namei_test_ctx *ctx = test->priv;
+ struct inode *dir = get_mock_inode(ctx, TEST_DIR_INO);
+ struct ext4_dir_entry_2 *de;
+ struct buffer_head *bh;
+
+ bh = map_and_get_dir_bh(ctx, dir, 0);
+ KUNIT_ASSERT_NOT_NULL(test, bh);
+ de = (struct ext4_dir_entry_2 *)bh->b_data;
+ dirdata_enable(ctx);
+
+ dirdata_make_de(bh->b_data, TEST_FILE1_INO, "abc", EXT4_FT_REG_FILE,
+ true, 1, true, 1, 2, TEST_BLK_SIZE);
+ KUNIT_EXPECT_EQ(test, ext4_check_dir_entry(dir, NULL, de, bh,
+ bh->b_data, TEST_BLK_SIZE,
+ 0), 0);
+
+ /* rec_len 28: the LUFID record ends at byte 29 */
+ dirdata_make_de(bh->b_data, TEST_FILE1_INO, "abc", EXT4_FT_REG_FILE,
+ true, 1, false, 0, 0, TEST_BLK_SIZE);
+ de->rec_len = ext4_rec_len_to_disk(28, TEST_BLK_SIZE);
+ KUNIT_EXPECT_NE(test, ext4_check_dir_entry(dir, NULL, de, bh,
+ bh->b_data, TEST_BLK_SIZE,
+ 0), 0);
+
+ /* rec_len 32 holds the LUFID but not a CFHASH after it */
+ dirdata_make_de(bh->b_data, TEST_FILE1_INO, "abc", EXT4_FT_REG_FILE,
+ true, 1, true, 1, 2, TEST_BLK_SIZE);
+ de->rec_len = ext4_rec_len_to_disk(32, TEST_BLK_SIZE);
+ KUNIT_EXPECT_NE(test, ext4_check_dir_entry(dir, NULL, de, bh,
+ bh->b_data, TEST_BLK_SIZE,
+ 0), 0);
+}
+
+/*
+ * Dirdata 3: dx_get_dx_info() finds dx_root_info after '.' and '..' of any
+ * length, rejects a root whose '..' records are malformed, and
+ * dx_root_limit() sizes the dx_entry array from where it starts.
+ */
+static void test_dirdata_dx_root_info(struct kunit *test)
+{
+ struct namei_test_ctx *ctx = test->priv;
+ struct inode *dir = get_mock_inode(ctx, TEST_DIR_INO);
+ struct dx_root_info *info;
+ struct buffer_head *bh;
+ unsigned int dotdot_len;
+ char *b;
+
+ bh = map_and_get_dir_bh(ctx, dir, 0);
+ KUNIT_ASSERT_NOT_NULL(test, bh);
+ b = bh->b_data;
+ dirdata_enable(ctx);
+
+ /* Plain root: dx_root_info at the offset struct dx_root gives it */
+ dirdata_make_de(b, dir->i_ino, ".", EXT4_FT_DIR, false, 0, false,
+ 0, 0, 12);
+ dirdata_make_de(b + 12, TEST_ROOT_INO, "..", EXT4_FT_DIR, false, 0,
+ false, 0, 0, TEST_BLK_SIZE - 12);
+ info = dx_get_dx_info_test(dir, b);
+ KUNIT_ASSERT_FALSE(test, IS_ERR(info));
+ KUNIT_EXPECT_EQ(test, (char *)info - b,
+ (long)offsetof(struct dx_root, info));
+ KUNIT_EXPECT_EQ(test, dx_root_limit(dir,
+ offsetof(struct dx_root, entries)),
+ 124U);
+
+ /* '..' with a LUFID moves dx_root_info by the record */
+ dotdot_len = round_up(EXT4_BASE_DIR_LEN + 2 + 1 + TEST_LUFID_LEN, 4);
+ dirdata_make_de(b + 12, TEST_ROOT_INO, "..", EXT4_FT_DIR, true, 7,
+ false, 0, 0, TEST_BLK_SIZE - 12);
+ info = dx_get_dx_info_test(dir, b);
+ KUNIT_ASSERT_FALSE(test, IS_ERR(info));
+ KUNIT_EXPECT_EQ(test, (char *)info - b, (long)(12 + dotdot_len));
+ KUNIT_EXPECT_EQ(test, dx_root_limit(dir, 12 + dotdot_len +
+ sizeof(struct dx_root_info)),
+ (TEST_BLK_SIZE - 12 - dotdot_len -
+ (unsigned int)sizeof(struct dx_root_info)) /
+ (unsigned int)sizeof(struct dx_entry));
+
+ /* '.' with a LUFID too: '..' is found through the rec_len of '.' */
+ dirdata_make_de(b, dir->i_ino, ".", EXT4_FT_DIR, true, 3, false, 0, 0,
+ 28);
+ dirdata_make_de(b + 28, TEST_ROOT_INO, "..", EXT4_FT_DIR, true, 7,
+ false, 0, 0, TEST_BLK_SIZE - 28);
+ info = dx_get_dx_info_test(dir, b);
+ KUNIT_ASSERT_FALSE(test, IS_ERR(info));
+ KUNIT_EXPECT_EQ(test, (char *)info - b, (long)(28 + dotdot_len));
+
+ /* A zero-length record in '..' is corruption, not "no dirdata" */
+ b[28 + EXT4_BASE_DIR_LEN + 2 + 1] = 0;
+ KUNIT_EXPECT_TRUE(test, IS_ERR(dx_get_dx_info_test(dir, b)));
+
+ /* So is a rec_len of '.' that leaves no room for '..' */
+ dirdata_make_de(b, dir->i_ino, ".", EXT4_FT_DIR, false, 0, false,
+ 0, 0, TEST_BLK_SIZE - 8);
+ KUNIT_EXPECT_TRUE(test, IS_ERR(dx_get_dx_info_test(dir, b)));
+}
+
+/*
+ * Dirdata 4: entries added with a LUFID carry it, entries added without
+ * one do not, and without the feature the LUFID is ignored.
+ */
+static void test_dirdata_add_entry_lufid(struct kunit *test)
+{
+ struct namei_test_ctx *ctx = test->priv;
+ struct inode *root_dir = get_mock_inode(ctx, TEST_ROOT_INO);
+ struct inode *dir = get_mock_inode(ctx, TEST_DIR_INO);
+ struct inode *file1 = get_mock_inode(ctx, TEST_FILE1_INO);
+ struct inode *file2 = get_mock_inode(ctx, TEST_FILE2_INO);
+ struct dentry *dentries, *parent, *child;
+ struct ext4_dir_entry_2 *de;
+ struct buffer_head *bh;
+
+ dentries = kunit_kcalloc(test, 2, sizeof(*dentries), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, dentries);
+ parent = &dentries[0];
+ child = &dentries[1];
+ ctx->es.s_feature_ro_compat |=
+ cpu_to_le32(EXT4_FEATURE_RO_COMPAT_METADATA_CSUM);
+ dirdata_enable(ctx);
+
+ KUNIT_ASSERT_EQ(test, ext4_init_new_dir(NULL, root_dir, dir), 0);
+ init_mock_dentry(parent, NULL, "dir", dir, &ctx->sb);
+
+ init_mock_dentry(child, parent, "with_fid", file1, &ctx->sb);
+ child->d_fsdata = dirdata_make_edp(test, 0x40);
+ KUNIT_ASSERT_EQ(test, ext4_add_entry_test(NULL, child, file1), 0);
+
+ init_mock_dentry(child, parent, "no_fid", file2, &ctx->sb);
+ KUNIT_ASSERT_EQ(test, ext4_add_entry_test(NULL, child, file2), 0);
+
+ bh = dirdata_find(test, dir, "with_fid", &de);
+ KUNIT_EXPECT_EQ(test, le32_to_cpu(de->inode), TEST_FILE1_INO);
+ KUNIT_EXPECT_EQ(test, de->file_type & ~EXT4_FT_MASK,
+ EXT4_DIRENT_LUFID);
+ KUNIT_EXPECT_EQ(test, de->file_type & EXT4_FT_MASK, EXT4_FT_REG_FILE);
+ KUNIT_EXPECT_TRUE(test, dirdata_has_fid(dir, de, 0x40));
+ KUNIT_EXPECT_EQ(test, ext4_dir_entry_len(de, TEST_BLK_SIZE, dir),
+ round_up(EXT4_BASE_DIR_LEN + 8 + 1 + TEST_LUFID_LEN,
+ 4));
+ brelse(bh);
+
+ bh = dirdata_find(test, dir, "no_fid", &de);
+ KUNIT_EXPECT_EQ(test, de->file_type, EXT4_FT_REG_FILE);
+ KUNIT_EXPECT_FALSE(test, dirdata_has_fid(dir, de, 0x40));
+ brelse(bh);
+
+ KUNIT_EXPECT_EQ(test, ext4_dirblock_csum_verify(dir,
+ map_and_get_dir_bh(ctx, dir, 0)), 1);
+
+ /* Without the feature the LUFID passed in is not stored */
+ ctx->es.s_feature_incompat &=
+ ~cpu_to_le32(EXT4_FEATURE_INCOMPAT_DIRDATA);
+ init_mock_dentry(child, parent, "ignored_fid", file1, &ctx->sb);
+ child->d_fsdata = dirdata_make_edp(test, 0x50);
+ KUNIT_ASSERT_EQ(test, ext4_add_entry_test(NULL, child, file1), 0);
+ bh = dirdata_find(test, dir, "ignored_fid", &de);
+ KUNIT_EXPECT_EQ(test, de->file_type, EXT4_FT_REG_FILE);
+ brelse(bh);
+}
+
+/*
+ * Dirdata 5: a one-block directory whose '..' carries a LUFID becomes an
+ * htree (make_indexed_dir): '..' keeps its LUFID, dx_root_info follows it,
+ * the root limit accounts for it, and every entry, each with its own
+ * LUFID, survives the leaf splits and is found again.
+ */
+static void test_dirdata_htree_with_lufids(struct kunit *test)
+{
+ struct namei_test_ctx *ctx = test->priv;
+ struct inode *root_dir = get_mock_inode(ctx, TEST_ROOT_INO);
+ struct inode *dir = get_mock_inode(ctx, TEST_DIR_INO);
+ struct inode *file1 = get_mock_inode(ctx, TEST_FILE1_INO);
+ struct dentry *dentries, *parent, *child;
+ struct ext4_dir_entry_2 *de, *dotdot;
+ struct dir_private_info *dir_info;
+ struct dx_root_info *info;
+ struct dx_entry *entries;
+ struct buffer_head *bh;
+ struct file *dir_file;
+ unsigned int dotdot_len, info_off;
+ __u32 curr_hash = 0, next_hash = 0;
+ int i, total = 0;
+
+ dentries = kunit_kcalloc(test, 2, sizeof(*dentries), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, dentries);
+ parent = &dentries[0];
+ child = &dentries[1];
+ dir_file = kunit_kzalloc(test, sizeof(*dir_file), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, dir_file);
+ ctx->es.s_feature_ro_compat |=
+ cpu_to_le32(EXT4_FEATURE_RO_COMPAT_METADATA_CSUM);
+ dirdata_enable(ctx);
+
+ KUNIT_ASSERT_EQ(test, ext4_init_new_dir(NULL, root_dir, dir), 0);
+ bh = map_and_get_dir_bh(ctx, dir, 0);
+ KUNIT_ASSERT_NOT_NULL(test, bh);
+ dirdata_set_dotdot_fid(test, dir, bh, 0x20);
+ init_mock_dentry(parent, NULL, "dir", dir, &ctx->sb);
+
+ /* 40 bytes per entry: ~24 fit in a block, so the leaves split */
+ for (i = 0; i < 60; i++) {
+ char name[16];
+
+ snprintf(name, sizeof(name), "file_%05d", i);
+ init_mock_dentry(child, parent, name, file1, &ctx->sb);
+ child->d_fsdata = dirdata_make_edp(test, i);
+ KUNIT_ASSERT_EQ_MSG(test,
+ ext4_add_entry_test(NULL, child, file1), 0,
+ "failed at entry %d", i);
+ }
+ KUNIT_EXPECT_TRUE(test, ext4_test_inode_flag(dir, EXT4_INODE_INDEX));
+ KUNIT_EXPECT_GE(test, dir->i_size, (loff_t)(4 * TEST_BLK_SIZE));
+
+ /* '..' kept its LUFID and now ends where dx_root_info starts */
+ dotdot = ext4_next_entry((struct ext4_dir_entry_2 *)bh->b_data,
+ TEST_BLK_SIZE);
+ KUNIT_EXPECT_TRUE(test, dirdata_has_fid(dir, dotdot, 0x20));
+ dotdot_len = round_up(EXT4_BASE_DIR_LEN + 2 + 1 + TEST_LUFID_LEN, 4);
+ KUNIT_EXPECT_EQ(test, ext4_rec_len_from_disk(dotdot->rec_len,
+ TEST_BLK_SIZE),
+ TEST_BLK_SIZE - 12);
+ info = dx_get_dx_info_test(dir, bh->b_data);
+ KUNIT_ASSERT_FALSE(test, IS_ERR(info));
+ info_off = (char *)info - bh->b_data;
+ KUNIT_EXPECT_EQ(test, info_off, 12 + dotdot_len);
+ KUNIT_EXPECT_EQ(test, info->info_length,
+ (u8)sizeof(struct dx_root_info));
+ entries = (struct dx_entry *)((char *)info + info->info_length);
+ KUNIT_EXPECT_EQ(test, dx_get_limit(entries),
+ dx_root_limit(dir, info_off + info->info_length));
+ KUNIT_EXPECT_EQ(test, dx_get_limit(entries),
+ (TEST_BLK_SIZE - info_off - info->info_length -
+ (unsigned int)sizeof(struct dx_tail)) /
+ (unsigned int)sizeof(struct dx_entry));
+ KUNIT_EXPECT_GT(test, dx_get_count(entries), 2U);
+
+ /* Every entry is found through the index, with its own LUFID */
+ for (i = 0; i < 60; i++) {
+ char name[16];
+ struct buffer_head *ebh;
+
+ snprintf(name, sizeof(name), "file_%05d", i);
+ ebh = dirdata_find(test, dir, name, &de);
+ KUNIT_EXPECT_EQ(test, le32_to_cpu(de->inode), TEST_FILE1_INO);
+ KUNIT_EXPECT_TRUE_MSG(test, dirdata_has_fid(dir, de, i),
+ "wrong LUFID on %s", name);
+ brelse(ebh);
+ }
+
+ /* readdir sees 60 files, '.' and '..' */
+ dir_file->f_inode = dir;
+ while (1) {
+ int cnt;
+
+ dir_info = kzalloc_obj(*dir_info);
+ KUNIT_ASSERT_NOT_NULL(test, dir_info);
+ dir_file->private_data = dir_info;
+ cnt = ext4_htree_fill_tree(dir_file, curr_hash, 0, &next_hash);
+ KUNIT_ASSERT_GE(test, cnt, 0);
+ total += cnt;
+ ext4_htree_free_dir_info(dir_info);
+ if (next_hash == ~0U)
+ break;
+ curr_hash = next_hash;
+ }
+ KUNIT_EXPECT_EQ(test, total, 62);
+}
+
+/* The rename tests look the target up through ext4_iget() */
+static struct inode *dirdata_iget_stub(struct super_block *sb,
+ unsigned long ino,
+ ext4_iget_flags flags,
+ const char *function,
+ unsigned int line)
+{
+ struct kunit *test = kunit_get_current_test();
+ struct namei_test_ctx *ctx = test->priv;
+ struct mock_inode_slot *slot = find_inode_slot_by_ino(ctx, ino);
+
+ ctx->last_iget_ino = ino;
+ if (!slot)
+ return ERR_PTR(-ESTALE);
+ ihold(&slot->ei.vfs_inode);
+ return &slot->ei.vfs_inode;
+}
+
+/*
+ * Dirdata 6: rename keeps the LUFID of the entry it moves, within a
+ * directory and across directories, and RENAME_EXCHANGE swaps the LUFIDs
+ * together with the inodes.
+ */
+static void test_dirdata_rename_keeps_lufid(struct kunit *test)
+{
+ struct namei_test_ctx *ctx = test->priv;
+ struct inode *root_dir = get_mock_inode(ctx, TEST_ROOT_INO);
+ struct inode *dir1 = get_mock_inode(ctx, TEST_DIR_INO);
+ struct inode *dir2 = get_mock_inode(ctx, TEST_DIR2_INO);
+ struct inode *file1 = get_mock_inode(ctx, TEST_FILE1_INO);
+ struct inode *file2 = get_mock_inode(ctx, TEST_FILE2_INO);
+ struct dentry *dentries, *d1, *d2, *old_dentry, *new_dentry;
+ struct ext4_dir_entry_2 *de;
+ struct buffer_head *bh;
+
+ dentries = kunit_kcalloc(test, 4, sizeof(*dentries), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, dentries);
+ d1 = &dentries[0];
+ d2 = &dentries[1];
+ old_dentry = &dentries[2];
+ new_dentry = &dentries[3];
+ ctx->es.s_feature_ro_compat |=
+ cpu_to_le32(EXT4_FEATURE_RO_COMPAT_METADATA_CSUM);
+ dirdata_enable(ctx);
+
+ KUNIT_ASSERT_EQ(test, ext4_init_new_dir(NULL, root_dir, dir1), 0);
+ KUNIT_ASSERT_EQ(test, ext4_init_new_dir(NULL, root_dir, dir2), 0);
+ init_mock_dentry(d1, NULL, "dir1", dir1, &ctx->sb);
+ init_mock_dentry(d2, NULL, "dir2", dir2, &ctx->sb);
+
+ init_mock_dentry(old_dentry, d1, "f1", file1, &ctx->sb);
+ old_dentry->d_fsdata = dirdata_make_edp(test, 0x11);
+ KUNIT_ASSERT_EQ(test, ext4_add_entry_test(NULL, old_dentry, file1), 0);
+ old_dentry->d_fsdata = NULL;
+
+ /* Within the directory */
+ init_mock_dentry(new_dentry, d1, "f1_renamed", NULL, &ctx->sb);
+ KUNIT_ASSERT_EQ(test,
+ ext4_dir_inode_operations.rename(&nop_mnt_idmap, dir1,
+ old_dentry, dir1, new_dentry, 0), 0);
+ bh = dirdata_find(test, dir1, "f1_renamed", &de);
+ KUNIT_EXPECT_TRUE(test, dirdata_has_fid(dir1, de, 0x11));
+ brelse(bh);
+
+ /* Across directories */
+ init_mock_dentry(old_dentry, d1, "f1_renamed", file1, &ctx->sb);
+ init_mock_dentry(new_dentry, d2, "f1_moved", NULL, &ctx->sb);
+ KUNIT_ASSERT_EQ(test,
+ ext4_dir_inode_operations.rename(&nop_mnt_idmap, dir1,
+ old_dentry, dir2, new_dentry, 0), 0);
+ bh = dirdata_find(test, dir2, "f1_moved", &de);
+ KUNIT_EXPECT_EQ(test, le32_to_cpu(de->inode), TEST_FILE1_INO);
+ KUNIT_EXPECT_TRUE(test, dirdata_has_fid(dir2, de, 0x11));
+ brelse(bh);
+
+ /* RENAME_EXCHANGE: each LUFID follows its inode */
+ init_mock_dentry(old_dentry, d1, "f2", file2, &ctx->sb);
+ old_dentry->d_fsdata = dirdata_make_edp(test, 0x22);
+ KUNIT_ASSERT_EQ(test, ext4_add_entry_test(NULL, old_dentry, file2), 0);
+ old_dentry->d_fsdata = NULL;
+ init_mock_dentry(new_dentry, d2, "f1_moved", file1, &ctx->sb);
+ KUNIT_ASSERT_EQ(test,
+ ext4_dir_inode_operations.rename(&nop_mnt_idmap, dir1,
+ old_dentry, dir2, new_dentry, RENAME_EXCHANGE), 0);
+ bh = dirdata_find(test, dir1, "f2", &de);
+ KUNIT_EXPECT_EQ(test, le32_to_cpu(de->inode), TEST_FILE1_INO);
+ KUNIT_EXPECT_TRUE(test, dirdata_has_fid(dir1, de, 0x11));
+ brelse(bh);
+ bh = dirdata_find(test, dir2, "f1_moved", &de);
+ KUNIT_EXPECT_EQ(test, le32_to_cpu(de->inode), TEST_FILE2_INO);
+ KUNIT_EXPECT_TRUE(test, dirdata_has_fid(dir2, de, 0x22));
+ brelse(bh);
+}
+
+/*
+ * Dirdata 7: ext4_dirdata_set_lufid(), behind EXT4_IOC_SET_LUFID, adds a
+ * LUFID to an existing entry and replaces it, and refuses a missing name
+ * and a filesystem without the feature.
+ */
+static void test_dirdata_set_lufid(struct kunit *test)
+{
+ struct namei_test_ctx *ctx = test->priv;
+ struct inode *root_dir = get_mock_inode(ctx, TEST_ROOT_INO);
+ struct inode *dir = get_mock_inode(ctx, TEST_DIR_INO);
+ struct inode *file1 = get_mock_inode(ctx, TEST_FILE1_INO);
+ struct dentry *dentries, *parent, *child;
+ struct ext4_dir_entry_2 *de;
+ struct buffer_head *bh;
+
+ kunit_activate_static_stub(test, __ext4_iget, dirdata_iget_stub);
+ dentries = kunit_kcalloc(test, 2, sizeof(*dentries), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, dentries);
+ parent = &dentries[0];
+ child = &dentries[1];
+ ctx->es.s_feature_ro_compat |=
+ cpu_to_le32(EXT4_FEATURE_RO_COMPAT_METADATA_CSUM);
+ dirdata_enable(ctx);
+
+ KUNIT_ASSERT_EQ(test, ext4_init_new_dir(NULL, root_dir, dir), 0);
+ init_mock_dentry(parent, NULL, "dir", dir, &ctx->sb);
+ init_mock_dentry(child, parent, "target", file1, &ctx->sb);
+ KUNIT_ASSERT_EQ(test, ext4_add_entry_test(NULL, child, file1), 0);
+
+ KUNIT_EXPECT_EQ(test,
+ ext4_dirdata_set_lufid(&nop_mnt_idmap, dir, "target", 6,
+ dirdata_make_edp(test, 0x30)), 0);
+ bh = dirdata_find(test, dir, "target", &de);
+ KUNIT_EXPECT_EQ(test, le32_to_cpu(de->inode), TEST_FILE1_INO);
+ KUNIT_EXPECT_TRUE(test, dirdata_has_fid(dir, de, 0x30));
+ brelse(bh);
+
+ KUNIT_EXPECT_EQ(test,
+ ext4_dirdata_set_lufid(&nop_mnt_idmap, dir, "target", 6,
+ dirdata_make_edp(test, 0x60)), 0);
+ bh = dirdata_find(test, dir, "target", &de);
+ KUNIT_EXPECT_TRUE(test, dirdata_has_fid(dir, de, 0x60));
+ brelse(bh);
+ KUNIT_EXPECT_EQ(test, ext4_dirblock_csum_verify(dir,
+ map_and_get_dir_bh(ctx, dir, 0)), 1);
+
+ KUNIT_EXPECT_EQ(test,
+ ext4_dirdata_set_lufid(&nop_mnt_idmap, dir, "missing", 7,
+ dirdata_make_edp(test, 0x30)), -ENOENT);
+
+ ctx->es.s_feature_incompat &=
+ ~cpu_to_le32(EXT4_FEATURE_INCOMPAT_DIRDATA);
+ KUNIT_EXPECT_EQ(test,
+ ext4_dirdata_set_lufid(&nop_mnt_idmap, dir, "target", 6,
+ dirdata_make_edp(test, 0x30)),
+ -EOPNOTSUPP);
+}
+
+/*
+ * Dirdata 8: in a casefolded and encrypted directory the hash is read from
+ * the CFHASH record, wherever it is, or from its fixed place after the name
+ * of an entry written before dirdata; an entry too short for the hash, such
+ * as '.', or with other records at that place, yields none.
+ */
+static void test_dirdata_hash_get(struct kunit *test)
+{
+ struct namei_test_ctx *ctx = test->priv;
+ struct inode *dir = get_mock_inode(ctx, TEST_DIR_INO);
+ struct dx_hash_info hinfo;
+ struct ext4_dir_entry_2 *de;
+ char *buf;
+
+ buf = kunit_kzalloc(test, TEST_BLK_SIZE, GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, buf);
+ de = (struct ext4_dir_entry_2 *)buf;
+ dir->i_flags |= S_CASEFOLD | S_ENCRYPTED;
+ KUNIT_ASSERT_TRUE(test, ext4_hash_in_dirent(dir));
+ dirdata_enable(ctx);
+
+ /* CFHASH record */
+ dirdata_make_de(buf, TEST_FILE1_INO, "abcd", EXT4_FT_REG_FILE,
+ false, 0, true, 0xaaaa0000, 0x0000bbbb, 64);
+ memset(&hinfo, 0, sizeof(hinfo));
+ KUNIT_EXPECT_TRUE(test,
+ ext4_dirent_hash_get(de, dir, TEST_BLK_SIZE, &hinfo));
+ KUNIT_EXPECT_EQ(test, hinfo.hash, 0xaaaa0000U);
+ KUNIT_EXPECT_EQ(test, hinfo.minor_hash, 0x0000bbbbU);
+
+ /* CFHASH after a LUFID */
+ dirdata_make_de(buf, TEST_FILE1_INO, "abcd", EXT4_FT_REG_FILE,
+ true, 9, true, 0xcccc0000, 0x0000dddd, 64);
+ memset(&hinfo, 0, sizeof(hinfo));
+ KUNIT_EXPECT_TRUE(test,
+ ext4_dirent_hash_get(de, dir, TEST_BLK_SIZE, &hinfo));
+ KUNIT_EXPECT_EQ(test, hinfo.hash, 0xcccc0000U);
+ KUNIT_EXPECT_EQ(test, hinfo.minor_hash, 0x0000ddddU);
+
+ /* A LUFID without CFHASH: the LUFID sits where the hash would be */
+ dirdata_make_de(buf, TEST_FILE1_INO, "abcd", EXT4_FT_REG_FILE,
+ true, 9, false, 0, 0, 64);
+ KUNIT_EXPECT_FALSE(test,
+ ext4_dirent_hash_get(de, dir, TEST_BLK_SIZE, &hinfo));
+
+ /* An entry written before dirdata: hash at its fixed offset */
+ dirdata_make_de(buf, TEST_FILE1_INO, "abcd", EXT4_FT_REG_FILE,
+ false, 0, false, 0, 0, 20);
+ EXT4_DIRENT_HASHES(de)->hash = cpu_to_le32(0x12340000);
+ EXT4_DIRENT_HASHES(de)->minor_hash = cpu_to_le32(0x00005678);
+ memset(&hinfo, 0, sizeof(hinfo));
+ KUNIT_EXPECT_TRUE(test,
+ ext4_dirent_hash_get(de, dir, TEST_BLK_SIZE, &hinfo));
+ KUNIT_EXPECT_EQ(test, hinfo.hash, 0x12340000U);
+ KUNIT_EXPECT_EQ(test, hinfo.minor_hash, 0x00005678U);
+
+ /* '.' is 12 bytes long and has no room for a hash */
+ dirdata_make_de(buf, dir->i_ino, ".", EXT4_FT_DIR, false, 0, false,
+ 0, 0, 12);
+ KUNIT_EXPECT_FALSE(test,
+ ext4_dirent_hash_get(de, dir, TEST_BLK_SIZE, &hinfo));
+
+ dir->i_flags &= ~(S_CASEFOLD | S_ENCRYPTED);
+}
+
+/*
+ * Dirdata 9: an htree index node of a casefolded and encrypted directory
+ * may have the limit that the kernel computes or the one that e2fsck
+ * computes, which reserves room for the hash in the fake dirent.
+ */
+static void test_dirdata_dx_node_limit(struct kunit *test)
+{
+ struct namei_test_ctx *ctx = test->priv;
+ struct inode *dir = get_mock_inode(ctx, TEST_DIR_INO);
+ unsigned int limit = dx_node_limit(dir);
+
+ /* Plain directory: one valid limit */
+ KUNIT_EXPECT_EQ(test, limit, 127U);
+ KUNIT_EXPECT_TRUE(test, dx_node_limit_ok(dir, limit));
+ KUNIT_EXPECT_FALSE(test, dx_node_limit_ok(dir, limit - 1));
+
+ dir->i_flags |= S_CASEFOLD | S_ENCRYPTED;
+ KUNIT_EXPECT_EQ(test, dx_node_limit(dir), limit);
+ KUNIT_EXPECT_TRUE(test, dx_node_limit_ok(dir, limit));
+ KUNIT_EXPECT_TRUE(test, dx_node_limit_ok(dir, limit - 1));
+ KUNIT_EXPECT_FALSE(test, dx_node_limit_ok(dir, limit - 2));
+ KUNIT_EXPECT_FALSE(test, dx_node_limit_ok(dir, limit + 1));
+
+ /* With metadata_csum the dx_tail comes off both */
+ ctx->es.s_feature_ro_compat |=
+ cpu_to_le32(EXT4_FEATURE_RO_COMPAT_METADATA_CSUM);
+ limit = dx_node_limit(dir);
+ KUNIT_EXPECT_EQ(test, limit, 126U);
+ KUNIT_EXPECT_TRUE(test, dx_node_limit_ok(dir, limit - 1));
+ KUNIT_EXPECT_FALSE(test, dx_node_limit_ok(dir, limit - 2));
+ dir->i_flags &= ~(S_CASEFOLD | S_ENCRYPTED);
+}
+
static struct kunit_case namei_test_cases[] = {
KUNIT_CASE(test_namei_dirent_tail_and_csum),
KUNIT_CASE(test_namei_dx_helpers_and_csum),
@@ -2344,6 +3075,27 @@ static struct kunit_suite namei_test_suite = {
.test_cases = namei_test_cases,
};
-kunit_test_suites(&namei_test_suite);
+static struct kunit_case dirdata_test_cases[] = {
+ KUNIT_CASE(test_dirdata_record_lengths),
+ KUNIT_CASE(test_dirdata_check_dir_entry),
+ KUNIT_CASE(test_dirdata_dx_root_info),
+ KUNIT_CASE(test_dirdata_add_entry_lufid),
+ KUNIT_CASE(test_dirdata_htree_with_lufids),
+ KUNIT_CASE(test_dirdata_rename_keeps_lufid),
+ KUNIT_CASE(test_dirdata_set_lufid),
+ KUNIT_CASE(test_dirdata_hash_get),
+ KUNIT_CASE(test_dirdata_dx_node_limit),
+ {}
+};
+
+/* Same fixture as ext4_namei_test */
+static struct kunit_suite dirdata_test_suite = {
+ .name = "ext4_dirdata_test",
+ .init = namei_test_init,
+ .exit = namei_test_exit,
+ .test_cases = dirdata_test_cases,
+};
+
+kunit_test_suites(&namei_test_suite, &dirdata_test_suite);
MODULE_LICENSE("GPL");
diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index f9250407cedb..b8a5bd803603 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -1323,6 +1323,7 @@ unsigned char ext4_dirdata_get(struct ext4_dir_entry_2 *de, struct inode *dir,
return ret;
}
+EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_dirdata_get);
/*
* ext4_dirent_hash_get() - read the hash stored in a casefold+fscrypt entry
@@ -1355,6 +1356,7 @@ bool ext4_dirent_hash_get(struct ext4_dir_entry_2 *de, struct inode *dir,
hinfo->minor_hash = EXT4_DIRENT_MINOR_HASH(de);
return true;
}
+EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_dirent_hash_get);
/*
* ext4_dirdata_set() - Write dirdata fields into a directory entry.
@@ -4991,6 +4993,7 @@ int ext4_dirdata_set_lufid(const struct mnt_idmap *idmap, struct inode *dir,
return err;
}
+EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_dirdata_set_lufid);
/*
* directories can handle most operations...
@@ -5132,6 +5135,12 @@ bool ext4_check_dx_root_test(struct inode *dir, struct dx_root *root)
}
EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_check_dx_root_test);
+struct dx_root_info *dx_get_dx_info_test(struct inode *dir, void *de_buf)
+{
+ return dx_get_dx_info(dir, de_buf);
+}
+EXPORT_SYMBOL_FOR_EXT4_TEST(dx_get_dx_info_test);
+
struct buffer_head *ext4_find_entry_test(struct inode *dir,
const struct qstr *d_name,
struct ext4_dir_entry_2 **res_dir,
diff --git a/fs/ext4/namei.h b/fs/ext4/namei.h
index 946a5f705f4d..3a7560a27720 100644
--- a/fs/ext4/namei.h
+++ b/fs/ext4/namei.h
@@ -227,6 +227,7 @@ struct ext4_dir_entry_2 *do_split_test(handle_t *handle, struct inode *dir,
struct dx_frame *frame,
struct dx_hash_info *hinfo);
bool ext4_check_dx_root_test(struct inode *dir, struct dx_root *root);
+struct dx_root_info *dx_get_dx_info_test(struct inode *dir, void *de_buf);
struct buffer_head *ext4_find_entry_test(struct inode *dir,
const struct qstr *d_name,
struct ext4_dir_entry_2 **res_dir,
--
2.52.0
^ permalink raw reply related [flat|nested] 26+ messages in thread
* Re: [PATCH v15 01/13] ext4: validate count against limit in ext4_dx_csum_verify/_set
2026-12-03 18:49 ` [PATCH v15 01/13] ext4: validate count against limit in ext4_dx_csum_verify/_set Artem Blagodarenko
@ 2026-12-03 19:03 ` sashiko-bot
0 siblings, 0 replies; 26+ messages in thread
From: sashiko-bot @ 2026-12-03 19:03 UTC (permalink / raw)
To: Artem Blagodarenko, linux-ext4; +Cc: tytso
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] ext4: missing Fixes trailer for commit dbe89444042a
--
commit dfe4323227733fc5c8188483fb42fa83963af5d1
Author: Artem Blagodarenko <artem.blagodarenko@gmail.com>
> ext4: validate count against limit in ext4_dx_csum_verify/_set
>
> dx_countlimit's count field was read from disk and used directly to
> compute the checksummed range (count_offset + count * sizeof(dx_entry))
> without ever being checked against limit -- only limit itself was
> bounds-checked against the block size. A corrupted or maliciously
> crafted filesystem image that sets count to a large value (e.g. 65535)
> makes ext4_chksum() read far past the end of the directory block
> buffer, hitting adjacent slab objects.
>
> Reported-by: xiaowu.417@qq.com
> Signed-off-by: Artem Blagodarenko <artem.blagodarenko@gmail.com>
> Assisted-by: Claude:claude-opus-5-5
[Severity: Low]
This fixes an out-of-bounds read in ext4_dx_csum_verify() and
ext4_dx_csum_set() that has existed since htree checksumming was
introduced. Should this include:
Fixes: dbe89444042a ("ext4: Calculate and verify checksums for htree nodes")
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203184938.422849-1-ablagodarenko@thelustrecollective.com?part=1
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v15 03/13] ext4: replace ext4_dir_entry with ext4_dir_entry_2
2026-12-03 18:49 ` [PATCH v15 03/13] ext4: replace ext4_dir_entry with ext4_dir_entry_2 Artem Blagodarenko
@ 2026-12-03 19:33 ` sashiko-bot
0 siblings, 0 replies; 26+ messages in thread
From: sashiko-bot @ 2026-12-03 19:33 UTC (permalink / raw)
To: Artem Blagodarenko, linux-ext4; +Cc: tytso
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] ext4: EXT4_BASE_DIR_LEN change exposes OOB read in
is_fake_dir_entry()
- [Low] ext4: commit message omits EXT4_BASE_DIR_LEN change and
misstates rationale
--
commit 905a73de84c1289ce2b9fc43573bee81e7c8e5f8
Author: Artem Blagodarenko <artem.blagodarenko@gmail.com>
> ext4: replace ext4_dir_entry with ext4_dir_entry_2
>
> Replace remaining uses of struct ext4_dir_entry in namei.c
> with struct ext4_dir_entry_2.
>
> The code paths affected by this change already depend on the
> filetype feature, so using struct ext4_dir_entry_2 is
> appropriate and avoids mixing the two directory entry types
> unnecessarily.
>
> This change does not affect support for 16-bit rec_len.
>
> Signed-off-by: Artem Blagodarenko <artem.blagodarenko@gmail.com>
> Reviewed-by: Andreas Dilger <adilger@dilger.ca>
[Severity: Low]
Does this commit message accurately cover the changes and rationale?
The patch also changes EXT4_BASE_DIR_LEN in fs/ext4/ext4.h from 9 to 8,
which alters the loop bound in ext4_search_dir() and is unrelated to
replacing struct ext4_dir_entry. If split out, that change would fix
commit c186f0887fe7 ("ext4: fix use-after-free in ext4_search_dir").
The changelog also omits the count_offset = sizeof(struct dx_node) cleanup
in get_dx_countlimit() and the KUnit updates in fs/ext4/namei.h and
fs/ext4/namei-test.c.
The converted functions in fs/ext4/namei.c run regardless of whether
EXT4_FEATURE_INCOMPAT_FILETYPE is set; the rename is safe because both
structs share the same inode and rec_len layout and none of these paths
access name_len or file_type.
Both structs also have the same __le16 rec_len field, so it is 16-bit
name_len rather than rec_len that differs between them.
> diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
> index dc2c181337a16..ca727888425ab 100644
> --- a/fs/ext4/ext4.h
> +++ b/fs/ext4/ext4.h
> @@ -2523,10 +2523,8 @@ static inline int ext4_emergency_state(struct super_block *sb)
> * Structure of a directory entry
> */
> #define EXT4_NAME_LEN 255
> -/*
> - * Base length of the ext4 directory entry excluding the name length
> - */
> -#define EXT4_BASE_DIR_LEN (sizeof(struct ext4_dir_entry_2) - EXT4_NAME_LEN)
> +/* offsetof avoids sizeof()+padding giving 9 instead of the correct on-disk 8 */
> +#define EXT4_BASE_DIR_LEN offsetof(struct ext4_dir_entry_2, name)
[Severity: Medium]
Can lowering EXT4_BASE_DIR_LEN from 9 to 8 cause a 1-byte out-of-bounds
read of de->name[1] in is_fake_dir_entry()?
Previously, EXT4_BASE_DIR_LEN was 9 due to trailing struct padding, so
ext4_search_dir() stopped at (char *)de < dlimit - 9 and kept de->name[1]
in bounds. With EXT4_BASE_DIR_LEN now 8, a corrupted directory block with
an unaligned rec_len can place an entry at dlimit - 9 with name_len == 1.
Fast-commit replay (ext4_fc_replay_unlink() -> __ext4_unlink() ->
ext4_find_entry()) can look up a 1-byte "." entry from EXT4_FC_TAG_UNLINK,
pass de->name + de->name_len <= dlimit in ext4_search_dir(), and reach
__ext4_check_dir_entry(), which calls is_fake_dir_entry(de) first:
fs/ext4/dir.c:is_fake_dir_entry() {
if ((de->name_len > 0) && (de->name_len <= 2) && (de->name[0] == '.') &&
(de->name[1] == '.' || de->name[1] == '\0'))
return true;
}
When de sits at dlimit - 9 with name_len == 1 and name[0] == '.',
de->name[1] reads at dlimit, one byte past the directory block buffer,
triggering a KASAN out-of-bounds report or a page fault. Handling
name_len == 1 without reading de->name[1] avoids the out-of-bounds read.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203184938.422849-1-ablagodarenko@thelustrecollective.com?part=3
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v15 05/13] ext4: add dirdata format definitions and access helpers
2026-12-03 18:49 ` [PATCH v15 05/13] ext4: add dirdata format definitions and access helpers Artem Blagodarenko
@ 2026-12-03 19:59 ` sashiko-bot
0 siblings, 0 replies; 26+ messages in thread
From: sashiko-bot @ 2026-12-03 19:59 UTC (permalink / raw)
To: Artem Blagodarenko, linux-ext4; +Cc: tytso
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Low] ext4: ext4_dirent_get_data_len() comment omits included NUL byte
- [Low] ext4: unused ext4_dirdata_next() helper bypassed by dirdata
parsers
--
commit 215944fd5c4851a4433f0516167e85726be4f765
Author: Artem Blagodarenko <artem.blagodarenko@gmail.com>
ext4: add dirdata format definitions and access helpers
This commit defines the on-disk flags and structures for ext4 directory
entry extension data (dirdata) and adds ext4_dirdata_next() and
ext4_dirent_get_data_len() helpers in fs/ext4/ext4.h.
> diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
> index a7fc2c2c98e16..a75a8cedd52f6 100644
> --- a/fs/ext4/ext4.h
> +++ b/fs/ext4/ext4.h
[ ... ]
> @@ -4378,6 +4427,80 @@ static inline bool ext4_dir_entry_is_tail(struct ext4_dir_entry_2 *de)
> t->det_reserved_ft == EXT4_FT_DIR_CSUM;
> }
>
> +/*
> + * Advance to the next dirdata record header starting from @ddh.
> + *
> + * Uses READ_ONCE() on ddh_length to prevent the compiler from re-fetching this
> + * on-disk field and to guard against a concurrent write racing with any bounds
> + * check the caller performs on the returned pointer.
> + */
> +static inline struct ext4_dirent_data_header *
> +ext4_dirdata_next(struct ext4_dirent_data_header *ddh)
> +{
> + return (struct ext4_dirent_data_header *)
> + ((char *)ddh + READ_ONCE(ddh->ddh_length));
> +}
[Severity: Low]
Is ext4_dirdata_next() needed? Nothing in this commit or the rest of the
series calls it.
Even ext4_dirent_get_data_len() below bypasses it because callers must
read ddh->ddh_length once and validate it against zero and rec_len before
advancing, rather than re-fetching it in ext4_dirdata_next(). Consider
dropping ext4_dirdata_next() and updating the commit message.
> +
> +/*
> + * ext4_dirent_get_data_len() - Compute the total dirdata length for an entry.
> + * @de: directory entry
> + * @rec_len: the record length of the directory entry (decoded)
> + *
> + * Computes the length of optional data stored after the filename (and its
> + * implicit NUL terminator). Each extension is indicated by a bit in the
> + * high 4 bits of de->file_type; the first byte of each extension is its
> + * length (including that length byte itself).
> + *
> + * Returns 0 for tail entries and for entries with no dirdata.
> + */
[ ... ]
> + if (ddh_len == 0 || ddh_len > rec_len - offset)
> + return 0;
> + dlen += ddh_len + (dlen == 0);
[Severity: Low]
Should the comment above ext4_dirent_get_data_len() document that the
returned length includes the 1-byte NUL separator when dirdata is
present?
The comment describes only the optional data after the NUL terminator,
whereas dlen += ddh_len + (dlen == 0) adds 1 byte on the first record.
A caller that copies ext4_dirent_get_data_len() bytes starting after the
NUL byte would overread by 1 byte past rec_len.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203184938.422849-1-ablagodarenko@thelustrecollective.com?part=5
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v15 06/13] ext4: refactor dx_root to support variable dirent sizes
2026-12-03 18:49 ` [PATCH v15 06/13] ext4: refactor dx_root to support variable dirent sizes Artem Blagodarenko
@ 2026-12-03 20:21 ` sashiko-bot
0 siblings, 0 replies; 26+ messages in thread
From: sashiko-bot @ 2026-12-03 20:21 UTC (permalink / raw)
To: Artem Blagodarenko, linux-ext4; +Cc: tytso
Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] ext4: dx_get_dx_info() misses 4-byte alignment check on
rec_len
- [Medium] ext4: dx_get_dx_info() offset shifts after make_indexed_dir()
updates dotdot
- [Medium] ext4: ext4_dx_add_entry() mutates root before
dx_get_dx_info()
- [Low] ext4: unnecessary dir parameter and dx_get_dx_info() call in
dx_release()
- [Low] ext4: dx_probe() regresses ext4_warning_inode() to
ext4_warning() with %d
--
commit 8c7de95a1909ec95132a5dca0d865a06d32ff26e
Author: Artem Blagodarenko <artem.blagodarenko@gmail.com>
ext4: refactor dx_root to support variable dirent sizes
This commit splits struct dx_root_info out of struct dx_root and adds
dx_get_dx_info() and ext4_dir_entry_len() so htree root blocks can
locate dx_root_info when '.' and '..' have variable sizes.
> diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
> index 8fc39c91b6ef8..edbe033b623e6 100644
> --- a/fs/ext4/namei.c
> +++ b/fs/ext4/namei.c
> @@ -443,6 +443,46 @@ static inline int ext4_handle_dirty_dx_node(handle_t *handle,
[ ... ]
> +static struct dx_root_info *dx_get_dx_info(struct inode *dir, void *de_buf)
> +{
> + unsigned int blocksize = dir->i_sb->s_blocksize;
> + struct ext4_dir_entry_2 *de = de_buf;
> + unsigned int off, rlen, need;
> +
> + rlen = ext4_rec_len_from_disk(de->rec_len, blocksize);
> + if (rlen < EXT4_BASE_DIR_LEN || rlen + EXT4_BASE_DIR_LEN > blocksize ||
> + ext4_dir_entry_len(de, blocksize, NULL) > rlen)
> + return ERR_PTR(-EFSCORRUPTED);
> + off = rlen;
> +
> + de = (struct ext4_dir_entry_2 *)((char *)de_buf + off);
> + rlen = ext4_rec_len_from_disk(de->rec_len, blocksize);
> + if (rlen < EXT4_BASE_DIR_LEN || off + rlen > blocksize ||
> + ext4_dir_entry_len(de, blocksize, NULL) > rlen)
> + return ERR_PTR(-EFSCORRUPTED);
> + off += ext4_dir_entry_len(de, blocksize, NULL);
[Severity: Medium]
Can an unaligned '.' rec_len cause dx_get_dx_info() to return a
misaligned struct dx_root_info pointer?
dx_probe() passes the raw root block directly to dx_get_dx_info()
without running __ext4_check_dir_entry(). When blocksize is below
65536 and '.' has an unaligned rec_len (such as 13), rlen passes the
checks here, leaving '..', dx_root_info, and the dx_entry array
unaligned and causing alignment faults on strict-alignment
architectures.
Checking (rlen & EXT4_DIR_ROUND) would reject unaligned entries. Also,
ext4_dir_entry_len(de, blocksize, NULL) is called twice on '..' and
could be cached in a local variable.
[ ... ]
> @@ -640,23 +680,31 @@ dx_probe(struct ext4_filename *fname, struct inode *dir,
> if (IS_ERR(frame->bh))
> return (struct dx_frame *) frame->bh;
>
> - root = (struct dx_root *) frame->bh->b_data;
> - if (root->info.hash_version != DX_HASH_TEA &&
> - root->info.hash_version != DX_HASH_HALF_MD4 &&
> - root->info.hash_version != DX_HASH_LEGACY &&
> - root->info.hash_version != DX_HASH_SIPHASH) {
> - ext4_warning_inode(dir, "Unrecognised inode hash code %u",
> - root->info.hash_version);
> + info = dx_get_dx_info(dir, (struct ext4_dir_entry_2 *)frame->bh->b_data);
> + if (IS_ERR(info))
> + goto fail;
> + if (info->info_length != sizeof(struct dx_root_info)) {
> + ext4_warning_inode(dir, "corrupted htree: info_length %u",
> + info->info_length);
> + goto fail;
> + }
> + if (info->hash_version != DX_HASH_TEA &&
> + info->hash_version != DX_HASH_HALF_MD4 &&
> + info->hash_version != DX_HASH_LEGACY &&
> + info->hash_version != DX_HASH_SIPHASH) {
> + ext4_warning(dir->i_sb,
> + "Unrecognised inode hash code %d for directory #%llu",
> + info->hash_version, dir->i_ino);
> goto fail;
> }
[Severity: Low]
Was switching ext4_warning_inode() back to ext4_warning() with %d
intentional? This drops current->comm from the log message, formats
unsigned u8 hash_version with %d instead of %u, and is inconsistent
with the surrounding ext4_warning_inode() calls in dx_probe().
[ ... ]
> @@ -783,7 +829,7 @@ dx_probe(struct ext4_filename *fname, struct inode *dir,
> return ret_err;
> }
>
> -static void dx_release(struct dx_frame *frames)
> +static void dx_release(struct inode *dir, struct dx_frame *frames)
> {
> struct dx_root_info *info;
> int i;
> @@ -792,7 +838,18 @@ static void dx_release(struct dx_frame *frames)
> if (frames[0].bh == NULL)
> return;
>
> - info = &((struct dx_root *)frames[0].bh->b_data)->info;
> + info = dx_get_dx_info(dir, (struct ext4_dir_entry_2 *)frames[0].bh->b_data);
> + if (IS_ERR(info)) {
> + /* Cannot determine indirect_levels; release all frame buffers
> + * that may have been loaded to avoid leaking them. */
> + for (i = 0; i < EXT4_HTREE_LEVEL; i++) {
> + if (!frames[i].bh)
> + break;
> + brelse(frames[i].bh);
> + frames[i].bh = NULL;
> + }
> + return;
> + }
[Severity: Low]
Does dx_release() need the dir parameter or dx_get_dx_info() at all?
Every caller allocates frames[EXT4_HTREE_LEVEL] and zero-initializes it
via dx_probe() or make_indexed_dir(). Using the bounded loop over
EXT4_HTREE_LEVEL unconditionally avoids re-parsing the root block on
every htree release and avoids adding the dir argument to all callers.
[ ... ]
> @@ -2096,17 +2165,22 @@ static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname,
> return retval;
> }
>
> - root = (struct dx_root *) bh->b_data;
> - if (!ext4_check_dx_root(dir, root)) {
> + dot_de = (struct ext4_dir_entry_2 *)bh->b_data;
> + if (!ext4_check_dx_root(dir, dot_de, &dotdot_de, &de)) {
> brelse(bh);
> return -EFSCORRUPTED;
> }
>
> + /* Find room for the hashing info before the directory is changed */
> + dx_info = dx_get_dx_info(dir, dot_de);
> + if (IS_ERR(dx_info)) {
> + EXT4_ERROR_INODE(dir, "no room for htree root info");
> + brelse(bh);
> + return PTR_ERR(dx_info);
> + }
[ ... ]
> @@ -2137,24 +2211,26 @@ static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname,
> ext4_initialize_dirent_tail(bh2, blocksize);
>
> /* Initialize the root; the dot dirents already exist */
> - de = (struct ext4_dir_entry_2 *) (&root->dotdot);
> - de->rec_len = ext4_rec_len_to_disk(
> - blocksize - ext4_dir_rec_len(2, NULL), blocksize);
> - memset (&root->info, 0, sizeof(root->info));
> - root->info.info_length = sizeof(root->info);
> + dotdot_de->rec_len =
> + ext4_rec_len_to_disk(blocksize - le16_to_cpu(dot_de->rec_len),
> + blocksize);
[Severity: Medium]
Can expanding dotdot_de->rec_len here shift the offset that later
dx_get_dx_info() calls compute, corrupting the newly indexed directory?
ext4_dir_entry_len() calls ext4_dirent_get_data_len() even without the
dirdata feature. If '..' has an upper bit set in file_type and a byte
>= 2 at offset 11, ext4_dirent_get_data_len() returns 0 while rec_len
is 12, so make_indexed_dir() writes dx_root_info at offset 24.
After dotdot_de->rec_len is expanded to cover the rest of the block,
ext4_dirent_get_data_len() accepts that same byte and later
dx_get_dx_info() calls resolve dx_root_info inside the dx_entry array.
Guarding dirdata parsing on the dirdata feature would avoid this.
[ ... ]
> @@ -2472,11 +2548,17 @@ static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
> /* Set up root */
> dx_set_count(entries, 1);
> dx_set_block(entries + 0, newblock);
> - dxroot = (struct dx_root *)frames[0].bh->b_data;
> - dxroot->info.indirect_levels += 1;
> + info = dx_get_dx_info(dir, (struct ext4_dir_entry_2 *)
> + frames[0].bh->b_data);
> + if (IS_ERR(info)) {
> + err = PTR_ERR(info);
> + brelse(bh2);
> + goto journal_error;
> + }
> + info->indirect_levels += 1;
[Severity: Medium]
Can mutating entries in frames[0].bh before calling dx_get_dx_info()
leave the root block partially updated or shift info into entries[]?
Because '..' spans the rest of the root block and ext4_dir_entry_len()
parses dirdata unconditionally, stray upper bits in '..' file_type can
make ext4_dirent_get_data_len() read into entries[0].block. Writing
newblock can change a byte that was zero during dx_probe() to non-zero.
dx_get_dx_info() then either returns -EFSCORRUPTED after frames[0].bh
was modified and dir->i_size grown by ext4_append(), or returns a
shifted pointer so info->indirect_levels += 1 corrupts an index entry.
Calling dx_get_dx_info() before ext4_append() and before mutating
entries, as make_indexed_dir() does, would avoid both.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203184938.422849-1-ablagodarenko@thelustrecollective.com?part=6
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v15 07/13] ext4: size the htree root from where its dx_entry array starts
2026-12-03 18:49 ` [PATCH v15 07/13] ext4: size the htree root from where its dx_entry array starts Artem Blagodarenko
@ 2026-12-03 20:37 ` sashiko-bot
0 siblings, 0 replies; 26+ messages in thread
From: sashiko-bot @ 2026-12-03 20:37 UTC (permalink / raw)
To: Artem Blagodarenko, linux-ext4; +Cc: tytso
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] ext4: dx_get_dx_info() omits 4-byte alignment check on dot
rec_len
- [Low] ext4: get_dtype() masks upper file_type bits without checking
dirdata feature
--
commit 59d75705ec9b16d55848b1ae1482b360635d7868
Author: Artem Blagodarenko <artem.blagodarenko@gmail.com>
ext4: size the htree root from where its dx_entry array starts
This commit changes dx_root_limit() to take the byte offset of the
dx_entry array instead of the dx_root_info size, updates its callers and
KUnit tests accordingly, and masks dirdata flags out of file_type in
get_dtype().
> diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
> index aa45735a9bb79..32401150503eb 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];
> }
[Severity: Low]
Should this mask filetype with EXT4_FT_MASK only when
ext4_has_feature_dirdata(sb) is set? On non-dirdata filesystems, a
corrupted entry with upper file_type bits set (such as 0x11) previously
returned DT_UNKNOWN to readdir, and now reports a concrete DT_* type like
DT_REG.
Also, this readdir file-type change is unrelated to sizing the htree root
and would be cleaner in a separate patch.
[ ... ]
> diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
> index edbe033b623e6..78ee4936e6cc4 100644
> --- a/fs/ext4/namei.c
> +++ b/fs/ext4/namei.c
[ ... ]
> @@ -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;
> }
[Severity: Medium]
Can entries land on an unaligned offset here if dot's rec_len is not
4-byte aligned?
dx_probe() reads block 0 via ext4_read_dirblock(), which does not run
__ext4_check_dir_entry(), and dx_get_dx_info() does not check alignment
on dot's rec_len:
fs/ext4/namei.c:dx_get_dx_info() {
...
rlen = ext4_rec_len_from_disk(de->rec_len, blocksize);
if (rlen < EXT4_BASE_DIR_LEN || rlen + EXT4_BASE_DIR_LEN > blocksize ||
ext4_dir_entry_len(de, blocksize, NULL) > rlen)
return ERR_PTR(-EFSCORRUPTED);
off = rlen;
...
}
On a corrupted htree root block with an odd dot rec_len (such as 13),
dotdot, info, and entries all land at odd offsets. Dereferencing the
non-packed dx_root_info, dx_countlimit, and dx_entry fields then causes
alignment traps on strict-alignment architectures and
CONFIG_UBSAN_ALIGNMENT warnings. Checking rlen & EXT4_DIR_ROUND in
dx_get_dx_info() (and ext4_check_dx_root()) would reject unaligned
offsets.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203184938.422849-1-ablagodarenko@thelustrecollective.com?part=7
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v15 08/13] ext4: rename ext4_dir_rec_len() and clarify dirdata usage
2026-12-03 18:49 ` [PATCH v15 08/13] ext4: rename ext4_dir_rec_len() and clarify dirdata usage Artem Blagodarenko
@ 2026-12-03 21:05 ` sashiko-bot
0 siblings, 0 replies; 26+ messages in thread
From: sashiko-bot @ 2026-12-03 21:05 UTC (permalink / raw)
To: Artem Blagodarenko, linux-ext4; +Cc: tytso
Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] ext4: ext4_dir_entry_len() parses dirdata flags on
non-dirdata filesystems
- [Medium] ext4: do_split() leaves unzeroed slack parsed as dirdata in
add_dirent_to_buf()
- [Medium] ext4: dx_get_dx_info() omits 4-byte rec_len alignment check
- [Low] ext4: commit message omits API and behavioral changes in rename
patch
- [Low] ext4: redundant ext4_dir_entry_len() walk in
__ext4_check_dir_entry()
--
commit 2d5f99bd8296e9ea507c7610391e9c358e49b150
Author: Artem Blagodarenko <artem.blagodarenko@gmail.com>
> ext4: rename ext4_dir_rec_len() and clarify dirdata usage
>
> Rename ext4_dir_rec_len() to ext4_dirent_rec_len() to better
> reflect that it computes the record length for a directory
> entry based on the provided name length.
>
> Update the comment to clarify handling of dirdata-enabled
> directories and document the use of ext4_dir_entry_len()
> when dirdata is present.
>
> ext4_dir_entry_len() reads the dirdata of an entry, so the length of
> the entry that do_split() hands to add_dirent_to_buf() now depends on
> on-disk data. add_dirent_to_buf() checks that this entry has room for
> the new one before anything is written, instead of overrunning the
> block when the directory is corrupted.
>
> Signed-off-by: Artem Blagodarenko <artem.blagodarenko@gmail.com>
> Assisted-by: Claude:claude-opus-5-5
[Severity: Low]
Should the commit message also describe the API and behavioral changes
bundled into this patch? In addition to the rename and the check in
add_dirent_to_buf(), the diff widens ext4_dirent_rec_len()'s name_len
parameter from __u8 to unsigned int (avoiding 8-bit truncation when
name_len + dirdata >= 256), adds a dlen parameter to exported
ext4_find_dest_de(), and switches __ext4_check_dir_entry(),
dx_move_dirents(), dx_pack_dirents(), ext4_find_dest_de(), and
ext4_insert_dentry() from ext4_dir_rec_len() to ext4_dir_entry_len().
Also, the new comment in ext4_add_dirent_to_inline() says the dirdata
types are defined in the next patch, though commit "ext4: add dirdata
format definitions and access helpers" already defined them.
> diff --git a/fs/ext4/dir.c b/fs/ext4/dir.c
> index 43f4f2d451aa8..06d8d2eb52ea4 100644
> --- a/fs/ext4/dir.c
> +++ b/fs/ext4/dir.c
> @@ -89,16 +89,15 @@ int __ext4_check_dir_entry(const char *function, unsigned int line,
> bool fake = is_fake_dir_entry(de);
> bool has_csum = ext4_has_feature_metadata_csum(dir->i_sb);
>
> - if (unlikely(rlen < ext4_dir_rec_len(1, fake ? NULL : dir)))
> + if (unlikely(rlen < ext4_dirent_rec_len(1, fake ? NULL : dir)))
> error_msg = "rec_len is smaller than minimal";
> else if (unlikely(rlen % 4 != 0))
> error_msg = "rec_len % 4 != 0";
> - else if (unlikely(rlen < ext4_dir_rec_len(de->name_len,
> - fake ? NULL : dir)))
> - error_msg = "rec_len is too small for name_len";
> else if (unlikely(next_offset > size))
> error_msg = "directory entry overrun";
> - else if (unlikely(next_offset > size - ext4_dir_rec_len(1,
> + else if (unlikely(rlen < ext4_dir_entry_len(de, size, fake ? NULL : dir)))
> + error_msg = "rec_len is too small for name_len";
> + else if (unlikely(next_offset > size - ext4_dirent_rec_len(1,
> has_csum ? NULL : dir) &&
> next_offset != size))
> error_msg = "directory entry too close to block end";
[Severity: Low]
Does calling ext4_dir_entry_len() here validate anything beyond
ext4_dirent_rec_len(de->name_len, fake ? NULL : dir)?
By this point rlen % 4 == 0 and next_offset <= size are already verified.
Inside ext4_dirent_get_data_len(), any non-zero dirdata length satisfies
EXT4_BASE_DIR_LEN + de->name_len + dirdata <= rec_len (and out-of-bounds
records return 0), so rounding up to 4 bytes can never exceed the already
4-byte-aligned rlen. Keeping ext4_dirent_rec_len() here avoids
re-decoding de->rec_len and running the READ_ONCE() dirdata walk on every
entry check (which ext4_find_dest_de() then repeats immediately).
> diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
> index 32401150503eb..e34d91cd63ece 100644
> --- a/fs/ext4/ext4.h
> +++ b/fs/ext4/ext4.h
[ ... ]
> @@ -4529,8 +4535,8 @@ static inline unsigned int ext4_dir_entry_len(struct ext4_dir_entry_2 *de,
> unsigned int rec_len = ext4_rec_len_from_disk(de->rec_len, blocksize);
> unsigned int dirdata = ext4_dirent_get_data_len(de, rec_len);
>
> - return max(ext4_dir_rec_len(de->name_len + dirdata, NULL),
> - ext4_dir_rec_len(de->name_len, dir));
> + return max(ext4_dirent_rec_len(de->name_len + dirdata, NULL),
> + ext4_dirent_rec_len(de->name_len, dir));
> }
[Severity: Medium]
Should ext4_dir_entry_len() skip ext4_dirent_get_data_len() when
EXT4_FEATURE_INCOMPAT_DIRDATA is not enabled?
On a non-dirdata filesystem, an entry with upper file_type bits set and a
padding byte after the name larger than its current rec_len gets
dirdata == 0 from ext4_dirent_get_data_len(), so it passes
__ext4_check_dir_entry() and dx_move_dirents() / dx_pack_dirents() copy
only its original length. Once do_split() expands the last entry's
rec_len to the rest of the half-block, subsequent ext4_dir_entry_len()
calls see that byte as in-bounds and jump the entry length, annexing
uncopied or stale block bytes into the entry (or failing in
add_dirent_to_buf() with -EFSCORRUPTED).
Passing the superblock to ext4_dir_entry_len() and gating
ext4_dirent_get_data_len() on ext4_has_feature_dirdata(sb) would keep
non-dirdata filesystems from interpreting those bits.
[Severity: Medium]
Can dx_get_dx_info() (added in commit "ext4: refactor dx_root to support
variable dirent sizes") return an unaligned pointer into the htree root
block?
fs/ext4/namei.c:dx_get_dx_info() {
...
rlen = ext4_rec_len_from_disk(de->rec_len, blocksize);
if (rlen < EXT4_BASE_DIR_LEN || rlen + EXT4_BASE_DIR_LEN > blocksize ||
ext4_dir_entry_len(de, blocksize, NULL) > rlen)
return ERR_PTR(-EFSCORRUPTED);
off = rlen;
...
}
For blocksize < 65536, ext4_rec_len_from_disk() returns the raw 16-bit
value without masking the low two bits, and dx_get_dx_info() does not
check rlen % 4 != 0. A '.' entry with an unaligned rec_len (such as 13
with name_len 1) passes ext4_dir_entry_len(de, blocksize, NULL) <= rlen,
shifting '..', dx_root_info, and the dx_entry array in dx_probe() to
unaligned offsets and causing unaligned 16-bit and 32-bit accesses on
non-packed structs.
> diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
> index 78ee4936e6cc4..6050dbfd9f786 100644
> --- a/fs/ext4/namei.c
> +++ b/fs/ext4/namei.c
[ ... ]
> @@ -1797,7 +1797,7 @@ static struct ext4_dir_entry_2 *dx_pack_dirents(struct inode *dir, char *base,
> while ((char*)de < base + blocksize) {
> next = ext4_next_entry(de, blocksize);
> if (de->inode && de->name_len) {
> - rec_len = ext4_dir_rec_len(de->name_len, dir);
> + rec_len = ext4_dir_entry_len(de, blocksize, dir);
> if (de > to)
> memmove(to, de, rec_len);
> to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
[ ... ]
> @@ -2035,9 +2036,29 @@ static int add_dirent_to_buf(handle_t *handle, struct ext4_filename *fname,
>
> if (!de) {
> err = ext4_find_dest_de(dir, bh, bh->b_data,
> - blocksize - csum_size, fname, &de);
> + blocksize - csum_size, fname, &de, 0);
> if (err)
> return err;
> + } else {
> + /*
> + * do_split() hands over the last entry of a half. Its length
> + * comes from on-disk dirdata, so check that the new entry fits
> + * before it is written. do_split() has already packed this
> + * half and left it to us to dirty, so dirty it on failure too.
> + */
> + unsigned int nlen = de->inode ?
> + ext4_dir_entry_len(de, blocksize, dir) : 0;
> + unsigned int rlen = ext4_rec_len_from_disk(de->rec_len,
> + blocksize);
> +
> + if (nlen > rlen ||
> + rlen - nlen < ext4_dirent_rec_len(fname_len(fname), dir)) {
> + EXT4_ERROR_INODE(dir,
> + "no room for new entry in split block %llu",
> + (unsigned long long)bh->b_blocknr);
> + ext4_handle_dirty_dirblock(handle, dir, bh);
> + return -EFSCORRUPTED;
> + }
> }
> BUFFER_TRACE(bh, "get_write_access");
> err = ext4_journal_get_write_access(handle, dir->i_sb, bh,
[Severity: Medium]
Can add_dirent_to_buf() and ext4_insert_dentry() parse unzeroed stale
bytes left after dx_pack_dirents() as dirdata?
dx_pack_dirents() compacts entries in the first half without zeroing the
trailing slack space, and do_split() then extends the last entry's
rec_len to blocksize - csum_size. If that entry has stray upper
file_type bits whose dirdata was previously ignored because its offset or
length exceeded the old rec_len, re-evaluating ext4_dir_entry_len() over
the expanded rec_len reads the unzeroed slack bytes as ddh_length.
That either makes add_dirent_to_buf() falsely report EXT4_ERROR_INODE and
return -EFSCORRUPTED, or causes ext4_insert_dentry() to split at the
bogus nlen and permanently attach stale block bytes to the entry as
phantom dirdata.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203184938.422849-1-ablagodarenko@thelustrecollective.com?part=8
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v15 09/13] ext4: dirdata feature
2026-12-03 18:49 ` [PATCH v15 09/13] ext4: dirdata feature Artem Blagodarenko
@ 2026-12-03 21:37 ` sashiko-bot
0 siblings, 0 replies; 26+ messages in thread
From: sashiko-bot @ 2026-12-03 21:37 UTC (permalink / raw)
To: Artem Blagodarenko, linux-ext4; +Cc: tytso
Thank you for your contribution! Sashiko AI review found 8 potential
issues to consider:
- [Medium] ext4: ext4_dir_entry_len() parses dirdata flags without
INCOMPAT_DIRDATA
- [Medium] ext4: allow mounting INCOMPAT_DIRDATA without
INCOMPAT_FILETYPE
- [Medium] ext4: missing rlen alignment and dirdata checks in
get_dx_countlimit()
- [Medium] ext4: ext4_feature_set_ok() allows dirdata without filetype
feature
- [Medium] ext4: enable EXT4_FEATURE_INCOMPAT_DIRDATA before dirdata
handlers exist
- [Low] ext4: misplaced bounds check and unchecked rec_len in
dx_show_leaf()
- [Low] ext4: unused data1/data2 parameters in ext4_init_new_dir_data()
- [Low] ext4: commit message omits feature enablement, LUFID, and API
changes
--
commit 9524cb5f9f3b3666d39ad56c5cf72650999c906d
Author: Artem Blagodarenko <artem.blagodarenko@gmail.com>
> ext4: dirdata feature
>
> When fscrypt and casefold are enabled together for a directory,
> all ext4_dir_entry[_2] in that directory store a n 8-byte hash
> of the filename after 'name' between 'name_len' and 'rec_len'.
>
> However, there is no clear indication there is important data
> stored in these bytes, which are only for padding and alignment
> in other directory entries. This adds complexity to code handling
> the on-disk directory entries, and there is no provision for other
> metadata to be stored in each dir entry after 'name'.
>
> The dirdata feature adds a mechanism to store multiple metadata
> entries in each dir entry after 'name' (including the fchash).
> The unused high 4 bits of 'file_type' are used to indicate whether
> additional data fields are stored after 'name'. If a bit is set,
> the corresponding dirdata record is present, starting after a NUL
> filename terminator. If present, a record starts with a 1-byte
> length (including the length byte itself) and the data immediately
> follows the length byte without any alignment.
>
> This allows up to four different dirdata records to be stored in
> each entry, and allows unhandled record bytes to be skipped without
> having to process the contents, providing forward compatibility.
>
> If and when the fourth and last dirdata record is needed, it is
> recommended to further subdivide it into sub-records, with
> the first byte being the total length, and then there being a
> second byte that gives the sub-record length, etc. as long as
> the total record length is less than 255 bytes. However, this
> would not affect compatibility with the current code since the
> record length would allow it to be skipped without processing.
>
> Signed-off-by: Pravin Shelar <pravin.shelar@sun.com>
> Signed-off-by: Artem Blagodarenko <artem.blagodarenko@gmail.com>
> Assisted-by: Claude:claude-opus-5-5
[Severity: Low]
This isn't a bug, but the commit message only describes the generic on-disk
dirdata format and fchash motivation, which were already introduced in
commit "ext4: add dirdata format definitions and access helpers". It omits
the actual changes made here: enabling EXT4_FEATURE_INCOMPAT_DIRDATA in
EXT4_FEATURE_INCOMPAT_SUPP and sysfs, adding ext4_dirent_data_corrupt() and
variable-size dot/dotdot handling in htree helpers, interpreting
dentry->d_fsdata as struct ext4_dentry_param via ext4_dentry_get_fid(),
marking fast-commit ineligible via EXT4_FC_REASON_DIRDATA, and introducing
ext4_insert_dentry_data() and ext4_init_new_dir_data().
[ ... ]
> diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
> index e34d91cd63ece..98014749e5d43 100644
> --- a/fs/ext4/ext4.h
> +++ b/fs/ext4/ext4.h
> @@ -2385,6 +2385,7 @@ EXT4_FEATURE_INCOMPAT_FUNCS(casefold, CASEFOLD)
> EXT4_FEATURE_INCOMPAT_FLEX_BG| \
> EXT4_FEATURE_INCOMPAT_EA_INODE| \
> EXT4_FEATURE_INCOMPAT_MMP | \
> + EXT4_FEATURE_INCOMPAT_DIRDATA | \
> EXT4_FEATURE_INCOMPAT_INLINE_DATA | \
> EXT4_FEATURE_INCOMPAT_ENCRYPT | \
> EXT4_FEATURE_INCOMPAT_CASEFOLD | \
[Severity: Medium]
Does adding EXT4_FEATURE_INCOMPAT_DIRDATA here allow mounting a dirdata
filesystem without EXT4_FEATURE_INCOMPAT_FILETYPE?
Dirdata stores its presence flags in the upper 4 bits of de->file_type,
which is the high byte of a 16-bit name_len when FILETYPE is disabled, yet
ext4_feature_set_ok() does not reject that combination. As a result,
ext4_dirdata_set() (added later in the series) unconditionally sets dirdata
bits in de->file_type while ext4_setent() gates file_type and LUFID updates
on ext4_has_feature_filetype(), skipping LUFID updates on overwriting
renames and leaving the overwritten inode's LUFID attached to the renamed
entry.
[Severity: Medium]
Can mounting EXT4_FEATURE_INCOMPAT_DIRDATA without
EXT4_FEATURE_INCOMPAT_FILETYPE also cause newly allocated directory blocks
to fail validation as corrupted?
Because ext4_feature_set_ok() permits this combination and
ext4_set_de_type() is a no-op without FILETYPE, ext4_init_dirblock() leaves
stale bytes in de->file_type for '.' and '..' when ext4_append() reuses an
already-uptodate buffer. ext4_dirent_data_corrupt() then interprets
non-zero upper bits in those stale bytes as dirdata flags and reports
corruption. Rejecting dirdata without filetype in ext4_feature_set_ok()
would prevent both issues.
[ ... ]
> @@ -4509,6 +4538,28 @@ static inline int ext4_dirent_get_data_len(struct ext4_dir_entry_2 *de,
> return dlen;
> }
>
> +/*
> + * ext4_dirent_data_corrupt() - check the dirdata records of an entry
> + * @sb: super block
> + * @de: directory entry
> + * @rec_len: the record length of the directory entry (decoded)
> + *
> + * On a dirdata filesystem every record flagged in the high bits of
> + * de->file_type must fit inside the entry. ext4_dirent_get_data_len()
> + * returns 0 when one does not, which would make the length of the entry
> + * depend on its current rec_len; callers treat that as corruption
> + * instead. Without the feature the flags are not interpreted.
> + */
> +static inline bool ext4_dirent_data_corrupt(struct super_block *sb,
> + struct ext4_dir_entry_2 *de,
> + unsigned int rec_len)
> +{
> + if (!ext4_has_feature_dirdata(sb) || ext4_dir_entry_is_tail(de) ||
> + !(de->file_type & ~EXT4_FT_MASK))
> + return false;
> + return ext4_dirent_get_data_len(de, rec_len) == 0;
> +}
[Severity: Medium]
Does ext4_dir_entry_len() still interpret dirdata flags when
EXT4_FEATURE_INCOMPAT_DIRDATA is disabled, contradicting
ext4_dirent_data_corrupt()?
fs/ext4/ext4.h:ext4_dir_entry_len() {
unsigned int rec_len = ext4_rec_len_from_disk(de->rec_len, blocksize);
unsigned int dirdata = ext4_dirent_get_data_len(de, rec_len);
return max(ext4_dirent_rec_len(de->name_len + dirdata, NULL),
ext4_dirent_rec_len(de->name_len, dir));
}
On a non-dirdata filesystem, if a 1-block directory has a '..' entry with
rec_len == 12, a non-zero upper nibble in file_type, and a non-zero padding
byte (for example 4), make_indexed_dir() passes validation because
ext4_dirent_get_data_len(dotdot_de, 12) returns 0, writes dx_root_info at
offset 24, and expands dotdot_de->rec_len to blocksize - 12. Subsequent
calls to get_dx_countlimit() and dx_get_dx_info() then see the expanded
rec_len, compute a larger dotdot_rec_len (16), and read dx_root_info at
offset 28 instead of 24, failing checksum generation and breaking the
directory with ERR_BAD_DX_DIR. Gating ext4_dirent_get_data_len() in
ext4_dir_entry_len() on ext4_has_feature_dirdata() would avoid this.
[ ... ]
> diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
> index 6050dbfd9f786..12ffd34cf46d0 100644
> --- a/fs/ext4/namei.c
> +++ b/fs/ext4/namei.c
> @@ -329,23 +329,33 @@ static struct dx_countlimit *get_dx_countlimit(struct inode *inode,
> {
> struct ext4_dir_entry_2 *de;
> struct dx_root_info *root;
> - int count_offset;
> + int count_offset, dotdot_rec_len;
> int blocksize = EXT4_BLOCK_SIZE(inode->i_sb);
> unsigned int rlen = ext4_rec_len_from_disk(dirent->rec_len, blocksize);
>
> - if (rlen == blocksize)
> + if (rlen == blocksize) {
> count_offset = sizeof(struct dx_node);
> - else if (rlen == 12) {
> - de = (struct ext4_dir_entry_2 *)(((void *)dirent) + 12);
> - if (ext4_rec_len_from_disk(de->rec_len, blocksize) != blocksize - 12)
> + } else {
> + if (rlen < EXT4_BASE_DIR_LEN ||
> + rlen + EXT4_BASE_DIR_LEN > blocksize)
> + return NULL;
> + de = (struct ext4_dir_entry_2 *)(((char *)dirent) + rlen);
> + if (ext4_rec_len_from_disk(de->rec_len, blocksize) !=
> + (unsigned int)(blocksize - rlen))
> + return NULL;
> + /* de->rec_len covers whole dx_root block, calculate actual length.
> + * This is the '..' entry, which never carries the casefold+fscrypt
> + * hash, so pass NULL for dir regardless of the directory's flags */
> + dotdot_rec_len = ext4_dir_entry_len(de, blocksize, NULL);
> + if (rlen + dotdot_rec_len + sizeof(struct dx_root_info) +
> + sizeof(struct dx_countlimit) > blocksize)
> return NULL;
> - root = (struct dx_root_info *)(((void *)de + 12));
> + root = (struct dx_root_info *)(((char *)de + dotdot_rec_len));
> if (root->reserved_zero ||
> root->info_length != sizeof(struct dx_root_info))
> return NULL;
> - count_offset = 32;
> - } else
> - return NULL;
> + count_offset = root->info_length + rlen + dotdot_rec_len;
> + }
>
> if (offset)
> *offset = count_offset;
[Severity: Medium]
Can an unaligned dot rec_len in get_dx_countlimit() cause unaligned
accesses to dx_root_info, dx_countlimit, dx_entry, and dx_tail?
Previously get_dx_countlimit() only accepted rlen == blocksize or
rlen == 12, guaranteeing 4-byte alignment. Now a corrupted root block with
an unaligned rlen (such as 9) passes and produces unaligned pointers (and
dx_get_dx_info() is missing the same (rlen & EXT4_DIR_ROUND) check).
Unlike dx_get_dx_info(), get_dx_countlimit() also omits
ext4_dirent_data_corrupt() on '.' and '..' and the
ext4_dir_entry_len(dirent, blocksize, NULL) > rlen check, so corrupt
dirdata causes ext4_dirent_get_data_len() to return 0 and silently compute
the wrong count_offset instead of returning NULL.
[ ... ]
> @@ -586,7 +598,9 @@ static struct stats dx_show_leaf(struct inode *dir,
> (unsigned) ((char *) de - base));
> #endif
> }
> - space += ext4_dirent_rec_len(de->name_len, dir);
> + if ((char *)de + EXT4_BASE_DIR_LEN > base + size)
> + break;
> + space += ext4_dir_entry_len(de, size, dir);
> names++;
> }
> de = ext4_next_entry(de, size);
[Severity: Low]
Is this bounds check in dx_show_leaf() too late and incomplete for a
corrupted leaf block?
In DX_DEBUG builds, the loop already dereferences de->inode (and, when
show_names is set, de->name_len, de->name, and EXT4_DIRENT_HASH(de)) before
reaching this check. When de->inode == 0, the check is bypassed before
ext4_next_entry(de, size) reads de->rec_len, and ext4_dir_entry_len() is
also called without verifying (char *)de + rec_len <= base + size, allowing
an out-of-bounds read past the leaf buffer.
[ ... ]
> @@ -2930,8 +2961,9 @@ int ext4_init_dirblock(handle_t *handle, struct inode *inode,
> }
> EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_init_dirblock);
>
> -int ext4_init_new_dir(handle_t *handle, struct inode *dir,
> - struct inode *inode)
> +int ext4_init_new_dir_data(handle_t *handle, struct inode *dir,
> + struct inode *inode,
> + const void *data1, const void *data2)
> {
> struct buffer_head *dir_block = NULL;
> ext4_lblk_t block = 0;
[Severity: Low]
Are data1 and data2 in ext4_init_new_dir_data() unused? Neither parameter
is referenced in the function body, and even at the end of the series the
function still ignores them with no direct in-tree callers (only the
ext4_init_new_dir() wrapper passing NULL, NULL); similarly, __ext4_link()
forwards dentry->d_fsdata even though no in-tree link caller sets it.
[ ... ]
> diff --git a/fs/ext4/sysfs.c b/fs/ext4/sysfs.c
> index faa76f0210991..a433418a8182b 100644
> --- a/fs/ext4/sysfs.c
> +++ b/fs/ext4/sysfs.c
> @@ -362,6 +362,7 @@ EXT4_ATTR_FEATURE(verity);
> #endif
> EXT4_ATTR_FEATURE(metadata_csum_seed);
> EXT4_ATTR_FEATURE(fast_commit);
> +EXT4_ATTR_FEATURE(dirdata);
> #if IS_ENABLED(CONFIG_UNICODE) && defined(CONFIG_FS_ENCRYPTION)
> EXT4_ATTR_FEATURE(encrypted_casefold);
> #endif
[Severity: Medium]
Does advertising dirdata in sysfs and adding EXT4_FEATURE_INCOMPAT_DIRDATA
to EXT4_FEATURE_INCOMPAT_SUPP in this commit allow read-write mounts of
dirdata filesystems before the dirdata read, write, and rename handlers
exist?
At this commit, ext4_insert_dentry_data() ignores its data parameter and
writes casefold+fscrypt hashes at the legacy fixed offset, hash readers
such as ext4_match() and ext4_readdir() still read the legacy offset, and
ext4_setent() copies de->file_type verbatim on rename without adjusting
dirdata payloads, corrupting entries on disk because
__ext4_check_dir_entry() already enforces ext4_dirent_data_corrupt().
These handlers are added later by commit "ext4: add dirdata LUFID support
for directory entry rename" and commit "ext4: add dirdata set/get helpers",
so deferring EXT4_FEATURE_INCOMPAT_SUPP and sysfs enablement until after
those patches would keep the series bisectable.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203184938.422849-1-ablagodarenko@thelustrecollective.com?part=9
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v15 10/13] ext4: add dirdata LUFID support for directory entry rename
2026-12-03 18:49 ` [PATCH v15 10/13] ext4: add dirdata LUFID support for directory entry rename Artem Blagodarenko
@ 2026-12-03 22:02 ` sashiko-bot
0 siblings, 0 replies; 26+ messages in thread
From: sashiko-bot @ 2026-12-03 22:02 UTC (permalink / raw)
To: Artem Blagodarenko, linux-ext4; +Cc: tytso
Thank you for your contribution! Sashiko AI review found 8 potential
issues to consider:
- [Medium] ext4: unnecessary kmalloc() and copy of LUFID wrapper in
ext4_rename()
- [Medium] ext4: TOCTOU double-fetch of dirent fields in rename LUFID
helpers
- [Medium] ext4: stale dot-dot LUFID left by ext4_rename_dir_finish() on
dir move
- [Medium] ext4: ext4_setent() ignores ext4_dirent_add_lufid() failure
and drops LUFID
- [Low] ext4: inconsistent EXT4_FEATURE_INCOMPAT_FILETYPE gating in
rename dirdata
- [Low] ext4: dead EXT4_DIRENT_LUFID masking in ext4_rename() and
ext4_cross_rename()
- [Low] ext4: undocumented __ext4_link() cleanup mixed into rename patch
- [Low] ext4: explicit GFP_NOFS allocations inside active transaction in
rename
--
commit e5f6d4f2d5e4add0a5ae9ac53340b2594e4b354f
Author: Artem Blagodarenko <artem.blagodarenko@gmail.com>
ext4: add dirdata LUFID support for directory entry rename
This commit extends ext4_setent() to replace or drop dirdata LUFID records
while preserving CFHASH, and snapshots source LUFIDs in ext4_rename() and
ext4_cross_rename() so they travel with the renamed inode.
> diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
> index 12ffd34cf46d0..8a1c292ddd2f4 100644
> --- a/fs/ext4/namei.c
> +++ b/fs/ext4/namei.c
> @@ -3467,11 +3467,6 @@ int __ext4_link(struct inode *dir, struct inode *inode,
> if (IS_DIRSYNC(dir))
> ext4_handle_sync(handle);
>
> - /* Fast-commit does not record dirdata in its journal format; force a
> - * full journal commit so the LUFID survives crash+replay. */
> - if (ext4_dentry_get_fid(dir->i_sb, dentry ? dentry->d_fsdata : NULL))
> - ext4_fc_mark_ineligible(dir->i_sb, EXT4_FC_REASON_DIRDATA, handle);
> -
> inode_set_ctime_current(inode);
[Severity: Low]
This removes the duplicate ext4_fc_mark_ineligible() check from
__ext4_link() that was added in commit "ext4: dirdata feature", but neither
the subject nor the changelog mentions it. Should this cleanup be folded
back into that earlier commit or noted in the commit message?
[ ... ]
> @@ -3645,8 +3640,81 @@ static int ext4_rename_dir_finish(handle_t *handle, struct ext4_renament *ent,
> return 0;
> }
[Severity: Medium]
Does ext4_rename_dir_finish() leave a stale LUFID on ".." when a directory
is moved to a different parent?
In ext4_rename() and ext4_cross_rename(), ext4_rename_dir_finish() updates
ent->parent_de->inode to the new parent's inode number without updating or
clearing EXT4_DIRENT_LUFID on ent->parent_de. On a dirdata filesystem with
pre-existing ".." LUFIDs (such as an ldiskfs volume), the old parent's LUFID
remains attached to the new parent's inode number. Handling HTree
directories here will also need to account for dx_root_info sitting
immediately after ".." if its length changes.
> +static void ext4_dirent_drop_lufid(struct ext4_dir_entry_2 *de,
> + unsigned int rec_len)
> +{
> + unsigned int off = EXT4_BASE_DIR_LEN + de->name_len + 1;
> + unsigned int dlen, end, llen;
> +
> + if (!(de->file_type & EXT4_DIRENT_LUFID))
> + return;
> + dlen = ext4_dirent_get_data_len(de, rec_len);
> + if (!dlen) {
> + de->file_type &= EXT4_FT_MASK;
> + return;
> + }
> + /* ext4_dirent_get_data_len() checked every record against rec_len */
> + llen = *((u8 *)de + off);
> + end = EXT4_BASE_DIR_LEN + de->name_len + dlen;
> + memmove((char *)de + off, (char *)de + off + llen, end - off - llen);
> + de->file_type &= ~EXT4_DIRENT_LUFID;
> +}
[ ... ]
> +static bool ext4_dirent_add_lufid(struct inode *dir,
> + struct ext4_dir_entry_2 *de,
> + unsigned int rec_len,
> + const struct ext4_dirent_fid *fid)
> +{
> + unsigned int off = EXT4_BASE_DIR_LEN + de->name_len + 1;
> + unsigned int flen = fid->df_header.ddh_length;
> + unsigned int dlen = ext4_dirent_get_data_len(de, rec_len);
> + unsigned int end;
> +
> + if ((de->file_type & ~EXT4_FT_MASK) && !dlen)
> + return false;
> + /* without a CFHASH record the hash sits right after the name */
> + if (ext4_hash_in_dirent(dir) && !(de->file_type & EXT4_DIRENT_CFHASH))
> + return false;
> + end = dlen ? EXT4_BASE_DIR_LEN + de->name_len + dlen : off;
> + if (end + flen > rec_len)
> + return false;
> + memmove((char *)de + off + flen, (char *)de + off, end - off);
> + *((char *)de + EXT4_BASE_DIR_LEN + de->name_len) = 0;
> + memcpy((char *)de + off, fid, flen);
> + de->file_type |= EXT4_DIRENT_LUFID;
> + return true;
> +}
[ ... ]
> @@ -3656,8 +3724,33 @@ static int ext4_setent(handle_t *handle, struct ext4_renament *ent,
> if (retval)
> return retval;
> ent->de->inode = cpu_to_le32(ino);
> - if (ext4_has_feature_filetype(ent->dir->i_sb))
> - ent->de->file_type = file_type;
> + if (ext4_has_feature_filetype(ent->dir->i_sb)) {
> + if (ext4_has_feature_dirdata(ent->dir->i_sb)) {
> + unsigned int rec_len =
> + ext4_rec_len_from_disk(ent->de->rec_len,
> + ent->dir->i_sb->s_blocksize);
> + unsigned int off =
> + EXT4_BASE_DIR_LEN + ent->de->name_len + 1;
> + struct ext4_dirent_fid *fid =
> + (struct ext4_dirent_fid *)((char *)ent->de + off);
> +
> + if (src_fid &&
> + (ent->de->file_type & EXT4_DIRENT_LUFID) &&
> + ext4_dirent_get_data_len(ent->de, rec_len) &&
> + fid->df_header.ddh_length ==
> + src_fid->df_header.ddh_length) {
> + memcpy(fid, src_fid,
> + src_fid->df_header.ddh_length);
[Severity: Medium]
Can concurrent writes to the directory buffer cause an unsigned underflow
or out-of-bounds access in these three helpers?
ext4_dirent_get_data_len() and ext4_lufid_snapshot() both use READ_ONCE()
to guard against the directory buffer changing under them, but these
helpers re-read on-disk fields after validation:
- In ext4_dirent_drop_lufid(), llen and de->name_len are re-read after
ext4_dirent_get_data_len(). If the on-disk LUFID length grows past
end - off, end - off - llen underflows unsigned int to ~4 GiB in
memmove().
- In ext4_dirent_add_lufid(), de->name_len is read separately for off, end,
and the NUL write, allowing end < off (underflowing end - off in
memmove()) or a NUL write past rec_len.
- In ext4_setent(), fid->df_header.ddh_length is re-fetched after
ext4_dirent_get_data_len() without checking
off + src_fid->df_header.ddh_length <= rec_len, allowing memcpy() to
write past rec_len.
A concurrent writer to the buffer (such as a corrupted image with
cross-linked directory blocks or writes to a mounted block device) could
corrupt kernel memory. Reading name_len and the LUFID length once with
READ_ONCE() and bounds-checking against rec_len before memmove()/memcpy()
would close the race.
> + } else {
> + ext4_dirent_drop_lufid(ent->de, rec_len);
> + if (src_fid)
> + ext4_dirent_add_lufid(ent->dir, ent->de,
> + rec_len, src_fid);
> + }
[Severity: Medium]
Should ext4_setent() silently drop the source LUFID when
ext4_dirent_add_lufid() returns false?
Because ext4_insert_dentry_data() trims rec_len to ext4_dir_entry_len()
when another entry is inserted after it, any non-tail entry created without
a LUFID has at most 3 bytes of padding and cannot fit a 17-byte or larger
LUFID in place. When ext4_rename() overwrites such an entry, ext4_setent()
ignores the false return and ext4_rename_delete() then removes old.de,
permanently losing the LUFID without returning an error (the same happens
in ext4_cross_rename() and on RENAME_WHITEOUT rollback after compaction).
Since ext4_rename() already reserves entry-addition credits, could it
delete and re-add the destination entry when the LUFID does not fit in
place?
[ ... ]
> @@ -3793,6 +3887,69 @@ static struct inode *ext4_whiteout_for_rename(const struct mnt_idmap *idmap,
[ ... ]
> + if (dlen <= sizeof(snap->buf)) {
> + memcpy(snap->buf, disk_fid, dlen);
> + snap->fid = (struct ext4_dirent_fid *)snap->buf;
> + } else {
> + snap->fid = kmemdup(disk_fid, dlen, GFP_NOFS);
[Severity: Low]
Can this kmemdup() (and the kmalloc() in ext4_rename()) use GFP_KERNEL
instead of GFP_NOFS?
Both run inside an active transaction handle, where start_this_handle() and
ext4_get_nojournal() already set memalloc_nofs_save() for the lifetime of
the handle. Also, because old.de is already looked up before starting the
transaction, snapshotting before ext4_whiteout_for_rename() would avoid
creating and then orphaning a whiteout inode on -ENOMEM.
[ ... ]
> @@ -3926,29 +4086,67 @@ static int ext4_rename(const struct mnt_idmap *idmap, struct inode *old_dir,
[ ... ]
> + if (ext4_has_feature_dirdata(old.dir->i_sb)) {
> + /* Snapshot before whiteout's ext4_setent() clears the LUFID
> + * flag in old.de; the snapshot is also needed for the !new.bh
> + * path below where we must carry the LUFID to the new entry. */
> + retval = ext4_lufid_snapshot(old.de,
> + old.dir->i_sb->s_blocksize,
> + &old_snap);
> + if (retval)
> + goto end_rename;
[Severity: Low]
Should this check (and the matching one in ext4_cross_rename()) also gate
on ext4_has_feature_filetype(), or should mount reject DIRDATA without
FILETYPE?
ext4_setent() nests its dirdata handling inside ext4_has_feature_filetype(),
while ext4_rename() and ext4_cross_rename() only check
ext4_has_feature_dirdata(). If a filesystem is mounted with DIRDATA set
and FILETYPE clear, rename will snapshot bogus LUFIDs and propagate them on
the !new.bh path while ext4_setent() skips dirdata updates on overwrite and
cross-rename.
> + if (!old_snap.fid)
> + old_file_type &= ~EXT4_DIRENT_LUFID;
> + }
[Severity: Low]
Is clearing EXT4_DIRENT_LUFID from old_file_type here (and from
old_de_file_type and new_file_type in ext4_cross_rename()) dead code?
ext4_setent() unconditionally masks its file_type argument with
EXT4_FT_MASK, so any upper dirdata bits in the parameter are ignored.
> + /* Allocate the LUFID wrapper for ext4_add_entry() before any
> + * destructive changes to the old entry. An OOM failure after
> + * ext4_setent() has already compacted the extensions would trigger
> + * the lossy resetent path; pre-allocating here avoids that. */
> + if (!new.bh && old_snap.fid) {
> + unsigned fid_size = old_snap.fid->df_header.ddh_length;
> +
> + edp = kmalloc(offsetof(struct ext4_dentry_param,
> + edp_dfid) + fid_size, GFP_NOFS);
> + if (!edp) {
> + retval = -ENOMEM;
> + goto end_rename;
> + }
> + edp->edp_magic = EXT4_LUFID_MAGIC;
> + memcpy(&edp->edp_dfid, old_snap.fid, fid_size);
> + }
[ ... ]
> if (!new.bh) {
> + new.dentry->d_fsdata = edp;
> retval = ext4_add_entry(handle, new.dentry, old.inode);
> + new.dentry->d_fsdata = NULL;
> if (retval)
> goto end_rename;
[Severity: Medium]
Does this kmalloc() defeat the inline stack buffer in ext4_lufid_snap on
every non-overwriting rename of a LUFID-bearing entry?
When !new.bh && old_snap.fid, ext4_rename() allocates edp and copies
old_snap.fid a second time while holding an active JBD2 transaction handle
and parent directory locks, only for static helper ext4_add_entry() ->
__ext4_add_entry() to unwrap it straight back to struct ext4_dirent_fid *
via ext4_dentry_get_fid(). This adds a per-rename slab allocation and an
avoidable -ENOMEM failure path.
Passing old_snap.fid directly to __ext4_add_entry() (after the
fscrypt_is_nokey_name() check) or embedding edp_magic in
struct ext4_lufid_snap would avoid the allocation and extra copy.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203184938.422849-1-ablagodarenko@thelustrecollective.com?part=10
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v15 11/13] ext4: add dirdata set/get helpers
2026-12-03 18:49 ` [PATCH v15 11/13] ext4: add dirdata set/get helpers Artem Blagodarenko
@ 2026-12-03 22:30 ` sashiko-bot
0 siblings, 0 replies; 26+ messages in thread
From: sashiko-bot @ 2026-12-03 22:30 UTC (permalink / raw)
To: Artem Blagodarenko, linux-ext4; +Cc: tytso
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] ext4: unchecked FILETYPE dependency when writing dirdata
flags
- [Low] ext4: inconsistent legacy hash fallback in ext4_dirdata_get()
and docs
- [Low] ext4: dx_show_leaf() still reads EXT4_DIRENT_HASH() directly
- [Low] ext4: ext4_dirdata_get() lacks dfid buffer size check and tail
entry guard
--
commit 9f42fb8c06ee8e0280f7f4685eb005c37759c828
Author: Artem Blagodarenko <artem.blagodarenko@gmail.com>
ext4: add dirdata set/get helpers
This patch adds ext4_dirdata_get(), ext4_dirent_hash_get(), and
ext4_dirdata_set() to read and write dirdata records in directory entries,
and converts existing casefold+encrypted hash readers and
ext4_insert_dentry_data() to use them.
> diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
> index 8a1c292ddd2f4..ae58ce5120e1e 100644
> --- a/fs/ext4/namei.c
> +++ b/fs/ext4/namei.c
[ ... ]
> @@ -1196,6 +1200,239 @@ static inline int search_dirblock(struct buffer_head *bh,
> * Directory block splitting, compacting
> */
>
> +/*
> + * ext4_dirdata_get() - Read dirdata fields from a directory entry.
[ ... ]
> +unsigned char ext4_dirdata_get(struct ext4_dir_entry_2 *de, struct inode *dir,
> + int buf_size,
> + struct ext4_dirent_fid *dfid,
> + struct dx_hash_info *hinfo)
> +{
[ ... ]
> + if (de->file_type & EXT4_DIRENT_LUFID) {
> + struct ext4_dirent_fid *disk_fid =
> + (struct ext4_dirent_fid *)((char *)de +
> + EXT4_BASE_DIR_LEN + data_offset);
> + unsigned int dlen;
> + /* struct ext4_fid df_fid[] does not provide the array size.
> + * First, verify that the header lies within the valid area, then
> + * verify that the entire record fits within it. */
> + if (EXT4_BASE_DIR_LEN + data_offset +
> + sizeof(disk_fid->df_header) > rec_len)
> + return ret;
> +
> + dlen = disk_fid->df_header.ddh_length;
> + if (dlen == 0 ||
> + EXT4_BASE_DIR_LEN + data_offset + dlen > rec_len)
> + return ret;
> +
> + if (dfid) {
> + memcpy(dfid, disk_fid, dlen);
> + ret |= EXT4_DIRENT_LUFID;
> + }
> + data_offset += dlen;
> + }
[Severity: Low]
Can memcpy(dfid, disk_fid, dlen) overflow a caller-supplied dfid buffer?
ddh_length is an on-disk u8 (up to 255 bytes), and ext4_dirdata_get() takes
no destination buffer size parameter (unlike ext4_lufid_snapshot(), which
checks dlen against its buffer size).
Also, ext4_dirdata_get() does not skip directory checksum tail entries via
ext4_dir_entry_is_tail(de). Because EXT4_FT_DIR_CSUM (0xde) has
EXT4_DIRENT_LUFID set, a call with a non-NULL dfid on a tail entry treats a
byte of det_checksum as ddh_length and can copy checksum bytes into dfid.
Adding a dfid_size parameter and returning 0 early when
ext4_dir_entry_is_tail(de) is true would prevent both.
[ ... ]
> +bool ext4_dirent_hash_get(struct ext4_dir_entry_2 *de, struct inode *dir,
> + int buf_size, struct dx_hash_info *hinfo)
> +{
> + unsigned int rec_len = ext4_rec_len_from_disk(de->rec_len, buf_size);
> +
> + if (ext4_has_feature_dirdata(dir->i_sb) &&
> + (de->file_type & ~EXT4_FT_MASK))
> + return ext4_dirdata_get(de, dir, buf_size, NULL, hinfo) &
> + EXT4_DIRENT_CFHASH;
> +
> + if (ext4_dirent_rec_len(de->name_len, dir) > rec_len)
> + return false;
> + hinfo->hash = EXT4_DIRENT_HASH(de);
> + hinfo->minor_hash = EXT4_DIRENT_MINOR_HASH(de);
> + return true;
> +}
> +
> +/*
> + * ext4_dirdata_set() - Write dirdata fields into a directory entry.
> + * @de: directory entry (name must already be set)
> + * @dir: directory inode
> + * @data: LUFID data to store (or NULL)
> + * @fname: filename info carrying the casefold hash
[Severity: Low]
Should ext4_dirdata_get() and ext4_dirent_hash_get() agree on whether
pre-dirdata hashes are read once EXT4_FEATURE_INCOMPAT_DIRDATA is enabled?
ext4_dirent_hash_get() falls back to EXT4_DIRENT_HASH(de) whenever no
dirdata bits are set in de->file_type, even with dirdata enabled.
ext4_dirdata_get() instead restricts that fallback to
!ext4_has_feature_dirdata(dir->i_sb), and its compatibility comment states
that pre-dirdata entries are misread as having no hash after dirdata is
enabled.
Separately, the doc comment above ext4_dirdata_set() documents @data while
the parameter is named dfid.
[ ... ]
> + if (dfid) {
> + unsigned int dlen = dfid->df_header.ddh_length;
> +
> + if (EXT4_BASE_DIR_LEN + data_offset + dlen > rec_len) {
> + EXT4_ERROR_INODE(dir, "Can not insert FID");
> + return;
> + }
> +
> + memcpy((char *)de + EXT4_BASE_DIR_LEN + data_offset, dfid, dlen);
> + de->file_type |= EXT4_DIRENT_LUFID;
> + data_offset += dlen;
> + }
> +
> + if (ext4_hash_in_dirent(dir)) {
> + if (ext4_has_feature_dirdata(dir->i_sb)) {
> + struct ext4_dirent_hash *dh =
> + (struct ext4_dirent_hash *)((char *)de +
> + EXT4_BASE_DIR_LEN + data_offset);
> +
> + if (EXT4_BASE_DIR_LEN + data_offset + sizeof(*dh) > rec_len) {
> + EXT4_ERROR_INODE(dir, "Can not insert dhash dirdata");
> + return;
> + }
> +
> + dh->dh_header.ddh_length = sizeof(*dh);
> + dh->dh_hash = cpu_to_le32(hinfo->hash);
> + dh->dh_minor_hash = cpu_to_le32(hinfo->minor_hash);
> + de->file_type |= EXT4_DIRENT_CFHASH;
[Severity: Medium]
Does ext4_dirdata_set() assume EXT4_FEATURE_INCOMPAT_FILETYPE is enabled?
Dirdata flags live in the upper bits of de->file_type, which is the upper
byte of name_len when filetype is disabled. ext4_feature_set_ok() does not
reject mounting a filesystem with EXT4_FEATURE_INCOMPAT_DIRDATA and without
EXT4_FEATURE_INCOMPAT_FILETYPE, while ext4_setent() gates its rename LUFID
maintenance on ext4_has_feature_filetype(), leaving stale LUFID records
after a rename.
Should ext4_feature_set_ok() require EXT4_FEATURE_INCOMPAT_FILETYPE when
EXT4_FEATURE_INCOMPAT_DIRDATA is set?
[ ... ]
> @@ -1219,11 +1456,13 @@ static int dx_make_map(struct inode *dir, struct buffer_head *bh,
> ((char *)de) - base))
> return -EFSCORRUPTED;
> if (de->name_len && de->inode) {
> - if (ext4_hash_in_dirent(dir))
> - h.hash = EXT4_DIRENT_HASH(de);
> - else {
> + if (ext4_hash_in_dirent(dir)) {
> + if (!ext4_dirent_hash_get(de, dir, blocksize,
> + &h))
> + return -EFSCORRUPTED;
> + } else {
> int err = ext4fs_dirhash(dir, de->name,
> - de->name_len, &h);
> + de->name_len, &h);
> if (err < 0)
> return err;
> }
[Severity: Low]
Was dx_show_leaf() missed when converting EXT4_DIRENT_HASH(de) callers to
ext4_dirent_hash_get()?
Under DX_DEBUG and CONFIG_FS_ENCRYPTION, dx_show_leaf() still reads
EXT4_DIRENT_HASH(de) directly (before its entry bounds check), which reads
the wrong offset on dirdata-enabled casefolded+encrypted directories.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203184938.422849-1-ablagodarenko@thelustrecollective.com?part=11
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v15 12/13] ext4: Add EXT4_IOC_SET_LUFID ioctl for setting LUFID on directory entries
2026-12-03 18:49 ` [PATCH v15 12/13] ext4: Add EXT4_IOC_SET_LUFID ioctl for setting LUFID on directory entries Artem Blagodarenko
@ 2026-12-03 23:01 ` sashiko-bot
0 siblings, 0 replies; 26+ messages in thread
From: sashiko-bot @ 2026-12-03 23:01 UTC (permalink / raw)
To: Artem Blagodarenko, linux-ext4; +Cc: tytso
Thank you for your contribution! Sashiko AI review found 11 potential
issues to consider:
- [Critical] ext4: rollback in ext4_dirdata_set_lufid() reuses stale
buffer_head
- [Critical] ext4: casefold ignorable code points bypass dot/dotdot
check in set_lufid
- [High] ext4: TOCTOU race on directory permissions and IS_IMMUTABLE in
set_lufid
- [Medium] ext4: ext4_dirdata_set_lufid() drops non-LUFID/CFHASH dirdata
records
- [Medium] ext4: ext4_dirdata_set_lufid() sets LUFID without filetype
feature
- [Medium] ext4: ext4_dirdata_set_lufid() skips in-place update and
over-reserves credits
- [Medium] ext4: EXT4_IOC_SET_LUFID UAPI lacks extensibility, alignment,
and read/clear
- [Low] ext4: commit bundles unrelated fixes and cleanups absent from
changelog
- [Low] ext4: ext4_dirdata_set_lufid() frees fscrypt buffer with raw
kfree()
- [Low] ext4: ext4_dirdata_set_lufid() ignores ext4_mark_inode_dirty()
error
- [Low] ext4: ext4_dirdata_set_lufid() repeats lookup and filename setup
unnecessarily
--
commit 7fa00ca4c562c2ac8bca881f8348e7b8059dfb52
Author: Artem Blagodarenko <artem.blagodarenko@gmail.com>
ext4: Add EXT4_IOC_SET_LUFID ioctl for setting LUFID on directory entries
This commit adds the EXT4_IOC_SET_LUFID ioctl along with
ext4_ioctl_set_lufid() and ext4_dirdata_set_lufid() to attach LUFID
dirdata to an existing directory entry by deleting and re-adding it.
> diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c
> index 83630cd40712c..a6baba5968f47 100644
> --- a/fs/ext4/ioctl.c
> +++ b/fs/ext4/ioctl.c
> @@ -1563,6 +1563,90 @@ static int ext4_ioctl_set_tune_sb(struct file *filp,
[ ... ]
> +static long ext4_ioctl_set_lufid(struct file *filp, unsigned long arg)
> +{
[ ... ]
> + /* This ioctl mutates directory entries; merely having the directory
> + * open (which only ever requires read access) is not enough.
> + * MAY_EXEC is required for entry lookup; MAY_WRITE for modification. */
> + err = inode_permission(idmap, dir, MAY_WRITE | MAY_EXEC);
> + if (err)
> + return err;
> +
> + /* Copy arguments from user space */
> + if (copy_from_user(&lufid_args, (struct ext4_set_lufid __user *)arg,
> + sizeof(lufid_args)))
> + return -EFAULT;
[Severity: High]
Can a concurrent chattr +i or chmod/chown race with inode_permission()
here and be bypassed?
inode_permission() checks directory write/exec permissions and
IS_IMMUTABLE(dir) without holding dir->i_rwsem, before the sleepable
copy_from_user() and mnt_want_write_file(). ext4_dirdata_set_lufid()
later acquires inode_lock_nested(dir, I_MUTEX_PARENT) and checks
IS_APPEND(dir), but never re-checks inode_permission() or
IS_IMMUTABLE(dir).
CPU0 (ioctl) CPU1 (chattr +i / chmod)
ext4_ioctl_set_lufid()
inode_permission(dir, ...)
copy_from_user() [stalls]
vfs_fileattr_set()
inode_lock(dir)
set S_IMMUTABLE on dir
inode_unlock(dir)
mnt_want_write_file()
ext4_dirdata_set_lufid()
inode_lock_nested(dir, ...)
ext4_delete_entry() <-- mutates immutable dir
A user with initial access can stall copy_from_user() (for example via
userfaultfd) while S_IMMUTABLE is set or permissions are revoked, then
resume and mutate the directory. Moving inode_permission(idmap, dir,
MAY_WRITE | MAY_EXEC) into ext4_dirdata_set_lufid() under
inode_lock_nested(dir, I_MUTEX_PARENT) closes the window and ensures
the exported helper enforces directory permissions itself.
[ ... ]
> + /* '.' and '..' are not ordinary entries -- they must stay the first
> + * two entries in the directory's first block, so they can't go
> + * through the general delete+re-add path this ioctl uses */
> + if (!strcmp(lufid_args.esl_name, ".") || !strcmp(lufid_args.esl_name, ".."))
> + return -EINVAL;
[Severity: Critical]
Can a casefolded name with Unicode default-ignorable code points bypass
this strcmp() check and match "." or ".."?
In a casefolded directory, utf8_casefold() strips default-ignorable code
points such as U+00AD (soft hyphen), so "." or ".." followed by U+00AD
passes strcmp() here but still matches "." or ".." in block 0 via
ext4_find_entry(). Matching "." hits EXT4_ERROR_INODE() on ino ==
dir->i_ino on a clean filesystem, triggering emergency read-only remount
or panic.
Matching ".." proceeds to ext4_delete_entry(), where
ext4_generic_delete_entry() merges ".." into "." and zeroes de->rec_len.
In an HTree directory where dotdot->rec_len spans blocksize - 12, that
wipes the entire dx_root block; in a linear directory, it relocates ".."
out of slot 2. Checking whether the matched dirent de is "." or ".." in
ext4_dirdata_set_lufid() avoids both.
> diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
> index ae58ce5120e1e..f9250407cedbc 100644
> --- a/fs/ext4/namei.c
> +++ b/fs/ext4/namei.c
> @@ -1372,9 +1372,9 @@ bool ext4_dirent_hash_get(struct ext4_dir_entry_2 *de, struct inode *dir,
> -static void ext4_dirdata_set(struct ext4_dir_entry_2 *de, struct inode *dir,
> - struct ext4_dirent_fid *dfid,
> - struct ext4_filename *fname)
> +static int ext4_dirdata_set(struct ext4_dir_entry_2 *de, struct inode *dir,
> + struct ext4_dirent_fid *dfid,
> + struct ext4_filename *fname)
[ ... ]
> @@ -1419,11 +1419,18 @@ static void ext4_dirdata_set(struct ext4_dir_entry_2 *de, struct inode *dir,
> - /* Compatibility: store hash inline after filename */
> - if (EXT4_BASE_DIR_LEN + data_offset +
> - sizeof(struct ext4_dir_entry_hash) > rec_len) {
> + /* Compatibility: store hash inline after filename.
> + * EXT4_DIRENT_HASHES places the hash at the
> + * 4-byte-aligned offset (8 + name_len + 3) & ~3, which
> + * may be less than EXT4_BASE_DIR_LEN + data_offset when
> + * name_len is a multiple of 4 (gap byte counted twice).
> + * Use the same aligned offset for the bounds check. */
> + unsigned int hash_off =
> + (EXT4_BASE_DIR_LEN + de->name_len + 3) & ~3;
> +
> + if (hash_off + sizeof(struct ext4_dir_entry_hash) > rec_len) {
> EXT4_ERROR_INODE(dir, "Can not insert dhash");
> - return;
> + return -EIO;
> }
[Severity: Low]
This isn't a bug, but the commit bundles several unrelated changes that
are not mentioned in the changelog: the legacy inline-hash bounds-check
fix and void-to-int conversion of ext4_dirdata_set() and
ext4_insert_dentry_data(), caching dot_rec_len in ext4_init_dirblock(),
simplifying kmalloc/memcpy in ext4_rename(), and comments in
ext4_dirent_rec_len() and ext4_init_new_dir_data(). Splitting these out
or folding them into the earlier patches in the series would make the
history easier to follow and bisect.
[ ... ]
> @@ -4685,6 +4718,280 @@ static int ext4_rename2(const struct mnt_idmap *idmap,
[ ... ]
> +int ext4_dirdata_set_lufid(const struct mnt_idmap *idmap, struct inode *dir,
> + const char *filename, int namelen,
> + struct ext4_dentry_param *edp)
> +{
[ ... ]
> + if (!ext4_has_feature_dirdata(sb))
> + return -EOPNOTSUPP;
[Severity: Medium]
Should this also require ext4_has_feature_filetype(sb)?
ext4_feature_set_ok() allows mounting a filesystem with dirdata enabled
and filetype disabled. In that configuration, ext4_dirdata_set() still
sets EXT4_DIRENT_LUFID in de->file_type (the high byte of legacy 16-bit
name_len), while ext4_setent() skips all dirdata maintenance when
filetype is off, leaving stale LUFIDs attached to the wrong inode on
later rename overwrites or exchanges.
[ ... ]
> + handle = ext4_journal_start(dir, EXT4_HT_DIR,
> + 3 * EXT4_DATA_TRANS_BLOCKS(sb) +
> + 2 * EXT4_INDEX_EXTRA_TRANS_BLOCKS);
[Severity: Medium]
Is it necessary to unconditionally reserve 105 journal credits (with
extents and quotas) and always delete and re-add the entry instead of
attempting an in-place LUFID update first?
ext4_setent() already updates LUFID in place via ext4_dirent_drop_lufid()
and ext4_dirent_add_lufid() when de->rec_len has room. Always deleting
and calling ext4_add_fname() causes O(N * B) linear rescans from block 0
when tagging entries across B blocks in a non-indexed directory, triggers
avoidable HTree leaf splits on hash-collision chains even for same-size
LUFID updates, and consumes excessive JBD2 credit budget under concurrent
metadata workloads.
[ ... ]
> + /*
> + * Look up the entry again inside the handle and check that it still
> + * refers to the same inode (an unlink and create could have run in
> + * between).
> + */
> + bh = ext4_find_entry(dir, &d_name, &de, &inlined);
[Severity: Low]
Why is ext4_find_entry() repeated here after releasing bh from the first
lookup?
inode_lock_nested(dir, I_MUTEX_PARENT) is held continuously across both
lookups, so no concurrent unlink or create on dir can run in between;
keeping bh from the first lookup (as __ext4_unlink() and ext4_rename()
do) avoids repeating the directory search under an open JBD2 handle.
Similarly, ext4_fname_setup_filename() is called twice below on the same
add_name only to hold different dfid pointers, whereas a single
ext4_filename can swap fname.dfid to old_snap.fid on rollback.
[ ... ]
> + /* Copy the old LUFID so a failed re-add can restore it */
> + err = ext4_lufid_snapshot(de, sb->s_blocksize, &old_snap);
> + if (err)
> + goto out_brelse;
[Severity: Medium]
Does deleting and re-adding the entry here silently drop non-LUFID,
non-CFHASH dirdata records such as EXT4_DIRENT_INO64 or 0x80?
Only EXT4_DIRENT_LUFID is snapshotted, and ext4_dirdata_set() only writes
LUFID and CFHASH, so any other dirdata records present on de are lost on
both success and rollback. Preserving trailing records (as ext4_setent()
does) or rejecting entries with unsupported dirdata flags with
-EOPNOTSUPP would avoid silent metadata loss.
[ ... ]
> + err = ext4_add_fname(handle, dir, &new_fname, inode);
> + if (err) {
> + /*
> + * Put the old entry back into the space it was deleted from.
> + * A new lookup by name could pick another block of the same
> + * hash, which may be full. An inline directory has only the
> + * one buffer, and the space is there.
> + */
> + if (inlined)
> + err2 = ext4_add_fname(handle, dir, &old_fname, inode);
> + else
> + err2 = add_dirent_to_buf(handle, &old_fname, dir,
> + inode, NULL, bh);
[Severity: Critical]
Can this rollback corrupt the directory by writing old_fname into a stale
bh after ext4_add_fname() has already transformed that block?
If dir is a 1-block linear directory with dir_index enabled and the
larger LUFID entry does not fit in block 0 (bh), ext4_add_fname() calls
make_indexed_dir() on bh. make_indexed_dir() moves existing entries to
block 1, converts bh into the HTree dx_root block (setting
dotdot_de->rec_len to blocksize - 12 and EXT4_INODE_INDEX on dir), and
then calls do_split(). If do_split() fails (for example with -ENOSPC or
-EDQUOT), make_indexed_dir() returns an error with bh permanently
converted to dx_root:
- Without metadata_csum, add_dirent_to_buf() treats the dx_root_info and
dx_entry[] area as slack space in dotdot_de, overwrites the HTree root
with old_fname, and returns 0, detaching every entry moved to block 1.
- With metadata_csum, __ext4_check_dir_entry() rejects dotdot_de as
overrunning blocksize - csum_size, firing EXT4_ERROR_INODE() and
failing rollback so the directory entry is permanently lost and inode
is orphaned.
The same staleness occurs in an already-indexed directory if
ext4_dx_add_entry() commits a leaf split in do_split() and then
add_dirent_to_buf() fails: rollback writes old_fname into the original bh
even when its hash now belongs in the new split block bh2, leaving the
entry unreachable by lookup while readdir still lists it.
[ ... ]
> + inode_set_ctime_current(dir);
> + inode_inc_iversion(dir);
> + ext4_mark_inode_dirty(handle, dir);
[Severity: Low]
Should the return value of ext4_mark_inode_dirty() be checked, or can
these three lines be removed?
Every success path of ext4_add_fname() (add_dirent_to_buf() and
ext4_try_add_inline_entry()) already updates dir's ctime and iversion and
calls ext4_mark_inode_dirty(handle, dir) with error checking, so this
repeats the work and silently discards any error.
[ ... ]
> + kfree(stored.name);
[Severity: Low]
Should the encrypted branch free stored with fscrypt_fname_free_buffer()
instead of raw kfree()?
When IS_CASEFOLDED(dir) && IS_ENCRYPTED(dir), stored is allocated via
fscrypt_fname_alloc_buffer(), so freeing it directly with kfree() relies
on fscrypt's internal allocation strategy.
> diff --git a/include/uapi/linux/ext4.h b/include/uapi/linux/ext4.h
> index 9c683991c32f9..5d5c8c3df2a43 100644
> --- a/include/uapi/linux/ext4.h
> +++ b/include/uapi/linux/ext4.h
[ ... ]
> @@ -92,6 +93,19 @@ struct move_extent {
> __u64 moved_len; /* moved block length */
> };
>
> +/*
> + * Structure for EXT4_IOC_SET_LUFID
> + * Sets LUFID on a directory entry
> + * Called on parent directory with filename and LUFID data as arguments
> + */
> +struct ext4_set_lufid {
> + __u8 esl_name_len; /* length of filename, NOT including NUL terminator
> + * (valid range: 1-255, matching EXT4_NAME_LEN) */
> + char esl_name[255 + 1]; /* filename (NUL-terminated) */
> + __u8 esl_data_len; /* length of LUFID data (1-48) */
> + char esl_data[255]; /* LUFID data (raw bytes) */
> +};
[Severity: Medium]
Does struct ext4_set_lufid have several UAPI layout and extensibility
issues that will be difficult to fix once merged?
Unlike struct fsuuid and struct ext4_tune_sb_params in this header,
struct ext4_set_lufid has no flags or reserved fields and has an
unaligned 513-byte size. esl_data uses signedness-dependent char[255]
instead of __u8 for raw bytes, yet esl_data_len is capped at 48 bytes
while the 207 unused tail bytes (and unused esl_name bytes) are never
checked for zero, preventing future reuse.
In addition, esl_data_len == 0 is rejected with -EINVAL so callers cannot
clear an existing LUFID, arbitrary lengths from 1 to 48 bytes are
accepted without validating 16-byte struct ext4_fid alignment, and there
is no UAPI to read LUFID data back from userspace.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203184938.422849-1-ablagodarenko@thelustrecollective.com?part=12
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v15 13/13] ext4: add KUnit tests for dirdata
2026-12-03 18:49 ` [PATCH v15 13/13] ext4: add KUnit tests for dirdata Artem Blagodarenko
@ 2026-12-03 23:15 ` sashiko-bot
0 siblings, 0 replies; 26+ messages in thread
From: sashiko-bot @ 2026-12-03 23:15 UTC (permalink / raw)
To: Artem Blagodarenko, linux-ext4; +Cc: tytso
Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [Critical] ext4: ext4_dirdata_set_lufid() rollback corrupts htree root
or orphans inode
- [Medium] ext4: TOCTOU race on dir permission/immutability in
ext4_ioctl_set_lufid()
- [Medium] ext4: unaligned rec_len accepted in dx_get_dx_info() and
get_dx_countlimit()
- [Low] ext4: inverted e2fsck vs kernel comment in
test_dirdata_dx_node_limit()
- [Low] ext4: inaccurate rename-test comment above dirdata_iget_stub()
- [Low] ext4: oversized dot entry test misses first guard in
dx_get_dx_info()
- [Low] ext4: leak of dir_info on assertion failure in
test_dirdata_htree_with_lufids()
--
commit 09dacab8b09110015062b9b89088f346310d76d3
Author: Artem Blagodarenko <artem.blagodarenko@gmail.com>
ext4: add KUnit tests for dirdata
This commit adds the ext4_dirdata_test KUnit suite to fs/ext4/namei-test.c
and exports the needed dirdata and htree test helpers from fs/ext4/namei.c.
> diff --git a/fs/ext4/namei-test.c b/fs/ext4/namei-test.c
> index b45f5ad3b7af6..ba1bb52d24425 100644
> --- a/fs/ext4/namei-test.c
> +++ b/fs/ext4/namei-test.c
[ ... ]
> + /* So is a rec_len of '.' that leaves no room for '..' */
> + dirdata_make_de(b, dir->i_ino, ".", EXT4_FT_DIR, false, 0, false,
> + 0, 0, TEST_BLK_SIZE - 8);
> + KUNIT_EXPECT_TRUE(test, IS_ERR(dx_get_dx_info_test(dir, b)));
[Severity: Low]
Does TEST_BLK_SIZE - 8 actually exercise the first-entry overflow guard in
dx_get_dx_info()?
dx_get_dx_info() checks rlen + EXT4_BASE_DIR_LEN > blocksize, and with
EXT4_BASE_DIR_LEN == 8 a rec_len of TEST_BLK_SIZE - 8 passes that check.
This subcase only returns an error because the trailing 8 bytes of b happen
to be zeroed from an earlier call; using TEST_BLK_SIZE - 4 would hit the
intended guard directly.
[ ... ]
> + dir_info = kzalloc_obj(*dir_info);
> + KUNIT_ASSERT_NOT_NULL(test, dir_info);
> + dir_file->private_data = dir_info;
> + cnt = ext4_htree_fill_tree(dir_file, curr_hash, 0, &next_hash);
> + KUNIT_ASSERT_GE(test, cnt, 0);
> + total += cnt;
> + ext4_htree_free_dir_info(dir_info);
[Severity: Low]
Can this leak dir_info if ext4_htree_fill_tree() fails?
KUNIT_ASSERT_GE() aborts the test before ext4_htree_free_dir_info() runs,
leaking both dir_info and any struct fname rb-tree nodes inserted before
the error. Calling ext4_htree_free_dir_info(dir_info) before the assertion
avoids the leak.
[ ... ]
> +/* The rename tests look the target up through ext4_iget() */
> +static struct inode *dirdata_iget_stub(struct super_block *sb,
[Severity: Low]
This isn't a bug, but test_dirdata_rename_keeps_lufid() never activates
dirdata_iget_stub(); the only caller is test_dirdata_set_lufid().
[ ... ]
> +/*
> + * Dirdata 9: an htree index node of a casefolded and encrypted directory
> + * may have the limit that the kernel computes or the one that e2fsck
> + * computes, which reserves room for the hash in the fake dirent.
> + */
[Severity: Low]
Is this comment inverted relative to fs/ext4/namei.h? dx_node_limit() and
dx_node_limit_ok() document that e2fsck (and the current kernel) do not
reserve room for the hash in the fake dirent, while older kernels did.
> diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
> index f9250407cedbc..b8a5bd8036036 100644
> --- a/fs/ext4/namei.c
> +++ b/fs/ext4/namei.c
[ ... ]
> @@ -4991,6 +4993,7 @@ int ext4_dirdata_set_lufid(const struct mnt_idmap *idmap, struct inode *dir,
>
> return err;
> }
> +EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_dirdata_set_lufid);
[Severity: Critical]
Can the error rollback in ext4_dirdata_set_lufid() (introduced in commit
"ext4: Add EXT4_IOC_SET_LUFID ioctl for setting LUFID on directory entries")
corrupt an htree root block or orphan the target inode?
On a one-block linear directory, ext4_dirdata_set_lufid() deletes the entry
from block 0 (bh) and calls ext4_add_fname(). If the larger LUFID entry
does not fit, make_indexed_dir() sets EXT4_INODE_INDEX, moves existing
entries to block 1, and rewrites bh into an htree root block before calling
do_split(). If do_split() then fails on ext4_append() with -ENOSPC or
-EDQUOT, make_indexed_dir() leaves dir indexed and bh formatted as an htree
root.
Rollback then calls add_dirent_to_buf(handle, &old_fname, dir, inode, NULL,
bh) on that same bh. Without metadata_csum, ext4_find_dest_de() treats the
block-spanning '..' rec_len as free space and overwrites dx_root_info and
dx_entry[] with old_fname, silently corrupting the htree root on disk. With
metadata_csum, ext4_check_dir_entry() rejects the full-block '..' entry with
-EFSCORRUPTED, triggering EXT4_ERROR_INODE() and leaving inode orphaned.
In an already-indexed directory, if ext4_dx_add_entry() splits the leaf
block before failing, writing old_fname back into the stale bh can also
place the entry in the wrong htree hash leaf.
[Severity: Medium]
Can a racing EXT4_IOC_SET_LUFID ioctl bypass a concurrent chmod or chattr +i
on the parent directory?
ext4_ioctl_set_lufid() (from commit "ext4: Add EXT4_IOC_SET_LUFID ioctl for
setting LUFID on directory entries") calls inode_permission(idmap, dir,
MAY_WRITE | MAY_EXEC) without holding dir->i_rwsem, and then sleeps in
copy_from_user(), mnt_want_write_file(), and dquot_initialize() before
ext4_dirdata_set_lufid() takes inode_lock_nested(dir, I_MUTEX_PARENT).
CPU0 CPU1
ext4_ioctl_set_lufid()
inode_permission(MAY_WRITE...)
copy_from_user() (sleeps)
vfs_fileattr_set() (chattr +i)
inode_lock(dir)
dir->i_flags |= S_IMMUTABLE
inode_unlock(dir)
ext4_dirdata_set_lufid()
inode_lock_nested(dir, ...)
... (no MAY_WRITE/IMMUTABLE
re-check)
ext4_delete_entry() <-- mutates immutable dir
Under dir->i_rwsem, ext4_dirdata_set_lufid() checks IS_APPEND(dir) but
relies on the earlier unlocked inode_permission() call for MAY_WRITE,
MAY_EXEC, and IS_IMMUTABLE(dir). Re-checking inode_permission(idmap, dir,
MAY_WRITE | MAY_EXEC) after acquiring inode_lock_nested(dir, I_MUTEX_PARENT)
would close this window.
[ ... ]
> @@ -5132,6 +5135,12 @@ bool ext4_check_dx_root_test(struct inode *dir, struct dx_root *root)
> }
> EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_check_dx_root_test);
>
> +struct dx_root_info *dx_get_dx_info_test(struct inode *dir, void *de_buf)
> +{
> + return dx_get_dx_info(dir, de_buf);
> +}
> +EXPORT_SYMBOL_FOR_EXT4_TEST(dx_get_dx_info_test);
[Severity: Medium]
Can dx_get_dx_info() and get_dx_countlimit() (updated in commit "ext4:
refactor dx_root to support variable dirent sizes") return unaligned htree
pointers on a corrupted root block?
On filesystems with blocksize < 65536, ext4_rec_len_from_disk() returns the
raw 16-bit rec_len without enforcing 4-byte alignment, and neither
dx_get_dx_info() nor get_dx_countlimit() checks (rlen & 3) != 0 on '.' (or
on '..' in dx_get_dx_info()). A crafted '.' rec_len such as 13 passes the
bounds checks and places the '..' entry, dx_root_info, dx_countlimit,
dx_entry[], and dx_tail at unaligned offsets even though none of those
structs are __packed, triggering UBSAN alignment faults or traps on
strict-alignment architectures.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203184938.422849-1-ablagodarenko@thelustrecollective.com?part=13
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v15 00/13] Data in direntry (dirdata) feature, with KUnit tests
2026-12-03 18:49 [PATCH v15 00/13] Data in direntry (dirdata) feature, with KUnit tests Artem Blagodarenko
` (12 preceding siblings ...)
2026-12-03 18:49 ` [PATCH v15 13/13] ext4: add KUnit tests for dirdata Artem Blagodarenko
@ 2026-12-03 23:15 ` sashiko-bot
13 siblings, 0 replies; 26+ messages in thread
From: sashiko-bot @ 2026-12-03 23:15 UTC (permalink / raw)
To: Artem Blagodarenko, linux-ext4; +Cc: tytso
Sashiko has finished reviewing this 13-patch series:
- Patches 2 and 4: no issues found.
- Patch 1 ("ext4: validate count against limit in ext4_dx_csum_v..."): 1
potential issue (1 Low)
- Patch 3 ("ext4: replace ext4_dir_entry with ext4_dir_entry_2"): 2
potential issues (1 Medium, 1 Low)
- Patch 5 ("ext4: add dirdata format definitions and access helpers"): 2
potential issues (2 Low)
- Patch 6 ("ext4: refactor dx_root to support variable dirent sizes"): 5
potential issues (3 Medium, 2 Low)
- Patch 7 ("ext4: size the htree root from where its dx_entry ar..."): 2
potential issues (1 Medium, 1 Low)
- Patch 8 ("ext4: rename ext4_dir_rec_len() and clarify dirdata..."): 5
potential issues (3 Medium, 2 Low)
- Patch 9 ("ext4: dirdata feature"): 8 potential issues (5 Medium, 3
Low)
- Patch 10 ("ext4: add dirdata LUFID support for directory entry..."): 8
potential issues (4 Medium, 4 Low)
- Patch 11 ("ext4: add dirdata set/get helpers"): 4 potential issues (1
Medium, 3 Low)
- Patch 12 ("ext4: Add EXT4_IOC_SET_LUFID ioctl for setting LUFID..."):
11 potential issues (2 Critical, 1 High, 4 Medium, 4 Low)
- Patch 13 ("ext4: add KUnit tests for dirdata"): 7 potential issues (1
Critical, 2 Medium, 4 Low)
Detailed reviews have been sent as separate replies to patches 1, 3, and
5-13.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203184938.422849-1-ablagodarenko@thelustrecollective.com
^ permalink raw reply [flat|nested] 26+ messages in thread
end of thread, other threads:[~2026-12-03 23:15 UTC | newest]
Thread overview: 26+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 18:49 [PATCH v15 00/13] Data in direntry (dirdata) feature, with KUnit tests Artem Blagodarenko
2026-12-03 18:49 ` [PATCH v15 01/13] ext4: validate count against limit in ext4_dx_csum_verify/_set Artem Blagodarenko
2026-12-03 19:03 ` sashiko-bot
2026-12-03 18:49 ` [PATCH v15 02/13] ext4: use the e2fsck limit for htree index nodes Artem Blagodarenko
2026-12-03 18:49 ` [PATCH v15 03/13] ext4: replace ext4_dir_entry with ext4_dir_entry_2 Artem Blagodarenko
2026-12-03 19:33 ` sashiko-bot
2026-12-03 18:49 ` [PATCH v15 04/13] ext4: add ext4_dir_entry_is_tail() Artem Blagodarenko
2026-12-03 18:49 ` [PATCH v15 05/13] ext4: add dirdata format definitions and access helpers Artem Blagodarenko
2026-12-03 19:59 ` sashiko-bot
2026-12-03 18:49 ` [PATCH v15 06/13] ext4: refactor dx_root to support variable dirent sizes Artem Blagodarenko
2026-12-03 20:21 ` sashiko-bot
2026-12-03 18:49 ` [PATCH v15 07/13] ext4: size the htree root from where its dx_entry array starts Artem Blagodarenko
2026-12-03 20:37 ` sashiko-bot
2026-12-03 18:49 ` [PATCH v15 08/13] ext4: rename ext4_dir_rec_len() and clarify dirdata usage Artem Blagodarenko
2026-12-03 21:05 ` sashiko-bot
2026-12-03 18:49 ` [PATCH v15 09/13] ext4: dirdata feature Artem Blagodarenko
2026-12-03 21:37 ` sashiko-bot
2026-12-03 18:49 ` [PATCH v15 10/13] ext4: add dirdata LUFID support for directory entry rename Artem Blagodarenko
2026-12-03 22:02 ` sashiko-bot
2026-12-03 18:49 ` [PATCH v15 11/13] ext4: add dirdata set/get helpers Artem Blagodarenko
2026-12-03 22:30 ` sashiko-bot
2026-12-03 18:49 ` [PATCH v15 12/13] ext4: Add EXT4_IOC_SET_LUFID ioctl for setting LUFID on directory entries Artem Blagodarenko
2026-12-03 23:01 ` sashiko-bot
2026-12-03 18:49 ` [PATCH v15 13/13] ext4: add KUnit tests for dirdata Artem Blagodarenko
2026-12-03 23:15 ` sashiko-bot
2026-12-03 23:15 ` [PATCH v15 00/13] Data in direntry (dirdata) feature, with KUnit tests sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox