Linux-mtd Archive on lore.kernel.org
 help / color / mirror / Atom feed
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 03/10] fuse: Add an interface for userspace backed devices
Date: Mon, 30 Nov 2026 09:26:26 +0100	[thread overview]
Message-ID: <20261130082634.2750846-4-rw@kernel.org> (raw)
In-Reply-To: <20261130082634.2750846-1-rw@kernel.org>

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/

  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 ` Richard Weinberger (sigma star gmbh) [this message]
2026-11-30  8:26 ` [PATCH 04/10] fuse: Convert CUSE to the interface for userspace backed devices 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 ` [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)

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-4-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