* Re: [PATCH v3] ext4: bound xattr journal credits to transaction limit
2026-11-22 9:37 ` [PATCH v3] " arzuagapozosergio
@ 2026-11-22 9:53 ` sashiko-bot
2026-11-24 12:38 ` Jan Kara
2026-11-25 4:51 ` [PATCH v4] " arzuagapozosergio
2 siblings, 0 replies; 22+ messages in thread
From: sashiko-bot @ 2026-11-22 9:53 UTC (permalink / raw)
To: arzuagapozosergio; +Cc: linux-ext4, tytso
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] selftests/ext4: duplicate skips and false pass in main() on
shared-block errors
- [Medium] selftests/ext4: prepare_shared_file() stores large_name in
inode body not block
--
commit acc0f729809bd687fef099d47921332f19729aeb
Author: Ryuji Hiroshi <arzuagapozosergio@gmail.com>
ext4: bound xattr journal credits to transaction limit
This commit refines ext4 xattr journal credit estimation by accounting for an
existing ea_inode reference only when the target attribute currently points to
one and bounding the estimate against the JBD2 transaction buffer limit, along
with adding an ext4 selftest.
Link: https://syzbot.org/bug?extid=25d45c0bd716f2b794a7
> diff --git a/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
> new file mode 100644
> index 0000000000000..313e817671ac3
> --- /dev/null
> +++ b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
> @@ -0,0 +1,244 @@
[ ... ]
> +#define LARGE_VALUE_SIZE 65296
> +#define FILLER_VALUE_SIZE 32
> +#define TEST_COUNT 10
> +
> +static const char large_name[] = "user.ext4-credit-large";
> +static const char small_name[] = "user.ext4-credit-small";
> +static const char target_name[] = "user.ext4-credit-target";
> +static const char filler_name[] = "user.ext4-credit-filler";
[ ... ]
> +static void report_setup_error(const char *what, unsigned int skip_count)
> +{
> + int saved_errno = errno;
> + unsigned int i;
> +
> + ksft_test_result_error("setup %s: %s\n", what,
> + strerror(saved_errno));
> + for (i = 0; i < skip_count; i++)
> + ksft_test_result_skip("setup failed\n");
> +}
> +
> +static bool prepare_shared_file(const char *path, const char *large_value,
> + const char *filler, char target_value)
> +{
> + return create_file(path) == 0 &&
> + set_xattr(path, target_name, &target_value, sizeof(target_value),
> + XATTR_CREATE) &&
> + set_xattr(path, filler_name, filler, FILLER_VALUE_SIZE,
> + XATTR_CREATE) &&
> + set_xattr(path, large_name, large_value, LARGE_VALUE_SIZE,
> + XATTR_CREATE);
> +}
[Severity: Medium]
Does prepare_shared_file() place large_name into the inode body instead of
the shared external xattr block?
On a 256-byte inode with i_extra_isize = 32, 88 bytes are available for xattr
entries and values in the inode body. target_name uses 40 bytes, leaving 48
bytes free. Because filler_name with FILLER_VALUE_SIZE = 32 needs 68 bytes,
ext4_xattr_ibody_set() returns -ENOSPC and spills filler_name to the external
xattr block without consuming the remaining 48 bytes in the inode body. When
large_name is added next, its value is stored in an ea_inode (i.in_inode = 1),
so its entry needs only 36 bytes in the xattr table and fits into the inode
body. As a result, the shared external xattr block only contains filler_name
with e_value_inum == 0, and the test never exercises the block_bh e_value_inum
credit loop in __ext4_xattr_set_credits() or ea_inode refcount updates during
block cloning.
[ ... ]
> + if (prepare_shared_block(shared_a, shared_b, value, readback)) {
> + shared_ready = true;
> + ksft_test_result_pass("shared external xattr block fixture\n");
> + } else {
> + report_setup_error("prepare shared xattr block", 2);
> + }
> +
> + if (shared_ready) {
> + memset(target_new, 0x22, sizeof(target_new));
> + shared_replaced = set_xattr(shared_a, target_name, target_new,
> + sizeof(target_new), XATTR_REPLACE);
> + result(shared_replaced,
> + "replace inode-body xattr with shared external block present");
> +
> + result(xattr_matches(shared_a, target_name, readback,
> + shared_replaced ? target_new : &target_old,
> + shared_replaced ? sizeof(target_new) :
> + sizeof(target_old)) &&
> + xattr_matches(shared_b, target_name, readback,
> + &target_old, sizeof(target_old)) &&
> + xattr_matches(shared_b, large_name, readback, value,
> + LARGE_VALUE_SIZE),
> + "xattr block clone preserves both files' values");
> + } else {
> + ksft_test_result_skip("shared-block replacement needs its fixture\n");
> + ksft_test_result_skip("shared-block readback needs its fixture\n");
> + }
[Severity: Medium]
When prepare_shared_block() fails, does calling report_setup_error(..., 2)
and then entering the !shared_ready branch emit four skip results instead of
two?
That produces 12 TAP results for a 10-test plan and inflates
ksft_cnt.ksft_xskip, which can make ksft_finished() compute
ksft_pass + ksft_xskip == ksft_plan and exit with 0 even when earlier tests
failed or errored.
Also, in the shared_ready branch, falling back to &target_old when
shared_replaced is false causes "xattr block clone preserves both files'
values" to report a pass when the replacement failed and no block clone took
place.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261122093732.136895-1-arzuagapozosergio@gmail.com?part=1
^ permalink raw reply [flat|nested] 22+ messages in thread* Re: [PATCH v3] ext4: bound xattr journal credits to transaction limit
2026-11-22 9:37 ` [PATCH v3] " arzuagapozosergio
2026-11-22 9:53 ` sashiko-bot
@ 2026-11-24 12:38 ` Jan Kara
2026-11-25 4:51 ` [PATCH v4] " arzuagapozosergio
2 siblings, 0 replies; 22+ messages in thread
From: Jan Kara @ 2026-11-24 12:38 UTC (permalink / raw)
To: arzuagapozosergio; +Cc: tytso, jack, linux-ext4, syzbot+25d45c0bd716f2b794a7
On Sun 22-11-26 04:37:32, arzuagapozosergio@gmail.com wrote:
> From: Ryuji Hiroshi <arzuagapozosergio@gmail.com>
>
> When setting an xattr without XATTR_CREATE, ext4 reserves credits for
> releasing an old ea_inode even when the attribute does not exist. On a
> 1 KiB-block filesystem with a minimum journal, this can request more
> credits than JBD2 permits, triggering a warning before the operation
> fails.
OK.
> Look up the existing attribute and account for the work done when its
> ea_inode reference is dropped. The final iput() and block truncation are
> deferred to a separate transaction. Check the credit limit after reserving
> space for the default JBD2 revoke records, and repeat the estimate under
> the xattr write lock before modifying the attribute. When the target is
> found in the inode body, also read the external xattr block so shared
> block EA inode references are included in the locked estimate.
So I think that since a54778243e88 ("ext4: convert all EA inode iput()
calls to ext4_put_ea_inode()") the removal credits reserved in
__ext4_xattr_set_credits() shouldn't be actually needed. Can't we just drop
the '!is_create' branch (and the function argument) from
__ext4_xattr_set_credits() and be done with it? Of course this would need
some careful testing but I think it should work...
Honza
>
> Add EXT4 selftests for the original no-XATTR_CREATE credit-estimation
> path, large xattr creation and replacement, preservation after a rejected
> XATTR_CREATE operation, shared external xattr block cloning, removal, and
> a small user xattr round trip.
>
> Fixes: dec214d00e0d ("ext4: xattr inode deduplication")
> Reported-by: syzbot+25d45c0bd716f2b794a7@syzkaller.appspotmail.com
> Link: https://syzbot.org/bug?extid=25d45c0bd716f2b794a7
> Assisted-by: Codex:GPT-6
> Signed-off-by: Ryuji Hiroshi <arzuagapozosergio@gmail.com>
> ---
> MAINTAINERS | 1 +
> fs/ext4/acl.c | 7 +-
> fs/ext4/crypto.c | 5 +-
> fs/ext4/ialloc.c | 6 +-
> fs/ext4/xattr.c | 174 +++++++++----
> fs/ext4/xattr.h | 7 +-
> fs/jbd2/transaction.c | 9 +
> include/linux/jbd2.h | 2 +
> tools/testing/selftests/Makefile | 1 +
> .../selftests/filesystems/ext4/.gitignore | 2 +
> .../selftests/filesystems/ext4/Makefile | 6 +
> .../ext4/ext4_xattr_journal_credits.c | 244 ++++++++++++++++++
> .../ext4/ext4_xattr_journal_credits.sh | 56 ++++
> 13 files changed, 455 insertions(+), 65 deletions(-)
> create mode 100644 tools/testing/selftests/filesystems/ext4/.gitignore
> create mode 100644 tools/testing/selftests/filesystems/ext4/Makefile
> create mode 100644 tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
> create mode 100755 tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
>
> Changes in v3:
> - Re-read the external xattr block for the locked estimate when the target
> attribute is in the inode body.
> - Replace the pre-VFS E2BIG check with the original no-XATTR_CREATE case,
> preservation after XATTR_CREATE rejection, and shared-block clone coverage.
> - Report setup errors separately, ignore the generated test binary, and add
> the EXT4 selftest path to MAINTAINERS.
>
> Tests:
> - make -j4 bzImage
> - make -C tools/testing/selftests/filesystems/ext4 clean all
> - sh -n tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
> - Booted the patched kernel in QEMU/KVM; all 10 selftest cases passed on a
> 1 KiB-block ea_inode filesystem with a 1 MiB journal; QEMU shut down cleanly.
> - Confirmed the shared-block fixture used the same external xattr block with
> refcount 2 before cloning.
> - e2fsck -fn passed after the guest unmounted the image.
> - The host kselftest runner returned KSFT_SKIP because the unprivileged host
> cannot mount the disposable filesystem; the binary passed in the VM as root.
>
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 2ac5128b7..1debcbeb0 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -10131,6 +10131,7 @@ F: Documentation/filesystems/ext4/
> F: fs/ext4/
> F: include/trace/events/ext4.h
> F: include/uapi/linux/ext4.h
> +F: tools/testing/selftests/filesystems/ext4/
>
> Extended Verification Module (EVM)
> M: Mimi Zohar <zohar@linux.ibm.com>
> diff --git a/fs/ext4/acl.c b/fs/ext4/acl.c
> index 59fac55a2..07b351694 100644
> --- a/fs/ext4/acl.c
> +++ b/fs/ext4/acl.c
> @@ -239,8 +239,11 @@ ext4_set_acl(const struct mnt_idmap *idmap, struct dentry *dentry,
> if (error)
> return error;
> retry:
> - error = ext4_xattr_set_credits(inode, acl_size, false /* is_create */,
> - &credits);
> + error = ext4_xattr_set_credits(inode,
> + type == ACL_TYPE_ACCESS ?
> + EXT4_XATTR_INDEX_POSIX_ACL_ACCESS :
> + EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT,
> + "", acl_size, false /* is_create */, &credits);
> if (error)
> return error;
>
> diff --git a/fs/ext4/crypto.c b/fs/ext4/crypto.c
> index 318b2eaea..98afd33b0 100644
> --- a/fs/ext4/crypto.c
> +++ b/fs/ext4/crypto.c
> @@ -203,8 +203,9 @@ static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
> if (res)
> return res;
> retry:
> - res = ext4_xattr_set_credits(inode, len, false /* is_create */,
> - &credits);
> + res = ext4_xattr_set_credits(inode, EXT4_XATTR_INDEX_ENCRYPTION,
> + EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
> + len, false /* is_create */, &credits);
> if (res)
> return res;
>
> diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
> index 527f1f703..927b2e8f0 100644
> --- a/fs/ext4/ialloc.c
> +++ b/fs/ext4/ialloc.c
> @@ -889,7 +889,7 @@ static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
> nblocks += (S_ISDIR(mode) ? 2 : 1) *
> __ext4_xattr_set_credits(sb, NULL /* inode */,
> NULL /* block_bh */, acl_size,
> - true /* is_create */);
> + true /* is_create */, 0);
> posix_acl_release(p);
> }
> #endif
> @@ -908,7 +908,7 @@ static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
> nblocks += num_security_xattrs *
> __ext4_xattr_set_credits(sb, NULL /* inode */,
> NULL /* block_bh */, 1024,
> - true /* is_create */);
> + true /* is_create */, 0);
> }
> #endif
> if (encrypt)
> @@ -916,7 +916,7 @@ static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
> NULL /* inode */,
> NULL /* block_bh */,
> FSCRYPT_SET_CONTEXT_MAX_SIZE,
> - true /* is_create */);
> + true /* is_create */, 0);
> return nblocks;
> }
>
> diff --git a/fs/ext4/xattr.c b/fs/ext4/xattr.c
> index 5c310747b..21061850d 100644
> --- a/fs/ext4/xattr.c
> +++ b/fs/ext4/xattr.c
> @@ -949,7 +949,7 @@ static void ext4_xattr_inode_free_quota(struct inode *parent,
>
> int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
> struct buffer_head *block_bh, size_t value_len,
> - bool is_create)
> + bool is_create, bool old_ea_inode)
> {
> int credits;
> int blocks;
> @@ -998,23 +998,14 @@ int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
> /* Blocks themselves. */
> credits += blocks;
>
> - if (!is_create) {
> - /* Dereference ea_inode holding old xattr value.
> - * Old ea_inode, inode map, block bitmap, group descriptor.
> - */
> - credits += 4;
> -
> - /* Data blocks for old ea_inode. */
> - blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits;
> -
> - /* Indirection block or one level of extent tree for old
> - * ea_inode.
> - */
> - blocks += 1;
> -
> - /* Block bitmap and group descriptor updates for each block. */
> - credits += blocks * 2;
> - }
> + /*
> + * Replacing an ea_inode xattr only drops its reference here. The
> + * final iput() and block truncation happen asynchronously in a separate
> + * transaction, so charge only for updating the ea_inode and, when its
> + * reference count reaches zero, adding it to the orphan list.
> + */
> + if (!is_create && old_ea_inode)
> + credits += 2;
>
> /* We may need to clone the existing xattr block in which case we need
> * to increment ref counts for existing ea_inodes referenced by it.
> @@ -2375,29 +2366,6 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
>
> ext4_write_lock_xattr(inode, &no_expand);
>
> - /* Check journal credits under write lock. */
> - if (ext4_handle_valid(handle)) {
> - struct buffer_head *bh;
> - int credits;
> -
> - bh = ext4_xattr_get_block(inode);
> - if (IS_ERR(bh)) {
> - error = PTR_ERR(bh);
> - goto cleanup;
> - }
> -
> - credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
> - value_len,
> - flags & XATTR_CREATE);
> - brelse(bh);
> -
> - if (jbd2_handle_buffer_credits(handle) < credits) {
> - error = -ENOSPC;
> - goto cleanup;
> - }
> - WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS));
> - }
> -
> error = ext4_reserve_inode_write(handle, inode, &is.iloc);
> if (error)
> goto cleanup;
> @@ -2415,6 +2383,39 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
> error = ext4_xattr_block_find(inode, &i, &bs);
> if (error)
> goto cleanup;
> +
> + /* Check journal credits under write lock using the old ea_inode state. */
> + if (ext4_handle_valid(handle)) {
> + struct ext4_xattr_entry *entry = NULL;
> + bool old_ea_inode = false;
> + int credits;
> +
> + if (!is.s.not_found)
> + entry = is.s.here;
> + else if (!bs.s.not_found)
> + entry = bs.s.here;
> + if (entry && entry->e_value_inum &&
> + !(flags & XATTR_CREATE))
> + old_ea_inode = true;
> + if (!bs.bh) {
> + struct buffer_head *bh = ext4_xattr_get_block(inode);
> +
> + if (IS_ERR(bh)) {
> + error = PTR_ERR(bh);
> + goto cleanup;
> + }
> + bs.bh = bh;
> + }
> + credits = __ext4_xattr_set_credits(inode->i_sb, inode, bs.bh,
> + value_len,
> + flags & XATTR_CREATE,
> + old_ea_inode);
> + if (jbd2_handle_buffer_credits(handle) < credits) {
> + error = -ENOSPC;
> + goto cleanup;
> + }
> + WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS));
> + }
> if (is.s.not_found && bs.s.not_found) {
> error = -ENODATA;
> if (flags & XATTR_REPLACE)
> @@ -2500,28 +2501,91 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
> return error;
> }
>
> -int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
> +int ext4_xattr_set_credits(struct inode *inode, int name_index,
> + const char *name, size_t value_len,
> bool is_create, int *credits)
> {
> - struct buffer_head *bh;
> - int err;
> + struct ext4_xattr_info i = {
> + .name_index = name_index,
> + .name = name,
> + };
> + struct ext4_xattr_ibody_find is = {
> + .s = { .not_found = -ENODATA, },
> + };
> + struct ext4_xattr_block_find bs = {
> + .s = { .not_found = -ENODATA, },
> + };
> + struct ext4_xattr_entry *entry = NULL;
> + journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
> + bool old_ea_inode = false;
> + int max_credits, revoke_credits, err;
>
> *credits = 0;
> + if (!name)
> + return -EINVAL;
> + if (strlen(name) > 255)
> + return -ERANGE;
>
> - if (!EXT4_SB(inode->i_sb)->s_journal)
> + if (!journal)
> return 0;
>
> + /*
> + * The old ea_inode reference is charged only when the target attribute
> + * currently points to one. xattr_sem is dropped before the transaction
> + * starts, so ext4_xattr_set_handle() repeats this check under the write
> + * lock.
> + */
> down_read(&EXT4_I(inode)->xattr_sem);
>
> - bh = ext4_xattr_get_block(inode);
> - if (IS_ERR(bh)) {
> - err = PTR_ERR(bh);
> - } else {
> - *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
> - value_len, is_create);
> - brelse(bh);
> - err = 0;
> + if (ext4_has_feature_ea_inode(inode->i_sb)) {
> + struct buffer_head *bh;
> +
> + if (is_create) {
> + /* The target lookup is unnecessary, but block refs matter. */
> + bh = ext4_xattr_get_block(inode);
> + if (IS_ERR(bh)) {
> + err = PTR_ERR(bh);
> + goto out_unlock;
> + }
> + bs.bh = bh;
> + } else {
> + err = ext4_get_inode_loc(inode, &is.iloc);
> + if (err)
> + goto out_unlock;
> + err = ext4_xattr_ibody_find(inode, &i, &is);
> + if (err)
> + goto out;
> + if (!is.s.not_found) {
> + entry = is.s.here;
> + } else {
> + err = ext4_xattr_block_find(inode, &i, &bs);
> + if (err)
> + goto out;
> + if (!bs.s.not_found)
> + entry = bs.s.here;
> + }
> + if (!bs.bh) {
> + bh = ext4_xattr_get_block(inode);
> + if (IS_ERR(bh)) {
> + err = PTR_ERR(bh);
> + goto out;
> + }
> + bs.bh = bh;
> + }
> + }
> }
> + if (!is_create && entry && entry->e_value_inum)
> + old_ea_inode = true;
> + *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bs.bh,
> + value_len, is_create,
> + old_ea_inode);
> + revoke_credits = ext4_trans_default_revoke_credits(inode->i_sb);
> + max_credits = jbd2_journal_get_max_transaction_buffers(journal, revoke_credits);
> + err = *credits > max_credits ? -ENOSPC : 0;
> +out:
> + brelse(is.iloc.bh);
> + brelse(bs.bh);
> +out_unlock:
>
> up_read(&EXT4_I(inode)->xattr_sem);
> return err;
> @@ -2549,8 +2613,8 @@ ext4_xattr_set(struct inode *inode, int name_index, const char *name,
> return error;
>
> retry:
> - error = ext4_xattr_set_credits(inode, value_len, flags & XATTR_CREATE,
> - &credits);
> + error = ext4_xattr_set_credits(inode, name_index, name, value_len,
> + flags & XATTR_CREATE, &credits);
> if (error)
> return error;
>
> diff --git a/fs/ext4/xattr.h b/fs/ext4/xattr.h
> index 821dc6a50..47fd2a7c5 100644
> --- a/fs/ext4/xattr.h
> +++ b/fs/ext4/xattr.h
> @@ -175,11 +175,12 @@ extern ssize_t ext4_listxattr(struct dentry *, char *, size_t);
> extern int ext4_xattr_get(struct inode *, int, const char *, void *, size_t);
> extern int ext4_xattr_set(struct inode *, int, const char *, const void *, size_t, int);
> extern int ext4_xattr_set_handle(handle_t *, struct inode *, int, const char *, const void *, size_t, int);
> -extern int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
> - bool is_create, int *credits);
> +int ext4_xattr_set_credits(struct inode *inode, int name_index,
> + const char *name, size_t value_len,
> + bool is_create, int *credits);
> extern int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
> struct buffer_head *block_bh, size_t value_len,
> - bool is_create);
> + bool is_create, bool old_ea_inode);
>
> extern int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
> int extra_credits);
> diff --git a/fs/jbd2/transaction.c b/fs/jbd2/transaction.c
> index 85d84d909..5fe49f118 100644
> --- a/fs/jbd2/transaction.c
> +++ b/fs/jbd2/transaction.c
> @@ -194,6 +194,15 @@ static int jbd2_max_user_trans_buffers(journal_t *journal)
> journal->j_transaction_overhead_buffers;
> }
>
> +/* Maximum buffer credits available after reserving @revoke_records. */
> +int jbd2_journal_get_max_transaction_buffers(journal_t *journal,
> + int revoke_records)
> +{
> + return jbd2_max_user_trans_buffers(journal) -
> + DIV_ROUND_UP(revoke_records, journal->j_revoke_records_per_block);
> +}
> +EXPORT_SYMBOL(jbd2_journal_get_max_transaction_buffers);
> +
> /*
> * Wait until we can add credits for handle to the running transaction. Called
> * with j_state_lock held for reading. Returns 0 if handle joined the running
> diff --git a/include/linux/jbd2.h b/include/linux/jbd2.h
> index e3886963b..a72f14440 100644
> --- a/include/linux/jbd2.h
> +++ b/include/linux/jbd2.h
> @@ -1523,6 +1523,8 @@ static inline handle_t *journal_current_handle(void)
> */
>
> extern handle_t *jbd2_journal_start(journal_t *, int nblocks);
> +int jbd2_journal_get_max_transaction_buffers(journal_t *journal,
> + int revoke_records);
> extern handle_t *jbd2__journal_start(journal_t *, int blocks, int rsv_blocks,
> int revoke_records, gfp_t gfp_mask,
> unsigned int type, unsigned int line_no);
> diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
> index 07c3719f8..c16b1132e 100644
> --- a/tools/testing/selftests/Makefile
> +++ b/tools/testing/selftests/Makefile
> @@ -37,6 +37,7 @@ TARGETS += filesystems/binderfs
> TARGETS += filesystems/configfs
> TARGETS += filesystems/epoll
> TARGETS += filesystems/eventfd
> +TARGETS += filesystems/ext4
> TARGETS += filesystems/failfs
> TARGETS += filesystems/fat
> TARGETS += filesystems/file_stressor
> diff --git a/tools/testing/selftests/filesystems/ext4/.gitignore b/tools/testing/selftests/filesystems/ext4/.gitignore
> new file mode 100644
> index 000000000..d3678b52d
> --- /dev/null
> +++ b/tools/testing/selftests/filesystems/ext4/.gitignore
> @@ -0,0 +1,2 @@
> +# SPDX-License-Identifier: GPL-2.0
> +/ext4_xattr_journal_credits
> diff --git a/tools/testing/selftests/filesystems/ext4/Makefile b/tools/testing/selftests/filesystems/ext4/Makefile
> new file mode 100644
> index 000000000..90569a1cc
> --- /dev/null
> +++ b/tools/testing/selftests/filesystems/ext4/Makefile
> @@ -0,0 +1,6 @@
> +# SPDX-License-Identifier: GPL-2.0
> +
> +TEST_GEN_PROGS_EXTENDED := ext4_xattr_journal_credits
> +TEST_PROGS := ext4_xattr_journal_credits.sh
> +
> +include ../../lib.mk
> diff --git a/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
> new file mode 100644
> index 000000000..313e81767
> --- /dev/null
> +++ b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
> @@ -0,0 +1,244 @@
> +// SPDX-License-Identifier: GPL-2.0
> +#define _GNU_SOURCE
> +#include <errno.h>
> +#include <fcntl.h>
> +#include <stdbool.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#include <sys/xattr.h>
> +#include <unistd.h>
> +
> +#include "../../kselftest.h"
> +
> +#define LARGE_VALUE_SIZE 65296
> +#define FILLER_VALUE_SIZE 32
> +#define TEST_COUNT 10
> +
> +static const char large_name[] = "user.ext4-credit-large";
> +static const char small_name[] = "user.ext4-credit-small";
> +static const char target_name[] = "user.ext4-credit-target";
> +static const char filler_name[] = "user.ext4-credit-filler";
> +
> +static void result(bool passed, const char *name)
> +{
> + ksft_test_result(passed, "%s\n", name);
> +}
> +
> +static bool xattr_matches(const char *path, const char *name,
> + char *buffer, const void *expected, size_t size)
> +{
> + ssize_t len;
> +
> + errno = 0;
> + len = getxattr(path, name, buffer, size);
> + if (len < 0) {
> + ksft_print_msg("getxattr(%s): %s\n", name, strerror(errno));
> + return false;
> + }
> + return len == size && !memcmp(buffer, expected, size);
> +}
> +
> +static int create_file(const char *path)
> +{
> + int fd = open(path, O_CREAT | O_EXCL | O_RDWR, 0600);
> + int error;
> +
> + if (fd < 0)
> + return -1;
> + if (!close(fd))
> + return 0;
> + error = errno;
> + errno = error;
> + return -1;
> +}
> +
> +static bool set_xattr(const char *path, const char *name,
> + const void *value, size_t size, int flags)
> +{
> + errno = 0;
> + if (!setxattr(path, name, value, size, flags))
> + return true;
> + ksft_print_msg("setxattr(%s): %s\n", name, strerror(errno));
> + return false;
> +}
> +
> +static void report_setup_error(const char *what, unsigned int skip_count)
> +{
> + int saved_errno = errno;
> + unsigned int i;
> +
> + ksft_test_result_error("setup %s: %s\n", what,
> + strerror(saved_errno));
> + for (i = 0; i < skip_count; i++)
> + ksft_test_result_skip("setup failed\n");
> +}
> +
> +static bool prepare_shared_file(const char *path, const char *large_value,
> + const char *filler, char target_value)
> +{
> + return create_file(path) == 0 &&
> + set_xattr(path, target_name, &target_value, sizeof(target_value),
> + XATTR_CREATE) &&
> + set_xattr(path, filler_name, filler, FILLER_VALUE_SIZE,
> + XATTR_CREATE) &&
> + set_xattr(path, large_name, large_value, LARGE_VALUE_SIZE,
> + XATTR_CREATE);
> +}
> +
> +static bool prepare_shared_block(const char *path_a, const char *path_b,
> + char *large_value, char *readback)
> +{
> + char target_value = 0x11;
> + char filler[FILLER_VALUE_SIZE];
> + char target_readback;
> +
> + memset(filler, 0x6c, sizeof(filler));
> + memset(large_value, 0x7e, LARGE_VALUE_SIZE);
> + large_value[0] = 0x42;
> +
> + if (!prepare_shared_file(path_a, large_value, filler, target_value) ||
> + !prepare_shared_file(path_b, large_value, filler, target_value))
> + return false;
> +
> + return xattr_matches(path_a, large_name, readback, large_value,
> + LARGE_VALUE_SIZE) &&
> + xattr_matches(path_b, large_name, readback, large_value,
> + LARGE_VALUE_SIZE) &&
> + xattr_matches(path_a, target_name, &target_readback,
> + &target_value, sizeof(target_value)) &&
> + xattr_matches(path_b, target_name, &target_readback,
> + &target_value, sizeof(target_value));
> +}
> +
> +int main(int argc, char **argv)
> +{
> + char path[4096], shared_a[4096], shared_b[4096];
> + char *value = NULL, *expected = NULL, *readback = NULL;
> + char target_new[64];
> + static const char small_value[] = "small-xattr";
> + char small_value_readback[sizeof(small_value)];
> + char target_old = 0x11;
> + int fd, path_len, shared_len;
> + bool created = false, replaced = false, shared_ready = false;
> + bool shared_replaced = false;
> + int ret;
> +
> + ksft_print_header();
> + ksft_set_plan(TEST_COUNT);
> + if (argc != 2)
> + ksft_exit_fail_msg("Usage: %s MOUNTPOINT\n", argv[0]);
> +
> + path_len = snprintf(path, sizeof(path), "%s/credit-test", argv[1]);
> + shared_len = snprintf(shared_a, sizeof(shared_a), "%s/shared-a", argv[1]);
> + if (path_len < 0 || (size_t)path_len >= sizeof(path) ||
> + shared_len < 0 || (size_t)shared_len >= sizeof(shared_a))
> + ksft_exit_fail_msg("Mount path is too long\n");
> + shared_len = snprintf(shared_b, sizeof(shared_b), "%s/shared-b", argv[1]);
> + if (shared_len < 0 || (size_t)shared_len >= sizeof(shared_b))
> + ksft_exit_fail_msg("Mount path is too long\n");
> +
> + fd = open(path, O_CREAT | O_EXCL | O_RDWR, 0600);
> + if (fd < 0) {
> + report_setup_error("create test file", TEST_COUNT - 1);
> + ksft_finished();
> + }
> + if (close(fd) < 0) {
> + report_setup_error("close test file", TEST_COUNT - 1);
> + ksft_finished();
> + }
> +
> + value = malloc(LARGE_VALUE_SIZE);
> + expected = malloc(LARGE_VALUE_SIZE);
> + readback = malloc(LARGE_VALUE_SIZE);
> + if (!value || !expected || !readback) {
> + report_setup_error("allocate xattr buffers", TEST_COUNT - 1);
> + ksft_finished();
> + }
> +
> + memset(value, 0x5a, LARGE_VALUE_SIZE);
> + value[0] = 1;
> + /* Exercise the original no-XATTR_CREATE credit-estimation path. */
> + created = set_xattr(path, large_name, value, LARGE_VALUE_SIZE, 0);
> + result(created, "large xattr creation");
> + if (created) {
> + result(xattr_matches(path, large_name, readback, value,
> + LARGE_VALUE_SIZE), "large xattr readback");
> +
> + memset(value, 0xa5, LARGE_VALUE_SIZE);
> + value[0] = 2;
> + replaced = set_xattr(path, large_name, value,
> + LARGE_VALUE_SIZE, XATTR_REPLACE);
> + result(replaced, "large xattr replacement");
> + } else {
> + ksft_test_result_skip("readback needs the created xattr\n");
> + ksft_test_result_skip("replacement needs the created xattr\n");
> + }
> +
> + if (replaced) {
> + result(xattr_matches(path, large_name, readback, value,
> + LARGE_VALUE_SIZE), "replacement readback");
> +
> + memcpy(expected, value, LARGE_VALUE_SIZE);
> + memset(value, 0x3c, LARGE_VALUE_SIZE);
> + value[0] = 3;
> + errno = 0;
> + ret = setxattr(path, large_name, value, LARGE_VALUE_SIZE,
> + XATTR_CREATE);
> + result(ret < 0 && errno == EEXIST &&
> + xattr_matches(path, large_name, readback, expected,
> + LARGE_VALUE_SIZE),
> + "XATTR_CREATE rejection preserves the old value");
> + } else {
> + ksft_test_result_skip("replacement readback needs a successful replacement\n");
> + ksft_test_result_skip("XATTR_CREATE check needs a successful replacement\n");
> + }
> +
> + if (prepare_shared_block(shared_a, shared_b, value, readback)) {
> + shared_ready = true;
> + ksft_test_result_pass("shared external xattr block fixture\n");
> + } else {
> + report_setup_error("prepare shared xattr block", 2);
> + }
> +
> + if (shared_ready) {
> + memset(target_new, 0x22, sizeof(target_new));
> + shared_replaced = set_xattr(shared_a, target_name, target_new,
> + sizeof(target_new), XATTR_REPLACE);
> + result(shared_replaced,
> + "replace inode-body xattr with shared external block present");
> +
> + result(xattr_matches(shared_a, target_name, readback,
> + shared_replaced ? target_new : &target_old,
> + shared_replaced ? sizeof(target_new) :
> + sizeof(target_old)) &&
> + xattr_matches(shared_b, target_name, readback,
> + &target_old, sizeof(target_old)) &&
> + xattr_matches(shared_b, large_name, readback, value,
> + LARGE_VALUE_SIZE),
> + "xattr block clone preserves both files' values");
> + } else {
> + ksft_test_result_skip("shared-block replacement needs its fixture\n");
> + ksft_test_result_skip("shared-block readback needs its fixture\n");
> + }
> +
> + if (created) {
> + ret = removexattr(path, large_name);
> + result(ret == 0, "large xattr removal");
> + } else {
> + ksft_test_result_skip("removal needs the created xattr\n");
> + }
> +
> + ret = setxattr(path, small_name, small_value, sizeof(small_value), 0);
> + result(ret == 0 &&
> + getxattr(path, small_name, small_value_readback,
> + sizeof(small_value_readback)) == sizeof(small_value) &&
> + !memcmp(small_value, small_value_readback, sizeof(small_value)) &&
> + removexattr(path, small_name) == 0,
> + "small user xattr round trip");
> +
> + free(readback);
> + free(expected);
> + free(value);
> + ksft_finished();
> +}
> diff --git a/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
> new file mode 100755
> index 000000000..365bef975
> --- /dev/null
> +++ b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
> @@ -0,0 +1,56 @@
> +#!/bin/sh
> +# SPDX-License-Identifier: GPL-2.0
> +
> +KSFT_SKIP=4
> +
> +skip()
> +{
> + printf 'SKIP: %s\n' "$1"
> + exit "$KSFT_SKIP"
> +}
> +
> +[ "$(id -u)" -eq 0 ] || skip "requires root to mount a disposable EXT4 image"
> +
> +for command in mkfs.ext4 mount umount truncate e2fsck losetup grep; do
> + command -v "$command" >/dev/null 2>&1 || skip "missing $command"
> +done
> +
> +grep -qw ext4 /proc/filesystems || skip "EXT4 is not available in this kernel"
> +losetup -f >/dev/null 2>&1 || skip "no loop device is available"
> +
> +test_dir=$(mktemp -d) || exit 1
> +mounted=0
> +
> +cleanup()
> +{
> + if [ "$mounted" -eq 1 ]; then
> + umount "$test_dir/mnt" || true
> + fi
> + rm -rf "$test_dir"
> +}
> +trap cleanup EXIT INT TERM
> +
> +truncate -s 32M "$test_dir/fs.img" || exit 1
> +if ! mkfs.ext4 -q -F -b 1024 -O ea_inode -J size=1 "$test_dir/fs.img" \
> + 2>"$test_dir/mkfs.err"; then
> + cat "$test_dir/mkfs.err" >&2
> + skip "mkfs.ext4 does not support the ea_inode test image"
> +fi
> +mkdir "$test_dir/mnt" || exit 1
> +if ! mount -t ext4 -o loop "$test_dir/fs.img" "$test_dir/mnt" \
> + 2>"$test_dir/mount.err"; then
> + cat "$test_dir/mount.err" >&2
> + if grep -Eqi 'permission denied|operation not permitted|not permitted' \
> + "$test_dir/mount.err"; then
> + skip "mounting a loop image is not permitted"
> + fi
> + exit 1
> +fi
> +mounted=1
> +
> +test_bin="$(dirname "$0")/ext4_xattr_journal_credits"
> +"$test_bin" "$test_dir/mnt" || exit $?
> +
> +umount "$test_dir/mnt" || exit 1
> +mounted=0
> +e2fsck -fn "$test_dir/fs.img"
>
> base-commit: c44104f3642da720164042623e4832836aefdd8e
> --
> 2.56.1
>
--
Jan Kara <jack@suse.com>
SUSE Labs, CR
^ permalink raw reply [flat|nested] 22+ messages in thread* [PATCH v4] ext4: bound xattr journal credits to transaction limit
2026-11-22 9:37 ` [PATCH v3] " arzuagapozosergio
2026-11-22 9:53 ` sashiko-bot
2026-11-24 12:38 ` Jan Kara
@ 2026-11-25 4:51 ` arzuagapozosergio
2026-11-25 5:09 ` sashiko-bot
2026-11-25 5:19 ` [PATCH v5] " arzuagapozosergio
2 siblings, 2 replies; 22+ messages in thread
From: arzuagapozosergio @ 2026-11-25 4:51 UTC (permalink / raw)
To: tytso; +Cc: jack, linux-ext4, syzbot+25d45c0bd716f2b794a7, arzuagapozosergio
From: Ryuji Hiroshi <arzuagapozosergio@gmail.com>
When setting an xattr without XATTR_CREATE, ext4 reserves credits for
releasing an old ea_inode even when the attribute does not exist. On a
1 KiB-block filesystem with a minimum journal, this can request more
credits than JBD2 permits, triggering a warning before the operation
fails.
Drop the obsolete credits for releasing an old ea_inode. Since
ext4_put_ea_inode() defers the final iput to a worker, freeing the old
value's blocks does not happen in the current transaction. Continue to
count ea_inode references in the external xattr block for the case where
it must be cloned, and check the estimate against the transaction limit
after reserving the default JBD2 revoke credits. Repeat the estimate under
the xattr write lock before modifying the attribute.
When an identical xattr block is reused from the cache, drop the
provisional ea_inode reference because the reused block already contains
that reference. Add an EXT4 selftest covering the original
no-XATTR_CREATE credit path, large xattr creation and replacement,
XATTR_CREATE rejection, shared external block cloning with an ea_inode
entry, removal, and a small user xattr round trip.
Fixes: dec214d00e0d ("ext4: xattr inode deduplication")
Reported-by: syzbot+25d45c0bd716f2b794a7@syzkaller.appspotmail.com
Link: https://syzbot.org/bug?extid=25d45c0bd716f2b794a7
Assisted-by: Codex:GPT-6
Signed-off-by: Ryuji Hiroshi <arzuagapozosergio@gmail.com>
Changes in v4:
- Remove obsolete credits for dropping an old ea_inode, as suggested by
Jan Kara, and remove the target xattr lookup that was only needed for
that estimate.
- Drop the provisional ea_inode reference when the xattr block cache
reuses an identical block.
- Make the shared-block fixture store the large ea_inode xattr in the
external block; prevent duplicate skips and false passes on errors.
- Fix the test image inode size so the fixture layout is deterministic.
Tests:
- make -j4 bzImage
- make -C tools/testing/selftests/filesystems/ext4 clean all
- sh -n tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
- Booted the v4 kernel in QEMU/KVM with a 1 KiB-block ea_inode filesystem
and 1 MiB journal; all 10 TAP tests passed.
- e2fsck -fn passed after the guest unmounted the filesystem.
---
MAINTAINERS | 1 +
fs/ext4/acl.c | 3 +-
fs/ext4/crypto.c | 3 +-
fs/ext4/ialloc.c | 9 +-
fs/ext4/xattr.c | 117 ++++-----
fs/ext4/xattr.h | 8 +-
fs/jbd2/transaction.c | 9 +
include/linux/jbd2.h | 2 +
tools/testing/selftests/Makefile | 1 +
.../selftests/filesystems/ext4/.gitignore | 2 +
.../selftests/filesystems/ext4/Makefile | 6 +
.../ext4/ext4_xattr_journal_credits.c | 241 ++++++++++++++++++
.../ext4/ext4_xattr_journal_credits.sh | 56 ++++
13 files changed, 386 insertions(+), 72 deletions(-)
create mode 100644 tools/testing/selftests/filesystems/ext4/.gitignore
create mode 100644 tools/testing/selftests/filesystems/ext4/Makefile
create mode 100644 tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
create mode 100755 tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
diff --git a/MAINTAINERS b/MAINTAINERS
index 2ac5128b7..1debcbeb0 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -10131,6 +10131,7 @@ F: Documentation/filesystems/ext4/
F: fs/ext4/
F: include/trace/events/ext4.h
F: include/uapi/linux/ext4.h
+F: tools/testing/selftests/filesystems/ext4/
Extended Verification Module (EVM)
M: Mimi Zohar <zohar@linux.ibm.com>
diff --git a/fs/ext4/acl.c b/fs/ext4/acl.c
index 59fac55a2..167a410ef 100644
--- a/fs/ext4/acl.c
+++ b/fs/ext4/acl.c
@@ -239,8 +239,7 @@ ext4_set_acl(const struct mnt_idmap *idmap, struct dentry *dentry,
if (error)
return error;
retry:
- error = ext4_xattr_set_credits(inode, acl_size, false /* is_create */,
- &credits);
+ error = ext4_xattr_set_credits(inode, acl_size, &credits);
if (error)
return error;
diff --git a/fs/ext4/crypto.c b/fs/ext4/crypto.c
index 318b2eaea..c1275009e 100644
--- a/fs/ext4/crypto.c
+++ b/fs/ext4/crypto.c
@@ -203,8 +203,7 @@ static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
if (res)
return res;
retry:
- res = ext4_xattr_set_credits(inode, len, false /* is_create */,
- &credits);
+ res = ext4_xattr_set_credits(inode, len, &credits);
if (res)
return res;
diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
index 527f1f703..82623f563 100644
--- a/fs/ext4/ialloc.c
+++ b/fs/ext4/ialloc.c
@@ -888,8 +888,7 @@ static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
nblocks += (S_ISDIR(mode) ? 2 : 1) *
__ext4_xattr_set_credits(sb, NULL /* inode */,
- NULL /* block_bh */, acl_size,
- true /* is_create */);
+ NULL /* block_bh */, acl_size);
posix_acl_release(p);
}
#endif
@@ -907,16 +906,14 @@ static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
*/
nblocks += num_security_xattrs *
__ext4_xattr_set_credits(sb, NULL /* inode */,
- NULL /* block_bh */, 1024,
- true /* is_create */);
+ NULL /* block_bh */, 1024);
}
#endif
if (encrypt)
nblocks += __ext4_xattr_set_credits(sb,
NULL /* inode */,
NULL /* block_bh */,
- FSCRYPT_SET_CONTEXT_MAX_SIZE,
- true /* is_create */);
+ FSCRYPT_SET_CONTEXT_MAX_SIZE);
return nblocks;
}
diff --git a/fs/ext4/xattr.c b/fs/ext4/xattr.c
index 5c310747b..b8e1d5017 100644
--- a/fs/ext4/xattr.c
+++ b/fs/ext4/xattr.c
@@ -948,8 +948,7 @@ static void ext4_xattr_inode_free_quota(struct inode *parent,
}
int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
- struct buffer_head *block_bh, size_t value_len,
- bool is_create)
+ struct buffer_head *block_bh, size_t value_len)
{
int credits;
int blocks;
@@ -998,24 +997,6 @@ int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
/* Blocks themselves. */
credits += blocks;
- if (!is_create) {
- /* Dereference ea_inode holding old xattr value.
- * Old ea_inode, inode map, block bitmap, group descriptor.
- */
- credits += 4;
-
- /* Data blocks for old ea_inode. */
- blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits;
-
- /* Indirection block or one level of extent tree for old
- * ea_inode.
- */
- blocks += 1;
-
- /* Block bitmap and group descriptor updates for each block. */
- credits += blocks * 2;
- }
-
/* We may need to clone the existing xattr block in which case we need
* to increment ref counts for existing ea_inodes referenced by it.
*/
@@ -2109,6 +2090,17 @@ ext4_xattr_block_set(handle_t *handle, struct inode *inode,
new_bh);
if (error)
goto cleanup_dquot;
+ if (ea_inode) {
+ /* The reused block already holds this reference. */
+ error = ext4_xattr_inode_dec_ref(handle,
+ ea_inode);
+ if (error)
+ ext4_warning_inode(ea_inode,
+ "dec ref error=%d",
+ error);
+ ext4_put_ea_inode(ea_inode);
+ ea_inode = NULL;
+ }
}
mb_cache_entry_touch(ea_block_cache, ce);
mb_cache_entry_put(ea_block_cache, ce);
@@ -2375,29 +2367,6 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
ext4_write_lock_xattr(inode, &no_expand);
- /* Check journal credits under write lock. */
- if (ext4_handle_valid(handle)) {
- struct buffer_head *bh;
- int credits;
-
- bh = ext4_xattr_get_block(inode);
- if (IS_ERR(bh)) {
- error = PTR_ERR(bh);
- goto cleanup;
- }
-
- credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
- value_len,
- flags & XATTR_CREATE);
- brelse(bh);
-
- if (jbd2_handle_buffer_credits(handle) < credits) {
- error = -ENOSPC;
- goto cleanup;
- }
- WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS));
- }
-
error = ext4_reserve_inode_write(handle, inode, &is.iloc);
if (error)
goto cleanup;
@@ -2415,6 +2384,28 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
error = ext4_xattr_block_find(inode, &i, &bs);
if (error)
goto cleanup;
+
+ /* Check journal credits under write lock. */
+ if (ext4_handle_valid(handle)) {
+ int credits;
+
+ if (!bs.bh) {
+ struct buffer_head *bh = ext4_xattr_get_block(inode);
+
+ if (IS_ERR(bh)) {
+ error = PTR_ERR(bh);
+ goto cleanup;
+ }
+ bs.bh = bh;
+ }
+ credits = __ext4_xattr_set_credits(inode->i_sb, inode, bs.bh,
+ value_len);
+ if (jbd2_handle_buffer_credits(handle) < credits) {
+ error = -ENOSPC;
+ goto cleanup;
+ }
+ WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS));
+ }
if (is.s.not_found && bs.s.not_found) {
error = -ENODATA;
if (flags & XATTR_REPLACE)
@@ -2501,27 +2492,38 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
}
int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
- bool is_create, int *credits)
+ int *credits)
{
- struct buffer_head *bh;
- int err;
+ struct ext4_xattr_block_find bs = {
+ .s = { .not_found = -ENODATA, },
+ };
+ journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
+ int max_credits, revoke_credits, err;
*credits = 0;
-
- if (!EXT4_SB(inode->i_sb)->s_journal)
+ if (!journal)
return 0;
+ /* xattr_sem protects the block while we count its ea_inode references. */
down_read(&EXT4_I(inode)->xattr_sem);
- bh = ext4_xattr_get_block(inode);
- if (IS_ERR(bh)) {
- err = PTR_ERR(bh);
- } else {
- *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
- value_len, is_create);
- brelse(bh);
- err = 0;
+ if (ext4_has_feature_ea_inode(inode->i_sb)) {
+ struct buffer_head *bh;
+
+ bh = ext4_xattr_get_block(inode);
+ if (IS_ERR(bh)) {
+ err = PTR_ERR(bh);
+ goto out_unlock;
+ }
+ bs.bh = bh;
}
+ *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bs.bh,
+ value_len);
+ revoke_credits = ext4_trans_default_revoke_credits(inode->i_sb);
+ max_credits = jbd2_journal_get_max_transaction_buffers(journal, revoke_credits);
+ err = *credits > max_credits ? -ENOSPC : 0;
+ brelse(bs.bh);
+out_unlock:
up_read(&EXT4_I(inode)->xattr_sem);
return err;
@@ -2549,8 +2551,7 @@ ext4_xattr_set(struct inode *inode, int name_index, const char *name,
return error;
retry:
- error = ext4_xattr_set_credits(inode, value_len, flags & XATTR_CREATE,
- &credits);
+ error = ext4_xattr_set_credits(inode, value_len, &credits);
if (error)
return error;
diff --git a/fs/ext4/xattr.h b/fs/ext4/xattr.h
index 821dc6a50..eb6e85036 100644
--- a/fs/ext4/xattr.h
+++ b/fs/ext4/xattr.h
@@ -175,11 +175,11 @@ extern ssize_t ext4_listxattr(struct dentry *, char *, size_t);
extern int ext4_xattr_get(struct inode *, int, const char *, void *, size_t);
extern int ext4_xattr_set(struct inode *, int, const char *, const void *, size_t, int);
extern int ext4_xattr_set_handle(handle_t *, struct inode *, int, const char *, const void *, size_t, int);
-extern int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
- bool is_create, int *credits);
+int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
+ int *credits);
extern int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
- struct buffer_head *block_bh, size_t value_len,
- bool is_create);
+ struct buffer_head *block_bh,
+ size_t value_len);
extern int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
int extra_credits);
diff --git a/fs/jbd2/transaction.c b/fs/jbd2/transaction.c
index 85d84d909..5fe49f118 100644
--- a/fs/jbd2/transaction.c
+++ b/fs/jbd2/transaction.c
@@ -194,6 +194,15 @@ static int jbd2_max_user_trans_buffers(journal_t *journal)
journal->j_transaction_overhead_buffers;
}
+/* Maximum buffer credits available after reserving @revoke_records. */
+int jbd2_journal_get_max_transaction_buffers(journal_t *journal,
+ int revoke_records)
+{
+ return jbd2_max_user_trans_buffers(journal) -
+ DIV_ROUND_UP(revoke_records, journal->j_revoke_records_per_block);
+}
+EXPORT_SYMBOL(jbd2_journal_get_max_transaction_buffers);
+
/*
* Wait until we can add credits for handle to the running transaction. Called
* with j_state_lock held for reading. Returns 0 if handle joined the running
diff --git a/include/linux/jbd2.h b/include/linux/jbd2.h
index e3886963b..a72f14440 100644
--- a/include/linux/jbd2.h
+++ b/include/linux/jbd2.h
@@ -1523,6 +1523,8 @@ static inline handle_t *journal_current_handle(void)
*/
extern handle_t *jbd2_journal_start(journal_t *, int nblocks);
+int jbd2_journal_get_max_transaction_buffers(journal_t *journal,
+ int revoke_records);
extern handle_t *jbd2__journal_start(journal_t *, int blocks, int rsv_blocks,
int revoke_records, gfp_t gfp_mask,
unsigned int type, unsigned int line_no);
diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
index 07c3719f8..c16b1132e 100644
--- a/tools/testing/selftests/Makefile
+++ b/tools/testing/selftests/Makefile
@@ -37,6 +37,7 @@ TARGETS += filesystems/binderfs
TARGETS += filesystems/configfs
TARGETS += filesystems/epoll
TARGETS += filesystems/eventfd
+TARGETS += filesystems/ext4
TARGETS += filesystems/failfs
TARGETS += filesystems/fat
TARGETS += filesystems/file_stressor
diff --git a/tools/testing/selftests/filesystems/ext4/.gitignore b/tools/testing/selftests/filesystems/ext4/.gitignore
new file mode 100644
index 000000000..d3678b52d
--- /dev/null
+++ b/tools/testing/selftests/filesystems/ext4/.gitignore
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0
+/ext4_xattr_journal_credits
diff --git a/tools/testing/selftests/filesystems/ext4/Makefile b/tools/testing/selftests/filesystems/ext4/Makefile
new file mode 100644
index 000000000..90569a1cc
--- /dev/null
+++ b/tools/testing/selftests/filesystems/ext4/Makefile
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0
+
+TEST_GEN_PROGS_EXTENDED := ext4_xattr_journal_credits
+TEST_PROGS := ext4_xattr_journal_credits.sh
+
+include ../../lib.mk
diff --git a/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
new file mode 100644
index 000000000..3b4bb726d
--- /dev/null
+++ b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
@@ -0,0 +1,241 @@
+// SPDX-License-Identifier: GPL-2.0
+#define _GNU_SOURCE
+#include <errno.h>
+#include <fcntl.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/xattr.h>
+#include <unistd.h>
+
+#include "../../kselftest.h"
+
+#define LARGE_VALUE_SIZE 65296
+#define FILLER_VALUE_SIZE 4
+#define TEST_COUNT 10
+
+static const char large_name[] = "user.ext4-credit-large";
+static const char small_name[] = "user.ext4-credit-small";
+static const char target_name[] = "user.ext4-credit-target";
+static const char filler_name[] = "user.ext4-fill";
+
+static void result(bool passed, const char *name)
+{
+ ksft_test_result(passed, "%s\n", name);
+}
+
+static bool xattr_matches(const char *path, const char *name,
+ char *buffer, const void *expected, size_t size)
+{
+ ssize_t len;
+
+ errno = 0;
+ len = getxattr(path, name, buffer, size);
+ if (len < 0) {
+ ksft_print_msg("getxattr(%s): %s\n", name, strerror(errno));
+ return false;
+ }
+ return len == size && !memcmp(buffer, expected, size);
+}
+
+static int create_file(const char *path)
+{
+ int fd = open(path, O_CREAT | O_EXCL | O_RDWR, 0600);
+ int error;
+
+ if (fd < 0)
+ return -1;
+ if (!close(fd))
+ return 0;
+ error = errno;
+ errno = error;
+ return -1;
+}
+
+static bool set_xattr(const char *path, const char *name,
+ const void *value, size_t size, int flags)
+{
+ errno = 0;
+ if (!setxattr(path, name, value, size, flags))
+ return true;
+ ksft_print_msg("setxattr(%s): %s\n", name, strerror(errno));
+ return false;
+}
+
+static void report_setup_error(const char *what, unsigned int skip_count)
+{
+ int saved_errno = errno;
+ unsigned int i;
+
+ ksft_test_result_error("setup %s: %s\n", what,
+ strerror(saved_errno));
+ for (i = 0; i < skip_count; i++)
+ ksft_test_result_skip("setup failed\n");
+}
+
+static bool prepare_shared_file(const char *path, const char *large_value,
+ const char *filler, char target_value)
+{
+ /* Keep filler in inode body so large_name spills into the block. */
+ return create_file(path) == 0 &&
+ set_xattr(path, target_name, &target_value, sizeof(target_value),
+ XATTR_CREATE) &&
+ set_xattr(path, filler_name, filler, FILLER_VALUE_SIZE,
+ XATTR_CREATE) &&
+ set_xattr(path, large_name, large_value, LARGE_VALUE_SIZE,
+ XATTR_CREATE);
+}
+
+static bool prepare_shared_block(const char *path_a, const char *path_b,
+ char *large_value, char *readback)
+{
+ char target_value = 0x11;
+ char filler[FILLER_VALUE_SIZE];
+ char target_readback;
+
+ memset(filler, 0x6c, sizeof(filler));
+ memset(large_value, 0x7e, LARGE_VALUE_SIZE);
+ large_value[0] = 0x42;
+
+ if (!prepare_shared_file(path_a, large_value, filler, target_value) ||
+ !prepare_shared_file(path_b, large_value, filler, target_value))
+ return false;
+
+ return xattr_matches(path_a, large_name, readback, large_value,
+ LARGE_VALUE_SIZE) &&
+ xattr_matches(path_b, large_name, readback, large_value,
+ LARGE_VALUE_SIZE) &&
+ xattr_matches(path_a, target_name, &target_readback,
+ &target_value, sizeof(target_value)) &&
+ xattr_matches(path_b, target_name, &target_readback,
+ &target_value, sizeof(target_value));
+}
+
+int main(int argc, char **argv)
+{
+ char path[4096], shared_a[4096], shared_b[4096];
+ char *value = NULL, *expected = NULL, *readback = NULL;
+ char target_new[64];
+ static const char small_value[] = "small-xattr";
+ char small_value_readback[sizeof(small_value)];
+ char target_old = 0x11;
+ int fd, path_len, shared_len;
+ bool created = false, replaced = false, shared_ready = false;
+ bool shared_replaced = false;
+ int ret;
+
+ ksft_print_header();
+ ksft_set_plan(TEST_COUNT);
+ if (argc != 2)
+ ksft_exit_fail_msg("Usage: %s MOUNTPOINT\n", argv[0]);
+
+ path_len = snprintf(path, sizeof(path), "%s/credit-test", argv[1]);
+ shared_len = snprintf(shared_a, sizeof(shared_a), "%s/shared-a", argv[1]);
+ if (path_len < 0 || (size_t)path_len >= sizeof(path) ||
+ shared_len < 0 || (size_t)shared_len >= sizeof(shared_a))
+ ksft_exit_fail_msg("Mount path is too long\n");
+ shared_len = snprintf(shared_b, sizeof(shared_b), "%s/shared-b", argv[1]);
+ if (shared_len < 0 || (size_t)shared_len >= sizeof(shared_b))
+ ksft_exit_fail_msg("Mount path is too long\n");
+
+ fd = open(path, O_CREAT | O_EXCL | O_RDWR, 0600);
+ if (fd < 0) {
+ report_setup_error("create test file", TEST_COUNT - 1);
+ ksft_finished();
+ }
+ if (close(fd) < 0) {
+ report_setup_error("close test file", TEST_COUNT - 1);
+ ksft_finished();
+ }
+
+ value = malloc(LARGE_VALUE_SIZE);
+ expected = malloc(LARGE_VALUE_SIZE);
+ readback = malloc(LARGE_VALUE_SIZE);
+ if (!value || !expected || !readback) {
+ report_setup_error("allocate xattr buffers", TEST_COUNT - 1);
+ ksft_finished();
+ }
+
+ memset(value, 0x5a, LARGE_VALUE_SIZE);
+ value[0] = 1;
+ /* Exercise the original no-XATTR_CREATE credit-estimation path. */
+ created = set_xattr(path, large_name, value, LARGE_VALUE_SIZE, 0);
+ result(created, "large xattr creation");
+ if (created) {
+ result(xattr_matches(path, large_name, readback, value,
+ LARGE_VALUE_SIZE), "large xattr readback");
+
+ memset(value, 0xa5, LARGE_VALUE_SIZE);
+ value[0] = 2;
+ replaced = set_xattr(path, large_name, value,
+ LARGE_VALUE_SIZE, XATTR_REPLACE);
+ result(replaced, "large xattr replacement");
+ } else {
+ ksft_test_result_skip("readback needs the created xattr\n");
+ ksft_test_result_skip("replacement needs the created xattr\n");
+ }
+
+ if (replaced) {
+ result(xattr_matches(path, large_name, readback, value,
+ LARGE_VALUE_SIZE), "replacement readback");
+
+ memcpy(expected, value, LARGE_VALUE_SIZE);
+ memset(value, 0x3c, LARGE_VALUE_SIZE);
+ value[0] = 3;
+ errno = 0;
+ ret = setxattr(path, large_name, value, LARGE_VALUE_SIZE,
+ XATTR_CREATE);
+ result(ret < 0 && errno == EEXIST &&
+ xattr_matches(path, large_name, readback, expected,
+ LARGE_VALUE_SIZE),
+ "XATTR_CREATE rejection preserves the old value");
+ } else {
+ ksft_test_result_skip("replacement readback needs a successful replacement\n");
+ ksft_test_result_skip("XATTR_CREATE check needs a successful replacement\n");
+ }
+
+ if (prepare_shared_block(shared_a, shared_b, value, readback)) {
+ shared_ready = true;
+ ksft_test_result_pass("shared external xattr block fixture\n");
+ } else {
+ report_setup_error("prepare shared xattr block", 2);
+ }
+
+ if (shared_ready) {
+ memset(target_new, 0x22, sizeof(target_new));
+ shared_replaced = set_xattr(shared_a, target_name, target_new,
+ sizeof(target_new), XATTR_REPLACE);
+ result(shared_replaced,
+ "replace inode-body xattr with shared external block present");
+
+ result(shared_replaced &&
+ xattr_matches(shared_a, target_name, readback, target_new,
+ sizeof(target_new)) &&
+ xattr_matches(shared_b, target_name, readback,
+ &target_old, sizeof(target_old)) &&
+ xattr_matches(shared_b, large_name, readback, value,
+ LARGE_VALUE_SIZE),
+ "xattr block clone preserves both files' values");
+ }
+
+ if (created) {
+ ret = removexattr(path, large_name);
+ result(ret == 0, "large xattr removal");
+ } else {
+ ksft_test_result_skip("removal needs the created xattr\n");
+ }
+
+ ret = setxattr(path, small_name, small_value, sizeof(small_value), 0);
+ result(ret == 0 &&
+ getxattr(path, small_name, small_value_readback,
+ sizeof(small_value_readback)) == sizeof(small_value) &&
+ !memcmp(small_value, small_value_readback, sizeof(small_value)) &&
+ removexattr(path, small_name) == 0,
+ "small user xattr round trip");
+
+ free(readback);
+ free(expected);
+ free(value);
+ ksft_finished();
+}
diff --git a/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
new file mode 100755
index 000000000..4f875aae3
--- /dev/null
+++ b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
@@ -0,0 +1,56 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+
+KSFT_SKIP=4
+
+skip()
+{
+ printf 'SKIP: %s\n' "$1"
+ exit "$KSFT_SKIP"
+}
+
+[ "$(id -u)" -eq 0 ] || skip "requires root to mount a disposable EXT4 image"
+
+for command in mkfs.ext4 mount umount truncate e2fsck losetup grep; do
+ command -v "$command" >/dev/null 2>&1 || skip "missing $command"
+done
+
+grep -qw ext4 /proc/filesystems || skip "EXT4 is not available in this kernel"
+losetup -f >/dev/null 2>&1 || skip "no loop device is available"
+
+test_dir=$(mktemp -d) || exit 1
+mounted=0
+
+cleanup()
+{
+ if [ "$mounted" -eq 1 ]; then
+ umount "$test_dir/mnt" || true
+ fi
+ rm -rf "$test_dir"
+}
+trap cleanup EXIT INT TERM
+
+truncate -s 32M "$test_dir/fs.img" || exit 1
+if ! mkfs.ext4 -q -F -b 1024 -I 256 -O ea_inode -J size=1 "$test_dir/fs.img" \
+ 2>"$test_dir/mkfs.err"; then
+ cat "$test_dir/mkfs.err" >&2
+ skip "mkfs.ext4 does not support the ea_inode test image"
+fi
+mkdir "$test_dir/mnt" || exit 1
+if ! mount -t ext4 -o loop "$test_dir/fs.img" "$test_dir/mnt" \
+ 2>"$test_dir/mount.err"; then
+ cat "$test_dir/mount.err" >&2
+ if grep -Eqi 'permission denied|operation not permitted|not permitted' \
+ "$test_dir/mount.err"; then
+ skip "mounting a loop image is not permitted"
+ fi
+ exit 1
+fi
+mounted=1
+
+test_bin="$(dirname "$0")/ext4_xattr_journal_credits"
+"$test_bin" "$test_dir/mnt" || exit $?
+
+umount "$test_dir/mnt" || exit 1
+mounted=0
+e2fsck -fn "$test_dir/fs.img"
base-commit: c44104f3642da720164042623e4832836aefdd8e
--
2.56.1
^ permalink raw reply related [flat|nested] 22+ messages in thread* Re: [PATCH v4] ext4: bound xattr journal credits to transaction limit
2026-11-25 4:51 ` [PATCH v4] " arzuagapozosergio
@ 2026-11-25 5:09 ` sashiko-bot
2026-11-25 5:19 ` [PATCH v5] " arzuagapozosergio
1 sibling, 0 replies; 22+ messages in thread
From: sashiko-bot @ 2026-11-25 5:09 UTC (permalink / raw)
To: arzuagapozosergio; +Cc: linux-ext4, tytso
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] ext4: double dec_ref of ea_inode in ext4_xattr_block_set()
- [Low] selftests/ext4: flat main() and magic number in
ext4_xattr_journal_credits
--
commit 4a8d7681896524501769fc05104b7297b7732a84
Author: Ryuji Hiroshi <arzuagapozosergio@gmail.com>
ext4: bound xattr journal credits to transaction limit
This commit removes the journal credit reservation for freeing an old
ea_inode, bounds the xattr credit estimate against the maximum JBD2
transaction buffers, drops the provisional ea_inode reference when reusing
a cached xattr block, and adds an ext4 kselftest.
Link: https://syzbot.org/bug?extid=25d45c0bd716f2b794a7
> diff --git a/fs/ext4/xattr.c b/fs/ext4/xattr.c
> index 5c310747b9650..b8e1d50179240 100644
> --- a/fs/ext4/xattr.c
> +++ b/fs/ext4/xattr.c
[ ... ]
> @@ -2109,6 +2090,17 @@ ext4_xattr_block_set(handle_t *handle, struct inode *inode,
> new_bh);
> if (error)
> goto cleanup_dquot;
> + if (ea_inode) {
> + /* The reused block already holds this reference. */
> + error = ext4_xattr_inode_dec_ref(handle,
> + ea_inode);
> + if (error)
> + ext4_warning_inode(ea_inode,
> + "dec ref error=%d",
> + error);
> + ext4_put_ea_inode(ea_inode);
> + ea_inode = NULL;
> + }
> }
> mb_cache_entry_touch(ea_block_cache, ce);
> mb_cache_entry_put(ea_block_cache, ce);
[Severity: High]
Does this decrement ea_inode's reference count twice when an unshared
bs->bh (h_refcount == 1) is modified in-place and then matches an existing
block in ea_block_cache (new_bh != bs->bh)?
In the in-place modification path, ext4_xattr_set_entry() writes
ea_inode->i_ino into bs->bh->b_data before jumping to inserted: with
ea_inode still non-NULL:
fs/ext4/xattr.c:ext4_xattr_block_set() {
...
if (header(s->base)->h_refcount == cpu_to_le32(1)) {
...
ea_bdebug(bs->bh, "modifying in-place");
error = ext4_xattr_set_entry(i, s, handle, inode,
ea_inode, true /* is_block */);
...
goto inserted;
}
...
if (bs->bh && bs->bh != new_bh) {
ext4_xattr_release_block(handle, inode, bs->bh,
0 /* extra_credits */);
}
}
When ext4_xattr_block_cache_find() returns a cached new_bh != bs->bh, this
new block drops ea_inode's reference count once, and
ext4_xattr_release_block() -> ext4_xattr_inode_dec_ref_all() on the
in-place modified bs->bh drops it a second time. If new_bh held the only
other reference to ea_inode, its reference count drops to 0 and ea_inode is
freed on disk while new_bh still references it.
Guarding the decrement here with !(bs->bh && s->base == bs->bh->b_data), or
releasing and clearing ea_inode right after the in-place
ext4_xattr_set_entry() succeeds, would avoid the double decrement.
[ ... ]
> diff --git a/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
> new file mode 100644
> index 0000000000000..3b4bb726db03e
> --- /dev/null
> +++ b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
> @@ -0,0 +1,241 @@
> +// SPDX-License-Identifier: GPL-2.0
> +#define _GNU_SOURCE
> +#include <errno.h>
> +#include <fcntl.h>
> +#include <stdbool.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#include <sys/xattr.h>
> +#include <unistd.h>
> +
> +#include "../../kselftest.h"
> +
> +#define LARGE_VALUE_SIZE 65296
> +#define FILLER_VALUE_SIZE 4
> +#define TEST_COUNT 10
[ ... ]
> +int main(int argc, char **argv)
> +{
> + char path[4096], shared_a[4096], shared_b[4096];
> + char *value = NULL, *expected = NULL, *readback = NULL;
> + char target_new[64];
> + static const char small_value[] = "small-xattr";
> + char small_value_readback[sizeof(small_value)];
> + char target_old = 0x11;
> + int fd, path_len, shared_len;
> + bool created = false, replaced = false, shared_ready = false;
> + bool shared_replaced = false;
> + int ret;
[Severity: Low]
This isn't a bug, but could this test use kselftest_harness.h
(FIXTURE()/TEST_F() with ASSERT_*/EXPECT_*) instead of a flat main() with
cascading state booleans and manual skip counts? Also, is there a specific
reason for choosing 65296 for LARGE_VALUE_SIZE that could be documented in
a comment?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261125045143.53035-1-arzuagapozosergio@gmail.com?part=1
^ permalink raw reply [flat|nested] 22+ messages in thread* [PATCH v5] ext4: bound xattr journal credits to transaction limit
2026-11-25 4:51 ` [PATCH v4] " arzuagapozosergio
2026-11-25 5:09 ` sashiko-bot
@ 2026-11-25 5:19 ` arzuagapozosergio
2026-11-25 5:39 ` sashiko-bot
2026-11-25 6:01 ` [PATCH v6] " arzuagapozosergio
1 sibling, 2 replies; 22+ messages in thread
From: arzuagapozosergio @ 2026-11-25 5:19 UTC (permalink / raw)
To: tytso; +Cc: jack, linux-ext4, syzbot+25d45c0bd716f2b794a7, arzuagapozosergio
From: Ryuji Hiroshi <arzuagapozosergio@gmail.com>
When setting an xattr without XATTR_CREATE, ext4 reserves credits for
releasing an old ea_inode even when the attribute does not exist. On a
1 KiB-block filesystem with a minimum journal, this can request more
credits than JBD2 permits, triggering a warning before the operation
fails.
Drop the obsolete credits for releasing an old ea_inode. Since
ext4_put_ea_inode() defers the final iput to a worker, freeing the old
value's blocks does not happen in the current transaction. Continue to
count ea_inode references in the external xattr block for the case where
it must be cloned, and check the estimate against the transaction limit
after reserving the default JBD2 revoke credits. Repeat the estimate under
the xattr write lock before modifying the attribute.
When an identical xattr block is reused from the cache, drop the
provisional ea_inode reference because the reused block already contains
that reference. Add an EXT4 selftest covering the original
no-XATTR_CREATE credit path, large xattr creation and replacement,
XATTR_CREATE rejection, shared external block cloning with an ea_inode
entry, removal, and a small user xattr round trip.
Fixes: dec214d00e0d ("ext4: xattr inode deduplication")
Reported-by: syzbot+25d45c0bd716f2b794a7@syzkaller.appspotmail.com
Link: https://syzbot.org/bug?extid=25d45c0bd716f2b794a7
Assisted-by: Codex:GPT-6
Signed-off-by: Ryuji Hiroshi <arzuagapozosergio@gmail.com>
Changes in v5:
- Avoid dropping the ea_inode reference twice when the old xattr block was
modified in place before an identical cached block was reused.
- Document why the test uses a value close to XATTR_SIZE_MAX.
Tests:
- make -j4 bzImage
- make -C tools/testing/selftests/filesystems/ext4 clean all
- sh -n tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
- Booted the v5 kernel in QEMU/KVM with a 1 KiB-block ea_inode filesystem
and 1 MiB journal; all 10 TAP tests passed.
- e2fsck -fn passed after the guest unmounted the filesystem.
---
MAINTAINERS | 1 +
fs/ext4/acl.c | 3 +-
fs/ext4/crypto.c | 3 +-
fs/ext4/ialloc.c | 9 +-
fs/ext4/xattr.c | 118 ++++-----
fs/ext4/xattr.h | 8 +-
fs/jbd2/transaction.c | 9 +
include/linux/jbd2.h | 2 +
tools/testing/selftests/Makefile | 1 +
.../selftests/filesystems/ext4/.gitignore | 2 +
.../selftests/filesystems/ext4/Makefile | 6 +
.../ext4/ext4_xattr_journal_credits.c | 243 ++++++++++++++++++
.../ext4/ext4_xattr_journal_credits.sh | 56 ++++
13 files changed, 389 insertions(+), 72 deletions(-)
create mode 100644 tools/testing/selftests/filesystems/ext4/.gitignore
create mode 100644 tools/testing/selftests/filesystems/ext4/Makefile
create mode 100644 tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
create mode 100755 tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
diff --git a/MAINTAINERS b/MAINTAINERS
index 2ac5128b7..1debcbeb0 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -10131,6 +10131,7 @@ F: Documentation/filesystems/ext4/
F: fs/ext4/
F: include/trace/events/ext4.h
F: include/uapi/linux/ext4.h
+F: tools/testing/selftests/filesystems/ext4/
Extended Verification Module (EVM)
M: Mimi Zohar <zohar@linux.ibm.com>
diff --git a/fs/ext4/acl.c b/fs/ext4/acl.c
index 59fac55a2..167a410ef 100644
--- a/fs/ext4/acl.c
+++ b/fs/ext4/acl.c
@@ -239,8 +239,7 @@ ext4_set_acl(const struct mnt_idmap *idmap, struct dentry *dentry,
if (error)
return error;
retry:
- error = ext4_xattr_set_credits(inode, acl_size, false /* is_create */,
- &credits);
+ error = ext4_xattr_set_credits(inode, acl_size, &credits);
if (error)
return error;
diff --git a/fs/ext4/crypto.c b/fs/ext4/crypto.c
index 318b2eaea..c1275009e 100644
--- a/fs/ext4/crypto.c
+++ b/fs/ext4/crypto.c
@@ -203,8 +203,7 @@ static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
if (res)
return res;
retry:
- res = ext4_xattr_set_credits(inode, len, false /* is_create */,
- &credits);
+ res = ext4_xattr_set_credits(inode, len, &credits);
if (res)
return res;
diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
index 527f1f703..82623f563 100644
--- a/fs/ext4/ialloc.c
+++ b/fs/ext4/ialloc.c
@@ -888,8 +888,7 @@ static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
nblocks += (S_ISDIR(mode) ? 2 : 1) *
__ext4_xattr_set_credits(sb, NULL /* inode */,
- NULL /* block_bh */, acl_size,
- true /* is_create */);
+ NULL /* block_bh */, acl_size);
posix_acl_release(p);
}
#endif
@@ -907,16 +906,14 @@ static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
*/
nblocks += num_security_xattrs *
__ext4_xattr_set_credits(sb, NULL /* inode */,
- NULL /* block_bh */, 1024,
- true /* is_create */);
+ NULL /* block_bh */, 1024);
}
#endif
if (encrypt)
nblocks += __ext4_xattr_set_credits(sb,
NULL /* inode */,
NULL /* block_bh */,
- FSCRYPT_SET_CONTEXT_MAX_SIZE,
- true /* is_create */);
+ FSCRYPT_SET_CONTEXT_MAX_SIZE);
return nblocks;
}
diff --git a/fs/ext4/xattr.c b/fs/ext4/xattr.c
index 5c310747b..0a955a6e0 100644
--- a/fs/ext4/xattr.c
+++ b/fs/ext4/xattr.c
@@ -948,8 +948,7 @@ static void ext4_xattr_inode_free_quota(struct inode *parent,
}
int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
- struct buffer_head *block_bh, size_t value_len,
- bool is_create)
+ struct buffer_head *block_bh, size_t value_len)
{
int credits;
int blocks;
@@ -998,24 +997,6 @@ int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
/* Blocks themselves. */
credits += blocks;
- if (!is_create) {
- /* Dereference ea_inode holding old xattr value.
- * Old ea_inode, inode map, block bitmap, group descriptor.
- */
- credits += 4;
-
- /* Data blocks for old ea_inode. */
- blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits;
-
- /* Indirection block or one level of extent tree for old
- * ea_inode.
- */
- blocks += 1;
-
- /* Block bitmap and group descriptor updates for each block. */
- credits += blocks * 2;
- }
-
/* We may need to clone the existing xattr block in which case we need
* to increment ref counts for existing ea_inodes referenced by it.
*/
@@ -2109,6 +2090,18 @@ ext4_xattr_block_set(handle_t *handle, struct inode *inode,
new_bh);
if (error)
goto cleanup_dquot;
+ if (ea_inode &&
+ !(bs->bh && s->base == bs->bh->b_data)) {
+ /* The reused block already holds this reference. */
+ error = ext4_xattr_inode_dec_ref(handle,
+ ea_inode);
+ if (error)
+ ext4_warning_inode(ea_inode,
+ "dec ref error=%d",
+ error);
+ ext4_put_ea_inode(ea_inode);
+ ea_inode = NULL;
+ }
}
mb_cache_entry_touch(ea_block_cache, ce);
mb_cache_entry_put(ea_block_cache, ce);
@@ -2375,29 +2368,6 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
ext4_write_lock_xattr(inode, &no_expand);
- /* Check journal credits under write lock. */
- if (ext4_handle_valid(handle)) {
- struct buffer_head *bh;
- int credits;
-
- bh = ext4_xattr_get_block(inode);
- if (IS_ERR(bh)) {
- error = PTR_ERR(bh);
- goto cleanup;
- }
-
- credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
- value_len,
- flags & XATTR_CREATE);
- brelse(bh);
-
- if (jbd2_handle_buffer_credits(handle) < credits) {
- error = -ENOSPC;
- goto cleanup;
- }
- WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS));
- }
-
error = ext4_reserve_inode_write(handle, inode, &is.iloc);
if (error)
goto cleanup;
@@ -2415,6 +2385,28 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
error = ext4_xattr_block_find(inode, &i, &bs);
if (error)
goto cleanup;
+
+ /* Check journal credits under write lock. */
+ if (ext4_handle_valid(handle)) {
+ int credits;
+
+ if (!bs.bh) {
+ struct buffer_head *bh = ext4_xattr_get_block(inode);
+
+ if (IS_ERR(bh)) {
+ error = PTR_ERR(bh);
+ goto cleanup;
+ }
+ bs.bh = bh;
+ }
+ credits = __ext4_xattr_set_credits(inode->i_sb, inode, bs.bh,
+ value_len);
+ if (jbd2_handle_buffer_credits(handle) < credits) {
+ error = -ENOSPC;
+ goto cleanup;
+ }
+ WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS));
+ }
if (is.s.not_found && bs.s.not_found) {
error = -ENODATA;
if (flags & XATTR_REPLACE)
@@ -2501,27 +2493,38 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
}
int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
- bool is_create, int *credits)
+ int *credits)
{
- struct buffer_head *bh;
- int err;
+ struct ext4_xattr_block_find bs = {
+ .s = { .not_found = -ENODATA, },
+ };
+ journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
+ int max_credits, revoke_credits, err;
*credits = 0;
-
- if (!EXT4_SB(inode->i_sb)->s_journal)
+ if (!journal)
return 0;
+ /* xattr_sem protects the block while we count its ea_inode references. */
down_read(&EXT4_I(inode)->xattr_sem);
- bh = ext4_xattr_get_block(inode);
- if (IS_ERR(bh)) {
- err = PTR_ERR(bh);
- } else {
- *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
- value_len, is_create);
- brelse(bh);
- err = 0;
+ if (ext4_has_feature_ea_inode(inode->i_sb)) {
+ struct buffer_head *bh;
+
+ bh = ext4_xattr_get_block(inode);
+ if (IS_ERR(bh)) {
+ err = PTR_ERR(bh);
+ goto out_unlock;
+ }
+ bs.bh = bh;
}
+ *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bs.bh,
+ value_len);
+ revoke_credits = ext4_trans_default_revoke_credits(inode->i_sb);
+ max_credits = jbd2_journal_get_max_transaction_buffers(journal, revoke_credits);
+ err = *credits > max_credits ? -ENOSPC : 0;
+ brelse(bs.bh);
+out_unlock:
up_read(&EXT4_I(inode)->xattr_sem);
return err;
@@ -2549,8 +2552,7 @@ ext4_xattr_set(struct inode *inode, int name_index, const char *name,
return error;
retry:
- error = ext4_xattr_set_credits(inode, value_len, flags & XATTR_CREATE,
- &credits);
+ error = ext4_xattr_set_credits(inode, value_len, &credits);
if (error)
return error;
diff --git a/fs/ext4/xattr.h b/fs/ext4/xattr.h
index 821dc6a50..eb6e85036 100644
--- a/fs/ext4/xattr.h
+++ b/fs/ext4/xattr.h
@@ -175,11 +175,11 @@ extern ssize_t ext4_listxattr(struct dentry *, char *, size_t);
extern int ext4_xattr_get(struct inode *, int, const char *, void *, size_t);
extern int ext4_xattr_set(struct inode *, int, const char *, const void *, size_t, int);
extern int ext4_xattr_set_handle(handle_t *, struct inode *, int, const char *, const void *, size_t, int);
-extern int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
- bool is_create, int *credits);
+int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
+ int *credits);
extern int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
- struct buffer_head *block_bh, size_t value_len,
- bool is_create);
+ struct buffer_head *block_bh,
+ size_t value_len);
extern int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
int extra_credits);
diff --git a/fs/jbd2/transaction.c b/fs/jbd2/transaction.c
index 85d84d909..5fe49f118 100644
--- a/fs/jbd2/transaction.c
+++ b/fs/jbd2/transaction.c
@@ -194,6 +194,15 @@ static int jbd2_max_user_trans_buffers(journal_t *journal)
journal->j_transaction_overhead_buffers;
}
+/* Maximum buffer credits available after reserving @revoke_records. */
+int jbd2_journal_get_max_transaction_buffers(journal_t *journal,
+ int revoke_records)
+{
+ return jbd2_max_user_trans_buffers(journal) -
+ DIV_ROUND_UP(revoke_records, journal->j_revoke_records_per_block);
+}
+EXPORT_SYMBOL(jbd2_journal_get_max_transaction_buffers);
+
/*
* Wait until we can add credits for handle to the running transaction. Called
* with j_state_lock held for reading. Returns 0 if handle joined the running
diff --git a/include/linux/jbd2.h b/include/linux/jbd2.h
index e3886963b..a72f14440 100644
--- a/include/linux/jbd2.h
+++ b/include/linux/jbd2.h
@@ -1523,6 +1523,8 @@ static inline handle_t *journal_current_handle(void)
*/
extern handle_t *jbd2_journal_start(journal_t *, int nblocks);
+int jbd2_journal_get_max_transaction_buffers(journal_t *journal,
+ int revoke_records);
extern handle_t *jbd2__journal_start(journal_t *, int blocks, int rsv_blocks,
int revoke_records, gfp_t gfp_mask,
unsigned int type, unsigned int line_no);
diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
index 07c3719f8..c16b1132e 100644
--- a/tools/testing/selftests/Makefile
+++ b/tools/testing/selftests/Makefile
@@ -37,6 +37,7 @@ TARGETS += filesystems/binderfs
TARGETS += filesystems/configfs
TARGETS += filesystems/epoll
TARGETS += filesystems/eventfd
+TARGETS += filesystems/ext4
TARGETS += filesystems/failfs
TARGETS += filesystems/fat
TARGETS += filesystems/file_stressor
diff --git a/tools/testing/selftests/filesystems/ext4/.gitignore b/tools/testing/selftests/filesystems/ext4/.gitignore
new file mode 100644
index 000000000..d3678b52d
--- /dev/null
+++ b/tools/testing/selftests/filesystems/ext4/.gitignore
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0
+/ext4_xattr_journal_credits
diff --git a/tools/testing/selftests/filesystems/ext4/Makefile b/tools/testing/selftests/filesystems/ext4/Makefile
new file mode 100644
index 000000000..90569a1cc
--- /dev/null
+++ b/tools/testing/selftests/filesystems/ext4/Makefile
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0
+
+TEST_GEN_PROGS_EXTENDED := ext4_xattr_journal_credits
+TEST_PROGS := ext4_xattr_journal_credits.sh
+
+include ../../lib.mk
diff --git a/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
new file mode 100644
index 000000000..85dd28d62
--- /dev/null
+++ b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
@@ -0,0 +1,243 @@
+// SPDX-License-Identifier: GPL-2.0
+#define _GNU_SOURCE
+#include <errno.h>
+#include <fcntl.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/xattr.h>
+#include <unistd.h>
+
+#include "../../kselftest.h"
+
+#define LARGE_VALUE_SIZE 65296
+#define FILLER_VALUE_SIZE 4
+#define TEST_COUNT 10
+
+/* Near XATTR_SIZE_MAX to exercise the largest ea_inode credit estimate. */
+
+static const char large_name[] = "user.ext4-credit-large";
+static const char small_name[] = "user.ext4-credit-small";
+static const char target_name[] = "user.ext4-credit-target";
+static const char filler_name[] = "user.ext4-fill";
+
+static void result(bool passed, const char *name)
+{
+ ksft_test_result(passed, "%s\n", name);
+}
+
+static bool xattr_matches(const char *path, const char *name,
+ char *buffer, const void *expected, size_t size)
+{
+ ssize_t len;
+
+ errno = 0;
+ len = getxattr(path, name, buffer, size);
+ if (len < 0) {
+ ksft_print_msg("getxattr(%s): %s\n", name, strerror(errno));
+ return false;
+ }
+ return len == size && !memcmp(buffer, expected, size);
+}
+
+static int create_file(const char *path)
+{
+ int fd = open(path, O_CREAT | O_EXCL | O_RDWR, 0600);
+ int error;
+
+ if (fd < 0)
+ return -1;
+ if (!close(fd))
+ return 0;
+ error = errno;
+ errno = error;
+ return -1;
+}
+
+static bool set_xattr(const char *path, const char *name,
+ const void *value, size_t size, int flags)
+{
+ errno = 0;
+ if (!setxattr(path, name, value, size, flags))
+ return true;
+ ksft_print_msg("setxattr(%s): %s\n", name, strerror(errno));
+ return false;
+}
+
+static void report_setup_error(const char *what, unsigned int skip_count)
+{
+ int saved_errno = errno;
+ unsigned int i;
+
+ ksft_test_result_error("setup %s: %s\n", what,
+ strerror(saved_errno));
+ for (i = 0; i < skip_count; i++)
+ ksft_test_result_skip("setup failed\n");
+}
+
+static bool prepare_shared_file(const char *path, const char *large_value,
+ const char *filler, char target_value)
+{
+ /* Keep filler in inode body so large_name spills into the block. */
+ return create_file(path) == 0 &&
+ set_xattr(path, target_name, &target_value, sizeof(target_value),
+ XATTR_CREATE) &&
+ set_xattr(path, filler_name, filler, FILLER_VALUE_SIZE,
+ XATTR_CREATE) &&
+ set_xattr(path, large_name, large_value, LARGE_VALUE_SIZE,
+ XATTR_CREATE);
+}
+
+static bool prepare_shared_block(const char *path_a, const char *path_b,
+ char *large_value, char *readback)
+{
+ char target_value = 0x11;
+ char filler[FILLER_VALUE_SIZE];
+ char target_readback;
+
+ memset(filler, 0x6c, sizeof(filler));
+ memset(large_value, 0x7e, LARGE_VALUE_SIZE);
+ large_value[0] = 0x42;
+
+ if (!prepare_shared_file(path_a, large_value, filler, target_value) ||
+ !prepare_shared_file(path_b, large_value, filler, target_value))
+ return false;
+
+ return xattr_matches(path_a, large_name, readback, large_value,
+ LARGE_VALUE_SIZE) &&
+ xattr_matches(path_b, large_name, readback, large_value,
+ LARGE_VALUE_SIZE) &&
+ xattr_matches(path_a, target_name, &target_readback,
+ &target_value, sizeof(target_value)) &&
+ xattr_matches(path_b, target_name, &target_readback,
+ &target_value, sizeof(target_value));
+}
+
+int main(int argc, char **argv)
+{
+ char path[4096], shared_a[4096], shared_b[4096];
+ char *value = NULL, *expected = NULL, *readback = NULL;
+ char target_new[64];
+ static const char small_value[] = "small-xattr";
+ char small_value_readback[sizeof(small_value)];
+ char target_old = 0x11;
+ int fd, path_len, shared_len;
+ bool created = false, replaced = false, shared_ready = false;
+ bool shared_replaced = false;
+ int ret;
+
+ ksft_print_header();
+ ksft_set_plan(TEST_COUNT);
+ if (argc != 2)
+ ksft_exit_fail_msg("Usage: %s MOUNTPOINT\n", argv[0]);
+
+ path_len = snprintf(path, sizeof(path), "%s/credit-test", argv[1]);
+ shared_len = snprintf(shared_a, sizeof(shared_a), "%s/shared-a", argv[1]);
+ if (path_len < 0 || (size_t)path_len >= sizeof(path) ||
+ shared_len < 0 || (size_t)shared_len >= sizeof(shared_a))
+ ksft_exit_fail_msg("Mount path is too long\n");
+ shared_len = snprintf(shared_b, sizeof(shared_b), "%s/shared-b", argv[1]);
+ if (shared_len < 0 || (size_t)shared_len >= sizeof(shared_b))
+ ksft_exit_fail_msg("Mount path is too long\n");
+
+ fd = open(path, O_CREAT | O_EXCL | O_RDWR, 0600);
+ if (fd < 0) {
+ report_setup_error("create test file", TEST_COUNT - 1);
+ ksft_finished();
+ }
+ if (close(fd) < 0) {
+ report_setup_error("close test file", TEST_COUNT - 1);
+ ksft_finished();
+ }
+
+ value = malloc(LARGE_VALUE_SIZE);
+ expected = malloc(LARGE_VALUE_SIZE);
+ readback = malloc(LARGE_VALUE_SIZE);
+ if (!value || !expected || !readback) {
+ report_setup_error("allocate xattr buffers", TEST_COUNT - 1);
+ ksft_finished();
+ }
+
+ memset(value, 0x5a, LARGE_VALUE_SIZE);
+ value[0] = 1;
+ /* Exercise the original no-XATTR_CREATE credit-estimation path. */
+ created = set_xattr(path, large_name, value, LARGE_VALUE_SIZE, 0);
+ result(created, "large xattr creation");
+ if (created) {
+ result(xattr_matches(path, large_name, readback, value,
+ LARGE_VALUE_SIZE), "large xattr readback");
+
+ memset(value, 0xa5, LARGE_VALUE_SIZE);
+ value[0] = 2;
+ replaced = set_xattr(path, large_name, value,
+ LARGE_VALUE_SIZE, XATTR_REPLACE);
+ result(replaced, "large xattr replacement");
+ } else {
+ ksft_test_result_skip("readback needs the created xattr\n");
+ ksft_test_result_skip("replacement needs the created xattr\n");
+ }
+
+ if (replaced) {
+ result(xattr_matches(path, large_name, readback, value,
+ LARGE_VALUE_SIZE), "replacement readback");
+
+ memcpy(expected, value, LARGE_VALUE_SIZE);
+ memset(value, 0x3c, LARGE_VALUE_SIZE);
+ value[0] = 3;
+ errno = 0;
+ ret = setxattr(path, large_name, value, LARGE_VALUE_SIZE,
+ XATTR_CREATE);
+ result(ret < 0 && errno == EEXIST &&
+ xattr_matches(path, large_name, readback, expected,
+ LARGE_VALUE_SIZE),
+ "XATTR_CREATE rejection preserves the old value");
+ } else {
+ ksft_test_result_skip("replacement readback needs a successful replacement\n");
+ ksft_test_result_skip("XATTR_CREATE check needs a successful replacement\n");
+ }
+
+ if (prepare_shared_block(shared_a, shared_b, value, readback)) {
+ shared_ready = true;
+ ksft_test_result_pass("shared external xattr block fixture\n");
+ } else {
+ report_setup_error("prepare shared xattr block", 2);
+ }
+
+ if (shared_ready) {
+ memset(target_new, 0x22, sizeof(target_new));
+ shared_replaced = set_xattr(shared_a, target_name, target_new,
+ sizeof(target_new), XATTR_REPLACE);
+ result(shared_replaced,
+ "replace inode-body xattr with shared external block present");
+
+ result(shared_replaced &&
+ xattr_matches(shared_a, target_name, readback, target_new,
+ sizeof(target_new)) &&
+ xattr_matches(shared_b, target_name, readback,
+ &target_old, sizeof(target_old)) &&
+ xattr_matches(shared_b, large_name, readback, value,
+ LARGE_VALUE_SIZE),
+ "xattr block clone preserves both files' values");
+ }
+
+ if (created) {
+ ret = removexattr(path, large_name);
+ result(ret == 0, "large xattr removal");
+ } else {
+ ksft_test_result_skip("removal needs the created xattr\n");
+ }
+
+ ret = setxattr(path, small_name, small_value, sizeof(small_value), 0);
+ result(ret == 0 &&
+ getxattr(path, small_name, small_value_readback,
+ sizeof(small_value_readback)) == sizeof(small_value) &&
+ !memcmp(small_value, small_value_readback, sizeof(small_value)) &&
+ removexattr(path, small_name) == 0,
+ "small user xattr round trip");
+
+ free(readback);
+ free(expected);
+ free(value);
+ ksft_finished();
+}
diff --git a/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
new file mode 100755
index 000000000..4f875aae3
--- /dev/null
+++ b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
@@ -0,0 +1,56 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+
+KSFT_SKIP=4
+
+skip()
+{
+ printf 'SKIP: %s\n' "$1"
+ exit "$KSFT_SKIP"
+}
+
+[ "$(id -u)" -eq 0 ] || skip "requires root to mount a disposable EXT4 image"
+
+for command in mkfs.ext4 mount umount truncate e2fsck losetup grep; do
+ command -v "$command" >/dev/null 2>&1 || skip "missing $command"
+done
+
+grep -qw ext4 /proc/filesystems || skip "EXT4 is not available in this kernel"
+losetup -f >/dev/null 2>&1 || skip "no loop device is available"
+
+test_dir=$(mktemp -d) || exit 1
+mounted=0
+
+cleanup()
+{
+ if [ "$mounted" -eq 1 ]; then
+ umount "$test_dir/mnt" || true
+ fi
+ rm -rf "$test_dir"
+}
+trap cleanup EXIT INT TERM
+
+truncate -s 32M "$test_dir/fs.img" || exit 1
+if ! mkfs.ext4 -q -F -b 1024 -I 256 -O ea_inode -J size=1 "$test_dir/fs.img" \
+ 2>"$test_dir/mkfs.err"; then
+ cat "$test_dir/mkfs.err" >&2
+ skip "mkfs.ext4 does not support the ea_inode test image"
+fi
+mkdir "$test_dir/mnt" || exit 1
+if ! mount -t ext4 -o loop "$test_dir/fs.img" "$test_dir/mnt" \
+ 2>"$test_dir/mount.err"; then
+ cat "$test_dir/mount.err" >&2
+ if grep -Eqi 'permission denied|operation not permitted|not permitted' \
+ "$test_dir/mount.err"; then
+ skip "mounting a loop image is not permitted"
+ fi
+ exit 1
+fi
+mounted=1
+
+test_bin="$(dirname "$0")/ext4_xattr_journal_credits"
+"$test_bin" "$test_dir/mnt" || exit $?
+
+umount "$test_dir/mnt" || exit 1
+mounted=0
+e2fsck -fn "$test_dir/fs.img"
base-commit: c44104f3642da720164042623e4832836aefdd8e
--
2.56.1
^ permalink raw reply related [flat|nested] 22+ messages in thread* Re: [PATCH v5] ext4: bound xattr journal credits to transaction limit
2026-11-25 5:19 ` [PATCH v5] " arzuagapozosergio
@ 2026-11-25 5:39 ` sashiko-bot
2026-11-25 6:01 ` [PATCH v6] " arzuagapozosergio
1 sibling, 0 replies; 22+ messages in thread
From: sashiko-bot @ 2026-11-25 5:39 UTC (permalink / raw)
To: arzuagapozosergio; +Cc: linux-ext4, tytso
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] ext4: ea_inode refcount leak in ext4_xattr_block_set() when
new_bh == bs->bh
- [Low] selftests/ext4: flat main() and manual TAP plan in
ext4_xattr_journal_credits
--
commit 772b6f10e7b839b9e96f3998c77f31c8ed924efa
Author: Ryuji Hiroshi <arzuagapozosergio@gmail.com>
ext4: bound xattr journal credits to transaction limit
This commit drops obsolete ea_inode removal credits from
__ext4_xattr_set_credits(), bounds xattr credit estimates against the
JBD2 transaction limit, drops the provisional ea_inode reference when
reusing a cached xattr block, and adds an ext4 selftest.
Link: https://syzbot.org/bug?extid=25d45c0bd716f2b794a7
> diff --git a/fs/ext4/xattr.c b/fs/ext4/xattr.c
> index 5c310747b9650..0a955a6e0912d 100644
> --- a/fs/ext4/xattr.c
> +++ b/fs/ext4/xattr.c
[ ... ]
> @@ -2109,6 +2090,18 @@ ext4_xattr_block_set(handle_t *handle, struct inode *inode,
> new_bh);
> if (error)
> goto cleanup_dquot;
> + if (ea_inode &&
> + !(bs->bh && s->base == bs->bh->b_data)) {
> + /* The reused block already holds this reference. */
> + error = ext4_xattr_inode_dec_ref(handle,
> + ea_inode);
> + if (error)
> + ext4_warning_inode(ea_inode,
> + "dec ref error=%d",
> + error);
> + ext4_put_ea_inode(ea_inode);
> + ea_inode = NULL;
> + }
> }
> mb_cache_entry_touch(ea_block_cache, ce);
> mb_cache_entry_put(ea_block_cache, ce);
[Severity: High]
Does this leak an on-disk reference on ea_inode when a cloned xattr block
matches bs->bh itself in ea_block_cache (new_bh == bs->bh)?
ext4_xattr_value_same() returns 0 whenever s->here->e_value_inum is set, so
overwriting an existing ea_inode-backed attribute with the same value still
enters ext4_xattr_block_set(), where ext4_xattr_inode_lookup_create() finds
the existing ea_inode and increments its on-disk refcount. When bs->bh is
shared (h_refcount > 1) or its mb_cache entry is busy, clone_block clears
s->here->e_value_inum in s->base so ext4_xattr_set_entry() does not drop the
existing reference, then writes the same ea_inode->i_ino back into s->base
and ext4_xattr_block_cache_find() returns new_bh == bs->bh:
fs/ext4/xattr.c:ext4_xattr_block_set() {
...
if (new_bh) {
/* We found an identical block in the cache. */
if (new_bh == bs->bh)
ea_bdebug(new_bh, "keeping");
else {
...
}
Because the new_bh == bs->bh branch skips the else block and bs->bh == new_bh
also skips ext4_xattr_release_block(), the provisional reference on ea_inode
is never decremented, leaving the ea_inode and its data blocks permanently
allocated on disk. Moving this ea_inode drop after the if (new_bh == bs->bh)
/ else block (still inside if (new_bh)) would cover both cases.
> diff --git a/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
> new file mode 100644
> index 0000000000000..85dd28d62bcf3
> --- /dev/null
> +++ b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
> @@ -0,0 +1,243 @@
[ ... ]
> +static void report_setup_error(const char *what, unsigned int skip_count)
> +{
> + int saved_errno = errno;
> + unsigned int i;
> +
> + ksft_test_result_error("setup %s: %s\n", what,
> + strerror(saved_errno));
> + for (i = 0; i < skip_count; i++)
> + ksft_test_result_skip("setup failed\n");
> +}
[ ... ]
> +int main(int argc, char **argv)
> +{
> + char path[4096], shared_a[4096], shared_b[4096];
> + char *value = NULL, *expected = NULL, *readback = NULL;
> + char target_new[64];
> + static const char small_value[] = "small-xattr";
> + char small_value_readback[sizeof(small_value)];
> + char target_old = 0x11;
> + int fd, path_len, shared_len;
> + bool created = false, replaced = false, shared_ready = false;
> + bool shared_replaced = false;
> + int ret;
[Severity: Low]
Could this test use kselftest_harness.h (FIXTURE(), FIXTURE_SETUP(),
FIXTURE_TEARDOWN(), and TEST_F() with ASSERT_*/EXPECT_*) instead of a flat
main() that chains checks with boolean flags and pads the TAP plan via
report_setup_error()?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261125051909.61227-1-arzuagapozosergio@gmail.com?part=1
^ permalink raw reply [flat|nested] 22+ messages in thread* [PATCH v6] ext4: bound xattr journal credits to transaction limit
2026-11-25 5:19 ` [PATCH v5] " arzuagapozosergio
2026-11-25 5:39 ` sashiko-bot
@ 2026-11-25 6:01 ` arzuagapozosergio
2026-11-25 6:23 ` sashiko-bot
2026-11-25 6:30 ` [PATCH v7] " arzuagapozosergio
1 sibling, 2 replies; 22+ messages in thread
From: arzuagapozosergio @ 2026-11-25 6:01 UTC (permalink / raw)
To: tytso; +Cc: jack, linux-ext4, syzbot+25d45c0bd716f2b794a7, arzuagapozosergio
From: Ryuji Hiroshi <arzuagapozosergio@gmail.com>
When setting an xattr without XATTR_CREATE, ext4 reserves credits for
releasing an old ea_inode even when the attribute does not exist. On a
1 KiB-block filesystem with a minimum journal, this can request more
credits than JBD2 permits, triggering a warning before the operation
fails.
Drop the obsolete credits for releasing an old ea_inode. Since
ext4_put_ea_inode() defers the final iput to a worker, freeing the old
value's blocks does not happen in the current transaction. Continue to
count ea_inode references in the external xattr block for the case where
it must be cloned, and check the estimate against the transaction limit
after reserving the default JBD2 revoke credits. Repeat the estimate under
the xattr write lock before modifying the attribute.
When an identical xattr block is reused from the cache, drop the
provisional ea_inode reference unless the block was modified in place.
Handle both a different cached block and the original block being found
after cloning. Add an EXT4 selftest for the credit path, large xattrs,
shared-block same-value replacement, and a small user xattr round trip.
Fixes: dec214d00e0d ("ext4: xattr inode deduplication")
Reported-by: syzbot+25d45c0bd716f2b794a7@syzkaller.appspotmail.com
Link: https://syzbot.org/bug?extid=25d45c0bd716f2b794a7
Assisted-by: Codex:GPT-6
Signed-off-by: Ryuji Hiroshi <arzuagapozosergio@gmail.com>
Changes in v6:
- Release the provisional ea_inode reference when the cache returns the
original block after cloning, while retaining the in-place update case.
- Convert the EXT4 test to kselftest_harness and add shared-block
same-value ea_inode replacement coverage.
Tests:
- make -j4 bzImage
- make -C tools/testing/selftests/filesystems/ext4 clean all
- sh -n tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
- Booted the v6 kernel in QEMU/KVM with a 1 KiB-block ea_inode filesystem
and 1 MiB journal; all 3 harness tests passed.
- e2fsck -fn passed after the guest unmounted the filesystem.
---
MAINTAINERS | 1 +
fs/ext4/acl.c | 3 +-
fs/ext4/crypto.c | 3 +-
fs/ext4/ialloc.c | 9 +-
fs/ext4/xattr.c | 116 ++++++------
fs/ext4/xattr.h | 8 +-
fs/jbd2/transaction.c | 9 +
include/linux/jbd2.h | 2 +
tools/testing/selftests/Makefile | 1 +
.../selftests/filesystems/ext4/.gitignore | 2 +
.../selftests/filesystems/ext4/Makefile | 6 +
.../ext4/ext4_xattr_journal_credits.c | 165 ++++++++++++++++++
.../ext4/ext4_xattr_journal_credits.sh | 56 ++++++
13 files changed, 309 insertions(+), 72 deletions(-)
create mode 100644 tools/testing/selftests/filesystems/ext4/.gitignore
create mode 100644 tools/testing/selftests/filesystems/ext4/Makefile
create mode 100644 tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
create mode 100755 tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
diff --git a/MAINTAINERS b/MAINTAINERS
index 2ac5128b7..1debcbeb0 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -10131,6 +10131,7 @@ F: Documentation/filesystems/ext4/
F: fs/ext4/
F: include/trace/events/ext4.h
F: include/uapi/linux/ext4.h
+F: tools/testing/selftests/filesystems/ext4/
Extended Verification Module (EVM)
M: Mimi Zohar <zohar@linux.ibm.com>
diff --git a/fs/ext4/acl.c b/fs/ext4/acl.c
index 59fac55a2..167a410ef 100644
--- a/fs/ext4/acl.c
+++ b/fs/ext4/acl.c
@@ -239,8 +239,7 @@ ext4_set_acl(const struct mnt_idmap *idmap, struct dentry *dentry,
if (error)
return error;
retry:
- error = ext4_xattr_set_credits(inode, acl_size, false /* is_create */,
- &credits);
+ error = ext4_xattr_set_credits(inode, acl_size, &credits);
if (error)
return error;
diff --git a/fs/ext4/crypto.c b/fs/ext4/crypto.c
index 318b2eaea..c1275009e 100644
--- a/fs/ext4/crypto.c
+++ b/fs/ext4/crypto.c
@@ -203,8 +203,7 @@ static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
if (res)
return res;
retry:
- res = ext4_xattr_set_credits(inode, len, false /* is_create */,
- &credits);
+ res = ext4_xattr_set_credits(inode, len, &credits);
if (res)
return res;
diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
index 527f1f703..82623f563 100644
--- a/fs/ext4/ialloc.c
+++ b/fs/ext4/ialloc.c
@@ -888,8 +888,7 @@ static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
nblocks += (S_ISDIR(mode) ? 2 : 1) *
__ext4_xattr_set_credits(sb, NULL /* inode */,
- NULL /* block_bh */, acl_size,
- true /* is_create */);
+ NULL /* block_bh */, acl_size);
posix_acl_release(p);
}
#endif
@@ -907,16 +906,14 @@ static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
*/
nblocks += num_security_xattrs *
__ext4_xattr_set_credits(sb, NULL /* inode */,
- NULL /* block_bh */, 1024,
- true /* is_create */);
+ NULL /* block_bh */, 1024);
}
#endif
if (encrypt)
nblocks += __ext4_xattr_set_credits(sb,
NULL /* inode */,
NULL /* block_bh */,
- FSCRYPT_SET_CONTEXT_MAX_SIZE,
- true /* is_create */);
+ FSCRYPT_SET_CONTEXT_MAX_SIZE);
return nblocks;
}
diff --git a/fs/ext4/xattr.c b/fs/ext4/xattr.c
index 5c310747b..753265c08 100644
--- a/fs/ext4/xattr.c
+++ b/fs/ext4/xattr.c
@@ -948,8 +948,7 @@ static void ext4_xattr_inode_free_quota(struct inode *parent,
}
int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
- struct buffer_head *block_bh, size_t value_len,
- bool is_create)
+ struct buffer_head *block_bh, size_t value_len)
{
int credits;
int blocks;
@@ -998,24 +997,6 @@ int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
/* Blocks themselves. */
credits += blocks;
- if (!is_create) {
- /* Dereference ea_inode holding old xattr value.
- * Old ea_inode, inode map, block bitmap, group descriptor.
- */
- credits += 4;
-
- /* Data blocks for old ea_inode. */
- blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits;
-
- /* Indirection block or one level of extent tree for old
- * ea_inode.
- */
- blocks += 1;
-
- /* Block bitmap and group descriptor updates for each block. */
- credits += blocks * 2;
- }
-
/* We may need to clone the existing xattr block in which case we need
* to increment ref counts for existing ea_inodes referenced by it.
*/
@@ -2110,6 +2091,16 @@ ext4_xattr_block_set(handle_t *handle, struct inode *inode,
if (error)
goto cleanup_dquot;
}
+ if (ea_inode &&
+ !(bs->bh && s->base == bs->bh->b_data)) {
+ /* The reused block already holds this reference. */
+ error = ext4_xattr_inode_dec_ref(handle, ea_inode);
+ if (error)
+ ext4_warning_inode(ea_inode,
+ "dec ref error=%d", error);
+ ext4_put_ea_inode(ea_inode);
+ ea_inode = NULL;
+ }
mb_cache_entry_touch(ea_block_cache, ce);
mb_cache_entry_put(ea_block_cache, ce);
ce = NULL;
@@ -2375,29 +2366,6 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
ext4_write_lock_xattr(inode, &no_expand);
- /* Check journal credits under write lock. */
- if (ext4_handle_valid(handle)) {
- struct buffer_head *bh;
- int credits;
-
- bh = ext4_xattr_get_block(inode);
- if (IS_ERR(bh)) {
- error = PTR_ERR(bh);
- goto cleanup;
- }
-
- credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
- value_len,
- flags & XATTR_CREATE);
- brelse(bh);
-
- if (jbd2_handle_buffer_credits(handle) < credits) {
- error = -ENOSPC;
- goto cleanup;
- }
- WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS));
- }
-
error = ext4_reserve_inode_write(handle, inode, &is.iloc);
if (error)
goto cleanup;
@@ -2415,6 +2383,28 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
error = ext4_xattr_block_find(inode, &i, &bs);
if (error)
goto cleanup;
+
+ /* Check journal credits under write lock. */
+ if (ext4_handle_valid(handle)) {
+ int credits;
+
+ if (!bs.bh) {
+ struct buffer_head *bh = ext4_xattr_get_block(inode);
+
+ if (IS_ERR(bh)) {
+ error = PTR_ERR(bh);
+ goto cleanup;
+ }
+ bs.bh = bh;
+ }
+ credits = __ext4_xattr_set_credits(inode->i_sb, inode, bs.bh,
+ value_len);
+ if (jbd2_handle_buffer_credits(handle) < credits) {
+ error = -ENOSPC;
+ goto cleanup;
+ }
+ WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS));
+ }
if (is.s.not_found && bs.s.not_found) {
error = -ENODATA;
if (flags & XATTR_REPLACE)
@@ -2501,27 +2491,38 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
}
int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
- bool is_create, int *credits)
+ int *credits)
{
- struct buffer_head *bh;
- int err;
+ struct ext4_xattr_block_find bs = {
+ .s = { .not_found = -ENODATA, },
+ };
+ journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
+ int max_credits, revoke_credits, err;
*credits = 0;
-
- if (!EXT4_SB(inode->i_sb)->s_journal)
+ if (!journal)
return 0;
+ /* xattr_sem protects the block while we count its ea_inode references. */
down_read(&EXT4_I(inode)->xattr_sem);
- bh = ext4_xattr_get_block(inode);
- if (IS_ERR(bh)) {
- err = PTR_ERR(bh);
- } else {
- *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
- value_len, is_create);
- brelse(bh);
- err = 0;
+ if (ext4_has_feature_ea_inode(inode->i_sb)) {
+ struct buffer_head *bh;
+
+ bh = ext4_xattr_get_block(inode);
+ if (IS_ERR(bh)) {
+ err = PTR_ERR(bh);
+ goto out_unlock;
+ }
+ bs.bh = bh;
}
+ *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bs.bh,
+ value_len);
+ revoke_credits = ext4_trans_default_revoke_credits(inode->i_sb);
+ max_credits = jbd2_journal_get_max_transaction_buffers(journal, revoke_credits);
+ err = *credits > max_credits ? -ENOSPC : 0;
+ brelse(bs.bh);
+out_unlock:
up_read(&EXT4_I(inode)->xattr_sem);
return err;
@@ -2549,8 +2550,7 @@ ext4_xattr_set(struct inode *inode, int name_index, const char *name,
return error;
retry:
- error = ext4_xattr_set_credits(inode, value_len, flags & XATTR_CREATE,
- &credits);
+ error = ext4_xattr_set_credits(inode, value_len, &credits);
if (error)
return error;
diff --git a/fs/ext4/xattr.h b/fs/ext4/xattr.h
index 821dc6a50..eb6e85036 100644
--- a/fs/ext4/xattr.h
+++ b/fs/ext4/xattr.h
@@ -175,11 +175,11 @@ extern ssize_t ext4_listxattr(struct dentry *, char *, size_t);
extern int ext4_xattr_get(struct inode *, int, const char *, void *, size_t);
extern int ext4_xattr_set(struct inode *, int, const char *, const void *, size_t, int);
extern int ext4_xattr_set_handle(handle_t *, struct inode *, int, const char *, const void *, size_t, int);
-extern int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
- bool is_create, int *credits);
+int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
+ int *credits);
extern int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
- struct buffer_head *block_bh, size_t value_len,
- bool is_create);
+ struct buffer_head *block_bh,
+ size_t value_len);
extern int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
int extra_credits);
diff --git a/fs/jbd2/transaction.c b/fs/jbd2/transaction.c
index 85d84d909..5fe49f118 100644
--- a/fs/jbd2/transaction.c
+++ b/fs/jbd2/transaction.c
@@ -194,6 +194,15 @@ static int jbd2_max_user_trans_buffers(journal_t *journal)
journal->j_transaction_overhead_buffers;
}
+/* Maximum buffer credits available after reserving @revoke_records. */
+int jbd2_journal_get_max_transaction_buffers(journal_t *journal,
+ int revoke_records)
+{
+ return jbd2_max_user_trans_buffers(journal) -
+ DIV_ROUND_UP(revoke_records, journal->j_revoke_records_per_block);
+}
+EXPORT_SYMBOL(jbd2_journal_get_max_transaction_buffers);
+
/*
* Wait until we can add credits for handle to the running transaction. Called
* with j_state_lock held for reading. Returns 0 if handle joined the running
diff --git a/include/linux/jbd2.h b/include/linux/jbd2.h
index e3886963b..a72f14440 100644
--- a/include/linux/jbd2.h
+++ b/include/linux/jbd2.h
@@ -1523,6 +1523,8 @@ static inline handle_t *journal_current_handle(void)
*/
extern handle_t *jbd2_journal_start(journal_t *, int nblocks);
+int jbd2_journal_get_max_transaction_buffers(journal_t *journal,
+ int revoke_records);
extern handle_t *jbd2__journal_start(journal_t *, int blocks, int rsv_blocks,
int revoke_records, gfp_t gfp_mask,
unsigned int type, unsigned int line_no);
diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
index 07c3719f8..c16b1132e 100644
--- a/tools/testing/selftests/Makefile
+++ b/tools/testing/selftests/Makefile
@@ -37,6 +37,7 @@ TARGETS += filesystems/binderfs
TARGETS += filesystems/configfs
TARGETS += filesystems/epoll
TARGETS += filesystems/eventfd
+TARGETS += filesystems/ext4
TARGETS += filesystems/failfs
TARGETS += filesystems/fat
TARGETS += filesystems/file_stressor
diff --git a/tools/testing/selftests/filesystems/ext4/.gitignore b/tools/testing/selftests/filesystems/ext4/.gitignore
new file mode 100644
index 000000000..d3678b52d
--- /dev/null
+++ b/tools/testing/selftests/filesystems/ext4/.gitignore
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0
+/ext4_xattr_journal_credits
diff --git a/tools/testing/selftests/filesystems/ext4/Makefile b/tools/testing/selftests/filesystems/ext4/Makefile
new file mode 100644
index 000000000..90569a1cc
--- /dev/null
+++ b/tools/testing/selftests/filesystems/ext4/Makefile
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0
+
+TEST_GEN_PROGS_EXTENDED := ext4_xattr_journal_credits
+TEST_PROGS := ext4_xattr_journal_credits.sh
+
+include ../../lib.mk
diff --git a/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
new file mode 100644
index 000000000..4cf51eed2
--- /dev/null
+++ b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
@@ -0,0 +1,165 @@
+// SPDX-License-Identifier: GPL-2.0
+#define _GNU_SOURCE
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/xattr.h>
+#include <unistd.h>
+
+#include "../../kselftest_harness.h"
+
+/* Near XATTR_SIZE_MAX to exercise the largest ea_inode credit estimate. */
+#define LARGE_VALUE_SIZE 65296
+
+static const char large_name[] = "user.ext4-credit-large";
+static const char small_name[] = "user.ext4-credit-small";
+
+FIXTURE(ext4_xattr_journal_credits)
+{
+ char path[PATH_MAX];
+ char shared_a[PATH_MAX];
+ char shared_b[PATH_MAX];
+ char *value;
+ char *readback;
+};
+
+FIXTURE_SETUP(ext4_xattr_journal_credits)
+{
+ int len;
+
+ len = snprintf(self->path, sizeof(self->path), "credit-test-%d",
+ getpid());
+ ASSERT_GE(len, 0);
+ ASSERT_LT(len, (int)sizeof(self->path));
+ len = snprintf(self->shared_a, sizeof(self->shared_a), "shared-a-%d",
+ getpid());
+ ASSERT_GE(len, 0);
+ ASSERT_LT(len, (int)sizeof(self->shared_a));
+ len = snprintf(self->shared_b, sizeof(self->shared_b), "shared-b-%d",
+ getpid());
+ ASSERT_GE(len, 0);
+ ASSERT_LT(len, (int)sizeof(self->shared_b));
+
+ self->value = malloc(LARGE_VALUE_SIZE);
+ ASSERT_NE(NULL, self->value);
+ self->readback = malloc(LARGE_VALUE_SIZE);
+ ASSERT_NE(NULL, self->readback);
+}
+
+FIXTURE_TEARDOWN(ext4_xattr_journal_credits)
+{
+ unlink(self->path);
+ unlink(self->shared_a);
+ unlink(self->shared_b);
+ free(self->readback);
+ free(self->value);
+}
+
+static void create_test_file(struct __test_metadata *_metadata,
+ const char *path)
+{
+ int fd = open(path, O_CREAT | O_EXCL | O_RDWR, 0600);
+
+ ASSERT_GE(fd, 0) {
+ TH_LOG("open(%s): %s", path, strerror(errno));
+ }
+ ASSERT_EQ(0, close(fd));
+}
+
+static void check_xattr_value(struct __test_metadata *_metadata,
+ const char *path,
+ const char *name, const void *expected,
+ size_t size, char *buffer)
+{
+ ssize_t len = getxattr(path, name, buffer, size);
+
+ ASSERT_EQ((ssize_t)size, len) {
+ TH_LOG("getxattr(%s): %s", name,
+ len < 0 ? strerror(errno) : "unexpected value size");
+ }
+ ASSERT_EQ(0, memcmp(buffer, expected, size));
+}
+
+TEST_F(ext4_xattr_journal_credits, large_xattr_lifecycle)
+{
+ char *expected = malloc(LARGE_VALUE_SIZE);
+ int ret;
+
+ ASSERT_NE(NULL, expected);
+ create_test_file(_metadata, self->path);
+ memset(self->value, 0x5a, LARGE_VALUE_SIZE);
+ self->value[0] = 1;
+
+ ASSERT_EQ(0, setxattr(self->path, large_name, self->value,
+ LARGE_VALUE_SIZE, 0));
+ check_xattr_value(_metadata, self->path, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+
+ memset(self->value, 0xa5, LARGE_VALUE_SIZE);
+ self->value[0] = 2;
+ ASSERT_EQ(0, setxattr(self->path, large_name, self->value,
+ LARGE_VALUE_SIZE, XATTR_REPLACE));
+ check_xattr_value(_metadata, self->path, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+
+ memcpy(expected, self->value, LARGE_VALUE_SIZE);
+ memset(self->value, 0x3c, LARGE_VALUE_SIZE);
+ self->value[0] = 3;
+ errno = 0;
+ ret = setxattr(self->path, large_name, self->value,
+ LARGE_VALUE_SIZE, XATTR_CREATE);
+ ASSERT_EQ(-1, ret);
+ ASSERT_EQ(EEXIST, errno);
+ check_xattr_value(_metadata, self->path, large_name, expected,
+ LARGE_VALUE_SIZE, self->readback);
+
+ ASSERT_EQ(0, removexattr(self->path, large_name));
+ free(expected);
+}
+
+TEST_F(ext4_xattr_journal_credits, shared_block_reuses_same_ea_inode)
+{
+ create_test_file(_metadata, self->shared_a);
+ create_test_file(_metadata, self->shared_b);
+ memset(self->value, 0x7e, LARGE_VALUE_SIZE);
+ self->value[0] = 0x42;
+
+ ASSERT_EQ(0, setxattr(self->shared_a, large_name, self->value,
+ LARGE_VALUE_SIZE, XATTR_CREATE));
+ ASSERT_EQ(0, setxattr(self->shared_b, large_name, self->value,
+ LARGE_VALUE_SIZE, XATTR_CREATE));
+ check_xattr_value(_metadata, self->shared_a, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+ check_xattr_value(_metadata, self->shared_b, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+
+ /* Clone the shared block, then deduplicate back to the original block. */
+ ASSERT_EQ(0, setxattr(self->shared_a, large_name, self->value,
+ LARGE_VALUE_SIZE, XATTR_REPLACE));
+ check_xattr_value(_metadata, self->shared_a, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+ check_xattr_value(_metadata, self->shared_b, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+
+ ASSERT_EQ(0, removexattr(self->shared_a, large_name));
+ ASSERT_EQ(0, removexattr(self->shared_b, large_name));
+}
+
+TEST_F(ext4_xattr_journal_credits, small_xattr_round_trip)
+{
+ static const char value[] = "small-xattr";
+ char readback[sizeof(value)];
+
+ create_test_file(_metadata, self->path);
+ ASSERT_EQ(0, setxattr(self->path, small_name, value, sizeof(value), 0));
+ ASSERT_EQ((ssize_t)sizeof(value),
+ getxattr(self->path, small_name, readback, sizeof(readback)));
+ ASSERT_EQ(0, memcmp(value, readback, sizeof(value)));
+ ASSERT_EQ(0, removexattr(self->path, small_name));
+}
+
+TEST_HARNESS_MAIN
diff --git a/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
new file mode 100755
index 000000000..81af22a52
--- /dev/null
+++ b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
@@ -0,0 +1,56 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+
+KSFT_SKIP=4
+
+skip()
+{
+ printf 'SKIP: %s\n' "$1"
+ exit "$KSFT_SKIP"
+}
+
+[ "$(id -u)" -eq 0 ] || skip "requires root to mount a disposable EXT4 image"
+
+for command in mkfs.ext4 mount umount truncate e2fsck losetup grep; do
+ command -v "$command" >/dev/null 2>&1 || skip "missing $command"
+done
+
+grep -qw ext4 /proc/filesystems || skip "EXT4 is not available in this kernel"
+losetup -f >/dev/null 2>&1 || skip "no loop device is available"
+
+test_dir=$(mktemp -d) || exit 1
+mounted=0
+
+cleanup()
+{
+ if [ "$mounted" -eq 1 ]; then
+ umount "$test_dir/mnt" || true
+ fi
+ rm -rf "$test_dir"
+}
+trap cleanup EXIT INT TERM
+
+truncate -s 32M "$test_dir/fs.img" || exit 1
+if ! mkfs.ext4 -q -F -b 1024 -I 256 -O ea_inode -J size=1 "$test_dir/fs.img" \
+ 2>"$test_dir/mkfs.err"; then
+ cat "$test_dir/mkfs.err" >&2
+ skip "mkfs.ext4 does not support the ea_inode test image"
+fi
+mkdir "$test_dir/mnt" || exit 1
+if ! mount -t ext4 -o loop "$test_dir/fs.img" "$test_dir/mnt" \
+ 2>"$test_dir/mount.err"; then
+ cat "$test_dir/mount.err" >&2
+ if grep -Eqi 'permission denied|operation not permitted|not permitted' \
+ "$test_dir/mount.err"; then
+ skip "mounting a loop image is not permitted"
+ fi
+ exit 1
+fi
+mounted=1
+
+test_bin="$(cd "$(dirname "$0")" && pwd)/ext4_xattr_journal_credits"
+(cd "$test_dir/mnt" && "$test_bin") || exit $?
+
+umount "$test_dir/mnt" || exit 1
+mounted=0
+e2fsck -fn "$test_dir/fs.img"
base-commit: c44104f3642da720164042623e4832836aefdd8e
--
2.56.1
^ permalink raw reply related [flat|nested] 22+ messages in thread* Re: [PATCH v6] ext4: bound xattr journal credits to transaction limit
2026-11-25 6:01 ` [PATCH v6] " arzuagapozosergio
@ 2026-11-25 6:23 ` sashiko-bot
2026-11-25 6:30 ` [PATCH v7] " arzuagapozosergio
1 sibling, 0 replies; 22+ messages in thread
From: sashiko-bot @ 2026-11-25 6:23 UTC (permalink / raw)
To: arzuagapozosergio; +Cc: linux-ext4, tytso
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] selftests/ext4: shared_block_reuses_same_ea_inode stores
xattr in ibody
--
commit 7a62229bf92048e6830756e7f4a7119dee79e887
Author: Ryuji Hiroshi <arzuagapozosergio@gmail.com>
ext4: bound xattr journal credits to transaction limit
This commit drops obsolete ea_inode release credits from the xattr credit
estimate, bounds the estimate against the journal transaction limit, drops
provisional ea_inode references when reusing a cached xattr block, and adds
an ext4 selftest.
Link: https://syzbot.org/bug?extid=25d45c0bd716f2b794a7
> diff --git a/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
> new file mode 100644
> index 0000000000000..4cf51eed24ade
> --- /dev/null
> +++ b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
[ ... ]
> +TEST_F(ext4_xattr_journal_credits, shared_block_reuses_same_ea_inode)
> +{
> + create_test_file(_metadata, self->shared_a);
> + create_test_file(_metadata, self->shared_b);
> + memset(self->value, 0x7e, LARGE_VALUE_SIZE);
> + self->value[0] = 0x42;
> +
> + ASSERT_EQ(0, setxattr(self->shared_a, large_name, self->value,
> + LARGE_VALUE_SIZE, XATTR_CREATE));
> + ASSERT_EQ(0, setxattr(self->shared_b, large_name, self->value,
> + LARGE_VALUE_SIZE, XATTR_CREATE));
> + check_xattr_value(_metadata, self->shared_a, large_name, self->value,
> + LARGE_VALUE_SIZE, self->readback);
> + check_xattr_value(_metadata, self->shared_b, large_name, self->value,
> + LARGE_VALUE_SIZE, self->readback);
> +
> + /* Clone the shared block, then deduplicate back to the original block. */
> + ASSERT_EQ(0, setxattr(self->shared_a, large_name, self->value,
> + LARGE_VALUE_SIZE, XATTR_REPLACE));
[Severity: Medium]
Does shared_block_reuses_same_ea_inode actually allocate or share an
external xattr block?
Because the value is stored in an ea_inode (i.in_inode = 1), only the
36-byte xattr entry is placed in the xattr table. Since
ext4_xattr_journal_credits.sh formats the filesystem with -I 256 (leaving
88 bytes of in-inode ibody space) and both test files have empty ibodies,
ext4_xattr_ibody_set() succeeds on both XATTR_CREATE calls and the
XATTR_REPLACE call without ever reaching ext4_xattr_block_set(). Filling
the in-inode xattr space on both files first (or formatting with -I 128)
would force large_name into a shared external xattr block so the
provisional ea_inode reference-drop path in ext4_xattr_block_set() is
exercised.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261125060131.10328-1-arzuagapozosergio@gmail.com?part=1
^ permalink raw reply [flat|nested] 22+ messages in thread* [PATCH v7] ext4: bound xattr journal credits to transaction limit
2026-11-25 6:01 ` [PATCH v6] " arzuagapozosergio
2026-11-25 6:23 ` sashiko-bot
@ 2026-11-25 6:30 ` arzuagapozosergio
2026-11-25 6:47 ` sashiko-bot
2026-11-25 7:00 ` [PATCH v8] " arzuagapozosergio
1 sibling, 2 replies; 22+ messages in thread
From: arzuagapozosergio @ 2026-11-25 6:30 UTC (permalink / raw)
To: tytso; +Cc: jack, linux-ext4, syzbot+25d45c0bd716f2b794a7, arzuagapozosergio
From: Ryuji Hiroshi <arzuagapozosergio@gmail.com>
When setting an xattr without XATTR_CREATE, ext4 reserves credits for
releasing an old ea_inode even when the attribute does not exist. On a
1 KiB-block filesystem with a minimum journal, this can request more
credits than JBD2 permits, triggering a warning before the operation
fails.
Drop the obsolete credits for releasing an old ea_inode. Since
ext4_put_ea_inode() defers the final iput to a worker, freeing the old
value's blocks does not happen in the current transaction. Continue to
count ea_inode references in the external xattr block for the case where
it must be cloned, and check the estimate against the transaction limit
after reserving the default JBD2 revoke credits. Repeat the estimate under
the xattr write lock before modifying the attribute.
When an identical xattr block is reused from the cache, drop the
provisional ea_inode reference unless the block was modified in place.
Handle both a different cached block and the original block being found
after cloning. Add an EXT4 selftest for the credit path, large xattrs,
shared-block same-value replacement, and a small user xattr round trip.
Fixes: dec214d00e0d ("ext4: xattr inode deduplication")
Reported-by: syzbot+25d45c0bd716f2b794a7@syzkaller.appspotmail.com
Link: https://syzbot.org/bug?extid=25d45c0bd716f2b794a7
Assisted-by: Codex:GPT-6
Signed-off-by: Ryuji Hiroshi <arzuagapozosergio@gmail.com>
Changes in v7:
- Format the selftest image with 128-byte inodes so xattrs cannot fit in
the in-inode area and the shared ea_inode test exercises external blocks.
Tests:
- make -C tools/testing/selftests/filesystems/ext4 clean all
- sh -n tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
- Booted the v6 kernel in QEMU/KVM with a 1 KiB-block, 128-byte-inode
ea_inode filesystem and 1 MiB journal; all 3 harness tests passed.
- e2fsck -fn passed after the guest unmounted the filesystem.
---
MAINTAINERS | 1 +
fs/ext4/acl.c | 3 +-
fs/ext4/crypto.c | 3 +-
fs/ext4/ialloc.c | 9 +-
fs/ext4/xattr.c | 116 ++++++------
fs/ext4/xattr.h | 8 +-
fs/jbd2/transaction.c | 9 +
include/linux/jbd2.h | 2 +
tools/testing/selftests/Makefile | 1 +
.../selftests/filesystems/ext4/.gitignore | 2 +
.../selftests/filesystems/ext4/Makefile | 6 +
.../ext4/ext4_xattr_journal_credits.c | 166 ++++++++++++++++++
.../ext4/ext4_xattr_journal_credits.sh | 57 ++++++
13 files changed, 311 insertions(+), 72 deletions(-)
create mode 100644 tools/testing/selftests/filesystems/ext4/.gitignore
create mode 100644 tools/testing/selftests/filesystems/ext4/Makefile
create mode 100644 tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
create mode 100755 tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
diff --git a/MAINTAINERS b/MAINTAINERS
index 2ac5128b7..1debcbeb0 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -10131,6 +10131,7 @@ F: Documentation/filesystems/ext4/
F: fs/ext4/
F: include/trace/events/ext4.h
F: include/uapi/linux/ext4.h
+F: tools/testing/selftests/filesystems/ext4/
Extended Verification Module (EVM)
M: Mimi Zohar <zohar@linux.ibm.com>
diff --git a/fs/ext4/acl.c b/fs/ext4/acl.c
index 59fac55a2..167a410ef 100644
--- a/fs/ext4/acl.c
+++ b/fs/ext4/acl.c
@@ -239,8 +239,7 @@ ext4_set_acl(const struct mnt_idmap *idmap, struct dentry *dentry,
if (error)
return error;
retry:
- error = ext4_xattr_set_credits(inode, acl_size, false /* is_create */,
- &credits);
+ error = ext4_xattr_set_credits(inode, acl_size, &credits);
if (error)
return error;
diff --git a/fs/ext4/crypto.c b/fs/ext4/crypto.c
index 318b2eaea..c1275009e 100644
--- a/fs/ext4/crypto.c
+++ b/fs/ext4/crypto.c
@@ -203,8 +203,7 @@ static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
if (res)
return res;
retry:
- res = ext4_xattr_set_credits(inode, len, false /* is_create */,
- &credits);
+ res = ext4_xattr_set_credits(inode, len, &credits);
if (res)
return res;
diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
index 527f1f703..82623f563 100644
--- a/fs/ext4/ialloc.c
+++ b/fs/ext4/ialloc.c
@@ -888,8 +888,7 @@ static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
nblocks += (S_ISDIR(mode) ? 2 : 1) *
__ext4_xattr_set_credits(sb, NULL /* inode */,
- NULL /* block_bh */, acl_size,
- true /* is_create */);
+ NULL /* block_bh */, acl_size);
posix_acl_release(p);
}
#endif
@@ -907,16 +906,14 @@ static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
*/
nblocks += num_security_xattrs *
__ext4_xattr_set_credits(sb, NULL /* inode */,
- NULL /* block_bh */, 1024,
- true /* is_create */);
+ NULL /* block_bh */, 1024);
}
#endif
if (encrypt)
nblocks += __ext4_xattr_set_credits(sb,
NULL /* inode */,
NULL /* block_bh */,
- FSCRYPT_SET_CONTEXT_MAX_SIZE,
- true /* is_create */);
+ FSCRYPT_SET_CONTEXT_MAX_SIZE);
return nblocks;
}
diff --git a/fs/ext4/xattr.c b/fs/ext4/xattr.c
index 5c310747b..753265c08 100644
--- a/fs/ext4/xattr.c
+++ b/fs/ext4/xattr.c
@@ -948,8 +948,7 @@ static void ext4_xattr_inode_free_quota(struct inode *parent,
}
int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
- struct buffer_head *block_bh, size_t value_len,
- bool is_create)
+ struct buffer_head *block_bh, size_t value_len)
{
int credits;
int blocks;
@@ -998,24 +997,6 @@ int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
/* Blocks themselves. */
credits += blocks;
- if (!is_create) {
- /* Dereference ea_inode holding old xattr value.
- * Old ea_inode, inode map, block bitmap, group descriptor.
- */
- credits += 4;
-
- /* Data blocks for old ea_inode. */
- blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits;
-
- /* Indirection block or one level of extent tree for old
- * ea_inode.
- */
- blocks += 1;
-
- /* Block bitmap and group descriptor updates for each block. */
- credits += blocks * 2;
- }
-
/* We may need to clone the existing xattr block in which case we need
* to increment ref counts for existing ea_inodes referenced by it.
*/
@@ -2110,6 +2091,16 @@ ext4_xattr_block_set(handle_t *handle, struct inode *inode,
if (error)
goto cleanup_dquot;
}
+ if (ea_inode &&
+ !(bs->bh && s->base == bs->bh->b_data)) {
+ /* The reused block already holds this reference. */
+ error = ext4_xattr_inode_dec_ref(handle, ea_inode);
+ if (error)
+ ext4_warning_inode(ea_inode,
+ "dec ref error=%d", error);
+ ext4_put_ea_inode(ea_inode);
+ ea_inode = NULL;
+ }
mb_cache_entry_touch(ea_block_cache, ce);
mb_cache_entry_put(ea_block_cache, ce);
ce = NULL;
@@ -2375,29 +2366,6 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
ext4_write_lock_xattr(inode, &no_expand);
- /* Check journal credits under write lock. */
- if (ext4_handle_valid(handle)) {
- struct buffer_head *bh;
- int credits;
-
- bh = ext4_xattr_get_block(inode);
- if (IS_ERR(bh)) {
- error = PTR_ERR(bh);
- goto cleanup;
- }
-
- credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
- value_len,
- flags & XATTR_CREATE);
- brelse(bh);
-
- if (jbd2_handle_buffer_credits(handle) < credits) {
- error = -ENOSPC;
- goto cleanup;
- }
- WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS));
- }
-
error = ext4_reserve_inode_write(handle, inode, &is.iloc);
if (error)
goto cleanup;
@@ -2415,6 +2383,28 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
error = ext4_xattr_block_find(inode, &i, &bs);
if (error)
goto cleanup;
+
+ /* Check journal credits under write lock. */
+ if (ext4_handle_valid(handle)) {
+ int credits;
+
+ if (!bs.bh) {
+ struct buffer_head *bh = ext4_xattr_get_block(inode);
+
+ if (IS_ERR(bh)) {
+ error = PTR_ERR(bh);
+ goto cleanup;
+ }
+ bs.bh = bh;
+ }
+ credits = __ext4_xattr_set_credits(inode->i_sb, inode, bs.bh,
+ value_len);
+ if (jbd2_handle_buffer_credits(handle) < credits) {
+ error = -ENOSPC;
+ goto cleanup;
+ }
+ WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS));
+ }
if (is.s.not_found && bs.s.not_found) {
error = -ENODATA;
if (flags & XATTR_REPLACE)
@@ -2501,27 +2491,38 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
}
int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
- bool is_create, int *credits)
+ int *credits)
{
- struct buffer_head *bh;
- int err;
+ struct ext4_xattr_block_find bs = {
+ .s = { .not_found = -ENODATA, },
+ };
+ journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
+ int max_credits, revoke_credits, err;
*credits = 0;
-
- if (!EXT4_SB(inode->i_sb)->s_journal)
+ if (!journal)
return 0;
+ /* xattr_sem protects the block while we count its ea_inode references. */
down_read(&EXT4_I(inode)->xattr_sem);
- bh = ext4_xattr_get_block(inode);
- if (IS_ERR(bh)) {
- err = PTR_ERR(bh);
- } else {
- *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
- value_len, is_create);
- brelse(bh);
- err = 0;
+ if (ext4_has_feature_ea_inode(inode->i_sb)) {
+ struct buffer_head *bh;
+
+ bh = ext4_xattr_get_block(inode);
+ if (IS_ERR(bh)) {
+ err = PTR_ERR(bh);
+ goto out_unlock;
+ }
+ bs.bh = bh;
}
+ *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bs.bh,
+ value_len);
+ revoke_credits = ext4_trans_default_revoke_credits(inode->i_sb);
+ max_credits = jbd2_journal_get_max_transaction_buffers(journal, revoke_credits);
+ err = *credits > max_credits ? -ENOSPC : 0;
+ brelse(bs.bh);
+out_unlock:
up_read(&EXT4_I(inode)->xattr_sem);
return err;
@@ -2549,8 +2550,7 @@ ext4_xattr_set(struct inode *inode, int name_index, const char *name,
return error;
retry:
- error = ext4_xattr_set_credits(inode, value_len, flags & XATTR_CREATE,
- &credits);
+ error = ext4_xattr_set_credits(inode, value_len, &credits);
if (error)
return error;
diff --git a/fs/ext4/xattr.h b/fs/ext4/xattr.h
index 821dc6a50..eb6e85036 100644
--- a/fs/ext4/xattr.h
+++ b/fs/ext4/xattr.h
@@ -175,11 +175,11 @@ extern ssize_t ext4_listxattr(struct dentry *, char *, size_t);
extern int ext4_xattr_get(struct inode *, int, const char *, void *, size_t);
extern int ext4_xattr_set(struct inode *, int, const char *, const void *, size_t, int);
extern int ext4_xattr_set_handle(handle_t *, struct inode *, int, const char *, const void *, size_t, int);
-extern int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
- bool is_create, int *credits);
+int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
+ int *credits);
extern int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
- struct buffer_head *block_bh, size_t value_len,
- bool is_create);
+ struct buffer_head *block_bh,
+ size_t value_len);
extern int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
int extra_credits);
diff --git a/fs/jbd2/transaction.c b/fs/jbd2/transaction.c
index 85d84d909..5fe49f118 100644
--- a/fs/jbd2/transaction.c
+++ b/fs/jbd2/transaction.c
@@ -194,6 +194,15 @@ static int jbd2_max_user_trans_buffers(journal_t *journal)
journal->j_transaction_overhead_buffers;
}
+/* Maximum buffer credits available after reserving @revoke_records. */
+int jbd2_journal_get_max_transaction_buffers(journal_t *journal,
+ int revoke_records)
+{
+ return jbd2_max_user_trans_buffers(journal) -
+ DIV_ROUND_UP(revoke_records, journal->j_revoke_records_per_block);
+}
+EXPORT_SYMBOL(jbd2_journal_get_max_transaction_buffers);
+
/*
* Wait until we can add credits for handle to the running transaction. Called
* with j_state_lock held for reading. Returns 0 if handle joined the running
diff --git a/include/linux/jbd2.h b/include/linux/jbd2.h
index e3886963b..a72f14440 100644
--- a/include/linux/jbd2.h
+++ b/include/linux/jbd2.h
@@ -1523,6 +1523,8 @@ static inline handle_t *journal_current_handle(void)
*/
extern handle_t *jbd2_journal_start(journal_t *, int nblocks);
+int jbd2_journal_get_max_transaction_buffers(journal_t *journal,
+ int revoke_records);
extern handle_t *jbd2__journal_start(journal_t *, int blocks, int rsv_blocks,
int revoke_records, gfp_t gfp_mask,
unsigned int type, unsigned int line_no);
diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
index 07c3719f8..c16b1132e 100644
--- a/tools/testing/selftests/Makefile
+++ b/tools/testing/selftests/Makefile
@@ -37,6 +37,7 @@ TARGETS += filesystems/binderfs
TARGETS += filesystems/configfs
TARGETS += filesystems/epoll
TARGETS += filesystems/eventfd
+TARGETS += filesystems/ext4
TARGETS += filesystems/failfs
TARGETS += filesystems/fat
TARGETS += filesystems/file_stressor
diff --git a/tools/testing/selftests/filesystems/ext4/.gitignore b/tools/testing/selftests/filesystems/ext4/.gitignore
new file mode 100644
index 000000000..d3678b52d
--- /dev/null
+++ b/tools/testing/selftests/filesystems/ext4/.gitignore
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0
+/ext4_xattr_journal_credits
diff --git a/tools/testing/selftests/filesystems/ext4/Makefile b/tools/testing/selftests/filesystems/ext4/Makefile
new file mode 100644
index 000000000..90569a1cc
--- /dev/null
+++ b/tools/testing/selftests/filesystems/ext4/Makefile
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0
+
+TEST_GEN_PROGS_EXTENDED := ext4_xattr_journal_credits
+TEST_PROGS := ext4_xattr_journal_credits.sh
+
+include ../../lib.mk
diff --git a/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
new file mode 100644
index 000000000..93e8c074d
--- /dev/null
+++ b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
@@ -0,0 +1,166 @@
+// SPDX-License-Identifier: GPL-2.0
+#define _GNU_SOURCE
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/xattr.h>
+#include <unistd.h>
+
+#include "../../kselftest_harness.h"
+
+/* Near XATTR_SIZE_MAX to exercise the largest ea_inode credit estimate. */
+#define LARGE_VALUE_SIZE 65296
+
+static const char large_name[] = "user.ext4-credit-large";
+static const char small_name[] = "user.ext4-credit-small";
+
+FIXTURE(ext4_xattr_journal_credits)
+{
+ char path[PATH_MAX];
+ char shared_a[PATH_MAX];
+ char shared_b[PATH_MAX];
+ char *value;
+ char *readback;
+};
+
+FIXTURE_SETUP(ext4_xattr_journal_credits)
+{
+ int len;
+
+ len = snprintf(self->path, sizeof(self->path), "credit-test-%d",
+ getpid());
+ ASSERT_GE(len, 0);
+ ASSERT_LT(len, (int)sizeof(self->path));
+ len = snprintf(self->shared_a, sizeof(self->shared_a), "shared-a-%d",
+ getpid());
+ ASSERT_GE(len, 0);
+ ASSERT_LT(len, (int)sizeof(self->shared_a));
+ len = snprintf(self->shared_b, sizeof(self->shared_b), "shared-b-%d",
+ getpid());
+ ASSERT_GE(len, 0);
+ ASSERT_LT(len, (int)sizeof(self->shared_b));
+
+ self->value = malloc(LARGE_VALUE_SIZE);
+ ASSERT_NE(NULL, self->value);
+ self->readback = malloc(LARGE_VALUE_SIZE);
+ ASSERT_NE(NULL, self->readback);
+}
+
+FIXTURE_TEARDOWN(ext4_xattr_journal_credits)
+{
+ unlink(self->path);
+ unlink(self->shared_a);
+ unlink(self->shared_b);
+ free(self->readback);
+ free(self->value);
+}
+
+static void create_test_file(struct __test_metadata *_metadata,
+ const char *path)
+{
+ int fd = open(path, O_CREAT | O_EXCL | O_RDWR, 0600);
+
+ ASSERT_GE(fd, 0) {
+ TH_LOG("open(%s): %s", path, strerror(errno));
+ }
+ ASSERT_EQ(0, close(fd));
+}
+
+static void check_xattr_value(struct __test_metadata *_metadata,
+ const char *path,
+ const char *name, const void *expected,
+ size_t size, char *buffer)
+{
+ ssize_t len = getxattr(path, name, buffer, size);
+
+ ASSERT_EQ((ssize_t)size, len) {
+ TH_LOG("getxattr(%s): %s", name,
+ len < 0 ? strerror(errno) : "unexpected value size");
+ }
+ ASSERT_EQ(0, memcmp(buffer, expected, size));
+}
+
+TEST_F(ext4_xattr_journal_credits, large_xattr_lifecycle)
+{
+ char *expected = malloc(LARGE_VALUE_SIZE);
+ int ret;
+
+ ASSERT_NE(NULL, expected);
+ create_test_file(_metadata, self->path);
+ memset(self->value, 0x5a, LARGE_VALUE_SIZE);
+ self->value[0] = 1;
+
+ ASSERT_EQ(0, setxattr(self->path, large_name, self->value,
+ LARGE_VALUE_SIZE, 0));
+ check_xattr_value(_metadata, self->path, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+
+ memset(self->value, 0xa5, LARGE_VALUE_SIZE);
+ self->value[0] = 2;
+ ASSERT_EQ(0, setxattr(self->path, large_name, self->value,
+ LARGE_VALUE_SIZE, XATTR_REPLACE));
+ check_xattr_value(_metadata, self->path, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+
+ memcpy(expected, self->value, LARGE_VALUE_SIZE);
+ memset(self->value, 0x3c, LARGE_VALUE_SIZE);
+ self->value[0] = 3;
+ errno = 0;
+ ret = setxattr(self->path, large_name, self->value,
+ LARGE_VALUE_SIZE, XATTR_CREATE);
+ ASSERT_EQ(-1, ret);
+ ASSERT_EQ(EEXIST, errno);
+ check_xattr_value(_metadata, self->path, large_name, expected,
+ LARGE_VALUE_SIZE, self->readback);
+
+ ASSERT_EQ(0, removexattr(self->path, large_name));
+ free(expected);
+}
+
+TEST_F(ext4_xattr_journal_credits, shared_block_reuses_same_ea_inode)
+{
+ /* The test image has 128-byte inodes, so both xattrs use external blocks. */
+ create_test_file(_metadata, self->shared_a);
+ create_test_file(_metadata, self->shared_b);
+ memset(self->value, 0x7e, LARGE_VALUE_SIZE);
+ self->value[0] = 0x42;
+
+ ASSERT_EQ(0, setxattr(self->shared_a, large_name, self->value,
+ LARGE_VALUE_SIZE, XATTR_CREATE));
+ ASSERT_EQ(0, setxattr(self->shared_b, large_name, self->value,
+ LARGE_VALUE_SIZE, XATTR_CREATE));
+ check_xattr_value(_metadata, self->shared_a, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+ check_xattr_value(_metadata, self->shared_b, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+
+ /* Clone the shared block, then deduplicate back to the original block. */
+ ASSERT_EQ(0, setxattr(self->shared_a, large_name, self->value,
+ LARGE_VALUE_SIZE, XATTR_REPLACE));
+ check_xattr_value(_metadata, self->shared_a, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+ check_xattr_value(_metadata, self->shared_b, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+
+ ASSERT_EQ(0, removexattr(self->shared_a, large_name));
+ ASSERT_EQ(0, removexattr(self->shared_b, large_name));
+}
+
+TEST_F(ext4_xattr_journal_credits, small_xattr_round_trip)
+{
+ static const char value[] = "small-xattr";
+ char readback[sizeof(value)];
+
+ create_test_file(_metadata, self->path);
+ ASSERT_EQ(0, setxattr(self->path, small_name, value, sizeof(value), 0));
+ ASSERT_EQ((ssize_t)sizeof(value),
+ getxattr(self->path, small_name, readback, sizeof(readback)));
+ ASSERT_EQ(0, memcmp(value, readback, sizeof(value)));
+ ASSERT_EQ(0, removexattr(self->path, small_name));
+}
+
+TEST_HARNESS_MAIN
diff --git a/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
new file mode 100755
index 000000000..977cdcf47
--- /dev/null
+++ b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+
+KSFT_SKIP=4
+
+skip()
+{
+ printf 'SKIP: %s\n' "$1"
+ exit "$KSFT_SKIP"
+}
+
+[ "$(id -u)" -eq 0 ] || skip "requires root to mount a disposable EXT4 image"
+
+for command in mkfs.ext4 mount umount truncate e2fsck losetup grep; do
+ command -v "$command" >/dev/null 2>&1 || skip "missing $command"
+done
+
+grep -qw ext4 /proc/filesystems || skip "EXT4 is not available in this kernel"
+losetup -f >/dev/null 2>&1 || skip "no loop device is available"
+
+test_dir=$(mktemp -d) || exit 1
+mounted=0
+
+cleanup()
+{
+ if [ "$mounted" -eq 1 ]; then
+ umount "$test_dir/mnt" || true
+ fi
+ rm -rf "$test_dir"
+}
+trap cleanup EXIT INT TERM
+
+truncate -s 32M "$test_dir/fs.img" || exit 1
+# 128-byte inodes have no ibody xattr space, forcing the external-block path.
+if ! mkfs.ext4 -q -F -b 1024 -I 128 -O ea_inode -J size=1 "$test_dir/fs.img" \
+ 2>"$test_dir/mkfs.err"; then
+ cat "$test_dir/mkfs.err" >&2
+ skip "mkfs.ext4 does not support the ea_inode test image"
+fi
+mkdir "$test_dir/mnt" || exit 1
+if ! mount -t ext4 -o loop "$test_dir/fs.img" "$test_dir/mnt" \
+ 2>"$test_dir/mount.err"; then
+ cat "$test_dir/mount.err" >&2
+ if grep -Eqi 'permission denied|operation not permitted|not permitted' \
+ "$test_dir/mount.err"; then
+ skip "mounting a loop image is not permitted"
+ fi
+ exit 1
+fi
+mounted=1
+
+test_bin="$(cd "$(dirname "$0")" && pwd)/ext4_xattr_journal_credits"
+(cd "$test_dir/mnt" && "$test_bin") || exit $?
+
+umount "$test_dir/mnt" || exit 1
+mounted=0
+e2fsck -fn "$test_dir/fs.img"
base-commit: c44104f3642da720164042623e4832836aefdd8e
--
2.56.1
^ permalink raw reply related [flat|nested] 22+ messages in thread* Re: [PATCH v7] ext4: bound xattr journal credits to transaction limit
2026-11-25 6:30 ` [PATCH v7] " arzuagapozosergio
@ 2026-11-25 6:47 ` sashiko-bot
2026-11-25 7:00 ` [PATCH v8] " arzuagapozosergio
1 sibling, 0 replies; 22+ messages in thread
From: sashiko-bot @ 2026-11-25 6:47 UTC (permalink / raw)
To: arzuagapozosergio; +Cc: linux-ext4, tytso
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] ext4: unbounded xattr journal credits in __ext4_new_inode()
--
commit 7d32898686e8e393a0ba63d6580e4c83b8362716
Author: Ryuji Hiroshi <arzuagapozosergio@gmail.com>
ext4: bound xattr journal credits to transaction limit
This commit removes the old-ea_inode dereference credits from
__ext4_xattr_set_credits(), bounds ext4_xattr_set_credits() against the
maximum JBD2 transaction buffers, drops provisional ea_inode references
when reusing cached xattr blocks, and adds an ext4 kselftest.
Link: https://syzbot.org/bug?extid=25d45c0bd716f2b794a7
> diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
> index 527f1f703c3e0..82623f5635c4c 100644
> --- a/fs/ext4/ialloc.c
> +++ b/fs/ext4/ialloc.c
> @@ -888,8 +888,7 @@ static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
>
> nblocks += (S_ISDIR(mode) ? 2 : 1) *
> __ext4_xattr_set_credits(sb, NULL /* inode */,
> - NULL /* block_bh */, acl_size,
> - true /* is_create */);
> + NULL /* block_bh */, acl_size);
> posix_acl_release(p);
> }
> #endif
[Severity: High]
Can nblocks in __ext4_new_inode() still exceed the journal transaction
limit when creating an inode in a directory with a large default ACL?
Dropping the old-ea_inode dereference credits allows ext4_set_acl() to set
a default POSIX ACL up to ~64 KiB on a 1 KiB-block ea_inode filesystem
with a 1 MiB journal. When a subdirectory is later created in that
directory, ext4_xattr_credits_for_new_inode() charges 2 *
__ext4_xattr_set_credits() for the inherited default and access ACLs plus
security and fscrypt xattr credits, without checking the total against
jbd2_journal_get_max_transaction_buffers():
fs/ext4/ialloc.c:__ext4_new_inode() {
...
if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) {
ret2 = ext4_xattr_credits_for_new_inode(dir, mode, encrypt);
if (ret2 < 0) {
err = ret2;
goto out;
}
nblocks += ret2;
}
...
handle = __ext4_journal_start_sb(NULL, dir->i_sb,
line_no, handle_type, nblocks, 0,
ext4_trans_default_revoke_credits(sb));
if (IS_ERR(handle)) {
err = PTR_ERR(handle);
ext4_std_error(sb, err);
goto out;
}
}
For a default ACL of 20 KiB or larger, nblocks exceeds the 248-buffer
limit, triggering WARN_ON(1) in start_this_handle() and passing -ENOSPC to
ext4_std_error(), which aborts the filesystem.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261125063023.18022-1-arzuagapozosergio@gmail.com?part=1
^ permalink raw reply [flat|nested] 22+ messages in thread* [PATCH v8] ext4: bound xattr journal credits to transaction limit
2026-11-25 6:30 ` [PATCH v7] " arzuagapozosergio
2026-11-25 6:47 ` sashiko-bot
@ 2026-11-25 7:00 ` arzuagapozosergio
2026-11-25 7:16 ` sashiko-bot
2026-11-25 7:20 ` [PATCH v9] " arzuagapozosergio
1 sibling, 2 replies; 22+ messages in thread
From: arzuagapozosergio @ 2026-11-25 7:00 UTC (permalink / raw)
To: tytso; +Cc: jack, linux-ext4, syzbot+25d45c0bd716f2b794a7, arzuagapozosergio
From: Ryuji Hiroshi <arzuagapozosergio@gmail.com>
When setting an xattr without XATTR_CREATE, ext4 reserves credits for
releasing an old ea_inode even when the attribute does not exist. On a
1 KiB-block filesystem with a minimum journal, this can request more
credits than JBD2 permits, triggering a warning before the operation
fails.
Drop the obsolete credits for releasing an old ea_inode. Since
ext4_put_ea_inode() defers the final iput to a worker, freeing the old
value's blocks does not happen in the current transaction. Continue to
count ea_inode references in the external xattr block for the case where
it must be cloned, and check the estimate against the transaction limit
after reserving the default JBD2 revoke credits. Repeat the estimate under
the xattr write lock before modifying the attribute.
When an identical xattr block is reused from the cache, drop the
provisional ea_inode reference unless the block was modified in place.
Handle both a different cached block and the original block being found
after cloning. Bound the aggregate credits for inherited xattrs during
inode creation against the JBD2 transaction limit before starting a
handle. Add EXT4 selftests for these paths.
Fixes: dec214d00e0d ("ext4: xattr inode deduplication")
Reported-by: syzbot+25d45c0bd716f2b794a7@syzkaller.appspotmail.com
Link: https://syzbot.org/bug?extid=25d45c0bd716f2b794a7
Assisted-by: Codex:GPT-6
Signed-off-by: Ryuji Hiroshi <arzuagapozosergio@gmail.com>
Changes in v8:
- Check the total inode-creation credits, including caller-provided
credits and inherited ACL/security/fscrypt xattrs, before starting JBD2.
- Add a large default ACL regression test that verifies an oversized
directory creation fails without aborting the filesystem.
Tests:
- make -j4 bzImage
- make -C tools/testing/selftests/filesystems/ext4 clean all
- sh -n tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
- Booted the v8 kernel in QEMU/KVM with a 1 KiB-block, 128-byte-inode
ea_inode filesystem and 1 MiB journal; all 4 harness tests passed.
- e2fsck -fn passed after the guest unmounted the filesystem.
---
MAINTAINERS | 1 +
fs/ext4/acl.c | 3 +-
fs/ext4/crypto.c | 3 +-
fs/ext4/ialloc.c | 18 +-
fs/ext4/xattr.c | 116 ++++-----
fs/ext4/xattr.h | 8 +-
fs/jbd2/transaction.c | 9 +
include/linux/jbd2.h | 2 +
tools/testing/selftests/Makefile | 1 +
.../selftests/filesystems/ext4/.gitignore | 2 +
.../selftests/filesystems/ext4/Makefile | 6 +
.../ext4/ext4_xattr_journal_credits.c | 238 ++++++++++++++++++
.../ext4/ext4_xattr_journal_credits.sh | 57 +++++
13 files changed, 392 insertions(+), 72 deletions(-)
create mode 100644 tools/testing/selftests/filesystems/ext4/.gitignore
create mode 100644 tools/testing/selftests/filesystems/ext4/Makefile
create mode 100644 tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
create mode 100755 tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
diff --git a/MAINTAINERS b/MAINTAINERS
index 2ac5128b7..1debcbeb0 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -10131,6 +10131,7 @@ F: Documentation/filesystems/ext4/
F: fs/ext4/
F: include/trace/events/ext4.h
F: include/uapi/linux/ext4.h
+F: tools/testing/selftests/filesystems/ext4/
Extended Verification Module (EVM)
M: Mimi Zohar <zohar@linux.ibm.com>
diff --git a/fs/ext4/acl.c b/fs/ext4/acl.c
index 59fac55a2..167a410ef 100644
--- a/fs/ext4/acl.c
+++ b/fs/ext4/acl.c
@@ -239,8 +239,7 @@ ext4_set_acl(const struct mnt_idmap *idmap, struct dentry *dentry,
if (error)
return error;
retry:
- error = ext4_xattr_set_credits(inode, acl_size, false /* is_create */,
- &credits);
+ error = ext4_xattr_set_credits(inode, acl_size, &credits);
if (error)
return error;
diff --git a/fs/ext4/crypto.c b/fs/ext4/crypto.c
index 318b2eaea..c1275009e 100644
--- a/fs/ext4/crypto.c
+++ b/fs/ext4/crypto.c
@@ -203,8 +203,7 @@ static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
if (res)
return res;
retry:
- res = ext4_xattr_set_credits(inode, len, false /* is_create */,
- &credits);
+ res = ext4_xattr_set_credits(inode, len, &credits);
if (res)
return res;
diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
index 527f1f703..49e556a66 100644
--- a/fs/ext4/ialloc.c
+++ b/fs/ext4/ialloc.c
@@ -888,8 +888,7 @@ static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
nblocks += (S_ISDIR(mode) ? 2 : 1) *
__ext4_xattr_set_credits(sb, NULL /* inode */,
- NULL /* block_bh */, acl_size,
- true /* is_create */);
+ NULL /* block_bh */, acl_size);
posix_acl_release(p);
}
#endif
@@ -907,16 +906,14 @@ static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
*/
nblocks += num_security_xattrs *
__ext4_xattr_set_credits(sb, NULL /* inode */,
- NULL /* block_bh */, 1024,
- true /* is_create */);
+ NULL /* block_bh */, 1024);
}
#endif
if (encrypt)
nblocks += __ext4_xattr_set_credits(sb,
NULL /* inode */,
NULL /* block_bh */,
- FSCRYPT_SET_CONTEXT_MAX_SIZE,
- true /* is_create */);
+ FSCRYPT_SET_CONTEXT_MAX_SIZE);
return nblocks;
}
@@ -1006,11 +1003,20 @@ struct inode *__ext4_new_inode(const struct mnt_idmap *idmap,
goto out;
if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) {
+ int max_credits, revoke_credits;
+ journal_t *journal = sbi->s_journal;
+
ret2 = ext4_xattr_credits_for_new_inode(dir, mode, encrypt);
if (ret2 < 0) {
err = ret2;
goto out;
}
+ revoke_credits = ext4_trans_default_revoke_credits(sb);
+ max_credits = jbd2_journal_get_max_transaction_buffers(journal, revoke_credits);
+ if (nblocks > max_credits || ret2 > max_credits - nblocks) {
+ err = -ENOSPC;
+ goto out;
+ }
nblocks += ret2;
}
diff --git a/fs/ext4/xattr.c b/fs/ext4/xattr.c
index 5c310747b..753265c08 100644
--- a/fs/ext4/xattr.c
+++ b/fs/ext4/xattr.c
@@ -948,8 +948,7 @@ static void ext4_xattr_inode_free_quota(struct inode *parent,
}
int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
- struct buffer_head *block_bh, size_t value_len,
- bool is_create)
+ struct buffer_head *block_bh, size_t value_len)
{
int credits;
int blocks;
@@ -998,24 +997,6 @@ int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
/* Blocks themselves. */
credits += blocks;
- if (!is_create) {
- /* Dereference ea_inode holding old xattr value.
- * Old ea_inode, inode map, block bitmap, group descriptor.
- */
- credits += 4;
-
- /* Data blocks for old ea_inode. */
- blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits;
-
- /* Indirection block or one level of extent tree for old
- * ea_inode.
- */
- blocks += 1;
-
- /* Block bitmap and group descriptor updates for each block. */
- credits += blocks * 2;
- }
-
/* We may need to clone the existing xattr block in which case we need
* to increment ref counts for existing ea_inodes referenced by it.
*/
@@ -2110,6 +2091,16 @@ ext4_xattr_block_set(handle_t *handle, struct inode *inode,
if (error)
goto cleanup_dquot;
}
+ if (ea_inode &&
+ !(bs->bh && s->base == bs->bh->b_data)) {
+ /* The reused block already holds this reference. */
+ error = ext4_xattr_inode_dec_ref(handle, ea_inode);
+ if (error)
+ ext4_warning_inode(ea_inode,
+ "dec ref error=%d", error);
+ ext4_put_ea_inode(ea_inode);
+ ea_inode = NULL;
+ }
mb_cache_entry_touch(ea_block_cache, ce);
mb_cache_entry_put(ea_block_cache, ce);
ce = NULL;
@@ -2375,29 +2366,6 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
ext4_write_lock_xattr(inode, &no_expand);
- /* Check journal credits under write lock. */
- if (ext4_handle_valid(handle)) {
- struct buffer_head *bh;
- int credits;
-
- bh = ext4_xattr_get_block(inode);
- if (IS_ERR(bh)) {
- error = PTR_ERR(bh);
- goto cleanup;
- }
-
- credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
- value_len,
- flags & XATTR_CREATE);
- brelse(bh);
-
- if (jbd2_handle_buffer_credits(handle) < credits) {
- error = -ENOSPC;
- goto cleanup;
- }
- WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS));
- }
-
error = ext4_reserve_inode_write(handle, inode, &is.iloc);
if (error)
goto cleanup;
@@ -2415,6 +2383,28 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
error = ext4_xattr_block_find(inode, &i, &bs);
if (error)
goto cleanup;
+
+ /* Check journal credits under write lock. */
+ if (ext4_handle_valid(handle)) {
+ int credits;
+
+ if (!bs.bh) {
+ struct buffer_head *bh = ext4_xattr_get_block(inode);
+
+ if (IS_ERR(bh)) {
+ error = PTR_ERR(bh);
+ goto cleanup;
+ }
+ bs.bh = bh;
+ }
+ credits = __ext4_xattr_set_credits(inode->i_sb, inode, bs.bh,
+ value_len);
+ if (jbd2_handle_buffer_credits(handle) < credits) {
+ error = -ENOSPC;
+ goto cleanup;
+ }
+ WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS));
+ }
if (is.s.not_found && bs.s.not_found) {
error = -ENODATA;
if (flags & XATTR_REPLACE)
@@ -2501,27 +2491,38 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
}
int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
- bool is_create, int *credits)
+ int *credits)
{
- struct buffer_head *bh;
- int err;
+ struct ext4_xattr_block_find bs = {
+ .s = { .not_found = -ENODATA, },
+ };
+ journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
+ int max_credits, revoke_credits, err;
*credits = 0;
-
- if (!EXT4_SB(inode->i_sb)->s_journal)
+ if (!journal)
return 0;
+ /* xattr_sem protects the block while we count its ea_inode references. */
down_read(&EXT4_I(inode)->xattr_sem);
- bh = ext4_xattr_get_block(inode);
- if (IS_ERR(bh)) {
- err = PTR_ERR(bh);
- } else {
- *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
- value_len, is_create);
- brelse(bh);
- err = 0;
+ if (ext4_has_feature_ea_inode(inode->i_sb)) {
+ struct buffer_head *bh;
+
+ bh = ext4_xattr_get_block(inode);
+ if (IS_ERR(bh)) {
+ err = PTR_ERR(bh);
+ goto out_unlock;
+ }
+ bs.bh = bh;
}
+ *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bs.bh,
+ value_len);
+ revoke_credits = ext4_trans_default_revoke_credits(inode->i_sb);
+ max_credits = jbd2_journal_get_max_transaction_buffers(journal, revoke_credits);
+ err = *credits > max_credits ? -ENOSPC : 0;
+ brelse(bs.bh);
+out_unlock:
up_read(&EXT4_I(inode)->xattr_sem);
return err;
@@ -2549,8 +2550,7 @@ ext4_xattr_set(struct inode *inode, int name_index, const char *name,
return error;
retry:
- error = ext4_xattr_set_credits(inode, value_len, flags & XATTR_CREATE,
- &credits);
+ error = ext4_xattr_set_credits(inode, value_len, &credits);
if (error)
return error;
diff --git a/fs/ext4/xattr.h b/fs/ext4/xattr.h
index 821dc6a50..eb6e85036 100644
--- a/fs/ext4/xattr.h
+++ b/fs/ext4/xattr.h
@@ -175,11 +175,11 @@ extern ssize_t ext4_listxattr(struct dentry *, char *, size_t);
extern int ext4_xattr_get(struct inode *, int, const char *, void *, size_t);
extern int ext4_xattr_set(struct inode *, int, const char *, const void *, size_t, int);
extern int ext4_xattr_set_handle(handle_t *, struct inode *, int, const char *, const void *, size_t, int);
-extern int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
- bool is_create, int *credits);
+int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
+ int *credits);
extern int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
- struct buffer_head *block_bh, size_t value_len,
- bool is_create);
+ struct buffer_head *block_bh,
+ size_t value_len);
extern int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
int extra_credits);
diff --git a/fs/jbd2/transaction.c b/fs/jbd2/transaction.c
index 85d84d909..5fe49f118 100644
--- a/fs/jbd2/transaction.c
+++ b/fs/jbd2/transaction.c
@@ -194,6 +194,15 @@ static int jbd2_max_user_trans_buffers(journal_t *journal)
journal->j_transaction_overhead_buffers;
}
+/* Maximum buffer credits available after reserving @revoke_records. */
+int jbd2_journal_get_max_transaction_buffers(journal_t *journal,
+ int revoke_records)
+{
+ return jbd2_max_user_trans_buffers(journal) -
+ DIV_ROUND_UP(revoke_records, journal->j_revoke_records_per_block);
+}
+EXPORT_SYMBOL(jbd2_journal_get_max_transaction_buffers);
+
/*
* Wait until we can add credits for handle to the running transaction. Called
* with j_state_lock held for reading. Returns 0 if handle joined the running
diff --git a/include/linux/jbd2.h b/include/linux/jbd2.h
index e3886963b..a72f14440 100644
--- a/include/linux/jbd2.h
+++ b/include/linux/jbd2.h
@@ -1523,6 +1523,8 @@ static inline handle_t *journal_current_handle(void)
*/
extern handle_t *jbd2_journal_start(journal_t *, int nblocks);
+int jbd2_journal_get_max_transaction_buffers(journal_t *journal,
+ int revoke_records);
extern handle_t *jbd2__journal_start(journal_t *, int blocks, int rsv_blocks,
int revoke_records, gfp_t gfp_mask,
unsigned int type, unsigned int line_no);
diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
index 07c3719f8..c16b1132e 100644
--- a/tools/testing/selftests/Makefile
+++ b/tools/testing/selftests/Makefile
@@ -37,6 +37,7 @@ TARGETS += filesystems/binderfs
TARGETS += filesystems/configfs
TARGETS += filesystems/epoll
TARGETS += filesystems/eventfd
+TARGETS += filesystems/ext4
TARGETS += filesystems/failfs
TARGETS += filesystems/fat
TARGETS += filesystems/file_stressor
diff --git a/tools/testing/selftests/filesystems/ext4/.gitignore b/tools/testing/selftests/filesystems/ext4/.gitignore
new file mode 100644
index 000000000..d3678b52d
--- /dev/null
+++ b/tools/testing/selftests/filesystems/ext4/.gitignore
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0
+/ext4_xattr_journal_credits
diff --git a/tools/testing/selftests/filesystems/ext4/Makefile b/tools/testing/selftests/filesystems/ext4/Makefile
new file mode 100644
index 000000000..90569a1cc
--- /dev/null
+++ b/tools/testing/selftests/filesystems/ext4/Makefile
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0
+
+TEST_GEN_PROGS_EXTENDED := ext4_xattr_journal_credits
+TEST_PROGS := ext4_xattr_journal_credits.sh
+
+include ../../lib.mk
diff --git a/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
new file mode 100644
index 000000000..450fd5212
--- /dev/null
+++ b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
@@ -0,0 +1,238 @@
+// SPDX-License-Identifier: GPL-2.0
+#define _GNU_SOURCE
+#include <errno.h>
+#include <endian.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/xattr.h>
+#include <unistd.h>
+
+#include <linux/posix_acl.h>
+#include <linux/posix_acl_xattr.h>
+
+#include "../../kselftest_harness.h"
+
+/* Near XATTR_SIZE_MAX to exercise the largest ea_inode credit estimate. */
+#define LARGE_VALUE_SIZE 65296
+#define NAMED_ACL_ENTRIES 2500
+
+static const char large_name[] = "user.ext4-credit-large";
+static const char small_name[] = "user.ext4-credit-small";
+
+FIXTURE(ext4_xattr_journal_credits)
+{
+ char path[PATH_MAX];
+ char acl_child[PATH_MAX];
+ char shared_a[PATH_MAX];
+ char shared_b[PATH_MAX];
+ char *value;
+ char *readback;
+};
+
+FIXTURE_SETUP(ext4_xattr_journal_credits)
+{
+ int len;
+
+ len = snprintf(self->path, sizeof(self->path), "credit-test-%d",
+ getpid());
+ ASSERT_GE(len, 0);
+ ASSERT_LT(len, (int)sizeof(self->path));
+ len = snprintf(self->acl_child, sizeof(self->acl_child), "%s/child",
+ self->path);
+ ASSERT_GE(len, 0);
+ ASSERT_LT(len, (int)sizeof(self->acl_child));
+ len = snprintf(self->shared_a, sizeof(self->shared_a), "shared-a-%d",
+ getpid());
+ ASSERT_GE(len, 0);
+ ASSERT_LT(len, (int)sizeof(self->shared_a));
+ len = snprintf(self->shared_b, sizeof(self->shared_b), "shared-b-%d",
+ getpid());
+ ASSERT_GE(len, 0);
+ ASSERT_LT(len, (int)sizeof(self->shared_b));
+
+ self->value = malloc(LARGE_VALUE_SIZE);
+ ASSERT_NE(NULL, self->value);
+ self->readback = malloc(LARGE_VALUE_SIZE);
+ ASSERT_NE(NULL, self->readback);
+}
+
+FIXTURE_TEARDOWN(ext4_xattr_journal_credits)
+{
+ unlink(self->path);
+ unlink(self->shared_a);
+ unlink(self->shared_b);
+ rmdir(self->acl_child);
+ rmdir(self->path);
+ rmdir(self->shared_a);
+ rmdir(self->shared_b);
+ free(self->readback);
+ free(self->value);
+}
+
+static void set_acl_entry(struct posix_acl_xattr_entry *entry, uint16_t tag,
+ uint16_t perm, uint32_t id)
+{
+ entry->e_tag = htole16(tag);
+ entry->e_perm = htole16(perm);
+ entry->e_id = htole32(id);
+}
+
+static void *make_large_default_acl(size_t *size)
+{
+ struct posix_acl_xattr_header *header;
+ struct posix_acl_xattr_entry *entry;
+ size_t entry_count = NAMED_ACL_ENTRIES * 2 + 4;
+ unsigned int i;
+
+ *size = sizeof(*header) + entry_count * sizeof(*entry);
+ header = calloc(1, *size);
+ if (!header)
+ return NULL;
+ header->a_version = htole32(POSIX_ACL_XATTR_VERSION);
+ entry = (void *)(header + 1);
+ set_acl_entry(entry++, ACL_USER_OBJ, 7, UINT32_MAX);
+ for (i = 0; i < NAMED_ACL_ENTRIES; i++)
+ set_acl_entry(entry++, ACL_USER, 4, i + 1);
+ set_acl_entry(entry++, ACL_GROUP_OBJ, 5, UINT32_MAX);
+ for (i = 0; i < NAMED_ACL_ENTRIES; i++)
+ set_acl_entry(entry++, ACL_GROUP, 4, i + 1);
+ set_acl_entry(entry++, ACL_MASK, 5, UINT32_MAX);
+ set_acl_entry(entry, ACL_OTHER, 0, UINT32_MAX);
+ return header;
+}
+
+static void create_test_file(struct __test_metadata *_metadata,
+ const char *path)
+{
+ int fd = open(path, O_CREAT | O_EXCL | O_RDWR, 0600);
+
+ ASSERT_GE(fd, 0) {
+ TH_LOG("open(%s): %s", path, strerror(errno));
+ }
+ ASSERT_EQ(0, close(fd));
+}
+
+static void check_xattr_value(struct __test_metadata *_metadata,
+ const char *path,
+ const char *name, const void *expected,
+ size_t size, char *buffer)
+{
+ ssize_t len = getxattr(path, name, buffer, size);
+
+ ASSERT_EQ((ssize_t)size, len) {
+ TH_LOG("getxattr(%s): %s", name,
+ len < 0 ? strerror(errno) : "unexpected value size");
+ }
+ ASSERT_EQ(0, memcmp(buffer, expected, size));
+}
+
+TEST_F(ext4_xattr_journal_credits, large_xattr_lifecycle)
+{
+ char *expected = malloc(LARGE_VALUE_SIZE);
+ int ret;
+
+ ASSERT_NE(NULL, expected);
+ create_test_file(_metadata, self->path);
+ memset(self->value, 0x5a, LARGE_VALUE_SIZE);
+ self->value[0] = 1;
+
+ ASSERT_EQ(0, setxattr(self->path, large_name, self->value,
+ LARGE_VALUE_SIZE, 0));
+ check_xattr_value(_metadata, self->path, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+
+ memset(self->value, 0xa5, LARGE_VALUE_SIZE);
+ self->value[0] = 2;
+ ASSERT_EQ(0, setxattr(self->path, large_name, self->value,
+ LARGE_VALUE_SIZE, XATTR_REPLACE));
+ check_xattr_value(_metadata, self->path, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+
+ memcpy(expected, self->value, LARGE_VALUE_SIZE);
+ memset(self->value, 0x3c, LARGE_VALUE_SIZE);
+ self->value[0] = 3;
+ errno = 0;
+ ret = setxattr(self->path, large_name, self->value,
+ LARGE_VALUE_SIZE, XATTR_CREATE);
+ ASSERT_EQ(-1, ret);
+ ASSERT_EQ(EEXIST, errno);
+ check_xattr_value(_metadata, self->path, large_name, expected,
+ LARGE_VALUE_SIZE, self->readback);
+
+ ASSERT_EQ(0, removexattr(self->path, large_name));
+ free(expected);
+}
+
+TEST_F(ext4_xattr_journal_credits, shared_block_reuses_same_ea_inode)
+{
+ /* The test image has 128-byte inodes, so both xattrs use external blocks. */
+ create_test_file(_metadata, self->shared_a);
+ create_test_file(_metadata, self->shared_b);
+ memset(self->value, 0x7e, LARGE_VALUE_SIZE);
+ self->value[0] = 0x42;
+
+ ASSERT_EQ(0, setxattr(self->shared_a, large_name, self->value,
+ LARGE_VALUE_SIZE, XATTR_CREATE));
+ ASSERT_EQ(0, setxattr(self->shared_b, large_name, self->value,
+ LARGE_VALUE_SIZE, XATTR_CREATE));
+ check_xattr_value(_metadata, self->shared_a, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+ check_xattr_value(_metadata, self->shared_b, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+
+ /* Clone the shared block, then deduplicate back to the original block. */
+ ASSERT_EQ(0, setxattr(self->shared_a, large_name, self->value,
+ LARGE_VALUE_SIZE, XATTR_REPLACE));
+ check_xattr_value(_metadata, self->shared_a, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+ check_xattr_value(_metadata, self->shared_b, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+
+ ASSERT_EQ(0, removexattr(self->shared_a, large_name));
+ ASSERT_EQ(0, removexattr(self->shared_b, large_name));
+}
+
+TEST_F(ext4_xattr_journal_credits, small_xattr_round_trip)
+{
+ static const char value[] = "small-xattr";
+ char readback[sizeof(value)];
+
+ create_test_file(_metadata, self->path);
+ ASSERT_EQ(0, setxattr(self->path, small_name, value, sizeof(value), 0));
+ ASSERT_EQ((ssize_t)sizeof(value),
+ getxattr(self->path, small_name, readback, sizeof(readback)));
+ ASSERT_EQ(0, memcmp(value, readback, sizeof(value)));
+ ASSERT_EQ(0, removexattr(self->path, small_name));
+}
+
+TEST_F(ext4_xattr_journal_credits, large_default_acl_respects_journal_limit)
+{
+ static const char small_value[] = "filesystem-still-writable";
+ size_t acl_size;
+ char *acl = make_large_default_acl(&acl_size);
+ int ret;
+
+ ASSERT_NE(NULL, acl);
+ ASSERT_EQ(0, mkdir(self->path, 0700));
+ ASSERT_EQ(0, setxattr(self->path, "system.posix_acl_default", acl,
+ acl_size, 0));
+
+ errno = 0;
+ ret = mkdir(self->acl_child, 0700);
+ ASSERT_EQ(-1, ret);
+ ASSERT_EQ(ENOSPC, errno);
+
+ /* The rejected transaction must not abort the journal. */
+ ASSERT_EQ(0, setxattr(self->path, small_name, small_value,
+ sizeof(small_value), 0));
+ ASSERT_EQ(0, removexattr(self->path, small_name));
+ free(acl);
+}
+
+TEST_HARNESS_MAIN
diff --git a/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
new file mode 100755
index 000000000..977cdcf47
--- /dev/null
+++ b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+
+KSFT_SKIP=4
+
+skip()
+{
+ printf 'SKIP: %s\n' "$1"
+ exit "$KSFT_SKIP"
+}
+
+[ "$(id -u)" -eq 0 ] || skip "requires root to mount a disposable EXT4 image"
+
+for command in mkfs.ext4 mount umount truncate e2fsck losetup grep; do
+ command -v "$command" >/dev/null 2>&1 || skip "missing $command"
+done
+
+grep -qw ext4 /proc/filesystems || skip "EXT4 is not available in this kernel"
+losetup -f >/dev/null 2>&1 || skip "no loop device is available"
+
+test_dir=$(mktemp -d) || exit 1
+mounted=0
+
+cleanup()
+{
+ if [ "$mounted" -eq 1 ]; then
+ umount "$test_dir/mnt" || true
+ fi
+ rm -rf "$test_dir"
+}
+trap cleanup EXIT INT TERM
+
+truncate -s 32M "$test_dir/fs.img" || exit 1
+# 128-byte inodes have no ibody xattr space, forcing the external-block path.
+if ! mkfs.ext4 -q -F -b 1024 -I 128 -O ea_inode -J size=1 "$test_dir/fs.img" \
+ 2>"$test_dir/mkfs.err"; then
+ cat "$test_dir/mkfs.err" >&2
+ skip "mkfs.ext4 does not support the ea_inode test image"
+fi
+mkdir "$test_dir/mnt" || exit 1
+if ! mount -t ext4 -o loop "$test_dir/fs.img" "$test_dir/mnt" \
+ 2>"$test_dir/mount.err"; then
+ cat "$test_dir/mount.err" >&2
+ if grep -Eqi 'permission denied|operation not permitted|not permitted' \
+ "$test_dir/mount.err"; then
+ skip "mounting a loop image is not permitted"
+ fi
+ exit 1
+fi
+mounted=1
+
+test_bin="$(cd "$(dirname "$0")" && pwd)/ext4_xattr_journal_credits"
+(cd "$test_dir/mnt" && "$test_bin") || exit $?
+
+umount "$test_dir/mnt" || exit 1
+mounted=0
+e2fsck -fn "$test_dir/fs.img"
base-commit: c44104f3642da720164042623e4832836aefdd8e
--
2.56.1
^ permalink raw reply related [flat|nested] 22+ messages in thread* Re: [PATCH v8] ext4: bound xattr journal credits to transaction limit
2026-11-25 7:00 ` [PATCH v8] " arzuagapozosergio
@ 2026-11-25 7:16 ` sashiko-bot
2026-11-25 7:20 ` [PATCH v9] " arzuagapozosergio
1 sibling, 0 replies; 22+ messages in thread
From: sashiko-bot @ 2026-11-25 7:16 UTC (permalink / raw)
To: arzuagapozosergio; +Cc: linux-ext4, tytso
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] selftests/ext4: missing skip when POSIX ACLs are unsupported
--
commit 192ecd2babbed3287e070bdb6ff5a60be8553a9b
Author: Ryuji Hiroshi <arzuagapozosergio@gmail.com>
ext4: bound xattr journal credits to transaction limit
This commit drops obsolete journal credit reservations for releasing old
ea_inodes, bounds xattr and new-inode journal credit estimates against the
JBD2 transaction limit, and adds ext4 selftests for these paths.
Link: https://syzbot.org/bug?extid=25d45c0bd716f2b794a7
> diff --git a/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
> new file mode 100644
> index 0000000000000..450fd52121022
> --- /dev/null
> +++ b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
> @@ -0,0 +1,238 @@
[ ... ]
> +TEST_F(ext4_xattr_journal_credits, large_default_acl_respects_journal_limit)
> +{
> + static const char small_value[] = "filesystem-still-writable";
> + size_t acl_size;
> + char *acl = make_large_default_acl(&acl_size);
> + int ret;
> +
> + ASSERT_NE(NULL, acl);
> + ASSERT_EQ(0, mkdir(self->path, 0700));
> + ASSERT_EQ(0, setxattr(self->path, "system.posix_acl_default", acl,
> + acl_size, 0));
[Severity: Low]
Should this skip the test with SKIP() when setxattr() fails with
errno == EOPNOTSUPP? When the kernel is built without
CONFIG_EXT4_FS_POSIX_ACL, setting "system.posix_acl_default" returns
EOPNOTSUPP and this assertion fails the test instead of skipping for the
missing config prerequisite.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261125070002.27868-1-arzuagapozosergio@gmail.com?part=1
^ permalink raw reply [flat|nested] 22+ messages in thread* [PATCH v9] ext4: bound xattr journal credits to transaction limit
2026-11-25 7:00 ` [PATCH v8] " arzuagapozosergio
2026-11-25 7:16 ` sashiko-bot
@ 2026-11-25 7:20 ` arzuagapozosergio
2026-11-25 7:37 ` sashiko-bot
2026-11-25 7:42 ` [PATCH v10] " arzuagapozosergio
1 sibling, 2 replies; 22+ messages in thread
From: arzuagapozosergio @ 2026-11-25 7:20 UTC (permalink / raw)
To: tytso; +Cc: jack, linux-ext4, syzbot+25d45c0bd716f2b794a7, arzuagapozosergio
From: Ryuji Hiroshi <arzuagapozosergio@gmail.com>
When setting an xattr without XATTR_CREATE, ext4 reserves credits for
releasing an old ea_inode even when the attribute does not exist. On a
1 KiB-block filesystem with a minimum journal, this can request more
credits than JBD2 permits, triggering a warning before the operation
fails.
Drop the obsolete credits for releasing an old ea_inode. Since
ext4_put_ea_inode() defers the final iput to a worker, freeing the old
value's blocks does not happen in the current transaction. Continue to
count ea_inode references in the external xattr block for the case where
it must be cloned, and check the estimate against the transaction limit
after reserving the default JBD2 revoke credits. Repeat the estimate under
the xattr write lock before modifying the attribute.
When an identical xattr block is reused from the cache, drop the
provisional ea_inode reference unless the block was modified in place.
Handle both a different cached block and the original block being found
after cloning. Bound the aggregate credits for inherited xattrs during
inode creation against the JBD2 transaction limit before starting a
handle. Add EXT4 selftests for these paths.
Fixes: dec214d00e0d ("ext4: xattr inode deduplication")
Reported-by: syzbot+25d45c0bd716f2b794a7@syzkaller.appspotmail.com
Link: https://syzbot.org/bug?extid=25d45c0bd716f2b794a7
Assisted-by: Codex:GPT-6
Signed-off-by: Ryuji Hiroshi <arzuagapozosergio@gmail.com>
Changes in v9:
- Skip the large default ACL test when the filesystem reports
EOPNOTSUPP, as happens when POSIX ACL support is disabled.
Tests:
- make -C tools/testing/selftests/filesystems/ext4 clean all
- sh -n tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
- Booted the v8 kernel in QEMU/KVM with a 1 KiB-block, 128-byte-inode
ea_inode filesystem and 1 MiB journal; all 4 harness tests passed.
- e2fsck -fn passed after the guest unmounted the filesystem.
---
MAINTAINERS | 1 +
fs/ext4/acl.c | 3 +-
fs/ext4/crypto.c | 3 +-
fs/ext4/ialloc.c | 18 +-
fs/ext4/xattr.c | 116 ++++-----
fs/ext4/xattr.h | 8 +-
fs/jbd2/transaction.c | 9 +
include/linux/jbd2.h | 2 +
tools/testing/selftests/Makefile | 1 +
.../selftests/filesystems/ext4/.gitignore | 2 +
.../selftests/filesystems/ext4/Makefile | 6 +
.../ext4/ext4_xattr_journal_credits.c | 243 ++++++++++++++++++
.../ext4/ext4_xattr_journal_credits.sh | 57 ++++
13 files changed, 397 insertions(+), 72 deletions(-)
create mode 100644 tools/testing/selftests/filesystems/ext4/.gitignore
create mode 100644 tools/testing/selftests/filesystems/ext4/Makefile
create mode 100644 tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
create mode 100755 tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
diff --git a/MAINTAINERS b/MAINTAINERS
index 2ac5128b7..1debcbeb0 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -10131,6 +10131,7 @@ F: Documentation/filesystems/ext4/
F: fs/ext4/
F: include/trace/events/ext4.h
F: include/uapi/linux/ext4.h
+F: tools/testing/selftests/filesystems/ext4/
Extended Verification Module (EVM)
M: Mimi Zohar <zohar@linux.ibm.com>
diff --git a/fs/ext4/acl.c b/fs/ext4/acl.c
index 59fac55a2..167a410ef 100644
--- a/fs/ext4/acl.c
+++ b/fs/ext4/acl.c
@@ -239,8 +239,7 @@ ext4_set_acl(const struct mnt_idmap *idmap, struct dentry *dentry,
if (error)
return error;
retry:
- error = ext4_xattr_set_credits(inode, acl_size, false /* is_create */,
- &credits);
+ error = ext4_xattr_set_credits(inode, acl_size, &credits);
if (error)
return error;
diff --git a/fs/ext4/crypto.c b/fs/ext4/crypto.c
index 318b2eaea..c1275009e 100644
--- a/fs/ext4/crypto.c
+++ b/fs/ext4/crypto.c
@@ -203,8 +203,7 @@ static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
if (res)
return res;
retry:
- res = ext4_xattr_set_credits(inode, len, false /* is_create */,
- &credits);
+ res = ext4_xattr_set_credits(inode, len, &credits);
if (res)
return res;
diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
index 527f1f703..49e556a66 100644
--- a/fs/ext4/ialloc.c
+++ b/fs/ext4/ialloc.c
@@ -888,8 +888,7 @@ static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
nblocks += (S_ISDIR(mode) ? 2 : 1) *
__ext4_xattr_set_credits(sb, NULL /* inode */,
- NULL /* block_bh */, acl_size,
- true /* is_create */);
+ NULL /* block_bh */, acl_size);
posix_acl_release(p);
}
#endif
@@ -907,16 +906,14 @@ static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
*/
nblocks += num_security_xattrs *
__ext4_xattr_set_credits(sb, NULL /* inode */,
- NULL /* block_bh */, 1024,
- true /* is_create */);
+ NULL /* block_bh */, 1024);
}
#endif
if (encrypt)
nblocks += __ext4_xattr_set_credits(sb,
NULL /* inode */,
NULL /* block_bh */,
- FSCRYPT_SET_CONTEXT_MAX_SIZE,
- true /* is_create */);
+ FSCRYPT_SET_CONTEXT_MAX_SIZE);
return nblocks;
}
@@ -1006,11 +1003,20 @@ struct inode *__ext4_new_inode(const struct mnt_idmap *idmap,
goto out;
if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) {
+ int max_credits, revoke_credits;
+ journal_t *journal = sbi->s_journal;
+
ret2 = ext4_xattr_credits_for_new_inode(dir, mode, encrypt);
if (ret2 < 0) {
err = ret2;
goto out;
}
+ revoke_credits = ext4_trans_default_revoke_credits(sb);
+ max_credits = jbd2_journal_get_max_transaction_buffers(journal, revoke_credits);
+ if (nblocks > max_credits || ret2 > max_credits - nblocks) {
+ err = -ENOSPC;
+ goto out;
+ }
nblocks += ret2;
}
diff --git a/fs/ext4/xattr.c b/fs/ext4/xattr.c
index 5c310747b..753265c08 100644
--- a/fs/ext4/xattr.c
+++ b/fs/ext4/xattr.c
@@ -948,8 +948,7 @@ static void ext4_xattr_inode_free_quota(struct inode *parent,
}
int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
- struct buffer_head *block_bh, size_t value_len,
- bool is_create)
+ struct buffer_head *block_bh, size_t value_len)
{
int credits;
int blocks;
@@ -998,24 +997,6 @@ int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
/* Blocks themselves. */
credits += blocks;
- if (!is_create) {
- /* Dereference ea_inode holding old xattr value.
- * Old ea_inode, inode map, block bitmap, group descriptor.
- */
- credits += 4;
-
- /* Data blocks for old ea_inode. */
- blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits;
-
- /* Indirection block or one level of extent tree for old
- * ea_inode.
- */
- blocks += 1;
-
- /* Block bitmap and group descriptor updates for each block. */
- credits += blocks * 2;
- }
-
/* We may need to clone the existing xattr block in which case we need
* to increment ref counts for existing ea_inodes referenced by it.
*/
@@ -2110,6 +2091,16 @@ ext4_xattr_block_set(handle_t *handle, struct inode *inode,
if (error)
goto cleanup_dquot;
}
+ if (ea_inode &&
+ !(bs->bh && s->base == bs->bh->b_data)) {
+ /* The reused block already holds this reference. */
+ error = ext4_xattr_inode_dec_ref(handle, ea_inode);
+ if (error)
+ ext4_warning_inode(ea_inode,
+ "dec ref error=%d", error);
+ ext4_put_ea_inode(ea_inode);
+ ea_inode = NULL;
+ }
mb_cache_entry_touch(ea_block_cache, ce);
mb_cache_entry_put(ea_block_cache, ce);
ce = NULL;
@@ -2375,29 +2366,6 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
ext4_write_lock_xattr(inode, &no_expand);
- /* Check journal credits under write lock. */
- if (ext4_handle_valid(handle)) {
- struct buffer_head *bh;
- int credits;
-
- bh = ext4_xattr_get_block(inode);
- if (IS_ERR(bh)) {
- error = PTR_ERR(bh);
- goto cleanup;
- }
-
- credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
- value_len,
- flags & XATTR_CREATE);
- brelse(bh);
-
- if (jbd2_handle_buffer_credits(handle) < credits) {
- error = -ENOSPC;
- goto cleanup;
- }
- WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS));
- }
-
error = ext4_reserve_inode_write(handle, inode, &is.iloc);
if (error)
goto cleanup;
@@ -2415,6 +2383,28 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
error = ext4_xattr_block_find(inode, &i, &bs);
if (error)
goto cleanup;
+
+ /* Check journal credits under write lock. */
+ if (ext4_handle_valid(handle)) {
+ int credits;
+
+ if (!bs.bh) {
+ struct buffer_head *bh = ext4_xattr_get_block(inode);
+
+ if (IS_ERR(bh)) {
+ error = PTR_ERR(bh);
+ goto cleanup;
+ }
+ bs.bh = bh;
+ }
+ credits = __ext4_xattr_set_credits(inode->i_sb, inode, bs.bh,
+ value_len);
+ if (jbd2_handle_buffer_credits(handle) < credits) {
+ error = -ENOSPC;
+ goto cleanup;
+ }
+ WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS));
+ }
if (is.s.not_found && bs.s.not_found) {
error = -ENODATA;
if (flags & XATTR_REPLACE)
@@ -2501,27 +2491,38 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
}
int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
- bool is_create, int *credits)
+ int *credits)
{
- struct buffer_head *bh;
- int err;
+ struct ext4_xattr_block_find bs = {
+ .s = { .not_found = -ENODATA, },
+ };
+ journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
+ int max_credits, revoke_credits, err;
*credits = 0;
-
- if (!EXT4_SB(inode->i_sb)->s_journal)
+ if (!journal)
return 0;
+ /* xattr_sem protects the block while we count its ea_inode references. */
down_read(&EXT4_I(inode)->xattr_sem);
- bh = ext4_xattr_get_block(inode);
- if (IS_ERR(bh)) {
- err = PTR_ERR(bh);
- } else {
- *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
- value_len, is_create);
- brelse(bh);
- err = 0;
+ if (ext4_has_feature_ea_inode(inode->i_sb)) {
+ struct buffer_head *bh;
+
+ bh = ext4_xattr_get_block(inode);
+ if (IS_ERR(bh)) {
+ err = PTR_ERR(bh);
+ goto out_unlock;
+ }
+ bs.bh = bh;
}
+ *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bs.bh,
+ value_len);
+ revoke_credits = ext4_trans_default_revoke_credits(inode->i_sb);
+ max_credits = jbd2_journal_get_max_transaction_buffers(journal, revoke_credits);
+ err = *credits > max_credits ? -ENOSPC : 0;
+ brelse(bs.bh);
+out_unlock:
up_read(&EXT4_I(inode)->xattr_sem);
return err;
@@ -2549,8 +2550,7 @@ ext4_xattr_set(struct inode *inode, int name_index, const char *name,
return error;
retry:
- error = ext4_xattr_set_credits(inode, value_len, flags & XATTR_CREATE,
- &credits);
+ error = ext4_xattr_set_credits(inode, value_len, &credits);
if (error)
return error;
diff --git a/fs/ext4/xattr.h b/fs/ext4/xattr.h
index 821dc6a50..eb6e85036 100644
--- a/fs/ext4/xattr.h
+++ b/fs/ext4/xattr.h
@@ -175,11 +175,11 @@ extern ssize_t ext4_listxattr(struct dentry *, char *, size_t);
extern int ext4_xattr_get(struct inode *, int, const char *, void *, size_t);
extern int ext4_xattr_set(struct inode *, int, const char *, const void *, size_t, int);
extern int ext4_xattr_set_handle(handle_t *, struct inode *, int, const char *, const void *, size_t, int);
-extern int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
- bool is_create, int *credits);
+int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
+ int *credits);
extern int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
- struct buffer_head *block_bh, size_t value_len,
- bool is_create);
+ struct buffer_head *block_bh,
+ size_t value_len);
extern int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
int extra_credits);
diff --git a/fs/jbd2/transaction.c b/fs/jbd2/transaction.c
index 85d84d909..5fe49f118 100644
--- a/fs/jbd2/transaction.c
+++ b/fs/jbd2/transaction.c
@@ -194,6 +194,15 @@ static int jbd2_max_user_trans_buffers(journal_t *journal)
journal->j_transaction_overhead_buffers;
}
+/* Maximum buffer credits available after reserving @revoke_records. */
+int jbd2_journal_get_max_transaction_buffers(journal_t *journal,
+ int revoke_records)
+{
+ return jbd2_max_user_trans_buffers(journal) -
+ DIV_ROUND_UP(revoke_records, journal->j_revoke_records_per_block);
+}
+EXPORT_SYMBOL(jbd2_journal_get_max_transaction_buffers);
+
/*
* Wait until we can add credits for handle to the running transaction. Called
* with j_state_lock held for reading. Returns 0 if handle joined the running
diff --git a/include/linux/jbd2.h b/include/linux/jbd2.h
index e3886963b..a72f14440 100644
--- a/include/linux/jbd2.h
+++ b/include/linux/jbd2.h
@@ -1523,6 +1523,8 @@ static inline handle_t *journal_current_handle(void)
*/
extern handle_t *jbd2_journal_start(journal_t *, int nblocks);
+int jbd2_journal_get_max_transaction_buffers(journal_t *journal,
+ int revoke_records);
extern handle_t *jbd2__journal_start(journal_t *, int blocks, int rsv_blocks,
int revoke_records, gfp_t gfp_mask,
unsigned int type, unsigned int line_no);
diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
index 07c3719f8..c16b1132e 100644
--- a/tools/testing/selftests/Makefile
+++ b/tools/testing/selftests/Makefile
@@ -37,6 +37,7 @@ TARGETS += filesystems/binderfs
TARGETS += filesystems/configfs
TARGETS += filesystems/epoll
TARGETS += filesystems/eventfd
+TARGETS += filesystems/ext4
TARGETS += filesystems/failfs
TARGETS += filesystems/fat
TARGETS += filesystems/file_stressor
diff --git a/tools/testing/selftests/filesystems/ext4/.gitignore b/tools/testing/selftests/filesystems/ext4/.gitignore
new file mode 100644
index 000000000..d3678b52d
--- /dev/null
+++ b/tools/testing/selftests/filesystems/ext4/.gitignore
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0
+/ext4_xattr_journal_credits
diff --git a/tools/testing/selftests/filesystems/ext4/Makefile b/tools/testing/selftests/filesystems/ext4/Makefile
new file mode 100644
index 000000000..90569a1cc
--- /dev/null
+++ b/tools/testing/selftests/filesystems/ext4/Makefile
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0
+
+TEST_GEN_PROGS_EXTENDED := ext4_xattr_journal_credits
+TEST_PROGS := ext4_xattr_journal_credits.sh
+
+include ../../lib.mk
diff --git a/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
new file mode 100644
index 000000000..8cb3e0172
--- /dev/null
+++ b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
@@ -0,0 +1,243 @@
+// SPDX-License-Identifier: GPL-2.0
+#define _GNU_SOURCE
+#include <errno.h>
+#include <endian.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/xattr.h>
+#include <unistd.h>
+
+#include <linux/posix_acl.h>
+#include <linux/posix_acl_xattr.h>
+
+#include "../../kselftest_harness.h"
+
+/* Near XATTR_SIZE_MAX to exercise the largest ea_inode credit estimate. */
+#define LARGE_VALUE_SIZE 65296
+#define NAMED_ACL_ENTRIES 2500
+
+static const char large_name[] = "user.ext4-credit-large";
+static const char small_name[] = "user.ext4-credit-small";
+
+FIXTURE(ext4_xattr_journal_credits)
+{
+ char path[PATH_MAX];
+ char acl_child[PATH_MAX];
+ char shared_a[PATH_MAX];
+ char shared_b[PATH_MAX];
+ char *value;
+ char *readback;
+};
+
+FIXTURE_SETUP(ext4_xattr_journal_credits)
+{
+ int len;
+
+ len = snprintf(self->path, sizeof(self->path), "credit-test-%d",
+ getpid());
+ ASSERT_GE(len, 0);
+ ASSERT_LT(len, (int)sizeof(self->path));
+ len = snprintf(self->acl_child, sizeof(self->acl_child), "%s/child",
+ self->path);
+ ASSERT_GE(len, 0);
+ ASSERT_LT(len, (int)sizeof(self->acl_child));
+ len = snprintf(self->shared_a, sizeof(self->shared_a), "shared-a-%d",
+ getpid());
+ ASSERT_GE(len, 0);
+ ASSERT_LT(len, (int)sizeof(self->shared_a));
+ len = snprintf(self->shared_b, sizeof(self->shared_b), "shared-b-%d",
+ getpid());
+ ASSERT_GE(len, 0);
+ ASSERT_LT(len, (int)sizeof(self->shared_b));
+
+ self->value = malloc(LARGE_VALUE_SIZE);
+ ASSERT_NE(NULL, self->value);
+ self->readback = malloc(LARGE_VALUE_SIZE);
+ ASSERT_NE(NULL, self->readback);
+}
+
+FIXTURE_TEARDOWN(ext4_xattr_journal_credits)
+{
+ unlink(self->path);
+ unlink(self->shared_a);
+ unlink(self->shared_b);
+ rmdir(self->acl_child);
+ rmdir(self->path);
+ rmdir(self->shared_a);
+ rmdir(self->shared_b);
+ free(self->readback);
+ free(self->value);
+}
+
+static void set_acl_entry(struct posix_acl_xattr_entry *entry, uint16_t tag,
+ uint16_t perm, uint32_t id)
+{
+ entry->e_tag = htole16(tag);
+ entry->e_perm = htole16(perm);
+ entry->e_id = htole32(id);
+}
+
+static void *make_large_default_acl(size_t *size)
+{
+ struct posix_acl_xattr_header *header;
+ struct posix_acl_xattr_entry *entry;
+ size_t entry_count = NAMED_ACL_ENTRIES * 2 + 4;
+ unsigned int i;
+
+ *size = sizeof(*header) + entry_count * sizeof(*entry);
+ header = calloc(1, *size);
+ if (!header)
+ return NULL;
+ header->a_version = htole32(POSIX_ACL_XATTR_VERSION);
+ entry = (void *)(header + 1);
+ set_acl_entry(entry++, ACL_USER_OBJ, 7, UINT32_MAX);
+ for (i = 0; i < NAMED_ACL_ENTRIES; i++)
+ set_acl_entry(entry++, ACL_USER, 4, i + 1);
+ set_acl_entry(entry++, ACL_GROUP_OBJ, 5, UINT32_MAX);
+ for (i = 0; i < NAMED_ACL_ENTRIES; i++)
+ set_acl_entry(entry++, ACL_GROUP, 4, i + 1);
+ set_acl_entry(entry++, ACL_MASK, 5, UINT32_MAX);
+ set_acl_entry(entry, ACL_OTHER, 0, UINT32_MAX);
+ return header;
+}
+
+static void create_test_file(struct __test_metadata *_metadata,
+ const char *path)
+{
+ int fd = open(path, O_CREAT | O_EXCL | O_RDWR, 0600);
+
+ ASSERT_GE(fd, 0) {
+ TH_LOG("open(%s): %s", path, strerror(errno));
+ }
+ ASSERT_EQ(0, close(fd));
+}
+
+static void check_xattr_value(struct __test_metadata *_metadata,
+ const char *path,
+ const char *name, const void *expected,
+ size_t size, char *buffer)
+{
+ ssize_t len = getxattr(path, name, buffer, size);
+
+ ASSERT_EQ((ssize_t)size, len) {
+ TH_LOG("getxattr(%s): %s", name,
+ len < 0 ? strerror(errno) : "unexpected value size");
+ }
+ ASSERT_EQ(0, memcmp(buffer, expected, size));
+}
+
+TEST_F(ext4_xattr_journal_credits, large_xattr_lifecycle)
+{
+ char *expected = malloc(LARGE_VALUE_SIZE);
+ int ret;
+
+ ASSERT_NE(NULL, expected);
+ create_test_file(_metadata, self->path);
+ memset(self->value, 0x5a, LARGE_VALUE_SIZE);
+ self->value[0] = 1;
+
+ ASSERT_EQ(0, setxattr(self->path, large_name, self->value,
+ LARGE_VALUE_SIZE, 0));
+ check_xattr_value(_metadata, self->path, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+
+ memset(self->value, 0xa5, LARGE_VALUE_SIZE);
+ self->value[0] = 2;
+ ASSERT_EQ(0, setxattr(self->path, large_name, self->value,
+ LARGE_VALUE_SIZE, XATTR_REPLACE));
+ check_xattr_value(_metadata, self->path, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+
+ memcpy(expected, self->value, LARGE_VALUE_SIZE);
+ memset(self->value, 0x3c, LARGE_VALUE_SIZE);
+ self->value[0] = 3;
+ errno = 0;
+ ret = setxattr(self->path, large_name, self->value,
+ LARGE_VALUE_SIZE, XATTR_CREATE);
+ ASSERT_EQ(-1, ret);
+ ASSERT_EQ(EEXIST, errno);
+ check_xattr_value(_metadata, self->path, large_name, expected,
+ LARGE_VALUE_SIZE, self->readback);
+
+ ASSERT_EQ(0, removexattr(self->path, large_name));
+ free(expected);
+}
+
+TEST_F(ext4_xattr_journal_credits, shared_block_reuses_same_ea_inode)
+{
+ /* The test image has 128-byte inodes, so both xattrs use external blocks. */
+ create_test_file(_metadata, self->shared_a);
+ create_test_file(_metadata, self->shared_b);
+ memset(self->value, 0x7e, LARGE_VALUE_SIZE);
+ self->value[0] = 0x42;
+
+ ASSERT_EQ(0, setxattr(self->shared_a, large_name, self->value,
+ LARGE_VALUE_SIZE, XATTR_CREATE));
+ ASSERT_EQ(0, setxattr(self->shared_b, large_name, self->value,
+ LARGE_VALUE_SIZE, XATTR_CREATE));
+ check_xattr_value(_metadata, self->shared_a, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+ check_xattr_value(_metadata, self->shared_b, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+
+ /* Clone the shared block, then deduplicate back to the original block. */
+ ASSERT_EQ(0, setxattr(self->shared_a, large_name, self->value,
+ LARGE_VALUE_SIZE, XATTR_REPLACE));
+ check_xattr_value(_metadata, self->shared_a, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+ check_xattr_value(_metadata, self->shared_b, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+
+ ASSERT_EQ(0, removexattr(self->shared_a, large_name));
+ ASSERT_EQ(0, removexattr(self->shared_b, large_name));
+}
+
+TEST_F(ext4_xattr_journal_credits, small_xattr_round_trip)
+{
+ static const char value[] = "small-xattr";
+ char readback[sizeof(value)];
+
+ create_test_file(_metadata, self->path);
+ ASSERT_EQ(0, setxattr(self->path, small_name, value, sizeof(value), 0));
+ ASSERT_EQ((ssize_t)sizeof(value),
+ getxattr(self->path, small_name, readback, sizeof(readback)));
+ ASSERT_EQ(0, memcmp(value, readback, sizeof(value)));
+ ASSERT_EQ(0, removexattr(self->path, small_name));
+}
+
+TEST_F(ext4_xattr_journal_credits, large_default_acl_respects_journal_limit)
+{
+ static const char small_value[] = "filesystem-still-writable";
+ size_t acl_size;
+ char *acl = make_large_default_acl(&acl_size);
+ int ret;
+
+ ASSERT_NE(NULL, acl);
+ ASSERT_EQ(0, mkdir(self->path, 0700));
+ ret = setxattr(self->path, "system.posix_acl_default", acl,
+ acl_size, 0);
+ if (ret < 0 && errno == EOPNOTSUPP)
+ SKIP(return, "POSIX ACLs are not supported");
+ ASSERT_EQ(0, ret) {
+ TH_LOG("set default ACL: %s", strerror(errno));
+ }
+
+ errno = 0;
+ ret = mkdir(self->acl_child, 0700);
+ ASSERT_EQ(-1, ret);
+ ASSERT_EQ(ENOSPC, errno);
+
+ /* The rejected transaction must not abort the journal. */
+ ASSERT_EQ(0, setxattr(self->path, small_name, small_value,
+ sizeof(small_value), 0));
+ ASSERT_EQ(0, removexattr(self->path, small_name));
+ free(acl);
+}
+
+TEST_HARNESS_MAIN
diff --git a/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
new file mode 100755
index 000000000..977cdcf47
--- /dev/null
+++ b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+
+KSFT_SKIP=4
+
+skip()
+{
+ printf 'SKIP: %s\n' "$1"
+ exit "$KSFT_SKIP"
+}
+
+[ "$(id -u)" -eq 0 ] || skip "requires root to mount a disposable EXT4 image"
+
+for command in mkfs.ext4 mount umount truncate e2fsck losetup grep; do
+ command -v "$command" >/dev/null 2>&1 || skip "missing $command"
+done
+
+grep -qw ext4 /proc/filesystems || skip "EXT4 is not available in this kernel"
+losetup -f >/dev/null 2>&1 || skip "no loop device is available"
+
+test_dir=$(mktemp -d) || exit 1
+mounted=0
+
+cleanup()
+{
+ if [ "$mounted" -eq 1 ]; then
+ umount "$test_dir/mnt" || true
+ fi
+ rm -rf "$test_dir"
+}
+trap cleanup EXIT INT TERM
+
+truncate -s 32M "$test_dir/fs.img" || exit 1
+# 128-byte inodes have no ibody xattr space, forcing the external-block path.
+if ! mkfs.ext4 -q -F -b 1024 -I 128 -O ea_inode -J size=1 "$test_dir/fs.img" \
+ 2>"$test_dir/mkfs.err"; then
+ cat "$test_dir/mkfs.err" >&2
+ skip "mkfs.ext4 does not support the ea_inode test image"
+fi
+mkdir "$test_dir/mnt" || exit 1
+if ! mount -t ext4 -o loop "$test_dir/fs.img" "$test_dir/mnt" \
+ 2>"$test_dir/mount.err"; then
+ cat "$test_dir/mount.err" >&2
+ if grep -Eqi 'permission denied|operation not permitted|not permitted' \
+ "$test_dir/mount.err"; then
+ skip "mounting a loop image is not permitted"
+ fi
+ exit 1
+fi
+mounted=1
+
+test_bin="$(cd "$(dirname "$0")" && pwd)/ext4_xattr_journal_credits"
+(cd "$test_dir/mnt" && "$test_bin") || exit $?
+
+umount "$test_dir/mnt" || exit 1
+mounted=0
+e2fsck -fn "$test_dir/fs.img"
base-commit: c44104f3642da720164042623e4832836aefdd8e
--
2.56.1
^ permalink raw reply related [flat|nested] 22+ messages in thread* Re: [PATCH v9] ext4: bound xattr journal credits to transaction limit
2026-11-25 7:20 ` [PATCH v9] " arzuagapozosergio
@ 2026-11-25 7:37 ` sashiko-bot
2026-11-25 7:42 ` [PATCH v10] " arzuagapozosergio
1 sibling, 0 replies; 22+ messages in thread
From: sashiko-bot @ 2026-11-25 7:37 UTC (permalink / raw)
To: arzuagapozosergio; +Cc: linux-ext4, tytso
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Low] selftests: ext4: missing KHDR_INCLUDES in Makefile
- [Low] ext4: changelog notes placed in commit message and missing Fixes
tags
--
commit 3240428a7d5aa26a79642ee4a851967a647d2656
Author: Ryuji Hiroshi <arzuagapozosergio@gmail.com>
> ext4: bound xattr journal credits to transaction limit
>
> When setting an xattr without XATTR_CREATE, ext4 reserves credits for
> releasing an old ea_inode even when the attribute does not exist. On a
> 1 KiB-block filesystem with a minimum journal, this can request more
> credits than JBD2 permits, triggering a warning before the operation
> fails.
>
> Drop the obsolete credits for releasing an old ea_inode. Since
> ext4_put_ea_inode() defers the final iput to a worker, freeing the old
> value's blocks does not happen in the current transaction. Continue to
> count ea_inode references in the external xattr block for the case where
> it must be cloned, and check the estimate against the transaction limit
> after reserving the default JBD2 revoke credits. Repeat the estimate under
> the xattr write lock before modifying the attribute.
>
> When an identical xattr block is reused from the cache, drop the
> provisional ea_inode reference unless the block was modified in place.
> Handle both a different cached block and the original block being found
> after cloning. Bound the aggregate credits for inherited xattrs during
> inode creation against the JBD2 transaction limit before starting a
> handle. Add EXT4 selftests for these paths.
>
> Fixes: dec214d00e0d ("ext4: xattr inode deduplication")
> Reported-by: syzbot+25d45c0bd716f2b794a7@syzkaller.appspotmail.com
> Link: https://syzbot.org/bug?extid=25d45c0bd716f2b794a7
> Assisted-by: Codex:GPT-6
> Signed-off-by: Ryuji Hiroshi <arzuagapozosergio@gmail.com>
>
> Changes in v9:
> - Skip the large default ACL test when the filesystem reports
> EOPNOTSUPP, as happens when POSIX ACL support is disabled.
>
> Tests:
> - make -C tools/testing/selftests/filesystems/ext4 clean all
> - sh -n tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
> - Booted the v8 kernel in QEMU/KVM with a 1 KiB-block, 128-byte-inode
> ea_inode filesystem and 1 MiB journal; all 4 harness tests passed.
> - e2fsck -fn passed after the guest unmounted the filesystem.
[Severity: Low]
The "Changes in v9:" and "Tests:" sections sit above the "---" separator,
so git am will record them in the permanent commit history.
Also, in addition to dec214d00e0d ("ext4: xattr inode deduplication"),
this patch fixes the unbounded new-inode xattr credit calculation in
__ext4_new_inode() and the provisional ea_inode reference leak on cached
xattr block reuse in ext4_xattr_block_set(). Should it also include:
Fixes: af65207c76ce ("ext4: fix __ext4_new_inode() journal credits calculation")
Fixes: 0a46ef234756 ("ext4: do not create EA inode under buffer lock")
[ ... ]
> diff --git a/tools/testing/selftests/filesystems/ext4/Makefile b/tools/testing/selftests/filesystems/ext4/Makefile
> new file mode 100644
> index 0000000000000..90569a1ccef38
> --- /dev/null
> +++ b/tools/testing/selftests/filesystems/ext4/Makefile
> @@ -0,0 +1,6 @@
> +# SPDX-License-Identifier: GPL-2.0
> +
> +TEST_GEN_PROGS_EXTENDED := ext4_xattr_journal_credits
> +TEST_PROGS := ext4_xattr_journal_credits.sh
> +
> +include ../../lib.mk
[Severity: Low]
Should this Makefile add "CFLAGS += $(KHDR_INCLUDES)"?
ext4_xattr_journal_credits.c includes <linux/posix_acl.h> and
<linux/posix_acl_xattr.h>, and lib.mk defines KHDR_INCLUDES without
adding it to CFLAGS automatically, so the build otherwise falls back to
the host's /usr/include.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261125072006.32251-1-arzuagapozosergio@gmail.com?part=1
^ permalink raw reply [flat|nested] 22+ messages in thread* [PATCH v10] ext4: bound xattr journal credits to transaction limit
2026-11-25 7:20 ` [PATCH v9] " arzuagapozosergio
2026-11-25 7:37 ` sashiko-bot
@ 2026-11-25 7:42 ` arzuagapozosergio
2026-11-25 7:46 ` [PATCH v11] " arzuagapozosergio
1 sibling, 1 reply; 22+ messages in thread
From: arzuagapozosergio @ 2026-11-25 7:42 UTC (permalink / raw)
To: tytso; +Cc: jack, linux-ext4, syzbot+25d45c0bd716f2b794a7, arzuagapozosergio
From: Ryuji Hiroshi <arzuagapozosergio@gmail.com>
When setting an xattr without XATTR_CREATE, ext4 reserves credits for
releasing an old ea_inode even when the attribute does not exist. On a
1 KiB-block filesystem with a minimum journal, this can request more
credits than JBD2 permits, triggering a warning before the operation
fails.
Drop the obsolete credits for releasing an old ea_inode. Since
ext4_put_ea_inode() defers the final iput to a worker, freeing the old
value's blocks does not happen in the current transaction. Continue to
count ea_inode references in the external xattr block for the case where
it must be cloned, and check the estimate against the transaction limit
after reserving the default JBD2 revoke credits. Repeat the estimate under
the xattr write lock before modifying the attribute.
When an identical xattr block is reused from the cache, drop the
provisional ea_inode reference unless the block was modified in place.
Handle both a different cached block and the original block being found
after cloning. Bound the aggregate credits for inherited xattrs during
inode creation against the JBD2 transaction limit before starting a
handle. Add EXT4 selftests for these paths.
Fixes: dec214d00e0d ("ext4: xattr inode deduplication")
Fixes: af65207c76ce ("ext4: fix __ext4_new_inode() journal credits calculation")
Fixes: 0a46ef234756 ("ext4: do not create EA inode under buffer lock")
Reported-by: syzbot+25d45c0bd716f2b794a7@syzkaller.appspotmail.com
Link: https://syzbot.org/bug?extid=25d45c0bd716f2b794a7
Signed-off-by: Ryuji Hiroshi <arzuagapozosergio@gmail.com>
---
Changes in v10:
- Add KHDR_INCLUDES to the ext4 selftests CFLAGS.
- Add Fixes tags for the inode-credit and ea_inode reference issues.
- Move version notes and test results below the patch separator.
Changes in v9:
- Skip the large default ACL test when the filesystem reports
EOPNOTSUPP, as happens when POSIX ACL support is disabled.
Tests:
- make -C tools/testing/selftests/filesystems/ext4 clean all
- sh -n tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
- Booted the v8 kernel in QEMU/KVM with a 1 KiB-block, 128-byte-inode
ea_inode filesystem and 1 MiB journal; all 4 harness tests passed.
- e2fsck -fn passed after the guest unmounted the filesystem.
Signed-off-by: Ryuji Hiroshi <arzuagapozosergio@gmail.com>
---
MAINTAINERS | 1 +
fs/ext4/acl.c | 3 +-
fs/ext4/crypto.c | 3 +-
fs/ext4/ialloc.c | 18 +-
fs/ext4/xattr.c | 116 ++++-----
fs/ext4/xattr.h | 8 +-
fs/jbd2/transaction.c | 9 +
include/linux/jbd2.h | 2 +
tools/testing/selftests/Makefile | 1 +
.../selftests/filesystems/ext4/.gitignore | 2 +
.../selftests/filesystems/ext4/Makefile | 8 +
.../ext4/ext4_xattr_journal_credits.c | 243 ++++++++++++++++++
.../ext4/ext4_xattr_journal_credits.sh | 57 ++++
13 files changed, 399 insertions(+), 72 deletions(-)
create mode 100644 tools/testing/selftests/filesystems/ext4/.gitignore
create mode 100644 tools/testing/selftests/filesystems/ext4/Makefile
create mode 100644 tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
create mode 100755 tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
diff --git a/MAINTAINERS b/MAINTAINERS
index 2ac5128b7..1debcbeb0 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -10131,6 +10131,7 @@ F: Documentation/filesystems/ext4/
F: fs/ext4/
F: include/trace/events/ext4.h
F: include/uapi/linux/ext4.h
+F: tools/testing/selftests/filesystems/ext4/
Extended Verification Module (EVM)
M: Mimi Zohar <zohar@linux.ibm.com>
diff --git a/fs/ext4/acl.c b/fs/ext4/acl.c
index 59fac55a2..167a410ef 100644
--- a/fs/ext4/acl.c
+++ b/fs/ext4/acl.c
@@ -239,8 +239,7 @@ ext4_set_acl(const struct mnt_idmap *idmap, struct dentry *dentry,
if (error)
return error;
retry:
- error = ext4_xattr_set_credits(inode, acl_size, false /* is_create */,
- &credits);
+ error = ext4_xattr_set_credits(inode, acl_size, &credits);
if (error)
return error;
diff --git a/fs/ext4/crypto.c b/fs/ext4/crypto.c
index 318b2eaea..c1275009e 100644
--- a/fs/ext4/crypto.c
+++ b/fs/ext4/crypto.c
@@ -203,8 +203,7 @@ static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
if (res)
return res;
retry:
- res = ext4_xattr_set_credits(inode, len, false /* is_create */,
- &credits);
+ res = ext4_xattr_set_credits(inode, len, &credits);
if (res)
return res;
diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
index 527f1f703..49e556a66 100644
--- a/fs/ext4/ialloc.c
+++ b/fs/ext4/ialloc.c
@@ -888,8 +888,7 @@ static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
nblocks += (S_ISDIR(mode) ? 2 : 1) *
__ext4_xattr_set_credits(sb, NULL /* inode */,
- NULL /* block_bh */, acl_size,
- true /* is_create */);
+ NULL /* block_bh */, acl_size);
posix_acl_release(p);
}
#endif
@@ -907,16 +906,14 @@ static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
*/
nblocks += num_security_xattrs *
__ext4_xattr_set_credits(sb, NULL /* inode */,
- NULL /* block_bh */, 1024,
- true /* is_create */);
+ NULL /* block_bh */, 1024);
}
#endif
if (encrypt)
nblocks += __ext4_xattr_set_credits(sb,
NULL /* inode */,
NULL /* block_bh */,
- FSCRYPT_SET_CONTEXT_MAX_SIZE,
- true /* is_create */);
+ FSCRYPT_SET_CONTEXT_MAX_SIZE);
return nblocks;
}
@@ -1006,11 +1003,20 @@ struct inode *__ext4_new_inode(const struct mnt_idmap *idmap,
goto out;
if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) {
+ int max_credits, revoke_credits;
+ journal_t *journal = sbi->s_journal;
+
ret2 = ext4_xattr_credits_for_new_inode(dir, mode, encrypt);
if (ret2 < 0) {
err = ret2;
goto out;
}
+ revoke_credits = ext4_trans_default_revoke_credits(sb);
+ max_credits = jbd2_journal_get_max_transaction_buffers(journal, revoke_credits);
+ if (nblocks > max_credits || ret2 > max_credits - nblocks) {
+ err = -ENOSPC;
+ goto out;
+ }
nblocks += ret2;
}
diff --git a/fs/ext4/xattr.c b/fs/ext4/xattr.c
index 5c310747b..753265c08 100644
--- a/fs/ext4/xattr.c
+++ b/fs/ext4/xattr.c
@@ -948,8 +948,7 @@ static void ext4_xattr_inode_free_quota(struct inode *parent,
}
int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
- struct buffer_head *block_bh, size_t value_len,
- bool is_create)
+ struct buffer_head *block_bh, size_t value_len)
{
int credits;
int blocks;
@@ -998,24 +997,6 @@ int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
/* Blocks themselves. */
credits += blocks;
- if (!is_create) {
- /* Dereference ea_inode holding old xattr value.
- * Old ea_inode, inode map, block bitmap, group descriptor.
- */
- credits += 4;
-
- /* Data blocks for old ea_inode. */
- blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits;
-
- /* Indirection block or one level of extent tree for old
- * ea_inode.
- */
- blocks += 1;
-
- /* Block bitmap and group descriptor updates for each block. */
- credits += blocks * 2;
- }
-
/* We may need to clone the existing xattr block in which case we need
* to increment ref counts for existing ea_inodes referenced by it.
*/
@@ -2110,6 +2091,16 @@ ext4_xattr_block_set(handle_t *handle, struct inode *inode,
if (error)
goto cleanup_dquot;
}
+ if (ea_inode &&
+ !(bs->bh && s->base == bs->bh->b_data)) {
+ /* The reused block already holds this reference. */
+ error = ext4_xattr_inode_dec_ref(handle, ea_inode);
+ if (error)
+ ext4_warning_inode(ea_inode,
+ "dec ref error=%d", error);
+ ext4_put_ea_inode(ea_inode);
+ ea_inode = NULL;
+ }
mb_cache_entry_touch(ea_block_cache, ce);
mb_cache_entry_put(ea_block_cache, ce);
ce = NULL;
@@ -2375,29 +2366,6 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
ext4_write_lock_xattr(inode, &no_expand);
- /* Check journal credits under write lock. */
- if (ext4_handle_valid(handle)) {
- struct buffer_head *bh;
- int credits;
-
- bh = ext4_xattr_get_block(inode);
- if (IS_ERR(bh)) {
- error = PTR_ERR(bh);
- goto cleanup;
- }
-
- credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
- value_len,
- flags & XATTR_CREATE);
- brelse(bh);
-
- if (jbd2_handle_buffer_credits(handle) < credits) {
- error = -ENOSPC;
- goto cleanup;
- }
- WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS));
- }
-
error = ext4_reserve_inode_write(handle, inode, &is.iloc);
if (error)
goto cleanup;
@@ -2415,6 +2383,28 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
error = ext4_xattr_block_find(inode, &i, &bs);
if (error)
goto cleanup;
+
+ /* Check journal credits under write lock. */
+ if (ext4_handle_valid(handle)) {
+ int credits;
+
+ if (!bs.bh) {
+ struct buffer_head *bh = ext4_xattr_get_block(inode);
+
+ if (IS_ERR(bh)) {
+ error = PTR_ERR(bh);
+ goto cleanup;
+ }
+ bs.bh = bh;
+ }
+ credits = __ext4_xattr_set_credits(inode->i_sb, inode, bs.bh,
+ value_len);
+ if (jbd2_handle_buffer_credits(handle) < credits) {
+ error = -ENOSPC;
+ goto cleanup;
+ }
+ WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS));
+ }
if (is.s.not_found && bs.s.not_found) {
error = -ENODATA;
if (flags & XATTR_REPLACE)
@@ -2501,27 +2491,38 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
}
int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
- bool is_create, int *credits)
+ int *credits)
{
- struct buffer_head *bh;
- int err;
+ struct ext4_xattr_block_find bs = {
+ .s = { .not_found = -ENODATA, },
+ };
+ journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
+ int max_credits, revoke_credits, err;
*credits = 0;
-
- if (!EXT4_SB(inode->i_sb)->s_journal)
+ if (!journal)
return 0;
+ /* xattr_sem protects the block while we count its ea_inode references. */
down_read(&EXT4_I(inode)->xattr_sem);
- bh = ext4_xattr_get_block(inode);
- if (IS_ERR(bh)) {
- err = PTR_ERR(bh);
- } else {
- *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
- value_len, is_create);
- brelse(bh);
- err = 0;
+ if (ext4_has_feature_ea_inode(inode->i_sb)) {
+ struct buffer_head *bh;
+
+ bh = ext4_xattr_get_block(inode);
+ if (IS_ERR(bh)) {
+ err = PTR_ERR(bh);
+ goto out_unlock;
+ }
+ bs.bh = bh;
}
+ *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bs.bh,
+ value_len);
+ revoke_credits = ext4_trans_default_revoke_credits(inode->i_sb);
+ max_credits = jbd2_journal_get_max_transaction_buffers(journal, revoke_credits);
+ err = *credits > max_credits ? -ENOSPC : 0;
+ brelse(bs.bh);
+out_unlock:
up_read(&EXT4_I(inode)->xattr_sem);
return err;
@@ -2549,8 +2550,7 @@ ext4_xattr_set(struct inode *inode, int name_index, const char *name,
return error;
retry:
- error = ext4_xattr_set_credits(inode, value_len, flags & XATTR_CREATE,
- &credits);
+ error = ext4_xattr_set_credits(inode, value_len, &credits);
if (error)
return error;
diff --git a/fs/ext4/xattr.h b/fs/ext4/xattr.h
index 821dc6a50..eb6e85036 100644
--- a/fs/ext4/xattr.h
+++ b/fs/ext4/xattr.h
@@ -175,11 +175,11 @@ extern ssize_t ext4_listxattr(struct dentry *, char *, size_t);
extern int ext4_xattr_get(struct inode *, int, const char *, void *, size_t);
extern int ext4_xattr_set(struct inode *, int, const char *, const void *, size_t, int);
extern int ext4_xattr_set_handle(handle_t *, struct inode *, int, const char *, const void *, size_t, int);
-extern int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
- bool is_create, int *credits);
+int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
+ int *credits);
extern int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
- struct buffer_head *block_bh, size_t value_len,
- bool is_create);
+ struct buffer_head *block_bh,
+ size_t value_len);
extern int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
int extra_credits);
diff --git a/fs/jbd2/transaction.c b/fs/jbd2/transaction.c
index 85d84d909..5fe49f118 100644
--- a/fs/jbd2/transaction.c
+++ b/fs/jbd2/transaction.c
@@ -194,6 +194,15 @@ static int jbd2_max_user_trans_buffers(journal_t *journal)
journal->j_transaction_overhead_buffers;
}
+/* Maximum buffer credits available after reserving @revoke_records. */
+int jbd2_journal_get_max_transaction_buffers(journal_t *journal,
+ int revoke_records)
+{
+ return jbd2_max_user_trans_buffers(journal) -
+ DIV_ROUND_UP(revoke_records, journal->j_revoke_records_per_block);
+}
+EXPORT_SYMBOL(jbd2_journal_get_max_transaction_buffers);
+
/*
* Wait until we can add credits for handle to the running transaction. Called
* with j_state_lock held for reading. Returns 0 if handle joined the running
diff --git a/include/linux/jbd2.h b/include/linux/jbd2.h
index e3886963b..a72f14440 100644
--- a/include/linux/jbd2.h
+++ b/include/linux/jbd2.h
@@ -1523,6 +1523,8 @@ static inline handle_t *journal_current_handle(void)
*/
extern handle_t *jbd2_journal_start(journal_t *, int nblocks);
+int jbd2_journal_get_max_transaction_buffers(journal_t *journal,
+ int revoke_records);
extern handle_t *jbd2__journal_start(journal_t *, int blocks, int rsv_blocks,
int revoke_records, gfp_t gfp_mask,
unsigned int type, unsigned int line_no);
diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
index 07c3719f8..c16b1132e 100644
--- a/tools/testing/selftests/Makefile
+++ b/tools/testing/selftests/Makefile
@@ -37,6 +37,7 @@ TARGETS += filesystems/binderfs
TARGETS += filesystems/configfs
TARGETS += filesystems/epoll
TARGETS += filesystems/eventfd
+TARGETS += filesystems/ext4
TARGETS += filesystems/failfs
TARGETS += filesystems/fat
TARGETS += filesystems/file_stressor
diff --git a/tools/testing/selftests/filesystems/ext4/.gitignore b/tools/testing/selftests/filesystems/ext4/.gitignore
new file mode 100644
index 000000000..d3678b52d
--- /dev/null
+++ b/tools/testing/selftests/filesystems/ext4/.gitignore
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0
+/ext4_xattr_journal_credits
diff --git a/tools/testing/selftests/filesystems/ext4/Makefile b/tools/testing/selftests/filesystems/ext4/Makefile
new file mode 100644
index 000000000..fbfe2e7a5
--- /dev/null
+++ b/tools/testing/selftests/filesystems/ext4/Makefile
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0
+
+CFLAGS += $(KHDR_INCLUDES)
+
+TEST_GEN_PROGS_EXTENDED := ext4_xattr_journal_credits
+TEST_PROGS := ext4_xattr_journal_credits.sh
+
+include ../../lib.mk
diff --git a/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
new file mode 100644
index 000000000..8cb3e0172
--- /dev/null
+++ b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
@@ -0,0 +1,243 @@
+// SPDX-License-Identifier: GPL-2.0
+#define _GNU_SOURCE
+#include <errno.h>
+#include <endian.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/xattr.h>
+#include <unistd.h>
+
+#include <linux/posix_acl.h>
+#include <linux/posix_acl_xattr.h>
+
+#include "../../kselftest_harness.h"
+
+/* Near XATTR_SIZE_MAX to exercise the largest ea_inode credit estimate. */
+#define LARGE_VALUE_SIZE 65296
+#define NAMED_ACL_ENTRIES 2500
+
+static const char large_name[] = "user.ext4-credit-large";
+static const char small_name[] = "user.ext4-credit-small";
+
+FIXTURE(ext4_xattr_journal_credits)
+{
+ char path[PATH_MAX];
+ char acl_child[PATH_MAX];
+ char shared_a[PATH_MAX];
+ char shared_b[PATH_MAX];
+ char *value;
+ char *readback;
+};
+
+FIXTURE_SETUP(ext4_xattr_journal_credits)
+{
+ int len;
+
+ len = snprintf(self->path, sizeof(self->path), "credit-test-%d",
+ getpid());
+ ASSERT_GE(len, 0);
+ ASSERT_LT(len, (int)sizeof(self->path));
+ len = snprintf(self->acl_child, sizeof(self->acl_child), "%s/child",
+ self->path);
+ ASSERT_GE(len, 0);
+ ASSERT_LT(len, (int)sizeof(self->acl_child));
+ len = snprintf(self->shared_a, sizeof(self->shared_a), "shared-a-%d",
+ getpid());
+ ASSERT_GE(len, 0);
+ ASSERT_LT(len, (int)sizeof(self->shared_a));
+ len = snprintf(self->shared_b, sizeof(self->shared_b), "shared-b-%d",
+ getpid());
+ ASSERT_GE(len, 0);
+ ASSERT_LT(len, (int)sizeof(self->shared_b));
+
+ self->value = malloc(LARGE_VALUE_SIZE);
+ ASSERT_NE(NULL, self->value);
+ self->readback = malloc(LARGE_VALUE_SIZE);
+ ASSERT_NE(NULL, self->readback);
+}
+
+FIXTURE_TEARDOWN(ext4_xattr_journal_credits)
+{
+ unlink(self->path);
+ unlink(self->shared_a);
+ unlink(self->shared_b);
+ rmdir(self->acl_child);
+ rmdir(self->path);
+ rmdir(self->shared_a);
+ rmdir(self->shared_b);
+ free(self->readback);
+ free(self->value);
+}
+
+static void set_acl_entry(struct posix_acl_xattr_entry *entry, uint16_t tag,
+ uint16_t perm, uint32_t id)
+{
+ entry->e_tag = htole16(tag);
+ entry->e_perm = htole16(perm);
+ entry->e_id = htole32(id);
+}
+
+static void *make_large_default_acl(size_t *size)
+{
+ struct posix_acl_xattr_header *header;
+ struct posix_acl_xattr_entry *entry;
+ size_t entry_count = NAMED_ACL_ENTRIES * 2 + 4;
+ unsigned int i;
+
+ *size = sizeof(*header) + entry_count * sizeof(*entry);
+ header = calloc(1, *size);
+ if (!header)
+ return NULL;
+ header->a_version = htole32(POSIX_ACL_XATTR_VERSION);
+ entry = (void *)(header + 1);
+ set_acl_entry(entry++, ACL_USER_OBJ, 7, UINT32_MAX);
+ for (i = 0; i < NAMED_ACL_ENTRIES; i++)
+ set_acl_entry(entry++, ACL_USER, 4, i + 1);
+ set_acl_entry(entry++, ACL_GROUP_OBJ, 5, UINT32_MAX);
+ for (i = 0; i < NAMED_ACL_ENTRIES; i++)
+ set_acl_entry(entry++, ACL_GROUP, 4, i + 1);
+ set_acl_entry(entry++, ACL_MASK, 5, UINT32_MAX);
+ set_acl_entry(entry, ACL_OTHER, 0, UINT32_MAX);
+ return header;
+}
+
+static void create_test_file(struct __test_metadata *_metadata,
+ const char *path)
+{
+ int fd = open(path, O_CREAT | O_EXCL | O_RDWR, 0600);
+
+ ASSERT_GE(fd, 0) {
+ TH_LOG("open(%s): %s", path, strerror(errno));
+ }
+ ASSERT_EQ(0, close(fd));
+}
+
+static void check_xattr_value(struct __test_metadata *_metadata,
+ const char *path,
+ const char *name, const void *expected,
+ size_t size, char *buffer)
+{
+ ssize_t len = getxattr(path, name, buffer, size);
+
+ ASSERT_EQ((ssize_t)size, len) {
+ TH_LOG("getxattr(%s): %s", name,
+ len < 0 ? strerror(errno) : "unexpected value size");
+ }
+ ASSERT_EQ(0, memcmp(buffer, expected, size));
+}
+
+TEST_F(ext4_xattr_journal_credits, large_xattr_lifecycle)
+{
+ char *expected = malloc(LARGE_VALUE_SIZE);
+ int ret;
+
+ ASSERT_NE(NULL, expected);
+ create_test_file(_metadata, self->path);
+ memset(self->value, 0x5a, LARGE_VALUE_SIZE);
+ self->value[0] = 1;
+
+ ASSERT_EQ(0, setxattr(self->path, large_name, self->value,
+ LARGE_VALUE_SIZE, 0));
+ check_xattr_value(_metadata, self->path, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+
+ memset(self->value, 0xa5, LARGE_VALUE_SIZE);
+ self->value[0] = 2;
+ ASSERT_EQ(0, setxattr(self->path, large_name, self->value,
+ LARGE_VALUE_SIZE, XATTR_REPLACE));
+ check_xattr_value(_metadata, self->path, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+
+ memcpy(expected, self->value, LARGE_VALUE_SIZE);
+ memset(self->value, 0x3c, LARGE_VALUE_SIZE);
+ self->value[0] = 3;
+ errno = 0;
+ ret = setxattr(self->path, large_name, self->value,
+ LARGE_VALUE_SIZE, XATTR_CREATE);
+ ASSERT_EQ(-1, ret);
+ ASSERT_EQ(EEXIST, errno);
+ check_xattr_value(_metadata, self->path, large_name, expected,
+ LARGE_VALUE_SIZE, self->readback);
+
+ ASSERT_EQ(0, removexattr(self->path, large_name));
+ free(expected);
+}
+
+TEST_F(ext4_xattr_journal_credits, shared_block_reuses_same_ea_inode)
+{
+ /* The test image has 128-byte inodes, so both xattrs use external blocks. */
+ create_test_file(_metadata, self->shared_a);
+ create_test_file(_metadata, self->shared_b);
+ memset(self->value, 0x7e, LARGE_VALUE_SIZE);
+ self->value[0] = 0x42;
+
+ ASSERT_EQ(0, setxattr(self->shared_a, large_name, self->value,
+ LARGE_VALUE_SIZE, XATTR_CREATE));
+ ASSERT_EQ(0, setxattr(self->shared_b, large_name, self->value,
+ LARGE_VALUE_SIZE, XATTR_CREATE));
+ check_xattr_value(_metadata, self->shared_a, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+ check_xattr_value(_metadata, self->shared_b, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+
+ /* Clone the shared block, then deduplicate back to the original block. */
+ ASSERT_EQ(0, setxattr(self->shared_a, large_name, self->value,
+ LARGE_VALUE_SIZE, XATTR_REPLACE));
+ check_xattr_value(_metadata, self->shared_a, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+ check_xattr_value(_metadata, self->shared_b, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+
+ ASSERT_EQ(0, removexattr(self->shared_a, large_name));
+ ASSERT_EQ(0, removexattr(self->shared_b, large_name));
+}
+
+TEST_F(ext4_xattr_journal_credits, small_xattr_round_trip)
+{
+ static const char value[] = "small-xattr";
+ char readback[sizeof(value)];
+
+ create_test_file(_metadata, self->path);
+ ASSERT_EQ(0, setxattr(self->path, small_name, value, sizeof(value), 0));
+ ASSERT_EQ((ssize_t)sizeof(value),
+ getxattr(self->path, small_name, readback, sizeof(readback)));
+ ASSERT_EQ(0, memcmp(value, readback, sizeof(value)));
+ ASSERT_EQ(0, removexattr(self->path, small_name));
+}
+
+TEST_F(ext4_xattr_journal_credits, large_default_acl_respects_journal_limit)
+{
+ static const char small_value[] = "filesystem-still-writable";
+ size_t acl_size;
+ char *acl = make_large_default_acl(&acl_size);
+ int ret;
+
+ ASSERT_NE(NULL, acl);
+ ASSERT_EQ(0, mkdir(self->path, 0700));
+ ret = setxattr(self->path, "system.posix_acl_default", acl,
+ acl_size, 0);
+ if (ret < 0 && errno == EOPNOTSUPP)
+ SKIP(return, "POSIX ACLs are not supported");
+ ASSERT_EQ(0, ret) {
+ TH_LOG("set default ACL: %s", strerror(errno));
+ }
+
+ errno = 0;
+ ret = mkdir(self->acl_child, 0700);
+ ASSERT_EQ(-1, ret);
+ ASSERT_EQ(ENOSPC, errno);
+
+ /* The rejected transaction must not abort the journal. */
+ ASSERT_EQ(0, setxattr(self->path, small_name, small_value,
+ sizeof(small_value), 0));
+ ASSERT_EQ(0, removexattr(self->path, small_name));
+ free(acl);
+}
+
+TEST_HARNESS_MAIN
diff --git a/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
new file mode 100755
index 000000000..977cdcf47
--- /dev/null
+++ b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+
+KSFT_SKIP=4
+
+skip()
+{
+ printf 'SKIP: %s\n' "$1"
+ exit "$KSFT_SKIP"
+}
+
+[ "$(id -u)" -eq 0 ] || skip "requires root to mount a disposable EXT4 image"
+
+for command in mkfs.ext4 mount umount truncate e2fsck losetup grep; do
+ command -v "$command" >/dev/null 2>&1 || skip "missing $command"
+done
+
+grep -qw ext4 /proc/filesystems || skip "EXT4 is not available in this kernel"
+losetup -f >/dev/null 2>&1 || skip "no loop device is available"
+
+test_dir=$(mktemp -d) || exit 1
+mounted=0
+
+cleanup()
+{
+ if [ "$mounted" -eq 1 ]; then
+ umount "$test_dir/mnt" || true
+ fi
+ rm -rf "$test_dir"
+}
+trap cleanup EXIT INT TERM
+
+truncate -s 32M "$test_dir/fs.img" || exit 1
+# 128-byte inodes have no ibody xattr space, forcing the external-block path.
+if ! mkfs.ext4 -q -F -b 1024 -I 128 -O ea_inode -J size=1 "$test_dir/fs.img" \
+ 2>"$test_dir/mkfs.err"; then
+ cat "$test_dir/mkfs.err" >&2
+ skip "mkfs.ext4 does not support the ea_inode test image"
+fi
+mkdir "$test_dir/mnt" || exit 1
+if ! mount -t ext4 -o loop "$test_dir/fs.img" "$test_dir/mnt" \
+ 2>"$test_dir/mount.err"; then
+ cat "$test_dir/mount.err" >&2
+ if grep -Eqi 'permission denied|operation not permitted|not permitted' \
+ "$test_dir/mount.err"; then
+ skip "mounting a loop image is not permitted"
+ fi
+ exit 1
+fi
+mounted=1
+
+test_bin="$(cd "$(dirname "$0")" && pwd)/ext4_xattr_journal_credits"
+(cd "$test_dir/mnt" && "$test_bin") || exit $?
+
+umount "$test_dir/mnt" || exit 1
+mounted=0
+e2fsck -fn "$test_dir/fs.img"
--
2.56.1
^ permalink raw reply related [flat|nested] 22+ messages in thread* [PATCH v11] ext4: bound xattr journal credits to transaction limit
2026-11-25 7:42 ` [PATCH v10] " arzuagapozosergio
@ 2026-11-25 7:46 ` arzuagapozosergio
2026-11-25 7:59 ` sashiko-bot
0 siblings, 1 reply; 22+ messages in thread
From: arzuagapozosergio @ 2026-11-25 7:46 UTC (permalink / raw)
To: tytso; +Cc: jack, linux-ext4, syzbot+25d45c0bd716f2b794a7, arzuagapozosergio
From: Ryuji Hiroshi <arzuagapozosergio@gmail.com>
When setting an xattr without XATTR_CREATE, ext4 reserves credits for
releasing an old ea_inode even when the attribute does not exist. On a
1 KiB-block filesystem with a minimum journal, this can request more
credits than JBD2 permits, triggering a warning before the operation
fails.
Drop the obsolete credits for releasing an old ea_inode. Since
ext4_put_ea_inode() defers the final iput to a worker, freeing the old
value's blocks does not happen in the current transaction. Continue to
count ea_inode references in the external xattr block for the case where
it must be cloned, and check the estimate against the transaction limit
after reserving the default JBD2 revoke credits. Repeat the estimate under
the xattr write lock before modifying the attribute.
When an identical xattr block is reused from the cache, drop the
provisional ea_inode reference unless the block was modified in place.
Handle both a different cached block and the original block being found
after cloning. Bound the aggregate credits for inherited xattrs during
inode creation against the JBD2 transaction limit before starting a
handle. Add EXT4 selftests for these paths.
Fixes: dec214d00e0d ("ext4: xattr inode deduplication")
Fixes: af65207c76ce ("ext4: fix __ext4_new_inode() journal credits calculation")
Fixes: 0a46ef234756 ("ext4: do not create EA inode under buffer lock")
Reported-by: syzbot+25d45c0bd716f2b794a7@syzkaller.appspotmail.com
Link: https://syzbot.org/bug?extid=25d45c0bd716f2b794a7
Assisted-by: Codex:GPT-6
Signed-off-by: Ryuji Hiroshi <arzuagapozosergio@gmail.com>
---
Changes in v11:
- Keep one Signed-off-by trailer in the commit message.
- Restore the Assisted-by tag.
Changes in v10:
- Add KHDR_INCLUDES to the ext4 selftests CFLAGS.
- Add Fixes tags for the inode-credit and ea_inode reference issues.
- Move version notes and test results below the patch separator.
Changes in v9:
- Skip the large default ACL test when the filesystem reports
EOPNOTSUPP, as happens when POSIX ACL support is disabled.
Tests:
- make -C tools/testing/selftests/filesystems/ext4 clean all
- sh -n tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
- Booted the v8 kernel in QEMU/KVM with a 1 KiB-block, 128-byte-inode
ea_inode filesystem and 1 MiB journal; all 4 harness tests passed.
- e2fsck -fn passed after the guest unmounted the filesystem.
---
MAINTAINERS | 1 +
fs/ext4/acl.c | 3 +-
fs/ext4/crypto.c | 3 +-
fs/ext4/ialloc.c | 18 +-
fs/ext4/xattr.c | 116 ++++-----
fs/ext4/xattr.h | 8 +-
fs/jbd2/transaction.c | 9 +
include/linux/jbd2.h | 2 +
tools/testing/selftests/Makefile | 1 +
.../selftests/filesystems/ext4/.gitignore | 2 +
.../selftests/filesystems/ext4/Makefile | 8 +
.../ext4/ext4_xattr_journal_credits.c | 243 ++++++++++++++++++
.../ext4/ext4_xattr_journal_credits.sh | 57 ++++
13 files changed, 399 insertions(+), 72 deletions(-)
create mode 100644 tools/testing/selftests/filesystems/ext4/.gitignore
create mode 100644 tools/testing/selftests/filesystems/ext4/Makefile
create mode 100644 tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
create mode 100755 tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
diff --git a/MAINTAINERS b/MAINTAINERS
index 2ac5128b7..1debcbeb0 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -10131,6 +10131,7 @@ F: Documentation/filesystems/ext4/
F: fs/ext4/
F: include/trace/events/ext4.h
F: include/uapi/linux/ext4.h
+F: tools/testing/selftests/filesystems/ext4/
Extended Verification Module (EVM)
M: Mimi Zohar <zohar@linux.ibm.com>
diff --git a/fs/ext4/acl.c b/fs/ext4/acl.c
index 59fac55a2..167a410ef 100644
--- a/fs/ext4/acl.c
+++ b/fs/ext4/acl.c
@@ -239,8 +239,7 @@ ext4_set_acl(const struct mnt_idmap *idmap, struct dentry *dentry,
if (error)
return error;
retry:
- error = ext4_xattr_set_credits(inode, acl_size, false /* is_create */,
- &credits);
+ error = ext4_xattr_set_credits(inode, acl_size, &credits);
if (error)
return error;
diff --git a/fs/ext4/crypto.c b/fs/ext4/crypto.c
index 318b2eaea..c1275009e 100644
--- a/fs/ext4/crypto.c
+++ b/fs/ext4/crypto.c
@@ -203,8 +203,7 @@ static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
if (res)
return res;
retry:
- res = ext4_xattr_set_credits(inode, len, false /* is_create */,
- &credits);
+ res = ext4_xattr_set_credits(inode, len, &credits);
if (res)
return res;
diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
index 527f1f703..49e556a66 100644
--- a/fs/ext4/ialloc.c
+++ b/fs/ext4/ialloc.c
@@ -888,8 +888,7 @@ static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
nblocks += (S_ISDIR(mode) ? 2 : 1) *
__ext4_xattr_set_credits(sb, NULL /* inode */,
- NULL /* block_bh */, acl_size,
- true /* is_create */);
+ NULL /* block_bh */, acl_size);
posix_acl_release(p);
}
#endif
@@ -907,16 +906,14 @@ static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
*/
nblocks += num_security_xattrs *
__ext4_xattr_set_credits(sb, NULL /* inode */,
- NULL /* block_bh */, 1024,
- true /* is_create */);
+ NULL /* block_bh */, 1024);
}
#endif
if (encrypt)
nblocks += __ext4_xattr_set_credits(sb,
NULL /* inode */,
NULL /* block_bh */,
- FSCRYPT_SET_CONTEXT_MAX_SIZE,
- true /* is_create */);
+ FSCRYPT_SET_CONTEXT_MAX_SIZE);
return nblocks;
}
@@ -1006,11 +1003,20 @@ struct inode *__ext4_new_inode(const struct mnt_idmap *idmap,
goto out;
if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) {
+ int max_credits, revoke_credits;
+ journal_t *journal = sbi->s_journal;
+
ret2 = ext4_xattr_credits_for_new_inode(dir, mode, encrypt);
if (ret2 < 0) {
err = ret2;
goto out;
}
+ revoke_credits = ext4_trans_default_revoke_credits(sb);
+ max_credits = jbd2_journal_get_max_transaction_buffers(journal, revoke_credits);
+ if (nblocks > max_credits || ret2 > max_credits - nblocks) {
+ err = -ENOSPC;
+ goto out;
+ }
nblocks += ret2;
}
diff --git a/fs/ext4/xattr.c b/fs/ext4/xattr.c
index 5c310747b..753265c08 100644
--- a/fs/ext4/xattr.c
+++ b/fs/ext4/xattr.c
@@ -948,8 +948,7 @@ static void ext4_xattr_inode_free_quota(struct inode *parent,
}
int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
- struct buffer_head *block_bh, size_t value_len,
- bool is_create)
+ struct buffer_head *block_bh, size_t value_len)
{
int credits;
int blocks;
@@ -998,24 +997,6 @@ int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
/* Blocks themselves. */
credits += blocks;
- if (!is_create) {
- /* Dereference ea_inode holding old xattr value.
- * Old ea_inode, inode map, block bitmap, group descriptor.
- */
- credits += 4;
-
- /* Data blocks for old ea_inode. */
- blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits;
-
- /* Indirection block or one level of extent tree for old
- * ea_inode.
- */
- blocks += 1;
-
- /* Block bitmap and group descriptor updates for each block. */
- credits += blocks * 2;
- }
-
/* We may need to clone the existing xattr block in which case we need
* to increment ref counts for existing ea_inodes referenced by it.
*/
@@ -2110,6 +2091,16 @@ ext4_xattr_block_set(handle_t *handle, struct inode *inode,
if (error)
goto cleanup_dquot;
}
+ if (ea_inode &&
+ !(bs->bh && s->base == bs->bh->b_data)) {
+ /* The reused block already holds this reference. */
+ error = ext4_xattr_inode_dec_ref(handle, ea_inode);
+ if (error)
+ ext4_warning_inode(ea_inode,
+ "dec ref error=%d", error);
+ ext4_put_ea_inode(ea_inode);
+ ea_inode = NULL;
+ }
mb_cache_entry_touch(ea_block_cache, ce);
mb_cache_entry_put(ea_block_cache, ce);
ce = NULL;
@@ -2375,29 +2366,6 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
ext4_write_lock_xattr(inode, &no_expand);
- /* Check journal credits under write lock. */
- if (ext4_handle_valid(handle)) {
- struct buffer_head *bh;
- int credits;
-
- bh = ext4_xattr_get_block(inode);
- if (IS_ERR(bh)) {
- error = PTR_ERR(bh);
- goto cleanup;
- }
-
- credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
- value_len,
- flags & XATTR_CREATE);
- brelse(bh);
-
- if (jbd2_handle_buffer_credits(handle) < credits) {
- error = -ENOSPC;
- goto cleanup;
- }
- WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS));
- }
-
error = ext4_reserve_inode_write(handle, inode, &is.iloc);
if (error)
goto cleanup;
@@ -2415,6 +2383,28 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
error = ext4_xattr_block_find(inode, &i, &bs);
if (error)
goto cleanup;
+
+ /* Check journal credits under write lock. */
+ if (ext4_handle_valid(handle)) {
+ int credits;
+
+ if (!bs.bh) {
+ struct buffer_head *bh = ext4_xattr_get_block(inode);
+
+ if (IS_ERR(bh)) {
+ error = PTR_ERR(bh);
+ goto cleanup;
+ }
+ bs.bh = bh;
+ }
+ credits = __ext4_xattr_set_credits(inode->i_sb, inode, bs.bh,
+ value_len);
+ if (jbd2_handle_buffer_credits(handle) < credits) {
+ error = -ENOSPC;
+ goto cleanup;
+ }
+ WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS));
+ }
if (is.s.not_found && bs.s.not_found) {
error = -ENODATA;
if (flags & XATTR_REPLACE)
@@ -2501,27 +2491,38 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
}
int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
- bool is_create, int *credits)
+ int *credits)
{
- struct buffer_head *bh;
- int err;
+ struct ext4_xattr_block_find bs = {
+ .s = { .not_found = -ENODATA, },
+ };
+ journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
+ int max_credits, revoke_credits, err;
*credits = 0;
-
- if (!EXT4_SB(inode->i_sb)->s_journal)
+ if (!journal)
return 0;
+ /* xattr_sem protects the block while we count its ea_inode references. */
down_read(&EXT4_I(inode)->xattr_sem);
- bh = ext4_xattr_get_block(inode);
- if (IS_ERR(bh)) {
- err = PTR_ERR(bh);
- } else {
- *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
- value_len, is_create);
- brelse(bh);
- err = 0;
+ if (ext4_has_feature_ea_inode(inode->i_sb)) {
+ struct buffer_head *bh;
+
+ bh = ext4_xattr_get_block(inode);
+ if (IS_ERR(bh)) {
+ err = PTR_ERR(bh);
+ goto out_unlock;
+ }
+ bs.bh = bh;
}
+ *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bs.bh,
+ value_len);
+ revoke_credits = ext4_trans_default_revoke_credits(inode->i_sb);
+ max_credits = jbd2_journal_get_max_transaction_buffers(journal, revoke_credits);
+ err = *credits > max_credits ? -ENOSPC : 0;
+ brelse(bs.bh);
+out_unlock:
up_read(&EXT4_I(inode)->xattr_sem);
return err;
@@ -2549,8 +2550,7 @@ ext4_xattr_set(struct inode *inode, int name_index, const char *name,
return error;
retry:
- error = ext4_xattr_set_credits(inode, value_len, flags & XATTR_CREATE,
- &credits);
+ error = ext4_xattr_set_credits(inode, value_len, &credits);
if (error)
return error;
diff --git a/fs/ext4/xattr.h b/fs/ext4/xattr.h
index 821dc6a50..eb6e85036 100644
--- a/fs/ext4/xattr.h
+++ b/fs/ext4/xattr.h
@@ -175,11 +175,11 @@ extern ssize_t ext4_listxattr(struct dentry *, char *, size_t);
extern int ext4_xattr_get(struct inode *, int, const char *, void *, size_t);
extern int ext4_xattr_set(struct inode *, int, const char *, const void *, size_t, int);
extern int ext4_xattr_set_handle(handle_t *, struct inode *, int, const char *, const void *, size_t, int);
-extern int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
- bool is_create, int *credits);
+int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
+ int *credits);
extern int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
- struct buffer_head *block_bh, size_t value_len,
- bool is_create);
+ struct buffer_head *block_bh,
+ size_t value_len);
extern int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
int extra_credits);
diff --git a/fs/jbd2/transaction.c b/fs/jbd2/transaction.c
index 85d84d909..5fe49f118 100644
--- a/fs/jbd2/transaction.c
+++ b/fs/jbd2/transaction.c
@@ -194,6 +194,15 @@ static int jbd2_max_user_trans_buffers(journal_t *journal)
journal->j_transaction_overhead_buffers;
}
+/* Maximum buffer credits available after reserving @revoke_records. */
+int jbd2_journal_get_max_transaction_buffers(journal_t *journal,
+ int revoke_records)
+{
+ return jbd2_max_user_trans_buffers(journal) -
+ DIV_ROUND_UP(revoke_records, journal->j_revoke_records_per_block);
+}
+EXPORT_SYMBOL(jbd2_journal_get_max_transaction_buffers);
+
/*
* Wait until we can add credits for handle to the running transaction. Called
* with j_state_lock held for reading. Returns 0 if handle joined the running
diff --git a/include/linux/jbd2.h b/include/linux/jbd2.h
index e3886963b..a72f14440 100644
--- a/include/linux/jbd2.h
+++ b/include/linux/jbd2.h
@@ -1523,6 +1523,8 @@ static inline handle_t *journal_current_handle(void)
*/
extern handle_t *jbd2_journal_start(journal_t *, int nblocks);
+int jbd2_journal_get_max_transaction_buffers(journal_t *journal,
+ int revoke_records);
extern handle_t *jbd2__journal_start(journal_t *, int blocks, int rsv_blocks,
int revoke_records, gfp_t gfp_mask,
unsigned int type, unsigned int line_no);
diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
index 07c3719f8..c16b1132e 100644
--- a/tools/testing/selftests/Makefile
+++ b/tools/testing/selftests/Makefile
@@ -37,6 +37,7 @@ TARGETS += filesystems/binderfs
TARGETS += filesystems/configfs
TARGETS += filesystems/epoll
TARGETS += filesystems/eventfd
+TARGETS += filesystems/ext4
TARGETS += filesystems/failfs
TARGETS += filesystems/fat
TARGETS += filesystems/file_stressor
diff --git a/tools/testing/selftests/filesystems/ext4/.gitignore b/tools/testing/selftests/filesystems/ext4/.gitignore
new file mode 100644
index 000000000..d3678b52d
--- /dev/null
+++ b/tools/testing/selftests/filesystems/ext4/.gitignore
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0
+/ext4_xattr_journal_credits
diff --git a/tools/testing/selftests/filesystems/ext4/Makefile b/tools/testing/selftests/filesystems/ext4/Makefile
new file mode 100644
index 000000000..fbfe2e7a5
--- /dev/null
+++ b/tools/testing/selftests/filesystems/ext4/Makefile
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0
+
+CFLAGS += $(KHDR_INCLUDES)
+
+TEST_GEN_PROGS_EXTENDED := ext4_xattr_journal_credits
+TEST_PROGS := ext4_xattr_journal_credits.sh
+
+include ../../lib.mk
diff --git a/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
new file mode 100644
index 000000000..8cb3e0172
--- /dev/null
+++ b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.c
@@ -0,0 +1,243 @@
+// SPDX-License-Identifier: GPL-2.0
+#define _GNU_SOURCE
+#include <errno.h>
+#include <endian.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/xattr.h>
+#include <unistd.h>
+
+#include <linux/posix_acl.h>
+#include <linux/posix_acl_xattr.h>
+
+#include "../../kselftest_harness.h"
+
+/* Near XATTR_SIZE_MAX to exercise the largest ea_inode credit estimate. */
+#define LARGE_VALUE_SIZE 65296
+#define NAMED_ACL_ENTRIES 2500
+
+static const char large_name[] = "user.ext4-credit-large";
+static const char small_name[] = "user.ext4-credit-small";
+
+FIXTURE(ext4_xattr_journal_credits)
+{
+ char path[PATH_MAX];
+ char acl_child[PATH_MAX];
+ char shared_a[PATH_MAX];
+ char shared_b[PATH_MAX];
+ char *value;
+ char *readback;
+};
+
+FIXTURE_SETUP(ext4_xattr_journal_credits)
+{
+ int len;
+
+ len = snprintf(self->path, sizeof(self->path), "credit-test-%d",
+ getpid());
+ ASSERT_GE(len, 0);
+ ASSERT_LT(len, (int)sizeof(self->path));
+ len = snprintf(self->acl_child, sizeof(self->acl_child), "%s/child",
+ self->path);
+ ASSERT_GE(len, 0);
+ ASSERT_LT(len, (int)sizeof(self->acl_child));
+ len = snprintf(self->shared_a, sizeof(self->shared_a), "shared-a-%d",
+ getpid());
+ ASSERT_GE(len, 0);
+ ASSERT_LT(len, (int)sizeof(self->shared_a));
+ len = snprintf(self->shared_b, sizeof(self->shared_b), "shared-b-%d",
+ getpid());
+ ASSERT_GE(len, 0);
+ ASSERT_LT(len, (int)sizeof(self->shared_b));
+
+ self->value = malloc(LARGE_VALUE_SIZE);
+ ASSERT_NE(NULL, self->value);
+ self->readback = malloc(LARGE_VALUE_SIZE);
+ ASSERT_NE(NULL, self->readback);
+}
+
+FIXTURE_TEARDOWN(ext4_xattr_journal_credits)
+{
+ unlink(self->path);
+ unlink(self->shared_a);
+ unlink(self->shared_b);
+ rmdir(self->acl_child);
+ rmdir(self->path);
+ rmdir(self->shared_a);
+ rmdir(self->shared_b);
+ free(self->readback);
+ free(self->value);
+}
+
+static void set_acl_entry(struct posix_acl_xattr_entry *entry, uint16_t tag,
+ uint16_t perm, uint32_t id)
+{
+ entry->e_tag = htole16(tag);
+ entry->e_perm = htole16(perm);
+ entry->e_id = htole32(id);
+}
+
+static void *make_large_default_acl(size_t *size)
+{
+ struct posix_acl_xattr_header *header;
+ struct posix_acl_xattr_entry *entry;
+ size_t entry_count = NAMED_ACL_ENTRIES * 2 + 4;
+ unsigned int i;
+
+ *size = sizeof(*header) + entry_count * sizeof(*entry);
+ header = calloc(1, *size);
+ if (!header)
+ return NULL;
+ header->a_version = htole32(POSIX_ACL_XATTR_VERSION);
+ entry = (void *)(header + 1);
+ set_acl_entry(entry++, ACL_USER_OBJ, 7, UINT32_MAX);
+ for (i = 0; i < NAMED_ACL_ENTRIES; i++)
+ set_acl_entry(entry++, ACL_USER, 4, i + 1);
+ set_acl_entry(entry++, ACL_GROUP_OBJ, 5, UINT32_MAX);
+ for (i = 0; i < NAMED_ACL_ENTRIES; i++)
+ set_acl_entry(entry++, ACL_GROUP, 4, i + 1);
+ set_acl_entry(entry++, ACL_MASK, 5, UINT32_MAX);
+ set_acl_entry(entry, ACL_OTHER, 0, UINT32_MAX);
+ return header;
+}
+
+static void create_test_file(struct __test_metadata *_metadata,
+ const char *path)
+{
+ int fd = open(path, O_CREAT | O_EXCL | O_RDWR, 0600);
+
+ ASSERT_GE(fd, 0) {
+ TH_LOG("open(%s): %s", path, strerror(errno));
+ }
+ ASSERT_EQ(0, close(fd));
+}
+
+static void check_xattr_value(struct __test_metadata *_metadata,
+ const char *path,
+ const char *name, const void *expected,
+ size_t size, char *buffer)
+{
+ ssize_t len = getxattr(path, name, buffer, size);
+
+ ASSERT_EQ((ssize_t)size, len) {
+ TH_LOG("getxattr(%s): %s", name,
+ len < 0 ? strerror(errno) : "unexpected value size");
+ }
+ ASSERT_EQ(0, memcmp(buffer, expected, size));
+}
+
+TEST_F(ext4_xattr_journal_credits, large_xattr_lifecycle)
+{
+ char *expected = malloc(LARGE_VALUE_SIZE);
+ int ret;
+
+ ASSERT_NE(NULL, expected);
+ create_test_file(_metadata, self->path);
+ memset(self->value, 0x5a, LARGE_VALUE_SIZE);
+ self->value[0] = 1;
+
+ ASSERT_EQ(0, setxattr(self->path, large_name, self->value,
+ LARGE_VALUE_SIZE, 0));
+ check_xattr_value(_metadata, self->path, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+
+ memset(self->value, 0xa5, LARGE_VALUE_SIZE);
+ self->value[0] = 2;
+ ASSERT_EQ(0, setxattr(self->path, large_name, self->value,
+ LARGE_VALUE_SIZE, XATTR_REPLACE));
+ check_xattr_value(_metadata, self->path, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+
+ memcpy(expected, self->value, LARGE_VALUE_SIZE);
+ memset(self->value, 0x3c, LARGE_VALUE_SIZE);
+ self->value[0] = 3;
+ errno = 0;
+ ret = setxattr(self->path, large_name, self->value,
+ LARGE_VALUE_SIZE, XATTR_CREATE);
+ ASSERT_EQ(-1, ret);
+ ASSERT_EQ(EEXIST, errno);
+ check_xattr_value(_metadata, self->path, large_name, expected,
+ LARGE_VALUE_SIZE, self->readback);
+
+ ASSERT_EQ(0, removexattr(self->path, large_name));
+ free(expected);
+}
+
+TEST_F(ext4_xattr_journal_credits, shared_block_reuses_same_ea_inode)
+{
+ /* The test image has 128-byte inodes, so both xattrs use external blocks. */
+ create_test_file(_metadata, self->shared_a);
+ create_test_file(_metadata, self->shared_b);
+ memset(self->value, 0x7e, LARGE_VALUE_SIZE);
+ self->value[0] = 0x42;
+
+ ASSERT_EQ(0, setxattr(self->shared_a, large_name, self->value,
+ LARGE_VALUE_SIZE, XATTR_CREATE));
+ ASSERT_EQ(0, setxattr(self->shared_b, large_name, self->value,
+ LARGE_VALUE_SIZE, XATTR_CREATE));
+ check_xattr_value(_metadata, self->shared_a, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+ check_xattr_value(_metadata, self->shared_b, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+
+ /* Clone the shared block, then deduplicate back to the original block. */
+ ASSERT_EQ(0, setxattr(self->shared_a, large_name, self->value,
+ LARGE_VALUE_SIZE, XATTR_REPLACE));
+ check_xattr_value(_metadata, self->shared_a, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+ check_xattr_value(_metadata, self->shared_b, large_name, self->value,
+ LARGE_VALUE_SIZE, self->readback);
+
+ ASSERT_EQ(0, removexattr(self->shared_a, large_name));
+ ASSERT_EQ(0, removexattr(self->shared_b, large_name));
+}
+
+TEST_F(ext4_xattr_journal_credits, small_xattr_round_trip)
+{
+ static const char value[] = "small-xattr";
+ char readback[sizeof(value)];
+
+ create_test_file(_metadata, self->path);
+ ASSERT_EQ(0, setxattr(self->path, small_name, value, sizeof(value), 0));
+ ASSERT_EQ((ssize_t)sizeof(value),
+ getxattr(self->path, small_name, readback, sizeof(readback)));
+ ASSERT_EQ(0, memcmp(value, readback, sizeof(value)));
+ ASSERT_EQ(0, removexattr(self->path, small_name));
+}
+
+TEST_F(ext4_xattr_journal_credits, large_default_acl_respects_journal_limit)
+{
+ static const char small_value[] = "filesystem-still-writable";
+ size_t acl_size;
+ char *acl = make_large_default_acl(&acl_size);
+ int ret;
+
+ ASSERT_NE(NULL, acl);
+ ASSERT_EQ(0, mkdir(self->path, 0700));
+ ret = setxattr(self->path, "system.posix_acl_default", acl,
+ acl_size, 0);
+ if (ret < 0 && errno == EOPNOTSUPP)
+ SKIP(return, "POSIX ACLs are not supported");
+ ASSERT_EQ(0, ret) {
+ TH_LOG("set default ACL: %s", strerror(errno));
+ }
+
+ errno = 0;
+ ret = mkdir(self->acl_child, 0700);
+ ASSERT_EQ(-1, ret);
+ ASSERT_EQ(ENOSPC, errno);
+
+ /* The rejected transaction must not abort the journal. */
+ ASSERT_EQ(0, setxattr(self->path, small_name, small_value,
+ sizeof(small_value), 0));
+ ASSERT_EQ(0, removexattr(self->path, small_name));
+ free(acl);
+}
+
+TEST_HARNESS_MAIN
diff --git a/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
new file mode 100755
index 000000000..977cdcf47
--- /dev/null
+++ b/tools/testing/selftests/filesystems/ext4/ext4_xattr_journal_credits.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+
+KSFT_SKIP=4
+
+skip()
+{
+ printf 'SKIP: %s\n' "$1"
+ exit "$KSFT_SKIP"
+}
+
+[ "$(id -u)" -eq 0 ] || skip "requires root to mount a disposable EXT4 image"
+
+for command in mkfs.ext4 mount umount truncate e2fsck losetup grep; do
+ command -v "$command" >/dev/null 2>&1 || skip "missing $command"
+done
+
+grep -qw ext4 /proc/filesystems || skip "EXT4 is not available in this kernel"
+losetup -f >/dev/null 2>&1 || skip "no loop device is available"
+
+test_dir=$(mktemp -d) || exit 1
+mounted=0
+
+cleanup()
+{
+ if [ "$mounted" -eq 1 ]; then
+ umount "$test_dir/mnt" || true
+ fi
+ rm -rf "$test_dir"
+}
+trap cleanup EXIT INT TERM
+
+truncate -s 32M "$test_dir/fs.img" || exit 1
+# 128-byte inodes have no ibody xattr space, forcing the external-block path.
+if ! mkfs.ext4 -q -F -b 1024 -I 128 -O ea_inode -J size=1 "$test_dir/fs.img" \
+ 2>"$test_dir/mkfs.err"; then
+ cat "$test_dir/mkfs.err" >&2
+ skip "mkfs.ext4 does not support the ea_inode test image"
+fi
+mkdir "$test_dir/mnt" || exit 1
+if ! mount -t ext4 -o loop "$test_dir/fs.img" "$test_dir/mnt" \
+ 2>"$test_dir/mount.err"; then
+ cat "$test_dir/mount.err" >&2
+ if grep -Eqi 'permission denied|operation not permitted|not permitted' \
+ "$test_dir/mount.err"; then
+ skip "mounting a loop image is not permitted"
+ fi
+ exit 1
+fi
+mounted=1
+
+test_bin="$(cd "$(dirname "$0")" && pwd)/ext4_xattr_journal_credits"
+(cd "$test_dir/mnt" && "$test_bin") || exit $?
+
+umount "$test_dir/mnt" || exit 1
+mounted=0
+e2fsck -fn "$test_dir/fs.img"
--
2.56.1
^ permalink raw reply related [flat|nested] 22+ messages in thread* Re: [PATCH v11] ext4: bound xattr journal credits to transaction limit
2026-11-25 7:46 ` [PATCH v11] " arzuagapozosergio
@ 2026-11-25 7:59 ` sashiko-bot
0 siblings, 0 replies; 22+ messages in thread
From: sashiko-bot @ 2026-11-25 7:59 UTC (permalink / raw)
To: arzuagapozosergio; +Cc: linux-ext4, tytso
> From: Ryuji Hiroshi <arzuagapozosergio@gmail.com>
>
> When setting an xattr without XATTR_CREATE, ext4 reserves credits for
> releasing an old ea_inode even when the attribute does not exist. On a
> 1 KiB-block filesystem with a minimum journal, this can request more
> credits than JBD2 permits, triggering a warning before the operation
> fails.
>
> Drop the obsolete credits for releasing an old ea_inode. Since
> ext4_put_ea_inode() defers the final iput to a worker, freeing the old
> value's blocks does not happen in the current transaction. Continue to
> count ea_inode references in the external xattr block for the case where
> it must be cloned, and check the estimate against the transaction limit
> after reserving the default JBD2 revoke credits. Repeat the estimate under
> the xattr write lock before modifying the attribute.
>
> When an identical xattr block is reused from the cache, drop the
> provisional ea_inode reference unless the block was modified in place.
> Handle both a different cached block and the original block being found
> after cloning. Bound the aggregate credits for inherited xattrs during
> inode creation against the JBD2 transaction limit before starting a
> handle. Add EXT4 selftests for these paths.
>
> Fixes: dec214d00e0d ("ext4: xattr inode deduplication")
> Fixes: af65207c76ce ("ext4: fix __ext4_new_inode() journal credits calculation")
> Fixes: 0a46ef234756 ("ext4: do not create EA inode under buffer lock")
> Reported-by: syzbot+25d45c0bd716f2b794a7@syzkaller.appspotmail.com
> Link: https://syzbot.org/bug?extid=25d45c0bd716f2b794a7
> Assisted-by: Codex:GPT-6
> Signed-off-by: Ryuji Hiroshi <arzuagapozosergio@gmail.com>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261125074616.35241-1-arzuagapozosergio@gmail.com?part=1
^ permalink raw reply [flat|nested] 22+ messages in thread