]> git.proxmox.com Git - mirror_zfs.git/blobdiff - module/zfs/vdev_file.c
OpenZFS 7614, 9064 - zfs device evacuation/removal
[mirror_zfs.git] / module / zfs / vdev_file.c
index 8c22aa5316a1178b4f402ea22978622cb8803b80..bd7e0bc2e39201a18ebb1dd99ba10aff9d4bb156 100644 (file)
  */
 /*
  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
  */
 
 #include <sys/zfs_context.h>
 #include <sys/spa.h>
+#include <sys/spa_impl.h>
 #include <sys/vdev_file.h>
 #include <sys/vdev_impl.h>
 #include <sys/zio.h>
 #include <sys/fs/zfs.h>
 #include <sys/fm/fs/zfs.h>
+#include <sys/abd.h>
 
 /*
  * Virtual device vector for files.
  */
 
+static taskq_t *vdev_file_taskq;
+
 static void
 vdev_file_hold(vdev_t *vd)
 {
@@ -47,19 +52,23 @@ vdev_file_rele(vdev_t *vd)
 }
 
 static int
-vdev_file_open(vdev_t *vd, uint64_t *psize, uint64_t *ashift)
+vdev_file_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
+    uint64_t *ashift)
 {
        vdev_file_t *vf;
        vnode_t *vp;
        vattr_t vattr;
        int error;
 
+       /* Rotational optimizations only make sense on block devices */
+       vd->vdev_nonrot = B_TRUE;
+
        /*
         * We must have a pathname, and it must be absolute.
         */
        if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
                vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
-               return (EINVAL);
+               return (SET_ERROR(EINVAL));
        }
 
        /*
@@ -97,7 +106,7 @@ vdev_file_open(vdev_t *vd, uint64_t *psize, uint64_t *ashift)
         */
        if (vp->v_type != VREG) {
                vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
-               return (ENODEV);
+               return (SET_ERROR(ENODEV));
        }
 #endif
 
@@ -112,7 +121,7 @@ skip_open:
                return (error);
        }
 
-       *psize = vattr.va_size;
+       *max_psize = *psize = vattr.va_size;
        *ashift = SPA_MINBLOCKSHIFT;
 
        return (0);
@@ -130,7 +139,6 @@ vdev_file_close(vdev_t *vd)
                (void) VOP_PUTPAGE(vf->vf_vnode, 0, 0, B_INVAL, kcred, NULL);
                (void) VOP_CLOSE(vf->vf_vnode, spa_mode(vd->vdev_spa), 1, 0,
                    kcred, NULL);
-               VN_RELE(vf->vf_vnode);
        }
 
        vd->vdev_delayed_close = B_FALSE;
@@ -138,43 +146,95 @@ vdev_file_close(vdev_t *vd)
        vd->vdev_tsd = NULL;
 }
 
-static int
-vdev_file_io_start(zio_t *zio)
+static void
+vdev_file_io_strategy(void *arg)
 {
+       zio_t *zio = (zio_t *)arg;
        vdev_t *vd = zio->io_vd;
        vdev_file_t *vf = vd->vdev_tsd;
        ssize_t resid;
+       void *buf;
+
+       if (zio->io_type == ZIO_TYPE_READ)
+               buf = abd_borrow_buf(zio->io_abd, zio->io_size);
+       else
+               buf = abd_borrow_buf_copy(zio->io_abd, zio->io_size);
+
+       zio->io_error = vn_rdwr(zio->io_type == ZIO_TYPE_READ ?
+           UIO_READ : UIO_WRITE, vf->vf_vnode, buf, zio->io_size,
+           zio->io_offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
+
+       if (zio->io_type == ZIO_TYPE_READ)
+               abd_return_buf_copy(zio->io_abd, buf, zio->io_size);
+       else
+               abd_return_buf(zio->io_abd, buf, zio->io_size);
+
+       if (resid != 0 && zio->io_error == 0)
+               zio->io_error = SET_ERROR(ENOSPC);
+
+       zio_delay_interrupt(zio);
+}
+
+static void
+vdev_file_io_fsync(void *arg)
+{
+       zio_t *zio = (zio_t *)arg;
+       vdev_file_t *vf = zio->io_vd->vdev_tsd;
+
+       zio->io_error = VOP_FSYNC(vf->vf_vnode, FSYNC | FDSYNC, kcred, NULL);
+
+       zio_interrupt(zio);
+}
+
+static void
+vdev_file_io_start(zio_t *zio)
+{
+       vdev_t *vd = zio->io_vd;
+       vdev_file_t *vf = vd->vdev_tsd;
 
        if (zio->io_type == ZIO_TYPE_IOCTL) {
                /* XXPOLICY */
                if (!vdev_readable(vd)) {
-                       zio->io_error = ENXIO;
-                       return (ZIO_PIPELINE_CONTINUE);
+                       zio->io_error = SET_ERROR(ENXIO);
+                       zio_interrupt(zio);
+                       return;
                }
 
                switch (zio->io_cmd) {
                case DKIOCFLUSHWRITECACHE:
+
+                       if (zfs_nocacheflush)
+                               break;
+
+                       /*
+                        * We cannot safely call vfs_fsync() when PF_FSTRANS
+                        * is set in the current context.  Filesystems like
+                        * XFS include sanity checks to verify it is not
+                        * already set, see xfs_vm_writepage().  Therefore
+                        * the sync must be dispatched to a different context.
+                        */
+                       if (__spl_pf_fstrans_check()) {
+                               VERIFY3U(taskq_dispatch(vdev_file_taskq,
+                                   vdev_file_io_fsync, zio, TQ_SLEEP), !=,
+                                   TASKQID_INVALID);
+                               return;
+                       }
+
                        zio->io_error = VOP_FSYNC(vf->vf_vnode, FSYNC | FDSYNC,
                            kcred, NULL);
                        break;
                default:
-                       zio->io_error = ENOTSUP;
+                       zio->io_error = SET_ERROR(ENOTSUP);
                }
 
-               return (ZIO_PIPELINE_CONTINUE);
+               zio_execute(zio);
+               return;
        }
 
-       zio->io_error = vn_rdwr(zio->io_type == ZIO_TYPE_READ ?
-           UIO_READ : UIO_WRITE, vf->vf_vnode, zio->io_data,
-           zio->io_size, zio->io_offset, UIO_SYSSPACE,
-           0, RLIM64_INFINITY, kcred, &resid);
-
-       if (resid != 0 && zio->io_error == 0)
-               zio->io_error = ENOSPC;
-
-       zio_interrupt(zio);
+       zio->io_target_timestamp = zio_handle_io_delay(zio);
 
-       return (ZIO_PIPELINE_STOP);
+       VERIFY3U(taskq_dispatch(vdev_file_taskq, vdev_file_io_strategy, zio,
+           TQ_SLEEP), !=, TASKQID_INVALID);
 }
 
 /* ARGSUSED */
@@ -190,12 +250,29 @@ vdev_ops_t vdev_file_ops = {
        vdev_file_io_start,
        vdev_file_io_done,
        NULL,
+       NULL,
        vdev_file_hold,
        vdev_file_rele,
+       NULL,
        VDEV_TYPE_FILE,         /* name of this vdev type */
        B_TRUE                  /* leaf vdev */
 };
 
+void
+vdev_file_init(void)
+{
+       vdev_file_taskq = taskq_create("z_vdev_file", MAX(boot_ncpus, 16),
+           minclsyspri, boot_ncpus, INT_MAX, TASKQ_DYNAMIC);
+
+       VERIFY(vdev_file_taskq);
+}
+
+void
+vdev_file_fini(void)
+{
+       taskq_destroy(vdev_file_taskq);
+}
+
 /*
  * From userland we access disks just like files.
  */
@@ -208,8 +285,10 @@ vdev_ops_t vdev_disk_ops = {
        vdev_file_io_start,
        vdev_file_io_done,
        NULL,
+       NULL,
        vdev_file_hold,
        vdev_file_rele,
+       NULL,
        VDEV_TYPE_DISK,         /* name of this vdev type */
        B_TRUE                  /* leaf vdev */
 };