xref: /illumos-gate/usr/src/uts/common/fs/zfs/zvol.c (revision 701f66c40463dddef764d4cac0cff6c8c33a6675)
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  *
35fa9e4066Sahrens  * These links are created by the ZFS-specific devfsadm link generator.
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;
83fa9e4066Sahrens 
84e7cbe64fSgw #define	ZVOL_DUMPSIZE		"dumpsize"
85e7cbe64fSgw 
86fa9e4066Sahrens /*
87fa9e4066Sahrens  * This lock protects the zvol_state structure from being modified
88fa9e4066Sahrens  * while it's being used, e.g. an open that comes in before a create
89fa9e4066Sahrens  * finishes.  It also protects temporary opens of the dataset so that,
90fa9e4066Sahrens  * e.g., an open doesn't get a spurious EBUSY.
91fa9e4066Sahrens  */
92fa9e4066Sahrens static kmutex_t zvol_state_lock;
93fa9e4066Sahrens static uint32_t zvol_minors;
94fa9e4066Sahrens 
95e7cbe64fSgw typedef struct zvol_extent {
9688b7b0f2SMatthew Ahrens 	list_node_t	ze_node;
97e7cbe64fSgw 	dva_t		ze_dva;		/* dva associated with this extent */
9888b7b0f2SMatthew Ahrens 	uint64_t	ze_nblks;	/* number of blocks in extent */
99e7cbe64fSgw } zvol_extent_t;
100e7cbe64fSgw 
101fa9e4066Sahrens /*
102fa9e4066Sahrens  * The in-core state of each volume.
103fa9e4066Sahrens  */
104fa9e4066Sahrens typedef struct zvol_state {
105fa9e4066Sahrens 	char		zv_name[MAXPATHLEN]; /* pool/dd name */
106fa9e4066Sahrens 	uint64_t	zv_volsize;	/* amount of space we advertise */
10767bd71c6Sperrin 	uint64_t	zv_volblocksize; /* volume block size */
108fa9e4066Sahrens 	minor_t		zv_minor;	/* minor number */
109fa9e4066Sahrens 	uint8_t		zv_min_bs;	/* minimum addressable block shift */
110*701f66c4SEric Taylor 	uint8_t		zv_flags;	/* readonly, dumpified, etc. */
111fa9e4066Sahrens 	objset_t	*zv_objset;	/* objset handle */
112fa9e4066Sahrens 	uint32_t	zv_mode;	/* DS_MODE_* flags at open time */
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
126*701f66c4SEric 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 
133e7cbe64fSgw extern int zfs_set_prop_nvlist(const char *, nvlist_t *);
134feb08c6bSbillm static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio);
135e7cbe64fSgw static int zvol_dumpify(zvol_state_t *zv);
136e7cbe64fSgw static int zvol_dump_fini(zvol_state_t *zv);
137e7cbe64fSgw static int zvol_dump_init(zvol_state_t *zv, boolean_t resize);
13867bd71c6Sperrin 
139fa9e4066Sahrens static void
14091ebeef5Sahrens zvol_size_changed(zvol_state_t *zv, major_t maj)
141fa9e4066Sahrens {
14291ebeef5Sahrens 	dev_t dev = makedevice(maj, zv->zv_minor);
143fa9e4066Sahrens 
144fa9e4066Sahrens 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
145fa9e4066Sahrens 	    "Size", zv->zv_volsize) == DDI_SUCCESS);
146fa9e4066Sahrens 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
147fa9e4066Sahrens 	    "Nblocks", lbtodb(zv->zv_volsize)) == DDI_SUCCESS);
148e7cbe64fSgw 
149e7cbe64fSgw 	/* Notify specfs to invalidate the cached size */
150e7cbe64fSgw 	spec_size_invalidate(dev, VBLK);
151e7cbe64fSgw 	spec_size_invalidate(dev, VCHR);
152fa9e4066Sahrens }
153fa9e4066Sahrens 
154fa9e4066Sahrens int
155e9dbad6fSeschrock zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
156fa9e4066Sahrens {
157e9dbad6fSeschrock 	if (volsize == 0)
158fa9e4066Sahrens 		return (EINVAL);
159fa9e4066Sahrens 
160e9dbad6fSeschrock 	if (volsize % blocksize != 0)
1615c5460e9Seschrock 		return (EINVAL);
1625c5460e9Seschrock 
163fa9e4066Sahrens #ifdef _ILP32
164e9dbad6fSeschrock 	if (volsize - 1 > SPEC_MAXOFFSET_T)
165fa9e4066Sahrens 		return (EOVERFLOW);
166fa9e4066Sahrens #endif
167fa9e4066Sahrens 	return (0);
168fa9e4066Sahrens }
169fa9e4066Sahrens 
170fa9e4066Sahrens int
171e9dbad6fSeschrock zvol_check_volblocksize(uint64_t volblocksize)
172fa9e4066Sahrens {
173e9dbad6fSeschrock 	if (volblocksize < SPA_MINBLOCKSIZE ||
174e9dbad6fSeschrock 	    volblocksize > SPA_MAXBLOCKSIZE ||
175e9dbad6fSeschrock 	    !ISP2(volblocksize))
176fa9e4066Sahrens 		return (EDOM);
177fa9e4066Sahrens 
178fa9e4066Sahrens 	return (0);
179fa9e4066Sahrens }
180fa9e4066Sahrens 
181fa9e4066Sahrens static void
182fa9e4066Sahrens zvol_readonly_changed_cb(void *arg, uint64_t newval)
183fa9e4066Sahrens {
184fa9e4066Sahrens 	zvol_state_t *zv = arg;
185fa9e4066Sahrens 
186e7cbe64fSgw 	if (newval)
187e7cbe64fSgw 		zv->zv_flags |= ZVOL_RDONLY;
188e7cbe64fSgw 	else
189e7cbe64fSgw 		zv->zv_flags &= ~ZVOL_RDONLY;
190fa9e4066Sahrens }
191fa9e4066Sahrens 
192fa9e4066Sahrens int
193a2eea2e1Sahrens zvol_get_stats(objset_t *os, nvlist_t *nv)
194fa9e4066Sahrens {
195fa9e4066Sahrens 	int error;
196fa9e4066Sahrens 	dmu_object_info_t doi;
197a2eea2e1Sahrens 	uint64_t val;
198fa9e4066Sahrens 
199fa9e4066Sahrens 
200a2eea2e1Sahrens 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
201fa9e4066Sahrens 	if (error)
202fa9e4066Sahrens 		return (error);
203fa9e4066Sahrens 
204a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
205a2eea2e1Sahrens 
206fa9e4066Sahrens 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
207fa9e4066Sahrens 
208a2eea2e1Sahrens 	if (error == 0) {
209a2eea2e1Sahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
210a2eea2e1Sahrens 		    doi.doi_data_block_size);
211a2eea2e1Sahrens 	}
212fa9e4066Sahrens 
213fa9e4066Sahrens 	return (error);
214fa9e4066Sahrens }
215fa9e4066Sahrens 
216fa9e4066Sahrens /*
217fa9e4066Sahrens  * Find a free minor number.
218fa9e4066Sahrens  */
219fa9e4066Sahrens static minor_t
220fa9e4066Sahrens zvol_minor_alloc(void)
221fa9e4066Sahrens {
222fa9e4066Sahrens 	minor_t minor;
223fa9e4066Sahrens 
224fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&zvol_state_lock));
225fa9e4066Sahrens 
226fa9e4066Sahrens 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++)
227fa9e4066Sahrens 		if (ddi_get_soft_state(zvol_state, minor) == NULL)
228fa9e4066Sahrens 			return (minor);
229fa9e4066Sahrens 
230fa9e4066Sahrens 	return (0);
231fa9e4066Sahrens }
232fa9e4066Sahrens 
233fa9e4066Sahrens static zvol_state_t *
234e9dbad6fSeschrock zvol_minor_lookup(const char *name)
235fa9e4066Sahrens {
236fa9e4066Sahrens 	minor_t minor;
237fa9e4066Sahrens 	zvol_state_t *zv;
238fa9e4066Sahrens 
239fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&zvol_state_lock));
240fa9e4066Sahrens 
241fa9e4066Sahrens 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) {
242fa9e4066Sahrens 		zv = ddi_get_soft_state(zvol_state, minor);
243fa9e4066Sahrens 		if (zv == NULL)
244fa9e4066Sahrens 			continue;
245fa9e4066Sahrens 		if (strcmp(zv->zv_name, name) == 0)
246fa9e4066Sahrens 			break;
247fa9e4066Sahrens 	}
248fa9e4066Sahrens 
249fa9e4066Sahrens 	return (zv);
250fa9e4066Sahrens }
251fa9e4066Sahrens 
252e7cbe64fSgw /* extent mapping arg */
253e7cbe64fSgw struct maparg {
25488b7b0f2SMatthew Ahrens 	zvol_state_t	*ma_zv;
25588b7b0f2SMatthew Ahrens 	uint64_t	ma_blks;
256e7cbe64fSgw };
257e7cbe64fSgw 
258e7cbe64fSgw /*ARGSUSED*/
259e7cbe64fSgw static int
26088b7b0f2SMatthew Ahrens zvol_map_block(spa_t *spa, blkptr_t *bp, const zbookmark_t *zb,
26188b7b0f2SMatthew Ahrens     const dnode_phys_t *dnp, void *arg)
262e7cbe64fSgw {
26388b7b0f2SMatthew Ahrens 	struct maparg *ma = arg;
26488b7b0f2SMatthew Ahrens 	zvol_extent_t *ze;
26588b7b0f2SMatthew Ahrens 	int bs = ma->ma_zv->zv_volblocksize;
266e7cbe64fSgw 
26788b7b0f2SMatthew Ahrens 	if (bp == NULL || zb->zb_object != ZVOL_OBJ || zb->zb_level != 0)
26888b7b0f2SMatthew Ahrens 		return (0);
269e7cbe64fSgw 
27088b7b0f2SMatthew Ahrens 	VERIFY3U(ma->ma_blks, ==, zb->zb_blkid);
27188b7b0f2SMatthew Ahrens 	ma->ma_blks++;
272e7cbe64fSgw 
27388b7b0f2SMatthew Ahrens 	/* Abort immediately if we have encountered gang blocks */
27488b7b0f2SMatthew Ahrens 	if (BP_IS_GANG(bp))
27588b7b0f2SMatthew Ahrens 		return (EFRAGS);
276e7cbe64fSgw 
27788b7b0f2SMatthew Ahrens 	/*
27888b7b0f2SMatthew Ahrens 	 * See if the block is at the end of the previous extent.
27988b7b0f2SMatthew Ahrens 	 */
28088b7b0f2SMatthew Ahrens 	ze = list_tail(&ma->ma_zv->zv_extents);
28188b7b0f2SMatthew Ahrens 	if (ze &&
28288b7b0f2SMatthew Ahrens 	    DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) &&
28388b7b0f2SMatthew Ahrens 	    DVA_GET_OFFSET(BP_IDENTITY(bp)) ==
28488b7b0f2SMatthew Ahrens 	    DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) {
28588b7b0f2SMatthew Ahrens 		ze->ze_nblks++;
28688b7b0f2SMatthew Ahrens 		return (0);
287e7cbe64fSgw 	}
288e7cbe64fSgw 
28988b7b0f2SMatthew Ahrens 	dprintf_bp(bp, "%s", "next blkptr:");
290e7cbe64fSgw 
29188b7b0f2SMatthew Ahrens 	/* start a new extent */
29288b7b0f2SMatthew Ahrens 	ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP);
29388b7b0f2SMatthew Ahrens 	ze->ze_dva = bp->blk_dva[0];	/* structure assignment */
29488b7b0f2SMatthew Ahrens 	ze->ze_nblks = 1;
29588b7b0f2SMatthew Ahrens 	list_insert_tail(&ma->ma_zv->zv_extents, ze);
29688b7b0f2SMatthew Ahrens 	return (0);
29788b7b0f2SMatthew Ahrens }
298e7cbe64fSgw 
29988b7b0f2SMatthew Ahrens static void
30088b7b0f2SMatthew Ahrens zvol_free_extents(zvol_state_t *zv)
30188b7b0f2SMatthew Ahrens {
30288b7b0f2SMatthew Ahrens 	zvol_extent_t *ze;
303e7cbe64fSgw 
30488b7b0f2SMatthew Ahrens 	while (ze = list_head(&zv->zv_extents)) {
30588b7b0f2SMatthew Ahrens 		list_remove(&zv->zv_extents, ze);
30688b7b0f2SMatthew Ahrens 		kmem_free(ze, sizeof (zvol_extent_t));
307e7cbe64fSgw 	}
30888b7b0f2SMatthew Ahrens }
309e7cbe64fSgw 
31088b7b0f2SMatthew Ahrens static int
31188b7b0f2SMatthew Ahrens zvol_get_lbas(zvol_state_t *zv)
31288b7b0f2SMatthew Ahrens {
31388b7b0f2SMatthew Ahrens 	struct maparg	ma;
31488b7b0f2SMatthew Ahrens 	int		err;
31588b7b0f2SMatthew Ahrens 
31688b7b0f2SMatthew Ahrens 	ma.ma_zv = zv;
31788b7b0f2SMatthew Ahrens 	ma.ma_blks = 0;
31888b7b0f2SMatthew Ahrens 	zvol_free_extents(zv);
31988b7b0f2SMatthew Ahrens 
32088b7b0f2SMatthew Ahrens 	err = traverse_dataset(dmu_objset_ds(zv->zv_objset), 0,
32188b7b0f2SMatthew Ahrens 	    TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma);
32288b7b0f2SMatthew Ahrens 	if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) {
32388b7b0f2SMatthew Ahrens 		zvol_free_extents(zv);
32488b7b0f2SMatthew Ahrens 		return (err ? err : EIO);
325e7cbe64fSgw 	}
32688b7b0f2SMatthew Ahrens 
327e7cbe64fSgw 	return (0);
328e7cbe64fSgw }
329e7cbe64fSgw 
330ecd6cf80Smarks /* ARGSUSED */
331fa9e4066Sahrens void
332ecd6cf80Smarks zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
333fa9e4066Sahrens {
334da6c28aaSamw 	zfs_creat_t *zct = arg;
335da6c28aaSamw 	nvlist_t *nvprops = zct->zct_props;
336fa9e4066Sahrens 	int error;
337e9dbad6fSeschrock 	uint64_t volblocksize, volsize;
338fa9e4066Sahrens 
339ecd6cf80Smarks 	VERIFY(nvlist_lookup_uint64(nvprops,
340e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
341ecd6cf80Smarks 	if (nvlist_lookup_uint64(nvprops,
342e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
343e9dbad6fSeschrock 		volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
344e9dbad6fSeschrock 
345e9dbad6fSeschrock 	/*
346e7cbe64fSgw 	 * These properties must be removed from the list so the generic
347e9dbad6fSeschrock 	 * property setting step won't apply to them.
348e9dbad6fSeschrock 	 */
349ecd6cf80Smarks 	VERIFY(nvlist_remove_all(nvprops,
350e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
351ecd6cf80Smarks 	(void) nvlist_remove_all(nvprops,
352e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
353e9dbad6fSeschrock 
354e9dbad6fSeschrock 	error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
355fa9e4066Sahrens 	    DMU_OT_NONE, 0, tx);
356fa9e4066Sahrens 	ASSERT(error == 0);
357fa9e4066Sahrens 
358fa9e4066Sahrens 	error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
359fa9e4066Sahrens 	    DMU_OT_NONE, 0, tx);
360fa9e4066Sahrens 	ASSERT(error == 0);
361fa9e4066Sahrens 
362e9dbad6fSeschrock 	error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
363fa9e4066Sahrens 	ASSERT(error == 0);
364fa9e4066Sahrens }
365fa9e4066Sahrens 
36622ac5be4Sperrin /*
36722ac5be4Sperrin  * Replay a TX_WRITE ZIL transaction that didn't get committed
36822ac5be4Sperrin  * after a system failure
36922ac5be4Sperrin  */
37022ac5be4Sperrin static int
37122ac5be4Sperrin zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
37222ac5be4Sperrin {
37322ac5be4Sperrin 	objset_t *os = zv->zv_objset;
37422ac5be4Sperrin 	char *data = (char *)(lr + 1);	/* data follows lr_write_t */
37522ac5be4Sperrin 	uint64_t off = lr->lr_offset;
37622ac5be4Sperrin 	uint64_t len = lr->lr_length;
37722ac5be4Sperrin 	dmu_tx_t *tx;
37822ac5be4Sperrin 	int error;
37922ac5be4Sperrin 
38022ac5be4Sperrin 	if (byteswap)
38122ac5be4Sperrin 		byteswap_uint64_array(lr, sizeof (*lr));
38222ac5be4Sperrin 
38322ac5be4Sperrin 	tx = dmu_tx_create(os);
38422ac5be4Sperrin 	dmu_tx_hold_write(tx, ZVOL_OBJ, off, len);
3851209a471SNeil Perrin 	error = dmu_tx_assign(tx, TXG_WAIT);
38622ac5be4Sperrin 	if (error) {
38722ac5be4Sperrin 		dmu_tx_abort(tx);
38822ac5be4Sperrin 	} else {
38922ac5be4Sperrin 		dmu_write(os, ZVOL_OBJ, off, len, data, tx);
39022ac5be4Sperrin 		dmu_tx_commit(tx);
39122ac5be4Sperrin 	}
39222ac5be4Sperrin 
39322ac5be4Sperrin 	return (error);
39422ac5be4Sperrin }
39522ac5be4Sperrin 
39622ac5be4Sperrin /* ARGSUSED */
39722ac5be4Sperrin static int
39822ac5be4Sperrin zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
39922ac5be4Sperrin {
40022ac5be4Sperrin 	return (ENOTSUP);
40122ac5be4Sperrin }
40222ac5be4Sperrin 
40322ac5be4Sperrin /*
40422ac5be4Sperrin  * Callback vectors for replaying records.
40522ac5be4Sperrin  * Only TX_WRITE is needed for zvol.
40622ac5be4Sperrin  */
40722ac5be4Sperrin zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = {
40822ac5be4Sperrin 	zvol_replay_err,	/* 0 no such transaction type */
40922ac5be4Sperrin 	zvol_replay_err,	/* TX_CREATE */
41022ac5be4Sperrin 	zvol_replay_err,	/* TX_MKDIR */
41122ac5be4Sperrin 	zvol_replay_err,	/* TX_MKXATTR */
41222ac5be4Sperrin 	zvol_replay_err,	/* TX_SYMLINK */
41322ac5be4Sperrin 	zvol_replay_err,	/* TX_REMOVE */
41422ac5be4Sperrin 	zvol_replay_err,	/* TX_RMDIR */
41522ac5be4Sperrin 	zvol_replay_err,	/* TX_LINK */
41622ac5be4Sperrin 	zvol_replay_err,	/* TX_RENAME */
41722ac5be4Sperrin 	zvol_replay_write,	/* TX_WRITE */
41822ac5be4Sperrin 	zvol_replay_err,	/* TX_TRUNCATE */
41922ac5be4Sperrin 	zvol_replay_err,	/* TX_SETATTR */
42022ac5be4Sperrin 	zvol_replay_err,	/* TX_ACL */
42122ac5be4Sperrin };
42222ac5be4Sperrin 
423e7cbe64fSgw /*
424e7cbe64fSgw  * Create a minor node (plus a whole lot more) for the specified volume.
425fa9e4066Sahrens  */
426fa9e4066Sahrens int
42791ebeef5Sahrens zvol_create_minor(const char *name, major_t maj)
428fa9e4066Sahrens {
429fa9e4066Sahrens 	zvol_state_t *zv;
430fa9e4066Sahrens 	objset_t *os;
43167bd71c6Sperrin 	dmu_object_info_t doi;
432fa9e4066Sahrens 	uint64_t volsize;
433fa9e4066Sahrens 	minor_t minor = 0;
434fa9e4066Sahrens 	struct pathname linkpath;
435745cd3c5Smaybee 	int ds_mode = DS_MODE_OWNER;
436fa9e4066Sahrens 	vnode_t *vp = NULL;
437fa9e4066Sahrens 	char *devpath;
438e7cbe64fSgw 	size_t devpathlen = strlen(ZVOL_FULL_DEV_DIR) + strlen(name) + 1;
439fa9e4066Sahrens 	char chrbuf[30], blkbuf[30];
440fa9e4066Sahrens 	int error;
441fa9e4066Sahrens 
442fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
443fa9e4066Sahrens 
444fa9e4066Sahrens 	if ((zv = zvol_minor_lookup(name)) != NULL) {
445fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
446fa9e4066Sahrens 		return (EEXIST);
447fa9e4066Sahrens 	}
448fa9e4066Sahrens 
449fa9e4066Sahrens 	if (strchr(name, '@') != 0)
450fa9e4066Sahrens 		ds_mode |= DS_MODE_READONLY;
451fa9e4066Sahrens 
452fa9e4066Sahrens 	error = dmu_objset_open(name, DMU_OST_ZVOL, ds_mode, &os);
453fa9e4066Sahrens 
454fa9e4066Sahrens 	if (error) {
455fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
456fa9e4066Sahrens 		return (error);
457fa9e4066Sahrens 	}
458fa9e4066Sahrens 
459fa9e4066Sahrens 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
460fa9e4066Sahrens 
461fa9e4066Sahrens 	if (error) {
462fa9e4066Sahrens 		dmu_objset_close(os);
463fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
464fa9e4066Sahrens 		return (error);
465fa9e4066Sahrens 	}
466fa9e4066Sahrens 
467fa9e4066Sahrens 	/*
468fa9e4066Sahrens 	 * If there's an existing /dev/zvol symlink, try to use the
469fa9e4066Sahrens 	 * same minor number we used last time.
470fa9e4066Sahrens 	 */
471fa9e4066Sahrens 	devpath = kmem_alloc(devpathlen, KM_SLEEP);
472fa9e4066Sahrens 
473e7cbe64fSgw 	(void) sprintf(devpath, "%s%s", ZVOL_FULL_DEV_DIR, name);
474fa9e4066Sahrens 
475fa9e4066Sahrens 	error = lookupname(devpath, UIO_SYSSPACE, NO_FOLLOW, NULL, &vp);
476fa9e4066Sahrens 
477fa9e4066Sahrens 	kmem_free(devpath, devpathlen);
478fa9e4066Sahrens 
479fa9e4066Sahrens 	if (error == 0 && vp->v_type != VLNK)
480fa9e4066Sahrens 		error = EINVAL;
481fa9e4066Sahrens 
482fa9e4066Sahrens 	if (error == 0) {
483fa9e4066Sahrens 		pn_alloc(&linkpath);
484fa9e4066Sahrens 		error = pn_getsymlink(vp, &linkpath, kcred);
485fa9e4066Sahrens 		if (error == 0) {
486fa9e4066Sahrens 			char *ms = strstr(linkpath.pn_path, ZVOL_PSEUDO_DEV);
487fa9e4066Sahrens 			if (ms != NULL) {
488fa9e4066Sahrens 				ms += strlen(ZVOL_PSEUDO_DEV);
489fa9e4066Sahrens 				minor = stoi(&ms);
490fa9e4066Sahrens 			}
491fa9e4066Sahrens 		}
492fa9e4066Sahrens 		pn_free(&linkpath);
493fa9e4066Sahrens 	}
494fa9e4066Sahrens 
495fa9e4066Sahrens 	if (vp != NULL)
496fa9e4066Sahrens 		VN_RELE(vp);
497fa9e4066Sahrens 
498fa9e4066Sahrens 	/*
499fa9e4066Sahrens 	 * If we found a minor but it's already in use, we must pick a new one.
500fa9e4066Sahrens 	 */
501fa9e4066Sahrens 	if (minor != 0 && ddi_get_soft_state(zvol_state, minor) != NULL)
502fa9e4066Sahrens 		minor = 0;
503fa9e4066Sahrens 
504fa9e4066Sahrens 	if (minor == 0)
505fa9e4066Sahrens 		minor = zvol_minor_alloc();
506fa9e4066Sahrens 
507fa9e4066Sahrens 	if (minor == 0) {
508fa9e4066Sahrens 		dmu_objset_close(os);
509fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
510fa9e4066Sahrens 		return (ENXIO);
511fa9e4066Sahrens 	}
512fa9e4066Sahrens 
513fa9e4066Sahrens 	if (ddi_soft_state_zalloc(zvol_state, minor) != DDI_SUCCESS) {
514fa9e4066Sahrens 		dmu_objset_close(os);
515fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
516fa9e4066Sahrens 		return (EAGAIN);
517fa9e4066Sahrens 	}
518fa9e4066Sahrens 
519e9dbad6fSeschrock 	(void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME,
520e9dbad6fSeschrock 	    (char *)name);
521fa9e4066Sahrens 
522fa9e4066Sahrens 	(void) sprintf(chrbuf, "%uc,raw", minor);
523fa9e4066Sahrens 
524fa9e4066Sahrens 	if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR,
525fa9e4066Sahrens 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
526fa9e4066Sahrens 		ddi_soft_state_free(zvol_state, minor);
527fa9e4066Sahrens 		dmu_objset_close(os);
528fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
529fa9e4066Sahrens 		return (EAGAIN);
530fa9e4066Sahrens 	}
531fa9e4066Sahrens 
532fa9e4066Sahrens 	(void) sprintf(blkbuf, "%uc", minor);
533fa9e4066Sahrens 
534fa9e4066Sahrens 	if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK,
535fa9e4066Sahrens 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
536fa9e4066Sahrens 		ddi_remove_minor_node(zfs_dip, chrbuf);
537fa9e4066Sahrens 		ddi_soft_state_free(zvol_state, minor);
538fa9e4066Sahrens 		dmu_objset_close(os);
539fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
540fa9e4066Sahrens 		return (EAGAIN);
541fa9e4066Sahrens 	}
542fa9e4066Sahrens 
543fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
544fa9e4066Sahrens 
545fa9e4066Sahrens 	(void) strcpy(zv->zv_name, name);
546fa9e4066Sahrens 	zv->zv_min_bs = DEV_BSHIFT;
547fa9e4066Sahrens 	zv->zv_minor = minor;
548fa9e4066Sahrens 	zv->zv_volsize = volsize;
549fa9e4066Sahrens 	zv->zv_objset = os;
550fa9e4066Sahrens 	zv->zv_mode = ds_mode;
55167bd71c6Sperrin 	zv->zv_zilog = zil_open(os, zvol_get_data);
552c2e6a7d6Sperrin 	mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
553c2e6a7d6Sperrin 	avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
554c2e6a7d6Sperrin 	    sizeof (rl_t), offsetof(rl_t, r_node));
55588b7b0f2SMatthew Ahrens 	list_create(&zv->zv_extents, sizeof (zvol_extent_t),
55688b7b0f2SMatthew Ahrens 	    offsetof(zvol_extent_t, ze_node));
55767bd71c6Sperrin 	/* get and cache the blocksize */
55867bd71c6Sperrin 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
55967bd71c6Sperrin 	ASSERT(error == 0);
56067bd71c6Sperrin 	zv->zv_volblocksize = doi.doi_data_block_size;
56122ac5be4Sperrin 
5621209a471SNeil Perrin 	zil_replay(os, zv, zvol_replay_vector);
56391ebeef5Sahrens 	zvol_size_changed(zv, maj);
564fa9e4066Sahrens 
565ea8dc4b6Seschrock 	/* XXX this should handle the possible i/o error */
566fa9e4066Sahrens 	VERIFY(dsl_prop_register(dmu_objset_ds(zv->zv_objset),
567fa9e4066Sahrens 	    "readonly", zvol_readonly_changed_cb, zv) == 0);
568fa9e4066Sahrens 
569fa9e4066Sahrens 	zvol_minors++;
570fa9e4066Sahrens 
571fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
572fa9e4066Sahrens 
573fa9e4066Sahrens 	return (0);
574fa9e4066Sahrens }
575fa9e4066Sahrens 
576fa9e4066Sahrens /*
577fa9e4066Sahrens  * Remove minor node for the specified volume.
578fa9e4066Sahrens  */
579fa9e4066Sahrens int
580e9dbad6fSeschrock zvol_remove_minor(const char *name)
581fa9e4066Sahrens {
582fa9e4066Sahrens 	zvol_state_t *zv;
583fa9e4066Sahrens 	char namebuf[30];
584fa9e4066Sahrens 
585fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
586fa9e4066Sahrens 
587e9dbad6fSeschrock 	if ((zv = zvol_minor_lookup(name)) == NULL) {
588fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
589fa9e4066Sahrens 		return (ENXIO);
590fa9e4066Sahrens 	}
591fa9e4066Sahrens 
592fa9e4066Sahrens 	if (zv->zv_total_opens != 0) {
593fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
594fa9e4066Sahrens 		return (EBUSY);
595fa9e4066Sahrens 	}
596fa9e4066Sahrens 
597fa9e4066Sahrens 	(void) sprintf(namebuf, "%uc,raw", zv->zv_minor);
598fa9e4066Sahrens 	ddi_remove_minor_node(zfs_dip, namebuf);
599fa9e4066Sahrens 
600fa9e4066Sahrens 	(void) sprintf(namebuf, "%uc", zv->zv_minor);
601fa9e4066Sahrens 	ddi_remove_minor_node(zfs_dip, namebuf);
602fa9e4066Sahrens 
603fa9e4066Sahrens 	VERIFY(dsl_prop_unregister(dmu_objset_ds(zv->zv_objset),
604fa9e4066Sahrens 	    "readonly", zvol_readonly_changed_cb, zv) == 0);
605fa9e4066Sahrens 
60622ac5be4Sperrin 	zil_close(zv->zv_zilog);
60722ac5be4Sperrin 	zv->zv_zilog = NULL;
608fa9e4066Sahrens 	dmu_objset_close(zv->zv_objset);
609fa9e4066Sahrens 	zv->zv_objset = NULL;
610c2e6a7d6Sperrin 	avl_destroy(&zv->zv_znode.z_range_avl);
611c2e6a7d6Sperrin 	mutex_destroy(&zv->zv_znode.z_range_lock);
612fa9e4066Sahrens 
613fa9e4066Sahrens 	ddi_soft_state_free(zvol_state, zv->zv_minor);
614fa9e4066Sahrens 
615fa9e4066Sahrens 	zvol_minors--;
616fa9e4066Sahrens 
617fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
618fa9e4066Sahrens 
619fa9e4066Sahrens 	return (0);
620fa9e4066Sahrens }
621fa9e4066Sahrens 
622e7cbe64fSgw int
623e7cbe64fSgw zvol_prealloc(zvol_state_t *zv)
624e7cbe64fSgw {
625e7cbe64fSgw 	objset_t *os = zv->zv_objset;
626e7cbe64fSgw 	dmu_tx_t *tx;
627e7cbe64fSgw 	uint64_t refd, avail, usedobjs, availobjs;
628e7cbe64fSgw 	uint64_t resid = zv->zv_volsize;
629e7cbe64fSgw 	uint64_t off = 0;
630e7cbe64fSgw 
631e7cbe64fSgw 	/* Check the space usage before attempting to allocate the space */
632e7cbe64fSgw 	dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs);
633e7cbe64fSgw 	if (avail < zv->zv_volsize)
634e7cbe64fSgw 		return (ENOSPC);
635e7cbe64fSgw 
636e7cbe64fSgw 	/* Free old extents if they exist */
637e7cbe64fSgw 	zvol_free_extents(zv);
638e7cbe64fSgw 
639e7cbe64fSgw 	while (resid != 0) {
640e7cbe64fSgw 		int error;
641e7cbe64fSgw 		uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE);
642e7cbe64fSgw 
643e7cbe64fSgw 		tx = dmu_tx_create(os);
644e7cbe64fSgw 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
645e7cbe64fSgw 		error = dmu_tx_assign(tx, TXG_WAIT);
646e7cbe64fSgw 		if (error) {
647e7cbe64fSgw 			dmu_tx_abort(tx);
648cdb0ab79Smaybee 			(void) dmu_free_long_range(os, ZVOL_OBJ, 0, off);
649e7cbe64fSgw 			return (error);
650e7cbe64fSgw 		}
65182c9918fSTim Haley 		dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx);
652e7cbe64fSgw 		dmu_tx_commit(tx);
653e7cbe64fSgw 		off += bytes;
654e7cbe64fSgw 		resid -= bytes;
655e7cbe64fSgw 	}
656e7cbe64fSgw 	txg_wait_synced(dmu_objset_pool(os), 0);
657e7cbe64fSgw 
658e7cbe64fSgw 	return (0);
659e7cbe64fSgw }
660e7cbe64fSgw 
661e7cbe64fSgw int
662e7cbe64fSgw zvol_update_volsize(zvol_state_t *zv, major_t maj, uint64_t volsize)
663e7cbe64fSgw {
664e7cbe64fSgw 	dmu_tx_t *tx;
665e7cbe64fSgw 	int error;
666e7cbe64fSgw 
667e7cbe64fSgw 	ASSERT(MUTEX_HELD(&zvol_state_lock));
668e7cbe64fSgw 
669e7cbe64fSgw 	tx = dmu_tx_create(zv->zv_objset);
670e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
671e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
672e7cbe64fSgw 	if (error) {
673e7cbe64fSgw 		dmu_tx_abort(tx);
674e7cbe64fSgw 		return (error);
675e7cbe64fSgw 	}
676e7cbe64fSgw 
677e7cbe64fSgw 	error = zap_update(zv->zv_objset, ZVOL_ZAP_OBJ, "size", 8, 1,
678e7cbe64fSgw 	    &volsize, tx);
679e7cbe64fSgw 	dmu_tx_commit(tx);
680e7cbe64fSgw 
681e7cbe64fSgw 	if (error == 0)
682cdb0ab79Smaybee 		error = dmu_free_long_range(zv->zv_objset,
683cdb0ab79Smaybee 		    ZVOL_OBJ, volsize, DMU_OBJECT_END);
684e7cbe64fSgw 
685bb0ade09Sahrens 	/*
686bb0ade09Sahrens 	 * If we are using a faked-up state (zv_minor == 0) then don't
687bb0ade09Sahrens 	 * try to update the in-core zvol state.
688bb0ade09Sahrens 	 */
689bb0ade09Sahrens 	if (error == 0 && zv->zv_minor) {
690e7cbe64fSgw 		zv->zv_volsize = volsize;
691e7cbe64fSgw 		zvol_size_changed(zv, maj);
692e7cbe64fSgw 	}
693e7cbe64fSgw 	return (error);
694e7cbe64fSgw }
695e7cbe64fSgw 
696fa9e4066Sahrens int
69791ebeef5Sahrens zvol_set_volsize(const char *name, major_t maj, uint64_t volsize)
698fa9e4066Sahrens {
699fa9e4066Sahrens 	zvol_state_t *zv;
700fa9e4066Sahrens 	int error;
7015c5460e9Seschrock 	dmu_object_info_t doi;
702e7cbe64fSgw 	uint64_t old_volsize = 0ULL;
703bb0ade09Sahrens 	zvol_state_t state = { 0 };
704fa9e4066Sahrens 
705fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
706fa9e4066Sahrens 
707e9dbad6fSeschrock 	if ((zv = zvol_minor_lookup(name)) == NULL) {
708bb0ade09Sahrens 		/*
709bb0ade09Sahrens 		 * If we are doing a "zfs clone -o volsize=", then the
710bb0ade09Sahrens 		 * minor node won't exist yet.
711bb0ade09Sahrens 		 */
712bb0ade09Sahrens 		error = dmu_objset_open(name, DMU_OST_ZVOL, DS_MODE_OWNER,
713bb0ade09Sahrens 		    &state.zv_objset);
714bb0ade09Sahrens 		if (error != 0)
715bb0ade09Sahrens 			goto out;
716bb0ade09Sahrens 		zv = &state;
717fa9e4066Sahrens 	}
718e7cbe64fSgw 	old_volsize = zv->zv_volsize;
719fa9e4066Sahrens 
7205c5460e9Seschrock 	if ((error = dmu_object_info(zv->zv_objset, ZVOL_OBJ, &doi)) != 0 ||
721e9dbad6fSeschrock 	    (error = zvol_check_volsize(volsize,
722bb0ade09Sahrens 	    doi.doi_data_block_size)) != 0)
723bb0ade09Sahrens 		goto out;
7245c5460e9Seschrock 
725e7cbe64fSgw 	if (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY)) {
726bb0ade09Sahrens 		error = EROFS;
727bb0ade09Sahrens 		goto out;
728fa9e4066Sahrens 	}
729fa9e4066Sahrens 
730e7cbe64fSgw 	error = zvol_update_volsize(zv, maj, volsize);
731fa9e4066Sahrens 
732e7cbe64fSgw 	/*
733e7cbe64fSgw 	 * Reinitialize the dump area to the new size. If we
734e7cbe64fSgw 	 * failed to resize the dump area then restore the it back to
735e7cbe64fSgw 	 * it's original size.
736e7cbe64fSgw 	 */
737e7cbe64fSgw 	if (error == 0 && zv->zv_flags & ZVOL_DUMPIFIED) {
738e7cbe64fSgw 		if ((error = zvol_dumpify(zv)) != 0 ||
739e7cbe64fSgw 		    (error = dumpvp_resize()) != 0) {
740e7cbe64fSgw 			(void) zvol_update_volsize(zv, maj, old_volsize);
741e7cbe64fSgw 			error = zvol_dumpify(zv);
742e7cbe64fSgw 		}
743fa9e4066Sahrens 	}
744fa9e4066Sahrens 
745bb0ade09Sahrens out:
746bb0ade09Sahrens 	if (state.zv_objset)
747bb0ade09Sahrens 		dmu_objset_close(state.zv_objset);
748bb0ade09Sahrens 
749fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
750fa9e4066Sahrens 
751fa9e4066Sahrens 	return (error);
752fa9e4066Sahrens }
753fa9e4066Sahrens 
754fa9e4066Sahrens int
755e9dbad6fSeschrock zvol_set_volblocksize(const char *name, uint64_t volblocksize)
756fa9e4066Sahrens {
757fa9e4066Sahrens 	zvol_state_t *zv;
758fa9e4066Sahrens 	dmu_tx_t *tx;
759fa9e4066Sahrens 	int error;
76088b7b0f2SMatthew Ahrens 	boolean_t needlock;
761fa9e4066Sahrens 
76288b7b0f2SMatthew Ahrens 	/*
76388b7b0f2SMatthew Ahrens 	 * The lock may already be held if we are being called from
76488b7b0f2SMatthew Ahrens 	 * zvol_dump_init().
76588b7b0f2SMatthew Ahrens 	 */
76688b7b0f2SMatthew Ahrens 	needlock = !MUTEX_HELD(&zvol_state_lock);
76788b7b0f2SMatthew Ahrens 	if (needlock)
76888b7b0f2SMatthew Ahrens 		mutex_enter(&zvol_state_lock);
769fa9e4066Sahrens 
770e9dbad6fSeschrock 	if ((zv = zvol_minor_lookup(name)) == NULL) {
77188b7b0f2SMatthew Ahrens 		if (needlock)
77288b7b0f2SMatthew Ahrens 			mutex_exit(&zvol_state_lock);
773fa9e4066Sahrens 		return (ENXIO);
774fa9e4066Sahrens 	}
775e7cbe64fSgw 	if (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY)) {
77688b7b0f2SMatthew Ahrens 		if (needlock)
77788b7b0f2SMatthew Ahrens 			mutex_exit(&zvol_state_lock);
778fa9e4066Sahrens 		return (EROFS);
779fa9e4066Sahrens 	}
780fa9e4066Sahrens 
781fa9e4066Sahrens 	tx = dmu_tx_create(zv->zv_objset);
782fa9e4066Sahrens 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
783fa9e4066Sahrens 	error = dmu_tx_assign(tx, TXG_WAIT);
784fa9e4066Sahrens 	if (error) {
785fa9e4066Sahrens 		dmu_tx_abort(tx);
786fa9e4066Sahrens 	} else {
787fa9e4066Sahrens 		error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ,
788e9dbad6fSeschrock 		    volblocksize, 0, tx);
789fa9e4066Sahrens 		if (error == ENOTSUP)
790fa9e4066Sahrens 			error = EBUSY;
791fa9e4066Sahrens 		dmu_tx_commit(tx);
79288b7b0f2SMatthew Ahrens 		if (error == 0)
79388b7b0f2SMatthew Ahrens 			zv->zv_volblocksize = volblocksize;
794fa9e4066Sahrens 	}
795fa9e4066Sahrens 
79688b7b0f2SMatthew Ahrens 	if (needlock)
79788b7b0f2SMatthew Ahrens 		mutex_exit(&zvol_state_lock);
798fa9e4066Sahrens 
799fa9e4066Sahrens 	return (error);
800fa9e4066Sahrens }
801fa9e4066Sahrens 
802fa9e4066Sahrens /*ARGSUSED*/
803fa9e4066Sahrens int
804fa9e4066Sahrens zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr)
805fa9e4066Sahrens {
806fa9e4066Sahrens 	minor_t minor = getminor(*devp);
807fa9e4066Sahrens 	zvol_state_t *zv;
808fa9e4066Sahrens 
809fa9e4066Sahrens 	if (minor == 0)			/* This is the control device */
810fa9e4066Sahrens 		return (0);
811fa9e4066Sahrens 
812fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
813fa9e4066Sahrens 
814fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
815fa9e4066Sahrens 	if (zv == NULL) {
816fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
817fa9e4066Sahrens 		return (ENXIO);
818fa9e4066Sahrens 	}
819fa9e4066Sahrens 
820fa9e4066Sahrens 	ASSERT(zv->zv_objset != NULL);
821fa9e4066Sahrens 
822fa9e4066Sahrens 	if ((flag & FWRITE) &&
823e7cbe64fSgw 	    (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY))) {
824fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
825fa9e4066Sahrens 		return (EROFS);
826fa9e4066Sahrens 	}
827c7f714e2SEric Taylor 	if (zv->zv_flags & ZVOL_EXCL) {
828c7f714e2SEric Taylor 		mutex_exit(&zvol_state_lock);
829c7f714e2SEric Taylor 		return (EBUSY);
830c7f714e2SEric Taylor 	}
831c7f714e2SEric Taylor 	if (flag & FEXCL) {
832c7f714e2SEric Taylor 		if (zv->zv_total_opens != 0) {
833c7f714e2SEric Taylor 			mutex_exit(&zvol_state_lock);
834c7f714e2SEric Taylor 			return (EBUSY);
835c7f714e2SEric Taylor 		}
836c7f714e2SEric Taylor 		zv->zv_flags |= ZVOL_EXCL;
837c7f714e2SEric Taylor 	}
838fa9e4066Sahrens 
839fa9e4066Sahrens 	if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) {
840fa9e4066Sahrens 		zv->zv_open_count[otyp]++;
841fa9e4066Sahrens 		zv->zv_total_opens++;
842fa9e4066Sahrens 	}
843fa9e4066Sahrens 
844fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
845fa9e4066Sahrens 
846fa9e4066Sahrens 	return (0);
847fa9e4066Sahrens }
848fa9e4066Sahrens 
849fa9e4066Sahrens /*ARGSUSED*/
850fa9e4066Sahrens int
851fa9e4066Sahrens zvol_close(dev_t dev, int flag, int otyp, cred_t *cr)
852fa9e4066Sahrens {
853fa9e4066Sahrens 	minor_t minor = getminor(dev);
854fa9e4066Sahrens 	zvol_state_t *zv;
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 
885fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
886fa9e4066Sahrens 
887fa9e4066Sahrens 	return (0);
888fa9e4066Sahrens }
889fa9e4066Sahrens 
890feb08c6bSbillm static void
89167bd71c6Sperrin zvol_get_done(dmu_buf_t *db, void *vzgd)
89267bd71c6Sperrin {
89367bd71c6Sperrin 	zgd_t *zgd = (zgd_t *)vzgd;
894c2e6a7d6Sperrin 	rl_t *rl = zgd->zgd_rl;
89567bd71c6Sperrin 
89667bd71c6Sperrin 	dmu_buf_rele(db, vzgd);
897c2e6a7d6Sperrin 	zfs_range_unlock(rl);
89817f17c2dSbonwick 	zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
89967bd71c6Sperrin 	kmem_free(zgd, sizeof (zgd_t));
90067bd71c6Sperrin }
90167bd71c6Sperrin 
90267bd71c6Sperrin /*
90367bd71c6Sperrin  * Get data to generate a TX_WRITE intent log record.
90467bd71c6Sperrin  */
905feb08c6bSbillm static int
90667bd71c6Sperrin zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
90767bd71c6Sperrin {
90867bd71c6Sperrin 	zvol_state_t *zv = arg;
90967bd71c6Sperrin 	objset_t *os = zv->zv_objset;
91067bd71c6Sperrin 	dmu_buf_t *db;
911c2e6a7d6Sperrin 	rl_t *rl;
91267bd71c6Sperrin 	zgd_t *zgd;
913c2e6a7d6Sperrin 	uint64_t boff; 			/* block starting offset */
914c2e6a7d6Sperrin 	int dlen = lr->lr_length;	/* length of user data */
91567bd71c6Sperrin 	int error;
91667bd71c6Sperrin 
91767bd71c6Sperrin 	ASSERT(zio);
918c2e6a7d6Sperrin 	ASSERT(dlen != 0);
919feb08c6bSbillm 
920c2e6a7d6Sperrin 	/*
921c2e6a7d6Sperrin 	 * Write records come in two flavors: immediate and indirect.
922c2e6a7d6Sperrin 	 * For small writes it's cheaper to store the data with the
923c2e6a7d6Sperrin 	 * log record (immediate); for large writes it's cheaper to
924c2e6a7d6Sperrin 	 * sync the data and get a pointer to it (indirect) so that
925c2e6a7d6Sperrin 	 * we don't have to write the data twice.
926c2e6a7d6Sperrin 	 */
927c2e6a7d6Sperrin 	if (buf != NULL) /* immediate write */
928c2e6a7d6Sperrin 		return (dmu_read(os, ZVOL_OBJ, lr->lr_offset, dlen, buf));
92967bd71c6Sperrin 
93067bd71c6Sperrin 	zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP);
93167bd71c6Sperrin 	zgd->zgd_zilog = zv->zv_zilog;
93267bd71c6Sperrin 	zgd->zgd_bp = &lr->lr_blkptr;
93367bd71c6Sperrin 
93467bd71c6Sperrin 	/*
935c2e6a7d6Sperrin 	 * Lock the range of the block to ensure that when the data is
936e7cbe64fSgw 	 * written out and its checksum is being calculated that no other
937c2e6a7d6Sperrin 	 * thread can change the block.
93867bd71c6Sperrin 	 */
939c2e6a7d6Sperrin 	boff = P2ALIGN_TYPED(lr->lr_offset, zv->zv_volblocksize, uint64_t);
940c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, boff, zv->zv_volblocksize,
941c2e6a7d6Sperrin 	    RL_READER);
942c2e6a7d6Sperrin 	zgd->zgd_rl = rl;
943c2e6a7d6Sperrin 
944c2e6a7d6Sperrin 	VERIFY(0 == dmu_buf_hold(os, ZVOL_OBJ, lr->lr_offset, zgd, &db));
94567bd71c6Sperrin 	error = dmu_sync(zio, db, &lr->lr_blkptr,
94667bd71c6Sperrin 	    lr->lr_common.lrc_txg, zvol_get_done, zgd);
947feb08c6bSbillm 	if (error == 0)
94817f17c2dSbonwick 		zil_add_block(zv->zv_zilog, &lr->lr_blkptr);
94967bd71c6Sperrin 	/*
95067bd71c6Sperrin 	 * If we get EINPROGRESS, then we need to wait for a
95167bd71c6Sperrin 	 * write IO initiated by dmu_sync() to complete before
95267bd71c6Sperrin 	 * we can release this dbuf.  We will finish everything
95367bd71c6Sperrin 	 * up in the zvol_get_done() callback.
95467bd71c6Sperrin 	 */
95567bd71c6Sperrin 	if (error == EINPROGRESS)
95667bd71c6Sperrin 		return (0);
95767bd71c6Sperrin 	dmu_buf_rele(db, zgd);
958c2e6a7d6Sperrin 	zfs_range_unlock(rl);
95967bd71c6Sperrin 	kmem_free(zgd, sizeof (zgd_t));
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
972feb08c6bSbillm zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t len)
97322ac5be4Sperrin {
974feb08c6bSbillm 	uint32_t blocksize = zv->zv_volblocksize;
9751209a471SNeil Perrin 	zilog_t *zilog = zv->zv_zilog;
97622ac5be4Sperrin 	lr_write_t *lr;
97722ac5be4Sperrin 
9781209a471SNeil Perrin 	if (zilog->zl_replay) {
9791209a471SNeil Perrin 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx);
9801209a471SNeil Perrin 		zilog->zl_replayed_seq[dmu_tx_get_txg(tx) & TXG_MASK] =
9811209a471SNeil Perrin 		    zilog->zl_replaying_seq;
9821209a471SNeil Perrin 		return;
9831209a471SNeil Perrin 	}
9841209a471SNeil Perrin 
985a24e15ceSperrin 	while (len) {
986feb08c6bSbillm 		ssize_t nbytes = MIN(len, blocksize - P2PHASE(off, blocksize));
987feb08c6bSbillm 		itx_t *itx = zil_itx_create(TX_WRITE, sizeof (*lr));
988feb08c6bSbillm 
989feb08c6bSbillm 		itx->itx_wr_state =
990feb08c6bSbillm 		    len > zvol_immediate_write_sz ?  WR_INDIRECT : WR_NEED_COPY;
991feb08c6bSbillm 		itx->itx_private = zv;
992feb08c6bSbillm 		lr = (lr_write_t *)&itx->itx_lr;
993feb08c6bSbillm 		lr->lr_foid = ZVOL_OBJ;
994feb08c6bSbillm 		lr->lr_offset = off;
995feb08c6bSbillm 		lr->lr_length = nbytes;
996feb08c6bSbillm 		lr->lr_blkoff = off - P2ALIGN_TYPED(off, blocksize, uint64_t);
997feb08c6bSbillm 		BP_ZERO(&lr->lr_blkptr);
998feb08c6bSbillm 
9991209a471SNeil Perrin 		(void) zil_itx_assign(zilog, itx, tx);
1000a24e15ceSperrin 		len -= nbytes;
1001a24e15ceSperrin 		off += nbytes;
100222ac5be4Sperrin 	}
100322ac5be4Sperrin }
100422ac5be4Sperrin 
100588b7b0f2SMatthew Ahrens static int
100688b7b0f2SMatthew Ahrens zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t size,
100788b7b0f2SMatthew Ahrens     boolean_t doread, boolean_t isdump)
1008e7cbe64fSgw {
1009e7cbe64fSgw 	vdev_disk_t *dvd;
1010e7cbe64fSgw 	int c;
1011e7cbe64fSgw 	int numerrors = 0;
1012e7cbe64fSgw 
1013e7cbe64fSgw 	for (c = 0; c < vd->vdev_children; c++) {
101488b7b0f2SMatthew Ahrens 		ASSERT(vd->vdev_ops == &vdev_mirror_ops);
101588b7b0f2SMatthew Ahrens 		int err = zvol_dumpio_vdev(vd->vdev_child[c],
101688b7b0f2SMatthew Ahrens 		    addr, offset, size, doread, isdump);
101788b7b0f2SMatthew Ahrens 		if (err != 0) {
1018e7cbe64fSgw 			numerrors++;
101988b7b0f2SMatthew Ahrens 		} else if (doread) {
1020e7cbe64fSgw 			break;
1021e7cbe64fSgw 		}
1022e7cbe64fSgw 	}
1023e7cbe64fSgw 
1024e7cbe64fSgw 	if (!vd->vdev_ops->vdev_op_leaf)
1025e7cbe64fSgw 		return (numerrors < vd->vdev_children ? 0 : EIO);
1026e7cbe64fSgw 
1027dc0bb255SEric Taylor 	if (doread && !vdev_readable(vd))
1028dc0bb255SEric Taylor 		return (EIO);
1029dc0bb255SEric Taylor 	else if (!doread && !vdev_writeable(vd))
1030e7cbe64fSgw 		return (EIO);
1031e7cbe64fSgw 
1032e7cbe64fSgw 	dvd = vd->vdev_tsd;
1033e7cbe64fSgw 	ASSERT3P(dvd, !=, NULL);
1034e7cbe64fSgw 	offset += VDEV_LABEL_START_SIZE;
1035e7cbe64fSgw 
1036e7cbe64fSgw 	if (ddi_in_panic() || isdump) {
103788b7b0f2SMatthew Ahrens 		ASSERT(!doread);
103888b7b0f2SMatthew Ahrens 		if (doread)
1039e7cbe64fSgw 			return (EIO);
1040e7cbe64fSgw 		return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset),
1041e7cbe64fSgw 		    lbtodb(size)));
1042e7cbe64fSgw 	} else {
1043e7cbe64fSgw 		return (vdev_disk_physio(dvd->vd_lh, addr, size, offset,
104488b7b0f2SMatthew Ahrens 		    doread ? B_READ : B_WRITE));
1045e7cbe64fSgw 	}
1046e7cbe64fSgw }
1047e7cbe64fSgw 
104888b7b0f2SMatthew Ahrens static int
104988b7b0f2SMatthew Ahrens zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size,
105088b7b0f2SMatthew Ahrens     boolean_t doread, boolean_t isdump)
1051e7cbe64fSgw {
1052e7cbe64fSgw 	vdev_t *vd;
1053e7cbe64fSgw 	int error;
105488b7b0f2SMatthew Ahrens 	zvol_extent_t *ze;
1055e7cbe64fSgw 	spa_t *spa = dmu_objset_spa(zv->zv_objset);
1056e7cbe64fSgw 
105788b7b0f2SMatthew Ahrens 	/* Must be sector aligned, and not stradle a block boundary. */
105888b7b0f2SMatthew Ahrens 	if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) ||
105988b7b0f2SMatthew Ahrens 	    P2BOUNDARY(offset, size, zv->zv_volblocksize)) {
1060e7cbe64fSgw 		return (EINVAL);
106188b7b0f2SMatthew Ahrens 	}
106288b7b0f2SMatthew Ahrens 	ASSERT(size <= zv->zv_volblocksize);
1063e7cbe64fSgw 
106488b7b0f2SMatthew Ahrens 	/* Locate the extent this belongs to */
106588b7b0f2SMatthew Ahrens 	ze = list_head(&zv->zv_extents);
106688b7b0f2SMatthew Ahrens 	while (offset >= ze->ze_nblks * zv->zv_volblocksize) {
106788b7b0f2SMatthew Ahrens 		offset -= ze->ze_nblks * zv->zv_volblocksize;
106888b7b0f2SMatthew Ahrens 		ze = list_next(&zv->zv_extents, ze);
106988b7b0f2SMatthew Ahrens 	}
1070e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
107188b7b0f2SMatthew Ahrens 	vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva));
107288b7b0f2SMatthew Ahrens 	offset += DVA_GET_OFFSET(&ze->ze_dva);
107388b7b0f2SMatthew Ahrens 	error = zvol_dumpio_vdev(vd, addr, offset, size, doread, isdump);
1074e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
1075e7cbe64fSgw 	return (error);
1076e7cbe64fSgw }
1077e7cbe64fSgw 
1078fa9e4066Sahrens int
1079fa9e4066Sahrens zvol_strategy(buf_t *bp)
1080fa9e4066Sahrens {
1081fa9e4066Sahrens 	zvol_state_t *zv = ddi_get_soft_state(zvol_state, getminor(bp->b_edev));
1082fa9e4066Sahrens 	uint64_t off, volsize;
108388b7b0f2SMatthew Ahrens 	size_t resid;
1084fa9e4066Sahrens 	char *addr;
108522ac5be4Sperrin 	objset_t *os;
1086c2e6a7d6Sperrin 	rl_t *rl;
1087fa9e4066Sahrens 	int error = 0;
108888b7b0f2SMatthew Ahrens 	boolean_t doread = bp->b_flags & B_READ;
108988b7b0f2SMatthew Ahrens 	boolean_t is_dump = zv->zv_flags & ZVOL_DUMPIFIED;
1090fa9e4066Sahrens 
1091fa9e4066Sahrens 	if (zv == NULL) {
1092fa9e4066Sahrens 		bioerror(bp, ENXIO);
1093fa9e4066Sahrens 		biodone(bp);
1094fa9e4066Sahrens 		return (0);
1095fa9e4066Sahrens 	}
1096fa9e4066Sahrens 
1097fa9e4066Sahrens 	if (getminor(bp->b_edev) == 0) {
1098fa9e4066Sahrens 		bioerror(bp, EINVAL);
1099fa9e4066Sahrens 		biodone(bp);
1100fa9e4066Sahrens 		return (0);
1101fa9e4066Sahrens 	}
1102fa9e4066Sahrens 
1103e7cbe64fSgw 	if (!(bp->b_flags & B_READ) &&
1104e7cbe64fSgw 	    (zv->zv_flags & ZVOL_RDONLY ||
1105e7cbe64fSgw 	    zv->zv_mode & DS_MODE_READONLY)) {
1106fa9e4066Sahrens 		bioerror(bp, EROFS);
1107fa9e4066Sahrens 		biodone(bp);
1108fa9e4066Sahrens 		return (0);
1109fa9e4066Sahrens 	}
1110fa9e4066Sahrens 
1111fa9e4066Sahrens 	off = ldbtob(bp->b_blkno);
1112fa9e4066Sahrens 	volsize = zv->zv_volsize;
1113fa9e4066Sahrens 
111422ac5be4Sperrin 	os = zv->zv_objset;
111522ac5be4Sperrin 	ASSERT(os != NULL);
1116fa9e4066Sahrens 
1117fa9e4066Sahrens 	bp_mapin(bp);
1118fa9e4066Sahrens 	addr = bp->b_un.b_addr;
1119fa9e4066Sahrens 	resid = bp->b_bcount;
1120fa9e4066Sahrens 
112188b7b0f2SMatthew Ahrens 	if (resid > 0 && (off < 0 || off >= volsize)) {
112288b7b0f2SMatthew Ahrens 		bioerror(bp, EIO);
112388b7b0f2SMatthew Ahrens 		biodone(bp);
112488b7b0f2SMatthew Ahrens 		return (0);
112588b7b0f2SMatthew Ahrens 	}
112673ec3d9cSgw 
1127a24e15ceSperrin 	/*
1128a24e15ceSperrin 	 * There must be no buffer changes when doing a dmu_sync() because
1129a24e15ceSperrin 	 * we can't change the data whilst calculating the checksum.
1130a24e15ceSperrin 	 */
1131c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, off, resid,
113288b7b0f2SMatthew Ahrens 	    doread ? RL_READER : RL_WRITER);
1133fa9e4066Sahrens 
1134e7cbe64fSgw 	while (resid != 0 && off < volsize) {
113588b7b0f2SMatthew Ahrens 		size_t size = MIN(resid, zvol_maxphys);
1136e7cbe64fSgw 		if (is_dump) {
1137e7cbe64fSgw 			size = MIN(size, P2END(off, zv->zv_volblocksize) - off);
113888b7b0f2SMatthew Ahrens 			error = zvol_dumpio(zv, addr, off, size,
113988b7b0f2SMatthew Ahrens 			    doread, B_FALSE);
114088b7b0f2SMatthew Ahrens 		} else if (doread) {
1141a24e15ceSperrin 			error = dmu_read(os, ZVOL_OBJ, off, size, addr);
1142fa9e4066Sahrens 		} else {
114322ac5be4Sperrin 			dmu_tx_t *tx = dmu_tx_create(os);
1144fa9e4066Sahrens 			dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
1145fa9e4066Sahrens 			error = dmu_tx_assign(tx, TXG_WAIT);
1146fa9e4066Sahrens 			if (error) {
1147fa9e4066Sahrens 				dmu_tx_abort(tx);
1148fa9e4066Sahrens 			} else {
114922ac5be4Sperrin 				dmu_write(os, ZVOL_OBJ, off, size, addr, tx);
1150feb08c6bSbillm 				zvol_log_write(zv, tx, off, size);
1151fa9e4066Sahrens 				dmu_tx_commit(tx);
1152fa9e4066Sahrens 			}
1153fa9e4066Sahrens 		}
1154b87f3af3Sperrin 		if (error) {
1155b87f3af3Sperrin 			/* convert checksum errors into IO errors */
1156b87f3af3Sperrin 			if (error == ECKSUM)
1157b87f3af3Sperrin 				error = EIO;
1158fa9e4066Sahrens 			break;
1159b87f3af3Sperrin 		}
1160fa9e4066Sahrens 		off += size;
1161fa9e4066Sahrens 		addr += size;
1162fa9e4066Sahrens 		resid -= size;
1163fa9e4066Sahrens 	}
1164c2e6a7d6Sperrin 	zfs_range_unlock(rl);
1165fa9e4066Sahrens 
1166fa9e4066Sahrens 	if ((bp->b_resid = resid) == bp->b_bcount)
1167fa9e4066Sahrens 		bioerror(bp, off > volsize ? EINVAL : error);
1168fa9e4066Sahrens 
1169*701f66c4SEric Taylor 	if (!(bp->b_flags & B_ASYNC) && !doread && !zil_disable &&
1170*701f66c4SEric Taylor 	    !is_dump && !(zv->zv_flags & ZVOL_WCE))
1171feb08c6bSbillm 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1172feb08c6bSbillm 	biodone(bp);
117322ac5be4Sperrin 
1174fa9e4066Sahrens 	return (0);
1175fa9e4066Sahrens }
1176fa9e4066Sahrens 
117767bd71c6Sperrin /*
117867bd71c6Sperrin  * Set the buffer count to the zvol maximum transfer.
117967bd71c6Sperrin  * Using our own routine instead of the default minphys()
118067bd71c6Sperrin  * means that for larger writes we write bigger buffers on X86
118167bd71c6Sperrin  * (128K instead of 56K) and flush the disk write cache less often
118267bd71c6Sperrin  * (every zvol_maxphys - currently 1MB) instead of minphys (currently
118367bd71c6Sperrin  * 56K on X86 and 128K on sparc).
118467bd71c6Sperrin  */
118567bd71c6Sperrin void
118667bd71c6Sperrin zvol_minphys(struct buf *bp)
118767bd71c6Sperrin {
118867bd71c6Sperrin 	if (bp->b_bcount > zvol_maxphys)
118967bd71c6Sperrin 		bp->b_bcount = zvol_maxphys;
119067bd71c6Sperrin }
119167bd71c6Sperrin 
1192e7cbe64fSgw int
1193e7cbe64fSgw zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks)
1194e7cbe64fSgw {
1195e7cbe64fSgw 	minor_t minor = getminor(dev);
1196e7cbe64fSgw 	zvol_state_t *zv;
1197e7cbe64fSgw 	int error = 0;
1198e7cbe64fSgw 	uint64_t size;
1199e7cbe64fSgw 	uint64_t boff;
1200e7cbe64fSgw 	uint64_t resid;
1201e7cbe64fSgw 
1202e7cbe64fSgw 	if (minor == 0)			/* This is the control device */
1203e7cbe64fSgw 		return (ENXIO);
1204e7cbe64fSgw 
1205e7cbe64fSgw 	zv = ddi_get_soft_state(zvol_state, minor);
1206e7cbe64fSgw 	if (zv == NULL)
1207e7cbe64fSgw 		return (ENXIO);
1208e7cbe64fSgw 
1209e7cbe64fSgw 	boff = ldbtob(blkno);
1210e7cbe64fSgw 	resid = ldbtob(nblocks);
121188b7b0f2SMatthew Ahrens 
121288b7b0f2SMatthew Ahrens 	VERIFY3U(boff + resid, <=, zv->zv_volsize);
121388b7b0f2SMatthew Ahrens 
1214e7cbe64fSgw 	while (resid) {
1215e7cbe64fSgw 		size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff);
121688b7b0f2SMatthew Ahrens 		error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE);
1217e7cbe64fSgw 		if (error)
1218e7cbe64fSgw 			break;
1219e7cbe64fSgw 		boff += size;
1220e7cbe64fSgw 		addr += size;
1221e7cbe64fSgw 		resid -= size;
1222e7cbe64fSgw 	}
1223e7cbe64fSgw 
1224e7cbe64fSgw 	return (error);
1225e7cbe64fSgw }
1226e7cbe64fSgw 
1227fa9e4066Sahrens /*ARGSUSED*/
1228fa9e4066Sahrens int
1229feb08c6bSbillm zvol_read(dev_t dev, uio_t *uio, cred_t *cr)
1230fa9e4066Sahrens {
1231c7ca1008Sgw 	minor_t minor = getminor(dev);
1232c7ca1008Sgw 	zvol_state_t *zv;
123373ec3d9cSgw 	uint64_t volsize;
1234c2e6a7d6Sperrin 	rl_t *rl;
1235feb08c6bSbillm 	int error = 0;
1236fa9e4066Sahrens 
1237c7ca1008Sgw 	if (minor == 0)			/* This is the control device */
1238c7ca1008Sgw 		return (ENXIO);
1239c7ca1008Sgw 
1240c7ca1008Sgw 	zv = ddi_get_soft_state(zvol_state, minor);
1241c7ca1008Sgw 	if (zv == NULL)
1242c7ca1008Sgw 		return (ENXIO);
1243c7ca1008Sgw 
124473ec3d9cSgw 	volsize = zv->zv_volsize;
124573ec3d9cSgw 	if (uio->uio_resid > 0 &&
124673ec3d9cSgw 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
124773ec3d9cSgw 		return (EIO);
124873ec3d9cSgw 
124988b7b0f2SMatthew Ahrens 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
125088b7b0f2SMatthew Ahrens 		error = physio(zvol_strategy, NULL, dev, B_READ,
125188b7b0f2SMatthew Ahrens 		    zvol_minphys, uio);
125288b7b0f2SMatthew Ahrens 		return (error);
125388b7b0f2SMatthew Ahrens 	}
125488b7b0f2SMatthew Ahrens 
1255c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1256c2e6a7d6Sperrin 	    RL_READER);
125773ec3d9cSgw 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1258feb08c6bSbillm 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1259fa9e4066Sahrens 
126073ec3d9cSgw 		/* don't read past the end */
126173ec3d9cSgw 		if (bytes > volsize - uio->uio_loffset)
126273ec3d9cSgw 			bytes = volsize - uio->uio_loffset;
126373ec3d9cSgw 
1264feb08c6bSbillm 		error =  dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes);
1265b87f3af3Sperrin 		if (error) {
1266b87f3af3Sperrin 			/* convert checksum errors into IO errors */
1267b87f3af3Sperrin 			if (error == ECKSUM)
1268b87f3af3Sperrin 				error = EIO;
1269feb08c6bSbillm 			break;
1270b87f3af3Sperrin 		}
1271feb08c6bSbillm 	}
1272c2e6a7d6Sperrin 	zfs_range_unlock(rl);
1273feb08c6bSbillm 	return (error);
1274fa9e4066Sahrens }
1275fa9e4066Sahrens 
1276fa9e4066Sahrens /*ARGSUSED*/
1277fa9e4066Sahrens int
1278feb08c6bSbillm zvol_write(dev_t dev, uio_t *uio, cred_t *cr)
1279fa9e4066Sahrens {
1280c7ca1008Sgw 	minor_t minor = getminor(dev);
1281c7ca1008Sgw 	zvol_state_t *zv;
128273ec3d9cSgw 	uint64_t volsize;
1283c2e6a7d6Sperrin 	rl_t *rl;
1284feb08c6bSbillm 	int error = 0;
1285feb08c6bSbillm 
1286c7ca1008Sgw 	if (minor == 0)			/* This is the control device */
1287c7ca1008Sgw 		return (ENXIO);
1288c7ca1008Sgw 
1289c7ca1008Sgw 	zv = ddi_get_soft_state(zvol_state, minor);
1290c7ca1008Sgw 	if (zv == NULL)
1291c7ca1008Sgw 		return (ENXIO);
1292c7ca1008Sgw 
129373ec3d9cSgw 	volsize = zv->zv_volsize;
129473ec3d9cSgw 	if (uio->uio_resid > 0 &&
129573ec3d9cSgw 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
129673ec3d9cSgw 		return (EIO);
129773ec3d9cSgw 
1298e7cbe64fSgw 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
1299e7cbe64fSgw 		error = physio(zvol_strategy, NULL, dev, B_WRITE,
1300e7cbe64fSgw 		    zvol_minphys, uio);
1301e7cbe64fSgw 		return (error);
1302e7cbe64fSgw 	}
1303e7cbe64fSgw 
1304c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1305c2e6a7d6Sperrin 	    RL_WRITER);
130673ec3d9cSgw 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1307feb08c6bSbillm 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1308feb08c6bSbillm 		uint64_t off = uio->uio_loffset;
1309feb08c6bSbillm 		dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
131073ec3d9cSgw 
131173ec3d9cSgw 		if (bytes > volsize - off)	/* don't write past the end */
131273ec3d9cSgw 			bytes = volsize - off;
131373ec3d9cSgw 
1314feb08c6bSbillm 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
1315feb08c6bSbillm 		error = dmu_tx_assign(tx, TXG_WAIT);
1316feb08c6bSbillm 		if (error) {
1317feb08c6bSbillm 			dmu_tx_abort(tx);
1318feb08c6bSbillm 			break;
1319feb08c6bSbillm 		}
1320feb08c6bSbillm 		error = dmu_write_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes, tx);
1321feb08c6bSbillm 		if (error == 0)
1322feb08c6bSbillm 			zvol_log_write(zv, tx, off, bytes);
1323feb08c6bSbillm 		dmu_tx_commit(tx);
1324feb08c6bSbillm 
1325feb08c6bSbillm 		if (error)
1326feb08c6bSbillm 			break;
1327feb08c6bSbillm 	}
1328c2e6a7d6Sperrin 	zfs_range_unlock(rl);
1329*701f66c4SEric Taylor 	if (!zil_disable && !(zv->zv_flags & ZVOL_WCE))
1330e08bf2c6SEric Taylor 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1331feb08c6bSbillm 	return (error);
1332fa9e4066Sahrens }
1333fa9e4066Sahrens 
1334c7f714e2SEric Taylor int
1335c7f714e2SEric Taylor zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs)
1336c7f714e2SEric Taylor {
1337c7f714e2SEric Taylor 	struct uuid uuid = EFI_RESERVED;
1338c7f714e2SEric Taylor 	efi_gpe_t gpe = { 0 };
1339c7f714e2SEric Taylor 	uint32_t crc;
1340c7f714e2SEric Taylor 	dk_efi_t efi;
1341c7f714e2SEric Taylor 	int length;
1342c7f714e2SEric Taylor 	char *ptr;
1343c7f714e2SEric Taylor 
1344c7f714e2SEric Taylor 	if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag))
1345c7f714e2SEric Taylor 		return (EFAULT);
1346c7f714e2SEric Taylor 	ptr = (char *)(uintptr_t)efi.dki_data_64;
1347c7f714e2SEric Taylor 	length = efi.dki_length;
1348c7f714e2SEric Taylor 	/*
1349c7f714e2SEric Taylor 	 * Some clients may attempt to request a PMBR for the
1350c7f714e2SEric Taylor 	 * zvol.  Currently this interface will return EINVAL to
1351c7f714e2SEric Taylor 	 * such requests.  These requests could be supported by
1352c7f714e2SEric Taylor 	 * adding a check for lba == 0 and consing up an appropriate
1353c7f714e2SEric Taylor 	 * PMBR.
1354c7f714e2SEric Taylor 	 */
1355c7f714e2SEric Taylor 	if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0)
1356c7f714e2SEric Taylor 		return (EINVAL);
1357c7f714e2SEric Taylor 
1358c7f714e2SEric Taylor 	gpe.efi_gpe_StartingLBA = LE_64(34ULL);
1359c7f714e2SEric Taylor 	gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1);
1360c7f714e2SEric Taylor 	UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
1361c7f714e2SEric Taylor 
1362c7f714e2SEric Taylor 	if (efi.dki_lba == 1) {
1363c7f714e2SEric Taylor 		efi_gpt_t gpt = { 0 };
1364c7f714e2SEric Taylor 
1365c7f714e2SEric Taylor 		gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
1366c7f714e2SEric Taylor 		gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
1367c7f714e2SEric Taylor 		gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt));
1368c7f714e2SEric Taylor 		gpt.efi_gpt_MyLBA = LE_64(1ULL);
1369c7f714e2SEric Taylor 		gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL);
1370c7f714e2SEric Taylor 		gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1);
1371c7f714e2SEric Taylor 		gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL);
1372c7f714e2SEric Taylor 		gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
1373c7f714e2SEric Taylor 		gpt.efi_gpt_SizeOfPartitionEntry =
1374c7f714e2SEric Taylor 		    LE_32(sizeof (efi_gpe_t));
1375c7f714e2SEric Taylor 		CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
1376c7f714e2SEric Taylor 		gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
1377c7f714e2SEric Taylor 		CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table);
1378c7f714e2SEric Taylor 		gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
1379c7f714e2SEric Taylor 		if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length),
1380c7f714e2SEric Taylor 		    flag))
1381c7f714e2SEric Taylor 			return (EFAULT);
1382c7f714e2SEric Taylor 		ptr += sizeof (gpt);
1383c7f714e2SEric Taylor 		length -= sizeof (gpt);
1384c7f714e2SEric Taylor 	}
1385c7f714e2SEric Taylor 	if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe),
1386c7f714e2SEric Taylor 	    length), flag))
1387c7f714e2SEric Taylor 		return (EFAULT);
1388c7f714e2SEric Taylor 	return (0);
1389c7f714e2SEric Taylor }
1390c7f714e2SEric Taylor 
1391fa9e4066Sahrens /*
1392fa9e4066Sahrens  * Dirtbag ioctls to support mkfs(1M) for UFS filesystems.  See dkio(7I).
1393fa9e4066Sahrens  */
1394fa9e4066Sahrens /*ARGSUSED*/
1395fa9e4066Sahrens int
1396fa9e4066Sahrens zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
1397fa9e4066Sahrens {
1398fa9e4066Sahrens 	zvol_state_t *zv;
1399af2c4821Smaybee 	struct dk_cinfo dki;
1400fa9e4066Sahrens 	struct dk_minfo dkm;
1401af2c4821Smaybee 	struct dk_callback *dkc;
1402fa9e4066Sahrens 	int error = 0;
1403e7cbe64fSgw 	rl_t *rl;
1404fa9e4066Sahrens 
1405fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
1406fa9e4066Sahrens 
1407fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, getminor(dev));
1408fa9e4066Sahrens 
1409fa9e4066Sahrens 	if (zv == NULL) {
1410fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
1411fa9e4066Sahrens 		return (ENXIO);
1412fa9e4066Sahrens 	}
1413*701f66c4SEric Taylor 	ASSERT(zv->zv_total_opens > 0);
1414fa9e4066Sahrens 
1415fa9e4066Sahrens 	switch (cmd) {
1416fa9e4066Sahrens 
1417fa9e4066Sahrens 	case DKIOCINFO:
1418af2c4821Smaybee 		bzero(&dki, sizeof (dki));
1419af2c4821Smaybee 		(void) strcpy(dki.dki_cname, "zvol");
1420af2c4821Smaybee 		(void) strcpy(dki.dki_dname, "zvol");
1421af2c4821Smaybee 		dki.dki_ctype = DKC_UNKNOWN;
1422af2c4821Smaybee 		dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs);
1423fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
1424af2c4821Smaybee 		if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag))
1425fa9e4066Sahrens 			error = EFAULT;
1426fa9e4066Sahrens 		return (error);
1427fa9e4066Sahrens 
1428fa9e4066Sahrens 	case DKIOCGMEDIAINFO:
1429fa9e4066Sahrens 		bzero(&dkm, sizeof (dkm));
1430fa9e4066Sahrens 		dkm.dki_lbsize = 1U << zv->zv_min_bs;
1431fa9e4066Sahrens 		dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
1432fa9e4066Sahrens 		dkm.dki_media_type = DK_UNKNOWN;
1433fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
1434fa9e4066Sahrens 		if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
1435fa9e4066Sahrens 			error = EFAULT;
1436fa9e4066Sahrens 		return (error);
1437fa9e4066Sahrens 
1438fa9e4066Sahrens 	case DKIOCGETEFI:
1439c7f714e2SEric Taylor 		{
1440c7f714e2SEric Taylor 			uint64_t vs = zv->zv_volsize;
1441c7f714e2SEric Taylor 			uint8_t bs = zv->zv_min_bs;
1442fa9e4066Sahrens 
144368a5ac4dSmaybee 			mutex_exit(&zvol_state_lock);
1444c7f714e2SEric Taylor 			error = zvol_getefi((void *)arg, flag, vs, bs);
1445c7f714e2SEric Taylor 			return (error);
144668a5ac4dSmaybee 		}
1447fa9e4066Sahrens 
1448feb08c6bSbillm 	case DKIOCFLUSHWRITECACHE:
1449af2c4821Smaybee 		dkc = (struct dk_callback *)arg;
1450*701f66c4SEric Taylor 		mutex_exit(&zvol_state_lock);
1451feb08c6bSbillm 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1452af2c4821Smaybee 		if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) {
1453af2c4821Smaybee 			(*dkc->dkc_callback)(dkc->dkc_cookie, error);
1454af2c4821Smaybee 			error = 0;
1455af2c4821Smaybee 		}
1456*701f66c4SEric Taylor 		return (error);
1457*701f66c4SEric Taylor 
1458*701f66c4SEric Taylor 	case DKIOCGETWCE:
1459*701f66c4SEric Taylor 		{
1460*701f66c4SEric Taylor 			int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0;
1461*701f66c4SEric Taylor 			if (ddi_copyout(&wce, (void *)arg, sizeof (int),
1462*701f66c4SEric Taylor 			    flag))
1463*701f66c4SEric Taylor 				error = EFAULT;
1464*701f66c4SEric Taylor 			break;
1465*701f66c4SEric Taylor 		}
1466*701f66c4SEric Taylor 	case DKIOCSETWCE:
1467*701f66c4SEric Taylor 		{
1468*701f66c4SEric Taylor 			int wce;
1469*701f66c4SEric Taylor 			if (ddi_copyin((void *)arg, &wce, sizeof (int),
1470*701f66c4SEric Taylor 			    flag)) {
1471*701f66c4SEric Taylor 				error = EFAULT;
1472*701f66c4SEric Taylor 				break;
1473*701f66c4SEric Taylor 			}
1474*701f66c4SEric Taylor 			if (wce) {
1475*701f66c4SEric Taylor 				zv->zv_flags |= ZVOL_WCE;
1476*701f66c4SEric Taylor 				mutex_exit(&zvol_state_lock);
1477*701f66c4SEric Taylor 			} else {
1478*701f66c4SEric Taylor 				zv->zv_flags &= ~ZVOL_WCE;
1479*701f66c4SEric Taylor 				mutex_exit(&zvol_state_lock);
1480*701f66c4SEric Taylor 				zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1481*701f66c4SEric Taylor 			}
1482*701f66c4SEric Taylor 			return (0);
1483*701f66c4SEric Taylor 		}
1484feb08c6bSbillm 
1485b6130eadSmaybee 	case DKIOCGGEOM:
1486b6130eadSmaybee 	case DKIOCGVTOC:
1487e7cbe64fSgw 		/*
1488e7cbe64fSgw 		 * commands using these (like prtvtoc) expect ENOTSUP
1489e7cbe64fSgw 		 * since we're emulating an EFI label
1490e7cbe64fSgw 		 */
1491b6130eadSmaybee 		error = ENOTSUP;
1492b6130eadSmaybee 		break;
1493b6130eadSmaybee 
1494e7cbe64fSgw 	case DKIOCDUMPINIT:
1495e7cbe64fSgw 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1496e7cbe64fSgw 		    RL_WRITER);
1497e7cbe64fSgw 		error = zvol_dumpify(zv);
1498e7cbe64fSgw 		zfs_range_unlock(rl);
1499e7cbe64fSgw 		break;
1500e7cbe64fSgw 
1501e7cbe64fSgw 	case DKIOCDUMPFINI:
150206d5ae10SEric Taylor 		if (!(zv->zv_flags & ZVOL_DUMPIFIED))
150306d5ae10SEric Taylor 			break;
1504e7cbe64fSgw 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1505e7cbe64fSgw 		    RL_WRITER);
1506e7cbe64fSgw 		error = zvol_dump_fini(zv);
1507e7cbe64fSgw 		zfs_range_unlock(rl);
1508e7cbe64fSgw 		break;
1509e7cbe64fSgw 
1510fa9e4066Sahrens 	default:
151168a5ac4dSmaybee 		error = ENOTTY;
1512fa9e4066Sahrens 		break;
1513fa9e4066Sahrens 
1514fa9e4066Sahrens 	}
1515fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
1516fa9e4066Sahrens 	return (error);
1517fa9e4066Sahrens }
1518fa9e4066Sahrens 
1519fa9e4066Sahrens int
1520fa9e4066Sahrens zvol_busy(void)
1521fa9e4066Sahrens {
1522fa9e4066Sahrens 	return (zvol_minors != 0);
1523fa9e4066Sahrens }
1524fa9e4066Sahrens 
1525fa9e4066Sahrens void
1526fa9e4066Sahrens zvol_init(void)
1527fa9e4066Sahrens {
1528fa9e4066Sahrens 	VERIFY(ddi_soft_state_init(&zvol_state, sizeof (zvol_state_t), 1) == 0);
1529fa9e4066Sahrens 	mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
1530fa9e4066Sahrens }
1531fa9e4066Sahrens 
1532fa9e4066Sahrens void
1533fa9e4066Sahrens zvol_fini(void)
1534fa9e4066Sahrens {
1535fa9e4066Sahrens 	mutex_destroy(&zvol_state_lock);
1536fa9e4066Sahrens 	ddi_soft_state_fini(&zvol_state);
1537fa9e4066Sahrens }
1538e7cbe64fSgw 
1539e7cbe64fSgw static boolean_t
1540e7cbe64fSgw zvol_is_swap(zvol_state_t *zv)
1541e7cbe64fSgw {
1542e7cbe64fSgw 	vnode_t *vp;
1543e7cbe64fSgw 	boolean_t ret = B_FALSE;
1544e7cbe64fSgw 	char *devpath;
1545e7cbe64fSgw 	size_t devpathlen;
1546e7cbe64fSgw 	int error;
1547e7cbe64fSgw 
1548e7cbe64fSgw 	devpathlen = strlen(ZVOL_FULL_DEV_DIR) + strlen(zv->zv_name) + 1;
1549e7cbe64fSgw 	devpath = kmem_alloc(devpathlen, KM_SLEEP);
1550e7cbe64fSgw 	(void) sprintf(devpath, "%s%s", ZVOL_FULL_DEV_DIR, zv->zv_name);
1551e7cbe64fSgw 	error = lookupname(devpath, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp);
1552e7cbe64fSgw 	kmem_free(devpath, devpathlen);
1553e7cbe64fSgw 
1554e7cbe64fSgw 	ret = !error && IS_SWAPVP(common_specvp(vp));
1555e7cbe64fSgw 
1556e7cbe64fSgw 	if (vp != NULL)
1557e7cbe64fSgw 		VN_RELE(vp);
1558e7cbe64fSgw 
1559e7cbe64fSgw 	return (ret);
1560e7cbe64fSgw }
1561e7cbe64fSgw 
1562e7cbe64fSgw static int
1563e7cbe64fSgw zvol_dump_init(zvol_state_t *zv, boolean_t resize)
1564e7cbe64fSgw {
1565e7cbe64fSgw 	dmu_tx_t *tx;
1566e7cbe64fSgw 	int error = 0;
1567e7cbe64fSgw 	objset_t *os = zv->zv_objset;
1568e7cbe64fSgw 	nvlist_t *nv = NULL;
1569e7cbe64fSgw 
1570e7cbe64fSgw 	ASSERT(MUTEX_HELD(&zvol_state_lock));
1571e7cbe64fSgw 
1572e7cbe64fSgw 	tx = dmu_tx_create(os);
1573e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1574e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
1575e7cbe64fSgw 	if (error) {
1576e7cbe64fSgw 		dmu_tx_abort(tx);
1577e7cbe64fSgw 		return (error);
1578e7cbe64fSgw 	}
1579e7cbe64fSgw 
1580e7cbe64fSgw 	/*
1581e7cbe64fSgw 	 * If we are resizing the dump device then we only need to
1582e7cbe64fSgw 	 * update the refreservation to match the newly updated
1583e7cbe64fSgw 	 * zvolsize. Otherwise, we save off the original state of the
1584e7cbe64fSgw 	 * zvol so that we can restore them if the zvol is ever undumpified.
1585e7cbe64fSgw 	 */
1586e7cbe64fSgw 	if (resize) {
1587e7cbe64fSgw 		error = zap_update(os, ZVOL_ZAP_OBJ,
1588e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1589e7cbe64fSgw 		    &zv->zv_volsize, tx);
1590e7cbe64fSgw 	} else {
159188b7b0f2SMatthew Ahrens 		uint64_t checksum, compress, refresrv, vbs;
159288b7b0f2SMatthew Ahrens 
1593e7cbe64fSgw 		error = dsl_prop_get_integer(zv->zv_name,
1594e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL);
1595e7cbe64fSgw 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
1596e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL);
1597e7cbe64fSgw 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
1598e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL);
159988b7b0f2SMatthew Ahrens 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
160088b7b0f2SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL);
1601e7cbe64fSgw 
1602e7cbe64fSgw 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1603e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1,
1604e7cbe64fSgw 		    &compress, tx);
1605e7cbe64fSgw 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1606e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx);
1607e7cbe64fSgw 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1608e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1609e7cbe64fSgw 		    &refresrv, tx);
161088b7b0f2SMatthew Ahrens 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
161188b7b0f2SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1,
161288b7b0f2SMatthew Ahrens 		    &vbs, tx);
1613e7cbe64fSgw 	}
1614e7cbe64fSgw 	dmu_tx_commit(tx);
1615e7cbe64fSgw 
1616e7cbe64fSgw 	/* Truncate the file */
1617e7cbe64fSgw 	if (!error)
1618cdb0ab79Smaybee 		error = dmu_free_long_range(zv->zv_objset,
1619cdb0ab79Smaybee 		    ZVOL_OBJ, 0, DMU_OBJECT_END);
1620e7cbe64fSgw 
1621e7cbe64fSgw 	if (error)
1622e7cbe64fSgw 		return (error);
1623e7cbe64fSgw 
1624e7cbe64fSgw 	/*
1625e7cbe64fSgw 	 * We only need update the zvol's property if we are initializing
1626e7cbe64fSgw 	 * the dump area for the first time.
1627e7cbe64fSgw 	 */
1628e7cbe64fSgw 	if (!resize) {
1629e7cbe64fSgw 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1630e7cbe64fSgw 		VERIFY(nvlist_add_uint64(nv,
1631e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0);
1632e7cbe64fSgw 		VERIFY(nvlist_add_uint64(nv,
1633e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
1634e7cbe64fSgw 		    ZIO_COMPRESS_OFF) == 0);
1635e7cbe64fSgw 		VERIFY(nvlist_add_uint64(nv,
1636e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
1637e7cbe64fSgw 		    ZIO_CHECKSUM_OFF) == 0);
163888b7b0f2SMatthew Ahrens 		VERIFY(nvlist_add_uint64(nv,
163988b7b0f2SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
164088b7b0f2SMatthew Ahrens 		    SPA_MAXBLOCKSIZE) == 0);
1641e7cbe64fSgw 
1642e7cbe64fSgw 		error = zfs_set_prop_nvlist(zv->zv_name, nv);
1643e7cbe64fSgw 		nvlist_free(nv);
1644e7cbe64fSgw 
1645e7cbe64fSgw 		if (error)
1646e7cbe64fSgw 			return (error);
1647e7cbe64fSgw 	}
1648e7cbe64fSgw 
1649e7cbe64fSgw 	/* Allocate the space for the dump */
1650e7cbe64fSgw 	error = zvol_prealloc(zv);
1651e7cbe64fSgw 	return (error);
1652e7cbe64fSgw }
1653e7cbe64fSgw 
1654e7cbe64fSgw static int
1655e7cbe64fSgw zvol_dumpify(zvol_state_t *zv)
1656e7cbe64fSgw {
1657e7cbe64fSgw 	int error = 0;
1658e7cbe64fSgw 	uint64_t dumpsize = 0;
1659e7cbe64fSgw 	dmu_tx_t *tx;
1660e7cbe64fSgw 	objset_t *os = zv->zv_objset;
1661e7cbe64fSgw 
1662e7cbe64fSgw 	if (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY))
1663e7cbe64fSgw 		return (EROFS);
1664e7cbe64fSgw 
1665e7cbe64fSgw 	/*
1666e7cbe64fSgw 	 * We do not support swap devices acting as dump devices.
1667e7cbe64fSgw 	 */
1668e7cbe64fSgw 	if (zvol_is_swap(zv))
1669e7cbe64fSgw 		return (ENOTSUP);
1670e7cbe64fSgw 
1671e7cbe64fSgw 	if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE,
1672e7cbe64fSgw 	    8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) {
1673e7cbe64fSgw 		boolean_t resize = (dumpsize > 0) ? B_TRUE : B_FALSE;
1674e7cbe64fSgw 
1675e7cbe64fSgw 		if ((error = zvol_dump_init(zv, resize)) != 0) {
1676e7cbe64fSgw 			(void) zvol_dump_fini(zv);
1677e7cbe64fSgw 			return (error);
1678e7cbe64fSgw 		}
1679e7cbe64fSgw 	}
1680e7cbe64fSgw 
1681e7cbe64fSgw 	/*
1682e7cbe64fSgw 	 * Build up our lba mapping.
1683e7cbe64fSgw 	 */
1684e7cbe64fSgw 	error = zvol_get_lbas(zv);
1685e7cbe64fSgw 	if (error) {
1686e7cbe64fSgw 		(void) zvol_dump_fini(zv);
1687e7cbe64fSgw 		return (error);
1688e7cbe64fSgw 	}
1689e7cbe64fSgw 
1690e7cbe64fSgw 	tx = dmu_tx_create(os);
1691e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1692e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
1693e7cbe64fSgw 	if (error) {
1694e7cbe64fSgw 		dmu_tx_abort(tx);
1695e7cbe64fSgw 		(void) zvol_dump_fini(zv);
1696e7cbe64fSgw 		return (error);
1697e7cbe64fSgw 	}
1698e7cbe64fSgw 
1699e7cbe64fSgw 	zv->zv_flags |= ZVOL_DUMPIFIED;
1700e7cbe64fSgw 	error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1,
1701e7cbe64fSgw 	    &zv->zv_volsize, tx);
1702e7cbe64fSgw 	dmu_tx_commit(tx);
1703e7cbe64fSgw 
1704e7cbe64fSgw 	if (error) {
1705e7cbe64fSgw 		(void) zvol_dump_fini(zv);
1706e7cbe64fSgw 		return (error);
1707e7cbe64fSgw 	}
1708e7cbe64fSgw 
1709e7cbe64fSgw 	txg_wait_synced(dmu_objset_pool(os), 0);
1710e7cbe64fSgw 	return (0);
1711e7cbe64fSgw }
1712e7cbe64fSgw 
1713e7cbe64fSgw static int
1714e7cbe64fSgw zvol_dump_fini(zvol_state_t *zv)
1715e7cbe64fSgw {
1716e7cbe64fSgw 	dmu_tx_t *tx;
1717e7cbe64fSgw 	objset_t *os = zv->zv_objset;
1718e7cbe64fSgw 	nvlist_t *nv;
1719e7cbe64fSgw 	int error = 0;
172088b7b0f2SMatthew Ahrens 	uint64_t checksum, compress, refresrv, vbs;
1721e7cbe64fSgw 
1722b7e50089Smaybee 	/*
1723b7e50089Smaybee 	 * Attempt to restore the zvol back to its pre-dumpified state.
1724b7e50089Smaybee 	 * This is a best-effort attempt as it's possible that not all
1725b7e50089Smaybee 	 * of these properties were initialized during the dumpify process
1726b7e50089Smaybee 	 * (i.e. error during zvol_dump_init).
1727b7e50089Smaybee 	 */
1728b7e50089Smaybee 
1729e7cbe64fSgw 	tx = dmu_tx_create(os);
1730e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1731e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
1732e7cbe64fSgw 	if (error) {
1733e7cbe64fSgw 		dmu_tx_abort(tx);
1734e7cbe64fSgw 		return (error);
1735e7cbe64fSgw 	}
1736b7e50089Smaybee 	(void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx);
1737b7e50089Smaybee 	dmu_tx_commit(tx);
1738e7cbe64fSgw 
1739e7cbe64fSgw 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1740e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum);
1741e7cbe64fSgw 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1742e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress);
1743e7cbe64fSgw 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1744e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv);
174588b7b0f2SMatthew Ahrens 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
174688b7b0f2SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs);
1747e7cbe64fSgw 
1748e7cbe64fSgw 	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1749e7cbe64fSgw 	(void) nvlist_add_uint64(nv,
1750e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum);
1751e7cbe64fSgw 	(void) nvlist_add_uint64(nv,
1752e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress);
1753e7cbe64fSgw 	(void) nvlist_add_uint64(nv,
1754e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv);
175588b7b0f2SMatthew Ahrens 	(void) nvlist_add_uint64(nv,
175688b7b0f2SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), vbs);
1757e7cbe64fSgw 	(void) zfs_set_prop_nvlist(zv->zv_name, nv);
1758e7cbe64fSgw 	nvlist_free(nv);
1759e7cbe64fSgw 
1760b7e50089Smaybee 	zvol_free_extents(zv);
1761b7e50089Smaybee 	zv->zv_flags &= ~ZVOL_DUMPIFIED;
1762b7e50089Smaybee 	(void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END);
1763b7e50089Smaybee 
1764e7cbe64fSgw 	return (0);
1765e7cbe64fSgw }
1766