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.
27 * Portions Copyright 2010 Robert Milkowski
29 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
30 * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
31 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
34 /* Portions Copyright 2011 Martin Matuska <mm@FreeBSD.org> */
37 * ZFS volume emulation driver.
39 * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes.
40 * Volumes are accessed through the symbolic links named:
42 * /dev/zvol/dsk/<pool_name>/<dataset_name>
43 * /dev/zvol/rdsk/<pool_name>/<dataset_name>
45 * These links are created by the /dev filesystem (sdev_zvolops.c).
46 * Volumes are persistent through reboot. No user command needs to be
47 * run before opening and using a device.
50 * On FreeBSD ZVOLs are simply GEOM providers like any other storage device
54 #include <sys/types.h>
55 #include <sys/param.h>
56 #include <sys/kernel.h>
57 #include <sys/errno.h>
63 #include <sys/cmn_err.h>
67 #include <sys/spa_impl.h>
70 #include <sys/dmu_traverse.h>
71 #include <sys/dnode.h>
72 #include <sys/dsl_dataset.h>
73 #include <sys/dsl_prop.h>
75 #include <sys/byteorder.h>
76 #include <sys/sunddi.h>
77 #include <sys/dirent.h>
78 #include <sys/policy.h>
79 #include <sys/queue.h>
80 #include <sys/fs/zfs.h>
81 #include <sys/zfs_ioctl.h>
83 #include <sys/refcount.h>
84 #include <sys/zfs_znode.h>
85 #include <sys/zfs_rlock.h>
86 #include <sys/vdev_impl.h>
87 #include <sys/vdev_raidz.h>
89 #include <sys/zil_impl.h>
91 #include <sys/dmu_tx.h>
92 #include <sys/zfeature.h>
93 #include <sys/zio_checksum.h>
95 #include <geom/geom.h>
97 #include "zfs_namecheck.h"
99 struct g_class zfs_zvol_class = {
101 .version = G_VERSION,
104 DECLARE_GEOM_CLASS(zfs_zvol_class, zfs_zvol);
107 static char *zvol_tag = "zvol_tag";
109 #define ZVOL_DUMPSIZE "dumpsize"
112 * The spa_namespace_lock protects the zfsdev_state structure from being
113 * modified while it's being used, e.g. an open that comes in before a
114 * create finishes. It also protects temporary opens of the dataset so that,
115 * e.g., an open doesn't get a spurious EBUSY.
117 static uint32_t zvol_minors;
119 SYSCTL_DECL(_vfs_zfs);
120 SYSCTL_NODE(_vfs_zfs, OID_AUTO, vol, CTLFLAG_RW, 0, "ZFS VOLUME");
121 static int volmode = ZFS_VOLMODE_GEOM;
122 TUNABLE_INT("vfs.zfs.vol.mode", &volmode);
123 SYSCTL_INT(_vfs_zfs_vol, OID_AUTO, mode, CTLFLAG_RWTUN, &volmode, 0,
124 "Expose as GEOM providers (1), device files (2) or neither");
126 typedef struct zvol_extent {
128 dva_t ze_dva; /* dva associated with this extent */
129 uint64_t ze_nblks; /* number of blocks in extent */
133 * The in-core state of each volume.
135 typedef struct zvol_state {
136 LIST_ENTRY(zvol_state) zv_links;
137 char zv_name[MAXPATHLEN]; /* pool/dd name */
138 uint64_t zv_volsize; /* amount of space we advertise */
139 uint64_t zv_volblocksize; /* volume block size */
140 struct cdev *zv_dev; /* non-GEOM device */
141 struct g_provider *zv_provider; /* GEOM provider */
142 uint8_t zv_min_bs; /* minimum addressable block shift */
143 uint8_t zv_flags; /* readonly, dumpified, etc. */
144 objset_t *zv_objset; /* objset handle */
145 uint32_t zv_total_opens; /* total open count */
146 zilog_t *zv_zilog; /* ZIL handle */
147 list_t zv_extents; /* List of extents for dump */
148 znode_t zv_znode; /* for range locking */
149 dmu_buf_t *zv_dbuf; /* bonus handle */
151 int zv_volmode; /* Provide GEOM or cdev */
152 struct bio_queue_head zv_queue;
153 struct mtx zv_queue_mtx; /* zv_queue mutex */
156 static LIST_HEAD(, zvol_state) all_zvols;
159 * zvol specific flags
161 #define ZVOL_RDONLY 0x1
162 #define ZVOL_DUMPIFIED 0x2
163 #define ZVOL_EXCL 0x4
167 * zvol maximum transfer in one DMU tx.
169 int zvol_maxphys = DMU_MAX_ACCESS/2;
171 static d_open_t zvol_d_open;
172 static d_close_t zvol_d_close;
173 static d_read_t zvol_read;
174 static d_write_t zvol_write;
175 static d_ioctl_t zvol_d_ioctl;
176 static d_strategy_t zvol_strategy;
178 static struct cdevsw zvol_cdevsw = {
179 .d_version = D_VERSION,
180 .d_open = zvol_d_open,
181 .d_close = zvol_d_close,
183 .d_write = zvol_write,
184 .d_ioctl = zvol_d_ioctl,
185 .d_strategy = zvol_strategy,
187 .d_flags = D_DISK | D_TRACKCLOSE,
190 extern int zfs_set_prop_nvlist(const char *, zprop_source_t,
191 nvlist_t *, nvlist_t *);
192 static void zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off,
193 uint64_t len, boolean_t sync);
194 static int zvol_remove_zv(zvol_state_t *);
195 static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio);
196 static int zvol_dumpify(zvol_state_t *zv);
197 static int zvol_dump_fini(zvol_state_t *zv);
198 static int zvol_dump_init(zvol_state_t *zv, boolean_t resize);
200 static void zvol_geom_run(zvol_state_t *zv);
201 static void zvol_geom_destroy(zvol_state_t *zv);
202 static int zvol_geom_access(struct g_provider *pp, int acr, int acw, int ace);
203 static void zvol_geom_start(struct bio *bp);
204 static void zvol_geom_worker(void *arg);
207 zvol_size_changed(zvol_state_t *zv)
210 dev_t dev = makedevice(maj, min);
212 VERIFY(ddi_prop_update_int64(dev, zfs_dip,
213 "Size", volsize) == DDI_SUCCESS);
214 VERIFY(ddi_prop_update_int64(dev, zfs_dip,
215 "Nblocks", lbtodb(volsize)) == DDI_SUCCESS);
217 /* Notify specfs to invalidate the cached size */
218 spec_size_invalidate(dev, VBLK);
219 spec_size_invalidate(dev, VCHR);
221 if (zv->zv_volmode == ZFS_VOLMODE_GEOM) {
222 struct g_provider *pp;
224 pp = zv->zv_provider;
228 g_resize_provider(pp, zv->zv_volsize);
235 zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
238 return (SET_ERROR(EINVAL));
240 if (volsize % blocksize != 0)
241 return (SET_ERROR(EINVAL));
244 if (volsize - 1 > SPEC_MAXOFFSET_T)
245 return (SET_ERROR(EOVERFLOW));
251 zvol_check_volblocksize(uint64_t volblocksize)
253 if (volblocksize < SPA_MINBLOCKSIZE ||
254 volblocksize > SPA_MAXBLOCKSIZE ||
256 return (SET_ERROR(EDOM));
262 zvol_get_stats(objset_t *os, nvlist_t *nv)
265 dmu_object_info_t doi;
268 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
272 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
274 error = dmu_object_info(os, ZVOL_OBJ, &doi);
277 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
278 doi.doi_data_block_size);
284 static zvol_state_t *
285 zvol_minor_lookup(const char *name)
289 ASSERT(MUTEX_HELD(&spa_namespace_lock));
291 LIST_FOREACH(zv, &all_zvols, zv_links) {
292 if (strcmp(zv->zv_name, name) == 0)
299 /* extent mapping arg */
307 zvol_map_block(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
308 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
310 struct maparg *ma = arg;
312 int bs = ma->ma_zv->zv_volblocksize;
314 if (BP_IS_HOLE(bp) ||
315 zb->zb_object != ZVOL_OBJ || zb->zb_level != 0)
318 VERIFY(!BP_IS_EMBEDDED(bp));
320 VERIFY3U(ma->ma_blks, ==, zb->zb_blkid);
323 /* Abort immediately if we have encountered gang blocks */
325 return (SET_ERROR(EFRAGS));
328 * See if the block is at the end of the previous extent.
330 ze = list_tail(&ma->ma_zv->zv_extents);
332 DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) &&
333 DVA_GET_OFFSET(BP_IDENTITY(bp)) ==
334 DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) {
339 dprintf_bp(bp, "%s", "next blkptr:");
341 /* start a new extent */
342 ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP);
343 ze->ze_dva = bp->blk_dva[0]; /* structure assignment */
345 list_insert_tail(&ma->ma_zv->zv_extents, ze);
350 zvol_free_extents(zvol_state_t *zv)
354 while (ze = list_head(&zv->zv_extents)) {
355 list_remove(&zv->zv_extents, ze);
356 kmem_free(ze, sizeof (zvol_extent_t));
361 zvol_get_lbas(zvol_state_t *zv)
363 objset_t *os = zv->zv_objset;
369 zvol_free_extents(zv);
371 /* commit any in-flight changes before traversing the dataset */
372 txg_wait_synced(dmu_objset_pool(os), 0);
373 err = traverse_dataset(dmu_objset_ds(os), 0,
374 TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma);
375 if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) {
376 zvol_free_extents(zv);
377 return (err ? err : EIO);
385 zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
387 zfs_creat_t *zct = arg;
388 nvlist_t *nvprops = zct->zct_props;
390 uint64_t volblocksize, volsize;
392 VERIFY(nvlist_lookup_uint64(nvprops,
393 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
394 if (nvlist_lookup_uint64(nvprops,
395 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
396 volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
399 * These properties must be removed from the list so the generic
400 * property setting step won't apply to them.
402 VERIFY(nvlist_remove_all(nvprops,
403 zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
404 (void) nvlist_remove_all(nvprops,
405 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
407 error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
411 error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
415 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
420 * Replay a TX_TRUNCATE ZIL transaction if asked. TX_TRUNCATE is how we
421 * implement DKIOCFREE/free-long-range.
424 zvol_replay_truncate(zvol_state_t *zv, lr_truncate_t *lr, boolean_t byteswap)
426 uint64_t offset, length;
429 byteswap_uint64_array(lr, sizeof (*lr));
431 offset = lr->lr_offset;
432 length = lr->lr_length;
434 return (dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, offset, length));
438 * Replay a TX_WRITE ZIL transaction that didn't get committed
439 * after a system failure
442 zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
444 objset_t *os = zv->zv_objset;
445 char *data = (char *)(lr + 1); /* data follows lr_write_t */
446 uint64_t offset, length;
451 byteswap_uint64_array(lr, sizeof (*lr));
453 offset = lr->lr_offset;
454 length = lr->lr_length;
456 /* If it's a dmu_sync() block, write the whole block */
457 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
458 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
459 if (length < blocksize) {
460 offset -= offset % blocksize;
465 tx = dmu_tx_create(os);
466 dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length);
467 error = dmu_tx_assign(tx, TXG_WAIT);
471 dmu_write(os, ZVOL_OBJ, offset, length, data, tx);
480 zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
482 return (SET_ERROR(ENOTSUP));
486 * Callback vectors for replaying records.
487 * Only TX_WRITE and TX_TRUNCATE are needed for zvol.
489 zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = {
490 zvol_replay_err, /* 0 no such transaction type */
491 zvol_replay_err, /* TX_CREATE */
492 zvol_replay_err, /* TX_MKDIR */
493 zvol_replay_err, /* TX_MKXATTR */
494 zvol_replay_err, /* TX_SYMLINK */
495 zvol_replay_err, /* TX_REMOVE */
496 zvol_replay_err, /* TX_RMDIR */
497 zvol_replay_err, /* TX_LINK */
498 zvol_replay_err, /* TX_RENAME */
499 zvol_replay_write, /* TX_WRITE */
500 zvol_replay_truncate, /* TX_TRUNCATE */
501 zvol_replay_err, /* TX_SETATTR */
502 zvol_replay_err, /* TX_ACL */
503 zvol_replay_err, /* TX_CREATE_ACL */
504 zvol_replay_err, /* TX_CREATE_ATTR */
505 zvol_replay_err, /* TX_CREATE_ACL_ATTR */
506 zvol_replay_err, /* TX_MKDIR_ACL */
507 zvol_replay_err, /* TX_MKDIR_ATTR */
508 zvol_replay_err, /* TX_MKDIR_ACL_ATTR */
509 zvol_replay_err, /* TX_WRITE2 */
514 zvol_name2minor(const char *name, minor_t *minor)
518 mutex_enter(&spa_namespace_lock);
519 zv = zvol_minor_lookup(name);
521 *minor = zv->zv_minor;
522 mutex_exit(&spa_namespace_lock);
523 return (zv ? 0 : -1);
528 * Create a minor node (plus a whole lot more) for the specified volume.
531 zvol_create_minor(const char *name)
533 zfs_soft_state_t *zs;
537 struct g_provider *pp;
539 dmu_object_info_t doi;
540 uint64_t volsize, mode;
543 ZFS_LOG(1, "Creating ZVOL %s...", name);
545 mutex_enter(&spa_namespace_lock);
547 if (zvol_minor_lookup(name) != NULL) {
548 mutex_exit(&spa_namespace_lock);
549 return (SET_ERROR(EEXIST));
552 /* lie and say we're read-only */
553 error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, FTAG, &os);
556 mutex_exit(&spa_namespace_lock);
561 if ((minor = zfsdev_minor_alloc()) == 0) {
562 dmu_objset_disown(os, FTAG);
563 mutex_exit(&spa_namespace_lock);
564 return (SET_ERROR(ENXIO));
567 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS) {
568 dmu_objset_disown(os, FTAG);
569 mutex_exit(&spa_namespace_lock);
570 return (SET_ERROR(EAGAIN));
572 (void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME,
575 (void) snprintf(chrbuf, sizeof (chrbuf), "%u,raw", minor);
577 if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR,
578 minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
579 ddi_soft_state_free(zfsdev_state, minor);
580 dmu_objset_disown(os, FTAG);
581 mutex_exit(&spa_namespace_lock);
582 return (SET_ERROR(EAGAIN));
585 (void) snprintf(blkbuf, sizeof (blkbuf), "%u", minor);
587 if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK,
588 minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
589 ddi_remove_minor_node(zfs_dip, chrbuf);
590 ddi_soft_state_free(zfsdev_state, minor);
591 dmu_objset_disown(os, FTAG);
592 mutex_exit(&spa_namespace_lock);
593 return (SET_ERROR(EAGAIN));
596 zs = ddi_get_soft_state(zfsdev_state, minor);
597 zs->zss_type = ZSST_ZVOL;
598 zv = zs->zss_data = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP);
601 zv = kmem_zalloc(sizeof(*zv), KM_SLEEP);
603 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
605 kmem_free(zv, sizeof(*zv));
606 dmu_objset_disown(os, zvol_tag);
607 mutex_exit(&spa_namespace_lock);
610 error = dsl_prop_get_integer(name,
611 zfs_prop_to_name(ZFS_PROP_VOLMODE), &mode, NULL);
612 if (error != 0 || mode == ZFS_VOLMODE_DEFAULT)
616 zv->zv_volsize = volsize;
617 zv->zv_volmode = mode;
618 if (zv->zv_volmode == ZFS_VOLMODE_GEOM) {
620 gp = g_new_geomf(&zfs_zvol_class, "zfs::zvol::%s", name);
621 gp->start = zvol_geom_start;
622 gp->access = zvol_geom_access;
623 pp = g_new_providerf(gp, "%s/%s", ZVOL_DRIVER, name);
624 pp->flags |= G_PF_DIRECT_RECEIVE | G_PF_DIRECT_SEND;
625 pp->sectorsize = DEV_BSIZE;
626 pp->mediasize = zv->zv_volsize;
629 zv->zv_provider = pp;
630 bioq_init(&zv->zv_queue);
631 mtx_init(&zv->zv_queue_mtx, "zvol", NULL, MTX_DEF);
632 } else if (zv->zv_volmode == ZFS_VOLMODE_DEV) {
633 if (make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK,
634 &dev, &zvol_cdevsw, NULL, UID_ROOT, GID_OPERATOR,
635 0640, "%s/%s", ZVOL_DRIVER, name) != 0) {
636 kmem_free(zv, sizeof(*zv));
637 dmu_objset_disown(os, FTAG);
638 mutex_exit(&spa_namespace_lock);
639 return (SET_ERROR(ENXIO));
642 dev->si_iosize_max = MAXPHYS;
645 LIST_INSERT_HEAD(&all_zvols, zv, zv_links);
648 (void) strlcpy(zv->zv_name, name, MAXPATHLEN);
649 zv->zv_min_bs = DEV_BSHIFT;
651 if (dmu_objset_is_snapshot(os) || !spa_writeable(dmu_objset_spa(os)))
652 zv->zv_flags |= ZVOL_RDONLY;
653 mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
654 avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
655 sizeof (rl_t), offsetof(rl_t, r_node));
656 list_create(&zv->zv_extents, sizeof (zvol_extent_t),
657 offsetof(zvol_extent_t, ze_node));
658 /* get and cache the blocksize */
659 error = dmu_object_info(os, ZVOL_OBJ, &doi);
661 zv->zv_volblocksize = doi.doi_data_block_size;
663 if (spa_writeable(dmu_objset_spa(os))) {
664 if (zil_replay_disable)
665 zil_destroy(dmu_objset_zil(os), B_FALSE);
667 zil_replay(os, zv, zvol_replay_vector);
669 dmu_objset_disown(os, FTAG);
670 zv->zv_objset = NULL;
674 mutex_exit(&spa_namespace_lock);
677 if (zv->zv_volmode == ZFS_VOLMODE_GEOM) {
684 ZFS_LOG(1, "ZVOL %s created.", name);
690 * Remove minor node for the specified volume.
693 zvol_remove_zv(zvol_state_t *zv)
696 minor_t minor = zv->zv_minor;
699 ASSERT(MUTEX_HELD(&spa_namespace_lock));
700 if (zv->zv_total_opens != 0)
701 return (SET_ERROR(EBUSY));
703 ZFS_LOG(1, "ZVOL %s destroyed.", zv->zv_name);
706 (void) snprintf(nmbuf, sizeof (nmbuf), "%u,raw", minor);
707 ddi_remove_minor_node(zfs_dip, nmbuf);
709 LIST_REMOVE(zv, zv_links);
710 if (zv->zv_volmode == ZFS_VOLMODE_GEOM) {
712 zvol_geom_destroy(zv);
714 } else if (zv->zv_volmode == ZFS_VOLMODE_DEV)
715 destroy_dev(zv->zv_dev);
718 avl_destroy(&zv->zv_znode.z_range_avl);
719 mutex_destroy(&zv->zv_znode.z_range_lock);
721 kmem_free(zv, sizeof(*zv));
728 zvol_remove_minor(const char *name)
733 mutex_enter(&spa_namespace_lock);
734 if ((zv = zvol_minor_lookup(name)) == NULL) {
735 mutex_exit(&spa_namespace_lock);
736 return (SET_ERROR(ENXIO));
738 rc = zvol_remove_zv(zv);
739 mutex_exit(&spa_namespace_lock);
744 zvol_first_open(zvol_state_t *zv)
751 /* lie and say we're read-only */
752 error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, B_TRUE,
757 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
760 dmu_objset_disown(os, zvol_tag);
764 error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf);
766 dmu_objset_disown(os, zvol_tag);
769 zv->zv_volsize = volsize;
770 zv->zv_zilog = zil_open(os, zvol_get_data);
771 zvol_size_changed(zv);
773 VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &readonly,
775 if (readonly || dmu_objset_is_snapshot(os) ||
776 !spa_writeable(dmu_objset_spa(os)))
777 zv->zv_flags |= ZVOL_RDONLY;
779 zv->zv_flags &= ~ZVOL_RDONLY;
784 zvol_last_close(zvol_state_t *zv)
786 zil_close(zv->zv_zilog);
789 dmu_buf_rele(zv->zv_dbuf, zvol_tag);
795 if (dsl_dataset_is_dirty(dmu_objset_ds(zv->zv_objset)) &&
796 !(zv->zv_flags & ZVOL_RDONLY))
797 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
798 dmu_objset_evict_dbufs(zv->zv_objset);
800 dmu_objset_disown(zv->zv_objset, zvol_tag);
801 zv->zv_objset = NULL;
806 zvol_prealloc(zvol_state_t *zv)
808 objset_t *os = zv->zv_objset;
810 uint64_t refd, avail, usedobjs, availobjs;
811 uint64_t resid = zv->zv_volsize;
814 /* Check the space usage before attempting to allocate the space */
815 dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs);
816 if (avail < zv->zv_volsize)
817 return (SET_ERROR(ENOSPC));
819 /* Free old extents if they exist */
820 zvol_free_extents(zv);
824 uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE);
826 tx = dmu_tx_create(os);
827 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
828 error = dmu_tx_assign(tx, TXG_WAIT);
831 (void) dmu_free_long_range(os, ZVOL_OBJ, 0, off);
834 dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx);
839 txg_wait_synced(dmu_objset_pool(os), 0);
846 zvol_update_volsize(objset_t *os, uint64_t volsize)
851 ASSERT(MUTEX_HELD(&spa_namespace_lock));
853 tx = dmu_tx_create(os);
854 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
855 dmu_tx_mark_netfree(tx);
856 error = dmu_tx_assign(tx, TXG_WAIT);
862 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
867 error = dmu_free_long_range(os,
868 ZVOL_OBJ, volsize, DMU_OBJECT_END);
873 zvol_remove_minors(const char *name)
875 zvol_state_t *zv, *tzv;
878 namelen = strlen(name);
881 mutex_enter(&spa_namespace_lock);
883 LIST_FOREACH_SAFE(zv, &all_zvols, zv_links, tzv) {
884 if (strcmp(zv->zv_name, name) == 0 ||
885 (strncmp(zv->zv_name, name, namelen) == 0 &&
886 strlen(zv->zv_name) > namelen && (zv->zv_name[namelen] == '/' ||
887 zv->zv_name[namelen] == '@'))) {
888 (void) zvol_remove_zv(zv);
892 mutex_exit(&spa_namespace_lock);
897 zvol_set_volsize(const char *name, major_t maj, uint64_t volsize)
899 zvol_state_t *zv = NULL;
902 dmu_object_info_t doi;
903 uint64_t old_volsize = 0ULL;
906 mutex_enter(&spa_namespace_lock);
907 zv = zvol_minor_lookup(name);
908 if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) {
909 mutex_exit(&spa_namespace_lock);
913 if ((error = dmu_object_info(os, ZVOL_OBJ, &doi)) != 0 ||
914 (error = zvol_check_volsize(volsize,
915 doi.doi_data_block_size)) != 0)
918 VERIFY(dsl_prop_get_integer(name, "readonly", &readonly,
925 error = zvol_update_volsize(os, volsize);
927 * Reinitialize the dump area to the new size. If we
928 * failed to resize the dump area then restore it back to
931 if (zv && error == 0) {
933 if (zv->zv_flags & ZVOL_DUMPIFIED) {
934 old_volsize = zv->zv_volsize;
935 zv->zv_volsize = volsize;
936 if ((error = zvol_dumpify(zv)) != 0 ||
937 (error = dumpvp_resize()) != 0) {
938 (void) zvol_update_volsize(os, old_volsize);
939 zv->zv_volsize = old_volsize;
940 error = zvol_dumpify(zv);
943 #endif /* ZVOL_DUMP */
945 zv->zv_volsize = volsize;
946 zvol_size_changed(zv);
952 * Generate a LUN expansion event.
954 if (zv && error == 0) {
957 char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
959 (void) snprintf(physpath, MAXPATHLEN, "%s%u", ZVOL_PSEUDO_DEV,
962 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
963 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
965 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
966 ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
969 kmem_free(physpath, MAXPATHLEN);
974 dmu_objset_rele(os, FTAG);
976 mutex_exit(&spa_namespace_lock);
983 zvol_open(struct g_provider *pp, int flag, int count)
987 boolean_t locked = B_FALSE;
990 * Protect against recursively entering spa_namespace_lock
991 * when spa_open() is used for a pool on a (local) ZVOL(s).
992 * This is needed since we replaced upstream zfsdev_state_lock
993 * with spa_namespace_lock in the ZVOL code.
994 * We are using the same trick as spa_open().
995 * Note that calls in zvol_first_open which need to resolve
996 * pool name to a spa object will enter spa_open()
997 * recursively, but that function already has all the
998 * necessary protection.
1000 if (!MUTEX_HELD(&spa_namespace_lock)) {
1001 mutex_enter(&spa_namespace_lock);
1008 mutex_exit(&spa_namespace_lock);
1009 return (SET_ERROR(ENXIO));
1012 if (zv->zv_total_opens == 0) {
1013 err = zvol_first_open(zv);
1016 mutex_exit(&spa_namespace_lock);
1019 pp->mediasize = zv->zv_volsize;
1020 pp->stripeoffset = 0;
1021 pp->stripesize = zv->zv_volblocksize;
1023 if ((flag & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
1024 err = SET_ERROR(EROFS);
1027 if (zv->zv_flags & ZVOL_EXCL) {
1028 err = SET_ERROR(EBUSY);
1033 if (zv->zv_total_opens != 0) {
1034 err = SET_ERROR(EBUSY);
1037 zv->zv_flags |= ZVOL_EXCL;
1041 zv->zv_total_opens += count;
1043 mutex_exit(&spa_namespace_lock);
1047 if (zv->zv_total_opens == 0)
1048 zvol_last_close(zv);
1050 mutex_exit(&spa_namespace_lock);
1056 zvol_close(struct g_provider *pp, int flag, int count)
1060 boolean_t locked = B_FALSE;
1062 /* See comment in zvol_open(). */
1063 if (!MUTEX_HELD(&spa_namespace_lock)) {
1064 mutex_enter(&spa_namespace_lock);
1071 mutex_exit(&spa_namespace_lock);
1072 return (SET_ERROR(ENXIO));
1075 if (zv->zv_flags & ZVOL_EXCL) {
1076 ASSERT(zv->zv_total_opens == 1);
1077 zv->zv_flags &= ~ZVOL_EXCL;
1081 * If the open count is zero, this is a spurious close.
1082 * That indicates a bug in the kernel / DDI framework.
1084 ASSERT(zv->zv_total_opens != 0);
1087 * You may get multiple opens, but only one close.
1089 zv->zv_total_opens -= count;
1091 if (zv->zv_total_opens == 0)
1092 zvol_last_close(zv);
1095 mutex_exit(&spa_namespace_lock);
1100 zvol_get_done(zgd_t *zgd, int error)
1103 dmu_buf_rele(zgd->zgd_db, zgd);
1105 zfs_range_unlock(zgd->zgd_rl);
1107 if (error == 0 && zgd->zgd_bp)
1108 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1110 kmem_free(zgd, sizeof (zgd_t));
1114 * Get data to generate a TX_WRITE intent log record.
1117 zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1119 zvol_state_t *zv = arg;
1120 objset_t *os = zv->zv_objset;
1121 uint64_t object = ZVOL_OBJ;
1122 uint64_t offset = lr->lr_offset;
1123 uint64_t size = lr->lr_length; /* length of user data */
1124 blkptr_t *bp = &lr->lr_blkptr;
1129 ASSERT(zio != NULL);
1132 zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1133 zgd->zgd_zilog = zv->zv_zilog;
1134 zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
1137 * Write records come in two flavors: immediate and indirect.
1138 * For small writes it's cheaper to store the data with the
1139 * log record (immediate); for large writes it's cheaper to
1140 * sync the data and get a pointer to it (indirect) so that
1141 * we don't have to write the data twice.
1143 if (buf != NULL) { /* immediate write */
1144 error = dmu_read(os, object, offset, size, buf,
1145 DMU_READ_NO_PREFETCH);
1147 size = zv->zv_volblocksize;
1148 offset = P2ALIGN(offset, size);
1149 error = dmu_buf_hold(os, object, offset, zgd, &db,
1150 DMU_READ_NO_PREFETCH);
1152 blkptr_t *obp = dmu_buf_get_blkptr(db);
1154 ASSERT(BP_IS_HOLE(bp));
1161 ASSERT(db->db_offset == offset);
1162 ASSERT(db->db_size == size);
1164 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1165 zvol_get_done, zgd);
1172 zvol_get_done(zgd, error);
1178 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
1180 * We store data in the log buffers if it's small enough.
1181 * Otherwise we will later flush the data out via dmu_sync().
1183 ssize_t zvol_immediate_write_sz = 32768;
1186 zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid,
1189 uint32_t blocksize = zv->zv_volblocksize;
1190 zilog_t *zilog = zv->zv_zilog;
1192 ssize_t immediate_write_sz;
1194 if (zil_replaying(zilog, tx))
1197 immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
1198 ? 0 : zvol_immediate_write_sz;
1200 slogging = spa_has_slogs(zilog->zl_spa) &&
1201 (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
1207 itx_wr_state_t write_state;
1210 * Unlike zfs_log_write() we can be called with
1211 * upto DMU_MAX_ACCESS/2 (5MB) writes.
1213 if (blocksize > immediate_write_sz && !slogging &&
1214 resid >= blocksize && off % blocksize == 0) {
1215 write_state = WR_INDIRECT; /* uses dmu_sync */
1218 write_state = WR_COPIED;
1219 len = MIN(ZIL_MAX_LOG_DATA, resid);
1221 write_state = WR_NEED_COPY;
1222 len = MIN(ZIL_MAX_LOG_DATA, resid);
1225 itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
1226 (write_state == WR_COPIED ? len : 0));
1227 lr = (lr_write_t *)&itx->itx_lr;
1228 if (write_state == WR_COPIED && dmu_read(zv->zv_objset,
1229 ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
1230 zil_itx_destroy(itx);
1231 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1232 lr = (lr_write_t *)&itx->itx_lr;
1233 write_state = WR_NEED_COPY;
1236 itx->itx_wr_state = write_state;
1237 if (write_state == WR_NEED_COPY)
1238 itx->itx_sod += len;
1239 lr->lr_foid = ZVOL_OBJ;
1240 lr->lr_offset = off;
1241 lr->lr_length = len;
1243 BP_ZERO(&lr->lr_blkptr);
1245 itx->itx_private = zv;
1246 itx->itx_sync = sync;
1248 zil_itx_assign(zilog, itx, tx);
1257 zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t origoffset,
1258 uint64_t size, boolean_t doread, boolean_t isdump)
1264 if (vd->vdev_ops == &vdev_mirror_ops ||
1265 vd->vdev_ops == &vdev_replacing_ops ||
1266 vd->vdev_ops == &vdev_spare_ops) {
1267 for (c = 0; c < vd->vdev_children; c++) {
1268 int err = zvol_dumpio_vdev(vd->vdev_child[c],
1269 addr, offset, origoffset, size, doread, isdump);
1272 } else if (doread) {
1278 if (!vd->vdev_ops->vdev_op_leaf && vd->vdev_ops != &vdev_raidz_ops)
1279 return (numerrors < vd->vdev_children ? 0 : EIO);
1281 if (doread && !vdev_readable(vd))
1282 return (SET_ERROR(EIO));
1283 else if (!doread && !vdev_writeable(vd))
1284 return (SET_ERROR(EIO));
1286 if (vd->vdev_ops == &vdev_raidz_ops) {
1287 return (vdev_raidz_physio(vd,
1288 addr, size, offset, origoffset, doread, isdump));
1291 offset += VDEV_LABEL_START_SIZE;
1293 if (ddi_in_panic() || isdump) {
1296 return (SET_ERROR(EIO));
1298 ASSERT3P(dvd, !=, NULL);
1299 return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset),
1303 ASSERT3P(dvd, !=, NULL);
1304 return (vdev_disk_ldi_physio(dvd->vd_lh, addr, size,
1305 offset, doread ? B_READ : B_WRITE));
1310 zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size,
1311 boolean_t doread, boolean_t isdump)
1316 spa_t *spa = dmu_objset_spa(zv->zv_objset);
1318 /* Must be sector aligned, and not stradle a block boundary. */
1319 if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) ||
1320 P2BOUNDARY(offset, size, zv->zv_volblocksize)) {
1321 return (SET_ERROR(EINVAL));
1323 ASSERT(size <= zv->zv_volblocksize);
1325 /* Locate the extent this belongs to */
1326 ze = list_head(&zv->zv_extents);
1327 while (offset >= ze->ze_nblks * zv->zv_volblocksize) {
1328 offset -= ze->ze_nblks * zv->zv_volblocksize;
1329 ze = list_next(&zv->zv_extents, ze);
1333 return (SET_ERROR(EINVAL));
1335 if (!ddi_in_panic())
1336 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
1338 vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva));
1339 offset += DVA_GET_OFFSET(&ze->ze_dva);
1340 error = zvol_dumpio_vdev(vd, addr, offset, DVA_GET_OFFSET(&ze->ze_dva),
1341 size, doread, isdump);
1343 if (!ddi_in_panic())
1344 spa_config_exit(spa, SCL_STATE, FTAG);
1351 zvol_strategy(struct bio *bp)
1354 uint64_t off, volsize;
1360 boolean_t doread = 0;
1361 boolean_t is_dumpified;
1365 zv = bp->bio_to->private;
1367 zv = bp->bio_dev->si_drv2;
1374 if (bp->bio_cmd != BIO_READ && (zv->zv_flags & ZVOL_RDONLY)) {
1379 switch (bp->bio_cmd) {
1392 off = bp->bio_offset;
1393 volsize = zv->zv_volsize;
1398 addr = bp->bio_data;
1399 resid = bp->bio_length;
1401 if (resid > 0 && (off < 0 || off >= volsize)) {
1407 is_dumpified = zv->zv_flags & ZVOL_DUMPIFIED;
1409 is_dumpified = B_FALSE;
1411 sync = !doread && !is_dumpified &&
1412 zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS;
1415 * There must be no buffer changes when doing a dmu_sync() because
1416 * we can't change the data whilst calculating the checksum.
1418 rl = zfs_range_lock(&zv->zv_znode, off, resid,
1419 doread ? RL_READER : RL_WRITER);
1421 if (bp->bio_cmd == BIO_DELETE) {
1422 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
1423 error = dmu_tx_assign(tx, TXG_WAIT);
1427 zvol_log_truncate(zv, tx, off, resid, B_TRUE);
1429 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ,
1436 while (resid != 0 && off < volsize) {
1437 size_t size = MIN(resid, zvol_maxphys);
1440 size = MIN(size, P2END(off, zv->zv_volblocksize) - off);
1441 error = zvol_dumpio(zv, addr, off, size,
1443 } else if (doread) {
1447 error = dmu_read(os, ZVOL_OBJ, off, size, addr,
1450 dmu_tx_t *tx = dmu_tx_create(os);
1451 dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
1452 error = dmu_tx_assign(tx, TXG_WAIT);
1456 dmu_write(os, ZVOL_OBJ, off, size, addr, tx);
1457 zvol_log_write(zv, tx, off, size, sync);
1462 /* convert checksum errors into IO errors */
1463 if (error == ECKSUM)
1464 error = SET_ERROR(EIO);
1472 zfs_range_unlock(rl);
1474 bp->bio_completed = bp->bio_length - resid;
1475 if (bp->bio_completed < bp->bio_length && off > volsize)
1480 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1484 g_io_deliver(bp, error);
1486 biofinish(bp, NULL, error);
1491 * Set the buffer count to the zvol maximum transfer.
1492 * Using our own routine instead of the default minphys()
1493 * means that for larger writes we write bigger buffers on X86
1494 * (128K instead of 56K) and flush the disk write cache less often
1495 * (every zvol_maxphys - currently 1MB) instead of minphys (currently
1496 * 56K on X86 and 128K on sparc).
1499 zvol_minphys(struct buf *bp)
1501 if (bp->b_bcount > zvol_maxphys)
1502 bp->b_bcount = zvol_maxphys;
1506 zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks)
1508 minor_t minor = getminor(dev);
1515 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1517 return (SET_ERROR(ENXIO));
1519 if ((zv->zv_flags & ZVOL_DUMPIFIED) == 0)
1520 return (SET_ERROR(EINVAL));
1522 boff = ldbtob(blkno);
1523 resid = ldbtob(nblocks);
1525 VERIFY3U(boff + resid, <=, zv->zv_volsize);
1528 size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff);
1529 error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE);
1542 zvol_read(dev_t dev, uio_t *uio, cred_t *cr)
1544 minor_t minor = getminor(dev);
1547 zvol_read(struct cdev *dev, struct uio *uio, int ioflag)
1556 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1558 return (SET_ERROR(ENXIO));
1563 volsize = zv->zv_volsize;
1564 if (uio->uio_resid > 0 &&
1565 (uio->uio_loffset < 0 || uio->uio_loffset > volsize))
1566 return (SET_ERROR(EIO));
1569 if (zv->zv_flags & ZVOL_DUMPIFIED) {
1570 error = physio(zvol_strategy, NULL, dev, B_READ,
1576 rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1578 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1579 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1581 /* don't read past the end */
1582 if (bytes > volsize - uio->uio_loffset)
1583 bytes = volsize - uio->uio_loffset;
1585 error = dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes);
1587 /* convert checksum errors into IO errors */
1588 if (error == ECKSUM)
1589 error = SET_ERROR(EIO);
1593 zfs_range_unlock(rl);
1600 zvol_write(dev_t dev, uio_t *uio, cred_t *cr)
1602 minor_t minor = getminor(dev);
1605 zvol_write(struct cdev *dev, struct uio *uio, int ioflag)
1615 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1617 return (SET_ERROR(ENXIO));
1622 volsize = zv->zv_volsize;
1623 if (uio->uio_resid > 0 &&
1624 (uio->uio_loffset < 0 || uio->uio_loffset > volsize))
1625 return (SET_ERROR(EIO));
1628 if (zv->zv_flags & ZVOL_DUMPIFIED) {
1629 error = physio(zvol_strategy, NULL, dev, B_WRITE,
1636 sync = !(zv->zv_flags & ZVOL_WCE) ||
1640 (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS);
1642 rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1644 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1645 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1646 uint64_t off = uio->uio_loffset;
1647 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
1649 if (bytes > volsize - off) /* don't write past the end */
1650 bytes = volsize - off;
1652 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
1653 error = dmu_tx_assign(tx, TXG_WAIT);
1658 error = dmu_write_uio_dbuf(zv->zv_dbuf, uio, bytes, tx);
1660 zvol_log_write(zv, tx, off, bytes, sync);
1666 zfs_range_unlock(rl);
1668 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1674 zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs)
1676 struct uuid uuid = EFI_RESERVED;
1677 efi_gpe_t gpe = { 0 };
1683 if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag))
1684 return (SET_ERROR(EFAULT));
1685 ptr = (char *)(uintptr_t)efi.dki_data_64;
1686 length = efi.dki_length;
1688 * Some clients may attempt to request a PMBR for the
1689 * zvol. Currently this interface will return EINVAL to
1690 * such requests. These requests could be supported by
1691 * adding a check for lba == 0 and consing up an appropriate
1694 if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0)
1695 return (SET_ERROR(EINVAL));
1697 gpe.efi_gpe_StartingLBA = LE_64(34ULL);
1698 gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1);
1699 UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
1701 if (efi.dki_lba == 1) {
1702 efi_gpt_t gpt = { 0 };
1704 gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
1705 gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
1706 gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt));
1707 gpt.efi_gpt_MyLBA = LE_64(1ULL);
1708 gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL);
1709 gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1);
1710 gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL);
1711 gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
1712 gpt.efi_gpt_SizeOfPartitionEntry =
1713 LE_32(sizeof (efi_gpe_t));
1714 CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
1715 gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
1716 CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table);
1717 gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
1718 if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length),
1720 return (SET_ERROR(EFAULT));
1721 ptr += sizeof (gpt);
1722 length -= sizeof (gpt);
1724 if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe),
1726 return (SET_ERROR(EFAULT));
1731 * BEGIN entry points to allow external callers access to the volume.
1734 * Return the volume parameters needed for access from an external caller.
1735 * These values are invariant as long as the volume is held open.
1738 zvol_get_volume_params(minor_t minor, uint64_t *blksize,
1739 uint64_t *max_xfer_len, void **minor_hdl, void **objset_hdl, void **zil_hdl,
1740 void **rl_hdl, void **bonus_hdl)
1744 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1746 return (SET_ERROR(ENXIO));
1747 if (zv->zv_flags & ZVOL_DUMPIFIED)
1748 return (SET_ERROR(ENXIO));
1750 ASSERT(blksize && max_xfer_len && minor_hdl &&
1751 objset_hdl && zil_hdl && rl_hdl && bonus_hdl);
1753 *blksize = zv->zv_volblocksize;
1754 *max_xfer_len = (uint64_t)zvol_maxphys;
1756 *objset_hdl = zv->zv_objset;
1757 *zil_hdl = zv->zv_zilog;
1758 *rl_hdl = &zv->zv_znode;
1759 *bonus_hdl = zv->zv_dbuf;
1764 * Return the current volume size to an external caller.
1765 * The size can change while the volume is open.
1768 zvol_get_volume_size(void *minor_hdl)
1770 zvol_state_t *zv = minor_hdl;
1772 return (zv->zv_volsize);
1776 * Return the current WCE setting to an external caller.
1777 * The WCE setting can change while the volume is open.
1780 zvol_get_volume_wce(void *minor_hdl)
1782 zvol_state_t *zv = minor_hdl;
1784 return ((zv->zv_flags & ZVOL_WCE) ? 1 : 0);
1788 * Entry point for external callers to zvol_log_write
1791 zvol_log_write_minor(void *minor_hdl, dmu_tx_t *tx, offset_t off, ssize_t resid,
1794 zvol_state_t *zv = minor_hdl;
1796 zvol_log_write(zv, tx, off, resid, sync);
1799 * END entry points to allow external callers access to the volume.
1804 * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE.
1807 zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off, uint64_t len,
1812 zilog_t *zilog = zv->zv_zilog;
1814 if (zil_replaying(zilog, tx))
1817 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1818 lr = (lr_truncate_t *)&itx->itx_lr;
1819 lr->lr_foid = ZVOL_OBJ;
1820 lr->lr_offset = off;
1821 lr->lr_length = len;
1823 itx->itx_sync = sync;
1824 zil_itx_assign(zilog, itx, tx);
1829 * Dirtbag ioctls to support mkfs(1M) for UFS filesystems. See dkio(7I).
1830 * Also a dirtbag dkio ioctl for unmap/free-block functionality.
1834 zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
1837 struct dk_callback *dkc;
1841 mutex_enter(&spa_namespace_lock);
1843 zv = zfsdev_get_soft_state(getminor(dev), ZSST_ZVOL);
1846 mutex_exit(&spa_namespace_lock);
1847 return (SET_ERROR(ENXIO));
1849 ASSERT(zv->zv_total_opens > 0);
1855 struct dk_cinfo dki;
1857 bzero(&dki, sizeof (dki));
1858 (void) strcpy(dki.dki_cname, "zvol");
1859 (void) strcpy(dki.dki_dname, "zvol");
1860 dki.dki_ctype = DKC_UNKNOWN;
1861 dki.dki_unit = getminor(dev);
1862 dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs);
1863 mutex_exit(&spa_namespace_lock);
1864 if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag))
1865 error = SET_ERROR(EFAULT);
1869 case DKIOCGMEDIAINFO:
1871 struct dk_minfo dkm;
1873 bzero(&dkm, sizeof (dkm));
1874 dkm.dki_lbsize = 1U << zv->zv_min_bs;
1875 dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
1876 dkm.dki_media_type = DK_UNKNOWN;
1877 mutex_exit(&spa_namespace_lock);
1878 if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
1879 error = SET_ERROR(EFAULT);
1883 case DKIOCGMEDIAINFOEXT:
1885 struct dk_minfo_ext dkmext;
1887 bzero(&dkmext, sizeof (dkmext));
1888 dkmext.dki_lbsize = 1U << zv->zv_min_bs;
1889 dkmext.dki_pbsize = zv->zv_volblocksize;
1890 dkmext.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
1891 dkmext.dki_media_type = DK_UNKNOWN;
1892 mutex_exit(&spa_namespace_lock);
1893 if (ddi_copyout(&dkmext, (void *)arg, sizeof (dkmext), flag))
1894 error = SET_ERROR(EFAULT);
1900 uint64_t vs = zv->zv_volsize;
1901 uint8_t bs = zv->zv_min_bs;
1903 mutex_exit(&spa_namespace_lock);
1904 error = zvol_getefi((void *)arg, flag, vs, bs);
1908 case DKIOCFLUSHWRITECACHE:
1909 dkc = (struct dk_callback *)arg;
1910 mutex_exit(&spa_namespace_lock);
1911 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1912 if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) {
1913 (*dkc->dkc_callback)(dkc->dkc_cookie, error);
1920 int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0;
1921 if (ddi_copyout(&wce, (void *)arg, sizeof (int),
1923 error = SET_ERROR(EFAULT);
1929 if (ddi_copyin((void *)arg, &wce, sizeof (int),
1931 error = SET_ERROR(EFAULT);
1935 zv->zv_flags |= ZVOL_WCE;
1936 mutex_exit(&spa_namespace_lock);
1938 zv->zv_flags &= ~ZVOL_WCE;
1939 mutex_exit(&spa_namespace_lock);
1940 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1948 * commands using these (like prtvtoc) expect ENOTSUP
1949 * since we're emulating an EFI label
1951 error = SET_ERROR(ENOTSUP);
1955 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1957 error = zvol_dumpify(zv);
1958 zfs_range_unlock(rl);
1962 if (!(zv->zv_flags & ZVOL_DUMPIFIED))
1964 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1966 error = zvol_dump_fini(zv);
1967 zfs_range_unlock(rl);
1975 if (ddi_copyin((void *)arg, &df, sizeof (df), flag)) {
1976 error = SET_ERROR(EFAULT);
1981 * Apply Postel's Law to length-checking. If they overshoot,
1982 * just blank out until the end, if there's a need to blank
1985 if (df.df_start >= zv->zv_volsize)
1986 break; /* No need to do anything... */
1987 if (df.df_start + df.df_length > zv->zv_volsize)
1988 df.df_length = DMU_OBJECT_END;
1990 rl = zfs_range_lock(&zv->zv_znode, df.df_start, df.df_length,
1992 tx = dmu_tx_create(zv->zv_objset);
1993 dmu_tx_mark_netfree(tx);
1994 error = dmu_tx_assign(tx, TXG_WAIT);
1998 zvol_log_truncate(zv, tx, df.df_start,
1999 df.df_length, B_TRUE);
2001 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ,
2002 df.df_start, df.df_length);
2005 zfs_range_unlock(rl);
2009 * If the write-cache is disabled or 'sync' property
2010 * is set to 'always' then treat this as a synchronous
2011 * operation (i.e. commit to zil).
2013 if (!(zv->zv_flags & ZVOL_WCE) ||
2014 (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS))
2015 zil_commit(zv->zv_zilog, ZVOL_OBJ);
2018 * If the caller really wants synchronous writes, and
2019 * can't wait for them, don't return until the write
2022 if (df.df_flags & DF_WAIT_SYNC) {
2024 dmu_objset_pool(zv->zv_objset), 0);
2031 error = SET_ERROR(ENOTTY);
2035 mutex_exit(&spa_namespace_lock);
2043 return (zvol_minors != 0);
2049 VERIFY(ddi_soft_state_init(&zfsdev_state, sizeof (zfs_soft_state_t),
2051 ZFS_LOG(1, "ZVOL Initialized.");
2057 ddi_soft_state_fini(&zfsdev_state);
2058 ZFS_LOG(1, "ZVOL Deinitialized.");
2064 zfs_mvdev_dump_feature_check(void *arg, dmu_tx_t *tx)
2066 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
2068 if (spa_feature_is_active(spa, SPA_FEATURE_MULTI_VDEV_CRASH_DUMP))
2075 zfs_mvdev_dump_activate_feature_sync(void *arg, dmu_tx_t *tx)
2077 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
2079 spa_feature_incr(spa, SPA_FEATURE_MULTI_VDEV_CRASH_DUMP, tx);
2083 zvol_dump_init(zvol_state_t *zv, boolean_t resize)
2087 objset_t *os = zv->zv_objset;
2088 spa_t *spa = dmu_objset_spa(os);
2089 vdev_t *vd = spa->spa_root_vdev;
2090 nvlist_t *nv = NULL;
2091 uint64_t version = spa_version(spa);
2092 enum zio_checksum checksum;
2094 ASSERT(MUTEX_HELD(&spa_namespace_lock));
2095 ASSERT(vd->vdev_ops == &vdev_root_ops);
2097 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0,
2099 /* wait for dmu_free_long_range to actually free the blocks */
2100 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
2103 * If the pool on which the dump device is being initialized has more
2104 * than one child vdev, check that the MULTI_VDEV_CRASH_DUMP feature is
2105 * enabled. If so, bump that feature's counter to indicate that the
2106 * feature is active. We also check the vdev type to handle the
2108 * # zpool create test raidz disk1 disk2 disk3
2109 * Now have spa_root_vdev->vdev_children == 1 (the raidz vdev),
2110 * the raidz vdev itself has 3 children.
2112 if (vd->vdev_children > 1 || vd->vdev_ops == &vdev_raidz_ops) {
2113 if (!spa_feature_is_enabled(spa,
2114 SPA_FEATURE_MULTI_VDEV_CRASH_DUMP))
2115 return (SET_ERROR(ENOTSUP));
2116 (void) dsl_sync_task(spa_name(spa),
2117 zfs_mvdev_dump_feature_check,
2118 zfs_mvdev_dump_activate_feature_sync, NULL,
2119 2, ZFS_SPACE_CHECK_RESERVED);
2122 tx = dmu_tx_create(os);
2123 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
2124 dmu_tx_hold_bonus(tx, ZVOL_OBJ);
2125 error = dmu_tx_assign(tx, TXG_WAIT);
2132 * If MULTI_VDEV_CRASH_DUMP is active, use the NOPARITY checksum
2133 * function. Otherwise, use the old default -- OFF.
2135 checksum = spa_feature_is_active(spa,
2136 SPA_FEATURE_MULTI_VDEV_CRASH_DUMP) ? ZIO_CHECKSUM_NOPARITY :
2140 * If we are resizing the dump device then we only need to
2141 * update the refreservation to match the newly updated
2142 * zvolsize. Otherwise, we save off the original state of the
2143 * zvol so that we can restore them if the zvol is ever undumpified.
2146 error = zap_update(os, ZVOL_ZAP_OBJ,
2147 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
2148 &zv->zv_volsize, tx);
2150 uint64_t checksum, compress, refresrv, vbs, dedup;
2152 error = dsl_prop_get_integer(zv->zv_name,
2153 zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL);
2154 error = error ? error : dsl_prop_get_integer(zv->zv_name,
2155 zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL);
2156 error = error ? error : dsl_prop_get_integer(zv->zv_name,
2157 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL);
2158 error = error ? error : dsl_prop_get_integer(zv->zv_name,
2159 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL);
2160 if (version >= SPA_VERSION_DEDUP) {
2161 error = error ? error :
2162 dsl_prop_get_integer(zv->zv_name,
2163 zfs_prop_to_name(ZFS_PROP_DEDUP), &dedup, NULL);
2166 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
2167 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1,
2169 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
2170 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx);
2171 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
2172 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
2174 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
2175 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1,
2177 error = error ? error : dmu_object_set_blocksize(
2178 os, ZVOL_OBJ, SPA_MAXBLOCKSIZE, 0, tx);
2179 if (version >= SPA_VERSION_DEDUP) {
2180 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
2181 zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1,
2185 zv->zv_volblocksize = SPA_MAXBLOCKSIZE;
2190 * We only need update the zvol's property if we are initializing
2191 * the dump area for the first time.
2194 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2195 VERIFY(nvlist_add_uint64(nv,
2196 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0);
2197 VERIFY(nvlist_add_uint64(nv,
2198 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
2199 ZIO_COMPRESS_OFF) == 0);
2200 VERIFY(nvlist_add_uint64(nv,
2201 zfs_prop_to_name(ZFS_PROP_CHECKSUM),
2203 if (version >= SPA_VERSION_DEDUP) {
2204 VERIFY(nvlist_add_uint64(nv,
2205 zfs_prop_to_name(ZFS_PROP_DEDUP),
2206 ZIO_CHECKSUM_OFF) == 0);
2209 error = zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
2217 /* Allocate the space for the dump */
2218 error = zvol_prealloc(zv);
2223 zvol_dumpify(zvol_state_t *zv)
2226 uint64_t dumpsize = 0;
2228 objset_t *os = zv->zv_objset;
2230 if (zv->zv_flags & ZVOL_RDONLY)
2231 return (SET_ERROR(EROFS));
2233 if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE,
2234 8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) {
2235 boolean_t resize = (dumpsize > 0);
2237 if ((error = zvol_dump_init(zv, resize)) != 0) {
2238 (void) zvol_dump_fini(zv);
2244 * Build up our lba mapping.
2246 error = zvol_get_lbas(zv);
2248 (void) zvol_dump_fini(zv);
2252 tx = dmu_tx_create(os);
2253 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
2254 error = dmu_tx_assign(tx, TXG_WAIT);
2257 (void) zvol_dump_fini(zv);
2261 zv->zv_flags |= ZVOL_DUMPIFIED;
2262 error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1,
2263 &zv->zv_volsize, tx);
2267 (void) zvol_dump_fini(zv);
2271 txg_wait_synced(dmu_objset_pool(os), 0);
2276 zvol_dump_fini(zvol_state_t *zv)
2279 objset_t *os = zv->zv_objset;
2282 uint64_t checksum, compress, refresrv, vbs, dedup;
2283 uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset));
2286 * Attempt to restore the zvol back to its pre-dumpified state.
2287 * This is a best-effort attempt as it's possible that not all
2288 * of these properties were initialized during the dumpify process
2289 * (i.e. error during zvol_dump_init).
2292 tx = dmu_tx_create(os);
2293 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
2294 error = dmu_tx_assign(tx, TXG_WAIT);
2299 (void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx);
2302 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2303 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum);
2304 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2305 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress);
2306 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2307 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv);
2308 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2309 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs);
2311 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2312 (void) nvlist_add_uint64(nv,
2313 zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum);
2314 (void) nvlist_add_uint64(nv,
2315 zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress);
2316 (void) nvlist_add_uint64(nv,
2317 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv);
2318 if (version >= SPA_VERSION_DEDUP &&
2319 zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2320 zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, &dedup) == 0) {
2321 (void) nvlist_add_uint64(nv,
2322 zfs_prop_to_name(ZFS_PROP_DEDUP), dedup);
2324 (void) zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
2328 zvol_free_extents(zv);
2329 zv->zv_flags &= ~ZVOL_DUMPIFIED;
2330 (void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END);
2331 /* wait for dmu_free_long_range to actually free the blocks */
2332 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
2333 tx = dmu_tx_create(os);
2334 dmu_tx_hold_bonus(tx, ZVOL_OBJ);
2335 error = dmu_tx_assign(tx, TXG_WAIT);
2340 if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0)
2341 zv->zv_volblocksize = vbs;
2349 zvol_geom_run(zvol_state_t *zv)
2351 struct g_provider *pp;
2353 pp = zv->zv_provider;
2354 g_error_provider(pp, 0);
2356 kproc_kthread_add(zvol_geom_worker, zv, &zfsproc, NULL, 0, 0,
2357 "zfskern", "zvol %s", pp->name + sizeof(ZVOL_DRIVER));
2361 zvol_geom_destroy(zvol_state_t *zv)
2363 struct g_provider *pp;
2365 g_topology_assert();
2367 mtx_lock(&zv->zv_queue_mtx);
2369 wakeup_one(&zv->zv_queue);
2370 while (zv->zv_state != 2)
2371 msleep(&zv->zv_state, &zv->zv_queue_mtx, 0, "zvol:w", 0);
2372 mtx_destroy(&zv->zv_queue_mtx);
2374 pp = zv->zv_provider;
2375 zv->zv_provider = NULL;
2377 g_wither_geom(pp->geom, ENXIO);
2381 zvol_geom_access(struct g_provider *pp, int acr, int acw, int ace)
2383 int count, error, flags;
2385 g_topology_assert();
2388 * To make it easier we expect either open or close, but not both
2391 KASSERT((acr >= 0 && acw >= 0 && ace >= 0) ||
2392 (acr <= 0 && acw <= 0 && ace <= 0),
2393 ("Unsupported access request to %s (acr=%d, acw=%d, ace=%d).",
2394 pp->name, acr, acw, ace));
2396 if (pp->private == NULL) {
2397 if (acr <= 0 && acw <= 0 && ace <= 0)
2403 * We don't pass FEXCL flag to zvol_open()/zvol_close() if ace != 0,
2404 * because GEOM already handles that and handles it a bit differently.
2405 * GEOM allows for multiple read/exclusive consumers and ZFS allows
2406 * only one exclusive consumer, no matter if it is reader or writer.
2407 * I like better the way GEOM works so I'll leave it for GEOM to
2408 * decide what to do.
2411 count = acr + acw + ace;
2416 if (acr != 0 || ace != 0)
2421 g_topology_unlock();
2423 error = zvol_open(pp, flags, count);
2425 error = zvol_close(pp, flags, -count);
2431 zvol_geom_start(struct bio *bp)
2436 zv = bp->bio_to->private;
2438 switch (bp->bio_cmd) {
2440 if (!THREAD_CAN_SLEEP())
2442 zil_commit(zv->zv_zilog, ZVOL_OBJ);
2443 g_io_deliver(bp, 0);
2448 if (!THREAD_CAN_SLEEP())
2453 if (g_handleattr_int(bp, "GEOM::candelete", 1))
2457 g_io_deliver(bp, EOPNOTSUPP);
2463 mtx_lock(&zv->zv_queue_mtx);
2464 first = (bioq_first(&zv->zv_queue) == NULL);
2465 bioq_insert_tail(&zv->zv_queue, bp);
2466 mtx_unlock(&zv->zv_queue_mtx);
2468 wakeup_one(&zv->zv_queue);
2472 zvol_geom_worker(void *arg)
2477 thread_lock(curthread);
2478 sched_prio(curthread, PRIBIO);
2479 thread_unlock(curthread);
2483 mtx_lock(&zv->zv_queue_mtx);
2484 bp = bioq_takefirst(&zv->zv_queue);
2486 if (zv->zv_state == 1) {
2488 wakeup(&zv->zv_state);
2489 mtx_unlock(&zv->zv_queue_mtx);
2492 msleep(&zv->zv_queue, &zv->zv_queue_mtx, PRIBIO | PDROP,
2496 mtx_unlock(&zv->zv_queue_mtx);
2497 switch (bp->bio_cmd) {
2499 zil_commit(zv->zv_zilog, ZVOL_OBJ);
2500 g_io_deliver(bp, 0);
2510 extern boolean_t dataset_name_hidden(const char *name);
2513 zvol_create_snapshots(objset_t *os, const char *name)
2515 uint64_t cookie, obj;
2520 sname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2523 (void) dmu_objset_find(name, dmu_objset_prefetch, NULL,
2528 len = snprintf(sname, MAXPATHLEN, "%s@", name);
2529 if (len >= MAXPATHLEN) {
2530 dmu_objset_rele(os, FTAG);
2531 error = ENAMETOOLONG;
2535 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
2536 error = dmu_snapshot_list_next(os, MAXPATHLEN - len,
2537 sname + len, &obj, &cookie, NULL);
2538 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
2540 if (error == ENOENT)
2545 if ((error = zvol_create_minor(sname)) != 0) {
2546 printf("ZFS WARNING: Unable to create ZVOL %s (error=%d).\n",
2552 kmem_free(sname, MAXPATHLEN);
2557 zvol_create_minors(const char *name)
2564 if (dataset_name_hidden(name))
2567 if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) {
2568 printf("ZFS WARNING: Unable to put hold on %s (error=%d).\n",
2572 if (dmu_objset_type(os) == DMU_OST_ZVOL) {
2573 dsl_dataset_long_hold(os->os_dsl_dataset, FTAG);
2574 dsl_pool_rele(dmu_objset_pool(os), FTAG);
2575 error = zvol_create_minor(name);
2576 if (error == 0 || error == EEXIST) {
2577 error = zvol_create_snapshots(os, name);
2579 printf("ZFS WARNING: Unable to create ZVOL %s (error=%d).\n",
2582 dsl_dataset_long_rele(os->os_dsl_dataset, FTAG);
2583 dsl_dataset_rele(os->os_dsl_dataset, FTAG);
2586 if (dmu_objset_type(os) != DMU_OST_ZFS) {
2587 dmu_objset_rele(os, FTAG);
2591 osname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2592 if (snprintf(osname, MAXPATHLEN, "%s/", name) >= MAXPATHLEN) {
2593 dmu_objset_rele(os, FTAG);
2594 kmem_free(osname, MAXPATHLEN);
2597 p = osname + strlen(osname);
2598 len = MAXPATHLEN - (p - osname);
2601 /* Prefetch the datasets. */
2603 while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0) {
2604 if (!dataset_name_hidden(osname))
2605 (void) dmu_objset_prefetch(osname, NULL);
2610 while (dmu_dir_list_next(os, MAXPATHLEN - (p - osname), p, NULL,
2612 dmu_objset_rele(os, FTAG);
2613 (void)zvol_create_minors(osname);
2614 if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) {
2615 printf("ZFS WARNING: Unable to put hold on %s (error=%d).\n",
2621 dmu_objset_rele(os, FTAG);
2622 kmem_free(osname, MAXPATHLEN);
2627 zvol_rename_minor(zvol_state_t *zv, const char *newname)
2630 struct g_provider *pp;
2633 ASSERT(MUTEX_HELD(&spa_namespace_lock));
2635 if (zv->zv_volmode == ZFS_VOLMODE_GEOM) {
2637 pp = zv->zv_provider;
2642 zv->zv_provider = NULL;
2643 g_wither_provider(pp, ENXIO);
2645 pp = g_new_providerf(gp, "%s/%s", ZVOL_DRIVER, newname);
2646 pp->flags |= G_PF_DIRECT_RECEIVE | G_PF_DIRECT_SEND;
2647 pp->sectorsize = DEV_BSIZE;
2648 pp->mediasize = zv->zv_volsize;
2650 zv->zv_provider = pp;
2651 g_error_provider(pp, 0);
2652 g_topology_unlock();
2653 } else if (zv->zv_volmode == ZFS_VOLMODE_DEV) {
2655 ASSERT(dev != NULL);
2659 if (make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK,
2660 &dev, &zvol_cdevsw, NULL, UID_ROOT, GID_OPERATOR,
2661 0640, "%s/%s", ZVOL_DRIVER, newname) == 0) {
2663 dev->si_iosize_max = MAXPHYS;
2667 strlcpy(zv->zv_name, newname, sizeof(zv->zv_name));
2671 zvol_rename_minors(const char *oldname, const char *newname)
2673 char name[MAXPATHLEN];
2674 struct g_provider *pp;
2676 size_t oldnamelen, newnamelen;
2679 boolean_t locked = B_FALSE;
2681 oldnamelen = strlen(oldname);
2682 newnamelen = strlen(newname);
2685 /* See comment in zvol_open(). */
2686 if (!MUTEX_HELD(&spa_namespace_lock)) {
2687 mutex_enter(&spa_namespace_lock);
2691 LIST_FOREACH(zv, &all_zvols, zv_links) {
2692 if (strcmp(zv->zv_name, oldname) == 0) {
2693 zvol_rename_minor(zv, newname);
2694 } else if (strncmp(zv->zv_name, oldname, oldnamelen) == 0 &&
2695 (zv->zv_name[oldnamelen] == '/' ||
2696 zv->zv_name[oldnamelen] == '@')) {
2697 snprintf(name, sizeof(name), "%s%c%s", newname,
2698 zv->zv_name[oldnamelen],
2699 zv->zv_name + oldnamelen + 1);
2700 zvol_rename_minor(zv, name);
2705 mutex_exit(&spa_namespace_lock);
2710 zvol_d_open(struct cdev *dev, int flags, int fmt, struct thread *td)
2715 mutex_enter(&spa_namespace_lock);
2718 mutex_exit(&spa_namespace_lock);
2719 return(ENXIO); /* zvol_create_minor() not done yet */
2722 if (zv->zv_total_opens == 0)
2723 err = zvol_first_open(zv);
2725 mutex_exit(&spa_namespace_lock);
2728 if ((flags & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
2729 err = SET_ERROR(EROFS);
2732 if (zv->zv_flags & ZVOL_EXCL) {
2733 err = SET_ERROR(EBUSY);
2737 if (flags & FEXCL) {
2738 if (zv->zv_total_opens != 0) {
2739 err = SET_ERROR(EBUSY);
2742 zv->zv_flags |= ZVOL_EXCL;
2746 zv->zv_total_opens++;
2747 mutex_exit(&spa_namespace_lock);
2750 if (zv->zv_total_opens == 0)
2751 zvol_last_close(zv);
2752 mutex_exit(&spa_namespace_lock);
2757 zvol_d_close(struct cdev *dev, int flags, int fmt, struct thread *td)
2762 mutex_enter(&spa_namespace_lock);
2765 mutex_exit(&spa_namespace_lock);
2769 if (zv->zv_flags & ZVOL_EXCL) {
2770 ASSERT(zv->zv_total_opens == 1);
2771 zv->zv_flags &= ~ZVOL_EXCL;
2775 * If the open count is zero, this is a spurious close.
2776 * That indicates a bug in the kernel / DDI framework.
2778 ASSERT(zv->zv_total_opens != 0);
2781 * You may get multiple opens, but only one close.
2783 zv->zv_total_opens--;
2785 if (zv->zv_total_opens == 0)
2786 zvol_last_close(zv);
2788 mutex_exit(&spa_namespace_lock);
2793 zvol_d_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
2797 off_t offset, length, chunk;
2804 KASSERT(zv->zv_total_opens > 0,
2805 ("Device with zero access count in zvol_d_ioctl"));
2807 i = IOCPARM_LEN(cmd);
2809 case DIOCGSECTORSIZE:
2810 *(u_int *)data = DEV_BSIZE;
2812 case DIOCGMEDIASIZE:
2813 *(off_t *)data = zv->zv_volsize;
2816 zil_commit(zv->zv_zilog, ZVOL_OBJ);
2819 offset = ((off_t *)data)[0];
2820 length = ((off_t *)data)[1];
2821 if ((offset % DEV_BSIZE) != 0 || (length % DEV_BSIZE) != 0 ||
2822 offset < 0 || offset >= zv->zv_volsize ||
2824 printf("%s: offset=%jd length=%jd\n", __func__, offset,
2830 rl = zfs_range_lock(&zv->zv_znode, offset, length, RL_WRITER);
2831 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
2832 error = dmu_tx_assign(tx, TXG_WAIT);
2836 zvol_log_truncate(zv, tx, offset, length, B_TRUE);
2838 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ,
2841 zfs_range_unlock(rl);
2842 if (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)
2843 zil_commit(zv->zv_zilog, ZVOL_OBJ);
2845 case DIOCGSTRIPESIZE:
2846 *(off_t *)data = zv->zv_volblocksize;
2848 case DIOCGSTRIPEOFFSET: