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 zv->zv_name[namelen] == '/')) {
887 (void) zvol_remove_zv(zv);
891 mutex_exit(&spa_namespace_lock);
896 zvol_set_volsize(const char *name, major_t maj, uint64_t volsize)
898 zvol_state_t *zv = NULL;
901 dmu_object_info_t doi;
902 uint64_t old_volsize = 0ULL;
905 mutex_enter(&spa_namespace_lock);
906 zv = zvol_minor_lookup(name);
907 if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) {
908 mutex_exit(&spa_namespace_lock);
912 if ((error = dmu_object_info(os, ZVOL_OBJ, &doi)) != 0 ||
913 (error = zvol_check_volsize(volsize,
914 doi.doi_data_block_size)) != 0)
917 VERIFY(dsl_prop_get_integer(name, "readonly", &readonly,
924 error = zvol_update_volsize(os, volsize);
926 * Reinitialize the dump area to the new size. If we
927 * failed to resize the dump area then restore it back to
930 if (zv && error == 0) {
932 if (zv->zv_flags & ZVOL_DUMPIFIED) {
933 old_volsize = zv->zv_volsize;
934 zv->zv_volsize = volsize;
935 if ((error = zvol_dumpify(zv)) != 0 ||
936 (error = dumpvp_resize()) != 0) {
937 (void) zvol_update_volsize(os, old_volsize);
938 zv->zv_volsize = old_volsize;
939 error = zvol_dumpify(zv);
942 #endif /* ZVOL_DUMP */
944 zv->zv_volsize = volsize;
945 zvol_size_changed(zv);
951 * Generate a LUN expansion event.
953 if (zv && error == 0) {
956 char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
958 (void) snprintf(physpath, MAXPATHLEN, "%s%u", ZVOL_PSEUDO_DEV,
961 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
962 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
964 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
965 ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
968 kmem_free(physpath, MAXPATHLEN);
973 dmu_objset_rele(os, FTAG);
975 mutex_exit(&spa_namespace_lock);
982 zvol_open(struct g_provider *pp, int flag, int count)
986 boolean_t locked = B_FALSE;
989 * Protect against recursively entering spa_namespace_lock
990 * when spa_open() is used for a pool on a (local) ZVOL(s).
991 * This is needed since we replaced upstream zfsdev_state_lock
992 * with spa_namespace_lock in the ZVOL code.
993 * We are using the same trick as spa_open().
994 * Note that calls in zvol_first_open which need to resolve
995 * pool name to a spa object will enter spa_open()
996 * recursively, but that function already has all the
997 * necessary protection.
999 if (!MUTEX_HELD(&spa_namespace_lock)) {
1000 mutex_enter(&spa_namespace_lock);
1007 mutex_exit(&spa_namespace_lock);
1008 return (SET_ERROR(ENXIO));
1011 if (zv->zv_total_opens == 0) {
1012 err = zvol_first_open(zv);
1015 mutex_exit(&spa_namespace_lock);
1018 pp->mediasize = zv->zv_volsize;
1019 pp->stripeoffset = 0;
1020 pp->stripesize = zv->zv_volblocksize;
1022 if ((flag & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
1023 err = SET_ERROR(EROFS);
1026 if (zv->zv_flags & ZVOL_EXCL) {
1027 err = SET_ERROR(EBUSY);
1032 if (zv->zv_total_opens != 0) {
1033 err = SET_ERROR(EBUSY);
1036 zv->zv_flags |= ZVOL_EXCL;
1040 zv->zv_total_opens += count;
1042 mutex_exit(&spa_namespace_lock);
1046 if (zv->zv_total_opens == 0)
1047 zvol_last_close(zv);
1049 mutex_exit(&spa_namespace_lock);
1055 zvol_close(struct g_provider *pp, int flag, int count)
1059 boolean_t locked = B_FALSE;
1061 /* See comment in zvol_open(). */
1062 if (!MUTEX_HELD(&spa_namespace_lock)) {
1063 mutex_enter(&spa_namespace_lock);
1070 mutex_exit(&spa_namespace_lock);
1071 return (SET_ERROR(ENXIO));
1074 if (zv->zv_flags & ZVOL_EXCL) {
1075 ASSERT(zv->zv_total_opens == 1);
1076 zv->zv_flags &= ~ZVOL_EXCL;
1080 * If the open count is zero, this is a spurious close.
1081 * That indicates a bug in the kernel / DDI framework.
1083 ASSERT(zv->zv_total_opens != 0);
1086 * You may get multiple opens, but only one close.
1088 zv->zv_total_opens -= count;
1090 if (zv->zv_total_opens == 0)
1091 zvol_last_close(zv);
1094 mutex_exit(&spa_namespace_lock);
1099 zvol_get_done(zgd_t *zgd, int error)
1102 dmu_buf_rele(zgd->zgd_db, zgd);
1104 zfs_range_unlock(zgd->zgd_rl);
1106 if (error == 0 && zgd->zgd_bp)
1107 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1109 kmem_free(zgd, sizeof (zgd_t));
1113 * Get data to generate a TX_WRITE intent log record.
1116 zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1118 zvol_state_t *zv = arg;
1119 objset_t *os = zv->zv_objset;
1120 uint64_t object = ZVOL_OBJ;
1121 uint64_t offset = lr->lr_offset;
1122 uint64_t size = lr->lr_length; /* length of user data */
1123 blkptr_t *bp = &lr->lr_blkptr;
1128 ASSERT(zio != NULL);
1131 zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1132 zgd->zgd_zilog = zv->zv_zilog;
1133 zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
1136 * Write records come in two flavors: immediate and indirect.
1137 * For small writes it's cheaper to store the data with the
1138 * log record (immediate); for large writes it's cheaper to
1139 * sync the data and get a pointer to it (indirect) so that
1140 * we don't have to write the data twice.
1142 if (buf != NULL) { /* immediate write */
1143 error = dmu_read(os, object, offset, size, buf,
1144 DMU_READ_NO_PREFETCH);
1146 size = zv->zv_volblocksize;
1147 offset = P2ALIGN(offset, size);
1148 error = dmu_buf_hold(os, object, offset, zgd, &db,
1149 DMU_READ_NO_PREFETCH);
1151 blkptr_t *obp = dmu_buf_get_blkptr(db);
1153 ASSERT(BP_IS_HOLE(bp));
1160 ASSERT(db->db_offset == offset);
1161 ASSERT(db->db_size == size);
1163 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1164 zvol_get_done, zgd);
1171 zvol_get_done(zgd, error);
1177 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
1179 * We store data in the log buffers if it's small enough.
1180 * Otherwise we will later flush the data out via dmu_sync().
1182 ssize_t zvol_immediate_write_sz = 32768;
1185 zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid,
1188 uint32_t blocksize = zv->zv_volblocksize;
1189 zilog_t *zilog = zv->zv_zilog;
1191 ssize_t immediate_write_sz;
1193 if (zil_replaying(zilog, tx))
1196 immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
1197 ? 0 : zvol_immediate_write_sz;
1199 slogging = spa_has_slogs(zilog->zl_spa) &&
1200 (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
1206 itx_wr_state_t write_state;
1209 * Unlike zfs_log_write() we can be called with
1210 * upto DMU_MAX_ACCESS/2 (5MB) writes.
1212 if (blocksize > immediate_write_sz && !slogging &&
1213 resid >= blocksize && off % blocksize == 0) {
1214 write_state = WR_INDIRECT; /* uses dmu_sync */
1217 write_state = WR_COPIED;
1218 len = MIN(ZIL_MAX_LOG_DATA, resid);
1220 write_state = WR_NEED_COPY;
1221 len = MIN(ZIL_MAX_LOG_DATA, resid);
1224 itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
1225 (write_state == WR_COPIED ? len : 0));
1226 lr = (lr_write_t *)&itx->itx_lr;
1227 if (write_state == WR_COPIED && dmu_read(zv->zv_objset,
1228 ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
1229 zil_itx_destroy(itx);
1230 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1231 lr = (lr_write_t *)&itx->itx_lr;
1232 write_state = WR_NEED_COPY;
1235 itx->itx_wr_state = write_state;
1236 if (write_state == WR_NEED_COPY)
1237 itx->itx_sod += len;
1238 lr->lr_foid = ZVOL_OBJ;
1239 lr->lr_offset = off;
1240 lr->lr_length = len;
1242 BP_ZERO(&lr->lr_blkptr);
1244 itx->itx_private = zv;
1245 itx->itx_sync = sync;
1247 zil_itx_assign(zilog, itx, tx);
1256 zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t origoffset,
1257 uint64_t size, boolean_t doread, boolean_t isdump)
1263 if (vd->vdev_ops == &vdev_mirror_ops ||
1264 vd->vdev_ops == &vdev_replacing_ops ||
1265 vd->vdev_ops == &vdev_spare_ops) {
1266 for (c = 0; c < vd->vdev_children; c++) {
1267 int err = zvol_dumpio_vdev(vd->vdev_child[c],
1268 addr, offset, origoffset, size, doread, isdump);
1271 } else if (doread) {
1277 if (!vd->vdev_ops->vdev_op_leaf && vd->vdev_ops != &vdev_raidz_ops)
1278 return (numerrors < vd->vdev_children ? 0 : EIO);
1280 if (doread && !vdev_readable(vd))
1281 return (SET_ERROR(EIO));
1282 else if (!doread && !vdev_writeable(vd))
1283 return (SET_ERROR(EIO));
1285 if (vd->vdev_ops == &vdev_raidz_ops) {
1286 return (vdev_raidz_physio(vd,
1287 addr, size, offset, origoffset, doread, isdump));
1290 offset += VDEV_LABEL_START_SIZE;
1292 if (ddi_in_panic() || isdump) {
1295 return (SET_ERROR(EIO));
1297 ASSERT3P(dvd, !=, NULL);
1298 return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset),
1302 ASSERT3P(dvd, !=, NULL);
1303 return (vdev_disk_ldi_physio(dvd->vd_lh, addr, size,
1304 offset, doread ? B_READ : B_WRITE));
1309 zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size,
1310 boolean_t doread, boolean_t isdump)
1315 spa_t *spa = dmu_objset_spa(zv->zv_objset);
1317 /* Must be sector aligned, and not stradle a block boundary. */
1318 if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) ||
1319 P2BOUNDARY(offset, size, zv->zv_volblocksize)) {
1320 return (SET_ERROR(EINVAL));
1322 ASSERT(size <= zv->zv_volblocksize);
1324 /* Locate the extent this belongs to */
1325 ze = list_head(&zv->zv_extents);
1326 while (offset >= ze->ze_nblks * zv->zv_volblocksize) {
1327 offset -= ze->ze_nblks * zv->zv_volblocksize;
1328 ze = list_next(&zv->zv_extents, ze);
1332 return (SET_ERROR(EINVAL));
1334 if (!ddi_in_panic())
1335 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
1337 vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva));
1338 offset += DVA_GET_OFFSET(&ze->ze_dva);
1339 error = zvol_dumpio_vdev(vd, addr, offset, DVA_GET_OFFSET(&ze->ze_dva),
1340 size, doread, isdump);
1342 if (!ddi_in_panic())
1343 spa_config_exit(spa, SCL_STATE, FTAG);
1350 zvol_strategy(struct bio *bp)
1353 uint64_t off, volsize;
1359 boolean_t doread = 0;
1360 boolean_t is_dumpified;
1364 zv = bp->bio_to->private;
1366 zv = bp->bio_dev->si_drv2;
1373 if (bp->bio_cmd != BIO_READ && (zv->zv_flags & ZVOL_RDONLY)) {
1378 switch (bp->bio_cmd) {
1391 off = bp->bio_offset;
1392 volsize = zv->zv_volsize;
1397 addr = bp->bio_data;
1398 resid = bp->bio_length;
1400 if (resid > 0 && (off < 0 || off >= volsize)) {
1406 is_dumpified = zv->zv_flags & ZVOL_DUMPIFIED;
1408 is_dumpified = B_FALSE;
1410 sync = !doread && !is_dumpified &&
1411 zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS;
1414 * There must be no buffer changes when doing a dmu_sync() because
1415 * we can't change the data whilst calculating the checksum.
1417 rl = zfs_range_lock(&zv->zv_znode, off, resid,
1418 doread ? RL_READER : RL_WRITER);
1420 if (bp->bio_cmd == BIO_DELETE) {
1421 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
1422 error = dmu_tx_assign(tx, TXG_WAIT);
1426 zvol_log_truncate(zv, tx, off, resid, B_TRUE);
1428 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ,
1435 while (resid != 0 && off < volsize) {
1436 size_t size = MIN(resid, zvol_maxphys);
1439 size = MIN(size, P2END(off, zv->zv_volblocksize) - off);
1440 error = zvol_dumpio(zv, addr, off, size,
1442 } else if (doread) {
1446 error = dmu_read(os, ZVOL_OBJ, off, size, addr,
1449 dmu_tx_t *tx = dmu_tx_create(os);
1450 dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
1451 error = dmu_tx_assign(tx, TXG_WAIT);
1455 dmu_write(os, ZVOL_OBJ, off, size, addr, tx);
1456 zvol_log_write(zv, tx, off, size, sync);
1461 /* convert checksum errors into IO errors */
1462 if (error == ECKSUM)
1463 error = SET_ERROR(EIO);
1471 zfs_range_unlock(rl);
1473 bp->bio_completed = bp->bio_length - resid;
1474 if (bp->bio_completed < bp->bio_length && off > volsize)
1479 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1483 g_io_deliver(bp, error);
1485 biofinish(bp, NULL, error);
1490 * Set the buffer count to the zvol maximum transfer.
1491 * Using our own routine instead of the default minphys()
1492 * means that for larger writes we write bigger buffers on X86
1493 * (128K instead of 56K) and flush the disk write cache less often
1494 * (every zvol_maxphys - currently 1MB) instead of minphys (currently
1495 * 56K on X86 and 128K on sparc).
1498 zvol_minphys(struct buf *bp)
1500 if (bp->b_bcount > zvol_maxphys)
1501 bp->b_bcount = zvol_maxphys;
1505 zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks)
1507 minor_t minor = getminor(dev);
1514 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1516 return (SET_ERROR(ENXIO));
1518 if ((zv->zv_flags & ZVOL_DUMPIFIED) == 0)
1519 return (SET_ERROR(EINVAL));
1521 boff = ldbtob(blkno);
1522 resid = ldbtob(nblocks);
1524 VERIFY3U(boff + resid, <=, zv->zv_volsize);
1527 size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff);
1528 error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE);
1541 zvol_read(dev_t dev, uio_t *uio, cred_t *cr)
1543 minor_t minor = getminor(dev);
1546 zvol_read(struct cdev *dev, struct uio *uio, int ioflag)
1555 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1557 return (SET_ERROR(ENXIO));
1562 volsize = zv->zv_volsize;
1563 if (uio->uio_resid > 0 &&
1564 (uio->uio_loffset < 0 || uio->uio_loffset > volsize))
1565 return (SET_ERROR(EIO));
1568 if (zv->zv_flags & ZVOL_DUMPIFIED) {
1569 error = physio(zvol_strategy, NULL, dev, B_READ,
1575 rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1577 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1578 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1580 /* don't read past the end */
1581 if (bytes > volsize - uio->uio_loffset)
1582 bytes = volsize - uio->uio_loffset;
1584 error = dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes);
1586 /* convert checksum errors into IO errors */
1587 if (error == ECKSUM)
1588 error = SET_ERROR(EIO);
1592 zfs_range_unlock(rl);
1599 zvol_write(dev_t dev, uio_t *uio, cred_t *cr)
1601 minor_t minor = getminor(dev);
1604 zvol_write(struct cdev *dev, struct uio *uio, int ioflag)
1614 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1616 return (SET_ERROR(ENXIO));
1621 volsize = zv->zv_volsize;
1622 if (uio->uio_resid > 0 &&
1623 (uio->uio_loffset < 0 || uio->uio_loffset > volsize))
1624 return (SET_ERROR(EIO));
1627 if (zv->zv_flags & ZVOL_DUMPIFIED) {
1628 error = physio(zvol_strategy, NULL, dev, B_WRITE,
1635 sync = !(zv->zv_flags & ZVOL_WCE) ||
1639 (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS);
1641 rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1643 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1644 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1645 uint64_t off = uio->uio_loffset;
1646 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
1648 if (bytes > volsize - off) /* don't write past the end */
1649 bytes = volsize - off;
1651 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
1652 error = dmu_tx_assign(tx, TXG_WAIT);
1657 error = dmu_write_uio_dbuf(zv->zv_dbuf, uio, bytes, tx);
1659 zvol_log_write(zv, tx, off, bytes, sync);
1665 zfs_range_unlock(rl);
1667 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1673 zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs)
1675 struct uuid uuid = EFI_RESERVED;
1676 efi_gpe_t gpe = { 0 };
1682 if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag))
1683 return (SET_ERROR(EFAULT));
1684 ptr = (char *)(uintptr_t)efi.dki_data_64;
1685 length = efi.dki_length;
1687 * Some clients may attempt to request a PMBR for the
1688 * zvol. Currently this interface will return EINVAL to
1689 * such requests. These requests could be supported by
1690 * adding a check for lba == 0 and consing up an appropriate
1693 if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0)
1694 return (SET_ERROR(EINVAL));
1696 gpe.efi_gpe_StartingLBA = LE_64(34ULL);
1697 gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1);
1698 UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
1700 if (efi.dki_lba == 1) {
1701 efi_gpt_t gpt = { 0 };
1703 gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
1704 gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
1705 gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt));
1706 gpt.efi_gpt_MyLBA = LE_64(1ULL);
1707 gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL);
1708 gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1);
1709 gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL);
1710 gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
1711 gpt.efi_gpt_SizeOfPartitionEntry =
1712 LE_32(sizeof (efi_gpe_t));
1713 CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
1714 gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
1715 CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table);
1716 gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
1717 if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length),
1719 return (SET_ERROR(EFAULT));
1720 ptr += sizeof (gpt);
1721 length -= sizeof (gpt);
1723 if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe),
1725 return (SET_ERROR(EFAULT));
1730 * BEGIN entry points to allow external callers access to the volume.
1733 * Return the volume parameters needed for access from an external caller.
1734 * These values are invariant as long as the volume is held open.
1737 zvol_get_volume_params(minor_t minor, uint64_t *blksize,
1738 uint64_t *max_xfer_len, void **minor_hdl, void **objset_hdl, void **zil_hdl,
1739 void **rl_hdl, void **bonus_hdl)
1743 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1745 return (SET_ERROR(ENXIO));
1746 if (zv->zv_flags & ZVOL_DUMPIFIED)
1747 return (SET_ERROR(ENXIO));
1749 ASSERT(blksize && max_xfer_len && minor_hdl &&
1750 objset_hdl && zil_hdl && rl_hdl && bonus_hdl);
1752 *blksize = zv->zv_volblocksize;
1753 *max_xfer_len = (uint64_t)zvol_maxphys;
1755 *objset_hdl = zv->zv_objset;
1756 *zil_hdl = zv->zv_zilog;
1757 *rl_hdl = &zv->zv_znode;
1758 *bonus_hdl = zv->zv_dbuf;
1763 * Return the current volume size to an external caller.
1764 * The size can change while the volume is open.
1767 zvol_get_volume_size(void *minor_hdl)
1769 zvol_state_t *zv = minor_hdl;
1771 return (zv->zv_volsize);
1775 * Return the current WCE setting to an external caller.
1776 * The WCE setting can change while the volume is open.
1779 zvol_get_volume_wce(void *minor_hdl)
1781 zvol_state_t *zv = minor_hdl;
1783 return ((zv->zv_flags & ZVOL_WCE) ? 1 : 0);
1787 * Entry point for external callers to zvol_log_write
1790 zvol_log_write_minor(void *minor_hdl, dmu_tx_t *tx, offset_t off, ssize_t resid,
1793 zvol_state_t *zv = minor_hdl;
1795 zvol_log_write(zv, tx, off, resid, sync);
1798 * END entry points to allow external callers access to the volume.
1803 * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE.
1806 zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off, uint64_t len,
1811 zilog_t *zilog = zv->zv_zilog;
1813 if (zil_replaying(zilog, tx))
1816 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1817 lr = (lr_truncate_t *)&itx->itx_lr;
1818 lr->lr_foid = ZVOL_OBJ;
1819 lr->lr_offset = off;
1820 lr->lr_length = len;
1822 itx->itx_sync = sync;
1823 zil_itx_assign(zilog, itx, tx);
1828 * Dirtbag ioctls to support mkfs(1M) for UFS filesystems. See dkio(7I).
1829 * Also a dirtbag dkio ioctl for unmap/free-block functionality.
1833 zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
1836 struct dk_callback *dkc;
1840 mutex_enter(&spa_namespace_lock);
1842 zv = zfsdev_get_soft_state(getminor(dev), ZSST_ZVOL);
1845 mutex_exit(&spa_namespace_lock);
1846 return (SET_ERROR(ENXIO));
1848 ASSERT(zv->zv_total_opens > 0);
1854 struct dk_cinfo dki;
1856 bzero(&dki, sizeof (dki));
1857 (void) strcpy(dki.dki_cname, "zvol");
1858 (void) strcpy(dki.dki_dname, "zvol");
1859 dki.dki_ctype = DKC_UNKNOWN;
1860 dki.dki_unit = getminor(dev);
1861 dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs);
1862 mutex_exit(&spa_namespace_lock);
1863 if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag))
1864 error = SET_ERROR(EFAULT);
1868 case DKIOCGMEDIAINFO:
1870 struct dk_minfo dkm;
1872 bzero(&dkm, sizeof (dkm));
1873 dkm.dki_lbsize = 1U << zv->zv_min_bs;
1874 dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
1875 dkm.dki_media_type = DK_UNKNOWN;
1876 mutex_exit(&spa_namespace_lock);
1877 if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
1878 error = SET_ERROR(EFAULT);
1882 case DKIOCGMEDIAINFOEXT:
1884 struct dk_minfo_ext dkmext;
1886 bzero(&dkmext, sizeof (dkmext));
1887 dkmext.dki_lbsize = 1U << zv->zv_min_bs;
1888 dkmext.dki_pbsize = zv->zv_volblocksize;
1889 dkmext.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
1890 dkmext.dki_media_type = DK_UNKNOWN;
1891 mutex_exit(&spa_namespace_lock);
1892 if (ddi_copyout(&dkmext, (void *)arg, sizeof (dkmext), flag))
1893 error = SET_ERROR(EFAULT);
1899 uint64_t vs = zv->zv_volsize;
1900 uint8_t bs = zv->zv_min_bs;
1902 mutex_exit(&spa_namespace_lock);
1903 error = zvol_getefi((void *)arg, flag, vs, bs);
1907 case DKIOCFLUSHWRITECACHE:
1908 dkc = (struct dk_callback *)arg;
1909 mutex_exit(&spa_namespace_lock);
1910 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1911 if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) {
1912 (*dkc->dkc_callback)(dkc->dkc_cookie, error);
1919 int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0;
1920 if (ddi_copyout(&wce, (void *)arg, sizeof (int),
1922 error = SET_ERROR(EFAULT);
1928 if (ddi_copyin((void *)arg, &wce, sizeof (int),
1930 error = SET_ERROR(EFAULT);
1934 zv->zv_flags |= ZVOL_WCE;
1935 mutex_exit(&spa_namespace_lock);
1937 zv->zv_flags &= ~ZVOL_WCE;
1938 mutex_exit(&spa_namespace_lock);
1939 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1947 * commands using these (like prtvtoc) expect ENOTSUP
1948 * since we're emulating an EFI label
1950 error = SET_ERROR(ENOTSUP);
1954 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1956 error = zvol_dumpify(zv);
1957 zfs_range_unlock(rl);
1961 if (!(zv->zv_flags & ZVOL_DUMPIFIED))
1963 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1965 error = zvol_dump_fini(zv);
1966 zfs_range_unlock(rl);
1974 if (ddi_copyin((void *)arg, &df, sizeof (df), flag)) {
1975 error = SET_ERROR(EFAULT);
1980 * Apply Postel's Law to length-checking. If they overshoot,
1981 * just blank out until the end, if there's a need to blank
1984 if (df.df_start >= zv->zv_volsize)
1985 break; /* No need to do anything... */
1986 if (df.df_start + df.df_length > zv->zv_volsize)
1987 df.df_length = DMU_OBJECT_END;
1989 rl = zfs_range_lock(&zv->zv_znode, df.df_start, df.df_length,
1991 tx = dmu_tx_create(zv->zv_objset);
1992 dmu_tx_mark_netfree(tx);
1993 error = dmu_tx_assign(tx, TXG_WAIT);
1997 zvol_log_truncate(zv, tx, df.df_start,
1998 df.df_length, B_TRUE);
2000 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ,
2001 df.df_start, df.df_length);
2004 zfs_range_unlock(rl);
2008 * If the write-cache is disabled or 'sync' property
2009 * is set to 'always' then treat this as a synchronous
2010 * operation (i.e. commit to zil).
2012 if (!(zv->zv_flags & ZVOL_WCE) ||
2013 (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS))
2014 zil_commit(zv->zv_zilog, ZVOL_OBJ);
2017 * If the caller really wants synchronous writes, and
2018 * can't wait for them, don't return until the write
2021 if (df.df_flags & DF_WAIT_SYNC) {
2023 dmu_objset_pool(zv->zv_objset), 0);
2030 error = SET_ERROR(ENOTTY);
2034 mutex_exit(&spa_namespace_lock);
2042 return (zvol_minors != 0);
2048 VERIFY(ddi_soft_state_init(&zfsdev_state, sizeof (zfs_soft_state_t),
2050 ZFS_LOG(1, "ZVOL Initialized.");
2056 ddi_soft_state_fini(&zfsdev_state);
2057 ZFS_LOG(1, "ZVOL Deinitialized.");
2063 zfs_mvdev_dump_feature_check(void *arg, dmu_tx_t *tx)
2065 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
2067 if (spa_feature_is_active(spa, SPA_FEATURE_MULTI_VDEV_CRASH_DUMP))
2074 zfs_mvdev_dump_activate_feature_sync(void *arg, dmu_tx_t *tx)
2076 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
2078 spa_feature_incr(spa, SPA_FEATURE_MULTI_VDEV_CRASH_DUMP, tx);
2082 zvol_dump_init(zvol_state_t *zv, boolean_t resize)
2086 objset_t *os = zv->zv_objset;
2087 spa_t *spa = dmu_objset_spa(os);
2088 vdev_t *vd = spa->spa_root_vdev;
2089 nvlist_t *nv = NULL;
2090 uint64_t version = spa_version(spa);
2091 enum zio_checksum checksum;
2093 ASSERT(MUTEX_HELD(&spa_namespace_lock));
2094 ASSERT(vd->vdev_ops == &vdev_root_ops);
2096 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0,
2098 /* wait for dmu_free_long_range to actually free the blocks */
2099 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
2102 * If the pool on which the dump device is being initialized has more
2103 * than one child vdev, check that the MULTI_VDEV_CRASH_DUMP feature is
2104 * enabled. If so, bump that feature's counter to indicate that the
2105 * feature is active. We also check the vdev type to handle the
2107 * # zpool create test raidz disk1 disk2 disk3
2108 * Now have spa_root_vdev->vdev_children == 1 (the raidz vdev),
2109 * the raidz vdev itself has 3 children.
2111 if (vd->vdev_children > 1 || vd->vdev_ops == &vdev_raidz_ops) {
2112 if (!spa_feature_is_enabled(spa,
2113 SPA_FEATURE_MULTI_VDEV_CRASH_DUMP))
2114 return (SET_ERROR(ENOTSUP));
2115 (void) dsl_sync_task(spa_name(spa),
2116 zfs_mvdev_dump_feature_check,
2117 zfs_mvdev_dump_activate_feature_sync, NULL,
2118 2, ZFS_SPACE_CHECK_RESERVED);
2121 tx = dmu_tx_create(os);
2122 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
2123 dmu_tx_hold_bonus(tx, ZVOL_OBJ);
2124 error = dmu_tx_assign(tx, TXG_WAIT);
2131 * If MULTI_VDEV_CRASH_DUMP is active, use the NOPARITY checksum
2132 * function. Otherwise, use the old default -- OFF.
2134 checksum = spa_feature_is_active(spa,
2135 SPA_FEATURE_MULTI_VDEV_CRASH_DUMP) ? ZIO_CHECKSUM_NOPARITY :
2139 * If we are resizing the dump device then we only need to
2140 * update the refreservation to match the newly updated
2141 * zvolsize. Otherwise, we save off the original state of the
2142 * zvol so that we can restore them if the zvol is ever undumpified.
2145 error = zap_update(os, ZVOL_ZAP_OBJ,
2146 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
2147 &zv->zv_volsize, tx);
2149 uint64_t checksum, compress, refresrv, vbs, dedup;
2151 error = dsl_prop_get_integer(zv->zv_name,
2152 zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL);
2153 error = error ? error : dsl_prop_get_integer(zv->zv_name,
2154 zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL);
2155 error = error ? error : dsl_prop_get_integer(zv->zv_name,
2156 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL);
2157 error = error ? error : dsl_prop_get_integer(zv->zv_name,
2158 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL);
2159 if (version >= SPA_VERSION_DEDUP) {
2160 error = error ? error :
2161 dsl_prop_get_integer(zv->zv_name,
2162 zfs_prop_to_name(ZFS_PROP_DEDUP), &dedup, NULL);
2165 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
2166 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1,
2168 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
2169 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx);
2170 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
2171 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
2173 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
2174 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1,
2176 error = error ? error : dmu_object_set_blocksize(
2177 os, ZVOL_OBJ, SPA_MAXBLOCKSIZE, 0, tx);
2178 if (version >= SPA_VERSION_DEDUP) {
2179 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
2180 zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1,
2184 zv->zv_volblocksize = SPA_MAXBLOCKSIZE;
2189 * We only need update the zvol's property if we are initializing
2190 * the dump area for the first time.
2193 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2194 VERIFY(nvlist_add_uint64(nv,
2195 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0);
2196 VERIFY(nvlist_add_uint64(nv,
2197 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
2198 ZIO_COMPRESS_OFF) == 0);
2199 VERIFY(nvlist_add_uint64(nv,
2200 zfs_prop_to_name(ZFS_PROP_CHECKSUM),
2202 if (version >= SPA_VERSION_DEDUP) {
2203 VERIFY(nvlist_add_uint64(nv,
2204 zfs_prop_to_name(ZFS_PROP_DEDUP),
2205 ZIO_CHECKSUM_OFF) == 0);
2208 error = zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
2216 /* Allocate the space for the dump */
2217 error = zvol_prealloc(zv);
2222 zvol_dumpify(zvol_state_t *zv)
2225 uint64_t dumpsize = 0;
2227 objset_t *os = zv->zv_objset;
2229 if (zv->zv_flags & ZVOL_RDONLY)
2230 return (SET_ERROR(EROFS));
2232 if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE,
2233 8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) {
2234 boolean_t resize = (dumpsize > 0);
2236 if ((error = zvol_dump_init(zv, resize)) != 0) {
2237 (void) zvol_dump_fini(zv);
2243 * Build up our lba mapping.
2245 error = zvol_get_lbas(zv);
2247 (void) zvol_dump_fini(zv);
2251 tx = dmu_tx_create(os);
2252 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
2253 error = dmu_tx_assign(tx, TXG_WAIT);
2256 (void) zvol_dump_fini(zv);
2260 zv->zv_flags |= ZVOL_DUMPIFIED;
2261 error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1,
2262 &zv->zv_volsize, tx);
2266 (void) zvol_dump_fini(zv);
2270 txg_wait_synced(dmu_objset_pool(os), 0);
2275 zvol_dump_fini(zvol_state_t *zv)
2278 objset_t *os = zv->zv_objset;
2281 uint64_t checksum, compress, refresrv, vbs, dedup;
2282 uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset));
2285 * Attempt to restore the zvol back to its pre-dumpified state.
2286 * This is a best-effort attempt as it's possible that not all
2287 * of these properties were initialized during the dumpify process
2288 * (i.e. error during zvol_dump_init).
2291 tx = dmu_tx_create(os);
2292 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
2293 error = dmu_tx_assign(tx, TXG_WAIT);
2298 (void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx);
2301 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2302 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum);
2303 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2304 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress);
2305 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2306 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv);
2307 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2308 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs);
2310 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2311 (void) nvlist_add_uint64(nv,
2312 zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum);
2313 (void) nvlist_add_uint64(nv,
2314 zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress);
2315 (void) nvlist_add_uint64(nv,
2316 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv);
2317 if (version >= SPA_VERSION_DEDUP &&
2318 zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2319 zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, &dedup) == 0) {
2320 (void) nvlist_add_uint64(nv,
2321 zfs_prop_to_name(ZFS_PROP_DEDUP), dedup);
2323 (void) zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
2327 zvol_free_extents(zv);
2328 zv->zv_flags &= ~ZVOL_DUMPIFIED;
2329 (void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END);
2330 /* wait for dmu_free_long_range to actually free the blocks */
2331 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
2332 tx = dmu_tx_create(os);
2333 dmu_tx_hold_bonus(tx, ZVOL_OBJ);
2334 error = dmu_tx_assign(tx, TXG_WAIT);
2339 if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0)
2340 zv->zv_volblocksize = vbs;
2348 zvol_geom_run(zvol_state_t *zv)
2350 struct g_provider *pp;
2352 pp = zv->zv_provider;
2353 g_error_provider(pp, 0);
2355 kproc_kthread_add(zvol_geom_worker, zv, &zfsproc, NULL, 0, 0,
2356 "zfskern", "zvol %s", pp->name + sizeof(ZVOL_DRIVER));
2360 zvol_geom_destroy(zvol_state_t *zv)
2362 struct g_provider *pp;
2364 g_topology_assert();
2366 mtx_lock(&zv->zv_queue_mtx);
2368 wakeup_one(&zv->zv_queue);
2369 while (zv->zv_state != 2)
2370 msleep(&zv->zv_state, &zv->zv_queue_mtx, 0, "zvol:w", 0);
2371 mtx_destroy(&zv->zv_queue_mtx);
2373 pp = zv->zv_provider;
2374 zv->zv_provider = NULL;
2376 g_wither_geom(pp->geom, ENXIO);
2380 zvol_geom_access(struct g_provider *pp, int acr, int acw, int ace)
2382 int count, error, flags;
2384 g_topology_assert();
2387 * To make it easier we expect either open or close, but not both
2390 KASSERT((acr >= 0 && acw >= 0 && ace >= 0) ||
2391 (acr <= 0 && acw <= 0 && ace <= 0),
2392 ("Unsupported access request to %s (acr=%d, acw=%d, ace=%d).",
2393 pp->name, acr, acw, ace));
2395 if (pp->private == NULL) {
2396 if (acr <= 0 && acw <= 0 && ace <= 0)
2402 * We don't pass FEXCL flag to zvol_open()/zvol_close() if ace != 0,
2403 * because GEOM already handles that and handles it a bit differently.
2404 * GEOM allows for multiple read/exclusive consumers and ZFS allows
2405 * only one exclusive consumer, no matter if it is reader or writer.
2406 * I like better the way GEOM works so I'll leave it for GEOM to
2407 * decide what to do.
2410 count = acr + acw + ace;
2415 if (acr != 0 || ace != 0)
2420 g_topology_unlock();
2422 error = zvol_open(pp, flags, count);
2424 error = zvol_close(pp, flags, -count);
2430 zvol_geom_start(struct bio *bp)
2435 zv = bp->bio_to->private;
2437 switch (bp->bio_cmd) {
2439 if (!THREAD_CAN_SLEEP())
2441 zil_commit(zv->zv_zilog, ZVOL_OBJ);
2442 g_io_deliver(bp, 0);
2447 if (!THREAD_CAN_SLEEP())
2452 if (g_handleattr_int(bp, "GEOM::candelete", 1))
2456 g_io_deliver(bp, EOPNOTSUPP);
2462 mtx_lock(&zv->zv_queue_mtx);
2463 first = (bioq_first(&zv->zv_queue) == NULL);
2464 bioq_insert_tail(&zv->zv_queue, bp);
2465 mtx_unlock(&zv->zv_queue_mtx);
2467 wakeup_one(&zv->zv_queue);
2471 zvol_geom_worker(void *arg)
2476 thread_lock(curthread);
2477 sched_prio(curthread, PRIBIO);
2478 thread_unlock(curthread);
2482 mtx_lock(&zv->zv_queue_mtx);
2483 bp = bioq_takefirst(&zv->zv_queue);
2485 if (zv->zv_state == 1) {
2487 wakeup(&zv->zv_state);
2488 mtx_unlock(&zv->zv_queue_mtx);
2491 msleep(&zv->zv_queue, &zv->zv_queue_mtx, PRIBIO | PDROP,
2495 mtx_unlock(&zv->zv_queue_mtx);
2496 switch (bp->bio_cmd) {
2498 zil_commit(zv->zv_zilog, ZVOL_OBJ);
2499 g_io_deliver(bp, 0);
2509 extern boolean_t dataset_name_hidden(const char *name);
2512 zvol_create_snapshots(objset_t *os, const char *name)
2514 uint64_t cookie, obj;
2519 sname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2522 (void) dmu_objset_find(name, dmu_objset_prefetch, NULL,
2527 len = snprintf(sname, MAXPATHLEN, "%s@", name);
2528 if (len >= MAXPATHLEN) {
2529 dmu_objset_rele(os, FTAG);
2530 error = ENAMETOOLONG;
2534 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
2535 error = dmu_snapshot_list_next(os, MAXPATHLEN - len,
2536 sname + len, &obj, &cookie, NULL);
2537 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
2539 if (error == ENOENT)
2544 if ((error = zvol_create_minor(sname)) != 0) {
2545 printf("ZFS WARNING: Unable to create ZVOL %s (error=%d).\n",
2551 kmem_free(sname, MAXPATHLEN);
2556 zvol_create_minors(const char *name)
2563 if (dataset_name_hidden(name))
2566 if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) {
2567 printf("ZFS WARNING: Unable to put hold on %s (error=%d).\n",
2571 if (dmu_objset_type(os) == DMU_OST_ZVOL) {
2572 dsl_dataset_long_hold(os->os_dsl_dataset, FTAG);
2573 dsl_pool_rele(dmu_objset_pool(os), FTAG);
2574 if ((error = zvol_create_minor(name)) == 0)
2575 error = zvol_create_snapshots(os, name);
2577 printf("ZFS WARNING: Unable to create ZVOL %s (error=%d).\n",
2580 dsl_dataset_long_rele(os->os_dsl_dataset, FTAG);
2581 dsl_dataset_rele(os->os_dsl_dataset, FTAG);
2584 if (dmu_objset_type(os) != DMU_OST_ZFS) {
2585 dmu_objset_rele(os, FTAG);
2589 osname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2590 if (snprintf(osname, MAXPATHLEN, "%s/", name) >= MAXPATHLEN) {
2591 dmu_objset_rele(os, FTAG);
2592 kmem_free(osname, MAXPATHLEN);
2595 p = osname + strlen(osname);
2596 len = MAXPATHLEN - (p - osname);
2599 /* Prefetch the datasets. */
2601 while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0) {
2602 if (!dataset_name_hidden(osname))
2603 (void) dmu_objset_prefetch(osname, NULL);
2608 while (dmu_dir_list_next(os, MAXPATHLEN - (p - osname), p, NULL,
2610 dmu_objset_rele(os, FTAG);
2611 (void)zvol_create_minors(osname);
2612 if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) {
2613 printf("ZFS WARNING: Unable to put hold on %s (error=%d).\n",
2619 dmu_objset_rele(os, FTAG);
2620 kmem_free(osname, MAXPATHLEN);
2625 zvol_rename_minor(zvol_state_t *zv, const char *newname)
2628 struct g_provider *pp;
2631 ASSERT(MUTEX_HELD(&spa_namespace_lock));
2633 if (zv->zv_volmode == ZFS_VOLMODE_GEOM) {
2635 pp = zv->zv_provider;
2640 zv->zv_provider = NULL;
2641 g_wither_provider(pp, ENXIO);
2643 pp = g_new_providerf(gp, "%s/%s", ZVOL_DRIVER, newname);
2644 pp->flags |= G_PF_DIRECT_RECEIVE | G_PF_DIRECT_SEND;
2645 pp->sectorsize = DEV_BSIZE;
2646 pp->mediasize = zv->zv_volsize;
2648 zv->zv_provider = pp;
2649 g_error_provider(pp, 0);
2650 g_topology_unlock();
2651 } else if (zv->zv_volmode == ZFS_VOLMODE_DEV) {
2653 ASSERT(dev != NULL);
2657 if (make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK,
2658 &dev, &zvol_cdevsw, NULL, UID_ROOT, GID_OPERATOR,
2659 0640, "%s/%s", ZVOL_DRIVER, newname) == 0) {
2661 dev->si_iosize_max = MAXPHYS;
2665 strlcpy(zv->zv_name, newname, sizeof(zv->zv_name));
2669 zvol_rename_minors(const char *oldname, const char *newname)
2671 char name[MAXPATHLEN];
2672 struct g_provider *pp;
2674 size_t oldnamelen, newnamelen;
2678 oldnamelen = strlen(oldname);
2679 newnamelen = strlen(newname);
2682 mutex_enter(&spa_namespace_lock);
2684 LIST_FOREACH(zv, &all_zvols, zv_links) {
2685 if (strcmp(zv->zv_name, oldname) == 0) {
2686 zvol_rename_minor(zv, newname);
2687 } else if (strncmp(zv->zv_name, oldname, oldnamelen) == 0 &&
2688 (zv->zv_name[oldnamelen] == '/' ||
2689 zv->zv_name[oldnamelen] == '@')) {
2690 snprintf(name, sizeof(name), "%s%c%s", newname,
2691 zv->zv_name[oldnamelen],
2692 zv->zv_name + oldnamelen + 1);
2693 zvol_rename_minor(zv, name);
2697 mutex_exit(&spa_namespace_lock);
2702 zvol_d_open(struct cdev *dev, int flags, int fmt, struct thread *td)
2707 mutex_enter(&spa_namespace_lock);
2710 mutex_exit(&spa_namespace_lock);
2711 return(ENXIO); /* zvol_create_minor() not done yet */
2714 if (zv->zv_total_opens == 0)
2715 err = zvol_first_open(zv);
2717 mutex_exit(&spa_namespace_lock);
2720 if ((flags & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
2721 err = SET_ERROR(EROFS);
2724 if (zv->zv_flags & ZVOL_EXCL) {
2725 err = SET_ERROR(EBUSY);
2729 if (flags & FEXCL) {
2730 if (zv->zv_total_opens != 0) {
2731 err = SET_ERROR(EBUSY);
2734 zv->zv_flags |= ZVOL_EXCL;
2738 zv->zv_total_opens++;
2739 mutex_exit(&spa_namespace_lock);
2742 if (zv->zv_total_opens == 0)
2743 zvol_last_close(zv);
2744 mutex_exit(&spa_namespace_lock);
2749 zvol_d_close(struct cdev *dev, int flags, int fmt, struct thread *td)
2754 mutex_enter(&spa_namespace_lock);
2757 mutex_exit(&spa_namespace_lock);
2761 if (zv->zv_flags & ZVOL_EXCL) {
2762 ASSERT(zv->zv_total_opens == 1);
2763 zv->zv_flags &= ~ZVOL_EXCL;
2767 * If the open count is zero, this is a spurious close.
2768 * That indicates a bug in the kernel / DDI framework.
2770 ASSERT(zv->zv_total_opens != 0);
2773 * You may get multiple opens, but only one close.
2775 zv->zv_total_opens--;
2777 if (zv->zv_total_opens == 0)
2778 zvol_last_close(zv);
2780 mutex_exit(&spa_namespace_lock);
2785 zvol_d_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
2789 off_t offset, length, chunk;
2796 KASSERT(zv->zv_total_opens > 0,
2797 ("Device with zero access count in zvol_d_ioctl"));
2799 i = IOCPARM_LEN(cmd);
2801 case DIOCGSECTORSIZE:
2802 *(u_int *)data = DEV_BSIZE;
2804 case DIOCGMEDIASIZE:
2805 *(off_t *)data = zv->zv_volsize;
2808 zil_commit(zv->zv_zilog, ZVOL_OBJ);
2811 offset = ((off_t *)data)[0];
2812 length = ((off_t *)data)[1];
2813 if ((offset % DEV_BSIZE) != 0 || (length % DEV_BSIZE) != 0 ||
2814 offset < 0 || offset >= zv->zv_volsize ||
2816 printf("%s: offset=%jd length=%jd\n", __func__, offset,
2822 rl = zfs_range_lock(&zv->zv_znode, offset, length, RL_WRITER);
2823 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
2824 error = dmu_tx_assign(tx, TXG_WAIT);
2828 zvol_log_truncate(zv, tx, offset, length, B_TRUE);
2830 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ,
2833 zfs_range_unlock(rl);
2834 if (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)
2835 zil_commit(zv->zv_zilog, ZVOL_OBJ);
2837 case DIOCGSTRIPESIZE:
2838 *(off_t *)data = zv->zv_volblocksize;
2840 case DIOCGSTRIPEOFFSET: