Linux Overlay Filesystem development
 help / color / mirror / Atom feed
From: Joanne Koong <joannelkoong@gmail.com>
To: amir73il@gmail.com, miklos@szeredi.hu
Cc: fuse-devel@lists.linux.dev, linux-unionfs@vger.kernel.org
Subject: [PATCH v2 18/21] backing-inode: add backing_inode_setattr()
Date: Fri, 15 May 2026 17:40:01 -0700	[thread overview]
Message-ID: <20260516004004.1455526-19-joannelkoong@gmail.com> (raw)
In-Reply-To: <20260516004004.1455526-1-joannelkoong@gmail.com>

Move logic in ovl_setattr() to a generic backing_inode_setattr() function
in fs/backing-inode.c, which other filesystems that use backing inodes
(eg fuse passthrough) will use.

Signed-off-by: Joanne Koong <joannelkoong@gmail.com>
---
 fs/backing-inode.c            | 54 ++++++++++++++++++++++++++++
 fs/overlayfs/inode.c          | 66 ++++++++++-------------------------
 include/linux/backing-inode.h |  4 +++
 3 files changed, 76 insertions(+), 48 deletions(-)

diff --git a/fs/backing-inode.c b/fs/backing-inode.c
index 474770a1fa9d..e72f278789d8 100644
--- a/fs/backing-inode.c
+++ b/fs/backing-inode.c
@@ -41,3 +41,57 @@ void backing_inode_copyattr(struct inode *inode,
 }
 EXPORT_SYMBOL_GPL(backing_inode_copyattr);
 
+int backing_inode_setattr(struct dentry *dentry,
+			  const struct path *backing_path,
+			  struct iattr *attr, const struct cred *cred)
+{
+	struct dentry *backing_dentry = backing_path->dentry;
+	struct inode *winode = NULL;
+	int err;
+
+	if (attr->ia_valid & ATTR_SIZE) {
+		winode = d_inode(backing_dentry);
+		err = get_write_access(winode);
+		if (err)
+			return err;
+	}
+
+	/*
+	 * Clear ATTR_MODE to avoid BUG_ON in notify_change(), which does not
+	 * allow ATTR_MODE together with ATTR_KILL_SUID/SGID (the first
+	 * notify_change() converted ATTR_KILL_SUID/SGID into ATTR_MODE using
+	 * @dentry's inode mode).
+	 *
+	 * This also ensures notify_change() recomputes the mode from the
+	 * backing inode's current mode, instead of a potentially stale value
+	 * from @dentry's inode.
+	 */
+	if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
+		attr->ia_valid &= ~ATTR_MODE;
+
+	/*
+	 * The filesystem's file is not meaningful to the backing filesystem.
+	 * Clear ATTR_FILE so the backing filesystem does not try to use it.
+	 */
+	attr->ia_valid &= ~ATTR_FILE;
+
+	err = mnt_want_write(backing_path->mnt);
+	if (err)
+		goto out_put_write;
+
+	inode_lock(backing_dentry->d_inode);
+	scoped_with_creds(cred)
+		err = notify_change(mnt_idmap(backing_path->mnt),
+				    backing_dentry, attr, NULL);
+	if (!err)
+		backing_inode_copyattr(dentry->d_inode, backing_path);
+	inode_unlock(backing_dentry->d_inode);
+	mnt_drop_write(backing_path->mnt);
+
+out_put_write:
+	if (winode)
+		put_write_access(winode);
+
+	return err;
+}
+EXPORT_SYMBOL_GPL(backing_inode_setattr);
diff --git a/fs/overlayfs/inode.c b/fs/overlayfs/inode.c
index 00c69707bda9..f1cd67e6e9c8 100644
--- a/fs/overlayfs/inode.c
+++ b/fs/overlayfs/inode.c
@@ -15,16 +15,17 @@
 #include <linux/namei.h>
 #include <linux/posix_acl.h>
 #include <linux/posix_acl_xattr.h>
+#include <linux/backing-inode.h>
 #include "overlayfs.h"
 
 
 int ovl_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
 		struct iattr *attr)
 {
+	struct path backing_path;
 	int err;
 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
 	bool full_copy_up = false;
-	struct dentry *upperdentry;
 
 	err = setattr_prepare(&nop_mnt_idmap, dentry, attr);
 	if (err)
@@ -39,57 +40,26 @@ int ovl_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
 		err = ovl_copy_up(dentry);
 	else
 		err = ovl_copy_up_with_data(dentry);
-	if (!err) {
-		struct inode *winode = NULL;
-
-		upperdentry = ovl_dentry_upper(dentry);
-
-		if (attr->ia_valid & ATTR_SIZE) {
-			winode = d_inode(upperdentry);
-			err = get_write_access(winode);
-			if (err)
-				goto out;
-		}
-
-		if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
-			attr->ia_valid &= ~ATTR_MODE;
 
-		/*
-		 * We might have to translate ovl file into real file object
-		 * once use cases emerge.  For now, simply don't let underlying
-		 * filesystem rely on attr->ia_file
-		 */
-		attr->ia_valid &= ~ATTR_FILE;
-
-		/*
-		 * If open(O_TRUNC) is done, VFS calls ->setattr with ATTR_OPEN
-		 * set.  Overlayfs does not pass O_TRUNC flag to underlying
-		 * filesystem during open -> do not pass ATTR_OPEN.  This
-		 * disables optimization in fuse which assumes open(O_TRUNC)
-		 * already set file size to 0.  But we never passed O_TRUNC to
-		 * fuse.  So by clearing ATTR_OPEN, fuse will be forced to send
-		 * setattr request to server.
-		 */
-		attr->ia_valid &= ~ATTR_OPEN;
+	if (err)
+		return err;
 
-		err = ovl_want_write(dentry);
-		if (err)
-			goto out_put_write;
+	/*
+	 * If open(O_TRUNC) is done, VFS calls ->setattr with ATTR_OPEN
+	 * set.  Overlayfs does not pass O_TRUNC flag to underlying
+	 * filesystem during open -> do not pass ATTR_OPEN.  This
+	 * disables optimization in fuse which assumes open(O_TRUNC)
+	 * already set file size to 0.  But we never passed O_TRUNC to
+	 * fuse.  So by clearing ATTR_OPEN, fuse will be forced to send
+	 * setattr request to server.
+	 */
+	attr->ia_valid &= ~ATTR_OPEN;
 
-		inode_lock(upperdentry->d_inode);
-		with_ovl_creds(dentry->d_sb)
-			err = ovl_do_notify_change(ofs, upperdentry, attr);
-		if (!err)
-			ovl_copyattr(dentry->d_inode);
-		inode_unlock(upperdentry->d_inode);
-		ovl_drop_write(dentry);
+	backing_path.dentry = ovl_dentry_upper(dentry);
+	backing_path.mnt = ovl_upper_mnt(ofs);
 
-out_put_write:
-		if (winode)
-			put_write_access(winode);
-	}
-out:
-	return err;
+	return backing_inode_setattr(dentry, &backing_path, attr,
+				     ovl_creds(dentry->d_sb));
 }
 
 static void ovl_map_dev_ino(struct dentry *dentry, struct kstat *stat, int fsid)
diff --git a/include/linux/backing-inode.h b/include/linux/backing-inode.h
index 6b43cba9fabd..fd02c87e0f99 100644
--- a/include/linux/backing-inode.h
+++ b/include/linux/backing-inode.h
@@ -11,4 +11,8 @@
 void backing_inode_copyattr(struct inode *inode,
 			    const struct path *backing_path);
 
+int backing_inode_setattr(struct dentry *dentry,
+			  const struct path *backing_path,
+			  struct iattr *attr, const struct cred *cred);
+
 #endif /* _LINUX_BACKING_INODE_H */
-- 
2.52.0


  parent reply	other threads:[~2026-05-16  0:53 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-16  0:39 [PATCH v2 00/21] fuse: extend passthrough to inode operations Joanne Koong
2026-05-16  0:39 ` [PATCH v2 01/21] fuse: introduce FUSE_PASSTHROUGH_INO mode Joanne Koong
2026-05-16  0:39 ` [PATCH v2 02/21] fuse: prepare for passthrough of inode operations Joanne Koong
2026-05-16  1:34   ` Joanne Koong
2026-05-16  0:39 ` [PATCH v2 03/21] fuse: prepare for readdir passthrough on directories Joanne Koong
2026-05-16  0:39 ` [PATCH v2 04/21] fuse: implement passthrough for readdir Joanne Koong
2026-05-16  0:39 ` [PATCH v2 05/21] fuse: prepare for long lived reference on backing file Joanne Koong
2026-05-16  0:39 ` [PATCH v2 06/21] fuse: implement passthrough for getattr/statx Joanne Koong
2026-05-16  0:39 ` [PATCH v2 07/21] fuse: prepare to setup backing inode passthrough on lookup Joanne Koong
2026-05-16  0:39 ` [PATCH v2 08/21] fuse: handle zero ops_mask in FUSE_DEV_IOC_BACKING_OPEN Joanne Koong
2026-05-16  0:39 ` [PATCH v2 09/21] fuse: handle partial io passthrough for read/write, splice, and mmap Joanne Koong
2026-05-16  0:39 ` [PATCH v2 10/21] fuse: prepare to cache statx attributes from entry replies Joanne Koong
2026-05-16  0:39 ` [PATCH v2 11/21] fuse: clean up fuse_dentry_revalidate() Joanne Koong
2026-05-16  0:39 ` [PATCH v2 12/21] fuse: add struct fuse_entry2_out and helpers for extended entry replies Joanne Koong
2026-05-16  0:39 ` [PATCH v2 13/21] fuse: add passthrough lookup Joanne Koong
2026-05-16  0:39 ` [PATCH v2 14/21] fuse: add passthrough support for entry creation Joanne Koong
2026-05-16  0:39 ` [PATCH v2 15/21] fuse: add passthrough support for create+open Joanne Koong
2026-05-16  0:39 ` [PATCH v2 16/21] fuse: allow backing_id=0 in open to inherit inode's backing file Joanne Koong
2026-05-16  0:40 ` [PATCH v2 17/21] backing-inode: add backing_inode_copyattr() Joanne Koong
2026-05-16  0:40 ` Joanne Koong [this message]
2026-05-16  0:40 ` [PATCH v2 19/21] fuse: add passthrough setattr Joanne Koong
2026-05-16  1:04   ` Joanne Koong
2026-05-16  0:40 ` [PATCH v2 20/21] fuse: use passthrough getattr in setattr suid/sgid handling Joanne Koong
2026-05-16  1:20   ` Joanne Koong
2026-05-16  0:40 ` [PATCH v2 21/21] docs: fuse: document extended passthrough (FUSE_PASSTHROUGH_INO) Joanne Koong

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=20260516004004.1455526-19-joannelkoong@gmail.com \
    --to=joannelkoong@gmail.com \
    --cc=amir73il@gmail.com \
    --cc=fuse-devel@lists.linux.dev \
    --cc=linux-unionfs@vger.kernel.org \
    --cc=miklos@szeredi.hu \
    /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