* [PATCH v9 0/3] fuse: atomic open
@ 2026-11-21 0:16 Jimmy Zuber
2026-11-21 0:16 ` [PATCH v9 1/3] fuse: move fuse_statx_to_attr() up Jimmy Zuber
` (3 more replies)
0 siblings, 4 replies; 11+ messages in thread
From: Jimmy Zuber @ 2026-11-21 0:16 UTC (permalink / raw)
To: Miklos Szeredi
Cc: fuse-devel, linux-kselftest, Shuah Khan, Bernd Schubert,
Jim Harris, Horst Birthelmer, Luis Henriques, NeilBrown,
Joanne Koong
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/20261110204421.1251726-1-jamz@amazon.com/
v7: https://lore.kernel.org/fuse-devel/20261110155219.2175318-1-jamz@amazon.com/
v6: https://lore.kernel.org/fuse-devel/20261029130758.818617-1-jamz@amazon.com/
v5: https://lore.kernel.org/fuse-devel/20261028200459.2922951-1-jamz@amazon.com/
v4: https://lore.kernel.org/fuse-devel/20261026221639.628325-1-jamz@amazon.com/
v3: https://lore.kernel.org/fuse-devel/20261026190747.2373775-1-jamz@amazon.com/
v2: https://lore.kernel.org/fuse-devel/20261026151828.562870-1-jamz@amazon.com/
v1: https://lore.kernel.org/fuse-devel/20261020141647.3271341-1-jamz@amazon.com/
[1] Dharmendra Singh, "FUSE: Implement atomic lookup + open/create"
https://lore.kernel.org/all/20220517100744.26849-1-dharamhans87@gmail.com/
[2] Bernd Schubert, "fuse: full atomic open and atomic-open-revalidate"
https://lore.kernel.org/all/20231023183035.11035-1-bschubert@ddn.com/
[3] Joanne Koong, "fuse: add FOPEN_FETCH_ATTR flag for fetching
attributes after open"
https://lore.kernel.org/all/20240813212149.1909627-1-joannelkoong@gmail.com/
[4] Bernd Schubert, "fuse: Add open-gettr for fuse-file-open"
https://lore.kernel.org/all/20240820211735.2098951-1-bschubert@ddn.com/
[5] https://lore.kernel.org/fuse-devel/fa582b7f-23ba-449d-b311-20f6ac8e867c@bsbernd.com/
[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
--
2.50.1
^ permalink raw reply [flat|nested] 11+ messages in thread* [PATCH v9 1/3] fuse: move fuse_statx_to_attr() up 2026-11-21 0:16 [PATCH v9 0/3] fuse: atomic open Jimmy Zuber @ 2026-11-21 0:16 ` Jimmy Zuber 2026-11-25 23:31 ` Bernd Schubert 2026-11-21 0:16 ` [PATCH v9 2/3] fuse: add FUSE_OPEN_ATOMIC Jimmy Zuber ` (2 subsequent siblings) 3 siblings, 1 reply; 11+ messages in thread From: Jimmy Zuber @ 2026-11-21 0:16 UTC (permalink / raw) To: Miklos Szeredi Cc: fuse-devel, linux-kselftest, Shuah Khan, Bernd Schubert, Jim Harris, Horst Birthelmer, Luis Henriques, NeilBrown, Joanne Koong Move fuse_statx_to_attr() above fuse_atomic_open(), for use by FUSE_OPEN_ATOMIC in the next patch. Signed-off-by: Jimmy Zuber <jamz@amazon.com> --- fs/fuse/dir.c | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/fs/fuse/dir.c b/fs/fuse/dir.c index da23ccd8685cf..a719e3516e187 100644 --- a/fs/fuse/dir.c +++ b/fs/fuse/dir.c @@ -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) { -- 2.50.1 ^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH v9 1/3] fuse: move fuse_statx_to_attr() up 2026-11-21 0:16 ` [PATCH v9 1/3] fuse: move fuse_statx_to_attr() up Jimmy Zuber @ 2026-11-25 23:31 ` Bernd Schubert 0 siblings, 0 replies; 11+ messages in thread From: Bernd Schubert @ 2026-11-25 23:31 UTC (permalink / raw) To: Jimmy Zuber, Miklos Szeredi Cc: fuse-devel, linux-kselftest, Shuah Khan, Jim Harris, Horst Birthelmer, Luis Henriques, NeilBrown, Joanne Koong On 11/21/26 01:16, Jimmy Zuber wrote: > Move fuse_statx_to_attr() above fuse_atomic_open(), for use by > FUSE_OPEN_ATOMIC in the next patch. > > Signed-off-by: Jimmy Zuber <jamz@amazon.com> > --- > fs/fuse/dir.c | 40 ++++++++++++++++++++-------------------- > 1 file changed, 20 insertions(+), 20 deletions(-) > > diff --git a/fs/fuse/dir.c b/fs/fuse/dir.c > index da23ccd8685cf..a719e3516e187 100644 > --- a/fs/fuse/dir.c > +++ b/fs/fuse/dir.c > @@ -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) > { Reviewed-by: Bernd Schubert <bernd@bsbernd.com> [remaining review of the other patches tomorrow] ^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH v9 2/3] fuse: add FUSE_OPEN_ATOMIC 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 2026-12-03 22:51 ` Bernd Schubert 2026-11-21 0:16 ` [PATCH v9 3/3] selftests/fuse: add test for FUSE_OPEN_ATOMIC Jimmy Zuber 2026-12-03 15:27 ` [PATCH v9 0/3] fuse: atomic open Jimmy Zuber 3 siblings, 1 reply; 11+ messages in thread From: Jimmy Zuber @ 2026-11-21 0:16 UTC (permalink / raw) To: Miklos Szeredi Cc: fuse-devel, linux-kselftest, Shuah Khan, Bernd Schubert, Jim Harris, Horst Birthelmer, Luis Henriques, NeilBrown, Joanne Koong 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 ^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH v9 2/3] fuse: add FUSE_OPEN_ATOMIC 2026-11-21 0:16 ` [PATCH v9 2/3] fuse: add FUSE_OPEN_ATOMIC Jimmy Zuber @ 2026-12-03 22:51 ` Bernd Schubert 0 siblings, 0 replies; 11+ messages in thread From: Bernd Schubert @ 2026-12-03 22:51 UTC (permalink / raw) To: Jimmy Zuber, Miklos Szeredi Cc: fuse-devel, linux-kselftest, Shuah Khan, Jim Harris, Horst Birthelmer, Luis Henriques, NeilBrown, Joanne Koong, John Groves On 11/21/26 01:16, Jimmy Zuber wrote: > 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); > + } Hmm, how about doing as in f622d990de63c3e3d0b1546ce49c2b129e50cb52? The alternative would be better comment - "An existing DAX file..." explains the code, but not the reason. Maybe /* DAX needs special handling, better not with complex atomic open */ And then John could handle DAX later if useful. In anyway, minor nit and could be ignored. > + 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; Reviewed-by: Bernd Schubert <bernd@bsbernd.com> ^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH v9 3/3] selftests/fuse: add test for FUSE_OPEN_ATOMIC 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 ` [PATCH v9 2/3] fuse: add FUSE_OPEN_ATOMIC Jimmy Zuber @ 2026-11-21 0:16 ` Jimmy Zuber 2026-12-03 23:15 ` Bernd Schubert 2026-12-03 15:27 ` [PATCH v9 0/3] fuse: atomic open Jimmy Zuber 3 siblings, 1 reply; 11+ messages in thread From: Jimmy Zuber @ 2026-11-21 0:16 UTC (permalink / raw) To: Miklos Szeredi Cc: fuse-devel, linux-kselftest, Shuah Khan, Bernd Schubert, Jim Harris, Horst Birthelmer, Luis Henriques, NeilBrown, Joanne Koong Add 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. Signed-off-by: Jimmy Zuber <jamz@amazon.com> --- .../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 diff --git a/tools/testing/selftests/filesystems/fuse/.gitignore b/tools/testing/selftests/filesystems/fuse/.gitignore index 7c787313e69b6..faac6d969ef94 100644 --- a/tools/testing/selftests/filesystems/fuse/.gitignore +++ b/tools/testing/selftests/filesystems/fuse/.gitignore @@ -1,4 +1,5 @@ # SPDX-License-Identifier: GPL-2.0-only +atomic_open_test fuse_mnt fusectl_test test_syncfs diff --git a/tools/testing/selftests/filesystems/fuse/Makefile b/tools/testing/selftests/filesystems/fuse/Makefile index e3ae3acff1139..8390d6019513b 100644 --- a/tools/testing/selftests/filesystems/fuse/Makefile +++ b/tools/testing/selftests/filesystems/fuse/Makefile @@ -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),) diff --git a/tools/testing/selftests/filesystems/fuse/atomic_open_test.c b/tools/testing/selftests/filesystems/fuse/atomic_open_test.c new file mode 100644 index 0000000000000..51cdf685bf678 --- /dev/null +++ b/tools/testing/selftests/filesystems/fuse/atomic_open_test.c @@ -0,0 +1,920 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Test the requests that open() sends with and without FUSE_OPEN_ATOMIC, + * against a raw /dev/fuse server. + * + * Requires root to mount fuse. + */ +#define _GNU_SOURCE +#include <errno.h> +#include <fcntl.h> +#include <grp.h> +#include <poll.h> +#include <pthread.h> +#include <sched.h> +#include <stddef.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <sys/mman.h> +#include <sys/mount.h> +#include <sys/stat.h> +#include <sys/syscall.h> +#include <sys/uio.h> +#include <sys/wait.h> +#include <linux/errno.h> +#include <linux/fuse.h> +#include <linux/openat2.h> + +#include "../../kselftest_harness.h" + +#define MAX_WRITE (128 * 1024) +#define MAX_ENTRIES 16 +#define NOBODY 65534 +#define MNT "/tmp/mnt" + +struct entry { + uint64_t parent; + char name[64]; + uint64_t nodeid; + uint32_t mode; + uint64_t size; +}; + +/* Malformed FUSE_OPEN_ATOMIC replies for an existing regular file */ +enum bad_reply { + BAD_NONE, + BAD_SHORT, /* without fuse_open_out */ + BAD_ZERO_NODEID, + BAD_ROOT_NODEID, + BAD_PARTIAL_MASK, /* without all of STATX_BASIC_STATS */ + BAD_CREATED, /* FUSE_OPEN_ATOMIC_CREATED */ + BAD_EXCL, /* opened for O_CREAT | O_EXCL */ + BAD_FLAG, /* an undefined reply flag */ + BAD_RESERVED, /* a nonzero reserved field */ + BAD_HONORED, /* an atomic_flags bit not requested */ +}; + +/* Server state, shared with the server thread */ +struct server { + int fd; + /* Written with a struct event for each FORGET and RELEASE */ + int event_pipe[2]; + int atomic, atomic_o_trunc; + /* Protocol minor version of the kernel, from FUSE_INIT */ + uint32_t minor; + struct entry ent[MAX_ENTRIES]; + int nent; + int nr_lookup, nr_create, nr_open, nr_open_atomic, nr_setattr; + int nr_release, nr_read, nr_enosys, nr_root_getattr; + /* Requests that carried O_TRUNC */ + int nr_trunc; + /* flags of the last FUSE_OPEN_ATOMIC */ + uint32_t open_flags; + /* FUSE_OPEN_ATOMIC requests with other than the expected inputs */ + int nr_bad_request; + /* FUSE_OPEN: -ENOSYS */ + int no_open; + /* Reply -ENOENT for a missing name without O_CREAT */ + int enoent; + /* Reply with only the entry for an existing file */ + int entry_only; + /* Reply FUSE_OPEN_ATOMIC_CREATED for an existing file */ + int created_existing; + enum bad_reply bad; +}; + +struct event { + uint32_t opcode; + uint64_t nodeid; +}; + +static void reply(int fd, uint64_t unique, int error, void *data, size_t len) +{ + struct fuse_out_header oh = { + .len = sizeof(oh) + (data ? len : 0), + .error = error, + .unique = unique, + }; + struct iovec iov[2] = { { &oh, sizeof(oh) }, { data, len } }; + + /* Errors here are teardown races (device closed on unmount) */ + if (writev(fd, iov, data ? 2 : 1) < 0) + return; +} + +static void post_event(struct server *s, uint32_t opcode, uint64_t nodeid) +{ + struct event ev = { opcode, nodeid }; + + /* The test reads the pipe; a full pipe means nobody waits */ + if (write(s->event_pipe[1], &ev, sizeof(ev)) < 0) + return; +} + +static struct entry *find_in(struct server *s, uint64_t parent, + const char *name) +{ + int i; + + for (i = 0; i < s->nent; i++) + if (s->ent[i].parent == parent && !strcmp(s->ent[i].name, name)) + return &s->ent[i]; + return NULL; +} + +static struct entry *find_node(struct server *s, uint64_t nodeid) +{ + int i; + + for (i = 0; i < s->nent; i++) + if (s->ent[i].nodeid == nodeid) + return &s->ent[i]; + return NULL; +} + +static struct entry *add_in(struct server *s, uint64_t parent, + const char *name, uint32_t mode) +{ + struct entry *e = &s->ent[s->nent]; + + if (s->nent >= MAX_ENTRIES) + return NULL; + e->parent = parent; + snprintf(e->name, sizeof(e->name), "%.63s", name); + e->nodeid = FUSE_ROOT_ID + 1 + s->nent++; + e->mode = mode; + return e; +} + +static void fill_attr(struct fuse_attr *attr, struct entry *e) +{ + attr->ino = e->nodeid; + attr->mode = e->mode; + attr->nlink = S_ISDIR(e->mode) ? 2 : 1; + attr->size = e->size; + attr->blksize = 4096; +} + +/* A NULL entry is a negative entry, cached like a found one */ +static void fill_entry(struct fuse_entry_out *out, struct entry *e) +{ + memset(out, 0, sizeof(*out)); + out->entry_valid = 3600; + if (!e) + return; + out->nodeid = e->nodeid; + out->attr_valid = 3600; + fill_attr(&out->attr, e); +} + +static void fill_open_atomic(struct fuse_open_atomic_out *out, + struct entry *e) +{ + memset(out, 0, sizeof(*out)); + out->entry_valid = 3600; + if (!e) + return; + out->nodeid = e->nodeid; + out->attr_valid = 3600; + out->stat.mask = STATX_BASIC_STATS; + out->stat.ino = e->nodeid; + out->stat.mode = e->mode; + out->stat.nlink = S_ISDIR(e->mode) ? 2 : 1; + out->stat.size = e->size; + out->stat.blksize = 4096; +} + +static void do_create(struct server *s, struct fuse_in_header *ih) +{ + struct fuse_create_in *in = (void *)(ih + 1); + const char *name = (char *)(in + 1); + struct entry *e = find_in(s, ih->nodeid, name); + struct { + struct fuse_entry_out entry; + struct fuse_open_out open; + } out = {0}; + + s->nr_create++; + if (e && (in->flags & O_EXCL)) + return reply(s->fd, ih->unique, -EEXIST, NULL, 0); + if (!e) + e = add_in(s, ih->nodeid, name, S_IFREG | in->mode); + if (!e) + return reply(s->fd, ih->unique, -ENOSPC, NULL, 0); + fill_entry(&out.entry, e); + reply(s->fd, ih->unique, 0, &out, sizeof(out)); +} + +static void do_open_atomic(struct server *s, struct fuse_in_header *ih) +{ + struct fuse_open_atomic_in *in = (void *)(ih + 1); + const char *name = (char *)(in + 1); + struct entry *e = find_in(s, ih->nodeid, name); + struct fuse_open_atomic_out out; + size_t len = sizeof(out); + int excl = (in->flags & O_CREAT) && (in->flags & O_EXCL); + + if (!s->atomic) { + s->nr_enosys++; + return reply(s->fd, ih->unique, -ENOSYS, NULL, 0); + } + s->nr_open_atomic++; + s->open_flags = in->flags; + s->nr_bad_request += in->open_flags || in->atomic_flags || + in->reserved[0] || in->reserved[1] || + in->sx_mask != STATX_BASIC_STATS; + if (in->flags & O_TRUNC) + s->nr_trunc++; + if (e && excl && s->bad != BAD_EXCL) + return reply(s->fd, ih->unique, -EEXIST, NULL, 0); + if (!e && (in->flags & O_CREAT)) { + e = add_in(s, ih->nodeid, name, S_IFREG | in->mode); + if (!e) + return reply(s->fd, ih->unique, -ENOSPC, NULL, 0); + fill_open_atomic(&out, e); + out.flags = FUSE_OPEN_ATOMIC_OPENED | FUSE_OPEN_ATOMIC_CREATED; + } else { + fill_open_atomic(&out, e); + if (!e && s->enoent) + return reply(s->fd, ih->unique, -ENOENT, NULL, 0); + if (!e || !S_ISREG(e->mode) || s->entry_only) + return reply(s->fd, ih->unique, 0, &out, len); + out.flags = FUSE_OPEN_ATOMIC_OPENED; + if (s->created_existing || s->bad == BAD_CREATED) + out.flags |= FUSE_OPEN_ATOMIC_CREATED; + } + if (s->bad == BAD_SHORT) + len = offsetof(struct fuse_open_atomic_out, open); + if (s->bad == BAD_ZERO_NODEID) + out.nodeid = 0; + if (s->bad == BAD_ROOT_NODEID) + out.nodeid = FUSE_ROOT_ID; + if (s->bad == BAD_HONORED) + out.atomic_flags = 1; + if (s->bad == BAD_RESERVED) + out.reserved = 1; + if (s->bad == BAD_FLAG) + out.flags |= 1u << 31; + if (s->bad == BAD_PARTIAL_MASK) + out.stat.mask = STATX_TYPE | STATX_MODE; + reply(s->fd, ih->unique, 0, &out, len); +} + +static void do_init(struct server *s, struct fuse_in_header *ih) +{ + struct fuse_init_in *in = (void *)(ih + 1); + struct fuse_init_out out = {0}; + + s->minor = in->minor; + out.major = FUSE_KERNEL_VERSION; + out.minor = in->minor < FUSE_KERNEL_MINOR_VERSION ? + in->minor : FUSE_KERNEL_MINOR_VERSION; + out.max_readahead = in->max_readahead; + out.max_write = MAX_WRITE; + out.max_background = 16; + out.congestion_threshold = 12; + if (s->atomic_o_trunc) + out.flags = FUSE_ATOMIC_O_TRUNC; + reply(s->fd, ih->unique, 0, &out, sizeof(out)); +} + +/* GETATTR and SETATTR; a size in SETATTR truncates */ +static void do_attr(struct server *s, struct fuse_in_header *ih) +{ + struct fuse_setattr_in *in = (void *)(ih + 1); + struct entry *e = find_node(s, ih->nodeid); + struct fuse_attr_out out = { + .attr_valid = 3600, + .attr = { .ino = FUSE_ROOT_ID, .mode = S_IFDIR | 0777, + .nlink = 2 }, + }; + + if (ih->opcode == FUSE_GETATTR && ih->nodeid == FUSE_ROOT_ID) + s->nr_root_getattr++; + if (ih->opcode == FUSE_SETATTR) { + s->nr_setattr++; + if (e && (in->valid & FATTR_SIZE)) + e->size = in->size; + } + if (e) { + memset(&out.attr, 0, sizeof(out.attr)); + fill_attr(&out.attr, e); + } + reply(s->fd, ih->unique, 0, &out, sizeof(out)); +} + +static void do_read(struct server *s, struct fuse_in_header *ih) +{ + struct fuse_read_in *in = (void *)(ih + 1); + struct entry *e = find_node(s, ih->nodeid); + static char data[MAX_WRITE]; + size_t len = 0; + + s->nr_read++; + if (e && in->offset < e->size) + len = e->size - in->offset; + if (len > in->size) + len = in->size; + reply(s->fd, ih->unique, 0, data, len); +} + +static void handle(struct server *s, struct fuse_in_header *ih) +{ + struct fuse_open_in *open_in = (void *)(ih + 1); + struct fuse_batch_forget_in *forget_in = (void *)(ih + 1); + struct fuse_forget_one *one = (void *)(forget_in + 1); + struct fuse_entry_out entry; + struct fuse_open_out open = {0}; + uint32_t i; + + switch (ih->opcode) { + case FUSE_INIT: + return do_init(s, ih); + case FUSE_GETATTR: + case FUSE_SETATTR: + return do_attr(s, ih); + case FUSE_LOOKUP: + s->nr_lookup++; + if (s->enoent && !find_in(s, ih->nodeid, (char *)(ih + 1))) + return reply(s->fd, ih->unique, -ENOENT, NULL, 0); + fill_entry(&entry, find_in(s, ih->nodeid, (char *)(ih + 1))); + return reply(s->fd, ih->unique, 0, &entry, sizeof(entry)); + case FUSE_CREATE: + return do_create(s, ih); + case FUSE_OPEN_ATOMIC: + return do_open_atomic(s, ih); + case FUSE_OPEN: + s->nr_open++; + if (s->no_open) + return reply(s->fd, ih->unique, -ENOSYS, NULL, 0); + if (open_in->flags & O_TRUNC) { + s->nr_trunc++; + find_node(s, ih->nodeid)->size = 0; + } + return reply(s->fd, ih->unique, 0, &open, sizeof(open)); + case FUSE_OPENDIR: + return reply(s->fd, ih->unique, 0, &open, sizeof(open)); + case FUSE_READ: + return do_read(s, ih); + case FUSE_RELEASE: + s->nr_release++; + post_event(s, FUSE_RELEASE, ih->nodeid); + return reply(s->fd, ih->unique, 0, NULL, 0); + case FUSE_FLUSH: + case FUSE_RELEASEDIR: + return reply(s->fd, ih->unique, 0, NULL, 0); + case FUSE_FORGET: + return post_event(s, FUSE_FORGET, ih->nodeid); + case FUSE_BATCH_FORGET: + for (i = 0; i < forget_in->count; i++) + post_event(s, FUSE_FORGET, one[i].nodeid); + return; + default: + return reply(s->fd, ih->unique, -ENOSYS, NULL, 0); + } +} + +static void *server_thread(void *arg) +{ + struct server *s = arg; + static char buf[MAX_WRITE + 4096]; + + for (;;) { + ssize_t n = read(s->fd, buf, sizeof(buf)); + + if (n < 0 && (errno == EINTR || errno == EAGAIN)) + continue; + if (n < 0) + return NULL; /* device closed on unmount */ + if (n >= (ssize_t)sizeof(struct fuse_in_header)) + handle(s, (struct fuse_in_header *)buf); + } +} + +FIXTURE(fuse) +{ + struct server *srv; + int nobody; +}; + +FIXTURE_VARIANT(fuse) +{ + int atomic; + int atomic_o_trunc; +}; + +FIXTURE_VARIANT_ADD(fuse, atomic) +{ + .atomic = 1, +}; + +FIXTURE_VARIANT_ADD(fuse, atomic_trunc) +{ + .atomic = 1, + .atomic_o_trunc = 1, +}; + +FIXTURE_VARIANT_ADD(fuse, plain) +{ + .atomic = 0, +}; + +static int become_nobody(void) +{ + return setgroups(0, NULL) || setgid(NOBODY) || setuid(NOBODY); +} + +static bool can_become_nobody(void) +{ + int status; + pid_t pid = fork(); + + if (pid == 0) + _exit(become_nobody()); + return pid > 0 && waitpid(pid, &status, 0) == pid && + WIFEXITED(status) && !WEXITSTATUS(status); +} + +/* + * Each test runs in its own process and mount namespace: the mount, the + * server thread and its device go away when the test exits. + */ +FIXTURE_SETUP(fuse) +{ + struct server *s; + char opts[128]; + struct stat st; + pthread_t t; + int err; + + if (geteuid() != 0) + SKIP(return, "need root to mount fuse"); + if (unshare(CLONE_NEWNS) || + mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL)) + SKIP(return, "no private mount namespace: %s", strerror(errno)); + if (mount("tmpfs", "/tmp", "tmpfs", 0, NULL)) + SKIP(return, "cannot mount tmpfs: %s", strerror(errno)); + self->nobody = can_become_nobody(); + + s = mmap(NULL, sizeof(*s), PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_ANONYMOUS, -1, 0); + ASSERT_NE(MAP_FAILED, s); + self->srv = s; + ASSERT_EQ(0, pipe(s->event_pipe)); + ASSERT_EQ(0, fcntl(s->event_pipe[1], F_SETFL, O_NONBLOCK)); + s->atomic = variant->atomic; + s->atomic_o_trunc = variant->atomic_o_trunc; + add_in(s, FUSE_ROOT_ID, "ro", S_IFREG | 0444); + add_in(s, FUSE_ROOT_ID, "rw", S_IFREG | 0644); + add_in(s, FUSE_ROOT_ID, "dir", S_IFDIR | 0755); + + s->fd = open("/dev/fuse", O_RDWR); + err = errno; + if (s->fd < 0) { + ASSERT_TRUE(err == ENOENT || err == ENODEV || err == ENXIO || + err == EACCES || err == EPERM); + SKIP(return, "cannot open /dev/fuse: %s", strerror(err)); + } + ASSERT_EQ(0, mkdir(MNT, 0755)); + snprintf(opts, sizeof(opts), + "fd=%d,rootmode=40000,user_id=0,group_id=0,default_permissions,allow_other", + s->fd); + if (mount("fuse", MNT, "fuse", 0, opts)) { + err = errno; + ASSERT_TRUE(err == EPERM || err == EACCES || err == ENODEV); + SKIP(return, "cannot mount fuse: %s", strerror(err)); + } + ASSERT_EQ(0, pthread_create(&t, NULL, server_thread, s)); + pthread_detach(t); + /* Wait for FUSE_INIT: the root GETATTR is queued behind it */ + ASSERT_EQ(0, stat(MNT, &st)); + if (s->minor < 48) + SKIP(return, "kernel without FUSE_OPEN_ATOMIC (protocol 7.48)"); +} + +FIXTURE_TEARDOWN(fuse) +{ + EXPECT_EQ(0, self->srv->nr_bad_request); + /* MNT_FORCE aborts the connection, so open files close at exit */ + umount2(MNT, MNT_FORCE); +} + +/* Open @name as an unprivileged user and return the errno, or 0 */ +static int open_as_nobody(struct __test_metadata *_metadata, + const char *name, int flags) +{ + char path[128]; + int status, fd; + pid_t pid; + + snprintf(path, sizeof(path), MNT "/%s", name); + pid = fork(); + ASSERT_GE(pid, 0); + if (pid == 0) { + if (become_nobody()) + _exit(255); + fd = open(path, flags, 0644); + _exit(fd < 0 ? errno : 0); + } + ASSERT_EQ(pid, waitpid(pid, &status, 0)); + ASSERT_TRUE(WIFEXITED(status)); + ASSERT_NE(255, WEXITSTATUS(status)); + return WEXITSTATUS(status); +} + +static int open_at(const char *name, int flags) +{ + char path[128]; + + snprintf(path, sizeof(path), MNT "/%s", name); + return open(path, flags, 0644); +} + +static void open_close(struct __test_metadata *_metadata, const char *name, + int flags) +{ + int fd = open_at(name, flags); + + ASSERT_GE(fd, 0); + close(fd); +} + +static uint64_t nodeid(struct server *s, const char *name) +{ + return find_in(s, FUSE_ROOT_ID, name)->nodeid; +} + +/* Wait up to ten seconds for the server to receive @opcode for @node */ +static void wait_event(struct __test_metadata *_metadata, struct server *s, + uint32_t opcode, uint64_t node) +{ + struct pollfd pfd = { .fd = s->event_pipe[0], .events = POLLIN }; + struct event ev; + + do { + ASSERT_EQ(1, poll(&pfd, 1, 10000)) + TH_LOG("no opcode %u for nodeid %llu", opcode, + (unsigned long long)node); + ASSERT_EQ(sizeof(ev), read(s->event_pipe[0], &ev, sizeof(ev))); + } while (ev.opcode != opcode || ev.nodeid != node); +} + +/* Drop the dentry of @name, as the server can at any time */ +static void inval_entry(struct __test_metadata *_metadata, struct server *s, + const char *name) +{ + struct fuse_notify_inval_entry_out out = { + .parent = FUSE_ROOT_ID, + .namelen = strlen(name), + }; + struct fuse_out_header oh = { + .len = sizeof(oh) + sizeof(out) + out.namelen + 1, + .error = FUSE_NOTIFY_INVAL_ENTRY, + }; + struct iovec iov[3] = { + { &oh, sizeof(oh) }, + { &out, sizeof(out) }, + { (void *)name, out.namelen + 1 }, + }; + + ASSERT_EQ(oh.len, writev(s->fd, iov, 3)); +} + +/* + * Read "rw" into the page cache through a file that stays open as @fd, + * and drop its dentry. + */ +static void cache_rw(struct __test_metadata *_metadata, struct server *s, + int *fd) +{ + char buf[16]; + + find_in(s, FUSE_ROOT_ID, "rw")->size = 4096; + *fd = open_at("rw", O_RDONLY); + ASSERT_GE(*fd, 0); + ASSERT_EQ(sizeof(buf), pread(*fd, buf, sizeof(buf), 0)); + inval_entry(_metadata, s, "rw"); +} + +/* Read "rw" through @fd again, and return the number of FUSE_READs */ +static int reread_rw(struct server *s, int fd) +{ + char buf[16]; + ssize_t n = pread(fd, buf, sizeof(buf), 0); + + close(fd); + return n == sizeof(buf) ? s->nr_read : -1; +} + +TEST_F(fuse, create_new) +{ + open_close(_metadata, "a", O_CREAT | O_WRONLY); + open_close(_metadata, "b", O_CREAT | O_EXCL | O_WRONLY); + + EXPECT_EQ(0, self->srv->nr_open); + EXPECT_EQ(variant->atomic ? 2 : 0, self->srv->nr_open_atomic); + EXPECT_EQ(variant->atomic ? 0 : 2, self->srv->nr_lookup); + EXPECT_EQ(variant->atomic ? 0 : 2, self->srv->nr_create); +} + +TEST_F(fuse, create_negative) +{ + struct stat st; + + /* O_CREAT on a cached negative dentry */ + ASSERT_EQ(-1, stat(MNT "/a", &st)); + open_close(_metadata, "a", O_CREAT | O_WRONLY); + ASSERT_EQ(0, stat(MNT "/a", &st)); + EXPECT_EQ(1, self->srv->nr_lookup); + EXPECT_EQ(variant->atomic ? 1 : 0, self->srv->nr_open_atomic); + EXPECT_EQ(variant->atomic ? 0 : 1, self->srv->nr_create); +} + +TEST_F(fuse, create_rejected) +{ + struct stat st; + int getattr; + + /* The server created the name of a rejected reply */ + if (!variant->atomic) + SKIP(return, "the server lacks FUSE_OPEN_ATOMIC"); + ASSERT_EQ(-1, stat(MNT "/a", &st)); + self->srv->bad = BAD_FLAG; + ASSERT_EQ(-1, open_at("a", O_CREAT | O_WRONLY)); + EXPECT_EQ(EIO, errno); + /* The kernel refetches the name and the directory attributes */ + getattr = self->srv->nr_root_getattr; + ASSERT_EQ(0, stat(MNT, &st)); + EXPECT_EQ(getattr + 1, self->srv->nr_root_getattr); + ASSERT_EQ(0, stat(MNT "/a", &st)); + EXPECT_EQ(2, self->srv->nr_lookup); +} + +TEST_F(fuse, create_existing) +{ + if (!self->nobody) + SKIP(return, "cannot switch to uid %d", NOBODY); + ASSERT_EQ(-1, open_at("ro", O_CREAT | O_EXCL | O_WRONLY)); + EXPECT_EQ(EEXIST, errno); + /* may_open() runs after the reply, and fput() releases the file */ + EXPECT_EQ(EACCES, open_as_nobody(_metadata, "ro", O_CREAT | O_RDWR)); + if (variant->atomic) + wait_event(_metadata, self->srv, FUSE_RELEASE, + nodeid(self->srv, "ro")); + EXPECT_EQ(0, self->srv->nr_create); +} + +TEST_F(fuse, read_only) +{ + ASSERT_EQ(0, mount(NULL, MNT, NULL, + MS_REMOUNT | MS_BIND | MS_RDONLY, NULL)); + /* An existing name gives EEXIST, not EROFS */ + ASSERT_EQ(-1, open_at("rw", O_CREAT | O_EXCL | O_WRONLY)); + EXPECT_EQ(EEXIST, errno); + ASSERT_EQ(-1, open_at("ro", O_RDONLY | O_TRUNC)); + EXPECT_EQ(EROFS, errno); + + EXPECT_EQ(0, self->srv->nr_create); + if (variant->atomic_o_trunc) { + EXPECT_EQ(0, self->srv->nr_open_atomic); + EXPECT_EQ(2, self->srv->nr_lookup); + } else { + EXPECT_EQ(variant->atomic ? 1 : 0, self->srv->nr_open_atomic); + EXPECT_EQ(variant->atomic ? 1 : 2, self->srv->nr_lookup); + } +} + +TEST_F(fuse, open_existing) +{ + if (!self->nobody) + SKIP(return, "cannot switch to uid %d", NOBODY); + /* may_open() runs after the reply, and fput() releases the file */ + EXPECT_EQ(EACCES, open_as_nobody(_metadata, "ro", O_RDWR)); + if (variant->atomic) + wait_event(_metadata, self->srv, FUSE_RELEASE, + nodeid(self->srv, "ro")); + open_close(_metadata, "rw", O_RDONLY); + ASSERT_EQ(-1, open_at("missing", O_RDONLY)); + EXPECT_EQ(ENOENT, errno); + /* A zero nodeid is cached like a negative LOOKUP */ + ASSERT_EQ(-1, open_at("missing", O_RDONLY)); + EXPECT_EQ(ENOENT, errno); + + EXPECT_EQ(variant->atomic ? 0 : 1, self->srv->nr_open); + EXPECT_EQ(variant->atomic ? 3 : 0, self->srv->nr_open_atomic); + EXPECT_EQ(variant->atomic ? 0 : 3, self->srv->nr_lookup); +} + +TEST_F(fuse, open_enoent) +{ + struct stat st; + + /* -ENOENT gives what FUSE_LOOKUP gives: no negative dentry cached */ + self->srv->enoent = 1; + ASSERT_EQ(-1, open_at("missing", O_RDONLY)); + EXPECT_EQ(ENOENT, errno); + ASSERT_EQ(-1, stat(MNT "/missing", &st)); + EXPECT_EQ(ENOENT, errno); + EXPECT_EQ(variant->atomic ? 1 : 0, self->srv->nr_open_atomic); + EXPECT_EQ(variant->atomic ? 1 : 2, self->srv->nr_lookup); +} + +TEST_F(fuse, open_trunc) +{ + open_close(_metadata, "rw", O_WRONLY | O_TRUNC); + /* As a shell "> ro" */ + open_close(_metadata, "ro", O_CREAT | O_TRUNC | O_WRONLY); + + EXPECT_EQ(0, self->srv->nr_create); + if (variant->atomic && !variant->atomic_o_trunc) { + /* The server opens the file and SETATTR truncates it */ + EXPECT_EQ(2, self->srv->nr_open_atomic); + EXPECT_EQ(0, self->srv->nr_lookup); + EXPECT_EQ(0, self->srv->nr_open); + EXPECT_EQ(0, self->srv->nr_trunc); + EXPECT_EQ(2, self->srv->nr_setattr); + } else { + /* FUSE_OPEN truncates with FUSE_ATOMIC_O_TRUNC, after LOOKUP */ + EXPECT_EQ(0, self->srv->nr_open_atomic); + EXPECT_EQ(2, self->srv->nr_lookup); + EXPECT_EQ(2, self->srv->nr_open); + EXPECT_EQ(variant->atomic_o_trunc ? 2 : 0, self->srv->nr_trunc); + EXPECT_EQ(variant->atomic_o_trunc ? 0 : 2, + self->srv->nr_setattr); + } +} + +TEST_F(fuse, open_directory) +{ + /* The server returns the entry only, and the VFS opens it */ + open_close(_metadata, "dir", O_RDONLY); + EXPECT_EQ(variant->atomic ? 1 : 0, self->srv->nr_open_atomic); + EXPECT_EQ(variant->atomic ? 0 : 1, self->srv->nr_lookup); +} + +TEST_F(fuse, open_o_directory) +{ + /* For O_DIRECTORY the kernel sends FUSE_LOOKUP */ + open_close(_metadata, "dir", O_RDONLY | O_DIRECTORY); + ASSERT_EQ(-1, open_at("rw", O_RDONLY | O_DIRECTORY)); + EXPECT_EQ(ENOTDIR, errno); + EXPECT_EQ(0, self->srv->nr_open_atomic); + EXPECT_EQ(2, self->srv->nr_lookup); + EXPECT_EQ(0, self->srv->nr_open); +} + +TEST_F(fuse, open_entry_only) +{ + /* The server returns the entry only; FUSE_OPEN opens the file */ + self->srv->entry_only = 1; + open_close(_metadata, "rw", O_RDONLY); + EXPECT_EQ(variant->atomic ? 1 : 0, self->srv->nr_open_atomic); + EXPECT_EQ(variant->atomic ? 0 : 1, self->srv->nr_lookup); + EXPECT_EQ(1, self->srv->nr_open); +} + +TEST_F(fuse, open_regular) +{ + struct open_how how = { .flags = O_RDONLY | OPENAT2_REGULAR }; + uint32_t flags; + int fd; + + /* OPENAT2_REGULAR is not part of the request */ + if (!variant->atomic) + SKIP(return, "the server lacks FUSE_OPEN_ATOMIC"); + fd = syscall(__NR_openat2, AT_FDCWD, MNT "/rw", &how, sizeof(how)); + if (fd < 0 && (errno == EINVAL || errno == ENOSYS) && + !self->srv->nr_open_atomic) + SKIP(return, "openat2 without OPENAT2_REGULAR"); + ASSERT_GE(fd, 0); + close(fd); + flags = self->srv->open_flags; + inval_entry(_metadata, self->srv, "rw"); + open_close(_metadata, "rw", O_RDONLY); + EXPECT_EQ(self->srv->open_flags, flags); + EXPECT_EQ(2, self->srv->nr_open_atomic); + /* The VFS refuses a directory */ + fd = syscall(__NR_openat2, AT_FDCWD, MNT "/dir", &how, sizeof(how)); + if (fd >= 0) + close(fd); + EXPECT_EQ(-1, fd); + EXPECT_EQ(EFTYPE, errno); + EXPECT_EQ(3, self->srv->nr_open_atomic); +} + +TEST_F(fuse, open_drops_cache) +{ + int fd; + + cache_rw(_metadata, self->srv, &fd); + /* An open without FOPEN_KEEP_CACHE drops the cached pages */ + open_close(_metadata, "rw", O_RDONLY); + EXPECT_EQ(2, reread_rw(self->srv, fd)); + /* Each open is one FUSE_OPEN_ATOMIC, or FUSE_LOOKUP and FUSE_OPEN */ + EXPECT_EQ(variant->atomic ? 0 : 2, self->srv->nr_open); + EXPECT_EQ(variant->atomic ? 2 : 0, self->srv->nr_open_atomic); +} + +TEST_F(fuse, create_drops_cache) +{ + int fd; + + cache_rw(_metadata, self->srv, &fd); + self->srv->created_existing = 1; + open_close(_metadata, "rw", O_CREAT | O_RDWR); + EXPECT_EQ(2, reread_rw(self->srv, fd)); +} + +TEST_F(fuse, bad_replies) +{ + /* A parsed reply with a nonzero nodeid takes a lookup reference */ + enum { NONE, RW, ROOT }; + static const struct { + enum bad_reply bad; + int flags; + int release; + int forget; + } cases[] = { + { BAD_SHORT, O_RDONLY, 0, NONE }, + { BAD_ZERO_NODEID, O_RDONLY, 1, NONE }, + { BAD_ROOT_NODEID, O_RDONLY, 1, ROOT }, + { BAD_PARTIAL_MASK, O_RDONLY, 1, RW }, + { BAD_CREATED, O_RDONLY, 1, RW }, + { BAD_EXCL, O_CREAT | O_EXCL | O_WRONLY, 1, RW }, + { BAD_FLAG, O_RDONLY, 1, RW }, + { BAD_RESERVED, O_RDONLY, 1, RW }, + { BAD_HONORED, O_RDONLY, 1, RW }, + }; + uint64_t forget[] = { 0, nodeid(self->srv, "rw"), FUSE_ROOT_ID }; + int i, released = 0; + + if (!variant->atomic) + SKIP(return, "the server lacks FUSE_OPEN_ATOMIC"); + for (i = 0; i < ARRAY_SIZE(cases); i++) { + self->srv->bad = cases[i].bad; + ASSERT_EQ(-1, open_at("rw", cases[i].flags)); + EXPECT_EQ(EIO, errno); + released += cases[i].release; + EXPECT_EQ(released, self->srv->nr_release) + TH_LOG("bad reply %d", cases[i].bad); + } + /* FORGETs arrive in the order of the replies */ + for (i = 0; i < ARRAY_SIZE(cases); i++) + if (cases[i].forget != NONE) + wait_event(_metadata, self->srv, FUSE_FORGET, + forget[cases[i].forget]); +} + +TEST_F(fuse, open_atomic_enosys) +{ + /* After -ENOSYS the kernel sends FUSE_LOOKUP */ + if (variant->atomic) + SKIP(return, "the server implements FUSE_OPEN_ATOMIC"); + open_close(_metadata, "rw", O_RDONLY); + open_close(_metadata, "a", O_CREAT | O_WRONLY); + + EXPECT_EQ(1, self->srv->nr_enosys); + EXPECT_EQ(0, self->srv->nr_open_atomic); + EXPECT_EQ(2, self->srv->nr_lookup); + EXPECT_EQ(1, self->srv->nr_open); + EXPECT_EQ(1, self->srv->nr_create); +} + +TEST_F(fuse, no_open_mode) +{ + struct stat st; + + /* FUSE_OPEN gets -ENOSYS */ + self->srv->no_open = 1; + ASSERT_EQ(0, stat(MNT "/rw", &st)); + open_close(_metadata, "rw", O_RDONLY); + /* Names not in the dcache take FUSE_LOOKUP and FUSE_CREATE */ + open_close(_metadata, "ro", O_RDONLY); + open_close(_metadata, "a", O_CREAT | O_WRONLY); + + EXPECT_EQ(0, self->srv->nr_enosys); + EXPECT_EQ(0, self->srv->nr_open_atomic); + EXPECT_EQ(1, self->srv->nr_open); + EXPECT_EQ(3, self->srv->nr_lookup); + EXPECT_EQ(1, self->srv->nr_create); +} + +TEST_F(fuse, name_too_long) +{ + char path[1200]; + int len; + + /* Longer than the 1024 byte limit of a server without FUSE_MAX_PAGES */ + len = snprintf(path, sizeof(path), MNT "/"); + memset(path + len, 'x', 1100); + path[len + 1100] = '\0'; + ASSERT_EQ(-1, open(path, O_CREAT | O_WRONLY, 0644)); + EXPECT_EQ(ENAMETOOLONG, errno); + EXPECT_EQ(0, self->srv->nr_enosys); + EXPECT_EQ(0, self->srv->nr_open_atomic); + EXPECT_EQ(0, self->srv->nr_create); +} + +TEST_HARNESS_MAIN -- 2.50.1 ^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH v9 3/3] selftests/fuse: add test for FUSE_OPEN_ATOMIC 2026-11-21 0:16 ` [PATCH v9 3/3] selftests/fuse: add test for FUSE_OPEN_ATOMIC Jimmy Zuber @ 2026-12-03 23:15 ` Bernd Schubert 0 siblings, 0 replies; 11+ messages in thread From: Bernd Schubert @ 2026-12-03 23:15 UTC (permalink / raw) To: Jimmy Zuber, Miklos Szeredi Cc: fuse-devel, linux-kselftest, Shuah Khan, Jim Harris, Horst Birthelmer, Luis Henriques, NeilBrown, Joanne Koong On 11/21/26 01:16, Jimmy Zuber wrote: > Add 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. > > Signed-off-by: Jimmy Zuber <jamz@amazon.com> > --- > .../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 > > diff --git a/tools/testing/selftests/filesystems/fuse/.gitignore b/tools/testing/selftests/filesystems/fuse/.gitignore > index 7c787313e69b6..faac6d969ef94 100644 > --- a/tools/testing/selftests/filesystems/fuse/.gitignore > +++ b/tools/testing/selftests/filesystems/fuse/.gitignore > @@ -1,4 +1,5 @@ > # SPDX-License-Identifier: GPL-2.0-only > +atomic_open_test > fuse_mnt > fusectl_test > test_syncfs > diff --git a/tools/testing/selftests/filesystems/fuse/Makefile b/tools/testing/selftests/filesystems/fuse/Makefile > index e3ae3acff1139..8390d6019513b 100644 > --- a/tools/testing/selftests/filesystems/fuse/Makefile > +++ b/tools/testing/selftests/filesystems/fuse/Makefile > @@ -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 Works, just a bit unusual for the style here to have it on the same line? > 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),) > diff --git a/tools/testing/selftests/filesystems/fuse/atomic_open_test.c b/tools/testing/selftests/filesystems/fuse/atomic_open_test.c > new file mode 100644 > index 0000000000000..51cdf685bf678 > --- /dev/null > +++ b/tools/testing/selftests/filesystems/fuse/atomic_open_test.c > @@ -0,0 +1,920 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* > + * Test the requests that open() sends with and without FUSE_OPEN_ATOMIC, > + * against a raw /dev/fuse server. > + * > + * Requires root to mount fuse. > + */ > +#define _GNU_SOURCE > +#include <errno.h> > +#include <fcntl.h> > +#include <grp.h> > +#include <poll.h> > +#include <pthread.h> > +#include <sched.h> > +#include <stddef.h> > +#include <stdint.h> > +#include <stdio.h> > +#include <string.h> > +#include <unistd.h> > +#include <sys/mman.h> > +#include <sys/mount.h> > +#include <sys/stat.h> > +#include <sys/syscall.h> > +#include <sys/uio.h> > +#include <sys/wait.h> > +#include <linux/errno.h> > +#include <linux/fuse.h> > +#include <linux/openat2.h> > + > +#include "../../kselftest_harness.h" > + > +#define MAX_WRITE (128 * 1024) > +#define MAX_ENTRIES 16 > +#define NOBODY 65534 > +#define MNT "/tmp/mnt" > + > +struct entry { > + uint64_t parent; > + char name[64]; > + uint64_t nodeid; > + uint32_t mode; > + uint64_t size; > +}; > + > +/* Malformed FUSE_OPEN_ATOMIC replies for an existing regular file */ > +enum bad_reply { > + BAD_NONE, > + BAD_SHORT, /* without fuse_open_out */ > + BAD_ZERO_NODEID, > + BAD_ROOT_NODEID, > + BAD_PARTIAL_MASK, /* without all of STATX_BASIC_STATS */ > + BAD_CREATED, /* FUSE_OPEN_ATOMIC_CREATED */ > + BAD_EXCL, /* opened for O_CREAT | O_EXCL */ > + BAD_FLAG, /* an undefined reply flag */ > + BAD_RESERVED, /* a nonzero reserved field */ > + BAD_HONORED, /* an atomic_flags bit not requested */ > +}; > + > +/* Server state, shared with the server thread */ > +struct server { > + int fd; > + /* Written with a struct event for each FORGET and RELEASE */ > + int event_pipe[2]; > + int atomic, atomic_o_trunc; > + /* Protocol minor version of the kernel, from FUSE_INIT */ > + uint32_t minor; > + struct entry ent[MAX_ENTRIES]; > + int nent; > + int nr_lookup, nr_create, nr_open, nr_open_atomic, nr_setattr; > + int nr_release, nr_read, nr_enosys, nr_root_getattr; > + /* Requests that carried O_TRUNC */ > + int nr_trunc; > + /* flags of the last FUSE_OPEN_ATOMIC */ > + uint32_t open_flags; > + /* FUSE_OPEN_ATOMIC requests with other than the expected inputs */ > + int nr_bad_request; > + /* FUSE_OPEN: -ENOSYS */ > + int no_open; > + /* Reply -ENOENT for a missing name without O_CREAT */ > + int enoent; > + /* Reply with only the entry for an existing file */ > + int entry_only; > + /* Reply FUSE_OPEN_ATOMIC_CREATED for an existing file */ > + int created_existing; > + enum bad_reply bad; > +}; > + > +struct event { > + uint32_t opcode; > + uint64_t nodeid; > +}; > + > +static void reply(int fd, uint64_t unique, int error, void *data, size_t len) > +{ > + struct fuse_out_header oh = { > + .len = sizeof(oh) + (data ? len : 0), > + .error = error, > + .unique = unique, > + }; > + struct iovec iov[2] = { { &oh, sizeof(oh) }, { data, len } }; > + > + /* Errors here are teardown races (device closed on unmount) */ > + if (writev(fd, iov, data ? 2 : 1) < 0) > + return; > +} > + > +static void post_event(struct server *s, uint32_t opcode, uint64_t nodeid) > +{ > + struct event ev = { opcode, nodeid }; > + > + /* The test reads the pipe; a full pipe means nobody waits */ > + if (write(s->event_pipe[1], &ev, sizeof(ev)) < 0) > + return; > +} > + > +static struct entry *find_in(struct server *s, uint64_t parent, > + const char *name) > +{ > + int i; > + > + for (i = 0; i < s->nent; i++) > + if (s->ent[i].parent == parent && !strcmp(s->ent[i].name, name)) > + return &s->ent[i]; > + return NULL; > +} > + > +static struct entry *find_node(struct server *s, uint64_t nodeid) > +{ > + int i; > + > + for (i = 0; i < s->nent; i++) > + if (s->ent[i].nodeid == nodeid) > + return &s->ent[i]; > + return NULL; > +} > + > +static struct entry *add_in(struct server *s, uint64_t parent, > + const char *name, uint32_t mode) > +{ > + struct entry *e = &s->ent[s->nent]; > + > + if (s->nent >= MAX_ENTRIES) > + return NULL; > + e->parent = parent; > + snprintf(e->name, sizeof(e->name), "%.63s", name); > + e->nodeid = FUSE_ROOT_ID + 1 + s->nent++; > + e->mode = mode; > + return e; > +} > + > +static void fill_attr(struct fuse_attr *attr, struct entry *e) > +{ > + attr->ino = e->nodeid; > + attr->mode = e->mode; > + attr->nlink = S_ISDIR(e->mode) ? 2 : 1; > + attr->size = e->size; > + attr->blksize = 4096; > +} > + > +/* A NULL entry is a negative entry, cached like a found one */ > +static void fill_entry(struct fuse_entry_out *out, struct entry *e) > +{ > + memset(out, 0, sizeof(*out)); > + out->entry_valid = 3600; > + if (!e) > + return; > + out->nodeid = e->nodeid; > + out->attr_valid = 3600; > + fill_attr(&out->attr, e); > +} > + > +static void fill_open_atomic(struct fuse_open_atomic_out *out, > + struct entry *e) > +{ > + memset(out, 0, sizeof(*out)); > + out->entry_valid = 3600; > + if (!e) > + return; > + out->nodeid = e->nodeid; > + out->attr_valid = 3600; > + out->stat.mask = STATX_BASIC_STATS; > + out->stat.ino = e->nodeid; > + out->stat.mode = e->mode; > + out->stat.nlink = S_ISDIR(e->mode) ? 2 : 1; > + out->stat.size = e->size; > + out->stat.blksize = 4096; > +} > + > +static void do_create(struct server *s, struct fuse_in_header *ih) > +{ > + struct fuse_create_in *in = (void *)(ih + 1); > + const char *name = (char *)(in + 1); > + struct entry *e = find_in(s, ih->nodeid, name); > + struct { > + struct fuse_entry_out entry; > + struct fuse_open_out open; > + } out = {0}; > + > + s->nr_create++; > + if (e && (in->flags & O_EXCL)) > + return reply(s->fd, ih->unique, -EEXIST, NULL, 0); > + if (!e) > + e = add_in(s, ih->nodeid, name, S_IFREG | in->mode); > + if (!e) > + return reply(s->fd, ih->unique, -ENOSPC, NULL, 0); > + fill_entry(&out.entry, e); > + reply(s->fd, ih->unique, 0, &out, sizeof(out)); > +} > + > +static void do_open_atomic(struct server *s, struct fuse_in_header *ih) > +{ > + struct fuse_open_atomic_in *in = (void *)(ih + 1); > + const char *name = (char *)(in + 1); > + struct entry *e = find_in(s, ih->nodeid, name); > + struct fuse_open_atomic_out out; > + size_t len = sizeof(out); > + int excl = (in->flags & O_CREAT) && (in->flags & O_EXCL); > + > + if (!s->atomic) { > + s->nr_enosys++; > + return reply(s->fd, ih->unique, -ENOSYS, NULL, 0); > + } > + s->nr_open_atomic++; > + s->open_flags = in->flags; > + s->nr_bad_request += in->open_flags || in->atomic_flags || > + in->reserved[0] || in->reserved[1] || > + in->sx_mask != STATX_BASIC_STATS; > + if (in->flags & O_TRUNC) > + s->nr_trunc++; > + if (e && excl && s->bad != BAD_EXCL) > + return reply(s->fd, ih->unique, -EEXIST, NULL, 0); > + if (!e && (in->flags & O_CREAT)) { > + e = add_in(s, ih->nodeid, name, S_IFREG | in->mode); > + if (!e) > + return reply(s->fd, ih->unique, -ENOSPC, NULL, 0); > + fill_open_atomic(&out, e); > + out.flags = FUSE_OPEN_ATOMIC_OPENED | FUSE_OPEN_ATOMIC_CREATED; > + } else { > + fill_open_atomic(&out, e); > + if (!e && s->enoent) > + return reply(s->fd, ih->unique, -ENOENT, NULL, 0); > + if (!e || !S_ISREG(e->mode) || s->entry_only) > + return reply(s->fd, ih->unique, 0, &out, len); > + out.flags = FUSE_OPEN_ATOMIC_OPENED; > + if (s->created_existing || s->bad == BAD_CREATED) > + out.flags |= FUSE_OPEN_ATOMIC_CREATED; > + } > + if (s->bad == BAD_SHORT) > + len = offsetof(struct fuse_open_atomic_out, open); > + if (s->bad == BAD_ZERO_NODEID) > + out.nodeid = 0; > + if (s->bad == BAD_ROOT_NODEID) > + out.nodeid = FUSE_ROOT_ID; > + if (s->bad == BAD_HONORED) > + out.atomic_flags = 1; > + if (s->bad == BAD_RESERVED) > + out.reserved = 1; > + if (s->bad == BAD_FLAG) > + out.flags |= 1u << 31; > + if (s->bad == BAD_PARTIAL_MASK) > + out.stat.mask = STATX_TYPE | STATX_MODE; > + reply(s->fd, ih->unique, 0, &out, len); > +} > + > +static void do_init(struct server *s, struct fuse_in_header *ih) > +{ > + struct fuse_init_in *in = (void *)(ih + 1); > + struct fuse_init_out out = {0}; > + > + s->minor = in->minor; > + out.major = FUSE_KERNEL_VERSION; > + out.minor = in->minor < FUSE_KERNEL_MINOR_VERSION ? > + in->minor : FUSE_KERNEL_MINOR_VERSION; > + out.max_readahead = in->max_readahead; > + out.max_write = MAX_WRITE; > + out.max_background = 16; > + out.congestion_threshold = 12; > + if (s->atomic_o_trunc) > + out.flags = FUSE_ATOMIC_O_TRUNC; > + reply(s->fd, ih->unique, 0, &out, sizeof(out)); > +} > + > +/* GETATTR and SETATTR; a size in SETATTR truncates */ > +static void do_attr(struct server *s, struct fuse_in_header *ih) > +{ > + struct fuse_setattr_in *in = (void *)(ih + 1); > + struct entry *e = find_node(s, ih->nodeid); > + struct fuse_attr_out out = { > + .attr_valid = 3600, > + .attr = { .ino = FUSE_ROOT_ID, .mode = S_IFDIR | 0777, > + .nlink = 2 }, > + }; > + > + if (ih->opcode == FUSE_GETATTR && ih->nodeid == FUSE_ROOT_ID) > + s->nr_root_getattr++; > + if (ih->opcode == FUSE_SETATTR) { > + s->nr_setattr++; > + if (e && (in->valid & FATTR_SIZE)) > + e->size = in->size; > + } > + if (e) { > + memset(&out.attr, 0, sizeof(out.attr)); > + fill_attr(&out.attr, e); > + } > + reply(s->fd, ih->unique, 0, &out, sizeof(out)); > +} > + > +static void do_read(struct server *s, struct fuse_in_header *ih) > +{ > + struct fuse_read_in *in = (void *)(ih + 1); > + struct entry *e = find_node(s, ih->nodeid); > + static char data[MAX_WRITE]; > + size_t len = 0; > + > + s->nr_read++; > + if (e && in->offset < e->size) > + len = e->size - in->offset; > + if (len > in->size) > + len = in->size; > + reply(s->fd, ih->unique, 0, data, len); > +} > + > +static void handle(struct server *s, struct fuse_in_header *ih) > +{ > + struct fuse_open_in *open_in = (void *)(ih + 1); > + struct fuse_batch_forget_in *forget_in = (void *)(ih + 1); > + struct fuse_forget_one *one = (void *)(forget_in + 1); > + struct fuse_entry_out entry; > + struct fuse_open_out open = {0}; > + uint32_t i; > + > + switch (ih->opcode) { > + case FUSE_INIT: > + return do_init(s, ih); > + case FUSE_GETATTR: > + case FUSE_SETATTR: > + return do_attr(s, ih); > + case FUSE_LOOKUP: > + s->nr_lookup++; > + if (s->enoent && !find_in(s, ih->nodeid, (char *)(ih + 1))) > + return reply(s->fd, ih->unique, -ENOENT, NULL, 0); > + fill_entry(&entry, find_in(s, ih->nodeid, (char *)(ih + 1))); > + return reply(s->fd, ih->unique, 0, &entry, sizeof(entry)); > + case FUSE_CREATE: > + return do_create(s, ih); > + case FUSE_OPEN_ATOMIC: > + return do_open_atomic(s, ih); > + case FUSE_OPEN: > + s->nr_open++; > + if (s->no_open) > + return reply(s->fd, ih->unique, -ENOSYS, NULL, 0); > + if (open_in->flags & O_TRUNC) { > + s->nr_trunc++; > + find_node(s, ih->nodeid)->size = 0; > + } > + return reply(s->fd, ih->unique, 0, &open, sizeof(open)); > + case FUSE_OPENDIR: > + return reply(s->fd, ih->unique, 0, &open, sizeof(open)); > + case FUSE_READ: > + return do_read(s, ih); > + case FUSE_RELEASE: > + s->nr_release++; > + post_event(s, FUSE_RELEASE, ih->nodeid); > + return reply(s->fd, ih->unique, 0, NULL, 0); > + case FUSE_FLUSH: > + case FUSE_RELEASEDIR: > + return reply(s->fd, ih->unique, 0, NULL, 0); > + case FUSE_FORGET: > + return post_event(s, FUSE_FORGET, ih->nodeid); > + case FUSE_BATCH_FORGET: > + for (i = 0; i < forget_in->count; i++) > + post_event(s, FUSE_FORGET, one[i].nodeid); > + return; > + default: > + return reply(s->fd, ih->unique, -ENOSYS, NULL, 0); > + } > +} > + > +static void *server_thread(void *arg) > +{ > + struct server *s = arg; > + static char buf[MAX_WRITE + 4096]; > + > + for (;;) { > + ssize_t n = read(s->fd, buf, sizeof(buf)); > + > + if (n < 0 && (errno == EINTR || errno == EAGAIN)) > + continue; > + if (n < 0) > + return NULL; /* device closed on unmount */ > + if (n >= (ssize_t)sizeof(struct fuse_in_header)) > + handle(s, (struct fuse_in_header *)buf); > + } > +} > + > +FIXTURE(fuse) > +{ > + struct server *srv; > + int nobody; > +}; > + > +FIXTURE_VARIANT(fuse) > +{ > + int atomic; > + int atomic_o_trunc; > +}; > + > +FIXTURE_VARIANT_ADD(fuse, atomic) > +{ > + .atomic = 1, > +}; > + > +FIXTURE_VARIANT_ADD(fuse, atomic_trunc) > +{ > + .atomic = 1, > + .atomic_o_trunc = 1, > +}; > + > +FIXTURE_VARIANT_ADD(fuse, plain) > +{ > + .atomic = 0, > +}; > + > +static int become_nobody(void) > +{ > + return setgroups(0, NULL) || setgid(NOBODY) || setuid(NOBODY); > +} > + > +static bool can_become_nobody(void) > +{ > + int status; > + pid_t pid = fork(); > + > + if (pid == 0) > + _exit(become_nobody()); > + return pid > 0 && waitpid(pid, &status, 0) == pid && > + WIFEXITED(status) && !WEXITSTATUS(status); > +} > + > +/* > + * Each test runs in its own process and mount namespace: the mount, the > + * server thread and its device go away when the test exits. > + */ > +FIXTURE_SETUP(fuse) > +{ > + struct server *s; > + char opts[128]; > + struct stat st; > + pthread_t t; > + int err; > + > + if (geteuid() != 0) > + SKIP(return, "need root to mount fuse"); > + if (unshare(CLONE_NEWNS) || > + mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL)) > + SKIP(return, "no private mount namespace: %s", strerror(errno)); > + if (mount("tmpfs", "/tmp", "tmpfs", 0, NULL)) > + SKIP(return, "cannot mount tmpfs: %s", strerror(errno)); > + self->nobody = can_become_nobody(); > + > + s = mmap(NULL, sizeof(*s), PROT_READ | PROT_WRITE, > + MAP_SHARED | MAP_ANONYMOUS, -1, 0); > + ASSERT_NE(MAP_FAILED, s); > + self->srv = s; > + ASSERT_EQ(0, pipe(s->event_pipe)); > + ASSERT_EQ(0, fcntl(s->event_pipe[1], F_SETFL, O_NONBLOCK)); > + s->atomic = variant->atomic; > + s->atomic_o_trunc = variant->atomic_o_trunc; > + add_in(s, FUSE_ROOT_ID, "ro", S_IFREG | 0444); > + add_in(s, FUSE_ROOT_ID, "rw", S_IFREG | 0644); > + add_in(s, FUSE_ROOT_ID, "dir", S_IFDIR | 0755); > + > + s->fd = open("/dev/fuse", O_RDWR); > + err = errno; > + if (s->fd < 0) { > + ASSERT_TRUE(err == ENOENT || err == ENODEV || err == ENXIO || > + err == EACCES || err == EPERM); > + SKIP(return, "cannot open /dev/fuse: %s", strerror(err)); > + } > + ASSERT_EQ(0, mkdir(MNT, 0755)); > + snprintf(opts, sizeof(opts), > + "fd=%d,rootmode=40000,user_id=0,group_id=0,default_permissions,allow_other", > + s->fd); > + if (mount("fuse", MNT, "fuse", 0, opts)) { > + err = errno; > + ASSERT_TRUE(err == EPERM || err == EACCES || err == ENODEV); > + SKIP(return, "cannot mount fuse: %s", strerror(err)); > + } > + ASSERT_EQ(0, pthread_create(&t, NULL, server_thread, s)); > + pthread_detach(t); > + /* Wait for FUSE_INIT: the root GETATTR is queued behind it */ > + ASSERT_EQ(0, stat(MNT, &st)); > + if (s->minor < 48) > + SKIP(return, "kernel without FUSE_OPEN_ATOMIC (protocol 7.48)"); > +} > + > +FIXTURE_TEARDOWN(fuse) > +{ > + EXPECT_EQ(0, self->srv->nr_bad_request); > + /* MNT_FORCE aborts the connection, so open files close at exit */ > + umount2(MNT, MNT_FORCE); > +} > + > +/* Open @name as an unprivileged user and return the errno, or 0 */ > +static int open_as_nobody(struct __test_metadata *_metadata, > + const char *name, int flags) > +{ > + char path[128]; > + int status, fd; > + pid_t pid; > + > + snprintf(path, sizeof(path), MNT "/%s", name); > + pid = fork(); > + ASSERT_GE(pid, 0); > + if (pid == 0) { > + if (become_nobody()) > + _exit(255); > + fd = open(path, flags, 0644); > + _exit(fd < 0 ? errno : 0); > + } > + ASSERT_EQ(pid, waitpid(pid, &status, 0)); > + ASSERT_TRUE(WIFEXITED(status)); > + ASSERT_NE(255, WEXITSTATUS(status)); > + return WEXITSTATUS(status); > +} > + > +static int open_at(const char *name, int flags) > +{ > + char path[128]; > + > + snprintf(path, sizeof(path), MNT "/%s", name); > + return open(path, flags, 0644); > +} > + > +static void open_close(struct __test_metadata *_metadata, const char *name, > + int flags) > +{ > + int fd = open_at(name, flags); > + > + ASSERT_GE(fd, 0); > + close(fd); > +} > + > +static uint64_t nodeid(struct server *s, const char *name) > +{ > + return find_in(s, FUSE_ROOT_ID, name)->nodeid; > +} > + > +/* Wait up to ten seconds for the server to receive @opcode for @node */ > +static void wait_event(struct __test_metadata *_metadata, struct server *s, > + uint32_t opcode, uint64_t node) > +{ > + struct pollfd pfd = { .fd = s->event_pipe[0], .events = POLLIN }; > + struct event ev; > + > + do { > + ASSERT_EQ(1, poll(&pfd, 1, 10000)) > + TH_LOG("no opcode %u for nodeid %llu", opcode, > + (unsigned long long)node); > + ASSERT_EQ(sizeof(ev), read(s->event_pipe[0], &ev, sizeof(ev))); > + } while (ev.opcode != opcode || ev.nodeid != node); > +} > + > +/* Drop the dentry of @name, as the server can at any time */ > +static void inval_entry(struct __test_metadata *_metadata, struct server *s, > + const char *name) > +{ > + struct fuse_notify_inval_entry_out out = { > + .parent = FUSE_ROOT_ID, > + .namelen = strlen(name), > + }; > + struct fuse_out_header oh = { > + .len = sizeof(oh) + sizeof(out) + out.namelen + 1, > + .error = FUSE_NOTIFY_INVAL_ENTRY, > + }; > + struct iovec iov[3] = { > + { &oh, sizeof(oh) }, > + { &out, sizeof(out) }, > + { (void *)name, out.namelen + 1 }, > + }; > + > + ASSERT_EQ(oh.len, writev(s->fd, iov, 3)); > +} > + > +/* > + * Read "rw" into the page cache through a file that stays open as @fd, > + * and drop its dentry. > + */ > +static void cache_rw(struct __test_metadata *_metadata, struct server *s, > + int *fd) > +{ > + char buf[16]; > + > + find_in(s, FUSE_ROOT_ID, "rw")->size = 4096; > + *fd = open_at("rw", O_RDONLY); > + ASSERT_GE(*fd, 0); > + ASSERT_EQ(sizeof(buf), pread(*fd, buf, sizeof(buf), 0)); > + inval_entry(_metadata, s, "rw"); > +} > + > +/* Read "rw" through @fd again, and return the number of FUSE_READs */ > +static int reread_rw(struct server *s, int fd) > +{ > + char buf[16]; > + ssize_t n = pread(fd, buf, sizeof(buf), 0); > + > + close(fd); > + return n == sizeof(buf) ? s->nr_read : -1; > +} > + > +TEST_F(fuse, create_new) > +{ > + open_close(_metadata, "a", O_CREAT | O_WRONLY); > + open_close(_metadata, "b", O_CREAT | O_EXCL | O_WRONLY); > + > + EXPECT_EQ(0, self->srv->nr_open); > + EXPECT_EQ(variant->atomic ? 2 : 0, self->srv->nr_open_atomic); > + EXPECT_EQ(variant->atomic ? 0 : 2, self->srv->nr_lookup); > + EXPECT_EQ(variant->atomic ? 0 : 2, self->srv->nr_create); > +} > + > +TEST_F(fuse, create_negative) > +{ > + struct stat st; > + > + /* O_CREAT on a cached negative dentry */ > + ASSERT_EQ(-1, stat(MNT "/a", &st)); > + open_close(_metadata, "a", O_CREAT | O_WRONLY); > + ASSERT_EQ(0, stat(MNT "/a", &st)); > + EXPECT_EQ(1, self->srv->nr_lookup); > + EXPECT_EQ(variant->atomic ? 1 : 0, self->srv->nr_open_atomic); > + EXPECT_EQ(variant->atomic ? 0 : 1, self->srv->nr_create); > +} > + > +TEST_F(fuse, create_rejected) > +{ > + struct stat st; > + int getattr; > + > + /* The server created the name of a rejected reply */ > + if (!variant->atomic) > + SKIP(return, "the server lacks FUSE_OPEN_ATOMIC"); > + ASSERT_EQ(-1, stat(MNT "/a", &st)); > + self->srv->bad = BAD_FLAG; > + ASSERT_EQ(-1, open_at("a", O_CREAT | O_WRONLY)); > + EXPECT_EQ(EIO, errno); > + /* The kernel refetches the name and the directory attributes */ > + getattr = self->srv->nr_root_getattr; > + ASSERT_EQ(0, stat(MNT, &st)); > + EXPECT_EQ(getattr + 1, self->srv->nr_root_getattr); > + ASSERT_EQ(0, stat(MNT "/a", &st)); > + EXPECT_EQ(2, self->srv->nr_lookup); > +} > + > +TEST_F(fuse, create_existing) > +{ > + if (!self->nobody) > + SKIP(return, "cannot switch to uid %d", NOBODY); > + ASSERT_EQ(-1, open_at("ro", O_CREAT | O_EXCL | O_WRONLY)); > + EXPECT_EQ(EEXIST, errno); > + /* may_open() runs after the reply, and fput() releases the file */ > + EXPECT_EQ(EACCES, open_as_nobody(_metadata, "ro", O_CREAT | O_RDWR)); > + if (variant->atomic) > + wait_event(_metadata, self->srv, FUSE_RELEASE, > + nodeid(self->srv, "ro")); > + EXPECT_EQ(0, self->srv->nr_create); > +} > + > +TEST_F(fuse, read_only) > +{ > + ASSERT_EQ(0, mount(NULL, MNT, NULL, > + MS_REMOUNT | MS_BIND | MS_RDONLY, NULL)); > + /* An existing name gives EEXIST, not EROFS */ > + ASSERT_EQ(-1, open_at("rw", O_CREAT | O_EXCL | O_WRONLY)); > + EXPECT_EQ(EEXIST, errno); > + ASSERT_EQ(-1, open_at("ro", O_RDONLY | O_TRUNC)); > + EXPECT_EQ(EROFS, errno); > + > + EXPECT_EQ(0, self->srv->nr_create); > + if (variant->atomic_o_trunc) { > + EXPECT_EQ(0, self->srv->nr_open_atomic); > + EXPECT_EQ(2, self->srv->nr_lookup); > + } else { > + EXPECT_EQ(variant->atomic ? 1 : 0, self->srv->nr_open_atomic); > + EXPECT_EQ(variant->atomic ? 1 : 2, self->srv->nr_lookup); > + } > +} > + > +TEST_F(fuse, open_existing) > +{ > + if (!self->nobody) > + SKIP(return, "cannot switch to uid %d", NOBODY); > + /* may_open() runs after the reply, and fput() releases the file */ > + EXPECT_EQ(EACCES, open_as_nobody(_metadata, "ro", O_RDWR)); > + if (variant->atomic) > + wait_event(_metadata, self->srv, FUSE_RELEASE, > + nodeid(self->srv, "ro")); > + open_close(_metadata, "rw", O_RDONLY); > + ASSERT_EQ(-1, open_at("missing", O_RDONLY)); > + EXPECT_EQ(ENOENT, errno); > + /* A zero nodeid is cached like a negative LOOKUP */ > + ASSERT_EQ(-1, open_at("missing", O_RDONLY)); > + EXPECT_EQ(ENOENT, errno); > + > + EXPECT_EQ(variant->atomic ? 0 : 1, self->srv->nr_open); > + EXPECT_EQ(variant->atomic ? 3 : 0, self->srv->nr_open_atomic); > + EXPECT_EQ(variant->atomic ? 0 : 3, self->srv->nr_lookup); > +} > + > +TEST_F(fuse, open_enoent) > +{ > + struct stat st; > + > + /* -ENOENT gives what FUSE_LOOKUP gives: no negative dentry cached */ > + self->srv->enoent = 1; > + ASSERT_EQ(-1, open_at("missing", O_RDONLY)); > + EXPECT_EQ(ENOENT, errno); > + ASSERT_EQ(-1, stat(MNT "/missing", &st)); > + EXPECT_EQ(ENOENT, errno); > + EXPECT_EQ(variant->atomic ? 1 : 0, self->srv->nr_open_atomic); > + EXPECT_EQ(variant->atomic ? 1 : 2, self->srv->nr_lookup); > +} > + > +TEST_F(fuse, open_trunc) > +{ > + open_close(_metadata, "rw", O_WRONLY | O_TRUNC); > + /* As a shell "> ro" */ > + open_close(_metadata, "ro", O_CREAT | O_TRUNC | O_WRONLY); > + > + EXPECT_EQ(0, self->srv->nr_create); > + if (variant->atomic && !variant->atomic_o_trunc) { > + /* The server opens the file and SETATTR truncates it */ > + EXPECT_EQ(2, self->srv->nr_open_atomic); > + EXPECT_EQ(0, self->srv->nr_lookup); > + EXPECT_EQ(0, self->srv->nr_open); > + EXPECT_EQ(0, self->srv->nr_trunc); > + EXPECT_EQ(2, self->srv->nr_setattr); > + } else { > + /* FUSE_OPEN truncates with FUSE_ATOMIC_O_TRUNC, after LOOKUP */ > + EXPECT_EQ(0, self->srv->nr_open_atomic); > + EXPECT_EQ(2, self->srv->nr_lookup); > + EXPECT_EQ(2, self->srv->nr_open); > + EXPECT_EQ(variant->atomic_o_trunc ? 2 : 0, self->srv->nr_trunc); > + EXPECT_EQ(variant->atomic_o_trunc ? 0 : 2, > + self->srv->nr_setattr); > + } > +} > + > +TEST_F(fuse, open_directory) > +{ > + /* The server returns the entry only, and the VFS opens it */ > + open_close(_metadata, "dir", O_RDONLY); > + EXPECT_EQ(variant->atomic ? 1 : 0, self->srv->nr_open_atomic); > + EXPECT_EQ(variant->atomic ? 0 : 1, self->srv->nr_lookup); > +} > + > +TEST_F(fuse, open_o_directory) > +{ > + /* For O_DIRECTORY the kernel sends FUSE_LOOKUP */ > + open_close(_metadata, "dir", O_RDONLY | O_DIRECTORY); > + ASSERT_EQ(-1, open_at("rw", O_RDONLY | O_DIRECTORY)); > + EXPECT_EQ(ENOTDIR, errno); > + EXPECT_EQ(0, self->srv->nr_open_atomic); > + EXPECT_EQ(2, self->srv->nr_lookup); > + EXPECT_EQ(0, self->srv->nr_open); > +} > + > +TEST_F(fuse, open_entry_only) > +{ > + /* The server returns the entry only; FUSE_OPEN opens the file */ > + self->srv->entry_only = 1; > + open_close(_metadata, "rw", O_RDONLY); > + EXPECT_EQ(variant->atomic ? 1 : 0, self->srv->nr_open_atomic); > + EXPECT_EQ(variant->atomic ? 0 : 1, self->srv->nr_lookup); > + EXPECT_EQ(1, self->srv->nr_open); > +} > + > +TEST_F(fuse, open_regular) > +{ > + struct open_how how = { .flags = O_RDONLY | OPENAT2_REGULAR }; > + uint32_t flags; > + int fd; > + > + /* OPENAT2_REGULAR is not part of the request */ > + if (!variant->atomic) > + SKIP(return, "the server lacks FUSE_OPEN_ATOMIC"); > + fd = syscall(__NR_openat2, AT_FDCWD, MNT "/rw", &how, sizeof(how)); > + if (fd < 0 && (errno == EINVAL || errno == ENOSYS) && > + !self->srv->nr_open_atomic) > + SKIP(return, "openat2 without OPENAT2_REGULAR"); > + ASSERT_GE(fd, 0); > + close(fd); > + flags = self->srv->open_flags; > + inval_entry(_metadata, self->srv, "rw"); > + open_close(_metadata, "rw", O_RDONLY); > + EXPECT_EQ(self->srv->open_flags, flags); > + EXPECT_EQ(2, self->srv->nr_open_atomic); > + /* The VFS refuses a directory */ > + fd = syscall(__NR_openat2, AT_FDCWD, MNT "/dir", &how, sizeof(how)); > + if (fd >= 0) > + close(fd); > + EXPECT_EQ(-1, fd); > + EXPECT_EQ(EFTYPE, errno); > + EXPECT_EQ(3, self->srv->nr_open_atomic); > +} > + > +TEST_F(fuse, open_drops_cache) > +{ > + int fd; > + > + cache_rw(_metadata, self->srv, &fd); > + /* An open without FOPEN_KEEP_CACHE drops the cached pages */ > + open_close(_metadata, "rw", O_RDONLY); > + EXPECT_EQ(2, reread_rw(self->srv, fd)); > + /* Each open is one FUSE_OPEN_ATOMIC, or FUSE_LOOKUP and FUSE_OPEN */ > + EXPECT_EQ(variant->atomic ? 0 : 2, self->srv->nr_open); > + EXPECT_EQ(variant->atomic ? 2 : 0, self->srv->nr_open_atomic); > +} > + > +TEST_F(fuse, create_drops_cache) > +{ > + int fd; > + > + cache_rw(_metadata, self->srv, &fd); > + self->srv->created_existing = 1; > + open_close(_metadata, "rw", O_CREAT | O_RDWR); > + EXPECT_EQ(2, reread_rw(self->srv, fd)); > +} > + > +TEST_F(fuse, bad_replies) > +{ > + /* A parsed reply with a nonzero nodeid takes a lookup reference */ > + enum { NONE, RW, ROOT }; > + static const struct { > + enum bad_reply bad; > + int flags; > + int release; > + int forget; > + } cases[] = { > + { BAD_SHORT, O_RDONLY, 0, NONE }, > + { BAD_ZERO_NODEID, O_RDONLY, 1, NONE }, > + { BAD_ROOT_NODEID, O_RDONLY, 1, ROOT }, > + { BAD_PARTIAL_MASK, O_RDONLY, 1, RW }, > + { BAD_CREATED, O_RDONLY, 1, RW }, > + { BAD_EXCL, O_CREAT | O_EXCL | O_WRONLY, 1, RW }, > + { BAD_FLAG, O_RDONLY, 1, RW }, > + { BAD_RESERVED, O_RDONLY, 1, RW }, > + { BAD_HONORED, O_RDONLY, 1, RW }, > + }; > + uint64_t forget[] = { 0, nodeid(self->srv, "rw"), FUSE_ROOT_ID }; > + int i, released = 0; > + > + if (!variant->atomic) > + SKIP(return, "the server lacks FUSE_OPEN_ATOMIC"); > + for (i = 0; i < ARRAY_SIZE(cases); i++) { > + self->srv->bad = cases[i].bad; > + ASSERT_EQ(-1, open_at("rw", cases[i].flags)); > + EXPECT_EQ(EIO, errno); > + released += cases[i].release; > + EXPECT_EQ(released, self->srv->nr_release) > + TH_LOG("bad reply %d", cases[i].bad); > + } > + /* FORGETs arrive in the order of the replies */ > + for (i = 0; i < ARRAY_SIZE(cases); i++) > + if (cases[i].forget != NONE) > + wait_event(_metadata, self->srv, FUSE_FORGET, > + forget[cases[i].forget]); > +} > + > +TEST_F(fuse, open_atomic_enosys) > +{ > + /* After -ENOSYS the kernel sends FUSE_LOOKUP */ > + if (variant->atomic) > + SKIP(return, "the server implements FUSE_OPEN_ATOMIC"); > + open_close(_metadata, "rw", O_RDONLY); > + open_close(_metadata, "a", O_CREAT | O_WRONLY); > + > + EXPECT_EQ(1, self->srv->nr_enosys); > + EXPECT_EQ(0, self->srv->nr_open_atomic); > + EXPECT_EQ(2, self->srv->nr_lookup); > + EXPECT_EQ(1, self->srv->nr_open); > + EXPECT_EQ(1, self->srv->nr_create); > +} > + > +TEST_F(fuse, no_open_mode) > +{ > + struct stat st; > + > + /* FUSE_OPEN gets -ENOSYS */ > + self->srv->no_open = 1; > + ASSERT_EQ(0, stat(MNT "/rw", &st)); > + open_close(_metadata, "rw", O_RDONLY); > + /* Names not in the dcache take FUSE_LOOKUP and FUSE_CREATE */ > + open_close(_metadata, "ro", O_RDONLY); > + open_close(_metadata, "a", O_CREAT | O_WRONLY); > + > + EXPECT_EQ(0, self->srv->nr_enosys); > + EXPECT_EQ(0, self->srv->nr_open_atomic); > + EXPECT_EQ(1, self->srv->nr_open); > + EXPECT_EQ(3, self->srv->nr_lookup); > + EXPECT_EQ(1, self->srv->nr_create); > +} > + > +TEST_F(fuse, name_too_long) > +{ > + char path[1200]; Given Christian recently found issues for readdir and long names, maybe 4095 characters? > + int len; > + > + /* Longer than the 1024 byte limit of a server without FUSE_MAX_PAGES */ > + len = snprintf(path, sizeof(path), MNT "/"); > + memset(path + len, 'x', 1100); > + path[len + 1100] = '\0'; > + ASSERT_EQ(-1, open(path, O_CREAT | O_WRONLY, 0644)); > + EXPECT_EQ(ENAMETOOLONG, errno); > + EXPECT_EQ(0, self->srv->nr_enosys); > + EXPECT_EQ(0, self->srv->nr_open_atomic); > + EXPECT_EQ(0, self->srv->nr_create); > +} > + > +TEST_HARNESS_MAIN I think at some point we should probably create a generic framework server or server_common.c that already does most of the fuse init/mount functionality - for sure not for this series. LGTM. Reviewed-by: Bernd Schubert <bernd@bsbernd.com> ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v9 0/3] fuse: atomic open 2026-11-21 0:16 [PATCH v9 0/3] fuse: atomic open Jimmy Zuber ` (2 preceding siblings ...) 2026-11-21 0:16 ` [PATCH v9 3/3] selftests/fuse: add test for FUSE_OPEN_ATOMIC Jimmy Zuber @ 2026-12-03 15:27 ` Jimmy Zuber 2026-12-03 15:33 ` Bernd Schubert 3 siblings, 1 reply; 11+ messages in thread From: Jimmy Zuber @ 2026-12-03 15:27 UTC (permalink / raw) To: Miklos Szeredi Cc: fuse-devel, linux-kselftest, Shuah Khan, Bernd Schubert, Jim Harris, Horst Birthelmer, Luis Henriques, NeilBrown, Joanne Koong Hi Miklos, Friendly ping on this series. I've iterated based on Bernd's feedback to add dedicated types and a more extensible reply format. I'm hoping this shape can enable a richer create/open capability without depending on full compounds. We ought to be able to extend this operation rather than adding many new operations or flags in the future. Also happy to rebase if desired. Thanks, Jimmy ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v9 0/3] fuse: atomic open 2026-12-03 15:27 ` [PATCH v9 0/3] fuse: atomic open Jimmy Zuber @ 2026-12-03 15:33 ` Bernd Schubert 2026-12-03 20:03 ` Jimmy Zuber 0 siblings, 1 reply; 11+ messages in thread From: Bernd Schubert @ 2026-12-03 15:33 UTC (permalink / raw) To: Jimmy Zuber, Miklos Szeredi Cc: fuse-devel, linux-kselftest, Shuah Khan, Jim Harris, Horst Birthelmer, Luis Henriques, NeilBrown, Joanne Koong On 12/3/26 16:27, Jimmy Zuber wrote: > Hi Miklos, > > Friendly ping on this series. I've iterated based on Bernd's feedback to > add dedicated types and a more extensible reply format. I'm hoping this > shape can enable a richer create/open capability without depending on > full compounds. We ought to be able to extend this operation rather than > adding many new operations or flags in the future. Also happy to rebase > if desired. Review comes today, sorry for the delay. Do you happen to have a libfuse patch? Thanks, Bernd ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v9 0/3] fuse: atomic open 2026-12-03 15:33 ` Bernd Schubert @ 2026-12-03 20:03 ` Jimmy Zuber 2026-12-03 23:09 ` Bernd Schubert 0 siblings, 1 reply; 11+ messages in thread From: Jimmy Zuber @ 2026-12-03 20:03 UTC (permalink / raw) To: Bernd Schubert Cc: Miklos Szeredi, fuse-devel, linux-kselftest, Shuah Khan, Jim Harris, Horst Birthelmer, Luis Henriques, NeilBrown, Joanne Koong On Thu, 3 Dec 2026, Bernd Schubert wrote: > Review comes today, sorry for the delay. Do you happen to have a libfuse > patch? Thanks Bernd! I've authored a PR for libfuse here: https://github.com/libfuse/libfuse/pull/1689 Jimmy ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v9 0/3] fuse: atomic open 2026-12-03 20:03 ` Jimmy Zuber @ 2026-12-03 23:09 ` Bernd Schubert 0 siblings, 0 replies; 11+ messages in thread From: Bernd Schubert @ 2026-12-03 23:09 UTC (permalink / raw) To: Jimmy Zuber Cc: Miklos Szeredi, fuse-devel, linux-kselftest, Shuah Khan, Jim Harris, Horst Birthelmer, Luis Henriques, NeilBrown, Joanne Koong On 12/3/26 21:03, Jimmy Zuber wrote: > On Thu, 3 Dec 2026, Bernd Schubert wrote: >> Review comes today, sorry for the delay. Do you happen to have a libfuse >> patch? > > Thanks Bernd! > > I've authored a PR for libfuse here: > https://github.com/libfuse/libfuse/pull/1689 Great, thank you! I will try to find some time before Monday to use FUSE_OPEN_ATOMIC and to create a new version for OPEN+GETATTR with that. Thanks, Bernd ^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2026-12-03 23:15 UTC | newest] Thread overview: 11+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 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-25 23:31 ` Bernd Schubert 2026-11-21 0:16 ` [PATCH v9 2/3] fuse: add FUSE_OPEN_ATOMIC Jimmy Zuber 2026-12-03 22:51 ` Bernd Schubert 2026-11-21 0:16 ` [PATCH v9 3/3] selftests/fuse: add test for FUSE_OPEN_ATOMIC Jimmy Zuber 2026-12-03 23:15 ` Bernd Schubert 2026-12-03 15:27 ` [PATCH v9 0/3] fuse: atomic open Jimmy Zuber 2026-12-03 15:33 ` Bernd Schubert 2026-12-03 20:03 ` Jimmy Zuber 2026-12-03 23:09 ` Bernd Schubert
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.