Skip to contentSkip to conversation

fuse/libfuse/e2fsprogs: containerize ext4 for safer operation

PATCH In review Darrick J. Wong 8 participants 231 messages Feb 23 – Apr 29 Page 2 of 3

Subsystems (9)
Stable branch VFS Library code BPF +5 more subsystems
lore
Share & tools
lore
#102 lore raw

PATCH 1/5 fuse: enable fuse servers to upload BPF programs to handle iomap requests

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

There are certain fuse servers that might benefit from the ability to upload a BPF program into the kernel to respond to ->iomap_begin requests instead of upcalling the fuse server itself.

For example, consider a fuse server that abstracts a large amount of storage for use as intermediate storage by programs. If the storage is striped across hardware devices (e.g. RAID0 or interleaved memory controllers) then the iomapping pattern will be completely regular but the mappings themselves might be very small.

Performance for large IOs will suck if it is necessary to upcall the fuse server every time we cross a mapping boundary. The fuse server can try to mitigate that hit by upserting mappings ahead of time, but there's a better solution for this usecase: BPF programs.

In this case, the fuse server can compile a BPF program that will compute the mapping data for a given request and upload the program. This avoids the overhead of cache lookups and server upcalls. Note that the BPF verifier still imposes instruction count and complexity limits on the uploaded programs.

Note that I embraced and extended some code from Joanne, but at this point I've modified it so heavily that it's not really the original anymore. But she still gets credit for coming up with the idea and engaging me in flinging prototypes around.

---
 fs/fuse/fuse_i.h         |    5 +
 fs/fuse/fuse_iomap_bpf.h |   88 +++++++++++++
 fs/fuse/Makefile         |    4 +
 fs/fuse/fuse_iomap.c     |   13 +-
 fs/fuse/fuse_iomap_bpf.c |  309 ++++++++++++++++++++++++++++++++++++++++++++++
 fs/fuse/inode.c          |    7 +
 6 files changed, 423 insertions(+), 3 deletions(-)
 create mode 100644 fs/fuse/fuse_iomap_bpf.h
 create mode 100644 fs/fuse/fuse_iomap_bpf.c
fs/fuse/fuse_i.h +5 -0
index 5f2e7755e3e4e4..677e1fa4c8586c 100644
@@ -686,6 +686,11 @@ struct fuse_iomap_conn {
 
 	/* maximum mapping cache size */
 	unsigned int cache_maxbytes;
+
+#ifdef CONFIG_BPF_SYSCALL
+	/* bpf iomap overrides for this fs */
+	struct fuse_iomap_bpf_ops __rcu *bpf_ops;
+#endif
 };
 #endif
 
fs/fuse/fuse_iomap_bpf.h new file +88 -0

88 lines not on this page · raw message

fs/fuse/Makefile +4 -0
index c672503da7bcbd..43ba967db197b1 100644
@@ -20,4 +20,8 @@ fuse-$(CONFIG_SYSCTL) += sysctl.o
 fuse-$(CONFIG_FUSE_IO_URING) += dev_uring.o
 fuse-$(CONFIG_FUSE_IOMAP) += fuse_iomap.o fuse_iomap_cache.o
 
+ifeq ($(CONFIG_BPF_SYSCALL),y)
+fuse-$(CONFIG_FUSE_IOMAP) += fuse_iomap_bpf.o
+endif
+
 virtiofs-y := virtio_fs.o
fs/fuse/fuse_iomap.c +10 -3
index 7bc938cd859fae..2e0c35e879ffcc 100644
@@ -16,6 +16,7 @@
 #include "fuse_iomap_i.h"
 #include "fuse_dev_i.h"
 #include "fuse_iomap_cache.h"
+#include "fuse_iomap_bpf.h"
 
 static bool __read_mostly enable_iomap =
 #if IS_ENABLED(CONFIG_FUSE_IOMAP_BY_DEFAULT)
@@ -783,7 +784,9 @@ static int fuse_iomap_begin(struct inode *inode, loff_t pos, loff_t count,
 	args.out_numargs = 1;
 	args.out_args[0].size = sizeof(outarg);
 	args.out_args[0].value = &outarg;
-	err = fuse_simple_request(fm, &args);
+	err = fuse_iomap_begin_bpf(inode, &inarg, &outarg);
+	if (err == -ENOSYS)
+		err = fuse_simple_request(fm, &args);
 	if (err) {
 		trace_fuse_iomap_begin_error(inode, pos, count, opflags, err);
 		return err;
@@ -938,7 +941,9 @@ static int fuse_iomap_end(struct inode *inode, loff_t pos, loff_t count,
 		args.in_numargs = 1;
 		args.in_args[0].size = sizeof(inarg);
 		args.in_args[0].value = &inarg;
-		err = fuse_simple_request(fm, &args);
+		err = fuse_iomap_end_bpf(inode, &inarg);
+		if (err == -ENOSYS)
+			err = fuse_simple_request(fm, &args);
 		if (err == -ENOSYS) {
 			/*
 			 * libfuse returns ENOSYS for servers that don't
@@ -1047,7 +1052,9 @@ static int fuse_iomap_ioend(struct inode *inode, loff_t pos, size_t written,
 		args.out_numargs = 1;
 		args.out_args[0].size = sizeof(outarg);
 		args.out_args[0].value = &outarg;
-		iomap_error = fuse_simple_request(fm, &args);
+		iomap_error = fuse_iomap_ioend_bpf(inode, &inarg, &outarg);
+		if (iomap_error == -ENOSYS)
+			iomap_error = fuse_simple_request(fm, &args);
 		switch (iomap_error) {
 		case -ENOSYS:
 			/*
fs/fuse/fuse_iomap_bpf.c new file +309 -0

309 lines not on this page · raw message

fs/fuse/inode.c +7 -0
index 0f2b12aa1ac4bb..1ab9c0dc3fc964 100644
@@ -10,6 +10,7 @@
 #include "fuse_dev_i.h"
 #include "dev_uring_i.h"
 #include "fuse_iomap.h"
+#include "fuse_iomap_bpf.h"
 
 #include <linux/dax.h>
 #include <linux/pagemap.h>
@@ -1718,6 +1719,8 @@ EXPORT_SYMBOL_GPL(fuse_send_init);
 void fuse_free_conn(struct fuse_conn *fc)
 {
 	WARN_ON(!list_empty(&fc->devices));
+
+	fuse_iomap_unmount_bpf(fc);
 	kfree(fc);
 }
 EXPORT_SYMBOL_GPL(fuse_free_conn);
@@ -2373,6 +2376,10 @@ static int __init fuse_fs_init(void)
 	if (!fuse_inode_cachep)
 		goto out;
 
+	err = fuse_iomap_init_bpf();
+	if (err)
+		goto out2;
+
 	err = register_fuseblk();
 	if (err)
 		goto out2;
#103 lore raw

PATCH 2/5 fuse_trace: enable fuse servers to upload BPF programs to handle iomap requests

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Add tracepoints to the previous patch.

---
 fs/fuse/fuse_trace.h     |   53 ++++++++++++++++++++++++++++++++++++++++++++++
 fs/fuse/fuse_iomap_bpf.c |   10 +++++++++
 fs/fuse/trace.c          |    1 +
 3 files changed, 64 insertions(+)
fs/fuse/fuse_trace.h +53 -0
index aa2d5ca88c9d40..956881dada5252 100644
@@ -1657,6 +1657,59 @@ TRACE_EVENT(fuse_iomap_inval_mappings,
 		  FUSE_FILE_RANGE_PRINTK_ARGS(read),
 		  FUSE_FILE_RANGE_PRINTK_ARGS(write))
 );
+
+#ifdef CONFIG_BPF_SYSCALL
+DECLARE_EVENT_CLASS(fuse_iomap_bpf_ops_class,
+	TP_PROTO(const struct fuse_conn *fc, struct fuse_iomap_bpf_ops *ops),
+
+	TP_ARGS(fc, ops),
+
+	TP_STRUCT__entry(
+		__field(dev_t,			connection)
+		__string(name,			ops->name)
+	),
+
+	TP_fast_assign(
+		__entry->connection	=	fc->dev;
+		__assign_str(name);
+	),
+
+	TP_printk("connection %u iomap_ops %s",
+		  __entry->connection,
+		  __get_str(name))
+);
+#define DEFINE_FUSE_IOMAP_BPF_OPS_EVENT(name) \
+DEFINE_EVENT(fuse_iomap_bpf_ops_class, name, \
+	TP_PROTO(const struct fuse_conn *fc, struct fuse_iomap_bpf_ops *ops), \
+	TP_ARGS(fc, ops))
+DEFINE_FUSE_IOMAP_BPF_OPS_EVENT(fuse_iomap_attach_bpf);
+DEFINE_FUSE_IOMAP_BPF_OPS_EVENT(fuse_iomap_detach_bpf);
+
+DECLARE_EVENT_CLASS(fuse_iomap_bpf_class,
+	TP_PROTO(const struct inode *inode),
+
+	TP_ARGS(inode),
+
+	TP_STRUCT__entry(
+		FUSE_INODE_FIELDS
+	),
+
+	TP_fast_assign(
+		FUSE_INODE_ASSIGN(inode, fi, fm);
+	),
+
+	TP_printk(FUSE_INODE_FMT,
+		  FUSE_INODE_PRINTK_ARGS)
+);
+#define DEFINE_FUSE_IOMAP_BPF_EVENT(name) \
+DEFINE_EVENT(fuse_iomap_bpf_class, name, \
+	TP_PROTO(const struct inode *inode), \
+	TP_ARGS(inode))
+DEFINE_FUSE_IOMAP_BPF_EVENT(fuse_iomap_begin_bpf);
+DEFINE_FUSE_IOMAP_BPF_EVENT(fuse_iomap_end_bpf);
+DEFINE_FUSE_IOMAP_BPF_EVENT(fuse_iomap_ioend_bpf);
+
+#endif /* CONFIG_BPF_SYSCALL */
 #endif /* CONFIG_FUSE_IOMAP */
 
 #endif /* _TRACE_FUSE_H */
fs/fuse/fuse_iomap_bpf.c +10 -0
index b104f3961721b2..d4b826e4440ca7 100644
@@ -115,6 +115,8 @@ static int fuse_iomap_bpf_reg(void *kdata, struct bpf_link *link)
 		return -EBUSY;
 	}
 
+	trace_fuse_iomap_attach_bpf(fc, ops);
+
 	/*
 	 * The initial ops user count bias is transferred to fc so that we only
 	 * initiate wakeup events when someone tries to unregister the BPF.
@@ -155,6 +157,8 @@ DEFINE_CLASS(iomap_bpf_ops, struct fuse_iomap_bpf_ops *,
 static void __fuse_iomap_detach_bpf(struct fuse_conn *fc,
 				    struct fuse_iomap_bpf_ops *ops)
 {
+	trace_fuse_iomap_detach_bpf(fc, ops);
+
 	ops->fc = NULL;
 	rcu_assign_pointer(fc->iomap_conn.bpf_ops, NULL);
 	fuse_iomap_put_bpf_ops(ops);
@@ -269,6 +273,8 @@ int fuse_iomap_begin_bpf(struct inode *inode,
 	if (!bpf_ops || !bpf_ops->iomap_begin)
 		return -ENOSYS;
 
+	trace_fuse_iomap_begin_bpf(inode);
+
 	ret = bpf_ops->iomap_begin(fi, inarg->pos, inarg->count,
 				   inarg->opflags, outarg);
 	return bpf_to_errno(ret);
@@ -285,6 +291,8 @@ int fuse_iomap_end_bpf(struct inode *inode,
 	if (!bpf_ops || !bpf_ops->iomap_end)
 		return -ENOSYS;
 
+	trace_fuse_iomap_end_bpf(inode);
+
 	ret = bpf_ops->iomap_end(fi, inarg->pos, inarg->count,
 				 inarg->written, inarg->opflags);
 	return bpf_to_errno(ret);
@@ -302,6 +310,8 @@ int fuse_iomap_ioend_bpf(struct inode *inode,
 	if (!bpf_ops || !bpf_ops->iomap_ioend)
 		return -ENOSYS;
 
+	trace_fuse_iomap_ioend_bpf(inode);
+
 	ret = bpf_ops->iomap_ioend(fi, inarg->pos, inarg->written,
 				   inarg->flags, inarg->error, inarg->dev,
 				   inarg->new_addr, outarg);
fs/fuse/trace.c +1 -0
index 69310d6f773ffa..7d65ccdc2be609 100644
@@ -9,6 +9,7 @@
 #include "fuse_iomap.h"
 #include "fuse_iomap_i.h"
 #include "fuse_iomap_cache.h"
+#include "fuse_iomap_bpf.h"
 
 #include <linux/pagemap.h>
 #include <linux/iomap.h>
#104 lore raw

PATCH 3/5 fuse: prevent iomap bpf programs from writing to most of the system

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

The stub implementation fuse_iomap_bpf_ops_btf_struct_access has the unfortunate behavior of allowing the bpf program to write to any struct pointer passed into the function! We don't want to allow random updates to struct fuse_inode, but we will eventually want to pass the pointer as a dumb cookie to the kfunc added a few patches from now.

Therefore, look up the btf types of the two structs for which the bpf program *can* write, and disallow all writes to any other structs. This requires a new export from the bpf subsystem.

---
 fs/fuse/fuse_iomap_bpf.c |   49 ++++++++++++++++++++++++++++++++++++++++++++++
 kernel/bpf/btf.c         |    1 +
 2 files changed, 50 insertions(+)
fs/fuse/fuse_iomap_bpf.c +49 -0
index d4b826e4440ca7..13b5d4b96b66b5 100644
@@ -5,6 +5,7 @@
  * Copied from: Joanne Koong <[email protected]>
  */
 #include <linux/bpf.h>
+#include <linux/bpf_verifier.h>
 
 #include "fuse_i.h"
 #include "fuse_dev_i.h"
@@ -12,6 +13,8 @@
 #include "fuse_iomap_i.h"
 #include "fuse_trace.h"
 
+static const struct btf_type *iomap_begin_out_type, *iomap_ioend_out_type;
+
 /* spinlock for atomically updating fuse_conn <-> bpf_ops pointers */
 static DEFINE_SPINLOCK(fuse_iomap_bpf_ops_lock);
 
@@ -38,6 +41,14 @@ static int fuse_iomap_bpf_ops_btf_struct_access(struct bpf_verifier_log *log,
 						const struct bpf_reg_state *reg,
 						int off, int size)
 {
+	const struct btf_type *t = btf_type_by_id(reg->btf, reg->btf_id);
+
+	if (t != iomap_begin_out_type && t != iomap_ioend_out_type) {
+		bpf_log(log,
+			"Cannot write to memory from a fuse-iomap program\n");
+		return -EACCES;
+	}
+
 	return 0;
 }
 
@@ -47,8 +58,46 @@ static const struct bpf_verifier_ops fuse_iomap_bpf_verifier_ops = {
 	.btf_struct_access	= fuse_iomap_bpf_ops_btf_struct_access,
 };
 
+static const struct btf_type *
+fuse_iomap_find_struct_type(struct btf *btf, const char *name)
+{
+	struct btf *some_btf;
+	const struct btf_type *ret;
+	s32 type_id;
+
+	type_id = bpf_find_btf_id(name, BTF_KIND_STRUCT, &some_btf);
+	if (type_id < 0)
+		return ERR_PTR(-ENOENT);
+
+	/*
+	 * It's only safe to alias a btf_type without a ref to the btf object
+	 * if the type is from the current module because the btf object won't
+	 * go away until the module unloads.
+	 */
+	if (some_btf == btf)
+		ret = btf_type_by_id(some_btf, type_id);
+	else
+		ret = ERR_PTR(-ENOENT);
+	btf_put(some_btf);
+
+	return ret;
+}
+
 static int fuse_iomap_bpf_ops_init(struct btf *btf)
 {
+	const struct btf_type *t1, *t2;
+
+	t1 = fuse_iomap_find_struct_type(btf, "fuse_iomap_begin_out");
+	if (IS_ERR(t1))
+		return PTR_ERR(t1);
+
+	t2 = fuse_iomap_find_struct_type(btf, "fuse_iomap_ioend_out");
+	if (IS_ERR(t2))
+		return PTR_ERR(t2);
+
+	iomap_begin_out_type = t1;
+	iomap_ioend_out_type = t2;
+
 	return 0;
 }
 
kernel/bpf/btf.c +1 -0
index 4872d2a6c42d3a..9a5b6480243f14 100644
@@ -1890,6 +1890,7 @@ void btf_put(struct btf *btf)
 		call_rcu(&btf->rcu, btf_free_rcu);
 	}
 }
+EXPORT_SYMBOL_GPL(btf_put);
 
 struct btf *btf_base_btf(const struct btf *btf)
 {
#105 lore raw

PATCH 4/5 fuse: add kfuncs for iomap bpf programs to manage the cache

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Add a couple of kfuncs so that a BPF program that generates iomappings can add them to the inode's mapping cache, thereby avoiding the need to go into BPF program on the next access.

---
 fs/fuse/fuse_iomap_i.h   |    6 ++++
 fs/fuse/fuse_iomap.c     |    4 +-
 fs/fuse/fuse_iomap_bpf.c |   76 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 84 insertions(+), 2 deletions(-)
fs/fuse/fuse_iomap_i.h +6 -0
index c37a7c5cfc862f..70c5a831dc9134 100644
@@ -40,6 +40,12 @@ while (static_branch_unlikely(&fuse_iomap_debug)) {			\
 	unlikely(__cond);						\
 })
 #endif /* CONFIG_FUSE_IOMAP_DEBUG */
+
+int fuse_iomap_inval_inode(struct inode *inode,
+		const struct fuse_iomap_inval_mappings_out *outarg);
+int fuse_iomap_upsert_inode(struct inode *inode,
+		const struct fuse_iomap_upsert_mappings_out *outarg);
+
 #endif /* CONFIG_FUSE_IOMAP */
 
 #endif /* _FS_FUSE_IOMAP_I_H */
fs/fuse/fuse_iomap.c +2 -2
index 2e0c35e879ffcc..d2642eef59b779 100644
@@ -2782,7 +2782,7 @@ fuse_iomap_upsert_validate_mappings(struct inode *inode,
 						  &outarg->write);
 }
 
-static int fuse_iomap_upsert_inode(struct inode *inode,
+int fuse_iomap_upsert_inode(struct inode *inode,
 		const struct fuse_iomap_upsert_mappings_out *outarg)
 {
 	int ret = fuse_iomap_upsert_validate_mappings(inode, outarg);
@@ -2877,7 +2877,7 @@ fuse_iomap_inval_validate_range(const struct inode *inode,
 	return true;
 }
 
-static int fuse_iomap_inval_inode(struct inode *inode,
+int fuse_iomap_inval_inode(struct inode *inode,
 		const struct fuse_iomap_inval_mappings_out *outarg)
 {
 	int ret = 0, ret2 = 0;
fs/fuse/fuse_iomap_bpf.c +76 -0

94 lines not on this page · raw message

#106 lore raw

PATCH 5/5 fuse: make fuse_inode opaque to iomap bpf programs

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Introduce an opaque type (and some casting helpers) for struct fuse_inode. This tricks BTF/BPF into thinking that the "inode" object we pass to BPF programs is an empty structure, which means that the BPF program cannot even look at the contents.

---
 fs/fuse/fuse_iomap_bpf.h |   41 ++++++++++++++++++++++++++++++++++++++---
 fs/fuse/fuse_iomap_bpf.c |   42 +++++++++++++++++++++++++++++++-----------
 2 files changed, 69 insertions(+), 14 deletions(-)
fs/fuse/fuse_iomap_bpf.h +38 -3
index f6bfd2133bf2bb..148e8f1fa1d1fb 100644
@@ -15,12 +15,47 @@ enum fuse_iomap_bpf_ret {
 	FIB_HANDLED = 1,
 };
 
+/* opaque structure so that bpf programs cannot see inside a fuse inode */
+struct fuse_bpf_inode { };
+
+static inline const struct fuse_inode *
+__fuse_inode_from_bpf_c(const struct fuse_bpf_inode *fbi)
+{
+	return (const struct fuse_inode *)fbi;
+}
+
+static inline struct fuse_inode *
+__fuse_inode_from_bpf(struct fuse_bpf_inode *fbi)
+{
+	return (struct fuse_inode *)fbi;
+}
+
+#define fuse_inode_from_bpf(x) _Generic((x), \
+	struct fuse_bpf_inode * :	__fuse_inode_from_bpf, \
+	const struct fuse_bpf_inode * :	__fuse_inode_from_bpf_c)(x)
+
+static inline const struct fuse_bpf_inode *
+__fuse_inode_to_bpf_c(const struct fuse_inode *fi)
+{
+	return (const struct fuse_bpf_inode *)fi;
+}
+
+static inline struct fuse_bpf_inode *
+__fuse_inode_to_bpf(struct fuse_inode *fi)
+{
+	return (struct fuse_bpf_inode *)fi;
+}
+
+#define fuse_inode_to_bpf(x) _Generic((x), \
+	struct fuse_inode * :		__fuse_inode_to_bpf, \
+	const struct fuse_inode * :	__fuse_inode_to_bpf_c)(x)
+
 struct fuse_iomap_bpf_ops {
 	/**
 	 * @iomap_begin: override iomap_begin.  See FUSE_IOMAP_BEGIN for
 	 * details.
 	 */
-	enum fuse_iomap_bpf_ret (*iomap_begin)(struct fuse_inode *fi,
+	enum fuse_iomap_bpf_ret (*iomap_begin)(struct fuse_bpf_inode *fbi,
 			uint64_t pos, uint64_t count, uint32_t opflags,
 			struct fuse_iomap_begin_out *outarg);
 
@@ -28,7 +63,7 @@ struct fuse_iomap_bpf_ops {
 	 * @iomap_end: override iomap_end.  See FUSE_IOMAP_END for
 	 * details.
 	 */
-	enum fuse_iomap_bpf_ret (*iomap_end)(struct fuse_inode *fi,
+	enum fuse_iomap_bpf_ret (*iomap_end)(struct fuse_bpf_inode *fbi,
 			uint64_t pos, uint64_t count, int64_t written,
 			uint32_t opflags);
 
@@ -36,7 +71,7 @@ struct fuse_iomap_bpf_ops {
 	 * @iomap_ioend: override iomap_ioend.  See FUSE_IOMAP_IOEND for
 	 * details.
 	 */
-	enum fuse_iomap_bpf_ret (*iomap_ioend)(struct fuse_inode *fi,
+	enum fuse_iomap_bpf_ret (*iomap_ioend)(struct fuse_bpf_inode *fbi,
 			uint64_t pos, int64_t written, uint32_t ioendflags,
 			int error, uint32_t dev, uint64_t new_addr,
 			struct fuse_iomap_ioend_out *outarg);
fs/fuse/fuse_iomap_bpf.c +31 -11

107 lines not on this page · raw message

#107 lore raw

PATCH v7 1/6 libfuse: allow servers to use iomap for better file IO performance

In reply to Darrick J. Wong

Hi all,

This series connects libfuse to the iomap-enabled fuse driver in Linux to get fuse servers out of the business of handling file I/O themselves. By keeping the IO path mostly within the kernel, we can dramatically improve the speed of disk-based filesystems. This enables us to move all the filesystem metadata parsing code out of the kernel and into userspace, which means that we can containerize them for security without losing a lot of performance.

If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.

With a bit of luck, this should all go splendidly. Comments and questions are, as always, welcome.

--D

kernel git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-fileio
---
Commits in this patchset:
* libfuse: bump kernel and library ABI versions
* libfuse: wait in do_destroy until all open files are closed
* libfuse: add kernel gates for FUSE_IOMAP
* libfuse: add fuse commands for iomap_begin and end
* libfuse: add upper level iomap commands
* libfuse: add a lowlevel notification to add a new device to iomap
* libfuse: add upper-level iomap add device function
* libfuse: add iomap ioend low level handler
* libfuse: add upper level iomap ioend commands
* libfuse: add a reply function to send FUSE_ATTR_* to the kernel
* libfuse: connect high level fuse library to fuse_reply_attr_iflags
* libfuse: support enabling exclusive mode for files
* libfuse: support direct I/O through iomap
* libfuse: don't allow hardlinking of iomap files in the upper level fuse library
* libfuse: allow discovery of the kernel's iomap capabilities
* libfuse: add lower level iomap_config implementation
* libfuse: add upper level iomap_config implementation
* libfuse: add low level code to invalidate iomap block device ranges
* libfuse: add upper-level API to invalidate parts of an iomap block device
* libfuse: add atomic write support
* libfuse: allow disabling of fs memory reclaim and write throttling
* libfuse: create a helper to transform an open regular file into an open loopdev
* libfuse: add swapfile support for iomap files
* libfuse: add lower-level filesystem freeze, thaw, and shutdown requests
* libfuse: add upper-level filesystem freeze, thaw, and shutdown events

---
 include/fuse.h          |  102 ++++++++
 include/fuse_common.h   |  143 +++++++++++
 include/fuse_kernel.h   |  147 ++++++++++++
 include/fuse_loopdev.h  |   27 ++
 include/fuse_lowlevel.h |  305 ++++++++++++++++++++++++
 lib/fuse_i.h            |    4 
 ChangeLog.rst           |    5 
 include/meson.build     |    4 
 lib/fuse.c              |  591 +++++++++++++++++++++++++++++++++++++++++++----
 lib/fuse_loopdev.c      |  403 ++++++++++++++++++++++++++++++++
 lib/fuse_lowlevel.c     |  560 +++++++++++++++++++++++++++++++++++++++++++--
 lib/fuse_versionscript  |   23 ++
 lib/meson.build         |    7 -
 meson.build             |   13 +
 14 files changed, 2251 insertions(+), 83 deletions(-)
 create mode 100644 include/fuse_loopdev.h
 create mode 100644 lib/fuse_loopdev.c
#108 lore raw

PATCH 1/25 libfuse: bump kernel and library ABI versions

From: Darrick J. Wong <[email protected]>

Bump the kernel ABI version to 7.99 and the libfuse ABI version to 3.99 to start our development. This patch exists to avoid confusion during the prototyping stage.

---
 include/fuse_kernel.h  |    5 ++++-
 ChangeLog.rst          |    5 +++++
 lib/fuse_versionscript |    3 +++
 lib/meson.build        |    4 ++--
 meson.build            |    2 +-
 5 files changed, 15 insertions(+), 4 deletions(-)
include/fuse_kernel.h +4 -1
index f0dee3d6cf51b0..842cc08a083a6f 100644
@@ -240,6 +240,9 @@
  *  - add FUSE_COPY_FILE_RANGE_64
  *  - add struct fuse_copy_file_range_out
  *  - add FUSE_NOTIFY_PRUNE
+ *
+ *  7.99
+ *  - XXX magic minor revision to make experimental code really obvious
  */
 
 #ifndef _LINUX_FUSE_H
@@ -275,7 +278,7 @@
 #define FUSE_KERNEL_VERSION 7
 
 /** Minor version number of this interface */
-#define FUSE_KERNEL_MINOR_VERSION 45
+#define FUSE_KERNEL_MINOR_VERSION 99
 
 /** The node ID of the root inode */
 #define FUSE_ROOT_ID 1
ChangeLog.rst +5 -0
index 15c998cf1623b8..3cb95081d42d06 100644
@@ -1,3 +1,8 @@
+libfuse 3.99-rc0 (2025-12-19)
+===============================
+
+* Add prototypes of iomap and syncfs (djwong)
+
 libfuse 3.18.0 (2025-12-18)
 ===========================
 
lib/fuse_versionscript +3 -0
index cce09610316f4b..826d9fee00a8ee 100644
@@ -229,6 +229,9 @@ FUSE_3.19 {
 		fuse_lowlevel_notify_prune;
 } FUSE_3.18;
 
+FUSE_3.99 {
+} FUSE_3.19;
+
 # Local Variables:
 # indent-tabs-mode: t
 # End:
lib/meson.build +2 -2
index fcd95741c9d374..a3d3d49f9ba42b 100644
@@ -44,12 +44,12 @@ fusermount_path = join_paths(get_option('prefix'), get_option('bindir'))
 libfuse = library('fuse3',
                   libfuse_sources,
                   version: base_version,
-                  soversion: '4',
+                  soversion: '99',
                   include_directories: include_dirs,
                   dependencies: deps,
                   install: true,
                   link_depends: 'fuse_versionscript',
-                  c_args: [ '-DFUSE_USE_VERSION=317',
+                  c_args: [ '-DFUSE_USE_VERSION=399',
                             '-DFUSERMOUNT_DIR="@0@"'.format(fusermount_path) ],
                   link_args: ['-Wl,--version-script,' + meson.current_source_dir()
                               + '/fuse_versionscript' ])
meson.build +1 -1
index 80c5f1dc0bd356..8359a489c351b9 100644
@@ -1,5 +1,5 @@
 project('libfuse3', ['c'],
-        version: '3.19.0-rc0',
+        version: '3.99.0-rc0',  # Version with RC suffix
         meson_version: '>= 0.60.0',
         default_options: [
             'buildtype=debugoptimized',
#109 lore raw

PATCH 2/25 libfuse: wait in do_destroy until all open files are closed

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

This patch complements the Linux kernel patch "fuse: flush pending fuse events before aborting the connection".

This test opens a large number of files, unlinks them (which really just renames them to fuse hidden files), closes the program, unmounts the filesystem, and runs fsck to check that there aren't any inconsistencies in the filesystem.

Unfortunately, the 488.full file shows that there are a lot of hidden files left over in the filesystem, with incorrect link counts. Tracing fuse_request_* shows that there are a large number of FUSE_RELEASE commands that are queued up on behalf of the unlinked files at the time that fuse_conn_destroy calls fuse_abort_conn. Had the connection not aborted, the fuse server would have responded to the RELEASE commands by removing the hidden files; instead they stick around.

For upper-level fuse servers that don't use fuseblk mode this isn't a problem because libfuse responds to the connection going down by pruning its inode cache and calling the fuse server's ->release for any open files before calling the server's ->destroy function.

For fuseblk servers this is a problem, however, because the kernel sends FUSE_DESTROY to the fuse server, and the fuse server has to write all of its pending changes to the block device before replying to the DESTROY request because the kernel releases its O_EXCL hold on the block device. This means that the kernel must flush all pending FUSE_RELEASE requests before issuing FUSE_DESTROY.

For fuse-iomap servers this will also be a problem because iomap servers are expected to release all exclusively-held resources before unmount returns from the kernel.

Create a function to push all the background requests to the queue before sending FUSE_DESTROY. That way, all the pending file release events are processed by the fuse server before it tears itself down, and we don't end up with a corrupt filesystem.

Note that multithreaded fuse servers will need to track the number of open files and defer a FUSE_DESTROY request until that number reaches zero. An earlier version of this patch made the kernel wait for the RELEASE acknowledgements before sending DESTROY, but the kernel people weren't comfortable with adding blocking waits to unmount.

This patch implements the deferral for the multithreaded libfuse backend. However, we must implement this deferral by starting a new background thread because libfuse in io_uring mode starts up a bunch of threads, each of which submit batches of SQEs to request fuse commands, and then waits for the kernel to mark some CQEs to note which slots now have fuse commands to process. Each uring thread processes the fuse comands in the CQE serially, which means that _do_destroy can't just wait for the open file counter to hit zero; it has to start a new background thread to do that, so that it can continue to process pending fuse commands.

[Aside: is this bad for fuse command processing latency? Suppose we get two CQE completions, then the second command won't even be looked at until the first one is done.]

Non-uring fuse by contrast reads one fuse command and processes it immediately, so one command taking a long time won't stall any other commands.

---
 lib/fuse_i.h        |    4 ++
 lib/fuse_lowlevel.c |  107 ++++++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 104 insertions(+), 7 deletions(-)
lib/fuse_i.h +4 -0
index 65d2f68f7f3091..c7d0d38408105f 100644
@@ -122,6 +122,10 @@ struct fuse_session {
 	 */
 	uint32_t conn_want;
 	uint64_t conn_want_ext;
+
+	/* destroy has to wait for all the open files to go away */
+	pthread_cond_t zero_open_files;
+	uint64_t open_files;
 };
 
 struct fuse_chan {
lib/fuse_lowlevel.c +100 -7

194 lines not on this page · raw message

#110 lore raw

PATCH 3/25 libfuse: add kernel gates for FUSE_IOMAP

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Add some flags to query and request kernel support for filesystem iomap for regular files.

---
 include/fuse_common.h |    5 +++++
 include/fuse_kernel.h |    3 +++
 lib/fuse_lowlevel.c   |   12 +++++++++++-
 3 files changed, 19 insertions(+), 1 deletion(-)
include/fuse_common.h +5 -0
index 041188ec7fa732..9d53354de78868 100644
@@ -512,6 +512,11 @@ struct fuse_loop_config_v1 {
  */
 #define FUSE_CAP_OVER_IO_URING (1UL << 31)
 
+/**
+ * Client supports using iomap for regular file operations
+ */
+#define FUSE_CAP_IOMAP (1ULL << 32)
+
 /**
  * Ioctl flags
  *
include/fuse_kernel.h +3 -0
index 842cc08a083a6f..354a6da01c2ecc 100644
@@ -243,6 +243,7 @@
  *
  *  7.99
  *  - XXX magic minor revision to make experimental code really obvious
+ *  - add FUSE_IOMAP and iomap_{begin,end,ioend} for regular file operations
  */
 
 #ifndef _LINUX_FUSE_H
@@ -451,6 +452,7 @@ struct fuse_file_lock {
  * FUSE_OVER_IO_URING: Indicate that client supports io-uring
  * FUSE_REQUEST_TIMEOUT: kernel supports timing out requests.
  *			 init_out.request_timeout contains the timeout (in secs)
+ * FUSE_IOMAP: Client supports iomap for regular file operations
  */
 #define FUSE_ASYNC_READ		(1 << 0)
 #define FUSE_POSIX_LOCKS	(1 << 1)
@@ -498,6 +500,7 @@ struct fuse_file_lock {
 #define FUSE_ALLOW_IDMAP	(1ULL << 40)
 #define FUSE_OVER_IO_URING	(1ULL << 41)
 #define FUSE_REQUEST_TIMEOUT	(1ULL << 42)
+#define FUSE_IOMAP		(1ULL << 43)
 
 /**
  * CUSE INIT request/reply flags
lib/fuse_lowlevel.c +11 -1
index c3d57f5c5b104c..237c02c6dce7d5 100644
@@ -2817,7 +2817,10 @@ _do_init(fuse_req_t req, const fuse_ino_t nodeid, const void *op_in,
 			se->conn.capable_ext |= FUSE_CAP_NO_EXPORT_SUPPORT;
 		if (inargflags & FUSE_OVER_IO_URING)
 			se->conn.capable_ext |= FUSE_CAP_OVER_IO_URING;
-
+		if (inargflags & FUSE_IOMAP)
+			se->conn.capable_ext |= FUSE_CAP_IOMAP;
+		/* Don't let anyone touch iomap until the end of the patchset. */
+		se->conn.capable_ext &= ~FUSE_CAP_IOMAP;
 	} else {
 		se->conn.max_readahead = 0;
 	}
@@ -2863,6 +2866,9 @@ _do_init(fuse_req_t req, const fuse_ino_t nodeid, const void *op_in,
 		       FUSE_CAP_READDIRPLUS_AUTO);
 	LL_SET_DEFAULT(1, FUSE_CAP_OVER_IO_URING);
 
+	/* servers need to opt-in to iomap explicitly */
+	LL_SET_DEFAULT(0, FUSE_CAP_IOMAP);
+
 	/* This could safely become default, but libfuse needs an API extension
 	 * to support it
 	 * LL_SET_DEFAULT(1, FUSE_CAP_SETXATTR_EXT);
@@ -2980,6 +2986,8 @@ _do_init(fuse_req_t req, const fuse_ino_t nodeid, const void *op_in,
 		outargflags |= FUSE_REQUEST_TIMEOUT;
 		outarg.request_timeout = se->conn.request_timeout;
 	}
+	if (se->conn.want_ext & FUSE_CAP_IOMAP)
+		outargflags |= FUSE_IOMAP;
 
 	outarg.max_readahead = se->conn.max_readahead;
 	outarg.max_write = se->conn.max_write;
@@ -3014,6 +3022,8 @@ _do_init(fuse_req_t req, const fuse_ino_t nodeid, const void *op_in,
 		if (se->conn.want_ext & FUSE_CAP_PASSTHROUGH)
 			fuse_log(FUSE_LOG_DEBUG, "   max_stack_depth=%u\n",
 				outarg.max_stack_depth);
+		if (se->conn.want_ext & FUSE_CAP_IOMAP)
+			fuse_log(FUSE_LOG_DEBUG, "   iomap=1\n");
 	}
 	if (arg->minor < 5)
 		outargsize = FUSE_COMPAT_INIT_OUT_SIZE;
#111 lore raw

PATCH 4/25 libfuse: add fuse commands for iomap_begin and end

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Teach the low level API how to handle iomap begin and end commands that we get from the kernel.

---
 include/fuse_common.h   |   71 +++++++++++++++++++++++++++++++++
 include/fuse_kernel.h   |   40 ++++++++++++++++++
 include/fuse_lowlevel.h |   59 +++++++++++++++++++++++++++
 lib/fuse_lowlevel.c     |  102 +++++++++++++++++++++++++++++++++++++++++++++++
 lib/fuse_versionscript  |    3 +
 5 files changed, 275 insertions(+)
include/fuse_common.h +71 -0
index 9d53354de78868..58726ce43b1014 100644
@@ -1135,7 +1135,78 @@ bool fuse_get_feature_flag(struct fuse_conn_info *conn, uint64_t flag);
  */
 int fuse_convert_to_conn_want_ext(struct fuse_conn_info *conn);
 
+/**
+ * iomap operations.
+ * These APIs are introduced in version 399 (FUSE_MAKE_VERSION(3, 99)).
+ */
 
+/* mapping types; see corresponding IOMAP_TYPE_ */
+#define FUSE_IOMAP_TYPE_HOLE		(0)
+#define FUSE_IOMAP_TYPE_DELALLOC	(1)
+#define FUSE_IOMAP_TYPE_MAPPED		(2)
+#define FUSE_IOMAP_TYPE_UNWRITTEN	(3)
+#define FUSE_IOMAP_TYPE_INLINE		(4)
+
+/* fuse-specific mapping type indicating that writes use the read mapping */
+#define FUSE_IOMAP_TYPE_PURE_OVERWRITE	(255)
+
+#define FUSE_IOMAP_DEV_NULL		(0U)	/* null device cookie */
+
+/* mapping flags passed back from iomap_begin; see corresponding IOMAP_F_ */
+#define FUSE_IOMAP_F_NEW		(1U << 0)
+#define FUSE_IOMAP_F_DIRTY		(1U << 1)
+#define FUSE_IOMAP_F_SHARED		(1U << 2)
+#define FUSE_IOMAP_F_MERGED		(1U << 3)
+#define FUSE_IOMAP_F_BOUNDARY		(1U << 4)
+#define FUSE_IOMAP_F_ANON_WRITE		(1U << 5)
+#define FUSE_IOMAP_F_ATOMIC_BIO		(1U << 6)
+
+/* fuse-specific mapping flag asking for ->iomap_end call */
+#define FUSE_IOMAP_F_WANT_IOMAP_END	(1U << 7)
+
+/* mapping flags passed to iomap_end */
+#define FUSE_IOMAP_F_SIZE_CHANGED	(1U << 8)
+#define FUSE_IOMAP_F_STALE		(1U << 9)
+
+/* operation flags from iomap; see corresponding IOMAP_* */
+#define FUSE_IOMAP_OP_WRITE		(1U << 0)
+#define FUSE_IOMAP_OP_ZERO		(1U << 1)
+#define FUSE_IOMAP_OP_REPORT		(1U << 2)
+#define FUSE_IOMAP_OP_FAULT		(1U << 3)
+#define FUSE_IOMAP_OP_DIRECT		(1U << 4)
+#define FUSE_IOMAP_OP_NOWAIT		(1U << 5)
+#define FUSE_IOMAP_OP_OVERWRITE_ONLY	(1U << 6)
+#define FUSE_IOMAP_OP_UNSHARE		(1U << 7)
+#define FUSE_IOMAP_OP_DAX		(1U << 8)
+#define FUSE_IOMAP_OP_ATOMIC		(1U << 9)
+#define FUSE_IOMAP_OP_DONTCACHE		(1U << 10)
+
+/* pagecache writeback operation */
+#define FUSE_IOMAP_OP_WRITEBACK		(1U << 31)
+
+#define FUSE_IOMAP_NULL_ADDR		(-1ULL)	/* addr is not valid */
+
+struct fuse_file_iomap {
+	uint64_t offset;	/* file offset of mapping, bytes */
+	uint64_t length;	/* length of mapping, bytes */
+	uint64_t addr;		/* disk offset of mapping, bytes */
+	uint16_t type;		/* FUSE_IOMAP_TYPE_* */
+	uint16_t flags;		/* FUSE_IOMAP_F_* */
+	uint32_t dev;		/* device cookie */
+};
+
+static inline bool fuse_iomap_is_write(unsigned int opflags)
+{
+	return opflags & (FUSE_IOMAP_OP_WRITE | FUSE_IOMAP_OP_ZERO |
+			  FUSE_IOMAP_OP_UNSHARE | FUSE_IOMAP_OP_WRITEBACK);
+}
+
+static inline bool fuse_iomap_need_write_allocate(unsigned int opflags,
+					const struct fuse_file_iomap *map)
+{
+	return map->type == FUSE_IOMAP_TYPE_HOLE &&
+		!(opflags & FUSE_IOMAP_OP_ZERO);
+}
 
 /* ----------------------------------------------------------- *
  * Compatibility stuff					       *
include/fuse_kernel.h +40 -0
index 354a6da01c2ecc..b3750bb6275620 100644
@@ -670,6 +670,9 @@ enum fuse_opcode {
 	FUSE_STATX		= 52,
 	FUSE_COPY_FILE_RANGE_64	= 53,
 
+	FUSE_IOMAP_BEGIN	= 4094,
+	FUSE_IOMAP_END		= 4095,
+
 	/* CUSE specific operations */
 	CUSE_INIT		= 4096,
 
@@ -1313,4 +1316,41 @@ struct fuse_uring_cmd_req {
 	uint8_t padding[6];
 };
 
+struct fuse_iomap_io {
+	uint64_t offset;	/* file offset of mapping, bytes */
+	uint64_t length;	/* length of mapping, bytes */
+	uint64_t addr;		/* disk offset of mapping, bytes */
+	uint16_t type;		/* FUSE_IOMAP_TYPE_* */
+	uint16_t flags;		/* FUSE_IOMAP_F_* */
+	uint32_t dev;		/* device cookie */
+};
+
+struct fuse_iomap_begin_in {
+	uint32_t opflags;	/* FUSE_IOMAP_OP_* */
+	uint32_t reserved;	/* zero */
+	uint64_t attr_ino;	/* matches fuse_attr:ino */
+	uint64_t pos;		/* file position, in bytes */
+	uint64_t count;		/* operation length, in bytes */
+};
+
+struct fuse_iomap_begin_out {
+	/* read file data from here */
+	struct fuse_iomap_io	read;
+
+	/* write file data to here, if applicable */
+	struct fuse_iomap_io	write;
+};
+
+struct fuse_iomap_end_in {
+	uint32_t opflags;	/* FUSE_IOMAP_OP_* */
+	uint32_t reserved;	/* zero */
+	uint64_t attr_ino;	/* matches fuse_attr:ino */
+	uint64_t pos;		/* file position, in bytes */
+	uint64_t count;		/* operation length, in bytes */
+	int64_t written;	/* bytes processed */
+
+	/* mapping that the kernel acted upon */
+	struct fuse_iomap_io	map;
+};
+
 #endif /* _LINUX_FUSE_H */
include/fuse_lowlevel.h +59 -0

71 lines not on this page · raw message

lib/fuse_lowlevel.c +102 -0

120 lines not on this page · raw message

lib/fuse_versionscript +3 -0
index 826d9fee00a8ee..e346ce29a7f7a3 100644
@@ -230,6 +230,9 @@ FUSE_3.19 {
 } FUSE_3.18;
 
 FUSE_3.99 {
+	global:
+		fuse_iomap_pure_overwrite;
+		fuse_reply_iomap_begin;
 } FUSE_3.19;
 
 # Local Variables:
#112 lore raw

PATCH 5/25 libfuse: add upper level iomap commands

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Teach the upper level fuse library about the iomap begin and end operations, and connect it to the lower level. This is needed for fuse2fs to start using iomap.

---
 include/fuse.h |   17 +++++++++
 lib/fuse.c     |  102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 119 insertions(+)
include/fuse.h +17 -0
index 595cac07f2be36..1c8caf0c2ee034 100644
@@ -885,6 +885,23 @@ struct fuse_operations {
 	 * calling fsync(2) on every file on the filesystem.
 	 */
 	int (*syncfs)(const char *path);
+
+	/**
+	 * Send a mapping to the kernel so that a file IO operation can run.
+	 */
+	int (*iomap_begin) (const char *path, uint64_t nodeid,
+			    uint64_t attr_ino, off_t pos_in,
+			    uint64_t length_in, uint32_t opflags_in,
+			    struct fuse_file_iomap *read_out,
+			    struct fuse_file_iomap *write_out);
+
+	/**
+	 * Respond to the outcome of a previous file mapping operation.
+	 */
+	int (*iomap_end) (const char *path, uint64_t nodeid, uint64_t attr_ino,
+			  off_t pos_in, uint64_t length_in,
+			  uint32_t opflags_in, ssize_t written_in,
+			  const struct fuse_file_iomap *iomap);
 };
 
 /** Extra context that may be needed by some filesystems
lib/fuse.c +102 -0

120 lines not on this page · raw message

#113 lore raw

PATCH 6/25 libfuse: add a lowlevel notification to add a new device to iomap

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Plumb in the pieces needed to attach block devices to a fuse+iomap mount for use with iomap operations. This enables us to have filesystems where the metadata could live somewhere else, but the actual file IO goes to locally attached storage.

---
 include/fuse_kernel.h   |    7 +++++++
 include/fuse_lowlevel.h |   30 +++++++++++++++++++++++++++++
 lib/fuse_lowlevel.c     |   49 +++++++++++++++++++++++++++++++++++++++++++++++
 lib/fuse_versionscript  |    2 ++
 4 files changed, 88 insertions(+)
include/fuse_kernel.h +7 -0
index b3750bb6275620..e69f9675c4b57d 100644
@@ -1135,6 +1135,13 @@ struct fuse_notify_prune_out {
 	uint64_t	spare;
 };
 
+#define FUSE_BACKING_TYPE_MASK		(0xFF)
+#define FUSE_BACKING_TYPE_PASSTHROUGH	(0)
+#define FUSE_BACKING_TYPE_IOMAP		(1)
+#define FUSE_BACKING_MAX_TYPE		(FUSE_BACKING_TYPE_IOMAP)
+
+#define FUSE_BACKING_FLAGS_ALL		(FUSE_BACKING_TYPE_MASK)
+
 struct fuse_backing_map {
 	int32_t		fd;
 	uint32_t	flags;
include/fuse_lowlevel.h +30 -0
index f01fb06d6737ba..7f9a56b0a7eda1 100644
@@ -2034,6 +2034,36 @@ int fuse_lowlevel_notify_retrieve(struct fuse_session *se, fuse_ino_t ino,
 int fuse_lowlevel_notify_prune(struct fuse_session *se,
 			       fuse_ino_t *nodeids, uint32_t count);
 
+/*
+ * Attach an open file descriptor to a fuse+iomap mount.  Currently must be
+ * a block device.
+ *
+ * Added in FUSE protocol version 7.99. If the kernel does not support
+ * this (or a newer) version, the function will return -ENOSYS and do
+ * nothing.
+ *
+ * @param se the session object
+ * @param fd file descriptor of an open block device
+ * @param flags flags for the operation; none defined so far
+ * @return positive nonzero device id on success, or negative errno on failure
+ */
+int fuse_lowlevel_iomap_device_add(struct fuse_session *se, int fd,
+				   unsigned int flags);
+
+/**
+ * Detach an open file from a fuse+iomap mount.  Must be a device id returned
+ * by fuse_lowlevel_iomap_device_add.
+ *
+ * Added in FUSE protocol version 7.99. If the kernel does not support
+ * this (or a newer) version, the function will return -ENOSYS and do
+ * nothing.
+ *
+ * @param se the session object
+ * @param device_id device index as returned by fuse_lowlevel_iomap_device_add
+ * @return 0 on success, or negative errno on failure
+ */
+int fuse_lowlevel_iomap_device_remove(struct fuse_session *se, int device_id);
+
 /* ----------------------------------------------------------- *
  * Utility functions					       *
  * ----------------------------------------------------------- */
lib/fuse_lowlevel.c +49 -0

55 lines not on this page · raw message

lib/fuse_versionscript +2 -0
index e346ce29a7f7a3..bd9255f95d9948 100644
@@ -233,6 +233,8 @@ FUSE_3.99 {
 	global:
 		fuse_iomap_pure_overwrite;
 		fuse_reply_iomap_begin;
+		fuse_lowlevel_iomap_device_add;
+		fuse_lowlevel_iomap_device_remove;
 } FUSE_3.19;
 
 # Local Variables:
#114 lore raw

PATCH 7/25 libfuse: add upper-level iomap add device function

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Make it so that the upper level fuse library can add iomap devices too.

---
 include/fuse.h         |   19 +++++++++++++++++++
 lib/fuse.c             |   16 ++++++++++++++++
 lib/fuse_versionscript |    2 ++
 3 files changed, 37 insertions(+)
include/fuse.h +19 -0
index 1c8caf0c2ee034..23b6d3f7c93303 100644
@@ -1403,6 +1403,25 @@ void fuse_fs_init(struct fuse_fs *fs, struct fuse_conn_info *conn,
 		struct fuse_config *cfg);
 void fuse_fs_destroy(struct fuse_fs *fs);
 
+/**
+ * Attach an open file descriptor to a fuse+iomap mount.  Currently must be
+ * a block device.
+ *
+ * @param fd file descriptor of an open block device
+ * @param flags flags for the operation; none defined so far
+ * @return positive nonzero device id on success, or negative errno on failure
+ */
+int fuse_fs_iomap_device_add(int fd, unsigned int flags);
+
+/**
+ * Detach an open file from a fuse+iomap mount.  Must be a device id returned
+ * by fuse_lowlevel_iomap_device_add.
+ *
+ * @param device_id device index as returned by fuse_lowlevel_iomap_device_add
+ * @return 0 on success, or negative errno on failure
+ */
+int fuse_fs_iomap_device_remove(int device_id);
+
 int fuse_notify_poll(struct fuse_pollhandle *ph);
 
 /**
lib/fuse.c +16 -0
index ac325b1a3d9e82..9662d007746809 100644
@@ -2847,6 +2847,22 @@ static int fuse_fs_iomap_end(struct fuse_fs *fs, const char *path,
 				written, iomap);
 }
 
+int fuse_fs_iomap_device_add(int fd, unsigned int flags)
+{
+	struct fuse_context *ctxt = fuse_get_context();
+	struct fuse_session *se = fuse_get_session(ctxt->fuse);
+
+	return fuse_lowlevel_iomap_device_add(se, fd, flags);
+}
+
+int fuse_fs_iomap_device_remove(int device_id)
+{
+	struct fuse_context *ctxt = fuse_get_context();
+	struct fuse_session *se = fuse_get_session(ctxt->fuse);
+
+	return fuse_lowlevel_iomap_device_remove(se, device_id);
+}
+
 static void fuse_lib_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,
 			     int valid, struct fuse_file_info *fi)
 {
lib/fuse_versionscript +2 -0
index bd9255f95d9948..5c4a7dc53b33f3 100644
@@ -235,6 +235,8 @@ FUSE_3.99 {
 		fuse_reply_iomap_begin;
 		fuse_lowlevel_iomap_device_add;
 		fuse_lowlevel_iomap_device_remove;
+		fuse_fs_iomap_device_add;
+		fuse_fs_iomap_device_remove;
 } FUSE_3.19;
 
 # Local Variables:
#115 lore raw

PATCH 8/25 libfuse: add iomap ioend low level handler

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Teach the low level library about the iomap ioend handler, which gets called by the kernel when we finish a file write that isn't a pure overwrite operation.

---
 include/fuse_common.h   |   13 +++++++++++++
 include/fuse_kernel.h   |   16 ++++++++++++++++
 include/fuse_lowlevel.h |   34 ++++++++++++++++++++++++++++++++++
 lib/fuse_lowlevel.c     |   32 ++++++++++++++++++++++++++++++++
 lib/fuse_versionscript  |    1 +
 5 files changed, 96 insertions(+)
include/fuse_common.h +13 -0
index 58726ce43b1014..013e74a0e9eefe 100644
@@ -1208,6 +1208,19 @@ static inline bool fuse_iomap_need_write_allocate(unsigned int opflags,
 		!(opflags & FUSE_IOMAP_OP_ZERO);
 }
 
+/* out of place write extent */
+#define FUSE_IOMAP_IOEND_SHARED		(1U << 0)
+/* unwritten extent */
+#define FUSE_IOMAP_IOEND_UNWRITTEN	(1U << 1)
+/* don't merge into previous ioend */
+#define FUSE_IOMAP_IOEND_BOUNDARY	(1U << 2)
+/* is direct I/O */
+#define FUSE_IOMAP_IOEND_DIRECT		(1U << 3)
+/* is append ioend */
+#define FUSE_IOMAP_IOEND_APPEND		(1U << 4)
+/* is pagecache writeback */
+#define FUSE_IOMAP_IOEND_WRITEBACK	(1U << 5)
+
 /* ----------------------------------------------------------- *
  * Compatibility stuff					       *
  * ----------------------------------------------------------- */
include/fuse_kernel.h +16 -0
index e69f9675c4b57d..732085a1b900b0 100644
@@ -670,6 +670,7 @@ enum fuse_opcode {
 	FUSE_STATX		= 52,
 	FUSE_COPY_FILE_RANGE_64	= 53,
 
+	FUSE_IOMAP_IOEND	= 4093,
 	FUSE_IOMAP_BEGIN	= 4094,
 	FUSE_IOMAP_END		= 4095,
 
@@ -1360,4 +1361,19 @@ struct fuse_iomap_end_in {
 	struct fuse_iomap_io	map;
 };
 
+struct fuse_iomap_ioend_in {
+	uint32_t flags;		/* FUSE_IOMAP_IOEND_* */
+	int32_t error;		/* negative errno or 0 */
+	uint64_t attr_ino;	/* matches fuse_attr:ino */
+	uint64_t pos;		/* file position, in bytes */
+	uint64_t new_addr;	/* disk offset of new mapping, in bytes */
+	uint64_t written;	/* bytes processed */
+	uint32_t dev;		/* device cookie */
+	uint32_t pad;		/* zero */
+};
+
+struct fuse_iomap_ioend_out {
+	uint64_t newsize;	/* new ondisk size */
+};
+
 #endif /* _LINUX_FUSE_H */
include/fuse_lowlevel.h +34 -0
index 7f9a56b0a7eda1..a8e845ba796937 100644
@@ -1394,6 +1394,28 @@ struct fuse_lowlevel_ops {
 	void (*iomap_end) (fuse_req_t req, fuse_ino_t nodeid, uint64_t attr_ino,
 			   off_t pos, uint64_t count, uint32_t opflags,
 			   ssize_t written, const struct fuse_file_iomap *iomap);
+
+	/**
+	 * Complete an iomap IO operation
+	 *
+	 * Valid replies:
+	 *   fuse_reply_ioend
+	 *   fuse_reply_err
+	 *
+	 * @param req request handle
+	 * @param nodeid the inode number
+	 * @param attr_ino inode number as told by fuse_attr::ino
+	 * @param pos position in file, in bytes
+	 * @param written number of bytes processed, or a negative errno
+	 * @param flags mask of FUSE_IOMAP_IOEND_ flags specifying operation
+	 * @param error errno code of what went wrong
+	 * @param dev device cookie of new address
+	 * @param new_addr disk address of new mapping, in bytes
+	 */
+	void (*iomap_ioend) (fuse_req_t req, fuse_ino_t nodeid,
+			     uint64_t attr_ino, off_t pos, uint64_t written,
+			     uint32_t ioendflags, int error, uint32_t dev,
+			     uint64_t new_addr);
 };
 
 /**
@@ -1810,6 +1832,18 @@ void fuse_iomap_pure_overwrite(struct fuse_file_iomap *write,
 int fuse_reply_iomap_begin(fuse_req_t req, const struct fuse_file_iomap *read,
 			   const struct fuse_file_iomap *write);
 
+/**
+ * Reply to an ioend with the new ondisk size
+ *
+ * Possible requests:
+ *   iomap_ioend
+ *
+ * @param req request handle
+ * @param newsize new ondisk file size
+ * @return zero for success, -errno for failure to send reply
+ */
+int fuse_reply_iomap_ioend(fuse_req_t req, off_t newsize);
+
 /* ----------------------------------------------------------- *
  * Notification						       *
  * ----------------------------------------------------------- */
lib/fuse_lowlevel.c +32 -0
index cf97ed8b471c49..45d5965caf7b7f 100644
@@ -2767,6 +2767,36 @@ static void do_iomap_end(fuse_req_t req, const fuse_ino_t nodeid,
 	_do_iomap_end(req, nodeid, inarg, NULL);
 }
 
+int fuse_reply_iomap_ioend(fuse_req_t req, off_t newsize)
+{
+	struct fuse_iomap_ioend_out arg = {
+		.newsize = newsize,
+	};
+
+	return send_reply_ok(req, &arg, sizeof(arg));
+}
+
+static void _do_iomap_ioend(fuse_req_t req, const fuse_ino_t nodeid,
+			    const void *op_in, const void *in_payload)
+{
+	const struct fuse_iomap_ioend_in *arg = op_in;
+	(void)in_payload;
+	(void)nodeid;
+
+	if (req->se->op.iomap_ioend)
+		req->se->op.iomap_ioend(req, nodeid, arg->attr_ino, arg->pos,
+					arg->written, arg->flags, arg->error,
+					arg->dev, arg->new_addr);
+	else
+		fuse_reply_err(req, ENOSYS);
+}
+
+static void do_iomap_ioend(fuse_req_t req, const fuse_ino_t nodeid,
+			   const void *inarg)
+{
+	_do_iomap_ioend(req, nodeid, inarg, NULL);
+}
+
 static bool want_flags_valid(uint64_t capable, uint64_t want)
 {
 	uint64_t unknown_flags = want & (~capable);
@@ -3755,6 +3785,7 @@ static struct {
 	[FUSE_STATX]	   = { do_statx,       "STATX"	     },
 	[FUSE_IOMAP_BEGIN] = { do_iomap_begin,	"IOMAP_BEGIN" },
 	[FUSE_IOMAP_END]   = { do_iomap_end,	"IOMAP_END" },
+	[FUSE_IOMAP_IOEND] = { do_iomap_ioend,	"IOMAP_IOEND" },
 	[CUSE_INIT]	   = { cuse_lowlevel_init, "CUSE_INIT"   },
 };
 
@@ -3814,6 +3845,7 @@ static struct {
 	[FUSE_STATX]		= { _do_statx,		"STATX" },
 	[FUSE_IOMAP_BEGIN]	= { _do_iomap_begin,	"IOMAP_BEGIN" },
 	[FUSE_IOMAP_END]	= { _do_iomap_end,	"IOMAP_END" },
+	[FUSE_IOMAP_IOEND]	= { _do_iomap_ioend,	"IOMAP_IOEND" },
 	[CUSE_INIT]		= { _cuse_lowlevel_init, "CUSE_INIT" },
 };
 
lib/fuse_versionscript +1 -0
index 5c4a7dc53b33f3..a018600d26ba4d 100644
@@ -233,6 +233,7 @@ FUSE_3.99 {
 	global:
 		fuse_iomap_pure_overwrite;
 		fuse_reply_iomap_begin;
+		fuse_reply_iomap_ioend;
 		fuse_lowlevel_iomap_device_add;
 		fuse_lowlevel_iomap_device_remove;
 		fuse_fs_iomap_device_add;
#116 lore raw

PATCH 9/25 libfuse: add upper level iomap ioend commands

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Teach the upper level fuse library about iomap ioend events, which happen when a write that isn't a pure overwrite completes.

---
 include/fuse.h |    9 +++++++++
 lib/fuse.c     |   53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 62 insertions(+)
include/fuse.h +9 -0
index 23b6d3f7c93303..dc4b79e98f6cd0 100644
@@ -902,6 +902,15 @@ struct fuse_operations {
 			  off_t pos_in, uint64_t length_in,
 			  uint32_t opflags_in, ssize_t written_in,
 			  const struct fuse_file_iomap *iomap);
+
+	/**
+	 * Respond to the outcome of a file IO operation.
+	 */
+	int (*iomap_ioend) (const char *path, uint64_t nodeid,
+			    uint64_t attr_ino, off_t pos_in,
+			    uint64_t written_in, uint32_t ioendflags_in,
+			    int error_in, uint32_t dev_in,
+			    uint64_t new_addr_in, off_t *newsize);
 };
 
 /** Extra context that may be needed by some filesystems
lib/fuse.c +53 -0

71 lines not on this page · raw message

#117 lore raw

PATCH 10/25 libfuse: add a reply function to send FUSE_ATTR_* to the kernel

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Create new fuse_reply_{attr,create,entry}_iflags functions so that we can send FUSE_ATTR_* flags to the kernel when instantiating an inode. Servers are expected to send FUSE_IFLAG_* values, which will be translated into what the kernel can understand.

---
 include/fuse_common.h   |    3 ++
 include/fuse_lowlevel.h |   83 +++++++++++++++++++++++++++++++++++++++++++++++
 lib/fuse_lowlevel.c     |   64 ++++++++++++++++++++++++++++--------
 lib/fuse_versionscript  |    4 ++
 4 files changed, 139 insertions(+), 15 deletions(-)
include/fuse_common.h +3 -0
index 013e74a0e9eefe..f34f4be6a61770 100644
@@ -1221,6 +1221,9 @@ static inline bool fuse_iomap_need_write_allocate(unsigned int opflags,
 /* is pagecache writeback */
 #define FUSE_IOMAP_IOEND_WRITEBACK	(1U << 5)
 
+/* enable fsdax */
+#define FUSE_IFLAG_DAX			(1U << 0)
+
 /* ----------------------------------------------------------- *
  * Compatibility stuff					       *
  * ----------------------------------------------------------- */
include/fuse_lowlevel.h +83 -0

161 lines not on this page · raw message

lib/fuse_lowlevel.c +49 -15

141 lines not on this page · raw message

lib/fuse_versionscript +4 -0
index a018600d26ba4d..fa1943e18dcafa 100644
@@ -238,6 +238,10 @@ FUSE_3.99 {
 		fuse_lowlevel_iomap_device_remove;
 		fuse_fs_iomap_device_add;
 		fuse_fs_iomap_device_remove;
+		fuse_reply_attr_iflags;
+		fuse_reply_create_iflags;
+		fuse_reply_entry_iflags;
+		fuse_add_direntry_plus_iflags;
 } FUSE_3.19;
 
 # Local Variables:
#118 lore raw

PATCH 11/25 libfuse: connect high level fuse library to fuse_reply_attr_iflags

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Create a new ->getattr_iflags function so that iomap filesystems can set the appropriate in-kernel inode flags on instantiation.

---
 include/fuse.h |    7 ++
 lib/fuse.c     |  191 ++++++++++++++++++++++++++++++++++++++++++--------------
 2 files changed, 151 insertions(+), 47 deletions(-)
include/fuse.h +7 -0
index dc4b79e98f6cd0..0db5f7e961d8fa 100644
@@ -911,6 +911,13 @@ struct fuse_operations {
 			    uint64_t written_in, uint32_t ioendflags_in,
 			    int error_in, uint32_t dev_in,
 			    uint64_t new_addr_in, off_t *newsize);
+
+	/**
+	 * Get file attributes and FUSE_IFLAG_* flags.  Otherwise the same as
+	 * getattr.
+	 */
+	int (*getattr_iflags) (const char *path, struct stat *buf,
+			       unsigned int *iflags, struct fuse_file_info *fi);
 };
 
 /** Extra context that may be needed by some filesystems
lib/fuse.c +144 -47

447 lines not on this page · raw message

#119 lore raw

PATCH 12/25 libfuse: support enabling exclusive mode for files

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Make it so that lowlevel fuse servers can ask for exclusive mode.

---
 include/fuse_common.h |    2 ++
 include/fuse_kernel.h |    4 ++++
 lib/fuse_lowlevel.c   |    2 ++
 3 files changed, 8 insertions(+)
include/fuse_common.h +2 -0
index f34f4be6a61770..a21f1c8dd12e91 100644
@@ -1223,6 +1223,8 @@ static inline bool fuse_iomap_need_write_allocate(unsigned int opflags,
 
 /* enable fsdax */
 #define FUSE_IFLAG_DAX			(1U << 0)
+/* exclusive attr mode */
+#define FUSE_IFLAG_EXCLUSIVE		(1U << 1)
 
 /* ----------------------------------------------------------- *
  * Compatibility stuff					       *
include/fuse_kernel.h +4 -0
index 732085a1b900b0..9b0894899ca453 100644
@@ -244,6 +244,7 @@
  *  7.99
  *  - XXX magic minor revision to make experimental code really obvious
  *  - add FUSE_IOMAP and iomap_{begin,end,ioend} for regular file operations
+ *  - add FUSE_ATTR_EXCLUSIVE to enable exclusive mode for specific inodes
  */
 
 #ifndef _LINUX_FUSE_H
@@ -584,9 +585,12 @@ struct fuse_file_lock {
  *
  * FUSE_ATTR_SUBMOUNT: Object is a submount root
  * FUSE_ATTR_DAX: Enable DAX for this file in per inode DAX mode
+ * FUSE_ATTR_EXCLUSIVE: This file can only be modified by this mount, so the
+ * kernel can use cached attributes more aggressively (e.g. ACL inheritance)
  */
 #define FUSE_ATTR_SUBMOUNT      (1 << 0)
 #define FUSE_ATTR_DAX		(1 << 1)
+#define FUSE_ATTR_EXCLUSIVE	(1 << 2)
 
 /**
  * Open flags
lib/fuse_lowlevel.c +2 -0
index c21e64787215cc..f34c86406552f9 100644
@@ -164,6 +164,8 @@ static void convert_stat(const struct stat *stbuf, struct fuse_attr *attr,
 	attr->flags	= 0;
 	if (iflags & FUSE_IFLAG_DAX)
 		attr->flags |= FUSE_ATTR_DAX;
+	if (iflags & FUSE_IFLAG_EXCLUSIVE)
+		attr->flags |= FUSE_ATTR_EXCLUSIVE;
 }
 
 static void convert_attr(const struct fuse_setattr_in *attr, struct stat *stbuf)
#120 lore raw

PATCH 13/25 libfuse: support direct I/O through iomap

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Make it so that fuse servers can ask the kernel fuse driver to use iomap to support direct IO.

---
 include/fuse_common.h |    2 ++
 include/fuse_kernel.h |    3 +++
 lib/fuse_lowlevel.c   |    2 ++
 3 files changed, 7 insertions(+)
include/fuse_common.h +2 -0
index a21f1c8dd12e91..bece561ef3ec9c 100644
@@ -1225,6 +1225,8 @@ static inline bool fuse_iomap_need_write_allocate(unsigned int opflags,
 #define FUSE_IFLAG_DAX			(1U << 0)
 /* exclusive attr mode */
 #define FUSE_IFLAG_EXCLUSIVE		(1U << 1)
+/* use iomap for this inode */
+#define FUSE_IFLAG_IOMAP		(1U << 2)
 
 /* ----------------------------------------------------------- *
  * Compatibility stuff					       *
include/fuse_kernel.h +3 -0
index 9b0894899ca453..6b1fcc44004dbf 100644
@@ -245,6 +245,7 @@
  *  - XXX magic minor revision to make experimental code really obvious
  *  - add FUSE_IOMAP and iomap_{begin,end,ioend} for regular file operations
  *  - add FUSE_ATTR_EXCLUSIVE to enable exclusive mode for specific inodes
+ *  - add FUSE_ATTR_IOMAP to enable iomap for specific inodes
  */
 
 #ifndef _LINUX_FUSE_H
@@ -587,10 +588,12 @@ struct fuse_file_lock {
  * FUSE_ATTR_DAX: Enable DAX for this file in per inode DAX mode
  * FUSE_ATTR_EXCLUSIVE: This file can only be modified by this mount, so the
  * kernel can use cached attributes more aggressively (e.g. ACL inheritance)
+ * FUSE_ATTR_IOMAP: Use iomap for this inode
  */
 #define FUSE_ATTR_SUBMOUNT      (1 << 0)
 #define FUSE_ATTR_DAX		(1 << 1)
 #define FUSE_ATTR_EXCLUSIVE	(1 << 2)
+#define FUSE_ATTR_IOMAP		(1 << 3)
 
 /**
  * Open flags
lib/fuse_lowlevel.c +2 -0
index f34c86406552f9..23169c1946ce0b 100644
@@ -166,6 +166,8 @@ static void convert_stat(const struct stat *stbuf, struct fuse_attr *attr,
 		attr->flags |= FUSE_ATTR_DAX;
 	if (iflags & FUSE_IFLAG_EXCLUSIVE)
 		attr->flags |= FUSE_ATTR_EXCLUSIVE;
+	if (iflags & FUSE_IFLAG_IOMAP)
+		attr->flags |= FUSE_ATTR_IOMAP;
 }
 
 static void convert_attr(const struct fuse_setattr_in *attr, struct stat *stbuf)
#121 lore raw

PATCH 14/25 libfuse: don't allow hardlinking of iomap files in the upper level fuse library

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

The upper level fuse library creates a separate node object for every (i)node referenced by a directory entry. Unfortunately, it doesn't account for the possibility of hardlinks, which means that we can create multiple nodeids that refer to the same hardlinked inode. Inode locking in iomap mode in the kernel relies there only being one inode object for a hardlinked file, so we cannot allow anyone to hardlink an iomap file. The client had better not turn on iomap for an existing hardlinked file.

---
 include/fuse.h         |   18 ++++++++++
 lib/fuse.c             |   90 +++++++++++++++++++++++++++++++++++++++++++-----
 lib/fuse_versionscript |    2 +
 3 files changed, 101 insertions(+), 9 deletions(-)
include/fuse.h +18 -0
index 0db5f7e961d8fa..817b3cf6c419c4 100644
@@ -1438,6 +1438,24 @@ int fuse_fs_iomap_device_add(int fd, unsigned int flags);
  */
 int fuse_fs_iomap_device_remove(int device_id);
 
+/**
+ * Decide if we can enable iomap mode for a particular file for an upper-level
+ * fuse server.
+ *
+ * @param statbuf stat information for the file.
+ * @return true if it can be enabled, false if not.
+ */
+bool fuse_fs_can_enable_iomap(const struct stat *statbuf);
+
+/**
+ * Decide if we can enable iomap mode for a particular file for an upper-level
+ * fuse server.
+ *
+ * @param statxbuf statx information for the file.
+ * @return true if it can be enabled, false if not.
+ */
+bool fuse_fs_can_enable_iomapx(const struct statx *statxbuf);
+
 int fuse_notify_poll(struct fuse_pollhandle *ph);
 
 /**
lib/fuse.c +81 -9

108 lines not on this page · raw message

lib/fuse_versionscript +2 -0
index fa1943e18dcafa..67c3bd614c44fc 100644
@@ -242,6 +242,8 @@ FUSE_3.99 {
 		fuse_reply_create_iflags;
 		fuse_reply_entry_iflags;
 		fuse_add_direntry_plus_iflags;
+		fuse_fs_can_enable_iomap;
+		fuse_fs_can_enable_iomapx;
 } FUSE_3.19;
 
 # Local Variables:
#122 lore raw

PATCH 15/25 libfuse: allow discovery of the kernel's iomap capabilities

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Create a library function so that we can discover the kernel's iomap capabilities ahead of time.

---
 include/fuse_common.h   |    7 +++++++
 include/fuse_kernel.h   |    7 +++++++
 include/fuse_lowlevel.h |    9 +++++++++
 lib/fuse_lowlevel.c     |   19 +++++++++++++++++++
 lib/fuse_versionscript  |    1 +
 5 files changed, 43 insertions(+)
include/fuse_common.h +7 -0
index bece561ef3ec9c..cce263aa62f5d0 100644
@@ -534,6 +534,13 @@ struct fuse_loop_config_v1 {
 
 #define FUSE_IOCTL_MAX_IOV	256
 
+/**
+ * iomap discovery flags
+ *
+ * FUSE_IOMAP_SUPPORT_FILEIO: basic file I/O functionality through iomap
+ */
+#define FUSE_IOMAP_SUPPORT_FILEIO	(1ULL << 0)
+
 /**
  * Connection information, passed to the ->init() method
  *
include/fuse_kernel.h +7 -0
index 6b1fcc44004dbf..95c6c179a4398a 100644
@@ -1156,12 +1156,19 @@ struct fuse_backing_map {
 	uint64_t	padding;
 };
 
+struct fuse_iomap_support {
+	uint64_t	flags;
+	uint64_t	padding;
+};
+
 /* Device ioctls: */
 #define FUSE_DEV_IOC_MAGIC		229
 #define FUSE_DEV_IOC_CLONE		_IOR(FUSE_DEV_IOC_MAGIC, 0, uint32_t)
 #define FUSE_DEV_IOC_BACKING_OPEN	_IOW(FUSE_DEV_IOC_MAGIC, 1, \
 					     struct fuse_backing_map)
 #define FUSE_DEV_IOC_BACKING_CLOSE	_IOW(FUSE_DEV_IOC_MAGIC, 2, uint32_t)
+#define FUSE_DEV_IOC_IOMAP_SUPPORT	_IOR(FUSE_DEV_IOC_MAGIC, 99, \
+					     struct fuse_iomap_support)
 
 struct fuse_lseek_in {
 	uint64_t	fh;
include/fuse_lowlevel.h +9 -0
index c113c85067fb82..f31f250f13a965 100644
@@ -2658,6 +2658,15 @@ bool fuse_req_is_uring(fuse_req_t req);
 int fuse_req_get_payload(fuse_req_t req, char **payload, size_t *payload_sz,
 			 void **mr);
 
+/**
+ * Discover the kernel's iomap capabilities.  Returns FUSE_CAP_IOMAP_* flags.
+ *
+ * @param fd open file descriptor to a fuse device, or -1 if you're running
+ *           in the same process that will call mount().
+ * @return FUSE_IOMAP_SUPPORT_* flags
+ */
+uint64_t fuse_lowlevel_discover_iomap(int fd);
+
 #ifdef __cplusplus
 }
 #endif
lib/fuse_lowlevel.c +19 -0
index 23169c1946ce0b..4f3585f9a6841d 100644
@@ -5073,3 +5073,22 @@ void fuse_session_stop_teardown_watchdog(void *data)
 	pthread_join(tt->thread_id, NULL);
 	fuse_tt_destruct(tt);
 }
+
+uint64_t fuse_lowlevel_discover_iomap(int fd)
+{
+	struct fuse_iomap_support ios = { };
+
+	if (fd >= 0) {
+		ioctl(fd, FUSE_DEV_IOC_IOMAP_SUPPORT, &ios);
+		return ios.flags;
+	}
+
+	fd = open("/dev/fuse", O_RDONLY | O_CLOEXEC);
+	if (fd < 0)
+		return 0;
+
+	ioctl(fd, FUSE_DEV_IOC_IOMAP_SUPPORT, &ios);
+	close(fd);
+
+	return ios.flags;
+}
lib/fuse_versionscript +1 -0
index 67c3bd614c44fc..f16698f3c4dbfa 100644
@@ -244,6 +244,7 @@ FUSE_3.99 {
 		fuse_add_direntry_plus_iflags;
 		fuse_fs_can_enable_iomap;
 		fuse_fs_can_enable_iomapx;
+		fuse_lowlevel_discover_iomap;
 } FUSE_3.19;
 
 # Local Variables:
#123 lore raw

PATCH 16/25 libfuse: add lower level iomap_config implementation

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Add FUSE_IOMAP_CONFIG helpers to the low level fuse library.

---
 include/fuse_common.h   |   31 ++++++++++++++++++
 include/fuse_kernel.h   |   31 ++++++++++++++++++
 include/fuse_lowlevel.h |   27 +++++++++++++++
 lib/fuse_lowlevel.c     |   82 +++++++++++++++++++++++++++++++++++++++++++++++
 lib/fuse_versionscript  |    1 +
 5 files changed, 172 insertions(+)
include/fuse_common.h +31 -0
index cce263aa62f5d0..2f6672fc14d90d 100644
@@ -1235,6 +1235,37 @@ static inline bool fuse_iomap_need_write_allocate(unsigned int opflags,
 /* use iomap for this inode */
 #define FUSE_IFLAG_IOMAP		(1U << 2)
 
+/* Which fields are set in fuse_iomap_config_out? */
+#define FUSE_IOMAP_CONFIG_SID		(1 << 0ULL)
+#define FUSE_IOMAP_CONFIG_UUID		(1 << 1ULL)
+#define FUSE_IOMAP_CONFIG_BLOCKSIZE	(1 << 2ULL)
+#define FUSE_IOMAP_CONFIG_MAX_LINKS	(1 << 3ULL)
+#define FUSE_IOMAP_CONFIG_TIME		(1 << 4ULL)
+#define FUSE_IOMAP_CONFIG_MAXBYTES	(1 << 5ULL)
+
+struct fuse_iomap_config{
+	uint64_t flags;		/* FUSE_IOMAP_CONFIG_* */
+
+	char s_id[32];		/* Informational name */
+	char s_uuid[16];	/* UUID */
+
+	uint8_t s_uuid_len;	/* length of s_uuid */
+
+	uint8_t s_pad[3];	/* must be zeroes */
+
+	uint32_t s_blocksize;	/* fs block size */
+	uint32_t s_max_links;	/* max hard links */
+
+	/* Granularity of c/m/atime in ns (cannot be worse than a second) */
+	uint32_t s_time_gran;
+
+	/* Time limits for c/m/atime in seconds */
+	int64_t s_time_min;
+	int64_t s_time_max;
+
+	int64_t s_maxbytes;	/* max file size */
+};
+
 /* ----------------------------------------------------------- *
  * Compatibility stuff					       *
  * ----------------------------------------------------------- */
include/fuse_kernel.h +31 -0
index 95c6c179a4398a..897d996a0ce60d 100644
@@ -246,6 +246,7 @@
  *  - add FUSE_IOMAP and iomap_{begin,end,ioend} for regular file operations
  *  - add FUSE_ATTR_EXCLUSIVE to enable exclusive mode for specific inodes
  *  - add FUSE_ATTR_IOMAP to enable iomap for specific inodes
+ *  - add FUSE_IOMAP_CONFIG so the fuse server can configure more fs geometry
  */
 
 #ifndef _LINUX_FUSE_H
@@ -677,6 +678,7 @@ enum fuse_opcode {
 	FUSE_STATX		= 52,
 	FUSE_COPY_FILE_RANGE_64	= 53,
 
+	FUSE_IOMAP_CONFIG	= 4092,
 	FUSE_IOMAP_IOEND	= 4093,
 	FUSE_IOMAP_BEGIN	= 4094,
 	FUSE_IOMAP_END		= 4095,
@@ -1390,4 +1392,33 @@ struct fuse_iomap_ioend_out {
 	uint64_t newsize;	/* new ondisk size */
 };
 
+struct fuse_iomap_config_in {
+	uint64_t flags;		/* supported FUSE_IOMAP_CONFIG_* flags */
+	int64_t maxbytes;	/* max supported file size */
+	uint64_t padding[6];	/* zero */
+};
+
+struct fuse_iomap_config_out {
+	uint64_t flags;		/* FUSE_IOMAP_CONFIG_* */
+
+	char s_id[32];		/* Informational name */
+	char s_uuid[16];	/* UUID */
+
+	uint8_t s_uuid_len;	/* length of s_uuid */
+
+	uint8_t s_pad[3];	/* must be zeroes */
+
+	uint32_t s_blocksize;	/* fs block size */
+	uint32_t s_max_links;	/* max hard links */
+
+	/* Granularity of c/m/atime in ns (cannot be worse than a second) */
+	uint32_t s_time_gran;
+
+	/* Time limits for c/m/atime in seconds */
+	int64_t s_time_min;
+	int64_t s_time_max;
+
+	int64_t s_maxbytes;	/* max file size */
+};
+
 #endif /* _LINUX_FUSE_H */
include/fuse_lowlevel.h +27 -0
index f31f250f13a965..a00b6d5a7fa0b6 100644
@@ -1425,6 +1425,20 @@ struct fuse_lowlevel_ops {
 			     uint64_t attr_ino, off_t pos, uint64_t written,
 			     uint32_t ioendflags, int error, uint32_t dev,
 			     uint64_t new_addr);
+
+	/**
+	 * Configure the filesystem geometry for iomap mode
+	 *
+	 * Valid replies:
+	 *   fuse_reply_iomap_config
+	 *   fuse_reply_err
+	 *
+	 * @param req request handle
+	 * @param flags FUSE_IOMAP_CONFIG_* flags that can be passed back
+	 * @param maxbytes maximum supported file size
+	 */
+	void (*iomap_config) (fuse_req_t req, uint64_t flags,
+			      uint64_t maxbytes);
 };
 
 /**
@@ -1927,6 +1941,19 @@ int fuse_reply_iomap_begin(fuse_req_t req, const struct fuse_file_iomap *read,
  */
 int fuse_reply_iomap_ioend(fuse_req_t req, off_t newsize);
 
+/**
+ * Reply with iomap configuration
+ *
+ * Possible requests:
+ *   iomap_config
+ *
+ * @param req request handle
+ * @param cfg iomap configuration
+ * @return zero for success, -errno for failure to send reply
+ */
+int fuse_reply_iomap_config(fuse_req_t req,
+			    const struct fuse_iomap_config *cfg);
+
 /* ----------------------------------------------------------- *
  * Notification						       *
  * ----------------------------------------------------------- */
lib/fuse_lowlevel.c +82 -0

100 lines not on this page · raw message

lib/fuse_versionscript +1 -0
index f16698f3c4dbfa..14c59928ca9f00 100644
@@ -245,6 +245,7 @@ FUSE_3.99 {
 		fuse_fs_can_enable_iomap;
 		fuse_fs_can_enable_iomapx;
 		fuse_lowlevel_discover_iomap;
+		fuse_reply_iomap_config;
 } FUSE_3.19;
 
 # Local Variables:
#124 lore raw

PATCH 17/25 libfuse: add upper level iomap_config implementation

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Add FUSE_IOMAP_CONFIG helpers to the upper level fuse library.

---
 include/fuse.h |    7 +++++++
 lib/fuse.c     |   37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
include/fuse.h +7 -0
index 817b3cf6c419c4..9f0ac44295e46f 100644
@@ -918,6 +918,13 @@ struct fuse_operations {
 	 */
 	int (*getattr_iflags) (const char *path, struct stat *buf,
 			       unsigned int *iflags, struct fuse_file_info *fi);
+
+	/**
+	 * Configure the filesystem geometry that will be used by iomap
+	 * files.
+	 */
+	int (*iomap_config) (uint64_t supported_flags, off_t maxbytes,
+			     struct fuse_iomap_config *cfg);
 };
 
 /** Extra context that may be needed by some filesystems
lib/fuse.c +37 -0
index 5d9acbc177a177..5301e34901bb3e 100644
@@ -2954,6 +2954,23 @@ static int fuse_fs_iomap_ioend(struct fuse_fs *fs, const char *path,
 				  ioendflags, error, dev, new_addr, newsize);
 }
 
+static int fuse_fs_iomap_config(struct fuse_fs *fs, uint64_t flags,
+				uint64_t maxbytes,
+				struct fuse_iomap_config *cfg)
+{
+	fuse_get_context()->private_data = fs->user_data;
+	if (!fs->op.iomap_config)
+		return -ENOSYS;
+
+	if (fs->debug) {
+		fuse_log(FUSE_LOG_DEBUG,
+			 "iomap_config flags 0x%llx maxbytes %lld\n",
+			 (unsigned long long)flags, (long long)maxbytes);
+	}
+
+	return fs->op.iomap_config(flags, maxbytes, cfg);
+}
+
 static void fuse_lib_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,
 			     int valid, struct fuse_file_info *fi)
 {
@@ -4834,6 +4851,25 @@ static void fuse_lib_iomap_ioend(fuse_req_t req, fuse_ino_t nodeid,
 	fuse_reply_iomap_ioend(req, newsize);
 }
 
+static void fuse_lib_iomap_config(fuse_req_t req, uint64_t flags,
+				  uint64_t maxbytes)
+{
+	struct fuse_iomap_config cfg = { };
+	struct fuse *f = req_fuse_prepare(req);
+	struct fuse_intr_data d;
+	int err;
+
+	fuse_prepare_interrupt(f, req, &d);
+	err = fuse_fs_iomap_config(f->fs, flags, maxbytes, &cfg);
+	fuse_finish_interrupt(f, req, &d);
+	if (err) {
+		reply_err(req, err);
+		return;
+	}
+
+	fuse_reply_iomap_config(req, &cfg);
+}
+
 static int clean_delay(struct fuse *f)
 {
 	/*
@@ -4939,6 +4975,7 @@ static struct fuse_lowlevel_ops fuse_path_ops = {
 	.iomap_begin = fuse_lib_iomap_begin,
 	.iomap_end = fuse_lib_iomap_end,
 	.iomap_ioend = fuse_lib_iomap_ioend,
+	.iomap_config = fuse_lib_iomap_config,
 };
 
 int fuse_notify_poll(struct fuse_pollhandle *ph)
#125 lore raw

PATCH 18/25 libfuse: add low level code to invalidate iomap block device ranges

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Make it easier to invalidate the page cache for a block device that is being used in conjunction with iomap. This allows a fuse server to kill all cached data for a block that is being freed, so that block reuse doesn't result in file corruption.

---
 include/fuse_kernel.h   |   15 +++++++++++++++
 include/fuse_lowlevel.h |   15 +++++++++++++++
 lib/fuse_lowlevel.c     |   22 ++++++++++++++++++++++
 lib/fuse_versionscript  |    1 +
 4 files changed, 53 insertions(+)
include/fuse_kernel.h +15 -0
index 897d996a0ce60d..1e7c9d8082cf23 100644
@@ -247,6 +247,7 @@
  *  - add FUSE_ATTR_EXCLUSIVE to enable exclusive mode for specific inodes
  *  - add FUSE_ATTR_IOMAP to enable iomap for specific inodes
  *  - add FUSE_IOMAP_CONFIG so the fuse server can configure more fs geometry
+ *  - add FUSE_NOTIFY_IOMAP_DEV_INVAL to invalidate iomap bdev ranges
  */
 
 #ifndef _LINUX_FUSE_H
@@ -701,6 +702,7 @@ enum fuse_notify_code {
 	FUSE_NOTIFY_RESEND = 7,
 	FUSE_NOTIFY_INC_EPOCH = 8,
 	FUSE_NOTIFY_PRUNE = 9,
+	FUSE_NOTIFY_IOMAP_DEV_INVAL = 99,
 };
 
 /* The read buffer is required to be at least 8k, but may be much larger */
@@ -1421,4 +1423,17 @@ struct fuse_iomap_config_out {
 	int64_t s_maxbytes;	/* max file size */
 };
 
+struct fuse_range {
+	uint64_t offset;
+	uint64_t length;
+};
+
+struct fuse_iomap_dev_inval_out {
+	uint32_t dev;		/* device cookie */
+	uint32_t reserved;	/* zero */
+
+	/* range of bdev pagecache to invalidate, in bytes */
+	struct fuse_range range;
+};
+
 #endif /* _LINUX_FUSE_H */
include/fuse_lowlevel.h +15 -0
index a00b6d5a7fa0b6..95429ac096a82c 100644
@@ -2208,6 +2208,21 @@ int fuse_lowlevel_iomap_device_add(struct fuse_session *se, int fd,
  */
 int fuse_lowlevel_iomap_device_remove(struct fuse_session *se, int device_id);
 
+/*
+ * Invalidate the page cache of a block device opened for use with iomap.
+ *
+ * Added in FUSE protocol version 7.99. If the kernel does not support
+ * this (or a newer) version, the function will return -ENOSYS and do
+ * nothing.
+ *
+ * @param se the session object
+ * @param dev device cookie returned by fuse_lowlevel_iomap_add_device
+ * @param offset start of the range to invalidate, in bytes
+ * @return length length of the range to invalidate, in bytes
+ */
+int fuse_lowlevel_iomap_device_invalidate(struct fuse_session *se, int dev,
+					  off_t offset, off_t length);
+
 /* ----------------------------------------------------------- *
  * Utility functions					       *
  * ----------------------------------------------------------- */
lib/fuse_lowlevel.c +22 -0
index 19f292c0cb8ee3..89e26f207addbf 100644
@@ -3684,6 +3684,28 @@ int fuse_lowlevel_notify_store(struct fuse_session *se, fuse_ino_t ino,
 	return res;
 }
 
+int fuse_lowlevel_iomap_device_invalidate(struct fuse_session *se, int dev,
+					  off_t offset, off_t length)
+{
+	struct fuse_iomap_dev_inval_out arg = {
+		.dev = dev,
+		.range.offset = offset,
+		.range.length = length,
+	};
+	struct iovec iov[2];
+
+	if (!se)
+		return -EINVAL;
+
+	if (!(se->conn.want_ext & FUSE_CAP_IOMAP))
+		return -ENOSYS;
+
+	iov[1].iov_base = &arg;
+	iov[1].iov_len = sizeof(arg);
+
+	return send_notify_iov(se, FUSE_NOTIFY_IOMAP_DEV_INVAL, iov, 2);
+}
+
 struct fuse_retrieve_req {
 	struct fuse_notify_req nreq;
 	void *cookie;
lib/fuse_versionscript +1 -0
index 14c59928ca9f00..56edd16862ca30 100644
@@ -246,6 +246,7 @@ FUSE_3.99 {
 		fuse_fs_can_enable_iomapx;
 		fuse_lowlevel_discover_iomap;
 		fuse_reply_iomap_config;
+		fuse_lowlevel_iomap_device_invalidate;
 } FUSE_3.19;
 
 # Local Variables:
#126 lore raw

PATCH 19/25 libfuse: add upper-level API to invalidate parts of an iomap block device

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Wire up the upper-level wrappers to
fuse_lowlevel_iomap_invalidate_device.

---
 include/fuse.h         |   10 ++++++++++
 lib/fuse.c             |    9 +++++++++
 lib/fuse_versionscript |    1 +
 3 files changed, 20 insertions(+)
include/fuse.h +10 -0
index 9f0ac44295e46f..db4281e3f330c6 100644
@@ -1445,6 +1445,16 @@ int fuse_fs_iomap_device_add(int fd, unsigned int flags);
  */
 int fuse_fs_iomap_device_remove(int device_id);
 
+/**
+ * Invalidate any pagecache for the given iomap (block) device.
+ *
+ * @param device_id device index as returned by fuse_lowlevel_iomap_device_add
+ * @param offset starting offset of the range to invalidate
+ * @param length length of the range to invalidate
+ * @return 0 on success, or negative errno on failure
+ */
+int fuse_fs_iomap_device_invalidate(int device_id, off_t offset, off_t length);
+
 /**
  * Decide if we can enable iomap mode for a particular file for an upper-level
  * fuse server.
lib/fuse.c +9 -0
index 5301e34901bb3e..022888c475cb3d 100644
@@ -2932,6 +2932,15 @@ int fuse_fs_iomap_device_remove(int device_id)
 	return fuse_lowlevel_iomap_device_remove(se, device_id);
 }
 
+int fuse_fs_iomap_device_invalidate(int device_id, off_t offset, off_t length)
+{
+	struct fuse_context *ctxt = fuse_get_context();
+	struct fuse_session *se = fuse_get_session(ctxt->fuse);
+
+	return fuse_lowlevel_iomap_device_invalidate(se, device_id, offset,
+						     length);
+}
+
 static int fuse_fs_iomap_ioend(struct fuse_fs *fs, const char *path,
 			       uint64_t nodeid, uint64_t attr_ino, off_t pos,
 			       uint64_t written, uint32_t ioendflags, int error,
lib/fuse_versionscript +1 -0
index 56edd16862ca30..56cc5fade272e5 100644
@@ -247,6 +247,7 @@ FUSE_3.99 {
 		fuse_lowlevel_discover_iomap;
 		fuse_reply_iomap_config;
 		fuse_lowlevel_iomap_device_invalidate;
+		fuse_fs_iomap_device_invalidate;
 } FUSE_3.19;
 
 # Local Variables:
#127 lore raw

PATCH 20/25 libfuse: add atomic write support

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Add the single flag that we need to turn on atomic write support in fuse.

---
 include/fuse_common.h |    4 ++++
 include/fuse_kernel.h |    3 +++
 lib/fuse_lowlevel.c   |    2 ++
 3 files changed, 9 insertions(+)
include/fuse_common.h +4 -0
index 2f6672fc14d90d..a42d70f79d57e1 100644
@@ -540,6 +540,8 @@ struct fuse_loop_config_v1 {
  * FUSE_IOMAP_SUPPORT_FILEIO: basic file I/O functionality through iomap
  */
 #define FUSE_IOMAP_SUPPORT_FILEIO	(1ULL << 0)
+/* untorn writes through iomap */
+#define FUSE_IOMAP_SUPPORT_ATOMIC	(1ULL << 1)
 
 /**
  * Connection information, passed to the ->init() method
@@ -1234,6 +1236,8 @@ static inline bool fuse_iomap_need_write_allocate(unsigned int opflags,
 #define FUSE_IFLAG_EXCLUSIVE		(1U << 1)
 /* use iomap for this inode */
 #define FUSE_IFLAG_IOMAP		(1U << 2)
+/* enable untorn writes */
+#define FUSE_IFLAG_ATOMIC		(1U << 3)
 
 /* Which fields are set in fuse_iomap_config_out? */
 #define FUSE_IOMAP_CONFIG_SID		(1 << 0ULL)
include/fuse_kernel.h +3 -0
index 1e7c9d8082cf23..71a6a92b4b4a65 100644
@@ -248,6 +248,7 @@
  *  - add FUSE_ATTR_IOMAP to enable iomap for specific inodes
  *  - add FUSE_IOMAP_CONFIG so the fuse server can configure more fs geometry
  *  - add FUSE_NOTIFY_IOMAP_DEV_INVAL to invalidate iomap bdev ranges
+ *  - add FUSE_ATTR_ATOMIC for single-fsblock atomic write support
  */
 
 #ifndef _LINUX_FUSE_H
@@ -591,11 +592,13 @@ struct fuse_file_lock {
  * FUSE_ATTR_EXCLUSIVE: This file can only be modified by this mount, so the
  * kernel can use cached attributes more aggressively (e.g. ACL inheritance)
  * FUSE_ATTR_IOMAP: Use iomap for this inode
+ * FUSE_ATTR_ATOMIC: Enable untorn writes
  */
 #define FUSE_ATTR_SUBMOUNT      (1 << 0)
 #define FUSE_ATTR_DAX		(1 << 1)
 #define FUSE_ATTR_EXCLUSIVE	(1 << 2)
 #define FUSE_ATTR_IOMAP		(1 << 3)
+#define FUSE_ATTR_ATOMIC	(1 << 4)
 
 /**
  * Open flags
lib/fuse_lowlevel.c +2 -0
index 89e26f207addbf..91aaf6a1183d30 100644
@@ -168,6 +168,8 @@ static void convert_stat(const struct stat *stbuf, struct fuse_attr *attr,
 		attr->flags |= FUSE_ATTR_EXCLUSIVE;
 	if (iflags & FUSE_IFLAG_IOMAP)
 		attr->flags |= FUSE_ATTR_IOMAP;
+	if (iflags & FUSE_IFLAG_ATOMIC)
+		attr->flags |= FUSE_ATTR_ATOMIC;
 }
 
 static void convert_attr(const struct fuse_setattr_in *attr, struct stat *stbuf)
#128 lore raw

PATCH 21/25 libfuse: allow disabling of fs memory reclaim and write throttling

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Create a library function so that fuse-iomap servers can ask the kernel to disable direct memory reclaim for filesystems and BDI write throttling.

Disabling fs reclaim prevents livelocks where the fuse server can allocate memory, fault into the kernel, and then the allocation tries to initiate writeback by calling back into the same fuse server.

Disabling BDI write throttling means that writeback won't be throttled by metadata writes to the filesystem.

---
 include/fuse_kernel.h   |    1 +
 include/fuse_lowlevel.h |   13 +++++++++++++
 lib/fuse_lowlevel.c     |    5 +++++
 lib/fuse_versionscript  |    1 +
 4 files changed, 20 insertions(+)
include/fuse_kernel.h +1 -0
index 71a6a92b4b4a65..0779e3917a1e8f 100644
@@ -1176,6 +1176,7 @@ struct fuse_iomap_support {
 #define FUSE_DEV_IOC_BACKING_CLOSE	_IOW(FUSE_DEV_IOC_MAGIC, 2, uint32_t)
 #define FUSE_DEV_IOC_IOMAP_SUPPORT	_IOR(FUSE_DEV_IOC_MAGIC, 99, \
 					     struct fuse_iomap_support)
+#define FUSE_DEV_IOC_SET_NOFS		_IOW(FUSE_DEV_IOC_MAGIC, 100, uint32_t)
 
 struct fuse_lseek_in {
 	uint64_t	fh;
include/fuse_lowlevel.h +13 -0
index 95429ac096a82c..e2127c40940640 100644
@@ -2709,6 +2709,19 @@ int fuse_req_get_payload(fuse_req_t req, char **payload, size_t *payload_sz,
  */
 uint64_t fuse_lowlevel_discover_iomap(int fd);
 
+/**
+ * Disable direct fs memory reclaim and BDI throttling for a fuse-iomap server.
+ * This prevents memory allocations for the fuse server from initiating
+ * pagecache writeback to the fuse server and only throttles writes to the
+ * fuse server's block devices.  The fuse connection must already be
+ * initialized with iomap enabled.
+ *
+ * @param se the session object
+ * @param val 1 to disable fs reclaim and throttling, 0 to enable them
+ * @return 0 on success, or -1 on failure with errno set
+ */
+int fuse_lowlevel_disable_fsreclaim(struct fuse_session *se, int val);
+
 #ifdef __cplusplus
 }
 #endif
lib/fuse_lowlevel.c +5 -0
index 91aaf6a1183d30..a6b65ccf9fe1df 100644
@@ -5198,3 +5198,8 @@ uint64_t fuse_lowlevel_discover_iomap(int fd)
 
 	return ios.flags;
 }
+
+int fuse_lowlevel_disable_fsreclaim(struct fuse_session *se, int val)
+{
+	return ioctl(se->fd, FUSE_DEV_IOC_SET_NOFS, &val);
+}
lib/fuse_versionscript +1 -0
index 56cc5fade272e5..556562f1bb4588 100644
@@ -248,6 +248,7 @@ FUSE_3.99 {
 		fuse_reply_iomap_config;
 		fuse_lowlevel_iomap_device_invalidate;
 		fuse_fs_iomap_device_invalidate;
+		fuse_lowlevel_disable_fsreclaim;
 } FUSE_3.19;
 
 # Local Variables:
#129 lore raw

PATCH 22/25 libfuse: create a helper to transform an open regular file into an open loopdev

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Create a helper function to configure a loop device for an open regular file fd, and then return an open fd to the loop device. This will enable the use of fuse+iomap file servers with filesystem image files.

---
 include/fuse_loopdev.h |   27 +++
 include/meson.build    |    4 
 lib/fuse_loopdev.c     |  403 ++++++++++++++++++++++++++++++++++++++++++++++++
 lib/fuse_versionscript |    1 
 lib/meson.build        |    3 
 meson.build            |   11 +
 6 files changed, 448 insertions(+), 1 deletion(-)
 create mode 100644 include/fuse_loopdev.h
 create mode 100644 lib/fuse_loopdev.c
include/fuse_loopdev.h new file +27 -0
new file mode 100644
index 00000000000000..b0a897c9ce4eda
@@ -0,0 +1,27 @@
+/*  FUSE: Filesystem in Userspace
+  Copyright (C) 2025-2026 Oracle.
+  Author: Darrick J. Wong <[email protected]>
+
+  This program can be distributed under the terms of the GNU LGPLv2.
+  See the file LGPL2.txt.
+*/
+#ifndef FUSE_LOOPDEV_H_
+#define FUSE_LOOPDEV_H_
+
+/**
+ * If possible, set up a loop device for the given file fd.  Return the opened
+ * loop device fd and the path to the loop device.  The loop device will be
+ * removed when the last close() occurs.
+ *
+ * @param file_fd an open file
+ * @param open_flags O_* flags that were used to open file_fd
+ * @param path path to the open file
+ * @param timeout spend this much time waiting to lock the file
+ * @param loop_fd set to an open fd to the new loop device or -1 if inappropriate
+ * @param loop_dev (optional) set to a pointer to the path to the loop device
+ * @return 0 for success, or -1 on error
+ */
+int fuse_loopdev_setup(int file_fd, int open_flags, const char *path,
+		       unsigned int timeout, int *loop_fd, char **loop_dev);
+
+#endif /* FUSE_LOOPDEV_H_ */
include/meson.build +4 -0
index bf671977a5a6a9..0b1e3a9d4fcb43 100644
@@ -1,4 +1,8 @@
 libfuse_headers = [ 'fuse.h', 'fuse_common.h', 'fuse_lowlevel.h',
 	            'fuse_opt.h', 'cuse_lowlevel.h', 'fuse_log.h' ]
 
+if private_cfg.get('FUSE_LOOPDEV_ENABLED')
+  libfuse_headers += [ 'fuse_loopdev.h' ]
+endif
+
 install_headers(libfuse_headers, subdir: 'fuse3')
lib/fuse_loopdev.c new file +403 -0

403 lines not on this page · raw message

lib/fuse_versionscript +1 -0
index 556562f1bb4588..f459d06c2f3377 100644
@@ -249,6 +249,7 @@ FUSE_3.99 {
 		fuse_lowlevel_iomap_device_invalidate;
 		fuse_fs_iomap_device_invalidate;
 		fuse_lowlevel_disable_fsreclaim;
+		fuse_loopdev_setup;
 } FUSE_3.19;
 
 # Local Variables:
lib/meson.build +2 -1
index a3d3d49f9ba42b..477b0fc2f86d38 100644
@@ -2,7 +2,8 @@ libfuse_sources = ['fuse.c', 'fuse_i.h', 'fuse_loop.c', 'fuse_loop_mt.c',
                    'fuse_lowlevel.c', 'fuse_misc.h', 'fuse_opt.c',
                    'fuse_signals.c', 'buffer.c', 'cuse_lowlevel.c',
                    'helper.c', 'modules/subdir.c', 'mount_util.c',
-                   'fuse_log.c', 'compat.c', 'util.c', 'util.h' ]
+                   'fuse_log.c', 'compat.c', 'util.c', 'util.h',
+                   'fuse_loopdev.c' ]
 
 if host_machine.system().startswith('linux')
    libfuse_sources += [ 'mount.c' ]
meson.build +11 -0
index 8359a489c351b9..73aee98c775a2a 100644
@@ -153,7 +153,18 @@ private_cfg.set('HAVE_STRUCT_STAT_ST_ATIMESPEC',
     cc.has_member('struct stat', 'st_atimespec',
                   prefix: include_default + '#include <sys/stat.h>',
                   args: args_default))
+private_cfg.set('HAVE_STRUCT_LOOP_CONFIG_INFO',
+    cc.has_member('struct loop_config', 'info',
+                  prefix: include_default + '#include <linux/loop.h>',
+                  args: args_default))
+private_cfg.set('HAVE_STATX_BASIC_STATS',
+    cc.has_member('struct statx', 'stx_ino',
+                  prefix: include_default + '#include <sys/stat.h>',
+                  args: args_default))
 
+private_cfg.set('FUSE_LOOPDEV_ENABLED', \
+    private_cfg.get('HAVE_STRUCT_LOOP_CONFIG_INFO') and \
+    private_cfg.get('HAVE_STATX_BASIC_STATS'))
 private_cfg.set('USDT_ENABLED', get_option('enable-usdt'))
 
 # Check for liburing with SQE128 support
#130 lore raw

PATCH 23/25 libfuse: add swapfile support for iomap files

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Add flags for swapfile activation and deactivation.

---
 include/fuse_common.h |    5 +++++
 1 file changed, 5 insertions(+)
include/fuse_common.h +5 -0
index a42d70f79d57e1..a8aec81ec123a2 100644
@@ -1190,6 +1190,9 @@ int fuse_convert_to_conn_want_ext(struct fuse_conn_info *conn);
 #define FUSE_IOMAP_OP_ATOMIC		(1U << 9)
 #define FUSE_IOMAP_OP_DONTCACHE		(1U << 10)
 
+/* swapfile config operation */
+#define FUSE_IOMAP_OP_SWAPFILE		(1U << 30)
+
 /* pagecache writeback operation */
 #define FUSE_IOMAP_OP_WRITEBACK		(1U << 31)
 
@@ -1229,6 +1232,8 @@ static inline bool fuse_iomap_need_write_allocate(unsigned int opflags,
 #define FUSE_IOMAP_IOEND_APPEND		(1U << 4)
 /* is pagecache writeback */
 #define FUSE_IOMAP_IOEND_WRITEBACK	(1U << 5)
+/* swapfile deactivation */
+#define FUSE_IOMAP_IOEND_SWAPOFF	(1U << 6)
 
 /* enable fsdax */
 #define FUSE_IFLAG_DAX			(1U << 0)
#131 lore raw

PATCH 24/25 libfuse: add lower-level filesystem freeze, thaw, and shutdown requests

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Pass the kernel's filesystem freeze, thaw, and shutdown requests through to low level fuse servers.

---
 include/fuse_kernel.h   |   12 +++++++++
 include/fuse_lowlevel.h |   35 +++++++++++++++++++++++++++
 lib/fuse_lowlevel.c     |   60 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 107 insertions(+)
include/fuse_kernel.h +12 -0
index 0779e3917a1e8f..ff21973e1c88f7 100644
@@ -682,6 +682,10 @@ enum fuse_opcode {
 	FUSE_STATX		= 52,
 	FUSE_COPY_FILE_RANGE_64	= 53,
 
+	FUSE_FREEZE_FS		= 4089,
+	FUSE_UNFREEZE_FS	= 4090,
+	FUSE_SHUTDOWN_FS	= 4091,
+
 	FUSE_IOMAP_CONFIG	= 4092,
 	FUSE_IOMAP_IOEND	= 4093,
 	FUSE_IOMAP_BEGIN	= 4094,
@@ -1238,6 +1242,14 @@ struct fuse_syncfs_in {
 	uint64_t	padding;
 };
 
+struct fuse_freezefs_in {
+	uint64_t	unlinked;
+};
+
+struct fuse_shutdownfs_in {
+	uint64_t	flags;
+};
+
 /*
  * For each security context, send fuse_secctx with size of security context
  * fuse_secctx will be followed by security context name and this in turn
include/fuse_lowlevel.h +35 -0
index e2127c40940640..0d7577718490ba 100644
@@ -1439,6 +1439,41 @@ struct fuse_lowlevel_ops {
 	 */
 	void (*iomap_config) (fuse_req_t req, uint64_t flags,
 			      uint64_t maxbytes);
+
+	/**
+	 * Freeze the filesystem
+	 *
+	 * Valid replies:
+	 *   fuse_reply_err
+	 *
+	 * @param req request handle
+	 * @param ino the root inode number
+	 * @param unlinked count of open unlinked inodes
+	 */
+	void (*freezefs) (fuse_req_t req, fuse_ino_t ino, uint64_t unlinked);
+
+	/**
+	 * Thaw the filesystem
+	 *
+	 * Valid replies:
+	 *   fuse_reply_err
+	 *
+	 * @param req request handle
+	 * @param ino the root inode number
+	 */
+	void (*unfreezefs) (fuse_req_t req, fuse_ino_t ino);
+
+	/**
+	 * Shut down the filesystem
+	 *
+	 * Valid replies:
+	 *   fuse_reply_err
+	 *
+	 * @param req request handle
+	 * @param ino the root inode number
+	 * @param flags zero, currently
+	 */
+	void (*shutdownfs) (fuse_req_t req, fuse_ino_t ino, uint64_t flags);
 };
 
 /**
lib/fuse_lowlevel.c +60 -0

78 lines not on this page · raw message

#132 lore raw

PATCH 25/25 libfuse: add upper-level filesystem freeze, thaw, and shutdown events

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Pass filesystem freeze, thaw, and shutdown requests from the low level library to the upper level library so that those fuse servers can handle the events.

---
 include/fuse.h |   15 +++++++++
 lib/fuse.c     |   95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 110 insertions(+)
include/fuse.h +15 -0
index db4281e3f330c6..2f73d42672acdd 100644
@@ -925,6 +925,21 @@ struct fuse_operations {
 	 */
 	int (*iomap_config) (uint64_t supported_flags, off_t maxbytes,
 			     struct fuse_iomap_config *cfg);
+
+	/**
+	 * Freeze the filesystem
+	 */
+	int (*freezefs) (const char *path, uint64_t unlinked_files);
+
+	/**
+	 * Thaw the filesystem
+	 */
+	int (*unfreezefs) (const char *path);
+
+	/**
+	 * Shut down the filesystem
+	 */
+	int (*shutdownfs) (const char *path, uint64_t flags);
 };
 
 /** Extra context that may be needed by some filesystems
lib/fuse.c +95 -0

113 lines not on this page · raw message

#133 lore raw

PATCH v7 2/6 libfuse: allow servers to specify root node id

In reply to Darrick J. Wong

Hi all,

This series grants fuse servers full control over the entire node id address space by allowing them to specify the nodeid of the root directory. With this new feature, fuse4fs will not have to translate node ids.

If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.

With a bit of luck, this should all go splendidly. Comments and questions are, as always, welcome.

--D

kernel git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-root-nodeid
---
Commits in this patchset:
* libfuse: allow root_nodeid mount option

---
 lib/mount.c |    1 +
 1 file changed, 1 insertion(+)
#134 lore raw

PATCH 1/1 libfuse: allow root_nodeid mount option

From: Darrick J. Wong <[email protected]>

Allow this mount option so that fuse servers can configure the root nodeid if they want to.

---
 lib/mount.c |    1 +
 1 file changed, 1 insertion(+)
lib/mount.c +1 -0
index 7a856c101a7fc4..c82fd4c293ce66 100644
@@ -100,6 +100,7 @@ static const struct fuse_opt fuse_mount_opts[] = {
 	FUSE_OPT_KEY("defcontext=",		KEY_KERN_OPT),
 	FUSE_OPT_KEY("rootcontext=",		KEY_KERN_OPT),
 	FUSE_OPT_KEY("max_read=",		KEY_KERN_OPT),
+	FUSE_OPT_KEY("root_nodeid=",		KEY_KERN_OPT),
 	FUSE_OPT_KEY("user=",			KEY_MTAB_OPT),
 	FUSE_OPT_KEY("-n",			KEY_MTAB_OPT),
 	FUSE_OPT_KEY("-r",			KEY_RO),
#135 lore raw

PATCH v7 3/6 libfuse: implement syncfs

In reply to Darrick J. Wong

Hi all,

Implement syncfs in libfuse so that iomap-compatible fuse servers can receive syncfs commands, and enable fuse servers to transmit inode flags to the kernel so that it can enforce sync, immutable, and append. Also enable some of the timestamp update mount options.

If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.

With a bit of luck, this should all go splendidly. Comments and questions are, as always, welcome.

--D

kernel git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-attrs
---
Commits in this patchset:
* libfuse: add strictatime/lazytime mount options
* libfuse: set sync, immutable, and append when loading files

---
 include/fuse_common.h |    6 ++++++
 include/fuse_kernel.h |    8 ++++++++
 lib/fuse_lowlevel.c   |    6 ++++++
 lib/mount.c           |   18 ++++++++++++++++--
 4 files changed, 36 insertions(+), 2 deletions(-)
#136 lore raw

PATCH 1/2 libfuse: add strictatime/lazytime mount options

From: Darrick J. Wong <[email protected]>

fuse+iomap leaves the kernel completely in charge of handling timestamps. Add the lazytime and strictatime mount options so that fuse+iomap filesystems can take advantage of those options.

---
 lib/mount.c |   18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
lib/mount.c +16 -2
index c82fd4c293ce66..1b20c4eab92d46 100644
@@ -117,9 +117,16 @@ static const struct fuse_opt fuse_mount_opts[] = {
 	FUSE_OPT_KEY("dirsync",			KEY_KERN_FLAG),
 	FUSE_OPT_KEY("noatime",			KEY_KERN_FLAG),
 	FUSE_OPT_KEY("nodiratime",		KEY_KERN_FLAG),
-	FUSE_OPT_KEY("nostrictatime",		KEY_KERN_FLAG),
 	FUSE_OPT_KEY("symfollow",		KEY_KERN_FLAG),
 	FUSE_OPT_KEY("nosymfollow",		KEY_KERN_FLAG),
+#ifdef MS_LAZYTIME
+	FUSE_OPT_KEY("lazytime",		KEY_KERN_FLAG),
+	FUSE_OPT_KEY("nolazytime",		KEY_KERN_FLAG),
+#endif
+#ifdef MS_STRICTATIME
+	FUSE_OPT_KEY("strictatime",		KEY_KERN_FLAG),
+	FUSE_OPT_KEY("nostrictatime",		KEY_KERN_FLAG),
+#endif
 	FUSE_OPT_END
 };
 
@@ -189,11 +196,18 @@ static const struct mount_flags mount_flags[] = {
 	{"noatime", MS_NOATIME,	    1},
 	{"nodiratime",	    MS_NODIRATIME,	1},
 	{"norelatime",	    MS_RELATIME,	0},
-	{"nostrictatime",   MS_STRICTATIME,	0},
 	{"symfollow",	    MS_NOSYMFOLLOW,	0},
 	{"nosymfollow",	    MS_NOSYMFOLLOW,	1},
 #ifndef __NetBSD__
 	{"dirsync", MS_DIRSYNC,	    1},
+#endif
+#ifdef MS_LAZYTIME
+	{"lazytime",	    MS_LAZYTIME,	1},
+	{"nolazytime",	    MS_LAZYTIME,	0},
+#endif
+#ifdef MS_STRICTATIME
+	{"strictatime",	    MS_STRICTATIME,	1},
+	{"nostrictatime",   MS_STRICTATIME,	0},
 #endif
 	{NULL,	    0,		    0}
 };
#137 lore raw

PATCH 2/2 libfuse: set sync, immutable, and append when loading files

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Add these three fuse_attr::flags bits so that servers can mark a file as immutable or append-only and have the kernel advertise and enforce that.

---
 include/fuse_common.h |    6 ++++++
 include/fuse_kernel.h |    8 ++++++++
 lib/fuse_lowlevel.c   |    6 ++++++
 3 files changed, 20 insertions(+)
include/fuse_common.h +6 -0
index a8aec81ec123a2..d1bd783cd667c7 100644
@@ -1243,6 +1243,12 @@ static inline bool fuse_iomap_need_write_allocate(unsigned int opflags,
 #define FUSE_IFLAG_IOMAP		(1U << 2)
 /* enable untorn writes */
 #define FUSE_IFLAG_ATOMIC		(1U << 3)
+/* file writes are synchronous */
+#define FUSE_IFLAG_SYNC			(1U << 4)
+/* file is immutable */
+#define FUSE_IFLAG_IMMUTABLE		(1U << 5)
+/* file is append only */
+#define FUSE_IFLAG_APPEND		(1U << 6)
 
 /* Which fields are set in fuse_iomap_config_out? */
 #define FUSE_IOMAP_CONFIG_SID		(1 << 0ULL)
include/fuse_kernel.h +8 -0
index ff21973e1c88f7..bee825a6d17ad5 100644
@@ -249,6 +249,8 @@
  *  - add FUSE_IOMAP_CONFIG so the fuse server can configure more fs geometry
  *  - add FUSE_NOTIFY_IOMAP_DEV_INVAL to invalidate iomap bdev ranges
  *  - add FUSE_ATTR_ATOMIC for single-fsblock atomic write support
+ *  - add FUSE_ATTR_{SYNC,IMMUTABLE,APPEND} for VFS enforcement of file
+ *    attributes
  */
 
 #ifndef _LINUX_FUSE_H
@@ -593,12 +595,18 @@ struct fuse_file_lock {
  * kernel can use cached attributes more aggressively (e.g. ACL inheritance)
  * FUSE_ATTR_IOMAP: Use iomap for this inode
  * FUSE_ATTR_ATOMIC: Enable untorn writes
+ * FUSE_ATTR_SYNC: File writes are always synchronous
+ * FUSE_ATTR_IMMUTABLE: File is immutable
+ * FUSE_ATTR_APPEND: File is append-only
  */
 #define FUSE_ATTR_SUBMOUNT      (1 << 0)
 #define FUSE_ATTR_DAX		(1 << 1)
 #define FUSE_ATTR_EXCLUSIVE	(1 << 2)
 #define FUSE_ATTR_IOMAP		(1 << 3)
 #define FUSE_ATTR_ATOMIC	(1 << 4)
+#define FUSE_ATTR_SYNC		(1 << 5)
+#define FUSE_ATTR_IMMUTABLE	(1 << 6)
+#define FUSE_ATTR_APPEND	(1 << 7)
 
 /**
  * Open flags
lib/fuse_lowlevel.c +6 -0
index 18503a1fa64d88..7b501e2f3ef047 100644
@@ -170,6 +170,12 @@ static void convert_stat(const struct stat *stbuf, struct fuse_attr *attr,
 		attr->flags |= FUSE_ATTR_IOMAP;
 	if (iflags & FUSE_IFLAG_ATOMIC)
 		attr->flags |= FUSE_ATTR_ATOMIC;
+	if (iflags & FUSE_IFLAG_SYNC)
+		attr->flags |= FUSE_ATTR_SYNC;
+	if (iflags & FUSE_IFLAG_IMMUTABLE)
+		attr->flags |= FUSE_ATTR_IMMUTABLE;
+	if (iflags & FUSE_IFLAG_APPEND)
+		attr->flags |= FUSE_ATTR_APPEND;
 }
 
 static void convert_attr(const struct fuse_setattr_in *attr, struct stat *stbuf)
#138 lore raw

PATCH v7 4/6 libfuse: cache iomap mappings for even better file IO performance

In reply to Darrick J. Wong

Hi all,

This series improves the performance (and correctness for some filesystems) by adding the ability to cache iomap mappings in the kernel. For filesystems that can change mapping states during pagecache writeback (e.g. unwritten extent conversion) this is absolutely necessary to deal with races with writes to the pagecache because writeback does not take i_rwsem. For everyone else, it simply eliminates roundtrips to userspace.

If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.

With a bit of luck, this should all go splendidly. Comments and questions are, as always, welcome.

--D

kernel git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-cache
---
Commits in this patchset:
* libfuse: enable iomap cache management for lowlevel fuse
* libfuse: add upper-level iomap cache management
* libfuse: allow constraining of iomap mapping cache size
* libfuse: add upper-level iomap mapping cache constraint code
* libfuse: enable iomap

---
 include/fuse.h          |   32 +++++++++++++++++
 include/fuse_common.h   |   15 ++++++++
 include/fuse_kernel.h   |   33 +++++++++++++++++
 include/fuse_lowlevel.h |   44 ++++++++++++++++++++++-
 lib/fuse.c              |   44 ++++++++++++++++++++---
 lib/fuse_lowlevel.c     |   90 ++++++++++++++++++++++++++++++++++++++++++++---
 lib/fuse_versionscript  |    4 ++
 7 files changed, 249 insertions(+), 13 deletions(-)
#139 lore raw

PATCH 1/5 libfuse: enable iomap cache management for lowlevel fuse

From: Darrick J. Wong <[email protected]>

Add the library methods so that fuse servers can manage an in-kernel iomap cache. This enables better performance on small IOs and is required if the filesystem needs synchronization between pagecache writes and writeback.

---
 include/fuse_common.h   |   12 +++++++
 include/fuse_kernel.h   |   27 ++++++++++++++++
 include/fuse_lowlevel.h |   41 +++++++++++++++++++++++++
 lib/fuse_lowlevel.c     |   77 +++++++++++++++++++++++++++++++++++++++++++++++
 lib/fuse_versionscript  |    2 +
 5 files changed, 159 insertions(+)
include/fuse_common.h +12 -0
index d1bd783cd667c7..313f78c9cb6632 100644
@@ -1158,6 +1158,10 @@ int fuse_convert_to_conn_want_ext(struct fuse_conn_info *conn);
 
 /* fuse-specific mapping type indicating that writes use the read mapping */
 #define FUSE_IOMAP_TYPE_PURE_OVERWRITE	(255)
+/* fuse-specific mapping type saying the server has populated the cache */
+#define FUSE_IOMAP_TYPE_RETRY_CACHE	(254)
+/* do not upsert this mapping */
+#define FUSE_IOMAP_TYPE_NOCACHE		(253)
 
 #define FUSE_IOMAP_DEV_NULL		(0U)	/* null device cookie */
 
@@ -1281,6 +1285,14 @@ struct fuse_iomap_config{
 	int64_t s_maxbytes;	/* max file size */
 };
 
+/* invalidate to end of file */
+#define FUSE_IOMAP_INVAL_TO_EOF		(~0ULL)
+
+struct fuse_file_range {
+	uint64_t offset;	/* file offset to invalidate, bytes */
+	uint64_t length;	/* length to invalidate, bytes */
+};
+
 /* ----------------------------------------------------------- *
  * Compatibility stuff					       *
  * ----------------------------------------------------------- */
include/fuse_kernel.h +27 -0
index bee825a6d17ad5..f2a1e187aea3a1 100644
@@ -251,6 +251,8 @@
  *  - add FUSE_ATTR_ATOMIC for single-fsblock atomic write support
  *  - add FUSE_ATTR_{SYNC,IMMUTABLE,APPEND} for VFS enforcement of file
  *    attributes
+ *  - add FUSE_NOTIFY_IOMAP_{UPSERT,INVAL}_MAPPINGS so fuse servers can cache
+ *    file range mappings in the kernel for iomap
  */
 
 #ifndef _LINUX_FUSE_H
@@ -718,6 +720,8 @@ enum fuse_notify_code {
 	FUSE_NOTIFY_INC_EPOCH = 8,
 	FUSE_NOTIFY_PRUNE = 9,
 	FUSE_NOTIFY_IOMAP_DEV_INVAL = 99,
+	FUSE_NOTIFY_IOMAP_UPSERT_MAPPINGS = 100,
+	FUSE_NOTIFY_IOMAP_INVAL_MAPPINGS = 101,
 };
 
 /* The read buffer is required to be at least 8k, but may be much larger */
@@ -1460,4 +1464,27 @@ struct fuse_iomap_dev_inval_out {
 	struct fuse_range range;
 };
 
+struct fuse_iomap_inval_mappings_out {
+	uint64_t nodeid;	/* Inode ID */
+	uint64_t attr_ino;	/* matches fuse_attr:ino */
+
+	/*
+	 * Range of read and mappings to invalidate.  Zero length means ignore
+	 * the range; and FUSE_IOMAP_INVAL_TO_EOF can be used for length.
+	 */
+	struct fuse_range read;
+	struct fuse_range write;
+};
+
+struct fuse_iomap_upsert_mappings_out {
+	uint64_t nodeid;	/* Inode ID */
+	uint64_t attr_ino;	/* matches fuse_attr:ino */
+
+	/* read file data from here */
+	struct fuse_iomap_io	read;
+
+	/* write file data to here, if applicable */
+	struct fuse_iomap_io	write;
+};
+
 #endif /* _LINUX_FUSE_H */
include/fuse_lowlevel.h +41 -0

47 lines not on this page · raw message

lib/fuse_lowlevel.c +77 -0

83 lines not on this page · raw message

lib/fuse_versionscript +2 -0
index f459d06c2f3377..ed388f8ebdb558 100644
@@ -250,6 +250,8 @@ FUSE_3.99 {
 		fuse_fs_iomap_device_invalidate;
 		fuse_lowlevel_disable_fsreclaim;
 		fuse_loopdev_setup;
+		fuse_lowlevel_iomap_upsert_mappings;
+		fuse_lowlevel_iomap_inval_mappings;
 } FUSE_3.19;
 
 # Local Variables:
#140 lore raw

PATCH 2/5 libfuse: add upper-level iomap cache management

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Make it so that upper-level fuse servers can use the iomap cache too.

---
 include/fuse.h         |   31 +++++++++++++++++++++++++++++++
 lib/fuse.c             |   30 ++++++++++++++++++++++++++++++
 lib/fuse_versionscript |    2 ++
 3 files changed, 63 insertions(+)
include/fuse.h +31 -0
index 2f73d42672acdd..e54aa1368bbb6b 100644
@@ -1488,6 +1488,37 @@ bool fuse_fs_can_enable_iomap(const struct stat *statbuf);
  */
 bool fuse_fs_can_enable_iomapx(const struct statx *statxbuf);
 
+/*
+ * Upsert some file mapping information into the kernel.  This is necessary
+ * for filesystems that require coordination of mapping state changes between
+ * buffered writes and writeback, and desirable for better performance
+ * elsewhere.
+ *
+ * @param nodeid the inode number
+ * @param attr_ino inode number as told by fuse_attr::ino
+ * @param read mapping information for file reads
+ * @param write mapping information for file writes
+ * @return zero for success, -errno for failure
+ */
+int fuse_fs_iomap_upsert(uint64_t nodeid, uint64_t attr_ino,
+			 const struct fuse_file_iomap *read,
+			 const struct fuse_file_iomap *write);
+
+/**
+ * Invalidate some file mapping information in the kernel.
+ *
+ * @param nodeid the inode number
+ * @param attr_ino inode number as told by fuse_attr::ino
+ * @param read_off start of the range of read mappings to invalidate
+ * @param read_len length of the range of read mappings to invalidate
+ * @param write_off start of the range of write mappings to invalidate
+ * @param write_len length of the range of write mappings to invalidate
+ * @return zero for success, -errno for failure
+ */
+int fuse_fs_iomap_inval(uint64_t nodeid, uint64_t attr_ino, loff_t read_off,
+			uint64_t read_len, loff_t write_off,
+			uint64_t write_len);
+
 int fuse_notify_poll(struct fuse_pollhandle *ph);
 
 /**
lib/fuse.c +30 -0
index 2969e0f332045f..0fb1bc106514a1 100644
@@ -3012,6 +3012,36 @@ static int fuse_fs_shutdownfs(struct fuse_fs *fs, const char *path,
 	return fs->op.shutdownfs(path, flags);
 }
 
+int fuse_fs_iomap_upsert(uint64_t nodeid, uint64_t attr_ino,
+			 const struct fuse_file_iomap *read,
+			 const struct fuse_file_iomap *write)
+{
+	struct fuse_context *ctxt = fuse_get_context();
+	struct fuse_session *se = fuse_get_session(ctxt->fuse);
+
+	return fuse_lowlevel_iomap_upsert_mappings(se, nodeid, attr_ino, read,
+						   write);
+}
+
+int fuse_fs_iomap_inval(uint64_t nodeid, uint64_t attr_ino,
+			loff_t read_off, uint64_t read_len,
+			loff_t write_off, uint64_t write_len)
+{
+	struct fuse_context *ctxt = fuse_get_context();
+	struct fuse_session *se = fuse_get_session(ctxt->fuse);
+	struct fuse_file_range read = {
+		.offset = read_off,
+		.length = read_len,
+	};
+	struct fuse_file_range write = {
+		.offset = write_off,
+		.length = write_len,
+	};
+
+	return fuse_lowlevel_iomap_inval_mappings(se, nodeid, attr_ino, &read,
+						  &write);
+}
+
 static void fuse_lib_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,
 			     int valid, struct fuse_file_info *fi)
 {
lib/fuse_versionscript +2 -0
index ed388f8ebdb558..60d5e1be50deaa 100644
@@ -252,6 +252,8 @@ FUSE_3.99 {
 		fuse_loopdev_setup;
 		fuse_lowlevel_iomap_upsert_mappings;
 		fuse_lowlevel_iomap_inval_mappings;
+		fuse_fs_iomap_upsert;
+		fuse_fs_iomap_inval;
 } FUSE_3.19;
 
 # Local Variables:
#141 lore raw

PATCH 3/5 libfuse: allow constraining of iomap mapping cache size

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Allow the fuse server to constrain the maximum size of each iomap mapping cache.

---
 include/fuse_common.h   |    3 +++
 include/fuse_kernel.h   |    6 +++++-
 include/fuse_lowlevel.h |    3 ++-
 lib/fuse.c              |    2 +-
 lib/fuse_lowlevel.c     |   11 ++++++++---
 5 files changed, 19 insertions(+), 6 deletions(-)
include/fuse_common.h +3 -0
index 313f78c9cb6632..18c2e0e11ce8ce 100644
@@ -1261,6 +1261,7 @@ static inline bool fuse_iomap_need_write_allocate(unsigned int opflags,
 #define FUSE_IOMAP_CONFIG_MAX_LINKS	(1 << 3ULL)
 #define FUSE_IOMAP_CONFIG_TIME		(1 << 4ULL)
 #define FUSE_IOMAP_CONFIG_MAXBYTES	(1 << 5ULL)
+#define FUSE_IOMAP_CONFIG_CACHE_MAXBYTES (1 << 6ULL)
 
 struct fuse_iomap_config{
 	uint64_t flags;		/* FUSE_IOMAP_CONFIG_* */
@@ -1283,6 +1284,8 @@ struct fuse_iomap_config{
 	int64_t s_time_max;
 
 	int64_t s_maxbytes;	/* max file size */
+
+	uint32_t cache_maxbytes; /* mapping cache maximum size */
 };
 
 /* invalidate to end of file */
include/fuse_kernel.h +5 -1
index f2a1e187aea3a1..8149657ac44cb9 100644
@@ -1425,7 +1425,9 @@ struct fuse_iomap_ioend_out {
 struct fuse_iomap_config_in {
 	uint64_t flags;		/* supported FUSE_IOMAP_CONFIG_* flags */
 	int64_t maxbytes;	/* max supported file size */
-	uint64_t padding[6];	/* zero */
+	uint32_t cache_maxbytes; /* mapping cache maxbytes */
+	uint32_t zero;		/* zero */
+	uint64_t padding[5];	/* zero */
 };
 
 struct fuse_iomap_config_out {
@@ -1449,6 +1451,8 @@ struct fuse_iomap_config_out {
 	int64_t s_time_max;
 
 	int64_t s_maxbytes;	/* max file size */
+
+	uint32_t cache_maxbytes; /* mapping cache maximum size */
 };
 
 struct fuse_range {
include/fuse_lowlevel.h +2 -1
index 67fdde0a5f49d9..77fc623386ce20 100644
@@ -1436,9 +1436,10 @@ struct fuse_lowlevel_ops {
 	 * @param req request handle
 	 * @param flags FUSE_IOMAP_CONFIG_* flags that can be passed back
 	 * @param maxbytes maximum supported file size
+	 * @param cache_maxbytes maximum allowed iomap cache size
 	 */
 	void (*iomap_config) (fuse_req_t req, uint64_t flags,
-			      uint64_t maxbytes);
+			      uint64_t maxbytes, uint32_t cache_maxbytes);
 
 	/**
 	 * Freeze the filesystem
lib/fuse.c +1 -1
index 0fb1bc106514a1..04836044e7a25b 100644
@@ -4923,7 +4923,7 @@ static void fuse_lib_iomap_ioend(fuse_req_t req, fuse_ino_t nodeid,
 }
 
 static void fuse_lib_iomap_config(fuse_req_t req, uint64_t flags,
-				  uint64_t maxbytes)
+				  uint64_t maxbytes, uint32_t cache_maxbytes)
 {
 	struct fuse_iomap_config cfg = { };
 	struct fuse *f = req_fuse_prepare(req);
lib/fuse_lowlevel.c +8 -3
index ea6aba18619458..b2bf2e5345cc71 100644
@@ -2852,7 +2852,8 @@ static void do_iomap_ioend(fuse_req_t req, const fuse_ino_t nodeid,
 			      FUSE_IOMAP_CONFIG_BLOCKSIZE | \
 			      FUSE_IOMAP_CONFIG_MAX_LINKS | \
 			      FUSE_IOMAP_CONFIG_TIME | \
-			      FUSE_IOMAP_CONFIG_MAXBYTES)
+			      FUSE_IOMAP_CONFIG_MAXBYTES | \
+			      FUSE_IOMAP_CONFIG_CACHE_MAXBYTES)
 
 #define FUSE_IOMAP_CONFIG_ALL (FUSE_IOMAP_CONFIG_V1)
 
@@ -2861,7 +2862,7 @@ static ssize_t iomap_config_reply_size(const struct fuse_iomap_config *cfg)
 	if (cfg->flags & ~FUSE_IOMAP_CONFIG_ALL)
 		return -EINVAL;
 
-	return offsetofend(struct fuse_iomap_config_out, s_maxbytes);
+	return offsetofend(struct fuse_iomap_config_out, cache_maxbytes);
 }
 
 int fuse_reply_iomap_config(fuse_req_t req, const struct fuse_iomap_config *cfg)
@@ -2899,6 +2900,9 @@ int fuse_reply_iomap_config(fuse_req_t req, const struct fuse_iomap_config *cfg)
 	if (cfg->flags & FUSE_IOMAP_CONFIG_MAXBYTES)
 		arg.s_maxbytes = cfg->s_maxbytes;
 
+	if (cfg->flags & FUSE_IOMAP_CONFIG_CACHE_MAXBYTES)
+		arg.cache_maxbytes = cfg->cache_maxbytes;
+
 	return send_reply_ok(req, &arg, reply_size);
 }
 
@@ -2912,7 +2916,8 @@ static void _do_iomap_config(fuse_req_t req, const fuse_ino_t nodeid,
 	if (req->se->op.iomap_config)
 		req->se->op.iomap_config(req,
 					 arg->flags & FUSE_IOMAP_CONFIG_ALL,
-					 arg->maxbytes);
+					 arg->maxbytes,
+					 arg->cache_maxbytes);
 	else
 		fuse_reply_err(req, ENOSYS);
 }
#142 lore raw

PATCH 4/5 libfuse: add upper-level iomap mapping cache constraint code

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Allow high-level fuse servers to constrain the maximum size of each iomap mapping cache.

---
 include/fuse.h |    1 +
 lib/fuse.c     |   12 +++++++-----
 2 files changed, 8 insertions(+), 5 deletions(-)
include/fuse.h +1 -0
index e54aa1368bbb6b..bdc910ad1c2381 100644
@@ -924,6 +924,7 @@ struct fuse_operations {
 	 * files.
 	 */
 	int (*iomap_config) (uint64_t supported_flags, off_t maxbytes,
+			     uint32_t cache_maxbytes,
 			     struct fuse_iomap_config *cfg);
 
 	/**
lib/fuse.c +7 -5
index 04836044e7a25b..15372b23a7aef4 100644
@@ -2964,7 +2964,7 @@ static int fuse_fs_iomap_ioend(struct fuse_fs *fs, const char *path,
 }
 
 static int fuse_fs_iomap_config(struct fuse_fs *fs, uint64_t flags,
-				uint64_t maxbytes,
+				uint64_t maxbytes, uint32_t cache_maxbytes,
 				struct fuse_iomap_config *cfg)
 {
 	fuse_get_context()->private_data = fs->user_data;
@@ -2973,11 +2973,12 @@ static int fuse_fs_iomap_config(struct fuse_fs *fs, uint64_t flags,
 
 	if (fs->debug) {
 		fuse_log(FUSE_LOG_DEBUG,
-			 "iomap_config flags 0x%llx maxbytes %lld\n",
-			 (unsigned long long)flags, (long long)maxbytes);
+			 "iomap_config flags 0x%llx maxbytes %lld cache_maxbytes %u\n",
+			 (unsigned long long)flags, (long long)maxbytes,
+			 cache_maxbytes);
 	}
 
-	return fs->op.iomap_config(flags, maxbytes, cfg);
+	return fs->op.iomap_config(flags, maxbytes, cache_maxbytes, cfg);
 }
 
 static int fuse_fs_freezefs(struct fuse_fs *fs, const char *path,
@@ -4931,7 +4932,8 @@ static void fuse_lib_iomap_config(fuse_req_t req, uint64_t flags,
 	int err;
 
 	fuse_prepare_interrupt(f, req, &d);
-	err = fuse_fs_iomap_config(f->fs, flags, maxbytes, &cfg);
+	err = fuse_fs_iomap_config(f->fs, flags, maxbytes, cache_maxbytes,
+				   &cfg);
 	fuse_finish_interrupt(f, req, &d);
 	if (err) {
 		reply_err(req, err);
#143 lore raw

PATCH 5/5 libfuse: enable iomap

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Remove the guard that we used to avoid bisection problems.

---
 lib/fuse_lowlevel.c |    2 --
 1 file changed, 2 deletions(-)
lib/fuse_lowlevel.c +0 -2
index b2bf2e5345cc71..714c26385c044f 100644
@@ -3181,8 +3181,6 @@ _do_init(fuse_req_t req, const fuse_ino_t nodeid, const void *op_in,
 			se->conn.capable_ext |= FUSE_CAP_OVER_IO_URING;
 		if (inargflags & FUSE_IOMAP)
 			se->conn.capable_ext |= FUSE_CAP_IOMAP;
-		/* Don't let anyone touch iomap until the end of the patchset. */
-		se->conn.capable_ext &= ~FUSE_CAP_IOMAP;
 	} else {
 		se->conn.max_readahead = 0;
 	}
#144 lore raw

PATCH v7 5/6 libfuse: run fuse servers as a contained service

In reply to Darrick J. Wong

Hi all,

This patchset defines the necessary communication protocols and library code so that users can mount fuse servers that run in unprivileged systemd service containers. That in turn allows unprivileged untrusted mounts, because the worst that can happen is that a malicious image crashes the fuse server and the mount dies, instead of corrupting the kernel. As part of the delegation, add a new ioctl allowing any process with an open fusedev fd to ask for permission for anyone with that fusedev fd to use iomap.

If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.

With a bit of luck, this should all go splendidly. Comments and questions are, as always, welcome.

--D

kernel git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-service-container
---
Commits in this patchset:
* libfuse: add systemd/inetd socket service mounting helper
* libfuse: integrate fuse services into mount.fuse3
* libfuse: delegate iomap privilege from mount.service to fuse services
* libfuse: enable setting iomap block device block size
* fuservicemount: create loop devices for regular files

---
 include/fuse_kernel.h       |    8 
 include/fuse_lowlevel.h     |   22 +
 include/fuse_service.h      |  170 +++++++
 include/fuse_service_priv.h |  127 +++++
 lib/fuse_i.h                |    5 
 util/mount_service.h        |   41 ++
 doc/fuservicemount3.8       |   32 +
 doc/meson.build             |    3 
 include/meson.build         |    4 
 lib/fuse_lowlevel.c         |   16 +
 lib/fuse_service.c          |  848 +++++++++++++++++++++++++++++++++
 lib/fuse_service_stub.c     |   91 ++++
 lib/fuse_versionscript      |   15 +
 lib/helper.c                |   53 ++
 lib/meson.build             |   14 +
 lib/mount.c                 |   57 ++
 meson.build                 |   36 +
 meson_options.txt           |    6 
 util/fuservicemount.c       |   66 +++
 util/meson.build            |   13 -
 util/mount.fuse.c           |   58 +-
 util/mount_service.c        | 1100 +++++++++++++++++++++++++++++++++++++++++++
 22 files changed, 2749 insertions(+), 36 deletions(-)
 create mode 100644 include/fuse_service.h
 create mode 100644 include/fuse_service_priv.h
 create mode 100644 util/mount_service.h
 create mode 100644 doc/fuservicemount3.8
 create mode 100644 lib/fuse_service.c
 create mode 100644 lib/fuse_service_stub.c
 create mode 100644 util/fuservicemount.c
 create mode 100644 util/mount_service.c
#145 lore raw

PATCH 1/5 libfuse: add systemd/inetd socket service mounting helper

From: Darrick J. Wong <[email protected]>

Create a mount.service helper that can start a fuse server that runs as a socket-based systemd (or inetd) service. To make things simpler for fuse server authors, define a new library interface to wrap all the functionality so that they don't have to know the details

This enables untrusted ext4 mounts via systemd service containers, which avoids the problem of malicious filesystems compromising the integrity of the running kernel through memory corruption.

---
 include/fuse_service.h      |  153 +++++++
 include/fuse_service_priv.h |  116 +++++
 lib/fuse_i.h                |    5 
 util/mount_service.h        |   32 +
 doc/fuservicemount3.8       |   24 +
 doc/meson.build             |    3 
 include/meson.build         |    4 
 lib/fuse_service.c          |  796 +++++++++++++++++++++++++++++++++++
 lib/fuse_service_stub.c     |   91 ++++
 lib/fuse_versionscript      |   12 +
 lib/helper.c                |   53 ++
 lib/meson.build             |   14 +
 lib/mount.c                 |   57 ++
 meson.build                 |   36 ++
 meson_options.txt           |    6 
 util/fuservicemount.c       |   18 +
 util/meson.build            |    9 
 util/mount_service.c        |  984 +++++++++++++++++++++++++++++++++++++++++++
 18 files changed, 2399 insertions(+), 14 deletions(-)
 create mode 100644 include/fuse_service.h
 create mode 100644 include/fuse_service_priv.h
 create mode 100644 util/mount_service.h
 create mode 100644 doc/fuservicemount3.8
 create mode 100644 lib/fuse_service.c
 create mode 100644 lib/fuse_service_stub.c
 create mode 100644 util/fuservicemount.c
 create mode 100644 util/mount_service.c
include/fuse_service.h new file +153 -0
new file mode 100644
index 00000000000000..d3a3a4a4237380
@@ -0,0 +1,153 @@
+/*  FUSE: Filesystem in Userspace
+  Copyright (C) 2025-2026 Oracle.
+  Author: Darrick J. Wong <[email protected]>
+
+  This program can be distributed under the terms of the GNU LGPLv2.
+  See the file LGPL2.txt.
+*/
+#ifndef FUSE_SERVICE_H_
+#define FUSE_SERVICE_H_
+
+struct fuse_service;
+
+/**
+ * Accept a socket created by mount.service for information exchange.
+ *
+ * @param sfp pointer to pointer to a service context
+ * @return -1 on error, 0 on success
+ */
+int fuse_service_accept(struct fuse_service **sfp);
+
+/**
+ * Has the fuse server accepted a service context?
+ *
+ * @param sf service context
+ */
+static inline bool fuse_service_accepted(struct fuse_service *sf)
+{
+	return sf != NULL;
+}
+
+/**
+ * Release all resources associated with the service context.
+ *
+ * @param sfp service context
+ */
+void fuse_service_release(struct fuse_service *sf);
+
+/**
+ * Destroy a service context and release all resources
+ *
+ * @param sfp pointer to pointer to a service context
+ */
+void fuse_service_destroy(struct fuse_service **sfp);
+
+/**
+ * Append the command line arguments from the mount service helper to an
+ * existing fuse_args structure.  The fuse_args should have been initialized
+ * with the argc and argv passed to main().
+ *
+ * @param sfp service context
+ * @param args arguments to modify (input+output)
+ * @return -1 on success, 0 on success
+ */
+int fuse_service_append_args(struct fuse_service *sf, struct fuse_args *args);
+
+/**
+ * Generate the effective fuse server command line from the args structure.
+ * The args structure should be the outcome from fuse_service_append_args.
+ * The resulting string is suitable for setproctitle and must be freed by the
+ * callre.
+ *
+ * @param argc argument count passed to main()
+ * @param argv argument vector passed to main()
+ * @param args fuse args structure
+ * @return effective command line string, or NULL
+ */
+char *fuse_service_cmdline(int argc, char *argv[], struct fuse_args *args);
+
+/**
+ * Take the fuse device fd passed from the mount.service helper
+ *
+ * @return device fd on success, -1 on error
+ */
+int fuse_service_take_fusedev(struct fuse_service *sfp);
+
+/**
+ * Utility function to parse common options for simple file systems
+ * using the low-level API. A help text that describes the available
+ * options can be printed with `fuse_cmdline_help`. A single
+ * non-option argument is treated as the mountpoint. Multiple
+ * non-option arguments will result in an error.
+ *
+ * If neither -o subtype= or -o fsname= options are given, a new
+ * subtype option will be added and set to the basename of the program
+ * (the fsname will remain unset, and then defaults to "fuse").
+ *
+ * Known options will be removed from *args*, unknown options will
+ * remain. The mountpoint will not be checked here; that is the job of
+ * mount.service.
+ *
+ * @param args argument vector (input+output)
+ * @param opts output argument for parsed options
+ * @return 0 on success, -1 on failure
+ */
+int fuse_service_parse_cmdline_opts(struct fuse_args *args,
+				    struct fuse_cmdline_opts *opts);
+
+/**
+ * Ask the mount.service helper to open a file on behalf of the fuse server.
+ *
+ * @param sf service context
+ * @param path path to file
+ * @param open_flags O_ flags
+ * @param create_mode mode with which to create the file
+ * @param request_flags set of FUSE_SERVICE_REQUEST_* flags
+ * @return 0 on success, -1 on failure
+ */
+int fuse_service_request_file(struct fuse_service *sf, const char *path,
+			      int open_flags, mode_t create_mode,
+			      unsigned int request_flags);
+
+/**
+ * Receive a file perviously requested.
+ *
+ * @param sf service context
+ * @param path to file
+ * @fdp pointer to file descriptor, which will be set to -1 if the file could
+ *      not be opened
+ * @return -1 on socket communication failure, 0 otherwise
+ */
+int fuse_service_receive_file(struct fuse_service *sf,
+			      const char *path, int *fdp);
+
+/**
+ * Prevent the mount.service server from sending us any more open files.
+ *
+ * @param sf service context
+ */
+int fuse_service_finish_file_requests(struct fuse_service *sf);
+
+/**
+ * Ask the mount.service helper to mount the filesystem for us.  The fuse client
+ * will begin sending requests to the fuse server immediately after this.
+ *
+ * @param sf service context
+ * @param se fuse session
+ * @param mountpoint place to mount the filesystem
+ * @return 0 on success, -1 on error
+ */
+int fuse_service_mount(struct fuse_service *sf, struct fuse_session *se,
+		       const char *mountpoint);
+
+/**
+ * Bid farewell to the mount.service helper.  It is still necessary to call
+ * fuse_service_destroy after this.
+ *
+ * @param sf service context
+ * @param error any additional errors to send to the mount helper
+ * @return 0 on success, -1 on error
+ */
+int fuse_service_send_goodbye(struct fuse_service *sf, int error);
+
+#endif /* FUSE_SERVICE_H_ */
include/fuse_service_priv.h new file +116 -0

116 lines not on this page · raw message

lib/fuse_i.h +5 -0
index c7d0d38408105f..4eade95f10f4e8 100644
@@ -226,6 +226,11 @@ unsigned get_max_read(struct mount_opts *o);
 void fuse_kern_unmount(const char *mountpoint, int fd);
 int fuse_kern_mount(const char *mountpoint, struct mount_opts *mo);
 
+char *fuse_mountopts_fstype(const struct mount_opts *mo);
+char *fuse_mountopts_source(const struct mount_opts *mo, const char *devname);
+char *fuse_mountopts_kernel_opts(const struct mount_opts *mo);
+unsigned int fuse_mountopts_flags(const struct mount_opts *mo);
+
 int fuse_send_reply_iov_nofree(fuse_req_t req, int error, struct iovec *iov,
 			       int count);
 void fuse_free_req(fuse_req_t req);
util/mount_service.h new file +32 -0
new file mode 100644
index 00000000000000..16e120da10765e
@@ -0,0 +1,32 @@
+/*
+  FUSE: Filesystem in Userspace
+  Copyright (C) 2025-2026 Oracle.
+  Author: Darrick J. Wong <[email protected]>
+
+  This program can be distributed under the terms of the GNU GPLv2.
+  See the file GPL2.txt.
+*/
+#ifndef MOUNT_SERVICE_H_
+#define MOUNT_SERVICE_H_
+
+/**
+ * Connect to a fuse service socket and try to mount the filesystem as
+ * specified with the CLI arguments.
+ *
+ * @argc argument count
+ * @argv vector of argument strings
+ * @return EXIT_SUCCESS for success, EXIT_FAILURE if mount fails
+ */
+int mount_service_main(int argc, char *argv[]);
+
+/**
+ * Return the fuse filesystem subtype from a full fuse filesystem type
+ * specification.  IOWs, fuse.Y -> Y; fuseblk.Z -> Z; or A -> A.  The returned
+ * pointer is within the caller's string.
+ *
+ * @param fstype full fuse filesystem type
+ * @return fuse subtype
+ */
+const char *mount_service_subtype(const char *fstype);
+
+#endif /* MOUNT_SERVICE_H_ */
doc/fuservicemount3.8 new file +24 -0
new file mode 100644
index 00000000000000..e45d6a89c8b81a
@@ -0,0 +1,24 @@
+.TH fuservicemount3 "8"
+.SH NAME
+fuservicemount3 \- mount a FUSE filesystem that runs as a system socket service
+.SH SYNOPSIS
+.B fuservicemount3
+.B source
+.B mountpoint
+.BI -t " fstype"
+[
+.I options
+]
+.SH DESCRIPTION
+Mount a filesystem using a FUSE server that runs as a socket service.
+These servers can be contained using the platform's service management
+framework.
+.SH "AUTHORS"
+.LP
+The author of the fuse socket service code is Darrick J. Wong <[email protected]>.
+Debian GNU/Linux distribution.
+.SH SEE ALSO
+.BR fusermount3 (1)
+.BR fusermount (1)
+.BR mount (8)
+.BR fuse (4)
doc/meson.build +3 -0
index db3e0b26f71975..c105cf3471fdf4 100644
@@ -2,3 +2,6 @@ if not platform.endswith('bsd') and platform != 'dragonfly'
   install_man('fusermount3.1', 'mount.fuse3.8')
 endif
 
+if private_cfg.get('HAVE_SERVICEMOUNT', false)
+  install_man('fuservicemount3.8')
+endif
include/meson.build +4 -0
index 0b1e3a9d4fcb43..5ab4ecf052bf56 100644
@@ -5,4 +5,8 @@ if private_cfg.get('FUSE_LOOPDEV_ENABLED')
   libfuse_headers += [ 'fuse_loopdev.h' ]
 endif
 
+if private_cfg.get('HAVE_SERVICEMOUNT', false)
+  libfuse_headers += [ 'fuse_service.h' ]
+endif
+
 install_headers(libfuse_headers, subdir: 'fuse3')
lib/fuse_service.c new file +796 -0

796 lines not on this page · raw message

lib/fuse_service_stub.c new file +91 -0

91 lines not on this page · raw message

lib/fuse_versionscript +12 -0
index 60d5e1be50deaa..935eaec820c7a7 100644
@@ -254,6 +254,18 @@ FUSE_3.99 {
 		fuse_lowlevel_iomap_inval_mappings;
 		fuse_fs_iomap_upsert;
 		fuse_fs_iomap_inval;
+		fuse_service_accept;
+		fuse_service_append_args;
+		fuse_service_destroy;
+		fuse_service_finish_file_requests;
+		fuse_service_mount;
+		fuse_service_cmdline;
+		fuse_service_parse_cmdline_opts;
+		fuse_service_receive_file;
+		fuse_service_release;
+		fuse_service_request_file;
+		fuse_service_send_goodbye;
+		fuse_service_take_fusedev;
 } FUSE_3.19;
 
 # Local Variables:
lib/helper.c +53 -0

71 lines not on this page · raw message

lib/meson.build +13 -1
index 477b0fc2f86d38..4fe11e346c46d3 100644
@@ -11,6 +11,12 @@ else
    libfuse_sources += [ 'mount_bsd.c' ]
 endif
 
+if private_cfg.get('HAVE_SERVICEMOUNT', false)
+  libfuse_sources += [ 'fuse_service.c' ]
+else
+  libfuse_sources += [ 'fuse_service_stub.c' ]
+endif
+
 deps = [ thread_dep ]
 if private_cfg.get('HAVE_ICONV')
    libfuse_sources += [ 'modules/iconv.c' ]
@@ -55,13 +61,19 @@ libfuse = library('fuse3',
                   link_args: ['-Wl,--version-script,' + meson.current_source_dir()
                               + '/fuse_versionscript' ])
 
+vars = []
+if private_cfg.get('HAVE_SERVICEMOUNT', false)
+  service_socket_dir = private_cfg.get_unquoted('FUSE_SERVICE_SOCKET_DIR', '')
+  vars += ['service_socket_dir=' + service_socket_dir]
+endif
 pkg = import('pkgconfig')
 pkg.generate(libraries: [ libfuse, '-lpthread' ],
              libraries_private: '-ldl',
              version: meson.project_version(),
              name: 'fuse3',
              description: 'Filesystem in Userspace',
-             subdirs: 'fuse3')
+             subdirs: 'fuse3',
+             variables: vars)
 
 libfuse_dep = declare_dependency(include_directories: include_dirs,
                                  link_with: libfuse, dependencies: deps)
lib/mount.c +44 -13

74 lines not on this page · raw message

meson.build +36 -0
index 73aee98c775a2a..360912f1773662 100644
@@ -69,6 +69,11 @@ args_default = [ '-D_GNU_SOURCE' ]
 #
 private_cfg = configuration_data()
 private_cfg.set_quoted('PACKAGE_VERSION', meson.project_version())
+service_socket_dir = get_option('service-socket-dir')
+if service_socket_dir == ''
+  service_socket_dir = '/run/filesystems'
+endif
+private_cfg.set_quoted('FUSE_SERVICE_SOCKET_DIR', service_socket_dir)
 
 # Test for presence of some functions
 test_funcs = [ 'fork', 'fstatat', 'openat', 'readlinkat', 'pipe2',
@@ -191,6 +196,37 @@ if get_option('enable-io-uring') and liburing.found() and libnuma.found()
    endif
 endif
 
+# Check for systemd support
+systemd_system_unit_dir = get_option('systemdsystemunitdir')
+if systemd_system_unit_dir == ''
+  systemd = dependency('systemd', required: false)
+  if systemd.found()
+     systemd_system_unit_dir = systemd.get_variable(pkgconfig: 'systemd_system_unit_dir')
+  endif
+endif
+
+if systemd_system_unit_dir == ''
+  warning('could not determine systemdsystemunitdir, systemd stuff will not be installed')
+else
+  private_cfg.set_quoted('SYSTEMD_SYSTEM_UNIT_DIR', systemd_system_unit_dir)
+  private_cfg.set('HAVE_SYSTEMD', true)
+endif
+
+# Check for libc SCM_RIGHTS support (aka Linux)
+code = '''
+#include <sys/socket.h>
+int main(void) {
+    int moo = SCM_RIGHTS;
+    return moo;
+}'''
+if cc.links(code, name: 'libc SCM_RIGHTS support')
+  private_cfg.set('HAVE_SCM_RIGHTS', true)
+endif
+
+if private_cfg.get('HAVE_SCM_RIGHTS', false) and private_cfg.get('HAVE_SYSTEMD', false)
+  private_cfg.set('HAVE_SERVICEMOUNT', true)
+endif
+
 #
 # Compiler configuration
 #
meson_options.txt +6 -0
index c1f8fe69467184..95655a0d64895c 100644
@@ -27,3 +27,9 @@ option('enable-usdt', type : 'boolean', value : false,
 
 option('enable-io-uring', type: 'boolean', value: true,
        description: 'Enable fuse-over-io-uring support')
+
+option('service-socket-dir', type : 'string', value : '',
+       description: 'Where to install fuse server sockets (if empty, /run/filesystems)')
+
+option('systemdsystemunitdir', type : 'string', value : '',
+       description: 'Where to install systemd unit files (if empty, query pkg-config(1))')
util/fuservicemount.c new file +18 -0
new file mode 100644
index 00000000000000..860c512a34e30f
@@ -0,0 +1,18 @@
+/*
+  FUSE: Filesystem in Userspace
+  Copyright (C) 2025-2026 Oracle.
+  Author: Darrick J. Wong <[email protected]>
+
+  This program can be distributed under the terms of the GNU GPLv2.
+  See the file GPL2.txt.
+*/
+/* This program does the mounting of FUSE filesystems that run in systemd */
+
+#define _GNU_SOURCE
+#include "fuse_config.h"
+#include "mount_service.h"
+
+int main(int argc, char *argv[])
+{
+	return mount_service_main(argc, argv);
+}
util/meson.build +9 -0
index 0e4b1cce95377e..68d8bb11f92955 100644
@@ -6,6 +6,15 @@ executable('fusermount3', ['fusermount.c', '../lib/mount_util.c', '../lib/util.c
            install_dir: get_option('bindir'),
            c_args: '-DFUSE_CONF="@0@"'.format(fuseconf_path))
 
+if private_cfg.get('HAVE_SERVICEMOUNT', false)
+  executable('fuservicemount3', ['mount_service.c', 'fuservicemount.c'],
+             include_directories: include_dirs,
+             link_with: [ libfuse ],
+             install: true,
+             install_dir: get_option('sbindir'),
+             c_args: '-DFUSE_USE_VERSION=317')
+endif
+
 executable('mount.fuse3', ['mount.fuse.c'],
            include_directories: include_dirs,
            link_with: [ libfuse ],
util/mount_service.c new file +984 -0

984 lines not on this page · raw message

#146 lore raw

PATCH 2/5 libfuse: integrate fuse services into mount.fuse3

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Teach mount.fuse3 how to start fuse via service, if present.

---
 util/mount_service.h  |    9 ++++++++
 doc/fuservicemount3.8 |   10 ++++++++
 util/fuservicemount.c |   48 +++++++++++++++++++++++++++++++++++++++++
 util/meson.build      |    4 +++
 util/mount.fuse.c     |   58 +++++++++++++++++++++++++++++++------------------
 util/mount_service.c  |   18 +++++++++++++++
 6 files changed, 124 insertions(+), 23 deletions(-)
util/mount_service.h +9 -0
index 16e120da10765e..b211c7ede4e034 100644
@@ -29,4 +29,13 @@ int mount_service_main(int argc, char *argv[]);
  */
 const char *mount_service_subtype(const char *fstype);
 
+/**
+ * Discover if there is a fuse service socket for the given fuse subtype.
+ *
+ * @param subtype subtype of a fuse filesystem type (e.g. Y from
+ *                mount_service_subtype)
+ * @return true if available, false if not
+ */
+bool mount_service_present(const char *subtype);
+
 #endif /* MOUNT_SERVICE_H_ */
doc/fuservicemount3.8 +9 -1
index e45d6a89c8b81a..aa2167cb4872c6 100644
@@ -7,12 +7,20 @@ .SH SYNOPSIS
 .B mountpoint
 .BI -t " fstype"
 [
-.I options
+.BI -o " options"
 ]
+
+.B fuservicemount3
+.BI -t " fstype"
+.B --check
+
 .SH DESCRIPTION
 Mount a filesystem using a FUSE server that runs as a socket service.
 These servers can be contained using the platform's service management
 framework.
+
+The second form checks if there is a FUSE service available for the given
+filesystem type.
 .SH "AUTHORS"
 .LP
 The author of the fuse socket service code is Darrick J. Wong <[email protected]>.
util/fuservicemount.c +48 -0

58 lines not on this page · raw message

util/meson.build +3 -1
index 68d8bb11f92955..3adf395bfb6386 100644
@@ -6,7 +6,9 @@ executable('fusermount3', ['fusermount.c', '../lib/mount_util.c', '../lib/util.c
            install_dir: get_option('bindir'),
            c_args: '-DFUSE_CONF="@0@"'.format(fuseconf_path))
 
+mount_fuse3_sources = ['mount.fuse.c']
 if private_cfg.get('HAVE_SERVICEMOUNT', false)
+  mount_fuse3_sources += ['mount_service.c']
   executable('fuservicemount3', ['mount_service.c', 'fuservicemount.c'],
              include_directories: include_dirs,
              link_with: [ libfuse ],
@@ -15,7 +17,7 @@ if private_cfg.get('HAVE_SERVICEMOUNT', false)
              c_args: '-DFUSE_USE_VERSION=317')
 endif
 
-executable('mount.fuse3', ['mount.fuse.c'],
+executable('mount.fuse3', mount_fuse3_sources,
            include_directories: include_dirs,
            link_with: [ libfuse ],
            install: true,
util/mount.fuse.c +37 -21

82 lines not on this page · raw message

util/mount_service.c +18 -0
index 5d92ffb264e4bd..642d4a19c443c4 100644
@@ -982,3 +982,21 @@ int mount_service_main(int argc, char *argv[])
 	mount_service_destroy(&mo);
 	return ret;
 }
+
+bool mount_service_present(const char *fstype)
+{
+	struct stat stbuf;
+	char path[PATH_MAX];
+	int ret;
+
+	snprintf(path, sizeof(path), FUSE_SERVICE_SOCKET_DIR "/%s", fstype);
+	ret = stat(path, &stbuf);
+	if (ret)
+		return false;
+
+	if (!S_ISSOCK(stbuf.st_mode))
+		return false;
+
+	ret = access(path, R_OK | W_OK);
+	return ret == 0;
+}
#147 lore raw

PATCH 3/5 libfuse: delegate iomap privilege from mount.service to fuse services

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Enable the mount.service helper to attach whatever privileges it might have to enable iomap to a /dev/fuse fd before passing that fd to the fuse server. Assuming that the fuse service itself does not have sufficient privilege to enable iomap on its own, it can now inherit that privilege via the fd.

---
 include/fuse_kernel.h       |    1 +
 include/fuse_lowlevel.h     |   10 +++++++
 include/fuse_service.h      |   11 +++++++
 include/fuse_service_priv.h |   10 +++++++
 lib/fuse_lowlevel.c         |    5 +++
 lib/fuse_service.c          |   49 +++++++++++++++++++++++++++++++++
 lib/fuse_versionscript      |    2 +
 util/mount_service.c        |   64 +++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 152 insertions(+)
include/fuse_kernel.h +1 -0
index 8149657ac44cb9..417f1d42b0618d 100644
@@ -1193,6 +1193,7 @@ struct fuse_iomap_support {
 #define FUSE_DEV_IOC_IOMAP_SUPPORT	_IOR(FUSE_DEV_IOC_MAGIC, 99, \
 					     struct fuse_iomap_support)
 #define FUSE_DEV_IOC_SET_NOFS		_IOW(FUSE_DEV_IOC_MAGIC, 100, uint32_t)
+#define FUSE_DEV_IOC_ADD_IOMAP		_IO(FUSE_DEV_IOC_MAGIC, 101)
 
 struct fuse_lseek_in {
 	uint64_t	fh;
include/fuse_lowlevel.h +10 -0
index 77fc623386ce20..eefc69360b8aa8 100644
@@ -2799,6 +2799,16 @@ uint64_t fuse_lowlevel_discover_iomap(int fd);
  */
 int fuse_lowlevel_disable_fsreclaim(struct fuse_session *se, int val);
 
+/**
+ * Request that iomap capabilities be added to this fuse device.  This enables
+ * a privileged mount helper to convey the privileges that allow iomap usage to
+ * a completely unprivileged fuse server.
+ *
+ * @param fd open file descriptor to a fuse device
+ * @return 0 on success, -1 on failure with errno set
+ */
+int fuse_lowlevel_add_iomap(int fd);
+
 #ifdef __cplusplus
 }
 #endif
include/fuse_service.h +11 -0
index d3a3a4a4237380..a88173a0e4c227 100644
@@ -128,6 +128,17 @@ int fuse_service_receive_file(struct fuse_service *sf,
  */
 int fuse_service_finish_file_requests(struct fuse_service *sf);
 
+/**
+ * Attach iomap to the fuse connection.
+ *
+ * @param sf service context
+ * @param mandatory true if the server requires iomap
+ * @param error result of trying to enable iomap
+ * @return 0 on success, -1 on error
+ */
+int fuse_service_configure_iomap(struct fuse_service *sf, bool mandatory,
+				 int *error);
+
 /**
  * Ask the mount.service helper to mount the filesystem for us.  The fuse client
  * will begin sending requests to the fuse server immediately after this.
include/fuse_service_priv.h +10 -0
index 4df323097c2470..f755710863c766 100644
@@ -32,6 +32,7 @@ struct fuse_service_memfd_argv {
 #define FUSE_SERVICE_MNTPT_CMD		0x4d4e5450	/* MNTP */
 #define FUSE_SERVICE_MOUNT_CMD		0x444f4954	/* DOIT */
 #define FUSE_SERVICE_BYE_CMD		0x42594545	/* BYEE */
+#define FUSE_SERVICE_IOMAP_CMD		0x494f4d41	/* IOMA */
 
 /* mount.service sends replies to the fuse server */
 #define FUSE_SERVICE_OPEN_REPLY		0x46494c45	/* FILE */
@@ -87,6 +88,15 @@ static inline size_t sizeof_fuse_service_open_command(size_t pathlen)
 	return sizeof(struct fuse_service_open_command) + pathlen + 1;
 }
 
+#define FUSE_IOMAP_MODE_OPTIONAL	0x503F /* P? */
+#define FUSE_IOMAP_MODE_MANDATORY	0x5021 /* P! */
+
+struct fuse_service_iomap_command {
+	struct fuse_service_packet p;
+	__be16 mode;
+	__be16 padding;
+};
+
 struct fuse_service_string_command {
 	struct fuse_service_packet p;
 	char value[];
lib/fuse_lowlevel.c +5 -0
index 714c26385c044f..4f27603400f2da 100644
@@ -5349,3 +5349,8 @@ int fuse_lowlevel_disable_fsreclaim(struct fuse_session *se, int val)
 {
 	return ioctl(se->fd, FUSE_DEV_IOC_SET_NOFS, &val);
 }
+
+int fuse_lowlevel_add_iomap(int fd)
+{
+	return ioctl(fd, FUSE_DEV_IOC_ADD_IOMAP);
+}
lib/fuse_service.c +49 -0

55 lines not on this page · raw message

lib/fuse_versionscript +2 -0
index 935eaec820c7a7..334a0f7cf3a096 100644
@@ -254,8 +254,10 @@ FUSE_3.99 {
 		fuse_lowlevel_iomap_inval_mappings;
 		fuse_fs_iomap_upsert;
 		fuse_fs_iomap_inval;
+		fuse_lowlevel_add_iomap;
 		fuse_service_accept;
 		fuse_service_append_args;
+		fuse_service_configure_iomap;
 		fuse_service_destroy;
 		fuse_service_finish_file_requests;
 		fuse_service_mount;
util/mount_service.c +64 -0

94 lines not on this page · raw message

#148 lore raw

PATCH 4/5 libfuse: enable setting iomap block device block size

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Create a means for an unprivileged fuse server to set the block size of a block device that it previously opened and associated with the fuse connection.

---
 include/fuse_kernel.h   |    7 +++++++
 include/fuse_lowlevel.h |   12 ++++++++++++
 lib/fuse_lowlevel.c     |   11 +++++++++++
 lib/fuse_versionscript  |    1 +
 4 files changed, 31 insertions(+)
include/fuse_kernel.h +7 -0
index 417f1d42b0618d..d9b65fe2395bde 100644
@@ -1184,6 +1184,11 @@ struct fuse_iomap_support {
 	uint64_t	padding;
 };
 
+struct fuse_iomap_backing_info {
+	uint32_t	backing_id;
+	uint32_t	blocksize;
+};
+
 /* Device ioctls: */
 #define FUSE_DEV_IOC_MAGIC		229
 #define FUSE_DEV_IOC_CLONE		_IOR(FUSE_DEV_IOC_MAGIC, 0, uint32_t)
@@ -1194,6 +1199,8 @@ struct fuse_iomap_support {
 					     struct fuse_iomap_support)
 #define FUSE_DEV_IOC_SET_NOFS		_IOW(FUSE_DEV_IOC_MAGIC, 100, uint32_t)
 #define FUSE_DEV_IOC_ADD_IOMAP		_IO(FUSE_DEV_IOC_MAGIC, 101)
+#define FUSE_DEV_IOC_IOMAP_SET_BLOCKSIZE _IOW(FUSE_DEV_IOC_MAGIC, 102, \
+					      struct fuse_iomap_backing_info)
 
 struct fuse_lseek_in {
 	uint64_t	fh;
include/fuse_lowlevel.h +12 -0
index eefc69360b8aa8..f0dba1918f09b6 100644
@@ -2809,6 +2809,18 @@ int fuse_lowlevel_disable_fsreclaim(struct fuse_session *se, int val);
  */
 int fuse_lowlevel_add_iomap(int fd);
 
+/**
+ * Set the block size of an open block device that has been opened for use with
+ * iomap.
+ *
+ * @param fd open file descriptor to a fuse device
+ * @param dev_index device index returned by fuse_lowlevel_iomap_device_add
+ * @param blocksize block size in bytes
+ * @return 0 on success, -1 on failure with errno set
+ */
+int fuse_lowlevel_iomap_set_blocksize(int fd, int dev_index,
+				      unsigned int blocksize);
+
 #ifdef __cplusplus
 }
 #endif
lib/fuse_lowlevel.c +11 -0
index 4f27603400f2da..1549b1d339e24b 100644
@@ -5354,3 +5354,14 @@ int fuse_lowlevel_add_iomap(int fd)
 {
 	return ioctl(fd, FUSE_DEV_IOC_ADD_IOMAP);
 }
+
+int fuse_lowlevel_iomap_set_blocksize(int fd, int dev_index,
+				      unsigned int blocksize)
+{
+	struct fuse_iomap_backing_info fbi = {
+		.backing_id = dev_index,
+		.blocksize = blocksize,
+	};
+
+	return ioctl(fd, FUSE_DEV_IOC_IOMAP_SET_BLOCKSIZE, &fbi);
+}
lib/fuse_versionscript +1 -0
index 334a0f7cf3a096..5f1a50701a9ba9 100644
@@ -268,6 +268,7 @@ FUSE_3.99 {
 		fuse_service_request_file;
 		fuse_service_send_goodbye;
 		fuse_service_take_fusedev;
+		fuse_lowlevel_iomap_set_blocksize;
 } FUSE_3.19;
 
 # Local Variables:
#149 lore raw

PATCH 5/5 fuservicemount: create loop devices for regular files

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

If a fuse server asks fuservicemount to open a regular file, try to create an auto-clear loop device so that the fuse server can use iomap.

---
 include/fuse_service.h      |    6 ++++++
 include/fuse_service_priv.h |    3 ++-
 lib/fuse_service.c          |    5 ++++-
 util/mount_service.c        |   34 ++++++++++++++++++++++++++++++++++
 4 files changed, 46 insertions(+), 2 deletions(-)
include/fuse_service.h +6 -0
index a88173a0e4c227..035ea57873e536 100644
@@ -95,6 +95,12 @@ int fuse_service_take_fusedev(struct fuse_service *sfp);
 int fuse_service_parse_cmdline_opts(struct fuse_args *args,
 				    struct fuse_cmdline_opts *opts);
 
+/**
+ * If the file opened is a regular file, try to create a loop device for it.
+ * If successful, the loop device is returned; if not, the regular file is.
+ */
+#define FUSE_SERVICE_REQUEST_FILE_TRYLOOP	(1U << 0)
+
 /**
  * Ask the mount.service helper to open a file on behalf of the fuse server.
  *
include/fuse_service_priv.h +2 -1
index f755710863c766..e8b6da260b089e 100644
@@ -73,7 +73,8 @@ static inline size_t sizeof_fuse_service_requested_file(size_t pathlen)
 	return sizeof(struct fuse_service_requested_file) + pathlen + 1;
 }
 
-#define FUSE_SERVICE_OPEN_FLAGS		(0)
+#define FUSE_SERVICE_OPEN_TRYLOOP	(1U << 0)
+#define FUSE_SERVICE_OPEN_FLAGS		(FUSE_SERVICE_OPEN_TRYLOOP)
 
 struct fuse_service_open_command {
 	struct fuse_service_packet p;
lib/fuse_service.c +4 -1
index a9fce8b022c005..bcab6913809dfd 100644
@@ -152,7 +152,7 @@ int fuse_service_receive_file(struct fuse_service *sf, const char *path,
 	return recv_requested_file(sf->sockfd, path, fdp);
 }
 
-#define FUSE_SERVICE_REQUEST_FILE_FLAGS	(0)
+#define FUSE_SERVICE_REQUEST_FILE_FLAGS	(FUSE_SERVICE_REQUEST_FILE_TRYLOOP)
 
 int fuse_service_request_file(struct fuse_service *sf, const char *path,
 			      int open_flags, mode_t create_mode,
@@ -177,6 +177,9 @@ int fuse_service_request_file(struct fuse_service *sf, const char *path,
 		return -1;
 	}
 
+	if (request_flags & FUSE_SERVICE_REQUEST_FILE_TRYLOOP)
+		rqflags |= FUSE_SERVICE_OPEN_TRYLOOP;
+
 	cmd = calloc(1, iov.iov_len);
 	if (!cmd) {
 		perror("fuse: alloc service file request");
util/mount_service.c +34 -0
index 241f47822deaa6..f1e2fc4c52092d 100644
@@ -25,15 +25,20 @@
 #include <limits.h>
 #include <sys/stat.h>
 #include <arpa/inet.h>
+#ifdef HAVE_STRUCT_LOOP_CONFIG_INFO
+# include <linux/loop.h>
+#endif
 
 #include "mount_util.h"
 #include "util.h"
 #include "fuse_i.h"
 #include "fuse_service_priv.h"
+#include "fuse_loopdev.h"
 #include "mount_service.h"
 
 #define FUSE_KERN_DEVICE_ENV	"FUSE_KERN_DEVICE"
 #define FUSE_DEV		"/dev/fuse"
+#define LOOPCTL			"/dev/loop-control"
 
 struct mount_service {
 	/* alleged fuse subtype based on -t cli argument */
@@ -556,6 +561,35 @@ static int mount_service_handle_open_cmd(struct mount_service *mo,
 		return mount_service_send_file_error(mo, error, oc->path);
 	}
 
+	if (request_flags & FUSE_SERVICE_OPEN_TRYLOOP) {
+		int loop_fd = -1;
+
+		ret = fuse_loopdev_setup(fd, ntohl(oc->open_flags), oc->path,
+					 5, &loop_fd, NULL);
+		if (ret) {
+			/*
+			 * If the setup function returned EBUSY, there is
+			 * already a loop device backed by this file, so we
+			 * must return an error.  For any other type of error
+			 * we'll send back the first file we opened.
+			 */
+			if (errno == EBUSY) {
+				ret = mount_service_send_file_error(mo, errno,
+						oc->path);
+				close(fd);
+				return ret;
+			}
+		} else if (loop_fd >= 0) {
+			/*
+			 * Send back the loop device instead of the file.
+			 */
+			ret = mount_service_send_file(mo, oc->path, loop_fd);
+			close(loop_fd);
+			close(fd);
+			return ret;
+		}
+	}
+
 	ret = mount_service_send_file(mo, oc->path, fd);
 	close(fd);
 	return ret;
#150 lore raw

RFC PATCH 6/6 fuse: allow fuse servers to upload iomap BPF programs

In reply to Darrick J. Wong

Hi all,

There are certain fuse servers that might benefit from the ability to upload a BPF program into the kernel to respond to ->iomap_begin requests instead of upcalling the fuse server itself.

For example, consider a fuse server that abstracts a large amount of storage for use as intermediate storage by programs. If the storage is striped across hardware devices (e.g. RAID0 or interleaved memory controllers) then the iomapping pattern will be completely regular but the mappings themselves might be very small.

Performance for large IOs will suck if it is necessary to upcall the fuse server every time we cross a mapping boundary. The fuse server can try to mitigate that hit by upserting mappings ahead of time, but there's a better solution for this usecase: BPF programs.

In this case, the fuse server can compile a BPF program that will compute the mapping data for a given request and upload the program. This avoids the overhead of cache lookups and server upcalls. Note that the BPF verifier still imposes instruction count and complexity limits on the uploaded programs.

Note that I embraced and extended some code from Joanne, but at this point I've modified it so heavily that it's not really the original anymore. But she still gets credit for coming up with the idea and engaging me in flinging prototypes around.

If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.

With a bit of luck, this should all go splendidly. Comments and questions are, as always, welcome.

--D

kernel git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-bpf
---
Commits in this patchset:
* libfuse: allow fuse servers to upload bpf code for iomap functions
* libfuse: add kfuncs for iomap bpf programs to manage the cache
* libfuse: make fuse_inode opaque to iomap bpf programs

---
 include/fuse_iomap_bpf.h |  263 ++++++++++++++++++++++++++++++++++++++++++++++
 include/meson.build      |    3 -
 2 files changed, 265 insertions(+), 1 deletion(-)
 create mode 100644 include/fuse_iomap_bpf.h
#151 lore raw

PATCH 1/3 libfuse: allow fuse servers to upload bpf code for iomap functions

From: Darrick J. Wong <[email protected]>

Create the necessary header files to enable lowlevel fuse servers to compile their own BPF blobs that override iomap functions, and a bunch of meson code to detect if the system is even capable of compiling BPF.

---
 include/fuse_iomap_bpf.h |  241 ++++++++++++++++++++++++++++++++++++++++++++++
 include/meson.build      |    3 -
 2 files changed, 243 insertions(+), 1 deletion(-)
 create mode 100644 include/fuse_iomap_bpf.h
include/fuse_iomap_bpf.h new file +241 -0
new file mode 100644
index 00000000000000..3e69fb33cbdd7c
@@ -0,0 +1,241 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2026 Oracle.  All Rights Reserved.
+ * Author: Darrick J. Wong <[email protected]>
+ * Copied from: Joanne Koong <[email protected]>
+ */
+#ifndef FUSE_IOMAP_BPF_H_
+#define FUSE_IOMAP_BPF_H_
+
+/* Note: You probably want to #include bpf_helpers and bpf_tracing.h */
+
+/*
+ * XXX: Avoid including fuse_lowlevel.h and fuse_kernel.h because their use of
+ * uint64_t (instead of u64 like the kernel requires for uapi) is extremely
+ * problematic due to the kernel and glibc not agreeing on the exact type
+ * definitions.  BPF is considered a separate architecture from the host
+ * machine, which complicates things further.
+ */
+
+/* mapping types; see corresponding IOMAP_TYPE_ */
+#define FUSE_IOMAP_TYPE_HOLE		(0)
+#define FUSE_IOMAP_TYPE_DELALLOC	(1)
+#define FUSE_IOMAP_TYPE_MAPPED		(2)
+#define FUSE_IOMAP_TYPE_UNWRITTEN	(3)
+#define FUSE_IOMAP_TYPE_INLINE		(4)
+
+/* fuse-specific mapping type indicating that writes use the read mapping */
+#define FUSE_IOMAP_TYPE_PURE_OVERWRITE	(255)
+/* fuse-specific mapping type saying the server has populated the cache */
+#define FUSE_IOMAP_TYPE_RETRY_CACHE	(254)
+/* do not upsert this mapping */
+#define FUSE_IOMAP_TYPE_NOCACHE		(253)
+
+#define FUSE_IOMAP_DEV_NULL		(0U)	/* null device cookie */
+
+/* mapping flags passed back from iomap_begin; see corresponding IOMAP_F_ */
+#define FUSE_IOMAP_F_NEW		(1U << 0)
+#define FUSE_IOMAP_F_DIRTY		(1U << 1)
+#define FUSE_IOMAP_F_SHARED		(1U << 2)
+#define FUSE_IOMAP_F_MERGED		(1U << 3)
+#define FUSE_IOMAP_F_BOUNDARY		(1U << 4)
+#define FUSE_IOMAP_F_ANON_WRITE		(1U << 5)
+#define FUSE_IOMAP_F_ATOMIC_BIO		(1U << 6)
+
+/* fuse-specific mapping flag asking for ->iomap_end call */
+#define FUSE_IOMAP_F_WANT_IOMAP_END	(1U << 7)
+
+/* mapping flags passed to iomap_end */
+#define FUSE_IOMAP_F_SIZE_CHANGED	(1U << 8)
+#define FUSE_IOMAP_F_STALE		(1U << 9)
+
+/* operation flags from iomap; see corresponding IOMAP_* */
+#define FUSE_IOMAP_OP_WRITE		(1U << 0)
+#define FUSE_IOMAP_OP_ZERO		(1U << 1)
+#define FUSE_IOMAP_OP_REPORT		(1U << 2)
+#define FUSE_IOMAP_OP_FAULT		(1U << 3)
+#define FUSE_IOMAP_OP_DIRECT		(1U << 4)
+#define FUSE_IOMAP_OP_NOWAIT		(1U << 5)
+#define FUSE_IOMAP_OP_OVERWRITE_ONLY	(1U << 6)
+#define FUSE_IOMAP_OP_UNSHARE		(1U << 7)
+#define FUSE_IOMAP_OP_DAX		(1U << 8)
+#define FUSE_IOMAP_OP_ATOMIC		(1U << 9)
+#define FUSE_IOMAP_OP_DONTCACHE		(1U << 10)
+
+/* swapfile config operation */
+#define FUSE_IOMAP_OP_SWAPFILE		(1U << 30)
+
+/* pagecache writeback operation */
+#define FUSE_IOMAP_OP_WRITEBACK		(1U << 31)
+
+#define FUSE_IOMAP_NULL_ADDR		(-1ULL)	/* addr is not valid */
+
+struct fuse_iomap_io {
+	uint64_t offset;	/* file offset of mapping, bytes */
+	uint64_t length;	/* length of mapping, bytes */
+	uint64_t addr;		/* disk offset of mapping, bytes */
+	uint16_t type;		/* FUSE_IOMAP_TYPE_* */
+	uint16_t flags;		/* FUSE_IOMAP_F_* */
+	uint32_t dev;		/* device cookie */
+};
+
+struct fuse_iomap_begin_in {
+	uint32_t opflags;	/* FUSE_IOMAP_OP_* */
+	uint32_t reserved;	/* zero */
+	uint64_t attr_ino;	/* matches fuse_attr:ino */
+	uint64_t pos;		/* file position, in bytes */
+	uint64_t count;		/* operation length, in bytes */
+};
+
+struct fuse_iomap_begin_out {
+	/* read file data from here */
+	struct fuse_iomap_io	read;
+
+	/* write file data to here, if applicable */
+	struct fuse_iomap_io	write;
+};
+
+struct fuse_iomap_end_in {
+	uint32_t opflags;	/* FUSE_IOMAP_OP_* */
+	uint32_t reserved;	/* zero */
+	uint64_t attr_ino;	/* matches fuse_attr:ino */
+	uint64_t pos;		/* file position, in bytes */
+	uint64_t count;		/* operation length, in bytes */
+	int64_t written;	/* bytes processed */
+
+	/* mapping that the kernel acted upon */
+	struct fuse_iomap_io	map;
+};
+
+/* out of place write extent */
+#define FUSE_IOMAP_IOEND_SHARED		(1U << 0)
+/* unwritten extent */
+#define FUSE_IOMAP_IOEND_UNWRITTEN	(1U << 1)
+/* don't merge into previous ioend */
+#define FUSE_IOMAP_IOEND_BOUNDARY	(1U << 2)
+/* is direct I/O */
+#define FUSE_IOMAP_IOEND_DIRECT		(1U << 3)
+/* is append ioend */
+#define FUSE_IOMAP_IOEND_APPEND		(1U << 4)
+/* is pagecache writeback */
+#define FUSE_IOMAP_IOEND_WRITEBACK	(1U << 5)
+/* swapfile deactivation */
+#define FUSE_IOMAP_IOEND_SWAPOFF	(1U << 6)
+
+struct fuse_iomap_ioend_in {
+	uint32_t flags;		/* FUSE_IOMAP_IOEND_* */
+	int32_t error;		/* negative errno or 0 */
+	uint64_t attr_ino;	/* matches fuse_attr:ino */
+	uint64_t pos;		/* file position, in bytes */
+	uint64_t new_addr;	/* disk offset of new mapping, in bytes */
+	uint64_t written;	/* bytes processed */
+	uint32_t dev;		/* device cookie */
+	uint32_t pad;		/* zero */
+};
+
+struct fuse_iomap_ioend_out {
+	uint64_t newsize;	/* new ondisk size */
+};
+
+/* Signal that reads and writes go to the same storage. */
+static inline void
+fuse_iomap_begin_pure_overwrite(struct fuse_iomap_begin_out *outarg)
+{
+	outarg->write.addr = FUSE_IOMAP_NULL_ADDR;
+	outarg->write.offset = outarg->read.offset;
+	outarg->write.length = outarg->read.length;
+	outarg->write.type = FUSE_IOMAP_TYPE_PURE_OVERWRITE;
+	outarg->write.flags = 0;
+	outarg->write.dev = FUSE_IOMAP_DEV_NULL;
+}
+
+enum fuse_iomap_bpf_ret {
+	/* fall back to fuse server upcall */
+	FIB_FALLBACK = 0,
+	/* bpf function handled event completely */
+	FIB_HANDLED = 1,
+};
+
+struct fuse_iomap_bpf_ops {
+	/**
+	 * @iomap_begin: override iomap_begin.  See FUSE_IOMAP_BEGIN for
+	 * details.
+	 */
+	enum fuse_iomap_bpf_ret (*iomap_begin)(struct fuse_inode *fi,
+			uint64_t pos, uint64_t count, uint32_t opflags,
+			struct fuse_iomap_begin_out *outarg);
+
+	/**
+	 * @iomap_end: override iomap_end.  See FUSE_IOMAP_END for
+	 * details.
+	 */
+	enum fuse_iomap_bpf_ret (*iomap_end)(struct fuse_inode *fi,
+			uint64_t pos, uint64_t count, int64_t written,
+			uint32_t opflags);
+
+	/**
+	 * @iomap_ioend: override iomap_ioend.  See FUSE_IOMAP_IOEND for
+	 * details.
+	 */
+	enum fuse_iomap_bpf_ret (*iomap_ioend)(struct fuse_inode *fi,
+			uint64_t pos, int64_t written, uint32_t ioendflags,
+			int error, uint32_t dev, uint64_t new_addr,
+			struct fuse_iomap_ioend_out *outarg);
+
+	/**
+	 * @fuse_fd: file descriptor of the open fuse device
+	 */
+	int fuse_fd;
+
+	/**
+	 * @zeropad: Explicitly pad to zero.
+	 */
+	unsigned int zeropad;
+
+	/**
+	 * @name: string describing the fuse iomap bpf operations
+	 */
+	char name[16];
+};
+
+/* BPF code must be GPLv2 */
+#define DECLARE_GPL2_LICENSE_FOR_FUSE_IOMAP_BPF \
+	char LICENSE[] SEC("license") = "GPL v2";
+
+/* Macros to handle creating iomap_ops override functions */
+#define FUSE_IOMAP_BEGIN_BPF_FUNC(name) \
+SEC("struct_ops/iomap_begin") \
+enum fuse_iomap_bpf_ret BPF_PROG(name, struct fuse_inode *fi, \
+		uint64_t pos, uint64_t count, uint32_t opflags, \
+		struct fuse_iomap_begin_out *outarg)
+
+#define FUSE_IOMAP_END_BPF_FUNC(name) \
+SEC("struct_ops/iomap_end") \
+enum fuse_iomap_bpf_ret BPF_PROG(name, struct fuse_inode *fi, \
+		uint64_t pos, uint64_t count, int64_t written, \
+		uint32_t opflags)
+
+#define FUSE_IOMAP_IOEND_BPF_FUNC(name) \
+SEC("struct_ops/iomap_ioend") \
+enum fuse_iomap_bpf_ret BPF_PROG(name, struct fuse_inode *fi, \
+		uint64_t pos, int64_t written, uint32_t ioendflags, int error, \
+		uint32_t dev, uint64_t new_addr, \
+		struct fuse_iomap_ioend_out *outarg)
+
+/*
+ * XXX: The BPF compiler plays some weird shenanigans with argument passing
+ * conventions, which means that the actual function signature of BPF_PROG
+ * functions does not actually match the C definitions.  Hence the horrible
+ * (void *) casts below.
+ */
+#define DEFINE_FUSE_IOMAP_BPF_OPS(ops_name, fancy_name, begin_fn, end_fn, \
+				  ioend_fn) \
+SEC(".struct_ops.link") \
+struct fuse_iomap_bpf_ops ops_name = { \
+	.iomap_begin	= (void *)begin_fn, \
+	.iomap_end	= (void *)end_fn, \
+	.iomap_ioend	= (void *)ioend_fn, \
+	.name		= fancy_name, \
+}
+
+#endif /* FUSE_IOMAP_BPF_H_ */
include/meson.build +2 -1
index 5ab4ecf052bf56..17f03df560475a 100644
@@ -1,5 +1,6 @@
 libfuse_headers = [ 'fuse.h', 'fuse_common.h', 'fuse_lowlevel.h',
-	            'fuse_opt.h', 'cuse_lowlevel.h', 'fuse_log.h' ]
+	            'fuse_opt.h', 'cuse_lowlevel.h', 'fuse_log.h',
+                    'fuse_iomap_bpf.h' ]
 
 if private_cfg.get('FUSE_LOOPDEV_ENABLED')
   libfuse_headers += [ 'fuse_loopdev.h' ]
#152 lore raw

PATCH 2/3 libfuse: add kfuncs for iomap bpf programs to manage the cache

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Add a couple of kfuncs so that a BPF program that generates iomappings can add them to the inode's mapping cache, thereby avoiding the need to go into BPF program on the next access.

---
 include/fuse_iomap_bpf.h |   28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)
include/fuse_iomap_bpf.h +22 -6
index 3e69fb33cbdd7c..1ff8f1831e12f6 100644
@@ -149,6 +149,14 @@ fuse_iomap_begin_pure_overwrite(struct fuse_iomap_begin_out *outarg)
 	outarg->write.dev = FUSE_IOMAP_DEV_NULL;
 }
 
+struct fuse_range {
+	uint64_t offset;
+	uint64_t length;
+};
+
+/* invalidate all cached iomap mappings up to EOF */
+#define FUSE_IOMAP_INVAL_TO_EOF		(~0ULL)
+
 enum fuse_iomap_bpf_ret {
 	/* fall back to fuse server upcall */
 	FIB_FALLBACK = 0,
@@ -204,20 +212,20 @@ struct fuse_iomap_bpf_ops {
 
 /* Macros to handle creating iomap_ops override functions */
 #define FUSE_IOMAP_BEGIN_BPF_FUNC(name) \
-SEC("struct_ops/iomap_begin") \
-enum fuse_iomap_bpf_ret BPF_PROG(name, struct fuse_inode *fi, \
+SEC("struct_ops.s/iomap_begin") \
+enum fuse_iomap_bpf_ret BPF_PROG(name, struct struct fuse_inode *fi, \
 		uint64_t pos, uint64_t count, uint32_t opflags, \
 		struct fuse_iomap_begin_out *outarg)
 
 #define FUSE_IOMAP_END_BPF_FUNC(name) \
-SEC("struct_ops/iomap_end") \
-enum fuse_iomap_bpf_ret BPF_PROG(name, struct fuse_inode *fi, \
+SEC("struct_ops.s/iomap_end") \
+enum fuse_iomap_bpf_ret BPF_PROG(name, struct struct fuse_inode *fi, \
 		uint64_t pos, uint64_t count, int64_t written, \
 		uint32_t opflags)
 
 #define FUSE_IOMAP_IOEND_BPF_FUNC(name) \
-SEC("struct_ops/iomap_ioend") \
-enum fuse_iomap_bpf_ret BPF_PROG(name, struct fuse_inode *fi, \
+SEC("struct_ops.s/iomap_ioend") \
+enum fuse_iomap_bpf_ret BPF_PROG(name, struct struct fuse_inode *fi, \
 		uint64_t pos, int64_t written, uint32_t ioendflags, int error, \
 		uint32_t dev, uint64_t new_addr, \
 		struct fuse_iomap_ioend_out *outarg)
@@ -238,4 +246,12 @@ struct fuse_iomap_bpf_ops ops_name = { \
 	.name		= fancy_name, \
 }
 
+/* kfuncs; these require __ksym to link correctly! */
+int __ksym fuse_bpf_iomap_inval_mappings(struct fuse_inode *fi,
+		const struct fuse_range *read__nullable,
+		const struct fuse_range *write__nullable);
+int __ksym fuse_bpf_iomap_upsert_mappings(struct fuse_inode *fi,
+		const struct fuse_iomap_io *read__nullable,
+		const struct fuse_iomap_io *write__nullable);
+
 #endif /* FUSE_IOMAP_BPF_H_ */
#153 lore raw

PATCH 3/3 libfuse: make fuse_inode opaque to iomap bpf programs

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Introduce an opaque type (and some casting helpers) for struct fuse_inode. This tricks BTF/BPF into thinking that the "inode" object we pass to BPF programs is an empty structure, which means that the BPF program cannot even look at the contents.

---
 include/fuse_iomap_bpf.h |   22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)
include/fuse_iomap_bpf.h +14 -8
index 1ff8f1831e12f6..ec2c3a1e5655af 100644
@@ -164,12 +164,15 @@ enum fuse_iomap_bpf_ret {
 	FIB_HANDLED = 1,
 };
 
+/* opaque structure for calling bpf kfuncs */
+struct fuse_bpf_inode { };
+
 struct fuse_iomap_bpf_ops {
 	/**
 	 * @iomap_begin: override iomap_begin.  See FUSE_IOMAP_BEGIN for
 	 * details.
 	 */
-	enum fuse_iomap_bpf_ret (*iomap_begin)(struct fuse_inode *fi,
+	enum fuse_iomap_bpf_ret (*iomap_begin)(struct fuse_bpf_inode *fbi,
 			uint64_t pos, uint64_t count, uint32_t opflags,
 			struct fuse_iomap_begin_out *outarg);
 
@@ -177,7 +180,7 @@ struct fuse_iomap_bpf_ops {
 	 * @iomap_end: override iomap_end.  See FUSE_IOMAP_END for
 	 * details.
 	 */
-	enum fuse_iomap_bpf_ret (*iomap_end)(struct fuse_inode *fi,
+	enum fuse_iomap_bpf_ret (*iomap_end)(struct fuse_bpf_inode *fbi,
 			uint64_t pos, uint64_t count, int64_t written,
 			uint32_t opflags);
 
@@ -185,7 +188,7 @@ struct fuse_iomap_bpf_ops {
 	 * @iomap_ioend: override iomap_ioend.  See FUSE_IOMAP_IOEND for
 	 * details.
 	 */
-	enum fuse_iomap_bpf_ret (*iomap_ioend)(struct fuse_inode *fi,
+	enum fuse_iomap_bpf_ret (*iomap_ioend)(struct fuse_bpf_inode *fbi,
 			uint64_t pos, int64_t written, uint32_t ioendflags,
 			int error, uint32_t dev, uint64_t new_addr,
 			struct fuse_iomap_ioend_out *outarg);
@@ -213,19 +216,19 @@ struct fuse_iomap_bpf_ops {
 /* Macros to handle creating iomap_ops override functions */
 #define FUSE_IOMAP_BEGIN_BPF_FUNC(name) \
 SEC("struct_ops.s/iomap_begin") \
-enum fuse_iomap_bpf_ret BPF_PROG(name, struct struct fuse_inode *fi, \
+enum fuse_iomap_bpf_ret BPF_PROG(name, struct fuse_bpf_inode *fbi, \
 		uint64_t pos, uint64_t count, uint32_t opflags, \
 		struct fuse_iomap_begin_out *outarg)
 
 #define FUSE_IOMAP_END_BPF_FUNC(name) \
 SEC("struct_ops.s/iomap_end") \
-enum fuse_iomap_bpf_ret BPF_PROG(name, struct struct fuse_inode *fi, \
+enum fuse_iomap_bpf_ret BPF_PROG(name, struct fuse_bpf_inode *fbi, \
 		uint64_t pos, uint64_t count, int64_t written, \
 		uint32_t opflags)
 
 #define FUSE_IOMAP_IOEND_BPF_FUNC(name) \
 SEC("struct_ops.s/iomap_ioend") \
-enum fuse_iomap_bpf_ret BPF_PROG(name, struct struct fuse_inode *fi, \
+enum fuse_iomap_bpf_ret BPF_PROG(name, struct fuse_bpf_inode *fbi, \
 		uint64_t pos, int64_t written, uint32_t ioendflags, int error, \
 		uint32_t dev, uint64_t new_addr, \
 		struct fuse_iomap_ioend_out *outarg)
@@ -247,11 +250,14 @@ struct fuse_iomap_bpf_ops ops_name = { \
 }
 
 /* kfuncs; these require __ksym to link correctly! */
-int __ksym fuse_bpf_iomap_inval_mappings(struct fuse_inode *fi,
+int __ksym fuse_bpf_iomap_inval_mappings(struct fuse_bpf_inode *fbi,
 		const struct fuse_range *read__nullable,
 		const struct fuse_range *write__nullable);
-int __ksym fuse_bpf_iomap_upsert_mappings(struct fuse_inode *fi,
+int __ksym fuse_bpf_iomap_upsert_mappings(struct fuse_bpf_inode *fbi,
 		const struct fuse_iomap_io *read__nullable,
 		const struct fuse_iomap_io *write__nullable);
 
+uint64_t __ksym fuse_bpf_inode_nodeid(const struct fuse_bpf_inode *fbi);
+uint64_t __ksym fuse_bpf_inode_orig_ino(const struct fuse_bpf_inode *fbi);
+
 #endif /* FUSE_IOMAP_BPF_H_ */
#154 lore raw

PATCH v7 1/8 fuse2fs: use fuse iomap data paths for better file I/O performance

In reply to Darrick J. Wong

Hi all,

Switch fuse2fs to use the new iomap file data IO paths instead of pushing it very slowly through the /dev/fuse connection. For local filesystems, all we have to do is respond to requests for file to device mappings; the rest of the IO hot path stays within the kernel. This means that we can get rid of all file data block processing within fuse2fs.

Because we're not pinning dirty pages through a potentially slow network connection, we don't need the heavy BDI throttling for which most fuse servers have become infamous. Yes, mapping lookups for writeback can stall, but mappings are small as compared to data and this situation exists for all kernel filesystems as well.

The performance of this new data path is quite stunning: on a warm system, streaming reads and writes through the pagecache go from 60-90MB/s to 2-2.5GB/s. Direct IO reads and writes improve from the same baseline to 2.5-8GB/s. FIEMAP and SEEK_DATA/SEEK_HOLE now work too. The kernel ext4 driver can manage about 1.6GB/s for pagecache IO and about 2.6-8.5GB/s, which means that fuse2fs is about as fast as the kernel for streaming file IO.

Random 4k buffered IO is not so good: plain fuse2fs pokes along at 25-50MB/s, whereas fuse2fs with iomap manages 90-1300MB/s. The kernel can do 900-1300MB/s. Random directio is worse: plain fuse2fs does 20-30MB/s, fuse-iomap does about 30-35MB/s, and the kernel does 40-55MB/s. I suspect that metadata heavy workloads do not perform well on fuse2fs because libext2fs wasn't designed for that and it doesn't even have a journal to absorb all the fsync writes. We also probably need iomap caching really badly.

These performance numbers are slanted: my machine is 12 years old, and fuse2fs is VERY poorly optimized for performance. It contains a single Big Filesystem Lock which nukes multi-threaded scalability. There's no inode cache nor is there a proper buffer cache, which means that fuse2fs reads metadata in from disk and checksums it on EVERY ACCESS. Sad!

Despite these gaps, this RFC demonstrates that it's feasible to run the metadata parsing parts of a filesystem in userspace while not sacrificing much performance. We now have a vehicle to move the filesystems out of the kernel, where they can be containerized so that malicious filesystems can be contained, somewhat.

iomap mode also calls FUSE_DESTROY before unmounting the filesystem, so for capable systems, fuse2fs doesn't need to run in fuseblk mode anymore.

However, there are some major warts remaining:

1. The iomap cookie validation is not present, which can lead to subtle races between pagecache zeroing and writeback on filesystems that support unwritten and delalloc mappings.

2. Mappings ought to be cached in the kernel for more speed.

3. iomap doesn't support things like fscrypt or fsverity, and I haven't yet figured out how inline data is supposed to work.

4. I would like to be able to turn on fuse+iomap on a per-inode basis, which currently isn't possible because the kernel fuse driver will iget inodes prior to calling FUSE_GETATTR to discover the properties of the inode it just read.

5. ext4 doesn't support out of place writes so I don't know if that actually works correctly.

6. iomap is an inode-based service, not a file-based service. This means that we /must/ push ext2's inode numbers into the kernel via FUSE_GETATTR so that it can report those same numbers back out through the FUSE_IOMAP_* calls. However, the fuse kernel uses a separate nodeid to index its incore inode, so we have to pass those too so that notifications work properly.

If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.

Comments and questions are, as always, welcome.

e2fsprogs git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/e2fsprogs.git/log/?h=fuse2fs-iomap-fileio
---
Commits in this patchset:
* fuse2fs: implement bare minimum iomap for file mapping reporting
* fuse2fs: add iomap= mount option
* fuse2fs: implement iomap configuration
* fuse2fs: register block devices for use with iomap
* fuse2fs: implement directio file reads
* fuse2fs: add extent dump function for debugging
* fuse2fs: implement direct write support
* fuse2fs: turn on iomap for pagecache IO
* fuse2fs: don't zero bytes in punch hole
* fuse2fs: don't do file data block IO when iomap is enabled
* fuse2fs: try to create loop device when ext4 device is a regular file
* fuse2fs: enable file IO to inline data files
* fuse2fs: set iomap-related inode flags
* fuse2fs: configure block device block size
* fuse4fs: separate invalidation
* fuse2fs: implement statx
* fuse2fs: enable atomic writes
* fuse4fs: disable fs reclaim and write throttling
* fuse2fs: implement freeze and shutdown requests

---
 configure            |   90 ++
 configure.ac         |   54 +
 fuse4fs/fuse4fs.1.in |    6 
 fuse4fs/fuse4fs.c    | 1917 +++++++++++++++++++++++++++++++++++++++++++++++++
 lib/config.h.in      |    6 
 misc/fuse2fs.1.in    |    6 
 misc/fuse2fs.c       | 1939 ++++++++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 3991 insertions(+), 27 deletions(-)
#155 lore raw

PATCH 1/19 fuse2fs: implement bare minimum iomap for file mapping reporting

From: Darrick J. Wong <[email protected]>

Add enough of an iomap implementation that we can do FIEMAP and SEEK_DATA and SEEK_HOLE.

---
 configure         |   49 +++++
 configure.ac      |   31 +++
 fuse4fs/fuse4fs.c |  533 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/config.h.in   |    3 
 misc/fuse2fs.c    |  533 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 1147 insertions(+), 2 deletions(-)
configure +49 -0
index d189adbdeb896d..03b17d07771117 100755
@@ -14578,6 +14578,7 @@ printf "%s\n" "yes" >&6; }
 fi
 
 
+have_fuse_iomap=
 if test -n "$FUSE_LIB"
 then
 	FUSE_USE_VERSION=314
@@ -14604,12 +14605,60 @@ esac
 fi
 
 done
+
+					{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for iomap_begin in libfuse" >&5
+printf %s "checking for iomap_begin in libfuse... " >&6; }
+	cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+	#define _GNU_SOURCE
+	#define _FILE_OFFSET_BITS	64
+	#define FUSE_USE_VERSION	399
+	#include <fuse.h>
+
+int
+main (void)
+{
+
+	struct fuse_operations fs_ops = {
+		.iomap_begin = NULL,
+		.iomap_end = NULL,
+	};
+	struct fuse_file_iomap narf = { };
+
+  ;
+  return 0;
+}
+
+_ACEOF
+if ac_fn_c_try_link "$LINENO"
+then :
+  have_fuse_iomap=yes
+	   { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: yes" >&5
+printf "%s\n" "yes" >&6; }
+else case e in #(
+  e) { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: no" >&5
+printf "%s\n" "no" >&6; } ;;
+esac
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.beam \
+    conftest$ac_exeext conftest.$ac_ext
+	if test "$have_fuse_iomap" = yes
+	then
+		FUSE_USE_VERSION=399
+	fi
 fi
 if test -n "$FUSE_USE_VERSION"
 then
 
 printf "%s\n" "#define FUSE_USE_VERSION $FUSE_USE_VERSION" >>confdefs.h
 
+fi
+if test -n "$have_fuse_iomap"
+then
+
+printf "%s\n" "#define HAVE_FUSE_IOMAP 1" >>confdefs.h
+
 fi
 
 have_fuse_lowlevel=
configure.ac +31 -0
index 39f2dfebf3b4ee..6ccb1fdc444adc 100644
@@ -1382,6 +1382,7 @@ dnl
 dnl Set FUSE_USE_VERSION, which is how fuse servers build against a particular
 dnl libfuse ABI.  Currently we link against the libfuse 3.14 ABI (hence 314)
 dnl
+have_fuse_iomap=
 if test -n "$FUSE_LIB"
 then
 	FUSE_USE_VERSION=314
@@ -1391,12 +1392,42 @@ then
 		[AC_MSG_FAILURE([Cannot build against fuse3 headers])],
 [#define _FILE_OFFSET_BITS	64
 #define FUSE_USE_VERSION	314])
+
+	dnl
+	dnl Check if the fuse library supports iomap, which requires a higher
+	dnl FUSE_USE_VERSION ABI version (3.99)
+	dnl
+	AC_MSG_CHECKING(for iomap_begin in libfuse)
+	AC_LINK_IFELSE(
+	[	AC_LANG_PROGRAM([[
+	#define _GNU_SOURCE
+	#define _FILE_OFFSET_BITS	64
+	#define FUSE_USE_VERSION	399
+	#include <fuse.h>
+		]], [[
+	struct fuse_operations fs_ops = {
+		.iomap_begin = NULL,
+		.iomap_end = NULL,
+	};
+	struct fuse_file_iomap narf = { };
+		]])
+	], have_fuse_iomap=yes
+	   AC_MSG_RESULT(yes),
+	   AC_MSG_RESULT(no))
+	if test "$have_fuse_iomap" = yes
+	then
+		FUSE_USE_VERSION=399
+	fi
 fi
 if test -n "$FUSE_USE_VERSION"
 then
 	AC_DEFINE_UNQUOTED(FUSE_USE_VERSION, $FUSE_USE_VERSION,
 		[Define to the version of FUSE to use])
 fi
+if test -n "$have_fuse_iomap"
+then
+	AC_DEFINE(HAVE_FUSE_IOMAP, 1, [Define to 1 if fuse supports iomap])
+fi
 
 dnl
 dnl Check if the FUSE lowlevel library is supported
fuse4fs/fuse4fs.c +532 -1

599 lines not on this page · raw message

lib/config.h.in +3 -0
index 274659b660ad4e..41598dcc1d7c5a 100644
@@ -79,6 +79,9 @@
 /* Define to 1 if fuse supports lowlevel API */
 #undef HAVE_FUSE_LOWLEVEL
 
+/* Define to 1 if fuse supports iomap */
+#undef HAVE_FUSE_IOMAP
+
 /* Define to 1 if you have the Mac OS X function
    CFLocaleCopyPreferredLanguages in the CoreFoundation framework. */
 #undef HAVE_CFLOCALECOPYPREFERREDLANGUAGES
misc/fuse2fs.c +532 -1

599 lines not on this page · raw message

#156 lore raw

PATCH 2/19 fuse2fs: add iomap= mount option

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Add a mount option to control iomap usage so that we can test before and after scenarios.

---
 fuse4fs/fuse4fs.1.in |    6 ++++++
 fuse4fs/fuse4fs.c    |   46 ++++++++++++++++++++++++++++++++++++++++++++++
 misc/fuse2fs.1.in    |    6 ++++++
 misc/fuse2fs.c       |   46 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 104 insertions(+)
fuse4fs/fuse4fs.1.in +6 -0
index 8bef5f48802385..8855867d27101d 100644
@@ -75,6 +75,12 @@ .SS "fuse4fs options:"
 \fB-o\fR fuse4fs_debug
 enable fuse4fs debugging
 .TP
+\fB-o\fR iomap=
+If set to \fI1\fR, requires iomap to be enabled.
+If set to \fI0\fR, forbids use of iomap.
+If set to \fIdefault\fR (or not set), enables iomap if present.
+This substantially improves the performance of the fuse4fs server.
+.TP
 \fB-o\fR kernel
 Behave more like the kernel ext4 driver in the following ways:
 Allows processes owned by other users to access the filesystem.
fuse4fs/fuse4fs.c +46 -0

100 lines not on this page · raw message

misc/fuse2fs.1.in +6 -0
index 6acfa092851292..2b55fa0e723966 100644
@@ -75,6 +75,12 @@ .SS "fuse2fs options:"
 \fB-o\fR fuse2fs_debug
 enable fuse2fs debugging
 .TP
+\fB-o\fR iomap=
+If set to \fI1\fR, requires iomap to be enabled.
+If set to \fI0\fR, forbids use of iomap.
+If set to \fIdefault\fR (or not set), enables iomap if present.
+This substantially improves the performance of the fuse2fs server.
+.TP
 \fB-o\fR kernel
 Behave more like the kernel ext4 driver in the following ways:
 Allows processes owned by other users to access the filesystem.
misc/fuse2fs.c +46 -0

100 lines not on this page · raw message

#157 lore raw

PATCH 3/19 fuse2fs: implement iomap configuration

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Upload the filesystem geometry to the kernel when asked.

---
 fuse4fs/fuse4fs.c |   96 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 misc/fuse2fs.c    |   96 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 186 insertions(+), 6 deletions(-)
fuse4fs/fuse4fs.c +93 -3
index d5476eb0ab90a4..617ef259133cba 100644
@@ -196,6 +196,10 @@ static inline uint64_t round_down(uint64_t b, unsigned int align)
 # define FL_ZERO_RANGE_FLAG (0)
 #endif
 
+#ifndef NSEC_PER_SEC
+# define NSEC_PER_SEC	(1000000000L)
+#endif
+
 errcode_t ext2fs_check_ext3_journal(ext2_filsys fs);
 errcode_t ext2fs_run_ext3_journal(ext2_filsys *fs);
 
@@ -967,9 +971,9 @@ static int update_atime(ext2_filsys fs, ext2_ino_t ino)
 	EXT4_INODE_GET_XTIME(i_mtime, &mtime, pinode);
 	get_now(&now);
 
-	datime = atime.tv_sec + ((double)atime.tv_nsec / 1000000000);
-	dmtime = mtime.tv_sec + ((double)mtime.tv_nsec / 1000000000);
-	dnow = now.tv_sec + ((double)now.tv_nsec / 1000000000);
+	datime = atime.tv_sec + ((double)atime.tv_nsec / NSEC_PER_SEC);
+	dmtime = mtime.tv_sec + ((double)mtime.tv_nsec / NSEC_PER_SEC);
+	dnow = now.tv_sec + ((double)now.tv_nsec / NSEC_PER_SEC);
 
 	/*
 	 * If atime is newer than mtime and atime hasn't been updated in thirty
@@ -6240,6 +6244,91 @@ static void op_iomap_end(fuse_req_t req, fuse_ino_t fino, uint64_t dontcare,
 
 	fuse_reply_err(req, 0);
 }
+
+/*
+ * Maximal extent format file size.
+ * Resulting logical blkno at s_maxbytes must fit in our on-disk
+ * extent format containers, within a sector_t, and within i_blocks
+ * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
+ * so that won't be a limiting factor.
+ *
+ * However there is other limiting factor. We do store extents in the form
+ * of starting block and length, hence the resulting length of the extent
+ * covering maximum file size must fit into on-disk format containers as
+ * well. Given that length is always by 1 unit bigger than max unit (because
+ * we count 0 as well) we have to lower the s_maxbytes by one fs block.
+ *
+ * Note, this does *not* consider any metadata overhead for vfs i_blocks.
+ */
+static off_t fuse4fs_max_size(struct fuse4fs *ff, off_t upper_limit)
+{
+	off_t res;
+
+	if (!ext2fs_has_feature_huge_file(ff->fs->super)) {
+		upper_limit = (1LL << 32) - 1;
+
+		/* total blocks in file system block size */
+		upper_limit >>= (ff->blocklog - 9);
+		upper_limit <<= ff->blocklog;
+	}
+
+	/*
+	 * 32-bit extent-start container, ee_block. We lower the maxbytes
+	 * by one fs block, so ee_len can cover the extent of maximum file
+	 * size
+	 */
+	res = (1LL << 32) - 1;
+	res <<= ff->blocklog;
+
+	/* Sanity check against vm- & vfs- imposed limits */
+	if (res > upper_limit)
+		res = upper_limit;
+
+	return res;
+}
+
+static void op_iomap_config(fuse_req_t req, uint64_t flags, uint64_t maxbytes)
+{
+	struct fuse_iomap_config cfg = { };
+	struct fuse4fs *ff = fuse4fs_get(req);
+	ext2_filsys fs;
+
+	FUSE4FS_CHECK_CONTEXT(req);
+
+	dbg_printf(ff, "%s: flags=0x%llx maxbytes=0x%llx\n", __func__,
+		   (unsigned long long)flags,
+		   (unsigned long long)maxbytes);
+	fs = fuse4fs_start(ff);
+
+	cfg.flags |= FUSE_IOMAP_CONFIG_UUID;
+	memcpy(cfg.s_uuid, fs->super->s_uuid, sizeof(cfg.s_uuid));
+	cfg.s_uuid_len = sizeof(fs->super->s_uuid);
+
+	cfg.flags |= FUSE_IOMAP_CONFIG_BLOCKSIZE;
+	cfg.s_blocksize = FUSE4FS_FSB_TO_B(ff, 1);
+
+	/*
+	 * If there inode is large enough to house i_[acm]time_extra then we
+	 * can turn on nanosecond timestamps; i_crtime was the next field added
+	 * after i_atime_extra.
+	 */
+	cfg.flags |= FUSE_IOMAP_CONFIG_TIME;
+	if (fs->super->s_inode_size >=
+	    offsetof(struct ext2_inode_large, i_crtime)) {
+		cfg.s_time_gran = 1;
+		cfg.s_time_max = EXT4_EXTRA_TIMESTAMP_MAX;
+	} else {
+		cfg.s_time_gran = NSEC_PER_SEC;
+		cfg.s_time_max = EXT4_NON_EXTRA_TIMESTAMP_MAX;
+	}
+	cfg.s_time_min = EXT4_TIMESTAMP_MIN;
+
+	cfg.flags |= FUSE_IOMAP_CONFIG_MAXBYTES;
+	cfg.s_maxbytes = fuse4fs_max_size(ff, maxbytes);
+
+	fuse4fs_finish(ff, 0);
+	fuse_reply_iomap_config(req, &cfg);
+}
 #endif /* HAVE_FUSE_IOMAP */
 
 static struct fuse_lowlevel_ops fs_ops = {
@@ -6288,6 +6377,7 @@ static struct fuse_lowlevel_ops fs_ops = {
 #ifdef HAVE_FUSE_IOMAP
 	.iomap_begin = op_iomap_begin,
 	.iomap_end = op_iomap_end,
+	.iomap_config = op_iomap_config,
 #endif /* HAVE_FUSE_IOMAP */
 };
 
misc/fuse2fs.c +93 -3

120 lines not on this page · raw message

#158 lore raw

PATCH 4/19 fuse2fs: register block devices for use with iomap

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Register the ext4 block device with the kernel for use with iomap.

---
 fuse4fs/fuse4fs.c |   44 ++++++++++++++++++++++++++++++++++++++++----
 misc/fuse2fs.c    |   42 ++++++++++++++++++++++++++++++++++++++----
 2 files changed, 78 insertions(+), 8 deletions(-)
fuse4fs/fuse4fs.c +40 -4
index 617ef259133cba..e6a60f4ea637f3 100644
@@ -272,6 +272,7 @@ struct fuse4fs {
 #ifdef HAVE_FUSE_IOMAP
 	enum fuse4fs_feature_toggle iomap_want;
 	enum fuse4fs_iomap_state iomap_state;
+	uint32_t iomap_dev;
 #endif
 	unsigned int blockmask;
 	unsigned long offset;
@@ -6028,7 +6029,7 @@ static errcode_t fuse4fs_iomap_begin_extent(struct fuse4fs *ff, uint64_t ino,
 	}
 
 	/* Mapping overlaps startoff, report this. */
-	iomap->dev = FUSE_IOMAP_DEV_NULL;
+	iomap->dev = ff->iomap_dev;
 	iomap->addr = FUSE4FS_FSB_TO_B(ff, extent.e_pblk);
 	iomap->offset = FUSE4FS_FSB_TO_B(ff, extent.e_lblk);
 	iomap->length = FUSE4FS_FSB_TO_B(ff, extent.e_len);
@@ -6061,13 +6062,14 @@ static int fuse4fs_iomap_begin_indirect(struct fuse4fs *ff, uint64_t ino,
 	if (err)
 		return translate_error(fs, ino, err);
 
-	iomap->dev = FUSE_IOMAP_DEV_NULL;
 	iomap->offset = FUSE4FS_FSB_TO_B(ff, startoff);
 	iomap->flags |= FUSE_IOMAP_F_MERGED;
 	if (startblock) {
+		iomap->dev = ff->iomap_dev;
 		iomap->addr = FUSE4FS_FSB_TO_B(ff, startblock);
 		iomap->type = FUSE_IOMAP_TYPE_MAPPED;
 	} else {
+		iomap->dev = FUSE_IOMAP_DEV_NULL;
 		iomap->addr = FUSE_IOMAP_NULL_ADDR;
 		iomap->type = FUSE_IOMAP_TYPE_HOLE;
 	}
@@ -6287,11 +6289,36 @@ static off_t fuse4fs_max_size(struct fuse4fs *ff, off_t upper_limit)
 	return res;
 }
 
+static int fuse4fs_iomap_config_devices(struct fuse4fs *ff)
+{
+	errcode_t err;
+	int fd;
+	int ret;
+
+	err = io_channel_get_fd(ff->fs->io, &fd);
+	if (err)
+		return translate_error(ff->fs, 0, err);
+
+	ret = fuse_lowlevel_iomap_device_add(ff->fuse, fd, 0);
+	if (ret < 0) {
+		dbg_printf(ff, "%s: cannot register iomap dev fd=%d, err=%d\n",
+			   __func__, fd, -ret);
+		return translate_error(ff->fs, 0, -ret);
+	}
+
+	dbg_printf(ff, "%s: registered iomap dev fd=%d iomap_dev=%u\n",
+		   __func__, fd, ff->iomap_dev);
+
+	ff->iomap_dev = ret;
+	return 0;
+}
+
 static void op_iomap_config(fuse_req_t req, uint64_t flags, uint64_t maxbytes)
 {
 	struct fuse_iomap_config cfg = { };
 	struct fuse4fs *ff = fuse4fs_get(req);
 	ext2_filsys fs;
+	int ret = 0;
 
 	FUSE4FS_CHECK_CONTEXT(req);
 
@@ -6326,8 +6353,16 @@ static void op_iomap_config(fuse_req_t req, uint64_t flags, uint64_t maxbytes)
 	cfg.flags |= FUSE_IOMAP_CONFIG_MAXBYTES;
 	cfg.s_maxbytes = fuse4fs_max_size(ff, maxbytes);
 
-	fuse4fs_finish(ff, 0);
-	fuse_reply_iomap_config(req, &cfg);
+	ret = fuse4fs_iomap_config_devices(ff);
+	if (ret)
+		goto out_unlock;
+
+out_unlock:
+	fuse4fs_finish(ff, ret);
+	if (ret)
+		fuse_reply_err(req, -ret);
+	else
+		fuse_reply_iomap_config(req, &cfg);
 }
 #endif /* HAVE_FUSE_IOMAP */
 
@@ -6786,6 +6821,7 @@ int main(int argc, char *argv[])
 #ifdef HAVE_FUSE_IOMAP
 		.iomap_want = FT_DEFAULT,
 		.iomap_state = IOMAP_UNKNOWN,
+		.iomap_dev = FUSE_IOMAP_DEV_NULL,
 #endif
 	};
 	errcode_t err;
misc/fuse2fs.c +38 -4

95 lines not on this page · raw message

#159 lore raw

PATCH 5/19 fuse2fs: implement directio file reads

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Implement file reads via iomap. Currently only directio is supported.

---
 fuse4fs/fuse4fs.c |   14 +++++++++++++-
 misc/fuse2fs.c    |   14 +++++++++++++-
 2 files changed, 26 insertions(+), 2 deletions(-)
fuse4fs/fuse4fs.c +13 -1
index e6a60f4ea637f3..9fa875d1d99ae8 100644
@@ -6151,7 +6151,19 @@ static int fuse4fs_iomap_begin_read(struct fuse4fs *ff, ext2_ino_t ino,
 				    uint64_t count, uint32_t opflags,
 				    struct fuse_file_iomap *read)
 {
-	return -ENOSYS;
+	if (!(opflags & FUSE_IOMAP_OP_DIRECT))
+		return -ENOSYS;
+
+	/* fall back to slow path for inline data reads */
+	if (inode->i_flags & EXT4_INLINE_DATA_FL)
+		return -ENOSYS;
+
+	if (inode->i_flags & EXT4_EXTENTS_FL)
+		return fuse4fs_iomap_begin_extent(ff, ino, inode, pos, count,
+						  opflags, read);
+
+	return fuse4fs_iomap_begin_indirect(ff, ino, inode, pos, count,
+					    opflags, read);
 }
 
 static int fuse4fs_iomap_begin_write(struct fuse4fs *ff, ext2_ino_t ino,
misc/fuse2fs.c +13 -1
index 70ded520cd318a..a5105855f1669a 100644
@@ -5597,7 +5597,19 @@ static int fuse2fs_iomap_begin_read(struct fuse2fs *ff, ext2_ino_t ino,
 				    uint64_t count, uint32_t opflags,
 				    struct fuse_file_iomap *read)
 {
-	return -ENOSYS;
+	if (!(opflags & FUSE_IOMAP_OP_DIRECT))
+		return -ENOSYS;
+
+	/* fall back to slow path for inline data reads */
+	if (inode->i_flags & EXT4_INLINE_DATA_FL)
+		return -ENOSYS;
+
+	if (inode->i_flags & EXT4_EXTENTS_FL)
+		return fuse2fs_iomap_begin_extent(ff, ino, inode, pos, count,
+						  opflags, read);
+
+	return fuse2fs_iomap_begin_indirect(ff, ino, inode, pos, count,
+					    opflags, read);
 }
 
 static int fuse2fs_iomap_begin_write(struct fuse2fs *ff, ext2_ino_t ino,
#160 lore raw

PATCH 6/19 fuse2fs: add extent dump function for debugging

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Add a function to dump an inode's extent map for debugging purposes. This helped debug a problem with generic/299 failing on 1k fsblock filesystems:

--- a/tests/generic/299.out 2025-07-15 14:45:15.030113607 -0700
+++ b/tests/generic/299.out.bad 2025-07-16 19:33:50.889344998 -0700
@@ -3,3 +3,4 @@ QA output created by 299
Run fio with random aio-dio pattern

Start fallocate/truncate loop
+fio: io_u error on file /opt/direct_aio.0.0: Input/output error: write offset=2602827776, buflen=131072

(The cause of this was misuse of the libext2fs extent code)

---
 fuse4fs/fuse4fs.c |   70 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 misc/fuse2fs.c    |   70 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 140 insertions(+)
fuse4fs/fuse4fs.c +70 -0
index 9fa875d1d99ae8..9d434294613d0d 100644
@@ -889,6 +889,74 @@ static inline int fuse4fs_iomap_enabled(const struct fuse4fs *ff)
 # define fuse4fs_iomap_enabled(...)	(0)
 #endif
 
+static inline void fuse4fs_dump_extents(struct fuse4fs *ff, ext2_ino_t ino,
+					struct ext2_inode_large *inode,
+					const char *why)
+{
+	ext2_filsys fs = ff->fs;
+	unsigned int nr = 0;
+	blk64_t blockcount = 0;
+	struct ext2_inode_large xinode;
+	struct ext2fs_extent extent;
+	ext2_extent_handle_t extents;
+	int op = EXT2_EXTENT_ROOT;
+	errcode_t retval;
+
+	if (!inode) {
+		inode = &xinode;
+
+		retval = fuse4fs_read_inode(fs, ino, inode);
+		if (retval) {
+			com_err(__func__, retval, _("reading ino %u"), ino);
+			return;
+		}
+	}
+
+	if (!(inode->i_flags & EXT4_EXTENTS_FL))
+		return;
+
+	printf("%s: %s ino=%u isize %llu iblocks %llu\n", __func__, why, ino,
+	       EXT2_I_SIZE(inode),
+	       (ext2fs_get_stat_i_blocks(fs, EXT2_INODE(inode)) * 512) /
+	        fs->blocksize);
+	fflush(stdout);
+
+	retval = ext2fs_extent_open(fs, ino, &extents);
+	if (retval) {
+		com_err(__func__, retval, _("opening extents of ino \"%u\""),
+			ino);
+		return;
+	}
+
+	while ((retval = ext2fs_extent_get(extents, op, &extent)) == 0) {
+		op = EXT2_EXTENT_NEXT;
+
+		if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
+			continue;
+
+		printf("[%u]: %s ino=%u lblk 0x%llx pblk 0x%llx len 0x%x flags 0x%x\n",
+		       nr++, why, ino, extent.e_lblk, extent.e_pblk,
+		       extent.e_len, extent.e_flags);
+		fflush(stdout);
+		if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF)
+			blockcount += extent.e_len;
+		else
+			blockcount++;
+	}
+	if (retval == EXT2_ET_EXTENT_NO_NEXT)
+		retval = 0;
+	if (retval) {
+		com_err(__func__, retval, ("getting extents of ino %u"),
+			ino);
+	}
+	if (inode->i_file_acl)
+		blockcount++;
+	printf("%s: %s sum(e_len) %llu\n", __func__, why, blockcount);
+	fflush(stdout);
+
+	ext2fs_extent_free(extents);
+}
+
 static void get_now(struct timespec *now)
 {
 #ifdef CLOCK_REALTIME
@@ -6225,6 +6293,8 @@ static void op_iomap_begin(fuse_req_t req, fuse_ino_t fino, uint64_t dontcare,
 
 	/* Not filling even the first byte will make the kernel unhappy. */
 	if (read.offset > pos || read.offset + read.length <= pos) {
+		if (ff->debug)
+			fuse4fs_dump_extents(ff, ino, &inode, "BAD DATA");
 		ret = translate_error(fs, ino, EXT2_ET_INODE_CORRUPTED);
 		goto out_unlock;
 	}
misc/fuse2fs.c +70 -0

82 lines not on this page · raw message

#161 lore raw

PATCH 7/19 fuse2fs: implement direct write support

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Wire up an iomap_begin method that can allocate into holes so that we can do directio writes.

---
 fuse4fs/fuse4fs.c |  477 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 misc/fuse2fs.c    |  471 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 942 insertions(+), 6 deletions(-)
fuse4fs/fuse4fs.c +474 -3
index 9d434294613d0d..2f95a1ecbfa330 100644
@@ -6234,12 +6234,106 @@ static int fuse4fs_iomap_begin_read(struct fuse4fs *ff, ext2_ino_t ino,
 					    opflags, read);
 }
 
+static int fuse4fs_iomap_write_allocate(struct fuse4fs *ff, ext2_ino_t ino,
+					struct ext2_inode_large *inode,
+					off_t pos, uint64_t count,
+					uint32_t opflags,
+					struct fuse_file_iomap *read,
+					bool *dirty)
+{
+	ext2_filsys fs = ff->fs;
+	blk64_t startoff = FUSE4FS_B_TO_FSBT(ff, pos);
+	blk64_t stopoff = FUSE4FS_B_TO_FSB(ff, pos + count);
+	blk64_t old_iblocks;
+	errcode_t err;
+	int ret;
+
+	dbg_printf(ff,
+ "%s: ino=%d startoff 0x%llx blockcount 0x%llx\n",
+		   __func__, ino, startoff, stopoff - startoff);
+
+	if (!fuse4fs_can_allocate(ff, stopoff - startoff))
+		return -ENOSPC;
+
+	old_iblocks = ext2fs_get_stat_i_blocks(fs, EXT2_INODE(inode));
+	err = ext2fs_fallocate(fs, EXT2_FALLOCATE_FORCE_UNINIT, ino,
+			       EXT2_INODE(inode), ~0ULL, startoff,
+			       stopoff - startoff);
+	if (err)
+		return translate_error(fs, ino, err);
+
+	/*
+	 * New allocations for file data blocks on indirect mapped files are
+	 * zeroed through the IO manager so we have to flush it to disk.
+	 */
+	if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
+	    old_iblocks != ext2fs_get_stat_i_blocks(fs, EXT2_INODE(inode))) {
+		err = io_channel_flush(fs->io);
+		if (err)
+			return translate_error(fs, ino, err);
+	}
+
+	/* pick up the newly allocated mapping */
+	ret = fuse4fs_iomap_begin_read(ff, ino, inode, pos, count, opflags,
+				       read);
+	if (ret)
+		return ret;
+
+	read->flags |= FUSE_IOMAP_F_DIRTY;
+	*dirty = true;
+	return 0;
+}
+
+static off_t fuse4fs_max_file_size(const struct fuse4fs *ff,
+				   const struct ext2_inode_large *inode)
+{
+	ext2_filsys fs = ff->fs;
+	blk64_t addr_per_block, max_map_block;
+
+	if (inode->i_flags & EXT4_EXTENTS_FL) {
+		max_map_block = (1ULL << 32) - 1;
+	} else {
+		addr_per_block = fs->blocksize >> 2;
+		max_map_block = addr_per_block;
+		max_map_block += addr_per_block * addr_per_block;
+		max_map_block += addr_per_block * addr_per_block * addr_per_block;
+		max_map_block += 12;
+	}
+
+	return FUSE4FS_FSB_TO_B(ff, max_map_block) + (fs->blocksize - 1);
+}
+
 static int fuse4fs_iomap_begin_write(struct fuse4fs *ff, ext2_ino_t ino,
 				     struct ext2_inode_large *inode, off_t pos,
 				     uint64_t count, uint32_t opflags,
-				     struct fuse_file_iomap *read)
+				     struct fuse_file_iomap *read,
+				     bool *dirty)
 {
-	return -ENOSYS;
+	off_t max_size = fuse4fs_max_file_size(ff, inode);
+	int ret;
+
+	if (!(opflags & FUSE_IOMAP_OP_DIRECT))
+		return -ENOSYS;
+
+	if (pos >= max_size)
+		return -EFBIG;
+
+	if (pos >= max_size - count)
+		count = max_size - pos;
+
+	ret = fuse4fs_iomap_begin_read(ff, ino, inode, pos, count, opflags,
+				       read);
+	if (ret)
+		return ret;
+
+	if (fuse_iomap_need_write_allocate(opflags, read)) {
+		ret = fuse4fs_iomap_write_allocate(ff, ino, inode, pos, count,
+						   opflags, read, dirty);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
 }
 
 static void op_iomap_begin(fuse_req_t req, fuse_ino_t fino, uint64_t dontcare,
@@ -6251,6 +6345,7 @@ static void op_iomap_begin(fuse_req_t req, fuse_ino_t fino, uint64_t dontcare,
 	ext2_filsys fs;
 	ext2_ino_t ino;
 	errcode_t err;
+	bool dirty = false;
 	int ret = 0;
 
 	FUSE4FS_CHECK_CONTEXT(req);
@@ -6274,7 +6369,7 @@ static void op_iomap_begin(fuse_req_t req, fuse_ino_t fino, uint64_t dontcare,
 						 opflags, &read);
 	else if (fuse_iomap_is_write(opflags))
 		ret = fuse4fs_iomap_begin_write(ff, ino, &inode, pos, count,
-						opflags, &read);
+						opflags, &read, &dirty);
 	else
 		ret = fuse4fs_iomap_begin_read(ff, ino, &inode, pos, count,
 					       opflags, &read);
@@ -6299,6 +6394,14 @@ static void op_iomap_begin(fuse_req_t req, fuse_ino_t fino, uint64_t dontcare,
 		goto out_unlock;
 	}
 
+	if (dirty) {
+		err = fuse4fs_write_inode(fs, ino, &inode);
+		if (err) {
+			ret = translate_error(fs, ino, err);
+			goto out_unlock;
+		}
+	}
+
 out_unlock:
 	fuse4fs_finish(ff, ret);
 	if (ret)
@@ -6446,6 +6549,373 @@ static void op_iomap_config(fuse_req_t req, uint64_t flags, uint64_t maxbytes)
 	else
 		fuse_reply_iomap_config(req, &cfg);
 }
+
+static inline bool fuse4fs_can_merge_mappings(const struct ext2fs_extent *left,
+					      const struct ext2fs_extent *right)
+{
+	uint64_t max_len = (left->e_flags & EXT2_EXTENT_FLAGS_UNINIT) ?
+				EXT_UNINIT_MAX_LEN : EXT_INIT_MAX_LEN;
+
+	return left->e_lblk + left->e_len == right->e_lblk &&
+	       left->e_pblk + left->e_len == right->e_pblk &&
+	       (left->e_flags & EXT2_EXTENT_FLAGS_UNINIT) ==
+	        (right->e_flags & EXT2_EXTENT_FLAGS_UNINIT) &&
+	       (uint64_t)left->e_len + right->e_len <= max_len;
+}
+
+static int fuse4fs_try_merge_mappings(struct fuse4fs *ff, ext2_ino_t ino,
+				      ext2_extent_handle_t handle,
+				      blk64_t startoff)
+{
+	ext2_filsys fs = ff->fs;
+	struct ext2fs_extent left, right;
+	errcode_t err;
+
+	/* Look up the mappings before startoff */
+	err = fuse4fs_get_mapping_at(ff, handle, startoff - 1, &left);
+	if (err == EXT2_ET_EXTENT_NOT_FOUND)
+		return 0;
+	if (err)
+		return translate_error(fs, ino, err);
+
+	/* Look up the mapping at startoff */
+	err = fuse4fs_get_mapping_at(ff, handle, startoff, &right);
+	if (err == EXT2_ET_EXTENT_NOT_FOUND)
+		return 0;
+	if (err)
+		return translate_error(fs, ino, err);
+
+	/* Can we combine them? */
+	if (!fuse4fs_can_merge_mappings(&left, &right))
+		return 0;
+
+	/*
+	 * Delete the mapping after startoff because libext2fs cannot handle
+	 * overlapping mappings.
+	 */
+	err = ext2fs_extent_delete(handle, 0);
+	DUMP_EXTENT(ff, "remover", startoff, err, &right);
+	if (err)
+		return translate_error(fs, ino, err);
+
+	err = ext2fs_extent_fix_parents(handle);
+	DUMP_EXTENT(ff, "fixremover", startoff, err, &right);
+	if (err)
+		return translate_error(fs, ino, err);
+
+	/* Move back and lengthen the mapping before startoff */
+	err = ext2fs_extent_goto(handle, left.e_lblk);
+	DUMP_EXTENT(ff, "movel", startoff - 1, err, &left);
+	if (err)
+		return translate_error(fs, ino, err);
+
+	left.e_len += right.e_len;
+	err = ext2fs_extent_replace(handle, 0, &left);
+	DUMP_EXTENT(ff, "replacel", startoff - 1, err, &left);
+	if (err)
+		return translate_error(fs, ino, err);
+
+	err = ext2fs_extent_fix_parents(handle);
+	DUMP_EXTENT(ff, "fixreplacel", startoff - 1, err, &left);
+	if (err)
+		return translate_error(fs, ino, err);
+
+	return 0;
+}
+
+static int fuse4fs_convert_unwritten_mapping(struct fuse4fs *ff,
+					     ext2_ino_t ino,
+					     struct ext2_inode_large *inode,
+					     ext2_extent_handle_t handle,
+					     blk64_t *cursor, blk64_t stopoff)
+{
+	ext2_filsys fs = ff->fs;
+	struct ext2fs_extent extent;
+	blk64_t startoff = *cursor;
+	errcode_t err;
+
+	/*
+	 * Find the mapping at startoff.  Note that we can find holes because
+	 * the mapping data can change due to racing writes.
+	 */
+	err = fuse4fs_get_mapping_at(ff, handle, startoff, &extent);
+	if (err == EXT2_ET_EXTENT_NOT_FOUND) {
+		/*
+		 * If we didn't find any mappings at all then the file is
+		 * completely sparse.  There's nothing to convert.
+		 */
+		*cursor = stopoff;
+		return 0;
+	}
+	if (err)
+		return translate_error(fs, ino, err);
+
+	/*
+	 * The mapping is completely to the left of the range that we want.
+	 * Let's see what's in the next extent, if there is one.
+	 */
+	if (startoff >= extent.e_lblk + extent.e_len) {
+		/*
+		 * Mapping ends to the left of the current position.  Try to
+		 * find the next mapping.  If there is no next mapping, then
+		 * we're done.
+		 */
+		err = fuse4fs_get_next_mapping(ff, handle, startoff, &extent);
+		if (err == EXT2_ET_EXTENT_NOT_FOUND) {
+			*cursor = stopoff;
+			return 0;
+		}
+		if (err)
+			return translate_error(fs, ino, err);
+	}
+
+	/*
+	 * The mapping is completely to the right of the range that we want,
+	 * so we're done.
+	 */
+	if (extent.e_lblk >= stopoff) {
+		*cursor = stopoff;
+		return 0;
+	}
+
+	/*
+	 * At this point, we have a mapping that overlaps (startoff, stopoff].
+	 * If the mapping is already written, move on to the next one.
+	 */
+	if (!(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT))
+		goto next;
+
+	if (startoff > extent.e_lblk) {
+		struct ext2fs_extent newex = extent;
+
+		/*
+		 * Unwritten mapping starts before startoff.  Shorten
+		 * the previous mapping...
+		 */
+		newex.e_len = startoff - extent.e_lblk;
+		err = ext2fs_extent_replace(handle, 0, &newex);
+		DUMP_EXTENT(ff, "shortenp", startoff, err, &newex);
+		if (err)
+			return translate_error(fs, ino, err);
+
+		err = ext2fs_extent_fix_parents(handle);
+		DUMP_EXTENT(ff, "fixshortenp", startoff, err, &newex);
+		if (err)
+			return translate_error(fs, ino, err);
+
+		/* ...and create new written mapping at startoff. */
+		extent.e_len -= newex.e_len;
+		extent.e_lblk += newex.e_len;
+		extent.e_pblk += newex.e_len;
+		extent.e_flags = newex.e_flags & ~EXT2_EXTENT_FLAGS_UNINIT;
+
+		err = ext2fs_extent_insert(handle,
+					   EXT2_EXTENT_INSERT_AFTER,
+					   &extent);
+		DUMP_EXTENT(ff, "insertx", startoff, err, &extent);
+		if (err)
+			return translate_error(fs, ino, err);
+
+		err = ext2fs_extent_fix_parents(handle);
+		DUMP_EXTENT(ff, "fixinsertx", startoff, err, &extent);
+		if (err)
+			return translate_error(fs, ino, err);
+	}
+
+	if (extent.e_lblk + extent.e_len > stopoff) {
+		struct ext2fs_extent newex = extent;
+
+		/*
+		 * Unwritten mapping ends after stopoff.  Shorten the current
+		 * mapping...
+		 */
+		extent.e_len = stopoff - extent.e_lblk;
+		extent.e_flags &= ~EXT2_EXTENT_FLAGS_UNINIT;
+
+		err = ext2fs_extent_replace(handle, 0, &extent);
+		DUMP_EXTENT(ff, "shortenn", startoff, err, &extent);
+		if (err)
+			return translate_error(fs, ino, err);
+
+		err = ext2fs_extent_fix_parents(handle);
+		DUMP_EXTENT(ff, "fixshortenn", startoff, err, &extent);
+		if (err)
+			return translate_error(fs, ino, err);
+
+		/* ..and create a new unwritten mapping at stopoff. */
+		newex.e_pblk += extent.e_len;
+		newex.e_lblk += extent.e_len;
+		newex.e_len -= extent.e_len;
+		newex.e_flags |= EXT2_EXTENT_FLAGS_UNINIT;
+
+		err = ext2fs_extent_insert(handle,
+					   EXT2_EXTENT_INSERT_AFTER,
+					   &newex);
+		DUMP_EXTENT(ff, "insertn", startoff, err, &newex);
+		if (err)
+			return translate_error(fs, ino, err);
+
+		err = ext2fs_extent_fix_parents(handle);
+		DUMP_EXTENT(ff, "fixinsertn", startoff, err, &newex);
+		if (err)
+			return translate_error(fs, ino, err);
+	}
+
+	/* Still unwritten?  Update the state. */
+	if (extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) {
+		extent.e_flags &= ~EXT2_EXTENT_FLAGS_UNINIT;
+
+		err = ext2fs_extent_replace(handle, 0, &extent);
+		DUMP_EXTENT(ff, "replacex", startoff, err, &extent);
+		if (err)
+			return translate_error(fs, ino, err);
+
+		err = ext2fs_extent_fix_parents(handle);
+		DUMP_EXTENT(ff, "fixreplacex", startoff, err, &extent);
+		if (err)
+			return translate_error(fs, ino, err);
+	}
+
+next:
+	/* Try to merge with the previous extent */
+	if (startoff > 0) {
+		err = fuse4fs_try_merge_mappings(ff, ino, handle, startoff);
+		if (err)
+			return translate_error(fs, ino, err);
+	}
+
+	*cursor = extent.e_lblk + extent.e_len;
+	return 0;
+}
+
+static int fuse4fs_convert_unwritten_mappings(struct fuse4fs *ff,
+					      ext2_ino_t ino,
+					      struct ext2_inode_large *inode,
+					      off_t pos, size_t written)
+{
+	ext2_extent_handle_t handle;
+	ext2_filsys fs = ff->fs;
+	blk64_t startoff = FUSE4FS_B_TO_FSBT(ff, pos);
+	const blk64_t stopoff = FUSE4FS_B_TO_FSB(ff, pos + written);
+	errcode_t err;
+	int ret;
+
+	err = ext2fs_extent_open2(fs, ino, EXT2_INODE(inode), &handle);
+	if (err)
+		return translate_error(fs, ino, err);
+
+	/* Walk every mapping in the range, converting them. */
+	while (startoff < stopoff) {
+		blk64_t old_startoff = startoff;
+
+		ret = fuse4fs_convert_unwritten_mapping(ff, ino, inode, handle,
+							&startoff, stopoff);
+		if (ret)
+			goto out_handle;
+		if (startoff <= old_startoff) {
+			/* Do not go backwards. */
+			ret = translate_error(fs, ino, EXT2_ET_INODE_CORRUPTED);
+			goto out_handle;
+		}
+	}
+
+	/* Try to merge the right edge */
+	ret = fuse4fs_try_merge_mappings(ff, ino, handle, stopoff);
+out_handle:
+	ext2fs_extent_free(handle);
+	return ret;
+}
+
+static void op_iomap_ioend(fuse_req_t req, fuse_ino_t fino, uint64_t dontcare,
+			   off_t pos, size_t written, uint32_t ioendflags,
+			   int error, uint32_t dev, uint64_t new_addr)
+{
+	struct fuse4fs *ff = fuse4fs_get(req);
+	struct ext2_inode_large inode;
+	ext2_filsys fs;
+	ext2_ino_t ino;
+	ext2_off64_t isize;
+	errcode_t err;
+	bool dirty = false;
+	off_t newsize = -1;
+	int ret = 0;
+
+	FUSE4FS_CHECK_CONTEXT(req);
+	FUSE4FS_CONVERT_FINO(req, &ino, fino);
+
+	dbg_printf(ff,
+ "%s: ino=%d pos=0x%llx written=0x%zx ioendflags=0x%x error=%d dev=%u new_addr=0x%llx\n",
+		   __func__, ino,
+		   (unsigned long long)pos,
+		   written,
+		   ioendflags,
+		   error,
+		   dev,
+		   (unsigned long long)new_addr);
+
+	if (error) {
+		fuse_reply_err(req, -error);
+		return;
+	}
+
+	fs = fuse4fs_start(ff);
+
+	/* should never see these ioend types */
+	if (ioendflags & FUSE_IOMAP_IOEND_SHARED) {
+		ret = translate_error(fs, ino, EXT2_ET_FILESYSTEM_CORRUPTED);
+		goto out_unlock;
+	}
+
+	err = fuse4fs_read_inode(fs, ino, &inode);
+	if (err) {
+		ret = translate_error(fs, ino, err);
+		goto out_unlock;
+	}
+
+	if (ioendflags & FUSE_IOMAP_IOEND_UNWRITTEN) {
+		/* unwritten extents are only supported on extents files */
+		if (!(inode.i_flags & EXT4_EXTENTS_FL)) {
+			ret = translate_error(fs, ino,
+					      EXT2_ET_FILESYSTEM_CORRUPTED);
+			goto out_unlock;
+		}
+
+		ret = fuse4fs_convert_unwritten_mappings(ff, ino, &inode,
+							 pos, written);
+		if (ret)
+			goto out_unlock;
+
+		dirty = true;
+	}
+
+	isize = EXT2_I_SIZE(&inode);
+	if (pos + written > isize) {
+		err = ext2fs_inode_size_set(fs, EXT2_INODE(&inode),
+					    pos + written);
+		if (err) {
+			ret = translate_error(fs, ino, err);
+			goto out_unlock;
+		}
+
+		dirty = true;
+	}
+
+	if (dirty) {
+		err = fuse4fs_write_inode(fs, ino, &inode);
+		if (err) {
+			ret = translate_error(fs, ino, err);
+			goto out_unlock;
+		}
+	}
+
+	newsize = EXT2_I_SIZE(&inode);
+out_unlock:
+	fuse4fs_finish(ff, ret);
+	if (ret)
+		fuse_reply_err(req, -ret);
+	else
+		fuse_reply_iomap_ioend(req, newsize);
+}
 #endif /* HAVE_FUSE_IOMAP */
 
 static struct fuse_lowlevel_ops fs_ops = {
@@ -6495,6 +6965,7 @@ static struct fuse_lowlevel_ops fs_ops = {
 	.iomap_begin = op_iomap_begin,
 	.iomap_end = op_iomap_end,
 	.iomap_config = op_iomap_config,
+	.iomap_ioend = op_iomap_ioend,
 #endif /* HAVE_FUSE_IOMAP */
 };
 
misc/fuse2fs.c +468 -3

511 lines not on this page · raw message

#162 lore raw

PATCH 8/19 fuse2fs: turn on iomap for pagecache IO

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Turn on iomap for pagecache IO to regular files.

---
 fuse4fs/fuse4fs.c |   61 +++++++++++++++++++++++++++++++++++++++++++++++------
 misc/fuse2fs.c    |   61 +++++++++++++++++++++++++++++++++++++++++++++++------
 2 files changed, 108 insertions(+), 14 deletions(-)
fuse4fs/fuse4fs.c +54 -7
index 2f95a1ecbfa330..39737c72e6133f 100644
@@ -6219,9 +6219,6 @@ static int fuse4fs_iomap_begin_read(struct fuse4fs *ff, ext2_ino_t ino,
 				    uint64_t count, uint32_t opflags,
 				    struct fuse_file_iomap *read)
 {
-	if (!(opflags & FUSE_IOMAP_OP_DIRECT))
-		return -ENOSYS;
-
 	/* fall back to slow path for inline data reads */
 	if (inode->i_flags & EXT4_INLINE_DATA_FL)
 		return -ENOSYS;
@@ -6312,9 +6309,6 @@ static int fuse4fs_iomap_begin_write(struct fuse4fs *ff, ext2_ino_t ino,
 	off_t max_size = fuse4fs_max_file_size(ff, inode);
 	int ret;
 
-	if (!(opflags & FUSE_IOMAP_OP_DIRECT))
-		return -ENOSYS;
-
 	if (pos >= max_size)
 		return -EFBIG;
 
@@ -6410,12 +6404,51 @@ static void op_iomap_begin(fuse_req_t req, fuse_ino_t fino, uint64_t dontcare,
 		fuse_reply_iomap_begin(req, &read, NULL);
 }
 
+static int fuse4fs_iomap_append_setsize(struct fuse4fs *ff, ext2_ino_t ino,
+					loff_t newsize)
+{
+	ext2_filsys fs = ff->fs;
+	struct ext2_inode_large inode;
+	ext2_off64_t isize;
+	errcode_t err;
+
+	dbg_printf(ff, "%s: ino=%u newsize=%llu\n", __func__, ino,
+		   (unsigned long long)newsize);
+
+	err = fuse4fs_read_inode(fs, ino, &inode);
+	if (err)
+		return translate_error(fs, ino, err);
+
+	isize = EXT2_I_SIZE(&inode);
+	if (newsize <= isize)
+		return 0;
+
+	dbg_printf(ff, "%s: ino=%u oldsize=%llu newsize=%llu\n", __func__, ino,
+		   (unsigned long long)isize,
+		   (unsigned long long)newsize);
+
+	/*
+	 * XXX cheesily update the ondisk size even though we only want to do
+	 * the incore size until writeback happens
+	 */
+	err = ext2fs_inode_size_set(fs, EXT2_INODE(&inode), newsize);
+	if (err)
+		return translate_error(fs, ino, err);
+
+	err = fuse4fs_write_inode(fs, ino, &inode);
+	if (err)
+		return translate_error(fs, ino, err);
+
+	return 0;
+}
+
 static void op_iomap_end(fuse_req_t req, fuse_ino_t fino, uint64_t dontcare,
 			 off_t pos, uint64_t count, uint32_t opflags,
 			 ssize_t written, const struct fuse_file_iomap *iomap)
 {
 	struct fuse4fs *ff = fuse4fs_get(req);
 	ext2_ino_t ino;
+	int ret = 0;
 
 	FUSE4FS_CHECK_CONTEXT(req);
 	FUSE4FS_CONVERT_FINO(req, &ino, fino);
@@ -6429,7 +6462,21 @@ static void op_iomap_end(fuse_req_t req, fuse_ino_t fino, uint64_t dontcare,
 		   written,
 		   iomap->flags);
 
-	fuse_reply_err(req, 0);
+	fuse4fs_start(ff);
+
+	/* XXX is this really necessary? */
+	if ((opflags & FUSE_IOMAP_OP_WRITE) &&
+	    !(opflags & FUSE_IOMAP_OP_DIRECT) &&
+	    (iomap->flags & FUSE_IOMAP_F_SIZE_CHANGED) &&
+	    written > 0) {
+		ret = fuse4fs_iomap_append_setsize(ff, ino, pos + written);
+		if (ret)
+			goto out_unlock;
+	}
+
+out_unlock:
+	fuse4fs_finish(ff, ret);
+	fuse_reply_err(req, -ret);
 }
 
 /*
misc/fuse2fs.c +54 -7

90 lines not on this page · raw message

#163 lore raw

PATCH 9/19 fuse2fs: don't zero bytes in punch hole

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

When iomap is in use for the pagecache, it will take care of zeroing the unaligned parts of punched out regions so we don't have to do it ourselves.

---
 fuse4fs/fuse4fs.c |    8 ++++++++
 misc/fuse2fs.c    |    9 +++++++++
 2 files changed, 17 insertions(+)
fuse4fs/fuse4fs.c +8 -0
index 39737c72e6133f..b28e92ed5f2e65 100644
@@ -5649,6 +5649,10 @@ static errcode_t fuse4fs_zero_middle(struct fuse4fs *ff, ext2_ino_t ino,
 	int retflags;
 	errcode_t err;
 
+	/* the kernel does this for us in iomap mode */
+	if (fuse4fs_iomap_enabled(ff))
+		return 0;
+
 	if (!*buf) {
 		err = ext2fs_get_mem(fs->blocksize, buf);
 		if (err)
@@ -5685,6 +5689,10 @@ static errcode_t fuse4fs_zero_edge(struct fuse4fs *ff, ext2_ino_t ino,
 	off_t residue;
 	errcode_t err;
 
+	/* the kernel does this for us in iomap mode */
+	if (fuse4fs_iomap_enabled(ff))
+		return 0;
+
 	residue = FUSE4FS_OFF_IN_FSB(ff, offset);
 	if (residue == 0)
 		return 0;
misc/fuse2fs.c +9 -0
index afb50a7e498694..be7004bb95cb8f 100644
@@ -726,6 +726,7 @@ static inline int fuse2fs_iomap_enabled(const struct fuse2fs *ff)
 }
 #else
 # define fuse2fs_iomap_enabled(...)	(0)
+# define fuse2fs_iomap_enabled(...)	(0)
 #endif
 
 static inline void fuse2fs_dump_extents(struct fuse2fs *ff, ext2_ino_t ino,
@@ -5096,6 +5097,10 @@ static errcode_t clean_block_middle(struct fuse2fs *ff, ext2_ino_t ino,
 	int retflags;
 	errcode_t err;
 
+	/* the kernel does this for us in iomap mode */
+	if (fuse2fs_iomap_enabled(ff))
+		return 0;
+
 	if (!*buf) {
 		err = ext2fs_get_mem(fs->blocksize, buf);
 		if (err)
@@ -5132,6 +5137,10 @@ static errcode_t clean_block_edge(struct fuse2fs *ff, ext2_ino_t ino,
 	off_t residue;
 	errcode_t err;
 
+	/* the kernel does this for us in iomap mode */
+	if (fuse2fs_iomap_enabled(ff))
+		return 0;
+
 	residue = FUSE2FS_OFF_IN_FSB(ff, offset);
 	if (residue == 0)
 		return 0;
#164 lore raw

PATCH 10/19 fuse2fs: don't do file data block IO when iomap is enabled

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

When iomap is in use for the page cache, the kernel will take care of all the file data block IO for us, including zeroing of punched ranges and post-EOF bytes. fuse2fs only needs to do IO for inline data.

Therefore, set the NOBLOCKIO ext2_file flag so that libext2fs will not do any regular file IO to or from disk blocks at all.

---
 fuse4fs/fuse4fs.c |   11 +++++++-
 misc/fuse2fs.c    |   72 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 81 insertions(+), 2 deletions(-)
fuse4fs/fuse4fs.c +10 -1
index b28e92ed5f2e65..b95ff73b7d9d8e 100644
@@ -3714,9 +3714,14 @@ static int fuse4fs_truncate(struct fuse4fs *ff, ext2_ino_t ino, off_t new_size)
 	ext2_file_t file;
 	__u64 old_isize;
 	errcode_t err;
+	int flags = EXT2_FILE_WRITE;
 	int ret = 0;
 
-	err = ext2fs_file_open(fs, ino, EXT2_FILE_WRITE, &file);
+	/* the kernel handles all eof zeroing for us in iomap mode */
+	if (fuse4fs_iomap_enabled(ff))
+		flags |= EXT2_FILE_NOBLOCKIO;
+
+	err = ext2fs_file_open(fs, ino, flags, &file);
 	if (err)
 		return translate_error(fs, ino, err);
 
@@ -3811,6 +3816,10 @@ static int fuse4fs_open_file(struct fuse4fs *ff, const struct fuse_ctx *ctxt,
 	if (linked)
 		check |= L_OK;
 
+	/* the kernel handles all block IO for us in iomap mode */
+	if (fuse4fs_iomap_enabled(ff))
+		file->open_flags |= EXT2_FILE_NOBLOCKIO;
+
 	/*
 	 * If the caller wants to truncate the file, we need to ask for full
 	 * write access even if the caller claims to be appending.
misc/fuse2fs.c +71 -1

92 lines not on this page · raw message

#165 lore raw

PATCH 11/19 fuse2fs: try to create loop device when ext4 device is a regular file

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

If the filesystem device is a regular file, try to create a loop device for it so that we can take advantage of iomap.

---
 configure         |   41 +++++++++++++++++++
 configure.ac      |   23 +++++++++++
 fuse4fs/fuse4fs.c |  111 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 lib/config.h.in   |    3 +
 misc/fuse2fs.c    |  112 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 5 files changed, 287 insertions(+), 3 deletions(-)
configure +41 -0
index 03b17d07771117..5db59894242aab 100755
@@ -14661,6 +14661,47 @@ printf "%s\n" "#define HAVE_FUSE_IOMAP 1" >>confdefs.h
 
 fi
 
+if test -n "$have_fuse_iomap"; then
+	{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for fuse_loopdev.h in libfuse" >&5
+printf %s "checking for fuse_loopdev.h in libfuse... " >&6; }
+	cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+	#define _GNU_SOURCE
+	#define _FILE_OFFSET_BITS	64
+	#define FUSE_USE_VERSION	399
+	#include <fuse_loopdev.h>
+
+int
+main (void)
+{
+
+
+  ;
+  return 0;
+}
+
+_ACEOF
+if ac_fn_c_try_link "$LINENO"
+then :
+  have_fuse_loopdev=yes
+	   { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: yes" >&5
+printf "%s\n" "yes" >&6; }
+else case e in #(
+  e) { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: no" >&5
+printf "%s\n" "no" >&6; } ;;
+esac
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.beam \
+    conftest$ac_exeext conftest.$ac_ext
+fi
+if test -n "$have_fuse_loopdev"
+then
+
+printf "%s\n" "#define HAVE_FUSE_LOOPDEV 1" >>confdefs.h
+
+fi
+
 have_fuse_lowlevel=
 if test -n "$FUSE_USE_VERSION"
 then
configure.ac +23 -0
index 6ccb1fdc444adc..f1bffa88b80fa4 100644
@@ -1429,6 +1429,29 @@ then
 	AC_DEFINE(HAVE_FUSE_IOMAP, 1, [Define to 1 if fuse supports iomap])
 fi
 
+dnl
+dnl Check if fuse library has fuse_loopdev.h, which it only gained after adding
+dnl iomap support.
+dnl
+if test -n "$have_fuse_iomap"; then
+	AC_MSG_CHECKING(for fuse_loopdev.h in libfuse)
+	AC_LINK_IFELSE(
+	[	AC_LANG_PROGRAM([[
+	#define _GNU_SOURCE
+	#define _FILE_OFFSET_BITS	64
+	#define FUSE_USE_VERSION	399
+	#include <fuse_loopdev.h>
+		]], [[
+		]])
+	], have_fuse_loopdev=yes
+	   AC_MSG_RESULT(yes),
+	   AC_MSG_RESULT(no))
+fi
+if test -n "$have_fuse_loopdev"
+then
+	AC_DEFINE(HAVE_FUSE_LOOPDEV, 1, [Define to 1 if fuse supports loopdev operations])
+fi
+
 dnl
 dnl Check if the FUSE lowlevel library is supported
 dnl
fuse4fs/fuse4fs.c +110 -1

185 lines not on this page · raw message

lib/config.h.in +3 -0
index 41598dcc1d7c5a..7e045b65131522 100644
@@ -82,6 +82,9 @@
 /* Define to 1 if fuse supports iomap */
 #undef HAVE_FUSE_IOMAP
 
+/* Define to 1 if fuse supports loopdev operations */
+#undef HAVE_FUSE_LOOPDEV
+
 /* Define to 1 if you have the Mac OS X function
    CFLocaleCopyPreferredLanguages in the CoreFoundation framework. */
 #undef HAVE_CFLOCALECOPYPREFERREDLANGUAGES
misc/fuse2fs.c +110 -2

186 lines not on this page · raw message

#166 lore raw

PATCH 12/19 fuse2fs: enable file IO to inline data files

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Enable file reads and writes from inline data files.

---
 fuse4fs/fuse4fs.c |    3 ++-
 misc/fuse2fs.c    |   42 ++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 42 insertions(+), 3 deletions(-)
fuse4fs/fuse4fs.c +2 -1
index 58a67252b7b613..59c8d7de7e4533 100644
@@ -6344,7 +6344,8 @@ static int fuse4fs_iomap_begin_read(struct fuse4fs *ff, ext2_ino_t ino,
 {
 	/* fall back to slow path for inline data reads */
 	if (inode->i_flags & EXT4_INLINE_DATA_FL)
-		return -ENOSYS;
+		return fuse4fs_iomap_begin_inline(ff, ino, inode, pos, count,
+						  read);
 
 	if (inode->i_flags & EXT4_EXTENTS_FL)
 		return fuse4fs_iomap_begin_extent(ff, ino, inode, pos, count,
misc/fuse2fs.c +40 -2

88 lines not on this page · raw message

#167 lore raw

PATCH 13/19 fuse2fs: set iomap-related inode flags

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Set FUSE_IFLAG_* when we do a getattr, so that all files will have iomap enabled.

---
 fuse4fs/fuse4fs.c |   46 +++++++++++++++++++++++++++++++++++-----------
 misc/fuse2fs.c    |   20 ++++++++++++++++++++
 2 files changed, 55 insertions(+), 11 deletions(-)
fuse4fs/fuse4fs.c +35 -11
index 59c8d7de7e4533..395f2fcd067633 100644
@@ -2043,6 +2043,7 @@ static void op_init(void *userdata, struct fuse_conn_info *conn)
 
 struct fuse4fs_stat {
 	struct fuse_entry_param	entry;
+	unsigned int iflags;
 };
 
 static int fuse4fs_stat_inode(struct fuse4fs *ff, ext2_ino_t ino,
@@ -2108,9 +2109,29 @@ static int fuse4fs_stat_inode(struct fuse4fs *ff, ext2_ino_t ino,
 	entry->attr_timeout = FUSE4FS_ATTR_TIMEOUT;
 	entry->entry_timeout = FUSE4FS_ATTR_TIMEOUT;
 
+	fstat->iflags = 0;
+#ifdef HAVE_FUSE_IOMAP
+	if (fuse4fs_iomap_enabled(ff))
+		fstat->iflags |= FUSE_IFLAG_IOMAP | FUSE_IFLAG_EXCLUSIVE;
+#endif
+
 	return 0;
 }
 
+#if FUSE_VERSION < FUSE_MAKE_VERSION(3, 99)
+#define fuse_reply_entry_iflags(req, entry, iflags) \
+	fuse_reply_entry((req), (entry))
+
+#define fuse_reply_attr_iflags(req, entry, iflags, timeout) \
+	fuse_reply_attr((req), (entry), (timeout))
+
+#define fuse_add_direntry_plus_iflags(req, buf, sz, name, iflags, entry, dirpos) \
+	fuse_add_direntry_plus((req), (buf), (sz), (name), (entry), (dirpos))
+
+#define fuse_reply_create_iflags(req, entry, iflags, fp) \
+	fuse_reply_create((req), (entry), (fp))
+#endif
+
 static void op_lookup(fuse_req_t req, fuse_ino_t fino, const char *name)
 {
 	struct fuse4fs_stat fstat;
@@ -2141,7 +2162,7 @@ static void op_lookup(fuse_req_t req, fuse_ino_t fino, const char *name)
 	if (ret)
 		fuse_reply_err(req, -ret);
 	else
-		fuse_reply_entry(req, &fstat.entry);
+		fuse_reply_entry_iflags(req, &fstat.entry, fstat.iflags);
 }
 
 static void op_getattr(fuse_req_t req, fuse_ino_t fino,
@@ -2161,8 +2182,8 @@ static void op_getattr(fuse_req_t req, fuse_ino_t fino,
 	if (ret)
 		fuse_reply_err(req, -ret);
 	else
-		fuse_reply_attr(req, &fstat.entry.attr,
-				fstat.entry.attr_timeout);
+		fuse_reply_attr_iflags(req, &fstat.entry.attr, fstat.iflags,
+				       fstat.entry.attr_timeout);
 }
 
 static void op_readlink(fuse_req_t req, fuse_ino_t fino)
@@ -2440,7 +2461,7 @@ static void fuse4fs_reply_entry(fuse_req_t req, ext2_ino_t ino,
 		return;
 	}
 
-	fuse_reply_entry(req, &fstat.entry);
+	fuse_reply_entry_iflags(req, &fstat.entry, fstat.iflags);
 }
 
 static void op_mknod(fuse_req_t req, fuse_ino_t fino, const char *name,
@@ -4768,10 +4789,13 @@ static int op_readdir_iter(ext2_ino_t dir EXT2FS_ATTR((unused)),
 	namebuf[dirent->name_len & 0xFF] = 0;
 
 	if (i->readdirplus) {
-		entrysize = fuse_add_direntry_plus(i->req, i->buf + i->bufused,
-						   i->bufsz - i->bufused,
-						   namebuf, &fstat.entry,
-						   i->dirpos);
+		entrysize = fuse_add_direntry_plus_iflags(i->req,
+							  i->buf + i->bufused,
+							  i->bufsz - i->bufused,
+							  namebuf,
+							  fstat.iflags,
+							  &fstat.entry,
+							  i->dirpos);
 	} else {
 		entrysize = fuse_add_direntry(i->req, i->buf + i->bufused,
 					      i->bufsz - i->bufused, namebuf,
@@ -4996,7 +5020,7 @@ static void op_create(fuse_req_t req, fuse_ino_t fino, const char *name,
 	if (ret)
 		fuse_reply_err(req, -ret);
 	else
-		fuse_reply_create(req, &fstat.entry, fp);
+		fuse_reply_create_iflags(req, &fstat.entry, fstat.iflags, fp);
 }
 
 #if FUSE_VERSION >= FUSE_MAKE_VERSION(3, 17)
@@ -5195,8 +5219,8 @@ static void op_setattr(fuse_req_t req, fuse_ino_t fino, struct stat *attr,
 	if (ret)
 		fuse_reply_err(req, -ret);
 	else
-		fuse_reply_attr(req, &fstat.entry.attr,
-				fstat.entry.attr_timeout);
+		fuse_reply_attr_iflags(req, &fstat.entry.attr, fstat.iflags,
+				       fstat.entry.attr_timeout);
 }
 
 #define FUSE4FS_MODIFIABLE_IFLAGS \
misc/fuse2fs.c +20 -0
index 5651b9633c5bb6..ed1b3068f22931 100644
@@ -1978,6 +1978,23 @@ static int op_getattr(const char *path, struct stat *statbuf,
 	return ret;
 }
 
+#if FUSE_VERSION >= FUSE_MAKE_VERSION(3, 99)
+static int op_getattr_iflags(const char *path, struct stat *statbuf,
+			     unsigned int *iflags, struct fuse_file_info *fi)
+{
+	int ret = op_getattr(path, statbuf, fi);
+
+	if (ret)
+		return ret;
+
+	if (fuse_fs_can_enable_iomap(statbuf))
+		*iflags |= FUSE_IFLAG_IOMAP | FUSE_IFLAG_EXCLUSIVE;
+
+	return 0;
+}
+#endif
+
+
 static int op_readlink(const char *path, char *buf, size_t len)
 {
 	struct fuse2fs *ff = fuse2fs_get();
@@ -6664,6 +6681,9 @@ static struct fuse_operations fs_ops = {
 #ifdef SUPPORT_FALLOCATE
 	.fallocate = op_fallocate,
 #endif
+#if FUSE_VERSION >= FUSE_MAKE_VERSION(3, 99)
+	.getattr_iflags = op_getattr_iflags,
+#endif
 #ifdef HAVE_FUSE_IOMAP
 	.iomap_begin = op_iomap_begin,
 	.iomap_end = op_iomap_end,
#168 lore raw

PATCH 14/19 fuse2fs: configure block device block size

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Set the blocksize of the block device to the filesystem blocksize. This prevents the bdev pagecache from caching file data blocks that iomap will read and write directly. Cache duplication is dangerous.

---
 fuse4fs/fuse4fs.c |   43 +++++++++++++++++++++++++++++++++++++++++++
 misc/fuse2fs.c    |   43 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 86 insertions(+)
fuse4fs/fuse4fs.c +43 -0
index 395f2fcd067633..9dd694be943255 100644
@@ -6669,6 +6669,45 @@ static off_t fuse4fs_max_size(struct fuse4fs *ff, off_t upper_limit)
 	return res;
 }
 
+/*
+ * Set the block device's blocksize to the fs blocksize.
+ *
+ * This is required to avoid creating uptodate bdev pagecache that aliases file
+ * data blocks because iomap reads and writes directly to file data blocks.
+ */
+static int fuse4fs_set_bdev_blocksize(struct fuse4fs *ff, int fd)
+{
+	int blocksize = ff->fs->blocksize;
+	int set_error;
+	int ret;
+
+	ret = ioctl(fd, BLKBSZSET, &blocksize);
+	if (!ret)
+		return 0;
+
+	/*
+	 * Save the original errno so we can report that if the block device
+	 * blocksize isn't set in an agreeable way.
+	 */
+	set_error = errno;
+
+	ret = ioctl(fd, BLKBSZGET, &blocksize);
+	if (ret)
+		goto out_bad;
+
+	/* Pretend that BLKBSZSET rejected our proposed block size */
+	if (blocksize > ff->fs->blocksize) {
+		set_error = EINVAL;
+		goto out_bad;
+	}
+
+	return 0;
+out_bad:
+	err_printf(ff, "%s: cannot set blocksize %u: %s\n", __func__,
+		   blocksize, strerror(set_error));
+	return -EIO;
+}
+
 static int fuse4fs_iomap_config_devices(struct fuse4fs *ff)
 {
 	errcode_t err;
@@ -6679,6 +6718,10 @@ static int fuse4fs_iomap_config_devices(struct fuse4fs *ff)
 	if (err)
 		return translate_error(ff->fs, 0, err);
 
+	ret = fuse4fs_set_bdev_blocksize(ff, fd);
+	if (ret)
+		return ret;
+
 	ret = fuse_lowlevel_iomap_device_add(ff->fuse, fd, 0);
 	if (ret < 0) {
 		dbg_printf(ff, "%s: cannot register iomap dev fd=%d, err=%d\n",
misc/fuse2fs.c +43 -0

55 lines not on this page · raw message

#169 lore raw

PATCH 15/19 fuse4fs: separate invalidation

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Use the new stuff

---
 fuse4fs/fuse4fs.c |   61 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 misc/fuse2fs.c    |   60 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 121 insertions(+)
fuse4fs/fuse4fs.c +61 -0
index 9dd694be943255..90b365149910c9 100644
@@ -281,6 +281,9 @@ struct fuse4fs {
 	enum fuse4fs_iomap_state iomap_state;
 	uint32_t iomap_dev;
 	uint64_t iomap_cap;
+	void (*old_alloc_stats)(ext2_filsys fs, blk64_t blk, int inuse);
+	void (*old_alloc_stats_range)(ext2_filsys fs, blk64_t blk, blk_t num,
+				      int inuse);
 #endif
 	unsigned int blockmask;
 	unsigned long offset;
@@ -6736,6 +6739,51 @@ static int fuse4fs_iomap_config_devices(struct fuse4fs *ff)
 	return 0;
 }
 
+static void fuse4fs_invalidate_bdev(struct fuse4fs *ff, blk64_t blk, blk_t num)
+{
+	off_t offset = FUSE4FS_FSB_TO_B(ff, blk);
+	off_t length = FUSE4FS_FSB_TO_B(ff, num);
+	int ret;
+
+	ret = fuse_lowlevel_iomap_device_invalidate(ff->fuse, ff->iomap_dev,
+						    offset, length);
+	if (!ret)
+		return;
+
+	if (num == 1)
+		err_printf(ff, "%s %llu: %s\n",
+			   _("error invalidating block"),
+			   (unsigned long long)blk,
+			   strerror(ret));
+	else
+		err_printf(ff, "%s %llu-%llu: %s\n",
+			   _("error invalidating blocks"),
+			   (unsigned long long)blk,
+			   (unsigned long long)blk + num - 1,
+			   strerror(ret));
+}
+
+static void fuse4fs_alloc_stats(ext2_filsys fs, blk64_t blk, int inuse)
+{
+	struct fuse4fs *ff = fs->priv_data;
+
+	if (inuse < 0)
+		fuse4fs_invalidate_bdev(ff, blk, 1);
+	if (ff->old_alloc_stats)
+		ff->old_alloc_stats(fs, blk, inuse);
+}
+
+static void fuse4fs_alloc_stats_range(ext2_filsys fs, blk64_t blk, blk_t num,
+				      int inuse)
+{
+	struct fuse4fs *ff = fs->priv_data;
+
+	if (inuse < 0)
+		fuse4fs_invalidate_bdev(ff, blk, num);
+	if (ff->old_alloc_stats_range)
+		ff->old_alloc_stats_range(fs, blk, num, inuse);
+}
+
 static void op_iomap_config(fuse_req_t req, uint64_t flags, uint64_t maxbytes)
 {
 	struct fuse_iomap_config cfg = { };
@@ -6780,6 +6828,19 @@ static void op_iomap_config(fuse_req_t req, uint64_t flags, uint64_t maxbytes)
 	if (ret)
 		goto out_unlock;
 
+	/*
+	 * If we let iomap do all file block IO, then we need to watch for
+	 * freed blocks so that we can invalidate any page cache that might
+	 * get written to the block deivce.
+	 */
+	if (fuse4fs_iomap_enabled(ff)) {
+		ext2fs_set_block_alloc_stats_callback(ff->fs,
+				fuse4fs_alloc_stats, &ff->old_alloc_stats);
+		ext2fs_set_block_alloc_stats_range_callback(ff->fs,
+				fuse4fs_alloc_stats_range,
+				&ff->old_alloc_stats_range);
+	}
+
 out_unlock:
 	fuse4fs_finish(ff, ret);
 	if (ret)
misc/fuse2fs.c +60 -0

78 lines not on this page · raw message

#170 lore raw

PATCH 16/19 fuse2fs: implement statx

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Implement statx.

---
 fuse4fs/fuse4fs.c |  136 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 misc/fuse2fs.c    |  131 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 267 insertions(+)
fuse4fs/fuse4fs.c +136 -0
index 90b365149910c9..62ab8f618015e9 100644
@@ -24,6 +24,7 @@
 #include <sys/xattr.h>
 #endif
 #include <sys/ioctl.h>
+#include <sys/sysmacros.h>
 #include <unistd.h>
 #include <ctype.h>
 #include <assert.h>
@@ -2189,6 +2190,138 @@ static void op_getattr(fuse_req_t req, fuse_ino_t fino,
 				       fstat.entry.attr_timeout);
 }
 
+#if FUSE_VERSION >= FUSE_MAKE_VERSION(3, 18) && defined(STATX_BASIC_STATS)
+static inline void fuse4fs_set_statx_attr(struct statx *stx,
+					  uint64_t statx_flag, int set)
+{
+	if (set)
+		stx->stx_attributes |= statx_flag;
+	stx->stx_attributes_mask |= statx_flag;
+}
+
+static void fuse4fs_statx_directio(struct fuse4fs *ff, struct statx *stx)
+{
+	struct statx devx;
+	errcode_t err;
+	int fd;
+
+	err = io_channel_get_fd(ff->fs->io, &fd);
+	if (err)
+		return;
+
+	err = statx(fd, "", AT_EMPTY_PATH, STATX_DIOALIGN, &devx);
+	if (err)
+		return;
+	if (!(devx.stx_mask & STATX_DIOALIGN))
+		return;
+
+	stx->stx_mask |= STATX_DIOALIGN;
+	stx->stx_dio_mem_align = devx.stx_dio_mem_align;
+	stx->stx_dio_offset_align = devx.stx_dio_offset_align;
+}
+
+static int fuse4fs_statx(struct fuse4fs *ff, ext2_ino_t ino, int statx_mask,
+			 struct statx *stx)
+{
+	struct ext2_inode_large inode;
+	ext2_filsys fs = ff->fs;;
+	dev_t fakedev = 0;
+	errcode_t err;
+	struct timespec tv;
+
+	err = fuse4fs_read_inode(fs, ino, &inode);
+	if (err)
+		return translate_error(fs, ino, err);
+
+	memcpy(&fakedev, fs->super->s_uuid, sizeof(fakedev));
+	stx->stx_mask = STATX_BASIC_STATS;
+	stx->stx_dev_major = major(fakedev);
+	stx->stx_dev_minor = minor(fakedev);
+	stx->stx_ino = ino;
+	stx->stx_mode = inode.i_mode;
+	stx->stx_nlink = inode.i_links_count;
+	stx->stx_uid = inode_uid(inode);
+	stx->stx_gid = inode_gid(inode);
+	stx->stx_size = EXT2_I_SIZE(&inode);
+	stx->stx_blksize = fs->blocksize;
+	stx->stx_blocks = ext2fs_get_stat_i_blocks(fs,
+						EXT2_INODE(&inode));
+	EXT4_INODE_GET_XTIME(i_atime, &tv, &inode);
+	stx->stx_atime.tv_sec = tv.tv_sec;
+	stx->stx_atime.tv_nsec = tv.tv_nsec;
+
+	EXT4_INODE_GET_XTIME(i_mtime, &tv, &inode);
+	stx->stx_mtime.tv_sec = tv.tv_sec;
+	stx->stx_mtime.tv_nsec = tv.tv_nsec;
+
+	EXT4_INODE_GET_XTIME(i_ctime, &tv, &inode);
+	stx->stx_ctime.tv_sec = tv.tv_sec;
+	stx->stx_ctime.tv_nsec = tv.tv_nsec;
+
+	if (EXT4_FITS_IN_INODE(&inode, i_crtime)) {
+		stx->stx_mask |= STATX_BTIME;
+		EXT4_INODE_GET_XTIME(i_crtime, &tv, &inode);
+		stx->stx_btime.tv_sec = tv.tv_sec;
+		stx->stx_btime.tv_nsec = tv.tv_nsec;
+	}
+
+	dbg_printf(ff, "%s: ino=%d atime=%lld.%d mtime=%lld.%d ctime=%lld.%d btime=%lld.%d\n",
+		   __func__, ino,
+		   (long long int)stx->stx_atime.tv_sec, stx->stx_atime.tv_nsec,
+		   (long long int)stx->stx_mtime.tv_sec, stx->stx_mtime.tv_nsec,
+		   (long long int)stx->stx_ctime.tv_sec, stx->stx_ctime.tv_nsec,
+		   (long long int)stx->stx_btime.tv_sec, stx->stx_btime.tv_nsec);
+
+	if (LINUX_S_ISCHR(inode.i_mode) ||
+	    LINUX_S_ISBLK(inode.i_mode)) {
+		if (inode.i_block[0]) {
+			stx->stx_rdev_major = major(inode.i_block[0]);
+			stx->stx_rdev_minor = minor(inode.i_block[0]);
+		} else {
+			stx->stx_rdev_major = major(inode.i_block[1]);
+			stx->stx_rdev_minor = minor(inode.i_block[1]);
+		}
+	}
+
+	fuse4fs_set_statx_attr(stx, STATX_ATTR_COMPRESSED,
+			       inode.i_flags & EXT2_COMPR_FL);
+	fuse4fs_set_statx_attr(stx, STATX_ATTR_IMMUTABLE,
+			       inode.i_flags & EXT2_IMMUTABLE_FL);
+	fuse4fs_set_statx_attr(stx, STATX_ATTR_APPEND,
+			       inode.i_flags & EXT2_APPEND_FL);
+	fuse4fs_set_statx_attr(stx, STATX_ATTR_NODUMP,
+			       inode.i_flags & EXT2_NODUMP_FL);
+
+	fuse4fs_statx_directio(ff, stx);
+
+	return 0;
+}
+
+static void op_statx(fuse_req_t req, fuse_ino_t fino, int flags, int mask,
+		     struct fuse_file_info *fi)
+{
+	struct statx stx = { };
+	struct fuse4fs *ff = fuse4fs_get(req);
+	ext2_ino_t ino;
+	int ret = 0;
+
+	FUSE4FS_CHECK_CONTEXT(req);
+	FUSE4FS_CONVERT_FINO(req, &ino, fino);
+	fuse4fs_start(ff);
+	ret = fuse4fs_statx(ff, ino, mask, &stx);
+	if (ret)
+		goto out;
+out:
+	fuse4fs_finish(ff, ret);
+	if (ret)
+		fuse_reply_err(req, -ret);
+	else
+		fuse_reply_statx(req, 0, &stx, FUSE4FS_ATTR_TIMEOUT);
+}
+#else
+# define op_statx		NULL
+#endif
+
 static void op_readlink(fuse_req_t req, fuse_ino_t fino)
 {
 	struct ext2_inode inode;
@@ -7260,6 +7393,9 @@ static struct fuse_lowlevel_ops fs_ops = {
 #ifdef SUPPORT_FALLOCATE
 	.fallocate = op_fallocate,
 #endif
+#if FUSE_VERSION >= FUSE_MAKE_VERSION(3, 18)
+	.statx = op_statx,
+#endif
 #ifdef HAVE_FUSE_IOMAP
 	.iomap_begin = op_iomap_begin,
 	.iomap_end = op_iomap_end,
misc/fuse2fs.c +131 -0

149 lines not on this page · raw message

#171 lore raw

PATCH 17/19 fuse2fs: enable atomic writes

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Advertise the single-fsblock atomic write capability that iomap can do.

---
 fuse4fs/fuse4fs.c |   67 +++++++++++++++++++++++++++++++++++++++++++++++++++
 misc/fuse2fs.c    |   69 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 134 insertions(+), 2 deletions(-)
fuse4fs/fuse4fs.c +66 -1
index 62ab8f618015e9..77207bae19e544 100644
@@ -285,6 +285,9 @@ struct fuse4fs {
 	void (*old_alloc_stats)(ext2_filsys fs, blk64_t blk, int inuse);
 	void (*old_alloc_stats_range)(ext2_filsys fs, blk64_t blk, blk_t num,
 				      int inuse);
+#ifdef STATX_WRITE_ATOMIC
+	unsigned int awu_min, awu_max;
+#endif
 #endif
 	unsigned int blockmask;
 	unsigned long offset;
@@ -910,10 +913,22 @@ static inline bool fuse4fs_can_iomap(const struct fuse4fs *ff)
 {
 	return ff->iomap_cap & FUSE_IOMAP_SUPPORT_FILEIO;
 }
+
+static inline bool fuse4fs_iomap_supports_hw_atomic(const struct fuse4fs *ff)
+{
+	return fuse4fs_iomap_enabled(ff) &&
+	       (ff->iomap_cap & FUSE_IOMAP_SUPPORT_ATOMIC) &&
+#ifdef STATX_WRITE_ATOMIC
+		ff->awu_min > 0 && ff->awu_min > 0;
+#else
+		0;
+#endif
+}
 #else
 # define fuse4fs_iomap_enabled(...)	(0)
 # define fuse4fs_discover_iomap(...)	((void)0)
 # define fuse4fs_can_iomap(...)		(false)
+# define fuse4fs_iomap_supports_hw_atomic(...)	(0)
 #endif
 
 static inline void fuse4fs_dump_extents(struct fuse4fs *ff, ext2_ino_t ino,
@@ -2115,8 +2130,12 @@ static int fuse4fs_stat_inode(struct fuse4fs *ff, ext2_ino_t ino,
 
 	fstat->iflags = 0;
 #ifdef HAVE_FUSE_IOMAP
-	if (fuse4fs_iomap_enabled(ff))
+	if (fuse4fs_iomap_enabled(ff)) {
 		fstat->iflags |= FUSE_IFLAG_IOMAP | FUSE_IFLAG_EXCLUSIVE;
+
+		if (fuse4fs_iomap_supports_hw_atomic(ff))
+			fstat->iflags |= FUSE_IFLAG_ATOMIC;
+	}
 #endif
 
 	return 0;
@@ -2294,6 +2313,15 @@ static int fuse4fs_statx(struct fuse4fs *ff, ext2_ino_t ino, int statx_mask,
 
 	fuse4fs_statx_directio(ff, stx);
 
+#ifdef STATX_WRITE_ATOMIC
+	if (fuse4fs_iomap_supports_hw_atomic(ff)) {
+		stx->stx_mask |= STATX_WRITE_ATOMIC;
+		stx->stx_atomic_write_unit_min = ff->awu_min;
+		stx->stx_atomic_write_unit_max = ff->awu_max;
+		stx->stx_atomic_write_segments_max = 1;
+	}
+#endif
+
 	return 0;
 }
 
@@ -6680,6 +6708,9 @@ static void op_iomap_begin(fuse_req_t req, fuse_ino_t fino, uint64_t dontcare,
 		}
 	}
 
+	if (opflags & FUSE_IOMAP_OP_ATOMIC)
+		read.flags |= FUSE_IOMAP_F_ATOMIC_BIO;
+
 out_unlock:
 	fuse4fs_finish(ff, ret);
 	if (ret)
@@ -6844,6 +6875,38 @@ static int fuse4fs_set_bdev_blocksize(struct fuse4fs *ff, int fd)
 	return -EIO;
 }
 
+#ifdef STATX_WRITE_ATOMIC
+static void fuse4fs_configure_atomic_write(struct fuse4fs *ff, int bdev_fd)
+{
+	struct statx devx;
+	unsigned int awu_min, awu_max;
+	int ret;
+
+	if (!ext2fs_has_feature_extents(ff->fs->super))
+		return;
+
+	ret = statx(bdev_fd, "", AT_EMPTY_PATH, STATX_WRITE_ATOMIC, &devx);
+	if (ret)
+		return;
+	if (!(devx.stx_mask & STATX_WRITE_ATOMIC))
+		return;
+
+	awu_min = max(ff->fs->blocksize, devx.stx_atomic_write_unit_min);
+	awu_max = min(ff->fs->blocksize, devx.stx_atomic_write_unit_max);
+	if (awu_min > awu_max)
+		return;
+
+	log_printf(ff, "%s awu_min: %u, awu_max: %u\n",
+		   _("Supports (experimental) DIO atomic writes"),
+		   awu_min, awu_max);
+
+	ff->awu_min = awu_min;
+	ff->awu_max = awu_max;
+}
+#else
+# define fuse4fs_configure_atomic_write(...)	((void)0)
+#endif
+
 static int fuse4fs_iomap_config_devices(struct fuse4fs *ff)
 {
 	errcode_t err;
@@ -6868,6 +6931,8 @@ static int fuse4fs_iomap_config_devices(struct fuse4fs *ff)
 	dbg_printf(ff, "%s: registered iomap dev fd=%d iomap_dev=%u\n",
 		   __func__, fd, ff->iomap_dev);
 
+	fuse4fs_configure_atomic_write(ff, fd);
+
 	ff->iomap_dev = ret;
 	return 0;
 }
misc/fuse2fs.c +68 -1

122 lines not on this page · raw message

#172 lore raw

PATCH 18/19 fuse4fs: disable fs reclaim and write throttling

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Ask the kernel if we can disable fs reclaim and write throttling.

Disabling fs reclaim prevents livelocks where the fuse server can allocate memory, fault into the kernel, and then the allocation tries to initiate writeback by calling back into the same fuse server.

Disabling BDI write throttling means that writeback won't be throttled by metadata writes to the filesystem.

---
 fuse4fs/fuse4fs.c |   33 ++++++++++++++++++++++++++++++---
 1 file changed, 30 insertions(+), 3 deletions(-)
fuse4fs/fuse4fs.c +30 -3
index 77207bae19e544..4499f4083f85dd 100644
@@ -7737,6 +7737,19 @@ static void try_set_io_flusher(struct fuse4fs *ff)
 #endif
 }
 
+/* Undo try_set_io_flusher */
+static void try_clear_io_flusher(struct fuse4fs *ff)
+{
+#ifdef HAVE_PR_SET_IO_FLUSHER
+	/*
+	 * zero ret means it's already set, negative means we can't even
+	 * look at the value so don't bother clearing it
+	 */
+	if (prctl(PR_GET_IO_FLUSHER, 0, 0, 0, 0) > 0)
+		prctl(PR_SET_IO_FLUSHER, 0, 0, 0, 0);
+#endif
+}
+
 /* Try to adjust the OOM score so that we don't get killed */
 static void try_adjust_oom_score(struct fuse4fs *ff)
 {
@@ -7842,12 +7855,27 @@ static int fuse4fs_main(struct fuse_args *args, struct fuse4fs *ff)
 	fuse_loop_cfg_set_idle_threads(loop_config, opts.max_idle_threads);
 	fuse_loop_cfg_set_max_threads(loop_config, 4);
 
+	/*
+	 * Try to set ourselves up with fs reclaim disabled to prevent
+	 * recursive reclaim and throttling.  This must be done before starting
+	 * the worker threads so that they inherit the process flags.
+	 */
+	ret = fuse_lowlevel_disable_fsreclaim(ff->fuse, 1);
+	if (ret) {
+		err_printf(ff, "%s: %s.\n",
+ _("Could not register as FS flusher thread"),
+			   strerror(errno));
+		try_set_io_flusher(ff);
+		ret = 0;
+	}
+
 	if (fuse_session_loop_mt(se, loop_config) != 0) {
 		ret = 8;
-		goto out_loopcfg;
+		goto out_flusher;
 	}
 
-out_loopcfg:
+out_flusher:
+	try_clear_io_flusher(ff);
 	fuse_loop_cfg_destroy(loop_config);
 out_remove_signal_handlers:
 	fuse_remove_signal_handlers(se);
@@ -7925,7 +7953,6 @@ int main(int argc, char *argv[])
 		goto out;
 	}
 
-	try_set_io_flusher(&fctx);
 	try_adjust_oom_score(&fctx);
 
 	/* Will we allow users to allocate every last block? */
#173 lore raw

PATCH 19/19 fuse2fs: implement freeze and shutdown requests

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Handle freezing and shutting down the filesystem if requested.

---
 fuse4fs/fuse4fs.c |   91 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 misc/fuse2fs.c    |   84 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 175 insertions(+)
fuse4fs/fuse4fs.c +91 -0
index 4499f4083f85dd..170accabfd9fd6 100644
@@ -228,6 +228,7 @@ struct fuse4fs_file_handle {
 
 enum fuse4fs_opstate {
 	F4OP_READONLY,
+	F4OP_WRITABLE_FROZEN,
 	F4OP_WRITABLE,
 	F4OP_SHUTDOWN,
 };
@@ -6166,6 +6167,91 @@ static void op_fallocate(fuse_req_t req, fuse_ino_t fino EXT2FS_ATTR((unused)),
 }
 #endif /* SUPPORT_FALLOCATE */
 
+#if FUSE_VERSION >= FUSE_MAKE_VERSION(3, 99)
+static void op_freezefs(fuse_req_t req, fuse_ino_t ino, uint64_t unlinked)
+{
+	struct fuse4fs *ff = fuse4fs_get(req);
+	ext2_filsys fs;
+	errcode_t err;
+	int ret = 0;
+
+	FUSE4FS_CHECK_CONTEXT(req);
+	fs = fuse4fs_start(ff);
+
+	if (ff->opstate == F4OP_WRITABLE) {
+		if (fs->super->s_error_count)
+			fs->super->s_state |= EXT2_ERROR_FS;
+		else if (!unlinked)
+			fs->super->s_state |= EXT2_VALID_FS;
+		ext2fs_mark_super_dirty(fs);
+		err = ext2fs_set_gdt_csum(fs);
+		if (err) {
+			ret = translate_error(fs, 0, err);
+			goto out_unlock;
+		}
+
+		err = ext2fs_flush2(fs, 0);
+		if (err) {
+			ret = translate_error(fs, 0, err);
+			goto out_unlock;
+		}
+
+		ff->opstate = F4OP_WRITABLE_FROZEN;
+	}
+
+out_unlock:
+	fs->super->s_state &= ~EXT2_VALID_FS;
+	fuse4fs_finish(ff, ret);
+	fuse_reply_err(req, -ret);
+}
+
+static void op_unfreezefs(fuse_req_t req, fuse_ino_t ino)
+{
+	struct fuse4fs *ff = fuse4fs_get(req);
+	ext2_filsys fs;
+	errcode_t err;
+	int ret = 0;
+
+	FUSE4FS_CHECK_CONTEXT(req);
+	fs = fuse4fs_start(ff);
+
+	if (ff->opstate == F4OP_WRITABLE_FROZEN) {
+		if (fs->super->s_error_count)
+			fs->super->s_state |= EXT2_ERROR_FS;
+		fs->super->s_state &= ~EXT2_VALID_FS;
+		ext2fs_mark_super_dirty(fs);
+		err = ext2fs_set_gdt_csum(fs);
+		if (err) {
+			ret = translate_error(fs, 0, err);
+			goto out_unlock;
+		}
+
+		err = ext2fs_flush2(fs, 0);
+		if (err) {
+			ret = translate_error(fs, 0, err);
+			goto out_unlock;
+		}
+
+		ff->opstate = F4OP_WRITABLE;
+	}
+
+out_unlock:
+	fuse4fs_finish(ff, ret);
+	fuse_reply_err(req, -ret);
+}
+
+static void op_shutdownfs(fuse_req_t req, fuse_ino_t ino, uint64_t flags)
+{
+	const struct fuse_ctx *ctxt = fuse_req_ctx(req);
+	struct fuse4fs *ff = fuse4fs_get(req);
+	int ret;
+
+	ret = ioctl_shutdown(ff, ctxt, NULL, NULL, 0);
+
+	fuse_reply_err(req, -ret);
+}
+#endif
+
 #ifdef HAVE_FUSE_IOMAP
 static void fuse4fs_iomap_hole(struct fuse4fs *ff, struct fuse_file_iomap *iomap,
 			       off_t pos, uint64_t count)
@@ -7461,6 +7547,11 @@ static struct fuse_lowlevel_ops fs_ops = {
 #if FUSE_VERSION >= FUSE_MAKE_VERSION(3, 18)
 	.statx = op_statx,
 #endif
+#if FUSE_VERSION >= FUSE_MAKE_VERSION(3, 99)
+	.freezefs = op_freezefs,
+	.unfreezefs = op_unfreezefs,
+	.shutdownfs = op_shutdownfs,
+#endif
 #ifdef HAVE_FUSE_IOMAP
 	.iomap_begin = op_iomap_begin,
 	.iomap_end = op_iomap_end,
misc/fuse2fs.c +84 -0

102 lines not on this page · raw message

#174 lore raw

PATCH v7 2/8 fuse4fs: specify the root node id

In reply to Darrick J. Wong

Hi all,

If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.

Comments and questions are, as always, welcome.

e2fsprogs git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/e2fsprogs.git/log/?h=fuse2fs-root-nodeid
---
Commits in this patchset:
* fuse4fs: don't use inode number translation when possible

---
 fuse4fs/fuse4fs.c |   30 ++++++++++++++++++++++++------
 1 file changed, 24 insertions(+), 6 deletions(-)
#175 lore raw

PATCH 1/1 fuse4fs: don't use inode number translation when possible

From: Darrick J. Wong <[email protected]>

Prior to the integration of iomap into fuse, the fuse client (aka the kernel) required that the root directory have an inumber of FUSE_ROOT_ID, which is 1. However, the ext2 filesystem defines the root inode number to be EXT2_ROOT_INO, which is 2. This dissonance means that we have to have translator functions, and that any access to inumber 1 (the ext2 badblocks file) will instead redirect to the root directory.

That's horrible. Use the new mount option to set the root directory nodeid to EXT2_ROOT_INO so that we don't need this translation.

---
 fuse4fs/fuse4fs.c |   30 ++++++++++++++++++++++++------
 1 file changed, 24 insertions(+), 6 deletions(-)
fuse4fs/fuse4fs.c +24 -6
index 170accabfd9fd6..bebc2410af382e 100644
@@ -273,6 +273,7 @@ struct fuse4fs {
 	int directio;
 	int acl;
 	int dirsync;
+	int translate_inums;
 
 	enum fuse4fs_opstate opstate;
 	int logfd;
@@ -345,17 +346,19 @@ struct fuse4fs {
 #define FUSE4FS_CHECK_CONTEXT_INIT(req) \
 	__FUSE4FS_CHECK_CONTEXT((req), abort(), abort())
 
-static inline void fuse4fs_ino_from_fuse(ext2_ino_t *inop, fuse_ino_t fino)
+static inline void fuse4fs_ino_from_fuse(const struct fuse4fs *ff,
+					 ext2_ino_t *inop, fuse_ino_t fino)
 {
-	if (fino == FUSE_ROOT_ID)
+	if (ff->translate_inums && fino == FUSE_ROOT_ID)
 		*inop = EXT2_ROOT_INO;
 	else
 		*inop = fino;
 }
 
-static inline void fuse4fs_ino_to_fuse(fuse_ino_t *finop, ext2_ino_t ino)
+static inline void fuse4fs_ino_to_fuse(const struct fuse4fs *ff,
+				       fuse_ino_t *finop, ext2_ino_t ino)
 {
-	if (ino == EXT2_ROOT_INO)
+	if (ff->translate_inums && ino == EXT2_ROOT_INO)
 		*finop = FUSE_ROOT_ID;
 	else
 		*finop = ino;
@@ -371,7 +374,7 @@ static inline void fuse4fs_ino_to_fuse(fuse_ino_t *finop, ext2_ino_t ino)
 			fuse_reply_err((req), EIO); \
 			return; \
 		} \
-		fuse4fs_ino_from_fuse(ext2_inop, fuse_ino); \
+		fuse4fs_ino_from_fuse(fuse4fs_get(req), ext2_inop, fuse_ino); \
 	} while (0)
 
 static int __translate_error(ext2_filsys fs, ext2_ino_t ino, errcode_t err,
@@ -2124,7 +2127,7 @@ static int fuse4fs_stat_inode(struct fuse4fs *ff, ext2_ino_t ino,
 			statbuf->st_rdev = inodep->i_block[1];
 	}
 
-	fuse4fs_ino_to_fuse(&entry->ino, ino);
+	fuse4fs_ino_to_fuse(ff, &entry->ino, ino);
 	entry->generation = inodep->i_generation;
 	entry->attr_timeout = FUSE4FS_ATTR_TIMEOUT;
 	entry->entry_timeout = FUSE4FS_ATTR_TIMEOUT;
@@ -7793,6 +7796,20 @@ static void fuse4fs_compute_libfuse_args(struct fuse4fs *ff,
  "-oallow_other,default_permissions,suid,dev");
 	}
 
+	if (fuse4fs_can_iomap(ff)) {
+		/*
+		 * The root_nodeid mount option was added when iomap support
+		 * was added to fuse.  This enables us to control the root
+		 * nodeid in the kernel, which enables a 1:1 translation of
+		 * ext2 to kernel inumbers.
+		 */
+		snprintf(extra_args, BUFSIZ, "-oroot_nodeid=%d",
+			 EXT2_ROOT_INO);
+		fuse_opt_add_arg(args, extra_args);
+		ff->translate_inums = 0;
+	}
+
+
 	if (ff->debug) {
 		int	i;
 
@@ -7998,6 +8015,7 @@ int main(int argc, char *argv[])
 #ifdef HAVE_FUSE_LOOPDEV
 		.loop_fd = -1,
 #endif
+		.translate_inums = 1,
 	};
 	errcode_t err;
 	FILE *orig_stderr = stderr;
#176 lore raw

PATCH v7 3/8 fuse2fs: handle timestamps and ACLs correctly when iomap is enabled

In reply to Darrick J. Wong

Hi all,

When iomap is enabled for a fuse file, we try to keep as much of the file IO path in the kernel as we possibly can. That means no calling out to the fuse server in the IO path when we can avoid it. However, the existing FUSE architecture defers all file attributes to the fuse server -- [cm]time updates, ACL metadata management, set[ug]id removal, and permissions checking thereof, etc.

We'd really rather do all these attribute updates in the kernel, and only push them to the fuse server when it's actually necessary (e.g. fsync). Furthermore, the POSIX ACL code has the weird behavior that if the access ACL can be represented entirely by i_mode bits, it will change the mode and delete the ACL, which fuse servers generally don't seem to implement.

IOWs, we want consistent and correct (as defined by fstests) behavior of file attributes in iomap mode. Let's make the kernel manage all that and push the results to userspace as needed. This improves performance even further, since it's sort of like writeback_cache mode but more aggressive.

If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.

Comments and questions are, as always, welcome.

e2fsprogs git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/e2fsprogs.git/log/?h=fuse2fs-iomap-attrs
---
Commits in this patchset:
* fuse2fs: add strictatime/lazytime mount options
* fuse2fs: skip permission checking on utimens when iomap is enabled
* fuse2fs: let the kernel tell us about acl/mode updates
* fuse2fs: better debugging for file mode updates
* fuse2fs: debug timestamp updates
* fuse2fs: use coarse timestamps for iomap mode
* fuse2fs: add tracing for retrieving timestamps
* fuse2fs: enable syncfs
* fuse2fs: set sync, immutable, and append at file load time
* fuse4fs: increase attribute timeout in iomap mode

---
 fuse4fs/fuse4fs.1.in |    6 +
 fuse4fs/fuse4fs.c    |  226 ++++++++++++++++++++++++++++++----------
 misc/fuse2fs.1.in    |    6 +
 misc/fuse2fs.c       |  282 +++++++++++++++++++++++++++++++++++++-------------
 4 files changed, 389 insertions(+), 131 deletions(-)
#177 lore raw

PATCH 1/10 fuse2fs: add strictatime/lazytime mount options

From: Darrick J. Wong <[email protected]>

In iomap mode, we can support the strictatime/lazytime mount options. Add them to fuse2fs.

---
 fuse4fs/fuse4fs.1.in |    6 ++++++
 fuse4fs/fuse4fs.c    |   28 +++++++++++++++++++++++++++-
 misc/fuse2fs.1.in    |    6 ++++++
 misc/fuse2fs.c       |   27 +++++++++++++++++++++++++++
 4 files changed, 66 insertions(+), 1 deletion(-)
fuse4fs/fuse4fs.1.in +6 -0
index 8855867d27101d..119cbcc903d8af 100644
@@ -90,6 +90,9 @@ .SS "fuse4fs options:"
 .I nosuid
 ) later.
 .TP
+\fB-o\fR lazytime
+if iomap is enabled, enable lazy updates of timestamps
+.TP
 \fB-o\fR lockfile=path
 use this file to control access to the filesystem
 .TP
@@ -98,6 +101,9 @@ .SS "fuse4fs options:"
 .TP
 \fB-o\fR norecovery
 do not replay the journal and mount the file system read-only
+.TP
+\fB-o\fR strictatime
+if iomap is enabled, update atime on every access
 .SS "FUSE options:"
 .TP
 \fB-d -o\fR debug
fuse4fs/fuse4fs.c +27 -1
index bebc2410af382e..428e4cb404cb45 100644
@@ -274,6 +274,7 @@ struct fuse4fs {
 	int acl;
 	int dirsync;
 	int translate_inums;
+	int iomap_passthrough_options;
 
 	enum fuse4fs_opstate opstate;
 	int logfd;
@@ -1376,6 +1377,12 @@ static errcode_t fuse4fs_check_support(struct fuse4fs *ff)
 		return EXT2_ET_FILESYSTEM_CORRUPTED;
 	}
 
+	if (ff->iomap_passthrough_options && !fuse4fs_can_iomap(ff)) {
+		err_printf(ff, "%s\n",
+			   _("Some mount options require iomap."));
+		return EINVAL;
+	}
+
 	return 0;
 }
 
@@ -2005,6 +2012,8 @@ static void fuse4fs_iomap_enable(struct fuse_conn_info *conn,
 	if (!fuse4fs_iomap_enabled(ff)) {
 		if (ff->iomap_want == FT_ENABLE)
 			err_printf(ff, "%s\n", _("Could not enable iomap."));
+		if (ff->iomap_passthrough_options)
+			err_printf(ff, "%s\n", _("Some mount options require iomap."));
 		return;
 	}
 }
@@ -7590,6 +7599,7 @@ enum {
 	FUSE4FS_ERRORS_BEHAVIOR,
 #ifdef HAVE_FUSE_IOMAP
 	FUSE4FS_IOMAP,
+	FUSE4FS_IOMAP_PASSTHROUGH,
 #endif
 };
 
@@ -7616,6 +7626,17 @@ static struct fuse_opt fuse4fs_opts[] = {
 	FUSE4FS_OPT("timing",		timing,			1),
 #endif
 
+#ifdef HAVE_FUSE_IOMAP
+#ifdef MS_LAZYTIME
+	FUSE_OPT_KEY("lazytime",	FUSE4FS_IOMAP_PASSTHROUGH),
+	FUSE_OPT_KEY("nolazytime",	FUSE4FS_IOMAP_PASSTHROUGH),
+#endif
+#ifdef MS_STRICTATIME
+	FUSE_OPT_KEY("strictatime",	FUSE4FS_IOMAP_PASSTHROUGH),
+	FUSE_OPT_KEY("nostrictatime",	FUSE4FS_IOMAP_PASSTHROUGH),
+#endif
+#endif
+
 	FUSE_OPT_KEY("user_xattr",	FUSE4FS_IGNORED),
 	FUSE_OPT_KEY("noblock_validity", FUSE4FS_IGNORED),
 	FUSE_OPT_KEY("nodelalloc",	FUSE4FS_IGNORED),
@@ -7642,6 +7663,12 @@ static int fuse4fs_opt_proc(void *data, const char *arg,
 	struct fuse4fs *ff = data;
 
 	switch (key) {
+#ifdef HAVE_FUSE_IOMAP
+	case FUSE4FS_IOMAP_PASSTHROUGH:
+		ff->iomap_passthrough_options = 1;
+		/* pass through to libfuse */
+		return 1;
+#endif
 	case FUSE4FS_DIRSYNC:
 		ff->dirsync = 1;
 		/* pass through to libfuse */
@@ -7809,7 +7836,6 @@ static void fuse4fs_compute_libfuse_args(struct fuse4fs *ff,
 		ff->translate_inums = 0;
 	}
 
-
 	if (ff->debug) {
 		int	i;
 
misc/fuse2fs.1.in +6 -0
index 2b55fa0e723966..0c0934f03c9543 100644
@@ -90,6 +90,9 @@ .SS "fuse2fs options:"
 .I nosuid
 ) later.
 .TP
+\fB-o\fR lazytime
+if iomap is enabled, enable lazy updates of timestamps
+.TP
 \fB-o\fR lockfile=path
 use this file to control access to the filesystem
 .TP
@@ -98,6 +101,9 @@ .SS "fuse2fs options:"
 .TP
 \fB-o\fR norecovery
 do not replay the journal and mount the file system read-only
+.TP
+\fB-o\fR strictatime
+if iomap is enabled, update atime on every access
 .SS "FUSE options:"
 .TP
 \fB-d -o\fR debug
misc/fuse2fs.c +27 -0
index 4535bb16efd586..3cb875d0d29481 100644
@@ -267,6 +267,7 @@ struct fuse2fs {
 	int directio;
 	int acl;
 	int dirsync;
+	int iomap_passthrough_options;
 
 	enum fuse2fs_opstate opstate;
 	int logfd;
@@ -1191,6 +1192,12 @@ static errcode_t fuse2fs_check_support(struct fuse2fs *ff)
 		return EXT2_ET_FILESYSTEM_CORRUPTED;
 	}
 
+	if (ff->iomap_passthrough_options && !fuse2fs_can_iomap(ff)) {
+		err_printf(ff, "%s\n",
+			   _("Some mount options require iomap."));
+		return EINVAL;
+	}
+
 	return 0;
 }
 
@@ -1811,6 +1818,8 @@ static void fuse2fs_iomap_enable(struct fuse_conn_info *conn,
 	if (!fuse2fs_iomap_enabled(ff)) {
 		if (ff->iomap_want == FT_ENABLE)
 			err_printf(ff, "%s\n", _("Could not enable iomap."));
+		if (ff->iomap_passthrough_options)
+			err_printf(ff, "%s\n", _("Some mount options require iomap."));
 		return;
 	}
 }
@@ -7104,6 +7113,7 @@ enum {
 	FUSE2FS_ERRORS_BEHAVIOR,
 #ifdef HAVE_FUSE_IOMAP
 	FUSE2FS_IOMAP,
+	FUSE2FS_IOMAP_PASSTHROUGH,
 #endif
 };
 
@@ -7130,6 +7140,17 @@ static struct fuse_opt fuse2fs_opts[] = {
 	FUSE2FS_OPT("timing",		timing,			1),
 #endif
 
+#ifdef HAVE_FUSE_IOMAP
+#ifdef MS_LAZYTIME
+	FUSE_OPT_KEY("lazytime",	FUSE2FS_IOMAP_PASSTHROUGH),
+	FUSE_OPT_KEY("nolazytime",	FUSE2FS_IOMAP_PASSTHROUGH),
+#endif
+#ifdef MS_STRICTATIME
+	FUSE_OPT_KEY("strictatime",	FUSE2FS_IOMAP_PASSTHROUGH),
+	FUSE_OPT_KEY("nostrictatime",	FUSE2FS_IOMAP_PASSTHROUGH),
+#endif
+#endif
+
 	FUSE_OPT_KEY("user_xattr",	FUSE2FS_IGNORED),
 	FUSE_OPT_KEY("noblock_validity", FUSE2FS_IGNORED),
 	FUSE_OPT_KEY("nodelalloc",	FUSE2FS_IGNORED),
@@ -7156,6 +7177,12 @@ static int fuse2fs_opt_proc(void *data, const char *arg,
 	struct fuse2fs *ff = data;
 
 	switch (key) {
+#ifdef HAVE_FUSE_IOMAP
+	case FUSE2FS_IOMAP_PASSTHROUGH:
+		ff->iomap_passthrough_options = 1;
+		/* pass through to libfuse */
+		return 1;
+#endif
 	case FUSE2FS_DIRSYNC:
 		ff->dirsync = 1;
 		/* pass through to libfuse */
#178 lore raw

PATCH 2/10 fuse2fs: skip permission checking on utimens when iomap is enabled

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

When iomap is enabled, the kernel is in charge of enforcing permissions checks on timestamp updates for files. We needn't do that in userspace anymore.

---
 fuse4fs/fuse4fs.c |   11 +++++++----
 misc/fuse2fs.c    |   11 +++++++----
 2 files changed, 14 insertions(+), 8 deletions(-)
fuse4fs/fuse4fs.c +7 -4
index 428e4cb404cb45..b7ee336b5e4546 100644
@@ -5276,13 +5276,16 @@ static int fuse4fs_utimens(struct fuse4fs *ff, const struct fuse_ctx *ctxt,
 
 	/*
 	 * ext4 allows timestamp updates of append-only files but only if we're
-	 * setting to current time
+	 * setting to current time.  If iomap is enabled, the kernel does the
+	 * permission checking for timestamp updates; skip the access check.
 	 */
 	if (aact == TA_NOW && mact == TA_NOW)
 		access |= A_OK;
-	ret = fuse4fs_inum_access(ff, ctxt, ino, access);
-	if (ret)
-		return ret;
+	if (!fuse4fs_iomap_enabled(ff)) {
+		ret = fuse4fs_inum_access(ff, ctxt, ino, access);
+		if (ret)
+			return ret;
+	}
 
 	if (aact != TA_OMIT)
 		EXT4_INODE_SET_XTIME(i_atime, &atime, inode);
misc/fuse2fs.c +7 -4
index 3cb875d0d29481..473a9124016712 100644
@@ -4930,13 +4930,16 @@ static int op_utimens(const char *path, const struct timespec ctv[2],
 
 	/*
 	 * ext4 allows timestamp updates of append-only files but only if we're
-	 * setting to current time
+	 * setting to current time.  If iomap is enabled, the kernel does the
+	 * permission checking for timestamp updates; skip the access check.
 	 */
 	if (ctv[0].tv_nsec == UTIME_NOW && ctv[1].tv_nsec == UTIME_NOW)
 		access |= A_OK;
-	ret = check_inum_access(ff, ino, access);
-	if (ret)
-		goto out;
+	if (!fuse2fs_iomap_enabled(ff)) {
+		ret = check_inum_access(ff, ino, access);
+		if (ret)
+			goto out;
+	}
 
 	err = fuse2fs_read_inode(fs, ino, &inode);
 	if (err) {
#179 lore raw

PATCH 3/10 fuse2fs: let the kernel tell us about acl/mode updates

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

When the kernel is running in iomap mode, it will also manage all the ACL updates and the resulting file mode changes for us. Disable the manual implementation of it in fuse2fs.

---
 fuse4fs/fuse4fs.c |    4 ++--
 misc/fuse2fs.c    |    4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
fuse4fs/fuse4fs.c +2 -2
index b7ee336b5e4546..2cbbd1ed19a00c 100644
@@ -2505,7 +2505,7 @@ static int fuse4fs_propagate_default_acls(struct fuse4fs *ff, ext2_ino_t parent,
 	size_t deflen;
 	int ret;
 
-	if (!ff->acl || S_ISDIR(mode))
+	if (!ff->acl || S_ISDIR(mode) || fuse4fs_iomap_enabled(ff))
 		return 0;
 
 	ret = fuse4fs_getxattr(ff, parent, XATTR_NAME_POSIX_ACL_DEFAULT, &def,
@@ -3931,7 +3931,7 @@ static int fuse4fs_chmod(struct fuse4fs *ff, fuse_req_t req, ext2_ino_t ino,
 	 * of the user's groups, but FUSE only tells us about the primary
 	 * group.
 	 */
-	if (!fuse4fs_is_superuser(ff, ctxt)) {
+	if (!fuse4fs_iomap_enabled(ff) && !fuse4fs_is_superuser(ff, ctxt)) {
 		ret = fuse4fs_in_file_group(ff, req, inode);
 		if (ret < 0)
 			return ret;
misc/fuse2fs.c +2 -2
index 473a9124016712..503d3c1cba5fd4 100644
@@ -2307,7 +2307,7 @@ static int propagate_default_acls(struct fuse2fs *ff, ext2_ino_t parent,
 	size_t deflen;
 	int ret;
 
-	if (!ff->acl || S_ISDIR(mode))
+	if (!ff->acl || S_ISDIR(mode) || fuse2fs_iomap_enabled(ff))
 		return 0;
 
 	ret = __getxattr(ff, parent, XATTR_NAME_POSIX_ACL_DEFAULT, &def,
@@ -3636,7 +3636,7 @@ static int op_chmod(const char *path, mode_t mode, struct fuse_file_info *fi)
 	 * of the user's groups, but FUSE only tells us about the primary
 	 * group.
 	 */
-	if (!is_superuser(ff, ctxt)) {
+	if (!fuse2fs_iomap_enabled(ff) && !is_superuser(ff, ctxt)) {
 		ret = in_file_group(ctxt, &inode);
 		if (ret < 0)
 			goto out;
#180 lore raw

PATCH 4/10 fuse2fs: better debugging for file mode updates

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Improve the tracing of a chmod operation so that we can debug file mode updates.

---
 fuse4fs/fuse4fs.c |   10 ++++++----
 misc/fuse2fs.c    |   12 +++++++-----
 2 files changed, 13 insertions(+), 9 deletions(-)
fuse4fs/fuse4fs.c +6 -4
index 2cbbd1ed19a00c..97747d42a64a52 100644
@@ -3914,6 +3914,7 @@ static int fuse4fs_chmod(struct fuse4fs *ff, fuse_req_t req, ext2_ino_t ino,
 			 mode_t mode, struct ext2_inode_large *inode)
 {
 	const struct fuse_ctx *ctxt = fuse_req_ctx(req);
+	mode_t new_mode;
 	int ret = 0;
 
 	dbg_printf(ff, "%s: ino=%d mode=0%o\n", __func__, ino, mode);
@@ -3940,11 +3941,12 @@ static int fuse4fs_chmod(struct fuse4fs *ff, fuse_req_t req, ext2_ino_t ino,
 			mode &= ~S_ISGID;
 	}
 
-	inode->i_mode &= ~0xFFF;
-	inode->i_mode |= mode & 0xFFF;
+	new_mode = (inode->i_mode & ~0xFFF) | (mode & 0xFFF);
 
-	dbg_printf(ff, "%s: ino=%d new_mode=0%o\n",
-		   __func__, ino, inode->i_mode);
+	dbg_printf(ff, "%s: ino=%d old_mode=0%o new_mode=0%o\n",
+		   __func__, ino, inode->i_mode, new_mode);
+
+	inode->i_mode = new_mode;
 
 	return 0;
 }
misc/fuse2fs.c +7 -5
index 503d3c1cba5fd4..90f537efe525ce 100644
@@ -3607,6 +3607,7 @@ static int op_chmod(const char *path, mode_t mode, struct fuse_file_info *fi)
 	errcode_t err;
 	ext2_ino_t ino;
 	struct ext2_inode_large inode;
+	mode_t new_mode;
 	int ret = 0;
 
 	FUSE2FS_CHECK_CONTEXT(ff);
@@ -3645,11 +3646,12 @@ static int op_chmod(const char *path, mode_t mode, struct fuse_file_info *fi)
 			mode &= ~S_ISGID;
 	}
 
-	inode.i_mode &= ~0xFFF;
-	inode.i_mode |= mode & 0xFFF;
+	new_mode = (inode.i_mode & ~0xFFF) | (mode & 0xFFF);
 
-	dbg_printf(ff, "%s: path=%s new_mode=0%o ino=%d\n", __func__,
-		   path, inode.i_mode, ino);
+	dbg_printf(ff, "%s: path=%s old_mode=0%o new_mode=0%o ino=%d\n",
+		   __func__, path, inode.i_mode, new_mode, ino);
+
+	inode.i_mode = new_mode;
 
 	ret = update_ctime(fs, ino, &inode);
 	if (ret)
@@ -3669,12 +3671,12 @@ static int op_chmod(const char *path, mode_t mode, struct fuse_file_info *fi)
 static int op_chown(const char *path, uid_t owner, gid_t group,
 		    struct fuse_file_info *fi)
 {
+	struct ext2_inode_large inode;
 	struct fuse_context *ctxt = fuse_get_context();
 	struct fuse2fs *ff = fuse2fs_get();
 	ext2_filsys fs;
 	errcode_t err;
 	ext2_ino_t ino;
-	struct ext2_inode_large inode;
 	int ret = 0;
 
 	FUSE2FS_CHECK_CONTEXT(ff);
#181 lore raw

PATCH 5/10 fuse2fs: debug timestamp updates

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Add tracing for timestamp updates to files.

---
 misc/fuse2fs.c |   97 +++++++++++++++++++++++++++++++++++---------------------
 1 file changed, 61 insertions(+), 36 deletions(-)
misc/fuse2fs.c +61 -36
index 90f537efe525ce..21e27efb835659 100644
@@ -864,7 +864,8 @@ static void increment_version(struct ext2_inode_large *inode)
 		inode->i_version_hi = ver >> 32;
 }
 
-static void init_times(struct ext2_inode_large *inode)
+static void fuse2fs_init_timestamps(struct fuse2fs *ff, ext2_ino_t ino,
+				    struct ext2_inode_large *inode)
 {
 	struct timespec now;
 
@@ -874,11 +875,15 @@ static void init_times(struct ext2_inode_large *inode)
 	EXT4_INODE_SET_XTIME(i_mtime, &now, inode);
 	EXT4_EINODE_SET_XTIME(i_crtime, &now, inode);
 	increment_version(inode);
+
+	dbg_printf(ff, "%s: ino=%u time %ld:%lu\n", __func__, ino, now.tv_sec,
+		   now.tv_nsec);
 }
 
-static int update_ctime(ext2_filsys fs, ext2_ino_t ino,
-			struct ext2_inode_large *pinode)
+static int fuse2fs_update_ctime(struct fuse2fs *ff, ext2_ino_t ino,
+				struct ext2_inode_large *pinode)
 {
+	ext2_filsys fs = ff->fs;
 	errcode_t err;
 	struct timespec now;
 	struct ext2_inode_large inode;
@@ -889,6 +894,10 @@ static int update_ctime(ext2_filsys fs, ext2_ino_t ino,
 	if (pinode) {
 		increment_version(pinode);
 		EXT4_INODE_SET_XTIME(i_ctime, &now, pinode);
+
+		dbg_printf(ff, "%s: ino=%u ctime %ld:%lu\n", __func__, ino,
+			   now.tv_sec, now.tv_nsec);
+
 		return 0;
 	}
 
@@ -900,6 +909,9 @@ static int update_ctime(ext2_filsys fs, ext2_ino_t ino,
 	increment_version(&inode);
 	EXT4_INODE_SET_XTIME(i_ctime, &now, &inode);
 
+	dbg_printf(ff, "%s: ino=%u ctime %ld:%lu\n", __func__, ino,
+		   now.tv_sec, now.tv_nsec);
+
 	err = fuse2fs_write_inode(fs, ino, &inode);
 	if (err)
 		return translate_error(fs, ino, err);
@@ -907,8 +919,9 @@ static int update_ctime(ext2_filsys fs, ext2_ino_t ino,
 	return 0;
 }
 
-static int update_atime(ext2_filsys fs, ext2_ino_t ino)
+static int fuse2fs_update_atime(struct fuse2fs *ff, ext2_ino_t ino)
 {
+	ext2_filsys fs = ff->fs;
 	errcode_t err;
 	struct ext2_inode_large inode, *pinode;
 	struct timespec atime, mtime, now;
@@ -927,6 +940,10 @@ static int update_atime(ext2_filsys fs, ext2_ino_t ino)
 	dmtime = mtime.tv_sec + ((double)mtime.tv_nsec / NSEC_PER_SEC);
 	dnow = now.tv_sec + ((double)now.tv_nsec / NSEC_PER_SEC);
 
+	dbg_printf(ff, "%s: ino=%u atime %ld:%lu mtime %ld:%lu now %ld:%lu\n",
+		   __func__, ino, atime.tv_sec, atime.tv_nsec, mtime.tv_sec,
+		   mtime.tv_nsec, now.tv_sec, now.tv_nsec);
+
 	/*
 	 * If atime is newer than mtime and atime hasn't been updated in thirty
 	 * seconds, skip the atime update.  Same idea as Linux "relatime".  Use
@@ -943,9 +960,10 @@ static int update_atime(ext2_filsys fs, ext2_ino_t ino)
 	return 0;
 }
 
-static int update_mtime(ext2_filsys fs, ext2_ino_t ino,
-			struct ext2_inode_large *pinode)
+static int fuse2fs_update_mtime(struct fuse2fs *ff, ext2_ino_t ino,
+				struct ext2_inode_large *pinode)
 {
+	ext2_filsys fs = ff->fs;
 	errcode_t err;
 	struct ext2_inode_large inode;
 	struct timespec now;
@@ -955,6 +973,10 @@ static int update_mtime(ext2_filsys fs, ext2_ino_t ino,
 		EXT4_INODE_SET_XTIME(i_mtime, &now, pinode);
 		EXT4_INODE_SET_XTIME(i_ctime, &now, pinode);
 		increment_version(pinode);
+
+		dbg_printf(ff, "%s: ino=%u mtime/ctime %ld:%lu\n",
+			   __func__, ino, now.tv_sec, now.tv_nsec);
+
 		return 0;
 	}
 
@@ -967,6 +989,9 @@ static int update_mtime(ext2_filsys fs, ext2_ino_t ino,
 	EXT4_INODE_SET_XTIME(i_ctime, &now, &inode);
 	increment_version(&inode);
 
+	dbg_printf(ff, "%s: ino=%u mtime/ctime %ld:%lu\n",
+		   __func__, ino, now.tv_sec, now.tv_nsec);
+
 	err = fuse2fs_write_inode(fs, ino, &inode);
 	if (err)
 		return translate_error(fs, ino, err);
@@ -2228,7 +2253,7 @@ static int op_readlink(const char *path, char *buf, size_t len)
 	buf[len] = 0;
 
 	if (fuse2fs_is_writeable(ff)) {
-		ret = update_atime(fs, ino);
+		ret = fuse2fs_update_atime(ff, ino);
 		if (ret)
 			goto out;
 	}
@@ -2502,7 +2527,7 @@ static int op_mknod(const char *path, mode_t mode, dev_t dev)
 		goto out2;
 	}
 
-	ret = update_mtime(fs, parent, NULL);
+	ret = fuse2fs_update_mtime(ff, parent, NULL);
 	if (ret)
 		goto out2;
 
@@ -2525,7 +2550,7 @@ static int op_mknod(const char *path, mode_t mode, dev_t dev)
 	}
 
 	inode.i_generation = ff->next_generation++;
-	init_times(&inode);
+	fuse2fs_init_timestamps(ff, child, &inode);
 	err = fuse2fs_write_inode(fs, child, &inode);
 	if (err) {
 		ret = translate_error(fs, child, err);
@@ -2611,7 +2636,7 @@ static int op_mkdir(const char *path, mode_t mode)
 		goto out2;
 	}
 
-	ret = update_mtime(fs, parent, NULL);
+	ret = fuse2fs_update_mtime(ff, parent, NULL);
 	if (ret)
 		goto out2;
 
@@ -2638,7 +2663,7 @@ static int op_mkdir(const char *path, mode_t mode)
 	if (parent_sgid)
 		inode.i_mode |= S_ISGID;
 	inode.i_generation = ff->next_generation++;
-	init_times(&inode);
+	fuse2fs_init_timestamps(ff, child, &inode);
 
 	err = fuse2fs_write_inode(fs, child, &inode);
 	if (err) {
@@ -2721,7 +2746,7 @@ static int fuse2fs_unlink(struct fuse2fs *ff, const char *path,
 	if (err)
 		return translate_error(fs, dir, err);
 
-	ret = update_mtime(fs, dir, NULL);
+	ret = fuse2fs_update_mtime(ff, dir, NULL);
 	if (ret)
 		return ret;
 
@@ -2812,7 +2837,7 @@ static int remove_inode(struct fuse2fs *ff, ext2_ino_t ino)
 			ext2fs_set_dtime(fs, EXT2_INODE(&inode));
 	}
 
-	ret = update_ctime(fs, ino, &inode);
+	ret = fuse2fs_update_ctime(ff, ino, &inode);
 	if (ret)
 		return ret;
 
@@ -2982,7 +3007,7 @@ static int __op_rmdir(struct fuse2fs *ff, const char *path)
 			goto out;
 		}
 		ext2fs_dec_nlink(EXT2_INODE(&inode));
-		ret = update_mtime(fs, rds.parent, &inode);
+		ret = fuse2fs_update_mtime(ff, rds.parent, &inode);
 		if (ret)
 			goto out;
 		err = fuse2fs_write_inode(fs, rds.parent, &inode);
@@ -3079,7 +3104,7 @@ static int op_symlink(const char *src, const char *dest)
 	}
 
 	/* Update parent dir's mtime */
-	ret = update_mtime(fs, parent, NULL);
+	ret = fuse2fs_update_mtime(ff, parent, NULL);
 	if (ret)
 		goto out2;
 
@@ -3103,7 +3128,7 @@ static int op_symlink(const char *src, const char *dest)
 	fuse2fs_set_uid(&inode, ctxt->uid);
 	fuse2fs_set_gid(&inode, gid);
 	inode.i_generation = ff->next_generation++;
-	init_times(&inode);
+	fuse2fs_init_timestamps(ff, child, &inode);
 
 	err = fuse2fs_write_inode(fs, child, &inode);
 	if (err) {
@@ -3388,11 +3413,11 @@ static int op_rename(const char *from, const char *to,
 	}
 
 	/* Update timestamps */
-	ret = update_ctime(fs, from_ino, NULL);
+	ret = fuse2fs_update_ctime(ff, from_ino, NULL);
 	if (ret)
 		goto out2;
 
-	ret = update_mtime(fs, to_dir_ino, NULL);
+	ret = fuse2fs_update_mtime(ff, to_dir_ino, NULL);
 	if (ret)
 		goto out2;
 
@@ -3486,7 +3511,7 @@ static int op_link(const char *src, const char *dest)
 	}
 
 	ext2fs_inc_nlink(fs, EXT2_INODE(&inode));
-	ret = update_ctime(fs, ino, &inode);
+	ret = fuse2fs_update_ctime(ff, ino, &inode);
 	if (ret)
 		goto out2;
 
@@ -3505,7 +3530,7 @@ static int op_link(const char *src, const char *dest)
 		goto out2;
 	}
 
-	ret = update_mtime(fs, parent, NULL);
+	ret = fuse2fs_update_mtime(ff, parent, NULL);
 	if (ret)
 		goto out2;
 
@@ -3653,7 +3678,7 @@ static int op_chmod(const char *path, mode_t mode, struct fuse_file_info *fi)
 
 	inode.i_mode = new_mode;
 
-	ret = update_ctime(fs, ino, &inode);
+	ret = fuse2fs_update_ctime(ff, ino, &inode);
 	if (ret)
 		goto out;
 
@@ -3720,7 +3745,7 @@ static int op_chown(const char *path, uid_t owner, gid_t group,
 		fuse2fs_set_gid(&inode, group);
 	}
 
-	ret = update_ctime(fs, ino, &inode);
+	ret = fuse2fs_update_ctime(ff, ino, &inode);
 	if (ret)
 		goto out;
 
@@ -3850,7 +3875,7 @@ static int fuse2fs_truncate(struct fuse2fs *ff, ext2_ino_t ino, off_t new_size)
 	if (err)
 		return translate_error(fs, ino, err);
 
-	ret = update_mtime(fs, ino, NULL);
+	ret = fuse2fs_update_mtime(ff, ino, NULL);
 	if (ret)
 		return ret;
 
@@ -4085,7 +4110,7 @@ static int op_read(const char *path EXT2FS_ATTR((unused)), char *buf,
 	}
 
 	if (fh->check_flags != X_OK && fuse2fs_is_writeable(ff)) {
-		ret = update_atime(fs, fh->ino);
+		ret = fuse2fs_update_atime(ff, fh->ino);
 		if (ret)
 			goto out;
 	}
@@ -4169,7 +4194,7 @@ static int op_write(const char *path EXT2FS_ATTR((unused)),
 		goto out;
 	}
 
-	ret = update_mtime(fs, fh->ino, NULL);
+	ret = fuse2fs_update_mtime(ff, fh->ino, NULL);
 	if (ret)
 		goto out;
 
@@ -4531,7 +4556,7 @@ static int op_setxattr(const char *path EXT2FS_ATTR((unused)),
 		goto out2;
 	}
 
-	ret = update_ctime(fs, ino, NULL);
+	ret = fuse2fs_update_ctime(ff, ino, NULL);
 out2:
 	err = ext2fs_xattrs_close(&h);
 	if (!ret && err)
@@ -4625,7 +4650,7 @@ static int op_removexattr(const char *path, const char *key)
 		goto out2;
 	}
 
-	ret = update_ctime(fs, ino, NULL);
+	ret = fuse2fs_update_ctime(ff, ino, NULL);
 out2:
 	err = ext2fs_xattrs_close(&h);
 	if (err && !ret)
@@ -4743,7 +4768,7 @@ static int op_readdir(const char *path EXT2FS_ATTR((unused)), void *buf,
 	}
 
 	if (fuse2fs_is_writeable(ff)) {
-		ret = update_atime(i.fs, fh->ino);
+		ret = fuse2fs_update_atime(ff, fh->ino);
 		if (ret)
 			goto out;
 	}
@@ -4848,7 +4873,7 @@ static int op_create(const char *path, mode_t mode, struct fuse_file_info *fp)
 		goto out2;
 	}
 
-	ret = update_mtime(fs, parent, NULL);
+	ret = fuse2fs_update_mtime(ff, parent, NULL);
 	if (ret)
 		goto out2;
 
@@ -4879,7 +4904,7 @@ static int op_create(const char *path, mode_t mode, struct fuse_file_info *fp)
 	}
 
 	inode.i_generation = ff->next_generation++;
-	init_times(&inode);
+	fuse2fs_init_timestamps(ff, child, &inode);
 	err = fuse2fs_write_inode(fs, child, &inode);
 	if (err) {
 		ret = translate_error(fs, child, err);
@@ -4963,7 +4988,7 @@ static int op_utimens(const char *path, const struct timespec ctv[2],
 	if (tv[1].tv_nsec != UTIME_OMIT)
 		EXT4_INODE_SET_XTIME(i_mtime, &tv[1], &inode);
 #endif /* UTIME_OMIT */
-	ret = update_ctime(fs, ino, &inode);
+	ret = fuse2fs_update_ctime(ff, ino, &inode);
 	if (ret)
 		goto out;
 
@@ -5031,7 +5056,7 @@ static int ioctl_setflags(struct fuse2fs *ff, struct fuse2fs_file_handle *fh,
 	if (ret)
 		return ret;
 
-	ret = update_ctime(fs, fh->ino, &inode);
+	ret = fuse2fs_update_ctime(ff, fh->ino, &inode);
 	if (ret)
 		return ret;
 
@@ -5078,7 +5103,7 @@ static int ioctl_setversion(struct fuse2fs *ff, struct fuse2fs_file_handle *fh,
 
 	inode.i_generation = generation;
 
-	ret = update_ctime(fs, fh->ino, &inode);
+	ret = fuse2fs_update_ctime(ff, fh->ino, &inode);
 	if (ret)
 		return ret;
 
@@ -5209,7 +5234,7 @@ static int ioctl_fssetxattr(struct fuse2fs *ff, struct fuse2fs_file_handle *fh,
 	if (ext2fs_inode_includes(inode_size, i_projid))
 		inode.i_projid = fsx->fsx_projid;
 
-	ret = update_ctime(fs, fh->ino, &inode);
+	ret = fuse2fs_update_ctime(ff, fh->ino, &inode);
 	if (ret)
 		return ret;
 
@@ -5481,7 +5506,7 @@ static int fuse2fs_allocate_range(struct fuse2fs *ff,
 		}
 	}
 
-	err = update_mtime(fs, fh->ino, &inode);
+	err = fuse2fs_update_mtime(ff, fh->ino, &inode);
 	if (err)
 		return err;
 
@@ -5654,7 +5679,7 @@ static int fuse2fs_punch_range(struct fuse2fs *ff,
 			return translate_error(fs, fh->ino, err);
 	}
 
-	err = update_mtime(fs, fh->ino, &inode);
+	err = fuse2fs_update_mtime(ff, fh->ino, &inode);
 	if (err)
 		return err;
 
#182 lore raw

PATCH 6/10 fuse2fs: use coarse timestamps for iomap mode

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

In iomap mode, the kernel is responsible for maintaining timestamps because file writes don't upcall to fuse2fs. The kernel's predicate for deciding if [cm]time should be updated bases its decisions off [cm]time being an exact match for the coarse clock (instead of checking that [cm]time < coarse_clock) which means that fuse2fs setting a fine-grained timestamp that is slightly ahead of the coarse clock can result in timestamps appearing to go backwards. generic/423 doesn't like seeing btime > ctime from statx, so we'll use the coarse clock in iomap mode.

---
 fuse4fs/fuse4fs.c |  110 +++++++++++++++++++++++++++++++----------------------
 misc/fuse2fs.c    |   34 ++++++++++++----
 2 files changed, 90 insertions(+), 54 deletions(-)
fuse4fs/fuse4fs.c +65 -45
index 97747d42a64a52..6cda267ad5cf40 100644
@@ -1004,8 +1004,24 @@ static inline void fuse4fs_dump_extents(struct fuse4fs *ff, ext2_ino_t ino,
 	ext2fs_extent_free(extents);
 }
 
-static void get_now(struct timespec *now)
+static void fuse4fs_get_now(struct fuse4fs *ff, struct timespec *now)
 {
+#ifdef CLOCK_REALTIME_COARSE
+	/*
+	 * In iomap mode, the kernel is responsible for maintaining timestamps
+	 * because file writes don't upcall to fuse4fs.  The kernel's predicate
+	 * for deciding if [cm]time should be updated bases its decisions off
+	 * [cm]time being an exact match for the coarse clock (instead of
+	 * checking that [cm]time < coarse_clock) which means that fuse4fs
+	 * setting a fine-grained timestamp that is slightly ahead of the
+	 * coarse clock can result in timestamps appearing to go backwards.
+	 * generic/423 doesn't like seeing btime > ctime from statx, so we'll
+	 * use the coarse clock in iomap mode.
+	 */
+	if (fuse4fs_iomap_enabled(ff) &&
+	    !clock_gettime(CLOCK_REALTIME_COARSE, now))
+		return;
+#endif
 #ifdef CLOCK_REALTIME
 	if (!clock_gettime(CLOCK_REALTIME, now))
 		return;
@@ -1028,11 +1044,12 @@ static void increment_version(struct ext2_inode_large *inode)
 		inode->i_version_hi = ver >> 32;
 }
 
-static void init_times(struct ext2_inode_large *inode)
+static void fuse4fs_init_timestamps(struct fuse4fs *ff,
+				    struct ext2_inode_large *inode)
 {
 	struct timespec now;
 
-	get_now(&now);
+	fuse4fs_get_now(ff, &now);
 	EXT4_INODE_SET_XTIME(i_atime, &now, inode);
 	EXT4_INODE_SET_XTIME(i_ctime, &now, inode);
 	EXT4_INODE_SET_XTIME(i_mtime, &now, inode);
@@ -1040,14 +1057,15 @@ static void init_times(struct ext2_inode_large *inode)
 	increment_version(inode);
 }
 
-static int update_ctime(ext2_filsys fs, ext2_ino_t ino,
-			struct ext2_inode_large *pinode)
+static int fuse4fs_update_ctime(struct fuse4fs *ff, ext2_ino_t ino,
+				struct ext2_inode_large *pinode)
 {
-	errcode_t err;
 	struct timespec now;
 	struct ext2_inode_large inode;
+	ext2_filsys fs = ff->fs;
+	errcode_t err;
 
-	get_now(&now);
+	fuse4fs_get_now(ff, &now);
 
 	/* If user already has a inode buffer, just update that */
 	if (pinode) {
@@ -1071,12 +1089,13 @@ static int update_ctime(ext2_filsys fs, ext2_ino_t ino,
 	return 0;
 }
 
-static int update_atime(ext2_filsys fs, ext2_ino_t ino)
+static int fuse4fs_update_atime(struct fuse4fs *ff, ext2_ino_t ino)
 {
-	errcode_t err;
 	struct ext2_inode_large inode, *pinode;
 	struct timespec atime, mtime, now;
+	ext2_filsys fs = ff->fs;
 	double datime, dmtime, dnow;
+	errcode_t err;
 
 	err = fuse4fs_read_inode(fs, ino, &inode);
 	if (err)
@@ -1085,7 +1104,7 @@ static int update_atime(ext2_filsys fs, ext2_ino_t ino)
 	pinode = &inode;
 	EXT4_INODE_GET_XTIME(i_atime, &atime, pinode);
 	EXT4_INODE_GET_XTIME(i_mtime, &mtime, pinode);
-	get_now(&now);
+	fuse4fs_get_now(ff, &now);
 
 	datime = atime.tv_sec + ((double)atime.tv_nsec / NSEC_PER_SEC);
 	dmtime = mtime.tv_sec + ((double)mtime.tv_nsec / NSEC_PER_SEC);
@@ -1107,15 +1126,16 @@ static int update_atime(ext2_filsys fs, ext2_ino_t ino)
 	return 0;
 }
 
-static int update_mtime(ext2_filsys fs, ext2_ino_t ino,
-			struct ext2_inode_large *pinode)
+static int fuse4fs_update_mtime(struct fuse4fs *ff, ext2_ino_t ino,
+				struct ext2_inode_large *pinode)
 {
-	errcode_t err;
 	struct ext2_inode_large inode;
 	struct timespec now;
+	ext2_filsys fs = ff->fs;
+	errcode_t err;
 
 	if (pinode) {
-		get_now(&now);
+		fuse4fs_get_now(ff, &now);
 		EXT4_INODE_SET_XTIME(i_mtime, &now, pinode);
 		EXT4_INODE_SET_XTIME(i_ctime, &now, pinode);
 		increment_version(pinode);
@@ -1126,7 +1146,7 @@ static int update_mtime(ext2_filsys fs, ext2_ino_t ino,
 	if (err)
 		return translate_error(fs, ino, err);
 
-	get_now(&now);
+	fuse4fs_get_now(ff, &now);
 	EXT4_INODE_SET_XTIME(i_mtime, &now, &inode);
 	EXT4_INODE_SET_XTIME(i_ctime, &now, &inode);
 	increment_version(&inode);
@@ -2422,7 +2442,7 @@ static void op_readlink(fuse_req_t req, fuse_ino_t fino)
 	buf[len] = 0;
 
 	if (fuse4fs_is_writeable(ff)) {
-		ret = update_atime(fs, ino);
+		ret = fuse4fs_update_atime(ff, ino);
 		if (ret)
 			goto out;
 	}
@@ -2691,7 +2711,7 @@ static void op_mknod(fuse_req_t req, fuse_ino_t fino, const char *name,
 		goto out2;
 	}
 
-	ret = update_mtime(fs, parent, NULL);
+	ret = fuse4fs_update_mtime(ff, parent, NULL);
 	if (ret)
 		goto out2;
 
@@ -2714,7 +2734,7 @@ static void op_mknod(fuse_req_t req, fuse_ino_t fino, const char *name,
 	}
 
 	inode.i_generation = ff->next_generation++;
-	init_times(&inode);
+	fuse4fs_init_timestamps(ff, &inode);
 	err = fuse4fs_write_inode(fs, child, &inode);
 	if (err) {
 		ret = translate_error(fs, child, err);
@@ -2776,7 +2796,7 @@ static void op_mkdir(fuse_req_t req, fuse_ino_t fino, const char *name,
 		goto out2;
 	}
 
-	ret = update_mtime(fs, parent, NULL);
+	ret = fuse4fs_update_mtime(ff, parent, NULL);
 	if (ret)
 		goto out2;
 
@@ -2802,7 +2822,7 @@ static void op_mkdir(fuse_req_t req, fuse_ino_t fino, const char *name,
 	if (parent_sgid)
 		inode.i_mode |= S_ISGID;
 	inode.i_generation = ff->next_generation++;
-	init_times(&inode);
+	fuse4fs_init_timestamps(ff, &inode);
 
 	err = fuse4fs_write_inode(fs, child, &inode);
 	if (err) {
@@ -3153,7 +3173,7 @@ static int fuse4fs_remove_inode(struct fuse4fs *ff, ext2_ino_t ino)
 		inode.i_links_count--;
 	}
 
-	ret = update_ctime(fs, ino, &inode);
+	ret = fuse4fs_update_ctime(ff, ino, &inode);
 	if (ret)
 		return ret;
 
@@ -3225,7 +3245,7 @@ static int fuse4fs_unlink(struct fuse4fs *ff, ext2_ino_t parent,
 		goto out;
 	}
 
-	ret = update_mtime(fs, parent, NULL);
+	ret = fuse4fs_update_mtime(ff, parent, NULL);
 	if (ret)
 		goto out;
 out:
@@ -3359,7 +3379,7 @@ static int fuse4fs_rmdir(struct fuse4fs *ff, ext2_ino_t parent,
 			goto out;
 		}
 		ext2fs_dec_nlink(EXT2_INODE(&inode));
-		ret = update_mtime(fs, rds.parent, &inode);
+		ret = fuse4fs_update_mtime(ff, rds.parent, &inode);
 		if (ret)
 			goto out;
 		err = fuse4fs_write_inode(fs, rds.parent, &inode);
@@ -3463,7 +3483,7 @@ static void op_symlink(fuse_req_t req, const char *target, fuse_ino_t fino,
 	}
 
 	/* Update parent dir's mtime */
-	ret = update_mtime(fs, parent, NULL);
+	ret = fuse4fs_update_mtime(ff, parent, NULL);
 	if (ret)
 		goto out2;
 
@@ -3486,7 +3506,7 @@ static void op_symlink(fuse_req_t req, const char *target, fuse_ino_t fino,
 	fuse4fs_set_uid(&inode, ctxt->uid);
 	fuse4fs_set_gid(&inode, gid);
 	inode.i_generation = ff->next_generation++;
-	init_times(&inode);
+	fuse4fs_init_timestamps(ff, &inode);
 
 	err = fuse4fs_write_inode(fs, child, &inode);
 	if (err) {
@@ -3717,11 +3737,11 @@ static void op_rename(fuse_req_t req, fuse_ino_t from_parent, const char *from,
 	}
 
 	/* Update timestamps */
-	ret = update_ctime(fs, from_ino, NULL);
+	ret = fuse4fs_update_ctime(ff, from_ino, NULL);
 	if (ret)
 		goto out;
 
-	ret = update_mtime(fs, to_dir_ino, NULL);
+	ret = fuse4fs_update_mtime(ff, to_dir_ino, NULL);
 	if (ret)
 		goto out;
 
@@ -3800,7 +3820,7 @@ static void op_link(fuse_req_t req, fuse_ino_t child_fino,
 	}
 
 	ext2fs_inc_nlink(fs, EXT2_INODE(&inode));
-	ret = update_ctime(fs, child, &inode);
+	ret = fuse4fs_update_ctime(ff, child, &inode);
 	if (ret)
 		goto out2;
 
@@ -3817,7 +3837,7 @@ static void op_link(fuse_req_t req, fuse_ino_t child_fino,
 		goto out2;
 	}
 
-	ret = update_mtime(fs, parent, NULL);
+	ret = fuse4fs_update_mtime(ff, parent, NULL);
 	if (ret)
 		goto out2;
 
@@ -4053,7 +4073,7 @@ static int fuse4fs_truncate(struct fuse4fs *ff, ext2_ino_t ino, off_t new_size)
 	if (err)
 		return translate_error(fs, ino, err);
 
-	ret = update_mtime(fs, ino, NULL);
+	ret = fuse4fs_update_mtime(ff, ino, NULL);
 	if (ret)
 		return ret;
 
@@ -4262,7 +4282,7 @@ static void op_read(fuse_req_t req, fuse_ino_t fino EXT2FS_ATTR((unused)),
 	}
 
 	if (fh->check_flags != X_OK && fuse4fs_is_writeable(ff)) {
-		ret = update_atime(fs, fh->ino);
+		ret = fuse4fs_update_atime(ff, fh->ino);
 		if (ret)
 			goto out;
 	}
@@ -4336,7 +4356,7 @@ static void op_write(fuse_req_t req, fuse_ino_t fino EXT2FS_ATTR((unused)),
 		goto out;
 	}
 
-	ret = update_mtime(fs, fh->ino, NULL);
+	ret = fuse4fs_update_mtime(ff, fh->ino, NULL);
 	if (ret)
 		goto out;
 
@@ -4783,7 +4803,7 @@ static void op_setxattr(fuse_req_t req, fuse_ino_t fino, const char *key,
 		goto out2;
 	}
 
-	ret = update_ctime(fs, ino, NULL);
+	ret = fuse4fs_update_ctime(ff, ino, NULL);
 out2:
 	err = ext2fs_xattrs_close(&h);
 	if (!ret && err)
@@ -4877,7 +4897,7 @@ static void op_removexattr(fuse_req_t req, fuse_ino_t fino, const char *key)
 		goto out2;
 	}
 
-	ret = update_ctime(fs, ino, NULL);
+	ret = fuse4fs_update_ctime(ff, ino, NULL);
 out2:
 	err = ext2fs_xattrs_close(&h);
 	if (err && !ret)
@@ -5024,7 +5044,7 @@ static void __op_readdir(fuse_req_t req, fuse_ino_t fino, size_t size,
 	}
 
 	if (fuse4fs_is_writeable(ff)) {
-		ret = update_atime(i.fs, fh->ino);
+		ret = fuse4fs_update_atime(i.ff, fh->ino);
 		if (ret)
 			goto out;
 	}
@@ -5124,7 +5144,7 @@ static void op_create(fuse_req_t req, fuse_ino_t fino, const char *name,
 			goto out2;
 		}
 
-		ret = update_mtime(fs, parent, NULL);
+		ret = fuse4fs_update_mtime(ff, parent, NULL);
 		if (ret)
 			goto out2;
 	} else {
@@ -5165,7 +5185,7 @@ static void op_create(fuse_req_t req, fuse_ino_t fino, const char *name,
 	}
 
 	inode.i_generation = ff->next_generation++;
-	init_times(&inode);
+	fuse4fs_init_timestamps(ff, &inode);
 	err = fuse4fs_write_inode(fs, child, &inode);
 	if (err) {
 		ret = translate_error(fs, child, err);
@@ -5244,7 +5264,7 @@ static int fuse4fs_utimens(struct fuse4fs *ff, const struct fuse_ctx *ctxt,
 	int ret = 0;
 
 	if (to_set & (FUSE_SET_ATTR_ATIME_NOW | FUSE_SET_ATTR_MTIME_NOW))
-		get_now(&now);
+		fuse4fs_get_now(ff, &now);
 
 	if (to_set & FUSE_SET_ATTR_ATIME_NOW) {
 		atime = now;
@@ -5382,7 +5402,7 @@ static void op_setattr(fuse_req_t req, fuse_ino_t fino, struct stat *attr,
 	}
 
 	/* Update ctime for any attribute change */
-	ret = update_ctime(fs, ino, &inode);
+	ret = fuse4fs_update_ctime(ff, ino, &inode);
 	if (ret)
 		goto out;
 
@@ -5464,7 +5484,7 @@ static int ioctl_setflags(struct fuse4fs *ff, const struct fuse_ctx *ctxt,
 	if (ret)
 		return ret;
 
-	ret = update_ctime(fs, fh->ino, &inode);
+	ret = fuse4fs_update_ctime(ff, fh->ino, &inode);
 	if (ret)
 		return ret;
 
@@ -5517,7 +5537,7 @@ static int ioctl_setversion(struct fuse4fs *ff, const struct fuse_ctx *ctxt,
 
 	inode.i_generation = *indata;
 
-	ret = update_ctime(fs, fh->ino, &inode);
+	ret = fuse4fs_update_ctime(ff, fh->ino, &inode);
 	if (ret)
 		return ret;
 
@@ -5653,7 +5673,7 @@ static int ioctl_fssetxattr(struct fuse4fs *ff, const struct fuse_ctx *ctxt,
 	if (ext2fs_inode_includes(inode_size, i_projid))
 		inode.i_projid = fsx->fsx_projid;
 
-	ret = update_ctime(fs, fh->ino, &inode);
+	ret = fuse4fs_update_ctime(ff, fh->ino, &inode);
 	if (ret)
 		return ret;
 
@@ -5949,7 +5969,7 @@ static int fuse4fs_allocate_range(struct fuse4fs *ff,
 		}
 	}
 
-	err = update_mtime(fs, fh->ino, &inode);
+	err = fuse4fs_update_mtime(ff, fh->ino, &inode);
 	if (err)
 		return err;
 
@@ -6122,7 +6142,7 @@ static int fuse4fs_punch_range(struct fuse4fs *ff,
 			return translate_error(fs, fh->ino, err);
 	}
 
-	err = update_mtime(fs, fh->ino, &inode);
+	err = fuse4fs_update_mtime(ff, fh->ino, &inode);
 	if (err)
 		return err;
 
@@ -8329,7 +8349,7 @@ static int __translate_error(ext2_filsys fs, ext2_ino_t ino, errcode_t err,
 			error_message(err), func, line);
 
 	/* Make a note in the error log */
-	get_now(&now);
+	fuse4fs_get_now(ff, &now);
 	ext2fs_set_tstamp(fs->super, s_last_error_time, now.tv_sec);
 	fs->super->s_last_error_ino = ino;
 	fs->super->s_last_error_line = line;
misc/fuse2fs.c +25 -9
index 21e27efb835659..9b536fe77dda37 100644
@@ -840,8 +840,24 @@ static inline void fuse2fs_dump_extents(struct fuse2fs *ff, ext2_ino_t ino,
 	ext2fs_extent_free(extents);
 }
 
-static void get_now(struct timespec *now)
+static void fuse2fs_get_now(struct fuse2fs *ff, struct timespec *now)
 {
+#ifdef CLOCK_REALTIME_COARSE
+	/*
+	 * In iomap mode, the kernel is responsible for maintaining timestamps
+	 * because file writes don't upcall to fuse2fs.  The kernel's predicate
+	 * for deciding if [cm]time should be updated bases its decisions off
+	 * [cm]time being an exact match for the coarse clock (instead of
+	 * checking that [cm]time < coarse_clock) which means that fuse2fs
+	 * setting a fine-grained timestamp that is slightly ahead of the
+	 * coarse clock can result in timestamps appearing to go backwards.
+	 * generic/423 doesn't like seeing btime > ctime from statx, so we'll
+	 * use the coarse clock in iomap mode.
+	 */
+	if (fuse2fs_iomap_enabled(ff) &&
+	    !clock_gettime(CLOCK_REALTIME_COARSE, now))
+		return;
+#endif
 #ifdef CLOCK_REALTIME
 	if (!clock_gettime(CLOCK_REALTIME, now))
 		return;
@@ -869,7 +885,7 @@ static void fuse2fs_init_timestamps(struct fuse2fs *ff, ext2_ino_t ino,
 {
 	struct timespec now;
 
-	get_now(&now);
+	fuse2fs_get_now(ff, &now);
 	EXT4_INODE_SET_XTIME(i_atime, &now, inode);
 	EXT4_INODE_SET_XTIME(i_ctime, &now, inode);
 	EXT4_INODE_SET_XTIME(i_mtime, &now, inode);
@@ -888,7 +904,7 @@ static int fuse2fs_update_ctime(struct fuse2fs *ff, ext2_ino_t ino,
 	struct timespec now;
 	struct ext2_inode_large inode;
 
-	get_now(&now);
+	fuse2fs_get_now(ff, &now);
 
 	/* If user already has a inode buffer, just update that */
 	if (pinode) {
@@ -934,7 +950,7 @@ static int fuse2fs_update_atime(struct fuse2fs *ff, ext2_ino_t ino)
 	pinode = &inode;
 	EXT4_INODE_GET_XTIME(i_atime, &atime, pinode);
 	EXT4_INODE_GET_XTIME(i_mtime, &mtime, pinode);
-	get_now(&now);
+	fuse2fs_get_now(ff, &now);
 
 	datime = atime.tv_sec + ((double)atime.tv_nsec / NSEC_PER_SEC);
 	dmtime = mtime.tv_sec + ((double)mtime.tv_nsec / NSEC_PER_SEC);
@@ -969,7 +985,7 @@ static int fuse2fs_update_mtime(struct fuse2fs *ff, ext2_ino_t ino,
 	struct timespec now;
 
 	if (pinode) {
-		get_now(&now);
+		fuse2fs_get_now(ff, &now);
 		EXT4_INODE_SET_XTIME(i_mtime, &now, pinode);
 		EXT4_INODE_SET_XTIME(i_ctime, &now, pinode);
 		increment_version(pinode);
@@ -984,7 +1000,7 @@ static int fuse2fs_update_mtime(struct fuse2fs *ff, ext2_ino_t ino,
 	if (err)
 		return translate_error(fs, ino, err);
 
-	get_now(&now);
+	fuse2fs_get_now(ff, &now);
 	EXT4_INODE_SET_XTIME(i_mtime, &now, &inode);
 	EXT4_INODE_SET_XTIME(i_ctime, &now, &inode);
 	increment_version(&inode);
@@ -4978,9 +4994,9 @@ static int op_utimens(const char *path, const struct timespec ctv[2],
 	tv[1] = ctv[1];
 #ifdef UTIME_NOW
 	if (tv[0].tv_nsec == UTIME_NOW)
-		get_now(tv);
+		fuse2fs_get_now(ff, tv);
 	if (tv[1].tv_nsec == UTIME_NOW)
-		get_now(tv + 1);
+		fuse2fs_get_now(ff, tv + 1);
 #endif /* UTIME_NOW */
 #ifdef UTIME_OMIT
 	if (tv[0].tv_nsec != UTIME_OMIT)
@@ -7736,7 +7752,7 @@ static int __translate_error(ext2_filsys fs, ext2_ino_t ino, errcode_t err,
 			error_message(err), func, line);
 
 	/* Make a note in the error log */
-	get_now(&now);
+	fuse2fs_get_now(ff, &now);
 	ext2fs_set_tstamp(fs->super, s_last_error_time, now.tv_sec);
 	fs->super->s_last_error_ino = ino;
 	fs->super->s_last_error_line = line;
#183 lore raw

PATCH 7/10 fuse2fs: add tracing for retrieving timestamps

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Add tracing for retrieving timestamps so we can debug the weird behavior.

---
 misc/fuse2fs.c |   20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)
misc/fuse2fs.c +14 -6
index 9b536fe77dda37..b11ffec3603bf9 100644
@@ -1946,9 +1946,11 @@ static void *op_init(struct fuse_conn_info *conn,
 	return ff;
 }
 
-static int stat_inode(ext2_filsys fs, ext2_ino_t ino, struct stat *statbuf)
+static int fuse2fs_stat(struct fuse2fs *ff, ext2_ino_t ino,
+			struct stat *statbuf)
 {
 	struct ext2_inode_large inode;
+	ext2_filsys fs = ff->fs;
 	dev_t fakedev = 0;
 	errcode_t err;
 	int ret = 0;
@@ -1987,6 +1989,13 @@ static int stat_inode(ext2_filsys fs, ext2_ino_t ino, struct stat *statbuf)
 #else
 	statbuf->st_ctime = tv.tv_sec;
 #endif
+
+	dbg_printf(ff, "%s: ino=%d atime=%lld.%ld mtime=%lld.%ld ctime=%lld.%ld\n",
+		   __func__, ino,
+		   (long long int)statbuf->st_atim.tv_sec, statbuf->st_atim.tv_nsec,
+		   (long long int)statbuf->st_mtim.tv_sec, statbuf->st_mtim.tv_nsec,
+		   (long long int)statbuf->st_ctim.tv_sec, statbuf->st_ctim.tv_nsec);
+
 	if (LINUX_S_ISCHR(inode.i_mode) ||
 	    LINUX_S_ISBLK(inode.i_mode)) {
 		if (inode.i_block[0])
@@ -2033,16 +2042,15 @@ static int op_getattr(const char *path, struct stat *statbuf,
 		      struct fuse_file_info *fi)
 {
 	struct fuse2fs *ff = fuse2fs_get();
-	ext2_filsys fs;
 	ext2_ino_t ino;
 	int ret = 0;
 
 	FUSE2FS_CHECK_CONTEXT(ff);
-	fs = fuse2fs_start(ff);
+	fuse2fs_start(ff);
 	ret = fuse2fs_file_ino(ff, path, fi, &ino);
 	if (ret)
 		goto out;
-	ret = stat_inode(fs, ino, statbuf);
+	ret = fuse2fs_stat(ff, ino, statbuf);
 out:
 	fuse2fs_finish(ff, ret);
 	return ret;
@@ -3832,7 +3840,7 @@ static int fuse2fs_file_uses_iomap(struct fuse2fs *ff, ext2_ino_t ino)
 	if (!fuse2fs_iomap_enabled(ff))
 		return 0;
 
-	ret = stat_inode(ff->fs, ino, &statbuf);
+	ret = fuse2fs_stat(ff, ino, &statbuf);
 	if (ret)
 		return ret;
 
@@ -4741,7 +4749,7 @@ static int op_readdir_iter(ext2_ino_t dir EXT2FS_ATTR((unused)),
 			(unsigned long long)i->dirpos);
 
 	if (i->flags == FUSE_READDIR_PLUS) {
-		ret = stat_inode(i->fs, dirent->inode, &stat);
+		ret = fuse2fs_stat(i->ff, dirent->inode, &stat);
 		if (ret)
 			return DIRENT_ABORT;
 	}
#184 lore raw

PATCH 8/10 fuse2fs: enable syncfs

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Enable syncfs calls in fuse2fs.

---
 fuse4fs/fuse4fs.c |   32 ++++++++++++++++++++++++++++++++
 misc/fuse2fs.c    |   34 ++++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+)
fuse4fs/fuse4fs.c +32 -0
index 6cda267ad5cf40..3d48eb79948ad3 100644
@@ -6284,7 +6284,38 @@ static void op_shutdownfs(fuse_req_t req, fuse_ino_t ino, uint64_t flags)
 	int ret;
 
 	ret = ioctl_shutdown(ff, ctxt, NULL, NULL, 0);
+	fuse_reply_err(req, -ret);
+}
 
+static void op_syncfs(fuse_req_t req, fuse_ino_t ino)
+{
+	struct fuse4fs *ff = fuse4fs_get(req);
+	ext2_filsys fs;
+	errcode_t err;
+	int ret = 0;
+
+	FUSE4FS_CHECK_CONTEXT(req);
+	fs = fuse4fs_start(ff);
+
+	if (ff->opstate == F4OP_WRITABLE) {
+		if (fs->super->s_error_count)
+			fs->super->s_state |= EXT2_ERROR_FS;
+		ext2fs_mark_super_dirty(fs);
+		err = ext2fs_set_gdt_csum(fs);
+		if (err) {
+			ret = translate_error(fs, 0, err);
+			goto out_unlock;
+		}
+
+		err = ext2fs_flush2(fs, 0);
+		if (err) {
+			ret = translate_error(fs, 0, err);
+			goto out_unlock;
+		}
+	}
+
+out_unlock:
+	fuse4fs_finish(ff, ret);
 	fuse_reply_err(req, -ret);
 }
 #endif
@@ -7588,6 +7619,7 @@ static struct fuse_lowlevel_ops fs_ops = {
 	.freezefs = op_freezefs,
 	.unfreezefs = op_unfreezefs,
 	.shutdownfs = op_shutdownfs,
+	.syncfs = op_syncfs,
 #endif
 #ifdef HAVE_FUSE_IOMAP
 	.iomap_begin = op_iomap_begin,
misc/fuse2fs.c +34 -0
index b11ffec3603bf9..f7c759ed86c7fb 100644
@@ -5842,6 +5842,39 @@ static int op_shutdownfs(const char *path, uint64_t flags)
 
 	return ioctl_shutdown(ff, NULL, NULL);
 }
+
+static int op_syncfs(const char *path)
+{
+	struct fuse2fs *ff = fuse2fs_get();
+	ext2_filsys fs;
+	errcode_t err;
+	int ret = 0;
+
+	FUSE2FS_CHECK_CONTEXT(ff);
+	dbg_printf(ff, "%s: path=%s\n", __func__, path);
+	fs = fuse2fs_start(ff);
+
+	if (ff->opstate == F2OP_WRITABLE) {
+		if (fs->super->s_error_count)
+			fs->super->s_state |= EXT2_ERROR_FS;
+		ext2fs_mark_super_dirty(fs);
+		err = ext2fs_set_gdt_csum(fs);
+		if (err) {
+			ret = translate_error(fs, 0, err);
+			goto out_unlock;
+		}
+
+		err = ext2fs_flush2(fs, 0);
+		if (err) {
+			ret = translate_error(fs, 0, err);
+			goto out_unlock;
+		}
+	}
+
+out_unlock:
+	fuse2fs_finish(ff, ret);
+	return ret;
+}
 #endif
 
 #ifdef HAVE_FUSE_IOMAP
@@ -7131,6 +7164,7 @@ static struct fuse_operations fs_ops = {
 	.freezefs = op_freezefs,
 	.unfreezefs = op_unfreezefs,
 	.shutdownfs = op_shutdownfs,
+	.syncfs = op_syncfs,
 #endif
 #ifdef HAVE_FUSE_IOMAP
 	.iomap_begin = op_iomap_begin,
#185 lore raw

PATCH 9/10 fuse2fs: set sync, immutable, and append at file load time

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Convey these three inode flags to the kernel when we're loading a file. This way the kernel can advertise and enforce those flags so that the fuse server doesn't have to.

---
 fuse4fs/fuse4fs.c |   16 ++++++++++++++++
 misc/fuse2fs.c    |   53 ++++++++++++++++++++++++++++++++++++++---------------
 2 files changed, 54 insertions(+), 15 deletions(-)
fuse4fs/fuse4fs.c +16 -0
index 3d48eb79948ad3..8ba904ec2fc9d9 100644
@@ -2162,6 +2162,22 @@ static int fuse4fs_stat_inode(struct fuse4fs *ff, ext2_ino_t ino,
 	entry->entry_timeout = FUSE4FS_ATTR_TIMEOUT;
 
 	fstat->iflags = 0;
+
+#ifdef FUSE_IFLAG_SYNC
+	if (inodep->i_flags & EXT2_SYNC_FL)
+		fstat->iflags |= FUSE_IFLAG_SYNC;
+#endif
+
+#ifdef FUSE_IFLAG_IMMUTABLE
+	if (inodep->i_flags & EXT2_IMMUTABLE_FL)
+		fstat->iflags |= FUSE_IFLAG_IMMUTABLE;
+#endif
+
+#ifdef FUSE_IFLAG_APPEND
+	if (inodep->i_flags & EXT2_APPEND_FL)
+		fstat->iflags |= FUSE_IFLAG_APPEND;
+#endif
+
 #ifdef HAVE_FUSE_IOMAP
 	if (fuse4fs_iomap_enabled(ff)) {
 		fstat->iflags |= FUSE_IFLAG_IOMAP | FUSE_IFLAG_EXCLUSIVE;
misc/fuse2fs.c +38 -15

103 lines not on this page · raw message

#186 lore raw

PATCH 10/10 fuse4fs: increase attribute timeout in iomap mode

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

In iomap mode, we trust the kernel to cache file attributes, because it is critical to keep all of the file IO permissions checking in the kernel as part of keeping all the file IO paths in the kernel. Therefore, increase the attribute timeout to 30 seconds to reduce the number of upcalls even further.

---
 fuse4fs/fuse4fs.c |   15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
fuse4fs/fuse4fs.c +12 -3
index 8ba904ec2fc9d9..0f62d5a04fc4a4 100644
@@ -123,7 +123,8 @@
 #endif
 #endif /* !defined(ENODATA) */
 
-#define FUSE4FS_ATTR_TIMEOUT	(0.0)
+#define FUSE4FS_IOMAP_ATTR_TIMEOUT	(0.0)
+#define FUSE4FS_ATTR_TIMEOUT		(30.0)
 
 static inline uint64_t round_up(uint64_t b, unsigned int align)
 {
@@ -2158,8 +2159,14 @@ static int fuse4fs_stat_inode(struct fuse4fs *ff, ext2_ino_t ino,
 
 	fuse4fs_ino_to_fuse(ff, &entry->ino, ino);
 	entry->generation = inodep->i_generation;
-	entry->attr_timeout = FUSE4FS_ATTR_TIMEOUT;
-	entry->entry_timeout = FUSE4FS_ATTR_TIMEOUT;
+
+	if (fuse4fs_iomap_enabled(ff)) {
+		entry->attr_timeout = FUSE4FS_IOMAP_ATTR_TIMEOUT;
+		entry->entry_timeout = FUSE4FS_IOMAP_ATTR_TIMEOUT;
+	} else {
+		entry->attr_timeout = FUSE4FS_ATTR_TIMEOUT;
+		entry->entry_timeout = FUSE4FS_ATTR_TIMEOUT;
+	}
 
 	fstat->iflags = 0;
 
@@ -2392,6 +2399,8 @@ static void op_statx(fuse_req_t req, fuse_ino_t fino, int flags, int mask,
 	fuse4fs_finish(ff, ret);
 	if (ret)
 		fuse_reply_err(req, -ret);
+	else if (fuse4fs_iomap_enabled(ff))
+		fuse_reply_statx(req, 0, &stx, FUSE4FS_IOMAP_ATTR_TIMEOUT);
 	else
 		fuse_reply_statx(req, 0, &stx, FUSE4FS_ATTR_TIMEOUT);
 }
#187 lore raw

PATCH v7 4/8 fuse2fs: cache iomap mappings for even better file IO performance

In reply to Darrick J. Wong

Hi all,

This series improves the performance (and correctness for some filesystems) by adding the ability to cache iomap mappings in the kernel. For filesystems that can change mapping states during pagecache writeback (e.g. unwritten extent conversion) this is absolutely necessary to deal with races with writes to the pagecache because writeback does not take i_rwsem. For everyone else, it simply eliminates roundtrips to userspace.

If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.

Comments and questions are, as always, welcome.

e2fsprogs git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/e2fsprogs.git/log/?h=fuse2fs-iomap-cache
---
Commits in this patchset:
* fuse2fs: enable caching of iomaps
* fuse2fs: constrain iomap mapping cache size
* fuse2fs: enable iomap

---
 fuse4fs/fuse4fs.c |   44 +++++++++++++++++++++++++++++++++++++-------
 misc/fuse2fs.c    |   40 ++++++++++++++++++++++++++++++++++------
 2 files changed, 71 insertions(+), 13 deletions(-)
#188 lore raw

PATCH 1/3 fuse2fs: enable caching of iomaps

From: Darrick J. Wong <[email protected]>

Cache the iomaps we generate in the kernel for better performance.

---
 fuse4fs/fuse4fs.c |   31 +++++++++++++++++++++++++++++++
 misc/fuse2fs.c    |   30 ++++++++++++++++++++++++++++++
 2 files changed, 61 insertions(+)
fuse4fs/fuse4fs.c +31 -0
index 0f62d5a04fc4a4..d7238db25261dc 100644
@@ -292,6 +292,8 @@ struct fuse4fs {
 #ifdef STATX_WRITE_ATOMIC
 	unsigned int awu_min, awu_max;
 #endif
+	/* options set by fuse_opt_parse must be of type int */
+	int iomap_cache;
 #endif
 	unsigned int blockmask;
 	unsigned long offset;
@@ -6890,6 +6892,30 @@ static void op_iomap_begin(fuse_req_t req, fuse_ino_t fino, uint64_t dontcare,
 	if (opflags & FUSE_IOMAP_OP_ATOMIC)
 		read.flags |= FUSE_IOMAP_F_ATOMIC_BIO;
 
+	/*
+	 * Cache the mapping in the kernel so that we can reuse them for
+	 * subsequent IO.
+	 */
+	if (ff->iomap_cache) {
+		ret = fuse_lowlevel_iomap_upsert_mappings(ff->fuse, fino, ino,
+							  &read, NULL);
+		if (ret) {
+			/*
+			 * Log the cache upsert error, but we can still return
+			 * the mapping via the reply.  EINVAL is the magic code
+			 * for the kernel declining to cache the mapping.
+			 */
+			if (ret != -ENOMEM && ret != -EINVAL)
+				translate_error(fs, ino, -ret);
+			goto out_unlock;
+		}
+
+		/* Tell the kernel to retry from cache */
+		read.type = FUSE_IOMAP_TYPE_RETRY_CACHE;
+		read.dev = FUSE_IOMAP_DEV_NULL;
+		read.addr = FUSE_IOMAP_NULL_ADDR;
+	}
+
 out_unlock:
 	fuse4fs_finish(ff, ret);
 	if (ret)
@@ -7707,6 +7733,10 @@ static struct fuse_opt fuse4fs_opts[] = {
 #ifdef HAVE_CLOCK_MONOTONIC
 	FUSE4FS_OPT("timing",		timing,			1),
 #endif
+#ifdef HAVE_FUSE_IOMAP
+	FUSE4FS_OPT("iomap_cache",	iomap_cache,		1),
+	FUSE4FS_OPT("noiomap_cache",	iomap_cache,		0),
+#endif
 
 #ifdef HAVE_FUSE_IOMAP
 #ifdef MS_LAZYTIME
@@ -8119,6 +8149,7 @@ int main(int argc, char *argv[])
 		.iomap_want = FT_DEFAULT,
 		.iomap_state = IOMAP_UNKNOWN,
 		.iomap_dev = FUSE_IOMAP_DEV_NULL,
+		.iomap_cache = 1,
 #endif
 #ifdef HAVE_FUSE_LOOPDEV
 		.loop_fd = -1,
misc/fuse2fs.c +30 -0
index 58ea8e1f2f1e51..5a217b821c2d4a 100644
@@ -284,6 +284,8 @@ struct fuse2fs {
 #ifdef STATX_WRITE_ATOMIC
 	unsigned int awu_min, awu_max;
 #endif
+	/* options set by fuse_opt_parse must be of type int */
+	int iomap_cache;
 #endif
 	unsigned int blockmask;
 	unsigned long offset;
@@ -6444,6 +6446,29 @@ static int op_iomap_begin(const char *path, uint64_t nodeid, uint64_t attr_ino,
 	if (opflags & FUSE_IOMAP_OP_ATOMIC)
 		read->flags |= FUSE_IOMAP_F_ATOMIC_BIO;
 
+	/*
+	 * Cache the mapping in the kernel so that we can reuse them for
+	 * subsequent IO.
+	 */
+	if (ff->iomap_cache) {
+		ret = fuse_fs_iomap_upsert(nodeid, attr_ino, read, NULL);
+		if (ret) {
+			/*
+			 * Log the cache upsert error, but we can still return
+			 * the mapping via the reply.  EINVAL is the magic code
+			 * for the kernel declining to cache the mapping.
+			 */
+			if (ret != -ENOMEM && ret != -EINVAL)
+				translate_error(fs, attr_ino, -ret);
+			goto out_unlock;
+		}
+
+		/* Tell the kernel to retry from cache */
+		read->type = FUSE_IOMAP_TYPE_RETRY_CACHE;
+		read->dev = FUSE_IOMAP_DEV_NULL;
+		read->addr = FUSE_IOMAP_NULL_ADDR;
+	}
+
 out_unlock:
 	fuse2fs_finish(ff, ret);
 	return ret;
@@ -7250,6 +7275,10 @@ static struct fuse_opt fuse2fs_opts[] = {
 #ifdef HAVE_CLOCK_MONOTONIC
 	FUSE2FS_OPT("timing",		timing,			1),
 #endif
+#ifdef HAVE_FUSE_IOMAP
+	FUSE2FS_OPT("iomap_cache",	iomap_cache,		1),
+	FUSE2FS_OPT("noiomap_cache",	iomap_cache,		0),
+#endif
 
 #ifdef HAVE_FUSE_IOMAP
 #ifdef MS_LAZYTIME
@@ -7530,6 +7559,7 @@ int main(int argc, char *argv[])
 		.iomap_want = FT_DEFAULT,
 		.iomap_state = IOMAP_UNKNOWN,
 		.iomap_dev = FUSE_IOMAP_DEV_NULL,
+		.iomap_cache = 1,
 #endif
 #ifdef HAVE_FUSE_LOOPDEV
 		.loop_fd = -1,
#189 lore raw

PATCH 2/3 fuse2fs: constrain iomap mapping cache size

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Update the iomap config functions to handle the new iomap mapping cache size restriction knob.

---
 fuse4fs/fuse4fs.c |    9 ++++++---
 misc/fuse2fs.c    |    6 ++++--
 2 files changed, 10 insertions(+), 5 deletions(-)
fuse4fs/fuse4fs.c +6 -3
index d7238db25261dc..c5cf471d630451 100644
@@ -7187,7 +7187,8 @@ static void fuse4fs_alloc_stats_range(ext2_filsys fs, blk64_t blk, blk_t num,
 		ff->old_alloc_stats_range(fs, blk, num, inuse);
 }
 
-static void op_iomap_config(fuse_req_t req, uint64_t flags, uint64_t maxbytes)
+static void op_iomap_config(fuse_req_t req, uint64_t flags, uint64_t maxbytes,
+			    uint32_t cache_maxbytes)
 {
 	struct fuse_iomap_config cfg = { };
 	struct fuse4fs *ff = fuse4fs_get(req);
@@ -7196,9 +7197,11 @@ static void op_iomap_config(fuse_req_t req, uint64_t flags, uint64_t maxbytes)
 
 	FUSE4FS_CHECK_CONTEXT(req);
 
-	dbg_printf(ff, "%s: flags=0x%llx maxbytes=0x%llx\n", __func__,
+	dbg_printf(ff, "%s: flags=0x%llx maxbytes=0x%llx cache_maxbytes=0x%x\n",
+		   __func__,
 		   (unsigned long long)flags,
-		   (unsigned long long)maxbytes);
+		   (unsigned long long)maxbytes,
+		   cache_maxbytes);
 	fs = fuse4fs_start(ff);
 
 	cfg.flags |= FUSE_IOMAP_CONFIG_UUID;
misc/fuse2fs.c +4 -2
index 5a217b821c2d4a..4dab8034ebb317 100644
@@ -6737,6 +6737,7 @@ static void fuse2fs_alloc_stats_range(ext2_filsys fs, blk64_t blk, blk_t num,
 }
 
 static int op_iomap_config(uint64_t flags, off_t maxbytes,
+			   uint32_t cache_maxbytes,
 			   struct fuse_iomap_config *cfg)
 {
 	struct fuse2fs *ff = fuse2fs_get();
@@ -6745,9 +6746,10 @@ static int op_iomap_config(uint64_t flags, off_t maxbytes,
 
 	FUSE2FS_CHECK_CONTEXT(ff);
 
-	dbg_printf(ff, "%s: flags=0x%llx maxbytes=0x%llx\n", __func__,
+	dbg_printf(ff, "%s: flags=0x%llx maxbytes=0x%llx cache_maxbytes=%u\n", __func__,
 		   (unsigned long long)flags,
-		   (unsigned long long)maxbytes);
+		   (unsigned long long)maxbytes,
+		   cache_maxbytes);
 	fs = fuse2fs_start(ff);
 
 	cfg->flags |= FUSE_IOMAP_CONFIG_UUID;
#190 lore raw

PATCH 3/3 fuse2fs: enable iomap

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Now that iomap functionality is complete, enable this for users.

---
 fuse4fs/fuse4fs.c |    4 ----
 misc/fuse2fs.c    |    4 ----
 2 files changed, 8 deletions(-)
fuse4fs/fuse4fs.c +0 -4
index c5cf471d630451..e8edeb8f62e88a 100644
@@ -2021,10 +2021,6 @@ static inline bool fuse4fs_wants_iomap(struct fuse4fs *ff)
 static void fuse4fs_iomap_enable(struct fuse_conn_info *conn,
 				 struct fuse4fs *ff)
 {
-	/* Don't let anyone touch iomap until the end of the patchset. */
-	ff->iomap_state = IOMAP_DISABLED;
-	return;
-
 	if (fuse4fs_wants_iomap(ff) &&
 	    fuse_set_feature_flag(conn, FUSE_CAP_IOMAP))
 		ff->iomap_state = IOMAP_ENABLED;
misc/fuse2fs.c +0 -4
index 4dab8034ebb317..96118942c324ba 100644
@@ -1847,10 +1847,6 @@ static inline bool fuse2fs_wants_iomap(struct fuse2fs *ff)
 static void fuse2fs_iomap_enable(struct fuse_conn_info *conn,
 				 struct fuse2fs *ff)
 {
-	/* Don't let anyone touch iomap until the end of the patchset. */
-	ff->iomap_state = IOMAP_DISABLED;
-	return;
-
 	if (fuse2fs_wants_iomap(ff) &&
 	    fuse_set_feature_flag(conn, FUSE_CAP_IOMAP))
 		ff->iomap_state = IOMAP_ENABLED;
#191 lore raw

PATCH v7 5/8 fuse2fs: improve block and inode caching

In reply to Darrick J. Wong

Hi all,

This series ports the libext2fs inode cache to the new cache.c hashtable code that was added for fuse4fs unlinked file support and improves on the UNIX I/O manager's block cache by adding a new I/O manager that does its own caching. Now we no longer have statically sized buffer caching for the two fuse servers.

If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.

Comments and questions are, as always, welcome.

e2fsprogs git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/e2fsprogs.git/log/?h=fuse2fs-caching
---
Commits in this patchset:
* libsupport: add caching IO manager
* iocache: add the actual buffer cache
* iocache: bump buffer mru priority every 50 accesses
* fuse2fs: enable caching IO manager
* fuse2fs: increase inode cache size
* libext2fs: improve caching for inodes

---
 lib/ext2fs/ext2fsP.h     |   13 +
 lib/support/cache.h      |    1 
 lib/support/iocache.h    |   17 +
 debugfs/Makefile.in      |    8 
 e2fsck/Makefile.in       |   12 -
 fuse4fs/Makefile.in      |   11 -
 fuse4fs/fuse4fs.c        |    8 
 lib/ext2fs/Makefile.in   |   70 ++--
 lib/ext2fs/inline_data.c |    4 
 lib/ext2fs/inode.c       |  215 ++++++++++---
 lib/ext2fs/io_manager.c  |    3 
 lib/support/Makefile.in  |    6 
 lib/support/cache.c      |   16 +
 lib/support/iocache.c    |  765 ++++++++++++++++++++++++++++++++++++++++++++++
 misc/Makefile.in         |   12 -
 misc/fuse2fs.c           |   10 +
 resize/Makefile.in       |   11 -
 tests/fuzz/Makefile.in   |    4 
 tests/progs/Makefile.in  |    4 
 19 files changed, 1068 insertions(+), 122 deletions(-)
 create mode 100644 lib/support/iocache.h
 create mode 100644 lib/support/iocache.c
#192 lore raw

PATCH 1/6 libsupport: add caching IO manager

From: Darrick J. Wong <[email protected]>

Start creating a caching IO manager so that we can have better caching of metadata blocks in fuse2fs. For now it's just a passthrough cache.

---
 lib/support/iocache.h   |   17 +++
 lib/ext2fs/io_manager.c |    3 
 lib/support/Makefile.in |    6 +
 lib/support/iocache.c   |  317 +++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 342 insertions(+), 1 deletion(-)
 create mode 100644 lib/support/iocache.h
 create mode 100644 lib/support/iocache.c
lib/support/iocache.h new file +17 -0
new file mode 100644
index 00000000000000..502eede08aadc5
@@ -0,0 +1,17 @@
+/*
+ * iocache.h - IO cache
+ *
+ * Copyright (C) 2025-2026 Oracle.
+ *
+ * %Begin-Header%
+ * This file may be redistributed under the terms of the GNU Public
+ * License.
+ * %End-Header%
+ */
+#ifndef __IOCACHE_H__
+#define __IOCACHE_H__
+
+errcode_t iocache_set_backing_manager(io_manager manager);
+extern io_manager iocache_io_manager;
+
+#endif /* __IOCACHE_H__ */
lib/ext2fs/io_manager.c +3 -0
index a92dba7b9dc880..a93415c151ffa0 100644
@@ -16,9 +16,12 @@
 #if HAVE_SYS_TYPES_H
 #include <sys/types.h>
 #endif
+#include <stdbool.h>
 
 #include "ext2_fs.h"
 #include "ext2fs.h"
+#include "support/list.h"
+#include "support/cache.h"
 
 errcode_t io_channel_set_options(io_channel channel, const char *opts)
 {
lib/support/Makefile.in +5 -1
index a09814f574008c..2950e80222ee72 100644
@@ -15,6 +15,7 @@ all::
 
 OBJS=		bthread.o \
 		cstring.o \
+		iocache.o \
 		mkquota.o \
 		plausible.o \
 		profile.o \
@@ -44,7 +45,8 @@ SRCS=		$(srcdir)/argv_parse.c \
 		$(srcdir)/quotaio_v2.c \
 		$(srcdir)/dict.c \
 		$(srcdir)/devname.c \
-		$(srcdir)/cache.c
+		$(srcdir)/cache.c \
+		$(srcdir)/iocache.c
 
 LIBRARY= libsupport
 LIBDIR= support
@@ -191,3 +193,5 @@ devname.o: $(srcdir)/devname.c $(top_builddir)/lib/config.h \
  $(top_builddir)/lib/dirpaths.h $(srcdir)/devname.h $(srcdir)/nls-enable.h
 cache.o: $(srcdir)/cache.c $(top_builddir)/lib/config.h \
  $(srcdir)/cache.h $(srcdir)/list.h $(srcdir)/xbitops.h
+iocache.o: $(srcdir)/iocache.c $(top_builddir)/lib/config.h \
+ $(srcdir)/iocache.h $(srcdir)/cache.h $(srcdir)/list.h $(srcdir)/xbitops.h
lib/support/iocache.c new file +317 -0

317 lines not on this page · raw message

#193 lore raw

PATCH 2/6 iocache: add the actual buffer cache

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Wire up buffer caching into our new caching IO manager.

---
 lib/support/iocache.c |  483 +++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 461 insertions(+), 22 deletions(-)
lib/support/iocache.c +461 -22
index 615f1fcc2d7f84..b72dcb7ebb470a 100644
@@ -9,46 +9,287 @@
  * %End-Header%
  */
 #include "config.h"
+#include <assert.h>
+#include <stdbool.h>
+#include <pthread.h>
+#include <unistd.h>
+#include <limits.h>
 #include "ext2fs/ext2_fs.h"
 #include "ext2fs/ext2fs.h"
 #include "ext2fs/ext2fsP.h"
 #include "support/iocache.h"
+#include "support/list.h"
+#include "support/cache.h"
 
 #define IOCACHE_IO_CHANNEL_MAGIC	0x424F5254	/* BORT */
 
 static io_manager iocache_backing_manager;
 
+static inline uint64_t B_TO_FSBT(io_channel channel, uint64_t number) {
+	return number / channel->block_size;
+}
+
+static inline uint64_t B_TO_FSB(io_channel channel, uint64_t number) {
+	return (number + channel->block_size - 1) / channel->block_size;
+}
+
 struct iocache_private_data {
 	int			magic;
-	io_channel		real;
+	io_channel		real;		/* lower level io channel */
+	io_channel		channel;	/* cache channel */
+	struct cache		cache;
+	pthread_mutex_t		stats_lock;
+	struct struct_io_stats	io_stats;
+	unsigned long long	write_errors;
 };
 
+#define IOCACHEDATA(cache) \
+	(container_of(cache, struct iocache_private_data, cache))
+
 static struct iocache_private_data *IOCACHE(io_channel channel)
 {
 	return (struct iocache_private_data *)channel->private_data;
 }
 
-static errcode_t iocache_read_error(io_channel channel, unsigned long block,
-				    int count, void *data, size_t size,
-				    int actual_bytes_read, errcode_t error)
+struct iocache_buf {
+	struct cache_node	node;
+	struct list_head	list;
+	blk64_t			block;
+	void			*buf;
+	errcode_t		write_error;
+	unsigned int		uptodate:1;
+	unsigned int		dirty:1;
+};
+
+static inline void iocache_buf_lock(struct iocache_buf *ubuf)
+{
+	pthread_mutex_lock(&ubuf->node.cn_mutex);
+}
+
+static inline void iocache_buf_unlock(struct iocache_buf *ubuf)
+{
+	pthread_mutex_unlock(&ubuf->node.cn_mutex);
+}
+
+struct iocache_key {
+	blk64_t			block;
+};
+
+#define IOKEY(key)	((struct iocache_key *)(key))
+#define IOBUF(node)	(container_of((node), struct iocache_buf, node))
+
+static unsigned int
+iocache_hash(cache_key_t key, unsigned int hashsize, unsigned int hashshift)
+{
+	uint64_t	hashval = IOKEY(key)->block;
+	uint64_t	tmp;
+
+	tmp = hashval ^ (GOLDEN_RATIO_PRIME + hashval) / CACHE_LINE_SIZE;
+	tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> hashshift);
+	return tmp % hashsize;
+}
+
+static int iocache_compare(struct cache_node *node, cache_key_t key)
+{
+	struct iocache_buf *ubuf = IOBUF(node);
+	struct iocache_key *ukey = IOKEY(key);
+
+	if (ubuf->block == ukey->block)
+		return CACHE_HIT;
+
+	return CACHE_MISS;
+}
+
+static struct cache_node *iocache_alloc_node(struct cache *cache,
+					     cache_key_t key)
+{
+	struct iocache_private_data *data = IOCACHEDATA(cache);
+	struct iocache_key *ukey = IOKEY(key);
+	struct iocache_buf *ubuf;
+	errcode_t retval;
+
+	retval = ext2fs_get_mem(sizeof(struct iocache_buf), &ubuf);
+	if (retval)
+		return NULL;
+	memset(ubuf, 0, sizeof(*ubuf));
+
+	retval = io_channel_alloc_buf(data->channel, 0, &ubuf->buf);
+	if (retval) {
+		free(ubuf);
+		return NULL;
+	}
+	memset(ubuf->buf, 0, data->channel->block_size);
+
+	INIT_LIST_HEAD(&ubuf->list);
+	ubuf->block = ukey->block;
+	return &ubuf->node;
+}
+
+static bool iocache_flush_node(struct cache *cache, struct cache_node *node)
+{
+	struct iocache_private_data *data = IOCACHEDATA(cache);
+	struct iocache_buf *ubuf = IOBUF(node);
+	errcode_t retval;
+
+	if (ubuf->dirty) {
+		retval = io_channel_write_blk64(data->real, ubuf->block, 1,
+						ubuf->buf);
+		if (retval) {
+			ubuf->write_error = retval;
+			data->write_errors++;
+		} else {
+			ubuf->dirty = 0;
+			ubuf->write_error = 0;
+		}
+	}
+
+	return ubuf->dirty;
+}
+
+static void iocache_relse(struct cache *cache, struct cache_node *node)
+{
+	struct iocache_buf *ubuf = IOBUF(node);
+
+	ext2fs_free_mem(&ubuf->buf);
+	ext2fs_free_mem(&ubuf);
+}
+
+static unsigned int iocache_bulkrelse(struct cache *cache,
+				      struct list_head *list)
+{
+	struct cache_node *cn, *n;
+	int count = 0;
+
+	if (list_empty(list))
+		return 0;
+
+	list_for_each_entry_safe(cn, n, list, cn_mru) {
+		iocache_relse(cache, cn);
+		count++;
+	}
+
+	return count;
+}
+
+/* Flush all dirty buffers in the cache to disk. */
+static errcode_t iocache_flush_cache(struct iocache_private_data *data)
+{
+	return cache_flush(&data->cache) ? 0 : EIO;
+}
+
+/* Flush all dirty buffers in this range of the cache to disk. */
+static errcode_t iocache_flush_range(struct iocache_private_data *data,
+				     blk64_t block, uint64_t count)
+{
+	uint64_t i;
+	bool still_dirty = false;
+
+	for (i = 0; i < count; i++) {
+		struct iocache_key ukey = {
+			.block = block + i,
+		};
+		struct cache_node *node;
+
+		cache_node_get(&data->cache, &ukey, CACHE_GET_INCORE,
+			       &node);
+		if (!node)
+			continue;
+
+		/* cache_flush holds cn_mutex across the node flush */
+		pthread_mutex_unlock(&node->cn_mutex);
+		still_dirty |= iocache_flush_node(&data->cache, node);
+		pthread_mutex_unlock(&node->cn_mutex);
+
+		cache_node_put(&data->cache, node);
+	}
+
+	return still_dirty ? EIO : 0;
+}
+
+static void iocache_add_list(struct cache *cache, struct cache_node *node,
+			     void *data)
+{
+	struct iocache_buf *ubuf = IOBUF(node);
+	struct list_head *list = data;
+
+	assert(node->cn_count == 0 || node->cn_count == 1);
+
+	iocache_buf_lock(ubuf);
+	cache_node_grab(cache, node);
+	list_add_tail(&ubuf->list, list);
+	iocache_buf_unlock(ubuf);
+}
+
+static void iocache_invalidate_bufs(struct iocache_private_data *data,
+				    struct list_head *list)
+{
+	struct iocache_buf *ubuf, *n;
+
+	list_for_each_entry_safe(ubuf, n, list, list) {
+		struct iocache_key ukey = {
+			.block = ubuf->block,
+		};
+
+		assert(ubuf->node.cn_count == 1);
+
+		iocache_buf_lock(ubuf);
+		ubuf->dirty = 0;
+		list_del_init(&ubuf->list);
+		iocache_buf_unlock(ubuf);
+
+		cache_node_put(&data->cache, &ubuf->node);
+		cache_node_purge(&data->cache, &ukey, &ubuf->node);
+	}
+}
+
+/*
+ * Remove all blocks from the cache.  Dirty contents are discarded.  Buffer
+ * refcounts must be zero!
+ */
+static void iocache_invalidate_cache(struct iocache_private_data *data)
 {
-	io_channel iocache_channel = channel->app_data;
+	LIST_HEAD(list);
 
-	return iocache_channel->read_error(iocache_channel, block, count, data,
-					   size, actual_bytes_read, error);
+	cache_walk(&data->cache, iocache_add_list, &list);
+	iocache_invalidate_bufs(data, &list);
 }
 
-static errcode_t iocache_write_error(io_channel channel, unsigned long block,
-				     int count, const void *data, size_t size,
-				     int actual_bytes_written,
-				     errcode_t error)
+/*
+ * Remove a range of blocks from the cache.  Dirty contents are discarded.
+ * Buffer refcounts must be zero!
+ */
+static void iocache_invalidate_range(struct iocache_private_data *data,
+				     blk64_t block, uint64_t count)
 {
-	io_channel iocache_channel = channel->app_data;
+	LIST_HEAD(list);
+	uint64_t i;
 
-	return iocache_channel->write_error(iocache_channel, block, count, data,
-					    size, actual_bytes_written, error);
+	for (i = 0; i < count; i++) {
+		struct iocache_key ukey = {
+			.block = block + i,
+		};
+		struct cache_node *node;
+
+		cache_node_get(&data->cache, &ukey, CACHE_GET_INCORE,
+			       &node);
+		if (node) {
+			iocache_add_list(&data->cache, node, &list);
+			cache_node_put(&data->cache, node);
+		}
+	}
+	iocache_invalidate_bufs(data, &list);
 }
 
+static const struct cache_operations iocache_ops = {
+	.hash		= iocache_hash,
+	.alloc		= iocache_alloc_node,
+	.flush		= iocache_flush_node,
+	.relse		= iocache_relse,
+	.compare	= iocache_compare,
+	.bulkrelse	= iocache_bulkrelse,
+	.resize		= cache_gradual_resize,
+};
+
 static errcode_t iocache_open(const char *name, int flags, io_channel *channel)
 {
 	io_channel	io = NULL;
@@ -65,6 +306,9 @@ static errcode_t iocache_open(const char *name, int flags, io_channel *channel)
 	if (retval)
 		return retval;
 
+	/* disable any static cache in the lower io manager */
+	io_channel_set_options(real, "cache=off");
+
 	retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
 	if (retval)
 		goto out_backing;
@@ -76,12 +320,19 @@ static errcode_t iocache_open(const char *name, int flags, io_channel *channel)
 		goto out_channel;
 	memset(data, 0, sizeof(struct iocache_private_data));
 	data->magic = IOCACHE_IO_CHANNEL_MAGIC;
+	data->io_stats.num_fields = 4;
+	data->channel = io;
 
 	io->manager = iocache_io_manager;
 	retval = ext2fs_get_mem(strlen(name) + 1, &io->name);
 	if (retval)
 		goto out_data;
 
+	retval = cache_init(CACHE_AUTO_SHRINK, 1U << 10, &iocache_ops,
+			    &data->cache);
+	if (retval)
+		goto out_name;
+
 	strcpy(io->name, name);
 	io->private_data = data;
 	io->block_size = real->block_size;
@@ -91,12 +342,14 @@ static errcode_t iocache_open(const char *name, int flags, io_channel *channel)
 	io->flags = real->flags;
 	data->real = real;
 	real->app_data = io;
-	real->read_error = iocache_read_error;
-	real->write_error = iocache_write_error;
+
+	pthread_mutex_init(&data->stats_lock, NULL);
 
 	*channel = io;
 	return 0;
 
+out_name:
+	ext2fs_free_mem(&io->name);
 out_data:
 	ext2fs_free_mem(&data);
 out_channel:
@@ -116,6 +369,10 @@ static errcode_t iocache_close(io_channel channel)
 
 	if (--channel->refcount > 0)
 		return 0;
+	pthread_mutex_destroy(&data->stats_lock);
+	cache_flush(&data->cache);
+	cache_purge(&data->cache);
+	cache_destroy(&data->cache);
 	if (data->real)
 		retval = io_channel_close(data->real);
 	ext2fs_free_mem(&channel->private_data);
@@ -134,6 +391,11 @@ static errcode_t iocache_set_blksize(io_channel channel, int blksize)
 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
 	EXT2_CHECK_MAGIC(data, IOCACHE_IO_CHANNEL_MAGIC);
 
+	retval = iocache_flush_cache(data);
+	if (retval)
+		return retval;
+	iocache_invalidate_cache(data);
+
 	retval = io_channel_set_blksize(data->real, blksize);
 	if (retval)
 		return retval;
@@ -145,21 +407,34 @@ static errcode_t iocache_set_blksize(io_channel channel, int blksize)
 static errcode_t iocache_flush(io_channel channel)
 {
 	struct iocache_private_data *data = IOCACHE(channel);
+	errcode_t retval = 0;
+	errcode_t retval2;
 
 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
 	EXT2_CHECK_MAGIC(data, IOCACHE_IO_CHANNEL_MAGIC);
 
-	return io_channel_flush(data->real);
+	retval = iocache_flush_cache(data);
+	retval2 = io_channel_flush(data->real);
+	if (retval)
+		return retval;
+	return retval2;
 }
 
 static errcode_t iocache_write_byte(io_channel channel, unsigned long offset,
 				    int count, const void *buf)
 {
 	struct iocache_private_data *data = IOCACHE(channel);
+	blk64_t bno = B_TO_FSBT(channel, offset);
+	blk64_t next_bno = B_TO_FSB(channel, offset + count);
+	errcode_t retval;
 
 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
 	EXT2_CHECK_MAGIC(data, IOCACHE_IO_CHANNEL_MAGIC);
 
+	retval = iocache_flush_range(data, bno, next_bno - bno);
+	if (retval)
+		return retval;
+	iocache_invalidate_range(data, bno, next_bno - bno);
 	return io_channel_write_byte(data->real, offset, count, buf);
 }
 
@@ -170,6 +445,31 @@ static errcode_t iocache_set_option(io_channel channel, const char *option,
 
 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
 	EXT2_CHECK_MAGIC(data, IOCACHE_IO_CHANNEL_MAGIC);
+	errcode_t retval;
+
+	/* don't let unix io cache= options leak through */
+	if (!strcmp(option, "cache"))
+		return 0;
+
+	if (!strcmp(option, "cache_blocks")) {
+		long long size;
+
+		if (!arg)
+			return EXT2_ET_INVALID_ARGUMENT;
+
+		errno = 0;
+		size = strtoll(arg, NULL, 0);
+		if (errno || size == 0 || size > UINT_MAX)
+			return EXT2_ET_INVALID_ARGUMENT;
+
+		cache_set_maxcount(&data->cache, size);
+		return 0;
+	}
+
+	retval = iocache_flush_cache(data);
+	if (retval)
+		return retval;
+	iocache_invalidate_cache(data);
 
 	return data->real->manager->set_option(data->real, option, arg);
 }
@@ -181,31 +481,157 @@ static errcode_t iocache_get_stats(io_channel channel, io_stats *io_stats)
 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
 	EXT2_CHECK_MAGIC(data, IOCACHE_IO_CHANNEL_MAGIC);
 
-	return data->real->manager->get_stats(data->real, io_stats);
+	/*
+	 * Yes, io_stats is a double-pointer, and we let the caller scribble on
+	 * our stats struct WITHOUT LOCKING!
+	 */
+	if (io_stats)
+		*io_stats = &data->io_stats;
+	return 0;
+}
+
+static void iocache_update_stats(struct iocache_private_data *data,
+				 unsigned long long bytes_read,
+				 unsigned long long bytes_written,
+				 int cache_op)
+{
+	pthread_mutex_lock(&data->stats_lock);
+	data->io_stats.bytes_read += bytes_read;
+	data->io_stats.bytes_written += bytes_written;
+	if (cache_op == CACHE_HIT)
+		data->io_stats.cache_hits++;
+	else
+		data->io_stats.cache_misses++;
+	pthread_mutex_unlock(&data->stats_lock);
 }
 
 static errcode_t iocache_read_blk64(io_channel channel,
 				    unsigned long long block, int count,
 				    void *buf)
 {
+	struct iocache_key ukey = {
+		.block = block,
+	};
 	struct iocache_private_data *data = IOCACHE(channel);
+	unsigned long long i;
+	errcode_t retval;
 
 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
 	EXT2_CHECK_MAGIC(data, IOCACHE_IO_CHANNEL_MAGIC);
 
-	return io_channel_read_blk64(data->real, block, count, buf);
+	/*
+	 * If we're doing an odd-sized read, flush out the cache and then do a
+	 * direct read.
+	 */
+	if (count < 0) {
+		uint64_t fsbcount = B_TO_FSB(channel, -count);
+
+		retval = iocache_flush_range(data, block, fsbcount);
+		if (retval)
+			return retval;
+		iocache_invalidate_range(data, block, fsbcount);
+		iocache_update_stats(data, 0, 0, CACHE_MISS);
+		return io_channel_read_blk64(data->real, block, count, buf);
+	}
+
+	for (i = 0; i < count; i++, ukey.block++, buf += channel->block_size) {
+		struct cache_node *node;
+		struct iocache_buf *ubuf;
+
+		cache_node_get(&data->cache, &ukey, 0, &node);
+		if (!node) {
+			/* cannot instantiate cache, just do a direct read */
+			retval = io_channel_read_blk64(data->real, ukey.block,
+						       1, buf);
+			if (retval)
+				return retval;
+			iocache_update_stats(data, channel->block_size, 0,
+					     CACHE_MISS);
+			continue;
+		}
+
+		ubuf = IOBUF(node);
+		iocache_buf_lock(ubuf);
+		if (!ubuf->uptodate) {
+			retval = io_channel_read_blk64(data->real, ukey.block,
+						       1, ubuf->buf);
+			if (!retval) {
+				ubuf->uptodate = 1;
+				iocache_update_stats(data, channel->block_size,
+						     0, CACHE_MISS);
+			}
+		} else {
+			iocache_update_stats(data, channel->block_size, 0,
+					     CACHE_HIT);
+		}
+		if (ubuf->uptodate)
+			memcpy(buf, ubuf->buf, channel->block_size);
+		iocache_buf_unlock(ubuf);
+		cache_node_put(&data->cache, node);
+		if (retval)
+			return retval;
+	}
+
+	return 0;
 }
 
 static errcode_t iocache_write_blk64(io_channel channel,
 				     unsigned long long block, int count,
 				     const void *buf)
 {
+	struct iocache_key ukey = {
+		.block = block,
+	};
 	struct iocache_private_data *data = IOCACHE(channel);
+	unsigned long long i;
+	errcode_t retval;
 
 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
 	EXT2_CHECK_MAGIC(data, IOCACHE_IO_CHANNEL_MAGIC);
 
-	return io_channel_write_blk64(data->real, block, count, buf);
+	/*
+	 * If we're doing an odd-sized write, flush out the cache and then do a
+	 * direct write.
+	 */
+	if (count < 0) {
+		uint64_t fsbcount = B_TO_FSB(channel, -count);
+
+		retval = iocache_flush_range(data, block, fsbcount);
+		if (retval)
+			return retval;
+		iocache_invalidate_range(data, block, fsbcount);
+		iocache_update_stats(data, 0, 0, CACHE_MISS);
+		return io_channel_write_blk64(data->real, block, count, buf);
+	}
+
+	for (i = 0; i < count; i++, ukey.block++, buf += channel->block_size) {
+		struct cache_node *node;
+		struct iocache_buf *ubuf;
+
+		cache_node_get(&data->cache, &ukey, 0, &node);
+		if (!node) {
+			/* cannot instantiate cache, do a direct write */
+			retval = io_channel_write_blk64(data->real, ukey.block,
+							1, buf);
+			if (retval)
+				return retval;
+			iocache_update_stats(data, 0, channel->block_size,
+					     CACHE_MISS);
+			continue;
+		}
+
+		ubuf = IOBUF(node);
+		iocache_buf_lock(ubuf);
+		memcpy(ubuf->buf, buf, channel->block_size);
+		iocache_update_stats(data, 0, channel->block_size,
+				     ubuf->uptodate ? CACHE_HIT : CACHE_MISS);
+		ubuf->dirty = 1;
+		ubuf->uptodate = 1;
+		iocache_buf_unlock(ubuf);
+		cache_node_put(&data->cache, node);
+	}
+
+	return 0;
 }
 
 static errcode_t iocache_read_blk(io_channel channel, unsigned long block,
@@ -224,11 +650,17 @@ static errcode_t iocache_discard(io_channel channel, unsigned long long block,
 				 unsigned long long count)
 {
 	struct iocache_private_data *data = IOCACHE(channel);
+	errcode_t retval;
 
 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
 	EXT2_CHECK_MAGIC(data, IOCACHE_IO_CHANNEL_MAGIC);
 
-	return io_channel_discard(data->real, block, count);
+	retval = io_channel_discard(data->real, block, count);
+	if (retval)
+		return retval;
+
+	iocache_invalidate_range(data, block, count);
+	return 0;
 }
 
 static errcode_t iocache_cache_readahead(io_channel channel,
@@ -247,11 +679,17 @@ static errcode_t iocache_zeroout(io_channel channel, unsigned long long block,
 				 unsigned long long count)
 {
 	struct iocache_private_data *data = IOCACHE(channel);
+	errcode_t retval;
 
 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
 	EXT2_CHECK_MAGIC(data, IOCACHE_IO_CHANNEL_MAGIC);
 
-	return io_channel_zeroout(data->real, block, count);
+	retval = io_channel_zeroout(data->real, block, count);
+	if (retval)
+		return retval;
+
+	iocache_invalidate_range(data, block, count);
+	return 0;
 }
 
 static errcode_t iocache_get_fd(io_channel channel, int *fd)
@@ -273,6 +711,7 @@ static errcode_t iocache_invalidate_blocks(io_channel channel,
 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
 	EXT2_CHECK_MAGIC(data, IOCACHE_IO_CHANNEL_MAGIC);
 
+	iocache_invalidate_range(data, block, count);
 	return io_channel_invalidate_blocks(data->real, block, count);
 }
 
#194 lore raw

PATCH 3/6 iocache: bump buffer mru priority every 50 accesses

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

If a buffer is hot enough to survive more than 50 access without being reclaimed, bump its priority to the next MRU so it sticks around longer.

---
 lib/support/cache.h   |    1 +
 lib/support/cache.c   |   16 ++++++++++++++++
 lib/support/iocache.c |    9 +++++++++
 3 files changed, 26 insertions(+)
lib/support/cache.h +1 -0
index 71fb9762f97866..d81726288bdc88 100644
@@ -180,5 +180,6 @@ int cache_node_purge(struct cache *, cache_key_t, struct cache_node *);
 void cache_report(FILE *fp, const char *, struct cache *);
 int cache_overflowed(struct cache *);
 struct cache_node *cache_node_grab(struct cache *cache, struct cache_node *node);
+void cache_node_bump_priority(struct cache *cache, struct cache_node *node);
 
 #endif	/* __CACHE_H__ */
lib/support/cache.c +16 -0
index 3a9e276f11af72..513a71829193a8 100644
@@ -678,6 +678,22 @@ cache_node_put(
 		cache_shrink(cache);
 }
 
+/* Bump the priority of a cache node.  Caller must hold cn_mutex. */
+void
+cache_node_bump_priority(
+	struct cache		*cache,
+	struct cache_node	*node)
+{
+	int			*priop;
+
+	if (node->cn_priority == CACHE_DIRTY_PRIORITY)
+		priop = &node->cn_old_priority;
+	else
+		priop = &node->cn_priority;
+	if (*priop < CACHE_MAX_PRIORITY)
+		(*priop)++;
+}
+
 void
 cache_node_set_priority(
 	struct cache *		cache,
lib/support/iocache.c +9 -0
index b72dcb7ebb470a..478d89174422df 100644
@@ -57,6 +57,7 @@ struct iocache_buf {
 	blk64_t			block;
 	void			*buf;
 	errcode_t		write_error;
+	uint8_t			access;
 	unsigned int		uptodate:1;
 	unsigned int		dirty:1;
 };
@@ -566,6 +567,10 @@ static errcode_t iocache_read_blk64(io_channel channel,
 		}
 		if (ubuf->uptodate)
 			memcpy(buf, ubuf->buf, channel->block_size);
+		if (++ubuf->access > 50) {
+			cache_node_bump_priority(&data->cache, node);
+			ubuf->access = 0;
+		}
 		iocache_buf_unlock(ubuf);
 		cache_node_put(&data->cache, node);
 		if (retval)
@@ -627,6 +632,10 @@ static errcode_t iocache_write_blk64(io_channel channel,
 				     ubuf->uptodate ? CACHE_HIT : CACHE_MISS);
 		ubuf->dirty = 1;
 		ubuf->uptodate = 1;
+		if (++ubuf->access > 50) {
+			cache_node_bump_priority(&data->cache, node);
+			ubuf->access = 0;
+		}
 		iocache_buf_unlock(ubuf);
 		cache_node_put(&data->cache, node);
 	}
#195 lore raw

PATCH 4/6 fuse2fs: enable caching IO manager

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Enable the new dynamic iocache I/O manager in the fuse server, and turn off all the other cache control.

---
 fuse4fs/Makefile.in |    3 ++-
 fuse4fs/fuse4fs.c   |    4 +++-
 misc/Makefile.in    |    4 +++-
 misc/fuse2fs.c      |    6 +++++-
 4 files changed, 13 insertions(+), 4 deletions(-)
fuse4fs/Makefile.in +2 -1
index 9f3547c271638f..0a558da23ced81 100644
@@ -147,7 +147,8 @@ fuse4fs.o: $(srcdir)/fuse4fs.c $(top_builddir)/lib/config.h \
  $(top_srcdir)/lib/ext2fs/bitops.h $(top_srcdir)/lib/ext2fs/ext2fsP.h \
  $(top_srcdir)/lib/ext2fs/ext2fs.h $(top_srcdir)/version.h \
  $(top_srcdir)/lib/e2p/e2p.h $(top_srcdir)/lib/support/cache.h \
- $(top_srcdir)/lib/support/list.h $(top_srcdir)/lib/support/xbitops.h
+ $(top_srcdir)/lib/support/list.h $(top_srcdir)/lib/support/xbitops.h \
+ $(top_srcdir)/lib/support/iocache.h
 journal.o: $(srcdir)/../debugfs/journal.c $(top_builddir)/lib/config.h \
  $(top_builddir)/lib/dirpaths.h $(srcdir)/../debugfs/journal.h \
  $(top_srcdir)/e2fsck/jfs_user.h $(top_srcdir)/e2fsck/e2fsck.h \
fuse4fs/fuse4fs.c +3 -1
index e8edeb8f62e88a..d91817f362674a 100644
@@ -56,6 +56,7 @@
 #include "support/bthread.h"
 #include "support/list.h"
 #include "support/cache.h"
+#include "support/iocache.h"
 
 #include "../version.h"
 #include "uuid/uuid.h"
@@ -1574,6 +1575,7 @@ static errcode_t fuse4fs_open(struct fuse4fs *ff)
 		flags |= EXT2_FLAG_DIRECT_IO;
 
 	dbg_printf(ff, "opening with flags=0x%x\n", flags);
+	iocache_set_backing_manager(unix_io_manager);
 
 	err = fuse4fs_try_losetup(ff, flags);
 	if (err)
@@ -1611,7 +1613,7 @@ static errcode_t fuse4fs_open(struct fuse4fs *ff)
 	deadline = init_deadline(FUSE4FS_OPEN_TIMEOUT);
 	do {
 		err = ext2fs_open2(fuse4fs_device(ff), options, flags, 0, 0,
-				   unix_io_manager, &ff->fs);
+				   iocache_io_manager, &ff->fs);
 		if ((err == EPERM || err == EACCES) &&
 		    (!ff->ro || (flags & EXT2_FLAG_RW))) {
 			/*
misc/Makefile.in +3 -1
index ec964688acd623..8a3adc70fb736e 100644
@@ -880,7 +880,9 @@ fuse2fs.o: $(srcdir)/fuse2fs.c $(top_builddir)/lib/config.h \
  $(top_srcdir)/lib/ext2fs/ext2_ext_attr.h $(top_srcdir)/lib/ext2fs/hashmap.h \
  $(top_srcdir)/lib/ext2fs/bitops.h $(top_srcdir)/lib/ext2fs/ext2fsP.h \
  $(top_srcdir)/lib/ext2fs/ext2fs.h $(top_srcdir)/version.h \
- $(top_srcdir)/lib/e2p/e2p.h
+ $(top_srcdir)/lib/e2p/e2p.h $(top_srcdir)/lib/support/cache.h \
+ $(top_srcdir)/lib/support/list.h $(top_srcdir)/lib/support/xbitops.h \
+ $(top_srcdir)/lib/support/iocache.h
 e2fuzz.o: $(srcdir)/e2fuzz.c $(top_builddir)/lib/config.h \
  $(top_builddir)/lib/dirpaths.h $(top_srcdir)/lib/ext2fs/ext2_fs.h \
  $(top_builddir)/lib/ext2fs/ext2_types.h $(top_srcdir)/lib/ext2fs/ext2fs.h \
misc/fuse2fs.c +5 -1
index 96118942c324ba..3d78a3967f7d9a 100644
@@ -53,6 +53,9 @@
 #include "ext2fs/ext2_fs.h"
 #include "ext2fs/ext2fsP.h"
 #include "support/bthread.h"
+#include "support/list.h"
+#include "support/cache.h"
+#include "support/iocache.h"
 
 #include "../version.h"
 #include "uuid/uuid.h"
@@ -1404,6 +1407,7 @@ static errcode_t fuse2fs_open(struct fuse2fs *ff)
 		flags |= EXT2_FLAG_DIRECT_IO;
 
 	dbg_printf(ff, "opening with flags=0x%x\n", flags);
+	iocache_set_backing_manager(unix_io_manager);
 
 	err = fuse2fs_try_losetup(ff, flags);
 	if (err)
@@ -1441,7 +1445,7 @@ static errcode_t fuse2fs_open(struct fuse2fs *ff)
 	deadline = init_deadline(FUSE2FS_OPEN_TIMEOUT);
 	do {
 		err = ext2fs_open2(fuse2fs_device(ff), options, flags, 0, 0,
-				   unix_io_manager, &ff->fs);
+				   iocache_io_manager, &ff->fs);
 		if ((err == EPERM || err == EACCES) &&
 		    (!ff->ro || (flags & EXT2_FLAG_RW))) {
 			/*
#196 lore raw

PATCH 5/6 fuse2fs: increase inode cache size

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Increase the internal inode cache size. Does this improve performance any?

---
 fuse4fs/fuse4fs.c |    4 ++++
 misc/fuse2fs.c    |    4 ++++
 2 files changed, 8 insertions(+)
fuse4fs/fuse4fs.c +4 -0
index d91817f362674a..1a4ac0cd9a038f 100644
@@ -1686,6 +1686,10 @@ static errcode_t fuse4fs_open(struct fuse4fs *ff)
 	if (err)
 		return translate_error(ff->fs, 0, err);
 
+	err = ext2fs_create_inode_cache(ff->fs, 1024);
+	if (err)
+		return translate_error(ff->fs, 0, err);
+
 	ff->fs->priv_data = ff;
 	ff->blocklog = u_log2(ff->fs->blocksize);
 	ff->blockmask = ff->fs->blocksize - 1;
misc/fuse2fs.c +4 -0
index 3d78a3967f7d9a..4d62b5d44279f9 100644
@@ -1514,6 +1514,10 @@ static errcode_t fuse2fs_open(struct fuse2fs *ff)
 		log_printf(ff, "%s %s.\n", _("mounted filesystem"), uuid);
 	}
 
+	err = ext2fs_create_inode_cache(ff->fs, 1024);
+	if (err)
+		return translate_error(ff->fs, 0, err);
+
 	ff->fs->priv_data = ff;
 	ff->blocklog = u_log2(ff->fs->blocksize);
 	ff->blockmask = ff->fs->blocksize - 1;
#197 lore raw

PATCH 6/6 libext2fs: improve caching for inodes

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Use our new cache code to improve the ondisk inode cache inside libext2fs. Oops, list.h duplication, and libext2fs needs to link against libsupport now.

---
 lib/ext2fs/ext2fsP.h     |   13 ++-
 debugfs/Makefile.in      |    8 +-
 e2fsck/Makefile.in       |   12 +--
 fuse4fs/Makefile.in      |    8 +-
 lib/ext2fs/Makefile.in   |   70 ++++++++-------
 lib/ext2fs/inline_data.c |    4 -
 lib/ext2fs/inode.c       |  215 ++++++++++++++++++++++++++++++++++++----------
 misc/Makefile.in         |    8 +-
 resize/Makefile.in       |   11 +-
 tests/fuzz/Makefile.in   |    4 -
 tests/progs/Makefile.in  |    4 -
 11 files changed, 240 insertions(+), 117 deletions(-)
lib/ext2fs/ext2fsP.h +9 -4
index 428081c9e2ff38..8490dd5139d543 100644
@@ -82,21 +82,26 @@ struct dir_context {
 	errcode_t	errcode;
 };
 
+#include "support/list.h"
+#include "support/cache.h"
+
 /*
  * Inode cache structure
  */
 struct ext2_inode_cache {
 	void *				buffer;
 	blk64_t				buffer_blk;
-	int				cache_last;
-	unsigned int			cache_size;
 	int				refcount;
-	struct ext2_inode_cache_ent	*cache;
+	struct cache			cache;
 };
 
 struct ext2_inode_cache_ent {
+	struct cache_node	node;
 	ext2_ino_t		ino;
-	struct ext2_inode	*inode;
+	uint8_t			access;
+
+	/* bytes representing a host-endian ext2_inode_large object */
+	char			raw[];
 };
 
 /*
debugfs/Makefile.in +4 -4
index 700ae87418c268..8bee4b67fc2de7 100644
@@ -38,15 +38,15 @@ SRCS= debug_cmds.c $(srcdir)/debugfs.c $(srcdir)/util.c $(srcdir)/ls.c \
 	$(srcdir)/../e2fsck/recovery.c $(srcdir)/do_journal.c \
 	$(srcdir)/do_orphan.c
 
-LIBS= $(LIBSUPPORT) $(LIBEXT2FS) $(LIBE2P) $(LIBSS) $(LIBCOM_ERR) $(LIBBLKID) \
+LIBS= $(LIBEXT2FS) $(LIBSUPPORT) $(LIBE2P) $(LIBSS) $(LIBCOM_ERR) $(LIBBLKID) \
 	$(LIBUUID) $(LIBMAGIC) $(SYSLIBS) $(LIBARCHIVE)
-DEPLIBS= $(DEPLIBSUPPORT) $(LIBEXT2FS) $(LIBE2P) $(DEPLIBSS) $(DEPLIBCOM_ERR) \
+DEPLIBS= $(LIBEXT2FS) $(DEPLIBSUPPORT) $(LIBE2P) $(DEPLIBSS) $(DEPLIBCOM_ERR) \
 	$(DEPLIBBLKID) $(DEPLIBUUID)
 
-STATIC_LIBS= $(STATIC_LIBSUPPORT) $(STATIC_LIBEXT2FS) $(STATIC_LIBSS) \
+STATIC_LIBS= $(STATIC_LIBEXT2FS) $(STATIC_LIBSUPPORT) $(STATIC_LIBSS) \
 	$(STATIC_LIBCOM_ERR) $(STATIC_LIBBLKID) $(STATIC_LIBUUID) \
 	$(STATIC_LIBE2P) $(LIBMAGIC) $(SYSLIBS)
-STATIC_DEPLIBS= $(STATIC_LIBEXT2FS) $(DEPSTATIC_LIBSS) \
+STATIC_DEPLIBS= $(STATIC_LIBEXT2FS) $(DEPSTATIC_LIBSUPPORT) $(DEPSTATIC_LIBSS) \
 		$(DEPSTATIC_LIBCOM_ERR) $(DEPSTATIC_LIBUUID) \
 		$(DEPSTATIC_LIBE2P)
 
e2fsck/Makefile.in +6 -6
index 52fad9cbfd2b23..d72244f47e47c0 100644
@@ -16,22 +16,22 @@ PROGS=		e2fsck
 MANPAGES=	e2fsck.8
 FMANPAGES=	e2fsck.conf.5
 
-LIBS= $(LIBSUPPORT) $(LIBEXT2FS) $(LIBCOM_ERR) $(LIBBLKID) $(LIBUUID) \
+LIBS= $(LIBEXT2FS) $(LIBSUPPORT) $(LIBCOM_ERR) $(LIBBLKID) $(LIBUUID) \
 	$(LIBINTL) $(LIBE2P) $(LIBMAGIC) $(SYSLIBS)
-DEPLIBS= $(DEPLIBSUPPORT) $(LIBEXT2FS) $(DEPLIBCOM_ERR) $(DEPLIBBLKID) \
+DEPLIBS= $(LIBEXT2FS) $(DEPLIBSUPPORT) $(DEPLIBCOM_ERR) $(DEPLIBBLKID) \
 	 $(DEPLIBUUID) $(DEPLIBE2P)
 
-STATIC_LIBS= $(STATIC_LIBSUPPORT) $(STATIC_LIBEXT2FS) $(STATIC_LIBCOM_ERR) \
+STATIC_LIBS= $(STATIC_LIBEXT2FS) $(STATIC_LIBSUPPORT) $(STATIC_LIBCOM_ERR) \
 	     $(STATIC_LIBBLKID) $(STATIC_LIBUUID) $(LIBINTL) $(STATIC_LIBE2P) \
 	     $(LIBMAGIC) $(SYSLIBS)
-STATIC_DEPLIBS= $(DEPSTATIC_LIBSUPPORT) $(STATIC_LIBEXT2FS) \
+STATIC_DEPLIBS= $(STATIC_LIBEXT2FS) $(DEPSTATIC_LIBSUPPORT) \
 		$(DEPSTATIC_LIBCOM_ERR) $(DEPSTATIC_LIBBLKID) \
 		$(DEPSTATIC_LIBUUID) $(DEPSTATIC_LIBE2P)
 
-PROFILED_LIBS= $(PROFILED_LIBSUPPORT) $(PROFILED_LIBEXT2FS) \
+PROFILED_LIBS= $(PROFILED_LIBEXT2FS) $(PROFILED_LIBSUPPORT) \
 	       $(PROFILED_LIBCOM_ERR) $(PROFILED_LIBBLKID) $(PROFILED_LIBUUID) \
 	       $(PROFILED_LIBE2P) $(LIBINTL) $(LIBMAGIC) $(SYSLIBS)
-PROFILED_DEPLIBS= $(DEPPROFILED_LIBSUPPORT) $(PROFILED_LIBEXT2FS) \
+PROFILED_DEPLIBS= $(PROFILED_LIBEXT2FS) $(DEPPROFILED_LIBSUPPORT) \
 		  $(DEPPROFILED_LIBCOM_ERR) $(DEPPROFILED_LIBBLKID) \
 		  $(DEPPROFILED_LIBUUID) $(DEPPROFILED_LIBE2P)
 
fuse4fs/Makefile.in +4 -4
index 0a558da23ced81..31afbd8def1de6 100644
@@ -30,11 +30,11 @@ SRCS=\
 
 LIBS= $(LIBEXT2FS) $(LIBCOM_ERR) $(LIBSUPPORT)
 DEPLIBS= $(LIBEXT2FS) $(DEPLIBCOM_ERR) $(DEPLIBSUPPORT)
-PROFILED_LIBS= $(LIBSUPPORT) $(PROFILED_LIBEXT2FS) $(PROFILED_LIBCOM_ERR)
-PROFILED_DEPLIBS= $(DEPLIBSUPPORT) $(PROFILED_LIBEXT2FS) $(DEPPROFILED_LIBCOM_ERR)
+PROFILED_LIBS= $(PROFILED_LIBEXT2FS) $(PROFILED_LIBSUPPORT) $(PROFILED_LIBCOM_ERR)
+PROFILED_DEPLIBS= $(PROFILED_LIBEXT2FS) $(DEPPROFILED_LIBSUPPORT) $(DEPPROFILED_LIBCOM_ERR)
 
-STATIC_LIBS= $(LIBSUPPORT) $(STATIC_LIBEXT2FS) $(STATIC_LIBCOM_ERR)
-STATIC_DEPLIBS= $(DEPLIBSUPPORT) $(STATIC_LIBEXT2FS) $(DEPSTATIC_LIBCOM_ERR)
+STATIC_LIBS= $(STATIC_LIBEXT2FS) $(STATIC_LIBSUPPORT) $(STATIC_LIBCOM_ERR)
+STATIC_DEPLIBS= $(STATIC_LIBEXT2FS) $(DEPSTATIC_LIBSUPPORT) $(DEPSTATIC_LIBCOM_ERR)
 
 LIBS_E2P= $(LIBE2P) $(LIBCOM_ERR)
 DEPLIBS_E2P= $(LIBE2P) $(DEPLIBCOM_ERR)
lib/ext2fs/Makefile.in +36 -34

161 lines not on this page · raw message

lib/ext2fs/inline_data.c +0 -4
index bd52e37708ccad..8ff4a23397f499 100644
@@ -817,10 +817,6 @@ int main(int argc, char *argv[])
 				"tst_inline_data: init inode cache failed\n");
 			exit(1);
 		}
-
-		/* setup inode cache */
-		for (i = 0; i < fs->icache->cache_size; i++)
-			fs->icache->cache[i].ino = first_ino++;
 	}
 
 	/* test */
lib/ext2fs/inode.c +167 -48

302 lines not on this page · raw message

misc/Makefile.in +4 -4
index 8a3adc70fb736e..5b19cdc96bf4f7 100644
@@ -115,11 +115,11 @@ SRCS=	$(srcdir)/tune2fs.c $(srcdir)/mklost+found.c $(srcdir)/mke2fs.c $(srcdir)/
 
 LIBS= $(LIBEXT2FS) $(LIBCOM_ERR) $(LIBSUPPORT)
 DEPLIBS= $(LIBEXT2FS) $(DEPLIBCOM_ERR) $(DEPLIBSUPPORT)
-PROFILED_LIBS= $(LIBSUPPORT) $(PROFILED_LIBEXT2FS) $(PROFILED_LIBCOM_ERR)
-PROFILED_DEPLIBS= $(DEPLIBSUPPORT) $(PROFILED_LIBEXT2FS) $(DEPPROFILED_LIBCOM_ERR)
+PROFILED_LIBS= $(PROFILED_LIBEXT2FS) $(PROFILED_LIBSUPPORT) $(PROFILED_LIBCOM_ERR)
+PROFILED_DEPLIBS= $(PROFILED_LIBEXT2FS) $(DEPPROFILED_LIBSUPPORT) $(DEPPROFILED_LIBCOM_ERR)
 
-STATIC_LIBS= $(LIBSUPPORT) $(STATIC_LIBEXT2FS) $(STATIC_LIBCOM_ERR)
-STATIC_DEPLIBS= $(DEPLIBSUPPORT) $(STATIC_LIBEXT2FS) $(DEPSTATIC_LIBCOM_ERR)
+STATIC_LIBS= $(STATIC_LIBEXT2FS) $(STATIC_LIBSUPPORT) $(STATIC_LIBCOM_ERR)
+STATIC_DEPLIBS= $(STATIC_LIBEXT2FS) $(DEPSTATIC_LIBSUPPORT) $(DEPSTATIC_LIBCOM_ERR)
 
 LIBS_E2P= $(LIBE2P) $(LIBCOM_ERR)
 DEPLIBS_E2P= $(LIBE2P) $(DEPLIBCOM_ERR)
resize/Makefile.in +6 -5
index 27f721305e052e..101cdbeaa9f1ef 100644
@@ -28,12 +28,13 @@ SRCS= $(srcdir)/extent.c \
 	$(srcdir)/resource_track.c \
 	$(srcdir)/sim_progress.c
 
-LIBS= $(LIBE2P) $(LIBEXT2FS) $(LIBCOM_ERR) $(LIBINTL) $(SYSLIBS)
-DEPLIBS= $(LIBE2P) $(LIBEXT2FS) $(DEPLIBCOM_ERR)
+LIBS= $(LIBE2P) $(LIBEXT2FS) $(LIBSUPPORT) $(LIBCOM_ERR) $(LIBINTL) $(SYSLIBS)
+DEPLIBS= $(LIBE2P) $(LIBEXT2FS) $(DEPLIBSUPPORT) $(DEPLIBCOM_ERR)
 
-STATIC_LIBS= $(STATIC_LIBE2P) $(STATIC_LIBEXT2FS) $(STATIC_LIBCOM_ERR) \
-	$(LIBINTL) $(SYSLIBS)
-DEPSTATIC_LIBS= $(STATIC_LIBE2P) $(STATIC_LIBEXT2FS) $(DEPSTATIC_LIBCOM_ERR) 
+STATIC_LIBS= $(STATIC_LIBE2P) $(STATIC_LIBEXT2FS) $(STATIC_LIBSUPPORT) \
+	     $(STATIC_LIBCOM_ERR) $(LIBINTL) $(SYSLIBS)
+DEPSTATIC_LIBS= $(STATIC_LIBE2P) $(STATIC_LIBEXT2FS) $(DEPSTATIC_LIBSUPPORT) \
+		$(DEPSTATIC_LIBCOM_ERR)
 
 .c.o:
 	$(E) "	CC $<"
tests/fuzz/Makefile.in +2 -2
index 949579e7c6501f..2b959f612e2079 100644
@@ -24,9 +24,9 @@ LOCAL_LDFLAGS= @fuzzer_ldflags@
 LIBS= $(LIBEXT2FS) $(LIBCOM_ERR) $(LIBSUPPORT)
 DEPLIBS= $(LIBEXT2FS) $(DEPLIBCOM_ERR) $(DEPLIBSUPPORT)
 
-STATIC_LIBS= $(LIBSUPPORT) $(STATIC_LIBE2P) $(STATIC_LIBEXT2FS) \
+STATIC_LIBS= $(STATIC_LIBE2P) $(STATIC_LIBEXT2FS) $(STATIC_LIBSUPPORT) \
 	$(STATIC_LIBCOM_ERR)
-STATIC_DEPLIBS= $(DEPLIBSUPPORT) $(STATIC_LIBE2P) $(STATIC_LIBEXT2FS) \
+STATIC_DEPLIBS= $(STATIC_LIBE2P) $(STATIC_LIBEXT2FS) $(DEPSTATIC_LIBSUPPORT) \
 	$(DEPSTATIC_LIBCOM_ERR)
 
 FUZZ_LDFLAGS= $(ALL_LDFLAGS)
tests/progs/Makefile.in +2 -2
index 1a8e9299a1c1ca..64069a52c57cd3 100644
@@ -23,8 +23,8 @@ TEST_ICOUNT_OBJS=	test_icount.o test_icount_cmds.o
 SRCS=	$(srcdir)/test_icount.c \
 	$(srcdir)/test_rel.c
 
-LIBS= $(LIBEXT2FS) $(LIBSS) $(LIBCOM_ERR) $(SYSLIBS)
-DEPLIBS= $(LIBEXT2FS) $(DEPLIBSS) $(DEPLIBCOM_ERR)
+LIBS= $(LIBEXT2FS) $(LIBSUPPORT) $(LIBSS) $(LIBCOM_ERR) $(SYSLIBS)
+DEPLIBS= $(LIBEXT2FS) $(DEPLIBSUPPORT) $(DEPLIBSS) $(DEPLIBCOM_ERR)
 
 .c.o:
 	$(E) "	CC $<"
#198 lore raw

PATCH v7 6/8 fuse4fs: run servers as a contained service

In reply to Darrick J. Wong

Hi all,

In this series of the fuse-iomap prototype, we package the newly created fuse4fs server into a systemd socket service. This service can be used by the "mount.service" helper in libfuse to implement untrusted unprivileged mounts.

If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.

Comments and questions are, as always, welcome.

e2fsprogs git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/e2fsprogs.git/log/?h=fuse4fs-service-container
---
Commits in this patchset:
* libext2fs: fix MMP code to work with unixfd IO manager
* fuse4fs: enable safe service mode
* fuse4fs: set proc title when in fuse service mode
* fuse4fs: upsert first file mapping to kernel on open
* fuse4fs: set iomap backing device blocksize
* fuse4fs: ask for loop devices when opening via fuservicemount
* fuse4fs: make MMP work correctly in safe service mode
* debian: update packaging for fuse4fs service

---
 lib/ext2fs/ext2fs.h         |    1 
 MCONFIG.in                  |    1 
 configure                   |  190 +++++++++++++++++++
 configure.ac                |   54 ++++++
 debian/e2fsprogs.install    |    7 +
 debian/fuse4fs.install      |    3 
 debian/rules                |    3 
 fuse4fs/Makefile.in         |   42 ++++
 fuse4fs/fuse4fs.c           |  421 +++++++++++++++++++++++++++++++++++++++++--
 fuse4fs/fuse4fs.socket.in   |   17 ++
 fuse4fs/[email protected] |   99 ++++++++++
 lib/config.h.in             |    6 +
 lib/ext2fs/mmp.c            |   82 ++++++++
 util/subst.conf.in          |    2 
 14 files changed, 902 insertions(+), 26 deletions(-)
 mode change 100644 => 100755 debian/fuse4fs.install
 create mode 100644 fuse4fs/fuse4fs.socket.in
 create mode 100644 fuse4fs/[email protected]
#199 lore raw

PATCH 1/8 libext2fs: fix MMP code to work with unixfd IO manager

From: Darrick J. Wong <[email protected]>

The MMP code wants to be able to read and write the MMP block directly to storage so that the pagecache does not get in the way. This is critical for correct operation of MMP, because it is guarding against two cluster nodes trying to change the filesystem at the same time.

Unfortunately there's no convenient way to tell an IO manager to perform a particular IO in directio mode, so the MMP code open()s the filesystem source device a second time so that it can set O_DIRECT and maintain its own file position independently of the IO channel. This is a gross layering violation.

For unprivileged containerized fuse4fs, we're going to have a privileged mount helper pass us the fd to the block device, so we'll be using the unixfd IO manager. Unfortunately, if the unixfd IO manager is in use, the filesystem "source" will be a string representation of the fd number, and MMP breaks.

Fix this (sort of) by detecting the unixfd IO manager and duplicating the open fd if it's in use. This adds a requirement that the unixfd originally be opened in O_DIRECT mode if the filesystem is on a block device, but that's the best we can do here.

---
 lib/ext2fs/ext2fs.h |    1 +
 lib/ext2fs/mmp.c    |   82 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 82 insertions(+), 1 deletion(-)
lib/ext2fs/ext2fs.h +1 -0
index 38d6074fdbbc87..23b0695a32d150 100644
@@ -229,6 +229,7 @@ typedef struct ext2_file *ext2_file_t;
  * Internal flags for use by the ext2fs library only
  */
 #define EXT2_FLAG2_USE_FAKE_TIME	0x000000001
+#define EXT2_FLAG2_MMP_USE_IOCHANNEL	0x000000002
 
 /*
  * Special flag in the ext2 inode i_flag field that means that this is
lib/ext2fs/mmp.c +81 -1

118 lines not on this page · raw message

#200 lore raw

PATCH 2/8 fuse4fs: enable safe service mode

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Make it possible to run fuse4fs as a safe systemd service, wherein the fuse server only has access to the fds that we pass in.

---
 MCONFIG.in                  |    1 
 configure                   |  135 +++++++++++++++++++++
 configure.ac                |   45 +++++++
 fuse4fs/Makefile.in         |   40 ++++++
 fuse4fs/fuse4fs.c           |  276 ++++++++++++++++++++++++++++++++++++++++++-
 fuse4fs/fuse4fs.socket.in   |   17 +++
 fuse4fs/[email protected] |   99 +++++++++++++++
 lib/config.h.in             |    3 
 util/subst.conf.in          |    2 
 9 files changed, 606 insertions(+), 12 deletions(-)
 create mode 100644 fuse4fs/fuse4fs.socket.in
 create mode 100644 fuse4fs/[email protected]
MCONFIG.in +1 -0
index d66e2f3bc1d552..2fcb71d898fef7 100644
@@ -42,6 +42,7 @@ HAVE_CROND = @have_crond@
 CROND_DIR = @crond_dir@
 HAVE_SYSTEMD = @have_systemd@
 SYSTEMD_SYSTEM_UNIT_DIR = @systemd_system_unit_dir@
+HAVE_FUSE_SERVICE = @have_fuse_service@
 
 @SET_MAKE@
 
configure +135 -0

159 lines not on this page · raw message

configure.ac +45 -0

51 lines not on this page · raw message

fuse4fs/Makefile.in +36 -4
index 31afbd8def1de6..119fb1f37ad1ae 100644
@@ -17,6 +17,13 @@ UMANPAGES=
 @FUSE4FS_CMT@UPROGS+=fuse4fs
 @FUSE4FS_CMT@UMANPAGES+=fuse4fs.1
 
+ifeq ($(HAVE_SYSTEMD),yes)
+SERVICE_FILES	+= fuse4fs.socket [email protected]
+INSTALLDIRS_TGT	+= installdirs-systemd
+INSTALL_TGT	+= install-systemd
+UNINSTALL_TGT	+= uninstall-systemd
+endif
+
 FUSE4FS_OBJS=	fuse4fs.o journal.o recovery.o revoke.o
 
 PROFILED_FUSE4FS_OJBS=	profiled/fuse4fs.o profiled/journal.o \
@@ -54,7 +61,7 @@ DEPEND_CFLAGS = -I$(top_srcdir)/e2fsck
 @PROFILE_CMT@	$(Q) $(CC) $(ALL_CFLAGS) -g -pg -o profiled/$*.o -c $<
 
 all:: profiled $(SPROGS) $(UPROGS) $(USPROGS) $(SMANPAGES) $(UMANPAGES) \
-	$(FMANPAGES) $(LPROGS)
+	$(FMANPAGES) $(LPROGS) $(SERVICE_FILES)
 
 all-static::
 
@@ -71,6 +78,14 @@ fuse4fs: $(FUSE4FS_OBJS) $(DEPLIBS) $(DEPLIBBLKID) $(DEPLIBUUID) \
 		$(LIBFUSE) $(LIBBLKID) $(LIBUUID) $(LIBEXT2FS) $(LIBINTL) \
 		$(CLOCK_GETTIME_LIB) $(SYSLIBS) $(LIBS_E2P)
 
+%.socket: %.socket.in $(DEP_SUBSTITUTE)
+	$(E) "	SUBST $@"
+	$(Q) $(SUBSTITUTE_UPTIME) $< $@
+
+%.service: %.service.in $(DEP_SUBSTITUTE)
+	$(E) "	SUBST $@"
+	$(Q) $(SUBSTITUTE_UPTIME) $< $@
+
 journal.o: $(srcdir)/../debugfs/journal.c
 	$(E) "	CC $<"
 	$(Q) $(CC) -c $(JOURNAL_CFLAGS) -I$(srcdir) \
@@ -93,11 +108,15 @@ fuse4fs.1: $(DEP_SUBSTITUTE) $(srcdir)/fuse4fs.1.in
 	$(E) "	SUBST $@"
 	$(Q) $(SUBSTITUTE_UPTIME) $(srcdir)/fuse4fs.1.in fuse4fs.1
 
-installdirs:
+installdirs: $(INSTALLDIRS_TGT)
 	$(E) "	MKDIR_P $(bindir) $(man1dir)"
 	$(Q) $(MKDIR_P) $(DESTDIR)$(bindir) $(DESTDIR)$(man1dir)
 
-install: all $(UMANPAGES) installdirs
+installdirs-systemd:
+	$(E) "	MKDIR_P $(SYSTEMD_SYSTEM_UNIT_DIR)"
+	$(Q) $(MKDIR_P) $(DESTDIR)$(SYSTEMD_SYSTEM_UNIT_DIR)
+
+install: all $(UMANPAGES) installdirs $(INSTALL_TGT)
 	$(Q) for i in $(UPROGS); do \
 		$(ES) "	INSTALL $(bindir)/$$i"; \
 		$(INSTALL_PROGRAM) $$i $(DESTDIR)$(bindir)/$$i; \
@@ -110,13 +129,19 @@ install: all $(UMANPAGES) installdirs
 		$(INSTALL_DATA) $$i $(DESTDIR)$(man1dir)/$$i; \
 	done
 
+install-systemd: $(SERVICE_FILES) installdirs-systemd
+	$(Q) for i in $(SERVICE_FILES); do \
+		$(ES) "	INSTALL_DATA $(SYSTEMD_SYSTEM_UNIT_DIR)/$$i"; \
+		$(INSTALL_DATA) $$i $(DESTDIR)$(SYSTEMD_SYSTEM_UNIT_DIR)/$$i; \
+	done
+
 install-strip: install
 	$(Q) for i in $(UPROGS); do \
 		$(E) "	STRIP $(bindir)/$$i"; \
 		$(STRIP) $(DESTDIR)$(bindir)/$$i; \
 	done
 
-uninstall:
+uninstall: $(UNINSTALL_TGT)
 	for i in $(UPROGS); do \
 		$(RM) -f $(DESTDIR)$(bindir)/$$i; \
 	done
@@ -124,9 +149,16 @@ uninstall:
 		$(RM) -f $(DESTDIR)$(man1dir)/$$i; \
 	done
 
+uninstall-systemd:
+	for i in $(SERVICE_FILES); do \
+		$(RM) -f $(DESTDIR)$(SYSTEMD_SYSTEM_UNIT_DIR)/$$i; \
+	done
+
 clean::
 	$(RM) -f $(UPROGS) $(UMANPAGES) profile.h \
 		fuse4fs.profiled \
+		$(SERVICE_FILES) \
+		fuse4fs.socket \
 		profiled/*.o \#* *.s *.o *.a *~ core gmon.out
 
 mostlyclean: clean
fuse4fs/fuse4fs.c +268 -8

391 lines not on this page · raw message

fuse4fs/fuse4fs.socket.in new file +17 -0
new file mode 100644
index 00000000000000..0e90a4f0c97f6e
@@ -0,0 +1,17 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (C) 2025-2026 Oracle.  All Rights Reserved.
+# Author: Darrick J. Wong <[email protected]>
+[Unit]
+Description=Socket for ext4 Service
+
+[Socket]
+ListenSequentialPacket=@fuse_service_socket_dir@/ext2
+ListenSequentialPacket=@fuse_service_socket_dir@/ext3
+ListenSequentialPacket=@fuse_service_socket_dir@/ext4
+Accept=yes
+SocketMode=0660
+RemoveOnStop=yes
+
+[Install]
+WantedBy=sockets.target
fuse4fs/[email protected] new file +99 -0

99 lines not on this page · raw message

lib/config.h.in +3 -0
index 7e045b65131522..8c5ba567a748a8 100644
@@ -85,6 +85,9 @@
 /* Define to 1 if fuse supports loopdev operations */
 #undef HAVE_FUSE_LOOPDEV
 
+/* Define to 1 if fuse supports service */
+#undef HAVE_FUSE_SERVICE
+
 /* Define to 1 if you have the Mac OS X function
    CFLocaleCopyPreferredLanguages in the CoreFoundation framework. */
 #undef HAVE_CFLOCALECOPYPREFERREDLANGUAGES
util/subst.conf.in +2 -0
index 5af5e356d46ac7..5fc7cf8f33fa76 100644
@@ -24,3 +24,5 @@ root_bindir		@root_bindir@
 libdir			@libdir@
 $exec_prefix		@exec_prefix@
 pkglibexecdir		@libexecdir@/e2fsprogs
+bindir			@bindir@
+fuse_service_socket_dir	@fuse_service_socket_dir@
#201 lore raw

PATCH 3/8 fuse4fs: set proc title when in fuse service mode

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

When in fuse service mode, set the process title so that we can identify fuse servers by mount arguments. When the service ends, amend the title again to say that we're cleaning up. This is done to make ps aux a bit more communicative as to what is going on.

---
 configure           |   55 +++++++++++++++++++++++++++++++++++++++++++++++++++
 configure.ac        |    9 ++++++++
 fuse4fs/Makefile.in |    2 +-
 fuse4fs/fuse4fs.c   |   28 +++++++++++++++++++++++++-
 lib/config.h.in     |    3 +++
 5 files changed, 95 insertions(+), 2 deletions(-)
configure +55 -0
index 15e9fd92eaf6e7..44bd0e73a74279 100755
@@ -695,6 +695,7 @@ gcc_ranlib
 gcc_ar
 UNI_DIFF_OPTS
 SEM_INIT_LIB
+LIBBSD_LIB
 FUSE4FS_CMT
 FUSE2FS_CMT
 fuse_service_socket_dir
@@ -15061,6 +15062,60 @@ printf "%s\n" "#define HAVE_FUSE_CACHE_READDIR 1" >>confdefs.h
 
 fi
 
+{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for setproctitle in -lbsd" >&5
+printf %s "checking for setproctitle in -lbsd... " >&6; }
+if test ${ac_cv_lib_bsd_setproctitle+y}
+then :
+  printf %s "(cached) " >&6
+else case e in #(
+  e) ac_check_lib_save_LIBS=$LIBS
+LIBS="-lbsd  $LIBS"
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+/* Override any GCC internal prototype to avoid an error.
+   Use char because int might match the return type of a GCC
+   builtin and then its argument prototype would still apply.
+   The 'extern "C"' is for builds by C++ compilers;
+   although this is not generally supported in C code supporting it here
+   has little cost and some practical benefit (sr 110532).  */
+#ifdef __cplusplus
+extern "C"
+#endif
+char setproctitle (void);
+int
+main (void)
+{
+return setproctitle ();
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_link "$LINENO"
+then :
+  ac_cv_lib_bsd_setproctitle=yes
+else case e in #(
+  e) ac_cv_lib_bsd_setproctitle=no ;;
+esac
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.beam \
+    conftest$ac_exeext conftest.$ac_ext
+LIBS=$ac_check_lib_save_LIBS ;;
+esac
+fi
+{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_bsd_setproctitle" >&5
+printf "%s\n" "$ac_cv_lib_bsd_setproctitle" >&6; }
+if test "x$ac_cv_lib_bsd_setproctitle" = xyes
+then :
+  LIBBSD_LIB=-lbsd
+fi
+
+
+if test "$ac_cv_lib_bsd_setproctitle" = yes ; then
+	printf "%s\n" "#define HAVE_SETPROCTITLE 1" >>confdefs.h
+
+fi
+
 { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for PR_SET_IO_FLUSHER" >&5
 printf %s "checking for PR_SET_IO_FLUSHER... " >&6; }
 cat confdefs.h - <<_ACEOF >conftest.$ac_ext
configure.ac +9 -0
index 8aa25ca7585f32..a3162a64123d20 100644
@@ -1625,6 +1625,15 @@ then
 		  [Define to 1 if fuse supports cache_readdir])
 fi
 
+dnl
+dnl see if setproctitle exists
+dnl
+AC_CHECK_LIB(bsd, setproctitle, [LIBBSD_LIB=-lbsd])
+AC_SUBST(LIBBSD_LIB)
+if test "$ac_cv_lib_bsd_setproctitle" = yes ; then
+	AC_DEFINE(HAVE_SETPROCTITLE, 1, Define to 1 if setproctitle])
+fi
+
 dnl
 dnl see if PR_SET_IO_FLUSHER exists
 dnl
fuse4fs/Makefile.in +1 -1
index 119fb1f37ad1ae..f6473ad0027e51 100644
@@ -76,7 +76,7 @@ fuse4fs: $(FUSE4FS_OBJS) $(DEPLIBS) $(DEPLIBBLKID) $(DEPLIBUUID) \
 	$(E) "	LD $@"
 	$(Q) $(CC) $(ALL_LDFLAGS) -o fuse4fs $(FUSE4FS_OBJS) $(LIBS) \
 		$(LIBFUSE) $(LIBBLKID) $(LIBUUID) $(LIBEXT2FS) $(LIBINTL) \
-		$(CLOCK_GETTIME_LIB) $(SYSLIBS) $(LIBS_E2P)
+		$(CLOCK_GETTIME_LIB) $(SYSLIBS) $(LIBS_E2P) @LIBBSD_LIB@
 
 %.socket: %.socket.in $(DEP_SUBSTITUTE)
 	$(E) "	SUBST $@"
fuse4fs/fuse4fs.c +27 -1
index acb6402a174ad3..522afde7c9356b 100644
@@ -49,6 +49,9 @@
 #ifdef HAVE_FUSE_SERVICE
 # include <sys/mount.h>
 # include <fuse_service.h>
+# ifdef HAVE_SETPROCTITLE
+#  include <bsd/unistd.h>
+# endif
 #endif
 #ifdef __SET_FOB_FOR_FUSE
 # undef _FILE_OFFSET_BITS
@@ -320,6 +323,7 @@ struct fuse4fs {
 	struct cache inodes;
 #ifdef HAVE_FUSE_SERVICE
 	struct fuse_service *service;
+	char *svc_cmdline;
 	int bdev_fd;
 	int fusedev_fd;
 #endif
@@ -1443,10 +1447,21 @@ static int fuse4fs_service_connect(struct fuse4fs *ff, struct fuse_args *args)
 
 	if (fuse4fs_is_service(ff))
 		fuse_service_append_args(ff->service, args);
-
 	return 0;
 }
 
+static void fuse4fs_service_set_proc_cmdline(struct fuse4fs *ff, int argc,
+					     char *argv[],
+					     struct fuse_args *args)
+{
+	setproctitle_init(argc, argv, environ);
+	ff->svc_cmdline = fuse_service_cmdline(argc, argv, args);
+	if (!ff->svc_cmdline)
+		return;
+
+	setproctitle("-%s", ff->svc_cmdline);
+}
+
 static inline int
 fuse4fs_service_parse_cmdline(struct fuse_args *args,
 			      struct fuse_cmdline_opts *opts)
@@ -1491,6 +1506,8 @@ static int fuse4fs_service_finish(struct fuse4fs *ff, int ret)
 	 * program scraping the journalctl output needs to see all of our
 	 * output.
 	 */
+	setproctitle("-%s [cleaning up]", ff->svc_cmdline);
+	free(ff->svc_cmdline);
 	sleep(2);
 	if (ret != EXIT_SUCCESS)
 		return EXIT_FAILURE;
@@ -1592,6 +1609,7 @@ static int fuse4fs_service(struct fuse4fs *ff, struct fuse_session *se,
 }
 #else
 # define fuse4fs_service_connect(...)		(0)
+# define fuse4fs_service_set_proc_cmdline(...)	((void)0)
 # define fuse4fs_service_parse_cmdline(...)	(EOPNOTSUPP)
 # define fuse4fs_service_release(...)		((void)0)
 # define fuse4fs_service_close_bdev(...)	((void)0)
@@ -4341,6 +4359,11 @@ static void detect_linux_executable_open(int kernel_flags, int *access_check,
 }
 #endif /* __linux__ */
 
+static int fuse4fs_iomap_begin_read(struct fuse4fs *ff, ext2_ino_t ino,
+				    struct ext2_inode_large *inode, off_t pos,
+				    uint64_t count, uint32_t opflags,
+				    struct fuse_file_iomap *read);
+
 static int fuse4fs_open_file(struct fuse4fs *ff, const struct fuse_ctx *ctxt,
 			     ext2_ino_t ino, bool linked,
 			     struct fuse_file_info *fp)
@@ -8406,6 +8429,9 @@ int main(int argc, char *argv[])
 		exit(1);
 	}
 
+	if (fuse4fs_is_service(&fctx))
+		fuse4fs_service_set_proc_cmdline(&fctx, argc, argv, &args);
+
 	ret = fuse_opt_parse(&args, &fctx, fuse4fs_opts, fuse4fs_opt_proc);
 	if (ret)
 		exit(1);
lib/config.h.in +3 -0
index 8c5ba567a748a8..129f7ffcf060f3 100644
@@ -714,4 +714,7 @@
 /* Define to 1 if CLOCK_MONOTONIC is present */
 #undef HAVE_CLOCK_MONOTONIC
 
+/* Define to 1 if you have the 'setproctitle' function. */
+#undef HAVE_SETPROCTITLE
+
 #include <dirpaths.h>
#202 lore raw

PATCH 4/8 fuse4fs: upsert first file mapping to kernel on open

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Try to speed up the first access to a file by upserting the first file space mapping to the kernel at open time.

---
 fuse4fs/fuse4fs.c |   33 ++++++++++++++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)
fuse4fs/fuse4fs.c +32 -1
index 522afde7c9356b..ad701649886380 100644
@@ -4364,6 +4364,37 @@ static int fuse4fs_iomap_begin_read(struct fuse4fs *ff, ext2_ino_t ino,
 				    uint64_t count, uint32_t opflags,
 				    struct fuse_file_iomap *read);
 
+static void fuse4fs_try_upsert_first_mapping(struct fuse4fs *ff, ext2_ino_t ino,
+					     struct fuse_file_info *fp)
+{
+	struct ext2_inode_large inode;
+	struct fuse_file_iomap read = { };
+	uint64_t fsize;
+	errcode_t err;
+
+	if (!ff->iomap_cache || (fp->flags & O_TRUNC))
+		return;
+
+	err = fuse4fs_read_inode(ff->fs, ino, &inode);
+	if (err)
+		return;
+
+	if (!S_ISREG(inode.i_mode))
+		return;
+
+	fsize = EXT2_I_SIZE(&inode);
+	if (!fsize)
+		return;
+
+	/* try to map the first 64k */
+	err = fuse4fs_iomap_begin_read(ff, ino, &inode, 0, min(fsize, 65536),
+			0, &read);
+	if (err)
+		return;
+
+	fuse_lowlevel_iomap_upsert_mappings(ff->fuse, ino, ino, &read, NULL);
+}
+
 static int fuse4fs_open_file(struct fuse4fs *ff, const struct fuse_ctx *ctxt,
 			     ext2_ino_t ino, bool linked,
 			     struct fuse_file_info *fp)
@@ -4458,7 +4489,7 @@ static int fuse4fs_open_file(struct fuse4fs *ff, const struct fuse_ctx *ctxt,
 	/* fuse 3.5: cache dirents from readdir contents */
 	fp->cache_readdir = 1;
 #endif
-
+	fuse4fs_try_upsert_first_mapping(ff, ino, fp);
 out:
 	if (ret)
 		ext2fs_free_mem(&file);
#203 lore raw

PATCH 5/8 fuse4fs: set iomap backing device blocksize

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

If we're running as an unprivileged iomap fuse server, we must ask the kernel to set the blocksize of the block device.

---
 fuse4fs/fuse4fs.c |   41 +++++++++++++++++++++++++++++++----------
 1 file changed, 31 insertions(+), 10 deletions(-)
fuse4fs/fuse4fs.c +31 -10
index ad701649886380..9d8dfde95e7256 100644
@@ -1607,6 +1607,21 @@ static int fuse4fs_service(struct fuse4fs *ff, struct fuse_session *se,
 
 	return 0;
 }
+
+int fuse4fs_service_set_bdev_blocksize(struct fuse4fs *ff, int dev_index)
+{
+	int ret;
+
+	ret = fuse_lowlevel_iomap_set_blocksize(ff->fusedev_fd, dev_index,
+						ff->fs->blocksize);
+	if (ret) {
+		err_printf(ff, "%s: cannot set blocksize %u: %s\n", __func__,
+			   ff->fs->blocksize, strerror(errno));
+		return -EIO;
+	}
+
+	return 0;
+}
 #else
 # define fuse4fs_service_connect(...)		(0)
 # define fuse4fs_service_set_proc_cmdline(...)	((void)0)
@@ -1618,6 +1633,7 @@ static int fuse4fs_service(struct fuse4fs *ff, struct fuse_session *se,
 # define fuse4fs_service_openfs(...)		(EOPNOTSUPP)
 # define fuse4fs_service_configure_iomap(...)	(EOPNOTSUPP)
 # define fuse4fs_service(...)			(EOPNOTSUPP)
+# define fuse4fs_service_set_bdev_blocksize(...) (EOPNOTSUPP)
 #endif
 
 static errcode_t fuse4fs_acquire_lockfile(struct fuse4fs *ff)
@@ -7376,21 +7392,19 @@ static int fuse4fs_iomap_config_devices(struct fuse4fs *ff)
 {
 	errcode_t err;
 	int fd;
+	int dev_index;
 	int ret;
 
 	err = io_channel_get_fd(ff->fs->io, &fd);
 	if (err)
 		return translate_error(ff->fs, 0, err);
 
-	ret = fuse4fs_set_bdev_blocksize(ff, fd);
-	if (ret)
-		return ret;
-
-	ret = fuse_lowlevel_iomap_device_add(ff->fuse, fd, 0);
-	if (ret < 0) {
-		dbg_printf(ff, "%s: cannot register iomap dev fd=%d, err=%d\n",
-			   __func__, fd, -ret);
-		return translate_error(ff->fs, 0, -ret);
+	dev_index = fuse_lowlevel_iomap_device_add(ff->fuse, fd, 0);
+	if (dev_index < 0) {
+		ret = -dev_index;
+		dbg_printf(ff, "%s: cannot register iomap dev fd=%d: %s\n",
+			   __func__, fd, strerror(ret));
+		return translate_error(ff->fs, 0, ret);
 	}
 
 	dbg_printf(ff, "%s: registered iomap dev fd=%d iomap_dev=%u\n",
@@ -7398,7 +7412,14 @@ static int fuse4fs_iomap_config_devices(struct fuse4fs *ff)
 
 	fuse4fs_configure_atomic_write(ff, fd);
 
-	ff->iomap_dev = ret;
+	if (fuse4fs_is_service(ff))
+		ret = fuse4fs_service_set_bdev_blocksize(ff, dev_index);
+	else
+		ret = fuse4fs_set_bdev_blocksize(ff, fd);
+	if (ret)
+		return ret;
+
+	ff->iomap_dev = dev_index;
 	return 0;
 }
 
#204 lore raw

PATCH 6/8 fuse4fs: ask for loop devices when opening via fuservicemount

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

When requesting a file, ask the fuservicemount program to transform an open regular file into a loop device for us, so that we can use iomap even when the filesystem is actually an image file.

---
 fuse4fs/fuse4fs.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fuse4fs/fuse4fs.c +2 -1
index 9d8dfde95e7256..e3d1e080822e16 100644
@@ -1522,7 +1522,8 @@ static int fuse4fs_service_get_config(struct fuse4fs *ff)
 
 	do {
 		ret = fuse_service_request_file(ff->service, ff->device,
-						open_flags, 0, 0);
+						open_flags, 0,
+						FUSE_SERVICE_REQUEST_FILE_TRYLOOP);
 		if (ret)
 			return ret;
 
#205 lore raw

PATCH 7/8 fuse4fs: make MMP work correctly in safe service mode

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Normally, the libext2fs MMP code open()s a complete separate file descriptor to read and write the MMP block so that it can have its own private open file with its own access mode and file position. However, if the unixfd IO manager is in use, it will reuse the io channel, which means that MMP and the unixfd share the same open file and hence the access mode and file position.

MMP requires directio access to block devices so that changes are immediately visible on other nodes. Therefore, we need the IO channel (and thus the filesystem) to be running in directio mode if MMP is in use.

To make this work correctly with the sole unixfd IO manager user (fuse4fs in unprivileged service mode), we must set O_DIRECT on the bdev fd and mount the filesystem in directio mode.

---
 fuse4fs/fuse4fs.c |   50 +++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 47 insertions(+), 3 deletions(-)
fuse4fs/fuse4fs.c +47 -3
index e3d1e080822e16..b12bd926931a69 100644
@@ -1558,13 +1558,57 @@ static int fuse4fs_service_get_config(struct fuse4fs *ff)
 }
 
 static errcode_t fuse4fs_service_openfs(struct fuse4fs *ff, char *options,
-					int flags)
+					int *flags)
 {
+	struct stat statbuf;
 	char path[32];
+	errcode_t retval;
+	int ret;
+
+	ret = fstat(ff->bdev_fd, &statbuf);
+	if (ret)
+		return errno;
 
 	snprintf(path, sizeof(path), "%d", ff->bdev_fd);
 	iocache_set_backing_manager(unixfd_io_manager);
-	return ext2fs_open2(path, options, flags, 0, 0, iocache_io_manager,
+
+	/*
+	 * Open the filesystem with SKIP_MMP so that we can find out if the
+	 * filesystem actually has MMP.
+	 */
+	retval = ext2fs_open2(path, options, *flags | EXT2_FLAG_SKIP_MMP, 0, 0,
+			      iocache_io_manager, &ff->fs);
+	if (retval)
+		return retval;
+
+	/*
+	 * If the fs doesn't have MMP then we're good to go.  Otherwise close
+	 * the filesystem so that we can reopen it with MMP enabled.
+	 */
+	if (!ext2fs_has_feature_mmp(ff->fs->super))
+		return 0;
+
+	retval = ext2fs_close_free(&ff->fs);
+	if (retval)
+		return retval;
+
+	/*
+	 * If the filesystem is not on a regular file, MMP will share the same
+	 * fd as the unixfd IO channel.  We need to set O_DIRECT on the bdev_fd
+	 * and open the filesystem in directio mode.
+	 */
+	if (!S_ISREG(statbuf.st_mode)) {
+		int fflags = fcntl(ff->bdev_fd, F_GETFL);
+
+		ret = fcntl(ff->bdev_fd, F_SETFL, fflags | O_DIRECT);
+		if (ret)
+			return EXT2_ET_MMP_OPEN_DIRECT;
+
+		ff->directio = 1;
+		*flags |= EXT2_FLAG_DIRECT_IO;
+	}
+
+	return ext2fs_open2(path, options, *flags, 0, 0, iocache_io_manager,
 			&ff->fs);
 }
 
@@ -1845,7 +1889,7 @@ static errcode_t fuse4fs_open(struct fuse4fs *ff)
 	deadline = init_deadline(FUSE4FS_OPEN_TIMEOUT);
 	do {
 		if (fuse4fs_is_service(ff))
-			err = fuse4fs_service_openfs(ff, options, flags);
+			err = fuse4fs_service_openfs(ff, options, &flags);
 		else
 			err = ext2fs_open2(fuse4fs_device(ff), options, flags,
 					   0, 0, iocache_io_manager, &ff->fs);
#206 lore raw

PATCH 8/8 debian: update packaging for fuse4fs service

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Update the Debian packaging code so that we can create fuse4fs service containers.

---
 debian/e2fsprogs.install |    7 ++++++-
 debian/fuse4fs.install   |    3 +++
 debian/rules             |    3 +++
 3 files changed, 12 insertions(+), 1 deletion(-)
 mode change 100644 => 100755 debian/fuse4fs.install
debian/e2fsprogs.install +6 -1
index 17a80e3922dcee..808474bcab1717 100755
@@ -50,4 +50,9 @@ usr/share/man/man8/resize2fs.8
 usr/share/man/man8/tune2fs.8
 etc
 [linux-any] ${deb_udevudevdir}/rules.d
-[linux-any] ${deb_systemdsystemunitdir}
+[linux-any] ${deb_systemdsystemunitdir}/[email protected]
+[linux-any] ${deb_systemdsystemunitdir}/[email protected]
+[linux-any] ${deb_systemdsystemunitdir}/e2scrub_all.service
+[linux-any] ${deb_systemdsystemunitdir}/e2scrub_all.timer
+[linux-any] ${deb_systemdsystemunitdir}/[email protected]
+[linux-any] ${deb_systemdsystemunitdir}/e2scrub_reap.service
debian/fuse4fs.install +3 -0
old mode 100644
new mode 100755
index 17bdc90e33cb67..56048136c2b28b
@@ -1,2 +1,5 @@
+#!/usr/bin/dh-exec
 usr/bin/fuse4fs
 usr/share/man/man1/fuse4fs.1
+[linux-any] ${deb_systemdsystemunitdir}/fuse4fs.socket
+[linux-any] ${deb_systemdsystemunitdir}/[email protected]
debian/rules +3 -0
index b680eb33ceac9e..b5c669c58c3a9b 100755
@@ -173,6 +173,9 @@ override_dh_installinfo:
 ifneq ($(DEB_HOST_ARCH_OS), hurd)
 override_dh_installsystemd:
 	dh_installsystemd -p e2fsprogs --no-restart-after-upgrade --no-stop-on-upgrade e2scrub_all.timer e2scrub_reap.service
+ifeq ($(SKIP_FUSE2FS),)
+	dh_installsystemd -p fuse4fs fuse4fs.socket
+endif
 endif
 
 override_dh_makeshlibs:
#207 lore raw

PATCH v7 7/8 fuse4fs: reclaim buffer cache under memory pressure

In reply to Darrick J. Wong

Hi all,

Having a static buffer cache limit of 32MB is very conservative. When there's plenty of free memory, evicting metadata from the cache isn't actually a good idea, so we'd like to let it grow to handle large working sets. However, we also don't want to OOM the kernel or (in the future) the fuse4fs container cgroup, so we need to listen for memory reclamation events in the kernel.

The solution to this is to open the kernel memory pressure stall indicator files, configure an event when too much time is spent waiting for reclamation, and to trim the buffer cache when the events happen.

If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.

Comments and questions are, as always, welcome.

e2fsprogs git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/e2fsprogs.git/log/?h=fuse4fs-memory-reclaim
---
Commits in this patchset:
* libsupport: add pressure stall monitor
* fuse2fs: only reclaim buffer cache when there is memory pressure
* fuse4fs: enable memory pressure monitoring with service containers
* fuse2fs: flush dirty metadata periodically

---
 lib/support/psi.h       |   66 ++++++
 fuse4fs/Makefile.in     |    2 
 fuse4fs/fuse4fs.c       |  263 +++++++++++++++++++++-
 lib/support/Makefile.in |    4 
 lib/support/iocache.c   |   19 ++
 lib/support/psi.c       |  557 +++++++++++++++++++++++++++++++++++++++++++++++
 misc/Makefile.in        |    2 
 misc/fuse2fs.c          |  196 ++++++++++++++++-
 8 files changed, 1089 insertions(+), 20 deletions(-)
 create mode 100644 lib/support/psi.h
 create mode 100644 lib/support/psi.c
#208 lore raw

PATCH 1/4 libsupport: add pressure stall monitor

From: Darrick J. Wong <[email protected]>

Create some monitoring code that will sit in the background and watch for resource pressure stalls and call some sort of handler when this happens. This will be useful for shrinking the buffer cache when memory gets tight.

---
 lib/support/psi.h       |   57 +++++
 lib/support/Makefile.in |    4 
 lib/support/iocache.c   |   19 ++
 lib/support/psi.c       |  510 +++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 590 insertions(+)
 create mode 100644 lib/support/psi.h
 create mode 100644 lib/support/psi.c
lib/support/psi.h new file +57 -0
new file mode 100644
index 00000000000000..675ebeb553da3e
@@ -0,0 +1,57 @@
+/*
+ * psi.h - Pressure stall monitor
+ *
+ * Copyright (C) 2025-2026 Oracle.
+ *
+ * %Begin-Header%
+ * This file may be redistributed under the terms of the GNU Public
+ * License.
+ * %End-Header%
+ */
+#ifndef __PSI_H__
+#define __PSI_H__
+
+struct psi;
+struct psi_handler;
+
+enum psi_type {
+	PSI_MEMORY,
+};
+
+void psi_destroy(struct psi **psip);
+
+/* call malloc_trim after calling handlers */
+#define PSI_TRIM_HEAP		(1U << 0)
+
+#define PSI_FLAGS		(PSI_TRIM_HEAP)
+
+int psi_create(enum psi_type type, unsigned int psi_flags,
+	       uint64_t stall_us, uint64_t window_us, uint64_t timeout_us,
+	       struct psi **psip);
+
+/* psi triggered due to timeout (and not pressure) */
+#define PSI_REASON_TIMEOUT	(1U << 0)
+
+/*
+ * Prototype of a function to call when a stall occurs.  Implementations must
+ * not block on any resources that are held if psi_stop_thread is called.
+ */
+typedef void (*psi_handler_fn)(const struct psi *psi, unsigned int reasons,
+			       void *data);
+
+int psi_add_handler(struct psi *psi, psi_handler_fn callback, void *data,
+		    struct psi_handler **hanp);
+void psi_del_handler(struct psi *psi, struct psi_handler **hanp);
+void psi_cancel_handler(struct psi *psi, struct psi_handler **hanp);
+
+int psi_start_thread(struct psi *psi);
+void psi_stop_thread(struct psi *psi);
+
+bool psi_thread_cancelled(const struct psi *psi);
+
+static inline bool psi_active(struct psi *psi)
+{
+	return psi != NULL;
+}
+
+#endif /* __PSI_H__ */
lib/support/Makefile.in +4 -0
index 2950e80222ee72..dd4cee928fee5c 100644
@@ -23,6 +23,7 @@ OBJS=		bthread.o \
 		print_fs_flags.o \
 		profile_helpers.o \
 		prof_err.o \
+		psi.o \
 		quotaio.o \
 		quotaio_v2.o \
 		quotaio_tree.o \
@@ -40,6 +41,7 @@ SRCS=		$(srcdir)/argv_parse.c \
 		$(srcdir)/profile.c \
 		$(srcdir)/profile_helpers.c \
 		prof_err.c \
+		$(srcdir)/psi.c \
 		$(srcdir)/quotaio.c \
 		$(srcdir)/quotaio_tree.c \
 		$(srcdir)/quotaio_v2.c \
@@ -195,3 +197,5 @@ cache.o: $(srcdir)/cache.c $(top_builddir)/lib/config.h \
  $(srcdir)/cache.h $(srcdir)/list.h $(srcdir)/xbitops.h
 iocache.o: $(srcdir)/iocache.c $(top_builddir)/lib/config.h \
  $(srcdir)/iocache.h $(srcdir)/cache.h $(srcdir)/list.h $(srcdir)/xbitops.h
+psi.o: $(srcdir)/psi.c $(top_builddir)/lib/config.h \
+ $(srcdir)/psi.h $(srcdir)/list.h $(srcdir)/xbitops.h
lib/support/iocache.c +19 -0
index 478d89174422df..dfb81d3cf58513 100644
@@ -452,6 +452,20 @@ static errcode_t iocache_set_option(io_channel channel, const char *option,
 	if (!strcmp(option, "cache"))
 		return 0;
 
+	if (!strcmp(option, "cache_auto_shrink")) {
+		if (!arg)
+			return EXT2_ET_INVALID_ARGUMENT;
+		if (!strcmp(arg, "on")) {
+			cache_set_flag(&data->cache, CACHE_AUTO_SHRINK);
+			return 0;
+		}
+		if (!strcmp(arg, "off")) {
+			cache_clear_flag(&data->cache, CACHE_AUTO_SHRINK);
+			return 0;
+		}
+		return EXT2_ET_INVALID_ARGUMENT;
+	}
+
 	if (!strcmp(option, "cache_blocks")) {
 		long long size;
 
@@ -467,6 +481,11 @@ static errcode_t iocache_set_option(io_channel channel, const char *option,
 		return 0;
 	}
 
+	if (!strcmp(option, "cache_shrink")) {
+		cache_shrink(&data->cache);
+		return 0;
+	}
+
 	retval = iocache_flush_cache(data);
 	if (retval)
 		return retval;
lib/support/psi.c new file +510 -0

510 lines not on this page · raw message

#209 lore raw

PATCH 2/4 fuse2fs: only reclaim buffer cache when there is memory pressure

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Use the pressure stall indicator library that we added in the previous patch to make it so that we only shrink the cache when there's memory pressure.

---
 fuse4fs/Makefile.in |    2 +
 fuse4fs/fuse4fs.c   |   84 +++++++++++++++++++++++++++++++++++++++++++++++++++
 misc/Makefile.in    |    2 +
 misc/fuse2fs.c      |   84 +++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 170 insertions(+), 2 deletions(-)
fuse4fs/Makefile.in +1 -1
index f6473ad0027e51..0600485b074158 100644
@@ -180,7 +180,7 @@ fuse4fs.o: $(srcdir)/fuse4fs.c $(top_builddir)/lib/config.h \
  $(top_srcdir)/lib/ext2fs/ext2fs.h $(top_srcdir)/version.h \
  $(top_srcdir)/lib/e2p/e2p.h $(top_srcdir)/lib/support/cache.h \
  $(top_srcdir)/lib/support/list.h $(top_srcdir)/lib/support/xbitops.h \
- $(top_srcdir)/lib/support/iocache.h
+ $(top_srcdir)/lib/support/iocache.h $(top_srcdir)/lib/support/psi.h
 journal.o: $(srcdir)/../debugfs/journal.c $(top_builddir)/lib/config.h \
  $(top_builddir)/lib/dirpaths.h $(srcdir)/../debugfs/journal.h \
  $(top_srcdir)/e2fsck/jfs_user.h $(top_srcdir)/e2fsck/e2fsck.h \
fuse4fs/fuse4fs.c +84 -0

126 lines not on this page · raw message

misc/Makefile.in +1 -1
index 5b19cdc96bf4f7..5a37c942188ddc 100644
@@ -882,7 +882,7 @@ fuse2fs.o: $(srcdir)/fuse2fs.c $(top_builddir)/lib/config.h \
  $(top_srcdir)/lib/ext2fs/ext2fs.h $(top_srcdir)/version.h \
  $(top_srcdir)/lib/e2p/e2p.h $(top_srcdir)/lib/support/cache.h \
  $(top_srcdir)/lib/support/list.h $(top_srcdir)/lib/support/xbitops.h \
- $(top_srcdir)/lib/support/iocache.h
+ $(top_srcdir)/lib/support/iocache.h $(top_srcdir)/lib/support/psi.h
 e2fuzz.o: $(srcdir)/e2fuzz.c $(top_builddir)/lib/config.h \
  $(top_builddir)/lib/dirpaths.h $(top_srcdir)/lib/ext2fs/ext2_fs.h \
  $(top_builddir)/lib/ext2fs/ext2_types.h $(top_srcdir)/lib/ext2fs/ext2fs.h \
misc/fuse2fs.c +84 -0

126 lines not on this page · raw message

#210 lore raw

PATCH 3/4 fuse4fs: enable memory pressure monitoring with service containers

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Ask the fuse filesystem service mount helper to open the memory pressure stall files because we cannot open them ourselves.

---
 lib/support/psi.h |    9 +++++++
 fuse4fs/fuse4fs.c |   71 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 lib/support/psi.c |   53 +++++++++++++++++++++++++++++++++++++---
 3 files changed, 128 insertions(+), 5 deletions(-)
lib/support/psi.h +9 -0
index 675ebeb553da3e..916ebf15d17431 100644
@@ -54,4 +54,13 @@ static inline bool psi_active(struct psi *psi)
 	return psi != NULL;
 }
 
+char *psi_system_path(enum psi_type type);
+ssize_t psi_cgroup_path(enum psi_type type, char *path, size_t pathsize);
+
+#define PSI_OPEN_FLAGS (O_RDWR | O_NONBLOCK)
+
+int psi_create_from(enum psi_type type, unsigned int psi_flags,
+		    uint64_t stall_us, uint64_t window_us, uint64_t timeout_us,
+		    int *system_fd, int *cgroup_fd, struct psi **psip);
+
 #endif /* __PSI_H__ */
fuse4fs/fuse4fs.c +69 -2

107 lines not on this page · raw message

lib/support/psi.c +50 -3

77 lines not on this page · raw message

#211 lore raw

PATCH 4/4 fuse2fs: flush dirty metadata periodically

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Flush dirty metadata out to disk periodically like the kernel, to reduce the potential for data loss if userspace doesn't explicitly fsync.

---
 fuse4fs/fuse4fs.c |  112 +++++++++++++++++++++++++++++++++++++++++++++++++----
 misc/fuse2fs.c    |  112 +++++++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 206 insertions(+), 18 deletions(-)
fuse4fs/fuse4fs.c +103 -9
index 4d48521fa8f763..1fbf5e48af8724 100644
@@ -28,6 +28,7 @@
 #include <unistd.h>
 #include <ctype.h>
 #include <assert.h>
+#include <limits.h>
 #ifdef HAVE_FUSE_LOOPDEV
 # include <fuse_loopdev.h>
 #endif
@@ -332,6 +333,10 @@ struct fuse4fs {
 #endif
 	struct psi *mem_psi;
 	struct psi_handler *mem_psi_handler;
+
+	struct bthread *flush_thread;
+	unsigned int flush_interval;
+	double last_flush;
 };
 
 #ifdef HAVE_FUSE_SERVICE
@@ -1007,6 +1012,71 @@ fuse4fs_set_handle(struct fuse_file_info *fp, struct fuse4fs_file_handle *fh)
 	fp->keep_cache = 1;
 }
 
+static errcode_t fuse4fs_flush(struct fuse4fs *ff, int flags)
+{
+	double last_flush = gettime_monotonic();
+	errcode_t err;
+
+	err = ext2fs_flush2(ff->fs, flags);
+	if (err)
+		return err;
+
+	ff->last_flush = last_flush;
+	return 0;
+}
+
+static inline int fuse4fs_flush_wanted(struct fuse4fs *ff)
+{
+	return ff->fs != NULL && ff->opstate == F4OP_WRITABLE &&
+	       ff->last_flush + ff->flush_interval <= gettime_monotonic();
+}
+
+static void fuse4fs_flush_bthread(void *data)
+{
+	struct fuse4fs *ff = data;
+	ext2_filsys fs;
+	errcode_t err;
+	int ret = 0;
+
+	fs = fuse4fs_start(ff);
+	if (fuse4fs_flush_wanted(ff) && !bthread_cancelled(ff->flush_thread)) {
+		err = fuse4fs_flush(ff, 0);
+		if (err)
+			ret = translate_error(fs, 0, err);
+	}
+	fuse4fs_finish(ff, ret);
+}
+
+static void fuse4fs_flush_start(struct fuse4fs *ff)
+{
+	int ret;
+
+	if (!ff->flush_interval)
+		return;
+
+	ret = bthread_create("fuse4fs_flush", fuse4fs_flush_bthread, ff,
+			     ff->flush_interval, &ff->flush_thread);
+	if (ret) {
+		err_printf(ff, "flusher: %s.\n", error_message(ret));
+		return;
+	}
+
+	ret = bthread_start(ff->flush_thread);
+	if (ret)
+		err_printf(ff, "flusher: %s.\n", error_message(ret));
+}
+
+static void fuse4fs_flush_cancel(struct fuse4fs *ff)
+{
+	if (ff->flush_thread)
+		bthread_cancel(ff->flush_thread);
+}
+
+static void fuse4fs_flush_destroy(struct fuse4fs *ff)
+{
+	bthread_destroy(&ff->flush_thread);
+}
+
 #ifdef HAVE_FUSE_IOMAP
 static inline int fuse4fs_iomap_enabled(const struct fuse4fs *ff)
 {
@@ -2240,7 +2310,7 @@ static int fuse4fs_mount(struct fuse4fs *ff)
 		ext2fs_set_tstamp(fs->super, s_mtime, time(NULL));
 		fs->super->s_state &= ~EXT2_VALID_FS;
 		ext2fs_mark_super_dirty(fs);
-		err = ext2fs_flush2(fs, 0);
+		err = fuse4fs_flush(ff, 0);
 		if (err)
 			return translate_error(fs, 0, err);
 	}
@@ -2268,7 +2338,7 @@ static void op_destroy(void *userdata)
 		if (err)
 			translate_error(fs, 0, err);
 
-		err = ext2fs_flush2(fs, 0);
+		err = fuse4fs_flush(ff, 0);
 		if (err)
 			translate_error(fs, 0, err);
 	}
@@ -2291,6 +2361,7 @@ static void op_destroy(void *userdata)
 	 * that the block device will be released before umount(2) returns.
 	 */
 	if (ff->iomap_state == IOMAP_ENABLED) {
+		fuse4fs_flush_cancel(ff);
 		fuse4fs_mmp_cancel(ff);
 		fuse4fs_unmount(ff);
 	}
@@ -2507,6 +2578,7 @@ static void op_init(void *userdata, struct fuse_conn_info *conn)
 	 */
 	fuse4fs_mmp_start(ff);
 	fuse4fs_psi_start(ff);
+	fuse4fs_flush_start(ff);
 
 #if FUSE_VERSION >= FUSE_MAKE_VERSION(3, 17)
 	/*
@@ -3064,7 +3136,7 @@ static inline int fuse4fs_dirsync_flush(struct fuse4fs *ff, ext2_ino_t ino,
 		*flushed = 0;
 	return 0;
 flush:
-	err = ext2fs_flush2(fs, 0);
+	err = fuse4fs_flush(ff, 0);
 	if (err)
 		return translate_error(fs, 0, err);
 
@@ -4936,7 +5008,7 @@ static void op_release(fuse_req_t req, fuse_ino_t fino EXT2FS_ATTR((unused)),
 	if ((fp->flags & O_SYNC) &&
 	    fuse4fs_is_writeable(ff) &&
 	    (fh->open_flags & EXT2_FILE_WRITE)) {
-		err = ext2fs_flush2(fs, EXT2_FLAG_FLUSH_NO_SYNC);
+		err = fuse4fs_flush(ff, EXT2_FLAG_FLUSH_NO_SYNC);
 		if (err)
 			ret = translate_error(fs, fh->ino, err);
 	}
@@ -4967,7 +5039,7 @@ static void op_fsync(fuse_req_t req, fuse_ino_t fino EXT2FS_ATTR((unused)),
 	fs = fuse4fs_start(ff);
 	/* For now, flush everything, even if it's slow */
 	if (fuse4fs_is_writeable(ff) && fh->open_flags & EXT2_FILE_WRITE) {
-		err = ext2fs_flush2(fs, 0);
+		err = fuse4fs_flush(ff, 0);
 		if (err)
 			ret = translate_error(fs, fh->ino, err);
 	}
@@ -6294,6 +6366,7 @@ static int ioctl_shutdown(struct fuse4fs *ff, const struct fuse_ctx *ctxt,
 
 	err_printf(ff, "%s.\n", _("shut down requested"));
 
+	fuse4fs_flush_cancel(ff);
 	fuse4fs_mmp_cancel(ff);
 
 	/*
@@ -6302,7 +6375,7 @@ static int ioctl_shutdown(struct fuse4fs *ff, const struct fuse_ctx *ctxt,
 	 * any of the flags.  Flush whatever is dirty and shut down.
 	 */
 	if (ff->opstate == F4OP_WRITABLE)
-		ext2fs_flush2(fs, 0);
+		fuse4fs_flush(ff, 0);
 	ff->opstate = F4OP_SHUTDOWN;
 	fs->flags &= ~EXT2_FLAG_RW;
 
@@ -6717,7 +6790,7 @@ static void op_freezefs(fuse_req_t req, fuse_ino_t ino, uint64_t unlinked)
 			goto out_unlock;
 		}
 
-		err = ext2fs_flush2(fs, 0);
+		err = fuse4fs_flush(ff, 0);
 		if (err) {
 			ret = translate_error(fs, 0, err);
 			goto out_unlock;
@@ -6753,7 +6826,7 @@ static void op_unfreezefs(fuse_req_t req, fuse_ino_t ino)
 			goto out_unlock;
 		}
 
-		err = ext2fs_flush2(fs, 0);
+		err = fuse4fs_flush(ff, 0);
 		if (err) {
 			ret = translate_error(fs, 0, err);
 			goto out_unlock;
@@ -6797,7 +6870,7 @@ static void op_syncfs(fuse_req_t req, fuse_ino_t ino)
 			goto out_unlock;
 		}
 
-		err = ext2fs_flush2(fs, 0);
+		err = fuse4fs_flush(ff, 0);
 		if (err) {
 			ret = translate_error(fs, 0, err);
 			goto out_unlock;
@@ -8176,6 +8249,7 @@ enum {
 	FUSE4FS_CACHE_SIZE,
 	FUSE4FS_DIRSYNC,
 	FUSE4FS_ERRORS_BEHAVIOR,
+	FUSE4FS_FLUSH_INTERVAL,
 #ifdef HAVE_FUSE_IOMAP
 	FUSE4FS_IOMAP,
 	FUSE4FS_IOMAP_PASSTHROUGH,
@@ -8204,6 +8278,7 @@ static struct fuse_opt fuse4fs_opts[] = {
 #ifdef HAVE_CLOCK_MONOTONIC
 	FUSE4FS_OPT("timing",		timing,			1),
 #endif
+	FUSE_OPT_KEY("flush_interval=%s", FUSE4FS_FLUSH_INTERVAL),
 #ifdef HAVE_FUSE_IOMAP
 	FUSE4FS_OPT("iomap_cache",	iomap_cache,		1),
 	FUSE4FS_OPT("noiomap_cache",	iomap_cache,		0),
@@ -8287,6 +8362,21 @@ static int fuse4fs_opt_proc(void *data, const char *arg,
 
 		/* do not pass through to libfuse */
 		return 0;
+	case FUSE4FS_FLUSH_INTERVAL:
+		char *p;
+		unsigned long val;
+
+		errno = 0;
+		val = strtoul(arg + 15, &p, 0);
+		if (p != arg + strlen(arg) || errno || val > UINT_MAX) {
+			fprintf(stderr, "%s: %s.\n", arg,
+				_("Unrecognized flush interval"));
+			return -1;
+		}
+
+		/* do not pass through to libfuse */
+		ff->flush_interval = val;
+		return 0;
 #ifdef HAVE_FUSE_IOMAP
 	case FUSE4FS_IOMAP:
 		if (strcmp(arg, "iomap") == 0 || strcmp(arg + 6, "1") == 0)
@@ -8334,6 +8424,7 @@ static int fuse4fs_opt_proc(void *data, const char *arg,
 #ifdef HAVE_FUSE_IOMAP
 	"    -o iomap=              0 to disable iomap, 1 to enable iomap\n"
 #endif
+	"    -o flush=<time>        flush dirty metadata on this interval\n"
 	"\n",
 			outargs->argv[0]);
 		if (key == FUSE4FS_HELPFULL) {
@@ -8643,6 +8734,7 @@ int main(int argc, char *argv[])
 		.loop_fd = -1,
 #endif
 		.translate_inums = 1,
+		.flush_interval = 30,
 #ifdef HAVE_FUSE_SERVICE
 		.bdev_fd = -1,
 		.fusedev_fd = -1,
@@ -8829,6 +8921,7 @@ int main(int argc, char *argv[])
  _("Mount failed while opening filesystem.  Check dmesg(1) for details."));
 		fflush(orig_stderr);
 	}
+	fuse4fs_flush_destroy(&fctx);
 	fuse4fs_psi_destroy(&fctx);
 	fuse4fs_mmp_destroy(&fctx);
 	fuse4fs_unmount(&fctx);
@@ -9007,6 +9100,7 @@ static int __translate_error(ext2_filsys fs, ext2_ino_t ino, errcode_t err,
  _("Remounting read-only due to errors."));
 			ff->opstate = F4OP_READONLY;
 		}
+		fuse4fs_flush_cancel(ff);
 		fuse4fs_mmp_cancel(ff);
 		fs->flags &= ~EXT2_FLAG_RW;
 		break;
misc/fuse2fs.c +103 -9

244 lines not on this page · raw message

#212 lore raw

RFC PATCH 8/8 fuse: allow fuse servers to upload iomap BPF programs

In reply to Darrick J. Wong

Hi all,

There are certain fuse servers that might benefit from the ability to upload a BPF program into the kernel to respond to ->iomap_begin requests instead of upcalling the fuse server itself.

For example, consider a fuse server that abstracts a large amount of storage for use as intermediate storage by programs. If the storage is striped across hardware devices (e.g. RAID0 or interleaved memory controllers) then the iomapping pattern will be completely regular but the mappings themselves might be very small.

Performance for large IOs will suck if it is necessary to upcall the fuse server every time we cross a mapping boundary. The fuse server can try to mitigate that hit by upserting mappings ahead of time, but there's a better solution for this usecase: BPF programs.

In this case, the fuse server can compile a BPF program that will compute the mapping data for a given request and upload the program. This avoids the overhead of cache lookups and server upcalls. Note that the BPF verifier still imposes instruction count and complexity limits on the uploaded programs.

Note that I embraced and extended some code from Joanne, but at this point I've modified it so heavily that it's not really the original anymore. But she still gets credit for coming up with the idea and engaging me in flinging prototypes around.

More recently, this patchset has changed strategies -- instead of compiling a static BPF program with very limited functionality, we instead compile it on the fly, which allows for dynamic behavior.

If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.

Comments and questions are, as always, welcome.

kernel git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-bpf

e2fsprogs git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/e2fsprogs.git/log/?h=fuse-iomap-bpf
---
Commits in this patchset:
* fuse4fs: add dynamic iomap bpf prototype which will break FIEMAP
* fuse4fs: wire up caching examples to fuse iomap bpf program
* fuse4fs: adjust test bpf program to deal with opaque inodes

---
 fuse4fs/fuse4fs_bpf.h |   54 +++++++
 MCONFIG.in            |    5 +
 configure             |  188 +++++++++++++++++++++++
 configure.ac          |   94 +++++++++++
 fuse4fs/Makefile.in   |   22 ++-
 fuse4fs/fuse4fs.c     |   92 +++++++++++
 fuse4fs/fuse4fs_bpf.c |  404 +++++++++++++++++++++++++++++++++++++++++++++++++
 lib/config.h.in       |   12 +
 8 files changed, 869 insertions(+), 2 deletions(-)
 create mode 100644 fuse4fs/fuse4fs_bpf.h
 create mode 100644 fuse4fs/fuse4fs_bpf.c
#213 lore raw

PATCH 1/3 fuse4fs: add dynamic iomap bpf prototype which will break FIEMAP

From: Darrick J. Wong <[email protected]>

Enhance fuse4fs to report rogue results from FIEMAP to prove that iomap bpf actually works. This patch employs dynamic compilation of the bpf program so that the filesystem can tailor the bpf code to the needs of the filesystem it's serving.

---
 fuse4fs/fuse4fs_bpf.h |   54 +++++++
 MCONFIG.in            |    5 +
 configure             |  188 +++++++++++++++++++++++
 configure.ac          |   94 +++++++++++
 fuse4fs/Makefile.in   |   22 ++-
 fuse4fs/fuse4fs.c     |   82 ++++++++++
 fuse4fs/fuse4fs_bpf.c |  404 +++++++++++++++++++++++++++++++++++++++++++++++++
 lib/config.h.in       |   12 +
 8 files changed, 859 insertions(+), 2 deletions(-)
 create mode 100644 fuse4fs/fuse4fs_bpf.h
 create mode 100644 fuse4fs/fuse4fs_bpf.c
fuse4fs/fuse4fs_bpf.h new file +54 -0
new file mode 100644
index 00000000000000..4fcede160a3731
@@ -0,0 +1,54 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 Oracle.  All Rights Reserved.
+ * Author: Darrick J. Wong <[email protected]>
+ */
+#ifndef __FUSE4FS_BPF_H__
+#define __FUSE4FS_BPF_H__
+
+struct fuse4fs_bpf_attrs {
+	/* vector to the binary elf data */
+	const void *elf_data;
+	size_t elf_size;
+
+	/* name associated with this bpf skeleton */
+	const char *skel_name;
+
+	/* name of the fuse_iomap_bpf_ops object in the bpf code */
+	const char *ops_name;
+
+	/* name of the iomap_begin bpf function, if provided */
+	const char *begin_fn_name;
+
+	/* name of the iomap_end bpf function, if provided */
+	const char *end_fn_name;
+
+	/* name of the iomap_ioend bpf function, if provided */
+	const char *ioend_fn_name;
+};
+
+struct fuse4fs_bpf_compile {
+	/* C source code */
+	const char *source_code;
+
+	/* directory containing vmlinux.h */
+	const char *vmlinux_h_dir;
+
+	/* directory containing fuse_iomap_bpf.h */
+	const char *fuse_include_dir;
+};
+
+struct fuse4fs_bpf_ctl {
+	struct fuse4fs_bpf *skel;
+	struct bpf_link *link;
+};
+
+int fuse4fs_bpf_ctl_setup(struct fuse4fs_bpf_ctl *arg, struct fuse_session *se,
+			  const struct fuse4fs_bpf_attrs *attrs);
+void fuse4fs_bpf_ctl_drop_skeleton(struct fuse4fs_bpf_ctl *arg);
+void fuse4fs_bpf_ctl_cleanup(struct fuse4fs_bpf_ctl *arg);
+
+int fuse4fs_bpf_compile(struct fuse4fs_bpf_attrs *attrs,
+			const struct fuse4fs_bpf_compile *cc);
+
+#endif /* __FUSE4FS_BPF_H__ */
MCONFIG.in +5 -0
index 2fcb71d898fef7..9b25cbb78d3f00 100644
@@ -44,6 +44,11 @@ HAVE_SYSTEMD = @have_systemd@
 SYSTEMD_SYSTEM_UNIT_DIR = @systemd_system_unit_dir@
 HAVE_FUSE_SERVICE = @have_fuse_service@
 
+VMLINUX_H_DIR = @VMLINUX_H_DIR@
+FUSE_INCLUDE_PATH = @FUSE_INCLUDE_PATH@
+HAVE_IOMAP_BPF = @have_iomap_bpf@
+LIBBPF = @BPF_LIB@
+
 @SET_MAKE@
 
 @ifGNUmake@ V =
configure +188 -0

236 lines not on this page · raw message

configure.ac +94 -0

106 lines not on this page · raw message

fuse4fs/Makefile.in +20 -2
index 0600485b074158..cb2d6c54a73f07 100644
@@ -35,12 +35,18 @@ SRCS=\
 	$(srcdir)/../e2fsck/revoke.c \
 	$(srcdir)/../e2fsck/recovery.c
 
-LIBS= $(LIBEXT2FS) $(LIBCOM_ERR) $(LIBSUPPORT)
+ifeq ($(HAVE_IOMAP_BPF),yes)
+SRCS += $(srcdir)/fuse4fs_bpf.c
+FUSE4FS_OBJS += fuse4fs_bpf.o
+BPF_SKEL=fuse4fs_bpf.h
+endif
+
+LIBS= $(LIBEXT2FS) $(LIBCOM_ERR) $(LIBSUPPORT) $(LIBBPF)
 DEPLIBS= $(LIBEXT2FS) $(DEPLIBCOM_ERR) $(DEPLIBSUPPORT)
 PROFILED_LIBS= $(PROFILED_LIBEXT2FS) $(PROFILED_LIBSUPPORT) $(PROFILED_LIBCOM_ERR)
 PROFILED_DEPLIBS= $(PROFILED_LIBEXT2FS) $(DEPPROFILED_LIBSUPPORT) $(DEPPROFILED_LIBCOM_ERR)
 
-STATIC_LIBS= $(STATIC_LIBEXT2FS) $(STATIC_LIBSUPPORT) $(STATIC_LIBCOM_ERR)
+STATIC_LIBS= $(STATIC_LIBEXT2FS) $(STATIC_LIBSUPPORT) $(STATIC_LIBCOM_ERR) $(LIBBPF)
 STATIC_DEPLIBS= $(STATIC_LIBEXT2FS) $(DEPSTATIC_LIBSUPPORT) $(DEPSTATIC_LIBCOM_ERR)
 
 LIBS_E2P= $(LIBE2P) $(LIBCOM_ERR)
@@ -171,6 +177,18 @@ distclean: clean
 # the Makefile.in file
 #
 fuse4fs.o: $(srcdir)/fuse4fs.c $(top_builddir)/lib/config.h \
+ $(top_builddir)/lib/dirpaths.h $(top_srcdir)/lib/ext2fs/ext2fs.h \
+ $(top_builddir)/lib/ext2fs/ext2_types.h $(top_srcdir)/lib/ext2fs/ext2_fs.h \
+ $(top_srcdir)/lib/ext2fs/ext3_extents.h $(top_srcdir)/lib/et/com_err.h \
+ $(top_srcdir)/lib/ext2fs/ext2_io.h $(top_builddir)/lib/ext2fs/ext2_err.h \
+ $(top_srcdir)/lib/ext2fs/ext2_ext_attr.h $(top_srcdir)/lib/ext2fs/hashmap.h \
+ $(top_srcdir)/lib/ext2fs/bitops.h $(top_srcdir)/lib/ext2fs/ext2fsP.h \
+ $(top_srcdir)/lib/ext2fs/ext2fs.h $(top_srcdir)/version.h \
+ $(top_srcdir)/lib/e2p/e2p.h $(top_srcdir)/lib/support/cache.h \
+ $(top_srcdir)/lib/support/list.h $(top_srcdir)/lib/support/xbitops.h \
+ $(top_srcdir)/lib/support/iocache.h $(top_srcdir)/lib/support/psi.h \
+ $(BPF_SKEL)
+fuse4fs_bpf.o: $(srcdir)/fuse4fs_bpf.c $(top_builddir)/lib/config.h \
  $(top_builddir)/lib/dirpaths.h $(top_srcdir)/lib/ext2fs/ext2fs.h \
  $(top_builddir)/lib/ext2fs/ext2_types.h $(top_srcdir)/lib/ext2fs/ext2_fs.h \
  $(top_srcdir)/lib/ext2fs/ext3_extents.h $(top_srcdir)/lib/et/com_err.h \
fuse4fs/fuse4fs.c +82 -0

114 lines not on this page · raw message

fuse4fs/fuse4fs_bpf.c new file +404 -0

404 lines not on this page · raw message

lib/config.h.in +12 -0
index 129f7ffcf060f3..ecc81eea839f16 100644
@@ -99,6 +99,9 @@
 /* Define to 1 if you have the 'chflags' function. */
 #undef HAVE_CHFLAGS
 
+/* Define to 1 if you have the 'close_range' function. */
+#undef HAVE_CLOSE_RANGE
+
 /* Define if the GNU dcgettext() function is already present or preinstalled.
    */
 #undef HAVE_DCGETTEXT
@@ -717,4 +720,13 @@
 /* Define to 1 if you have the 'setproctitle' function. */
 #undef HAVE_SETPROCTITLE
 
+/* Define to 1 if fuse iomap bpf present */
+#undef HAVE_IOMAP_BPF
+
+/* Location of vmlinux.h */
+#undef VMLINUX_H_DIR
+
+/* Location of fuse headers */
+#undef FUSE_INCLUDE_PATH
+
 #include <dirpaths.h>