xref: /onnv-gate/usr/src/uts/common/fs/zfs/zvol.c (revision 7837:001de5627df3)
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 /*
226423Sgw25295  * Copyright 2008 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>
78789Sahrens 
79789Sahrens #include "zfs_namecheck.h"
80789Sahrens 
816423Sgw25295 static void *zvol_state;
82789Sahrens 
836423Sgw25295 #define	ZVOL_DUMPSIZE		"dumpsize"
84789Sahrens 
85789Sahrens /*
86789Sahrens  * This lock protects the zvol_state structure from being modified
87789Sahrens  * while it's being used, e.g. an open that comes in before a create
88789Sahrens  * finishes.  It also protects temporary opens of the dataset so that,
89789Sahrens  * e.g., an open doesn't get a spurious EBUSY.
90789Sahrens  */
91789Sahrens static kmutex_t zvol_state_lock;
92789Sahrens static uint32_t zvol_minors;
93789Sahrens 
946423Sgw25295 typedef struct zvol_extent {
95*7837SMatthew.Ahrens@Sun.COM 	list_node_t	ze_node;
966423Sgw25295 	dva_t		ze_dva;		/* dva associated with this extent */
97*7837SMatthew.Ahrens@Sun.COM 	uint64_t	ze_nblks;	/* number of blocks in extent */
986423Sgw25295 } zvol_extent_t;
996423Sgw25295 
1006423Sgw25295 /*
101789Sahrens  * The in-core state of each volume.
102789Sahrens  */
103789Sahrens typedef struct zvol_state {
104789Sahrens 	char		zv_name[MAXPATHLEN]; /* pool/dd name */
105789Sahrens 	uint64_t	zv_volsize;	/* amount of space we advertise */
1063063Sperrin 	uint64_t	zv_volblocksize; /* volume block size */
107789Sahrens 	minor_t		zv_minor;	/* minor number */
108789Sahrens 	uint8_t		zv_min_bs;	/* minimum addressable block shift */
1096423Sgw25295 	uint8_t		zv_flags;	/* readonly; dumpified */
110789Sahrens 	objset_t	*zv_objset;	/* objset handle */
111789Sahrens 	uint32_t	zv_mode;	/* DS_MODE_* flags at open time */
112789Sahrens 	uint32_t	zv_open_count[OTYPCNT];	/* open counts */
113789Sahrens 	uint32_t	zv_total_opens;	/* total open count */
1141141Sperrin 	zilog_t		*zv_zilog;	/* ZIL handle */
115*7837SMatthew.Ahrens@Sun.COM 	list_t		zv_extents;	/* List of extents for dump */
1161141Sperrin 	uint64_t	zv_txg_assign;	/* txg to assign during ZIL replay */
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 {
253*7837SMatthew.Ahrens@Sun.COM 	zvol_state_t	*ma_zv;
254*7837SMatthew.Ahrens@Sun.COM 	uint64_t	ma_blks;
2556423Sgw25295 };
2566423Sgw25295 
2576423Sgw25295 /*ARGSUSED*/
2586423Sgw25295 static int
259*7837SMatthew.Ahrens@Sun.COM zvol_map_block(spa_t *spa, blkptr_t *bp, const zbookmark_t *zb,
260*7837SMatthew.Ahrens@Sun.COM     const dnode_phys_t *dnp, void *arg)
2616423Sgw25295 {
262*7837SMatthew.Ahrens@Sun.COM 	struct maparg *ma = arg;
263*7837SMatthew.Ahrens@Sun.COM 	zvol_extent_t *ze;
264*7837SMatthew.Ahrens@Sun.COM 	int bs = ma->ma_zv->zv_volblocksize;
2656423Sgw25295 
266*7837SMatthew.Ahrens@Sun.COM 	if (bp == NULL || zb->zb_object != ZVOL_OBJ || zb->zb_level != 0)
2676423Sgw25295 		return (0);
2686423Sgw25295 
269*7837SMatthew.Ahrens@Sun.COM 	VERIFY3U(ma->ma_blks, ==, zb->zb_blkid);
270*7837SMatthew.Ahrens@Sun.COM 	ma->ma_blks++;
271*7837SMatthew.Ahrens@Sun.COM 
2726423Sgw25295 	/* Abort immediately if we have encountered gang blocks */
273*7837SMatthew.Ahrens@Sun.COM 	if (BP_IS_GANG(bp))
274*7837SMatthew.Ahrens@Sun.COM 		return (EFRAGS);
2756423Sgw25295 
276*7837SMatthew.Ahrens@Sun.COM 	/*
277*7837SMatthew.Ahrens@Sun.COM 	 * See if the block is at the end of the previous extent.
278*7837SMatthew.Ahrens@Sun.COM 	 */
279*7837SMatthew.Ahrens@Sun.COM 	ze = list_tail(&ma->ma_zv->zv_extents);
280*7837SMatthew.Ahrens@Sun.COM 	if (ze &&
281*7837SMatthew.Ahrens@Sun.COM 	    DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) &&
282*7837SMatthew.Ahrens@Sun.COM 	    DVA_GET_OFFSET(BP_IDENTITY(bp)) ==
283*7837SMatthew.Ahrens@Sun.COM 	    DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) {
284*7837SMatthew.Ahrens@Sun.COM 		ze->ze_nblks++;
2856423Sgw25295 		return (0);
2866423Sgw25295 	}
2876423Sgw25295 
288*7837SMatthew.Ahrens@Sun.COM 	dprintf_bp(bp, "%s", "next blkptr:");
289*7837SMatthew.Ahrens@Sun.COM 
290*7837SMatthew.Ahrens@Sun.COM 	/* start a new extent */
291*7837SMatthew.Ahrens@Sun.COM 	ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP);
292*7837SMatthew.Ahrens@Sun.COM 	ze->ze_dva = bp->blk_dva[0];	/* structure assignment */
293*7837SMatthew.Ahrens@Sun.COM 	ze->ze_nblks = 1;
294*7837SMatthew.Ahrens@Sun.COM 	list_insert_tail(&ma->ma_zv->zv_extents, ze);
295*7837SMatthew.Ahrens@Sun.COM 	return (0);
296*7837SMatthew.Ahrens@Sun.COM }
297*7837SMatthew.Ahrens@Sun.COM 
298*7837SMatthew.Ahrens@Sun.COM static void
299*7837SMatthew.Ahrens@Sun.COM zvol_free_extents(zvol_state_t *zv)
300*7837SMatthew.Ahrens@Sun.COM {
301*7837SMatthew.Ahrens@Sun.COM 	zvol_extent_t *ze;
302*7837SMatthew.Ahrens@Sun.COM 
303*7837SMatthew.Ahrens@Sun.COM 	while (ze = list_head(&zv->zv_extents)) {
304*7837SMatthew.Ahrens@Sun.COM 		list_remove(&zv->zv_extents, ze);
305*7837SMatthew.Ahrens@Sun.COM 		kmem_free(ze, sizeof (zvol_extent_t));
306*7837SMatthew.Ahrens@Sun.COM 	}
307*7837SMatthew.Ahrens@Sun.COM }
308*7837SMatthew.Ahrens@Sun.COM 
309*7837SMatthew.Ahrens@Sun.COM static int
310*7837SMatthew.Ahrens@Sun.COM zvol_get_lbas(zvol_state_t *zv)
311*7837SMatthew.Ahrens@Sun.COM {
312*7837SMatthew.Ahrens@Sun.COM 	struct maparg	ma;
313*7837SMatthew.Ahrens@Sun.COM 	int		err;
314*7837SMatthew.Ahrens@Sun.COM 
315*7837SMatthew.Ahrens@Sun.COM 	ma.ma_zv = zv;
316*7837SMatthew.Ahrens@Sun.COM 	ma.ma_blks = 0;
317*7837SMatthew.Ahrens@Sun.COM 	zvol_free_extents(zv);
318*7837SMatthew.Ahrens@Sun.COM 
319*7837SMatthew.Ahrens@Sun.COM 	err = traverse_dataset(dmu_objset_ds(zv->zv_objset), 0,
320*7837SMatthew.Ahrens@Sun.COM 	    TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma);
321*7837SMatthew.Ahrens@Sun.COM 	if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) {
322*7837SMatthew.Ahrens@Sun.COM 		zvol_free_extents(zv);
323*7837SMatthew.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);
3841141Sperrin 	error = dmu_tx_assign(tx, zv->zv_txg_assign);
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));
554*7837SMatthew.Ahrens@Sun.COM 	list_create(&zv->zv_extents, sizeof (zvol_extent_t),
555*7837SMatthew.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 
5617638SNeil.Perrin@Sun.COM 	zil_replay(os, zv, &zv->zv_txg_assign, zvol_replay_vector, NULL);
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 	void *data;
6276423Sgw25295 	uint64_t refd, avail, usedobjs, availobjs;
6286423Sgw25295 	uint64_t resid = zv->zv_volsize;
6296423Sgw25295 	uint64_t off = 0;
6306423Sgw25295 
6316423Sgw25295 	/* Check the space usage before attempting to allocate the space */
6326423Sgw25295 	dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs);
6336423Sgw25295 	if (avail < zv->zv_volsize)
6346423Sgw25295 		return (ENOSPC);
6356423Sgw25295 
6366423Sgw25295 	/* Free old extents if they exist */
6376423Sgw25295 	zvol_free_extents(zv);
6386423Sgw25295 
6396423Sgw25295 	/* allocate the blocks by writing each one */
6406423Sgw25295 	data = kmem_zalloc(SPA_MAXBLOCKSIZE, KM_SLEEP);
6416423Sgw25295 
6426423Sgw25295 	while (resid != 0) {
6436423Sgw25295 		int error;
6446423Sgw25295 		uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE);
6456423Sgw25295 
6466423Sgw25295 		tx = dmu_tx_create(os);
6476423Sgw25295 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
6486423Sgw25295 		error = dmu_tx_assign(tx, TXG_WAIT);
6496423Sgw25295 		if (error) {
6506423Sgw25295 			dmu_tx_abort(tx);
6516423Sgw25295 			kmem_free(data, SPA_MAXBLOCKSIZE);
6526992Smaybee 			(void) dmu_free_long_range(os, ZVOL_OBJ, 0, off);
6536423Sgw25295 			return (error);
6546423Sgw25295 		}
6556423Sgw25295 		dmu_write(os, ZVOL_OBJ, off, bytes, data, tx);
6566423Sgw25295 		dmu_tx_commit(tx);
6576423Sgw25295 		off += bytes;
6586423Sgw25295 		resid -= bytes;
6596423Sgw25295 	}
6606423Sgw25295 	kmem_free(data, SPA_MAXBLOCKSIZE);
6616423Sgw25295 	txg_wait_synced(dmu_objset_pool(os), 0);
6626423Sgw25295 
6636423Sgw25295 	return (0);
6646423Sgw25295 }
6656423Sgw25295 
6666423Sgw25295 int
6676423Sgw25295 zvol_update_volsize(zvol_state_t *zv, major_t maj, uint64_t volsize)
6686423Sgw25295 {
6696423Sgw25295 	dmu_tx_t *tx;
6706423Sgw25295 	int error;
6716423Sgw25295 
6726423Sgw25295 	ASSERT(MUTEX_HELD(&zvol_state_lock));
6736423Sgw25295 
6746423Sgw25295 	tx = dmu_tx_create(zv->zv_objset);
6756423Sgw25295 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
6766423Sgw25295 	error = dmu_tx_assign(tx, TXG_WAIT);
6776423Sgw25295 	if (error) {
6786423Sgw25295 		dmu_tx_abort(tx);
6796423Sgw25295 		return (error);
6806423Sgw25295 	}
6816423Sgw25295 
6826423Sgw25295 	error = zap_update(zv->zv_objset, ZVOL_ZAP_OBJ, "size", 8, 1,
6836423Sgw25295 	    &volsize, tx);
6846423Sgw25295 	dmu_tx_commit(tx);
6856423Sgw25295 
6866423Sgw25295 	if (error == 0)
6876992Smaybee 		error = dmu_free_long_range(zv->zv_objset,
6886992Smaybee 		    ZVOL_OBJ, volsize, DMU_OBJECT_END);
6896423Sgw25295 
6907265Sahrens 	/*
6917265Sahrens 	 * If we are using a faked-up state (zv_minor == 0) then don't
6927265Sahrens 	 * try to update the in-core zvol state.
6937265Sahrens 	 */
6947265Sahrens 	if (error == 0 && zv->zv_minor) {
6956423Sgw25295 		zv->zv_volsize = volsize;
6966423Sgw25295 		zvol_size_changed(zv, maj);
6976423Sgw25295 	}
6986423Sgw25295 	return (error);
6996423Sgw25295 }
7006423Sgw25295 
701789Sahrens int
7024787Sahrens zvol_set_volsize(const char *name, major_t maj, uint64_t volsize)
703789Sahrens {
704789Sahrens 	zvol_state_t *zv;
705789Sahrens 	int error;
7061133Seschrock 	dmu_object_info_t doi;
7076423Sgw25295 	uint64_t old_volsize = 0ULL;
7087265Sahrens 	zvol_state_t state = { 0 };
709789Sahrens 
710789Sahrens 	mutex_enter(&zvol_state_lock);
711789Sahrens 
7122676Seschrock 	if ((zv = zvol_minor_lookup(name)) == NULL) {
7137265Sahrens 		/*
7147265Sahrens 		 * If we are doing a "zfs clone -o volsize=", then the
7157265Sahrens 		 * minor node won't exist yet.
7167265Sahrens 		 */
7177265Sahrens 		error = dmu_objset_open(name, DMU_OST_ZVOL, DS_MODE_OWNER,
7187265Sahrens 		    &state.zv_objset);
7197265Sahrens 		if (error != 0)
7207265Sahrens 			goto out;
7217265Sahrens 		zv = &state;
722789Sahrens 	}
7236423Sgw25295 	old_volsize = zv->zv_volsize;
724789Sahrens 
7251133Seschrock 	if ((error = dmu_object_info(zv->zv_objset, ZVOL_OBJ, &doi)) != 0 ||
7262676Seschrock 	    (error = zvol_check_volsize(volsize,
7277265Sahrens 	    doi.doi_data_block_size)) != 0)
7287265Sahrens 		goto out;
7291133Seschrock 
7306423Sgw25295 	if (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY)) {
7317265Sahrens 		error = EROFS;
7327265Sahrens 		goto out;
733789Sahrens 	}
734789Sahrens 
7356423Sgw25295 	error = zvol_update_volsize(zv, maj, volsize);
736789Sahrens 
7376423Sgw25295 	/*
7386423Sgw25295 	 * Reinitialize the dump area to the new size. If we
7396423Sgw25295 	 * failed to resize the dump area then restore the it back to
7406423Sgw25295 	 * it's original size.
7416423Sgw25295 	 */
7426423Sgw25295 	if (error == 0 && zv->zv_flags & ZVOL_DUMPIFIED) {
7436423Sgw25295 		if ((error = zvol_dumpify(zv)) != 0 ||
7446423Sgw25295 		    (error = dumpvp_resize()) != 0) {
7456423Sgw25295 			(void) zvol_update_volsize(zv, maj, old_volsize);
7466423Sgw25295 			error = zvol_dumpify(zv);
7476423Sgw25295 		}
748789Sahrens 	}
749789Sahrens 
7507265Sahrens out:
7517265Sahrens 	if (state.zv_objset)
7527265Sahrens 		dmu_objset_close(state.zv_objset);
7537265Sahrens 
754789Sahrens 	mutex_exit(&zvol_state_lock);
755789Sahrens 
756789Sahrens 	return (error);
757789Sahrens }
758789Sahrens 
759789Sahrens int
7602676Seschrock zvol_set_volblocksize(const char *name, uint64_t volblocksize)
761789Sahrens {
762789Sahrens 	zvol_state_t *zv;
763789Sahrens 	dmu_tx_t *tx;
764789Sahrens 	int error;
765*7837SMatthew.Ahrens@Sun.COM 	boolean_t needlock;
766789Sahrens 
767*7837SMatthew.Ahrens@Sun.COM 	/*
768*7837SMatthew.Ahrens@Sun.COM 	 * The lock may already be held if we are being called from
769*7837SMatthew.Ahrens@Sun.COM 	 * zvol_dump_init().
770*7837SMatthew.Ahrens@Sun.COM 	 */
771*7837SMatthew.Ahrens@Sun.COM 	needlock = !MUTEX_HELD(&zvol_state_lock);
772*7837SMatthew.Ahrens@Sun.COM 	if (needlock)
773*7837SMatthew.Ahrens@Sun.COM 		mutex_enter(&zvol_state_lock);
774789Sahrens 
7752676Seschrock 	if ((zv = zvol_minor_lookup(name)) == NULL) {
776*7837SMatthew.Ahrens@Sun.COM 		if (needlock)
777*7837SMatthew.Ahrens@Sun.COM 			mutex_exit(&zvol_state_lock);
778789Sahrens 		return (ENXIO);
779789Sahrens 	}
7806423Sgw25295 	if (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY)) {
781*7837SMatthew.Ahrens@Sun.COM 		if (needlock)
782*7837SMatthew.Ahrens@Sun.COM 			mutex_exit(&zvol_state_lock);
783789Sahrens 		return (EROFS);
784789Sahrens 	}
785789Sahrens 
786789Sahrens 	tx = dmu_tx_create(zv->zv_objset);
787789Sahrens 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
788789Sahrens 	error = dmu_tx_assign(tx, TXG_WAIT);
789789Sahrens 	if (error) {
790789Sahrens 		dmu_tx_abort(tx);
791789Sahrens 	} else {
792789Sahrens 		error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ,
7932676Seschrock 		    volblocksize, 0, tx);
794789Sahrens 		if (error == ENOTSUP)
795789Sahrens 			error = EBUSY;
796789Sahrens 		dmu_tx_commit(tx);
797*7837SMatthew.Ahrens@Sun.COM 		if (error == 0)
798*7837SMatthew.Ahrens@Sun.COM 			zv->zv_volblocksize = volblocksize;
799789Sahrens 	}
800789Sahrens 
801*7837SMatthew.Ahrens@Sun.COM 	if (needlock)
802*7837SMatthew.Ahrens@Sun.COM 		mutex_exit(&zvol_state_lock);
803789Sahrens 
804789Sahrens 	return (error);
805789Sahrens }
806789Sahrens 
807789Sahrens /*ARGSUSED*/
808789Sahrens int
809789Sahrens zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr)
810789Sahrens {
811789Sahrens 	minor_t minor = getminor(*devp);
812789Sahrens 	zvol_state_t *zv;
813789Sahrens 
814789Sahrens 	if (minor == 0)			/* This is the control device */
815789Sahrens 		return (0);
816789Sahrens 
817789Sahrens 	mutex_enter(&zvol_state_lock);
818789Sahrens 
819789Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
820789Sahrens 	if (zv == NULL) {
821789Sahrens 		mutex_exit(&zvol_state_lock);
822789Sahrens 		return (ENXIO);
823789Sahrens 	}
824789Sahrens 
825789Sahrens 	ASSERT(zv->zv_objset != NULL);
826789Sahrens 
827789Sahrens 	if ((flag & FWRITE) &&
8286423Sgw25295 	    (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY))) {
829789Sahrens 		mutex_exit(&zvol_state_lock);
830789Sahrens 		return (EROFS);
831789Sahrens 	}
8327405SEric.Taylor@Sun.COM 	if (zv->zv_flags & ZVOL_EXCL) {
8337405SEric.Taylor@Sun.COM 		mutex_exit(&zvol_state_lock);
8347405SEric.Taylor@Sun.COM 		return (EBUSY);
8357405SEric.Taylor@Sun.COM 	}
8367405SEric.Taylor@Sun.COM 	if (flag & FEXCL) {
8377405SEric.Taylor@Sun.COM 		if (zv->zv_total_opens != 0) {
8387405SEric.Taylor@Sun.COM 			mutex_exit(&zvol_state_lock);
8397405SEric.Taylor@Sun.COM 			return (EBUSY);
8407405SEric.Taylor@Sun.COM 		}
8417405SEric.Taylor@Sun.COM 		zv->zv_flags |= ZVOL_EXCL;
8427405SEric.Taylor@Sun.COM 	}
843789Sahrens 
844789Sahrens 	if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) {
845789Sahrens 		zv->zv_open_count[otyp]++;
846789Sahrens 		zv->zv_total_opens++;
847789Sahrens 	}
848789Sahrens 
849789Sahrens 	mutex_exit(&zvol_state_lock);
850789Sahrens 
851789Sahrens 	return (0);
852789Sahrens }
853789Sahrens 
854789Sahrens /*ARGSUSED*/
855789Sahrens int
856789Sahrens zvol_close(dev_t dev, int flag, int otyp, cred_t *cr)
857789Sahrens {
858789Sahrens 	minor_t minor = getminor(dev);
859789Sahrens 	zvol_state_t *zv;
860789Sahrens 
861789Sahrens 	if (minor == 0)		/* This is the control device */
862789Sahrens 		return (0);
863789Sahrens 
864789Sahrens 	mutex_enter(&zvol_state_lock);
865789Sahrens 
866789Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
867789Sahrens 	if (zv == NULL) {
868789Sahrens 		mutex_exit(&zvol_state_lock);
869789Sahrens 		return (ENXIO);
870789Sahrens 	}
871789Sahrens 
8727405SEric.Taylor@Sun.COM 	if (zv->zv_flags & ZVOL_EXCL) {
8737405SEric.Taylor@Sun.COM 		ASSERT(zv->zv_total_opens == 1);
8747405SEric.Taylor@Sun.COM 		zv->zv_flags &= ~ZVOL_EXCL;
875789Sahrens 	}
876789Sahrens 
877789Sahrens 	/*
878789Sahrens 	 * If the open count is zero, this is a spurious close.
879789Sahrens 	 * That indicates a bug in the kernel / DDI framework.
880789Sahrens 	 */
881789Sahrens 	ASSERT(zv->zv_open_count[otyp] != 0);
882789Sahrens 	ASSERT(zv->zv_total_opens != 0);
883789Sahrens 
884789Sahrens 	/*
885789Sahrens 	 * You may get multiple opens, but only one close.
886789Sahrens 	 */
887789Sahrens 	zv->zv_open_count[otyp]--;
888789Sahrens 	zv->zv_total_opens--;
889789Sahrens 
890789Sahrens 	mutex_exit(&zvol_state_lock);
891789Sahrens 
892789Sahrens 	return (0);
893789Sahrens }
894789Sahrens 
8953638Sbillm static void
8963063Sperrin zvol_get_done(dmu_buf_t *db, void *vzgd)
8973063Sperrin {
8983063Sperrin 	zgd_t *zgd = (zgd_t *)vzgd;
8993755Sperrin 	rl_t *rl = zgd->zgd_rl;
9003063Sperrin 
9013063Sperrin 	dmu_buf_rele(db, vzgd);
9023755Sperrin 	zfs_range_unlock(rl);
9035688Sbonwick 	zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
9043063Sperrin 	kmem_free(zgd, sizeof (zgd_t));
9053063Sperrin }
9063063Sperrin 
9073063Sperrin /*
9083063Sperrin  * Get data to generate a TX_WRITE intent log record.
9093063Sperrin  */
9103638Sbillm static int
9113063Sperrin zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
9123063Sperrin {
9133063Sperrin 	zvol_state_t *zv = arg;
9143063Sperrin 	objset_t *os = zv->zv_objset;
9153063Sperrin 	dmu_buf_t *db;
9163755Sperrin 	rl_t *rl;
9173063Sperrin 	zgd_t *zgd;
9183755Sperrin 	uint64_t boff; 			/* block starting offset */
9193755Sperrin 	int dlen = lr->lr_length;	/* length of user data */
9203063Sperrin 	int error;
9213063Sperrin 
9223063Sperrin 	ASSERT(zio);
9233755Sperrin 	ASSERT(dlen != 0);
9243638Sbillm 
9253755Sperrin 	/*
9263755Sperrin 	 * Write records come in two flavors: immediate and indirect.
9273755Sperrin 	 * For small writes it's cheaper to store the data with the
9283755Sperrin 	 * log record (immediate); for large writes it's cheaper to
9293755Sperrin 	 * sync the data and get a pointer to it (indirect) so that
9303755Sperrin 	 * we don't have to write the data twice.
9313755Sperrin 	 */
9323755Sperrin 	if (buf != NULL) /* immediate write */
9333755Sperrin 		return (dmu_read(os, ZVOL_OBJ, lr->lr_offset, dlen, buf));
9343063Sperrin 
9353063Sperrin 	zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP);
9363063Sperrin 	zgd->zgd_zilog = zv->zv_zilog;
9373063Sperrin 	zgd->zgd_bp = &lr->lr_blkptr;
9383063Sperrin 
9393755Sperrin 	/*
9403755Sperrin 	 * Lock the range of the block to ensure that when the data is
9416423Sgw25295 	 * written out and its checksum is being calculated that no other
9423755Sperrin 	 * thread can change the block.
9433755Sperrin 	 */
9443755Sperrin 	boff = P2ALIGN_TYPED(lr->lr_offset, zv->zv_volblocksize, uint64_t);
9453755Sperrin 	rl = zfs_range_lock(&zv->zv_znode, boff, zv->zv_volblocksize,
9463755Sperrin 	    RL_READER);
9473755Sperrin 	zgd->zgd_rl = rl;
9483755Sperrin 
9493063Sperrin 	VERIFY(0 == dmu_buf_hold(os, ZVOL_OBJ, lr->lr_offset, zgd, &db));
9503063Sperrin 	error = dmu_sync(zio, db, &lr->lr_blkptr,
9513063Sperrin 	    lr->lr_common.lrc_txg, zvol_get_done, zgd);
9523638Sbillm 	if (error == 0)
9535688Sbonwick 		zil_add_block(zv->zv_zilog, &lr->lr_blkptr);
9543063Sperrin 	/*
9553063Sperrin 	 * If we get EINPROGRESS, then we need to wait for a
9563063Sperrin 	 * write IO initiated by dmu_sync() to complete before
9573063Sperrin 	 * we can release this dbuf.  We will finish everything
9583063Sperrin 	 * up in the zvol_get_done() callback.
9593063Sperrin 	 */
9603063Sperrin 	if (error == EINPROGRESS)
9613063Sperrin 		return (0);
9623063Sperrin 	dmu_buf_rele(db, zgd);
9633755Sperrin 	zfs_range_unlock(rl);
9643063Sperrin 	kmem_free(zgd, sizeof (zgd_t));
9653063Sperrin 	return (error);
9663063Sperrin }
9673063Sperrin 
9681861Sperrin /*
9691861Sperrin  * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
9701141Sperrin  *
9711141Sperrin  * We store data in the log buffers if it's small enough.
9723063Sperrin  * Otherwise we will later flush the data out via dmu_sync().
9731141Sperrin  */
9743063Sperrin ssize_t zvol_immediate_write_sz = 32768;
9751141Sperrin 
9763638Sbillm static void
9773638Sbillm zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t len)
9781141Sperrin {
9793638Sbillm 	uint32_t blocksize = zv->zv_volblocksize;
9801141Sperrin 	lr_write_t *lr;
9811861Sperrin 
9821861Sperrin 	while (len) {
9833638Sbillm 		ssize_t nbytes = MIN(len, blocksize - P2PHASE(off, blocksize));
9843638Sbillm 		itx_t *itx = zil_itx_create(TX_WRITE, sizeof (*lr));
9853638Sbillm 
9863638Sbillm 		itx->itx_wr_state =
9873638Sbillm 		    len > zvol_immediate_write_sz ?  WR_INDIRECT : WR_NEED_COPY;
9883638Sbillm 		itx->itx_private = zv;
9893638Sbillm 		lr = (lr_write_t *)&itx->itx_lr;
9903638Sbillm 		lr->lr_foid = ZVOL_OBJ;
9913638Sbillm 		lr->lr_offset = off;
9923638Sbillm 		lr->lr_length = nbytes;
9933638Sbillm 		lr->lr_blkoff = off - P2ALIGN_TYPED(off, blocksize, uint64_t);
9943638Sbillm 		BP_ZERO(&lr->lr_blkptr);
9953638Sbillm 
9963638Sbillm 		(void) zil_itx_assign(zv->zv_zilog, itx, tx);
9971861Sperrin 		len -= nbytes;
9981861Sperrin 		off += nbytes;
9991141Sperrin 	}
10001141Sperrin }
10011141Sperrin 
1002*7837SMatthew.Ahrens@Sun.COM static int
1003*7837SMatthew.Ahrens@Sun.COM zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t size,
1004*7837SMatthew.Ahrens@Sun.COM     boolean_t doread, boolean_t isdump)
10056423Sgw25295 {
10066423Sgw25295 	vdev_disk_t *dvd;
10076423Sgw25295 	int c;
10086423Sgw25295 	int numerrors = 0;
10096423Sgw25295 
10106423Sgw25295 	for (c = 0; c < vd->vdev_children; c++) {
1011*7837SMatthew.Ahrens@Sun.COM 		ASSERT(vd->vdev_ops == &vdev_mirror_ops);
1012*7837SMatthew.Ahrens@Sun.COM 		int err = zvol_dumpio_vdev(vd->vdev_child[c],
1013*7837SMatthew.Ahrens@Sun.COM 		    addr, offset, size, doread, isdump);
1014*7837SMatthew.Ahrens@Sun.COM 		ASSERT3U(err, ==, 0);
1015*7837SMatthew.Ahrens@Sun.COM 		if (err != 0) {
10166423Sgw25295 			numerrors++;
1017*7837SMatthew.Ahrens@Sun.COM 		} else if (doread) {
10186423Sgw25295 			break;
10196423Sgw25295 		}
10206423Sgw25295 	}
10216423Sgw25295 
10226423Sgw25295 	if (!vd->vdev_ops->vdev_op_leaf)
10236423Sgw25295 		return (numerrors < vd->vdev_children ? 0 : EIO);
10246423Sgw25295 
1025*7837SMatthew.Ahrens@Sun.COM 	ASSERT(vdev_writeable(vd));
10266423Sgw25295 	if (!vdev_writeable(vd))
10276423Sgw25295 		return (EIO);
10286423Sgw25295 
10296423Sgw25295 	dvd = vd->vdev_tsd;
10306423Sgw25295 	ASSERT3P(dvd, !=, NULL);
10316423Sgw25295 	offset += VDEV_LABEL_START_SIZE;
10326423Sgw25295 
10336423Sgw25295 	if (ddi_in_panic() || isdump) {
1034*7837SMatthew.Ahrens@Sun.COM 		ASSERT(!doread);
1035*7837SMatthew.Ahrens@Sun.COM 		if (doread)
10366423Sgw25295 			return (EIO);
10376423Sgw25295 		return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset),
10386423Sgw25295 		    lbtodb(size)));
10396423Sgw25295 	} else {
10406423Sgw25295 		return (vdev_disk_physio(dvd->vd_lh, addr, size, offset,
1041*7837SMatthew.Ahrens@Sun.COM 		    doread ? B_READ : B_WRITE));
10426423Sgw25295 	}
10436423Sgw25295 }
10446423Sgw25295 
1045*7837SMatthew.Ahrens@Sun.COM static int
1046*7837SMatthew.Ahrens@Sun.COM zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size,
1047*7837SMatthew.Ahrens@Sun.COM     boolean_t doread, boolean_t isdump)
10486423Sgw25295 {
10496423Sgw25295 	vdev_t *vd;
10506423Sgw25295 	int error;
1051*7837SMatthew.Ahrens@Sun.COM 	zvol_extent_t *ze;
10526423Sgw25295 	spa_t *spa = dmu_objset_spa(zv->zv_objset);
10536423Sgw25295 
1054*7837SMatthew.Ahrens@Sun.COM 	/* Must be sector aligned, and not stradle a block boundary. */
1055*7837SMatthew.Ahrens@Sun.COM 	if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) ||
1056*7837SMatthew.Ahrens@Sun.COM 	    P2BOUNDARY(offset, size, zv->zv_volblocksize)) {
1057*7837SMatthew.Ahrens@Sun.COM 		return (EINVAL);
1058*7837SMatthew.Ahrens@Sun.COM 	}
10596423Sgw25295 	ASSERT(size <= zv->zv_volblocksize);
10606423Sgw25295 
1061*7837SMatthew.Ahrens@Sun.COM 	/* Locate the extent this belongs to */
1062*7837SMatthew.Ahrens@Sun.COM 	ze = list_head(&zv->zv_extents);
1063*7837SMatthew.Ahrens@Sun.COM 	while (offset >= ze->ze_nblks * zv->zv_volblocksize) {
1064*7837SMatthew.Ahrens@Sun.COM 		offset -= ze->ze_nblks * zv->zv_volblocksize;
1065*7837SMatthew.Ahrens@Sun.COM 		ze = list_next(&zv->zv_extents, ze);
1066*7837SMatthew.Ahrens@Sun.COM 	}
10677754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
1068*7837SMatthew.Ahrens@Sun.COM 	vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva));
1069*7837SMatthew.Ahrens@Sun.COM 	offset += DVA_GET_OFFSET(&ze->ze_dva);
1070*7837SMatthew.Ahrens@Sun.COM 	error = zvol_dumpio_vdev(vd, addr, offset, size, doread, isdump);
10717754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
10726423Sgw25295 	return (error);
10736423Sgw25295 }
10746423Sgw25295 
10756423Sgw25295 int
1076789Sahrens zvol_strategy(buf_t *bp)
1077789Sahrens {
1078789Sahrens 	zvol_state_t *zv = ddi_get_soft_state(zvol_state, getminor(bp->b_edev));
1079789Sahrens 	uint64_t off, volsize;
1080*7837SMatthew.Ahrens@Sun.COM 	size_t resid;
1081789Sahrens 	char *addr;
10821141Sperrin 	objset_t *os;
10833755Sperrin 	rl_t *rl;
1084789Sahrens 	int error = 0;
1085*7837SMatthew.Ahrens@Sun.COM 	boolean_t doread = bp->b_flags & B_READ;
1086*7837SMatthew.Ahrens@Sun.COM 	boolean_t is_dump = zv->zv_flags & ZVOL_DUMPIFIED;
1087789Sahrens 
1088789Sahrens 	if (zv == NULL) {
1089789Sahrens 		bioerror(bp, ENXIO);
1090789Sahrens 		biodone(bp);
1091789Sahrens 		return (0);
1092789Sahrens 	}
1093789Sahrens 
1094789Sahrens 	if (getminor(bp->b_edev) == 0) {
1095789Sahrens 		bioerror(bp, EINVAL);
1096789Sahrens 		biodone(bp);
1097789Sahrens 		return (0);
1098789Sahrens 	}
1099789Sahrens 
11006423Sgw25295 	if (!(bp->b_flags & B_READ) &&
11016423Sgw25295 	    (zv->zv_flags & ZVOL_RDONLY ||
11026423Sgw25295 	    zv->zv_mode & DS_MODE_READONLY)) {
1103789Sahrens 		bioerror(bp, EROFS);
1104789Sahrens 		biodone(bp);
1105789Sahrens 		return (0);
1106789Sahrens 	}
1107789Sahrens 
1108789Sahrens 	off = ldbtob(bp->b_blkno);
1109789Sahrens 	volsize = zv->zv_volsize;
1110789Sahrens 
11111141Sperrin 	os = zv->zv_objset;
11121141Sperrin 	ASSERT(os != NULL);
1113789Sahrens 
1114789Sahrens 	bp_mapin(bp);
1115789Sahrens 	addr = bp->b_un.b_addr;
1116789Sahrens 	resid = bp->b_bcount;
1117789Sahrens 
1118*7837SMatthew.Ahrens@Sun.COM 	if (resid > 0 && (off < 0 || off >= volsize)) {
1119*7837SMatthew.Ahrens@Sun.COM 		bioerror(bp, EIO);
1120*7837SMatthew.Ahrens@Sun.COM 		biodone(bp);
1121*7837SMatthew.Ahrens@Sun.COM 		return (0);
1122*7837SMatthew.Ahrens@Sun.COM 	}
11237013Sgw25295 
11241861Sperrin 	/*
11251861Sperrin 	 * There must be no buffer changes when doing a dmu_sync() because
11261861Sperrin 	 * we can't change the data whilst calculating the checksum.
11271861Sperrin 	 */
11283755Sperrin 	rl = zfs_range_lock(&zv->zv_znode, off, resid,
1129*7837SMatthew.Ahrens@Sun.COM 	    doread ? RL_READER : RL_WRITER);
11306423Sgw25295 
1131789Sahrens 	while (resid != 0 && off < volsize) {
1132*7837SMatthew.Ahrens@Sun.COM 		size_t size = MIN(resid, zvol_maxphys);
11336423Sgw25295 		if (is_dump) {
11346423Sgw25295 			size = MIN(size, P2END(off, zv->zv_volblocksize) - off);
1135*7837SMatthew.Ahrens@Sun.COM 			error = zvol_dumpio(zv, addr, off, size,
1136*7837SMatthew.Ahrens@Sun.COM 			    doread, B_FALSE);
1137*7837SMatthew.Ahrens@Sun.COM 		} else if (doread) {
11381861Sperrin 			error = dmu_read(os, ZVOL_OBJ, off, size, addr);
1139789Sahrens 		} else {
11401141Sperrin 			dmu_tx_t *tx = dmu_tx_create(os);
1141789Sahrens 			dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
1142789Sahrens 			error = dmu_tx_assign(tx, TXG_WAIT);
1143789Sahrens 			if (error) {
1144789Sahrens 				dmu_tx_abort(tx);
1145789Sahrens 			} else {
11461141Sperrin 				dmu_write(os, ZVOL_OBJ, off, size, addr, tx);
11473638Sbillm 				zvol_log_write(zv, tx, off, size);
1148789Sahrens 				dmu_tx_commit(tx);
1149789Sahrens 			}
1150789Sahrens 		}
11517294Sperrin 		if (error) {
11527294Sperrin 			/* convert checksum errors into IO errors */
11537294Sperrin 			if (error == ECKSUM)
11547294Sperrin 				error = EIO;
1155789Sahrens 			break;
11567294Sperrin 		}
1157789Sahrens 		off += size;
1158789Sahrens 		addr += size;
1159789Sahrens 		resid -= size;
1160789Sahrens 	}
11613755Sperrin 	zfs_range_unlock(rl);
1162789Sahrens 
1163789Sahrens 	if ((bp->b_resid = resid) == bp->b_bcount)
1164789Sahrens 		bioerror(bp, off > volsize ? EINVAL : error);
1165789Sahrens 
1166*7837SMatthew.Ahrens@Sun.COM 	if (!(bp->b_flags & B_ASYNC) && !doread && !zil_disable && !is_dump)
11673638Sbillm 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
11683638Sbillm 	biodone(bp);
11691141Sperrin 
1170789Sahrens 	return (0);
1171789Sahrens }
1172789Sahrens 
11733063Sperrin /*
11743063Sperrin  * Set the buffer count to the zvol maximum transfer.
11753063Sperrin  * Using our own routine instead of the default minphys()
11763063Sperrin  * means that for larger writes we write bigger buffers on X86
11773063Sperrin  * (128K instead of 56K) and flush the disk write cache less often
11783063Sperrin  * (every zvol_maxphys - currently 1MB) instead of minphys (currently
11793063Sperrin  * 56K on X86 and 128K on sparc).
11803063Sperrin  */
11813063Sperrin void
11823063Sperrin zvol_minphys(struct buf *bp)
11833063Sperrin {
11843063Sperrin 	if (bp->b_bcount > zvol_maxphys)
11853063Sperrin 		bp->b_bcount = zvol_maxphys;
11863063Sperrin }
11873063Sperrin 
11886423Sgw25295 int
11896423Sgw25295 zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks)
11906423Sgw25295 {
11916423Sgw25295 	minor_t minor = getminor(dev);
11926423Sgw25295 	zvol_state_t *zv;
11936423Sgw25295 	int error = 0;
11946423Sgw25295 	uint64_t size;
11956423Sgw25295 	uint64_t boff;
11966423Sgw25295 	uint64_t resid;
11976423Sgw25295 
11986423Sgw25295 	if (minor == 0)			/* This is the control device */
11996423Sgw25295 		return (ENXIO);
12006423Sgw25295 
12016423Sgw25295 	zv = ddi_get_soft_state(zvol_state, minor);
12026423Sgw25295 	if (zv == NULL)
12036423Sgw25295 		return (ENXIO);
12046423Sgw25295 
12056423Sgw25295 	boff = ldbtob(blkno);
12066423Sgw25295 	resid = ldbtob(nblocks);
1207*7837SMatthew.Ahrens@Sun.COM 
1208*7837SMatthew.Ahrens@Sun.COM 	VERIFY3U(boff + resid, <=, zv->zv_volsize);
1209*7837SMatthew.Ahrens@Sun.COM 
12106423Sgw25295 	while (resid) {
12116423Sgw25295 		size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff);
1212*7837SMatthew.Ahrens@Sun.COM 		error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE);
12136423Sgw25295 		if (error)
12146423Sgw25295 			break;
12156423Sgw25295 		boff += size;
12166423Sgw25295 		addr += size;
12176423Sgw25295 		resid -= size;
12186423Sgw25295 	}
12196423Sgw25295 
12206423Sgw25295 	return (error);
12216423Sgw25295 }
12226423Sgw25295 
1223789Sahrens /*ARGSUSED*/
1224789Sahrens int
12253638Sbillm zvol_read(dev_t dev, uio_t *uio, cred_t *cr)
1226789Sahrens {
12274107Sgw25295 	minor_t minor = getminor(dev);
12284107Sgw25295 	zvol_state_t *zv;
12297013Sgw25295 	uint64_t volsize;
12303755Sperrin 	rl_t *rl;
12313638Sbillm 	int error = 0;
12323638Sbillm 
12334107Sgw25295 	if (minor == 0)			/* This is the control device */
12344107Sgw25295 		return (ENXIO);
12354107Sgw25295 
12364107Sgw25295 	zv = ddi_get_soft_state(zvol_state, minor);
12374107Sgw25295 	if (zv == NULL)
12384107Sgw25295 		return (ENXIO);
12394107Sgw25295 
12407013Sgw25295 	volsize = zv->zv_volsize;
12417013Sgw25295 	if (uio->uio_resid > 0 &&
12427013Sgw25295 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
12437013Sgw25295 		return (EIO);
12447013Sgw25295 
1245*7837SMatthew.Ahrens@Sun.COM 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
1246*7837SMatthew.Ahrens@Sun.COM 		error = physio(zvol_strategy, NULL, dev, B_READ,
1247*7837SMatthew.Ahrens@Sun.COM 		    zvol_minphys, uio);
1248*7837SMatthew.Ahrens@Sun.COM 		return (error);
1249*7837SMatthew.Ahrens@Sun.COM 	}
1250*7837SMatthew.Ahrens@Sun.COM 
12513755Sperrin 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
12523755Sperrin 	    RL_READER);
12537013Sgw25295 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
12543638Sbillm 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
12553638Sbillm 
12567013Sgw25295 		/* don't read past the end */
12577013Sgw25295 		if (bytes > volsize - uio->uio_loffset)
12587013Sgw25295 			bytes = volsize - uio->uio_loffset;
12597013Sgw25295 
12603638Sbillm 		error =  dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes);
12617294Sperrin 		if (error) {
12627294Sperrin 			/* convert checksum errors into IO errors */
12637294Sperrin 			if (error == ECKSUM)
12647294Sperrin 				error = EIO;
12653638Sbillm 			break;
12667294Sperrin 		}
12673638Sbillm 	}
12683755Sperrin 	zfs_range_unlock(rl);
12693638Sbillm 	return (error);
1270789Sahrens }
1271789Sahrens 
1272789Sahrens /*ARGSUSED*/
1273789Sahrens int
12743638Sbillm zvol_write(dev_t dev, uio_t *uio, cred_t *cr)
1275789Sahrens {
12764107Sgw25295 	minor_t minor = getminor(dev);
12774107Sgw25295 	zvol_state_t *zv;
12787013Sgw25295 	uint64_t volsize;
12793755Sperrin 	rl_t *rl;
12803638Sbillm 	int error = 0;
12813638Sbillm 
12824107Sgw25295 	if (minor == 0)			/* This is the control device */
12834107Sgw25295 		return (ENXIO);
12844107Sgw25295 
12854107Sgw25295 	zv = ddi_get_soft_state(zvol_state, minor);
12864107Sgw25295 	if (zv == NULL)
12874107Sgw25295 		return (ENXIO);
12884107Sgw25295 
12897013Sgw25295 	volsize = zv->zv_volsize;
12907013Sgw25295 	if (uio->uio_resid > 0 &&
12917013Sgw25295 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
12927013Sgw25295 		return (EIO);
12937013Sgw25295 
12946423Sgw25295 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
12956423Sgw25295 		error = physio(zvol_strategy, NULL, dev, B_WRITE,
12966423Sgw25295 		    zvol_minphys, uio);
12976423Sgw25295 		return (error);
12986423Sgw25295 	}
12996423Sgw25295 
13003755Sperrin 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
13013755Sperrin 	    RL_WRITER);
13027013Sgw25295 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
13033638Sbillm 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
13043638Sbillm 		uint64_t off = uio->uio_loffset;
13057013Sgw25295 		dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
1306789Sahrens 
13077013Sgw25295 		if (bytes > volsize - off)	/* don't write past the end */
13087013Sgw25295 			bytes = volsize - off;
13097013Sgw25295 
13103638Sbillm 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
13113638Sbillm 		error = dmu_tx_assign(tx, TXG_WAIT);
13123638Sbillm 		if (error) {
13133638Sbillm 			dmu_tx_abort(tx);
13143638Sbillm 			break;
13153638Sbillm 		}
13163638Sbillm 		error = dmu_write_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes, tx);
13173638Sbillm 		if (error == 0)
13183638Sbillm 			zvol_log_write(zv, tx, off, bytes);
13193638Sbillm 		dmu_tx_commit(tx);
13203638Sbillm 
13213638Sbillm 		if (error)
13223638Sbillm 			break;
13233638Sbillm 	}
13243755Sperrin 	zfs_range_unlock(rl);
13253638Sbillm 	return (error);
1326789Sahrens }
1327789Sahrens 
13287405SEric.Taylor@Sun.COM int
13297405SEric.Taylor@Sun.COM zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs)
13307405SEric.Taylor@Sun.COM {
13317405SEric.Taylor@Sun.COM 	struct uuid uuid = EFI_RESERVED;
13327405SEric.Taylor@Sun.COM 	efi_gpe_t gpe = { 0 };
13337405SEric.Taylor@Sun.COM 	uint32_t crc;
13347405SEric.Taylor@Sun.COM 	dk_efi_t efi;
13357405SEric.Taylor@Sun.COM 	int length;
13367405SEric.Taylor@Sun.COM 	char *ptr;
13377405SEric.Taylor@Sun.COM 
13387405SEric.Taylor@Sun.COM 	if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag))
13397405SEric.Taylor@Sun.COM 		return (EFAULT);
13407405SEric.Taylor@Sun.COM 	ptr = (char *)(uintptr_t)efi.dki_data_64;
13417405SEric.Taylor@Sun.COM 	length = efi.dki_length;
13427405SEric.Taylor@Sun.COM 	/*
13437405SEric.Taylor@Sun.COM 	 * Some clients may attempt to request a PMBR for the
13447405SEric.Taylor@Sun.COM 	 * zvol.  Currently this interface will return EINVAL to
13457405SEric.Taylor@Sun.COM 	 * such requests.  These requests could be supported by
13467405SEric.Taylor@Sun.COM 	 * adding a check for lba == 0 and consing up an appropriate
13477405SEric.Taylor@Sun.COM 	 * PMBR.
13487405SEric.Taylor@Sun.COM 	 */
13497405SEric.Taylor@Sun.COM 	if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0)
13507405SEric.Taylor@Sun.COM 		return (EINVAL);
13517405SEric.Taylor@Sun.COM 
13527405SEric.Taylor@Sun.COM 	gpe.efi_gpe_StartingLBA = LE_64(34ULL);
13537405SEric.Taylor@Sun.COM 	gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1);
13547405SEric.Taylor@Sun.COM 	UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
13557405SEric.Taylor@Sun.COM 
13567405SEric.Taylor@Sun.COM 	if (efi.dki_lba == 1) {
13577405SEric.Taylor@Sun.COM 		efi_gpt_t gpt = { 0 };
13587405SEric.Taylor@Sun.COM 
13597405SEric.Taylor@Sun.COM 		gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
13607405SEric.Taylor@Sun.COM 		gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
13617405SEric.Taylor@Sun.COM 		gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt));
13627405SEric.Taylor@Sun.COM 		gpt.efi_gpt_MyLBA = LE_64(1ULL);
13637405SEric.Taylor@Sun.COM 		gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL);
13647405SEric.Taylor@Sun.COM 		gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1);
13657405SEric.Taylor@Sun.COM 		gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL);
13667405SEric.Taylor@Sun.COM 		gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
13677405SEric.Taylor@Sun.COM 		gpt.efi_gpt_SizeOfPartitionEntry =
13687405SEric.Taylor@Sun.COM 		    LE_32(sizeof (efi_gpe_t));
13697405SEric.Taylor@Sun.COM 		CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
13707405SEric.Taylor@Sun.COM 		gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
13717405SEric.Taylor@Sun.COM 		CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table);
13727405SEric.Taylor@Sun.COM 		gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
13737405SEric.Taylor@Sun.COM 		if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length),
13747405SEric.Taylor@Sun.COM 		    flag))
13757405SEric.Taylor@Sun.COM 			return (EFAULT);
13767405SEric.Taylor@Sun.COM 		ptr += sizeof (gpt);
13777405SEric.Taylor@Sun.COM 		length -= sizeof (gpt);
13787405SEric.Taylor@Sun.COM 	}
13797405SEric.Taylor@Sun.COM 	if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe),
13807405SEric.Taylor@Sun.COM 	    length), flag))
13817405SEric.Taylor@Sun.COM 		return (EFAULT);
13827405SEric.Taylor@Sun.COM 	return (0);
13837405SEric.Taylor@Sun.COM }
13847405SEric.Taylor@Sun.COM 
1385789Sahrens /*
1386789Sahrens  * Dirtbag ioctls to support mkfs(1M) for UFS filesystems.  See dkio(7I).
1387789Sahrens  */
1388789Sahrens /*ARGSUSED*/
1389789Sahrens int
1390789Sahrens zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
1391789Sahrens {
1392789Sahrens 	zvol_state_t *zv;
13933897Smaybee 	struct dk_cinfo dki;
1394789Sahrens 	struct dk_minfo dkm;
13953897Smaybee 	struct dk_callback *dkc;
1396789Sahrens 	int error = 0;
13976423Sgw25295 	rl_t *rl;
1398789Sahrens 
1399789Sahrens 	mutex_enter(&zvol_state_lock);
1400789Sahrens 
1401789Sahrens 	zv = ddi_get_soft_state(zvol_state, getminor(dev));
1402789Sahrens 
1403789Sahrens 	if (zv == NULL) {
1404789Sahrens 		mutex_exit(&zvol_state_lock);
1405789Sahrens 		return (ENXIO);
1406789Sahrens 	}
1407789Sahrens 
1408789Sahrens 	switch (cmd) {
1409789Sahrens 
1410789Sahrens 	case DKIOCINFO:
14113897Smaybee 		bzero(&dki, sizeof (dki));
14123897Smaybee 		(void) strcpy(dki.dki_cname, "zvol");
14133897Smaybee 		(void) strcpy(dki.dki_dname, "zvol");
14143897Smaybee 		dki.dki_ctype = DKC_UNKNOWN;
14153897Smaybee 		dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs);
1416789Sahrens 		mutex_exit(&zvol_state_lock);
14173897Smaybee 		if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag))
1418789Sahrens 			error = EFAULT;
1419789Sahrens 		return (error);
1420789Sahrens 
1421789Sahrens 	case DKIOCGMEDIAINFO:
1422789Sahrens 		bzero(&dkm, sizeof (dkm));
1423789Sahrens 		dkm.dki_lbsize = 1U << zv->zv_min_bs;
1424789Sahrens 		dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
1425789Sahrens 		dkm.dki_media_type = DK_UNKNOWN;
1426789Sahrens 		mutex_exit(&zvol_state_lock);
1427789Sahrens 		if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
1428789Sahrens 			error = EFAULT;
1429789Sahrens 		return (error);
1430789Sahrens 
1431789Sahrens 	case DKIOCGETEFI:
14327405SEric.Taylor@Sun.COM 		{
14337405SEric.Taylor@Sun.COM 			uint64_t vs = zv->zv_volsize;
14347405SEric.Taylor@Sun.COM 			uint8_t bs = zv->zv_min_bs;
14353016Smaybee 
14363016Smaybee 			mutex_exit(&zvol_state_lock);
14377405SEric.Taylor@Sun.COM 			error = zvol_getefi((void *)arg, flag, vs, bs);
14387405SEric.Taylor@Sun.COM 			return (error);
14393016Smaybee 		}
1440789Sahrens 
14413638Sbillm 	case DKIOCFLUSHWRITECACHE:
14423897Smaybee 		dkc = (struct dk_callback *)arg;
14433638Sbillm 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
14443897Smaybee 		if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) {
14453897Smaybee 			(*dkc->dkc_callback)(dkc->dkc_cookie, error);
14463897Smaybee 			error = 0;
14473897Smaybee 		}
14483638Sbillm 		break;
14493638Sbillm 
14503245Smaybee 	case DKIOCGGEOM:
14513245Smaybee 	case DKIOCGVTOC:
14526423Sgw25295 		/*
14536423Sgw25295 		 * commands using these (like prtvtoc) expect ENOTSUP
14546423Sgw25295 		 * since we're emulating an EFI label
14556423Sgw25295 		 */
14563245Smaybee 		error = ENOTSUP;
14573245Smaybee 		break;
14583245Smaybee 
14596423Sgw25295 	case DKIOCDUMPINIT:
14606423Sgw25295 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
14616423Sgw25295 		    RL_WRITER);
14626423Sgw25295 		error = zvol_dumpify(zv);
14636423Sgw25295 		zfs_range_unlock(rl);
14646423Sgw25295 		break;
14656423Sgw25295 
14666423Sgw25295 	case DKIOCDUMPFINI:
14676423Sgw25295 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
14686423Sgw25295 		    RL_WRITER);
14696423Sgw25295 		error = zvol_dump_fini(zv);
14706423Sgw25295 		zfs_range_unlock(rl);
14716423Sgw25295 		break;
14726423Sgw25295 
1473789Sahrens 	default:
14743016Smaybee 		error = ENOTTY;
1475789Sahrens 		break;
1476789Sahrens 
1477789Sahrens 	}
1478789Sahrens 	mutex_exit(&zvol_state_lock);
1479789Sahrens 	return (error);
1480789Sahrens }
1481789Sahrens 
1482789Sahrens int
1483789Sahrens zvol_busy(void)
1484789Sahrens {
1485789Sahrens 	return (zvol_minors != 0);
1486789Sahrens }
1487789Sahrens 
1488789Sahrens void
1489789Sahrens zvol_init(void)
1490789Sahrens {
1491789Sahrens 	VERIFY(ddi_soft_state_init(&zvol_state, sizeof (zvol_state_t), 1) == 0);
1492789Sahrens 	mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
1493789Sahrens }
1494789Sahrens 
1495789Sahrens void
1496789Sahrens zvol_fini(void)
1497789Sahrens {
1498789Sahrens 	mutex_destroy(&zvol_state_lock);
1499789Sahrens 	ddi_soft_state_fini(&zvol_state);
1500789Sahrens }
15016423Sgw25295 
15026423Sgw25295 static boolean_t
15036423Sgw25295 zvol_is_swap(zvol_state_t *zv)
15046423Sgw25295 {
15056423Sgw25295 	vnode_t *vp;
15066423Sgw25295 	boolean_t ret = B_FALSE;
15076423Sgw25295 	char *devpath;
15086423Sgw25295 	size_t devpathlen;
15096423Sgw25295 	int error;
15106423Sgw25295 
15116423Sgw25295 	devpathlen = strlen(ZVOL_FULL_DEV_DIR) + strlen(zv->zv_name) + 1;
15126423Sgw25295 	devpath = kmem_alloc(devpathlen, KM_SLEEP);
15136423Sgw25295 	(void) sprintf(devpath, "%s%s", ZVOL_FULL_DEV_DIR, zv->zv_name);
15146423Sgw25295 	error = lookupname(devpath, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp);
15156423Sgw25295 	kmem_free(devpath, devpathlen);
15166423Sgw25295 
15176423Sgw25295 	ret = !error && IS_SWAPVP(common_specvp(vp));
15186423Sgw25295 
15196423Sgw25295 	if (vp != NULL)
15206423Sgw25295 		VN_RELE(vp);
15216423Sgw25295 
15226423Sgw25295 	return (ret);
15236423Sgw25295 }
15246423Sgw25295 
15256423Sgw25295 static int
15266423Sgw25295 zvol_dump_init(zvol_state_t *zv, boolean_t resize)
15276423Sgw25295 {
15286423Sgw25295 	dmu_tx_t *tx;
15296423Sgw25295 	int error = 0;
15306423Sgw25295 	objset_t *os = zv->zv_objset;
15316423Sgw25295 	nvlist_t *nv = NULL;
15326423Sgw25295 
15336423Sgw25295 	ASSERT(MUTEX_HELD(&zvol_state_lock));
15346423Sgw25295 
15356423Sgw25295 	tx = dmu_tx_create(os);
15366423Sgw25295 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
15376423Sgw25295 	error = dmu_tx_assign(tx, TXG_WAIT);
15386423Sgw25295 	if (error) {
15396423Sgw25295 		dmu_tx_abort(tx);
15406423Sgw25295 		return (error);
15416423Sgw25295 	}
15426423Sgw25295 
15436423Sgw25295 	/*
15446423Sgw25295 	 * If we are resizing the dump device then we only need to
15456423Sgw25295 	 * update the refreservation to match the newly updated
15466423Sgw25295 	 * zvolsize. Otherwise, we save off the original state of the
15476423Sgw25295 	 * zvol so that we can restore them if the zvol is ever undumpified.
15486423Sgw25295 	 */
15496423Sgw25295 	if (resize) {
15506423Sgw25295 		error = zap_update(os, ZVOL_ZAP_OBJ,
15516423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
15526423Sgw25295 		    &zv->zv_volsize, tx);
15536423Sgw25295 	} else {
1554*7837SMatthew.Ahrens@Sun.COM 		uint64_t checksum, compress, refresrv, vbs;
1555*7837SMatthew.Ahrens@Sun.COM 
15566423Sgw25295 		error = dsl_prop_get_integer(zv->zv_name,
15576423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL);
15586423Sgw25295 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
15596423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL);
15606423Sgw25295 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
15616423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL);
1562*7837SMatthew.Ahrens@Sun.COM 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
1563*7837SMatthew.Ahrens@Sun.COM 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL);
15646423Sgw25295 
15656423Sgw25295 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
15666423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1,
15676423Sgw25295 		    &compress, tx);
15686423Sgw25295 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
15696423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx);
15706423Sgw25295 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
15716423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
15726423Sgw25295 		    &refresrv, tx);
1573*7837SMatthew.Ahrens@Sun.COM 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1574*7837SMatthew.Ahrens@Sun.COM 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1,
1575*7837SMatthew.Ahrens@Sun.COM 		    &vbs, tx);
15766423Sgw25295 	}
15776423Sgw25295 	dmu_tx_commit(tx);
15786423Sgw25295 
15796423Sgw25295 	/* Truncate the file */
15806423Sgw25295 	if (!error)
15816992Smaybee 		error = dmu_free_long_range(zv->zv_objset,
15826992Smaybee 		    ZVOL_OBJ, 0, DMU_OBJECT_END);
15836423Sgw25295 
15846423Sgw25295 	if (error)
15856423Sgw25295 		return (error);
15866423Sgw25295 
15876423Sgw25295 	/*
15886423Sgw25295 	 * We only need update the zvol's property if we are initializing
15896423Sgw25295 	 * the dump area for the first time.
15906423Sgw25295 	 */
15916423Sgw25295 	if (!resize) {
15926423Sgw25295 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
15936423Sgw25295 		VERIFY(nvlist_add_uint64(nv,
15946423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0);
15956423Sgw25295 		VERIFY(nvlist_add_uint64(nv,
15966423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
15976423Sgw25295 		    ZIO_COMPRESS_OFF) == 0);
15986423Sgw25295 		VERIFY(nvlist_add_uint64(nv,
15996423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
16006423Sgw25295 		    ZIO_CHECKSUM_OFF) == 0);
1601*7837SMatthew.Ahrens@Sun.COM 		VERIFY(nvlist_add_uint64(nv,
1602*7837SMatthew.Ahrens@Sun.COM 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1603*7837SMatthew.Ahrens@Sun.COM 		    SPA_MAXBLOCKSIZE) == 0);
16046423Sgw25295 
16056423Sgw25295 		error = zfs_set_prop_nvlist(zv->zv_name, nv);
16066423Sgw25295 		nvlist_free(nv);
16076423Sgw25295 
16086423Sgw25295 		if (error)
16096423Sgw25295 			return (error);
16106423Sgw25295 	}
16116423Sgw25295 
16126423Sgw25295 	/* Allocate the space for the dump */
16136423Sgw25295 	error = zvol_prealloc(zv);
16146423Sgw25295 	return (error);
16156423Sgw25295 }
16166423Sgw25295 
16176423Sgw25295 static int
16186423Sgw25295 zvol_dumpify(zvol_state_t *zv)
16196423Sgw25295 {
16206423Sgw25295 	int error = 0;
16216423Sgw25295 	uint64_t dumpsize = 0;
16226423Sgw25295 	dmu_tx_t *tx;
16236423Sgw25295 	objset_t *os = zv->zv_objset;
16246423Sgw25295 
16256423Sgw25295 	if (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY))
16266423Sgw25295 		return (EROFS);
16276423Sgw25295 
16286423Sgw25295 	/*
16296423Sgw25295 	 * We do not support swap devices acting as dump devices.
16306423Sgw25295 	 */
16316423Sgw25295 	if (zvol_is_swap(zv))
16326423Sgw25295 		return (ENOTSUP);
16336423Sgw25295 
16346423Sgw25295 	if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE,
16356423Sgw25295 	    8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) {
16366423Sgw25295 		boolean_t resize = (dumpsize > 0) ? B_TRUE : B_FALSE;
16376423Sgw25295 
16386423Sgw25295 		if ((error = zvol_dump_init(zv, resize)) != 0) {
16396423Sgw25295 			(void) zvol_dump_fini(zv);
16406423Sgw25295 			return (error);
16416423Sgw25295 		}
16426423Sgw25295 	}
16436423Sgw25295 
16446423Sgw25295 	/*
16456423Sgw25295 	 * Build up our lba mapping.
16466423Sgw25295 	 */
16476423Sgw25295 	error = zvol_get_lbas(zv);
16486423Sgw25295 	if (error) {
16496423Sgw25295 		(void) zvol_dump_fini(zv);
16506423Sgw25295 		return (error);
16516423Sgw25295 	}
16526423Sgw25295 
16536423Sgw25295 	tx = dmu_tx_create(os);
16546423Sgw25295 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
16556423Sgw25295 	error = dmu_tx_assign(tx, TXG_WAIT);
16566423Sgw25295 	if (error) {
16576423Sgw25295 		dmu_tx_abort(tx);
16586423Sgw25295 		(void) zvol_dump_fini(zv);
16596423Sgw25295 		return (error);
16606423Sgw25295 	}
16616423Sgw25295 
16626423Sgw25295 	zv->zv_flags |= ZVOL_DUMPIFIED;
16636423Sgw25295 	error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1,
16646423Sgw25295 	    &zv->zv_volsize, tx);
16656423Sgw25295 	dmu_tx_commit(tx);
16666423Sgw25295 
16676423Sgw25295 	if (error) {
16686423Sgw25295 		(void) zvol_dump_fini(zv);
16696423Sgw25295 		return (error);
16706423Sgw25295 	}
16716423Sgw25295 
16726423Sgw25295 	txg_wait_synced(dmu_objset_pool(os), 0);
16736423Sgw25295 	return (0);
16746423Sgw25295 }
16756423Sgw25295 
16766423Sgw25295 static int
16776423Sgw25295 zvol_dump_fini(zvol_state_t *zv)
16786423Sgw25295 {
16796423Sgw25295 	dmu_tx_t *tx;
16806423Sgw25295 	objset_t *os = zv->zv_objset;
16816423Sgw25295 	nvlist_t *nv;
16826423Sgw25295 	int error = 0;
1683*7837SMatthew.Ahrens@Sun.COM 	uint64_t checksum, compress, refresrv, vbs;
16846423Sgw25295 
16857080Smaybee 	/*
16867080Smaybee 	 * Attempt to restore the zvol back to its pre-dumpified state.
16877080Smaybee 	 * This is a best-effort attempt as it's possible that not all
16887080Smaybee 	 * of these properties were initialized during the dumpify process
16897080Smaybee 	 * (i.e. error during zvol_dump_init).
16907080Smaybee 	 */
16917080Smaybee 
16926423Sgw25295 	tx = dmu_tx_create(os);
16936423Sgw25295 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
16946423Sgw25295 	error = dmu_tx_assign(tx, TXG_WAIT);
16956423Sgw25295 	if (error) {
16966423Sgw25295 		dmu_tx_abort(tx);
16976423Sgw25295 		return (error);
16986423Sgw25295 	}
16997080Smaybee 	(void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx);
17007080Smaybee 	dmu_tx_commit(tx);
17016423Sgw25295 
17026423Sgw25295 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
17036423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum);
17046423Sgw25295 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
17056423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress);
17066423Sgw25295 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
17076423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv);
1708*7837SMatthew.Ahrens@Sun.COM 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1709*7837SMatthew.Ahrens@Sun.COM 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs);
17106423Sgw25295 
17116423Sgw25295 	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
17126423Sgw25295 	(void) nvlist_add_uint64(nv,
17136423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum);
17146423Sgw25295 	(void) nvlist_add_uint64(nv,
17156423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress);
17166423Sgw25295 	(void) nvlist_add_uint64(nv,
17176423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv);
1718*7837SMatthew.Ahrens@Sun.COM 	(void) nvlist_add_uint64(nv,
1719*7837SMatthew.Ahrens@Sun.COM 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), vbs);
17206423Sgw25295 	(void) zfs_set_prop_nvlist(zv->zv_name, nv);
17216423Sgw25295 	nvlist_free(nv);
17226423Sgw25295 
17237080Smaybee 	zvol_free_extents(zv);
17247080Smaybee 	zv->zv_flags &= ~ZVOL_DUMPIFIED;
17257080Smaybee 	(void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END);
17267080Smaybee 
17276423Sgw25295 	return (0);
17286423Sgw25295 }
1729