xref: /illumos-gate/usr/src/uts/common/fs/zfs/zvol.c (revision 92241e0b80813d0b83c08e730a29b9d1831794fc)
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  *
35681d9761SEric 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*92241e0bSTom Erickson extern int zfs_set_prop_nvlist(const char *, zprop_source_t,
134*92241e0bSTom Erickson     nvlist_t *, nvlist_t **);
135681d9761SEric Taylor static int zvol_remove_zv(zvol_state_t *);
136feb08c6bSbillm static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio);
137e7cbe64fSgw static int zvol_dumpify(zvol_state_t *zv);
138e7cbe64fSgw static int zvol_dump_fini(zvol_state_t *zv);
139e7cbe64fSgw static int zvol_dump_init(zvol_state_t *zv, boolean_t resize);
14067bd71c6Sperrin 
141fa9e4066Sahrens static void
142681d9761SEric Taylor zvol_size_changed(uint64_t volsize, major_t maj, minor_t min)
143fa9e4066Sahrens {
144681d9761SEric Taylor 	dev_t dev = makedevice(maj, min);
145fa9e4066Sahrens 
146fa9e4066Sahrens 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
147681d9761SEric Taylor 	    "Size", volsize) == DDI_SUCCESS);
148fa9e4066Sahrens 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
149681d9761SEric Taylor 	    "Nblocks", lbtodb(volsize)) == DDI_SUCCESS);
150e7cbe64fSgw 
151e7cbe64fSgw 	/* Notify specfs to invalidate the cached size */
152e7cbe64fSgw 	spec_size_invalidate(dev, VBLK);
153e7cbe64fSgw 	spec_size_invalidate(dev, VCHR);
154fa9e4066Sahrens }
155fa9e4066Sahrens 
156fa9e4066Sahrens int
157e9dbad6fSeschrock zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
158fa9e4066Sahrens {
159e9dbad6fSeschrock 	if (volsize == 0)
160fa9e4066Sahrens 		return (EINVAL);
161fa9e4066Sahrens 
162e9dbad6fSeschrock 	if (volsize % blocksize != 0)
1635c5460e9Seschrock 		return (EINVAL);
1645c5460e9Seschrock 
165fa9e4066Sahrens #ifdef _ILP32
166e9dbad6fSeschrock 	if (volsize - 1 > SPEC_MAXOFFSET_T)
167fa9e4066Sahrens 		return (EOVERFLOW);
168fa9e4066Sahrens #endif
169fa9e4066Sahrens 	return (0);
170fa9e4066Sahrens }
171fa9e4066Sahrens 
172fa9e4066Sahrens int
173e9dbad6fSeschrock zvol_check_volblocksize(uint64_t volblocksize)
174fa9e4066Sahrens {
175e9dbad6fSeschrock 	if (volblocksize < SPA_MINBLOCKSIZE ||
176e9dbad6fSeschrock 	    volblocksize > SPA_MAXBLOCKSIZE ||
177e9dbad6fSeschrock 	    !ISP2(volblocksize))
178fa9e4066Sahrens 		return (EDOM);
179fa9e4066Sahrens 
180fa9e4066Sahrens 	return (0);
181fa9e4066Sahrens }
182fa9e4066Sahrens 
183fa9e4066Sahrens int
184a2eea2e1Sahrens zvol_get_stats(objset_t *os, nvlist_t *nv)
185fa9e4066Sahrens {
186fa9e4066Sahrens 	int error;
187fa9e4066Sahrens 	dmu_object_info_t doi;
188a2eea2e1Sahrens 	uint64_t val;
189fa9e4066Sahrens 
190a2eea2e1Sahrens 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
191fa9e4066Sahrens 	if (error)
192fa9e4066Sahrens 		return (error);
193fa9e4066Sahrens 
194a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
195a2eea2e1Sahrens 
196fa9e4066Sahrens 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
197fa9e4066Sahrens 
198a2eea2e1Sahrens 	if (error == 0) {
199a2eea2e1Sahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
200a2eea2e1Sahrens 		    doi.doi_data_block_size);
201a2eea2e1Sahrens 	}
202fa9e4066Sahrens 
203fa9e4066Sahrens 	return (error);
204fa9e4066Sahrens }
205fa9e4066Sahrens 
206fa9e4066Sahrens /*
207fa9e4066Sahrens  * Find a free minor number.
208fa9e4066Sahrens  */
209fa9e4066Sahrens static minor_t
210fa9e4066Sahrens zvol_minor_alloc(void)
211fa9e4066Sahrens {
212fa9e4066Sahrens 	minor_t minor;
213fa9e4066Sahrens 
214fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&zvol_state_lock));
215fa9e4066Sahrens 
216fa9e4066Sahrens 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++)
217fa9e4066Sahrens 		if (ddi_get_soft_state(zvol_state, minor) == NULL)
218fa9e4066Sahrens 			return (minor);
219fa9e4066Sahrens 
220fa9e4066Sahrens 	return (0);
221fa9e4066Sahrens }
222fa9e4066Sahrens 
223fa9e4066Sahrens static zvol_state_t *
224e9dbad6fSeschrock zvol_minor_lookup(const char *name)
225fa9e4066Sahrens {
226fa9e4066Sahrens 	minor_t minor;
227fa9e4066Sahrens 	zvol_state_t *zv;
228fa9e4066Sahrens 
229fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&zvol_state_lock));
230fa9e4066Sahrens 
231fa9e4066Sahrens 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) {
232fa9e4066Sahrens 		zv = ddi_get_soft_state(zvol_state, minor);
233fa9e4066Sahrens 		if (zv == NULL)
234fa9e4066Sahrens 			continue;
235fa9e4066Sahrens 		if (strcmp(zv->zv_name, name) == 0)
236fa9e4066Sahrens 			break;
237fa9e4066Sahrens 	}
238fa9e4066Sahrens 
239fa9e4066Sahrens 	return (zv);
240fa9e4066Sahrens }
241fa9e4066Sahrens 
242e7cbe64fSgw /* extent mapping arg */
243e7cbe64fSgw struct maparg {
24488b7b0f2SMatthew Ahrens 	zvol_state_t	*ma_zv;
24588b7b0f2SMatthew Ahrens 	uint64_t	ma_blks;
246e7cbe64fSgw };
247e7cbe64fSgw 
248e7cbe64fSgw /*ARGSUSED*/
249e7cbe64fSgw static int
250b24ab676SJeff Bonwick zvol_map_block(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
251b24ab676SJeff Bonwick     const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
252e7cbe64fSgw {
25388b7b0f2SMatthew Ahrens 	struct maparg *ma = arg;
25488b7b0f2SMatthew Ahrens 	zvol_extent_t *ze;
25588b7b0f2SMatthew Ahrens 	int bs = ma->ma_zv->zv_volblocksize;
256e7cbe64fSgw 
25788b7b0f2SMatthew Ahrens 	if (bp == NULL || zb->zb_object != ZVOL_OBJ || zb->zb_level != 0)
25888b7b0f2SMatthew Ahrens 		return (0);
259e7cbe64fSgw 
26088b7b0f2SMatthew Ahrens 	VERIFY3U(ma->ma_blks, ==, zb->zb_blkid);
26188b7b0f2SMatthew Ahrens 	ma->ma_blks++;
262e7cbe64fSgw 
26388b7b0f2SMatthew Ahrens 	/* Abort immediately if we have encountered gang blocks */
26488b7b0f2SMatthew Ahrens 	if (BP_IS_GANG(bp))
26588b7b0f2SMatthew Ahrens 		return (EFRAGS);
266e7cbe64fSgw 
26788b7b0f2SMatthew Ahrens 	/*
26888b7b0f2SMatthew Ahrens 	 * See if the block is at the end of the previous extent.
26988b7b0f2SMatthew Ahrens 	 */
27088b7b0f2SMatthew Ahrens 	ze = list_tail(&ma->ma_zv->zv_extents);
27188b7b0f2SMatthew Ahrens 	if (ze &&
27288b7b0f2SMatthew Ahrens 	    DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) &&
27388b7b0f2SMatthew Ahrens 	    DVA_GET_OFFSET(BP_IDENTITY(bp)) ==
27488b7b0f2SMatthew Ahrens 	    DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) {
27588b7b0f2SMatthew Ahrens 		ze->ze_nblks++;
27688b7b0f2SMatthew Ahrens 		return (0);
277e7cbe64fSgw 	}
278e7cbe64fSgw 
27988b7b0f2SMatthew Ahrens 	dprintf_bp(bp, "%s", "next blkptr:");
280e7cbe64fSgw 
28188b7b0f2SMatthew Ahrens 	/* start a new extent */
28288b7b0f2SMatthew Ahrens 	ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP);
28388b7b0f2SMatthew Ahrens 	ze->ze_dva = bp->blk_dva[0];	/* structure assignment */
28488b7b0f2SMatthew Ahrens 	ze->ze_nblks = 1;
28588b7b0f2SMatthew Ahrens 	list_insert_tail(&ma->ma_zv->zv_extents, ze);
28688b7b0f2SMatthew Ahrens 	return (0);
28788b7b0f2SMatthew Ahrens }
288e7cbe64fSgw 
28988b7b0f2SMatthew Ahrens static void
29088b7b0f2SMatthew Ahrens zvol_free_extents(zvol_state_t *zv)
29188b7b0f2SMatthew Ahrens {
29288b7b0f2SMatthew Ahrens 	zvol_extent_t *ze;
293e7cbe64fSgw 
29488b7b0f2SMatthew Ahrens 	while (ze = list_head(&zv->zv_extents)) {
29588b7b0f2SMatthew Ahrens 		list_remove(&zv->zv_extents, ze);
29688b7b0f2SMatthew Ahrens 		kmem_free(ze, sizeof (zvol_extent_t));
297e7cbe64fSgw 	}
29888b7b0f2SMatthew Ahrens }
299e7cbe64fSgw 
30088b7b0f2SMatthew Ahrens static int
30188b7b0f2SMatthew Ahrens zvol_get_lbas(zvol_state_t *zv)
30288b7b0f2SMatthew Ahrens {
30388b7b0f2SMatthew Ahrens 	struct maparg	ma;
30488b7b0f2SMatthew Ahrens 	int		err;
30588b7b0f2SMatthew Ahrens 
30688b7b0f2SMatthew Ahrens 	ma.ma_zv = zv;
30788b7b0f2SMatthew Ahrens 	ma.ma_blks = 0;
30888b7b0f2SMatthew Ahrens 	zvol_free_extents(zv);
30988b7b0f2SMatthew Ahrens 
31088b7b0f2SMatthew Ahrens 	err = traverse_dataset(dmu_objset_ds(zv->zv_objset), 0,
31188b7b0f2SMatthew Ahrens 	    TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma);
31288b7b0f2SMatthew Ahrens 	if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) {
31388b7b0f2SMatthew Ahrens 		zvol_free_extents(zv);
31488b7b0f2SMatthew Ahrens 		return (err ? err : EIO);
315e7cbe64fSgw 	}
31688b7b0f2SMatthew Ahrens 
317e7cbe64fSgw 	return (0);
318e7cbe64fSgw }
319e7cbe64fSgw 
320ecd6cf80Smarks /* ARGSUSED */
321fa9e4066Sahrens void
322ecd6cf80Smarks zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
323fa9e4066Sahrens {
324da6c28aaSamw 	zfs_creat_t *zct = arg;
325da6c28aaSamw 	nvlist_t *nvprops = zct->zct_props;
326fa9e4066Sahrens 	int error;
327e9dbad6fSeschrock 	uint64_t volblocksize, volsize;
328fa9e4066Sahrens 
329ecd6cf80Smarks 	VERIFY(nvlist_lookup_uint64(nvprops,
330e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
331ecd6cf80Smarks 	if (nvlist_lookup_uint64(nvprops,
332e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
333e9dbad6fSeschrock 		volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
334e9dbad6fSeschrock 
335e9dbad6fSeschrock 	/*
336e7cbe64fSgw 	 * These properties must be removed from the list so the generic
337e9dbad6fSeschrock 	 * property setting step won't apply to them.
338e9dbad6fSeschrock 	 */
339ecd6cf80Smarks 	VERIFY(nvlist_remove_all(nvprops,
340e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
341ecd6cf80Smarks 	(void) nvlist_remove_all(nvprops,
342e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
343e9dbad6fSeschrock 
344e9dbad6fSeschrock 	error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
345fa9e4066Sahrens 	    DMU_OT_NONE, 0, tx);
346fa9e4066Sahrens 	ASSERT(error == 0);
347fa9e4066Sahrens 
348fa9e4066Sahrens 	error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
349fa9e4066Sahrens 	    DMU_OT_NONE, 0, tx);
350fa9e4066Sahrens 	ASSERT(error == 0);
351fa9e4066Sahrens 
352e9dbad6fSeschrock 	error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
353fa9e4066Sahrens 	ASSERT(error == 0);
354fa9e4066Sahrens }
355fa9e4066Sahrens 
35622ac5be4Sperrin /*
35722ac5be4Sperrin  * Replay a TX_WRITE ZIL transaction that didn't get committed
35822ac5be4Sperrin  * after a system failure
35922ac5be4Sperrin  */
36022ac5be4Sperrin static int
36122ac5be4Sperrin zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
36222ac5be4Sperrin {
36322ac5be4Sperrin 	objset_t *os = zv->zv_objset;
36422ac5be4Sperrin 	char *data = (char *)(lr + 1);	/* data follows lr_write_t */
365b24ab676SJeff Bonwick 	uint64_t offset, length;
36622ac5be4Sperrin 	dmu_tx_t *tx;
36722ac5be4Sperrin 	int error;
36822ac5be4Sperrin 
36922ac5be4Sperrin 	if (byteswap)
37022ac5be4Sperrin 		byteswap_uint64_array(lr, sizeof (*lr));
37122ac5be4Sperrin 
372b24ab676SJeff Bonwick 	offset = lr->lr_offset;
373b24ab676SJeff Bonwick 	length = lr->lr_length;
374b24ab676SJeff Bonwick 
375b24ab676SJeff Bonwick 	/* If it's a dmu_sync() block, write the whole block */
376b24ab676SJeff Bonwick 	if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
377b24ab676SJeff Bonwick 		uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
378b24ab676SJeff Bonwick 		if (length < blocksize) {
379b24ab676SJeff Bonwick 			offset -= offset % blocksize;
380b24ab676SJeff Bonwick 			length = blocksize;
381b24ab676SJeff Bonwick 		}
382b24ab676SJeff Bonwick 	}
383975c32a0SNeil Perrin 
38422ac5be4Sperrin 	tx = dmu_tx_create(os);
385b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length);
3861209a471SNeil Perrin 	error = dmu_tx_assign(tx, TXG_WAIT);
38722ac5be4Sperrin 	if (error) {
38822ac5be4Sperrin 		dmu_tx_abort(tx);
38922ac5be4Sperrin 	} else {
390b24ab676SJeff Bonwick 		dmu_write(os, ZVOL_OBJ, offset, length, data, tx);
39122ac5be4Sperrin 		dmu_tx_commit(tx);
39222ac5be4Sperrin 	}
39322ac5be4Sperrin 
39422ac5be4Sperrin 	return (error);
39522ac5be4Sperrin }
39622ac5be4Sperrin 
39722ac5be4Sperrin /* ARGSUSED */
39822ac5be4Sperrin static int
39922ac5be4Sperrin zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
40022ac5be4Sperrin {
40122ac5be4Sperrin 	return (ENOTSUP);
40222ac5be4Sperrin }
40322ac5be4Sperrin 
40422ac5be4Sperrin /*
40522ac5be4Sperrin  * Callback vectors for replaying records.
40622ac5be4Sperrin  * Only TX_WRITE is needed for zvol.
40722ac5be4Sperrin  */
40822ac5be4Sperrin zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = {
40922ac5be4Sperrin 	zvol_replay_err,	/* 0 no such transaction type */
41022ac5be4Sperrin 	zvol_replay_err,	/* TX_CREATE */
41122ac5be4Sperrin 	zvol_replay_err,	/* TX_MKDIR */
41222ac5be4Sperrin 	zvol_replay_err,	/* TX_MKXATTR */
41322ac5be4Sperrin 	zvol_replay_err,	/* TX_SYMLINK */
41422ac5be4Sperrin 	zvol_replay_err,	/* TX_REMOVE */
41522ac5be4Sperrin 	zvol_replay_err,	/* TX_RMDIR */
41622ac5be4Sperrin 	zvol_replay_err,	/* TX_LINK */
41722ac5be4Sperrin 	zvol_replay_err,	/* TX_RENAME */
41822ac5be4Sperrin 	zvol_replay_write,	/* TX_WRITE */
41922ac5be4Sperrin 	zvol_replay_err,	/* TX_TRUNCATE */
42022ac5be4Sperrin 	zvol_replay_err,	/* TX_SETATTR */
42122ac5be4Sperrin 	zvol_replay_err,	/* TX_ACL */
422975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_CREATE_ACL */
423975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_CREATE_ATTR */
424975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_CREATE_ACL_ATTR */
425975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_MKDIR_ACL */
426975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_MKDIR_ATTR */
427975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_MKDIR_ACL_ATTR */
428975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_WRITE2 */
42922ac5be4Sperrin };
43022ac5be4Sperrin 
431681d9761SEric Taylor int
432681d9761SEric Taylor zvol_name2minor(const char *name, minor_t *minor)
433681d9761SEric Taylor {
434681d9761SEric Taylor 	zvol_state_t *zv;
435681d9761SEric Taylor 
436681d9761SEric Taylor 	mutex_enter(&zvol_state_lock);
437681d9761SEric Taylor 	zv = zvol_minor_lookup(name);
438681d9761SEric Taylor 	if (minor && zv)
439681d9761SEric Taylor 		*minor = zv->zv_minor;
440681d9761SEric Taylor 	mutex_exit(&zvol_state_lock);
441681d9761SEric Taylor 	return (zv ? 0 : -1);
442681d9761SEric Taylor }
443681d9761SEric Taylor 
444e7cbe64fSgw /*
445e7cbe64fSgw  * Create a minor node (plus a whole lot more) for the specified volume.
446fa9e4066Sahrens  */
447fa9e4066Sahrens int
448681d9761SEric Taylor zvol_create_minor(const char *name)
449fa9e4066Sahrens {
450fa9e4066Sahrens 	zvol_state_t *zv;
451fa9e4066Sahrens 	objset_t *os;
45267bd71c6Sperrin 	dmu_object_info_t doi;
453fa9e4066Sahrens 	minor_t minor = 0;
454fa9e4066Sahrens 	char chrbuf[30], blkbuf[30];
455fa9e4066Sahrens 	int error;
456fa9e4066Sahrens 
457fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
458fa9e4066Sahrens 
459fa9e4066Sahrens 	if ((zv = zvol_minor_lookup(name)) != NULL) {
460fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
461fa9e4066Sahrens 		return (EEXIST);
462fa9e4066Sahrens 	}
463fa9e4066Sahrens 
464503ad85cSMatthew Ahrens 	/* lie and say we're read-only */
465503ad85cSMatthew Ahrens 	error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, zvol_tag, &os);
466fa9e4066Sahrens 
467fa9e4066Sahrens 	if (error) {
468fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
469fa9e4066Sahrens 		return (error);
470fa9e4066Sahrens 	}
471fa9e4066Sahrens 
472681d9761SEric Taylor 	if ((minor = zvol_minor_alloc()) == 0) {
473503ad85cSMatthew Ahrens 		dmu_objset_disown(os, zvol_tag);
474fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
475fa9e4066Sahrens 		return (ENXIO);
476fa9e4066Sahrens 	}
477fa9e4066Sahrens 
478fa9e4066Sahrens 	if (ddi_soft_state_zalloc(zvol_state, minor) != DDI_SUCCESS) {
479503ad85cSMatthew Ahrens 		dmu_objset_disown(os, zvol_tag);
480fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
481fa9e4066Sahrens 		return (EAGAIN);
482fa9e4066Sahrens 	}
483e9dbad6fSeschrock 	(void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME,
484e9dbad6fSeschrock 	    (char *)name);
485fa9e4066Sahrens 
486681d9761SEric Taylor 	(void) snprintf(chrbuf, sizeof (chrbuf), "%u,raw", minor);
487fa9e4066Sahrens 
488fa9e4066Sahrens 	if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR,
489fa9e4066Sahrens 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
490fa9e4066Sahrens 		ddi_soft_state_free(zvol_state, minor);
491503ad85cSMatthew Ahrens 		dmu_objset_disown(os, zvol_tag);
492fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
493fa9e4066Sahrens 		return (EAGAIN);
494fa9e4066Sahrens 	}
495fa9e4066Sahrens 
496681d9761SEric Taylor 	(void) snprintf(blkbuf, sizeof (blkbuf), "%u", minor);
497fa9e4066Sahrens 
498fa9e4066Sahrens 	if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK,
499fa9e4066Sahrens 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
500fa9e4066Sahrens 		ddi_remove_minor_node(zfs_dip, chrbuf);
501fa9e4066Sahrens 		ddi_soft_state_free(zvol_state, minor);
502503ad85cSMatthew Ahrens 		dmu_objset_disown(os, zvol_tag);
503fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
504fa9e4066Sahrens 		return (EAGAIN);
505fa9e4066Sahrens 	}
506fa9e4066Sahrens 
507fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
508fa9e4066Sahrens 
509681d9761SEric Taylor 	(void) strlcpy(zv->zv_name, name, MAXPATHLEN);
510fa9e4066Sahrens 	zv->zv_min_bs = DEV_BSHIFT;
511fa9e4066Sahrens 	zv->zv_minor = minor;
512fa9e4066Sahrens 	zv->zv_objset = os;
513681d9761SEric Taylor 	if (dmu_objset_is_snapshot(os))
514681d9761SEric Taylor 		zv->zv_flags |= ZVOL_RDONLY;
515c2e6a7d6Sperrin 	mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
516c2e6a7d6Sperrin 	avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
517c2e6a7d6Sperrin 	    sizeof (rl_t), offsetof(rl_t, r_node));
51888b7b0f2SMatthew Ahrens 	list_create(&zv->zv_extents, sizeof (zvol_extent_t),
51988b7b0f2SMatthew Ahrens 	    offsetof(zvol_extent_t, ze_node));
52067bd71c6Sperrin 	/* get and cache the blocksize */
52167bd71c6Sperrin 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
52267bd71c6Sperrin 	ASSERT(error == 0);
52367bd71c6Sperrin 	zv->zv_volblocksize = doi.doi_data_block_size;
52422ac5be4Sperrin 
5251209a471SNeil Perrin 	zil_replay(os, zv, zvol_replay_vector);
526681d9761SEric Taylor 	dmu_objset_disown(os, zvol_tag);
527681d9761SEric Taylor 	zv->zv_objset = NULL;
528fa9e4066Sahrens 
529fa9e4066Sahrens 	zvol_minors++;
530fa9e4066Sahrens 
531fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
532fa9e4066Sahrens 
533fa9e4066Sahrens 	return (0);
534fa9e4066Sahrens }
535fa9e4066Sahrens 
536fa9e4066Sahrens /*
537fa9e4066Sahrens  * Remove minor node for the specified volume.
538fa9e4066Sahrens  */
539681d9761SEric Taylor static int
540681d9761SEric Taylor zvol_remove_zv(zvol_state_t *zv)
541681d9761SEric Taylor {
542681d9761SEric Taylor 	char nmbuf[20];
543681d9761SEric Taylor 
544681d9761SEric Taylor 	ASSERT(MUTEX_HELD(&zvol_state_lock));
545681d9761SEric Taylor 	if (zv->zv_total_opens != 0)
546681d9761SEric Taylor 		return (EBUSY);
547681d9761SEric Taylor 
548681d9761SEric Taylor 	(void) snprintf(nmbuf, sizeof (nmbuf), "%u,raw", zv->zv_minor);
549681d9761SEric Taylor 	ddi_remove_minor_node(zfs_dip, nmbuf);
550681d9761SEric Taylor 
551681d9761SEric Taylor 	(void) snprintf(nmbuf, sizeof (nmbuf), "%u", zv->zv_minor);
552681d9761SEric Taylor 	ddi_remove_minor_node(zfs_dip, nmbuf);
553681d9761SEric Taylor 
554681d9761SEric Taylor 	avl_destroy(&zv->zv_znode.z_range_avl);
555681d9761SEric Taylor 	mutex_destroy(&zv->zv_znode.z_range_lock);
556681d9761SEric Taylor 
557681d9761SEric Taylor 	ddi_soft_state_free(zvol_state, zv->zv_minor);
558681d9761SEric Taylor 
559681d9761SEric Taylor 	zvol_minors--;
560681d9761SEric Taylor 	return (0);
561681d9761SEric Taylor }
562681d9761SEric Taylor 
563fa9e4066Sahrens int
564e9dbad6fSeschrock zvol_remove_minor(const char *name)
565fa9e4066Sahrens {
566fa9e4066Sahrens 	zvol_state_t *zv;
567681d9761SEric Taylor 	int rc;
568fa9e4066Sahrens 
569fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
570e9dbad6fSeschrock 	if ((zv = zvol_minor_lookup(name)) == NULL) {
571fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
572fa9e4066Sahrens 		return (ENXIO);
573fa9e4066Sahrens 	}
574681d9761SEric Taylor 	rc = zvol_remove_zv(zv);
575681d9761SEric Taylor 	mutex_exit(&zvol_state_lock);
576681d9761SEric Taylor 	return (rc);
577681d9761SEric Taylor }
578fa9e4066Sahrens 
579681d9761SEric Taylor int
580681d9761SEric Taylor zvol_first_open(zvol_state_t *zv)
581681d9761SEric Taylor {
582681d9761SEric Taylor 	objset_t *os;
583681d9761SEric Taylor 	uint64_t volsize;
584681d9761SEric Taylor 	int error;
585681d9761SEric Taylor 	uint64_t readonly;
586fa9e4066Sahrens 
587681d9761SEric Taylor 	/* lie and say we're read-only */
588681d9761SEric Taylor 	error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, B_TRUE,
589681d9761SEric Taylor 	    zvol_tag, &os);
590681d9761SEric Taylor 	if (error)
591681d9761SEric Taylor 		return (error);
592fa9e4066Sahrens 
593681d9761SEric Taylor 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
594681d9761SEric Taylor 	if (error) {
595681d9761SEric Taylor 		ASSERT(error == 0);
596681d9761SEric Taylor 		dmu_objset_disown(os, zvol_tag);
597681d9761SEric Taylor 		return (error);
598681d9761SEric Taylor 	}
599681d9761SEric Taylor 	zv->zv_objset = os;
600681d9761SEric Taylor 	zv->zv_volsize = volsize;
601681d9761SEric Taylor 	zv->zv_zilog = zil_open(os, zvol_get_data);
602681d9761SEric Taylor 	zvol_size_changed(zv->zv_volsize, ddi_driver_major(zfs_dip),
603681d9761SEric Taylor 	    zv->zv_minor);
604fa9e4066Sahrens 
605681d9761SEric Taylor 	VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &readonly,
606681d9761SEric Taylor 	    NULL) == 0);
607681d9761SEric Taylor 	if (readonly || dmu_objset_is_snapshot(os))
608681d9761SEric Taylor 		zv->zv_flags |= ZVOL_RDONLY;
609681d9761SEric Taylor 	else
610681d9761SEric Taylor 		zv->zv_flags &= ~ZVOL_RDONLY;
611681d9761SEric Taylor 	return (error);
612681d9761SEric Taylor }
613fa9e4066Sahrens 
614681d9761SEric Taylor void
615681d9761SEric Taylor zvol_last_close(zvol_state_t *zv)
616681d9761SEric Taylor {
61722ac5be4Sperrin 	zil_close(zv->zv_zilog);
61822ac5be4Sperrin 	zv->zv_zilog = NULL;
619503ad85cSMatthew Ahrens 	dmu_objset_disown(zv->zv_objset, zvol_tag);
620fa9e4066Sahrens 	zv->zv_objset = NULL;
621fa9e4066Sahrens }
622fa9e4066Sahrens 
623e7cbe64fSgw int
624e7cbe64fSgw zvol_prealloc(zvol_state_t *zv)
625e7cbe64fSgw {
626e7cbe64fSgw 	objset_t *os = zv->zv_objset;
627e7cbe64fSgw 	dmu_tx_t *tx;
628e7cbe64fSgw 	uint64_t refd, avail, usedobjs, availobjs;
629e7cbe64fSgw 	uint64_t resid = zv->zv_volsize;
630e7cbe64fSgw 	uint64_t off = 0;
631e7cbe64fSgw 
632e7cbe64fSgw 	/* Check the space usage before attempting to allocate the space */
633e7cbe64fSgw 	dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs);
634e7cbe64fSgw 	if (avail < zv->zv_volsize)
635e7cbe64fSgw 		return (ENOSPC);
636e7cbe64fSgw 
637e7cbe64fSgw 	/* Free old extents if they exist */
638e7cbe64fSgw 	zvol_free_extents(zv);
639e7cbe64fSgw 
640e7cbe64fSgw 	while (resid != 0) {
641e7cbe64fSgw 		int error;
642e7cbe64fSgw 		uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE);
643e7cbe64fSgw 
644e7cbe64fSgw 		tx = dmu_tx_create(os);
645e7cbe64fSgw 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
646e7cbe64fSgw 		error = dmu_tx_assign(tx, TXG_WAIT);
647e7cbe64fSgw 		if (error) {
648e7cbe64fSgw 			dmu_tx_abort(tx);
649cdb0ab79Smaybee 			(void) dmu_free_long_range(os, ZVOL_OBJ, 0, off);
650e7cbe64fSgw 			return (error);
651e7cbe64fSgw 		}
65282c9918fSTim Haley 		dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx);
653e7cbe64fSgw 		dmu_tx_commit(tx);
654e7cbe64fSgw 		off += bytes;
655e7cbe64fSgw 		resid -= bytes;
656e7cbe64fSgw 	}
657e7cbe64fSgw 	txg_wait_synced(dmu_objset_pool(os), 0);
658e7cbe64fSgw 
659e7cbe64fSgw 	return (0);
660e7cbe64fSgw }
661e7cbe64fSgw 
662e7cbe64fSgw int
663681d9761SEric Taylor zvol_update_volsize(objset_t *os, uint64_t volsize)
664e7cbe64fSgw {
665e7cbe64fSgw 	dmu_tx_t *tx;
666e7cbe64fSgw 	int error;
667e7cbe64fSgw 
668e7cbe64fSgw 	ASSERT(MUTEX_HELD(&zvol_state_lock));
669e7cbe64fSgw 
670681d9761SEric Taylor 	tx = dmu_tx_create(os);
671e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
672e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
673e7cbe64fSgw 	if (error) {
674e7cbe64fSgw 		dmu_tx_abort(tx);
675e7cbe64fSgw 		return (error);
676e7cbe64fSgw 	}
677e7cbe64fSgw 
678681d9761SEric Taylor 	error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
679e7cbe64fSgw 	    &volsize, tx);
680e7cbe64fSgw 	dmu_tx_commit(tx);
681e7cbe64fSgw 
682e7cbe64fSgw 	if (error == 0)
683681d9761SEric Taylor 		error = dmu_free_long_range(os,
684cdb0ab79Smaybee 		    ZVOL_OBJ, volsize, DMU_OBJECT_END);
685681d9761SEric Taylor 	return (error);
686681d9761SEric Taylor }
687e7cbe64fSgw 
688681d9761SEric Taylor void
689681d9761SEric Taylor zvol_remove_minors(const char *name)
690681d9761SEric Taylor {
691681d9761SEric Taylor 	zvol_state_t *zv;
692681d9761SEric Taylor 	char *namebuf;
693681d9761SEric Taylor 	minor_t minor;
694681d9761SEric Taylor 
695681d9761SEric Taylor 	namebuf = kmem_zalloc(strlen(name) + 2, KM_SLEEP);
696681d9761SEric Taylor 	(void) strncpy(namebuf, name, strlen(name));
697681d9761SEric Taylor 	(void) strcat(namebuf, "/");
698681d9761SEric Taylor 	mutex_enter(&zvol_state_lock);
699681d9761SEric Taylor 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) {
700681d9761SEric Taylor 
701681d9761SEric Taylor 		zv = ddi_get_soft_state(zvol_state, minor);
702681d9761SEric Taylor 		if (zv == NULL)
703681d9761SEric Taylor 			continue;
704681d9761SEric Taylor 		if (strncmp(namebuf, zv->zv_name, strlen(namebuf)) == 0)
705681d9761SEric Taylor 			(void) zvol_remove_zv(zv);
706e7cbe64fSgw 	}
707681d9761SEric Taylor 	kmem_free(namebuf, strlen(name) + 2);
708681d9761SEric Taylor 
709681d9761SEric Taylor 	mutex_exit(&zvol_state_lock);
710e7cbe64fSgw }
711e7cbe64fSgw 
712fa9e4066Sahrens int
71391ebeef5Sahrens zvol_set_volsize(const char *name, major_t maj, uint64_t volsize)
714fa9e4066Sahrens {
715681d9761SEric Taylor 	zvol_state_t *zv = NULL;
716681d9761SEric Taylor 	objset_t *os;
717fa9e4066Sahrens 	int error;
7185c5460e9Seschrock 	dmu_object_info_t doi;
719e7cbe64fSgw 	uint64_t old_volsize = 0ULL;
720681d9761SEric Taylor 	uint64_t readonly;
721fa9e4066Sahrens 
722fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
723681d9761SEric Taylor 	zv = zvol_minor_lookup(name);
724681d9761SEric Taylor 	if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) {
725681d9761SEric Taylor 		mutex_exit(&zvol_state_lock);
726681d9761SEric Taylor 		return (error);
727fa9e4066Sahrens 	}
728fa9e4066Sahrens 
729681d9761SEric Taylor 	if ((error = dmu_object_info(os, ZVOL_OBJ, &doi)) != 0 ||
730e9dbad6fSeschrock 	    (error = zvol_check_volsize(volsize,
731bb0ade09Sahrens 	    doi.doi_data_block_size)) != 0)
732bb0ade09Sahrens 		goto out;
7335c5460e9Seschrock 
734681d9761SEric Taylor 	VERIFY(dsl_prop_get_integer(name, "readonly", &readonly,
735681d9761SEric Taylor 	    NULL) == 0);
736681d9761SEric Taylor 	if (readonly) {
737bb0ade09Sahrens 		error = EROFS;
738bb0ade09Sahrens 		goto out;
739fa9e4066Sahrens 	}
740fa9e4066Sahrens 
741681d9761SEric Taylor 	error = zvol_update_volsize(os, volsize);
742e7cbe64fSgw 	/*
743e7cbe64fSgw 	 * Reinitialize the dump area to the new size. If we
744681d9761SEric Taylor 	 * failed to resize the dump area then restore it back to
745681d9761SEric Taylor 	 * its original size.
746e7cbe64fSgw 	 */
747681d9761SEric Taylor 	if (zv && error == 0) {
748681d9761SEric Taylor 		if (zv->zv_flags & ZVOL_DUMPIFIED) {
749681d9761SEric Taylor 			old_volsize = zv->zv_volsize;
750681d9761SEric Taylor 			zv->zv_volsize = volsize;
751681d9761SEric Taylor 			if ((error = zvol_dumpify(zv)) != 0 ||
752681d9761SEric Taylor 			    (error = dumpvp_resize()) != 0) {
753681d9761SEric Taylor 				(void) zvol_update_volsize(os, old_volsize);
754681d9761SEric Taylor 				zv->zv_volsize = old_volsize;
755681d9761SEric Taylor 				error = zvol_dumpify(zv);
756681d9761SEric Taylor 			}
757681d9761SEric Taylor 		}
758681d9761SEric Taylor 		if (error == 0) {
759681d9761SEric Taylor 			zv->zv_volsize = volsize;
760681d9761SEric Taylor 			zvol_size_changed(volsize, maj, zv->zv_minor);
761e7cbe64fSgw 		}
762fa9e4066Sahrens 	}
763fa9e4066Sahrens 
764573ca77eSGeorge Wilson 	/*
765573ca77eSGeorge Wilson 	 * Generate a LUN expansion event.
766573ca77eSGeorge Wilson 	 */
767681d9761SEric Taylor 	if (zv && error == 0) {
768573ca77eSGeorge Wilson 		sysevent_id_t eid;
769573ca77eSGeorge Wilson 		nvlist_t *attr;
770573ca77eSGeorge Wilson 		char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
771573ca77eSGeorge Wilson 
772681d9761SEric Taylor 		(void) snprintf(physpath, MAXPATHLEN, "%s%u", ZVOL_PSEUDO_DEV,
773573ca77eSGeorge Wilson 		    zv->zv_minor);
774573ca77eSGeorge Wilson 
775573ca77eSGeorge Wilson 		VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
776573ca77eSGeorge Wilson 		VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
777573ca77eSGeorge Wilson 
778573ca77eSGeorge Wilson 		(void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
779573ca77eSGeorge Wilson 		    ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
780573ca77eSGeorge Wilson 
781573ca77eSGeorge Wilson 		nvlist_free(attr);
782573ca77eSGeorge Wilson 		kmem_free(physpath, MAXPATHLEN);
783573ca77eSGeorge Wilson 	}
784573ca77eSGeorge Wilson 
785bb0ade09Sahrens out:
786681d9761SEric Taylor 	dmu_objset_rele(os, FTAG);
787bb0ade09Sahrens 
788fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
789fa9e4066Sahrens 
790fa9e4066Sahrens 	return (error);
791fa9e4066Sahrens }
792fa9e4066Sahrens 
793fa9e4066Sahrens /*ARGSUSED*/
794fa9e4066Sahrens int
795fa9e4066Sahrens zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr)
796fa9e4066Sahrens {
797fa9e4066Sahrens 	minor_t minor = getminor(*devp);
798fa9e4066Sahrens 	zvol_state_t *zv;
799681d9761SEric Taylor 	int err = 0;
800fa9e4066Sahrens 
801fa9e4066Sahrens 	if (minor == 0)			/* This is the control device */
802fa9e4066Sahrens 		return (0);
803fa9e4066Sahrens 
804fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
805fa9e4066Sahrens 
806fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
807fa9e4066Sahrens 	if (zv == NULL) {
808fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
809fa9e4066Sahrens 		return (ENXIO);
810fa9e4066Sahrens 	}
811fa9e4066Sahrens 
812681d9761SEric Taylor 	if (zv->zv_total_opens == 0)
813681d9761SEric Taylor 		err = zvol_first_open(zv);
814681d9761SEric Taylor 	if (err) {
815fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
816681d9761SEric Taylor 		return (err);
817681d9761SEric Taylor 	}
818681d9761SEric Taylor 	if ((flag & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
819681d9761SEric Taylor 		err = EROFS;
820681d9761SEric Taylor 		goto out;
821fa9e4066Sahrens 	}
822c7f714e2SEric Taylor 	if (zv->zv_flags & ZVOL_EXCL) {
823681d9761SEric Taylor 		err = EBUSY;
824681d9761SEric Taylor 		goto out;
825c7f714e2SEric Taylor 	}
826c7f714e2SEric Taylor 	if (flag & FEXCL) {
827c7f714e2SEric Taylor 		if (zv->zv_total_opens != 0) {
828681d9761SEric Taylor 			err = EBUSY;
829681d9761SEric Taylor 			goto out;
830c7f714e2SEric Taylor 		}
831c7f714e2SEric Taylor 		zv->zv_flags |= ZVOL_EXCL;
832c7f714e2SEric Taylor 	}
833fa9e4066Sahrens 
834fa9e4066Sahrens 	if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) {
835fa9e4066Sahrens 		zv->zv_open_count[otyp]++;
836fa9e4066Sahrens 		zv->zv_total_opens++;
837fa9e4066Sahrens 	}
838fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
839fa9e4066Sahrens 
840681d9761SEric Taylor 	return (err);
841681d9761SEric Taylor out:
842681d9761SEric Taylor 	if (zv->zv_total_opens == 0)
843681d9761SEric Taylor 		zvol_last_close(zv);
844681d9761SEric Taylor 	mutex_exit(&zvol_state_lock);
845681d9761SEric Taylor 	return (err);
846fa9e4066Sahrens }
847fa9e4066Sahrens 
848fa9e4066Sahrens /*ARGSUSED*/
849fa9e4066Sahrens int
850fa9e4066Sahrens zvol_close(dev_t dev, int flag, int otyp, cred_t *cr)
851fa9e4066Sahrens {
852fa9e4066Sahrens 	minor_t minor = getminor(dev);
853fa9e4066Sahrens 	zvol_state_t *zv;
854681d9761SEric Taylor 	int error = 0;
855fa9e4066Sahrens 
856fa9e4066Sahrens 	if (minor == 0)		/* This is the control device */
857fa9e4066Sahrens 		return (0);
858fa9e4066Sahrens 
859fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
860fa9e4066Sahrens 
861fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
862fa9e4066Sahrens 	if (zv == NULL) {
863fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
864fa9e4066Sahrens 		return (ENXIO);
865fa9e4066Sahrens 	}
866fa9e4066Sahrens 
867c7f714e2SEric Taylor 	if (zv->zv_flags & ZVOL_EXCL) {
868c7f714e2SEric Taylor 		ASSERT(zv->zv_total_opens == 1);
869c7f714e2SEric Taylor 		zv->zv_flags &= ~ZVOL_EXCL;
870fa9e4066Sahrens 	}
871fa9e4066Sahrens 
872fa9e4066Sahrens 	/*
873fa9e4066Sahrens 	 * If the open count is zero, this is a spurious close.
874fa9e4066Sahrens 	 * That indicates a bug in the kernel / DDI framework.
875fa9e4066Sahrens 	 */
876fa9e4066Sahrens 	ASSERT(zv->zv_open_count[otyp] != 0);
877fa9e4066Sahrens 	ASSERT(zv->zv_total_opens != 0);
878fa9e4066Sahrens 
879fa9e4066Sahrens 	/*
880fa9e4066Sahrens 	 * You may get multiple opens, but only one close.
881fa9e4066Sahrens 	 */
882fa9e4066Sahrens 	zv->zv_open_count[otyp]--;
883fa9e4066Sahrens 	zv->zv_total_opens--;
884fa9e4066Sahrens 
885681d9761SEric Taylor 	if (zv->zv_total_opens == 0)
886681d9761SEric Taylor 		zvol_last_close(zv);
887fa9e4066Sahrens 
888681d9761SEric Taylor 	mutex_exit(&zvol_state_lock);
889681d9761SEric Taylor 	return (error);
890fa9e4066Sahrens }
891fa9e4066Sahrens 
892feb08c6bSbillm static void
893b24ab676SJeff Bonwick zvol_get_done(zgd_t *zgd, int error)
89467bd71c6Sperrin {
895b24ab676SJeff Bonwick 	if (zgd->zgd_db)
896b24ab676SJeff Bonwick 		dmu_buf_rele(zgd->zgd_db, zgd);
897b24ab676SJeff Bonwick 
898b24ab676SJeff Bonwick 	zfs_range_unlock(zgd->zgd_rl);
899b24ab676SJeff Bonwick 
900b24ab676SJeff Bonwick 	if (error == 0 && zgd->zgd_bp)
901b24ab676SJeff Bonwick 		zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
90267bd71c6Sperrin 
90367bd71c6Sperrin 	kmem_free(zgd, sizeof (zgd_t));
90467bd71c6Sperrin }
90567bd71c6Sperrin 
90667bd71c6Sperrin /*
90767bd71c6Sperrin  * Get data to generate a TX_WRITE intent log record.
90867bd71c6Sperrin  */
909feb08c6bSbillm static int
91067bd71c6Sperrin zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
91167bd71c6Sperrin {
91267bd71c6Sperrin 	zvol_state_t *zv = arg;
91367bd71c6Sperrin 	objset_t *os = zv->zv_objset;
914b24ab676SJeff Bonwick 	uint64_t object = ZVOL_OBJ;
915b24ab676SJeff Bonwick 	uint64_t offset = lr->lr_offset;
916b24ab676SJeff Bonwick 	uint64_t size = lr->lr_length;	/* length of user data */
917b24ab676SJeff Bonwick 	blkptr_t *bp = &lr->lr_blkptr;
91867bd71c6Sperrin 	dmu_buf_t *db;
91967bd71c6Sperrin 	zgd_t *zgd;
92067bd71c6Sperrin 	int error;
92167bd71c6Sperrin 
922b24ab676SJeff Bonwick 	ASSERT(zio != NULL);
923b24ab676SJeff Bonwick 	ASSERT(size != 0);
924b24ab676SJeff Bonwick 
925b24ab676SJeff Bonwick 	zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
926b24ab676SJeff Bonwick 	zgd->zgd_zilog = zv->zv_zilog;
927b24ab676SJeff Bonwick 	zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
928feb08c6bSbillm 
929c2e6a7d6Sperrin 	/*
930c2e6a7d6Sperrin 	 * Write records come in two flavors: immediate and indirect.
931c2e6a7d6Sperrin 	 * For small writes it's cheaper to store the data with the
932c2e6a7d6Sperrin 	 * log record (immediate); for large writes it's cheaper to
933c2e6a7d6Sperrin 	 * sync the data and get a pointer to it (indirect) so that
934c2e6a7d6Sperrin 	 * we don't have to write the data twice.
935c2e6a7d6Sperrin 	 */
936b24ab676SJeff Bonwick 	if (buf != NULL) {	/* immediate write */
937b24ab676SJeff Bonwick 		error = dmu_read(os, object, offset, size, buf,
938b24ab676SJeff Bonwick 		    DMU_READ_NO_PREFETCH);
939b24ab676SJeff Bonwick 	} else {
940b24ab676SJeff Bonwick 		size = zv->zv_volblocksize;
941b24ab676SJeff Bonwick 		offset = P2ALIGN(offset, size);
942b24ab676SJeff Bonwick 		error = dmu_buf_hold(os, object, offset, zgd, &db);
943b24ab676SJeff Bonwick 		if (error == 0) {
944b24ab676SJeff Bonwick 			zgd->zgd_db = db;
945b24ab676SJeff Bonwick 			zgd->zgd_bp = bp;
94667bd71c6Sperrin 
947b24ab676SJeff Bonwick 			ASSERT(db->db_offset == offset);
948b24ab676SJeff Bonwick 			ASSERT(db->db_size == size);
94967bd71c6Sperrin 
950b24ab676SJeff Bonwick 			error = dmu_sync(zio, lr->lr_common.lrc_txg,
951b24ab676SJeff Bonwick 			    zvol_get_done, zgd);
952975c32a0SNeil Perrin 
953b24ab676SJeff Bonwick 			if (error == 0)
954b24ab676SJeff Bonwick 				return (0);
955b24ab676SJeff Bonwick 		}
956975c32a0SNeil Perrin 	}
957975c32a0SNeil Perrin 
958b24ab676SJeff Bonwick 	zvol_get_done(zgd, error);
959b24ab676SJeff Bonwick 
96067bd71c6Sperrin 	return (error);
96167bd71c6Sperrin }
96267bd71c6Sperrin 
963a24e15ceSperrin /*
964a24e15ceSperrin  * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
96522ac5be4Sperrin  *
96622ac5be4Sperrin  * We store data in the log buffers if it's small enough.
96767bd71c6Sperrin  * Otherwise we will later flush the data out via dmu_sync().
96822ac5be4Sperrin  */
96967bd71c6Sperrin ssize_t zvol_immediate_write_sz = 32768;
97022ac5be4Sperrin 
971feb08c6bSbillm static void
972510b6c0eSNeil Perrin zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid,
973510b6c0eSNeil Perrin     boolean_t sync)
97422ac5be4Sperrin {
975feb08c6bSbillm 	uint32_t blocksize = zv->zv_volblocksize;
9761209a471SNeil Perrin 	zilog_t *zilog = zv->zv_zilog;
977510b6c0eSNeil Perrin 	boolean_t slogging;
978e09fa4daSNeil Perrin 	ssize_t immediate_write_sz;
979510b6c0eSNeil Perrin 
980510b6c0eSNeil Perrin 	if (zil_disable)
981510b6c0eSNeil Perrin 		return;
98222ac5be4Sperrin 
983b24ab676SJeff Bonwick 	if (zil_replaying(zilog, tx))
9841209a471SNeil Perrin 		return;
9851209a471SNeil Perrin 
986e09fa4daSNeil Perrin 	immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
987e09fa4daSNeil Perrin 	    ? 0 : zvol_immediate_write_sz;
988e09fa4daSNeil Perrin 
989e09fa4daSNeil Perrin 	slogging = spa_has_slogs(zilog->zl_spa) &&
990e09fa4daSNeil Perrin 	    (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
991feb08c6bSbillm 
992510b6c0eSNeil Perrin 	while (resid) {
993510b6c0eSNeil Perrin 		itx_t *itx;
994510b6c0eSNeil Perrin 		lr_write_t *lr;
995510b6c0eSNeil Perrin 		ssize_t len;
996510b6c0eSNeil Perrin 		itx_wr_state_t write_state;
997510b6c0eSNeil Perrin 
998510b6c0eSNeil Perrin 		/*
999510b6c0eSNeil Perrin 		 * Unlike zfs_log_write() we can be called with
1000510b6c0eSNeil Perrin 		 * upto DMU_MAX_ACCESS/2 (5MB) writes.
1001510b6c0eSNeil Perrin 		 */
1002e09fa4daSNeil Perrin 		if (blocksize > immediate_write_sz && !slogging &&
1003510b6c0eSNeil Perrin 		    resid >= blocksize && off % blocksize == 0) {
1004510b6c0eSNeil Perrin 			write_state = WR_INDIRECT; /* uses dmu_sync */
1005510b6c0eSNeil Perrin 			len = blocksize;
1006510b6c0eSNeil Perrin 		} else if (sync) {
1007510b6c0eSNeil Perrin 			write_state = WR_COPIED;
1008510b6c0eSNeil Perrin 			len = MIN(ZIL_MAX_LOG_DATA, resid);
1009510b6c0eSNeil Perrin 		} else {
1010510b6c0eSNeil Perrin 			write_state = WR_NEED_COPY;
1011510b6c0eSNeil Perrin 			len = MIN(ZIL_MAX_LOG_DATA, resid);
1012510b6c0eSNeil Perrin 		}
1013510b6c0eSNeil Perrin 
1014510b6c0eSNeil Perrin 		itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
1015510b6c0eSNeil Perrin 		    (write_state == WR_COPIED ? len : 0));
1016feb08c6bSbillm 		lr = (lr_write_t *)&itx->itx_lr;
1017510b6c0eSNeil Perrin 		if (write_state == WR_COPIED && dmu_read(zv->zv_objset,
10187bfdf011SNeil Perrin 		    ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
1019b24ab676SJeff Bonwick 			zil_itx_destroy(itx);
1020510b6c0eSNeil Perrin 			itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1021510b6c0eSNeil Perrin 			lr = (lr_write_t *)&itx->itx_lr;
1022510b6c0eSNeil Perrin 			write_state = WR_NEED_COPY;
1023510b6c0eSNeil Perrin 		}
1024510b6c0eSNeil Perrin 
1025510b6c0eSNeil Perrin 		itx->itx_wr_state = write_state;
1026510b6c0eSNeil Perrin 		if (write_state == WR_NEED_COPY)
1027510b6c0eSNeil Perrin 			itx->itx_sod += len;
1028feb08c6bSbillm 		lr->lr_foid = ZVOL_OBJ;
1029feb08c6bSbillm 		lr->lr_offset = off;
1030510b6c0eSNeil Perrin 		lr->lr_length = len;
1031b24ab676SJeff Bonwick 		lr->lr_blkoff = 0;
1032feb08c6bSbillm 		BP_ZERO(&lr->lr_blkptr);
1033feb08c6bSbillm 
1034510b6c0eSNeil Perrin 		itx->itx_private = zv;
1035510b6c0eSNeil Perrin 		itx->itx_sync = sync;
1036510b6c0eSNeil Perrin 
10371209a471SNeil Perrin 		(void) zil_itx_assign(zilog, itx, tx);
1038510b6c0eSNeil Perrin 
1039510b6c0eSNeil Perrin 		off += len;
1040510b6c0eSNeil Perrin 		resid -= len;
104122ac5be4Sperrin 	}
104222ac5be4Sperrin }
104322ac5be4Sperrin 
104488b7b0f2SMatthew Ahrens static int
104588b7b0f2SMatthew Ahrens zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t size,
104688b7b0f2SMatthew Ahrens     boolean_t doread, boolean_t isdump)
1047e7cbe64fSgw {
1048e7cbe64fSgw 	vdev_disk_t *dvd;
1049e7cbe64fSgw 	int c;
1050e7cbe64fSgw 	int numerrors = 0;
1051e7cbe64fSgw 
1052e7cbe64fSgw 	for (c = 0; c < vd->vdev_children; c++) {
105321ecdf64SLin Ling 		ASSERT(vd->vdev_ops == &vdev_mirror_ops ||
105421ecdf64SLin Ling 		    vd->vdev_ops == &vdev_replacing_ops ||
105521ecdf64SLin Ling 		    vd->vdev_ops == &vdev_spare_ops);
105688b7b0f2SMatthew Ahrens 		int err = zvol_dumpio_vdev(vd->vdev_child[c],
105788b7b0f2SMatthew Ahrens 		    addr, offset, size, doread, isdump);
105888b7b0f2SMatthew Ahrens 		if (err != 0) {
1059e7cbe64fSgw 			numerrors++;
106088b7b0f2SMatthew Ahrens 		} else if (doread) {
1061e7cbe64fSgw 			break;
1062e7cbe64fSgw 		}
1063e7cbe64fSgw 	}
1064e7cbe64fSgw 
1065e7cbe64fSgw 	if (!vd->vdev_ops->vdev_op_leaf)
1066e7cbe64fSgw 		return (numerrors < vd->vdev_children ? 0 : EIO);
1067e7cbe64fSgw 
1068dc0bb255SEric Taylor 	if (doread && !vdev_readable(vd))
1069dc0bb255SEric Taylor 		return (EIO);
1070dc0bb255SEric Taylor 	else if (!doread && !vdev_writeable(vd))
1071e7cbe64fSgw 		return (EIO);
1072e7cbe64fSgw 
1073e7cbe64fSgw 	dvd = vd->vdev_tsd;
1074e7cbe64fSgw 	ASSERT3P(dvd, !=, NULL);
1075e7cbe64fSgw 	offset += VDEV_LABEL_START_SIZE;
1076e7cbe64fSgw 
1077e7cbe64fSgw 	if (ddi_in_panic() || isdump) {
107888b7b0f2SMatthew Ahrens 		ASSERT(!doread);
107988b7b0f2SMatthew Ahrens 		if (doread)
1080e7cbe64fSgw 			return (EIO);
1081e7cbe64fSgw 		return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset),
1082e7cbe64fSgw 		    lbtodb(size)));
1083e7cbe64fSgw 	} else {
1084e7cbe64fSgw 		return (vdev_disk_physio(dvd->vd_lh, addr, size, offset,
108588b7b0f2SMatthew Ahrens 		    doread ? B_READ : B_WRITE));
1086e7cbe64fSgw 	}
1087e7cbe64fSgw }
1088e7cbe64fSgw 
108988b7b0f2SMatthew Ahrens static int
109088b7b0f2SMatthew Ahrens zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size,
109188b7b0f2SMatthew Ahrens     boolean_t doread, boolean_t isdump)
1092e7cbe64fSgw {
1093e7cbe64fSgw 	vdev_t *vd;
1094e7cbe64fSgw 	int error;
109588b7b0f2SMatthew Ahrens 	zvol_extent_t *ze;
1096e7cbe64fSgw 	spa_t *spa = dmu_objset_spa(zv->zv_objset);
1097e7cbe64fSgw 
109888b7b0f2SMatthew Ahrens 	/* Must be sector aligned, and not stradle a block boundary. */
109988b7b0f2SMatthew Ahrens 	if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) ||
110088b7b0f2SMatthew Ahrens 	    P2BOUNDARY(offset, size, zv->zv_volblocksize)) {
1101e7cbe64fSgw 		return (EINVAL);
110288b7b0f2SMatthew Ahrens 	}
110388b7b0f2SMatthew Ahrens 	ASSERT(size <= zv->zv_volblocksize);
1104e7cbe64fSgw 
110588b7b0f2SMatthew Ahrens 	/* Locate the extent this belongs to */
110688b7b0f2SMatthew Ahrens 	ze = list_head(&zv->zv_extents);
110788b7b0f2SMatthew Ahrens 	while (offset >= ze->ze_nblks * zv->zv_volblocksize) {
110888b7b0f2SMatthew Ahrens 		offset -= ze->ze_nblks * zv->zv_volblocksize;
110988b7b0f2SMatthew Ahrens 		ze = list_next(&zv->zv_extents, ze);
111088b7b0f2SMatthew Ahrens 	}
1111e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
111288b7b0f2SMatthew Ahrens 	vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva));
111388b7b0f2SMatthew Ahrens 	offset += DVA_GET_OFFSET(&ze->ze_dva);
111488b7b0f2SMatthew Ahrens 	error = zvol_dumpio_vdev(vd, addr, offset, size, doread, isdump);
1115e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
1116e7cbe64fSgw 	return (error);
1117e7cbe64fSgw }
1118e7cbe64fSgw 
1119fa9e4066Sahrens int
1120fa9e4066Sahrens zvol_strategy(buf_t *bp)
1121fa9e4066Sahrens {
1122fa9e4066Sahrens 	zvol_state_t *zv = ddi_get_soft_state(zvol_state, getminor(bp->b_edev));
1123fa9e4066Sahrens 	uint64_t off, volsize;
112488b7b0f2SMatthew Ahrens 	size_t resid;
1125fa9e4066Sahrens 	char *addr;
112622ac5be4Sperrin 	objset_t *os;
1127c2e6a7d6Sperrin 	rl_t *rl;
1128fa9e4066Sahrens 	int error = 0;
112988b7b0f2SMatthew Ahrens 	boolean_t doread = bp->b_flags & B_READ;
113088b7b0f2SMatthew Ahrens 	boolean_t is_dump = zv->zv_flags & ZVOL_DUMPIFIED;
1131510b6c0eSNeil Perrin 	boolean_t sync;
1132fa9e4066Sahrens 
1133fa9e4066Sahrens 	if (zv == NULL) {
1134fa9e4066Sahrens 		bioerror(bp, ENXIO);
1135fa9e4066Sahrens 		biodone(bp);
1136fa9e4066Sahrens 		return (0);
1137fa9e4066Sahrens 	}
1138fa9e4066Sahrens 
1139fa9e4066Sahrens 	if (getminor(bp->b_edev) == 0) {
1140fa9e4066Sahrens 		bioerror(bp, EINVAL);
1141fa9e4066Sahrens 		biodone(bp);
1142fa9e4066Sahrens 		return (0);
1143fa9e4066Sahrens 	}
1144fa9e4066Sahrens 
1145681d9761SEric Taylor 	if (!(bp->b_flags & B_READ) && (zv->zv_flags & ZVOL_RDONLY)) {
1146fa9e4066Sahrens 		bioerror(bp, EROFS);
1147fa9e4066Sahrens 		biodone(bp);
1148fa9e4066Sahrens 		return (0);
1149fa9e4066Sahrens 	}
1150fa9e4066Sahrens 
1151fa9e4066Sahrens 	off = ldbtob(bp->b_blkno);
1152fa9e4066Sahrens 	volsize = zv->zv_volsize;
1153fa9e4066Sahrens 
115422ac5be4Sperrin 	os = zv->zv_objset;
115522ac5be4Sperrin 	ASSERT(os != NULL);
1156fa9e4066Sahrens 
1157fa9e4066Sahrens 	bp_mapin(bp);
1158fa9e4066Sahrens 	addr = bp->b_un.b_addr;
1159fa9e4066Sahrens 	resid = bp->b_bcount;
1160fa9e4066Sahrens 
116188b7b0f2SMatthew Ahrens 	if (resid > 0 && (off < 0 || off >= volsize)) {
116288b7b0f2SMatthew Ahrens 		bioerror(bp, EIO);
116388b7b0f2SMatthew Ahrens 		biodone(bp);
116488b7b0f2SMatthew Ahrens 		return (0);
116588b7b0f2SMatthew Ahrens 	}
116673ec3d9cSgw 
1167510b6c0eSNeil Perrin 	sync = !(bp->b_flags & B_ASYNC) && !doread && !is_dump &&
1168510b6c0eSNeil Perrin 	    !(zv->zv_flags & ZVOL_WCE) && !zil_disable;
1169510b6c0eSNeil Perrin 
1170a24e15ceSperrin 	/*
1171a24e15ceSperrin 	 * There must be no buffer changes when doing a dmu_sync() because
1172a24e15ceSperrin 	 * we can't change the data whilst calculating the checksum.
1173a24e15ceSperrin 	 */
1174c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, off, resid,
117588b7b0f2SMatthew Ahrens 	    doread ? RL_READER : RL_WRITER);
1176fa9e4066Sahrens 
1177e7cbe64fSgw 	while (resid != 0 && off < volsize) {
117888b7b0f2SMatthew Ahrens 		size_t size = MIN(resid, zvol_maxphys);
1179e7cbe64fSgw 		if (is_dump) {
1180e7cbe64fSgw 			size = MIN(size, P2END(off, zv->zv_volblocksize) - off);
118188b7b0f2SMatthew Ahrens 			error = zvol_dumpio(zv, addr, off, size,
118288b7b0f2SMatthew Ahrens 			    doread, B_FALSE);
118388b7b0f2SMatthew Ahrens 		} else if (doread) {
11847bfdf011SNeil Perrin 			error = dmu_read(os, ZVOL_OBJ, off, size, addr,
11857bfdf011SNeil Perrin 			    DMU_READ_PREFETCH);
1186fa9e4066Sahrens 		} else {
118722ac5be4Sperrin 			dmu_tx_t *tx = dmu_tx_create(os);
1188fa9e4066Sahrens 			dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
1189fa9e4066Sahrens 			error = dmu_tx_assign(tx, TXG_WAIT);
1190fa9e4066Sahrens 			if (error) {
1191fa9e4066Sahrens 				dmu_tx_abort(tx);
1192fa9e4066Sahrens 			} else {
119322ac5be4Sperrin 				dmu_write(os, ZVOL_OBJ, off, size, addr, tx);
1194510b6c0eSNeil Perrin 				zvol_log_write(zv, tx, off, size, sync);
1195fa9e4066Sahrens 				dmu_tx_commit(tx);
1196fa9e4066Sahrens 			}
1197fa9e4066Sahrens 		}
1198b87f3af3Sperrin 		if (error) {
1199b87f3af3Sperrin 			/* convert checksum errors into IO errors */
1200b87f3af3Sperrin 			if (error == ECKSUM)
1201b87f3af3Sperrin 				error = EIO;
1202fa9e4066Sahrens 			break;
1203b87f3af3Sperrin 		}
1204fa9e4066Sahrens 		off += size;
1205fa9e4066Sahrens 		addr += size;
1206fa9e4066Sahrens 		resid -= size;
1207fa9e4066Sahrens 	}
1208c2e6a7d6Sperrin 	zfs_range_unlock(rl);
1209fa9e4066Sahrens 
1210fa9e4066Sahrens 	if ((bp->b_resid = resid) == bp->b_bcount)
1211fa9e4066Sahrens 		bioerror(bp, off > volsize ? EINVAL : error);
1212fa9e4066Sahrens 
1213510b6c0eSNeil Perrin 	if (sync)
1214feb08c6bSbillm 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1215feb08c6bSbillm 	biodone(bp);
121622ac5be4Sperrin 
1217fa9e4066Sahrens 	return (0);
1218fa9e4066Sahrens }
1219fa9e4066Sahrens 
122067bd71c6Sperrin /*
122167bd71c6Sperrin  * Set the buffer count to the zvol maximum transfer.
122267bd71c6Sperrin  * Using our own routine instead of the default minphys()
122367bd71c6Sperrin  * means that for larger writes we write bigger buffers on X86
122467bd71c6Sperrin  * (128K instead of 56K) and flush the disk write cache less often
122567bd71c6Sperrin  * (every zvol_maxphys - currently 1MB) instead of minphys (currently
122667bd71c6Sperrin  * 56K on X86 and 128K on sparc).
122767bd71c6Sperrin  */
122867bd71c6Sperrin void
122967bd71c6Sperrin zvol_minphys(struct buf *bp)
123067bd71c6Sperrin {
123167bd71c6Sperrin 	if (bp->b_bcount > zvol_maxphys)
123267bd71c6Sperrin 		bp->b_bcount = zvol_maxphys;
123367bd71c6Sperrin }
123467bd71c6Sperrin 
1235e7cbe64fSgw int
1236e7cbe64fSgw zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks)
1237e7cbe64fSgw {
1238e7cbe64fSgw 	minor_t minor = getminor(dev);
1239e7cbe64fSgw 	zvol_state_t *zv;
1240e7cbe64fSgw 	int error = 0;
1241e7cbe64fSgw 	uint64_t size;
1242e7cbe64fSgw 	uint64_t boff;
1243e7cbe64fSgw 	uint64_t resid;
1244e7cbe64fSgw 
1245e7cbe64fSgw 	if (minor == 0)			/* This is the control device */
1246e7cbe64fSgw 		return (ENXIO);
1247e7cbe64fSgw 
1248e7cbe64fSgw 	zv = ddi_get_soft_state(zvol_state, minor);
1249e7cbe64fSgw 	if (zv == NULL)
1250e7cbe64fSgw 		return (ENXIO);
1251e7cbe64fSgw 
1252e7cbe64fSgw 	boff = ldbtob(blkno);
1253e7cbe64fSgw 	resid = ldbtob(nblocks);
125488b7b0f2SMatthew Ahrens 
125588b7b0f2SMatthew Ahrens 	VERIFY3U(boff + resid, <=, zv->zv_volsize);
125688b7b0f2SMatthew Ahrens 
1257e7cbe64fSgw 	while (resid) {
1258e7cbe64fSgw 		size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff);
125988b7b0f2SMatthew Ahrens 		error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE);
1260e7cbe64fSgw 		if (error)
1261e7cbe64fSgw 			break;
1262e7cbe64fSgw 		boff += size;
1263e7cbe64fSgw 		addr += size;
1264e7cbe64fSgw 		resid -= size;
1265e7cbe64fSgw 	}
1266e7cbe64fSgw 
1267e7cbe64fSgw 	return (error);
1268e7cbe64fSgw }
1269e7cbe64fSgw 
1270fa9e4066Sahrens /*ARGSUSED*/
1271fa9e4066Sahrens int
1272feb08c6bSbillm zvol_read(dev_t dev, uio_t *uio, cred_t *cr)
1273fa9e4066Sahrens {
1274c7ca1008Sgw 	minor_t minor = getminor(dev);
1275c7ca1008Sgw 	zvol_state_t *zv;
127673ec3d9cSgw 	uint64_t volsize;
1277c2e6a7d6Sperrin 	rl_t *rl;
1278feb08c6bSbillm 	int error = 0;
1279fa9e4066Sahrens 
1280c7ca1008Sgw 	if (minor == 0)			/* This is the control device */
1281c7ca1008Sgw 		return (ENXIO);
1282c7ca1008Sgw 
1283c7ca1008Sgw 	zv = ddi_get_soft_state(zvol_state, minor);
1284c7ca1008Sgw 	if (zv == NULL)
1285c7ca1008Sgw 		return (ENXIO);
1286c7ca1008Sgw 
128773ec3d9cSgw 	volsize = zv->zv_volsize;
128873ec3d9cSgw 	if (uio->uio_resid > 0 &&
128973ec3d9cSgw 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
129073ec3d9cSgw 		return (EIO);
129173ec3d9cSgw 
129288b7b0f2SMatthew Ahrens 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
129388b7b0f2SMatthew Ahrens 		error = physio(zvol_strategy, NULL, dev, B_READ,
129488b7b0f2SMatthew Ahrens 		    zvol_minphys, uio);
129588b7b0f2SMatthew Ahrens 		return (error);
129688b7b0f2SMatthew Ahrens 	}
129788b7b0f2SMatthew Ahrens 
1298c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1299c2e6a7d6Sperrin 	    RL_READER);
130073ec3d9cSgw 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1301feb08c6bSbillm 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1302fa9e4066Sahrens 
130373ec3d9cSgw 		/* don't read past the end */
130473ec3d9cSgw 		if (bytes > volsize - uio->uio_loffset)
130573ec3d9cSgw 			bytes = volsize - uio->uio_loffset;
130673ec3d9cSgw 
1307feb08c6bSbillm 		error =  dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes);
1308b87f3af3Sperrin 		if (error) {
1309b87f3af3Sperrin 			/* convert checksum errors into IO errors */
1310b87f3af3Sperrin 			if (error == ECKSUM)
1311b87f3af3Sperrin 				error = EIO;
1312feb08c6bSbillm 			break;
1313b87f3af3Sperrin 		}
1314feb08c6bSbillm 	}
1315c2e6a7d6Sperrin 	zfs_range_unlock(rl);
1316feb08c6bSbillm 	return (error);
1317fa9e4066Sahrens }
1318fa9e4066Sahrens 
1319fa9e4066Sahrens /*ARGSUSED*/
1320fa9e4066Sahrens int
1321feb08c6bSbillm zvol_write(dev_t dev, uio_t *uio, cred_t *cr)
1322fa9e4066Sahrens {
1323c7ca1008Sgw 	minor_t minor = getminor(dev);
1324c7ca1008Sgw 	zvol_state_t *zv;
132573ec3d9cSgw 	uint64_t volsize;
1326c2e6a7d6Sperrin 	rl_t *rl;
1327feb08c6bSbillm 	int error = 0;
1328510b6c0eSNeil Perrin 	boolean_t sync;
1329feb08c6bSbillm 
1330c7ca1008Sgw 	if (minor == 0)			/* This is the control device */
1331c7ca1008Sgw 		return (ENXIO);
1332c7ca1008Sgw 
1333c7ca1008Sgw 	zv = ddi_get_soft_state(zvol_state, minor);
1334c7ca1008Sgw 	if (zv == NULL)
1335c7ca1008Sgw 		return (ENXIO);
1336c7ca1008Sgw 
133773ec3d9cSgw 	volsize = zv->zv_volsize;
133873ec3d9cSgw 	if (uio->uio_resid > 0 &&
133973ec3d9cSgw 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
134073ec3d9cSgw 		return (EIO);
134173ec3d9cSgw 
1342e7cbe64fSgw 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
1343e7cbe64fSgw 		error = physio(zvol_strategy, NULL, dev, B_WRITE,
1344e7cbe64fSgw 		    zvol_minphys, uio);
1345e7cbe64fSgw 		return (error);
1346e7cbe64fSgw 	}
1347e7cbe64fSgw 
1348510b6c0eSNeil Perrin 	sync = !(zv->zv_flags & ZVOL_WCE) && !zil_disable;
1349510b6c0eSNeil Perrin 
1350c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1351c2e6a7d6Sperrin 	    RL_WRITER);
135273ec3d9cSgw 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1353feb08c6bSbillm 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1354feb08c6bSbillm 		uint64_t off = uio->uio_loffset;
1355feb08c6bSbillm 		dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
135673ec3d9cSgw 
135773ec3d9cSgw 		if (bytes > volsize - off)	/* don't write past the end */
135873ec3d9cSgw 			bytes = volsize - off;
135973ec3d9cSgw 
1360feb08c6bSbillm 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
1361feb08c6bSbillm 		error = dmu_tx_assign(tx, TXG_WAIT);
1362feb08c6bSbillm 		if (error) {
1363feb08c6bSbillm 			dmu_tx_abort(tx);
1364feb08c6bSbillm 			break;
1365feb08c6bSbillm 		}
1366feb08c6bSbillm 		error = dmu_write_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes, tx);
1367feb08c6bSbillm 		if (error == 0)
1368510b6c0eSNeil Perrin 			zvol_log_write(zv, tx, off, bytes, sync);
1369feb08c6bSbillm 		dmu_tx_commit(tx);
1370feb08c6bSbillm 
1371feb08c6bSbillm 		if (error)
1372feb08c6bSbillm 			break;
1373feb08c6bSbillm 	}
1374c2e6a7d6Sperrin 	zfs_range_unlock(rl);
1375510b6c0eSNeil Perrin 	if (sync)
1376e08bf2c6SEric Taylor 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1377feb08c6bSbillm 	return (error);
1378fa9e4066Sahrens }
1379fa9e4066Sahrens 
1380c7f714e2SEric Taylor int
1381c7f714e2SEric Taylor zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs)
1382c7f714e2SEric Taylor {
1383c7f714e2SEric Taylor 	struct uuid uuid = EFI_RESERVED;
1384c7f714e2SEric Taylor 	efi_gpe_t gpe = { 0 };
1385c7f714e2SEric Taylor 	uint32_t crc;
1386c7f714e2SEric Taylor 	dk_efi_t efi;
1387c7f714e2SEric Taylor 	int length;
1388c7f714e2SEric Taylor 	char *ptr;
1389c7f714e2SEric Taylor 
1390c7f714e2SEric Taylor 	if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag))
1391c7f714e2SEric Taylor 		return (EFAULT);
1392c7f714e2SEric Taylor 	ptr = (char *)(uintptr_t)efi.dki_data_64;
1393c7f714e2SEric Taylor 	length = efi.dki_length;
1394c7f714e2SEric Taylor 	/*
1395c7f714e2SEric Taylor 	 * Some clients may attempt to request a PMBR for the
1396c7f714e2SEric Taylor 	 * zvol.  Currently this interface will return EINVAL to
1397c7f714e2SEric Taylor 	 * such requests.  These requests could be supported by
1398c7f714e2SEric Taylor 	 * adding a check for lba == 0 and consing up an appropriate
1399c7f714e2SEric Taylor 	 * PMBR.
1400c7f714e2SEric Taylor 	 */
1401c7f714e2SEric Taylor 	if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0)
1402c7f714e2SEric Taylor 		return (EINVAL);
1403c7f714e2SEric Taylor 
1404c7f714e2SEric Taylor 	gpe.efi_gpe_StartingLBA = LE_64(34ULL);
1405c7f714e2SEric Taylor 	gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1);
1406c7f714e2SEric Taylor 	UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
1407c7f714e2SEric Taylor 
1408c7f714e2SEric Taylor 	if (efi.dki_lba == 1) {
1409c7f714e2SEric Taylor 		efi_gpt_t gpt = { 0 };
1410c7f714e2SEric Taylor 
1411c7f714e2SEric Taylor 		gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
1412c7f714e2SEric Taylor 		gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
1413c7f714e2SEric Taylor 		gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt));
1414c7f714e2SEric Taylor 		gpt.efi_gpt_MyLBA = LE_64(1ULL);
1415c7f714e2SEric Taylor 		gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL);
1416c7f714e2SEric Taylor 		gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1);
1417c7f714e2SEric Taylor 		gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL);
1418c7f714e2SEric Taylor 		gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
1419c7f714e2SEric Taylor 		gpt.efi_gpt_SizeOfPartitionEntry =
1420c7f714e2SEric Taylor 		    LE_32(sizeof (efi_gpe_t));
1421c7f714e2SEric Taylor 		CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
1422c7f714e2SEric Taylor 		gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
1423c7f714e2SEric Taylor 		CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table);
1424c7f714e2SEric Taylor 		gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
1425c7f714e2SEric Taylor 		if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length),
1426c7f714e2SEric Taylor 		    flag))
1427c7f714e2SEric Taylor 			return (EFAULT);
1428c7f714e2SEric Taylor 		ptr += sizeof (gpt);
1429c7f714e2SEric Taylor 		length -= sizeof (gpt);
1430c7f714e2SEric Taylor 	}
1431c7f714e2SEric Taylor 	if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe),
1432c7f714e2SEric Taylor 	    length), flag))
1433c7f714e2SEric Taylor 		return (EFAULT);
1434c7f714e2SEric Taylor 	return (0);
1435c7f714e2SEric Taylor }
1436c7f714e2SEric Taylor 
1437fa9e4066Sahrens /*
1438fa9e4066Sahrens  * Dirtbag ioctls to support mkfs(1M) for UFS filesystems.  See dkio(7I).
1439fa9e4066Sahrens  */
1440fa9e4066Sahrens /*ARGSUSED*/
1441fa9e4066Sahrens int
1442fa9e4066Sahrens zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
1443fa9e4066Sahrens {
1444fa9e4066Sahrens 	zvol_state_t *zv;
1445af2c4821Smaybee 	struct dk_cinfo dki;
1446fa9e4066Sahrens 	struct dk_minfo dkm;
1447af2c4821Smaybee 	struct dk_callback *dkc;
1448fa9e4066Sahrens 	int error = 0;
1449e7cbe64fSgw 	rl_t *rl;
1450fa9e4066Sahrens 
1451fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
1452fa9e4066Sahrens 
1453fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, getminor(dev));
1454fa9e4066Sahrens 
1455fa9e4066Sahrens 	if (zv == NULL) {
1456fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
1457fa9e4066Sahrens 		return (ENXIO);
1458fa9e4066Sahrens 	}
1459701f66c4SEric Taylor 	ASSERT(zv->zv_total_opens > 0);
1460fa9e4066Sahrens 
1461fa9e4066Sahrens 	switch (cmd) {
1462fa9e4066Sahrens 
1463fa9e4066Sahrens 	case DKIOCINFO:
1464af2c4821Smaybee 		bzero(&dki, sizeof (dki));
1465af2c4821Smaybee 		(void) strcpy(dki.dki_cname, "zvol");
1466af2c4821Smaybee 		(void) strcpy(dki.dki_dname, "zvol");
1467af2c4821Smaybee 		dki.dki_ctype = DKC_UNKNOWN;
1468af2c4821Smaybee 		dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs);
1469fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
1470af2c4821Smaybee 		if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag))
1471fa9e4066Sahrens 			error = EFAULT;
1472fa9e4066Sahrens 		return (error);
1473fa9e4066Sahrens 
1474fa9e4066Sahrens 	case DKIOCGMEDIAINFO:
1475fa9e4066Sahrens 		bzero(&dkm, sizeof (dkm));
1476fa9e4066Sahrens 		dkm.dki_lbsize = 1U << zv->zv_min_bs;
1477fa9e4066Sahrens 		dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
1478fa9e4066Sahrens 		dkm.dki_media_type = DK_UNKNOWN;
1479fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
1480fa9e4066Sahrens 		if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
1481fa9e4066Sahrens 			error = EFAULT;
1482fa9e4066Sahrens 		return (error);
1483fa9e4066Sahrens 
1484fa9e4066Sahrens 	case DKIOCGETEFI:
1485c7f714e2SEric Taylor 		{
1486c7f714e2SEric Taylor 			uint64_t vs = zv->zv_volsize;
1487c7f714e2SEric Taylor 			uint8_t bs = zv->zv_min_bs;
1488fa9e4066Sahrens 
148968a5ac4dSmaybee 			mutex_exit(&zvol_state_lock);
1490c7f714e2SEric Taylor 			error = zvol_getefi((void *)arg, flag, vs, bs);
1491c7f714e2SEric Taylor 			return (error);
149268a5ac4dSmaybee 		}
1493fa9e4066Sahrens 
1494feb08c6bSbillm 	case DKIOCFLUSHWRITECACHE:
1495af2c4821Smaybee 		dkc = (struct dk_callback *)arg;
1496701f66c4SEric Taylor 		mutex_exit(&zvol_state_lock);
1497feb08c6bSbillm 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1498af2c4821Smaybee 		if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) {
1499af2c4821Smaybee 			(*dkc->dkc_callback)(dkc->dkc_cookie, error);
1500af2c4821Smaybee 			error = 0;
1501af2c4821Smaybee 		}
1502701f66c4SEric Taylor 		return (error);
1503701f66c4SEric Taylor 
1504701f66c4SEric Taylor 	case DKIOCGETWCE:
1505701f66c4SEric Taylor 		{
1506701f66c4SEric Taylor 			int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0;
1507701f66c4SEric Taylor 			if (ddi_copyout(&wce, (void *)arg, sizeof (int),
1508701f66c4SEric Taylor 			    flag))
1509701f66c4SEric Taylor 				error = EFAULT;
1510701f66c4SEric Taylor 			break;
1511701f66c4SEric Taylor 		}
1512701f66c4SEric Taylor 	case DKIOCSETWCE:
1513701f66c4SEric Taylor 		{
1514701f66c4SEric Taylor 			int wce;
1515701f66c4SEric Taylor 			if (ddi_copyin((void *)arg, &wce, sizeof (int),
1516701f66c4SEric Taylor 			    flag)) {
1517701f66c4SEric Taylor 				error = EFAULT;
1518701f66c4SEric Taylor 				break;
1519701f66c4SEric Taylor 			}
1520701f66c4SEric Taylor 			if (wce) {
1521701f66c4SEric Taylor 				zv->zv_flags |= ZVOL_WCE;
1522701f66c4SEric Taylor 				mutex_exit(&zvol_state_lock);
1523701f66c4SEric Taylor 			} else {
1524701f66c4SEric Taylor 				zv->zv_flags &= ~ZVOL_WCE;
1525701f66c4SEric Taylor 				mutex_exit(&zvol_state_lock);
1526701f66c4SEric Taylor 				zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1527701f66c4SEric Taylor 			}
1528701f66c4SEric Taylor 			return (0);
1529701f66c4SEric Taylor 		}
1530feb08c6bSbillm 
1531b6130eadSmaybee 	case DKIOCGGEOM:
1532b6130eadSmaybee 	case DKIOCGVTOC:
1533e7cbe64fSgw 		/*
1534e7cbe64fSgw 		 * commands using these (like prtvtoc) expect ENOTSUP
1535e7cbe64fSgw 		 * since we're emulating an EFI label
1536e7cbe64fSgw 		 */
1537b6130eadSmaybee 		error = ENOTSUP;
1538b6130eadSmaybee 		break;
1539b6130eadSmaybee 
1540e7cbe64fSgw 	case DKIOCDUMPINIT:
1541e7cbe64fSgw 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1542e7cbe64fSgw 		    RL_WRITER);
1543e7cbe64fSgw 		error = zvol_dumpify(zv);
1544e7cbe64fSgw 		zfs_range_unlock(rl);
1545e7cbe64fSgw 		break;
1546e7cbe64fSgw 
1547e7cbe64fSgw 	case DKIOCDUMPFINI:
154806d5ae10SEric Taylor 		if (!(zv->zv_flags & ZVOL_DUMPIFIED))
154906d5ae10SEric Taylor 			break;
1550e7cbe64fSgw 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1551e7cbe64fSgw 		    RL_WRITER);
1552e7cbe64fSgw 		error = zvol_dump_fini(zv);
1553e7cbe64fSgw 		zfs_range_unlock(rl);
1554e7cbe64fSgw 		break;
1555e7cbe64fSgw 
1556fa9e4066Sahrens 	default:
155768a5ac4dSmaybee 		error = ENOTTY;
1558fa9e4066Sahrens 		break;
1559fa9e4066Sahrens 
1560fa9e4066Sahrens 	}
1561fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
1562fa9e4066Sahrens 	return (error);
1563fa9e4066Sahrens }
1564fa9e4066Sahrens 
1565fa9e4066Sahrens int
1566fa9e4066Sahrens zvol_busy(void)
1567fa9e4066Sahrens {
1568fa9e4066Sahrens 	return (zvol_minors != 0);
1569fa9e4066Sahrens }
1570fa9e4066Sahrens 
1571fa9e4066Sahrens void
1572fa9e4066Sahrens zvol_init(void)
1573fa9e4066Sahrens {
1574fa9e4066Sahrens 	VERIFY(ddi_soft_state_init(&zvol_state, sizeof (zvol_state_t), 1) == 0);
1575fa9e4066Sahrens 	mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
1576fa9e4066Sahrens }
1577fa9e4066Sahrens 
1578fa9e4066Sahrens void
1579fa9e4066Sahrens zvol_fini(void)
1580fa9e4066Sahrens {
1581fa9e4066Sahrens 	mutex_destroy(&zvol_state_lock);
1582fa9e4066Sahrens 	ddi_soft_state_fini(&zvol_state);
1583fa9e4066Sahrens }
1584e7cbe64fSgw 
1585e7cbe64fSgw static int
1586e7cbe64fSgw zvol_dump_init(zvol_state_t *zv, boolean_t resize)
1587e7cbe64fSgw {
1588e7cbe64fSgw 	dmu_tx_t *tx;
1589e7cbe64fSgw 	int error = 0;
1590e7cbe64fSgw 	objset_t *os = zv->zv_objset;
1591e7cbe64fSgw 	nvlist_t *nv = NULL;
1592e7cbe64fSgw 
1593e7cbe64fSgw 	ASSERT(MUTEX_HELD(&zvol_state_lock));
1594681d9761SEric Taylor 	error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0,
1595681d9761SEric Taylor 	    DMU_OBJECT_END);
1596681d9761SEric Taylor 	/* wait for dmu_free_long_range to actually free the blocks */
1597681d9761SEric Taylor 	txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
1598e7cbe64fSgw 
1599e7cbe64fSgw 	tx = dmu_tx_create(os);
1600e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1601681d9761SEric Taylor 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
1602e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
1603e7cbe64fSgw 	if (error) {
1604e7cbe64fSgw 		dmu_tx_abort(tx);
1605e7cbe64fSgw 		return (error);
1606e7cbe64fSgw 	}
1607e7cbe64fSgw 
1608e7cbe64fSgw 	/*
1609e7cbe64fSgw 	 * If we are resizing the dump device then we only need to
1610e7cbe64fSgw 	 * update the refreservation to match the newly updated
1611e7cbe64fSgw 	 * zvolsize. Otherwise, we save off the original state of the
1612e7cbe64fSgw 	 * zvol so that we can restore them if the zvol is ever undumpified.
1613e7cbe64fSgw 	 */
1614e7cbe64fSgw 	if (resize) {
1615e7cbe64fSgw 		error = zap_update(os, ZVOL_ZAP_OBJ,
1616e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1617e7cbe64fSgw 		    &zv->zv_volsize, tx);
1618e7cbe64fSgw 	} else {
161988b7b0f2SMatthew Ahrens 		uint64_t checksum, compress, refresrv, vbs;
162088b7b0f2SMatthew Ahrens 
1621e7cbe64fSgw 		error = dsl_prop_get_integer(zv->zv_name,
1622e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL);
1623e7cbe64fSgw 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
1624e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL);
1625e7cbe64fSgw 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
1626e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL);
162788b7b0f2SMatthew Ahrens 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
162888b7b0f2SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL);
1629e7cbe64fSgw 
1630e7cbe64fSgw 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1631e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1,
1632e7cbe64fSgw 		    &compress, tx);
1633e7cbe64fSgw 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1634e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx);
1635e7cbe64fSgw 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1636e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1637e7cbe64fSgw 		    &refresrv, tx);
163888b7b0f2SMatthew Ahrens 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
163988b7b0f2SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1,
164088b7b0f2SMatthew Ahrens 		    &vbs, tx);
1641681d9761SEric Taylor 		error = error ? error : dmu_object_set_blocksize(
1642681d9761SEric Taylor 		    os, ZVOL_OBJ, SPA_MAXBLOCKSIZE, 0, tx);
1643681d9761SEric Taylor 		if (error == 0)
1644681d9761SEric Taylor 			zv->zv_volblocksize = SPA_MAXBLOCKSIZE;
1645e7cbe64fSgw 	}
1646e7cbe64fSgw 	dmu_tx_commit(tx);
1647e7cbe64fSgw 
1648e7cbe64fSgw 	/*
1649e7cbe64fSgw 	 * We only need update the zvol's property if we are initializing
1650e7cbe64fSgw 	 * the dump area for the first time.
1651e7cbe64fSgw 	 */
1652e7cbe64fSgw 	if (!resize) {
1653e7cbe64fSgw 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1654e7cbe64fSgw 		VERIFY(nvlist_add_uint64(nv,
1655e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0);
1656e7cbe64fSgw 		VERIFY(nvlist_add_uint64(nv,
1657e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
1658e7cbe64fSgw 		    ZIO_COMPRESS_OFF) == 0);
1659e7cbe64fSgw 		VERIFY(nvlist_add_uint64(nv,
1660e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
1661e7cbe64fSgw 		    ZIO_CHECKSUM_OFF) == 0);
1662e7cbe64fSgw 
1663*92241e0bSTom Erickson 		error = zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
1664*92241e0bSTom Erickson 		    nv, NULL);
1665e7cbe64fSgw 		nvlist_free(nv);
1666e7cbe64fSgw 
1667e7cbe64fSgw 		if (error)
1668e7cbe64fSgw 			return (error);
1669e7cbe64fSgw 	}
1670e7cbe64fSgw 
1671e7cbe64fSgw 	/* Allocate the space for the dump */
1672e7cbe64fSgw 	error = zvol_prealloc(zv);
1673e7cbe64fSgw 	return (error);
1674e7cbe64fSgw }
1675e7cbe64fSgw 
1676e7cbe64fSgw static int
1677e7cbe64fSgw zvol_dumpify(zvol_state_t *zv)
1678e7cbe64fSgw {
1679e7cbe64fSgw 	int error = 0;
1680e7cbe64fSgw 	uint64_t dumpsize = 0;
1681e7cbe64fSgw 	dmu_tx_t *tx;
1682e7cbe64fSgw 	objset_t *os = zv->zv_objset;
1683e7cbe64fSgw 
1684681d9761SEric Taylor 	if (zv->zv_flags & ZVOL_RDONLY)
1685e7cbe64fSgw 		return (EROFS);
1686e7cbe64fSgw 
1687e7cbe64fSgw 	if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE,
1688e7cbe64fSgw 	    8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) {
1689e7cbe64fSgw 		boolean_t resize = (dumpsize > 0) ? B_TRUE : B_FALSE;
1690e7cbe64fSgw 
1691e7cbe64fSgw 		if ((error = zvol_dump_init(zv, resize)) != 0) {
1692e7cbe64fSgw 			(void) zvol_dump_fini(zv);
1693e7cbe64fSgw 			return (error);
1694e7cbe64fSgw 		}
1695e7cbe64fSgw 	}
1696e7cbe64fSgw 
1697e7cbe64fSgw 	/*
1698e7cbe64fSgw 	 * Build up our lba mapping.
1699e7cbe64fSgw 	 */
1700e7cbe64fSgw 	error = zvol_get_lbas(zv);
1701e7cbe64fSgw 	if (error) {
1702e7cbe64fSgw 		(void) zvol_dump_fini(zv);
1703e7cbe64fSgw 		return (error);
1704e7cbe64fSgw 	}
1705e7cbe64fSgw 
1706e7cbe64fSgw 	tx = dmu_tx_create(os);
1707e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1708e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
1709e7cbe64fSgw 	if (error) {
1710e7cbe64fSgw 		dmu_tx_abort(tx);
1711e7cbe64fSgw 		(void) zvol_dump_fini(zv);
1712e7cbe64fSgw 		return (error);
1713e7cbe64fSgw 	}
1714e7cbe64fSgw 
1715e7cbe64fSgw 	zv->zv_flags |= ZVOL_DUMPIFIED;
1716e7cbe64fSgw 	error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1,
1717e7cbe64fSgw 	    &zv->zv_volsize, tx);
1718e7cbe64fSgw 	dmu_tx_commit(tx);
1719e7cbe64fSgw 
1720e7cbe64fSgw 	if (error) {
1721e7cbe64fSgw 		(void) zvol_dump_fini(zv);
1722e7cbe64fSgw 		return (error);
1723e7cbe64fSgw 	}
1724e7cbe64fSgw 
1725e7cbe64fSgw 	txg_wait_synced(dmu_objset_pool(os), 0);
1726e7cbe64fSgw 	return (0);
1727e7cbe64fSgw }
1728e7cbe64fSgw 
1729e7cbe64fSgw static int
1730e7cbe64fSgw zvol_dump_fini(zvol_state_t *zv)
1731e7cbe64fSgw {
1732e7cbe64fSgw 	dmu_tx_t *tx;
1733e7cbe64fSgw 	objset_t *os = zv->zv_objset;
1734e7cbe64fSgw 	nvlist_t *nv;
1735e7cbe64fSgw 	int error = 0;
173688b7b0f2SMatthew Ahrens 	uint64_t checksum, compress, refresrv, vbs;
1737e7cbe64fSgw 
1738b7e50089Smaybee 	/*
1739b7e50089Smaybee 	 * Attempt to restore the zvol back to its pre-dumpified state.
1740b7e50089Smaybee 	 * This is a best-effort attempt as it's possible that not all
1741b7e50089Smaybee 	 * of these properties were initialized during the dumpify process
1742b7e50089Smaybee 	 * (i.e. error during zvol_dump_init).
1743b7e50089Smaybee 	 */
1744b7e50089Smaybee 
1745e7cbe64fSgw 	tx = dmu_tx_create(os);
1746e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1747e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
1748e7cbe64fSgw 	if (error) {
1749e7cbe64fSgw 		dmu_tx_abort(tx);
1750e7cbe64fSgw 		return (error);
1751e7cbe64fSgw 	}
1752b7e50089Smaybee 	(void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx);
1753b7e50089Smaybee 	dmu_tx_commit(tx);
1754e7cbe64fSgw 
1755e7cbe64fSgw 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1756e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum);
1757e7cbe64fSgw 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1758e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress);
1759e7cbe64fSgw 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1760e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv);
176188b7b0f2SMatthew Ahrens 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
176288b7b0f2SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs);
1763e7cbe64fSgw 
1764e7cbe64fSgw 	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1765e7cbe64fSgw 	(void) nvlist_add_uint64(nv,
1766e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum);
1767e7cbe64fSgw 	(void) nvlist_add_uint64(nv,
1768e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress);
1769e7cbe64fSgw 	(void) nvlist_add_uint64(nv,
1770e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv);
1771*92241e0bSTom Erickson 	(void) zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
1772*92241e0bSTom Erickson 	    nv, NULL);
1773e7cbe64fSgw 	nvlist_free(nv);
1774e7cbe64fSgw 
1775b7e50089Smaybee 	zvol_free_extents(zv);
1776b7e50089Smaybee 	zv->zv_flags &= ~ZVOL_DUMPIFIED;
1777b7e50089Smaybee 	(void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END);
1778681d9761SEric Taylor 	/* wait for dmu_free_long_range to actually free the blocks */
1779681d9761SEric Taylor 	txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
1780681d9761SEric Taylor 	tx = dmu_tx_create(os);
1781681d9761SEric Taylor 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
1782681d9761SEric Taylor 	error = dmu_tx_assign(tx, TXG_WAIT);
1783681d9761SEric Taylor 	if (error) {
1784681d9761SEric Taylor 		dmu_tx_abort(tx);
1785681d9761SEric Taylor 		return (error);
1786681d9761SEric Taylor 	}
1787b24ab676SJeff Bonwick 	if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0)
1788b24ab676SJeff Bonwick 		zv->zv_volblocksize = vbs;
1789681d9761SEric Taylor 	dmu_tx_commit(tx);
1790b7e50089Smaybee 
1791e7cbe64fSgw 	return (0);
1792e7cbe64fSgw }
1793