xref: /illumos-gate/usr/src/uts/common/fs/zfs/zvol.c (revision 573ca77e)
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>
54e7cbe64fSgw #include <sys/dmu_traverse.h>
55e7cbe64fSgw #include <sys/dnode.h>
56e7cbe64fSgw #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>
74e7cbe64fSgw #include <sys/vdev_disk.h>
75e7cbe64fSgw #include <sys/vdev_impl.h>
76e7cbe64fSgw #include <sys/zvol.h>
77e7cbe64fSgw #include <sys/dumphdr.h>
781209a471SNeil Perrin #include <sys/zil_impl.h>
79fa9e4066Sahrens 
80fa9e4066Sahrens #include "zfs_namecheck.h"
81fa9e4066Sahrens 
82fa9e4066Sahrens static void *zvol_state;
83fa9e4066Sahrens 
84e7cbe64fSgw #define	ZVOL_DUMPSIZE		"dumpsize"
85e7cbe64fSgw 
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 
95e7cbe64fSgw typedef struct zvol_extent {
9688b7b0f2SMatthew Ahrens 	list_node_t	ze_node;
97e7cbe64fSgw 	dva_t		ze_dva;		/* dva associated with this extent */
9888b7b0f2SMatthew Ahrens 	uint64_t	ze_nblks;	/* number of blocks in extent */
99e7cbe64fSgw } zvol_extent_t;
100e7cbe64fSgw 
101fa9e4066Sahrens /*
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 
120e7cbe64fSgw /*
121e7cbe64fSgw  * zvol specific flags
122e7cbe64fSgw  */
123e7cbe64fSgw #define	ZVOL_RDONLY	0x1
124e7cbe64fSgw #define	ZVOL_DUMPIFIED	0x2
125c7f714e2SEric Taylor #define	ZVOL_EXCL	0x4
126701f66c4SEric Taylor #define	ZVOL_WCE	0x8
127e7cbe64fSgw 
12867bd71c6Sperrin /*
12967bd71c6Sperrin  * zvol maximum transfer in one DMU tx.
13067bd71c6Sperrin  */
13167bd71c6Sperrin int zvol_maxphys = DMU_MAX_ACCESS/2;
13267bd71c6Sperrin 
133e7cbe64fSgw 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);
135e7cbe64fSgw static int zvol_dumpify(zvol_state_t *zv);
136e7cbe64fSgw static int zvol_dump_fini(zvol_state_t *zv);
137e7cbe64fSgw 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);
148e7cbe64fSgw 
149e7cbe64fSgw 	/* Notify specfs to invalidate the cached size */
150e7cbe64fSgw 	spec_size_invalidate(dev, VBLK);
151e7cbe64fSgw 	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 
186e7cbe64fSgw 	if (newval)
187e7cbe64fSgw 		zv->zv_flags |= ZVOL_RDONLY;
188e7cbe64fSgw 	else
189e7cbe64fSgw 		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 
252e7cbe64fSgw /* extent mapping arg */
253e7cbe64fSgw struct maparg {
25488b7b0f2SMatthew Ahrens 	zvol_state_t	*ma_zv;
25588b7b0f2SMatthew Ahrens 	uint64_t	ma_blks;
256e7cbe64fSgw };
257e7cbe64fSgw 
258e7cbe64fSgw /*ARGSUSED*/
259e7cbe64fSgw 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)
262e7cbe64fSgw {
26388b7b0f2SMatthew Ahrens 	struct maparg *ma = arg;
26488b7b0f2SMatthew Ahrens 	zvol_extent_t *ze;
26588b7b0f2SMatthew Ahrens 	int bs = ma->ma_zv->zv_volblocksize;
266e7cbe64fSgw 
26788b7b0f2SMatthew Ahrens 	if (bp == NULL || zb->zb_object != ZVOL_OBJ || zb->zb_level != 0)
26888b7b0f2SMatthew Ahrens 		return (0);
269e7cbe64fSgw 
27088b7b0f2SMatthew Ahrens 	VERIFY3U(ma->ma_blks, ==, zb->zb_blkid);
27188b7b0f2SMatthew Ahrens 	ma->ma_blks++;
272e7cbe64fSgw 
27388b7b0f2SMatthew Ahrens 	/* Abort immediately if we have encountered gang blocks */
27488b7b0f2SMatthew Ahrens 	if (BP_IS_GANG(bp))
27588b7b0f2SMatthew Ahrens 		return (EFRAGS);
276e7cbe64fSgw 
27788b7b0f2SMatthew Ahrens 	/*
27888b7b0f2SMatthew Ahrens 	 * See if the block is at the end of the previous extent.
27988b7b0f2SMatthew Ahrens 	 */
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);
287e7cbe64fSgw 	}
288e7cbe64fSgw 
28988b7b0f2SMatthew Ahrens 	dprintf_bp(bp, "%s", "next blkptr:");
290e7cbe64fSgw 
29188b7b0f2SMatthew Ahrens 	/* 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);
29788b7b0f2SMatthew Ahrens }
298e7cbe64fSgw 
29988b7b0f2SMatthew Ahrens static void
30088b7b0f2SMatthew Ahrens zvol_free_extents(zvol_state_t *zv)
30188b7b0f2SMatthew Ahrens {
30288b7b0f2SMatthew Ahrens 	zvol_extent_t *ze;
303e7cbe64fSgw 
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));
307e7cbe64fSgw 	}
30888b7b0f2SMatthew Ahrens }
309e7cbe64fSgw 
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);
325e7cbe64fSgw 	}
32688b7b0f2SMatthew Ahrens 
327e7cbe64fSgw 	return (0);
328e7cbe64fSgw }
329e7cbe64fSgw 
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 	/*
346e7cbe64fSgw 	 * 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 
36622ac5be4Sperrin /*
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 
423e7cbe64fSgw /*
424e7cbe64fSgw  * 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;
438e7cbe64fSgw 	size_t devpathlen = strlen(ZVOL_FULL_DEV_DIR) + strlen(name) + 1;
439fa9e4066Sahrens 	char chrbuf[30], blkbuf[30];
440fa9e4066Sahrens 	int error;
441fa9e4066Sahrens 
442fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
443fa9e4066Sahrens 
444fa9e4066Sahrens 	if ((zv = zvol_minor_lookup(name)) != NULL) {
445fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
446fa9e4066Sahrens 		return (EEXIST);
447fa9e4066Sahrens 	}
448fa9e4066Sahrens 
449fa9e4066Sahrens 	if (strchr(name, '@') != 0)
450fa9e4066Sahrens 		ds_mode |= DS_MODE_READONLY;
451fa9e4066Sahrens 
452fa9e4066Sahrens 	error = dmu_objset_open(name, DMU_OST_ZVOL, ds_mode, &os);
453fa9e4066Sahrens 
454fa9e4066Sahrens 	if (error) {
455fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
456fa9e4066Sahrens 		return (error);
457fa9e4066Sahrens 	}
458fa9e4066Sahrens 
459fa9e4066Sahrens 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
460fa9e4066Sahrens 
461fa9e4066Sahrens 	if (error) {
462fa9e4066Sahrens 		dmu_objset_close(os);
463fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
464fa9e4066Sahrens 		return (error);
465fa9e4066Sahrens 	}
466fa9e4066Sahrens 
467fa9e4066Sahrens 	/*
468fa9e4066Sahrens 	 * If there's an existing /dev/zvol symlink, try to use the
469fa9e4066Sahrens 	 * same minor number we used last time.
470fa9e4066Sahrens 	 */
471fa9e4066Sahrens 	devpath = kmem_alloc(devpathlen, KM_SLEEP);
472fa9e4066Sahrens 
473e7cbe64fSgw 	(void) sprintf(devpath, "%s%s", ZVOL_FULL_DEV_DIR, name);
474fa9e4066Sahrens 
475fa9e4066Sahrens 	error = lookupname(devpath, UIO_SYSSPACE, NO_FOLLOW, NULL, &vp);
476fa9e4066Sahrens 
477fa9e4066Sahrens 	kmem_free(devpath, devpathlen);
478fa9e4066Sahrens 
479fa9e4066Sahrens 	if (error == 0 && vp->v_type != VLNK)
480fa9e4066Sahrens 		error = EINVAL;
481fa9e4066Sahrens 
482fa9e4066Sahrens 	if (error == 0) {
483fa9e4066Sahrens 		pn_alloc(&linkpath);
484fa9e4066Sahrens 		error = pn_getsymlink(vp, &linkpath, kcred);
485fa9e4066Sahrens 		if (error == 0) {
486fa9e4066Sahrens 			char *ms = strstr(linkpath.pn_path, ZVOL_PSEUDO_DEV);
487fa9e4066Sahrens 			if (ms != NULL) {
488fa9e4066Sahrens 				ms += strlen(ZVOL_PSEUDO_DEV);
489fa9e4066Sahrens 				minor = stoi(&ms);
490fa9e4066Sahrens 			}
491fa9e4066Sahrens 		}
492fa9e4066Sahrens 		pn_free(&linkpath);
493fa9e4066Sahrens 	}
494fa9e4066Sahrens 
495fa9e4066Sahrens 	if (vp != NULL)
496fa9e4066Sahrens 		VN_RELE(vp);
497fa9e4066Sahrens 
498fa9e4066Sahrens 	/*
499fa9e4066Sahrens 	 * If we found a minor but it's already in use, we must pick a new one.
500fa9e4066Sahrens 	 */
501fa9e4066Sahrens 	if (minor != 0 && ddi_get_soft_state(zvol_state, minor) != NULL)
502fa9e4066Sahrens 		minor = 0;
503fa9e4066Sahrens 
504fa9e4066Sahrens 	if (minor == 0)
505fa9e4066Sahrens 		minor = zvol_minor_alloc();
506fa9e4066Sahrens 
507fa9e4066Sahrens 	if (minor == 0) {
508fa9e4066Sahrens 		dmu_objset_close(os);
509fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
510fa9e4066Sahrens 		return (ENXIO);
511fa9e4066Sahrens 	}
512fa9e4066Sahrens 
513fa9e4066Sahrens 	if (ddi_soft_state_zalloc(zvol_state, minor) != DDI_SUCCESS) {
514fa9e4066Sahrens 		dmu_objset_close(os);
515fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
516fa9e4066Sahrens 		return (EAGAIN);
517fa9e4066Sahrens 	}
518fa9e4066Sahrens 
519e9dbad6fSeschrock 	(void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME,
520e9dbad6fSeschrock 	    (char *)name);
521fa9e4066Sahrens 
522fa9e4066Sahrens 	(void) sprintf(chrbuf, "%uc,raw", minor);
523fa9e4066Sahrens 
524fa9e4066Sahrens 	if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR,
525fa9e4066Sahrens 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
526fa9e4066Sahrens 		ddi_soft_state_free(zvol_state, minor);
527fa9e4066Sahrens 		dmu_objset_close(os);
528fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
529fa9e4066Sahrens 		return (EAGAIN);
530fa9e4066Sahrens 	}
531fa9e4066Sahrens 
532fa9e4066Sahrens 	(void) sprintf(blkbuf, "%uc", minor);
533fa9e4066Sahrens 
534fa9e4066Sahrens 	if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK,
535fa9e4066Sahrens 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
536fa9e4066Sahrens 		ddi_remove_minor_node(zfs_dip, chrbuf);
537fa9e4066Sahrens 		ddi_soft_state_free(zvol_state, minor);
538fa9e4066Sahrens 		dmu_objset_close(os);
539fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
540fa9e4066Sahrens 		return (EAGAIN);
541fa9e4066Sahrens 	}
542fa9e4066Sahrens 
543fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
544fa9e4066Sahrens 
545fa9e4066Sahrens 	(void) strcpy(zv->zv_name, name);
546fa9e4066Sahrens 	zv->zv_min_bs = DEV_BSHIFT;
547fa9e4066Sahrens 	zv->zv_minor = minor;
548fa9e4066Sahrens 	zv->zv_volsize = volsize;
549fa9e4066Sahrens 	zv->zv_objset = os;
550fa9e4066Sahrens 	zv->zv_mode = ds_mode;
55167bd71c6Sperrin 	zv->zv_zilog = zil_open(os, zvol_get_data);
552c2e6a7d6Sperrin 	mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
553c2e6a7d6Sperrin 	avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
554c2e6a7d6Sperrin 	    sizeof (rl_t), offsetof(rl_t, r_node));
55588b7b0f2SMatthew Ahrens 	list_create(&zv->zv_extents, sizeof (zvol_extent_t),
55688b7b0f2SMatthew Ahrens 	    offsetof(zvol_extent_t, ze_node));
55767bd71c6Sperrin 	/* get and cache the blocksize */
55867bd71c6Sperrin 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
55967bd71c6Sperrin 	ASSERT(error == 0);
56067bd71c6Sperrin 	zv->zv_volblocksize = doi.doi_data_block_size;
56122ac5be4Sperrin 
5621209a471SNeil Perrin 	zil_replay(os, zv, zvol_replay_vector);
56391ebeef5Sahrens 	zvol_size_changed(zv, maj);
564fa9e4066Sahrens 
565ea8dc4b6Seschrock 	/* XXX this should handle the possible i/o error */
566fa9e4066Sahrens 	VERIFY(dsl_prop_register(dmu_objset_ds(zv->zv_objset),
567fa9e4066Sahrens 	    "readonly", zvol_readonly_changed_cb, zv) == 0);
568fa9e4066Sahrens 
569fa9e4066Sahrens 	zvol_minors++;
570fa9e4066Sahrens 
571fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
572fa9e4066Sahrens 
573fa9e4066Sahrens 	return (0);
574fa9e4066Sahrens }
575fa9e4066Sahrens 
576fa9e4066Sahrens /*
577fa9e4066Sahrens  * Remove minor node for the specified volume.
578fa9e4066Sahrens  */
579fa9e4066Sahrens int
580e9dbad6fSeschrock zvol_remove_minor(const char *name)
581fa9e4066Sahrens {
582fa9e4066Sahrens 	zvol_state_t *zv;
583fa9e4066Sahrens 	char namebuf[30];
584fa9e4066Sahrens 
585fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
586fa9e4066Sahrens 
587e9dbad6fSeschrock 	if ((zv = zvol_minor_lookup(name)) == NULL) {
588fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
589fa9e4066Sahrens 		return (ENXIO);
590fa9e4066Sahrens 	}
591fa9e4066Sahrens 
592fa9e4066Sahrens 	if (zv->zv_total_opens != 0) {
593fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
594fa9e4066Sahrens 		return (EBUSY);
595fa9e4066Sahrens 	}
596fa9e4066Sahrens 
597fa9e4066Sahrens 	(void) sprintf(namebuf, "%uc,raw", zv->zv_minor);
598fa9e4066Sahrens 	ddi_remove_minor_node(zfs_dip, namebuf);
599fa9e4066Sahrens 
600fa9e4066Sahrens 	(void) sprintf(namebuf, "%uc", zv->zv_minor);
601fa9e4066Sahrens 	ddi_remove_minor_node(zfs_dip, namebuf);
602fa9e4066Sahrens 
603fa9e4066Sahrens 	VERIFY(dsl_prop_unregister(dmu_objset_ds(zv->zv_objset),
604fa9e4066Sahrens 	    "readonly", zvol_readonly_changed_cb, zv) == 0);
605fa9e4066Sahrens 
60622ac5be4Sperrin 	zil_close(zv->zv_zilog);
60722ac5be4Sperrin 	zv->zv_zilog = NULL;
608fa9e4066Sahrens 	dmu_objset_close(zv->zv_objset);
609fa9e4066Sahrens 	zv->zv_objset = NULL;
610c2e6a7d6Sperrin 	avl_destroy(&zv->zv_znode.z_range_avl);
611c2e6a7d6Sperrin 	mutex_destroy(&zv->zv_znode.z_range_lock);
612fa9e4066Sahrens 
613fa9e4066Sahrens 	ddi_soft_state_free(zvol_state, zv->zv_minor);
614fa9e4066Sahrens 
615fa9e4066Sahrens 	zvol_minors--;
616fa9e4066Sahrens 
617fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
618fa9e4066Sahrens 
619fa9e4066Sahrens 	return (0);
620fa9e4066Sahrens }
621fa9e4066Sahrens 
622e7cbe64fSgw int
623e7cbe64fSgw zvol_prealloc(zvol_state_t *zv)
624e7cbe64fSgw {
625e7cbe64fSgw 	objset_t *os = zv->zv_objset;
626e7cbe64fSgw 	dmu_tx_t *tx;
627e7cbe64fSgw 	uint64_t refd, avail, usedobjs, availobjs;
628e7cbe64fSgw 	uint64_t resid = zv->zv_volsize;
629e7cbe64fSgw 	uint64_t off = 0;
630e7cbe64fSgw 
631e7cbe64fSgw 	/* Check the space usage before attempting to allocate the space */
632e7cbe64fSgw 	dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs);
633e7cbe64fSgw 	if (avail < zv->zv_volsize)
634e7cbe64fSgw 		return (ENOSPC);
635e7cbe64fSgw 
636e7cbe64fSgw 	/* Free old extents if they exist */
637e7cbe64fSgw 	zvol_free_extents(zv);
638e7cbe64fSgw 
639e7cbe64fSgw 	while (resid != 0) {
640e7cbe64fSgw 		int error;
641e7cbe64fSgw 		uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE);
642e7cbe64fSgw 
643e7cbe64fSgw 		tx = dmu_tx_create(os);
644e7cbe64fSgw 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
645e7cbe64fSgw 		error = dmu_tx_assign(tx, TXG_WAIT);
646e7cbe64fSgw 		if (error) {
647e7cbe64fSgw 			dmu_tx_abort(tx);
648cdb0ab79Smaybee 			(void) dmu_free_long_range(os, ZVOL_OBJ, 0, off);
649e7cbe64fSgw 			return (error);
650e7cbe64fSgw 		}
65182c9918fSTim Haley 		dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx);
652e7cbe64fSgw 		dmu_tx_commit(tx);
653e7cbe64fSgw 		off += bytes;
654e7cbe64fSgw 		resid -= bytes;
655e7cbe64fSgw 	}
656e7cbe64fSgw 	txg_wait_synced(dmu_objset_pool(os), 0);
657e7cbe64fSgw 
658e7cbe64fSgw 	return (0);
659e7cbe64fSgw }
660e7cbe64fSgw 
661e7cbe64fSgw int
662e7cbe64fSgw zvol_update_volsize(zvol_state_t *zv, major_t maj, uint64_t volsize)
663e7cbe64fSgw {
664e7cbe64fSgw 	dmu_tx_t *tx;
665e7cbe64fSgw 	int error;
666e7cbe64fSgw 
667e7cbe64fSgw 	ASSERT(MUTEX_HELD(&zvol_state_lock));
668e7cbe64fSgw 
669e7cbe64fSgw 	tx = dmu_tx_create(zv->zv_objset);
670e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
671e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
672e7cbe64fSgw 	if (error) {
673e7cbe64fSgw 		dmu_tx_abort(tx);
674e7cbe64fSgw 		return (error);
675e7cbe64fSgw 	}
676e7cbe64fSgw 
677e7cbe64fSgw 	error = zap_update(zv->zv_objset, ZVOL_ZAP_OBJ, "size", 8, 1,
678e7cbe64fSgw 	    &volsize, tx);
679e7cbe64fSgw 	dmu_tx_commit(tx);
680e7cbe64fSgw 
681e7cbe64fSgw 	if (error == 0)
682cdb0ab79Smaybee 		error = dmu_free_long_range(zv->zv_objset,
683cdb0ab79Smaybee 		    ZVOL_OBJ, volsize, DMU_OBJECT_END);
684e7cbe64fSgw 
685bb0ade09Sahrens 	/*
686bb0ade09Sahrens 	 * If we are using a faked-up state (zv_minor == 0) then don't
687bb0ade09Sahrens 	 * try to update the in-core zvol state.
688bb0ade09Sahrens 	 */
689bb0ade09Sahrens 	if (error == 0 && zv->zv_minor) {
690e7cbe64fSgw 		zv->zv_volsize = volsize;
691e7cbe64fSgw 		zvol_size_changed(zv, maj);
692e7cbe64fSgw 	}
693e7cbe64fSgw 	return (error);
694e7cbe64fSgw }
695e7cbe64fSgw 
696fa9e4066Sahrens int
69791ebeef5Sahrens zvol_set_volsize(const char *name, major_t maj, uint64_t volsize)
698fa9e4066Sahrens {
699fa9e4066Sahrens 	zvol_state_t *zv;
700fa9e4066Sahrens 	int error;
7015c5460e9Seschrock 	dmu_object_info_t doi;
702e7cbe64fSgw 	uint64_t old_volsize = 0ULL;
703bb0ade09Sahrens 	zvol_state_t state = { 0 };
704fa9e4066Sahrens 
705fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
706fa9e4066Sahrens 
707e9dbad6fSeschrock 	if ((zv = zvol_minor_lookup(name)) == NULL) {
708bb0ade09Sahrens 		/*
709bb0ade09Sahrens 		 * If we are doing a "zfs clone -o volsize=", then the
710bb0ade09Sahrens 		 * minor node won't exist yet.
711bb0ade09Sahrens 		 */
712bb0ade09Sahrens 		error = dmu_objset_open(name, DMU_OST_ZVOL, DS_MODE_OWNER,
713bb0ade09Sahrens 		    &state.zv_objset);
714bb0ade09Sahrens 		if (error != 0)
715bb0ade09Sahrens 			goto out;
716bb0ade09Sahrens 		zv = &state;
717fa9e4066Sahrens 	}
718e7cbe64fSgw 	old_volsize = zv->zv_volsize;
719fa9e4066Sahrens 
7205c5460e9Seschrock 	if ((error = dmu_object_info(zv->zv_objset, ZVOL_OBJ, &doi)) != 0 ||
721e9dbad6fSeschrock 	    (error = zvol_check_volsize(volsize,
722bb0ade09Sahrens 	    doi.doi_data_block_size)) != 0)
723bb0ade09Sahrens 		goto out;
7245c5460e9Seschrock 
725e7cbe64fSgw 	if (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY)) {
726bb0ade09Sahrens 		error = EROFS;
727bb0ade09Sahrens 		goto out;
728fa9e4066Sahrens 	}
729fa9e4066Sahrens 
730e7cbe64fSgw 	error = zvol_update_volsize(zv, maj, volsize);
731fa9e4066Sahrens 
732e7cbe64fSgw 	/*
733e7cbe64fSgw 	 * Reinitialize the dump area to the new size. If we
734e7cbe64fSgw 	 * failed to resize the dump area then restore the it back to
735e7cbe64fSgw 	 * it's original size.
736e7cbe64fSgw 	 */
737e7cbe64fSgw 	if (error == 0 && zv->zv_flags & ZVOL_DUMPIFIED) {
738e7cbe64fSgw 		if ((error = zvol_dumpify(zv)) != 0 ||
739e7cbe64fSgw 		    (error = dumpvp_resize()) != 0) {
740e7cbe64fSgw 			(void) zvol_update_volsize(zv, maj, old_volsize);
741e7cbe64fSgw 			error = zvol_dumpify(zv);
742e7cbe64fSgw 		}
743fa9e4066Sahrens 	}
744fa9e4066Sahrens 
745*573ca77eSGeorge Wilson 	/*
746*573ca77eSGeorge Wilson 	 * Generate a LUN expansion event.
747*573ca77eSGeorge Wilson 	 */
748*573ca77eSGeorge Wilson 	if (error == 0) {
749*573ca77eSGeorge Wilson 		sysevent_id_t eid;
750*573ca77eSGeorge Wilson 		nvlist_t *attr;
751*573ca77eSGeorge Wilson 		char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
752*573ca77eSGeorge Wilson 
753*573ca77eSGeorge Wilson 		(void) snprintf(physpath, MAXPATHLEN, "%s%uc", ZVOL_PSEUDO_DEV,
754*573ca77eSGeorge Wilson 		    zv->zv_minor);
755*573ca77eSGeorge Wilson 
756*573ca77eSGeorge Wilson 		VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
757*573ca77eSGeorge Wilson 		VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
758*573ca77eSGeorge Wilson 
759*573ca77eSGeorge Wilson 		(void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
760*573ca77eSGeorge Wilson 		    ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
761*573ca77eSGeorge Wilson 
762*573ca77eSGeorge Wilson 		nvlist_free(attr);
763*573ca77eSGeorge Wilson 		kmem_free(physpath, MAXPATHLEN);
764*573ca77eSGeorge Wilson 	}
765*573ca77eSGeorge Wilson 
766bb0ade09Sahrens out:
767bb0ade09Sahrens 	if (state.zv_objset)
768bb0ade09Sahrens 		dmu_objset_close(state.zv_objset);
769bb0ade09Sahrens 
770fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
771fa9e4066Sahrens 
772fa9e4066Sahrens 	return (error);
773fa9e4066Sahrens }
774fa9e4066Sahrens 
775fa9e4066Sahrens int
776e9dbad6fSeschrock zvol_set_volblocksize(const char *name, uint64_t volblocksize)
777fa9e4066Sahrens {
778fa9e4066Sahrens 	zvol_state_t *zv;
779fa9e4066Sahrens 	dmu_tx_t *tx;
780fa9e4066Sahrens 	int error;
78188b7b0f2SMatthew Ahrens 	boolean_t needlock;
782fa9e4066Sahrens 
78388b7b0f2SMatthew Ahrens 	/*
78488b7b0f2SMatthew Ahrens 	 * The lock may already be held if we are being called from
78588b7b0f2SMatthew Ahrens 	 * zvol_dump_init().
78688b7b0f2SMatthew Ahrens 	 */
78788b7b0f2SMatthew Ahrens 	needlock = !MUTEX_HELD(&zvol_state_lock);
78888b7b0f2SMatthew Ahrens 	if (needlock)
78988b7b0f2SMatthew Ahrens 		mutex_enter(&zvol_state_lock);
790fa9e4066Sahrens 
791e9dbad6fSeschrock 	if ((zv = zvol_minor_lookup(name)) == NULL) {
79288b7b0f2SMatthew Ahrens 		if (needlock)
79388b7b0f2SMatthew Ahrens 			mutex_exit(&zvol_state_lock);
794fa9e4066Sahrens 		return (ENXIO);
795fa9e4066Sahrens 	}
796e7cbe64fSgw 	if (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY)) {
79788b7b0f2SMatthew Ahrens 		if (needlock)
79888b7b0f2SMatthew Ahrens 			mutex_exit(&zvol_state_lock);
799fa9e4066Sahrens 		return (EROFS);
800fa9e4066Sahrens 	}
801fa9e4066Sahrens 
802fa9e4066Sahrens 	tx = dmu_tx_create(zv->zv_objset);
803fa9e4066Sahrens 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
804fa9e4066Sahrens 	error = dmu_tx_assign(tx, TXG_WAIT);
805fa9e4066Sahrens 	if (error) {
806fa9e4066Sahrens 		dmu_tx_abort(tx);
807fa9e4066Sahrens 	} else {
808fa9e4066Sahrens 		error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ,
809e9dbad6fSeschrock 		    volblocksize, 0, tx);
810fa9e4066Sahrens 		if (error == ENOTSUP)
811fa9e4066Sahrens 			error = EBUSY;
812fa9e4066Sahrens 		dmu_tx_commit(tx);
81388b7b0f2SMatthew Ahrens 		if (error == 0)
81488b7b0f2SMatthew Ahrens 			zv->zv_volblocksize = volblocksize;
815fa9e4066Sahrens 	}
816fa9e4066Sahrens 
81788b7b0f2SMatthew Ahrens 	if (needlock)
81888b7b0f2SMatthew Ahrens 		mutex_exit(&zvol_state_lock);
819fa9e4066Sahrens 
820fa9e4066Sahrens 	return (error);
821fa9e4066Sahrens }
822fa9e4066Sahrens 
823fa9e4066Sahrens /*ARGSUSED*/
824fa9e4066Sahrens int
825fa9e4066Sahrens zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr)
826fa9e4066Sahrens {
827fa9e4066Sahrens 	minor_t minor = getminor(*devp);
828fa9e4066Sahrens 	zvol_state_t *zv;
829fa9e4066Sahrens 
830fa9e4066Sahrens 	if (minor == 0)			/* This is the control device */
831fa9e4066Sahrens 		return (0);
832fa9e4066Sahrens 
833fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
834fa9e4066Sahrens 
835fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
836fa9e4066Sahrens 	if (zv == NULL) {
837fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
838fa9e4066Sahrens 		return (ENXIO);
839fa9e4066Sahrens 	}
840fa9e4066Sahrens 
841fa9e4066Sahrens 	ASSERT(zv->zv_objset != NULL);
842fa9e4066Sahrens 
843fa9e4066Sahrens 	if ((flag & FWRITE) &&
844e7cbe64fSgw 	    (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY))) {
845fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
846fa9e4066Sahrens 		return (EROFS);
847fa9e4066Sahrens 	}
848c7f714e2SEric Taylor 	if (zv->zv_flags & ZVOL_EXCL) {
849c7f714e2SEric Taylor 		mutex_exit(&zvol_state_lock);
850c7f714e2SEric Taylor 		return (EBUSY);
851c7f714e2SEric Taylor 	}
852c7f714e2SEric Taylor 	if (flag & FEXCL) {
853c7f714e2SEric Taylor 		if (zv->zv_total_opens != 0) {
854c7f714e2SEric Taylor 			mutex_exit(&zvol_state_lock);
855c7f714e2SEric Taylor 			return (EBUSY);
856c7f714e2SEric Taylor 		}
857c7f714e2SEric Taylor 		zv->zv_flags |= ZVOL_EXCL;
858c7f714e2SEric Taylor 	}
859fa9e4066Sahrens 
860fa9e4066Sahrens 	if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) {
861fa9e4066Sahrens 		zv->zv_open_count[otyp]++;
862fa9e4066Sahrens 		zv->zv_total_opens++;
863fa9e4066Sahrens 	}
864fa9e4066Sahrens 
865fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
866fa9e4066Sahrens 
867fa9e4066Sahrens 	return (0);
868fa9e4066Sahrens }
869fa9e4066Sahrens 
870fa9e4066Sahrens /*ARGSUSED*/
871fa9e4066Sahrens int
872fa9e4066Sahrens zvol_close(dev_t dev, int flag, int otyp, cred_t *cr)
873fa9e4066Sahrens {
874fa9e4066Sahrens 	minor_t minor = getminor(dev);
875fa9e4066Sahrens 	zvol_state_t *zv;
876fa9e4066Sahrens 
877fa9e4066Sahrens 	if (minor == 0)		/* This is the control device */
878fa9e4066Sahrens 		return (0);
879fa9e4066Sahrens 
880fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
881fa9e4066Sahrens 
882fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
883fa9e4066Sahrens 	if (zv == NULL) {
884fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
885fa9e4066Sahrens 		return (ENXIO);
886fa9e4066Sahrens 	}
887fa9e4066Sahrens 
888c7f714e2SEric Taylor 	if (zv->zv_flags & ZVOL_EXCL) {
889c7f714e2SEric Taylor 		ASSERT(zv->zv_total_opens == 1);
890c7f714e2SEric Taylor 		zv->zv_flags &= ~ZVOL_EXCL;
891fa9e4066Sahrens 	}
892fa9e4066Sahrens 
893fa9e4066Sahrens 	/*
894fa9e4066Sahrens 	 * If the open count is zero, this is a spurious close.
895fa9e4066Sahrens 	 * That indicates a bug in the kernel / DDI framework.
896fa9e4066Sahrens 	 */
897fa9e4066Sahrens 	ASSERT(zv->zv_open_count[otyp] != 0);
898fa9e4066Sahrens 	ASSERT(zv->zv_total_opens != 0);
899fa9e4066Sahrens 
900fa9e4066Sahrens 	/*
901fa9e4066Sahrens 	 * You may get multiple opens, but only one close.
902fa9e4066Sahrens 	 */
903fa9e4066Sahrens 	zv->zv_open_count[otyp]--;
904fa9e4066Sahrens 	zv->zv_total_opens--;
905fa9e4066Sahrens 
906fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
907fa9e4066Sahrens 
908fa9e4066Sahrens 	return (0);
909fa9e4066Sahrens }
910fa9e4066Sahrens 
911feb08c6bSbillm static void
91267bd71c6Sperrin zvol_get_done(dmu_buf_t *db, void *vzgd)
91367bd71c6Sperrin {
91467bd71c6Sperrin 	zgd_t *zgd = (zgd_t *)vzgd;
915c2e6a7d6Sperrin 	rl_t *rl = zgd->zgd_rl;
91667bd71c6Sperrin 
91767bd71c6Sperrin 	dmu_buf_rele(db, vzgd);
918c2e6a7d6Sperrin 	zfs_range_unlock(rl);
91917f17c2dSbonwick 	zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
92067bd71c6Sperrin 	kmem_free(zgd, sizeof (zgd_t));
92167bd71c6Sperrin }
92267bd71c6Sperrin 
92367bd71c6Sperrin /*
92467bd71c6Sperrin  * Get data to generate a TX_WRITE intent log record.
92567bd71c6Sperrin  */
926feb08c6bSbillm static int
92767bd71c6Sperrin zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
92867bd71c6Sperrin {
92967bd71c6Sperrin 	zvol_state_t *zv = arg;
93067bd71c6Sperrin 	objset_t *os = zv->zv_objset;
93167bd71c6Sperrin 	dmu_buf_t *db;
932c2e6a7d6Sperrin 	rl_t *rl;
93367bd71c6Sperrin 	zgd_t *zgd;
934c2e6a7d6Sperrin 	uint64_t boff; 			/* block starting offset */
935c2e6a7d6Sperrin 	int dlen = lr->lr_length;	/* length of user data */
93667bd71c6Sperrin 	int error;
93767bd71c6Sperrin 
93867bd71c6Sperrin 	ASSERT(zio);
939c2e6a7d6Sperrin 	ASSERT(dlen != 0);
940feb08c6bSbillm 
941c2e6a7d6Sperrin 	/*
942c2e6a7d6Sperrin 	 * Write records come in two flavors: immediate and indirect.
943c2e6a7d6Sperrin 	 * For small writes it's cheaper to store the data with the
944c2e6a7d6Sperrin 	 * log record (immediate); for large writes it's cheaper to
945c2e6a7d6Sperrin 	 * sync the data and get a pointer to it (indirect) so that
946c2e6a7d6Sperrin 	 * we don't have to write the data twice.
947c2e6a7d6Sperrin 	 */
948c2e6a7d6Sperrin 	if (buf != NULL) /* immediate write */
9497bfdf011SNeil Perrin 		return (dmu_read(os, ZVOL_OBJ, lr->lr_offset, dlen, buf,
9507bfdf011SNeil Perrin 		    DMU_READ_NO_PREFETCH));
95167bd71c6Sperrin 
95267bd71c6Sperrin 	zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP);
95367bd71c6Sperrin 	zgd->zgd_zilog = zv->zv_zilog;
95467bd71c6Sperrin 	zgd->zgd_bp = &lr->lr_blkptr;
95567bd71c6Sperrin 
95667bd71c6Sperrin 	/*
957c2e6a7d6Sperrin 	 * Lock the range of the block to ensure that when the data is
958e7cbe64fSgw 	 * written out and its checksum is being calculated that no other
959c2e6a7d6Sperrin 	 * thread can change the block.
96067bd71c6Sperrin 	 */
961c2e6a7d6Sperrin 	boff = P2ALIGN_TYPED(lr->lr_offset, zv->zv_volblocksize, uint64_t);
962c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, boff, zv->zv_volblocksize,
963c2e6a7d6Sperrin 	    RL_READER);
964c2e6a7d6Sperrin 	zgd->zgd_rl = rl;
965c2e6a7d6Sperrin 
966c2e6a7d6Sperrin 	VERIFY(0 == dmu_buf_hold(os, ZVOL_OBJ, lr->lr_offset, zgd, &db));
96767bd71c6Sperrin 	error = dmu_sync(zio, db, &lr->lr_blkptr,
96867bd71c6Sperrin 	    lr->lr_common.lrc_txg, zvol_get_done, zgd);
969feb08c6bSbillm 	if (error == 0)
97017f17c2dSbonwick 		zil_add_block(zv->zv_zilog, &lr->lr_blkptr);
97167bd71c6Sperrin 	/*
97267bd71c6Sperrin 	 * If we get EINPROGRESS, then we need to wait for a
97367bd71c6Sperrin 	 * write IO initiated by dmu_sync() to complete before
97467bd71c6Sperrin 	 * we can release this dbuf.  We will finish everything
97567bd71c6Sperrin 	 * up in the zvol_get_done() callback.
97667bd71c6Sperrin 	 */
97767bd71c6Sperrin 	if (error == EINPROGRESS)
97867bd71c6Sperrin 		return (0);
97967bd71c6Sperrin 	dmu_buf_rele(db, zgd);
980c2e6a7d6Sperrin 	zfs_range_unlock(rl);
98167bd71c6Sperrin 	kmem_free(zgd, sizeof (zgd_t));
98267bd71c6Sperrin 	return (error);
98367bd71c6Sperrin }
98467bd71c6Sperrin 
985a24e15ceSperrin /*
986a24e15ceSperrin  * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
98722ac5be4Sperrin  *
98822ac5be4Sperrin  * We store data in the log buffers if it's small enough.
98967bd71c6Sperrin  * Otherwise we will later flush the data out via dmu_sync().
99022ac5be4Sperrin  */
99167bd71c6Sperrin ssize_t zvol_immediate_write_sz = 32768;
99222ac5be4Sperrin 
993feb08c6bSbillm static void
994510b6c0eSNeil Perrin zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid,
995510b6c0eSNeil Perrin     boolean_t sync)
99622ac5be4Sperrin {
997feb08c6bSbillm 	uint32_t blocksize = zv->zv_volblocksize;
9981209a471SNeil Perrin 	zilog_t *zilog = zv->zv_zilog;
999510b6c0eSNeil Perrin 	boolean_t slogging;
1000510b6c0eSNeil Perrin 
1001510b6c0eSNeil Perrin 	if (zil_disable)
1002510b6c0eSNeil Perrin 		return;
100322ac5be4Sperrin 
10041209a471SNeil Perrin 	if (zilog->zl_replay) {
10051209a471SNeil Perrin 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
10061209a471SNeil Perrin 		zilog->zl_replayed_seq[dmu_tx_get_txg(tx) & TXG_MASK] =
10071209a471SNeil Perrin 		    zilog->zl_replaying_seq;
10081209a471SNeil Perrin 		return;
10091209a471SNeil Perrin 	}
10101209a471SNeil Perrin 
1011510b6c0eSNeil Perrin 	slogging = spa_has_slogs(zilog->zl_spa);
1012feb08c6bSbillm 
1013510b6c0eSNeil Perrin 	while (resid) {
1014510b6c0eSNeil Perrin 		itx_t *itx;
1015510b6c0eSNeil Perrin 		lr_write_t *lr;
1016510b6c0eSNeil Perrin 		ssize_t len;
1017510b6c0eSNeil Perrin 		itx_wr_state_t write_state;
1018510b6c0eSNeil Perrin 
1019510b6c0eSNeil Perrin 		/*
1020510b6c0eSNeil Perrin 		 * Unlike zfs_log_write() we can be called with
1021510b6c0eSNeil Perrin 		 * upto DMU_MAX_ACCESS/2 (5MB) writes.
1022510b6c0eSNeil Perrin 		 */
1023510b6c0eSNeil Perrin 		if (blocksize > zvol_immediate_write_sz && !slogging &&
1024510b6c0eSNeil Perrin 		    resid >= blocksize && off % blocksize == 0) {
1025510b6c0eSNeil Perrin 			write_state = WR_INDIRECT; /* uses dmu_sync */
1026510b6c0eSNeil Perrin 			len = blocksize;
1027510b6c0eSNeil Perrin 		} else if (sync) {
1028510b6c0eSNeil Perrin 			write_state = WR_COPIED;
1029510b6c0eSNeil Perrin 			len = MIN(ZIL_MAX_LOG_DATA, resid);
1030510b6c0eSNeil Perrin 		} else {
1031510b6c0eSNeil Perrin 			write_state = WR_NEED_COPY;
1032510b6c0eSNeil Perrin 			len = MIN(ZIL_MAX_LOG_DATA, resid);
1033510b6c0eSNeil Perrin 		}
1034510b6c0eSNeil Perrin 
1035510b6c0eSNeil Perrin 		itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
1036510b6c0eSNeil Perrin 		    (write_state == WR_COPIED ? len : 0));
1037feb08c6bSbillm 		lr = (lr_write_t *)&itx->itx_lr;
1038510b6c0eSNeil Perrin 		if (write_state == WR_COPIED && dmu_read(zv->zv_objset,
10397bfdf011SNeil Perrin 		    ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
1040510b6c0eSNeil Perrin 			kmem_free(itx, offsetof(itx_t, itx_lr) +
1041510b6c0eSNeil Perrin 			    itx->itx_lr.lrc_reclen);
1042510b6c0eSNeil Perrin 			itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1043510b6c0eSNeil Perrin 			lr = (lr_write_t *)&itx->itx_lr;
1044510b6c0eSNeil Perrin 			write_state = WR_NEED_COPY;
1045510b6c0eSNeil Perrin 		}
1046510b6c0eSNeil Perrin 
1047510b6c0eSNeil Perrin 		itx->itx_wr_state = write_state;
1048510b6c0eSNeil Perrin 		if (write_state == WR_NEED_COPY)
1049510b6c0eSNeil Perrin 			itx->itx_sod += len;
1050feb08c6bSbillm 		lr->lr_foid = ZVOL_OBJ;
1051feb08c6bSbillm 		lr->lr_offset = off;
1052510b6c0eSNeil Perrin 		lr->lr_length = len;
1053feb08c6bSbillm 		lr->lr_blkoff = off - P2ALIGN_TYPED(off, blocksize, uint64_t);
1054feb08c6bSbillm 		BP_ZERO(&lr->lr_blkptr);
1055feb08c6bSbillm 
1056510b6c0eSNeil Perrin 		itx->itx_private = zv;
1057510b6c0eSNeil Perrin 		itx->itx_sync = sync;
1058510b6c0eSNeil Perrin 
10591209a471SNeil Perrin 		(void) zil_itx_assign(zilog, itx, tx);
1060510b6c0eSNeil Perrin 
1061510b6c0eSNeil Perrin 		off += len;
1062510b6c0eSNeil Perrin 		resid -= len;
106322ac5be4Sperrin 	}
106422ac5be4Sperrin }
106522ac5be4Sperrin 
106688b7b0f2SMatthew Ahrens static int
106788b7b0f2SMatthew Ahrens zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t size,
106888b7b0f2SMatthew Ahrens     boolean_t doread, boolean_t isdump)
1069e7cbe64fSgw {
1070e7cbe64fSgw 	vdev_disk_t *dvd;
1071e7cbe64fSgw 	int c;
1072e7cbe64fSgw 	int numerrors = 0;
1073e7cbe64fSgw 
1074e7cbe64fSgw 	for (c = 0; c < vd->vdev_children; c++) {
107521ecdf64SLin Ling 		ASSERT(vd->vdev_ops == &vdev_mirror_ops ||
107621ecdf64SLin Ling 		    vd->vdev_ops == &vdev_replacing_ops ||
107721ecdf64SLin Ling 		    vd->vdev_ops == &vdev_spare_ops);
107888b7b0f2SMatthew Ahrens 		int err = zvol_dumpio_vdev(vd->vdev_child[c],
107988b7b0f2SMatthew Ahrens 		    addr, offset, size, doread, isdump);
108088b7b0f2SMatthew Ahrens 		if (err != 0) {
1081e7cbe64fSgw 			numerrors++;
108288b7b0f2SMatthew Ahrens 		} else if (doread) {
1083e7cbe64fSgw 			break;
1084e7cbe64fSgw 		}
1085e7cbe64fSgw 	}
1086e7cbe64fSgw 
1087e7cbe64fSgw 	if (!vd->vdev_ops->vdev_op_leaf)
1088e7cbe64fSgw 		return (numerrors < vd->vdev_children ? 0 : EIO);
1089e7cbe64fSgw 
1090dc0bb255SEric Taylor 	if (doread && !vdev_readable(vd))
1091dc0bb255SEric Taylor 		return (EIO);
1092dc0bb255SEric Taylor 	else if (!doread && !vdev_writeable(vd))
1093e7cbe64fSgw 		return (EIO);
1094e7cbe64fSgw 
1095e7cbe64fSgw 	dvd = vd->vdev_tsd;
1096e7cbe64fSgw 	ASSERT3P(dvd, !=, NULL);
1097e7cbe64fSgw 	offset += VDEV_LABEL_START_SIZE;
1098e7cbe64fSgw 
1099e7cbe64fSgw 	if (ddi_in_panic() || isdump) {
110088b7b0f2SMatthew Ahrens 		ASSERT(!doread);
110188b7b0f2SMatthew Ahrens 		if (doread)
1102e7cbe64fSgw 			return (EIO);
1103e7cbe64fSgw 		return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset),
1104e7cbe64fSgw 		    lbtodb(size)));
1105e7cbe64fSgw 	} else {
1106e7cbe64fSgw 		return (vdev_disk_physio(dvd->vd_lh, addr, size, offset,
110788b7b0f2SMatthew Ahrens 		    doread ? B_READ : B_WRITE));
1108e7cbe64fSgw 	}
1109e7cbe64fSgw }
1110e7cbe64fSgw 
111188b7b0f2SMatthew Ahrens static int
111288b7b0f2SMatthew Ahrens zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size,
111388b7b0f2SMatthew Ahrens     boolean_t doread, boolean_t isdump)
1114e7cbe64fSgw {
1115e7cbe64fSgw 	vdev_t *vd;
1116e7cbe64fSgw 	int error;
111788b7b0f2SMatthew Ahrens 	zvol_extent_t *ze;
1118e7cbe64fSgw 	spa_t *spa = dmu_objset_spa(zv->zv_objset);
1119e7cbe64fSgw 
112088b7b0f2SMatthew Ahrens 	/* Must be sector aligned, and not stradle a block boundary. */
112188b7b0f2SMatthew Ahrens 	if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) ||
112288b7b0f2SMatthew Ahrens 	    P2BOUNDARY(offset, size, zv->zv_volblocksize)) {
1123e7cbe64fSgw 		return (EINVAL);
112488b7b0f2SMatthew Ahrens 	}
112588b7b0f2SMatthew Ahrens 	ASSERT(size <= zv->zv_volblocksize);
1126e7cbe64fSgw 
112788b7b0f2SMatthew Ahrens 	/* Locate the extent this belongs to */
112888b7b0f2SMatthew Ahrens 	ze = list_head(&zv->zv_extents);
112988b7b0f2SMatthew Ahrens 	while (offset >= ze->ze_nblks * zv->zv_volblocksize) {
113088b7b0f2SMatthew Ahrens 		offset -= ze->ze_nblks * zv->zv_volblocksize;
113188b7b0f2SMatthew Ahrens 		ze = list_next(&zv->zv_extents, ze);
113288b7b0f2SMatthew Ahrens 	}
1133e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
113488b7b0f2SMatthew Ahrens 	vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva));
113588b7b0f2SMatthew Ahrens 	offset += DVA_GET_OFFSET(&ze->ze_dva);
113688b7b0f2SMatthew Ahrens 	error = zvol_dumpio_vdev(vd, addr, offset, size, doread, isdump);
1137e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
1138e7cbe64fSgw 	return (error);
1139e7cbe64fSgw }
1140e7cbe64fSgw 
1141fa9e4066Sahrens int
1142fa9e4066Sahrens zvol_strategy(buf_t *bp)
1143fa9e4066Sahrens {
1144fa9e4066Sahrens 	zvol_state_t *zv = ddi_get_soft_state(zvol_state, getminor(bp->b_edev));
1145fa9e4066Sahrens 	uint64_t off, volsize;
114688b7b0f2SMatthew Ahrens 	size_t resid;
1147fa9e4066Sahrens 	char *addr;
114822ac5be4Sperrin 	objset_t *os;
1149c2e6a7d6Sperrin 	rl_t *rl;
1150fa9e4066Sahrens 	int error = 0;
115188b7b0f2SMatthew Ahrens 	boolean_t doread = bp->b_flags & B_READ;
115288b7b0f2SMatthew Ahrens 	boolean_t is_dump = zv->zv_flags & ZVOL_DUMPIFIED;
1153510b6c0eSNeil Perrin 	boolean_t sync;
1154fa9e4066Sahrens 
1155fa9e4066Sahrens 	if (zv == NULL) {
1156fa9e4066Sahrens 		bioerror(bp, ENXIO);
1157fa9e4066Sahrens 		biodone(bp);
1158fa9e4066Sahrens 		return (0);
1159fa9e4066Sahrens 	}
1160fa9e4066Sahrens 
1161fa9e4066Sahrens 	if (getminor(bp->b_edev) == 0) {
1162fa9e4066Sahrens 		bioerror(bp, EINVAL);
1163fa9e4066Sahrens 		biodone(bp);
1164fa9e4066Sahrens 		return (0);
1165fa9e4066Sahrens 	}
1166fa9e4066Sahrens 
1167e7cbe64fSgw 	if (!(bp->b_flags & B_READ) &&
1168e7cbe64fSgw 	    (zv->zv_flags & ZVOL_RDONLY ||
1169e7cbe64fSgw 	    zv->zv_mode & DS_MODE_READONLY)) {
1170fa9e4066Sahrens 		bioerror(bp, EROFS);
1171fa9e4066Sahrens 		biodone(bp);
1172fa9e4066Sahrens 		return (0);
1173fa9e4066Sahrens 	}
1174fa9e4066Sahrens 
1175fa9e4066Sahrens 	off = ldbtob(bp->b_blkno);
1176fa9e4066Sahrens 	volsize = zv->zv_volsize;
1177fa9e4066Sahrens 
117822ac5be4Sperrin 	os = zv->zv_objset;
117922ac5be4Sperrin 	ASSERT(os != NULL);
1180fa9e4066Sahrens 
1181fa9e4066Sahrens 	bp_mapin(bp);
1182fa9e4066Sahrens 	addr = bp->b_un.b_addr;
1183fa9e4066Sahrens 	resid = bp->b_bcount;
1184fa9e4066Sahrens 
118588b7b0f2SMatthew Ahrens 	if (resid > 0 && (off < 0 || off >= volsize)) {
118688b7b0f2SMatthew Ahrens 		bioerror(bp, EIO);
118788b7b0f2SMatthew Ahrens 		biodone(bp);
118888b7b0f2SMatthew Ahrens 		return (0);
118988b7b0f2SMatthew Ahrens 	}
119073ec3d9cSgw 
1191510b6c0eSNeil Perrin 	sync = !(bp->b_flags & B_ASYNC) && !doread && !is_dump &&
1192510b6c0eSNeil Perrin 	    !(zv->zv_flags & ZVOL_WCE) && !zil_disable;
1193510b6c0eSNeil Perrin 
1194a24e15ceSperrin 	/*
1195a24e15ceSperrin 	 * There must be no buffer changes when doing a dmu_sync() because
1196a24e15ceSperrin 	 * we can't change the data whilst calculating the checksum.
1197a24e15ceSperrin 	 */
1198c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, off, resid,
119988b7b0f2SMatthew Ahrens 	    doread ? RL_READER : RL_WRITER);
1200fa9e4066Sahrens 
1201e7cbe64fSgw 	while (resid != 0 && off < volsize) {
120288b7b0f2SMatthew Ahrens 		size_t size = MIN(resid, zvol_maxphys);
1203e7cbe64fSgw 		if (is_dump) {
1204e7cbe64fSgw 			size = MIN(size, P2END(off, zv->zv_volblocksize) - off);
120588b7b0f2SMatthew Ahrens 			error = zvol_dumpio(zv, addr, off, size,
120688b7b0f2SMatthew Ahrens 			    doread, B_FALSE);
120788b7b0f2SMatthew Ahrens 		} else if (doread) {
12087bfdf011SNeil Perrin 			error = dmu_read(os, ZVOL_OBJ, off, size, addr,
12097bfdf011SNeil Perrin 			    DMU_READ_PREFETCH);
1210fa9e4066Sahrens 		} else {
121122ac5be4Sperrin 			dmu_tx_t *tx = dmu_tx_create(os);
1212fa9e4066Sahrens 			dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
1213fa9e4066Sahrens 			error = dmu_tx_assign(tx, TXG_WAIT);
1214fa9e4066Sahrens 			if (error) {
1215fa9e4066Sahrens 				dmu_tx_abort(tx);
1216fa9e4066Sahrens 			} else {
121722ac5be4Sperrin 				dmu_write(os, ZVOL_OBJ, off, size, addr, tx);
1218510b6c0eSNeil Perrin 				zvol_log_write(zv, tx, off, size, sync);
1219fa9e4066Sahrens 				dmu_tx_commit(tx);
1220fa9e4066Sahrens 			}
1221fa9e4066Sahrens 		}
1222b87f3af3Sperrin 		if (error) {
1223b87f3af3Sperrin 			/* convert checksum errors into IO errors */
1224b87f3af3Sperrin 			if (error == ECKSUM)
1225b87f3af3Sperrin 				error = EIO;
1226fa9e4066Sahrens 			break;
1227b87f3af3Sperrin 		}
1228fa9e4066Sahrens 		off += size;
1229fa9e4066Sahrens 		addr += size;
1230fa9e4066Sahrens 		resid -= size;
1231fa9e4066Sahrens 	}
1232c2e6a7d6Sperrin 	zfs_range_unlock(rl);
1233fa9e4066Sahrens 
1234fa9e4066Sahrens 	if ((bp->b_resid = resid) == bp->b_bcount)
1235fa9e4066Sahrens 		bioerror(bp, off > volsize ? EINVAL : error);
1236fa9e4066Sahrens 
1237510b6c0eSNeil Perrin 	if (sync)
1238feb08c6bSbillm 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1239feb08c6bSbillm 	biodone(bp);
124022ac5be4Sperrin 
1241fa9e4066Sahrens 	return (0);
1242fa9e4066Sahrens }
1243fa9e4066Sahrens 
124467bd71c6Sperrin /*
124567bd71c6Sperrin  * Set the buffer count to the zvol maximum transfer.
124667bd71c6Sperrin  * Using our own routine instead of the default minphys()
124767bd71c6Sperrin  * means that for larger writes we write bigger buffers on X86
124867bd71c6Sperrin  * (128K instead of 56K) and flush the disk write cache less often
124967bd71c6Sperrin  * (every zvol_maxphys - currently 1MB) instead of minphys (currently
125067bd71c6Sperrin  * 56K on X86 and 128K on sparc).
125167bd71c6Sperrin  */
125267bd71c6Sperrin void
125367bd71c6Sperrin zvol_minphys(struct buf *bp)
125467bd71c6Sperrin {
125567bd71c6Sperrin 	if (bp->b_bcount > zvol_maxphys)
125667bd71c6Sperrin 		bp->b_bcount = zvol_maxphys;
125767bd71c6Sperrin }
125867bd71c6Sperrin 
1259e7cbe64fSgw int
1260e7cbe64fSgw zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks)
1261e7cbe64fSgw {
1262e7cbe64fSgw 	minor_t minor = getminor(dev);
1263e7cbe64fSgw 	zvol_state_t *zv;
1264e7cbe64fSgw 	int error = 0;
1265e7cbe64fSgw 	uint64_t size;
1266e7cbe64fSgw 	uint64_t boff;
1267e7cbe64fSgw 	uint64_t resid;
1268e7cbe64fSgw 
1269e7cbe64fSgw 	if (minor == 0)			/* This is the control device */
1270e7cbe64fSgw 		return (ENXIO);
1271e7cbe64fSgw 
1272e7cbe64fSgw 	zv = ddi_get_soft_state(zvol_state, minor);
1273e7cbe64fSgw 	if (zv == NULL)
1274e7cbe64fSgw 		return (ENXIO);
1275e7cbe64fSgw 
1276e7cbe64fSgw 	boff = ldbtob(blkno);
1277e7cbe64fSgw 	resid = ldbtob(nblocks);
127888b7b0f2SMatthew Ahrens 
127988b7b0f2SMatthew Ahrens 	VERIFY3U(boff + resid, <=, zv->zv_volsize);
128088b7b0f2SMatthew Ahrens 
1281e7cbe64fSgw 	while (resid) {
1282e7cbe64fSgw 		size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff);
128388b7b0f2SMatthew Ahrens 		error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE);
1284e7cbe64fSgw 		if (error)
1285e7cbe64fSgw 			break;
1286e7cbe64fSgw 		boff += size;
1287e7cbe64fSgw 		addr += size;
1288e7cbe64fSgw 		resid -= size;
1289e7cbe64fSgw 	}
1290e7cbe64fSgw 
1291e7cbe64fSgw 	return (error);
1292e7cbe64fSgw }
1293e7cbe64fSgw 
1294fa9e4066Sahrens /*ARGSUSED*/
1295fa9e4066Sahrens int
1296feb08c6bSbillm zvol_read(dev_t dev, uio_t *uio, cred_t *cr)
1297fa9e4066Sahrens {
1298c7ca1008Sgw 	minor_t minor = getminor(dev);
1299c7ca1008Sgw 	zvol_state_t *zv;
130073ec3d9cSgw 	uint64_t volsize;
1301c2e6a7d6Sperrin 	rl_t *rl;
1302feb08c6bSbillm 	int error = 0;
1303fa9e4066Sahrens 
1304c7ca1008Sgw 	if (minor == 0)			/* This is the control device */
1305c7ca1008Sgw 		return (ENXIO);
1306c7ca1008Sgw 
1307c7ca1008Sgw 	zv = ddi_get_soft_state(zvol_state, minor);
1308c7ca1008Sgw 	if (zv == NULL)
1309c7ca1008Sgw 		return (ENXIO);
1310c7ca1008Sgw 
131173ec3d9cSgw 	volsize = zv->zv_volsize;
131273ec3d9cSgw 	if (uio->uio_resid > 0 &&
131373ec3d9cSgw 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
131473ec3d9cSgw 		return (EIO);
131573ec3d9cSgw 
131688b7b0f2SMatthew Ahrens 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
131788b7b0f2SMatthew Ahrens 		error = physio(zvol_strategy, NULL, dev, B_READ,
131888b7b0f2SMatthew Ahrens 		    zvol_minphys, uio);
131988b7b0f2SMatthew Ahrens 		return (error);
132088b7b0f2SMatthew Ahrens 	}
132188b7b0f2SMatthew Ahrens 
1322c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1323c2e6a7d6Sperrin 	    RL_READER);
132473ec3d9cSgw 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1325feb08c6bSbillm 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1326fa9e4066Sahrens 
132773ec3d9cSgw 		/* don't read past the end */
132873ec3d9cSgw 		if (bytes > volsize - uio->uio_loffset)
132973ec3d9cSgw 			bytes = volsize - uio->uio_loffset;
133073ec3d9cSgw 
1331feb08c6bSbillm 		error =  dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes);
1332b87f3af3Sperrin 		if (error) {
1333b87f3af3Sperrin 			/* convert checksum errors into IO errors */
1334b87f3af3Sperrin 			if (error == ECKSUM)
1335b87f3af3Sperrin 				error = EIO;
1336feb08c6bSbillm 			break;
1337b87f3af3Sperrin 		}
1338feb08c6bSbillm 	}
1339c2e6a7d6Sperrin 	zfs_range_unlock(rl);
1340feb08c6bSbillm 	return (error);
1341fa9e4066Sahrens }
1342fa9e4066Sahrens 
1343fa9e4066Sahrens /*ARGSUSED*/
1344fa9e4066Sahrens int
1345feb08c6bSbillm zvol_write(dev_t dev, uio_t *uio, cred_t *cr)
1346fa9e4066Sahrens {
1347c7ca1008Sgw 	minor_t minor = getminor(dev);
1348c7ca1008Sgw 	zvol_state_t *zv;
134973ec3d9cSgw 	uint64_t volsize;
1350c2e6a7d6Sperrin 	rl_t *rl;
1351feb08c6bSbillm 	int error = 0;
1352510b6c0eSNeil Perrin 	boolean_t sync;
1353feb08c6bSbillm 
1354c7ca1008Sgw 	if (minor == 0)			/* This is the control device */
1355c7ca1008Sgw 		return (ENXIO);
1356c7ca1008Sgw 
1357c7ca1008Sgw 	zv = ddi_get_soft_state(zvol_state, minor);
1358c7ca1008Sgw 	if (zv == NULL)
1359c7ca1008Sgw 		return (ENXIO);
1360c7ca1008Sgw 
136173ec3d9cSgw 	volsize = zv->zv_volsize;
136273ec3d9cSgw 	if (uio->uio_resid > 0 &&
136373ec3d9cSgw 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
136473ec3d9cSgw 		return (EIO);
136573ec3d9cSgw 
1366e7cbe64fSgw 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
1367e7cbe64fSgw 		error = physio(zvol_strategy, NULL, dev, B_WRITE,
1368e7cbe64fSgw 		    zvol_minphys, uio);
1369e7cbe64fSgw 		return (error);
1370e7cbe64fSgw 	}
1371e7cbe64fSgw 
1372510b6c0eSNeil Perrin 	sync = !(zv->zv_flags & ZVOL_WCE) && !zil_disable;
1373510b6c0eSNeil Perrin 
1374c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1375c2e6a7d6Sperrin 	    RL_WRITER);
137673ec3d9cSgw 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1377feb08c6bSbillm 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1378feb08c6bSbillm 		uint64_t off = uio->uio_loffset;
1379feb08c6bSbillm 		dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
138073ec3d9cSgw 
138173ec3d9cSgw 		if (bytes > volsize - off)	/* don't write past the end */
138273ec3d9cSgw 			bytes = volsize - off;
138373ec3d9cSgw 
1384feb08c6bSbillm 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
1385feb08c6bSbillm 		error = dmu_tx_assign(tx, TXG_WAIT);
1386feb08c6bSbillm 		if (error) {
1387feb08c6bSbillm 			dmu_tx_abort(tx);
1388feb08c6bSbillm 			break;
1389feb08c6bSbillm 		}
1390feb08c6bSbillm 		error = dmu_write_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes, tx);
1391feb08c6bSbillm 		if (error == 0)
1392510b6c0eSNeil Perrin 			zvol_log_write(zv, tx, off, bytes, sync);
1393feb08c6bSbillm 		dmu_tx_commit(tx);
1394feb08c6bSbillm 
1395feb08c6bSbillm 		if (error)
1396feb08c6bSbillm 			break;
1397feb08c6bSbillm 	}
1398c2e6a7d6Sperrin 	zfs_range_unlock(rl);
1399510b6c0eSNeil Perrin 	if (sync)
1400e08bf2c6SEric Taylor 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1401feb08c6bSbillm 	return (error);
1402fa9e4066Sahrens }
1403fa9e4066Sahrens 
1404c7f714e2SEric Taylor int
1405c7f714e2SEric Taylor zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs)
1406c7f714e2SEric Taylor {
1407c7f714e2SEric Taylor 	struct uuid uuid = EFI_RESERVED;
1408c7f714e2SEric Taylor 	efi_gpe_t gpe = { 0 };
1409c7f714e2SEric Taylor 	uint32_t crc;
1410c7f714e2SEric Taylor 	dk_efi_t efi;
1411c7f714e2SEric Taylor 	int length;
1412c7f714e2SEric Taylor 	char *ptr;
1413c7f714e2SEric Taylor 
1414c7f714e2SEric Taylor 	if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag))
1415c7f714e2SEric Taylor 		return (EFAULT);
1416c7f714e2SEric Taylor 	ptr = (char *)(uintptr_t)efi.dki_data_64;
1417c7f714e2SEric Taylor 	length = efi.dki_length;
1418c7f714e2SEric Taylor 	/*
1419c7f714e2SEric Taylor 	 * Some clients may attempt to request a PMBR for the
1420c7f714e2SEric Taylor 	 * zvol.  Currently this interface will return EINVAL to
1421c7f714e2SEric Taylor 	 * such requests.  These requests could be supported by
1422c7f714e2SEric Taylor 	 * adding a check for lba == 0 and consing up an appropriate
1423c7f714e2SEric Taylor 	 * PMBR.
1424c7f714e2SEric Taylor 	 */
1425c7f714e2SEric Taylor 	if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0)
1426c7f714e2SEric Taylor 		return (EINVAL);
1427c7f714e2SEric Taylor 
1428c7f714e2SEric Taylor 	gpe.efi_gpe_StartingLBA = LE_64(34ULL);
1429c7f714e2SEric Taylor 	gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1);
1430c7f714e2SEric Taylor 	UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
1431c7f714e2SEric Taylor 
1432c7f714e2SEric Taylor 	if (efi.dki_lba == 1) {
1433c7f714e2SEric Taylor 		efi_gpt_t gpt = { 0 };
1434c7f714e2SEric Taylor 
1435c7f714e2SEric Taylor 		gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
1436c7f714e2SEric Taylor 		gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
1437c7f714e2SEric Taylor 		gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt));
1438c7f714e2SEric Taylor 		gpt.efi_gpt_MyLBA = LE_64(1ULL);
1439c7f714e2SEric Taylor 		gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL);
1440c7f714e2SEric Taylor 		gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1);
1441c7f714e2SEric Taylor 		gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL);
1442c7f714e2SEric Taylor 		gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
1443c7f714e2SEric Taylor 		gpt.efi_gpt_SizeOfPartitionEntry =
1444c7f714e2SEric Taylor 		    LE_32(sizeof (efi_gpe_t));
1445c7f714e2SEric Taylor 		CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
1446c7f714e2SEric Taylor 		gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
1447c7f714e2SEric Taylor 		CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table);
1448c7f714e2SEric Taylor 		gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
1449c7f714e2SEric Taylor 		if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length),
1450c7f714e2SEric Taylor 		    flag))
1451c7f714e2SEric Taylor 			return (EFAULT);
1452c7f714e2SEric Taylor 		ptr += sizeof (gpt);
1453c7f714e2SEric Taylor 		length -= sizeof (gpt);
1454c7f714e2SEric Taylor 	}
1455c7f714e2SEric Taylor 	if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe),
1456c7f714e2SEric Taylor 	    length), flag))
1457c7f714e2SEric Taylor 		return (EFAULT);
1458c7f714e2SEric Taylor 	return (0);
1459c7f714e2SEric Taylor }
1460c7f714e2SEric Taylor 
1461fa9e4066Sahrens /*
1462fa9e4066Sahrens  * Dirtbag ioctls to support mkfs(1M) for UFS filesystems.  See dkio(7I).
1463fa9e4066Sahrens  */
1464fa9e4066Sahrens /*ARGSUSED*/
1465fa9e4066Sahrens int
1466fa9e4066Sahrens zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
1467fa9e4066Sahrens {
1468fa9e4066Sahrens 	zvol_state_t *zv;
1469af2c4821Smaybee 	struct dk_cinfo dki;
1470fa9e4066Sahrens 	struct dk_minfo dkm;
1471af2c4821Smaybee 	struct dk_callback *dkc;
1472fa9e4066Sahrens 	int error = 0;
1473e7cbe64fSgw 	rl_t *rl;
1474fa9e4066Sahrens 
1475fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
1476fa9e4066Sahrens 
1477fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, getminor(dev));
1478fa9e4066Sahrens 
1479fa9e4066Sahrens 	if (zv == NULL) {
1480fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
1481fa9e4066Sahrens 		return (ENXIO);
1482fa9e4066Sahrens 	}
1483701f66c4SEric Taylor 	ASSERT(zv->zv_total_opens > 0);
1484fa9e4066Sahrens 
1485fa9e4066Sahrens 	switch (cmd) {
1486fa9e4066Sahrens 
1487fa9e4066Sahrens 	case DKIOCINFO:
1488af2c4821Smaybee 		bzero(&dki, sizeof (dki));
1489af2c4821Smaybee 		(void) strcpy(dki.dki_cname, "zvol");
1490af2c4821Smaybee 		(void) strcpy(dki.dki_dname, "zvol");
1491af2c4821Smaybee 		dki.dki_ctype = DKC_UNKNOWN;
1492af2c4821Smaybee 		dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs);
1493fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
1494af2c4821Smaybee 		if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag))
1495fa9e4066Sahrens 			error = EFAULT;
1496fa9e4066Sahrens 		return (error);
1497fa9e4066Sahrens 
1498fa9e4066Sahrens 	case DKIOCGMEDIAINFO:
1499fa9e4066Sahrens 		bzero(&dkm, sizeof (dkm));
1500fa9e4066Sahrens 		dkm.dki_lbsize = 1U << zv->zv_min_bs;
1501fa9e4066Sahrens 		dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
1502fa9e4066Sahrens 		dkm.dki_media_type = DK_UNKNOWN;
1503fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
1504fa9e4066Sahrens 		if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
1505fa9e4066Sahrens 			error = EFAULT;
1506fa9e4066Sahrens 		return (error);
1507fa9e4066Sahrens 
1508fa9e4066Sahrens 	case DKIOCGETEFI:
1509c7f714e2SEric Taylor 		{
1510c7f714e2SEric Taylor 			uint64_t vs = zv->zv_volsize;
1511c7f714e2SEric Taylor 			uint8_t bs = zv->zv_min_bs;
1512fa9e4066Sahrens 
151368a5ac4dSmaybee 			mutex_exit(&zvol_state_lock);
1514c7f714e2SEric Taylor 			error = zvol_getefi((void *)arg, flag, vs, bs);
1515c7f714e2SEric Taylor 			return (error);
151668a5ac4dSmaybee 		}
1517fa9e4066Sahrens 
1518feb08c6bSbillm 	case DKIOCFLUSHWRITECACHE:
1519af2c4821Smaybee 		dkc = (struct dk_callback *)arg;
1520701f66c4SEric Taylor 		mutex_exit(&zvol_state_lock);
1521feb08c6bSbillm 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1522af2c4821Smaybee 		if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) {
1523af2c4821Smaybee 			(*dkc->dkc_callback)(dkc->dkc_cookie, error);
1524af2c4821Smaybee 			error = 0;
1525af2c4821Smaybee 		}
1526701f66c4SEric Taylor 		return (error);
1527701f66c4SEric Taylor 
1528701f66c4SEric Taylor 	case DKIOCGETWCE:
1529701f66c4SEric Taylor 		{
1530701f66c4SEric Taylor 			int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0;
1531701f66c4SEric Taylor 			if (ddi_copyout(&wce, (void *)arg, sizeof (int),
1532701f66c4SEric Taylor 			    flag))
1533701f66c4SEric Taylor 				error = EFAULT;
1534701f66c4SEric Taylor 			break;
1535701f66c4SEric Taylor 		}
1536701f66c4SEric Taylor 	case DKIOCSETWCE:
1537701f66c4SEric Taylor 		{
1538701f66c4SEric Taylor 			int wce;
1539701f66c4SEric Taylor 			if (ddi_copyin((void *)arg, &wce, sizeof (int),
1540701f66c4SEric Taylor 			    flag)) {
1541701f66c4SEric Taylor 				error = EFAULT;
1542701f66c4SEric Taylor 				break;
1543701f66c4SEric Taylor 			}
1544701f66c4SEric Taylor 			if (wce) {
1545701f66c4SEric Taylor 				zv->zv_flags |= ZVOL_WCE;
1546701f66c4SEric Taylor 				mutex_exit(&zvol_state_lock);
1547701f66c4SEric Taylor 			} else {
1548701f66c4SEric Taylor 				zv->zv_flags &= ~ZVOL_WCE;
1549701f66c4SEric Taylor 				mutex_exit(&zvol_state_lock);
1550701f66c4SEric Taylor 				zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1551701f66c4SEric Taylor 			}
1552701f66c4SEric Taylor 			return (0);
1553701f66c4SEric Taylor 		}
1554feb08c6bSbillm 
1555b6130eadSmaybee 	case DKIOCGGEOM:
1556b6130eadSmaybee 	case DKIOCGVTOC:
1557e7cbe64fSgw 		/*
1558e7cbe64fSgw 		 * commands using these (like prtvtoc) expect ENOTSUP
1559e7cbe64fSgw 		 * since we're emulating an EFI label
1560e7cbe64fSgw 		 */
1561b6130eadSmaybee 		error = ENOTSUP;
1562b6130eadSmaybee 		break;
1563b6130eadSmaybee 
1564e7cbe64fSgw 	case DKIOCDUMPINIT:
1565e7cbe64fSgw 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1566e7cbe64fSgw 		    RL_WRITER);
1567e7cbe64fSgw 		error = zvol_dumpify(zv);
1568e7cbe64fSgw 		zfs_range_unlock(rl);
1569e7cbe64fSgw 		break;
1570e7cbe64fSgw 
1571e7cbe64fSgw 	case DKIOCDUMPFINI:
157206d5ae10SEric Taylor 		if (!(zv->zv_flags & ZVOL_DUMPIFIED))
157306d5ae10SEric Taylor 			break;
1574e7cbe64fSgw 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1575e7cbe64fSgw 		    RL_WRITER);
1576e7cbe64fSgw 		error = zvol_dump_fini(zv);
1577e7cbe64fSgw 		zfs_range_unlock(rl);
1578e7cbe64fSgw 		break;
1579e7cbe64fSgw 
1580fa9e4066Sahrens 	default:
158168a5ac4dSmaybee 		error = ENOTTY;
1582fa9e4066Sahrens 		break;
1583fa9e4066Sahrens 
1584fa9e4066Sahrens 	}
1585fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
1586fa9e4066Sahrens 	return (error);
1587fa9e4066Sahrens }
1588fa9e4066Sahrens 
1589fa9e4066Sahrens int
1590fa9e4066Sahrens zvol_busy(void)
1591fa9e4066Sahrens {
1592fa9e4066Sahrens 	return (zvol_minors != 0);
1593fa9e4066Sahrens }
1594fa9e4066Sahrens 
1595fa9e4066Sahrens void
1596fa9e4066Sahrens zvol_init(void)
1597fa9e4066Sahrens {
1598fa9e4066Sahrens 	VERIFY(ddi_soft_state_init(&zvol_state, sizeof (zvol_state_t), 1) == 0);
1599fa9e4066Sahrens 	mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
1600fa9e4066Sahrens }
1601fa9e4066Sahrens 
1602fa9e4066Sahrens void
1603fa9e4066Sahrens zvol_fini(void)
1604fa9e4066Sahrens {
1605fa9e4066Sahrens 	mutex_destroy(&zvol_state_lock);
1606fa9e4066Sahrens 	ddi_soft_state_fini(&zvol_state);
1607fa9e4066Sahrens }
1608e7cbe64fSgw 
1609e7cbe64fSgw static boolean_t
1610e7cbe64fSgw zvol_is_swap(zvol_state_t *zv)
1611e7cbe64fSgw {
1612e7cbe64fSgw 	vnode_t *vp;
1613e7cbe64fSgw 	boolean_t ret = B_FALSE;
1614e7cbe64fSgw 	char *devpath;
1615e7cbe64fSgw 	size_t devpathlen;
1616e7cbe64fSgw 	int error;
1617e7cbe64fSgw 
1618e7cbe64fSgw 	devpathlen = strlen(ZVOL_FULL_DEV_DIR) + strlen(zv->zv_name) + 1;
1619e7cbe64fSgw 	devpath = kmem_alloc(devpathlen, KM_SLEEP);
1620e7cbe64fSgw 	(void) sprintf(devpath, "%s%s", ZVOL_FULL_DEV_DIR, zv->zv_name);
1621e7cbe64fSgw 	error = lookupname(devpath, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp);
1622e7cbe64fSgw 	kmem_free(devpath, devpathlen);
1623e7cbe64fSgw 
1624e7cbe64fSgw 	ret = !error && IS_SWAPVP(common_specvp(vp));
1625e7cbe64fSgw 
1626e7cbe64fSgw 	if (vp != NULL)
1627e7cbe64fSgw 		VN_RELE(vp);
1628e7cbe64fSgw 
1629e7cbe64fSgw 	return (ret);
1630e7cbe64fSgw }
1631e7cbe64fSgw 
1632e7cbe64fSgw static int
1633e7cbe64fSgw zvol_dump_init(zvol_state_t *zv, boolean_t resize)
1634e7cbe64fSgw {
1635e7cbe64fSgw 	dmu_tx_t *tx;
1636e7cbe64fSgw 	int error = 0;
1637e7cbe64fSgw 	objset_t *os = zv->zv_objset;
1638e7cbe64fSgw 	nvlist_t *nv = NULL;
1639e7cbe64fSgw 
1640e7cbe64fSgw 	ASSERT(MUTEX_HELD(&zvol_state_lock));
1641e7cbe64fSgw 
1642e7cbe64fSgw 	tx = dmu_tx_create(os);
1643e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1644e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
1645e7cbe64fSgw 	if (error) {
1646e7cbe64fSgw 		dmu_tx_abort(tx);
1647e7cbe64fSgw 		return (error);
1648e7cbe64fSgw 	}
1649e7cbe64fSgw 
1650e7cbe64fSgw 	/*
1651e7cbe64fSgw 	 * If we are resizing the dump device then we only need to
1652e7cbe64fSgw 	 * update the refreservation to match the newly updated
1653e7cbe64fSgw 	 * zvolsize. Otherwise, we save off the original state of the
1654e7cbe64fSgw 	 * zvol so that we can restore them if the zvol is ever undumpified.
1655e7cbe64fSgw 	 */
1656e7cbe64fSgw 	if (resize) {
1657e7cbe64fSgw 		error = zap_update(os, ZVOL_ZAP_OBJ,
1658e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1659e7cbe64fSgw 		    &zv->zv_volsize, tx);
1660e7cbe64fSgw 	} else {
166188b7b0f2SMatthew Ahrens 		uint64_t checksum, compress, refresrv, vbs;
166288b7b0f2SMatthew Ahrens 
1663e7cbe64fSgw 		error = dsl_prop_get_integer(zv->zv_name,
1664e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL);
1665e7cbe64fSgw 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
1666e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL);
1667e7cbe64fSgw 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
1668e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL);
166988b7b0f2SMatthew Ahrens 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
167088b7b0f2SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL);
1671e7cbe64fSgw 
1672e7cbe64fSgw 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1673e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1,
1674e7cbe64fSgw 		    &compress, tx);
1675e7cbe64fSgw 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1676e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx);
1677e7cbe64fSgw 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1678e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1679e7cbe64fSgw 		    &refresrv, tx);
168088b7b0f2SMatthew Ahrens 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
168188b7b0f2SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1,
168288b7b0f2SMatthew Ahrens 		    &vbs, tx);
1683e7cbe64fSgw 	}
1684e7cbe64fSgw 	dmu_tx_commit(tx);
1685e7cbe64fSgw 
1686e7cbe64fSgw 	/* Truncate the file */
1687e7cbe64fSgw 	if (!error)
1688cdb0ab79Smaybee 		error = dmu_free_long_range(zv->zv_objset,
1689cdb0ab79Smaybee 		    ZVOL_OBJ, 0, DMU_OBJECT_END);
1690e7cbe64fSgw 
1691e7cbe64fSgw 	if (error)
1692e7cbe64fSgw 		return (error);
1693e7cbe64fSgw 
1694e7cbe64fSgw 	/*
1695e7cbe64fSgw 	 * We only need update the zvol's property if we are initializing
1696e7cbe64fSgw 	 * the dump area for the first time.
1697e7cbe64fSgw 	 */
1698e7cbe64fSgw 	if (!resize) {
1699e7cbe64fSgw 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1700e7cbe64fSgw 		VERIFY(nvlist_add_uint64(nv,
1701e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0);
1702e7cbe64fSgw 		VERIFY(nvlist_add_uint64(nv,
1703e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
1704e7cbe64fSgw 		    ZIO_COMPRESS_OFF) == 0);
1705e7cbe64fSgw 		VERIFY(nvlist_add_uint64(nv,
1706e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
1707e7cbe64fSgw 		    ZIO_CHECKSUM_OFF) == 0);
170888b7b0f2SMatthew Ahrens 		VERIFY(nvlist_add_uint64(nv,
170988b7b0f2SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
171088b7b0f2SMatthew Ahrens 		    SPA_MAXBLOCKSIZE) == 0);
1711e7cbe64fSgw 
1712e7cbe64fSgw 		error = zfs_set_prop_nvlist(zv->zv_name, nv);
1713e7cbe64fSgw 		nvlist_free(nv);
1714e7cbe64fSgw 
1715e7cbe64fSgw 		if (error)
1716e7cbe64fSgw 			return (error);
1717e7cbe64fSgw 	}
1718e7cbe64fSgw 
1719e7cbe64fSgw 	/* Allocate the space for the dump */
1720e7cbe64fSgw 	error = zvol_prealloc(zv);
1721e7cbe64fSgw 	return (error);
1722e7cbe64fSgw }
1723e7cbe64fSgw 
1724e7cbe64fSgw static int
1725e7cbe64fSgw zvol_dumpify(zvol_state_t *zv)
1726e7cbe64fSgw {
1727e7cbe64fSgw 	int error = 0;
1728e7cbe64fSgw 	uint64_t dumpsize = 0;
1729e7cbe64fSgw 	dmu_tx_t *tx;
1730e7cbe64fSgw 	objset_t *os = zv->zv_objset;
1731e7cbe64fSgw 
1732e7cbe64fSgw 	if (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY))
1733e7cbe64fSgw 		return (EROFS);
1734e7cbe64fSgw 
1735e7cbe64fSgw 	/*
1736e7cbe64fSgw 	 * We do not support swap devices acting as dump devices.
1737e7cbe64fSgw 	 */
1738e7cbe64fSgw 	if (zvol_is_swap(zv))
1739e7cbe64fSgw 		return (ENOTSUP);
1740e7cbe64fSgw 
1741e7cbe64fSgw 	if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE,
1742e7cbe64fSgw 	    8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) {
1743e7cbe64fSgw 		boolean_t resize = (dumpsize > 0) ? B_TRUE : B_FALSE;
1744e7cbe64fSgw 
1745e7cbe64fSgw 		if ((error = zvol_dump_init(zv, resize)) != 0) {
1746e7cbe64fSgw 			(void) zvol_dump_fini(zv);
1747e7cbe64fSgw 			return (error);
1748e7cbe64fSgw 		}
1749e7cbe64fSgw 	}
1750e7cbe64fSgw 
1751e7cbe64fSgw 	/*
1752e7cbe64fSgw 	 * Build up our lba mapping.
1753e7cbe64fSgw 	 */
1754e7cbe64fSgw 	error = zvol_get_lbas(zv);
1755e7cbe64fSgw 	if (error) {
1756e7cbe64fSgw 		(void) zvol_dump_fini(zv);
1757e7cbe64fSgw 		return (error);
1758e7cbe64fSgw 	}
1759e7cbe64fSgw 
1760e7cbe64fSgw 	tx = dmu_tx_create(os);
1761e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1762e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
1763e7cbe64fSgw 	if (error) {
1764e7cbe64fSgw 		dmu_tx_abort(tx);
1765e7cbe64fSgw 		(void) zvol_dump_fini(zv);
1766e7cbe64fSgw 		return (error);
1767e7cbe64fSgw 	}
1768e7cbe64fSgw 
1769e7cbe64fSgw 	zv->zv_flags |= ZVOL_DUMPIFIED;
1770e7cbe64fSgw 	error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1,
1771e7cbe64fSgw 	    &zv->zv_volsize, tx);
1772e7cbe64fSgw 	dmu_tx_commit(tx);
1773e7cbe64fSgw 
1774e7cbe64fSgw 	if (error) {
1775e7cbe64fSgw 		(void) zvol_dump_fini(zv);
1776e7cbe64fSgw 		return (error);
1777e7cbe64fSgw 	}
1778e7cbe64fSgw 
1779e7cbe64fSgw 	txg_wait_synced(dmu_objset_pool(os), 0);
1780e7cbe64fSgw 	return (0);
1781e7cbe64fSgw }
1782e7cbe64fSgw 
1783e7cbe64fSgw static int
1784e7cbe64fSgw zvol_dump_fini(zvol_state_t *zv)
1785e7cbe64fSgw {
1786e7cbe64fSgw 	dmu_tx_t *tx;
1787e7cbe64fSgw 	objset_t *os = zv->zv_objset;
1788e7cbe64fSgw 	nvlist_t *nv;
1789e7cbe64fSgw 	int error = 0;
179088b7b0f2SMatthew Ahrens 	uint64_t checksum, compress, refresrv, vbs;
1791e7cbe64fSgw 
1792b7e50089Smaybee 	/*
1793b7e50089Smaybee 	 * Attempt to restore the zvol back to its pre-dumpified state.
1794b7e50089Smaybee 	 * This is a best-effort attempt as it's possible that not all
1795b7e50089Smaybee 	 * of these properties were initialized during the dumpify process
1796b7e50089Smaybee 	 * (i.e. error during zvol_dump_init).
1797b7e50089Smaybee 	 */
1798b7e50089Smaybee 
1799e7cbe64fSgw 	tx = dmu_tx_create(os);
1800e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1801e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
1802e7cbe64fSgw 	if (error) {
1803e7cbe64fSgw 		dmu_tx_abort(tx);
1804e7cbe64fSgw 		return (error);
1805e7cbe64fSgw 	}
1806b7e50089Smaybee 	(void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx);
1807b7e50089Smaybee 	dmu_tx_commit(tx);
1808e7cbe64fSgw 
1809e7cbe64fSgw 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1810e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum);
1811e7cbe64fSgw 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1812e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress);
1813e7cbe64fSgw 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1814e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv);
181588b7b0f2SMatthew Ahrens 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
181688b7b0f2SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs);
1817e7cbe64fSgw 
1818e7cbe64fSgw 	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1819e7cbe64fSgw 	(void) nvlist_add_uint64(nv,
1820e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum);
1821e7cbe64fSgw 	(void) nvlist_add_uint64(nv,
1822e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress);
1823e7cbe64fSgw 	(void) nvlist_add_uint64(nv,
1824e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv);
182588b7b0f2SMatthew Ahrens 	(void) nvlist_add_uint64(nv,
182688b7b0f2SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), vbs);
1827e7cbe64fSgw 	(void) zfs_set_prop_nvlist(zv->zv_name, nv);
1828e7cbe64fSgw 	nvlist_free(nv);
1829e7cbe64fSgw 
1830b7e50089Smaybee 	zvol_free_extents(zv);
1831b7e50089Smaybee 	zv->zv_flags &= ~ZVOL_DUMPIFIED;
1832b7e50089Smaybee 	(void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END);
1833b7e50089Smaybee 
1834e7cbe64fSgw 	return (0);
1835e7cbe64fSgw }
1836