From: Richard Weinberger <rich…@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 <rich…@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