Skip to content

Commit 1dd5395

Browse files
rhvgoyalMiklos Szeredi
authored andcommitted
virtiofs: add a mount option to enable dax
Add a mount option to allow using dax with virtio_fs. Signed-off-by: Vivek Goyal <vgoyal@redhat.com> Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
1 parent 22f3787 commit 1dd5395

6 files changed

Lines changed: 151 additions & 17 deletions

File tree

‎fs/fuse/Kconfig‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,16 @@ config VIRTIO_FS
3838

3939
If you want to share files between guests or with the host, answer Y
4040
or M.
41+
42+
config FUSE_DAX
43+
bool "Virtio Filesystem Direct Host Memory Access support"
44+
default y
45+
depends on VIRTIO_FS
46+
depends on FS_DAX
47+
depends on DAX_DRIVER
48+
help
49+
This allows bypassing guest page cache and allows mapping host page
50+
cache directly in guest address space.
51+
52+
If you want to allow mounting a Virtio Filesystem with the "dax"
53+
option, answer Y.

‎fs/fuse/Makefile‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ obj-$(CONFIG_FUSE_FS) += fuse.o
77
obj-$(CONFIG_CUSE) += cuse.o
88
obj-$(CONFIG_VIRTIO_FS) += virtiofs.o
99

10-
fuse-objs := dev.o dir.o file.o inode.o control.o xattr.o acl.o readdir.o
11-
virtiofs-y += virtio_fs.o
10+
fuse-y := dev.o dir.o file.o inode.o control.o xattr.o acl.o readdir.o
11+
fuse-$(CONFIG_FUSE_DAX) += dax.o
12+
13+
virtiofs-y := virtio_fs.o

‎fs/fuse/dax.c‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* dax: direct host memory access
4+
* Copyright (C) 2020 Red Hat, Inc.
5+
*/
6+
7+
#include "fuse_i.h"
8+
9+
#include <linux/dax.h>
10+
11+
struct fuse_conn_dax {
12+
/* DAX device */
13+
struct dax_device *dev;
14+
};
15+
16+
void fuse_dax_conn_free(struct fuse_conn *fc)
17+
{
18+
kfree(fc->dax);
19+
}
20+
21+
int fuse_dax_conn_alloc(struct fuse_conn *fc, struct dax_device *dax_dev)
22+
{
23+
struct fuse_conn_dax *fcd;
24+
25+
if (!dax_dev)
26+
return 0;
27+
28+
fcd = kzalloc(sizeof(*fcd), GFP_KERNEL);
29+
if (!fcd)
30+
return -ENOMEM;
31+
32+
fcd->dev = dax_dev;
33+
34+
fc->dax = fcd;
35+
return 0;
36+
}

‎fs/fuse/fuse_i.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,10 +483,14 @@ struct fuse_fs_context {
483483
bool no_control:1;
484484
bool no_force_umount:1;
485485
bool legacy_opts_show:1;
486+
bool dax:1;
486487
unsigned int max_read;
487488
unsigned int blksize;
488489
const char *subtype;
489490

491+
/* DAX device, may be NULL */
492+
struct dax_device *dax_dev;
493+
490494
/* fuse_dev pointer to fill in, should contain NULL on entry */
491495
void **fudptr;
492496
};
@@ -755,6 +759,11 @@ struct fuse_conn {
755759

756760
/** List of device instances belonging to this connection */
757761
struct list_head devices;
762+
763+
#ifdef CONFIG_FUSE_DAX
764+
/* Dax specific conn data, non-NULL if DAX is enabled */
765+
struct fuse_conn_dax *dax;
766+
#endif
758767
};
759768

760769
static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb)
@@ -1093,4 +1102,9 @@ unsigned int fuse_len_args(unsigned int numargs, struct fuse_arg *args);
10931102
u64 fuse_get_unique(struct fuse_iqueue *fiq);
10941103
void fuse_free_conn(struct fuse_conn *fc);
10951104

1105+
/* dax.c */
1106+
1107+
int fuse_dax_conn_alloc(struct fuse_conn *fc, struct dax_device *dax_dev);
1108+
void fuse_dax_conn_free(struct fuse_conn *fc);
1109+
10961110
#endif /* _FS_FUSE_I_H */

‎fs/fuse/inode.c‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,11 @@ static int fuse_show_options(struct seq_file *m, struct dentry *root)
587587
if (sb->s_bdev && sb->s_blocksize != FUSE_DEFAULT_BLKSIZE)
588588
seq_printf(m, ",blksize=%lu", sb->s_blocksize);
589589
}
590+
#ifdef CONFIG_FUSE_DAX
591+
if (fc->dax)
592+
seq_puts(m, ",dax");
593+
#endif
594+
590595
return 0;
591596
}
592597

@@ -651,6 +656,8 @@ void fuse_conn_put(struct fuse_conn *fc)
651656
if (refcount_dec_and_test(&fc->count)) {
652657
struct fuse_iqueue *fiq = &fc->iq;
653658

659+
if (IS_ENABLED(CONFIG_FUSE_DAX))
660+
fuse_dax_conn_free(fc);
654661
if (fiq->ops->release)
655662
fiq->ops->release(fiq);
656663
put_pid_ns(fc->pid_ns);
@@ -1175,11 +1182,17 @@ int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
11751182
if (sb->s_user_ns != &init_user_ns)
11761183
sb->s_xattr = fuse_no_acl_xattr_handlers;
11771184

1185+
if (IS_ENABLED(CONFIG_FUSE_DAX)) {
1186+
err = fuse_dax_conn_alloc(fc, ctx->dax_dev);
1187+
if (err)
1188+
goto err;
1189+
}
1190+
11781191
if (ctx->fudptr) {
11791192
err = -ENOMEM;
11801193
fud = fuse_dev_alloc_install(fc);
11811194
if (!fud)
1182-
goto err;
1195+
goto err_free_dax;
11831196
}
11841197

11851198
fc->dev = sb->s_dev;
@@ -1234,6 +1247,9 @@ int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
12341247
err_dev_free:
12351248
if (fud)
12361249
fuse_dev_free(fud);
1250+
err_free_dax:
1251+
if (IS_ENABLED(CONFIG_FUSE_DAX))
1252+
fuse_dax_conn_free(fc);
12371253
err:
12381254
return err;
12391255
}

‎fs/fuse/virtio_fs.c‎

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/virtio_fs.h>
1414
#include <linux/delay.h>
1515
#include <linux/fs_context.h>
16+
#include <linux/fs_parser.h>
1617
#include <linux/highmem.h>
1718
#include <linux/uio.h>
1819
#include "fuse_i.h"
@@ -81,6 +82,44 @@ struct virtio_fs_req_work {
8182
static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq,
8283
struct fuse_req *req, bool in_flight);
8384

85+
enum {
86+
OPT_DAX,
87+
};
88+
89+
static const struct fs_parameter_spec virtio_fs_parameters[] = {
90+
fsparam_flag("dax", OPT_DAX),
91+
{}
92+
};
93+
94+
static int virtio_fs_parse_param(struct fs_context *fc,
95+
struct fs_parameter *param)
96+
{
97+
struct fs_parse_result result;
98+
struct fuse_fs_context *ctx = fc->fs_private;
99+
int opt;
100+
101+
opt = fs_parse(fc, virtio_fs_parameters, param, &result);
102+
if (opt < 0)
103+
return opt;
104+
105+
switch (opt) {
106+
case OPT_DAX:
107+
ctx->dax = 1;
108+
break;
109+
default:
110+
return -EINVAL;
111+
}
112+
113+
return 0;
114+
}
115+
116+
static void virtio_fs_free_fc(struct fs_context *fc)
117+
{
118+
struct fuse_fs_context *ctx = fc->fs_private;
119+
120+
kfree(ctx);
121+
}
122+
84123
static inline struct virtio_fs_vq *vq_to_fsvq(struct virtqueue *vq)
85124
{
86125
struct virtio_fs *fs = vq->vdev->priv;
@@ -1219,23 +1258,27 @@ static const struct fuse_iqueue_ops virtio_fs_fiq_ops = {
12191258
.release = virtio_fs_fiq_release,
12201259
};
12211260

1222-
static int virtio_fs_fill_super(struct super_block *sb)
1261+
static inline void virtio_fs_ctx_set_defaults(struct fuse_fs_context *ctx)
1262+
{
1263+
ctx->rootmode = S_IFDIR;
1264+
ctx->default_permissions = 1;
1265+
ctx->allow_other = 1;
1266+
ctx->max_read = UINT_MAX;
1267+
ctx->blksize = 512;
1268+
ctx->destroy = true;
1269+
ctx->no_control = true;
1270+
ctx->no_force_umount = true;
1271+
}
1272+
1273+
static int virtio_fs_fill_super(struct super_block *sb, struct fs_context *fsc)
12231274
{
12241275
struct fuse_conn *fc = get_fuse_conn_super(sb);
12251276
struct virtio_fs *fs = fc->iq.priv;
1277+
struct fuse_fs_context *ctx = fsc->fs_private;
12261278
unsigned int i;
12271279
int err;
1228-
struct fuse_fs_context ctx = {
1229-
.rootmode = S_IFDIR,
1230-
.default_permissions = 1,
1231-
.allow_other = 1,
1232-
.max_read = UINT_MAX,
1233-
.blksize = 512,
1234-
.destroy = true,
1235-
.no_control = true,
1236-
.no_force_umount = true,
1237-
};
12381280

1281+
virtio_fs_ctx_set_defaults(ctx);
12391282
mutex_lock(&virtio_fs_mutex);
12401283

12411284
/* After holding mutex, make sure virtiofs device is still there.
@@ -1259,8 +1302,10 @@ static int virtio_fs_fill_super(struct super_block *sb)
12591302
}
12601303

12611304
/* virtiofs allocates and installs its own fuse devices */
1262-
ctx.fudptr = NULL;
1263-
err = fuse_fill_super_common(sb, &ctx);
1305+
ctx->fudptr = NULL;
1306+
if (ctx->dax)
1307+
ctx->dax_dev = fs->dax_dev;
1308+
err = fuse_fill_super_common(sb, ctx);
12641309
if (err < 0)
12651310
goto err_free_fuse_devs;
12661311

@@ -1371,7 +1416,7 @@ static int virtio_fs_get_tree(struct fs_context *fsc)
13711416
return PTR_ERR(sb);
13721417

13731418
if (!sb->s_root) {
1374-
err = virtio_fs_fill_super(sb);
1419+
err = virtio_fs_fill_super(sb, fsc);
13751420
if (err) {
13761421
deactivate_locked_super(sb);
13771422
return err;
@@ -1386,11 +1431,19 @@ static int virtio_fs_get_tree(struct fs_context *fsc)
13861431
}
13871432

13881433
static const struct fs_context_operations virtio_fs_context_ops = {
1434+
.free = virtio_fs_free_fc,
1435+
.parse_param = virtio_fs_parse_param,
13891436
.get_tree = virtio_fs_get_tree,
13901437
};
13911438

13921439
static int virtio_fs_init_fs_context(struct fs_context *fsc)
13931440
{
1441+
struct fuse_fs_context *ctx;
1442+
1443+
ctx = kzalloc(sizeof(struct fuse_fs_context), GFP_KERNEL);
1444+
if (!ctx)
1445+
return -ENOMEM;
1446+
fsc->fs_private = ctx;
13941447
fsc->ops = &virtio_fs_context_ops;
13951448
return 0;
13961449
}

0 commit comments

Comments
 (0)