]> granicus.if.org Git - zfs/blobdiff - module/zfs/zvol.c
Bound zvol_threads module option
[zfs] / module / zfs / zvol.c
index f52d8bbc142a001fb7ee34f82dfaa1904978d142..b073a3c5ee5d2ec85e0b2e038e953dbc603524c0 100644 (file)
@@ -35,6 +35,7 @@
  * needs to be run before opening and using a device.
  */
 
+#include <sys/dbuf.h>
 #include <sys/dmu_traverse.h>
 #include <sys/dsl_dataset.h>
 #include <sys/dsl_prop.h>
@@ -61,8 +62,8 @@ static char *zvol_tag = "zvol_tag";
  */
 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 */
@@ -93,16 +94,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 +116,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 +134,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);
 }
 
 
@@ -159,7 +160,7 @@ zvol_is_zvol(const char *device)
        bdput(bdev);
 
        if (major == zvol_major)
-            return (B_TRUE);
+               return (B_TRUE);
 
        return (B_FALSE);
 }
@@ -214,10 +215,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 +226,34 @@ 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;
+/*
+ * 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 */
+
+       bdput(bdev);
 }
 
 /*
@@ -237,14 +263,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);
+               return (SET_ERROR(EOVERFLOW));
 #endif
        return (0);
 }
@@ -253,9 +279,8 @@ 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;
 
@@ -266,39 +291,30 @@ zvol_update_volsize(zvol_state_t *zv, uint64_t volsize, objset_t *os)
        error = dmu_tx_assign(tx, TXG_WAIT);
        if (error) {
                dmu_tx_abort(tx);
-               return (error);
+               return (SET_ERROR(error));
        }
 
        error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
            &volsize, tx);
        dmu_tx_commit(tx);
 
-       if (error)
-               return (error);
+       if (error == 0)
+               error = dmu_free_long_range(os,
+                   ZVOL_OBJ, volsize, DMU_OBJECT_END);
 
-       error = dmu_free_long_range(os,
-           ZVOL_OBJ, volsize, DMU_OBJECT_END);
-       if (error)
-               return (error);
+       return (error);
+}
 
-       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 */
+static int
+zvol_update_live_volsize(zvol_state_t *zv, uint64_t volsize)
+{
+       zvol_size_changed(zv, volsize);
 
-       bdput(bdev);
+       /*
+        * 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,57 +325,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;
 
        error = dsl_prop_get_integer(name,
            zfs_prop_to_name(ZFS_PROP_READONLY), &readonly, NULL);
        if (error != 0)
-               return (error);
+               return (SET_ERROR(error));
        if (readonly)
-               return (EROFS);
+               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);
-
-       error = dmu_objset_hold(name, FTAG, &os);
-       if (error)
-               goto out_doi;
+       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;
+       }
 
-       if ((error = dmu_object_info(os, ZVOL_OBJ, doi)) != 0 ||
-           (error = zvol_check_volsize(volsize,doi->doi_data_block_size)) != 0)
-               goto out_doi;
+       doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
 
-       VERIFY(dsl_prop_get_integer(name, "readonly", &readonly, NULL) == 0);
-       if (readonly) {
-               error = EROFS;
-               goto out_doi;
-       }
+       if ((error = dmu_object_info(os, ZVOL_OBJ, doi)) ||
+           (error = zvol_check_volsize(volsize, doi->doi_data_block_size)))
+               goto out;
 
-       if (get_disk_ro(zv->zv_disk) || (zv->zv_flags & ZVOL_RDONLY)) {
-               error = EROFS;
-               goto out_doi;
-       }
+       error = zvol_update_volsize(volsize, os);
+       kmem_free(doi, sizeof (dmu_object_info_t));
 
-       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);
 }
 
@@ -372,7 +385,7 @@ zvol_check_volblocksize(uint64_t volblocksize)
        if (volblocksize < SPA_MINBLOCKSIZE ||
            volblocksize > SPA_MAXBLOCKSIZE ||
            !ISP2(volblocksize))
-               return (EDOM);
+               return (SET_ERROR(EDOM));
 
        return (0);
 }
@@ -391,12 +404,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;
        }
 
@@ -409,7 +422,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;
@@ -417,7 +430,7 @@ zvol_set_volblocksize(const char *name, uint64_t volblocksize)
 out:
        mutex_exit(&zvol_state_lock);
 
-       return (error);
+       return (SET_ERROR(error));
 }
 
 /*
@@ -447,13 +460,13 @@ 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));
 }
 
 /*
@@ -485,8 +498,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;
@@ -564,20 +577,13 @@ zvol_write(void *arg)
        struct request *req = (struct request *)arg;
        struct request_queue *q = req->q;
        zvol_state_t *zv = q->queuedata;
+       fstrans_cookie_t cookie = spl_fstrans_mark();
        uint64_t offset = blk_rq_pos(req) << 9;
        uint64_t size = blk_rq_bytes(req);
        int error = 0;
        dmu_tx_t *tx;
        rl_t *rl;
 
-       /*
-        * 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;
-
        if (req->cmd_flags & VDEV_REQ_FLUSH)
                zil_commit(zv->zv_zilog, ZVOL_OBJ);
 
@@ -585,7 +591,7 @@ zvol_write(void *arg)
         * Some requests are just for flush and nothing else.
         */
        if (size == 0) {
-               blk_end_request(req, 0, size);
+               error = 0;
                goto out;
        }
 
@@ -599,7 +605,6 @@ zvol_write(void *arg)
        if (error) {
                dmu_tx_abort(tx);
                zfs_range_unlock(rl);
-               blk_end_request(req, -error, size);
                goto out;
        }
 
@@ -615,9 +620,9 @@ zvol_write(void *arg)
            zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)
                zil_commit(zv->zv_zilog, ZVOL_OBJ);
 
-       blk_end_request(req, -error, size);
 out:
-       current->flags &= ~PF_NOFS;
+       blk_end_request(req, -error, size);
+       spl_fstrans_unmark(cookie);
 }
 
 #ifdef HAVE_BLK_QUEUE_DISCARD
@@ -627,21 +632,14 @@ zvol_discard(void *arg)
        struct request *req = (struct request *)arg;
        struct request_queue *q = req->q;
        zvol_state_t *zv = q->queuedata;
+       fstrans_cookie_t cookie = spl_fstrans_mark();
        uint64_t start = blk_rq_pos(req) << 9;
        uint64_t end = start + blk_rq_bytes(req);
        int error;
        rl_t *rl;
 
-       /*
-        * 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;
-
        if (end > zv->zv_volsize) {
-               blk_end_request(req, -EIO, blk_rq_bytes(req));
+               error = EIO;
                goto out;
        }
 
@@ -655,23 +653,22 @@ zvol_discard(void *arg)
        end = P2ALIGN(end, zv->zv_volblocksize);
 
        if (start >= end) {
-               blk_end_request(req, 0, blk_rq_bytes(req));
+               error = 0;
                goto out;
        }
 
        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);
+       error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, start, end-start);
 
        /*
         * TODO: maybe we should add the operation to the log.
         */
 
        zfs_range_unlock(rl);
-
-       blk_end_request(req, -error, blk_rq_bytes(req));
 out:
-       current->flags &= ~PF_NOFS;
+       blk_end_request(req, -error, blk_rq_bytes(req));
+       spl_fstrans_unmark(cookie);
 }
 #endif /* HAVE_BLK_QUEUE_DISCARD */
 
@@ -687,14 +684,15 @@ zvol_read(void *arg)
        struct request *req = (struct request *)arg;
        struct request_queue *q = req->q;
        zvol_state_t *zv = q->queuedata;
+       fstrans_cookie_t cookie = spl_fstrans_mark();
        uint64_t offset = blk_rq_pos(req) << 9;
        uint64_t size = blk_rq_bytes(req);
        int error;
        rl_t *rl;
 
        if (size == 0) {
-               blk_end_request(req, 0, size);
-               return;
+               error = 0;
+               goto out;
        }
 
        rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
@@ -705,9 +703,11 @@ zvol_read(void *arg)
 
        /* convert checksum errors into IO errors */
        if (error == ECKSUM)
-               error = EIO;
+               error = SET_ERROR(EIO);
 
+out:
        blk_end_request(req, -error, size);
+       spl_fstrans_unmark(cookie);
 }
 
 /*
@@ -749,28 +749,27 @@ zvol_request(struct request_queue *q)
                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));
+                           "%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;
                }
 
                if (!blk_fs_request(req)) {
                        printk(KERN_INFO "%s: non-fs cmd\n",
-                              req->rq_disk->disk_name);
+                           req->rq_disk->disk_name);
                        __blk_end_request(req, -EIO, size);
                        continue;
                }
 
-               switch (rq_data_dir(req)) {
+               switch ((int)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)) {
+                       if (unlikely(zv->zv_flags & ZVOL_RDONLY)) {
                                __blk_end_request(req, -EROFS, size);
                                break;
                        }
@@ -786,7 +785,7 @@ zvol_request(struct request_queue *q)
                        break;
                default:
                        printk(KERN_INFO "%s: unknown cmd: %d\n",
-                              req->rq_disk->disk_name, (int)rq_data_dir(req));
+                           req->rq_disk->disk_name, (int)rq_data_dir(req));
                        __blk_end_request(req, -EIO, size);
                        break;
                }
@@ -815,8 +814,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;
@@ -824,7 +825,7 @@ 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);
 
@@ -836,14 +837,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;
 
@@ -861,7 +868,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));
 }
 
 /*
@@ -875,7 +882,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;
        }
@@ -922,9 +929,13 @@ zvol_first_open(zvol_state_t *zv)
        if (!mutex_owned(&spa_namespace_lock)) {
                locked = mutex_tryenter(&spa_namespace_lock);
                if (!locked)
-                       return (-ERESTARTSYS);
+                       return (-SET_ERROR(ERESTARTSYS));
        }
 
+       error = dsl_prop_get_integer(zv->zv_name, "readonly", &ro, NULL);
+       if (error)
+               goto out_mutex;
+
        /* lie and say we're read-only */
        error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, 1, zvol_tag, &os);
        if (error)
@@ -947,7 +958,6 @@ zvol_first_open(zvol_state_t *zv)
        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);
@@ -961,7 +971,7 @@ out_mutex:
        if (locked)
                mutex_exit(&spa_namespace_lock);
 
-       return (-error);
+       return (SET_ERROR(-error));
 }
 
 static void
@@ -1010,8 +1020,7 @@ 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;
        }
@@ -1028,7 +1037,7 @@ out_mutex:
 
        check_disk_change(bdev);
 
-       return (error);
+       return (SET_ERROR(error));
 }
 
 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
@@ -1046,11 +1055,11 @@ zvol_release(struct gendisk *disk, fmode_t mode)
                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);
+       if (zv->zv_open_count > 0) {
+               zv->zv_open_count--;
+               if (zv->zv_open_count == 0)
+                       zvol_last_close(zv);
+       }
 
        if (drop_mutex)
                mutex_exit(&zvol_state_lock);
@@ -1062,13 +1071,13 @@ zvol_release(struct gendisk *disk, fmode_t mode)
 
 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);
+               return (SET_ERROR(-ENXIO));
 
        switch (cmd) {
        case BLKFLSBUF:
@@ -1084,25 +1093,25 @@ 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;
+       return (zv->zv_changed);
 }
 
 static int zvol_revalidate_disk(struct gendisk *disk)
@@ -1112,7 +1121,7 @@ static int zvol_revalidate_disk(struct gendisk *disk)
        zv->zv_changed = 0;
        set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
 
-       return 0;
+       return (0);
 }
 
 /*
@@ -1138,7 +1147,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 *
@@ -1152,19 +1161,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 */
@@ -1172,47 +1181,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 */
 
@@ -1272,14 +1283,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);
 }
 
 /*
@@ -1301,22 +1312,24 @@ zvol_free(zvol_state_t *zv)
 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);
+       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 = SET_ERROR(ENODEV);
+       }
+
+       kmem_free(parent, MAXPATHLEN);
+
+       return (SET_ERROR(error));
 }
 
 static int
@@ -1333,7 +1346,7 @@ __zvol_create_minor(const char *name, boolean_t ignore_snapdev)
 
        zv = zvol_find_by_name(name);
        if (zv) {
-               error = EEXIST;
+               error = SET_ERROR(EEXIST);
                goto out;
        }
 
@@ -1343,7 +1356,7 @@ __zvol_create_minor(const char *name, boolean_t ignore_snapdev)
                        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)
@@ -1363,7 +1376,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;
        }
 
@@ -1376,7 +1389,7 @@ __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 / 512);
        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);
@@ -1402,7 +1415,7 @@ __zvol_create_minor(const char *name, boolean_t ignore_snapdev)
 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) {
@@ -1410,7 +1423,7 @@ out:
                add_disk(zv->zv_disk);
        }
 
-       return (error);
+       return (SET_ERROR(error));
 }
 
 /*
@@ -1427,7 +1440,7 @@ zvol_create_minor(const char *name)
        error = __zvol_create_minor(name, B_FALSE);
        mutex_exit(&zvol_state_lock);
 
-       return (error);
+       return (SET_ERROR(error));
 }
 
 static int
@@ -1439,10 +1452,10 @@ __zvol_remove_minor(const char *name)
 
        zv = zvol_find_by_name(name);
        if (zv == NULL)
-               return (ENXIO);
+               return (SET_ERROR(ENXIO));
 
        if (zv->zv_open_count > 0)
-               return (EBUSY);
+               return (SET_ERROR(EBUSY));
 
        zvol_remove(zv);
        zvol_free(zv);
@@ -1462,80 +1475,121 @@ zvol_remove_minor(const char *name)
        error = __zvol_remove_minor(name);
        mutex_exit(&zvol_state_lock);
 
-       return (error);
+       return (SET_ERROR(error));
+}
+
+/*
+ * Rename a block device minor mode for the specified volume.
+ */
+static void
+__zvol_rename_minor(zvol_state_t *zv, const char *newname)
+{
+       int readonly = get_disk_ro(zv->zv_disk);
+
+       ASSERT(MUTEX_HELD(&zvol_state_lock));
+
+       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);
 }
 
 static int
 zvol_create_minors_cb(const char *dsname, void *arg)
 {
-       if (strchr(dsname, '/') == NULL)
-               return 0;
+       (void) zvol_create_minor(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 specified dataset including children and snapshots.
  */
 int
-zvol_create_minors(char *pool)
+zvol_create_minors(const char *name)
 {
-       spa_t *spa = NULL;
        int error = 0;
 
+       if (!zvol_inhibit_dev)
+               error = dmu_objset_find((char *)name, zvol_create_minors_cb,
+                   NULL, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
+
+       return (SET_ERROR(error));
+}
+
+/*
+ * Remove minors for specified dataset including children and snapshots.
+ */
+void
+zvol_remove_minors(const char *name)
+{
+       zvol_state_t *zv, *zv_next;
+       int namelen = ((name) ? strlen(name) : 0);
+
        if (zvol_inhibit_dev)
-               return (0);
+               return;
 
        mutex_enter(&zvol_state_lock);
-       if (pool) {
-               error = dmu_objset_find(pool, zvol_create_minors_cb,
-                   NULL, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
-       } else {
-               mutex_enter(&spa_namespace_lock);
-               while ((spa = spa_next(spa)) != NULL) {
-                       error = dmu_objset_find(spa_name(spa), zvol_create_minors_cb, NULL,
-                           DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
-                       if (error)
-                               break;
+
+       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] == '/')) {
+                       zvol_remove(zv);
+                       zvol_free(zv);
                }
-               mutex_exit(&spa_namespace_lock);
        }
-       mutex_exit(&zvol_state_lock);
 
-       return error;
+       mutex_exit(&zvol_state_lock);
 }
 
 /*
- * Remove minors for specified pool, if pool is NULL remove all minors.
+ * Rename minors for specified dataset including children and snapshots.
  */
 void
-zvol_remove_minors(const char *pool)
+zvol_rename_minors(const char *oldname, const char *newname)
 {
        zvol_state_t *zv, *zv_next;
-       char *str;
+       int oldnamelen, newnamelen;
+       char *name;
 
        if (zvol_inhibit_dev)
                return;
 
-       str = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
-       if (pool) {
-               (void) strncpy(str, pool, strlen(pool));
-               (void) strcat(str, "/");
-       }
+       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 (pool == NULL || !strncmp(str, zv->zv_name, strlen(str))) {
-                       zvol_remove(zv);
-                       zvol_free(zv);
+               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);
 }
 
 static int
@@ -1543,7 +1597,7 @@ snapdev_snapshot_changed_cb(const char *dsname, void *arg) {
        uint64_t snapdev = *(uint64_t *) arg;
 
        if (strchr(dsname, '@') == NULL)
-               return 0;
+               return (0);
 
        switch (snapdev) {
                case ZFS_SNAPDEV_VISIBLE:
@@ -1555,7 +1609,8 @@ snapdev_snapshot_changed_cb(const char *dsname, void *arg) {
                        (void) zvol_remove_minor(dsname);
                        break;
        }
-       return 0;
+
+       return (0);
 }
 
 int
@@ -1566,18 +1621,19 @@ zvol_set_snapdev(const char *dsname, uint64_t snapdev) {
        return (-1);
 }
 
-
 int
 zvol_init(void)
 {
+       int threads = MIN(MAX(zvol_threads, 1), 1024);
        int error;
 
        list_create(&zvol_state_list, sizeof (zvol_state_t),
-                   offsetof(zvol_state_t, zv_next));
+           offsetof(zvol_state_t, zv_next));
+
        mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
 
-       zvol_taskq = taskq_create(ZVOL_DRIVER, zvol_threads, maxclsyspri,
-                                 zvol_threads, INT_MAX, TASKQ_PREPOPULATE);
+       zvol_taskq = taskq_create(ZVOL_DRIVER, threads, maxclsyspri,
+           threads * 2, INT_MAX, TASKQ_PREPOPULATE | TASKQ_DYNAMIC);
        if (zvol_taskq == NULL) {
                printk(KERN_INFO "ZFS: taskq_create() failed\n");
                error = -ENOMEM;
@@ -1591,7 +1647,7 @@ zvol_init(void)
        }
 
        blk_register_region(MKDEV(zvol_major, 0), 1UL << MINORBITS,
-                           THIS_MODULE, zvol_probe, NULL, NULL);
+           THIS_MODULE, zvol_probe, NULL, NULL);
 
        return (0);
 
@@ -1601,7 +1657,7 @@ out1:
        mutex_destroy(&zvol_state_lock);
        list_destroy(&zvol_state_list);
 
-       return (error);
+       return (SET_ERROR(error));
 }
 
 void
@@ -1622,7 +1678,7 @@ 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_PARM_DESC(zvol_threads, "Max number of threads to handle I/O requests");
 
 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");