xref: /illumos-gate/usr/src/uts/common/fs/zfs/zvol.c (revision 681d9761e8516a7dc5ab6589e2dfe717777e1123)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
22e08bf2c6SEric Taylor  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens /*
27fa9e4066Sahrens  * ZFS volume emulation driver.
28fa9e4066Sahrens  *
29fa9e4066Sahrens  * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes.
30fa9e4066Sahrens  * Volumes are accessed through the symbolic links named:
31fa9e4066Sahrens  *
32fa9e4066Sahrens  * /dev/zvol/dsk/<pool_name>/<dataset_name>
33fa9e4066Sahrens  * /dev/zvol/rdsk/<pool_name>/<dataset_name>
34fa9e4066Sahrens  *
35*681d9761SEric Taylor  * These links are created by the /dev filesystem (sdev_zvolops.c).
36fa9e4066Sahrens  * Volumes are persistent through reboot.  No user command needs to be
37fa9e4066Sahrens  * run before opening and using a device.
38fa9e4066Sahrens  */
39fa9e4066Sahrens 
40fa9e4066Sahrens #include <sys/types.h>
41fa9e4066Sahrens #include <sys/param.h>
42fa9e4066Sahrens #include <sys/errno.h>
43fa9e4066Sahrens #include <sys/uio.h>
44fa9e4066Sahrens #include <sys/buf.h>
45fa9e4066Sahrens #include <sys/modctl.h>
46fa9e4066Sahrens #include <sys/open.h>
47fa9e4066Sahrens #include <sys/kmem.h>
48fa9e4066Sahrens #include <sys/conf.h>
49fa9e4066Sahrens #include <sys/cmn_err.h>
50fa9e4066Sahrens #include <sys/stat.h>
51fa9e4066Sahrens #include <sys/zap.h>
52fa9e4066Sahrens #include <sys/spa.h>
53fa9e4066Sahrens #include <sys/zio.h>
54e7cbe64fSgw #include <sys/dmu_traverse.h>
55e7cbe64fSgw #include <sys/dnode.h>
56e7cbe64fSgw #include <sys/dsl_dataset.h>
57fa9e4066Sahrens #include <sys/dsl_prop.h>
58fa9e4066Sahrens #include <sys/dkio.h>
59fa9e4066Sahrens #include <sys/efi_partition.h>
60fa9e4066Sahrens #include <sys/byteorder.h>
61fa9e4066Sahrens #include <sys/pathname.h>
62fa9e4066Sahrens #include <sys/ddi.h>
63fa9e4066Sahrens #include <sys/sunddi.h>
64fa9e4066Sahrens #include <sys/crc32.h>
65fa9e4066Sahrens #include <sys/dirent.h>
66fa9e4066Sahrens #include <sys/policy.h>
67fa9e4066Sahrens #include <sys/fs/zfs.h>
68fa9e4066Sahrens #include <sys/zfs_ioctl.h>
69fa9e4066Sahrens #include <sys/mkdev.h>
7022ac5be4Sperrin #include <sys/zil.h>
71c5c6ffa0Smaybee #include <sys/refcount.h>
72c2e6a7d6Sperrin #include <sys/zfs_znode.h>
73c2e6a7d6Sperrin #include <sys/zfs_rlock.h>
74e7cbe64fSgw #include <sys/vdev_disk.h>
75e7cbe64fSgw #include <sys/vdev_impl.h>
76e7cbe64fSgw #include <sys/zvol.h>
77e7cbe64fSgw #include <sys/dumphdr.h>
781209a471SNeil Perrin #include <sys/zil_impl.h>
79fa9e4066Sahrens 
80fa9e4066Sahrens #include "zfs_namecheck.h"
81fa9e4066Sahrens 
82fa9e4066Sahrens static void *zvol_state;
83503ad85cSMatthew Ahrens static char *zvol_tag = "zvol_tag";
84fa9e4066Sahrens 
85e7cbe64fSgw #define	ZVOL_DUMPSIZE		"dumpsize"
86e7cbe64fSgw 
87fa9e4066Sahrens /*
88fa9e4066Sahrens  * This lock protects the zvol_state structure from being modified
89fa9e4066Sahrens  * while it's being used, e.g. an open that comes in before a create
90fa9e4066Sahrens  * finishes.  It also protects temporary opens of the dataset so that,
91fa9e4066Sahrens  * e.g., an open doesn't get a spurious EBUSY.
92fa9e4066Sahrens  */
93fa9e4066Sahrens static kmutex_t zvol_state_lock;
94fa9e4066Sahrens static uint32_t zvol_minors;
95fa9e4066Sahrens 
96e7cbe64fSgw typedef struct zvol_extent {
9788b7b0f2SMatthew Ahrens 	list_node_t	ze_node;
98e7cbe64fSgw 	dva_t		ze_dva;		/* dva associated with this extent */
9988b7b0f2SMatthew Ahrens 	uint64_t	ze_nblks;	/* number of blocks in extent */
100e7cbe64fSgw } zvol_extent_t;
101e7cbe64fSgw 
102fa9e4066Sahrens /*
103fa9e4066Sahrens  * The in-core state of each volume.
104fa9e4066Sahrens  */
105fa9e4066Sahrens typedef struct zvol_state {
106fa9e4066Sahrens 	char		zv_name[MAXPATHLEN]; /* pool/dd name */
107fa9e4066Sahrens 	uint64_t	zv_volsize;	/* amount of space we advertise */
10867bd71c6Sperrin 	uint64_t	zv_volblocksize; /* volume block size */
109fa9e4066Sahrens 	minor_t		zv_minor;	/* minor number */
110fa9e4066Sahrens 	uint8_t		zv_min_bs;	/* minimum addressable block shift */
111701f66c4SEric Taylor 	uint8_t		zv_flags;	/* readonly, dumpified, etc. */
112fa9e4066Sahrens 	objset_t	*zv_objset;	/* objset handle */
113fa9e4066Sahrens 	uint32_t	zv_open_count[OTYPCNT];	/* open counts */
114fa9e4066Sahrens 	uint32_t	zv_total_opens;	/* total open count */
11522ac5be4Sperrin 	zilog_t		*zv_zilog;	/* ZIL handle */
11688b7b0f2SMatthew Ahrens 	list_t		zv_extents;	/* List of extents for dump */
117c2e6a7d6Sperrin 	znode_t		zv_znode;	/* for range locking */
118fa9e4066Sahrens } zvol_state_t;
119fa9e4066Sahrens 
120e7cbe64fSgw /*
121e7cbe64fSgw  * zvol specific flags
122e7cbe64fSgw  */
123e7cbe64fSgw #define	ZVOL_RDONLY	0x1
124e7cbe64fSgw #define	ZVOL_DUMPIFIED	0x2
125c7f714e2SEric Taylor #define	ZVOL_EXCL	0x4
126701f66c4SEric Taylor #define	ZVOL_WCE	0x8
127e7cbe64fSgw 
12867bd71c6Sperrin /*
12967bd71c6Sperrin  * zvol maximum transfer in one DMU tx.
13067bd71c6Sperrin  */
13167bd71c6Sperrin int zvol_maxphys = DMU_MAX_ACCESS/2;
13267bd71c6Sperrin 
133*681d9761SEric Taylor static int zvol_remove_zv(zvol_state_t *);
134e7cbe64fSgw extern int zfs_set_prop_nvlist(const char *, nvlist_t *);
135feb08c6bSbillm static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio);
136e7cbe64fSgw static int zvol_dumpify(zvol_state_t *zv);
137e7cbe64fSgw static int zvol_dump_fini(zvol_state_t *zv);
138e7cbe64fSgw static int zvol_dump_init(zvol_state_t *zv, boolean_t resize);
13967bd71c6Sperrin 
140fa9e4066Sahrens static void
141*681d9761SEric Taylor zvol_size_changed(uint64_t volsize, major_t maj, minor_t min)
142fa9e4066Sahrens {
143*681d9761SEric Taylor 	dev_t dev = makedevice(maj, min);
144fa9e4066Sahrens 
145fa9e4066Sahrens 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
146*681d9761SEric Taylor 	    "Size", volsize) == DDI_SUCCESS);
147fa9e4066Sahrens 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
148*681d9761SEric Taylor 	    "Nblocks", lbtodb(volsize)) == DDI_SUCCESS);
149e7cbe64fSgw 
150e7cbe64fSgw 	/* Notify specfs to invalidate the cached size */
151e7cbe64fSgw 	spec_size_invalidate(dev, VBLK);
152e7cbe64fSgw 	spec_size_invalidate(dev, VCHR);
153fa9e4066Sahrens }
154fa9e4066Sahrens 
155fa9e4066Sahrens int
156e9dbad6fSeschrock zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
157fa9e4066Sahrens {
158e9dbad6fSeschrock 	if (volsize == 0)
159fa9e4066Sahrens 		return (EINVAL);
160fa9e4066Sahrens 
161e9dbad6fSeschrock 	if (volsize % blocksize != 0)
1625c5460e9Seschrock 		return (EINVAL);
1635c5460e9Seschrock 
164fa9e4066Sahrens #ifdef _ILP32
165e9dbad6fSeschrock 	if (volsize - 1 > SPEC_MAXOFFSET_T)
166fa9e4066Sahrens 		return (EOVERFLOW);
167fa9e4066Sahrens #endif
168fa9e4066Sahrens 	return (0);
169fa9e4066Sahrens }
170fa9e4066Sahrens 
171fa9e4066Sahrens int
172e9dbad6fSeschrock zvol_check_volblocksize(uint64_t volblocksize)
173fa9e4066Sahrens {
174e9dbad6fSeschrock 	if (volblocksize < SPA_MINBLOCKSIZE ||
175e9dbad6fSeschrock 	    volblocksize > SPA_MAXBLOCKSIZE ||
176e9dbad6fSeschrock 	    !ISP2(volblocksize))
177fa9e4066Sahrens 		return (EDOM);
178fa9e4066Sahrens 
179fa9e4066Sahrens 	return (0);
180fa9e4066Sahrens }
181fa9e4066Sahrens 
182fa9e4066Sahrens int
183a2eea2e1Sahrens zvol_get_stats(objset_t *os, nvlist_t *nv)
184fa9e4066Sahrens {
185fa9e4066Sahrens 	int error;
186fa9e4066Sahrens 	dmu_object_info_t doi;
187a2eea2e1Sahrens 	uint64_t val;
188fa9e4066Sahrens 
189a2eea2e1Sahrens 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
190fa9e4066Sahrens 	if (error)
191fa9e4066Sahrens 		return (error);
192fa9e4066Sahrens 
193a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
194a2eea2e1Sahrens 
195fa9e4066Sahrens 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
196fa9e4066Sahrens 
197a2eea2e1Sahrens 	if (error == 0) {
198a2eea2e1Sahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
199a2eea2e1Sahrens 		    doi.doi_data_block_size);
200a2eea2e1Sahrens 	}
201fa9e4066Sahrens 
202fa9e4066Sahrens 	return (error);
203fa9e4066Sahrens }
204fa9e4066Sahrens 
205fa9e4066Sahrens /*
206fa9e4066Sahrens  * Find a free minor number.
207fa9e4066Sahrens  */
208fa9e4066Sahrens static minor_t
209fa9e4066Sahrens zvol_minor_alloc(void)
210fa9e4066Sahrens {
211fa9e4066Sahrens 	minor_t minor;
212fa9e4066Sahrens 
213fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&zvol_state_lock));
214fa9e4066Sahrens 
215fa9e4066Sahrens 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++)
216fa9e4066Sahrens 		if (ddi_get_soft_state(zvol_state, minor) == NULL)
217fa9e4066Sahrens 			return (minor);
218fa9e4066Sahrens 
219fa9e4066Sahrens 	return (0);
220fa9e4066Sahrens }
221fa9e4066Sahrens 
222fa9e4066Sahrens static zvol_state_t *
223e9dbad6fSeschrock zvol_minor_lookup(const char *name)
224fa9e4066Sahrens {
225fa9e4066Sahrens 	minor_t minor;
226fa9e4066Sahrens 	zvol_state_t *zv;
227fa9e4066Sahrens 
228fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&zvol_state_lock));
229fa9e4066Sahrens 
230fa9e4066Sahrens 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) {
231fa9e4066Sahrens 		zv = ddi_get_soft_state(zvol_state, minor);
232fa9e4066Sahrens 		if (zv == NULL)
233fa9e4066Sahrens 			continue;
234fa9e4066Sahrens 		if (strcmp(zv->zv_name, name) == 0)
235fa9e4066Sahrens 			break;
236fa9e4066Sahrens 	}
237fa9e4066Sahrens 
238fa9e4066Sahrens 	return (zv);
239fa9e4066Sahrens }
240fa9e4066Sahrens 
241e7cbe64fSgw /* extent mapping arg */
242e7cbe64fSgw struct maparg {
24388b7b0f2SMatthew Ahrens 	zvol_state_t	*ma_zv;
24488b7b0f2SMatthew Ahrens 	uint64_t	ma_blks;
245e7cbe64fSgw };
246e7cbe64fSgw 
247e7cbe64fSgw /*ARGSUSED*/
248e7cbe64fSgw static int
24988b7b0f2SMatthew Ahrens zvol_map_block(spa_t *spa, blkptr_t *bp, const zbookmark_t *zb,
25088b7b0f2SMatthew Ahrens     const dnode_phys_t *dnp, void *arg)
251e7cbe64fSgw {
25288b7b0f2SMatthew Ahrens 	struct maparg *ma = arg;
25388b7b0f2SMatthew Ahrens 	zvol_extent_t *ze;
25488b7b0f2SMatthew Ahrens 	int bs = ma->ma_zv->zv_volblocksize;
255e7cbe64fSgw 
25688b7b0f2SMatthew Ahrens 	if (bp == NULL || zb->zb_object != ZVOL_OBJ || zb->zb_level != 0)
25788b7b0f2SMatthew Ahrens 		return (0);
258e7cbe64fSgw 
25988b7b0f2SMatthew Ahrens 	VERIFY3U(ma->ma_blks, ==, zb->zb_blkid);
26088b7b0f2SMatthew Ahrens 	ma->ma_blks++;
261e7cbe64fSgw 
26288b7b0f2SMatthew Ahrens 	/* Abort immediately if we have encountered gang blocks */
26388b7b0f2SMatthew Ahrens 	if (BP_IS_GANG(bp))
26488b7b0f2SMatthew Ahrens 		return (EFRAGS);
265e7cbe64fSgw 
26688b7b0f2SMatthew Ahrens 	/*
26788b7b0f2SMatthew Ahrens 	 * See if the block is at the end of the previous extent.
26888b7b0f2SMatthew Ahrens 	 */
26988b7b0f2SMatthew Ahrens 	ze = list_tail(&ma->ma_zv->zv_extents);
27088b7b0f2SMatthew Ahrens 	if (ze &&
27188b7b0f2SMatthew Ahrens 	    DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) &&
27288b7b0f2SMatthew Ahrens 	    DVA_GET_OFFSET(BP_IDENTITY(bp)) ==
27388b7b0f2SMatthew Ahrens 	    DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) {
27488b7b0f2SMatthew Ahrens 		ze->ze_nblks++;
27588b7b0f2SMatthew Ahrens 		return (0);
276e7cbe64fSgw 	}
277e7cbe64fSgw 
27888b7b0f2SMatthew Ahrens 	dprintf_bp(bp, "%s", "next blkptr:");
279e7cbe64fSgw 
28088b7b0f2SMatthew Ahrens 	/* start a new extent */
28188b7b0f2SMatthew Ahrens 	ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP);
28288b7b0f2SMatthew Ahrens 	ze->ze_dva = bp->blk_dva[0];	/* structure assignment */
28388b7b0f2SMatthew Ahrens 	ze->ze_nblks = 1;
28488b7b0f2SMatthew Ahrens 	list_insert_tail(&ma->ma_zv->zv_extents, ze);
28588b7b0f2SMatthew Ahrens 	return (0);
28688b7b0f2SMatthew Ahrens }
287e7cbe64fSgw 
28888b7b0f2SMatthew Ahrens static void
28988b7b0f2SMatthew Ahrens zvol_free_extents(zvol_state_t *zv)
29088b7b0f2SMatthew Ahrens {
29188b7b0f2SMatthew Ahrens 	zvol_extent_t *ze;
292e7cbe64fSgw 
29388b7b0f2SMatthew Ahrens 	while (ze = list_head(&zv->zv_extents)) {
29488b7b0f2SMatthew Ahrens 		list_remove(&zv->zv_extents, ze);
29588b7b0f2SMatthew Ahrens 		kmem_free(ze, sizeof (zvol_extent_t));
296e7cbe64fSgw 	}
29788b7b0f2SMatthew Ahrens }
298e7cbe64fSgw 
29988b7b0f2SMatthew Ahrens static int
30088b7b0f2SMatthew Ahrens zvol_get_lbas(zvol_state_t *zv)
30188b7b0f2SMatthew Ahrens {
30288b7b0f2SMatthew Ahrens 	struct maparg	ma;
30388b7b0f2SMatthew Ahrens 	int		err;
30488b7b0f2SMatthew Ahrens 
30588b7b0f2SMatthew Ahrens 	ma.ma_zv = zv;
30688b7b0f2SMatthew Ahrens 	ma.ma_blks = 0;
30788b7b0f2SMatthew Ahrens 	zvol_free_extents(zv);
30888b7b0f2SMatthew Ahrens 
30988b7b0f2SMatthew Ahrens 	err = traverse_dataset(dmu_objset_ds(zv->zv_objset), 0,
31088b7b0f2SMatthew Ahrens 	    TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma);
31188b7b0f2SMatthew Ahrens 	if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) {
31288b7b0f2SMatthew Ahrens 		zvol_free_extents(zv);
31388b7b0f2SMatthew Ahrens 		return (err ? err : EIO);
314e7cbe64fSgw 	}
31588b7b0f2SMatthew Ahrens 
316e7cbe64fSgw 	return (0);
317e7cbe64fSgw }
318e7cbe64fSgw 
319ecd6cf80Smarks /* ARGSUSED */
320fa9e4066Sahrens void
321ecd6cf80Smarks zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
322fa9e4066Sahrens {
323da6c28aaSamw 	zfs_creat_t *zct = arg;
324da6c28aaSamw 	nvlist_t *nvprops = zct->zct_props;
325fa9e4066Sahrens 	int error;
326e9dbad6fSeschrock 	uint64_t volblocksize, volsize;
327fa9e4066Sahrens 
328ecd6cf80Smarks 	VERIFY(nvlist_lookup_uint64(nvprops,
329e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
330ecd6cf80Smarks 	if (nvlist_lookup_uint64(nvprops,
331e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
332e9dbad6fSeschrock 		volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
333e9dbad6fSeschrock 
334e9dbad6fSeschrock 	/*
335e7cbe64fSgw 	 * These properties must be removed from the list so the generic
336e9dbad6fSeschrock 	 * property setting step won't apply to them.
337e9dbad6fSeschrock 	 */
338ecd6cf80Smarks 	VERIFY(nvlist_remove_all(nvprops,
339e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
340ecd6cf80Smarks 	(void) nvlist_remove_all(nvprops,
341e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
342e9dbad6fSeschrock 
343e9dbad6fSeschrock 	error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
344fa9e4066Sahrens 	    DMU_OT_NONE, 0, tx);
345fa9e4066Sahrens 	ASSERT(error == 0);
346fa9e4066Sahrens 
347fa9e4066Sahrens 	error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
348fa9e4066Sahrens 	    DMU_OT_NONE, 0, tx);
349fa9e4066Sahrens 	ASSERT(error == 0);
350fa9e4066Sahrens 
351e9dbad6fSeschrock 	error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
352fa9e4066Sahrens 	ASSERT(error == 0);
353fa9e4066Sahrens }
354fa9e4066Sahrens 
35522ac5be4Sperrin /*
35622ac5be4Sperrin  * Replay a TX_WRITE ZIL transaction that didn't get committed
35722ac5be4Sperrin  * after a system failure
35822ac5be4Sperrin  */
35922ac5be4Sperrin static int
36022ac5be4Sperrin zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
36122ac5be4Sperrin {
36222ac5be4Sperrin 	objset_t *os = zv->zv_objset;
36322ac5be4Sperrin 	char *data = (char *)(lr + 1);	/* data follows lr_write_t */
36422ac5be4Sperrin 	uint64_t off = lr->lr_offset;
36522ac5be4Sperrin 	uint64_t len = lr->lr_length;
36622ac5be4Sperrin 	dmu_tx_t *tx;
36722ac5be4Sperrin 	int error;
36822ac5be4Sperrin 
36922ac5be4Sperrin 	if (byteswap)
37022ac5be4Sperrin 		byteswap_uint64_array(lr, sizeof (*lr));
37122ac5be4Sperrin 
37222ac5be4Sperrin 	tx = dmu_tx_create(os);
37322ac5be4Sperrin 	dmu_tx_hold_write(tx, ZVOL_OBJ, off, len);
3741209a471SNeil Perrin 	error = dmu_tx_assign(tx, TXG_WAIT);
37522ac5be4Sperrin 	if (error) {
37622ac5be4Sperrin 		dmu_tx_abort(tx);
37722ac5be4Sperrin 	} else {
37822ac5be4Sperrin 		dmu_write(os, ZVOL_OBJ, off, len, data, tx);
37922ac5be4Sperrin 		dmu_tx_commit(tx);
38022ac5be4Sperrin 	}
38122ac5be4Sperrin 
38222ac5be4Sperrin 	return (error);
38322ac5be4Sperrin }
38422ac5be4Sperrin 
38522ac5be4Sperrin /* ARGSUSED */
38622ac5be4Sperrin static int
38722ac5be4Sperrin zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
38822ac5be4Sperrin {
38922ac5be4Sperrin 	return (ENOTSUP);
39022ac5be4Sperrin }
39122ac5be4Sperrin 
39222ac5be4Sperrin /*
39322ac5be4Sperrin  * Callback vectors for replaying records.
39422ac5be4Sperrin  * Only TX_WRITE is needed for zvol.
39522ac5be4Sperrin  */
39622ac5be4Sperrin zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = {
39722ac5be4Sperrin 	zvol_replay_err,	/* 0 no such transaction type */
39822ac5be4Sperrin 	zvol_replay_err,	/* TX_CREATE */
39922ac5be4Sperrin 	zvol_replay_err,	/* TX_MKDIR */
40022ac5be4Sperrin 	zvol_replay_err,	/* TX_MKXATTR */
40122ac5be4Sperrin 	zvol_replay_err,	/* TX_SYMLINK */
40222ac5be4Sperrin 	zvol_replay_err,	/* TX_REMOVE */
40322ac5be4Sperrin 	zvol_replay_err,	/* TX_RMDIR */
40422ac5be4Sperrin 	zvol_replay_err,	/* TX_LINK */
40522ac5be4Sperrin 	zvol_replay_err,	/* TX_RENAME */
40622ac5be4Sperrin 	zvol_replay_write,	/* TX_WRITE */
40722ac5be4Sperrin 	zvol_replay_err,	/* TX_TRUNCATE */
40822ac5be4Sperrin 	zvol_replay_err,	/* TX_SETATTR */
40922ac5be4Sperrin 	zvol_replay_err,	/* TX_ACL */
41022ac5be4Sperrin };
41122ac5be4Sperrin 
412*681d9761SEric Taylor int
413*681d9761SEric Taylor zvol_name2minor(const char *name, minor_t *minor)
414*681d9761SEric Taylor {
415*681d9761SEric Taylor 	zvol_state_t *zv;
416*681d9761SEric Taylor 
417*681d9761SEric Taylor 	mutex_enter(&zvol_state_lock);
418*681d9761SEric Taylor 	zv = zvol_minor_lookup(name);
419*681d9761SEric Taylor 	if (minor && zv)
420*681d9761SEric Taylor 		*minor = zv->zv_minor;
421*681d9761SEric Taylor 	mutex_exit(&zvol_state_lock);
422*681d9761SEric Taylor 	return (zv ? 0 : -1);
423*681d9761SEric Taylor }
424*681d9761SEric Taylor 
425e7cbe64fSgw /*
426e7cbe64fSgw  * Create a minor node (plus a whole lot more) for the specified volume.
427fa9e4066Sahrens  */
428fa9e4066Sahrens int
429*681d9761SEric Taylor zvol_create_minor(const char *name)
430fa9e4066Sahrens {
431fa9e4066Sahrens 	zvol_state_t *zv;
432fa9e4066Sahrens 	objset_t *os;
43367bd71c6Sperrin 	dmu_object_info_t doi;
434fa9e4066Sahrens 	minor_t minor = 0;
435fa9e4066Sahrens 	char chrbuf[30], blkbuf[30];
436fa9e4066Sahrens 	int error;
437fa9e4066Sahrens 
438fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
439fa9e4066Sahrens 
440fa9e4066Sahrens 	if ((zv = zvol_minor_lookup(name)) != NULL) {
441fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
442fa9e4066Sahrens 		return (EEXIST);
443fa9e4066Sahrens 	}
444fa9e4066Sahrens 
445503ad85cSMatthew Ahrens 	/* lie and say we're read-only */
446503ad85cSMatthew Ahrens 	error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, zvol_tag, &os);
447fa9e4066Sahrens 
448fa9e4066Sahrens 	if (error) {
449fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
450fa9e4066Sahrens 		return (error);
451fa9e4066Sahrens 	}
452fa9e4066Sahrens 
453*681d9761SEric Taylor 	if ((minor = zvol_minor_alloc()) == 0) {
454503ad85cSMatthew Ahrens 		dmu_objset_disown(os, zvol_tag);
455fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
456fa9e4066Sahrens 		return (ENXIO);
457fa9e4066Sahrens 	}
458fa9e4066Sahrens 
459fa9e4066Sahrens 	if (ddi_soft_state_zalloc(zvol_state, minor) != DDI_SUCCESS) {
460503ad85cSMatthew Ahrens 		dmu_objset_disown(os, zvol_tag);
461fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
462fa9e4066Sahrens 		return (EAGAIN);
463fa9e4066Sahrens 	}
464e9dbad6fSeschrock 	(void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME,
465e9dbad6fSeschrock 	    (char *)name);
466fa9e4066Sahrens 
467*681d9761SEric Taylor 	(void) snprintf(chrbuf, sizeof (chrbuf), "%u,raw", minor);
468fa9e4066Sahrens 
469fa9e4066Sahrens 	if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR,
470fa9e4066Sahrens 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
471fa9e4066Sahrens 		ddi_soft_state_free(zvol_state, minor);
472503ad85cSMatthew Ahrens 		dmu_objset_disown(os, zvol_tag);
473fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
474fa9e4066Sahrens 		return (EAGAIN);
475fa9e4066Sahrens 	}
476fa9e4066Sahrens 
477*681d9761SEric Taylor 	(void) snprintf(blkbuf, sizeof (blkbuf), "%u", minor);
478fa9e4066Sahrens 
479fa9e4066Sahrens 	if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK,
480fa9e4066Sahrens 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
481fa9e4066Sahrens 		ddi_remove_minor_node(zfs_dip, chrbuf);
482fa9e4066Sahrens 		ddi_soft_state_free(zvol_state, minor);
483503ad85cSMatthew Ahrens 		dmu_objset_disown(os, zvol_tag);
484fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
485fa9e4066Sahrens 		return (EAGAIN);
486fa9e4066Sahrens 	}
487fa9e4066Sahrens 
488fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
489fa9e4066Sahrens 
490*681d9761SEric Taylor 	(void) strlcpy(zv->zv_name, name, MAXPATHLEN);
491fa9e4066Sahrens 	zv->zv_min_bs = DEV_BSHIFT;
492fa9e4066Sahrens 	zv->zv_minor = minor;
493fa9e4066Sahrens 	zv->zv_objset = os;
494*681d9761SEric Taylor 	if (dmu_objset_is_snapshot(os))
495*681d9761SEric Taylor 		zv->zv_flags |= ZVOL_RDONLY;
496c2e6a7d6Sperrin 	mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
497c2e6a7d6Sperrin 	avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
498c2e6a7d6Sperrin 	    sizeof (rl_t), offsetof(rl_t, r_node));
49988b7b0f2SMatthew Ahrens 	list_create(&zv->zv_extents, sizeof (zvol_extent_t),
50088b7b0f2SMatthew Ahrens 	    offsetof(zvol_extent_t, ze_node));
50167bd71c6Sperrin 	/* get and cache the blocksize */
50267bd71c6Sperrin 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
50367bd71c6Sperrin 	ASSERT(error == 0);
50467bd71c6Sperrin 	zv->zv_volblocksize = doi.doi_data_block_size;
50522ac5be4Sperrin 
5061209a471SNeil Perrin 	zil_replay(os, zv, zvol_replay_vector);
507*681d9761SEric Taylor 	dmu_objset_disown(os, zvol_tag);
508*681d9761SEric Taylor 	zv->zv_objset = NULL;
509fa9e4066Sahrens 
510fa9e4066Sahrens 	zvol_minors++;
511fa9e4066Sahrens 
512fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
513fa9e4066Sahrens 
514fa9e4066Sahrens 	return (0);
515fa9e4066Sahrens }
516fa9e4066Sahrens 
517fa9e4066Sahrens /*
518fa9e4066Sahrens  * Remove minor node for the specified volume.
519fa9e4066Sahrens  */
520*681d9761SEric Taylor static int
521*681d9761SEric Taylor zvol_remove_zv(zvol_state_t *zv)
522*681d9761SEric Taylor {
523*681d9761SEric Taylor 	char nmbuf[20];
524*681d9761SEric Taylor 
525*681d9761SEric Taylor 	ASSERT(MUTEX_HELD(&zvol_state_lock));
526*681d9761SEric Taylor 	if (zv->zv_total_opens != 0)
527*681d9761SEric Taylor 		return (EBUSY);
528*681d9761SEric Taylor 
529*681d9761SEric Taylor 	(void) snprintf(nmbuf, sizeof (nmbuf), "%u,raw", zv->zv_minor);
530*681d9761SEric Taylor 	ddi_remove_minor_node(zfs_dip, nmbuf);
531*681d9761SEric Taylor 
532*681d9761SEric Taylor 	(void) snprintf(nmbuf, sizeof (nmbuf), "%u", zv->zv_minor);
533*681d9761SEric Taylor 	ddi_remove_minor_node(zfs_dip, nmbuf);
534*681d9761SEric Taylor 
535*681d9761SEric Taylor 	avl_destroy(&zv->zv_znode.z_range_avl);
536*681d9761SEric Taylor 	mutex_destroy(&zv->zv_znode.z_range_lock);
537*681d9761SEric Taylor 
538*681d9761SEric Taylor 	ddi_soft_state_free(zvol_state, zv->zv_minor);
539*681d9761SEric Taylor 
540*681d9761SEric Taylor 	zvol_minors--;
541*681d9761SEric Taylor 	return (0);
542*681d9761SEric Taylor }
543*681d9761SEric Taylor 
544fa9e4066Sahrens int
545e9dbad6fSeschrock zvol_remove_minor(const char *name)
546fa9e4066Sahrens {
547fa9e4066Sahrens 	zvol_state_t *zv;
548*681d9761SEric Taylor 	int rc;
549fa9e4066Sahrens 
550fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
551e9dbad6fSeschrock 	if ((zv = zvol_minor_lookup(name)) == NULL) {
552fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
553fa9e4066Sahrens 		return (ENXIO);
554fa9e4066Sahrens 	}
555*681d9761SEric Taylor 	rc = zvol_remove_zv(zv);
556*681d9761SEric Taylor 	mutex_exit(&zvol_state_lock);
557*681d9761SEric Taylor 	return (rc);
558*681d9761SEric Taylor }
559fa9e4066Sahrens 
560*681d9761SEric Taylor int
561*681d9761SEric Taylor zvol_first_open(zvol_state_t *zv)
562*681d9761SEric Taylor {
563*681d9761SEric Taylor 	objset_t *os;
564*681d9761SEric Taylor 	uint64_t volsize;
565*681d9761SEric Taylor 	int error;
566*681d9761SEric Taylor 	uint64_t readonly;
567fa9e4066Sahrens 
568*681d9761SEric Taylor 	/* lie and say we're read-only */
569*681d9761SEric Taylor 	error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, B_TRUE,
570*681d9761SEric Taylor 	    zvol_tag, &os);
571*681d9761SEric Taylor 	if (error)
572*681d9761SEric Taylor 		return (error);
573fa9e4066Sahrens 
574*681d9761SEric Taylor 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
575*681d9761SEric Taylor 	if (error) {
576*681d9761SEric Taylor 		ASSERT(error == 0);
577*681d9761SEric Taylor 		dmu_objset_disown(os, zvol_tag);
578*681d9761SEric Taylor 		return (error);
579*681d9761SEric Taylor 	}
580*681d9761SEric Taylor 	zv->zv_objset = os;
581*681d9761SEric Taylor 	zv->zv_volsize = volsize;
582*681d9761SEric Taylor 	zv->zv_zilog = zil_open(os, zvol_get_data);
583*681d9761SEric Taylor 	zvol_size_changed(zv->zv_volsize, ddi_driver_major(zfs_dip),
584*681d9761SEric Taylor 	    zv->zv_minor);
585fa9e4066Sahrens 
586*681d9761SEric Taylor 	VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &readonly,
587*681d9761SEric Taylor 	    NULL) == 0);
588*681d9761SEric Taylor 	if (readonly || dmu_objset_is_snapshot(os))
589*681d9761SEric Taylor 		zv->zv_flags |= ZVOL_RDONLY;
590*681d9761SEric Taylor 	else
591*681d9761SEric Taylor 		zv->zv_flags &= ~ZVOL_RDONLY;
592*681d9761SEric Taylor 	return (error);
593*681d9761SEric Taylor }
594fa9e4066Sahrens 
595*681d9761SEric Taylor void
596*681d9761SEric Taylor zvol_last_close(zvol_state_t *zv)
597*681d9761SEric Taylor {
59822ac5be4Sperrin 	zil_close(zv->zv_zilog);
59922ac5be4Sperrin 	zv->zv_zilog = NULL;
600503ad85cSMatthew Ahrens 	dmu_objset_disown(zv->zv_objset, zvol_tag);
601fa9e4066Sahrens 	zv->zv_objset = NULL;
602fa9e4066Sahrens }
603fa9e4066Sahrens 
604e7cbe64fSgw int
605e7cbe64fSgw zvol_prealloc(zvol_state_t *zv)
606e7cbe64fSgw {
607e7cbe64fSgw 	objset_t *os = zv->zv_objset;
608e7cbe64fSgw 	dmu_tx_t *tx;
609e7cbe64fSgw 	uint64_t refd, avail, usedobjs, availobjs;
610e7cbe64fSgw 	uint64_t resid = zv->zv_volsize;
611e7cbe64fSgw 	uint64_t off = 0;
612e7cbe64fSgw 
613e7cbe64fSgw 	/* Check the space usage before attempting to allocate the space */
614e7cbe64fSgw 	dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs);
615e7cbe64fSgw 	if (avail < zv->zv_volsize)
616e7cbe64fSgw 		return (ENOSPC);
617e7cbe64fSgw 
618e7cbe64fSgw 	/* Free old extents if they exist */
619e7cbe64fSgw 	zvol_free_extents(zv);
620e7cbe64fSgw 
621e7cbe64fSgw 	while (resid != 0) {
622e7cbe64fSgw 		int error;
623e7cbe64fSgw 		uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE);
624e7cbe64fSgw 
625e7cbe64fSgw 		tx = dmu_tx_create(os);
626e7cbe64fSgw 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
627e7cbe64fSgw 		error = dmu_tx_assign(tx, TXG_WAIT);
628e7cbe64fSgw 		if (error) {
629e7cbe64fSgw 			dmu_tx_abort(tx);
630cdb0ab79Smaybee 			(void) dmu_free_long_range(os, ZVOL_OBJ, 0, off);
631e7cbe64fSgw 			return (error);
632e7cbe64fSgw 		}
63382c9918fSTim Haley 		dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx);
634e7cbe64fSgw 		dmu_tx_commit(tx);
635e7cbe64fSgw 		off += bytes;
636e7cbe64fSgw 		resid -= bytes;
637e7cbe64fSgw 	}
638e7cbe64fSgw 	txg_wait_synced(dmu_objset_pool(os), 0);
639e7cbe64fSgw 
640e7cbe64fSgw 	return (0);
641e7cbe64fSgw }
642e7cbe64fSgw 
643e7cbe64fSgw int
644*681d9761SEric Taylor zvol_update_volsize(objset_t *os, uint64_t volsize)
645e7cbe64fSgw {
646e7cbe64fSgw 	dmu_tx_t *tx;
647e7cbe64fSgw 	int error;
648e7cbe64fSgw 
649e7cbe64fSgw 	ASSERT(MUTEX_HELD(&zvol_state_lock));
650e7cbe64fSgw 
651*681d9761SEric Taylor 	tx = dmu_tx_create(os);
652e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
653e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
654e7cbe64fSgw 	if (error) {
655e7cbe64fSgw 		dmu_tx_abort(tx);
656e7cbe64fSgw 		return (error);
657e7cbe64fSgw 	}
658e7cbe64fSgw 
659*681d9761SEric Taylor 	error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
660e7cbe64fSgw 	    &volsize, tx);
661e7cbe64fSgw 	dmu_tx_commit(tx);
662e7cbe64fSgw 
663e7cbe64fSgw 	if (error == 0)
664*681d9761SEric Taylor 		error = dmu_free_long_range(os,
665cdb0ab79Smaybee 		    ZVOL_OBJ, volsize, DMU_OBJECT_END);
666*681d9761SEric Taylor 	return (error);
667*681d9761SEric Taylor }
668e7cbe64fSgw 
669*681d9761SEric Taylor void
670*681d9761SEric Taylor zvol_remove_minors(const char *name)
671*681d9761SEric Taylor {
672*681d9761SEric Taylor 	zvol_state_t *zv;
673*681d9761SEric Taylor 	char *namebuf;
674*681d9761SEric Taylor 	minor_t minor;
675*681d9761SEric Taylor 
676*681d9761SEric Taylor 	namebuf = kmem_zalloc(strlen(name) + 2, KM_SLEEP);
677*681d9761SEric Taylor 	(void) strncpy(namebuf, name, strlen(name));
678*681d9761SEric Taylor 	(void) strcat(namebuf, "/");
679*681d9761SEric Taylor 	mutex_enter(&zvol_state_lock);
680*681d9761SEric Taylor 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) {
681*681d9761SEric Taylor 
682*681d9761SEric Taylor 		zv = ddi_get_soft_state(zvol_state, minor);
683*681d9761SEric Taylor 		if (zv == NULL)
684*681d9761SEric Taylor 			continue;
685*681d9761SEric Taylor 		if (strncmp(namebuf, zv->zv_name, strlen(namebuf)) == 0)
686*681d9761SEric Taylor 			(void) zvol_remove_zv(zv);
687e7cbe64fSgw 	}
688*681d9761SEric Taylor 	kmem_free(namebuf, strlen(name) + 2);
689*681d9761SEric Taylor 
690*681d9761SEric Taylor 	mutex_exit(&zvol_state_lock);
691e7cbe64fSgw }
692e7cbe64fSgw 
693fa9e4066Sahrens int
69491ebeef5Sahrens zvol_set_volsize(const char *name, major_t maj, uint64_t volsize)
695fa9e4066Sahrens {
696*681d9761SEric Taylor 	zvol_state_t *zv = NULL;
697*681d9761SEric Taylor 	objset_t *os;
698fa9e4066Sahrens 	int error;
6995c5460e9Seschrock 	dmu_object_info_t doi;
700e7cbe64fSgw 	uint64_t old_volsize = 0ULL;
701*681d9761SEric Taylor 	uint64_t readonly;
702fa9e4066Sahrens 
703fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
704*681d9761SEric Taylor 	zv = zvol_minor_lookup(name);
705*681d9761SEric Taylor 	if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) {
706*681d9761SEric Taylor 		mutex_exit(&zvol_state_lock);
707*681d9761SEric Taylor 		return (error);
708fa9e4066Sahrens 	}
709fa9e4066Sahrens 
710*681d9761SEric Taylor 	if ((error = dmu_object_info(os, ZVOL_OBJ, &doi)) != 0 ||
711e9dbad6fSeschrock 	    (error = zvol_check_volsize(volsize,
712bb0ade09Sahrens 	    doi.doi_data_block_size)) != 0)
713bb0ade09Sahrens 		goto out;
7145c5460e9Seschrock 
715*681d9761SEric Taylor 	VERIFY(dsl_prop_get_integer(name, "readonly", &readonly,
716*681d9761SEric Taylor 	    NULL) == 0);
717*681d9761SEric Taylor 	if (readonly) {
718bb0ade09Sahrens 		error = EROFS;
719bb0ade09Sahrens 		goto out;
720fa9e4066Sahrens 	}
721fa9e4066Sahrens 
722*681d9761SEric Taylor 	error = zvol_update_volsize(os, volsize);
723e7cbe64fSgw 	/*
724e7cbe64fSgw 	 * Reinitialize the dump area to the new size. If we
725*681d9761SEric Taylor 	 * failed to resize the dump area then restore it back to
726*681d9761SEric Taylor 	 * its original size.
727e7cbe64fSgw 	 */
728*681d9761SEric Taylor 	if (zv && error == 0) {
729*681d9761SEric Taylor 		if (zv->zv_flags & ZVOL_DUMPIFIED) {
730*681d9761SEric Taylor 			old_volsize = zv->zv_volsize;
731*681d9761SEric Taylor 			zv->zv_volsize = volsize;
732*681d9761SEric Taylor 			if ((error = zvol_dumpify(zv)) != 0 ||
733*681d9761SEric Taylor 			    (error = dumpvp_resize()) != 0) {
734*681d9761SEric Taylor 				(void) zvol_update_volsize(os, old_volsize);
735*681d9761SEric Taylor 				zv->zv_volsize = old_volsize;
736*681d9761SEric Taylor 				error = zvol_dumpify(zv);
737*681d9761SEric Taylor 			}
738*681d9761SEric Taylor 		}
739*681d9761SEric Taylor 		if (error == 0) {
740*681d9761SEric Taylor 			zv->zv_volsize = volsize;
741*681d9761SEric Taylor 			zvol_size_changed(volsize, maj, zv->zv_minor);
742e7cbe64fSgw 		}
743fa9e4066Sahrens 	}
744fa9e4066Sahrens 
745573ca77eSGeorge Wilson 	/*
746573ca77eSGeorge Wilson 	 * Generate a LUN expansion event.
747573ca77eSGeorge Wilson 	 */
748*681d9761SEric Taylor 	if (zv && error == 0) {
749573ca77eSGeorge Wilson 		sysevent_id_t eid;
750573ca77eSGeorge Wilson 		nvlist_t *attr;
751573ca77eSGeorge Wilson 		char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
752573ca77eSGeorge Wilson 
753*681d9761SEric Taylor 		(void) snprintf(physpath, MAXPATHLEN, "%s%u", ZVOL_PSEUDO_DEV,
754573ca77eSGeorge Wilson 		    zv->zv_minor);
755573ca77eSGeorge Wilson 
756573ca77eSGeorge Wilson 		VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
757573ca77eSGeorge Wilson 		VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
758573ca77eSGeorge Wilson 
759573ca77eSGeorge Wilson 		(void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
760573ca77eSGeorge Wilson 		    ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
761573ca77eSGeorge Wilson 
762573ca77eSGeorge Wilson 		nvlist_free(attr);
763573ca77eSGeorge Wilson 		kmem_free(physpath, MAXPATHLEN);
764573ca77eSGeorge Wilson 	}
765573ca77eSGeorge Wilson 
766bb0ade09Sahrens out:
767*681d9761SEric Taylor 	dmu_objset_rele(os, FTAG);
768bb0ade09Sahrens 
769fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
770fa9e4066Sahrens 
771fa9e4066Sahrens 	return (error);
772fa9e4066Sahrens }
773fa9e4066Sahrens 
774fa9e4066Sahrens /*ARGSUSED*/
775fa9e4066Sahrens int
776fa9e4066Sahrens zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr)
777fa9e4066Sahrens {
778fa9e4066Sahrens 	minor_t minor = getminor(*devp);
779fa9e4066Sahrens 	zvol_state_t *zv;
780*681d9761SEric Taylor 	int err = 0;
781fa9e4066Sahrens 
782fa9e4066Sahrens 	if (minor == 0)			/* This is the control device */
783fa9e4066Sahrens 		return (0);
784fa9e4066Sahrens 
785fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
786fa9e4066Sahrens 
787fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
788fa9e4066Sahrens 	if (zv == NULL) {
789fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
790fa9e4066Sahrens 		return (ENXIO);
791fa9e4066Sahrens 	}
792fa9e4066Sahrens 
793*681d9761SEric Taylor 	if (zv->zv_total_opens == 0)
794*681d9761SEric Taylor 		err = zvol_first_open(zv);
795*681d9761SEric Taylor 	if (err) {
796fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
797*681d9761SEric Taylor 		return (err);
798*681d9761SEric Taylor 	}
799*681d9761SEric Taylor 	if ((flag & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
800*681d9761SEric Taylor 		err = EROFS;
801*681d9761SEric Taylor 		goto out;
802fa9e4066Sahrens 	}
803c7f714e2SEric Taylor 	if (zv->zv_flags & ZVOL_EXCL) {
804*681d9761SEric Taylor 		err = EBUSY;
805*681d9761SEric Taylor 		goto out;
806c7f714e2SEric Taylor 	}
807c7f714e2SEric Taylor 	if (flag & FEXCL) {
808c7f714e2SEric Taylor 		if (zv->zv_total_opens != 0) {
809*681d9761SEric Taylor 			err = EBUSY;
810*681d9761SEric Taylor 			goto out;
811c7f714e2SEric Taylor 		}
812c7f714e2SEric Taylor 		zv->zv_flags |= ZVOL_EXCL;
813c7f714e2SEric Taylor 	}
814fa9e4066Sahrens 
815fa9e4066Sahrens 	if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) {
816fa9e4066Sahrens 		zv->zv_open_count[otyp]++;
817fa9e4066Sahrens 		zv->zv_total_opens++;
818fa9e4066Sahrens 	}
819fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
820fa9e4066Sahrens 
821*681d9761SEric Taylor 	return (err);
822*681d9761SEric Taylor out:
823*681d9761SEric Taylor 	if (zv->zv_total_opens == 0)
824*681d9761SEric Taylor 		zvol_last_close(zv);
825*681d9761SEric Taylor 	mutex_exit(&zvol_state_lock);
826*681d9761SEric Taylor 	return (err);
827fa9e4066Sahrens }
828fa9e4066Sahrens 
829fa9e4066Sahrens /*ARGSUSED*/
830fa9e4066Sahrens int
831fa9e4066Sahrens zvol_close(dev_t dev, int flag, int otyp, cred_t *cr)
832fa9e4066Sahrens {
833fa9e4066Sahrens 	minor_t minor = getminor(dev);
834fa9e4066Sahrens 	zvol_state_t *zv;
835*681d9761SEric Taylor 	int error = 0;
836fa9e4066Sahrens 
837fa9e4066Sahrens 	if (minor == 0)		/* This is the control device */
838fa9e4066Sahrens 		return (0);
839fa9e4066Sahrens 
840fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
841fa9e4066Sahrens 
842fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
843fa9e4066Sahrens 	if (zv == NULL) {
844fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
845fa9e4066Sahrens 		return (ENXIO);
846fa9e4066Sahrens 	}
847fa9e4066Sahrens 
848c7f714e2SEric Taylor 	if (zv->zv_flags & ZVOL_EXCL) {
849c7f714e2SEric Taylor 		ASSERT(zv->zv_total_opens == 1);
850c7f714e2SEric Taylor 		zv->zv_flags &= ~ZVOL_EXCL;
851fa9e4066Sahrens 	}
852fa9e4066Sahrens 
853fa9e4066Sahrens 	/*
854fa9e4066Sahrens 	 * If the open count is zero, this is a spurious close.
855fa9e4066Sahrens 	 * That indicates a bug in the kernel / DDI framework.
856fa9e4066Sahrens 	 */
857fa9e4066Sahrens 	ASSERT(zv->zv_open_count[otyp] != 0);
858fa9e4066Sahrens 	ASSERT(zv->zv_total_opens != 0);
859fa9e4066Sahrens 
860fa9e4066Sahrens 	/*
861fa9e4066Sahrens 	 * You may get multiple opens, but only one close.
862fa9e4066Sahrens 	 */
863fa9e4066Sahrens 	zv->zv_open_count[otyp]--;
864fa9e4066Sahrens 	zv->zv_total_opens--;
865fa9e4066Sahrens 
866*681d9761SEric Taylor 	if (zv->zv_total_opens == 0)
867*681d9761SEric Taylor 		zvol_last_close(zv);
868fa9e4066Sahrens 
869*681d9761SEric Taylor 	mutex_exit(&zvol_state_lock);
870*681d9761SEric Taylor 	return (error);
871fa9e4066Sahrens }
872fa9e4066Sahrens 
873feb08c6bSbillm static void
87467bd71c6Sperrin zvol_get_done(dmu_buf_t *db, void *vzgd)
87567bd71c6Sperrin {
87667bd71c6Sperrin 	zgd_t *zgd = (zgd_t *)vzgd;
877c2e6a7d6Sperrin 	rl_t *rl = zgd->zgd_rl;
87867bd71c6Sperrin 
87967bd71c6Sperrin 	dmu_buf_rele(db, vzgd);
880c2e6a7d6Sperrin 	zfs_range_unlock(rl);
88117f17c2dSbonwick 	zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
88267bd71c6Sperrin 	kmem_free(zgd, sizeof (zgd_t));
88367bd71c6Sperrin }
88467bd71c6Sperrin 
88567bd71c6Sperrin /*
88667bd71c6Sperrin  * Get data to generate a TX_WRITE intent log record.
88767bd71c6Sperrin  */
888feb08c6bSbillm static int
88967bd71c6Sperrin zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
89067bd71c6Sperrin {
89167bd71c6Sperrin 	zvol_state_t *zv = arg;
89267bd71c6Sperrin 	objset_t *os = zv->zv_objset;
89367bd71c6Sperrin 	dmu_buf_t *db;
894c2e6a7d6Sperrin 	rl_t *rl;
89567bd71c6Sperrin 	zgd_t *zgd;
896c2e6a7d6Sperrin 	uint64_t boff; 			/* block starting offset */
897c2e6a7d6Sperrin 	int dlen = lr->lr_length;	/* length of user data */
89867bd71c6Sperrin 	int error;
89967bd71c6Sperrin 
90067bd71c6Sperrin 	ASSERT(zio);
901c2e6a7d6Sperrin 	ASSERT(dlen != 0);
902feb08c6bSbillm 
903c2e6a7d6Sperrin 	/*
904c2e6a7d6Sperrin 	 * Write records come in two flavors: immediate and indirect.
905c2e6a7d6Sperrin 	 * For small writes it's cheaper to store the data with the
906c2e6a7d6Sperrin 	 * log record (immediate); for large writes it's cheaper to
907c2e6a7d6Sperrin 	 * sync the data and get a pointer to it (indirect) so that
908c2e6a7d6Sperrin 	 * we don't have to write the data twice.
909c2e6a7d6Sperrin 	 */
910c2e6a7d6Sperrin 	if (buf != NULL) /* immediate write */
9117bfdf011SNeil Perrin 		return (dmu_read(os, ZVOL_OBJ, lr->lr_offset, dlen, buf,
9127bfdf011SNeil Perrin 		    DMU_READ_NO_PREFETCH));
91367bd71c6Sperrin 
91467bd71c6Sperrin 	zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP);
91567bd71c6Sperrin 	zgd->zgd_zilog = zv->zv_zilog;
91667bd71c6Sperrin 	zgd->zgd_bp = &lr->lr_blkptr;
91767bd71c6Sperrin 
91867bd71c6Sperrin 	/*
919c2e6a7d6Sperrin 	 * Lock the range of the block to ensure that when the data is
920e7cbe64fSgw 	 * written out and its checksum is being calculated that no other
921c2e6a7d6Sperrin 	 * thread can change the block.
92267bd71c6Sperrin 	 */
923c2e6a7d6Sperrin 	boff = P2ALIGN_TYPED(lr->lr_offset, zv->zv_volblocksize, uint64_t);
924c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, boff, zv->zv_volblocksize,
925c2e6a7d6Sperrin 	    RL_READER);
926c2e6a7d6Sperrin 	zgd->zgd_rl = rl;
927c2e6a7d6Sperrin 
928c2e6a7d6Sperrin 	VERIFY(0 == dmu_buf_hold(os, ZVOL_OBJ, lr->lr_offset, zgd, &db));
92967bd71c6Sperrin 	error = dmu_sync(zio, db, &lr->lr_blkptr,
93067bd71c6Sperrin 	    lr->lr_common.lrc_txg, zvol_get_done, zgd);
931feb08c6bSbillm 	if (error == 0)
93217f17c2dSbonwick 		zil_add_block(zv->zv_zilog, &lr->lr_blkptr);
93367bd71c6Sperrin 	/*
93467bd71c6Sperrin 	 * If we get EINPROGRESS, then we need to wait for a
93567bd71c6Sperrin 	 * write IO initiated by dmu_sync() to complete before
93667bd71c6Sperrin 	 * we can release this dbuf.  We will finish everything
93767bd71c6Sperrin 	 * up in the zvol_get_done() callback.
93867bd71c6Sperrin 	 */
93967bd71c6Sperrin 	if (error == EINPROGRESS)
94067bd71c6Sperrin 		return (0);
94167bd71c6Sperrin 	dmu_buf_rele(db, zgd);
942c2e6a7d6Sperrin 	zfs_range_unlock(rl);
94367bd71c6Sperrin 	kmem_free(zgd, sizeof (zgd_t));
94467bd71c6Sperrin 	return (error);
94567bd71c6Sperrin }
94667bd71c6Sperrin 
947a24e15ceSperrin /*
948a24e15ceSperrin  * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
94922ac5be4Sperrin  *
95022ac5be4Sperrin  * We store data in the log buffers if it's small enough.
95167bd71c6Sperrin  * Otherwise we will later flush the data out via dmu_sync().
95222ac5be4Sperrin  */
95367bd71c6Sperrin ssize_t zvol_immediate_write_sz = 32768;
95422ac5be4Sperrin 
955feb08c6bSbillm static void
956510b6c0eSNeil Perrin zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid,
957510b6c0eSNeil Perrin     boolean_t sync)
95822ac5be4Sperrin {
959feb08c6bSbillm 	uint32_t blocksize = zv->zv_volblocksize;
9601209a471SNeil Perrin 	zilog_t *zilog = zv->zv_zilog;
961510b6c0eSNeil Perrin 	boolean_t slogging;
962e09fa4daSNeil Perrin 	ssize_t immediate_write_sz;
963510b6c0eSNeil Perrin 
964510b6c0eSNeil Perrin 	if (zil_disable)
965510b6c0eSNeil Perrin 		return;
96622ac5be4Sperrin 
9671209a471SNeil Perrin 	if (zilog->zl_replay) {
9681209a471SNeil Perrin 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
9691209a471SNeil Perrin 		zilog->zl_replayed_seq[dmu_tx_get_txg(tx) & TXG_MASK] =
9701209a471SNeil Perrin 		    zilog->zl_replaying_seq;
9711209a471SNeil Perrin 		return;
9721209a471SNeil Perrin 	}
9731209a471SNeil Perrin 
974e09fa4daSNeil Perrin 	immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
975e09fa4daSNeil Perrin 	    ? 0 : zvol_immediate_write_sz;
976e09fa4daSNeil Perrin 
977e09fa4daSNeil Perrin 	slogging = spa_has_slogs(zilog->zl_spa) &&
978e09fa4daSNeil Perrin 	    (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
979feb08c6bSbillm 
980510b6c0eSNeil Perrin 	while (resid) {
981510b6c0eSNeil Perrin 		itx_t *itx;
982510b6c0eSNeil Perrin 		lr_write_t *lr;
983510b6c0eSNeil Perrin 		ssize_t len;
984510b6c0eSNeil Perrin 		itx_wr_state_t write_state;
985510b6c0eSNeil Perrin 
986510b6c0eSNeil Perrin 		/*
987510b6c0eSNeil Perrin 		 * Unlike zfs_log_write() we can be called with
988510b6c0eSNeil Perrin 		 * upto DMU_MAX_ACCESS/2 (5MB) writes.
989510b6c0eSNeil Perrin 		 */
990e09fa4daSNeil Perrin 		if (blocksize > immediate_write_sz && !slogging &&
991510b6c0eSNeil Perrin 		    resid >= blocksize && off % blocksize == 0) {
992510b6c0eSNeil Perrin 			write_state = WR_INDIRECT; /* uses dmu_sync */
993510b6c0eSNeil Perrin 			len = blocksize;
994510b6c0eSNeil Perrin 		} else if (sync) {
995510b6c0eSNeil Perrin 			write_state = WR_COPIED;
996510b6c0eSNeil Perrin 			len = MIN(ZIL_MAX_LOG_DATA, resid);
997510b6c0eSNeil Perrin 		} else {
998510b6c0eSNeil Perrin 			write_state = WR_NEED_COPY;
999510b6c0eSNeil Perrin 			len = MIN(ZIL_MAX_LOG_DATA, resid);
1000510b6c0eSNeil Perrin 		}
1001510b6c0eSNeil Perrin 
1002510b6c0eSNeil Perrin 		itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
1003510b6c0eSNeil Perrin 		    (write_state == WR_COPIED ? len : 0));
1004feb08c6bSbillm 		lr = (lr_write_t *)&itx->itx_lr;
1005510b6c0eSNeil Perrin 		if (write_state == WR_COPIED && dmu_read(zv->zv_objset,
10067bfdf011SNeil Perrin 		    ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
1007510b6c0eSNeil Perrin 			kmem_free(itx, offsetof(itx_t, itx_lr) +
1008510b6c0eSNeil Perrin 			    itx->itx_lr.lrc_reclen);
1009510b6c0eSNeil Perrin 			itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1010510b6c0eSNeil Perrin 			lr = (lr_write_t *)&itx->itx_lr;
1011510b6c0eSNeil Perrin 			write_state = WR_NEED_COPY;
1012510b6c0eSNeil Perrin 		}
1013510b6c0eSNeil Perrin 
1014510b6c0eSNeil Perrin 		itx->itx_wr_state = write_state;
1015510b6c0eSNeil Perrin 		if (write_state == WR_NEED_COPY)
1016510b6c0eSNeil Perrin 			itx->itx_sod += len;
1017feb08c6bSbillm 		lr->lr_foid = ZVOL_OBJ;
1018feb08c6bSbillm 		lr->lr_offset = off;
1019510b6c0eSNeil Perrin 		lr->lr_length = len;
1020feb08c6bSbillm 		lr->lr_blkoff = off - P2ALIGN_TYPED(off, blocksize, uint64_t);
1021feb08c6bSbillm 		BP_ZERO(&lr->lr_blkptr);
1022feb08c6bSbillm 
1023510b6c0eSNeil Perrin 		itx->itx_private = zv;
1024510b6c0eSNeil Perrin 		itx->itx_sync = sync;
1025510b6c0eSNeil Perrin 
10261209a471SNeil Perrin 		(void) zil_itx_assign(zilog, itx, tx);
1027510b6c0eSNeil Perrin 
1028510b6c0eSNeil Perrin 		off += len;
1029510b6c0eSNeil Perrin 		resid -= len;
103022ac5be4Sperrin 	}
103122ac5be4Sperrin }
103222ac5be4Sperrin 
103388b7b0f2SMatthew Ahrens static int
103488b7b0f2SMatthew Ahrens zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t size,
103588b7b0f2SMatthew Ahrens     boolean_t doread, boolean_t isdump)
1036e7cbe64fSgw {
1037e7cbe64fSgw 	vdev_disk_t *dvd;
1038e7cbe64fSgw 	int c;
1039e7cbe64fSgw 	int numerrors = 0;
1040e7cbe64fSgw 
1041e7cbe64fSgw 	for (c = 0; c < vd->vdev_children; c++) {
104221ecdf64SLin Ling 		ASSERT(vd->vdev_ops == &vdev_mirror_ops ||
104321ecdf64SLin Ling 		    vd->vdev_ops == &vdev_replacing_ops ||
104421ecdf64SLin Ling 		    vd->vdev_ops == &vdev_spare_ops);
104588b7b0f2SMatthew Ahrens 		int err = zvol_dumpio_vdev(vd->vdev_child[c],
104688b7b0f2SMatthew Ahrens 		    addr, offset, size, doread, isdump);
104788b7b0f2SMatthew Ahrens 		if (err != 0) {
1048e7cbe64fSgw 			numerrors++;
104988b7b0f2SMatthew Ahrens 		} else if (doread) {
1050e7cbe64fSgw 			break;
1051e7cbe64fSgw 		}
1052e7cbe64fSgw 	}
1053e7cbe64fSgw 
1054e7cbe64fSgw 	if (!vd->vdev_ops->vdev_op_leaf)
1055e7cbe64fSgw 		return (numerrors < vd->vdev_children ? 0 : EIO);
1056e7cbe64fSgw 
1057dc0bb255SEric Taylor 	if (doread && !vdev_readable(vd))
1058dc0bb255SEric Taylor 		return (EIO);
1059dc0bb255SEric Taylor 	else if (!doread && !vdev_writeable(vd))
1060e7cbe64fSgw 		return (EIO);
1061e7cbe64fSgw 
1062e7cbe64fSgw 	dvd = vd->vdev_tsd;
1063e7cbe64fSgw 	ASSERT3P(dvd, !=, NULL);
1064e7cbe64fSgw 	offset += VDEV_LABEL_START_SIZE;
1065e7cbe64fSgw 
1066e7cbe64fSgw 	if (ddi_in_panic() || isdump) {
106788b7b0f2SMatthew Ahrens 		ASSERT(!doread);
106888b7b0f2SMatthew Ahrens 		if (doread)
1069e7cbe64fSgw 			return (EIO);
1070e7cbe64fSgw 		return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset),
1071e7cbe64fSgw 		    lbtodb(size)));
1072e7cbe64fSgw 	} else {
1073e7cbe64fSgw 		return (vdev_disk_physio(dvd->vd_lh, addr, size, offset,
107488b7b0f2SMatthew Ahrens 		    doread ? B_READ : B_WRITE));
1075e7cbe64fSgw 	}
1076e7cbe64fSgw }
1077e7cbe64fSgw 
107888b7b0f2SMatthew Ahrens static int
107988b7b0f2SMatthew Ahrens zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size,
108088b7b0f2SMatthew Ahrens     boolean_t doread, boolean_t isdump)
1081e7cbe64fSgw {
1082e7cbe64fSgw 	vdev_t *vd;
1083e7cbe64fSgw 	int error;
108488b7b0f2SMatthew Ahrens 	zvol_extent_t *ze;
1085e7cbe64fSgw 	spa_t *spa = dmu_objset_spa(zv->zv_objset);
1086e7cbe64fSgw 
108788b7b0f2SMatthew Ahrens 	/* Must be sector aligned, and not stradle a block boundary. */
108888b7b0f2SMatthew Ahrens 	if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) ||
108988b7b0f2SMatthew Ahrens 	    P2BOUNDARY(offset, size, zv->zv_volblocksize)) {
1090e7cbe64fSgw 		return (EINVAL);
109188b7b0f2SMatthew Ahrens 	}
109288b7b0f2SMatthew Ahrens 	ASSERT(size <= zv->zv_volblocksize);
1093e7cbe64fSgw 
109488b7b0f2SMatthew Ahrens 	/* Locate the extent this belongs to */
109588b7b0f2SMatthew Ahrens 	ze = list_head(&zv->zv_extents);
109688b7b0f2SMatthew Ahrens 	while (offset >= ze->ze_nblks * zv->zv_volblocksize) {
109788b7b0f2SMatthew Ahrens 		offset -= ze->ze_nblks * zv->zv_volblocksize;
109888b7b0f2SMatthew Ahrens 		ze = list_next(&zv->zv_extents, ze);
109988b7b0f2SMatthew Ahrens 	}
1100e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
110188b7b0f2SMatthew Ahrens 	vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva));
110288b7b0f2SMatthew Ahrens 	offset += DVA_GET_OFFSET(&ze->ze_dva);
110388b7b0f2SMatthew Ahrens 	error = zvol_dumpio_vdev(vd, addr, offset, size, doread, isdump);
1104e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
1105e7cbe64fSgw 	return (error);
1106e7cbe64fSgw }
1107e7cbe64fSgw 
1108fa9e4066Sahrens int
1109fa9e4066Sahrens zvol_strategy(buf_t *bp)
1110fa9e4066Sahrens {
1111fa9e4066Sahrens 	zvol_state_t *zv = ddi_get_soft_state(zvol_state, getminor(bp->b_edev));
1112fa9e4066Sahrens 	uint64_t off, volsize;
111388b7b0f2SMatthew Ahrens 	size_t resid;
1114fa9e4066Sahrens 	char *addr;
111522ac5be4Sperrin 	objset_t *os;
1116c2e6a7d6Sperrin 	rl_t *rl;
1117fa9e4066Sahrens 	int error = 0;
111888b7b0f2SMatthew Ahrens 	boolean_t doread = bp->b_flags & B_READ;
111988b7b0f2SMatthew Ahrens 	boolean_t is_dump = zv->zv_flags & ZVOL_DUMPIFIED;
1120510b6c0eSNeil Perrin 	boolean_t sync;
1121fa9e4066Sahrens 
1122fa9e4066Sahrens 	if (zv == NULL) {
1123fa9e4066Sahrens 		bioerror(bp, ENXIO);
1124fa9e4066Sahrens 		biodone(bp);
1125fa9e4066Sahrens 		return (0);
1126fa9e4066Sahrens 	}
1127fa9e4066Sahrens 
1128fa9e4066Sahrens 	if (getminor(bp->b_edev) == 0) {
1129fa9e4066Sahrens 		bioerror(bp, EINVAL);
1130fa9e4066Sahrens 		biodone(bp);
1131fa9e4066Sahrens 		return (0);
1132fa9e4066Sahrens 	}
1133fa9e4066Sahrens 
1134*681d9761SEric Taylor 	if (!(bp->b_flags & B_READ) && (zv->zv_flags & ZVOL_RDONLY)) {
1135fa9e4066Sahrens 		bioerror(bp, EROFS);
1136fa9e4066Sahrens 		biodone(bp);
1137fa9e4066Sahrens 		return (0);
1138fa9e4066Sahrens 	}
1139fa9e4066Sahrens 
1140fa9e4066Sahrens 	off = ldbtob(bp->b_blkno);
1141fa9e4066Sahrens 	volsize = zv->zv_volsize;
1142fa9e4066Sahrens 
114322ac5be4Sperrin 	os = zv->zv_objset;
114422ac5be4Sperrin 	ASSERT(os != NULL);
1145fa9e4066Sahrens 
1146fa9e4066Sahrens 	bp_mapin(bp);
1147fa9e4066Sahrens 	addr = bp->b_un.b_addr;
1148fa9e4066Sahrens 	resid = bp->b_bcount;
1149fa9e4066Sahrens 
115088b7b0f2SMatthew Ahrens 	if (resid > 0 && (off < 0 || off >= volsize)) {
115188b7b0f2SMatthew Ahrens 		bioerror(bp, EIO);
115288b7b0f2SMatthew Ahrens 		biodone(bp);
115388b7b0f2SMatthew Ahrens 		return (0);
115488b7b0f2SMatthew Ahrens 	}
115573ec3d9cSgw 
1156510b6c0eSNeil Perrin 	sync = !(bp->b_flags & B_ASYNC) && !doread && !is_dump &&
1157510b6c0eSNeil Perrin 	    !(zv->zv_flags & ZVOL_WCE) && !zil_disable;
1158510b6c0eSNeil Perrin 
1159a24e15ceSperrin 	/*
1160a24e15ceSperrin 	 * There must be no buffer changes when doing a dmu_sync() because
1161a24e15ceSperrin 	 * we can't change the data whilst calculating the checksum.
1162a24e15ceSperrin 	 */
1163c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, off, resid,
116488b7b0f2SMatthew Ahrens 	    doread ? RL_READER : RL_WRITER);
1165fa9e4066Sahrens 
1166e7cbe64fSgw 	while (resid != 0 && off < volsize) {
116788b7b0f2SMatthew Ahrens 		size_t size = MIN(resid, zvol_maxphys);
1168e7cbe64fSgw 		if (is_dump) {
1169e7cbe64fSgw 			size = MIN(size, P2END(off, zv->zv_volblocksize) - off);
117088b7b0f2SMatthew Ahrens 			error = zvol_dumpio(zv, addr, off, size,
117188b7b0f2SMatthew Ahrens 			    doread, B_FALSE);
117288b7b0f2SMatthew Ahrens 		} else if (doread) {
11737bfdf011SNeil Perrin 			error = dmu_read(os, ZVOL_OBJ, off, size, addr,
11747bfdf011SNeil Perrin 			    DMU_READ_PREFETCH);
1175fa9e4066Sahrens 		} else {
117622ac5be4Sperrin 			dmu_tx_t *tx = dmu_tx_create(os);
1177fa9e4066Sahrens 			dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
1178fa9e4066Sahrens 			error = dmu_tx_assign(tx, TXG_WAIT);
1179fa9e4066Sahrens 			if (error) {
1180fa9e4066Sahrens 				dmu_tx_abort(tx);
1181fa9e4066Sahrens 			} else {
118222ac5be4Sperrin 				dmu_write(os, ZVOL_OBJ, off, size, addr, tx);
1183510b6c0eSNeil Perrin 				zvol_log_write(zv, tx, off, size, sync);
1184fa9e4066Sahrens 				dmu_tx_commit(tx);
1185fa9e4066Sahrens 			}
1186fa9e4066Sahrens 		}
1187b87f3af3Sperrin 		if (error) {
1188b87f3af3Sperrin 			/* convert checksum errors into IO errors */
1189b87f3af3Sperrin 			if (error == ECKSUM)
1190b87f3af3Sperrin 				error = EIO;
1191fa9e4066Sahrens 			break;
1192b87f3af3Sperrin 		}
1193fa9e4066Sahrens 		off += size;
1194fa9e4066Sahrens 		addr += size;
1195fa9e4066Sahrens 		resid -= size;
1196fa9e4066Sahrens 	}
1197c2e6a7d6Sperrin 	zfs_range_unlock(rl);
1198fa9e4066Sahrens 
1199fa9e4066Sahrens 	if ((bp->b_resid = resid) == bp->b_bcount)
1200fa9e4066Sahrens 		bioerror(bp, off > volsize ? EINVAL : error);
1201fa9e4066Sahrens 
1202510b6c0eSNeil Perrin 	if (sync)
1203feb08c6bSbillm 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1204feb08c6bSbillm 	biodone(bp);
120522ac5be4Sperrin 
1206fa9e4066Sahrens 	return (0);
1207fa9e4066Sahrens }
1208fa9e4066Sahrens 
120967bd71c6Sperrin /*
121067bd71c6Sperrin  * Set the buffer count to the zvol maximum transfer.
121167bd71c6Sperrin  * Using our own routine instead of the default minphys()
121267bd71c6Sperrin  * means that for larger writes we write bigger buffers on X86
121367bd71c6Sperrin  * (128K instead of 56K) and flush the disk write cache less often
121467bd71c6Sperrin  * (every zvol_maxphys - currently 1MB) instead of minphys (currently
121567bd71c6Sperrin  * 56K on X86 and 128K on sparc).
121667bd71c6Sperrin  */
121767bd71c6Sperrin void
121867bd71c6Sperrin zvol_minphys(struct buf *bp)
121967bd71c6Sperrin {
122067bd71c6Sperrin 	if (bp->b_bcount > zvol_maxphys)
122167bd71c6Sperrin 		bp->b_bcount = zvol_maxphys;
122267bd71c6Sperrin }
122367bd71c6Sperrin 
1224e7cbe64fSgw int
1225e7cbe64fSgw zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks)
1226e7cbe64fSgw {
1227e7cbe64fSgw 	minor_t minor = getminor(dev);
1228e7cbe64fSgw 	zvol_state_t *zv;
1229e7cbe64fSgw 	int error = 0;
1230e7cbe64fSgw 	uint64_t size;
1231e7cbe64fSgw 	uint64_t boff;
1232e7cbe64fSgw 	uint64_t resid;
1233e7cbe64fSgw 
1234e7cbe64fSgw 	if (minor == 0)			/* This is the control device */
1235e7cbe64fSgw 		return (ENXIO);
1236e7cbe64fSgw 
1237e7cbe64fSgw 	zv = ddi_get_soft_state(zvol_state, minor);
1238e7cbe64fSgw 	if (zv == NULL)
1239e7cbe64fSgw 		return (ENXIO);
1240e7cbe64fSgw 
1241e7cbe64fSgw 	boff = ldbtob(blkno);
1242e7cbe64fSgw 	resid = ldbtob(nblocks);
124388b7b0f2SMatthew Ahrens 
124488b7b0f2SMatthew Ahrens 	VERIFY3U(boff + resid, <=, zv->zv_volsize);
124588b7b0f2SMatthew Ahrens 
1246e7cbe64fSgw 	while (resid) {
1247e7cbe64fSgw 		size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff);
124888b7b0f2SMatthew Ahrens 		error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE);
1249e7cbe64fSgw 		if (error)
1250e7cbe64fSgw 			break;
1251e7cbe64fSgw 		boff += size;
1252e7cbe64fSgw 		addr += size;
1253e7cbe64fSgw 		resid -= size;
1254e7cbe64fSgw 	}
1255e7cbe64fSgw 
1256e7cbe64fSgw 	return (error);
1257e7cbe64fSgw }
1258e7cbe64fSgw 
1259fa9e4066Sahrens /*ARGSUSED*/
1260fa9e4066Sahrens int
1261feb08c6bSbillm zvol_read(dev_t dev, uio_t *uio, cred_t *cr)
1262fa9e4066Sahrens {
1263c7ca1008Sgw 	minor_t minor = getminor(dev);
1264c7ca1008Sgw 	zvol_state_t *zv;
126573ec3d9cSgw 	uint64_t volsize;
1266c2e6a7d6Sperrin 	rl_t *rl;
1267feb08c6bSbillm 	int error = 0;
1268fa9e4066Sahrens 
1269c7ca1008Sgw 	if (minor == 0)			/* This is the control device */
1270c7ca1008Sgw 		return (ENXIO);
1271c7ca1008Sgw 
1272c7ca1008Sgw 	zv = ddi_get_soft_state(zvol_state, minor);
1273c7ca1008Sgw 	if (zv == NULL)
1274c7ca1008Sgw 		return (ENXIO);
1275c7ca1008Sgw 
127673ec3d9cSgw 	volsize = zv->zv_volsize;
127773ec3d9cSgw 	if (uio->uio_resid > 0 &&
127873ec3d9cSgw 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
127973ec3d9cSgw 		return (EIO);
128073ec3d9cSgw 
128188b7b0f2SMatthew Ahrens 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
128288b7b0f2SMatthew Ahrens 		error = physio(zvol_strategy, NULL, dev, B_READ,
128388b7b0f2SMatthew Ahrens 		    zvol_minphys, uio);
128488b7b0f2SMatthew Ahrens 		return (error);
128588b7b0f2SMatthew Ahrens 	}
128688b7b0f2SMatthew Ahrens 
1287c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1288c2e6a7d6Sperrin 	    RL_READER);
128973ec3d9cSgw 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1290feb08c6bSbillm 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1291fa9e4066Sahrens 
129273ec3d9cSgw 		/* don't read past the end */
129373ec3d9cSgw 		if (bytes > volsize - uio->uio_loffset)
129473ec3d9cSgw 			bytes = volsize - uio->uio_loffset;
129573ec3d9cSgw 
1296feb08c6bSbillm 		error =  dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes);
1297b87f3af3Sperrin 		if (error) {
1298b87f3af3Sperrin 			/* convert checksum errors into IO errors */
1299b87f3af3Sperrin 			if (error == ECKSUM)
1300b87f3af3Sperrin 				error = EIO;
1301feb08c6bSbillm 			break;
1302b87f3af3Sperrin 		}
1303feb08c6bSbillm 	}
1304c2e6a7d6Sperrin 	zfs_range_unlock(rl);
1305feb08c6bSbillm 	return (error);
1306fa9e4066Sahrens }
1307fa9e4066Sahrens 
1308fa9e4066Sahrens /*ARGSUSED*/
1309fa9e4066Sahrens int
1310feb08c6bSbillm zvol_write(dev_t dev, uio_t *uio, cred_t *cr)
1311fa9e4066Sahrens {
1312c7ca1008Sgw 	minor_t minor = getminor(dev);
1313c7ca1008Sgw 	zvol_state_t *zv;
131473ec3d9cSgw 	uint64_t volsize;
1315c2e6a7d6Sperrin 	rl_t *rl;
1316feb08c6bSbillm 	int error = 0;
1317510b6c0eSNeil Perrin 	boolean_t sync;
1318feb08c6bSbillm 
1319c7ca1008Sgw 	if (minor == 0)			/* This is the control device */
1320c7ca1008Sgw 		return (ENXIO);
1321c7ca1008Sgw 
1322c7ca1008Sgw 	zv = ddi_get_soft_state(zvol_state, minor);
1323c7ca1008Sgw 	if (zv == NULL)
1324c7ca1008Sgw 		return (ENXIO);
1325c7ca1008Sgw 
132673ec3d9cSgw 	volsize = zv->zv_volsize;
132773ec3d9cSgw 	if (uio->uio_resid > 0 &&
132873ec3d9cSgw 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
132973ec3d9cSgw 		return (EIO);
133073ec3d9cSgw 
1331e7cbe64fSgw 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
1332e7cbe64fSgw 		error = physio(zvol_strategy, NULL, dev, B_WRITE,
1333e7cbe64fSgw 		    zvol_minphys, uio);
1334e7cbe64fSgw 		return (error);
1335e7cbe64fSgw 	}
1336e7cbe64fSgw 
1337510b6c0eSNeil Perrin 	sync = !(zv->zv_flags & ZVOL_WCE) && !zil_disable;
1338510b6c0eSNeil Perrin 
1339c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1340c2e6a7d6Sperrin 	    RL_WRITER);
134173ec3d9cSgw 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1342feb08c6bSbillm 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1343feb08c6bSbillm 		uint64_t off = uio->uio_loffset;
1344feb08c6bSbillm 		dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
134573ec3d9cSgw 
134673ec3d9cSgw 		if (bytes > volsize - off)	/* don't write past the end */
134773ec3d9cSgw 			bytes = volsize - off;
134873ec3d9cSgw 
1349feb08c6bSbillm 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
1350feb08c6bSbillm 		error = dmu_tx_assign(tx, TXG_WAIT);
1351feb08c6bSbillm 		if (error) {
1352feb08c6bSbillm 			dmu_tx_abort(tx);
1353feb08c6bSbillm 			break;
1354feb08c6bSbillm 		}
1355feb08c6bSbillm 		error = dmu_write_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes, tx);
1356feb08c6bSbillm 		if (error == 0)
1357510b6c0eSNeil Perrin 			zvol_log_write(zv, tx, off, bytes, sync);
1358feb08c6bSbillm 		dmu_tx_commit(tx);
1359feb08c6bSbillm 
1360feb08c6bSbillm 		if (error)
1361feb08c6bSbillm 			break;
1362feb08c6bSbillm 	}
1363c2e6a7d6Sperrin 	zfs_range_unlock(rl);
1364510b6c0eSNeil Perrin 	if (sync)
1365e08bf2c6SEric Taylor 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1366feb08c6bSbillm 	return (error);
1367fa9e4066Sahrens }
1368fa9e4066Sahrens 
1369c7f714e2SEric Taylor int
1370c7f714e2SEric Taylor zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs)
1371c7f714e2SEric Taylor {
1372c7f714e2SEric Taylor 	struct uuid uuid = EFI_RESERVED;
1373c7f714e2SEric Taylor 	efi_gpe_t gpe = { 0 };
1374c7f714e2SEric Taylor 	uint32_t crc;
1375c7f714e2SEric Taylor 	dk_efi_t efi;
1376c7f714e2SEric Taylor 	int length;
1377c7f714e2SEric Taylor 	char *ptr;
1378c7f714e2SEric Taylor 
1379c7f714e2SEric Taylor 	if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag))
1380c7f714e2SEric Taylor 		return (EFAULT);
1381c7f714e2SEric Taylor 	ptr = (char *)(uintptr_t)efi.dki_data_64;
1382c7f714e2SEric Taylor 	length = efi.dki_length;
1383c7f714e2SEric Taylor 	/*
1384c7f714e2SEric Taylor 	 * Some clients may attempt to request a PMBR for the
1385c7f714e2SEric Taylor 	 * zvol.  Currently this interface will return EINVAL to
1386c7f714e2SEric Taylor 	 * such requests.  These requests could be supported by
1387c7f714e2SEric Taylor 	 * adding a check for lba == 0 and consing up an appropriate
1388c7f714e2SEric Taylor 	 * PMBR.
1389c7f714e2SEric Taylor 	 */
1390c7f714e2SEric Taylor 	if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0)
1391c7f714e2SEric Taylor 		return (EINVAL);
1392c7f714e2SEric Taylor 
1393c7f714e2SEric Taylor 	gpe.efi_gpe_StartingLBA = LE_64(34ULL);
1394c7f714e2SEric Taylor 	gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1);
1395c7f714e2SEric Taylor 	UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
1396c7f714e2SEric Taylor 
1397c7f714e2SEric Taylor 	if (efi.dki_lba == 1) {
1398c7f714e2SEric Taylor 		efi_gpt_t gpt = { 0 };
1399c7f714e2SEric Taylor 
1400c7f714e2SEric Taylor 		gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
1401c7f714e2SEric Taylor 		gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
1402c7f714e2SEric Taylor 		gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt));
1403c7f714e2SEric Taylor 		gpt.efi_gpt_MyLBA = LE_64(1ULL);
1404c7f714e2SEric Taylor 		gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL);
1405c7f714e2SEric Taylor 		gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1);
1406c7f714e2SEric Taylor 		gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL);
1407c7f714e2SEric Taylor 		gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
1408c7f714e2SEric Taylor 		gpt.efi_gpt_SizeOfPartitionEntry =
1409c7f714e2SEric Taylor 		    LE_32(sizeof (efi_gpe_t));
1410c7f714e2SEric Taylor 		CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
1411c7f714e2SEric Taylor 		gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
1412c7f714e2SEric Taylor 		CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table);
1413c7f714e2SEric Taylor 		gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
1414c7f714e2SEric Taylor 		if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length),
1415c7f714e2SEric Taylor 		    flag))
1416c7f714e2SEric Taylor 			return (EFAULT);
1417c7f714e2SEric Taylor 		ptr += sizeof (gpt);
1418c7f714e2SEric Taylor 		length -= sizeof (gpt);
1419c7f714e2SEric Taylor 	}
1420c7f714e2SEric Taylor 	if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe),
1421c7f714e2SEric Taylor 	    length), flag))
1422c7f714e2SEric Taylor 		return (EFAULT);
1423c7f714e2SEric Taylor 	return (0);
1424c7f714e2SEric Taylor }
1425c7f714e2SEric Taylor 
1426fa9e4066Sahrens /*
1427fa9e4066Sahrens  * Dirtbag ioctls to support mkfs(1M) for UFS filesystems.  See dkio(7I).
1428fa9e4066Sahrens  */
1429fa9e4066Sahrens /*ARGSUSED*/
1430fa9e4066Sahrens int
1431fa9e4066Sahrens zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
1432fa9e4066Sahrens {
1433fa9e4066Sahrens 	zvol_state_t *zv;
1434af2c4821Smaybee 	struct dk_cinfo dki;
1435fa9e4066Sahrens 	struct dk_minfo dkm;
1436af2c4821Smaybee 	struct dk_callback *dkc;
1437fa9e4066Sahrens 	int error = 0;
1438e7cbe64fSgw 	rl_t *rl;
1439fa9e4066Sahrens 
1440fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
1441fa9e4066Sahrens 
1442fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, getminor(dev));
1443fa9e4066Sahrens 
1444fa9e4066Sahrens 	if (zv == NULL) {
1445fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
1446fa9e4066Sahrens 		return (ENXIO);
1447fa9e4066Sahrens 	}
1448701f66c4SEric Taylor 	ASSERT(zv->zv_total_opens > 0);
1449fa9e4066Sahrens 
1450fa9e4066Sahrens 	switch (cmd) {
1451fa9e4066Sahrens 
1452fa9e4066Sahrens 	case DKIOCINFO:
1453af2c4821Smaybee 		bzero(&dki, sizeof (dki));
1454af2c4821Smaybee 		(void) strcpy(dki.dki_cname, "zvol");
1455af2c4821Smaybee 		(void) strcpy(dki.dki_dname, "zvol");
1456af2c4821Smaybee 		dki.dki_ctype = DKC_UNKNOWN;
1457af2c4821Smaybee 		dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs);
1458fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
1459af2c4821Smaybee 		if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag))
1460fa9e4066Sahrens 			error = EFAULT;
1461fa9e4066Sahrens 		return (error);
1462fa9e4066Sahrens 
1463fa9e4066Sahrens 	case DKIOCGMEDIAINFO:
1464fa9e4066Sahrens 		bzero(&dkm, sizeof (dkm));
1465fa9e4066Sahrens 		dkm.dki_lbsize = 1U << zv->zv_min_bs;
1466fa9e4066Sahrens 		dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
1467fa9e4066Sahrens 		dkm.dki_media_type = DK_UNKNOWN;
1468fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
1469fa9e4066Sahrens 		if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
1470fa9e4066Sahrens 			error = EFAULT;
1471fa9e4066Sahrens 		return (error);
1472fa9e4066Sahrens 
1473fa9e4066Sahrens 	case DKIOCGETEFI:
1474c7f714e2SEric Taylor 		{
1475c7f714e2SEric Taylor 			uint64_t vs = zv->zv_volsize;
1476c7f714e2SEric Taylor 			uint8_t bs = zv->zv_min_bs;
1477fa9e4066Sahrens 
147868a5ac4dSmaybee 			mutex_exit(&zvol_state_lock);
1479c7f714e2SEric Taylor 			error = zvol_getefi((void *)arg, flag, vs, bs);
1480c7f714e2SEric Taylor 			return (error);
148168a5ac4dSmaybee 		}
1482fa9e4066Sahrens 
1483feb08c6bSbillm 	case DKIOCFLUSHWRITECACHE:
1484af2c4821Smaybee 		dkc = (struct dk_callback *)arg;
1485701f66c4SEric Taylor 		mutex_exit(&zvol_state_lock);
1486feb08c6bSbillm 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1487af2c4821Smaybee 		if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) {
1488af2c4821Smaybee 			(*dkc->dkc_callback)(dkc->dkc_cookie, error);
1489af2c4821Smaybee 			error = 0;
1490af2c4821Smaybee 		}
1491701f66c4SEric Taylor 		return (error);
1492701f66c4SEric Taylor 
1493701f66c4SEric Taylor 	case DKIOCGETWCE:
1494701f66c4SEric Taylor 		{
1495701f66c4SEric Taylor 			int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0;
1496701f66c4SEric Taylor 			if (ddi_copyout(&wce, (void *)arg, sizeof (int),
1497701f66c4SEric Taylor 			    flag))
1498701f66c4SEric Taylor 				error = EFAULT;
1499701f66c4SEric Taylor 			break;
1500701f66c4SEric Taylor 		}
1501701f66c4SEric Taylor 	case DKIOCSETWCE:
1502701f66c4SEric Taylor 		{
1503701f66c4SEric Taylor 			int wce;
1504701f66c4SEric Taylor 			if (ddi_copyin((void *)arg, &wce, sizeof (int),
1505701f66c4SEric Taylor 			    flag)) {
1506701f66c4SEric Taylor 				error = EFAULT;
1507701f66c4SEric Taylor 				break;
1508701f66c4SEric Taylor 			}
1509701f66c4SEric Taylor 			if (wce) {
1510701f66c4SEric Taylor 				zv->zv_flags |= ZVOL_WCE;
1511701f66c4SEric Taylor 				mutex_exit(&zvol_state_lock);
1512701f66c4SEric Taylor 			} else {
1513701f66c4SEric Taylor 				zv->zv_flags &= ~ZVOL_WCE;
1514701f66c4SEric Taylor 				mutex_exit(&zvol_state_lock);
1515701f66c4SEric Taylor 				zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1516701f66c4SEric Taylor 			}
1517701f66c4SEric Taylor 			return (0);
1518701f66c4SEric Taylor 		}
1519feb08c6bSbillm 
1520b6130eadSmaybee 	case DKIOCGGEOM:
1521b6130eadSmaybee 	case DKIOCGVTOC:
1522e7cbe64fSgw 		/*
1523e7cbe64fSgw 		 * commands using these (like prtvtoc) expect ENOTSUP
1524e7cbe64fSgw 		 * since we're emulating an EFI label
1525e7cbe64fSgw 		 */
1526b6130eadSmaybee 		error = ENOTSUP;
1527b6130eadSmaybee 		break;
1528b6130eadSmaybee 
1529e7cbe64fSgw 	case DKIOCDUMPINIT:
1530e7cbe64fSgw 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1531e7cbe64fSgw 		    RL_WRITER);
1532e7cbe64fSgw 		error = zvol_dumpify(zv);
1533e7cbe64fSgw 		zfs_range_unlock(rl);
1534e7cbe64fSgw 		break;
1535e7cbe64fSgw 
1536e7cbe64fSgw 	case DKIOCDUMPFINI:
153706d5ae10SEric Taylor 		if (!(zv->zv_flags & ZVOL_DUMPIFIED))
153806d5ae10SEric Taylor 			break;
1539e7cbe64fSgw 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1540e7cbe64fSgw 		    RL_WRITER);
1541e7cbe64fSgw 		error = zvol_dump_fini(zv);
1542e7cbe64fSgw 		zfs_range_unlock(rl);
1543e7cbe64fSgw 		break;
1544e7cbe64fSgw 
1545fa9e4066Sahrens 	default:
154668a5ac4dSmaybee 		error = ENOTTY;
1547fa9e4066Sahrens 		break;
1548fa9e4066Sahrens 
1549fa9e4066Sahrens 	}
1550fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
1551fa9e4066Sahrens 	return (error);
1552fa9e4066Sahrens }
1553fa9e4066Sahrens 
1554fa9e4066Sahrens int
1555fa9e4066Sahrens zvol_busy(void)
1556fa9e4066Sahrens {
1557fa9e4066Sahrens 	return (zvol_minors != 0);
1558fa9e4066Sahrens }
1559fa9e4066Sahrens 
1560fa9e4066Sahrens void
1561fa9e4066Sahrens zvol_init(void)
1562fa9e4066Sahrens {
1563fa9e4066Sahrens 	VERIFY(ddi_soft_state_init(&zvol_state, sizeof (zvol_state_t), 1) == 0);
1564fa9e4066Sahrens 	mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
1565fa9e4066Sahrens }
1566fa9e4066Sahrens 
1567fa9e4066Sahrens void
1568fa9e4066Sahrens zvol_fini(void)
1569fa9e4066Sahrens {
1570fa9e4066Sahrens 	mutex_destroy(&zvol_state_lock);
1571fa9e4066Sahrens 	ddi_soft_state_fini(&zvol_state);
1572fa9e4066Sahrens }
1573e7cbe64fSgw 
1574e7cbe64fSgw static int
1575e7cbe64fSgw zvol_dump_init(zvol_state_t *zv, boolean_t resize)
1576e7cbe64fSgw {
1577e7cbe64fSgw 	dmu_tx_t *tx;
1578e7cbe64fSgw 	int error = 0;
1579e7cbe64fSgw 	objset_t *os = zv->zv_objset;
1580e7cbe64fSgw 	nvlist_t *nv = NULL;
1581e7cbe64fSgw 
1582e7cbe64fSgw 	ASSERT(MUTEX_HELD(&zvol_state_lock));
1583*681d9761SEric Taylor 	error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0,
1584*681d9761SEric Taylor 	    DMU_OBJECT_END);
1585*681d9761SEric Taylor 	/* wait for dmu_free_long_range to actually free the blocks */
1586*681d9761SEric Taylor 	txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
1587e7cbe64fSgw 
1588e7cbe64fSgw 	tx = dmu_tx_create(os);
1589e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1590*681d9761SEric Taylor 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
1591e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
1592e7cbe64fSgw 	if (error) {
1593e7cbe64fSgw 		dmu_tx_abort(tx);
1594e7cbe64fSgw 		return (error);
1595e7cbe64fSgw 	}
1596e7cbe64fSgw 
1597e7cbe64fSgw 	/*
1598e7cbe64fSgw 	 * If we are resizing the dump device then we only need to
1599e7cbe64fSgw 	 * update the refreservation to match the newly updated
1600e7cbe64fSgw 	 * zvolsize. Otherwise, we save off the original state of the
1601e7cbe64fSgw 	 * zvol so that we can restore them if the zvol is ever undumpified.
1602e7cbe64fSgw 	 */
1603e7cbe64fSgw 	if (resize) {
1604e7cbe64fSgw 		error = zap_update(os, ZVOL_ZAP_OBJ,
1605e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1606e7cbe64fSgw 		    &zv->zv_volsize, tx);
1607e7cbe64fSgw 	} else {
160888b7b0f2SMatthew Ahrens 		uint64_t checksum, compress, refresrv, vbs;
160988b7b0f2SMatthew Ahrens 
1610e7cbe64fSgw 		error = dsl_prop_get_integer(zv->zv_name,
1611e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL);
1612e7cbe64fSgw 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
1613e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL);
1614e7cbe64fSgw 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
1615e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL);
161688b7b0f2SMatthew Ahrens 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
161788b7b0f2SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL);
1618e7cbe64fSgw 
1619e7cbe64fSgw 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1620e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1,
1621e7cbe64fSgw 		    &compress, tx);
1622e7cbe64fSgw 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1623e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx);
1624e7cbe64fSgw 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1625e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1626e7cbe64fSgw 		    &refresrv, tx);
162788b7b0f2SMatthew Ahrens 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
162888b7b0f2SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1,
162988b7b0f2SMatthew Ahrens 		    &vbs, tx);
1630*681d9761SEric Taylor 		error = error ? error : dmu_object_set_blocksize(
1631*681d9761SEric Taylor 		    os, ZVOL_OBJ, SPA_MAXBLOCKSIZE, 0, tx);
1632*681d9761SEric Taylor 		if (error == 0)
1633*681d9761SEric Taylor 			zv->zv_volblocksize = SPA_MAXBLOCKSIZE;
1634e7cbe64fSgw 	}
1635e7cbe64fSgw 	dmu_tx_commit(tx);
1636e7cbe64fSgw 
1637e7cbe64fSgw 	/*
1638e7cbe64fSgw 	 * We only need update the zvol's property if we are initializing
1639e7cbe64fSgw 	 * the dump area for the first time.
1640e7cbe64fSgw 	 */
1641e7cbe64fSgw 	if (!resize) {
1642e7cbe64fSgw 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1643e7cbe64fSgw 		VERIFY(nvlist_add_uint64(nv,
1644e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0);
1645e7cbe64fSgw 		VERIFY(nvlist_add_uint64(nv,
1646e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
1647e7cbe64fSgw 		    ZIO_COMPRESS_OFF) == 0);
1648e7cbe64fSgw 		VERIFY(nvlist_add_uint64(nv,
1649e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
1650e7cbe64fSgw 		    ZIO_CHECKSUM_OFF) == 0);
1651e7cbe64fSgw 
1652e7cbe64fSgw 		error = zfs_set_prop_nvlist(zv->zv_name, nv);
1653e7cbe64fSgw 		nvlist_free(nv);
1654e7cbe64fSgw 
1655e7cbe64fSgw 		if (error)
1656e7cbe64fSgw 			return (error);
1657e7cbe64fSgw 	}
1658e7cbe64fSgw 
1659e7cbe64fSgw 	/* Allocate the space for the dump */
1660e7cbe64fSgw 	error = zvol_prealloc(zv);
1661e7cbe64fSgw 	return (error);
1662e7cbe64fSgw }
1663e7cbe64fSgw 
1664e7cbe64fSgw static int
1665e7cbe64fSgw zvol_dumpify(zvol_state_t *zv)
1666e7cbe64fSgw {
1667e7cbe64fSgw 	int error = 0;
1668e7cbe64fSgw 	uint64_t dumpsize = 0;
1669e7cbe64fSgw 	dmu_tx_t *tx;
1670e7cbe64fSgw 	objset_t *os = zv->zv_objset;
1671e7cbe64fSgw 
1672*681d9761SEric Taylor 	if (zv->zv_flags & ZVOL_RDONLY)
1673e7cbe64fSgw 		return (EROFS);
1674e7cbe64fSgw 
1675e7cbe64fSgw 	if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE,
1676e7cbe64fSgw 	    8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) {
1677e7cbe64fSgw 		boolean_t resize = (dumpsize > 0) ? B_TRUE : B_FALSE;
1678e7cbe64fSgw 
1679e7cbe64fSgw 		if ((error = zvol_dump_init(zv, resize)) != 0) {
1680e7cbe64fSgw 			(void) zvol_dump_fini(zv);
1681e7cbe64fSgw 			return (error);
1682e7cbe64fSgw 		}
1683e7cbe64fSgw 	}
1684e7cbe64fSgw 
1685e7cbe64fSgw 	/*
1686e7cbe64fSgw 	 * Build up our lba mapping.
1687e7cbe64fSgw 	 */
1688e7cbe64fSgw 	error = zvol_get_lbas(zv);
1689e7cbe64fSgw 	if (error) {
1690e7cbe64fSgw 		(void) zvol_dump_fini(zv);
1691e7cbe64fSgw 		return (error);
1692e7cbe64fSgw 	}
1693e7cbe64fSgw 
1694e7cbe64fSgw 	tx = dmu_tx_create(os);
1695e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1696e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
1697e7cbe64fSgw 	if (error) {
1698e7cbe64fSgw 		dmu_tx_abort(tx);
1699e7cbe64fSgw 		(void) zvol_dump_fini(zv);
1700e7cbe64fSgw 		return (error);
1701e7cbe64fSgw 	}
1702e7cbe64fSgw 
1703e7cbe64fSgw 	zv->zv_flags |= ZVOL_DUMPIFIED;
1704e7cbe64fSgw 	error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1,
1705e7cbe64fSgw 	    &zv->zv_volsize, tx);
1706e7cbe64fSgw 	dmu_tx_commit(tx);
1707e7cbe64fSgw 
1708e7cbe64fSgw 	if (error) {
1709e7cbe64fSgw 		(void) zvol_dump_fini(zv);
1710e7cbe64fSgw 		return (error);
1711e7cbe64fSgw 	}
1712e7cbe64fSgw 
1713e7cbe64fSgw 	txg_wait_synced(dmu_objset_pool(os), 0);
1714e7cbe64fSgw 	return (0);
1715e7cbe64fSgw }
1716e7cbe64fSgw 
1717e7cbe64fSgw static int
1718e7cbe64fSgw zvol_dump_fini(zvol_state_t *zv)
1719e7cbe64fSgw {
1720e7cbe64fSgw 	dmu_tx_t *tx;
1721e7cbe64fSgw 	objset_t *os = zv->zv_objset;
1722e7cbe64fSgw 	nvlist_t *nv;
1723e7cbe64fSgw 	int error = 0;
172488b7b0f2SMatthew Ahrens 	uint64_t checksum, compress, refresrv, vbs;
1725e7cbe64fSgw 
1726b7e50089Smaybee 	/*
1727b7e50089Smaybee 	 * Attempt to restore the zvol back to its pre-dumpified state.
1728b7e50089Smaybee 	 * This is a best-effort attempt as it's possible that not all
1729b7e50089Smaybee 	 * of these properties were initialized during the dumpify process
1730b7e50089Smaybee 	 * (i.e. error during zvol_dump_init).
1731b7e50089Smaybee 	 */
1732b7e50089Smaybee 
1733e7cbe64fSgw 	tx = dmu_tx_create(os);
1734e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1735e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
1736e7cbe64fSgw 	if (error) {
1737e7cbe64fSgw 		dmu_tx_abort(tx);
1738e7cbe64fSgw 		return (error);
1739e7cbe64fSgw 	}
1740b7e50089Smaybee 	(void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx);
1741b7e50089Smaybee 	dmu_tx_commit(tx);
1742e7cbe64fSgw 
1743e7cbe64fSgw 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1744e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum);
1745e7cbe64fSgw 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1746e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress);
1747e7cbe64fSgw 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1748e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv);
174988b7b0f2SMatthew Ahrens 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
175088b7b0f2SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs);
1751e7cbe64fSgw 
1752e7cbe64fSgw 	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1753e7cbe64fSgw 	(void) nvlist_add_uint64(nv,
1754e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum);
1755e7cbe64fSgw 	(void) nvlist_add_uint64(nv,
1756e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress);
1757e7cbe64fSgw 	(void) nvlist_add_uint64(nv,
1758e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv);
1759e7cbe64fSgw 	(void) zfs_set_prop_nvlist(zv->zv_name, nv);
1760e7cbe64fSgw 	nvlist_free(nv);
1761e7cbe64fSgw 
1762b7e50089Smaybee 	zvol_free_extents(zv);
1763b7e50089Smaybee 	zv->zv_flags &= ~ZVOL_DUMPIFIED;
1764b7e50089Smaybee 	(void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END);
1765*681d9761SEric Taylor 	/* wait for dmu_free_long_range to actually free the blocks */
1766*681d9761SEric Taylor 	txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
1767*681d9761SEric Taylor 	tx = dmu_tx_create(os);
1768*681d9761SEric Taylor 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
1769*681d9761SEric Taylor 	error = dmu_tx_assign(tx, TXG_WAIT);
1770*681d9761SEric Taylor 	if (error) {
1771*681d9761SEric Taylor 		dmu_tx_abort(tx);
1772*681d9761SEric Taylor 		return (error);
1773*681d9761SEric Taylor 	}
1774*681d9761SEric Taylor 	dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx);
1775*681d9761SEric Taylor 	dmu_tx_commit(tx);
1776b7e50089Smaybee 
1777e7cbe64fSgw 	return (0);
1778e7cbe64fSgw }
1779