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.
29 /* Portions Copyright 2010 Robert Milkowski */
30 /* Portions Copyright 2011 Martin Matuska <mm@FreeBSD.org> */
33 * ZFS volume emulation driver.
35 * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes.
36 * Volumes are accessed through the symbolic links named:
38 * /dev/zvol/dsk/<pool_name>/<dataset_name>
39 * /dev/zvol/rdsk/<pool_name>/<dataset_name>
41 * These links are created by the /dev filesystem (sdev_zvolops.c).
42 * Volumes are persistent through reboot. No user command needs to be
43 * run before opening and using a device.
46 * On FreeBSD ZVOLs are simply GEOM providers like any other storage device
50 #include <sys/types.h>
51 #include <sys/param.h>
52 #include <sys/kernel.h>
53 #include <sys/errno.h>
59 #include <sys/cmn_err.h>
64 #include <sys/dmu_traverse.h>
65 #include <sys/dnode.h>
66 #include <sys/dsl_dataset.h>
67 #include <sys/dsl_prop.h>
69 #include <sys/byteorder.h>
70 #include <sys/sunddi.h>
71 #include <sys/dirent.h>
72 #include <sys/policy.h>
73 #include <sys/fs/zfs.h>
74 #include <sys/zfs_ioctl.h>
76 #include <sys/refcount.h>
77 #include <sys/zfs_znode.h>
78 #include <sys/zfs_rlock.h>
79 #include <sys/vdev_impl.h>
81 #include <sys/zil_impl.h>
83 #include <geom/geom.h>
85 #include "zfs_namecheck.h"
87 struct g_class zfs_zvol_class = {
92 DECLARE_GEOM_CLASS(zfs_zvol_class, zfs_zvol);
95 static char *zvol_tag = "zvol_tag";
97 #define ZVOL_DUMPSIZE "dumpsize"
100 * The spa_namespace_lock protects the zfsdev_state structure from being
101 * modified while it's being used, e.g. an open that comes in before a
102 * create finishes. It also protects temporary opens of the dataset so that,
103 * e.g., an open doesn't get a spurious EBUSY.
105 static uint32_t zvol_minors;
107 typedef struct zvol_extent {
109 dva_t ze_dva; /* dva associated with this extent */
110 uint64_t ze_nblks; /* number of blocks in extent */
114 * The in-core state of each volume.
116 typedef struct zvol_state {
117 char zv_name[MAXPATHLEN]; /* pool/dd name */
118 uint64_t zv_volsize; /* amount of space we advertise */
119 uint64_t zv_volblocksize; /* volume block size */
120 struct g_provider *zv_provider; /* GEOM provider */
121 uint8_t zv_min_bs; /* minimum addressable block shift */
122 uint8_t zv_flags; /* readonly, dumpified, etc. */
123 objset_t *zv_objset; /* objset handle */
124 uint32_t zv_total_opens; /* total open count */
125 zilog_t *zv_zilog; /* ZIL handle */
126 list_t zv_extents; /* List of extents for dump */
127 znode_t zv_znode; /* for range locking */
128 dmu_buf_t *zv_dbuf; /* bonus handle */
130 struct bio_queue_head zv_queue;
131 struct mtx zv_queue_mtx; /* zv_queue mutex */
135 * zvol specific flags
137 #define ZVOL_RDONLY 0x1
138 #define ZVOL_DUMPIFIED 0x2
139 #define ZVOL_EXCL 0x4
143 * zvol maximum transfer in one DMU tx.
145 int zvol_maxphys = DMU_MAX_ACCESS/2;
147 extern int zfs_set_prop_nvlist(const char *, zprop_source_t,
148 nvlist_t *, nvlist_t *);
149 static int zvol_remove_zv(zvol_state_t *);
150 static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio);
151 static int zvol_dumpify(zvol_state_t *zv);
152 static int zvol_dump_fini(zvol_state_t *zv);
153 static int zvol_dump_init(zvol_state_t *zv, boolean_t resize);
155 static zvol_state_t *zvol_geom_create(const char *name);
156 static void zvol_geom_run(zvol_state_t *zv);
157 static void zvol_geom_destroy(zvol_state_t *zv);
158 static int zvol_geom_access(struct g_provider *pp, int acr, int acw, int ace);
159 static void zvol_geom_start(struct bio *bp);
160 static void zvol_geom_worker(void *arg);
163 zvol_size_changed(zvol_state_t *zv)
166 dev_t dev = makedevice(maj, min);
168 VERIFY(ddi_prop_update_int64(dev, zfs_dip,
169 "Size", volsize) == DDI_SUCCESS);
170 VERIFY(ddi_prop_update_int64(dev, zfs_dip,
171 "Nblocks", lbtodb(volsize)) == DDI_SUCCESS);
173 /* Notify specfs to invalidate the cached size */
174 spec_size_invalidate(dev, VBLK);
175 spec_size_invalidate(dev, VCHR);
177 struct g_provider *pp;
179 pp = zv->zv_provider;
182 if (zv->zv_volsize == pp->mediasize)
185 * Changing provider size is not really supported by GEOM, but it
186 * should be safe when provider is closed.
188 if (zv->zv_total_opens > 0)
190 pp->mediasize = zv->zv_volsize;
195 zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
198 return (SET_ERROR(EINVAL));
200 if (volsize % blocksize != 0)
201 return (SET_ERROR(EINVAL));
204 if (volsize - 1 > SPEC_MAXOFFSET_T)
205 return (SET_ERROR(EOVERFLOW));
211 zvol_check_volblocksize(uint64_t volblocksize)
213 if (volblocksize < SPA_MINBLOCKSIZE ||
214 volblocksize > SPA_MAXBLOCKSIZE ||
216 return (SET_ERROR(EDOM));
222 zvol_get_stats(objset_t *os, nvlist_t *nv)
225 dmu_object_info_t doi;
228 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
232 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
234 error = dmu_object_info(os, ZVOL_OBJ, &doi);
237 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
238 doi.doi_data_block_size);
244 static zvol_state_t *
245 zvol_minor_lookup(const char *name)
247 struct g_provider *pp;
249 zvol_state_t *zv = NULL;
251 ASSERT(MUTEX_HELD(&spa_namespace_lock));
254 LIST_FOREACH(gp, &zfs_zvol_class.geom, geom) {
255 pp = LIST_FIRST(&gp->provider);
261 if (strcmp(zv->zv_name, name) == 0)
266 return (gp != NULL ? zv : NULL);
269 /* extent mapping arg */
277 zvol_map_block(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
278 const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
280 struct maparg *ma = arg;
282 int bs = ma->ma_zv->zv_volblocksize;
284 if (bp == NULL || zb->zb_object != ZVOL_OBJ || zb->zb_level != 0)
287 VERIFY3U(ma->ma_blks, ==, zb->zb_blkid);
290 /* Abort immediately if we have encountered gang blocks */
292 return (SET_ERROR(EFRAGS));
295 * See if the block is at the end of the previous extent.
297 ze = list_tail(&ma->ma_zv->zv_extents);
299 DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) &&
300 DVA_GET_OFFSET(BP_IDENTITY(bp)) ==
301 DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) {
306 dprintf_bp(bp, "%s", "next blkptr:");
308 /* start a new extent */
309 ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP);
310 ze->ze_dva = bp->blk_dva[0]; /* structure assignment */
312 list_insert_tail(&ma->ma_zv->zv_extents, ze);
317 zvol_free_extents(zvol_state_t *zv)
321 while (ze = list_head(&zv->zv_extents)) {
322 list_remove(&zv->zv_extents, ze);
323 kmem_free(ze, sizeof (zvol_extent_t));
328 zvol_get_lbas(zvol_state_t *zv)
330 objset_t *os = zv->zv_objset;
336 zvol_free_extents(zv);
338 /* commit any in-flight changes before traversing the dataset */
339 txg_wait_synced(dmu_objset_pool(os), 0);
340 err = traverse_dataset(dmu_objset_ds(os), 0,
341 TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma);
342 if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) {
343 zvol_free_extents(zv);
344 return (err ? err : EIO);
352 zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
354 zfs_creat_t *zct = arg;
355 nvlist_t *nvprops = zct->zct_props;
357 uint64_t volblocksize, volsize;
359 VERIFY(nvlist_lookup_uint64(nvprops,
360 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
361 if (nvlist_lookup_uint64(nvprops,
362 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
363 volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
366 * These properties must be removed from the list so the generic
367 * property setting step won't apply to them.
369 VERIFY(nvlist_remove_all(nvprops,
370 zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
371 (void) nvlist_remove_all(nvprops,
372 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
374 error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
378 error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
382 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
387 * Replay a TX_WRITE ZIL transaction that didn't get committed
388 * after a system failure
391 zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
393 objset_t *os = zv->zv_objset;
394 char *data = (char *)(lr + 1); /* data follows lr_write_t */
395 uint64_t offset, length;
400 byteswap_uint64_array(lr, sizeof (*lr));
402 offset = lr->lr_offset;
403 length = lr->lr_length;
405 /* If it's a dmu_sync() block, write the whole block */
406 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
407 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
408 if (length < blocksize) {
409 offset -= offset % blocksize;
414 tx = dmu_tx_create(os);
415 dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length);
416 error = dmu_tx_assign(tx, TXG_WAIT);
420 dmu_write(os, ZVOL_OBJ, offset, length, data, tx);
429 zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
431 return (SET_ERROR(ENOTSUP));
435 * Callback vectors for replaying records.
436 * Only TX_WRITE is needed for zvol.
438 zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = {
439 zvol_replay_err, /* 0 no such transaction type */
440 zvol_replay_err, /* TX_CREATE */
441 zvol_replay_err, /* TX_MKDIR */
442 zvol_replay_err, /* TX_MKXATTR */
443 zvol_replay_err, /* TX_SYMLINK */
444 zvol_replay_err, /* TX_REMOVE */
445 zvol_replay_err, /* TX_RMDIR */
446 zvol_replay_err, /* TX_LINK */
447 zvol_replay_err, /* TX_RENAME */
448 zvol_replay_write, /* TX_WRITE */
449 zvol_replay_err, /* TX_TRUNCATE */
450 zvol_replay_err, /* TX_SETATTR */
451 zvol_replay_err, /* TX_ACL */
452 zvol_replay_err, /* TX_CREATE_ACL */
453 zvol_replay_err, /* TX_CREATE_ATTR */
454 zvol_replay_err, /* TX_CREATE_ACL_ATTR */
455 zvol_replay_err, /* TX_MKDIR_ACL */
456 zvol_replay_err, /* TX_MKDIR_ATTR */
457 zvol_replay_err, /* TX_MKDIR_ACL_ATTR */
458 zvol_replay_err, /* TX_WRITE2 */
463 zvol_name2minor(const char *name, minor_t *minor)
467 mutex_enter(&spa_namespace_lock);
468 zv = zvol_minor_lookup(name);
470 *minor = zv->zv_minor;
471 mutex_exit(&spa_namespace_lock);
472 return (zv ? 0 : -1);
477 * Create a minor node (plus a whole lot more) for the specified volume.
480 zvol_create_minor(const char *name)
482 zfs_soft_state_t *zs;
485 dmu_object_info_t doi;
489 ZFS_LOG(1, "Creating ZVOL %s...", name);
491 mutex_enter(&spa_namespace_lock);
493 if (zvol_minor_lookup(name) != NULL) {
494 mutex_exit(&spa_namespace_lock);
495 return (SET_ERROR(EEXIST));
498 /* lie and say we're read-only */
499 error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, FTAG, &os);
502 mutex_exit(&spa_namespace_lock);
507 if ((minor = zfsdev_minor_alloc()) == 0) {
508 dmu_objset_disown(os, FTAG);
509 mutex_exit(&spa_namespace_lock);
510 return (SET_ERROR(ENXIO));
513 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS) {
514 dmu_objset_disown(os, FTAG);
515 mutex_exit(&spa_namespace_lock);
516 return (SET_ERROR(EAGAIN));
518 (void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME,
521 (void) snprintf(chrbuf, sizeof (chrbuf), "%u,raw", minor);
523 if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR,
524 minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
525 ddi_soft_state_free(zfsdev_state, minor);
526 dmu_objset_disown(os, FTAG);
527 mutex_exit(&spa_namespace_lock);
528 return (SET_ERROR(EAGAIN));
531 (void) snprintf(blkbuf, sizeof (blkbuf), "%u", minor);
533 if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK,
534 minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
535 ddi_remove_minor_node(zfs_dip, chrbuf);
536 ddi_soft_state_free(zfsdev_state, minor);
537 dmu_objset_disown(os, FTAG);
538 mutex_exit(&spa_namespace_lock);
539 return (SET_ERROR(EAGAIN));
542 zs = ddi_get_soft_state(zfsdev_state, minor);
543 zs->zss_type = ZSST_ZVOL;
544 zv = zs->zss_data = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP);
547 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
550 dmu_objset_disown(os, zvol_tag);
551 mutex_exit(&spa_namespace_lock);
557 zv = zvol_geom_create(name);
558 zv->zv_volsize = volsize;
559 zv->zv_provider->mediasize = zv->zv_volsize;
563 (void) strlcpy(zv->zv_name, name, MAXPATHLEN);
564 zv->zv_min_bs = DEV_BSHIFT;
566 if (dmu_objset_is_snapshot(os) || !spa_writeable(dmu_objset_spa(os)))
567 zv->zv_flags |= ZVOL_RDONLY;
568 mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
569 avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
570 sizeof (rl_t), offsetof(rl_t, r_node));
571 list_create(&zv->zv_extents, sizeof (zvol_extent_t),
572 offsetof(zvol_extent_t, ze_node));
573 /* get and cache the blocksize */
574 error = dmu_object_info(os, ZVOL_OBJ, &doi);
576 zv->zv_volblocksize = doi.doi_data_block_size;
578 if (spa_writeable(dmu_objset_spa(os))) {
579 if (zil_replay_disable)
580 zil_destroy(dmu_objset_zil(os), B_FALSE);
582 zil_replay(os, zv, zvol_replay_vector);
584 dmu_objset_disown(os, FTAG);
585 zv->zv_objset = NULL;
589 mutex_exit(&spa_namespace_lock);
596 ZFS_LOG(1, "ZVOL %s created.", name);
602 * Remove minor node for the specified volume.
605 zvol_remove_zv(zvol_state_t *zv)
608 minor_t minor = zv->zv_minor;
611 ASSERT(MUTEX_HELD(&spa_namespace_lock));
612 if (zv->zv_total_opens != 0)
613 return (SET_ERROR(EBUSY));
615 ZFS_LOG(1, "ZVOL %s destroyed.", zv->zv_name);
618 (void) snprintf(nmbuf, sizeof (nmbuf), "%u,raw", minor);
619 ddi_remove_minor_node(zfs_dip, nmbuf);
622 avl_destroy(&zv->zv_znode.z_range_avl);
623 mutex_destroy(&zv->zv_znode.z_range_lock);
625 zvol_geom_destroy(zv);
632 zvol_remove_minor(const char *name)
637 mutex_enter(&spa_namespace_lock);
638 if ((zv = zvol_minor_lookup(name)) == NULL) {
639 mutex_exit(&spa_namespace_lock);
640 return (SET_ERROR(ENXIO));
643 rc = zvol_remove_zv(zv);
645 mutex_exit(&spa_namespace_lock);
650 zvol_first_open(zvol_state_t *zv)
657 /* lie and say we're read-only */
658 error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, B_TRUE,
663 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
666 dmu_objset_disown(os, zvol_tag);
670 error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf);
672 dmu_objset_disown(os, zvol_tag);
675 zv->zv_volsize = volsize;
676 zv->zv_zilog = zil_open(os, zvol_get_data);
677 zvol_size_changed(zv);
679 VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &readonly,
681 if (readonly || dmu_objset_is_snapshot(os) ||
682 !spa_writeable(dmu_objset_spa(os)))
683 zv->zv_flags |= ZVOL_RDONLY;
685 zv->zv_flags &= ~ZVOL_RDONLY;
690 zvol_last_close(zvol_state_t *zv)
692 zil_close(zv->zv_zilog);
695 dmu_buf_rele(zv->zv_dbuf, zvol_tag);
701 if (dsl_dataset_is_dirty(dmu_objset_ds(zv->zv_objset)) &&
702 !(zv->zv_flags & ZVOL_RDONLY))
703 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
704 dmu_objset_evict_dbufs(zv->zv_objset);
706 dmu_objset_disown(zv->zv_objset, zvol_tag);
707 zv->zv_objset = NULL;
712 zvol_prealloc(zvol_state_t *zv)
714 objset_t *os = zv->zv_objset;
716 uint64_t refd, avail, usedobjs, availobjs;
717 uint64_t resid = zv->zv_volsize;
720 /* Check the space usage before attempting to allocate the space */
721 dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs);
722 if (avail < zv->zv_volsize)
723 return (SET_ERROR(ENOSPC));
725 /* Free old extents if they exist */
726 zvol_free_extents(zv);
730 uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE);
732 tx = dmu_tx_create(os);
733 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
734 error = dmu_tx_assign(tx, TXG_WAIT);
737 (void) dmu_free_long_range(os, ZVOL_OBJ, 0, off);
740 dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx);
745 txg_wait_synced(dmu_objset_pool(os), 0);
752 zvol_update_volsize(objset_t *os, uint64_t volsize)
757 ASSERT(MUTEX_HELD(&spa_namespace_lock));
759 tx = dmu_tx_create(os);
760 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
761 error = dmu_tx_assign(tx, TXG_WAIT);
767 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
772 error = dmu_free_long_range(os,
773 ZVOL_OBJ, volsize, DMU_OBJECT_END);
778 zvol_remove_minors(const char *name)
780 struct g_geom *gp, *gptmp;
781 struct g_provider *pp;
785 namelen = strlen(name);
788 mutex_enter(&spa_namespace_lock);
791 LIST_FOREACH_SAFE(gp, &zfs_zvol_class.geom, geom, gptmp) {
792 pp = LIST_FIRST(&gp->provider);
798 if (strcmp(zv->zv_name, name) == 0 ||
799 (strncmp(zv->zv_name, name, namelen) == 0 &&
800 zv->zv_name[namelen] == '/')) {
801 (void) zvol_remove_zv(zv);
806 mutex_exit(&spa_namespace_lock);
811 zvol_set_volsize(const char *name, major_t maj, uint64_t volsize)
813 zvol_state_t *zv = NULL;
816 dmu_object_info_t doi;
817 uint64_t old_volsize = 0ULL;
820 mutex_enter(&spa_namespace_lock);
821 zv = zvol_minor_lookup(name);
822 if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) {
823 mutex_exit(&spa_namespace_lock);
827 if ((error = dmu_object_info(os, ZVOL_OBJ, &doi)) != 0 ||
828 (error = zvol_check_volsize(volsize,
829 doi.doi_data_block_size)) != 0)
832 VERIFY(dsl_prop_get_integer(name, "readonly", &readonly,
839 error = zvol_update_volsize(os, volsize);
841 * Reinitialize the dump area to the new size. If we
842 * failed to resize the dump area then restore it back to
845 if (zv && error == 0) {
847 if (zv->zv_flags & ZVOL_DUMPIFIED) {
848 old_volsize = zv->zv_volsize;
849 zv->zv_volsize = volsize;
850 if ((error = zvol_dumpify(zv)) != 0 ||
851 (error = dumpvp_resize()) != 0) {
852 (void) zvol_update_volsize(os, old_volsize);
853 zv->zv_volsize = old_volsize;
854 error = zvol_dumpify(zv);
857 #endif /* ZVOL_DUMP */
859 zv->zv_volsize = volsize;
860 zvol_size_changed(zv);
866 * Generate a LUN expansion event.
868 if (zv && error == 0) {
871 char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
873 (void) snprintf(physpath, MAXPATHLEN, "%s%u", ZVOL_PSEUDO_DEV,
876 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
877 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
879 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
880 ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
883 kmem_free(physpath, MAXPATHLEN);
888 dmu_objset_rele(os, FTAG);
890 mutex_exit(&spa_namespace_lock);
897 zvol_open(struct g_provider *pp, int flag, int count)
901 boolean_t locked = B_FALSE;
904 * Protect against recursively entering spa_namespace_lock
905 * when spa_open() is used for a pool on a (local) ZVOL(s).
906 * This is needed since we replaced upstream zfsdev_state_lock
907 * with spa_namespace_lock in the ZVOL code.
908 * We are using the same trick as spa_open().
909 * Note that calls in zvol_first_open which need to resolve
910 * pool name to a spa object will enter spa_open()
911 * recursively, but that function already has all the
912 * necessary protection.
914 if (!MUTEX_HELD(&spa_namespace_lock)) {
915 mutex_enter(&spa_namespace_lock);
922 mutex_exit(&spa_namespace_lock);
923 return (SET_ERROR(ENXIO));
926 if (zv->zv_total_opens == 0)
927 err = zvol_first_open(zv);
930 mutex_exit(&spa_namespace_lock);
933 if ((flag & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
934 err = SET_ERROR(EROFS);
937 if (zv->zv_flags & ZVOL_EXCL) {
938 err = SET_ERROR(EBUSY);
943 if (zv->zv_total_opens != 0) {
944 err = SET_ERROR(EBUSY);
947 zv->zv_flags |= ZVOL_EXCL;
951 zv->zv_total_opens += count;
953 mutex_exit(&spa_namespace_lock);
957 if (zv->zv_total_opens == 0)
960 mutex_exit(&spa_namespace_lock);
966 zvol_close(struct g_provider *pp, int flag, int count)
970 boolean_t locked = B_FALSE;
972 /* See comment in zvol_open(). */
973 if (!MUTEX_HELD(&spa_namespace_lock)) {
974 mutex_enter(&spa_namespace_lock);
981 mutex_exit(&spa_namespace_lock);
982 return (SET_ERROR(ENXIO));
985 if (zv->zv_flags & ZVOL_EXCL) {
986 ASSERT(zv->zv_total_opens == 1);
987 zv->zv_flags &= ~ZVOL_EXCL;
991 * If the open count is zero, this is a spurious close.
992 * That indicates a bug in the kernel / DDI framework.
994 ASSERT(zv->zv_total_opens != 0);
997 * You may get multiple opens, but only one close.
999 zv->zv_total_opens -= count;
1001 if (zv->zv_total_opens == 0)
1002 zvol_last_close(zv);
1005 mutex_exit(&spa_namespace_lock);
1010 zvol_get_done(zgd_t *zgd, int error)
1013 dmu_buf_rele(zgd->zgd_db, zgd);
1015 zfs_range_unlock(zgd->zgd_rl);
1017 if (error == 0 && zgd->zgd_bp)
1018 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1020 kmem_free(zgd, sizeof (zgd_t));
1024 * Get data to generate a TX_WRITE intent log record.
1027 zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1029 zvol_state_t *zv = arg;
1030 objset_t *os = zv->zv_objset;
1031 uint64_t object = ZVOL_OBJ;
1032 uint64_t offset = lr->lr_offset;
1033 uint64_t size = lr->lr_length; /* length of user data */
1034 blkptr_t *bp = &lr->lr_blkptr;
1039 ASSERT(zio != NULL);
1042 zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1043 zgd->zgd_zilog = zv->zv_zilog;
1044 zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
1047 * Write records come in two flavors: immediate and indirect.
1048 * For small writes it's cheaper to store the data with the
1049 * log record (immediate); for large writes it's cheaper to
1050 * sync the data and get a pointer to it (indirect) so that
1051 * we don't have to write the data twice.
1053 if (buf != NULL) { /* immediate write */
1054 error = dmu_read(os, object, offset, size, buf,
1055 DMU_READ_NO_PREFETCH);
1057 size = zv->zv_volblocksize;
1058 offset = P2ALIGN(offset, size);
1059 error = dmu_buf_hold(os, object, offset, zgd, &db,
1060 DMU_READ_NO_PREFETCH);
1062 blkptr_t *obp = dmu_buf_get_blkptr(db);
1064 ASSERT(BP_IS_HOLE(bp));
1071 ASSERT(db->db_offset == offset);
1072 ASSERT(db->db_size == size);
1074 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1075 zvol_get_done, zgd);
1082 zvol_get_done(zgd, error);
1088 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
1090 * We store data in the log buffers if it's small enough.
1091 * Otherwise we will later flush the data out via dmu_sync().
1093 ssize_t zvol_immediate_write_sz = 32768;
1096 zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid,
1099 uint32_t blocksize = zv->zv_volblocksize;
1100 zilog_t *zilog = zv->zv_zilog;
1102 ssize_t immediate_write_sz;
1104 if (zil_replaying(zilog, tx))
1107 immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
1108 ? 0 : zvol_immediate_write_sz;
1110 slogging = spa_has_slogs(zilog->zl_spa) &&
1111 (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
1117 itx_wr_state_t write_state;
1120 * Unlike zfs_log_write() we can be called with
1121 * upto DMU_MAX_ACCESS/2 (5MB) writes.
1123 if (blocksize > immediate_write_sz && !slogging &&
1124 resid >= blocksize && off % blocksize == 0) {
1125 write_state = WR_INDIRECT; /* uses dmu_sync */
1128 write_state = WR_COPIED;
1129 len = MIN(ZIL_MAX_LOG_DATA, resid);
1131 write_state = WR_NEED_COPY;
1132 len = MIN(ZIL_MAX_LOG_DATA, resid);
1135 itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
1136 (write_state == WR_COPIED ? len : 0));
1137 lr = (lr_write_t *)&itx->itx_lr;
1138 if (write_state == WR_COPIED && dmu_read(zv->zv_objset,
1139 ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
1140 zil_itx_destroy(itx);
1141 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1142 lr = (lr_write_t *)&itx->itx_lr;
1143 write_state = WR_NEED_COPY;
1146 itx->itx_wr_state = write_state;
1147 if (write_state == WR_NEED_COPY)
1148 itx->itx_sod += len;
1149 lr->lr_foid = ZVOL_OBJ;
1150 lr->lr_offset = off;
1151 lr->lr_length = len;
1153 BP_ZERO(&lr->lr_blkptr);
1155 itx->itx_private = zv;
1156 itx->itx_sync = sync;
1158 zil_itx_assign(zilog, itx, tx);
1167 zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t size,
1168 boolean_t doread, boolean_t isdump)
1174 for (c = 0; c < vd->vdev_children; c++) {
1175 ASSERT(vd->vdev_ops == &vdev_mirror_ops ||
1176 vd->vdev_ops == &vdev_replacing_ops ||
1177 vd->vdev_ops == &vdev_spare_ops);
1178 int err = zvol_dumpio_vdev(vd->vdev_child[c],
1179 addr, offset, size, doread, isdump);
1182 } else if (doread) {
1187 if (!vd->vdev_ops->vdev_op_leaf)
1188 return (numerrors < vd->vdev_children ? 0 : EIO);
1190 if (doread && !vdev_readable(vd))
1191 return (SET_ERROR(EIO));
1192 else if (!doread && !vdev_writeable(vd))
1193 return (SET_ERROR(EIO));
1196 ASSERT3P(dvd, !=, NULL);
1197 offset += VDEV_LABEL_START_SIZE;
1199 if (ddi_in_panic() || isdump) {
1202 return (SET_ERROR(EIO));
1203 return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset),
1206 return (vdev_disk_physio(dvd->vd_lh, addr, size, offset,
1207 doread ? B_READ : B_WRITE));
1212 zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size,
1213 boolean_t doread, boolean_t isdump)
1218 spa_t *spa = dmu_objset_spa(zv->zv_objset);
1220 /* Must be sector aligned, and not stradle a block boundary. */
1221 if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) ||
1222 P2BOUNDARY(offset, size, zv->zv_volblocksize)) {
1223 return (SET_ERROR(EINVAL));
1225 ASSERT(size <= zv->zv_volblocksize);
1227 /* Locate the extent this belongs to */
1228 ze = list_head(&zv->zv_extents);
1229 while (offset >= ze->ze_nblks * zv->zv_volblocksize) {
1230 offset -= ze->ze_nblks * zv->zv_volblocksize;
1231 ze = list_next(&zv->zv_extents, ze);
1235 return (SET_ERROR(EINVAL));
1237 if (!ddi_in_panic())
1238 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
1240 vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva));
1241 offset += DVA_GET_OFFSET(&ze->ze_dva);
1242 error = zvol_dumpio_vdev(vd, addr, offset, size, doread, isdump);
1244 if (!ddi_in_panic())
1245 spa_config_exit(spa, SCL_STATE, FTAG);
1252 zvol_strategy(struct bio *bp)
1254 zvol_state_t *zv = bp->bio_to->private;
1255 uint64_t off, volsize;
1261 boolean_t doread = (bp->bio_cmd == BIO_READ);
1265 g_io_deliver(bp, ENXIO);
1269 if (bp->bio_cmd != BIO_READ && (zv->zv_flags & ZVOL_RDONLY)) {
1270 g_io_deliver(bp, EROFS);
1274 off = bp->bio_offset;
1275 volsize = zv->zv_volsize;
1280 addr = bp->bio_data;
1281 resid = bp->bio_length;
1283 if (resid > 0 && (off < 0 || off >= volsize)) {
1284 g_io_deliver(bp, EIO);
1288 sync = !doread && zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS;
1291 * There must be no buffer changes when doing a dmu_sync() because
1292 * we can't change the data whilst calculating the checksum.
1294 rl = zfs_range_lock(&zv->zv_znode, off, resid,
1295 doread ? RL_READER : RL_WRITER);
1297 while (resid != 0 && off < volsize) {
1298 size_t size = MIN(resid, zvol_maxphys);
1300 error = dmu_read(os, ZVOL_OBJ, off, size, addr,
1303 dmu_tx_t *tx = dmu_tx_create(os);
1304 dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
1305 error = dmu_tx_assign(tx, TXG_WAIT);
1309 dmu_write(os, ZVOL_OBJ, off, size, addr, tx);
1310 zvol_log_write(zv, tx, off, size, sync);
1315 /* convert checksum errors into IO errors */
1316 if (error == ECKSUM)
1317 error = SET_ERROR(EIO);
1324 zfs_range_unlock(rl);
1326 bp->bio_completed = bp->bio_length - resid;
1327 if (bp->bio_completed < bp->bio_length)
1328 bp->bio_error = (off > volsize ? EINVAL : error);
1331 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1332 g_io_deliver(bp, 0);
1339 * Set the buffer count to the zvol maximum transfer.
1340 * Using our own routine instead of the default minphys()
1341 * means that for larger writes we write bigger buffers on X86
1342 * (128K instead of 56K) and flush the disk write cache less often
1343 * (every zvol_maxphys - currently 1MB) instead of minphys (currently
1344 * 56K on X86 and 128K on sparc).
1347 zvol_minphys(struct buf *bp)
1349 if (bp->b_bcount > zvol_maxphys)
1350 bp->b_bcount = zvol_maxphys;
1354 zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks)
1356 minor_t minor = getminor(dev);
1363 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1365 return (SET_ERROR(ENXIO));
1367 if ((zv->zv_flags & ZVOL_DUMPIFIED) == 0)
1368 return (SET_ERROR(EINVAL));
1370 boff = ldbtob(blkno);
1371 resid = ldbtob(nblocks);
1373 VERIFY3U(boff + resid, <=, zv->zv_volsize);
1376 size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff);
1377 error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE);
1390 zvol_read(dev_t dev, uio_t *uio, cred_t *cr)
1392 minor_t minor = getminor(dev);
1398 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1400 return (SET_ERROR(ENXIO));
1402 volsize = zv->zv_volsize;
1403 if (uio->uio_resid > 0 &&
1404 (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
1405 return (SET_ERROR(EIO));
1407 if (zv->zv_flags & ZVOL_DUMPIFIED) {
1408 error = physio(zvol_strategy, NULL, dev, B_READ,
1413 rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1415 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1416 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1418 /* don't read past the end */
1419 if (bytes > volsize - uio->uio_loffset)
1420 bytes = volsize - uio->uio_loffset;
1422 error = dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes);
1424 /* convert checksum errors into IO errors */
1425 if (error == ECKSUM)
1426 error = SET_ERROR(EIO);
1430 zfs_range_unlock(rl);
1436 zvol_write(dev_t dev, uio_t *uio, cred_t *cr)
1438 minor_t minor = getminor(dev);
1445 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1447 return (SET_ERROR(ENXIO));
1449 volsize = zv->zv_volsize;
1450 if (uio->uio_resid > 0 &&
1451 (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
1452 return (SET_ERROR(EIO));
1454 if (zv->zv_flags & ZVOL_DUMPIFIED) {
1455 error = physio(zvol_strategy, NULL, dev, B_WRITE,
1460 sync = !(zv->zv_flags & ZVOL_WCE) ||
1461 (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS);
1463 rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1465 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1466 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1467 uint64_t off = uio->uio_loffset;
1468 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
1470 if (bytes > volsize - off) /* don't write past the end */
1471 bytes = volsize - off;
1473 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
1474 error = dmu_tx_assign(tx, TXG_WAIT);
1479 error = dmu_write_uio_dbuf(zv->zv_dbuf, uio, bytes, tx);
1481 zvol_log_write(zv, tx, off, bytes, sync);
1487 zfs_range_unlock(rl);
1489 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1494 zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs)
1496 struct uuid uuid = EFI_RESERVED;
1497 efi_gpe_t gpe = { 0 };
1503 if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag))
1504 return (SET_ERROR(EFAULT));
1505 ptr = (char *)(uintptr_t)efi.dki_data_64;
1506 length = efi.dki_length;
1508 * Some clients may attempt to request a PMBR for the
1509 * zvol. Currently this interface will return EINVAL to
1510 * such requests. These requests could be supported by
1511 * adding a check for lba == 0 and consing up an appropriate
1514 if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0)
1515 return (SET_ERROR(EINVAL));
1517 gpe.efi_gpe_StartingLBA = LE_64(34ULL);
1518 gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1);
1519 UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
1521 if (efi.dki_lba == 1) {
1522 efi_gpt_t gpt = { 0 };
1524 gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
1525 gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
1526 gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt));
1527 gpt.efi_gpt_MyLBA = LE_64(1ULL);
1528 gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL);
1529 gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1);
1530 gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL);
1531 gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
1532 gpt.efi_gpt_SizeOfPartitionEntry =
1533 LE_32(sizeof (efi_gpe_t));
1534 CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
1535 gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
1536 CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table);
1537 gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
1538 if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length),
1540 return (SET_ERROR(EFAULT));
1541 ptr += sizeof (gpt);
1542 length -= sizeof (gpt);
1544 if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe),
1546 return (SET_ERROR(EFAULT));
1551 * BEGIN entry points to allow external callers access to the volume.
1554 * Return the volume parameters needed for access from an external caller.
1555 * These values are invariant as long as the volume is held open.
1558 zvol_get_volume_params(minor_t minor, uint64_t *blksize,
1559 uint64_t *max_xfer_len, void **minor_hdl, void **objset_hdl, void **zil_hdl,
1560 void **rl_hdl, void **bonus_hdl)
1564 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1566 return (SET_ERROR(ENXIO));
1567 if (zv->zv_flags & ZVOL_DUMPIFIED)
1568 return (SET_ERROR(ENXIO));
1570 ASSERT(blksize && max_xfer_len && minor_hdl &&
1571 objset_hdl && zil_hdl && rl_hdl && bonus_hdl);
1573 *blksize = zv->zv_volblocksize;
1574 *max_xfer_len = (uint64_t)zvol_maxphys;
1576 *objset_hdl = zv->zv_objset;
1577 *zil_hdl = zv->zv_zilog;
1578 *rl_hdl = &zv->zv_znode;
1579 *bonus_hdl = zv->zv_dbuf;
1584 * Return the current volume size to an external caller.
1585 * The size can change while the volume is open.
1588 zvol_get_volume_size(void *minor_hdl)
1590 zvol_state_t *zv = minor_hdl;
1592 return (zv->zv_volsize);
1596 * Return the current WCE setting to an external caller.
1597 * The WCE setting can change while the volume is open.
1600 zvol_get_volume_wce(void *minor_hdl)
1602 zvol_state_t *zv = minor_hdl;
1604 return ((zv->zv_flags & ZVOL_WCE) ? 1 : 0);
1608 * Entry point for external callers to zvol_log_write
1611 zvol_log_write_minor(void *minor_hdl, dmu_tx_t *tx, offset_t off, ssize_t resid,
1614 zvol_state_t *zv = minor_hdl;
1616 zvol_log_write(zv, tx, off, resid, sync);
1619 * END entry points to allow external callers access to the volume.
1623 * Dirtbag ioctls to support mkfs(1M) for UFS filesystems. See dkio(7I).
1627 zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
1630 struct dk_cinfo dki;
1631 struct dk_minfo dkm;
1632 struct dk_callback *dkc;
1636 mutex_enter(&spa_namespace_lock);
1638 zv = zfsdev_get_soft_state(getminor(dev), ZSST_ZVOL);
1641 mutex_exit(&spa_namespace_lock);
1642 return (SET_ERROR(ENXIO));
1644 ASSERT(zv->zv_total_opens > 0);
1649 bzero(&dki, sizeof (dki));
1650 (void) strcpy(dki.dki_cname, "zvol");
1651 (void) strcpy(dki.dki_dname, "zvol");
1652 dki.dki_ctype = DKC_UNKNOWN;
1653 dki.dki_unit = getminor(dev);
1654 dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs);
1655 mutex_exit(&spa_namespace_lock);
1656 if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag))
1657 error = SET_ERROR(EFAULT);
1660 case DKIOCGMEDIAINFO:
1661 bzero(&dkm, sizeof (dkm));
1662 dkm.dki_lbsize = 1U << zv->zv_min_bs;
1663 dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
1664 dkm.dki_media_type = DK_UNKNOWN;
1665 mutex_exit(&spa_namespace_lock);
1666 if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
1667 error = SET_ERROR(EFAULT);
1672 uint64_t vs = zv->zv_volsize;
1673 uint8_t bs = zv->zv_min_bs;
1675 mutex_exit(&spa_namespace_lock);
1676 error = zvol_getefi((void *)arg, flag, vs, bs);
1680 case DKIOCFLUSHWRITECACHE:
1681 dkc = (struct dk_callback *)arg;
1682 mutex_exit(&spa_namespace_lock);
1683 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1684 if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) {
1685 (*dkc->dkc_callback)(dkc->dkc_cookie, error);
1692 int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0;
1693 if (ddi_copyout(&wce, (void *)arg, sizeof (int),
1695 error = SET_ERROR(EFAULT);
1701 if (ddi_copyin((void *)arg, &wce, sizeof (int),
1703 error = SET_ERROR(EFAULT);
1707 zv->zv_flags |= ZVOL_WCE;
1708 mutex_exit(&spa_namespace_lock);
1710 zv->zv_flags &= ~ZVOL_WCE;
1711 mutex_exit(&spa_namespace_lock);
1712 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1720 * commands using these (like prtvtoc) expect ENOTSUP
1721 * since we're emulating an EFI label
1723 error = SET_ERROR(ENOTSUP);
1727 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1729 error = zvol_dumpify(zv);
1730 zfs_range_unlock(rl);
1734 if (!(zv->zv_flags & ZVOL_DUMPIFIED))
1736 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1738 error = zvol_dump_fini(zv);
1739 zfs_range_unlock(rl);
1747 if (ddi_copyin((void *)arg, &df, sizeof (df), flag)) {
1748 error = SET_ERROR(EFAULT);
1753 * Apply Postel's Law to length-checking. If they overshoot,
1754 * just blank out until the end, if there's a need to blank
1757 if (df.df_start >= zv->zv_volsize)
1758 break; /* No need to do anything... */
1759 if (df.df_start + df.df_length > zv->zv_volsize)
1760 df.df_length = DMU_OBJECT_END;
1762 rl = zfs_range_lock(&zv->zv_znode, df.df_start, df.df_length,
1764 tx = dmu_tx_create(zv->zv_objset);
1765 error = dmu_tx_assign(tx, TXG_WAIT);
1769 zvol_log_truncate(zv, tx, df.df_start,
1770 df.df_length, B_TRUE);
1772 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ,
1773 df.df_start, df.df_length);
1776 zfs_range_unlock(rl);
1780 * If the write-cache is disabled or 'sync' property
1781 * is set to 'always' then treat this as a synchronous
1782 * operation (i.e. commit to zil).
1784 if (!(zv->zv_flags & ZVOL_WCE) ||
1785 (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS))
1786 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1789 * If the caller really wants synchronous writes, and
1790 * can't wait for them, don't return until the write
1793 if (df.df_flags & DF_WAIT_SYNC) {
1795 dmu_objset_pool(zv->zv_objset), 0);
1802 error = SET_ERROR(ENOTTY);
1806 mutex_exit(&spa_namespace_lock);
1814 return (zvol_minors != 0);
1820 VERIFY(ddi_soft_state_init(&zfsdev_state, sizeof (zfs_soft_state_t),
1822 ZFS_LOG(1, "ZVOL Initialized.");
1828 ddi_soft_state_fini(&zfsdev_state);
1829 ZFS_LOG(1, "ZVOL Deinitialized.");
1834 zvol_dump_init(zvol_state_t *zv, boolean_t resize)
1838 objset_t *os = zv->zv_objset;
1839 nvlist_t *nv = NULL;
1840 uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset));
1842 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1843 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0,
1845 /* wait for dmu_free_long_range to actually free the blocks */
1846 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
1848 tx = dmu_tx_create(os);
1849 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1850 dmu_tx_hold_bonus(tx, ZVOL_OBJ);
1851 error = dmu_tx_assign(tx, TXG_WAIT);
1858 * If we are resizing the dump device then we only need to
1859 * update the refreservation to match the newly updated
1860 * zvolsize. Otherwise, we save off the original state of the
1861 * zvol so that we can restore them if the zvol is ever undumpified.
1864 error = zap_update(os, ZVOL_ZAP_OBJ,
1865 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1866 &zv->zv_volsize, tx);
1868 uint64_t checksum, compress, refresrv, vbs, dedup;
1870 error = dsl_prop_get_integer(zv->zv_name,
1871 zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL);
1872 error = error ? error : dsl_prop_get_integer(zv->zv_name,
1873 zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL);
1874 error = error ? error : dsl_prop_get_integer(zv->zv_name,
1875 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL);
1876 error = error ? error : dsl_prop_get_integer(zv->zv_name,
1877 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL);
1878 if (version >= SPA_VERSION_DEDUP) {
1879 error = error ? error :
1880 dsl_prop_get_integer(zv->zv_name,
1881 zfs_prop_to_name(ZFS_PROP_DEDUP), &dedup, NULL);
1884 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1885 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1,
1887 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1888 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx);
1889 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1890 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1892 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1893 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1,
1895 error = error ? error : dmu_object_set_blocksize(
1896 os, ZVOL_OBJ, SPA_MAXBLOCKSIZE, 0, tx);
1897 if (version >= SPA_VERSION_DEDUP) {
1898 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1899 zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1,
1903 zv->zv_volblocksize = SPA_MAXBLOCKSIZE;
1908 * We only need update the zvol's property if we are initializing
1909 * the dump area for the first time.
1912 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1913 VERIFY(nvlist_add_uint64(nv,
1914 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0);
1915 VERIFY(nvlist_add_uint64(nv,
1916 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
1917 ZIO_COMPRESS_OFF) == 0);
1918 VERIFY(nvlist_add_uint64(nv,
1919 zfs_prop_to_name(ZFS_PROP_CHECKSUM),
1920 ZIO_CHECKSUM_OFF) == 0);
1921 if (version >= SPA_VERSION_DEDUP) {
1922 VERIFY(nvlist_add_uint64(nv,
1923 zfs_prop_to_name(ZFS_PROP_DEDUP),
1924 ZIO_CHECKSUM_OFF) == 0);
1927 error = zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
1935 /* Allocate the space for the dump */
1936 error = zvol_prealloc(zv);
1941 zvol_dumpify(zvol_state_t *zv)
1944 uint64_t dumpsize = 0;
1946 objset_t *os = zv->zv_objset;
1948 if (zv->zv_flags & ZVOL_RDONLY)
1949 return (SET_ERROR(EROFS));
1951 if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE,
1952 8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) {
1953 boolean_t resize = (dumpsize > 0);
1955 if ((error = zvol_dump_init(zv, resize)) != 0) {
1956 (void) zvol_dump_fini(zv);
1962 * Build up our lba mapping.
1964 error = zvol_get_lbas(zv);
1966 (void) zvol_dump_fini(zv);
1970 tx = dmu_tx_create(os);
1971 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1972 error = dmu_tx_assign(tx, TXG_WAIT);
1975 (void) zvol_dump_fini(zv);
1979 zv->zv_flags |= ZVOL_DUMPIFIED;
1980 error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1,
1981 &zv->zv_volsize, tx);
1985 (void) zvol_dump_fini(zv);
1989 txg_wait_synced(dmu_objset_pool(os), 0);
1994 zvol_dump_fini(zvol_state_t *zv)
1997 objset_t *os = zv->zv_objset;
2000 uint64_t checksum, compress, refresrv, vbs, dedup;
2001 uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset));
2004 * Attempt to restore the zvol back to its pre-dumpified state.
2005 * This is a best-effort attempt as it's possible that not all
2006 * of these properties were initialized during the dumpify process
2007 * (i.e. error during zvol_dump_init).
2010 tx = dmu_tx_create(os);
2011 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
2012 error = dmu_tx_assign(tx, TXG_WAIT);
2017 (void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx);
2020 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2021 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum);
2022 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2023 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress);
2024 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2025 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv);
2026 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2027 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs);
2029 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2030 (void) nvlist_add_uint64(nv,
2031 zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum);
2032 (void) nvlist_add_uint64(nv,
2033 zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress);
2034 (void) nvlist_add_uint64(nv,
2035 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv);
2036 if (version >= SPA_VERSION_DEDUP &&
2037 zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2038 zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, &dedup) == 0) {
2039 (void) nvlist_add_uint64(nv,
2040 zfs_prop_to_name(ZFS_PROP_DEDUP), dedup);
2042 (void) zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
2046 zvol_free_extents(zv);
2047 zv->zv_flags &= ~ZVOL_DUMPIFIED;
2048 (void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END);
2049 /* wait for dmu_free_long_range to actually free the blocks */
2050 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
2051 tx = dmu_tx_create(os);
2052 dmu_tx_hold_bonus(tx, ZVOL_OBJ);
2053 error = dmu_tx_assign(tx, TXG_WAIT);
2058 if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0)
2059 zv->zv_volblocksize = vbs;
2066 static zvol_state_t *
2067 zvol_geom_create(const char *name)
2069 struct g_provider *pp;
2073 gp = g_new_geomf(&zfs_zvol_class, "zfs::zvol::%s", name);
2074 gp->start = zvol_geom_start;
2075 gp->access = zvol_geom_access;
2076 pp = g_new_providerf(gp, "%s/%s", ZVOL_DRIVER, name);
2077 pp->sectorsize = DEV_BSIZE;
2079 zv = kmem_zalloc(sizeof(*zv), KM_SLEEP);
2080 zv->zv_provider = pp;
2082 bioq_init(&zv->zv_queue);
2083 mtx_init(&zv->zv_queue_mtx, "zvol", NULL, MTX_DEF);
2091 zvol_geom_run(zvol_state_t *zv)
2093 struct g_provider *pp;
2095 pp = zv->zv_provider;
2096 g_error_provider(pp, 0);
2098 kproc_kthread_add(zvol_geom_worker, zv, &zfsproc, NULL, 0, 0,
2099 "zfskern", "zvol %s", pp->name + sizeof(ZVOL_DRIVER));
2103 zvol_geom_destroy(zvol_state_t *zv)
2105 struct g_provider *pp;
2107 g_topology_assert();
2109 mtx_lock(&zv->zv_queue_mtx);
2111 wakeup_one(&zv->zv_queue);
2112 while (zv->zv_state != 2)
2113 msleep(&zv->zv_state, &zv->zv_queue_mtx, 0, "zvol:w", 0);
2114 mtx_destroy(&zv->zv_queue_mtx);
2116 pp = zv->zv_provider;
2117 zv->zv_provider = NULL;
2119 g_wither_geom(pp->geom, ENXIO);
2121 kmem_free(zv, sizeof(*zv));
2125 zvol_geom_access(struct g_provider *pp, int acr, int acw, int ace)
2127 int count, error, flags;
2129 g_topology_assert();
2132 * To make it easier we expect either open or close, but not both
2135 KASSERT((acr >= 0 && acw >= 0 && ace >= 0) ||
2136 (acr <= 0 && acw <= 0 && ace <= 0),
2137 ("Unsupported access request to %s (acr=%d, acw=%d, ace=%d).",
2138 pp->name, acr, acw, ace));
2140 if (pp->private == NULL) {
2141 if (acr <= 0 && acw <= 0 && ace <= 0)
2147 * We don't pass FEXCL flag to zvol_open()/zvol_close() if ace != 0,
2148 * because GEOM already handles that and handles it a bit differently.
2149 * GEOM allows for multiple read/exclusive consumers and ZFS allows
2150 * only one exclusive consumer, no matter if it is reader or writer.
2151 * I like better the way GEOM works so I'll leave it for GEOM to
2152 * decide what to do.
2155 count = acr + acw + ace;
2160 if (acr != 0 || ace != 0)
2165 g_topology_unlock();
2167 error = zvol_open(pp, flags, count);
2169 error = zvol_close(pp, flags, -count);
2175 zvol_geom_start(struct bio *bp)
2180 switch (bp->bio_cmd) {
2184 zv = bp->bio_to->private;
2186 mtx_lock(&zv->zv_queue_mtx);
2187 first = (bioq_first(&zv->zv_queue) == NULL);
2188 bioq_insert_tail(&zv->zv_queue, bp);
2189 mtx_unlock(&zv->zv_queue_mtx);
2191 wakeup_one(&zv->zv_queue);
2196 g_io_deliver(bp, EOPNOTSUPP);
2202 zvol_geom_worker(void *arg)
2207 thread_lock(curthread);
2208 sched_prio(curthread, PRIBIO);
2209 thread_unlock(curthread);
2213 mtx_lock(&zv->zv_queue_mtx);
2214 bp = bioq_takefirst(&zv->zv_queue);
2216 if (zv->zv_state == 1) {
2218 wakeup(&zv->zv_state);
2219 mtx_unlock(&zv->zv_queue_mtx);
2222 msleep(&zv->zv_queue, &zv->zv_queue_mtx, PRIBIO | PDROP,
2226 mtx_unlock(&zv->zv_queue_mtx);
2227 switch (bp->bio_cmd) {
2229 zil_commit(zv->zv_zilog, ZVOL_OBJ);
2230 g_io_deliver(bp, 0);
2240 extern boolean_t dataset_name_hidden(const char *name);
2243 zvol_create_snapshots(objset_t *os, const char *name)
2245 uint64_t cookie, obj;
2250 sname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2253 (void) dmu_objset_find(name, dmu_objset_prefetch, NULL,
2258 len = snprintf(sname, MAXPATHLEN, "%s@", name);
2259 if (len >= MAXPATHLEN) {
2260 dmu_objset_rele(os, FTAG);
2261 error = ENAMETOOLONG;
2265 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
2266 error = dmu_snapshot_list_next(os, MAXPATHLEN - len,
2267 sname + len, &obj, &cookie, NULL);
2268 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
2270 if (error == ENOENT)
2275 if ((error = zvol_create_minor(sname)) != 0) {
2276 printf("ZFS WARNING: Unable to create ZVOL %s (error=%d).\n",
2282 kmem_free(sname, MAXPATHLEN);
2287 zvol_create_minors(const char *name)
2294 if (dataset_name_hidden(name))
2297 if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) {
2298 printf("ZFS WARNING: Unable to put hold on %s (error=%d).\n",
2302 if (dmu_objset_type(os) == DMU_OST_ZVOL) {
2303 dsl_dataset_long_hold(os->os_dsl_dataset, FTAG);
2304 dsl_pool_rele(dmu_objset_pool(os), FTAG);
2305 if ((error = zvol_create_minor(name)) == 0)
2306 error = zvol_create_snapshots(os, name);
2308 printf("ZFS WARNING: Unable to create ZVOL %s (error=%d).\n",
2311 dsl_dataset_long_rele(os->os_dsl_dataset, FTAG);
2312 dsl_dataset_rele(os->os_dsl_dataset, FTAG);
2315 if (dmu_objset_type(os) != DMU_OST_ZFS) {
2316 dmu_objset_rele(os, FTAG);
2320 osname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2321 if (snprintf(osname, MAXPATHLEN, "%s/", name) >= MAXPATHLEN) {
2322 dmu_objset_rele(os, FTAG);
2323 kmem_free(osname, MAXPATHLEN);
2326 p = osname + strlen(osname);
2327 len = MAXPATHLEN - (p - osname);
2330 /* Prefetch the datasets. */
2332 while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0) {
2333 if (!dataset_name_hidden(osname))
2334 (void) dmu_objset_prefetch(osname, NULL);
2339 while (dmu_dir_list_next(os, MAXPATHLEN - (p - osname), p, NULL,
2341 dmu_objset_rele(os, FTAG);
2342 (void)zvol_create_minors(osname);
2343 if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) {
2344 printf("ZFS WARNING: Unable to put hold on %s (error=%d).\n",
2350 dmu_objset_rele(os, FTAG);
2351 kmem_free(osname, MAXPATHLEN);
2356 zvol_rename_minor(struct g_geom *gp, const char *newname)
2358 struct g_provider *pp;
2361 ASSERT(MUTEX_HELD(&spa_namespace_lock));
2362 g_topology_assert();
2364 pp = LIST_FIRST(&gp->provider);
2369 zv->zv_provider = NULL;
2370 g_wither_provider(pp, ENXIO);
2372 pp = g_new_providerf(gp, "%s/%s", ZVOL_DRIVER, newname);
2373 pp->sectorsize = DEV_BSIZE;
2374 pp->mediasize = zv->zv_volsize;
2376 zv->zv_provider = pp;
2377 strlcpy(zv->zv_name, newname, sizeof(zv->zv_name));
2378 g_error_provider(pp, 0);
2382 zvol_rename_minors(const char *oldname, const char *newname)
2384 char name[MAXPATHLEN];
2385 struct g_provider *pp;
2387 size_t oldnamelen, newnamelen;
2391 oldnamelen = strlen(oldname);
2392 newnamelen = strlen(newname);
2395 mutex_enter(&spa_namespace_lock);
2398 LIST_FOREACH(gp, &zfs_zvol_class.geom, geom) {
2399 pp = LIST_FIRST(&gp->provider);
2405 if (strcmp(zv->zv_name, oldname) == 0) {
2406 zvol_rename_minor(gp, newname);
2407 } else if (strncmp(zv->zv_name, oldname, oldnamelen) == 0 &&
2408 (zv->zv_name[oldnamelen] == '/' ||
2409 zv->zv_name[oldnamelen] == '@')) {
2410 snprintf(name, sizeof(name), "%s%c%s", newname,
2411 zv->zv_name[oldnamelen],
2412 zv->zv_name + oldnamelen + 1);
2413 zvol_rename_minor(gp, name);
2417 g_topology_unlock();
2418 mutex_exit(&spa_namespace_lock);