xref: /illumos-gate/usr/src/uts/common/fs/zfs/zvol.c (revision 94d1a2100edbb6781ea1c047a6334bb3f15640f5)
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 /*
22f80ce222SChris Kirby  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23fa9e4066Sahrens  */
24fa9e4066Sahrens 
25fa9e4066Sahrens /*
26fa9e4066Sahrens  * ZFS volume emulation driver.
27fa9e4066Sahrens  *
28fa9e4066Sahrens  * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes.
29fa9e4066Sahrens  * Volumes are accessed through the symbolic links named:
30fa9e4066Sahrens  *
31fa9e4066Sahrens  * /dev/zvol/dsk/<pool_name>/<dataset_name>
32fa9e4066Sahrens  * /dev/zvol/rdsk/<pool_name>/<dataset_name>
33fa9e4066Sahrens  *
34681d9761SEric Taylor  * These links are created by the /dev filesystem (sdev_zvolops.c).
35fa9e4066Sahrens  * Volumes are persistent through reboot.  No user command needs to be
36fa9e4066Sahrens  * run before opening and using a device.
37fa9e4066Sahrens  */
38fa9e4066Sahrens 
39fa9e4066Sahrens #include <sys/types.h>
40fa9e4066Sahrens #include <sys/param.h>
41fa9e4066Sahrens #include <sys/errno.h>
42fa9e4066Sahrens #include <sys/uio.h>
43fa9e4066Sahrens #include <sys/buf.h>
44fa9e4066Sahrens #include <sys/modctl.h>
45fa9e4066Sahrens #include <sys/open.h>
46fa9e4066Sahrens #include <sys/kmem.h>
47fa9e4066Sahrens #include <sys/conf.h>
48fa9e4066Sahrens #include <sys/cmn_err.h>
49fa9e4066Sahrens #include <sys/stat.h>
50fa9e4066Sahrens #include <sys/zap.h>
51fa9e4066Sahrens #include <sys/spa.h>
52fa9e4066Sahrens #include <sys/zio.h>
53e7cbe64fSgw #include <sys/dmu_traverse.h>
54e7cbe64fSgw #include <sys/dnode.h>
55e7cbe64fSgw #include <sys/dsl_dataset.h>
56fa9e4066Sahrens #include <sys/dsl_prop.h>
57fa9e4066Sahrens #include <sys/dkio.h>
58fa9e4066Sahrens #include <sys/efi_partition.h>
59fa9e4066Sahrens #include <sys/byteorder.h>
60fa9e4066Sahrens #include <sys/pathname.h>
61fa9e4066Sahrens #include <sys/ddi.h>
62fa9e4066Sahrens #include <sys/sunddi.h>
63fa9e4066Sahrens #include <sys/crc32.h>
64fa9e4066Sahrens #include <sys/dirent.h>
65fa9e4066Sahrens #include <sys/policy.h>
66fa9e4066Sahrens #include <sys/fs/zfs.h>
67fa9e4066Sahrens #include <sys/zfs_ioctl.h>
68fa9e4066Sahrens #include <sys/mkdev.h>
6922ac5be4Sperrin #include <sys/zil.h>
70c5c6ffa0Smaybee #include <sys/refcount.h>
71c2e6a7d6Sperrin #include <sys/zfs_znode.h>
72c2e6a7d6Sperrin #include <sys/zfs_rlock.h>
73e7cbe64fSgw #include <sys/vdev_disk.h>
74e7cbe64fSgw #include <sys/vdev_impl.h>
75e7cbe64fSgw #include <sys/zvol.h>
76e7cbe64fSgw #include <sys/dumphdr.h>
771209a471SNeil Perrin #include <sys/zil_impl.h>
78fa9e4066Sahrens 
79fa9e4066Sahrens #include "zfs_namecheck.h"
80fa9e4066Sahrens 
81fa9e4066Sahrens static void *zvol_state;
82503ad85cSMatthew Ahrens static char *zvol_tag = "zvol_tag";
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_open_count[OTYPCNT];	/* open counts */
113fa9e4066Sahrens 	uint32_t	zv_total_opens;	/* total open count */
11422ac5be4Sperrin 	zilog_t		*zv_zilog;	/* ZIL handle */
11588b7b0f2SMatthew Ahrens 	list_t		zv_extents;	/* List of extents for dump */
116c2e6a7d6Sperrin 	znode_t		zv_znode;	/* for range locking */
117*94d1a210STim Haley 	dmu_buf_t	*zv_dbuf;	/* bonus handle */
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 
13392241e0bSTom Erickson extern int zfs_set_prop_nvlist(const char *, zprop_source_t,
13492241e0bSTom Erickson     nvlist_t *, nvlist_t **);
135681d9761SEric Taylor static int zvol_remove_zv(zvol_state_t *);
136feb08c6bSbillm static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio);
137e7cbe64fSgw static int zvol_dumpify(zvol_state_t *zv);
138e7cbe64fSgw static int zvol_dump_fini(zvol_state_t *zv);
139e7cbe64fSgw static int zvol_dump_init(zvol_state_t *zv, boolean_t resize);
14067bd71c6Sperrin 
141fa9e4066Sahrens static void
142681d9761SEric Taylor zvol_size_changed(uint64_t volsize, major_t maj, minor_t min)
143fa9e4066Sahrens {
144681d9761SEric Taylor 	dev_t dev = makedevice(maj, min);
145fa9e4066Sahrens 
146fa9e4066Sahrens 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
147681d9761SEric Taylor 	    "Size", volsize) == DDI_SUCCESS);
148fa9e4066Sahrens 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
149681d9761SEric Taylor 	    "Nblocks", lbtodb(volsize)) == DDI_SUCCESS);
150e7cbe64fSgw 
151e7cbe64fSgw 	/* Notify specfs to invalidate the cached size */
152e7cbe64fSgw 	spec_size_invalidate(dev, VBLK);
153e7cbe64fSgw 	spec_size_invalidate(dev, VCHR);
154fa9e4066Sahrens }
155fa9e4066Sahrens 
156fa9e4066Sahrens int
157e9dbad6fSeschrock zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
158fa9e4066Sahrens {
159e9dbad6fSeschrock 	if (volsize == 0)
160fa9e4066Sahrens 		return (EINVAL);
161fa9e4066Sahrens 
162e9dbad6fSeschrock 	if (volsize % blocksize != 0)
1635c5460e9Seschrock 		return (EINVAL);
1645c5460e9Seschrock 
165fa9e4066Sahrens #ifdef _ILP32
166e9dbad6fSeschrock 	if (volsize - 1 > SPEC_MAXOFFSET_T)
167fa9e4066Sahrens 		return (EOVERFLOW);
168fa9e4066Sahrens #endif
169fa9e4066Sahrens 	return (0);
170fa9e4066Sahrens }
171fa9e4066Sahrens 
172fa9e4066Sahrens int
173e9dbad6fSeschrock zvol_check_volblocksize(uint64_t volblocksize)
174fa9e4066Sahrens {
175e9dbad6fSeschrock 	if (volblocksize < SPA_MINBLOCKSIZE ||
176e9dbad6fSeschrock 	    volblocksize > SPA_MAXBLOCKSIZE ||
177e9dbad6fSeschrock 	    !ISP2(volblocksize))
178fa9e4066Sahrens 		return (EDOM);
179fa9e4066Sahrens 
180fa9e4066Sahrens 	return (0);
181fa9e4066Sahrens }
182fa9e4066Sahrens 
183fa9e4066Sahrens int
184a2eea2e1Sahrens zvol_get_stats(objset_t *os, nvlist_t *nv)
185fa9e4066Sahrens {
186fa9e4066Sahrens 	int error;
187fa9e4066Sahrens 	dmu_object_info_t doi;
188a2eea2e1Sahrens 	uint64_t val;
189fa9e4066Sahrens 
190a2eea2e1Sahrens 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
191fa9e4066Sahrens 	if (error)
192fa9e4066Sahrens 		return (error);
193fa9e4066Sahrens 
194a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
195a2eea2e1Sahrens 
196fa9e4066Sahrens 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
197fa9e4066Sahrens 
198a2eea2e1Sahrens 	if (error == 0) {
199a2eea2e1Sahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
200a2eea2e1Sahrens 		    doi.doi_data_block_size);
201a2eea2e1Sahrens 	}
202fa9e4066Sahrens 
203fa9e4066Sahrens 	return (error);
204fa9e4066Sahrens }
205fa9e4066Sahrens 
206fa9e4066Sahrens /*
207fa9e4066Sahrens  * Find a free minor number.
208fa9e4066Sahrens  */
209fa9e4066Sahrens static minor_t
210fa9e4066Sahrens zvol_minor_alloc(void)
211fa9e4066Sahrens {
212fa9e4066Sahrens 	minor_t minor;
213fa9e4066Sahrens 
214fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&zvol_state_lock));
215fa9e4066Sahrens 
216fa9e4066Sahrens 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++)
217fa9e4066Sahrens 		if (ddi_get_soft_state(zvol_state, minor) == NULL)
218fa9e4066Sahrens 			return (minor);
219fa9e4066Sahrens 
220fa9e4066Sahrens 	return (0);
221fa9e4066Sahrens }
222fa9e4066Sahrens 
223fa9e4066Sahrens static zvol_state_t *
224e9dbad6fSeschrock zvol_minor_lookup(const char *name)
225fa9e4066Sahrens {
226fa9e4066Sahrens 	minor_t minor;
227fa9e4066Sahrens 	zvol_state_t *zv;
228fa9e4066Sahrens 
229fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&zvol_state_lock));
230fa9e4066Sahrens 
231fa9e4066Sahrens 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) {
232fa9e4066Sahrens 		zv = ddi_get_soft_state(zvol_state, minor);
233fa9e4066Sahrens 		if (zv == NULL)
234fa9e4066Sahrens 			continue;
235fa9e4066Sahrens 		if (strcmp(zv->zv_name, name) == 0)
236f80ce222SChris Kirby 			return (zv);
237fa9e4066Sahrens 	}
238fa9e4066Sahrens 
239f80ce222SChris Kirby 	return (NULL);
240fa9e4066Sahrens }
241fa9e4066Sahrens 
242e7cbe64fSgw /* extent mapping arg */
243e7cbe64fSgw struct maparg {
24488b7b0f2SMatthew Ahrens 	zvol_state_t	*ma_zv;
24588b7b0f2SMatthew Ahrens 	uint64_t	ma_blks;
246e7cbe64fSgw };
247e7cbe64fSgw 
248e7cbe64fSgw /*ARGSUSED*/
249e7cbe64fSgw static int
250b24ab676SJeff Bonwick zvol_map_block(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
251b24ab676SJeff Bonwick     const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
252e7cbe64fSgw {
25388b7b0f2SMatthew Ahrens 	struct maparg *ma = arg;
25488b7b0f2SMatthew Ahrens 	zvol_extent_t *ze;
25588b7b0f2SMatthew Ahrens 	int bs = ma->ma_zv->zv_volblocksize;
256e7cbe64fSgw 
25788b7b0f2SMatthew Ahrens 	if (bp == NULL || zb->zb_object != ZVOL_OBJ || zb->zb_level != 0)
25888b7b0f2SMatthew Ahrens 		return (0);
259e7cbe64fSgw 
26088b7b0f2SMatthew Ahrens 	VERIFY3U(ma->ma_blks, ==, zb->zb_blkid);
26188b7b0f2SMatthew Ahrens 	ma->ma_blks++;
262e7cbe64fSgw 
26388b7b0f2SMatthew Ahrens 	/* Abort immediately if we have encountered gang blocks */
26488b7b0f2SMatthew Ahrens 	if (BP_IS_GANG(bp))
26588b7b0f2SMatthew Ahrens 		return (EFRAGS);
266e7cbe64fSgw 
26788b7b0f2SMatthew Ahrens 	/*
26888b7b0f2SMatthew Ahrens 	 * See if the block is at the end of the previous extent.
26988b7b0f2SMatthew Ahrens 	 */
27088b7b0f2SMatthew Ahrens 	ze = list_tail(&ma->ma_zv->zv_extents);
27188b7b0f2SMatthew Ahrens 	if (ze &&
27288b7b0f2SMatthew Ahrens 	    DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) &&
27388b7b0f2SMatthew Ahrens 	    DVA_GET_OFFSET(BP_IDENTITY(bp)) ==
27488b7b0f2SMatthew Ahrens 	    DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) {
27588b7b0f2SMatthew Ahrens 		ze->ze_nblks++;
27688b7b0f2SMatthew Ahrens 		return (0);
277e7cbe64fSgw 	}
278e7cbe64fSgw 
27988b7b0f2SMatthew Ahrens 	dprintf_bp(bp, "%s", "next blkptr:");
280e7cbe64fSgw 
28188b7b0f2SMatthew Ahrens 	/* start a new extent */
28288b7b0f2SMatthew Ahrens 	ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP);
28388b7b0f2SMatthew Ahrens 	ze->ze_dva = bp->blk_dva[0];	/* structure assignment */
28488b7b0f2SMatthew Ahrens 	ze->ze_nblks = 1;
28588b7b0f2SMatthew Ahrens 	list_insert_tail(&ma->ma_zv->zv_extents, ze);
28688b7b0f2SMatthew Ahrens 	return (0);
28788b7b0f2SMatthew Ahrens }
288e7cbe64fSgw 
28988b7b0f2SMatthew Ahrens static void
29088b7b0f2SMatthew Ahrens zvol_free_extents(zvol_state_t *zv)
29188b7b0f2SMatthew Ahrens {
29288b7b0f2SMatthew Ahrens 	zvol_extent_t *ze;
293e7cbe64fSgw 
29488b7b0f2SMatthew Ahrens 	while (ze = list_head(&zv->zv_extents)) {
29588b7b0f2SMatthew Ahrens 		list_remove(&zv->zv_extents, ze);
29688b7b0f2SMatthew Ahrens 		kmem_free(ze, sizeof (zvol_extent_t));
297e7cbe64fSgw 	}
29888b7b0f2SMatthew Ahrens }
299e7cbe64fSgw 
30088b7b0f2SMatthew Ahrens static int
30188b7b0f2SMatthew Ahrens zvol_get_lbas(zvol_state_t *zv)
30288b7b0f2SMatthew Ahrens {
3033adc9019SEric Taylor 	objset_t *os = zv->zv_objset;
30488b7b0f2SMatthew Ahrens 	struct maparg	ma;
30588b7b0f2SMatthew Ahrens 	int		err;
30688b7b0f2SMatthew Ahrens 
30788b7b0f2SMatthew Ahrens 	ma.ma_zv = zv;
30888b7b0f2SMatthew Ahrens 	ma.ma_blks = 0;
30988b7b0f2SMatthew Ahrens 	zvol_free_extents(zv);
31088b7b0f2SMatthew Ahrens 
3113adc9019SEric Taylor 	/* commit any in-flight changes before traversing the dataset */
3123adc9019SEric Taylor 	txg_wait_synced(dmu_objset_pool(os), 0);
3133adc9019SEric Taylor 	err = traverse_dataset(dmu_objset_ds(os), 0,
31488b7b0f2SMatthew Ahrens 	    TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma);
31588b7b0f2SMatthew Ahrens 	if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) {
31688b7b0f2SMatthew Ahrens 		zvol_free_extents(zv);
31788b7b0f2SMatthew Ahrens 		return (err ? err : EIO);
318e7cbe64fSgw 	}
31988b7b0f2SMatthew Ahrens 
320e7cbe64fSgw 	return (0);
321e7cbe64fSgw }
322e7cbe64fSgw 
323ecd6cf80Smarks /* ARGSUSED */
324fa9e4066Sahrens void
325ecd6cf80Smarks zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
326fa9e4066Sahrens {
327da6c28aaSamw 	zfs_creat_t *zct = arg;
328da6c28aaSamw 	nvlist_t *nvprops = zct->zct_props;
329fa9e4066Sahrens 	int error;
330e9dbad6fSeschrock 	uint64_t volblocksize, volsize;
331fa9e4066Sahrens 
332ecd6cf80Smarks 	VERIFY(nvlist_lookup_uint64(nvprops,
333e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
334ecd6cf80Smarks 	if (nvlist_lookup_uint64(nvprops,
335e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
336e9dbad6fSeschrock 		volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
337e9dbad6fSeschrock 
338e9dbad6fSeschrock 	/*
339e7cbe64fSgw 	 * These properties must be removed from the list so the generic
340e9dbad6fSeschrock 	 * property setting step won't apply to them.
341e9dbad6fSeschrock 	 */
342ecd6cf80Smarks 	VERIFY(nvlist_remove_all(nvprops,
343e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
344ecd6cf80Smarks 	(void) nvlist_remove_all(nvprops,
345e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
346e9dbad6fSeschrock 
347e9dbad6fSeschrock 	error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
348fa9e4066Sahrens 	    DMU_OT_NONE, 0, tx);
349fa9e4066Sahrens 	ASSERT(error == 0);
350fa9e4066Sahrens 
351fa9e4066Sahrens 	error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
352fa9e4066Sahrens 	    DMU_OT_NONE, 0, tx);
353fa9e4066Sahrens 	ASSERT(error == 0);
354fa9e4066Sahrens 
355e9dbad6fSeschrock 	error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
356fa9e4066Sahrens 	ASSERT(error == 0);
357fa9e4066Sahrens }
358fa9e4066Sahrens 
35922ac5be4Sperrin /*
36022ac5be4Sperrin  * Replay a TX_WRITE ZIL transaction that didn't get committed
36122ac5be4Sperrin  * after a system failure
36222ac5be4Sperrin  */
36322ac5be4Sperrin static int
36422ac5be4Sperrin zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
36522ac5be4Sperrin {
36622ac5be4Sperrin 	objset_t *os = zv->zv_objset;
36722ac5be4Sperrin 	char *data = (char *)(lr + 1);	/* data follows lr_write_t */
368b24ab676SJeff Bonwick 	uint64_t offset, length;
36922ac5be4Sperrin 	dmu_tx_t *tx;
37022ac5be4Sperrin 	int error;
37122ac5be4Sperrin 
37222ac5be4Sperrin 	if (byteswap)
37322ac5be4Sperrin 		byteswap_uint64_array(lr, sizeof (*lr));
37422ac5be4Sperrin 
375b24ab676SJeff Bonwick 	offset = lr->lr_offset;
376b24ab676SJeff Bonwick 	length = lr->lr_length;
377b24ab676SJeff Bonwick 
378b24ab676SJeff Bonwick 	/* If it's a dmu_sync() block, write the whole block */
379b24ab676SJeff Bonwick 	if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
380b24ab676SJeff Bonwick 		uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
381b24ab676SJeff Bonwick 		if (length < blocksize) {
382b24ab676SJeff Bonwick 			offset -= offset % blocksize;
383b24ab676SJeff Bonwick 			length = blocksize;
384b24ab676SJeff Bonwick 		}
385b24ab676SJeff Bonwick 	}
386975c32a0SNeil Perrin 
38722ac5be4Sperrin 	tx = dmu_tx_create(os);
388b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length);
3891209a471SNeil Perrin 	error = dmu_tx_assign(tx, TXG_WAIT);
39022ac5be4Sperrin 	if (error) {
39122ac5be4Sperrin 		dmu_tx_abort(tx);
39222ac5be4Sperrin 	} else {
393b24ab676SJeff Bonwick 		dmu_write(os, ZVOL_OBJ, offset, length, data, tx);
39422ac5be4Sperrin 		dmu_tx_commit(tx);
39522ac5be4Sperrin 	}
39622ac5be4Sperrin 
39722ac5be4Sperrin 	return (error);
39822ac5be4Sperrin }
39922ac5be4Sperrin 
40022ac5be4Sperrin /* ARGSUSED */
40122ac5be4Sperrin static int
40222ac5be4Sperrin zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
40322ac5be4Sperrin {
40422ac5be4Sperrin 	return (ENOTSUP);
40522ac5be4Sperrin }
40622ac5be4Sperrin 
40722ac5be4Sperrin /*
40822ac5be4Sperrin  * Callback vectors for replaying records.
40922ac5be4Sperrin  * Only TX_WRITE is needed for zvol.
41022ac5be4Sperrin  */
41122ac5be4Sperrin zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = {
41222ac5be4Sperrin 	zvol_replay_err,	/* 0 no such transaction type */
41322ac5be4Sperrin 	zvol_replay_err,	/* TX_CREATE */
41422ac5be4Sperrin 	zvol_replay_err,	/* TX_MKDIR */
41522ac5be4Sperrin 	zvol_replay_err,	/* TX_MKXATTR */
41622ac5be4Sperrin 	zvol_replay_err,	/* TX_SYMLINK */
41722ac5be4Sperrin 	zvol_replay_err,	/* TX_REMOVE */
41822ac5be4Sperrin 	zvol_replay_err,	/* TX_RMDIR */
41922ac5be4Sperrin 	zvol_replay_err,	/* TX_LINK */
42022ac5be4Sperrin 	zvol_replay_err,	/* TX_RENAME */
42122ac5be4Sperrin 	zvol_replay_write,	/* TX_WRITE */
42222ac5be4Sperrin 	zvol_replay_err,	/* TX_TRUNCATE */
42322ac5be4Sperrin 	zvol_replay_err,	/* TX_SETATTR */
42422ac5be4Sperrin 	zvol_replay_err,	/* TX_ACL */
425975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_CREATE_ACL */
426975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_CREATE_ATTR */
427975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_CREATE_ACL_ATTR */
428975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_MKDIR_ACL */
429975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_MKDIR_ATTR */
430975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_MKDIR_ACL_ATTR */
431975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_WRITE2 */
43222ac5be4Sperrin };
43322ac5be4Sperrin 
434681d9761SEric Taylor int
435681d9761SEric Taylor zvol_name2minor(const char *name, minor_t *minor)
436681d9761SEric Taylor {
437681d9761SEric Taylor 	zvol_state_t *zv;
438681d9761SEric Taylor 
439681d9761SEric Taylor 	mutex_enter(&zvol_state_lock);
440681d9761SEric Taylor 	zv = zvol_minor_lookup(name);
441681d9761SEric Taylor 	if (minor && zv)
442681d9761SEric Taylor 		*minor = zv->zv_minor;
443681d9761SEric Taylor 	mutex_exit(&zvol_state_lock);
444681d9761SEric Taylor 	return (zv ? 0 : -1);
445681d9761SEric Taylor }
446681d9761SEric Taylor 
447e7cbe64fSgw /*
448e7cbe64fSgw  * Create a minor node (plus a whole lot more) for the specified volume.
449fa9e4066Sahrens  */
450fa9e4066Sahrens int
451681d9761SEric Taylor zvol_create_minor(const char *name)
452fa9e4066Sahrens {
453fa9e4066Sahrens 	zvol_state_t *zv;
454fa9e4066Sahrens 	objset_t *os;
45567bd71c6Sperrin 	dmu_object_info_t doi;
456fa9e4066Sahrens 	minor_t minor = 0;
457fa9e4066Sahrens 	char chrbuf[30], blkbuf[30];
458fa9e4066Sahrens 	int error;
459fa9e4066Sahrens 
460fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
461fa9e4066Sahrens 
4621195e687SMark J Musante 	if (zvol_minor_lookup(name) != NULL) {
463fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
464fa9e4066Sahrens 		return (EEXIST);
465fa9e4066Sahrens 	}
466fa9e4066Sahrens 
467503ad85cSMatthew Ahrens 	/* lie and say we're read-only */
468503ad85cSMatthew Ahrens 	error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, zvol_tag, &os);
469fa9e4066Sahrens 
470fa9e4066Sahrens 	if (error) {
471fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
472fa9e4066Sahrens 		return (error);
473fa9e4066Sahrens 	}
474fa9e4066Sahrens 
475681d9761SEric Taylor 	if ((minor = zvol_minor_alloc()) == 0) {
476503ad85cSMatthew Ahrens 		dmu_objset_disown(os, zvol_tag);
477fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
478fa9e4066Sahrens 		return (ENXIO);
479fa9e4066Sahrens 	}
480fa9e4066Sahrens 
481fa9e4066Sahrens 	if (ddi_soft_state_zalloc(zvol_state, minor) != DDI_SUCCESS) {
482503ad85cSMatthew Ahrens 		dmu_objset_disown(os, zvol_tag);
483fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
484fa9e4066Sahrens 		return (EAGAIN);
485fa9e4066Sahrens 	}
486e9dbad6fSeschrock 	(void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME,
487e9dbad6fSeschrock 	    (char *)name);
488fa9e4066Sahrens 
489681d9761SEric Taylor 	(void) snprintf(chrbuf, sizeof (chrbuf), "%u,raw", minor);
490fa9e4066Sahrens 
491fa9e4066Sahrens 	if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR,
492fa9e4066Sahrens 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
493fa9e4066Sahrens 		ddi_soft_state_free(zvol_state, minor);
494503ad85cSMatthew Ahrens 		dmu_objset_disown(os, zvol_tag);
495fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
496fa9e4066Sahrens 		return (EAGAIN);
497fa9e4066Sahrens 	}
498fa9e4066Sahrens 
499681d9761SEric Taylor 	(void) snprintf(blkbuf, sizeof (blkbuf), "%u", minor);
500fa9e4066Sahrens 
501fa9e4066Sahrens 	if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK,
502fa9e4066Sahrens 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
503fa9e4066Sahrens 		ddi_remove_minor_node(zfs_dip, chrbuf);
504fa9e4066Sahrens 		ddi_soft_state_free(zvol_state, minor);
505503ad85cSMatthew Ahrens 		dmu_objset_disown(os, zvol_tag);
506fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
507fa9e4066Sahrens 		return (EAGAIN);
508fa9e4066Sahrens 	}
509fa9e4066Sahrens 
510fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
511fa9e4066Sahrens 
512681d9761SEric Taylor 	(void) strlcpy(zv->zv_name, name, MAXPATHLEN);
513fa9e4066Sahrens 	zv->zv_min_bs = DEV_BSHIFT;
514fa9e4066Sahrens 	zv->zv_minor = minor;
515fa9e4066Sahrens 	zv->zv_objset = os;
516681d9761SEric Taylor 	if (dmu_objset_is_snapshot(os))
517681d9761SEric Taylor 		zv->zv_flags |= ZVOL_RDONLY;
518c2e6a7d6Sperrin 	mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
519c2e6a7d6Sperrin 	avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
520c2e6a7d6Sperrin 	    sizeof (rl_t), offsetof(rl_t, r_node));
52188b7b0f2SMatthew Ahrens 	list_create(&zv->zv_extents, sizeof (zvol_extent_t),
52288b7b0f2SMatthew Ahrens 	    offsetof(zvol_extent_t, ze_node));
52367bd71c6Sperrin 	/* get and cache the blocksize */
52467bd71c6Sperrin 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
52567bd71c6Sperrin 	ASSERT(error == 0);
52667bd71c6Sperrin 	zv->zv_volblocksize = doi.doi_data_block_size;
52722ac5be4Sperrin 
5281209a471SNeil Perrin 	zil_replay(os, zv, zvol_replay_vector);
529681d9761SEric Taylor 	dmu_objset_disown(os, zvol_tag);
530681d9761SEric Taylor 	zv->zv_objset = NULL;
531fa9e4066Sahrens 
532fa9e4066Sahrens 	zvol_minors++;
533fa9e4066Sahrens 
534fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
535fa9e4066Sahrens 
536fa9e4066Sahrens 	return (0);
537fa9e4066Sahrens }
538fa9e4066Sahrens 
539fa9e4066Sahrens /*
540fa9e4066Sahrens  * Remove minor node for the specified volume.
541fa9e4066Sahrens  */
542681d9761SEric Taylor static int
543681d9761SEric Taylor zvol_remove_zv(zvol_state_t *zv)
544681d9761SEric Taylor {
545681d9761SEric Taylor 	char nmbuf[20];
546681d9761SEric Taylor 
547681d9761SEric Taylor 	ASSERT(MUTEX_HELD(&zvol_state_lock));
548681d9761SEric Taylor 	if (zv->zv_total_opens != 0)
549681d9761SEric Taylor 		return (EBUSY);
550681d9761SEric Taylor 
551681d9761SEric Taylor 	(void) snprintf(nmbuf, sizeof (nmbuf), "%u,raw", zv->zv_minor);
552681d9761SEric Taylor 	ddi_remove_minor_node(zfs_dip, nmbuf);
553681d9761SEric Taylor 
554681d9761SEric Taylor 	(void) snprintf(nmbuf, sizeof (nmbuf), "%u", zv->zv_minor);
555681d9761SEric Taylor 	ddi_remove_minor_node(zfs_dip, nmbuf);
556681d9761SEric Taylor 
557681d9761SEric Taylor 	avl_destroy(&zv->zv_znode.z_range_avl);
558681d9761SEric Taylor 	mutex_destroy(&zv->zv_znode.z_range_lock);
559681d9761SEric Taylor 
560681d9761SEric Taylor 	ddi_soft_state_free(zvol_state, zv->zv_minor);
561681d9761SEric Taylor 
562681d9761SEric Taylor 	zvol_minors--;
563681d9761SEric Taylor 	return (0);
564681d9761SEric Taylor }
565681d9761SEric Taylor 
566fa9e4066Sahrens int
567e9dbad6fSeschrock zvol_remove_minor(const char *name)
568fa9e4066Sahrens {
569fa9e4066Sahrens 	zvol_state_t *zv;
570681d9761SEric Taylor 	int rc;
571fa9e4066Sahrens 
572fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
573e9dbad6fSeschrock 	if ((zv = zvol_minor_lookup(name)) == NULL) {
574fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
575fa9e4066Sahrens 		return (ENXIO);
576fa9e4066Sahrens 	}
577681d9761SEric Taylor 	rc = zvol_remove_zv(zv);
578681d9761SEric Taylor 	mutex_exit(&zvol_state_lock);
579681d9761SEric Taylor 	return (rc);
580681d9761SEric Taylor }
581fa9e4066Sahrens 
582681d9761SEric Taylor int
583681d9761SEric Taylor zvol_first_open(zvol_state_t *zv)
584681d9761SEric Taylor {
585681d9761SEric Taylor 	objset_t *os;
586681d9761SEric Taylor 	uint64_t volsize;
587681d9761SEric Taylor 	int error;
588681d9761SEric Taylor 	uint64_t readonly;
589fa9e4066Sahrens 
590681d9761SEric Taylor 	/* lie and say we're read-only */
591681d9761SEric Taylor 	error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, B_TRUE,
592681d9761SEric Taylor 	    zvol_tag, &os);
593681d9761SEric Taylor 	if (error)
594681d9761SEric Taylor 		return (error);
595fa9e4066Sahrens 
596681d9761SEric Taylor 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
597681d9761SEric Taylor 	if (error) {
598681d9761SEric Taylor 		ASSERT(error == 0);
599681d9761SEric Taylor 		dmu_objset_disown(os, zvol_tag);
600681d9761SEric Taylor 		return (error);
601681d9761SEric Taylor 	}
602681d9761SEric Taylor 	zv->zv_objset = os;
603*94d1a210STim Haley 	error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf);
604*94d1a210STim Haley 	if (error) {
605*94d1a210STim Haley 		dmu_objset_disown(os, zvol_tag);
606*94d1a210STim Haley 		return (error);
607*94d1a210STim Haley 	}
608681d9761SEric Taylor 	zv->zv_volsize = volsize;
609681d9761SEric Taylor 	zv->zv_zilog = zil_open(os, zvol_get_data);
610681d9761SEric Taylor 	zvol_size_changed(zv->zv_volsize, ddi_driver_major(zfs_dip),
611681d9761SEric Taylor 	    zv->zv_minor);
612fa9e4066Sahrens 
613681d9761SEric Taylor 	VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &readonly,
614681d9761SEric Taylor 	    NULL) == 0);
615681d9761SEric Taylor 	if (readonly || dmu_objset_is_snapshot(os))
616681d9761SEric Taylor 		zv->zv_flags |= ZVOL_RDONLY;
617681d9761SEric Taylor 	else
618681d9761SEric Taylor 		zv->zv_flags &= ~ZVOL_RDONLY;
619681d9761SEric Taylor 	return (error);
620681d9761SEric Taylor }
621fa9e4066Sahrens 
622681d9761SEric Taylor void
623681d9761SEric Taylor zvol_last_close(zvol_state_t *zv)
624681d9761SEric Taylor {
62522ac5be4Sperrin 	zil_close(zv->zv_zilog);
62622ac5be4Sperrin 	zv->zv_zilog = NULL;
627*94d1a210STim Haley 	dmu_buf_rele(zv->zv_dbuf, zvol_tag);
628*94d1a210STim Haley 	zv->zv_dbuf = NULL;
629503ad85cSMatthew Ahrens 	dmu_objset_disown(zv->zv_objset, zvol_tag);
630fa9e4066Sahrens 	zv->zv_objset = NULL;
631fa9e4066Sahrens }
632fa9e4066Sahrens 
633e7cbe64fSgw int
634e7cbe64fSgw zvol_prealloc(zvol_state_t *zv)
635e7cbe64fSgw {
636e7cbe64fSgw 	objset_t *os = zv->zv_objset;
637e7cbe64fSgw 	dmu_tx_t *tx;
638e7cbe64fSgw 	uint64_t refd, avail, usedobjs, availobjs;
639e7cbe64fSgw 	uint64_t resid = zv->zv_volsize;
640e7cbe64fSgw 	uint64_t off = 0;
641e7cbe64fSgw 
642e7cbe64fSgw 	/* Check the space usage before attempting to allocate the space */
643e7cbe64fSgw 	dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs);
644e7cbe64fSgw 	if (avail < zv->zv_volsize)
645e7cbe64fSgw 		return (ENOSPC);
646e7cbe64fSgw 
647e7cbe64fSgw 	/* Free old extents if they exist */
648e7cbe64fSgw 	zvol_free_extents(zv);
649e7cbe64fSgw 
650e7cbe64fSgw 	while (resid != 0) {
651e7cbe64fSgw 		int error;
652e7cbe64fSgw 		uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE);
653e7cbe64fSgw 
654e7cbe64fSgw 		tx = dmu_tx_create(os);
655e7cbe64fSgw 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
656e7cbe64fSgw 		error = dmu_tx_assign(tx, TXG_WAIT);
657e7cbe64fSgw 		if (error) {
658e7cbe64fSgw 			dmu_tx_abort(tx);
659cdb0ab79Smaybee 			(void) dmu_free_long_range(os, ZVOL_OBJ, 0, off);
660e7cbe64fSgw 			return (error);
661e7cbe64fSgw 		}
66282c9918fSTim Haley 		dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx);
663e7cbe64fSgw 		dmu_tx_commit(tx);
664e7cbe64fSgw 		off += bytes;
665e7cbe64fSgw 		resid -= bytes;
666e7cbe64fSgw 	}
667e7cbe64fSgw 	txg_wait_synced(dmu_objset_pool(os), 0);
668e7cbe64fSgw 
669e7cbe64fSgw 	return (0);
670e7cbe64fSgw }
671e7cbe64fSgw 
672e7cbe64fSgw int
673681d9761SEric Taylor zvol_update_volsize(objset_t *os, uint64_t volsize)
674e7cbe64fSgw {
675e7cbe64fSgw 	dmu_tx_t *tx;
676e7cbe64fSgw 	int error;
677e7cbe64fSgw 
678e7cbe64fSgw 	ASSERT(MUTEX_HELD(&zvol_state_lock));
679e7cbe64fSgw 
680681d9761SEric Taylor 	tx = dmu_tx_create(os);
681e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
682e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
683e7cbe64fSgw 	if (error) {
684e7cbe64fSgw 		dmu_tx_abort(tx);
685e7cbe64fSgw 		return (error);
686e7cbe64fSgw 	}
687e7cbe64fSgw 
688681d9761SEric Taylor 	error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
689e7cbe64fSgw 	    &volsize, tx);
690e7cbe64fSgw 	dmu_tx_commit(tx);
691e7cbe64fSgw 
692e7cbe64fSgw 	if (error == 0)
693681d9761SEric Taylor 		error = dmu_free_long_range(os,
694cdb0ab79Smaybee 		    ZVOL_OBJ, volsize, DMU_OBJECT_END);
695681d9761SEric Taylor 	return (error);
696681d9761SEric Taylor }
697e7cbe64fSgw 
698681d9761SEric Taylor void
699681d9761SEric Taylor zvol_remove_minors(const char *name)
700681d9761SEric Taylor {
701681d9761SEric Taylor 	zvol_state_t *zv;
702681d9761SEric Taylor 	char *namebuf;
703681d9761SEric Taylor 	minor_t minor;
704681d9761SEric Taylor 
705681d9761SEric Taylor 	namebuf = kmem_zalloc(strlen(name) + 2, KM_SLEEP);
706681d9761SEric Taylor 	(void) strncpy(namebuf, name, strlen(name));
707681d9761SEric Taylor 	(void) strcat(namebuf, "/");
708681d9761SEric Taylor 	mutex_enter(&zvol_state_lock);
709681d9761SEric Taylor 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) {
710681d9761SEric Taylor 
711681d9761SEric Taylor 		zv = ddi_get_soft_state(zvol_state, minor);
712681d9761SEric Taylor 		if (zv == NULL)
713681d9761SEric Taylor 			continue;
714681d9761SEric Taylor 		if (strncmp(namebuf, zv->zv_name, strlen(namebuf)) == 0)
715681d9761SEric Taylor 			(void) zvol_remove_zv(zv);
716e7cbe64fSgw 	}
717681d9761SEric Taylor 	kmem_free(namebuf, strlen(name) + 2);
718681d9761SEric Taylor 
719681d9761SEric Taylor 	mutex_exit(&zvol_state_lock);
720e7cbe64fSgw }
721e7cbe64fSgw 
722fa9e4066Sahrens int
72391ebeef5Sahrens zvol_set_volsize(const char *name, major_t maj, uint64_t volsize)
724fa9e4066Sahrens {
725681d9761SEric Taylor 	zvol_state_t *zv = NULL;
726681d9761SEric Taylor 	objset_t *os;
727fa9e4066Sahrens 	int error;
7285c5460e9Seschrock 	dmu_object_info_t doi;
729e7cbe64fSgw 	uint64_t old_volsize = 0ULL;
730681d9761SEric Taylor 	uint64_t readonly;
731fa9e4066Sahrens 
732fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
733681d9761SEric Taylor 	zv = zvol_minor_lookup(name);
734681d9761SEric Taylor 	if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) {
735681d9761SEric Taylor 		mutex_exit(&zvol_state_lock);
736681d9761SEric Taylor 		return (error);
737fa9e4066Sahrens 	}
738fa9e4066Sahrens 
739681d9761SEric Taylor 	if ((error = dmu_object_info(os, ZVOL_OBJ, &doi)) != 0 ||
740e9dbad6fSeschrock 	    (error = zvol_check_volsize(volsize,
741bb0ade09Sahrens 	    doi.doi_data_block_size)) != 0)
742bb0ade09Sahrens 		goto out;
7435c5460e9Seschrock 
744681d9761SEric Taylor 	VERIFY(dsl_prop_get_integer(name, "readonly", &readonly,
745681d9761SEric Taylor 	    NULL) == 0);
746681d9761SEric Taylor 	if (readonly) {
747bb0ade09Sahrens 		error = EROFS;
748bb0ade09Sahrens 		goto out;
749fa9e4066Sahrens 	}
750fa9e4066Sahrens 
751681d9761SEric Taylor 	error = zvol_update_volsize(os, volsize);
752e7cbe64fSgw 	/*
753e7cbe64fSgw 	 * Reinitialize the dump area to the new size. If we
754681d9761SEric Taylor 	 * failed to resize the dump area then restore it back to
755681d9761SEric Taylor 	 * its original size.
756e7cbe64fSgw 	 */
757681d9761SEric Taylor 	if (zv && error == 0) {
758681d9761SEric Taylor 		if (zv->zv_flags & ZVOL_DUMPIFIED) {
759681d9761SEric Taylor 			old_volsize = zv->zv_volsize;
760681d9761SEric Taylor 			zv->zv_volsize = volsize;
761681d9761SEric Taylor 			if ((error = zvol_dumpify(zv)) != 0 ||
762681d9761SEric Taylor 			    (error = dumpvp_resize()) != 0) {
763681d9761SEric Taylor 				(void) zvol_update_volsize(os, old_volsize);
764681d9761SEric Taylor 				zv->zv_volsize = old_volsize;
765681d9761SEric Taylor 				error = zvol_dumpify(zv);
766681d9761SEric Taylor 			}
767681d9761SEric Taylor 		}
768681d9761SEric Taylor 		if (error == 0) {
769681d9761SEric Taylor 			zv->zv_volsize = volsize;
770681d9761SEric Taylor 			zvol_size_changed(volsize, maj, zv->zv_minor);
771e7cbe64fSgw 		}
772fa9e4066Sahrens 	}
773fa9e4066Sahrens 
774573ca77eSGeorge Wilson 	/*
775573ca77eSGeorge Wilson 	 * Generate a LUN expansion event.
776573ca77eSGeorge Wilson 	 */
777681d9761SEric Taylor 	if (zv && error == 0) {
778573ca77eSGeorge Wilson 		sysevent_id_t eid;
779573ca77eSGeorge Wilson 		nvlist_t *attr;
780573ca77eSGeorge Wilson 		char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
781573ca77eSGeorge Wilson 
782681d9761SEric Taylor 		(void) snprintf(physpath, MAXPATHLEN, "%s%u", ZVOL_PSEUDO_DEV,
783573ca77eSGeorge Wilson 		    zv->zv_minor);
784573ca77eSGeorge Wilson 
785573ca77eSGeorge Wilson 		VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
786573ca77eSGeorge Wilson 		VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
787573ca77eSGeorge Wilson 
788573ca77eSGeorge Wilson 		(void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
789573ca77eSGeorge Wilson 		    ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
790573ca77eSGeorge Wilson 
791573ca77eSGeorge Wilson 		nvlist_free(attr);
792573ca77eSGeorge Wilson 		kmem_free(physpath, MAXPATHLEN);
793573ca77eSGeorge Wilson 	}
794573ca77eSGeorge Wilson 
795bb0ade09Sahrens out:
796681d9761SEric Taylor 	dmu_objset_rele(os, FTAG);
797bb0ade09Sahrens 
798fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
799fa9e4066Sahrens 
800fa9e4066Sahrens 	return (error);
801fa9e4066Sahrens }
802fa9e4066Sahrens 
803fa9e4066Sahrens /*ARGSUSED*/
804fa9e4066Sahrens int
805fa9e4066Sahrens zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr)
806fa9e4066Sahrens {
807fa9e4066Sahrens 	minor_t minor = getminor(*devp);
808fa9e4066Sahrens 	zvol_state_t *zv;
809681d9761SEric Taylor 	int err = 0;
810fa9e4066Sahrens 
811fa9e4066Sahrens 	if (minor == 0)			/* This is the control device */
812fa9e4066Sahrens 		return (0);
813fa9e4066Sahrens 
814fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
815fa9e4066Sahrens 
816fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
817fa9e4066Sahrens 	if (zv == NULL) {
818fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
819fa9e4066Sahrens 		return (ENXIO);
820fa9e4066Sahrens 	}
821fa9e4066Sahrens 
822681d9761SEric Taylor 	if (zv->zv_total_opens == 0)
823681d9761SEric Taylor 		err = zvol_first_open(zv);
824681d9761SEric Taylor 	if (err) {
825fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
826681d9761SEric Taylor 		return (err);
827681d9761SEric Taylor 	}
828681d9761SEric Taylor 	if ((flag & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
829681d9761SEric Taylor 		err = EROFS;
830681d9761SEric Taylor 		goto out;
831fa9e4066Sahrens 	}
832c7f714e2SEric Taylor 	if (zv->zv_flags & ZVOL_EXCL) {
833681d9761SEric Taylor 		err = EBUSY;
834681d9761SEric Taylor 		goto out;
835c7f714e2SEric Taylor 	}
836c7f714e2SEric Taylor 	if (flag & FEXCL) {
837c7f714e2SEric Taylor 		if (zv->zv_total_opens != 0) {
838681d9761SEric Taylor 			err = EBUSY;
839681d9761SEric Taylor 			goto out;
840c7f714e2SEric Taylor 		}
841c7f714e2SEric Taylor 		zv->zv_flags |= ZVOL_EXCL;
842c7f714e2SEric Taylor 	}
843fa9e4066Sahrens 
844fa9e4066Sahrens 	if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) {
845fa9e4066Sahrens 		zv->zv_open_count[otyp]++;
846fa9e4066Sahrens 		zv->zv_total_opens++;
847fa9e4066Sahrens 	}
848fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
849fa9e4066Sahrens 
850681d9761SEric Taylor 	return (err);
851681d9761SEric Taylor out:
852681d9761SEric Taylor 	if (zv->zv_total_opens == 0)
853681d9761SEric Taylor 		zvol_last_close(zv);
854681d9761SEric Taylor 	mutex_exit(&zvol_state_lock);
855681d9761SEric Taylor 	return (err);
856fa9e4066Sahrens }
857fa9e4066Sahrens 
858fa9e4066Sahrens /*ARGSUSED*/
859fa9e4066Sahrens int
860fa9e4066Sahrens zvol_close(dev_t dev, int flag, int otyp, cred_t *cr)
861fa9e4066Sahrens {
862fa9e4066Sahrens 	minor_t minor = getminor(dev);
863fa9e4066Sahrens 	zvol_state_t *zv;
864681d9761SEric Taylor 	int error = 0;
865fa9e4066Sahrens 
866fa9e4066Sahrens 	if (minor == 0)		/* This is the control device */
867fa9e4066Sahrens 		return (0);
868fa9e4066Sahrens 
869fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
870fa9e4066Sahrens 
871fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
872fa9e4066Sahrens 	if (zv == NULL) {
873fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
874fa9e4066Sahrens 		return (ENXIO);
875fa9e4066Sahrens 	}
876fa9e4066Sahrens 
877c7f714e2SEric Taylor 	if (zv->zv_flags & ZVOL_EXCL) {
878c7f714e2SEric Taylor 		ASSERT(zv->zv_total_opens == 1);
879c7f714e2SEric Taylor 		zv->zv_flags &= ~ZVOL_EXCL;
880fa9e4066Sahrens 	}
881fa9e4066Sahrens 
882fa9e4066Sahrens 	/*
883fa9e4066Sahrens 	 * If the open count is zero, this is a spurious close.
884fa9e4066Sahrens 	 * That indicates a bug in the kernel / DDI framework.
885fa9e4066Sahrens 	 */
886fa9e4066Sahrens 	ASSERT(zv->zv_open_count[otyp] != 0);
887fa9e4066Sahrens 	ASSERT(zv->zv_total_opens != 0);
888fa9e4066Sahrens 
889fa9e4066Sahrens 	/*
890fa9e4066Sahrens 	 * You may get multiple opens, but only one close.
891fa9e4066Sahrens 	 */
892fa9e4066Sahrens 	zv->zv_open_count[otyp]--;
893fa9e4066Sahrens 	zv->zv_total_opens--;
894fa9e4066Sahrens 
895681d9761SEric Taylor 	if (zv->zv_total_opens == 0)
896681d9761SEric Taylor 		zvol_last_close(zv);
897fa9e4066Sahrens 
898681d9761SEric Taylor 	mutex_exit(&zvol_state_lock);
899681d9761SEric Taylor 	return (error);
900fa9e4066Sahrens }
901fa9e4066Sahrens 
902feb08c6bSbillm static void
903b24ab676SJeff Bonwick zvol_get_done(zgd_t *zgd, int error)
90467bd71c6Sperrin {
905b24ab676SJeff Bonwick 	if (zgd->zgd_db)
906b24ab676SJeff Bonwick 		dmu_buf_rele(zgd->zgd_db, zgd);
907b24ab676SJeff Bonwick 
908b24ab676SJeff Bonwick 	zfs_range_unlock(zgd->zgd_rl);
909b24ab676SJeff Bonwick 
910b24ab676SJeff Bonwick 	if (error == 0 && zgd->zgd_bp)
911b24ab676SJeff Bonwick 		zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
91267bd71c6Sperrin 
91367bd71c6Sperrin 	kmem_free(zgd, sizeof (zgd_t));
91467bd71c6Sperrin }
91567bd71c6Sperrin 
91667bd71c6Sperrin /*
91767bd71c6Sperrin  * Get data to generate a TX_WRITE intent log record.
91867bd71c6Sperrin  */
919feb08c6bSbillm static int
92067bd71c6Sperrin zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
92167bd71c6Sperrin {
92267bd71c6Sperrin 	zvol_state_t *zv = arg;
92367bd71c6Sperrin 	objset_t *os = zv->zv_objset;
924b24ab676SJeff Bonwick 	uint64_t object = ZVOL_OBJ;
925b24ab676SJeff Bonwick 	uint64_t offset = lr->lr_offset;
926b24ab676SJeff Bonwick 	uint64_t size = lr->lr_length;	/* length of user data */
927b24ab676SJeff Bonwick 	blkptr_t *bp = &lr->lr_blkptr;
92867bd71c6Sperrin 	dmu_buf_t *db;
92967bd71c6Sperrin 	zgd_t *zgd;
93067bd71c6Sperrin 	int error;
93167bd71c6Sperrin 
932b24ab676SJeff Bonwick 	ASSERT(zio != NULL);
933b24ab676SJeff Bonwick 	ASSERT(size != 0);
934b24ab676SJeff Bonwick 
935b24ab676SJeff Bonwick 	zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
936b24ab676SJeff Bonwick 	zgd->zgd_zilog = zv->zv_zilog;
937b24ab676SJeff Bonwick 	zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
938feb08c6bSbillm 
939c2e6a7d6Sperrin 	/*
940c2e6a7d6Sperrin 	 * Write records come in two flavors: immediate and indirect.
941c2e6a7d6Sperrin 	 * For small writes it's cheaper to store the data with the
942c2e6a7d6Sperrin 	 * log record (immediate); for large writes it's cheaper to
943c2e6a7d6Sperrin 	 * sync the data and get a pointer to it (indirect) so that
944c2e6a7d6Sperrin 	 * we don't have to write the data twice.
945c2e6a7d6Sperrin 	 */
946b24ab676SJeff Bonwick 	if (buf != NULL) {	/* immediate write */
947b24ab676SJeff Bonwick 		error = dmu_read(os, object, offset, size, buf,
948b24ab676SJeff Bonwick 		    DMU_READ_NO_PREFETCH);
949b24ab676SJeff Bonwick 	} else {
950b24ab676SJeff Bonwick 		size = zv->zv_volblocksize;
951b24ab676SJeff Bonwick 		offset = P2ALIGN(offset, size);
952b24ab676SJeff Bonwick 		error = dmu_buf_hold(os, object, offset, zgd, &db);
953b24ab676SJeff Bonwick 		if (error == 0) {
954b24ab676SJeff Bonwick 			zgd->zgd_db = db;
955b24ab676SJeff Bonwick 			zgd->zgd_bp = bp;
95667bd71c6Sperrin 
957b24ab676SJeff Bonwick 			ASSERT(db->db_offset == offset);
958b24ab676SJeff Bonwick 			ASSERT(db->db_size == size);
95967bd71c6Sperrin 
960b24ab676SJeff Bonwick 			error = dmu_sync(zio, lr->lr_common.lrc_txg,
961b24ab676SJeff Bonwick 			    zvol_get_done, zgd);
962975c32a0SNeil Perrin 
963b24ab676SJeff Bonwick 			if (error == 0)
964b24ab676SJeff Bonwick 				return (0);
965b24ab676SJeff Bonwick 		}
966975c32a0SNeil Perrin 	}
967975c32a0SNeil Perrin 
968b24ab676SJeff Bonwick 	zvol_get_done(zgd, error);
969b24ab676SJeff Bonwick 
97067bd71c6Sperrin 	return (error);
97167bd71c6Sperrin }
97267bd71c6Sperrin 
973a24e15ceSperrin /*
974a24e15ceSperrin  * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
97522ac5be4Sperrin  *
97622ac5be4Sperrin  * We store data in the log buffers if it's small enough.
97767bd71c6Sperrin  * Otherwise we will later flush the data out via dmu_sync().
97822ac5be4Sperrin  */
97967bd71c6Sperrin ssize_t zvol_immediate_write_sz = 32768;
98022ac5be4Sperrin 
981feb08c6bSbillm static void
982510b6c0eSNeil Perrin zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid,
983510b6c0eSNeil Perrin     boolean_t sync)
98422ac5be4Sperrin {
985feb08c6bSbillm 	uint32_t blocksize = zv->zv_volblocksize;
9861209a471SNeil Perrin 	zilog_t *zilog = zv->zv_zilog;
987510b6c0eSNeil Perrin 	boolean_t slogging;
988e09fa4daSNeil Perrin 	ssize_t immediate_write_sz;
989510b6c0eSNeil Perrin 
990510b6c0eSNeil Perrin 	if (zil_disable)
991510b6c0eSNeil Perrin 		return;
99222ac5be4Sperrin 
993b24ab676SJeff Bonwick 	if (zil_replaying(zilog, tx))
9941209a471SNeil Perrin 		return;
9951209a471SNeil Perrin 
996e09fa4daSNeil Perrin 	immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
997e09fa4daSNeil Perrin 	    ? 0 : zvol_immediate_write_sz;
998e09fa4daSNeil Perrin 
999e09fa4daSNeil Perrin 	slogging = spa_has_slogs(zilog->zl_spa) &&
1000e09fa4daSNeil Perrin 	    (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
1001feb08c6bSbillm 
1002510b6c0eSNeil Perrin 	while (resid) {
1003510b6c0eSNeil Perrin 		itx_t *itx;
1004510b6c0eSNeil Perrin 		lr_write_t *lr;
1005510b6c0eSNeil Perrin 		ssize_t len;
1006510b6c0eSNeil Perrin 		itx_wr_state_t write_state;
1007510b6c0eSNeil Perrin 
1008510b6c0eSNeil Perrin 		/*
1009510b6c0eSNeil Perrin 		 * Unlike zfs_log_write() we can be called with
1010510b6c0eSNeil Perrin 		 * upto DMU_MAX_ACCESS/2 (5MB) writes.
1011510b6c0eSNeil Perrin 		 */
1012e09fa4daSNeil Perrin 		if (blocksize > immediate_write_sz && !slogging &&
1013510b6c0eSNeil Perrin 		    resid >= blocksize && off % blocksize == 0) {
1014510b6c0eSNeil Perrin 			write_state = WR_INDIRECT; /* uses dmu_sync */
1015510b6c0eSNeil Perrin 			len = blocksize;
1016510b6c0eSNeil Perrin 		} else if (sync) {
1017510b6c0eSNeil Perrin 			write_state = WR_COPIED;
1018510b6c0eSNeil Perrin 			len = MIN(ZIL_MAX_LOG_DATA, resid);
1019510b6c0eSNeil Perrin 		} else {
1020510b6c0eSNeil Perrin 			write_state = WR_NEED_COPY;
1021510b6c0eSNeil Perrin 			len = MIN(ZIL_MAX_LOG_DATA, resid);
1022510b6c0eSNeil Perrin 		}
1023510b6c0eSNeil Perrin 
1024510b6c0eSNeil Perrin 		itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
1025510b6c0eSNeil Perrin 		    (write_state == WR_COPIED ? len : 0));
1026feb08c6bSbillm 		lr = (lr_write_t *)&itx->itx_lr;
1027510b6c0eSNeil Perrin 		if (write_state == WR_COPIED && dmu_read(zv->zv_objset,
10287bfdf011SNeil Perrin 		    ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
1029b24ab676SJeff Bonwick 			zil_itx_destroy(itx);
1030510b6c0eSNeil Perrin 			itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1031510b6c0eSNeil Perrin 			lr = (lr_write_t *)&itx->itx_lr;
1032510b6c0eSNeil Perrin 			write_state = WR_NEED_COPY;
1033510b6c0eSNeil Perrin 		}
1034510b6c0eSNeil Perrin 
1035510b6c0eSNeil Perrin 		itx->itx_wr_state = write_state;
1036510b6c0eSNeil Perrin 		if (write_state == WR_NEED_COPY)
1037510b6c0eSNeil Perrin 			itx->itx_sod += len;
1038feb08c6bSbillm 		lr->lr_foid = ZVOL_OBJ;
1039feb08c6bSbillm 		lr->lr_offset = off;
1040510b6c0eSNeil Perrin 		lr->lr_length = len;
1041b24ab676SJeff Bonwick 		lr->lr_blkoff = 0;
1042feb08c6bSbillm 		BP_ZERO(&lr->lr_blkptr);
1043feb08c6bSbillm 
1044510b6c0eSNeil Perrin 		itx->itx_private = zv;
1045510b6c0eSNeil Perrin 		itx->itx_sync = sync;
1046510b6c0eSNeil Perrin 
10471209a471SNeil Perrin 		(void) zil_itx_assign(zilog, itx, tx);
1048510b6c0eSNeil Perrin 
1049510b6c0eSNeil Perrin 		off += len;
1050510b6c0eSNeil Perrin 		resid -= len;
105122ac5be4Sperrin 	}
105222ac5be4Sperrin }
105322ac5be4Sperrin 
105488b7b0f2SMatthew Ahrens static int
105588b7b0f2SMatthew Ahrens zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t size,
105688b7b0f2SMatthew Ahrens     boolean_t doread, boolean_t isdump)
1057e7cbe64fSgw {
1058e7cbe64fSgw 	vdev_disk_t *dvd;
1059e7cbe64fSgw 	int c;
1060e7cbe64fSgw 	int numerrors = 0;
1061e7cbe64fSgw 
1062e7cbe64fSgw 	for (c = 0; c < vd->vdev_children; c++) {
106321ecdf64SLin Ling 		ASSERT(vd->vdev_ops == &vdev_mirror_ops ||
106421ecdf64SLin Ling 		    vd->vdev_ops == &vdev_replacing_ops ||
106521ecdf64SLin Ling 		    vd->vdev_ops == &vdev_spare_ops);
106688b7b0f2SMatthew Ahrens 		int err = zvol_dumpio_vdev(vd->vdev_child[c],
106788b7b0f2SMatthew Ahrens 		    addr, offset, size, doread, isdump);
106888b7b0f2SMatthew Ahrens 		if (err != 0) {
1069e7cbe64fSgw 			numerrors++;
107088b7b0f2SMatthew Ahrens 		} else if (doread) {
1071e7cbe64fSgw 			break;
1072e7cbe64fSgw 		}
1073e7cbe64fSgw 	}
1074e7cbe64fSgw 
1075e7cbe64fSgw 	if (!vd->vdev_ops->vdev_op_leaf)
1076e7cbe64fSgw 		return (numerrors < vd->vdev_children ? 0 : EIO);
1077e7cbe64fSgw 
1078dc0bb255SEric Taylor 	if (doread && !vdev_readable(vd))
1079dc0bb255SEric Taylor 		return (EIO);
1080dc0bb255SEric Taylor 	else if (!doread && !vdev_writeable(vd))
1081e7cbe64fSgw 		return (EIO);
1082e7cbe64fSgw 
1083e7cbe64fSgw 	dvd = vd->vdev_tsd;
1084e7cbe64fSgw 	ASSERT3P(dvd, !=, NULL);
1085e7cbe64fSgw 	offset += VDEV_LABEL_START_SIZE;
1086e7cbe64fSgw 
1087e7cbe64fSgw 	if (ddi_in_panic() || isdump) {
108888b7b0f2SMatthew Ahrens 		ASSERT(!doread);
108988b7b0f2SMatthew Ahrens 		if (doread)
1090e7cbe64fSgw 			return (EIO);
1091e7cbe64fSgw 		return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset),
1092e7cbe64fSgw 		    lbtodb(size)));
1093e7cbe64fSgw 	} else {
1094e7cbe64fSgw 		return (vdev_disk_physio(dvd->vd_lh, addr, size, offset,
109588b7b0f2SMatthew Ahrens 		    doread ? B_READ : B_WRITE));
1096e7cbe64fSgw 	}
1097e7cbe64fSgw }
1098e7cbe64fSgw 
109988b7b0f2SMatthew Ahrens static int
110088b7b0f2SMatthew Ahrens zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size,
110188b7b0f2SMatthew Ahrens     boolean_t doread, boolean_t isdump)
1102e7cbe64fSgw {
1103e7cbe64fSgw 	vdev_t *vd;
1104e7cbe64fSgw 	int error;
110588b7b0f2SMatthew Ahrens 	zvol_extent_t *ze;
1106e7cbe64fSgw 	spa_t *spa = dmu_objset_spa(zv->zv_objset);
1107e7cbe64fSgw 
110888b7b0f2SMatthew Ahrens 	/* Must be sector aligned, and not stradle a block boundary. */
110988b7b0f2SMatthew Ahrens 	if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) ||
111088b7b0f2SMatthew Ahrens 	    P2BOUNDARY(offset, size, zv->zv_volblocksize)) {
1111e7cbe64fSgw 		return (EINVAL);
111288b7b0f2SMatthew Ahrens 	}
111388b7b0f2SMatthew Ahrens 	ASSERT(size <= zv->zv_volblocksize);
1114e7cbe64fSgw 
111588b7b0f2SMatthew Ahrens 	/* Locate the extent this belongs to */
111688b7b0f2SMatthew Ahrens 	ze = list_head(&zv->zv_extents);
111788b7b0f2SMatthew Ahrens 	while (offset >= ze->ze_nblks * zv->zv_volblocksize) {
111888b7b0f2SMatthew Ahrens 		offset -= ze->ze_nblks * zv->zv_volblocksize;
111988b7b0f2SMatthew Ahrens 		ze = list_next(&zv->zv_extents, ze);
112088b7b0f2SMatthew Ahrens 	}
112124cc0e1cSGeorge Wilson 
112224cc0e1cSGeorge Wilson 	if (!ddi_in_panic())
112324cc0e1cSGeorge Wilson 		spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
112424cc0e1cSGeorge Wilson 
112588b7b0f2SMatthew Ahrens 	vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva));
112688b7b0f2SMatthew Ahrens 	offset += DVA_GET_OFFSET(&ze->ze_dva);
112788b7b0f2SMatthew Ahrens 	error = zvol_dumpio_vdev(vd, addr, offset, size, doread, isdump);
112824cc0e1cSGeorge Wilson 
112924cc0e1cSGeorge Wilson 	if (!ddi_in_panic())
113024cc0e1cSGeorge Wilson 		spa_config_exit(spa, SCL_STATE, FTAG);
113124cc0e1cSGeorge Wilson 
1132e7cbe64fSgw 	return (error);
1133e7cbe64fSgw }
1134e7cbe64fSgw 
1135fa9e4066Sahrens int
1136fa9e4066Sahrens zvol_strategy(buf_t *bp)
1137fa9e4066Sahrens {
1138fa9e4066Sahrens 	zvol_state_t *zv = ddi_get_soft_state(zvol_state, getminor(bp->b_edev));
1139fa9e4066Sahrens 	uint64_t off, volsize;
114088b7b0f2SMatthew Ahrens 	size_t resid;
1141fa9e4066Sahrens 	char *addr;
114222ac5be4Sperrin 	objset_t *os;
1143c2e6a7d6Sperrin 	rl_t *rl;
1144fa9e4066Sahrens 	int error = 0;
114588b7b0f2SMatthew Ahrens 	boolean_t doread = bp->b_flags & B_READ;
1146f80ce222SChris Kirby 	boolean_t is_dump;
1147510b6c0eSNeil Perrin 	boolean_t sync;
1148fa9e4066Sahrens 
1149fa9e4066Sahrens 	if (zv == NULL) {
1150fa9e4066Sahrens 		bioerror(bp, ENXIO);
1151fa9e4066Sahrens 		biodone(bp);
1152fa9e4066Sahrens 		return (0);
1153fa9e4066Sahrens 	}
1154fa9e4066Sahrens 
1155fa9e4066Sahrens 	if (getminor(bp->b_edev) == 0) {
1156fa9e4066Sahrens 		bioerror(bp, EINVAL);
1157fa9e4066Sahrens 		biodone(bp);
1158fa9e4066Sahrens 		return (0);
1159fa9e4066Sahrens 	}
1160fa9e4066Sahrens 
1161681d9761SEric Taylor 	if (!(bp->b_flags & B_READ) && (zv->zv_flags & ZVOL_RDONLY)) {
1162fa9e4066Sahrens 		bioerror(bp, EROFS);
1163fa9e4066Sahrens 		biodone(bp);
1164fa9e4066Sahrens 		return (0);
1165fa9e4066Sahrens 	}
1166fa9e4066Sahrens 
1167fa9e4066Sahrens 	off = ldbtob(bp->b_blkno);
1168fa9e4066Sahrens 	volsize = zv->zv_volsize;
1169fa9e4066Sahrens 
117022ac5be4Sperrin 	os = zv->zv_objset;
117122ac5be4Sperrin 	ASSERT(os != NULL);
1172fa9e4066Sahrens 
1173fa9e4066Sahrens 	bp_mapin(bp);
1174fa9e4066Sahrens 	addr = bp->b_un.b_addr;
1175fa9e4066Sahrens 	resid = bp->b_bcount;
1176fa9e4066Sahrens 
117788b7b0f2SMatthew Ahrens 	if (resid > 0 && (off < 0 || off >= volsize)) {
117888b7b0f2SMatthew Ahrens 		bioerror(bp, EIO);
117988b7b0f2SMatthew Ahrens 		biodone(bp);
118088b7b0f2SMatthew Ahrens 		return (0);
118188b7b0f2SMatthew Ahrens 	}
118273ec3d9cSgw 
1183f80ce222SChris Kirby 	is_dump = zv->zv_flags & ZVOL_DUMPIFIED;
1184510b6c0eSNeil Perrin 	sync = !(bp->b_flags & B_ASYNC) && !doread && !is_dump &&
1185510b6c0eSNeil Perrin 	    !(zv->zv_flags & ZVOL_WCE) && !zil_disable;
1186510b6c0eSNeil Perrin 
1187a24e15ceSperrin 	/*
1188a24e15ceSperrin 	 * There must be no buffer changes when doing a dmu_sync() because
1189a24e15ceSperrin 	 * we can't change the data whilst calculating the checksum.
1190a24e15ceSperrin 	 */
1191c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, off, resid,
119288b7b0f2SMatthew Ahrens 	    doread ? RL_READER : RL_WRITER);
1193fa9e4066Sahrens 
1194e7cbe64fSgw 	while (resid != 0 && off < volsize) {
119588b7b0f2SMatthew Ahrens 		size_t size = MIN(resid, zvol_maxphys);
1196e7cbe64fSgw 		if (is_dump) {
1197e7cbe64fSgw 			size = MIN(size, P2END(off, zv->zv_volblocksize) - off);
119888b7b0f2SMatthew Ahrens 			error = zvol_dumpio(zv, addr, off, size,
119988b7b0f2SMatthew Ahrens 			    doread, B_FALSE);
120088b7b0f2SMatthew Ahrens 		} else if (doread) {
12017bfdf011SNeil Perrin 			error = dmu_read(os, ZVOL_OBJ, off, size, addr,
12027bfdf011SNeil Perrin 			    DMU_READ_PREFETCH);
1203fa9e4066Sahrens 		} else {
120422ac5be4Sperrin 			dmu_tx_t *tx = dmu_tx_create(os);
1205fa9e4066Sahrens 			dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
1206fa9e4066Sahrens 			error = dmu_tx_assign(tx, TXG_WAIT);
1207fa9e4066Sahrens 			if (error) {
1208fa9e4066Sahrens 				dmu_tx_abort(tx);
1209fa9e4066Sahrens 			} else {
121022ac5be4Sperrin 				dmu_write(os, ZVOL_OBJ, off, size, addr, tx);
1211510b6c0eSNeil Perrin 				zvol_log_write(zv, tx, off, size, sync);
1212fa9e4066Sahrens 				dmu_tx_commit(tx);
1213fa9e4066Sahrens 			}
1214fa9e4066Sahrens 		}
1215b87f3af3Sperrin 		if (error) {
1216b87f3af3Sperrin 			/* convert checksum errors into IO errors */
1217b87f3af3Sperrin 			if (error == ECKSUM)
1218b87f3af3Sperrin 				error = EIO;
1219fa9e4066Sahrens 			break;
1220b87f3af3Sperrin 		}
1221fa9e4066Sahrens 		off += size;
1222fa9e4066Sahrens 		addr += size;
1223fa9e4066Sahrens 		resid -= size;
1224fa9e4066Sahrens 	}
1225c2e6a7d6Sperrin 	zfs_range_unlock(rl);
1226fa9e4066Sahrens 
1227fa9e4066Sahrens 	if ((bp->b_resid = resid) == bp->b_bcount)
1228fa9e4066Sahrens 		bioerror(bp, off > volsize ? EINVAL : error);
1229fa9e4066Sahrens 
1230510b6c0eSNeil Perrin 	if (sync)
1231feb08c6bSbillm 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1232feb08c6bSbillm 	biodone(bp);
123322ac5be4Sperrin 
1234fa9e4066Sahrens 	return (0);
1235fa9e4066Sahrens }
1236fa9e4066Sahrens 
123767bd71c6Sperrin /*
123867bd71c6Sperrin  * Set the buffer count to the zvol maximum transfer.
123967bd71c6Sperrin  * Using our own routine instead of the default minphys()
124067bd71c6Sperrin  * means that for larger writes we write bigger buffers on X86
124167bd71c6Sperrin  * (128K instead of 56K) and flush the disk write cache less often
124267bd71c6Sperrin  * (every zvol_maxphys - currently 1MB) instead of minphys (currently
124367bd71c6Sperrin  * 56K on X86 and 128K on sparc).
124467bd71c6Sperrin  */
124567bd71c6Sperrin void
124667bd71c6Sperrin zvol_minphys(struct buf *bp)
124767bd71c6Sperrin {
124867bd71c6Sperrin 	if (bp->b_bcount > zvol_maxphys)
124967bd71c6Sperrin 		bp->b_bcount = zvol_maxphys;
125067bd71c6Sperrin }
125167bd71c6Sperrin 
1252e7cbe64fSgw int
1253e7cbe64fSgw zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks)
1254e7cbe64fSgw {
1255e7cbe64fSgw 	minor_t minor = getminor(dev);
1256e7cbe64fSgw 	zvol_state_t *zv;
1257e7cbe64fSgw 	int error = 0;
1258e7cbe64fSgw 	uint64_t size;
1259e7cbe64fSgw 	uint64_t boff;
1260e7cbe64fSgw 	uint64_t resid;
1261e7cbe64fSgw 
1262e7cbe64fSgw 	if (minor == 0)			/* This is the control device */
1263e7cbe64fSgw 		return (ENXIO);
1264e7cbe64fSgw 
1265e7cbe64fSgw 	zv = ddi_get_soft_state(zvol_state, minor);
1266e7cbe64fSgw 	if (zv == NULL)
1267e7cbe64fSgw 		return (ENXIO);
1268e7cbe64fSgw 
1269e7cbe64fSgw 	boff = ldbtob(blkno);
1270e7cbe64fSgw 	resid = ldbtob(nblocks);
127188b7b0f2SMatthew Ahrens 
127288b7b0f2SMatthew Ahrens 	VERIFY3U(boff + resid, <=, zv->zv_volsize);
127388b7b0f2SMatthew Ahrens 
1274e7cbe64fSgw 	while (resid) {
1275e7cbe64fSgw 		size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff);
127688b7b0f2SMatthew Ahrens 		error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE);
1277e7cbe64fSgw 		if (error)
1278e7cbe64fSgw 			break;
1279e7cbe64fSgw 		boff += size;
1280e7cbe64fSgw 		addr += size;
1281e7cbe64fSgw 		resid -= size;
1282e7cbe64fSgw 	}
1283e7cbe64fSgw 
1284e7cbe64fSgw 	return (error);
1285e7cbe64fSgw }
1286e7cbe64fSgw 
1287fa9e4066Sahrens /*ARGSUSED*/
1288fa9e4066Sahrens int
1289feb08c6bSbillm zvol_read(dev_t dev, uio_t *uio, cred_t *cr)
1290fa9e4066Sahrens {
1291c7ca1008Sgw 	minor_t minor = getminor(dev);
1292c7ca1008Sgw 	zvol_state_t *zv;
129373ec3d9cSgw 	uint64_t volsize;
1294c2e6a7d6Sperrin 	rl_t *rl;
1295feb08c6bSbillm 	int error = 0;
1296fa9e4066Sahrens 
1297c7ca1008Sgw 	if (minor == 0)			/* This is the control device */
1298c7ca1008Sgw 		return (ENXIO);
1299c7ca1008Sgw 
1300c7ca1008Sgw 	zv = ddi_get_soft_state(zvol_state, minor);
1301c7ca1008Sgw 	if (zv == NULL)
1302c7ca1008Sgw 		return (ENXIO);
1303c7ca1008Sgw 
130473ec3d9cSgw 	volsize = zv->zv_volsize;
130573ec3d9cSgw 	if (uio->uio_resid > 0 &&
130673ec3d9cSgw 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
130773ec3d9cSgw 		return (EIO);
130873ec3d9cSgw 
130988b7b0f2SMatthew Ahrens 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
131088b7b0f2SMatthew Ahrens 		error = physio(zvol_strategy, NULL, dev, B_READ,
131188b7b0f2SMatthew Ahrens 		    zvol_minphys, uio);
131288b7b0f2SMatthew Ahrens 		return (error);
131388b7b0f2SMatthew Ahrens 	}
131488b7b0f2SMatthew Ahrens 
1315c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1316c2e6a7d6Sperrin 	    RL_READER);
131773ec3d9cSgw 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1318feb08c6bSbillm 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1319fa9e4066Sahrens 
132073ec3d9cSgw 		/* don't read past the end */
132173ec3d9cSgw 		if (bytes > volsize - uio->uio_loffset)
132273ec3d9cSgw 			bytes = volsize - uio->uio_loffset;
132373ec3d9cSgw 
1324feb08c6bSbillm 		error =  dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes);
1325b87f3af3Sperrin 		if (error) {
1326b87f3af3Sperrin 			/* convert checksum errors into IO errors */
1327b87f3af3Sperrin 			if (error == ECKSUM)
1328b87f3af3Sperrin 				error = EIO;
1329feb08c6bSbillm 			break;
1330b87f3af3Sperrin 		}
1331feb08c6bSbillm 	}
1332c2e6a7d6Sperrin 	zfs_range_unlock(rl);
1333feb08c6bSbillm 	return (error);
1334fa9e4066Sahrens }
1335fa9e4066Sahrens 
1336fa9e4066Sahrens /*ARGSUSED*/
1337fa9e4066Sahrens int
1338feb08c6bSbillm zvol_write(dev_t dev, uio_t *uio, cred_t *cr)
1339fa9e4066Sahrens {
1340c7ca1008Sgw 	minor_t minor = getminor(dev);
1341c7ca1008Sgw 	zvol_state_t *zv;
134273ec3d9cSgw 	uint64_t volsize;
1343c2e6a7d6Sperrin 	rl_t *rl;
1344feb08c6bSbillm 	int error = 0;
1345510b6c0eSNeil Perrin 	boolean_t sync;
1346feb08c6bSbillm 
1347c7ca1008Sgw 	if (minor == 0)			/* This is the control device */
1348c7ca1008Sgw 		return (ENXIO);
1349c7ca1008Sgw 
1350c7ca1008Sgw 	zv = ddi_get_soft_state(zvol_state, minor);
1351c7ca1008Sgw 	if (zv == NULL)
1352c7ca1008Sgw 		return (ENXIO);
1353c7ca1008Sgw 
135473ec3d9cSgw 	volsize = zv->zv_volsize;
135573ec3d9cSgw 	if (uio->uio_resid > 0 &&
135673ec3d9cSgw 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
135773ec3d9cSgw 		return (EIO);
135873ec3d9cSgw 
1359e7cbe64fSgw 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
1360e7cbe64fSgw 		error = physio(zvol_strategy, NULL, dev, B_WRITE,
1361e7cbe64fSgw 		    zvol_minphys, uio);
1362e7cbe64fSgw 		return (error);
1363e7cbe64fSgw 	}
1364e7cbe64fSgw 
1365510b6c0eSNeil Perrin 	sync = !(zv->zv_flags & ZVOL_WCE) && !zil_disable;
1366510b6c0eSNeil Perrin 
1367c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1368c2e6a7d6Sperrin 	    RL_WRITER);
136973ec3d9cSgw 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1370feb08c6bSbillm 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1371feb08c6bSbillm 		uint64_t off = uio->uio_loffset;
1372feb08c6bSbillm 		dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
137373ec3d9cSgw 
137473ec3d9cSgw 		if (bytes > volsize - off)	/* don't write past the end */
137573ec3d9cSgw 			bytes = volsize - off;
137673ec3d9cSgw 
1377feb08c6bSbillm 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
1378feb08c6bSbillm 		error = dmu_tx_assign(tx, TXG_WAIT);
1379feb08c6bSbillm 		if (error) {
1380feb08c6bSbillm 			dmu_tx_abort(tx);
1381feb08c6bSbillm 			break;
1382feb08c6bSbillm 		}
1383*94d1a210STim Haley 		error = dmu_write_uio_dbuf(zv->zv_dbuf, uio, bytes, tx);
1384feb08c6bSbillm 		if (error == 0)
1385510b6c0eSNeil Perrin 			zvol_log_write(zv, tx, off, bytes, sync);
1386feb08c6bSbillm 		dmu_tx_commit(tx);
1387feb08c6bSbillm 
1388feb08c6bSbillm 		if (error)
1389feb08c6bSbillm 			break;
1390feb08c6bSbillm 	}
1391c2e6a7d6Sperrin 	zfs_range_unlock(rl);
1392510b6c0eSNeil Perrin 	if (sync)
1393e08bf2c6SEric Taylor 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1394feb08c6bSbillm 	return (error);
1395fa9e4066Sahrens }
1396fa9e4066Sahrens 
1397c7f714e2SEric Taylor int
1398c7f714e2SEric Taylor zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs)
1399c7f714e2SEric Taylor {
1400c7f714e2SEric Taylor 	struct uuid uuid = EFI_RESERVED;
1401c7f714e2SEric Taylor 	efi_gpe_t gpe = { 0 };
1402c7f714e2SEric Taylor 	uint32_t crc;
1403c7f714e2SEric Taylor 	dk_efi_t efi;
1404c7f714e2SEric Taylor 	int length;
1405c7f714e2SEric Taylor 	char *ptr;
1406c7f714e2SEric Taylor 
1407c7f714e2SEric Taylor 	if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag))
1408c7f714e2SEric Taylor 		return (EFAULT);
1409c7f714e2SEric Taylor 	ptr = (char *)(uintptr_t)efi.dki_data_64;
1410c7f714e2SEric Taylor 	length = efi.dki_length;
1411c7f714e2SEric Taylor 	/*
1412c7f714e2SEric Taylor 	 * Some clients may attempt to request a PMBR for the
1413c7f714e2SEric Taylor 	 * zvol.  Currently this interface will return EINVAL to
1414c7f714e2SEric Taylor 	 * such requests.  These requests could be supported by
1415c7f714e2SEric Taylor 	 * adding a check for lba == 0 and consing up an appropriate
1416c7f714e2SEric Taylor 	 * PMBR.
1417c7f714e2SEric Taylor 	 */
1418c7f714e2SEric Taylor 	if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0)
1419c7f714e2SEric Taylor 		return (EINVAL);
1420c7f714e2SEric Taylor 
1421c7f714e2SEric Taylor 	gpe.efi_gpe_StartingLBA = LE_64(34ULL);
1422c7f714e2SEric Taylor 	gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1);
1423c7f714e2SEric Taylor 	UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
1424c7f714e2SEric Taylor 
1425c7f714e2SEric Taylor 	if (efi.dki_lba == 1) {
1426c7f714e2SEric Taylor 		efi_gpt_t gpt = { 0 };
1427c7f714e2SEric Taylor 
1428c7f714e2SEric Taylor 		gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
1429c7f714e2SEric Taylor 		gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
1430c7f714e2SEric Taylor 		gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt));
1431c7f714e2SEric Taylor 		gpt.efi_gpt_MyLBA = LE_64(1ULL);
1432c7f714e2SEric Taylor 		gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL);
1433c7f714e2SEric Taylor 		gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1);
1434c7f714e2SEric Taylor 		gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL);
1435c7f714e2SEric Taylor 		gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
1436c7f714e2SEric Taylor 		gpt.efi_gpt_SizeOfPartitionEntry =
1437c7f714e2SEric Taylor 		    LE_32(sizeof (efi_gpe_t));
1438c7f714e2SEric Taylor 		CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
1439c7f714e2SEric Taylor 		gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
1440c7f714e2SEric Taylor 		CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table);
1441c7f714e2SEric Taylor 		gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
1442c7f714e2SEric Taylor 		if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length),
1443c7f714e2SEric Taylor 		    flag))
1444c7f714e2SEric Taylor 			return (EFAULT);
1445c7f714e2SEric Taylor 		ptr += sizeof (gpt);
1446c7f714e2SEric Taylor 		length -= sizeof (gpt);
1447c7f714e2SEric Taylor 	}
1448c7f714e2SEric Taylor 	if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe),
1449c7f714e2SEric Taylor 	    length), flag))
1450c7f714e2SEric Taylor 		return (EFAULT);
1451c7f714e2SEric Taylor 	return (0);
1452c7f714e2SEric Taylor }
1453c7f714e2SEric Taylor 
1454fa9e4066Sahrens /*
1455fa9e4066Sahrens  * Dirtbag ioctls to support mkfs(1M) for UFS filesystems.  See dkio(7I).
1456fa9e4066Sahrens  */
1457fa9e4066Sahrens /*ARGSUSED*/
1458fa9e4066Sahrens int
1459fa9e4066Sahrens zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
1460fa9e4066Sahrens {
1461fa9e4066Sahrens 	zvol_state_t *zv;
1462af2c4821Smaybee 	struct dk_cinfo dki;
1463fa9e4066Sahrens 	struct dk_minfo dkm;
1464af2c4821Smaybee 	struct dk_callback *dkc;
1465fa9e4066Sahrens 	int error = 0;
1466e7cbe64fSgw 	rl_t *rl;
1467fa9e4066Sahrens 
1468fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
1469fa9e4066Sahrens 
1470fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, getminor(dev));
1471fa9e4066Sahrens 
1472fa9e4066Sahrens 	if (zv == NULL) {
1473fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
1474fa9e4066Sahrens 		return (ENXIO);
1475fa9e4066Sahrens 	}
1476701f66c4SEric Taylor 	ASSERT(zv->zv_total_opens > 0);
1477fa9e4066Sahrens 
1478fa9e4066Sahrens 	switch (cmd) {
1479fa9e4066Sahrens 
1480fa9e4066Sahrens 	case DKIOCINFO:
1481af2c4821Smaybee 		bzero(&dki, sizeof (dki));
1482af2c4821Smaybee 		(void) strcpy(dki.dki_cname, "zvol");
1483af2c4821Smaybee 		(void) strcpy(dki.dki_dname, "zvol");
1484af2c4821Smaybee 		dki.dki_ctype = DKC_UNKNOWN;
14853adc9019SEric Taylor 		dki.dki_unit = getminor(dev);
1486af2c4821Smaybee 		dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs);
1487fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
1488af2c4821Smaybee 		if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag))
1489fa9e4066Sahrens 			error = EFAULT;
1490fa9e4066Sahrens 		return (error);
1491fa9e4066Sahrens 
1492fa9e4066Sahrens 	case DKIOCGMEDIAINFO:
1493fa9e4066Sahrens 		bzero(&dkm, sizeof (dkm));
1494fa9e4066Sahrens 		dkm.dki_lbsize = 1U << zv->zv_min_bs;
1495fa9e4066Sahrens 		dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
1496fa9e4066Sahrens 		dkm.dki_media_type = DK_UNKNOWN;
1497fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
1498fa9e4066Sahrens 		if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
1499fa9e4066Sahrens 			error = EFAULT;
1500fa9e4066Sahrens 		return (error);
1501fa9e4066Sahrens 
1502fa9e4066Sahrens 	case DKIOCGETEFI:
1503c7f714e2SEric Taylor 		{
1504c7f714e2SEric Taylor 			uint64_t vs = zv->zv_volsize;
1505c7f714e2SEric Taylor 			uint8_t bs = zv->zv_min_bs;
1506fa9e4066Sahrens 
150768a5ac4dSmaybee 			mutex_exit(&zvol_state_lock);
1508c7f714e2SEric Taylor 			error = zvol_getefi((void *)arg, flag, vs, bs);
1509c7f714e2SEric Taylor 			return (error);
151068a5ac4dSmaybee 		}
1511fa9e4066Sahrens 
1512feb08c6bSbillm 	case DKIOCFLUSHWRITECACHE:
1513af2c4821Smaybee 		dkc = (struct dk_callback *)arg;
1514701f66c4SEric Taylor 		mutex_exit(&zvol_state_lock);
1515feb08c6bSbillm 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1516af2c4821Smaybee 		if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) {
1517af2c4821Smaybee 			(*dkc->dkc_callback)(dkc->dkc_cookie, error);
1518af2c4821Smaybee 			error = 0;
1519af2c4821Smaybee 		}
1520701f66c4SEric Taylor 		return (error);
1521701f66c4SEric Taylor 
1522701f66c4SEric Taylor 	case DKIOCGETWCE:
1523701f66c4SEric Taylor 		{
1524701f66c4SEric Taylor 			int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0;
1525701f66c4SEric Taylor 			if (ddi_copyout(&wce, (void *)arg, sizeof (int),
1526701f66c4SEric Taylor 			    flag))
1527701f66c4SEric Taylor 				error = EFAULT;
1528701f66c4SEric Taylor 			break;
1529701f66c4SEric Taylor 		}
1530701f66c4SEric Taylor 	case DKIOCSETWCE:
1531701f66c4SEric Taylor 		{
1532701f66c4SEric Taylor 			int wce;
1533701f66c4SEric Taylor 			if (ddi_copyin((void *)arg, &wce, sizeof (int),
1534701f66c4SEric Taylor 			    flag)) {
1535701f66c4SEric Taylor 				error = EFAULT;
1536701f66c4SEric Taylor 				break;
1537701f66c4SEric Taylor 			}
1538701f66c4SEric Taylor 			if (wce) {
1539701f66c4SEric Taylor 				zv->zv_flags |= ZVOL_WCE;
1540701f66c4SEric Taylor 				mutex_exit(&zvol_state_lock);
1541701f66c4SEric Taylor 			} else {
1542701f66c4SEric Taylor 				zv->zv_flags &= ~ZVOL_WCE;
1543701f66c4SEric Taylor 				mutex_exit(&zvol_state_lock);
1544701f66c4SEric Taylor 				zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1545701f66c4SEric Taylor 			}
1546701f66c4SEric Taylor 			return (0);
1547701f66c4SEric Taylor 		}
1548feb08c6bSbillm 
1549b6130eadSmaybee 	case DKIOCGGEOM:
1550b6130eadSmaybee 	case DKIOCGVTOC:
1551e7cbe64fSgw 		/*
1552e7cbe64fSgw 		 * commands using these (like prtvtoc) expect ENOTSUP
1553e7cbe64fSgw 		 * since we're emulating an EFI label
1554e7cbe64fSgw 		 */
1555b6130eadSmaybee 		error = ENOTSUP;
1556b6130eadSmaybee 		break;
1557b6130eadSmaybee 
1558e7cbe64fSgw 	case DKIOCDUMPINIT:
1559e7cbe64fSgw 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1560e7cbe64fSgw 		    RL_WRITER);
1561e7cbe64fSgw 		error = zvol_dumpify(zv);
1562e7cbe64fSgw 		zfs_range_unlock(rl);
1563e7cbe64fSgw 		break;
1564e7cbe64fSgw 
1565e7cbe64fSgw 	case DKIOCDUMPFINI:
156606d5ae10SEric Taylor 		if (!(zv->zv_flags & ZVOL_DUMPIFIED))
156706d5ae10SEric Taylor 			break;
1568e7cbe64fSgw 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1569e7cbe64fSgw 		    RL_WRITER);
1570e7cbe64fSgw 		error = zvol_dump_fini(zv);
1571e7cbe64fSgw 		zfs_range_unlock(rl);
1572e7cbe64fSgw 		break;
1573e7cbe64fSgw 
1574fa9e4066Sahrens 	default:
157568a5ac4dSmaybee 		error = ENOTTY;
1576fa9e4066Sahrens 		break;
1577fa9e4066Sahrens 
1578fa9e4066Sahrens 	}
1579fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
1580fa9e4066Sahrens 	return (error);
1581fa9e4066Sahrens }
1582fa9e4066Sahrens 
1583fa9e4066Sahrens int
1584fa9e4066Sahrens zvol_busy(void)
1585fa9e4066Sahrens {
1586fa9e4066Sahrens 	return (zvol_minors != 0);
1587fa9e4066Sahrens }
1588fa9e4066Sahrens 
1589fa9e4066Sahrens void
1590fa9e4066Sahrens zvol_init(void)
1591fa9e4066Sahrens {
1592fa9e4066Sahrens 	VERIFY(ddi_soft_state_init(&zvol_state, sizeof (zvol_state_t), 1) == 0);
1593fa9e4066Sahrens 	mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
1594fa9e4066Sahrens }
1595fa9e4066Sahrens 
1596fa9e4066Sahrens void
1597fa9e4066Sahrens zvol_fini(void)
1598fa9e4066Sahrens {
1599fa9e4066Sahrens 	mutex_destroy(&zvol_state_lock);
1600fa9e4066Sahrens 	ddi_soft_state_fini(&zvol_state);
1601fa9e4066Sahrens }
1602e7cbe64fSgw 
1603e7cbe64fSgw static int
1604e7cbe64fSgw zvol_dump_init(zvol_state_t *zv, boolean_t resize)
1605e7cbe64fSgw {
1606e7cbe64fSgw 	dmu_tx_t *tx;
1607e7cbe64fSgw 	int error = 0;
1608e7cbe64fSgw 	objset_t *os = zv->zv_objset;
1609e7cbe64fSgw 	nvlist_t *nv = NULL;
16108d265e66SGeorge Wilson 	uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset));
1611e7cbe64fSgw 
1612e7cbe64fSgw 	ASSERT(MUTEX_HELD(&zvol_state_lock));
1613681d9761SEric Taylor 	error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0,
1614681d9761SEric Taylor 	    DMU_OBJECT_END);
1615681d9761SEric Taylor 	/* wait for dmu_free_long_range to actually free the blocks */
1616681d9761SEric Taylor 	txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
1617e7cbe64fSgw 
1618e7cbe64fSgw 	tx = dmu_tx_create(os);
1619e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1620681d9761SEric Taylor 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
1621e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
1622e7cbe64fSgw 	if (error) {
1623e7cbe64fSgw 		dmu_tx_abort(tx);
1624e7cbe64fSgw 		return (error);
1625e7cbe64fSgw 	}
1626e7cbe64fSgw 
1627e7cbe64fSgw 	/*
1628e7cbe64fSgw 	 * If we are resizing the dump device then we only need to
1629e7cbe64fSgw 	 * update the refreservation to match the newly updated
1630e7cbe64fSgw 	 * zvolsize. Otherwise, we save off the original state of the
1631e7cbe64fSgw 	 * zvol so that we can restore them if the zvol is ever undumpified.
1632e7cbe64fSgw 	 */
1633e7cbe64fSgw 	if (resize) {
1634e7cbe64fSgw 		error = zap_update(os, ZVOL_ZAP_OBJ,
1635e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1636e7cbe64fSgw 		    &zv->zv_volsize, tx);
1637e7cbe64fSgw 	} else {
1638afee20e4SGeorge Wilson 		uint64_t checksum, compress, refresrv, vbs, dedup;
163988b7b0f2SMatthew Ahrens 
1640e7cbe64fSgw 		error = dsl_prop_get_integer(zv->zv_name,
1641e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL);
1642e7cbe64fSgw 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
1643e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL);
1644e7cbe64fSgw 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
1645e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL);
164688b7b0f2SMatthew Ahrens 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
164788b7b0f2SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL);
16488d265e66SGeorge Wilson 		if (version >= SPA_VERSION_DEDUP) {
16498d265e66SGeorge Wilson 			error = error ? error :
16508d265e66SGeorge Wilson 			    dsl_prop_get_integer(zv->zv_name,
16518d265e66SGeorge Wilson 			    zfs_prop_to_name(ZFS_PROP_DEDUP), &dedup, NULL);
16528d265e66SGeorge Wilson 		}
1653e7cbe64fSgw 
1654e7cbe64fSgw 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1655e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1,
1656e7cbe64fSgw 		    &compress, tx);
1657e7cbe64fSgw 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1658e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx);
1659e7cbe64fSgw 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1660e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1661e7cbe64fSgw 		    &refresrv, tx);
166288b7b0f2SMatthew Ahrens 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
166388b7b0f2SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1,
166488b7b0f2SMatthew Ahrens 		    &vbs, tx);
1665681d9761SEric Taylor 		error = error ? error : dmu_object_set_blocksize(
1666681d9761SEric Taylor 		    os, ZVOL_OBJ, SPA_MAXBLOCKSIZE, 0, tx);
16678d265e66SGeorge Wilson 		if (version >= SPA_VERSION_DEDUP) {
16688d265e66SGeorge Wilson 			error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
16698d265e66SGeorge Wilson 			    zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1,
16708d265e66SGeorge Wilson 			    &dedup, tx);
16718d265e66SGeorge Wilson 		}
1672681d9761SEric Taylor 		if (error == 0)
1673681d9761SEric Taylor 			zv->zv_volblocksize = SPA_MAXBLOCKSIZE;
1674e7cbe64fSgw 	}
1675e7cbe64fSgw 	dmu_tx_commit(tx);
1676e7cbe64fSgw 
1677e7cbe64fSgw 	/*
1678e7cbe64fSgw 	 * We only need update the zvol's property if we are initializing
1679e7cbe64fSgw 	 * the dump area for the first time.
1680e7cbe64fSgw 	 */
1681e7cbe64fSgw 	if (!resize) {
1682e7cbe64fSgw 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1683e7cbe64fSgw 		VERIFY(nvlist_add_uint64(nv,
1684e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0);
1685e7cbe64fSgw 		VERIFY(nvlist_add_uint64(nv,
1686e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
1687e7cbe64fSgw 		    ZIO_COMPRESS_OFF) == 0);
1688e7cbe64fSgw 		VERIFY(nvlist_add_uint64(nv,
1689e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
1690e7cbe64fSgw 		    ZIO_CHECKSUM_OFF) == 0);
16918d265e66SGeorge Wilson 		if (version >= SPA_VERSION_DEDUP) {
16928d265e66SGeorge Wilson 			VERIFY(nvlist_add_uint64(nv,
16938d265e66SGeorge Wilson 			    zfs_prop_to_name(ZFS_PROP_DEDUP),
16948d265e66SGeorge Wilson 			    ZIO_CHECKSUM_OFF) == 0);
16958d265e66SGeorge Wilson 		}
1696e7cbe64fSgw 
169792241e0bSTom Erickson 		error = zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
169892241e0bSTom Erickson 		    nv, NULL);
1699e7cbe64fSgw 		nvlist_free(nv);
1700e7cbe64fSgw 
1701e7cbe64fSgw 		if (error)
1702e7cbe64fSgw 			return (error);
1703e7cbe64fSgw 	}
1704e7cbe64fSgw 
1705e7cbe64fSgw 	/* Allocate the space for the dump */
1706e7cbe64fSgw 	error = zvol_prealloc(zv);
1707e7cbe64fSgw 	return (error);
1708e7cbe64fSgw }
1709e7cbe64fSgw 
1710e7cbe64fSgw static int
1711e7cbe64fSgw zvol_dumpify(zvol_state_t *zv)
1712e7cbe64fSgw {
1713e7cbe64fSgw 	int error = 0;
1714e7cbe64fSgw 	uint64_t dumpsize = 0;
1715e7cbe64fSgw 	dmu_tx_t *tx;
1716e7cbe64fSgw 	objset_t *os = zv->zv_objset;
1717e7cbe64fSgw 
1718681d9761SEric Taylor 	if (zv->zv_flags & ZVOL_RDONLY)
1719e7cbe64fSgw 		return (EROFS);
1720e7cbe64fSgw 
1721e7cbe64fSgw 	if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE,
1722e7cbe64fSgw 	    8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) {
1723e7cbe64fSgw 		boolean_t resize = (dumpsize > 0) ? B_TRUE : B_FALSE;
1724e7cbe64fSgw 
1725e7cbe64fSgw 		if ((error = zvol_dump_init(zv, resize)) != 0) {
1726e7cbe64fSgw 			(void) zvol_dump_fini(zv);
1727e7cbe64fSgw 			return (error);
1728e7cbe64fSgw 		}
1729e7cbe64fSgw 	}
1730e7cbe64fSgw 
1731e7cbe64fSgw 	/*
1732e7cbe64fSgw 	 * Build up our lba mapping.
1733e7cbe64fSgw 	 */
1734e7cbe64fSgw 	error = zvol_get_lbas(zv);
1735e7cbe64fSgw 	if (error) {
1736e7cbe64fSgw 		(void) zvol_dump_fini(zv);
1737e7cbe64fSgw 		return (error);
1738e7cbe64fSgw 	}
1739e7cbe64fSgw 
1740e7cbe64fSgw 	tx = dmu_tx_create(os);
1741e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1742e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
1743e7cbe64fSgw 	if (error) {
1744e7cbe64fSgw 		dmu_tx_abort(tx);
1745e7cbe64fSgw 		(void) zvol_dump_fini(zv);
1746e7cbe64fSgw 		return (error);
1747e7cbe64fSgw 	}
1748e7cbe64fSgw 
1749e7cbe64fSgw 	zv->zv_flags |= ZVOL_DUMPIFIED;
1750e7cbe64fSgw 	error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1,
1751e7cbe64fSgw 	    &zv->zv_volsize, tx);
1752e7cbe64fSgw 	dmu_tx_commit(tx);
1753e7cbe64fSgw 
1754e7cbe64fSgw 	if (error) {
1755e7cbe64fSgw 		(void) zvol_dump_fini(zv);
1756e7cbe64fSgw 		return (error);
1757e7cbe64fSgw 	}
1758e7cbe64fSgw 
1759e7cbe64fSgw 	txg_wait_synced(dmu_objset_pool(os), 0);
1760e7cbe64fSgw 	return (0);
1761e7cbe64fSgw }
1762e7cbe64fSgw 
1763e7cbe64fSgw static int
1764e7cbe64fSgw zvol_dump_fini(zvol_state_t *zv)
1765e7cbe64fSgw {
1766e7cbe64fSgw 	dmu_tx_t *tx;
1767e7cbe64fSgw 	objset_t *os = zv->zv_objset;
1768e7cbe64fSgw 	nvlist_t *nv;
1769e7cbe64fSgw 	int error = 0;
1770afee20e4SGeorge Wilson 	uint64_t checksum, compress, refresrv, vbs, dedup;
17718d265e66SGeorge Wilson 	uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset));
1772e7cbe64fSgw 
1773b7e50089Smaybee 	/*
1774b7e50089Smaybee 	 * Attempt to restore the zvol back to its pre-dumpified state.
1775b7e50089Smaybee 	 * This is a best-effort attempt as it's possible that not all
1776b7e50089Smaybee 	 * of these properties were initialized during the dumpify process
1777b7e50089Smaybee 	 * (i.e. error during zvol_dump_init).
1778b7e50089Smaybee 	 */
1779b7e50089Smaybee 
1780e7cbe64fSgw 	tx = dmu_tx_create(os);
1781e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1782e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
1783e7cbe64fSgw 	if (error) {
1784e7cbe64fSgw 		dmu_tx_abort(tx);
1785e7cbe64fSgw 		return (error);
1786e7cbe64fSgw 	}
1787b7e50089Smaybee 	(void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx);
1788b7e50089Smaybee 	dmu_tx_commit(tx);
1789e7cbe64fSgw 
1790e7cbe64fSgw 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1791e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum);
1792e7cbe64fSgw 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1793e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress);
1794e7cbe64fSgw 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1795e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv);
179688b7b0f2SMatthew Ahrens 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
179788b7b0f2SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs);
1798e7cbe64fSgw 
1799e7cbe64fSgw 	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1800e7cbe64fSgw 	(void) nvlist_add_uint64(nv,
1801e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum);
1802e7cbe64fSgw 	(void) nvlist_add_uint64(nv,
1803e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress);
1804e7cbe64fSgw 	(void) nvlist_add_uint64(nv,
1805e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv);
18068d265e66SGeorge Wilson 	if (version >= SPA_VERSION_DEDUP &&
18078d265e66SGeorge Wilson 	    zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
18088d265e66SGeorge Wilson 	    zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, &dedup) == 0) {
18098d265e66SGeorge Wilson 		(void) nvlist_add_uint64(nv,
18108d265e66SGeorge Wilson 		    zfs_prop_to_name(ZFS_PROP_DEDUP), dedup);
18118d265e66SGeorge Wilson 	}
181292241e0bSTom Erickson 	(void) zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
181392241e0bSTom Erickson 	    nv, NULL);
1814e7cbe64fSgw 	nvlist_free(nv);
1815e7cbe64fSgw 
1816b7e50089Smaybee 	zvol_free_extents(zv);
1817b7e50089Smaybee 	zv->zv_flags &= ~ZVOL_DUMPIFIED;
1818b7e50089Smaybee 	(void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END);
1819681d9761SEric Taylor 	/* wait for dmu_free_long_range to actually free the blocks */
1820681d9761SEric Taylor 	txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
1821681d9761SEric Taylor 	tx = dmu_tx_create(os);
1822681d9761SEric Taylor 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
1823681d9761SEric Taylor 	error = dmu_tx_assign(tx, TXG_WAIT);
1824681d9761SEric Taylor 	if (error) {
1825681d9761SEric Taylor 		dmu_tx_abort(tx);
1826681d9761SEric Taylor 		return (error);
1827681d9761SEric Taylor 	}
1828b24ab676SJeff Bonwick 	if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0)
1829b24ab676SJeff Bonwick 		zv->zv_volblocksize = vbs;
1830681d9761SEric Taylor 	dmu_tx_commit(tx);
1831b7e50089Smaybee 
1832e7cbe64fSgw 	return (0);
1833e7cbe64fSgw }
1834