xref: /illumos-gate/usr/src/uts/common/fs/zfs/zvol.c (revision 3fb517f7)
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 
2555da60b9SMark J Musante /* Portions Copyright 2010 Robert Milkowski */
2655da60b9SMark J Musante 
27fa9e4066Sahrens /*
28fa9e4066Sahrens  * ZFS volume emulation driver.
29fa9e4066Sahrens  *
30fa9e4066Sahrens  * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes.
31fa9e4066Sahrens  * Volumes are accessed through the symbolic links named:
32fa9e4066Sahrens  *
33fa9e4066Sahrens  * /dev/zvol/dsk/<pool_name>/<dataset_name>
34fa9e4066Sahrens  * /dev/zvol/rdsk/<pool_name>/<dataset_name>
35fa9e4066Sahrens  *
36681d9761SEric Taylor  * These links are created by the /dev filesystem (sdev_zvolops.c).
37fa9e4066Sahrens  * Volumes are persistent through reboot.  No user command needs to be
38fa9e4066Sahrens  * run before opening and using a device.
39fa9e4066Sahrens  */
40fa9e4066Sahrens 
41fa9e4066Sahrens #include <sys/types.h>
42fa9e4066Sahrens #include <sys/param.h>
43fa9e4066Sahrens #include <sys/errno.h>
44fa9e4066Sahrens #include <sys/uio.h>
45fa9e4066Sahrens #include <sys/buf.h>
46fa9e4066Sahrens #include <sys/modctl.h>
47fa9e4066Sahrens #include <sys/open.h>
48fa9e4066Sahrens #include <sys/kmem.h>
49fa9e4066Sahrens #include <sys/conf.h>
50fa9e4066Sahrens #include <sys/cmn_err.h>
51fa9e4066Sahrens #include <sys/stat.h>
52fa9e4066Sahrens #include <sys/zap.h>
53fa9e4066Sahrens #include <sys/spa.h>
54fa9e4066Sahrens #include <sys/zio.h>
55e7cbe64fSgw #include <sys/dmu_traverse.h>
56e7cbe64fSgw #include <sys/dnode.h>
57e7cbe64fSgw #include <sys/dsl_dataset.h>
58fa9e4066Sahrens #include <sys/dsl_prop.h>
59fa9e4066Sahrens #include <sys/dkio.h>
60fa9e4066Sahrens #include <sys/efi_partition.h>
61fa9e4066Sahrens #include <sys/byteorder.h>
62fa9e4066Sahrens #include <sys/pathname.h>
63fa9e4066Sahrens #include <sys/ddi.h>
64fa9e4066Sahrens #include <sys/sunddi.h>
65fa9e4066Sahrens #include <sys/crc32.h>
66fa9e4066Sahrens #include <sys/dirent.h>
67fa9e4066Sahrens #include <sys/policy.h>
68fa9e4066Sahrens #include <sys/fs/zfs.h>
69fa9e4066Sahrens #include <sys/zfs_ioctl.h>
70fa9e4066Sahrens #include <sys/mkdev.h>
7122ac5be4Sperrin #include <sys/zil.h>
72c5c6ffa0Smaybee #include <sys/refcount.h>
73c2e6a7d6Sperrin #include <sys/zfs_znode.h>
74c2e6a7d6Sperrin #include <sys/zfs_rlock.h>
75e7cbe64fSgw #include <sys/vdev_disk.h>
76e7cbe64fSgw #include <sys/vdev_impl.h>
77e7cbe64fSgw #include <sys/zvol.h>
78e7cbe64fSgw #include <sys/dumphdr.h>
791209a471SNeil Perrin #include <sys/zil_impl.h>
80fa9e4066Sahrens 
81fa9e4066Sahrens #include "zfs_namecheck.h"
82fa9e4066Sahrens 
83fa9e4066Sahrens static void *zvol_state;
84503ad85cSMatthew Ahrens static char *zvol_tag = "zvol_tag";
85fa9e4066Sahrens 
86e7cbe64fSgw #define	ZVOL_DUMPSIZE		"dumpsize"
87e7cbe64fSgw 
88fa9e4066Sahrens /*
89fa9e4066Sahrens  * This lock protects the zvol_state structure from being modified
90fa9e4066Sahrens  * while it's being used, e.g. an open that comes in before a create
91fa9e4066Sahrens  * finishes.  It also protects temporary opens of the dataset so that,
92fa9e4066Sahrens  * e.g., an open doesn't get a spurious EBUSY.
93fa9e4066Sahrens  */
94fa9e4066Sahrens static kmutex_t zvol_state_lock;
95fa9e4066Sahrens static uint32_t zvol_minors;
96fa9e4066Sahrens 
97e7cbe64fSgw typedef struct zvol_extent {
9888b7b0f2SMatthew Ahrens 	list_node_t	ze_node;
99e7cbe64fSgw 	dva_t		ze_dva;		/* dva associated with this extent */
10088b7b0f2SMatthew Ahrens 	uint64_t	ze_nblks;	/* number of blocks in extent */
101e7cbe64fSgw } zvol_extent_t;
102e7cbe64fSgw 
103fa9e4066Sahrens /*
104fa9e4066Sahrens  * The in-core state of each volume.
105fa9e4066Sahrens  */
106fa9e4066Sahrens typedef struct zvol_state {
107fa9e4066Sahrens 	char		zv_name[MAXPATHLEN]; /* pool/dd name */
108fa9e4066Sahrens 	uint64_t	zv_volsize;	/* amount of space we advertise */
10967bd71c6Sperrin 	uint64_t	zv_volblocksize; /* volume block size */
110fa9e4066Sahrens 	minor_t		zv_minor;	/* minor number */
111fa9e4066Sahrens 	uint8_t		zv_min_bs;	/* minimum addressable block shift */
112701f66c4SEric Taylor 	uint8_t		zv_flags;	/* readonly, dumpified, etc. */
113fa9e4066Sahrens 	objset_t	*zv_objset;	/* objset handle */
114fa9e4066Sahrens 	uint32_t	zv_open_count[OTYPCNT];	/* open counts */
115fa9e4066Sahrens 	uint32_t	zv_total_opens;	/* total open count */
11622ac5be4Sperrin 	zilog_t		*zv_zilog;	/* ZIL handle */
11788b7b0f2SMatthew Ahrens 	list_t		zv_extents;	/* List of extents for dump */
118c2e6a7d6Sperrin 	znode_t		zv_znode;	/* for range locking */
11994d1a210STim Haley 	dmu_buf_t	*zv_dbuf;	/* bonus handle */
120fa9e4066Sahrens } zvol_state_t;
121fa9e4066Sahrens 
122e7cbe64fSgw /*
123e7cbe64fSgw  * zvol specific flags
124e7cbe64fSgw  */
125e7cbe64fSgw #define	ZVOL_RDONLY	0x1
126e7cbe64fSgw #define	ZVOL_DUMPIFIED	0x2
127c7f714e2SEric Taylor #define	ZVOL_EXCL	0x4
128701f66c4SEric Taylor #define	ZVOL_WCE	0x8
129e7cbe64fSgw 
13067bd71c6Sperrin /*
13167bd71c6Sperrin  * zvol maximum transfer in one DMU tx.
13267bd71c6Sperrin  */
13367bd71c6Sperrin int zvol_maxphys = DMU_MAX_ACCESS/2;
13467bd71c6Sperrin 
13592241e0bSTom Erickson extern int zfs_set_prop_nvlist(const char *, zprop_source_t,
13692241e0bSTom Erickson     nvlist_t *, nvlist_t **);
137681d9761SEric Taylor static int zvol_remove_zv(zvol_state_t *);
138feb08c6bSbillm static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio);
139e7cbe64fSgw static int zvol_dumpify(zvol_state_t *zv);
140e7cbe64fSgw static int zvol_dump_fini(zvol_state_t *zv);
141e7cbe64fSgw static int zvol_dump_init(zvol_state_t *zv, boolean_t resize);
14267bd71c6Sperrin 
143fa9e4066Sahrens static void
144681d9761SEric Taylor zvol_size_changed(uint64_t volsize, major_t maj, minor_t min)
145fa9e4066Sahrens {
146681d9761SEric Taylor 	dev_t dev = makedevice(maj, min);
147fa9e4066Sahrens 
148fa9e4066Sahrens 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
149681d9761SEric Taylor 	    "Size", volsize) == DDI_SUCCESS);
150fa9e4066Sahrens 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
151681d9761SEric Taylor 	    "Nblocks", lbtodb(volsize)) == DDI_SUCCESS);
152e7cbe64fSgw 
153e7cbe64fSgw 	/* Notify specfs to invalidate the cached size */
154e7cbe64fSgw 	spec_size_invalidate(dev, VBLK);
155e7cbe64fSgw 	spec_size_invalidate(dev, VCHR);
156fa9e4066Sahrens }
157fa9e4066Sahrens 
158fa9e4066Sahrens int
159e9dbad6fSeschrock zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
160fa9e4066Sahrens {
161e9dbad6fSeschrock 	if (volsize == 0)
162fa9e4066Sahrens 		return (EINVAL);
163fa9e4066Sahrens 
164e9dbad6fSeschrock 	if (volsize % blocksize != 0)
1655c5460e9Seschrock 		return (EINVAL);
1665c5460e9Seschrock 
167fa9e4066Sahrens #ifdef _ILP32
168e9dbad6fSeschrock 	if (volsize - 1 > SPEC_MAXOFFSET_T)
169fa9e4066Sahrens 		return (EOVERFLOW);
170fa9e4066Sahrens #endif
171fa9e4066Sahrens 	return (0);
172fa9e4066Sahrens }
173fa9e4066Sahrens 
174fa9e4066Sahrens int
175e9dbad6fSeschrock zvol_check_volblocksize(uint64_t volblocksize)
176fa9e4066Sahrens {
177e9dbad6fSeschrock 	if (volblocksize < SPA_MINBLOCKSIZE ||
178e9dbad6fSeschrock 	    volblocksize > SPA_MAXBLOCKSIZE ||
179e9dbad6fSeschrock 	    !ISP2(volblocksize))
180fa9e4066Sahrens 		return (EDOM);
181fa9e4066Sahrens 
182fa9e4066Sahrens 	return (0);
183fa9e4066Sahrens }
184fa9e4066Sahrens 
185fa9e4066Sahrens int
186a2eea2e1Sahrens zvol_get_stats(objset_t *os, nvlist_t *nv)
187fa9e4066Sahrens {
188fa9e4066Sahrens 	int error;
189fa9e4066Sahrens 	dmu_object_info_t doi;
190a2eea2e1Sahrens 	uint64_t val;
191fa9e4066Sahrens 
192a2eea2e1Sahrens 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
193fa9e4066Sahrens 	if (error)
194fa9e4066Sahrens 		return (error);
195fa9e4066Sahrens 
196a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
197a2eea2e1Sahrens 
198fa9e4066Sahrens 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
199fa9e4066Sahrens 
200a2eea2e1Sahrens 	if (error == 0) {
201a2eea2e1Sahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
202a2eea2e1Sahrens 		    doi.doi_data_block_size);
203a2eea2e1Sahrens 	}
204fa9e4066Sahrens 
205fa9e4066Sahrens 	return (error);
206fa9e4066Sahrens }
207fa9e4066Sahrens 
208fa9e4066Sahrens /*
209fa9e4066Sahrens  * Find a free minor number.
210fa9e4066Sahrens  */
211fa9e4066Sahrens static minor_t
212fa9e4066Sahrens zvol_minor_alloc(void)
213fa9e4066Sahrens {
214fa9e4066Sahrens 	minor_t minor;
215fa9e4066Sahrens 
216fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&zvol_state_lock));
217fa9e4066Sahrens 
218fa9e4066Sahrens 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++)
219fa9e4066Sahrens 		if (ddi_get_soft_state(zvol_state, minor) == NULL)
220fa9e4066Sahrens 			return (minor);
221fa9e4066Sahrens 
222fa9e4066Sahrens 	return (0);
223fa9e4066Sahrens }
224fa9e4066Sahrens 
225fa9e4066Sahrens static zvol_state_t *
226e9dbad6fSeschrock zvol_minor_lookup(const char *name)
227fa9e4066Sahrens {
228fa9e4066Sahrens 	minor_t minor;
229fa9e4066Sahrens 	zvol_state_t *zv;
230fa9e4066Sahrens 
231fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&zvol_state_lock));
232fa9e4066Sahrens 
233fa9e4066Sahrens 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) {
234fa9e4066Sahrens 		zv = ddi_get_soft_state(zvol_state, minor);
235fa9e4066Sahrens 		if (zv == NULL)
236fa9e4066Sahrens 			continue;
237fa9e4066Sahrens 		if (strcmp(zv->zv_name, name) == 0)
238f80ce222SChris Kirby 			return (zv);
239fa9e4066Sahrens 	}
240fa9e4066Sahrens 
241f80ce222SChris Kirby 	return (NULL);
242fa9e4066Sahrens }
243fa9e4066Sahrens 
244e7cbe64fSgw /* extent mapping arg */
245e7cbe64fSgw struct maparg {
24688b7b0f2SMatthew Ahrens 	zvol_state_t	*ma_zv;
24788b7b0f2SMatthew Ahrens 	uint64_t	ma_blks;
248e7cbe64fSgw };
249e7cbe64fSgw 
250e7cbe64fSgw /*ARGSUSED*/
251e7cbe64fSgw static int
2523f9d6ad7SLin Ling zvol_map_block(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, arc_buf_t *pbuf,
253b24ab676SJeff Bonwick     const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
254e7cbe64fSgw {
25588b7b0f2SMatthew Ahrens 	struct maparg *ma = arg;
25688b7b0f2SMatthew Ahrens 	zvol_extent_t *ze;
25788b7b0f2SMatthew Ahrens 	int bs = ma->ma_zv->zv_volblocksize;
258e7cbe64fSgw 
25988b7b0f2SMatthew Ahrens 	if (bp == NULL || zb->zb_object != ZVOL_OBJ || zb->zb_level != 0)
26088b7b0f2SMatthew Ahrens 		return (0);
261e7cbe64fSgw 
26288b7b0f2SMatthew Ahrens 	VERIFY3U(ma->ma_blks, ==, zb->zb_blkid);
26388b7b0f2SMatthew Ahrens 	ma->ma_blks++;
264e7cbe64fSgw 
26588b7b0f2SMatthew Ahrens 	/* Abort immediately if we have encountered gang blocks */
26688b7b0f2SMatthew Ahrens 	if (BP_IS_GANG(bp))
26788b7b0f2SMatthew Ahrens 		return (EFRAGS);
268e7cbe64fSgw 
26988b7b0f2SMatthew Ahrens 	/*
27088b7b0f2SMatthew Ahrens 	 * See if the block is at the end of the previous extent.
27188b7b0f2SMatthew Ahrens 	 */
27288b7b0f2SMatthew Ahrens 	ze = list_tail(&ma->ma_zv->zv_extents);
27388b7b0f2SMatthew Ahrens 	if (ze &&
27488b7b0f2SMatthew Ahrens 	    DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) &&
27588b7b0f2SMatthew Ahrens 	    DVA_GET_OFFSET(BP_IDENTITY(bp)) ==
27688b7b0f2SMatthew Ahrens 	    DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) {
27788b7b0f2SMatthew Ahrens 		ze->ze_nblks++;
27888b7b0f2SMatthew Ahrens 		return (0);
279e7cbe64fSgw 	}
280e7cbe64fSgw 
28188b7b0f2SMatthew Ahrens 	dprintf_bp(bp, "%s", "next blkptr:");
282e7cbe64fSgw 
28388b7b0f2SMatthew Ahrens 	/* start a new extent */
28488b7b0f2SMatthew Ahrens 	ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP);
28588b7b0f2SMatthew Ahrens 	ze->ze_dva = bp->blk_dva[0];	/* structure assignment */
28688b7b0f2SMatthew Ahrens 	ze->ze_nblks = 1;
28788b7b0f2SMatthew Ahrens 	list_insert_tail(&ma->ma_zv->zv_extents, ze);
28888b7b0f2SMatthew Ahrens 	return (0);
28988b7b0f2SMatthew Ahrens }
290e7cbe64fSgw 
29188b7b0f2SMatthew Ahrens static void
29288b7b0f2SMatthew Ahrens zvol_free_extents(zvol_state_t *zv)
29388b7b0f2SMatthew Ahrens {
29488b7b0f2SMatthew Ahrens 	zvol_extent_t *ze;
295e7cbe64fSgw 
29688b7b0f2SMatthew Ahrens 	while (ze = list_head(&zv->zv_extents)) {
29788b7b0f2SMatthew Ahrens 		list_remove(&zv->zv_extents, ze);
29888b7b0f2SMatthew Ahrens 		kmem_free(ze, sizeof (zvol_extent_t));
299e7cbe64fSgw 	}
30088b7b0f2SMatthew Ahrens }
301e7cbe64fSgw 
30288b7b0f2SMatthew Ahrens static int
30388b7b0f2SMatthew Ahrens zvol_get_lbas(zvol_state_t *zv)
30488b7b0f2SMatthew Ahrens {
3053adc9019SEric Taylor 	objset_t *os = zv->zv_objset;
30688b7b0f2SMatthew Ahrens 	struct maparg	ma;
30788b7b0f2SMatthew Ahrens 	int		err;
30888b7b0f2SMatthew Ahrens 
30988b7b0f2SMatthew Ahrens 	ma.ma_zv = zv;
31088b7b0f2SMatthew Ahrens 	ma.ma_blks = 0;
31188b7b0f2SMatthew Ahrens 	zvol_free_extents(zv);
31288b7b0f2SMatthew Ahrens 
3133adc9019SEric Taylor 	/* commit any in-flight changes before traversing the dataset */
3143adc9019SEric Taylor 	txg_wait_synced(dmu_objset_pool(os), 0);
3153adc9019SEric Taylor 	err = traverse_dataset(dmu_objset_ds(os), 0,
31688b7b0f2SMatthew Ahrens 	    TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma);
31788b7b0f2SMatthew Ahrens 	if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) {
31888b7b0f2SMatthew Ahrens 		zvol_free_extents(zv);
31988b7b0f2SMatthew Ahrens 		return (err ? err : EIO);
320e7cbe64fSgw 	}
32188b7b0f2SMatthew Ahrens 
322e7cbe64fSgw 	return (0);
323e7cbe64fSgw }
324e7cbe64fSgw 
325ecd6cf80Smarks /* ARGSUSED */
326fa9e4066Sahrens void
327ecd6cf80Smarks zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
328fa9e4066Sahrens {
329da6c28aaSamw 	zfs_creat_t *zct = arg;
330da6c28aaSamw 	nvlist_t *nvprops = zct->zct_props;
331fa9e4066Sahrens 	int error;
332e9dbad6fSeschrock 	uint64_t volblocksize, volsize;
333fa9e4066Sahrens 
334ecd6cf80Smarks 	VERIFY(nvlist_lookup_uint64(nvprops,
335e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
336ecd6cf80Smarks 	if (nvlist_lookup_uint64(nvprops,
337e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
338e9dbad6fSeschrock 		volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
339e9dbad6fSeschrock 
340e9dbad6fSeschrock 	/*
341e7cbe64fSgw 	 * These properties must be removed from the list so the generic
342e9dbad6fSeschrock 	 * property setting step won't apply to them.
343e9dbad6fSeschrock 	 */
344ecd6cf80Smarks 	VERIFY(nvlist_remove_all(nvprops,
345e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
346ecd6cf80Smarks 	(void) nvlist_remove_all(nvprops,
347e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
348e9dbad6fSeschrock 
349e9dbad6fSeschrock 	error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
350fa9e4066Sahrens 	    DMU_OT_NONE, 0, tx);
351fa9e4066Sahrens 	ASSERT(error == 0);
352fa9e4066Sahrens 
353fa9e4066Sahrens 	error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
354fa9e4066Sahrens 	    DMU_OT_NONE, 0, tx);
355fa9e4066Sahrens 	ASSERT(error == 0);
356fa9e4066Sahrens 
357e9dbad6fSeschrock 	error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
358fa9e4066Sahrens 	ASSERT(error == 0);
359fa9e4066Sahrens }
360fa9e4066Sahrens 
36122ac5be4Sperrin /*
36222ac5be4Sperrin  * Replay a TX_WRITE ZIL transaction that didn't get committed
36322ac5be4Sperrin  * after a system failure
36422ac5be4Sperrin  */
36522ac5be4Sperrin static int
36622ac5be4Sperrin zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
36722ac5be4Sperrin {
36822ac5be4Sperrin 	objset_t *os = zv->zv_objset;
36922ac5be4Sperrin 	char *data = (char *)(lr + 1);	/* data follows lr_write_t */
370b24ab676SJeff Bonwick 	uint64_t offset, length;
37122ac5be4Sperrin 	dmu_tx_t *tx;
37222ac5be4Sperrin 	int error;
37322ac5be4Sperrin 
37422ac5be4Sperrin 	if (byteswap)
37522ac5be4Sperrin 		byteswap_uint64_array(lr, sizeof (*lr));
37622ac5be4Sperrin 
377b24ab676SJeff Bonwick 	offset = lr->lr_offset;
378b24ab676SJeff Bonwick 	length = lr->lr_length;
379b24ab676SJeff Bonwick 
380b24ab676SJeff Bonwick 	/* If it's a dmu_sync() block, write the whole block */
381b24ab676SJeff Bonwick 	if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
382b24ab676SJeff Bonwick 		uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
383b24ab676SJeff Bonwick 		if (length < blocksize) {
384b24ab676SJeff Bonwick 			offset -= offset % blocksize;
385b24ab676SJeff Bonwick 			length = blocksize;
386b24ab676SJeff Bonwick 		}
387b24ab676SJeff Bonwick 	}
388975c32a0SNeil Perrin 
38922ac5be4Sperrin 	tx = dmu_tx_create(os);
390b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length);
3911209a471SNeil Perrin 	error = dmu_tx_assign(tx, TXG_WAIT);
39222ac5be4Sperrin 	if (error) {
39322ac5be4Sperrin 		dmu_tx_abort(tx);
39422ac5be4Sperrin 	} else {
395b24ab676SJeff Bonwick 		dmu_write(os, ZVOL_OBJ, offset, length, data, tx);
39622ac5be4Sperrin 		dmu_tx_commit(tx);
39722ac5be4Sperrin 	}
39822ac5be4Sperrin 
39922ac5be4Sperrin 	return (error);
40022ac5be4Sperrin }
40122ac5be4Sperrin 
40222ac5be4Sperrin /* ARGSUSED */
40322ac5be4Sperrin static int
40422ac5be4Sperrin zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
40522ac5be4Sperrin {
40622ac5be4Sperrin 	return (ENOTSUP);
40722ac5be4Sperrin }
40822ac5be4Sperrin 
40922ac5be4Sperrin /*
41022ac5be4Sperrin  * Callback vectors for replaying records.
41122ac5be4Sperrin  * Only TX_WRITE is needed for zvol.
41222ac5be4Sperrin  */
41322ac5be4Sperrin zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = {
41422ac5be4Sperrin 	zvol_replay_err,	/* 0 no such transaction type */
41522ac5be4Sperrin 	zvol_replay_err,	/* TX_CREATE */
41622ac5be4Sperrin 	zvol_replay_err,	/* TX_MKDIR */
41722ac5be4Sperrin 	zvol_replay_err,	/* TX_MKXATTR */
41822ac5be4Sperrin 	zvol_replay_err,	/* TX_SYMLINK */
41922ac5be4Sperrin 	zvol_replay_err,	/* TX_REMOVE */
42022ac5be4Sperrin 	zvol_replay_err,	/* TX_RMDIR */
42122ac5be4Sperrin 	zvol_replay_err,	/* TX_LINK */
42222ac5be4Sperrin 	zvol_replay_err,	/* TX_RENAME */
42322ac5be4Sperrin 	zvol_replay_write,	/* TX_WRITE */
42422ac5be4Sperrin 	zvol_replay_err,	/* TX_TRUNCATE */
42522ac5be4Sperrin 	zvol_replay_err,	/* TX_SETATTR */
42622ac5be4Sperrin 	zvol_replay_err,	/* TX_ACL */
427975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_CREATE_ACL */
428975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_CREATE_ATTR */
429975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_CREATE_ACL_ATTR */
430975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_MKDIR_ACL */
431975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_MKDIR_ATTR */
432975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_MKDIR_ACL_ATTR */
433975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_WRITE2 */
43422ac5be4Sperrin };
43522ac5be4Sperrin 
436681d9761SEric Taylor int
437681d9761SEric Taylor zvol_name2minor(const char *name, minor_t *minor)
438681d9761SEric Taylor {
439681d9761SEric Taylor 	zvol_state_t *zv;
440681d9761SEric Taylor 
441681d9761SEric Taylor 	mutex_enter(&zvol_state_lock);
442681d9761SEric Taylor 	zv = zvol_minor_lookup(name);
443681d9761SEric Taylor 	if (minor && zv)
444681d9761SEric Taylor 		*minor = zv->zv_minor;
445681d9761SEric Taylor 	mutex_exit(&zvol_state_lock);
446681d9761SEric Taylor 	return (zv ? 0 : -1);
447681d9761SEric Taylor }
448681d9761SEric Taylor 
449e7cbe64fSgw /*
450e7cbe64fSgw  * Create a minor node (plus a whole lot more) for the specified volume.
451fa9e4066Sahrens  */
452fa9e4066Sahrens int
453681d9761SEric Taylor zvol_create_minor(const char *name)
454fa9e4066Sahrens {
455fa9e4066Sahrens 	zvol_state_t *zv;
456fa9e4066Sahrens 	objset_t *os;
45767bd71c6Sperrin 	dmu_object_info_t doi;
458fa9e4066Sahrens 	minor_t minor = 0;
459fa9e4066Sahrens 	char chrbuf[30], blkbuf[30];
460fa9e4066Sahrens 	int error;
461fa9e4066Sahrens 
462fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
463fa9e4066Sahrens 
4641195e687SMark J Musante 	if (zvol_minor_lookup(name) != NULL) {
465fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
466fa9e4066Sahrens 		return (EEXIST);
467fa9e4066Sahrens 	}
468fa9e4066Sahrens 
469503ad85cSMatthew Ahrens 	/* lie and say we're read-only */
470503ad85cSMatthew Ahrens 	error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, zvol_tag, &os);
471fa9e4066Sahrens 
472fa9e4066Sahrens 	if (error) {
473fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
474fa9e4066Sahrens 		return (error);
475fa9e4066Sahrens 	}
476fa9e4066Sahrens 
477681d9761SEric Taylor 	if ((minor = zvol_minor_alloc()) == 0) {
478503ad85cSMatthew Ahrens 		dmu_objset_disown(os, zvol_tag);
479fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
480fa9e4066Sahrens 		return (ENXIO);
481fa9e4066Sahrens 	}
482fa9e4066Sahrens 
483fa9e4066Sahrens 	if (ddi_soft_state_zalloc(zvol_state, minor) != DDI_SUCCESS) {
484503ad85cSMatthew Ahrens 		dmu_objset_disown(os, zvol_tag);
485fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
486fa9e4066Sahrens 		return (EAGAIN);
487fa9e4066Sahrens 	}
488e9dbad6fSeschrock 	(void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME,
489e9dbad6fSeschrock 	    (char *)name);
490fa9e4066Sahrens 
491681d9761SEric Taylor 	(void) snprintf(chrbuf, sizeof (chrbuf), "%u,raw", minor);
492fa9e4066Sahrens 
493fa9e4066Sahrens 	if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR,
494fa9e4066Sahrens 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
495fa9e4066Sahrens 		ddi_soft_state_free(zvol_state, minor);
496503ad85cSMatthew Ahrens 		dmu_objset_disown(os, zvol_tag);
497fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
498fa9e4066Sahrens 		return (EAGAIN);
499fa9e4066Sahrens 	}
500fa9e4066Sahrens 
501681d9761SEric Taylor 	(void) snprintf(blkbuf, sizeof (blkbuf), "%u", minor);
502fa9e4066Sahrens 
503fa9e4066Sahrens 	if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK,
504fa9e4066Sahrens 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
505fa9e4066Sahrens 		ddi_remove_minor_node(zfs_dip, chrbuf);
506fa9e4066Sahrens 		ddi_soft_state_free(zvol_state, minor);
507503ad85cSMatthew Ahrens 		dmu_objset_disown(os, zvol_tag);
508fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
509fa9e4066Sahrens 		return (EAGAIN);
510fa9e4066Sahrens 	}
511fa9e4066Sahrens 
512fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
513fa9e4066Sahrens 
514681d9761SEric Taylor 	(void) strlcpy(zv->zv_name, name, MAXPATHLEN);
515fa9e4066Sahrens 	zv->zv_min_bs = DEV_BSHIFT;
516fa9e4066Sahrens 	zv->zv_minor = minor;
517fa9e4066Sahrens 	zv->zv_objset = os;
518681d9761SEric Taylor 	if (dmu_objset_is_snapshot(os))
519681d9761SEric Taylor 		zv->zv_flags |= ZVOL_RDONLY;
520c2e6a7d6Sperrin 	mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
521c2e6a7d6Sperrin 	avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
522c2e6a7d6Sperrin 	    sizeof (rl_t), offsetof(rl_t, r_node));
52388b7b0f2SMatthew Ahrens 	list_create(&zv->zv_extents, sizeof (zvol_extent_t),
52488b7b0f2SMatthew Ahrens 	    offsetof(zvol_extent_t, ze_node));
52567bd71c6Sperrin 	/* get and cache the blocksize */
52667bd71c6Sperrin 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
52767bd71c6Sperrin 	ASSERT(error == 0);
52867bd71c6Sperrin 	zv->zv_volblocksize = doi.doi_data_block_size;
52922ac5be4Sperrin 
53055da60b9SMark J Musante 	if (zil_replay_disable)
53155da60b9SMark J Musante 		zil_destroy(dmu_objset_zil(os), B_FALSE);
53255da60b9SMark J Musante 	else
53355da60b9SMark J Musante 		zil_replay(os, zv, zvol_replay_vector);
534681d9761SEric Taylor 	dmu_objset_disown(os, zvol_tag);
535681d9761SEric Taylor 	zv->zv_objset = NULL;
536fa9e4066Sahrens 
537fa9e4066Sahrens 	zvol_minors++;
538fa9e4066Sahrens 
539fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
540fa9e4066Sahrens 
541fa9e4066Sahrens 	return (0);
542fa9e4066Sahrens }
543fa9e4066Sahrens 
544fa9e4066Sahrens /*
545fa9e4066Sahrens  * Remove minor node for the specified volume.
546fa9e4066Sahrens  */
547681d9761SEric Taylor static int
548681d9761SEric Taylor zvol_remove_zv(zvol_state_t *zv)
549681d9761SEric Taylor {
550681d9761SEric Taylor 	char nmbuf[20];
551681d9761SEric Taylor 
552681d9761SEric Taylor 	ASSERT(MUTEX_HELD(&zvol_state_lock));
553681d9761SEric Taylor 	if (zv->zv_total_opens != 0)
554681d9761SEric Taylor 		return (EBUSY);
555681d9761SEric Taylor 
556681d9761SEric Taylor 	(void) snprintf(nmbuf, sizeof (nmbuf), "%u,raw", zv->zv_minor);
557681d9761SEric Taylor 	ddi_remove_minor_node(zfs_dip, nmbuf);
558681d9761SEric Taylor 
559681d9761SEric Taylor 	(void) snprintf(nmbuf, sizeof (nmbuf), "%u", zv->zv_minor);
560681d9761SEric Taylor 	ddi_remove_minor_node(zfs_dip, nmbuf);
561681d9761SEric Taylor 
562681d9761SEric Taylor 	avl_destroy(&zv->zv_znode.z_range_avl);
563681d9761SEric Taylor 	mutex_destroy(&zv->zv_znode.z_range_lock);
564681d9761SEric Taylor 
565681d9761SEric Taylor 	ddi_soft_state_free(zvol_state, zv->zv_minor);
566681d9761SEric Taylor 
567681d9761SEric Taylor 	zvol_minors--;
568681d9761SEric Taylor 	return (0);
569681d9761SEric Taylor }
570681d9761SEric Taylor 
571fa9e4066Sahrens int
572e9dbad6fSeschrock zvol_remove_minor(const char *name)
573fa9e4066Sahrens {
574fa9e4066Sahrens 	zvol_state_t *zv;
575681d9761SEric Taylor 	int rc;
576fa9e4066Sahrens 
577fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
578e9dbad6fSeschrock 	if ((zv = zvol_minor_lookup(name)) == NULL) {
579fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
580fa9e4066Sahrens 		return (ENXIO);
581fa9e4066Sahrens 	}
582681d9761SEric Taylor 	rc = zvol_remove_zv(zv);
583681d9761SEric Taylor 	mutex_exit(&zvol_state_lock);
584681d9761SEric Taylor 	return (rc);
585681d9761SEric Taylor }
586fa9e4066Sahrens 
587681d9761SEric Taylor int
588681d9761SEric Taylor zvol_first_open(zvol_state_t *zv)
589681d9761SEric Taylor {
590681d9761SEric Taylor 	objset_t *os;
591681d9761SEric Taylor 	uint64_t volsize;
592681d9761SEric Taylor 	int error;
593681d9761SEric Taylor 	uint64_t readonly;
594fa9e4066Sahrens 
595681d9761SEric Taylor 	/* lie and say we're read-only */
596681d9761SEric Taylor 	error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, B_TRUE,
597681d9761SEric Taylor 	    zvol_tag, &os);
598681d9761SEric Taylor 	if (error)
599681d9761SEric Taylor 		return (error);
600fa9e4066Sahrens 
601681d9761SEric Taylor 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
602681d9761SEric Taylor 	if (error) {
603681d9761SEric Taylor 		ASSERT(error == 0);
604681d9761SEric Taylor 		dmu_objset_disown(os, zvol_tag);
605681d9761SEric Taylor 		return (error);
606681d9761SEric Taylor 	}
607681d9761SEric Taylor 	zv->zv_objset = os;
60894d1a210STim Haley 	error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf);
60994d1a210STim Haley 	if (error) {
61094d1a210STim Haley 		dmu_objset_disown(os, zvol_tag);
61194d1a210STim Haley 		return (error);
61294d1a210STim Haley 	}
613681d9761SEric Taylor 	zv->zv_volsize = volsize;
614681d9761SEric Taylor 	zv->zv_zilog = zil_open(os, zvol_get_data);
615681d9761SEric Taylor 	zvol_size_changed(zv->zv_volsize, ddi_driver_major(zfs_dip),
616681d9761SEric Taylor 	    zv->zv_minor);
617fa9e4066Sahrens 
618681d9761SEric Taylor 	VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &readonly,
619681d9761SEric Taylor 	    NULL) == 0);
620681d9761SEric Taylor 	if (readonly || dmu_objset_is_snapshot(os))
621681d9761SEric Taylor 		zv->zv_flags |= ZVOL_RDONLY;
622681d9761SEric Taylor 	else
623681d9761SEric Taylor 		zv->zv_flags &= ~ZVOL_RDONLY;
624681d9761SEric Taylor 	return (error);
625681d9761SEric Taylor }
626fa9e4066Sahrens 
627681d9761SEric Taylor void
628681d9761SEric Taylor zvol_last_close(zvol_state_t *zv)
629681d9761SEric Taylor {
63022ac5be4Sperrin 	zil_close(zv->zv_zilog);
63122ac5be4Sperrin 	zv->zv_zilog = NULL;
63294d1a210STim Haley 	dmu_buf_rele(zv->zv_dbuf, zvol_tag);
63394d1a210STim Haley 	zv->zv_dbuf = NULL;
634503ad85cSMatthew Ahrens 	dmu_objset_disown(zv->zv_objset, zvol_tag);
635fa9e4066Sahrens 	zv->zv_objset = NULL;
636fa9e4066Sahrens }
637fa9e4066Sahrens 
638e7cbe64fSgw int
639e7cbe64fSgw zvol_prealloc(zvol_state_t *zv)
640e7cbe64fSgw {
641e7cbe64fSgw 	objset_t *os = zv->zv_objset;
642e7cbe64fSgw 	dmu_tx_t *tx;
643e7cbe64fSgw 	uint64_t refd, avail, usedobjs, availobjs;
644e7cbe64fSgw 	uint64_t resid = zv->zv_volsize;
645e7cbe64fSgw 	uint64_t off = 0;
646e7cbe64fSgw 
647e7cbe64fSgw 	/* Check the space usage before attempting to allocate the space */
648e7cbe64fSgw 	dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs);
649e7cbe64fSgw 	if (avail < zv->zv_volsize)
650e7cbe64fSgw 		return (ENOSPC);
651e7cbe64fSgw 
652e7cbe64fSgw 	/* Free old extents if they exist */
653e7cbe64fSgw 	zvol_free_extents(zv);
654e7cbe64fSgw 
655e7cbe64fSgw 	while (resid != 0) {
656e7cbe64fSgw 		int error;
657e7cbe64fSgw 		uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE);
658e7cbe64fSgw 
659e7cbe64fSgw 		tx = dmu_tx_create(os);
660e7cbe64fSgw 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
661e7cbe64fSgw 		error = dmu_tx_assign(tx, TXG_WAIT);
662e7cbe64fSgw 		if (error) {
663e7cbe64fSgw 			dmu_tx_abort(tx);
664cdb0ab79Smaybee 			(void) dmu_free_long_range(os, ZVOL_OBJ, 0, off);
665e7cbe64fSgw 			return (error);
666e7cbe64fSgw 		}
66782c9918fSTim Haley 		dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx);
668e7cbe64fSgw 		dmu_tx_commit(tx);
669e7cbe64fSgw 		off += bytes;
670e7cbe64fSgw 		resid -= bytes;
671e7cbe64fSgw 	}
672e7cbe64fSgw 	txg_wait_synced(dmu_objset_pool(os), 0);
673e7cbe64fSgw 
674e7cbe64fSgw 	return (0);
675e7cbe64fSgw }
676e7cbe64fSgw 
677e7cbe64fSgw int
678681d9761SEric Taylor zvol_update_volsize(objset_t *os, uint64_t volsize)
679e7cbe64fSgw {
680e7cbe64fSgw 	dmu_tx_t *tx;
681e7cbe64fSgw 	int error;
682e7cbe64fSgw 
683e7cbe64fSgw 	ASSERT(MUTEX_HELD(&zvol_state_lock));
684e7cbe64fSgw 
685681d9761SEric Taylor 	tx = dmu_tx_create(os);
686e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
687e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
688e7cbe64fSgw 	if (error) {
689e7cbe64fSgw 		dmu_tx_abort(tx);
690e7cbe64fSgw 		return (error);
691e7cbe64fSgw 	}
692e7cbe64fSgw 
693681d9761SEric Taylor 	error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
694e7cbe64fSgw 	    &volsize, tx);
695e7cbe64fSgw 	dmu_tx_commit(tx);
696e7cbe64fSgw 
697e7cbe64fSgw 	if (error == 0)
698681d9761SEric Taylor 		error = dmu_free_long_range(os,
699cdb0ab79Smaybee 		    ZVOL_OBJ, volsize, DMU_OBJECT_END);
700681d9761SEric Taylor 	return (error);
701681d9761SEric Taylor }
702e7cbe64fSgw 
703681d9761SEric Taylor void
704681d9761SEric Taylor zvol_remove_minors(const char *name)
705681d9761SEric Taylor {
706681d9761SEric Taylor 	zvol_state_t *zv;
707681d9761SEric Taylor 	char *namebuf;
708681d9761SEric Taylor 	minor_t minor;
709681d9761SEric Taylor 
710681d9761SEric Taylor 	namebuf = kmem_zalloc(strlen(name) + 2, KM_SLEEP);
711681d9761SEric Taylor 	(void) strncpy(namebuf, name, strlen(name));
712681d9761SEric Taylor 	(void) strcat(namebuf, "/");
713681d9761SEric Taylor 	mutex_enter(&zvol_state_lock);
714681d9761SEric Taylor 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) {
715681d9761SEric Taylor 
716681d9761SEric Taylor 		zv = ddi_get_soft_state(zvol_state, minor);
717681d9761SEric Taylor 		if (zv == NULL)
718681d9761SEric Taylor 			continue;
719681d9761SEric Taylor 		if (strncmp(namebuf, zv->zv_name, strlen(namebuf)) == 0)
720681d9761SEric Taylor 			(void) zvol_remove_zv(zv);
721e7cbe64fSgw 	}
722681d9761SEric Taylor 	kmem_free(namebuf, strlen(name) + 2);
723681d9761SEric Taylor 
724681d9761SEric Taylor 	mutex_exit(&zvol_state_lock);
725e7cbe64fSgw }
726e7cbe64fSgw 
727fa9e4066Sahrens int
72891ebeef5Sahrens zvol_set_volsize(const char *name, major_t maj, uint64_t volsize)
729fa9e4066Sahrens {
730681d9761SEric Taylor 	zvol_state_t *zv = NULL;
731681d9761SEric Taylor 	objset_t *os;
732fa9e4066Sahrens 	int error;
7335c5460e9Seschrock 	dmu_object_info_t doi;
734e7cbe64fSgw 	uint64_t old_volsize = 0ULL;
735681d9761SEric Taylor 	uint64_t readonly;
736fa9e4066Sahrens 
737fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
738681d9761SEric Taylor 	zv = zvol_minor_lookup(name);
739681d9761SEric Taylor 	if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) {
740681d9761SEric Taylor 		mutex_exit(&zvol_state_lock);
741681d9761SEric Taylor 		return (error);
742fa9e4066Sahrens 	}
743fa9e4066Sahrens 
744681d9761SEric Taylor 	if ((error = dmu_object_info(os, ZVOL_OBJ, &doi)) != 0 ||
745e9dbad6fSeschrock 	    (error = zvol_check_volsize(volsize,
746bb0ade09Sahrens 	    doi.doi_data_block_size)) != 0)
747bb0ade09Sahrens 		goto out;
7485c5460e9Seschrock 
749681d9761SEric Taylor 	VERIFY(dsl_prop_get_integer(name, "readonly", &readonly,
750681d9761SEric Taylor 	    NULL) == 0);
751681d9761SEric Taylor 	if (readonly) {
752bb0ade09Sahrens 		error = EROFS;
753bb0ade09Sahrens 		goto out;
754fa9e4066Sahrens 	}
755fa9e4066Sahrens 
756681d9761SEric Taylor 	error = zvol_update_volsize(os, volsize);
757e7cbe64fSgw 	/*
758e7cbe64fSgw 	 * Reinitialize the dump area to the new size. If we
759681d9761SEric Taylor 	 * failed to resize the dump area then restore it back to
760681d9761SEric Taylor 	 * its original size.
761e7cbe64fSgw 	 */
762681d9761SEric Taylor 	if (zv && error == 0) {
763681d9761SEric Taylor 		if (zv->zv_flags & ZVOL_DUMPIFIED) {
764681d9761SEric Taylor 			old_volsize = zv->zv_volsize;
765681d9761SEric Taylor 			zv->zv_volsize = volsize;
766681d9761SEric Taylor 			if ((error = zvol_dumpify(zv)) != 0 ||
767681d9761SEric Taylor 			    (error = dumpvp_resize()) != 0) {
768681d9761SEric Taylor 				(void) zvol_update_volsize(os, old_volsize);
769681d9761SEric Taylor 				zv->zv_volsize = old_volsize;
770681d9761SEric Taylor 				error = zvol_dumpify(zv);
771681d9761SEric Taylor 			}
772681d9761SEric Taylor 		}
773681d9761SEric Taylor 		if (error == 0) {
774681d9761SEric Taylor 			zv->zv_volsize = volsize;
775681d9761SEric Taylor 			zvol_size_changed(volsize, maj, zv->zv_minor);
776e7cbe64fSgw 		}
777fa9e4066Sahrens 	}
778fa9e4066Sahrens 
779573ca77eSGeorge Wilson 	/*
780573ca77eSGeorge Wilson 	 * Generate a LUN expansion event.
781573ca77eSGeorge Wilson 	 */
782681d9761SEric Taylor 	if (zv && error == 0) {
783573ca77eSGeorge Wilson 		sysevent_id_t eid;
784573ca77eSGeorge Wilson 		nvlist_t *attr;
785573ca77eSGeorge Wilson 		char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
786573ca77eSGeorge Wilson 
787681d9761SEric Taylor 		(void) snprintf(physpath, MAXPATHLEN, "%s%u", ZVOL_PSEUDO_DEV,
788573ca77eSGeorge Wilson 		    zv->zv_minor);
789573ca77eSGeorge Wilson 
790573ca77eSGeorge Wilson 		VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
791573ca77eSGeorge Wilson 		VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
792573ca77eSGeorge Wilson 
793573ca77eSGeorge Wilson 		(void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
794573ca77eSGeorge Wilson 		    ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
795573ca77eSGeorge Wilson 
796573ca77eSGeorge Wilson 		nvlist_free(attr);
797573ca77eSGeorge Wilson 		kmem_free(physpath, MAXPATHLEN);
798573ca77eSGeorge Wilson 	}
799573ca77eSGeorge Wilson 
800bb0ade09Sahrens out:
801681d9761SEric Taylor 	dmu_objset_rele(os, FTAG);
802bb0ade09Sahrens 
803fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
804fa9e4066Sahrens 
805fa9e4066Sahrens 	return (error);
806fa9e4066Sahrens }
807fa9e4066Sahrens 
808fa9e4066Sahrens /*ARGSUSED*/
809fa9e4066Sahrens int
810fa9e4066Sahrens zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr)
811fa9e4066Sahrens {
812fa9e4066Sahrens 	minor_t minor = getminor(*devp);
813fa9e4066Sahrens 	zvol_state_t *zv;
814681d9761SEric Taylor 	int err = 0;
815fa9e4066Sahrens 
816fa9e4066Sahrens 	if (minor == 0)			/* This is the control device */
817fa9e4066Sahrens 		return (0);
818fa9e4066Sahrens 
819fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
820fa9e4066Sahrens 
821fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
822fa9e4066Sahrens 	if (zv == NULL) {
823fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
824fa9e4066Sahrens 		return (ENXIO);
825fa9e4066Sahrens 	}
826fa9e4066Sahrens 
827681d9761SEric Taylor 	if (zv->zv_total_opens == 0)
828681d9761SEric Taylor 		err = zvol_first_open(zv);
829681d9761SEric Taylor 	if (err) {
830fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
831681d9761SEric Taylor 		return (err);
832681d9761SEric Taylor 	}
833681d9761SEric Taylor 	if ((flag & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
834681d9761SEric Taylor 		err = EROFS;
835681d9761SEric Taylor 		goto out;
836fa9e4066Sahrens 	}
837c7f714e2SEric Taylor 	if (zv->zv_flags & ZVOL_EXCL) {
838681d9761SEric Taylor 		err = EBUSY;
839681d9761SEric Taylor 		goto out;
840c7f714e2SEric Taylor 	}
841c7f714e2SEric Taylor 	if (flag & FEXCL) {
842c7f714e2SEric Taylor 		if (zv->zv_total_opens != 0) {
843681d9761SEric Taylor 			err = EBUSY;
844681d9761SEric Taylor 			goto out;
845c7f714e2SEric Taylor 		}
846c7f714e2SEric Taylor 		zv->zv_flags |= ZVOL_EXCL;
847c7f714e2SEric Taylor 	}
848fa9e4066Sahrens 
849fa9e4066Sahrens 	if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) {
850fa9e4066Sahrens 		zv->zv_open_count[otyp]++;
851fa9e4066Sahrens 		zv->zv_total_opens++;
852fa9e4066Sahrens 	}
853fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
854fa9e4066Sahrens 
855681d9761SEric Taylor 	return (err);
856681d9761SEric Taylor out:
857681d9761SEric Taylor 	if (zv->zv_total_opens == 0)
858681d9761SEric Taylor 		zvol_last_close(zv);
859681d9761SEric Taylor 	mutex_exit(&zvol_state_lock);
860681d9761SEric Taylor 	return (err);
861fa9e4066Sahrens }
862fa9e4066Sahrens 
863fa9e4066Sahrens /*ARGSUSED*/
864fa9e4066Sahrens int
865fa9e4066Sahrens zvol_close(dev_t dev, int flag, int otyp, cred_t *cr)
866fa9e4066Sahrens {
867fa9e4066Sahrens 	minor_t minor = getminor(dev);
868fa9e4066Sahrens 	zvol_state_t *zv;
869681d9761SEric Taylor 	int error = 0;
870fa9e4066Sahrens 
871fa9e4066Sahrens 	if (minor == 0)		/* This is the control device */
872fa9e4066Sahrens 		return (0);
873fa9e4066Sahrens 
874fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
875fa9e4066Sahrens 
876fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
877fa9e4066Sahrens 	if (zv == NULL) {
878fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
879fa9e4066Sahrens 		return (ENXIO);
880fa9e4066Sahrens 	}
881fa9e4066Sahrens 
882c7f714e2SEric Taylor 	if (zv->zv_flags & ZVOL_EXCL) {
883c7f714e2SEric Taylor 		ASSERT(zv->zv_total_opens == 1);
884c7f714e2SEric Taylor 		zv->zv_flags &= ~ZVOL_EXCL;
885fa9e4066Sahrens 	}
886fa9e4066Sahrens 
887fa9e4066Sahrens 	/*
888fa9e4066Sahrens 	 * If the open count is zero, this is a spurious close.
889fa9e4066Sahrens 	 * That indicates a bug in the kernel / DDI framework.
890fa9e4066Sahrens 	 */
891fa9e4066Sahrens 	ASSERT(zv->zv_open_count[otyp] != 0);
892fa9e4066Sahrens 	ASSERT(zv->zv_total_opens != 0);
893fa9e4066Sahrens 
894fa9e4066Sahrens 	/*
895fa9e4066Sahrens 	 * You may get multiple opens, but only one close.
896fa9e4066Sahrens 	 */
897fa9e4066Sahrens 	zv->zv_open_count[otyp]--;
898fa9e4066Sahrens 	zv->zv_total_opens--;
899fa9e4066Sahrens 
900681d9761SEric Taylor 	if (zv->zv_total_opens == 0)
901681d9761SEric Taylor 		zvol_last_close(zv);
902fa9e4066Sahrens 
903681d9761SEric Taylor 	mutex_exit(&zvol_state_lock);
904681d9761SEric Taylor 	return (error);
905fa9e4066Sahrens }
906fa9e4066Sahrens 
907feb08c6bSbillm static void
908b24ab676SJeff Bonwick zvol_get_done(zgd_t *zgd, int error)
90967bd71c6Sperrin {
910b24ab676SJeff Bonwick 	if (zgd->zgd_db)
911b24ab676SJeff Bonwick 		dmu_buf_rele(zgd->zgd_db, zgd);
912b24ab676SJeff Bonwick 
913b24ab676SJeff Bonwick 	zfs_range_unlock(zgd->zgd_rl);
914b24ab676SJeff Bonwick 
915b24ab676SJeff Bonwick 	if (error == 0 && zgd->zgd_bp)
916b24ab676SJeff Bonwick 		zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
91767bd71c6Sperrin 
91867bd71c6Sperrin 	kmem_free(zgd, sizeof (zgd_t));
91967bd71c6Sperrin }
92067bd71c6Sperrin 
92167bd71c6Sperrin /*
92267bd71c6Sperrin  * Get data to generate a TX_WRITE intent log record.
92367bd71c6Sperrin  */
924feb08c6bSbillm static int
92567bd71c6Sperrin zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
92667bd71c6Sperrin {
92767bd71c6Sperrin 	zvol_state_t *zv = arg;
92867bd71c6Sperrin 	objset_t *os = zv->zv_objset;
929b24ab676SJeff Bonwick 	uint64_t object = ZVOL_OBJ;
930b24ab676SJeff Bonwick 	uint64_t offset = lr->lr_offset;
931b24ab676SJeff Bonwick 	uint64_t size = lr->lr_length;	/* length of user data */
932b24ab676SJeff Bonwick 	blkptr_t *bp = &lr->lr_blkptr;
93367bd71c6Sperrin 	dmu_buf_t *db;
93467bd71c6Sperrin 	zgd_t *zgd;
93567bd71c6Sperrin 	int error;
93667bd71c6Sperrin 
937b24ab676SJeff Bonwick 	ASSERT(zio != NULL);
938b24ab676SJeff Bonwick 	ASSERT(size != 0);
939b24ab676SJeff Bonwick 
940b24ab676SJeff Bonwick 	zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
941b24ab676SJeff Bonwick 	zgd->zgd_zilog = zv->zv_zilog;
942b24ab676SJeff Bonwick 	zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
943feb08c6bSbillm 
944c2e6a7d6Sperrin 	/*
945c2e6a7d6Sperrin 	 * Write records come in two flavors: immediate and indirect.
946c2e6a7d6Sperrin 	 * For small writes it's cheaper to store the data with the
947c2e6a7d6Sperrin 	 * log record (immediate); for large writes it's cheaper to
948c2e6a7d6Sperrin 	 * sync the data and get a pointer to it (indirect) so that
949c2e6a7d6Sperrin 	 * we don't have to write the data twice.
950c2e6a7d6Sperrin 	 */
951b24ab676SJeff Bonwick 	if (buf != NULL) {	/* immediate write */
952b24ab676SJeff Bonwick 		error = dmu_read(os, object, offset, size, buf,
953b24ab676SJeff Bonwick 		    DMU_READ_NO_PREFETCH);
954b24ab676SJeff Bonwick 	} else {
955b24ab676SJeff Bonwick 		size = zv->zv_volblocksize;
956b24ab676SJeff Bonwick 		offset = P2ALIGN(offset, size);
95747cb52daSJeff Bonwick 		error = dmu_buf_hold(os, object, offset, zgd, &db,
95847cb52daSJeff Bonwick 		    DMU_READ_NO_PREFETCH);
959b24ab676SJeff Bonwick 		if (error == 0) {
960b24ab676SJeff Bonwick 			zgd->zgd_db = db;
961b24ab676SJeff Bonwick 			zgd->zgd_bp = bp;
96267bd71c6Sperrin 
963b24ab676SJeff Bonwick 			ASSERT(db->db_offset == offset);
964b24ab676SJeff Bonwick 			ASSERT(db->db_size == size);
96567bd71c6Sperrin 
966b24ab676SJeff Bonwick 			error = dmu_sync(zio, lr->lr_common.lrc_txg,
967b24ab676SJeff Bonwick 			    zvol_get_done, zgd);
968975c32a0SNeil Perrin 
969b24ab676SJeff Bonwick 			if (error == 0)
970b24ab676SJeff Bonwick 				return (0);
971b24ab676SJeff Bonwick 		}
972975c32a0SNeil Perrin 	}
973975c32a0SNeil Perrin 
974b24ab676SJeff Bonwick 	zvol_get_done(zgd, error);
975b24ab676SJeff Bonwick 
97667bd71c6Sperrin 	return (error);
97767bd71c6Sperrin }
97867bd71c6Sperrin 
979a24e15ceSperrin /*
980a24e15ceSperrin  * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
98122ac5be4Sperrin  *
98222ac5be4Sperrin  * We store data in the log buffers if it's small enough.
98367bd71c6Sperrin  * Otherwise we will later flush the data out via dmu_sync().
98422ac5be4Sperrin  */
98567bd71c6Sperrin ssize_t zvol_immediate_write_sz = 32768;
98622ac5be4Sperrin 
987feb08c6bSbillm static void
988510b6c0eSNeil Perrin zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid,
989510b6c0eSNeil Perrin     boolean_t sync)
99022ac5be4Sperrin {
991feb08c6bSbillm 	uint32_t blocksize = zv->zv_volblocksize;
9921209a471SNeil Perrin 	zilog_t *zilog = zv->zv_zilog;
993510b6c0eSNeil Perrin 	boolean_t slogging;
994e09fa4daSNeil Perrin 	ssize_t immediate_write_sz;
995510b6c0eSNeil Perrin 
996b24ab676SJeff Bonwick 	if (zil_replaying(zilog, tx))
9971209a471SNeil Perrin 		return;
9981209a471SNeil Perrin 
999e09fa4daSNeil Perrin 	immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
1000e09fa4daSNeil Perrin 	    ? 0 : zvol_immediate_write_sz;
1001e09fa4daSNeil Perrin 
1002e09fa4daSNeil Perrin 	slogging = spa_has_slogs(zilog->zl_spa) &&
1003e09fa4daSNeil Perrin 	    (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
1004feb08c6bSbillm 
1005510b6c0eSNeil Perrin 	while (resid) {
1006510b6c0eSNeil Perrin 		itx_t *itx;
1007510b6c0eSNeil Perrin 		lr_write_t *lr;
1008510b6c0eSNeil Perrin 		ssize_t len;
1009510b6c0eSNeil Perrin 		itx_wr_state_t write_state;
1010510b6c0eSNeil Perrin 
1011510b6c0eSNeil Perrin 		/*
1012510b6c0eSNeil Perrin 		 * Unlike zfs_log_write() we can be called with
1013510b6c0eSNeil Perrin 		 * upto DMU_MAX_ACCESS/2 (5MB) writes.
1014510b6c0eSNeil Perrin 		 */
1015e09fa4daSNeil Perrin 		if (blocksize > immediate_write_sz && !slogging &&
1016510b6c0eSNeil Perrin 		    resid >= blocksize && off % blocksize == 0) {
1017510b6c0eSNeil Perrin 			write_state = WR_INDIRECT; /* uses dmu_sync */
1018510b6c0eSNeil Perrin 			len = blocksize;
1019510b6c0eSNeil Perrin 		} else if (sync) {
1020510b6c0eSNeil Perrin 			write_state = WR_COPIED;
1021510b6c0eSNeil Perrin 			len = MIN(ZIL_MAX_LOG_DATA, resid);
1022510b6c0eSNeil Perrin 		} else {
1023510b6c0eSNeil Perrin 			write_state = WR_NEED_COPY;
1024510b6c0eSNeil Perrin 			len = MIN(ZIL_MAX_LOG_DATA, resid);
1025510b6c0eSNeil Perrin 		}
1026510b6c0eSNeil Perrin 
1027510b6c0eSNeil Perrin 		itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
1028510b6c0eSNeil Perrin 		    (write_state == WR_COPIED ? len : 0));
1029feb08c6bSbillm 		lr = (lr_write_t *)&itx->itx_lr;
1030510b6c0eSNeil Perrin 		if (write_state == WR_COPIED && dmu_read(zv->zv_objset,
10317bfdf011SNeil Perrin 		    ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
1032b24ab676SJeff Bonwick 			zil_itx_destroy(itx);
1033510b6c0eSNeil Perrin 			itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1034510b6c0eSNeil Perrin 			lr = (lr_write_t *)&itx->itx_lr;
1035510b6c0eSNeil Perrin 			write_state = WR_NEED_COPY;
1036510b6c0eSNeil Perrin 		}
1037510b6c0eSNeil Perrin 
1038510b6c0eSNeil Perrin 		itx->itx_wr_state = write_state;
1039510b6c0eSNeil Perrin 		if (write_state == WR_NEED_COPY)
1040510b6c0eSNeil Perrin 			itx->itx_sod += len;
1041feb08c6bSbillm 		lr->lr_foid = ZVOL_OBJ;
1042feb08c6bSbillm 		lr->lr_offset = off;
1043510b6c0eSNeil Perrin 		lr->lr_length = len;
1044b24ab676SJeff Bonwick 		lr->lr_blkoff = 0;
1045feb08c6bSbillm 		BP_ZERO(&lr->lr_blkptr);
1046feb08c6bSbillm 
1047510b6c0eSNeil Perrin 		itx->itx_private = zv;
1048510b6c0eSNeil Perrin 		itx->itx_sync = sync;
1049510b6c0eSNeil Perrin 
10501209a471SNeil Perrin 		(void) zil_itx_assign(zilog, itx, tx);
1051510b6c0eSNeil Perrin 
1052510b6c0eSNeil Perrin 		off += len;
1053510b6c0eSNeil Perrin 		resid -= len;
105422ac5be4Sperrin 	}
105522ac5be4Sperrin }
105622ac5be4Sperrin 
105788b7b0f2SMatthew Ahrens static int
105888b7b0f2SMatthew Ahrens zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t size,
105988b7b0f2SMatthew Ahrens     boolean_t doread, boolean_t isdump)
1060e7cbe64fSgw {
1061e7cbe64fSgw 	vdev_disk_t *dvd;
1062e7cbe64fSgw 	int c;
1063e7cbe64fSgw 	int numerrors = 0;
1064e7cbe64fSgw 
1065e7cbe64fSgw 	for (c = 0; c < vd->vdev_children; c++) {
106621ecdf64SLin Ling 		ASSERT(vd->vdev_ops == &vdev_mirror_ops ||
106721ecdf64SLin Ling 		    vd->vdev_ops == &vdev_replacing_ops ||
106821ecdf64SLin Ling 		    vd->vdev_ops == &vdev_spare_ops);
106988b7b0f2SMatthew Ahrens 		int err = zvol_dumpio_vdev(vd->vdev_child[c],
107088b7b0f2SMatthew Ahrens 		    addr, offset, size, doread, isdump);
107188b7b0f2SMatthew Ahrens 		if (err != 0) {
1072e7cbe64fSgw 			numerrors++;
107388b7b0f2SMatthew Ahrens 		} else if (doread) {
1074e7cbe64fSgw 			break;
1075e7cbe64fSgw 		}
1076e7cbe64fSgw 	}
1077e7cbe64fSgw 
1078e7cbe64fSgw 	if (!vd->vdev_ops->vdev_op_leaf)
1079e7cbe64fSgw 		return (numerrors < vd->vdev_children ? 0 : EIO);
1080e7cbe64fSgw 
1081dc0bb255SEric Taylor 	if (doread && !vdev_readable(vd))
1082dc0bb255SEric Taylor 		return (EIO);
1083dc0bb255SEric Taylor 	else if (!doread && !vdev_writeable(vd))
1084e7cbe64fSgw 		return (EIO);
1085e7cbe64fSgw 
1086e7cbe64fSgw 	dvd = vd->vdev_tsd;
1087e7cbe64fSgw 	ASSERT3P(dvd, !=, NULL);
1088e7cbe64fSgw 	offset += VDEV_LABEL_START_SIZE;
1089e7cbe64fSgw 
1090e7cbe64fSgw 	if (ddi_in_panic() || isdump) {
109188b7b0f2SMatthew Ahrens 		ASSERT(!doread);
109288b7b0f2SMatthew Ahrens 		if (doread)
1093e7cbe64fSgw 			return (EIO);
1094e7cbe64fSgw 		return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset),
1095e7cbe64fSgw 		    lbtodb(size)));
1096e7cbe64fSgw 	} else {
1097e7cbe64fSgw 		return (vdev_disk_physio(dvd->vd_lh, addr, size, offset,
109888b7b0f2SMatthew Ahrens 		    doread ? B_READ : B_WRITE));
1099e7cbe64fSgw 	}
1100e7cbe64fSgw }
1101e7cbe64fSgw 
110288b7b0f2SMatthew Ahrens static int
110388b7b0f2SMatthew Ahrens zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size,
110488b7b0f2SMatthew Ahrens     boolean_t doread, boolean_t isdump)
1105e7cbe64fSgw {
1106e7cbe64fSgw 	vdev_t *vd;
1107e7cbe64fSgw 	int error;
110888b7b0f2SMatthew Ahrens 	zvol_extent_t *ze;
1109e7cbe64fSgw 	spa_t *spa = dmu_objset_spa(zv->zv_objset);
1110e7cbe64fSgw 
111188b7b0f2SMatthew Ahrens 	/* Must be sector aligned, and not stradle a block boundary. */
111288b7b0f2SMatthew Ahrens 	if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) ||
111388b7b0f2SMatthew Ahrens 	    P2BOUNDARY(offset, size, zv->zv_volblocksize)) {
1114e7cbe64fSgw 		return (EINVAL);
111588b7b0f2SMatthew Ahrens 	}
111688b7b0f2SMatthew Ahrens 	ASSERT(size <= zv->zv_volblocksize);
1117e7cbe64fSgw 
111888b7b0f2SMatthew Ahrens 	/* Locate the extent this belongs to */
111988b7b0f2SMatthew Ahrens 	ze = list_head(&zv->zv_extents);
112088b7b0f2SMatthew Ahrens 	while (offset >= ze->ze_nblks * zv->zv_volblocksize) {
112188b7b0f2SMatthew Ahrens 		offset -= ze->ze_nblks * zv->zv_volblocksize;
112288b7b0f2SMatthew Ahrens 		ze = list_next(&zv->zv_extents, ze);
112388b7b0f2SMatthew Ahrens 	}
112424cc0e1cSGeorge Wilson 
112524cc0e1cSGeorge Wilson 	if (!ddi_in_panic())
112624cc0e1cSGeorge Wilson 		spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
112724cc0e1cSGeorge Wilson 
112888b7b0f2SMatthew Ahrens 	vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva));
112988b7b0f2SMatthew Ahrens 	offset += DVA_GET_OFFSET(&ze->ze_dva);
113088b7b0f2SMatthew Ahrens 	error = zvol_dumpio_vdev(vd, addr, offset, size, doread, isdump);
113124cc0e1cSGeorge Wilson 
113224cc0e1cSGeorge Wilson 	if (!ddi_in_panic())
113324cc0e1cSGeorge Wilson 		spa_config_exit(spa, SCL_STATE, FTAG);
113424cc0e1cSGeorge Wilson 
1135e7cbe64fSgw 	return (error);
1136e7cbe64fSgw }
1137e7cbe64fSgw 
1138fa9e4066Sahrens int
1139fa9e4066Sahrens zvol_strategy(buf_t *bp)
1140fa9e4066Sahrens {
1141fa9e4066Sahrens 	zvol_state_t *zv = ddi_get_soft_state(zvol_state, getminor(bp->b_edev));
1142fa9e4066Sahrens 	uint64_t off, volsize;
114388b7b0f2SMatthew Ahrens 	size_t resid;
1144fa9e4066Sahrens 	char *addr;
114522ac5be4Sperrin 	objset_t *os;
1146c2e6a7d6Sperrin 	rl_t *rl;
1147fa9e4066Sahrens 	int error = 0;
114888b7b0f2SMatthew Ahrens 	boolean_t doread = bp->b_flags & B_READ;
1149f80ce222SChris Kirby 	boolean_t is_dump;
1150510b6c0eSNeil Perrin 	boolean_t sync;
1151fa9e4066Sahrens 
1152fa9e4066Sahrens 	if (zv == NULL) {
1153fa9e4066Sahrens 		bioerror(bp, ENXIO);
1154fa9e4066Sahrens 		biodone(bp);
1155fa9e4066Sahrens 		return (0);
1156fa9e4066Sahrens 	}
1157fa9e4066Sahrens 
1158fa9e4066Sahrens 	if (getminor(bp->b_edev) == 0) {
1159fa9e4066Sahrens 		bioerror(bp, EINVAL);
1160fa9e4066Sahrens 		biodone(bp);
1161fa9e4066Sahrens 		return (0);
1162fa9e4066Sahrens 	}
1163fa9e4066Sahrens 
1164681d9761SEric Taylor 	if (!(bp->b_flags & B_READ) && (zv->zv_flags & ZVOL_RDONLY)) {
1165fa9e4066Sahrens 		bioerror(bp, EROFS);
1166fa9e4066Sahrens 		biodone(bp);
1167fa9e4066Sahrens 		return (0);
1168fa9e4066Sahrens 	}
1169fa9e4066Sahrens 
1170fa9e4066Sahrens 	off = ldbtob(bp->b_blkno);
1171fa9e4066Sahrens 	volsize = zv->zv_volsize;
1172fa9e4066Sahrens 
117322ac5be4Sperrin 	os = zv->zv_objset;
117422ac5be4Sperrin 	ASSERT(os != NULL);
1175fa9e4066Sahrens 
1176fa9e4066Sahrens 	bp_mapin(bp);
1177fa9e4066Sahrens 	addr = bp->b_un.b_addr;
1178fa9e4066Sahrens 	resid = bp->b_bcount;
1179fa9e4066Sahrens 
118088b7b0f2SMatthew Ahrens 	if (resid > 0 && (off < 0 || off >= volsize)) {
118188b7b0f2SMatthew Ahrens 		bioerror(bp, EIO);
118288b7b0f2SMatthew Ahrens 		biodone(bp);
118388b7b0f2SMatthew Ahrens 		return (0);
118488b7b0f2SMatthew Ahrens 	}
118573ec3d9cSgw 
1186f80ce222SChris Kirby 	is_dump = zv->zv_flags & ZVOL_DUMPIFIED;
118755da60b9SMark J Musante 	sync = ((!(bp->b_flags & B_ASYNC) &&
118855da60b9SMark J Musante 	    !(zv->zv_flags & ZVOL_WCE)) ||
118955da60b9SMark J Musante 	    (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)) &&
119055da60b9SMark J Musante 	    !doread && !is_dump;
1191510b6c0eSNeil Perrin 
1192a24e15ceSperrin 	/*
1193a24e15ceSperrin 	 * There must be no buffer changes when doing a dmu_sync() because
1194a24e15ceSperrin 	 * we can't change the data whilst calculating the checksum.
1195a24e15ceSperrin 	 */
1196c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, off, resid,
119788b7b0f2SMatthew Ahrens 	    doread ? RL_READER : RL_WRITER);
1198fa9e4066Sahrens 
1199e7cbe64fSgw 	while (resid != 0 && off < volsize) {
120088b7b0f2SMatthew Ahrens 		size_t size = MIN(resid, zvol_maxphys);
1201e7cbe64fSgw 		if (is_dump) {
1202e7cbe64fSgw 			size = MIN(size, P2END(off, zv->zv_volblocksize) - off);
120388b7b0f2SMatthew Ahrens 			error = zvol_dumpio(zv, addr, off, size,
120488b7b0f2SMatthew Ahrens 			    doread, B_FALSE);
120588b7b0f2SMatthew Ahrens 		} else if (doread) {
12067bfdf011SNeil Perrin 			error = dmu_read(os, ZVOL_OBJ, off, size, addr,
12077bfdf011SNeil Perrin 			    DMU_READ_PREFETCH);
1208fa9e4066Sahrens 		} else {
120922ac5be4Sperrin 			dmu_tx_t *tx = dmu_tx_create(os);
1210fa9e4066Sahrens 			dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
1211fa9e4066Sahrens 			error = dmu_tx_assign(tx, TXG_WAIT);
1212fa9e4066Sahrens 			if (error) {
1213fa9e4066Sahrens 				dmu_tx_abort(tx);
1214fa9e4066Sahrens 			} else {
121522ac5be4Sperrin 				dmu_write(os, ZVOL_OBJ, off, size, addr, tx);
1216510b6c0eSNeil Perrin 				zvol_log_write(zv, tx, off, size, sync);
1217fa9e4066Sahrens 				dmu_tx_commit(tx);
1218fa9e4066Sahrens 			}
1219fa9e4066Sahrens 		}
1220b87f3af3Sperrin 		if (error) {
1221b87f3af3Sperrin 			/* convert checksum errors into IO errors */
1222b87f3af3Sperrin 			if (error == ECKSUM)
1223b87f3af3Sperrin 				error = EIO;
1224fa9e4066Sahrens 			break;
1225b87f3af3Sperrin 		}
1226fa9e4066Sahrens 		off += size;
1227fa9e4066Sahrens 		addr += size;
1228fa9e4066Sahrens 		resid -= size;
1229fa9e4066Sahrens 	}
1230c2e6a7d6Sperrin 	zfs_range_unlock(rl);
1231fa9e4066Sahrens 
1232fa9e4066Sahrens 	if ((bp->b_resid = resid) == bp->b_bcount)
1233fa9e4066Sahrens 		bioerror(bp, off > volsize ? EINVAL : error);
1234fa9e4066Sahrens 
1235510b6c0eSNeil Perrin 	if (sync)
1236feb08c6bSbillm 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1237feb08c6bSbillm 	biodone(bp);
123822ac5be4Sperrin 
1239fa9e4066Sahrens 	return (0);
1240fa9e4066Sahrens }
1241fa9e4066Sahrens 
124267bd71c6Sperrin /*
124367bd71c6Sperrin  * Set the buffer count to the zvol maximum transfer.
124467bd71c6Sperrin  * Using our own routine instead of the default minphys()
124567bd71c6Sperrin  * means that for larger writes we write bigger buffers on X86
124667bd71c6Sperrin  * (128K instead of 56K) and flush the disk write cache less often
124767bd71c6Sperrin  * (every zvol_maxphys - currently 1MB) instead of minphys (currently
124867bd71c6Sperrin  * 56K on X86 and 128K on sparc).
124967bd71c6Sperrin  */
125067bd71c6Sperrin void
125167bd71c6Sperrin zvol_minphys(struct buf *bp)
125267bd71c6Sperrin {
125367bd71c6Sperrin 	if (bp->b_bcount > zvol_maxphys)
125467bd71c6Sperrin 		bp->b_bcount = zvol_maxphys;
125567bd71c6Sperrin }
125667bd71c6Sperrin 
1257e7cbe64fSgw int
1258e7cbe64fSgw zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks)
1259e7cbe64fSgw {
1260e7cbe64fSgw 	minor_t minor = getminor(dev);
1261e7cbe64fSgw 	zvol_state_t *zv;
1262e7cbe64fSgw 	int error = 0;
1263e7cbe64fSgw 	uint64_t size;
1264e7cbe64fSgw 	uint64_t boff;
1265e7cbe64fSgw 	uint64_t resid;
1266e7cbe64fSgw 
1267e7cbe64fSgw 	if (minor == 0)			/* This is the control device */
1268e7cbe64fSgw 		return (ENXIO);
1269e7cbe64fSgw 
1270e7cbe64fSgw 	zv = ddi_get_soft_state(zvol_state, minor);
1271e7cbe64fSgw 	if (zv == NULL)
1272e7cbe64fSgw 		return (ENXIO);
1273e7cbe64fSgw 
1274e7cbe64fSgw 	boff = ldbtob(blkno);
1275e7cbe64fSgw 	resid = ldbtob(nblocks);
127688b7b0f2SMatthew Ahrens 
127788b7b0f2SMatthew Ahrens 	VERIFY3U(boff + resid, <=, zv->zv_volsize);
127888b7b0f2SMatthew Ahrens 
1279e7cbe64fSgw 	while (resid) {
1280e7cbe64fSgw 		size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff);
128188b7b0f2SMatthew Ahrens 		error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE);
1282e7cbe64fSgw 		if (error)
1283e7cbe64fSgw 			break;
1284e7cbe64fSgw 		boff += size;
1285e7cbe64fSgw 		addr += size;
1286e7cbe64fSgw 		resid -= size;
1287e7cbe64fSgw 	}
1288e7cbe64fSgw 
1289e7cbe64fSgw 	return (error);
1290e7cbe64fSgw }
1291e7cbe64fSgw 
1292fa9e4066Sahrens /*ARGSUSED*/
1293fa9e4066Sahrens int
1294feb08c6bSbillm zvol_read(dev_t dev, uio_t *uio, cred_t *cr)
1295fa9e4066Sahrens {
1296c7ca1008Sgw 	minor_t minor = getminor(dev);
1297c7ca1008Sgw 	zvol_state_t *zv;
129873ec3d9cSgw 	uint64_t volsize;
1299c2e6a7d6Sperrin 	rl_t *rl;
1300feb08c6bSbillm 	int error = 0;
1301fa9e4066Sahrens 
1302c7ca1008Sgw 	if (minor == 0)			/* This is the control device */
1303c7ca1008Sgw 		return (ENXIO);
1304c7ca1008Sgw 
1305c7ca1008Sgw 	zv = ddi_get_soft_state(zvol_state, minor);
1306c7ca1008Sgw 	if (zv == NULL)
1307c7ca1008Sgw 		return (ENXIO);
1308c7ca1008Sgw 
130973ec3d9cSgw 	volsize = zv->zv_volsize;
131073ec3d9cSgw 	if (uio->uio_resid > 0 &&
131173ec3d9cSgw 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
131273ec3d9cSgw 		return (EIO);
131373ec3d9cSgw 
131488b7b0f2SMatthew Ahrens 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
131588b7b0f2SMatthew Ahrens 		error = physio(zvol_strategy, NULL, dev, B_READ,
131688b7b0f2SMatthew Ahrens 		    zvol_minphys, uio);
131788b7b0f2SMatthew Ahrens 		return (error);
131888b7b0f2SMatthew Ahrens 	}
131988b7b0f2SMatthew Ahrens 
1320c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1321c2e6a7d6Sperrin 	    RL_READER);
132273ec3d9cSgw 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1323feb08c6bSbillm 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1324fa9e4066Sahrens 
132573ec3d9cSgw 		/* don't read past the end */
132673ec3d9cSgw 		if (bytes > volsize - uio->uio_loffset)
132773ec3d9cSgw 			bytes = volsize - uio->uio_loffset;
132873ec3d9cSgw 
1329feb08c6bSbillm 		error =  dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes);
1330b87f3af3Sperrin 		if (error) {
1331b87f3af3Sperrin 			/* convert checksum errors into IO errors */
1332b87f3af3Sperrin 			if (error == ECKSUM)
1333b87f3af3Sperrin 				error = EIO;
1334feb08c6bSbillm 			break;
1335b87f3af3Sperrin 		}
1336feb08c6bSbillm 	}
1337c2e6a7d6Sperrin 	zfs_range_unlock(rl);
1338feb08c6bSbillm 	return (error);
1339fa9e4066Sahrens }
1340fa9e4066Sahrens 
1341fa9e4066Sahrens /*ARGSUSED*/
1342fa9e4066Sahrens int
1343feb08c6bSbillm zvol_write(dev_t dev, uio_t *uio, cred_t *cr)
1344fa9e4066Sahrens {
1345c7ca1008Sgw 	minor_t minor = getminor(dev);
1346c7ca1008Sgw 	zvol_state_t *zv;
134773ec3d9cSgw 	uint64_t volsize;
1348c2e6a7d6Sperrin 	rl_t *rl;
1349feb08c6bSbillm 	int error = 0;
1350510b6c0eSNeil Perrin 	boolean_t sync;
1351feb08c6bSbillm 
1352c7ca1008Sgw 	if (minor == 0)			/* This is the control device */
1353c7ca1008Sgw 		return (ENXIO);
1354c7ca1008Sgw 
1355c7ca1008Sgw 	zv = ddi_get_soft_state(zvol_state, minor);
1356c7ca1008Sgw 	if (zv == NULL)
1357c7ca1008Sgw 		return (ENXIO);
1358c7ca1008Sgw 
135973ec3d9cSgw 	volsize = zv->zv_volsize;
136073ec3d9cSgw 	if (uio->uio_resid > 0 &&
136173ec3d9cSgw 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
136273ec3d9cSgw 		return (EIO);
136373ec3d9cSgw 
1364e7cbe64fSgw 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
1365e7cbe64fSgw 		error = physio(zvol_strategy, NULL, dev, B_WRITE,
1366e7cbe64fSgw 		    zvol_minphys, uio);
1367e7cbe64fSgw 		return (error);
1368e7cbe64fSgw 	}
1369e7cbe64fSgw 
137055da60b9SMark J Musante 	sync = !(zv->zv_flags & ZVOL_WCE) ||
137155da60b9SMark J Musante 	    (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS);
1372510b6c0eSNeil Perrin 
1373c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1374c2e6a7d6Sperrin 	    RL_WRITER);
137573ec3d9cSgw 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1376feb08c6bSbillm 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1377feb08c6bSbillm 		uint64_t off = uio->uio_loffset;
1378feb08c6bSbillm 		dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
137973ec3d9cSgw 
138073ec3d9cSgw 		if (bytes > volsize - off)	/* don't write past the end */
138173ec3d9cSgw 			bytes = volsize - off;
138273ec3d9cSgw 
1383feb08c6bSbillm 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
1384feb08c6bSbillm 		error = dmu_tx_assign(tx, TXG_WAIT);
1385feb08c6bSbillm 		if (error) {
1386feb08c6bSbillm 			dmu_tx_abort(tx);
1387feb08c6bSbillm 			break;
1388feb08c6bSbillm 		}
138994d1a210STim Haley 		error = dmu_write_uio_dbuf(zv->zv_dbuf, uio, bytes, tx);
1390feb08c6bSbillm 		if (error == 0)
1391510b6c0eSNeil Perrin 			zvol_log_write(zv, tx, off, bytes, sync);
1392feb08c6bSbillm 		dmu_tx_commit(tx);
1393feb08c6bSbillm 
1394feb08c6bSbillm 		if (error)
1395feb08c6bSbillm 			break;
1396feb08c6bSbillm 	}
1397c2e6a7d6Sperrin 	zfs_range_unlock(rl);
1398510b6c0eSNeil Perrin 	if (sync)
1399e08bf2c6SEric Taylor 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1400feb08c6bSbillm 	return (error);
1401fa9e4066Sahrens }
1402fa9e4066Sahrens 
1403c7f714e2SEric Taylor int
1404c7f714e2SEric Taylor zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs)
1405c7f714e2SEric Taylor {
1406c7f714e2SEric Taylor 	struct uuid uuid = EFI_RESERVED;
1407c7f714e2SEric Taylor 	efi_gpe_t gpe = { 0 };
1408c7f714e2SEric Taylor 	uint32_t crc;
1409c7f714e2SEric Taylor 	dk_efi_t efi;
1410c7f714e2SEric Taylor 	int length;
1411c7f714e2SEric Taylor 	char *ptr;
1412c7f714e2SEric Taylor 
1413c7f714e2SEric Taylor 	if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag))
1414c7f714e2SEric Taylor 		return (EFAULT);
1415c7f714e2SEric Taylor 	ptr = (char *)(uintptr_t)efi.dki_data_64;
1416c7f714e2SEric Taylor 	length = efi.dki_length;
1417c7f714e2SEric Taylor 	/*
1418c7f714e2SEric Taylor 	 * Some clients may attempt to request a PMBR for the
1419c7f714e2SEric Taylor 	 * zvol.  Currently this interface will return EINVAL to
1420c7f714e2SEric Taylor 	 * such requests.  These requests could be supported by
1421c7f714e2SEric Taylor 	 * adding a check for lba == 0 and consing up an appropriate
1422c7f714e2SEric Taylor 	 * PMBR.
1423c7f714e2SEric Taylor 	 */
1424c7f714e2SEric Taylor 	if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0)
1425c7f714e2SEric Taylor 		return (EINVAL);
1426c7f714e2SEric Taylor 
1427c7f714e2SEric Taylor 	gpe.efi_gpe_StartingLBA = LE_64(34ULL);
1428c7f714e2SEric Taylor 	gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1);
1429c7f714e2SEric Taylor 	UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
1430c7f714e2SEric Taylor 
1431c7f714e2SEric Taylor 	if (efi.dki_lba == 1) {
1432c7f714e2SEric Taylor 		efi_gpt_t gpt = { 0 };
1433c7f714e2SEric Taylor 
1434c7f714e2SEric Taylor 		gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
1435c7f714e2SEric Taylor 		gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
1436c7f714e2SEric Taylor 		gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt));
1437c7f714e2SEric Taylor 		gpt.efi_gpt_MyLBA = LE_64(1ULL);
1438c7f714e2SEric Taylor 		gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL);
1439c7f714e2SEric Taylor 		gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1);
1440c7f714e2SEric Taylor 		gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL);
1441c7f714e2SEric Taylor 		gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
1442c7f714e2SEric Taylor 		gpt.efi_gpt_SizeOfPartitionEntry =
1443c7f714e2SEric Taylor 		    LE_32(sizeof (efi_gpe_t));
1444c7f714e2SEric Taylor 		CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
1445c7f714e2SEric Taylor 		gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
1446c7f714e2SEric Taylor 		CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table);
1447c7f714e2SEric Taylor 		gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
1448c7f714e2SEric Taylor 		if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length),
1449c7f714e2SEric Taylor 		    flag))
1450c7f714e2SEric Taylor 			return (EFAULT);
1451c7f714e2SEric Taylor 		ptr += sizeof (gpt);
1452c7f714e2SEric Taylor 		length -= sizeof (gpt);
1453c7f714e2SEric Taylor 	}
1454c7f714e2SEric Taylor 	if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe),
1455c7f714e2SEric Taylor 	    length), flag))
1456c7f714e2SEric Taylor 		return (EFAULT);
1457c7f714e2SEric Taylor 	return (0);
1458c7f714e2SEric Taylor }
1459c7f714e2SEric Taylor 
1460*3fb517f7SJames Moore /*
1461*3fb517f7SJames Moore  * BEGIN entry points to allow external callers access to the volume.
1462*3fb517f7SJames Moore  */
1463*3fb517f7SJames Moore /*
1464*3fb517f7SJames Moore  * Return the volume parameters needed for access from an external caller.
1465*3fb517f7SJames Moore  * These values are invariant as long as the volume is held open.
1466*3fb517f7SJames Moore  */
1467*3fb517f7SJames Moore int
1468*3fb517f7SJames Moore zvol_get_volume_params(minor_t minor, uint64_t *blksize,
1469*3fb517f7SJames Moore     uint64_t *max_xfer_len, void **minor_hdl, void **objset_hdl, void **zil_hdl,
1470*3fb517f7SJames Moore     void **rl_hdl, void **bonus_hdl)
1471*3fb517f7SJames Moore {
1472*3fb517f7SJames Moore 	zvol_state_t *zv;
1473*3fb517f7SJames Moore 
1474*3fb517f7SJames Moore 	if (minor == 0)
1475*3fb517f7SJames Moore 		return (ENXIO);
1476*3fb517f7SJames Moore 	if ((zv = ddi_get_soft_state(zvol_state, minor)) == NULL)
1477*3fb517f7SJames Moore 		return (ENXIO);
1478*3fb517f7SJames Moore 	if (zv->zv_flags & ZVOL_DUMPIFIED)
1479*3fb517f7SJames Moore 		return (ENXIO);
1480*3fb517f7SJames Moore 
1481*3fb517f7SJames Moore 	ASSERT(blksize && max_xfer_len && minor_hdl &&
1482*3fb517f7SJames Moore 	    objset_hdl && zil_hdl && rl_hdl && bonus_hdl);
1483*3fb517f7SJames Moore 
1484*3fb517f7SJames Moore 	*blksize = zv->zv_volblocksize;
1485*3fb517f7SJames Moore 	*max_xfer_len = (uint64_t)zvol_maxphys;
1486*3fb517f7SJames Moore 	*minor_hdl = zv;
1487*3fb517f7SJames Moore 	*objset_hdl = zv->zv_objset;
1488*3fb517f7SJames Moore 	*zil_hdl = zv->zv_zilog;
1489*3fb517f7SJames Moore 	*rl_hdl = &zv->zv_znode;
1490*3fb517f7SJames Moore 	*bonus_hdl = zv->zv_dbuf;
1491*3fb517f7SJames Moore 	return (0);
1492*3fb517f7SJames Moore }
1493*3fb517f7SJames Moore 
1494*3fb517f7SJames Moore /*
1495*3fb517f7SJames Moore  * Return the current volume size to an external caller.
1496*3fb517f7SJames Moore  * The size can change while the volume is open.
1497*3fb517f7SJames Moore  */
1498*3fb517f7SJames Moore uint64_t
1499*3fb517f7SJames Moore zvol_get_volume_size(void *minor_hdl)
1500*3fb517f7SJames Moore {
1501*3fb517f7SJames Moore 	zvol_state_t *zv = minor_hdl;
1502*3fb517f7SJames Moore 
1503*3fb517f7SJames Moore 	return (zv->zv_volsize);
1504*3fb517f7SJames Moore }
1505*3fb517f7SJames Moore 
1506*3fb517f7SJames Moore /*
1507*3fb517f7SJames Moore  * Return the current WCE setting to an external caller.
1508*3fb517f7SJames Moore  * The WCE setting can change while the volume is open.
1509*3fb517f7SJames Moore  */
1510*3fb517f7SJames Moore int
1511*3fb517f7SJames Moore zvol_get_volume_wce(void *minor_hdl)
1512*3fb517f7SJames Moore {
1513*3fb517f7SJames Moore 	zvol_state_t *zv = minor_hdl;
1514*3fb517f7SJames Moore 
1515*3fb517f7SJames Moore 	return ((zv->zv_flags & ZVOL_WCE) ? 1 : 0);
1516*3fb517f7SJames Moore }
1517*3fb517f7SJames Moore 
1518*3fb517f7SJames Moore /*
1519*3fb517f7SJames Moore  * Entry point for external callers to zvol_log_write
1520*3fb517f7SJames Moore  */
1521*3fb517f7SJames Moore void
1522*3fb517f7SJames Moore zvol_log_write_minor(void *minor_hdl, dmu_tx_t *tx, offset_t off, ssize_t resid,
1523*3fb517f7SJames Moore     boolean_t sync)
1524*3fb517f7SJames Moore {
1525*3fb517f7SJames Moore 	zvol_state_t *zv = minor_hdl;
1526*3fb517f7SJames Moore 
1527*3fb517f7SJames Moore 	zvol_log_write(zv, tx, off, resid, sync);
1528*3fb517f7SJames Moore }
1529*3fb517f7SJames Moore /*
1530*3fb517f7SJames Moore  * END entry points to allow external callers access to the volume.
1531*3fb517f7SJames Moore  */
1532*3fb517f7SJames Moore 
1533fa9e4066Sahrens /*
1534fa9e4066Sahrens  * Dirtbag ioctls to support mkfs(1M) for UFS filesystems.  See dkio(7I).
1535fa9e4066Sahrens  */
1536fa9e4066Sahrens /*ARGSUSED*/
1537fa9e4066Sahrens int
1538fa9e4066Sahrens zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
1539fa9e4066Sahrens {
1540fa9e4066Sahrens 	zvol_state_t *zv;
1541af2c4821Smaybee 	struct dk_cinfo dki;
1542fa9e4066Sahrens 	struct dk_minfo dkm;
1543af2c4821Smaybee 	struct dk_callback *dkc;
1544fa9e4066Sahrens 	int error = 0;
1545e7cbe64fSgw 	rl_t *rl;
1546fa9e4066Sahrens 
1547fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
1548fa9e4066Sahrens 
1549fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, getminor(dev));
1550fa9e4066Sahrens 
1551fa9e4066Sahrens 	if (zv == NULL) {
1552fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
1553fa9e4066Sahrens 		return (ENXIO);
1554fa9e4066Sahrens 	}
1555701f66c4SEric Taylor 	ASSERT(zv->zv_total_opens > 0);
1556fa9e4066Sahrens 
1557fa9e4066Sahrens 	switch (cmd) {
1558fa9e4066Sahrens 
1559fa9e4066Sahrens 	case DKIOCINFO:
1560af2c4821Smaybee 		bzero(&dki, sizeof (dki));
1561af2c4821Smaybee 		(void) strcpy(dki.dki_cname, "zvol");
1562af2c4821Smaybee 		(void) strcpy(dki.dki_dname, "zvol");
1563af2c4821Smaybee 		dki.dki_ctype = DKC_UNKNOWN;
15643adc9019SEric Taylor 		dki.dki_unit = getminor(dev);
1565af2c4821Smaybee 		dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs);
1566fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
1567af2c4821Smaybee 		if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag))
1568fa9e4066Sahrens 			error = EFAULT;
1569fa9e4066Sahrens 		return (error);
1570fa9e4066Sahrens 
1571fa9e4066Sahrens 	case DKIOCGMEDIAINFO:
1572fa9e4066Sahrens 		bzero(&dkm, sizeof (dkm));
1573fa9e4066Sahrens 		dkm.dki_lbsize = 1U << zv->zv_min_bs;
1574fa9e4066Sahrens 		dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
1575fa9e4066Sahrens 		dkm.dki_media_type = DK_UNKNOWN;
1576fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
1577fa9e4066Sahrens 		if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
1578fa9e4066Sahrens 			error = EFAULT;
1579fa9e4066Sahrens 		return (error);
1580fa9e4066Sahrens 
1581fa9e4066Sahrens 	case DKIOCGETEFI:
1582c7f714e2SEric Taylor 		{
1583c7f714e2SEric Taylor 			uint64_t vs = zv->zv_volsize;
1584c7f714e2SEric Taylor 			uint8_t bs = zv->zv_min_bs;
1585fa9e4066Sahrens 
158668a5ac4dSmaybee 			mutex_exit(&zvol_state_lock);
1587c7f714e2SEric Taylor 			error = zvol_getefi((void *)arg, flag, vs, bs);
1588c7f714e2SEric Taylor 			return (error);
158968a5ac4dSmaybee 		}
1590fa9e4066Sahrens 
1591feb08c6bSbillm 	case DKIOCFLUSHWRITECACHE:
1592af2c4821Smaybee 		dkc = (struct dk_callback *)arg;
1593701f66c4SEric Taylor 		mutex_exit(&zvol_state_lock);
1594feb08c6bSbillm 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1595af2c4821Smaybee 		if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) {
1596af2c4821Smaybee 			(*dkc->dkc_callback)(dkc->dkc_cookie, error);
1597af2c4821Smaybee 			error = 0;
1598af2c4821Smaybee 		}
1599701f66c4SEric Taylor 		return (error);
1600701f66c4SEric Taylor 
1601701f66c4SEric Taylor 	case DKIOCGETWCE:
1602701f66c4SEric Taylor 		{
1603701f66c4SEric Taylor 			int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0;
1604701f66c4SEric Taylor 			if (ddi_copyout(&wce, (void *)arg, sizeof (int),
1605701f66c4SEric Taylor 			    flag))
1606701f66c4SEric Taylor 				error = EFAULT;
1607701f66c4SEric Taylor 			break;
1608701f66c4SEric Taylor 		}
1609701f66c4SEric Taylor 	case DKIOCSETWCE:
1610701f66c4SEric Taylor 		{
1611701f66c4SEric Taylor 			int wce;
1612701f66c4SEric Taylor 			if (ddi_copyin((void *)arg, &wce, sizeof (int),
1613701f66c4SEric Taylor 			    flag)) {
1614701f66c4SEric Taylor 				error = EFAULT;
1615701f66c4SEric Taylor 				break;
1616701f66c4SEric Taylor 			}
1617701f66c4SEric Taylor 			if (wce) {
1618701f66c4SEric Taylor 				zv->zv_flags |= ZVOL_WCE;
1619701f66c4SEric Taylor 				mutex_exit(&zvol_state_lock);
1620701f66c4SEric Taylor 			} else {
1621701f66c4SEric Taylor 				zv->zv_flags &= ~ZVOL_WCE;
1622701f66c4SEric Taylor 				mutex_exit(&zvol_state_lock);
1623701f66c4SEric Taylor 				zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1624701f66c4SEric Taylor 			}
1625701f66c4SEric Taylor 			return (0);
1626701f66c4SEric Taylor 		}
1627feb08c6bSbillm 
1628b6130eadSmaybee 	case DKIOCGGEOM:
1629b6130eadSmaybee 	case DKIOCGVTOC:
1630e7cbe64fSgw 		/*
1631e7cbe64fSgw 		 * commands using these (like prtvtoc) expect ENOTSUP
1632e7cbe64fSgw 		 * since we're emulating an EFI label
1633e7cbe64fSgw 		 */
1634b6130eadSmaybee 		error = ENOTSUP;
1635b6130eadSmaybee 		break;
1636b6130eadSmaybee 
1637e7cbe64fSgw 	case DKIOCDUMPINIT:
1638e7cbe64fSgw 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1639e7cbe64fSgw 		    RL_WRITER);
1640e7cbe64fSgw 		error = zvol_dumpify(zv);
1641e7cbe64fSgw 		zfs_range_unlock(rl);
1642e7cbe64fSgw 		break;
1643e7cbe64fSgw 
1644e7cbe64fSgw 	case DKIOCDUMPFINI:
164506d5ae10SEric Taylor 		if (!(zv->zv_flags & ZVOL_DUMPIFIED))
164606d5ae10SEric Taylor 			break;
1647e7cbe64fSgw 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1648e7cbe64fSgw 		    RL_WRITER);
1649e7cbe64fSgw 		error = zvol_dump_fini(zv);
1650e7cbe64fSgw 		zfs_range_unlock(rl);
1651e7cbe64fSgw 		break;
1652e7cbe64fSgw 
1653fa9e4066Sahrens 	default:
165468a5ac4dSmaybee 		error = ENOTTY;
1655fa9e4066Sahrens 		break;
1656fa9e4066Sahrens 
1657fa9e4066Sahrens 	}
1658fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
1659fa9e4066Sahrens 	return (error);
1660fa9e4066Sahrens }
1661fa9e4066Sahrens 
1662fa9e4066Sahrens int
1663fa9e4066Sahrens zvol_busy(void)
1664fa9e4066Sahrens {
1665fa9e4066Sahrens 	return (zvol_minors != 0);
1666fa9e4066Sahrens }
1667fa9e4066Sahrens 
1668fa9e4066Sahrens void
1669fa9e4066Sahrens zvol_init(void)
1670fa9e4066Sahrens {
1671fa9e4066Sahrens 	VERIFY(ddi_soft_state_init(&zvol_state, sizeof (zvol_state_t), 1) == 0);
1672fa9e4066Sahrens 	mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
1673fa9e4066Sahrens }
1674fa9e4066Sahrens 
1675fa9e4066Sahrens void
1676fa9e4066Sahrens zvol_fini(void)
1677fa9e4066Sahrens {
1678fa9e4066Sahrens 	mutex_destroy(&zvol_state_lock);
1679fa9e4066Sahrens 	ddi_soft_state_fini(&zvol_state);
1680fa9e4066Sahrens }
1681e7cbe64fSgw 
1682e7cbe64fSgw static int
1683e7cbe64fSgw zvol_dump_init(zvol_state_t *zv, boolean_t resize)
1684e7cbe64fSgw {
1685e7cbe64fSgw 	dmu_tx_t *tx;
1686e7cbe64fSgw 	int error = 0;
1687e7cbe64fSgw 	objset_t *os = zv->zv_objset;
1688e7cbe64fSgw 	nvlist_t *nv = NULL;
16898d265e66SGeorge Wilson 	uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset));
1690e7cbe64fSgw 
1691e7cbe64fSgw 	ASSERT(MUTEX_HELD(&zvol_state_lock));
1692681d9761SEric Taylor 	error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0,
1693681d9761SEric Taylor 	    DMU_OBJECT_END);
1694681d9761SEric Taylor 	/* wait for dmu_free_long_range to actually free the blocks */
1695681d9761SEric Taylor 	txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
1696e7cbe64fSgw 
1697e7cbe64fSgw 	tx = dmu_tx_create(os);
1698e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1699681d9761SEric Taylor 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
1700e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
1701e7cbe64fSgw 	if (error) {
1702e7cbe64fSgw 		dmu_tx_abort(tx);
1703e7cbe64fSgw 		return (error);
1704e7cbe64fSgw 	}
1705e7cbe64fSgw 
1706e7cbe64fSgw 	/*
1707e7cbe64fSgw 	 * If we are resizing the dump device then we only need to
1708e7cbe64fSgw 	 * update the refreservation to match the newly updated
1709e7cbe64fSgw 	 * zvolsize. Otherwise, we save off the original state of the
1710e7cbe64fSgw 	 * zvol so that we can restore them if the zvol is ever undumpified.
1711e7cbe64fSgw 	 */
1712e7cbe64fSgw 	if (resize) {
1713e7cbe64fSgw 		error = zap_update(os, ZVOL_ZAP_OBJ,
1714e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1715e7cbe64fSgw 		    &zv->zv_volsize, tx);
1716e7cbe64fSgw 	} else {
1717afee20e4SGeorge Wilson 		uint64_t checksum, compress, refresrv, vbs, dedup;
171888b7b0f2SMatthew Ahrens 
1719e7cbe64fSgw 		error = dsl_prop_get_integer(zv->zv_name,
1720e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL);
1721e7cbe64fSgw 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
1722e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL);
1723e7cbe64fSgw 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
1724e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL);
172588b7b0f2SMatthew Ahrens 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
172688b7b0f2SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL);
17278d265e66SGeorge Wilson 		if (version >= SPA_VERSION_DEDUP) {
17288d265e66SGeorge Wilson 			error = error ? error :
17298d265e66SGeorge Wilson 			    dsl_prop_get_integer(zv->zv_name,
17308d265e66SGeorge Wilson 			    zfs_prop_to_name(ZFS_PROP_DEDUP), &dedup, NULL);
17318d265e66SGeorge Wilson 		}
1732e7cbe64fSgw 
1733e7cbe64fSgw 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1734e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1,
1735e7cbe64fSgw 		    &compress, tx);
1736e7cbe64fSgw 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1737e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx);
1738e7cbe64fSgw 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1739e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1740e7cbe64fSgw 		    &refresrv, tx);
174188b7b0f2SMatthew Ahrens 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
174288b7b0f2SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1,
174388b7b0f2SMatthew Ahrens 		    &vbs, tx);
1744681d9761SEric Taylor 		error = error ? error : dmu_object_set_blocksize(
1745681d9761SEric Taylor 		    os, ZVOL_OBJ, SPA_MAXBLOCKSIZE, 0, tx);
17468d265e66SGeorge Wilson 		if (version >= SPA_VERSION_DEDUP) {
17478d265e66SGeorge Wilson 			error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
17488d265e66SGeorge Wilson 			    zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1,
17498d265e66SGeorge Wilson 			    &dedup, tx);
17508d265e66SGeorge Wilson 		}
1751681d9761SEric Taylor 		if (error == 0)
1752681d9761SEric Taylor 			zv->zv_volblocksize = SPA_MAXBLOCKSIZE;
1753e7cbe64fSgw 	}
1754e7cbe64fSgw 	dmu_tx_commit(tx);
1755e7cbe64fSgw 
1756e7cbe64fSgw 	/*
1757e7cbe64fSgw 	 * We only need update the zvol's property if we are initializing
1758e7cbe64fSgw 	 * the dump area for the first time.
1759e7cbe64fSgw 	 */
1760e7cbe64fSgw 	if (!resize) {
1761e7cbe64fSgw 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1762e7cbe64fSgw 		VERIFY(nvlist_add_uint64(nv,
1763e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0);
1764e7cbe64fSgw 		VERIFY(nvlist_add_uint64(nv,
1765e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
1766e7cbe64fSgw 		    ZIO_COMPRESS_OFF) == 0);
1767e7cbe64fSgw 		VERIFY(nvlist_add_uint64(nv,
1768e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
1769e7cbe64fSgw 		    ZIO_CHECKSUM_OFF) == 0);
17708d265e66SGeorge Wilson 		if (version >= SPA_VERSION_DEDUP) {
17718d265e66SGeorge Wilson 			VERIFY(nvlist_add_uint64(nv,
17728d265e66SGeorge Wilson 			    zfs_prop_to_name(ZFS_PROP_DEDUP),
17738d265e66SGeorge Wilson 			    ZIO_CHECKSUM_OFF) == 0);
17748d265e66SGeorge Wilson 		}
1775e7cbe64fSgw 
177692241e0bSTom Erickson 		error = zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
177792241e0bSTom Erickson 		    nv, NULL);
1778e7cbe64fSgw 		nvlist_free(nv);
1779e7cbe64fSgw 
1780e7cbe64fSgw 		if (error)
1781e7cbe64fSgw 			return (error);
1782e7cbe64fSgw 	}
1783e7cbe64fSgw 
1784e7cbe64fSgw 	/* Allocate the space for the dump */
1785e7cbe64fSgw 	error = zvol_prealloc(zv);
1786e7cbe64fSgw 	return (error);
1787e7cbe64fSgw }
1788e7cbe64fSgw 
1789e7cbe64fSgw static int
1790e7cbe64fSgw zvol_dumpify(zvol_state_t *zv)
1791e7cbe64fSgw {
1792e7cbe64fSgw 	int error = 0;
1793e7cbe64fSgw 	uint64_t dumpsize = 0;
1794e7cbe64fSgw 	dmu_tx_t *tx;
1795e7cbe64fSgw 	objset_t *os = zv->zv_objset;
1796e7cbe64fSgw 
1797681d9761SEric Taylor 	if (zv->zv_flags & ZVOL_RDONLY)
1798e7cbe64fSgw 		return (EROFS);
1799e7cbe64fSgw 
1800e7cbe64fSgw 	if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE,
1801e7cbe64fSgw 	    8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) {
1802e7cbe64fSgw 		boolean_t resize = (dumpsize > 0) ? B_TRUE : B_FALSE;
1803e7cbe64fSgw 
1804e7cbe64fSgw 		if ((error = zvol_dump_init(zv, resize)) != 0) {
1805e7cbe64fSgw 			(void) zvol_dump_fini(zv);
1806e7cbe64fSgw 			return (error);
1807e7cbe64fSgw 		}
1808e7cbe64fSgw 	}
1809e7cbe64fSgw 
1810e7cbe64fSgw 	/*
1811e7cbe64fSgw 	 * Build up our lba mapping.
1812e7cbe64fSgw 	 */
1813e7cbe64fSgw 	error = zvol_get_lbas(zv);
1814e7cbe64fSgw 	if (error) {
1815e7cbe64fSgw 		(void) zvol_dump_fini(zv);
1816e7cbe64fSgw 		return (error);
1817e7cbe64fSgw 	}
1818e7cbe64fSgw 
1819e7cbe64fSgw 	tx = dmu_tx_create(os);
1820e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1821e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
1822e7cbe64fSgw 	if (error) {
1823e7cbe64fSgw 		dmu_tx_abort(tx);
1824e7cbe64fSgw 		(void) zvol_dump_fini(zv);
1825e7cbe64fSgw 		return (error);
1826e7cbe64fSgw 	}
1827e7cbe64fSgw 
1828e7cbe64fSgw 	zv->zv_flags |= ZVOL_DUMPIFIED;
1829e7cbe64fSgw 	error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1,
1830e7cbe64fSgw 	    &zv->zv_volsize, tx);
1831e7cbe64fSgw 	dmu_tx_commit(tx);
1832e7cbe64fSgw 
1833e7cbe64fSgw 	if (error) {
1834e7cbe64fSgw 		(void) zvol_dump_fini(zv);
1835e7cbe64fSgw 		return (error);
1836e7cbe64fSgw 	}
1837e7cbe64fSgw 
1838e7cbe64fSgw 	txg_wait_synced(dmu_objset_pool(os), 0);
1839e7cbe64fSgw 	return (0);
1840e7cbe64fSgw }
1841e7cbe64fSgw 
1842e7cbe64fSgw static int
1843e7cbe64fSgw zvol_dump_fini(zvol_state_t *zv)
1844e7cbe64fSgw {
1845e7cbe64fSgw 	dmu_tx_t *tx;
1846e7cbe64fSgw 	objset_t *os = zv->zv_objset;
1847e7cbe64fSgw 	nvlist_t *nv;
1848e7cbe64fSgw 	int error = 0;
1849afee20e4SGeorge Wilson 	uint64_t checksum, compress, refresrv, vbs, dedup;
18508d265e66SGeorge Wilson 	uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset));
1851e7cbe64fSgw 
1852b7e50089Smaybee 	/*
1853b7e50089Smaybee 	 * Attempt to restore the zvol back to its pre-dumpified state.
1854b7e50089Smaybee 	 * This is a best-effort attempt as it's possible that not all
1855b7e50089Smaybee 	 * of these properties were initialized during the dumpify process
1856b7e50089Smaybee 	 * (i.e. error during zvol_dump_init).
1857b7e50089Smaybee 	 */
1858b7e50089Smaybee 
1859e7cbe64fSgw 	tx = dmu_tx_create(os);
1860e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1861e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
1862e7cbe64fSgw 	if (error) {
1863e7cbe64fSgw 		dmu_tx_abort(tx);
1864e7cbe64fSgw 		return (error);
1865e7cbe64fSgw 	}
1866b7e50089Smaybee 	(void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx);
1867b7e50089Smaybee 	dmu_tx_commit(tx);
1868e7cbe64fSgw 
1869e7cbe64fSgw 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1870e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum);
1871e7cbe64fSgw 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1872e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress);
1873e7cbe64fSgw 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1874e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv);
187588b7b0f2SMatthew Ahrens 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
187688b7b0f2SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs);
1877e7cbe64fSgw 
1878e7cbe64fSgw 	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1879e7cbe64fSgw 	(void) nvlist_add_uint64(nv,
1880e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum);
1881e7cbe64fSgw 	(void) nvlist_add_uint64(nv,
1882e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress);
1883e7cbe64fSgw 	(void) nvlist_add_uint64(nv,
1884e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv);
18858d265e66SGeorge Wilson 	if (version >= SPA_VERSION_DEDUP &&
18868d265e66SGeorge Wilson 	    zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
18878d265e66SGeorge Wilson 	    zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, &dedup) == 0) {
18888d265e66SGeorge Wilson 		(void) nvlist_add_uint64(nv,
18898d265e66SGeorge Wilson 		    zfs_prop_to_name(ZFS_PROP_DEDUP), dedup);
18908d265e66SGeorge Wilson 	}
189192241e0bSTom Erickson 	(void) zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
189292241e0bSTom Erickson 	    nv, NULL);
1893e7cbe64fSgw 	nvlist_free(nv);
1894e7cbe64fSgw 
1895b7e50089Smaybee 	zvol_free_extents(zv);
1896b7e50089Smaybee 	zv->zv_flags &= ~ZVOL_DUMPIFIED;
1897b7e50089Smaybee 	(void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END);
1898681d9761SEric Taylor 	/* wait for dmu_free_long_range to actually free the blocks */
1899681d9761SEric Taylor 	txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
1900681d9761SEric Taylor 	tx = dmu_tx_create(os);
1901681d9761SEric Taylor 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
1902681d9761SEric Taylor 	error = dmu_tx_assign(tx, TXG_WAIT);
1903681d9761SEric Taylor 	if (error) {
1904681d9761SEric Taylor 		dmu_tx_abort(tx);
1905681d9761SEric Taylor 		return (error);
1906681d9761SEric Taylor 	}
1907b24ab676SJeff Bonwick 	if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0)
1908b24ab676SJeff Bonwick 		zv->zv_volblocksize = vbs;
1909681d9761SEric Taylor 	dmu_tx_commit(tx);
1910b7e50089Smaybee 
1911e7cbe64fSgw 	return (0);
1912e7cbe64fSgw }
1913