xref: /illumos-gate/usr/src/uts/common/fs/zfs/zvol.c (revision ae46e4c775f2becc5343ff90b60a95acb79735f9)
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;
83fa9e4066Sahrens 
84e7cbe64fSgw25295 #define	ZVOL_DUMPSIZE		"dumpsize"
85e7cbe64fSgw25295 
86fa9e4066Sahrens /*
87fa9e4066Sahrens  * This lock protects the zvol_state structure from being modified
88fa9e4066Sahrens  * while it's being used, e.g. an open that comes in before a create
89fa9e4066Sahrens  * finishes.  It also protects temporary opens of the dataset so that,
90fa9e4066Sahrens  * e.g., an open doesn't get a spurious EBUSY.
91fa9e4066Sahrens  */
92fa9e4066Sahrens static kmutex_t zvol_state_lock;
93fa9e4066Sahrens static uint32_t zvol_minors;
94fa9e4066Sahrens 
95e7cbe64fSgw25295 typedef struct zvol_extent {
9688b7b0f2SMatthew Ahrens 	list_node_t	ze_node;
97e7cbe64fSgw25295 	dva_t		ze_dva;		/* dva associated with this extent */
9888b7b0f2SMatthew Ahrens 	uint64_t	ze_nblks;	/* number of blocks in extent */
99e7cbe64fSgw25295 } zvol_extent_t;
100e7cbe64fSgw25295 
101e7cbe64fSgw25295 /*
102fa9e4066Sahrens  * The in-core state of each volume.
103fa9e4066Sahrens  */
104fa9e4066Sahrens typedef struct zvol_state {
105fa9e4066Sahrens 	char		zv_name[MAXPATHLEN]; /* pool/dd name */
106fa9e4066Sahrens 	uint64_t	zv_volsize;	/* amount of space we advertise */
10767bd71c6Sperrin 	uint64_t	zv_volblocksize; /* volume block size */
108fa9e4066Sahrens 	minor_t		zv_minor;	/* minor number */
109fa9e4066Sahrens 	uint8_t		zv_min_bs;	/* minimum addressable block shift */
110701f66c4SEric Taylor 	uint8_t		zv_flags;	/* readonly, dumpified, etc. */
111fa9e4066Sahrens 	objset_t	*zv_objset;	/* objset handle */
112fa9e4066Sahrens 	uint32_t	zv_mode;	/* DS_MODE_* flags at open time */
113fa9e4066Sahrens 	uint32_t	zv_open_count[OTYPCNT];	/* open counts */
114fa9e4066Sahrens 	uint32_t	zv_total_opens;	/* total open count */
11522ac5be4Sperrin 	zilog_t		*zv_zilog;	/* ZIL handle */
11688b7b0f2SMatthew Ahrens 	list_t		zv_extents;	/* List of extents for dump */
117c2e6a7d6Sperrin 	znode_t		zv_znode;	/* for range locking */
118fa9e4066Sahrens } zvol_state_t;
119fa9e4066Sahrens 
12067bd71c6Sperrin /*
121e7cbe64fSgw25295  * zvol specific flags
122e7cbe64fSgw25295  */
123e7cbe64fSgw25295 #define	ZVOL_RDONLY	0x1
124e7cbe64fSgw25295 #define	ZVOL_DUMPIFIED	0x2
125c7f714e2SEric Taylor #define	ZVOL_EXCL	0x4
126701f66c4SEric Taylor #define	ZVOL_WCE	0x8
127e7cbe64fSgw25295 
128e7cbe64fSgw25295 /*
12967bd71c6Sperrin  * zvol maximum transfer in one DMU tx.
13067bd71c6Sperrin  */
13167bd71c6Sperrin int zvol_maxphys = DMU_MAX_ACCESS/2;
13267bd71c6Sperrin 
133e7cbe64fSgw25295 extern int zfs_set_prop_nvlist(const char *, nvlist_t *);
134feb08c6bSbillm static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio);
135e7cbe64fSgw25295 static int zvol_dumpify(zvol_state_t *zv);
136e7cbe64fSgw25295 static int zvol_dump_fini(zvol_state_t *zv);
137e7cbe64fSgw25295 static int zvol_dump_init(zvol_state_t *zv, boolean_t resize);
13867bd71c6Sperrin 
139fa9e4066Sahrens static void
14091ebeef5Sahrens zvol_size_changed(zvol_state_t *zv, major_t maj)
141fa9e4066Sahrens {
14291ebeef5Sahrens 	dev_t dev = makedevice(maj, zv->zv_minor);
143fa9e4066Sahrens 
144fa9e4066Sahrens 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
145fa9e4066Sahrens 	    "Size", zv->zv_volsize) == DDI_SUCCESS);
146fa9e4066Sahrens 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
147fa9e4066Sahrens 	    "Nblocks", lbtodb(zv->zv_volsize)) == DDI_SUCCESS);
148e7cbe64fSgw25295 
149e7cbe64fSgw25295 	/* Notify specfs to invalidate the cached size */
150e7cbe64fSgw25295 	spec_size_invalidate(dev, VBLK);
151e7cbe64fSgw25295 	spec_size_invalidate(dev, VCHR);
152fa9e4066Sahrens }
153fa9e4066Sahrens 
154fa9e4066Sahrens int
155e9dbad6fSeschrock zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
156fa9e4066Sahrens {
157e9dbad6fSeschrock 	if (volsize == 0)
158fa9e4066Sahrens 		return (EINVAL);
159fa9e4066Sahrens 
160e9dbad6fSeschrock 	if (volsize % blocksize != 0)
1615c5460e9Seschrock 		return (EINVAL);
1625c5460e9Seschrock 
163fa9e4066Sahrens #ifdef _ILP32
164e9dbad6fSeschrock 	if (volsize - 1 > SPEC_MAXOFFSET_T)
165fa9e4066Sahrens 		return (EOVERFLOW);
166fa9e4066Sahrens #endif
167fa9e4066Sahrens 	return (0);
168fa9e4066Sahrens }
169fa9e4066Sahrens 
170fa9e4066Sahrens int
171e9dbad6fSeschrock zvol_check_volblocksize(uint64_t volblocksize)
172fa9e4066Sahrens {
173e9dbad6fSeschrock 	if (volblocksize < SPA_MINBLOCKSIZE ||
174e9dbad6fSeschrock 	    volblocksize > SPA_MAXBLOCKSIZE ||
175e9dbad6fSeschrock 	    !ISP2(volblocksize))
176fa9e4066Sahrens 		return (EDOM);
177fa9e4066Sahrens 
178fa9e4066Sahrens 	return (0);
179fa9e4066Sahrens }
180fa9e4066Sahrens 
181fa9e4066Sahrens static void
182fa9e4066Sahrens zvol_readonly_changed_cb(void *arg, uint64_t newval)
183fa9e4066Sahrens {
184fa9e4066Sahrens 	zvol_state_t *zv = arg;
185fa9e4066Sahrens 
186e7cbe64fSgw25295 	if (newval)
187e7cbe64fSgw25295 		zv->zv_flags |= ZVOL_RDONLY;
188e7cbe64fSgw25295 	else
189e7cbe64fSgw25295 		zv->zv_flags &= ~ZVOL_RDONLY;
190fa9e4066Sahrens }
191fa9e4066Sahrens 
192fa9e4066Sahrens int
193a2eea2e1Sahrens zvol_get_stats(objset_t *os, nvlist_t *nv)
194fa9e4066Sahrens {
195fa9e4066Sahrens 	int error;
196fa9e4066Sahrens 	dmu_object_info_t doi;
197a2eea2e1Sahrens 	uint64_t val;
198fa9e4066Sahrens 
199fa9e4066Sahrens 
200a2eea2e1Sahrens 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
201fa9e4066Sahrens 	if (error)
202fa9e4066Sahrens 		return (error);
203fa9e4066Sahrens 
204a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
205a2eea2e1Sahrens 
206fa9e4066Sahrens 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
207fa9e4066Sahrens 
208a2eea2e1Sahrens 	if (error == 0) {
209a2eea2e1Sahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
210a2eea2e1Sahrens 		    doi.doi_data_block_size);
211a2eea2e1Sahrens 	}
212fa9e4066Sahrens 
213fa9e4066Sahrens 	return (error);
214fa9e4066Sahrens }
215fa9e4066Sahrens 
216fa9e4066Sahrens /*
217fa9e4066Sahrens  * Find a free minor number.
218fa9e4066Sahrens  */
219fa9e4066Sahrens static minor_t
220fa9e4066Sahrens zvol_minor_alloc(void)
221fa9e4066Sahrens {
222fa9e4066Sahrens 	minor_t minor;
223fa9e4066Sahrens 
224fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&zvol_state_lock));
225fa9e4066Sahrens 
226fa9e4066Sahrens 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++)
227fa9e4066Sahrens 		if (ddi_get_soft_state(zvol_state, minor) == NULL)
228fa9e4066Sahrens 			return (minor);
229fa9e4066Sahrens 
230fa9e4066Sahrens 	return (0);
231fa9e4066Sahrens }
232fa9e4066Sahrens 
233fa9e4066Sahrens static zvol_state_t *
234e9dbad6fSeschrock zvol_minor_lookup(const char *name)
235fa9e4066Sahrens {
236fa9e4066Sahrens 	minor_t minor;
237fa9e4066Sahrens 	zvol_state_t *zv;
238fa9e4066Sahrens 
239fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&zvol_state_lock));
240fa9e4066Sahrens 
241fa9e4066Sahrens 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) {
242fa9e4066Sahrens 		zv = ddi_get_soft_state(zvol_state, minor);
243fa9e4066Sahrens 		if (zv == NULL)
244fa9e4066Sahrens 			continue;
245fa9e4066Sahrens 		if (strcmp(zv->zv_name, name) == 0)
246fa9e4066Sahrens 			break;
247fa9e4066Sahrens 	}
248fa9e4066Sahrens 
249fa9e4066Sahrens 	return (zv);
250fa9e4066Sahrens }
251fa9e4066Sahrens 
252e7cbe64fSgw25295 /* extent mapping arg */
253e7cbe64fSgw25295 struct maparg {
25488b7b0f2SMatthew Ahrens 	zvol_state_t	*ma_zv;
25588b7b0f2SMatthew Ahrens 	uint64_t	ma_blks;
256e7cbe64fSgw25295 };
257e7cbe64fSgw25295 
258e7cbe64fSgw25295 /*ARGSUSED*/
259e7cbe64fSgw25295 static int
26088b7b0f2SMatthew Ahrens zvol_map_block(spa_t *spa, blkptr_t *bp, const zbookmark_t *zb,
26188b7b0f2SMatthew Ahrens     const dnode_phys_t *dnp, void *arg)
262e7cbe64fSgw25295 {
26388b7b0f2SMatthew Ahrens 	struct maparg *ma = arg;
26488b7b0f2SMatthew Ahrens 	zvol_extent_t *ze;
26588b7b0f2SMatthew Ahrens 	int bs = ma->ma_zv->zv_volblocksize;
266e7cbe64fSgw25295 
26788b7b0f2SMatthew Ahrens 	if (bp == NULL || zb->zb_object != ZVOL_OBJ || zb->zb_level != 0)
268e7cbe64fSgw25295 		return (0);
269e7cbe64fSgw25295 
27088b7b0f2SMatthew Ahrens 	VERIFY3U(ma->ma_blks, ==, zb->zb_blkid);
27188b7b0f2SMatthew Ahrens 	ma->ma_blks++;
27288b7b0f2SMatthew Ahrens 
273e7cbe64fSgw25295 	/* Abort immediately if we have encountered gang blocks */
27488b7b0f2SMatthew Ahrens 	if (BP_IS_GANG(bp))
27588b7b0f2SMatthew Ahrens 		return (EFRAGS);
276e7cbe64fSgw25295 
277e7cbe64fSgw25295 	/*
27888b7b0f2SMatthew Ahrens 	 * See if the block is at the end of the previous extent.
279e7cbe64fSgw25295 	 */
28088b7b0f2SMatthew Ahrens 	ze = list_tail(&ma->ma_zv->zv_extents);
28188b7b0f2SMatthew Ahrens 	if (ze &&
28288b7b0f2SMatthew Ahrens 	    DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) &&
28388b7b0f2SMatthew Ahrens 	    DVA_GET_OFFSET(BP_IDENTITY(bp)) ==
28488b7b0f2SMatthew Ahrens 	    DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) {
28588b7b0f2SMatthew Ahrens 		ze->ze_nblks++;
28688b7b0f2SMatthew Ahrens 		return (0);
28788b7b0f2SMatthew Ahrens 	}
28888b7b0f2SMatthew Ahrens 
289e7cbe64fSgw25295 	dprintf_bp(bp, "%s", "next blkptr:");
29088b7b0f2SMatthew Ahrens 
291e7cbe64fSgw25295 	/* start a new extent */
29288b7b0f2SMatthew Ahrens 	ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP);
29388b7b0f2SMatthew Ahrens 	ze->ze_dva = bp->blk_dva[0];	/* structure assignment */
29488b7b0f2SMatthew Ahrens 	ze->ze_nblks = 1;
29588b7b0f2SMatthew Ahrens 	list_insert_tail(&ma->ma_zv->zv_extents, ze);
29688b7b0f2SMatthew Ahrens 	return (0);
297e7cbe64fSgw25295 }
29888b7b0f2SMatthew Ahrens 
29988b7b0f2SMatthew Ahrens static void
30088b7b0f2SMatthew Ahrens zvol_free_extents(zvol_state_t *zv)
30188b7b0f2SMatthew Ahrens {
30288b7b0f2SMatthew Ahrens 	zvol_extent_t *ze;
30388b7b0f2SMatthew Ahrens 
30488b7b0f2SMatthew Ahrens 	while (ze = list_head(&zv->zv_extents)) {
30588b7b0f2SMatthew Ahrens 		list_remove(&zv->zv_extents, ze);
30688b7b0f2SMatthew Ahrens 		kmem_free(ze, sizeof (zvol_extent_t));
30788b7b0f2SMatthew Ahrens 	}
30888b7b0f2SMatthew Ahrens }
30988b7b0f2SMatthew Ahrens 
31088b7b0f2SMatthew Ahrens static int
31188b7b0f2SMatthew Ahrens zvol_get_lbas(zvol_state_t *zv)
31288b7b0f2SMatthew Ahrens {
31388b7b0f2SMatthew Ahrens 	struct maparg	ma;
31488b7b0f2SMatthew Ahrens 	int		err;
31588b7b0f2SMatthew Ahrens 
31688b7b0f2SMatthew Ahrens 	ma.ma_zv = zv;
31788b7b0f2SMatthew Ahrens 	ma.ma_blks = 0;
31888b7b0f2SMatthew Ahrens 	zvol_free_extents(zv);
31988b7b0f2SMatthew Ahrens 
32088b7b0f2SMatthew Ahrens 	err = traverse_dataset(dmu_objset_ds(zv->zv_objset), 0,
32188b7b0f2SMatthew Ahrens 	    TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma);
32288b7b0f2SMatthew Ahrens 	if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) {
32388b7b0f2SMatthew Ahrens 		zvol_free_extents(zv);
32488b7b0f2SMatthew Ahrens 		return (err ? err : EIO);
32588b7b0f2SMatthew Ahrens 	}
32688b7b0f2SMatthew Ahrens 
327e7cbe64fSgw25295 	return (0);
328e7cbe64fSgw25295 }
329e7cbe64fSgw25295 
330ecd6cf80Smarks /* ARGSUSED */
331fa9e4066Sahrens void
332ecd6cf80Smarks zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
333fa9e4066Sahrens {
334da6c28aaSamw 	zfs_creat_t *zct = arg;
335da6c28aaSamw 	nvlist_t *nvprops = zct->zct_props;
336fa9e4066Sahrens 	int error;
337e9dbad6fSeschrock 	uint64_t volblocksize, volsize;
338fa9e4066Sahrens 
339ecd6cf80Smarks 	VERIFY(nvlist_lookup_uint64(nvprops,
340e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
341ecd6cf80Smarks 	if (nvlist_lookup_uint64(nvprops,
342e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
343e9dbad6fSeschrock 		volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
344e9dbad6fSeschrock 
345e9dbad6fSeschrock 	/*
346e7cbe64fSgw25295 	 * These properties must be removed from the list so the generic
347e9dbad6fSeschrock 	 * property setting step won't apply to them.
348e9dbad6fSeschrock 	 */
349ecd6cf80Smarks 	VERIFY(nvlist_remove_all(nvprops,
350e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
351ecd6cf80Smarks 	(void) nvlist_remove_all(nvprops,
352e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
353e9dbad6fSeschrock 
354e9dbad6fSeschrock 	error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
355fa9e4066Sahrens 	    DMU_OT_NONE, 0, tx);
356fa9e4066Sahrens 	ASSERT(error == 0);
357fa9e4066Sahrens 
358fa9e4066Sahrens 	error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
359fa9e4066Sahrens 	    DMU_OT_NONE, 0, tx);
360fa9e4066Sahrens 	ASSERT(error == 0);
361fa9e4066Sahrens 
362e9dbad6fSeschrock 	error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
363fa9e4066Sahrens 	ASSERT(error == 0);
364fa9e4066Sahrens }
365fa9e4066Sahrens 
366fa9e4066Sahrens /*
36722ac5be4Sperrin  * Replay a TX_WRITE ZIL transaction that didn't get committed
36822ac5be4Sperrin  * after a system failure
36922ac5be4Sperrin  */
37022ac5be4Sperrin static int
37122ac5be4Sperrin zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
37222ac5be4Sperrin {
37322ac5be4Sperrin 	objset_t *os = zv->zv_objset;
37422ac5be4Sperrin 	char *data = (char *)(lr + 1);	/* data follows lr_write_t */
37522ac5be4Sperrin 	uint64_t off = lr->lr_offset;
37622ac5be4Sperrin 	uint64_t len = lr->lr_length;
37722ac5be4Sperrin 	dmu_tx_t *tx;
37822ac5be4Sperrin 	int error;
37922ac5be4Sperrin 
38022ac5be4Sperrin 	if (byteswap)
38122ac5be4Sperrin 		byteswap_uint64_array(lr, sizeof (*lr));
38222ac5be4Sperrin 
38322ac5be4Sperrin 	tx = dmu_tx_create(os);
38422ac5be4Sperrin 	dmu_tx_hold_write(tx, ZVOL_OBJ, off, len);
3851209a471SNeil Perrin 	error = dmu_tx_assign(tx, TXG_WAIT);
38622ac5be4Sperrin 	if (error) {
38722ac5be4Sperrin 		dmu_tx_abort(tx);
38822ac5be4Sperrin 	} else {
38922ac5be4Sperrin 		dmu_write(os, ZVOL_OBJ, off, len, data, tx);
39022ac5be4Sperrin 		dmu_tx_commit(tx);
39122ac5be4Sperrin 	}
39222ac5be4Sperrin 
39322ac5be4Sperrin 	return (error);
39422ac5be4Sperrin }
39522ac5be4Sperrin 
39622ac5be4Sperrin /* ARGSUSED */
39722ac5be4Sperrin static int
39822ac5be4Sperrin zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
39922ac5be4Sperrin {
40022ac5be4Sperrin 	return (ENOTSUP);
40122ac5be4Sperrin }
40222ac5be4Sperrin 
40322ac5be4Sperrin /*
40422ac5be4Sperrin  * Callback vectors for replaying records.
40522ac5be4Sperrin  * Only TX_WRITE is needed for zvol.
40622ac5be4Sperrin  */
40722ac5be4Sperrin zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = {
40822ac5be4Sperrin 	zvol_replay_err,	/* 0 no such transaction type */
40922ac5be4Sperrin 	zvol_replay_err,	/* TX_CREATE */
41022ac5be4Sperrin 	zvol_replay_err,	/* TX_MKDIR */
41122ac5be4Sperrin 	zvol_replay_err,	/* TX_MKXATTR */
41222ac5be4Sperrin 	zvol_replay_err,	/* TX_SYMLINK */
41322ac5be4Sperrin 	zvol_replay_err,	/* TX_REMOVE */
41422ac5be4Sperrin 	zvol_replay_err,	/* TX_RMDIR */
41522ac5be4Sperrin 	zvol_replay_err,	/* TX_LINK */
41622ac5be4Sperrin 	zvol_replay_err,	/* TX_RENAME */
41722ac5be4Sperrin 	zvol_replay_write,	/* TX_WRITE */
41822ac5be4Sperrin 	zvol_replay_err,	/* TX_TRUNCATE */
41922ac5be4Sperrin 	zvol_replay_err,	/* TX_SETATTR */
42022ac5be4Sperrin 	zvol_replay_err,	/* TX_ACL */
42122ac5be4Sperrin };
42222ac5be4Sperrin 
42322ac5be4Sperrin /*
424e7cbe64fSgw25295  * Create a minor node (plus a whole lot more) for the specified volume.
425fa9e4066Sahrens  */
426fa9e4066Sahrens int
42791ebeef5Sahrens zvol_create_minor(const char *name, major_t maj)
428fa9e4066Sahrens {
429fa9e4066Sahrens 	zvol_state_t *zv;
430fa9e4066Sahrens 	objset_t *os;
43167bd71c6Sperrin 	dmu_object_info_t doi;
432fa9e4066Sahrens 	uint64_t volsize;
433fa9e4066Sahrens 	minor_t minor = 0;
434fa9e4066Sahrens 	struct pathname linkpath;
435745cd3c5Smaybee 	int ds_mode = DS_MODE_OWNER;
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 
448fa9e4066Sahrens 	if (strchr(name, '@') != 0)
449fa9e4066Sahrens 		ds_mode |= DS_MODE_READONLY;
450fa9e4066Sahrens 
451fa9e4066Sahrens 	error = dmu_objset_open(name, DMU_OST_ZVOL, ds_mode, &os);
452fa9e4066Sahrens 
453fa9e4066Sahrens 	if (error) {
454fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
455fa9e4066Sahrens 		return (error);
456fa9e4066Sahrens 	}
457fa9e4066Sahrens 
458fa9e4066Sahrens 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
459fa9e4066Sahrens 
460fa9e4066Sahrens 	if (error) {
461fa9e4066Sahrens 		dmu_objset_close(os);
462fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
463fa9e4066Sahrens 		return (error);
464fa9e4066Sahrens 	}
465fa9e4066Sahrens 
466fa9e4066Sahrens 	/*
467fa9e4066Sahrens 	 * If there's an existing /dev/zvol symlink, try to use the
468fa9e4066Sahrens 	 * same minor number we used last time.
469fa9e4066Sahrens 	 */
470*ae46e4c7SMatthew Ahrens 	devpath = kmem_asprintf("%s%s", ZVOL_FULL_DEV_DIR, name);
471fa9e4066Sahrens 	error = lookupname(devpath, UIO_SYSSPACE, NO_FOLLOW, NULL, &vp);
472*ae46e4c7SMatthew Ahrens 	strfree(devpath);
473fa9e4066Sahrens 
474fa9e4066Sahrens 	if (error == 0 && vp->v_type != VLNK)
475fa9e4066Sahrens 		error = EINVAL;
476fa9e4066Sahrens 
477fa9e4066Sahrens 	if (error == 0) {
478fa9e4066Sahrens 		pn_alloc(&linkpath);
479fa9e4066Sahrens 		error = pn_getsymlink(vp, &linkpath, kcred);
480fa9e4066Sahrens 		if (error == 0) {
481fa9e4066Sahrens 			char *ms = strstr(linkpath.pn_path, ZVOL_PSEUDO_DEV);
482fa9e4066Sahrens 			if (ms != NULL) {
483fa9e4066Sahrens 				ms += strlen(ZVOL_PSEUDO_DEV);
484fa9e4066Sahrens 				minor = stoi(&ms);
485fa9e4066Sahrens 			}
486fa9e4066Sahrens 		}
487fa9e4066Sahrens 		pn_free(&linkpath);
488fa9e4066Sahrens 	}
489fa9e4066Sahrens 
490fa9e4066Sahrens 	if (vp != NULL)
491fa9e4066Sahrens 		VN_RELE(vp);
492fa9e4066Sahrens 
493fa9e4066Sahrens 	/*
494fa9e4066Sahrens 	 * If we found a minor but it's already in use, we must pick a new one.
495fa9e4066Sahrens 	 */
496fa9e4066Sahrens 	if (minor != 0 && ddi_get_soft_state(zvol_state, minor) != NULL)
497fa9e4066Sahrens 		minor = 0;
498fa9e4066Sahrens 
499fa9e4066Sahrens 	if (minor == 0)
500fa9e4066Sahrens 		minor = zvol_minor_alloc();
501fa9e4066Sahrens 
502fa9e4066Sahrens 	if (minor == 0) {
503fa9e4066Sahrens 		dmu_objset_close(os);
504fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
505fa9e4066Sahrens 		return (ENXIO);
506fa9e4066Sahrens 	}
507fa9e4066Sahrens 
508fa9e4066Sahrens 	if (ddi_soft_state_zalloc(zvol_state, minor) != DDI_SUCCESS) {
509fa9e4066Sahrens 		dmu_objset_close(os);
510fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
511fa9e4066Sahrens 		return (EAGAIN);
512fa9e4066Sahrens 	}
513fa9e4066Sahrens 
514e9dbad6fSeschrock 	(void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME,
515e9dbad6fSeschrock 	    (char *)name);
516fa9e4066Sahrens 
517fa9e4066Sahrens 	(void) sprintf(chrbuf, "%uc,raw", minor);
518fa9e4066Sahrens 
519fa9e4066Sahrens 	if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR,
520fa9e4066Sahrens 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
521fa9e4066Sahrens 		ddi_soft_state_free(zvol_state, minor);
522fa9e4066Sahrens 		dmu_objset_close(os);
523fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
524fa9e4066Sahrens 		return (EAGAIN);
525fa9e4066Sahrens 	}
526fa9e4066Sahrens 
527fa9e4066Sahrens 	(void) sprintf(blkbuf, "%uc", minor);
528fa9e4066Sahrens 
529fa9e4066Sahrens 	if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK,
530fa9e4066Sahrens 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
531fa9e4066Sahrens 		ddi_remove_minor_node(zfs_dip, chrbuf);
532fa9e4066Sahrens 		ddi_soft_state_free(zvol_state, minor);
533fa9e4066Sahrens 		dmu_objset_close(os);
534fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
535fa9e4066Sahrens 		return (EAGAIN);
536fa9e4066Sahrens 	}
537fa9e4066Sahrens 
538fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
539fa9e4066Sahrens 
540fa9e4066Sahrens 	(void) strcpy(zv->zv_name, name);
541fa9e4066Sahrens 	zv->zv_min_bs = DEV_BSHIFT;
542fa9e4066Sahrens 	zv->zv_minor = minor;
543fa9e4066Sahrens 	zv->zv_volsize = volsize;
544fa9e4066Sahrens 	zv->zv_objset = os;
545fa9e4066Sahrens 	zv->zv_mode = ds_mode;
54667bd71c6Sperrin 	zv->zv_zilog = zil_open(os, zvol_get_data);
547c2e6a7d6Sperrin 	mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
548c2e6a7d6Sperrin 	avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
549c2e6a7d6Sperrin 	    sizeof (rl_t), offsetof(rl_t, r_node));
55088b7b0f2SMatthew Ahrens 	list_create(&zv->zv_extents, sizeof (zvol_extent_t),
55188b7b0f2SMatthew Ahrens 	    offsetof(zvol_extent_t, ze_node));
55267bd71c6Sperrin 	/* get and cache the blocksize */
55367bd71c6Sperrin 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
55467bd71c6Sperrin 	ASSERT(error == 0);
55567bd71c6Sperrin 	zv->zv_volblocksize = doi.doi_data_block_size;
55622ac5be4Sperrin 
5571209a471SNeil Perrin 	zil_replay(os, zv, zvol_replay_vector);
55891ebeef5Sahrens 	zvol_size_changed(zv, maj);
559fa9e4066Sahrens 
560ea8dc4b6Seschrock 	/* XXX this should handle the possible i/o error */
561fa9e4066Sahrens 	VERIFY(dsl_prop_register(dmu_objset_ds(zv->zv_objset),
562fa9e4066Sahrens 	    "readonly", zvol_readonly_changed_cb, zv) == 0);
563fa9e4066Sahrens 
564fa9e4066Sahrens 	zvol_minors++;
565fa9e4066Sahrens 
566fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
567fa9e4066Sahrens 
568fa9e4066Sahrens 	return (0);
569fa9e4066Sahrens }
570fa9e4066Sahrens 
571fa9e4066Sahrens /*
572fa9e4066Sahrens  * Remove minor node for the specified volume.
573fa9e4066Sahrens  */
574fa9e4066Sahrens int
575e9dbad6fSeschrock zvol_remove_minor(const char *name)
576fa9e4066Sahrens {
577fa9e4066Sahrens 	zvol_state_t *zv;
578fa9e4066Sahrens 	char namebuf[30];
579fa9e4066Sahrens 
580fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
581fa9e4066Sahrens 
582e9dbad6fSeschrock 	if ((zv = zvol_minor_lookup(name)) == NULL) {
583fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
584fa9e4066Sahrens 		return (ENXIO);
585fa9e4066Sahrens 	}
586fa9e4066Sahrens 
587fa9e4066Sahrens 	if (zv->zv_total_opens != 0) {
588fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
589fa9e4066Sahrens 		return (EBUSY);
590fa9e4066Sahrens 	}
591fa9e4066Sahrens 
592fa9e4066Sahrens 	(void) sprintf(namebuf, "%uc,raw", zv->zv_minor);
593fa9e4066Sahrens 	ddi_remove_minor_node(zfs_dip, namebuf);
594fa9e4066Sahrens 
595fa9e4066Sahrens 	(void) sprintf(namebuf, "%uc", zv->zv_minor);
596fa9e4066Sahrens 	ddi_remove_minor_node(zfs_dip, namebuf);
597fa9e4066Sahrens 
598fa9e4066Sahrens 	VERIFY(dsl_prop_unregister(dmu_objset_ds(zv->zv_objset),
599fa9e4066Sahrens 	    "readonly", zvol_readonly_changed_cb, zv) == 0);
600fa9e4066Sahrens 
60122ac5be4Sperrin 	zil_close(zv->zv_zilog);
60222ac5be4Sperrin 	zv->zv_zilog = NULL;
603fa9e4066Sahrens 	dmu_objset_close(zv->zv_objset);
604fa9e4066Sahrens 	zv->zv_objset = NULL;
605c2e6a7d6Sperrin 	avl_destroy(&zv->zv_znode.z_range_avl);
606c2e6a7d6Sperrin 	mutex_destroy(&zv->zv_znode.z_range_lock);
607fa9e4066Sahrens 
608fa9e4066Sahrens 	ddi_soft_state_free(zvol_state, zv->zv_minor);
609fa9e4066Sahrens 
610fa9e4066Sahrens 	zvol_minors--;
611fa9e4066Sahrens 
612fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
613fa9e4066Sahrens 
614fa9e4066Sahrens 	return (0);
615fa9e4066Sahrens }
616fa9e4066Sahrens 
617e7cbe64fSgw25295 int
618e7cbe64fSgw25295 zvol_prealloc(zvol_state_t *zv)
619e7cbe64fSgw25295 {
620e7cbe64fSgw25295 	objset_t *os = zv->zv_objset;
621e7cbe64fSgw25295 	dmu_tx_t *tx;
622e7cbe64fSgw25295 	uint64_t refd, avail, usedobjs, availobjs;
623e7cbe64fSgw25295 	uint64_t resid = zv->zv_volsize;
624e7cbe64fSgw25295 	uint64_t off = 0;
625e7cbe64fSgw25295 
626e7cbe64fSgw25295 	/* Check the space usage before attempting to allocate the space */
627e7cbe64fSgw25295 	dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs);
628e7cbe64fSgw25295 	if (avail < zv->zv_volsize)
629e7cbe64fSgw25295 		return (ENOSPC);
630e7cbe64fSgw25295 
631e7cbe64fSgw25295 	/* Free old extents if they exist */
632e7cbe64fSgw25295 	zvol_free_extents(zv);
633e7cbe64fSgw25295 
634e7cbe64fSgw25295 	while (resid != 0) {
635e7cbe64fSgw25295 		int error;
636e7cbe64fSgw25295 		uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE);
637e7cbe64fSgw25295 
638e7cbe64fSgw25295 		tx = dmu_tx_create(os);
639e7cbe64fSgw25295 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
640e7cbe64fSgw25295 		error = dmu_tx_assign(tx, TXG_WAIT);
641e7cbe64fSgw25295 		if (error) {
642e7cbe64fSgw25295 			dmu_tx_abort(tx);
643cdb0ab79Smaybee 			(void) dmu_free_long_range(os, ZVOL_OBJ, 0, off);
644e7cbe64fSgw25295 			return (error);
645e7cbe64fSgw25295 		}
64682c9918fSTim Haley 		dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx);
647e7cbe64fSgw25295 		dmu_tx_commit(tx);
648e7cbe64fSgw25295 		off += bytes;
649e7cbe64fSgw25295 		resid -= bytes;
650e7cbe64fSgw25295 	}
651e7cbe64fSgw25295 	txg_wait_synced(dmu_objset_pool(os), 0);
652e7cbe64fSgw25295 
653e7cbe64fSgw25295 	return (0);
654e7cbe64fSgw25295 }
655e7cbe64fSgw25295 
656e7cbe64fSgw25295 int
657e7cbe64fSgw25295 zvol_update_volsize(zvol_state_t *zv, major_t maj, uint64_t volsize)
658e7cbe64fSgw25295 {
659e7cbe64fSgw25295 	dmu_tx_t *tx;
660e7cbe64fSgw25295 	int error;
661e7cbe64fSgw25295 
662e7cbe64fSgw25295 	ASSERT(MUTEX_HELD(&zvol_state_lock));
663e7cbe64fSgw25295 
664e7cbe64fSgw25295 	tx = dmu_tx_create(zv->zv_objset);
665e7cbe64fSgw25295 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
666e7cbe64fSgw25295 	error = dmu_tx_assign(tx, TXG_WAIT);
667e7cbe64fSgw25295 	if (error) {
668e7cbe64fSgw25295 		dmu_tx_abort(tx);
669e7cbe64fSgw25295 		return (error);
670e7cbe64fSgw25295 	}
671e7cbe64fSgw25295 
672e7cbe64fSgw25295 	error = zap_update(zv->zv_objset, ZVOL_ZAP_OBJ, "size", 8, 1,
673e7cbe64fSgw25295 	    &volsize, tx);
674e7cbe64fSgw25295 	dmu_tx_commit(tx);
675e7cbe64fSgw25295 
676e7cbe64fSgw25295 	if (error == 0)
677cdb0ab79Smaybee 		error = dmu_free_long_range(zv->zv_objset,
678cdb0ab79Smaybee 		    ZVOL_OBJ, volsize, DMU_OBJECT_END);
679e7cbe64fSgw25295 
680bb0ade09Sahrens 	/*
681bb0ade09Sahrens 	 * If we are using a faked-up state (zv_minor == 0) then don't
682bb0ade09Sahrens 	 * try to update the in-core zvol state.
683bb0ade09Sahrens 	 */
684bb0ade09Sahrens 	if (error == 0 && zv->zv_minor) {
685e7cbe64fSgw25295 		zv->zv_volsize = volsize;
686e7cbe64fSgw25295 		zvol_size_changed(zv, maj);
687e7cbe64fSgw25295 	}
688e7cbe64fSgw25295 	return (error);
689e7cbe64fSgw25295 }
690e7cbe64fSgw25295 
691fa9e4066Sahrens int
69291ebeef5Sahrens zvol_set_volsize(const char *name, major_t maj, uint64_t volsize)
693fa9e4066Sahrens {
694fa9e4066Sahrens 	zvol_state_t *zv;
695fa9e4066Sahrens 	int error;
6965c5460e9Seschrock 	dmu_object_info_t doi;
697e7cbe64fSgw25295 	uint64_t old_volsize = 0ULL;
698bb0ade09Sahrens 	zvol_state_t state = { 0 };
699fa9e4066Sahrens 
700fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
701fa9e4066Sahrens 
702e9dbad6fSeschrock 	if ((zv = zvol_minor_lookup(name)) == NULL) {
703bb0ade09Sahrens 		/*
704bb0ade09Sahrens 		 * If we are doing a "zfs clone -o volsize=", then the
705bb0ade09Sahrens 		 * minor node won't exist yet.
706bb0ade09Sahrens 		 */
707bb0ade09Sahrens 		error = dmu_objset_open(name, DMU_OST_ZVOL, DS_MODE_OWNER,
708bb0ade09Sahrens 		    &state.zv_objset);
709bb0ade09Sahrens 		if (error != 0)
710bb0ade09Sahrens 			goto out;
711bb0ade09Sahrens 		zv = &state;
712fa9e4066Sahrens 	}
713e7cbe64fSgw25295 	old_volsize = zv->zv_volsize;
714fa9e4066Sahrens 
7155c5460e9Seschrock 	if ((error = dmu_object_info(zv->zv_objset, ZVOL_OBJ, &doi)) != 0 ||
716e9dbad6fSeschrock 	    (error = zvol_check_volsize(volsize,
717bb0ade09Sahrens 	    doi.doi_data_block_size)) != 0)
718bb0ade09Sahrens 		goto out;
7195c5460e9Seschrock 
720e7cbe64fSgw25295 	if (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY)) {
721bb0ade09Sahrens 		error = EROFS;
722bb0ade09Sahrens 		goto out;
723fa9e4066Sahrens 	}
724fa9e4066Sahrens 
725e7cbe64fSgw25295 	error = zvol_update_volsize(zv, maj, volsize);
726e7cbe64fSgw25295 
727e7cbe64fSgw25295 	/*
728e7cbe64fSgw25295 	 * Reinitialize the dump area to the new size. If we
729e7cbe64fSgw25295 	 * failed to resize the dump area then restore the it back to
730e7cbe64fSgw25295 	 * it's original size.
731e7cbe64fSgw25295 	 */
732e7cbe64fSgw25295 	if (error == 0 && zv->zv_flags & ZVOL_DUMPIFIED) {
733e7cbe64fSgw25295 		if ((error = zvol_dumpify(zv)) != 0 ||
734e7cbe64fSgw25295 		    (error = dumpvp_resize()) != 0) {
735e7cbe64fSgw25295 			(void) zvol_update_volsize(zv, maj, old_volsize);
736e7cbe64fSgw25295 			error = zvol_dumpify(zv);
737fa9e4066Sahrens 		}
738fa9e4066Sahrens 	}
739fa9e4066Sahrens 
740573ca77eSGeorge Wilson 	/*
741573ca77eSGeorge Wilson 	 * Generate a LUN expansion event.
742573ca77eSGeorge Wilson 	 */
743573ca77eSGeorge Wilson 	if (error == 0) {
744573ca77eSGeorge Wilson 		sysevent_id_t eid;
745573ca77eSGeorge Wilson 		nvlist_t *attr;
746573ca77eSGeorge Wilson 		char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
747573ca77eSGeorge Wilson 
748573ca77eSGeorge Wilson 		(void) snprintf(physpath, MAXPATHLEN, "%s%uc", ZVOL_PSEUDO_DEV,
749573ca77eSGeorge Wilson 		    zv->zv_minor);
750573ca77eSGeorge Wilson 
751573ca77eSGeorge Wilson 		VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
752573ca77eSGeorge Wilson 		VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
753573ca77eSGeorge Wilson 
754573ca77eSGeorge Wilson 		(void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
755573ca77eSGeorge Wilson 		    ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
756573ca77eSGeorge Wilson 
757573ca77eSGeorge Wilson 		nvlist_free(attr);
758573ca77eSGeorge Wilson 		kmem_free(physpath, MAXPATHLEN);
759573ca77eSGeorge Wilson 	}
760573ca77eSGeorge Wilson 
761bb0ade09Sahrens out:
762bb0ade09Sahrens 	if (state.zv_objset)
763bb0ade09Sahrens 		dmu_objset_close(state.zv_objset);
764bb0ade09Sahrens 
765fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
766fa9e4066Sahrens 
767fa9e4066Sahrens 	return (error);
768fa9e4066Sahrens }
769fa9e4066Sahrens 
770fa9e4066Sahrens int
771e9dbad6fSeschrock zvol_set_volblocksize(const char *name, uint64_t volblocksize)
772fa9e4066Sahrens {
773fa9e4066Sahrens 	zvol_state_t *zv;
774fa9e4066Sahrens 	dmu_tx_t *tx;
775fa9e4066Sahrens 	int error;
77688b7b0f2SMatthew Ahrens 	boolean_t needlock;
777fa9e4066Sahrens 
77888b7b0f2SMatthew Ahrens 	/*
77988b7b0f2SMatthew Ahrens 	 * The lock may already be held if we are being called from
78088b7b0f2SMatthew Ahrens 	 * zvol_dump_init().
78188b7b0f2SMatthew Ahrens 	 */
78288b7b0f2SMatthew Ahrens 	needlock = !MUTEX_HELD(&zvol_state_lock);
78388b7b0f2SMatthew Ahrens 	if (needlock)
784fa9e4066Sahrens 		mutex_enter(&zvol_state_lock);
785fa9e4066Sahrens 
786e9dbad6fSeschrock 	if ((zv = zvol_minor_lookup(name)) == NULL) {
78788b7b0f2SMatthew Ahrens 		if (needlock)
788fa9e4066Sahrens 			mutex_exit(&zvol_state_lock);
789fa9e4066Sahrens 		return (ENXIO);
790fa9e4066Sahrens 	}
791e7cbe64fSgw25295 	if (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY)) {
79288b7b0f2SMatthew Ahrens 		if (needlock)
793fa9e4066Sahrens 			mutex_exit(&zvol_state_lock);
794fa9e4066Sahrens 		return (EROFS);
795fa9e4066Sahrens 	}
796fa9e4066Sahrens 
797fa9e4066Sahrens 	tx = dmu_tx_create(zv->zv_objset);
798fa9e4066Sahrens 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
799fa9e4066Sahrens 	error = dmu_tx_assign(tx, TXG_WAIT);
800fa9e4066Sahrens 	if (error) {
801fa9e4066Sahrens 		dmu_tx_abort(tx);
802fa9e4066Sahrens 	} else {
803fa9e4066Sahrens 		error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ,
804e9dbad6fSeschrock 		    volblocksize, 0, tx);
805fa9e4066Sahrens 		if (error == ENOTSUP)
806fa9e4066Sahrens 			error = EBUSY;
807fa9e4066Sahrens 		dmu_tx_commit(tx);
80888b7b0f2SMatthew Ahrens 		if (error == 0)
80988b7b0f2SMatthew Ahrens 			zv->zv_volblocksize = volblocksize;
810fa9e4066Sahrens 	}
811fa9e4066Sahrens 
81288b7b0f2SMatthew Ahrens 	if (needlock)
813fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
814fa9e4066Sahrens 
815fa9e4066Sahrens 	return (error);
816fa9e4066Sahrens }
817fa9e4066Sahrens 
818fa9e4066Sahrens /*ARGSUSED*/
819fa9e4066Sahrens int
820fa9e4066Sahrens zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr)
821fa9e4066Sahrens {
822fa9e4066Sahrens 	minor_t minor = getminor(*devp);
823fa9e4066Sahrens 	zvol_state_t *zv;
824fa9e4066Sahrens 
825fa9e4066Sahrens 	if (minor == 0)			/* This is the control device */
826fa9e4066Sahrens 		return (0);
827fa9e4066Sahrens 
828fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
829fa9e4066Sahrens 
830fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
831fa9e4066Sahrens 	if (zv == NULL) {
832fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
833fa9e4066Sahrens 		return (ENXIO);
834fa9e4066Sahrens 	}
835fa9e4066Sahrens 
836fa9e4066Sahrens 	ASSERT(zv->zv_objset != NULL);
837fa9e4066Sahrens 
838fa9e4066Sahrens 	if ((flag & FWRITE) &&
839e7cbe64fSgw25295 	    (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY))) {
840fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
841fa9e4066Sahrens 		return (EROFS);
842fa9e4066Sahrens 	}
843c7f714e2SEric Taylor 	if (zv->zv_flags & ZVOL_EXCL) {
844c7f714e2SEric Taylor 		mutex_exit(&zvol_state_lock);
845c7f714e2SEric Taylor 		return (EBUSY);
846c7f714e2SEric Taylor 	}
847c7f714e2SEric Taylor 	if (flag & FEXCL) {
848c7f714e2SEric Taylor 		if (zv->zv_total_opens != 0) {
849c7f714e2SEric Taylor 			mutex_exit(&zvol_state_lock);
850c7f714e2SEric Taylor 			return (EBUSY);
851c7f714e2SEric Taylor 		}
852c7f714e2SEric Taylor 		zv->zv_flags |= ZVOL_EXCL;
853c7f714e2SEric Taylor 	}
854fa9e4066Sahrens 
855fa9e4066Sahrens 	if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) {
856fa9e4066Sahrens 		zv->zv_open_count[otyp]++;
857fa9e4066Sahrens 		zv->zv_total_opens++;
858fa9e4066Sahrens 	}
859fa9e4066Sahrens 
860fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
861fa9e4066Sahrens 
862fa9e4066Sahrens 	return (0);
863fa9e4066Sahrens }
864fa9e4066Sahrens 
865fa9e4066Sahrens /*ARGSUSED*/
866fa9e4066Sahrens int
867fa9e4066Sahrens zvol_close(dev_t dev, int flag, int otyp, cred_t *cr)
868fa9e4066Sahrens {
869fa9e4066Sahrens 	minor_t minor = getminor(dev);
870fa9e4066Sahrens 	zvol_state_t *zv;
871fa9e4066Sahrens 
872fa9e4066Sahrens 	if (minor == 0)		/* This is the control device */
873fa9e4066Sahrens 		return (0);
874fa9e4066Sahrens 
875fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
876fa9e4066Sahrens 
877fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
878fa9e4066Sahrens 	if (zv == NULL) {
879fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
880fa9e4066Sahrens 		return (ENXIO);
881fa9e4066Sahrens 	}
882fa9e4066Sahrens 
883c7f714e2SEric Taylor 	if (zv->zv_flags & ZVOL_EXCL) {
884c7f714e2SEric Taylor 		ASSERT(zv->zv_total_opens == 1);
885c7f714e2SEric Taylor 		zv->zv_flags &= ~ZVOL_EXCL;
886fa9e4066Sahrens 	}
887fa9e4066Sahrens 
888fa9e4066Sahrens 	/*
889fa9e4066Sahrens 	 * If the open count is zero, this is a spurious close.
890fa9e4066Sahrens 	 * That indicates a bug in the kernel / DDI framework.
891fa9e4066Sahrens 	 */
892fa9e4066Sahrens 	ASSERT(zv->zv_open_count[otyp] != 0);
893fa9e4066Sahrens 	ASSERT(zv->zv_total_opens != 0);
894fa9e4066Sahrens 
895fa9e4066Sahrens 	/*
896fa9e4066Sahrens 	 * You may get multiple opens, but only one close.
897fa9e4066Sahrens 	 */
898fa9e4066Sahrens 	zv->zv_open_count[otyp]--;
899fa9e4066Sahrens 	zv->zv_total_opens--;
900fa9e4066Sahrens 
901fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
902fa9e4066Sahrens 
903fa9e4066Sahrens 	return (0);
904fa9e4066Sahrens }
905fa9e4066Sahrens 
906feb08c6bSbillm static void
90767bd71c6Sperrin zvol_get_done(dmu_buf_t *db, void *vzgd)
90867bd71c6Sperrin {
90967bd71c6Sperrin 	zgd_t *zgd = (zgd_t *)vzgd;
910c2e6a7d6Sperrin 	rl_t *rl = zgd->zgd_rl;
91167bd71c6Sperrin 
91267bd71c6Sperrin 	dmu_buf_rele(db, vzgd);
913c2e6a7d6Sperrin 	zfs_range_unlock(rl);
91417f17c2dSbonwick 	zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
91567bd71c6Sperrin 	kmem_free(zgd, sizeof (zgd_t));
91667bd71c6Sperrin }
91767bd71c6Sperrin 
91867bd71c6Sperrin /*
91967bd71c6Sperrin  * Get data to generate a TX_WRITE intent log record.
92067bd71c6Sperrin  */
921feb08c6bSbillm static int
92267bd71c6Sperrin zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
92367bd71c6Sperrin {
92467bd71c6Sperrin 	zvol_state_t *zv = arg;
92567bd71c6Sperrin 	objset_t *os = zv->zv_objset;
92667bd71c6Sperrin 	dmu_buf_t *db;
927c2e6a7d6Sperrin 	rl_t *rl;
92867bd71c6Sperrin 	zgd_t *zgd;
929c2e6a7d6Sperrin 	uint64_t boff; 			/* block starting offset */
930c2e6a7d6Sperrin 	int dlen = lr->lr_length;	/* length of user data */
93167bd71c6Sperrin 	int error;
93267bd71c6Sperrin 
93367bd71c6Sperrin 	ASSERT(zio);
934c2e6a7d6Sperrin 	ASSERT(dlen != 0);
935feb08c6bSbillm 
936c2e6a7d6Sperrin 	/*
937c2e6a7d6Sperrin 	 * Write records come in two flavors: immediate and indirect.
938c2e6a7d6Sperrin 	 * For small writes it's cheaper to store the data with the
939c2e6a7d6Sperrin 	 * log record (immediate); for large writes it's cheaper to
940c2e6a7d6Sperrin 	 * sync the data and get a pointer to it (indirect) so that
941c2e6a7d6Sperrin 	 * we don't have to write the data twice.
942c2e6a7d6Sperrin 	 */
943c2e6a7d6Sperrin 	if (buf != NULL) /* immediate write */
9447bfdf011SNeil Perrin 		return (dmu_read(os, ZVOL_OBJ, lr->lr_offset, dlen, buf,
9457bfdf011SNeil Perrin 		    DMU_READ_NO_PREFETCH));
94667bd71c6Sperrin 
94767bd71c6Sperrin 	zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP);
94867bd71c6Sperrin 	zgd->zgd_zilog = zv->zv_zilog;
94967bd71c6Sperrin 	zgd->zgd_bp = &lr->lr_blkptr;
95067bd71c6Sperrin 
95167bd71c6Sperrin 	/*
952c2e6a7d6Sperrin 	 * Lock the range of the block to ensure that when the data is
953e7cbe64fSgw25295 	 * written out and its checksum is being calculated that no other
954c2e6a7d6Sperrin 	 * thread can change the block.
95567bd71c6Sperrin 	 */
956c2e6a7d6Sperrin 	boff = P2ALIGN_TYPED(lr->lr_offset, zv->zv_volblocksize, uint64_t);
957c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, boff, zv->zv_volblocksize,
958c2e6a7d6Sperrin 	    RL_READER);
959c2e6a7d6Sperrin 	zgd->zgd_rl = rl;
960c2e6a7d6Sperrin 
961c2e6a7d6Sperrin 	VERIFY(0 == dmu_buf_hold(os, ZVOL_OBJ, lr->lr_offset, zgd, &db));
96267bd71c6Sperrin 	error = dmu_sync(zio, db, &lr->lr_blkptr,
96367bd71c6Sperrin 	    lr->lr_common.lrc_txg, zvol_get_done, zgd);
964feb08c6bSbillm 	if (error == 0)
96517f17c2dSbonwick 		zil_add_block(zv->zv_zilog, &lr->lr_blkptr);
96667bd71c6Sperrin 	/*
96767bd71c6Sperrin 	 * If we get EINPROGRESS, then we need to wait for a
96867bd71c6Sperrin 	 * write IO initiated by dmu_sync() to complete before
96967bd71c6Sperrin 	 * we can release this dbuf.  We will finish everything
97067bd71c6Sperrin 	 * up in the zvol_get_done() callback.
97167bd71c6Sperrin 	 */
97267bd71c6Sperrin 	if (error == EINPROGRESS)
97367bd71c6Sperrin 		return (0);
97467bd71c6Sperrin 	dmu_buf_rele(db, zgd);
975c2e6a7d6Sperrin 	zfs_range_unlock(rl);
97667bd71c6Sperrin 	kmem_free(zgd, sizeof (zgd_t));
97767bd71c6Sperrin 	return (error);
97867bd71c6Sperrin }
97967bd71c6Sperrin 
980a24e15ceSperrin /*
981a24e15ceSperrin  * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
98222ac5be4Sperrin  *
98322ac5be4Sperrin  * We store data in the log buffers if it's small enough.
98467bd71c6Sperrin  * Otherwise we will later flush the data out via dmu_sync().
98522ac5be4Sperrin  */
98667bd71c6Sperrin ssize_t zvol_immediate_write_sz = 32768;
98722ac5be4Sperrin 
988feb08c6bSbillm static void
989510b6c0eSNeil Perrin zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid,
990510b6c0eSNeil Perrin     boolean_t sync)
99122ac5be4Sperrin {
992feb08c6bSbillm 	uint32_t blocksize = zv->zv_volblocksize;
9931209a471SNeil Perrin 	zilog_t *zilog = zv->zv_zilog;
994510b6c0eSNeil Perrin 	boolean_t slogging;
995510b6c0eSNeil Perrin 
996510b6c0eSNeil Perrin 	if (zil_disable)
997510b6c0eSNeil Perrin 		return;
998a24e15ceSperrin 
9991209a471SNeil Perrin 	if (zilog->zl_replay) {
10001209a471SNeil Perrin 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
10011209a471SNeil Perrin 		zilog->zl_replayed_seq[dmu_tx_get_txg(tx) & TXG_MASK] =
10021209a471SNeil Perrin 		    zilog->zl_replaying_seq;
10031209a471SNeil Perrin 		return;
10041209a471SNeil Perrin 	}
10051209a471SNeil Perrin 
1006510b6c0eSNeil Perrin 	slogging = spa_has_slogs(zilog->zl_spa);
1007feb08c6bSbillm 
1008510b6c0eSNeil Perrin 	while (resid) {
1009510b6c0eSNeil Perrin 		itx_t *itx;
1010510b6c0eSNeil Perrin 		lr_write_t *lr;
1011510b6c0eSNeil Perrin 		ssize_t len;
1012510b6c0eSNeil Perrin 		itx_wr_state_t write_state;
1013510b6c0eSNeil Perrin 
1014510b6c0eSNeil Perrin 		/*
1015510b6c0eSNeil Perrin 		 * Unlike zfs_log_write() we can be called with
1016510b6c0eSNeil Perrin 		 * upto DMU_MAX_ACCESS/2 (5MB) writes.
1017510b6c0eSNeil Perrin 		 */
1018510b6c0eSNeil Perrin 		if (blocksize > zvol_immediate_write_sz && !slogging &&
1019510b6c0eSNeil Perrin 		    resid >= blocksize && off % blocksize == 0) {
1020510b6c0eSNeil Perrin 			write_state = WR_INDIRECT; /* uses dmu_sync */
1021510b6c0eSNeil Perrin 			len = blocksize;
1022510b6c0eSNeil Perrin 		} else if (sync) {
1023510b6c0eSNeil Perrin 			write_state = WR_COPIED;
1024510b6c0eSNeil Perrin 			len = MIN(ZIL_MAX_LOG_DATA, resid);
1025510b6c0eSNeil Perrin 		} else {
1026510b6c0eSNeil Perrin 			write_state = WR_NEED_COPY;
1027510b6c0eSNeil Perrin 			len = MIN(ZIL_MAX_LOG_DATA, resid);
1028510b6c0eSNeil Perrin 		}
1029510b6c0eSNeil Perrin 
1030510b6c0eSNeil Perrin 		itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
1031510b6c0eSNeil Perrin 		    (write_state == WR_COPIED ? len : 0));
103222ac5be4Sperrin 		lr = (lr_write_t *)&itx->itx_lr;
1033510b6c0eSNeil Perrin 		if (write_state == WR_COPIED && dmu_read(zv->zv_objset,
10347bfdf011SNeil Perrin 		    ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
1035510b6c0eSNeil Perrin 			kmem_free(itx, offsetof(itx_t, itx_lr) +
1036510b6c0eSNeil Perrin 			    itx->itx_lr.lrc_reclen);
1037510b6c0eSNeil Perrin 			itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1038510b6c0eSNeil Perrin 			lr = (lr_write_t *)&itx->itx_lr;
1039510b6c0eSNeil Perrin 			write_state = WR_NEED_COPY;
1040510b6c0eSNeil Perrin 		}
1041510b6c0eSNeil Perrin 
1042510b6c0eSNeil Perrin 		itx->itx_wr_state = write_state;
1043510b6c0eSNeil Perrin 		if (write_state == WR_NEED_COPY)
1044510b6c0eSNeil Perrin 			itx->itx_sod += len;
104522ac5be4Sperrin 		lr->lr_foid = ZVOL_OBJ;
104622ac5be4Sperrin 		lr->lr_offset = off;
1047510b6c0eSNeil Perrin 		lr->lr_length = len;
1048feb08c6bSbillm 		lr->lr_blkoff = off - P2ALIGN_TYPED(off, blocksize, uint64_t);
104922ac5be4Sperrin 		BP_ZERO(&lr->lr_blkptr);
1050feb08c6bSbillm 
1051510b6c0eSNeil Perrin 		itx->itx_private = zv;
1052510b6c0eSNeil Perrin 		itx->itx_sync = sync;
1053510b6c0eSNeil Perrin 
10541209a471SNeil Perrin 		(void) zil_itx_assign(zilog, itx, tx);
1055510b6c0eSNeil Perrin 
1056510b6c0eSNeil Perrin 		off += len;
1057510b6c0eSNeil Perrin 		resid -= len;
1058a24e15ceSperrin 	}
105922ac5be4Sperrin }
106022ac5be4Sperrin 
106188b7b0f2SMatthew Ahrens static int
106288b7b0f2SMatthew Ahrens zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t size,
106388b7b0f2SMatthew Ahrens     boolean_t doread, boolean_t isdump)
1064e7cbe64fSgw25295 {
1065e7cbe64fSgw25295 	vdev_disk_t *dvd;
1066e7cbe64fSgw25295 	int c;
1067e7cbe64fSgw25295 	int numerrors = 0;
1068e7cbe64fSgw25295 
1069e7cbe64fSgw25295 	for (c = 0; c < vd->vdev_children; c++) {
107021ecdf64SLin Ling 		ASSERT(vd->vdev_ops == &vdev_mirror_ops ||
107121ecdf64SLin Ling 		    vd->vdev_ops == &vdev_replacing_ops ||
107221ecdf64SLin Ling 		    vd->vdev_ops == &vdev_spare_ops);
107388b7b0f2SMatthew Ahrens 		int err = zvol_dumpio_vdev(vd->vdev_child[c],
107488b7b0f2SMatthew Ahrens 		    addr, offset, size, doread, isdump);
107588b7b0f2SMatthew Ahrens 		if (err != 0) {
1076e7cbe64fSgw25295 			numerrors++;
107788b7b0f2SMatthew Ahrens 		} else if (doread) {
1078e7cbe64fSgw25295 			break;
1079e7cbe64fSgw25295 		}
1080e7cbe64fSgw25295 	}
1081e7cbe64fSgw25295 
1082e7cbe64fSgw25295 	if (!vd->vdev_ops->vdev_op_leaf)
1083e7cbe64fSgw25295 		return (numerrors < vd->vdev_children ? 0 : EIO);
1084e7cbe64fSgw25295 
1085dc0bb255SEric Taylor 	if (doread && !vdev_readable(vd))
1086dc0bb255SEric Taylor 		return (EIO);
1087dc0bb255SEric Taylor 	else if (!doread && !vdev_writeable(vd))
1088e7cbe64fSgw25295 		return (EIO);
1089e7cbe64fSgw25295 
1090e7cbe64fSgw25295 	dvd = vd->vdev_tsd;
1091e7cbe64fSgw25295 	ASSERT3P(dvd, !=, NULL);
1092e7cbe64fSgw25295 	offset += VDEV_LABEL_START_SIZE;
1093e7cbe64fSgw25295 
1094e7cbe64fSgw25295 	if (ddi_in_panic() || isdump) {
109588b7b0f2SMatthew Ahrens 		ASSERT(!doread);
109688b7b0f2SMatthew Ahrens 		if (doread)
1097e7cbe64fSgw25295 			return (EIO);
1098e7cbe64fSgw25295 		return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset),
1099e7cbe64fSgw25295 		    lbtodb(size)));
1100e7cbe64fSgw25295 	} else {
1101e7cbe64fSgw25295 		return (vdev_disk_physio(dvd->vd_lh, addr, size, offset,
110288b7b0f2SMatthew Ahrens 		    doread ? B_READ : B_WRITE));
1103e7cbe64fSgw25295 	}
1104e7cbe64fSgw25295 }
1105e7cbe64fSgw25295 
110688b7b0f2SMatthew Ahrens static int
110788b7b0f2SMatthew Ahrens zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size,
110888b7b0f2SMatthew Ahrens     boolean_t doread, boolean_t isdump)
1109e7cbe64fSgw25295 {
1110e7cbe64fSgw25295 	vdev_t *vd;
1111e7cbe64fSgw25295 	int error;
111288b7b0f2SMatthew Ahrens 	zvol_extent_t *ze;
1113e7cbe64fSgw25295 	spa_t *spa = dmu_objset_spa(zv->zv_objset);
1114e7cbe64fSgw25295 
111588b7b0f2SMatthew Ahrens 	/* Must be sector aligned, and not stradle a block boundary. */
111688b7b0f2SMatthew Ahrens 	if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) ||
111788b7b0f2SMatthew Ahrens 	    P2BOUNDARY(offset, size, zv->zv_volblocksize)) {
111888b7b0f2SMatthew Ahrens 		return (EINVAL);
111988b7b0f2SMatthew Ahrens 	}
1120e7cbe64fSgw25295 	ASSERT(size <= zv->zv_volblocksize);
1121e7cbe64fSgw25295 
112288b7b0f2SMatthew Ahrens 	/* Locate the extent this belongs to */
112388b7b0f2SMatthew Ahrens 	ze = list_head(&zv->zv_extents);
112488b7b0f2SMatthew Ahrens 	while (offset >= ze->ze_nblks * zv->zv_volblocksize) {
112588b7b0f2SMatthew Ahrens 		offset -= ze->ze_nblks * zv->zv_volblocksize;
112688b7b0f2SMatthew Ahrens 		ze = list_next(&zv->zv_extents, ze);
112788b7b0f2SMatthew Ahrens 	}
1128e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
112988b7b0f2SMatthew Ahrens 	vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva));
113088b7b0f2SMatthew Ahrens 	offset += DVA_GET_OFFSET(&ze->ze_dva);
113188b7b0f2SMatthew Ahrens 	error = zvol_dumpio_vdev(vd, addr, offset, size, doread, isdump);
1132e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
1133e7cbe64fSgw25295 	return (error);
1134e7cbe64fSgw25295 }
1135e7cbe64fSgw25295 
1136e7cbe64fSgw25295 int
1137fa9e4066Sahrens zvol_strategy(buf_t *bp)
1138fa9e4066Sahrens {
1139fa9e4066Sahrens 	zvol_state_t *zv = ddi_get_soft_state(zvol_state, getminor(bp->b_edev));
1140fa9e4066Sahrens 	uint64_t off, volsize;
114188b7b0f2SMatthew Ahrens 	size_t resid;
1142fa9e4066Sahrens 	char *addr;
114322ac5be4Sperrin 	objset_t *os;
1144c2e6a7d6Sperrin 	rl_t *rl;
1145fa9e4066Sahrens 	int error = 0;
114688b7b0f2SMatthew Ahrens 	boolean_t doread = bp->b_flags & B_READ;
114788b7b0f2SMatthew Ahrens 	boolean_t is_dump = zv->zv_flags & ZVOL_DUMPIFIED;
1148510b6c0eSNeil Perrin 	boolean_t sync;
1149fa9e4066Sahrens 
1150fa9e4066Sahrens 	if (zv == NULL) {
1151fa9e4066Sahrens 		bioerror(bp, ENXIO);
1152fa9e4066Sahrens 		biodone(bp);
1153fa9e4066Sahrens 		return (0);
1154fa9e4066Sahrens 	}
1155fa9e4066Sahrens 
1156fa9e4066Sahrens 	if (getminor(bp->b_edev) == 0) {
1157fa9e4066Sahrens 		bioerror(bp, EINVAL);
1158fa9e4066Sahrens 		biodone(bp);
1159fa9e4066Sahrens 		return (0);
1160fa9e4066Sahrens 	}
1161fa9e4066Sahrens 
1162e7cbe64fSgw25295 	if (!(bp->b_flags & B_READ) &&
1163e7cbe64fSgw25295 	    (zv->zv_flags & ZVOL_RDONLY ||
1164e7cbe64fSgw25295 	    zv->zv_mode & DS_MODE_READONLY)) {
1165fa9e4066Sahrens 		bioerror(bp, EROFS);
1166fa9e4066Sahrens 		biodone(bp);
1167fa9e4066Sahrens 		return (0);
1168fa9e4066Sahrens 	}
1169fa9e4066Sahrens 
1170fa9e4066Sahrens 	off = ldbtob(bp->b_blkno);
1171fa9e4066Sahrens 	volsize = zv->zv_volsize;
1172fa9e4066Sahrens 
117322ac5be4Sperrin 	os = zv->zv_objset;
117422ac5be4Sperrin 	ASSERT(os != NULL);
1175fa9e4066Sahrens 
1176fa9e4066Sahrens 	bp_mapin(bp);
1177fa9e4066Sahrens 	addr = bp->b_un.b_addr;
1178fa9e4066Sahrens 	resid = bp->b_bcount;
1179fa9e4066Sahrens 
118088b7b0f2SMatthew Ahrens 	if (resid > 0 && (off < 0 || off >= volsize)) {
118188b7b0f2SMatthew Ahrens 		bioerror(bp, EIO);
118288b7b0f2SMatthew Ahrens 		biodone(bp);
118388b7b0f2SMatthew Ahrens 		return (0);
118488b7b0f2SMatthew Ahrens 	}
118573ec3d9cSgw25295 
1186510b6c0eSNeil Perrin 	sync = !(bp->b_flags & B_ASYNC) && !doread && !is_dump &&
1187510b6c0eSNeil Perrin 	    !(zv->zv_flags & ZVOL_WCE) && !zil_disable;
1188510b6c0eSNeil Perrin 
1189a24e15ceSperrin 	/*
1190a24e15ceSperrin 	 * There must be no buffer changes when doing a dmu_sync() because
1191a24e15ceSperrin 	 * we can't change the data whilst calculating the checksum.
1192a24e15ceSperrin 	 */
1193c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, off, resid,
119488b7b0f2SMatthew Ahrens 	    doread ? RL_READER : RL_WRITER);
1195e7cbe64fSgw25295 
1196fa9e4066Sahrens 	while (resid != 0 && off < volsize) {
119788b7b0f2SMatthew Ahrens 		size_t size = MIN(resid, zvol_maxphys);
1198e7cbe64fSgw25295 		if (is_dump) {
1199e7cbe64fSgw25295 			size = MIN(size, P2END(off, zv->zv_volblocksize) - off);
120088b7b0f2SMatthew Ahrens 			error = zvol_dumpio(zv, addr, off, size,
120188b7b0f2SMatthew Ahrens 			    doread, B_FALSE);
120288b7b0f2SMatthew Ahrens 		} else if (doread) {
12037bfdf011SNeil Perrin 			error = dmu_read(os, ZVOL_OBJ, off, size, addr,
12047bfdf011SNeil Perrin 			    DMU_READ_PREFETCH);
1205fa9e4066Sahrens 		} else {
120622ac5be4Sperrin 			dmu_tx_t *tx = dmu_tx_create(os);
1207fa9e4066Sahrens 			dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
1208fa9e4066Sahrens 			error = dmu_tx_assign(tx, TXG_WAIT);
1209fa9e4066Sahrens 			if (error) {
1210fa9e4066Sahrens 				dmu_tx_abort(tx);
1211fa9e4066Sahrens 			} else {
121222ac5be4Sperrin 				dmu_write(os, ZVOL_OBJ, off, size, addr, tx);
1213510b6c0eSNeil Perrin 				zvol_log_write(zv, tx, off, size, sync);
1214fa9e4066Sahrens 				dmu_tx_commit(tx);
1215fa9e4066Sahrens 			}
1216fa9e4066Sahrens 		}
1217b87f3af3Sperrin 		if (error) {
1218b87f3af3Sperrin 			/* convert checksum errors into IO errors */
1219b87f3af3Sperrin 			if (error == ECKSUM)
1220b87f3af3Sperrin 				error = EIO;
1221fa9e4066Sahrens 			break;
1222b87f3af3Sperrin 		}
1223fa9e4066Sahrens 		off += size;
1224fa9e4066Sahrens 		addr += size;
1225fa9e4066Sahrens 		resid -= size;
1226fa9e4066Sahrens 	}
1227c2e6a7d6Sperrin 	zfs_range_unlock(rl);
1228fa9e4066Sahrens 
1229fa9e4066Sahrens 	if ((bp->b_resid = resid) == bp->b_bcount)
1230fa9e4066Sahrens 		bioerror(bp, off > volsize ? EINVAL : error);
1231fa9e4066Sahrens 
1232510b6c0eSNeil Perrin 	if (sync)
1233feb08c6bSbillm 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1234feb08c6bSbillm 	biodone(bp);
123522ac5be4Sperrin 
1236fa9e4066Sahrens 	return (0);
1237fa9e4066Sahrens }
1238fa9e4066Sahrens 
123967bd71c6Sperrin /*
124067bd71c6Sperrin  * Set the buffer count to the zvol maximum transfer.
124167bd71c6Sperrin  * Using our own routine instead of the default minphys()
124267bd71c6Sperrin  * means that for larger writes we write bigger buffers on X86
124367bd71c6Sperrin  * (128K instead of 56K) and flush the disk write cache less often
124467bd71c6Sperrin  * (every zvol_maxphys - currently 1MB) instead of minphys (currently
124567bd71c6Sperrin  * 56K on X86 and 128K on sparc).
124667bd71c6Sperrin  */
124767bd71c6Sperrin void
124867bd71c6Sperrin zvol_minphys(struct buf *bp)
124967bd71c6Sperrin {
125067bd71c6Sperrin 	if (bp->b_bcount > zvol_maxphys)
125167bd71c6Sperrin 		bp->b_bcount = zvol_maxphys;
125267bd71c6Sperrin }
125367bd71c6Sperrin 
1254e7cbe64fSgw25295 int
1255e7cbe64fSgw25295 zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks)
1256e7cbe64fSgw25295 {
1257e7cbe64fSgw25295 	minor_t minor = getminor(dev);
1258e7cbe64fSgw25295 	zvol_state_t *zv;
1259e7cbe64fSgw25295 	int error = 0;
1260e7cbe64fSgw25295 	uint64_t size;
1261e7cbe64fSgw25295 	uint64_t boff;
1262e7cbe64fSgw25295 	uint64_t resid;
1263e7cbe64fSgw25295 
1264e7cbe64fSgw25295 	if (minor == 0)			/* This is the control device */
1265e7cbe64fSgw25295 		return (ENXIO);
1266e7cbe64fSgw25295 
1267e7cbe64fSgw25295 	zv = ddi_get_soft_state(zvol_state, minor);
1268e7cbe64fSgw25295 	if (zv == NULL)
1269e7cbe64fSgw25295 		return (ENXIO);
1270e7cbe64fSgw25295 
1271e7cbe64fSgw25295 	boff = ldbtob(blkno);
1272e7cbe64fSgw25295 	resid = ldbtob(nblocks);
1273e7cbe64fSgw25295 
127488b7b0f2SMatthew Ahrens 	VERIFY3U(boff + resid, <=, zv->zv_volsize);
127588b7b0f2SMatthew Ahrens 
127688b7b0f2SMatthew Ahrens 	while (resid) {
127788b7b0f2SMatthew Ahrens 		size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff);
127888b7b0f2SMatthew Ahrens 		error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE);
1279e7cbe64fSgw25295 		if (error)
1280e7cbe64fSgw25295 			break;
1281e7cbe64fSgw25295 		boff += size;
1282e7cbe64fSgw25295 		addr += size;
1283e7cbe64fSgw25295 		resid -= size;
1284e7cbe64fSgw25295 	}
1285e7cbe64fSgw25295 
1286e7cbe64fSgw25295 	return (error);
1287e7cbe64fSgw25295 }
1288e7cbe64fSgw25295 
1289fa9e4066Sahrens /*ARGSUSED*/
1290fa9e4066Sahrens int
1291feb08c6bSbillm zvol_read(dev_t dev, uio_t *uio, cred_t *cr)
1292fa9e4066Sahrens {
1293c7ca1008Sgw25295 	minor_t minor = getminor(dev);
1294c7ca1008Sgw25295 	zvol_state_t *zv;
129573ec3d9cSgw25295 	uint64_t volsize;
1296c2e6a7d6Sperrin 	rl_t *rl;
1297feb08c6bSbillm 	int error = 0;
1298feb08c6bSbillm 
1299c7ca1008Sgw25295 	if (minor == 0)			/* This is the control device */
1300c7ca1008Sgw25295 		return (ENXIO);
1301c7ca1008Sgw25295 
1302c7ca1008Sgw25295 	zv = ddi_get_soft_state(zvol_state, minor);
1303c7ca1008Sgw25295 	if (zv == NULL)
1304c7ca1008Sgw25295 		return (ENXIO);
1305c7ca1008Sgw25295 
130673ec3d9cSgw25295 	volsize = zv->zv_volsize;
130773ec3d9cSgw25295 	if (uio->uio_resid > 0 &&
130873ec3d9cSgw25295 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
130973ec3d9cSgw25295 		return (EIO);
131073ec3d9cSgw25295 
131188b7b0f2SMatthew Ahrens 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
131288b7b0f2SMatthew Ahrens 		error = physio(zvol_strategy, NULL, dev, B_READ,
131388b7b0f2SMatthew Ahrens 		    zvol_minphys, uio);
131488b7b0f2SMatthew Ahrens 		return (error);
131588b7b0f2SMatthew Ahrens 	}
131688b7b0f2SMatthew Ahrens 
1317c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1318c2e6a7d6Sperrin 	    RL_READER);
131973ec3d9cSgw25295 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1320feb08c6bSbillm 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1321feb08c6bSbillm 
132273ec3d9cSgw25295 		/* don't read past the end */
132373ec3d9cSgw25295 		if (bytes > volsize - uio->uio_loffset)
132473ec3d9cSgw25295 			bytes = volsize - uio->uio_loffset;
132573ec3d9cSgw25295 
1326feb08c6bSbillm 		error =  dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes);
1327b87f3af3Sperrin 		if (error) {
1328b87f3af3Sperrin 			/* convert checksum errors into IO errors */
1329b87f3af3Sperrin 			if (error == ECKSUM)
1330b87f3af3Sperrin 				error = EIO;
1331feb08c6bSbillm 			break;
1332feb08c6bSbillm 		}
1333b87f3af3Sperrin 	}
1334c2e6a7d6Sperrin 	zfs_range_unlock(rl);
1335feb08c6bSbillm 	return (error);
1336fa9e4066Sahrens }
1337fa9e4066Sahrens 
1338fa9e4066Sahrens /*ARGSUSED*/
1339fa9e4066Sahrens int
1340feb08c6bSbillm zvol_write(dev_t dev, uio_t *uio, cred_t *cr)
1341fa9e4066Sahrens {
1342c7ca1008Sgw25295 	minor_t minor = getminor(dev);
1343c7ca1008Sgw25295 	zvol_state_t *zv;
134473ec3d9cSgw25295 	uint64_t volsize;
1345c2e6a7d6Sperrin 	rl_t *rl;
1346feb08c6bSbillm 	int error = 0;
1347510b6c0eSNeil Perrin 	boolean_t sync;
1348fa9e4066Sahrens 
1349c7ca1008Sgw25295 	if (minor == 0)			/* This is the control device */
1350c7ca1008Sgw25295 		return (ENXIO);
1351c7ca1008Sgw25295 
1352c7ca1008Sgw25295 	zv = ddi_get_soft_state(zvol_state, minor);
1353c7ca1008Sgw25295 	if (zv == NULL)
1354c7ca1008Sgw25295 		return (ENXIO);
1355c7ca1008Sgw25295 
135673ec3d9cSgw25295 	volsize = zv->zv_volsize;
135773ec3d9cSgw25295 	if (uio->uio_resid > 0 &&
135873ec3d9cSgw25295 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
135973ec3d9cSgw25295 		return (EIO);
136073ec3d9cSgw25295 
1361e7cbe64fSgw25295 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
1362e7cbe64fSgw25295 		error = physio(zvol_strategy, NULL, dev, B_WRITE,
1363e7cbe64fSgw25295 		    zvol_minphys, uio);
1364e7cbe64fSgw25295 		return (error);
1365e7cbe64fSgw25295 	}
1366e7cbe64fSgw25295 
1367510b6c0eSNeil Perrin 	sync = !(zv->zv_flags & ZVOL_WCE) && !zil_disable;
1368510b6c0eSNeil Perrin 
1369c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1370c2e6a7d6Sperrin 	    RL_WRITER);
137173ec3d9cSgw25295 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1372feb08c6bSbillm 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1373feb08c6bSbillm 		uint64_t off = uio->uio_loffset;
1374feb08c6bSbillm 		dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
137573ec3d9cSgw25295 
137673ec3d9cSgw25295 		if (bytes > volsize - off)	/* don't write past the end */
137773ec3d9cSgw25295 			bytes = volsize - off;
137873ec3d9cSgw25295 
1379feb08c6bSbillm 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
1380feb08c6bSbillm 		error = dmu_tx_assign(tx, TXG_WAIT);
1381feb08c6bSbillm 		if (error) {
1382feb08c6bSbillm 			dmu_tx_abort(tx);
1383feb08c6bSbillm 			break;
1384feb08c6bSbillm 		}
1385feb08c6bSbillm 		error = dmu_write_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes, tx);
1386feb08c6bSbillm 		if (error == 0)
1387510b6c0eSNeil Perrin 			zvol_log_write(zv, tx, off, bytes, sync);
1388feb08c6bSbillm 		dmu_tx_commit(tx);
1389feb08c6bSbillm 
1390feb08c6bSbillm 		if (error)
1391feb08c6bSbillm 			break;
1392feb08c6bSbillm 	}
1393c2e6a7d6Sperrin 	zfs_range_unlock(rl);
1394510b6c0eSNeil Perrin 	if (sync)
1395e08bf2c6SEric Taylor 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1396feb08c6bSbillm 	return (error);
1397fa9e4066Sahrens }
1398fa9e4066Sahrens 
1399c7f714e2SEric Taylor int
1400c7f714e2SEric Taylor zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs)
1401c7f714e2SEric Taylor {
1402c7f714e2SEric Taylor 	struct uuid uuid = EFI_RESERVED;
1403c7f714e2SEric Taylor 	efi_gpe_t gpe = { 0 };
1404c7f714e2SEric Taylor 	uint32_t crc;
1405c7f714e2SEric Taylor 	dk_efi_t efi;
1406c7f714e2SEric Taylor 	int length;
1407c7f714e2SEric Taylor 	char *ptr;
1408c7f714e2SEric Taylor 
1409c7f714e2SEric Taylor 	if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag))
1410c7f714e2SEric Taylor 		return (EFAULT);
1411c7f714e2SEric Taylor 	ptr = (char *)(uintptr_t)efi.dki_data_64;
1412c7f714e2SEric Taylor 	length = efi.dki_length;
1413c7f714e2SEric Taylor 	/*
1414c7f714e2SEric Taylor 	 * Some clients may attempt to request a PMBR for the
1415c7f714e2SEric Taylor 	 * zvol.  Currently this interface will return EINVAL to
1416c7f714e2SEric Taylor 	 * such requests.  These requests could be supported by
1417c7f714e2SEric Taylor 	 * adding a check for lba == 0 and consing up an appropriate
1418c7f714e2SEric Taylor 	 * PMBR.
1419c7f714e2SEric Taylor 	 */
1420c7f714e2SEric Taylor 	if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0)
1421c7f714e2SEric Taylor 		return (EINVAL);
1422c7f714e2SEric Taylor 
1423c7f714e2SEric Taylor 	gpe.efi_gpe_StartingLBA = LE_64(34ULL);
1424c7f714e2SEric Taylor 	gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1);
1425c7f714e2SEric Taylor 	UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
1426c7f714e2SEric Taylor 
1427c7f714e2SEric Taylor 	if (efi.dki_lba == 1) {
1428c7f714e2SEric Taylor 		efi_gpt_t gpt = { 0 };
1429c7f714e2SEric Taylor 
1430c7f714e2SEric Taylor 		gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
1431c7f714e2SEric Taylor 		gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
1432c7f714e2SEric Taylor 		gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt));
1433c7f714e2SEric Taylor 		gpt.efi_gpt_MyLBA = LE_64(1ULL);
1434c7f714e2SEric Taylor 		gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL);
1435c7f714e2SEric Taylor 		gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1);
1436c7f714e2SEric Taylor 		gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL);
1437c7f714e2SEric Taylor 		gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
1438c7f714e2SEric Taylor 		gpt.efi_gpt_SizeOfPartitionEntry =
1439c7f714e2SEric Taylor 		    LE_32(sizeof (efi_gpe_t));
1440c7f714e2SEric Taylor 		CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
1441c7f714e2SEric Taylor 		gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
1442c7f714e2SEric Taylor 		CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table);
1443c7f714e2SEric Taylor 		gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
1444c7f714e2SEric Taylor 		if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length),
1445c7f714e2SEric Taylor 		    flag))
1446c7f714e2SEric Taylor 			return (EFAULT);
1447c7f714e2SEric Taylor 		ptr += sizeof (gpt);
1448c7f714e2SEric Taylor 		length -= sizeof (gpt);
1449c7f714e2SEric Taylor 	}
1450c7f714e2SEric Taylor 	if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe),
1451c7f714e2SEric Taylor 	    length), flag))
1452c7f714e2SEric Taylor 		return (EFAULT);
1453c7f714e2SEric Taylor 	return (0);
1454c7f714e2SEric Taylor }
1455c7f714e2SEric Taylor 
1456fa9e4066Sahrens /*
1457fa9e4066Sahrens  * Dirtbag ioctls to support mkfs(1M) for UFS filesystems.  See dkio(7I).
1458fa9e4066Sahrens  */
1459fa9e4066Sahrens /*ARGSUSED*/
1460fa9e4066Sahrens int
1461fa9e4066Sahrens zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
1462fa9e4066Sahrens {
1463fa9e4066Sahrens 	zvol_state_t *zv;
1464af2c4821Smaybee 	struct dk_cinfo dki;
1465fa9e4066Sahrens 	struct dk_minfo dkm;
1466af2c4821Smaybee 	struct dk_callback *dkc;
1467fa9e4066Sahrens 	int error = 0;
1468e7cbe64fSgw25295 	rl_t *rl;
1469fa9e4066Sahrens 
1470fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
1471fa9e4066Sahrens 
1472fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, getminor(dev));
1473fa9e4066Sahrens 
1474fa9e4066Sahrens 	if (zv == NULL) {
1475fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
1476fa9e4066Sahrens 		return (ENXIO);
1477fa9e4066Sahrens 	}
1478701f66c4SEric Taylor 	ASSERT(zv->zv_total_opens > 0);
1479fa9e4066Sahrens 
1480fa9e4066Sahrens 	switch (cmd) {
1481fa9e4066Sahrens 
1482fa9e4066Sahrens 	case DKIOCINFO:
1483af2c4821Smaybee 		bzero(&dki, sizeof (dki));
1484af2c4821Smaybee 		(void) strcpy(dki.dki_cname, "zvol");
1485af2c4821Smaybee 		(void) strcpy(dki.dki_dname, "zvol");
1486af2c4821Smaybee 		dki.dki_ctype = DKC_UNKNOWN;
1487af2c4821Smaybee 		dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs);
1488fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
1489af2c4821Smaybee 		if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag))
1490fa9e4066Sahrens 			error = EFAULT;
1491fa9e4066Sahrens 		return (error);
1492fa9e4066Sahrens 
1493fa9e4066Sahrens 	case DKIOCGMEDIAINFO:
1494fa9e4066Sahrens 		bzero(&dkm, sizeof (dkm));
1495fa9e4066Sahrens 		dkm.dki_lbsize = 1U << zv->zv_min_bs;
1496fa9e4066Sahrens 		dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
1497fa9e4066Sahrens 		dkm.dki_media_type = DK_UNKNOWN;
1498fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
1499fa9e4066Sahrens 		if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
1500fa9e4066Sahrens 			error = EFAULT;
1501fa9e4066Sahrens 		return (error);
1502fa9e4066Sahrens 
1503fa9e4066Sahrens 	case DKIOCGETEFI:
1504c7f714e2SEric Taylor 		{
1505c7f714e2SEric Taylor 			uint64_t vs = zv->zv_volsize;
1506c7f714e2SEric Taylor 			uint8_t bs = zv->zv_min_bs;
1507fa9e4066Sahrens 
1508fa9e4066Sahrens 			mutex_exit(&zvol_state_lock);
1509c7f714e2SEric Taylor 			error = zvol_getefi((void *)arg, flag, vs, bs);
1510fa9e4066Sahrens 			return (error);
1511c7f714e2SEric Taylor 		}
1512fa9e4066Sahrens 
1513feb08c6bSbillm 	case DKIOCFLUSHWRITECACHE:
1514af2c4821Smaybee 		dkc = (struct dk_callback *)arg;
1515701f66c4SEric Taylor 		mutex_exit(&zvol_state_lock);
1516feb08c6bSbillm 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1517af2c4821Smaybee 		if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) {
1518af2c4821Smaybee 			(*dkc->dkc_callback)(dkc->dkc_cookie, error);
1519af2c4821Smaybee 			error = 0;
1520af2c4821Smaybee 		}
1521701f66c4SEric Taylor 		return (error);
1522701f66c4SEric Taylor 
1523701f66c4SEric Taylor 	case DKIOCGETWCE:
1524701f66c4SEric Taylor 		{
1525701f66c4SEric Taylor 			int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0;
1526701f66c4SEric Taylor 			if (ddi_copyout(&wce, (void *)arg, sizeof (int),
1527701f66c4SEric Taylor 			    flag))
1528701f66c4SEric Taylor 				error = EFAULT;
1529feb08c6bSbillm 			break;
1530701f66c4SEric Taylor 		}
1531701f66c4SEric Taylor 	case DKIOCSETWCE:
1532701f66c4SEric Taylor 		{
1533701f66c4SEric Taylor 			int wce;
1534701f66c4SEric Taylor 			if (ddi_copyin((void *)arg, &wce, sizeof (int),
1535701f66c4SEric Taylor 			    flag)) {
1536701f66c4SEric Taylor 				error = EFAULT;
1537701f66c4SEric Taylor 				break;
1538701f66c4SEric Taylor 			}
1539701f66c4SEric Taylor 			if (wce) {
1540701f66c4SEric Taylor 				zv->zv_flags |= ZVOL_WCE;
1541701f66c4SEric Taylor 				mutex_exit(&zvol_state_lock);
1542701f66c4SEric Taylor 			} else {
1543701f66c4SEric Taylor 				zv->zv_flags &= ~ZVOL_WCE;
1544701f66c4SEric Taylor 				mutex_exit(&zvol_state_lock);
1545701f66c4SEric Taylor 				zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1546701f66c4SEric Taylor 			}
1547701f66c4SEric Taylor 			return (0);
1548701f66c4SEric Taylor 		}
1549feb08c6bSbillm 
1550b6130eadSmaybee 	case DKIOCGGEOM:
1551b6130eadSmaybee 	case DKIOCGVTOC:
1552e7cbe64fSgw25295 		/*
1553e7cbe64fSgw25295 		 * commands using these (like prtvtoc) expect ENOTSUP
1554e7cbe64fSgw25295 		 * since we're emulating an EFI label
1555e7cbe64fSgw25295 		 */
1556b6130eadSmaybee 		error = ENOTSUP;
1557b6130eadSmaybee 		break;
1558b6130eadSmaybee 
1559e7cbe64fSgw25295 	case DKIOCDUMPINIT:
1560e7cbe64fSgw25295 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1561e7cbe64fSgw25295 		    RL_WRITER);
1562e7cbe64fSgw25295 		error = zvol_dumpify(zv);
1563e7cbe64fSgw25295 		zfs_range_unlock(rl);
1564e7cbe64fSgw25295 		break;
1565e7cbe64fSgw25295 
1566e7cbe64fSgw25295 	case DKIOCDUMPFINI:
156706d5ae10SEric Taylor 		if (!(zv->zv_flags & ZVOL_DUMPIFIED))
156806d5ae10SEric Taylor 			break;
1569e7cbe64fSgw25295 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1570e7cbe64fSgw25295 		    RL_WRITER);
1571e7cbe64fSgw25295 		error = zvol_dump_fini(zv);
1572e7cbe64fSgw25295 		zfs_range_unlock(rl);
1573e7cbe64fSgw25295 		break;
1574e7cbe64fSgw25295 
1575fa9e4066Sahrens 	default:
157668a5ac4dSmaybee 		error = ENOTTY;
1577fa9e4066Sahrens 		break;
1578fa9e4066Sahrens 
1579fa9e4066Sahrens 	}
1580fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
1581fa9e4066Sahrens 	return (error);
1582fa9e4066Sahrens }
1583fa9e4066Sahrens 
1584fa9e4066Sahrens int
1585fa9e4066Sahrens zvol_busy(void)
1586fa9e4066Sahrens {
1587fa9e4066Sahrens 	return (zvol_minors != 0);
1588fa9e4066Sahrens }
1589fa9e4066Sahrens 
1590fa9e4066Sahrens void
1591fa9e4066Sahrens zvol_init(void)
1592fa9e4066Sahrens {
1593fa9e4066Sahrens 	VERIFY(ddi_soft_state_init(&zvol_state, sizeof (zvol_state_t), 1) == 0);
1594fa9e4066Sahrens 	mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
1595fa9e4066Sahrens }
1596fa9e4066Sahrens 
1597fa9e4066Sahrens void
1598fa9e4066Sahrens zvol_fini(void)
1599fa9e4066Sahrens {
1600fa9e4066Sahrens 	mutex_destroy(&zvol_state_lock);
1601fa9e4066Sahrens 	ddi_soft_state_fini(&zvol_state);
1602fa9e4066Sahrens }
1603e7cbe64fSgw25295 
1604e7cbe64fSgw25295 static boolean_t
1605e7cbe64fSgw25295 zvol_is_swap(zvol_state_t *zv)
1606e7cbe64fSgw25295 {
1607e7cbe64fSgw25295 	vnode_t *vp;
1608e7cbe64fSgw25295 	boolean_t ret = B_FALSE;
1609e7cbe64fSgw25295 	char *devpath;
1610e7cbe64fSgw25295 	int error;
1611e7cbe64fSgw25295 
1612*ae46e4c7SMatthew Ahrens 	devpath = kmem_asprintf("%s%s", ZVOL_FULL_DEV_DIR, zv->zv_name);
1613e7cbe64fSgw25295 	error = lookupname(devpath, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp);
1614*ae46e4c7SMatthew Ahrens 	strfree(devpath);
1615e7cbe64fSgw25295 
1616e7cbe64fSgw25295 	ret = !error && IS_SWAPVP(common_specvp(vp));
1617e7cbe64fSgw25295 
1618e7cbe64fSgw25295 	if (vp != NULL)
1619e7cbe64fSgw25295 		VN_RELE(vp);
1620e7cbe64fSgw25295 
1621e7cbe64fSgw25295 	return (ret);
1622e7cbe64fSgw25295 }
1623e7cbe64fSgw25295 
1624e7cbe64fSgw25295 static int
1625e7cbe64fSgw25295 zvol_dump_init(zvol_state_t *zv, boolean_t resize)
1626e7cbe64fSgw25295 {
1627e7cbe64fSgw25295 	dmu_tx_t *tx;
1628e7cbe64fSgw25295 	int error = 0;
1629e7cbe64fSgw25295 	objset_t *os = zv->zv_objset;
1630e7cbe64fSgw25295 	nvlist_t *nv = NULL;
1631e7cbe64fSgw25295 
1632e7cbe64fSgw25295 	ASSERT(MUTEX_HELD(&zvol_state_lock));
1633e7cbe64fSgw25295 
1634e7cbe64fSgw25295 	tx = dmu_tx_create(os);
1635e7cbe64fSgw25295 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1636e7cbe64fSgw25295 	error = dmu_tx_assign(tx, TXG_WAIT);
1637e7cbe64fSgw25295 	if (error) {
1638e7cbe64fSgw25295 		dmu_tx_abort(tx);
1639e7cbe64fSgw25295 		return (error);
1640e7cbe64fSgw25295 	}
1641e7cbe64fSgw25295 
1642e7cbe64fSgw25295 	/*
1643e7cbe64fSgw25295 	 * If we are resizing the dump device then we only need to
1644e7cbe64fSgw25295 	 * update the refreservation to match the newly updated
1645e7cbe64fSgw25295 	 * zvolsize. Otherwise, we save off the original state of the
1646e7cbe64fSgw25295 	 * zvol so that we can restore them if the zvol is ever undumpified.
1647e7cbe64fSgw25295 	 */
1648e7cbe64fSgw25295 	if (resize) {
1649e7cbe64fSgw25295 		error = zap_update(os, ZVOL_ZAP_OBJ,
1650e7cbe64fSgw25295 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1651e7cbe64fSgw25295 		    &zv->zv_volsize, tx);
1652e7cbe64fSgw25295 	} else {
165388b7b0f2SMatthew Ahrens 		uint64_t checksum, compress, refresrv, vbs;
165488b7b0f2SMatthew Ahrens 
1655e7cbe64fSgw25295 		error = dsl_prop_get_integer(zv->zv_name,
1656e7cbe64fSgw25295 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL);
1657e7cbe64fSgw25295 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
1658e7cbe64fSgw25295 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL);
1659e7cbe64fSgw25295 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
1660e7cbe64fSgw25295 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL);
166188b7b0f2SMatthew Ahrens 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
166288b7b0f2SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL);
1663e7cbe64fSgw25295 
1664e7cbe64fSgw25295 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1665e7cbe64fSgw25295 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1,
1666e7cbe64fSgw25295 		    &compress, tx);
1667e7cbe64fSgw25295 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1668e7cbe64fSgw25295 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx);
1669e7cbe64fSgw25295 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1670e7cbe64fSgw25295 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1671e7cbe64fSgw25295 		    &refresrv, tx);
167288b7b0f2SMatthew Ahrens 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
167388b7b0f2SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1,
167488b7b0f2SMatthew Ahrens 		    &vbs, tx);
1675e7cbe64fSgw25295 	}
1676e7cbe64fSgw25295 	dmu_tx_commit(tx);
1677e7cbe64fSgw25295 
1678e7cbe64fSgw25295 	/* Truncate the file */
1679e7cbe64fSgw25295 	if (!error)
1680cdb0ab79Smaybee 		error = dmu_free_long_range(zv->zv_objset,
1681cdb0ab79Smaybee 		    ZVOL_OBJ, 0, DMU_OBJECT_END);
1682e7cbe64fSgw25295 
1683e7cbe64fSgw25295 	if (error)
1684e7cbe64fSgw25295 		return (error);
1685e7cbe64fSgw25295 
1686e7cbe64fSgw25295 	/*
1687e7cbe64fSgw25295 	 * We only need update the zvol's property if we are initializing
1688e7cbe64fSgw25295 	 * the dump area for the first time.
1689e7cbe64fSgw25295 	 */
1690e7cbe64fSgw25295 	if (!resize) {
1691e7cbe64fSgw25295 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1692e7cbe64fSgw25295 		VERIFY(nvlist_add_uint64(nv,
1693e7cbe64fSgw25295 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0);
1694e7cbe64fSgw25295 		VERIFY(nvlist_add_uint64(nv,
1695e7cbe64fSgw25295 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
1696e7cbe64fSgw25295 		    ZIO_COMPRESS_OFF) == 0);
1697e7cbe64fSgw25295 		VERIFY(nvlist_add_uint64(nv,
1698e7cbe64fSgw25295 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
1699e7cbe64fSgw25295 		    ZIO_CHECKSUM_OFF) == 0);
170088b7b0f2SMatthew Ahrens 		VERIFY(nvlist_add_uint64(nv,
170188b7b0f2SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
170288b7b0f2SMatthew Ahrens 		    SPA_MAXBLOCKSIZE) == 0);
1703e7cbe64fSgw25295 
1704e7cbe64fSgw25295 		error = zfs_set_prop_nvlist(zv->zv_name, nv);
1705e7cbe64fSgw25295 		nvlist_free(nv);
1706e7cbe64fSgw25295 
1707e7cbe64fSgw25295 		if (error)
1708e7cbe64fSgw25295 			return (error);
1709e7cbe64fSgw25295 	}
1710e7cbe64fSgw25295 
1711e7cbe64fSgw25295 	/* Allocate the space for the dump */
1712e7cbe64fSgw25295 	error = zvol_prealloc(zv);
1713e7cbe64fSgw25295 	return (error);
1714e7cbe64fSgw25295 }
1715e7cbe64fSgw25295 
1716e7cbe64fSgw25295 static int
1717e7cbe64fSgw25295 zvol_dumpify(zvol_state_t *zv)
1718e7cbe64fSgw25295 {
1719e7cbe64fSgw25295 	int error = 0;
1720e7cbe64fSgw25295 	uint64_t dumpsize = 0;
1721e7cbe64fSgw25295 	dmu_tx_t *tx;
1722e7cbe64fSgw25295 	objset_t *os = zv->zv_objset;
1723e7cbe64fSgw25295 
1724e7cbe64fSgw25295 	if (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY))
1725e7cbe64fSgw25295 		return (EROFS);
1726e7cbe64fSgw25295 
1727e7cbe64fSgw25295 	/*
1728e7cbe64fSgw25295 	 * We do not support swap devices acting as dump devices.
1729e7cbe64fSgw25295 	 */
1730e7cbe64fSgw25295 	if (zvol_is_swap(zv))
1731e7cbe64fSgw25295 		return (ENOTSUP);
1732e7cbe64fSgw25295 
1733e7cbe64fSgw25295 	if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE,
1734e7cbe64fSgw25295 	    8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) {
1735e7cbe64fSgw25295 		boolean_t resize = (dumpsize > 0) ? B_TRUE : B_FALSE;
1736e7cbe64fSgw25295 
1737e7cbe64fSgw25295 		if ((error = zvol_dump_init(zv, resize)) != 0) {
1738e7cbe64fSgw25295 			(void) zvol_dump_fini(zv);
1739e7cbe64fSgw25295 			return (error);
1740e7cbe64fSgw25295 		}
1741e7cbe64fSgw25295 	}
1742e7cbe64fSgw25295 
1743e7cbe64fSgw25295 	/*
1744e7cbe64fSgw25295 	 * Build up our lba mapping.
1745e7cbe64fSgw25295 	 */
1746e7cbe64fSgw25295 	error = zvol_get_lbas(zv);
1747e7cbe64fSgw25295 	if (error) {
1748e7cbe64fSgw25295 		(void) zvol_dump_fini(zv);
1749e7cbe64fSgw25295 		return (error);
1750e7cbe64fSgw25295 	}
1751e7cbe64fSgw25295 
1752e7cbe64fSgw25295 	tx = dmu_tx_create(os);
1753e7cbe64fSgw25295 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1754e7cbe64fSgw25295 	error = dmu_tx_assign(tx, TXG_WAIT);
1755e7cbe64fSgw25295 	if (error) {
1756e7cbe64fSgw25295 		dmu_tx_abort(tx);
1757e7cbe64fSgw25295 		(void) zvol_dump_fini(zv);
1758e7cbe64fSgw25295 		return (error);
1759e7cbe64fSgw25295 	}
1760e7cbe64fSgw25295 
1761e7cbe64fSgw25295 	zv->zv_flags |= ZVOL_DUMPIFIED;
1762e7cbe64fSgw25295 	error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1,
1763e7cbe64fSgw25295 	    &zv->zv_volsize, tx);
1764e7cbe64fSgw25295 	dmu_tx_commit(tx);
1765e7cbe64fSgw25295 
1766e7cbe64fSgw25295 	if (error) {
1767e7cbe64fSgw25295 		(void) zvol_dump_fini(zv);
1768e7cbe64fSgw25295 		return (error);
1769e7cbe64fSgw25295 	}
1770e7cbe64fSgw25295 
1771e7cbe64fSgw25295 	txg_wait_synced(dmu_objset_pool(os), 0);
1772e7cbe64fSgw25295 	return (0);
1773e7cbe64fSgw25295 }
1774e7cbe64fSgw25295 
1775e7cbe64fSgw25295 static int
1776e7cbe64fSgw25295 zvol_dump_fini(zvol_state_t *zv)
1777e7cbe64fSgw25295 {
1778e7cbe64fSgw25295 	dmu_tx_t *tx;
1779e7cbe64fSgw25295 	objset_t *os = zv->zv_objset;
1780e7cbe64fSgw25295 	nvlist_t *nv;
1781e7cbe64fSgw25295 	int error = 0;
178288b7b0f2SMatthew Ahrens 	uint64_t checksum, compress, refresrv, vbs;
1783e7cbe64fSgw25295 
1784b7e50089Smaybee 	/*
1785b7e50089Smaybee 	 * Attempt to restore the zvol back to its pre-dumpified state.
1786b7e50089Smaybee 	 * This is a best-effort attempt as it's possible that not all
1787b7e50089Smaybee 	 * of these properties were initialized during the dumpify process
1788b7e50089Smaybee 	 * (i.e. error during zvol_dump_init).
1789b7e50089Smaybee 	 */
1790b7e50089Smaybee 
1791e7cbe64fSgw25295 	tx = dmu_tx_create(os);
1792e7cbe64fSgw25295 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1793e7cbe64fSgw25295 	error = dmu_tx_assign(tx, TXG_WAIT);
1794e7cbe64fSgw25295 	if (error) {
1795e7cbe64fSgw25295 		dmu_tx_abort(tx);
1796e7cbe64fSgw25295 		return (error);
1797e7cbe64fSgw25295 	}
1798b7e50089Smaybee 	(void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx);
1799b7e50089Smaybee 	dmu_tx_commit(tx);
1800e7cbe64fSgw25295 
1801e7cbe64fSgw25295 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1802e7cbe64fSgw25295 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum);
1803e7cbe64fSgw25295 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1804e7cbe64fSgw25295 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress);
1805e7cbe64fSgw25295 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1806e7cbe64fSgw25295 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv);
180788b7b0f2SMatthew Ahrens 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
180888b7b0f2SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs);
1809e7cbe64fSgw25295 
1810e7cbe64fSgw25295 	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1811e7cbe64fSgw25295 	(void) nvlist_add_uint64(nv,
1812e7cbe64fSgw25295 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum);
1813e7cbe64fSgw25295 	(void) nvlist_add_uint64(nv,
1814e7cbe64fSgw25295 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress);
1815e7cbe64fSgw25295 	(void) nvlist_add_uint64(nv,
1816e7cbe64fSgw25295 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv);
181788b7b0f2SMatthew Ahrens 	(void) nvlist_add_uint64(nv,
181888b7b0f2SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), vbs);
1819e7cbe64fSgw25295 	(void) zfs_set_prop_nvlist(zv->zv_name, nv);
1820e7cbe64fSgw25295 	nvlist_free(nv);
1821e7cbe64fSgw25295 
1822b7e50089Smaybee 	zvol_free_extents(zv);
1823b7e50089Smaybee 	zv->zv_flags &= ~ZVOL_DUMPIFIED;
1824b7e50089Smaybee 	(void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END);
1825b7e50089Smaybee 
1826e7cbe64fSgw25295 	return (0);
1827e7cbe64fSgw25295 }
1828