Linux Kernel Selftest development
 help / color / mirror / Atom feed
From: Jimmy Zuber <jamz@amazon.com>
To: Miklos Szeredi <miklos@szeredi.hu>
Cc: <fuse-devel@lists.linux.dev>, <linux-kselftest@vger.kernel.org>,
	"Shuah Khan" <shuah@kernel.org>,
	Bernd Schubert <bernd@bsbernd.com>,
	Jim Harris <jiharris@nvidia.com>,
	Horst Birthelmer <horst@birthelmer.de>,
	Luis Henriques <luis@igalia.com>, NeilBrown <neil@brown.name>,
	Joanne Koong <joannelkoong@gmail.com>
Subject: [PATCH v9 2/3] fuse: add FUSE_OPEN_ATOMIC
Date: Sat, 21 Nov 2026 00:16:07 +0000	[thread overview]
Message-ID: <20261121001608.996811-3-jamz@amazon.com> (raw)
In-Reply-To: <20261121001608.996811-1-jamz@amazon.com>

Add FUSE_OPEN_ATOMIC, a request that combines FUSE_LOOKUP with FUSE_OPEN
or FUSE_CREATE. With this patch, the kernel sends it from
->atomic_open(), so that an open() of a name that is not in the dcache
needs one request instead of two. Returning -ENOSYS triggers fallback to
the old behavior, starting with the open() that received it.

This patch defines the request as struct fuse_open_atomic_in followed by
the name and, with O_CREAT, the extensions of FUSE_CREATE. The request
carries per-request flags (atomic_flags). A server must ignore
atomic_flags bits that it does not implement and must return the bits it
honored in the reply. This patch defines the reply as the fixed-size
struct fuse_open_atomic_out: the entry with its attributes as struct
fuse_statx, the flags FUSE_OPEN_ATOMIC_OPENED and
FUSE_OPEN_ATOMIC_CREATED, and fuse_open_out.

For an existing regular file, the server must either open it or return
only the entry, and for any other file type it must return only the
entry. For a missing name, the server must create and open a regular
file with O_CREAT, and return a zero nodeid or -ENOENT without O_CREAT.
With this patch, the kernel returns -EIO for any other reply.

With this patch, the VFS checks permissions after the reply, as it does
on other filesystems with ->atomic_open(), so the server can see
FUSE_OPEN_ATOMIC and FUSE_RELEASE for a file that open() then refuses.
The kernel clears O_TRUNC in the request and truncates with FUSE_SETATTR
after the permission check. An open without FOPEN_KEEP_CACHE drops the
cached pages when the reply arrives, as FUSE_CREATE does.

Not supported: O_DIRECTORY, O_EXCL without O_CREAT, O_TRUNC on a server
with FUSE_ATOMIC_O_TRUNC, and servers in no-open mode. These opens fall
back to FUSE_LOOKUP, FUSE_OPEN and FUSE_CREATE.

If the server opens an existing DAX file, the kernel releases it and
opens it again with FUSE_OPEN, which handles DAX.

Signed-off-by: Jimmy Zuber <jamz@amazon.com>
---
 fs/fuse/dir.c             | 281 ++++++++++++++++++++++++++++++++++++++
 fs/fuse/fuse_i.h          |   3 +
 fs/fuse/fuse_trace.h      |   1 +
 include/uapi/linux/fuse.h |  75 +++++++++-
 4 files changed, 359 insertions(+), 1 deletion(-)

diff --git a/fs/fuse/dir.c b/fs/fuse/dir.c
index a719e3516e187..9c6b5fb709cb8 100644
--- a/fs/fuse/dir.c
+++ b/fs/fuse/dir.c
@@ -955,6 +955,280 @@ static void fuse_statx_to_attr(struct fuse_statx *sx, struct fuse_attr *attr)
 	attr->blksize = sx->blksize;
 }
 
+/* Release the server's open of a reply that the kernel drops, or free @ff */
+static void fuse_open_atomic_drop(struct fuse_file *ff,
+				  struct fuse_open_atomic_out *outarg,
+				  unsigned int flags)
+{
+	if (!(outarg->flags & FUSE_OPEN_ATOMIC_OPENED)) {
+		fuse_file_free(ff);
+		return;
+	}
+	ff->fh = outarg->open.fh;
+	ff->nodeid = outarg->nodeid;
+	fuse_sync_release(NULL, ff, flags & ~(O_CREAT | O_EXCL));
+}
+
+static bool fuse_open_atomic_valid(const struct fuse_open_atomic_in *inarg,
+				   struct fuse_open_atomic_out *outarg,
+				   struct fuse_attr *attr, unsigned int flags)
+{
+	bool opened = outarg->flags & FUSE_OPEN_ATOMIC_OPENED;
+	bool created = outarg->flags & FUSE_OPEN_ATOMIC_CREATED;
+
+	if ((outarg->flags &
+	     ~(FUSE_OPEN_ATOMIC_OPENED | FUSE_OPEN_ATOMIC_CREATED)) ||
+	    outarg->padding || outarg->reserved)
+		return false;
+	if (outarg->atomic_flags & ~inarg->atomic_flags)
+		return false;
+	if (created && (!opened || !(flags & O_CREAT)))
+		return false;
+	/* O_EXCL: the server creates the file or returns -EEXIST */
+	if ((flags & O_EXCL) && !created)
+		return false;
+	/* A zero nodeid: the name does not exist */
+	if (!outarg->nodeid)
+		return !opened && !(flags & O_CREAT);
+	if (invalid_nodeid(outarg->nodeid) || fuse_invalid_attr(attr) ||
+	    (outarg->stat.mask & STATX_BASIC_STATS) != STATX_BASIC_STATS)
+		return false;
+	return !opened || S_ISREG(attr->mode);
+}
+
+static int fuse_open_atomic_send(const struct mnt_idmap *idmap,
+				 struct inode *dir, struct dentry *entry,
+				 unsigned int flags, umode_t mode,
+				 struct fuse_open_atomic_in *inarg,
+				 struct fuse_open_atomic_out *outarg)
+{
+	struct fuse_mount *fm = get_fuse_mount(dir);
+	FUSE_ARGS(args);
+	bool locked = false;
+	int err;
+
+	/* The VFS truncates after may_open() */
+	inarg->flags = flags & ~(O_NOCTTY | O_TRUNC | __O_REGULAR);
+	inarg->mode = mode;
+	inarg->umask = current_umask();
+	inarg->sx_mask = STATX_BASIC_STATS;
+	args.opcode = FUSE_OPEN_ATOMIC;
+	args.nodeid = get_node_id(dir);
+	args.in_numargs = 2;
+	args.in_args[0].size = sizeof(*inarg);
+	args.in_args[0].value = inarg;
+	args.in_args[1].size = entry->d_name.len + 1;
+	args.in_args[1].value = entry->d_name.name;
+	args.out_numargs = 1;
+	args.out_args[0].size = sizeof(*outarg);
+	args.out_args[0].value = outarg;
+
+	if (flags & O_CREAT) {
+		err = get_create_ext(idmap, &args, dir, entry, mode);
+		if (err)
+			return err;
+	} else {
+		/* Locked as for FUSE_LOOKUP */
+		idmap = &invalid_mnt_idmap;
+		locked = fuse_lock_inode(dir);
+	}
+	err = fuse_simple_idmap_request(idmap, fm, &args);
+	fuse_unlock_inode(dir, locked);
+	free_ext_value(&args);
+	return err;
+}
+
+/* Instantiate @entry with @inode, without opening it */
+static int fuse_open_atomic_no_open(struct file *file, struct dentry *entry,
+				    struct inode *inode, int epoch,
+				    struct fuse_open_atomic_out *outarg)
+{
+	/*
+	 * @entry can be a hashed negative dentry (O_CREAT on a cached
+	 * name), which d_splice_alias() accepts.
+	 */
+	struct dentry *res = d_splice_alias(inode, entry);
+	u64 time = fuse_time_to_jiffies(outarg->entry_valid,
+					outarg->entry_valid_nsec);
+
+	if (!IS_ERR(res)) {
+		fuse_dentry_set_epoch(res ?: entry, epoch);
+		fuse_dentry_settime(res ?: entry, time);
+	}
+	return finish_no_open(file, res);
+}
+
+/* Open @file with the server's open from @outarg */
+static int fuse_open_atomic_finish(struct file *file, struct dentry *entry,
+				   struct inode *inode, int epoch,
+				   struct fuse_file *ff,
+				   struct fuse_open_atomic_out *outarg,
+				   unsigned int flags)
+{
+	u64 time = fuse_time_to_jiffies(outarg->entry_valid,
+					outarg->entry_valid_nsec);
+	int err;
+
+	if (outarg->flags & FUSE_OPEN_ATOMIC_CREATED)
+		file->f_mode |= FMODE_CREATED;
+	/* Stored for fuse_finish_open() */
+	ff->args->open_outarg = outarg->open;
+	ff->fh = outarg->open.fh;
+	ff->open_flags = outarg->open.open_flags;
+	ff->nodeid = outarg->nodeid;
+	/* NULL for a regular file */
+	d_splice_alias(inode, entry);
+	fuse_dentry_set_epoch(entry, epoch);
+	fuse_dentry_settime(entry, time);
+
+	err = generic_file_open(inode, file);
+	if (!err) {
+		file->private_data = ff;
+		err = finish_open(file, entry, fuse_finish_open);
+	}
+	if (!err) {
+		if (!(ff->open_flags & FOPEN_KEEP_CACHE))
+			invalidate_inode_pages2(inode->i_mapping);
+	} else if (!(file->f_mode & FMODE_OPENED)) {
+		/* fput() releases a file that finish_open() opened */
+		fuse_sync_release(get_fuse_inode(inode), ff,
+				  flags & ~(O_CREAT | O_EXCL));
+	}
+	return err;
+}
+
+/*
+ * Send FUSE_LOOKUP plus FUSE_OPEN or FUSE_CREATE as one FUSE_OPEN_ATOMIC
+ * request.  The VFS checks permissions after the reply.  Returns 1 after
+ * -ENOSYS; the caller then uses FUSE_LOOKUP.
+ */
+static int fuse_open_atomic(const struct mnt_idmap *idmap, struct inode *dir,
+			    struct dentry *entry, struct file *file,
+			    unsigned int flags, umode_t mode)
+{
+	struct fuse_mount *fm = get_fuse_mount(dir);
+	struct fuse_conn *fc = fm->fc;
+	struct fuse_open_atomic_in inarg = {};
+	struct fuse_open_atomic_out outarg = {};
+	struct fuse_forget_link *forget;
+	struct inode *inode = NULL;
+	struct fuse_attr attr;
+	u64 attr_version, evict_ctr;
+	struct fuse_file *ff;
+	bool created;
+	int epoch, err;
+
+	if (entry->d_name.len > fc->name_max)
+		return -ENAMETOOLONG;
+	forget = fuse_alloc_forget();
+	ff = fuse_file_alloc(fm, true);
+	err = -ENOMEM;
+	if (!forget || !ff)
+		goto out_free;
+	if (!fc->dont_mask)
+		mode &= ~current_umask();
+
+	epoch = atomic_read(&fc->epoch);
+	attr_version = fuse_get_attr_version(fc);
+	evict_ctr = fuse_get_evict_ctr(fc);
+	err = fuse_open_atomic_send(idmap, dir, entry, flags, mode, &inarg,
+				    &outarg);
+	if (err == -ENOSYS) {
+		fc->no_open_atomic = 1;
+		err = 1;
+	} else if (err == -EEXIST) {
+		fuse_invalidate_entry(entry);
+	}
+	if (err)
+		goto out_free;
+
+	fuse_statx_to_attr(&outarg.stat, &attr);
+	attr.flags = outarg.attr_flags;
+	err = -EIO;
+	if (!fuse_open_atomic_valid(&inarg, &outarg, &attr, flags))
+		goto out_reject;
+	created = outarg.flags & FUSE_OPEN_ATOMIC_CREATED;
+	if (created) {
+		/* As for FUSE_CREATE: a new file has current attributes */
+		attr_version = 0;
+		evict_ctr = 0;
+		fuse_dir_changed(dir);
+	}
+	if (outarg.nodeid) {
+		inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
+				  &attr, ATTR_TIMEOUT(&outarg), attr_version,
+				  evict_ctr);
+		err = -ENOMEM;
+		if (!inode)
+			goto out_reject;
+		kfree(forget);
+		forget = NULL;
+		err = -EIO;
+		if (fuse_is_bad(inode))
+			goto out_reject;
+		fuse_advise_use_readdirplus(dir);
+	}
+
+	/* An existing DAX file opens in ->open() */
+	if (!(outarg.flags & FUSE_OPEN_ATOMIC_OPENED) ||
+	    (!created && IS_DAX(inode))) {
+		kfree(forget);
+		fuse_open_atomic_drop(ff, &outarg, inarg.flags);
+		return fuse_open_atomic_no_open(file, entry, inode, epoch,
+						&outarg);
+	}
+	return fuse_open_atomic_finish(file, entry, inode, epoch, ff, &outarg,
+				       inarg.flags);
+
+out_reject:
+	fuse_open_atomic_drop(ff, &outarg, inarg.flags);
+	/* The server can have created the name: drop it and the dir attrs */
+	fuse_invalidate_entry(entry);
+	if (flags & O_CREAT)
+		fuse_dir_changed(dir);
+	iput(inode);
+	if (forget && outarg.nodeid)
+		fuse_chan_queue_forget(fc->chan, forget, outarg.nodeid, 1);
+	else
+		kfree(forget);
+	return err;
+
+out_free:
+	if (ff)
+		fuse_file_free(ff);
+	kfree(forget);
+	return err;
+}
+
+static bool fuse_use_open_atomic(struct fuse_conn *fc, struct dentry *entry,
+				 struct file *file, unsigned int flags)
+{
+	if (fc->no_open_atomic || fc->no_open)
+		return false;
+
+	/* With atomic_o_trunc, ->open() truncates, after may_open() */
+	if ((file->f_flags & O_TRUNC) && fc->atomic_o_trunc)
+		return false;
+
+	if (flags & O_CREAT)
+		return true;
+
+	/* Without O_CREAT, a dentry not in lookup is a valid negative dentry */
+	if (!d_in_lookup(entry))
+		return false;
+
+	/*
+	 * O_EXCL without O_CREAT can mean the VFS cleared O_CREAT: with
+	 * FUSE_LOOKUP, do_open() fails with -EEXIST and the server sees only
+	 * FUSE_LOOKUP.
+	 */
+	if (flags & O_EXCL)
+		return false;
+
+	/* The server opens only regular files in FUSE_OPEN_ATOMIC */
+	return !(flags & O_DIRECTORY);
+}
+
 static int fuse_mknod(const struct mnt_idmap *, struct inode *, struct dentry *,
 		      umode_t, dev_t);
 static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
@@ -968,6 +1242,13 @@ static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
 	if (fuse_is_bad(dir))
 		return -EIO;
 
+	if (fuse_use_open_atomic(fc, entry, file, flags)) {
+		err = fuse_open_atomic(idmap, dir, entry, file, flags,
+				       mode | S_IFREG);
+		if (err <= 0)
+			return err;
+	}
+
 	if (d_in_lookup(entry)) {
 		struct dentry *res = fuse_lookup(dir, entry, 0);
 		if (res || d_really_is_positive(entry))
diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h
index a033b0bc45762..abe24ebb6325b 100644
--- a/fs/fuse/fuse_i.h
+++ b/fs/fuse/fuse_i.h
@@ -618,6 +618,9 @@ struct fuse_conn {
 	/** @no_create: Is create not implemented by fs? */
 	unsigned no_create:1;
 
+	/** @no_open_atomic: Is FUSE_OPEN_ATOMIC not implemented by fs? */
+	unsigned no_open_atomic:1;
+
 	/** @no_bmap: Is bmap not implemented by fs? */
 	unsigned no_bmap:1;
 
diff --git a/fs/fuse/fuse_trace.h b/fs/fuse/fuse_trace.h
index 60baa10bbcb99..54d7a1891c382 100644
--- a/fs/fuse/fuse_trace.h
+++ b/fs/fuse/fuse_trace.h
@@ -58,6 +58,7 @@
 	EM( FUSE_SYNCFS,		"FUSE_SYNCFS")		\
 	EM( FUSE_TMPFILE,		"FUSE_TMPFILE")		\
 	EM( FUSE_STATX,			"FUSE_STATX")		\
+	EM( FUSE_OPEN_ATOMIC,		"FUSE_OPEN_ATOMIC")	\
 	EMe(CUSE_INIT,			"CUSE_INIT")
 
 /*
diff --git a/include/uapi/linux/fuse.h b/include/uapi/linux/fuse.h
index 616430a2973c6..c2f0699771c2c 100644
--- a/include/uapi/linux/fuse.h
+++ b/include/uapi/linux/fuse.h
@@ -257,6 +257,9 @@
  *  - add backing_id_64 to fuse_open_out
  *  - add FUSE_NOTIFY_BACKING_MAP, fuse_notify_backing_map_out, fuse_extent
  *  - add FUSE_BACKING_MAP_CREATE, FUSE_BACKING_MAP_CYCLIC
+ *
+ *  7.48
+ *  - add FUSE_OPEN_ATOMIC, fuse_open_atomic_in and fuse_open_atomic_out
  */
 
 #ifndef _LINUX_FUSE_H
@@ -292,7 +295,7 @@
 #define FUSE_KERNEL_VERSION 7
 
 /** Minor version number of this interface */
-#define FUSE_KERNEL_MINOR_VERSION 47
+#define FUSE_KERNEL_MINOR_VERSION 48
 
 /** The node ID of the root inode */
 #define FUSE_ROOT_ID 1
@@ -699,6 +702,12 @@ enum fuse_opcode {
 	FUSE_STATX		= 52,
 	FUSE_COPY_FILE_RANGE_64	= 53,
 
+	/*
+	 * FUSE_OPEN_ATOMIC is FUSE_LOOKUP plus FUSE_OPEN or FUSE_CREATE in one
+	 * request.  See struct fuse_open_atomic_in.
+	 */
+	FUSE_OPEN_ATOMIC	= 54,
+
 	/* CUSE specific operations */
 	CUSE_INIT		= 4096,
 
@@ -848,6 +857,70 @@ struct fuse_open_out {
 	uint64_t	backing_id_64;
 };
 
+/*
+ * FUSE_OPEN_ATOMIC request: fuse_open_atomic_in, then the name.  With
+ * O_CREAT, the extensions of FUSE_CREATE (security context, supplementary
+ * group) follow the name.
+ *
+ * @flags: open(2) flags, without O_TRUNC and O_NOCTTY
+ * @mode, @umask: as in fuse_create_in
+ * @open_flags: 0
+ * @atomic_flags: request variants.  The server must ignore the bits that
+ *	it does not implement, and must set the bits that it honored in the
+ *	atomic_flags of the reply.  A later atomic_flags bit can define a
+ *	longer reply.
+ * @sx_mask: STATX_... attributes that the kernel asks for
+ *
+ * The server must reply with fuse_open_atomic_out as follows:
+ * - O_CREAT | O_EXCL on an existing name: -EEXIST.
+ * - An existing regular file: open it and set FUSE_OPEN_ATOMIC_OPENED, or
+ *   return the entry only.
+ * - An existing file of another type: the entry only.
+ * - A missing name with O_CREAT: create and open a regular file, and set
+ *   FUSE_OPEN_ATOMIC_OPENED and FUSE_OPEN_ATOMIC_CREATED.
+ * - A missing name without O_CREAT: a zero nodeid, or -ENOENT.
+ *
+ * For a nonzero nodeid, @stat must contain at least STATX_BASIC_STATS, and
+ * the reply takes a lookup reference, as for FUSE_LOOKUP.  A file that the
+ * server opens gets FUSE_RELEASE, as for FUSE_CREATE.
+ */
+struct fuse_open_atomic_in {
+	uint32_t	flags;
+	uint32_t	mode;
+	uint32_t	umask;
+	uint32_t	open_flags;	/* FUSE_OPEN_... */
+	uint32_t	atomic_flags;
+	uint32_t	sx_mask;
+	uint64_t	reserved[2];
+};
+
+/*
+ * fuse_open_atomic_out flags
+ * FUSE_OPEN_ATOMIC_OPENED: the server opened the file, and @open is valid
+ * FUSE_OPEN_ATOMIC_CREATED: the server created the file, for O_CREAT
+ * The other bits, padding and reserved in fuse_open_atomic_out must be zero.
+ * @open is fuse_open_out as of protocol 7.48; later fields of fuse_open_out
+ * reach this reply only with an atomic_flags bit.
+ */
+#define FUSE_OPEN_ATOMIC_OPENED		(1 << 0)
+#define FUSE_OPEN_ATOMIC_CREATED	(1 << 1)
+
+struct fuse_open_atomic_out {
+	uint64_t	nodeid;
+	uint64_t	generation;
+	uint64_t	entry_valid;
+	uint64_t	attr_valid;
+	uint32_t	entry_valid_nsec;
+	uint32_t	attr_valid_nsec;
+	uint32_t	flags;		/* FUSE_OPEN_ATOMIC_OPENED, ... */
+	uint32_t	attr_flags;	/* FUSE_ATTR_... */
+	uint32_t	atomic_flags;	/* honored request atomic_flags */
+	uint32_t	padding;
+	uint64_t	reserved;
+	struct fuse_statx stat;
+	struct fuse_open_out open;
+};
+
 struct fuse_release_in {
 	uint64_t	fh;
 	uint32_t	flags;
-- 
2.50.1


  parent reply	other threads:[~2026-11-21  0:17 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-21  0:16 [PATCH v9 0/3] fuse: atomic open Jimmy Zuber
2026-11-21  0:16 ` [PATCH v9 1/3] fuse: move fuse_statx_to_attr() up Jimmy Zuber
2026-11-21  0:16 ` Jimmy Zuber [this message]
2026-11-21  0:16 ` [PATCH v9 3/3] selftests/fuse: add test for FUSE_OPEN_ATOMIC Jimmy Zuber

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=20261121001608.996811-3-jamz@amazon.com \
    --to=jamz@amazon.com \
    --cc=bernd@bsbernd.com \
    --cc=fuse-devel@lists.linux.dev \
    --cc=horst@birthelmer.de \
    --cc=jiharris@nvidia.com \
    --cc=joannelkoong@gmail.com \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=luis@igalia.com \
    --cc=miklos@szeredi.hu \
    --cc=neil@brown.name \
    --cc=shuah@kernel.org \
    /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