* [PATCH 0/4] ext4: don't fail FS_IOC_{SETFLAGS,FSSETXATTR} after applying them aka things that fell out of my uapi16 library work
@ 2026-11-30 13:22 Christian Brauner
2026-11-30 13:22 ` [PATCH 1/4] ext4: don't lose the result of ext4_ext_migrate() Christian Brauner
` (4 more replies)
0 siblings, 5 replies; 6+ messages in thread
From: Christian Brauner @ 2026-11-30 13:22 UTC (permalink / raw)
To: Theodore Ts'o, Jan Kara, linux-ext4
Cc: linux-fsdevel, Christian Brauner (Amutable)
I'm working on a uapi16 shared library and tool that generates
filesystem manifests according to https://uapi-group.org/specifications/specs/file_manifest
It's a Rust crate + C shared library + Rust binary. It has extensive
testing and as part of that some ext4 misbehavior was detected. So
here's my attempt at fixing it:
ext4 fails FS_IOC_SETFLAGS and FS_IOC_FSSETXATTR after it has already
written part of the request. The flags and the ctime are written first
and only then is the block mapping converted, the journaling mode
switched or the project ID changed. If one of those is refused the call
fails but the flags stick. We hit this with a test that passed
FS_NODUMP_FL | FS_NOATIME_FL to FS_IOC_SETFLAGS without reading the
flags first. It got EOPNOTSUPP back with both flags set.
xfs and btrfs check everything before they write anything and then do
the whole change in a single transaction. This gets ext4 there for
everything that can share a handle. Converting to block mapping and
changing the project ID now happen in the same handle as the flags.
The first patch fixes ext4_ext_migrate(). Since v5.5 it returns the
result of its last credit check instead of its own, so a conversion to
extents that fails with ENOSPC is reported as success and a restarted
handle comes back as 1. Once the conversion runs before the flags that
1 would silently drop the rest of the call.
The one real question is what to do about a cleared extent flag that
ext4 can't honor. Callers that don't start from FS_IOC_GETFLAGS leave
the flag out. Today that silently converts small files to block mapping
and fails for everything else. I'm treating it like the other visible
but unsettable flags. If anything else was asked for the rest is applied
and the call succeeds. If clearing the extent flag is all that was asked
for it still fails and nothing changes. btrfs silently leaves NOCOW
alone on a non-empty file too.
Failing and changing nothing would be stricter but it would break
restore tools that only work today because the flags stick. BorgBackup
before 1.4.2 builds its flags from scratch and ignores EOPNOTSUPP for
example. Borg switched to FS_IOC_GETFLAGS in 1.4.2 after running into
exactly this (cf. [1]) but the older releases are still around. The
downside is that repeating such a call fails the second time because
then only the extent flag differs. That's the same request as chattr -e
though so I don't see a way to tell them apart.
A refused attempt to set the extent flag, i.e., no extents feature or
inline data, still fails but doesn't apply the rest anymore. I didn't
give it the same treatment as the cleared flag. If you'd rather have
that it's a small change.
Setting the extent flag and switching the journaling mode still use
their own handles. ext4_ext_migrate() builds the new tree in a temporary
inode across several handles and the mode switch flushes the journal.
Their refusals happen before anything is written now but an I/O error
between them and the flags handle can still leave part of the change
behind.
Tested in VMs on 4k and 1k blocks with bigalloc, ext3, inline data,
buffered_iomap, dioread_lock, DAX and project quota, and with two new
fstests cases that I'll send separately.
[1]: https://github.com/borgbackup/borg/issues/9039
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
Christian Brauner (4):
ext4: don't lose the result of ext4_ext_migrate()
ext4: don't fail FS_IOC_SETFLAGS after applying it
ext4: split the flag checks out of ext4_ioctl_setflags()
ext4: don't fail FS_IOC_FSSETXATTR after applying it
fs/ext4/ext4.h | 3 +-
fs/ext4/ioctl.c | 333 ++++++++++++++++++++++++++++++------------------------
fs/ext4/migrate.c | 69 ++++++-----
3 files changed, 218 insertions(+), 187 deletions(-)
---
base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
change-id: 20261130-work-ext4-setflags_migrate-41e19f46f4e1
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 1/4] ext4: don't lose the result of ext4_ext_migrate()
2026-11-30 13:22 [PATCH 0/4] ext4: don't fail FS_IOC_{SETFLAGS,FSSETXATTR} after applying them aka things that fell out of my uapi16 library work Christian Brauner
@ 2026-11-30 13:22 ` Christian Brauner
2026-11-30 13:22 ` [PATCH 2/4] ext4: don't fail FS_IOC_SETFLAGS after applying it Christian Brauner
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Christian Brauner @ 2026-11-30 13:22 UTC (permalink / raw)
To: Theodore Ts'o, Jan Kara, linux-ext4
Cc: linux-fsdevel, Christian Brauner (Amutable)
Since commit a413036791d0 ("ext4: Provide function to handle transaction
restarts") ext4_ext_migrate() overwrites its result with the return
value of the final ext4_journal_ensure_credits(). So if building the
extent tree fails, e.g., with ENOSPC, or ext4_ext_swap_inode_data()
returns EAGAIN because a block got allocated in the meantime, we report
success and the inode stays block mapped. And if the handle had to be
restarted we return 1 to FS_IOC_SETFLAGS and EXT4_IOC_MIGRATE.
Keep the result of the migration and only return the error of the
credit check if nothing else failed.
Fixes: a413036791d0 ("ext4: Provide function to handle transaction restarts")
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
fs/ext4/migrate.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/fs/ext4/migrate.c b/fs/ext4/migrate.c
index 09931d3ba2c6a..be48456fa76f3 100644
--- a/fs/ext4/migrate.c
+++ b/fs/ext4/migrate.c
@@ -410,7 +410,7 @@ static int free_ext_block(handle_t *handle, struct inode *inode)
int ext4_ext_migrate(struct inode *inode)
{
handle_t *handle;
- int retval = 0, i;
+ int retval = 0, err, i;
__le32 *i_data;
struct ext4_inode_info *ei;
struct inode *tmp_inode = NULL;
@@ -567,9 +567,12 @@ int ext4_ext_migrate(struct inode *inode)
}
/* We mark the tmp_inode dirty via ext4_ext_tree_init. */
- retval = ext4_journal_ensure_credits(handle, 1, 0);
- if (retval < 0)
+ err = ext4_journal_ensure_credits(handle, 1, 0);
+ if (err < 0) {
+ if (!retval)
+ retval = err;
goto out_stop;
+ }
/*
* Mark the tmp_inode as of size zero
*/
--
2.53.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH 2/4] ext4: don't fail FS_IOC_SETFLAGS after applying it
2026-11-30 13:22 [PATCH 0/4] ext4: don't fail FS_IOC_{SETFLAGS,FSSETXATTR} after applying them aka things that fell out of my uapi16 library work Christian Brauner
2026-11-30 13:22 ` [PATCH 1/4] ext4: don't lose the result of ext4_ext_migrate() Christian Brauner
@ 2026-11-30 13:22 ` Christian Brauner
2026-11-30 13:22 ` [PATCH 3/4] ext4: split the flag checks out of ext4_ioctl_setflags() Christian Brauner
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Christian Brauner @ 2026-11-30 13:22 UTC (permalink / raw)
To: Theodore Ts'o, Jan Kara, linux-ext4
Cc: linux-fsdevel, Christian Brauner (Amutable)
ext4_ioctl_setflags() writes all flags except the extent and the journal
data flag together with a new ctime in its own handle. Only after that
does it switch the journaling mode and convert the block mapping. If
either is refused the call fails but the other flags and the ctime are
already on disk.
That's easy to hit. Callers that don't start from FS_IOC_GETFLAGS leave
EXT4_EXTENTS_FL out and so ask ext4_ind_migrate() to convert the inode
to block mapping. It refuses anything but a single extent within the
first twelve blocks. So on a file with two extents chattr =dA fails with
EOPNOTSUPP but d and A stick, and a plain chattr -e that fails still
bumps the ctime. Setting the extent flag without the extents feature has
the same problem with EINVAL and so does switching the journaling mode
on a DAX inode with EBUSY.
So do everything that can be refused before anything is written. Move
the IS_DAX() check up to the other checks and convert to extents before
the flags are written. Split ext4_ind_migrate() into
ext4_ind_migrate_prepare(), which refuses bigalloc and buffered iomap
inodes and forces out delayed allocations, and ext4_ind_migrate(), which
checks the extent tree and rewrites the block map in the same handle as
the flags under the writepages lock.
A cleared extent flag that can't be honored is now treated like the
other visible but unsettable flags if the caller asked for anything
else. The rest is applied and the call succeeds. If clearing the extent
flag is all that was asked for the call still fails and nothing changes,
not even the ctime.
Setting the extent flag and switching the journaling mode still use
their own handles. ext4_ext_migrate() builds the new tree in a temporary
inode across several handles and the mode switch flushes the journal.
Both run before the flags are written now.
Fixes: 996bb9fddd5b ("ext4: support simple conversion of extent-mapped inodes to use i_blocks")
Fixes: 4db46fc266b8 ("ext4: hook the ext3 migration interface to the EXT4_IOC_SETFLAGS ioctl")
Fixes: e9072d859df3 ("ext4: prevent data corruption with journaling + DAX")
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
fs/ext4/ext4.h | 3 +-
fs/ext4/ioctl.c | 96 ++++++++++++++++++++++++++++++++++++-------------------
fs/ext4/migrate.c | 60 ++++++++++++++++------------------
3 files changed, 92 insertions(+), 67 deletions(-)
diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index bf0325ecbec7a..de3ce16bacd09 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -3279,7 +3279,8 @@ int ext4_force_shutdown(struct super_block *sb, u32 flags);
/* migrate.c */
extern int ext4_ext_migrate(struct inode *);
-extern int ext4_ind_migrate(struct inode *inode);
+extern int ext4_ind_migrate_prepare(struct inode *inode);
+extern int ext4_ind_migrate(handle_t *handle, struct inode *inode);
/* namei.c */
extern int ext4_init_new_dir(handle_t *handle, struct inode *dir,
diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c
index e8e579498c990..a541f67c274e9 100644
--- a/fs/ext4/ioctl.c
+++ b/fs/ext4/ioctl.c
@@ -592,10 +592,11 @@ static int ext4_ioctl_setflags(struct inode *inode,
{
struct ext4_inode_info *ei = EXT4_I(inode);
handle_t *handle = NULL;
- int err = -EPERM, migrate = 0;
+ int err = -EPERM, rc, alloc_ctx = 0;
struct ext4_iloc iloc;
unsigned int oldflags, mask, i;
struct super_block *sb = inode->i_sb;
+ bool others, to_ind = false;
/* Is it quota file? Do not allow user to mess with it */
if (ext4_is_quota_file(inode))
@@ -616,9 +617,6 @@ static int ext4_ioctl_setflags(struct inode *inode,
goto flags_out;
}
- if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
- migrate = 1;
-
if ((flags ^ oldflags) & EXT4_CASEFOLD_FL) {
if (!ext4_has_feature_casefold(sb)) {
err = -EOPNOTSUPP;
@@ -636,6 +634,47 @@ static int ext4_ioctl_setflags(struct inode *inode,
}
}
+ /*
+ * Changes to the journaling mode can cause unsafe changes to
+ * S_DAX if the inode is DAX
+ */
+ if (((flags ^ oldflags) & EXT4_JOURNAL_DATA_FL) && IS_DAX(inode)) {
+ err = -EBUSY;
+ goto flags_out;
+ }
+
+ /*
+ * Callers that don't start from GETFLAGS leave the extent flag out, so
+ * a refused conversion to block mapping only fails the call if nothing
+ * else was asked for.
+ */
+ others = (flags ^ oldflags) & EXT4_FL_USER_MODIFIABLE &
+ ~EXT4_EXTENTS_FL;
+
+ /* Convert to extents first so that a refusal changes nothing. */
+ if (!(oldflags & EXT4_EXTENTS_FL) && (flags & EXT4_EXTENTS_FL)) {
+ err = ext4_ext_migrate(inode);
+ if (err)
+ goto flags_out;
+ }
+
+ if ((flags ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
+ err = ext4_change_inode_journal_flag(inode,
+ flags & EXT4_JOURNAL_DATA_FL);
+ if (err)
+ goto flags_out;
+ }
+
+ /* The conversion to block mapping shares the handle with the flags. */
+ if ((oldflags & EXT4_EXTENTS_FL) && !(flags & EXT4_EXTENTS_FL)) {
+ err = ext4_ind_migrate_prepare(inode);
+ to_ind = !err;
+ if (err == -EOPNOTSUPP && others)
+ err = 0;
+ if (err)
+ goto flags_out;
+ }
+
/*
* Wait for all pending directio and then flush all the dirty pages
* for this file. The flush marks all the pages readonly, so any
@@ -650,10 +689,13 @@ static int ext4_ioctl_setflags(struct inode *inode,
goto flags_out;
}
+ if (to_ind)
+ alloc_ctx = ext4_writepages_down_write(sb);
+
handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
if (IS_ERR(handle)) {
err = PTR_ERR(handle);
- goto flags_out;
+ goto out_unlock;
}
if (IS_SYNC(inode))
ext4_handle_sync(handle);
@@ -661,12 +703,20 @@ static int ext4_ioctl_setflags(struct inode *inode,
if (err)
goto flags_err;
+ if (to_ind) {
+ err = ext4_ind_migrate(handle, inode);
+ if (err == -EOPNOTSUPP && others)
+ err = 0;
+ if (err)
+ goto out_dirty;
+ }
+
ext4_dax_dontcache(inode, flags);
for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
if (!(mask & EXT4_FL_USER_MODIFIABLE))
continue;
- /* These flags get special treatment later */
+ /* These flags got special treatment above */
if (mask == EXT4_JOURNAL_DATA_FL || mask == EXT4_EXTENTS_FL)
continue;
if (mask & flags)
@@ -679,35 +729,15 @@ static int ext4_ioctl_setflags(struct inode *inode,
inode_set_ctime_current(inode);
inode_inc_iversion(inode);
-
- err = ext4_mark_iloc_dirty(handle, inode, &iloc);
+out_dirty:
+ rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
+ if (!err)
+ err = rc;
flags_err:
ext4_journal_stop(handle);
- if (err)
- goto flags_out;
-
- if ((flags ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
- /*
- * Changes to the journaling mode can cause unsafe changes to
- * S_DAX if the inode is DAX
- */
- if (IS_DAX(inode)) {
- err = -EBUSY;
- goto flags_out;
- }
-
- err = ext4_change_inode_journal_flag(inode,
- flags & EXT4_JOURNAL_DATA_FL);
- if (err)
- goto flags_out;
- }
- if (migrate) {
- if (flags & EXT4_EXTENTS_FL)
- err = ext4_ext_migrate(inode);
- else
- err = ext4_ind_migrate(inode);
- }
-
+out_unlock:
+ if (to_ind)
+ ext4_writepages_up_write(sb, alloc_ctx);
flags_out:
return err;
}
diff --git a/fs/ext4/migrate.c b/fs/ext4/migrate.c
index be48456fa76f3..856b30586b7a1 100644
--- a/fs/ext4/migrate.c
+++ b/fs/ext4/migrate.c
@@ -602,22 +602,11 @@ int ext4_ext_migrate(struct inode *inode)
}
/*
- * Migrate a simple extent-based inode to use the i_blocks[] array
+ * Refuse bigalloc and buffered iomap inodes and force out delayed allocations
+ * so that ext4_ind_migrate() can check the whole extent tree.
*/
-int ext4_ind_migrate(struct inode *inode)
+int ext4_ind_migrate_prepare(struct inode *inode)
{
- struct ext4_extent_header *eh;
- struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
- struct ext4_super_block *es = sbi->s_es;
- struct ext4_inode_info *ei = EXT4_I(inode);
- struct ext4_extent *ex;
- unsigned int i, len;
- ext4_lblk_t start, end;
- ext4_fsblk_t blk;
- handle_t *handle;
- int ret, ret2 = 0;
- int alloc_ctx;
-
if (!ext4_has_feature_extents(inode->i_sb) ||
(!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
return -EINVAL;
@@ -634,20 +623,24 @@ int ext4_ind_migrate(struct inode *inode)
*/
if (test_opt(inode->i_sb, DELALLOC))
ext4_alloc_da_blocks(inode);
+ return 0;
+}
- alloc_ctx = ext4_writepages_down_write(inode->i_sb);
-
- handle = ext4_journal_start(inode, EXT4_HT_MIGRATE, 1);
- if (IS_ERR(handle)) {
- ret = PTR_ERR(handle);
- goto out_unlock;
- }
- /*
- * This operation rewrites the inode's block mapping layout
- * (extents to indirect blocks) and is not tracked in the fast
- * commit log, so disable fast commits for this transaction.
- */
- ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_MIGRATE, handle);
+/*
+ * Migrate a simple extent-based inode to use the i_blocks[] array. The caller
+ * holds the writepages lock, reserved the inode in @handle and marks it dirty.
+ */
+int ext4_ind_migrate(handle_t *handle, struct inode *inode)
+{
+ struct ext4_extent_header *eh;
+ struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
+ struct ext4_super_block *es = sbi->s_es;
+ struct ext4_inode_info *ei = EXT4_I(inode);
+ struct ext4_extent *ex;
+ unsigned int i, len;
+ ext4_lblk_t start, end;
+ ext4_fsblk_t blk;
+ int ret;
down_write(&EXT4_I(inode)->i_data_sem);
ret = ext4_ext_check_inode(inode);
@@ -674,17 +667,18 @@ int ext4_ind_migrate(struct inode *inode)
}
}
+ /*
+ * This operation rewrites the inode's block mapping layout
+ * (extents to indirect blocks) and is not tracked in the fast
+ * commit log, so disable fast commits for this transaction.
+ */
+ ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_MIGRATE, handle);
+
ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
memset(ei->i_data, 0, sizeof(ei->i_data));
for (i = start; i <= end; i++)
ei->i_data[i] = cpu_to_le32(blk++);
- ret2 = ext4_mark_inode_dirty(handle, inode);
- if (unlikely(ret2 && !ret))
- ret = ret2;
errout:
up_write(&EXT4_I(inode)->i_data_sem);
- ext4_journal_stop(handle);
-out_unlock:
- ext4_writepages_up_write(inode->i_sb, alloc_ctx);
return ret;
}
--
2.53.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH 3/4] ext4: split the flag checks out of ext4_ioctl_setflags()
2026-11-30 13:22 [PATCH 0/4] ext4: don't fail FS_IOC_{SETFLAGS,FSSETXATTR} after applying them aka things that fell out of my uapi16 library work Christian Brauner
2026-11-30 13:22 ` [PATCH 1/4] ext4: don't lose the result of ext4_ext_migrate() Christian Brauner
2026-11-30 13:22 ` [PATCH 2/4] ext4: don't fail FS_IOC_SETFLAGS after applying it Christian Brauner
@ 2026-11-30 13:22 ` Christian Brauner
2026-11-30 13:22 ` [PATCH 4/4] ext4: don't fail FS_IOC_FSSETXATTR after applying it Christian Brauner
2026-11-30 13:39 ` [PATCH 0/4] ext4: don't fail FS_IOC_{SETFLAGS,FSSETXATTR} after applying them aka things that fell out of my uapi16 library work sashiko-bot
4 siblings, 0 replies; 6+ messages in thread
From: Christian Brauner @ 2026-11-30 13:22 UTC (permalink / raw)
To: Theodore Ts'o, Jan Kara, linux-ext4
Cc: linux-fsdevel, Christian Brauner (Amutable)
Move the checks that refuse a flags change into
ext4_ioctl_check_setflags() and call it from ext4_fileattr_set() before
ext4_ioctl_setflags(). The next patch needs them to run before the
project ID is changed. No functional changes.
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
fs/ext4/ioctl.c | 62 ++++++++++++++++++++++++++++-----------------------------
1 file changed, 31 insertions(+), 31 deletions(-)
diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c
index a541f67c274e9..d341e5dbda108 100644
--- a/fs/ext4/ioctl.c
+++ b/fs/ext4/ioctl.c
@@ -587,61 +587,58 @@ static bool dax_compatible(struct inode *inode, unsigned int oldflags,
return true;
}
-static int ext4_ioctl_setflags(struct inode *inode,
- unsigned int flags)
+static int ext4_ioctl_check_setflags(struct inode *inode, unsigned int flags)
{
- struct ext4_inode_info *ei = EXT4_I(inode);
- handle_t *handle = NULL;
- int err = -EPERM, rc, alloc_ctx = 0;
- struct ext4_iloc iloc;
- unsigned int oldflags, mask, i;
+ unsigned int oldflags = EXT4_I(inode)->i_flags;
struct super_block *sb = inode->i_sb;
- bool others, to_ind = false;
/* Is it quota file? Do not allow user to mess with it */
if (ext4_is_quota_file(inode))
- goto flags_out;
+ return -EPERM;
- oldflags = ei->i_flags;
/*
* The JOURNAL_DATA flag can only be changed by
* the relevant capability.
*/
if ((flags ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
if (!capable(CAP_SYS_RESOURCE))
- goto flags_out;
+ return -EPERM;
}
- if (!dax_compatible(inode, oldflags, flags)) {
- err = -EOPNOTSUPP;
- goto flags_out;
- }
+ if (!dax_compatible(inode, oldflags, flags))
+ return -EOPNOTSUPP;
if ((flags ^ oldflags) & EXT4_CASEFOLD_FL) {
- if (!ext4_has_feature_casefold(sb)) {
- err = -EOPNOTSUPP;
- goto flags_out;
- }
+ if (!ext4_has_feature_casefold(sb))
+ return -EOPNOTSUPP;
- if (!S_ISDIR(inode->i_mode)) {
- err = -ENOTDIR;
- goto flags_out;
- }
+ if (!S_ISDIR(inode->i_mode))
+ return -ENOTDIR;
- if (!ext4_empty_dir(inode)) {
- err = -ENOTEMPTY;
- goto flags_out;
- }
+ if (!ext4_empty_dir(inode))
+ return -ENOTEMPTY;
}
/*
* Changes to the journaling mode can cause unsafe changes to
* S_DAX if the inode is DAX
*/
- if (((flags ^ oldflags) & EXT4_JOURNAL_DATA_FL) && IS_DAX(inode)) {
- err = -EBUSY;
- goto flags_out;
- }
+ if (((flags ^ oldflags) & EXT4_JOURNAL_DATA_FL) && IS_DAX(inode))
+ return -EBUSY;
+
+ return 0;
+}
+
+static int ext4_ioctl_setflags(struct inode *inode,
+ unsigned int flags)
+{
+ struct ext4_inode_info *ei = EXT4_I(inode);
+ handle_t *handle = NULL;
+ int err, rc, alloc_ctx = 0;
+ struct ext4_iloc iloc;
+ unsigned int oldflags = ei->i_flags, mask, i;
+ struct super_block *sb = inode->i_sb;
+ bool others, to_ind = false;
/*
* Callers that don't start from GETFLAGS leave the extent flag out, so
@@ -1058,6 +1055,9 @@ int ext4_fileattr_set(const struct mnt_idmap *idmap,
if (ext4_mask_flags(inode->i_mode, flags) != flags)
goto out;
err = ext4_ioctl_check_immutable(inode, fa->fsx_projid, flags);
+ if (err)
+ goto out;
+ err = ext4_ioctl_check_setflags(inode, flags);
if (err)
goto out;
err = ext4_ioctl_setflags(inode, flags);
--
2.53.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH 4/4] ext4: don't fail FS_IOC_FSSETXATTR after applying it
2026-11-30 13:22 [PATCH 0/4] ext4: don't fail FS_IOC_{SETFLAGS,FSSETXATTR} after applying them aka things that fell out of my uapi16 library work Christian Brauner
` (2 preceding siblings ...)
2026-11-30 13:22 ` [PATCH 3/4] ext4: split the flag checks out of ext4_ioctl_setflags() Christian Brauner
@ 2026-11-30 13:22 ` Christian Brauner
2026-11-30 13:39 ` [PATCH 0/4] ext4: don't fail FS_IOC_{SETFLAGS,FSSETXATTR} after applying them aka things that fell out of my uapi16 library work sashiko-bot
4 siblings, 0 replies; 6+ messages in thread
From: Christian Brauner @ 2026-11-30 13:22 UTC (permalink / raw)
To: Theodore Ts'o, Jan Kara, linux-ext4
Cc: linux-fsdevel, Christian Brauner (Amutable)
ext4_fileattr_set() writes the flags and the ctime in
ext4_ioctl_setflags() and only afterwards calls ext4_ioctl_setproject()
which can still refuse the new project ID. It returns EOPNOTSUPP if the
filesystem doesn't have the project feature and __dquot_transfer()
returns EDQUOT if the inode doesn't fit under the hard limit of the new
project. In both cases FS_IOC_FSSETXATTR fails with the flags and the
ctime already changed. Asking for a new project ID alone still bumps the
ctime before failing.
Check the project ID together with the flags before anything is written
and change it in the same handle as the flags. dquot_initialize() and
making room for i_projid in the inode still happen before the handle is
started. The quota transfer runs first in the handle so a refused
transfer leaves the inode alone. The project ID and the flags are now
written in a single handle instead of two.
A project ID change never comes together with a conversion or a journal
mode switch. FS_IOC_FSSETXATTR can't ask for either because the vfs
fills in the flags it doesn't know about from the inode and
FS_IOC_SETFLAGS passes the current project ID.
Note that the quota file check in ext4_ioctl_setproject() goes away
because ext4_ioctl_check_setflags() already refuses any change to a
quota file. The check for 128 byte inodes now only applies if the
project ID actually changes. Without CONFIG_QUOTA nothing changes in
practice because ext4 doesn't mount a filesystem with the quota or
project feature read-write there.
Fixes: 9b7365fc1c82 ("ext4: add FS_IOC_FSSETXATTR/FS_IOC_FSGETXATTR interface support")
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
fs/ext4/ioctl.c | 209 ++++++++++++++++++++++++++++----------------------------
1 file changed, 106 insertions(+), 103 deletions(-)
diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c
index d341e5dbda108..552ae3505971c 100644
--- a/fs/ext4/ioctl.c
+++ b/fs/ext4/ioctl.c
@@ -629,12 +629,94 @@ static int ext4_ioctl_check_setflags(struct inode *inode, unsigned int flags)
return 0;
}
-static int ext4_ioctl_setflags(struct inode *inode,
- unsigned int flags)
+#ifdef CONFIG_QUOTA
+/* Make room for the project ID before the handle is started. */
+static int ext4_ioctl_prepare_setproject(struct inode *inode)
+{
+ struct super_block *sb = inode->i_sb;
+ struct ext4_inode_info *ei = EXT4_I(inode);
+ struct ext4_inode *raw_inode;
+ struct ext4_iloc iloc;
+ int err;
+
+ err = dquot_initialize(inode);
+ if (err)
+ return err;
+
+ err = ext4_get_inode_loc(inode, &iloc);
+ if (err)
+ return err;
+
+ raw_inode = ext4_raw_inode(&iloc);
+ if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid))
+ return ext4_expand_extra_isize(inode,
+ EXT4_SB(sb)->s_want_extra_isize,
+ &iloc);
+ brelse(iloc.bh);
+ return 0;
+}
+
+/* Move the inode reserved in the running handle into the project @kprojid. */
+static int ext4_ioctl_setproject(struct inode *inode, kprojid_t kprojid)
+{
+ struct dquot *transfer_to[MAXQUOTAS] = { };
+ int err;
+
+ transfer_to[PRJQUOTA] = dqget(inode->i_sb, make_kqid_projid(kprojid));
+ if (!IS_ERR(transfer_to[PRJQUOTA])) {
+
+ /* __dquot_transfer() calls back ext4_get_inode_usage() which
+ * counts xattr inode references.
+ */
+ down_read(&EXT4_I(inode)->xattr_sem);
+ err = __dquot_transfer(inode, transfer_to);
+ up_read(&EXT4_I(inode)->xattr_sem);
+ dqput(transfer_to[PRJQUOTA]);
+ if (err)
+ return err;
+ }
+
+ EXT4_I(inode)->i_projid = kprojid;
+ return 0;
+}
+#else
+static int ext4_ioctl_prepare_setproject(struct inode *inode)
+{
+ return -EOPNOTSUPP;
+}
+
+static int ext4_ioctl_setproject(struct inode *inode, kprojid_t kprojid)
+{
+ return -EOPNOTSUPP;
+}
+#endif
+
+/* Refuse a project ID change before anything is written. */
+static int ext4_ioctl_check_setproject(struct inode *inode, __u32 projid)
+{
+ struct super_block *sb = inode->i_sb;
+
+ if (projid_eq(make_kprojid(&init_user_ns, (projid_t)projid),
+ EXT4_I(inode)->i_projid))
+ return 0;
+
+ if (!IS_ENABLED(CONFIG_QUOTA) || !ext4_has_feature_project(sb))
+ return -EOPNOTSUPP;
+
+ if (EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE)
+ return -EOPNOTSUPP;
+
+ return 0;
+}
+
+static int ext4_ioctl_setflags(struct inode *inode, unsigned int flags,
+ __u32 projid)
{
struct ext4_inode_info *ei = EXT4_I(inode);
+ kprojid_t kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
+ bool set_prj = !projid_eq(kprojid, ei->i_projid);
handle_t *handle = NULL;
- int err, rc, alloc_ctx = 0;
+ int err, rc, credits = 1, alloc_ctx = 0;
struct ext4_iloc iloc;
unsigned int oldflags = ei->i_flags, mask, i;
struct super_block *sb = inode->i_sb;
@@ -645,8 +727,9 @@ static int ext4_ioctl_setflags(struct inode *inode,
* a refused conversion to block mapping only fails the call if nothing
* else was asked for.
*/
- others = (flags ^ oldflags) & EXT4_FL_USER_MODIFIABLE &
- ~EXT4_EXTENTS_FL;
+ others = set_prj ||
+ ((flags ^ oldflags) & EXT4_FL_USER_MODIFIABLE &
+ ~EXT4_EXTENTS_FL);
/* Convert to extents first so that a refusal changes nothing. */
if (!(oldflags & EXT4_EXTENTS_FL) && (flags & EXT4_EXTENTS_FL)) {
@@ -672,6 +755,15 @@ static int ext4_ioctl_setflags(struct inode *inode,
goto flags_out;
}
+ /* The project ID changes in the same handle as the flags. */
+ if (set_prj) {
+ err = ext4_ioctl_prepare_setproject(inode);
+ if (err)
+ goto flags_out;
+ credits = EXT4_QUOTA_INIT_BLOCKS(sb) +
+ EXT4_QUOTA_DEL_BLOCKS(sb) + 3;
+ }
+
/*
* Wait for all pending directio and then flush all the dirty pages
* for this file. The flush marks all the pages readonly, so any
@@ -689,7 +781,7 @@ static int ext4_ioctl_setflags(struct inode *inode,
if (to_ind)
alloc_ctx = ext4_writepages_down_write(sb);
- handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
+ handle = ext4_journal_start(inode, EXT4_HT_INODE, credits);
if (IS_ERR(handle)) {
err = PTR_ERR(handle);
goto out_unlock;
@@ -700,6 +792,12 @@ static int ext4_ioctl_setflags(struct inode *inode,
if (err)
goto flags_err;
+ if (set_prj) {
+ err = ext4_ioctl_setproject(inode, kprojid);
+ if (err)
+ goto out_dirty;
+ }
+
if (to_ind) {
err = ext4_ind_migrate(handle, inode);
if (err == -EOPNOTSUPP && others)
@@ -739,101 +837,6 @@ static int ext4_ioctl_setflags(struct inode *inode,
return err;
}
-#ifdef CONFIG_QUOTA
-static int ext4_ioctl_setproject(struct inode *inode, __u32 projid)
-{
- struct super_block *sb = inode->i_sb;
- struct ext4_inode_info *ei = EXT4_I(inode);
- int err, rc;
- handle_t *handle;
- kprojid_t kprojid;
- struct ext4_iloc iloc;
- struct ext4_inode *raw_inode;
- struct dquot *transfer_to[MAXQUOTAS] = { };
-
- if (!ext4_has_feature_project(sb)) {
- if (projid != EXT4_DEF_PROJID)
- return -EOPNOTSUPP;
- else
- return 0;
- }
-
- if (EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE)
- return -EOPNOTSUPP;
-
- kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
-
- if (projid_eq(kprojid, EXT4_I(inode)->i_projid))
- return 0;
-
- err = -EPERM;
- /* Is it quota file? Do not allow user to mess with it */
- if (ext4_is_quota_file(inode))
- return err;
-
- err = dquot_initialize(inode);
- if (err)
- return err;
-
- err = ext4_get_inode_loc(inode, &iloc);
- if (err)
- return err;
-
- raw_inode = ext4_raw_inode(&iloc);
- if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) {
- err = ext4_expand_extra_isize(inode,
- EXT4_SB(sb)->s_want_extra_isize,
- &iloc);
- if (err)
- return err;
- } else {
- brelse(iloc.bh);
- }
-
- handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
- EXT4_QUOTA_INIT_BLOCKS(sb) +
- EXT4_QUOTA_DEL_BLOCKS(sb) + 3);
- if (IS_ERR(handle))
- return PTR_ERR(handle);
-
- err = ext4_reserve_inode_write(handle, inode, &iloc);
- if (err)
- goto out_stop;
-
- transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
- if (!IS_ERR(transfer_to[PRJQUOTA])) {
-
- /* __dquot_transfer() calls back ext4_get_inode_usage() which
- * counts xattr inode references.
- */
- down_read(&EXT4_I(inode)->xattr_sem);
- err = __dquot_transfer(inode, transfer_to);
- up_read(&EXT4_I(inode)->xattr_sem);
- dqput(transfer_to[PRJQUOTA]);
- if (err)
- goto out_dirty;
- }
-
- EXT4_I(inode)->i_projid = kprojid;
- inode_set_ctime_current(inode);
- inode_inc_iversion(inode);
-out_dirty:
- rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
- if (!err)
- err = rc;
-out_stop:
- ext4_journal_stop(handle);
- return err;
-}
-#else
-static int ext4_ioctl_setproject(struct inode *inode, __u32 projid)
-{
- if (projid != EXT4_DEF_PROJID)
- return -EOPNOTSUPP;
- return 0;
-}
-#endif
-
int ext4_force_shutdown(struct super_block *sb, u32 flags)
{
struct ext4_sb_info *sbi = EXT4_SB(sb);
@@ -1060,10 +1063,10 @@ int ext4_fileattr_set(const struct mnt_idmap *idmap,
err = ext4_ioctl_check_setflags(inode, flags);
if (err)
goto out;
- err = ext4_ioctl_setflags(inode, flags);
+ err = ext4_ioctl_check_setproject(inode, fa->fsx_projid);
if (err)
goto out;
- err = ext4_ioctl_setproject(inode, fa->fsx_projid);
+ err = ext4_ioctl_setflags(inode, flags, fa->fsx_projid);
out:
return err;
}
--
2.53.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH 0/4] ext4: don't fail FS_IOC_{SETFLAGS,FSSETXATTR} after applying them aka things that fell out of my uapi16 library work
2026-11-30 13:22 [PATCH 0/4] ext4: don't fail FS_IOC_{SETFLAGS,FSSETXATTR} after applying them aka things that fell out of my uapi16 library work Christian Brauner
` (3 preceding siblings ...)
2026-11-30 13:22 ` [PATCH 4/4] ext4: don't fail FS_IOC_FSSETXATTR after applying it Christian Brauner
@ 2026-11-30 13:39 ` sashiko-bot
4 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-30 13:39 UTC (permalink / raw)
To: Christian Brauner, linux-ext4; +Cc: tytso
Sashiko has reviewed this 4-patch series and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261130-work-ext4-setflags_migrate-v1-0-80f1875cfadc@kernel.org
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-11-30 13:39 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 13:22 [PATCH 0/4] ext4: don't fail FS_IOC_{SETFLAGS,FSSETXATTR} after applying them aka things that fell out of my uapi16 library work Christian Brauner
2026-11-30 13:22 ` [PATCH 1/4] ext4: don't lose the result of ext4_ext_migrate() Christian Brauner
2026-11-30 13:22 ` [PATCH 2/4] ext4: don't fail FS_IOC_SETFLAGS after applying it Christian Brauner
2026-11-30 13:22 ` [PATCH 3/4] ext4: split the flag checks out of ext4_ioctl_setflags() Christian Brauner
2026-11-30 13:22 ` [PATCH 4/4] ext4: don't fail FS_IOC_FSSETXATTR after applying it Christian Brauner
2026-11-30 13:39 ` [PATCH 0/4] ext4: don't fail FS_IOC_{SETFLAGS,FSSETXATTR} after applying them aka things that fell out of my uapi16 library work sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox