fuse: atomic open
Subsystems (2)
cover fuse: atomic open
From: Jimmy Zuber <[email protected]>
Add FUSE_OPEN_ATOMIC, allowing the kernel to skip LOOKUP to improve performance. A server implementing FUSE_OPEN_ATOMIC will look up the name, create it if O_CREAT is supplied, and open it in one request.
The reply says whether the file was created, as Miklos suggested in [1]. The overall pattern follows his sketch in [2]. For a cached name, the kernel compares the returned nodeid with the cached one and looks the name up again on a mismatch.
A server without FUSE_OPEN_ATOMIC replies -ENOSYS and keeps today's behaviour.
Tested with a new selftest under UML and KVM.
[1] https://lore.kernel.org/all/CAJfpeguEK4JsFo60=yHkpLL6XkDGS7cQ5GOhiFL379KsYk0TsQ@mail.gmail.com/
[2] https://lore.kernel.org/all/CAJfpegvXtXY=Pbxv+dMGFR8mvWN0DUwhSo6NwaVexk6Y6sao+w@mail.gmail.com/
Jimmy Zuber (3):
fuse: add FUSE_OPEN_ATOMIC
fuse: open cached files with FUSE_OPEN_ATOMIC
selftests/fuse: add test for FUSE_OPEN_ATOMIC
fs/fuse/dir.c | 359 +++++++++++-- fs/fuse/file.c | 4 +- fs/fuse/fuse_i.h | 4 + include/uapi/linux/fuse.h | 8 +- .../selftests/filesystems/fuse/.gitignore | 1 + .../selftests/filesystems/fuse/Makefile | 3 +- .../filesystems/fuse/atomic_open_test.c | 473 ++++++++++++++++++ 7 files changed, 797 insertions(+), 55 deletions(-) create mode 100644 tools/testing/selftests/filesystems/fuse/atomic_open_test.c
base-commit: b776cbe24cc1b79c7f5101762b0069b3f48b6a4b
Signature · 1 line
2.50.1
PATCH 1/3 fuse: add FUSE_OPEN_ATOMIC
From: Jimmy Zuber <[email protected]>
Add FUSE_OPEN_ATOMIC, allowing the kernel to skip LOOKUP to improve performance. A server implementing FUSE_OPEN_ATOMIC will look up the name, create it if O_CREAT is supplied, and open it in one request.
The server replies with the entry and the open file. If the file was created, the server returns FOPEN_CREATED, or just the entry if it was not created. If the name does not exist, the server returns a zero nodeid, cacheable like a negative LOOKUP. An existing file opened with O_TRUNC is returned unopened, so that truncation can be performed after permissions are checked.
FMODE_CREATED is set only when the server reports that it created the file. Therefore a file created by someone else between LOOKUP and CREATE is no longer opened without a permission check.
A server without the request replies -ENOSYS, and the kernel falls back to FUSE_LOOKUP, keeping backwards compatibility.
--- fs/fuse/dir.c | 112 +++++++++++++++++++++++++++++++++++--- fs/fuse/fuse_i.h | 3 + include/uapi/linux/fuse.h | 8 ++- 3 files changed, 113 insertions(+), 10 deletions(-)
fs/fuse/dir.c +103 -9
@@ -815,7 +815,47 @@ static void free_ext_value(struct fuse_args *args) }/* -* Atomic create+open operation +* FUSE_OPEN_ATOMIC found an existing name and left the open to the VFS. A +* zero nodeid means the name does not exist. +*/ +static int fuse_open_atomic_found (struct inode *dir,struct dentry *entry, +struct file *file, +struct fuse_entry_out *outarg, +struct fuse_forget_link *forget,int epoch, +u64 attr_version,u64 evict_ctr) +{ +struct fuse_conn *fc =get_fuse_conn (dir); +struct inode *inode =NULL ; +struct dentry *res; + +if (outarg->nodeid) { + inode =fuse_iget (dir->i_sb, outarg->nodeid, outarg->generation, + &outarg->attr,ATTR_TIMEOUT (outarg), + attr_version, evict_ctr); +if (!inode) { +fuse_chan_queue_forget (fc->chan, forget, + outarg->nodeid,1 ); +return -ENOMEM ; + } + } +kfree (forget); +if (inode &&get_node_id (inode) ==FUSE_ROOT_ID ) { +iput (inode); +return -EIO ; + } + +d_drop (entry); + res =d_splice_alias (inode, entry); +if (!IS_ERR (res)) { +fuse_dentry_set_epoch (res ?: entry, epoch); +fuse_change_entry_timeout (res ?: entry, outarg); + } +return finish_no_open (file, res); +} + +/* +* Atomic create+open operation, or lookup with optional create and open for +* FUSE_OPEN_ATOMIC * * If the filesystem doesn't support this, then fall back to separate * 'mknod' + 'open' requests.
@@ -833,8 +873,12 @@ static int fuse_create_open(struct mnt_idmap *idmap, struct inode *dir,struct fuse_entry_out outentry;struct fuse_inode *fi;struct fuse_file *ff; +u64 attr_version, evict_ctr;int epoch, err;bool trunc = flags &O_TRUNC ; +bool atomic = opcode ==FUSE_OPEN_ATOMIC ; +bool create = !atomic || (flags &O_CREAT ); +bool locked, created;/* Userspace expects S_IFREG in create mode */ BUG_ON ((mode &S_IFMT ) !=S_IFREG );
@@ -878,26 +922,62 @@ static int fuse_create_open(struct mnt_idmap *idmap, struct inode *dir, outopenp = &ff->args->open_outarg; args.out_args[1 ].size =sizeof (*outopenp); args.out_args[1 ].value = outopenp; +/* FUSE_OPEN_ATOMIC leaves out fuse_open_out if it did not open */ + args.out_argvar = atomic; - err =get_create_ext (idmap, &args, dir, entry, mode); -if (err) - goto out_free_ff; +if (create) { + err =get_create_ext (idmap, &args, dir, entry, mode); +if (err) + goto out_free_ff; + }else { + idmap = &invalid_mnt_idmap; + } + attr_version =fuse_get_attr_version (fm->fc); + evict_ctr =fuse_get_evict_ctr (fm->fc); + +/* Without O_CREAT the parent is locked shared, as for a lookup */ + locked = !create &&fuse_lock_inode (dir); err =fuse_simple_idmap_request (idmap, fm, &args); +fuse_unlock_inode (dir, locked);free_ext_value (&args); -if (err) +if (err <0 ) goto out_free_ff; err = -EIO ; +if (atomic && !args.out_args[1 ].size) { +/* A zero nodeid: the name does not exist and was not created */ +if (!outentry.nodeid && create) + goto out_free_ff; +if (outentry.nodeid && (invalid_nodeid (outentry.nodeid) || +fuse_invalid_attr (&outentry.attr))) + goto out_free_ff; +fuse_file_free (ff); +return fuse_open_atomic_found (dir, entry, file, &outentry, + forget, epoch, attr_version, + evict_ctr); + } +if (!S_ISREG (outentry.attr.mode) ||invalid_nodeid (outentry.nodeid) || -fuse_invalid_attr (&outentry.attr)) +fuse_invalid_attr (&outentry.attr) || + args.out_args[1 ].size !=sizeof (*outopenp)) goto out_free_ff; + created = !atomic || (outopenp->open_flags &FOPEN_CREATED ); +if (atomic) { +/* Only O_CREAT creates; O_TRUNC opens are left to the VFS */ +if ((created && !create) || (!created && trunc)) + goto out_free_ff; +if (created) + file->f_mode |=FMODE_CREATED ; + } + ff->fh = outopenp->fh; ff->nodeid = outentry.nodeid; ff->open_flags = outopenp->open_flags; inode =fuse_iget (dir->i_sb, outentry.nodeid, outentry.generation, - &outentry.attr,ATTR_TIMEOUT (&outentry),0 ,0 ); + &outentry.attr,ATTR_TIMEOUT (&outentry), + created ?0 : attr_version, created ?0 : evict_ctr);if (!inode) { flags &= ~(O_CREAT |O_EXCL |O_TRUNC );fuse_sync_release (NULL , ff, flags);
@@ -906,10 +986,14 @@ static int fuse_create_open(struct mnt_idmap *idmap, struct inode *dir, goto out_err; }kfree (forget); -d_instantiate (entry, inode); +if (d_in_lookup (entry)) +d_splice_alias (inode, entry); +else +d_instantiate (entry, inode);fuse_dentry_set_epoch (entry, epoch);fuse_change_entry_timeout (entry, &outentry); -fuse_dir_changed (dir); +if (created) +fuse_dir_changed (dir); err =generic_file_open (inode, file);if (!err) { file->private_data = ff;
@@ -947,6 +1031,16 @@ static int fuse_atomic_open(struct inode *dir, struct dentry *entry,if (fuse_is_bad (dir))return -EIO ; +if (!fc->no_open_atomic && (d_in_lookup (entry) || (flags &O_CREAT ))) { + err =fuse_create_open (idmap, dir, entry, file, flags, + mode |S_IFREG ,FUSE_OPEN_ATOMIC ); +if (err == -EEXIST ) +fuse_invalidate_entry (entry); +if (err != -ENOSYS ) +return err; + fc->no_open_atomic =1 ; + } +if (d_in_lookup (entry)) {struct dentry *res =fuse_lookup (dir, entry,0 );if (res ||d_really_is_positive (entry))
fs/fuse/fuse_i.h +3 -0
@@ -589,6 +589,9 @@ struct fuse_conn {/** @no_create: Is create not implemented by fs? */ unsigned no_create:1; +/** @no_open_atomic: Is open atomic not implemented by fs? */ + unsigned no_open_atomic:1; +/** @no_bmap: Is bmap not implemented by fs? */ unsigned no_bmap:1;
include/uapi/linux/fuse.h +7 -1
@@ -251,6 +251,9 @@* * 7.47 * - add FUSE_HAS_SYNCFS opt-in flag for privileged userspace servers +* +* 7.48 +* - add FUSE_OPEN_ATOMIC and FOPEN_CREATED */ #ifndef _LINUX_FUSE_H
@@ -286,7 +289,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
@@ -400,6 +403,7 @@ struct fuse_file_lock {* (FUSE_URING_ZERO_COPY) and the request carries page * payload. Otherwise reads/writes fall back to * copying. +* FOPEN_CREATED: FUSE_OPEN_ATOMIC created the file */ #define FOPEN_DIRECT_IO (1 << 0)#define FOPEN_KEEP_CACHE (1 << 1)
@@ -410,6 +414,7 @@ struct fuse_file_lock {#define FOPEN_PARALLEL_DIRECT_WRITES (1 << 6)#define FOPEN_PASSTHROUGH (1 << 7)#define FOPEN_IO_URING_ZERO_COPY (1 << 8) +#define FOPEN_CREATED (1 << 9)/** * INIT request/reply flags
@@ -690,6 +695,7 @@ enum fuse_opcode {FUSE_TMPFILE =51 ,FUSE_STATX =52 ,FUSE_COPY_FILE_RANGE_64 =53 , +FUSE_OPEN_ATOMIC =54 ,/* CUSE specific operations */ CUSE_INIT = 4096,
Signature · 1 line
2.50.1
Re: [PATCH 1/3] fuse: add FUSE_OPEN_ATOMIC
Quoted 58 lines · jamz
From: Jimmy Zuber <[email protected]>
Add
FUSE_OPEN_ATOMIC, allowing the kernel to skip LOOKUP to improve performance. A server implementingFUSE_OPEN_ATOMICwill look up the name, create it ifO_CREATis supplied, and open it in one request.The server replies with the entry and the open file. If the file was created, the server returns
FOPEN_CREATED, or just the entry if it was not created. If the name does not exist, the server returns a zero nodeid, cacheable like a negative LOOKUP. An existing file opened withO_TRUNCis returned unopened, so that truncation can be performed after permissions are checked.
FMODE_CREATEDis set only when the server reports that it created the file. Therefore a file created by someone else between LOOKUP and CREATE is no longer opened without a permission check.A server without the request replies
-ENOSYS, and the kernel falls back toFUSE_LOOKUP, keeping backwards compatibility.Assisted-by Claude:claude-opus-5-5 [Claude-Code]Signed-off-by Jimmy Zuber <[email protected]>--- fs/fuse/dir.c | 112 +++++++++++++++++++++++++++++++++++--- fs/fuse/fuse_i.h | 3 + include/uapi/linux/fuse.h | 8 ++- 3 files changed, 113 insertions(+), 10 deletions(-)fs/fuse/dir.c +21
-1@@ -815,7 +815,47 @@ static void free_ext_value(struct fuse_args *args) }/* -* Atomic create+open operation +* FUSE_OPEN_ATOMIC found an existing name and left the open to the VFS. A +* zero nodeid means the name does not exist. +*/ +static int fuse_open_atomic_found (struct inode *dir,struct dentry *entry, +struct file *file, +struct fuse_entry_out *outarg, +struct fuse_forget_link *forget,int epoch, +u64 attr_version,u64 evict_ctr) +{ +struct fuse_conn *fc =get_fuse_conn (dir); +struct inode *inode =NULL ; +struct dentry *res; + +if (outarg->nodeid) { + inode =fuse_iget (dir->i_sb, outarg->nodeid, outarg->generation, + &outarg->attr,ATTR_TIMEOUT (outarg), + attr_version, evict_ctr); +if (!inode) { +fuse_chan_queue_forget (fc->chan, forget, + outarg->nodeid,1 ); +return -ENOMEM ;
fs/fuse/dir.c static void free_ext_value(struct fuse_args *args) · line 848
Quoted 10 lines
fs/fuse/dir.c +10
-0+ } + } +kfree (forget); +if (inode &&get_node_id (inode) ==FUSE_ROOT_ID ) { +iput (inode); +return -EIO ; + } + +d_drop (entry); + res =d_splice_alias (inode, entry);
As of f1ac607f786f d_splice_alias() handles hashed dentries and the d_drop() is not needed and not wanted.
Quoted 96 lines
fs/fuse/dir.c +56
-6+if (!IS_ERR (res)) { +fuse_dentry_set_epoch (res ?: entry, epoch); +fuse_change_entry_timeout (res ?: entry, outarg); + } +return finish_no_open (file, res); +} + +/* +* Atomic create+open operation, or lookup with optional create and open for +* FUSE_OPEN_ATOMIC * * If the filesystem doesn't support this, then fall back to separate * 'mknod' + 'open' requests. @@ -833,8 +873,12 @@ static int fuse_create_open(struct mnt_idmap *idmap, struct inode *dir,struct fuse_entry_out outentry;struct fuse_inode *fi;struct fuse_file *ff; +u64 attr_version, evict_ctr;int epoch, err;bool trunc = flags &O_TRUNC ; +bool atomic = opcode ==FUSE_OPEN_ATOMIC ; +bool create = !atomic || (flags &O_CREAT ); +bool locked, created;/* Userspace expects S_IFREG in create mode */ BUG_ON ((mode &S_IFMT ) !=S_IFREG );@@ -878,26 +922,62 @@ static int fuse_create_open(struct mnt_idmap *idmap, struct inode *dir, outopenp = &ff->args->open_outarg; args.out_args[1 ].size =sizeof (*outopenp); args.out_args[1 ].value = outopenp; +/* FUSE_OPEN_ATOMIC leaves out fuse_open_out if it did not open */ + args.out_argvar = atomic; - err =get_create_ext (idmap, &args, dir, entry, mode); -if (err) - goto out_free_ff; +if (create) { + err =get_create_ext (idmap, &args, dir, entry, mode); +if (err) + goto out_free_ff; + }else { + idmap = &invalid_mnt_idmap; + } + attr_version =fuse_get_attr_version (fm->fc); + evict_ctr =fuse_get_evict_ctr (fm->fc); + +/* Without O_CREAT the parent is locked shared, as for a lookup */ + locked = !create &&fuse_lock_inode (dir); err =fuse_simple_idmap_request (idmap, fm, &args); +fuse_unlock_inode (dir, locked);free_ext_value (&args); -if (err) +if (err <0 ) goto out_free_ff; err = -EIO ; +if (atomic && !args.out_args[1 ].size) { +/* A zero nodeid: the name does not exist and was not created */ +if (!outentry.nodeid && create) + goto out_free_ff; +if (outentry.nodeid && (invalid_nodeid (outentry.nodeid) || +fuse_invalid_attr (&outentry.attr))) + goto out_free_ff; +fuse_file_free (ff); +return fuse_open_atomic_found (dir, entry, file, &outentry, + forget, epoch, attr_version, + evict_ctr); + } +if (!S_ISREG (outentry.attr.mode) ||invalid_nodeid (outentry.nodeid) || -fuse_invalid_attr (&outentry.attr)) +fuse_invalid_attr (&outentry.attr) || + args.out_args[1 ].size !=sizeof (*outopenp)) goto out_free_ff; + created = !atomic || (outopenp->open_flags &FOPEN_CREATED ); +if (atomic) { +/* Only O_CREAT creates; O_TRUNC opens are left to the VFS */ +if ((created && !create) || (!created && trunc)) + goto out_free_ff; +if (created) + file->f_mode |=FMODE_CREATED ; + } + ff->fh = outopenp->fh; ff->nodeid = outentry.nodeid; ff->open_flags = outopenp->open_flags; inode =fuse_iget (dir->i_sb, outentry.nodeid, outentry.generation, - &outentry.attr,ATTR_TIMEOUT (&outentry),0 ,0 ); + &outentry.attr,ATTR_TIMEOUT (&outentry), + created ?0 : attr_version, created ?0 : evict_ctr);if (!inode) { flags &= ~(O_CREAT |O_EXCL |O_TRUNC );fuse_sync_release (NULL , ff, flags);
fs/fuse/dir.c static int fuse_create_open(struct mnt_idmap *idmap, struct inode *dir, · line 992
Quoted 8 lines
fs/fuse/dir.c +4
-1@@ -906,10 +986,14 @@ static int fuse_create_open(struct mnt_idmap *idmap, struct inode *dir, goto out_err; }kfree (forget); -d_instantiate (entry, inode); +if (d_in_lookup (entry)) +d_splice_alias (inode, entry); +else +d_instantiate (entry, inode);
Similar to above, d_splice_alias() is now sufficient for all cases.
Thanks,
NeilBrown
Quoted 90 lines
fs/fuse/dir.c +12
-1fuse_dentry_set_epoch (entry, epoch);fuse_change_entry_timeout (entry, &outentry); -fuse_dir_changed (dir); +if (created) +fuse_dir_changed (dir); err =generic_file_open (inode, file);if (!err) { file->private_data = ff;@@ -947,6 +1031,16 @@ static int fuse_atomic_open(struct inode *dir, struct dentry *entry,if (fuse_is_bad (dir))return -EIO ; +if (!fc->no_open_atomic && (d_in_lookup (entry) || (flags &O_CREAT ))) { + err =fuse_create_open (idmap, dir, entry, file, flags, + mode |S_IFREG ,FUSE_OPEN_ATOMIC ); +if (err == -EEXIST ) +fuse_invalidate_entry (entry); +if (err != -ENOSYS ) +return err; + fc->no_open_atomic =1 ; + } +if (d_in_lookup (entry)) {struct dentry *res =fuse_lookup (dir, entry,0 );if (res ||d_really_is_positive (entry))fs/fuse/fuse_i.h +3
-0@@ -589,6 +589,9 @@ struct fuse_conn {/** @no_create: Is create not implemented by fs? */ unsigned no_create:1; +/** @no_open_atomic: Is open atomic not implemented by fs? */ + unsigned no_open_atomic:1; +/** @no_bmap: Is bmap not implemented by fs? */ unsigned no_bmap:1;include/uapi/linux/fuse.h +7
-1@@ -251,6 +251,9 @@* * 7.47 * - add FUSE_HAS_SYNCFS opt-in flag for privileged userspace servers +* +* 7.48 +* - add FUSE_OPEN_ATOMIC and FOPEN_CREATED */ #ifndef _LINUX_FUSE_H@@ -286,7 +289,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@@ -400,6 +403,7 @@ struct fuse_file_lock {* (FUSE_URING_ZERO_COPY) and the request carries page * payload. Otherwise reads/writes fall back to * copying. +* FOPEN_CREATED: FUSE_OPEN_ATOMIC created the file */ #define FOPEN_DIRECT_IO (1 << 0)#define FOPEN_KEEP_CACHE (1 << 1)@@ -410,6 +414,7 @@ struct fuse_file_lock {#define FOPEN_PARALLEL_DIRECT_WRITES (1 << 6)#define FOPEN_PASSTHROUGH (1 << 7)#define FOPEN_IO_URING_ZERO_COPY (1 << 8) +#define FOPEN_CREATED (1 << 9)/** * INIT request/reply flags @@ -690,6 +695,7 @@ enum fuse_opcode {FUSE_TMPFILE =51 ,FUSE_STATX =52 ,FUSE_COPY_FILE_RANGE_64 =53 , +FUSE_OPEN_ATOMIC =54 ,/* CUSE specific operations */ CUSE_INIT = 4096,Signature · 1 line
2.50.1
Re: [PATCH 1/3] fuse: add FUSE_OPEN_ATOMIC
Quoted 5 lines · NeilBrown
fs/fuse/dir.c static void free_ext_value(struct fuse_args *args) · line 848
Quoted 2 lines
fs/fuse/dir.c +2
-0+d_drop (entry); + res =d_splice_alias (inode, entry);As of
f1ac607f786fd_splice_alias()handles hashed dentries and thed_drop()is not needed and not wanted.
Thanks. I'll rebase onto 7.4-rc1 (will make note of the dependency), which has the commit f1ac607f786f.
Quoted 7 lines
fs/fuse/dir.c static int fuse_create_open(struct mnt_idmap *idmap, struct inode *dir, · line 992
Quoted 5 lines
fs/fuse/dir.c +4
-1-d_instantiate (entry, inode); +if (d_in_lookup (entry)) +d_splice_alias (inode, entry); +else +d_instantiate (entry, inode);Similar to above,
d_splice_alias()is now sufficient for all cases.
Thanks, fuse_create_open() also handles FUSE_TMPFILE, which needs d_instantiate() for its dentry to stay unhashed. Otherwise I can have it use d_splice_alias().
Re: [PATCH 1/3] fuse: add FUSE_OPEN_ATOMIC
Quoted 18 lines · Jimmy Zuber
Quoted 5 lines · NeilBrown
fs/fuse/dir.c static void free_ext_value(struct fuse_args *args) · line 848
Quoted 2 lines
fs/fuse/dir.c +2
-0+d_drop (entry); + res =d_splice_alias (inode, entry);As of
f1ac607f786fd_splice_alias()handles hashed dentries and thed_drop()is not needed and not wanted.Thanks. I'll rebase onto 7.4-rc1 (will make note of the dependency), which has the commit
f1ac607f786f.Quoted 7 lines
fs/fuse/dir.c static int fuse_create_open(struct mnt_idmap *idmap, struct inode *dir, · line 992
Quoted 5 lines
fs/fuse/dir.c +4
-1-d_instantiate (entry, inode); +if (d_in_lookup (entry)) +d_splice_alias (inode, entry); +else +d_instantiate (entry, inode);Similar to above,
d_splice_alias()is now sufficient for all cases.
Quoted 4 lines
Thanks,
fuse_create_open()also handlesFUSE_TMPFILE, which needsd_instantiate()for its dentry to stay unhashed. Otherwise I can have it used_splice_alias().
Almost all other filesystems use d_tmpfile() for tmp-files (which calls d_instantiate). Is there a reason that fuse cannot use d_tmpfile()?
Thanks,
NeilBrown
Re: [PATCH 1/3] fuse: add FUSE_OPEN_ATOMIC
Quoted 2 lines · NeilBrown
Almost all other filesystems use
d_tmpfile()for tmp-files (which callsd_instantiate). Is there a reason that fuse cannot used_tmpfile()?
fuse_tmpfile() follows a slightly different pattern than d_tmpfile(). A fuse tmpfile shows up in /proc as "<dir>// (deleted)", whereas d_tmpfile() renames the dentry so that it shows up as "<dir>/#<ino> (deleted)". Also, fuse gets nlink for the tmpfile from the server, while d_tmpfile() decrements it. Reusing d_tmpfile() seems like it would be a behavior change, which I'd like to avoid.
Re: [PATCH 1/3] fuse: add FUSE_OPEN_ATOMIC
Quoted 11 lines · Jimmy Zuber
Quoted 2 lines · NeilBrown
Almost all other filesystems use
d_tmpfile()for tmp-files (which callsd_instantiate). Is there a reason that fuse cannot used_tmpfile()?
fuse_tmpfile()follows a slightly different pattern thand_tmpfile(). A fuse tmpfile shows up in /proc as "<dir>// (deleted)", whereasd_tmpfile()renames the dentry so that it shows up as "<dir>/#<ino> (deleted)". Also, fuse gets nlink for the tmpfile from the server, whiled_tmpfile()decrements it. Reusingd_tmpfile()seems like it would be a behavior change, which I'd like to avoid.
The /proc different seems cosmetic and probably of no consequence. The nlink issue might be significant - I cannot say.
It might be nice to include a comment where d_instantiate() is used, explaining why d_tmpfile() was avoided(?).
Thank for the explanation,
NeilBrown
Re: [PATCH 1/3] fuse: add FUSE_OPEN_ATOMIC
Quoted 4 lines · NeilBrown
The /proc different seems cosmetic and probably of no consequence. The nlink issue might be significant - I cannot say.
It might be nice to include a comment whered_instantiate()is used, explaining whyd_tmpfile()was avoided(?).
I dug into this a bit more. I couldn't find a FUSE server that actually implements FUSE_TMPFILE, besides the example servers in some FUSE libraries. However, switching to d_tmpfile() is a behavior change that's unrelated to the core of the atomic open series. I'd be happy to cut a separate patch series for it if you think it's an important improvement, but I want to keep things decoupled. In v7 of the atomic open series I'll make the "why" of d_instantiate() clear, i.e. that it is uniform with the rest of the fuse kernel module.
Thanks!
Jimmy
Re: [PATCH 1/3] fuse: add FUSE_OPEN_ATOMIC
Quoted 6 lines · Jimmy Zuber
Quoted 4 lines · NeilBrown
The /proc different seems cosmetic and probably of no consequence. The nlink issue might be significant - I cannot say.
It might be nice to include a comment whered_instantiate()is used, explaining whyd_tmpfile()was avoided(?).
Quoted 8 lines
I dug into this a bit more. I couldn't find a FUSE server that actually implements
FUSE_TMPFILE, besides the example servers in some FUSE libraries. However, switching tod_tmpfile()is a behavior change that's unrelated to the core of the atomic open series. I'd be happy to cut a separate patch series for it if you think it's an important improvement, but I want to keep things decoupled. In v7 of the atomic open series I'll make the "why" ofd_instantiate()clear, i.e. that it is uniform with the rest of the fuse kernel module.
It took me a while to find the link.
https://lore.kernel.org/all/[email protected]/
In [PATCH v4 09/10] vfs: open inside ->tmpfile()
<quote>
This is in preparation for adding tmpfile support to fuse, which requires that the tmpfile creation and opening are done as a single operation.
Replace the 'struct dentry *' argument of i_op->tmpfile with 'struct file *'.
Call finish_open_simple() as the last thing in ->tmpfile() instances (may be omitted in the error case).
Change d_tmpfile() argument to 'struct file *' as well to make callers more readable.
</quote>
PATCH 2/3 fuse: open cached files with FUSE_OPEN_ATOMIC
In reply to jamzFrom: Jimmy Zuber <[email protected]>
When the server implements FUSE_OPEN_ATOMIC, skip LOOKUP for a regular file being opened, opening it by name from fuse_open() instead. If the name now refers to a different file or to nothing, drop the dentry and return -EOPENSTALE, so that the VFS looks the name up again. A matching reply refreshes the attributes and the entry timeout.
If the server replies -ENOSYS, fuse_open() sends the skipped LOOKUP itself.
--- fs/fuse/dir.c | 247 ++++++++++++++++++++++++++++++++++++++--------- fs/fuse/file.c | 4 +- fs/fuse/fuse_i.h | 1 + 3 files changed, 208 insertions(+), 44 deletions(-)
fs/fuse/dir.c +204 -43
@@ -379,6 +379,60 @@ static void fuse_lookup_init(struct fuse_args *args, u64 nodeid, args->out_args[0 ].value = outarg; } +/* +* Look the name of @entry up again. Returns 1 if it still refers to the +* inode of @entry, refreshing its attributes and entry timeout, 0 if it does +* not, or -ENOMEM / -EINTR. +*/ +static int fuse_revalidate_lookup (struct inode *dir,const struct qstr *name, +struct dentry *entry) +{ +struct inode *inode =d_inode (entry); +struct fuse_mount *fm =get_fuse_mount (inode); +struct fuse_inode *fi =get_fuse_inode (inode); +struct fuse_entry_out outarg; +FUSE_ARGS (args); +struct fuse_forget_link *forget; +u64 attr_version; +int ret; + + forget =fuse_alloc_forget (); +if (!forget) +return -ENOMEM ; + + attr_version =fuse_get_attr_version (fm->fc); + +fuse_lookup_init (&args,get_node_id (dir), name, &outarg); + ret =fuse_simple_request (fm, &args); +/* Zero nodeid is same as -ENOENT */ +if (!ret && !outarg.nodeid) + ret = -ENOENT ; +if (!ret) { +if (outarg.nodeid !=get_node_id (inode) || + (bool )IS_AUTOMOUNT (inode) != (bool ) (outarg.attr.flags &FUSE_ATTR_SUBMOUNT )) { +fuse_chan_queue_forget (fm->fc->chan, forget, + outarg.nodeid,1 ); +return 0 ; + } +spin_lock (&fi->lock); + fi->nlookup++; +spin_unlock (&fi->lock); + } +kfree (forget); +if (ret == -ENOMEM || ret == -EINTR ) +return ret; +if (ret ||fuse_invalid_attr (&outarg.attr) || +fuse_stale_inode (inode, outarg.generation, &outarg.attr)) +return 0 ; + +forget_all_cached_acls (inode); +fuse_change_attributes (inode, &outarg.attr,NULL , +ATTR_TIMEOUT (&outarg), + attr_version); +fuse_change_entry_timeout (entry, &outarg); +return 1 ; +} +/* * Check whether the dentry is still valid *
@@ -392,7 +446,6 @@ static int fuse_dentry_revalidate(struct inode *dir, const struct qstr *name, struct dentry *entry, unsigned int flags) {struct inode *inode; -struct fuse_mount *fm;struct fuse_conn *fc;struct fuse_inode *fi;struct fuse_dentry *fd = entry->d_fsdata;
@@ -407,57 +460,24 @@ static int fuse_dentry_revalidate(struct inode *dir, const struct qstr *name, goto invalid; else if (time_before64(fuse_dentry_time(entry), get_jiffies_64()) || (flags & (LOOKUP_EXCL | LOOKUP_REVAL | LOOKUP_RENAME_TARGET))) { -struct fuse_entry_out outarg; -FUSE_ARGS (args); -struct fuse_forget_link *forget; -u64 attr_version; -/* For negative dentries, always do a fresh lookup */ if (!inode) goto invalid; +/* fuse_open_revalidate() checks the name of a file it opens */ +if (!fc->no_open_atomic && (flags &LOOKUP_OPEN ) && + !(flags & (LOOKUP_EXCL |LOOKUP_REVAL | +LOOKUP_RENAME_TARGET )) && +S_ISREG (inode->i_mode) && !d_mountpoint (entry)) + goto valid; + ret = -ECHILD ;if (flags &LOOKUP_RCU ) goto out; - fm =get_fuse_mount (inode); - - forget =fuse_alloc_forget (); - ret = -ENOMEM ; -if (!forget) - goto out; - - attr_version =fuse_get_attr_version (fm->fc); - -fuse_lookup_init (&args,get_node_id (dir), name, &outarg); - ret =fuse_simple_request (fm, &args); -/* Zero nodeid is same as -ENOENT */ -if (!ret && !outarg.nodeid) - ret = -ENOENT ; -if (!ret) { - fi =get_fuse_inode (inode); -if (outarg.nodeid !=get_node_id (inode) || - (bool )IS_AUTOMOUNT (inode) != (bool ) (outarg.attr.flags &FUSE_ATTR_SUBMOUNT )) { -fuse_chan_queue_forget (fm->fc->chan, forget, - outarg.nodeid,1 ); - goto invalid; - } -spin_lock (&fi->lock); - fi->nlookup++; -spin_unlock (&fi->lock); - } -kfree (forget); -if (ret == -ENOMEM || ret == -EINTR ) + ret =fuse_revalidate_lookup (dir, name, entry); +if (ret <=0 ) goto out; -if (ret ||fuse_invalid_attr (&outarg.attr) || -fuse_stale_inode (inode, outarg.generation, &outarg.attr)) - goto invalid; - -forget_all_cached_acls (inode); -fuse_change_attributes (inode, &outarg.attr,NULL , -ATTR_TIMEOUT (&outarg), - attr_version); -fuse_change_entry_timeout (entry, &outarg); } else if (inode) { fi =get_fuse_inode (inode);if (flags &LOOKUP_RCU ) {
@@ -467,6 +487,7 @@ static int fuse_dentry_revalidate(struct inode *dir, const struct qstr *name,fuse_advise_use_readdirplus (dir); } } +valid: ret =1 ; out:return ret;
@@ -1018,6 +1039,146 @@ static int fuse_create_open(struct mnt_idmap *idmap, struct inode *dir,return err; } +/* +* Open a regular file whose dentry has expired with FUSE_OPEN_ATOMIC, which +* revalidates the name in the same request. Returns 1 if the file still +* needs FUSE_OPEN, or -EOPENSTALE if the name no longer refers to it. +*/ +int fuse_open_revalidate (struct file *file) +{ +struct dentry *entry = file->f_path.dentry; +struct inode *inode =d_inode (entry); +struct fuse_inode *fi =get_fuse_inode (inode); +struct fuse_mount *fm =get_fuse_mount (inode); +struct fuse_conn *fc = fm->fc; +FUSE_ARGS (args); +struct fuse_forget_link *forget; +struct fuse_create_in inarg; +struct fuse_entry_out outentry; +struct fuse_open_out *outopenp; +struct name_snapshot name; +struct dentry *parent; +struct fuse_file *ff; +u64 attr_version; +int err; + +if (fc->no_open_atomic ||IS_ROOT (entry) || !S_ISREG (inode->i_mode) || + !time_before64 (fuse_dentry_time (entry),get_jiffies_64 ())) +return 1 ; + + forget =fuse_alloc_forget (); +if (!forget) +return -ENOMEM ; + ff =fuse_file_alloc (fm, true); +if (!ff) { +kfree (forget); +return -ENOMEM ; + } + +memset (&inarg,0 ,sizeof (inarg)); +memset (&outentry,0 ,sizeof (outentry)); + inarg.flags = file->f_flags & ~(O_CREAT |O_EXCL |O_NOCTTY ); + inarg.mode =S_IFREG ; + parent =dget_parent (entry); +take_dentry_name_snapshot (&name, entry); + args.opcode =FUSE_OPEN_ATOMIC ; + args.nodeid =get_node_id (d_inode (parent)); + args.in_numargs =2 ; + args.in_args[0 ].size =sizeof (inarg); + args.in_args[0 ].value = &inarg; + args.in_args[1 ].size = name.name.len +1 ; + args.in_args[1 ].value = name.name.name; + args.out_numargs =2 ; + args.out_args[0 ].size =sizeof (outentry); + args.out_args[0 ].value = &outentry; + outopenp = &ff->args->open_outarg; + args.out_args[1 ].size =sizeof (*outopenp); + args.out_args[1 ].value = outopenp; + args.out_argvar = true; + + attr_version =fuse_get_attr_version (fc); + err =fuse_simple_request (fm, &args); +if (err == -ENOSYS ) { +/* d_revalidate() skipped the LOOKUP, so send it here */ + fc->no_open_atomic =1 ; + err =fuse_revalidate_lookup (d_inode (parent), &name.name, + entry); +if (!err) { +d_drop (entry); + err = -EOPENSTALE ; + } +release_dentry_name_snapshot (&name); +dput (parent); +fuse_file_free (ff); +kfree (forget); +return err; + } +release_dentry_name_snapshot (&name); +dput (parent); + +if (err == -ENOENT ) { +d_drop (entry); + err = -EOPENSTALE ; + } +if (err <0 ) + goto out_free; + +if (args.out_args[1 ].size) { + ff->fh = outopenp->fh; + ff->nodeid = outentry.nodeid; + ff->open_flags = outopenp->open_flags; + } + +/* The retry looks the name up again */ + err = -EOPENSTALE ; +if (outentry.nodeid !=get_node_id (inode) || +fuse_invalid_attr (&outentry.attr) || +fuse_stale_inode (inode, outentry.generation, &outentry.attr)) { +d_drop (entry); + goto out_release; + } + +/* Only O_CREAT creates; O_TRUNC leaves the file unopened */ + err = -EIO ; +if (args.out_args[1 ].size && + (args.out_args[1 ].size !=sizeof (*outopenp) || + (outopenp->open_flags &FOPEN_CREATED ) || + (file->f_flags &O_TRUNC ))) + goto out_release; + +spin_lock (&fi->lock); + fi->nlookup++; +spin_unlock (&fi->lock); +kfree (forget); +forget_all_cached_acls (inode); +fuse_change_attributes (inode, &outentry.attr,NULL , +ATTR_TIMEOUT (&outentry), attr_version); +fuse_change_entry_timeout (entry, &outentry); + +if (!args.out_args[1 ].size) { +fuse_file_free (ff); +return 1 ; + } + file->private_data = ff; +return 0 ; + +out_release: +if (outentry.nodeid) +fuse_chan_queue_forget (fc->chan, forget, outentry.nodeid,1 ); +else +kfree (forget); +if (args.out_args[1 ].size) +fuse_sync_release (NULL , ff, file->f_flags); +else +fuse_file_free (ff); +return err; + +out_free: +fuse_file_free (ff); +kfree (forget); +return err; +} +static int fuse_mknod (struct mnt_idmap *,struct inode *,struct dentry *,umode_t ,dev_t ); static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
fs/fuse/file.c +3 -1
@@ -278,7 +278,9 @@ static int fuse_open(struct inode *inode, struct file *file)if (is_wb_truncate || vdax_truncate)fuse_set_nowrite (inode); - err =fuse_do_open (fm,get_node_id (inode), file, false); + err =fuse_open_revalidate (file); +if (err >0 ) + err =fuse_do_open (fm,get_node_id (inode), file, false);if (!err) { ff = file->private_data; err =fuse_finish_open (inode, file);
fs/fuse/fuse_i.h +1 -0
@@ -954,6 +954,7 @@ void fuse_read_args_fill(struct fuse_io_args *ia, struct file *file, loff_t pos,struct fuse_file *fuse_file_alloc (struct fuse_mount *fm,bool release);void fuse_file_free (struct fuse_file *ff);int fuse_finish_open (struct inode *inode,struct file *file); +int fuse_open_revalidate (struct file *file);void fuse_sync_release (struct fuse_inode *fi,struct fuse_file *ff,unsigned int flags);
Signature · 1 line
2.50.1
PATCH 3/3 selftests/fuse: add test for FUSE_OPEN_ATOMIC
In reply to jamzFrom: Jimmy Zuber <[email protected]>
Check the requests sent when opening and creating new, existing, missing and expired names, against a raw /dev/fuse server with and without FUSE_OPEN_ATOMIC, and that opening an existing read-only file is still permission checked.
--- .../selftests/filesystems/fuse/.gitignore | 1 + .../selftests/filesystems/fuse/Makefile | 3 +- .../filesystems/fuse/atomic_open_test.c | 473 ++++++++++++++++++ 3 files changed, 476 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/filesystems/fuse/atomic_open_test.c
tools/testing/selftests/filesystems/fuse/.gitignore +1 -0
@@ -1,4 +1,5 @@ # SPDX-License-Identifier: GPL-2.0-only +atomic_open_test fuse_mnt fusectl_test test_syncfs
tools/testing/selftests/filesystems/fuse/Makefile +2 -1
@@ -3,7 +3,7 @@ CFLAGS += -Wall -O2 -g $(KHDR_INCLUDES) TEST_GEN_PROGS := fusectl_test test_syncfs -TEST_GEN_PROGS += write_extend_eof_test +TEST_GEN_PROGS += write_extend_eof_test atomic_open_test TEST_GEN_FILES := fuse_mnt # fuse_acl_cache_test requires libfuse3; add it only when the library is present.
@@ -16,6 +16,7 @@ endif include ../../lib.mk $(OUTPUT)/write_extend_eof_test: LDLIBS += -lpthread +$(OUTPUT)/atomic_open_test: LDLIBS += -lpthread VAR_CFLAGS := $(shell pkg-config fuse --cflags 2>/dev/null) ifeq ($(VAR_CFLAGS),)
tools/testing/selftests/filesystems/fuse/atomic_open_test.c new file +473 -0
473 lines not on this page · raw message
Signature · 1 line
2.50.1
Quoted 6 lines · jamz
From: Jimmy Zuber <[email protected]>
Add
FUSE_OPEN_ATOMIC, allowing the kernel to skip LOOKUP to improve performance. A server implementingFUSE_OPEN_ATOMICwill look up the name, create it ifO_CREATis supplied, and open it in one request.
Quoted 7 lines
The reply says whether the file was created, as Miklos suggested in [1]. The overall pattern follows his sketch in [2]. For a cached name, the kernel compares the returned nodeid with the cached one and looks the name up again on a mismatch.
A server without
FUSE_OPEN_ATOMICreplies-ENOSYSand keeps today's behaviour.
This all looks like a reasonably sensible design. As permission checking happens in the client, not on the server, it is appropriate that FUSE_OPEN_ATOMIC only opens the file if it actually created it. So it doesn't make sense to implement O_TRUNC.
However the code seems to suggest that the server might open the file if it was found to already exist. In that case, were does the permission check happen...
Maybe I need a more explicit statement of what FUSE_OPEN_ATOMIC is meant to do in the various cases. Or maybe I just don't know enough about fuse.
There is work underway to support O_CREAT|O_DIRECTORY so that a directory can be created and opened in a single operation. Would it make sense to include this in FUSE_OPEN_ATOMIC ??
Thanks,
NeilBrown
Quoted 28 lines
Tested with a new selftest under UML and KVM.
[1] https://lore.kernel.org/all/CAJfpeguEK4JsFo60=yHkpLL6XkDGS7cQ5GOhiFL379KsYk0TsQ@mail.gmail.com/
[2] https://lore.kernel.org/all/CAJfpegvXtXY=Pbxv+dMGFR8mvWN0DUwhSo6NwaVexk6Y6sao+w@mail.gmail.com/Jimmy Zuber (3):
fuse: addFUSE_OPEN_ATOMIC
fuse: open cached files withFUSE_OPEN_ATOMIC
selftests/fuse: add test forFUSE_OPEN_ATOMICfs/fuse/dir.c | 359 +++++++++++-- fs/fuse/file.c | 4 +- fs/fuse/fuse_i.h | 4 + include/uapi/linux/fuse.h | 8 +- .../selftests/filesystems/fuse/.gitignore | 1 + .../selftests/filesystems/fuse/Makefile | 3 +- .../filesystems/fuse/atomic_open_test.c | 473 ++++++++++++++++++ 7 files changed, 797 insertions(+), 55 deletions(-) create mode 100644 tools/testing/selftests/filesystems/fuse/atomic_open_test.cbase-commit:
b776cbe24cc1b79c7f5101762b0069b3f48b6a4bSignature · 1 line
2.50.1
Quoted 5 lines · NeilBrown
This all looks like a reasonably sensible design. As permission checking happens in the client, not on the server, it is appropriate that
FUSE_OPEN_ATOMIConly opens the file if it actually created it. So it doesn't make sense to implementO_TRUNC.
Quoted 7 lines
However the code seems to suggest that the server might open the file if it was found to already exist. In that case, were does the permission check happen...
Maybe I need a more explicit statement of what
FUSE_OPEN_ATOMICis meant to do in the various cases. Or maybe I just don't know enough about fuse.
FUSE_OPEN_ATOMIC will open an existing regular file unless O_TRUNC is specified. If the server opens an existing file, the kernel will perform a permission check after the reply (do_open() calls may_open()). If that permission check fails, we will release the file. This matches other file systems that support atomic_open(), such as ceph, where a file can be opened before a kernel check is performed. Servers that don't want this should just return the entry only to have the kernel check permissions and open. I'll try to make the expected behavior of FUSE_OPEN_ATOMIC implementations more clear in v2.
Quoted 3 lines
There is work underway to support O_CREAT|O_DIRECTORY so that a directory can be created and opened in a single operation. Would it make sense to include this in
FUSE_OPEN_ATOMIC??
Oh thanks for the heads up. This would be a nice fit. If that series is accepted I can update this one accordingly. That said, even with that series it might be nice to have an init flag in case implementations want to selectively support atomic file but not directory open/create (ENOSYS is too coarse grained).
Quoted 14 lines · Jimmy Zuber
Quoted 5 lines · NeilBrown
This all looks like a reasonably sensible design. As permission checking happens in the client, not on the server, it is appropriate that
FUSE_OPEN_ATOMIConly opens the file if it actually created it. So it doesn't make sense to implementO_TRUNC.Quoted 7 lines
However the code seems to suggest that the server might open the file if it was found to already exist. In that case, were does the permission check happen...
Maybe I need a more explicit statement of what
FUSE_OPEN_ATOMICis meant to do in the various cases. Or maybe I just don't know enough about fuse.
Quoted 9 lines
FUSE_OPEN_ATOMICwill open an existing regular file unlessO_TRUNCis specified. If the server opens an existing file, the kernel will perform a permission check after the reply (do_open()callsmay_open()). If that permission check fails, we will release the file. This matches other file systems that supportatomic_open(), such as ceph, where a file can be opened before a kernel check is performed. Servers that don't want this should just return the entry only to have the kernel check permissions and open. I'll try to make the expected behavior ofFUSE_OPEN_ATOMICimplementations more clear in v2.
It seems a little odd performing the "open" before any permission check, but I can see how it might work, and as you say the server an refuse if it has reason too.
Thanks for the clarification.
NeilBrown
Quoted 11 lines
Quoted 4 lines
There is work underway to support O_CREAT|O_DIRECTORY so that a directory can be created and opened in a single operation. Would it make sense to include this in
FUSE_OPEN_ATOMIC??Oh thanks for the heads up. This would be a nice fit. If that series is accepted I can update this one accordingly. That said, even with that series it might be nice to have an init flag in case implementations want to selectively support atomic file but not directory open/create (ENOSYS is too coarse grained).