xref: /onnv-gate/usr/src/uts/common/fs/zfs/zvol.c (revision 13049:bda0decf867b)
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
zvol_size_changed(uint64_t volsize,major_t maj,minor_t min)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
zvol_check_volsize(uint64_t volsize,uint64_t blocksize)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
zvol_check_volblocksize(uint64_t volblocksize)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
zvol_get_stats(objset_t * os,nvlist_t * nv)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 *
zvol_minor_lookup(const char * name)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
zvol_map_block(spa_t * spa,zilog_t * zilog,const blkptr_t * bp,arc_buf_t * pbuf,const zbookmark_t * zb,const dnode_phys_t * dnp,void * arg)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
zvol_free_extents(zvol_state_t * zv)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
zvol_get_lbas(zvol_state_t * zv)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
zvol_create_cb(objset_t * os,void * arg,cred_t * cr,dmu_tx_t * tx)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
zvol_replay_write(zvol_state_t * zv,lr_write_t * lr,boolean_t byteswap)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
zvol_replay_err(zvol_state_t * zv,lr_t * lr,boolean_t byteswap)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
zvol_name2minor(const char * name,minor_t * minor)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
zvol_create_minor(const char * name)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 */
45412827SMatthew.Ahrens@Sun.COM 	error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, FTAG, &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) {
46212827SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(os, FTAG);
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) {
46812827SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(os, FTAG);
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);
48012827SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(os, FTAG);
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);
49112827SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(os, FTAG);
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;
503*13049SGeorge.Wilson@Sun.COM 	if (dmu_objset_is_snapshot(os) || !spa_writeable(dmu_objset_spa(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 
515*13049SGeorge.Wilson@Sun.COM 	if (spa_writeable(dmu_objset_spa(os))) {
516*13049SGeorge.Wilson@Sun.COM 		if (zil_replay_disable)
517*13049SGeorge.Wilson@Sun.COM 			zil_destroy(dmu_objset_zil(os), B_FALSE);
518*13049SGeorge.Wilson@Sun.COM 		else
519*13049SGeorge.Wilson@Sun.COM 			zil_replay(os, zv, zvol_replay_vector);
520*13049SGeorge.Wilson@Sun.COM 	}
52112827SMatthew.Ahrens@Sun.COM 	dmu_objset_disown(os, FTAG);
52210588SEric.Taylor@Sun.COM 	zv->zv_objset = NULL;
523789Sahrens 
524789Sahrens 	zvol_minors++;
525789Sahrens 
52612527SChris.Kirby@oracle.com 	mutex_exit(&zfsdev_state_lock);
527789Sahrens 
528789Sahrens 	return (0);
529789Sahrens }
530789Sahrens 
531789Sahrens /*
532789Sahrens  * Remove minor node for the specified volume.
533789Sahrens  */
53410588SEric.Taylor@Sun.COM static int
zvol_remove_zv(zvol_state_t * zv)53510588SEric.Taylor@Sun.COM zvol_remove_zv(zvol_state_t *zv)
536789Sahrens {
53710588SEric.Taylor@Sun.COM 	char nmbuf[20];
53812527SChris.Kirby@oracle.com 	minor_t minor = zv->zv_minor;
539789Sahrens 
54012527SChris.Kirby@oracle.com 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
54110588SEric.Taylor@Sun.COM 	if (zv->zv_total_opens != 0)
542789Sahrens 		return (EBUSY);
543789Sahrens 
54412527SChris.Kirby@oracle.com 	(void) snprintf(nmbuf, sizeof (nmbuf), "%u,raw", minor);
54510588SEric.Taylor@Sun.COM 	ddi_remove_minor_node(zfs_dip, nmbuf);
546789Sahrens 
54712527SChris.Kirby@oracle.com 	(void) snprintf(nmbuf, sizeof (nmbuf), "%u", minor);
54810588SEric.Taylor@Sun.COM 	ddi_remove_minor_node(zfs_dip, nmbuf);
549789Sahrens 
5503755Sperrin 	avl_destroy(&zv->zv_znode.z_range_avl);
5513755Sperrin 	mutex_destroy(&zv->zv_znode.z_range_lock);
552789Sahrens 
55312527SChris.Kirby@oracle.com 	kmem_free(zv, sizeof (zvol_state_t));
55412527SChris.Kirby@oracle.com 
55512527SChris.Kirby@oracle.com 	ddi_soft_state_free(zfsdev_state, minor);
556789Sahrens 
557789Sahrens 	zvol_minors--;
55810588SEric.Taylor@Sun.COM 	return (0);
55910588SEric.Taylor@Sun.COM }
560789Sahrens 
56110588SEric.Taylor@Sun.COM int
zvol_remove_minor(const char * name)56210588SEric.Taylor@Sun.COM zvol_remove_minor(const char *name)
56310588SEric.Taylor@Sun.COM {
56410588SEric.Taylor@Sun.COM 	zvol_state_t *zv;
56510588SEric.Taylor@Sun.COM 	int rc;
56610588SEric.Taylor@Sun.COM 
56712527SChris.Kirby@oracle.com 	mutex_enter(&zfsdev_state_lock);
56810588SEric.Taylor@Sun.COM 	if ((zv = zvol_minor_lookup(name)) == NULL) {
56912527SChris.Kirby@oracle.com 		mutex_exit(&zfsdev_state_lock);
57010588SEric.Taylor@Sun.COM 		return (ENXIO);
57110588SEric.Taylor@Sun.COM 	}
57210588SEric.Taylor@Sun.COM 	rc = zvol_remove_zv(zv);
57312527SChris.Kirby@oracle.com 	mutex_exit(&zfsdev_state_lock);
57410588SEric.Taylor@Sun.COM 	return (rc);
57510588SEric.Taylor@Sun.COM }
576789Sahrens 
57710588SEric.Taylor@Sun.COM int
zvol_first_open(zvol_state_t * zv)57810588SEric.Taylor@Sun.COM zvol_first_open(zvol_state_t *zv)
57910588SEric.Taylor@Sun.COM {
58010588SEric.Taylor@Sun.COM 	objset_t *os;
58110588SEric.Taylor@Sun.COM 	uint64_t volsize;
58210588SEric.Taylor@Sun.COM 	int error;
58310588SEric.Taylor@Sun.COM 	uint64_t readonly;
58410588SEric.Taylor@Sun.COM 
58510588SEric.Taylor@Sun.COM 	/* lie and say we're read-only */
58610588SEric.Taylor@Sun.COM 	error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, B_TRUE,
58710588SEric.Taylor@Sun.COM 	    zvol_tag, &os);
58810588SEric.Taylor@Sun.COM 	if (error)
58910588SEric.Taylor@Sun.COM 		return (error);
59010588SEric.Taylor@Sun.COM 
59110588SEric.Taylor@Sun.COM 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
59210588SEric.Taylor@Sun.COM 	if (error) {
59310588SEric.Taylor@Sun.COM 		ASSERT(error == 0);
59410588SEric.Taylor@Sun.COM 		dmu_objset_disown(os, zvol_tag);
59510588SEric.Taylor@Sun.COM 		return (error);
59610588SEric.Taylor@Sun.COM 	}
59710588SEric.Taylor@Sun.COM 	zv->zv_objset = os;
59812123STim.Haley@Sun.COM 	error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf);
59912123STim.Haley@Sun.COM 	if (error) {
60012123STim.Haley@Sun.COM 		dmu_objset_disown(os, zvol_tag);
60112123STim.Haley@Sun.COM 		return (error);
60212123STim.Haley@Sun.COM 	}
60310588SEric.Taylor@Sun.COM 	zv->zv_volsize = volsize;
60410588SEric.Taylor@Sun.COM 	zv->zv_zilog = zil_open(os, zvol_get_data);
60510588SEric.Taylor@Sun.COM 	zvol_size_changed(zv->zv_volsize, ddi_driver_major(zfs_dip),
60610588SEric.Taylor@Sun.COM 	    zv->zv_minor);
60710588SEric.Taylor@Sun.COM 
60810588SEric.Taylor@Sun.COM 	VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &readonly,
60910588SEric.Taylor@Sun.COM 	    NULL) == 0);
610*13049SGeorge.Wilson@Sun.COM 	if (readonly || dmu_objset_is_snapshot(os) ||
611*13049SGeorge.Wilson@Sun.COM 	    !spa_writeable(dmu_objset_spa(os)))
61210588SEric.Taylor@Sun.COM 		zv->zv_flags |= ZVOL_RDONLY;
61310588SEric.Taylor@Sun.COM 	else
61410588SEric.Taylor@Sun.COM 		zv->zv_flags &= ~ZVOL_RDONLY;
61510588SEric.Taylor@Sun.COM 	return (error);
61610588SEric.Taylor@Sun.COM }
61710588SEric.Taylor@Sun.COM 
61810588SEric.Taylor@Sun.COM void
zvol_last_close(zvol_state_t * zv)61910588SEric.Taylor@Sun.COM zvol_last_close(zvol_state_t *zv)
62010588SEric.Taylor@Sun.COM {
62110588SEric.Taylor@Sun.COM 	zil_close(zv->zv_zilog);
62210588SEric.Taylor@Sun.COM 	zv->zv_zilog = NULL;
62312123STim.Haley@Sun.COM 	dmu_buf_rele(zv->zv_dbuf, zvol_tag);
62412123STim.Haley@Sun.COM 	zv->zv_dbuf = NULL;
62510588SEric.Taylor@Sun.COM 	dmu_objset_disown(zv->zv_objset, zvol_tag);
62610588SEric.Taylor@Sun.COM 	zv->zv_objset = NULL;
627789Sahrens }
628789Sahrens 
6296423Sgw25295 int
zvol_prealloc(zvol_state_t * zv)6306423Sgw25295 zvol_prealloc(zvol_state_t *zv)
6316423Sgw25295 {
6326423Sgw25295 	objset_t *os = zv->zv_objset;
6336423Sgw25295 	dmu_tx_t *tx;
6346423Sgw25295 	uint64_t refd, avail, usedobjs, availobjs;
6356423Sgw25295 	uint64_t resid = zv->zv_volsize;
6366423Sgw25295 	uint64_t off = 0;
6376423Sgw25295 
6386423Sgw25295 	/* Check the space usage before attempting to allocate the space */
6396423Sgw25295 	dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs);
6406423Sgw25295 	if (avail < zv->zv_volsize)
6416423Sgw25295 		return (ENOSPC);
6426423Sgw25295 
6436423Sgw25295 	/* Free old extents if they exist */
6446423Sgw25295 	zvol_free_extents(zv);
6456423Sgw25295 
6466423Sgw25295 	while (resid != 0) {
6476423Sgw25295 		int error;
6486423Sgw25295 		uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE);
6496423Sgw25295 
6506423Sgw25295 		tx = dmu_tx_create(os);
6516423Sgw25295 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
6526423Sgw25295 		error = dmu_tx_assign(tx, TXG_WAIT);
6536423Sgw25295 		if (error) {
6546423Sgw25295 			dmu_tx_abort(tx);
6556992Smaybee 			(void) dmu_free_long_range(os, ZVOL_OBJ, 0, off);
6566423Sgw25295 			return (error);
6576423Sgw25295 		}
6587872STim.Haley@Sun.COM 		dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx);
6596423Sgw25295 		dmu_tx_commit(tx);
6606423Sgw25295 		off += bytes;
6616423Sgw25295 		resid -= bytes;
6626423Sgw25295 	}
6636423Sgw25295 	txg_wait_synced(dmu_objset_pool(os), 0);
6646423Sgw25295 
6656423Sgw25295 	return (0);
6666423Sgw25295 }
6676423Sgw25295 
6686423Sgw25295 int
zvol_update_volsize(objset_t * os,uint64_t volsize)66910588SEric.Taylor@Sun.COM zvol_update_volsize(objset_t *os, uint64_t volsize)
6706423Sgw25295 {
6716423Sgw25295 	dmu_tx_t *tx;
6726423Sgw25295 	int error;
6736423Sgw25295 
67412527SChris.Kirby@oracle.com 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
6756423Sgw25295 
67610588SEric.Taylor@Sun.COM 	tx = dmu_tx_create(os);
6776423Sgw25295 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
6786423Sgw25295 	error = dmu_tx_assign(tx, TXG_WAIT);
6796423Sgw25295 	if (error) {
6806423Sgw25295 		dmu_tx_abort(tx);
6816423Sgw25295 		return (error);
6826423Sgw25295 	}
6836423Sgw25295 
68410588SEric.Taylor@Sun.COM 	error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
6856423Sgw25295 	    &volsize, tx);
6866423Sgw25295 	dmu_tx_commit(tx);
6876423Sgw25295 
6886423Sgw25295 	if (error == 0)
68910588SEric.Taylor@Sun.COM 		error = dmu_free_long_range(os,
6906992Smaybee 		    ZVOL_OBJ, volsize, DMU_OBJECT_END);
69110588SEric.Taylor@Sun.COM 	return (error);
69210588SEric.Taylor@Sun.COM }
69310588SEric.Taylor@Sun.COM 
69410588SEric.Taylor@Sun.COM void
zvol_remove_minors(const char * name)69510588SEric.Taylor@Sun.COM zvol_remove_minors(const char *name)
69610588SEric.Taylor@Sun.COM {
69710588SEric.Taylor@Sun.COM 	zvol_state_t *zv;
69810588SEric.Taylor@Sun.COM 	char *namebuf;
69910588SEric.Taylor@Sun.COM 	minor_t minor;
7006423Sgw25295 
70110588SEric.Taylor@Sun.COM 	namebuf = kmem_zalloc(strlen(name) + 2, KM_SLEEP);
70210588SEric.Taylor@Sun.COM 	(void) strncpy(namebuf, name, strlen(name));
70310588SEric.Taylor@Sun.COM 	(void) strcat(namebuf, "/");
70412527SChris.Kirby@oracle.com 	mutex_enter(&zfsdev_state_lock);
70512527SChris.Kirby@oracle.com 	for (minor = 1; minor <= ZFSDEV_MAX_MINOR; minor++) {
70610588SEric.Taylor@Sun.COM 
70712527SChris.Kirby@oracle.com 		zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
70810588SEric.Taylor@Sun.COM 		if (zv == NULL)
70910588SEric.Taylor@Sun.COM 			continue;
71010588SEric.Taylor@Sun.COM 		if (strncmp(namebuf, zv->zv_name, strlen(namebuf)) == 0)
71110588SEric.Taylor@Sun.COM 			(void) zvol_remove_zv(zv);
7126423Sgw25295 	}
71310588SEric.Taylor@Sun.COM 	kmem_free(namebuf, strlen(name) + 2);
71410588SEric.Taylor@Sun.COM 
71512527SChris.Kirby@oracle.com 	mutex_exit(&zfsdev_state_lock);
7166423Sgw25295 }
7176423Sgw25295 
718789Sahrens int
zvol_set_volsize(const char * name,major_t maj,uint64_t volsize)7194787Sahrens zvol_set_volsize(const char *name, major_t maj, uint64_t volsize)
720789Sahrens {
72110588SEric.Taylor@Sun.COM 	zvol_state_t *zv = NULL;
72210588SEric.Taylor@Sun.COM 	objset_t *os;
723789Sahrens 	int error;
7241133Seschrock 	dmu_object_info_t doi;
7256423Sgw25295 	uint64_t old_volsize = 0ULL;
72610588SEric.Taylor@Sun.COM 	uint64_t readonly;
727789Sahrens 
72812527SChris.Kirby@oracle.com 	mutex_enter(&zfsdev_state_lock);
72910588SEric.Taylor@Sun.COM 	zv = zvol_minor_lookup(name);
73010588SEric.Taylor@Sun.COM 	if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) {
73112527SChris.Kirby@oracle.com 		mutex_exit(&zfsdev_state_lock);
73210588SEric.Taylor@Sun.COM 		return (error);
73310588SEric.Taylor@Sun.COM 	}
734789Sahrens 
73510588SEric.Taylor@Sun.COM 	if ((error = dmu_object_info(os, ZVOL_OBJ, &doi)) != 0 ||
7362676Seschrock 	    (error = zvol_check_volsize(volsize,
7377265Sahrens 	    doi.doi_data_block_size)) != 0)
7387265Sahrens 		goto out;
7391133Seschrock 
74010588SEric.Taylor@Sun.COM 	VERIFY(dsl_prop_get_integer(name, "readonly", &readonly,
74110588SEric.Taylor@Sun.COM 	    NULL) == 0);
74210588SEric.Taylor@Sun.COM 	if (readonly) {
7437265Sahrens 		error = EROFS;
7447265Sahrens 		goto out;
745789Sahrens 	}
746789Sahrens 
74710588SEric.Taylor@Sun.COM 	error = zvol_update_volsize(os, volsize);
7486423Sgw25295 	/*
7496423Sgw25295 	 * Reinitialize the dump area to the new size. If we
75010588SEric.Taylor@Sun.COM 	 * failed to resize the dump area then restore it back to
75110588SEric.Taylor@Sun.COM 	 * its original size.
7526423Sgw25295 	 */
75310588SEric.Taylor@Sun.COM 	if (zv && error == 0) {
75410588SEric.Taylor@Sun.COM 		if (zv->zv_flags & ZVOL_DUMPIFIED) {
75510588SEric.Taylor@Sun.COM 			old_volsize = zv->zv_volsize;
75610588SEric.Taylor@Sun.COM 			zv->zv_volsize = volsize;
75710588SEric.Taylor@Sun.COM 			if ((error = zvol_dumpify(zv)) != 0 ||
75810588SEric.Taylor@Sun.COM 			    (error = dumpvp_resize()) != 0) {
75910588SEric.Taylor@Sun.COM 				(void) zvol_update_volsize(os, old_volsize);
76010588SEric.Taylor@Sun.COM 				zv->zv_volsize = old_volsize;
76110588SEric.Taylor@Sun.COM 				error = zvol_dumpify(zv);
76210588SEric.Taylor@Sun.COM 			}
76310588SEric.Taylor@Sun.COM 		}
76410588SEric.Taylor@Sun.COM 		if (error == 0) {
76510588SEric.Taylor@Sun.COM 			zv->zv_volsize = volsize;
76610588SEric.Taylor@Sun.COM 			zvol_size_changed(volsize, maj, zv->zv_minor);
7676423Sgw25295 		}
768789Sahrens 	}
769789Sahrens 
7709816SGeorge.Wilson@Sun.COM 	/*
7719816SGeorge.Wilson@Sun.COM 	 * Generate a LUN expansion event.
7729816SGeorge.Wilson@Sun.COM 	 */
77310588SEric.Taylor@Sun.COM 	if (zv && error == 0) {
7749816SGeorge.Wilson@Sun.COM 		sysevent_id_t eid;
7759816SGeorge.Wilson@Sun.COM 		nvlist_t *attr;
7769816SGeorge.Wilson@Sun.COM 		char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
7779816SGeorge.Wilson@Sun.COM 
77810588SEric.Taylor@Sun.COM 		(void) snprintf(physpath, MAXPATHLEN, "%s%u", ZVOL_PSEUDO_DEV,
7799816SGeorge.Wilson@Sun.COM 		    zv->zv_minor);
7809816SGeorge.Wilson@Sun.COM 
7819816SGeorge.Wilson@Sun.COM 		VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
7829816SGeorge.Wilson@Sun.COM 		VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
7839816SGeorge.Wilson@Sun.COM 
7849816SGeorge.Wilson@Sun.COM 		(void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
7859816SGeorge.Wilson@Sun.COM 		    ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
7869816SGeorge.Wilson@Sun.COM 
7879816SGeorge.Wilson@Sun.COM 		nvlist_free(attr);
7889816SGeorge.Wilson@Sun.COM 		kmem_free(physpath, MAXPATHLEN);
7899816SGeorge.Wilson@Sun.COM 	}
7909816SGeorge.Wilson@Sun.COM 
7917265Sahrens out:
79210588SEric.Taylor@Sun.COM 	dmu_objset_rele(os, FTAG);
7937265Sahrens 
79412527SChris.Kirby@oracle.com 	mutex_exit(&zfsdev_state_lock);
795789Sahrens 
796789Sahrens 	return (error);
797789Sahrens }
798789Sahrens 
799789Sahrens /*ARGSUSED*/
800789Sahrens int
zvol_open(dev_t * devp,int flag,int otyp,cred_t * cr)801789Sahrens zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr)
802789Sahrens {
803789Sahrens 	zvol_state_t *zv;
80410588SEric.Taylor@Sun.COM 	int err = 0;
805789Sahrens 
80612527SChris.Kirby@oracle.com 	mutex_enter(&zfsdev_state_lock);
807789Sahrens 
80812527SChris.Kirby@oracle.com 	zv = zfsdev_get_soft_state(getminor(*devp), ZSST_ZVOL);
809789Sahrens 	if (zv == NULL) {
81012527SChris.Kirby@oracle.com 		mutex_exit(&zfsdev_state_lock);
811789Sahrens 		return (ENXIO);
812789Sahrens 	}
813789Sahrens 
81410588SEric.Taylor@Sun.COM 	if (zv->zv_total_opens == 0)
81510588SEric.Taylor@Sun.COM 		err = zvol_first_open(zv);
81610588SEric.Taylor@Sun.COM 	if (err) {
81712527SChris.Kirby@oracle.com 		mutex_exit(&zfsdev_state_lock);
81810588SEric.Taylor@Sun.COM 		return (err);
81910588SEric.Taylor@Sun.COM 	}
82010588SEric.Taylor@Sun.COM 	if ((flag & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
82110588SEric.Taylor@Sun.COM 		err = EROFS;
82210588SEric.Taylor@Sun.COM 		goto out;
823789Sahrens 	}
8247405SEric.Taylor@Sun.COM 	if (zv->zv_flags & ZVOL_EXCL) {
82510588SEric.Taylor@Sun.COM 		err = EBUSY;
82610588SEric.Taylor@Sun.COM 		goto out;
8277405SEric.Taylor@Sun.COM 	}
8287405SEric.Taylor@Sun.COM 	if (flag & FEXCL) {
8297405SEric.Taylor@Sun.COM 		if (zv->zv_total_opens != 0) {
83010588SEric.Taylor@Sun.COM 			err = EBUSY;
83110588SEric.Taylor@Sun.COM 			goto out;
8327405SEric.Taylor@Sun.COM 		}
8337405SEric.Taylor@Sun.COM 		zv->zv_flags |= ZVOL_EXCL;
8347405SEric.Taylor@Sun.COM 	}
835789Sahrens 
836789Sahrens 	if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) {
837789Sahrens 		zv->zv_open_count[otyp]++;
838789Sahrens 		zv->zv_total_opens++;
839789Sahrens 	}
84012527SChris.Kirby@oracle.com 	mutex_exit(&zfsdev_state_lock);
841789Sahrens 
84210588SEric.Taylor@Sun.COM 	return (err);
84310588SEric.Taylor@Sun.COM out:
84410588SEric.Taylor@Sun.COM 	if (zv->zv_total_opens == 0)
84510588SEric.Taylor@Sun.COM 		zvol_last_close(zv);
84612527SChris.Kirby@oracle.com 	mutex_exit(&zfsdev_state_lock);
84710588SEric.Taylor@Sun.COM 	return (err);
848789Sahrens }
849789Sahrens 
850789Sahrens /*ARGSUSED*/
851789Sahrens int
zvol_close(dev_t dev,int flag,int otyp,cred_t * cr)852789Sahrens zvol_close(dev_t dev, int flag, int otyp, cred_t *cr)
853789Sahrens {
854789Sahrens 	minor_t minor = getminor(dev);
855789Sahrens 	zvol_state_t *zv;
85610588SEric.Taylor@Sun.COM 	int error = 0;
857789Sahrens 
85812527SChris.Kirby@oracle.com 	mutex_enter(&zfsdev_state_lock);
859789Sahrens 
86012527SChris.Kirby@oracle.com 	zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
861789Sahrens 	if (zv == NULL) {
86212527SChris.Kirby@oracle.com 		mutex_exit(&zfsdev_state_lock);
863789Sahrens 		return (ENXIO);
864789Sahrens 	}
865789Sahrens 
8667405SEric.Taylor@Sun.COM 	if (zv->zv_flags & ZVOL_EXCL) {
8677405SEric.Taylor@Sun.COM 		ASSERT(zv->zv_total_opens == 1);
8687405SEric.Taylor@Sun.COM 		zv->zv_flags &= ~ZVOL_EXCL;
869789Sahrens 	}
870789Sahrens 
871789Sahrens 	/*
872789Sahrens 	 * If the open count is zero, this is a spurious close.
873789Sahrens 	 * That indicates a bug in the kernel / DDI framework.
874789Sahrens 	 */
875789Sahrens 	ASSERT(zv->zv_open_count[otyp] != 0);
876789Sahrens 	ASSERT(zv->zv_total_opens != 0);
877789Sahrens 
878789Sahrens 	/*
879789Sahrens 	 * You may get multiple opens, but only one close.
880789Sahrens 	 */
881789Sahrens 	zv->zv_open_count[otyp]--;
882789Sahrens 	zv->zv_total_opens--;
883789Sahrens 
88410588SEric.Taylor@Sun.COM 	if (zv->zv_total_opens == 0)
88510588SEric.Taylor@Sun.COM 		zvol_last_close(zv);
886789Sahrens 
88712527SChris.Kirby@oracle.com 	mutex_exit(&zfsdev_state_lock);
88810588SEric.Taylor@Sun.COM 	return (error);
889789Sahrens }
890789Sahrens 
8913638Sbillm static void
zvol_get_done(zgd_t * zgd,int error)89210922SJeff.Bonwick@Sun.COM zvol_get_done(zgd_t *zgd, int error)
8933063Sperrin {
89410922SJeff.Bonwick@Sun.COM 	if (zgd->zgd_db)
89510922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(zgd->zgd_db, zgd);
8963063Sperrin 
89710922SJeff.Bonwick@Sun.COM 	zfs_range_unlock(zgd->zgd_rl);
89810922SJeff.Bonwick@Sun.COM 
89910922SJeff.Bonwick@Sun.COM 	if (error == 0 && zgd->zgd_bp)
90010922SJeff.Bonwick@Sun.COM 		zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
90110922SJeff.Bonwick@Sun.COM 
9023063Sperrin 	kmem_free(zgd, sizeof (zgd_t));
9033063Sperrin }
9043063Sperrin 
9053063Sperrin /*
9063063Sperrin  * Get data to generate a TX_WRITE intent log record.
9073063Sperrin  */
9083638Sbillm static int
zvol_get_data(void * arg,lr_write_t * lr,char * buf,zio_t * zio)9093063Sperrin zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
9103063Sperrin {
9113063Sperrin 	zvol_state_t *zv = arg;
9123063Sperrin 	objset_t *os = zv->zv_objset;
91310922SJeff.Bonwick@Sun.COM 	uint64_t object = ZVOL_OBJ;
91410922SJeff.Bonwick@Sun.COM 	uint64_t offset = lr->lr_offset;
91510922SJeff.Bonwick@Sun.COM 	uint64_t size = lr->lr_length;	/* length of user data */
91610922SJeff.Bonwick@Sun.COM 	blkptr_t *bp = &lr->lr_blkptr;
9173063Sperrin 	dmu_buf_t *db;
9183063Sperrin 	zgd_t *zgd;
9193063Sperrin 	int error;
9203063Sperrin 
92110922SJeff.Bonwick@Sun.COM 	ASSERT(zio != NULL);
92210922SJeff.Bonwick@Sun.COM 	ASSERT(size != 0);
92310922SJeff.Bonwick@Sun.COM 
92410922SJeff.Bonwick@Sun.COM 	zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
92510922SJeff.Bonwick@Sun.COM 	zgd->zgd_zilog = zv->zv_zilog;
92610922SJeff.Bonwick@Sun.COM 	zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
9273638Sbillm 
9283755Sperrin 	/*
9293755Sperrin 	 * Write records come in two flavors: immediate and indirect.
9303755Sperrin 	 * For small writes it's cheaper to store the data with the
9313755Sperrin 	 * log record (immediate); for large writes it's cheaper to
9323755Sperrin 	 * sync the data and get a pointer to it (indirect) so that
9333755Sperrin 	 * we don't have to write the data twice.
9343755Sperrin 	 */
93510922SJeff.Bonwick@Sun.COM 	if (buf != NULL) {	/* immediate write */
93610922SJeff.Bonwick@Sun.COM 		error = dmu_read(os, object, offset, size, buf,
93710922SJeff.Bonwick@Sun.COM 		    DMU_READ_NO_PREFETCH);
93810922SJeff.Bonwick@Sun.COM 	} else {
93910922SJeff.Bonwick@Sun.COM 		size = zv->zv_volblocksize;
94010922SJeff.Bonwick@Sun.COM 		offset = P2ALIGN(offset, size);
94112285SJeff.Bonwick@Sun.COM 		error = dmu_buf_hold(os, object, offset, zgd, &db,
94212285SJeff.Bonwick@Sun.COM 		    DMU_READ_NO_PREFETCH);
94310922SJeff.Bonwick@Sun.COM 		if (error == 0) {
94410922SJeff.Bonwick@Sun.COM 			zgd->zgd_db = db;
94510922SJeff.Bonwick@Sun.COM 			zgd->zgd_bp = bp;
9463063Sperrin 
94710922SJeff.Bonwick@Sun.COM 			ASSERT(db->db_offset == offset);
94810922SJeff.Bonwick@Sun.COM 			ASSERT(db->db_size == size);
9493755Sperrin 
95010922SJeff.Bonwick@Sun.COM 			error = dmu_sync(zio, lr->lr_common.lrc_txg,
95110922SJeff.Bonwick@Sun.COM 			    zvol_get_done, zgd);
95210800SNeil.Perrin@Sun.COM 
95310922SJeff.Bonwick@Sun.COM 			if (error == 0)
95410922SJeff.Bonwick@Sun.COM 				return (0);
95510922SJeff.Bonwick@Sun.COM 		}
95610800SNeil.Perrin@Sun.COM 	}
95710800SNeil.Perrin@Sun.COM 
95810922SJeff.Bonwick@Sun.COM 	zvol_get_done(zgd, error);
95910922SJeff.Bonwick@Sun.COM 
9603063Sperrin 	return (error);
9613063Sperrin }
9623063Sperrin 
9631861Sperrin /*
9641861Sperrin  * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
9651141Sperrin  *
9661141Sperrin  * We store data in the log buffers if it's small enough.
9673063Sperrin  * Otherwise we will later flush the data out via dmu_sync().
9681141Sperrin  */
9693063Sperrin ssize_t zvol_immediate_write_sz = 32768;
9701141Sperrin 
9713638Sbillm static void
zvol_log_write(zvol_state_t * zv,dmu_tx_t * tx,offset_t off,ssize_t resid,boolean_t sync)9729401SNeil.Perrin@Sun.COM zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid,
9739401SNeil.Perrin@Sun.COM     boolean_t sync)
9741141Sperrin {
9753638Sbillm 	uint32_t blocksize = zv->zv_volblocksize;
9768227SNeil.Perrin@Sun.COM 	zilog_t *zilog = zv->zv_zilog;
9779401SNeil.Perrin@Sun.COM 	boolean_t slogging;
97810310SNeil.Perrin@Sun.COM 	ssize_t immediate_write_sz;
9799401SNeil.Perrin@Sun.COM 
98010922SJeff.Bonwick@Sun.COM 	if (zil_replaying(zilog, tx))
9818227SNeil.Perrin@Sun.COM 		return;
9828227SNeil.Perrin@Sun.COM 
98310310SNeil.Perrin@Sun.COM 	immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
98410310SNeil.Perrin@Sun.COM 	    ? 0 : zvol_immediate_write_sz;
98510310SNeil.Perrin@Sun.COM 
98610310SNeil.Perrin@Sun.COM 	slogging = spa_has_slogs(zilog->zl_spa) &&
98710310SNeil.Perrin@Sun.COM 	    (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
9889401SNeil.Perrin@Sun.COM 
9899401SNeil.Perrin@Sun.COM 	while (resid) {
9909401SNeil.Perrin@Sun.COM 		itx_t *itx;
9919401SNeil.Perrin@Sun.COM 		lr_write_t *lr;
9929401SNeil.Perrin@Sun.COM 		ssize_t len;
9939401SNeil.Perrin@Sun.COM 		itx_wr_state_t write_state;
9943638Sbillm 
9959401SNeil.Perrin@Sun.COM 		/*
9969401SNeil.Perrin@Sun.COM 		 * Unlike zfs_log_write() we can be called with
9979401SNeil.Perrin@Sun.COM 		 * upto DMU_MAX_ACCESS/2 (5MB) writes.
9989401SNeil.Perrin@Sun.COM 		 */
99910310SNeil.Perrin@Sun.COM 		if (blocksize > immediate_write_sz && !slogging &&
10009401SNeil.Perrin@Sun.COM 		    resid >= blocksize && off % blocksize == 0) {
10019401SNeil.Perrin@Sun.COM 			write_state = WR_INDIRECT; /* uses dmu_sync */
10029401SNeil.Perrin@Sun.COM 			len = blocksize;
10039401SNeil.Perrin@Sun.COM 		} else if (sync) {
10049401SNeil.Perrin@Sun.COM 			write_state = WR_COPIED;
10059401SNeil.Perrin@Sun.COM 			len = MIN(ZIL_MAX_LOG_DATA, resid);
10069401SNeil.Perrin@Sun.COM 		} else {
10079401SNeil.Perrin@Sun.COM 			write_state = WR_NEED_COPY;
10089401SNeil.Perrin@Sun.COM 			len = MIN(ZIL_MAX_LOG_DATA, resid);
10099401SNeil.Perrin@Sun.COM 		}
10109401SNeil.Perrin@Sun.COM 
10119401SNeil.Perrin@Sun.COM 		itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
10129401SNeil.Perrin@Sun.COM 		    (write_state == WR_COPIED ? len : 0));
10133638Sbillm 		lr = (lr_write_t *)&itx->itx_lr;
10149401SNeil.Perrin@Sun.COM 		if (write_state == WR_COPIED && dmu_read(zv->zv_objset,
10159512SNeil.Perrin@Sun.COM 		    ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
101610922SJeff.Bonwick@Sun.COM 			zil_itx_destroy(itx);
10179401SNeil.Perrin@Sun.COM 			itx = zil_itx_create(TX_WRITE, sizeof (*lr));
10189401SNeil.Perrin@Sun.COM 			lr = (lr_write_t *)&itx->itx_lr;
10199401SNeil.Perrin@Sun.COM 			write_state = WR_NEED_COPY;
10209401SNeil.Perrin@Sun.COM 		}
10219401SNeil.Perrin@Sun.COM 
10229401SNeil.Perrin@Sun.COM 		itx->itx_wr_state = write_state;
10239401SNeil.Perrin@Sun.COM 		if (write_state == WR_NEED_COPY)
10249401SNeil.Perrin@Sun.COM 			itx->itx_sod += len;
10253638Sbillm 		lr->lr_foid = ZVOL_OBJ;
10263638Sbillm 		lr->lr_offset = off;
10279401SNeil.Perrin@Sun.COM 		lr->lr_length = len;
102810922SJeff.Bonwick@Sun.COM 		lr->lr_blkoff = 0;
10293638Sbillm 		BP_ZERO(&lr->lr_blkptr);
10303638Sbillm 
10319401SNeil.Perrin@Sun.COM 		itx->itx_private = zv;
10329401SNeil.Perrin@Sun.COM 		itx->itx_sync = sync;
10339401SNeil.Perrin@Sun.COM 
103412699SNeil.Perrin@Sun.COM 		zil_itx_assign(zilog, itx, tx);
10359401SNeil.Perrin@Sun.COM 
10369401SNeil.Perrin@Sun.COM 		off += len;
10379401SNeil.Perrin@Sun.COM 		resid -= len;
10381141Sperrin 	}
10391141Sperrin }
10401141Sperrin 
10417837SMatthew.Ahrens@Sun.COM static int
zvol_dumpio_vdev(vdev_t * vd,void * addr,uint64_t offset,uint64_t size,boolean_t doread,boolean_t isdump)10427837SMatthew.Ahrens@Sun.COM zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t size,
10437837SMatthew.Ahrens@Sun.COM     boolean_t doread, boolean_t isdump)
10446423Sgw25295 {
10456423Sgw25295 	vdev_disk_t *dvd;
10466423Sgw25295 	int c;
10476423Sgw25295 	int numerrors = 0;
10486423Sgw25295 
10496423Sgw25295 	for (c = 0; c < vd->vdev_children; c++) {
10509790SLin.Ling@Sun.COM 		ASSERT(vd->vdev_ops == &vdev_mirror_ops ||
10519790SLin.Ling@Sun.COM 		    vd->vdev_ops == &vdev_replacing_ops ||
10529790SLin.Ling@Sun.COM 		    vd->vdev_ops == &vdev_spare_ops);
10537837SMatthew.Ahrens@Sun.COM 		int err = zvol_dumpio_vdev(vd->vdev_child[c],
10547837SMatthew.Ahrens@Sun.COM 		    addr, offset, size, doread, isdump);
10557837SMatthew.Ahrens@Sun.COM 		if (err != 0) {
10566423Sgw25295 			numerrors++;
10577837SMatthew.Ahrens@Sun.COM 		} else if (doread) {
10586423Sgw25295 			break;
10596423Sgw25295 		}
10606423Sgw25295 	}
10616423Sgw25295 
10626423Sgw25295 	if (!vd->vdev_ops->vdev_op_leaf)
10636423Sgw25295 		return (numerrors < vd->vdev_children ? 0 : EIO);
10646423Sgw25295 
10657903SEric.Taylor@Sun.COM 	if (doread && !vdev_readable(vd))
10667903SEric.Taylor@Sun.COM 		return (EIO);
10677903SEric.Taylor@Sun.COM 	else if (!doread && !vdev_writeable(vd))
10686423Sgw25295 		return (EIO);
10696423Sgw25295 
10706423Sgw25295 	dvd = vd->vdev_tsd;
10716423Sgw25295 	ASSERT3P(dvd, !=, NULL);
10726423Sgw25295 	offset += VDEV_LABEL_START_SIZE;
10736423Sgw25295 
10746423Sgw25295 	if (ddi_in_panic() || isdump) {
10757837SMatthew.Ahrens@Sun.COM 		ASSERT(!doread);
10767837SMatthew.Ahrens@Sun.COM 		if (doread)
10776423Sgw25295 			return (EIO);
10786423Sgw25295 		return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset),
10796423Sgw25295 		    lbtodb(size)));
10806423Sgw25295 	} else {
10816423Sgw25295 		return (vdev_disk_physio(dvd->vd_lh, addr, size, offset,
10827837SMatthew.Ahrens@Sun.COM 		    doread ? B_READ : B_WRITE));
10836423Sgw25295 	}
10846423Sgw25295 }
10856423Sgw25295 
10867837SMatthew.Ahrens@Sun.COM static int
zvol_dumpio(zvol_state_t * zv,void * addr,uint64_t offset,uint64_t size,boolean_t doread,boolean_t isdump)10877837SMatthew.Ahrens@Sun.COM zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size,
10887837SMatthew.Ahrens@Sun.COM     boolean_t doread, boolean_t isdump)
10896423Sgw25295 {
10906423Sgw25295 	vdev_t *vd;
10916423Sgw25295 	int error;
10927837SMatthew.Ahrens@Sun.COM 	zvol_extent_t *ze;
10936423Sgw25295 	spa_t *spa = dmu_objset_spa(zv->zv_objset);
10946423Sgw25295 
10957837SMatthew.Ahrens@Sun.COM 	/* Must be sector aligned, and not stradle a block boundary. */
10967837SMatthew.Ahrens@Sun.COM 	if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) ||
10977837SMatthew.Ahrens@Sun.COM 	    P2BOUNDARY(offset, size, zv->zv_volblocksize)) {
10987837SMatthew.Ahrens@Sun.COM 		return (EINVAL);
10997837SMatthew.Ahrens@Sun.COM 	}
11006423Sgw25295 	ASSERT(size <= zv->zv_volblocksize);
11016423Sgw25295 
11027837SMatthew.Ahrens@Sun.COM 	/* Locate the extent this belongs to */
11037837SMatthew.Ahrens@Sun.COM 	ze = list_head(&zv->zv_extents);
11047837SMatthew.Ahrens@Sun.COM 	while (offset >= ze->ze_nblks * zv->zv_volblocksize) {
11057837SMatthew.Ahrens@Sun.COM 		offset -= ze->ze_nblks * zv->zv_volblocksize;
11067837SMatthew.Ahrens@Sun.COM 		ze = list_next(&zv->zv_extents, ze);
11077837SMatthew.Ahrens@Sun.COM 	}
110811806SGeorge.Wilson@Sun.COM 
110911806SGeorge.Wilson@Sun.COM 	if (!ddi_in_panic())
111011806SGeorge.Wilson@Sun.COM 		spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
111111806SGeorge.Wilson@Sun.COM 
11127837SMatthew.Ahrens@Sun.COM 	vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva));
11137837SMatthew.Ahrens@Sun.COM 	offset += DVA_GET_OFFSET(&ze->ze_dva);
11147837SMatthew.Ahrens@Sun.COM 	error = zvol_dumpio_vdev(vd, addr, offset, size, doread, isdump);
111511806SGeorge.Wilson@Sun.COM 
111611806SGeorge.Wilson@Sun.COM 	if (!ddi_in_panic())
111711806SGeorge.Wilson@Sun.COM 		spa_config_exit(spa, SCL_STATE, FTAG);
111811806SGeorge.Wilson@Sun.COM 
11196423Sgw25295 	return (error);
11206423Sgw25295 }
11216423Sgw25295 
11226423Sgw25295 int
zvol_strategy(buf_t * bp)1123789Sahrens zvol_strategy(buf_t *bp)
1124789Sahrens {
112512527SChris.Kirby@oracle.com 	zfs_soft_state_t *zs = NULL;
112612527SChris.Kirby@oracle.com 	zvol_state_t *zv;
1127789Sahrens 	uint64_t off, volsize;
11287837SMatthew.Ahrens@Sun.COM 	size_t resid;
1129789Sahrens 	char *addr;
11301141Sperrin 	objset_t *os;
11313755Sperrin 	rl_t *rl;
1132789Sahrens 	int error = 0;
11337837SMatthew.Ahrens@Sun.COM 	boolean_t doread = bp->b_flags & B_READ;
113412095SChris.Kirby@sun.com 	boolean_t is_dump;
11359401SNeil.Perrin@Sun.COM 	boolean_t sync;
1136789Sahrens 
113712527SChris.Kirby@oracle.com 	if (getminor(bp->b_edev) == 0) {
113812527SChris.Kirby@oracle.com 		error = EINVAL;
113912527SChris.Kirby@oracle.com 	} else {
114012527SChris.Kirby@oracle.com 		zs = ddi_get_soft_state(zfsdev_state, getminor(bp->b_edev));
114112527SChris.Kirby@oracle.com 		if (zs == NULL)
114212527SChris.Kirby@oracle.com 			error = ENXIO;
114312527SChris.Kirby@oracle.com 		else if (zs->zss_type != ZSST_ZVOL)
114412527SChris.Kirby@oracle.com 			error = EINVAL;
114512527SChris.Kirby@oracle.com 	}
114612527SChris.Kirby@oracle.com 
114712527SChris.Kirby@oracle.com 	if (error) {
114812527SChris.Kirby@oracle.com 		bioerror(bp, error);
1149789Sahrens 		biodone(bp);
1150789Sahrens 		return (0);
1151789Sahrens 	}
1152789Sahrens 
115312527SChris.Kirby@oracle.com 	zv = zs->zss_data;
1154789Sahrens 
115510588SEric.Taylor@Sun.COM 	if (!(bp->b_flags & B_READ) && (zv->zv_flags & ZVOL_RDONLY)) {
1156789Sahrens 		bioerror(bp, EROFS);
1157789Sahrens 		biodone(bp);
1158789Sahrens 		return (0);
1159789Sahrens 	}
1160789Sahrens 
1161789Sahrens 	off = ldbtob(bp->b_blkno);
1162789Sahrens 	volsize = zv->zv_volsize;
1163789Sahrens 
11641141Sperrin 	os = zv->zv_objset;
11651141Sperrin 	ASSERT(os != NULL);
1166789Sahrens 
1167789Sahrens 	bp_mapin(bp);
1168789Sahrens 	addr = bp->b_un.b_addr;
1169789Sahrens 	resid = bp->b_bcount;
1170789Sahrens 
11717837SMatthew.Ahrens@Sun.COM 	if (resid > 0 && (off < 0 || off >= volsize)) {
11727837SMatthew.Ahrens@Sun.COM 		bioerror(bp, EIO);
11737837SMatthew.Ahrens@Sun.COM 		biodone(bp);
11747837SMatthew.Ahrens@Sun.COM 		return (0);
11757837SMatthew.Ahrens@Sun.COM 	}
11767013Sgw25295 
117712095SChris.Kirby@sun.com 	is_dump = zv->zv_flags & ZVOL_DUMPIFIED;
117812294SMark.Musante@Sun.COM 	sync = ((!(bp->b_flags & B_ASYNC) &&
117912294SMark.Musante@Sun.COM 	    !(zv->zv_flags & ZVOL_WCE)) ||
118012294SMark.Musante@Sun.COM 	    (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)) &&
118112294SMark.Musante@Sun.COM 	    !doread && !is_dump;
11829401SNeil.Perrin@Sun.COM 
11831861Sperrin 	/*
11841861Sperrin 	 * There must be no buffer changes when doing a dmu_sync() because
11851861Sperrin 	 * we can't change the data whilst calculating the checksum.
11861861Sperrin 	 */
11873755Sperrin 	rl = zfs_range_lock(&zv->zv_znode, off, resid,
11887837SMatthew.Ahrens@Sun.COM 	    doread ? RL_READER : RL_WRITER);
11896423Sgw25295 
1190789Sahrens 	while (resid != 0 && off < volsize) {
11917837SMatthew.Ahrens@Sun.COM 		size_t size = MIN(resid, zvol_maxphys);
11926423Sgw25295 		if (is_dump) {
11936423Sgw25295 			size = MIN(size, P2END(off, zv->zv_volblocksize) - off);
11947837SMatthew.Ahrens@Sun.COM 			error = zvol_dumpio(zv, addr, off, size,
11957837SMatthew.Ahrens@Sun.COM 			    doread, B_FALSE);
11967837SMatthew.Ahrens@Sun.COM 		} else if (doread) {
11979512SNeil.Perrin@Sun.COM 			error = dmu_read(os, ZVOL_OBJ, off, size, addr,
11989512SNeil.Perrin@Sun.COM 			    DMU_READ_PREFETCH);
1199789Sahrens 		} else {
12001141Sperrin 			dmu_tx_t *tx = dmu_tx_create(os);
1201789Sahrens 			dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
1202789Sahrens 			error = dmu_tx_assign(tx, TXG_WAIT);
1203789Sahrens 			if (error) {
1204789Sahrens 				dmu_tx_abort(tx);
1205789Sahrens 			} else {
12061141Sperrin 				dmu_write(os, ZVOL_OBJ, off, size, addr, tx);
12079401SNeil.Perrin@Sun.COM 				zvol_log_write(zv, tx, off, size, sync);
1208789Sahrens 				dmu_tx_commit(tx);
1209789Sahrens 			}
1210789Sahrens 		}
12117294Sperrin 		if (error) {
12127294Sperrin 			/* convert checksum errors into IO errors */
12137294Sperrin 			if (error == ECKSUM)
12147294Sperrin 				error = EIO;
1215789Sahrens 			break;
12167294Sperrin 		}
1217789Sahrens 		off += size;
1218789Sahrens 		addr += size;
1219789Sahrens 		resid -= size;
1220789Sahrens 	}
12213755Sperrin 	zfs_range_unlock(rl);
1222789Sahrens 
1223789Sahrens 	if ((bp->b_resid = resid) == bp->b_bcount)
1224789Sahrens 		bioerror(bp, off > volsize ? EINVAL : error);
1225789Sahrens 
12269401SNeil.Perrin@Sun.COM 	if (sync)
122712699SNeil.Perrin@Sun.COM 		zil_commit(zv->zv_zilog, ZVOL_OBJ);
12283638Sbillm 	biodone(bp);
12291141Sperrin 
1230789Sahrens 	return (0);
1231789Sahrens }
1232789Sahrens 
12333063Sperrin /*
12343063Sperrin  * Set the buffer count to the zvol maximum transfer.
12353063Sperrin  * Using our own routine instead of the default minphys()
12363063Sperrin  * means that for larger writes we write bigger buffers on X86
12373063Sperrin  * (128K instead of 56K) and flush the disk write cache less often
12383063Sperrin  * (every zvol_maxphys - currently 1MB) instead of minphys (currently
12393063Sperrin  * 56K on X86 and 128K on sparc).
12403063Sperrin  */
12413063Sperrin void
zvol_minphys(struct buf * bp)12423063Sperrin zvol_minphys(struct buf *bp)
12433063Sperrin {
12443063Sperrin 	if (bp->b_bcount > zvol_maxphys)
12453063Sperrin 		bp->b_bcount = zvol_maxphys;
12463063Sperrin }
12473063Sperrin 
12486423Sgw25295 int
zvol_dump(dev_t dev,caddr_t addr,daddr_t blkno,int nblocks)12496423Sgw25295 zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks)
12506423Sgw25295 {
12516423Sgw25295 	minor_t minor = getminor(dev);
12526423Sgw25295 	zvol_state_t *zv;
12536423Sgw25295 	int error = 0;
12546423Sgw25295 	uint64_t size;
12556423Sgw25295 	uint64_t boff;
12566423Sgw25295 	uint64_t resid;
12576423Sgw25295 
125812527SChris.Kirby@oracle.com 	zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
12596423Sgw25295 	if (zv == NULL)
12606423Sgw25295 		return (ENXIO);
12616423Sgw25295 
12626423Sgw25295 	boff = ldbtob(blkno);
12636423Sgw25295 	resid = ldbtob(nblocks);
12647837SMatthew.Ahrens@Sun.COM 
12657837SMatthew.Ahrens@Sun.COM 	VERIFY3U(boff + resid, <=, zv->zv_volsize);
12667837SMatthew.Ahrens@Sun.COM 
12676423Sgw25295 	while (resid) {
12686423Sgw25295 		size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff);
12697837SMatthew.Ahrens@Sun.COM 		error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE);
12706423Sgw25295 		if (error)
12716423Sgw25295 			break;
12726423Sgw25295 		boff += size;
12736423Sgw25295 		addr += size;
12746423Sgw25295 		resid -= size;
12756423Sgw25295 	}
12766423Sgw25295 
12776423Sgw25295 	return (error);
12786423Sgw25295 }
12796423Sgw25295 
1280789Sahrens /*ARGSUSED*/
1281789Sahrens int
zvol_read(dev_t dev,uio_t * uio,cred_t * cr)12823638Sbillm zvol_read(dev_t dev, uio_t *uio, cred_t *cr)
1283789Sahrens {
12844107Sgw25295 	minor_t minor = getminor(dev);
12854107Sgw25295 	zvol_state_t *zv;
12867013Sgw25295 	uint64_t volsize;
12873755Sperrin 	rl_t *rl;
12883638Sbillm 	int error = 0;
12893638Sbillm 
129012527SChris.Kirby@oracle.com 	zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
12914107Sgw25295 	if (zv == NULL)
12924107Sgw25295 		return (ENXIO);
12934107Sgw25295 
12947013Sgw25295 	volsize = zv->zv_volsize;
12957013Sgw25295 	if (uio->uio_resid > 0 &&
12967013Sgw25295 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
12977013Sgw25295 		return (EIO);
12987013Sgw25295 
12997837SMatthew.Ahrens@Sun.COM 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
13007837SMatthew.Ahrens@Sun.COM 		error = physio(zvol_strategy, NULL, dev, B_READ,
13017837SMatthew.Ahrens@Sun.COM 		    zvol_minphys, uio);
13027837SMatthew.Ahrens@Sun.COM 		return (error);
13037837SMatthew.Ahrens@Sun.COM 	}
13047837SMatthew.Ahrens@Sun.COM 
13053755Sperrin 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
13063755Sperrin 	    RL_READER);
13077013Sgw25295 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
13083638Sbillm 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
13093638Sbillm 
13107013Sgw25295 		/* don't read past the end */
13117013Sgw25295 		if (bytes > volsize - uio->uio_loffset)
13127013Sgw25295 			bytes = volsize - uio->uio_loffset;
13137013Sgw25295 
13143638Sbillm 		error =  dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes);
13157294Sperrin 		if (error) {
13167294Sperrin 			/* convert checksum errors into IO errors */
13177294Sperrin 			if (error == ECKSUM)
13187294Sperrin 				error = EIO;
13193638Sbillm 			break;
13207294Sperrin 		}
13213638Sbillm 	}
13223755Sperrin 	zfs_range_unlock(rl);
13233638Sbillm 	return (error);
1324789Sahrens }
1325789Sahrens 
1326789Sahrens /*ARGSUSED*/
1327789Sahrens int
zvol_write(dev_t dev,uio_t * uio,cred_t * cr)13283638Sbillm zvol_write(dev_t dev, uio_t *uio, cred_t *cr)
1329789Sahrens {
13304107Sgw25295 	minor_t minor = getminor(dev);
13314107Sgw25295 	zvol_state_t *zv;
13327013Sgw25295 	uint64_t volsize;
13333755Sperrin 	rl_t *rl;
13343638Sbillm 	int error = 0;
13359401SNeil.Perrin@Sun.COM 	boolean_t sync;
13363638Sbillm 
133712527SChris.Kirby@oracle.com 	zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
13384107Sgw25295 	if (zv == NULL)
13394107Sgw25295 		return (ENXIO);
13404107Sgw25295 
13417013Sgw25295 	volsize = zv->zv_volsize;
13427013Sgw25295 	if (uio->uio_resid > 0 &&
13437013Sgw25295 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
13447013Sgw25295 		return (EIO);
13457013Sgw25295 
13466423Sgw25295 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
13476423Sgw25295 		error = physio(zvol_strategy, NULL, dev, B_WRITE,
13486423Sgw25295 		    zvol_minphys, uio);
13496423Sgw25295 		return (error);
13506423Sgw25295 	}
13516423Sgw25295 
135212294SMark.Musante@Sun.COM 	sync = !(zv->zv_flags & ZVOL_WCE) ||
135312294SMark.Musante@Sun.COM 	    (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS);
13549401SNeil.Perrin@Sun.COM 
13553755Sperrin 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
13563755Sperrin 	    RL_WRITER);
13577013Sgw25295 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
13583638Sbillm 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
13593638Sbillm 		uint64_t off = uio->uio_loffset;
13607013Sgw25295 		dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
1361789Sahrens 
13627013Sgw25295 		if (bytes > volsize - off)	/* don't write past the end */
13637013Sgw25295 			bytes = volsize - off;
13647013Sgw25295 
13653638Sbillm 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
13663638Sbillm 		error = dmu_tx_assign(tx, TXG_WAIT);
13673638Sbillm 		if (error) {
13683638Sbillm 			dmu_tx_abort(tx);
13693638Sbillm 			break;
13703638Sbillm 		}
137112123STim.Haley@Sun.COM 		error = dmu_write_uio_dbuf(zv->zv_dbuf, uio, bytes, tx);
13723638Sbillm 		if (error == 0)
13739401SNeil.Perrin@Sun.COM 			zvol_log_write(zv, tx, off, bytes, sync);
13743638Sbillm 		dmu_tx_commit(tx);
13753638Sbillm 
13763638Sbillm 		if (error)
13773638Sbillm 			break;
13783638Sbillm 	}
13793755Sperrin 	zfs_range_unlock(rl);
13809401SNeil.Perrin@Sun.COM 	if (sync)
138112699SNeil.Perrin@Sun.COM 		zil_commit(zv->zv_zilog, ZVOL_OBJ);
13823638Sbillm 	return (error);
1383789Sahrens }
1384789Sahrens 
13857405SEric.Taylor@Sun.COM int
zvol_getefi(void * arg,int flag,uint64_t vs,uint8_t bs)13867405SEric.Taylor@Sun.COM zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs)
13877405SEric.Taylor@Sun.COM {
13887405SEric.Taylor@Sun.COM 	struct uuid uuid = EFI_RESERVED;
13897405SEric.Taylor@Sun.COM 	efi_gpe_t gpe = { 0 };
13907405SEric.Taylor@Sun.COM 	uint32_t crc;
13917405SEric.Taylor@Sun.COM 	dk_efi_t efi;
13927405SEric.Taylor@Sun.COM 	int length;
13937405SEric.Taylor@Sun.COM 	char *ptr;
13947405SEric.Taylor@Sun.COM 
13957405SEric.Taylor@Sun.COM 	if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag))
13967405SEric.Taylor@Sun.COM 		return (EFAULT);
13977405SEric.Taylor@Sun.COM 	ptr = (char *)(uintptr_t)efi.dki_data_64;
13987405SEric.Taylor@Sun.COM 	length = efi.dki_length;
13997405SEric.Taylor@Sun.COM 	/*
14007405SEric.Taylor@Sun.COM 	 * Some clients may attempt to request a PMBR for the
14017405SEric.Taylor@Sun.COM 	 * zvol.  Currently this interface will return EINVAL to
14027405SEric.Taylor@Sun.COM 	 * such requests.  These requests could be supported by
14037405SEric.Taylor@Sun.COM 	 * adding a check for lba == 0 and consing up an appropriate
14047405SEric.Taylor@Sun.COM 	 * PMBR.
14057405SEric.Taylor@Sun.COM 	 */
14067405SEric.Taylor@Sun.COM 	if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0)
14077405SEric.Taylor@Sun.COM 		return (EINVAL);
14087405SEric.Taylor@Sun.COM 
14097405SEric.Taylor@Sun.COM 	gpe.efi_gpe_StartingLBA = LE_64(34ULL);
14107405SEric.Taylor@Sun.COM 	gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1);
14117405SEric.Taylor@Sun.COM 	UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
14127405SEric.Taylor@Sun.COM 
14137405SEric.Taylor@Sun.COM 	if (efi.dki_lba == 1) {
14147405SEric.Taylor@Sun.COM 		efi_gpt_t gpt = { 0 };
14157405SEric.Taylor@Sun.COM 
14167405SEric.Taylor@Sun.COM 		gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
14177405SEric.Taylor@Sun.COM 		gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
14187405SEric.Taylor@Sun.COM 		gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt));
14197405SEric.Taylor@Sun.COM 		gpt.efi_gpt_MyLBA = LE_64(1ULL);
14207405SEric.Taylor@Sun.COM 		gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL);
14217405SEric.Taylor@Sun.COM 		gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1);
14227405SEric.Taylor@Sun.COM 		gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL);
14237405SEric.Taylor@Sun.COM 		gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
14247405SEric.Taylor@Sun.COM 		gpt.efi_gpt_SizeOfPartitionEntry =
14257405SEric.Taylor@Sun.COM 		    LE_32(sizeof (efi_gpe_t));
14267405SEric.Taylor@Sun.COM 		CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
14277405SEric.Taylor@Sun.COM 		gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
14287405SEric.Taylor@Sun.COM 		CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table);
14297405SEric.Taylor@Sun.COM 		gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
14307405SEric.Taylor@Sun.COM 		if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length),
14317405SEric.Taylor@Sun.COM 		    flag))
14327405SEric.Taylor@Sun.COM 			return (EFAULT);
14337405SEric.Taylor@Sun.COM 		ptr += sizeof (gpt);
14347405SEric.Taylor@Sun.COM 		length -= sizeof (gpt);
14357405SEric.Taylor@Sun.COM 	}
14367405SEric.Taylor@Sun.COM 	if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe),
14377405SEric.Taylor@Sun.COM 	    length), flag))
14387405SEric.Taylor@Sun.COM 		return (EFAULT);
14397405SEric.Taylor@Sun.COM 	return (0);
14407405SEric.Taylor@Sun.COM }
14417405SEric.Taylor@Sun.COM 
1442789Sahrens /*
144312314SJames.Moore@Sun.COM  * BEGIN entry points to allow external callers access to the volume.
144412314SJames.Moore@Sun.COM  */
144512314SJames.Moore@Sun.COM /*
144612314SJames.Moore@Sun.COM  * Return the volume parameters needed for access from an external caller.
144712314SJames.Moore@Sun.COM  * These values are invariant as long as the volume is held open.
144812314SJames.Moore@Sun.COM  */
144912314SJames.Moore@Sun.COM int
zvol_get_volume_params(minor_t minor,uint64_t * blksize,uint64_t * max_xfer_len,void ** minor_hdl,void ** objset_hdl,void ** zil_hdl,void ** rl_hdl,void ** bonus_hdl)145012314SJames.Moore@Sun.COM zvol_get_volume_params(minor_t minor, uint64_t *blksize,
145112314SJames.Moore@Sun.COM     uint64_t *max_xfer_len, void **minor_hdl, void **objset_hdl, void **zil_hdl,
145212314SJames.Moore@Sun.COM     void **rl_hdl, void **bonus_hdl)
145312314SJames.Moore@Sun.COM {
145412314SJames.Moore@Sun.COM 	zvol_state_t *zv;
145512314SJames.Moore@Sun.COM 
145612527SChris.Kirby@oracle.com 	zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
145712527SChris.Kirby@oracle.com 	if (zv == NULL)
145812314SJames.Moore@Sun.COM 		return (ENXIO);
145912314SJames.Moore@Sun.COM 	if (zv->zv_flags & ZVOL_DUMPIFIED)
146012314SJames.Moore@Sun.COM 		return (ENXIO);
146112314SJames.Moore@Sun.COM 
146212314SJames.Moore@Sun.COM 	ASSERT(blksize && max_xfer_len && minor_hdl &&
146312314SJames.Moore@Sun.COM 	    objset_hdl && zil_hdl && rl_hdl && bonus_hdl);
146412314SJames.Moore@Sun.COM 
146512314SJames.Moore@Sun.COM 	*blksize = zv->zv_volblocksize;
146612314SJames.Moore@Sun.COM 	*max_xfer_len = (uint64_t)zvol_maxphys;
146712314SJames.Moore@Sun.COM 	*minor_hdl = zv;
146812314SJames.Moore@Sun.COM 	*objset_hdl = zv->zv_objset;
146912314SJames.Moore@Sun.COM 	*zil_hdl = zv->zv_zilog;
147012314SJames.Moore@Sun.COM 	*rl_hdl = &zv->zv_znode;
147112314SJames.Moore@Sun.COM 	*bonus_hdl = zv->zv_dbuf;
147212314SJames.Moore@Sun.COM 	return (0);
147312314SJames.Moore@Sun.COM }
147412314SJames.Moore@Sun.COM 
147512314SJames.Moore@Sun.COM /*
147612314SJames.Moore@Sun.COM  * Return the current volume size to an external caller.
147712314SJames.Moore@Sun.COM  * The size can change while the volume is open.
147812314SJames.Moore@Sun.COM  */
147912314SJames.Moore@Sun.COM uint64_t
zvol_get_volume_size(void * minor_hdl)148012314SJames.Moore@Sun.COM zvol_get_volume_size(void *minor_hdl)
148112314SJames.Moore@Sun.COM {
148212314SJames.Moore@Sun.COM 	zvol_state_t *zv = minor_hdl;
148312314SJames.Moore@Sun.COM 
148412314SJames.Moore@Sun.COM 	return (zv->zv_volsize);
148512314SJames.Moore@Sun.COM }
148612314SJames.Moore@Sun.COM 
148712314SJames.Moore@Sun.COM /*
148812314SJames.Moore@Sun.COM  * Return the current WCE setting to an external caller.
148912314SJames.Moore@Sun.COM  * The WCE setting can change while the volume is open.
149012314SJames.Moore@Sun.COM  */
149112314SJames.Moore@Sun.COM int
zvol_get_volume_wce(void * minor_hdl)149212314SJames.Moore@Sun.COM zvol_get_volume_wce(void *minor_hdl)
149312314SJames.Moore@Sun.COM {
149412314SJames.Moore@Sun.COM 	zvol_state_t *zv = minor_hdl;
149512314SJames.Moore@Sun.COM 
149612314SJames.Moore@Sun.COM 	return ((zv->zv_flags & ZVOL_WCE) ? 1 : 0);
149712314SJames.Moore@Sun.COM }
149812314SJames.Moore@Sun.COM 
149912314SJames.Moore@Sun.COM /*
150012314SJames.Moore@Sun.COM  * Entry point for external callers to zvol_log_write
150112314SJames.Moore@Sun.COM  */
150212314SJames.Moore@Sun.COM void
zvol_log_write_minor(void * minor_hdl,dmu_tx_t * tx,offset_t off,ssize_t resid,boolean_t sync)150312314SJames.Moore@Sun.COM zvol_log_write_minor(void *minor_hdl, dmu_tx_t *tx, offset_t off, ssize_t resid,
150412314SJames.Moore@Sun.COM     boolean_t sync)
150512314SJames.Moore@Sun.COM {
150612314SJames.Moore@Sun.COM 	zvol_state_t *zv = minor_hdl;
150712314SJames.Moore@Sun.COM 
150812314SJames.Moore@Sun.COM 	zvol_log_write(zv, tx, off, resid, sync);
150912314SJames.Moore@Sun.COM }
151012314SJames.Moore@Sun.COM /*
151112314SJames.Moore@Sun.COM  * END entry points to allow external callers access to the volume.
151212314SJames.Moore@Sun.COM  */
151312314SJames.Moore@Sun.COM 
151412314SJames.Moore@Sun.COM /*
1515789Sahrens  * Dirtbag ioctls to support mkfs(1M) for UFS filesystems.  See dkio(7I).
1516789Sahrens  */
1517789Sahrens /*ARGSUSED*/
1518789Sahrens int
zvol_ioctl(dev_t dev,int cmd,intptr_t arg,int flag,cred_t * cr,int * rvalp)1519789Sahrens zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
1520789Sahrens {
1521789Sahrens 	zvol_state_t *zv;
15223897Smaybee 	struct dk_cinfo dki;
1523789Sahrens 	struct dk_minfo dkm;
15243897Smaybee 	struct dk_callback *dkc;
1525789Sahrens 	int error = 0;
15266423Sgw25295 	rl_t *rl;
1527789Sahrens 
152812527SChris.Kirby@oracle.com 	mutex_enter(&zfsdev_state_lock);
1529789Sahrens 
153012527SChris.Kirby@oracle.com 	zv = zfsdev_get_soft_state(getminor(dev), ZSST_ZVOL);
1531789Sahrens 
1532789Sahrens 	if (zv == NULL) {
153312527SChris.Kirby@oracle.com 		mutex_exit(&zfsdev_state_lock);
1534789Sahrens 		return (ENXIO);
1535789Sahrens 	}
15369303SEric.Taylor@Sun.COM 	ASSERT(zv->zv_total_opens > 0);
1537789Sahrens 
1538789Sahrens 	switch (cmd) {
1539789Sahrens 
1540789Sahrens 	case DKIOCINFO:
15413897Smaybee 		bzero(&dki, sizeof (dki));
15423897Smaybee 		(void) strcpy(dki.dki_cname, "zvol");
15433897Smaybee 		(void) strcpy(dki.dki_dname, "zvol");
15443897Smaybee 		dki.dki_ctype = DKC_UNKNOWN;
154511689SEric.Taylor@Sun.COM 		dki.dki_unit = getminor(dev);
15463897Smaybee 		dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs);
154712527SChris.Kirby@oracle.com 		mutex_exit(&zfsdev_state_lock);
15483897Smaybee 		if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag))
1549789Sahrens 			error = EFAULT;
1550789Sahrens 		return (error);
1551789Sahrens 
1552789Sahrens 	case DKIOCGMEDIAINFO:
1553789Sahrens 		bzero(&dkm, sizeof (dkm));
1554789Sahrens 		dkm.dki_lbsize = 1U << zv->zv_min_bs;
1555789Sahrens 		dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
1556789Sahrens 		dkm.dki_media_type = DK_UNKNOWN;
155712527SChris.Kirby@oracle.com 		mutex_exit(&zfsdev_state_lock);
1558789Sahrens 		if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
1559789Sahrens 			error = EFAULT;
1560789Sahrens 		return (error);
1561789Sahrens 
1562789Sahrens 	case DKIOCGETEFI:
15637405SEric.Taylor@Sun.COM 		{
15647405SEric.Taylor@Sun.COM 			uint64_t vs = zv->zv_volsize;
15657405SEric.Taylor@Sun.COM 			uint8_t bs = zv->zv_min_bs;
15663016Smaybee 
156712527SChris.Kirby@oracle.com 			mutex_exit(&zfsdev_state_lock);
15687405SEric.Taylor@Sun.COM 			error = zvol_getefi((void *)arg, flag, vs, bs);
15697405SEric.Taylor@Sun.COM 			return (error);
15703016Smaybee 		}
1571789Sahrens 
15723638Sbillm 	case DKIOCFLUSHWRITECACHE:
15733897Smaybee 		dkc = (struct dk_callback *)arg;
157412527SChris.Kirby@oracle.com 		mutex_exit(&zfsdev_state_lock);
157512699SNeil.Perrin@Sun.COM 		zil_commit(zv->zv_zilog, ZVOL_OBJ);
15763897Smaybee 		if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) {
15773897Smaybee 			(*dkc->dkc_callback)(dkc->dkc_cookie, error);
15783897Smaybee 			error = 0;
15793897Smaybee 		}
15809303SEric.Taylor@Sun.COM 		return (error);
15819303SEric.Taylor@Sun.COM 
15829303SEric.Taylor@Sun.COM 	case DKIOCGETWCE:
15839303SEric.Taylor@Sun.COM 		{
15849303SEric.Taylor@Sun.COM 			int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0;
15859303SEric.Taylor@Sun.COM 			if (ddi_copyout(&wce, (void *)arg, sizeof (int),
15869303SEric.Taylor@Sun.COM 			    flag))
15879303SEric.Taylor@Sun.COM 				error = EFAULT;
15889303SEric.Taylor@Sun.COM 			break;
15899303SEric.Taylor@Sun.COM 		}
15909303SEric.Taylor@Sun.COM 	case DKIOCSETWCE:
15919303SEric.Taylor@Sun.COM 		{
15929303SEric.Taylor@Sun.COM 			int wce;
15939303SEric.Taylor@Sun.COM 			if (ddi_copyin((void *)arg, &wce, sizeof (int),
15949303SEric.Taylor@Sun.COM 			    flag)) {
15959303SEric.Taylor@Sun.COM 				error = EFAULT;
15969303SEric.Taylor@Sun.COM 				break;
15979303SEric.Taylor@Sun.COM 			}
15989303SEric.Taylor@Sun.COM 			if (wce) {
15999303SEric.Taylor@Sun.COM 				zv->zv_flags |= ZVOL_WCE;
160012527SChris.Kirby@oracle.com 				mutex_exit(&zfsdev_state_lock);
16019303SEric.Taylor@Sun.COM 			} else {
16029303SEric.Taylor@Sun.COM 				zv->zv_flags &= ~ZVOL_WCE;
160312527SChris.Kirby@oracle.com 				mutex_exit(&zfsdev_state_lock);
160412699SNeil.Perrin@Sun.COM 				zil_commit(zv->zv_zilog, ZVOL_OBJ);
16059303SEric.Taylor@Sun.COM 			}
16069303SEric.Taylor@Sun.COM 			return (0);
16079303SEric.Taylor@Sun.COM 		}
16083638Sbillm 
16093245Smaybee 	case DKIOCGGEOM:
16103245Smaybee 	case DKIOCGVTOC:
16116423Sgw25295 		/*
16126423Sgw25295 		 * commands using these (like prtvtoc) expect ENOTSUP
16136423Sgw25295 		 * since we're emulating an EFI label
16146423Sgw25295 		 */
16153245Smaybee 		error = ENOTSUP;
16163245Smaybee 		break;
16173245Smaybee 
16186423Sgw25295 	case DKIOCDUMPINIT:
16196423Sgw25295 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
16206423Sgw25295 		    RL_WRITER);
16216423Sgw25295 		error = zvol_dumpify(zv);
16226423Sgw25295 		zfs_range_unlock(rl);
16236423Sgw25295 		break;
16246423Sgw25295 
16256423Sgw25295 	case DKIOCDUMPFINI:
16269277SEric.Taylor@Sun.COM 		if (!(zv->zv_flags & ZVOL_DUMPIFIED))
16279277SEric.Taylor@Sun.COM 			break;
16286423Sgw25295 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
16296423Sgw25295 		    RL_WRITER);
16306423Sgw25295 		error = zvol_dump_fini(zv);
16316423Sgw25295 		zfs_range_unlock(rl);
16326423Sgw25295 		break;
16336423Sgw25295 
1634789Sahrens 	default:
16353016Smaybee 		error = ENOTTY;
1636789Sahrens 		break;
1637789Sahrens 
1638789Sahrens 	}
163912527SChris.Kirby@oracle.com 	mutex_exit(&zfsdev_state_lock);
1640789Sahrens 	return (error);
1641789Sahrens }
1642789Sahrens 
1643789Sahrens int
zvol_busy(void)1644789Sahrens zvol_busy(void)
1645789Sahrens {
1646789Sahrens 	return (zvol_minors != 0);
1647789Sahrens }
1648789Sahrens 
1649789Sahrens void
zvol_init(void)1650789Sahrens zvol_init(void)
1651789Sahrens {
165212527SChris.Kirby@oracle.com 	VERIFY(ddi_soft_state_init(&zfsdev_state, sizeof (zfs_soft_state_t),
165312527SChris.Kirby@oracle.com 	    1) == 0);
165412527SChris.Kirby@oracle.com 	mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL);
1655789Sahrens }
1656789Sahrens 
1657789Sahrens void
zvol_fini(void)1658789Sahrens zvol_fini(void)
1659789Sahrens {
166012527SChris.Kirby@oracle.com 	mutex_destroy(&zfsdev_state_lock);
166112527SChris.Kirby@oracle.com 	ddi_soft_state_fini(&zfsdev_state);
1662789Sahrens }
16636423Sgw25295 
16646423Sgw25295 static int
zvol_dump_init(zvol_state_t * zv,boolean_t resize)16656423Sgw25295 zvol_dump_init(zvol_state_t *zv, boolean_t resize)
16666423Sgw25295 {
16676423Sgw25295 	dmu_tx_t *tx;
16686423Sgw25295 	int error = 0;
16696423Sgw25295 	objset_t *os = zv->zv_objset;
16706423Sgw25295 	nvlist_t *nv = NULL;
167111656SGeorge.Wilson@Sun.COM 	uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset));
16726423Sgw25295 
167312527SChris.Kirby@oracle.com 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
167410588SEric.Taylor@Sun.COM 	error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0,
167510588SEric.Taylor@Sun.COM 	    DMU_OBJECT_END);
167610588SEric.Taylor@Sun.COM 	/* wait for dmu_free_long_range to actually free the blocks */
167710588SEric.Taylor@Sun.COM 	txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
16786423Sgw25295 
16796423Sgw25295 	tx = dmu_tx_create(os);
16806423Sgw25295 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
168110588SEric.Taylor@Sun.COM 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
16826423Sgw25295 	error = dmu_tx_assign(tx, TXG_WAIT);
16836423Sgw25295 	if (error) {
16846423Sgw25295 		dmu_tx_abort(tx);
16856423Sgw25295 		return (error);
16866423Sgw25295 	}
16876423Sgw25295 
16886423Sgw25295 	/*
16896423Sgw25295 	 * If we are resizing the dump device then we only need to
16906423Sgw25295 	 * update the refreservation to match the newly updated
16916423Sgw25295 	 * zvolsize. Otherwise, we save off the original state of the
16926423Sgw25295 	 * zvol so that we can restore them if the zvol is ever undumpified.
16936423Sgw25295 	 */
16946423Sgw25295 	if (resize) {
16956423Sgw25295 		error = zap_update(os, ZVOL_ZAP_OBJ,
16966423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
16976423Sgw25295 		    &zv->zv_volsize, tx);
16986423Sgw25295 	} else {
169911619SGeorge.Wilson@Sun.COM 		uint64_t checksum, compress, refresrv, vbs, dedup;
17007837SMatthew.Ahrens@Sun.COM 
17016423Sgw25295 		error = dsl_prop_get_integer(zv->zv_name,
17026423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL);
17036423Sgw25295 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
17046423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL);
17056423Sgw25295 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
17066423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL);
17077837SMatthew.Ahrens@Sun.COM 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
17087837SMatthew.Ahrens@Sun.COM 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL);
170911656SGeorge.Wilson@Sun.COM 		if (version >= SPA_VERSION_DEDUP) {
171011656SGeorge.Wilson@Sun.COM 			error = error ? error :
171111656SGeorge.Wilson@Sun.COM 			    dsl_prop_get_integer(zv->zv_name,
171211656SGeorge.Wilson@Sun.COM 			    zfs_prop_to_name(ZFS_PROP_DEDUP), &dedup, NULL);
171311656SGeorge.Wilson@Sun.COM 		}
17146423Sgw25295 
17156423Sgw25295 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
17166423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1,
17176423Sgw25295 		    &compress, tx);
17186423Sgw25295 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
17196423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx);
17206423Sgw25295 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
17216423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
17226423Sgw25295 		    &refresrv, tx);
17237837SMatthew.Ahrens@Sun.COM 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
17247837SMatthew.Ahrens@Sun.COM 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1,
17257837SMatthew.Ahrens@Sun.COM 		    &vbs, tx);
172610588SEric.Taylor@Sun.COM 		error = error ? error : dmu_object_set_blocksize(
172710588SEric.Taylor@Sun.COM 		    os, ZVOL_OBJ, SPA_MAXBLOCKSIZE, 0, tx);
172811656SGeorge.Wilson@Sun.COM 		if (version >= SPA_VERSION_DEDUP) {
172911656SGeorge.Wilson@Sun.COM 			error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
173011656SGeorge.Wilson@Sun.COM 			    zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1,
173111656SGeorge.Wilson@Sun.COM 			    &dedup, tx);
173211656SGeorge.Wilson@Sun.COM 		}
173310588SEric.Taylor@Sun.COM 		if (error == 0)
173410588SEric.Taylor@Sun.COM 			zv->zv_volblocksize = SPA_MAXBLOCKSIZE;
17356423Sgw25295 	}
17366423Sgw25295 	dmu_tx_commit(tx);
17376423Sgw25295 
17386423Sgw25295 	/*
17396423Sgw25295 	 * We only need update the zvol's property if we are initializing
17406423Sgw25295 	 * the dump area for the first time.
17416423Sgw25295 	 */
17426423Sgw25295 	if (!resize) {
17436423Sgw25295 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
17446423Sgw25295 		VERIFY(nvlist_add_uint64(nv,
17456423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0);
17466423Sgw25295 		VERIFY(nvlist_add_uint64(nv,
17476423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
17486423Sgw25295 		    ZIO_COMPRESS_OFF) == 0);
17496423Sgw25295 		VERIFY(nvlist_add_uint64(nv,
17506423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
17516423Sgw25295 		    ZIO_CHECKSUM_OFF) == 0);
175211656SGeorge.Wilson@Sun.COM 		if (version >= SPA_VERSION_DEDUP) {
175311656SGeorge.Wilson@Sun.COM 			VERIFY(nvlist_add_uint64(nv,
175411656SGeorge.Wilson@Sun.COM 			    zfs_prop_to_name(ZFS_PROP_DEDUP),
175511656SGeorge.Wilson@Sun.COM 			    ZIO_CHECKSUM_OFF) == 0);
175611656SGeorge.Wilson@Sun.COM 		}
17576423Sgw25295 
175811022STom.Erickson@Sun.COM 		error = zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
175911022STom.Erickson@Sun.COM 		    nv, NULL);
17606423Sgw25295 		nvlist_free(nv);
17616423Sgw25295 
17626423Sgw25295 		if (error)
17636423Sgw25295 			return (error);
17646423Sgw25295 	}
17656423Sgw25295 
17666423Sgw25295 	/* Allocate the space for the dump */
17676423Sgw25295 	error = zvol_prealloc(zv);
17686423Sgw25295 	return (error);
17696423Sgw25295 }
17706423Sgw25295 
17716423Sgw25295 static int
zvol_dumpify(zvol_state_t * zv)17726423Sgw25295 zvol_dumpify(zvol_state_t *zv)
17736423Sgw25295 {
17746423Sgw25295 	int error = 0;
17756423Sgw25295 	uint64_t dumpsize = 0;
17766423Sgw25295 	dmu_tx_t *tx;
17776423Sgw25295 	objset_t *os = zv->zv_objset;
17786423Sgw25295 
177910588SEric.Taylor@Sun.COM 	if (zv->zv_flags & ZVOL_RDONLY)
17806423Sgw25295 		return (EROFS);
17816423Sgw25295 
17826423Sgw25295 	if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE,
17836423Sgw25295 	    8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) {
17846423Sgw25295 		boolean_t resize = (dumpsize > 0) ? B_TRUE : B_FALSE;
17856423Sgw25295 
17866423Sgw25295 		if ((error = zvol_dump_init(zv, resize)) != 0) {
17876423Sgw25295 			(void) zvol_dump_fini(zv);
17886423Sgw25295 			return (error);
17896423Sgw25295 		}
17906423Sgw25295 	}
17916423Sgw25295 
17926423Sgw25295 	/*
17936423Sgw25295 	 * Build up our lba mapping.
17946423Sgw25295 	 */
17956423Sgw25295 	error = zvol_get_lbas(zv);
17966423Sgw25295 	if (error) {
17976423Sgw25295 		(void) zvol_dump_fini(zv);
17986423Sgw25295 		return (error);
17996423Sgw25295 	}
18006423Sgw25295 
18016423Sgw25295 	tx = dmu_tx_create(os);
18026423Sgw25295 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
18036423Sgw25295 	error = dmu_tx_assign(tx, TXG_WAIT);
18046423Sgw25295 	if (error) {
18056423Sgw25295 		dmu_tx_abort(tx);
18066423Sgw25295 		(void) zvol_dump_fini(zv);
18076423Sgw25295 		return (error);
18086423Sgw25295 	}
18096423Sgw25295 
18106423Sgw25295 	zv->zv_flags |= ZVOL_DUMPIFIED;
18116423Sgw25295 	error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1,
18126423Sgw25295 	    &zv->zv_volsize, tx);
18136423Sgw25295 	dmu_tx_commit(tx);
18146423Sgw25295 
18156423Sgw25295 	if (error) {
18166423Sgw25295 		(void) zvol_dump_fini(zv);
18176423Sgw25295 		return (error);
18186423Sgw25295 	}
18196423Sgw25295 
18206423Sgw25295 	txg_wait_synced(dmu_objset_pool(os), 0);
18216423Sgw25295 	return (0);
18226423Sgw25295 }
18236423Sgw25295 
18246423Sgw25295 static int
zvol_dump_fini(zvol_state_t * zv)18256423Sgw25295 zvol_dump_fini(zvol_state_t *zv)
18266423Sgw25295 {
18276423Sgw25295 	dmu_tx_t *tx;
18286423Sgw25295 	objset_t *os = zv->zv_objset;
18296423Sgw25295 	nvlist_t *nv;
18306423Sgw25295 	int error = 0;
183111619SGeorge.Wilson@Sun.COM 	uint64_t checksum, compress, refresrv, vbs, dedup;
183211656SGeorge.Wilson@Sun.COM 	uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset));
18336423Sgw25295 
18347080Smaybee 	/*
18357080Smaybee 	 * Attempt to restore the zvol back to its pre-dumpified state.
18367080Smaybee 	 * This is a best-effort attempt as it's possible that not all
18377080Smaybee 	 * of these properties were initialized during the dumpify process
18387080Smaybee 	 * (i.e. error during zvol_dump_init).
18397080Smaybee 	 */
18407080Smaybee 
18416423Sgw25295 	tx = dmu_tx_create(os);
18426423Sgw25295 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
18436423Sgw25295 	error = dmu_tx_assign(tx, TXG_WAIT);
18446423Sgw25295 	if (error) {
18456423Sgw25295 		dmu_tx_abort(tx);
18466423Sgw25295 		return (error);
18476423Sgw25295 	}
18487080Smaybee 	(void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx);
18497080Smaybee 	dmu_tx_commit(tx);
18506423Sgw25295 
18516423Sgw25295 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
18526423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum);
18536423Sgw25295 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
18546423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress);
18556423Sgw25295 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
18566423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv);
18577837SMatthew.Ahrens@Sun.COM 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
18587837SMatthew.Ahrens@Sun.COM 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs);
18596423Sgw25295 
18606423Sgw25295 	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
18616423Sgw25295 	(void) nvlist_add_uint64(nv,
18626423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum);
18636423Sgw25295 	(void) nvlist_add_uint64(nv,
18646423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress);
18656423Sgw25295 	(void) nvlist_add_uint64(nv,
18666423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv);
186711656SGeorge.Wilson@Sun.COM 	if (version >= SPA_VERSION_DEDUP &&
186811656SGeorge.Wilson@Sun.COM 	    zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
186911656SGeorge.Wilson@Sun.COM 	    zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, &dedup) == 0) {
187011656SGeorge.Wilson@Sun.COM 		(void) nvlist_add_uint64(nv,
187111656SGeorge.Wilson@Sun.COM 		    zfs_prop_to_name(ZFS_PROP_DEDUP), dedup);
187211656SGeorge.Wilson@Sun.COM 	}
187311022STom.Erickson@Sun.COM 	(void) zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
187411022STom.Erickson@Sun.COM 	    nv, NULL);
18756423Sgw25295 	nvlist_free(nv);
18766423Sgw25295 
18777080Smaybee 	zvol_free_extents(zv);
18787080Smaybee 	zv->zv_flags &= ~ZVOL_DUMPIFIED;
18797080Smaybee 	(void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END);
188010588SEric.Taylor@Sun.COM 	/* wait for dmu_free_long_range to actually free the blocks */
188110588SEric.Taylor@Sun.COM 	txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
188210588SEric.Taylor@Sun.COM 	tx = dmu_tx_create(os);
188310588SEric.Taylor@Sun.COM 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
188410588SEric.Taylor@Sun.COM 	error = dmu_tx_assign(tx, TXG_WAIT);
188510588SEric.Taylor@Sun.COM 	if (error) {
188610588SEric.Taylor@Sun.COM 		dmu_tx_abort(tx);
188710588SEric.Taylor@Sun.COM 		return (error);
188810588SEric.Taylor@Sun.COM 	}
188910922SJeff.Bonwick@Sun.COM 	if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0)
189010922SJeff.Bonwick@Sun.COM 		zv->zv_volblocksize = vbs;
189110588SEric.Taylor@Sun.COM 	dmu_tx_commit(tx);
18927080Smaybee 
18936423Sgw25295 	return (0);
18946423Sgw25295 }
1895