Linux EXT4 FS development
 help / color / mirror / Atom feed
From: "Theodore Ts'o" <tytso@mit.edu>
To: Ext4 Developers List <linux-ext4@vger.kernel.org>
Cc: "Theodore Ts'o" <tytso@mit.edu>
Subject: [PATCH 05/23] ext4: enforce feature prerequisites when tuning a mounted fs
Date: Wed, 11 Nov 2026 23:46:54 -0500	[thread overview]
Message-ID: <20261112044712.1577296-6-tytso@mit.edu> (raw)
In-Reply-To: <20261112044712.1577296-1-tytso@mit.edu>

EXT4_IOC_SET_TUNE_SB_PARAM lets userspace enable a number of features
on a mounted file system, but it skips prerequisites that are
otherwise enforced when the file system is mounted (or by tune2fs),
leading to corrupted metadata or file systems that can no longer be
mounted read-write:

 - metadata_csum_seed: ext4_sb_setparams() stores sbi->s_csum_seed in
   s_checksum_seed, but that is only initialized when metadata_csum
   (or ea_inode) is enabled; otherwise a zero seed is written.
   tune2fs likewise requires metadata_csum for this feature.

 - ea_inode: EA inode hashes are seeded with sbi->s_csum_seed, which
   is zero unless metadata_csum or csum_seed was enabled at mount
   time.  After a remount the seed is computed from the UUID, so all
   EA inodes written before the remount fail hash verification.  The
   feature is also refused at mount time in Hurd compat mode.

 - encrypt: ext4 disables large folios at mount time when encryption
   is enabled, since fscrypt can't handle them.  Enabling it online
   while large folios are in use lets new encrypted files use large
   folios, tripping VM_BUG_ON_FOLIO() in
   fscrypt_encrypt_pagecache_blocks().

 - project: a kernel built without CONFIG_QUOTA and CONFIG_QFMT_V2
   refuses to mount a file system with this feature read-write.
   Project ids (like the fields covered by extra_isize) also need
   inodes larger than 128 bytes; tune2fs refuses to enable project
   quotas otherwise.

Reject these cases with -EINVAL or -EOPNOTSUPP, and when ea_inode is
enabled on a file system without metadata_csum or csum_seed, compute
sbi->s_csum_seed the same way the next mount will.

Fixes: 04a91570ac67 ("ext4: implemet new ioctls to set and get superblock parameters")
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
---
 fs/ext4/ioctl-test.c | 95 ++++++++++++++++++++++++++++++++++++++++++++
 fs/ext4/ioctl.c      | 42 ++++++++++++++++++++
 2 files changed, 137 insertions(+)

diff --git a/fs/ext4/ioctl-test.c b/fs/ext4/ioctl-test.c
index 4641ba7b31ff..fd7b4ff2cd81 100644
--- a/fs/ext4/ioctl-test.c
+++ b/fs/ext4/ioctl-test.c
@@ -795,6 +795,100 @@ static void test_tune_sb_dir_index_hash_state(struct kunit *test)
 			EXT2_FLAGS_UNSIGNED_HASH);
 }
 
+static int ioctl_test_set_features(struct super_block *sb, u32 incompat,
+				   u32 ro_compat)
+{
+	struct ext4_tune_sb_params params;
+
+	memset(&params, 0, sizeof(params));
+	params.set_flags = EXT4_TUNE_FL_EDIT_FEATURES;
+	params.set_feature_incompat_mask = incompat;
+	params.set_feature_ro_compat_mask = ro_compat;
+	return ext4_validate_tune_sb_params_test(sb, &params);
+}
+
+/*
+ * Verify that features whose prerequisites are otherwise only enforced at
+ * mount time are rejected when they can't be satisfied, and that enabling
+ * ea_inode online initializes the checksum seed used for EA inode hashes.
+ */
+static void test_tune_sb_feature_prereqs(struct kunit *test)
+{
+	struct ioctl_test_ctx *ctx = test->priv;
+	struct super_block *sb = ctx->sb;
+	struct ext4_sb_info *sbi = &ctx->sbi;
+	struct ext4_super_block *es = ctx->es;
+	u32 seed = ext4_chksum(~0, es->s_uuid, sizeof(es->s_uuid));
+	int expected;
+
+	/* csum_seed requires metadata_csum */
+	KUNIT_EXPECT_EQ(test,
+			ioctl_test_set_features(sb,
+					EXT4_FEATURE_INCOMPAT_CSUM_SEED, 0),
+			0);
+
+	/* ea_inode is refused in Hurd compat mode */
+	set_opt2(sb, HURD_COMPAT);
+	KUNIT_EXPECT_EQ(test,
+			ioctl_test_set_features(sb,
+					EXT4_FEATURE_INCOMPAT_EA_INODE, 0),
+			-EINVAL);
+	clear_opt2(sb, HURD_COMPAT);
+
+	/* encrypt is refused while large folios are in use */
+	sbi->s_max_folio_order = 2;
+	KUNIT_EXPECT_EQ(test,
+			ioctl_test_set_features(sb,
+					EXT4_FEATURE_INCOMPAT_ENCRYPT, 0),
+			-EOPNOTSUPP);
+	sbi->s_max_folio_order = 0;
+	KUNIT_EXPECT_EQ(test,
+			ioctl_test_set_features(sb,
+					EXT4_FEATURE_INCOMPAT_ENCRYPT, 0),
+			0);
+
+	/* project needs quota support and large inodes */
+	expected = IS_ENABLED(CONFIG_QUOTA) && IS_ENABLED(CONFIG_QFMT_V2) ?
+		   0 : -EOPNOTSUPP;
+	KUNIT_EXPECT_EQ(test,
+			ioctl_test_set_features(sb, 0,
+					EXT4_FEATURE_RO_COMPAT_PROJECT),
+			expected);
+
+	ext4_clear_feature_extra_isize(sb);
+	es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
+	sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
+	KUNIT_EXPECT_EQ(test,
+			ioctl_test_set_features(sb, 0,
+					EXT4_FEATURE_RO_COMPAT_PROJECT),
+			-EINVAL);
+	KUNIT_EXPECT_EQ(test,
+			ioctl_test_set_features(sb, 0,
+					EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE),
+			-EINVAL);
+	es->s_inode_size = cpu_to_le16(IOCTL_TEST_INODE_SIZE);
+	sbi->s_inode_size = IOCTL_TEST_INODE_SIZE;
+	KUNIT_EXPECT_EQ(test,
+			ioctl_test_set_features(sb, 0,
+					EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE),
+			0);
+
+	/* Without metadata_csum, csum_seed is refused ... */
+	ext4_clear_feature_metadata_csum(sb);
+	KUNIT_EXPECT_EQ(test,
+			ioctl_test_set_features(sb,
+					EXT4_FEATURE_INCOMPAT_CSUM_SEED, 0),
+			-EINVAL);
+
+	/* ... and enabling ea_inode sets up the checksum seed */
+	sbi->s_csum_seed = 0;
+	KUNIT_EXPECT_EQ(test,
+			ioctl_test_set_features(sb,
+					EXT4_FEATURE_INCOMPAT_EA_INODE, 0),
+			0);
+	KUNIT_EXPECT_EQ(test, sbi->s_csum_seed, seed);
+}
+
 /*
  * Verify ext4_update_backup_sb, ext4_update_superblocks_fn, and
  * ext4_update_overhead across primary + 4 sparse_super backup groups (1, 3,
@@ -1303,6 +1397,7 @@ static struct kunit_case ioctl_test_cases[] = {
 	KUNIT_CASE(test_validate_tune_sb_params),
 	KUNIT_CASE(test_tune_sb_mount_opts_guard),
 	KUNIT_CASE(test_tune_sb_dir_index_hash_state),
+	KUNIT_CASE(test_tune_sb_feature_prereqs),
 	KUNIT_CASE(test_update_superblocks_and_overhead),
 	KUNIT_CASE(test_update_backup_sb_bad_csum),
 	KUNIT_CASE(test_swap_inode_data_and_seed),
diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c
index e2a5713a4562..8bd4958217e0 100644
--- a/fs/ext4/ioctl.c
+++ b/fs/ext4/ioctl.c
@@ -1489,6 +1489,48 @@ static int ext4_validate_tune_sb_params(struct super_block *sb,
 			~le32_to_cpu(es->s_feature_incompat);
 		params->set_feature_ro_compat_mask &=
 			~le32_to_cpu(es->s_feature_ro_compat);
+
+		/*
+		 * Reject features whose prerequisites are otherwise only
+		 * checked at mount time.  The checksum seed must already be
+		 * initialized from metadata_csum, ea_inode is unsupported
+		 * in Hurd compat mode, encryption is incompatible with
+		 * large folios, and project quotas / extra_isize need
+		 * large inodes (and project quotas need quota support).
+		 */
+		if ((params->set_feature_incompat_mask &
+		     EXT4_FEATURE_INCOMPAT_CSUM_SEED) &&
+		    !ext4_has_feature_metadata_csum(sb))
+			return -EINVAL;
+		if ((params->set_feature_incompat_mask &
+		     EXT4_FEATURE_INCOMPAT_EA_INODE) &&
+		    test_opt2(sb, HURD_COMPAT))
+			return -EINVAL;
+		if ((params->set_feature_incompat_mask &
+		     EXT4_FEATURE_INCOMPAT_ENCRYPT) &&
+		    sbi->s_max_folio_order)
+			return -EOPNOTSUPP;
+		if ((params->set_feature_ro_compat_mask &
+		     (EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE |
+		      EXT4_FEATURE_RO_COMPAT_PROJECT)) &&
+		    EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE)
+			return -EINVAL;
+		if ((params->set_feature_ro_compat_mask &
+		     EXT4_FEATURE_RO_COMPAT_PROJECT) &&
+		    (!IS_ENABLED(CONFIG_QUOTA) || !IS_ENABLED(CONFIG_QFMT_V2)))
+			return -EOPNOTSUPP;
+
+		/*
+		 * ea_inode hashes use s_csum_seed, which is only computed
+		 * at mount time if metadata_csum or csum_seed is enabled.
+		 * Initialize it the same way the next mount will.
+		 */
+		if ((params->set_feature_incompat_mask &
+		     EXT4_FEATURE_INCOMPAT_EA_INODE) &&
+		    !ext4_has_feature_metadata_csum(sb) &&
+		    !ext4_has_feature_csum_seed(sb))
+			sbi->s_csum_seed = ext4_chksum(~0, es->s_uuid,
+						       sizeof(es->s_uuid));
 		if (params->set_feature_compat_mask &
 		    EXT4_FEATURE_COMPAT_DIR_INDEX) {
 			uuid_t uu;
-- 
2.56.0.542.g3ef89b7020-goog


  parent reply	other threads:[~2026-11-12  4:47 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-12  4:46 [PATCH 00/23] ext4: lots of misc bug fixes Theodore Ts'o
2026-11-12  4:46 ` [PATCH 01/23] ext4: only update s_mount_opts when EXT4_TUNE_FL_MOUNT_OPTS is set Theodore Ts'o
2026-11-12  4:46 ` [PATCH 02/23] ext4: return -EFSBADCRC when skipping a corrupt backup superblock Theodore Ts'o
2026-11-12  4:46 ` [PATCH 03/23] ext4: don't allow enabling casefold via EXT4_IOC_SET_TUNE_SB_PARAM Theodore Ts'o
2026-11-12  4:46 ` [PATCH 04/23] ext4: persist the htree hash seed when enabling dir_index online Theodore Ts'o
2026-11-12  4:46 ` Theodore Ts'o [this message]
2026-11-12  4:46 ` [PATCH 06/23] ext4: handle s_lastcheck_hi in the get/set tune superblock ioctls Theodore Ts'o
2026-11-12  4:46 ` [PATCH 07/23] ext4: pass missing ioctls through ext4_compat_ioctl Theodore Ts'o
2026-11-12  4:46 ` [PATCH 08/23] ext4: fix inverted err_report_sec check in ext4_update_super() Theodore Ts'o
2026-11-12  4:46 ` [PATCH 09/23] ext4: fix err_report_sec_store() timer and value handling Theodore Ts'o
2026-11-12  4:46 ` [PATCH 10/23] ext4: fix inode table accounting in count_overhead() Theodore Ts'o
2026-11-12  4:47 ` [PATCH 11/23] ext4: handle flex groups straddling s_first_meta_bg in online resize Theodore Ts'o
2026-11-12  4:47 ` [PATCH 12/23] ext4: reject EXT4_IOC_GROUP_ADD inputs larger than a block group Theodore Ts'o
2026-11-12  4:47 ` [PATCH 13/23] ext4: propagate ext4_alloc_group_tables() errors from ext4_resize_fs() Theodore Ts'o
2026-11-12  4:47 ` [PATCH 14/23] ext4: reject online shrink before probing the device size Theodore Ts'o
2026-11-12  4:47 ` [PATCH 15/23] ext4: cap migrated extents at EXT_INIT_MAX_LEN Theodore Ts'o
2026-11-12  4:47 ` [PATCH 16/23] ext4: don't treat a handle restart as an error in finish_range() Theodore Ts'o
2026-11-12  4:47 ` [PATCH 17/23] ext4: don't clobber the migration result in ext4_ext_migrate() Theodore Ts'o
2026-11-12  4:47 ` [PATCH 18/23] ext4: fix EOF clamping of the range in mext_check_adjust_range() Theodore Ts'o
2026-11-12  4:47 ` [PATCH 19/23] ext4: bound the MMP update and check intervals Theodore Ts'o
2026-11-12  4:47 ` [PATCH 20/23] ext4: fix MMP claim of a cleanly unmounted filesystem Theodore Ts'o
2026-11-12  4:47 ` [PATCH 21/23] ext4: fix device comparison overflow in ext4_getfsmap_dev_compare() Theodore Ts'o
2026-11-12  4:47 ` [PATCH 22/23] ext4: don't allocate over indirect branches that failed to load Theodore Ts'o
2026-11-12  4:47 ` [PATCH 23/23] ext4: don't stop EXT4_IOC_GET_ES_CACHE at uncached ranges Theodore Ts'o

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=20261112044712.1577296-6-tytso@mit.edu \
    --to=tytso@mit.edu \
    --cc=linux-ext4@vger.kernel.org \
    /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