xref: /onnv-gate/usr/src/uts/common/fs/zfs/zvol.c (revision 11619:d3fb7ae6fe56)
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 /*
2211422SMark.Musante@Sun.COM  * Copyright 2010 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  *
3510588SEric.Taylor@Sun.COM  * These links are created by the /dev filesystem (sdev_zvolops.c).
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 */
113789Sahrens 	uint32_t	zv_open_count[OTYPCNT];	/* open counts */
114789Sahrens 	uint32_t	zv_total_opens;	/* total open count */
1151141Sperrin 	zilog_t		*zv_zilog;	/* ZIL handle */
1167837SMatthew.Ahrens@Sun.COM 	list_t		zv_extents;	/* List of extents for dump */
1173755Sperrin 	znode_t		zv_znode;	/* for range locking */
118789Sahrens } zvol_state_t;
119789Sahrens 
1203063Sperrin /*
1216423Sgw25295  * zvol specific flags
1226423Sgw25295  */
1236423Sgw25295 #define	ZVOL_RDONLY	0x1
1246423Sgw25295 #define	ZVOL_DUMPIFIED	0x2
1257405SEric.Taylor@Sun.COM #define	ZVOL_EXCL	0x4
1269303SEric.Taylor@Sun.COM #define	ZVOL_WCE	0x8
1276423Sgw25295 
1286423Sgw25295 /*
1293063Sperrin  * zvol maximum transfer in one DMU tx.
1303063Sperrin  */
1313063Sperrin int zvol_maxphys = DMU_MAX_ACCESS/2;
1323063Sperrin 
13311022STom.Erickson@Sun.COM extern int zfs_set_prop_nvlist(const char *, zprop_source_t,
13411022STom.Erickson@Sun.COM     nvlist_t *, nvlist_t **);
13510588SEric.Taylor@Sun.COM static int zvol_remove_zv(zvol_state_t *);
1363638Sbillm static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio);
1376423Sgw25295 static int zvol_dumpify(zvol_state_t *zv);
1386423Sgw25295 static int zvol_dump_fini(zvol_state_t *zv);
1396423Sgw25295 static int zvol_dump_init(zvol_state_t *zv, boolean_t resize);
1403063Sperrin 
141789Sahrens static void
14210588SEric.Taylor@Sun.COM zvol_size_changed(uint64_t volsize, major_t maj, minor_t min)
143789Sahrens {
14410588SEric.Taylor@Sun.COM 	dev_t dev = makedevice(maj, min);
145789Sahrens 
146789Sahrens 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
14710588SEric.Taylor@Sun.COM 	    "Size", volsize) == DDI_SUCCESS);
148789Sahrens 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
14910588SEric.Taylor@Sun.COM 	    "Nblocks", lbtodb(volsize)) == DDI_SUCCESS);
1506423Sgw25295 
1516423Sgw25295 	/* Notify specfs to invalidate the cached size */
1526423Sgw25295 	spec_size_invalidate(dev, VBLK);
1536423Sgw25295 	spec_size_invalidate(dev, VCHR);
154789Sahrens }
155789Sahrens 
156789Sahrens int
1572676Seschrock zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
158789Sahrens {
1592676Seschrock 	if (volsize == 0)
160789Sahrens 		return (EINVAL);
161789Sahrens 
1622676Seschrock 	if (volsize % blocksize != 0)
1631133Seschrock 		return (EINVAL);
1641133Seschrock 
165789Sahrens #ifdef _ILP32
1662676Seschrock 	if (volsize - 1 > SPEC_MAXOFFSET_T)
167789Sahrens 		return (EOVERFLOW);
168789Sahrens #endif
169789Sahrens 	return (0);
170789Sahrens }
171789Sahrens 
172789Sahrens int
1732676Seschrock zvol_check_volblocksize(uint64_t volblocksize)
174789Sahrens {
1752676Seschrock 	if (volblocksize < SPA_MINBLOCKSIZE ||
1762676Seschrock 	    volblocksize > SPA_MAXBLOCKSIZE ||
1772676Seschrock 	    !ISP2(volblocksize))
178789Sahrens 		return (EDOM);
179789Sahrens 
180789Sahrens 	return (0);
181789Sahrens }
182789Sahrens 
183789Sahrens int
1842885Sahrens zvol_get_stats(objset_t *os, nvlist_t *nv)
185789Sahrens {
186789Sahrens 	int error;
187789Sahrens 	dmu_object_info_t doi;
1882885Sahrens 	uint64_t val;
189789Sahrens 
1902885Sahrens 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
191789Sahrens 	if (error)
192789Sahrens 		return (error);
193789Sahrens 
1942885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
1952885Sahrens 
196789Sahrens 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
197789Sahrens 
1982885Sahrens 	if (error == 0) {
1992885Sahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
2002885Sahrens 		    doi.doi_data_block_size);
2012885Sahrens 	}
202789Sahrens 
203789Sahrens 	return (error);
204789Sahrens }
205789Sahrens 
206789Sahrens /*
207789Sahrens  * Find a free minor number.
208789Sahrens  */
209789Sahrens static minor_t
210789Sahrens zvol_minor_alloc(void)
211789Sahrens {
212789Sahrens 	minor_t minor;
213789Sahrens 
214789Sahrens 	ASSERT(MUTEX_HELD(&zvol_state_lock));
215789Sahrens 
216789Sahrens 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++)
217789Sahrens 		if (ddi_get_soft_state(zvol_state, minor) == NULL)
218789Sahrens 			return (minor);
219789Sahrens 
220789Sahrens 	return (0);
221789Sahrens }
222789Sahrens 
223789Sahrens static zvol_state_t *
2242676Seschrock zvol_minor_lookup(const char *name)
225789Sahrens {
226789Sahrens 	minor_t minor;
227789Sahrens 	zvol_state_t *zv;
228789Sahrens 
229789Sahrens 	ASSERT(MUTEX_HELD(&zvol_state_lock));
230789Sahrens 
231789Sahrens 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) {
232789Sahrens 		zv = ddi_get_soft_state(zvol_state, minor);
233789Sahrens 		if (zv == NULL)
234789Sahrens 			continue;
235789Sahrens 		if (strcmp(zv->zv_name, name) == 0)
236789Sahrens 			break;
237789Sahrens 	}
238789Sahrens 
239789Sahrens 	return (zv);
240789Sahrens }
241789Sahrens 
2426423Sgw25295 /* extent mapping arg */
2436423Sgw25295 struct maparg {
2447837SMatthew.Ahrens@Sun.COM 	zvol_state_t	*ma_zv;
2457837SMatthew.Ahrens@Sun.COM 	uint64_t	ma_blks;
2466423Sgw25295 };
2476423Sgw25295 
2486423Sgw25295 /*ARGSUSED*/
2496423Sgw25295 static int
25010922SJeff.Bonwick@Sun.COM zvol_map_block(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
25110922SJeff.Bonwick@Sun.COM     const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
2526423Sgw25295 {
2537837SMatthew.Ahrens@Sun.COM 	struct maparg *ma = arg;
2547837SMatthew.Ahrens@Sun.COM 	zvol_extent_t *ze;
2557837SMatthew.Ahrens@Sun.COM 	int bs = ma->ma_zv->zv_volblocksize;
2566423Sgw25295 
2577837SMatthew.Ahrens@Sun.COM 	if (bp == NULL || zb->zb_object != ZVOL_OBJ || zb->zb_level != 0)
2586423Sgw25295 		return (0);
2596423Sgw25295 
2607837SMatthew.Ahrens@Sun.COM 	VERIFY3U(ma->ma_blks, ==, zb->zb_blkid);
2617837SMatthew.Ahrens@Sun.COM 	ma->ma_blks++;
2627837SMatthew.Ahrens@Sun.COM 
2636423Sgw25295 	/* Abort immediately if we have encountered gang blocks */
2647837SMatthew.Ahrens@Sun.COM 	if (BP_IS_GANG(bp))
2657837SMatthew.Ahrens@Sun.COM 		return (EFRAGS);
2666423Sgw25295 
2677837SMatthew.Ahrens@Sun.COM 	/*
2687837SMatthew.Ahrens@Sun.COM 	 * See if the block is at the end of the previous extent.
2697837SMatthew.Ahrens@Sun.COM 	 */
2707837SMatthew.Ahrens@Sun.COM 	ze = list_tail(&ma->ma_zv->zv_extents);
2717837SMatthew.Ahrens@Sun.COM 	if (ze &&
2727837SMatthew.Ahrens@Sun.COM 	    DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) &&
2737837SMatthew.Ahrens@Sun.COM 	    DVA_GET_OFFSET(BP_IDENTITY(bp)) ==
2747837SMatthew.Ahrens@Sun.COM 	    DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) {
2757837SMatthew.Ahrens@Sun.COM 		ze->ze_nblks++;
2766423Sgw25295 		return (0);
2776423Sgw25295 	}
2786423Sgw25295 
2797837SMatthew.Ahrens@Sun.COM 	dprintf_bp(bp, "%s", "next blkptr:");
2807837SMatthew.Ahrens@Sun.COM 
2817837SMatthew.Ahrens@Sun.COM 	/* start a new extent */
2827837SMatthew.Ahrens@Sun.COM 	ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP);
2837837SMatthew.Ahrens@Sun.COM 	ze->ze_dva = bp->blk_dva[0];	/* structure assignment */
2847837SMatthew.Ahrens@Sun.COM 	ze->ze_nblks = 1;
2857837SMatthew.Ahrens@Sun.COM 	list_insert_tail(&ma->ma_zv->zv_extents, ze);
2867837SMatthew.Ahrens@Sun.COM 	return (0);
2877837SMatthew.Ahrens@Sun.COM }
2887837SMatthew.Ahrens@Sun.COM 
2897837SMatthew.Ahrens@Sun.COM static void
2907837SMatthew.Ahrens@Sun.COM zvol_free_extents(zvol_state_t *zv)
2917837SMatthew.Ahrens@Sun.COM {
2927837SMatthew.Ahrens@Sun.COM 	zvol_extent_t *ze;
2937837SMatthew.Ahrens@Sun.COM 
2947837SMatthew.Ahrens@Sun.COM 	while (ze = list_head(&zv->zv_extents)) {
2957837SMatthew.Ahrens@Sun.COM 		list_remove(&zv->zv_extents, ze);
2967837SMatthew.Ahrens@Sun.COM 		kmem_free(ze, sizeof (zvol_extent_t));
2977837SMatthew.Ahrens@Sun.COM 	}
2987837SMatthew.Ahrens@Sun.COM }
2997837SMatthew.Ahrens@Sun.COM 
3007837SMatthew.Ahrens@Sun.COM static int
3017837SMatthew.Ahrens@Sun.COM zvol_get_lbas(zvol_state_t *zv)
3027837SMatthew.Ahrens@Sun.COM {
3037837SMatthew.Ahrens@Sun.COM 	struct maparg	ma;
3047837SMatthew.Ahrens@Sun.COM 	int		err;
3057837SMatthew.Ahrens@Sun.COM 
3067837SMatthew.Ahrens@Sun.COM 	ma.ma_zv = zv;
3077837SMatthew.Ahrens@Sun.COM 	ma.ma_blks = 0;
3087837SMatthew.Ahrens@Sun.COM 	zvol_free_extents(zv);
3097837SMatthew.Ahrens@Sun.COM 
3107837SMatthew.Ahrens@Sun.COM 	err = traverse_dataset(dmu_objset_ds(zv->zv_objset), 0,
3117837SMatthew.Ahrens@Sun.COM 	    TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma);
3127837SMatthew.Ahrens@Sun.COM 	if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) {
3137837SMatthew.Ahrens@Sun.COM 		zvol_free_extents(zv);
3147837SMatthew.Ahrens@Sun.COM 		return (err ? err : EIO);
3156423Sgw25295 	}
3166423Sgw25295 
3176423Sgw25295 	return (0);
3186423Sgw25295 }
3196423Sgw25295 
3204543Smarks /* ARGSUSED */
321789Sahrens void
3224543Smarks zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
323789Sahrens {
3245331Samw 	zfs_creat_t *zct = arg;
3255331Samw 	nvlist_t *nvprops = zct->zct_props;
326789Sahrens 	int error;
3272676Seschrock 	uint64_t volblocksize, volsize;
328789Sahrens 
3294543Smarks 	VERIFY(nvlist_lookup_uint64(nvprops,
3302676Seschrock 	    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
3314543Smarks 	if (nvlist_lookup_uint64(nvprops,
3322676Seschrock 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
3332676Seschrock 		volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
3342676Seschrock 
3352676Seschrock 	/*
3366423Sgw25295 	 * These properties must be removed from the list so the generic
3372676Seschrock 	 * property setting step won't apply to them.
3382676Seschrock 	 */
3394543Smarks 	VERIFY(nvlist_remove_all(nvprops,
3402676Seschrock 	    zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
3414543Smarks 	(void) nvlist_remove_all(nvprops,
3422676Seschrock 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
3432676Seschrock 
3442676Seschrock 	error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
345789Sahrens 	    DMU_OT_NONE, 0, tx);
346789Sahrens 	ASSERT(error == 0);
347789Sahrens 
348789Sahrens 	error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
349789Sahrens 	    DMU_OT_NONE, 0, tx);
350789Sahrens 	ASSERT(error == 0);
351789Sahrens 
3522676Seschrock 	error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
353789Sahrens 	ASSERT(error == 0);
354789Sahrens }
355789Sahrens 
356789Sahrens /*
3571141Sperrin  * Replay a TX_WRITE ZIL transaction that didn't get committed
3581141Sperrin  * after a system failure
3591141Sperrin  */
3601141Sperrin static int
3611141Sperrin zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
3621141Sperrin {
3631141Sperrin 	objset_t *os = zv->zv_objset;
3641141Sperrin 	char *data = (char *)(lr + 1);	/* data follows lr_write_t */
36510922SJeff.Bonwick@Sun.COM 	uint64_t offset, length;
3661141Sperrin 	dmu_tx_t *tx;
3671141Sperrin 	int error;
3681141Sperrin 
3691141Sperrin 	if (byteswap)
3701141Sperrin 		byteswap_uint64_array(lr, sizeof (*lr));
3711141Sperrin 
37210922SJeff.Bonwick@Sun.COM 	offset = lr->lr_offset;
37310922SJeff.Bonwick@Sun.COM 	length = lr->lr_length;
37410922SJeff.Bonwick@Sun.COM 
37510922SJeff.Bonwick@Sun.COM 	/* If it's a dmu_sync() block, write the whole block */
37610922SJeff.Bonwick@Sun.COM 	if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
37710922SJeff.Bonwick@Sun.COM 		uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
37810922SJeff.Bonwick@Sun.COM 		if (length < blocksize) {
37910922SJeff.Bonwick@Sun.COM 			offset -= offset % blocksize;
38010922SJeff.Bonwick@Sun.COM 			length = blocksize;
38110922SJeff.Bonwick@Sun.COM 		}
38210922SJeff.Bonwick@Sun.COM 	}
38310800SNeil.Perrin@Sun.COM 
3841141Sperrin 	tx = dmu_tx_create(os);
38510922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length);
3868227SNeil.Perrin@Sun.COM 	error = dmu_tx_assign(tx, TXG_WAIT);
3871141Sperrin 	if (error) {
3881141Sperrin 		dmu_tx_abort(tx);
3891141Sperrin 	} else {
39010922SJeff.Bonwick@Sun.COM 		dmu_write(os, ZVOL_OBJ, offset, length, 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 */
42210800SNeil.Perrin@Sun.COM 	zvol_replay_err,	/* TX_CREATE_ACL */
42310800SNeil.Perrin@Sun.COM 	zvol_replay_err,	/* TX_CREATE_ATTR */
42410800SNeil.Perrin@Sun.COM 	zvol_replay_err,	/* TX_CREATE_ACL_ATTR */
42510800SNeil.Perrin@Sun.COM 	zvol_replay_err,	/* TX_MKDIR_ACL */
42610800SNeil.Perrin@Sun.COM 	zvol_replay_err,	/* TX_MKDIR_ATTR */
42710800SNeil.Perrin@Sun.COM 	zvol_replay_err,	/* TX_MKDIR_ACL_ATTR */
42810800SNeil.Perrin@Sun.COM 	zvol_replay_err,	/* TX_WRITE2 */
4291141Sperrin };
4301141Sperrin 
43110588SEric.Taylor@Sun.COM int
43210588SEric.Taylor@Sun.COM zvol_name2minor(const char *name, minor_t *minor)
43310588SEric.Taylor@Sun.COM {
43410588SEric.Taylor@Sun.COM 	zvol_state_t *zv;
43510588SEric.Taylor@Sun.COM 
43610588SEric.Taylor@Sun.COM 	mutex_enter(&zvol_state_lock);
43710588SEric.Taylor@Sun.COM 	zv = zvol_minor_lookup(name);
43810588SEric.Taylor@Sun.COM 	if (minor && zv)
43910588SEric.Taylor@Sun.COM 		*minor = zv->zv_minor;
44010588SEric.Taylor@Sun.COM 	mutex_exit(&zvol_state_lock);
44110588SEric.Taylor@Sun.COM 	return (zv ? 0 : -1);
44210588SEric.Taylor@Sun.COM }
44310588SEric.Taylor@Sun.COM 
4441141Sperrin /*
4456423Sgw25295  * Create a minor node (plus a whole lot more) for the specified volume.
446789Sahrens  */
447789Sahrens int
44810588SEric.Taylor@Sun.COM zvol_create_minor(const char *name)
449789Sahrens {
450789Sahrens 	zvol_state_t *zv;
451789Sahrens 	objset_t *os;
4523063Sperrin 	dmu_object_info_t doi;
453789Sahrens 	minor_t minor = 0;
454789Sahrens 	char chrbuf[30], blkbuf[30];
455789Sahrens 	int error;
456789Sahrens 
457789Sahrens 	mutex_enter(&zvol_state_lock);
458789Sahrens 
45911422SMark.Musante@Sun.COM 	if (zvol_minor_lookup(name) != NULL) {
460789Sahrens 		mutex_exit(&zvol_state_lock);
461789Sahrens 		return (EEXIST);
462789Sahrens 	}
463789Sahrens 
46410298SMatthew.Ahrens@Sun.COM 	/* lie and say we're read-only */
46510298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, zvol_tag, &os);
466789Sahrens 
467789Sahrens 	if (error) {
468789Sahrens 		mutex_exit(&zvol_state_lock);
469789Sahrens 		return (error);
470789Sahrens 	}
471789Sahrens 
47210588SEric.Taylor@Sun.COM 	if ((minor = zvol_minor_alloc()) == 0) {
47310298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(os, zvol_tag);
474789Sahrens 		mutex_exit(&zvol_state_lock);
475789Sahrens 		return (ENXIO);
476789Sahrens 	}
477789Sahrens 
478789Sahrens 	if (ddi_soft_state_zalloc(zvol_state, minor) != DDI_SUCCESS) {
47910298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(os, zvol_tag);
480789Sahrens 		mutex_exit(&zvol_state_lock);
481789Sahrens 		return (EAGAIN);
482789Sahrens 	}
4832676Seschrock 	(void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME,
4842676Seschrock 	    (char *)name);
485789Sahrens 
48610588SEric.Taylor@Sun.COM 	(void) snprintf(chrbuf, sizeof (chrbuf), "%u,raw", minor);
487789Sahrens 
488789Sahrens 	if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR,
489789Sahrens 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
490789Sahrens 		ddi_soft_state_free(zvol_state, minor);
49110298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(os, zvol_tag);
492789Sahrens 		mutex_exit(&zvol_state_lock);
493789Sahrens 		return (EAGAIN);
494789Sahrens 	}
495789Sahrens 
49610588SEric.Taylor@Sun.COM 	(void) snprintf(blkbuf, sizeof (blkbuf), "%u", minor);
497789Sahrens 
498789Sahrens 	if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK,
499789Sahrens 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
500789Sahrens 		ddi_remove_minor_node(zfs_dip, chrbuf);
501789Sahrens 		ddi_soft_state_free(zvol_state, minor);
50210298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(os, zvol_tag);
503789Sahrens 		mutex_exit(&zvol_state_lock);
504789Sahrens 		return (EAGAIN);
505789Sahrens 	}
506789Sahrens 
507789Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
508789Sahrens 
50910588SEric.Taylor@Sun.COM 	(void) strlcpy(zv->zv_name, name, MAXPATHLEN);
510789Sahrens 	zv->zv_min_bs = DEV_BSHIFT;
511789Sahrens 	zv->zv_minor = minor;
512789Sahrens 	zv->zv_objset = os;
51310588SEric.Taylor@Sun.COM 	if (dmu_objset_is_snapshot(os))
51410588SEric.Taylor@Sun.COM 		zv->zv_flags |= ZVOL_RDONLY;
5153755Sperrin 	mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
5163755Sperrin 	avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
5173755Sperrin 	    sizeof (rl_t), offsetof(rl_t, r_node));
5187837SMatthew.Ahrens@Sun.COM 	list_create(&zv->zv_extents, sizeof (zvol_extent_t),
5197837SMatthew.Ahrens@Sun.COM 	    offsetof(zvol_extent_t, ze_node));
5203063Sperrin 	/* get and cache the blocksize */
5213063Sperrin 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
5223063Sperrin 	ASSERT(error == 0);
5233063Sperrin 	zv->zv_volblocksize = doi.doi_data_block_size;
5241141Sperrin 
5258227SNeil.Perrin@Sun.COM 	zil_replay(os, zv, zvol_replay_vector);
52610588SEric.Taylor@Sun.COM 	dmu_objset_disown(os, zvol_tag);
52710588SEric.Taylor@Sun.COM 	zv->zv_objset = NULL;
528789Sahrens 
529789Sahrens 	zvol_minors++;
530789Sahrens 
531789Sahrens 	mutex_exit(&zvol_state_lock);
532789Sahrens 
533789Sahrens 	return (0);
534789Sahrens }
535789Sahrens 
536789Sahrens /*
537789Sahrens  * Remove minor node for the specified volume.
538789Sahrens  */
53910588SEric.Taylor@Sun.COM static int
54010588SEric.Taylor@Sun.COM zvol_remove_zv(zvol_state_t *zv)
541789Sahrens {
54210588SEric.Taylor@Sun.COM 	char nmbuf[20];
543789Sahrens 
54410588SEric.Taylor@Sun.COM 	ASSERT(MUTEX_HELD(&zvol_state_lock));
54510588SEric.Taylor@Sun.COM 	if (zv->zv_total_opens != 0)
546789Sahrens 		return (EBUSY);
547789Sahrens 
54810588SEric.Taylor@Sun.COM 	(void) snprintf(nmbuf, sizeof (nmbuf), "%u,raw", zv->zv_minor);
54910588SEric.Taylor@Sun.COM 	ddi_remove_minor_node(zfs_dip, nmbuf);
550789Sahrens 
55110588SEric.Taylor@Sun.COM 	(void) snprintf(nmbuf, sizeof (nmbuf), "%u", zv->zv_minor);
55210588SEric.Taylor@Sun.COM 	ddi_remove_minor_node(zfs_dip, nmbuf);
553789Sahrens 
5543755Sperrin 	avl_destroy(&zv->zv_znode.z_range_avl);
5553755Sperrin 	mutex_destroy(&zv->zv_znode.z_range_lock);
556789Sahrens 
557789Sahrens 	ddi_soft_state_free(zvol_state, zv->zv_minor);
558789Sahrens 
559789Sahrens 	zvol_minors--;
56010588SEric.Taylor@Sun.COM 	return (0);
56110588SEric.Taylor@Sun.COM }
562789Sahrens 
56310588SEric.Taylor@Sun.COM int
56410588SEric.Taylor@Sun.COM zvol_remove_minor(const char *name)
56510588SEric.Taylor@Sun.COM {
56610588SEric.Taylor@Sun.COM 	zvol_state_t *zv;
56710588SEric.Taylor@Sun.COM 	int rc;
56810588SEric.Taylor@Sun.COM 
56910588SEric.Taylor@Sun.COM 	mutex_enter(&zvol_state_lock);
57010588SEric.Taylor@Sun.COM 	if ((zv = zvol_minor_lookup(name)) == NULL) {
57110588SEric.Taylor@Sun.COM 		mutex_exit(&zvol_state_lock);
57210588SEric.Taylor@Sun.COM 		return (ENXIO);
57310588SEric.Taylor@Sun.COM 	}
57410588SEric.Taylor@Sun.COM 	rc = zvol_remove_zv(zv);
575789Sahrens 	mutex_exit(&zvol_state_lock);
57610588SEric.Taylor@Sun.COM 	return (rc);
57710588SEric.Taylor@Sun.COM }
578789Sahrens 
57910588SEric.Taylor@Sun.COM int
58010588SEric.Taylor@Sun.COM zvol_first_open(zvol_state_t *zv)
58110588SEric.Taylor@Sun.COM {
58210588SEric.Taylor@Sun.COM 	objset_t *os;
58310588SEric.Taylor@Sun.COM 	uint64_t volsize;
58410588SEric.Taylor@Sun.COM 	int error;
58510588SEric.Taylor@Sun.COM 	uint64_t readonly;
58610588SEric.Taylor@Sun.COM 
58710588SEric.Taylor@Sun.COM 	/* lie and say we're read-only */
58810588SEric.Taylor@Sun.COM 	error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, B_TRUE,
58910588SEric.Taylor@Sun.COM 	    zvol_tag, &os);
59010588SEric.Taylor@Sun.COM 	if (error)
59110588SEric.Taylor@Sun.COM 		return (error);
59210588SEric.Taylor@Sun.COM 
59310588SEric.Taylor@Sun.COM 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
59410588SEric.Taylor@Sun.COM 	if (error) {
59510588SEric.Taylor@Sun.COM 		ASSERT(error == 0);
59610588SEric.Taylor@Sun.COM 		dmu_objset_disown(os, zvol_tag);
59710588SEric.Taylor@Sun.COM 		return (error);
59810588SEric.Taylor@Sun.COM 	}
59910588SEric.Taylor@Sun.COM 	zv->zv_objset = os;
60010588SEric.Taylor@Sun.COM 	zv->zv_volsize = volsize;
60110588SEric.Taylor@Sun.COM 	zv->zv_zilog = zil_open(os, zvol_get_data);
60210588SEric.Taylor@Sun.COM 	zvol_size_changed(zv->zv_volsize, ddi_driver_major(zfs_dip),
60310588SEric.Taylor@Sun.COM 	    zv->zv_minor);
60410588SEric.Taylor@Sun.COM 
60510588SEric.Taylor@Sun.COM 	VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &readonly,
60610588SEric.Taylor@Sun.COM 	    NULL) == 0);
60710588SEric.Taylor@Sun.COM 	if (readonly || dmu_objset_is_snapshot(os))
60810588SEric.Taylor@Sun.COM 		zv->zv_flags |= ZVOL_RDONLY;
60910588SEric.Taylor@Sun.COM 	else
61010588SEric.Taylor@Sun.COM 		zv->zv_flags &= ~ZVOL_RDONLY;
61110588SEric.Taylor@Sun.COM 	return (error);
61210588SEric.Taylor@Sun.COM }
61310588SEric.Taylor@Sun.COM 
61410588SEric.Taylor@Sun.COM void
61510588SEric.Taylor@Sun.COM zvol_last_close(zvol_state_t *zv)
61610588SEric.Taylor@Sun.COM {
61710588SEric.Taylor@Sun.COM 	zil_close(zv->zv_zilog);
61810588SEric.Taylor@Sun.COM 	zv->zv_zilog = NULL;
61910588SEric.Taylor@Sun.COM 	dmu_objset_disown(zv->zv_objset, zvol_tag);
62010588SEric.Taylor@Sun.COM 	zv->zv_objset = NULL;
621789Sahrens }
622789Sahrens 
6236423Sgw25295 int
6246423Sgw25295 zvol_prealloc(zvol_state_t *zv)
6256423Sgw25295 {
6266423Sgw25295 	objset_t *os = zv->zv_objset;
6276423Sgw25295 	dmu_tx_t *tx;
6286423Sgw25295 	uint64_t refd, avail, usedobjs, availobjs;
6296423Sgw25295 	uint64_t resid = zv->zv_volsize;
6306423Sgw25295 	uint64_t off = 0;
6316423Sgw25295 
6326423Sgw25295 	/* Check the space usage before attempting to allocate the space */
6336423Sgw25295 	dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs);
6346423Sgw25295 	if (avail < zv->zv_volsize)
6356423Sgw25295 		return (ENOSPC);
6366423Sgw25295 
6376423Sgw25295 	/* Free old extents if they exist */
6386423Sgw25295 	zvol_free_extents(zv);
6396423Sgw25295 
6406423Sgw25295 	while (resid != 0) {
6416423Sgw25295 		int error;
6426423Sgw25295 		uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE);
6436423Sgw25295 
6446423Sgw25295 		tx = dmu_tx_create(os);
6456423Sgw25295 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
6466423Sgw25295 		error = dmu_tx_assign(tx, TXG_WAIT);
6476423Sgw25295 		if (error) {
6486423Sgw25295 			dmu_tx_abort(tx);
6496992Smaybee 			(void) dmu_free_long_range(os, ZVOL_OBJ, 0, off);
6506423Sgw25295 			return (error);
6516423Sgw25295 		}
6527872STim.Haley@Sun.COM 		dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx);
6536423Sgw25295 		dmu_tx_commit(tx);
6546423Sgw25295 		off += bytes;
6556423Sgw25295 		resid -= bytes;
6566423Sgw25295 	}
6576423Sgw25295 	txg_wait_synced(dmu_objset_pool(os), 0);
6586423Sgw25295 
6596423Sgw25295 	return (0);
6606423Sgw25295 }
6616423Sgw25295 
6626423Sgw25295 int
66310588SEric.Taylor@Sun.COM zvol_update_volsize(objset_t *os, uint64_t volsize)
6646423Sgw25295 {
6656423Sgw25295 	dmu_tx_t *tx;
6666423Sgw25295 	int error;
6676423Sgw25295 
6686423Sgw25295 	ASSERT(MUTEX_HELD(&zvol_state_lock));
6696423Sgw25295 
67010588SEric.Taylor@Sun.COM 	tx = dmu_tx_create(os);
6716423Sgw25295 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
6726423Sgw25295 	error = dmu_tx_assign(tx, TXG_WAIT);
6736423Sgw25295 	if (error) {
6746423Sgw25295 		dmu_tx_abort(tx);
6756423Sgw25295 		return (error);
6766423Sgw25295 	}
6776423Sgw25295 
67810588SEric.Taylor@Sun.COM 	error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
6796423Sgw25295 	    &volsize, tx);
6806423Sgw25295 	dmu_tx_commit(tx);
6816423Sgw25295 
6826423Sgw25295 	if (error == 0)
68310588SEric.Taylor@Sun.COM 		error = dmu_free_long_range(os,
6846992Smaybee 		    ZVOL_OBJ, volsize, DMU_OBJECT_END);
68510588SEric.Taylor@Sun.COM 	return (error);
68610588SEric.Taylor@Sun.COM }
68710588SEric.Taylor@Sun.COM 
68810588SEric.Taylor@Sun.COM void
68910588SEric.Taylor@Sun.COM zvol_remove_minors(const char *name)
69010588SEric.Taylor@Sun.COM {
69110588SEric.Taylor@Sun.COM 	zvol_state_t *zv;
69210588SEric.Taylor@Sun.COM 	char *namebuf;
69310588SEric.Taylor@Sun.COM 	minor_t minor;
6946423Sgw25295 
69510588SEric.Taylor@Sun.COM 	namebuf = kmem_zalloc(strlen(name) + 2, KM_SLEEP);
69610588SEric.Taylor@Sun.COM 	(void) strncpy(namebuf, name, strlen(name));
69710588SEric.Taylor@Sun.COM 	(void) strcat(namebuf, "/");
69810588SEric.Taylor@Sun.COM 	mutex_enter(&zvol_state_lock);
69910588SEric.Taylor@Sun.COM 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) {
70010588SEric.Taylor@Sun.COM 
70110588SEric.Taylor@Sun.COM 		zv = ddi_get_soft_state(zvol_state, minor);
70210588SEric.Taylor@Sun.COM 		if (zv == NULL)
70310588SEric.Taylor@Sun.COM 			continue;
70410588SEric.Taylor@Sun.COM 		if (strncmp(namebuf, zv->zv_name, strlen(namebuf)) == 0)
70510588SEric.Taylor@Sun.COM 			(void) zvol_remove_zv(zv);
7066423Sgw25295 	}
70710588SEric.Taylor@Sun.COM 	kmem_free(namebuf, strlen(name) + 2);
70810588SEric.Taylor@Sun.COM 
70910588SEric.Taylor@Sun.COM 	mutex_exit(&zvol_state_lock);
7106423Sgw25295 }
7116423Sgw25295 
712789Sahrens int
7134787Sahrens zvol_set_volsize(const char *name, major_t maj, uint64_t volsize)
714789Sahrens {
71510588SEric.Taylor@Sun.COM 	zvol_state_t *zv = NULL;
71610588SEric.Taylor@Sun.COM 	objset_t *os;
717789Sahrens 	int error;
7181133Seschrock 	dmu_object_info_t doi;
7196423Sgw25295 	uint64_t old_volsize = 0ULL;
72010588SEric.Taylor@Sun.COM 	uint64_t readonly;
721789Sahrens 
722789Sahrens 	mutex_enter(&zvol_state_lock);
72310588SEric.Taylor@Sun.COM 	zv = zvol_minor_lookup(name);
72410588SEric.Taylor@Sun.COM 	if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) {
72510588SEric.Taylor@Sun.COM 		mutex_exit(&zvol_state_lock);
72610588SEric.Taylor@Sun.COM 		return (error);
72710588SEric.Taylor@Sun.COM 	}
728789Sahrens 
72910588SEric.Taylor@Sun.COM 	if ((error = dmu_object_info(os, ZVOL_OBJ, &doi)) != 0 ||
7302676Seschrock 	    (error = zvol_check_volsize(volsize,
7317265Sahrens 	    doi.doi_data_block_size)) != 0)
7327265Sahrens 		goto out;
7331133Seschrock 
73410588SEric.Taylor@Sun.COM 	VERIFY(dsl_prop_get_integer(name, "readonly", &readonly,
73510588SEric.Taylor@Sun.COM 	    NULL) == 0);
73610588SEric.Taylor@Sun.COM 	if (readonly) {
7377265Sahrens 		error = EROFS;
7387265Sahrens 		goto out;
739789Sahrens 	}
740789Sahrens 
74110588SEric.Taylor@Sun.COM 	error = zvol_update_volsize(os, volsize);
7426423Sgw25295 	/*
7436423Sgw25295 	 * Reinitialize the dump area to the new size. If we
74410588SEric.Taylor@Sun.COM 	 * failed to resize the dump area then restore it back to
74510588SEric.Taylor@Sun.COM 	 * its original size.
7466423Sgw25295 	 */
74710588SEric.Taylor@Sun.COM 	if (zv && error == 0) {
74810588SEric.Taylor@Sun.COM 		if (zv->zv_flags & ZVOL_DUMPIFIED) {
74910588SEric.Taylor@Sun.COM 			old_volsize = zv->zv_volsize;
75010588SEric.Taylor@Sun.COM 			zv->zv_volsize = volsize;
75110588SEric.Taylor@Sun.COM 			if ((error = zvol_dumpify(zv)) != 0 ||
75210588SEric.Taylor@Sun.COM 			    (error = dumpvp_resize()) != 0) {
75310588SEric.Taylor@Sun.COM 				(void) zvol_update_volsize(os, old_volsize);
75410588SEric.Taylor@Sun.COM 				zv->zv_volsize = old_volsize;
75510588SEric.Taylor@Sun.COM 				error = zvol_dumpify(zv);
75610588SEric.Taylor@Sun.COM 			}
75710588SEric.Taylor@Sun.COM 		}
75810588SEric.Taylor@Sun.COM 		if (error == 0) {
75910588SEric.Taylor@Sun.COM 			zv->zv_volsize = volsize;
76010588SEric.Taylor@Sun.COM 			zvol_size_changed(volsize, maj, zv->zv_minor);
7616423Sgw25295 		}
762789Sahrens 	}
763789Sahrens 
7649816SGeorge.Wilson@Sun.COM 	/*
7659816SGeorge.Wilson@Sun.COM 	 * Generate a LUN expansion event.
7669816SGeorge.Wilson@Sun.COM 	 */
76710588SEric.Taylor@Sun.COM 	if (zv && error == 0) {
7689816SGeorge.Wilson@Sun.COM 		sysevent_id_t eid;
7699816SGeorge.Wilson@Sun.COM 		nvlist_t *attr;
7709816SGeorge.Wilson@Sun.COM 		char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
7719816SGeorge.Wilson@Sun.COM 
77210588SEric.Taylor@Sun.COM 		(void) snprintf(physpath, MAXPATHLEN, "%s%u", ZVOL_PSEUDO_DEV,
7739816SGeorge.Wilson@Sun.COM 		    zv->zv_minor);
7749816SGeorge.Wilson@Sun.COM 
7759816SGeorge.Wilson@Sun.COM 		VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
7769816SGeorge.Wilson@Sun.COM 		VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
7779816SGeorge.Wilson@Sun.COM 
7789816SGeorge.Wilson@Sun.COM 		(void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
7799816SGeorge.Wilson@Sun.COM 		    ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
7809816SGeorge.Wilson@Sun.COM 
7819816SGeorge.Wilson@Sun.COM 		nvlist_free(attr);
7829816SGeorge.Wilson@Sun.COM 		kmem_free(physpath, MAXPATHLEN);
7839816SGeorge.Wilson@Sun.COM 	}
7849816SGeorge.Wilson@Sun.COM 
7857265Sahrens out:
78610588SEric.Taylor@Sun.COM 	dmu_objset_rele(os, FTAG);
7877265Sahrens 
788789Sahrens 	mutex_exit(&zvol_state_lock);
789789Sahrens 
790789Sahrens 	return (error);
791789Sahrens }
792789Sahrens 
793789Sahrens /*ARGSUSED*/
794789Sahrens int
795789Sahrens zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr)
796789Sahrens {
797789Sahrens 	minor_t minor = getminor(*devp);
798789Sahrens 	zvol_state_t *zv;
79910588SEric.Taylor@Sun.COM 	int err = 0;
800789Sahrens 
801789Sahrens 	if (minor == 0)			/* This is the control device */
802789Sahrens 		return (0);
803789Sahrens 
804789Sahrens 	mutex_enter(&zvol_state_lock);
805789Sahrens 
806789Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
807789Sahrens 	if (zv == NULL) {
808789Sahrens 		mutex_exit(&zvol_state_lock);
809789Sahrens 		return (ENXIO);
810789Sahrens 	}
811789Sahrens 
81210588SEric.Taylor@Sun.COM 	if (zv->zv_total_opens == 0)
81310588SEric.Taylor@Sun.COM 		err = zvol_first_open(zv);
81410588SEric.Taylor@Sun.COM 	if (err) {
815789Sahrens 		mutex_exit(&zvol_state_lock);
81610588SEric.Taylor@Sun.COM 		return (err);
81710588SEric.Taylor@Sun.COM 	}
81810588SEric.Taylor@Sun.COM 	if ((flag & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
81910588SEric.Taylor@Sun.COM 		err = EROFS;
82010588SEric.Taylor@Sun.COM 		goto out;
821789Sahrens 	}
8227405SEric.Taylor@Sun.COM 	if (zv->zv_flags & ZVOL_EXCL) {
82310588SEric.Taylor@Sun.COM 		err = EBUSY;
82410588SEric.Taylor@Sun.COM 		goto out;
8257405SEric.Taylor@Sun.COM 	}
8267405SEric.Taylor@Sun.COM 	if (flag & FEXCL) {
8277405SEric.Taylor@Sun.COM 		if (zv->zv_total_opens != 0) {
82810588SEric.Taylor@Sun.COM 			err = EBUSY;
82910588SEric.Taylor@Sun.COM 			goto out;
8307405SEric.Taylor@Sun.COM 		}
8317405SEric.Taylor@Sun.COM 		zv->zv_flags |= ZVOL_EXCL;
8327405SEric.Taylor@Sun.COM 	}
833789Sahrens 
834789Sahrens 	if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) {
835789Sahrens 		zv->zv_open_count[otyp]++;
836789Sahrens 		zv->zv_total_opens++;
837789Sahrens 	}
838789Sahrens 	mutex_exit(&zvol_state_lock);
839789Sahrens 
84010588SEric.Taylor@Sun.COM 	return (err);
84110588SEric.Taylor@Sun.COM out:
84210588SEric.Taylor@Sun.COM 	if (zv->zv_total_opens == 0)
84310588SEric.Taylor@Sun.COM 		zvol_last_close(zv);
84410588SEric.Taylor@Sun.COM 	mutex_exit(&zvol_state_lock);
84510588SEric.Taylor@Sun.COM 	return (err);
846789Sahrens }
847789Sahrens 
848789Sahrens /*ARGSUSED*/
849789Sahrens int
850789Sahrens zvol_close(dev_t dev, int flag, int otyp, cred_t *cr)
851789Sahrens {
852789Sahrens 	minor_t minor = getminor(dev);
853789Sahrens 	zvol_state_t *zv;
85410588SEric.Taylor@Sun.COM 	int error = 0;
855789Sahrens 
856789Sahrens 	if (minor == 0)		/* This is the control device */
857789Sahrens 		return (0);
858789Sahrens 
859789Sahrens 	mutex_enter(&zvol_state_lock);
860789Sahrens 
861789Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
862789Sahrens 	if (zv == NULL) {
863789Sahrens 		mutex_exit(&zvol_state_lock);
864789Sahrens 		return (ENXIO);
865789Sahrens 	}
866789Sahrens 
8677405SEric.Taylor@Sun.COM 	if (zv->zv_flags & ZVOL_EXCL) {
8687405SEric.Taylor@Sun.COM 		ASSERT(zv->zv_total_opens == 1);
8697405SEric.Taylor@Sun.COM 		zv->zv_flags &= ~ZVOL_EXCL;
870789Sahrens 	}
871789Sahrens 
872789Sahrens 	/*
873789Sahrens 	 * If the open count is zero, this is a spurious close.
874789Sahrens 	 * That indicates a bug in the kernel / DDI framework.
875789Sahrens 	 */
876789Sahrens 	ASSERT(zv->zv_open_count[otyp] != 0);
877789Sahrens 	ASSERT(zv->zv_total_opens != 0);
878789Sahrens 
879789Sahrens 	/*
880789Sahrens 	 * You may get multiple opens, but only one close.
881789Sahrens 	 */
882789Sahrens 	zv->zv_open_count[otyp]--;
883789Sahrens 	zv->zv_total_opens--;
884789Sahrens 
88510588SEric.Taylor@Sun.COM 	if (zv->zv_total_opens == 0)
88610588SEric.Taylor@Sun.COM 		zvol_last_close(zv);
887789Sahrens 
88810588SEric.Taylor@Sun.COM 	mutex_exit(&zvol_state_lock);
88910588SEric.Taylor@Sun.COM 	return (error);
890789Sahrens }
891789Sahrens 
8923638Sbillm static void
89310922SJeff.Bonwick@Sun.COM zvol_get_done(zgd_t *zgd, int error)
8943063Sperrin {
89510922SJeff.Bonwick@Sun.COM 	if (zgd->zgd_db)
89610922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(zgd->zgd_db, zgd);
8973063Sperrin 
89810922SJeff.Bonwick@Sun.COM 	zfs_range_unlock(zgd->zgd_rl);
89910922SJeff.Bonwick@Sun.COM 
90010922SJeff.Bonwick@Sun.COM 	if (error == 0 && zgd->zgd_bp)
90110922SJeff.Bonwick@Sun.COM 		zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
90210922SJeff.Bonwick@Sun.COM 
9033063Sperrin 	kmem_free(zgd, sizeof (zgd_t));
9043063Sperrin }
9053063Sperrin 
9063063Sperrin /*
9073063Sperrin  * Get data to generate a TX_WRITE intent log record.
9083063Sperrin  */
9093638Sbillm static int
9103063Sperrin zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
9113063Sperrin {
9123063Sperrin 	zvol_state_t *zv = arg;
9133063Sperrin 	objset_t *os = zv->zv_objset;
91410922SJeff.Bonwick@Sun.COM 	uint64_t object = ZVOL_OBJ;
91510922SJeff.Bonwick@Sun.COM 	uint64_t offset = lr->lr_offset;
91610922SJeff.Bonwick@Sun.COM 	uint64_t size = lr->lr_length;	/* length of user data */
91710922SJeff.Bonwick@Sun.COM 	blkptr_t *bp = &lr->lr_blkptr;
9183063Sperrin 	dmu_buf_t *db;
9193063Sperrin 	zgd_t *zgd;
9203063Sperrin 	int error;
9213063Sperrin 
92210922SJeff.Bonwick@Sun.COM 	ASSERT(zio != NULL);
92310922SJeff.Bonwick@Sun.COM 	ASSERT(size != 0);
92410922SJeff.Bonwick@Sun.COM 
92510922SJeff.Bonwick@Sun.COM 	zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
92610922SJeff.Bonwick@Sun.COM 	zgd->zgd_zilog = zv->zv_zilog;
92710922SJeff.Bonwick@Sun.COM 	zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
9283638Sbillm 
9293755Sperrin 	/*
9303755Sperrin 	 * Write records come in two flavors: immediate and indirect.
9313755Sperrin 	 * For small writes it's cheaper to store the data with the
9323755Sperrin 	 * log record (immediate); for large writes it's cheaper to
9333755Sperrin 	 * sync the data and get a pointer to it (indirect) so that
9343755Sperrin 	 * we don't have to write the data twice.
9353755Sperrin 	 */
93610922SJeff.Bonwick@Sun.COM 	if (buf != NULL) {	/* immediate write */
93710922SJeff.Bonwick@Sun.COM 		error = dmu_read(os, object, offset, size, buf,
93810922SJeff.Bonwick@Sun.COM 		    DMU_READ_NO_PREFETCH);
93910922SJeff.Bonwick@Sun.COM 	} else {
94010922SJeff.Bonwick@Sun.COM 		size = zv->zv_volblocksize;
94110922SJeff.Bonwick@Sun.COM 		offset = P2ALIGN(offset, size);
94210922SJeff.Bonwick@Sun.COM 		error = dmu_buf_hold(os, object, offset, zgd, &db);
94310922SJeff.Bonwick@Sun.COM 		if (error == 0) {
94410922SJeff.Bonwick@Sun.COM 			zgd->zgd_db = db;
94510922SJeff.Bonwick@Sun.COM 			zgd->zgd_bp = bp;
9463063Sperrin 
94710922SJeff.Bonwick@Sun.COM 			ASSERT(db->db_offset == offset);
94810922SJeff.Bonwick@Sun.COM 			ASSERT(db->db_size == size);
9493755Sperrin 
95010922SJeff.Bonwick@Sun.COM 			error = dmu_sync(zio, lr->lr_common.lrc_txg,
95110922SJeff.Bonwick@Sun.COM 			    zvol_get_done, zgd);
95210800SNeil.Perrin@Sun.COM 
95310922SJeff.Bonwick@Sun.COM 			if (error == 0)
95410922SJeff.Bonwick@Sun.COM 				return (0);
95510922SJeff.Bonwick@Sun.COM 		}
95610800SNeil.Perrin@Sun.COM 	}
95710800SNeil.Perrin@Sun.COM 
95810922SJeff.Bonwick@Sun.COM 	zvol_get_done(zgd, error);
95910922SJeff.Bonwick@Sun.COM 
9603063Sperrin 	return (error);
9613063Sperrin }
9623063Sperrin 
9631861Sperrin /*
9641861Sperrin  * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
9651141Sperrin  *
9661141Sperrin  * We store data in the log buffers if it's small enough.
9673063Sperrin  * Otherwise we will later flush the data out via dmu_sync().
9681141Sperrin  */
9693063Sperrin ssize_t zvol_immediate_write_sz = 32768;
9701141Sperrin 
9713638Sbillm static void
9729401SNeil.Perrin@Sun.COM zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid,
9739401SNeil.Perrin@Sun.COM     boolean_t sync)
9741141Sperrin {
9753638Sbillm 	uint32_t blocksize = zv->zv_volblocksize;
9768227SNeil.Perrin@Sun.COM 	zilog_t *zilog = zv->zv_zilog;
9779401SNeil.Perrin@Sun.COM 	boolean_t slogging;
97810310SNeil.Perrin@Sun.COM 	ssize_t immediate_write_sz;
9799401SNeil.Perrin@Sun.COM 
9809401SNeil.Perrin@Sun.COM 	if (zil_disable)
9819401SNeil.Perrin@Sun.COM 		return;
9821861Sperrin 
98310922SJeff.Bonwick@Sun.COM 	if (zil_replaying(zilog, tx))
9848227SNeil.Perrin@Sun.COM 		return;
9858227SNeil.Perrin@Sun.COM 
98610310SNeil.Perrin@Sun.COM 	immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
98710310SNeil.Perrin@Sun.COM 	    ? 0 : zvol_immediate_write_sz;
98810310SNeil.Perrin@Sun.COM 
98910310SNeil.Perrin@Sun.COM 	slogging = spa_has_slogs(zilog->zl_spa) &&
99010310SNeil.Perrin@Sun.COM 	    (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
9919401SNeil.Perrin@Sun.COM 
9929401SNeil.Perrin@Sun.COM 	while (resid) {
9939401SNeil.Perrin@Sun.COM 		itx_t *itx;
9949401SNeil.Perrin@Sun.COM 		lr_write_t *lr;
9959401SNeil.Perrin@Sun.COM 		ssize_t len;
9969401SNeil.Perrin@Sun.COM 		itx_wr_state_t write_state;
9973638Sbillm 
9989401SNeil.Perrin@Sun.COM 		/*
9999401SNeil.Perrin@Sun.COM 		 * Unlike zfs_log_write() we can be called with
10009401SNeil.Perrin@Sun.COM 		 * upto DMU_MAX_ACCESS/2 (5MB) writes.
10019401SNeil.Perrin@Sun.COM 		 */
100210310SNeil.Perrin@Sun.COM 		if (blocksize > immediate_write_sz && !slogging &&
10039401SNeil.Perrin@Sun.COM 		    resid >= blocksize && off % blocksize == 0) {
10049401SNeil.Perrin@Sun.COM 			write_state = WR_INDIRECT; /* uses dmu_sync */
10059401SNeil.Perrin@Sun.COM 			len = blocksize;
10069401SNeil.Perrin@Sun.COM 		} else if (sync) {
10079401SNeil.Perrin@Sun.COM 			write_state = WR_COPIED;
10089401SNeil.Perrin@Sun.COM 			len = MIN(ZIL_MAX_LOG_DATA, resid);
10099401SNeil.Perrin@Sun.COM 		} else {
10109401SNeil.Perrin@Sun.COM 			write_state = WR_NEED_COPY;
10119401SNeil.Perrin@Sun.COM 			len = MIN(ZIL_MAX_LOG_DATA, resid);
10129401SNeil.Perrin@Sun.COM 		}
10139401SNeil.Perrin@Sun.COM 
10149401SNeil.Perrin@Sun.COM 		itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
10159401SNeil.Perrin@Sun.COM 		    (write_state == WR_COPIED ? len : 0));
10163638Sbillm 		lr = (lr_write_t *)&itx->itx_lr;
10179401SNeil.Perrin@Sun.COM 		if (write_state == WR_COPIED && dmu_read(zv->zv_objset,
10189512SNeil.Perrin@Sun.COM 		    ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
101910922SJeff.Bonwick@Sun.COM 			zil_itx_destroy(itx);
10209401SNeil.Perrin@Sun.COM 			itx = zil_itx_create(TX_WRITE, sizeof (*lr));
10219401SNeil.Perrin@Sun.COM 			lr = (lr_write_t *)&itx->itx_lr;
10229401SNeil.Perrin@Sun.COM 			write_state = WR_NEED_COPY;
10239401SNeil.Perrin@Sun.COM 		}
10249401SNeil.Perrin@Sun.COM 
10259401SNeil.Perrin@Sun.COM 		itx->itx_wr_state = write_state;
10269401SNeil.Perrin@Sun.COM 		if (write_state == WR_NEED_COPY)
10279401SNeil.Perrin@Sun.COM 			itx->itx_sod += len;
10283638Sbillm 		lr->lr_foid = ZVOL_OBJ;
10293638Sbillm 		lr->lr_offset = off;
10309401SNeil.Perrin@Sun.COM 		lr->lr_length = len;
103110922SJeff.Bonwick@Sun.COM 		lr->lr_blkoff = 0;
10323638Sbillm 		BP_ZERO(&lr->lr_blkptr);
10333638Sbillm 
10349401SNeil.Perrin@Sun.COM 		itx->itx_private = zv;
10359401SNeil.Perrin@Sun.COM 		itx->itx_sync = sync;
10369401SNeil.Perrin@Sun.COM 
10378227SNeil.Perrin@Sun.COM 		(void) zil_itx_assign(zilog, itx, tx);
10389401SNeil.Perrin@Sun.COM 
10399401SNeil.Perrin@Sun.COM 		off += len;
10409401SNeil.Perrin@Sun.COM 		resid -= len;
10411141Sperrin 	}
10421141Sperrin }
10431141Sperrin 
10447837SMatthew.Ahrens@Sun.COM static int
10457837SMatthew.Ahrens@Sun.COM zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t size,
10467837SMatthew.Ahrens@Sun.COM     boolean_t doread, boolean_t isdump)
10476423Sgw25295 {
10486423Sgw25295 	vdev_disk_t *dvd;
10496423Sgw25295 	int c;
10506423Sgw25295 	int numerrors = 0;
10516423Sgw25295 
10526423Sgw25295 	for (c = 0; c < vd->vdev_children; c++) {
10539790SLin.Ling@Sun.COM 		ASSERT(vd->vdev_ops == &vdev_mirror_ops ||
10549790SLin.Ling@Sun.COM 		    vd->vdev_ops == &vdev_replacing_ops ||
10559790SLin.Ling@Sun.COM 		    vd->vdev_ops == &vdev_spare_ops);
10567837SMatthew.Ahrens@Sun.COM 		int err = zvol_dumpio_vdev(vd->vdev_child[c],
10577837SMatthew.Ahrens@Sun.COM 		    addr, offset, size, doread, isdump);
10587837SMatthew.Ahrens@Sun.COM 		if (err != 0) {
10596423Sgw25295 			numerrors++;
10607837SMatthew.Ahrens@Sun.COM 		} else if (doread) {
10616423Sgw25295 			break;
10626423Sgw25295 		}
10636423Sgw25295 	}
10646423Sgw25295 
10656423Sgw25295 	if (!vd->vdev_ops->vdev_op_leaf)
10666423Sgw25295 		return (numerrors < vd->vdev_children ? 0 : EIO);
10676423Sgw25295 
10687903SEric.Taylor@Sun.COM 	if (doread && !vdev_readable(vd))
10697903SEric.Taylor@Sun.COM 		return (EIO);
10707903SEric.Taylor@Sun.COM 	else if (!doread && !vdev_writeable(vd))
10716423Sgw25295 		return (EIO);
10726423Sgw25295 
10736423Sgw25295 	dvd = vd->vdev_tsd;
10746423Sgw25295 	ASSERT3P(dvd, !=, NULL);
10756423Sgw25295 	offset += VDEV_LABEL_START_SIZE;
10766423Sgw25295 
10776423Sgw25295 	if (ddi_in_panic() || isdump) {
10787837SMatthew.Ahrens@Sun.COM 		ASSERT(!doread);
10797837SMatthew.Ahrens@Sun.COM 		if (doread)
10806423Sgw25295 			return (EIO);
10816423Sgw25295 		return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset),
10826423Sgw25295 		    lbtodb(size)));
10836423Sgw25295 	} else {
10846423Sgw25295 		return (vdev_disk_physio(dvd->vd_lh, addr, size, offset,
10857837SMatthew.Ahrens@Sun.COM 		    doread ? B_READ : B_WRITE));
10866423Sgw25295 	}
10876423Sgw25295 }
10886423Sgw25295 
10897837SMatthew.Ahrens@Sun.COM static int
10907837SMatthew.Ahrens@Sun.COM zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size,
10917837SMatthew.Ahrens@Sun.COM     boolean_t doread, boolean_t isdump)
10926423Sgw25295 {
10936423Sgw25295 	vdev_t *vd;
10946423Sgw25295 	int error;
10957837SMatthew.Ahrens@Sun.COM 	zvol_extent_t *ze;
10966423Sgw25295 	spa_t *spa = dmu_objset_spa(zv->zv_objset);
10976423Sgw25295 
10987837SMatthew.Ahrens@Sun.COM 	/* Must be sector aligned, and not stradle a block boundary. */
10997837SMatthew.Ahrens@Sun.COM 	if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) ||
11007837SMatthew.Ahrens@Sun.COM 	    P2BOUNDARY(offset, size, zv->zv_volblocksize)) {
11017837SMatthew.Ahrens@Sun.COM 		return (EINVAL);
11027837SMatthew.Ahrens@Sun.COM 	}
11036423Sgw25295 	ASSERT(size <= zv->zv_volblocksize);
11046423Sgw25295 
11057837SMatthew.Ahrens@Sun.COM 	/* Locate the extent this belongs to */
11067837SMatthew.Ahrens@Sun.COM 	ze = list_head(&zv->zv_extents);
11077837SMatthew.Ahrens@Sun.COM 	while (offset >= ze->ze_nblks * zv->zv_volblocksize) {
11087837SMatthew.Ahrens@Sun.COM 		offset -= ze->ze_nblks * zv->zv_volblocksize;
11097837SMatthew.Ahrens@Sun.COM 		ze = list_next(&zv->zv_extents, ze);
11107837SMatthew.Ahrens@Sun.COM 	}
11117754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
11127837SMatthew.Ahrens@Sun.COM 	vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva));
11137837SMatthew.Ahrens@Sun.COM 	offset += DVA_GET_OFFSET(&ze->ze_dva);
11147837SMatthew.Ahrens@Sun.COM 	error = zvol_dumpio_vdev(vd, addr, offset, size, doread, isdump);
11157754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
11166423Sgw25295 	return (error);
11176423Sgw25295 }
11186423Sgw25295 
11196423Sgw25295 int
1120789Sahrens zvol_strategy(buf_t *bp)
1121789Sahrens {
1122789Sahrens 	zvol_state_t *zv = ddi_get_soft_state(zvol_state, getminor(bp->b_edev));
1123789Sahrens 	uint64_t off, volsize;
11247837SMatthew.Ahrens@Sun.COM 	size_t resid;
1125789Sahrens 	char *addr;
11261141Sperrin 	objset_t *os;
11273755Sperrin 	rl_t *rl;
1128789Sahrens 	int error = 0;
11297837SMatthew.Ahrens@Sun.COM 	boolean_t doread = bp->b_flags & B_READ;
11307837SMatthew.Ahrens@Sun.COM 	boolean_t is_dump = zv->zv_flags & ZVOL_DUMPIFIED;
11319401SNeil.Perrin@Sun.COM 	boolean_t sync;
1132789Sahrens 
1133789Sahrens 	if (zv == NULL) {
1134789Sahrens 		bioerror(bp, ENXIO);
1135789Sahrens 		biodone(bp);
1136789Sahrens 		return (0);
1137789Sahrens 	}
1138789Sahrens 
1139789Sahrens 	if (getminor(bp->b_edev) == 0) {
1140789Sahrens 		bioerror(bp, EINVAL);
1141789Sahrens 		biodone(bp);
1142789Sahrens 		return (0);
1143789Sahrens 	}
1144789Sahrens 
114510588SEric.Taylor@Sun.COM 	if (!(bp->b_flags & B_READ) && (zv->zv_flags & ZVOL_RDONLY)) {
1146789Sahrens 		bioerror(bp, EROFS);
1147789Sahrens 		biodone(bp);
1148789Sahrens 		return (0);
1149789Sahrens 	}
1150789Sahrens 
1151789Sahrens 	off = ldbtob(bp->b_blkno);
1152789Sahrens 	volsize = zv->zv_volsize;
1153789Sahrens 
11541141Sperrin 	os = zv->zv_objset;
11551141Sperrin 	ASSERT(os != NULL);
1156789Sahrens 
1157789Sahrens 	bp_mapin(bp);
1158789Sahrens 	addr = bp->b_un.b_addr;
1159789Sahrens 	resid = bp->b_bcount;
1160789Sahrens 
11617837SMatthew.Ahrens@Sun.COM 	if (resid > 0 && (off < 0 || off >= volsize)) {
11627837SMatthew.Ahrens@Sun.COM 		bioerror(bp, EIO);
11637837SMatthew.Ahrens@Sun.COM 		biodone(bp);
11647837SMatthew.Ahrens@Sun.COM 		return (0);
11657837SMatthew.Ahrens@Sun.COM 	}
11667013Sgw25295 
11679401SNeil.Perrin@Sun.COM 	sync = !(bp->b_flags & B_ASYNC) && !doread && !is_dump &&
11689401SNeil.Perrin@Sun.COM 	    !(zv->zv_flags & ZVOL_WCE) && !zil_disable;
11699401SNeil.Perrin@Sun.COM 
11701861Sperrin 	/*
11711861Sperrin 	 * There must be no buffer changes when doing a dmu_sync() because
11721861Sperrin 	 * we can't change the data whilst calculating the checksum.
11731861Sperrin 	 */
11743755Sperrin 	rl = zfs_range_lock(&zv->zv_znode, off, resid,
11757837SMatthew.Ahrens@Sun.COM 	    doread ? RL_READER : RL_WRITER);
11766423Sgw25295 
1177789Sahrens 	while (resid != 0 && off < volsize) {
11787837SMatthew.Ahrens@Sun.COM 		size_t size = MIN(resid, zvol_maxphys);
11796423Sgw25295 		if (is_dump) {
11806423Sgw25295 			size = MIN(size, P2END(off, zv->zv_volblocksize) - off);
11817837SMatthew.Ahrens@Sun.COM 			error = zvol_dumpio(zv, addr, off, size,
11827837SMatthew.Ahrens@Sun.COM 			    doread, B_FALSE);
11837837SMatthew.Ahrens@Sun.COM 		} else if (doread) {
11849512SNeil.Perrin@Sun.COM 			error = dmu_read(os, ZVOL_OBJ, off, size, addr,
11859512SNeil.Perrin@Sun.COM 			    DMU_READ_PREFETCH);
1186789Sahrens 		} else {
11871141Sperrin 			dmu_tx_t *tx = dmu_tx_create(os);
1188789Sahrens 			dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
1189789Sahrens 			error = dmu_tx_assign(tx, TXG_WAIT);
1190789Sahrens 			if (error) {
1191789Sahrens 				dmu_tx_abort(tx);
1192789Sahrens 			} else {
11931141Sperrin 				dmu_write(os, ZVOL_OBJ, off, size, addr, tx);
11949401SNeil.Perrin@Sun.COM 				zvol_log_write(zv, tx, off, size, sync);
1195789Sahrens 				dmu_tx_commit(tx);
1196789Sahrens 			}
1197789Sahrens 		}
11987294Sperrin 		if (error) {
11997294Sperrin 			/* convert checksum errors into IO errors */
12007294Sperrin 			if (error == ECKSUM)
12017294Sperrin 				error = EIO;
1202789Sahrens 			break;
12037294Sperrin 		}
1204789Sahrens 		off += size;
1205789Sahrens 		addr += size;
1206789Sahrens 		resid -= size;
1207789Sahrens 	}
12083755Sperrin 	zfs_range_unlock(rl);
1209789Sahrens 
1210789Sahrens 	if ((bp->b_resid = resid) == bp->b_bcount)
1211789Sahrens 		bioerror(bp, off > volsize ? EINVAL : error);
1212789Sahrens 
12139401SNeil.Perrin@Sun.COM 	if (sync)
12143638Sbillm 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
12153638Sbillm 	biodone(bp);
12161141Sperrin 
1217789Sahrens 	return (0);
1218789Sahrens }
1219789Sahrens 
12203063Sperrin /*
12213063Sperrin  * Set the buffer count to the zvol maximum transfer.
12223063Sperrin  * Using our own routine instead of the default minphys()
12233063Sperrin  * means that for larger writes we write bigger buffers on X86
12243063Sperrin  * (128K instead of 56K) and flush the disk write cache less often
12253063Sperrin  * (every zvol_maxphys - currently 1MB) instead of minphys (currently
12263063Sperrin  * 56K on X86 and 128K on sparc).
12273063Sperrin  */
12283063Sperrin void
12293063Sperrin zvol_minphys(struct buf *bp)
12303063Sperrin {
12313063Sperrin 	if (bp->b_bcount > zvol_maxphys)
12323063Sperrin 		bp->b_bcount = zvol_maxphys;
12333063Sperrin }
12343063Sperrin 
12356423Sgw25295 int
12366423Sgw25295 zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks)
12376423Sgw25295 {
12386423Sgw25295 	minor_t minor = getminor(dev);
12396423Sgw25295 	zvol_state_t *zv;
12406423Sgw25295 	int error = 0;
12416423Sgw25295 	uint64_t size;
12426423Sgw25295 	uint64_t boff;
12436423Sgw25295 	uint64_t resid;
12446423Sgw25295 
12456423Sgw25295 	if (minor == 0)			/* This is the control device */
12466423Sgw25295 		return (ENXIO);
12476423Sgw25295 
12486423Sgw25295 	zv = ddi_get_soft_state(zvol_state, minor);
12496423Sgw25295 	if (zv == NULL)
12506423Sgw25295 		return (ENXIO);
12516423Sgw25295 
12526423Sgw25295 	boff = ldbtob(blkno);
12536423Sgw25295 	resid = ldbtob(nblocks);
12547837SMatthew.Ahrens@Sun.COM 
12557837SMatthew.Ahrens@Sun.COM 	VERIFY3U(boff + resid, <=, zv->zv_volsize);
12567837SMatthew.Ahrens@Sun.COM 
12576423Sgw25295 	while (resid) {
12586423Sgw25295 		size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff);
12597837SMatthew.Ahrens@Sun.COM 		error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE);
12606423Sgw25295 		if (error)
12616423Sgw25295 			break;
12626423Sgw25295 		boff += size;
12636423Sgw25295 		addr += size;
12646423Sgw25295 		resid -= size;
12656423Sgw25295 	}
12666423Sgw25295 
12676423Sgw25295 	return (error);
12686423Sgw25295 }
12696423Sgw25295 
1270789Sahrens /*ARGSUSED*/
1271789Sahrens int
12723638Sbillm zvol_read(dev_t dev, uio_t *uio, cred_t *cr)
1273789Sahrens {
12744107Sgw25295 	minor_t minor = getminor(dev);
12754107Sgw25295 	zvol_state_t *zv;
12767013Sgw25295 	uint64_t volsize;
12773755Sperrin 	rl_t *rl;
12783638Sbillm 	int error = 0;
12793638Sbillm 
12804107Sgw25295 	if (minor == 0)			/* This is the control device */
12814107Sgw25295 		return (ENXIO);
12824107Sgw25295 
12834107Sgw25295 	zv = ddi_get_soft_state(zvol_state, minor);
12844107Sgw25295 	if (zv == NULL)
12854107Sgw25295 		return (ENXIO);
12864107Sgw25295 
12877013Sgw25295 	volsize = zv->zv_volsize;
12887013Sgw25295 	if (uio->uio_resid > 0 &&
12897013Sgw25295 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
12907013Sgw25295 		return (EIO);
12917013Sgw25295 
12927837SMatthew.Ahrens@Sun.COM 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
12937837SMatthew.Ahrens@Sun.COM 		error = physio(zvol_strategy, NULL, dev, B_READ,
12947837SMatthew.Ahrens@Sun.COM 		    zvol_minphys, uio);
12957837SMatthew.Ahrens@Sun.COM 		return (error);
12967837SMatthew.Ahrens@Sun.COM 	}
12977837SMatthew.Ahrens@Sun.COM 
12983755Sperrin 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
12993755Sperrin 	    RL_READER);
13007013Sgw25295 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
13013638Sbillm 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
13023638Sbillm 
13037013Sgw25295 		/* don't read past the end */
13047013Sgw25295 		if (bytes > volsize - uio->uio_loffset)
13057013Sgw25295 			bytes = volsize - uio->uio_loffset;
13067013Sgw25295 
13073638Sbillm 		error =  dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes);
13087294Sperrin 		if (error) {
13097294Sperrin 			/* convert checksum errors into IO errors */
13107294Sperrin 			if (error == ECKSUM)
13117294Sperrin 				error = EIO;
13123638Sbillm 			break;
13137294Sperrin 		}
13143638Sbillm 	}
13153755Sperrin 	zfs_range_unlock(rl);
13163638Sbillm 	return (error);
1317789Sahrens }
1318789Sahrens 
1319789Sahrens /*ARGSUSED*/
1320789Sahrens int
13213638Sbillm zvol_write(dev_t dev, uio_t *uio, cred_t *cr)
1322789Sahrens {
13234107Sgw25295 	minor_t minor = getminor(dev);
13244107Sgw25295 	zvol_state_t *zv;
13257013Sgw25295 	uint64_t volsize;
13263755Sperrin 	rl_t *rl;
13273638Sbillm 	int error = 0;
13289401SNeil.Perrin@Sun.COM 	boolean_t sync;
13293638Sbillm 
13304107Sgw25295 	if (minor == 0)			/* This is the control device */
13314107Sgw25295 		return (ENXIO);
13324107Sgw25295 
13334107Sgw25295 	zv = ddi_get_soft_state(zvol_state, minor);
13344107Sgw25295 	if (zv == NULL)
13354107Sgw25295 		return (ENXIO);
13364107Sgw25295 
13377013Sgw25295 	volsize = zv->zv_volsize;
13387013Sgw25295 	if (uio->uio_resid > 0 &&
13397013Sgw25295 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
13407013Sgw25295 		return (EIO);
13417013Sgw25295 
13426423Sgw25295 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
13436423Sgw25295 		error = physio(zvol_strategy, NULL, dev, B_WRITE,
13446423Sgw25295 		    zvol_minphys, uio);
13456423Sgw25295 		return (error);
13466423Sgw25295 	}
13476423Sgw25295 
13489401SNeil.Perrin@Sun.COM 	sync = !(zv->zv_flags & ZVOL_WCE) && !zil_disable;
13499401SNeil.Perrin@Sun.COM 
13503755Sperrin 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
13513755Sperrin 	    RL_WRITER);
13527013Sgw25295 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
13533638Sbillm 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
13543638Sbillm 		uint64_t off = uio->uio_loffset;
13557013Sgw25295 		dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
1356789Sahrens 
13577013Sgw25295 		if (bytes > volsize - off)	/* don't write past the end */
13587013Sgw25295 			bytes = volsize - off;
13597013Sgw25295 
13603638Sbillm 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
13613638Sbillm 		error = dmu_tx_assign(tx, TXG_WAIT);
13623638Sbillm 		if (error) {
13633638Sbillm 			dmu_tx_abort(tx);
13643638Sbillm 			break;
13653638Sbillm 		}
13663638Sbillm 		error = dmu_write_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes, tx);
13673638Sbillm 		if (error == 0)
13689401SNeil.Perrin@Sun.COM 			zvol_log_write(zv, tx, off, bytes, sync);
13693638Sbillm 		dmu_tx_commit(tx);
13703638Sbillm 
13713638Sbillm 		if (error)
13723638Sbillm 			break;
13733638Sbillm 	}
13743755Sperrin 	zfs_range_unlock(rl);
13759401SNeil.Perrin@Sun.COM 	if (sync)
13768524SEric.Taylor@Sun.COM 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
13773638Sbillm 	return (error);
1378789Sahrens }
1379789Sahrens 
13807405SEric.Taylor@Sun.COM int
13817405SEric.Taylor@Sun.COM zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs)
13827405SEric.Taylor@Sun.COM {
13837405SEric.Taylor@Sun.COM 	struct uuid uuid = EFI_RESERVED;
13847405SEric.Taylor@Sun.COM 	efi_gpe_t gpe = { 0 };
13857405SEric.Taylor@Sun.COM 	uint32_t crc;
13867405SEric.Taylor@Sun.COM 	dk_efi_t efi;
13877405SEric.Taylor@Sun.COM 	int length;
13887405SEric.Taylor@Sun.COM 	char *ptr;
13897405SEric.Taylor@Sun.COM 
13907405SEric.Taylor@Sun.COM 	if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag))
13917405SEric.Taylor@Sun.COM 		return (EFAULT);
13927405SEric.Taylor@Sun.COM 	ptr = (char *)(uintptr_t)efi.dki_data_64;
13937405SEric.Taylor@Sun.COM 	length = efi.dki_length;
13947405SEric.Taylor@Sun.COM 	/*
13957405SEric.Taylor@Sun.COM 	 * Some clients may attempt to request a PMBR for the
13967405SEric.Taylor@Sun.COM 	 * zvol.  Currently this interface will return EINVAL to
13977405SEric.Taylor@Sun.COM 	 * such requests.  These requests could be supported by
13987405SEric.Taylor@Sun.COM 	 * adding a check for lba == 0 and consing up an appropriate
13997405SEric.Taylor@Sun.COM 	 * PMBR.
14007405SEric.Taylor@Sun.COM 	 */
14017405SEric.Taylor@Sun.COM 	if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0)
14027405SEric.Taylor@Sun.COM 		return (EINVAL);
14037405SEric.Taylor@Sun.COM 
14047405SEric.Taylor@Sun.COM 	gpe.efi_gpe_StartingLBA = LE_64(34ULL);
14057405SEric.Taylor@Sun.COM 	gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1);
14067405SEric.Taylor@Sun.COM 	UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
14077405SEric.Taylor@Sun.COM 
14087405SEric.Taylor@Sun.COM 	if (efi.dki_lba == 1) {
14097405SEric.Taylor@Sun.COM 		efi_gpt_t gpt = { 0 };
14107405SEric.Taylor@Sun.COM 
14117405SEric.Taylor@Sun.COM 		gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
14127405SEric.Taylor@Sun.COM 		gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
14137405SEric.Taylor@Sun.COM 		gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt));
14147405SEric.Taylor@Sun.COM 		gpt.efi_gpt_MyLBA = LE_64(1ULL);
14157405SEric.Taylor@Sun.COM 		gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL);
14167405SEric.Taylor@Sun.COM 		gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1);
14177405SEric.Taylor@Sun.COM 		gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL);
14187405SEric.Taylor@Sun.COM 		gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
14197405SEric.Taylor@Sun.COM 		gpt.efi_gpt_SizeOfPartitionEntry =
14207405SEric.Taylor@Sun.COM 		    LE_32(sizeof (efi_gpe_t));
14217405SEric.Taylor@Sun.COM 		CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
14227405SEric.Taylor@Sun.COM 		gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
14237405SEric.Taylor@Sun.COM 		CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table);
14247405SEric.Taylor@Sun.COM 		gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
14257405SEric.Taylor@Sun.COM 		if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length),
14267405SEric.Taylor@Sun.COM 		    flag))
14277405SEric.Taylor@Sun.COM 			return (EFAULT);
14287405SEric.Taylor@Sun.COM 		ptr += sizeof (gpt);
14297405SEric.Taylor@Sun.COM 		length -= sizeof (gpt);
14307405SEric.Taylor@Sun.COM 	}
14317405SEric.Taylor@Sun.COM 	if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe),
14327405SEric.Taylor@Sun.COM 	    length), flag))
14337405SEric.Taylor@Sun.COM 		return (EFAULT);
14347405SEric.Taylor@Sun.COM 	return (0);
14357405SEric.Taylor@Sun.COM }
14367405SEric.Taylor@Sun.COM 
1437789Sahrens /*
1438789Sahrens  * Dirtbag ioctls to support mkfs(1M) for UFS filesystems.  See dkio(7I).
1439789Sahrens  */
1440789Sahrens /*ARGSUSED*/
1441789Sahrens int
1442789Sahrens zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
1443789Sahrens {
1444789Sahrens 	zvol_state_t *zv;
14453897Smaybee 	struct dk_cinfo dki;
1446789Sahrens 	struct dk_minfo dkm;
14473897Smaybee 	struct dk_callback *dkc;
1448789Sahrens 	int error = 0;
14496423Sgw25295 	rl_t *rl;
1450789Sahrens 
1451789Sahrens 	mutex_enter(&zvol_state_lock);
1452789Sahrens 
1453789Sahrens 	zv = ddi_get_soft_state(zvol_state, getminor(dev));
1454789Sahrens 
1455789Sahrens 	if (zv == NULL) {
1456789Sahrens 		mutex_exit(&zvol_state_lock);
1457789Sahrens 		return (ENXIO);
1458789Sahrens 	}
14599303SEric.Taylor@Sun.COM 	ASSERT(zv->zv_total_opens > 0);
1460789Sahrens 
1461789Sahrens 	switch (cmd) {
1462789Sahrens 
1463789Sahrens 	case DKIOCINFO:
14643897Smaybee 		bzero(&dki, sizeof (dki));
14653897Smaybee 		(void) strcpy(dki.dki_cname, "zvol");
14663897Smaybee 		(void) strcpy(dki.dki_dname, "zvol");
14673897Smaybee 		dki.dki_ctype = DKC_UNKNOWN;
14683897Smaybee 		dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs);
1469789Sahrens 		mutex_exit(&zvol_state_lock);
14703897Smaybee 		if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag))
1471789Sahrens 			error = EFAULT;
1472789Sahrens 		return (error);
1473789Sahrens 
1474789Sahrens 	case DKIOCGMEDIAINFO:
1475789Sahrens 		bzero(&dkm, sizeof (dkm));
1476789Sahrens 		dkm.dki_lbsize = 1U << zv->zv_min_bs;
1477789Sahrens 		dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
1478789Sahrens 		dkm.dki_media_type = DK_UNKNOWN;
1479789Sahrens 		mutex_exit(&zvol_state_lock);
1480789Sahrens 		if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
1481789Sahrens 			error = EFAULT;
1482789Sahrens 		return (error);
1483789Sahrens 
1484789Sahrens 	case DKIOCGETEFI:
14857405SEric.Taylor@Sun.COM 		{
14867405SEric.Taylor@Sun.COM 			uint64_t vs = zv->zv_volsize;
14877405SEric.Taylor@Sun.COM 			uint8_t bs = zv->zv_min_bs;
14883016Smaybee 
14893016Smaybee 			mutex_exit(&zvol_state_lock);
14907405SEric.Taylor@Sun.COM 			error = zvol_getefi((void *)arg, flag, vs, bs);
14917405SEric.Taylor@Sun.COM 			return (error);
14923016Smaybee 		}
1493789Sahrens 
14943638Sbillm 	case DKIOCFLUSHWRITECACHE:
14953897Smaybee 		dkc = (struct dk_callback *)arg;
14969303SEric.Taylor@Sun.COM 		mutex_exit(&zvol_state_lock);
14973638Sbillm 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
14983897Smaybee 		if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) {
14993897Smaybee 			(*dkc->dkc_callback)(dkc->dkc_cookie, error);
15003897Smaybee 			error = 0;
15013897Smaybee 		}
15029303SEric.Taylor@Sun.COM 		return (error);
15039303SEric.Taylor@Sun.COM 
15049303SEric.Taylor@Sun.COM 	case DKIOCGETWCE:
15059303SEric.Taylor@Sun.COM 		{
15069303SEric.Taylor@Sun.COM 			int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0;
15079303SEric.Taylor@Sun.COM 			if (ddi_copyout(&wce, (void *)arg, sizeof (int),
15089303SEric.Taylor@Sun.COM 			    flag))
15099303SEric.Taylor@Sun.COM 				error = EFAULT;
15109303SEric.Taylor@Sun.COM 			break;
15119303SEric.Taylor@Sun.COM 		}
15129303SEric.Taylor@Sun.COM 	case DKIOCSETWCE:
15139303SEric.Taylor@Sun.COM 		{
15149303SEric.Taylor@Sun.COM 			int wce;
15159303SEric.Taylor@Sun.COM 			if (ddi_copyin((void *)arg, &wce, sizeof (int),
15169303SEric.Taylor@Sun.COM 			    flag)) {
15179303SEric.Taylor@Sun.COM 				error = EFAULT;
15189303SEric.Taylor@Sun.COM 				break;
15199303SEric.Taylor@Sun.COM 			}
15209303SEric.Taylor@Sun.COM 			if (wce) {
15219303SEric.Taylor@Sun.COM 				zv->zv_flags |= ZVOL_WCE;
15229303SEric.Taylor@Sun.COM 				mutex_exit(&zvol_state_lock);
15239303SEric.Taylor@Sun.COM 			} else {
15249303SEric.Taylor@Sun.COM 				zv->zv_flags &= ~ZVOL_WCE;
15259303SEric.Taylor@Sun.COM 				mutex_exit(&zvol_state_lock);
15269303SEric.Taylor@Sun.COM 				zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
15279303SEric.Taylor@Sun.COM 			}
15289303SEric.Taylor@Sun.COM 			return (0);
15299303SEric.Taylor@Sun.COM 		}
15303638Sbillm 
15313245Smaybee 	case DKIOCGGEOM:
15323245Smaybee 	case DKIOCGVTOC:
15336423Sgw25295 		/*
15346423Sgw25295 		 * commands using these (like prtvtoc) expect ENOTSUP
15356423Sgw25295 		 * since we're emulating an EFI label
15366423Sgw25295 		 */
15373245Smaybee 		error = ENOTSUP;
15383245Smaybee 		break;
15393245Smaybee 
15406423Sgw25295 	case DKIOCDUMPINIT:
15416423Sgw25295 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
15426423Sgw25295 		    RL_WRITER);
15436423Sgw25295 		error = zvol_dumpify(zv);
15446423Sgw25295 		zfs_range_unlock(rl);
15456423Sgw25295 		break;
15466423Sgw25295 
15476423Sgw25295 	case DKIOCDUMPFINI:
15489277SEric.Taylor@Sun.COM 		if (!(zv->zv_flags & ZVOL_DUMPIFIED))
15499277SEric.Taylor@Sun.COM 			break;
15506423Sgw25295 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
15516423Sgw25295 		    RL_WRITER);
15526423Sgw25295 		error = zvol_dump_fini(zv);
15536423Sgw25295 		zfs_range_unlock(rl);
15546423Sgw25295 		break;
15556423Sgw25295 
1556789Sahrens 	default:
15573016Smaybee 		error = ENOTTY;
1558789Sahrens 		break;
1559789Sahrens 
1560789Sahrens 	}
1561789Sahrens 	mutex_exit(&zvol_state_lock);
1562789Sahrens 	return (error);
1563789Sahrens }
1564789Sahrens 
1565789Sahrens int
1566789Sahrens zvol_busy(void)
1567789Sahrens {
1568789Sahrens 	return (zvol_minors != 0);
1569789Sahrens }
1570789Sahrens 
1571789Sahrens void
1572789Sahrens zvol_init(void)
1573789Sahrens {
1574789Sahrens 	VERIFY(ddi_soft_state_init(&zvol_state, sizeof (zvol_state_t), 1) == 0);
1575789Sahrens 	mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
1576789Sahrens }
1577789Sahrens 
1578789Sahrens void
1579789Sahrens zvol_fini(void)
1580789Sahrens {
1581789Sahrens 	mutex_destroy(&zvol_state_lock);
1582789Sahrens 	ddi_soft_state_fini(&zvol_state);
1583789Sahrens }
15846423Sgw25295 
15856423Sgw25295 static int
15866423Sgw25295 zvol_dump_init(zvol_state_t *zv, boolean_t resize)
15876423Sgw25295 {
15886423Sgw25295 	dmu_tx_t *tx;
15896423Sgw25295 	int error = 0;
15906423Sgw25295 	objset_t *os = zv->zv_objset;
15916423Sgw25295 	nvlist_t *nv = NULL;
15926423Sgw25295 
15936423Sgw25295 	ASSERT(MUTEX_HELD(&zvol_state_lock));
159410588SEric.Taylor@Sun.COM 	error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0,
159510588SEric.Taylor@Sun.COM 	    DMU_OBJECT_END);
159610588SEric.Taylor@Sun.COM 	/* wait for dmu_free_long_range to actually free the blocks */
159710588SEric.Taylor@Sun.COM 	txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
15986423Sgw25295 
15996423Sgw25295 	tx = dmu_tx_create(os);
16006423Sgw25295 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
160110588SEric.Taylor@Sun.COM 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
16026423Sgw25295 	error = dmu_tx_assign(tx, TXG_WAIT);
16036423Sgw25295 	if (error) {
16046423Sgw25295 		dmu_tx_abort(tx);
16056423Sgw25295 		return (error);
16066423Sgw25295 	}
16076423Sgw25295 
16086423Sgw25295 	/*
16096423Sgw25295 	 * If we are resizing the dump device then we only need to
16106423Sgw25295 	 * update the refreservation to match the newly updated
16116423Sgw25295 	 * zvolsize. Otherwise, we save off the original state of the
16126423Sgw25295 	 * zvol so that we can restore them if the zvol is ever undumpified.
16136423Sgw25295 	 */
16146423Sgw25295 	if (resize) {
16156423Sgw25295 		error = zap_update(os, ZVOL_ZAP_OBJ,
16166423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
16176423Sgw25295 		    &zv->zv_volsize, tx);
16186423Sgw25295 	} else {
1619*11619SGeorge.Wilson@Sun.COM 		uint64_t checksum, compress, refresrv, vbs, dedup;
16207837SMatthew.Ahrens@Sun.COM 
16216423Sgw25295 		error = dsl_prop_get_integer(zv->zv_name,
16226423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL);
16236423Sgw25295 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
16246423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL);
16256423Sgw25295 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
16266423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL);
16277837SMatthew.Ahrens@Sun.COM 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
16287837SMatthew.Ahrens@Sun.COM 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL);
1629*11619SGeorge.Wilson@Sun.COM 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
1630*11619SGeorge.Wilson@Sun.COM 		    zfs_prop_to_name(ZFS_PROP_DEDUP), &dedup, NULL);
16316423Sgw25295 
16326423Sgw25295 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
16336423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1,
16346423Sgw25295 		    &compress, tx);
16356423Sgw25295 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
16366423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx);
16376423Sgw25295 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
16386423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
16396423Sgw25295 		    &refresrv, tx);
16407837SMatthew.Ahrens@Sun.COM 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
16417837SMatthew.Ahrens@Sun.COM 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1,
16427837SMatthew.Ahrens@Sun.COM 		    &vbs, tx);
164310588SEric.Taylor@Sun.COM 		error = error ? error : dmu_object_set_blocksize(
164410588SEric.Taylor@Sun.COM 		    os, ZVOL_OBJ, SPA_MAXBLOCKSIZE, 0, tx);
1645*11619SGeorge.Wilson@Sun.COM 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1646*11619SGeorge.Wilson@Sun.COM 		    zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1,
1647*11619SGeorge.Wilson@Sun.COM 		    &dedup, tx);
164810588SEric.Taylor@Sun.COM 		if (error == 0)
164910588SEric.Taylor@Sun.COM 			zv->zv_volblocksize = SPA_MAXBLOCKSIZE;
16506423Sgw25295 	}
16516423Sgw25295 	dmu_tx_commit(tx);
16526423Sgw25295 
16536423Sgw25295 	/*
16546423Sgw25295 	 * We only need update the zvol's property if we are initializing
16556423Sgw25295 	 * the dump area for the first time.
16566423Sgw25295 	 */
16576423Sgw25295 	if (!resize) {
16586423Sgw25295 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
16596423Sgw25295 		VERIFY(nvlist_add_uint64(nv,
16606423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0);
16616423Sgw25295 		VERIFY(nvlist_add_uint64(nv,
16626423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
16636423Sgw25295 		    ZIO_COMPRESS_OFF) == 0);
16646423Sgw25295 		VERIFY(nvlist_add_uint64(nv,
16656423Sgw25295 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
16666423Sgw25295 		    ZIO_CHECKSUM_OFF) == 0);
1667*11619SGeorge.Wilson@Sun.COM 		VERIFY(nvlist_add_uint64(nv,
1668*11619SGeorge.Wilson@Sun.COM 		    zfs_prop_to_name(ZFS_PROP_DEDUP),
1669*11619SGeorge.Wilson@Sun.COM 		    ZIO_CHECKSUM_OFF) == 0);
16706423Sgw25295 
167111022STom.Erickson@Sun.COM 		error = zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
167211022STom.Erickson@Sun.COM 		    nv, NULL);
16736423Sgw25295 		nvlist_free(nv);
16746423Sgw25295 
16756423Sgw25295 		if (error)
16766423Sgw25295 			return (error);
16776423Sgw25295 	}
16786423Sgw25295 
16796423Sgw25295 	/* Allocate the space for the dump */
16806423Sgw25295 	error = zvol_prealloc(zv);
16816423Sgw25295 	return (error);
16826423Sgw25295 }
16836423Sgw25295 
16846423Sgw25295 static int
16856423Sgw25295 zvol_dumpify(zvol_state_t *zv)
16866423Sgw25295 {
16876423Sgw25295 	int error = 0;
16886423Sgw25295 	uint64_t dumpsize = 0;
16896423Sgw25295 	dmu_tx_t *tx;
16906423Sgw25295 	objset_t *os = zv->zv_objset;
16916423Sgw25295 
169210588SEric.Taylor@Sun.COM 	if (zv->zv_flags & ZVOL_RDONLY)
16936423Sgw25295 		return (EROFS);
16946423Sgw25295 
16956423Sgw25295 	if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE,
16966423Sgw25295 	    8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) {
16976423Sgw25295 		boolean_t resize = (dumpsize > 0) ? B_TRUE : B_FALSE;
16986423Sgw25295 
16996423Sgw25295 		if ((error = zvol_dump_init(zv, resize)) != 0) {
17006423Sgw25295 			(void) zvol_dump_fini(zv);
17016423Sgw25295 			return (error);
17026423Sgw25295 		}
17036423Sgw25295 	}
17046423Sgw25295 
17056423Sgw25295 	/*
17066423Sgw25295 	 * Build up our lba mapping.
17076423Sgw25295 	 */
17086423Sgw25295 	error = zvol_get_lbas(zv);
17096423Sgw25295 	if (error) {
17106423Sgw25295 		(void) zvol_dump_fini(zv);
17116423Sgw25295 		return (error);
17126423Sgw25295 	}
17136423Sgw25295 
17146423Sgw25295 	tx = dmu_tx_create(os);
17156423Sgw25295 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
17166423Sgw25295 	error = dmu_tx_assign(tx, TXG_WAIT);
17176423Sgw25295 	if (error) {
17186423Sgw25295 		dmu_tx_abort(tx);
17196423Sgw25295 		(void) zvol_dump_fini(zv);
17206423Sgw25295 		return (error);
17216423Sgw25295 	}
17226423Sgw25295 
17236423Sgw25295 	zv->zv_flags |= ZVOL_DUMPIFIED;
17246423Sgw25295 	error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1,
17256423Sgw25295 	    &zv->zv_volsize, tx);
17266423Sgw25295 	dmu_tx_commit(tx);
17276423Sgw25295 
17286423Sgw25295 	if (error) {
17296423Sgw25295 		(void) zvol_dump_fini(zv);
17306423Sgw25295 		return (error);
17316423Sgw25295 	}
17326423Sgw25295 
17336423Sgw25295 	txg_wait_synced(dmu_objset_pool(os), 0);
17346423Sgw25295 	return (0);
17356423Sgw25295 }
17366423Sgw25295 
17376423Sgw25295 static int
17386423Sgw25295 zvol_dump_fini(zvol_state_t *zv)
17396423Sgw25295 {
17406423Sgw25295 	dmu_tx_t *tx;
17416423Sgw25295 	objset_t *os = zv->zv_objset;
17426423Sgw25295 	nvlist_t *nv;
17436423Sgw25295 	int error = 0;
1744*11619SGeorge.Wilson@Sun.COM 	uint64_t checksum, compress, refresrv, vbs, dedup;
17456423Sgw25295 
17467080Smaybee 	/*
17477080Smaybee 	 * Attempt to restore the zvol back to its pre-dumpified state.
17487080Smaybee 	 * This is a best-effort attempt as it's possible that not all
17497080Smaybee 	 * of these properties were initialized during the dumpify process
17507080Smaybee 	 * (i.e. error during zvol_dump_init).
17517080Smaybee 	 */
17527080Smaybee 
17536423Sgw25295 	tx = dmu_tx_create(os);
17546423Sgw25295 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
17556423Sgw25295 	error = dmu_tx_assign(tx, TXG_WAIT);
17566423Sgw25295 	if (error) {
17576423Sgw25295 		dmu_tx_abort(tx);
17586423Sgw25295 		return (error);
17596423Sgw25295 	}
17607080Smaybee 	(void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx);
17617080Smaybee 	dmu_tx_commit(tx);
17626423Sgw25295 
17636423Sgw25295 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
17646423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum);
17656423Sgw25295 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
17666423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress);
17676423Sgw25295 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
17686423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv);
17697837SMatthew.Ahrens@Sun.COM 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
17707837SMatthew.Ahrens@Sun.COM 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs);
1771*11619SGeorge.Wilson@Sun.COM 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1772*11619SGeorge.Wilson@Sun.COM 	    zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, &dedup);
17736423Sgw25295 
17746423Sgw25295 	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
17756423Sgw25295 	(void) nvlist_add_uint64(nv,
17766423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum);
17776423Sgw25295 	(void) nvlist_add_uint64(nv,
17786423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress);
17796423Sgw25295 	(void) nvlist_add_uint64(nv,
17806423Sgw25295 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv);
1781*11619SGeorge.Wilson@Sun.COM 	(void) nvlist_add_uint64(nv,
1782*11619SGeorge.Wilson@Sun.COM 	    zfs_prop_to_name(ZFS_PROP_DEDUP), dedup);
178311022STom.Erickson@Sun.COM 	(void) zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
178411022STom.Erickson@Sun.COM 	    nv, NULL);
17856423Sgw25295 	nvlist_free(nv);
17866423Sgw25295 
17877080Smaybee 	zvol_free_extents(zv);
17887080Smaybee 	zv->zv_flags &= ~ZVOL_DUMPIFIED;
17897080Smaybee 	(void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END);
179010588SEric.Taylor@Sun.COM 	/* wait for dmu_free_long_range to actually free the blocks */
179110588SEric.Taylor@Sun.COM 	txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
179210588SEric.Taylor@Sun.COM 	tx = dmu_tx_create(os);
179310588SEric.Taylor@Sun.COM 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
179410588SEric.Taylor@Sun.COM 	error = dmu_tx_assign(tx, TXG_WAIT);
179510588SEric.Taylor@Sun.COM 	if (error) {
179610588SEric.Taylor@Sun.COM 		dmu_tx_abort(tx);
179710588SEric.Taylor@Sun.COM 		return (error);
179810588SEric.Taylor@Sun.COM 	}
179910922SJeff.Bonwick@Sun.COM 	if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0)
180010922SJeff.Bonwick@Sun.COM 		zv->zv_volblocksize = vbs;
180110588SEric.Taylor@Sun.COM 	dmu_tx_commit(tx);
18027080Smaybee 
18036423Sgw25295 	return (0);
18046423Sgw25295 }
1805