xref: /onnv-gate/usr/src/uts/common/fs/zfs/zvol.c (revision 10310:ba87b3315737)
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 /*
228524SEric.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;
8310298SMatthew.Ahrens@Sun.COM static char *zvol_tag = "zvol_tag";
84789Sahrens 
856423Sgw25295 #define	ZVOL_DUMPSIZE		"dumpsize"
86789Sahrens 
87789Sahrens /*
88789Sahrens  * This lock protects the zvol_state structure from being modified
89789Sahrens  * while it's being used, e.g. an open that comes in before a create
90789Sahrens  * finishes.  It also protects temporary opens of the dataset so that,
91789Sahrens  * e.g., an open doesn't get a spurious EBUSY.
92789Sahrens  */
93789Sahrens static kmutex_t zvol_state_lock;
94789Sahrens static uint32_t zvol_minors;
95789Sahrens 
966423Sgw25295 typedef struct zvol_extent {
977837SMatthew.Ahrens@Sun.COM 	list_node_t	ze_node;
986423Sgw25295 	dva_t		ze_dva;		/* dva associated with this extent */
997837SMatthew.Ahrens@Sun.COM 	uint64_t	ze_nblks;	/* number of blocks in extent */
1006423Sgw25295 } zvol_extent_t;
1016423Sgw25295 
1026423Sgw25295 /*
103789Sahrens  * The in-core state of each volume.
104789Sahrens  */
105789Sahrens typedef struct zvol_state {
106789Sahrens 	char		zv_name[MAXPATHLEN]; /* pool/dd name */
107789Sahrens 	uint64_t	zv_volsize;	/* amount of space we advertise */
1083063Sperrin 	uint64_t	zv_volblocksize; /* volume block size */
109789Sahrens 	minor_t		zv_minor;	/* minor number */
110789Sahrens 	uint8_t		zv_min_bs;	/* minimum addressable block shift */
1119303SEric.Taylor@Sun.COM 	uint8_t		zv_flags;	/* readonly, dumpified, etc. */
112789Sahrens 	objset_t	*zv_objset;	/* objset handle */
11310298SMatthew.Ahrens@Sun.COM 	boolean_t 	zv_issnap;	/* is a snapshot (read-only) */
114789Sahrens 	uint32_t	zv_open_count[OTYPCNT];	/* open counts */
115789Sahrens 	uint32_t	zv_total_opens;	/* total open count */
1161141Sperrin 	zilog_t		*zv_zilog;	/* ZIL handle */
1177837SMatthew.Ahrens@Sun.COM 	list_t		zv_extents;	/* List of extents for dump */
1183755Sperrin 	znode_t		zv_znode;	/* for range locking */
119789Sahrens } zvol_state_t;
120789Sahrens 
1213063Sperrin /*
1226423Sgw25295  * zvol specific flags
1236423Sgw25295  */
1246423Sgw25295 #define	ZVOL_RDONLY	0x1
1256423Sgw25295 #define	ZVOL_DUMPIFIED	0x2
1267405SEric.Taylor@Sun.COM #define	ZVOL_EXCL	0x4
1279303SEric.Taylor@Sun.COM #define	ZVOL_WCE	0x8
1286423Sgw25295 
1296423Sgw25295 /*
1303063Sperrin  * zvol maximum transfer in one DMU tx.
1313063Sperrin  */
1323063Sperrin int zvol_maxphys = DMU_MAX_ACCESS/2;
1333063Sperrin 
1346423Sgw25295 extern int zfs_set_prop_nvlist(const char *, nvlist_t *);
1353638Sbillm static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio);
1366423Sgw25295 static int zvol_dumpify(zvol_state_t *zv);
1376423Sgw25295 static int zvol_dump_fini(zvol_state_t *zv);
1386423Sgw25295 static int zvol_dump_init(zvol_state_t *zv, boolean_t resize);
1393063Sperrin 
140789Sahrens static void
1414787Sahrens zvol_size_changed(zvol_state_t *zv, major_t maj)
142789Sahrens {
1434787Sahrens 	dev_t dev = makedevice(maj, zv->zv_minor);
144789Sahrens 
145789Sahrens 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
146789Sahrens 	    "Size", zv->zv_volsize) == DDI_SUCCESS);
147789Sahrens 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
148789Sahrens 	    "Nblocks", lbtodb(zv->zv_volsize)) == DDI_SUCCESS);
1496423Sgw25295 
1506423Sgw25295 	/* Notify specfs to invalidate the cached size */
1516423Sgw25295 	spec_size_invalidate(dev, VBLK);
1526423Sgw25295 	spec_size_invalidate(dev, VCHR);
153789Sahrens }
154789Sahrens 
155789Sahrens int
1562676Seschrock zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
157789Sahrens {
1582676Seschrock 	if (volsize == 0)
159789Sahrens 		return (EINVAL);
160789Sahrens 
1612676Seschrock 	if (volsize % blocksize != 0)
1621133Seschrock 		return (EINVAL);
1631133Seschrock 
164789Sahrens #ifdef _ILP32
1652676Seschrock 	if (volsize - 1 > SPEC_MAXOFFSET_T)
166789Sahrens 		return (EOVERFLOW);
167789Sahrens #endif
168789Sahrens 	return (0);
169789Sahrens }
170789Sahrens 
171789Sahrens int
1722676Seschrock zvol_check_volblocksize(uint64_t volblocksize)
173789Sahrens {
1742676Seschrock 	if (volblocksize < SPA_MINBLOCKSIZE ||
1752676Seschrock 	    volblocksize > SPA_MAXBLOCKSIZE ||
1762676Seschrock 	    !ISP2(volblocksize))
177789Sahrens 		return (EDOM);
178789Sahrens 
179789Sahrens 	return (0);
180789Sahrens }
181789Sahrens 
182789Sahrens static void
183789Sahrens zvol_readonly_changed_cb(void *arg, uint64_t newval)
184789Sahrens {
185789Sahrens 	zvol_state_t *zv = arg;
186789Sahrens 
1876423Sgw25295 	if (newval)
1886423Sgw25295 		zv->zv_flags |= ZVOL_RDONLY;
1896423Sgw25295 	else
1906423Sgw25295 		zv->zv_flags &= ~ZVOL_RDONLY;
191789Sahrens }
192789Sahrens 
193789Sahrens int
1942885Sahrens zvol_get_stats(objset_t *os, nvlist_t *nv)
195789Sahrens {
196789Sahrens 	int error;
197789Sahrens 	dmu_object_info_t doi;
1982885Sahrens 	uint64_t val;
199789Sahrens 
200789Sahrens 
2012885Sahrens 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
202789Sahrens 	if (error)
203789Sahrens 		return (error);
204789Sahrens 
2052885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
2062885Sahrens 
207789Sahrens 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
208789Sahrens 
2092885Sahrens 	if (error == 0) {
2102885Sahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
2112885Sahrens 		    doi.doi_data_block_size);
2122885Sahrens 	}
213789Sahrens 
214789Sahrens 	return (error);
215789Sahrens }
216789Sahrens 
217789Sahrens /*
218789Sahrens  * Find a free minor number.
219789Sahrens  */
220789Sahrens static minor_t
221789Sahrens zvol_minor_alloc(void)
222789Sahrens {
223789Sahrens 	minor_t minor;
224789Sahrens 
225789Sahrens 	ASSERT(MUTEX_HELD(&zvol_state_lock));
226789Sahrens 
227789Sahrens 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++)
228789Sahrens 		if (ddi_get_soft_state(zvol_state, minor) == NULL)
229789Sahrens 			return (minor);
230789Sahrens 
231789Sahrens 	return (0);
232789Sahrens }
233789Sahrens 
234789Sahrens static zvol_state_t *
2352676Seschrock zvol_minor_lookup(const char *name)
236789Sahrens {
237789Sahrens 	minor_t minor;
238789Sahrens 	zvol_state_t *zv;
239789Sahrens 
240789Sahrens 	ASSERT(MUTEX_HELD(&zvol_state_lock));
241789Sahrens 
242789Sahrens 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) {
243789Sahrens 		zv = ddi_get_soft_state(zvol_state, minor);
244789Sahrens 		if (zv == NULL)
245789Sahrens 			continue;
246789Sahrens 		if (strcmp(zv->zv_name, name) == 0)
247789Sahrens 			break;
248789Sahrens 	}
249789Sahrens 
250789Sahrens 	return (zv);
251789Sahrens }
252789Sahrens 
2536423Sgw25295 /* extent mapping arg */
2546423Sgw25295 struct maparg {
2557837SMatthew.Ahrens@Sun.COM 	zvol_state_t	*ma_zv;
2567837SMatthew.Ahrens@Sun.COM 	uint64_t	ma_blks;
2576423Sgw25295 };
2586423Sgw25295 
2596423Sgw25295 /*ARGSUSED*/
2606423Sgw25295 static int
2617837SMatthew.Ahrens@Sun.COM zvol_map_block(spa_t *spa, blkptr_t *bp, const zbookmark_t *zb,
2627837SMatthew.Ahrens@Sun.COM     const dnode_phys_t *dnp, void *arg)
2636423Sgw25295 {
2647837SMatthew.Ahrens@Sun.COM 	struct maparg *ma = arg;
2657837SMatthew.Ahrens@Sun.COM 	zvol_extent_t *ze;
2667837SMatthew.Ahrens@Sun.COM 	int bs = ma->ma_zv->zv_volblocksize;
2676423Sgw25295 
2687837SMatthew.Ahrens@Sun.COM 	if (bp == NULL || zb->zb_object != ZVOL_OBJ || zb->zb_level != 0)
2696423Sgw25295 		return (0);
2706423Sgw25295 
2717837SMatthew.Ahrens@Sun.COM 	VERIFY3U(ma->ma_blks, ==, zb->zb_blkid);
2727837SMatthew.Ahrens@Sun.COM 	ma->ma_blks++;
2737837SMatthew.Ahrens@Sun.COM 
2746423Sgw25295 	/* Abort immediately if we have encountered gang blocks */
2757837SMatthew.Ahrens@Sun.COM 	if (BP_IS_GANG(bp))
2767837SMatthew.Ahrens@Sun.COM 		return (EFRAGS);
2776423Sgw25295 
2787837SMatthew.Ahrens@Sun.COM 	/*
2797837SMatthew.Ahrens@Sun.COM 	 * See if the block is at the end of the previous extent.
2807837SMatthew.Ahrens@Sun.COM 	 */
2817837SMatthew.Ahrens@Sun.COM 	ze = list_tail(&ma->ma_zv->zv_extents);
2827837SMatthew.Ahrens@Sun.COM 	if (ze &&
2837837SMatthew.Ahrens@Sun.COM 	    DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) &&
2847837SMatthew.Ahrens@Sun.COM 	    DVA_GET_OFFSET(BP_IDENTITY(bp)) ==
2857837SMatthew.Ahrens@Sun.COM 	    DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) {
2867837SMatthew.Ahrens@Sun.COM 		ze->ze_nblks++;
2876423Sgw25295 		return (0);
2886423Sgw25295 	}
2896423Sgw25295 
2907837SMatthew.Ahrens@Sun.COM 	dprintf_bp(bp, "%s", "next blkptr:");
2917837SMatthew.Ahrens@Sun.COM 
2927837SMatthew.Ahrens@Sun.COM 	/* start a new extent */
2937837SMatthew.Ahrens@Sun.COM 	ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP);
2947837SMatthew.Ahrens@Sun.COM 	ze->ze_dva = bp->blk_dva[0];	/* structure assignment */
2957837SMatthew.Ahrens@Sun.COM 	ze->ze_nblks = 1;
2967837SMatthew.Ahrens@Sun.COM 	list_insert_tail(&ma->ma_zv->zv_extents, ze);
2977837SMatthew.Ahrens@Sun.COM 	return (0);
2987837SMatthew.Ahrens@Sun.COM }
2997837SMatthew.Ahrens@Sun.COM 
3007837SMatthew.Ahrens@Sun.COM static void
3017837SMatthew.Ahrens@Sun.COM zvol_free_extents(zvol_state_t *zv)
3027837SMatthew.Ahrens@Sun.COM {
3037837SMatthew.Ahrens@Sun.COM 	zvol_extent_t *ze;
3047837SMatthew.Ahrens@Sun.COM 
3057837SMatthew.Ahrens@Sun.COM 	while (ze = list_head(&zv->zv_extents)) {
3067837SMatthew.Ahrens@Sun.COM 		list_remove(&zv->zv_extents, ze);
3077837SMatthew.Ahrens@Sun.COM 		kmem_free(ze, sizeof (zvol_extent_t));
3087837SMatthew.Ahrens@Sun.COM 	}
3097837SMatthew.Ahrens@Sun.COM }
3107837SMatthew.Ahrens@Sun.COM 
3117837SMatthew.Ahrens@Sun.COM static int
3127837SMatthew.Ahrens@Sun.COM zvol_get_lbas(zvol_state_t *zv)
3137837SMatthew.Ahrens@Sun.COM {
3147837SMatthew.Ahrens@Sun.COM 	struct maparg	ma;
3157837SMatthew.Ahrens@Sun.COM 	int		err;
3167837SMatthew.Ahrens@Sun.COM 
3177837SMatthew.Ahrens@Sun.COM 	ma.ma_zv = zv;
3187837SMatthew.Ahrens@Sun.COM 	ma.ma_blks = 0;
3197837SMatthew.Ahrens@Sun.COM 	zvol_free_extents(zv);
3207837SMatthew.Ahrens@Sun.COM 
3217837SMatthew.Ahrens@Sun.COM 	err = traverse_dataset(dmu_objset_ds(zv->zv_objset), 0,
3227837SMatthew.Ahrens@Sun.COM 	    TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma);
3237837SMatthew.Ahrens@Sun.COM 	if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) {
3247837SMatthew.Ahrens@Sun.COM 		zvol_free_extents(zv);
3257837SMatthew.Ahrens@Sun.COM 		return (err ? err : EIO);
3266423Sgw25295 	}
3276423Sgw25295 
3286423Sgw25295 	return (0);
3296423Sgw25295 }
3306423Sgw25295 
3314543Smarks /* ARGSUSED */
332789Sahrens void
3334543Smarks zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
334789Sahrens {
3355331Samw 	zfs_creat_t *zct = arg;
3365331Samw 	nvlist_t *nvprops = zct->zct_props;
337789Sahrens 	int error;
3382676Seschrock 	uint64_t volblocksize, volsize;
339789Sahrens 
3404543Smarks 	VERIFY(nvlist_lookup_uint64(nvprops,
3412676Seschrock 	    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
3424543Smarks 	if (nvlist_lookup_uint64(nvprops,
3432676Seschrock 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
3442676Seschrock 		volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
3452676Seschrock 
3462676Seschrock 	/*
3476423Sgw25295 	 * These properties must be removed from the list so the generic
3482676Seschrock 	 * property setting step won't apply to them.
3492676Seschrock 	 */
3504543Smarks 	VERIFY(nvlist_remove_all(nvprops,
3512676Seschrock 	    zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
3524543Smarks 	(void) nvlist_remove_all(nvprops,
3532676Seschrock 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
3542676Seschrock 
3552676Seschrock 	error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
356789Sahrens 	    DMU_OT_NONE, 0, tx);
357789Sahrens 	ASSERT(error == 0);
358789Sahrens 
359789Sahrens 	error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
360789Sahrens 	    DMU_OT_NONE, 0, tx);
361789Sahrens 	ASSERT(error == 0);
362789Sahrens 
3632676Seschrock 	error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
364789Sahrens 	ASSERT(error == 0);
365789Sahrens }
366789Sahrens 
367789Sahrens /*
3681141Sperrin  * Replay a TX_WRITE ZIL transaction that didn't get committed
3691141Sperrin  * after a system failure
3701141Sperrin  */
3711141Sperrin static int
3721141Sperrin zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
3731141Sperrin {
3741141Sperrin 	objset_t *os = zv->zv_objset;
3751141Sperrin 	char *data = (char *)(lr + 1);	/* data follows lr_write_t */
3761141Sperrin 	uint64_t off = lr->lr_offset;
3771141Sperrin 	uint64_t len = lr->lr_length;
3781141Sperrin 	dmu_tx_t *tx;
3791141Sperrin 	int error;
3801141Sperrin 
3811141Sperrin 	if (byteswap)
3821141Sperrin 		byteswap_uint64_array(lr, sizeof (*lr));
3831141Sperrin 
3841141Sperrin 	tx = dmu_tx_create(os);
3851141Sperrin 	dmu_tx_hold_write(tx, ZVOL_OBJ, off, len);
3868227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_WAIT);
3871141Sperrin 	if (error) {
3881141Sperrin 		dmu_tx_abort(tx);
3891141Sperrin 	} else {
3901141Sperrin 		dmu_write(os, ZVOL_OBJ, off, len, data, tx);
3911141Sperrin 		dmu_tx_commit(tx);
3921141Sperrin 	}
3931141Sperrin 
3941141Sperrin 	return (error);
3951141Sperrin }
3961141Sperrin 
3971141Sperrin /* ARGSUSED */
3981141Sperrin static int
3991141Sperrin zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
4001141Sperrin {
4011141Sperrin 	return (ENOTSUP);
4021141Sperrin }
4031141Sperrin 
4041141Sperrin /*
4051141Sperrin  * Callback vectors for replaying records.
4061141Sperrin  * Only TX_WRITE is needed for zvol.
4071141Sperrin  */
4081141Sperrin zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = {
4091141Sperrin 	zvol_replay_err,	/* 0 no such transaction type */
4101141Sperrin 	zvol_replay_err,	/* TX_CREATE */
4111141Sperrin 	zvol_replay_err,	/* TX_MKDIR */
4121141Sperrin 	zvol_replay_err,	/* TX_MKXATTR */
4131141Sperrin 	zvol_replay_err,	/* TX_SYMLINK */
4141141Sperrin 	zvol_replay_err,	/* TX_REMOVE */
4151141Sperrin 	zvol_replay_err,	/* TX_RMDIR */
4161141Sperrin 	zvol_replay_err,	/* TX_LINK */
4171141Sperrin 	zvol_replay_err,	/* TX_RENAME */
4181141Sperrin 	zvol_replay_write,	/* TX_WRITE */
4191141Sperrin 	zvol_replay_err,	/* TX_TRUNCATE */
4201141Sperrin 	zvol_replay_err,	/* TX_SETATTR */
4211141Sperrin 	zvol_replay_err,	/* TX_ACL */
4221141Sperrin };
4231141Sperrin 
4241141Sperrin /*
4256423Sgw25295  * Create a minor node (plus a whole lot more) for the specified volume.
426789Sahrens  */
427789Sahrens int
4284787Sahrens zvol_create_minor(const char *name, major_t maj)
429789Sahrens {
430789Sahrens 	zvol_state_t *zv;
431789Sahrens 	objset_t *os;
4323063Sperrin 	dmu_object_info_t doi;
433789Sahrens 	uint64_t volsize;
434789Sahrens 	minor_t minor = 0;
435789Sahrens 	struct pathname linkpath;
436789Sahrens 	vnode_t *vp = NULL;
437789Sahrens 	char *devpath;
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 
44810298SMatthew.Ahrens@Sun.COM 	/* lie and say we're read-only */
44910298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, zvol_tag, &os);
450789Sahrens 
451789Sahrens 	if (error) {
452789Sahrens 		mutex_exit(&zvol_state_lock);
453789Sahrens 		return (error);
454789Sahrens 	}
455789Sahrens 
456789Sahrens 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
457789Sahrens 
458789Sahrens 	if (error) {
45910298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(os, zvol_tag);
460789Sahrens 		mutex_exit(&zvol_state_lock);
461789Sahrens 		return (error);
462789Sahrens 	}
463789Sahrens 
464789Sahrens 	/*
465789Sahrens 	 * If there's an existing /dev/zvol symlink, try to use the
466789Sahrens 	 * same minor number we used last time.
467789Sahrens 	 */
46810272SMatthew.Ahrens@Sun.COM 	devpath = kmem_asprintf("%s%s", ZVOL_FULL_DEV_DIR, name);
469789Sahrens 	error = lookupname(devpath, UIO_SYSSPACE, NO_FOLLOW, NULL, &vp);
47010272SMatthew.Ahrens@Sun.COM 	strfree(devpath);
471789Sahrens 
472789Sahrens 	if (error == 0 && vp->v_type != VLNK)
473789Sahrens 		error = EINVAL;
474789Sahrens 
475789Sahrens 	if (error == 0) {
476789Sahrens 		pn_alloc(&linkpath);
477789Sahrens 		error = pn_getsymlink(vp, &linkpath, kcred);
478789Sahrens 		if (error == 0) {
479789Sahrens 			char *ms = strstr(linkpath.pn_path, ZVOL_PSEUDO_DEV);
480789Sahrens 			if (ms != NULL) {
481789Sahrens 				ms += strlen(ZVOL_PSEUDO_DEV);
482789Sahrens 				minor = stoi(&ms);
483789Sahrens 			}
484789Sahrens 		}
485789Sahrens 		pn_free(&linkpath);
486789Sahrens 	}
487789Sahrens 
488789Sahrens 	if (vp != NULL)
489789Sahrens 		VN_RELE(vp);
490789Sahrens 
491789Sahrens 	/*
492789Sahrens 	 * If we found a minor but it's already in use, we must pick a new one.
493789Sahrens 	 */
494789Sahrens 	if (minor != 0 && ddi_get_soft_state(zvol_state, minor) != NULL)
495789Sahrens 		minor = 0;
496789Sahrens 
497789Sahrens 	if (minor == 0)
498789Sahrens 		minor = zvol_minor_alloc();
499789Sahrens 
500789Sahrens 	if (minor == 0) {
50110298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(os, zvol_tag);
502789Sahrens 		mutex_exit(&zvol_state_lock);
503789Sahrens 		return (ENXIO);
504789Sahrens 	}
505789Sahrens 
506789Sahrens 	if (ddi_soft_state_zalloc(zvol_state, minor) != DDI_SUCCESS) {
50710298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(os, zvol_tag);
508789Sahrens 		mutex_exit(&zvol_state_lock);
509789Sahrens 		return (EAGAIN);
510789Sahrens 	}
511789Sahrens 
5122676Seschrock 	(void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME,
5132676Seschrock 	    (char *)name);
514789Sahrens 
515789Sahrens 	(void) sprintf(chrbuf, "%uc,raw", minor);
516789Sahrens 
517789Sahrens 	if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR,
518789Sahrens 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
519789Sahrens 		ddi_soft_state_free(zvol_state, minor);
52010298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(os, zvol_tag);
521789Sahrens 		mutex_exit(&zvol_state_lock);
522789Sahrens 		return (EAGAIN);
523789Sahrens 	}
524789Sahrens 
525789Sahrens 	(void) sprintf(blkbuf, "%uc", minor);
526789Sahrens 
527789Sahrens 	if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK,
528789Sahrens 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
529789Sahrens 		ddi_remove_minor_node(zfs_dip, chrbuf);
530789Sahrens 		ddi_soft_state_free(zvol_state, minor);
53110298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(os, zvol_tag);
532789Sahrens 		mutex_exit(&zvol_state_lock);
533789Sahrens 		return (EAGAIN);
534789Sahrens 	}
535789Sahrens 
536789Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
537789Sahrens 
538789Sahrens 	(void) strcpy(zv->zv_name, name);
539789Sahrens 	zv->zv_min_bs = DEV_BSHIFT;
540789Sahrens 	zv->zv_minor = minor;
541789Sahrens 	zv->zv_volsize = volsize;
542789Sahrens 	zv->zv_objset = os;
54310298SMatthew.Ahrens@Sun.COM 	zv->zv_issnap = dmu_objset_is_snapshot(os);
5443063Sperrin 	zv->zv_zilog = zil_open(os, zvol_get_data);
5453755Sperrin 	mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
5463755Sperrin 	avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
5473755Sperrin 	    sizeof (rl_t), offsetof(rl_t, r_node));
5487837SMatthew.Ahrens@Sun.COM 	list_create(&zv->zv_extents, sizeof (zvol_extent_t),
5497837SMatthew.Ahrens@Sun.COM 	    offsetof(zvol_extent_t, ze_node));
5503063Sperrin 	/* get and cache the blocksize */
5513063Sperrin 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
5523063Sperrin 	ASSERT(error == 0);
5533063Sperrin 	zv->zv_volblocksize = doi.doi_data_block_size;
5541141Sperrin 
5558227SNeil.Perrin@Sun.COM 	zil_replay(os, zv, zvol_replay_vector);
5564787Sahrens 	zvol_size_changed(zv, maj);
557789Sahrens 
5581544Seschrock 	/* XXX this should handle the possible i/o error */
559789Sahrens 	VERIFY(dsl_prop_register(dmu_objset_ds(zv->zv_objset),
560789Sahrens 	    "readonly", zvol_readonly_changed_cb, zv) == 0);
561789Sahrens 
562789Sahrens 	zvol_minors++;
563789Sahrens 
564789Sahrens 	mutex_exit(&zvol_state_lock);
565789Sahrens 
566789Sahrens 	return (0);
567789Sahrens }
568789Sahrens 
569789Sahrens /*
570789Sahrens  * Remove minor node for the specified volume.
571789Sahrens  */
572789Sahrens int
5732676Seschrock zvol_remove_minor(const char *name)
574789Sahrens {
575789Sahrens 	zvol_state_t *zv;
576789Sahrens 	char namebuf[30];
577789Sahrens 
578789Sahrens 	mutex_enter(&zvol_state_lock);
579789Sahrens 
5802676Seschrock 	if ((zv = zvol_minor_lookup(name)) == NULL) {
581789Sahrens 		mutex_exit(&zvol_state_lock);
582789Sahrens 		return (ENXIO);
583789Sahrens 	}
584789Sahrens 
585789Sahrens 	if (zv->zv_total_opens != 0) {
586789Sahrens 		mutex_exit(&zvol_state_lock);
587789Sahrens 		return (EBUSY);
588789Sahrens 	}
589789Sahrens 
590789Sahrens 	(void) sprintf(namebuf, "%uc,raw", zv->zv_minor);
591789Sahrens 	ddi_remove_minor_node(zfs_dip, namebuf);
592789Sahrens 
593789Sahrens 	(void) sprintf(namebuf, "%uc", zv->zv_minor);
594789Sahrens 	ddi_remove_minor_node(zfs_dip, namebuf);
595789Sahrens 
596789Sahrens 	VERIFY(dsl_prop_unregister(dmu_objset_ds(zv->zv_objset),
597789Sahrens 	    "readonly", zvol_readonly_changed_cb, zv) == 0);
598789Sahrens 
5991141Sperrin 	zil_close(zv->zv_zilog);
6001141Sperrin 	zv->zv_zilog = NULL;
60110298SMatthew.Ahrens@Sun.COM 	dmu_objset_disown(zv->zv_objset, zvol_tag);
602789Sahrens 	zv->zv_objset = NULL;
6033755Sperrin 	avl_destroy(&zv->zv_znode.z_range_avl);
6043755Sperrin 	mutex_destroy(&zv->zv_znode.z_range_lock);
605789Sahrens 
606789Sahrens 	ddi_soft_state_free(zvol_state, zv->zv_minor);
607789Sahrens 
608789Sahrens 	zvol_minors--;
609789Sahrens 
610789Sahrens 	mutex_exit(&zvol_state_lock);
611789Sahrens 
612789Sahrens 	return (0);
613789Sahrens }
614789Sahrens 
6156423Sgw25295 int
6166423Sgw25295 zvol_prealloc(zvol_state_t *zv)
6176423Sgw25295 {
6186423Sgw25295 	objset_t *os = zv->zv_objset;
6196423Sgw25295 	dmu_tx_t *tx;
6206423Sgw25295 	uint64_t refd, avail, usedobjs, availobjs;
6216423Sgw25295 	uint64_t resid = zv->zv_volsize;
6226423Sgw25295 	uint64_t off = 0;
6236423Sgw25295 
6246423Sgw25295 	/* Check the space usage before attempting to allocate the space */
6256423Sgw25295 	dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs);
6266423Sgw25295 	if (avail < zv->zv_volsize)
6276423Sgw25295 		return (ENOSPC);
6286423Sgw25295 
6296423Sgw25295 	/* Free old extents if they exist */
6306423Sgw25295 	zvol_free_extents(zv);
6316423Sgw25295 
6326423Sgw25295 	while (resid != 0) {
6336423Sgw25295 		int error;
6346423Sgw25295 		uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE);
6356423Sgw25295 
6366423Sgw25295 		tx = dmu_tx_create(os);
6376423Sgw25295 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
6386423Sgw25295 		error = dmu_tx_assign(tx, TXG_WAIT);
6396423Sgw25295 		if (error) {
6406423Sgw25295 			dmu_tx_abort(tx);
6416992Smaybee 			(void) dmu_free_long_range(os, ZVOL_OBJ, 0, off);
6426423Sgw25295 			return (error);
6436423Sgw25295 		}
6447872STim.Haley@Sun.COM 		dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx);
6456423Sgw25295 		dmu_tx_commit(tx);
6466423Sgw25295 		off += bytes;
6476423Sgw25295 		resid -= bytes;
6486423Sgw25295 	}
6496423Sgw25295 	txg_wait_synced(dmu_objset_pool(os), 0);
6506423Sgw25295 
6516423Sgw25295 	return (0);
6526423Sgw25295 }
6536423Sgw25295 
6546423Sgw25295 int
6556423Sgw25295 zvol_update_volsize(zvol_state_t *zv, major_t maj, uint64_t volsize)
6566423Sgw25295 {
6576423Sgw25295 	dmu_tx_t *tx;
6586423Sgw25295 	int error;
6596423Sgw25295 
6606423Sgw25295 	ASSERT(MUTEX_HELD(&zvol_state_lock));
6616423Sgw25295 
6626423Sgw25295 	tx = dmu_tx_create(zv->zv_objset);
6636423Sgw25295 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
6646423Sgw25295 	error = dmu_tx_assign(tx, TXG_WAIT);
6656423Sgw25295 	if (error) {
6666423Sgw25295 		dmu_tx_abort(tx);
6676423Sgw25295 		return (error);
6686423Sgw25295 	}
6696423Sgw25295 
6706423Sgw25295 	error = zap_update(zv->zv_objset, ZVOL_ZAP_OBJ, "size", 8, 1,
6716423Sgw25295 	    &volsize, tx);
6726423Sgw25295 	dmu_tx_commit(tx);
6736423Sgw25295 
6746423Sgw25295 	if (error == 0)
6756992Smaybee 		error = dmu_free_long_range(zv->zv_objset,
6766992Smaybee 		    ZVOL_OBJ, volsize, DMU_OBJECT_END);
6776423Sgw25295 
6787265Sahrens 	/*
6797265Sahrens 	 * If we are using a faked-up state (zv_minor == 0) then don't
6807265Sahrens 	 * try to update the in-core zvol state.
6817265Sahrens 	 */
6827265Sahrens 	if (error == 0 && zv->zv_minor) {
6836423Sgw25295 		zv->zv_volsize = volsize;
6846423Sgw25295 		zvol_size_changed(zv, maj);
6856423Sgw25295 	}
6866423Sgw25295 	return (error);
6876423Sgw25295 }
6886423Sgw25295 
689789Sahrens int
6904787Sahrens zvol_set_volsize(const char *name, major_t maj, uint64_t volsize)
691789Sahrens {
692789Sahrens 	zvol_state_t *zv;
693789Sahrens 	int error;
6941133Seschrock 	dmu_object_info_t doi;
6956423Sgw25295 	uint64_t old_volsize = 0ULL;
6967265Sahrens 	zvol_state_t state = { 0 };
697789Sahrens 
698789Sahrens 	mutex_enter(&zvol_state_lock);
699789Sahrens 
7002676Seschrock 	if ((zv = zvol_minor_lookup(name)) == NULL) {
7017265Sahrens 		/*
7027265Sahrens 		 * If we are doing a "zfs clone -o volsize=", then the
7037265Sahrens 		 * minor node won't exist yet.
7047265Sahrens 		 */
70510298SMatthew.Ahrens@Sun.COM 		error = dmu_objset_own(name, DMU_OST_ZVOL, B_FALSE, FTAG,
7067265Sahrens 		    &state.zv_objset);
7077265Sahrens 		if (error != 0)
7087265Sahrens 			goto out;
7097265Sahrens 		zv = &state;
710789Sahrens 	}
7116423Sgw25295 	old_volsize = zv->zv_volsize;
712789Sahrens 
7131133Seschrock 	if ((error = dmu_object_info(zv->zv_objset, ZVOL_OBJ, &doi)) != 0 ||
7142676Seschrock 	    (error = zvol_check_volsize(volsize,
7157265Sahrens 	    doi.doi_data_block_size)) != 0)
7167265Sahrens 		goto out;
7171133Seschrock 
71810298SMatthew.Ahrens@Sun.COM 	if (zv->zv_flags & ZVOL_RDONLY || zv->zv_issnap) {
7197265Sahrens 		error = EROFS;
7207265Sahrens 		goto out;
721789Sahrens 	}
722789Sahrens 
7236423Sgw25295 	error = zvol_update_volsize(zv, maj, volsize);
724789Sahrens 
7256423Sgw25295 	/*
7266423Sgw25295 	 * Reinitialize the dump area to the new size. If we
7276423Sgw25295 	 * failed to resize the dump area then restore the it back to
7286423Sgw25295 	 * it's original size.
7296423Sgw25295 	 */
7306423Sgw25295 	if (error == 0 && zv->zv_flags & ZVOL_DUMPIFIED) {
7316423Sgw25295 		if ((error = zvol_dumpify(zv)) != 0 ||
7326423Sgw25295 		    (error = dumpvp_resize()) != 0) {
7336423Sgw25295 			(void) zvol_update_volsize(zv, maj, old_volsize);
7346423Sgw25295 			error = zvol_dumpify(zv);
7356423Sgw25295 		}
736789Sahrens 	}
737789Sahrens 
7389816SGeorge.Wilson@Sun.COM 	/*
7399816SGeorge.Wilson@Sun.COM 	 * Generate a LUN expansion event.
7409816SGeorge.Wilson@Sun.COM 	 */
7419816SGeorge.Wilson@Sun.COM 	if (error == 0) {
7429816SGeorge.Wilson@Sun.COM 		sysevent_id_t eid;
7439816SGeorge.Wilson@Sun.COM 		nvlist_t *attr;
7449816SGeorge.Wilson@Sun.COM 		char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
7459816SGeorge.Wilson@Sun.COM 
7469816SGeorge.Wilson@Sun.COM 		(void) snprintf(physpath, MAXPATHLEN, "%s%uc", ZVOL_PSEUDO_DEV,
7479816SGeorge.Wilson@Sun.COM 		    zv->zv_minor);
7489816SGeorge.Wilson@Sun.COM 
7499816SGeorge.Wilson@Sun.COM 		VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
7509816SGeorge.Wilson@Sun.COM 		VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
7519816SGeorge.Wilson@Sun.COM 
7529816SGeorge.Wilson@Sun.COM 		(void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
7539816SGeorge.Wilson@Sun.COM 		    ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
7549816SGeorge.Wilson@Sun.COM 
7559816SGeorge.Wilson@Sun.COM 		nvlist_free(attr);
7569816SGeorge.Wilson@Sun.COM 		kmem_free(physpath, MAXPATHLEN);
7579816SGeorge.Wilson@Sun.COM 	}
7589816SGeorge.Wilson@Sun.COM 
7597265Sahrens out:
7607265Sahrens 	if (state.zv_objset)
76110298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(state.zv_objset, FTAG);
7627265Sahrens 
763789Sahrens 	mutex_exit(&zvol_state_lock);
764789Sahrens 
765789Sahrens 	return (error);
766789Sahrens }
767789Sahrens 
768789Sahrens int
7692676Seschrock zvol_set_volblocksize(const char *name, uint64_t volblocksize)
770789Sahrens {
771789Sahrens 	zvol_state_t *zv;
772789Sahrens 	dmu_tx_t *tx;
773789Sahrens 	int error;
7747837SMatthew.Ahrens@Sun.COM 	boolean_t needlock;
775789Sahrens 
7767837SMatthew.Ahrens@Sun.COM 	/*
7777837SMatthew.Ahrens@Sun.COM 	 * The lock may already be held if we are being called from
7787837SMatthew.Ahrens@Sun.COM 	 * zvol_dump_init().
7797837SMatthew.Ahrens@Sun.COM 	 */
7807837SMatthew.Ahrens@Sun.COM 	needlock = !MUTEX_HELD(&zvol_state_lock);
7817837SMatthew.Ahrens@Sun.COM 	if (needlock)
7827837SMatthew.Ahrens@Sun.COM 		mutex_enter(&zvol_state_lock);
783789Sahrens 
7842676Seschrock 	if ((zv = zvol_minor_lookup(name)) == NULL) {
7857837SMatthew.Ahrens@Sun.COM 		if (needlock)
7867837SMatthew.Ahrens@Sun.COM 			mutex_exit(&zvol_state_lock);
787789Sahrens 		return (ENXIO);
788789Sahrens 	}
78910298SMatthew.Ahrens@Sun.COM 	if (zv->zv_flags & ZVOL_RDONLY || zv->zv_issnap) {
7907837SMatthew.Ahrens@Sun.COM 		if (needlock)
7917837SMatthew.Ahrens@Sun.COM 			mutex_exit(&zvol_state_lock);
792789Sahrens 		return (EROFS);
793789Sahrens 	}
794789Sahrens 
795789Sahrens 	tx = dmu_tx_create(zv->zv_objset);
796789Sahrens 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
797789Sahrens 	error = dmu_tx_assign(tx, TXG_WAIT);
798789Sahrens 	if (error) {
799789Sahrens 		dmu_tx_abort(tx);
800789Sahrens 	} else {
801789Sahrens 		error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ,
8022676Seschrock 		    volblocksize, 0, tx);
803789Sahrens 		if (error == ENOTSUP)
804789Sahrens 			error = EBUSY;
805789Sahrens 		dmu_tx_commit(tx);
8067837SMatthew.Ahrens@Sun.COM 		if (error == 0)
8077837SMatthew.Ahrens@Sun.COM 			zv->zv_volblocksize = volblocksize;
808789Sahrens 	}
809789Sahrens 
8107837SMatthew.Ahrens@Sun.COM 	if (needlock)
8117837SMatthew.Ahrens@Sun.COM 		mutex_exit(&zvol_state_lock);
812789Sahrens 
813789Sahrens 	return (error);
814789Sahrens }
815789Sahrens 
816789Sahrens /*ARGSUSED*/
817789Sahrens int
818789Sahrens zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr)
819789Sahrens {
820789Sahrens 	minor_t minor = getminor(*devp);
821789Sahrens 	zvol_state_t *zv;
822789Sahrens 
823789Sahrens 	if (minor == 0)			/* This is the control device */
824789Sahrens 		return (0);
825789Sahrens 
826789Sahrens 	mutex_enter(&zvol_state_lock);
827789Sahrens 
828789Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
829789Sahrens 	if (zv == NULL) {
830789Sahrens 		mutex_exit(&zvol_state_lock);
831789Sahrens 		return (ENXIO);
832789Sahrens 	}
833789Sahrens 
834789Sahrens 	ASSERT(zv->zv_objset != NULL);
835789Sahrens 
836789Sahrens 	if ((flag & FWRITE) &&
83710298SMatthew.Ahrens@Sun.COM 	    (zv->zv_flags & ZVOL_RDONLY || zv->zv_issnap)) {
838789Sahrens 		mutex_exit(&zvol_state_lock);
839789Sahrens 		return (EROFS);
840789Sahrens 	}
8417405SEric.Taylor@Sun.COM 	if (zv->zv_flags & ZVOL_EXCL) {
8427405SEric.Taylor@Sun.COM 		mutex_exit(&zvol_state_lock);
8437405SEric.Taylor@Sun.COM 		return (EBUSY);
8447405SEric.Taylor@Sun.COM 	}
8457405SEric.Taylor@Sun.COM 	if (flag & FEXCL) {
8467405SEric.Taylor@Sun.COM 		if (zv->zv_total_opens != 0) {
8477405SEric.Taylor@Sun.COM 			mutex_exit(&zvol_state_lock);
8487405SEric.Taylor@Sun.COM 			return (EBUSY);
8497405SEric.Taylor@Sun.COM 		}
8507405SEric.Taylor@Sun.COM 		zv->zv_flags |= ZVOL_EXCL;
8517405SEric.Taylor@Sun.COM 	}
852789Sahrens 
853789Sahrens 	if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) {
854789Sahrens 		zv->zv_open_count[otyp]++;
855789Sahrens 		zv->zv_total_opens++;
856789Sahrens 	}
857789Sahrens 
858789Sahrens 	mutex_exit(&zvol_state_lock);
859789Sahrens 
860789Sahrens 	return (0);
861789Sahrens }
862789Sahrens 
863789Sahrens /*ARGSUSED*/
864789Sahrens int
865789Sahrens zvol_close(dev_t dev, int flag, int otyp, cred_t *cr)
866789Sahrens {
867789Sahrens 	minor_t minor = getminor(dev);
868789Sahrens 	zvol_state_t *zv;
869789Sahrens 
870789Sahrens 	if (minor == 0)		/* This is the control device */
871789Sahrens 		return (0);
872789Sahrens 
873789Sahrens 	mutex_enter(&zvol_state_lock);
874789Sahrens 
875789Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
876789Sahrens 	if (zv == NULL) {
877789Sahrens 		mutex_exit(&zvol_state_lock);
878789Sahrens 		return (ENXIO);
879789Sahrens 	}
880789Sahrens 
8817405SEric.Taylor@Sun.COM 	if (zv->zv_flags & ZVOL_EXCL) {
8827405SEric.Taylor@Sun.COM 		ASSERT(zv->zv_total_opens == 1);
8837405SEric.Taylor@Sun.COM 		zv->zv_flags &= ~ZVOL_EXCL;
884789Sahrens 	}
885789Sahrens 
886789Sahrens 	/*
887789Sahrens 	 * If the open count is zero, this is a spurious close.
888789Sahrens 	 * That indicates a bug in the kernel / DDI framework.
889789Sahrens 	 */
890789Sahrens 	ASSERT(zv->zv_open_count[otyp] != 0);
891789Sahrens 	ASSERT(zv->zv_total_opens != 0);
892789Sahrens 
893789Sahrens 	/*
894789Sahrens 	 * You may get multiple opens, but only one close.
895789Sahrens 	 */
896789Sahrens 	zv->zv_open_count[otyp]--;
897789Sahrens 	zv->zv_total_opens--;
898789Sahrens 
899789Sahrens 	mutex_exit(&zvol_state_lock);
900789Sahrens 
901789Sahrens 	return (0);
902789Sahrens }
903789Sahrens 
9043638Sbillm static void
9053063Sperrin zvol_get_done(dmu_buf_t *db, void *vzgd)
9063063Sperrin {
9073063Sperrin 	zgd_t *zgd = (zgd_t *)vzgd;
9083755Sperrin 	rl_t *rl = zgd->zgd_rl;
9093063Sperrin 
9103063Sperrin 	dmu_buf_rele(db, vzgd);
9113755Sperrin 	zfs_range_unlock(rl);
9125688Sbonwick 	zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
9133063Sperrin 	kmem_free(zgd, sizeof (zgd_t));
9143063Sperrin }
9153063Sperrin 
9163063Sperrin /*
9173063Sperrin  * Get data to generate a TX_WRITE intent log record.
9183063Sperrin  */
9193638Sbillm static int
9203063Sperrin zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
9213063Sperrin {
9223063Sperrin 	zvol_state_t *zv = arg;
9233063Sperrin 	objset_t *os = zv->zv_objset;
9243063Sperrin 	dmu_buf_t *db;
9253755Sperrin 	rl_t *rl;
9263063Sperrin 	zgd_t *zgd;
9273755Sperrin 	uint64_t boff; 			/* block starting offset */
9283755Sperrin 	int dlen = lr->lr_length;	/* length of user data */
9293063Sperrin 	int error;
9303063Sperrin 
9313063Sperrin 	ASSERT(zio);
9323755Sperrin 	ASSERT(dlen != 0);
9333638Sbillm 
9343755Sperrin 	/*
9353755Sperrin 	 * Write records come in two flavors: immediate and indirect.
9363755Sperrin 	 * For small writes it's cheaper to store the data with the
9373755Sperrin 	 * log record (immediate); for large writes it's cheaper to
9383755Sperrin 	 * sync the data and get a pointer to it (indirect) so that
9393755Sperrin 	 * we don't have to write the data twice.
9403755Sperrin 	 */
9413755Sperrin 	if (buf != NULL) /* immediate write */
9429512SNeil.Perrin@Sun.COM 		return (dmu_read(os, ZVOL_OBJ, lr->lr_offset, dlen, buf,
9439512SNeil.Perrin@Sun.COM 		    DMU_READ_NO_PREFETCH));
9443063Sperrin 
9453063Sperrin 	zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP);
9463063Sperrin 	zgd->zgd_zilog = zv->zv_zilog;
9473063Sperrin 	zgd->zgd_bp = &lr->lr_blkptr;
9483063Sperrin 
9493755Sperrin 	/*
9503755Sperrin 	 * Lock the range of the block to ensure that when the data is
9516423Sgw25295 	 * written out and its checksum is being calculated that no other
9523755Sperrin 	 * thread can change the block.
9533755Sperrin 	 */
9543755Sperrin 	boff = P2ALIGN_TYPED(lr->lr_offset, zv->zv_volblocksize, uint64_t);
9553755Sperrin 	rl = zfs_range_lock(&zv->zv_znode, boff, zv->zv_volblocksize,
9563755Sperrin 	    RL_READER);
9573755Sperrin 	zgd->zgd_rl = rl;
9583755Sperrin 
9593063Sperrin 	VERIFY(0 == dmu_buf_hold(os, ZVOL_OBJ, lr->lr_offset, zgd, &db));
9603063Sperrin 	error = dmu_sync(zio, db, &lr->lr_blkptr,
9613063Sperrin 	    lr->lr_common.lrc_txg, zvol_get_done, zgd);
9623638Sbillm 	if (error == 0)
9635688Sbonwick 		zil_add_block(zv->zv_zilog, &lr->lr_blkptr);
9643063Sperrin 	/*
9653063Sperrin 	 * If we get EINPROGRESS, then we need to wait for a
9663063Sperrin 	 * write IO initiated by dmu_sync() to complete before
9673063Sperrin 	 * we can release this dbuf.  We will finish everything
9683063Sperrin 	 * up in the zvol_get_done() callback.
9693063Sperrin 	 */
9703063Sperrin 	if (error == EINPROGRESS)
9713063Sperrin 		return (0);
9723063Sperrin 	dmu_buf_rele(db, zgd);
9733755Sperrin 	zfs_range_unlock(rl);
9743063Sperrin 	kmem_free(zgd, sizeof (zgd_t));
9753063Sperrin 	return (error);
9763063Sperrin }
9773063Sperrin 
9781861Sperrin /*
9791861Sperrin  * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
9801141Sperrin  *
9811141Sperrin  * We store data in the log buffers if it's small enough.
9823063Sperrin  * Otherwise we will later flush the data out via dmu_sync().
9831141Sperrin  */
9843063Sperrin ssize_t zvol_immediate_write_sz = 32768;
9851141Sperrin 
9863638Sbillm static void
9879401SNeil.Perrin@Sun.COM zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid,
9889401SNeil.Perrin@Sun.COM     boolean_t sync)
9891141Sperrin {
9903638Sbillm 	uint32_t blocksize = zv->zv_volblocksize;
9918227SNeil.Perrin@Sun.COM 	zilog_t *zilog = zv->zv_zilog;
9929401SNeil.Perrin@Sun.COM 	boolean_t slogging;
993*10310SNeil.Perrin@Sun.COM 	ssize_t immediate_write_sz;
9949401SNeil.Perrin@Sun.COM 
9959401SNeil.Perrin@Sun.COM 	if (zil_disable)
9969401SNeil.Perrin@Sun.COM 		return;
9971861Sperrin 
9988227SNeil.Perrin@Sun.COM 	if (zilog->zl_replay) {
9998227SNeil.Perrin@Sun.COM 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
10008227SNeil.Perrin@Sun.COM 		zilog->zl_replayed_seq[dmu_tx_get_txg(tx) & TXG_MASK] =
10018227SNeil.Perrin@Sun.COM 		    zilog->zl_replaying_seq;
10028227SNeil.Perrin@Sun.COM 		return;
10038227SNeil.Perrin@Sun.COM 	}
10048227SNeil.Perrin@Sun.COM 
1005*10310SNeil.Perrin@Sun.COM 	immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
1006*10310SNeil.Perrin@Sun.COM 	    ? 0 : zvol_immediate_write_sz;
1007*10310SNeil.Perrin@Sun.COM 
1008*10310SNeil.Perrin@Sun.COM 	slogging = spa_has_slogs(zilog->zl_spa) &&
1009*10310SNeil.Perrin@Sun.COM 	    (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
10109401SNeil.Perrin@Sun.COM 
10119401SNeil.Perrin@Sun.COM 	while (resid) {
10129401SNeil.Perrin@Sun.COM 		itx_t *itx;
10139401SNeil.Perrin@Sun.COM 		lr_write_t *lr;
10149401SNeil.Perrin@Sun.COM 		ssize_t len;
10159401SNeil.Perrin@Sun.COM 		itx_wr_state_t write_state;
10163638Sbillm 
10179401SNeil.Perrin@Sun.COM 		/*
10189401SNeil.Perrin@Sun.COM 		 * Unlike zfs_log_write() we can be called with
10199401SNeil.Perrin@Sun.COM 		 * upto DMU_MAX_ACCESS/2 (5MB) writes.
10209401SNeil.Perrin@Sun.COM 		 */
1021*10310SNeil.Perrin@Sun.COM 		if (blocksize > immediate_write_sz && !slogging &&
10229401SNeil.Perrin@Sun.COM 		    resid >= blocksize && off % blocksize == 0) {
10239401SNeil.Perrin@Sun.COM 			write_state = WR_INDIRECT; /* uses dmu_sync */
10249401SNeil.Perrin@Sun.COM 			len = blocksize;
10259401SNeil.Perrin@Sun.COM 		} else if (sync) {
10269401SNeil.Perrin@Sun.COM 			write_state = WR_COPIED;
10279401SNeil.Perrin@Sun.COM 			len = MIN(ZIL_MAX_LOG_DATA, resid);
10289401SNeil.Perrin@Sun.COM 		} else {
10299401SNeil.Perrin@Sun.COM 			write_state = WR_NEED_COPY;
10309401SNeil.Perrin@Sun.COM 			len = MIN(ZIL_MAX_LOG_DATA, resid);
10319401SNeil.Perrin@Sun.COM 		}
10329401SNeil.Perrin@Sun.COM 
10339401SNeil.Perrin@Sun.COM 		itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
10349401SNeil.Perrin@Sun.COM 		    (write_state == WR_COPIED ? len : 0));
10353638Sbillm 		lr = (lr_write_t *)&itx->itx_lr;
10369401SNeil.Perrin@Sun.COM 		if (write_state == WR_COPIED && dmu_read(zv->zv_objset,
10379512SNeil.Perrin@Sun.COM 		    ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
10389401SNeil.Perrin@Sun.COM 			kmem_free(itx, offsetof(itx_t, itx_lr) +
10399401SNeil.Perrin@Sun.COM 			    itx->itx_lr.lrc_reclen);
10409401SNeil.Perrin@Sun.COM 			itx = zil_itx_create(TX_WRITE, sizeof (*lr));
10419401SNeil.Perrin@Sun.COM 			lr = (lr_write_t *)&itx->itx_lr;
10429401SNeil.Perrin@Sun.COM 			write_state = WR_NEED_COPY;
10439401SNeil.Perrin@Sun.COM 		}
10449401SNeil.Perrin@Sun.COM 
10459401SNeil.Perrin@Sun.COM 		itx->itx_wr_state = write_state;
10469401SNeil.Perrin@Sun.COM 		if (write_state == WR_NEED_COPY)
10479401SNeil.Perrin@Sun.COM 			itx->itx_sod += len;
10483638Sbillm 		lr->lr_foid = ZVOL_OBJ;
10493638Sbillm 		lr->lr_offset = off;
10509401SNeil.Perrin@Sun.COM 		lr->lr_length = len;
10513638Sbillm 		lr->lr_blkoff = off - P2ALIGN_TYPED(off, blocksize, uint64_t);
10523638Sbillm 		BP_ZERO(&lr->lr_blkptr);
10533638Sbillm 
10549401SNeil.Perrin@Sun.COM 		itx->itx_private = zv;
10559401SNeil.Perrin@Sun.COM 		itx->itx_sync = sync;
10569401SNeil.Perrin@Sun.COM 
10578227SNeil.Perrin@Sun.COM 		(void) zil_itx_assign(zilog, itx, tx);
10589401SNeil.Perrin@Sun.COM 
10599401SNeil.Perrin@Sun.COM 		off += len;
10609401SNeil.Perrin@Sun.COM 		resid -= len;
10611141Sperrin 	}
10621141Sperrin }
10631141Sperrin 
10647837SMatthew.Ahrens@Sun.COM static int
10657837SMatthew.Ahrens@Sun.COM zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t size,
10667837SMatthew.Ahrens@Sun.COM     boolean_t doread, boolean_t isdump)
10676423Sgw25295 {
10686423Sgw25295 	vdev_disk_t *dvd;
10696423Sgw25295 	int c;
10706423Sgw25295 	int numerrors = 0;
10716423Sgw25295 
10726423Sgw25295 	for (c = 0; c < vd->vdev_children; c++) {
10739790SLin.Ling@Sun.COM 		ASSERT(vd->vdev_ops == &vdev_mirror_ops ||
10749790SLin.Ling@Sun.COM 		    vd->vdev_ops == &vdev_replacing_ops ||
10759790SLin.Ling@Sun.COM 		    vd->vdev_ops == &vdev_spare_ops);
10767837SMatthew.Ahrens@Sun.COM 		int err = zvol_dumpio_vdev(vd->vdev_child[c],
10777837SMatthew.Ahrens@Sun.COM 		    addr, offset, size, doread, isdump);
10787837SMatthew.Ahrens@Sun.COM 		if (err != 0) {
10796423Sgw25295 			numerrors++;
10807837SMatthew.Ahrens@Sun.COM 		} else if (doread) {
10816423Sgw25295 			break;
10826423Sgw25295 		}
10836423Sgw25295 	}
10846423Sgw25295 
10856423Sgw25295 	if (!vd->vdev_ops->vdev_op_leaf)
10866423Sgw25295 		return (numerrors < vd->vdev_children ? 0 : EIO);
10876423Sgw25295 
10887903SEric.Taylor@Sun.COM 	if (doread && !vdev_readable(vd))
10897903SEric.Taylor@Sun.COM 		return (EIO);
10907903SEric.Taylor@Sun.COM 	else if (!doread && !vdev_writeable(vd))
10916423Sgw25295 		return (EIO);
10926423Sgw25295 
10936423Sgw25295 	dvd = vd->vdev_tsd;
10946423Sgw25295 	ASSERT3P(dvd, !=, NULL);
10956423Sgw25295 	offset += VDEV_LABEL_START_SIZE;
10966423Sgw25295 
10976423Sgw25295 	if (ddi_in_panic() || isdump) {
10987837SMatthew.Ahrens@Sun.COM 		ASSERT(!doread);
10997837SMatthew.Ahrens@Sun.COM 		if (doread)
11006423Sgw25295 			return (EIO);
11016423Sgw25295 		return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset),
11026423Sgw25295 		    lbtodb(size)));
11036423Sgw25295 	} else {
11046423Sgw25295 		return (vdev_disk_physio(dvd->vd_lh, addr, size, offset,
11057837SMatthew.Ahrens@Sun.COM 		    doread ? B_READ : B_WRITE));
11066423Sgw25295 	}
11076423Sgw25295 }
11086423Sgw25295 
11097837SMatthew.Ahrens@Sun.COM static int
11107837SMatthew.Ahrens@Sun.COM zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size,
11117837SMatthew.Ahrens@Sun.COM     boolean_t doread, boolean_t isdump)
11126423Sgw25295 {
11136423Sgw25295 	vdev_t *vd;
11146423Sgw25295 	int error;
11157837SMatthew.Ahrens@Sun.COM 	zvol_extent_t *ze;
11166423Sgw25295 	spa_t *spa = dmu_objset_spa(zv->zv_objset);
11176423Sgw25295 
11187837SMatthew.Ahrens@Sun.COM 	/* Must be sector aligned, and not stradle a block boundary. */
11197837SMatthew.Ahrens@Sun.COM 	if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) ||
11207837SMatthew.Ahrens@Sun.COM 	    P2BOUNDARY(offset, size, zv->zv_volblocksize)) {
11217837SMatthew.Ahrens@Sun.COM 		return (EINVAL);
11227837SMatthew.Ahrens@Sun.COM 	}
11236423Sgw25295 	ASSERT(size <= zv->zv_volblocksize);
11246423Sgw25295 
11257837SMatthew.Ahrens@Sun.COM 	/* Locate the extent this belongs to */
11267837SMatthew.Ahrens@Sun.COM 	ze = list_head(&zv->zv_extents);
11277837SMatthew.Ahrens@Sun.COM 	while (offset >= ze->ze_nblks * zv->zv_volblocksize) {
11287837SMatthew.Ahrens@Sun.COM 		offset -= ze->ze_nblks * zv->zv_volblocksize;
11297837SMatthew.Ahrens@Sun.COM 		ze = list_next(&zv->zv_extents, ze);
11307837SMatthew.Ahrens@Sun.COM 	}
11317754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
11327837SMatthew.Ahrens@Sun.COM 	vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva));
11337837SMatthew.Ahrens@Sun.COM 	offset += DVA_GET_OFFSET(&ze->ze_dva);
11347837SMatthew.Ahrens@Sun.COM 	error = zvol_dumpio_vdev(vd, addr, offset, size, doread, isdump);
11357754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
11366423Sgw25295 	return (error);
11376423Sgw25295 }
11386423Sgw25295 
11396423Sgw25295 int
1140789Sahrens zvol_strategy(buf_t *bp)
1141789Sahrens {
1142789Sahrens 	zvol_state_t *zv = ddi_get_soft_state(zvol_state, getminor(bp->b_edev));
1143789Sahrens 	uint64_t off, volsize;
11447837SMatthew.Ahrens@Sun.COM 	size_t resid;
1145789Sahrens 	char *addr;
11461141Sperrin 	objset_t *os;
11473755Sperrin 	rl_t *rl;
1148789Sahrens 	int error = 0;
11497837SMatthew.Ahrens@Sun.COM 	boolean_t doread = bp->b_flags & B_READ;
11507837SMatthew.Ahrens@Sun.COM 	boolean_t is_dump = zv->zv_flags & ZVOL_DUMPIFIED;
11519401SNeil.Perrin@Sun.COM 	boolean_t sync;
1152789Sahrens 
1153789Sahrens 	if (zv == NULL) {
1154789Sahrens 		bioerror(bp, ENXIO);
1155789Sahrens 		biodone(bp);
1156789Sahrens 		return (0);
1157789Sahrens 	}
1158789Sahrens 
1159789Sahrens 	if (getminor(bp->b_edev) == 0) {
1160789Sahrens 		bioerror(bp, EINVAL);
1161789Sahrens 		biodone(bp);
1162789Sahrens 		return (0);
1163789Sahrens 	}
1164789Sahrens 
11656423Sgw25295 	if (!(bp->b_flags & B_READ) &&
116610298SMatthew.Ahrens@Sun.COM 	    (zv->zv_flags & ZVOL_RDONLY || zv->zv_issnap)) {
1167789Sahrens 		bioerror(bp, EROFS);
1168789Sahrens 		biodone(bp);
1169789Sahrens 		return (0);
1170789Sahrens 	}
1171789Sahrens 
1172789Sahrens 	off = ldbtob(bp->b_blkno);
1173789Sahrens 	volsize = zv->zv_volsize;
1174789Sahrens 
11751141Sperrin 	os = zv->zv_objset;
11761141Sperrin 	ASSERT(os != NULL);
1177789Sahrens 
1178789Sahrens 	bp_mapin(bp);
1179789Sahrens 	addr = bp->b_un.b_addr;
1180789Sahrens 	resid = bp->b_bcount;
1181789Sahrens 
11827837SMatthew.Ahrens@Sun.COM 	if (resid > 0 && (off < 0 || off >= volsize)) {
11837837SMatthew.Ahrens@Sun.COM 		bioerror(bp, EIO);
11847837SMatthew.Ahrens@Sun.COM 		biodone(bp);
11857837SMatthew.Ahrens@Sun.COM 		return (0);
11867837SMatthew.Ahrens@Sun.COM 	}
11877013Sgw25295 
11889401SNeil.Perrin@Sun.COM 	sync = !(bp->b_flags & B_ASYNC) && !doread && !is_dump &&
11899401SNeil.Perrin@Sun.COM 	    !(zv->zv_flags & ZVOL_WCE) && !zil_disable;
11909401SNeil.Perrin@Sun.COM 
11911861Sperrin 	/*
11921861Sperrin 	 * There must be no buffer changes when doing a dmu_sync() because
11931861Sperrin 	 * we can't change the data whilst calculating the checksum.
11941861Sperrin 	 */
11953755Sperrin 	rl = zfs_range_lock(&zv->zv_znode, off, resid,
11967837SMatthew.Ahrens@Sun.COM 	    doread ? RL_READER : RL_WRITER);
11976423Sgw25295 
1198789Sahrens 	while (resid != 0 && off < volsize) {
11997837SMatthew.Ahrens@Sun.COM 		size_t size = MIN(resid, zvol_maxphys);
12006423Sgw25295 		if (is_dump) {
12016423Sgw25295 			size = MIN(size, P2END(off, zv->zv_volblocksize) - off);
12027837SMatthew.Ahrens@Sun.COM 			error = zvol_dumpio(zv, addr, off, size,
12037837SMatthew.Ahrens@Sun.COM 			    doread, B_FALSE);
12047837SMatthew.Ahrens@Sun.COM 		} else if (doread) {
12059512SNeil.Perrin@Sun.COM 			error = dmu_read(os, ZVOL_OBJ, off, size, addr,
12069512SNeil.Perrin@Sun.COM 			    DMU_READ_PREFETCH);
1207789Sahrens 		} else {
12081141Sperrin 			dmu_tx_t *tx = dmu_tx_create(os);
1209789Sahrens 			dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
1210789Sahrens 			error = dmu_tx_assign(tx, TXG_WAIT);
1211789Sahrens 			if (error) {
1212789Sahrens 				dmu_tx_abort(tx);
1213789Sahrens 			} else {
12141141Sperrin 				dmu_write(os, ZVOL_OBJ, off, size, addr, tx);
12159401SNeil.Perrin@Sun.COM 				zvol_log_write(zv, tx, off, size, sync);
1216789Sahrens 				dmu_tx_commit(tx);
1217789Sahrens 			}
1218789Sahrens 		}
12197294Sperrin 		if (error) {
12207294Sperrin 			/* convert checksum errors into IO errors */
12217294Sperrin 			if (error == ECKSUM)
12227294Sperrin 				error = EIO;
1223789Sahrens 			break;
12247294Sperrin 		}
1225789Sahrens 		off += size;
1226789Sahrens 		addr += size;
1227789Sahrens 		resid -= size;
1228789Sahrens 	}
12293755Sperrin 	zfs_range_unlock(rl);
1230789Sahrens 
1231789Sahrens 	if ((bp->b_resid = resid) == bp->b_bcount)
1232789Sahrens 		bioerror(bp, off > volsize ? EINVAL : error);
1233789Sahrens 
12349401SNeil.Perrin@Sun.COM 	if (sync)
12353638Sbillm 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
12363638Sbillm 	biodone(bp);
12371141Sperrin 
1238789Sahrens 	return (0);
1239789Sahrens }
1240789Sahrens 
12413063Sperrin /*
12423063Sperrin  * Set the buffer count to the zvol maximum transfer.
12433063Sperrin  * Using our own routine instead of the default minphys()
12443063Sperrin  * means that for larger writes we write bigger buffers on X86
12453063Sperrin  * (128K instead of 56K) and flush the disk write cache less often
12463063Sperrin  * (every zvol_maxphys - currently 1MB) instead of minphys (currently
12473063Sperrin  * 56K on X86 and 128K on sparc).
12483063Sperrin  */
12493063Sperrin void
12503063Sperrin zvol_minphys(struct buf *bp)
12513063Sperrin {
12523063Sperrin 	if (bp->b_bcount > zvol_maxphys)
12533063Sperrin 		bp->b_bcount = zvol_maxphys;
12543063Sperrin }
12553063Sperrin 
12566423Sgw25295 int
12576423Sgw25295 zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks)
12586423Sgw25295 {
12596423Sgw25295 	minor_t minor = getminor(dev);
12606423Sgw25295 	zvol_state_t *zv;
12616423Sgw25295 	int error = 0;
12626423Sgw25295 	uint64_t size;
12636423Sgw25295 	uint64_t boff;
12646423Sgw25295 	uint64_t resid;
12656423Sgw25295 
12666423Sgw25295 	if (minor == 0)			/* This is the control device */
12676423Sgw25295 		return (ENXIO);
12686423Sgw25295 
12696423Sgw25295 	zv = ddi_get_soft_state(zvol_state, minor);
12706423Sgw25295 	if (zv == NULL)
12716423Sgw25295 		return (ENXIO);
12726423Sgw25295 
12736423Sgw25295 	boff = ldbtob(blkno);
12746423Sgw25295 	resid = ldbtob(nblocks);
12757837SMatthew.Ahrens@Sun.COM 
12767837SMatthew.Ahrens@Sun.COM 	VERIFY3U(boff + resid, <=, zv->zv_volsize);
12777837SMatthew.Ahrens@Sun.COM 
12786423Sgw25295 	while (resid) {
12796423Sgw25295 		size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff);
12807837SMatthew.Ahrens@Sun.COM 		error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE);
12816423Sgw25295 		if (error)
12826423Sgw25295 			break;
12836423Sgw25295 		boff += size;
12846423Sgw25295 		addr += size;
12856423Sgw25295 		resid -= size;
12866423Sgw25295 	}
12876423Sgw25295 
12886423Sgw25295 	return (error);
12896423Sgw25295 }
12906423Sgw25295 
1291789Sahrens /*ARGSUSED*/
1292789Sahrens int
12933638Sbillm zvol_read(dev_t dev, uio_t *uio, cred_t *cr)
1294789Sahrens {
12954107Sgw25295 	minor_t minor = getminor(dev);
12964107Sgw25295 	zvol_state_t *zv;
12977013Sgw25295 	uint64_t volsize;
12983755Sperrin 	rl_t *rl;
12993638Sbillm 	int error = 0;
13003638Sbillm 
13014107Sgw25295 	if (minor == 0)			/* This is the control device */
13024107Sgw25295 		return (ENXIO);
13034107Sgw25295 
13044107Sgw25295 	zv = ddi_get_soft_state(zvol_state, minor);
13054107Sgw25295 	if (zv == NULL)
13064107Sgw25295 		return (ENXIO);
13074107Sgw25295 
13087013Sgw25295 	volsize = zv->zv_volsize;
13097013Sgw25295 	if (uio->uio_resid > 0 &&
13107013Sgw25295 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
13117013Sgw25295 		return (EIO);
13127013Sgw25295 
13137837SMatthew.Ahrens@Sun.COM 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
13147837SMatthew.Ahrens@Sun.COM 		error = physio(zvol_strategy, NULL, dev, B_READ,
13157837SMatthew.Ahrens@Sun.COM 		    zvol_minphys, uio);
13167837SMatthew.Ahrens@Sun.COM 		return (error);
13177837SMatthew.Ahrens@Sun.COM 	}
13187837SMatthew.Ahrens@Sun.COM 
13193755Sperrin 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
13203755Sperrin 	    RL_READER);
13217013Sgw25295 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
13223638Sbillm 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
13233638Sbillm 
13247013Sgw25295 		/* don't read past the end */
13257013Sgw25295 		if (bytes > volsize - uio->uio_loffset)
13267013Sgw25295 			bytes = volsize - uio->uio_loffset;
13277013Sgw25295 
13283638Sbillm 		error =  dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes);
13297294Sperrin 		if (error) {
13307294Sperrin 			/* convert checksum errors into IO errors */
13317294Sperrin 			if (error == ECKSUM)
13327294Sperrin 				error = EIO;
13333638Sbillm 			break;
13347294Sperrin 		}
13353638Sbillm 	}
13363755Sperrin 	zfs_range_unlock(rl);
13373638Sbillm 	return (error);
1338789Sahrens }
1339789Sahrens 
1340789Sahrens /*ARGSUSED*/
1341789Sahrens int
13423638Sbillm zvol_write(dev_t dev, uio_t *uio, cred_t *cr)
1343789Sahrens {
13444107Sgw25295 	minor_t minor = getminor(dev);
13454107Sgw25295 	zvol_state_t *zv;
13467013Sgw25295 	uint64_t volsize;
13473755Sperrin 	rl_t *rl;
13483638Sbillm 	int error = 0;
13499401SNeil.Perrin@Sun.COM 	boolean_t sync;
13503638Sbillm 
13514107Sgw25295 	if (minor == 0)			/* This is the control device */
13524107Sgw25295 		return (ENXIO);
13534107Sgw25295 
13544107Sgw25295 	zv = ddi_get_soft_state(zvol_state, minor);
13554107Sgw25295 	if (zv == NULL)
13564107Sgw25295 		return (ENXIO);
13574107Sgw25295 
13587013Sgw25295 	volsize = zv->zv_volsize;
13597013Sgw25295 	if (uio->uio_resid > 0 &&
13607013Sgw25295 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
13617013Sgw25295 		return (EIO);
13627013Sgw25295 
13636423Sgw25295 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
13646423Sgw25295 		error = physio(zvol_strategy, NULL, dev, B_WRITE,
13656423Sgw25295 		    zvol_minphys, uio);
13666423Sgw25295 		return (error);
13676423Sgw25295 	}
13686423Sgw25295 
13699401SNeil.Perrin@Sun.COM 	sync = !(zv->zv_flags & ZVOL_WCE) && !zil_disable;
13709401SNeil.Perrin@Sun.COM 
13713755Sperrin 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
13723755Sperrin 	    RL_WRITER);
13737013Sgw25295 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
13743638Sbillm 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
13753638Sbillm 		uint64_t off = uio->uio_loffset;
13767013Sgw25295 		dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
1377789Sahrens 
13787013Sgw25295 		if (bytes > volsize - off)	/* don't write past the end */
13797013Sgw25295 			bytes = volsize - off;
13807013Sgw25295 
13813638Sbillm 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
13823638Sbillm 		error = dmu_tx_assign(tx, TXG_WAIT);
13833638Sbillm 		if (error) {
13843638Sbillm 			dmu_tx_abort(tx);
13853638Sbillm 			break;
13863638Sbillm 		}
13873638Sbillm 		error = dmu_write_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes, tx);
13883638Sbillm 		if (error == 0)
13899401SNeil.Perrin@Sun.COM 			zvol_log_write(zv, tx, off, bytes, sync);
13903638Sbillm 		dmu_tx_commit(tx);
13913638Sbillm 
13923638Sbillm 		if (error)
13933638Sbillm 			break;
13943638Sbillm 	}
13953755Sperrin 	zfs_range_unlock(rl);
13969401SNeil.Perrin@Sun.COM 	if (sync)
13978524SEric.Taylor@Sun.COM 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
13983638Sbillm 	return (error);
1399789Sahrens }
1400789Sahrens 
14017405SEric.Taylor@Sun.COM int
14027405SEric.Taylor@Sun.COM zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs)
14037405SEric.Taylor@Sun.COM {
14047405SEric.Taylor@Sun.COM 	struct uuid uuid = EFI_RESERVED;
14057405SEric.Taylor@Sun.COM 	efi_gpe_t gpe = { 0 };
14067405SEric.Taylor@Sun.COM 	uint32_t crc;
14077405SEric.Taylor@Sun.COM 	dk_efi_t efi;
14087405SEric.Taylor@Sun.COM 	int length;
14097405SEric.Taylor@Sun.COM 	char *ptr;
14107405SEric.Taylor@Sun.COM 
14117405SEric.Taylor@Sun.COM 	if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag))
14127405SEric.Taylor@Sun.COM 		return (EFAULT);
14137405SEric.Taylor@Sun.COM 	ptr = (char *)(uintptr_t)efi.dki_data_64;
14147405SEric.Taylor@Sun.COM 	length = efi.dki_length;
14157405SEric.Taylor@Sun.COM 	/*
14167405SEric.Taylor@Sun.COM 	 * Some clients may attempt to request a PMBR for the
14177405SEric.Taylor@Sun.COM 	 * zvol.  Currently this interface will return EINVAL to
14187405SEric.Taylor@Sun.COM 	 * such requests.  These requests could be supported by
14197405SEric.Taylor@Sun.COM 	 * adding a check for lba == 0 and consing up an appropriate
14207405SEric.Taylor@Sun.COM 	 * PMBR.
14217405SEric.Taylor@Sun.COM 	 */
14227405SEric.Taylor@Sun.COM 	if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0)
14237405SEric.Taylor@Sun.COM 		return (EINVAL);
14247405SEric.Taylor@Sun.COM 
14257405SEric.Taylor@Sun.COM 	gpe.efi_gpe_StartingLBA = LE_64(34ULL);
14267405SEric.Taylor@Sun.COM 	gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1);
14277405SEric.Taylor@Sun.COM 	UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
14287405SEric.Taylor@Sun.COM 
14297405SEric.Taylor@Sun.COM 	if (efi.dki_lba == 1) {
14307405SEric.Taylor@Sun.COM 		efi_gpt_t gpt = { 0 };
14317405SEric.Taylor@Sun.COM 
14327405SEric.Taylor@Sun.COM 		gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
14337405SEric.Taylor@Sun.COM 		gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
14347405SEric.Taylor@Sun.COM 		gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt));
14357405SEric.Taylor@Sun.COM 		gpt.efi_gpt_MyLBA = LE_64(1ULL);
14367405SEric.Taylor@Sun.COM 		gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL);
14377405SEric.Taylor@Sun.COM 		gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1);
14387405SEric.Taylor@Sun.COM 		gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL);
14397405SEric.Taylor@Sun.COM 		gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
14407405SEric.Taylor@Sun.COM 		gpt.efi_gpt_SizeOfPartitionEntry =
14417405SEric.Taylor@Sun.COM 		    LE_32(sizeof (efi_gpe_t));
14427405SEric.Taylor@Sun.COM 		CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
14437405SEric.Taylor@Sun.COM 		gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
14447405SEric.Taylor@Sun.COM 		CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table);
14457405SEric.Taylor@Sun.COM 		gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
14467405SEric.Taylor@Sun.COM 		if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length),
14477405SEric.Taylor@Sun.COM 		    flag))
14487405SEric.Taylor@Sun.COM 			return (EFAULT);
14497405SEric.Taylor@Sun.COM 		ptr += sizeof (gpt);
14507405SEric.Taylor@Sun.COM 		length -= sizeof (gpt);
14517405SEric.Taylor@Sun.COM 	}
14527405SEric.Taylor@Sun.COM 	if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe),
14537405SEric.Taylor@Sun.COM 	    length), flag))
14547405SEric.Taylor@Sun.COM 		return (EFAULT);
14557405SEric.Taylor@Sun.COM 	return (0);
14567405SEric.Taylor@Sun.COM }
14577405SEric.Taylor@Sun.COM 
1458789Sahrens /*
1459789Sahrens  * Dirtbag ioctls to support mkfs(1M) for UFS filesystems.  See dkio(7I).
1460789Sahrens  */
1461789Sahrens /*ARGSUSED*/
1462789Sahrens int
1463789Sahrens zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
1464789Sahrens {
1465789Sahrens 	zvol_state_t *zv;
14663897Smaybee 	struct dk_cinfo dki;
1467789Sahrens 	struct dk_minfo dkm;
14683897Smaybee 	struct dk_callback *dkc;
1469789Sahrens 	int error = 0;
14706423Sgw25295 	rl_t *rl;
1471789Sahrens 
1472789Sahrens 	mutex_enter(&zvol_state_lock);
1473789Sahrens 
1474789Sahrens 	zv = ddi_get_soft_state(zvol_state, getminor(dev));
1475789Sahrens 
1476789Sahrens 	if (zv == NULL) {
1477789Sahrens 		mutex_exit(&zvol_state_lock);
1478789Sahrens 		return (ENXIO);
1479789Sahrens 	}
14809303SEric.Taylor@Sun.COM 	ASSERT(zv->zv_total_opens > 0);
1481789Sahrens 
1482789Sahrens 	switch (cmd) {
1483789Sahrens 
1484789Sahrens 	case DKIOCINFO:
14853897Smaybee 		bzero(&dki, sizeof (dki));
14863897Smaybee 		(void) strcpy(dki.dki_cname, "zvol");
14873897Smaybee 		(void) strcpy(dki.dki_dname, "zvol");
14883897Smaybee 		dki.dki_ctype = DKC_UNKNOWN;
14893897Smaybee 		dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs);
1490789Sahrens 		mutex_exit(&zvol_state_lock);
14913897Smaybee 		if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag))
1492789Sahrens 			error = EFAULT;
1493789Sahrens 		return (error);
1494789Sahrens 
1495789Sahrens 	case DKIOCGMEDIAINFO:
1496789Sahrens 		bzero(&dkm, sizeof (dkm));
1497789Sahrens 		dkm.dki_lbsize = 1U << zv->zv_min_bs;
1498789Sahrens 		dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
1499789Sahrens 		dkm.dki_media_type = DK_UNKNOWN;
1500789Sahrens 		mutex_exit(&zvol_state_lock);
1501789Sahrens 		if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
1502789Sahrens 			error = EFAULT;
1503789Sahrens 		return (error);
1504789Sahrens 
1505789Sahrens 	case DKIOCGETEFI:
15067405SEric.Taylor@Sun.COM 		{
15077405SEric.Taylor@Sun.COM 			uint64_t vs = zv->zv_volsize;
15087405SEric.Taylor@Sun.COM 			uint8_t bs = zv->zv_min_bs;
15093016Smaybee 
15103016Smaybee 			mutex_exit(&zvol_state_lock);
15117405SEric.Taylor@Sun.COM 			error = zvol_getefi((void *)arg, flag, vs, bs);
15127405SEric.Taylor@Sun.COM 			return (error);
15133016Smaybee 		}
1514789Sahrens 
15153638Sbillm 	case DKIOCFLUSHWRITECACHE:
15163897Smaybee 		dkc = (struct dk_callback *)arg;
15179303SEric.Taylor@Sun.COM 		mutex_exit(&zvol_state_lock);
15183638Sbillm 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
15193897Smaybee 		if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) {
15203897Smaybee 			(*dkc->dkc_callback)(dkc->dkc_cookie, error);
15213897Smaybee 			error = 0;
15223897Smaybee 		}
15239303SEric.Taylor@Sun.COM 		return (error);
15249303SEric.Taylor@Sun.COM 
15259303SEric.Taylor@Sun.COM 	case DKIOCGETWCE:
15269303SEric.Taylor@Sun.COM 		{
15279303SEric.Taylor@Sun.COM 			int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0;
15289303SEric.Taylor@Sun.COM 			if (ddi_copyout(&wce, (void *)arg, sizeof (int),
15299303SEric.Taylor@Sun.COM 			    flag))
15309303SEric.Taylor@Sun.COM 				error = EFAULT;
15319303SEric.Taylor@Sun.COM 			break;
15329303SEric.Taylor@Sun.COM 		}
15339303SEric.Taylor@Sun.COM 	case DKIOCSETWCE:
15349303SEric.Taylor@Sun.COM 		{
15359303SEric.Taylor@Sun.COM 			int wce;
15369303SEric.Taylor@Sun.COM 			if (ddi_copyin((void *)arg, &wce, sizeof (int),
15379303SEric.Taylor@Sun.COM 			    flag)) {
15389303SEric.Taylor@Sun.COM 				error = EFAULT;
15399303SEric.Taylor@Sun.COM 				break;
15409303SEric.Taylor@Sun.COM 			}
15419303SEric.Taylor@Sun.COM 			if (wce) {
15429303SEric.Taylor@Sun.COM 				zv->zv_flags |= ZVOL_WCE;
15439303SEric.Taylor@Sun.COM 				mutex_exit(&zvol_state_lock);
15449303SEric.Taylor@Sun.COM 			} else {
15459303SEric.Taylor@Sun.COM 				zv->zv_flags &= ~ZVOL_WCE;
15469303SEric.Taylor@Sun.COM 				mutex_exit(&zvol_state_lock);
15479303SEric.Taylor@Sun.COM 				zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
15489303SEric.Taylor@Sun.COM 			}
15499303SEric.Taylor@Sun.COM 			return (0);
15509303SEric.Taylor@Sun.COM 		}
15513638Sbillm 
15523245Smaybee 	case DKIOCGGEOM:
15533245Smaybee 	case DKIOCGVTOC:
15546423Sgw25295 		/*
15556423Sgw25295 		 * commands using these (like prtvtoc) expect ENOTSUP
15566423Sgw25295 		 * since we're emulating an EFI label
15576423Sgw25295 		 */
15583245Smaybee 		error = ENOTSUP;
15593245Smaybee 		break;
15603245Smaybee 
15616423Sgw25295 	case DKIOCDUMPINIT:
15626423Sgw25295 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
15636423Sgw25295 		    RL_WRITER);
15646423Sgw25295 		error = zvol_dumpify(zv);
15656423Sgw25295 		zfs_range_unlock(rl);
15666423Sgw25295 		break;
15676423Sgw25295 
15686423Sgw25295 	case DKIOCDUMPFINI:
15699277SEric.Taylor@Sun.COM 		if (!(zv->zv_flags & ZVOL_DUMPIFIED))
15709277SEric.Taylor@Sun.COM 			break;
15716423Sgw25295 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
15726423Sgw25295 		    RL_WRITER);
15736423Sgw25295 		error = zvol_dump_fini(zv);
15746423Sgw25295 		zfs_range_unlock(rl);
15756423Sgw25295 		break;
15766423Sgw25295 
1577789Sahrens 	default:
15783016Smaybee 		error = ENOTTY;
1579789Sahrens 		break;
1580789Sahrens 
1581789Sahrens 	}
1582789Sahrens 	mutex_exit(&zvol_state_lock);
1583789Sahrens 	return (error);
1584789Sahrens }
1585789Sahrens 
1586789Sahrens int
1587789Sahrens zvol_busy(void)
1588789Sahrens {
1589789Sahrens 	return (zvol_minors != 0);
1590789Sahrens }
1591789Sahrens 
1592789Sahrens void
1593789Sahrens zvol_init(void)
1594789Sahrens {
1595789Sahrens 	VERIFY(ddi_soft_state_init(&zvol_state, sizeof (zvol_state_t), 1) == 0);
1596789Sahrens 	mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
1597789Sahrens }
1598789Sahrens 
1599789Sahrens void
1600789Sahrens zvol_fini(void)
1601789Sahrens {
1602789Sahrens 	mutex_destroy(&zvol_state_lock);
1603789Sahrens 	ddi_soft_state_fini(&zvol_state);
1604789Sahrens }
16056423Sgw25295 
16066423Sgw25295 static boolean_t
16076423Sgw25295 zvol_is_swap(zvol_state_t *zv)
16086423Sgw25295 {
16096423Sgw25295 	vnode_t *vp;
16106423Sgw25295 	boolean_t ret = B_FALSE;
16116423Sgw25295 	char *devpath;
16126423Sgw25295 	int error;
16136423Sgw25295 
161410272SMatthew.Ahrens@Sun.COM 	devpath = kmem_asprintf("%s%s", ZVOL_FULL_DEV_DIR, zv->zv_name);
16156423Sgw25295 	error = lookupname(devpath, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp);
161610272SMatthew.Ahrens@Sun.COM 	strfree(devpath);
16176423Sgw25295 
16186423Sgw25295 	ret = !error && IS_SWAPVP(common_specvp(vp));
16196423Sgw25295 
16206423Sgw25295 	if (vp != NULL)
16216423Sgw25295 		VN_RELE(vp);
16226423Sgw25295 
16236423Sgw25295 	return (ret);
16246423Sgw25295 }
16256423Sgw25295 
16266423Sgw25295 static int
16276423Sgw25295 zvol_dump_init(zvol_state_t *zv, boolean_t resize)
16286423Sgw25295 {
16296423Sgw25295 	dmu_tx_t *tx;
16306423Sgw25295 	int error = 0;
16316423Sgw25295 	objset_t *os = zv->zv_objset;
16326423Sgw25295 	nvlist_t *nv = NULL;
16336423Sgw25295 
16346423Sgw25295 	ASSERT(MUTEX_HELD(&zvol_state_lock));
16356423Sgw25295 
16366423Sgw25295 	tx = dmu_tx_create(os);
16376423Sgw25295 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
16386423Sgw25295 	error = dmu_tx_assign(tx, TXG_WAIT);
16396423Sgw25295 	if (error) {
16406423Sgw25295 		dmu_tx_abort(tx);
16416423Sgw25295 		return (error);
16426423Sgw25295 	}
16436423Sgw25295 
16446423Sgw25295 	/*
16456423Sgw25295 	 * If we are resizing the dump device then we only need to
16466423Sgw25295 	 * update the refreservation to match the newly updated
16476423Sgw25295 	 * zvolsize. Otherwise, we save off the original state of the
16486423Sgw25295 	 * zvol so that we can restore them if the zvol is ever undumpified.
16496423Sgw25295 	 */
16506423Sgw25295 	if (resize) {
16516423Sgw25295 		error = zap_update(os, ZVOL_ZAP_OBJ,
16526423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
16536423Sgw25295 		    &zv->zv_volsize, tx);
16546423Sgw25295 	} else {
16557837SMatthew.Ahrens@Sun.COM 		uint64_t checksum, compress, refresrv, vbs;
16567837SMatthew.Ahrens@Sun.COM 
16576423Sgw25295 		error = dsl_prop_get_integer(zv->zv_name,
16586423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL);
16596423Sgw25295 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
16606423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL);
16616423Sgw25295 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
16626423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL);
16637837SMatthew.Ahrens@Sun.COM 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
16647837SMatthew.Ahrens@Sun.COM 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL);
16656423Sgw25295 
16666423Sgw25295 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
16676423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1,
16686423Sgw25295 		    &compress, tx);
16696423Sgw25295 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
16706423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx);
16716423Sgw25295 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
16726423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
16736423Sgw25295 		    &refresrv, tx);
16747837SMatthew.Ahrens@Sun.COM 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
16757837SMatthew.Ahrens@Sun.COM 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1,
16767837SMatthew.Ahrens@Sun.COM 		    &vbs, tx);
16776423Sgw25295 	}
16786423Sgw25295 	dmu_tx_commit(tx);
16796423Sgw25295 
16806423Sgw25295 	/* Truncate the file */
16816423Sgw25295 	if (!error)
16826992Smaybee 		error = dmu_free_long_range(zv->zv_objset,
16836992Smaybee 		    ZVOL_OBJ, 0, DMU_OBJECT_END);
16846423Sgw25295 
16856423Sgw25295 	if (error)
16866423Sgw25295 		return (error);
16876423Sgw25295 
16886423Sgw25295 	/*
16896423Sgw25295 	 * We only need update the zvol's property if we are initializing
16906423Sgw25295 	 * the dump area for the first time.
16916423Sgw25295 	 */
16926423Sgw25295 	if (!resize) {
16936423Sgw25295 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
16946423Sgw25295 		VERIFY(nvlist_add_uint64(nv,
16956423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0);
16966423Sgw25295 		VERIFY(nvlist_add_uint64(nv,
16976423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
16986423Sgw25295 		    ZIO_COMPRESS_OFF) == 0);
16996423Sgw25295 		VERIFY(nvlist_add_uint64(nv,
17006423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
17016423Sgw25295 		    ZIO_CHECKSUM_OFF) == 0);
17027837SMatthew.Ahrens@Sun.COM 		VERIFY(nvlist_add_uint64(nv,
17037837SMatthew.Ahrens@Sun.COM 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
17047837SMatthew.Ahrens@Sun.COM 		    SPA_MAXBLOCKSIZE) == 0);
17056423Sgw25295 
17066423Sgw25295 		error = zfs_set_prop_nvlist(zv->zv_name, nv);
17076423Sgw25295 		nvlist_free(nv);
17086423Sgw25295 
17096423Sgw25295 		if (error)
17106423Sgw25295 			return (error);
17116423Sgw25295 	}
17126423Sgw25295 
17136423Sgw25295 	/* Allocate the space for the dump */
17146423Sgw25295 	error = zvol_prealloc(zv);
17156423Sgw25295 	return (error);
17166423Sgw25295 }
17176423Sgw25295 
17186423Sgw25295 static int
17196423Sgw25295 zvol_dumpify(zvol_state_t *zv)
17206423Sgw25295 {
17216423Sgw25295 	int error = 0;
17226423Sgw25295 	uint64_t dumpsize = 0;
17236423Sgw25295 	dmu_tx_t *tx;
17246423Sgw25295 	objset_t *os = zv->zv_objset;
17256423Sgw25295 
172610298SMatthew.Ahrens@Sun.COM 	if (zv->zv_flags & ZVOL_RDONLY || zv->zv_issnap)
17276423Sgw25295 		return (EROFS);
17286423Sgw25295 
17296423Sgw25295 	/*
17306423Sgw25295 	 * We do not support swap devices acting as dump devices.
17316423Sgw25295 	 */
17326423Sgw25295 	if (zvol_is_swap(zv))
17336423Sgw25295 		return (ENOTSUP);
17346423Sgw25295 
17356423Sgw25295 	if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE,
17366423Sgw25295 	    8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) {
17376423Sgw25295 		boolean_t resize = (dumpsize > 0) ? B_TRUE : B_FALSE;
17386423Sgw25295 
17396423Sgw25295 		if ((error = zvol_dump_init(zv, resize)) != 0) {
17406423Sgw25295 			(void) zvol_dump_fini(zv);
17416423Sgw25295 			return (error);
17426423Sgw25295 		}
17436423Sgw25295 	}
17446423Sgw25295 
17456423Sgw25295 	/*
17466423Sgw25295 	 * Build up our lba mapping.
17476423Sgw25295 	 */
17486423Sgw25295 	error = zvol_get_lbas(zv);
17496423Sgw25295 	if (error) {
17506423Sgw25295 		(void) zvol_dump_fini(zv);
17516423Sgw25295 		return (error);
17526423Sgw25295 	}
17536423Sgw25295 
17546423Sgw25295 	tx = dmu_tx_create(os);
17556423Sgw25295 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
17566423Sgw25295 	error = dmu_tx_assign(tx, TXG_WAIT);
17576423Sgw25295 	if (error) {
17586423Sgw25295 		dmu_tx_abort(tx);
17596423Sgw25295 		(void) zvol_dump_fini(zv);
17606423Sgw25295 		return (error);
17616423Sgw25295 	}
17626423Sgw25295 
17636423Sgw25295 	zv->zv_flags |= ZVOL_DUMPIFIED;
17646423Sgw25295 	error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1,
17656423Sgw25295 	    &zv->zv_volsize, tx);
17666423Sgw25295 	dmu_tx_commit(tx);
17676423Sgw25295 
17686423Sgw25295 	if (error) {
17696423Sgw25295 		(void) zvol_dump_fini(zv);
17706423Sgw25295 		return (error);
17716423Sgw25295 	}
17726423Sgw25295 
17736423Sgw25295 	txg_wait_synced(dmu_objset_pool(os), 0);
17746423Sgw25295 	return (0);
17756423Sgw25295 }
17766423Sgw25295 
17776423Sgw25295 static int
17786423Sgw25295 zvol_dump_fini(zvol_state_t *zv)
17796423Sgw25295 {
17806423Sgw25295 	dmu_tx_t *tx;
17816423Sgw25295 	objset_t *os = zv->zv_objset;
17826423Sgw25295 	nvlist_t *nv;
17836423Sgw25295 	int error = 0;
17847837SMatthew.Ahrens@Sun.COM 	uint64_t checksum, compress, refresrv, vbs;
17856423Sgw25295 
17867080Smaybee 	/*
17877080Smaybee 	 * Attempt to restore the zvol back to its pre-dumpified state.
17887080Smaybee 	 * This is a best-effort attempt as it's possible that not all
17897080Smaybee 	 * of these properties were initialized during the dumpify process
17907080Smaybee 	 * (i.e. error during zvol_dump_init).
17917080Smaybee 	 */
17927080Smaybee 
17936423Sgw25295 	tx = dmu_tx_create(os);
17946423Sgw25295 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
17956423Sgw25295 	error = dmu_tx_assign(tx, TXG_WAIT);
17966423Sgw25295 	if (error) {
17976423Sgw25295 		dmu_tx_abort(tx);
17986423Sgw25295 		return (error);
17996423Sgw25295 	}
18007080Smaybee 	(void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx);
18017080Smaybee 	dmu_tx_commit(tx);
18026423Sgw25295 
18036423Sgw25295 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
18046423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum);
18056423Sgw25295 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
18066423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress);
18076423Sgw25295 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
18086423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv);
18097837SMatthew.Ahrens@Sun.COM 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
18107837SMatthew.Ahrens@Sun.COM 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs);
18116423Sgw25295 
18126423Sgw25295 	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
18136423Sgw25295 	(void) nvlist_add_uint64(nv,
18146423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum);
18156423Sgw25295 	(void) nvlist_add_uint64(nv,
18166423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress);
18176423Sgw25295 	(void) nvlist_add_uint64(nv,
18186423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv);
18197837SMatthew.Ahrens@Sun.COM 	(void) nvlist_add_uint64(nv,
18207837SMatthew.Ahrens@Sun.COM 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), vbs);
18216423Sgw25295 	(void) zfs_set_prop_nvlist(zv->zv_name, nv);
18226423Sgw25295 	nvlist_free(nv);
18236423Sgw25295 
18247080Smaybee 	zvol_free_extents(zv);
18257080Smaybee 	zv->zv_flags &= ~ZVOL_DUMPIFIED;
18267080Smaybee 	(void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END);
18277080Smaybee 
18286423Sgw25295 	return (0);
18296423Sgw25295 }
1830