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