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.
26 * Copyright (c) 2013 by Delphix. All rights reserved.
27 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
29 * Portions Copyright 2010 Robert Milkowski
31 * Copyright 2011 Nexenta Systems, 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_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 VERIFY3U(ma->ma_blks, ==, zb->zb_blkid);
321 /* Abort immediately if we have encountered gang blocks */
323 return (SET_ERROR(EFRAGS));
326 * See if the block is at the end of the previous extent.
328 ze = list_tail(&ma->ma_zv->zv_extents);
330 DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) &&
331 DVA_GET_OFFSET(BP_IDENTITY(bp)) ==
332 DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) {
337 dprintf_bp(bp, "%s", "next blkptr:");
339 /* start a new extent */
340 ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP);
341 ze->ze_dva = bp->blk_dva[0]; /* structure assignment */
343 list_insert_tail(&ma->ma_zv->zv_extents, ze);
348 zvol_free_extents(zvol_state_t *zv)
352 while (ze = list_head(&zv->zv_extents)) {
353 list_remove(&zv->zv_extents, ze);
354 kmem_free(ze, sizeof (zvol_extent_t));
359 zvol_get_lbas(zvol_state_t *zv)
361 objset_t *os = zv->zv_objset;
367 zvol_free_extents(zv);
369 /* commit any in-flight changes before traversing the dataset */
370 txg_wait_synced(dmu_objset_pool(os), 0);
371 err = traverse_dataset(dmu_objset_ds(os), 0,
372 TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma);
373 if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) {
374 zvol_free_extents(zv);
375 return (err ? err : EIO);
383 zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
385 zfs_creat_t *zct = arg;
386 nvlist_t *nvprops = zct->zct_props;
388 uint64_t volblocksize, volsize;
390 VERIFY(nvlist_lookup_uint64(nvprops,
391 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
392 if (nvlist_lookup_uint64(nvprops,
393 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
394 volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
397 * These properties must be removed from the list so the generic
398 * property setting step won't apply to them.
400 VERIFY(nvlist_remove_all(nvprops,
401 zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
402 (void) nvlist_remove_all(nvprops,
403 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
405 error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
409 error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
413 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
418 * Replay a TX_TRUNCATE ZIL transaction if asked. TX_TRUNCATE is how we
419 * implement DKIOCFREE/free-long-range.
422 zvol_replay_truncate(zvol_state_t *zv, lr_truncate_t *lr, boolean_t byteswap)
424 uint64_t offset, length;
427 byteswap_uint64_array(lr, sizeof (*lr));
429 offset = lr->lr_offset;
430 length = lr->lr_length;
432 return (dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, offset, length));
436 * Replay a TX_WRITE ZIL transaction that didn't get committed
437 * after a system failure
440 zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
442 objset_t *os = zv->zv_objset;
443 char *data = (char *)(lr + 1); /* data follows lr_write_t */
444 uint64_t offset, length;
449 byteswap_uint64_array(lr, sizeof (*lr));
451 offset = lr->lr_offset;
452 length = lr->lr_length;
454 /* If it's a dmu_sync() block, write the whole block */
455 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
456 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
457 if (length < blocksize) {
458 offset -= offset % blocksize;
463 tx = dmu_tx_create(os);
464 dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length);
465 error = dmu_tx_assign(tx, TXG_WAIT);
469 dmu_write(os, ZVOL_OBJ, offset, length, data, tx);
478 zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
480 return (SET_ERROR(ENOTSUP));
484 * Callback vectors for replaying records.
485 * Only TX_WRITE and TX_TRUNCATE are needed for zvol.
487 zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = {
488 zvol_replay_err, /* 0 no such transaction type */
489 zvol_replay_err, /* TX_CREATE */
490 zvol_replay_err, /* TX_MKDIR */
491 zvol_replay_err, /* TX_MKXATTR */
492 zvol_replay_err, /* TX_SYMLINK */
493 zvol_replay_err, /* TX_REMOVE */
494 zvol_replay_err, /* TX_RMDIR */
495 zvol_replay_err, /* TX_LINK */
496 zvol_replay_err, /* TX_RENAME */
497 zvol_replay_write, /* TX_WRITE */
498 zvol_replay_truncate, /* TX_TRUNCATE */
499 zvol_replay_err, /* TX_SETATTR */
500 zvol_replay_err, /* TX_ACL */
501 zvol_replay_err, /* TX_CREATE_ACL */
502 zvol_replay_err, /* TX_CREATE_ATTR */
503 zvol_replay_err, /* TX_CREATE_ACL_ATTR */
504 zvol_replay_err, /* TX_MKDIR_ACL */
505 zvol_replay_err, /* TX_MKDIR_ATTR */
506 zvol_replay_err, /* TX_MKDIR_ACL_ATTR */
507 zvol_replay_err, /* TX_WRITE2 */
512 zvol_name2minor(const char *name, minor_t *minor)
516 mutex_enter(&spa_namespace_lock);
517 zv = zvol_minor_lookup(name);
519 *minor = zv->zv_minor;
520 mutex_exit(&spa_namespace_lock);
521 return (zv ? 0 : -1);
526 * Create a minor node (plus a whole lot more) for the specified volume.
529 zvol_create_minor(const char *name)
531 zfs_soft_state_t *zs;
535 struct g_provider *pp;
537 dmu_object_info_t doi;
538 uint64_t volsize, mode;
541 ZFS_LOG(1, "Creating ZVOL %s...", name);
543 mutex_enter(&spa_namespace_lock);
545 if (zvol_minor_lookup(name) != NULL) {
546 mutex_exit(&spa_namespace_lock);
547 return (SET_ERROR(EEXIST));
550 /* lie and say we're read-only */
551 error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, FTAG, &os);
554 mutex_exit(&spa_namespace_lock);
559 if ((minor = zfsdev_minor_alloc()) == 0) {
560 dmu_objset_disown(os, FTAG);
561 mutex_exit(&spa_namespace_lock);
562 return (SET_ERROR(ENXIO));
565 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS) {
566 dmu_objset_disown(os, FTAG);
567 mutex_exit(&spa_namespace_lock);
568 return (SET_ERROR(EAGAIN));
570 (void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME,
573 (void) snprintf(chrbuf, sizeof (chrbuf), "%u,raw", minor);
575 if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR,
576 minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
577 ddi_soft_state_free(zfsdev_state, minor);
578 dmu_objset_disown(os, FTAG);
579 mutex_exit(&spa_namespace_lock);
580 return (SET_ERROR(EAGAIN));
583 (void) snprintf(blkbuf, sizeof (blkbuf), "%u", minor);
585 if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK,
586 minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
587 ddi_remove_minor_node(zfs_dip, chrbuf);
588 ddi_soft_state_free(zfsdev_state, minor);
589 dmu_objset_disown(os, FTAG);
590 mutex_exit(&spa_namespace_lock);
591 return (SET_ERROR(EAGAIN));
594 zs = ddi_get_soft_state(zfsdev_state, minor);
595 zs->zss_type = ZSST_ZVOL;
596 zv = zs->zss_data = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP);
599 zv = kmem_zalloc(sizeof(*zv), KM_SLEEP);
601 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
603 kmem_free(zv, sizeof(*zv));
604 dmu_objset_disown(os, zvol_tag);
605 mutex_exit(&spa_namespace_lock);
608 error = dsl_prop_get_integer(name,
609 zfs_prop_to_name(ZFS_PROP_VOLMODE), &mode, NULL);
610 if (error != 0 || mode == ZFS_VOLMODE_DEFAULT)
614 zv->zv_volsize = volsize;
615 zv->zv_volmode = mode;
616 if (zv->zv_volmode == ZFS_VOLMODE_GEOM) {
618 gp = g_new_geomf(&zfs_zvol_class, "zfs::zvol::%s", name);
619 gp->start = zvol_geom_start;
620 gp->access = zvol_geom_access;
621 pp = g_new_providerf(gp, "%s/%s", ZVOL_DRIVER, name);
622 pp->flags |= G_PF_DIRECT_RECEIVE | G_PF_DIRECT_SEND;
623 pp->sectorsize = DEV_BSIZE;
624 pp->mediasize = zv->zv_volsize;
627 zv->zv_provider = pp;
628 bioq_init(&zv->zv_queue);
629 mtx_init(&zv->zv_queue_mtx, "zvol", NULL, MTX_DEF);
630 } else if (zv->zv_volmode == ZFS_VOLMODE_DEV) {
631 if (make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK,
632 &dev, &zvol_cdevsw, NULL, UID_ROOT, GID_OPERATOR,
633 0640, "%s/%s", ZVOL_DRIVER, name) != 0) {
634 kmem_free(zv, sizeof(*zv));
635 dmu_objset_disown(os, FTAG);
636 mutex_exit(&spa_namespace_lock);
637 return (SET_ERROR(ENXIO));
640 dev->si_iosize_max = MAXPHYS;
643 LIST_INSERT_HEAD(&all_zvols, zv, zv_links);
646 (void) strlcpy(zv->zv_name, name, MAXPATHLEN);
647 zv->zv_min_bs = DEV_BSHIFT;
649 if (dmu_objset_is_snapshot(os) || !spa_writeable(dmu_objset_spa(os)))
650 zv->zv_flags |= ZVOL_RDONLY;
651 mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
652 avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
653 sizeof (rl_t), offsetof(rl_t, r_node));
654 list_create(&zv->zv_extents, sizeof (zvol_extent_t),
655 offsetof(zvol_extent_t, ze_node));
656 /* get and cache the blocksize */
657 error = dmu_object_info(os, ZVOL_OBJ, &doi);
659 zv->zv_volblocksize = doi.doi_data_block_size;
661 if (spa_writeable(dmu_objset_spa(os))) {
662 if (zil_replay_disable)
663 zil_destroy(dmu_objset_zil(os), B_FALSE);
665 zil_replay(os, zv, zvol_replay_vector);
667 dmu_objset_disown(os, FTAG);
668 zv->zv_objset = NULL;
672 mutex_exit(&spa_namespace_lock);
675 if (zv->zv_volmode == ZFS_VOLMODE_GEOM) {
682 ZFS_LOG(1, "ZVOL %s created.", name);
688 * Remove minor node for the specified volume.
691 zvol_remove_zv(zvol_state_t *zv)
694 minor_t minor = zv->zv_minor;
697 ASSERT(MUTEX_HELD(&spa_namespace_lock));
698 if (zv->zv_total_opens != 0)
699 return (SET_ERROR(EBUSY));
701 ZFS_LOG(1, "ZVOL %s destroyed.", zv->zv_name);
704 (void) snprintf(nmbuf, sizeof (nmbuf), "%u,raw", minor);
705 ddi_remove_minor_node(zfs_dip, nmbuf);
707 LIST_REMOVE(zv, zv_links);
708 if (zv->zv_volmode == ZFS_VOLMODE_GEOM) {
710 zvol_geom_destroy(zv);
712 } else if (zv->zv_volmode == ZFS_VOLMODE_DEV)
713 destroy_dev(zv->zv_dev);
716 avl_destroy(&zv->zv_znode.z_range_avl);
717 mutex_destroy(&zv->zv_znode.z_range_lock);
719 kmem_free(zv, sizeof(*zv));
726 zvol_remove_minor(const char *name)
731 mutex_enter(&spa_namespace_lock);
732 if ((zv = zvol_minor_lookup(name)) == NULL) {
733 mutex_exit(&spa_namespace_lock);
734 return (SET_ERROR(ENXIO));
736 rc = zvol_remove_zv(zv);
737 mutex_exit(&spa_namespace_lock);
742 zvol_first_open(zvol_state_t *zv)
749 /* lie and say we're read-only */
750 error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, B_TRUE,
755 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
758 dmu_objset_disown(os, zvol_tag);
762 error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf);
764 dmu_objset_disown(os, zvol_tag);
767 zv->zv_volsize = volsize;
768 zv->zv_zilog = zil_open(os, zvol_get_data);
769 zvol_size_changed(zv);
771 VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &readonly,
773 if (readonly || dmu_objset_is_snapshot(os) ||
774 !spa_writeable(dmu_objset_spa(os)))
775 zv->zv_flags |= ZVOL_RDONLY;
777 zv->zv_flags &= ~ZVOL_RDONLY;
782 zvol_last_close(zvol_state_t *zv)
784 zil_close(zv->zv_zilog);
787 dmu_buf_rele(zv->zv_dbuf, zvol_tag);
793 if (dsl_dataset_is_dirty(dmu_objset_ds(zv->zv_objset)) &&
794 !(zv->zv_flags & ZVOL_RDONLY))
795 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
796 dmu_objset_evict_dbufs(zv->zv_objset);
798 dmu_objset_disown(zv->zv_objset, zvol_tag);
799 zv->zv_objset = NULL;
804 zvol_prealloc(zvol_state_t *zv)
806 objset_t *os = zv->zv_objset;
808 uint64_t refd, avail, usedobjs, availobjs;
809 uint64_t resid = zv->zv_volsize;
812 /* Check the space usage before attempting to allocate the space */
813 dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs);
814 if (avail < zv->zv_volsize)
815 return (SET_ERROR(ENOSPC));
817 /* Free old extents if they exist */
818 zvol_free_extents(zv);
822 uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE);
824 tx = dmu_tx_create(os);
825 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
826 error = dmu_tx_assign(tx, TXG_WAIT);
829 (void) dmu_free_long_range(os, ZVOL_OBJ, 0, off);
832 dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx);
837 txg_wait_synced(dmu_objset_pool(os), 0);
844 zvol_update_volsize(objset_t *os, uint64_t volsize)
849 ASSERT(MUTEX_HELD(&spa_namespace_lock));
851 tx = dmu_tx_create(os);
852 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
853 error = dmu_tx_assign(tx, TXG_WAIT);
859 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
864 error = dmu_free_long_range(os,
865 ZVOL_OBJ, volsize, DMU_OBJECT_END);
870 zvol_remove_minors(const char *name)
872 zvol_state_t *zv, *tzv;
875 namelen = strlen(name);
878 mutex_enter(&spa_namespace_lock);
880 LIST_FOREACH_SAFE(zv, &all_zvols, zv_links, tzv) {
881 if (strcmp(zv->zv_name, name) == 0 ||
882 (strncmp(zv->zv_name, name, namelen) == 0 &&
883 zv->zv_name[namelen] == '/')) {
884 (void) zvol_remove_zv(zv);
888 mutex_exit(&spa_namespace_lock);
893 zvol_set_volsize(const char *name, major_t maj, uint64_t volsize)
895 zvol_state_t *zv = NULL;
898 dmu_object_info_t doi;
899 uint64_t old_volsize = 0ULL;
902 mutex_enter(&spa_namespace_lock);
903 zv = zvol_minor_lookup(name);
904 if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) {
905 mutex_exit(&spa_namespace_lock);
909 if ((error = dmu_object_info(os, ZVOL_OBJ, &doi)) != 0 ||
910 (error = zvol_check_volsize(volsize,
911 doi.doi_data_block_size)) != 0)
914 VERIFY(dsl_prop_get_integer(name, "readonly", &readonly,
921 error = zvol_update_volsize(os, volsize);
923 * Reinitialize the dump area to the new size. If we
924 * failed to resize the dump area then restore it back to
927 if (zv && error == 0) {
929 if (zv->zv_flags & ZVOL_DUMPIFIED) {
930 old_volsize = zv->zv_volsize;
931 zv->zv_volsize = volsize;
932 if ((error = zvol_dumpify(zv)) != 0 ||
933 (error = dumpvp_resize()) != 0) {
934 (void) zvol_update_volsize(os, old_volsize);
935 zv->zv_volsize = old_volsize;
936 error = zvol_dumpify(zv);
939 #endif /* ZVOL_DUMP */
941 zv->zv_volsize = volsize;
942 zvol_size_changed(zv);
948 * Generate a LUN expansion event.
950 if (zv && error == 0) {
953 char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
955 (void) snprintf(physpath, MAXPATHLEN, "%s%u", ZVOL_PSEUDO_DEV,
958 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
959 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
961 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
962 ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
965 kmem_free(physpath, MAXPATHLEN);
970 dmu_objset_rele(os, FTAG);
972 mutex_exit(&spa_namespace_lock);
979 zvol_open(struct g_provider *pp, int flag, int count)
983 boolean_t locked = B_FALSE;
986 * Protect against recursively entering spa_namespace_lock
987 * when spa_open() is used for a pool on a (local) ZVOL(s).
988 * This is needed since we replaced upstream zfsdev_state_lock
989 * with spa_namespace_lock in the ZVOL code.
990 * We are using the same trick as spa_open().
991 * Note that calls in zvol_first_open which need to resolve
992 * pool name to a spa object will enter spa_open()
993 * recursively, but that function already has all the
994 * necessary protection.
996 if (!MUTEX_HELD(&spa_namespace_lock)) {
997 mutex_enter(&spa_namespace_lock);
1004 mutex_exit(&spa_namespace_lock);
1005 return (SET_ERROR(ENXIO));
1008 if (zv->zv_total_opens == 0) {
1009 err = zvol_first_open(zv);
1012 mutex_exit(&spa_namespace_lock);
1015 pp->mediasize = zv->zv_volsize;
1016 pp->stripeoffset = 0;
1017 pp->stripesize = zv->zv_volblocksize;
1019 if ((flag & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
1020 err = SET_ERROR(EROFS);
1023 if (zv->zv_flags & ZVOL_EXCL) {
1024 err = SET_ERROR(EBUSY);
1029 if (zv->zv_total_opens != 0) {
1030 err = SET_ERROR(EBUSY);
1033 zv->zv_flags |= ZVOL_EXCL;
1037 zv->zv_total_opens += count;
1039 mutex_exit(&spa_namespace_lock);
1043 if (zv->zv_total_opens == 0)
1044 zvol_last_close(zv);
1046 mutex_exit(&spa_namespace_lock);
1052 zvol_close(struct g_provider *pp, int flag, int count)
1056 boolean_t locked = B_FALSE;
1058 /* See comment in zvol_open(). */
1059 if (!MUTEX_HELD(&spa_namespace_lock)) {
1060 mutex_enter(&spa_namespace_lock);
1067 mutex_exit(&spa_namespace_lock);
1068 return (SET_ERROR(ENXIO));
1071 if (zv->zv_flags & ZVOL_EXCL) {
1072 ASSERT(zv->zv_total_opens == 1);
1073 zv->zv_flags &= ~ZVOL_EXCL;
1077 * If the open count is zero, this is a spurious close.
1078 * That indicates a bug in the kernel / DDI framework.
1080 ASSERT(zv->zv_total_opens != 0);
1083 * You may get multiple opens, but only one close.
1085 zv->zv_total_opens -= count;
1087 if (zv->zv_total_opens == 0)
1088 zvol_last_close(zv);
1091 mutex_exit(&spa_namespace_lock);
1096 zvol_get_done(zgd_t *zgd, int error)
1099 dmu_buf_rele(zgd->zgd_db, zgd);
1101 zfs_range_unlock(zgd->zgd_rl);
1103 if (error == 0 && zgd->zgd_bp)
1104 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1106 kmem_free(zgd, sizeof (zgd_t));
1110 * Get data to generate a TX_WRITE intent log record.
1113 zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1115 zvol_state_t *zv = arg;
1116 objset_t *os = zv->zv_objset;
1117 uint64_t object = ZVOL_OBJ;
1118 uint64_t offset = lr->lr_offset;
1119 uint64_t size = lr->lr_length; /* length of user data */
1120 blkptr_t *bp = &lr->lr_blkptr;
1125 ASSERT(zio != NULL);
1128 zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1129 zgd->zgd_zilog = zv->zv_zilog;
1130 zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
1133 * Write records come in two flavors: immediate and indirect.
1134 * For small writes it's cheaper to store the data with the
1135 * log record (immediate); for large writes it's cheaper to
1136 * sync the data and get a pointer to it (indirect) so that
1137 * we don't have to write the data twice.
1139 if (buf != NULL) { /* immediate write */
1140 error = dmu_read(os, object, offset, size, buf,
1141 DMU_READ_NO_PREFETCH);
1143 size = zv->zv_volblocksize;
1144 offset = P2ALIGN(offset, size);
1145 error = dmu_buf_hold(os, object, offset, zgd, &db,
1146 DMU_READ_NO_PREFETCH);
1148 blkptr_t *obp = dmu_buf_get_blkptr(db);
1150 ASSERT(BP_IS_HOLE(bp));
1157 ASSERT(db->db_offset == offset);
1158 ASSERT(db->db_size == size);
1160 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1161 zvol_get_done, zgd);
1168 zvol_get_done(zgd, error);
1174 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
1176 * We store data in the log buffers if it's small enough.
1177 * Otherwise we will later flush the data out via dmu_sync().
1179 ssize_t zvol_immediate_write_sz = 32768;
1182 zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid,
1185 uint32_t blocksize = zv->zv_volblocksize;
1186 zilog_t *zilog = zv->zv_zilog;
1188 ssize_t immediate_write_sz;
1190 if (zil_replaying(zilog, tx))
1193 immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
1194 ? 0 : zvol_immediate_write_sz;
1196 slogging = spa_has_slogs(zilog->zl_spa) &&
1197 (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
1203 itx_wr_state_t write_state;
1206 * Unlike zfs_log_write() we can be called with
1207 * upto DMU_MAX_ACCESS/2 (5MB) writes.
1209 if (blocksize > immediate_write_sz && !slogging &&
1210 resid >= blocksize && off % blocksize == 0) {
1211 write_state = WR_INDIRECT; /* uses dmu_sync */
1214 write_state = WR_COPIED;
1215 len = MIN(ZIL_MAX_LOG_DATA, resid);
1217 write_state = WR_NEED_COPY;
1218 len = MIN(ZIL_MAX_LOG_DATA, resid);
1221 itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
1222 (write_state == WR_COPIED ? len : 0));
1223 lr = (lr_write_t *)&itx->itx_lr;
1224 if (write_state == WR_COPIED && dmu_read(zv->zv_objset,
1225 ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
1226 zil_itx_destroy(itx);
1227 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1228 lr = (lr_write_t *)&itx->itx_lr;
1229 write_state = WR_NEED_COPY;
1232 itx->itx_wr_state = write_state;
1233 if (write_state == WR_NEED_COPY)
1234 itx->itx_sod += len;
1235 lr->lr_foid = ZVOL_OBJ;
1236 lr->lr_offset = off;
1237 lr->lr_length = len;
1239 BP_ZERO(&lr->lr_blkptr);
1241 itx->itx_private = zv;
1242 itx->itx_sync = sync;
1244 zil_itx_assign(zilog, itx, tx);
1253 zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t origoffset,
1254 uint64_t size, boolean_t doread, boolean_t isdump)
1260 if (vd->vdev_ops == &vdev_mirror_ops ||
1261 vd->vdev_ops == &vdev_replacing_ops ||
1262 vd->vdev_ops == &vdev_spare_ops) {
1263 for (c = 0; c < vd->vdev_children; c++) {
1264 int err = zvol_dumpio_vdev(vd->vdev_child[c],
1265 addr, offset, origoffset, size, doread, isdump);
1268 } else if (doread) {
1274 if (!vd->vdev_ops->vdev_op_leaf && vd->vdev_ops != &vdev_raidz_ops)
1275 return (numerrors < vd->vdev_children ? 0 : EIO);
1277 if (doread && !vdev_readable(vd))
1278 return (SET_ERROR(EIO));
1279 else if (!doread && !vdev_writeable(vd))
1280 return (SET_ERROR(EIO));
1282 if (vd->vdev_ops == &vdev_raidz_ops) {
1283 return (vdev_raidz_physio(vd,
1284 addr, size, offset, origoffset, doread, isdump));
1287 offset += VDEV_LABEL_START_SIZE;
1289 if (ddi_in_panic() || isdump) {
1292 return (SET_ERROR(EIO));
1294 ASSERT3P(dvd, !=, NULL);
1295 return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset),
1299 ASSERT3P(dvd, !=, NULL);
1300 return (vdev_disk_ldi_physio(dvd->vd_lh, addr, size,
1301 offset, doread ? B_READ : B_WRITE));
1306 zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size,
1307 boolean_t doread, boolean_t isdump)
1312 spa_t *spa = dmu_objset_spa(zv->zv_objset);
1314 /* Must be sector aligned, and not stradle a block boundary. */
1315 if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) ||
1316 P2BOUNDARY(offset, size, zv->zv_volblocksize)) {
1317 return (SET_ERROR(EINVAL));
1319 ASSERT(size <= zv->zv_volblocksize);
1321 /* Locate the extent this belongs to */
1322 ze = list_head(&zv->zv_extents);
1323 while (offset >= ze->ze_nblks * zv->zv_volblocksize) {
1324 offset -= ze->ze_nblks * zv->zv_volblocksize;
1325 ze = list_next(&zv->zv_extents, ze);
1329 return (SET_ERROR(EINVAL));
1331 if (!ddi_in_panic())
1332 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
1334 vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva));
1335 offset += DVA_GET_OFFSET(&ze->ze_dva);
1336 error = zvol_dumpio_vdev(vd, addr, offset, DVA_GET_OFFSET(&ze->ze_dva),
1337 size, doread, isdump);
1339 if (!ddi_in_panic())
1340 spa_config_exit(spa, SCL_STATE, FTAG);
1347 zvol_strategy(struct bio *bp)
1350 uint64_t off, volsize;
1356 boolean_t doread = 0;
1357 boolean_t is_dumpified;
1361 zv = bp->bio_to->private;
1363 zv = bp->bio_dev->si_drv2;
1370 if (bp->bio_cmd != BIO_READ && (zv->zv_flags & ZVOL_RDONLY)) {
1375 switch (bp->bio_cmd) {
1388 off = bp->bio_offset;
1389 volsize = zv->zv_volsize;
1394 addr = bp->bio_data;
1395 resid = bp->bio_length;
1397 if (resid > 0 && (off < 0 || off >= volsize)) {
1403 is_dumpified = zv->zv_flags & ZVOL_DUMPIFIED;
1405 is_dumpified = B_FALSE;
1407 sync = !doread && !is_dumpified &&
1408 zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS;
1411 * There must be no buffer changes when doing a dmu_sync() because
1412 * we can't change the data whilst calculating the checksum.
1414 rl = zfs_range_lock(&zv->zv_znode, off, resid,
1415 doread ? RL_READER : RL_WRITER);
1417 if (bp->bio_cmd == BIO_DELETE) {
1418 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
1419 error = dmu_tx_assign(tx, TXG_WAIT);
1423 zvol_log_truncate(zv, tx, off, resid, B_TRUE);
1425 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ,
1432 while (resid != 0 && off < volsize) {
1433 size_t size = MIN(resid, zvol_maxphys);
1436 size = MIN(size, P2END(off, zv->zv_volblocksize) - off);
1437 error = zvol_dumpio(zv, addr, off, size,
1439 } else if (doread) {
1443 error = dmu_read(os, ZVOL_OBJ, off, size, addr,
1446 dmu_tx_t *tx = dmu_tx_create(os);
1447 dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
1448 error = dmu_tx_assign(tx, TXG_WAIT);
1452 dmu_write(os, ZVOL_OBJ, off, size, addr, tx);
1453 zvol_log_write(zv, tx, off, size, sync);
1458 /* convert checksum errors into IO errors */
1459 if (error == ECKSUM)
1460 error = SET_ERROR(EIO);
1468 zfs_range_unlock(rl);
1470 bp->bio_completed = bp->bio_length - resid;
1471 if (bp->bio_completed < bp->bio_length && off > volsize)
1476 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1480 g_io_deliver(bp, error);
1482 biofinish(bp, NULL, error);
1487 * Set the buffer count to the zvol maximum transfer.
1488 * Using our own routine instead of the default minphys()
1489 * means that for larger writes we write bigger buffers on X86
1490 * (128K instead of 56K) and flush the disk write cache less often
1491 * (every zvol_maxphys - currently 1MB) instead of minphys (currently
1492 * 56K on X86 and 128K on sparc).
1495 zvol_minphys(struct buf *bp)
1497 if (bp->b_bcount > zvol_maxphys)
1498 bp->b_bcount = zvol_maxphys;
1502 zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks)
1504 minor_t minor = getminor(dev);
1511 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1513 return (SET_ERROR(ENXIO));
1515 if ((zv->zv_flags & ZVOL_DUMPIFIED) == 0)
1516 return (SET_ERROR(EINVAL));
1518 boff = ldbtob(blkno);
1519 resid = ldbtob(nblocks);
1521 VERIFY3U(boff + resid, <=, zv->zv_volsize);
1524 size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff);
1525 error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE);
1538 zvol_read(dev_t dev, uio_t *uio, cred_t *cr)
1540 minor_t minor = getminor(dev);
1543 zvol_read(struct cdev *dev, struct uio *uio, int ioflag)
1552 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1554 return (SET_ERROR(ENXIO));
1559 volsize = zv->zv_volsize;
1560 if (uio->uio_resid > 0 &&
1561 (uio->uio_loffset < 0 || uio->uio_loffset > volsize))
1562 return (SET_ERROR(EIO));
1565 if (zv->zv_flags & ZVOL_DUMPIFIED) {
1566 error = physio(zvol_strategy, NULL, dev, B_READ,
1572 rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1574 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1575 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1577 /* don't read past the end */
1578 if (bytes > volsize - uio->uio_loffset)
1579 bytes = volsize - uio->uio_loffset;
1581 error = dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes);
1583 /* convert checksum errors into IO errors */
1584 if (error == ECKSUM)
1585 error = SET_ERROR(EIO);
1589 zfs_range_unlock(rl);
1596 zvol_write(dev_t dev, uio_t *uio, cred_t *cr)
1598 minor_t minor = getminor(dev);
1601 zvol_write(struct cdev *dev, struct uio *uio, int ioflag)
1611 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1613 return (SET_ERROR(ENXIO));
1618 volsize = zv->zv_volsize;
1619 if (uio->uio_resid > 0 &&
1620 (uio->uio_loffset < 0 || uio->uio_loffset > volsize))
1621 return (SET_ERROR(EIO));
1624 if (zv->zv_flags & ZVOL_DUMPIFIED) {
1625 error = physio(zvol_strategy, NULL, dev, B_WRITE,
1631 sync = !(zv->zv_flags & ZVOL_WCE) ||
1632 (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS);
1634 rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1636 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1637 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1638 uint64_t off = uio->uio_loffset;
1639 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
1641 if (bytes > volsize - off) /* don't write past the end */
1642 bytes = volsize - off;
1644 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
1645 error = dmu_tx_assign(tx, TXG_WAIT);
1650 error = dmu_write_uio_dbuf(zv->zv_dbuf, uio, bytes, tx);
1652 zvol_log_write(zv, tx, off, bytes, sync);
1658 zfs_range_unlock(rl);
1660 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1666 zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs)
1668 struct uuid uuid = EFI_RESERVED;
1669 efi_gpe_t gpe = { 0 };
1675 if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag))
1676 return (SET_ERROR(EFAULT));
1677 ptr = (char *)(uintptr_t)efi.dki_data_64;
1678 length = efi.dki_length;
1680 * Some clients may attempt to request a PMBR for the
1681 * zvol. Currently this interface will return EINVAL to
1682 * such requests. These requests could be supported by
1683 * adding a check for lba == 0 and consing up an appropriate
1686 if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0)
1687 return (SET_ERROR(EINVAL));
1689 gpe.efi_gpe_StartingLBA = LE_64(34ULL);
1690 gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1);
1691 UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
1693 if (efi.dki_lba == 1) {
1694 efi_gpt_t gpt = { 0 };
1696 gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
1697 gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
1698 gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt));
1699 gpt.efi_gpt_MyLBA = LE_64(1ULL);
1700 gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL);
1701 gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1);
1702 gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL);
1703 gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
1704 gpt.efi_gpt_SizeOfPartitionEntry =
1705 LE_32(sizeof (efi_gpe_t));
1706 CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
1707 gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
1708 CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table);
1709 gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
1710 if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length),
1712 return (SET_ERROR(EFAULT));
1713 ptr += sizeof (gpt);
1714 length -= sizeof (gpt);
1716 if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe),
1718 return (SET_ERROR(EFAULT));
1723 * BEGIN entry points to allow external callers access to the volume.
1726 * Return the volume parameters needed for access from an external caller.
1727 * These values are invariant as long as the volume is held open.
1730 zvol_get_volume_params(minor_t minor, uint64_t *blksize,
1731 uint64_t *max_xfer_len, void **minor_hdl, void **objset_hdl, void **zil_hdl,
1732 void **rl_hdl, void **bonus_hdl)
1736 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1738 return (SET_ERROR(ENXIO));
1739 if (zv->zv_flags & ZVOL_DUMPIFIED)
1740 return (SET_ERROR(ENXIO));
1742 ASSERT(blksize && max_xfer_len && minor_hdl &&
1743 objset_hdl && zil_hdl && rl_hdl && bonus_hdl);
1745 *blksize = zv->zv_volblocksize;
1746 *max_xfer_len = (uint64_t)zvol_maxphys;
1748 *objset_hdl = zv->zv_objset;
1749 *zil_hdl = zv->zv_zilog;
1750 *rl_hdl = &zv->zv_znode;
1751 *bonus_hdl = zv->zv_dbuf;
1756 * Return the current volume size to an external caller.
1757 * The size can change while the volume is open.
1760 zvol_get_volume_size(void *minor_hdl)
1762 zvol_state_t *zv = minor_hdl;
1764 return (zv->zv_volsize);
1768 * Return the current WCE setting to an external caller.
1769 * The WCE setting can change while the volume is open.
1772 zvol_get_volume_wce(void *minor_hdl)
1774 zvol_state_t *zv = minor_hdl;
1776 return ((zv->zv_flags & ZVOL_WCE) ? 1 : 0);
1780 * Entry point for external callers to zvol_log_write
1783 zvol_log_write_minor(void *minor_hdl, dmu_tx_t *tx, offset_t off, ssize_t resid,
1786 zvol_state_t *zv = minor_hdl;
1788 zvol_log_write(zv, tx, off, resid, sync);
1791 * END entry points to allow external callers access to the volume.
1796 * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE.
1799 zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off, uint64_t len,
1804 zilog_t *zilog = zv->zv_zilog;
1806 if (zil_replaying(zilog, tx))
1809 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1810 lr = (lr_truncate_t *)&itx->itx_lr;
1811 lr->lr_foid = ZVOL_OBJ;
1812 lr->lr_offset = off;
1813 lr->lr_length = len;
1815 itx->itx_sync = sync;
1816 zil_itx_assign(zilog, itx, tx);
1821 * Dirtbag ioctls to support mkfs(1M) for UFS filesystems. See dkio(7I).
1822 * Also a dirtbag dkio ioctl for unmap/free-block functionality.
1826 zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
1829 struct dk_callback *dkc;
1833 mutex_enter(&spa_namespace_lock);
1835 zv = zfsdev_get_soft_state(getminor(dev), ZSST_ZVOL);
1838 mutex_exit(&spa_namespace_lock);
1839 return (SET_ERROR(ENXIO));
1841 ASSERT(zv->zv_total_opens > 0);
1847 struct dk_cinfo dki;
1849 bzero(&dki, sizeof (dki));
1850 (void) strcpy(dki.dki_cname, "zvol");
1851 (void) strcpy(dki.dki_dname, "zvol");
1852 dki.dki_ctype = DKC_UNKNOWN;
1853 dki.dki_unit = getminor(dev);
1854 dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs);
1855 mutex_exit(&spa_namespace_lock);
1856 if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag))
1857 error = SET_ERROR(EFAULT);
1861 case DKIOCGMEDIAINFO:
1863 struct dk_minfo dkm;
1865 bzero(&dkm, sizeof (dkm));
1866 dkm.dki_lbsize = 1U << zv->zv_min_bs;
1867 dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
1868 dkm.dki_media_type = DK_UNKNOWN;
1869 mutex_exit(&spa_namespace_lock);
1870 if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
1871 error = SET_ERROR(EFAULT);
1875 case DKIOCGMEDIAINFOEXT:
1877 struct dk_minfo_ext dkmext;
1879 bzero(&dkmext, sizeof (dkmext));
1880 dkmext.dki_lbsize = 1U << zv->zv_min_bs;
1881 dkmext.dki_pbsize = zv->zv_volblocksize;
1882 dkmext.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
1883 dkmext.dki_media_type = DK_UNKNOWN;
1884 mutex_exit(&spa_namespace_lock);
1885 if (ddi_copyout(&dkmext, (void *)arg, sizeof (dkmext), flag))
1886 error = SET_ERROR(EFAULT);
1892 uint64_t vs = zv->zv_volsize;
1893 uint8_t bs = zv->zv_min_bs;
1895 mutex_exit(&spa_namespace_lock);
1896 error = zvol_getefi((void *)arg, flag, vs, bs);
1900 case DKIOCFLUSHWRITECACHE:
1901 dkc = (struct dk_callback *)arg;
1902 mutex_exit(&spa_namespace_lock);
1903 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1904 if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) {
1905 (*dkc->dkc_callback)(dkc->dkc_cookie, error);
1912 int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0;
1913 if (ddi_copyout(&wce, (void *)arg, sizeof (int),
1915 error = SET_ERROR(EFAULT);
1921 if (ddi_copyin((void *)arg, &wce, sizeof (int),
1923 error = SET_ERROR(EFAULT);
1927 zv->zv_flags |= ZVOL_WCE;
1928 mutex_exit(&spa_namespace_lock);
1930 zv->zv_flags &= ~ZVOL_WCE;
1931 mutex_exit(&spa_namespace_lock);
1932 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1940 * commands using these (like prtvtoc) expect ENOTSUP
1941 * since we're emulating an EFI label
1943 error = SET_ERROR(ENOTSUP);
1947 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1949 error = zvol_dumpify(zv);
1950 zfs_range_unlock(rl);
1954 if (!(zv->zv_flags & ZVOL_DUMPIFIED))
1956 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1958 error = zvol_dump_fini(zv);
1959 zfs_range_unlock(rl);
1967 if (ddi_copyin((void *)arg, &df, sizeof (df), flag)) {
1968 error = SET_ERROR(EFAULT);
1973 * Apply Postel's Law to length-checking. If they overshoot,
1974 * just blank out until the end, if there's a need to blank
1977 if (df.df_start >= zv->zv_volsize)
1978 break; /* No need to do anything... */
1979 if (df.df_start + df.df_length > zv->zv_volsize)
1980 df.df_length = DMU_OBJECT_END;
1982 rl = zfs_range_lock(&zv->zv_znode, df.df_start, df.df_length,
1984 tx = dmu_tx_create(zv->zv_objset);
1985 error = dmu_tx_assign(tx, TXG_WAIT);
1989 zvol_log_truncate(zv, tx, df.df_start,
1990 df.df_length, B_TRUE);
1992 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ,
1993 df.df_start, df.df_length);
1996 zfs_range_unlock(rl);
2000 * If the write-cache is disabled or 'sync' property
2001 * is set to 'always' then treat this as a synchronous
2002 * operation (i.e. commit to zil).
2004 if (!(zv->zv_flags & ZVOL_WCE) ||
2005 (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS))
2006 zil_commit(zv->zv_zilog, ZVOL_OBJ);
2009 * If the caller really wants synchronous writes, and
2010 * can't wait for them, don't return until the write
2013 if (df.df_flags & DF_WAIT_SYNC) {
2015 dmu_objset_pool(zv->zv_objset), 0);
2022 error = SET_ERROR(ENOTTY);
2026 mutex_exit(&spa_namespace_lock);
2034 return (zvol_minors != 0);
2040 VERIFY(ddi_soft_state_init(&zfsdev_state, sizeof (zfs_soft_state_t),
2042 ZFS_LOG(1, "ZVOL Initialized.");
2048 ddi_soft_state_fini(&zfsdev_state);
2049 ZFS_LOG(1, "ZVOL Deinitialized.");
2055 zfs_mvdev_dump_feature_check(void *arg, dmu_tx_t *tx)
2057 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
2059 if (spa_feature_is_active(spa, SPA_FEATURE_MULTI_VDEV_CRASH_DUMP))
2066 zfs_mvdev_dump_activate_feature_sync(void *arg, dmu_tx_t *tx)
2068 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
2070 spa_feature_incr(spa, SPA_FEATURE_MULTI_VDEV_CRASH_DUMP, tx);
2074 zvol_dump_init(zvol_state_t *zv, boolean_t resize)
2078 objset_t *os = zv->zv_objset;
2079 spa_t *spa = dmu_objset_spa(os);
2080 vdev_t *vd = spa->spa_root_vdev;
2081 nvlist_t *nv = NULL;
2082 uint64_t version = spa_version(spa);
2083 enum zio_checksum checksum;
2085 ASSERT(MUTEX_HELD(&spa_namespace_lock));
2086 ASSERT(vd->vdev_ops == &vdev_root_ops);
2088 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0,
2090 /* wait for dmu_free_long_range to actually free the blocks */
2091 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
2094 * If the pool on which the dump device is being initialized has more
2095 * than one child vdev, check that the MULTI_VDEV_CRASH_DUMP feature is
2096 * enabled. If so, bump that feature's counter to indicate that the
2097 * feature is active. We also check the vdev type to handle the
2099 * # zpool create test raidz disk1 disk2 disk3
2100 * Now have spa_root_vdev->vdev_children == 1 (the raidz vdev),
2101 * the raidz vdev itself has 3 children.
2103 if (vd->vdev_children > 1 || vd->vdev_ops == &vdev_raidz_ops) {
2104 if (!spa_feature_is_enabled(spa,
2105 SPA_FEATURE_MULTI_VDEV_CRASH_DUMP))
2106 return (SET_ERROR(ENOTSUP));
2107 (void) dsl_sync_task(spa_name(spa),
2108 zfs_mvdev_dump_feature_check,
2109 zfs_mvdev_dump_activate_feature_sync, NULL, 2);
2112 tx = dmu_tx_create(os);
2113 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
2114 dmu_tx_hold_bonus(tx, ZVOL_OBJ);
2115 error = dmu_tx_assign(tx, TXG_WAIT);
2122 * If MULTI_VDEV_CRASH_DUMP is active, use the NOPARITY checksum
2123 * function. Otherwise, use the old default -- OFF.
2125 checksum = spa_feature_is_active(spa,
2126 SPA_FEATURE_MULTI_VDEV_CRASH_DUMP) ? ZIO_CHECKSUM_NOPARITY :
2130 * If we are resizing the dump device then we only need to
2131 * update the refreservation to match the newly updated
2132 * zvolsize. Otherwise, we save off the original state of the
2133 * zvol so that we can restore them if the zvol is ever undumpified.
2136 error = zap_update(os, ZVOL_ZAP_OBJ,
2137 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
2138 &zv->zv_volsize, tx);
2140 uint64_t checksum, compress, refresrv, vbs, dedup;
2142 error = dsl_prop_get_integer(zv->zv_name,
2143 zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL);
2144 error = error ? error : dsl_prop_get_integer(zv->zv_name,
2145 zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL);
2146 error = error ? error : dsl_prop_get_integer(zv->zv_name,
2147 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL);
2148 error = error ? error : dsl_prop_get_integer(zv->zv_name,
2149 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL);
2150 if (version >= SPA_VERSION_DEDUP) {
2151 error = error ? error :
2152 dsl_prop_get_integer(zv->zv_name,
2153 zfs_prop_to_name(ZFS_PROP_DEDUP), &dedup, NULL);
2156 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
2157 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1,
2159 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
2160 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx);
2161 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
2162 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
2164 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
2165 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1,
2167 error = error ? error : dmu_object_set_blocksize(
2168 os, ZVOL_OBJ, SPA_MAXBLOCKSIZE, 0, tx);
2169 if (version >= SPA_VERSION_DEDUP) {
2170 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
2171 zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1,
2175 zv->zv_volblocksize = SPA_MAXBLOCKSIZE;
2180 * We only need update the zvol's property if we are initializing
2181 * the dump area for the first time.
2184 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2185 VERIFY(nvlist_add_uint64(nv,
2186 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0);
2187 VERIFY(nvlist_add_uint64(nv,
2188 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
2189 ZIO_COMPRESS_OFF) == 0);
2190 VERIFY(nvlist_add_uint64(nv,
2191 zfs_prop_to_name(ZFS_PROP_CHECKSUM),
2193 if (version >= SPA_VERSION_DEDUP) {
2194 VERIFY(nvlist_add_uint64(nv,
2195 zfs_prop_to_name(ZFS_PROP_DEDUP),
2196 ZIO_CHECKSUM_OFF) == 0);
2199 error = zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
2207 /* Allocate the space for the dump */
2208 error = zvol_prealloc(zv);
2213 zvol_dumpify(zvol_state_t *zv)
2216 uint64_t dumpsize = 0;
2218 objset_t *os = zv->zv_objset;
2220 if (zv->zv_flags & ZVOL_RDONLY)
2221 return (SET_ERROR(EROFS));
2223 if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE,
2224 8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) {
2225 boolean_t resize = (dumpsize > 0);
2227 if ((error = zvol_dump_init(zv, resize)) != 0) {
2228 (void) zvol_dump_fini(zv);
2234 * Build up our lba mapping.
2236 error = zvol_get_lbas(zv);
2238 (void) zvol_dump_fini(zv);
2242 tx = dmu_tx_create(os);
2243 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
2244 error = dmu_tx_assign(tx, TXG_WAIT);
2247 (void) zvol_dump_fini(zv);
2251 zv->zv_flags |= ZVOL_DUMPIFIED;
2252 error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1,
2253 &zv->zv_volsize, tx);
2257 (void) zvol_dump_fini(zv);
2261 txg_wait_synced(dmu_objset_pool(os), 0);
2266 zvol_dump_fini(zvol_state_t *zv)
2269 objset_t *os = zv->zv_objset;
2272 uint64_t checksum, compress, refresrv, vbs, dedup;
2273 uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset));
2276 * Attempt to restore the zvol back to its pre-dumpified state.
2277 * This is a best-effort attempt as it's possible that not all
2278 * of these properties were initialized during the dumpify process
2279 * (i.e. error during zvol_dump_init).
2282 tx = dmu_tx_create(os);
2283 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
2284 error = dmu_tx_assign(tx, TXG_WAIT);
2289 (void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx);
2292 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2293 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum);
2294 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress);
2296 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2297 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv);
2298 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2299 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs);
2301 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2302 (void) nvlist_add_uint64(nv,
2303 zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum);
2304 (void) nvlist_add_uint64(nv,
2305 zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress);
2306 (void) nvlist_add_uint64(nv,
2307 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv);
2308 if (version >= SPA_VERSION_DEDUP &&
2309 zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2310 zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, &dedup) == 0) {
2311 (void) nvlist_add_uint64(nv,
2312 zfs_prop_to_name(ZFS_PROP_DEDUP), dedup);
2314 (void) zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
2318 zvol_free_extents(zv);
2319 zv->zv_flags &= ~ZVOL_DUMPIFIED;
2320 (void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END);
2321 /* wait for dmu_free_long_range to actually free the blocks */
2322 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
2323 tx = dmu_tx_create(os);
2324 dmu_tx_hold_bonus(tx, ZVOL_OBJ);
2325 error = dmu_tx_assign(tx, TXG_WAIT);
2330 if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0)
2331 zv->zv_volblocksize = vbs;
2339 zvol_geom_run(zvol_state_t *zv)
2341 struct g_provider *pp;
2343 pp = zv->zv_provider;
2344 g_error_provider(pp, 0);
2346 kproc_kthread_add(zvol_geom_worker, zv, &zfsproc, NULL, 0, 0,
2347 "zfskern", "zvol %s", pp->name + sizeof(ZVOL_DRIVER));
2351 zvol_geom_destroy(zvol_state_t *zv)
2353 struct g_provider *pp;
2355 g_topology_assert();
2357 mtx_lock(&zv->zv_queue_mtx);
2359 wakeup_one(&zv->zv_queue);
2360 while (zv->zv_state != 2)
2361 msleep(&zv->zv_state, &zv->zv_queue_mtx, 0, "zvol:w", 0);
2362 mtx_destroy(&zv->zv_queue_mtx);
2364 pp = zv->zv_provider;
2365 zv->zv_provider = NULL;
2367 g_wither_geom(pp->geom, ENXIO);
2371 zvol_geom_access(struct g_provider *pp, int acr, int acw, int ace)
2373 int count, error, flags;
2375 g_topology_assert();
2378 * To make it easier we expect either open or close, but not both
2381 KASSERT((acr >= 0 && acw >= 0 && ace >= 0) ||
2382 (acr <= 0 && acw <= 0 && ace <= 0),
2383 ("Unsupported access request to %s (acr=%d, acw=%d, ace=%d).",
2384 pp->name, acr, acw, ace));
2386 if (pp->private == NULL) {
2387 if (acr <= 0 && acw <= 0 && ace <= 0)
2393 * We don't pass FEXCL flag to zvol_open()/zvol_close() if ace != 0,
2394 * because GEOM already handles that and handles it a bit differently.
2395 * GEOM allows for multiple read/exclusive consumers and ZFS allows
2396 * only one exclusive consumer, no matter if it is reader or writer.
2397 * I like better the way GEOM works so I'll leave it for GEOM to
2398 * decide what to do.
2401 count = acr + acw + ace;
2406 if (acr != 0 || ace != 0)
2411 g_topology_unlock();
2413 error = zvol_open(pp, flags, count);
2415 error = zvol_close(pp, flags, -count);
2421 zvol_geom_start(struct bio *bp)
2426 zv = bp->bio_to->private;
2428 switch (bp->bio_cmd) {
2430 if (!THREAD_CAN_SLEEP())
2432 zil_commit(zv->zv_zilog, ZVOL_OBJ);
2433 g_io_deliver(bp, 0);
2438 if (!THREAD_CAN_SLEEP())
2443 if (g_handleattr_int(bp, "GEOM::candelete", 1))
2447 g_io_deliver(bp, EOPNOTSUPP);
2453 mtx_lock(&zv->zv_queue_mtx);
2454 first = (bioq_first(&zv->zv_queue) == NULL);
2455 bioq_insert_tail(&zv->zv_queue, bp);
2456 mtx_unlock(&zv->zv_queue_mtx);
2458 wakeup_one(&zv->zv_queue);
2462 zvol_geom_worker(void *arg)
2467 thread_lock(curthread);
2468 sched_prio(curthread, PRIBIO);
2469 thread_unlock(curthread);
2473 mtx_lock(&zv->zv_queue_mtx);
2474 bp = bioq_takefirst(&zv->zv_queue);
2476 if (zv->zv_state == 1) {
2478 wakeup(&zv->zv_state);
2479 mtx_unlock(&zv->zv_queue_mtx);
2482 msleep(&zv->zv_queue, &zv->zv_queue_mtx, PRIBIO | PDROP,
2486 mtx_unlock(&zv->zv_queue_mtx);
2487 switch (bp->bio_cmd) {
2489 zil_commit(zv->zv_zilog, ZVOL_OBJ);
2490 g_io_deliver(bp, 0);
2500 extern boolean_t dataset_name_hidden(const char *name);
2503 zvol_create_snapshots(objset_t *os, const char *name)
2505 uint64_t cookie, obj;
2510 sname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2513 (void) dmu_objset_find(name, dmu_objset_prefetch, NULL,
2518 len = snprintf(sname, MAXPATHLEN, "%s@", name);
2519 if (len >= MAXPATHLEN) {
2520 dmu_objset_rele(os, FTAG);
2521 error = ENAMETOOLONG;
2525 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
2526 error = dmu_snapshot_list_next(os, MAXPATHLEN - len,
2527 sname + len, &obj, &cookie, NULL);
2528 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
2530 if (error == ENOENT)
2535 if ((error = zvol_create_minor(sname)) != 0) {
2536 printf("ZFS WARNING: Unable to create ZVOL %s (error=%d).\n",
2542 kmem_free(sname, MAXPATHLEN);
2547 zvol_create_minors(const char *name)
2554 if (dataset_name_hidden(name))
2557 if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) {
2558 printf("ZFS WARNING: Unable to put hold on %s (error=%d).\n",
2562 if (dmu_objset_type(os) == DMU_OST_ZVOL) {
2563 dsl_dataset_long_hold(os->os_dsl_dataset, FTAG);
2564 dsl_pool_rele(dmu_objset_pool(os), FTAG);
2565 if ((error = zvol_create_minor(name)) == 0)
2566 error = zvol_create_snapshots(os, name);
2568 printf("ZFS WARNING: Unable to create ZVOL %s (error=%d).\n",
2571 dsl_dataset_long_rele(os->os_dsl_dataset, FTAG);
2572 dsl_dataset_rele(os->os_dsl_dataset, FTAG);
2575 if (dmu_objset_type(os) != DMU_OST_ZFS) {
2576 dmu_objset_rele(os, FTAG);
2580 osname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2581 if (snprintf(osname, MAXPATHLEN, "%s/", name) >= MAXPATHLEN) {
2582 dmu_objset_rele(os, FTAG);
2583 kmem_free(osname, MAXPATHLEN);
2586 p = osname + strlen(osname);
2587 len = MAXPATHLEN - (p - osname);
2590 /* Prefetch the datasets. */
2592 while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0) {
2593 if (!dataset_name_hidden(osname))
2594 (void) dmu_objset_prefetch(osname, NULL);
2599 while (dmu_dir_list_next(os, MAXPATHLEN - (p - osname), p, NULL,
2601 dmu_objset_rele(os, FTAG);
2602 (void)zvol_create_minors(osname);
2603 if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) {
2604 printf("ZFS WARNING: Unable to put hold on %s (error=%d).\n",
2610 dmu_objset_rele(os, FTAG);
2611 kmem_free(osname, MAXPATHLEN);
2616 zvol_rename_minor(zvol_state_t *zv, const char *newname)
2619 struct g_provider *pp;
2622 ASSERT(MUTEX_HELD(&spa_namespace_lock));
2624 if (zv->zv_volmode == ZFS_VOLMODE_GEOM) {
2626 pp = zv->zv_provider;
2631 zv->zv_provider = NULL;
2632 g_wither_provider(pp, ENXIO);
2634 pp = g_new_providerf(gp, "%s/%s", ZVOL_DRIVER, newname);
2635 pp->flags |= G_PF_DIRECT_RECEIVE | G_PF_DIRECT_SEND;
2636 pp->sectorsize = DEV_BSIZE;
2637 pp->mediasize = zv->zv_volsize;
2639 zv->zv_provider = pp;
2640 g_error_provider(pp, 0);
2641 g_topology_unlock();
2642 } else if (zv->zv_volmode == ZFS_VOLMODE_DEV) {
2644 ASSERT(dev != NULL);
2648 if (make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK,
2649 &dev, &zvol_cdevsw, NULL, UID_ROOT, GID_OPERATOR,
2650 0640, "%s/%s", ZVOL_DRIVER, newname) == 0) {
2652 dev->si_iosize_max = MAXPHYS;
2656 strlcpy(zv->zv_name, newname, sizeof(zv->zv_name));
2660 zvol_rename_minors(const char *oldname, const char *newname)
2662 char name[MAXPATHLEN];
2663 struct g_provider *pp;
2665 size_t oldnamelen, newnamelen;
2669 oldnamelen = strlen(oldname);
2670 newnamelen = strlen(newname);
2673 mutex_enter(&spa_namespace_lock);
2675 LIST_FOREACH(zv, &all_zvols, zv_links) {
2676 if (strcmp(zv->zv_name, oldname) == 0) {
2677 zvol_rename_minor(zv, newname);
2678 } else if (strncmp(zv->zv_name, oldname, oldnamelen) == 0 &&
2679 (zv->zv_name[oldnamelen] == '/' ||
2680 zv->zv_name[oldnamelen] == '@')) {
2681 snprintf(name, sizeof(name), "%s%c%s", newname,
2682 zv->zv_name[oldnamelen],
2683 zv->zv_name + oldnamelen + 1);
2684 zvol_rename_minor(zv, name);
2688 mutex_exit(&spa_namespace_lock);
2693 zvol_d_open(struct cdev *dev, int flags, int fmt, struct thread *td)
2698 mutex_enter(&spa_namespace_lock);
2701 mutex_exit(&spa_namespace_lock);
2702 return(ENXIO); /* zvol_create_minor() not done yet */
2705 if (zv->zv_total_opens == 0)
2706 err = zvol_first_open(zv);
2708 mutex_exit(&spa_namespace_lock);
2711 if ((flags & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
2712 err = SET_ERROR(EROFS);
2715 if (zv->zv_flags & ZVOL_EXCL) {
2716 err = SET_ERROR(EBUSY);
2720 if (flags & FEXCL) {
2721 if (zv->zv_total_opens != 0) {
2722 err = SET_ERROR(EBUSY);
2725 zv->zv_flags |= ZVOL_EXCL;
2729 zv->zv_total_opens++;
2730 mutex_exit(&spa_namespace_lock);
2733 if (zv->zv_total_opens == 0)
2734 zvol_last_close(zv);
2735 mutex_exit(&spa_namespace_lock);
2740 zvol_d_close(struct cdev *dev, int flags, int fmt, struct thread *td)
2745 mutex_enter(&spa_namespace_lock);
2748 mutex_exit(&spa_namespace_lock);
2752 if (zv->zv_flags & ZVOL_EXCL) {
2753 ASSERT(zv->zv_total_opens == 1);
2754 zv->zv_flags &= ~ZVOL_EXCL;
2758 * If the open count is zero, this is a spurious close.
2759 * That indicates a bug in the kernel / DDI framework.
2761 ASSERT(zv->zv_total_opens != 0);
2764 * You may get multiple opens, but only one close.
2766 zv->zv_total_opens--;
2768 if (zv->zv_total_opens == 0)
2769 zvol_last_close(zv);
2771 mutex_exit(&spa_namespace_lock);
2776 zvol_d_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
2780 off_t offset, length, chunk;
2787 KASSERT(zv->zv_total_opens > 0,
2788 ("Device with zero access count in zvol_d_ioctl"));
2790 i = IOCPARM_LEN(cmd);
2792 case DIOCGSECTORSIZE:
2793 *(u_int *)data = DEV_BSIZE;
2795 case DIOCGMEDIASIZE:
2796 *(off_t *)data = zv->zv_volsize;
2799 zil_commit(zv->zv_zilog, ZVOL_OBJ);
2802 offset = ((off_t *)data)[0];
2803 length = ((off_t *)data)[1];
2804 if ((offset % DEV_BSIZE) != 0 || (length % DEV_BSIZE) != 0 ||
2805 offset < 0 || offset >= zv->zv_volsize ||
2807 printf("%s: offset=%jd length=%jd\n", __func__, offset,
2813 rl = zfs_range_lock(&zv->zv_znode, offset, length, RL_WRITER);
2814 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
2815 error = dmu_tx_assign(tx, TXG_WAIT);
2819 zvol_log_truncate(zv, tx, offset, length, B_TRUE);
2821 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ,
2824 zfs_range_unlock(rl);
2825 if (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)
2826 zil_commit(zv->zv_zilog, ZVOL_OBJ);
2828 case DIOCGSTRIPESIZE:
2829 *(off_t *)data = zv->zv_volblocksize;
2831 case DIOCGSTRIPEOFFSET: