From: Christian Brauner <brauner@kernel.org>
To: Theodore Ts'o <tytso@mit.edu>, Jan Kara <jack@suse.cz>,
linux-ext4@vger.kernel.org
Cc: linux-fsdevel@vger.kernel.org,
"Christian Brauner (Amutable)" <brauner@kernel.org>
Subject: [PATCH 2/4] ext4: don't fail FS_IOC_SETFLAGS after applying it
Date: Mon, 30 Nov 2026 14:22:14 +0100 [thread overview]
Message-ID: <20261130-work-ext4-setflags_migrate-v1-2-80f1875cfadc@kernel.org> (raw)
In-Reply-To: <20261130-work-ext4-setflags_migrate-v1-0-80f1875cfadc@kernel.org>
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
next prev parent reply other threads:[~2026-11-30 13:22 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261130-work-ext4-setflags_migrate-v1-2-80f1875cfadc@kernel.org \
--to=brauner@kernel.org \
--cc=jack@suse.cz \
--cc=linux-ext4@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=tytso@mit.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox