From: "Richard Weinberger (sigma star gmbh)" <rw@kernel.org>
To: linux-mtd@lists.infradead.org
Cc: fuse-devel@lists.linux.dev, netdev@vger.kernel.org,
horms@kernel.org, pabeni@redhat.com, kuba@kernel.org,
edumazet@google.com, davem@davemloft.net, miklos@szeredi.hu,
vigneshr@ti.com, miquel.raynal@bootlin.com,
david.oberhollenzer@sigma-star.at, upstream+linux@sigma-star.at,
Richard Weinberger <richard@nod.at>
Subject: [PATCH 09/10] mtd: Implement MUSE - MTD in userspace
Date: Mon, 30 Nov 2026 09:26:32 +0100 [thread overview]
Message-ID: <20261130082634.2750846-10-rw@kernel.org> (raw)
In-Reply-To: <20261130082634.2750846-1-rw@kernel.org>
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
next prev parent reply other threads:[~2026-11-30 8:27 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
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 ` [PATCH 03/10] fuse: Add an interface for userspace backed devices Richard Weinberger (sigma star gmbh)
2026-11-30 8:26 ` [PATCH 04/10] fuse: Convert CUSE to the " 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)
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 ` [PATCH 07/10] mtd: Add MTD_MUSE flag 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)
2026-11-30 8:26 ` Richard Weinberger (sigma star gmbh) [this message]
2026-11-30 8:26 ` [PATCH 10/10] MAINTAINERS: Add entry for MUSE Richard Weinberger (sigma star gmbh)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261130082634.2750846-10-rw@kernel.org \
--to=rw@kernel.org \
--cc=davem@davemloft.net \
--cc=david.oberhollenzer@sigma-star.at \
--cc=edumazet@google.com \
--cc=fuse-devel@lists.linux.dev \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-mtd@lists.infradead.org \
--cc=miklos@szeredi.hu \
--cc=miquel.raynal@bootlin.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=richard@nod.at \
--cc=upstream+linux@sigma-star.at \
--cc=vigneshr@ti.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox