Skip to content

Commit cc080e9

Browse files
author
Miklos Szeredi
committed
fuse: introduce per-instance fuse_dev structure
Allow fuse device clones to refer to be distinguished. This patch just adds the infrastructure by associating a separate "struct fuse_dev" with each clone. Signed-off-by: Miklos Szeredi <mszeredi@suse.cz> Reviewed-by: Ashish Samant <ashish.samant@oracle.com>
1 parent 00c570f commit cc080e9

4 files changed

Lines changed: 114 additions & 34 deletions

File tree

‎fs/fuse/cuse.c‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ static void cuse_fc_release(struct fuse_conn *fc)
489489
*/
490490
static int cuse_channel_open(struct inode *inode, struct file *file)
491491
{
492+
struct fuse_dev *fud;
492493
struct cuse_conn *cc;
493494
int rc;
494495

@@ -499,16 +500,22 @@ static int cuse_channel_open(struct inode *inode, struct file *file)
499500

500501
fuse_conn_init(&cc->fc);
501502

503+
fud = fuse_dev_alloc(&cc->fc);
504+
if (!fud) {
505+
kfree(cc);
506+
return -ENOMEM;
507+
}
508+
502509
INIT_LIST_HEAD(&cc->list);
503510
cc->fc.release = cuse_fc_release;
504511

505512
cc->fc.initialized = 1;
506513
rc = cuse_send_init(cc);
507514
if (rc) {
508-
fuse_conn_put(&cc->fc);
515+
fuse_dev_free(fud);
509516
return rc;
510517
}
511-
file->private_data = &cc->fc; /* channel owns base reference to cc */
518+
file->private_data = fud;
512519

513520
return 0;
514521
}
@@ -526,7 +533,8 @@ static int cuse_channel_open(struct inode *inode, struct file *file)
526533
*/
527534
static int cuse_channel_release(struct inode *inode, struct file *file)
528535
{
529-
struct cuse_conn *cc = fc_to_cc(file->private_data);
536+
struct fuse_dev *fud = file->private_data;
537+
struct cuse_conn *cc = fc_to_cc(fud->fc);
530538
int rc;
531539

532540
/* remove from the conntbl, no more access from this point on */

‎fs/fuse/dev.c‎

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ MODULE_ALIAS("devname:fuse");
2525

2626
static struct kmem_cache *fuse_req_cachep;
2727

28-
static struct fuse_conn *fuse_get_conn(struct file *file)
28+
static struct fuse_dev *fuse_get_dev(struct file *file)
2929
{
3030
/*
3131
* Lockless access is OK, because file->private data is set
3232
* once during mount and is valid until the file is released.
3333
*/
34-
return file->private_data;
34+
return ACCESS_ONCE(file->private_data);
3535
}
3636

3737
static void fuse_request_init(struct fuse_req *req, struct page **pages,
@@ -1348,16 +1348,17 @@ static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to)
13481348
{
13491349
struct fuse_copy_state cs;
13501350
struct file *file = iocb->ki_filp;
1351-
struct fuse_conn *fc = fuse_get_conn(file);
1352-
if (!fc)
1351+
struct fuse_dev *fud = fuse_get_dev(file);
1352+
1353+
if (!fud)
13531354
return -EPERM;
13541355

13551356
if (!iter_is_iovec(to))
13561357
return -EINVAL;
13571358

13581359
fuse_copy_init(&cs, 1, to);
13591360

1360-
return fuse_dev_do_read(fc, file, &cs, iov_iter_count(to));
1361+
return fuse_dev_do_read(fud->fc, file, &cs, iov_iter_count(to));
13611362
}
13621363

13631364
static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
@@ -1369,8 +1370,9 @@ static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
13691370
int do_wakeup = 0;
13701371
struct pipe_buffer *bufs;
13711372
struct fuse_copy_state cs;
1372-
struct fuse_conn *fc = fuse_get_conn(in);
1373-
if (!fc)
1373+
struct fuse_dev *fud = fuse_get_dev(in);
1374+
1375+
if (!fud)
13741376
return -EPERM;
13751377

13761378
bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
@@ -1380,7 +1382,7 @@ static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
13801382
fuse_copy_init(&cs, 1, NULL);
13811383
cs.pipebufs = bufs;
13821384
cs.pipe = pipe;
1383-
ret = fuse_dev_do_read(fc, in, &cs, len);
1385+
ret = fuse_dev_do_read(fud->fc, in, &cs, len);
13841386
if (ret < 0)
13851387
goto out;
13861388

@@ -1954,16 +1956,17 @@ static ssize_t fuse_dev_do_write(struct fuse_conn *fc,
19541956
static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from)
19551957
{
19561958
struct fuse_copy_state cs;
1957-
struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp);
1958-
if (!fc)
1959+
struct fuse_dev *fud = fuse_get_dev(iocb->ki_filp);
1960+
1961+
if (!fud)
19591962
return -EPERM;
19601963

19611964
if (!iter_is_iovec(from))
19621965
return -EINVAL;
19631966

19641967
fuse_copy_init(&cs, 0, from);
19651968

1966-
return fuse_dev_do_write(fc, &cs, iov_iter_count(from));
1969+
return fuse_dev_do_write(fud->fc, &cs, iov_iter_count(from));
19671970
}
19681971

19691972
static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
@@ -1974,12 +1977,12 @@ static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
19741977
unsigned idx;
19751978
struct pipe_buffer *bufs;
19761979
struct fuse_copy_state cs;
1977-
struct fuse_conn *fc;
1980+
struct fuse_dev *fud;
19781981
size_t rem;
19791982
ssize_t ret;
19801983

1981-
fc = fuse_get_conn(out);
1982-
if (!fc)
1984+
fud = fuse_get_dev(out);
1985+
if (!fud)
19831986
return -EPERM;
19841987

19851988
bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
@@ -2034,7 +2037,7 @@ static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
20342037
if (flags & SPLICE_F_MOVE)
20352038
cs.move_pages = 1;
20362039

2037-
ret = fuse_dev_do_write(fc, &cs, len);
2040+
ret = fuse_dev_do_write(fud->fc, &cs, len);
20382041

20392042
for (idx = 0; idx < nbuf; idx++) {
20402043
struct pipe_buffer *buf = &bufs[idx];
@@ -2049,11 +2052,12 @@ static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
20492052
{
20502053
unsigned mask = POLLOUT | POLLWRNORM;
20512054
struct fuse_iqueue *fiq;
2052-
struct fuse_conn *fc = fuse_get_conn(file);
2053-
if (!fc)
2055+
struct fuse_dev *fud = fuse_get_dev(file);
2056+
2057+
if (!fud)
20542058
return POLLERR;
20552059

2056-
fiq = &fc->iq;
2060+
fiq = &fud->fc->iq;
20572061
poll_wait(file, &fiq->waitq, wait);
20582062

20592063
spin_lock(&fiq->waitq.lock);
@@ -2175,12 +2179,15 @@ EXPORT_SYMBOL_GPL(fuse_abort_conn);
21752179

21762180
int fuse_dev_release(struct inode *inode, struct file *file)
21772181
{
2178-
struct fuse_conn *fc = fuse_get_conn(file);
2179-
if (fc) {
2182+
struct fuse_dev *fud = fuse_get_dev(file);
2183+
2184+
if (fud) {
2185+
struct fuse_conn *fc = fud->fc;
2186+
21802187
WARN_ON(!list_empty(&fc->pq.io));
21812188
WARN_ON(fc->iq.fasync != NULL);
21822189
fuse_abort_conn(fc);
2183-
fuse_conn_put(fc);
2190+
fuse_dev_free(fud);
21842191
}
21852192

21862193
return 0;
@@ -2189,20 +2196,27 @@ EXPORT_SYMBOL_GPL(fuse_dev_release);
21892196

21902197
static int fuse_dev_fasync(int fd, struct file *file, int on)
21912198
{
2192-
struct fuse_conn *fc = fuse_get_conn(file);
2193-
if (!fc)
2199+
struct fuse_dev *fud = fuse_get_dev(file);
2200+
2201+
if (!fud)
21942202
return -EPERM;
21952203

21962204
/* No locking - fasync_helper does its own locking */
2197-
return fasync_helper(fd, file, on, &fc->iq.fasync);
2205+
return fasync_helper(fd, file, on, &fud->fc->iq.fasync);
21982206
}
21992207

22002208
static int fuse_device_clone(struct fuse_conn *fc, struct file *new)
22012209
{
2210+
struct fuse_dev *fud;
2211+
22022212
if (new->private_data)
22032213
return -EINVAL;
22042214

2205-
new->private_data = fuse_conn_get(fc);
2215+
fud = fuse_dev_alloc(fc);
2216+
if (!fud)
2217+
return -ENOMEM;
2218+
2219+
new->private_data = fud;
22062220

22072221
return 0;
22082222
}
@@ -2221,11 +2235,11 @@ static long fuse_dev_ioctl(struct file *file, unsigned int cmd,
22212235

22222236
err = -EINVAL;
22232237
if (old) {
2224-
struct fuse_conn *fc = fuse_get_conn(old);
2238+
struct fuse_dev *fud = fuse_get_dev(old);
22252239

2226-
if (fc) {
2240+
if (fud) {
22272241
mutex_lock(&fuse_mutex);
2228-
err = fuse_device_clone(fc, file);
2242+
err = fuse_device_clone(fud->fc, file);
22292243
mutex_unlock(&fuse_mutex);
22302244
}
22312245
fput(old);

‎fs/fuse/fuse_i.h‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,17 @@ struct fuse_pqueue {
417417
struct list_head io;
418418
};
419419

420+
/**
421+
* Fuse device instance
422+
*/
423+
struct fuse_dev {
424+
/** Fuse connection for this device */
425+
struct fuse_conn *fc;
426+
427+
/** list entry on fc->devices */
428+
struct list_head entry;
429+
};
430+
420431
/**
421432
* A Fuse connection.
422433
*
@@ -629,6 +640,9 @@ struct fuse_conn {
629640

630641
/** Read/write semaphore to hold when accessing sb. */
631642
struct rw_semaphore killsb;
643+
644+
/** List of device instances belonging to this connection */
645+
struct list_head devices;
632646
};
633647

634648
static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb)
@@ -841,6 +855,9 @@ void fuse_conn_init(struct fuse_conn *fc);
841855
*/
842856
void fuse_conn_put(struct fuse_conn *fc);
843857

858+
struct fuse_dev *fuse_dev_alloc(struct fuse_conn *fc);
859+
void fuse_dev_free(struct fuse_dev *fud);
860+
844861
/**
845862
* Add connection to control filesystem
846863
*/

‎fs/fuse/inode.c‎

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,7 @@ void fuse_conn_init(struct fuse_conn *fc)
598598
fuse_pqueue_init(&fc->pq);
599599
INIT_LIST_HEAD(&fc->bg_queue);
600600
INIT_LIST_HEAD(&fc->entry);
601+
INIT_LIST_HEAD(&fc->devices);
601602
atomic_set(&fc->num_waiting, 0);
602603
fc->max_background = FUSE_DEFAULT_MAX_BACKGROUND;
603604
fc->congestion_threshold = FUSE_DEFAULT_CONGESTION_THRESHOLD;
@@ -945,6 +946,7 @@ static void fuse_send_init(struct fuse_conn *fc, struct fuse_req *req)
945946

946947
static void fuse_free_conn(struct fuse_conn *fc)
947948
{
949+
WARN_ON(!list_empty(&fc->devices));
948950
kfree_rcu(fc, rcu);
949951
}
950952

@@ -990,8 +992,41 @@ static int fuse_bdi_init(struct fuse_conn *fc, struct super_block *sb)
990992
return 0;
991993
}
992994

995+
struct fuse_dev *fuse_dev_alloc(struct fuse_conn *fc)
996+
{
997+
struct fuse_dev *fud;
998+
999+
fud = kzalloc(sizeof(struct fuse_dev), GFP_KERNEL);
1000+
if (fud) {
1001+
fud->fc = fuse_conn_get(fc);
1002+
1003+
spin_lock(&fc->lock);
1004+
list_add_tail(&fud->entry, &fc->devices);
1005+
spin_unlock(&fc->lock);
1006+
}
1007+
1008+
return fud;
1009+
}
1010+
EXPORT_SYMBOL_GPL(fuse_dev_alloc);
1011+
1012+
void fuse_dev_free(struct fuse_dev *fud)
1013+
{
1014+
struct fuse_conn *fc = fud->fc;
1015+
1016+
if (fc) {
1017+
spin_lock(&fc->lock);
1018+
list_del(&fud->entry);
1019+
spin_unlock(&fc->lock);
1020+
1021+
fuse_conn_put(fc);
1022+
}
1023+
kfree(fud);
1024+
}
1025+
EXPORT_SYMBOL_GPL(fuse_dev_free);
1026+
9931027
static int fuse_fill_super(struct super_block *sb, void *data, int silent)
9941028
{
1029+
struct fuse_dev *fud;
9951030
struct fuse_conn *fc;
9961031
struct inode *root;
9971032
struct fuse_mount_data d;
@@ -1043,11 +1078,15 @@ static int fuse_fill_super(struct super_block *sb, void *data, int silent)
10431078
fuse_conn_init(fc);
10441079
fc->release = fuse_free_conn;
10451080

1081+
fud = fuse_dev_alloc(fc);
1082+
if (!fud)
1083+
goto err_put_conn;
1084+
10461085
fc->dev = sb->s_dev;
10471086
fc->sb = sb;
10481087
err = fuse_bdi_init(fc, sb);
10491088
if (err)
1050-
goto err_put_conn;
1089+
goto err_dev_free;
10511090

10521091
sb->s_bdi = &fc->bdi;
10531092

@@ -1068,7 +1107,7 @@ static int fuse_fill_super(struct super_block *sb, void *data, int silent)
10681107
root = fuse_get_root_inode(sb, d.rootmode);
10691108
root_dentry = d_make_root(root);
10701109
if (!root_dentry)
1071-
goto err_put_conn;
1110+
goto err_dev_free;
10721111
/* only now - we want root dentry with NULL ->d_op */
10731112
sb->s_d_op = &fuse_dentry_operations;
10741113

@@ -1094,7 +1133,7 @@ static int fuse_fill_super(struct super_block *sb, void *data, int silent)
10941133

10951134
list_add_tail(&fc->entry, &fuse_conn_list);
10961135
sb->s_root = root_dentry;
1097-
file->private_data = fuse_conn_get(fc);
1136+
file->private_data = fud;
10981137
mutex_unlock(&fuse_mutex);
10991138
/*
11001139
* atomic_dec_and_test() in fput() provides the necessary
@@ -1113,6 +1152,8 @@ static int fuse_fill_super(struct super_block *sb, void *data, int silent)
11131152
fuse_request_free(init_req);
11141153
err_put_root:
11151154
dput(root_dentry);
1155+
err_dev_free:
1156+
fuse_dev_free(fud);
11161157
err_put_conn:
11171158
fuse_bdi_destroy(fc);
11181159
fuse_conn_put(fc);

0 commit comments

Comments
 (0)