From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.49 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794765854; b=YZow2LBCjC3yA49JrI1YLMkLKZKHmxMied8C28D541wDF5uHGNnFF+/LvDAbltJLXyOs 8k4DT0yCKPyqDnlLcpaRKewVBpAPCgvQzd9GZILvodfD3xyBLLaJONAe/Fnm9sMN1SFro s38AhtE1z6KnzojvG3I49AWlvMvXl3KAZhHChMLA3Otp0h9u+oxfTnd8ShDs70Uwcb3Yt 3PBbmmYwCUdQ6eMI2CR9Dq/nCXIyg8Gv/q68rliLvtfjiqAHcHc/VOxTvT7YreY09VBOf 3WXSCZL25Xj38v0yleH7SQaunXq9/r9xtyUogImi/o5rv+mQBq563BNtXchtr6RABgQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794765854; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=dt00QcB/UAbcoWfb+Nc1N3msJVe+tAMzrHb6Fo7W5/Y=; b=CBy1dZgl1P51osnBG3VT/Vy2qhHvIXgTQfVTns0wWpONIM3dgoIFCTRBrNXwK7DXxLS8 8ImG0qjDEEQeuD6zROMRk7+anG0lwULCeNgWUIO/u29sZCb5bSs9uYMR0PQxp/LCJocb6 iCu5T84+MKW9pxWH42WzG8olgGLpiOByYHc0WBHL+CVxWKktmssAETWoR4kDOj4KFkqkQ Y4QXCn7gojD9WpEIVzF1i2FvS2eiSZWyRb5ttV02rva+SjVuWAyraDqocKBTJAxoDzfFQ qtNglLwWBVIXTHIGFdMMVpFCG/QwBVHiKpaJZwNK+3Wi9/e3UipmmTl7XCQJDrJlqQw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=gudTlvRy; arc=none smtp.remote-ip=209.85.221.49 Received: from mail-wr1-f49.google.com (mail-wr1-f49.google.com [209.85.221.49]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2A09F60A5BCE for ; Sun, 15 Nov 2026 18:04:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2A09F60A5BCE Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=gudTlvRy Received: by mail-wr1-f49.google.com with SMTP id ffacd0b85a97d-48dee593f18so2930367f8f.3 for ; Sun, 15 Nov 2026 10:04:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1794765852; x=1795370652; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=dt00QcB/UAbcoWfb+Nc1N3msJVe+tAMzrHb6Fo7W5/Y=; b=gudTlvRybSJy4KLmYFZtNr9ekZ8L2alX0H3pXn05eEH3Y71gowCbfqSK14PUokehUu 4BxfuuQukOHh4LKpkMI/IwaZELD8tsUAHTcYi5wyozwdkClCAzu8+dxdoC5rcEJEm7Bk ZS3WkN+bFmWrei1bYBQWwxIF0tQzF7lyoi5dAnRHKCO7O43Kl9edafYh5X5xlKQTA6Fr I7nyTa4AMxp1KV7GhugUFMg7CipLxXlEcJvUGt/khnJSi70VSYQm0pZTCNUG7Xd7EoAo NOY3cQW0eZ6EPN9T8M9IbWSSCNdWpIzlDs1+JH18HnaoSqfd+dMRGd444+JAqRIqyI2q 7gjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794765852; x=1795370652; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=dt00QcB/UAbcoWfb+Nc1N3msJVe+tAMzrHb6Fo7W5/Y=; b=l/gdCjmNCRWwo03xKA6/5Ft2MWlTJhFsQF+SW7sHEXUbAo5HnmSRJmfrJaL5bFc9S/ 18G8eGCzgir+LbveE2tPU1ddF2xCbLTbrEZeKMufBHqzQF65z4TvzIq8kcsv5AXxMtNQ 79qtKJuaoTv9akesVIrZFZL+Wk3U4KQq4/LnU7JTpI93iKy4okxRgCpoMgzFKk/GqooG cP8ykfqb1YZzGm7cuT8Atk8lf2UL7jvnUDZ6oZEcddr7I+oEiiq/NHiKTsRXNFgwfiv8 hgXTyyHCXWjaHitoUGYT5FLSmZ59FEGjo9cx/uGuX3L3jS7doIiaujijpIq9d0IaogdO NfbA== X-Forwarded-Encrypted: i=1; AL+hErGVpN6W2671t0QJR3ZU/nG8RdiL7zAxVEbc5+Odp3IvrTcWI9kv1j3KHux8fB7jCCgvjBHyfye7XmoPDng=@vger.kernel.org X-Gm-Message-State: APuU4Z8ZeIkSKi8O1sfEaNVosDAjRpgKKeI1ZB2QZ60lk5TueNX7reo4 LH7ebUxB3zf/aYgzc+uCutXgzoesm3hnduUATs7C94Rj1kIQvz0J16S5 X-Gm-Gg: AYBFou3thT6DD7NvqPEDJxaqlfVXy5VRXQpXWTsCZEUBZXApNKwI/hE9Xf/uSSb/GSc Oa0eRrcr5K5VQwgIUOdpYXzz3gBIunHFJhPr3PsROJ+JFMBEpNoOsbv2CMx6jwBzHC/Zz5GkmsO qehBGeC205DpOLYi7EAeYtBJYL1Zl0xFYsNxsbi9AOF2pVMU+3kRtE/DG1LMn1YenPcFzAs9Rat Vbmbns3UR9jQqvxyPCu90qutrXIIwZ6VC/J17rsIQuT8RhtpYa7BPYD+EO2UjB+tGkJWLh1QFow uWyGyIMLC93Vsuo3NYKZP3V4TqqlNKOXUFm22u784/8c5zeWmunszrPrxLlFop2W1nmUcaW1O/V H5rnme+j9EYkW2ltmOxMTrgvuiU5DrvS7oUbDrjgY8Dh13CHh0ZSxc0vMyDYxymh+9JsTGCfF8d SUFapRhms5Hcz1IPO2MJw9GbD1oVRRb/8zoGqmV0suapqy9usXK6kublQOiGUePIR2/LKPAkHIx eG2PhWCqZ+5uO9dOreG7p5+KntJ5ZJRDFcjxmmOdczpV1JYYqH1qeLTgOuKARCKZkmiMLJItnVy 7bWWFJzIALD8dr4KNQvQsbrb X-Received: by 2002:a05:6000:4022:b0:49e:1601:2fb1 with SMTP id ffacd0b85a97d-49e1601310bmr10387323f8f.24.1794765852233; Sun, 15 Nov 2026 10:04:12 -0800 (PST) Received: from infinity ([2001:b07:5d26:7a6a:365a:60ff:fe0d:cfc6]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-49c60f8c248sm17980448f8f.58.2026.11.15.10.04.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Nov 2026 10:04:11 -0800 (PST) From: koraynilay To: David Sterba , Namjae Jeon , Sungjong Seo , Alexander Viro , Christian Brauner , Trond Myklebust , Anna Schumaker , Chuck Lever , Jeff Layton , Konstantin Komarov , Paulo Alcantara Cc: Chris Mason , Yuezhang Mo , Jan Kara , NeilBrown , Olga Kornievskaia , Dai Ngo , Tom Talpey , Ronnie Sahlberg , Shyam Prasad N , Bharath SM , Sergey Senozhatsky , ChenXiaoSong , "Darrick J . Wong" , Sean Smith , Andrey Albershteyn , Pedro Falcato , Amir Goldstein , =?UTF-8?q?Pali=20Roh=C3=A1r?= , linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, exfat@lists.linux.dev, ntfs3@lists.linux.dev, linux-cifs@vger.kernel.org, linux-nfs@vger.kernel.org, linux-api@vger.kernel.org, koraynilay Subject: [RFC PATCH v4 06/10] vfs: add __vfs_fileattr_set_locked() for inode_lock()'d callers Date: Sun, 15 Nov 2026 19:03:39 +0100 Message-ID: <20261115180343.576773-7-koray.fra@gmail.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261115180343.576773-1-koray.fra@gmail.com> References: <20261115180343.576773-1-koray.fra@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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); int ioctl_getflags(struct file *file, unsigned int __user *argp); int ioctl_setflags(struct file *file, unsigned int __user *argp); int ioctl_fsgetxattr(struct file *file, void __user *argp); -- 2.56.0