fuse: atomic open
Subsystems (2)
v9 cover fuse: atomic open
Add FUSE_OPEN_ATOMIC, a request that combines FUSE_LOOKUP with FUSE_OPEN or FUSE_CREATE. With this series, 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:
open() of a name not in the dcache today with this series existing file LOOKUP + OPEN OPEN_ATOMIC O_CREAT, new file LOOKUP + CREATE OPEN_ATOMIC O_CREAT, existing file LOOKUP + OPEN OPEN_ATOMIC missing name LOOKUP OPEN_ATOMIC
The server must handle FUSE_OPEN_ATOMIC as one conditional operation:
open the name or return only its entry if it is a regular file, create and open it for O_CREAT if it is missing, return only the entry for any other file type, and report whether it created the file. The open depends on the nodeid that the lookup finds, and the choice between opening, creating and returning the entry depends on what the server finds, so both steps need to run on the server in one request, as in openat(2) (see also [6] on combined requests). Returning -ENOSYS triggers fallback to the old behavior.
The series gives the request and reply their own structs, as suggested by Bernd Schubert [5]. 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, so that later variants of the request would need no FUSE_INIT flag. The series defines the reply with a fixed size, explicit OPENED and CREATED flags, and the attributes as struct fuse_statx, so that later variants could return attributes that struct fuse_attr lacks.
With this series, if the server opens an existing file, the VFS checks permissions only after the reply, as it does on other filesystems with ->atomic_open(). The kernel then sends FUSE_RELEASE for a file that the server opened and that open() does not return, as it does for FUSE_CREATE. The kernel clears O_TRUNC in the request and truncates with FUSE_SETATTR after the permission check.
Not supported by this series, and unchanged:
- names that are already in the dcache (FUSE_OPEN);
- O_DIRECTORY, O_EXCL without O_CREAT, and O_TRUNC on a server with FUSE_ATOMIC_O_TRUNC (FUSE_LOOKUP, then FUSE_OPEN).
Earlier series also proposed one request for lookup and open in FUSE [1][2], and one for open and getattr [3][4].
I used Claude Code and Sashiko to help write and review the code. I reviewed and tested every change. Tested with a new selftest under UML.
Changes in v9 (from Bernd Schubert's review of v8 [5]):
- Replace fuse_create_in and fuse_entry_out with fuse_open_atomic_in and
fuse_open_atomic_out: per-request atomic_flags, fuse_statx attributes, and OPENED and CREATED reply flags. FOPEN_CREATED becomes FUSE_OPEN_ATOMIC_CREATED.
- Require the full reply size.
- A server must ignore atomic_flags bits that it does not implement and must report the bits it honored in the reply.
- Move the ->atomic_open() condition into fuse_use_open_atomic().
- Move fuse_statx_to_attr() up, in its own patch.
- Reject FUSE_OPEN_ATOMIC_CREATED without O_CREAT, and an opened existing file for O_EXCL.
- Drop cached pages on open as FUSE_CREATE does, and open an existing DAX file in ->open().
Changes in v2-v8: see the earlier versions below.
On truncation in the request without default_permissions (Bernd's question): the VFS can still refuse the open after the reply, for example with -EROFS on a read-only bind mount, for an append-only file, or from an LSM, so this series clears O_TRUNC in the request and truncates after the checks.
v8: https://lore.kernel.org/fuse-devel/[email protected]/
v7: https://lore.kernel.org/fuse-devel/[email protected]/
v6: https://lore.kernel.org/fuse-devel/[email protected]/
v5: https://lore.kernel.org/fuse-devel/[email protected]/
v4: https://lore.kernel.org/fuse-devel/[email protected]/
v3: https://lore.kernel.org/fuse-devel/[email protected]/
v2: https://lore.kernel.org/fuse-devel/[email protected]/
v1: https://lore.kernel.org/fuse-devel/[email protected]/
[1] Dharmendra Singh, "FUSE: Implement atomic lookup + open/create"
https://lore.kernel.org/all/[email protected]/ [2] Bernd Schubert, "fuse: full atomic open and atomic-open-revalidate" https://lore.kernel.org/all/[email protected]/
[3] Joanne Koong, "fuse: add FOPEN_FETCH_ATTR flag for fetching attributes after open"
https://lore.kernel.org/all/[email protected]/
[4] Bernd Schubert, "fuse: Add open-gettr for fuse-file-open"
https://lore.kernel.org/all/[email protected]/ [5] https://lore.kernel.org/fuse-devel/[email protected]/ [6] Miklos Szeredi, on combined requests and compounds https://lore.kernel.org/all/CAJfpegshcrjXJ0USZ8RRdBy=e0MxmBTJSCE0xnxG8LXgXy-xuQ@mail.gmail.com/
Jimmy Zuber (3):
fuse: move fuse_statx_to_attr() up
fuse: add FUSE_OPEN_ATOMIC
selftests/fuse: add test for FUSE_OPEN_ATOMIC
fs/fuse/dir.c | 321 +++++- fs/fuse/fuse_i.h | 3 + fs/fuse/fuse_trace.h | 1 + include/uapi/linux/fuse.h | 75 +- .../selftests/filesystems/fuse/.gitignore | 1 + .../selftests/filesystems/fuse/Makefile | 3 +- .../filesystems/fuse/atomic_open_test.c | 920 ++++++++++++++++++ 7 files changed, 1302 insertions(+), 22 deletions(-) create mode 100644 tools/testing/selftests/filesystems/fuse/atomic_open_test.c
base-commit: ab9787d7e66ac7e6e94fc975b37c5b56fff98ca9
Signature · 1 line
2.50.1
PATCH v9 1/3 fuse: move fuse_statx_to_attr() up
Move fuse_statx_to_attr() above fuse_atomic_open(), for use by FUSE_OPEN_ATOMIC in the next patch.
--- fs/fuse/dir.c | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-)
fs/fuse/dir.c +20 -20
@@ -935,6 +935,26 @@ static int fuse_create_open(const struct mnt_idmap *idmap, struct inode *dir,return err; } +static void fuse_statx_to_attr (struct fuse_statx *sx,struct fuse_attr *attr) +{ +memset (attr,0 ,sizeof (*attr)); + attr->ino = sx->ino; + attr->size = sx->size; + attr->blocks = sx->blocks; + attr->atime = sx->atime.tv_sec; + attr->mtime = sx->mtime.tv_sec; + attr->ctime = sx->ctime.tv_sec; + attr->atimensec = sx->atime.tv_nsec; + attr->mtimensec = sx->mtime.tv_nsec; + attr->ctimensec = sx->ctime.tv_nsec; + attr->mode = sx->mode; + attr->nlink = sx->nlink; + attr->uid = sx->uid; + attr->gid = sx->gid; + attr->rdev =new_encode_dev (MKDEV (sx->rdev_major, sx->rdev_minor)); + attr->blksize = sx->blksize; +} +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,
@@ -1410,26 +1430,6 @@ static void fuse_fillattr(const struct mnt_idmap *idmap, struct inode *inode, stat->blksize =1 << blkbits; } -static void fuse_statx_to_attr (struct fuse_statx *sx,struct fuse_attr *attr) -{ -memset (attr,0 ,sizeof (*attr)); - attr->ino = sx->ino; - attr->size = sx->size; - attr->blocks = sx->blocks; - attr->atime = sx->atime.tv_sec; - attr->mtime = sx->mtime.tv_sec; - attr->ctime = sx->ctime.tv_sec; - attr->atimensec = sx->atime.tv_nsec; - attr->mtimensec = sx->mtime.tv_nsec; - attr->ctimensec = sx->ctime.tv_nsec; - attr->mode = sx->mode; - attr->nlink = sx->nlink; - attr->uid = sx->uid; - attr->gid = sx->gid; - attr->rdev =new_encode_dev (MKDEV (sx->rdev_major, sx->rdev_minor)); - attr->blksize = sx->blksize; -} -static int fuse_do_statx (const struct mnt_idmap *idmap,struct inode *inode,struct file *file,struct kstat *stat) {
Signature · 1 line
2.50.1
PATCH v9 2/3 fuse: add FUSE_OPEN_ATOMIC
In reply to Jimmy ZuberAdd 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.
--- 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(-)
fs/fuse/dir.c +281 -0
@@ -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))
fs/fuse/fuse_i.h +3 -0
@@ -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;
fs/fuse/fuse_trace.h +1 -0
@@ -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" )/*
include/uapi/linux/fuse.h +74 -1
99 lines not on this page · raw message
Signature · 1 line
2.50.1
PATCH v9 3/3 selftests/fuse: add test for FUSE_OPEN_ATOMIC
In reply to Jimmy ZuberAdd atomic_open_test. atomic_open_test contains a FUSE server that reads requests from /dev/fuse and counts the requests that the kernel sends for open(). Each test case checks the result of open(), and most also check the requests: for names in and out of the dcache, with O_CREAT, O_EXCL and O_TRUNC, for files that the VFS denies, for the page cache, for replies that the kernel rejects, and for servers without FUSE_OPEN_ATOMIC.
Each test case runs with three server configurations: without FUSE_OPEN_ATOMIC, with it, and with it and FUSE_ATOMIC_O_TRUNC. Each test case runs in its own mount namespace, and skips below protocol 7.48 or when the system refuses /dev/fuse or the fuse mount.
--- .../selftests/filesystems/fuse/.gitignore | 1 + .../selftests/filesystems/fuse/Makefile | 3 +- .../filesystems/fuse/atomic_open_test.c | 920 ++++++++++++++++++ 3 files changed, 923 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/filesystems/fuse/atomic_open_test.c
tools/testing/selftests/filesystems/fuse/.gitignore +1 -0
@@ -1,4 +1,5 @@ # SPDX-License-Identifier: GPL-2.0-only +atomic_open_test fuse_mnt fusectl_test test_syncfs
tools/testing/selftests/filesystems/fuse/Makefile +2 -1
@@ -3,7 +3,7 @@ CFLAGS += -Wall -O2 -g $(KHDR_INCLUDES) TEST_GEN_PROGS := fusectl_test test_syncfs -TEST_GEN_PROGS += write_extend_eof_test +TEST_GEN_PROGS += write_extend_eof_test atomic_open_test TEST_GEN_PROGS += fuse_acl_cache_test TEST_GEN_PROGS += fuse_symlink_cache_test TEST_GEN_PROGS += fuse_readdir_cache_test
@@ -12,6 +12,7 @@ TEST_GEN_FILES := fuse_mnt include ../../lib.mk $(OUTPUT)/write_extend_eof_test: LDLIBS += -lpthread +$(OUTPUT)/atomic_open_test: LDLIBS += -lpthread VAR_CFLAGS := $(shell pkg-config fuse3 --cflags 2>/dev/null) ifeq ($(VAR_CFLAGS),)
tools/testing/selftests/filesystems/fuse/atomic_open_test.c new file +920 -0
920 lines not on this page · raw message
Signature · 1 line
2.50.1