4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2006-2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
25 * All rights reserved.
28 /* Portions Copyright 2010 Robert Milkowski */
29 /* Portions Copyright 2011 Martin Matuska <mm@FreeBSD.org> */
32 * ZFS volume emulation driver.
34 * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes.
35 * Volumes are accessed through the symbolic links named:
37 * /dev/zvol/dsk/<pool_name>/<dataset_name>
38 * /dev/zvol/rdsk/<pool_name>/<dataset_name>
40 * These links are created by the /dev filesystem (sdev_zvolops.c).
41 * Volumes are persistent through reboot. No user command needs to be
42 * run before opening and using a device.
45 * On FreeBSD ZVOLs are simply GEOM providers like any other storage device
49 #include <sys/types.h>
50 #include <sys/param.h>
51 #include <sys/kernel.h>
52 #include <sys/errno.h>
58 #include <sys/cmn_err.h>
63 #include <sys/dmu_traverse.h>
64 #include <sys/dnode.h>
65 #include <sys/dsl_dataset.h>
66 #include <sys/dsl_prop.h>
68 #include <sys/byteorder.h>
69 #include <sys/sunddi.h>
70 #include <sys/dirent.h>
71 #include <sys/policy.h>
72 #include <sys/fs/zfs.h>
73 #include <sys/zfs_ioctl.h>
75 #include <sys/refcount.h>
76 #include <sys/zfs_znode.h>
77 #include <sys/zfs_rlock.h>
78 #include <sys/vdev_impl.h>
80 #include <sys/zil_impl.h>
81 #include <geom/geom.h>
83 #include "zfs_namecheck.h"
85 struct g_class zfs_zvol_class = {
90 DECLARE_GEOM_CLASS(zfs_zvol_class, zfs_zvol);
93 static char *zvol_tag = "zvol_tag";
95 #define ZVOL_DUMPSIZE "dumpsize"
98 * The spa_namespace_lock protects the zfsdev_state structure from being
99 * modified while it's being used, e.g. an open that comes in before a
100 * create finishes. It also protects temporary opens of the dataset so that,
101 * e.g., an open doesn't get a spurious EBUSY.
103 static uint32_t zvol_minors;
105 typedef struct zvol_extent {
107 dva_t ze_dva; /* dva associated with this extent */
108 uint64_t ze_nblks; /* number of blocks in extent */
112 * The in-core state of each volume.
114 typedef struct zvol_state {
115 char zv_name[MAXPATHLEN]; /* pool/dd name */
116 uint64_t zv_volsize; /* amount of space we advertise */
117 uint64_t zv_volblocksize; /* volume block size */
118 struct g_provider *zv_provider; /* GEOM provider */
119 uint8_t zv_min_bs; /* minimum addressable block shift */
120 uint8_t zv_flags; /* readonly, dumpified, etc. */
121 objset_t *zv_objset; /* objset handle */
122 uint32_t zv_total_opens; /* total open count */
123 zilog_t *zv_zilog; /* ZIL handle */
124 list_t zv_extents; /* List of extents for dump */
125 znode_t zv_znode; /* for range locking */
126 dmu_buf_t *zv_dbuf; /* bonus handle */
128 struct bio_queue_head zv_queue;
129 struct mtx zv_queue_mtx; /* zv_queue mutex */
133 * zvol specific flags
135 #define ZVOL_RDONLY 0x1
136 #define ZVOL_DUMPIFIED 0x2
137 #define ZVOL_EXCL 0x4
141 * zvol maximum transfer in one DMU tx.
143 int zvol_maxphys = DMU_MAX_ACCESS/2;
145 extern int zfs_set_prop_nvlist(const char *, zprop_source_t,
146 nvlist_t *, nvlist_t **);
147 static int zvol_remove_zv(zvol_state_t *);
148 static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio);
149 static int zvol_dumpify(zvol_state_t *zv);
150 static int zvol_dump_fini(zvol_state_t *zv);
151 static int zvol_dump_init(zvol_state_t *zv, boolean_t resize);
153 static zvol_state_t *zvol_geom_create(const char *name);
154 static void zvol_geom_run(zvol_state_t *zv);
155 static void zvol_geom_destroy(zvol_state_t *zv);
156 static int zvol_geom_access(struct g_provider *pp, int acr, int acw, int ace);
157 static void zvol_geom_start(struct bio *bp);
158 static void zvol_geom_worker(void *arg);
161 zvol_size_changed(zvol_state_t *zv)
164 dev_t dev = makedevice(maj, min);
166 VERIFY(ddi_prop_update_int64(dev, zfs_dip,
167 "Size", volsize) == DDI_SUCCESS);
168 VERIFY(ddi_prop_update_int64(dev, zfs_dip,
169 "Nblocks", lbtodb(volsize)) == DDI_SUCCESS);
171 /* Notify specfs to invalidate the cached size */
172 spec_size_invalidate(dev, VBLK);
173 spec_size_invalidate(dev, VCHR);
175 struct g_provider *pp;
177 pp = zv->zv_provider;
180 if (zv->zv_volsize == pp->mediasize)
183 * Changing provider size is not really supported by GEOM, but it
184 * should be safe when provider is closed.
186 if (zv->zv_total_opens > 0)
188 pp->mediasize = zv->zv_volsize;
193 zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
198 if (volsize % blocksize != 0)
202 if (volsize - 1 > SPEC_MAXOFFSET_T)
209 zvol_check_volblocksize(uint64_t volblocksize)
211 if (volblocksize < SPA_MINBLOCKSIZE ||
212 volblocksize > SPA_MAXBLOCKSIZE ||
220 zvol_get_stats(objset_t *os, nvlist_t *nv)
223 dmu_object_info_t doi;
226 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
230 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
232 error = dmu_object_info(os, ZVOL_OBJ, &doi);
235 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
236 doi.doi_data_block_size);
242 static zvol_state_t *
243 zvol_minor_lookup(const char *name)
245 struct g_provider *pp;
247 zvol_state_t *zv = NULL;
249 ASSERT(MUTEX_HELD(&spa_namespace_lock));
252 LIST_FOREACH(gp, &zfs_zvol_class.geom, geom) {
253 pp = LIST_FIRST(&gp->provider);
259 if (strcmp(zv->zv_name, name) == 0)
264 return (gp != NULL ? zv : NULL);
267 /* extent mapping arg */
275 zvol_map_block(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, arc_buf_t *pbuf,
276 const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
278 struct maparg *ma = arg;
280 int bs = ma->ma_zv->zv_volblocksize;
282 if (bp == NULL || zb->zb_object != ZVOL_OBJ || zb->zb_level != 0)
285 VERIFY3U(ma->ma_blks, ==, zb->zb_blkid);
288 /* Abort immediately if we have encountered gang blocks */
293 * See if the block is at the end of the previous extent.
295 ze = list_tail(&ma->ma_zv->zv_extents);
297 DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) &&
298 DVA_GET_OFFSET(BP_IDENTITY(bp)) ==
299 DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) {
304 dprintf_bp(bp, "%s", "next blkptr:");
306 /* start a new extent */
307 ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP);
308 ze->ze_dva = bp->blk_dva[0]; /* structure assignment */
310 list_insert_tail(&ma->ma_zv->zv_extents, ze);
315 zvol_free_extents(zvol_state_t *zv)
319 while (ze = list_head(&zv->zv_extents)) {
320 list_remove(&zv->zv_extents, ze);
321 kmem_free(ze, sizeof (zvol_extent_t));
326 zvol_get_lbas(zvol_state_t *zv)
328 objset_t *os = zv->zv_objset;
334 zvol_free_extents(zv);
336 /* commit any in-flight changes before traversing the dataset */
337 txg_wait_synced(dmu_objset_pool(os), 0);
338 err = traverse_dataset(dmu_objset_ds(os), 0,
339 TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma);
340 if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) {
341 zvol_free_extents(zv);
342 return (err ? err : EIO);
350 zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
352 zfs_creat_t *zct = arg;
353 nvlist_t *nvprops = zct->zct_props;
355 uint64_t volblocksize, volsize;
357 VERIFY(nvlist_lookup_uint64(nvprops,
358 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
359 if (nvlist_lookup_uint64(nvprops,
360 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
361 volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
364 * These properties must be removed from the list so the generic
365 * property setting step won't apply to them.
367 VERIFY(nvlist_remove_all(nvprops,
368 zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
369 (void) nvlist_remove_all(nvprops,
370 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
372 error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
376 error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
380 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
385 * Replay a TX_WRITE ZIL transaction that didn't get committed
386 * after a system failure
389 zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
391 objset_t *os = zv->zv_objset;
392 char *data = (char *)(lr + 1); /* data follows lr_write_t */
393 uint64_t offset, length;
398 byteswap_uint64_array(lr, sizeof (*lr));
400 offset = lr->lr_offset;
401 length = lr->lr_length;
403 /* If it's a dmu_sync() block, write the whole block */
404 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
405 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
406 if (length < blocksize) {
407 offset -= offset % blocksize;
412 tx = dmu_tx_create(os);
413 dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length);
414 error = dmu_tx_assign(tx, TXG_WAIT);
418 dmu_write(os, ZVOL_OBJ, offset, length, data, tx);
427 zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
433 * Callback vectors for replaying records.
434 * Only TX_WRITE is needed for zvol.
436 zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = {
437 zvol_replay_err, /* 0 no such transaction type */
438 zvol_replay_err, /* TX_CREATE */
439 zvol_replay_err, /* TX_MKDIR */
440 zvol_replay_err, /* TX_MKXATTR */
441 zvol_replay_err, /* TX_SYMLINK */
442 zvol_replay_err, /* TX_REMOVE */
443 zvol_replay_err, /* TX_RMDIR */
444 zvol_replay_err, /* TX_LINK */
445 zvol_replay_err, /* TX_RENAME */
446 zvol_replay_write, /* TX_WRITE */
447 zvol_replay_err, /* TX_TRUNCATE */
448 zvol_replay_err, /* TX_SETATTR */
449 zvol_replay_err, /* TX_ACL */
450 zvol_replay_err, /* TX_CREATE_ACL */
451 zvol_replay_err, /* TX_CREATE_ATTR */
452 zvol_replay_err, /* TX_CREATE_ACL_ATTR */
453 zvol_replay_err, /* TX_MKDIR_ACL */
454 zvol_replay_err, /* TX_MKDIR_ATTR */
455 zvol_replay_err, /* TX_MKDIR_ACL_ATTR */
456 zvol_replay_err, /* TX_WRITE2 */
461 zvol_name2minor(const char *name, minor_t *minor)
465 mutex_enter(&spa_namespace_lock);
466 zv = zvol_minor_lookup(name);
468 *minor = zv->zv_minor;
469 mutex_exit(&spa_namespace_lock);
470 return (zv ? 0 : -1);
475 * Create a minor node (plus a whole lot more) for the specified volume.
478 zvol_create_minor(const char *name)
480 zfs_soft_state_t *zs;
483 dmu_object_info_t doi;
487 ZFS_LOG(1, "Creating ZVOL %s...", name);
489 mutex_enter(&spa_namespace_lock);
491 if (zvol_minor_lookup(name) != NULL) {
492 mutex_exit(&spa_namespace_lock);
496 /* lie and say we're read-only */
497 error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, FTAG, &os);
500 mutex_exit(&spa_namespace_lock);
505 if ((minor = zfsdev_minor_alloc()) == 0) {
506 dmu_objset_disown(os, FTAG);
507 mutex_exit(&spa_namespace_lock);
511 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS) {
512 dmu_objset_disown(os, FTAG);
513 mutex_exit(&spa_namespace_lock);
516 (void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME,
519 (void) snprintf(chrbuf, sizeof (chrbuf), "%u,raw", minor);
521 if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR,
522 minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
523 ddi_soft_state_free(zfsdev_state, minor);
524 dmu_objset_disown(os, FTAG);
525 mutex_exit(&spa_namespace_lock);
529 (void) snprintf(blkbuf, sizeof (blkbuf), "%u", minor);
531 if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK,
532 minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
533 ddi_remove_minor_node(zfs_dip, chrbuf);
534 ddi_soft_state_free(zfsdev_state, minor);
535 dmu_objset_disown(os, FTAG);
536 mutex_exit(&spa_namespace_lock);
540 zs = ddi_get_soft_state(zfsdev_state, minor);
541 zs->zss_type = ZSST_ZVOL;
542 zv = zs->zss_data = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP);
545 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
548 dmu_objset_disown(os, zvol_tag);
549 mutex_exit(&spa_namespace_lock);
555 zv = zvol_geom_create(name);
556 zv->zv_volsize = volsize;
557 zv->zv_provider->mediasize = zv->zv_volsize;
561 (void) strlcpy(zv->zv_name, name, MAXPATHLEN);
562 zv->zv_min_bs = DEV_BSHIFT;
564 if (dmu_objset_is_snapshot(os) || !spa_writeable(dmu_objset_spa(os)))
565 zv->zv_flags |= ZVOL_RDONLY;
566 mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
567 avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
568 sizeof (rl_t), offsetof(rl_t, r_node));
569 list_create(&zv->zv_extents, sizeof (zvol_extent_t),
570 offsetof(zvol_extent_t, ze_node));
571 /* get and cache the blocksize */
572 error = dmu_object_info(os, ZVOL_OBJ, &doi);
574 zv->zv_volblocksize = doi.doi_data_block_size;
576 if (spa_writeable(dmu_objset_spa(os))) {
577 if (zil_replay_disable)
578 zil_destroy(dmu_objset_zil(os), B_FALSE);
580 zil_replay(os, zv, zvol_replay_vector);
582 dmu_objset_disown(os, FTAG);
583 zv->zv_objset = NULL;
587 mutex_exit(&spa_namespace_lock);
594 ZFS_LOG(1, "ZVOL %s created.", name);
600 * Remove minor node for the specified volume.
603 zvol_remove_zv(zvol_state_t *zv)
606 minor_t minor = zv->zv_minor;
609 ASSERT(MUTEX_HELD(&spa_namespace_lock));
610 if (zv->zv_total_opens != 0)
613 ZFS_LOG(1, "ZVOL %s destroyed.", zv->zv_name);
616 (void) snprintf(nmbuf, sizeof (nmbuf), "%u,raw", minor);
617 ddi_remove_minor_node(zfs_dip, nmbuf);
620 avl_destroy(&zv->zv_znode.z_range_avl);
621 mutex_destroy(&zv->zv_znode.z_range_lock);
623 zvol_geom_destroy(zv);
630 zvol_remove_minor(const char *name)
635 mutex_enter(&spa_namespace_lock);
636 if ((zv = zvol_minor_lookup(name)) == NULL) {
637 mutex_exit(&spa_namespace_lock);
641 rc = zvol_remove_zv(zv);
643 mutex_exit(&spa_namespace_lock);
648 zvol_first_open(zvol_state_t *zv)
655 /* lie and say we're read-only */
656 error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, B_TRUE,
661 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
664 dmu_objset_disown(os, zvol_tag);
668 error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf);
670 dmu_objset_disown(os, zvol_tag);
673 zv->zv_volsize = volsize;
674 zv->zv_zilog = zil_open(os, zvol_get_data);
675 zvol_size_changed(zv);
677 VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &readonly,
679 if (readonly || dmu_objset_is_snapshot(os) ||
680 !spa_writeable(dmu_objset_spa(os)))
681 zv->zv_flags |= ZVOL_RDONLY;
683 zv->zv_flags &= ~ZVOL_RDONLY;
688 zvol_last_close(zvol_state_t *zv)
690 zil_close(zv->zv_zilog);
693 dmu_buf_rele(zv->zv_dbuf, zvol_tag);
699 if (dsl_dataset_is_dirty(dmu_objset_ds(zv->zv_objset)) &&
700 !(zv->zv_flags & ZVOL_RDONLY))
701 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
702 (void) dmu_objset_evict_dbufs(zv->zv_objset);
704 dmu_objset_disown(zv->zv_objset, zvol_tag);
705 zv->zv_objset = NULL;
710 zvol_prealloc(zvol_state_t *zv)
712 objset_t *os = zv->zv_objset;
714 uint64_t refd, avail, usedobjs, availobjs;
715 uint64_t resid = zv->zv_volsize;
718 /* Check the space usage before attempting to allocate the space */
719 dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs);
720 if (avail < zv->zv_volsize)
723 /* Free old extents if they exist */
724 zvol_free_extents(zv);
728 uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE);
730 tx = dmu_tx_create(os);
731 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
732 error = dmu_tx_assign(tx, TXG_WAIT);
735 (void) dmu_free_long_range(os, ZVOL_OBJ, 0, off);
738 dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx);
743 txg_wait_synced(dmu_objset_pool(os), 0);
750 zvol_update_volsize(objset_t *os, uint64_t volsize)
755 ASSERT(MUTEX_HELD(&spa_namespace_lock));
757 tx = dmu_tx_create(os);
758 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
759 error = dmu_tx_assign(tx, TXG_WAIT);
765 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
770 error = dmu_free_long_range(os,
771 ZVOL_OBJ, volsize, DMU_OBJECT_END);
776 zvol_remove_minors(const char *name)
778 struct g_geom *gp, *gptmp;
779 struct g_provider *pp;
783 namelen = strlen(name);
786 mutex_enter(&spa_namespace_lock);
789 LIST_FOREACH_SAFE(gp, &zfs_zvol_class.geom, geom, gptmp) {
790 pp = LIST_FIRST(&gp->provider);
796 if (strcmp(zv->zv_name, name) == 0 ||
797 (strncmp(zv->zv_name, name, namelen) == 0 &&
798 zv->zv_name[namelen] == '/')) {
799 (void) zvol_remove_zv(zv);
804 mutex_exit(&spa_namespace_lock);
809 zvol_set_volsize(const char *name, major_t maj, uint64_t volsize)
811 zvol_state_t *zv = NULL;
814 dmu_object_info_t doi;
815 uint64_t old_volsize = 0ULL;
818 mutex_enter(&spa_namespace_lock);
819 zv = zvol_minor_lookup(name);
820 if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) {
821 mutex_exit(&spa_namespace_lock);
825 if ((error = dmu_object_info(os, ZVOL_OBJ, &doi)) != 0 ||
826 (error = zvol_check_volsize(volsize,
827 doi.doi_data_block_size)) != 0)
830 VERIFY(dsl_prop_get_integer(name, "readonly", &readonly,
837 error = zvol_update_volsize(os, volsize);
839 * Reinitialize the dump area to the new size. If we
840 * failed to resize the dump area then restore it back to
843 if (zv && error == 0) {
845 if (zv->zv_flags & ZVOL_DUMPIFIED) {
846 old_volsize = zv->zv_volsize;
847 zv->zv_volsize = volsize;
848 if ((error = zvol_dumpify(zv)) != 0 ||
849 (error = dumpvp_resize()) != 0) {
850 (void) zvol_update_volsize(os, old_volsize);
851 zv->zv_volsize = old_volsize;
852 error = zvol_dumpify(zv);
855 #endif /* ZVOL_DUMP */
857 zv->zv_volsize = volsize;
858 zvol_size_changed(zv);
864 * Generate a LUN expansion event.
866 if (zv && error == 0) {
869 char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
871 (void) snprintf(physpath, MAXPATHLEN, "%s%u", ZVOL_PSEUDO_DEV,
874 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
875 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
877 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
878 ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
881 kmem_free(physpath, MAXPATHLEN);
886 dmu_objset_rele(os, FTAG);
888 mutex_exit(&spa_namespace_lock);
895 zvol_open(struct g_provider *pp, int flag, int count)
899 boolean_t locked = B_FALSE;
902 * Protect against recursively entering spa_namespace_lock
903 * when spa_open() is used for a pool on a (local) ZVOL(s).
904 * This is needed since we replaced upstream zfsdev_state_lock
905 * with spa_namespace_lock in the ZVOL code.
906 * We are using the same trick as spa_open().
907 * Note that calls in zvol_first_open which need to resolve
908 * pool name to a spa object will enter spa_open()
909 * recursively, but that function already has all the
910 * necessary protection.
912 if (!MUTEX_HELD(&spa_namespace_lock)) {
913 mutex_enter(&spa_namespace_lock);
920 mutex_exit(&spa_namespace_lock);
924 if (zv->zv_total_opens == 0)
925 err = zvol_first_open(zv);
928 mutex_exit(&spa_namespace_lock);
931 if ((flag & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
935 if (zv->zv_flags & ZVOL_EXCL) {
941 if (zv->zv_total_opens != 0) {
945 zv->zv_flags |= ZVOL_EXCL;
949 zv->zv_total_opens += count;
951 mutex_exit(&spa_namespace_lock);
955 if (zv->zv_total_opens == 0)
958 mutex_exit(&spa_namespace_lock);
964 zvol_close(struct g_provider *pp, int flag, int count)
968 boolean_t locked = B_FALSE;
970 /* See comment in zvol_open(). */
971 if (!MUTEX_HELD(&spa_namespace_lock)) {
972 mutex_enter(&spa_namespace_lock);
979 mutex_exit(&spa_namespace_lock);
983 if (zv->zv_flags & ZVOL_EXCL) {
984 ASSERT(zv->zv_total_opens == 1);
985 zv->zv_flags &= ~ZVOL_EXCL;
989 * If the open count is zero, this is a spurious close.
990 * That indicates a bug in the kernel / DDI framework.
992 ASSERT(zv->zv_total_opens != 0);
995 * You may get multiple opens, but only one close.
997 zv->zv_total_opens -= count;
999 if (zv->zv_total_opens == 0)
1000 zvol_last_close(zv);
1003 mutex_exit(&spa_namespace_lock);
1008 zvol_get_done(zgd_t *zgd, int error)
1011 dmu_buf_rele(zgd->zgd_db, zgd);
1013 zfs_range_unlock(zgd->zgd_rl);
1015 if (error == 0 && zgd->zgd_bp)
1016 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1018 kmem_free(zgd, sizeof (zgd_t));
1022 * Get data to generate a TX_WRITE intent log record.
1025 zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1027 zvol_state_t *zv = arg;
1028 objset_t *os = zv->zv_objset;
1029 uint64_t object = ZVOL_OBJ;
1030 uint64_t offset = lr->lr_offset;
1031 uint64_t size = lr->lr_length; /* length of user data */
1032 blkptr_t *bp = &lr->lr_blkptr;
1037 ASSERT(zio != NULL);
1040 zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1041 zgd->zgd_zilog = zv->zv_zilog;
1042 zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
1045 * Write records come in two flavors: immediate and indirect.
1046 * For small writes it's cheaper to store the data with the
1047 * log record (immediate); for large writes it's cheaper to
1048 * sync the data and get a pointer to it (indirect) so that
1049 * we don't have to write the data twice.
1051 if (buf != NULL) { /* immediate write */
1052 error = dmu_read(os, object, offset, size, buf,
1053 DMU_READ_NO_PREFETCH);
1055 size = zv->zv_volblocksize;
1056 offset = P2ALIGN(offset, size);
1057 error = dmu_buf_hold(os, object, offset, zgd, &db,
1058 DMU_READ_NO_PREFETCH);
1063 ASSERT(db->db_offset == offset);
1064 ASSERT(db->db_size == size);
1066 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1067 zvol_get_done, zgd);
1074 zvol_get_done(zgd, error);
1080 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
1082 * We store data in the log buffers if it's small enough.
1083 * Otherwise we will later flush the data out via dmu_sync().
1085 ssize_t zvol_immediate_write_sz = 32768;
1088 zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid,
1091 uint32_t blocksize = zv->zv_volblocksize;
1092 zilog_t *zilog = zv->zv_zilog;
1094 ssize_t immediate_write_sz;
1096 if (zil_replaying(zilog, tx))
1099 immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
1100 ? 0 : zvol_immediate_write_sz;
1102 slogging = spa_has_slogs(zilog->zl_spa) &&
1103 (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
1109 itx_wr_state_t write_state;
1112 * Unlike zfs_log_write() we can be called with
1113 * upto DMU_MAX_ACCESS/2 (5MB) writes.
1115 if (blocksize > immediate_write_sz && !slogging &&
1116 resid >= blocksize && off % blocksize == 0) {
1117 write_state = WR_INDIRECT; /* uses dmu_sync */
1120 write_state = WR_COPIED;
1121 len = MIN(ZIL_MAX_LOG_DATA, resid);
1123 write_state = WR_NEED_COPY;
1124 len = MIN(ZIL_MAX_LOG_DATA, resid);
1127 itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
1128 (write_state == WR_COPIED ? len : 0));
1129 lr = (lr_write_t *)&itx->itx_lr;
1130 if (write_state == WR_COPIED && dmu_read(zv->zv_objset,
1131 ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
1132 zil_itx_destroy(itx);
1133 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1134 lr = (lr_write_t *)&itx->itx_lr;
1135 write_state = WR_NEED_COPY;
1138 itx->itx_wr_state = write_state;
1139 if (write_state == WR_NEED_COPY)
1140 itx->itx_sod += len;
1141 lr->lr_foid = ZVOL_OBJ;
1142 lr->lr_offset = off;
1143 lr->lr_length = len;
1145 BP_ZERO(&lr->lr_blkptr);
1147 itx->itx_private = zv;
1148 itx->itx_sync = sync;
1150 zil_itx_assign(zilog, itx, tx);
1159 zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t size,
1160 boolean_t doread, boolean_t isdump)
1166 for (c = 0; c < vd->vdev_children; c++) {
1167 ASSERT(vd->vdev_ops == &vdev_mirror_ops ||
1168 vd->vdev_ops == &vdev_replacing_ops ||
1169 vd->vdev_ops == &vdev_spare_ops);
1170 int err = zvol_dumpio_vdev(vd->vdev_child[c],
1171 addr, offset, size, doread, isdump);
1174 } else if (doread) {
1179 if (!vd->vdev_ops->vdev_op_leaf)
1180 return (numerrors < vd->vdev_children ? 0 : EIO);
1182 if (doread && !vdev_readable(vd))
1184 else if (!doread && !vdev_writeable(vd))
1188 ASSERT3P(dvd, !=, NULL);
1189 offset += VDEV_LABEL_START_SIZE;
1191 if (ddi_in_panic() || isdump) {
1195 return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset),
1198 return (vdev_disk_physio(dvd->vd_lh, addr, size, offset,
1199 doread ? B_READ : B_WRITE));
1204 zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size,
1205 boolean_t doread, boolean_t isdump)
1210 spa_t *spa = dmu_objset_spa(zv->zv_objset);
1212 /* Must be sector aligned, and not stradle a block boundary. */
1213 if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) ||
1214 P2BOUNDARY(offset, size, zv->zv_volblocksize)) {
1217 ASSERT(size <= zv->zv_volblocksize);
1219 /* Locate the extent this belongs to */
1220 ze = list_head(&zv->zv_extents);
1221 while (offset >= ze->ze_nblks * zv->zv_volblocksize) {
1222 offset -= ze->ze_nblks * zv->zv_volblocksize;
1223 ze = list_next(&zv->zv_extents, ze);
1226 if (!ddi_in_panic())
1227 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
1229 vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva));
1230 offset += DVA_GET_OFFSET(&ze->ze_dva);
1231 error = zvol_dumpio_vdev(vd, addr, offset, size, doread, isdump);
1233 if (!ddi_in_panic())
1234 spa_config_exit(spa, SCL_STATE, FTAG);
1241 zvol_strategy(struct bio *bp)
1243 zvol_state_t *zv = bp->bio_to->private;
1244 uint64_t off, volsize;
1250 boolean_t doread = (bp->bio_cmd == BIO_READ);
1254 g_io_deliver(bp, ENXIO);
1258 if (bp->bio_cmd != BIO_READ && (zv->zv_flags & ZVOL_RDONLY)) {
1259 g_io_deliver(bp, EROFS);
1263 off = bp->bio_offset;
1264 volsize = zv->zv_volsize;
1269 addr = bp->bio_data;
1270 resid = bp->bio_length;
1272 if (resid > 0 && (off < 0 || off >= volsize)) {
1273 g_io_deliver(bp, EIO);
1277 sync = !doread && zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS;
1280 * There must be no buffer changes when doing a dmu_sync() because
1281 * we can't change the data whilst calculating the checksum.
1283 rl = zfs_range_lock(&zv->zv_znode, off, resid,
1284 doread ? RL_READER : RL_WRITER);
1286 while (resid != 0 && off < volsize) {
1287 size_t size = MIN(resid, zvol_maxphys);
1289 error = dmu_read(os, ZVOL_OBJ, off, size, addr,
1292 dmu_tx_t *tx = dmu_tx_create(os);
1293 dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
1294 error = dmu_tx_assign(tx, TXG_WAIT);
1298 dmu_write(os, ZVOL_OBJ, off, size, addr, tx);
1299 zvol_log_write(zv, tx, off, size, sync);
1304 /* convert checksum errors into IO errors */
1305 if (error == ECKSUM)
1313 zfs_range_unlock(rl);
1315 bp->bio_completed = bp->bio_length - resid;
1316 if (bp->bio_completed < bp->bio_length)
1317 bp->bio_error = (off > volsize ? EINVAL : error);
1320 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1321 g_io_deliver(bp, 0);
1328 * Set the buffer count to the zvol maximum transfer.
1329 * Using our own routine instead of the default minphys()
1330 * means that for larger writes we write bigger buffers on X86
1331 * (128K instead of 56K) and flush the disk write cache less often
1332 * (every zvol_maxphys - currently 1MB) instead of minphys (currently
1333 * 56K on X86 and 128K on sparc).
1336 zvol_minphys(struct buf *bp)
1338 if (bp->b_bcount > zvol_maxphys)
1339 bp->b_bcount = zvol_maxphys;
1343 zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks)
1345 minor_t minor = getminor(dev);
1352 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1356 boff = ldbtob(blkno);
1357 resid = ldbtob(nblocks);
1359 VERIFY3U(boff + resid, <=, zv->zv_volsize);
1362 size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff);
1363 error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE);
1376 zvol_read(dev_t dev, uio_t *uio, cred_t *cr)
1378 minor_t minor = getminor(dev);
1384 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1388 volsize = zv->zv_volsize;
1389 if (uio->uio_resid > 0 &&
1390 (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
1393 if (zv->zv_flags & ZVOL_DUMPIFIED) {
1394 error = physio(zvol_strategy, NULL, dev, B_READ,
1399 rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1401 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1402 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1404 /* don't read past the end */
1405 if (bytes > volsize - uio->uio_loffset)
1406 bytes = volsize - uio->uio_loffset;
1408 error = dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes);
1410 /* convert checksum errors into IO errors */
1411 if (error == ECKSUM)
1416 zfs_range_unlock(rl);
1422 zvol_write(dev_t dev, uio_t *uio, cred_t *cr)
1424 minor_t minor = getminor(dev);
1431 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1435 volsize = zv->zv_volsize;
1436 if (uio->uio_resid > 0 &&
1437 (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
1440 if (zv->zv_flags & ZVOL_DUMPIFIED) {
1441 error = physio(zvol_strategy, NULL, dev, B_WRITE,
1446 sync = !(zv->zv_flags & ZVOL_WCE) ||
1447 (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS);
1449 rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1451 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1452 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1453 uint64_t off = uio->uio_loffset;
1454 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
1456 if (bytes > volsize - off) /* don't write past the end */
1457 bytes = volsize - off;
1459 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
1460 error = dmu_tx_assign(tx, TXG_WAIT);
1465 error = dmu_write_uio_dbuf(zv->zv_dbuf, uio, bytes, tx);
1467 zvol_log_write(zv, tx, off, bytes, sync);
1473 zfs_range_unlock(rl);
1475 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1480 zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs)
1482 struct uuid uuid = EFI_RESERVED;
1483 efi_gpe_t gpe = { 0 };
1489 if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag))
1491 ptr = (char *)(uintptr_t)efi.dki_data_64;
1492 length = efi.dki_length;
1494 * Some clients may attempt to request a PMBR for the
1495 * zvol. Currently this interface will return EINVAL to
1496 * such requests. These requests could be supported by
1497 * adding a check for lba == 0 and consing up an appropriate
1500 if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0)
1503 gpe.efi_gpe_StartingLBA = LE_64(34ULL);
1504 gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1);
1505 UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
1507 if (efi.dki_lba == 1) {
1508 efi_gpt_t gpt = { 0 };
1510 gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
1511 gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
1512 gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt));
1513 gpt.efi_gpt_MyLBA = LE_64(1ULL);
1514 gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL);
1515 gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1);
1516 gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL);
1517 gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
1518 gpt.efi_gpt_SizeOfPartitionEntry =
1519 LE_32(sizeof (efi_gpe_t));
1520 CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
1521 gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
1522 CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table);
1523 gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
1524 if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length),
1527 ptr += sizeof (gpt);
1528 length -= sizeof (gpt);
1530 if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe),
1537 * BEGIN entry points to allow external callers access to the volume.
1540 * Return the volume parameters needed for access from an external caller.
1541 * These values are invariant as long as the volume is held open.
1544 zvol_get_volume_params(minor_t minor, uint64_t *blksize,
1545 uint64_t *max_xfer_len, void **minor_hdl, void **objset_hdl, void **zil_hdl,
1546 void **rl_hdl, void **bonus_hdl)
1550 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1553 if (zv->zv_flags & ZVOL_DUMPIFIED)
1556 ASSERT(blksize && max_xfer_len && minor_hdl &&
1557 objset_hdl && zil_hdl && rl_hdl && bonus_hdl);
1559 *blksize = zv->zv_volblocksize;
1560 *max_xfer_len = (uint64_t)zvol_maxphys;
1562 *objset_hdl = zv->zv_objset;
1563 *zil_hdl = zv->zv_zilog;
1564 *rl_hdl = &zv->zv_znode;
1565 *bonus_hdl = zv->zv_dbuf;
1570 * Return the current volume size to an external caller.
1571 * The size can change while the volume is open.
1574 zvol_get_volume_size(void *minor_hdl)
1576 zvol_state_t *zv = minor_hdl;
1578 return (zv->zv_volsize);
1582 * Return the current WCE setting to an external caller.
1583 * The WCE setting can change while the volume is open.
1586 zvol_get_volume_wce(void *minor_hdl)
1588 zvol_state_t *zv = minor_hdl;
1590 return ((zv->zv_flags & ZVOL_WCE) ? 1 : 0);
1594 * Entry point for external callers to zvol_log_write
1597 zvol_log_write_minor(void *minor_hdl, dmu_tx_t *tx, offset_t off, ssize_t resid,
1600 zvol_state_t *zv = minor_hdl;
1602 zvol_log_write(zv, tx, off, resid, sync);
1605 * END entry points to allow external callers access to the volume.
1609 * Dirtbag ioctls to support mkfs(1M) for UFS filesystems. See dkio(7I).
1613 zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
1616 struct dk_cinfo dki;
1617 struct dk_minfo dkm;
1618 struct dk_callback *dkc;
1622 mutex_enter(&spa_namespace_lock);
1624 zv = zfsdev_get_soft_state(getminor(dev), ZSST_ZVOL);
1627 mutex_exit(&spa_namespace_lock);
1630 ASSERT(zv->zv_total_opens > 0);
1635 bzero(&dki, sizeof (dki));
1636 (void) strcpy(dki.dki_cname, "zvol");
1637 (void) strcpy(dki.dki_dname, "zvol");
1638 dki.dki_ctype = DKC_UNKNOWN;
1639 dki.dki_unit = getminor(dev);
1640 dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs);
1641 mutex_exit(&spa_namespace_lock);
1642 if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag))
1646 case DKIOCGMEDIAINFO:
1647 bzero(&dkm, sizeof (dkm));
1648 dkm.dki_lbsize = 1U << zv->zv_min_bs;
1649 dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
1650 dkm.dki_media_type = DK_UNKNOWN;
1651 mutex_exit(&spa_namespace_lock);
1652 if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
1658 uint64_t vs = zv->zv_volsize;
1659 uint8_t bs = zv->zv_min_bs;
1661 mutex_exit(&spa_namespace_lock);
1662 error = zvol_getefi((void *)arg, flag, vs, bs);
1666 case DKIOCFLUSHWRITECACHE:
1667 dkc = (struct dk_callback *)arg;
1668 mutex_exit(&spa_namespace_lock);
1669 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1670 if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) {
1671 (*dkc->dkc_callback)(dkc->dkc_cookie, error);
1678 int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0;
1679 if (ddi_copyout(&wce, (void *)arg, sizeof (int),
1687 if (ddi_copyin((void *)arg, &wce, sizeof (int),
1693 zv->zv_flags |= ZVOL_WCE;
1694 mutex_exit(&spa_namespace_lock);
1696 zv->zv_flags &= ~ZVOL_WCE;
1697 mutex_exit(&spa_namespace_lock);
1698 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1706 * commands using these (like prtvtoc) expect ENOTSUP
1707 * since we're emulating an EFI label
1713 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1715 error = zvol_dumpify(zv);
1716 zfs_range_unlock(rl);
1720 if (!(zv->zv_flags & ZVOL_DUMPIFIED))
1722 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1724 error = zvol_dump_fini(zv);
1725 zfs_range_unlock(rl);
1733 mutex_exit(&spa_namespace_lock);
1741 return (zvol_minors != 0);
1747 VERIFY(ddi_soft_state_init(&zfsdev_state, sizeof (zfs_soft_state_t),
1749 ZFS_LOG(1, "ZVOL Initialized.");
1755 ddi_soft_state_fini(&zfsdev_state);
1756 ZFS_LOG(1, "ZVOL Deinitialized.");
1761 zvol_dump_init(zvol_state_t *zv, boolean_t resize)
1765 objset_t *os = zv->zv_objset;
1766 nvlist_t *nv = NULL;
1767 uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset));
1769 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1770 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0,
1772 /* wait for dmu_free_long_range to actually free the blocks */
1773 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
1775 tx = dmu_tx_create(os);
1776 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1777 dmu_tx_hold_bonus(tx, ZVOL_OBJ);
1778 error = dmu_tx_assign(tx, TXG_WAIT);
1785 * If we are resizing the dump device then we only need to
1786 * update the refreservation to match the newly updated
1787 * zvolsize. Otherwise, we save off the original state of the
1788 * zvol so that we can restore them if the zvol is ever undumpified.
1791 error = zap_update(os, ZVOL_ZAP_OBJ,
1792 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1793 &zv->zv_volsize, tx);
1795 uint64_t checksum, compress, refresrv, vbs, dedup;
1797 error = dsl_prop_get_integer(zv->zv_name,
1798 zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL);
1799 error = error ? error : dsl_prop_get_integer(zv->zv_name,
1800 zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL);
1801 error = error ? error : dsl_prop_get_integer(zv->zv_name,
1802 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL);
1803 error = error ? error : dsl_prop_get_integer(zv->zv_name,
1804 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL);
1805 if (version >= SPA_VERSION_DEDUP) {
1806 error = error ? error :
1807 dsl_prop_get_integer(zv->zv_name,
1808 zfs_prop_to_name(ZFS_PROP_DEDUP), &dedup, NULL);
1811 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1812 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1,
1814 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1815 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx);
1816 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1817 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1819 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1820 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1,
1822 error = error ? error : dmu_object_set_blocksize(
1823 os, ZVOL_OBJ, SPA_MAXBLOCKSIZE, 0, tx);
1824 if (version >= SPA_VERSION_DEDUP) {
1825 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1826 zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1,
1830 zv->zv_volblocksize = SPA_MAXBLOCKSIZE;
1835 * We only need update the zvol's property if we are initializing
1836 * the dump area for the first time.
1839 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1840 VERIFY(nvlist_add_uint64(nv,
1841 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0);
1842 VERIFY(nvlist_add_uint64(nv,
1843 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
1844 ZIO_COMPRESS_OFF) == 0);
1845 VERIFY(nvlist_add_uint64(nv,
1846 zfs_prop_to_name(ZFS_PROP_CHECKSUM),
1847 ZIO_CHECKSUM_OFF) == 0);
1848 if (version >= SPA_VERSION_DEDUP) {
1849 VERIFY(nvlist_add_uint64(nv,
1850 zfs_prop_to_name(ZFS_PROP_DEDUP),
1851 ZIO_CHECKSUM_OFF) == 0);
1854 error = zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
1862 /* Allocate the space for the dump */
1863 error = zvol_prealloc(zv);
1868 zvol_dumpify(zvol_state_t *zv)
1871 uint64_t dumpsize = 0;
1873 objset_t *os = zv->zv_objset;
1875 if (zv->zv_flags & ZVOL_RDONLY)
1878 if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE,
1879 8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) {
1880 boolean_t resize = (dumpsize > 0) ? B_TRUE : B_FALSE;
1882 if ((error = zvol_dump_init(zv, resize)) != 0) {
1883 (void) zvol_dump_fini(zv);
1889 * Build up our lba mapping.
1891 error = zvol_get_lbas(zv);
1893 (void) zvol_dump_fini(zv);
1897 tx = dmu_tx_create(os);
1898 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1899 error = dmu_tx_assign(tx, TXG_WAIT);
1902 (void) zvol_dump_fini(zv);
1906 zv->zv_flags |= ZVOL_DUMPIFIED;
1907 error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1,
1908 &zv->zv_volsize, tx);
1912 (void) zvol_dump_fini(zv);
1916 txg_wait_synced(dmu_objset_pool(os), 0);
1921 zvol_dump_fini(zvol_state_t *zv)
1924 objset_t *os = zv->zv_objset;
1927 uint64_t checksum, compress, refresrv, vbs, dedup;
1928 uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset));
1931 * Attempt to restore the zvol back to its pre-dumpified state.
1932 * This is a best-effort attempt as it's possible that not all
1933 * of these properties were initialized during the dumpify process
1934 * (i.e. error during zvol_dump_init).
1937 tx = dmu_tx_create(os);
1938 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1939 error = dmu_tx_assign(tx, TXG_WAIT);
1944 (void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx);
1947 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1948 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum);
1949 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1950 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress);
1951 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1952 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv);
1953 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1954 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs);
1956 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1957 (void) nvlist_add_uint64(nv,
1958 zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum);
1959 (void) nvlist_add_uint64(nv,
1960 zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress);
1961 (void) nvlist_add_uint64(nv,
1962 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv);
1963 if (version >= SPA_VERSION_DEDUP &&
1964 zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1965 zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, &dedup) == 0) {
1966 (void) nvlist_add_uint64(nv,
1967 zfs_prop_to_name(ZFS_PROP_DEDUP), dedup);
1969 (void) zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
1973 zvol_free_extents(zv);
1974 zv->zv_flags &= ~ZVOL_DUMPIFIED;
1975 (void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END);
1976 /* wait for dmu_free_long_range to actually free the blocks */
1977 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
1978 tx = dmu_tx_create(os);
1979 dmu_tx_hold_bonus(tx, ZVOL_OBJ);
1980 error = dmu_tx_assign(tx, TXG_WAIT);
1985 if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0)
1986 zv->zv_volblocksize = vbs;
1993 static zvol_state_t *
1994 zvol_geom_create(const char *name)
1996 struct g_provider *pp;
2000 gp = g_new_geomf(&zfs_zvol_class, "zfs::zvol::%s", name);
2001 gp->start = zvol_geom_start;
2002 gp->access = zvol_geom_access;
2003 pp = g_new_providerf(gp, "%s/%s", ZVOL_DRIVER, name);
2004 pp->sectorsize = DEV_BSIZE;
2006 zv = kmem_zalloc(sizeof(*zv), KM_SLEEP);
2007 zv->zv_provider = pp;
2009 bioq_init(&zv->zv_queue);
2010 mtx_init(&zv->zv_queue_mtx, "zvol", NULL, MTX_DEF);
2018 zvol_geom_run(zvol_state_t *zv)
2020 struct g_provider *pp;
2022 pp = zv->zv_provider;
2023 g_error_provider(pp, 0);
2025 kproc_kthread_add(zvol_geom_worker, zv, &zfsproc, NULL, 0, 0,
2026 "zfskern", "zvol %s", pp->name + sizeof(ZVOL_DRIVER));
2030 zvol_geom_destroy(zvol_state_t *zv)
2032 struct g_provider *pp;
2034 g_topology_assert();
2036 mtx_lock(&zv->zv_queue_mtx);
2038 wakeup_one(&zv->zv_queue);
2039 while (zv->zv_state != 2)
2040 msleep(&zv->zv_state, &zv->zv_queue_mtx, 0, "zvol:w", 0);
2041 mtx_destroy(&zv->zv_queue_mtx);
2043 pp = zv->zv_provider;
2044 zv->zv_provider = NULL;
2046 g_wither_geom(pp->geom, ENXIO);
2048 kmem_free(zv, sizeof(*zv));
2052 zvol_geom_access(struct g_provider *pp, int acr, int acw, int ace)
2054 int count, error, flags;
2056 g_topology_assert();
2059 * To make it easier we expect either open or close, but not both
2062 KASSERT((acr >= 0 && acw >= 0 && ace >= 0) ||
2063 (acr <= 0 && acw <= 0 && ace <= 0),
2064 ("Unsupported access request to %s (acr=%d, acw=%d, ace=%d).",
2065 pp->name, acr, acw, ace));
2067 if (pp->private == NULL) {
2068 if (acr <= 0 && acw <= 0 && ace <= 0)
2074 * We don't pass FEXCL flag to zvol_open()/zvol_close() if ace != 0,
2075 * because GEOM already handles that and handles it a bit differently.
2076 * GEOM allows for multiple read/exclusive consumers and ZFS allows
2077 * only one exclusive consumer, no matter if it is reader or writer.
2078 * I like better the way GEOM works so I'll leave it for GEOM to
2079 * decide what to do.
2082 count = acr + acw + ace;
2087 if (acr != 0 || ace != 0)
2092 g_topology_unlock();
2094 error = zvol_open(pp, flags, count);
2096 error = zvol_close(pp, flags, -count);
2102 zvol_geom_start(struct bio *bp)
2107 switch (bp->bio_cmd) {
2111 zv = bp->bio_to->private;
2113 mtx_lock(&zv->zv_queue_mtx);
2114 first = (bioq_first(&zv->zv_queue) == NULL);
2115 bioq_insert_tail(&zv->zv_queue, bp);
2116 mtx_unlock(&zv->zv_queue_mtx);
2118 wakeup_one(&zv->zv_queue);
2123 g_io_deliver(bp, EOPNOTSUPP);
2129 zvol_geom_worker(void *arg)
2134 thread_lock(curthread);
2135 sched_prio(curthread, PRIBIO);
2136 thread_unlock(curthread);
2140 mtx_lock(&zv->zv_queue_mtx);
2141 bp = bioq_takefirst(&zv->zv_queue);
2143 if (zv->zv_state == 1) {
2145 wakeup(&zv->zv_state);
2146 mtx_unlock(&zv->zv_queue_mtx);
2149 msleep(&zv->zv_queue, &zv->zv_queue_mtx, PRIBIO | PDROP,
2153 mtx_unlock(&zv->zv_queue_mtx);
2154 switch (bp->bio_cmd) {
2156 zil_commit(zv->zv_zilog, ZVOL_OBJ);
2157 g_io_deliver(bp, 0);
2167 extern boolean_t dataset_name_hidden(const char *name);
2170 zvol_create_snapshots(objset_t *os, const char *name)
2172 uint64_t cookie, obj;
2177 sname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2179 (void) dmu_objset_find(name, dmu_objset_prefetch, NULL,
2183 len = snprintf(sname, MAXPATHLEN, "%s@", name);
2184 if (len >= MAXPATHLEN) {
2185 dmu_objset_rele(os, FTAG);
2186 error = ENAMETOOLONG;
2190 error = dmu_snapshot_list_next(os, MAXPATHLEN - len,
2191 sname + len, &obj, &cookie, NULL);
2193 if (error == ENOENT)
2198 if ((error = zvol_create_minor(sname)) != 0) {
2199 printf("ZFS WARNING: Unable to create ZVOL %s (error=%d).\n",
2205 kmem_free(sname, MAXPATHLEN);
2210 zvol_create_minors(const char *name)
2217 if (dataset_name_hidden(name))
2220 if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) {
2221 printf("ZFS WARNING: Unable to put hold on %s (error=%d).\n",
2225 if (dmu_objset_type(os) == DMU_OST_ZVOL) {
2226 if ((error = zvol_create_minor(name)) == 0)
2227 error = zvol_create_snapshots(os, name);
2229 printf("ZFS WARNING: Unable to create ZVOL %s (error=%d).\n",
2232 dmu_objset_rele(os, FTAG);
2235 if (dmu_objset_type(os) != DMU_OST_ZFS) {
2236 dmu_objset_rele(os, FTAG);
2240 osname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2241 if (snprintf(osname, MAXPATHLEN, "%s/", name) >= MAXPATHLEN) {
2242 dmu_objset_rele(os, FTAG);
2243 kmem_free(osname, MAXPATHLEN);
2246 p = osname + strlen(osname);
2247 len = MAXPATHLEN - (p - osname);
2249 /* Prefetch the datasets. */
2251 while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0) {
2252 if (!dataset_name_hidden(osname))
2253 (void) dmu_objset_prefetch(osname, NULL);
2257 while (dmu_dir_list_next(os, MAXPATHLEN - (p - osname), p, NULL,
2259 dmu_objset_rele(os, FTAG);
2260 (void)zvol_create_minors(osname);
2261 if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) {
2262 printf("ZFS WARNING: Unable to put hold on %s (error=%d).\n",
2268 dmu_objset_rele(os, FTAG);
2269 kmem_free(osname, MAXPATHLEN);
2274 zvol_rename_minor(struct g_geom *gp, const char *newname)
2276 struct g_provider *pp;
2279 ASSERT(MUTEX_HELD(&spa_namespace_lock));
2280 g_topology_assert();
2282 pp = LIST_FIRST(&gp->provider);
2287 zv->zv_provider = NULL;
2288 g_wither_provider(pp, ENXIO);
2290 pp = g_new_providerf(gp, "%s/%s", ZVOL_DRIVER, newname);
2291 pp->sectorsize = DEV_BSIZE;
2292 pp->mediasize = zv->zv_volsize;
2294 zv->zv_provider = pp;
2295 strlcpy(zv->zv_name, newname, sizeof(zv->zv_name));
2296 g_error_provider(pp, 0);
2300 zvol_rename_minors(const char *oldname, const char *newname)
2302 char name[MAXPATHLEN];
2303 struct g_provider *pp;
2305 size_t oldnamelen, newnamelen;
2309 oldnamelen = strlen(oldname);
2310 newnamelen = strlen(newname);
2313 mutex_enter(&spa_namespace_lock);
2316 LIST_FOREACH(gp, &zfs_zvol_class.geom, geom) {
2317 pp = LIST_FIRST(&gp->provider);
2323 if (strcmp(zv->zv_name, oldname) == 0) {
2324 zvol_rename_minor(gp, newname);
2325 } else if (strncmp(zv->zv_name, oldname, oldnamelen) == 0 &&
2326 (zv->zv_name[oldnamelen] == '/' ||
2327 zv->zv_name[oldnamelen] == '@')) {
2328 snprintf(name, sizeof(name), "%s%c%s", newname,
2329 zv->zv_name[oldnamelen],
2330 zv->zv_name + oldnamelen + 1);
2331 zvol_rename_minor(gp, name);
2335 g_topology_unlock();
2336 mutex_exit(&spa_namespace_lock);