From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796044961; b=kw9ZrDrwUnUloFXwgymF9UDgxH9apoSi4W96ZI3KopGffFrVIfbSa1s3/nAH487ftbjQ A/lOfhTOD/HWOAlHWgz5qj+g2rJxyL/iA/cMbR5HWeq0q6y0rRaWlMcPN6CLRcc9Dzgpb zZaGP5p/w7VGgY2ZHbrqKNC4QloVliJzv66XmTtCfWGbL6XGYUcImHj+7AGgokbhNVBGc 3zyOeKQnAs8/4AC/8u6Cfy2uzEHYnLYT0rfmHMub477ilc0rdTrLJPaw/6FV2n0HSkL1r wViWbh0MonNDkZxweuOxbzKp5ESYxX2Vl+J9yaYby2qk+Bkfxjd+6uj/gX3MrKQfxGA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796044961; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key; bh=61Y4PeAa7xTKiGi/6a7Jk79k80vxVek6/JR1v4obsZc=; b=DmLlUVNzVrUC7K6G430OwTH2TtihFuH1f0xOKtNuS+L++g5VfWoKJ+hl8d+Rc+9ys4jj PZaTvsiw+wCL9Z2FEPWDUCPi3bP9w6Ne53nr16xd0Iatyi2NMUIuTROdFy0xNO1RJdC1x MU1jBSlxyfxhtS+kHTefQSVbRuwsAo0QjLY/Xw18CbeMkOd8/7p6/uaigwTCnl9XWX/dv Cur5ToEyLPRborO/jjRZXXTFwNSRVRVTc82YdKCTOfFe3HyUiErcFGH1rO8p8YhRLgNtd mxFvVwMHjEXdCRT8khRkGZIvbtyM2Oj2ljtZ8VTzikTk2lttLBwzyapHChjIiYBqLcg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=SLGpWbio; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E5903606A699; Mon, 30 Nov 2026 13:22:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=SLGpWbio Received: by smtp.kernel.org (Postfix) with ESMTPSA id 49EAA1F0089F; Mon, 30 Nov 2026 13:22:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796044960; bh=61Y4PeAa7xTKiGi/6a7Jk79k80vxVek6/JR1v4obsZc=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=SLGpWbiozQWtfG8SLiD4rpuGV1qv00eWnaG0eUL8T3hJczQl6/j5xkYHnYmTH3V+k D85pwHNjSY7nxRxMdv8UiabVm9LhlrUkSbTrcLytz5wc+64swr2zuXvxI30lc1y2Gj wDooFc0q3P16+Yb42Nq82BSB/jr+NtspXcpmp8jSusvEaWgF9KNcOWxOhaLKu0A2T9 VFkIni/uPAM60harcaytrkQR/OqkaMxwGKMOGsJZTBt6exRqoK/AhWt+DWxVJ8/voy zxiG/92yyJfMbWWLOmmlQykxAkUn5IQY693OtzZitD3LasG0FwwbQZj+ZEkKhu52yK 7InDMj8jSyn5A== From: Christian Brauner Date: Mon, 30 Nov 2026 14:22:14 +0100 Subject: [PATCH 2/4] ext4: don't fail FS_IOC_SETFLAGS after applying it X-Mailing-List: linux-ext4@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261130-work-ext4-setflags_migrate-v1-2-80f1875cfadc@kernel.org> References: <20261130-work-ext4-setflags_migrate-v1-0-80f1875cfadc@kernel.org> In-Reply-To: <20261130-work-ext4-setflags_migrate-v1-0-80f1875cfadc@kernel.org> To: Theodore Ts'o , Jan Kara , linux-ext4@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, "Christian Brauner (Amutable)" X-Mailer: b4 0.17-dev-04696 X-Developer-Signature: v=1; a=openpgp-sha256; l=10387; i=brauner@kernel.org; h=from:subject:message-id; bh=Ep5Stgq191tvydiuoHSz+/KWdNMbavFO5fuuj0t+Pys=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWTzVkz9yLM+evnPZH/z5QqKBfMN+q9Ns+YL45hh4NfyM izq0dkpHaUsDGJcDLJiiiwO7Sbhcst5KjYbZWrAzGFlAhnCwMUpABNx2cjI8NUhKvNY1D/XsDOt iytnzNRtZzG9pHByQvD9HJ49X+YaZzD8s86XiOG9NKWq+O3CGBePZRn/5O+t3yWZ9j3+gW6ykEU QHwA= X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 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) --- 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