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=1796044965; b=ahrGLhBSYT1VSUZ2bDl1WzNkW9+NgjFxfXqQ8Qzb23+v2WmjeHX0w5BEGXeTIVWZEomO BFSSYKn9RYCinkx90EkJZTVXdjTadiskkQXUa4+riBEjh4pKEf8duIzuX312N4JQiZM9c Z3yeV6yKZCMvCOz0vkrvDJPrTKzbYgoCan0vfTgDBo+YYgYgNTonVHvEET0tJSD9Dq4NH MhikmXdg2QVX3mc4P1DiZOwCHMNhKLJok3kVHNqV0fxxYyumlDqhMyZ0BgdORMKrq8bCk XdS1xlpZe4R3RuTubpuytEbWczbW9fAFPy0dtQsxuCaQBtymZDfhW31EVM2hRJmbF3g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796044965; 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=Yhci3mM5GY8nSviGN2dNKe9Z3mhIsq/7vplKLl79Egg=; b=S/wG0NFMdIQ2o9U3qMtwpuVFVknBCB56MSE5kzZHCnOCSNcuC+2rZanWY2hRmu0wWLP+ TvNhdaoTdkKU+VD3a1dcABGUpzr4X4gH8FMsaj+5ZINnXIRM2PTvyhc+EdguRqHIYZp7Q GznoZsL891sq/3sfYRdEqWo77FNt02QjYyCZKx6F9dzJZjPheMfOfTxQ8X8/2C5mUkJhb Qj1pri2xpk5oqEjjRv9bOFKcCSK9fnkQkC2oGez2/9iPPDLaJN6T/p1iEZM45tk2W5voj 6lC7IuEnquJYA2R/wvSfNjfU9PKAW8LLgsSFv9nBjtkGQgXsyLmICQTCgLqEVXs94IQ== 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=DMdpYLHw; 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 DC179606A32C; Mon, 30 Nov 2026 13:22:45 +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=DMdpYLHw Received: by smtp.kernel.org (Postfix) with ESMTPSA id CB6A51F00919; Mon, 30 Nov 2026 13:22:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796044965; bh=Yhci3mM5GY8nSviGN2dNKe9Z3mhIsq/7vplKLl79Egg=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=DMdpYLHwgViNeFnX9NP0YBygidpMn6+Xh5n6DPNOSU0xhdjWDrhvmCJKB/+pkkCsr GAzGAymegBduW9R+p/Yh7BkiddLhmbpDTXJ3hdUAadjbngRh+3GRP3ypI9REgms6hX j3jC7PkmmX99x9RbgcsiWEotRTUFO5KB4QP6vdgiayoWO3C+T4n24uvn7hkHrO8mW5 14J8ElDGf4QqiozewsObFB/cNqQUgJllPaFiFgsuXQhQLZ9PFcYlThHU/qd/LJs27z QlpTbXR2dWeo43xA/BtbS8FsJsBPlbnwzsiPDmys8yxjfFqc/i3LEYmlkzp3Zll73c QHWk31/HkZS0g== From: Christian Brauner Date: Mon, 30 Nov 2026 14:22:16 +0100 Subject: [PATCH 4/4] ext4: don't fail FS_IOC_FSSETXATTR 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-4-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=9476; i=brauner@kernel.org; h=from:subject:message-id; bh=mFnsaKvjMK1Z6UkfJw0TQYAJs1/QNqIx6ZLTNOn7edE=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWTzVkyX31PwstFzdmjK5l++U/dYpB5mVBGX+vsmlMUho Llja0xsRykLgxgXg6yYIotDu0m43HKeis1GmRowc1iZQIYwcHEKwERMpzD8U25IW+8TaTvtZoxW 9aOtPd+WlwvdO/ruyMV77C8i307s287wz+7rKoFf7FXLYnx1tbW+pjREOrpltgTqRASZqVcFa6x mBgA= X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 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) --- 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