From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2CDA6606A330 for ; Mon, 30 Nov 2026 08:27:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=nR8QkbkN; dkim=fail reason="signature verification failed" (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=At2F3z97 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Unsubscribe-Post:List-Owner; bh=G1PaSDLdtOx8W9e/lbJL4C+ceSQNRS+mi6QdXVdWNgo=; b=nR8QkbkN26lXjsYphQpDOki+m/ m5fG/zl6c/UunkJMLsZnP+OhZcUpPFLJZHEQbpeRSE83RhKxfPCkeHKdXfoKvL5vvxWyoF2XR+3Fd 2NHG9YFbbGHVbDWTPEz5AwJ/KERJpb3jYFwhfTLFe5J9LhLDBQA1xw8yUJLaZwutMwBvZszaPiUt0 DhKyE1jz3Oj9X/BONl07coww3a43L+sfm/ktdLTUqt+oL7SnURx2pWuVV5jSnVziab3M3jbSn7frt hsZvwbMg8O9hQ4FGtLY/U5RZLruyT0/UAsRfy3y+YCWWZ2A/rDASMaNvI4/EG0gGioKIdm4+DO8qH HBvHKCcQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xXwiy-00000003k1O-3m6N; Mon, 30 Nov 2026 08:27:00 +0000 Received: from tor.source.kernel.org ([2600:3c04:e001:324:0:1991:8:25]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xXwiy-00000003k0i-1EmY for linux-mtd@lists.infradead.org; Mon, 30 Nov 2026 08:27:00 +0000 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id BCA9161DCB; Mon, 30 Nov 2026 08:26:59 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C2A051F00896; Mon, 30 Nov 2026 08:26:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796027218; bh=xNZ44ZPXdkwQ8xHbnjGYf7jhbaJ4uaEgaQHmUjSk00M=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=At2F3z973VxUI6QxQFMoqXTi/eS3vytH1eEPlCB3qNl/KYvcM1DnS0B37wAvEbXvl d54w4MJRHGjvPwo/k1vl+YAvi3t3+wFBgVaWLCcDNRIPGoC55BrIQMZZyRsyfgA2qf KDdQPsrkzrvG9oi3HKluSvYfU3QNDOq398c7uQ9F5aTHO8kkN+O5w2w31WbmpDOGr7 Pb2ml2lmdCZqLAlHcBCkeQ80q1aaRiRK4pXP9otloHnFnIId+ZNk51Fk75/imcbpYs oPzU98GBV6ySOUP4G5evIch1eK7ucXUHNSEe7jgtEVGkxcByE/dOKecNFintfxjumZ EV+dSsumKGbsA== From: "Richard Weinberger (sigma star gmbh)" 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 Subject: [PATCH 03/10] fuse: Add an interface for userspace backed devices Date: Mon, 30 Nov 2026 09:26:26 +0100 Message-ID: <20261130082634.2750846-4-rw@kernel.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261130082634.2750846-1-rw@kernel.org> References: <20261130082634.2750846-1-rw@kernel.org> X-Archived-By: MIME-Version: 1.0 X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Linux MTD discussion mailing list List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-mtd" X-Spam-Status: No, score=-16.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 From: Richard Weinberger 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 --- 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 @@ -11,9 +11,168 @@ #include #include +#include #include +#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 . + */ +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/