xref: /onnv-gate/usr/src/uts/common/fs/zfs/zvol.c (revision 8524:a56dffa8fba9)
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 /*
22*8524SEric.Taylor@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens /*
27789Sahrens  * ZFS volume emulation driver.
28789Sahrens  *
29789Sahrens  * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes.
30789Sahrens  * Volumes are accessed through the symbolic links named:
31789Sahrens  *
32789Sahrens  * /dev/zvol/dsk/<pool_name>/<dataset_name>
33789Sahrens  * /dev/zvol/rdsk/<pool_name>/<dataset_name>
34789Sahrens  *
35789Sahrens  * These links are created by the ZFS-specific devfsadm link generator.
36789Sahrens  * Volumes are persistent through reboot.  No user command needs to be
37789Sahrens  * run before opening and using a device.
38789Sahrens  */
39789Sahrens 
40789Sahrens #include <sys/types.h>
41789Sahrens #include <sys/param.h>
42789Sahrens #include <sys/errno.h>
43789Sahrens #include <sys/uio.h>
44789Sahrens #include <sys/buf.h>
45789Sahrens #include <sys/modctl.h>
46789Sahrens #include <sys/open.h>
47789Sahrens #include <sys/kmem.h>
48789Sahrens #include <sys/conf.h>
49789Sahrens #include <sys/cmn_err.h>
50789Sahrens #include <sys/stat.h>
51789Sahrens #include <sys/zap.h>
52789Sahrens #include <sys/spa.h>
53789Sahrens #include <sys/zio.h>
546423Sgw25295 #include <sys/dmu_traverse.h>
556423Sgw25295 #include <sys/dnode.h>
566423Sgw25295 #include <sys/dsl_dataset.h>
57789Sahrens #include <sys/dsl_prop.h>
58789Sahrens #include <sys/dkio.h>
59789Sahrens #include <sys/efi_partition.h>
60789Sahrens #include <sys/byteorder.h>
61789Sahrens #include <sys/pathname.h>
62789Sahrens #include <sys/ddi.h>
63789Sahrens #include <sys/sunddi.h>
64789Sahrens #include <sys/crc32.h>
65789Sahrens #include <sys/dirent.h>
66789Sahrens #include <sys/policy.h>
67789Sahrens #include <sys/fs/zfs.h>
68789Sahrens #include <sys/zfs_ioctl.h>
69789Sahrens #include <sys/mkdev.h>
701141Sperrin #include <sys/zil.h>
712237Smaybee #include <sys/refcount.h>
723755Sperrin #include <sys/zfs_znode.h>
733755Sperrin #include <sys/zfs_rlock.h>
746423Sgw25295 #include <sys/vdev_disk.h>
756423Sgw25295 #include <sys/vdev_impl.h>
766423Sgw25295 #include <sys/zvol.h>
776423Sgw25295 #include <sys/dumphdr.h>
788227SNeil.Perrin@Sun.COM #include <sys/zil_impl.h>
79789Sahrens 
80789Sahrens #include "zfs_namecheck.h"
81789Sahrens 
826423Sgw25295 static void *zvol_state;
83789Sahrens 
846423Sgw25295 #define	ZVOL_DUMPSIZE		"dumpsize"
85789Sahrens 
86789Sahrens /*
87789Sahrens  * This lock protects the zvol_state structure from being modified
88789Sahrens  * while it's being used, e.g. an open that comes in before a create
89789Sahrens  * finishes.  It also protects temporary opens of the dataset so that,
90789Sahrens  * e.g., an open doesn't get a spurious EBUSY.
91789Sahrens  */
92789Sahrens static kmutex_t zvol_state_lock;
93789Sahrens static uint32_t zvol_minors;
94789Sahrens 
956423Sgw25295 typedef struct zvol_extent {
967837SMatthew.Ahrens@Sun.COM 	list_node_t	ze_node;
976423Sgw25295 	dva_t		ze_dva;		/* dva associated with this extent */
987837SMatthew.Ahrens@Sun.COM 	uint64_t	ze_nblks;	/* number of blocks in extent */
996423Sgw25295 } zvol_extent_t;
1006423Sgw25295 
1016423Sgw25295 /*
102789Sahrens  * The in-core state of each volume.
103789Sahrens  */
104789Sahrens typedef struct zvol_state {
105789Sahrens 	char		zv_name[MAXPATHLEN]; /* pool/dd name */
106789Sahrens 	uint64_t	zv_volsize;	/* amount of space we advertise */
1073063Sperrin 	uint64_t	zv_volblocksize; /* volume block size */
108789Sahrens 	minor_t		zv_minor;	/* minor number */
109789Sahrens 	uint8_t		zv_min_bs;	/* minimum addressable block shift */
1106423Sgw25295 	uint8_t		zv_flags;	/* readonly; dumpified */
111789Sahrens 	objset_t	*zv_objset;	/* objset handle */
112789Sahrens 	uint32_t	zv_mode;	/* DS_MODE_* flags at open time */
113789Sahrens 	uint32_t	zv_open_count[OTYPCNT];	/* open counts */
114789Sahrens 	uint32_t	zv_total_opens;	/* total open count */
1151141Sperrin 	zilog_t		*zv_zilog;	/* ZIL handle */
1167837SMatthew.Ahrens@Sun.COM 	list_t		zv_extents;	/* List of extents for dump */
1173755Sperrin 	znode_t		zv_znode;	/* for range locking */
118789Sahrens } zvol_state_t;
119789Sahrens 
1203063Sperrin /*
1216423Sgw25295  * zvol specific flags
1226423Sgw25295  */
1236423Sgw25295 #define	ZVOL_RDONLY	0x1
1246423Sgw25295 #define	ZVOL_DUMPIFIED	0x2
1257405SEric.Taylor@Sun.COM #define	ZVOL_EXCL	0x4
1266423Sgw25295 
1276423Sgw25295 /*
1283063Sperrin  * zvol maximum transfer in one DMU tx.
1293063Sperrin  */
1303063Sperrin int zvol_maxphys = DMU_MAX_ACCESS/2;
1313063Sperrin 
1326423Sgw25295 extern int zfs_set_prop_nvlist(const char *, nvlist_t *);
1333638Sbillm static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio);
1346423Sgw25295 static int zvol_dumpify(zvol_state_t *zv);
1356423Sgw25295 static int zvol_dump_fini(zvol_state_t *zv);
1366423Sgw25295 static int zvol_dump_init(zvol_state_t *zv, boolean_t resize);
1373063Sperrin 
138789Sahrens static void
1394787Sahrens zvol_size_changed(zvol_state_t *zv, major_t maj)
140789Sahrens {
1414787Sahrens 	dev_t dev = makedevice(maj, zv->zv_minor);
142789Sahrens 
143789Sahrens 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
144789Sahrens 	    "Size", zv->zv_volsize) == DDI_SUCCESS);
145789Sahrens 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
146789Sahrens 	    "Nblocks", lbtodb(zv->zv_volsize)) == DDI_SUCCESS);
1476423Sgw25295 
1486423Sgw25295 	/* Notify specfs to invalidate the cached size */
1496423Sgw25295 	spec_size_invalidate(dev, VBLK);
1506423Sgw25295 	spec_size_invalidate(dev, VCHR);
151789Sahrens }
152789Sahrens 
153789Sahrens int
1542676Seschrock zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
155789Sahrens {
1562676Seschrock 	if (volsize == 0)
157789Sahrens 		return (EINVAL);
158789Sahrens 
1592676Seschrock 	if (volsize % blocksize != 0)
1601133Seschrock 		return (EINVAL);
1611133Seschrock 
162789Sahrens #ifdef _ILP32
1632676Seschrock 	if (volsize - 1 > SPEC_MAXOFFSET_T)
164789Sahrens 		return (EOVERFLOW);
165789Sahrens #endif
166789Sahrens 	return (0);
167789Sahrens }
168789Sahrens 
169789Sahrens int
1702676Seschrock zvol_check_volblocksize(uint64_t volblocksize)
171789Sahrens {
1722676Seschrock 	if (volblocksize < SPA_MINBLOCKSIZE ||
1732676Seschrock 	    volblocksize > SPA_MAXBLOCKSIZE ||
1742676Seschrock 	    !ISP2(volblocksize))
175789Sahrens 		return (EDOM);
176789Sahrens 
177789Sahrens 	return (0);
178789Sahrens }
179789Sahrens 
180789Sahrens static void
181789Sahrens zvol_readonly_changed_cb(void *arg, uint64_t newval)
182789Sahrens {
183789Sahrens 	zvol_state_t *zv = arg;
184789Sahrens 
1856423Sgw25295 	if (newval)
1866423Sgw25295 		zv->zv_flags |= ZVOL_RDONLY;
1876423Sgw25295 	else
1886423Sgw25295 		zv->zv_flags &= ~ZVOL_RDONLY;
189789Sahrens }
190789Sahrens 
191789Sahrens int
1922885Sahrens zvol_get_stats(objset_t *os, nvlist_t *nv)
193789Sahrens {
194789Sahrens 	int error;
195789Sahrens 	dmu_object_info_t doi;
1962885Sahrens 	uint64_t val;
197789Sahrens 
198789Sahrens 
1992885Sahrens 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
200789Sahrens 	if (error)
201789Sahrens 		return (error);
202789Sahrens 
2032885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
2042885Sahrens 
205789Sahrens 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
206789Sahrens 
2072885Sahrens 	if (error == 0) {
2082885Sahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
2092885Sahrens 		    doi.doi_data_block_size);
2102885Sahrens 	}
211789Sahrens 
212789Sahrens 	return (error);
213789Sahrens }
214789Sahrens 
215789Sahrens /*
216789Sahrens  * Find a free minor number.
217789Sahrens  */
218789Sahrens static minor_t
219789Sahrens zvol_minor_alloc(void)
220789Sahrens {
221789Sahrens 	minor_t minor;
222789Sahrens 
223789Sahrens 	ASSERT(MUTEX_HELD(&zvol_state_lock));
224789Sahrens 
225789Sahrens 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++)
226789Sahrens 		if (ddi_get_soft_state(zvol_state, minor) == NULL)
227789Sahrens 			return (minor);
228789Sahrens 
229789Sahrens 	return (0);
230789Sahrens }
231789Sahrens 
232789Sahrens static zvol_state_t *
2332676Seschrock zvol_minor_lookup(const char *name)
234789Sahrens {
235789Sahrens 	minor_t minor;
236789Sahrens 	zvol_state_t *zv;
237789Sahrens 
238789Sahrens 	ASSERT(MUTEX_HELD(&zvol_state_lock));
239789Sahrens 
240789Sahrens 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) {
241789Sahrens 		zv = ddi_get_soft_state(zvol_state, minor);
242789Sahrens 		if (zv == NULL)
243789Sahrens 			continue;
244789Sahrens 		if (strcmp(zv->zv_name, name) == 0)
245789Sahrens 			break;
246789Sahrens 	}
247789Sahrens 
248789Sahrens 	return (zv);
249789Sahrens }
250789Sahrens 
2516423Sgw25295 /* extent mapping arg */
2526423Sgw25295 struct maparg {
2537837SMatthew.Ahrens@Sun.COM 	zvol_state_t	*ma_zv;
2547837SMatthew.Ahrens@Sun.COM 	uint64_t	ma_blks;
2556423Sgw25295 };
2566423Sgw25295 
2576423Sgw25295 /*ARGSUSED*/
2586423Sgw25295 static int
2597837SMatthew.Ahrens@Sun.COM zvol_map_block(spa_t *spa, blkptr_t *bp, const zbookmark_t *zb,
2607837SMatthew.Ahrens@Sun.COM     const dnode_phys_t *dnp, void *arg)
2616423Sgw25295 {
2627837SMatthew.Ahrens@Sun.COM 	struct maparg *ma = arg;
2637837SMatthew.Ahrens@Sun.COM 	zvol_extent_t *ze;
2647837SMatthew.Ahrens@Sun.COM 	int bs = ma->ma_zv->zv_volblocksize;
2656423Sgw25295 
2667837SMatthew.Ahrens@Sun.COM 	if (bp == NULL || zb->zb_object != ZVOL_OBJ || zb->zb_level != 0)
2676423Sgw25295 		return (0);
2686423Sgw25295 
2697837SMatthew.Ahrens@Sun.COM 	VERIFY3U(ma->ma_blks, ==, zb->zb_blkid);
2707837SMatthew.Ahrens@Sun.COM 	ma->ma_blks++;
2717837SMatthew.Ahrens@Sun.COM 
2726423Sgw25295 	/* Abort immediately if we have encountered gang blocks */
2737837SMatthew.Ahrens@Sun.COM 	if (BP_IS_GANG(bp))
2747837SMatthew.Ahrens@Sun.COM 		return (EFRAGS);
2756423Sgw25295 
2767837SMatthew.Ahrens@Sun.COM 	/*
2777837SMatthew.Ahrens@Sun.COM 	 * See if the block is at the end of the previous extent.
2787837SMatthew.Ahrens@Sun.COM 	 */
2797837SMatthew.Ahrens@Sun.COM 	ze = list_tail(&ma->ma_zv->zv_extents);
2807837SMatthew.Ahrens@Sun.COM 	if (ze &&
2817837SMatthew.Ahrens@Sun.COM 	    DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) &&
2827837SMatthew.Ahrens@Sun.COM 	    DVA_GET_OFFSET(BP_IDENTITY(bp)) ==
2837837SMatthew.Ahrens@Sun.COM 	    DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) {
2847837SMatthew.Ahrens@Sun.COM 		ze->ze_nblks++;
2856423Sgw25295 		return (0);
2866423Sgw25295 	}
2876423Sgw25295 
2887837SMatthew.Ahrens@Sun.COM 	dprintf_bp(bp, "%s", "next blkptr:");
2897837SMatthew.Ahrens@Sun.COM 
2907837SMatthew.Ahrens@Sun.COM 	/* start a new extent */
2917837SMatthew.Ahrens@Sun.COM 	ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP);
2927837SMatthew.Ahrens@Sun.COM 	ze->ze_dva = bp->blk_dva[0];	/* structure assignment */
2937837SMatthew.Ahrens@Sun.COM 	ze->ze_nblks = 1;
2947837SMatthew.Ahrens@Sun.COM 	list_insert_tail(&ma->ma_zv->zv_extents, ze);
2957837SMatthew.Ahrens@Sun.COM 	return (0);
2967837SMatthew.Ahrens@Sun.COM }
2977837SMatthew.Ahrens@Sun.COM 
2987837SMatthew.Ahrens@Sun.COM static void
2997837SMatthew.Ahrens@Sun.COM zvol_free_extents(zvol_state_t *zv)
3007837SMatthew.Ahrens@Sun.COM {
3017837SMatthew.Ahrens@Sun.COM 	zvol_extent_t *ze;
3027837SMatthew.Ahrens@Sun.COM 
3037837SMatthew.Ahrens@Sun.COM 	while (ze = list_head(&zv->zv_extents)) {
3047837SMatthew.Ahrens@Sun.COM 		list_remove(&zv->zv_extents, ze);
3057837SMatthew.Ahrens@Sun.COM 		kmem_free(ze, sizeof (zvol_extent_t));
3067837SMatthew.Ahrens@Sun.COM 	}
3077837SMatthew.Ahrens@Sun.COM }
3087837SMatthew.Ahrens@Sun.COM 
3097837SMatthew.Ahrens@Sun.COM static int
3107837SMatthew.Ahrens@Sun.COM zvol_get_lbas(zvol_state_t *zv)
3117837SMatthew.Ahrens@Sun.COM {
3127837SMatthew.Ahrens@Sun.COM 	struct maparg	ma;
3137837SMatthew.Ahrens@Sun.COM 	int		err;
3147837SMatthew.Ahrens@Sun.COM 
3157837SMatthew.Ahrens@Sun.COM 	ma.ma_zv = zv;
3167837SMatthew.Ahrens@Sun.COM 	ma.ma_blks = 0;
3177837SMatthew.Ahrens@Sun.COM 	zvol_free_extents(zv);
3187837SMatthew.Ahrens@Sun.COM 
3197837SMatthew.Ahrens@Sun.COM 	err = traverse_dataset(dmu_objset_ds(zv->zv_objset), 0,
3207837SMatthew.Ahrens@Sun.COM 	    TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma);
3217837SMatthew.Ahrens@Sun.COM 	if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) {
3227837SMatthew.Ahrens@Sun.COM 		zvol_free_extents(zv);
3237837SMatthew.Ahrens@Sun.COM 		return (err ? err : EIO);
3246423Sgw25295 	}
3256423Sgw25295 
3266423Sgw25295 	return (0);
3276423Sgw25295 }
3286423Sgw25295 
3294543Smarks /* ARGSUSED */
330789Sahrens void
3314543Smarks zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
332789Sahrens {
3335331Samw 	zfs_creat_t *zct = arg;
3345331Samw 	nvlist_t *nvprops = zct->zct_props;
335789Sahrens 	int error;
3362676Seschrock 	uint64_t volblocksize, volsize;
337789Sahrens 
3384543Smarks 	VERIFY(nvlist_lookup_uint64(nvprops,
3392676Seschrock 	    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
3404543Smarks 	if (nvlist_lookup_uint64(nvprops,
3412676Seschrock 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
3422676Seschrock 		volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
3432676Seschrock 
3442676Seschrock 	/*
3456423Sgw25295 	 * These properties must be removed from the list so the generic
3462676Seschrock 	 * property setting step won't apply to them.
3472676Seschrock 	 */
3484543Smarks 	VERIFY(nvlist_remove_all(nvprops,
3492676Seschrock 	    zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
3504543Smarks 	(void) nvlist_remove_all(nvprops,
3512676Seschrock 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
3522676Seschrock 
3532676Seschrock 	error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
354789Sahrens 	    DMU_OT_NONE, 0, tx);
355789Sahrens 	ASSERT(error == 0);
356789Sahrens 
357789Sahrens 	error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
358789Sahrens 	    DMU_OT_NONE, 0, tx);
359789Sahrens 	ASSERT(error == 0);
360789Sahrens 
3612676Seschrock 	error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
362789Sahrens 	ASSERT(error == 0);
363789Sahrens }
364789Sahrens 
365789Sahrens /*
3661141Sperrin  * Replay a TX_WRITE ZIL transaction that didn't get committed
3671141Sperrin  * after a system failure
3681141Sperrin  */
3691141Sperrin static int
3701141Sperrin zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
3711141Sperrin {
3721141Sperrin 	objset_t *os = zv->zv_objset;
3731141Sperrin 	char *data = (char *)(lr + 1);	/* data follows lr_write_t */
3741141Sperrin 	uint64_t off = lr->lr_offset;
3751141Sperrin 	uint64_t len = lr->lr_length;
3761141Sperrin 	dmu_tx_t *tx;
3771141Sperrin 	int error;
3781141Sperrin 
3791141Sperrin 	if (byteswap)
3801141Sperrin 		byteswap_uint64_array(lr, sizeof (*lr));
3811141Sperrin 
3821141Sperrin 	tx = dmu_tx_create(os);
3831141Sperrin 	dmu_tx_hold_write(tx, ZVOL_OBJ, off, len);
3848227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_WAIT);
3851141Sperrin 	if (error) {
3861141Sperrin 		dmu_tx_abort(tx);
3871141Sperrin 	} else {
3881141Sperrin 		dmu_write(os, ZVOL_OBJ, off, len, data, tx);
3891141Sperrin 		dmu_tx_commit(tx);
3901141Sperrin 	}
3911141Sperrin 
3921141Sperrin 	return (error);
3931141Sperrin }
3941141Sperrin 
3951141Sperrin /* ARGSUSED */
3961141Sperrin static int
3971141Sperrin zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
3981141Sperrin {
3991141Sperrin 	return (ENOTSUP);
4001141Sperrin }
4011141Sperrin 
4021141Sperrin /*
4031141Sperrin  * Callback vectors for replaying records.
4041141Sperrin  * Only TX_WRITE is needed for zvol.
4051141Sperrin  */
4061141Sperrin zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = {
4071141Sperrin 	zvol_replay_err,	/* 0 no such transaction type */
4081141Sperrin 	zvol_replay_err,	/* TX_CREATE */
4091141Sperrin 	zvol_replay_err,	/* TX_MKDIR */
4101141Sperrin 	zvol_replay_err,	/* TX_MKXATTR */
4111141Sperrin 	zvol_replay_err,	/* TX_SYMLINK */
4121141Sperrin 	zvol_replay_err,	/* TX_REMOVE */
4131141Sperrin 	zvol_replay_err,	/* TX_RMDIR */
4141141Sperrin 	zvol_replay_err,	/* TX_LINK */
4151141Sperrin 	zvol_replay_err,	/* TX_RENAME */
4161141Sperrin 	zvol_replay_write,	/* TX_WRITE */
4171141Sperrin 	zvol_replay_err,	/* TX_TRUNCATE */
4181141Sperrin 	zvol_replay_err,	/* TX_SETATTR */
4191141Sperrin 	zvol_replay_err,	/* TX_ACL */
4201141Sperrin };
4211141Sperrin 
4221141Sperrin /*
4236423Sgw25295  * Create a minor node (plus a whole lot more) for the specified volume.
424789Sahrens  */
425789Sahrens int
4264787Sahrens zvol_create_minor(const char *name, major_t maj)
427789Sahrens {
428789Sahrens 	zvol_state_t *zv;
429789Sahrens 	objset_t *os;
4303063Sperrin 	dmu_object_info_t doi;
431789Sahrens 	uint64_t volsize;
432789Sahrens 	minor_t minor = 0;
433789Sahrens 	struct pathname linkpath;
4346689Smaybee 	int ds_mode = DS_MODE_OWNER;
435789Sahrens 	vnode_t *vp = NULL;
436789Sahrens 	char *devpath;
4376423Sgw25295 	size_t devpathlen = strlen(ZVOL_FULL_DEV_DIR) + strlen(name) + 1;
438789Sahrens 	char chrbuf[30], blkbuf[30];
439789Sahrens 	int error;
440789Sahrens 
441789Sahrens 	mutex_enter(&zvol_state_lock);
442789Sahrens 
443789Sahrens 	if ((zv = zvol_minor_lookup(name)) != NULL) {
444789Sahrens 		mutex_exit(&zvol_state_lock);
445789Sahrens 		return (EEXIST);
446789Sahrens 	}
447789Sahrens 
448789Sahrens 	if (strchr(name, '@') != 0)
449789Sahrens 		ds_mode |= DS_MODE_READONLY;
450789Sahrens 
451789Sahrens 	error = dmu_objset_open(name, DMU_OST_ZVOL, ds_mode, &os);
452789Sahrens 
453789Sahrens 	if (error) {
454789Sahrens 		mutex_exit(&zvol_state_lock);
455789Sahrens 		return (error);
456789Sahrens 	}
457789Sahrens 
458789Sahrens 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
459789Sahrens 
460789Sahrens 	if (error) {
461789Sahrens 		dmu_objset_close(os);
462789Sahrens 		mutex_exit(&zvol_state_lock);
463789Sahrens 		return (error);
464789Sahrens 	}
465789Sahrens 
466789Sahrens 	/*
467789Sahrens 	 * If there's an existing /dev/zvol symlink, try to use the
468789Sahrens 	 * same minor number we used last time.
469789Sahrens 	 */
470789Sahrens 	devpath = kmem_alloc(devpathlen, KM_SLEEP);
471789Sahrens 
4726423Sgw25295 	(void) sprintf(devpath, "%s%s", ZVOL_FULL_DEV_DIR, name);
473789Sahrens 
474789Sahrens 	error = lookupname(devpath, UIO_SYSSPACE, NO_FOLLOW, NULL, &vp);
475789Sahrens 
476789Sahrens 	kmem_free(devpath, devpathlen);
477789Sahrens 
478789Sahrens 	if (error == 0 && vp->v_type != VLNK)
479789Sahrens 		error = EINVAL;
480789Sahrens 
481789Sahrens 	if (error == 0) {
482789Sahrens 		pn_alloc(&linkpath);
483789Sahrens 		error = pn_getsymlink(vp, &linkpath, kcred);
484789Sahrens 		if (error == 0) {
485789Sahrens 			char *ms = strstr(linkpath.pn_path, ZVOL_PSEUDO_DEV);
486789Sahrens 			if (ms != NULL) {
487789Sahrens 				ms += strlen(ZVOL_PSEUDO_DEV);
488789Sahrens 				minor = stoi(&ms);
489789Sahrens 			}
490789Sahrens 		}
491789Sahrens 		pn_free(&linkpath);
492789Sahrens 	}
493789Sahrens 
494789Sahrens 	if (vp != NULL)
495789Sahrens 		VN_RELE(vp);
496789Sahrens 
497789Sahrens 	/*
498789Sahrens 	 * If we found a minor but it's already in use, we must pick a new one.
499789Sahrens 	 */
500789Sahrens 	if (minor != 0 && ddi_get_soft_state(zvol_state, minor) != NULL)
501789Sahrens 		minor = 0;
502789Sahrens 
503789Sahrens 	if (minor == 0)
504789Sahrens 		minor = zvol_minor_alloc();
505789Sahrens 
506789Sahrens 	if (minor == 0) {
507789Sahrens 		dmu_objset_close(os);
508789Sahrens 		mutex_exit(&zvol_state_lock);
509789Sahrens 		return (ENXIO);
510789Sahrens 	}
511789Sahrens 
512789Sahrens 	if (ddi_soft_state_zalloc(zvol_state, minor) != DDI_SUCCESS) {
513789Sahrens 		dmu_objset_close(os);
514789Sahrens 		mutex_exit(&zvol_state_lock);
515789Sahrens 		return (EAGAIN);
516789Sahrens 	}
517789Sahrens 
5182676Seschrock 	(void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME,
5192676Seschrock 	    (char *)name);
520789Sahrens 
521789Sahrens 	(void) sprintf(chrbuf, "%uc,raw", minor);
522789Sahrens 
523789Sahrens 	if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR,
524789Sahrens 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
525789Sahrens 		ddi_soft_state_free(zvol_state, minor);
526789Sahrens 		dmu_objset_close(os);
527789Sahrens 		mutex_exit(&zvol_state_lock);
528789Sahrens 		return (EAGAIN);
529789Sahrens 	}
530789Sahrens 
531789Sahrens 	(void) sprintf(blkbuf, "%uc", minor);
532789Sahrens 
533789Sahrens 	if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK,
534789Sahrens 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
535789Sahrens 		ddi_remove_minor_node(zfs_dip, chrbuf);
536789Sahrens 		ddi_soft_state_free(zvol_state, minor);
537789Sahrens 		dmu_objset_close(os);
538789Sahrens 		mutex_exit(&zvol_state_lock);
539789Sahrens 		return (EAGAIN);
540789Sahrens 	}
541789Sahrens 
542789Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
543789Sahrens 
544789Sahrens 	(void) strcpy(zv->zv_name, name);
545789Sahrens 	zv->zv_min_bs = DEV_BSHIFT;
546789Sahrens 	zv->zv_minor = minor;
547789Sahrens 	zv->zv_volsize = volsize;
548789Sahrens 	zv->zv_objset = os;
549789Sahrens 	zv->zv_mode = ds_mode;
5503063Sperrin 	zv->zv_zilog = zil_open(os, zvol_get_data);
5513755Sperrin 	mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
5523755Sperrin 	avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
5533755Sperrin 	    sizeof (rl_t), offsetof(rl_t, r_node));
5547837SMatthew.Ahrens@Sun.COM 	list_create(&zv->zv_extents, sizeof (zvol_extent_t),
5557837SMatthew.Ahrens@Sun.COM 	    offsetof(zvol_extent_t, ze_node));
5563063Sperrin 	/* get and cache the blocksize */
5573063Sperrin 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
5583063Sperrin 	ASSERT(error == 0);
5593063Sperrin 	zv->zv_volblocksize = doi.doi_data_block_size;
5601141Sperrin 
5618227SNeil.Perrin@Sun.COM 	zil_replay(os, zv, zvol_replay_vector);
5624787Sahrens 	zvol_size_changed(zv, maj);
563789Sahrens 
5641544Seschrock 	/* XXX this should handle the possible i/o error */
565789Sahrens 	VERIFY(dsl_prop_register(dmu_objset_ds(zv->zv_objset),
566789Sahrens 	    "readonly", zvol_readonly_changed_cb, zv) == 0);
567789Sahrens 
568789Sahrens 	zvol_minors++;
569789Sahrens 
570789Sahrens 	mutex_exit(&zvol_state_lock);
571789Sahrens 
572789Sahrens 	return (0);
573789Sahrens }
574789Sahrens 
575789Sahrens /*
576789Sahrens  * Remove minor node for the specified volume.
577789Sahrens  */
578789Sahrens int
5792676Seschrock zvol_remove_minor(const char *name)
580789Sahrens {
581789Sahrens 	zvol_state_t *zv;
582789Sahrens 	char namebuf[30];
583789Sahrens 
584789Sahrens 	mutex_enter(&zvol_state_lock);
585789Sahrens 
5862676Seschrock 	if ((zv = zvol_minor_lookup(name)) == NULL) {
587789Sahrens 		mutex_exit(&zvol_state_lock);
588789Sahrens 		return (ENXIO);
589789Sahrens 	}
590789Sahrens 
591789Sahrens 	if (zv->zv_total_opens != 0) {
592789Sahrens 		mutex_exit(&zvol_state_lock);
593789Sahrens 		return (EBUSY);
594789Sahrens 	}
595789Sahrens 
596789Sahrens 	(void) sprintf(namebuf, "%uc,raw", zv->zv_minor);
597789Sahrens 	ddi_remove_minor_node(zfs_dip, namebuf);
598789Sahrens 
599789Sahrens 	(void) sprintf(namebuf, "%uc", zv->zv_minor);
600789Sahrens 	ddi_remove_minor_node(zfs_dip, namebuf);
601789Sahrens 
602789Sahrens 	VERIFY(dsl_prop_unregister(dmu_objset_ds(zv->zv_objset),
603789Sahrens 	    "readonly", zvol_readonly_changed_cb, zv) == 0);
604789Sahrens 
6051141Sperrin 	zil_close(zv->zv_zilog);
6061141Sperrin 	zv->zv_zilog = NULL;
607789Sahrens 	dmu_objset_close(zv->zv_objset);
608789Sahrens 	zv->zv_objset = NULL;
6093755Sperrin 	avl_destroy(&zv->zv_znode.z_range_avl);
6103755Sperrin 	mutex_destroy(&zv->zv_znode.z_range_lock);
611789Sahrens 
612789Sahrens 	ddi_soft_state_free(zvol_state, zv->zv_minor);
613789Sahrens 
614789Sahrens 	zvol_minors--;
615789Sahrens 
616789Sahrens 	mutex_exit(&zvol_state_lock);
617789Sahrens 
618789Sahrens 	return (0);
619789Sahrens }
620789Sahrens 
6216423Sgw25295 int
6226423Sgw25295 zvol_prealloc(zvol_state_t *zv)
6236423Sgw25295 {
6246423Sgw25295 	objset_t *os = zv->zv_objset;
6256423Sgw25295 	dmu_tx_t *tx;
6266423Sgw25295 	uint64_t refd, avail, usedobjs, availobjs;
6276423Sgw25295 	uint64_t resid = zv->zv_volsize;
6286423Sgw25295 	uint64_t off = 0;
6296423Sgw25295 
6306423Sgw25295 	/* Check the space usage before attempting to allocate the space */
6316423Sgw25295 	dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs);
6326423Sgw25295 	if (avail < zv->zv_volsize)
6336423Sgw25295 		return (ENOSPC);
6346423Sgw25295 
6356423Sgw25295 	/* Free old extents if they exist */
6366423Sgw25295 	zvol_free_extents(zv);
6376423Sgw25295 
6386423Sgw25295 	while (resid != 0) {
6396423Sgw25295 		int error;
6406423Sgw25295 		uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE);
6416423Sgw25295 
6426423Sgw25295 		tx = dmu_tx_create(os);
6436423Sgw25295 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
6446423Sgw25295 		error = dmu_tx_assign(tx, TXG_WAIT);
6456423Sgw25295 		if (error) {
6466423Sgw25295 			dmu_tx_abort(tx);
6476992Smaybee 			(void) dmu_free_long_range(os, ZVOL_OBJ, 0, off);
6486423Sgw25295 			return (error);
6496423Sgw25295 		}
6507872STim.Haley@Sun.COM 		dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx);
6516423Sgw25295 		dmu_tx_commit(tx);
6526423Sgw25295 		off += bytes;
6536423Sgw25295 		resid -= bytes;
6546423Sgw25295 	}
6556423Sgw25295 	txg_wait_synced(dmu_objset_pool(os), 0);
6566423Sgw25295 
6576423Sgw25295 	return (0);
6586423Sgw25295 }
6596423Sgw25295 
6606423Sgw25295 int
6616423Sgw25295 zvol_update_volsize(zvol_state_t *zv, major_t maj, uint64_t volsize)
6626423Sgw25295 {
6636423Sgw25295 	dmu_tx_t *tx;
6646423Sgw25295 	int error;
6656423Sgw25295 
6666423Sgw25295 	ASSERT(MUTEX_HELD(&zvol_state_lock));
6676423Sgw25295 
6686423Sgw25295 	tx = dmu_tx_create(zv->zv_objset);
6696423Sgw25295 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
6706423Sgw25295 	error = dmu_tx_assign(tx, TXG_WAIT);
6716423Sgw25295 	if (error) {
6726423Sgw25295 		dmu_tx_abort(tx);
6736423Sgw25295 		return (error);
6746423Sgw25295 	}
6756423Sgw25295 
6766423Sgw25295 	error = zap_update(zv->zv_objset, ZVOL_ZAP_OBJ, "size", 8, 1,
6776423Sgw25295 	    &volsize, tx);
6786423Sgw25295 	dmu_tx_commit(tx);
6796423Sgw25295 
6806423Sgw25295 	if (error == 0)
6816992Smaybee 		error = dmu_free_long_range(zv->zv_objset,
6826992Smaybee 		    ZVOL_OBJ, volsize, DMU_OBJECT_END);
6836423Sgw25295 
6847265Sahrens 	/*
6857265Sahrens 	 * If we are using a faked-up state (zv_minor == 0) then don't
6867265Sahrens 	 * try to update the in-core zvol state.
6877265Sahrens 	 */
6887265Sahrens 	if (error == 0 && zv->zv_minor) {
6896423Sgw25295 		zv->zv_volsize = volsize;
6906423Sgw25295 		zvol_size_changed(zv, maj);
6916423Sgw25295 	}
6926423Sgw25295 	return (error);
6936423Sgw25295 }
6946423Sgw25295 
695789Sahrens int
6964787Sahrens zvol_set_volsize(const char *name, major_t maj, uint64_t volsize)
697789Sahrens {
698789Sahrens 	zvol_state_t *zv;
699789Sahrens 	int error;
7001133Seschrock 	dmu_object_info_t doi;
7016423Sgw25295 	uint64_t old_volsize = 0ULL;
7027265Sahrens 	zvol_state_t state = { 0 };
703789Sahrens 
704789Sahrens 	mutex_enter(&zvol_state_lock);
705789Sahrens 
7062676Seschrock 	if ((zv = zvol_minor_lookup(name)) == NULL) {
7077265Sahrens 		/*
7087265Sahrens 		 * If we are doing a "zfs clone -o volsize=", then the
7097265Sahrens 		 * minor node won't exist yet.
7107265Sahrens 		 */
7117265Sahrens 		error = dmu_objset_open(name, DMU_OST_ZVOL, DS_MODE_OWNER,
7127265Sahrens 		    &state.zv_objset);
7137265Sahrens 		if (error != 0)
7147265Sahrens 			goto out;
7157265Sahrens 		zv = &state;
716789Sahrens 	}
7176423Sgw25295 	old_volsize = zv->zv_volsize;
718789Sahrens 
7191133Seschrock 	if ((error = dmu_object_info(zv->zv_objset, ZVOL_OBJ, &doi)) != 0 ||
7202676Seschrock 	    (error = zvol_check_volsize(volsize,
7217265Sahrens 	    doi.doi_data_block_size)) != 0)
7227265Sahrens 		goto out;
7231133Seschrock 
7246423Sgw25295 	if (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY)) {
7257265Sahrens 		error = EROFS;
7267265Sahrens 		goto out;
727789Sahrens 	}
728789Sahrens 
7296423Sgw25295 	error = zvol_update_volsize(zv, maj, volsize);
730789Sahrens 
7316423Sgw25295 	/*
7326423Sgw25295 	 * Reinitialize the dump area to the new size. If we
7336423Sgw25295 	 * failed to resize the dump area then restore the it back to
7346423Sgw25295 	 * it's original size.
7356423Sgw25295 	 */
7366423Sgw25295 	if (error == 0 && zv->zv_flags & ZVOL_DUMPIFIED) {
7376423Sgw25295 		if ((error = zvol_dumpify(zv)) != 0 ||
7386423Sgw25295 		    (error = dumpvp_resize()) != 0) {
7396423Sgw25295 			(void) zvol_update_volsize(zv, maj, old_volsize);
7406423Sgw25295 			error = zvol_dumpify(zv);
7416423Sgw25295 		}
742789Sahrens 	}
743789Sahrens 
7447265Sahrens out:
7457265Sahrens 	if (state.zv_objset)
7467265Sahrens 		dmu_objset_close(state.zv_objset);
7477265Sahrens 
748789Sahrens 	mutex_exit(&zvol_state_lock);
749789Sahrens 
750789Sahrens 	return (error);
751789Sahrens }
752789Sahrens 
753789Sahrens int
7542676Seschrock zvol_set_volblocksize(const char *name, uint64_t volblocksize)
755789Sahrens {
756789Sahrens 	zvol_state_t *zv;
757789Sahrens 	dmu_tx_t *tx;
758789Sahrens 	int error;
7597837SMatthew.Ahrens@Sun.COM 	boolean_t needlock;
760789Sahrens 
7617837SMatthew.Ahrens@Sun.COM 	/*
7627837SMatthew.Ahrens@Sun.COM 	 * The lock may already be held if we are being called from
7637837SMatthew.Ahrens@Sun.COM 	 * zvol_dump_init().
7647837SMatthew.Ahrens@Sun.COM 	 */
7657837SMatthew.Ahrens@Sun.COM 	needlock = !MUTEX_HELD(&zvol_state_lock);
7667837SMatthew.Ahrens@Sun.COM 	if (needlock)
7677837SMatthew.Ahrens@Sun.COM 		mutex_enter(&zvol_state_lock);
768789Sahrens 
7692676Seschrock 	if ((zv = zvol_minor_lookup(name)) == NULL) {
7707837SMatthew.Ahrens@Sun.COM 		if (needlock)
7717837SMatthew.Ahrens@Sun.COM 			mutex_exit(&zvol_state_lock);
772789Sahrens 		return (ENXIO);
773789Sahrens 	}
7746423Sgw25295 	if (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY)) {
7757837SMatthew.Ahrens@Sun.COM 		if (needlock)
7767837SMatthew.Ahrens@Sun.COM 			mutex_exit(&zvol_state_lock);
777789Sahrens 		return (EROFS);
778789Sahrens 	}
779789Sahrens 
780789Sahrens 	tx = dmu_tx_create(zv->zv_objset);
781789Sahrens 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
782789Sahrens 	error = dmu_tx_assign(tx, TXG_WAIT);
783789Sahrens 	if (error) {
784789Sahrens 		dmu_tx_abort(tx);
785789Sahrens 	} else {
786789Sahrens 		error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ,
7872676Seschrock 		    volblocksize, 0, tx);
788789Sahrens 		if (error == ENOTSUP)
789789Sahrens 			error = EBUSY;
790789Sahrens 		dmu_tx_commit(tx);
7917837SMatthew.Ahrens@Sun.COM 		if (error == 0)
7927837SMatthew.Ahrens@Sun.COM 			zv->zv_volblocksize = volblocksize;
793789Sahrens 	}
794789Sahrens 
7957837SMatthew.Ahrens@Sun.COM 	if (needlock)
7967837SMatthew.Ahrens@Sun.COM 		mutex_exit(&zvol_state_lock);
797789Sahrens 
798789Sahrens 	return (error);
799789Sahrens }
800789Sahrens 
801789Sahrens /*ARGSUSED*/
802789Sahrens int
803789Sahrens zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr)
804789Sahrens {
805789Sahrens 	minor_t minor = getminor(*devp);
806789Sahrens 	zvol_state_t *zv;
807789Sahrens 
808789Sahrens 	if (minor == 0)			/* This is the control device */
809789Sahrens 		return (0);
810789Sahrens 
811789Sahrens 	mutex_enter(&zvol_state_lock);
812789Sahrens 
813789Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
814789Sahrens 	if (zv == NULL) {
815789Sahrens 		mutex_exit(&zvol_state_lock);
816789Sahrens 		return (ENXIO);
817789Sahrens 	}
818789Sahrens 
819789Sahrens 	ASSERT(zv->zv_objset != NULL);
820789Sahrens 
821789Sahrens 	if ((flag & FWRITE) &&
8226423Sgw25295 	    (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY))) {
823789Sahrens 		mutex_exit(&zvol_state_lock);
824789Sahrens 		return (EROFS);
825789Sahrens 	}
8267405SEric.Taylor@Sun.COM 	if (zv->zv_flags & ZVOL_EXCL) {
8277405SEric.Taylor@Sun.COM 		mutex_exit(&zvol_state_lock);
8287405SEric.Taylor@Sun.COM 		return (EBUSY);
8297405SEric.Taylor@Sun.COM 	}
8307405SEric.Taylor@Sun.COM 	if (flag & FEXCL) {
8317405SEric.Taylor@Sun.COM 		if (zv->zv_total_opens != 0) {
8327405SEric.Taylor@Sun.COM 			mutex_exit(&zvol_state_lock);
8337405SEric.Taylor@Sun.COM 			return (EBUSY);
8347405SEric.Taylor@Sun.COM 		}
8357405SEric.Taylor@Sun.COM 		zv->zv_flags |= ZVOL_EXCL;
8367405SEric.Taylor@Sun.COM 	}
837789Sahrens 
838789Sahrens 	if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) {
839789Sahrens 		zv->zv_open_count[otyp]++;
840789Sahrens 		zv->zv_total_opens++;
841789Sahrens 	}
842789Sahrens 
843789Sahrens 	mutex_exit(&zvol_state_lock);
844789Sahrens 
845789Sahrens 	return (0);
846789Sahrens }
847789Sahrens 
848789Sahrens /*ARGSUSED*/
849789Sahrens int
850789Sahrens zvol_close(dev_t dev, int flag, int otyp, cred_t *cr)
851789Sahrens {
852789Sahrens 	minor_t minor = getminor(dev);
853789Sahrens 	zvol_state_t *zv;
854789Sahrens 
855789Sahrens 	if (minor == 0)		/* This is the control device */
856789Sahrens 		return (0);
857789Sahrens 
858789Sahrens 	mutex_enter(&zvol_state_lock);
859789Sahrens 
860789Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
861789Sahrens 	if (zv == NULL) {
862789Sahrens 		mutex_exit(&zvol_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 
884789Sahrens 	mutex_exit(&zvol_state_lock);
885789Sahrens 
886789Sahrens 	return (0);
887789Sahrens }
888789Sahrens 
8893638Sbillm static void
8903063Sperrin zvol_get_done(dmu_buf_t *db, void *vzgd)
8913063Sperrin {
8923063Sperrin 	zgd_t *zgd = (zgd_t *)vzgd;
8933755Sperrin 	rl_t *rl = zgd->zgd_rl;
8943063Sperrin 
8953063Sperrin 	dmu_buf_rele(db, vzgd);
8963755Sperrin 	zfs_range_unlock(rl);
8975688Sbonwick 	zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
8983063Sperrin 	kmem_free(zgd, sizeof (zgd_t));
8993063Sperrin }
9003063Sperrin 
9013063Sperrin /*
9023063Sperrin  * Get data to generate a TX_WRITE intent log record.
9033063Sperrin  */
9043638Sbillm static int
9053063Sperrin zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
9063063Sperrin {
9073063Sperrin 	zvol_state_t *zv = arg;
9083063Sperrin 	objset_t *os = zv->zv_objset;
9093063Sperrin 	dmu_buf_t *db;
9103755Sperrin 	rl_t *rl;
9113063Sperrin 	zgd_t *zgd;
9123755Sperrin 	uint64_t boff; 			/* block starting offset */
9133755Sperrin 	int dlen = lr->lr_length;	/* length of user data */
9143063Sperrin 	int error;
9153063Sperrin 
9163063Sperrin 	ASSERT(zio);
9173755Sperrin 	ASSERT(dlen != 0);
9183638Sbillm 
9193755Sperrin 	/*
9203755Sperrin 	 * Write records come in two flavors: immediate and indirect.
9213755Sperrin 	 * For small writes it's cheaper to store the data with the
9223755Sperrin 	 * log record (immediate); for large writes it's cheaper to
9233755Sperrin 	 * sync the data and get a pointer to it (indirect) so that
9243755Sperrin 	 * we don't have to write the data twice.
9253755Sperrin 	 */
9263755Sperrin 	if (buf != NULL) /* immediate write */
9273755Sperrin 		return (dmu_read(os, ZVOL_OBJ, lr->lr_offset, dlen, buf));
9283063Sperrin 
9293063Sperrin 	zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP);
9303063Sperrin 	zgd->zgd_zilog = zv->zv_zilog;
9313063Sperrin 	zgd->zgd_bp = &lr->lr_blkptr;
9323063Sperrin 
9333755Sperrin 	/*
9343755Sperrin 	 * Lock the range of the block to ensure that when the data is
9356423Sgw25295 	 * written out and its checksum is being calculated that no other
9363755Sperrin 	 * thread can change the block.
9373755Sperrin 	 */
9383755Sperrin 	boff = P2ALIGN_TYPED(lr->lr_offset, zv->zv_volblocksize, uint64_t);
9393755Sperrin 	rl = zfs_range_lock(&zv->zv_znode, boff, zv->zv_volblocksize,
9403755Sperrin 	    RL_READER);
9413755Sperrin 	zgd->zgd_rl = rl;
9423755Sperrin 
9433063Sperrin 	VERIFY(0 == dmu_buf_hold(os, ZVOL_OBJ, lr->lr_offset, zgd, &db));
9443063Sperrin 	error = dmu_sync(zio, db, &lr->lr_blkptr,
9453063Sperrin 	    lr->lr_common.lrc_txg, zvol_get_done, zgd);
9463638Sbillm 	if (error == 0)
9475688Sbonwick 		zil_add_block(zv->zv_zilog, &lr->lr_blkptr);
9483063Sperrin 	/*
9493063Sperrin 	 * If we get EINPROGRESS, then we need to wait for a
9503063Sperrin 	 * write IO initiated by dmu_sync() to complete before
9513063Sperrin 	 * we can release this dbuf.  We will finish everything
9523063Sperrin 	 * up in the zvol_get_done() callback.
9533063Sperrin 	 */
9543063Sperrin 	if (error == EINPROGRESS)
9553063Sperrin 		return (0);
9563063Sperrin 	dmu_buf_rele(db, zgd);
9573755Sperrin 	zfs_range_unlock(rl);
9583063Sperrin 	kmem_free(zgd, sizeof (zgd_t));
9593063Sperrin 	return (error);
9603063Sperrin }
9613063Sperrin 
9621861Sperrin /*
9631861Sperrin  * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
9641141Sperrin  *
9651141Sperrin  * We store data in the log buffers if it's small enough.
9663063Sperrin  * Otherwise we will later flush the data out via dmu_sync().
9671141Sperrin  */
9683063Sperrin ssize_t zvol_immediate_write_sz = 32768;
9691141Sperrin 
9703638Sbillm static void
9713638Sbillm zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t len)
9721141Sperrin {
9733638Sbillm 	uint32_t blocksize = zv->zv_volblocksize;
9748227SNeil.Perrin@Sun.COM 	zilog_t *zilog = zv->zv_zilog;
9751141Sperrin 	lr_write_t *lr;
9761861Sperrin 
9778227SNeil.Perrin@Sun.COM 	if (zilog->zl_replay) {
9788227SNeil.Perrin@Sun.COM 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
9798227SNeil.Perrin@Sun.COM 		zilog->zl_replayed_seq[dmu_tx_get_txg(tx) & TXG_MASK] =
9808227SNeil.Perrin@Sun.COM 		    zilog->zl_replaying_seq;
9818227SNeil.Perrin@Sun.COM 		return;
9828227SNeil.Perrin@Sun.COM 	}
9838227SNeil.Perrin@Sun.COM 
9841861Sperrin 	while (len) {
9853638Sbillm 		ssize_t nbytes = MIN(len, blocksize - P2PHASE(off, blocksize));
9863638Sbillm 		itx_t *itx = zil_itx_create(TX_WRITE, sizeof (*lr));
9873638Sbillm 
9883638Sbillm 		itx->itx_wr_state =
9893638Sbillm 		    len > zvol_immediate_write_sz ?  WR_INDIRECT : WR_NEED_COPY;
9903638Sbillm 		itx->itx_private = zv;
9913638Sbillm 		lr = (lr_write_t *)&itx->itx_lr;
9923638Sbillm 		lr->lr_foid = ZVOL_OBJ;
9933638Sbillm 		lr->lr_offset = off;
9943638Sbillm 		lr->lr_length = nbytes;
9953638Sbillm 		lr->lr_blkoff = off - P2ALIGN_TYPED(off, blocksize, uint64_t);
9963638Sbillm 		BP_ZERO(&lr->lr_blkptr);
9973638Sbillm 
9988227SNeil.Perrin@Sun.COM 		(void) zil_itx_assign(zilog, itx, tx);
9991861Sperrin 		len -= nbytes;
10001861Sperrin 		off += nbytes;
10011141Sperrin 	}
10021141Sperrin }
10031141Sperrin 
10047837SMatthew.Ahrens@Sun.COM static int
10057837SMatthew.Ahrens@Sun.COM zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t size,
10067837SMatthew.Ahrens@Sun.COM     boolean_t doread, boolean_t isdump)
10076423Sgw25295 {
10086423Sgw25295 	vdev_disk_t *dvd;
10096423Sgw25295 	int c;
10106423Sgw25295 	int numerrors = 0;
10116423Sgw25295 
10126423Sgw25295 	for (c = 0; c < vd->vdev_children; c++) {
10137837SMatthew.Ahrens@Sun.COM 		ASSERT(vd->vdev_ops == &vdev_mirror_ops);
10147837SMatthew.Ahrens@Sun.COM 		int err = zvol_dumpio_vdev(vd->vdev_child[c],
10157837SMatthew.Ahrens@Sun.COM 		    addr, offset, size, doread, isdump);
10167837SMatthew.Ahrens@Sun.COM 		if (err != 0) {
10176423Sgw25295 			numerrors++;
10187837SMatthew.Ahrens@Sun.COM 		} else if (doread) {
10196423Sgw25295 			break;
10206423Sgw25295 		}
10216423Sgw25295 	}
10226423Sgw25295 
10236423Sgw25295 	if (!vd->vdev_ops->vdev_op_leaf)
10246423Sgw25295 		return (numerrors < vd->vdev_children ? 0 : EIO);
10256423Sgw25295 
10267903SEric.Taylor@Sun.COM 	if (doread && !vdev_readable(vd))
10277903SEric.Taylor@Sun.COM 		return (EIO);
10287903SEric.Taylor@Sun.COM 	else if (!doread && !vdev_writeable(vd))
10296423Sgw25295 		return (EIO);
10306423Sgw25295 
10316423Sgw25295 	dvd = vd->vdev_tsd;
10326423Sgw25295 	ASSERT3P(dvd, !=, NULL);
10336423Sgw25295 	offset += VDEV_LABEL_START_SIZE;
10346423Sgw25295 
10356423Sgw25295 	if (ddi_in_panic() || isdump) {
10367837SMatthew.Ahrens@Sun.COM 		ASSERT(!doread);
10377837SMatthew.Ahrens@Sun.COM 		if (doread)
10386423Sgw25295 			return (EIO);
10396423Sgw25295 		return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset),
10406423Sgw25295 		    lbtodb(size)));
10416423Sgw25295 	} else {
10426423Sgw25295 		return (vdev_disk_physio(dvd->vd_lh, addr, size, offset,
10437837SMatthew.Ahrens@Sun.COM 		    doread ? B_READ : B_WRITE));
10446423Sgw25295 	}
10456423Sgw25295 }
10466423Sgw25295 
10477837SMatthew.Ahrens@Sun.COM static int
10487837SMatthew.Ahrens@Sun.COM zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size,
10497837SMatthew.Ahrens@Sun.COM     boolean_t doread, boolean_t isdump)
10506423Sgw25295 {
10516423Sgw25295 	vdev_t *vd;
10526423Sgw25295 	int error;
10537837SMatthew.Ahrens@Sun.COM 	zvol_extent_t *ze;
10546423Sgw25295 	spa_t *spa = dmu_objset_spa(zv->zv_objset);
10556423Sgw25295 
10567837SMatthew.Ahrens@Sun.COM 	/* Must be sector aligned, and not stradle a block boundary. */
10577837SMatthew.Ahrens@Sun.COM 	if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) ||
10587837SMatthew.Ahrens@Sun.COM 	    P2BOUNDARY(offset, size, zv->zv_volblocksize)) {
10597837SMatthew.Ahrens@Sun.COM 		return (EINVAL);
10607837SMatthew.Ahrens@Sun.COM 	}
10616423Sgw25295 	ASSERT(size <= zv->zv_volblocksize);
10626423Sgw25295 
10637837SMatthew.Ahrens@Sun.COM 	/* Locate the extent this belongs to */
10647837SMatthew.Ahrens@Sun.COM 	ze = list_head(&zv->zv_extents);
10657837SMatthew.Ahrens@Sun.COM 	while (offset >= ze->ze_nblks * zv->zv_volblocksize) {
10667837SMatthew.Ahrens@Sun.COM 		offset -= ze->ze_nblks * zv->zv_volblocksize;
10677837SMatthew.Ahrens@Sun.COM 		ze = list_next(&zv->zv_extents, ze);
10687837SMatthew.Ahrens@Sun.COM 	}
10697754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
10707837SMatthew.Ahrens@Sun.COM 	vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva));
10717837SMatthew.Ahrens@Sun.COM 	offset += DVA_GET_OFFSET(&ze->ze_dva);
10727837SMatthew.Ahrens@Sun.COM 	error = zvol_dumpio_vdev(vd, addr, offset, size, doread, isdump);
10737754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
10746423Sgw25295 	return (error);
10756423Sgw25295 }
10766423Sgw25295 
10776423Sgw25295 int
1078789Sahrens zvol_strategy(buf_t *bp)
1079789Sahrens {
1080789Sahrens 	zvol_state_t *zv = ddi_get_soft_state(zvol_state, getminor(bp->b_edev));
1081789Sahrens 	uint64_t off, volsize;
10827837SMatthew.Ahrens@Sun.COM 	size_t resid;
1083789Sahrens 	char *addr;
10841141Sperrin 	objset_t *os;
10853755Sperrin 	rl_t *rl;
1086789Sahrens 	int error = 0;
10877837SMatthew.Ahrens@Sun.COM 	boolean_t doread = bp->b_flags & B_READ;
10887837SMatthew.Ahrens@Sun.COM 	boolean_t is_dump = zv->zv_flags & ZVOL_DUMPIFIED;
1089789Sahrens 
1090789Sahrens 	if (zv == NULL) {
1091789Sahrens 		bioerror(bp, ENXIO);
1092789Sahrens 		biodone(bp);
1093789Sahrens 		return (0);
1094789Sahrens 	}
1095789Sahrens 
1096789Sahrens 	if (getminor(bp->b_edev) == 0) {
1097789Sahrens 		bioerror(bp, EINVAL);
1098789Sahrens 		biodone(bp);
1099789Sahrens 		return (0);
1100789Sahrens 	}
1101789Sahrens 
11026423Sgw25295 	if (!(bp->b_flags & B_READ) &&
11036423Sgw25295 	    (zv->zv_flags & ZVOL_RDONLY ||
11046423Sgw25295 	    zv->zv_mode & DS_MODE_READONLY)) {
1105789Sahrens 		bioerror(bp, EROFS);
1106789Sahrens 		biodone(bp);
1107789Sahrens 		return (0);
1108789Sahrens 	}
1109789Sahrens 
1110789Sahrens 	off = ldbtob(bp->b_blkno);
1111789Sahrens 	volsize = zv->zv_volsize;
1112789Sahrens 
11131141Sperrin 	os = zv->zv_objset;
11141141Sperrin 	ASSERT(os != NULL);
1115789Sahrens 
1116789Sahrens 	bp_mapin(bp);
1117789Sahrens 	addr = bp->b_un.b_addr;
1118789Sahrens 	resid = bp->b_bcount;
1119789Sahrens 
11207837SMatthew.Ahrens@Sun.COM 	if (resid > 0 && (off < 0 || off >= volsize)) {
11217837SMatthew.Ahrens@Sun.COM 		bioerror(bp, EIO);
11227837SMatthew.Ahrens@Sun.COM 		biodone(bp);
11237837SMatthew.Ahrens@Sun.COM 		return (0);
11247837SMatthew.Ahrens@Sun.COM 	}
11257013Sgw25295 
11261861Sperrin 	/*
11271861Sperrin 	 * There must be no buffer changes when doing a dmu_sync() because
11281861Sperrin 	 * we can't change the data whilst calculating the checksum.
11291861Sperrin 	 */
11303755Sperrin 	rl = zfs_range_lock(&zv->zv_znode, off, resid,
11317837SMatthew.Ahrens@Sun.COM 	    doread ? RL_READER : RL_WRITER);
11326423Sgw25295 
1133789Sahrens 	while (resid != 0 && off < volsize) {
11347837SMatthew.Ahrens@Sun.COM 		size_t size = MIN(resid, zvol_maxphys);
11356423Sgw25295 		if (is_dump) {
11366423Sgw25295 			size = MIN(size, P2END(off, zv->zv_volblocksize) - off);
11377837SMatthew.Ahrens@Sun.COM 			error = zvol_dumpio(zv, addr, off, size,
11387837SMatthew.Ahrens@Sun.COM 			    doread, B_FALSE);
11397837SMatthew.Ahrens@Sun.COM 		} else if (doread) {
11401861Sperrin 			error = dmu_read(os, ZVOL_OBJ, off, size, addr);
1141789Sahrens 		} else {
11421141Sperrin 			dmu_tx_t *tx = dmu_tx_create(os);
1143789Sahrens 			dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
1144789Sahrens 			error = dmu_tx_assign(tx, TXG_WAIT);
1145789Sahrens 			if (error) {
1146789Sahrens 				dmu_tx_abort(tx);
1147789Sahrens 			} else {
11481141Sperrin 				dmu_write(os, ZVOL_OBJ, off, size, addr, tx);
11493638Sbillm 				zvol_log_write(zv, tx, off, size);
1150789Sahrens 				dmu_tx_commit(tx);
1151789Sahrens 			}
1152789Sahrens 		}
11537294Sperrin 		if (error) {
11547294Sperrin 			/* convert checksum errors into IO errors */
11557294Sperrin 			if (error == ECKSUM)
11567294Sperrin 				error = EIO;
1157789Sahrens 			break;
11587294Sperrin 		}
1159789Sahrens 		off += size;
1160789Sahrens 		addr += size;
1161789Sahrens 		resid -= size;
1162789Sahrens 	}
11633755Sperrin 	zfs_range_unlock(rl);
1164789Sahrens 
1165789Sahrens 	if ((bp->b_resid = resid) == bp->b_bcount)
1166789Sahrens 		bioerror(bp, off > volsize ? EINVAL : error);
1167789Sahrens 
11687837SMatthew.Ahrens@Sun.COM 	if (!(bp->b_flags & B_ASYNC) && !doread && !zil_disable && !is_dump)
11693638Sbillm 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
11703638Sbillm 	biodone(bp);
11711141Sperrin 
1172789Sahrens 	return (0);
1173789Sahrens }
1174789Sahrens 
11753063Sperrin /*
11763063Sperrin  * Set the buffer count to the zvol maximum transfer.
11773063Sperrin  * Using our own routine instead of the default minphys()
11783063Sperrin  * means that for larger writes we write bigger buffers on X86
11793063Sperrin  * (128K instead of 56K) and flush the disk write cache less often
11803063Sperrin  * (every zvol_maxphys - currently 1MB) instead of minphys (currently
11813063Sperrin  * 56K on X86 and 128K on sparc).
11823063Sperrin  */
11833063Sperrin void
11843063Sperrin zvol_minphys(struct buf *bp)
11853063Sperrin {
11863063Sperrin 	if (bp->b_bcount > zvol_maxphys)
11873063Sperrin 		bp->b_bcount = zvol_maxphys;
11883063Sperrin }
11893063Sperrin 
11906423Sgw25295 int
11916423Sgw25295 zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks)
11926423Sgw25295 {
11936423Sgw25295 	minor_t minor = getminor(dev);
11946423Sgw25295 	zvol_state_t *zv;
11956423Sgw25295 	int error = 0;
11966423Sgw25295 	uint64_t size;
11976423Sgw25295 	uint64_t boff;
11986423Sgw25295 	uint64_t resid;
11996423Sgw25295 
12006423Sgw25295 	if (minor == 0)			/* This is the control device */
12016423Sgw25295 		return (ENXIO);
12026423Sgw25295 
12036423Sgw25295 	zv = ddi_get_soft_state(zvol_state, minor);
12046423Sgw25295 	if (zv == NULL)
12056423Sgw25295 		return (ENXIO);
12066423Sgw25295 
12076423Sgw25295 	boff = ldbtob(blkno);
12086423Sgw25295 	resid = ldbtob(nblocks);
12097837SMatthew.Ahrens@Sun.COM 
12107837SMatthew.Ahrens@Sun.COM 	VERIFY3U(boff + resid, <=, zv->zv_volsize);
12117837SMatthew.Ahrens@Sun.COM 
12126423Sgw25295 	while (resid) {
12136423Sgw25295 		size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff);
12147837SMatthew.Ahrens@Sun.COM 		error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE);
12156423Sgw25295 		if (error)
12166423Sgw25295 			break;
12176423Sgw25295 		boff += size;
12186423Sgw25295 		addr += size;
12196423Sgw25295 		resid -= size;
12206423Sgw25295 	}
12216423Sgw25295 
12226423Sgw25295 	return (error);
12236423Sgw25295 }
12246423Sgw25295 
1225789Sahrens /*ARGSUSED*/
1226789Sahrens int
12273638Sbillm zvol_read(dev_t dev, uio_t *uio, cred_t *cr)
1228789Sahrens {
12294107Sgw25295 	minor_t minor = getminor(dev);
12304107Sgw25295 	zvol_state_t *zv;
12317013Sgw25295 	uint64_t volsize;
12323755Sperrin 	rl_t *rl;
12333638Sbillm 	int error = 0;
12343638Sbillm 
12354107Sgw25295 	if (minor == 0)			/* This is the control device */
12364107Sgw25295 		return (ENXIO);
12374107Sgw25295 
12384107Sgw25295 	zv = ddi_get_soft_state(zvol_state, minor);
12394107Sgw25295 	if (zv == NULL)
12404107Sgw25295 		return (ENXIO);
12414107Sgw25295 
12427013Sgw25295 	volsize = zv->zv_volsize;
12437013Sgw25295 	if (uio->uio_resid > 0 &&
12447013Sgw25295 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
12457013Sgw25295 		return (EIO);
12467013Sgw25295 
12477837SMatthew.Ahrens@Sun.COM 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
12487837SMatthew.Ahrens@Sun.COM 		error = physio(zvol_strategy, NULL, dev, B_READ,
12497837SMatthew.Ahrens@Sun.COM 		    zvol_minphys, uio);
12507837SMatthew.Ahrens@Sun.COM 		return (error);
12517837SMatthew.Ahrens@Sun.COM 	}
12527837SMatthew.Ahrens@Sun.COM 
12533755Sperrin 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
12543755Sperrin 	    RL_READER);
12557013Sgw25295 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
12563638Sbillm 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
12573638Sbillm 
12587013Sgw25295 		/* don't read past the end */
12597013Sgw25295 		if (bytes > volsize - uio->uio_loffset)
12607013Sgw25295 			bytes = volsize - uio->uio_loffset;
12617013Sgw25295 
12623638Sbillm 		error =  dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes);
12637294Sperrin 		if (error) {
12647294Sperrin 			/* convert checksum errors into IO errors */
12657294Sperrin 			if (error == ECKSUM)
12667294Sperrin 				error = EIO;
12673638Sbillm 			break;
12687294Sperrin 		}
12693638Sbillm 	}
12703755Sperrin 	zfs_range_unlock(rl);
12713638Sbillm 	return (error);
1272789Sahrens }
1273789Sahrens 
1274789Sahrens /*ARGSUSED*/
1275789Sahrens int
12763638Sbillm zvol_write(dev_t dev, uio_t *uio, cred_t *cr)
1277789Sahrens {
12784107Sgw25295 	minor_t minor = getminor(dev);
12794107Sgw25295 	zvol_state_t *zv;
12807013Sgw25295 	uint64_t volsize;
12813755Sperrin 	rl_t *rl;
12823638Sbillm 	int error = 0;
12833638Sbillm 
12844107Sgw25295 	if (minor == 0)			/* This is the control device */
12854107Sgw25295 		return (ENXIO);
12864107Sgw25295 
12874107Sgw25295 	zv = ddi_get_soft_state(zvol_state, minor);
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 
12966423Sgw25295 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
12976423Sgw25295 		error = physio(zvol_strategy, NULL, dev, B_WRITE,
12986423Sgw25295 		    zvol_minphys, uio);
12996423Sgw25295 		return (error);
13006423Sgw25295 	}
13016423Sgw25295 
13023755Sperrin 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
13033755Sperrin 	    RL_WRITER);
13047013Sgw25295 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
13053638Sbillm 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
13063638Sbillm 		uint64_t off = uio->uio_loffset;
13077013Sgw25295 		dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
1308789Sahrens 
13097013Sgw25295 		if (bytes > volsize - off)	/* don't write past the end */
13107013Sgw25295 			bytes = volsize - off;
13117013Sgw25295 
13123638Sbillm 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
13133638Sbillm 		error = dmu_tx_assign(tx, TXG_WAIT);
13143638Sbillm 		if (error) {
13153638Sbillm 			dmu_tx_abort(tx);
13163638Sbillm 			break;
13173638Sbillm 		}
13183638Sbillm 		error = dmu_write_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes, tx);
13193638Sbillm 		if (error == 0)
13203638Sbillm 			zvol_log_write(zv, tx, off, bytes);
13213638Sbillm 		dmu_tx_commit(tx);
13223638Sbillm 
13233638Sbillm 		if (error)
13243638Sbillm 			break;
13253638Sbillm 	}
13263755Sperrin 	zfs_range_unlock(rl);
1327*8524SEric.Taylor@Sun.COM 	if (!zil_disable)
1328*8524SEric.Taylor@Sun.COM 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
13293638Sbillm 	return (error);
1330789Sahrens }
1331789Sahrens 
13327405SEric.Taylor@Sun.COM int
13337405SEric.Taylor@Sun.COM zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs)
13347405SEric.Taylor@Sun.COM {
13357405SEric.Taylor@Sun.COM 	struct uuid uuid = EFI_RESERVED;
13367405SEric.Taylor@Sun.COM 	efi_gpe_t gpe = { 0 };
13377405SEric.Taylor@Sun.COM 	uint32_t crc;
13387405SEric.Taylor@Sun.COM 	dk_efi_t efi;
13397405SEric.Taylor@Sun.COM 	int length;
13407405SEric.Taylor@Sun.COM 	char *ptr;
13417405SEric.Taylor@Sun.COM 
13427405SEric.Taylor@Sun.COM 	if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag))
13437405SEric.Taylor@Sun.COM 		return (EFAULT);
13447405SEric.Taylor@Sun.COM 	ptr = (char *)(uintptr_t)efi.dki_data_64;
13457405SEric.Taylor@Sun.COM 	length = efi.dki_length;
13467405SEric.Taylor@Sun.COM 	/*
13477405SEric.Taylor@Sun.COM 	 * Some clients may attempt to request a PMBR for the
13487405SEric.Taylor@Sun.COM 	 * zvol.  Currently this interface will return EINVAL to
13497405SEric.Taylor@Sun.COM 	 * such requests.  These requests could be supported by
13507405SEric.Taylor@Sun.COM 	 * adding a check for lba == 0 and consing up an appropriate
13517405SEric.Taylor@Sun.COM 	 * PMBR.
13527405SEric.Taylor@Sun.COM 	 */
13537405SEric.Taylor@Sun.COM 	if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0)
13547405SEric.Taylor@Sun.COM 		return (EINVAL);
13557405SEric.Taylor@Sun.COM 
13567405SEric.Taylor@Sun.COM 	gpe.efi_gpe_StartingLBA = LE_64(34ULL);
13577405SEric.Taylor@Sun.COM 	gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1);
13587405SEric.Taylor@Sun.COM 	UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
13597405SEric.Taylor@Sun.COM 
13607405SEric.Taylor@Sun.COM 	if (efi.dki_lba == 1) {
13617405SEric.Taylor@Sun.COM 		efi_gpt_t gpt = { 0 };
13627405SEric.Taylor@Sun.COM 
13637405SEric.Taylor@Sun.COM 		gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
13647405SEric.Taylor@Sun.COM 		gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
13657405SEric.Taylor@Sun.COM 		gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt));
13667405SEric.Taylor@Sun.COM 		gpt.efi_gpt_MyLBA = LE_64(1ULL);
13677405SEric.Taylor@Sun.COM 		gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL);
13687405SEric.Taylor@Sun.COM 		gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1);
13697405SEric.Taylor@Sun.COM 		gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL);
13707405SEric.Taylor@Sun.COM 		gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
13717405SEric.Taylor@Sun.COM 		gpt.efi_gpt_SizeOfPartitionEntry =
13727405SEric.Taylor@Sun.COM 		    LE_32(sizeof (efi_gpe_t));
13737405SEric.Taylor@Sun.COM 		CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
13747405SEric.Taylor@Sun.COM 		gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
13757405SEric.Taylor@Sun.COM 		CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table);
13767405SEric.Taylor@Sun.COM 		gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
13777405SEric.Taylor@Sun.COM 		if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length),
13787405SEric.Taylor@Sun.COM 		    flag))
13797405SEric.Taylor@Sun.COM 			return (EFAULT);
13807405SEric.Taylor@Sun.COM 		ptr += sizeof (gpt);
13817405SEric.Taylor@Sun.COM 		length -= sizeof (gpt);
13827405SEric.Taylor@Sun.COM 	}
13837405SEric.Taylor@Sun.COM 	if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe),
13847405SEric.Taylor@Sun.COM 	    length), flag))
13857405SEric.Taylor@Sun.COM 		return (EFAULT);
13867405SEric.Taylor@Sun.COM 	return (0);
13877405SEric.Taylor@Sun.COM }
13887405SEric.Taylor@Sun.COM 
1389789Sahrens /*
1390789Sahrens  * Dirtbag ioctls to support mkfs(1M) for UFS filesystems.  See dkio(7I).
1391789Sahrens  */
1392789Sahrens /*ARGSUSED*/
1393789Sahrens int
1394789Sahrens zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
1395789Sahrens {
1396789Sahrens 	zvol_state_t *zv;
13973897Smaybee 	struct dk_cinfo dki;
1398789Sahrens 	struct dk_minfo dkm;
13993897Smaybee 	struct dk_callback *dkc;
1400789Sahrens 	int error = 0;
14016423Sgw25295 	rl_t *rl;
1402789Sahrens 
1403789Sahrens 	mutex_enter(&zvol_state_lock);
1404789Sahrens 
1405789Sahrens 	zv = ddi_get_soft_state(zvol_state, getminor(dev));
1406789Sahrens 
1407789Sahrens 	if (zv == NULL) {
1408789Sahrens 		mutex_exit(&zvol_state_lock);
1409789Sahrens 		return (ENXIO);
1410789Sahrens 	}
1411789Sahrens 
1412789Sahrens 	switch (cmd) {
1413789Sahrens 
1414789Sahrens 	case DKIOCINFO:
14153897Smaybee 		bzero(&dki, sizeof (dki));
14163897Smaybee 		(void) strcpy(dki.dki_cname, "zvol");
14173897Smaybee 		(void) strcpy(dki.dki_dname, "zvol");
14183897Smaybee 		dki.dki_ctype = DKC_UNKNOWN;
14193897Smaybee 		dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs);
1420789Sahrens 		mutex_exit(&zvol_state_lock);
14213897Smaybee 		if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag))
1422789Sahrens 			error = EFAULT;
1423789Sahrens 		return (error);
1424789Sahrens 
1425789Sahrens 	case DKIOCGMEDIAINFO:
1426789Sahrens 		bzero(&dkm, sizeof (dkm));
1427789Sahrens 		dkm.dki_lbsize = 1U << zv->zv_min_bs;
1428789Sahrens 		dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
1429789Sahrens 		dkm.dki_media_type = DK_UNKNOWN;
1430789Sahrens 		mutex_exit(&zvol_state_lock);
1431789Sahrens 		if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
1432789Sahrens 			error = EFAULT;
1433789Sahrens 		return (error);
1434789Sahrens 
1435789Sahrens 	case DKIOCGETEFI:
14367405SEric.Taylor@Sun.COM 		{
14377405SEric.Taylor@Sun.COM 			uint64_t vs = zv->zv_volsize;
14387405SEric.Taylor@Sun.COM 			uint8_t bs = zv->zv_min_bs;
14393016Smaybee 
14403016Smaybee 			mutex_exit(&zvol_state_lock);
14417405SEric.Taylor@Sun.COM 			error = zvol_getefi((void *)arg, flag, vs, bs);
14427405SEric.Taylor@Sun.COM 			return (error);
14433016Smaybee 		}
1444789Sahrens 
14453638Sbillm 	case DKIOCFLUSHWRITECACHE:
14463897Smaybee 		dkc = (struct dk_callback *)arg;
14473638Sbillm 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
14483897Smaybee 		if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) {
14493897Smaybee 			(*dkc->dkc_callback)(dkc->dkc_cookie, error);
14503897Smaybee 			error = 0;
14513897Smaybee 		}
14523638Sbillm 		break;
14533638Sbillm 
14543245Smaybee 	case DKIOCGGEOM:
14553245Smaybee 	case DKIOCGVTOC:
14566423Sgw25295 		/*
14576423Sgw25295 		 * commands using these (like prtvtoc) expect ENOTSUP
14586423Sgw25295 		 * since we're emulating an EFI label
14596423Sgw25295 		 */
14603245Smaybee 		error = ENOTSUP;
14613245Smaybee 		break;
14623245Smaybee 
14636423Sgw25295 	case DKIOCDUMPINIT:
14646423Sgw25295 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
14656423Sgw25295 		    RL_WRITER);
14666423Sgw25295 		error = zvol_dumpify(zv);
14676423Sgw25295 		zfs_range_unlock(rl);
14686423Sgw25295 		break;
14696423Sgw25295 
14706423Sgw25295 	case DKIOCDUMPFINI:
14716423Sgw25295 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
14726423Sgw25295 		    RL_WRITER);
14736423Sgw25295 		error = zvol_dump_fini(zv);
14746423Sgw25295 		zfs_range_unlock(rl);
14756423Sgw25295 		break;
14766423Sgw25295 
1477789Sahrens 	default:
14783016Smaybee 		error = ENOTTY;
1479789Sahrens 		break;
1480789Sahrens 
1481789Sahrens 	}
1482789Sahrens 	mutex_exit(&zvol_state_lock);
1483789Sahrens 	return (error);
1484789Sahrens }
1485789Sahrens 
1486789Sahrens int
1487789Sahrens zvol_busy(void)
1488789Sahrens {
1489789Sahrens 	return (zvol_minors != 0);
1490789Sahrens }
1491789Sahrens 
1492789Sahrens void
1493789Sahrens zvol_init(void)
1494789Sahrens {
1495789Sahrens 	VERIFY(ddi_soft_state_init(&zvol_state, sizeof (zvol_state_t), 1) == 0);
1496789Sahrens 	mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
1497789Sahrens }
1498789Sahrens 
1499789Sahrens void
1500789Sahrens zvol_fini(void)
1501789Sahrens {
1502789Sahrens 	mutex_destroy(&zvol_state_lock);
1503789Sahrens 	ddi_soft_state_fini(&zvol_state);
1504789Sahrens }
15056423Sgw25295 
15066423Sgw25295 static boolean_t
15076423Sgw25295 zvol_is_swap(zvol_state_t *zv)
15086423Sgw25295 {
15096423Sgw25295 	vnode_t *vp;
15106423Sgw25295 	boolean_t ret = B_FALSE;
15116423Sgw25295 	char *devpath;
15126423Sgw25295 	size_t devpathlen;
15136423Sgw25295 	int error;
15146423Sgw25295 
15156423Sgw25295 	devpathlen = strlen(ZVOL_FULL_DEV_DIR) + strlen(zv->zv_name) + 1;
15166423Sgw25295 	devpath = kmem_alloc(devpathlen, KM_SLEEP);
15176423Sgw25295 	(void) sprintf(devpath, "%s%s", ZVOL_FULL_DEV_DIR, zv->zv_name);
15186423Sgw25295 	error = lookupname(devpath, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp);
15196423Sgw25295 	kmem_free(devpath, devpathlen);
15206423Sgw25295 
15216423Sgw25295 	ret = !error && IS_SWAPVP(common_specvp(vp));
15226423Sgw25295 
15236423Sgw25295 	if (vp != NULL)
15246423Sgw25295 		VN_RELE(vp);
15256423Sgw25295 
15266423Sgw25295 	return (ret);
15276423Sgw25295 }
15286423Sgw25295 
15296423Sgw25295 static int
15306423Sgw25295 zvol_dump_init(zvol_state_t *zv, boolean_t resize)
15316423Sgw25295 {
15326423Sgw25295 	dmu_tx_t *tx;
15336423Sgw25295 	int error = 0;
15346423Sgw25295 	objset_t *os = zv->zv_objset;
15356423Sgw25295 	nvlist_t *nv = NULL;
15366423Sgw25295 
15376423Sgw25295 	ASSERT(MUTEX_HELD(&zvol_state_lock));
15386423Sgw25295 
15396423Sgw25295 	tx = dmu_tx_create(os);
15406423Sgw25295 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
15416423Sgw25295 	error = dmu_tx_assign(tx, TXG_WAIT);
15426423Sgw25295 	if (error) {
15436423Sgw25295 		dmu_tx_abort(tx);
15446423Sgw25295 		return (error);
15456423Sgw25295 	}
15466423Sgw25295 
15476423Sgw25295 	/*
15486423Sgw25295 	 * If we are resizing the dump device then we only need to
15496423Sgw25295 	 * update the refreservation to match the newly updated
15506423Sgw25295 	 * zvolsize. Otherwise, we save off the original state of the
15516423Sgw25295 	 * zvol so that we can restore them if the zvol is ever undumpified.
15526423Sgw25295 	 */
15536423Sgw25295 	if (resize) {
15546423Sgw25295 		error = zap_update(os, ZVOL_ZAP_OBJ,
15556423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
15566423Sgw25295 		    &zv->zv_volsize, tx);
15576423Sgw25295 	} else {
15587837SMatthew.Ahrens@Sun.COM 		uint64_t checksum, compress, refresrv, vbs;
15597837SMatthew.Ahrens@Sun.COM 
15606423Sgw25295 		error = dsl_prop_get_integer(zv->zv_name,
15616423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL);
15626423Sgw25295 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
15636423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL);
15646423Sgw25295 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
15656423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL);
15667837SMatthew.Ahrens@Sun.COM 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
15677837SMatthew.Ahrens@Sun.COM 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL);
15686423Sgw25295 
15696423Sgw25295 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
15706423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1,
15716423Sgw25295 		    &compress, tx);
15726423Sgw25295 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
15736423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx);
15746423Sgw25295 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
15756423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
15766423Sgw25295 		    &refresrv, tx);
15777837SMatthew.Ahrens@Sun.COM 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
15787837SMatthew.Ahrens@Sun.COM 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1,
15797837SMatthew.Ahrens@Sun.COM 		    &vbs, tx);
15806423Sgw25295 	}
15816423Sgw25295 	dmu_tx_commit(tx);
15826423Sgw25295 
15836423Sgw25295 	/* Truncate the file */
15846423Sgw25295 	if (!error)
15856992Smaybee 		error = dmu_free_long_range(zv->zv_objset,
15866992Smaybee 		    ZVOL_OBJ, 0, DMU_OBJECT_END);
15876423Sgw25295 
15886423Sgw25295 	if (error)
15896423Sgw25295 		return (error);
15906423Sgw25295 
15916423Sgw25295 	/*
15926423Sgw25295 	 * We only need update the zvol's property if we are initializing
15936423Sgw25295 	 * the dump area for the first time.
15946423Sgw25295 	 */
15956423Sgw25295 	if (!resize) {
15966423Sgw25295 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
15976423Sgw25295 		VERIFY(nvlist_add_uint64(nv,
15986423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0);
15996423Sgw25295 		VERIFY(nvlist_add_uint64(nv,
16006423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
16016423Sgw25295 		    ZIO_COMPRESS_OFF) == 0);
16026423Sgw25295 		VERIFY(nvlist_add_uint64(nv,
16036423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
16046423Sgw25295 		    ZIO_CHECKSUM_OFF) == 0);
16057837SMatthew.Ahrens@Sun.COM 		VERIFY(nvlist_add_uint64(nv,
16067837SMatthew.Ahrens@Sun.COM 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
16077837SMatthew.Ahrens@Sun.COM 		    SPA_MAXBLOCKSIZE) == 0);
16086423Sgw25295 
16096423Sgw25295 		error = zfs_set_prop_nvlist(zv->zv_name, nv);
16106423Sgw25295 		nvlist_free(nv);
16116423Sgw25295 
16126423Sgw25295 		if (error)
16136423Sgw25295 			return (error);
16146423Sgw25295 	}
16156423Sgw25295 
16166423Sgw25295 	/* Allocate the space for the dump */
16176423Sgw25295 	error = zvol_prealloc(zv);
16186423Sgw25295 	return (error);
16196423Sgw25295 }
16206423Sgw25295 
16216423Sgw25295 static int
16226423Sgw25295 zvol_dumpify(zvol_state_t *zv)
16236423Sgw25295 {
16246423Sgw25295 	int error = 0;
16256423Sgw25295 	uint64_t dumpsize = 0;
16266423Sgw25295 	dmu_tx_t *tx;
16276423Sgw25295 	objset_t *os = zv->zv_objset;
16286423Sgw25295 
16296423Sgw25295 	if (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY))
16306423Sgw25295 		return (EROFS);
16316423Sgw25295 
16326423Sgw25295 	/*
16336423Sgw25295 	 * We do not support swap devices acting as dump devices.
16346423Sgw25295 	 */
16356423Sgw25295 	if (zvol_is_swap(zv))
16366423Sgw25295 		return (ENOTSUP);
16376423Sgw25295 
16386423Sgw25295 	if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE,
16396423Sgw25295 	    8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) {
16406423Sgw25295 		boolean_t resize = (dumpsize > 0) ? B_TRUE : B_FALSE;
16416423Sgw25295 
16426423Sgw25295 		if ((error = zvol_dump_init(zv, resize)) != 0) {
16436423Sgw25295 			(void) zvol_dump_fini(zv);
16446423Sgw25295 			return (error);
16456423Sgw25295 		}
16466423Sgw25295 	}
16476423Sgw25295 
16486423Sgw25295 	/*
16496423Sgw25295 	 * Build up our lba mapping.
16506423Sgw25295 	 */
16516423Sgw25295 	error = zvol_get_lbas(zv);
16526423Sgw25295 	if (error) {
16536423Sgw25295 		(void) zvol_dump_fini(zv);
16546423Sgw25295 		return (error);
16556423Sgw25295 	}
16566423Sgw25295 
16576423Sgw25295 	tx = dmu_tx_create(os);
16586423Sgw25295 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
16596423Sgw25295 	error = dmu_tx_assign(tx, TXG_WAIT);
16606423Sgw25295 	if (error) {
16616423Sgw25295 		dmu_tx_abort(tx);
16626423Sgw25295 		(void) zvol_dump_fini(zv);
16636423Sgw25295 		return (error);
16646423Sgw25295 	}
16656423Sgw25295 
16666423Sgw25295 	zv->zv_flags |= ZVOL_DUMPIFIED;
16676423Sgw25295 	error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1,
16686423Sgw25295 	    &zv->zv_volsize, tx);
16696423Sgw25295 	dmu_tx_commit(tx);
16706423Sgw25295 
16716423Sgw25295 	if (error) {
16726423Sgw25295 		(void) zvol_dump_fini(zv);
16736423Sgw25295 		return (error);
16746423Sgw25295 	}
16756423Sgw25295 
16766423Sgw25295 	txg_wait_synced(dmu_objset_pool(os), 0);
16776423Sgw25295 	return (0);
16786423Sgw25295 }
16796423Sgw25295 
16806423Sgw25295 static int
16816423Sgw25295 zvol_dump_fini(zvol_state_t *zv)
16826423Sgw25295 {
16836423Sgw25295 	dmu_tx_t *tx;
16846423Sgw25295 	objset_t *os = zv->zv_objset;
16856423Sgw25295 	nvlist_t *nv;
16866423Sgw25295 	int error = 0;
16877837SMatthew.Ahrens@Sun.COM 	uint64_t checksum, compress, refresrv, vbs;
16886423Sgw25295 
16897080Smaybee 	/*
16907080Smaybee 	 * Attempt to restore the zvol back to its pre-dumpified state.
16917080Smaybee 	 * This is a best-effort attempt as it's possible that not all
16927080Smaybee 	 * of these properties were initialized during the dumpify process
16937080Smaybee 	 * (i.e. error during zvol_dump_init).
16947080Smaybee 	 */
16957080Smaybee 
16966423Sgw25295 	tx = dmu_tx_create(os);
16976423Sgw25295 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
16986423Sgw25295 	error = dmu_tx_assign(tx, TXG_WAIT);
16996423Sgw25295 	if (error) {
17006423Sgw25295 		dmu_tx_abort(tx);
17016423Sgw25295 		return (error);
17026423Sgw25295 	}
17037080Smaybee 	(void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx);
17047080Smaybee 	dmu_tx_commit(tx);
17056423Sgw25295 
17066423Sgw25295 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
17076423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum);
17086423Sgw25295 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
17096423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress);
17106423Sgw25295 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
17116423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv);
17127837SMatthew.Ahrens@Sun.COM 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
17137837SMatthew.Ahrens@Sun.COM 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs);
17146423Sgw25295 
17156423Sgw25295 	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
17166423Sgw25295 	(void) nvlist_add_uint64(nv,
17176423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum);
17186423Sgw25295 	(void) nvlist_add_uint64(nv,
17196423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress);
17206423Sgw25295 	(void) nvlist_add_uint64(nv,
17216423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv);
17227837SMatthew.Ahrens@Sun.COM 	(void) nvlist_add_uint64(nv,
17237837SMatthew.Ahrens@Sun.COM 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), vbs);
17246423Sgw25295 	(void) zfs_set_prop_nvlist(zv->zv_name, nv);
17256423Sgw25295 	nvlist_free(nv);
17266423Sgw25295 
17277080Smaybee 	zvol_free_extents(zv);
17287080Smaybee 	zv->zv_flags &= ~ZVOL_DUMPIFIED;
17297080Smaybee 	(void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END);
17307080Smaybee 
17316423Sgw25295 	return (0);
17326423Sgw25295 }
1733