From: sashiko-bot@kernel.org
To: "Artem Blagodarenko" <artem.blagodarenko@gmail.com>,
linux-ext4@vger.kernel.org
Cc: tytso@mit.edu
Subject: Re: [PATCH v15 10/13] ext4: add dirdata LUFID support for directory entry rename
Date: Thu, 03 Dec 2026 22:02:03 +0000 [thread overview]
Message-ID: <sashiko-outbox-248721@kernel.org> (raw)
In-Reply-To: <20261203184938.422849-11-ablagodarenko@thelustrecollective.com>
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
next prev parent reply other threads:[~2026-12-03 22:02 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sashiko-outbox-248721@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=artem.blagodarenko@gmail.com \
--cc=linux-ext4@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
--cc=tytso@mit.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox