Re: [RFC PATCH v4 06/10] vfs: add __vfs_fileattr_set_locked() for inode_lock()'d callers

From: koraynilay

Date: Mon Nov 16 2026 - 10:55:20 EST


On Mon Nov 16, 2026 at 2:52 PM CET, Amir Goldstein wrote:
> On Sun, Nov 15, 2026 at 7:04 PM koraynilay <koray.fra@xxxxxxxxx> wrote:
>>
>> Currently there is no way for a caller to invoke vfs_fileattr_set()
>> while holding an inode lock without having the kernel go into deadlock.
>>
>> Fix this by moving the vfs_fileattr_set() logic into
>> __vfs_fileattr_set_locked() and making vfs_fileattr_set() a simple
>> wrapper that only handles the inode lock.
>>
>> Assisted-by: Gemini:3.8-flash-high antigravity-cli-1.5.7
>> Signed-off-by: koraynilay <koray.fra@xxxxxxxxx>
>> ---
>> fs/file_attr.c | 139 +++++++++++++++++++++++----------------
>> include/linux/fileattr.h | 2 +
>> 2 files changed, 86 insertions(+), 55 deletions(-)
>>
>> diff --git a/fs/file_attr.c b/fs/file_attr.c
>> index 12610fb6a751..063c81fbfa6a 100644
>> --- a/fs/file_attr.c
>> +++ b/fs/file_attr.c
>> @@ -288,6 +288,89 @@ static int fileattr_set_prepare(struct inode *inode,
>> return 0;
>> }
>>
>> +/**
>> + * __vfs_fileattr_set_locked - change miscellaneous file attributes while
>> + * holding the inode lock
>> + *
>> + * @idmap: idmap of the mount
>> + * @dentry: the object to change
>> + * @fa: fileattr pointer
>> + *
>> + * Same as vfs_fileattr_set(), but caller must hold the inode lock.
>> + *
>> + * Return: 0 on success, or a negative error on failure.
>> + */
>> +int __vfs_fileattr_set_locked(const struct mnt_idmap *idmap, struct dentry *dentry,
>> + struct file_kattr *fa)
>> +{
>> + struct inode *inode = d_inode(dentry);
>> + struct file_kattr old_ma = {};
>> + int err;
>> +
>> + if (!inode->i_op->fileattr_set)
>> + return -ENOIOCTLCMD;
>> +
>> + if (!inode_owner_or_capable(idmap, inode))
>> + return -EPERM;
>> +
>> + err = vfs_fileattr_get(dentry, &old_ma);
>> + if (err)
>> + return err;
>> +
>> + /* initialize missing bits from old_ma */
>> + if (fa->flags_valid) {
>> + /* FS_IOC_SETFLAGS */
>> + fa->fsx_xflags |= (old_ma.fsx_xflags & FS_XFLAGS_MASK) &
>> + ~FS_XFLAG_COMMON;
>> + fa->fsx_extsize = old_ma.fsx_extsize;
>> + fa->fsx_nextents = old_ma.fsx_nextents;
>> + fa->fsx_projid = old_ma.fsx_projid;
>> + fa->fsx_cowextsize = old_ma.fsx_cowextsize;
>> + fa->fsx_ptime = old_ma.fsx_ptime;
>> + } else {
>> + /*
>> + * Preserve unmasked xflags when running file_setattr()
>> + * with a mask, which doesn't need a read-modify-write
>> + * so these attributes would be set to 0.
>> + *
>> + * With FS_IOC_FSSETXATTR or mask == 0 (legacy mode)
>> + * this is not needed as userspace does
>> + * read-modify-write already.
>> + */
>> + if (fa->fsx_xflags_mask) {
>> + fa->fsx_xflags |= old_ma.fsx_xflags &
>> + ~fa->fsx_xflags_mask;
>> + fileattr_fill_xflags(fa, fa->fsx_xflags &
>> + ~FS_XFLAG_RDONLY_MASK);
>> + if (!(fa->fsx_xflags_mask & FS_XFLAG_EXTSIZE))
>> + fa->fsx_extsize = old_ma.fsx_extsize;
>> + if (!(fa->fsx_xflags_mask & FS_XFLAG_PROJID))
>> + fa->fsx_projid = old_ma.fsx_projid;
>> + if (!(fa->fsx_xflags_mask & FS_XFLAG_COWEXTSIZE))
>> + fa->fsx_cowextsize = old_ma.fsx_cowextsize;
>> + }
>> + fa->flags |= old_ma.flags & ~FS_COMMON_FL;
>> +
>> + /* xflags not present in fsxattr */
>> + if (!(fa->fsx_xflags_mask & FS_XFLAG_PTIME))
>> + fa->fsx_ptime = old_ma.fsx_ptime;
>> + }
>> +
>> + err = fileattr_set_prepare(inode, &old_ma, fa);
>> + if (err)
>> + return err;
>> + err = security_inode_file_setattr(dentry, fa);
>> + if (err)
>> + return err;
>> + err = inode->i_op->fileattr_set(idmap, dentry, fa);
>> + if (err)
>> + return err;
>> + fsnotify_xattr(dentry);
>> +
>> + return 0;
>> +}
>> +EXPORT_SYMBOL(__vfs_fileattr_set_locked);
>> +
>> /**
>> * vfs_fileattr_set - change miscellaneous file attributes
>> * @idmap: idmap of the mount
>> @@ -308,7 +391,6 @@ int vfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry,
>> struct file_kattr *fa)
>> {
>> struct inode *inode = d_inode(dentry);
>> - struct file_kattr old_ma = {};
>> int err;
>>
>> if (!inode->i_op->fileattr_set)
>> @@ -318,60 +400,7 @@ int vfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry,
>> return -EPERM;
>>
>> inode_lock(inode);
>> - err = vfs_fileattr_get(dentry, &old_ma);
>> - if (!err) {
>> - /* initialize missing bits from old_ma */
>> - if (fa->flags_valid) {
>> - /* FS_IOC_SETFLAGS */
>> - fa->fsx_xflags |= (old_ma.fsx_xflags & FS_XFLAGS_MASK) &
>> - ~FS_XFLAG_COMMON;
>> - fa->fsx_extsize = old_ma.fsx_extsize;
>> - fa->fsx_nextents = old_ma.fsx_nextents;
>> - fa->fsx_projid = old_ma.fsx_projid;
>> - fa->fsx_cowextsize = old_ma.fsx_cowextsize;
>> - fa->fsx_ptime = old_ma.fsx_ptime;
>> - } else {
>> - /*
>> - * Preserve unmasked xflags when running file_setattr()
>> - * with a mask, which doesn't need a read-modify-write
>> - * so these attributes would be set to 0.
>> - *
>> - * With FS_IOC_FSSETXATTR or mask == 0 (legacy mode)
>> - * this is not needed as userspace does
>> - * read-modify-write already.
>> - */
>> - if (fa->fsx_xflags_mask) {
>> - fa->fsx_xflags |= old_ma.fsx_xflags &
>> - ~fa->fsx_xflags_mask;
>> - fileattr_fill_xflags(fa, fa->fsx_xflags &
>> - ~FS_XFLAG_RDONLY_MASK);
>> - if (!(fa->fsx_xflags_mask & FS_XFLAG_EXTSIZE))
>> - fa->fsx_extsize = old_ma.fsx_extsize;
>> - if (!(fa->fsx_xflags_mask & FS_XFLAG_PROJID))
>> - fa->fsx_projid = old_ma.fsx_projid;
>> - if (!(fa->fsx_xflags_mask & FS_XFLAG_COWEXTSIZE))
>> - fa->fsx_cowextsize = old_ma.fsx_cowextsize;
>> - }
>> - fa->flags |= old_ma.flags & ~FS_COMMON_FL;
>> -
>> - /* xflags not present in fsxattr */
>> - if (!(fa->fsx_xflags_mask & FS_XFLAG_PTIME))
>> - fa->fsx_ptime = old_ma.fsx_ptime;
>> - }
>> -
>> - err = fileattr_set_prepare(inode, &old_ma, fa);
>> - if (err)
>> - goto out;
>> - err = security_inode_file_setattr(dentry, fa);
>> - if (err)
>> - goto out;
>> - err = inode->i_op->fileattr_set(idmap, dentry, fa);
>> - if (err)
>> - goto out;
>> - fsnotify_xattr(dentry);
>> - }
>> -
>> -out:
>> + err = __vfs_fileattr_set_locked(idmap, dentry, fa);
>> inode_unlock(inode);
>> return err;
>> }
>> diff --git a/include/linux/fileattr.h b/include/linux/fileattr.h
>> index ba7ed3b387d8..7ff62ee27e58 100644
>> --- a/include/linux/fileattr.h
>> +++ b/include/linux/fileattr.h
>> @@ -97,6 +97,8 @@ static inline bool fileattr_has_fsx(const struct file_kattr *fa)
>> int vfs_fileattr_get(struct dentry *dentry, struct file_kattr *fa);
>> int vfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry,
>> struct file_kattr *fa);
>> +int __vfs_fileattr_set_locked(const struct mnt_idmap *idmap, struct dentry *dentry,
>> + struct file_kattr *fa);
>
> There is no reason for __ prefix here and I think it should be discouraged for
> global scope if possible.

Ah ok, I copied what had been done for __vfs_setxattr_locked() /
vfs_setxattr() and __vfs_removexattr_locked() / vfs_removexattr(), but I
agree that __ makes it more "internal", I'll change it for v5.

Thanks,
koraynilay

> Thanks,
> Amir.


Attachment: signature.asc
Description: PGP signature