virtiofs: add a mount option to enable dax
authorVivek Goyal <vgoyal@redhat.com>
Wed, 19 Aug 2020 22:19:47 +0000 (18:19 -0400)
committerMiklos Szeredi <mszeredi@redhat.com>
Thu, 10 Sep 2020 09:39:22 +0000 (11:39 +0200)
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>
fs/fuse/Kconfig
fs/fuse/Makefile
fs/fuse/dax.c [new file with mode: 0644]
fs/fuse/fuse_i.h
fs/fuse/inode.c
fs/fuse/virtio_fs.c

index 0156dc8..fddd406 100644 (file)
@@ -38,3 +38,16 @@ config VIRTIO_FS
 
          If you want to share files between guests or with the host, answer Y
          or M.
+
+config FUSE_DAX
+       bool "Virtio Filesystem Direct Host Memory Access support"
+       default y
+       depends on VIRTIO_FS
+       depends on FS_DAX
+       depends on DAX_DRIVER
+       help
+         This allows bypassing guest page cache and allows mapping host page
+         cache directly in guest address space.
+
+         If you want to allow mounting a Virtio Filesystem with the "dax"
+         option, answer Y.
index 3e8cebf..8c7021f 100644 (file)
@@ -7,5 +7,7 @@ obj-$(CONFIG_FUSE_FS) += fuse.o
 obj-$(CONFIG_CUSE) += cuse.o
 obj-$(CONFIG_VIRTIO_FS) += virtiofs.o
 
-fuse-objs := dev.o dir.o file.o inode.o control.o xattr.o acl.o readdir.o
-virtiofs-y += virtio_fs.o
+fuse-y := dev.o dir.o file.o inode.o control.o xattr.o acl.o readdir.o
+fuse-$(CONFIG_FUSE_DAX) += dax.o
+
+virtiofs-y := virtio_fs.o
diff --git a/fs/fuse/dax.c b/fs/fuse/dax.c
new file mode 100644 (file)
index 0000000..9660d01
--- /dev/null
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * dax: direct host memory access
+ * Copyright (C) 2020 Red Hat, Inc.
+ */
+
+#include "fuse_i.h"
+
+#include <linux/dax.h>
+
+struct fuse_conn_dax {
+       /* DAX device */
+       struct dax_device *dev;
+};
+
+void fuse_dax_conn_free(struct fuse_conn *fc)
+{
+       kfree(fc->dax);
+}
+
+int fuse_dax_conn_alloc(struct fuse_conn *fc, struct dax_device *dax_dev)
+{
+       struct fuse_conn_dax *fcd;
+
+       if (!dax_dev)
+               return 0;
+
+       fcd = kzalloc(sizeof(*fcd), GFP_KERNEL);
+       if (!fcd)
+               return -ENOMEM;
+
+       fcd->dev = dax_dev;
+
+       fc->dax = fcd;
+       return 0;
+}
index 30737aa..97af795 100644 (file)
@@ -483,10 +483,14 @@ struct fuse_fs_context {
        bool no_control:1;
        bool no_force_umount:1;
        bool legacy_opts_show:1;
+       bool dax:1;
        unsigned int max_read;
        unsigned int blksize;
        const char *subtype;
 
+       /* DAX device, may be NULL */
+       struct dax_device *dax_dev;
+
        /* fuse_dev pointer to fill in, should contain NULL on entry */
        void **fudptr;
 };
@@ -755,6 +759,11 @@ struct fuse_conn {
 
        /** List of device instances belonging to this connection */
        struct list_head devices;
+
+#ifdef CONFIG_FUSE_DAX
+       /* Dax specific conn data, non-NULL if DAX is enabled */
+       struct fuse_conn_dax *dax;
+#endif
 };
 
 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);
 u64 fuse_get_unique(struct fuse_iqueue *fiq);
 void fuse_free_conn(struct fuse_conn *fc);
 
+/* dax.c */
+
+int fuse_dax_conn_alloc(struct fuse_conn *fc, struct dax_device *dax_dev);
+void fuse_dax_conn_free(struct fuse_conn *fc);
+
 #endif /* _FS_FUSE_I_H */
index bdae765..1780dfe 100644 (file)
@@ -587,6 +587,11 @@ static int fuse_show_options(struct seq_file *m, struct dentry *root)
                if (sb->s_bdev && sb->s_blocksize != FUSE_DEFAULT_BLKSIZE)
                        seq_printf(m, ",blksize=%lu", sb->s_blocksize);
        }
+#ifdef CONFIG_FUSE_DAX
+       if (fc->dax)
+               seq_puts(m, ",dax");
+#endif
+
        return 0;
 }
 
@@ -651,6 +656,8 @@ void fuse_conn_put(struct fuse_conn *fc)
        if (refcount_dec_and_test(&fc->count)) {
                struct fuse_iqueue *fiq = &fc->iq;
 
+               if (IS_ENABLED(CONFIG_FUSE_DAX))
+                       fuse_dax_conn_free(fc);
                if (fiq->ops->release)
                        fiq->ops->release(fiq);
                put_pid_ns(fc->pid_ns);
@@ -1175,11 +1182,17 @@ int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
        if (sb->s_user_ns != &init_user_ns)
                sb->s_xattr = fuse_no_acl_xattr_handlers;
 
+       if (IS_ENABLED(CONFIG_FUSE_DAX)) {
+               err = fuse_dax_conn_alloc(fc, ctx->dax_dev);
+               if (err)
+                       goto err;
+       }
+
        if (ctx->fudptr) {
                err = -ENOMEM;
                fud = fuse_dev_alloc_install(fc);
                if (!fud)
-                       goto err;
+                       goto err_free_dax;
        }
 
        fc->dev = sb->s_dev;
@@ -1234,6 +1247,9 @@ int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
  err_dev_free:
        if (fud)
                fuse_dev_free(fud);
+ err_free_dax:
+       if (IS_ENABLED(CONFIG_FUSE_DAX))
+               fuse_dax_conn_free(fc);
  err:
        return err;
 }
index f31a59f..e25f622 100644 (file)
@@ -13,6 +13,7 @@
 #include <linux/virtio_fs.h>
 #include <linux/delay.h>
 #include <linux/fs_context.h>
+#include <linux/fs_parser.h>
 #include <linux/highmem.h>
 #include <linux/uio.h>
 #include "fuse_i.h"
@@ -81,6 +82,44 @@ struct virtio_fs_req_work {
 static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq,
                                 struct fuse_req *req, bool in_flight);
 
+enum {
+       OPT_DAX,
+};
+
+static const struct fs_parameter_spec virtio_fs_parameters[] = {
+       fsparam_flag("dax", OPT_DAX),
+       {}
+};
+
+static int virtio_fs_parse_param(struct fs_context *fc,
+                                struct fs_parameter *param)
+{
+       struct fs_parse_result result;
+       struct fuse_fs_context *ctx = fc->fs_private;
+       int opt;
+
+       opt = fs_parse(fc, virtio_fs_parameters, param, &result);
+       if (opt < 0)
+               return opt;
+
+       switch (opt) {
+       case OPT_DAX:
+               ctx->dax = 1;
+               break;
+       default:
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+static void virtio_fs_free_fc(struct fs_context *fc)
+{
+       struct fuse_fs_context *ctx = fc->fs_private;
+
+       kfree(ctx);
+}
+
 static inline struct virtio_fs_vq *vq_to_fsvq(struct virtqueue *vq)
 {
        struct virtio_fs *fs = vq->vdev->priv;
@@ -1219,23 +1258,27 @@ static const struct fuse_iqueue_ops virtio_fs_fiq_ops = {
        .release                        = virtio_fs_fiq_release,
 };
 
-static int virtio_fs_fill_super(struct super_block *sb)
+static inline void virtio_fs_ctx_set_defaults(struct fuse_fs_context *ctx)
+{
+       ctx->rootmode = S_IFDIR;
+       ctx->default_permissions = 1;
+       ctx->allow_other = 1;
+       ctx->max_read = UINT_MAX;
+       ctx->blksize = 512;
+       ctx->destroy = true;
+       ctx->no_control = true;
+       ctx->no_force_umount = true;
+}
+
+static int virtio_fs_fill_super(struct super_block *sb, struct fs_context *fsc)
 {
        struct fuse_conn *fc = get_fuse_conn_super(sb);
        struct virtio_fs *fs = fc->iq.priv;
+       struct fuse_fs_context *ctx = fsc->fs_private;
        unsigned int i;
        int err;
-       struct fuse_fs_context ctx = {
-               .rootmode = S_IFDIR,
-               .default_permissions = 1,
-               .allow_other = 1,
-               .max_read = UINT_MAX,
-               .blksize = 512,
-               .destroy = true,
-               .no_control = true,
-               .no_force_umount = true,
-       };
 
+       virtio_fs_ctx_set_defaults(ctx);
        mutex_lock(&virtio_fs_mutex);
 
        /* After holding mutex, make sure virtiofs device is still there.
@@ -1259,8 +1302,10 @@ static int virtio_fs_fill_super(struct super_block *sb)
        }
 
        /* virtiofs allocates and installs its own fuse devices */
-       ctx.fudptr = NULL;
-       err = fuse_fill_super_common(sb, &ctx);
+       ctx->fudptr = NULL;
+       if (ctx->dax)
+               ctx->dax_dev = fs->dax_dev;
+       err = fuse_fill_super_common(sb, ctx);
        if (err < 0)
                goto err_free_fuse_devs;
 
@@ -1371,7 +1416,7 @@ static int virtio_fs_get_tree(struct fs_context *fsc)
                return PTR_ERR(sb);
 
        if (!sb->s_root) {
-               err = virtio_fs_fill_super(sb);
+               err = virtio_fs_fill_super(sb, fsc);
                if (err) {
                        deactivate_locked_super(sb);
                        return err;
@@ -1386,11 +1431,19 @@ static int virtio_fs_get_tree(struct fs_context *fsc)
 }
 
 static const struct fs_context_operations virtio_fs_context_ops = {
+       .free           = virtio_fs_free_fc,
+       .parse_param    = virtio_fs_parse_param,
        .get_tree       = virtio_fs_get_tree,
 };
 
 static int virtio_fs_init_fs_context(struct fs_context *fsc)
 {
+       struct fuse_fs_context *ctx;
+
+       ctx = kzalloc(sizeof(struct fuse_fs_context), GFP_KERNEL);
+       if (!ctx)
+               return -ENOMEM;
+       fsc->fs_private = ctx;
        fsc->ops = &virtio_fs_context_ops;
        return 0;
 }