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