xref: /onnv-gate/usr/src/uts/common/fs/zfs/zvol.c (revision 12699:36aebb51164a)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
2212095SChris.Kirby@sun.com  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23789Sahrens  */
24789Sahrens 
2512294SMark.Musante@Sun.COM /* Portions Copyright 2010 Robert Milkowski */
2612294SMark.Musante@Sun.COM 
27789Sahrens /*
28789Sahrens  * ZFS volume emulation driver.
29789Sahrens  *
30789Sahrens  * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes.
31789Sahrens  * Volumes are accessed through the symbolic links named:
32789Sahrens  *
33789Sahrens  * /dev/zvol/dsk/<pool_name>/<dataset_name>
34789Sahrens  * /dev/zvol/rdsk/<pool_name>/<dataset_name>
35789Sahrens  *
3610588SEric.Taylor@Sun.COM  * These links are created by the /dev filesystem (sdev_zvolops.c).
37789Sahrens  * Volumes are persistent through reboot.  No user command needs to be
38789Sahrens  * run before opening and using a device.
39789Sahrens  */
40789Sahrens 
41789Sahrens #include <sys/types.h>
42789Sahrens #include <sys/param.h>
43789Sahrens #include <sys/errno.h>
44789Sahrens #include <sys/uio.h>
45789Sahrens #include <sys/buf.h>
46789Sahrens #include <sys/modctl.h>
47789Sahrens #include <sys/open.h>
48789Sahrens #include <sys/kmem.h>
49789Sahrens #include <sys/conf.h>
50789Sahrens #include <sys/cmn_err.h>
51789Sahrens #include <sys/stat.h>
52789Sahrens #include <sys/zap.h>
53789Sahrens #include <sys/spa.h>
54789Sahrens #include <sys/zio.h>
556423Sgw25295 #include <sys/dmu_traverse.h>
566423Sgw25295 #include <sys/dnode.h>
576423Sgw25295 #include <sys/dsl_dataset.h>
58789Sahrens #include <sys/dsl_prop.h>
59789Sahrens #include <sys/dkio.h>
60789Sahrens #include <sys/efi_partition.h>
61789Sahrens #include <sys/byteorder.h>
62789Sahrens #include <sys/pathname.h>
63789Sahrens #include <sys/ddi.h>
64789Sahrens #include <sys/sunddi.h>
65789Sahrens #include <sys/crc32.h>
66789Sahrens #include <sys/dirent.h>
67789Sahrens #include <sys/policy.h>
68789Sahrens #include <sys/fs/zfs.h>
69789Sahrens #include <sys/zfs_ioctl.h>
70789Sahrens #include <sys/mkdev.h>
711141Sperrin #include <sys/zil.h>
722237Smaybee #include <sys/refcount.h>
733755Sperrin #include <sys/zfs_znode.h>
743755Sperrin #include <sys/zfs_rlock.h>
756423Sgw25295 #include <sys/vdev_disk.h>
766423Sgw25295 #include <sys/vdev_impl.h>
776423Sgw25295 #include <sys/zvol.h>
786423Sgw25295 #include <sys/dumphdr.h>
798227SNeil.Perrin@Sun.COM #include <sys/zil_impl.h>
80789Sahrens 
81789Sahrens #include "zfs_namecheck.h"
82789Sahrens 
8312527SChris.Kirby@oracle.com void *zfsdev_state;
8410298SMatthew.Ahrens@Sun.COM static char *zvol_tag = "zvol_tag";
85789Sahrens 
866423Sgw25295 #define	ZVOL_DUMPSIZE		"dumpsize"
87789Sahrens 
88789Sahrens /*
8912527SChris.Kirby@oracle.com  * This lock protects the zfsdev_state structure from being modified
90789Sahrens  * while it's being used, e.g. an open that comes in before a create
91789Sahrens  * finishes.  It also protects temporary opens of the dataset so that,
92789Sahrens  * e.g., an open doesn't get a spurious EBUSY.
93789Sahrens  */
9412527SChris.Kirby@oracle.com kmutex_t zfsdev_state_lock;
95789Sahrens static uint32_t zvol_minors;
96789Sahrens 
976423Sgw25295 typedef struct zvol_extent {
987837SMatthew.Ahrens@Sun.COM 	list_node_t	ze_node;
996423Sgw25295 	dva_t		ze_dva;		/* dva associated with this extent */
1007837SMatthew.Ahrens@Sun.COM 	uint64_t	ze_nblks;	/* number of blocks in extent */
1016423Sgw25295 } zvol_extent_t;
1026423Sgw25295 
1036423Sgw25295 /*
104789Sahrens  * The in-core state of each volume.
105789Sahrens  */
106789Sahrens typedef struct zvol_state {
107789Sahrens 	char		zv_name[MAXPATHLEN]; /* pool/dd name */
108789Sahrens 	uint64_t	zv_volsize;	/* amount of space we advertise */
1093063Sperrin 	uint64_t	zv_volblocksize; /* volume block size */
110789Sahrens 	minor_t		zv_minor;	/* minor number */
111789Sahrens 	uint8_t		zv_min_bs;	/* minimum addressable block shift */
1129303SEric.Taylor@Sun.COM 	uint8_t		zv_flags;	/* readonly, dumpified, etc. */
113789Sahrens 	objset_t	*zv_objset;	/* objset handle */
114789Sahrens 	uint32_t	zv_open_count[OTYPCNT];	/* open counts */
115789Sahrens 	uint32_t	zv_total_opens;	/* total open count */
1161141Sperrin 	zilog_t		*zv_zilog;	/* ZIL handle */
1177837SMatthew.Ahrens@Sun.COM 	list_t		zv_extents;	/* List of extents for dump */
1183755Sperrin 	znode_t		zv_znode;	/* for range locking */
11912123STim.Haley@Sun.COM 	dmu_buf_t	*zv_dbuf;	/* bonus handle */
120789Sahrens } zvol_state_t;
121789Sahrens 
1223063Sperrin /*
1236423Sgw25295  * zvol specific flags
1246423Sgw25295  */
1256423Sgw25295 #define	ZVOL_RDONLY	0x1
1266423Sgw25295 #define	ZVOL_DUMPIFIED	0x2
1277405SEric.Taylor@Sun.COM #define	ZVOL_EXCL	0x4
1289303SEric.Taylor@Sun.COM #define	ZVOL_WCE	0x8
1296423Sgw25295 
1306423Sgw25295 /*
1313063Sperrin  * zvol maximum transfer in one DMU tx.
1323063Sperrin  */
1333063Sperrin int zvol_maxphys = DMU_MAX_ACCESS/2;
1343063Sperrin 
13511022STom.Erickson@Sun.COM extern int zfs_set_prop_nvlist(const char *, zprop_source_t,
13611022STom.Erickson@Sun.COM     nvlist_t *, nvlist_t **);
13710588SEric.Taylor@Sun.COM static int zvol_remove_zv(zvol_state_t *);
1383638Sbillm static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio);
1396423Sgw25295 static int zvol_dumpify(zvol_state_t *zv);
1406423Sgw25295 static int zvol_dump_fini(zvol_state_t *zv);
1416423Sgw25295 static int zvol_dump_init(zvol_state_t *zv, boolean_t resize);
1423063Sperrin 
143789Sahrens static void
14410588SEric.Taylor@Sun.COM zvol_size_changed(uint64_t volsize, major_t maj, minor_t min)
145789Sahrens {
14610588SEric.Taylor@Sun.COM 	dev_t dev = makedevice(maj, min);
147789Sahrens 
148789Sahrens 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
14910588SEric.Taylor@Sun.COM 	    "Size", volsize) == DDI_SUCCESS);
150789Sahrens 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
15110588SEric.Taylor@Sun.COM 	    "Nblocks", lbtodb(volsize)) == DDI_SUCCESS);
1526423Sgw25295 
1536423Sgw25295 	/* Notify specfs to invalidate the cached size */
1546423Sgw25295 	spec_size_invalidate(dev, VBLK);
1556423Sgw25295 	spec_size_invalidate(dev, VCHR);
156789Sahrens }
157789Sahrens 
158789Sahrens int
1592676Seschrock zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
160789Sahrens {
1612676Seschrock 	if (volsize == 0)
162789Sahrens 		return (EINVAL);
163789Sahrens 
1642676Seschrock 	if (volsize % blocksize != 0)
1651133Seschrock 		return (EINVAL);
1661133Seschrock 
167789Sahrens #ifdef _ILP32
1682676Seschrock 	if (volsize - 1 > SPEC_MAXOFFSET_T)
169789Sahrens 		return (EOVERFLOW);
170789Sahrens #endif
171789Sahrens 	return (0);
172789Sahrens }
173789Sahrens 
174789Sahrens int
1752676Seschrock zvol_check_volblocksize(uint64_t volblocksize)
176789Sahrens {
1772676Seschrock 	if (volblocksize < SPA_MINBLOCKSIZE ||
1782676Seschrock 	    volblocksize > SPA_MAXBLOCKSIZE ||
1792676Seschrock 	    !ISP2(volblocksize))
180789Sahrens 		return (EDOM);
181789Sahrens 
182789Sahrens 	return (0);
183789Sahrens }
184789Sahrens 
185789Sahrens int
1862885Sahrens zvol_get_stats(objset_t *os, nvlist_t *nv)
187789Sahrens {
188789Sahrens 	int error;
189789Sahrens 	dmu_object_info_t doi;
1902885Sahrens 	uint64_t val;
191789Sahrens 
1922885Sahrens 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
193789Sahrens 	if (error)
194789Sahrens 		return (error);
195789Sahrens 
1962885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
1972885Sahrens 
198789Sahrens 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
199789Sahrens 
2002885Sahrens 	if (error == 0) {
2012885Sahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
2022885Sahrens 		    doi.doi_data_block_size);
2032885Sahrens 	}
204789Sahrens 
205789Sahrens 	return (error);
206789Sahrens }
207789Sahrens 
208789Sahrens static zvol_state_t *
2092676Seschrock zvol_minor_lookup(const char *name)
210789Sahrens {
211789Sahrens 	minor_t minor;
212789Sahrens 	zvol_state_t *zv;
213789Sahrens 
21412527SChris.Kirby@oracle.com 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
215789Sahrens 
21612527SChris.Kirby@oracle.com 	for (minor = 1; minor <= ZFSDEV_MAX_MINOR; minor++) {
21712527SChris.Kirby@oracle.com 		zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
218789Sahrens 		if (zv == NULL)
219789Sahrens 			continue;
220789Sahrens 		if (strcmp(zv->zv_name, name) == 0)
22112095SChris.Kirby@sun.com 			return (zv);
222789Sahrens 	}
223789Sahrens 
22412095SChris.Kirby@sun.com 	return (NULL);
225789Sahrens }
226789Sahrens 
2276423Sgw25295 /* extent mapping arg */
2286423Sgw25295 struct maparg {
2297837SMatthew.Ahrens@Sun.COM 	zvol_state_t	*ma_zv;
2307837SMatthew.Ahrens@Sun.COM 	uint64_t	ma_blks;
2316423Sgw25295 };
2326423Sgw25295 
2336423Sgw25295 /*ARGSUSED*/
2346423Sgw25295 static int
23512296SLin.Ling@Sun.COM zvol_map_block(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, arc_buf_t *pbuf,
23610922SJeff.Bonwick@Sun.COM     const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
2376423Sgw25295 {
2387837SMatthew.Ahrens@Sun.COM 	struct maparg *ma = arg;
2397837SMatthew.Ahrens@Sun.COM 	zvol_extent_t *ze;
2407837SMatthew.Ahrens@Sun.COM 	int bs = ma->ma_zv->zv_volblocksize;
2416423Sgw25295 
2427837SMatthew.Ahrens@Sun.COM 	if (bp == NULL || zb->zb_object != ZVOL_OBJ || zb->zb_level != 0)
2436423Sgw25295 		return (0);
2446423Sgw25295 
2457837SMatthew.Ahrens@Sun.COM 	VERIFY3U(ma->ma_blks, ==, zb->zb_blkid);
2467837SMatthew.Ahrens@Sun.COM 	ma->ma_blks++;
2477837SMatthew.Ahrens@Sun.COM 
2486423Sgw25295 	/* Abort immediately if we have encountered gang blocks */
2497837SMatthew.Ahrens@Sun.COM 	if (BP_IS_GANG(bp))
2507837SMatthew.Ahrens@Sun.COM 		return (EFRAGS);
2516423Sgw25295 
2527837SMatthew.Ahrens@Sun.COM 	/*
2537837SMatthew.Ahrens@Sun.COM 	 * See if the block is at the end of the previous extent.
2547837SMatthew.Ahrens@Sun.COM 	 */
2557837SMatthew.Ahrens@Sun.COM 	ze = list_tail(&ma->ma_zv->zv_extents);
2567837SMatthew.Ahrens@Sun.COM 	if (ze &&
2577837SMatthew.Ahrens@Sun.COM 	    DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) &&
2587837SMatthew.Ahrens@Sun.COM 	    DVA_GET_OFFSET(BP_IDENTITY(bp)) ==
2597837SMatthew.Ahrens@Sun.COM 	    DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) {
2607837SMatthew.Ahrens@Sun.COM 		ze->ze_nblks++;
2616423Sgw25295 		return (0);
2626423Sgw25295 	}
2636423Sgw25295 
2647837SMatthew.Ahrens@Sun.COM 	dprintf_bp(bp, "%s", "next blkptr:");
2657837SMatthew.Ahrens@Sun.COM 
2667837SMatthew.Ahrens@Sun.COM 	/* start a new extent */
2677837SMatthew.Ahrens@Sun.COM 	ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP);
2687837SMatthew.Ahrens@Sun.COM 	ze->ze_dva = bp->blk_dva[0];	/* structure assignment */
2697837SMatthew.Ahrens@Sun.COM 	ze->ze_nblks = 1;
2707837SMatthew.Ahrens@Sun.COM 	list_insert_tail(&ma->ma_zv->zv_extents, ze);
2717837SMatthew.Ahrens@Sun.COM 	return (0);
2727837SMatthew.Ahrens@Sun.COM }
2737837SMatthew.Ahrens@Sun.COM 
2747837SMatthew.Ahrens@Sun.COM static void
2757837SMatthew.Ahrens@Sun.COM zvol_free_extents(zvol_state_t *zv)
2767837SMatthew.Ahrens@Sun.COM {
2777837SMatthew.Ahrens@Sun.COM 	zvol_extent_t *ze;
2787837SMatthew.Ahrens@Sun.COM 
2797837SMatthew.Ahrens@Sun.COM 	while (ze = list_head(&zv->zv_extents)) {
2807837SMatthew.Ahrens@Sun.COM 		list_remove(&zv->zv_extents, ze);
2817837SMatthew.Ahrens@Sun.COM 		kmem_free(ze, sizeof (zvol_extent_t));
2827837SMatthew.Ahrens@Sun.COM 	}
2837837SMatthew.Ahrens@Sun.COM }
2847837SMatthew.Ahrens@Sun.COM 
2857837SMatthew.Ahrens@Sun.COM static int
2867837SMatthew.Ahrens@Sun.COM zvol_get_lbas(zvol_state_t *zv)
2877837SMatthew.Ahrens@Sun.COM {
28811689SEric.Taylor@Sun.COM 	objset_t *os = zv->zv_objset;
2897837SMatthew.Ahrens@Sun.COM 	struct maparg	ma;
2907837SMatthew.Ahrens@Sun.COM 	int		err;
2917837SMatthew.Ahrens@Sun.COM 
2927837SMatthew.Ahrens@Sun.COM 	ma.ma_zv = zv;
2937837SMatthew.Ahrens@Sun.COM 	ma.ma_blks = 0;
2947837SMatthew.Ahrens@Sun.COM 	zvol_free_extents(zv);
2957837SMatthew.Ahrens@Sun.COM 
29611689SEric.Taylor@Sun.COM 	/* commit any in-flight changes before traversing the dataset */
29711689SEric.Taylor@Sun.COM 	txg_wait_synced(dmu_objset_pool(os), 0);
29811689SEric.Taylor@Sun.COM 	err = traverse_dataset(dmu_objset_ds(os), 0,
2997837SMatthew.Ahrens@Sun.COM 	    TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma);
3007837SMatthew.Ahrens@Sun.COM 	if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) {
3017837SMatthew.Ahrens@Sun.COM 		zvol_free_extents(zv);
3027837SMatthew.Ahrens@Sun.COM 		return (err ? err : EIO);
3036423Sgw25295 	}
3046423Sgw25295 
3056423Sgw25295 	return (0);
3066423Sgw25295 }
3076423Sgw25295 
3084543Smarks /* ARGSUSED */
309789Sahrens void
3104543Smarks zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
311789Sahrens {
3125331Samw 	zfs_creat_t *zct = arg;
3135331Samw 	nvlist_t *nvprops = zct->zct_props;
314789Sahrens 	int error;
3152676Seschrock 	uint64_t volblocksize, volsize;
316789Sahrens 
3174543Smarks 	VERIFY(nvlist_lookup_uint64(nvprops,
3182676Seschrock 	    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
3194543Smarks 	if (nvlist_lookup_uint64(nvprops,
3202676Seschrock 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
3212676Seschrock 		volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
3222676Seschrock 
3232676Seschrock 	/*
3246423Sgw25295 	 * These properties must be removed from the list so the generic
3252676Seschrock 	 * property setting step won't apply to them.
3262676Seschrock 	 */
3274543Smarks 	VERIFY(nvlist_remove_all(nvprops,
3282676Seschrock 	    zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
3294543Smarks 	(void) nvlist_remove_all(nvprops,
3302676Seschrock 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
3312676Seschrock 
3322676Seschrock 	error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
333789Sahrens 	    DMU_OT_NONE, 0, tx);
334789Sahrens 	ASSERT(error == 0);
335789Sahrens 
336789Sahrens 	error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
337789Sahrens 	    DMU_OT_NONE, 0, tx);
338789Sahrens 	ASSERT(error == 0);
339789Sahrens 
3402676Seschrock 	error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
341789Sahrens 	ASSERT(error == 0);
342789Sahrens }
343789Sahrens 
344789Sahrens /*
3451141Sperrin  * Replay a TX_WRITE ZIL transaction that didn't get committed
3461141Sperrin  * after a system failure
3471141Sperrin  */
3481141Sperrin static int
3491141Sperrin zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
3501141Sperrin {
3511141Sperrin 	objset_t *os = zv->zv_objset;
3521141Sperrin 	char *data = (char *)(lr + 1);	/* data follows lr_write_t */
35310922SJeff.Bonwick@Sun.COM 	uint64_t offset, length;
3541141Sperrin 	dmu_tx_t *tx;
3551141Sperrin 	int error;
3561141Sperrin 
3571141Sperrin 	if (byteswap)
3581141Sperrin 		byteswap_uint64_array(lr, sizeof (*lr));
3591141Sperrin 
36010922SJeff.Bonwick@Sun.COM 	offset = lr->lr_offset;
36110922SJeff.Bonwick@Sun.COM 	length = lr->lr_length;
36210922SJeff.Bonwick@Sun.COM 
36310922SJeff.Bonwick@Sun.COM 	/* If it's a dmu_sync() block, write the whole block */
36410922SJeff.Bonwick@Sun.COM 	if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
36510922SJeff.Bonwick@Sun.COM 		uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
36610922SJeff.Bonwick@Sun.COM 		if (length < blocksize) {
36710922SJeff.Bonwick@Sun.COM 			offset -= offset % blocksize;
36810922SJeff.Bonwick@Sun.COM 			length = blocksize;
36910922SJeff.Bonwick@Sun.COM 		}
37010922SJeff.Bonwick@Sun.COM 	}
37110800SNeil.Perrin@Sun.COM 
3721141Sperrin 	tx = dmu_tx_create(os);
37310922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length);
3748227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_WAIT);
3751141Sperrin 	if (error) {
3761141Sperrin 		dmu_tx_abort(tx);
3771141Sperrin 	} else {
37810922SJeff.Bonwick@Sun.COM 		dmu_write(os, ZVOL_OBJ, offset, length, data, tx);
3791141Sperrin 		dmu_tx_commit(tx);
3801141Sperrin 	}
3811141Sperrin 
3821141Sperrin 	return (error);
3831141Sperrin }
3841141Sperrin 
3851141Sperrin /* ARGSUSED */
3861141Sperrin static int
3871141Sperrin zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
3881141Sperrin {
3891141Sperrin 	return (ENOTSUP);
3901141Sperrin }
3911141Sperrin 
3921141Sperrin /*
3931141Sperrin  * Callback vectors for replaying records.
3941141Sperrin  * Only TX_WRITE is needed for zvol.
3951141Sperrin  */
3961141Sperrin zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = {
3971141Sperrin 	zvol_replay_err,	/* 0 no such transaction type */
3981141Sperrin 	zvol_replay_err,	/* TX_CREATE */
3991141Sperrin 	zvol_replay_err,	/* TX_MKDIR */
4001141Sperrin 	zvol_replay_err,	/* TX_MKXATTR */
4011141Sperrin 	zvol_replay_err,	/* TX_SYMLINK */
4021141Sperrin 	zvol_replay_err,	/* TX_REMOVE */
4031141Sperrin 	zvol_replay_err,	/* TX_RMDIR */
4041141Sperrin 	zvol_replay_err,	/* TX_LINK */
4051141Sperrin 	zvol_replay_err,	/* TX_RENAME */
4061141Sperrin 	zvol_replay_write,	/* TX_WRITE */
4071141Sperrin 	zvol_replay_err,	/* TX_TRUNCATE */
4081141Sperrin 	zvol_replay_err,	/* TX_SETATTR */
4091141Sperrin 	zvol_replay_err,	/* TX_ACL */
41010800SNeil.Perrin@Sun.COM 	zvol_replay_err,	/* TX_CREATE_ACL */
41110800SNeil.Perrin@Sun.COM 	zvol_replay_err,	/* TX_CREATE_ATTR */
41210800SNeil.Perrin@Sun.COM 	zvol_replay_err,	/* TX_CREATE_ACL_ATTR */
41310800SNeil.Perrin@Sun.COM 	zvol_replay_err,	/* TX_MKDIR_ACL */
41410800SNeil.Perrin@Sun.COM 	zvol_replay_err,	/* TX_MKDIR_ATTR */
41510800SNeil.Perrin@Sun.COM 	zvol_replay_err,	/* TX_MKDIR_ACL_ATTR */
41610800SNeil.Perrin@Sun.COM 	zvol_replay_err,	/* TX_WRITE2 */
4171141Sperrin };
4181141Sperrin 
41910588SEric.Taylor@Sun.COM int
42010588SEric.Taylor@Sun.COM zvol_name2minor(const char *name, minor_t *minor)
42110588SEric.Taylor@Sun.COM {
42210588SEric.Taylor@Sun.COM 	zvol_state_t *zv;
42310588SEric.Taylor@Sun.COM 
42412527SChris.Kirby@oracle.com 	mutex_enter(&zfsdev_state_lock);
42510588SEric.Taylor@Sun.COM 	zv = zvol_minor_lookup(name);
42610588SEric.Taylor@Sun.COM 	if (minor && zv)
42710588SEric.Taylor@Sun.COM 		*minor = zv->zv_minor;
42812527SChris.Kirby@oracle.com 	mutex_exit(&zfsdev_state_lock);
42910588SEric.Taylor@Sun.COM 	return (zv ? 0 : -1);
43010588SEric.Taylor@Sun.COM }
43110588SEric.Taylor@Sun.COM 
4321141Sperrin /*
4336423Sgw25295  * Create a minor node (plus a whole lot more) for the specified volume.
434789Sahrens  */
435789Sahrens int
43610588SEric.Taylor@Sun.COM zvol_create_minor(const char *name)
437789Sahrens {
43812527SChris.Kirby@oracle.com 	zfs_soft_state_t *zs;
439789Sahrens 	zvol_state_t *zv;
440789Sahrens 	objset_t *os;
4413063Sperrin 	dmu_object_info_t doi;
442789Sahrens 	minor_t minor = 0;
443789Sahrens 	char chrbuf[30], blkbuf[30];
444789Sahrens 	int error;
445789Sahrens 
44612527SChris.Kirby@oracle.com 	mutex_enter(&zfsdev_state_lock);
447789Sahrens 
44811422SMark.Musante@Sun.COM 	if (zvol_minor_lookup(name) != NULL) {
44912527SChris.Kirby@oracle.com 		mutex_exit(&zfsdev_state_lock);
450789Sahrens 		return (EEXIST);
451789Sahrens 	}
452789Sahrens 
45310298SMatthew.Ahrens@Sun.COM 	/* lie and say we're read-only */
45410298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, zvol_tag, &os);
455789Sahrens 
456789Sahrens 	if (error) {
45712527SChris.Kirby@oracle.com 		mutex_exit(&zfsdev_state_lock);
458789Sahrens 		return (error);
459789Sahrens 	}
460789Sahrens 
46112527SChris.Kirby@oracle.com 	if ((minor = zfsdev_minor_alloc()) == 0) {
46210298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(os, zvol_tag);
46312527SChris.Kirby@oracle.com 		mutex_exit(&zfsdev_state_lock);
464789Sahrens 		return (ENXIO);
465789Sahrens 	}
466789Sahrens 
46712527SChris.Kirby@oracle.com 	if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS) {
46810298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(os, zvol_tag);
46912527SChris.Kirby@oracle.com 		mutex_exit(&zfsdev_state_lock);
470789Sahrens 		return (EAGAIN);
471789Sahrens 	}
4722676Seschrock 	(void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME,
4732676Seschrock 	    (char *)name);
474789Sahrens 
47510588SEric.Taylor@Sun.COM 	(void) snprintf(chrbuf, sizeof (chrbuf), "%u,raw", minor);
476789Sahrens 
477789Sahrens 	if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR,
478789Sahrens 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
47912527SChris.Kirby@oracle.com 		ddi_soft_state_free(zfsdev_state, minor);
48010298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(os, zvol_tag);
48112527SChris.Kirby@oracle.com 		mutex_exit(&zfsdev_state_lock);
482789Sahrens 		return (EAGAIN);
483789Sahrens 	}
484789Sahrens 
48510588SEric.Taylor@Sun.COM 	(void) snprintf(blkbuf, sizeof (blkbuf), "%u", minor);
486789Sahrens 
487789Sahrens 	if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK,
488789Sahrens 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
489789Sahrens 		ddi_remove_minor_node(zfs_dip, chrbuf);
49012527SChris.Kirby@oracle.com 		ddi_soft_state_free(zfsdev_state, minor);
49110298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(os, zvol_tag);
49212527SChris.Kirby@oracle.com 		mutex_exit(&zfsdev_state_lock);
493789Sahrens 		return (EAGAIN);
494789Sahrens 	}
495789Sahrens 
49612527SChris.Kirby@oracle.com 	zs = ddi_get_soft_state(zfsdev_state, minor);
49712527SChris.Kirby@oracle.com 	zs->zss_type = ZSST_ZVOL;
49812527SChris.Kirby@oracle.com 	zv = zs->zss_data = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP);
49910588SEric.Taylor@Sun.COM 	(void) strlcpy(zv->zv_name, name, MAXPATHLEN);
500789Sahrens 	zv->zv_min_bs = DEV_BSHIFT;
501789Sahrens 	zv->zv_minor = minor;
502789Sahrens 	zv->zv_objset = os;
50310588SEric.Taylor@Sun.COM 	if (dmu_objset_is_snapshot(os))
50410588SEric.Taylor@Sun.COM 		zv->zv_flags |= ZVOL_RDONLY;
5053755Sperrin 	mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
5063755Sperrin 	avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
5073755Sperrin 	    sizeof (rl_t), offsetof(rl_t, r_node));
5087837SMatthew.Ahrens@Sun.COM 	list_create(&zv->zv_extents, sizeof (zvol_extent_t),
5097837SMatthew.Ahrens@Sun.COM 	    offsetof(zvol_extent_t, ze_node));
5103063Sperrin 	/* get and cache the blocksize */
5113063Sperrin 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
5123063Sperrin 	ASSERT(error == 0);
5133063Sperrin 	zv->zv_volblocksize = doi.doi_data_block_size;
5141141Sperrin 
51512294SMark.Musante@Sun.COM 	if (zil_replay_disable)
51612294SMark.Musante@Sun.COM 		zil_destroy(dmu_objset_zil(os), B_FALSE);
51712294SMark.Musante@Sun.COM 	else
51812294SMark.Musante@Sun.COM 		zil_replay(os, zv, zvol_replay_vector);
51910588SEric.Taylor@Sun.COM 	dmu_objset_disown(os, zvol_tag);
52010588SEric.Taylor@Sun.COM 	zv->zv_objset = NULL;
521789Sahrens 
522789Sahrens 	zvol_minors++;
523789Sahrens 
52412527SChris.Kirby@oracle.com 	mutex_exit(&zfsdev_state_lock);
525789Sahrens 
526789Sahrens 	return (0);
527789Sahrens }
528789Sahrens 
529789Sahrens /*
530789Sahrens  * Remove minor node for the specified volume.
531789Sahrens  */
53210588SEric.Taylor@Sun.COM static int
53310588SEric.Taylor@Sun.COM zvol_remove_zv(zvol_state_t *zv)
534789Sahrens {
53510588SEric.Taylor@Sun.COM 	char nmbuf[20];
53612527SChris.Kirby@oracle.com 	minor_t minor = zv->zv_minor;
537789Sahrens 
53812527SChris.Kirby@oracle.com 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
53910588SEric.Taylor@Sun.COM 	if (zv->zv_total_opens != 0)
540789Sahrens 		return (EBUSY);
541789Sahrens 
54212527SChris.Kirby@oracle.com 	(void) snprintf(nmbuf, sizeof (nmbuf), "%u,raw", minor);
54310588SEric.Taylor@Sun.COM 	ddi_remove_minor_node(zfs_dip, nmbuf);
544789Sahrens 
54512527SChris.Kirby@oracle.com 	(void) snprintf(nmbuf, sizeof (nmbuf), "%u", minor);
54610588SEric.Taylor@Sun.COM 	ddi_remove_minor_node(zfs_dip, nmbuf);
547789Sahrens 
5483755Sperrin 	avl_destroy(&zv->zv_znode.z_range_avl);
5493755Sperrin 	mutex_destroy(&zv->zv_znode.z_range_lock);
550789Sahrens 
55112527SChris.Kirby@oracle.com 	kmem_free(zv, sizeof (zvol_state_t));
55212527SChris.Kirby@oracle.com 
55312527SChris.Kirby@oracle.com 	ddi_soft_state_free(zfsdev_state, minor);
554789Sahrens 
555789Sahrens 	zvol_minors--;
55610588SEric.Taylor@Sun.COM 	return (0);
55710588SEric.Taylor@Sun.COM }
558789Sahrens 
55910588SEric.Taylor@Sun.COM int
56010588SEric.Taylor@Sun.COM zvol_remove_minor(const char *name)
56110588SEric.Taylor@Sun.COM {
56210588SEric.Taylor@Sun.COM 	zvol_state_t *zv;
56310588SEric.Taylor@Sun.COM 	int rc;
56410588SEric.Taylor@Sun.COM 
56512527SChris.Kirby@oracle.com 	mutex_enter(&zfsdev_state_lock);
56610588SEric.Taylor@Sun.COM 	if ((zv = zvol_minor_lookup(name)) == NULL) {
56712527SChris.Kirby@oracle.com 		mutex_exit(&zfsdev_state_lock);
56810588SEric.Taylor@Sun.COM 		return (ENXIO);
56910588SEric.Taylor@Sun.COM 	}
57010588SEric.Taylor@Sun.COM 	rc = zvol_remove_zv(zv);
57112527SChris.Kirby@oracle.com 	mutex_exit(&zfsdev_state_lock);
57210588SEric.Taylor@Sun.COM 	return (rc);
57310588SEric.Taylor@Sun.COM }
574789Sahrens 
57510588SEric.Taylor@Sun.COM int
57610588SEric.Taylor@Sun.COM zvol_first_open(zvol_state_t *zv)
57710588SEric.Taylor@Sun.COM {
57810588SEric.Taylor@Sun.COM 	objset_t *os;
57910588SEric.Taylor@Sun.COM 	uint64_t volsize;
58010588SEric.Taylor@Sun.COM 	int error;
58110588SEric.Taylor@Sun.COM 	uint64_t readonly;
58210588SEric.Taylor@Sun.COM 
58310588SEric.Taylor@Sun.COM 	/* lie and say we're read-only */
58410588SEric.Taylor@Sun.COM 	error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, B_TRUE,
58510588SEric.Taylor@Sun.COM 	    zvol_tag, &os);
58610588SEric.Taylor@Sun.COM 	if (error)
58710588SEric.Taylor@Sun.COM 		return (error);
58810588SEric.Taylor@Sun.COM 
58910588SEric.Taylor@Sun.COM 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
59010588SEric.Taylor@Sun.COM 	if (error) {
59110588SEric.Taylor@Sun.COM 		ASSERT(error == 0);
59210588SEric.Taylor@Sun.COM 		dmu_objset_disown(os, zvol_tag);
59310588SEric.Taylor@Sun.COM 		return (error);
59410588SEric.Taylor@Sun.COM 	}
59510588SEric.Taylor@Sun.COM 	zv->zv_objset = os;
59612123STim.Haley@Sun.COM 	error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf);
59712123STim.Haley@Sun.COM 	if (error) {
59812123STim.Haley@Sun.COM 		dmu_objset_disown(os, zvol_tag);
59912123STim.Haley@Sun.COM 		return (error);
60012123STim.Haley@Sun.COM 	}
60110588SEric.Taylor@Sun.COM 	zv->zv_volsize = volsize;
60210588SEric.Taylor@Sun.COM 	zv->zv_zilog = zil_open(os, zvol_get_data);
60310588SEric.Taylor@Sun.COM 	zvol_size_changed(zv->zv_volsize, ddi_driver_major(zfs_dip),
60410588SEric.Taylor@Sun.COM 	    zv->zv_minor);
60510588SEric.Taylor@Sun.COM 
60610588SEric.Taylor@Sun.COM 	VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &readonly,
60710588SEric.Taylor@Sun.COM 	    NULL) == 0);
60810588SEric.Taylor@Sun.COM 	if (readonly || dmu_objset_is_snapshot(os))
60910588SEric.Taylor@Sun.COM 		zv->zv_flags |= ZVOL_RDONLY;
61010588SEric.Taylor@Sun.COM 	else
61110588SEric.Taylor@Sun.COM 		zv->zv_flags &= ~ZVOL_RDONLY;
61210588SEric.Taylor@Sun.COM 	return (error);
61310588SEric.Taylor@Sun.COM }
61410588SEric.Taylor@Sun.COM 
61510588SEric.Taylor@Sun.COM void
61610588SEric.Taylor@Sun.COM zvol_last_close(zvol_state_t *zv)
61710588SEric.Taylor@Sun.COM {
61810588SEric.Taylor@Sun.COM 	zil_close(zv->zv_zilog);
61910588SEric.Taylor@Sun.COM 	zv->zv_zilog = NULL;
62012123STim.Haley@Sun.COM 	dmu_buf_rele(zv->zv_dbuf, zvol_tag);
62112123STim.Haley@Sun.COM 	zv->zv_dbuf = NULL;
62210588SEric.Taylor@Sun.COM 	dmu_objset_disown(zv->zv_objset, zvol_tag);
62310588SEric.Taylor@Sun.COM 	zv->zv_objset = NULL;
624789Sahrens }
625789Sahrens 
6266423Sgw25295 int
6276423Sgw25295 zvol_prealloc(zvol_state_t *zv)
6286423Sgw25295 {
6296423Sgw25295 	objset_t *os = zv->zv_objset;
6306423Sgw25295 	dmu_tx_t *tx;
6316423Sgw25295 	uint64_t refd, avail, usedobjs, availobjs;
6326423Sgw25295 	uint64_t resid = zv->zv_volsize;
6336423Sgw25295 	uint64_t off = 0;
6346423Sgw25295 
6356423Sgw25295 	/* Check the space usage before attempting to allocate the space */
6366423Sgw25295 	dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs);
6376423Sgw25295 	if (avail < zv->zv_volsize)
6386423Sgw25295 		return (ENOSPC);
6396423Sgw25295 
6406423Sgw25295 	/* Free old extents if they exist */
6416423Sgw25295 	zvol_free_extents(zv);
6426423Sgw25295 
6436423Sgw25295 	while (resid != 0) {
6446423Sgw25295 		int error;
6456423Sgw25295 		uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE);
6466423Sgw25295 
6476423Sgw25295 		tx = dmu_tx_create(os);
6486423Sgw25295 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
6496423Sgw25295 		error = dmu_tx_assign(tx, TXG_WAIT);
6506423Sgw25295 		if (error) {
6516423Sgw25295 			dmu_tx_abort(tx);
6526992Smaybee 			(void) dmu_free_long_range(os, ZVOL_OBJ, 0, off);
6536423Sgw25295 			return (error);
6546423Sgw25295 		}
6557872STim.Haley@Sun.COM 		dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx);
6566423Sgw25295 		dmu_tx_commit(tx);
6576423Sgw25295 		off += bytes;
6586423Sgw25295 		resid -= bytes;
6596423Sgw25295 	}
6606423Sgw25295 	txg_wait_synced(dmu_objset_pool(os), 0);
6616423Sgw25295 
6626423Sgw25295 	return (0);
6636423Sgw25295 }
6646423Sgw25295 
6656423Sgw25295 int
66610588SEric.Taylor@Sun.COM zvol_update_volsize(objset_t *os, uint64_t volsize)
6676423Sgw25295 {
6686423Sgw25295 	dmu_tx_t *tx;
6696423Sgw25295 	int error;
6706423Sgw25295 
67112527SChris.Kirby@oracle.com 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
6726423Sgw25295 
67310588SEric.Taylor@Sun.COM 	tx = dmu_tx_create(os);
6746423Sgw25295 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
6756423Sgw25295 	error = dmu_tx_assign(tx, TXG_WAIT);
6766423Sgw25295 	if (error) {
6776423Sgw25295 		dmu_tx_abort(tx);
6786423Sgw25295 		return (error);
6796423Sgw25295 	}
6806423Sgw25295 
68110588SEric.Taylor@Sun.COM 	error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
6826423Sgw25295 	    &volsize, tx);
6836423Sgw25295 	dmu_tx_commit(tx);
6846423Sgw25295 
6856423Sgw25295 	if (error == 0)
68610588SEric.Taylor@Sun.COM 		error = dmu_free_long_range(os,
6876992Smaybee 		    ZVOL_OBJ, volsize, DMU_OBJECT_END);
68810588SEric.Taylor@Sun.COM 	return (error);
68910588SEric.Taylor@Sun.COM }
69010588SEric.Taylor@Sun.COM 
69110588SEric.Taylor@Sun.COM void
69210588SEric.Taylor@Sun.COM zvol_remove_minors(const char *name)
69310588SEric.Taylor@Sun.COM {
69410588SEric.Taylor@Sun.COM 	zvol_state_t *zv;
69510588SEric.Taylor@Sun.COM 	char *namebuf;
69610588SEric.Taylor@Sun.COM 	minor_t minor;
6976423Sgw25295 
69810588SEric.Taylor@Sun.COM 	namebuf = kmem_zalloc(strlen(name) + 2, KM_SLEEP);
69910588SEric.Taylor@Sun.COM 	(void) strncpy(namebuf, name, strlen(name));
70010588SEric.Taylor@Sun.COM 	(void) strcat(namebuf, "/");
70112527SChris.Kirby@oracle.com 	mutex_enter(&zfsdev_state_lock);
70212527SChris.Kirby@oracle.com 	for (minor = 1; minor <= ZFSDEV_MAX_MINOR; minor++) {
70310588SEric.Taylor@Sun.COM 
70412527SChris.Kirby@oracle.com 		zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
70510588SEric.Taylor@Sun.COM 		if (zv == NULL)
70610588SEric.Taylor@Sun.COM 			continue;
70710588SEric.Taylor@Sun.COM 		if (strncmp(namebuf, zv->zv_name, strlen(namebuf)) == 0)
70810588SEric.Taylor@Sun.COM 			(void) zvol_remove_zv(zv);
7096423Sgw25295 	}
71010588SEric.Taylor@Sun.COM 	kmem_free(namebuf, strlen(name) + 2);
71110588SEric.Taylor@Sun.COM 
71212527SChris.Kirby@oracle.com 	mutex_exit(&zfsdev_state_lock);
7136423Sgw25295 }
7146423Sgw25295 
715789Sahrens int
7164787Sahrens zvol_set_volsize(const char *name, major_t maj, uint64_t volsize)
717789Sahrens {
71810588SEric.Taylor@Sun.COM 	zvol_state_t *zv = NULL;
71910588SEric.Taylor@Sun.COM 	objset_t *os;
720789Sahrens 	int error;
7211133Seschrock 	dmu_object_info_t doi;
7226423Sgw25295 	uint64_t old_volsize = 0ULL;
72310588SEric.Taylor@Sun.COM 	uint64_t readonly;
724789Sahrens 
72512527SChris.Kirby@oracle.com 	mutex_enter(&zfsdev_state_lock);
72610588SEric.Taylor@Sun.COM 	zv = zvol_minor_lookup(name);
72710588SEric.Taylor@Sun.COM 	if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) {
72812527SChris.Kirby@oracle.com 		mutex_exit(&zfsdev_state_lock);
72910588SEric.Taylor@Sun.COM 		return (error);
73010588SEric.Taylor@Sun.COM 	}
731789Sahrens 
73210588SEric.Taylor@Sun.COM 	if ((error = dmu_object_info(os, ZVOL_OBJ, &doi)) != 0 ||
7332676Seschrock 	    (error = zvol_check_volsize(volsize,
7347265Sahrens 	    doi.doi_data_block_size)) != 0)
7357265Sahrens 		goto out;
7361133Seschrock 
73710588SEric.Taylor@Sun.COM 	VERIFY(dsl_prop_get_integer(name, "readonly", &readonly,
73810588SEric.Taylor@Sun.COM 	    NULL) == 0);
73910588SEric.Taylor@Sun.COM 	if (readonly) {
7407265Sahrens 		error = EROFS;
7417265Sahrens 		goto out;
742789Sahrens 	}
743789Sahrens 
74410588SEric.Taylor@Sun.COM 	error = zvol_update_volsize(os, volsize);
7456423Sgw25295 	/*
7466423Sgw25295 	 * Reinitialize the dump area to the new size. If we
74710588SEric.Taylor@Sun.COM 	 * failed to resize the dump area then restore it back to
74810588SEric.Taylor@Sun.COM 	 * its original size.
7496423Sgw25295 	 */
75010588SEric.Taylor@Sun.COM 	if (zv && error == 0) {
75110588SEric.Taylor@Sun.COM 		if (zv->zv_flags & ZVOL_DUMPIFIED) {
75210588SEric.Taylor@Sun.COM 			old_volsize = zv->zv_volsize;
75310588SEric.Taylor@Sun.COM 			zv->zv_volsize = volsize;
75410588SEric.Taylor@Sun.COM 			if ((error = zvol_dumpify(zv)) != 0 ||
75510588SEric.Taylor@Sun.COM 			    (error = dumpvp_resize()) != 0) {
75610588SEric.Taylor@Sun.COM 				(void) zvol_update_volsize(os, old_volsize);
75710588SEric.Taylor@Sun.COM 				zv->zv_volsize = old_volsize;
75810588SEric.Taylor@Sun.COM 				error = zvol_dumpify(zv);
75910588SEric.Taylor@Sun.COM 			}
76010588SEric.Taylor@Sun.COM 		}
76110588SEric.Taylor@Sun.COM 		if (error == 0) {
76210588SEric.Taylor@Sun.COM 			zv->zv_volsize = volsize;
76310588SEric.Taylor@Sun.COM 			zvol_size_changed(volsize, maj, zv->zv_minor);
7646423Sgw25295 		}
765789Sahrens 	}
766789Sahrens 
7679816SGeorge.Wilson@Sun.COM 	/*
7689816SGeorge.Wilson@Sun.COM 	 * Generate a LUN expansion event.
7699816SGeorge.Wilson@Sun.COM 	 */
77010588SEric.Taylor@Sun.COM 	if (zv && error == 0) {
7719816SGeorge.Wilson@Sun.COM 		sysevent_id_t eid;
7729816SGeorge.Wilson@Sun.COM 		nvlist_t *attr;
7739816SGeorge.Wilson@Sun.COM 		char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
7749816SGeorge.Wilson@Sun.COM 
77510588SEric.Taylor@Sun.COM 		(void) snprintf(physpath, MAXPATHLEN, "%s%u", ZVOL_PSEUDO_DEV,
7769816SGeorge.Wilson@Sun.COM 		    zv->zv_minor);
7779816SGeorge.Wilson@Sun.COM 
7789816SGeorge.Wilson@Sun.COM 		VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
7799816SGeorge.Wilson@Sun.COM 		VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
7809816SGeorge.Wilson@Sun.COM 
7819816SGeorge.Wilson@Sun.COM 		(void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
7829816SGeorge.Wilson@Sun.COM 		    ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
7839816SGeorge.Wilson@Sun.COM 
7849816SGeorge.Wilson@Sun.COM 		nvlist_free(attr);
7859816SGeorge.Wilson@Sun.COM 		kmem_free(physpath, MAXPATHLEN);
7869816SGeorge.Wilson@Sun.COM 	}
7879816SGeorge.Wilson@Sun.COM 
7887265Sahrens out:
78910588SEric.Taylor@Sun.COM 	dmu_objset_rele(os, FTAG);
7907265Sahrens 
79112527SChris.Kirby@oracle.com 	mutex_exit(&zfsdev_state_lock);
792789Sahrens 
793789Sahrens 	return (error);
794789Sahrens }
795789Sahrens 
796789Sahrens /*ARGSUSED*/
797789Sahrens int
798789Sahrens zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr)
799789Sahrens {
800789Sahrens 	zvol_state_t *zv;
80110588SEric.Taylor@Sun.COM 	int err = 0;
802789Sahrens 
80312527SChris.Kirby@oracle.com 	mutex_enter(&zfsdev_state_lock);
804789Sahrens 
80512527SChris.Kirby@oracle.com 	zv = zfsdev_get_soft_state(getminor(*devp), ZSST_ZVOL);
806789Sahrens 	if (zv == NULL) {
80712527SChris.Kirby@oracle.com 		mutex_exit(&zfsdev_state_lock);
808789Sahrens 		return (ENXIO);
809789Sahrens 	}
810789Sahrens 
81110588SEric.Taylor@Sun.COM 	if (zv->zv_total_opens == 0)
81210588SEric.Taylor@Sun.COM 		err = zvol_first_open(zv);
81310588SEric.Taylor@Sun.COM 	if (err) {
81412527SChris.Kirby@oracle.com 		mutex_exit(&zfsdev_state_lock);
81510588SEric.Taylor@Sun.COM 		return (err);
81610588SEric.Taylor@Sun.COM 	}
81710588SEric.Taylor@Sun.COM 	if ((flag & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
81810588SEric.Taylor@Sun.COM 		err = EROFS;
81910588SEric.Taylor@Sun.COM 		goto out;
820789Sahrens 	}
8217405SEric.Taylor@Sun.COM 	if (zv->zv_flags & ZVOL_EXCL) {
82210588SEric.Taylor@Sun.COM 		err = EBUSY;
82310588SEric.Taylor@Sun.COM 		goto out;
8247405SEric.Taylor@Sun.COM 	}
8257405SEric.Taylor@Sun.COM 	if (flag & FEXCL) {
8267405SEric.Taylor@Sun.COM 		if (zv->zv_total_opens != 0) {
82710588SEric.Taylor@Sun.COM 			err = EBUSY;
82810588SEric.Taylor@Sun.COM 			goto out;
8297405SEric.Taylor@Sun.COM 		}
8307405SEric.Taylor@Sun.COM 		zv->zv_flags |= ZVOL_EXCL;
8317405SEric.Taylor@Sun.COM 	}
832789Sahrens 
833789Sahrens 	if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) {
834789Sahrens 		zv->zv_open_count[otyp]++;
835789Sahrens 		zv->zv_total_opens++;
836789Sahrens 	}
83712527SChris.Kirby@oracle.com 	mutex_exit(&zfsdev_state_lock);
838789Sahrens 
83910588SEric.Taylor@Sun.COM 	return (err);
84010588SEric.Taylor@Sun.COM out:
84110588SEric.Taylor@Sun.COM 	if (zv->zv_total_opens == 0)
84210588SEric.Taylor@Sun.COM 		zvol_last_close(zv);
84312527SChris.Kirby@oracle.com 	mutex_exit(&zfsdev_state_lock);
84410588SEric.Taylor@Sun.COM 	return (err);
845789Sahrens }
846789Sahrens 
847789Sahrens /*ARGSUSED*/
848789Sahrens int
849789Sahrens zvol_close(dev_t dev, int flag, int otyp, cred_t *cr)
850789Sahrens {
851789Sahrens 	minor_t minor = getminor(dev);
852789Sahrens 	zvol_state_t *zv;
85310588SEric.Taylor@Sun.COM 	int error = 0;
854789Sahrens 
85512527SChris.Kirby@oracle.com 	mutex_enter(&zfsdev_state_lock);
856789Sahrens 
85712527SChris.Kirby@oracle.com 	zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
858789Sahrens 	if (zv == NULL) {
85912527SChris.Kirby@oracle.com 		mutex_exit(&zfsdev_state_lock);
860789Sahrens 		return (ENXIO);
861789Sahrens 	}
862789Sahrens 
8637405SEric.Taylor@Sun.COM 	if (zv->zv_flags & ZVOL_EXCL) {
8647405SEric.Taylor@Sun.COM 		ASSERT(zv->zv_total_opens == 1);
8657405SEric.Taylor@Sun.COM 		zv->zv_flags &= ~ZVOL_EXCL;
866789Sahrens 	}
867789Sahrens 
868789Sahrens 	/*
869789Sahrens 	 * If the open count is zero, this is a spurious close.
870789Sahrens 	 * That indicates a bug in the kernel / DDI framework.
871789Sahrens 	 */
872789Sahrens 	ASSERT(zv->zv_open_count[otyp] != 0);
873789Sahrens 	ASSERT(zv->zv_total_opens != 0);
874789Sahrens 
875789Sahrens 	/*
876789Sahrens 	 * You may get multiple opens, but only one close.
877789Sahrens 	 */
878789Sahrens 	zv->zv_open_count[otyp]--;
879789Sahrens 	zv->zv_total_opens--;
880789Sahrens 
88110588SEric.Taylor@Sun.COM 	if (zv->zv_total_opens == 0)
88210588SEric.Taylor@Sun.COM 		zvol_last_close(zv);
883789Sahrens 
88412527SChris.Kirby@oracle.com 	mutex_exit(&zfsdev_state_lock);
88510588SEric.Taylor@Sun.COM 	return (error);
886789Sahrens }
887789Sahrens 
8883638Sbillm static void
88910922SJeff.Bonwick@Sun.COM zvol_get_done(zgd_t *zgd, int error)
8903063Sperrin {
89110922SJeff.Bonwick@Sun.COM 	if (zgd->zgd_db)
89210922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(zgd->zgd_db, zgd);
8933063Sperrin 
89410922SJeff.Bonwick@Sun.COM 	zfs_range_unlock(zgd->zgd_rl);
89510922SJeff.Bonwick@Sun.COM 
89610922SJeff.Bonwick@Sun.COM 	if (error == 0 && zgd->zgd_bp)
89710922SJeff.Bonwick@Sun.COM 		zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
89810922SJeff.Bonwick@Sun.COM 
8993063Sperrin 	kmem_free(zgd, sizeof (zgd_t));
9003063Sperrin }
9013063Sperrin 
9023063Sperrin /*
9033063Sperrin  * Get data to generate a TX_WRITE intent log record.
9043063Sperrin  */
9053638Sbillm static int
9063063Sperrin zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
9073063Sperrin {
9083063Sperrin 	zvol_state_t *zv = arg;
9093063Sperrin 	objset_t *os = zv->zv_objset;
91010922SJeff.Bonwick@Sun.COM 	uint64_t object = ZVOL_OBJ;
91110922SJeff.Bonwick@Sun.COM 	uint64_t offset = lr->lr_offset;
91210922SJeff.Bonwick@Sun.COM 	uint64_t size = lr->lr_length;	/* length of user data */
91310922SJeff.Bonwick@Sun.COM 	blkptr_t *bp = &lr->lr_blkptr;
9143063Sperrin 	dmu_buf_t *db;
9153063Sperrin 	zgd_t *zgd;
9163063Sperrin 	int error;
9173063Sperrin 
91810922SJeff.Bonwick@Sun.COM 	ASSERT(zio != NULL);
91910922SJeff.Bonwick@Sun.COM 	ASSERT(size != 0);
92010922SJeff.Bonwick@Sun.COM 
92110922SJeff.Bonwick@Sun.COM 	zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
92210922SJeff.Bonwick@Sun.COM 	zgd->zgd_zilog = zv->zv_zilog;
92310922SJeff.Bonwick@Sun.COM 	zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
9243638Sbillm 
9253755Sperrin 	/*
9263755Sperrin 	 * Write records come in two flavors: immediate and indirect.
9273755Sperrin 	 * For small writes it's cheaper to store the data with the
9283755Sperrin 	 * log record (immediate); for large writes it's cheaper to
9293755Sperrin 	 * sync the data and get a pointer to it (indirect) so that
9303755Sperrin 	 * we don't have to write the data twice.
9313755Sperrin 	 */
93210922SJeff.Bonwick@Sun.COM 	if (buf != NULL) {	/* immediate write */
93310922SJeff.Bonwick@Sun.COM 		error = dmu_read(os, object, offset, size, buf,
93410922SJeff.Bonwick@Sun.COM 		    DMU_READ_NO_PREFETCH);
93510922SJeff.Bonwick@Sun.COM 	} else {
93610922SJeff.Bonwick@Sun.COM 		size = zv->zv_volblocksize;
93710922SJeff.Bonwick@Sun.COM 		offset = P2ALIGN(offset, size);
93812285SJeff.Bonwick@Sun.COM 		error = dmu_buf_hold(os, object, offset, zgd, &db,
93912285SJeff.Bonwick@Sun.COM 		    DMU_READ_NO_PREFETCH);
94010922SJeff.Bonwick@Sun.COM 		if (error == 0) {
94110922SJeff.Bonwick@Sun.COM 			zgd->zgd_db = db;
94210922SJeff.Bonwick@Sun.COM 			zgd->zgd_bp = bp;
9433063Sperrin 
94410922SJeff.Bonwick@Sun.COM 			ASSERT(db->db_offset == offset);
94510922SJeff.Bonwick@Sun.COM 			ASSERT(db->db_size == size);
9463755Sperrin 
94710922SJeff.Bonwick@Sun.COM 			error = dmu_sync(zio, lr->lr_common.lrc_txg,
94810922SJeff.Bonwick@Sun.COM 			    zvol_get_done, zgd);
94910800SNeil.Perrin@Sun.COM 
95010922SJeff.Bonwick@Sun.COM 			if (error == 0)
95110922SJeff.Bonwick@Sun.COM 				return (0);
95210922SJeff.Bonwick@Sun.COM 		}
95310800SNeil.Perrin@Sun.COM 	}
95410800SNeil.Perrin@Sun.COM 
95510922SJeff.Bonwick@Sun.COM 	zvol_get_done(zgd, error);
95610922SJeff.Bonwick@Sun.COM 
9573063Sperrin 	return (error);
9583063Sperrin }
9593063Sperrin 
9601861Sperrin /*
9611861Sperrin  * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
9621141Sperrin  *
9631141Sperrin  * We store data in the log buffers if it's small enough.
9643063Sperrin  * Otherwise we will later flush the data out via dmu_sync().
9651141Sperrin  */
9663063Sperrin ssize_t zvol_immediate_write_sz = 32768;
9671141Sperrin 
9683638Sbillm static void
9699401SNeil.Perrin@Sun.COM zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid,
9709401SNeil.Perrin@Sun.COM     boolean_t sync)
9711141Sperrin {
9723638Sbillm 	uint32_t blocksize = zv->zv_volblocksize;
9738227SNeil.Perrin@Sun.COM 	zilog_t *zilog = zv->zv_zilog;
9749401SNeil.Perrin@Sun.COM 	boolean_t slogging;
97510310SNeil.Perrin@Sun.COM 	ssize_t immediate_write_sz;
9769401SNeil.Perrin@Sun.COM 
97710922SJeff.Bonwick@Sun.COM 	if (zil_replaying(zilog, tx))
9788227SNeil.Perrin@Sun.COM 		return;
9798227SNeil.Perrin@Sun.COM 
98010310SNeil.Perrin@Sun.COM 	immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
98110310SNeil.Perrin@Sun.COM 	    ? 0 : zvol_immediate_write_sz;
98210310SNeil.Perrin@Sun.COM 
98310310SNeil.Perrin@Sun.COM 	slogging = spa_has_slogs(zilog->zl_spa) &&
98410310SNeil.Perrin@Sun.COM 	    (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
9859401SNeil.Perrin@Sun.COM 
9869401SNeil.Perrin@Sun.COM 	while (resid) {
9879401SNeil.Perrin@Sun.COM 		itx_t *itx;
9889401SNeil.Perrin@Sun.COM 		lr_write_t *lr;
9899401SNeil.Perrin@Sun.COM 		ssize_t len;
9909401SNeil.Perrin@Sun.COM 		itx_wr_state_t write_state;
9913638Sbillm 
9929401SNeil.Perrin@Sun.COM 		/*
9939401SNeil.Perrin@Sun.COM 		 * Unlike zfs_log_write() we can be called with
9949401SNeil.Perrin@Sun.COM 		 * upto DMU_MAX_ACCESS/2 (5MB) writes.
9959401SNeil.Perrin@Sun.COM 		 */
99610310SNeil.Perrin@Sun.COM 		if (blocksize > immediate_write_sz && !slogging &&
9979401SNeil.Perrin@Sun.COM 		    resid >= blocksize && off % blocksize == 0) {
9989401SNeil.Perrin@Sun.COM 			write_state = WR_INDIRECT; /* uses dmu_sync */
9999401SNeil.Perrin@Sun.COM 			len = blocksize;
10009401SNeil.Perrin@Sun.COM 		} else if (sync) {
10019401SNeil.Perrin@Sun.COM 			write_state = WR_COPIED;
10029401SNeil.Perrin@Sun.COM 			len = MIN(ZIL_MAX_LOG_DATA, resid);
10039401SNeil.Perrin@Sun.COM 		} else {
10049401SNeil.Perrin@Sun.COM 			write_state = WR_NEED_COPY;
10059401SNeil.Perrin@Sun.COM 			len = MIN(ZIL_MAX_LOG_DATA, resid);
10069401SNeil.Perrin@Sun.COM 		}
10079401SNeil.Perrin@Sun.COM 
10089401SNeil.Perrin@Sun.COM 		itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
10099401SNeil.Perrin@Sun.COM 		    (write_state == WR_COPIED ? len : 0));
10103638Sbillm 		lr = (lr_write_t *)&itx->itx_lr;
10119401SNeil.Perrin@Sun.COM 		if (write_state == WR_COPIED && dmu_read(zv->zv_objset,
10129512SNeil.Perrin@Sun.COM 		    ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
101310922SJeff.Bonwick@Sun.COM 			zil_itx_destroy(itx);
10149401SNeil.Perrin@Sun.COM 			itx = zil_itx_create(TX_WRITE, sizeof (*lr));
10159401SNeil.Perrin@Sun.COM 			lr = (lr_write_t *)&itx->itx_lr;
10169401SNeil.Perrin@Sun.COM 			write_state = WR_NEED_COPY;
10179401SNeil.Perrin@Sun.COM 		}
10189401SNeil.Perrin@Sun.COM 
10199401SNeil.Perrin@Sun.COM 		itx->itx_wr_state = write_state;
10209401SNeil.Perrin@Sun.COM 		if (write_state == WR_NEED_COPY)
10219401SNeil.Perrin@Sun.COM 			itx->itx_sod += len;
10223638Sbillm 		lr->lr_foid = ZVOL_OBJ;
10233638Sbillm 		lr->lr_offset = off;
10249401SNeil.Perrin@Sun.COM 		lr->lr_length = len;
102510922SJeff.Bonwick@Sun.COM 		lr->lr_blkoff = 0;
10263638Sbillm 		BP_ZERO(&lr->lr_blkptr);
10273638Sbillm 
10289401SNeil.Perrin@Sun.COM 		itx->itx_private = zv;
10299401SNeil.Perrin@Sun.COM 		itx->itx_sync = sync;
10309401SNeil.Perrin@Sun.COM 
1031*12699SNeil.Perrin@Sun.COM 		zil_itx_assign(zilog, itx, tx);
10329401SNeil.Perrin@Sun.COM 
10339401SNeil.Perrin@Sun.COM 		off += len;
10349401SNeil.Perrin@Sun.COM 		resid -= len;
10351141Sperrin 	}
10361141Sperrin }
10371141Sperrin 
10387837SMatthew.Ahrens@Sun.COM static int
10397837SMatthew.Ahrens@Sun.COM zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t size,
10407837SMatthew.Ahrens@Sun.COM     boolean_t doread, boolean_t isdump)
10416423Sgw25295 {
10426423Sgw25295 	vdev_disk_t *dvd;
10436423Sgw25295 	int c;
10446423Sgw25295 	int numerrors = 0;
10456423Sgw25295 
10466423Sgw25295 	for (c = 0; c < vd->vdev_children; c++) {
10479790SLin.Ling@Sun.COM 		ASSERT(vd->vdev_ops == &vdev_mirror_ops ||
10489790SLin.Ling@Sun.COM 		    vd->vdev_ops == &vdev_replacing_ops ||
10499790SLin.Ling@Sun.COM 		    vd->vdev_ops == &vdev_spare_ops);
10507837SMatthew.Ahrens@Sun.COM 		int err = zvol_dumpio_vdev(vd->vdev_child[c],
10517837SMatthew.Ahrens@Sun.COM 		    addr, offset, size, doread, isdump);
10527837SMatthew.Ahrens@Sun.COM 		if (err != 0) {
10536423Sgw25295 			numerrors++;
10547837SMatthew.Ahrens@Sun.COM 		} else if (doread) {
10556423Sgw25295 			break;
10566423Sgw25295 		}
10576423Sgw25295 	}
10586423Sgw25295 
10596423Sgw25295 	if (!vd->vdev_ops->vdev_op_leaf)
10606423Sgw25295 		return (numerrors < vd->vdev_children ? 0 : EIO);
10616423Sgw25295 
10627903SEric.Taylor@Sun.COM 	if (doread && !vdev_readable(vd))
10637903SEric.Taylor@Sun.COM 		return (EIO);
10647903SEric.Taylor@Sun.COM 	else if (!doread && !vdev_writeable(vd))
10656423Sgw25295 		return (EIO);
10666423Sgw25295 
10676423Sgw25295 	dvd = vd->vdev_tsd;
10686423Sgw25295 	ASSERT3P(dvd, !=, NULL);
10696423Sgw25295 	offset += VDEV_LABEL_START_SIZE;
10706423Sgw25295 
10716423Sgw25295 	if (ddi_in_panic() || isdump) {
10727837SMatthew.Ahrens@Sun.COM 		ASSERT(!doread);
10737837SMatthew.Ahrens@Sun.COM 		if (doread)
10746423Sgw25295 			return (EIO);
10756423Sgw25295 		return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset),
10766423Sgw25295 		    lbtodb(size)));
10776423Sgw25295 	} else {
10786423Sgw25295 		return (vdev_disk_physio(dvd->vd_lh, addr, size, offset,
10797837SMatthew.Ahrens@Sun.COM 		    doread ? B_READ : B_WRITE));
10806423Sgw25295 	}
10816423Sgw25295 }
10826423Sgw25295 
10837837SMatthew.Ahrens@Sun.COM static int
10847837SMatthew.Ahrens@Sun.COM zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size,
10857837SMatthew.Ahrens@Sun.COM     boolean_t doread, boolean_t isdump)
10866423Sgw25295 {
10876423Sgw25295 	vdev_t *vd;
10886423Sgw25295 	int error;
10897837SMatthew.Ahrens@Sun.COM 	zvol_extent_t *ze;
10906423Sgw25295 	spa_t *spa = dmu_objset_spa(zv->zv_objset);
10916423Sgw25295 
10927837SMatthew.Ahrens@Sun.COM 	/* Must be sector aligned, and not stradle a block boundary. */
10937837SMatthew.Ahrens@Sun.COM 	if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) ||
10947837SMatthew.Ahrens@Sun.COM 	    P2BOUNDARY(offset, size, zv->zv_volblocksize)) {
10957837SMatthew.Ahrens@Sun.COM 		return (EINVAL);
10967837SMatthew.Ahrens@Sun.COM 	}
10976423Sgw25295 	ASSERT(size <= zv->zv_volblocksize);
10986423Sgw25295 
10997837SMatthew.Ahrens@Sun.COM 	/* Locate the extent this belongs to */
11007837SMatthew.Ahrens@Sun.COM 	ze = list_head(&zv->zv_extents);
11017837SMatthew.Ahrens@Sun.COM 	while (offset >= ze->ze_nblks * zv->zv_volblocksize) {
11027837SMatthew.Ahrens@Sun.COM 		offset -= ze->ze_nblks * zv->zv_volblocksize;
11037837SMatthew.Ahrens@Sun.COM 		ze = list_next(&zv->zv_extents, ze);
11047837SMatthew.Ahrens@Sun.COM 	}
110511806SGeorge.Wilson@Sun.COM 
110611806SGeorge.Wilson@Sun.COM 	if (!ddi_in_panic())
110711806SGeorge.Wilson@Sun.COM 		spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
110811806SGeorge.Wilson@Sun.COM 
11097837SMatthew.Ahrens@Sun.COM 	vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva));
11107837SMatthew.Ahrens@Sun.COM 	offset += DVA_GET_OFFSET(&ze->ze_dva);
11117837SMatthew.Ahrens@Sun.COM 	error = zvol_dumpio_vdev(vd, addr, offset, size, doread, isdump);
111211806SGeorge.Wilson@Sun.COM 
111311806SGeorge.Wilson@Sun.COM 	if (!ddi_in_panic())
111411806SGeorge.Wilson@Sun.COM 		spa_config_exit(spa, SCL_STATE, FTAG);
111511806SGeorge.Wilson@Sun.COM 
11166423Sgw25295 	return (error);
11176423Sgw25295 }
11186423Sgw25295 
11196423Sgw25295 int
1120789Sahrens zvol_strategy(buf_t *bp)
1121789Sahrens {
112212527SChris.Kirby@oracle.com 	zfs_soft_state_t *zs = NULL;
112312527SChris.Kirby@oracle.com 	zvol_state_t *zv;
1124789Sahrens 	uint64_t off, volsize;
11257837SMatthew.Ahrens@Sun.COM 	size_t resid;
1126789Sahrens 	char *addr;
11271141Sperrin 	objset_t *os;
11283755Sperrin 	rl_t *rl;
1129789Sahrens 	int error = 0;
11307837SMatthew.Ahrens@Sun.COM 	boolean_t doread = bp->b_flags & B_READ;
113112095SChris.Kirby@sun.com 	boolean_t is_dump;
11329401SNeil.Perrin@Sun.COM 	boolean_t sync;
1133789Sahrens 
113412527SChris.Kirby@oracle.com 	if (getminor(bp->b_edev) == 0) {
113512527SChris.Kirby@oracle.com 		error = EINVAL;
113612527SChris.Kirby@oracle.com 	} else {
113712527SChris.Kirby@oracle.com 		zs = ddi_get_soft_state(zfsdev_state, getminor(bp->b_edev));
113812527SChris.Kirby@oracle.com 		if (zs == NULL)
113912527SChris.Kirby@oracle.com 			error = ENXIO;
114012527SChris.Kirby@oracle.com 		else if (zs->zss_type != ZSST_ZVOL)
114112527SChris.Kirby@oracle.com 			error = EINVAL;
114212527SChris.Kirby@oracle.com 	}
114312527SChris.Kirby@oracle.com 
114412527SChris.Kirby@oracle.com 	if (error) {
114512527SChris.Kirby@oracle.com 		bioerror(bp, error);
1146789Sahrens 		biodone(bp);
1147789Sahrens 		return (0);
1148789Sahrens 	}
1149789Sahrens 
115012527SChris.Kirby@oracle.com 	zv = zs->zss_data;
1151789Sahrens 
115210588SEric.Taylor@Sun.COM 	if (!(bp->b_flags & B_READ) && (zv->zv_flags & ZVOL_RDONLY)) {
1153789Sahrens 		bioerror(bp, EROFS);
1154789Sahrens 		biodone(bp);
1155789Sahrens 		return (0);
1156789Sahrens 	}
1157789Sahrens 
1158789Sahrens 	off = ldbtob(bp->b_blkno);
1159789Sahrens 	volsize = zv->zv_volsize;
1160789Sahrens 
11611141Sperrin 	os = zv->zv_objset;
11621141Sperrin 	ASSERT(os != NULL);
1163789Sahrens 
1164789Sahrens 	bp_mapin(bp);
1165789Sahrens 	addr = bp->b_un.b_addr;
1166789Sahrens 	resid = bp->b_bcount;
1167789Sahrens 
11687837SMatthew.Ahrens@Sun.COM 	if (resid > 0 && (off < 0 || off >= volsize)) {
11697837SMatthew.Ahrens@Sun.COM 		bioerror(bp, EIO);
11707837SMatthew.Ahrens@Sun.COM 		biodone(bp);
11717837SMatthew.Ahrens@Sun.COM 		return (0);
11727837SMatthew.Ahrens@Sun.COM 	}
11737013Sgw25295 
117412095SChris.Kirby@sun.com 	is_dump = zv->zv_flags & ZVOL_DUMPIFIED;
117512294SMark.Musante@Sun.COM 	sync = ((!(bp->b_flags & B_ASYNC) &&
117612294SMark.Musante@Sun.COM 	    !(zv->zv_flags & ZVOL_WCE)) ||
117712294SMark.Musante@Sun.COM 	    (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)) &&
117812294SMark.Musante@Sun.COM 	    !doread && !is_dump;
11799401SNeil.Perrin@Sun.COM 
11801861Sperrin 	/*
11811861Sperrin 	 * There must be no buffer changes when doing a dmu_sync() because
11821861Sperrin 	 * we can't change the data whilst calculating the checksum.
11831861Sperrin 	 */
11843755Sperrin 	rl = zfs_range_lock(&zv->zv_znode, off, resid,
11857837SMatthew.Ahrens@Sun.COM 	    doread ? RL_READER : RL_WRITER);
11866423Sgw25295 
1187789Sahrens 	while (resid != 0 && off < volsize) {
11887837SMatthew.Ahrens@Sun.COM 		size_t size = MIN(resid, zvol_maxphys);
11896423Sgw25295 		if (is_dump) {
11906423Sgw25295 			size = MIN(size, P2END(off, zv->zv_volblocksize) - off);
11917837SMatthew.Ahrens@Sun.COM 			error = zvol_dumpio(zv, addr, off, size,
11927837SMatthew.Ahrens@Sun.COM 			    doread, B_FALSE);
11937837SMatthew.Ahrens@Sun.COM 		} else if (doread) {
11949512SNeil.Perrin@Sun.COM 			error = dmu_read(os, ZVOL_OBJ, off, size, addr,
11959512SNeil.Perrin@Sun.COM 			    DMU_READ_PREFETCH);
1196789Sahrens 		} else {
11971141Sperrin 			dmu_tx_t *tx = dmu_tx_create(os);
1198789Sahrens 			dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
1199789Sahrens 			error = dmu_tx_assign(tx, TXG_WAIT);
1200789Sahrens 			if (error) {
1201789Sahrens 				dmu_tx_abort(tx);
1202789Sahrens 			} else {
12031141Sperrin 				dmu_write(os, ZVOL_OBJ, off, size, addr, tx);
12049401SNeil.Perrin@Sun.COM 				zvol_log_write(zv, tx, off, size, sync);
1205789Sahrens 				dmu_tx_commit(tx);
1206789Sahrens 			}
1207789Sahrens 		}
12087294Sperrin 		if (error) {
12097294Sperrin 			/* convert checksum errors into IO errors */
12107294Sperrin 			if (error == ECKSUM)
12117294Sperrin 				error = EIO;
1212789Sahrens 			break;
12137294Sperrin 		}
1214789Sahrens 		off += size;
1215789Sahrens 		addr += size;
1216789Sahrens 		resid -= size;
1217789Sahrens 	}
12183755Sperrin 	zfs_range_unlock(rl);
1219789Sahrens 
1220789Sahrens 	if ((bp->b_resid = resid) == bp->b_bcount)
1221789Sahrens 		bioerror(bp, off > volsize ? EINVAL : error);
1222789Sahrens 
12239401SNeil.Perrin@Sun.COM 	if (sync)
1224*12699SNeil.Perrin@Sun.COM 		zil_commit(zv->zv_zilog, ZVOL_OBJ);
12253638Sbillm 	biodone(bp);
12261141Sperrin 
1227789Sahrens 	return (0);
1228789Sahrens }
1229789Sahrens 
12303063Sperrin /*
12313063Sperrin  * Set the buffer count to the zvol maximum transfer.
12323063Sperrin  * Using our own routine instead of the default minphys()
12333063Sperrin  * means that for larger writes we write bigger buffers on X86
12343063Sperrin  * (128K instead of 56K) and flush the disk write cache less often
12353063Sperrin  * (every zvol_maxphys - currently 1MB) instead of minphys (currently
12363063Sperrin  * 56K on X86 and 128K on sparc).
12373063Sperrin  */
12383063Sperrin void
12393063Sperrin zvol_minphys(struct buf *bp)
12403063Sperrin {
12413063Sperrin 	if (bp->b_bcount > zvol_maxphys)
12423063Sperrin 		bp->b_bcount = zvol_maxphys;
12433063Sperrin }
12443063Sperrin 
12456423Sgw25295 int
12466423Sgw25295 zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks)
12476423Sgw25295 {
12486423Sgw25295 	minor_t minor = getminor(dev);
12496423Sgw25295 	zvol_state_t *zv;
12506423Sgw25295 	int error = 0;
12516423Sgw25295 	uint64_t size;
12526423Sgw25295 	uint64_t boff;
12536423Sgw25295 	uint64_t resid;
12546423Sgw25295 
125512527SChris.Kirby@oracle.com 	zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
12566423Sgw25295 	if (zv == NULL)
12576423Sgw25295 		return (ENXIO);
12586423Sgw25295 
12596423Sgw25295 	boff = ldbtob(blkno);
12606423Sgw25295 	resid = ldbtob(nblocks);
12617837SMatthew.Ahrens@Sun.COM 
12627837SMatthew.Ahrens@Sun.COM 	VERIFY3U(boff + resid, <=, zv->zv_volsize);
12637837SMatthew.Ahrens@Sun.COM 
12646423Sgw25295 	while (resid) {
12656423Sgw25295 		size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff);
12667837SMatthew.Ahrens@Sun.COM 		error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE);
12676423Sgw25295 		if (error)
12686423Sgw25295 			break;
12696423Sgw25295 		boff += size;
12706423Sgw25295 		addr += size;
12716423Sgw25295 		resid -= size;
12726423Sgw25295 	}
12736423Sgw25295 
12746423Sgw25295 	return (error);
12756423Sgw25295 }
12766423Sgw25295 
1277789Sahrens /*ARGSUSED*/
1278789Sahrens int
12793638Sbillm zvol_read(dev_t dev, uio_t *uio, cred_t *cr)
1280789Sahrens {
12814107Sgw25295 	minor_t minor = getminor(dev);
12824107Sgw25295 	zvol_state_t *zv;
12837013Sgw25295 	uint64_t volsize;
12843755Sperrin 	rl_t *rl;
12853638Sbillm 	int error = 0;
12863638Sbillm 
128712527SChris.Kirby@oracle.com 	zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
12884107Sgw25295 	if (zv == NULL)
12894107Sgw25295 		return (ENXIO);
12904107Sgw25295 
12917013Sgw25295 	volsize = zv->zv_volsize;
12927013Sgw25295 	if (uio->uio_resid > 0 &&
12937013Sgw25295 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
12947013Sgw25295 		return (EIO);
12957013Sgw25295 
12967837SMatthew.Ahrens@Sun.COM 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
12977837SMatthew.Ahrens@Sun.COM 		error = physio(zvol_strategy, NULL, dev, B_READ,
12987837SMatthew.Ahrens@Sun.COM 		    zvol_minphys, uio);
12997837SMatthew.Ahrens@Sun.COM 		return (error);
13007837SMatthew.Ahrens@Sun.COM 	}
13017837SMatthew.Ahrens@Sun.COM 
13023755Sperrin 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
13033755Sperrin 	    RL_READER);
13047013Sgw25295 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
13053638Sbillm 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
13063638Sbillm 
13077013Sgw25295 		/* don't read past the end */
13087013Sgw25295 		if (bytes > volsize - uio->uio_loffset)
13097013Sgw25295 			bytes = volsize - uio->uio_loffset;
13107013Sgw25295 
13113638Sbillm 		error =  dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes);
13127294Sperrin 		if (error) {
13137294Sperrin 			/* convert checksum errors into IO errors */
13147294Sperrin 			if (error == ECKSUM)
13157294Sperrin 				error = EIO;
13163638Sbillm 			break;
13177294Sperrin 		}
13183638Sbillm 	}
13193755Sperrin 	zfs_range_unlock(rl);
13203638Sbillm 	return (error);
1321789Sahrens }
1322789Sahrens 
1323789Sahrens /*ARGSUSED*/
1324789Sahrens int
13253638Sbillm zvol_write(dev_t dev, uio_t *uio, cred_t *cr)
1326789Sahrens {
13274107Sgw25295 	minor_t minor = getminor(dev);
13284107Sgw25295 	zvol_state_t *zv;
13297013Sgw25295 	uint64_t volsize;
13303755Sperrin 	rl_t *rl;
13313638Sbillm 	int error = 0;
13329401SNeil.Perrin@Sun.COM 	boolean_t sync;
13333638Sbillm 
133412527SChris.Kirby@oracle.com 	zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
13354107Sgw25295 	if (zv == NULL)
13364107Sgw25295 		return (ENXIO);
13374107Sgw25295 
13387013Sgw25295 	volsize = zv->zv_volsize;
13397013Sgw25295 	if (uio->uio_resid > 0 &&
13407013Sgw25295 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
13417013Sgw25295 		return (EIO);
13427013Sgw25295 
13436423Sgw25295 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
13446423Sgw25295 		error = physio(zvol_strategy, NULL, dev, B_WRITE,
13456423Sgw25295 		    zvol_minphys, uio);
13466423Sgw25295 		return (error);
13476423Sgw25295 	}
13486423Sgw25295 
134912294SMark.Musante@Sun.COM 	sync = !(zv->zv_flags & ZVOL_WCE) ||
135012294SMark.Musante@Sun.COM 	    (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS);
13519401SNeil.Perrin@Sun.COM 
13523755Sperrin 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
13533755Sperrin 	    RL_WRITER);
13547013Sgw25295 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
13553638Sbillm 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
13563638Sbillm 		uint64_t off = uio->uio_loffset;
13577013Sgw25295 		dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
1358789Sahrens 
13597013Sgw25295 		if (bytes > volsize - off)	/* don't write past the end */
13607013Sgw25295 			bytes = volsize - off;
13617013Sgw25295 
13623638Sbillm 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
13633638Sbillm 		error = dmu_tx_assign(tx, TXG_WAIT);
13643638Sbillm 		if (error) {
13653638Sbillm 			dmu_tx_abort(tx);
13663638Sbillm 			break;
13673638Sbillm 		}
136812123STim.Haley@Sun.COM 		error = dmu_write_uio_dbuf(zv->zv_dbuf, uio, bytes, tx);
13693638Sbillm 		if (error == 0)
13709401SNeil.Perrin@Sun.COM 			zvol_log_write(zv, tx, off, bytes, sync);
13713638Sbillm 		dmu_tx_commit(tx);
13723638Sbillm 
13733638Sbillm 		if (error)
13743638Sbillm 			break;
13753638Sbillm 	}
13763755Sperrin 	zfs_range_unlock(rl);
13779401SNeil.Perrin@Sun.COM 	if (sync)
1378*12699SNeil.Perrin@Sun.COM 		zil_commit(zv->zv_zilog, ZVOL_OBJ);
13793638Sbillm 	return (error);
1380789Sahrens }
1381789Sahrens 
13827405SEric.Taylor@Sun.COM int
13837405SEric.Taylor@Sun.COM zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs)
13847405SEric.Taylor@Sun.COM {
13857405SEric.Taylor@Sun.COM 	struct uuid uuid = EFI_RESERVED;
13867405SEric.Taylor@Sun.COM 	efi_gpe_t gpe = { 0 };
13877405SEric.Taylor@Sun.COM 	uint32_t crc;
13887405SEric.Taylor@Sun.COM 	dk_efi_t efi;
13897405SEric.Taylor@Sun.COM 	int length;
13907405SEric.Taylor@Sun.COM 	char *ptr;
13917405SEric.Taylor@Sun.COM 
13927405SEric.Taylor@Sun.COM 	if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag))
13937405SEric.Taylor@Sun.COM 		return (EFAULT);
13947405SEric.Taylor@Sun.COM 	ptr = (char *)(uintptr_t)efi.dki_data_64;
13957405SEric.Taylor@Sun.COM 	length = efi.dki_length;
13967405SEric.Taylor@Sun.COM 	/*
13977405SEric.Taylor@Sun.COM 	 * Some clients may attempt to request a PMBR for the
13987405SEric.Taylor@Sun.COM 	 * zvol.  Currently this interface will return EINVAL to
13997405SEric.Taylor@Sun.COM 	 * such requests.  These requests could be supported by
14007405SEric.Taylor@Sun.COM 	 * adding a check for lba == 0 and consing up an appropriate
14017405SEric.Taylor@Sun.COM 	 * PMBR.
14027405SEric.Taylor@Sun.COM 	 */
14037405SEric.Taylor@Sun.COM 	if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0)
14047405SEric.Taylor@Sun.COM 		return (EINVAL);
14057405SEric.Taylor@Sun.COM 
14067405SEric.Taylor@Sun.COM 	gpe.efi_gpe_StartingLBA = LE_64(34ULL);
14077405SEric.Taylor@Sun.COM 	gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1);
14087405SEric.Taylor@Sun.COM 	UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
14097405SEric.Taylor@Sun.COM 
14107405SEric.Taylor@Sun.COM 	if (efi.dki_lba == 1) {
14117405SEric.Taylor@Sun.COM 		efi_gpt_t gpt = { 0 };
14127405SEric.Taylor@Sun.COM 
14137405SEric.Taylor@Sun.COM 		gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
14147405SEric.Taylor@Sun.COM 		gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
14157405SEric.Taylor@Sun.COM 		gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt));
14167405SEric.Taylor@Sun.COM 		gpt.efi_gpt_MyLBA = LE_64(1ULL);
14177405SEric.Taylor@Sun.COM 		gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL);
14187405SEric.Taylor@Sun.COM 		gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1);
14197405SEric.Taylor@Sun.COM 		gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL);
14207405SEric.Taylor@Sun.COM 		gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
14217405SEric.Taylor@Sun.COM 		gpt.efi_gpt_SizeOfPartitionEntry =
14227405SEric.Taylor@Sun.COM 		    LE_32(sizeof (efi_gpe_t));
14237405SEric.Taylor@Sun.COM 		CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
14247405SEric.Taylor@Sun.COM 		gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
14257405SEric.Taylor@Sun.COM 		CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table);
14267405SEric.Taylor@Sun.COM 		gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
14277405SEric.Taylor@Sun.COM 		if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length),
14287405SEric.Taylor@Sun.COM 		    flag))
14297405SEric.Taylor@Sun.COM 			return (EFAULT);
14307405SEric.Taylor@Sun.COM 		ptr += sizeof (gpt);
14317405SEric.Taylor@Sun.COM 		length -= sizeof (gpt);
14327405SEric.Taylor@Sun.COM 	}
14337405SEric.Taylor@Sun.COM 	if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe),
14347405SEric.Taylor@Sun.COM 	    length), flag))
14357405SEric.Taylor@Sun.COM 		return (EFAULT);
14367405SEric.Taylor@Sun.COM 	return (0);
14377405SEric.Taylor@Sun.COM }
14387405SEric.Taylor@Sun.COM 
1439789Sahrens /*
144012314SJames.Moore@Sun.COM  * BEGIN entry points to allow external callers access to the volume.
144112314SJames.Moore@Sun.COM  */
144212314SJames.Moore@Sun.COM /*
144312314SJames.Moore@Sun.COM  * Return the volume parameters needed for access from an external caller.
144412314SJames.Moore@Sun.COM  * These values are invariant as long as the volume is held open.
144512314SJames.Moore@Sun.COM  */
144612314SJames.Moore@Sun.COM int
144712314SJames.Moore@Sun.COM zvol_get_volume_params(minor_t minor, uint64_t *blksize,
144812314SJames.Moore@Sun.COM     uint64_t *max_xfer_len, void **minor_hdl, void **objset_hdl, void **zil_hdl,
144912314SJames.Moore@Sun.COM     void **rl_hdl, void **bonus_hdl)
145012314SJames.Moore@Sun.COM {
145112314SJames.Moore@Sun.COM 	zvol_state_t *zv;
145212314SJames.Moore@Sun.COM 
145312527SChris.Kirby@oracle.com 	zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
145412527SChris.Kirby@oracle.com 	if (zv == NULL)
145512314SJames.Moore@Sun.COM 		return (ENXIO);
145612314SJames.Moore@Sun.COM 	if (zv->zv_flags & ZVOL_DUMPIFIED)
145712314SJames.Moore@Sun.COM 		return (ENXIO);
145812314SJames.Moore@Sun.COM 
145912314SJames.Moore@Sun.COM 	ASSERT(blksize && max_xfer_len && minor_hdl &&
146012314SJames.Moore@Sun.COM 	    objset_hdl && zil_hdl && rl_hdl && bonus_hdl);
146112314SJames.Moore@Sun.COM 
146212314SJames.Moore@Sun.COM 	*blksize = zv->zv_volblocksize;
146312314SJames.Moore@Sun.COM 	*max_xfer_len = (uint64_t)zvol_maxphys;
146412314SJames.Moore@Sun.COM 	*minor_hdl = zv;
146512314SJames.Moore@Sun.COM 	*objset_hdl = zv->zv_objset;
146612314SJames.Moore@Sun.COM 	*zil_hdl = zv->zv_zilog;
146712314SJames.Moore@Sun.COM 	*rl_hdl = &zv->zv_znode;
146812314SJames.Moore@Sun.COM 	*bonus_hdl = zv->zv_dbuf;
146912314SJames.Moore@Sun.COM 	return (0);
147012314SJames.Moore@Sun.COM }
147112314SJames.Moore@Sun.COM 
147212314SJames.Moore@Sun.COM /*
147312314SJames.Moore@Sun.COM  * Return the current volume size to an external caller.
147412314SJames.Moore@Sun.COM  * The size can change while the volume is open.
147512314SJames.Moore@Sun.COM  */
147612314SJames.Moore@Sun.COM uint64_t
147712314SJames.Moore@Sun.COM zvol_get_volume_size(void *minor_hdl)
147812314SJames.Moore@Sun.COM {
147912314SJames.Moore@Sun.COM 	zvol_state_t *zv = minor_hdl;
148012314SJames.Moore@Sun.COM 
148112314SJames.Moore@Sun.COM 	return (zv->zv_volsize);
148212314SJames.Moore@Sun.COM }
148312314SJames.Moore@Sun.COM 
148412314SJames.Moore@Sun.COM /*
148512314SJames.Moore@Sun.COM  * Return the current WCE setting to an external caller.
148612314SJames.Moore@Sun.COM  * The WCE setting can change while the volume is open.
148712314SJames.Moore@Sun.COM  */
148812314SJames.Moore@Sun.COM int
148912314SJames.Moore@Sun.COM zvol_get_volume_wce(void *minor_hdl)
149012314SJames.Moore@Sun.COM {
149112314SJames.Moore@Sun.COM 	zvol_state_t *zv = minor_hdl;
149212314SJames.Moore@Sun.COM 
149312314SJames.Moore@Sun.COM 	return ((zv->zv_flags & ZVOL_WCE) ? 1 : 0);
149412314SJames.Moore@Sun.COM }
149512314SJames.Moore@Sun.COM 
149612314SJames.Moore@Sun.COM /*
149712314SJames.Moore@Sun.COM  * Entry point for external callers to zvol_log_write
149812314SJames.Moore@Sun.COM  */
149912314SJames.Moore@Sun.COM void
150012314SJames.Moore@Sun.COM zvol_log_write_minor(void *minor_hdl, dmu_tx_t *tx, offset_t off, ssize_t resid,
150112314SJames.Moore@Sun.COM     boolean_t sync)
150212314SJames.Moore@Sun.COM {
150312314SJames.Moore@Sun.COM 	zvol_state_t *zv = minor_hdl;
150412314SJames.Moore@Sun.COM 
150512314SJames.Moore@Sun.COM 	zvol_log_write(zv, tx, off, resid, sync);
150612314SJames.Moore@Sun.COM }
150712314SJames.Moore@Sun.COM /*
150812314SJames.Moore@Sun.COM  * END entry points to allow external callers access to the volume.
150912314SJames.Moore@Sun.COM  */
151012314SJames.Moore@Sun.COM 
151112314SJames.Moore@Sun.COM /*
1512789Sahrens  * Dirtbag ioctls to support mkfs(1M) for UFS filesystems.  See dkio(7I).
1513789Sahrens  */
1514789Sahrens /*ARGSUSED*/
1515789Sahrens int
1516789Sahrens zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
1517789Sahrens {
1518789Sahrens 	zvol_state_t *zv;
15193897Smaybee 	struct dk_cinfo dki;
1520789Sahrens 	struct dk_minfo dkm;
15213897Smaybee 	struct dk_callback *dkc;
1522789Sahrens 	int error = 0;
15236423Sgw25295 	rl_t *rl;
1524789Sahrens 
152512527SChris.Kirby@oracle.com 	mutex_enter(&zfsdev_state_lock);
1526789Sahrens 
152712527SChris.Kirby@oracle.com 	zv = zfsdev_get_soft_state(getminor(dev), ZSST_ZVOL);
1528789Sahrens 
1529789Sahrens 	if (zv == NULL) {
153012527SChris.Kirby@oracle.com 		mutex_exit(&zfsdev_state_lock);
1531789Sahrens 		return (ENXIO);
1532789Sahrens 	}
15339303SEric.Taylor@Sun.COM 	ASSERT(zv->zv_total_opens > 0);
1534789Sahrens 
1535789Sahrens 	switch (cmd) {
1536789Sahrens 
1537789Sahrens 	case DKIOCINFO:
15383897Smaybee 		bzero(&dki, sizeof (dki));
15393897Smaybee 		(void) strcpy(dki.dki_cname, "zvol");
15403897Smaybee 		(void) strcpy(dki.dki_dname, "zvol");
15413897Smaybee 		dki.dki_ctype = DKC_UNKNOWN;
154211689SEric.Taylor@Sun.COM 		dki.dki_unit = getminor(dev);
15433897Smaybee 		dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs);
154412527SChris.Kirby@oracle.com 		mutex_exit(&zfsdev_state_lock);
15453897Smaybee 		if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag))
1546789Sahrens 			error = EFAULT;
1547789Sahrens 		return (error);
1548789Sahrens 
1549789Sahrens 	case DKIOCGMEDIAINFO:
1550789Sahrens 		bzero(&dkm, sizeof (dkm));
1551789Sahrens 		dkm.dki_lbsize = 1U << zv->zv_min_bs;
1552789Sahrens 		dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
1553789Sahrens 		dkm.dki_media_type = DK_UNKNOWN;
155412527SChris.Kirby@oracle.com 		mutex_exit(&zfsdev_state_lock);
1555789Sahrens 		if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
1556789Sahrens 			error = EFAULT;
1557789Sahrens 		return (error);
1558789Sahrens 
1559789Sahrens 	case DKIOCGETEFI:
15607405SEric.Taylor@Sun.COM 		{
15617405SEric.Taylor@Sun.COM 			uint64_t vs = zv->zv_volsize;
15627405SEric.Taylor@Sun.COM 			uint8_t bs = zv->zv_min_bs;
15633016Smaybee 
156412527SChris.Kirby@oracle.com 			mutex_exit(&zfsdev_state_lock);
15657405SEric.Taylor@Sun.COM 			error = zvol_getefi((void *)arg, flag, vs, bs);
15667405SEric.Taylor@Sun.COM 			return (error);
15673016Smaybee 		}
1568789Sahrens 
15693638Sbillm 	case DKIOCFLUSHWRITECACHE:
15703897Smaybee 		dkc = (struct dk_callback *)arg;
157112527SChris.Kirby@oracle.com 		mutex_exit(&zfsdev_state_lock);
1572*12699SNeil.Perrin@Sun.COM 		zil_commit(zv->zv_zilog, ZVOL_OBJ);
15733897Smaybee 		if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) {
15743897Smaybee 			(*dkc->dkc_callback)(dkc->dkc_cookie, error);
15753897Smaybee 			error = 0;
15763897Smaybee 		}
15779303SEric.Taylor@Sun.COM 		return (error);
15789303SEric.Taylor@Sun.COM 
15799303SEric.Taylor@Sun.COM 	case DKIOCGETWCE:
15809303SEric.Taylor@Sun.COM 		{
15819303SEric.Taylor@Sun.COM 			int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0;
15829303SEric.Taylor@Sun.COM 			if (ddi_copyout(&wce, (void *)arg, sizeof (int),
15839303SEric.Taylor@Sun.COM 			    flag))
15849303SEric.Taylor@Sun.COM 				error = EFAULT;
15859303SEric.Taylor@Sun.COM 			break;
15869303SEric.Taylor@Sun.COM 		}
15879303SEric.Taylor@Sun.COM 	case DKIOCSETWCE:
15889303SEric.Taylor@Sun.COM 		{
15899303SEric.Taylor@Sun.COM 			int wce;
15909303SEric.Taylor@Sun.COM 			if (ddi_copyin((void *)arg, &wce, sizeof (int),
15919303SEric.Taylor@Sun.COM 			    flag)) {
15929303SEric.Taylor@Sun.COM 				error = EFAULT;
15939303SEric.Taylor@Sun.COM 				break;
15949303SEric.Taylor@Sun.COM 			}
15959303SEric.Taylor@Sun.COM 			if (wce) {
15969303SEric.Taylor@Sun.COM 				zv->zv_flags |= ZVOL_WCE;
159712527SChris.Kirby@oracle.com 				mutex_exit(&zfsdev_state_lock);
15989303SEric.Taylor@Sun.COM 			} else {
15999303SEric.Taylor@Sun.COM 				zv->zv_flags &= ~ZVOL_WCE;
160012527SChris.Kirby@oracle.com 				mutex_exit(&zfsdev_state_lock);
1601*12699SNeil.Perrin@Sun.COM 				zil_commit(zv->zv_zilog, ZVOL_OBJ);
16029303SEric.Taylor@Sun.COM 			}
16039303SEric.Taylor@Sun.COM 			return (0);
16049303SEric.Taylor@Sun.COM 		}
16053638Sbillm 
16063245Smaybee 	case DKIOCGGEOM:
16073245Smaybee 	case DKIOCGVTOC:
16086423Sgw25295 		/*
16096423Sgw25295 		 * commands using these (like prtvtoc) expect ENOTSUP
16106423Sgw25295 		 * since we're emulating an EFI label
16116423Sgw25295 		 */
16123245Smaybee 		error = ENOTSUP;
16133245Smaybee 		break;
16143245Smaybee 
16156423Sgw25295 	case DKIOCDUMPINIT:
16166423Sgw25295 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
16176423Sgw25295 		    RL_WRITER);
16186423Sgw25295 		error = zvol_dumpify(zv);
16196423Sgw25295 		zfs_range_unlock(rl);
16206423Sgw25295 		break;
16216423Sgw25295 
16226423Sgw25295 	case DKIOCDUMPFINI:
16239277SEric.Taylor@Sun.COM 		if (!(zv->zv_flags & ZVOL_DUMPIFIED))
16249277SEric.Taylor@Sun.COM 			break;
16256423Sgw25295 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
16266423Sgw25295 		    RL_WRITER);
16276423Sgw25295 		error = zvol_dump_fini(zv);
16286423Sgw25295 		zfs_range_unlock(rl);
16296423Sgw25295 		break;
16306423Sgw25295 
1631789Sahrens 	default:
16323016Smaybee 		error = ENOTTY;
1633789Sahrens 		break;
1634789Sahrens 
1635789Sahrens 	}
163612527SChris.Kirby@oracle.com 	mutex_exit(&zfsdev_state_lock);
1637789Sahrens 	return (error);
1638789Sahrens }
1639789Sahrens 
1640789Sahrens int
1641789Sahrens zvol_busy(void)
1642789Sahrens {
1643789Sahrens 	return (zvol_minors != 0);
1644789Sahrens }
1645789Sahrens 
1646789Sahrens void
1647789Sahrens zvol_init(void)
1648789Sahrens {
164912527SChris.Kirby@oracle.com 	VERIFY(ddi_soft_state_init(&zfsdev_state, sizeof (zfs_soft_state_t),
165012527SChris.Kirby@oracle.com 	    1) == 0);
165112527SChris.Kirby@oracle.com 	mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL);
1652789Sahrens }
1653789Sahrens 
1654789Sahrens void
1655789Sahrens zvol_fini(void)
1656789Sahrens {
165712527SChris.Kirby@oracle.com 	mutex_destroy(&zfsdev_state_lock);
165812527SChris.Kirby@oracle.com 	ddi_soft_state_fini(&zfsdev_state);
1659789Sahrens }
16606423Sgw25295 
16616423Sgw25295 static int
16626423Sgw25295 zvol_dump_init(zvol_state_t *zv, boolean_t resize)
16636423Sgw25295 {
16646423Sgw25295 	dmu_tx_t *tx;
16656423Sgw25295 	int error = 0;
16666423Sgw25295 	objset_t *os = zv->zv_objset;
16676423Sgw25295 	nvlist_t *nv = NULL;
166811656SGeorge.Wilson@Sun.COM 	uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset));
16696423Sgw25295 
167012527SChris.Kirby@oracle.com 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
167110588SEric.Taylor@Sun.COM 	error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0,
167210588SEric.Taylor@Sun.COM 	    DMU_OBJECT_END);
167310588SEric.Taylor@Sun.COM 	/* wait for dmu_free_long_range to actually free the blocks */
167410588SEric.Taylor@Sun.COM 	txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
16756423Sgw25295 
16766423Sgw25295 	tx = dmu_tx_create(os);
16776423Sgw25295 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
167810588SEric.Taylor@Sun.COM 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
16796423Sgw25295 	error = dmu_tx_assign(tx, TXG_WAIT);
16806423Sgw25295 	if (error) {
16816423Sgw25295 		dmu_tx_abort(tx);
16826423Sgw25295 		return (error);
16836423Sgw25295 	}
16846423Sgw25295 
16856423Sgw25295 	/*
16866423Sgw25295 	 * If we are resizing the dump device then we only need to
16876423Sgw25295 	 * update the refreservation to match the newly updated
16886423Sgw25295 	 * zvolsize. Otherwise, we save off the original state of the
16896423Sgw25295 	 * zvol so that we can restore them if the zvol is ever undumpified.
16906423Sgw25295 	 */
16916423Sgw25295 	if (resize) {
16926423Sgw25295 		error = zap_update(os, ZVOL_ZAP_OBJ,
16936423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
16946423Sgw25295 		    &zv->zv_volsize, tx);
16956423Sgw25295 	} else {
169611619SGeorge.Wilson@Sun.COM 		uint64_t checksum, compress, refresrv, vbs, dedup;
16977837SMatthew.Ahrens@Sun.COM 
16986423Sgw25295 		error = dsl_prop_get_integer(zv->zv_name,
16996423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL);
17006423Sgw25295 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
17016423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL);
17026423Sgw25295 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
17036423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL);
17047837SMatthew.Ahrens@Sun.COM 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
17057837SMatthew.Ahrens@Sun.COM 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL);
170611656SGeorge.Wilson@Sun.COM 		if (version >= SPA_VERSION_DEDUP) {
170711656SGeorge.Wilson@Sun.COM 			error = error ? error :
170811656SGeorge.Wilson@Sun.COM 			    dsl_prop_get_integer(zv->zv_name,
170911656SGeorge.Wilson@Sun.COM 			    zfs_prop_to_name(ZFS_PROP_DEDUP), &dedup, NULL);
171011656SGeorge.Wilson@Sun.COM 		}
17116423Sgw25295 
17126423Sgw25295 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
17136423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1,
17146423Sgw25295 		    &compress, tx);
17156423Sgw25295 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
17166423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx);
17176423Sgw25295 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
17186423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
17196423Sgw25295 		    &refresrv, tx);
17207837SMatthew.Ahrens@Sun.COM 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
17217837SMatthew.Ahrens@Sun.COM 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1,
17227837SMatthew.Ahrens@Sun.COM 		    &vbs, tx);
172310588SEric.Taylor@Sun.COM 		error = error ? error : dmu_object_set_blocksize(
172410588SEric.Taylor@Sun.COM 		    os, ZVOL_OBJ, SPA_MAXBLOCKSIZE, 0, tx);
172511656SGeorge.Wilson@Sun.COM 		if (version >= SPA_VERSION_DEDUP) {
172611656SGeorge.Wilson@Sun.COM 			error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
172711656SGeorge.Wilson@Sun.COM 			    zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1,
172811656SGeorge.Wilson@Sun.COM 			    &dedup, tx);
172911656SGeorge.Wilson@Sun.COM 		}
173010588SEric.Taylor@Sun.COM 		if (error == 0)
173110588SEric.Taylor@Sun.COM 			zv->zv_volblocksize = SPA_MAXBLOCKSIZE;
17326423Sgw25295 	}
17336423Sgw25295 	dmu_tx_commit(tx);
17346423Sgw25295 
17356423Sgw25295 	/*
17366423Sgw25295 	 * We only need update the zvol's property if we are initializing
17376423Sgw25295 	 * the dump area for the first time.
17386423Sgw25295 	 */
17396423Sgw25295 	if (!resize) {
17406423Sgw25295 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
17416423Sgw25295 		VERIFY(nvlist_add_uint64(nv,
17426423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0);
17436423Sgw25295 		VERIFY(nvlist_add_uint64(nv,
17446423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
17456423Sgw25295 		    ZIO_COMPRESS_OFF) == 0);
17466423Sgw25295 		VERIFY(nvlist_add_uint64(nv,
17476423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
17486423Sgw25295 		    ZIO_CHECKSUM_OFF) == 0);
174911656SGeorge.Wilson@Sun.COM 		if (version >= SPA_VERSION_DEDUP) {
175011656SGeorge.Wilson@Sun.COM 			VERIFY(nvlist_add_uint64(nv,
175111656SGeorge.Wilson@Sun.COM 			    zfs_prop_to_name(ZFS_PROP_DEDUP),
175211656SGeorge.Wilson@Sun.COM 			    ZIO_CHECKSUM_OFF) == 0);
175311656SGeorge.Wilson@Sun.COM 		}
17546423Sgw25295 
175511022STom.Erickson@Sun.COM 		error = zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
175611022STom.Erickson@Sun.COM 		    nv, NULL);
17576423Sgw25295 		nvlist_free(nv);
17586423Sgw25295 
17596423Sgw25295 		if (error)
17606423Sgw25295 			return (error);
17616423Sgw25295 	}
17626423Sgw25295 
17636423Sgw25295 	/* Allocate the space for the dump */
17646423Sgw25295 	error = zvol_prealloc(zv);
17656423Sgw25295 	return (error);
17666423Sgw25295 }
17676423Sgw25295 
17686423Sgw25295 static int
17696423Sgw25295 zvol_dumpify(zvol_state_t *zv)
17706423Sgw25295 {
17716423Sgw25295 	int error = 0;
17726423Sgw25295 	uint64_t dumpsize = 0;
17736423Sgw25295 	dmu_tx_t *tx;
17746423Sgw25295 	objset_t *os = zv->zv_objset;
17756423Sgw25295 
177610588SEric.Taylor@Sun.COM 	if (zv->zv_flags & ZVOL_RDONLY)
17776423Sgw25295 		return (EROFS);
17786423Sgw25295 
17796423Sgw25295 	if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE,
17806423Sgw25295 	    8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) {
17816423Sgw25295 		boolean_t resize = (dumpsize > 0) ? B_TRUE : B_FALSE;
17826423Sgw25295 
17836423Sgw25295 		if ((error = zvol_dump_init(zv, resize)) != 0) {
17846423Sgw25295 			(void) zvol_dump_fini(zv);
17856423Sgw25295 			return (error);
17866423Sgw25295 		}
17876423Sgw25295 	}
17886423Sgw25295 
17896423Sgw25295 	/*
17906423Sgw25295 	 * Build up our lba mapping.
17916423Sgw25295 	 */
17926423Sgw25295 	error = zvol_get_lbas(zv);
17936423Sgw25295 	if (error) {
17946423Sgw25295 		(void) zvol_dump_fini(zv);
17956423Sgw25295 		return (error);
17966423Sgw25295 	}
17976423Sgw25295 
17986423Sgw25295 	tx = dmu_tx_create(os);
17996423Sgw25295 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
18006423Sgw25295 	error = dmu_tx_assign(tx, TXG_WAIT);
18016423Sgw25295 	if (error) {
18026423Sgw25295 		dmu_tx_abort(tx);
18036423Sgw25295 		(void) zvol_dump_fini(zv);
18046423Sgw25295 		return (error);
18056423Sgw25295 	}
18066423Sgw25295 
18076423Sgw25295 	zv->zv_flags |= ZVOL_DUMPIFIED;
18086423Sgw25295 	error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1,
18096423Sgw25295 	    &zv->zv_volsize, tx);
18106423Sgw25295 	dmu_tx_commit(tx);
18116423Sgw25295 
18126423Sgw25295 	if (error) {
18136423Sgw25295 		(void) zvol_dump_fini(zv);
18146423Sgw25295 		return (error);
18156423Sgw25295 	}
18166423Sgw25295 
18176423Sgw25295 	txg_wait_synced(dmu_objset_pool(os), 0);
18186423Sgw25295 	return (0);
18196423Sgw25295 }
18206423Sgw25295 
18216423Sgw25295 static int
18226423Sgw25295 zvol_dump_fini(zvol_state_t *zv)
18236423Sgw25295 {
18246423Sgw25295 	dmu_tx_t *tx;
18256423Sgw25295 	objset_t *os = zv->zv_objset;
18266423Sgw25295 	nvlist_t *nv;
18276423Sgw25295 	int error = 0;
182811619SGeorge.Wilson@Sun.COM 	uint64_t checksum, compress, refresrv, vbs, dedup;
182911656SGeorge.Wilson@Sun.COM 	uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset));
18306423Sgw25295 
18317080Smaybee 	/*
18327080Smaybee 	 * Attempt to restore the zvol back to its pre-dumpified state.
18337080Smaybee 	 * This is a best-effort attempt as it's possible that not all
18347080Smaybee 	 * of these properties were initialized during the dumpify process
18357080Smaybee 	 * (i.e. error during zvol_dump_init).
18367080Smaybee 	 */
18377080Smaybee 
18386423Sgw25295 	tx = dmu_tx_create(os);
18396423Sgw25295 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
18406423Sgw25295 	error = dmu_tx_assign(tx, TXG_WAIT);
18416423Sgw25295 	if (error) {
18426423Sgw25295 		dmu_tx_abort(tx);
18436423Sgw25295 		return (error);
18446423Sgw25295 	}
18457080Smaybee 	(void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx);
18467080Smaybee 	dmu_tx_commit(tx);
18476423Sgw25295 
18486423Sgw25295 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
18496423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum);
18506423Sgw25295 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
18516423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress);
18526423Sgw25295 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
18536423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv);
18547837SMatthew.Ahrens@Sun.COM 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
18557837SMatthew.Ahrens@Sun.COM 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs);
18566423Sgw25295 
18576423Sgw25295 	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
18586423Sgw25295 	(void) nvlist_add_uint64(nv,
18596423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum);
18606423Sgw25295 	(void) nvlist_add_uint64(nv,
18616423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress);
18626423Sgw25295 	(void) nvlist_add_uint64(nv,
18636423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv);
186411656SGeorge.Wilson@Sun.COM 	if (version >= SPA_VERSION_DEDUP &&
186511656SGeorge.Wilson@Sun.COM 	    zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
186611656SGeorge.Wilson@Sun.COM 	    zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, &dedup) == 0) {
186711656SGeorge.Wilson@Sun.COM 		(void) nvlist_add_uint64(nv,
186811656SGeorge.Wilson@Sun.COM 		    zfs_prop_to_name(ZFS_PROP_DEDUP), dedup);
186911656SGeorge.Wilson@Sun.COM 	}
187011022STom.Erickson@Sun.COM 	(void) zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
187111022STom.Erickson@Sun.COM 	    nv, NULL);
18726423Sgw25295 	nvlist_free(nv);
18736423Sgw25295 
18747080Smaybee 	zvol_free_extents(zv);
18757080Smaybee 	zv->zv_flags &= ~ZVOL_DUMPIFIED;
18767080Smaybee 	(void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END);
187710588SEric.Taylor@Sun.COM 	/* wait for dmu_free_long_range to actually free the blocks */
187810588SEric.Taylor@Sun.COM 	txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
187910588SEric.Taylor@Sun.COM 	tx = dmu_tx_create(os);
188010588SEric.Taylor@Sun.COM 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
188110588SEric.Taylor@Sun.COM 	error = dmu_tx_assign(tx, TXG_WAIT);
188210588SEric.Taylor@Sun.COM 	if (error) {
188310588SEric.Taylor@Sun.COM 		dmu_tx_abort(tx);
188410588SEric.Taylor@Sun.COM 		return (error);
188510588SEric.Taylor@Sun.COM 	}
188610922SJeff.Bonwick@Sun.COM 	if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0)
188710922SJeff.Bonwick@Sun.COM 		zv->zv_volblocksize = vbs;
188810588SEric.Taylor@Sun.COM 	dmu_tx_commit(tx);
18897080Smaybee 
18906423Sgw25295 	return (0);
18916423Sgw25295 }
1892