]> git.proxmox.com Git - mirror_zfs.git/blobdiff - module/zfs/zvol.c
Introduce ARC Buffer Data (ABD)
[mirror_zfs.git] / module / zfs / zvol.c
index 43a7bb69f0bdd98f1da70193a473533f5061fac4..ea6997b5b9e979902a59bf1fba095e99b2e715d5 100644 (file)
  *
  * Volumes are persistent through reboot and module load.  No user command
  * needs to be run before opening and using a device.
+ *
+ * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
+ * Copyright (c) 2016 Actifio, Inc. All rights reserved.
  */
 
+#include <sys/dbuf.h>
 #include <sys/dmu_traverse.h>
 #include <sys/dsl_dataset.h>
 #include <sys/dsl_prop.h>
+#include <sys/dsl_dir.h>
 #include <sys/zap.h>
+#include <sys/zfeature.h>
 #include <sys/zil_impl.h>
+#include <sys/dmu_tx.h>
 #include <sys/zio.h>
 #include <sys/zfs_rlock.h>
 #include <sys/zfs_znode.h>
+#include <sys/spa_impl.h>
 #include <sys/zvol.h>
 #include <linux/blkdev_compat.h>
 
 unsigned int zvol_inhibit_dev = 0;
 unsigned int zvol_major = ZVOL_MAJOR;
-unsigned int zvol_threads = 32;
+unsigned int zvol_prefetch_bytes = (128 * 1024);
 unsigned long zvol_max_discard_blocks = 16384;
 
-static taskq_t *zvol_taskq;
 static kmutex_t zvol_state_lock;
 static list_t zvol_state_list;
-static char *zvol_tag = "zvol_tag";
+void *zvol_tag = "zvol_tag";
 
 /*
  * The in-core state of each volume.
  */
 typedef struct zvol_state {
        char                    zv_name[MAXNAMELEN];    /* name */
-       uint64_t                zv_volsize;     /* advertised space */
-       uint64_t                zv_volblocksize;/* volume block size */
+       uint64_t                zv_volsize;             /* advertised space */
+       uint64_t                zv_volblocksize;        /* volume block size */
        objset_t                *zv_objset;     /* objset handle */
        uint32_t                zv_flags;       /* ZVOL_* flags */
        uint32_t                zv_open_count;  /* open counts */
        uint32_t                zv_changed;     /* disk changed */
        zilog_t                 *zv_zilog;      /* ZIL handle */
-       znode_t                 zv_znode;       /* for range locking */
+       zfs_rlock_t             zv_range_lock;  /* range lock */
        dmu_buf_t               *zv_dbuf;       /* bonus handle */
        dev_t                   zv_dev;         /* device id */
        struct gendisk          *zv_disk;       /* generic disk */
        struct request_queue    *zv_queue;      /* request queue */
-       spinlock_t              zv_lock;        /* request queue lock */
        list_node_t             zv_next;        /* next zvol_state_t linkage */
 } zvol_state_t;
 
+typedef enum {
+       ZVOL_ASYNC_CREATE_MINORS,
+       ZVOL_ASYNC_REMOVE_MINORS,
+       ZVOL_ASYNC_RENAME_MINORS,
+       ZVOL_ASYNC_SET_SNAPDEV,
+       ZVOL_ASYNC_MAX
+} zvol_async_op_t;
+
+typedef struct {
+       zvol_async_op_t op;
+       char pool[MAXNAMELEN];
+       char name1[MAXNAMELEN];
+       char name2[MAXNAMELEN];
+       zprop_source_t source;
+       uint64_t snapdev;
+} zvol_task_t;
+
 #define        ZVOL_RDONLY     0x1
 
 /*
@@ -93,16 +116,16 @@ zvol_find_minor(unsigned *minor)
        *minor = 0;
        ASSERT(MUTEX_HELD(&zvol_state_lock));
        for (zv = list_head(&zvol_state_list); zv != NULL;
-            zv = list_next(&zvol_state_list, zv), *minor += ZVOL_MINORS) {
+           zv = list_next(&zvol_state_list, zv), *minor += ZVOL_MINORS) {
                if (MINOR(zv->zv_dev) != MINOR(*minor))
                        break;
        }
 
        /* All minors are in use */
        if (*minor >= (1 << MINORBITS))
-               return ENXIO;
+               return (SET_ERROR(ENXIO));
 
-       return 0;
+       return (0);
 }
 
 /*
@@ -115,12 +138,12 @@ zvol_find_by_dev(dev_t dev)
 
        ASSERT(MUTEX_HELD(&zvol_state_lock));
        for (zv = list_head(&zvol_state_list); zv != NULL;
-            zv = list_next(&zvol_state_list, zv)) {
+           zv = list_next(&zvol_state_list, zv)) {
                if (zv->zv_dev == dev)
-                       return zv;
+                       return (zv);
        }
 
-       return NULL;
+       return (NULL);
 }
 
 /*
@@ -133,12 +156,12 @@ zvol_find_by_name(const char *name)
 
        ASSERT(MUTEX_HELD(&zvol_state_lock));
        for (zv = list_head(&zvol_state_list); zv != NULL;
-            zv = list_next(&zvol_state_list, zv)) {
-               if (!strncmp(zv->zv_name, name, MAXNAMELEN))
-                       return zv;
+           zv = list_next(&zvol_state_list, zv)) {
+               if (strncmp(zv->zv_name, name, MAXNAMELEN) == 0)
+                       return (zv);
        }
 
-       return NULL;
+       return (NULL);
 }
 
 
@@ -151,7 +174,7 @@ zvol_is_zvol(const char *device)
        struct block_device *bdev;
        unsigned int major;
 
-       bdev = lookup_bdev(device);
+       bdev = vdev_lookup_bdev(device);
        if (IS_ERR(bdev))
                return (B_FALSE);
 
@@ -159,7 +182,7 @@ zvol_is_zvol(const char *device)
        bdput(bdev);
 
        if (major == zvol_major)
-            return (B_TRUE);
+               return (B_TRUE);
 
        return (B_FALSE);
 }
@@ -214,10 +237,10 @@ zvol_get_stats(objset_t *os, nvlist_t *nv)
 
        error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
        if (error)
-               return (error);
+               return (SET_ERROR(error));
 
        dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
-       doi = kmem_alloc(sizeof(dmu_object_info_t), KM_SLEEP);
+       doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
        error = dmu_object_info(os, ZVOL_OBJ, doi);
 
        if (error == 0) {
@@ -225,9 +248,24 @@ zvol_get_stats(objset_t *os, nvlist_t *nv)
                    doi->doi_data_block_size);
        }
 
-       kmem_free(doi, sizeof(dmu_object_info_t));
+       kmem_free(doi, sizeof (dmu_object_info_t));
 
-       return (error);
+       return (SET_ERROR(error));
+}
+
+static void
+zvol_size_changed(zvol_state_t *zv, uint64_t volsize)
+{
+       struct block_device *bdev;
+
+       bdev = bdget_disk(zv->zv_disk, 0);
+       if (bdev == NULL)
+               return;
+       set_capacity(zv->zv_disk, volsize >> 9);
+       zv->zv_volsize = volsize;
+       check_disk_size_change(zv->zv_disk, bdev);
+
+       bdput(bdev);
 }
 
 /*
@@ -237,14 +275,14 @@ int
 zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
 {
        if (volsize == 0)
-               return (EINVAL);
+               return (SET_ERROR(EINVAL));
 
        if (volsize % blocksize != 0)
-               return (EINVAL);
+               return (SET_ERROR(EINVAL));
 
 #ifdef _ILP32
-       if (volsize - 1 > MAXOFFSET_T)
-               return (EOVERFLOW);
+       if (volsize - 1 > SPEC_MAXOFFSET_T)
+               return (SET_ERROR(EOVERFLOW));
 #endif
        return (0);
 }
@@ -253,52 +291,47 @@ zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
  * Ensure the zap is flushed then inform the VFS of the capacity change.
  */
 static int
-zvol_update_volsize(zvol_state_t *zv, uint64_t volsize, objset_t *os)
+zvol_update_volsize(uint64_t volsize, objset_t *os)
 {
-       struct block_device *bdev;
        dmu_tx_t *tx;
        int error;
+       uint64_t txg;
 
        ASSERT(MUTEX_HELD(&zvol_state_lock));
 
        tx = dmu_tx_create(os);
        dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
+       dmu_tx_mark_netfree(tx);
        error = dmu_tx_assign(tx, TXG_WAIT);
        if (error) {
                dmu_tx_abort(tx);
-               return (error);
+               return (SET_ERROR(error));
        }
+       txg = dmu_tx_get_txg(tx);
 
        error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
            &volsize, tx);
        dmu_tx_commit(tx);
 
-       if (error)
-               return (error);
+       txg_wait_synced(dmu_objset_pool(os), txg);
 
-       error = dmu_free_long_range(os,
-           ZVOL_OBJ, volsize, DMU_OBJECT_END);
-       if (error)
-               return (error);
+       if (error == 0)
+               error = dmu_free_long_range(os,
+                   ZVOL_OBJ, volsize, DMU_OBJECT_END);
 
-       bdev = bdget_disk(zv->zv_disk, 0);
-       if (!bdev)
-               return (EIO);
-/*
- * 2.6.28 API change
- * Added check_disk_size_change() helper function.
- */
-#ifdef HAVE_CHECK_DISK_SIZE_CHANGE
-       set_capacity(zv->zv_disk, volsize >> 9);
-       zv->zv_volsize = volsize;
-       check_disk_size_change(zv->zv_disk, bdev);
-#else
-       zv->zv_volsize = volsize;
-       zv->zv_changed = 1;
-       (void) check_disk_change(bdev);
-#endif /* HAVE_CHECK_DISK_SIZE_CHANGE */
+       return (error);
+}
 
-       bdput(bdev);
+static int
+zvol_update_live_volsize(zvol_state_t *zv, uint64_t volsize)
+{
+       zvol_size_changed(zv, volsize);
+
+       /*
+        * We should post a event here describing the expansion.  However,
+        * the zfs_ereport_post() interface doesn't nicely support posting
+        * events for zvols, it assumes events relate to vdevs or zios.
+        */
 
        return (0);
 }
@@ -309,50 +342,54 @@ zvol_update_volsize(zvol_state_t *zv, uint64_t volsize, objset_t *os)
 int
 zvol_set_volsize(const char *name, uint64_t volsize)
 {
-       zvol_state_t *zv;
-       dmu_object_info_t *doi;
+       zvol_state_t *zv = NULL;
        objset_t *os = NULL;
-       uint64_t readonly;
        int error;
+       dmu_object_info_t *doi;
+       uint64_t readonly;
+       boolean_t owned = B_FALSE;
 
-       mutex_enter(&zvol_state_lock);
+       error = dsl_prop_get_integer(name,
+           zfs_prop_to_name(ZFS_PROP_READONLY), &readonly, NULL);
+       if (error != 0)
+               return (SET_ERROR(error));
+       if (readonly)
+               return (SET_ERROR(EROFS));
 
+       mutex_enter(&zvol_state_lock);
        zv = zvol_find_by_name(name);
-       if (zv == NULL) {
-               error = ENXIO;
-               goto out;
-       }
 
-       doi = kmem_alloc(sizeof(dmu_object_info_t), KM_SLEEP);
+       if (zv == NULL || zv->zv_objset == NULL) {
+               if ((error = dmu_objset_own(name, DMU_OST_ZVOL, B_FALSE,
+                   FTAG, &os)) != 0) {
+                       mutex_exit(&zvol_state_lock);
+                       return (SET_ERROR(error));
+               }
+               owned = B_TRUE;
+               if (zv != NULL)
+                       zv->zv_objset = os;
+       } else {
+               os = zv->zv_objset;
+       }
 
-       error = dmu_objset_hold(name, FTAG, &os);
-       if (error)
-               goto out_doi;
+       doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
 
-       if ((error = dmu_object_info(os, ZVOL_OBJ, doi)) != 0 ||
-           (error = zvol_check_volsize(volsize,doi->doi_data_block_size)) != 0)
-               goto out_doi;
+       if ((error = dmu_object_info(os, ZVOL_OBJ, doi)) ||
+           (error = zvol_check_volsize(volsize, doi->doi_data_block_size)))
+               goto out;
 
-       VERIFY(dsl_prop_get_integer(name, "readonly", &readonly, NULL) == 0);
-       if (readonly) {
-               error = EROFS;
-               goto out_doi;
-       }
+       error = zvol_update_volsize(volsize, os);
+       kmem_free(doi, sizeof (dmu_object_info_t));
 
-       if (get_disk_ro(zv->zv_disk) || (zv->zv_flags & ZVOL_RDONLY)) {
-               error = EROFS;
-               goto out_doi;
-       }
-
-       error = zvol_update_volsize(zv, volsize, os);
-out_doi:
-       kmem_free(doi, sizeof(dmu_object_info_t));
+       if (error == 0 && zv != NULL)
+               error = zvol_update_live_volsize(zv, volsize);
 out:
-       if (os)
-               dmu_objset_rele(os, FTAG);
-
+       if (owned) {
+               dmu_objset_disown(os, FTAG);
+               if (zv != NULL)
+                       zv->zv_objset = NULL;
+       }
        mutex_exit(&zvol_state_lock);
-
        return (error);
 }
 
@@ -360,12 +397,35 @@ out:
  * Sanity check volume block size.
  */
 int
-zvol_check_volblocksize(uint64_t volblocksize)
+zvol_check_volblocksize(const char *name, uint64_t volblocksize)
 {
+       /* Record sizes above 128k need the feature to be enabled */
+       if (volblocksize > SPA_OLD_MAXBLOCKSIZE) {
+               spa_t *spa;
+               int error;
+
+               if ((error = spa_open(name, &spa, FTAG)) != 0)
+                       return (error);
+
+               if (!spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) {
+                       spa_close(spa, FTAG);
+                       return (SET_ERROR(ENOTSUP));
+               }
+
+               /*
+                * We don't allow setting the property above 1MB,
+                * unless the tunable has been changed.
+                */
+               if (volblocksize > zfs_max_recordsize)
+                       return (SET_ERROR(EDOM));
+
+               spa_close(spa, FTAG);
+       }
+
        if (volblocksize < SPA_MINBLOCKSIZE ||
            volblocksize > SPA_MAXBLOCKSIZE ||
            !ISP2(volblocksize))
-               return (EDOM);
+               return (SET_ERROR(EDOM));
 
        return (0);
 }
@@ -384,12 +444,12 @@ zvol_set_volblocksize(const char *name, uint64_t volblocksize)
 
        zv = zvol_find_by_name(name);
        if (zv == NULL) {
-               error = ENXIO;
+               error = SET_ERROR(ENXIO);
                goto out;
        }
 
-       if (get_disk_ro(zv->zv_disk) || (zv->zv_flags & ZVOL_RDONLY)) {
-               error = EROFS;
+       if (zv->zv_flags & ZVOL_RDONLY) {
+               error = SET_ERROR(EROFS);
                goto out;
        }
 
@@ -402,7 +462,7 @@ zvol_set_volblocksize(const char *name, uint64_t volblocksize)
                error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ,
                    volblocksize, 0, tx);
                if (error == ENOTSUP)
-                       error = EBUSY;
+                       error = SET_ERROR(EBUSY);
                dmu_tx_commit(tx);
                if (error == 0)
                        zv->zv_volblocksize = volblocksize;
@@ -410,7 +470,25 @@ zvol_set_volblocksize(const char *name, uint64_t volblocksize)
 out:
        mutex_exit(&zvol_state_lock);
 
-       return (error);
+       return (SET_ERROR(error));
+}
+
+/*
+ * Replay a TX_TRUNCATE ZIL transaction if asked.  TX_TRUNCATE is how we
+ * implement DKIOCFREE/free-long-range.
+ */
+static int
+zvol_replay_truncate(zvol_state_t *zv, lr_truncate_t *lr, boolean_t byteswap)
+{
+       uint64_t offset, length;
+
+       if (byteswap)
+               byteswap_uint64_array(lr, sizeof (*lr));
+
+       offset = lr->lr_offset;
+       length = lr->lr_length;
+
+       return (dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, offset, length));
 }
 
 /*
@@ -440,18 +518,18 @@ zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
                dmu_tx_commit(tx);
        }
 
-       return (error);
+       return (SET_ERROR(error));
 }
 
 static int
 zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
 {
-       return (ENOTSUP);
+       return (SET_ERROR(ENOTSUP));
 }
 
 /*
  * Callback vectors for replaying records.
- * Only TX_WRITE is needed for zvol.
+ * Only TX_WRITE and TX_TRUNCATE are needed for zvol.
  */
 zil_replay_func_t zvol_replay_vector[TX_MAX_TYPE] = {
        (zil_replay_func_t)zvol_replay_err,     /* no such transaction type */
@@ -464,7 +542,7 @@ zil_replay_func_t zvol_replay_vector[TX_MAX_TYPE] = {
        (zil_replay_func_t)zvol_replay_err,     /* TX_LINK */
        (zil_replay_func_t)zvol_replay_err,     /* TX_RENAME */
        (zil_replay_func_t)zvol_replay_write,   /* TX_WRITE */
-       (zil_replay_func_t)zvol_replay_err,     /* TX_TRUNCATE */
+       (zil_replay_func_t)zvol_replay_truncate, /* TX_TRUNCATE */
        (zil_replay_func_t)zvol_replay_err,     /* TX_SETATTR */
        (zil_replay_func_t)zvol_replay_err,     /* TX_ACL */
 };
@@ -478,8 +556,8 @@ zil_replay_func_t zvol_replay_vector[TX_MAX_TYPE] = {
 ssize_t zvol_immediate_write_sz = 32768;
 
 static void
-zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx,
-              uint64_t offset, uint64_t size, int sync)
+zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, uint64_t offset,
+    uint64_t size, int sync)
 {
        uint32_t blocksize = zv->zv_volblocksize;
        zilog_t *zilog = zv->zv_zilog;
@@ -546,244 +624,220 @@ zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx,
        }
 }
 
-/*
- * Common write path running under the zvol taskq context.  This function
- * is responsible for copying the request structure data in to the DMU and
- * signaling the request queue with the result of the copy.
- */
-static void
-zvol_write(void *arg)
+static int
+zvol_write(zvol_state_t *zv, uio_t *uio, boolean_t sync)
 {
-       struct request *req = (struct request *)arg;
-       struct request_queue *q = req->q;
-       zvol_state_t *zv = q->queuedata;
-       uint64_t offset = blk_rq_pos(req) << 9;
-       uint64_t size = blk_rq_bytes(req);
-       int error = 0;
-       dmu_tx_t *tx;
+       uint64_t volsize = zv->zv_volsize;
        rl_t *rl;
+       int error = 0;
 
-       /*
-        * Annotate this call path with a flag that indicates that it is
-        * unsafe to use KM_SLEEP during memory allocations due to the
-        * potential for a deadlock.  KM_PUSHPAGE should be used instead.
-        */
-       ASSERT(!(current->flags & PF_NOFS));
-       current->flags |= PF_NOFS;
+       ASSERT(zv && zv->zv_open_count > 0);
 
-       if (req->cmd_flags & VDEV_REQ_FLUSH)
-               zil_commit(zv->zv_zilog, ZVOL_OBJ);
+       rl = zfs_range_lock(&zv->zv_range_lock, uio->uio_loffset,
+           uio->uio_resid, RL_WRITER);
 
-       /*
-        * Some requests are just for flush and nothing else.
-        */
-       if (size == 0) {
-               blk_end_request(req, 0, size);
-               goto out;
-       }
+       while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
+               uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
+               uint64_t off = uio->uio_loffset;
+               dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
 
-       rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_WRITER);
+               if (bytes > volsize - off)      /* don't write past the end */
+                       bytes = volsize - off;
 
-       tx = dmu_tx_create(zv->zv_objset);
-       dmu_tx_hold_write(tx, ZVOL_OBJ, offset, size);
+               dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
 
-       /* This will only fail for ENOSPC */
-       error = dmu_tx_assign(tx, TXG_WAIT);
-       if (error) {
-               dmu_tx_abort(tx);
-               zfs_range_unlock(rl);
-               blk_end_request(req, -error, size);
-               goto out;
+               /* This will only fail for ENOSPC */
+               error = dmu_tx_assign(tx, TXG_WAIT);
+               if (error) {
+                       dmu_tx_abort(tx);
+                       break;
+               }
+               error = dmu_write_uio_dbuf(zv->zv_dbuf, uio, bytes, tx);
+               if (error == 0)
+                       zvol_log_write(zv, tx, off, bytes, sync);
+               dmu_tx_commit(tx);
+
+               if (error)
+                       break;
        }
+       zfs_range_unlock(rl);
+       if (sync)
+               zil_commit(zv->zv_zilog, ZVOL_OBJ);
+       return (error);
+}
 
-       error = dmu_write_req(zv->zv_objset, ZVOL_OBJ, req, tx);
-       if (error == 0)
-               zvol_log_write(zv, tx, offset, size,
-                   req->cmd_flags & VDEV_REQ_FUA);
+/*
+ * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE.
+ */
+static void
+zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off, uint64_t len,
+    boolean_t sync)
+{
+       itx_t *itx;
+       lr_truncate_t *lr;
+       zilog_t *zilog = zv->zv_zilog;
 
-       dmu_tx_commit(tx);
-       zfs_range_unlock(rl);
+       if (zil_replaying(zilog, tx))
+               return;
 
-       if ((req->cmd_flags & VDEV_REQ_FUA) ||
-           zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)
-               zil_commit(zv->zv_zilog, ZVOL_OBJ);
+       itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
+       lr = (lr_truncate_t *)&itx->itx_lr;
+       lr->lr_foid = ZVOL_OBJ;
+       lr->lr_offset = off;
+       lr->lr_length = len;
 
-       blk_end_request(req, -error, size);
-out:
-       current->flags &= ~PF_NOFS;
+       itx->itx_sync = sync;
+       zil_itx_assign(zilog, itx, tx);
 }
 
-#ifdef HAVE_BLK_QUEUE_DISCARD
-static void
-zvol_discard(void *arg)
+static int
+zvol_discard(struct bio *bio)
 {
-       struct request *req = (struct request *)arg;
-       struct request_queue *q = req->q;
-       zvol_state_t *zv = q->queuedata;
-       uint64_t start = blk_rq_pos(req) << 9;
-       uint64_t end = start + blk_rq_bytes(req);
+       zvol_state_t *zv = bio->bi_bdev->bd_disk->private_data;
+       uint64_t start = BIO_BI_SECTOR(bio) << 9;
+       uint64_t size = BIO_BI_SIZE(bio);
+       uint64_t end = start + size;
        int error;
        rl_t *rl;
+       dmu_tx_t *tx;
 
-       /*
-        * Annotate this call path with a flag that indicates that it is
-        * unsafe to use KM_SLEEP during memory allocations due to the
-        * potential for a deadlock.  KM_PUSHPAGE should be used instead.
-        */
-       ASSERT(!(current->flags & PF_NOFS));
-       current->flags |= PF_NOFS;
+       ASSERT(zv && zv->zv_open_count > 0);
 
-       if (end > zv->zv_volsize) {
-               blk_end_request(req, -EIO, blk_rq_bytes(req));
-               goto out;
-       }
+       if (end > zv->zv_volsize)
+               return (SET_ERROR(EIO));
 
        /*
-        * Align the request to volume block boundaries. If we don't,
-        * then this will force dnode_free_range() to zero out the
-        * unaligned parts, which is slow (read-modify-write) and
-        * useless since we are not freeing any space by doing so.
+        * Align the request to volume block boundaries when a secure erase is
+        * not required.  This will prevent dnode_free_range() from zeroing out
+        * the unaligned parts which is slow (read-modify-write) and useless
+        * since we are not freeing any space by doing so.
         */
-       start = P2ROUNDUP(start, zv->zv_volblocksize);
-       end = P2ALIGN(end, zv->zv_volblocksize);
-
-       if (start >= end) {
-               blk_end_request(req, 0, blk_rq_bytes(req));
-               goto out;
+       if (!bio_is_secure_erase(bio)) {
+               start = P2ROUNDUP(start, zv->zv_volblocksize);
+               end = P2ALIGN(end, zv->zv_volblocksize);
+               size = end - start;
        }
 
-       rl = zfs_range_lock(&zv->zv_znode, start, end - start, RL_WRITER);
-
-       error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, start, end - start);
+       if (start >= end)
+               return (0);
 
-       /*
-        * TODO: maybe we should add the operation to the log.
-        */
+       rl = zfs_range_lock(&zv->zv_range_lock, start, size, RL_WRITER);
+       tx = dmu_tx_create(zv->zv_objset);
+       dmu_tx_mark_netfree(tx);
+       error = dmu_tx_assign(tx, TXG_WAIT);
+       if (error != 0) {
+               dmu_tx_abort(tx);
+       } else {
+               zvol_log_truncate(zv, tx, start, size, B_TRUE);
+               dmu_tx_commit(tx);
+               error = dmu_free_long_range(zv->zv_objset,
+                   ZVOL_OBJ, start, size);
+       }
 
        zfs_range_unlock(rl);
 
-       blk_end_request(req, -error, blk_rq_bytes(req));
-out:
-       current->flags &= ~PF_NOFS;
+       return (error);
 }
-#endif /* HAVE_BLK_QUEUE_DISCARD */
 
-/*
- * Common read path running under the zvol taskq context.  This function
- * is responsible for copying the requested data out of the DMU and in to
- * a linux request structure.  It then must signal the request queue with
- * an error code describing the result of the copy.
- */
-static void
-zvol_read(void *arg)
+static int
+zvol_read(zvol_state_t *zv, uio_t *uio)
 {
-       struct request *req = (struct request *)arg;
-       struct request_queue *q = req->q;
-       zvol_state_t *zv = q->queuedata;
-       uint64_t offset = blk_rq_pos(req) << 9;
-       uint64_t size = blk_rq_bytes(req);
-       int error;
+       uint64_t volsize = zv->zv_volsize;
        rl_t *rl;
+       int error = 0;
 
-       if (size == 0) {
-               blk_end_request(req, 0, size);
-               return;
-       }
+       ASSERT(zv && zv->zv_open_count > 0);
 
-       rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
+       rl = zfs_range_lock(&zv->zv_range_lock, uio->uio_loffset,
+           uio->uio_resid, RL_READER);
+       while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
+               uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
 
-       error = dmu_read_req(zv->zv_objset, ZVOL_OBJ, req);
+               /* don't read past the end */
+               if (bytes > volsize - uio->uio_loffset)
+                       bytes = volsize - uio->uio_loffset;
 
+               error = dmu_read_uio_dbuf(zv->zv_dbuf, uio, bytes);
+               if (error) {
+                       /* convert checksum errors into IO errors */
+                       if (error == ECKSUM)
+                               error = SET_ERROR(EIO);
+                       break;
+               }
+       }
        zfs_range_unlock(rl);
-
-       /* convert checksum errors into IO errors */
-       if (error == ECKSUM)
-               error = EIO;
-
-       blk_end_request(req, -error, size);
-}
-
-/*
- * Request will be added back to the request queue and retried if
- * it cannot be immediately dispatched to the taskq for handling
- */
-static inline void
-zvol_dispatch(task_func_t func, struct request *req)
-{
-       if (!taskq_dispatch(zvol_taskq, func, (void *)req, TQ_NOSLEEP))
-               blk_requeue_request(req->q, req);
+       return (error);
 }
 
-/*
- * Common request path.  Rather than registering a custom make_request()
- * function we use the generic Linux version.  This is done because it allows
- * us to easily merge read requests which would otherwise we performed
- * synchronously by the DMU.  This is less critical in write case where the
- * DMU will perform the correct merging within a transaction group.  Using
- * the generic make_request() also let's use leverage the fact that the
- * elevator with ensure correct ordering in regards to barrior IOs.  On
- * the downside it means that in the write case we end up doing request
- * merging twice once in the elevator and once in the DMU.
- *
- * The request handler is called under a spin lock so all the real work
- * is handed off to be done in the context of the zvol taskq.  This function
- * simply performs basic request sanity checking and hands off the request.
- */
-static void
-zvol_request(struct request_queue *q)
+static MAKE_REQUEST_FN_RET
+zvol_request(struct request_queue *q, struct bio *bio)
 {
+       uio_t uio;
        zvol_state_t *zv = q->queuedata;
-       struct request *req;
-       unsigned int size;
-
-       while ((req = blk_fetch_request(q)) != NULL) {
-               size = blk_rq_bytes(req);
-
-               if (size != 0 && blk_rq_pos(req) + blk_rq_sectors(req) >
-                   get_capacity(zv->zv_disk)) {
-                       printk(KERN_INFO
-                              "%s: bad access: block=%llu, count=%lu\n",
-                              req->rq_disk->disk_name,
-                              (long long unsigned)blk_rq_pos(req),
-                              (long unsigned)blk_rq_sectors(req));
-                       __blk_end_request(req, -EIO, size);
-                       continue;
-               }
+       fstrans_cookie_t cookie = spl_fstrans_mark();
+       int rw = bio_data_dir(bio);
+#ifdef HAVE_GENERIC_IO_ACCT
+       unsigned long start = jiffies;
+#endif
+       int error = 0;
 
-               if (!blk_fs_request(req)) {
-                       printk(KERN_INFO "%s: non-fs cmd\n",
-                              req->rq_disk->disk_name);
-                       __blk_end_request(req, -EIO, size);
-                       continue;
-               }
+       uio.uio_bvec = &bio->bi_io_vec[BIO_BI_IDX(bio)];
+       uio.uio_skip = BIO_BI_SKIP(bio);
+       uio.uio_resid = BIO_BI_SIZE(bio);
+       uio.uio_iovcnt = bio->bi_vcnt - BIO_BI_IDX(bio);
+       uio.uio_loffset = BIO_BI_SECTOR(bio) << 9;
+       uio.uio_limit = MAXOFFSET_T;
+       uio.uio_segflg = UIO_BVEC;
+
+       if (bio_has_data(bio) && uio.uio_loffset + uio.uio_resid >
+           zv->zv_volsize) {
+               printk(KERN_INFO
+                   "%s: bad access: offset=%llu, size=%lu\n",
+                   zv->zv_disk->disk_name,
+                   (long long unsigned)uio.uio_loffset,
+                   (long unsigned)uio.uio_resid);
+               error = SET_ERROR(EIO);
+               goto out1;
+       }
 
-               switch (rq_data_dir(req)) {
-               case READ:
-                       zvol_dispatch(zvol_read, req);
-                       break;
-               case WRITE:
-                       if (unlikely(get_disk_ro(zv->zv_disk)) ||
-                           unlikely(zv->zv_flags & ZVOL_RDONLY)) {
-                               __blk_end_request(req, -EROFS, size);
-                               break;
-                       }
+       generic_start_io_acct(rw, bio_sectors(bio), &zv->zv_disk->part0);
 
-#ifdef HAVE_BLK_QUEUE_DISCARD
-                       if (req->cmd_flags & VDEV_REQ_DISCARD) {
-                               zvol_dispatch(zvol_discard, req);
-                               break;
-                       }
-#endif /* HAVE_BLK_QUEUE_DISCARD */
+       if (rw == WRITE) {
+               if (unlikely(zv->zv_flags & ZVOL_RDONLY)) {
+                       error = SET_ERROR(EROFS);
+                       goto out2;
+               }
 
-                       zvol_dispatch(zvol_write, req);
-                       break;
-               default:
-                       printk(KERN_INFO "%s: unknown cmd: %d\n",
-                              req->rq_disk->disk_name, (int)rq_data_dir(req));
-                       __blk_end_request(req, -EIO, size);
-                       break;
+               if (bio_is_discard(bio) || bio_is_secure_erase(bio)) {
+                       error = zvol_discard(bio);
+                       goto out2;
                }
-       }
+
+               /*
+                * Some requests are just for flush and nothing else.
+                */
+               if (uio.uio_resid == 0) {
+                       if (bio_is_flush(bio))
+                               zil_commit(zv->zv_zilog, ZVOL_OBJ);
+                       goto out2;
+               }
+
+               error = zvol_write(zv, &uio,
+                   bio_is_flush(bio) || bio_is_fua(bio) ||
+                   zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS);
+       } else
+               error = zvol_read(zv, &uio);
+
+out2:
+       generic_end_io_acct(rw, &zv->zv_disk->part0, start);
+out1:
+       BIO_END_IO(bio, -error);
+       spl_fstrans_unmark(cookie);
+#ifdef HAVE_MAKE_REQUEST_FN_RET_INT
+       return (0);
+#elif defined(HAVE_MAKE_REQUEST_FN_RET_QC)
+       return (BLK_QC_T_NONE);
+#endif
 }
 
 static void
@@ -808,8 +862,10 @@ zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
 {
        zvol_state_t *zv = arg;
        objset_t *os = zv->zv_objset;
+       uint64_t object = ZVOL_OBJ;
        uint64_t offset = lr->lr_offset;
        uint64_t size = lr->lr_length;
+       blkptr_t *bp = &lr->lr_blkptr;
        dmu_buf_t *db;
        zgd_t *zgd;
        int error;
@@ -817,9 +873,10 @@ zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
        ASSERT(zio != NULL);
        ASSERT(size != 0);
 
-       zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_PUSHPAGE);
+       zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
        zgd->zgd_zilog = zv->zv_zilog;
-       zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
+       zgd->zgd_rl = zfs_range_lock(&zv->zv_range_lock, offset, size,
+           RL_READER);
 
        /*
         * Write records come in two flavors: immediate and indirect.
@@ -829,14 +886,20 @@ zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
         * we don't have to write the data twice.
         */
        if (buf != NULL) { /* immediate write */
-               error = dmu_read(os, ZVOL_OBJ, offset, size, buf,
+               error = dmu_read(os, object, offset, size, buf,
                    DMU_READ_NO_PREFETCH);
        } else {
                size = zv->zv_volblocksize;
                offset = P2ALIGN_TYPED(offset, size, uint64_t);
-               error = dmu_buf_hold(os, ZVOL_OBJ, offset, zgd, &db,
+               error = dmu_buf_hold(os, object, offset, zgd, &db,
                    DMU_READ_NO_PREFETCH);
                if (error == 0) {
+                       blkptr_t *obp = dmu_buf_get_blkptr(db);
+                       if (obp) {
+                               ASSERT(BP_IS_HOLE(bp));
+                               *bp = *obp;
+                       }
+
                        zgd->zgd_db = db;
                        zgd->zgd_bp = &lr->lr_blkptr;
 
@@ -854,7 +917,7 @@ zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
 
        zvol_get_done(zgd, error);
 
-       return (error);
+       return (SET_ERROR(error));
 }
 
 /*
@@ -868,7 +931,7 @@ zvol_insert(zvol_state_t *zv_insert)
        ASSERT(MUTEX_HELD(&zvol_state_lock));
        ASSERT3U(MINOR(zv_insert->zv_dev) & ZVOL_MINOR_MASK, ==, 0);
        for (zv = list_head(&zvol_state_list); zv != NULL;
-            zv = list_next(&zvol_state_list, zv)) {
+           zv = list_next(&zvol_state_list, zv)) {
                if (MINOR(zv->zv_dev) > MINOR(zv_insert->zv_dev))
                        break;
        }
@@ -891,56 +954,32 @@ zvol_first_open(zvol_state_t *zv)
 {
        objset_t *os;
        uint64_t volsize;
-       int locked = 0;
        int error;
        uint64_t ro;
 
-       /*
-        * In all other cases the spa_namespace_lock is taken before the
-        * bdev->bd_mutex lock.  But in this case the Linux __blkdev_get()
-        * function calls fops->open() with the bdev->bd_mutex lock held.
-        *
-        * To avoid a potential lock inversion deadlock we preemptively
-        * try to take the spa_namespace_lock().  Normally it will not
-        * be contended and this is safe because spa_open_common() handles
-        * the case where the caller already holds the spa_namespace_lock.
-        *
-        * When it is contended we risk a lock inversion if we were to
-        * block waiting for the lock.  Luckily, the __blkdev_get()
-        * function allows us to return -ERESTARTSYS which will result in
-        * bdev->bd_mutex being dropped, reacquired, and fops->open() being
-        * called again.  This process can be repeated safely until both
-        * locks are acquired.
-        */
-       if (!mutex_owned(&spa_namespace_lock)) {
-               locked = mutex_tryenter(&spa_namespace_lock);
-               if (!locked)
-                       return (-ERESTARTSYS);
-       }
-
        /* lie and say we're read-only */
        error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, 1, zvol_tag, &os);
        if (error)
-               goto out_mutex;
+               return (SET_ERROR(-error));
+
+       zv->zv_objset = os;
+
+       error = dsl_prop_get_integer(zv->zv_name, "readonly", &ro, NULL);
+       if (error)
+               goto out_owned;
 
        error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
-       if (error) {
-               dmu_objset_disown(os, zvol_tag);
-               goto out_mutex;
-       }
+       if (error)
+               goto out_owned;
 
-       zv->zv_objset = os;
        error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf);
-       if (error) {
-               dmu_objset_disown(os, zvol_tag);
-               goto out_mutex;
-       }
+       if (error)
+               goto out_owned;
 
        set_capacity(zv->zv_disk, volsize >> 9);
        zv->zv_volsize = volsize;
        zv->zv_zilog = zil_open(os, zvol_get_data);
 
-       VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &ro, NULL) == 0);
        if (ro || dmu_objset_is_snapshot(os) ||
            !spa_writeable(dmu_objset_spa(os))) {
                set_disk_ro(zv->zv_disk, 1);
@@ -950,11 +989,13 @@ zvol_first_open(zvol_state_t *zv)
                zv->zv_flags &= ~ZVOL_RDONLY;
        }
 
-out_mutex:
-       if (locked)
-               mutex_exit(&spa_namespace_lock);
+out_owned:
+       if (error) {
+               dmu_objset_disown(os, zvol_tag);
+               zv->zv_objset = NULL;
+       }
 
-       return (-error);
+       return (SET_ERROR(-error));
 }
 
 static void
@@ -981,12 +1022,12 @@ zvol_last_close(zvol_state_t *zv)
 static int
 zvol_open(struct block_device *bdev, fmode_t flag)
 {
-       zvol_state_t *zv = bdev->bd_disk->private_data;
+       zvol_state_t *zv;
        int error = 0, drop_mutex = 0;
 
        /*
         * If the caller is already holding the mutex do not take it
-        * again, this will happen as part of zvol_create_minor().
+        * again, this will happen as part of zvol_create_minor_impl().
         * Once add_disk() is called the device is live and the kernel
         * will attempt to open it to read the partition information.
         */
@@ -995,7 +1036,17 @@ zvol_open(struct block_device *bdev, fmode_t flag)
                drop_mutex = 1;
        }
 
-       ASSERT3P(zv, !=, NULL);
+       /*
+        * Obtain a copy of private_data under the lock to make sure
+        * that either the result of zvol_freeg() setting
+        * bdev->bd_disk->private_data to NULL is observed, or zvol_free()
+        * is not called on this zv because of the positive zv_open_count.
+        */
+       zv = bdev->bd_disk->private_data;
+       if (zv == NULL) {
+               error = -ENXIO;
+               goto out_mutex;
+       }
 
        if (zv->zv_open_count == 0) {
                error = zvol_first_open(zv);
@@ -1003,14 +1054,15 @@ zvol_open(struct block_device *bdev, fmode_t flag)
                        goto out_mutex;
        }
 
-       if ((flag & FMODE_WRITE) &&
-           (get_disk_ro(zv->zv_disk) || (zv->zv_flags & ZVOL_RDONLY))) {
+       if ((flag & FMODE_WRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
                error = -EROFS;
                goto out_open_count;
        }
 
        zv->zv_open_count++;
 
+       check_disk_change(bdev);
+
 out_open_count:
        if (zv->zv_open_count == 0)
                zvol_last_close(zv);
@@ -1019,24 +1071,26 @@ out_mutex:
        if (drop_mutex)
                mutex_exit(&zvol_state_lock);
 
-       check_disk_change(bdev);
-
-       return (error);
+       return (SET_ERROR(error));
 }
 
+#ifdef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
+static void
+#else
 static int
+#endif
 zvol_release(struct gendisk *disk, fmode_t mode)
 {
        zvol_state_t *zv = disk->private_data;
        int drop_mutex = 0;
 
+       ASSERT(zv && zv->zv_open_count > 0);
+
        if (!mutex_owned(&zvol_state_lock)) {
                mutex_enter(&zvol_state_lock);
                drop_mutex = 1;
        }
 
-       ASSERT3P(zv, !=, NULL);
-       ASSERT3U(zv->zv_open_count, >, 0);
        zv->zv_open_count--;
        if (zv->zv_open_count == 0)
                zvol_last_close(zv);
@@ -1044,18 +1098,19 @@ zvol_release(struct gendisk *disk, fmode_t mode)
        if (drop_mutex)
                mutex_exit(&zvol_state_lock);
 
+#ifndef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
        return (0);
+#endif
 }
 
 static int
 zvol_ioctl(struct block_device *bdev, fmode_t mode,
-           unsigned int cmd, unsigned long arg)
+    unsigned int cmd, unsigned long arg)
 {
        zvol_state_t *zv = bdev->bd_disk->private_data;
        int error = 0;
 
-       if (zv == NULL)
-               return (-ENXIO);
+       ASSERT(zv && zv->zv_open_count > 0);
 
        switch (cmd) {
        case BLKFLSBUF:
@@ -1071,35 +1126,39 @@ zvol_ioctl(struct block_device *bdev, fmode_t mode,
 
        }
 
-       return (error);
+       return (SET_ERROR(error));
 }
 
 #ifdef CONFIG_COMPAT
 static int
 zvol_compat_ioctl(struct block_device *bdev, fmode_t mode,
-                  unsigned cmd, unsigned long arg)
+    unsigned cmd, unsigned long arg)
 {
-       return zvol_ioctl(bdev, mode, cmd, arg);
+       return (zvol_ioctl(bdev, mode, cmd, arg));
 }
 #else
-#define zvol_compat_ioctl   NULL
+#define        zvol_compat_ioctl       NULL
 #endif
 
 static int zvol_media_changed(struct gendisk *disk)
 {
        zvol_state_t *zv = disk->private_data;
 
-       return zv->zv_changed;
+       ASSERT(zv && zv->zv_open_count > 0);
+
+       return (zv->zv_changed);
 }
 
 static int zvol_revalidate_disk(struct gendisk *disk)
 {
        zvol_state_t *zv = disk->private_data;
 
+       ASSERT(zv && zv->zv_open_count > 0);
+
        zv->zv_changed = 0;
        set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
 
-       return 0;
+       return (0);
 }
 
 /*
@@ -1112,7 +1171,11 @@ static int
 zvol_getgeo(struct block_device *bdev, struct hd_geometry *geo)
 {
        zvol_state_t *zv = bdev->bd_disk->private_data;
-       sector_t sectors = get_capacity(zv->zv_disk);
+       sector_t sectors;
+
+       ASSERT(zv && zv->zv_open_count > 0);
+
+       sectors = get_capacity(zv->zv_disk);
 
        if (sectors > 2048) {
                geo->heads = 16;
@@ -1125,7 +1188,7 @@ zvol_getgeo(struct block_device *bdev, struct hd_geometry *geo)
        geo->start = 0;
        geo->cylinders = sectors / (geo->heads * geo->sectors);
 
-       return 0;
+       return (0);
 }
 
 static struct kobject *
@@ -1139,19 +1202,19 @@ zvol_probe(dev_t dev, int *part, void *arg)
        kobj = zv ? get_disk(zv->zv_disk) : NULL;
        mutex_exit(&zvol_state_lock);
 
-       return kobj;
+       return (kobj);
 }
 
 #ifdef HAVE_BDEV_BLOCK_DEVICE_OPERATIONS
 static struct block_device_operations zvol_ops = {
-       .open            = zvol_open,
-       .release         = zvol_release,
-       .ioctl           = zvol_ioctl,
-       .compat_ioctl    = zvol_compat_ioctl,
-       .media_changed   = zvol_media_changed,
-       .revalidate_disk = zvol_revalidate_disk,
-       .getgeo          = zvol_getgeo,
-        .owner           = THIS_MODULE,
+       .open                   = zvol_open,
+       .release                = zvol_release,
+       .ioctl                  = zvol_ioctl,
+       .compat_ioctl           = zvol_compat_ioctl,
+       .media_changed          = zvol_media_changed,
+       .revalidate_disk        = zvol_revalidate_disk,
+       .getgeo                 = zvol_getgeo,
+       .owner                  = THIS_MODULE,
 };
 
 #else /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
@@ -1159,47 +1222,49 @@ static struct block_device_operations zvol_ops = {
 static int
 zvol_open_by_inode(struct inode *inode, struct file *file)
 {
-       return zvol_open(inode->i_bdev, file->f_mode);
+       return (zvol_open(inode->i_bdev, file->f_mode));
 }
 
 static int
 zvol_release_by_inode(struct inode *inode, struct file *file)
 {
-       return zvol_release(inode->i_bdev->bd_disk, file->f_mode);
+       return (zvol_release(inode->i_bdev->bd_disk, file->f_mode));
 }
 
 static int
 zvol_ioctl_by_inode(struct inode *inode, struct file *file,
-                    unsigned int cmd, unsigned long arg)
+    unsigned int cmd, unsigned long arg)
 {
        if (file == NULL || inode == NULL)
-               return -EINVAL;
-       return zvol_ioctl(inode->i_bdev, file->f_mode, cmd, arg);
+               return (SET_ERROR(-EINVAL));
+
+       return (zvol_ioctl(inode->i_bdev, file->f_mode, cmd, arg));
 }
 
-# ifdef CONFIG_COMPAT
+#ifdef CONFIG_COMPAT
 static long
 zvol_compat_ioctl_by_inode(struct file *file,
-                           unsigned int cmd, unsigned long arg)
+    unsigned int cmd, unsigned long arg)
 {
        if (file == NULL)
-               return -EINVAL;
-       return zvol_compat_ioctl(file->f_dentry->d_inode->i_bdev,
-                                file->f_mode, cmd, arg);
+               return (SET_ERROR(-EINVAL));
+
+       return (zvol_compat_ioctl(file->f_dentry->d_inode->i_bdev,
+           file->f_mode, cmd, arg));
 }
-# else
-# define zvol_compat_ioctl_by_inode   NULL
-# endif
+#else
+#define        zvol_compat_ioctl_by_inode      NULL
+#endif
 
 static struct block_device_operations zvol_ops = {
-       .open            = zvol_open_by_inode,
-       .release         = zvol_release_by_inode,
-       .ioctl           = zvol_ioctl_by_inode,
-       .compat_ioctl    = zvol_compat_ioctl_by_inode,
-       .media_changed   = zvol_media_changed,
-       .revalidate_disk = zvol_revalidate_disk,
-       .getgeo          = zvol_getgeo,
-        .owner           = THIS_MODULE,
+       .open                   = zvol_open_by_inode,
+       .release                = zvol_release_by_inode,
+       .ioctl                  = zvol_ioctl_by_inode,
+       .compat_ioctl           = zvol_compat_ioctl_by_inode,
+       .media_changed          = zvol_media_changed,
+       .revalidate_disk        = zvol_revalidate_disk,
+       .getgeo                 = zvol_getgeo,
+       .owner                  = THIS_MODULE,
 };
 #endif /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
 
@@ -1211,28 +1276,17 @@ static zvol_state_t *
 zvol_alloc(dev_t dev, const char *name)
 {
        zvol_state_t *zv;
-       int error = 0;
 
        zv = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP);
 
-       zv->zv_queue = blk_init_queue(zvol_request, &zv->zv_lock);
+       list_link_init(&zv->zv_next);
+
+       zv->zv_queue = blk_alloc_queue(GFP_ATOMIC);
        if (zv->zv_queue == NULL)
                goto out_kmem;
 
-#ifdef HAVE_ELEVATOR_CHANGE
-       error = elevator_change(zv->zv_queue, "noop");
-#endif /* HAVE_ELEVATOR_CHANGE */
-       if (error) {
-               printk("ZFS: Unable to set \"%s\" scheduler for zvol %s: %d\n",
-                   "noop", name, error);
-               goto out_queue;
-       }
-
-#ifdef HAVE_BLK_QUEUE_FLUSH
-       blk_queue_flush(zv->zv_queue, VDEV_REQ_FLUSH | VDEV_REQ_FUA);
-#else
-       blk_queue_ordered(zv->zv_queue, QUEUE_ORDERED_DRAIN, NULL);
-#endif /* HAVE_BLK_QUEUE_FLUSH */
+       blk_queue_make_request(zv->zv_queue, zvol_request);
+       blk_queue_set_write_cache(zv->zv_queue, B_TRUE, B_TRUE);
 
        zv->zv_disk = alloc_disk(ZVOL_MINORS);
        if (zv->zv_disk == NULL)
@@ -1243,13 +1297,7 @@ zvol_alloc(dev_t dev, const char *name)
        zv->zv_open_count = 0;
        strlcpy(zv->zv_name, name, MAXNAMELEN);
 
-       mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
-       avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
-           sizeof (rl_t), offsetof(rl_t, r_node));
-       zv->zv_znode.z_is_zvol = TRUE;
-
-       spin_lock_init(&zv->zv_lock);
-       list_link_init(&zv->zv_next);
+       zfs_rlock_init(&zv->zv_range_lock);
 
        zv->zv_disk->major = zvol_major;
        zv->zv_disk->first_minor = (dev & MINORMASK);
@@ -1259,14 +1307,14 @@ zvol_alloc(dev_t dev, const char *name)
        snprintf(zv->zv_disk->disk_name, DISK_NAME_LEN, "%s%d",
            ZVOL_DEV_NAME, (dev & MINORMASK));
 
-       return zv;
+       return (zv);
 
 out_queue:
        blk_cleanup_queue(zv->zv_queue);
 out_kmem:
        kmem_free(zv, sizeof (zvol_state_t));
 
-       return NULL;
+       return (NULL);
 }
 
 /*
@@ -1275,8 +1323,12 @@ out_kmem:
 static void
 zvol_free(zvol_state_t *zv)
 {
-       avl_destroy(&zv->zv_znode.z_range_avl);
-       mutex_destroy(&zv->zv_znode.z_range_lock);
+       ASSERT(MUTEX_HELD(&zvol_state_lock));
+       ASSERT(zv->zv_open_count == 0);
+
+       zfs_rlock_destroy(&zv->zv_range_lock);
+
+       zv->zv_disk->private_data = NULL;
 
        del_gendisk(zv->zv_disk);
        blk_cleanup_queue(zv->zv_queue);
@@ -1285,52 +1337,31 @@ zvol_free(zvol_state_t *zv)
        kmem_free(zv, sizeof (zvol_state_t));
 }
 
+/*
+ * Create a block device minor node and setup the linkage between it
+ * and the specified volume.  Once this function returns the block
+ * device is live and ready for use.
+ */
 static int
-__zvol_snapdev_hidden(const char *name)
-{
-        uint64_t snapdev;
-        char *parent;
-        char *atp;
-        int error = 0;
-
-        parent = kmem_alloc(MAXPATHLEN, KM_SLEEP);
-        (void) strlcpy(parent, name, MAXPATHLEN);
-
-        if ((atp = strrchr(parent, '@')) != NULL) {
-                *atp = '\0';
-                error = dsl_prop_get_integer(parent, "snapdev", &snapdev, NULL);
-                if ((error == 0) && (snapdev == ZFS_SNAPDEV_HIDDEN))
-                        error = ENODEV;
-        }
-        kmem_free(parent, MAXPATHLEN);
-        return (error);
-}
-
-static int
-__zvol_create_minor(const char *name, boolean_t ignore_snapdev)
+zvol_create_minor_impl(const char *name)
 {
        zvol_state_t *zv;
        objset_t *os;
        dmu_object_info_t *doi;
        uint64_t volsize;
+       uint64_t len;
        unsigned minor = 0;
        int error = 0;
 
-       ASSERT(MUTEX_HELD(&zvol_state_lock));
+       mutex_enter(&zvol_state_lock);
 
        zv = zvol_find_by_name(name);
        if (zv) {
-               error = EEXIST;
+               error = SET_ERROR(EEXIST);
                goto out;
        }
 
-       if (ignore_snapdev == B_FALSE) {
-               error = __zvol_snapdev_hidden(name);
-               if (error)
-                       goto out;
-       }
-
-       doi = kmem_alloc(sizeof(dmu_object_info_t), KM_SLEEP);
+       doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
 
        error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, zvol_tag, &os);
        if (error)
@@ -1350,7 +1381,7 @@ __zvol_create_minor(const char *name, boolean_t ignore_snapdev)
 
        zv = zvol_alloc(MKDEV(zvol_major, minor), name);
        if (zv == NULL) {
-               error = EAGAIN;
+               error = SET_ERROR(EAGAIN);
                goto out_dmu_objset_disown;
        }
 
@@ -1363,20 +1394,21 @@ __zvol_create_minor(const char *name, boolean_t ignore_snapdev)
 
        set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
 
-       blk_queue_max_hw_sectors(zv->zv_queue, UINT_MAX);
+       blk_queue_max_hw_sectors(zv->zv_queue, (DMU_MAX_ACCESS / 4) >> 9);
        blk_queue_max_segments(zv->zv_queue, UINT16_MAX);
        blk_queue_max_segment_size(zv->zv_queue, UINT_MAX);
        blk_queue_physical_block_size(zv->zv_queue, zv->zv_volblocksize);
        blk_queue_io_opt(zv->zv_queue, zv->zv_volblocksize);
-#ifdef HAVE_BLK_QUEUE_DISCARD
        blk_queue_max_discard_sectors(zv->zv_queue,
            (zvol_max_discard_blocks * zv->zv_volblocksize) >> 9);
        blk_queue_discard_granularity(zv->zv_queue, zv->zv_volblocksize);
        queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, zv->zv_queue);
-#endif
-#ifdef HAVE_BLK_QUEUE_NONROT
+#ifdef QUEUE_FLAG_NONROT
        queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zv->zv_queue);
 #endif
+#ifdef QUEUE_FLAG_ADD_RANDOM
+       queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, zv->zv_queue);
+#endif
 
        if (spa_writeable(dmu_objset_spa(os))) {
                if (zil_replay_disable)
@@ -1385,217 +1417,556 @@ __zvol_create_minor(const char *name, boolean_t ignore_snapdev)
                        zil_replay(os, zv, zvol_replay_vector);
        }
 
+       /*
+        * When udev detects the addition of the device it will immediately
+        * invoke blkid(8) to determine the type of content on the device.
+        * Prefetching the blocks commonly scanned by blkid(8) will speed
+        * up this process.
+        */
+       len = MIN(MAX(zvol_prefetch_bytes, 0), SPA_MAXBLOCKSIZE);
+       if (len > 0) {
+               dmu_prefetch(os, ZVOL_OBJ, 0, 0, len, ZIO_PRIORITY_SYNC_READ);
+               dmu_prefetch(os, ZVOL_OBJ, 0, volsize - len, len,
+                       ZIO_PRIORITY_SYNC_READ);
+       }
+
        zv->zv_objset = NULL;
 out_dmu_objset_disown:
        dmu_objset_disown(os, zvol_tag);
 out_doi:
-       kmem_free(doi, sizeof(dmu_object_info_t));
+       kmem_free(doi, sizeof (dmu_object_info_t));
 out:
 
        if (error == 0) {
                zvol_insert(zv);
+               /*
+                * Drop the lock to prevent deadlock with sys_open() ->
+                * zvol_open(), which first takes bd_disk->bd_mutex and then
+                * takes zvol_state_lock, whereas this code path first takes
+                * zvol_state_lock, and then takes bd_disk->bd_mutex.
+                */
+               mutex_exit(&zvol_state_lock);
                add_disk(zv->zv_disk);
+       } else {
+               mutex_exit(&zvol_state_lock);
        }
 
-       return (error);
+       return (SET_ERROR(error));
 }
 
 /*
- * Create a block device minor node and setup the linkage between it
- * and the specified volume.  Once this function returns the block
- * device is live and ready for use.
+ * Rename a block device minor mode for the specified volume.
  */
-int
-zvol_create_minor(const char *name)
+static void
+zvol_rename_minor(zvol_state_t *zv, const char *newname)
 {
-       int error;
+       int readonly = get_disk_ro(zv->zv_disk);
 
-       mutex_enter(&zvol_state_lock);
-       error = __zvol_create_minor(name, B_FALSE);
-       mutex_exit(&zvol_state_lock);
+       ASSERT(MUTEX_HELD(&zvol_state_lock));
 
-       return (error);
+       strlcpy(zv->zv_name, newname, sizeof (zv->zv_name));
+
+       /*
+        * The block device's read-only state is briefly changed causing
+        * a KOBJ_CHANGE uevent to be issued.  This ensures udev detects
+        * the name change and fixes the symlinks.  This does not change
+        * ZVOL_RDONLY in zv->zv_flags so the actual read-only state never
+        * changes.  This would normally be done using kobject_uevent() but
+        * that is a GPL-only symbol which is why we need this workaround.
+        */
+       set_disk_ro(zv->zv_disk, !readonly);
+       set_disk_ro(zv->zv_disk, readonly);
 }
 
+
+/*
+ * Mask errors to continue dmu_objset_find() traversal
+ */
 static int
-__zvol_remove_minor(const char *name)
+zvol_create_snap_minor_cb(const char *dsname, void *arg)
 {
-       zvol_state_t *zv;
-
-       ASSERT(MUTEX_HELD(&zvol_state_lock));
+       const char *name = (const char *)arg;
 
-       zv = zvol_find_by_name(name);
-       if (zv == NULL)
-               return (ENXIO);
+       ASSERT0(MUTEX_HELD(&spa_namespace_lock));
 
-       if (zv->zv_open_count > 0)
-               return (EBUSY);
+       /* skip the designated dataset */
+       if (name && strcmp(dsname, name) == 0)
+               return (0);
 
-       zvol_remove(zv);
-       zvol_free(zv);
+       /* at this point, the dsname should name a snapshot */
+       if (strchr(dsname, '@') == 0) {
+               dprintf("zvol_create_snap_minor_cb(): "
+                       "%s is not a shapshot name\n", dsname);
+       } else {
+               (void) zvol_create_minor_impl(dsname);
+       }
 
        return (0);
 }
 
 /*
- * Remove a block device minor node for the specified volume.
+ * Mask errors to continue dmu_objset_find() traversal
  */
-int
-zvol_remove_minor(const char *name)
+static int
+zvol_create_minors_cb(const char *dsname, void *arg)
 {
+       uint64_t snapdev;
        int error;
 
-       mutex_enter(&zvol_state_lock);
-       error = __zvol_remove_minor(name);
-       mutex_exit(&zvol_state_lock);
+       ASSERT0(MUTEX_HELD(&spa_namespace_lock));
 
-       return (error);
-}
+       error = dsl_prop_get_integer(dsname, "snapdev", &snapdev, NULL);
+       if (error)
+               return (0);
 
-static int
-zvol_create_minors_cb(spa_t *spa, uint64_t dsobj,
-                     const char *dsname, void *arg)
-{
-       if (strchr(dsname, '/') == NULL)
-               return 0;
+       /*
+        * Given the name and the 'snapdev' property, create device minor nodes
+        * with the linkages to zvols/snapshots as needed.
+        * If the name represents a zvol, create a minor node for the zvol, then
+        * check if its snapshots are 'visible', and if so, iterate over the
+        * snapshots and create device minor nodes for those.
+        */
+       if (strchr(dsname, '@') == 0) {
+               /* create minor for the 'dsname' explicitly */
+               error = zvol_create_minor_impl(dsname);
+               if ((error == 0 || error == EEXIST) &&
+                   (snapdev == ZFS_SNAPDEV_VISIBLE)) {
+                       fstrans_cookie_t cookie = spl_fstrans_mark();
+                       /*
+                        * traverse snapshots only, do not traverse children,
+                        * and skip the 'dsname'
+                        */
+                       error = dmu_objset_find((char *)dsname,
+                           zvol_create_snap_minor_cb, (void *)dsname,
+                           DS_FIND_SNAPSHOTS);
+                       spl_fstrans_unmark(cookie);
+               }
+       } else {
+               dprintf("zvol_create_minors_cb(): %s is not a zvol name\n",
+                       dsname);
+       }
 
-       (void) __zvol_create_minor(dsname, B_FALSE);
        return (0);
 }
 
 /*
- * Create minors for specified pool, if pool is NULL create minors
- * for all available pools.
+ * Create minors for the specified dataset, including children and snapshots.
+ * Pay attention to the 'snapdev' property and iterate over the snapshots
+ * only if they are 'visible'. This approach allows one to assure that the
+ * snapshot metadata is read from disk only if it is needed.
+ *
+ * The name can represent a dataset to be recursively scanned for zvols and
+ * their snapshots, or a single zvol snapshot. If the name represents a
+ * dataset, the scan is performed in two nested stages:
+ * - scan the dataset for zvols, and
+ * - for each zvol, create a minor node, then check if the zvol's snapshots
+ *   are 'visible', and only then iterate over the snapshots if needed
+ *
+ * If the name represents a snapshot, a check is perfromed if the snapshot is
+ * 'visible' (which also verifies that the parent is a zvol), and if so,
+ * a minor node for that snapshot is created.
  */
-int
-zvol_create_minors(const char *pool)
+static int
+zvol_create_minors_impl(const char *name)
 {
-       spa_t *spa = NULL;
        int error = 0;
+       fstrans_cookie_t cookie;
+       char *atp, *parent;
 
        if (zvol_inhibit_dev)
                return (0);
 
-       mutex_enter(&zvol_state_lock);
-       if (pool) {
-               error = dmu_objset_find_spa(NULL, pool, zvol_create_minors_cb,
-                   NULL, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
+       parent = kmem_alloc(MAXPATHLEN, KM_SLEEP);
+       (void) strlcpy(parent, name, MAXPATHLEN);
+
+       if ((atp = strrchr(parent, '@')) != NULL) {
+               uint64_t snapdev;
+
+               *atp = '\0';
+               error = dsl_prop_get_integer(parent, "snapdev",
+                   &snapdev, NULL);
+
+               if (error == 0 && snapdev == ZFS_SNAPDEV_VISIBLE)
+                       error = zvol_create_minor_impl(name);
        } else {
-               mutex_enter(&spa_namespace_lock);
-               while ((spa = spa_next(spa)) != NULL) {
-                       error = dmu_objset_find_spa(NULL,
-                           spa_name(spa), zvol_create_minors_cb, NULL,
-                           DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
-                       if (error)
-                               break;
-               }
-               mutex_exit(&spa_namespace_lock);
+               cookie = spl_fstrans_mark();
+               error = dmu_objset_find(parent, zvol_create_minors_cb,
+                   NULL, DS_FIND_CHILDREN);
+               spl_fstrans_unmark(cookie);
        }
-       mutex_exit(&zvol_state_lock);
 
-       return error;
+       kmem_free(parent, MAXPATHLEN);
+
+       return (SET_ERROR(error));
 }
 
 /*
- * Remove minors for specified pool, if pool is NULL remove all minors.
+ * Remove minors for specified dataset including children and snapshots.
  */
-void
-zvol_remove_minors(const char *pool)
+static void
+zvol_remove_minors_impl(const char *name)
 {
        zvol_state_t *zv, *zv_next;
-       char *str;
+       int namelen = ((name) ? strlen(name) : 0);
 
        if (zvol_inhibit_dev)
                return;
 
-       str = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
-       if (pool) {
-               (void) strncpy(str, pool, strlen(pool));
-               (void) strcat(str, "/");
+       mutex_enter(&zvol_state_lock);
+
+       for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
+               zv_next = list_next(&zvol_state_list, zv);
+
+               if (name == NULL || strcmp(zv->zv_name, name) == 0 ||
+                   (strncmp(zv->zv_name, name, namelen) == 0 &&
+                   (zv->zv_name[namelen] == '/' ||
+                   zv->zv_name[namelen] == '@'))) {
+
+                       /* If in use, leave alone */
+                       if (zv->zv_open_count > 0)
+                               continue;
+
+                       zvol_remove(zv);
+                       zvol_free(zv);
+               }
        }
 
+       mutex_exit(&zvol_state_lock);
+}
+
+/* Remove minor for this specific snapshot only */
+static void
+zvol_remove_minor_impl(const char *name)
+{
+       zvol_state_t *zv, *zv_next;
+
+       if (zvol_inhibit_dev)
+               return;
+
+       if (strchr(name, '@') == NULL)
+               return;
+
        mutex_enter(&zvol_state_lock);
+
        for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
                zv_next = list_next(&zvol_state_list, zv);
 
-               if (pool == NULL || !strncmp(str, zv->zv_name, strlen(str))) {
+               if (strcmp(zv->zv_name, name) == 0) {
+                       /* If in use, leave alone */
+                       if (zv->zv_open_count > 0)
+                               continue;
                        zvol_remove(zv);
                        zvol_free(zv);
+                       break;
+               }
+       }
+
+       mutex_exit(&zvol_state_lock);
+}
+
+/*
+ * Rename minors for specified dataset including children and snapshots.
+ */
+static void
+zvol_rename_minors_impl(const char *oldname, const char *newname)
+{
+       zvol_state_t *zv, *zv_next;
+       int oldnamelen, newnamelen;
+       char *name;
+
+       if (zvol_inhibit_dev)
+               return;
+
+       oldnamelen = strlen(oldname);
+       newnamelen = strlen(newname);
+       name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
+
+       mutex_enter(&zvol_state_lock);
+
+       for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
+               zv_next = list_next(&zvol_state_list, zv);
+
+               /* If in use, leave alone */
+               if (zv->zv_open_count > 0)
+                       continue;
+
+               if (strcmp(zv->zv_name, oldname) == 0) {
+                       zvol_rename_minor(zv, newname);
+               } else if (strncmp(zv->zv_name, oldname, oldnamelen) == 0 &&
+                   (zv->zv_name[oldnamelen] == '/' ||
+                   zv->zv_name[oldnamelen] == '@')) {
+                       snprintf(name, MAXNAMELEN, "%s%c%s", newname,
+                           zv->zv_name[oldnamelen],
+                           zv->zv_name + oldnamelen + 1);
+                       zvol_rename_minor(zv, name);
                }
        }
+
        mutex_exit(&zvol_state_lock);
-       kmem_free(str, MAXNAMELEN);
+
+       kmem_free(name, MAXNAMELEN);
 }
 
+typedef struct zvol_snapdev_cb_arg {
+       uint64_t snapdev;
+} zvol_snapdev_cb_arg_t;
+
 static int
-snapdev_snapshot_changed_cb(const char *dsname, void *arg) {
-       uint64_t snapdev = *(uint64_t *) arg;
+zvol_set_snapdev_cb(const char *dsname, void *param) {
+       zvol_snapdev_cb_arg_t *arg = param;
 
        if (strchr(dsname, '@') == NULL)
-               return 0;
+               return (0);
 
-       switch (snapdev) {
+       switch (arg->snapdev) {
                case ZFS_SNAPDEV_VISIBLE:
-                       mutex_enter(&zvol_state_lock);
-                       (void) __zvol_create_minor(dsname, B_TRUE);
-                       mutex_exit(&zvol_state_lock);
+                       (void) zvol_create_minor_impl(dsname);
                        break;
                case ZFS_SNAPDEV_HIDDEN:
-                       (void) zvol_remove_minor(dsname);
+                       (void) zvol_remove_minor_impl(dsname);
                        break;
        }
-       return 0;
+
+       return (0);
+}
+
+static void
+zvol_set_snapdev_impl(char *name, uint64_t snapdev)
+{
+       zvol_snapdev_cb_arg_t arg = {snapdev};
+       fstrans_cookie_t cookie = spl_fstrans_mark();
+       /*
+        * The zvol_set_snapdev_sync() sets snapdev appropriately
+        * in the dataset hierarchy. Here, we only scan snapshots.
+        */
+       dmu_objset_find(name, zvol_set_snapdev_cb, &arg, DS_FIND_SNAPSHOTS);
+       spl_fstrans_unmark(cookie);
+}
+
+static zvol_task_t *
+zvol_task_alloc(zvol_async_op_t op, const char *name1, const char *name2,
+    uint64_t snapdev)
+{
+       zvol_task_t *task;
+       char *delim;
+
+       /* Never allow tasks on hidden names. */
+       if (name1[0] == '$')
+               return (NULL);
+
+       task = kmem_zalloc(sizeof (zvol_task_t), KM_SLEEP);
+       task->op = op;
+       task->snapdev = snapdev;
+       delim = strchr(name1, '/');
+       strlcpy(task->pool, name1, delim ? (delim - name1 + 1) : MAXNAMELEN);
+
+       strlcpy(task->name1, name1, MAXNAMELEN);
+       if (name2 != NULL)
+               strlcpy(task->name2, name2, MAXNAMELEN);
+
+       return (task);
+}
+
+static void
+zvol_task_free(zvol_task_t *task)
+{
+       kmem_free(task, sizeof (zvol_task_t));
+}
+
+/*
+ * The worker thread function performed asynchronously.
+ */
+static void
+zvol_task_cb(void *param)
+{
+       zvol_task_t *task = (zvol_task_t *)param;
+
+       switch (task->op) {
+       case ZVOL_ASYNC_CREATE_MINORS:
+               (void) zvol_create_minors_impl(task->name1);
+               break;
+       case ZVOL_ASYNC_REMOVE_MINORS:
+               zvol_remove_minors_impl(task->name1);
+               break;
+       case ZVOL_ASYNC_RENAME_MINORS:
+               zvol_rename_minors_impl(task->name1, task->name2);
+               break;
+       case ZVOL_ASYNC_SET_SNAPDEV:
+               zvol_set_snapdev_impl(task->name1, task->snapdev);
+               break;
+       default:
+               VERIFY(0);
+               break;
+       }
+
+       zvol_task_free(task);
+}
+
+typedef struct zvol_set_snapdev_arg {
+       const char *zsda_name;
+       uint64_t zsda_value;
+       zprop_source_t zsda_source;
+       dmu_tx_t *zsda_tx;
+} zvol_set_snapdev_arg_t;
+
+/*
+ * Sanity check the dataset for safe use by the sync task.  No additional
+ * conditions are imposed.
+ */
+static int
+zvol_set_snapdev_check(void *arg, dmu_tx_t *tx)
+{
+       zvol_set_snapdev_arg_t *zsda = arg;
+       dsl_pool_t *dp = dmu_tx_pool(tx);
+       dsl_dir_t *dd;
+       int error;
+
+       error = dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL);
+       if (error != 0)
+               return (error);
+
+       dsl_dir_rele(dd, FTAG);
+
+       return (error);
+}
+
+static int
+zvol_set_snapdev_sync_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
+{
+       zvol_set_snapdev_arg_t *zsda = arg;
+       char dsname[MAXNAMELEN];
+       zvol_task_t *task;
+
+       dsl_dataset_name(ds, dsname);
+       dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_SNAPDEV),
+           zsda->zsda_source, sizeof (zsda->zsda_value), 1,
+           &zsda->zsda_value, zsda->zsda_tx);
+
+       task = zvol_task_alloc(ZVOL_ASYNC_SET_SNAPDEV, dsname,
+           NULL, zsda->zsda_value);
+       if (task == NULL)
+               return (0);
+
+       (void) taskq_dispatch(dp->dp_spa->spa_zvol_taskq, zvol_task_cb,
+               task, TQ_SLEEP);
+       return (0);
+}
+
+/*
+ * Traverse all child snapshot datasets and apply snapdev appropriately.
+ */
+static void
+zvol_set_snapdev_sync(void *arg, dmu_tx_t *tx)
+{
+       zvol_set_snapdev_arg_t *zsda = arg;
+       dsl_pool_t *dp = dmu_tx_pool(tx);
+       dsl_dir_t *dd;
+
+       VERIFY0(dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL));
+       zsda->zsda_tx = tx;
+
+       dmu_objset_find_dp(dp, dd->dd_object, zvol_set_snapdev_sync_cb,
+           zsda, DS_FIND_CHILDREN);
+
+       dsl_dir_rele(dd, FTAG);
 }
 
 int
-zvol_set_snapdev(const char *dsname, uint64_t snapdev) {
-       (void) dmu_objset_find((char *) dsname, snapdev_snapshot_changed_cb,
-               &snapdev, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
-       /* caller should continue to modify snapdev property */
-       return (-1);
+zvol_set_snapdev(const char *ddname, zprop_source_t source, uint64_t snapdev)
+{
+       zvol_set_snapdev_arg_t zsda;
+
+       zsda.zsda_name = ddname;
+       zsda.zsda_source = source;
+       zsda.zsda_value = snapdev;
+
+       return (dsl_sync_task(ddname, zvol_set_snapdev_check,
+           zvol_set_snapdev_sync, &zsda, 0, ZFS_SPACE_CHECK_NONE));
 }
 
+void
+zvol_create_minors(spa_t *spa, const char *name, boolean_t async)
+{
+       zvol_task_t *task;
+       taskqid_t id;
+
+       task = zvol_task_alloc(ZVOL_ASYNC_CREATE_MINORS, name, NULL, ~0ULL);
+       if (task == NULL)
+               return;
+
+       id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP);
+       if ((async == B_FALSE) && (id != TASKQID_INVALID))
+               taskq_wait_id(spa->spa_zvol_taskq, id);
+}
+
+void
+zvol_remove_minors(spa_t *spa, const char *name, boolean_t async)
+{
+       zvol_task_t *task;
+       taskqid_t id;
+
+       task = zvol_task_alloc(ZVOL_ASYNC_REMOVE_MINORS, name, NULL, ~0ULL);
+       if (task == NULL)
+               return;
+
+       id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP);
+       if ((async == B_FALSE) && (id != TASKQID_INVALID))
+               taskq_wait_id(spa->spa_zvol_taskq, id);
+}
+
+void
+zvol_rename_minors(spa_t *spa, const char *name1, const char *name2,
+    boolean_t async)
+{
+       zvol_task_t *task;
+       taskqid_t id;
+
+       task = zvol_task_alloc(ZVOL_ASYNC_RENAME_MINORS, name1, name2, ~0ULL);
+       if (task == NULL)
+               return;
+
+       id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP);
+       if ((async == B_FALSE) && (id != TASKQID_INVALID))
+               taskq_wait_id(spa->spa_zvol_taskq, id);
+}
 
 int
 zvol_init(void)
 {
        int error;
 
-       zvol_taskq = taskq_create(ZVOL_DRIVER, zvol_threads, maxclsyspri,
-                                 zvol_threads, INT_MAX, TASKQ_PREPOPULATE);
-       if (zvol_taskq == NULL) {
-               printk(KERN_INFO "ZFS: taskq_create() failed\n");
-               return (-ENOMEM);
-       }
+       list_create(&zvol_state_list, sizeof (zvol_state_t),
+           offsetof(zvol_state_t, zv_next));
+       mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
 
        error = register_blkdev(zvol_major, ZVOL_DRIVER);
        if (error) {
                printk(KERN_INFO "ZFS: register_blkdev() failed %d\n", error);
-               taskq_destroy(zvol_taskq);
-               return (error);
+               goto out;
        }
 
        blk_register_region(MKDEV(zvol_major, 0), 1UL << MINORBITS,
-                           THIS_MODULE, zvol_probe, NULL, NULL);
+           THIS_MODULE, zvol_probe, NULL, NULL);
 
-       mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
-       list_create(&zvol_state_list, sizeof (zvol_state_t),
-                   offsetof(zvol_state_t, zv_next));
+       return (0);
 
-       (void) zvol_create_minors(NULL);
+out:
+       mutex_destroy(&zvol_state_lock);
+       list_destroy(&zvol_state_list);
 
-       return (0);
+       return (SET_ERROR(error));
 }
 
 void
 zvol_fini(void)
 {
-       zvol_remove_minors(NULL);
+       zvol_remove_minors_impl(NULL);
+
        blk_unregister_region(MKDEV(zvol_major, 0), 1UL << MINORBITS);
        unregister_blkdev(zvol_major, ZVOL_DRIVER);
-       taskq_destroy(zvol_taskq);
-       mutex_destroy(&zvol_state_lock);
+
        list_destroy(&zvol_state_list);
+       mutex_destroy(&zvol_state_lock);
 }
 
 module_param(zvol_inhibit_dev, uint, 0644);
@@ -1604,8 +1975,8 @@ MODULE_PARM_DESC(zvol_inhibit_dev, "Do not create zvol device nodes");
 module_param(zvol_major, uint, 0444);
 MODULE_PARM_DESC(zvol_major, "Major number for zvol device");
 
-module_param(zvol_threads, uint, 0444);
-MODULE_PARM_DESC(zvol_threads, "Number of threads for zvol device");
-
 module_param(zvol_max_discard_blocks, ulong, 0444);
-MODULE_PARM_DESC(zvol_max_discard_blocks, "Max number of blocks to discard at once");
+MODULE_PARM_DESC(zvol_max_discard_blocks, "Max number of blocks to discard");
+
+module_param(zvol_prefetch_bytes, uint, 0644);
+MODULE_PARM_DESC(zvol_prefetch_bytes, "Prefetch N bytes at zvol start+end");