Skip to contentSkip to conversation

fuse: skip LOOKUP before exclusive create

PATCH New Jimmy Zuber 1 participant 3 messages Oct 29

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover fuse: skip LOOKUP before exclusive create

mkdir(), mknod(), symlink(), link() and renameat2() with RENAME_NOREPLACE look the new name up with FUSE_LOOKUP before sending the request. This series adds an INIT flag, FUSE_EXCL_CREATE, with which the server fails these requests with -EEXIST for an existing name, so the kernel can skip the LOOKUP.

This series is independent of "fuse: atomic open" [1], which covers open(). Both add a 7.48 entry to the uapi changelog.

Tested with the new selftest under UML and KVM.

Changes in v2 (from the Sashiko review of v1 [2]):
- Use INIT bit 46; bit 45 is FUSE_PASSTHROUGH_V2.
- Document that an existing name can fail with the error of a VFS check that runs before the request, such as -EROFS.
- Keep the test server's state mapped at teardown, where the server thread may still read it, and mount in a private mount namespace. Skip without /dev/fuse or kernel support. Test mknod() and link() of an existing name, and the dcache after each request.
- Rebase onto fuse for-next.

v1: https://lore.kernel.org/fuse-devel/[email protected]/

[1] https://lore.kernel.org/fuse-devel/[email protected]/
[2] https://sashiko.dev/#/patchset/[email protected]

Jimmy Zuber (2):
fuse: skip LOOKUP before exclusive create
selftests/fuse: test FUSE_EXCL_CREATE

 fs/fuse/dir.c                                 |  10 +
 fs/fuse/fuse_i.h                              |   6 +
 fs/fuse/inode.c                               |   4 +-
 include/uapi/linux/fuse.h                     |  12 +-
 .../selftests/filesystems/fuse/.gitignore     |   1 +
 .../selftests/filesystems/fuse/Makefile       |   3 +-
 .../filesystems/fuse/excl_create_test.c       | 386 ++++++++++++++++++
 7 files changed, 419 insertions(+), 3 deletions(-)
 create mode 100644 tools/testing/selftests/filesystems/fuse/excl_create_test.c

base-commit: 9b1da7b4a4e197072e630c3b85d4100a399752ff

Signature · 1 line
2.50.1
#1 lore raw

PATCH v2 1/2 fuse: skip LOOKUP before exclusive create

mkdir(), mknod(), symlink(), link() and renameat2() with RENAME_NOREPLACE look the new name up with FUSE_LOOKUP before sending the request, which costs a round trip to the server.

Add an INIT flag, FUSE_EXCL_CREATE, with which the server fails FUSE_MKNOD, FUSE_MKDIR, FUSE_SYMLINK, FUSE_LINK, and FUSE_RENAME2 with RENAME_NOREPLACE, with -EEXIST for an existing name. When the server sets it, the lookup of a name that is about to be created returns without sending FUSE_LOOKUP, and an existing name is reported by the -EEXIST of the request. The VFS checks the request before sending it, so an existing name can also fail with the error of such a check, such as -EROFS on a read-only mount or -EACCES without write permission on the directory. After a failed request the name stays uncached, so the next lookup goes to the server.

A rename that may replace its target looks the target up, so that the kernel can check the permission to replace it.

Assisted-by Claude:claude-opus-5-5 [Claude-Code]
---
 fs/fuse/dir.c             | 10 ++++++++++
 fs/fuse/fuse_i.h          |  6 ++++++
 fs/fuse/inode.c           |  4 +++-
 include/uapi/linux/fuse.h | 12 +++++++++++-
 4 files changed, 30 insertions(+), 2 deletions(-)
fs/fuse/dir.c +10 -0
index 28516a1760e24..c4d47ef1a195c 100644
@@ -627,6 +627,16 @@ static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
 		return ERR_PTR(-EIO);
 
 	fc = get_fuse_conn_super(dir->i_sb);
+
+	/* The request fails with -EEXIST if the name exists */
+	if (fc->excl_create &&
+	    (flags & (LOOKUP_CREATE | LOOKUP_EXCL)) ==
+	    (LOOKUP_CREATE | LOOKUP_EXCL)) {
+		if (entry->d_name.len > fc->name_max)
+			return ERR_PTR(-ENAMETOOLONG);
+		return NULL;
+	}
+
 	epoch = atomic_read(&fc->epoch);
 
 	locked = fuse_lock_inode(dir);
fs/fuse/fuse_i.h +6 -0
index c91af283aef20..1d4cc77887e50 100644
@@ -537,6 +537,12 @@ struct fuse_conn {
 	 */
 	unsigned atomic_o_trunc:1;
 
+	/**
+	 * @excl_create: Create requests check for an existing name.
+	 * Only set in INIT
+	 */
+	unsigned excl_create:1;
+
 	/**
 	 * @export_support: Filesystem supports NFS exporting.
 	 * Only set in INIT
fs/fuse/inode.c +3 -1
index 1341b681d0ed3..97ff1b87186d9 100644
@@ -1445,6 +1445,8 @@ static void process_init_reply(struct fuse_args *args, int error)
 
 			if (fuse_syncfs_enable(fc, flags))
 				fc->sync_fs = 1;
+			if (flags & FUSE_EXCL_CREATE)
+				fc->excl_create = 1;
 		} else {
 			ra_pages = fc->max_read / PAGE_SIZE;
 			fc->no_lock = 1;
@@ -1503,7 +1505,7 @@ static struct fuse_init_args *fuse_new_init(struct fuse_mount *fm)
 		FUSE_SECURITY_CTX | FUSE_CREATE_SUPP_GROUP |
 		FUSE_HAS_EXPIRE_ONLY | FUSE_DIRECT_IO_ALLOW_MMAP |
 		FUSE_NO_EXPORT_SUPPORT | FUSE_HAS_RESEND | FUSE_ALLOW_IDMAP |
-		FUSE_REQUEST_TIMEOUT;
+		FUSE_REQUEST_TIMEOUT | FUSE_EXCL_CREATE;
 #ifdef CONFIG_FUSE_VDAX
 	if (fm->fc->vdax)
 		flags |= FUSE_MAP_ALIGNMENT;
include/uapi/linux/fuse.h +11 -1
index 616430a2973c6..0731e555ad9cb 100644
@@ -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_EXCL_CREATE
  */
 
 #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
@@ -480,6 +483,12 @@ struct fuse_file_lock {
  *		privilege that mounting virtiofs or fuseblk requires).
  *		Insufficiently privileged servers ignore it.
  * FUSE_PASSTHROUGH_V2: use 64 bit server allocated backing ID
+ * FUSE_EXCL_CREATE: FUSE_MKNOD, FUSE_MKDIR, FUSE_SYMLINK, FUSE_LINK and
+ *		FUSE_RENAME2 with RENAME_NOREPLACE fail with -EEXIST for an
+ *		existing name, so the kernel sends them without a LOOKUP of
+ *		the name. The VFS checks the request before sending it, so an
+ *		existing name can also fail with the error of such a check,
+ *		such as -EROFS
  */
 #define FUSE_ASYNC_READ		(1 << 0)
 #define FUSE_POSIX_LOCKS	(1 << 1)
@@ -530,6 +539,7 @@ struct fuse_file_lock {
 #define FUSE_HAS_IO_URING_BUFPOOL (1ULL << 43)
 #define FUSE_HAS_SYNCFS		(1ULL << 44)
 #define FUSE_PASSTHROUGH_V2	(1ULL << 45)
+#define FUSE_EXCL_CREATE	(1ULL << 46)
 
 /*
  * CUSE INIT request/reply flags
Signature · 1 line
2.50.1
#2 lore raw

PATCH v2 2/2 selftests/fuse: test FUSE_EXCL_CREATE

In reply to Jimmy Zuber

Check that mkdir(), mknod(), symlink(), link() and renameat2() with RENAME_NOREPLACE of new names send no FUSE_LOOKUP when the server sets FUSE_EXCL_CREATE, that the created names are cached, and that creating an existing name fails with EEXIST and leaves the name uncached. The FUSE_EXCL_CREATE cases skip on kernels that do not offer the flag.

Assisted-by Claude:claude-opus-5-5 [Claude-Code]
---
 .../selftests/filesystems/fuse/.gitignore     |   1 +
 .../selftests/filesystems/fuse/Makefile       |   3 +-
 .../filesystems/fuse/excl_create_test.c       | 386 ++++++++++++++++++
 3 files changed, 389 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/filesystems/fuse/excl_create_test.c
tools/testing/selftests/filesystems/fuse/.gitignore +1 -0
index 7c787313e69b6..719298cb98c5f 100644
@@ -1,4 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0-only
+excl_create_test
 fuse_mnt
 fusectl_test
 test_syncfs
tools/testing/selftests/filesystems/fuse/Makefile +2 -1
index e3ae3acff1139..ef1a241b79f8f 100644
@@ -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 excl_create_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)/excl_create_test: LDLIBS += -lpthread
 
 VAR_CFLAGS := $(shell pkg-config fuse3 --cflags 2>/dev/null)
 ifeq ($(VAR_CFLAGS),)
tools/testing/selftests/filesystems/fuse/excl_create_test.c new file +386 -0

386 lines not on this page · raw message

Signature · 1 line
2.50.1