xref: /illumos-gate/usr/src/uts/common/fs/zfs/zvol.c (revision e09fa4dacfb671e707d50a55ae9b5cc191e1b8cb)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
22e08bf2c6SEric Taylor  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens /*
27fa9e4066Sahrens  * ZFS volume emulation driver.
28fa9e4066Sahrens  *
29fa9e4066Sahrens  * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes.
30fa9e4066Sahrens  * Volumes are accessed through the symbolic links named:
31fa9e4066Sahrens  *
32fa9e4066Sahrens  * /dev/zvol/dsk/<pool_name>/<dataset_name>
33fa9e4066Sahrens  * /dev/zvol/rdsk/<pool_name>/<dataset_name>
34fa9e4066Sahrens  *
35fa9e4066Sahrens  * These links are created by the ZFS-specific devfsadm link generator.
36fa9e4066Sahrens  * Volumes are persistent through reboot.  No user command needs to be
37fa9e4066Sahrens  * run before opening and using a device.
38fa9e4066Sahrens  */
39fa9e4066Sahrens 
40fa9e4066Sahrens #include <sys/types.h>
41fa9e4066Sahrens #include <sys/param.h>
42fa9e4066Sahrens #include <sys/errno.h>
43fa9e4066Sahrens #include <sys/uio.h>
44fa9e4066Sahrens #include <sys/buf.h>
45fa9e4066Sahrens #include <sys/modctl.h>
46fa9e4066Sahrens #include <sys/open.h>
47fa9e4066Sahrens #include <sys/kmem.h>
48fa9e4066Sahrens #include <sys/conf.h>
49fa9e4066Sahrens #include <sys/cmn_err.h>
50fa9e4066Sahrens #include <sys/stat.h>
51fa9e4066Sahrens #include <sys/zap.h>
52fa9e4066Sahrens #include <sys/spa.h>
53fa9e4066Sahrens #include <sys/zio.h>
54e7cbe64fSgw25295 #include <sys/dmu_traverse.h>
55e7cbe64fSgw25295 #include <sys/dnode.h>
56e7cbe64fSgw25295 #include <sys/dsl_dataset.h>
57fa9e4066Sahrens #include <sys/dsl_prop.h>
58fa9e4066Sahrens #include <sys/dkio.h>
59fa9e4066Sahrens #include <sys/efi_partition.h>
60fa9e4066Sahrens #include <sys/byteorder.h>
61fa9e4066Sahrens #include <sys/pathname.h>
62fa9e4066Sahrens #include <sys/ddi.h>
63fa9e4066Sahrens #include <sys/sunddi.h>
64fa9e4066Sahrens #include <sys/crc32.h>
65fa9e4066Sahrens #include <sys/dirent.h>
66fa9e4066Sahrens #include <sys/policy.h>
67fa9e4066Sahrens #include <sys/fs/zfs.h>
68fa9e4066Sahrens #include <sys/zfs_ioctl.h>
69fa9e4066Sahrens #include <sys/mkdev.h>
7022ac5be4Sperrin #include <sys/zil.h>
71c5c6ffa0Smaybee #include <sys/refcount.h>
72c2e6a7d6Sperrin #include <sys/zfs_znode.h>
73c2e6a7d6Sperrin #include <sys/zfs_rlock.h>
74e7cbe64fSgw25295 #include <sys/vdev_disk.h>
75e7cbe64fSgw25295 #include <sys/vdev_impl.h>
76e7cbe64fSgw25295 #include <sys/zvol.h>
77e7cbe64fSgw25295 #include <sys/dumphdr.h>
781209a471SNeil Perrin #include <sys/zil_impl.h>
79fa9e4066Sahrens 
80fa9e4066Sahrens #include "zfs_namecheck.h"
81fa9e4066Sahrens 
82fa9e4066Sahrens static void *zvol_state;
83503ad85cSMatthew Ahrens static char *zvol_tag = "zvol_tag";
84fa9e4066Sahrens 
85e7cbe64fSgw25295 #define	ZVOL_DUMPSIZE		"dumpsize"
86e7cbe64fSgw25295 
87fa9e4066Sahrens /*
88fa9e4066Sahrens  * This lock protects the zvol_state structure from being modified
89fa9e4066Sahrens  * while it's being used, e.g. an open that comes in before a create
90fa9e4066Sahrens  * finishes.  It also protects temporary opens of the dataset so that,
91fa9e4066Sahrens  * e.g., an open doesn't get a spurious EBUSY.
92fa9e4066Sahrens  */
93fa9e4066Sahrens static kmutex_t zvol_state_lock;
94fa9e4066Sahrens static uint32_t zvol_minors;
95fa9e4066Sahrens 
96e7cbe64fSgw25295 typedef struct zvol_extent {
9788b7b0f2SMatthew Ahrens 	list_node_t	ze_node;
98e7cbe64fSgw25295 	dva_t		ze_dva;		/* dva associated with this extent */
9988b7b0f2SMatthew Ahrens 	uint64_t	ze_nblks;	/* number of blocks in extent */
100e7cbe64fSgw25295 } zvol_extent_t;
101e7cbe64fSgw25295 
102e7cbe64fSgw25295 /*
103fa9e4066Sahrens  * The in-core state of each volume.
104fa9e4066Sahrens  */
105fa9e4066Sahrens typedef struct zvol_state {
106fa9e4066Sahrens 	char		zv_name[MAXPATHLEN]; /* pool/dd name */
107fa9e4066Sahrens 	uint64_t	zv_volsize;	/* amount of space we advertise */
10867bd71c6Sperrin 	uint64_t	zv_volblocksize; /* volume block size */
109fa9e4066Sahrens 	minor_t		zv_minor;	/* minor number */
110fa9e4066Sahrens 	uint8_t		zv_min_bs;	/* minimum addressable block shift */
111701f66c4SEric Taylor 	uint8_t		zv_flags;	/* readonly, dumpified, etc. */
112fa9e4066Sahrens 	objset_t	*zv_objset;	/* objset handle */
113503ad85cSMatthew Ahrens 	boolean_t 	zv_issnap;	/* is a snapshot (read-only) */
114fa9e4066Sahrens 	uint32_t	zv_open_count[OTYPCNT];	/* open counts */
115fa9e4066Sahrens 	uint32_t	zv_total_opens;	/* total open count */
11622ac5be4Sperrin 	zilog_t		*zv_zilog;	/* ZIL handle */
11788b7b0f2SMatthew Ahrens 	list_t		zv_extents;	/* List of extents for dump */
118c2e6a7d6Sperrin 	znode_t		zv_znode;	/* for range locking */
119fa9e4066Sahrens } zvol_state_t;
120fa9e4066Sahrens 
12167bd71c6Sperrin /*
122e7cbe64fSgw25295  * zvol specific flags
123e7cbe64fSgw25295  */
124e7cbe64fSgw25295 #define	ZVOL_RDONLY	0x1
125e7cbe64fSgw25295 #define	ZVOL_DUMPIFIED	0x2
126c7f714e2SEric Taylor #define	ZVOL_EXCL	0x4
127701f66c4SEric Taylor #define	ZVOL_WCE	0x8
128e7cbe64fSgw25295 
129e7cbe64fSgw25295 /*
13067bd71c6Sperrin  * zvol maximum transfer in one DMU tx.
13167bd71c6Sperrin  */
13267bd71c6Sperrin int zvol_maxphys = DMU_MAX_ACCESS/2;
13367bd71c6Sperrin 
134e7cbe64fSgw25295 extern int zfs_set_prop_nvlist(const char *, nvlist_t *);
135feb08c6bSbillm static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio);
136e7cbe64fSgw25295 static int zvol_dumpify(zvol_state_t *zv);
137e7cbe64fSgw25295 static int zvol_dump_fini(zvol_state_t *zv);
138e7cbe64fSgw25295 static int zvol_dump_init(zvol_state_t *zv, boolean_t resize);
13967bd71c6Sperrin 
140fa9e4066Sahrens static void
14191ebeef5Sahrens zvol_size_changed(zvol_state_t *zv, major_t maj)
142fa9e4066Sahrens {
14391ebeef5Sahrens 	dev_t dev = makedevice(maj, zv->zv_minor);
144fa9e4066Sahrens 
145fa9e4066Sahrens 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
146fa9e4066Sahrens 	    "Size", zv->zv_volsize) == DDI_SUCCESS);
147fa9e4066Sahrens 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
148fa9e4066Sahrens 	    "Nblocks", lbtodb(zv->zv_volsize)) == DDI_SUCCESS);
149e7cbe64fSgw25295 
150e7cbe64fSgw25295 	/* Notify specfs to invalidate the cached size */
151e7cbe64fSgw25295 	spec_size_invalidate(dev, VBLK);
152e7cbe64fSgw25295 	spec_size_invalidate(dev, VCHR);
153fa9e4066Sahrens }
154fa9e4066Sahrens 
155fa9e4066Sahrens int
156e9dbad6fSeschrock zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
157fa9e4066Sahrens {
158e9dbad6fSeschrock 	if (volsize == 0)
159fa9e4066Sahrens 		return (EINVAL);
160fa9e4066Sahrens 
161e9dbad6fSeschrock 	if (volsize % blocksize != 0)
1625c5460e9Seschrock 		return (EINVAL);
1635c5460e9Seschrock 
164fa9e4066Sahrens #ifdef _ILP32
165e9dbad6fSeschrock 	if (volsize - 1 > SPEC_MAXOFFSET_T)
166fa9e4066Sahrens 		return (EOVERFLOW);
167fa9e4066Sahrens #endif
168fa9e4066Sahrens 	return (0);
169fa9e4066Sahrens }
170fa9e4066Sahrens 
171fa9e4066Sahrens int
172e9dbad6fSeschrock zvol_check_volblocksize(uint64_t volblocksize)
173fa9e4066Sahrens {
174e9dbad6fSeschrock 	if (volblocksize < SPA_MINBLOCKSIZE ||
175e9dbad6fSeschrock 	    volblocksize > SPA_MAXBLOCKSIZE ||
176e9dbad6fSeschrock 	    !ISP2(volblocksize))
177fa9e4066Sahrens 		return (EDOM);
178fa9e4066Sahrens 
179fa9e4066Sahrens 	return (0);
180fa9e4066Sahrens }
181fa9e4066Sahrens 
182fa9e4066Sahrens static void
183fa9e4066Sahrens zvol_readonly_changed_cb(void *arg, uint64_t newval)
184fa9e4066Sahrens {
185fa9e4066Sahrens 	zvol_state_t *zv = arg;
186fa9e4066Sahrens 
187e7cbe64fSgw25295 	if (newval)
188e7cbe64fSgw25295 		zv->zv_flags |= ZVOL_RDONLY;
189e7cbe64fSgw25295 	else
190e7cbe64fSgw25295 		zv->zv_flags &= ~ZVOL_RDONLY;
191fa9e4066Sahrens }
192fa9e4066Sahrens 
193fa9e4066Sahrens int
194a2eea2e1Sahrens zvol_get_stats(objset_t *os, nvlist_t *nv)
195fa9e4066Sahrens {
196fa9e4066Sahrens 	int error;
197fa9e4066Sahrens 	dmu_object_info_t doi;
198a2eea2e1Sahrens 	uint64_t val;
199fa9e4066Sahrens 
200fa9e4066Sahrens 
201a2eea2e1Sahrens 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
202fa9e4066Sahrens 	if (error)
203fa9e4066Sahrens 		return (error);
204fa9e4066Sahrens 
205a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
206a2eea2e1Sahrens 
207fa9e4066Sahrens 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
208fa9e4066Sahrens 
209a2eea2e1Sahrens 	if (error == 0) {
210a2eea2e1Sahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
211a2eea2e1Sahrens 		    doi.doi_data_block_size);
212a2eea2e1Sahrens 	}
213fa9e4066Sahrens 
214fa9e4066Sahrens 	return (error);
215fa9e4066Sahrens }
216fa9e4066Sahrens 
217fa9e4066Sahrens /*
218fa9e4066Sahrens  * Find a free minor number.
219fa9e4066Sahrens  */
220fa9e4066Sahrens static minor_t
221fa9e4066Sahrens zvol_minor_alloc(void)
222fa9e4066Sahrens {
223fa9e4066Sahrens 	minor_t minor;
224fa9e4066Sahrens 
225fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&zvol_state_lock));
226fa9e4066Sahrens 
227fa9e4066Sahrens 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++)
228fa9e4066Sahrens 		if (ddi_get_soft_state(zvol_state, minor) == NULL)
229fa9e4066Sahrens 			return (minor);
230fa9e4066Sahrens 
231fa9e4066Sahrens 	return (0);
232fa9e4066Sahrens }
233fa9e4066Sahrens 
234fa9e4066Sahrens static zvol_state_t *
235e9dbad6fSeschrock zvol_minor_lookup(const char *name)
236fa9e4066Sahrens {
237fa9e4066Sahrens 	minor_t minor;
238fa9e4066Sahrens 	zvol_state_t *zv;
239fa9e4066Sahrens 
240fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&zvol_state_lock));
241fa9e4066Sahrens 
242fa9e4066Sahrens 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) {
243fa9e4066Sahrens 		zv = ddi_get_soft_state(zvol_state, minor);
244fa9e4066Sahrens 		if (zv == NULL)
245fa9e4066Sahrens 			continue;
246fa9e4066Sahrens 		if (strcmp(zv->zv_name, name) == 0)
247fa9e4066Sahrens 			break;
248fa9e4066Sahrens 	}
249fa9e4066Sahrens 
250fa9e4066Sahrens 	return (zv);
251fa9e4066Sahrens }
252fa9e4066Sahrens 
253e7cbe64fSgw25295 /* extent mapping arg */
254e7cbe64fSgw25295 struct maparg {
25588b7b0f2SMatthew Ahrens 	zvol_state_t	*ma_zv;
25688b7b0f2SMatthew Ahrens 	uint64_t	ma_blks;
257e7cbe64fSgw25295 };
258e7cbe64fSgw25295 
259e7cbe64fSgw25295 /*ARGSUSED*/
260e7cbe64fSgw25295 static int
26188b7b0f2SMatthew Ahrens zvol_map_block(spa_t *spa, blkptr_t *bp, const zbookmark_t *zb,
26288b7b0f2SMatthew Ahrens     const dnode_phys_t *dnp, void *arg)
263e7cbe64fSgw25295 {
26488b7b0f2SMatthew Ahrens 	struct maparg *ma = arg;
26588b7b0f2SMatthew Ahrens 	zvol_extent_t *ze;
26688b7b0f2SMatthew Ahrens 	int bs = ma->ma_zv->zv_volblocksize;
267e7cbe64fSgw25295 
26888b7b0f2SMatthew Ahrens 	if (bp == NULL || zb->zb_object != ZVOL_OBJ || zb->zb_level != 0)
269e7cbe64fSgw25295 		return (0);
270e7cbe64fSgw25295 
27188b7b0f2SMatthew Ahrens 	VERIFY3U(ma->ma_blks, ==, zb->zb_blkid);
27288b7b0f2SMatthew Ahrens 	ma->ma_blks++;
27388b7b0f2SMatthew Ahrens 
274e7cbe64fSgw25295 	/* Abort immediately if we have encountered gang blocks */
27588b7b0f2SMatthew Ahrens 	if (BP_IS_GANG(bp))
27688b7b0f2SMatthew Ahrens 		return (EFRAGS);
277e7cbe64fSgw25295 
278e7cbe64fSgw25295 	/*
27988b7b0f2SMatthew Ahrens 	 * See if the block is at the end of the previous extent.
280e7cbe64fSgw25295 	 */
28188b7b0f2SMatthew Ahrens 	ze = list_tail(&ma->ma_zv->zv_extents);
28288b7b0f2SMatthew Ahrens 	if (ze &&
28388b7b0f2SMatthew Ahrens 	    DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) &&
28488b7b0f2SMatthew Ahrens 	    DVA_GET_OFFSET(BP_IDENTITY(bp)) ==
28588b7b0f2SMatthew Ahrens 	    DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) {
28688b7b0f2SMatthew Ahrens 		ze->ze_nblks++;
28788b7b0f2SMatthew Ahrens 		return (0);
28888b7b0f2SMatthew Ahrens 	}
28988b7b0f2SMatthew Ahrens 
290e7cbe64fSgw25295 	dprintf_bp(bp, "%s", "next blkptr:");
29188b7b0f2SMatthew Ahrens 
292e7cbe64fSgw25295 	/* start a new extent */
29388b7b0f2SMatthew Ahrens 	ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP);
29488b7b0f2SMatthew Ahrens 	ze->ze_dva = bp->blk_dva[0];	/* structure assignment */
29588b7b0f2SMatthew Ahrens 	ze->ze_nblks = 1;
29688b7b0f2SMatthew Ahrens 	list_insert_tail(&ma->ma_zv->zv_extents, ze);
29788b7b0f2SMatthew Ahrens 	return (0);
298e7cbe64fSgw25295 }
29988b7b0f2SMatthew Ahrens 
30088b7b0f2SMatthew Ahrens static void
30188b7b0f2SMatthew Ahrens zvol_free_extents(zvol_state_t *zv)
30288b7b0f2SMatthew Ahrens {
30388b7b0f2SMatthew Ahrens 	zvol_extent_t *ze;
30488b7b0f2SMatthew Ahrens 
30588b7b0f2SMatthew Ahrens 	while (ze = list_head(&zv->zv_extents)) {
30688b7b0f2SMatthew Ahrens 		list_remove(&zv->zv_extents, ze);
30788b7b0f2SMatthew Ahrens 		kmem_free(ze, sizeof (zvol_extent_t));
30888b7b0f2SMatthew Ahrens 	}
30988b7b0f2SMatthew Ahrens }
31088b7b0f2SMatthew Ahrens 
31188b7b0f2SMatthew Ahrens static int
31288b7b0f2SMatthew Ahrens zvol_get_lbas(zvol_state_t *zv)
31388b7b0f2SMatthew Ahrens {
31488b7b0f2SMatthew Ahrens 	struct maparg	ma;
31588b7b0f2SMatthew Ahrens 	int		err;
31688b7b0f2SMatthew Ahrens 
31788b7b0f2SMatthew Ahrens 	ma.ma_zv = zv;
31888b7b0f2SMatthew Ahrens 	ma.ma_blks = 0;
31988b7b0f2SMatthew Ahrens 	zvol_free_extents(zv);
32088b7b0f2SMatthew Ahrens 
32188b7b0f2SMatthew Ahrens 	err = traverse_dataset(dmu_objset_ds(zv->zv_objset), 0,
32288b7b0f2SMatthew Ahrens 	    TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma);
32388b7b0f2SMatthew Ahrens 	if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) {
32488b7b0f2SMatthew Ahrens 		zvol_free_extents(zv);
32588b7b0f2SMatthew Ahrens 		return (err ? err : EIO);
32688b7b0f2SMatthew Ahrens 	}
32788b7b0f2SMatthew Ahrens 
328e7cbe64fSgw25295 	return (0);
329e7cbe64fSgw25295 }
330e7cbe64fSgw25295 
331ecd6cf80Smarks /* ARGSUSED */
332fa9e4066Sahrens void
333ecd6cf80Smarks zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
334fa9e4066Sahrens {
335da6c28aaSamw 	zfs_creat_t *zct = arg;
336da6c28aaSamw 	nvlist_t *nvprops = zct->zct_props;
337fa9e4066Sahrens 	int error;
338e9dbad6fSeschrock 	uint64_t volblocksize, volsize;
339fa9e4066Sahrens 
340ecd6cf80Smarks 	VERIFY(nvlist_lookup_uint64(nvprops,
341e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
342ecd6cf80Smarks 	if (nvlist_lookup_uint64(nvprops,
343e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
344e9dbad6fSeschrock 		volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
345e9dbad6fSeschrock 
346e9dbad6fSeschrock 	/*
347e7cbe64fSgw25295 	 * These properties must be removed from the list so the generic
348e9dbad6fSeschrock 	 * property setting step won't apply to them.
349e9dbad6fSeschrock 	 */
350ecd6cf80Smarks 	VERIFY(nvlist_remove_all(nvprops,
351e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
352ecd6cf80Smarks 	(void) nvlist_remove_all(nvprops,
353e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
354e9dbad6fSeschrock 
355e9dbad6fSeschrock 	error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
356fa9e4066Sahrens 	    DMU_OT_NONE, 0, tx);
357fa9e4066Sahrens 	ASSERT(error == 0);
358fa9e4066Sahrens 
359fa9e4066Sahrens 	error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
360fa9e4066Sahrens 	    DMU_OT_NONE, 0, tx);
361fa9e4066Sahrens 	ASSERT(error == 0);
362fa9e4066Sahrens 
363e9dbad6fSeschrock 	error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
364fa9e4066Sahrens 	ASSERT(error == 0);
365fa9e4066Sahrens }
366fa9e4066Sahrens 
367fa9e4066Sahrens /*
36822ac5be4Sperrin  * Replay a TX_WRITE ZIL transaction that didn't get committed
36922ac5be4Sperrin  * after a system failure
37022ac5be4Sperrin  */
37122ac5be4Sperrin static int
37222ac5be4Sperrin zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
37322ac5be4Sperrin {
37422ac5be4Sperrin 	objset_t *os = zv->zv_objset;
37522ac5be4Sperrin 	char *data = (char *)(lr + 1);	/* data follows lr_write_t */
37622ac5be4Sperrin 	uint64_t off = lr->lr_offset;
37722ac5be4Sperrin 	uint64_t len = lr->lr_length;
37822ac5be4Sperrin 	dmu_tx_t *tx;
37922ac5be4Sperrin 	int error;
38022ac5be4Sperrin 
38122ac5be4Sperrin 	if (byteswap)
38222ac5be4Sperrin 		byteswap_uint64_array(lr, sizeof (*lr));
38322ac5be4Sperrin 
38422ac5be4Sperrin 	tx = dmu_tx_create(os);
38522ac5be4Sperrin 	dmu_tx_hold_write(tx, ZVOL_OBJ, off, len);
3861209a471SNeil Perrin 	error = dmu_tx_assign(tx, TXG_WAIT);
38722ac5be4Sperrin 	if (error) {
38822ac5be4Sperrin 		dmu_tx_abort(tx);
38922ac5be4Sperrin 	} else {
39022ac5be4Sperrin 		dmu_write(os, ZVOL_OBJ, off, len, data, tx);
39122ac5be4Sperrin 		dmu_tx_commit(tx);
39222ac5be4Sperrin 	}
39322ac5be4Sperrin 
39422ac5be4Sperrin 	return (error);
39522ac5be4Sperrin }
39622ac5be4Sperrin 
39722ac5be4Sperrin /* ARGSUSED */
39822ac5be4Sperrin static int
39922ac5be4Sperrin zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
40022ac5be4Sperrin {
40122ac5be4Sperrin 	return (ENOTSUP);
40222ac5be4Sperrin }
40322ac5be4Sperrin 
40422ac5be4Sperrin /*
40522ac5be4Sperrin  * Callback vectors for replaying records.
40622ac5be4Sperrin  * Only TX_WRITE is needed for zvol.
40722ac5be4Sperrin  */
40822ac5be4Sperrin zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = {
40922ac5be4Sperrin 	zvol_replay_err,	/* 0 no such transaction type */
41022ac5be4Sperrin 	zvol_replay_err,	/* TX_CREATE */
41122ac5be4Sperrin 	zvol_replay_err,	/* TX_MKDIR */
41222ac5be4Sperrin 	zvol_replay_err,	/* TX_MKXATTR */
41322ac5be4Sperrin 	zvol_replay_err,	/* TX_SYMLINK */
41422ac5be4Sperrin 	zvol_replay_err,	/* TX_REMOVE */
41522ac5be4Sperrin 	zvol_replay_err,	/* TX_RMDIR */
41622ac5be4Sperrin 	zvol_replay_err,	/* TX_LINK */
41722ac5be4Sperrin 	zvol_replay_err,	/* TX_RENAME */
41822ac5be4Sperrin 	zvol_replay_write,	/* TX_WRITE */
41922ac5be4Sperrin 	zvol_replay_err,	/* TX_TRUNCATE */
42022ac5be4Sperrin 	zvol_replay_err,	/* TX_SETATTR */
42122ac5be4Sperrin 	zvol_replay_err,	/* TX_ACL */
42222ac5be4Sperrin };
42322ac5be4Sperrin 
42422ac5be4Sperrin /*
425e7cbe64fSgw25295  * Create a minor node (plus a whole lot more) for the specified volume.
426fa9e4066Sahrens  */
427fa9e4066Sahrens int
42891ebeef5Sahrens zvol_create_minor(const char *name, major_t maj)
429fa9e4066Sahrens {
430fa9e4066Sahrens 	zvol_state_t *zv;
431fa9e4066Sahrens 	objset_t *os;
43267bd71c6Sperrin 	dmu_object_info_t doi;
433fa9e4066Sahrens 	uint64_t volsize;
434fa9e4066Sahrens 	minor_t minor = 0;
435fa9e4066Sahrens 	struct pathname linkpath;
436fa9e4066Sahrens 	vnode_t *vp = NULL;
437fa9e4066Sahrens 	char *devpath;
438fa9e4066Sahrens 	char chrbuf[30], blkbuf[30];
439fa9e4066Sahrens 	int error;
440fa9e4066Sahrens 
441fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
442fa9e4066Sahrens 
443fa9e4066Sahrens 	if ((zv = zvol_minor_lookup(name)) != NULL) {
444fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
445fa9e4066Sahrens 		return (EEXIST);
446fa9e4066Sahrens 	}
447fa9e4066Sahrens 
448503ad85cSMatthew Ahrens 	/* lie and say we're read-only */
449503ad85cSMatthew Ahrens 	error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, zvol_tag, &os);
450fa9e4066Sahrens 
451fa9e4066Sahrens 	if (error) {
452fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
453fa9e4066Sahrens 		return (error);
454fa9e4066Sahrens 	}
455fa9e4066Sahrens 
456fa9e4066Sahrens 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
457fa9e4066Sahrens 
458fa9e4066Sahrens 	if (error) {
459503ad85cSMatthew Ahrens 		dmu_objset_disown(os, zvol_tag);
460fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
461fa9e4066Sahrens 		return (error);
462fa9e4066Sahrens 	}
463fa9e4066Sahrens 
464fa9e4066Sahrens 	/*
465fa9e4066Sahrens 	 * If there's an existing /dev/zvol symlink, try to use the
466fa9e4066Sahrens 	 * same minor number we used last time.
467fa9e4066Sahrens 	 */
468ae46e4c7SMatthew Ahrens 	devpath = kmem_asprintf("%s%s", ZVOL_FULL_DEV_DIR, name);
469fa9e4066Sahrens 	error = lookupname(devpath, UIO_SYSSPACE, NO_FOLLOW, NULL, &vp);
470ae46e4c7SMatthew Ahrens 	strfree(devpath);
471fa9e4066Sahrens 
472fa9e4066Sahrens 	if (error == 0 && vp->v_type != VLNK)
473fa9e4066Sahrens 		error = EINVAL;
474fa9e4066Sahrens 
475fa9e4066Sahrens 	if (error == 0) {
476fa9e4066Sahrens 		pn_alloc(&linkpath);
477fa9e4066Sahrens 		error = pn_getsymlink(vp, &linkpath, kcred);
478fa9e4066Sahrens 		if (error == 0) {
479fa9e4066Sahrens 			char *ms = strstr(linkpath.pn_path, ZVOL_PSEUDO_DEV);
480fa9e4066Sahrens 			if (ms != NULL) {
481fa9e4066Sahrens 				ms += strlen(ZVOL_PSEUDO_DEV);
482fa9e4066Sahrens 				minor = stoi(&ms);
483fa9e4066Sahrens 			}
484fa9e4066Sahrens 		}
485fa9e4066Sahrens 		pn_free(&linkpath);
486fa9e4066Sahrens 	}
487fa9e4066Sahrens 
488fa9e4066Sahrens 	if (vp != NULL)
489fa9e4066Sahrens 		VN_RELE(vp);
490fa9e4066Sahrens 
491fa9e4066Sahrens 	/*
492fa9e4066Sahrens 	 * If we found a minor but it's already in use, we must pick a new one.
493fa9e4066Sahrens 	 */
494fa9e4066Sahrens 	if (minor != 0 && ddi_get_soft_state(zvol_state, minor) != NULL)
495fa9e4066Sahrens 		minor = 0;
496fa9e4066Sahrens 
497fa9e4066Sahrens 	if (minor == 0)
498fa9e4066Sahrens 		minor = zvol_minor_alloc();
499fa9e4066Sahrens 
500fa9e4066Sahrens 	if (minor == 0) {
501503ad85cSMatthew Ahrens 		dmu_objset_disown(os, zvol_tag);
502fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
503fa9e4066Sahrens 		return (ENXIO);
504fa9e4066Sahrens 	}
505fa9e4066Sahrens 
506fa9e4066Sahrens 	if (ddi_soft_state_zalloc(zvol_state, minor) != DDI_SUCCESS) {
507503ad85cSMatthew Ahrens 		dmu_objset_disown(os, zvol_tag);
508fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
509fa9e4066Sahrens 		return (EAGAIN);
510fa9e4066Sahrens 	}
511fa9e4066Sahrens 
512e9dbad6fSeschrock 	(void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME,
513e9dbad6fSeschrock 	    (char *)name);
514fa9e4066Sahrens 
515fa9e4066Sahrens 	(void) sprintf(chrbuf, "%uc,raw", minor);
516fa9e4066Sahrens 
517fa9e4066Sahrens 	if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR,
518fa9e4066Sahrens 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
519fa9e4066Sahrens 		ddi_soft_state_free(zvol_state, minor);
520503ad85cSMatthew Ahrens 		dmu_objset_disown(os, zvol_tag);
521fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
522fa9e4066Sahrens 		return (EAGAIN);
523fa9e4066Sahrens 	}
524fa9e4066Sahrens 
525fa9e4066Sahrens 	(void) sprintf(blkbuf, "%uc", minor);
526fa9e4066Sahrens 
527fa9e4066Sahrens 	if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK,
528fa9e4066Sahrens 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
529fa9e4066Sahrens 		ddi_remove_minor_node(zfs_dip, chrbuf);
530fa9e4066Sahrens 		ddi_soft_state_free(zvol_state, minor);
531503ad85cSMatthew Ahrens 		dmu_objset_disown(os, zvol_tag);
532fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
533fa9e4066Sahrens 		return (EAGAIN);
534fa9e4066Sahrens 	}
535fa9e4066Sahrens 
536fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
537fa9e4066Sahrens 
538fa9e4066Sahrens 	(void) strcpy(zv->zv_name, name);
539fa9e4066Sahrens 	zv->zv_min_bs = DEV_BSHIFT;
540fa9e4066Sahrens 	zv->zv_minor = minor;
541fa9e4066Sahrens 	zv->zv_volsize = volsize;
542fa9e4066Sahrens 	zv->zv_objset = os;
543503ad85cSMatthew Ahrens 	zv->zv_issnap = dmu_objset_is_snapshot(os);
54467bd71c6Sperrin 	zv->zv_zilog = zil_open(os, zvol_get_data);
545c2e6a7d6Sperrin 	mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
546c2e6a7d6Sperrin 	avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
547c2e6a7d6Sperrin 	    sizeof (rl_t), offsetof(rl_t, r_node));
54888b7b0f2SMatthew Ahrens 	list_create(&zv->zv_extents, sizeof (zvol_extent_t),
54988b7b0f2SMatthew Ahrens 	    offsetof(zvol_extent_t, ze_node));
55067bd71c6Sperrin 	/* get and cache the blocksize */
55167bd71c6Sperrin 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
55267bd71c6Sperrin 	ASSERT(error == 0);
55367bd71c6Sperrin 	zv->zv_volblocksize = doi.doi_data_block_size;
55422ac5be4Sperrin 
5551209a471SNeil Perrin 	zil_replay(os, zv, zvol_replay_vector);
55691ebeef5Sahrens 	zvol_size_changed(zv, maj);
557fa9e4066Sahrens 
558ea8dc4b6Seschrock 	/* XXX this should handle the possible i/o error */
559fa9e4066Sahrens 	VERIFY(dsl_prop_register(dmu_objset_ds(zv->zv_objset),
560fa9e4066Sahrens 	    "readonly", zvol_readonly_changed_cb, zv) == 0);
561fa9e4066Sahrens 
562fa9e4066Sahrens 	zvol_minors++;
563fa9e4066Sahrens 
564fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
565fa9e4066Sahrens 
566fa9e4066Sahrens 	return (0);
567fa9e4066Sahrens }
568fa9e4066Sahrens 
569fa9e4066Sahrens /*
570fa9e4066Sahrens  * Remove minor node for the specified volume.
571fa9e4066Sahrens  */
572fa9e4066Sahrens int
573e9dbad6fSeschrock zvol_remove_minor(const char *name)
574fa9e4066Sahrens {
575fa9e4066Sahrens 	zvol_state_t *zv;
576fa9e4066Sahrens 	char namebuf[30];
577fa9e4066Sahrens 
578fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
579fa9e4066Sahrens 
580e9dbad6fSeschrock 	if ((zv = zvol_minor_lookup(name)) == NULL) {
581fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
582fa9e4066Sahrens 		return (ENXIO);
583fa9e4066Sahrens 	}
584fa9e4066Sahrens 
585fa9e4066Sahrens 	if (zv->zv_total_opens != 0) {
586fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
587fa9e4066Sahrens 		return (EBUSY);
588fa9e4066Sahrens 	}
589fa9e4066Sahrens 
590fa9e4066Sahrens 	(void) sprintf(namebuf, "%uc,raw", zv->zv_minor);
591fa9e4066Sahrens 	ddi_remove_minor_node(zfs_dip, namebuf);
592fa9e4066Sahrens 
593fa9e4066Sahrens 	(void) sprintf(namebuf, "%uc", zv->zv_minor);
594fa9e4066Sahrens 	ddi_remove_minor_node(zfs_dip, namebuf);
595fa9e4066Sahrens 
596fa9e4066Sahrens 	VERIFY(dsl_prop_unregister(dmu_objset_ds(zv->zv_objset),
597fa9e4066Sahrens 	    "readonly", zvol_readonly_changed_cb, zv) == 0);
598fa9e4066Sahrens 
59922ac5be4Sperrin 	zil_close(zv->zv_zilog);
60022ac5be4Sperrin 	zv->zv_zilog = NULL;
601503ad85cSMatthew Ahrens 	dmu_objset_disown(zv->zv_objset, zvol_tag);
602fa9e4066Sahrens 	zv->zv_objset = NULL;
603c2e6a7d6Sperrin 	avl_destroy(&zv->zv_znode.z_range_avl);
604c2e6a7d6Sperrin 	mutex_destroy(&zv->zv_znode.z_range_lock);
605fa9e4066Sahrens 
606fa9e4066Sahrens 	ddi_soft_state_free(zvol_state, zv->zv_minor);
607fa9e4066Sahrens 
608fa9e4066Sahrens 	zvol_minors--;
609fa9e4066Sahrens 
610fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
611fa9e4066Sahrens 
612fa9e4066Sahrens 	return (0);
613fa9e4066Sahrens }
614fa9e4066Sahrens 
615e7cbe64fSgw25295 int
616e7cbe64fSgw25295 zvol_prealloc(zvol_state_t *zv)
617e7cbe64fSgw25295 {
618e7cbe64fSgw25295 	objset_t *os = zv->zv_objset;
619e7cbe64fSgw25295 	dmu_tx_t *tx;
620e7cbe64fSgw25295 	uint64_t refd, avail, usedobjs, availobjs;
621e7cbe64fSgw25295 	uint64_t resid = zv->zv_volsize;
622e7cbe64fSgw25295 	uint64_t off = 0;
623e7cbe64fSgw25295 
624e7cbe64fSgw25295 	/* Check the space usage before attempting to allocate the space */
625e7cbe64fSgw25295 	dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs);
626e7cbe64fSgw25295 	if (avail < zv->zv_volsize)
627e7cbe64fSgw25295 		return (ENOSPC);
628e7cbe64fSgw25295 
629e7cbe64fSgw25295 	/* Free old extents if they exist */
630e7cbe64fSgw25295 	zvol_free_extents(zv);
631e7cbe64fSgw25295 
632e7cbe64fSgw25295 	while (resid != 0) {
633e7cbe64fSgw25295 		int error;
634e7cbe64fSgw25295 		uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE);
635e7cbe64fSgw25295 
636e7cbe64fSgw25295 		tx = dmu_tx_create(os);
637e7cbe64fSgw25295 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
638e7cbe64fSgw25295 		error = dmu_tx_assign(tx, TXG_WAIT);
639e7cbe64fSgw25295 		if (error) {
640e7cbe64fSgw25295 			dmu_tx_abort(tx);
641cdb0ab79Smaybee 			(void) dmu_free_long_range(os, ZVOL_OBJ, 0, off);
642e7cbe64fSgw25295 			return (error);
643e7cbe64fSgw25295 		}
64482c9918fSTim Haley 		dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx);
645e7cbe64fSgw25295 		dmu_tx_commit(tx);
646e7cbe64fSgw25295 		off += bytes;
647e7cbe64fSgw25295 		resid -= bytes;
648e7cbe64fSgw25295 	}
649e7cbe64fSgw25295 	txg_wait_synced(dmu_objset_pool(os), 0);
650e7cbe64fSgw25295 
651e7cbe64fSgw25295 	return (0);
652e7cbe64fSgw25295 }
653e7cbe64fSgw25295 
654e7cbe64fSgw25295 int
655e7cbe64fSgw25295 zvol_update_volsize(zvol_state_t *zv, major_t maj, uint64_t volsize)
656e7cbe64fSgw25295 {
657e7cbe64fSgw25295 	dmu_tx_t *tx;
658e7cbe64fSgw25295 	int error;
659e7cbe64fSgw25295 
660e7cbe64fSgw25295 	ASSERT(MUTEX_HELD(&zvol_state_lock));
661e7cbe64fSgw25295 
662e7cbe64fSgw25295 	tx = dmu_tx_create(zv->zv_objset);
663e7cbe64fSgw25295 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
664e7cbe64fSgw25295 	error = dmu_tx_assign(tx, TXG_WAIT);
665e7cbe64fSgw25295 	if (error) {
666e7cbe64fSgw25295 		dmu_tx_abort(tx);
667e7cbe64fSgw25295 		return (error);
668e7cbe64fSgw25295 	}
669e7cbe64fSgw25295 
670e7cbe64fSgw25295 	error = zap_update(zv->zv_objset, ZVOL_ZAP_OBJ, "size", 8, 1,
671e7cbe64fSgw25295 	    &volsize, tx);
672e7cbe64fSgw25295 	dmu_tx_commit(tx);
673e7cbe64fSgw25295 
674e7cbe64fSgw25295 	if (error == 0)
675cdb0ab79Smaybee 		error = dmu_free_long_range(zv->zv_objset,
676cdb0ab79Smaybee 		    ZVOL_OBJ, volsize, DMU_OBJECT_END);
677e7cbe64fSgw25295 
678bb0ade09Sahrens 	/*
679bb0ade09Sahrens 	 * If we are using a faked-up state (zv_minor == 0) then don't
680bb0ade09Sahrens 	 * try to update the in-core zvol state.
681bb0ade09Sahrens 	 */
682bb0ade09Sahrens 	if (error == 0 && zv->zv_minor) {
683e7cbe64fSgw25295 		zv->zv_volsize = volsize;
684e7cbe64fSgw25295 		zvol_size_changed(zv, maj);
685e7cbe64fSgw25295 	}
686e7cbe64fSgw25295 	return (error);
687e7cbe64fSgw25295 }
688e7cbe64fSgw25295 
689fa9e4066Sahrens int
69091ebeef5Sahrens zvol_set_volsize(const char *name, major_t maj, uint64_t volsize)
691fa9e4066Sahrens {
692fa9e4066Sahrens 	zvol_state_t *zv;
693fa9e4066Sahrens 	int error;
6945c5460e9Seschrock 	dmu_object_info_t doi;
695e7cbe64fSgw25295 	uint64_t old_volsize = 0ULL;
696bb0ade09Sahrens 	zvol_state_t state = { 0 };
697fa9e4066Sahrens 
698fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
699fa9e4066Sahrens 
700e9dbad6fSeschrock 	if ((zv = zvol_minor_lookup(name)) == NULL) {
701bb0ade09Sahrens 		/*
702bb0ade09Sahrens 		 * If we are doing a "zfs clone -o volsize=", then the
703bb0ade09Sahrens 		 * minor node won't exist yet.
704bb0ade09Sahrens 		 */
705503ad85cSMatthew Ahrens 		error = dmu_objset_own(name, DMU_OST_ZVOL, B_FALSE, FTAG,
706bb0ade09Sahrens 		    &state.zv_objset);
707bb0ade09Sahrens 		if (error != 0)
708bb0ade09Sahrens 			goto out;
709bb0ade09Sahrens 		zv = &state;
710fa9e4066Sahrens 	}
711e7cbe64fSgw25295 	old_volsize = zv->zv_volsize;
712fa9e4066Sahrens 
7135c5460e9Seschrock 	if ((error = dmu_object_info(zv->zv_objset, ZVOL_OBJ, &doi)) != 0 ||
714e9dbad6fSeschrock 	    (error = zvol_check_volsize(volsize,
715bb0ade09Sahrens 	    doi.doi_data_block_size)) != 0)
716bb0ade09Sahrens 		goto out;
7175c5460e9Seschrock 
718503ad85cSMatthew Ahrens 	if (zv->zv_flags & ZVOL_RDONLY || zv->zv_issnap) {
719bb0ade09Sahrens 		error = EROFS;
720bb0ade09Sahrens 		goto out;
721fa9e4066Sahrens 	}
722fa9e4066Sahrens 
723e7cbe64fSgw25295 	error = zvol_update_volsize(zv, maj, volsize);
724e7cbe64fSgw25295 
725e7cbe64fSgw25295 	/*
726e7cbe64fSgw25295 	 * Reinitialize the dump area to the new size. If we
727e7cbe64fSgw25295 	 * failed to resize the dump area then restore the it back to
728e7cbe64fSgw25295 	 * it's original size.
729e7cbe64fSgw25295 	 */
730e7cbe64fSgw25295 	if (error == 0 && zv->zv_flags & ZVOL_DUMPIFIED) {
731e7cbe64fSgw25295 		if ((error = zvol_dumpify(zv)) != 0 ||
732e7cbe64fSgw25295 		    (error = dumpvp_resize()) != 0) {
733e7cbe64fSgw25295 			(void) zvol_update_volsize(zv, maj, old_volsize);
734e7cbe64fSgw25295 			error = zvol_dumpify(zv);
735fa9e4066Sahrens 		}
736fa9e4066Sahrens 	}
737fa9e4066Sahrens 
738573ca77eSGeorge Wilson 	/*
739573ca77eSGeorge Wilson 	 * Generate a LUN expansion event.
740573ca77eSGeorge Wilson 	 */
741573ca77eSGeorge Wilson 	if (error == 0) {
742573ca77eSGeorge Wilson 		sysevent_id_t eid;
743573ca77eSGeorge Wilson 		nvlist_t *attr;
744573ca77eSGeorge Wilson 		char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
745573ca77eSGeorge Wilson 
746573ca77eSGeorge Wilson 		(void) snprintf(physpath, MAXPATHLEN, "%s%uc", ZVOL_PSEUDO_DEV,
747573ca77eSGeorge Wilson 		    zv->zv_minor);
748573ca77eSGeorge Wilson 
749573ca77eSGeorge Wilson 		VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
750573ca77eSGeorge Wilson 		VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
751573ca77eSGeorge Wilson 
752573ca77eSGeorge Wilson 		(void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
753573ca77eSGeorge Wilson 		    ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
754573ca77eSGeorge Wilson 
755573ca77eSGeorge Wilson 		nvlist_free(attr);
756573ca77eSGeorge Wilson 		kmem_free(physpath, MAXPATHLEN);
757573ca77eSGeorge Wilson 	}
758573ca77eSGeorge Wilson 
759bb0ade09Sahrens out:
760bb0ade09Sahrens 	if (state.zv_objset)
761503ad85cSMatthew Ahrens 		dmu_objset_disown(state.zv_objset, FTAG);
762bb0ade09Sahrens 
763fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
764fa9e4066Sahrens 
765fa9e4066Sahrens 	return (error);
766fa9e4066Sahrens }
767fa9e4066Sahrens 
768fa9e4066Sahrens int
769e9dbad6fSeschrock zvol_set_volblocksize(const char *name, uint64_t volblocksize)
770fa9e4066Sahrens {
771fa9e4066Sahrens 	zvol_state_t *zv;
772fa9e4066Sahrens 	dmu_tx_t *tx;
773fa9e4066Sahrens 	int error;
77488b7b0f2SMatthew Ahrens 	boolean_t needlock;
775fa9e4066Sahrens 
77688b7b0f2SMatthew Ahrens 	/*
77788b7b0f2SMatthew Ahrens 	 * The lock may already be held if we are being called from
77888b7b0f2SMatthew Ahrens 	 * zvol_dump_init().
77988b7b0f2SMatthew Ahrens 	 */
78088b7b0f2SMatthew Ahrens 	needlock = !MUTEX_HELD(&zvol_state_lock);
78188b7b0f2SMatthew Ahrens 	if (needlock)
782fa9e4066Sahrens 		mutex_enter(&zvol_state_lock);
783fa9e4066Sahrens 
784e9dbad6fSeschrock 	if ((zv = zvol_minor_lookup(name)) == NULL) {
78588b7b0f2SMatthew Ahrens 		if (needlock)
786fa9e4066Sahrens 			mutex_exit(&zvol_state_lock);
787fa9e4066Sahrens 		return (ENXIO);
788fa9e4066Sahrens 	}
789503ad85cSMatthew Ahrens 	if (zv->zv_flags & ZVOL_RDONLY || zv->zv_issnap) {
79088b7b0f2SMatthew Ahrens 		if (needlock)
791fa9e4066Sahrens 			mutex_exit(&zvol_state_lock);
792fa9e4066Sahrens 		return (EROFS);
793fa9e4066Sahrens 	}
794fa9e4066Sahrens 
795fa9e4066Sahrens 	tx = dmu_tx_create(zv->zv_objset);
796fa9e4066Sahrens 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
797fa9e4066Sahrens 	error = dmu_tx_assign(tx, TXG_WAIT);
798fa9e4066Sahrens 	if (error) {
799fa9e4066Sahrens 		dmu_tx_abort(tx);
800fa9e4066Sahrens 	} else {
801fa9e4066Sahrens 		error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ,
802e9dbad6fSeschrock 		    volblocksize, 0, tx);
803fa9e4066Sahrens 		if (error == ENOTSUP)
804fa9e4066Sahrens 			error = EBUSY;
805fa9e4066Sahrens 		dmu_tx_commit(tx);
80688b7b0f2SMatthew Ahrens 		if (error == 0)
80788b7b0f2SMatthew Ahrens 			zv->zv_volblocksize = volblocksize;
808fa9e4066Sahrens 	}
809fa9e4066Sahrens 
81088b7b0f2SMatthew Ahrens 	if (needlock)
811fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
812fa9e4066Sahrens 
813fa9e4066Sahrens 	return (error);
814fa9e4066Sahrens }
815fa9e4066Sahrens 
816fa9e4066Sahrens /*ARGSUSED*/
817fa9e4066Sahrens int
818fa9e4066Sahrens zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr)
819fa9e4066Sahrens {
820fa9e4066Sahrens 	minor_t minor = getminor(*devp);
821fa9e4066Sahrens 	zvol_state_t *zv;
822fa9e4066Sahrens 
823fa9e4066Sahrens 	if (minor == 0)			/* This is the control device */
824fa9e4066Sahrens 		return (0);
825fa9e4066Sahrens 
826fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
827fa9e4066Sahrens 
828fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
829fa9e4066Sahrens 	if (zv == NULL) {
830fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
831fa9e4066Sahrens 		return (ENXIO);
832fa9e4066Sahrens 	}
833fa9e4066Sahrens 
834fa9e4066Sahrens 	ASSERT(zv->zv_objset != NULL);
835fa9e4066Sahrens 
836fa9e4066Sahrens 	if ((flag & FWRITE) &&
837503ad85cSMatthew Ahrens 	    (zv->zv_flags & ZVOL_RDONLY || zv->zv_issnap)) {
838fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
839fa9e4066Sahrens 		return (EROFS);
840fa9e4066Sahrens 	}
841c7f714e2SEric Taylor 	if (zv->zv_flags & ZVOL_EXCL) {
842c7f714e2SEric Taylor 		mutex_exit(&zvol_state_lock);
843c7f714e2SEric Taylor 		return (EBUSY);
844c7f714e2SEric Taylor 	}
845c7f714e2SEric Taylor 	if (flag & FEXCL) {
846c7f714e2SEric Taylor 		if (zv->zv_total_opens != 0) {
847c7f714e2SEric Taylor 			mutex_exit(&zvol_state_lock);
848c7f714e2SEric Taylor 			return (EBUSY);
849c7f714e2SEric Taylor 		}
850c7f714e2SEric Taylor 		zv->zv_flags |= ZVOL_EXCL;
851c7f714e2SEric Taylor 	}
852fa9e4066Sahrens 
853fa9e4066Sahrens 	if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) {
854fa9e4066Sahrens 		zv->zv_open_count[otyp]++;
855fa9e4066Sahrens 		zv->zv_total_opens++;
856fa9e4066Sahrens 	}
857fa9e4066Sahrens 
858fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
859fa9e4066Sahrens 
860fa9e4066Sahrens 	return (0);
861fa9e4066Sahrens }
862fa9e4066Sahrens 
863fa9e4066Sahrens /*ARGSUSED*/
864fa9e4066Sahrens int
865fa9e4066Sahrens zvol_close(dev_t dev, int flag, int otyp, cred_t *cr)
866fa9e4066Sahrens {
867fa9e4066Sahrens 	minor_t minor = getminor(dev);
868fa9e4066Sahrens 	zvol_state_t *zv;
869fa9e4066Sahrens 
870fa9e4066Sahrens 	if (minor == 0)		/* This is the control device */
871fa9e4066Sahrens 		return (0);
872fa9e4066Sahrens 
873fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
874fa9e4066Sahrens 
875fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
876fa9e4066Sahrens 	if (zv == NULL) {
877fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
878fa9e4066Sahrens 		return (ENXIO);
879fa9e4066Sahrens 	}
880fa9e4066Sahrens 
881c7f714e2SEric Taylor 	if (zv->zv_flags & ZVOL_EXCL) {
882c7f714e2SEric Taylor 		ASSERT(zv->zv_total_opens == 1);
883c7f714e2SEric Taylor 		zv->zv_flags &= ~ZVOL_EXCL;
884fa9e4066Sahrens 	}
885fa9e4066Sahrens 
886fa9e4066Sahrens 	/*
887fa9e4066Sahrens 	 * If the open count is zero, this is a spurious close.
888fa9e4066Sahrens 	 * That indicates a bug in the kernel / DDI framework.
889fa9e4066Sahrens 	 */
890fa9e4066Sahrens 	ASSERT(zv->zv_open_count[otyp] != 0);
891fa9e4066Sahrens 	ASSERT(zv->zv_total_opens != 0);
892fa9e4066Sahrens 
893fa9e4066Sahrens 	/*
894fa9e4066Sahrens 	 * You may get multiple opens, but only one close.
895fa9e4066Sahrens 	 */
896fa9e4066Sahrens 	zv->zv_open_count[otyp]--;
897fa9e4066Sahrens 	zv->zv_total_opens--;
898fa9e4066Sahrens 
899fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
900fa9e4066Sahrens 
901fa9e4066Sahrens 	return (0);
902fa9e4066Sahrens }
903fa9e4066Sahrens 
904feb08c6bSbillm static void
90567bd71c6Sperrin zvol_get_done(dmu_buf_t *db, void *vzgd)
90667bd71c6Sperrin {
90767bd71c6Sperrin 	zgd_t *zgd = (zgd_t *)vzgd;
908c2e6a7d6Sperrin 	rl_t *rl = zgd->zgd_rl;
90967bd71c6Sperrin 
91067bd71c6Sperrin 	dmu_buf_rele(db, vzgd);
911c2e6a7d6Sperrin 	zfs_range_unlock(rl);
91217f17c2dSbonwick 	zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
91367bd71c6Sperrin 	kmem_free(zgd, sizeof (zgd_t));
91467bd71c6Sperrin }
91567bd71c6Sperrin 
91667bd71c6Sperrin /*
91767bd71c6Sperrin  * Get data to generate a TX_WRITE intent log record.
91867bd71c6Sperrin  */
919feb08c6bSbillm static int
92067bd71c6Sperrin zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
92167bd71c6Sperrin {
92267bd71c6Sperrin 	zvol_state_t *zv = arg;
92367bd71c6Sperrin 	objset_t *os = zv->zv_objset;
92467bd71c6Sperrin 	dmu_buf_t *db;
925c2e6a7d6Sperrin 	rl_t *rl;
92667bd71c6Sperrin 	zgd_t *zgd;
927c2e6a7d6Sperrin 	uint64_t boff; 			/* block starting offset */
928c2e6a7d6Sperrin 	int dlen = lr->lr_length;	/* length of user data */
92967bd71c6Sperrin 	int error;
93067bd71c6Sperrin 
93167bd71c6Sperrin 	ASSERT(zio);
932c2e6a7d6Sperrin 	ASSERT(dlen != 0);
933feb08c6bSbillm 
934c2e6a7d6Sperrin 	/*
935c2e6a7d6Sperrin 	 * Write records come in two flavors: immediate and indirect.
936c2e6a7d6Sperrin 	 * For small writes it's cheaper to store the data with the
937c2e6a7d6Sperrin 	 * log record (immediate); for large writes it's cheaper to
938c2e6a7d6Sperrin 	 * sync the data and get a pointer to it (indirect) so that
939c2e6a7d6Sperrin 	 * we don't have to write the data twice.
940c2e6a7d6Sperrin 	 */
941c2e6a7d6Sperrin 	if (buf != NULL) /* immediate write */
9427bfdf011SNeil Perrin 		return (dmu_read(os, ZVOL_OBJ, lr->lr_offset, dlen, buf,
9437bfdf011SNeil Perrin 		    DMU_READ_NO_PREFETCH));
94467bd71c6Sperrin 
94567bd71c6Sperrin 	zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP);
94667bd71c6Sperrin 	zgd->zgd_zilog = zv->zv_zilog;
94767bd71c6Sperrin 	zgd->zgd_bp = &lr->lr_blkptr;
94867bd71c6Sperrin 
94967bd71c6Sperrin 	/*
950c2e6a7d6Sperrin 	 * Lock the range of the block to ensure that when the data is
951e7cbe64fSgw25295 	 * written out and its checksum is being calculated that no other
952c2e6a7d6Sperrin 	 * thread can change the block.
95367bd71c6Sperrin 	 */
954c2e6a7d6Sperrin 	boff = P2ALIGN_TYPED(lr->lr_offset, zv->zv_volblocksize, uint64_t);
955c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, boff, zv->zv_volblocksize,
956c2e6a7d6Sperrin 	    RL_READER);
957c2e6a7d6Sperrin 	zgd->zgd_rl = rl;
958c2e6a7d6Sperrin 
959c2e6a7d6Sperrin 	VERIFY(0 == dmu_buf_hold(os, ZVOL_OBJ, lr->lr_offset, zgd, &db));
96067bd71c6Sperrin 	error = dmu_sync(zio, db, &lr->lr_blkptr,
96167bd71c6Sperrin 	    lr->lr_common.lrc_txg, zvol_get_done, zgd);
962feb08c6bSbillm 	if (error == 0)
96317f17c2dSbonwick 		zil_add_block(zv->zv_zilog, &lr->lr_blkptr);
96467bd71c6Sperrin 	/*
96567bd71c6Sperrin 	 * If we get EINPROGRESS, then we need to wait for a
96667bd71c6Sperrin 	 * write IO initiated by dmu_sync() to complete before
96767bd71c6Sperrin 	 * we can release this dbuf.  We will finish everything
96867bd71c6Sperrin 	 * up in the zvol_get_done() callback.
96967bd71c6Sperrin 	 */
97067bd71c6Sperrin 	if (error == EINPROGRESS)
97167bd71c6Sperrin 		return (0);
97267bd71c6Sperrin 	dmu_buf_rele(db, zgd);
973c2e6a7d6Sperrin 	zfs_range_unlock(rl);
97467bd71c6Sperrin 	kmem_free(zgd, sizeof (zgd_t));
97567bd71c6Sperrin 	return (error);
97667bd71c6Sperrin }
97767bd71c6Sperrin 
978a24e15ceSperrin /*
979a24e15ceSperrin  * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
98022ac5be4Sperrin  *
98122ac5be4Sperrin  * We store data in the log buffers if it's small enough.
98267bd71c6Sperrin  * Otherwise we will later flush the data out via dmu_sync().
98322ac5be4Sperrin  */
98467bd71c6Sperrin ssize_t zvol_immediate_write_sz = 32768;
98522ac5be4Sperrin 
986feb08c6bSbillm static void
987510b6c0eSNeil Perrin zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid,
988510b6c0eSNeil Perrin     boolean_t sync)
98922ac5be4Sperrin {
990feb08c6bSbillm 	uint32_t blocksize = zv->zv_volblocksize;
9911209a471SNeil Perrin 	zilog_t *zilog = zv->zv_zilog;
992510b6c0eSNeil Perrin 	boolean_t slogging;
993*e09fa4daSNeil Perrin 	ssize_t immediate_write_sz;
994510b6c0eSNeil Perrin 
995510b6c0eSNeil Perrin 	if (zil_disable)
996510b6c0eSNeil Perrin 		return;
997a24e15ceSperrin 
9981209a471SNeil Perrin 	if (zilog->zl_replay) {
9991209a471SNeil Perrin 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
10001209a471SNeil Perrin 		zilog->zl_replayed_seq[dmu_tx_get_txg(tx) & TXG_MASK] =
10011209a471SNeil Perrin 		    zilog->zl_replaying_seq;
10021209a471SNeil Perrin 		return;
10031209a471SNeil Perrin 	}
10041209a471SNeil Perrin 
1005*e09fa4daSNeil Perrin 	immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
1006*e09fa4daSNeil Perrin 	    ? 0 : zvol_immediate_write_sz;
1007*e09fa4daSNeil Perrin 
1008*e09fa4daSNeil Perrin 	slogging = spa_has_slogs(zilog->zl_spa) &&
1009*e09fa4daSNeil Perrin 	    (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
1010feb08c6bSbillm 
1011510b6c0eSNeil Perrin 	while (resid) {
1012510b6c0eSNeil Perrin 		itx_t *itx;
1013510b6c0eSNeil Perrin 		lr_write_t *lr;
1014510b6c0eSNeil Perrin 		ssize_t len;
1015510b6c0eSNeil Perrin 		itx_wr_state_t write_state;
1016510b6c0eSNeil Perrin 
1017510b6c0eSNeil Perrin 		/*
1018510b6c0eSNeil Perrin 		 * Unlike zfs_log_write() we can be called with
1019510b6c0eSNeil Perrin 		 * upto DMU_MAX_ACCESS/2 (5MB) writes.
1020510b6c0eSNeil Perrin 		 */
1021*e09fa4daSNeil Perrin 		if (blocksize > immediate_write_sz && !slogging &&
1022510b6c0eSNeil Perrin 		    resid >= blocksize && off % blocksize == 0) {
1023510b6c0eSNeil Perrin 			write_state = WR_INDIRECT; /* uses dmu_sync */
1024510b6c0eSNeil Perrin 			len = blocksize;
1025510b6c0eSNeil Perrin 		} else if (sync) {
1026510b6c0eSNeil Perrin 			write_state = WR_COPIED;
1027510b6c0eSNeil Perrin 			len = MIN(ZIL_MAX_LOG_DATA, resid);
1028510b6c0eSNeil Perrin 		} else {
1029510b6c0eSNeil Perrin 			write_state = WR_NEED_COPY;
1030510b6c0eSNeil Perrin 			len = MIN(ZIL_MAX_LOG_DATA, resid);
1031510b6c0eSNeil Perrin 		}
1032510b6c0eSNeil Perrin 
1033510b6c0eSNeil Perrin 		itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
1034510b6c0eSNeil Perrin 		    (write_state == WR_COPIED ? len : 0));
103522ac5be4Sperrin 		lr = (lr_write_t *)&itx->itx_lr;
1036510b6c0eSNeil Perrin 		if (write_state == WR_COPIED && dmu_read(zv->zv_objset,
10377bfdf011SNeil Perrin 		    ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
1038510b6c0eSNeil Perrin 			kmem_free(itx, offsetof(itx_t, itx_lr) +
1039510b6c0eSNeil Perrin 			    itx->itx_lr.lrc_reclen);
1040510b6c0eSNeil Perrin 			itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1041510b6c0eSNeil Perrin 			lr = (lr_write_t *)&itx->itx_lr;
1042510b6c0eSNeil Perrin 			write_state = WR_NEED_COPY;
1043510b6c0eSNeil Perrin 		}
1044510b6c0eSNeil Perrin 
1045510b6c0eSNeil Perrin 		itx->itx_wr_state = write_state;
1046510b6c0eSNeil Perrin 		if (write_state == WR_NEED_COPY)
1047510b6c0eSNeil Perrin 			itx->itx_sod += len;
104822ac5be4Sperrin 		lr->lr_foid = ZVOL_OBJ;
104922ac5be4Sperrin 		lr->lr_offset = off;
1050510b6c0eSNeil Perrin 		lr->lr_length = len;
1051feb08c6bSbillm 		lr->lr_blkoff = off - P2ALIGN_TYPED(off, blocksize, uint64_t);
105222ac5be4Sperrin 		BP_ZERO(&lr->lr_blkptr);
1053feb08c6bSbillm 
1054510b6c0eSNeil Perrin 		itx->itx_private = zv;
1055510b6c0eSNeil Perrin 		itx->itx_sync = sync;
1056510b6c0eSNeil Perrin 
10571209a471SNeil Perrin 		(void) zil_itx_assign(zilog, itx, tx);
1058510b6c0eSNeil Perrin 
1059510b6c0eSNeil Perrin 		off += len;
1060510b6c0eSNeil Perrin 		resid -= len;
1061a24e15ceSperrin 	}
106222ac5be4Sperrin }
106322ac5be4Sperrin 
106488b7b0f2SMatthew Ahrens static int
106588b7b0f2SMatthew Ahrens zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t size,
106688b7b0f2SMatthew Ahrens     boolean_t doread, boolean_t isdump)
1067e7cbe64fSgw25295 {
1068e7cbe64fSgw25295 	vdev_disk_t *dvd;
1069e7cbe64fSgw25295 	int c;
1070e7cbe64fSgw25295 	int numerrors = 0;
1071e7cbe64fSgw25295 
1072e7cbe64fSgw25295 	for (c = 0; c < vd->vdev_children; c++) {
107321ecdf64SLin Ling 		ASSERT(vd->vdev_ops == &vdev_mirror_ops ||
107421ecdf64SLin Ling 		    vd->vdev_ops == &vdev_replacing_ops ||
107521ecdf64SLin Ling 		    vd->vdev_ops == &vdev_spare_ops);
107688b7b0f2SMatthew Ahrens 		int err = zvol_dumpio_vdev(vd->vdev_child[c],
107788b7b0f2SMatthew Ahrens 		    addr, offset, size, doread, isdump);
107888b7b0f2SMatthew Ahrens 		if (err != 0) {
1079e7cbe64fSgw25295 			numerrors++;
108088b7b0f2SMatthew Ahrens 		} else if (doread) {
1081e7cbe64fSgw25295 			break;
1082e7cbe64fSgw25295 		}
1083e7cbe64fSgw25295 	}
1084e7cbe64fSgw25295 
1085e7cbe64fSgw25295 	if (!vd->vdev_ops->vdev_op_leaf)
1086e7cbe64fSgw25295 		return (numerrors < vd->vdev_children ? 0 : EIO);
1087e7cbe64fSgw25295 
1088dc0bb255SEric Taylor 	if (doread && !vdev_readable(vd))
1089dc0bb255SEric Taylor 		return (EIO);
1090dc0bb255SEric Taylor 	else if (!doread && !vdev_writeable(vd))
1091e7cbe64fSgw25295 		return (EIO);
1092e7cbe64fSgw25295 
1093e7cbe64fSgw25295 	dvd = vd->vdev_tsd;
1094e7cbe64fSgw25295 	ASSERT3P(dvd, !=, NULL);
1095e7cbe64fSgw25295 	offset += VDEV_LABEL_START_SIZE;
1096e7cbe64fSgw25295 
1097e7cbe64fSgw25295 	if (ddi_in_panic() || isdump) {
109888b7b0f2SMatthew Ahrens 		ASSERT(!doread);
109988b7b0f2SMatthew Ahrens 		if (doread)
1100e7cbe64fSgw25295 			return (EIO);
1101e7cbe64fSgw25295 		return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset),
1102e7cbe64fSgw25295 		    lbtodb(size)));
1103e7cbe64fSgw25295 	} else {
1104e7cbe64fSgw25295 		return (vdev_disk_physio(dvd->vd_lh, addr, size, offset,
110588b7b0f2SMatthew Ahrens 		    doread ? B_READ : B_WRITE));
1106e7cbe64fSgw25295 	}
1107e7cbe64fSgw25295 }
1108e7cbe64fSgw25295 
110988b7b0f2SMatthew Ahrens static int
111088b7b0f2SMatthew Ahrens zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size,
111188b7b0f2SMatthew Ahrens     boolean_t doread, boolean_t isdump)
1112e7cbe64fSgw25295 {
1113e7cbe64fSgw25295 	vdev_t *vd;
1114e7cbe64fSgw25295 	int error;
111588b7b0f2SMatthew Ahrens 	zvol_extent_t *ze;
1116e7cbe64fSgw25295 	spa_t *spa = dmu_objset_spa(zv->zv_objset);
1117e7cbe64fSgw25295 
111888b7b0f2SMatthew Ahrens 	/* Must be sector aligned, and not stradle a block boundary. */
111988b7b0f2SMatthew Ahrens 	if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) ||
112088b7b0f2SMatthew Ahrens 	    P2BOUNDARY(offset, size, zv->zv_volblocksize)) {
112188b7b0f2SMatthew Ahrens 		return (EINVAL);
112288b7b0f2SMatthew Ahrens 	}
1123e7cbe64fSgw25295 	ASSERT(size <= zv->zv_volblocksize);
1124e7cbe64fSgw25295 
112588b7b0f2SMatthew Ahrens 	/* Locate the extent this belongs to */
112688b7b0f2SMatthew Ahrens 	ze = list_head(&zv->zv_extents);
112788b7b0f2SMatthew Ahrens 	while (offset >= ze->ze_nblks * zv->zv_volblocksize) {
112888b7b0f2SMatthew Ahrens 		offset -= ze->ze_nblks * zv->zv_volblocksize;
112988b7b0f2SMatthew Ahrens 		ze = list_next(&zv->zv_extents, ze);
113088b7b0f2SMatthew Ahrens 	}
1131e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
113288b7b0f2SMatthew Ahrens 	vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva));
113388b7b0f2SMatthew Ahrens 	offset += DVA_GET_OFFSET(&ze->ze_dva);
113488b7b0f2SMatthew Ahrens 	error = zvol_dumpio_vdev(vd, addr, offset, size, doread, isdump);
1135e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
1136e7cbe64fSgw25295 	return (error);
1137e7cbe64fSgw25295 }
1138e7cbe64fSgw25295 
1139e7cbe64fSgw25295 int
1140fa9e4066Sahrens zvol_strategy(buf_t *bp)
1141fa9e4066Sahrens {
1142fa9e4066Sahrens 	zvol_state_t *zv = ddi_get_soft_state(zvol_state, getminor(bp->b_edev));
1143fa9e4066Sahrens 	uint64_t off, volsize;
114488b7b0f2SMatthew Ahrens 	size_t resid;
1145fa9e4066Sahrens 	char *addr;
114622ac5be4Sperrin 	objset_t *os;
1147c2e6a7d6Sperrin 	rl_t *rl;
1148fa9e4066Sahrens 	int error = 0;
114988b7b0f2SMatthew Ahrens 	boolean_t doread = bp->b_flags & B_READ;
115088b7b0f2SMatthew Ahrens 	boolean_t is_dump = zv->zv_flags & ZVOL_DUMPIFIED;
1151510b6c0eSNeil Perrin 	boolean_t sync;
1152fa9e4066Sahrens 
1153fa9e4066Sahrens 	if (zv == NULL) {
1154fa9e4066Sahrens 		bioerror(bp, ENXIO);
1155fa9e4066Sahrens 		biodone(bp);
1156fa9e4066Sahrens 		return (0);
1157fa9e4066Sahrens 	}
1158fa9e4066Sahrens 
1159fa9e4066Sahrens 	if (getminor(bp->b_edev) == 0) {
1160fa9e4066Sahrens 		bioerror(bp, EINVAL);
1161fa9e4066Sahrens 		biodone(bp);
1162fa9e4066Sahrens 		return (0);
1163fa9e4066Sahrens 	}
1164fa9e4066Sahrens 
1165e7cbe64fSgw25295 	if (!(bp->b_flags & B_READ) &&
1166503ad85cSMatthew Ahrens 	    (zv->zv_flags & ZVOL_RDONLY || zv->zv_issnap)) {
1167fa9e4066Sahrens 		bioerror(bp, EROFS);
1168fa9e4066Sahrens 		biodone(bp);
1169fa9e4066Sahrens 		return (0);
1170fa9e4066Sahrens 	}
1171fa9e4066Sahrens 
1172fa9e4066Sahrens 	off = ldbtob(bp->b_blkno);
1173fa9e4066Sahrens 	volsize = zv->zv_volsize;
1174fa9e4066Sahrens 
117522ac5be4Sperrin 	os = zv->zv_objset;
117622ac5be4Sperrin 	ASSERT(os != NULL);
1177fa9e4066Sahrens 
1178fa9e4066Sahrens 	bp_mapin(bp);
1179fa9e4066Sahrens 	addr = bp->b_un.b_addr;
1180fa9e4066Sahrens 	resid = bp->b_bcount;
1181fa9e4066Sahrens 
118288b7b0f2SMatthew Ahrens 	if (resid > 0 && (off < 0 || off >= volsize)) {
118388b7b0f2SMatthew Ahrens 		bioerror(bp, EIO);
118488b7b0f2SMatthew Ahrens 		biodone(bp);
118588b7b0f2SMatthew Ahrens 		return (0);
118688b7b0f2SMatthew Ahrens 	}
118773ec3d9cSgw25295 
1188510b6c0eSNeil Perrin 	sync = !(bp->b_flags & B_ASYNC) && !doread && !is_dump &&
1189510b6c0eSNeil Perrin 	    !(zv->zv_flags & ZVOL_WCE) && !zil_disable;
1190510b6c0eSNeil Perrin 
1191a24e15ceSperrin 	/*
1192a24e15ceSperrin 	 * There must be no buffer changes when doing a dmu_sync() because
1193a24e15ceSperrin 	 * we can't change the data whilst calculating the checksum.
1194a24e15ceSperrin 	 */
1195c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, off, resid,
119688b7b0f2SMatthew Ahrens 	    doread ? RL_READER : RL_WRITER);
1197e7cbe64fSgw25295 
1198fa9e4066Sahrens 	while (resid != 0 && off < volsize) {
119988b7b0f2SMatthew Ahrens 		size_t size = MIN(resid, zvol_maxphys);
1200e7cbe64fSgw25295 		if (is_dump) {
1201e7cbe64fSgw25295 			size = MIN(size, P2END(off, zv->zv_volblocksize) - off);
120288b7b0f2SMatthew Ahrens 			error = zvol_dumpio(zv, addr, off, size,
120388b7b0f2SMatthew Ahrens 			    doread, B_FALSE);
120488b7b0f2SMatthew Ahrens 		} else if (doread) {
12057bfdf011SNeil Perrin 			error = dmu_read(os, ZVOL_OBJ, off, size, addr,
12067bfdf011SNeil Perrin 			    DMU_READ_PREFETCH);
1207fa9e4066Sahrens 		} else {
120822ac5be4Sperrin 			dmu_tx_t *tx = dmu_tx_create(os);
1209fa9e4066Sahrens 			dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
1210fa9e4066Sahrens 			error = dmu_tx_assign(tx, TXG_WAIT);
1211fa9e4066Sahrens 			if (error) {
1212fa9e4066Sahrens 				dmu_tx_abort(tx);
1213fa9e4066Sahrens 			} else {
121422ac5be4Sperrin 				dmu_write(os, ZVOL_OBJ, off, size, addr, tx);
1215510b6c0eSNeil Perrin 				zvol_log_write(zv, tx, off, size, sync);
1216fa9e4066Sahrens 				dmu_tx_commit(tx);
1217fa9e4066Sahrens 			}
1218fa9e4066Sahrens 		}
1219b87f3af3Sperrin 		if (error) {
1220b87f3af3Sperrin 			/* convert checksum errors into IO errors */
1221b87f3af3Sperrin 			if (error == ECKSUM)
1222b87f3af3Sperrin 				error = EIO;
1223fa9e4066Sahrens 			break;
1224b87f3af3Sperrin 		}
1225fa9e4066Sahrens 		off += size;
1226fa9e4066Sahrens 		addr += size;
1227fa9e4066Sahrens 		resid -= size;
1228fa9e4066Sahrens 	}
1229c2e6a7d6Sperrin 	zfs_range_unlock(rl);
1230fa9e4066Sahrens 
1231fa9e4066Sahrens 	if ((bp->b_resid = resid) == bp->b_bcount)
1232fa9e4066Sahrens 		bioerror(bp, off > volsize ? EINVAL : error);
1233fa9e4066Sahrens 
1234510b6c0eSNeil Perrin 	if (sync)
1235feb08c6bSbillm 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1236feb08c6bSbillm 	biodone(bp);
123722ac5be4Sperrin 
1238fa9e4066Sahrens 	return (0);
1239fa9e4066Sahrens }
1240fa9e4066Sahrens 
124167bd71c6Sperrin /*
124267bd71c6Sperrin  * Set the buffer count to the zvol maximum transfer.
124367bd71c6Sperrin  * Using our own routine instead of the default minphys()
124467bd71c6Sperrin  * means that for larger writes we write bigger buffers on X86
124567bd71c6Sperrin  * (128K instead of 56K) and flush the disk write cache less often
124667bd71c6Sperrin  * (every zvol_maxphys - currently 1MB) instead of minphys (currently
124767bd71c6Sperrin  * 56K on X86 and 128K on sparc).
124867bd71c6Sperrin  */
124967bd71c6Sperrin void
125067bd71c6Sperrin zvol_minphys(struct buf *bp)
125167bd71c6Sperrin {
125267bd71c6Sperrin 	if (bp->b_bcount > zvol_maxphys)
125367bd71c6Sperrin 		bp->b_bcount = zvol_maxphys;
125467bd71c6Sperrin }
125567bd71c6Sperrin 
1256e7cbe64fSgw25295 int
1257e7cbe64fSgw25295 zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks)
1258e7cbe64fSgw25295 {
1259e7cbe64fSgw25295 	minor_t minor = getminor(dev);
1260e7cbe64fSgw25295 	zvol_state_t *zv;
1261e7cbe64fSgw25295 	int error = 0;
1262e7cbe64fSgw25295 	uint64_t size;
1263e7cbe64fSgw25295 	uint64_t boff;
1264e7cbe64fSgw25295 	uint64_t resid;
1265e7cbe64fSgw25295 
1266e7cbe64fSgw25295 	if (minor == 0)			/* This is the control device */
1267e7cbe64fSgw25295 		return (ENXIO);
1268e7cbe64fSgw25295 
1269e7cbe64fSgw25295 	zv = ddi_get_soft_state(zvol_state, minor);
1270e7cbe64fSgw25295 	if (zv == NULL)
1271e7cbe64fSgw25295 		return (ENXIO);
1272e7cbe64fSgw25295 
1273e7cbe64fSgw25295 	boff = ldbtob(blkno);
1274e7cbe64fSgw25295 	resid = ldbtob(nblocks);
1275e7cbe64fSgw25295 
127688b7b0f2SMatthew Ahrens 	VERIFY3U(boff + resid, <=, zv->zv_volsize);
127788b7b0f2SMatthew Ahrens 
127888b7b0f2SMatthew Ahrens 	while (resid) {
127988b7b0f2SMatthew Ahrens 		size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff);
128088b7b0f2SMatthew Ahrens 		error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE);
1281e7cbe64fSgw25295 		if (error)
1282e7cbe64fSgw25295 			break;
1283e7cbe64fSgw25295 		boff += size;
1284e7cbe64fSgw25295 		addr += size;
1285e7cbe64fSgw25295 		resid -= size;
1286e7cbe64fSgw25295 	}
1287e7cbe64fSgw25295 
1288e7cbe64fSgw25295 	return (error);
1289e7cbe64fSgw25295 }
1290e7cbe64fSgw25295 
1291fa9e4066Sahrens /*ARGSUSED*/
1292fa9e4066Sahrens int
1293feb08c6bSbillm zvol_read(dev_t dev, uio_t *uio, cred_t *cr)
1294fa9e4066Sahrens {
1295c7ca1008Sgw25295 	minor_t minor = getminor(dev);
1296c7ca1008Sgw25295 	zvol_state_t *zv;
129773ec3d9cSgw25295 	uint64_t volsize;
1298c2e6a7d6Sperrin 	rl_t *rl;
1299feb08c6bSbillm 	int error = 0;
1300feb08c6bSbillm 
1301c7ca1008Sgw25295 	if (minor == 0)			/* This is the control device */
1302c7ca1008Sgw25295 		return (ENXIO);
1303c7ca1008Sgw25295 
1304c7ca1008Sgw25295 	zv = ddi_get_soft_state(zvol_state, minor);
1305c7ca1008Sgw25295 	if (zv == NULL)
1306c7ca1008Sgw25295 		return (ENXIO);
1307c7ca1008Sgw25295 
130873ec3d9cSgw25295 	volsize = zv->zv_volsize;
130973ec3d9cSgw25295 	if (uio->uio_resid > 0 &&
131073ec3d9cSgw25295 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
131173ec3d9cSgw25295 		return (EIO);
131273ec3d9cSgw25295 
131388b7b0f2SMatthew Ahrens 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
131488b7b0f2SMatthew Ahrens 		error = physio(zvol_strategy, NULL, dev, B_READ,
131588b7b0f2SMatthew Ahrens 		    zvol_minphys, uio);
131688b7b0f2SMatthew Ahrens 		return (error);
131788b7b0f2SMatthew Ahrens 	}
131888b7b0f2SMatthew Ahrens 
1319c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1320c2e6a7d6Sperrin 	    RL_READER);
132173ec3d9cSgw25295 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1322feb08c6bSbillm 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1323feb08c6bSbillm 
132473ec3d9cSgw25295 		/* don't read past the end */
132573ec3d9cSgw25295 		if (bytes > volsize - uio->uio_loffset)
132673ec3d9cSgw25295 			bytes = volsize - uio->uio_loffset;
132773ec3d9cSgw25295 
1328feb08c6bSbillm 		error =  dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes);
1329b87f3af3Sperrin 		if (error) {
1330b87f3af3Sperrin 			/* convert checksum errors into IO errors */
1331b87f3af3Sperrin 			if (error == ECKSUM)
1332b87f3af3Sperrin 				error = EIO;
1333feb08c6bSbillm 			break;
1334feb08c6bSbillm 		}
1335b87f3af3Sperrin 	}
1336c2e6a7d6Sperrin 	zfs_range_unlock(rl);
1337feb08c6bSbillm 	return (error);
1338fa9e4066Sahrens }
1339fa9e4066Sahrens 
1340fa9e4066Sahrens /*ARGSUSED*/
1341fa9e4066Sahrens int
1342feb08c6bSbillm zvol_write(dev_t dev, uio_t *uio, cred_t *cr)
1343fa9e4066Sahrens {
1344c7ca1008Sgw25295 	minor_t minor = getminor(dev);
1345c7ca1008Sgw25295 	zvol_state_t *zv;
134673ec3d9cSgw25295 	uint64_t volsize;
1347c2e6a7d6Sperrin 	rl_t *rl;
1348feb08c6bSbillm 	int error = 0;
1349510b6c0eSNeil Perrin 	boolean_t sync;
1350fa9e4066Sahrens 
1351c7ca1008Sgw25295 	if (minor == 0)			/* This is the control device */
1352c7ca1008Sgw25295 		return (ENXIO);
1353c7ca1008Sgw25295 
1354c7ca1008Sgw25295 	zv = ddi_get_soft_state(zvol_state, minor);
1355c7ca1008Sgw25295 	if (zv == NULL)
1356c7ca1008Sgw25295 		return (ENXIO);
1357c7ca1008Sgw25295 
135873ec3d9cSgw25295 	volsize = zv->zv_volsize;
135973ec3d9cSgw25295 	if (uio->uio_resid > 0 &&
136073ec3d9cSgw25295 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
136173ec3d9cSgw25295 		return (EIO);
136273ec3d9cSgw25295 
1363e7cbe64fSgw25295 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
1364e7cbe64fSgw25295 		error = physio(zvol_strategy, NULL, dev, B_WRITE,
1365e7cbe64fSgw25295 		    zvol_minphys, uio);
1366e7cbe64fSgw25295 		return (error);
1367e7cbe64fSgw25295 	}
1368e7cbe64fSgw25295 
1369510b6c0eSNeil Perrin 	sync = !(zv->zv_flags & ZVOL_WCE) && !zil_disable;
1370510b6c0eSNeil Perrin 
1371c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1372c2e6a7d6Sperrin 	    RL_WRITER);
137373ec3d9cSgw25295 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1374feb08c6bSbillm 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1375feb08c6bSbillm 		uint64_t off = uio->uio_loffset;
1376feb08c6bSbillm 		dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
137773ec3d9cSgw25295 
137873ec3d9cSgw25295 		if (bytes > volsize - off)	/* don't write past the end */
137973ec3d9cSgw25295 			bytes = volsize - off;
138073ec3d9cSgw25295 
1381feb08c6bSbillm 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
1382feb08c6bSbillm 		error = dmu_tx_assign(tx, TXG_WAIT);
1383feb08c6bSbillm 		if (error) {
1384feb08c6bSbillm 			dmu_tx_abort(tx);
1385feb08c6bSbillm 			break;
1386feb08c6bSbillm 		}
1387feb08c6bSbillm 		error = dmu_write_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes, tx);
1388feb08c6bSbillm 		if (error == 0)
1389510b6c0eSNeil Perrin 			zvol_log_write(zv, tx, off, bytes, sync);
1390feb08c6bSbillm 		dmu_tx_commit(tx);
1391feb08c6bSbillm 
1392feb08c6bSbillm 		if (error)
1393feb08c6bSbillm 			break;
1394feb08c6bSbillm 	}
1395c2e6a7d6Sperrin 	zfs_range_unlock(rl);
1396510b6c0eSNeil Perrin 	if (sync)
1397e08bf2c6SEric Taylor 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1398feb08c6bSbillm 	return (error);
1399fa9e4066Sahrens }
1400fa9e4066Sahrens 
1401c7f714e2SEric Taylor int
1402c7f714e2SEric Taylor zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs)
1403c7f714e2SEric Taylor {
1404c7f714e2SEric Taylor 	struct uuid uuid = EFI_RESERVED;
1405c7f714e2SEric Taylor 	efi_gpe_t gpe = { 0 };
1406c7f714e2SEric Taylor 	uint32_t crc;
1407c7f714e2SEric Taylor 	dk_efi_t efi;
1408c7f714e2SEric Taylor 	int length;
1409c7f714e2SEric Taylor 	char *ptr;
1410c7f714e2SEric Taylor 
1411c7f714e2SEric Taylor 	if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag))
1412c7f714e2SEric Taylor 		return (EFAULT);
1413c7f714e2SEric Taylor 	ptr = (char *)(uintptr_t)efi.dki_data_64;
1414c7f714e2SEric Taylor 	length = efi.dki_length;
1415c7f714e2SEric Taylor 	/*
1416c7f714e2SEric Taylor 	 * Some clients may attempt to request a PMBR for the
1417c7f714e2SEric Taylor 	 * zvol.  Currently this interface will return EINVAL to
1418c7f714e2SEric Taylor 	 * such requests.  These requests could be supported by
1419c7f714e2SEric Taylor 	 * adding a check for lba == 0 and consing up an appropriate
1420c7f714e2SEric Taylor 	 * PMBR.
1421c7f714e2SEric Taylor 	 */
1422c7f714e2SEric Taylor 	if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0)
1423c7f714e2SEric Taylor 		return (EINVAL);
1424c7f714e2SEric Taylor 
1425c7f714e2SEric Taylor 	gpe.efi_gpe_StartingLBA = LE_64(34ULL);
1426c7f714e2SEric Taylor 	gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1);
1427c7f714e2SEric Taylor 	UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
1428c7f714e2SEric Taylor 
1429c7f714e2SEric Taylor 	if (efi.dki_lba == 1) {
1430c7f714e2SEric Taylor 		efi_gpt_t gpt = { 0 };
1431c7f714e2SEric Taylor 
1432c7f714e2SEric Taylor 		gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
1433c7f714e2SEric Taylor 		gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
1434c7f714e2SEric Taylor 		gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt));
1435c7f714e2SEric Taylor 		gpt.efi_gpt_MyLBA = LE_64(1ULL);
1436c7f714e2SEric Taylor 		gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL);
1437c7f714e2SEric Taylor 		gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1);
1438c7f714e2SEric Taylor 		gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL);
1439c7f714e2SEric Taylor 		gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
1440c7f714e2SEric Taylor 		gpt.efi_gpt_SizeOfPartitionEntry =
1441c7f714e2SEric Taylor 		    LE_32(sizeof (efi_gpe_t));
1442c7f714e2SEric Taylor 		CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
1443c7f714e2SEric Taylor 		gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
1444c7f714e2SEric Taylor 		CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table);
1445c7f714e2SEric Taylor 		gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
1446c7f714e2SEric Taylor 		if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length),
1447c7f714e2SEric Taylor 		    flag))
1448c7f714e2SEric Taylor 			return (EFAULT);
1449c7f714e2SEric Taylor 		ptr += sizeof (gpt);
1450c7f714e2SEric Taylor 		length -= sizeof (gpt);
1451c7f714e2SEric Taylor 	}
1452c7f714e2SEric Taylor 	if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe),
1453c7f714e2SEric Taylor 	    length), flag))
1454c7f714e2SEric Taylor 		return (EFAULT);
1455c7f714e2SEric Taylor 	return (0);
1456c7f714e2SEric Taylor }
1457c7f714e2SEric Taylor 
1458fa9e4066Sahrens /*
1459fa9e4066Sahrens  * Dirtbag ioctls to support mkfs(1M) for UFS filesystems.  See dkio(7I).
1460fa9e4066Sahrens  */
1461fa9e4066Sahrens /*ARGSUSED*/
1462fa9e4066Sahrens int
1463fa9e4066Sahrens zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
1464fa9e4066Sahrens {
1465fa9e4066Sahrens 	zvol_state_t *zv;
1466af2c4821Smaybee 	struct dk_cinfo dki;
1467fa9e4066Sahrens 	struct dk_minfo dkm;
1468af2c4821Smaybee 	struct dk_callback *dkc;
1469fa9e4066Sahrens 	int error = 0;
1470e7cbe64fSgw25295 	rl_t *rl;
1471fa9e4066Sahrens 
1472fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
1473fa9e4066Sahrens 
1474fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, getminor(dev));
1475fa9e4066Sahrens 
1476fa9e4066Sahrens 	if (zv == NULL) {
1477fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
1478fa9e4066Sahrens 		return (ENXIO);
1479fa9e4066Sahrens 	}
1480701f66c4SEric Taylor 	ASSERT(zv->zv_total_opens > 0);
1481fa9e4066Sahrens 
1482fa9e4066Sahrens 	switch (cmd) {
1483fa9e4066Sahrens 
1484fa9e4066Sahrens 	case DKIOCINFO:
1485af2c4821Smaybee 		bzero(&dki, sizeof (dki));
1486af2c4821Smaybee 		(void) strcpy(dki.dki_cname, "zvol");
1487af2c4821Smaybee 		(void) strcpy(dki.dki_dname, "zvol");
1488af2c4821Smaybee 		dki.dki_ctype = DKC_UNKNOWN;
1489af2c4821Smaybee 		dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs);
1490fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
1491af2c4821Smaybee 		if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag))
1492fa9e4066Sahrens 			error = EFAULT;
1493fa9e4066Sahrens 		return (error);
1494fa9e4066Sahrens 
1495fa9e4066Sahrens 	case DKIOCGMEDIAINFO:
1496fa9e4066Sahrens 		bzero(&dkm, sizeof (dkm));
1497fa9e4066Sahrens 		dkm.dki_lbsize = 1U << zv->zv_min_bs;
1498fa9e4066Sahrens 		dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
1499fa9e4066Sahrens 		dkm.dki_media_type = DK_UNKNOWN;
1500fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
1501fa9e4066Sahrens 		if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
1502fa9e4066Sahrens 			error = EFAULT;
1503fa9e4066Sahrens 		return (error);
1504fa9e4066Sahrens 
1505fa9e4066Sahrens 	case DKIOCGETEFI:
1506c7f714e2SEric Taylor 		{
1507c7f714e2SEric Taylor 			uint64_t vs = zv->zv_volsize;
1508c7f714e2SEric Taylor 			uint8_t bs = zv->zv_min_bs;
1509fa9e4066Sahrens 
1510fa9e4066Sahrens 			mutex_exit(&zvol_state_lock);
1511c7f714e2SEric Taylor 			error = zvol_getefi((void *)arg, flag, vs, bs);
1512fa9e4066Sahrens 			return (error);
1513c7f714e2SEric Taylor 		}
1514fa9e4066Sahrens 
1515feb08c6bSbillm 	case DKIOCFLUSHWRITECACHE:
1516af2c4821Smaybee 		dkc = (struct dk_callback *)arg;
1517701f66c4SEric Taylor 		mutex_exit(&zvol_state_lock);
1518feb08c6bSbillm 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1519af2c4821Smaybee 		if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) {
1520af2c4821Smaybee 			(*dkc->dkc_callback)(dkc->dkc_cookie, error);
1521af2c4821Smaybee 			error = 0;
1522af2c4821Smaybee 		}
1523701f66c4SEric Taylor 		return (error);
1524701f66c4SEric Taylor 
1525701f66c4SEric Taylor 	case DKIOCGETWCE:
1526701f66c4SEric Taylor 		{
1527701f66c4SEric Taylor 			int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0;
1528701f66c4SEric Taylor 			if (ddi_copyout(&wce, (void *)arg, sizeof (int),
1529701f66c4SEric Taylor 			    flag))
1530701f66c4SEric Taylor 				error = EFAULT;
1531feb08c6bSbillm 			break;
1532701f66c4SEric Taylor 		}
1533701f66c4SEric Taylor 	case DKIOCSETWCE:
1534701f66c4SEric Taylor 		{
1535701f66c4SEric Taylor 			int wce;
1536701f66c4SEric Taylor 			if (ddi_copyin((void *)arg, &wce, sizeof (int),
1537701f66c4SEric Taylor 			    flag)) {
1538701f66c4SEric Taylor 				error = EFAULT;
1539701f66c4SEric Taylor 				break;
1540701f66c4SEric Taylor 			}
1541701f66c4SEric Taylor 			if (wce) {
1542701f66c4SEric Taylor 				zv->zv_flags |= ZVOL_WCE;
1543701f66c4SEric Taylor 				mutex_exit(&zvol_state_lock);
1544701f66c4SEric Taylor 			} else {
1545701f66c4SEric Taylor 				zv->zv_flags &= ~ZVOL_WCE;
1546701f66c4SEric Taylor 				mutex_exit(&zvol_state_lock);
1547701f66c4SEric Taylor 				zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1548701f66c4SEric Taylor 			}
1549701f66c4SEric Taylor 			return (0);
1550701f66c4SEric Taylor 		}
1551feb08c6bSbillm 
1552b6130eadSmaybee 	case DKIOCGGEOM:
1553b6130eadSmaybee 	case DKIOCGVTOC:
1554e7cbe64fSgw25295 		/*
1555e7cbe64fSgw25295 		 * commands using these (like prtvtoc) expect ENOTSUP
1556e7cbe64fSgw25295 		 * since we're emulating an EFI label
1557e7cbe64fSgw25295 		 */
1558b6130eadSmaybee 		error = ENOTSUP;
1559b6130eadSmaybee 		break;
1560b6130eadSmaybee 
1561e7cbe64fSgw25295 	case DKIOCDUMPINIT:
1562e7cbe64fSgw25295 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1563e7cbe64fSgw25295 		    RL_WRITER);
1564e7cbe64fSgw25295 		error = zvol_dumpify(zv);
1565e7cbe64fSgw25295 		zfs_range_unlock(rl);
1566e7cbe64fSgw25295 		break;
1567e7cbe64fSgw25295 
1568e7cbe64fSgw25295 	case DKIOCDUMPFINI:
156906d5ae10SEric Taylor 		if (!(zv->zv_flags & ZVOL_DUMPIFIED))
157006d5ae10SEric Taylor 			break;
1571e7cbe64fSgw25295 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1572e7cbe64fSgw25295 		    RL_WRITER);
1573e7cbe64fSgw25295 		error = zvol_dump_fini(zv);
1574e7cbe64fSgw25295 		zfs_range_unlock(rl);
1575e7cbe64fSgw25295 		break;
1576e7cbe64fSgw25295 
1577fa9e4066Sahrens 	default:
157868a5ac4dSmaybee 		error = ENOTTY;
1579fa9e4066Sahrens 		break;
1580fa9e4066Sahrens 
1581fa9e4066Sahrens 	}
1582fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
1583fa9e4066Sahrens 	return (error);
1584fa9e4066Sahrens }
1585fa9e4066Sahrens 
1586fa9e4066Sahrens int
1587fa9e4066Sahrens zvol_busy(void)
1588fa9e4066Sahrens {
1589fa9e4066Sahrens 	return (zvol_minors != 0);
1590fa9e4066Sahrens }
1591fa9e4066Sahrens 
1592fa9e4066Sahrens void
1593fa9e4066Sahrens zvol_init(void)
1594fa9e4066Sahrens {
1595fa9e4066Sahrens 	VERIFY(ddi_soft_state_init(&zvol_state, sizeof (zvol_state_t), 1) == 0);
1596fa9e4066Sahrens 	mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
1597fa9e4066Sahrens }
1598fa9e4066Sahrens 
1599fa9e4066Sahrens void
1600fa9e4066Sahrens zvol_fini(void)
1601fa9e4066Sahrens {
1602fa9e4066Sahrens 	mutex_destroy(&zvol_state_lock);
1603fa9e4066Sahrens 	ddi_soft_state_fini(&zvol_state);
1604fa9e4066Sahrens }
1605e7cbe64fSgw25295 
1606e7cbe64fSgw25295 static boolean_t
1607e7cbe64fSgw25295 zvol_is_swap(zvol_state_t *zv)
1608e7cbe64fSgw25295 {
1609e7cbe64fSgw25295 	vnode_t *vp;
1610e7cbe64fSgw25295 	boolean_t ret = B_FALSE;
1611e7cbe64fSgw25295 	char *devpath;
1612e7cbe64fSgw25295 	int error;
1613e7cbe64fSgw25295 
1614ae46e4c7SMatthew Ahrens 	devpath = kmem_asprintf("%s%s", ZVOL_FULL_DEV_DIR, zv->zv_name);
1615e7cbe64fSgw25295 	error = lookupname(devpath, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp);
1616ae46e4c7SMatthew Ahrens 	strfree(devpath);
1617e7cbe64fSgw25295 
1618e7cbe64fSgw25295 	ret = !error && IS_SWAPVP(common_specvp(vp));
1619e7cbe64fSgw25295 
1620e7cbe64fSgw25295 	if (vp != NULL)
1621e7cbe64fSgw25295 		VN_RELE(vp);
1622e7cbe64fSgw25295 
1623e7cbe64fSgw25295 	return (ret);
1624e7cbe64fSgw25295 }
1625e7cbe64fSgw25295 
1626e7cbe64fSgw25295 static int
1627e7cbe64fSgw25295 zvol_dump_init(zvol_state_t *zv, boolean_t resize)
1628e7cbe64fSgw25295 {
1629e7cbe64fSgw25295 	dmu_tx_t *tx;
1630e7cbe64fSgw25295 	int error = 0;
1631e7cbe64fSgw25295 	objset_t *os = zv->zv_objset;
1632e7cbe64fSgw25295 	nvlist_t *nv = NULL;
1633e7cbe64fSgw25295 
1634e7cbe64fSgw25295 	ASSERT(MUTEX_HELD(&zvol_state_lock));
1635e7cbe64fSgw25295 
1636e7cbe64fSgw25295 	tx = dmu_tx_create(os);
1637e7cbe64fSgw25295 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1638e7cbe64fSgw25295 	error = dmu_tx_assign(tx, TXG_WAIT);
1639e7cbe64fSgw25295 	if (error) {
1640e7cbe64fSgw25295 		dmu_tx_abort(tx);
1641e7cbe64fSgw25295 		return (error);
1642e7cbe64fSgw25295 	}
1643e7cbe64fSgw25295 
1644e7cbe64fSgw25295 	/*
1645e7cbe64fSgw25295 	 * If we are resizing the dump device then we only need to
1646e7cbe64fSgw25295 	 * update the refreservation to match the newly updated
1647e7cbe64fSgw25295 	 * zvolsize. Otherwise, we save off the original state of the
1648e7cbe64fSgw25295 	 * zvol so that we can restore them if the zvol is ever undumpified.
1649e7cbe64fSgw25295 	 */
1650e7cbe64fSgw25295 	if (resize) {
1651e7cbe64fSgw25295 		error = zap_update(os, ZVOL_ZAP_OBJ,
1652e7cbe64fSgw25295 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1653e7cbe64fSgw25295 		    &zv->zv_volsize, tx);
1654e7cbe64fSgw25295 	} else {
165588b7b0f2SMatthew Ahrens 		uint64_t checksum, compress, refresrv, vbs;
165688b7b0f2SMatthew Ahrens 
1657e7cbe64fSgw25295 		error = dsl_prop_get_integer(zv->zv_name,
1658e7cbe64fSgw25295 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL);
1659e7cbe64fSgw25295 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
1660e7cbe64fSgw25295 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL);
1661e7cbe64fSgw25295 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
1662e7cbe64fSgw25295 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL);
166388b7b0f2SMatthew Ahrens 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
166488b7b0f2SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL);
1665e7cbe64fSgw25295 
1666e7cbe64fSgw25295 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1667e7cbe64fSgw25295 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1,
1668e7cbe64fSgw25295 		    &compress, tx);
1669e7cbe64fSgw25295 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1670e7cbe64fSgw25295 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx);
1671e7cbe64fSgw25295 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1672e7cbe64fSgw25295 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1673e7cbe64fSgw25295 		    &refresrv, tx);
167488b7b0f2SMatthew Ahrens 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
167588b7b0f2SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1,
167688b7b0f2SMatthew Ahrens 		    &vbs, tx);
1677e7cbe64fSgw25295 	}
1678e7cbe64fSgw25295 	dmu_tx_commit(tx);
1679e7cbe64fSgw25295 
1680e7cbe64fSgw25295 	/* Truncate the file */
1681e7cbe64fSgw25295 	if (!error)
1682cdb0ab79Smaybee 		error = dmu_free_long_range(zv->zv_objset,
1683cdb0ab79Smaybee 		    ZVOL_OBJ, 0, DMU_OBJECT_END);
1684e7cbe64fSgw25295 
1685e7cbe64fSgw25295 	if (error)
1686e7cbe64fSgw25295 		return (error);
1687e7cbe64fSgw25295 
1688e7cbe64fSgw25295 	/*
1689e7cbe64fSgw25295 	 * We only need update the zvol's property if we are initializing
1690e7cbe64fSgw25295 	 * the dump area for the first time.
1691e7cbe64fSgw25295 	 */
1692e7cbe64fSgw25295 	if (!resize) {
1693e7cbe64fSgw25295 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1694e7cbe64fSgw25295 		VERIFY(nvlist_add_uint64(nv,
1695e7cbe64fSgw25295 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0);
1696e7cbe64fSgw25295 		VERIFY(nvlist_add_uint64(nv,
1697e7cbe64fSgw25295 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
1698e7cbe64fSgw25295 		    ZIO_COMPRESS_OFF) == 0);
1699e7cbe64fSgw25295 		VERIFY(nvlist_add_uint64(nv,
1700e7cbe64fSgw25295 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
1701e7cbe64fSgw25295 		    ZIO_CHECKSUM_OFF) == 0);
170288b7b0f2SMatthew Ahrens 		VERIFY(nvlist_add_uint64(nv,
170388b7b0f2SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
170488b7b0f2SMatthew Ahrens 		    SPA_MAXBLOCKSIZE) == 0);
1705e7cbe64fSgw25295 
1706e7cbe64fSgw25295 		error = zfs_set_prop_nvlist(zv->zv_name, nv);
1707e7cbe64fSgw25295 		nvlist_free(nv);
1708e7cbe64fSgw25295 
1709e7cbe64fSgw25295 		if (error)
1710e7cbe64fSgw25295 			return (error);
1711e7cbe64fSgw25295 	}
1712e7cbe64fSgw25295 
1713e7cbe64fSgw25295 	/* Allocate the space for the dump */
1714e7cbe64fSgw25295 	error = zvol_prealloc(zv);
1715e7cbe64fSgw25295 	return (error);
1716e7cbe64fSgw25295 }
1717e7cbe64fSgw25295 
1718e7cbe64fSgw25295 static int
1719e7cbe64fSgw25295 zvol_dumpify(zvol_state_t *zv)
1720e7cbe64fSgw25295 {
1721e7cbe64fSgw25295 	int error = 0;
1722e7cbe64fSgw25295 	uint64_t dumpsize = 0;
1723e7cbe64fSgw25295 	dmu_tx_t *tx;
1724e7cbe64fSgw25295 	objset_t *os = zv->zv_objset;
1725e7cbe64fSgw25295 
1726503ad85cSMatthew Ahrens 	if (zv->zv_flags & ZVOL_RDONLY || zv->zv_issnap)
1727e7cbe64fSgw25295 		return (EROFS);
1728e7cbe64fSgw25295 
1729e7cbe64fSgw25295 	/*
1730e7cbe64fSgw25295 	 * We do not support swap devices acting as dump devices.
1731e7cbe64fSgw25295 	 */
1732e7cbe64fSgw25295 	if (zvol_is_swap(zv))
1733e7cbe64fSgw25295 		return (ENOTSUP);
1734e7cbe64fSgw25295 
1735e7cbe64fSgw25295 	if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE,
1736e7cbe64fSgw25295 	    8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) {
1737e7cbe64fSgw25295 		boolean_t resize = (dumpsize > 0) ? B_TRUE : B_FALSE;
1738e7cbe64fSgw25295 
1739e7cbe64fSgw25295 		if ((error = zvol_dump_init(zv, resize)) != 0) {
1740e7cbe64fSgw25295 			(void) zvol_dump_fini(zv);
1741e7cbe64fSgw25295 			return (error);
1742e7cbe64fSgw25295 		}
1743e7cbe64fSgw25295 	}
1744e7cbe64fSgw25295 
1745e7cbe64fSgw25295 	/*
1746e7cbe64fSgw25295 	 * Build up our lba mapping.
1747e7cbe64fSgw25295 	 */
1748e7cbe64fSgw25295 	error = zvol_get_lbas(zv);
1749e7cbe64fSgw25295 	if (error) {
1750e7cbe64fSgw25295 		(void) zvol_dump_fini(zv);
1751e7cbe64fSgw25295 		return (error);
1752e7cbe64fSgw25295 	}
1753e7cbe64fSgw25295 
1754e7cbe64fSgw25295 	tx = dmu_tx_create(os);
1755e7cbe64fSgw25295 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1756e7cbe64fSgw25295 	error = dmu_tx_assign(tx, TXG_WAIT);
1757e7cbe64fSgw25295 	if (error) {
1758e7cbe64fSgw25295 		dmu_tx_abort(tx);
1759e7cbe64fSgw25295 		(void) zvol_dump_fini(zv);
1760e7cbe64fSgw25295 		return (error);
1761e7cbe64fSgw25295 	}
1762e7cbe64fSgw25295 
1763e7cbe64fSgw25295 	zv->zv_flags |= ZVOL_DUMPIFIED;
1764e7cbe64fSgw25295 	error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1,
1765e7cbe64fSgw25295 	    &zv->zv_volsize, tx);
1766e7cbe64fSgw25295 	dmu_tx_commit(tx);
1767e7cbe64fSgw25295 
1768e7cbe64fSgw25295 	if (error) {
1769e7cbe64fSgw25295 		(void) zvol_dump_fini(zv);
1770e7cbe64fSgw25295 		return (error);
1771e7cbe64fSgw25295 	}
1772e7cbe64fSgw25295 
1773e7cbe64fSgw25295 	txg_wait_synced(dmu_objset_pool(os), 0);
1774e7cbe64fSgw25295 	return (0);
1775e7cbe64fSgw25295 }
1776e7cbe64fSgw25295 
1777e7cbe64fSgw25295 static int
1778e7cbe64fSgw25295 zvol_dump_fini(zvol_state_t *zv)
1779e7cbe64fSgw25295 {
1780e7cbe64fSgw25295 	dmu_tx_t *tx;
1781e7cbe64fSgw25295 	objset_t *os = zv->zv_objset;
1782e7cbe64fSgw25295 	nvlist_t *nv;
1783e7cbe64fSgw25295 	int error = 0;
178488b7b0f2SMatthew Ahrens 	uint64_t checksum, compress, refresrv, vbs;
1785e7cbe64fSgw25295 
1786b7e50089Smaybee 	/*
1787b7e50089Smaybee 	 * Attempt to restore the zvol back to its pre-dumpified state.
1788b7e50089Smaybee 	 * This is a best-effort attempt as it's possible that not all
1789b7e50089Smaybee 	 * of these properties were initialized during the dumpify process
1790b7e50089Smaybee 	 * (i.e. error during zvol_dump_init).
1791b7e50089Smaybee 	 */
1792b7e50089Smaybee 
1793e7cbe64fSgw25295 	tx = dmu_tx_create(os);
1794e7cbe64fSgw25295 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1795e7cbe64fSgw25295 	error = dmu_tx_assign(tx, TXG_WAIT);
1796e7cbe64fSgw25295 	if (error) {
1797e7cbe64fSgw25295 		dmu_tx_abort(tx);
1798e7cbe64fSgw25295 		return (error);
1799e7cbe64fSgw25295 	}
1800b7e50089Smaybee 	(void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx);
1801b7e50089Smaybee 	dmu_tx_commit(tx);
1802e7cbe64fSgw25295 
1803e7cbe64fSgw25295 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1804e7cbe64fSgw25295 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum);
1805e7cbe64fSgw25295 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1806e7cbe64fSgw25295 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress);
1807e7cbe64fSgw25295 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1808e7cbe64fSgw25295 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv);
180988b7b0f2SMatthew Ahrens 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
181088b7b0f2SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs);
1811e7cbe64fSgw25295 
1812e7cbe64fSgw25295 	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1813e7cbe64fSgw25295 	(void) nvlist_add_uint64(nv,
1814e7cbe64fSgw25295 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum);
1815e7cbe64fSgw25295 	(void) nvlist_add_uint64(nv,
1816e7cbe64fSgw25295 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress);
1817e7cbe64fSgw25295 	(void) nvlist_add_uint64(nv,
1818e7cbe64fSgw25295 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv);
181988b7b0f2SMatthew Ahrens 	(void) nvlist_add_uint64(nv,
182088b7b0f2SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), vbs);
1821e7cbe64fSgw25295 	(void) zfs_set_prop_nvlist(zv->zv_name, nv);
1822e7cbe64fSgw25295 	nvlist_free(nv);
1823e7cbe64fSgw25295 
1824b7e50089Smaybee 	zvol_free_extents(zv);
1825b7e50089Smaybee 	zv->zv_flags &= ~ZVOL_DUMPIFIED;
1826b7e50089Smaybee 	(void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END);
1827b7e50089Smaybee 
1828e7cbe64fSgw25295 	return (0);
1829e7cbe64fSgw25295 }
1830