* [PATCH 01/10] fuse: Share cuse_parse_one() as fuse_kv_parse_one()
2026-11-30 8:26 [PATCH 0/10] MUSE: Userspace backed MTD v4 Richard Weinberger (sigma star gmbh)
@ 2026-11-30 8:26 ` Richard Weinberger (sigma star gmbh)
2026-11-30 8:26 ` [PATCH 02/10] fuse: Add a public header for users outside of fs/fuse/ Richard Weinberger (sigma star gmbh)
` (8 subsequent siblings)
9 siblings, 0 replies; 13+ messages in thread
From: Richard Weinberger (sigma star gmbh) @ 2026-11-30 8:26 UTC (permalink / raw)
To: linux-mtd
Cc: fuse-devel, netdev, horms, pabeni, kuba, edumazet, davem, miklos,
vigneshr, miquel.raynal, david.oberhollenzer, upstream+linux,
Richard Weinberger
From: Richard Weinberger <richard@nod.at>
CUSE_INIT replies with the device parameters as a list of key=value
strings, cuse_parse_one() walks it. MUSE will describe its MTD in the
same way.
Move the function into the new fs/fuse/helper.c and rename it to
fuse_kv_parse_one(). The file is built into fuse.ko if FUSE_HELPER is
selected, which CUSE does now.
As the function is no longer CUSE only, its two error messages carry
the prefix "fuse: " instead of "CUSE: ".
Signed-off-by: Richard Weinberger <richard@nod.at>
---
fs/fuse/Kconfig | 4 +++
fs/fuse/Makefile | 1 +
fs/fuse/cuse.c | 58 +-------------------------------------
fs/fuse/fuse_i.h | 4 +++
fs/fuse/helper.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 83 insertions(+), 57 deletions(-)
create mode 100644 fs/fuse/helper.c
diff --git a/fs/fuse/Kconfig b/fs/fuse/Kconfig
index 3d1e52896b87..93ead2528ca8 100644
--- a/fs/fuse/Kconfig
+++ b/fs/fuse/Kconfig
@@ -19,9 +19,13 @@ config FUSE_FS
If you want to develop a userspace FS, or if you want to use
a filesystem based on FUSE, answer Y or M.
+config FUSE_HELPER
+ bool
+
config CUSE
tristate "Character device in Userspace support"
depends on FUSE_FS
+ select FUSE_HELPER
help
This FUSE extension allows character devices to be
implemented in userspace.
diff --git a/fs/fuse/Makefile b/fs/fuse/Makefile
index da9e802d7ae1..adcf912c87a1 100644
--- a/fs/fuse/Makefile
+++ b/fs/fuse/Makefile
@@ -17,6 +17,7 @@ fuse-y += iomode.o
fuse-$(CONFIG_FUSE_VDAX) += dax.o
fuse-$(CONFIG_FUSE_PASSTHROUGH) += passthrough.o backing.o ext_map.o
fuse-$(CONFIG_SYSCTL) += sysctl.o
+fuse-$(CONFIG_FUSE_HELPER) += helper.o
fuse-$(CONFIG_FUSE_IO_URING) += dev_uring.o
virtiofs-y := virtio_fs.o
diff --git a/fs/fuse/cuse.c b/fs/fuse/cuse.c
index 2538db153991..73512b26da10 100644
--- a/fs/fuse/cuse.c
+++ b/fs/fuse/cuse.c
@@ -201,62 +201,6 @@ struct cuse_devinfo {
const char *name;
};
-/**
- * cuse_parse_one - parse one key=value pair
- * @pp: i/o parameter for the current position
- * @end: points to one past the end of the packed string
- * @keyp: out parameter for key
- * @valp: out parameter for value
- *
- * *@pp points to packed strings - "key0=val0\0key1=val1\0" which ends
- * at @end - 1. This function parses one pair and set *@keyp to the
- * start of the key and *@valp to the start of the value. Note that
- * the original string is modified such that the key string is
- * terminated with '\0'. *@pp is updated to point to the next string.
- *
- * RETURNS:
- * 1 on successful parse, 0 on EOF, -errno on failure.
- */
-static int cuse_parse_one(char **pp, char *end, char **keyp, char **valp)
-{
- char *p = *pp;
- char *key, *val;
-
- while (p < end && *p == '\0')
- p++;
- if (p == end)
- return 0;
-
- if (end[-1] != '\0') {
- pr_err("info not properly terminated\n");
- return -EINVAL;
- }
-
- key = val = p;
- p += strlen(p);
-
- if (valp) {
- strsep(&val, "=");
- if (!val)
- val = key + strlen(key);
- key = strstrip(key);
- val = strstrip(val);
- } else
- key = strstrip(key);
-
- if (!strlen(key)) {
- pr_err("zero length info key specified\n");
- return -EINVAL;
- }
-
- *pp = p;
- *keyp = key;
- if (valp)
- *valp = val;
-
- return 1;
-}
-
/**
* cuse_parse_devinfo - parse device info
* @p: device info string
@@ -277,7 +221,7 @@ static int cuse_parse_devinfo(char *p, size_t len, struct cuse_devinfo *devinfo)
int rc;
while (true) {
- rc = cuse_parse_one(&p, end, &key, &val);
+ rc = fuse_kv_parse_one(&p, end, &key, &val);
if (rc < 0)
return rc;
if (!rc)
diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h
index e0992fcbcc55..b6153db7e76a 100644
--- a/fs/fuse/fuse_i.h
+++ b/fs/fuse/fuse_i.h
@@ -1410,4 +1410,8 @@ int fuse_ext_map_mmap(struct file *file, struct vm_area_struct *vma);
bool fuse_ext_map_is_dax(struct fuse_backing *fb);
int fuse_ext_map_populate(struct fuse_conn *fc, struct fuse_notify_backing_map_out *arg,
struct fuse_extent *ext);
+
+/* helper.c */
+int fuse_kv_parse_one(char **pp, char *end, char **keyp, char **valp);
+
#endif /* _FS_FUSE_I_H */
diff --git a/fs/fuse/helper.c b/fs/fuse/helper.c
new file mode 100644
index 000000000000..29db362d930c
--- /dev/null
+++ b/fs/fuse/helper.c
@@ -0,0 +1,73 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Helper functions used by CUSE and MUSE
+ *
+ * Copyright (C) 2008-2009 SUSE Linux Products GmbH
+ * Copyright (C) 2008-2009 Tejun Heo <tj@kernel.org>
+ *
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/export.h>
+#include <linux/printk.h>
+#include <linux/string.h>
+
+#include "fuse_i.h"
+
+/**
+ * fuse_kv_parse_one - parse one key=value pair
+ * @pp: i/o parameter for the current position
+ * @end: points to one past the end of the packed string
+ * @keyp: out parameter for key
+ * @valp: out parameter for value
+ *
+ * *@pp points to packed strings - "key0=val0\0key1=val1\0" which ends
+ * at @end - 1. This function parses one pair and set *@keyp to the
+ * start of the key and *@valp to the start of the value. Note that
+ * the original string is modified such that the key string is
+ * terminated with '\0'. *@pp is updated to point to the next string.
+ *
+ * RETURNS:
+ * 1 on successful parse, 0 on EOF, -errno on failure.
+ */
+int fuse_kv_parse_one(char **pp, char *end, char **keyp, char **valp)
+{
+ char *p = *pp;
+ char *key, *val;
+
+ while (p < end && *p == '\0')
+ p++;
+ if (p == end)
+ return 0;
+
+ if (end[-1] != '\0') {
+ pr_err("info not properly terminated\n");
+ return -EINVAL;
+ }
+
+ key = val = p;
+ p += strlen(p);
+
+ if (valp) {
+ strsep(&val, "=");
+ if (!val)
+ val = key + strlen(key);
+ key = strstrip(key);
+ val = strstrip(val);
+ } else
+ key = strstrip(key);
+
+ if (!strlen(key)) {
+ pr_err("zero length info key specified\n");
+ return -EINVAL;
+ }
+
+ *pp = p;
+ *keyp = key;
+ if (valp)
+ *valp = val;
+
+ return 1;
+}
+EXPORT_SYMBOL_GPL(fuse_kv_parse_one);
--
2.55.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH 02/10] fuse: Add a public header for users outside of fs/fuse/
2026-11-30 8:26 [PATCH 0/10] MUSE: Userspace backed MTD v4 Richard Weinberger (sigma star gmbh)
2026-11-30 8:26 ` [PATCH 01/10] fuse: Share cuse_parse_one() as fuse_kv_parse_one() Richard Weinberger (sigma star gmbh)
@ 2026-11-30 8:26 ` Richard Weinberger (sigma star gmbh)
2026-11-30 9:14 ` sashiko-bot
2026-11-30 8:26 ` [PATCH 03/10] fuse: Add an interface for userspace backed devices Richard Weinberger (sigma star gmbh)
` (7 subsequent siblings)
9 siblings, 1 reply; 13+ messages in thread
From: Richard Weinberger (sigma star gmbh) @ 2026-11-30 8:26 UTC (permalink / raw)
To: linux-mtd
Cc: fuse-devel, netdev, horms, pabeni, kuba, edumazet, davem, miklos,
vigneshr, miquel.raynal, david.oberhollenzer, upstream+linux,
Richard Weinberger
From: Richard Weinberger <richard@nod.at>
A driver which implements a userspace backed device on top of the FUSE
transport does not have to live in fs/fuse/. So far such a driver would
need fuse_i.h, and with it every internal data structure of FUSE.
Add <net/fuse.h> and move the bare minimum to it: the request arguments
(struct fuse_args), the device file operations, fuse_dev_release(),
fuse_dev_put(), reference counting of struct fuse_conn, and
fuse_kv_parse_one(). struct fuse_conn and struct fuse_dev are opaque to
users of this header.
No functional change, the private headers include the new one.
Signed-off-by: Richard Weinberger <richard@nod.at>
---
MAINTAINERS | 1 +
fs/fuse/args.h | 44 +----------------------
fs/fuse/dev.h | 12 +------
fs/fuse/fuse_dev_i.h | 3 +-
fs/fuse/fuse_i.h | 9 -----
include/net/fuse.h | 85 ++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 89 insertions(+), 65 deletions(-)
create mode 100644 include/net/fuse.h
diff --git a/MAINTAINERS b/MAINTAINERS
index 84cb98ef4c49..6626e026deea 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -11039,6 +11039,7 @@ W: https://github.com/libfuse/
T: git git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/fuse.git
F: Documentation/filesystems/fuse/*
F: fs/fuse/
+F: include/net/fuse.h
F: include/uapi/linux/fuse.h
F: tools/testing/selftests/filesystems/fuse/
diff --git a/fs/fuse/args.h b/fs/fuse/args.h
index 36ef0c6d5999..d9c4fb1a19b3 100644
--- a/fs/fuse/args.h
+++ b/fs/fuse/args.h
@@ -4,52 +4,10 @@
#define _FS_FUSE_ARGS_H
#include <linux/types.h>
+#include <net/fuse.h>
struct fuse_mount;
-/** One input argument of a request */
-struct fuse_in_arg {
- unsigned size;
- const void *value;
-};
-
-/** One output argument of a request */
-struct fuse_arg {
- unsigned size;
- void *value;
-};
-
-struct fuse_args {
- u64 nodeid;
- u32 opcode;
- u32 uid;
- u32 gid;
- u32 pid;
- u8 in_numargs;
- u8 out_numargs;
- u8 ext_idx;
- bool force:1;
- bool noreply:1;
- bool nocreds:1;
- bool in_pages:1;
- bool out_pages:1;
- bool user_pages:1;
- bool out_argvar:1;
- bool page_zeroing:1;
- bool may_block:1;
- bool is_ext:1;
- bool is_pinned:1;
- bool invalidate_vmap:1;
- bool abort_on_kill:1;
- /* server requested io-uring zero-copy for this op */
- bool zero_copy:1;
- struct fuse_in_arg in_args[4];
- struct fuse_arg out_args[2];
- void (*end)(struct fuse_args *args, int error);
- /* Used for kvec iter backed by vmalloc address */
- void *vmap_base;
-};
-
/** FUSE folio descriptor */
struct fuse_folio_desc {
unsigned int length;
diff --git a/fs/fuse/dev.h b/fs/fuse/dev.h
index dbffd5bed2f5..3e4838b32c79 100644
--- a/fs/fuse/dev.h
+++ b/fs/fuse/dev.h
@@ -4,6 +4,7 @@
#define _FS_FUSE_DEV_H
#include <linux/cleanup.h>
+#include <net/fuse.h>
/** Maximum number of outstanding background requests */
#define FUSE_DEFAULT_MAX_BACKGROUND 12
@@ -59,7 +60,6 @@ void fuse_dev_cleanup(void);
void fuse_dev_install(struct fuse_dev *fud, struct fuse_chan *fch);
bool fuse_dev_verify(struct fuse_dev *fud, struct fuse_chan *fch);
-void fuse_dev_put(struct fuse_dev *fud);
bool fuse_dev_is_installed(struct fuse_dev *fud);
bool fuse_dev_is_sync_init(struct fuse_dev *fud);
struct fuse_dev *fuse_dev_grab(struct file *file);
@@ -70,16 +70,6 @@ void fuse_init_server_timeout(struct fuse_chan *fch, unsigned int timeout);
void fuse_chan_abort(struct fuse_chan *fch, bool abort_with_err);
void fuse_chan_wait_aborted(struct fuse_chan *fch);
-/**
- * Acquire reference to fuse_conn
- */
-struct fuse_conn *fuse_conn_get(struct fuse_conn *fc);
-
-/**
- * Release reference to fuse_conn
- */
-void fuse_conn_put(struct fuse_conn *fc);
-
dev_t fuse_conn_get_id(struct fuse_conn *fc);
void fuse_end_polls(struct fuse_conn *fc);
diff --git a/fs/fuse/fuse_dev_i.h b/fs/fuse/fuse_dev_i.h
index a8eba46a3e51..82621b8d41e6 100644
--- a/fs/fuse/fuse_dev_i.h
+++ b/fs/fuse/fuse_dev_i.h
@@ -12,6 +12,7 @@
#include <linux/wait.h>
#include <linux/workqueue.h>
#include <linux/fs.h>
+#include <net/fuse.h>
/* Ordinary requests have even IDs, while interrupts IDs are odd */
#define FUSE_INT_REQ_BIT (1ULL << 0)
@@ -411,8 +412,6 @@ u64 fuse_get_unique(struct fuse_iqueue *fiq);
struct fuse_dev *fuse_dev_alloc_install(struct fuse_chan *fch);
struct fuse_dev *fuse_dev_alloc(void);
-int fuse_dev_release(struct inode *inode, struct file *file);
-
struct list_head *fuse_pqueue_alloc(void);
/*
diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h
index b6153db7e76a..8bceab59c1ca 100644
--- a/fs/fuse/fuse_i.h
+++ b/fs/fuse/fuse_i.h
@@ -365,8 +365,6 @@ union fuse_file_args {
struct fuse_release_args release_args;
};
-#define FUSE_ARGS(args) struct fuse_args args = {}
-
/** The request IO state (for asynchronous processing) */
struct fuse_io_priv {
struct kref refcnt;
@@ -959,9 +957,6 @@ static inline void fuse_sync_bucket_dec(struct fuse_sync_bucket *bucket)
rcu_read_unlock();
}
-/** Device operations */
-extern const struct file_operations fuse_dev_operations;
-
extern const struct dentry_operations fuse_dentry_operations;
/*
@@ -1410,8 +1405,4 @@ int fuse_ext_map_mmap(struct file *file, struct vm_area_struct *vma);
bool fuse_ext_map_is_dax(struct fuse_backing *fb);
int fuse_ext_map_populate(struct fuse_conn *fc, struct fuse_notify_backing_map_out *arg,
struct fuse_extent *ext);
-
-/* helper.c */
-int fuse_kv_parse_one(char **pp, char *end, char **keyp, char **valp);
-
#endif /* _FS_FUSE_I_H */
diff --git a/include/net/fuse.h b/include/net/fuse.h
new file mode 100644
index 000000000000..206193f4653a
--- /dev/null
+++ b/include/net/fuse.h
@@ -0,0 +1,85 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * FUSE transport interface for users outside of fs/fuse/.
+ *
+ * This is the bare minimum a driver needs to implement a device which is
+ * backed by a userspace server.
+ */
+
+#ifndef _NET_FUSE_H
+#define _NET_FUSE_H
+
+#include <linux/types.h>
+
+struct file;
+struct file_operations;
+struct fuse_conn;
+struct fuse_dev;
+struct inode;
+struct user_namespace;
+
+/** One input argument of a request */
+struct fuse_in_arg {
+ unsigned size;
+ const void *value;
+};
+
+/** One output argument of a request */
+struct fuse_arg {
+ unsigned size;
+ void *value;
+};
+
+struct fuse_args {
+ u64 nodeid;
+ u32 opcode;
+ u32 uid;
+ u32 gid;
+ u32 pid;
+ u8 in_numargs;
+ u8 out_numargs;
+ u8 ext_idx;
+ bool force:1;
+ bool noreply:1;
+ bool nocreds:1;
+ bool in_pages:1;
+ bool out_pages:1;
+ bool user_pages:1;
+ bool out_argvar:1;
+ bool page_zeroing:1;
+ bool may_block:1;
+ bool is_ext:1;
+ bool is_pinned:1;
+ bool invalidate_vmap:1;
+ bool abort_on_kill:1;
+ /* server requested io-uring zero-copy for this op */
+ bool zero_copy:1;
+ struct fuse_in_arg in_args[4];
+ struct fuse_arg out_args[2];
+ void (*end)(struct fuse_args *args, int error);
+ /* Used for kvec iter backed by vmalloc address */
+ void *vmap_base;
+};
+
+#define FUSE_ARGS(args) struct fuse_args args = {}
+
+/** Device operations */
+extern const struct file_operations fuse_dev_operations;
+
+int fuse_dev_release(struct inode *inode, struct file *file);
+void fuse_dev_put(struct fuse_dev *fud);
+
+/**
+ * Acquire reference to fuse_conn
+ */
+struct fuse_conn *fuse_conn_get(struct fuse_conn *fc);
+
+/**
+ * Release reference to fuse_conn
+ */
+void fuse_conn_put(struct fuse_conn *fc);
+
+/* Helpers for userspace backed devices, see fs/fuse/helper.c */
+int fuse_kv_parse_one(char **pp, char *end, char **keyp, char **valp);
+
+#endif /* _NET_FUSE_H */
--
2.55.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 13+ messages in thread* Re: [PATCH 02/10] fuse: Add a public header for users outside of fs/fuse/
2026-11-30 8:26 ` [PATCH 02/10] fuse: Add a public header for users outside of fs/fuse/ Richard Weinberger (sigma star gmbh)
@ 2026-11-30 9:14 ` sashiko-bot
0 siblings, 0 replies; 13+ messages in thread
From: sashiko-bot @ 2026-11-30 9:14 UTC (permalink / raw)
To: Richard Weinberger (sigma star gmbh), linux-mtd
Cc: horms, pabeni, kuba, edumazet, davem, miklos, vigneshr,
miquel.raynal, david.oberhollenzer, upstream+linux,
Richard Weinberger
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] fuse: public FUSE transport header placed in include/net/fuse.h
--
commit 049451d0282a3717aec52b6b57650e75dba5be93
Author: Richard Weinberger <richard@nod.at>
fuse: Add a public header for users outside of fs/fuse/
This commit introduces include/net/fuse.h to expose the core FUSE
transport structures and helpers to drivers outside of fs/fuse/.
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 68d5736ca3ccd..0a2c796959315 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -11054,6 +11054,7 @@ W: https://github.com/libfuse/
> T: git git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/fuse.git
> F: Documentation/filesystems/fuse/*
> F: fs/fuse/
> +F: include/net/fuse.h
> F: include/uapi/linux/fuse.h
> F: tools/testing/selftests/filesystems/fuse/
>
[ ... ]
> diff --git a/include/net/fuse.h b/include/net/fuse.h
> new file mode 100644
> index 0000000000000..206193f4653a7
> --- /dev/null
> +++ b/include/net/fuse.h
> @@ -0,0 +1,85 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +/*
> + * FUSE transport interface for users outside of fs/fuse/.
> + *
> + * This is the bare minimum a driver needs to implement a device which is
> + * backed by a userspace server.
> + */
> +
> +#ifndef _NET_FUSE_H
> +#define _NET_FUSE_H
[Severity: Low]
Should this public FUSE transport header live under include/linux/ rather
than include/net/?
include/net/ is reserved for networking headers and is covered by the
F: include/net/ wildcard entry under NETWORKING [GENERAL] in MAINTAINERS,
so placing a FUSE device-transport header there routes patches touching
include/net/fuse.h to netdev@vger.kernel.org and networking maintainers.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261130082634.2750846-1-rw@kernel.org?part=2
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH 03/10] fuse: Add an interface for userspace backed devices
2026-11-30 8:26 [PATCH 0/10] MUSE: Userspace backed MTD v4 Richard Weinberger (sigma star gmbh)
2026-11-30 8:26 ` [PATCH 01/10] fuse: Share cuse_parse_one() as fuse_kv_parse_one() Richard Weinberger (sigma star gmbh)
2026-11-30 8:26 ` [PATCH 02/10] fuse: Add a public header for users outside of fs/fuse/ Richard Weinberger (sigma star gmbh)
@ 2026-11-30 8:26 ` Richard Weinberger (sigma star gmbh)
2026-11-30 8:26 ` [PATCH 04/10] fuse: Convert CUSE to the " Richard Weinberger (sigma star gmbh)
` (6 subsequent siblings)
9 siblings, 0 replies; 13+ messages in thread
From: Richard Weinberger (sigma star gmbh) @ 2026-11-30 8:26 UTC (permalink / raw)
To: linux-mtd
Cc: fuse-devel, netdev, horms, pabeni, kuba, edumazet, davem, miklos,
vigneshr, miquel.raynal, david.oberhollenzer, upstream+linux,
Richard Weinberger
From: Richard Weinberger <richard@nod.at>
CUSE embeds struct fuse_conn and struct fuse_mount and therefore has to
live next to the FUSE core. A driver outside of fs/fuse/ shall not see
these structures.
Add a small interface on top of the opaque struct fuse_conn:
fuse_conn_new_dev() allocates a connection together with its dummy
mount and its channel, and takes a release callback and a private
pointer of the caller.
fuse_conn_priv() returns that private pointer.
fuse_conn_install_dev() attaches the connection to the just opened
control device and makes it accept requests.
fuse_conn_from_file() returns the connection of an open control device.
fuse_conn_send() and fuse_conn_send_bg() send a request, like
fuse_simple_request() and fuse_simple_background() do.
fuse_conn_abort() aborts the connection.
Signed-off-by: Richard Weinberger <richard@nod.at>
---
fs/fuse/helper.c | 161 ++++++++++++++++++++++++++++++++++++++++++++-
include/net/fuse.h | 17 ++++-
2 files changed, 175 insertions(+), 3 deletions(-)
diff --git a/fs/fuse/helper.c b/fs/fuse/helper.c
index 29db362d930c..5266c4e47eeb 100644
--- a/fs/fuse/helper.c
+++ b/fs/fuse/helper.c
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
- * Helper functions used by CUSE and MUSE
+ * Helper functions used by userspace backed devices like CUSE and MUSE
*
* Copyright (C) 2008-2009 SUSE Linux Products GmbH
* Copyright (C) 2008-2009 Tejun Heo <tj@kernel.org>
@@ -11,9 +11,168 @@
#include <linux/export.h>
#include <linux/printk.h>
+#include <linux/slab.h>
#include <linux/string.h>
+#include "dev.h"
#include "fuse_i.h"
+#include "fuse_dev_i.h"
+
+/*
+ * A connection which does not belong to a filesystem but to a device which
+ * is implemented by a userspace server. The device driver sees only an
+ * opaque struct fuse_conn, see <net/fuse.h>.
+ */
+struct fuse_dev_conn {
+ struct fuse_conn fc;
+ struct fuse_mount fm; /* Dummy mount referencing fc */
+ void (*release)(void *priv);
+ void *priv;
+};
+
+static struct fuse_dev_conn *to_dev_conn(struct fuse_conn *fc)
+{
+ return container_of(fc, struct fuse_dev_conn, fc);
+}
+
+static void fuse_dev_conn_release(struct fuse_conn *fc)
+{
+ struct fuse_dev_conn *dc = to_dev_conn(fc);
+
+ if (dc->release)
+ dc->release(dc->priv);
+ kfree(dc);
+}
+
+/**
+ * fuse_conn_new_dev - create a connection for a userspace backed device
+ * @user_ns: user namespace of the connection
+ * @release: called when the last reference to the connection is dropped,
+ * can be NULL
+ * @priv: private data of the caller, see fuse_conn_priv()
+ *
+ * The caller owns the returned reference and drops it with fuse_conn_put().
+ * @release is called from an RCU callback. A module which passes one has to
+ * call rcu_barrier() before it goes away.
+ *
+ * RETURNS:
+ * The new connection or NULL if out of memory.
+ */
+struct fuse_conn *fuse_conn_new_dev(struct user_namespace *user_ns,
+ void (*release)(void *priv), void *priv)
+{
+ struct fuse_chan *fch __free(fuse_chan_free) = fuse_dev_chan_new();
+ struct fuse_dev_conn *dc;
+
+ if (!fch)
+ return NULL;
+
+ dc = kzalloc_obj(*dc);
+ if (!dc)
+ return NULL;
+
+ fuse_conn_init(&dc->fc, &dc->fm, user_ns, no_free_ptr(fch));
+ dc->fc.release = fuse_dev_conn_release;
+ dc->release = release;
+ dc->priv = priv;
+
+ return &dc->fc;
+}
+EXPORT_SYMBOL_GPL(fuse_conn_new_dev);
+
+/**
+ * fuse_conn_priv - private data of a connection
+ * @fc: connection created by fuse_conn_new_dev()
+ *
+ * RETURNS:
+ * The @priv pointer passed to fuse_conn_new_dev().
+ */
+void *fuse_conn_priv(struct fuse_conn *fc)
+{
+ return to_dev_conn(fc)->priv;
+}
+EXPORT_SYMBOL_GPL(fuse_conn_priv);
+
+/**
+ * fuse_conn_install_dev - attach a connection to its control device
+ * @fc: connection created by fuse_conn_new_dev()
+ *
+ * To be called from the open method of the control device. On success the
+ * connection accepts requests. Store the result in file->private_data,
+ * fuse_dev_operations and fuse_dev_release() take it from there. If open
+ * fails later, drop it with fuse_dev_put().
+ *
+ * RETURNS:
+ * The FUSE device or NULL if out of memory.
+ */
+struct fuse_dev *fuse_conn_install_dev(struct fuse_conn *fc)
+{
+ struct fuse_dev *fud;
+
+ fud = fuse_dev_alloc_install(fc->chan);
+ if (!fud)
+ return NULL;
+
+ /* Pairs with smp_load_acquire() readers of fch->initialized */
+ smp_store_release(&fc->chan->initialized, 1);
+
+ return fud;
+}
+EXPORT_SYMBOL_GPL(fuse_conn_install_dev);
+
+/**
+ * fuse_conn_from_file - connection of an open control device
+ * @file: file with the result of fuse_conn_install_dev() as private data
+ *
+ * RETURNS:
+ * The connection, valid until fuse_dev_release() got called for @file.
+ */
+struct fuse_conn *fuse_conn_from_file(struct file *file)
+{
+ return fuse_dev_chan_get(fuse_file_to_fud(file))->conn;
+}
+EXPORT_SYMBOL_GPL(fuse_conn_from_file);
+
+/**
+ * fuse_conn_send - send a request and wait for the reply
+ * @fc: connection created by fuse_conn_new_dev()
+ * @args: the request
+ *
+ * RETURNS:
+ * A negative error code if the request failed. Otherwise 0, or the size of
+ * the last output argument if @args->out_argvar is set.
+ */
+ssize_t fuse_conn_send(struct fuse_conn *fc, struct fuse_args *args)
+{
+ return fuse_simple_request(&to_dev_conn(fc)->fm, args);
+}
+EXPORT_SYMBOL_GPL(fuse_conn_send);
+
+/**
+ * fuse_conn_send_bg - send a request without waiting for the reply
+ * @fc: connection created by fuse_conn_new_dev()
+ * @args: the request, @args->end is called when it is done
+ * @gfp_flags: allocation flags
+ *
+ * RETURNS:
+ * 0 if the request got queued, -errno otherwise.
+ */
+int fuse_conn_send_bg(struct fuse_conn *fc, struct fuse_args *args,
+ gfp_t gfp_flags)
+{
+ return fuse_simple_background(&to_dev_conn(fc)->fm, args, gfp_flags);
+}
+EXPORT_SYMBOL_GPL(fuse_conn_send_bg);
+
+/**
+ * fuse_conn_abort - abort all requests and refuse new ones
+ * @fc: connection created by fuse_conn_new_dev()
+ */
+void fuse_conn_abort(struct fuse_conn *fc)
+{
+ fuse_chan_abort(fc->chan, false);
+}
+EXPORT_SYMBOL_GPL(fuse_conn_abort);
/**
* fuse_kv_parse_one - parse one key=value pair
diff --git a/include/net/fuse.h b/include/net/fuse.h
index 206193f4653a..30155f2ed17c 100644
--- a/include/net/fuse.h
+++ b/include/net/fuse.h
@@ -3,7 +3,10 @@
* FUSE transport interface for users outside of fs/fuse/.
*
* This is the bare minimum a driver needs to implement a device which is
- * backed by a userspace server.
+ * backed by a userspace server, like MUSE does for MTD. Such a driver owns a
+ * control device (a clone of fuse_dev_operations). Every open of it creates
+ * a FUSE connection the driver uses to send requests to the server which
+ * opened the control device.
*/
#ifndef _NET_FUSE_H
@@ -79,7 +82,17 @@ struct fuse_conn *fuse_conn_get(struct fuse_conn *fc);
*/
void fuse_conn_put(struct fuse_conn *fc);
-/* Helpers for userspace backed devices, see fs/fuse/helper.c */
+/* Connections of userspace backed devices, see fs/fuse/helper.c */
+struct fuse_conn *fuse_conn_new_dev(struct user_namespace *user_ns,
+ void (*release)(void *priv), void *priv);
+void *fuse_conn_priv(struct fuse_conn *fc);
+struct fuse_dev *fuse_conn_install_dev(struct fuse_conn *fc);
+struct fuse_conn *fuse_conn_from_file(struct file *file);
+ssize_t fuse_conn_send(struct fuse_conn *fc, struct fuse_args *args);
+int fuse_conn_send_bg(struct fuse_conn *fc, struct fuse_args *args,
+ gfp_t gfp_flags);
+void fuse_conn_abort(struct fuse_conn *fc);
+
int fuse_kv_parse_one(char **pp, char *end, char **keyp, char **valp);
#endif /* _NET_FUSE_H */
--
2.55.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH 04/10] fuse: Convert CUSE to the interface for userspace backed devices
2026-11-30 8:26 [PATCH 0/10] MUSE: Userspace backed MTD v4 Richard Weinberger (sigma star gmbh)
` (2 preceding siblings ...)
2026-11-30 8:26 ` [PATCH 03/10] fuse: Add an interface for userspace backed devices Richard Weinberger (sigma star gmbh)
@ 2026-11-30 8:26 ` Richard Weinberger (sigma star gmbh)
2026-11-30 8:26 ` [PATCH 05/10] fuse: Allow three output arguments per request Richard Weinberger (sigma star gmbh)
` (5 subsequent siblings)
9 siblings, 0 replies; 13+ messages in thread
From: Richard Weinberger (sigma star gmbh) @ 2026-11-30 8:26 UTC (permalink / raw)
To: linux-mtd
Cc: fuse-devel, netdev, horms, pabeni, kuba, edumazet, davem, miklos,
vigneshr, miquel.raynal, david.oberhollenzer, upstream+linux,
Richard Weinberger
From: Richard Weinberger <richard@nod.at>
CUSE embeds struct fuse_conn and struct fuse_mount in struct cuse_conn
and sets up the connection, its channel and its device on its own.
fuse_conn_new_dev() and fuse_conn_install_dev() do the very same.
Use them, and fuse_conn_from_file(), fuse_conn_send_bg() and
fuse_conn_abort() too. struct cuse_conn becomes the private data of the
connection and is freed by the release callback.
CUSE opens files on the dummy mount of its connection. Add
fuse_conn_dev_mount() for that, it is not part of <net/fuse.h>.
Signed-off-by: Richard Weinberger <richard@nod.at>
---
fs/fuse/cuse.c | 56 ++++++++++++++++++++++--------------------------
fs/fuse/fuse_i.h | 3 +++
fs/fuse/helper.c | 10 +++++++++
3 files changed, 39 insertions(+), 30 deletions(-)
diff --git a/fs/fuse/cuse.c b/fs/fuse/cuse.c
index 73512b26da10..c77cc85513e0 100644
--- a/fs/fuse/cuse.c
+++ b/fs/fuse/cuse.c
@@ -13,7 +13,7 @@
*
* A CUSE instance involves the following objects.
*
- * cuse_conn : contains fuse_conn and serves as bonding structure
+ * cuse_conn : private data of the fuse_conn, serves as bonding structure
* channel : file handle connected to the userland CUSE server
* cdev : the implemented character device
* dev : generic device for cdev
@@ -59,8 +59,7 @@
struct cuse_conn {
struct list_head list; /* linked on cuse_conntbl */
- struct fuse_mount fm; /* Dummy mount referencing fc */
- struct fuse_conn fc; /* fuse connection */
+ struct fuse_conn *fc; /* fuse connection */
struct cdev *cdev; /* associated character device */
struct device *dev; /* device representing @cdev */
@@ -74,7 +73,7 @@ static struct class *cuse_class;
static struct cuse_conn *fc_to_cc(struct fuse_conn *fc)
{
- return container_of(fc, struct cuse_conn, fc);
+ return fuse_conn_priv(fc);
}
static struct list_head *cuse_conntbl_head(dev_t devt)
@@ -123,7 +122,7 @@ static int cuse_open(struct inode *inode, struct file *file)
mutex_lock(&cuse_lock);
list_for_each_entry(pos, cuse_conntbl_head(devt), list)
if (pos->dev->devt == devt) {
- fuse_conn_get(&pos->fc);
+ fuse_conn_get(pos->fc);
cc = pos;
break;
}
@@ -137,9 +136,9 @@ static int cuse_open(struct inode *inode, struct file *file)
* Generic permission check is already done against the chrdev
* file, proceed to open.
*/
- rc = fuse_do_open(&cc->fm, 0, file, 0);
+ rc = fuse_do_open(fuse_conn_dev_mount(cc->fc), 0, file, 0);
if (rc)
- fuse_conn_put(&cc->fc);
+ fuse_conn_put(cc->fc);
return rc;
}
@@ -370,7 +369,7 @@ static void cuse_process_init_reply(struct fuse_args *args, int error)
err_region:
unregister_chrdev_region(devt, 1);
err:
- fuse_chan_abort(fc->chan, false);
+ fuse_conn_abort(fc);
goto out;
}
@@ -378,7 +377,6 @@ static int cuse_send_init(struct cuse_conn *cc)
{
int rc;
struct folio *folio;
- struct fuse_mount *fm = &cc->fm;
struct cuse_init_args *ia;
struct fuse_args_pages *ap;
@@ -413,10 +411,10 @@ static int cuse_send_init(struct cuse_conn *cc)
ap->descs = &ia->desc;
ia->folio = folio;
ia->desc.length = ap->args.out_args[1].size;
- ia->fc = &cc->fc;
+ ia->fc = cc->fc;
ap->args.end = cuse_process_init_reply;
- rc = fuse_simple_background(fm, &ap->args, GFP_KERNEL);
+ rc = fuse_conn_send_bg(cc->fc, &ap->args, GFP_KERNEL);
if (rc) {
kfree(ia);
err_free_folio:
@@ -426,9 +424,9 @@ static int cuse_send_init(struct cuse_conn *cc)
return rc;
}
-static void cuse_fc_release(struct fuse_conn *fc)
+static void cuse_conn_release(void *priv)
{
- kfree(fc_to_cc(fc));
+ kfree(priv);
}
/**
@@ -450,32 +448,31 @@ static int cuse_channel_open(struct inode *inode, struct file *file)
{
struct fuse_dev *fud;
struct cuse_conn *cc;
- struct fuse_chan *fch __free(fuse_chan_free) = fuse_dev_chan_new();
int rc;
- if (!fch)
- return -ENOMEM;
-
/* set up cuse_conn */
cc = kzalloc_obj(*cc);
if (!cc)
return -ENOMEM;
+ INIT_LIST_HEAD(&cc->list);
+
/*
* Limit the cuse channel to requests that can
* be represented in file->f_cred->user_ns.
*/
- fuse_conn_init(&cc->fc, &cc->fm, file->f_cred->user_ns, no_free_ptr(fch));
- cc->fc.release = cuse_fc_release;
- fud = fuse_dev_alloc_install(cc->fc.chan);
- fuse_conn_put(&cc->fc);
- if (!fud)
+ cc->fc = fuse_conn_new_dev(file->f_cred->user_ns, cuse_conn_release, cc);
+ if (!cc->fc) {
+ kfree(cc);
return -ENOMEM;
+ }
- INIT_LIST_HEAD(&cc->list);
+ /* From now on the channel holds the connection, and with it cc */
+ fud = fuse_conn_install_dev(cc->fc);
+ fuse_conn_put(cc->fc);
+ if (!fud)
+ return -ENOMEM;
- /* Pairs with smp_load_acquire() readers of fch->initialized */
- smp_store_release(&cc->fc.chan->initialized, 1);
rc = cuse_send_init(cc);
if (rc) {
fuse_dev_put(fud);
@@ -499,8 +496,7 @@ static int cuse_channel_open(struct inode *inode, struct file *file)
*/
static int cuse_channel_release(struct inode *inode, struct file *file)
{
- struct fuse_dev *fud = __fuse_get_dev(file);
- struct cuse_conn *cc = fc_to_cc(fud->chan->conn);
+ struct cuse_conn *cc = fc_to_cc(fuse_conn_from_file(file));
/* remove from the conntbl, no more access from this point on */
mutex_lock(&cuse_lock);
@@ -532,7 +528,7 @@ static ssize_t cuse_class_waiting_show(struct device *dev,
{
struct cuse_conn *cc = dev_get_drvdata(dev);
- return sprintf(buf, "%d\n", atomic_read(&cc->fc.chan->num_waiting));
+ return sprintf(buf, "%d\n", atomic_read(&cc->fc->chan->num_waiting));
}
static DEVICE_ATTR(waiting, 0400, cuse_class_waiting_show, NULL);
@@ -542,7 +538,7 @@ static ssize_t cuse_class_abort_store(struct device *dev,
{
struct cuse_conn *cc = dev_get_drvdata(dev);
- fuse_chan_abort(cc->fc.chan, false);
+ fuse_conn_abort(cc->fc);
return count;
}
static DEVICE_ATTR(abort, 0200, NULL, cuse_class_abort_store);
@@ -600,7 +596,7 @@ static void __exit cuse_exit(void)
class_destroy(cuse_class);
/*
* Wait for pending call_rcu() callbacks that call back into
- * this module via fc->release (cuse_fc_release).
+ * this module (cuse_conn_release).
*/
rcu_barrier();
}
diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h
index 8bceab59c1ca..a90ac46f1837 100644
--- a/fs/fuse/fuse_i.h
+++ b/fs/fuse/fuse_i.h
@@ -1117,6 +1117,9 @@ void fuse_dentry_set_epoch(struct dentry *dentry, u64 epoch);
void fuse_conn_init(struct fuse_conn *fc, struct fuse_mount *fm,
struct user_namespace *user_ns, struct fuse_chan *fch);
+/* The dummy mount of a connection made by fuse_conn_new_dev() */
+struct fuse_mount *fuse_conn_dev_mount(struct fuse_conn *fc);
+
int fuse_send_init(struct fuse_mount *fm);
/**
diff --git a/fs/fuse/helper.c b/fs/fuse/helper.c
index 5266c4e47eeb..1e389b4f5df9 100644
--- a/fs/fuse/helper.c
+++ b/fs/fuse/helper.c
@@ -93,6 +93,16 @@ void *fuse_conn_priv(struct fuse_conn *fc)
}
EXPORT_SYMBOL_GPL(fuse_conn_priv);
+/*
+ * For CUSE, which opens files on the dummy mount of its connection. Not part
+ * of the interface for users outside of fs/fuse/.
+ */
+struct fuse_mount *fuse_conn_dev_mount(struct fuse_conn *fc)
+{
+ return &to_dev_conn(fc)->fm;
+}
+EXPORT_SYMBOL_GPL(fuse_conn_dev_mount);
+
/**
* fuse_conn_install_dev - attach a connection to its control device
* @fc: connection created by fuse_conn_new_dev()
--
2.55.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH 05/10] fuse: Allow three output arguments per request
2026-11-30 8:26 [PATCH 0/10] MUSE: Userspace backed MTD v4 Richard Weinberger (sigma star gmbh)
` (3 preceding siblings ...)
2026-11-30 8:26 ` [PATCH 04/10] fuse: Convert CUSE to the " Richard Weinberger (sigma star gmbh)
@ 2026-11-30 8:26 ` Richard Weinberger (sigma star gmbh)
2026-11-30 8:26 ` [PATCH 06/10] fuse: Add MUSE specific defines to FUSE interface Richard Weinberger (sigma star gmbh)
` (4 subsequent siblings)
9 siblings, 0 replies; 13+ messages in thread
From: Richard Weinberger (sigma star gmbh) @ 2026-11-30 8:26 UTC (permalink / raw)
To: linux-mtd
Cc: fuse-devel, netdev, horms, pabeni, kuba, edumazet, davem, miklos,
vigneshr, miquel.raynal, david.oberhollenzer, upstream+linux,
Richard Weinberger
From: Richard Weinberger <richard@nod.at>
MTD reads return in-band and out-of-band data in one operation. To
serve such a read with a single request, the reply has to carry a
header, the in-band data and the out-of-band data, each in a buffer of
its own.
Raise the number of output arguments of struct fuse_args from two to
three. The code which copies a reply iterates over out_numargs, so
nothing else has to change.
struct fuse_args is part of every request, so each of them grows by the
size of one struct fuse_arg, 16 bytes on 64-bit.
The first user will be MUSE.
Signed-off-by: Richard Weinberger <richard@nod.at>
---
include/net/fuse.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/include/net/fuse.h b/include/net/fuse.h
index 30155f2ed17c..bd34f2025287 100644
--- a/include/net/fuse.h
+++ b/include/net/fuse.h
@@ -58,7 +58,7 @@ struct fuse_args {
/* server requested io-uring zero-copy for this op */
bool zero_copy:1;
struct fuse_in_arg in_args[4];
- struct fuse_arg out_args[2];
+ struct fuse_arg out_args[3];
void (*end)(struct fuse_args *args, int error);
/* Used for kvec iter backed by vmalloc address */
void *vmap_base;
--
2.55.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH 06/10] fuse: Add MUSE specific defines to FUSE interface
2026-11-30 8:26 [PATCH 0/10] MUSE: Userspace backed MTD v4 Richard Weinberger (sigma star gmbh)
` (4 preceding siblings ...)
2026-11-30 8:26 ` [PATCH 05/10] fuse: Allow three output arguments per request Richard Weinberger (sigma star gmbh)
@ 2026-11-30 8:26 ` Richard Weinberger (sigma star gmbh)
2026-11-30 8:26 ` [PATCH 07/10] mtd: Add MTD_MUSE flag Richard Weinberger (sigma star gmbh)
` (3 subsequent siblings)
9 siblings, 0 replies; 13+ messages in thread
From: Richard Weinberger (sigma star gmbh) @ 2026-11-30 8:26 UTC (permalink / raw)
To: linux-mtd
Cc: fuse-devel, netdev, horms, pabeni, kuba, edumazet, davem, miklos,
vigneshr, miquel.raynal, david.oberhollenzer, upstream+linux,
Richard Weinberger
From: Richard Weinberger <richard@nod.at>
Raise the FUSE API minor version to 48 and add all MUSE specific
operations and data structures.
MUSE_INIT: Initialize a new connection and install the MTD
MUSE_ERASE: Erase a range of the MTD
MUSE_READ: Read in-band and/or out-of-band data
MUSE_WRITE: Write in-band and/or out-of-band data
MUSE_MARKBAD: Mark a block as bad
MUSE_ISBAD: Check whether a block is bad
MUSE_SYNC: Flush all cached data
The requests and their replies are documented next to the definitions.
The keys of the MTD description a server passes to MUSE_INIT are
documented in drivers/mtd/muse.c.
Signed-off-by: Richard Weinberger <richard@nod.at>
---
include/uapi/linux/fuse.h | 249 +++++++++++++++++++++++++++++++++++++-
1 file changed, 248 insertions(+), 1 deletion(-)
diff --git a/include/uapi/linux/fuse.h b/include/uapi/linux/fuse.h
index cb9e9554f0ef..a80e50a32c98 100644
--- a/include/uapi/linux/fuse.h
+++ b/include/uapi/linux/fuse.h
@@ -258,6 +258,10 @@
* - add FUSE_NOTIFY_BACKING_MAP, fuse_notify_backing_map_out, fuse_extent
* - add FUSE_BACKING_MAP_CREATE, FUSE_BACKING_MAP_CYCLIC
* - extend the meaning of FUSE_HAS_INODE_DAX and FUSE_ATTR_DAX
+ *
+ * 7.48
+ * - add support for MUSE: MUSE_INIT, MUSE_ERASE, MUSE_READ, MUSE_WRITE,
+ * MUSE_MARKBAD, MUSE_ISBAD and MUSE_SYNC
*/
#ifndef _LINUX_FUSE_H
@@ -293,7 +297,7 @@
#define FUSE_KERNEL_VERSION 7
/** Minor version number of this interface */
-#define FUSE_KERNEL_MINOR_VERSION 47
+#define FUSE_KERNEL_MINOR_VERSION 48
/** The node ID of the root inode */
#define FUSE_ROOT_ID 1
@@ -708,6 +712,15 @@ enum fuse_opcode {
/* CUSE specific operations */
CUSE_INIT = 4096,
+ /* MUSE specific operations */
+ MUSE_INIT = 8192,
+ MUSE_ERASE = 8193,
+ MUSE_READ = 8194,
+ MUSE_WRITE = 8195,
+ MUSE_MARKBAD = 8196,
+ MUSE_ISBAD = 8197,
+ MUSE_SYNC = 8198,
+
/* Reserved opcodes: helpful to detect structure endian-ness */
CUSE_INIT_BSWAP_RESERVED = 1048576, /* CUSE_INIT << 8 */
FUSE_INIT_BSWAP_RESERVED = 436207616, /* FUSE_INIT << 24 */
@@ -1450,4 +1463,238 @@ struct fuse_uring_cmd_req {
};
};
+/*
+ * MUSE: MTD in userspace
+ *
+ * A MUSE server opens /dev/muse and serves the requests it reads from it,
+ * like a FUSE or CUSE server does. The first request is MUSE_INIT, the reply
+ * to it describes an MTD which the kernel then creates. From then on the
+ * kernel forwards the operations on that MTD to the server. The MTD is
+ * removed when the server closes the device and the MTD has no more users.
+ *
+ * Requests and replies start with struct fuse_in_header and struct
+ * fuse_out_header. The nodeid is always FUSE_ROOT_ID. Replying with an
+ * error in struct fuse_out_header makes the operation on the MTD fail with
+ * that error.
+ *
+ * MUSE_INIT: create the MTD
+ * in: struct muse_init_in
+ * out: struct muse_init_out, followed by the description of the MTD
+ * MUSE_ERASE: erase a range of the MTD
+ * in: struct muse_erase_in
+ * out: nothing
+ * MUSE_READ: read in-band and/or out-of-band data
+ * in: struct muse_read_in
+ * out: struct muse_read_out, followed by the data
+ * MUSE_WRITE: write in-band and/or out-of-band data
+ * in: struct muse_write_in, followed by the data
+ * out: struct muse_write_out
+ * MUSE_MARKBAD: mark an erase block as bad, only sent for NAND type MTDs
+ * in: struct muse_markbad_in
+ * out: nothing
+ * MUSE_ISBAD: ask whether an erase block is bad, only sent for NAND type MTDs
+ * in: struct muse_isbad_in
+ * out: struct muse_isbad_out
+ * MUSE_SYNC: write all cached data to the storage, an error is ignored
+ * in: nothing
+ * out: nothing
+ */
+
+/**
+ * define MUSE_INIT_INFO_MAX - Maximum size of the description of the MTD
+ *
+ * The reply to MUSE_INIT carries the description of the MTD right after
+ * struct muse_init_out, as a sequence of "KEY=VALUE" strings, each of them
+ * terminated by a NUL byte. This is the format CUSE_INIT uses. Numbers are
+ * decimal. Known keys are documented in drivers/mtd/muse.c
+ */
+#define MUSE_INIT_INFO_MAX 4096
+
+/**
+ * struct muse_init_in - MUSE_INIT request
+ * @fuse_major: major version of the protocol the kernel speaks,
+ * FUSE_KERNEL_VERSION
+ * @fuse_minor: minor version of the protocol the kernel speaks,
+ * FUSE_KERNEL_MINOR_VERSION
+ */
+struct muse_init_in {
+ uint32_t fuse_major;
+ uint32_t fuse_minor;
+};
+
+/**
+ * struct muse_init_out - MUSE_INIT reply
+ * @fuse_major: major version of the protocol the server speaks, has to be
+ * the one of the kernel
+ * @fuse_minor: minor version of the protocol the server speaks, at least 48
+ * @max_read: maximum number of bytes, in-band and out-of-band data together,
+ * the server returns in one MUSE_READ reply
+ * @max_write: maximum number of bytes, in-band and out-of-band data
+ * together, the server takes in one MUSE_WRITE request
+ *
+ * The kernel splits larger reads and writes into multiple requests. It uses
+ * a limit of at least 4096 bytes, and both limits have to allow for a
+ * request of WRITEBUFSIZE bytes of in-band data together with OOBSIZE bytes
+ * of out-of-band data.
+ *
+ * The description of the MTD follows this structure, see MUSE_INIT_INFO_MAX.
+ */
+struct muse_init_out {
+ uint32_t fuse_major;
+ uint32_t fuse_minor;
+ uint32_t max_read;
+ uint32_t max_write;
+};
+
+/**
+ * struct muse_erase_in - MUSE_ERASE request
+ * @addr: address on the MTD where the range to erase starts
+ * @len: length of the range in bytes
+ *
+ * The range is within the MTD. It is up to the server to reject a range
+ * which does not consist of whole erase blocks.
+ */
+struct muse_erase_in {
+ uint64_t addr;
+ uint64_t len;
+};
+
+/*
+ * MUSE_READ and MUSE_WRITE request flags
+ *
+ * MUSE_IO_INBAND: the request has in-band data, len is not 0
+ * MUSE_IO_OOB_AUTO: the request has out-of-band data, ooblen is not 0. It
+ * addresses only the free bytes of the out-of-band area,
+ * OOBAVAIL bytes per page, the server places them
+ * MUSE_IO_OOB_PLACE: the request has out-of-band data, ooblen is not 0. It
+ * addresses the whole out-of-band area, OOBSIZE bytes per
+ * page
+ * MUSE_IO_RAW: raw access, without error correction
+ *
+ * At most one of MUSE_IO_OOB_AUTO and MUSE_IO_OOB_PLACE is set.
+ */
+#define MUSE_IO_INBAND (1 << 0)
+#define MUSE_IO_OOB_AUTO (1 << 1)
+#define MUSE_IO_OOB_PLACE (1 << 2)
+#define MUSE_IO_RAW (1 << 3)
+
+/**
+ * struct muse_read_in - MUSE_READ request
+ * @addr: address on the MTD where the in-band data starts
+ * @len: number of bytes of in-band data to read
+ * @ooblen: number of bytes of out-of-band data to read
+ * @ooboffs: offset in the out-of-band area of the page @addr belongs to
+ * where the out-of-band data starts. It continues at offset 0 of
+ * the out-of-band area of the following pages
+ * @flags: MUSE_IO_* flags
+ * @padding: unused
+ *
+ * A request can ask for in-band data, out-of-band data or both, but not for
+ * nothing. @len and @ooblen together do not exceed max_read of struct
+ * muse_init_out. The kernel splits a larger read at page boundaries, such
+ * that a request asks for the out-of-band data of the very pages its
+ * in-band data is in.
+ */
+struct muse_read_in {
+ uint64_t addr;
+ uint64_t len;
+ uint32_t ooblen;
+ uint32_t ooboffs;
+ uint32_t flags;
+ uint32_t padding;
+};
+
+/**
+ * struct muse_read_out - MUSE_READ reply
+ * @len: number of bytes of in-band data read
+ * @ooblen: number of bytes of out-of-band data read
+ * @soft_error: 0 or a negative errno value
+ *
+ * This structure is followed by exactly as many bytes of in-band data as the
+ * request asked for, and then by exactly as many bytes of out-of-band data
+ * as it asked for. Short reads are not allowed: @len and @ooblen have to be
+ * the values of the request, unless @soft_error is set.
+ *
+ * With @soft_error the server makes the read return an error although the
+ * request itself was served. Two values are what a flash driver uses to
+ * report on its error correction, the data which was read is passed on to
+ * the reader together with them: -EUCLEAN, bit-flips were corrected, and
+ * -EBADMSG, the data has bit-flips which could not be corrected. Any other
+ * value makes the read fail with it.
+ */
+struct muse_read_out {
+ uint64_t len;
+ uint32_t ooblen;
+ int32_t soft_error;
+};
+
+/**
+ * struct muse_write_in - MUSE_WRITE request
+ * @addr: address on the MTD where the in-band data starts
+ * @len: number of bytes of in-band data to write
+ * @ooblen: number of bytes of out-of-band data to write
+ * @ooboffs: offset in the out-of-band area of the page @addr belongs to
+ * where the out-of-band data starts. It continues at offset 0 of
+ * the out-of-band area of the following pages
+ * @flags: MUSE_IO_* flags
+ * @padding: unused
+ *
+ * This structure is followed by @len bytes of in-band data and then by
+ * @ooblen bytes of out-of-band data. A request can carry in-band data,
+ * out-of-band data or both, but not nothing. @len and @ooblen together do
+ * not exceed max_write of struct muse_init_out. The kernel splits a larger
+ * write at page boundaries, such that a request carries the out-of-band data
+ * of the very pages its in-band data is for.
+ */
+struct muse_write_in {
+ uint64_t addr;
+ uint64_t len;
+ uint32_t ooblen;
+ uint32_t ooboffs;
+ uint32_t flags;
+ uint32_t padding;
+};
+
+/**
+ * struct muse_write_out - MUSE_WRITE reply
+ * @len: number of bytes of in-band data written
+ * @ooblen: number of bytes of out-of-band data written
+ * @soft_error: 0 or a negative errno value
+ *
+ * Short writes are not allowed: @len and @ooblen have to be the values of
+ * the request, unless @soft_error is set. A @soft_error makes the write
+ * fail with it.
+ */
+struct muse_write_out {
+ uint64_t len;
+ uint32_t ooblen;
+ int32_t soft_error;
+};
+
+/**
+ * struct muse_markbad_in - MUSE_MARKBAD request
+ * @addr: address on the MTD within the erase block to mark as bad
+ */
+struct muse_markbad_in {
+ uint64_t addr;
+};
+
+/**
+ * struct muse_isbad_in - MUSE_ISBAD request
+ * @addr: address on the MTD within the erase block in question
+ */
+struct muse_isbad_in {
+ uint64_t addr;
+};
+
+/**
+ * struct muse_isbad_out - MUSE_ISBAD reply
+ * @result: 1 if the erase block is bad, 0 if it is not
+ * @padding: unused
+ */
+struct muse_isbad_out {
+ uint32_t result;
+ uint32_t padding;
+};
+
#endif /* _LINUX_FUSE_H */
--
2.55.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH 07/10] mtd: Add MTD_MUSE flag
2026-11-30 8:26 [PATCH 0/10] MUSE: Userspace backed MTD v4 Richard Weinberger (sigma star gmbh)
` (5 preceding siblings ...)
2026-11-30 8:26 ` [PATCH 06/10] fuse: Add MUSE specific defines to FUSE interface Richard Weinberger (sigma star gmbh)
@ 2026-11-30 8:26 ` Richard Weinberger (sigma star gmbh)
2026-11-30 8:26 ` [PATCH 08/10] mtd: Allow passing a custom mtdparts string to the cmdline parser Richard Weinberger (sigma star gmbh)
` (2 subsequent siblings)
9 siblings, 0 replies; 13+ messages in thread
From: Richard Weinberger (sigma star gmbh) @ 2026-11-30 8:26 UTC (permalink / raw)
To: linux-mtd
Cc: fuse-devel, netdev, horms, pabeni, kuba, edumazet, davem, miklos,
vigneshr, miquel.raynal, david.oberhollenzer, upstream+linux,
Richard Weinberger
From: Richard Weinberger <richard@nod.at>
This flag is set if an MTD is implemented in userspace using MUSE.
Signed-off-by: Richard Weinberger <richard@nod.at>
---
include/uapi/mtd/mtd-abi.h | 1 +
1 file changed, 1 insertion(+)
diff --git a/include/uapi/mtd/mtd-abi.h b/include/uapi/mtd/mtd-abi.h
index 714d55b49d2a..f72d07387b5e 100644
--- a/include/uapi/mtd/mtd-abi.h
+++ b/include/uapi/mtd/mtd-abi.h
@@ -152,6 +152,7 @@ struct mtd_read_req {
#define MTD_NO_ERASE 0x1000 /* No erase necessary */
#define MTD_POWERUP_LOCK 0x2000 /* Always locked after reset */
#define MTD_SLC_ON_MLC_EMULATION 0x4000 /* Emulate SLC behavior on MLC NANDs */
+#define MTD_MUSE 0x8000 /* This MTD is implemented in userspace */
/* Some common devices / combinations of capabilities */
#define MTD_CAP_ROM 0
--
2.55.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH 08/10] mtd: Allow passing a custom mtdparts string to the cmdline parser
2026-11-30 8:26 [PATCH 0/10] MUSE: Userspace backed MTD v4 Richard Weinberger (sigma star gmbh)
` (6 preceding siblings ...)
2026-11-30 8:26 ` [PATCH 07/10] mtd: Add MTD_MUSE flag Richard Weinberger (sigma star gmbh)
@ 2026-11-30 8:26 ` Richard Weinberger (sigma star gmbh)
2026-11-30 8:26 ` [PATCH 09/10] mtd: Implement MUSE - MTD in userspace Richard Weinberger (sigma star gmbh)
2026-11-30 8:26 ` [PATCH 10/10] MAINTAINERS: Add entry for MUSE Richard Weinberger (sigma star gmbh)
9 siblings, 0 replies; 13+ messages in thread
From: Richard Weinberger (sigma star gmbh) @ 2026-11-30 8:26 UTC (permalink / raw)
To: linux-mtd
Cc: fuse-devel, netdev, horms, pabeni, kuba, edumazet, davem, miklos,
vigneshr, miquel.raynal, david.oberhollenzer, upstream+linux,
Richard Weinberger
From: Richard Weinberger <richard@nod.at>
The cmdline parser takes the partition definitions from the mtdparts=
parameter of the kernel command line. For special purpose MTDs like
MUSE it is useful to pass a custom mtdparts string to the parser. That
way an MTD simulator in userspace can define the partitions of its MTD.
Add an mtdparts pointer to struct mtd_part_parser_data. If it is set,
the parser uses this string and ignores the kernel command line.
What is parsed from a custom string is freed before the parser returns,
while the definitions from the kernel command line are kept forever. So
far the partitions handed to the MTD core pointed to names inside these
definitions. Copy the names into the returned array, such that it does
not depend on the lifetime of what was parsed.
Signed-off-by: Richard Weinberger <richard@nod.at>
---
drivers/mtd/parsers/cmdlinepart.c | 99 +++++++++++++++++++++++++------
include/linux/mtd/partitions.h | 2 +
2 files changed, 82 insertions(+), 19 deletions(-)
diff --git a/drivers/mtd/parsers/cmdlinepart.c b/drivers/mtd/parsers/cmdlinepart.c
index 4caf1b3804f2..9848bbea271c 100644
--- a/drivers/mtd/parsers/cmdlinepart.c
+++ b/drivers/mtd/parsers/cmdlinepart.c
@@ -56,7 +56,7 @@ struct cmdline_mtd_partition {
};
/* mtdpart_setup() parses into here */
-static struct cmdline_mtd_partition *partitions;
+static struct cmdline_mtd_partition *cmdline_partitions;
/* the command line passed to mtdpart_setup() */
static char *mtdparts;
@@ -209,10 +209,8 @@ static struct mtd_partition * newpart(char *s,
/*
* Parse the command line.
*/
-static int mtdpart_setup_real(char *s)
+static int mtdpart_setup_real(char *s, struct cmdline_mtd_partition **partitions)
{
- cmdline_parsed = 1;
-
for( ; s != NULL; )
{
struct cmdline_mtd_partition *this_mtd;
@@ -292,8 +290,8 @@ static int mtdpart_setup_real(char *s)
strscpy(this_mtd->mtd_id, mtd_id, mtd_id_len + 1);
/* link into chain */
- this_mtd->next = partitions;
- partitions = this_mtd;
+ this_mtd->next = *partitions;
+ *partitions = this_mtd;
pr_debug("mtdid=<%s> num_parts=<%d>\n",
this_mtd->mtd_id, this_mtd->num_parts);
@@ -314,6 +312,62 @@ static int mtdpart_setup_real(char *s)
return 0;
}
+/*
+ * Free a list built by mtdpart_setup_real(). The partitions of an entry,
+ * their names and the entry itself share one allocation, see newpart().
+ */
+static void free_partitions(struct cmdline_mtd_partition *part)
+{
+ while (part) {
+ struct cmdline_mtd_partition *next = part->next;
+
+ kfree(part->parts);
+ part = next;
+ }
+}
+
+/*
+ * Copy the partitions of an entry and their names into one allocation. The
+ * names of the entry cannot be handed out: for a custom mtdparts string
+ * they are gone when parse_cmdline_partitions() returns.
+ */
+static struct mtd_partition *dup_parts(const struct cmdline_mtd_partition *part)
+{
+ size_t parts_size = sizeof(*part->parts) * part->num_parts;
+ size_t names_size = 0;
+ struct mtd_partition *parts;
+ char *name;
+ int i;
+
+ for (i = 0; i < part->num_parts; i++)
+ names_size += strlen(part->parts[i].name) + 1;
+
+ parts = kmalloc(parts_size + names_size, GFP_KERNEL);
+ if (!parts)
+ return NULL;
+
+ memcpy(parts, part->parts, parts_size);
+ name = (char *)(parts + part->num_parts);
+ for (i = 0; i < part->num_parts; i++) {
+ size_t len = strlen(part->parts[i].name) + 1;
+
+ parts[i].name = memcpy(name, part->parts[i].name, len);
+ name += len;
+ }
+
+ return parts;
+}
+
+/* Parse the kernel command line once, what it defines is kept. */
+static int parse_kernel_cmdline(void)
+{
+ if (cmdline_parsed)
+ return 0;
+
+ cmdline_parsed = 1;
+ return mtdpart_setup_real(cmdline, &cmdline_partitions);
+}
+
/*
* Main function to be called from the MTD mapping driver/device to
* obtain the partitioning information. At this point the command line
@@ -326,28 +380,34 @@ static int parse_cmdline_partitions(struct mtd_info *master,
struct mtd_part_parser_data *data)
{
unsigned long long offset;
- int i, err;
+ int i, ret;
struct cmdline_mtd_partition *part;
+ struct cmdline_mtd_partition *custom_parts = NULL;
const char *mtd_id = master->name;
- /* parse command line */
- if (!cmdline_parsed) {
- err = mtdpart_setup_real(cmdline);
- if (err)
- return err;
+ if (data && data->mtdparts) {
+ /* A string of the caller, the kernel command line is not used. */
+ ret = mtdpart_setup_real(data->mtdparts, &custom_parts);
+ part = custom_parts;
+ } else {
+ ret = parse_kernel_cmdline();
+ part = cmdline_partitions;
}
+ if (ret)
+ goto out;
/*
* Search for the partition definition matching master->name.
* If master->name is not set, stop at first partition definition.
*/
- for (part = partitions; part; part = part->next) {
+ for (; part; part = part->next) {
if ((!mtd_id) || (!strcmp(part->mtd_id, mtd_id)))
break;
}
+ ret = 0;
if (!part)
- return 0;
+ goto out;
for (i = 0, offset = 0; i < part->num_parts; i++) {
if (part->parts[i].offset == OFFSET_CONTINUOUS)
@@ -375,12 +435,13 @@ static int parse_cmdline_partitions(struct mtd_info *master,
}
}
- *pparts = kmemdup(part->parts, sizeof(*part->parts) * part->num_parts,
- GFP_KERNEL);
- if (!*pparts)
- return -ENOMEM;
+ *pparts = dup_parts(part);
+ ret = *pparts ? part->num_parts : -ENOMEM;
- return part->num_parts;
+out:
+ /* What was parsed from the kernel command line is kept. */
+ free_partitions(custom_parts);
+ return ret;
}
diff --git a/include/linux/mtd/partitions.h b/include/linux/mtd/partitions.h
index b74a539ec581..6c8b3399143d 100644
--- a/include/linux/mtd/partitions.h
+++ b/include/linux/mtd/partitions.h
@@ -65,9 +65,11 @@ struct device_node;
/**
* struct mtd_part_parser_data - used to pass data to MTD partition parsers.
* @origin: for RedBoot, start address of MTD device
+ * @mtdparts: for cmdline parser, use this string instead of mtdparts= from cmdline
*/
struct mtd_part_parser_data {
unsigned long origin;
+ char *mtdparts;
};
--
2.55.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH 09/10] mtd: Implement MUSE - MTD in userspace
2026-11-30 8:26 [PATCH 0/10] MUSE: Userspace backed MTD v4 Richard Weinberger (sigma star gmbh)
` (7 preceding siblings ...)
2026-11-30 8:26 ` [PATCH 08/10] mtd: Allow passing a custom mtdparts string to the cmdline parser Richard Weinberger (sigma star gmbh)
@ 2026-11-30 8:26 ` Richard Weinberger (sigma star gmbh)
2026-11-30 9:14 ` sashiko-bot
2026-11-30 8:26 ` [PATCH 10/10] MAINTAINERS: Add entry for MUSE Richard Weinberger (sigma star gmbh)
9 siblings, 1 reply; 13+ messages in thread
From: Richard Weinberger (sigma star gmbh) @ 2026-11-30 8:26 UTC (permalink / raw)
To: linux-mtd
Cc: fuse-devel, netdev, horms, pabeni, kuba, edumazet, davem, miklos,
vigneshr, miquel.raynal, david.oberhollenzer, upstream+linux,
Richard Weinberger
From: Richard Weinberger <richard@nod.at>
MUSE allows implementing an MTD in userspace. A server opens /dev/muse,
describes the MTD in its reply to MUSE_INIT and then serves the
requests of that MTD. So far userspace has control over mtd_read,
mtd_write, mtd_erase, mtd_block_isbad, mtd_block_markbad, and mtd_sync.
It can also set the various MTD parameters such as name, type, flags,
size, writesize and erasesize, and it can pass partitions in the syntax
of the mtdparts= kernel parameter.
That way advanced simulators for many types of flashes can be
implemented in userspace such that the complexity is in userspace.
Furthermore at some point we can deprecate ad-hoc in-kernel MTD
simulators such as nandsim.
MUSE uses the FUSE transport through <net/fuse.h>, the driver itself
lives in drivers/mtd/. In-band and out-of-band data of an operation
travel in the same request.
Opening /dev/muse needs CAP_SYS_ADMIN.
The MTD exists as long as the server keeps /dev/muse open. If the MTD
still has users when the server goes away, it is removed as soon as the
last user is gone. Until then no new user can get it and all operations
fail with -ENOTCONN.
Each MUSE MTD and each of its partitions has a muse_pid file in sysfs,
it shows the PID of the server.
Tested with mtdsim, including UBI on top of a simulated NAND, NOR and
RAM.
Signed-off-by: Richard Weinberger <richard@nod.at>
---
Documentation/ABI/testing/sysfs-class-mtd | 8 +
drivers/mtd/Kconfig | 15 +
drivers/mtd/Makefile | 1 +
drivers/mtd/muse.c | 995 ++++++++++++++++++++++
4 files changed, 1019 insertions(+)
create mode 100644 drivers/mtd/muse.c
diff --git a/Documentation/ABI/testing/sysfs-class-mtd b/Documentation/ABI/testing/sysfs-class-mtd
index f77fa4f6d465..bac61b8b6e03 100644
--- a/Documentation/ABI/testing/sysfs-class-mtd
+++ b/Documentation/ABI/testing/sysfs-class-mtd
@@ -240,3 +240,11 @@ Contact: linux-mtd@lists.infradead.org
Description:
Number of bytes available for a client to place data into
the out of band area.
+
+What: /sys/class/mtd/mtdX/muse_pid
+Date: December 2026
+KernelVersion: 7.5
+Contact: linux-mtd@lists.infradead.org
+Description:
+ If this MTD is a userspace driven MTD, muse_pid shows the PID
+ of the process behind it at creation time.
diff --git a/drivers/mtd/Kconfig b/drivers/mtd/Kconfig
index 0421c6208de7..52b161d2b1a4 100644
--- a/drivers/mtd/Kconfig
+++ b/drivers/mtd/Kconfig
@@ -193,6 +193,21 @@ config MTD_SWAP
The driver provides wear leveling by storing erase counter into the
OOB.
+config MTD_MUSE
+ tristate "MTD in Userspace support (MUSE)"
+ depends on FUSE_FS
+ select FUSE_HELPER
+ select MTD_CMDLINE_PARTS
+ help
+ MUSE allows an MTD to be implemented by a userspace program, like
+ FUSE does for filesystems and CUSE for character devices. The
+ program opens /dev/muse, describes the MTD and then serves read,
+ write and erase requests. This is useful for flash simulators such
+ as mtdsim.
+
+ If you want to develop or use a userspace MTD based on MUSE,
+ answer Y or M.
+
config MTD_PARTITIONED_MASTER
bool "Retain master device when partitioned"
default n
diff --git a/drivers/mtd/Makefile b/drivers/mtd/Makefile
index 7b6dd53e8150..2a00b89ea495 100644
--- a/drivers/mtd/Makefile
+++ b/drivers/mtd/Makefile
@@ -23,6 +23,7 @@ obj-$(CONFIG_SM_FTL) += sm_ftl.o
obj-$(CONFIG_MTD_OOPS) += mtdoops.o
obj-$(CONFIG_MTD_PSTORE) += mtdpstore.o
obj-$(CONFIG_MTD_SWAP) += mtdswap.o
+obj-$(CONFIG_MTD_MUSE) += muse.o
nftl-objs := nftlcore.o nftlmount.o
inftl-objs := inftlcore.o inftlmount.o
diff --git a/drivers/mtd/muse.c b/drivers/mtd/muse.c
new file mode 100644
index 000000000000..aa14f987da6f
--- /dev/null
+++ b/drivers/mtd/muse.c
@@ -0,0 +1,995 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * MUSE: MTD in userspace
+ * Copyright (C) 2026 sigma star gmbh
+ * Author: Richard Weinberger <richard@nod.at>
+ */
+
+#define pr_fmt(fmt) "MUSE: " fmt
+
+#include <linux/capability.h>
+#include <linux/fs.h>
+#include <linux/fuse.h>
+#include <linux/miscdevice.h>
+#include <linux/module.h>
+#include <linux/mtd/mtd.h>
+#include <linux/mtd/partitions.h>
+#include <linux/mutex.h>
+#include <linux/pid.h>
+#include <linux/slab.h>
+#include <linux/sysfs.h>
+#include <linux/workqueue.h>
+#include <net/fuse.h>
+
+/* The MUSE requests are part of the FUSE protocol since 7.48. */
+#define MUSE_MIN_MINOR 48
+
+/*
+ * A connection to a userspace server. Each connection implements a single
+ * (master) MTD.
+ */
+struct muse_conn {
+ struct fuse_conn *fc;
+ /* Maximum size of a MUSE_READ/MUSE_WRITE as negotiated with userspace. */
+ unsigned int max_read;
+ unsigned int max_write;
+ struct mtd_info mtd;
+ /* Parameters for the MTD partition parser, an mtdparts string. */
+ struct mtd_part_parser_data pd;
+ /* PID of the creating process. */
+ struct pid *creator;
+ /*
+ * Userspace is disconnected, the MTD is removed as soon as the last
+ * user vanishes.
+ */
+ bool want_exit;
+ /* Set by the init work, the exit work waits for it. */
+ bool mtd_registered;
+ /* Number of users of the MTD, including its partitions. */
+ unsigned int mtd_users;
+ struct work_struct mtd_init_work;
+ struct work_struct mtd_exit_work;
+ /* Synchronizes want_exit, mtd_users and MTD put/get. */
+ struct mutex ref_mutex;
+};
+
+/* Arguments of the MUSE_INIT request. */
+struct muse_init_args {
+ struct fuse_args args;
+ struct muse_init_in in;
+ struct muse_init_out out;
+ /* The description of the MTD, key-value pairs from userspace. */
+ char *info;
+ struct muse_conn *mc;
+};
+
+/* The MTD as desired by userspace, see MUSE_INIT_INFO_MAX in fuse.h. */
+struct muse_mtd_create_req {
+ const char *name;
+ u32 type;
+ u32 flags;
+ u64 size;
+ u32 writesize;
+ u32 writebufsize;
+ u32 erasesize;
+ u32 oobsize;
+ u32 oobavail;
+ u32 subpage_shift;
+ /* Without the leading MTD name. */
+ const char *mtdparts;
+};
+
+/* An MTD read or write, split into MUSE requests. */
+struct muse_io {
+ bool write;
+ /* Where the next request starts. */
+ loff_t pos;
+ /* The data which is left. */
+ u8 *datbuf;
+ size_t len;
+ u8 *oobbuf;
+ size_t ooblen;
+ /* Offset in the out-of-band area of the page pos belongs to. */
+ u32 ooboffs;
+ u32 oob_per_page;
+ /* A request covers at most that many pages. */
+ size_t max_pages;
+ /* MUSE_IO_* flags of all requests and of these with out-of-band data. */
+ int io_mode;
+ int oob_mode;
+ /* Number of requests with corrected and uncorrectable bit-flips. */
+ int bitflips;
+ int eccerrors;
+};
+
+/* MTD registration and removal sleep on userspace and on each other. */
+static struct workqueue_struct *muse_wq;
+
+/* Called from an RCU callback, see fuse_conn_new_dev(). */
+static void muse_conn_release(void *priv)
+{
+ struct muse_conn *mc = priv;
+
+ WARN_ON_ONCE(mc->mtd_users);
+ put_pid(mc->creator);
+ kfree(mc->pd.mtdparts);
+ kfree(mc->mtd.name);
+ kfree(mc);
+}
+
+static struct muse_conn *get_mc_from_mtd(struct mtd_info *mtd)
+{
+ struct mtd_info *master = mtd_get_master(mtd);
+
+ return master->priv;
+}
+
+static int muse_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
+{
+ struct muse_conn *mc = get_mc_from_mtd(mtd);
+ struct muse_erase_in inarg;
+ FUSE_ARGS(args);
+ ssize_t ret;
+
+ inarg.addr = instr->addr;
+ inarg.len = instr->len;
+
+ args.opcode = MUSE_ERASE;
+ args.nodeid = FUSE_ROOT_ID;
+ args.in_numargs = 1;
+ args.in_args[0].size = sizeof(inarg);
+ args.in_args[0].value = &inarg;
+
+ ret = fuse_conn_send(mc->fc, &args);
+ if (ret < 0)
+ return ret;
+
+ return 0;
+}
+
+static int muse_mtd_markbad(struct mtd_info *mtd, loff_t addr)
+{
+ struct muse_conn *mc = get_mc_from_mtd(mtd);
+ struct muse_markbad_in inarg;
+ FUSE_ARGS(args);
+ ssize_t ret;
+
+ inarg.addr = addr;
+
+ args.opcode = MUSE_MARKBAD;
+ args.nodeid = FUSE_ROOT_ID;
+ args.in_numargs = 1;
+ args.in_args[0].size = sizeof(inarg);
+ args.in_args[0].value = &inarg;
+
+ ret = fuse_conn_send(mc->fc, &args);
+ if (ret < 0)
+ return ret;
+
+ return 0;
+}
+
+static int muse_mtd_isbad(struct mtd_info *mtd, loff_t addr)
+{
+ struct muse_conn *mc = get_mc_from_mtd(mtd);
+ struct muse_isbad_in inarg;
+ struct muse_isbad_out outarg;
+ FUSE_ARGS(args);
+ ssize_t ret;
+
+ inarg.addr = addr;
+
+ args.opcode = MUSE_ISBAD;
+ args.nodeid = FUSE_ROOT_ID;
+ args.in_numargs = 1;
+ args.in_args[0].size = sizeof(inarg);
+ args.in_args[0].value = &inarg;
+ args.out_numargs = 1;
+ args.out_args[0].size = sizeof(outarg);
+ args.out_args[0].value = &outarg;
+
+ ret = fuse_conn_send(mc->fc, &args);
+ if (ret < 0)
+ return ret;
+
+ return !!outarg.result;
+}
+
+static void muse_mtd_sync(struct mtd_info *mtd)
+{
+ struct muse_conn *mc = get_mc_from_mtd(mtd);
+ FUSE_ARGS(args);
+
+ args.opcode = MUSE_SYNC;
+ args.nodeid = FUSE_ROOT_ID;
+ args.in_numargs = 0;
+
+ fuse_conn_send(mc->fc, &args);
+}
+
+static int muse_send_write(struct muse_conn *mc, const struct muse_io *io,
+ size_t len, size_t ooblen, int flags,
+ int *soft_error)
+{
+ struct muse_write_out out = {};
+ struct muse_write_in in = {};
+ FUSE_ARGS(args);
+ ssize_t ret;
+
+ in.addr = io->pos;
+ in.len = len;
+ in.ooblen = ooblen;
+ in.ooboffs = io->ooboffs;
+ in.flags = flags;
+
+ args.opcode = MUSE_WRITE;
+ args.nodeid = FUSE_ROOT_ID;
+ args.in_args[0].size = sizeof(in);
+ args.in_args[0].value = ∈
+ args.in_numargs = 1;
+ if (len) {
+ args.in_args[args.in_numargs].size = len;
+ args.in_args[args.in_numargs].value = io->datbuf;
+ args.in_numargs++;
+ }
+ if (ooblen) {
+ args.in_args[args.in_numargs].size = ooblen;
+ args.in_args[args.in_numargs].value = io->oobbuf;
+ args.in_numargs++;
+ }
+ args.out_numargs = 1;
+ args.out_args[0].size = sizeof(out);
+ args.out_args[0].value = &out;
+
+ ret = fuse_conn_send(mc->fc, &args);
+ if (ret < 0)
+ return ret;
+
+ *soft_error = out.soft_error;
+
+ /* No short writes are allowed in MTD. */
+ if (!out.soft_error && (out.len != len || out.ooblen != ooblen))
+ return -EIO;
+
+ return 0;
+}
+
+static int muse_send_read(struct muse_conn *mc, const struct muse_io *io,
+ size_t len, size_t ooblen, int flags,
+ int *soft_error)
+{
+ struct muse_read_out out = {};
+ struct muse_read_in in = {};
+ FUSE_ARGS(args);
+ ssize_t ret;
+
+ in.addr = io->pos;
+ in.len = len;
+ in.ooblen = ooblen;
+ in.ooboffs = io->ooboffs;
+ in.flags = flags;
+
+ args.opcode = MUSE_READ;
+ args.nodeid = FUSE_ROOT_ID;
+ args.in_numargs = 1;
+ args.in_args[0].size = sizeof(in);
+ args.in_args[0].value = ∈
+
+ /*
+ * FUSE supports only one variable sized output argument. We don't
+ * need any since no short reads are allowed in MTD. Userspace has
+ * to reply with exactly len bytes of in-band data followed by exactly
+ * ooblen bytes of out-of-band data.
+ */
+ args.out_args[0].size = sizeof(out);
+ args.out_args[0].value = &out;
+ args.out_numargs = 1;
+ if (len) {
+ args.out_args[args.out_numargs].size = len;
+ args.out_args[args.out_numargs].value = io->datbuf;
+ args.out_numargs++;
+ }
+ if (ooblen) {
+ args.out_args[args.out_numargs].size = ooblen;
+ args.out_args[args.out_numargs].value = io->oobbuf;
+ args.out_numargs++;
+ }
+
+ ret = fuse_conn_send(mc->fc, &args);
+ if (ret < 0)
+ return ret;
+
+ *soft_error = out.soft_error;
+
+ /*
+ * No short reads are allowed in MTD, also not in case of
+ * -EUCLEAN and -EBADMSG. Any other injected error fails the
+ * operation anyway.
+ */
+ if (*soft_error && *soft_error != -EUCLEAN && *soft_error != -EBADMSG)
+ return 0;
+
+ if (out.len != len || out.ooblen != ooblen)
+ return -EIO;
+
+ return 0;
+}
+
+static int muse_io_init(struct muse_conn *mc, struct mtd_oob_ops *ops,
+ struct muse_io *io)
+{
+ size_t max_io = io->write ? mc->max_write : mc->max_read;
+ struct mtd_info *mtd = &mc->mtd;
+
+ if (ops->mode == MTD_OPS_RAW)
+ io->io_mode = MUSE_IO_RAW;
+
+ /* Without out-of-band data the kind of it does not matter. */
+ if (io->ooblen) {
+ io->oob_per_page = mtd_oobavail(mtd, ops);
+ if (!io->oob_per_page || io->ooboffs >= io->oob_per_page)
+ return -EINVAL;
+
+ if (ops->mode == MTD_OPS_PLACE_OOB || ops->mode == MTD_OPS_RAW)
+ io->oob_mode = MUSE_IO_OOB_PLACE;
+ else if (ops->mode == MTD_OPS_AUTO_OOB)
+ io->oob_mode = MUSE_IO_OOB_AUTO;
+ else
+ return -EOPNOTSUPP;
+ }
+
+ /*
+ * A full page, including its out-of-band data if requested, needs to
+ * fit into a single FUSE request.
+ */
+ io->max_pages = max_io / (mtd->writesize + io->oob_per_page);
+ if (!io->max_pages)
+ return -ENOBUFS;
+
+ return 0;
+}
+
+/*
+ * Userspace can inject an error into a request it served. -EUCLEAN and
+ * -EBADMSG are special for reads: MTD expects a device to return all
+ * requested data even if there are (un)correctable bit-flips. The upper
+ * layer, such as UBI, has to deal with them. Anything else is fatal.
+ */
+static int muse_io_soft_error(struct muse_io *io, int soft_error)
+{
+ if (soft_error > 0)
+ return -EIO;
+
+ if (!io->write && soft_error == -EUCLEAN)
+ io->bitflips++;
+ else if (!io->write && soft_error == -EBADMSG)
+ io->eccerrors++;
+ else
+ return soft_error;
+
+ return 0;
+}
+
+/*
+ * Sends one request. It carries the in-band data of a number of pages
+ * together with the out-of-band data of the very same pages. If there is more
+ * out-of-band data than in-band data, the remaining out-of-band data is
+ * processed by out-of-band only requests.
+ */
+static int muse_io_one(struct muse_conn *mc, struct muse_io *io,
+ struct mtd_oob_ops *ops)
+{
+ struct mtd_info *mtd = &mc->mtd;
+ u32 page_offs = mtd_mod_by_ws(io->pos, mtd);
+ size_t nbytes = 0, noob = 0, npages = io->max_pages;
+ int flags = io->io_mode;
+ int soft_error = 0;
+ int ret;
+
+ if (io->len) {
+ nbytes = min_t(size_t, io->len,
+ io->max_pages * mtd->writesize - page_offs);
+ npages = DIV_ROUND_UP(page_offs + nbytes, mtd->writesize);
+ flags |= MUSE_IO_INBAND;
+ }
+ if (io->ooblen) {
+ noob = min_t(size_t, io->ooblen,
+ npages * io->oob_per_page - io->ooboffs);
+ flags |= io->oob_mode;
+ }
+
+ if (io->write)
+ ret = muse_send_write(mc, io, nbytes, noob, flags, &soft_error);
+ else
+ ret = muse_send_read(mc, io, nbytes, noob, flags, &soft_error);
+ if (!ret)
+ ret = muse_io_soft_error(io, soft_error);
+ if (ret)
+ return ret;
+
+ io->len -= nbytes;
+ io->datbuf += nbytes;
+ ops->retlen += nbytes;
+
+ io->ooblen -= noob;
+ io->oobbuf += noob;
+ ops->oobretlen += noob;
+
+ /* The next request starts at the page right after the ones of this one. */
+ io->pos += (loff_t)npages * mtd->writesize - page_offs;
+ io->ooboffs = 0;
+
+ return 0;
+}
+
+/*
+ * Processes a read from or a write to the MTD. In-band and out-of-band data
+ * can be processed in the same operation. All IO is synchronous and the
+ * buffers of @ops have to be kernel memory. An operation which does not fit
+ * into a single FUSE request is split at MTD page boundaries.
+ */
+static int muse_do_io(struct muse_conn *mc, struct mtd_oob_ops *ops,
+ loff_t pos, bool write)
+{
+ struct mtd_info *mtd = &mc->mtd;
+ struct muse_io io = {
+ .write = write,
+ .pos = pos,
+ .datbuf = ops->datbuf,
+ .len = ops->len,
+ .oobbuf = ops->oobbuf,
+ .ooblen = ops->ooblen,
+ .ooboffs = ops->ooboffs,
+ };
+ int ret;
+
+ ops->retlen = 0;
+ ops->oobretlen = 0;
+
+ ret = muse_io_init(mc, ops, &io);
+ while (!ret && (io.len || io.ooblen))
+ ret = muse_io_one(mc, &io, ops);
+
+ /* A fatal error overrides -EUCLEAN and -EBADMSG. */
+ if (ret)
+ return ret;
+ if (io.eccerrors) {
+ mtd->ecc_stats.failed += io.eccerrors;
+ return -EBADMSG;
+ }
+ if (io.bitflips) {
+ mtd->ecc_stats.corrected += io.bitflips;
+ return -EUCLEAN;
+ }
+
+ return 0;
+}
+
+static int muse_mtd_read_oob(struct mtd_info *mtd, loff_t from,
+ struct mtd_oob_ops *ops)
+{
+ struct muse_conn *mc = get_mc_from_mtd(mtd);
+
+ return muse_do_io(mc, ops, from, false);
+}
+
+static int muse_mtd_write_oob(struct mtd_info *mtd, loff_t to,
+ struct mtd_oob_ops *ops)
+{
+ struct muse_conn *mc = get_mc_from_mtd(mtd);
+
+ return muse_do_io(mc, ops, to, true);
+}
+
+static int muse_mtd_get_device(struct mtd_info *mtd)
+{
+ struct muse_conn *mc = get_mc_from_mtd(mtd);
+ int ret = 0;
+
+ mutex_lock(&mc->ref_mutex);
+
+ /* Refuse a new reference if userspace is no longer connected. */
+ if (mc->want_exit) {
+ ret = -ENODEV;
+ goto out;
+ }
+
+ fuse_conn_get(mc->fc);
+ mc->mtd_users++;
+
+out:
+ mutex_unlock(&mc->ref_mutex);
+ return ret;
+}
+
+static ssize_t muse_pid_show(struct device *dev, struct device_attribute *attr,
+ char *buf)
+{
+ struct muse_conn *mc = get_mc_from_mtd(dev_get_drvdata(dev));
+
+ return sysfs_emit(buf, "%d\n", pid_vnr(mc->creator));
+}
+
+static DEVICE_ATTR_RO(muse_pid);
+
+/*
+ * Adds the sysfs file to the master device, if it is visible, and to its
+ * partitions. There is no need to remove the files, they vanish together
+ * with their device.
+ */
+static int install_sysfs_attrs(struct mtd_info *mtd)
+{
+ struct mtd_info *child;
+ int ret = 0;
+
+ mutex_lock(&mtd->master.partitions_lock);
+
+ if (device_is_registered(&mtd->dev))
+ ret = sysfs_create_file(&mtd->dev.kobj, &dev_attr_muse_pid.attr);
+
+ list_for_each_entry(child, &mtd->partitions, part.node) {
+ if (ret)
+ break;
+ ret = sysfs_create_file(&child->dev.kobj, &dev_attr_muse_pid.attr);
+ }
+
+ mutex_unlock(&mtd->master.partitions_lock);
+ return ret;
+}
+
+static void muse_exit_mtd_work(struct work_struct *work)
+{
+ struct muse_conn *mc = container_of(work, struct muse_conn, mtd_exit_work);
+
+ /* The init work might be still about to register the MTD. */
+ flush_work(&mc->mtd_init_work);
+
+ if (mc->mtd_registered)
+ mtd_device_unregister(&mc->mtd);
+
+ fuse_conn_put(mc->fc);
+}
+
+/*
+ * MTD deregistration has to happen asynchronously. It will grab
+ * mtd_table_mutex but depending on the context we hold it already or hold
+ * mc->ref_mutex. The locking order is mtd_table_mutex > mc->ref_mutex.
+ */
+static void muse_remove_mtd_async(struct muse_conn *mc)
+{
+ queue_work(muse_wq, &mc->mtd_exit_work);
+}
+
+static void muse_mtd_put_device(struct mtd_info *mtd)
+{
+ struct muse_conn *mc = get_mc_from_mtd(mtd);
+
+ mutex_lock(&mc->ref_mutex);
+
+ mc->mtd_users--;
+
+ /*
+ * If this was the last reference on the MTD and userspace is gone,
+ * remove it now. The reference on the FUSE connection goes to the exit
+ * work then.
+ */
+ if (mc->want_exit && mc->mtd_users == 0)
+ muse_remove_mtd_async(mc);
+ else
+ fuse_conn_put(mc->fc);
+
+ mutex_unlock(&mc->ref_mutex);
+}
+
+static int muse_verify_mtdreq(struct muse_mtd_create_req *req)
+{
+ u64 tmp;
+
+ if (!req->name)
+ return -EINVAL;
+
+ if (!req->size || !req->writesize || !req->erasesize)
+ return -EINVAL;
+
+ tmp = req->size;
+ if (do_div(tmp, req->erasesize))
+ return -EINVAL;
+
+ if (req->erasesize % req->writesize)
+ return -EINVAL;
+
+ if (req->writebufsize % req->writesize)
+ return -EINVAL;
+
+ if (req->oobsize < req->oobavail || req->oobsize >= req->writesize)
+ return -EINVAL;
+
+ if (req->flags & ~(MTD_WRITEABLE | MTD_BIT_WRITEABLE | MTD_NO_ERASE))
+ return -EINVAL;
+
+ if (req->subpage_shift > 2)
+ return -EINVAL;
+
+ switch (req->type) {
+ case MTD_RAM:
+ case MTD_ROM:
+ case MTD_NORFLASH:
+ case MTD_NANDFLASH:
+ case MTD_DATAFLASH:
+ case MTD_MLCNANDFLASH:
+ return 0;
+ default:
+ return -EINVAL;
+ }
+}
+
+/*
+ * The keys of the description of the MTD, see MUSE_INIT_INFO_MAX in fuse.h.
+ * Numbers are decimal. An unknown key fails MUSE_INIT.
+ *
+ * NAME: name of the MTD, required
+ * TYPE: type of the MTD, required. One of MTD_RAM, MTD_ROM,
+ * MTD_NORFLASH, MTD_NANDFLASH, MTD_DATAFLASH and
+ * MTD_MLCNANDFLASH, see <mtd/mtd-abi.h>
+ * FLAGS: flags of the MTD, any of MTD_WRITEABLE, MTD_BIT_WRITEABLE
+ * and MTD_NO_ERASE
+ * SIZE: size of the MTD in bytes, required, a multiple of ERASESIZE
+ * WRITESIZE: minimal writable unit in bytes, required. The page size of
+ * a NAND, 1 for a NOR
+ * ERASESIZE: size of an erase block in bytes, required, a multiple of
+ * WRITESIZE
+ * WRITEBUFSIZE: size of the write buffer of the MTD in bytes, a multiple of
+ * WRITESIZE. WRITESIZE if not given
+ * OOBSIZE: out-of-band bytes per page, less than WRITESIZE
+ * OOBAVAIL: out-of-band bytes per page which are free to use, not more
+ * than OOBSIZE
+ * SUBPAGESHIFT: a page consists of 1 << SUBPAGESHIFT sub-pages, at most 2
+ * PARTSCMDLINE: partitions of the MTD in the syntax of the mtdparts= kernel
+ * parameter, without the leading "<mtd-id>:"
+ */
+static int muse_parse_key(struct muse_mtd_create_req *req, const char *key,
+ const char *val)
+{
+ if (!strcmp(key, "NAME")) {
+ req->name = val;
+ return 0;
+ }
+ if (!strcmp(key, "PARTSCMDLINE")) {
+ req->mtdparts = val;
+ return 0;
+ }
+ if (!strcmp(key, "SIZE"))
+ return kstrtou64(val, 10, &req->size);
+ if (!strcmp(key, "TYPE"))
+ return kstrtou32(val, 10, &req->type);
+ if (!strcmp(key, "FLAGS"))
+ return kstrtou32(val, 10, &req->flags);
+ if (!strcmp(key, "WRITESIZE"))
+ return kstrtou32(val, 10, &req->writesize);
+ if (!strcmp(key, "WRITEBUFSIZE"))
+ return kstrtou32(val, 10, &req->writebufsize);
+ if (!strcmp(key, "ERASESIZE"))
+ return kstrtou32(val, 10, &req->erasesize);
+ if (!strcmp(key, "OOBSIZE"))
+ return kstrtou32(val, 10, &req->oobsize);
+ if (!strcmp(key, "OOBAVAIL"))
+ return kstrtou32(val, 10, &req->oobavail);
+ if (!strcmp(key, "SUBPAGESHIFT"))
+ return kstrtou32(val, 10, &req->subpage_shift);
+
+ pr_warn_ratelimited("Unknown MTD param \"%s\"\n", key);
+ return -EINVAL;
+}
+
+/* The strings allocated here are freed together with the connection. */
+static int muse_fill_mtd(struct muse_conn *mc,
+ const struct muse_mtd_create_req *req)
+{
+ struct mtd_info *mtd = &mc->mtd;
+
+ mtd->name = kstrdup(req->name, GFP_KERNEL);
+ if (!mtd->name)
+ return -ENOMEM;
+
+ if (req->mtdparts && *req->mtdparts) {
+ mc->pd.mtdparts = kasprintf(GFP_KERNEL, "%s:%s", req->name,
+ req->mtdparts);
+ if (!mc->pd.mtdparts)
+ return -ENOMEM;
+ }
+
+ mtd->size = req->size;
+ mtd->erasesize = req->erasesize;
+ mtd->writesize = req->writesize;
+ mtd->writebufsize = req->writebufsize ?: req->writesize;
+ mtd->oobsize = req->oobsize;
+ mtd->oobavail = req->oobavail;
+ mtd->subpage_sft = req->subpage_shift;
+ mtd->type = req->type;
+ mtd->flags = MTD_MUSE | req->flags;
+
+ return 0;
+}
+
+static int muse_parse_mtdreq(char *p, size_t len, struct muse_conn *mc)
+{
+ struct muse_mtd_create_req req = {};
+ char *end = p + len;
+ char *key, *val;
+ int ret;
+
+ for (;;) {
+ ret = fuse_kv_parse_one(&p, end, &key, &val);
+ if (ret < 0)
+ return ret;
+ if (!ret)
+ break;
+
+ ret = muse_parse_key(&req, key, val);
+ if (ret)
+ return ret;
+ }
+
+ ret = muse_verify_mtdreq(&req);
+ if (ret)
+ return ret;
+
+ return muse_fill_mtd(mc, &req);
+}
+
+static void muse_init_mtd_work(struct work_struct *work)
+{
+ struct muse_conn *mc = container_of(work, struct muse_conn, mtd_init_work);
+ static const char * const part_probe_types[] = { "cmdlinepart", NULL };
+ int ret;
+
+ ret = mtd_device_parse_register(&mc->mtd, part_probe_types, &mc->pd,
+ NULL, 0);
+ if (!ret) {
+ mc->mtd_registered = true;
+ ret = install_sysfs_attrs(&mc->mtd);
+ }
+
+ /* The MTD, if any, is removed as soon as userspace closes the device. */
+ if (ret)
+ fuse_conn_abort(mc->fc);
+
+ fuse_conn_put(mc->fc);
+}
+
+static void muse_setup_mtd_ops(struct muse_conn *mc)
+{
+ struct mtd_info *mtd = &mc->mtd;
+
+ mtd->_erase = muse_mtd_erase;
+ mtd->_sync = muse_mtd_sync;
+ mtd->_read_oob = muse_mtd_read_oob;
+ mtd->_write_oob = muse_mtd_write_oob;
+ mtd->_get_device = muse_mtd_get_device;
+ mtd->_put_device = muse_mtd_put_device;
+
+ /*
+ * Bad blocks make only sense on NAND devices. As soon as _block_isbad
+ * is set, upper layers such as UBI expect a working _block_isbad, so
+ * userspace has to implement MUSE_ISBAD.
+ */
+ if (mtd_type_is_nand(mtd)) {
+ mtd->_block_isbad = muse_mtd_isbad;
+ mtd->_block_markbad = muse_mtd_markbad;
+ }
+
+ mtd->priv = mc;
+ mtd->owner = THIS_MODULE;
+}
+
+static void muse_process_init_reply(struct fuse_args *args, int error)
+{
+ struct muse_init_args *mia = container_of(args, struct muse_init_args, args);
+ struct muse_conn *mc = mia->mc;
+ struct muse_init_out *arg = &mia->out;
+ struct mtd_info *mtd = &mc->mtd;
+
+ if (error || arg->fuse_major != FUSE_KERNEL_VERSION ||
+ arg->fuse_minor < MUSE_MIN_MINOR)
+ goto abort;
+
+ mc->max_read = max_t(unsigned int, arg->max_read, 4096);
+ mc->max_write = max_t(unsigned int, arg->max_write, 4096);
+
+ if (muse_parse_mtdreq(mia->info, args->out_args[1].size, mc))
+ goto abort;
+
+ /*
+ * We want one READ/WRITE op per MTD io. So the MTD pagesize plus
+ * the out-of-band area needs to fit into max_write/max_read.
+ */
+ if (mc->max_write < mtd->writebufsize + mtd->oobsize ||
+ mc->max_read < mtd->writebufsize + mtd->oobsize)
+ goto abort;
+
+ muse_setup_mtd_ops(mc);
+ mc->creator = get_pid(task_tgid(current));
+
+ kfree(mia->info);
+ kfree(mia);
+
+ /*
+ * MTD can access the device while probing it, e.g. scanning for bad
+ * blocks or custom partition parsers. So we need to do the final step
+ * in a different process context. Otherwise we will lockup here if the
+ * userspace side of this MUSE MTD is single threaded.
+ */
+ fuse_conn_get(mc->fc);
+ queue_work(muse_wq, &mc->mtd_init_work);
+ return;
+
+abort:
+ kfree(mia->info);
+ kfree(mia);
+ fuse_conn_abort(mc->fc);
+}
+
+static int muse_send_init(struct muse_conn *mc)
+{
+ struct muse_init_args *mia;
+ struct fuse_args *args;
+ int ret = -ENOMEM;
+
+ mia = kzalloc_obj(*mia);
+ if (!mia)
+ goto err;
+
+ mia->info = kzalloc(MUSE_INIT_INFO_MAX, GFP_KERNEL);
+ if (!mia->info)
+ goto err_mia;
+
+ args = &mia->args;
+ mia->mc = mc;
+ mia->in.fuse_major = FUSE_KERNEL_VERSION;
+ mia->in.fuse_minor = FUSE_KERNEL_MINOR_VERSION;
+ args->opcode = MUSE_INIT;
+ args->in_numargs = 1;
+ args->in_args[0].size = sizeof(mia->in);
+ args->in_args[0].value = &mia->in;
+ args->out_numargs = 2;
+ args->out_args[0].size = sizeof(mia->out);
+ args->out_args[0].value = &mia->out;
+ args->out_args[1].size = MUSE_INIT_INFO_MAX;
+ args->out_args[1].value = mia->info;
+ args->out_argvar = true;
+ args->end = muse_process_init_reply;
+
+ ret = fuse_conn_send_bg(mc->fc, args, GFP_KERNEL);
+ if (ret)
+ goto err_info;
+
+ return 0;
+
+err_info:
+ kfree(mia->info);
+err_mia:
+ kfree(mia);
+err:
+ return ret;
+}
+
+static int muse_ctrl_open(struct inode *inode, struct file *file)
+{
+ struct muse_conn *mc;
+ struct fuse_dev *fud;
+ int ret;
+
+ /* Paranoia check. */
+ if (!capable(CAP_SYS_ADMIN))
+ return -EPERM;
+
+ mc = kzalloc_obj(*mc);
+ if (!mc)
+ return -ENOMEM;
+
+ mutex_init(&mc->ref_mutex);
+ INIT_WORK(&mc->mtd_init_work, muse_init_mtd_work);
+ INIT_WORK(&mc->mtd_exit_work, muse_exit_mtd_work);
+
+ mc->fc = fuse_conn_new_dev(&init_user_ns, muse_conn_release, mc);
+ if (!mc->fc) {
+ kfree(mc);
+ return -ENOMEM;
+ }
+
+ /*
+ * From now on mc is freed via muse_conn_release() as soon as the last
+ * reference on the connection is gone.
+ */
+ fud = fuse_conn_install_dev(mc->fc);
+ if (!fud) {
+ ret = -ENOMEM;
+ goto err_conn;
+ }
+
+ ret = muse_send_init(mc);
+ if (ret)
+ goto err_dev;
+
+ file->private_data = fud;
+
+ return 0;
+
+err_dev:
+ fuse_dev_put(fud);
+err_conn:
+ fuse_conn_put(mc->fc);
+ return ret;
+}
+
+static int muse_ctrl_release(struct inode *inode, struct file *file)
+{
+ struct muse_conn *mc = fuse_conn_priv(fuse_conn_from_file(file));
+
+ mutex_lock(&mc->ref_mutex);
+ /* Make sure that nobody can gain a new reference on our MTD. */
+ mc->want_exit = true;
+
+ /*
+ * If the MTD has no users, remove it right now, keep it otherwise
+ * until the last user is gone. During this phase all operations will
+ * fail with -ENOTCONN.
+ */
+ if (mc->mtd_users == 0)
+ muse_remove_mtd_async(mc);
+ else
+ fuse_conn_put(mc->fc);
+ mutex_unlock(&mc->ref_mutex);
+
+ return fuse_dev_release(inode, file);
+}
+
+static struct file_operations muse_ctrl_fops;
+
+static struct miscdevice muse_ctrl_dev = {
+ .minor = MISC_DYNAMIC_MINOR,
+ .name = "muse",
+ .fops = &muse_ctrl_fops,
+};
+
+static int __init muse_init(void)
+{
+ int ret;
+
+ muse_wq = alloc_workqueue("muse", WQ_UNBOUND, 0);
+ if (!muse_wq)
+ return -ENOMEM;
+
+ /* Inherit from fuse_dev_operations and override open() plus release(). */
+ muse_ctrl_fops = fuse_dev_operations;
+ muse_ctrl_fops.owner = THIS_MODULE;
+ muse_ctrl_fops.open = muse_ctrl_open;
+ muse_ctrl_fops.release = muse_ctrl_release;
+ /* Like CUSE, MUSE is not prepared for FUSE_DEV_IOC_CLONE */
+ muse_ctrl_fops.unlocked_ioctl = NULL;
+
+ ret = misc_register(&muse_ctrl_dev);
+ if (ret)
+ destroy_workqueue(muse_wq);
+
+ return ret;
+}
+
+static void __exit muse_exit(void)
+{
+ misc_deregister(&muse_ctrl_dev);
+ /* An exit work can be still pending, nothing holds the module for it. */
+ destroy_workqueue(muse_wq);
+ /*
+ * Wait for pending call_rcu() callbacks that call back into
+ * this module (muse_conn_release).
+ */
+ rcu_barrier();
+}
+
+module_init(muse_init);
+module_exit(muse_exit);
+
+MODULE_AUTHOR("Richard Weinberger <richard@nod.at>");
+MODULE_DESCRIPTION("MTD in userspace");
+MODULE_LICENSE("GPL");
--
2.55.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 13+ messages in thread* Re: [PATCH 09/10] mtd: Implement MUSE - MTD in userspace
2026-11-30 8:26 ` [PATCH 09/10] mtd: Implement MUSE - MTD in userspace Richard Weinberger (sigma star gmbh)
@ 2026-11-30 9:14 ` sashiko-bot
0 siblings, 0 replies; 13+ messages in thread
From: sashiko-bot @ 2026-11-30 9:14 UTC (permalink / raw)
To: Richard Weinberger (sigma star gmbh), linux-mtd
Cc: horms, pabeni, kuba, edumazet, davem, miklos, vigneshr,
miquel.raynal, david.oberhollenzer, upstream+linux,
Richard Weinberger
Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [Medium] mtd: muse: 32-bit overflow in muse_process_init_reply() and
muse_io_init()
- [Medium] mtd: muse: missing writesize check for subpage_shift in
muse_verify_mtdreq()
- [Medium] mtd: muse: missing ops->stats update and dropped bitflips in
muse_do_io()
- [Medium] mtd: muse: missing mtd->ecc_stats.badblocks increment in
muse_mtd_markbad()
- [Medium] mtd: muse: module unload race between muse_mtd_put_device()
and muse_exit()
- [Medium] mtd: muse: integer overflows from unbounded I/O limits and
MTD size
- [Low] mtd: muse: data race on mtd->ecc_stats in muse_do_io()
--
commit fab9511930c9c6fbb7cc8a643d0853ebd17716d5
Author: Richard Weinberger <richard@nod.at>
mtd: Implement MUSE - MTD in userspace
This commit adds the MUSE driver (/dev/muse), which allows implementing an
MTD device in userspace over the FUSE transport.
> diff --git a/drivers/mtd/muse.c b/drivers/mtd/muse.c
> new file mode 100644
> index 0000000000000..aa14f987da6f3
> --- /dev/null
> +++ b/drivers/mtd/muse.c
> @@ -0,0 +1,995 @@
[ ... ]
> +static int muse_mtd_markbad(struct mtd_info *mtd, loff_t addr)
> +{
> + struct muse_conn *mc = get_mc_from_mtd(mtd);
> + struct muse_markbad_in inarg;
> + FUSE_ARGS(args);
> + ssize_t ret;
> +
> + inarg.addr = addr;
> +
> + args.opcode = MUSE_MARKBAD;
> + args.nodeid = FUSE_ROOT_ID;
> + args.in_numargs = 1;
> + args.in_args[0].size = sizeof(inarg);
> + args.in_args[0].value = &inarg;
> +
> + ret = fuse_conn_send(mc->fc, &args);
> + if (ret < 0)
> + return ret;
> +
> + return 0;
> +}
[Severity: Medium]
Should muse_mtd_markbad() increment mtd->ecc_stats.badblocks on success?
mtd_block_markbad() only increments ecc_stats.badblocks on partitions with
a non-NULL parent, relying on master->_block_markbad() to update the master
device's badblock count, so sysfs bad_blocks and ECCGETSTATS on the master
MTD remain stale when blocks are marked bad.
[ ... ]
> +static int muse_io_init(struct muse_conn *mc, struct mtd_oob_ops *ops,
> + struct muse_io *io)
> +{
> + size_t max_io = io->write ? mc->max_write : mc->max_read;
> + struct mtd_info *mtd = &mc->mtd;
> +
> + if (ops->mode == MTD_OPS_RAW)
> + io->io_mode = MUSE_IO_RAW;
> +
> + /* Without out-of-band data the kind of it does not matter. */
> + if (io->ooblen) {
> + io->oob_per_page = mtd_oobavail(mtd, ops);
> + if (!io->oob_per_page || io->ooboffs >= io->oob_per_page)
> + return -EINVAL;
> +
> + if (ops->mode == MTD_OPS_PLACE_OOB || ops->mode == MTD_OPS_RAW)
> + io->oob_mode = MUSE_IO_OOB_PLACE;
> + else if (ops->mode == MTD_OPS_AUTO_OOB)
> + io->oob_mode = MUSE_IO_OOB_AUTO;
> + else
> + return -EOPNOTSUPP;
> + }
> +
> + /*
> + * A full page, including its out-of-band data if requested, needs to
> + * fit into a single FUSE request.
> + */
> + io->max_pages = max_io / (mtd->writesize + io->oob_per_page);
> + if (!io->max_pages)
> + return -ENOBUFS;
> +
> + return 0;
> +}
[Severity: Medium]
Can mtd->writesize + io->oob_per_page wrap around in 32-bit unsigned
arithmetic and divide by zero?
Both mtd->writesize and io->oob_per_page are 32-bit unsigned integers, and
muse_verify_mtdreq() only checks req->oobsize < req->writesize. If
userspace replies to MUSE_INIT with sizes whose sum wraps modulo 2^32
(such as writesize = 0x80000001 and oobsize = oobavail = 0x7fffffff),
mtd->writebufsize + mtd->oobsize in muse_process_init_reply() also wraps
in 32-bit arithmetic and bypasses the max_read/max_write check, causing an
OOB read or write to divide by zero here.
[ ... ]
> +static int muse_do_io(struct muse_conn *mc, struct mtd_oob_ops *ops,
> + loff_t pos, bool write)
> +{
> + struct mtd_info *mtd = &mc->mtd;
> + struct muse_io io = {
> + .write = write,
> + .pos = pos,
> + .datbuf = ops->datbuf,
> + .len = ops->len,
> + .oobbuf = ops->oobbuf,
> + .ooblen = ops->ooblen,
> + .ooboffs = ops->ooboffs,
> + };
> + int ret;
> +
> + ops->retlen = 0;
> + ops->oobretlen = 0;
> +
> + ret = muse_io_init(mc, ops, &io);
> + while (!ret && (io.len || io.ooblen))
> + ret = muse_io_one(mc, &io, ops);
> +
> + /* A fatal error overrides -EUCLEAN and -EBADMSG. */
> + if (ret)
> + return ret;
> + if (io.eccerrors) {
> + mtd->ecc_stats.failed += io.eccerrors;
> + return -EBADMSG;
> + }
> + if (io.bitflips) {
> + mtd->ecc_stats.corrected += io.bitflips;
> + return -EUCLEAN;
> + }
> +
> + return 0;
> +}
[Severity: Medium]
Does muse_do_io() fail to update ops->stats and drop io.bitflips when
io.eccerrors is non-zero?
If a multi-request read sees both -EUCLEAN and -EBADMSG across its
sub-requests, returning early on io.eccerrors skips adding io.bitflips to
mtd->ecc_stats.corrected. In addition, ops->stats is never populated when
non-NULL, so mtdchar_read_ioctl() (MEMREAD) always reports zero ECC stats
to userspace and loses -EBADMSG or -EUCLEAN on multi-chunk reads whenever
the final chunk succeeds.
[Severity: Low]
Can concurrent reads cause a data race on mtd->ecc_stats.failed and
mtd->ecc_stats.corrected? Unlike hardware NAND drivers that serialize
page reads under a device lock, muse_do_io() runs FUSE read requests
concurrently and updates the shared master mtd->ecc_stats without
synchronization, which can lose counter updates and skew partition ECC
deltas in mtd_update_ecc_stats().
[ ... ]
> +static void muse_mtd_put_device(struct mtd_info *mtd)
> +{
> + struct muse_conn *mc = get_mc_from_mtd(mtd);
> +
> + mutex_lock(&mc->ref_mutex);
> +
> + mc->mtd_users--;
> +
> + /*
> + * If this was the last reference on the MTD and userspace is gone,
> + * remove it now. The reference on the FUSE connection goes to the exit
> + * work then.
> + */
> + if (mc->want_exit && mc->mtd_users == 0)
> + muse_remove_mtd_async(mc);
> + else
> + fuse_conn_put(mc->fc);
> +
> + mutex_unlock(&mc->ref_mutex);
> +}
[Severity: Medium]
Can module unload race with muse_mtd_put_device() when the last MTD user
exits after /dev/muse has already been closed?
When /dev/muse is closed while mc->mtd_users > 0, muse_ctrl_release() sets
mc->want_exit = true without queuing mtd_exit_work, and __fput() drops the
file's reference on THIS_MODULE. When the last MTD user later closes the
MTD device, __put_mtd_device() drops the final module reference before
calling master->_put_device():
drivers/mtd/mtdcore.c:__put_mtd_device() {
...
module_put(master->owner);
/* must be the last as master can be freed in the _put_device */
if (master->_put_device)
master->_put_device(master);
}
That allows a concurrent module unload to destroy muse_wq and unmap the
module before muse_mtd_put_device() queues mtd_exit_work:
CPU0 CPU1
__put_mtd_device()
module_put(master->owner)
delete_module()
muse_exit()
destroy_workqueue(muse_wq)
rcu_barrier()
muse_mtd_put_device()
muse_remove_mtd_async(mc)
queue_work(muse_wq, ...) <-- use-after-free
> +static int muse_verify_mtdreq(struct muse_mtd_create_req *req)
> +{
> + u64 tmp;
> +
> + if (!req->name)
> + return -EINVAL;
> +
> + if (!req->size || !req->writesize || !req->erasesize)
> + return -EINVAL;
> +
> + tmp = req->size;
> + if (do_div(tmp, req->erasesize))
> + return -EINVAL;
> +
> + if (req->erasesize % req->writesize)
> + return -EINVAL;
> +
> + if (req->writebufsize % req->writesize)
> + return -EINVAL;
> +
> + if (req->oobsize < req->oobavail || req->oobsize >= req->writesize)
> + return -EINVAL;
> +
> + if (req->flags & ~(MTD_WRITEABLE | MTD_BIT_WRITEABLE | MTD_NO_ERASE))
> + return -EINVAL;
> +
> + if (req->subpage_shift > 2)
> + return -EINVAL;
[Severity: Medium]
Can req->writesize be smaller than (or not divisible by)
1U << req->subpage_shift?
If userspace sets WRITESIZE=1 or 2 with SUBPAGESHIFT=2,
mtd->writesize >> mtd->subpage_sft evaluates to 0. When UBI attaches to
the device, io_init() in drivers/mtd/ubi/build.c sets
ubi->hdrs_min_io_size = 0 and divides by zero in
ubi->min_io_size % ubi->hdrs_min_io_size.
[ ... ]
> +static void muse_process_init_reply(struct fuse_args *args, int error)
> +{
> + struct muse_init_args *mia = container_of(args, struct muse_init_args, args);
> + struct muse_conn *mc = mia->mc;
> + struct muse_init_out *arg = &mia->out;
> + struct mtd_info *mtd = &mc->mtd;
> +
> + if (error || arg->fuse_major != FUSE_KERNEL_VERSION ||
> + arg->fuse_minor < MUSE_MIN_MINOR)
> + goto abort;
> +
> + mc->max_read = max_t(unsigned int, arg->max_read, 4096);
> + mc->max_write = max_t(unsigned int, arg->max_write, 4096);
> +
> + if (muse_parse_mtdreq(mia->info, args->out_args[1].size, mc))
> + goto abort;
> +
> + /*
> + * We want one READ/WRITE op per MTD io. So the MTD pagesize plus
> + * the out-of-band area needs to fit into max_write/max_read.
> + */
> + if (mc->max_write < mtd->writebufsize + mtd->oobsize ||
> + mc->max_read < mtd->writebufsize + mtd->oobsize)
> + goto abort;
[Severity: Medium]
Should mc->max_read, mc->max_write, and req->size have upper bounds?
mc->max_read and mc->max_write are only clamped from below; with values
near UINT_MAX, DIV_ROUND_UP(page_offs + nbytes, mtd->writesize) in
muse_io_one() can overflow 32-bit size_t to 0 on 32-bit architectures, and
combined in-band plus OOB payloads can overflow the 32-bit request length
calculations in fuse_len_args() and fuse_copy_out_args(). Also, req->size
is parsed as an unsigned u64 without checking req->size <= LLONG_MAX, even
though MTD offsets and muse_io.pos use signed loff_t.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261130082634.2750846-1-rw@kernel.org?part=9
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH 10/10] MAINTAINERS: Add entry for MUSE
2026-11-30 8:26 [PATCH 0/10] MUSE: Userspace backed MTD v4 Richard Weinberger (sigma star gmbh)
` (8 preceding siblings ...)
2026-11-30 8:26 ` [PATCH 09/10] mtd: Implement MUSE - MTD in userspace Richard Weinberger (sigma star gmbh)
@ 2026-11-30 8:26 ` Richard Weinberger (sigma star gmbh)
9 siblings, 0 replies; 13+ messages in thread
From: Richard Weinberger (sigma star gmbh) @ 2026-11-30 8:26 UTC (permalink / raw)
To: linux-mtd
Cc: fuse-devel, netdev, horms, pabeni, kuba, edumazet, davem, miklos,
vigneshr, miquel.raynal, david.oberhollenzer, upstream+linux,
Richard Weinberger
From: Richard Weinberger <richard@nod.at>
MUSE is an MTD driver which sits on top of the FUSE transport. Make
sure that both linux-mtd@ and linux-fsdevel@ are CC'ed on patches such
that MTD and FUSE related aspects of changes can be reviewed.
Signed-off-by: Richard Weinberger <richard@nod.at>
---
MAINTAINERS | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/MAINTAINERS b/MAINTAINERS
index 6626e026deea..1bb8c7e90f99 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -19186,6 +19186,13 @@ L: linux-usb@vger.kernel.org
S: Maintained
F: drivers/usb/musb/
+MUSE: MTD IN USERSPACE DRIVER
+M: Richard Weinberger <richard@nod.at>
+L: linux-mtd@lists.infradead.org
+L: linux-fsdevel@vger.kernel.org
+S: Maintained
+F: drivers/mtd/muse.c
+
MXL301RF MEDIA DRIVER
M: Akihiro Tsukada <tskd08@gmail.com>
L: linux-media@vger.kernel.org
--
2.55.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 13+ messages in thread