xref: /illumos-gate/usr/src/uts/common/fs/zfs/zvol.c (revision dc0bb255)
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 /*
22e7cbe64fSgw  * Copyright 2008 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>
78fa9e4066Sahrens 
79fa9e4066Sahrens #include "zfs_namecheck.h"
80fa9e4066Sahrens 
81fa9e4066Sahrens static void *zvol_state;
82fa9e4066Sahrens 
83e7cbe64fSgw #define	ZVOL_DUMPSIZE		"dumpsize"
84e7cbe64fSgw 
85fa9e4066Sahrens /*
86fa9e4066Sahrens  * This lock protects the zvol_state structure from being modified
87fa9e4066Sahrens  * while it's being used, e.g. an open that comes in before a create
88fa9e4066Sahrens  * finishes.  It also protects temporary opens of the dataset so that,
89fa9e4066Sahrens  * e.g., an open doesn't get a spurious EBUSY.
90fa9e4066Sahrens  */
91fa9e4066Sahrens static kmutex_t zvol_state_lock;
92fa9e4066Sahrens static uint32_t zvol_minors;
93fa9e4066Sahrens 
94e7cbe64fSgw typedef struct zvol_extent {
9588b7b0f2SMatthew Ahrens 	list_node_t	ze_node;
96e7cbe64fSgw 	dva_t		ze_dva;		/* dva associated with this extent */
9788b7b0f2SMatthew Ahrens 	uint64_t	ze_nblks;	/* number of blocks in extent */
98e7cbe64fSgw } zvol_extent_t;
99e7cbe64fSgw 
100fa9e4066Sahrens /*
101fa9e4066Sahrens  * The in-core state of each volume.
102fa9e4066Sahrens  */
103fa9e4066Sahrens typedef struct zvol_state {
104fa9e4066Sahrens 	char		zv_name[MAXPATHLEN]; /* pool/dd name */
105fa9e4066Sahrens 	uint64_t	zv_volsize;	/* amount of space we advertise */
10667bd71c6Sperrin 	uint64_t	zv_volblocksize; /* volume block size */
107fa9e4066Sahrens 	minor_t		zv_minor;	/* minor number */
108fa9e4066Sahrens 	uint8_t		zv_min_bs;	/* minimum addressable block shift */
109e7cbe64fSgw 	uint8_t		zv_flags;	/* readonly; dumpified */
110fa9e4066Sahrens 	objset_t	*zv_objset;	/* objset handle */
111fa9e4066Sahrens 	uint32_t	zv_mode;	/* DS_MODE_* flags at open time */
112fa9e4066Sahrens 	uint32_t	zv_open_count[OTYPCNT];	/* open counts */
113fa9e4066Sahrens 	uint32_t	zv_total_opens;	/* total open count */
11422ac5be4Sperrin 	zilog_t		*zv_zilog;	/* ZIL handle */
11588b7b0f2SMatthew Ahrens 	list_t		zv_extents;	/* List of extents for dump */
11622ac5be4Sperrin 	uint64_t	zv_txg_assign;	/* txg to assign during ZIL replay */
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
126e7cbe64fSgw 
12767bd71c6Sperrin /*
12867bd71c6Sperrin  * zvol maximum transfer in one DMU tx.
12967bd71c6Sperrin  */
13067bd71c6Sperrin int zvol_maxphys = DMU_MAX_ACCESS/2;
13167bd71c6Sperrin 
132e7cbe64fSgw extern int zfs_set_prop_nvlist(const char *, nvlist_t *);
133feb08c6bSbillm static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio);
134e7cbe64fSgw static int zvol_dumpify(zvol_state_t *zv);
135e7cbe64fSgw static int zvol_dump_fini(zvol_state_t *zv);
136e7cbe64fSgw static int zvol_dump_init(zvol_state_t *zv, boolean_t resize);
13767bd71c6Sperrin 
138fa9e4066Sahrens static void
13991ebeef5Sahrens zvol_size_changed(zvol_state_t *zv, major_t maj)
140fa9e4066Sahrens {
14191ebeef5Sahrens 	dev_t dev = makedevice(maj, zv->zv_minor);
142fa9e4066Sahrens 
143fa9e4066Sahrens 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
144fa9e4066Sahrens 	    "Size", zv->zv_volsize) == DDI_SUCCESS);
145fa9e4066Sahrens 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
146fa9e4066Sahrens 	    "Nblocks", lbtodb(zv->zv_volsize)) == DDI_SUCCESS);
147e7cbe64fSgw 
148e7cbe64fSgw 	/* Notify specfs to invalidate the cached size */
149e7cbe64fSgw 	spec_size_invalidate(dev, VBLK);
150e7cbe64fSgw 	spec_size_invalidate(dev, VCHR);
151fa9e4066Sahrens }
152fa9e4066Sahrens 
153fa9e4066Sahrens int
154e9dbad6fSeschrock zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
155fa9e4066Sahrens {
156e9dbad6fSeschrock 	if (volsize == 0)
157fa9e4066Sahrens 		return (EINVAL);
158fa9e4066Sahrens 
159e9dbad6fSeschrock 	if (volsize % blocksize != 0)
1605c5460e9Seschrock 		return (EINVAL);
1615c5460e9Seschrock 
162fa9e4066Sahrens #ifdef _ILP32
163e9dbad6fSeschrock 	if (volsize - 1 > SPEC_MAXOFFSET_T)
164fa9e4066Sahrens 		return (EOVERFLOW);
165fa9e4066Sahrens #endif
166fa9e4066Sahrens 	return (0);
167fa9e4066Sahrens }
168fa9e4066Sahrens 
169fa9e4066Sahrens int
170e9dbad6fSeschrock zvol_check_volblocksize(uint64_t volblocksize)
171fa9e4066Sahrens {
172e9dbad6fSeschrock 	if (volblocksize < SPA_MINBLOCKSIZE ||
173e9dbad6fSeschrock 	    volblocksize > SPA_MAXBLOCKSIZE ||
174e9dbad6fSeschrock 	    !ISP2(volblocksize))
175fa9e4066Sahrens 		return (EDOM);
176fa9e4066Sahrens 
177fa9e4066Sahrens 	return (0);
178fa9e4066Sahrens }
179fa9e4066Sahrens 
180fa9e4066Sahrens static void
181fa9e4066Sahrens zvol_readonly_changed_cb(void *arg, uint64_t newval)
182fa9e4066Sahrens {
183fa9e4066Sahrens 	zvol_state_t *zv = arg;
184fa9e4066Sahrens 
185e7cbe64fSgw 	if (newval)
186e7cbe64fSgw 		zv->zv_flags |= ZVOL_RDONLY;
187e7cbe64fSgw 	else
188e7cbe64fSgw 		zv->zv_flags &= ~ZVOL_RDONLY;
189fa9e4066Sahrens }
190fa9e4066Sahrens 
191fa9e4066Sahrens int
192a2eea2e1Sahrens zvol_get_stats(objset_t *os, nvlist_t *nv)
193fa9e4066Sahrens {
194fa9e4066Sahrens 	int error;
195fa9e4066Sahrens 	dmu_object_info_t doi;
196a2eea2e1Sahrens 	uint64_t val;
197fa9e4066Sahrens 
198fa9e4066Sahrens 
199a2eea2e1Sahrens 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
200fa9e4066Sahrens 	if (error)
201fa9e4066Sahrens 		return (error);
202fa9e4066Sahrens 
203a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
204a2eea2e1Sahrens 
205fa9e4066Sahrens 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
206fa9e4066Sahrens 
207a2eea2e1Sahrens 	if (error == 0) {
208a2eea2e1Sahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
209a2eea2e1Sahrens 		    doi.doi_data_block_size);
210a2eea2e1Sahrens 	}
211fa9e4066Sahrens 
212fa9e4066Sahrens 	return (error);
213fa9e4066Sahrens }
214fa9e4066Sahrens 
215fa9e4066Sahrens /*
216fa9e4066Sahrens  * Find a free minor number.
217fa9e4066Sahrens  */
218fa9e4066Sahrens static minor_t
219fa9e4066Sahrens zvol_minor_alloc(void)
220fa9e4066Sahrens {
221fa9e4066Sahrens 	minor_t minor;
222fa9e4066Sahrens 
223fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&zvol_state_lock));
224fa9e4066Sahrens 
225fa9e4066Sahrens 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++)
226fa9e4066Sahrens 		if (ddi_get_soft_state(zvol_state, minor) == NULL)
227fa9e4066Sahrens 			return (minor);
228fa9e4066Sahrens 
229fa9e4066Sahrens 	return (0);
230fa9e4066Sahrens }
231fa9e4066Sahrens 
232fa9e4066Sahrens static zvol_state_t *
233e9dbad6fSeschrock zvol_minor_lookup(const char *name)
234fa9e4066Sahrens {
235fa9e4066Sahrens 	minor_t minor;
236fa9e4066Sahrens 	zvol_state_t *zv;
237fa9e4066Sahrens 
238fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&zvol_state_lock));
239fa9e4066Sahrens 
240fa9e4066Sahrens 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) {
241fa9e4066Sahrens 		zv = ddi_get_soft_state(zvol_state, minor);
242fa9e4066Sahrens 		if (zv == NULL)
243fa9e4066Sahrens 			continue;
244fa9e4066Sahrens 		if (strcmp(zv->zv_name, name) == 0)
245fa9e4066Sahrens 			break;
246fa9e4066Sahrens 	}
247fa9e4066Sahrens 
248fa9e4066Sahrens 	return (zv);
249fa9e4066Sahrens }
250fa9e4066Sahrens 
251e7cbe64fSgw /* extent mapping arg */
252e7cbe64fSgw struct maparg {
25388b7b0f2SMatthew Ahrens 	zvol_state_t	*ma_zv;
25488b7b0f2SMatthew Ahrens 	uint64_t	ma_blks;
255e7cbe64fSgw };
256e7cbe64fSgw 
257e7cbe64fSgw /*ARGSUSED*/
258e7cbe64fSgw static int
25988b7b0f2SMatthew Ahrens zvol_map_block(spa_t *spa, blkptr_t *bp, const zbookmark_t *zb,
26088b7b0f2SMatthew Ahrens     const dnode_phys_t *dnp, void *arg)
261e7cbe64fSgw {
26288b7b0f2SMatthew Ahrens 	struct maparg *ma = arg;
26388b7b0f2SMatthew Ahrens 	zvol_extent_t *ze;
26488b7b0f2SMatthew Ahrens 	int bs = ma->ma_zv->zv_volblocksize;
265e7cbe64fSgw 
26688b7b0f2SMatthew Ahrens 	if (bp == NULL || zb->zb_object != ZVOL_OBJ || zb->zb_level != 0)
26788b7b0f2SMatthew Ahrens 		return (0);
268e7cbe64fSgw 
26988b7b0f2SMatthew Ahrens 	VERIFY3U(ma->ma_blks, ==, zb->zb_blkid);
27088b7b0f2SMatthew Ahrens 	ma->ma_blks++;
271e7cbe64fSgw 
27288b7b0f2SMatthew Ahrens 	/* Abort immediately if we have encountered gang blocks */
27388b7b0f2SMatthew Ahrens 	if (BP_IS_GANG(bp))
27488b7b0f2SMatthew Ahrens 		return (EFRAGS);
275e7cbe64fSgw 
27688b7b0f2SMatthew Ahrens 	/*
27788b7b0f2SMatthew Ahrens 	 * See if the block is at the end of the previous extent.
27888b7b0f2SMatthew Ahrens 	 */
27988b7b0f2SMatthew Ahrens 	ze = list_tail(&ma->ma_zv->zv_extents);
28088b7b0f2SMatthew Ahrens 	if (ze &&
28188b7b0f2SMatthew Ahrens 	    DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) &&
28288b7b0f2SMatthew Ahrens 	    DVA_GET_OFFSET(BP_IDENTITY(bp)) ==
28388b7b0f2SMatthew Ahrens 	    DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) {
28488b7b0f2SMatthew Ahrens 		ze->ze_nblks++;
28588b7b0f2SMatthew Ahrens 		return (0);
286e7cbe64fSgw 	}
287e7cbe64fSgw 
28888b7b0f2SMatthew Ahrens 	dprintf_bp(bp, "%s", "next blkptr:");
289e7cbe64fSgw 
29088b7b0f2SMatthew Ahrens 	/* start a new extent */
29188b7b0f2SMatthew Ahrens 	ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP);
29288b7b0f2SMatthew Ahrens 	ze->ze_dva = bp->blk_dva[0];	/* structure assignment */
29388b7b0f2SMatthew Ahrens 	ze->ze_nblks = 1;
29488b7b0f2SMatthew Ahrens 	list_insert_tail(&ma->ma_zv->zv_extents, ze);
29588b7b0f2SMatthew Ahrens 	return (0);
29688b7b0f2SMatthew Ahrens }
297e7cbe64fSgw 
29888b7b0f2SMatthew Ahrens static void
29988b7b0f2SMatthew Ahrens zvol_free_extents(zvol_state_t *zv)
30088b7b0f2SMatthew Ahrens {
30188b7b0f2SMatthew Ahrens 	zvol_extent_t *ze;
302e7cbe64fSgw 
30388b7b0f2SMatthew Ahrens 	while (ze = list_head(&zv->zv_extents)) {
30488b7b0f2SMatthew Ahrens 		list_remove(&zv->zv_extents, ze);
30588b7b0f2SMatthew Ahrens 		kmem_free(ze, sizeof (zvol_extent_t));
306e7cbe64fSgw 	}
30788b7b0f2SMatthew Ahrens }
308e7cbe64fSgw 
30988b7b0f2SMatthew Ahrens static int
31088b7b0f2SMatthew Ahrens zvol_get_lbas(zvol_state_t *zv)
31188b7b0f2SMatthew Ahrens {
31288b7b0f2SMatthew Ahrens 	struct maparg	ma;
31388b7b0f2SMatthew Ahrens 	int		err;
31488b7b0f2SMatthew Ahrens 
31588b7b0f2SMatthew Ahrens 	ma.ma_zv = zv;
31688b7b0f2SMatthew Ahrens 	ma.ma_blks = 0;
31788b7b0f2SMatthew Ahrens 	zvol_free_extents(zv);
31888b7b0f2SMatthew Ahrens 
31988b7b0f2SMatthew Ahrens 	err = traverse_dataset(dmu_objset_ds(zv->zv_objset), 0,
32088b7b0f2SMatthew Ahrens 	    TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma);
32188b7b0f2SMatthew Ahrens 	if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) {
32288b7b0f2SMatthew Ahrens 		zvol_free_extents(zv);
32388b7b0f2SMatthew Ahrens 		return (err ? err : EIO);
324e7cbe64fSgw 	}
32588b7b0f2SMatthew Ahrens 
326e7cbe64fSgw 	return (0);
327e7cbe64fSgw }
328e7cbe64fSgw 
329ecd6cf80Smarks /* ARGSUSED */
330fa9e4066Sahrens void
331ecd6cf80Smarks zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
332fa9e4066Sahrens {
333da6c28aaSamw 	zfs_creat_t *zct = arg;
334da6c28aaSamw 	nvlist_t *nvprops = zct->zct_props;
335fa9e4066Sahrens 	int error;
336e9dbad6fSeschrock 	uint64_t volblocksize, volsize;
337fa9e4066Sahrens 
338ecd6cf80Smarks 	VERIFY(nvlist_lookup_uint64(nvprops,
339e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
340ecd6cf80Smarks 	if (nvlist_lookup_uint64(nvprops,
341e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
342e9dbad6fSeschrock 		volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
343e9dbad6fSeschrock 
344e9dbad6fSeschrock 	/*
345e7cbe64fSgw 	 * These properties must be removed from the list so the generic
346e9dbad6fSeschrock 	 * property setting step won't apply to them.
347e9dbad6fSeschrock 	 */
348ecd6cf80Smarks 	VERIFY(nvlist_remove_all(nvprops,
349e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
350ecd6cf80Smarks 	(void) nvlist_remove_all(nvprops,
351e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
352e9dbad6fSeschrock 
353e9dbad6fSeschrock 	error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
354fa9e4066Sahrens 	    DMU_OT_NONE, 0, tx);
355fa9e4066Sahrens 	ASSERT(error == 0);
356fa9e4066Sahrens 
357fa9e4066Sahrens 	error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
358fa9e4066Sahrens 	    DMU_OT_NONE, 0, tx);
359fa9e4066Sahrens 	ASSERT(error == 0);
360fa9e4066Sahrens 
361e9dbad6fSeschrock 	error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
362fa9e4066Sahrens 	ASSERT(error == 0);
363fa9e4066Sahrens }
364fa9e4066Sahrens 
36522ac5be4Sperrin /*
36622ac5be4Sperrin  * Replay a TX_WRITE ZIL transaction that didn't get committed
36722ac5be4Sperrin  * after a system failure
36822ac5be4Sperrin  */
36922ac5be4Sperrin static int
37022ac5be4Sperrin zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
37122ac5be4Sperrin {
37222ac5be4Sperrin 	objset_t *os = zv->zv_objset;
37322ac5be4Sperrin 	char *data = (char *)(lr + 1);	/* data follows lr_write_t */
37422ac5be4Sperrin 	uint64_t off = lr->lr_offset;
37522ac5be4Sperrin 	uint64_t len = lr->lr_length;
37622ac5be4Sperrin 	dmu_tx_t *tx;
37722ac5be4Sperrin 	int error;
37822ac5be4Sperrin 
37922ac5be4Sperrin 	if (byteswap)
38022ac5be4Sperrin 		byteswap_uint64_array(lr, sizeof (*lr));
38122ac5be4Sperrin 
38222ac5be4Sperrin 	tx = dmu_tx_create(os);
38322ac5be4Sperrin 	dmu_tx_hold_write(tx, ZVOL_OBJ, off, len);
38422ac5be4Sperrin 	error = dmu_tx_assign(tx, zv->zv_txg_assign);
38522ac5be4Sperrin 	if (error) {
38622ac5be4Sperrin 		dmu_tx_abort(tx);
38722ac5be4Sperrin 	} else {
38822ac5be4Sperrin 		dmu_write(os, ZVOL_OBJ, off, len, data, tx);
38922ac5be4Sperrin 		dmu_tx_commit(tx);
39022ac5be4Sperrin 	}
39122ac5be4Sperrin 
39222ac5be4Sperrin 	return (error);
39322ac5be4Sperrin }
39422ac5be4Sperrin 
39522ac5be4Sperrin /* ARGSUSED */
39622ac5be4Sperrin static int
39722ac5be4Sperrin zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
39822ac5be4Sperrin {
39922ac5be4Sperrin 	return (ENOTSUP);
40022ac5be4Sperrin }
40122ac5be4Sperrin 
40222ac5be4Sperrin /*
40322ac5be4Sperrin  * Callback vectors for replaying records.
40422ac5be4Sperrin  * Only TX_WRITE is needed for zvol.
40522ac5be4Sperrin  */
40622ac5be4Sperrin zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = {
40722ac5be4Sperrin 	zvol_replay_err,	/* 0 no such transaction type */
40822ac5be4Sperrin 	zvol_replay_err,	/* TX_CREATE */
40922ac5be4Sperrin 	zvol_replay_err,	/* TX_MKDIR */
41022ac5be4Sperrin 	zvol_replay_err,	/* TX_MKXATTR */
41122ac5be4Sperrin 	zvol_replay_err,	/* TX_SYMLINK */
41222ac5be4Sperrin 	zvol_replay_err,	/* TX_REMOVE */
41322ac5be4Sperrin 	zvol_replay_err,	/* TX_RMDIR */
41422ac5be4Sperrin 	zvol_replay_err,	/* TX_LINK */
41522ac5be4Sperrin 	zvol_replay_err,	/* TX_RENAME */
41622ac5be4Sperrin 	zvol_replay_write,	/* TX_WRITE */
41722ac5be4Sperrin 	zvol_replay_err,	/* TX_TRUNCATE */
41822ac5be4Sperrin 	zvol_replay_err,	/* TX_SETATTR */
41922ac5be4Sperrin 	zvol_replay_err,	/* TX_ACL */
42022ac5be4Sperrin };
42122ac5be4Sperrin 
422e7cbe64fSgw /*
423e7cbe64fSgw  * Create a minor node (plus a whole lot more) for the specified volume.
424fa9e4066Sahrens  */
425fa9e4066Sahrens int
42691ebeef5Sahrens zvol_create_minor(const char *name, major_t maj)
427fa9e4066Sahrens {
428fa9e4066Sahrens 	zvol_state_t *zv;
429fa9e4066Sahrens 	objset_t *os;
43067bd71c6Sperrin 	dmu_object_info_t doi;
431fa9e4066Sahrens 	uint64_t volsize;
432fa9e4066Sahrens 	minor_t minor = 0;
433fa9e4066Sahrens 	struct pathname linkpath;
434745cd3c5Smaybee 	int ds_mode = DS_MODE_OWNER;
435fa9e4066Sahrens 	vnode_t *vp = NULL;
436fa9e4066Sahrens 	char *devpath;
437e7cbe64fSgw 	size_t devpathlen = strlen(ZVOL_FULL_DEV_DIR) + strlen(name) + 1;
438fa9e4066Sahrens 	char chrbuf[30], blkbuf[30];
439fa9e4066Sahrens 	int error;
440fa9e4066Sahrens 
441fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
442fa9e4066Sahrens 
443fa9e4066Sahrens 	if ((zv = zvol_minor_lookup(name)) != NULL) {
444fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
445fa9e4066Sahrens 		return (EEXIST);
446fa9e4066Sahrens 	}
447fa9e4066Sahrens 
448fa9e4066Sahrens 	if (strchr(name, '@') != 0)
449fa9e4066Sahrens 		ds_mode |= DS_MODE_READONLY;
450fa9e4066Sahrens 
451fa9e4066Sahrens 	error = dmu_objset_open(name, DMU_OST_ZVOL, ds_mode, &os);
452fa9e4066Sahrens 
453fa9e4066Sahrens 	if (error) {
454fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
455fa9e4066Sahrens 		return (error);
456fa9e4066Sahrens 	}
457fa9e4066Sahrens 
458fa9e4066Sahrens 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
459fa9e4066Sahrens 
460fa9e4066Sahrens 	if (error) {
461fa9e4066Sahrens 		dmu_objset_close(os);
462fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
463fa9e4066Sahrens 		return (error);
464fa9e4066Sahrens 	}
465fa9e4066Sahrens 
466fa9e4066Sahrens 	/*
467fa9e4066Sahrens 	 * If there's an existing /dev/zvol symlink, try to use the
468fa9e4066Sahrens 	 * same minor number we used last time.
469fa9e4066Sahrens 	 */
470fa9e4066Sahrens 	devpath = kmem_alloc(devpathlen, KM_SLEEP);
471fa9e4066Sahrens 
472e7cbe64fSgw 	(void) sprintf(devpath, "%s%s", ZVOL_FULL_DEV_DIR, name);
473fa9e4066Sahrens 
474fa9e4066Sahrens 	error = lookupname(devpath, UIO_SYSSPACE, NO_FOLLOW, NULL, &vp);
475fa9e4066Sahrens 
476fa9e4066Sahrens 	kmem_free(devpath, devpathlen);
477fa9e4066Sahrens 
478fa9e4066Sahrens 	if (error == 0 && vp->v_type != VLNK)
479fa9e4066Sahrens 		error = EINVAL;
480fa9e4066Sahrens 
481fa9e4066Sahrens 	if (error == 0) {
482fa9e4066Sahrens 		pn_alloc(&linkpath);
483fa9e4066Sahrens 		error = pn_getsymlink(vp, &linkpath, kcred);
484fa9e4066Sahrens 		if (error == 0) {
485fa9e4066Sahrens 			char *ms = strstr(linkpath.pn_path, ZVOL_PSEUDO_DEV);
486fa9e4066Sahrens 			if (ms != NULL) {
487fa9e4066Sahrens 				ms += strlen(ZVOL_PSEUDO_DEV);
488fa9e4066Sahrens 				minor = stoi(&ms);
489fa9e4066Sahrens 			}
490fa9e4066Sahrens 		}
491fa9e4066Sahrens 		pn_free(&linkpath);
492fa9e4066Sahrens 	}
493fa9e4066Sahrens 
494fa9e4066Sahrens 	if (vp != NULL)
495fa9e4066Sahrens 		VN_RELE(vp);
496fa9e4066Sahrens 
497fa9e4066Sahrens 	/*
498fa9e4066Sahrens 	 * If we found a minor but it's already in use, we must pick a new one.
499fa9e4066Sahrens 	 */
500fa9e4066Sahrens 	if (minor != 0 && ddi_get_soft_state(zvol_state, minor) != NULL)
501fa9e4066Sahrens 		minor = 0;
502fa9e4066Sahrens 
503fa9e4066Sahrens 	if (minor == 0)
504fa9e4066Sahrens 		minor = zvol_minor_alloc();
505fa9e4066Sahrens 
506fa9e4066Sahrens 	if (minor == 0) {
507fa9e4066Sahrens 		dmu_objset_close(os);
508fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
509fa9e4066Sahrens 		return (ENXIO);
510fa9e4066Sahrens 	}
511fa9e4066Sahrens 
512fa9e4066Sahrens 	if (ddi_soft_state_zalloc(zvol_state, minor) != DDI_SUCCESS) {
513fa9e4066Sahrens 		dmu_objset_close(os);
514fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
515fa9e4066Sahrens 		return (EAGAIN);
516fa9e4066Sahrens 	}
517fa9e4066Sahrens 
518e9dbad6fSeschrock 	(void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME,
519e9dbad6fSeschrock 	    (char *)name);
520fa9e4066Sahrens 
521fa9e4066Sahrens 	(void) sprintf(chrbuf, "%uc,raw", minor);
522fa9e4066Sahrens 
523fa9e4066Sahrens 	if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR,
524fa9e4066Sahrens 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
525fa9e4066Sahrens 		ddi_soft_state_free(zvol_state, minor);
526fa9e4066Sahrens 		dmu_objset_close(os);
527fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
528fa9e4066Sahrens 		return (EAGAIN);
529fa9e4066Sahrens 	}
530fa9e4066Sahrens 
531fa9e4066Sahrens 	(void) sprintf(blkbuf, "%uc", minor);
532fa9e4066Sahrens 
533fa9e4066Sahrens 	if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK,
534fa9e4066Sahrens 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
535fa9e4066Sahrens 		ddi_remove_minor_node(zfs_dip, chrbuf);
536fa9e4066Sahrens 		ddi_soft_state_free(zvol_state, minor);
537fa9e4066Sahrens 		dmu_objset_close(os);
538fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
539fa9e4066Sahrens 		return (EAGAIN);
540fa9e4066Sahrens 	}
541fa9e4066Sahrens 
542fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
543fa9e4066Sahrens 
544fa9e4066Sahrens 	(void) strcpy(zv->zv_name, name);
545fa9e4066Sahrens 	zv->zv_min_bs = DEV_BSHIFT;
546fa9e4066Sahrens 	zv->zv_minor = minor;
547fa9e4066Sahrens 	zv->zv_volsize = volsize;
548fa9e4066Sahrens 	zv->zv_objset = os;
549fa9e4066Sahrens 	zv->zv_mode = ds_mode;
55067bd71c6Sperrin 	zv->zv_zilog = zil_open(os, zvol_get_data);
551c2e6a7d6Sperrin 	mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
552c2e6a7d6Sperrin 	avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
553c2e6a7d6Sperrin 	    sizeof (rl_t), offsetof(rl_t, r_node));
55488b7b0f2SMatthew Ahrens 	list_create(&zv->zv_extents, sizeof (zvol_extent_t),
55588b7b0f2SMatthew Ahrens 	    offsetof(zvol_extent_t, ze_node));
55667bd71c6Sperrin 	/* get and cache the blocksize */
55767bd71c6Sperrin 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
55867bd71c6Sperrin 	ASSERT(error == 0);
55967bd71c6Sperrin 	zv->zv_volblocksize = doi.doi_data_block_size;
56022ac5be4Sperrin 
561a6e57bd4SNeil Perrin 	zil_replay(os, zv, &zv->zv_txg_assign, zvol_replay_vector, NULL);
56291ebeef5Sahrens 	zvol_size_changed(zv, maj);
563fa9e4066Sahrens 
564ea8dc4b6Seschrock 	/* XXX this should handle the possible i/o error */
565fa9e4066Sahrens 	VERIFY(dsl_prop_register(dmu_objset_ds(zv->zv_objset),
566fa9e4066Sahrens 	    "readonly", zvol_readonly_changed_cb, zv) == 0);
567fa9e4066Sahrens 
568fa9e4066Sahrens 	zvol_minors++;
569fa9e4066Sahrens 
570fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
571fa9e4066Sahrens 
572fa9e4066Sahrens 	return (0);
573fa9e4066Sahrens }
574fa9e4066Sahrens 
575fa9e4066Sahrens /*
576fa9e4066Sahrens  * Remove minor node for the specified volume.
577fa9e4066Sahrens  */
578fa9e4066Sahrens int
579e9dbad6fSeschrock zvol_remove_minor(const char *name)
580fa9e4066Sahrens {
581fa9e4066Sahrens 	zvol_state_t *zv;
582fa9e4066Sahrens 	char namebuf[30];
583fa9e4066Sahrens 
584fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
585fa9e4066Sahrens 
586e9dbad6fSeschrock 	if ((zv = zvol_minor_lookup(name)) == NULL) {
587fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
588fa9e4066Sahrens 		return (ENXIO);
589fa9e4066Sahrens 	}
590fa9e4066Sahrens 
591fa9e4066Sahrens 	if (zv->zv_total_opens != 0) {
592fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
593fa9e4066Sahrens 		return (EBUSY);
594fa9e4066Sahrens 	}
595fa9e4066Sahrens 
596fa9e4066Sahrens 	(void) sprintf(namebuf, "%uc,raw", zv->zv_minor);
597fa9e4066Sahrens 	ddi_remove_minor_node(zfs_dip, namebuf);
598fa9e4066Sahrens 
599fa9e4066Sahrens 	(void) sprintf(namebuf, "%uc", zv->zv_minor);
600fa9e4066Sahrens 	ddi_remove_minor_node(zfs_dip, namebuf);
601fa9e4066Sahrens 
602fa9e4066Sahrens 	VERIFY(dsl_prop_unregister(dmu_objset_ds(zv->zv_objset),
603fa9e4066Sahrens 	    "readonly", zvol_readonly_changed_cb, zv) == 0);
604fa9e4066Sahrens 
60522ac5be4Sperrin 	zil_close(zv->zv_zilog);
60622ac5be4Sperrin 	zv->zv_zilog = NULL;
607fa9e4066Sahrens 	dmu_objset_close(zv->zv_objset);
608fa9e4066Sahrens 	zv->zv_objset = NULL;
609c2e6a7d6Sperrin 	avl_destroy(&zv->zv_znode.z_range_avl);
610c2e6a7d6Sperrin 	mutex_destroy(&zv->zv_znode.z_range_lock);
611fa9e4066Sahrens 
612fa9e4066Sahrens 	ddi_soft_state_free(zvol_state, zv->zv_minor);
613fa9e4066Sahrens 
614fa9e4066Sahrens 	zvol_minors--;
615fa9e4066Sahrens 
616fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
617fa9e4066Sahrens 
618fa9e4066Sahrens 	return (0);
619fa9e4066Sahrens }
620fa9e4066Sahrens 
621e7cbe64fSgw int
622e7cbe64fSgw zvol_prealloc(zvol_state_t *zv)
623e7cbe64fSgw {
624e7cbe64fSgw 	objset_t *os = zv->zv_objset;
625e7cbe64fSgw 	dmu_tx_t *tx;
626e7cbe64fSgw 	uint64_t refd, avail, usedobjs, availobjs;
627e7cbe64fSgw 	uint64_t resid = zv->zv_volsize;
628e7cbe64fSgw 	uint64_t off = 0;
629e7cbe64fSgw 
630e7cbe64fSgw 	/* Check the space usage before attempting to allocate the space */
631e7cbe64fSgw 	dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs);
632e7cbe64fSgw 	if (avail < zv->zv_volsize)
633e7cbe64fSgw 		return (ENOSPC);
634e7cbe64fSgw 
635e7cbe64fSgw 	/* Free old extents if they exist */
636e7cbe64fSgw 	zvol_free_extents(zv);
637e7cbe64fSgw 
638e7cbe64fSgw 	while (resid != 0) {
639e7cbe64fSgw 		int error;
640e7cbe64fSgw 		uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE);
641e7cbe64fSgw 
642e7cbe64fSgw 		tx = dmu_tx_create(os);
643e7cbe64fSgw 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
644e7cbe64fSgw 		error = dmu_tx_assign(tx, TXG_WAIT);
645e7cbe64fSgw 		if (error) {
646e7cbe64fSgw 			dmu_tx_abort(tx);
647cdb0ab79Smaybee 			(void) dmu_free_long_range(os, ZVOL_OBJ, 0, off);
648e7cbe64fSgw 			return (error);
649e7cbe64fSgw 		}
65082c9918fSTim Haley 		dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx);
651e7cbe64fSgw 		dmu_tx_commit(tx);
652e7cbe64fSgw 		off += bytes;
653e7cbe64fSgw 		resid -= bytes;
654e7cbe64fSgw 	}
655e7cbe64fSgw 	txg_wait_synced(dmu_objset_pool(os), 0);
656e7cbe64fSgw 
657e7cbe64fSgw 	return (0);
658e7cbe64fSgw }
659e7cbe64fSgw 
660e7cbe64fSgw int
661e7cbe64fSgw zvol_update_volsize(zvol_state_t *zv, major_t maj, uint64_t volsize)
662e7cbe64fSgw {
663e7cbe64fSgw 	dmu_tx_t *tx;
664e7cbe64fSgw 	int error;
665e7cbe64fSgw 
666e7cbe64fSgw 	ASSERT(MUTEX_HELD(&zvol_state_lock));
667e7cbe64fSgw 
668e7cbe64fSgw 	tx = dmu_tx_create(zv->zv_objset);
669e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
670e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
671e7cbe64fSgw 	if (error) {
672e7cbe64fSgw 		dmu_tx_abort(tx);
673e7cbe64fSgw 		return (error);
674e7cbe64fSgw 	}
675e7cbe64fSgw 
676e7cbe64fSgw 	error = zap_update(zv->zv_objset, ZVOL_ZAP_OBJ, "size", 8, 1,
677e7cbe64fSgw 	    &volsize, tx);
678e7cbe64fSgw 	dmu_tx_commit(tx);
679e7cbe64fSgw 
680e7cbe64fSgw 	if (error == 0)
681cdb0ab79Smaybee 		error = dmu_free_long_range(zv->zv_objset,
682cdb0ab79Smaybee 		    ZVOL_OBJ, volsize, DMU_OBJECT_END);
683e7cbe64fSgw 
684bb0ade09Sahrens 	/*
685bb0ade09Sahrens 	 * If we are using a faked-up state (zv_minor == 0) then don't
686bb0ade09Sahrens 	 * try to update the in-core zvol state.
687bb0ade09Sahrens 	 */
688bb0ade09Sahrens 	if (error == 0 && zv->zv_minor) {
689e7cbe64fSgw 		zv->zv_volsize = volsize;
690e7cbe64fSgw 		zvol_size_changed(zv, maj);
691e7cbe64fSgw 	}
692e7cbe64fSgw 	return (error);
693e7cbe64fSgw }
694e7cbe64fSgw 
695fa9e4066Sahrens int
69691ebeef5Sahrens zvol_set_volsize(const char *name, major_t maj, uint64_t volsize)
697fa9e4066Sahrens {
698fa9e4066Sahrens 	zvol_state_t *zv;
699fa9e4066Sahrens 	int error;
7005c5460e9Seschrock 	dmu_object_info_t doi;
701e7cbe64fSgw 	uint64_t old_volsize = 0ULL;
702bb0ade09Sahrens 	zvol_state_t state = { 0 };
703fa9e4066Sahrens 
704fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
705fa9e4066Sahrens 
706e9dbad6fSeschrock 	if ((zv = zvol_minor_lookup(name)) == NULL) {
707bb0ade09Sahrens 		/*
708bb0ade09Sahrens 		 * If we are doing a "zfs clone -o volsize=", then the
709bb0ade09Sahrens 		 * minor node won't exist yet.
710bb0ade09Sahrens 		 */
711bb0ade09Sahrens 		error = dmu_objset_open(name, DMU_OST_ZVOL, DS_MODE_OWNER,
712bb0ade09Sahrens 		    &state.zv_objset);
713bb0ade09Sahrens 		if (error != 0)
714bb0ade09Sahrens 			goto out;
715bb0ade09Sahrens 		zv = &state;
716fa9e4066Sahrens 	}
717e7cbe64fSgw 	old_volsize = zv->zv_volsize;
718fa9e4066Sahrens 
7195c5460e9Seschrock 	if ((error = dmu_object_info(zv->zv_objset, ZVOL_OBJ, &doi)) != 0 ||
720e9dbad6fSeschrock 	    (error = zvol_check_volsize(volsize,
721bb0ade09Sahrens 	    doi.doi_data_block_size)) != 0)
722bb0ade09Sahrens 		goto out;
7235c5460e9Seschrock 
724e7cbe64fSgw 	if (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY)) {
725bb0ade09Sahrens 		error = EROFS;
726bb0ade09Sahrens 		goto out;
727fa9e4066Sahrens 	}
728fa9e4066Sahrens 
729e7cbe64fSgw 	error = zvol_update_volsize(zv, maj, volsize);
730fa9e4066Sahrens 
731e7cbe64fSgw 	/*
732e7cbe64fSgw 	 * Reinitialize the dump area to the new size. If we
733e7cbe64fSgw 	 * failed to resize the dump area then restore the it back to
734e7cbe64fSgw 	 * it's original size.
735e7cbe64fSgw 	 */
736e7cbe64fSgw 	if (error == 0 && zv->zv_flags & ZVOL_DUMPIFIED) {
737e7cbe64fSgw 		if ((error = zvol_dumpify(zv)) != 0 ||
738e7cbe64fSgw 		    (error = dumpvp_resize()) != 0) {
739e7cbe64fSgw 			(void) zvol_update_volsize(zv, maj, old_volsize);
740e7cbe64fSgw 			error = zvol_dumpify(zv);
741e7cbe64fSgw 		}
742fa9e4066Sahrens 	}
743fa9e4066Sahrens 
744bb0ade09Sahrens out:
745bb0ade09Sahrens 	if (state.zv_objset)
746bb0ade09Sahrens 		dmu_objset_close(state.zv_objset);
747bb0ade09Sahrens 
748fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
749fa9e4066Sahrens 
750fa9e4066Sahrens 	return (error);
751fa9e4066Sahrens }
752fa9e4066Sahrens 
753fa9e4066Sahrens int
754e9dbad6fSeschrock zvol_set_volblocksize(const char *name, uint64_t volblocksize)
755fa9e4066Sahrens {
756fa9e4066Sahrens 	zvol_state_t *zv;
757fa9e4066Sahrens 	dmu_tx_t *tx;
758fa9e4066Sahrens 	int error;
75988b7b0f2SMatthew Ahrens 	boolean_t needlock;
760fa9e4066Sahrens 
76188b7b0f2SMatthew Ahrens 	/*
76288b7b0f2SMatthew Ahrens 	 * The lock may already be held if we are being called from
76388b7b0f2SMatthew Ahrens 	 * zvol_dump_init().
76488b7b0f2SMatthew Ahrens 	 */
76588b7b0f2SMatthew Ahrens 	needlock = !MUTEX_HELD(&zvol_state_lock);
76688b7b0f2SMatthew Ahrens 	if (needlock)
76788b7b0f2SMatthew Ahrens 		mutex_enter(&zvol_state_lock);
768fa9e4066Sahrens 
769e9dbad6fSeschrock 	if ((zv = zvol_minor_lookup(name)) == NULL) {
77088b7b0f2SMatthew Ahrens 		if (needlock)
77188b7b0f2SMatthew Ahrens 			mutex_exit(&zvol_state_lock);
772fa9e4066Sahrens 		return (ENXIO);
773fa9e4066Sahrens 	}
774e7cbe64fSgw 	if (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY)) {
77588b7b0f2SMatthew Ahrens 		if (needlock)
77688b7b0f2SMatthew Ahrens 			mutex_exit(&zvol_state_lock);
777fa9e4066Sahrens 		return (EROFS);
778fa9e4066Sahrens 	}
779fa9e4066Sahrens 
780fa9e4066Sahrens 	tx = dmu_tx_create(zv->zv_objset);
781fa9e4066Sahrens 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
782fa9e4066Sahrens 	error = dmu_tx_assign(tx, TXG_WAIT);
783fa9e4066Sahrens 	if (error) {
784fa9e4066Sahrens 		dmu_tx_abort(tx);
785fa9e4066Sahrens 	} else {
786fa9e4066Sahrens 		error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ,
787e9dbad6fSeschrock 		    volblocksize, 0, tx);
788fa9e4066Sahrens 		if (error == ENOTSUP)
789fa9e4066Sahrens 			error = EBUSY;
790fa9e4066Sahrens 		dmu_tx_commit(tx);
79188b7b0f2SMatthew Ahrens 		if (error == 0)
79288b7b0f2SMatthew Ahrens 			zv->zv_volblocksize = volblocksize;
793fa9e4066Sahrens 	}
794fa9e4066Sahrens 
79588b7b0f2SMatthew Ahrens 	if (needlock)
79688b7b0f2SMatthew Ahrens 		mutex_exit(&zvol_state_lock);
797fa9e4066Sahrens 
798fa9e4066Sahrens 	return (error);
799fa9e4066Sahrens }
800fa9e4066Sahrens 
801fa9e4066Sahrens /*ARGSUSED*/
802fa9e4066Sahrens int
803fa9e4066Sahrens zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr)
804fa9e4066Sahrens {
805fa9e4066Sahrens 	minor_t minor = getminor(*devp);
806fa9e4066Sahrens 	zvol_state_t *zv;
807fa9e4066Sahrens 
808fa9e4066Sahrens 	if (minor == 0)			/* This is the control device */
809fa9e4066Sahrens 		return (0);
810fa9e4066Sahrens 
811fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
812fa9e4066Sahrens 
813fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
814fa9e4066Sahrens 	if (zv == NULL) {
815fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
816fa9e4066Sahrens 		return (ENXIO);
817fa9e4066Sahrens 	}
818fa9e4066Sahrens 
819fa9e4066Sahrens 	ASSERT(zv->zv_objset != NULL);
820fa9e4066Sahrens 
821fa9e4066Sahrens 	if ((flag & FWRITE) &&
822e7cbe64fSgw 	    (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY))) {
823fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
824fa9e4066Sahrens 		return (EROFS);
825fa9e4066Sahrens 	}
826c7f714e2SEric Taylor 	if (zv->zv_flags & ZVOL_EXCL) {
827c7f714e2SEric Taylor 		mutex_exit(&zvol_state_lock);
828c7f714e2SEric Taylor 		return (EBUSY);
829c7f714e2SEric Taylor 	}
830c7f714e2SEric Taylor 	if (flag & FEXCL) {
831c7f714e2SEric Taylor 		if (zv->zv_total_opens != 0) {
832c7f714e2SEric Taylor 			mutex_exit(&zvol_state_lock);
833c7f714e2SEric Taylor 			return (EBUSY);
834c7f714e2SEric Taylor 		}
835c7f714e2SEric Taylor 		zv->zv_flags |= ZVOL_EXCL;
836c7f714e2SEric Taylor 	}
837fa9e4066Sahrens 
838fa9e4066Sahrens 	if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) {
839fa9e4066Sahrens 		zv->zv_open_count[otyp]++;
840fa9e4066Sahrens 		zv->zv_total_opens++;
841fa9e4066Sahrens 	}
842fa9e4066Sahrens 
843fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
844fa9e4066Sahrens 
845fa9e4066Sahrens 	return (0);
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;
854fa9e4066Sahrens 
855fa9e4066Sahrens 	if (minor == 0)		/* This is the control device */
856fa9e4066Sahrens 		return (0);
857fa9e4066Sahrens 
858fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
859fa9e4066Sahrens 
860fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
861fa9e4066Sahrens 	if (zv == NULL) {
862fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
863fa9e4066Sahrens 		return (ENXIO);
864fa9e4066Sahrens 	}
865fa9e4066Sahrens 
866c7f714e2SEric Taylor 	if (zv->zv_flags & ZVOL_EXCL) {
867c7f714e2SEric Taylor 		ASSERT(zv->zv_total_opens == 1);
868c7f714e2SEric Taylor 		zv->zv_flags &= ~ZVOL_EXCL;
869fa9e4066Sahrens 	}
870fa9e4066Sahrens 
871fa9e4066Sahrens 	/*
872fa9e4066Sahrens 	 * If the open count is zero, this is a spurious close.
873fa9e4066Sahrens 	 * That indicates a bug in the kernel / DDI framework.
874fa9e4066Sahrens 	 */
875fa9e4066Sahrens 	ASSERT(zv->zv_open_count[otyp] != 0);
876fa9e4066Sahrens 	ASSERT(zv->zv_total_opens != 0);
877fa9e4066Sahrens 
878fa9e4066Sahrens 	/*
879fa9e4066Sahrens 	 * You may get multiple opens, but only one close.
880fa9e4066Sahrens 	 */
881fa9e4066Sahrens 	zv->zv_open_count[otyp]--;
882fa9e4066Sahrens 	zv->zv_total_opens--;
883fa9e4066Sahrens 
884fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
885fa9e4066Sahrens 
886fa9e4066Sahrens 	return (0);
887fa9e4066Sahrens }
888fa9e4066Sahrens 
889feb08c6bSbillm static void
89067bd71c6Sperrin zvol_get_done(dmu_buf_t *db, void *vzgd)
89167bd71c6Sperrin {
89267bd71c6Sperrin 	zgd_t *zgd = (zgd_t *)vzgd;
893c2e6a7d6Sperrin 	rl_t *rl = zgd->zgd_rl;
89467bd71c6Sperrin 
89567bd71c6Sperrin 	dmu_buf_rele(db, vzgd);
896c2e6a7d6Sperrin 	zfs_range_unlock(rl);
89717f17c2dSbonwick 	zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
89867bd71c6Sperrin 	kmem_free(zgd, sizeof (zgd_t));
89967bd71c6Sperrin }
90067bd71c6Sperrin 
90167bd71c6Sperrin /*
90267bd71c6Sperrin  * Get data to generate a TX_WRITE intent log record.
90367bd71c6Sperrin  */
904feb08c6bSbillm static int
90567bd71c6Sperrin zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
90667bd71c6Sperrin {
90767bd71c6Sperrin 	zvol_state_t *zv = arg;
90867bd71c6Sperrin 	objset_t *os = zv->zv_objset;
90967bd71c6Sperrin 	dmu_buf_t *db;
910c2e6a7d6Sperrin 	rl_t *rl;
91167bd71c6Sperrin 	zgd_t *zgd;
912c2e6a7d6Sperrin 	uint64_t boff; 			/* block starting offset */
913c2e6a7d6Sperrin 	int dlen = lr->lr_length;	/* length of user data */
91467bd71c6Sperrin 	int error;
91567bd71c6Sperrin 
91667bd71c6Sperrin 	ASSERT(zio);
917c2e6a7d6Sperrin 	ASSERT(dlen != 0);
918feb08c6bSbillm 
919c2e6a7d6Sperrin 	/*
920c2e6a7d6Sperrin 	 * Write records come in two flavors: immediate and indirect.
921c2e6a7d6Sperrin 	 * For small writes it's cheaper to store the data with the
922c2e6a7d6Sperrin 	 * log record (immediate); for large writes it's cheaper to
923c2e6a7d6Sperrin 	 * sync the data and get a pointer to it (indirect) so that
924c2e6a7d6Sperrin 	 * we don't have to write the data twice.
925c2e6a7d6Sperrin 	 */
926c2e6a7d6Sperrin 	if (buf != NULL) /* immediate write */
927c2e6a7d6Sperrin 		return (dmu_read(os, ZVOL_OBJ, lr->lr_offset, dlen, buf));
92867bd71c6Sperrin 
92967bd71c6Sperrin 	zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP);
93067bd71c6Sperrin 	zgd->zgd_zilog = zv->zv_zilog;
93167bd71c6Sperrin 	zgd->zgd_bp = &lr->lr_blkptr;
93267bd71c6Sperrin 
93367bd71c6Sperrin 	/*
934c2e6a7d6Sperrin 	 * Lock the range of the block to ensure that when the data is
935e7cbe64fSgw 	 * written out and its checksum is being calculated that no other
936c2e6a7d6Sperrin 	 * thread can change the block.
93767bd71c6Sperrin 	 */
938c2e6a7d6Sperrin 	boff = P2ALIGN_TYPED(lr->lr_offset, zv->zv_volblocksize, uint64_t);
939c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, boff, zv->zv_volblocksize,
940c2e6a7d6Sperrin 	    RL_READER);
941c2e6a7d6Sperrin 	zgd->zgd_rl = rl;
942c2e6a7d6Sperrin 
943c2e6a7d6Sperrin 	VERIFY(0 == dmu_buf_hold(os, ZVOL_OBJ, lr->lr_offset, zgd, &db));
94467bd71c6Sperrin 	error = dmu_sync(zio, db, &lr->lr_blkptr,
94567bd71c6Sperrin 	    lr->lr_common.lrc_txg, zvol_get_done, zgd);
946feb08c6bSbillm 	if (error == 0)
94717f17c2dSbonwick 		zil_add_block(zv->zv_zilog, &lr->lr_blkptr);
94867bd71c6Sperrin 	/*
94967bd71c6Sperrin 	 * If we get EINPROGRESS, then we need to wait for a
95067bd71c6Sperrin 	 * write IO initiated by dmu_sync() to complete before
95167bd71c6Sperrin 	 * we can release this dbuf.  We will finish everything
95267bd71c6Sperrin 	 * up in the zvol_get_done() callback.
95367bd71c6Sperrin 	 */
95467bd71c6Sperrin 	if (error == EINPROGRESS)
95567bd71c6Sperrin 		return (0);
95667bd71c6Sperrin 	dmu_buf_rele(db, zgd);
957c2e6a7d6Sperrin 	zfs_range_unlock(rl);
95867bd71c6Sperrin 	kmem_free(zgd, sizeof (zgd_t));
95967bd71c6Sperrin 	return (error);
96067bd71c6Sperrin }
96167bd71c6Sperrin 
962a24e15ceSperrin /*
963a24e15ceSperrin  * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
96422ac5be4Sperrin  *
96522ac5be4Sperrin  * We store data in the log buffers if it's small enough.
96667bd71c6Sperrin  * Otherwise we will later flush the data out via dmu_sync().
96722ac5be4Sperrin  */
96867bd71c6Sperrin ssize_t zvol_immediate_write_sz = 32768;
96922ac5be4Sperrin 
970feb08c6bSbillm static void
971feb08c6bSbillm zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t len)
97222ac5be4Sperrin {
973feb08c6bSbillm 	uint32_t blocksize = zv->zv_volblocksize;
97422ac5be4Sperrin 	lr_write_t *lr;
97522ac5be4Sperrin 
976a24e15ceSperrin 	while (len) {
977feb08c6bSbillm 		ssize_t nbytes = MIN(len, blocksize - P2PHASE(off, blocksize));
978feb08c6bSbillm 		itx_t *itx = zil_itx_create(TX_WRITE, sizeof (*lr));
979feb08c6bSbillm 
980feb08c6bSbillm 		itx->itx_wr_state =
981feb08c6bSbillm 		    len > zvol_immediate_write_sz ?  WR_INDIRECT : WR_NEED_COPY;
982feb08c6bSbillm 		itx->itx_private = zv;
983feb08c6bSbillm 		lr = (lr_write_t *)&itx->itx_lr;
984feb08c6bSbillm 		lr->lr_foid = ZVOL_OBJ;
985feb08c6bSbillm 		lr->lr_offset = off;
986feb08c6bSbillm 		lr->lr_length = nbytes;
987feb08c6bSbillm 		lr->lr_blkoff = off - P2ALIGN_TYPED(off, blocksize, uint64_t);
988feb08c6bSbillm 		BP_ZERO(&lr->lr_blkptr);
989feb08c6bSbillm 
990feb08c6bSbillm 		(void) zil_itx_assign(zv->zv_zilog, itx, tx);
991a24e15ceSperrin 		len -= nbytes;
992a24e15ceSperrin 		off += nbytes;
99322ac5be4Sperrin 	}
99422ac5be4Sperrin }
99522ac5be4Sperrin 
99688b7b0f2SMatthew Ahrens static int
99788b7b0f2SMatthew Ahrens zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t size,
99888b7b0f2SMatthew Ahrens     boolean_t doread, boolean_t isdump)
999e7cbe64fSgw {
1000e7cbe64fSgw 	vdev_disk_t *dvd;
1001e7cbe64fSgw 	int c;
1002e7cbe64fSgw 	int numerrors = 0;
1003e7cbe64fSgw 
1004e7cbe64fSgw 	for (c = 0; c < vd->vdev_children; c++) {
100588b7b0f2SMatthew Ahrens 		ASSERT(vd->vdev_ops == &vdev_mirror_ops);
100688b7b0f2SMatthew Ahrens 		int err = zvol_dumpio_vdev(vd->vdev_child[c],
100788b7b0f2SMatthew Ahrens 		    addr, offset, size, doread, isdump);
100888b7b0f2SMatthew Ahrens 		if (err != 0) {
1009e7cbe64fSgw 			numerrors++;
101088b7b0f2SMatthew Ahrens 		} else if (doread) {
1011e7cbe64fSgw 			break;
1012e7cbe64fSgw 		}
1013e7cbe64fSgw 	}
1014e7cbe64fSgw 
1015e7cbe64fSgw 	if (!vd->vdev_ops->vdev_op_leaf)
1016e7cbe64fSgw 		return (numerrors < vd->vdev_children ? 0 : EIO);
1017e7cbe64fSgw 
1018*dc0bb255SEric Taylor 	if (doread && !vdev_readable(vd))
1019*dc0bb255SEric Taylor 		return (EIO);
1020*dc0bb255SEric Taylor 	else if (!doread && !vdev_writeable(vd))
1021e7cbe64fSgw 		return (EIO);
1022e7cbe64fSgw 
1023e7cbe64fSgw 	dvd = vd->vdev_tsd;
1024e7cbe64fSgw 	ASSERT3P(dvd, !=, NULL);
1025e7cbe64fSgw 	offset += VDEV_LABEL_START_SIZE;
1026e7cbe64fSgw 
1027e7cbe64fSgw 	if (ddi_in_panic() || isdump) {
102888b7b0f2SMatthew Ahrens 		ASSERT(!doread);
102988b7b0f2SMatthew Ahrens 		if (doread)
1030e7cbe64fSgw 			return (EIO);
1031e7cbe64fSgw 		return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset),
1032e7cbe64fSgw 		    lbtodb(size)));
1033e7cbe64fSgw 	} else {
1034e7cbe64fSgw 		return (vdev_disk_physio(dvd->vd_lh, addr, size, offset,
103588b7b0f2SMatthew Ahrens 		    doread ? B_READ : B_WRITE));
1036e7cbe64fSgw 	}
1037e7cbe64fSgw }
1038e7cbe64fSgw 
103988b7b0f2SMatthew Ahrens static int
104088b7b0f2SMatthew Ahrens zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size,
104188b7b0f2SMatthew Ahrens     boolean_t doread, boolean_t isdump)
1042e7cbe64fSgw {
1043e7cbe64fSgw 	vdev_t *vd;
1044e7cbe64fSgw 	int error;
104588b7b0f2SMatthew Ahrens 	zvol_extent_t *ze;
1046e7cbe64fSgw 	spa_t *spa = dmu_objset_spa(zv->zv_objset);
1047e7cbe64fSgw 
104888b7b0f2SMatthew Ahrens 	/* Must be sector aligned, and not stradle a block boundary. */
104988b7b0f2SMatthew Ahrens 	if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) ||
105088b7b0f2SMatthew Ahrens 	    P2BOUNDARY(offset, size, zv->zv_volblocksize)) {
1051e7cbe64fSgw 		return (EINVAL);
105288b7b0f2SMatthew Ahrens 	}
105388b7b0f2SMatthew Ahrens 	ASSERT(size <= zv->zv_volblocksize);
1054e7cbe64fSgw 
105588b7b0f2SMatthew Ahrens 	/* Locate the extent this belongs to */
105688b7b0f2SMatthew Ahrens 	ze = list_head(&zv->zv_extents);
105788b7b0f2SMatthew Ahrens 	while (offset >= ze->ze_nblks * zv->zv_volblocksize) {
105888b7b0f2SMatthew Ahrens 		offset -= ze->ze_nblks * zv->zv_volblocksize;
105988b7b0f2SMatthew Ahrens 		ze = list_next(&zv->zv_extents, ze);
106088b7b0f2SMatthew Ahrens 	}
1061e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
106288b7b0f2SMatthew Ahrens 	vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva));
106388b7b0f2SMatthew Ahrens 	offset += DVA_GET_OFFSET(&ze->ze_dva);
106488b7b0f2SMatthew Ahrens 	error = zvol_dumpio_vdev(vd, addr, offset, size, doread, isdump);
1065e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
1066e7cbe64fSgw 	return (error);
1067e7cbe64fSgw }
1068e7cbe64fSgw 
1069fa9e4066Sahrens int
1070fa9e4066Sahrens zvol_strategy(buf_t *bp)
1071fa9e4066Sahrens {
1072fa9e4066Sahrens 	zvol_state_t *zv = ddi_get_soft_state(zvol_state, getminor(bp->b_edev));
1073fa9e4066Sahrens 	uint64_t off, volsize;
107488b7b0f2SMatthew Ahrens 	size_t resid;
1075fa9e4066Sahrens 	char *addr;
107622ac5be4Sperrin 	objset_t *os;
1077c2e6a7d6Sperrin 	rl_t *rl;
1078fa9e4066Sahrens 	int error = 0;
107988b7b0f2SMatthew Ahrens 	boolean_t doread = bp->b_flags & B_READ;
108088b7b0f2SMatthew Ahrens 	boolean_t is_dump = zv->zv_flags & ZVOL_DUMPIFIED;
1081fa9e4066Sahrens 
1082fa9e4066Sahrens 	if (zv == NULL) {
1083fa9e4066Sahrens 		bioerror(bp, ENXIO);
1084fa9e4066Sahrens 		biodone(bp);
1085fa9e4066Sahrens 		return (0);
1086fa9e4066Sahrens 	}
1087fa9e4066Sahrens 
1088fa9e4066Sahrens 	if (getminor(bp->b_edev) == 0) {
1089fa9e4066Sahrens 		bioerror(bp, EINVAL);
1090fa9e4066Sahrens 		biodone(bp);
1091fa9e4066Sahrens 		return (0);
1092fa9e4066Sahrens 	}
1093fa9e4066Sahrens 
1094e7cbe64fSgw 	if (!(bp->b_flags & B_READ) &&
1095e7cbe64fSgw 	    (zv->zv_flags & ZVOL_RDONLY ||
1096e7cbe64fSgw 	    zv->zv_mode & DS_MODE_READONLY)) {
1097fa9e4066Sahrens 		bioerror(bp, EROFS);
1098fa9e4066Sahrens 		biodone(bp);
1099fa9e4066Sahrens 		return (0);
1100fa9e4066Sahrens 	}
1101fa9e4066Sahrens 
1102fa9e4066Sahrens 	off = ldbtob(bp->b_blkno);
1103fa9e4066Sahrens 	volsize = zv->zv_volsize;
1104fa9e4066Sahrens 
110522ac5be4Sperrin 	os = zv->zv_objset;
110622ac5be4Sperrin 	ASSERT(os != NULL);
1107fa9e4066Sahrens 
1108fa9e4066Sahrens 	bp_mapin(bp);
1109fa9e4066Sahrens 	addr = bp->b_un.b_addr;
1110fa9e4066Sahrens 	resid = bp->b_bcount;
1111fa9e4066Sahrens 
111288b7b0f2SMatthew Ahrens 	if (resid > 0 && (off < 0 || off >= volsize)) {
111388b7b0f2SMatthew Ahrens 		bioerror(bp, EIO);
111488b7b0f2SMatthew Ahrens 		biodone(bp);
111588b7b0f2SMatthew Ahrens 		return (0);
111688b7b0f2SMatthew Ahrens 	}
111773ec3d9cSgw 
1118a24e15ceSperrin 	/*
1119a24e15ceSperrin 	 * There must be no buffer changes when doing a dmu_sync() because
1120a24e15ceSperrin 	 * we can't change the data whilst calculating the checksum.
1121a24e15ceSperrin 	 */
1122c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, off, resid,
112388b7b0f2SMatthew Ahrens 	    doread ? RL_READER : RL_WRITER);
1124fa9e4066Sahrens 
1125e7cbe64fSgw 	while (resid != 0 && off < volsize) {
112688b7b0f2SMatthew Ahrens 		size_t size = MIN(resid, zvol_maxphys);
1127e7cbe64fSgw 		if (is_dump) {
1128e7cbe64fSgw 			size = MIN(size, P2END(off, zv->zv_volblocksize) - off);
112988b7b0f2SMatthew Ahrens 			error = zvol_dumpio(zv, addr, off, size,
113088b7b0f2SMatthew Ahrens 			    doread, B_FALSE);
113188b7b0f2SMatthew Ahrens 		} else if (doread) {
1132a24e15ceSperrin 			error = dmu_read(os, ZVOL_OBJ, off, size, addr);
1133fa9e4066Sahrens 		} else {
113422ac5be4Sperrin 			dmu_tx_t *tx = dmu_tx_create(os);
1135fa9e4066Sahrens 			dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
1136fa9e4066Sahrens 			error = dmu_tx_assign(tx, TXG_WAIT);
1137fa9e4066Sahrens 			if (error) {
1138fa9e4066Sahrens 				dmu_tx_abort(tx);
1139fa9e4066Sahrens 			} else {
114022ac5be4Sperrin 				dmu_write(os, ZVOL_OBJ, off, size, addr, tx);
1141feb08c6bSbillm 				zvol_log_write(zv, tx, off, size);
1142fa9e4066Sahrens 				dmu_tx_commit(tx);
1143fa9e4066Sahrens 			}
1144fa9e4066Sahrens 		}
1145b87f3af3Sperrin 		if (error) {
1146b87f3af3Sperrin 			/* convert checksum errors into IO errors */
1147b87f3af3Sperrin 			if (error == ECKSUM)
1148b87f3af3Sperrin 				error = EIO;
1149fa9e4066Sahrens 			break;
1150b87f3af3Sperrin 		}
1151fa9e4066Sahrens 		off += size;
1152fa9e4066Sahrens 		addr += size;
1153fa9e4066Sahrens 		resid -= size;
1154fa9e4066Sahrens 	}
1155c2e6a7d6Sperrin 	zfs_range_unlock(rl);
1156fa9e4066Sahrens 
1157fa9e4066Sahrens 	if ((bp->b_resid = resid) == bp->b_bcount)
1158fa9e4066Sahrens 		bioerror(bp, off > volsize ? EINVAL : error);
1159fa9e4066Sahrens 
116088b7b0f2SMatthew Ahrens 	if (!(bp->b_flags & B_ASYNC) && !doread && !zil_disable && !is_dump)
1161feb08c6bSbillm 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1162feb08c6bSbillm 	biodone(bp);
116322ac5be4Sperrin 
1164fa9e4066Sahrens 	return (0);
1165fa9e4066Sahrens }
1166fa9e4066Sahrens 
116767bd71c6Sperrin /*
116867bd71c6Sperrin  * Set the buffer count to the zvol maximum transfer.
116967bd71c6Sperrin  * Using our own routine instead of the default minphys()
117067bd71c6Sperrin  * means that for larger writes we write bigger buffers on X86
117167bd71c6Sperrin  * (128K instead of 56K) and flush the disk write cache less often
117267bd71c6Sperrin  * (every zvol_maxphys - currently 1MB) instead of minphys (currently
117367bd71c6Sperrin  * 56K on X86 and 128K on sparc).
117467bd71c6Sperrin  */
117567bd71c6Sperrin void
117667bd71c6Sperrin zvol_minphys(struct buf *bp)
117767bd71c6Sperrin {
117867bd71c6Sperrin 	if (bp->b_bcount > zvol_maxphys)
117967bd71c6Sperrin 		bp->b_bcount = zvol_maxphys;
118067bd71c6Sperrin }
118167bd71c6Sperrin 
1182e7cbe64fSgw int
1183e7cbe64fSgw zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks)
1184e7cbe64fSgw {
1185e7cbe64fSgw 	minor_t minor = getminor(dev);
1186e7cbe64fSgw 	zvol_state_t *zv;
1187e7cbe64fSgw 	int error = 0;
1188e7cbe64fSgw 	uint64_t size;
1189e7cbe64fSgw 	uint64_t boff;
1190e7cbe64fSgw 	uint64_t resid;
1191e7cbe64fSgw 
1192e7cbe64fSgw 	if (minor == 0)			/* This is the control device */
1193e7cbe64fSgw 		return (ENXIO);
1194e7cbe64fSgw 
1195e7cbe64fSgw 	zv = ddi_get_soft_state(zvol_state, minor);
1196e7cbe64fSgw 	if (zv == NULL)
1197e7cbe64fSgw 		return (ENXIO);
1198e7cbe64fSgw 
1199e7cbe64fSgw 	boff = ldbtob(blkno);
1200e7cbe64fSgw 	resid = ldbtob(nblocks);
120188b7b0f2SMatthew Ahrens 
120288b7b0f2SMatthew Ahrens 	VERIFY3U(boff + resid, <=, zv->zv_volsize);
120388b7b0f2SMatthew Ahrens 
1204e7cbe64fSgw 	while (resid) {
1205e7cbe64fSgw 		size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff);
120688b7b0f2SMatthew Ahrens 		error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE);
1207e7cbe64fSgw 		if (error)
1208e7cbe64fSgw 			break;
1209e7cbe64fSgw 		boff += size;
1210e7cbe64fSgw 		addr += size;
1211e7cbe64fSgw 		resid -= size;
1212e7cbe64fSgw 	}
1213e7cbe64fSgw 
1214e7cbe64fSgw 	return (error);
1215e7cbe64fSgw }
1216e7cbe64fSgw 
1217fa9e4066Sahrens /*ARGSUSED*/
1218fa9e4066Sahrens int
1219feb08c6bSbillm zvol_read(dev_t dev, uio_t *uio, cred_t *cr)
1220fa9e4066Sahrens {
1221c7ca1008Sgw 	minor_t minor = getminor(dev);
1222c7ca1008Sgw 	zvol_state_t *zv;
122373ec3d9cSgw 	uint64_t volsize;
1224c2e6a7d6Sperrin 	rl_t *rl;
1225feb08c6bSbillm 	int error = 0;
1226fa9e4066Sahrens 
1227c7ca1008Sgw 	if (minor == 0)			/* This is the control device */
1228c7ca1008Sgw 		return (ENXIO);
1229c7ca1008Sgw 
1230c7ca1008Sgw 	zv = ddi_get_soft_state(zvol_state, minor);
1231c7ca1008Sgw 	if (zv == NULL)
1232c7ca1008Sgw 		return (ENXIO);
1233c7ca1008Sgw 
123473ec3d9cSgw 	volsize = zv->zv_volsize;
123573ec3d9cSgw 	if (uio->uio_resid > 0 &&
123673ec3d9cSgw 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
123773ec3d9cSgw 		return (EIO);
123873ec3d9cSgw 
123988b7b0f2SMatthew Ahrens 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
124088b7b0f2SMatthew Ahrens 		error = physio(zvol_strategy, NULL, dev, B_READ,
124188b7b0f2SMatthew Ahrens 		    zvol_minphys, uio);
124288b7b0f2SMatthew Ahrens 		return (error);
124388b7b0f2SMatthew Ahrens 	}
124488b7b0f2SMatthew Ahrens 
1245c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1246c2e6a7d6Sperrin 	    RL_READER);
124773ec3d9cSgw 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1248feb08c6bSbillm 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1249fa9e4066Sahrens 
125073ec3d9cSgw 		/* don't read past the end */
125173ec3d9cSgw 		if (bytes > volsize - uio->uio_loffset)
125273ec3d9cSgw 			bytes = volsize - uio->uio_loffset;
125373ec3d9cSgw 
1254feb08c6bSbillm 		error =  dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes);
1255b87f3af3Sperrin 		if (error) {
1256b87f3af3Sperrin 			/* convert checksum errors into IO errors */
1257b87f3af3Sperrin 			if (error == ECKSUM)
1258b87f3af3Sperrin 				error = EIO;
1259feb08c6bSbillm 			break;
1260b87f3af3Sperrin 		}
1261feb08c6bSbillm 	}
1262c2e6a7d6Sperrin 	zfs_range_unlock(rl);
1263feb08c6bSbillm 	return (error);
1264fa9e4066Sahrens }
1265fa9e4066Sahrens 
1266fa9e4066Sahrens /*ARGSUSED*/
1267fa9e4066Sahrens int
1268feb08c6bSbillm zvol_write(dev_t dev, uio_t *uio, cred_t *cr)
1269fa9e4066Sahrens {
1270c7ca1008Sgw 	minor_t minor = getminor(dev);
1271c7ca1008Sgw 	zvol_state_t *zv;
127273ec3d9cSgw 	uint64_t volsize;
1273c2e6a7d6Sperrin 	rl_t *rl;
1274feb08c6bSbillm 	int error = 0;
1275feb08c6bSbillm 
1276c7ca1008Sgw 	if (minor == 0)			/* This is the control device */
1277c7ca1008Sgw 		return (ENXIO);
1278c7ca1008Sgw 
1279c7ca1008Sgw 	zv = ddi_get_soft_state(zvol_state, minor);
1280c7ca1008Sgw 	if (zv == NULL)
1281c7ca1008Sgw 		return (ENXIO);
1282c7ca1008Sgw 
128373ec3d9cSgw 	volsize = zv->zv_volsize;
128473ec3d9cSgw 	if (uio->uio_resid > 0 &&
128573ec3d9cSgw 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
128673ec3d9cSgw 		return (EIO);
128773ec3d9cSgw 
1288e7cbe64fSgw 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
1289e7cbe64fSgw 		error = physio(zvol_strategy, NULL, dev, B_WRITE,
1290e7cbe64fSgw 		    zvol_minphys, uio);
1291e7cbe64fSgw 		return (error);
1292e7cbe64fSgw 	}
1293e7cbe64fSgw 
1294c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1295c2e6a7d6Sperrin 	    RL_WRITER);
129673ec3d9cSgw 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1297feb08c6bSbillm 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1298feb08c6bSbillm 		uint64_t off = uio->uio_loffset;
1299feb08c6bSbillm 		dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
130073ec3d9cSgw 
130173ec3d9cSgw 		if (bytes > volsize - off)	/* don't write past the end */
130273ec3d9cSgw 			bytes = volsize - off;
130373ec3d9cSgw 
1304feb08c6bSbillm 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
1305feb08c6bSbillm 		error = dmu_tx_assign(tx, TXG_WAIT);
1306feb08c6bSbillm 		if (error) {
1307feb08c6bSbillm 			dmu_tx_abort(tx);
1308feb08c6bSbillm 			break;
1309feb08c6bSbillm 		}
1310feb08c6bSbillm 		error = dmu_write_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes, tx);
1311feb08c6bSbillm 		if (error == 0)
1312feb08c6bSbillm 			zvol_log_write(zv, tx, off, bytes);
1313feb08c6bSbillm 		dmu_tx_commit(tx);
1314feb08c6bSbillm 
1315feb08c6bSbillm 		if (error)
1316feb08c6bSbillm 			break;
1317feb08c6bSbillm 	}
1318c2e6a7d6Sperrin 	zfs_range_unlock(rl);
1319feb08c6bSbillm 	return (error);
1320fa9e4066Sahrens }
1321fa9e4066Sahrens 
1322c7f714e2SEric Taylor int
1323c7f714e2SEric Taylor zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs)
1324c7f714e2SEric Taylor {
1325c7f714e2SEric Taylor 	struct uuid uuid = EFI_RESERVED;
1326c7f714e2SEric Taylor 	efi_gpe_t gpe = { 0 };
1327c7f714e2SEric Taylor 	uint32_t crc;
1328c7f714e2SEric Taylor 	dk_efi_t efi;
1329c7f714e2SEric Taylor 	int length;
1330c7f714e2SEric Taylor 	char *ptr;
1331c7f714e2SEric Taylor 
1332c7f714e2SEric Taylor 	if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag))
1333c7f714e2SEric Taylor 		return (EFAULT);
1334c7f714e2SEric Taylor 	ptr = (char *)(uintptr_t)efi.dki_data_64;
1335c7f714e2SEric Taylor 	length = efi.dki_length;
1336c7f714e2SEric Taylor 	/*
1337c7f714e2SEric Taylor 	 * Some clients may attempt to request a PMBR for the
1338c7f714e2SEric Taylor 	 * zvol.  Currently this interface will return EINVAL to
1339c7f714e2SEric Taylor 	 * such requests.  These requests could be supported by
1340c7f714e2SEric Taylor 	 * adding a check for lba == 0 and consing up an appropriate
1341c7f714e2SEric Taylor 	 * PMBR.
1342c7f714e2SEric Taylor 	 */
1343c7f714e2SEric Taylor 	if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0)
1344c7f714e2SEric Taylor 		return (EINVAL);
1345c7f714e2SEric Taylor 
1346c7f714e2SEric Taylor 	gpe.efi_gpe_StartingLBA = LE_64(34ULL);
1347c7f714e2SEric Taylor 	gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1);
1348c7f714e2SEric Taylor 	UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
1349c7f714e2SEric Taylor 
1350c7f714e2SEric Taylor 	if (efi.dki_lba == 1) {
1351c7f714e2SEric Taylor 		efi_gpt_t gpt = { 0 };
1352c7f714e2SEric Taylor 
1353c7f714e2SEric Taylor 		gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
1354c7f714e2SEric Taylor 		gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
1355c7f714e2SEric Taylor 		gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt));
1356c7f714e2SEric Taylor 		gpt.efi_gpt_MyLBA = LE_64(1ULL);
1357c7f714e2SEric Taylor 		gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL);
1358c7f714e2SEric Taylor 		gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1);
1359c7f714e2SEric Taylor 		gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL);
1360c7f714e2SEric Taylor 		gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
1361c7f714e2SEric Taylor 		gpt.efi_gpt_SizeOfPartitionEntry =
1362c7f714e2SEric Taylor 		    LE_32(sizeof (efi_gpe_t));
1363c7f714e2SEric Taylor 		CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
1364c7f714e2SEric Taylor 		gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
1365c7f714e2SEric Taylor 		CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table);
1366c7f714e2SEric Taylor 		gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
1367c7f714e2SEric Taylor 		if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length),
1368c7f714e2SEric Taylor 		    flag))
1369c7f714e2SEric Taylor 			return (EFAULT);
1370c7f714e2SEric Taylor 		ptr += sizeof (gpt);
1371c7f714e2SEric Taylor 		length -= sizeof (gpt);
1372c7f714e2SEric Taylor 	}
1373c7f714e2SEric Taylor 	if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe),
1374c7f714e2SEric Taylor 	    length), flag))
1375c7f714e2SEric Taylor 		return (EFAULT);
1376c7f714e2SEric Taylor 	return (0);
1377c7f714e2SEric Taylor }
1378c7f714e2SEric Taylor 
1379fa9e4066Sahrens /*
1380fa9e4066Sahrens  * Dirtbag ioctls to support mkfs(1M) for UFS filesystems.  See dkio(7I).
1381fa9e4066Sahrens  */
1382fa9e4066Sahrens /*ARGSUSED*/
1383fa9e4066Sahrens int
1384fa9e4066Sahrens zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
1385fa9e4066Sahrens {
1386fa9e4066Sahrens 	zvol_state_t *zv;
1387af2c4821Smaybee 	struct dk_cinfo dki;
1388fa9e4066Sahrens 	struct dk_minfo dkm;
1389af2c4821Smaybee 	struct dk_callback *dkc;
1390fa9e4066Sahrens 	int error = 0;
1391e7cbe64fSgw 	rl_t *rl;
1392fa9e4066Sahrens 
1393fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
1394fa9e4066Sahrens 
1395fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, getminor(dev));
1396fa9e4066Sahrens 
1397fa9e4066Sahrens 	if (zv == NULL) {
1398fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
1399fa9e4066Sahrens 		return (ENXIO);
1400fa9e4066Sahrens 	}
1401fa9e4066Sahrens 
1402fa9e4066Sahrens 	switch (cmd) {
1403fa9e4066Sahrens 
1404fa9e4066Sahrens 	case DKIOCINFO:
1405af2c4821Smaybee 		bzero(&dki, sizeof (dki));
1406af2c4821Smaybee 		(void) strcpy(dki.dki_cname, "zvol");
1407af2c4821Smaybee 		(void) strcpy(dki.dki_dname, "zvol");
1408af2c4821Smaybee 		dki.dki_ctype = DKC_UNKNOWN;
1409af2c4821Smaybee 		dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs);
1410fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
1411af2c4821Smaybee 		if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag))
1412fa9e4066Sahrens 			error = EFAULT;
1413fa9e4066Sahrens 		return (error);
1414fa9e4066Sahrens 
1415fa9e4066Sahrens 	case DKIOCGMEDIAINFO:
1416fa9e4066Sahrens 		bzero(&dkm, sizeof (dkm));
1417fa9e4066Sahrens 		dkm.dki_lbsize = 1U << zv->zv_min_bs;
1418fa9e4066Sahrens 		dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
1419fa9e4066Sahrens 		dkm.dki_media_type = DK_UNKNOWN;
1420fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
1421fa9e4066Sahrens 		if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
1422fa9e4066Sahrens 			error = EFAULT;
1423fa9e4066Sahrens 		return (error);
1424fa9e4066Sahrens 
1425fa9e4066Sahrens 	case DKIOCGETEFI:
1426c7f714e2SEric Taylor 		{
1427c7f714e2SEric Taylor 			uint64_t vs = zv->zv_volsize;
1428c7f714e2SEric Taylor 			uint8_t bs = zv->zv_min_bs;
1429fa9e4066Sahrens 
143068a5ac4dSmaybee 			mutex_exit(&zvol_state_lock);
1431c7f714e2SEric Taylor 			error = zvol_getefi((void *)arg, flag, vs, bs);
1432c7f714e2SEric Taylor 			return (error);
143368a5ac4dSmaybee 		}
1434fa9e4066Sahrens 
1435feb08c6bSbillm 	case DKIOCFLUSHWRITECACHE:
1436af2c4821Smaybee 		dkc = (struct dk_callback *)arg;
1437feb08c6bSbillm 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1438af2c4821Smaybee 		if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) {
1439af2c4821Smaybee 			(*dkc->dkc_callback)(dkc->dkc_cookie, error);
1440af2c4821Smaybee 			error = 0;
1441af2c4821Smaybee 		}
1442feb08c6bSbillm 		break;
1443feb08c6bSbillm 
1444b6130eadSmaybee 	case DKIOCGGEOM:
1445b6130eadSmaybee 	case DKIOCGVTOC:
1446e7cbe64fSgw 		/*
1447e7cbe64fSgw 		 * commands using these (like prtvtoc) expect ENOTSUP
1448e7cbe64fSgw 		 * since we're emulating an EFI label
1449e7cbe64fSgw 		 */
1450b6130eadSmaybee 		error = ENOTSUP;
1451b6130eadSmaybee 		break;
1452b6130eadSmaybee 
1453e7cbe64fSgw 	case DKIOCDUMPINIT:
1454e7cbe64fSgw 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1455e7cbe64fSgw 		    RL_WRITER);
1456e7cbe64fSgw 		error = zvol_dumpify(zv);
1457e7cbe64fSgw 		zfs_range_unlock(rl);
1458e7cbe64fSgw 		break;
1459e7cbe64fSgw 
1460e7cbe64fSgw 	case DKIOCDUMPFINI:
1461e7cbe64fSgw 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1462e7cbe64fSgw 		    RL_WRITER);
1463e7cbe64fSgw 		error = zvol_dump_fini(zv);
1464e7cbe64fSgw 		zfs_range_unlock(rl);
1465e7cbe64fSgw 		break;
1466e7cbe64fSgw 
1467fa9e4066Sahrens 	default:
146868a5ac4dSmaybee 		error = ENOTTY;
1469fa9e4066Sahrens 		break;
1470fa9e4066Sahrens 
1471fa9e4066Sahrens 	}
1472fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
1473fa9e4066Sahrens 	return (error);
1474fa9e4066Sahrens }
1475fa9e4066Sahrens 
1476fa9e4066Sahrens int
1477fa9e4066Sahrens zvol_busy(void)
1478fa9e4066Sahrens {
1479fa9e4066Sahrens 	return (zvol_minors != 0);
1480fa9e4066Sahrens }
1481fa9e4066Sahrens 
1482fa9e4066Sahrens void
1483fa9e4066Sahrens zvol_init(void)
1484fa9e4066Sahrens {
1485fa9e4066Sahrens 	VERIFY(ddi_soft_state_init(&zvol_state, sizeof (zvol_state_t), 1) == 0);
1486fa9e4066Sahrens 	mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
1487fa9e4066Sahrens }
1488fa9e4066Sahrens 
1489fa9e4066Sahrens void
1490fa9e4066Sahrens zvol_fini(void)
1491fa9e4066Sahrens {
1492fa9e4066Sahrens 	mutex_destroy(&zvol_state_lock);
1493fa9e4066Sahrens 	ddi_soft_state_fini(&zvol_state);
1494fa9e4066Sahrens }
1495e7cbe64fSgw 
1496e7cbe64fSgw static boolean_t
1497e7cbe64fSgw zvol_is_swap(zvol_state_t *zv)
1498e7cbe64fSgw {
1499e7cbe64fSgw 	vnode_t *vp;
1500e7cbe64fSgw 	boolean_t ret = B_FALSE;
1501e7cbe64fSgw 	char *devpath;
1502e7cbe64fSgw 	size_t devpathlen;
1503e7cbe64fSgw 	int error;
1504e7cbe64fSgw 
1505e7cbe64fSgw 	devpathlen = strlen(ZVOL_FULL_DEV_DIR) + strlen(zv->zv_name) + 1;
1506e7cbe64fSgw 	devpath = kmem_alloc(devpathlen, KM_SLEEP);
1507e7cbe64fSgw 	(void) sprintf(devpath, "%s%s", ZVOL_FULL_DEV_DIR, zv->zv_name);
1508e7cbe64fSgw 	error = lookupname(devpath, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp);
1509e7cbe64fSgw 	kmem_free(devpath, devpathlen);
1510e7cbe64fSgw 
1511e7cbe64fSgw 	ret = !error && IS_SWAPVP(common_specvp(vp));
1512e7cbe64fSgw 
1513e7cbe64fSgw 	if (vp != NULL)
1514e7cbe64fSgw 		VN_RELE(vp);
1515e7cbe64fSgw 
1516e7cbe64fSgw 	return (ret);
1517e7cbe64fSgw }
1518e7cbe64fSgw 
1519e7cbe64fSgw static int
1520e7cbe64fSgw zvol_dump_init(zvol_state_t *zv, boolean_t resize)
1521e7cbe64fSgw {
1522e7cbe64fSgw 	dmu_tx_t *tx;
1523e7cbe64fSgw 	int error = 0;
1524e7cbe64fSgw 	objset_t *os = zv->zv_objset;
1525e7cbe64fSgw 	nvlist_t *nv = NULL;
1526e7cbe64fSgw 
1527e7cbe64fSgw 	ASSERT(MUTEX_HELD(&zvol_state_lock));
1528e7cbe64fSgw 
1529e7cbe64fSgw 	tx = dmu_tx_create(os);
1530e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1531e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
1532e7cbe64fSgw 	if (error) {
1533e7cbe64fSgw 		dmu_tx_abort(tx);
1534e7cbe64fSgw 		return (error);
1535e7cbe64fSgw 	}
1536e7cbe64fSgw 
1537e7cbe64fSgw 	/*
1538e7cbe64fSgw 	 * If we are resizing the dump device then we only need to
1539e7cbe64fSgw 	 * update the refreservation to match the newly updated
1540e7cbe64fSgw 	 * zvolsize. Otherwise, we save off the original state of the
1541e7cbe64fSgw 	 * zvol so that we can restore them if the zvol is ever undumpified.
1542e7cbe64fSgw 	 */
1543e7cbe64fSgw 	if (resize) {
1544e7cbe64fSgw 		error = zap_update(os, ZVOL_ZAP_OBJ,
1545e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1546e7cbe64fSgw 		    &zv->zv_volsize, tx);
1547e7cbe64fSgw 	} else {
154888b7b0f2SMatthew Ahrens 		uint64_t checksum, compress, refresrv, vbs;
154988b7b0f2SMatthew Ahrens 
1550e7cbe64fSgw 		error = dsl_prop_get_integer(zv->zv_name,
1551e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL);
1552e7cbe64fSgw 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
1553e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL);
1554e7cbe64fSgw 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
1555e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL);
155688b7b0f2SMatthew Ahrens 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
155788b7b0f2SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL);
1558e7cbe64fSgw 
1559e7cbe64fSgw 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1560e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1,
1561e7cbe64fSgw 		    &compress, tx);
1562e7cbe64fSgw 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1563e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx);
1564e7cbe64fSgw 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1565e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1566e7cbe64fSgw 		    &refresrv, tx);
156788b7b0f2SMatthew Ahrens 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
156888b7b0f2SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1,
156988b7b0f2SMatthew Ahrens 		    &vbs, tx);
1570e7cbe64fSgw 	}
1571e7cbe64fSgw 	dmu_tx_commit(tx);
1572e7cbe64fSgw 
1573e7cbe64fSgw 	/* Truncate the file */
1574e7cbe64fSgw 	if (!error)
1575cdb0ab79Smaybee 		error = dmu_free_long_range(zv->zv_objset,
1576cdb0ab79Smaybee 		    ZVOL_OBJ, 0, DMU_OBJECT_END);
1577e7cbe64fSgw 
1578e7cbe64fSgw 	if (error)
1579e7cbe64fSgw 		return (error);
1580e7cbe64fSgw 
1581e7cbe64fSgw 	/*
1582e7cbe64fSgw 	 * We only need update the zvol's property if we are initializing
1583e7cbe64fSgw 	 * the dump area for the first time.
1584e7cbe64fSgw 	 */
1585e7cbe64fSgw 	if (!resize) {
1586e7cbe64fSgw 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1587e7cbe64fSgw 		VERIFY(nvlist_add_uint64(nv,
1588e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0);
1589e7cbe64fSgw 		VERIFY(nvlist_add_uint64(nv,
1590e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
1591e7cbe64fSgw 		    ZIO_COMPRESS_OFF) == 0);
1592e7cbe64fSgw 		VERIFY(nvlist_add_uint64(nv,
1593e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
1594e7cbe64fSgw 		    ZIO_CHECKSUM_OFF) == 0);
159588b7b0f2SMatthew Ahrens 		VERIFY(nvlist_add_uint64(nv,
159688b7b0f2SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
159788b7b0f2SMatthew Ahrens 		    SPA_MAXBLOCKSIZE) == 0);
1598e7cbe64fSgw 
1599e7cbe64fSgw 		error = zfs_set_prop_nvlist(zv->zv_name, nv);
1600e7cbe64fSgw 		nvlist_free(nv);
1601e7cbe64fSgw 
1602e7cbe64fSgw 		if (error)
1603e7cbe64fSgw 			return (error);
1604e7cbe64fSgw 	}
1605e7cbe64fSgw 
1606e7cbe64fSgw 	/* Allocate the space for the dump */
1607e7cbe64fSgw 	error = zvol_prealloc(zv);
1608e7cbe64fSgw 	return (error);
1609e7cbe64fSgw }
1610e7cbe64fSgw 
1611e7cbe64fSgw static int
1612e7cbe64fSgw zvol_dumpify(zvol_state_t *zv)
1613e7cbe64fSgw {
1614e7cbe64fSgw 	int error = 0;
1615e7cbe64fSgw 	uint64_t dumpsize = 0;
1616e7cbe64fSgw 	dmu_tx_t *tx;
1617e7cbe64fSgw 	objset_t *os = zv->zv_objset;
1618e7cbe64fSgw 
1619e7cbe64fSgw 	if (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY))
1620e7cbe64fSgw 		return (EROFS);
1621e7cbe64fSgw 
1622e7cbe64fSgw 	/*
1623e7cbe64fSgw 	 * We do not support swap devices acting as dump devices.
1624e7cbe64fSgw 	 */
1625e7cbe64fSgw 	if (zvol_is_swap(zv))
1626e7cbe64fSgw 		return (ENOTSUP);
1627e7cbe64fSgw 
1628e7cbe64fSgw 	if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE,
1629e7cbe64fSgw 	    8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) {
1630e7cbe64fSgw 		boolean_t resize = (dumpsize > 0) ? B_TRUE : B_FALSE;
1631e7cbe64fSgw 
1632e7cbe64fSgw 		if ((error = zvol_dump_init(zv, resize)) != 0) {
1633e7cbe64fSgw 			(void) zvol_dump_fini(zv);
1634e7cbe64fSgw 			return (error);
1635e7cbe64fSgw 		}
1636e7cbe64fSgw 	}
1637e7cbe64fSgw 
1638e7cbe64fSgw 	/*
1639e7cbe64fSgw 	 * Build up our lba mapping.
1640e7cbe64fSgw 	 */
1641e7cbe64fSgw 	error = zvol_get_lbas(zv);
1642e7cbe64fSgw 	if (error) {
1643e7cbe64fSgw 		(void) zvol_dump_fini(zv);
1644e7cbe64fSgw 		return (error);
1645e7cbe64fSgw 	}
1646e7cbe64fSgw 
1647e7cbe64fSgw 	tx = dmu_tx_create(os);
1648e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1649e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
1650e7cbe64fSgw 	if (error) {
1651e7cbe64fSgw 		dmu_tx_abort(tx);
1652e7cbe64fSgw 		(void) zvol_dump_fini(zv);
1653e7cbe64fSgw 		return (error);
1654e7cbe64fSgw 	}
1655e7cbe64fSgw 
1656e7cbe64fSgw 	zv->zv_flags |= ZVOL_DUMPIFIED;
1657e7cbe64fSgw 	error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1,
1658e7cbe64fSgw 	    &zv->zv_volsize, tx);
1659e7cbe64fSgw 	dmu_tx_commit(tx);
1660e7cbe64fSgw 
1661e7cbe64fSgw 	if (error) {
1662e7cbe64fSgw 		(void) zvol_dump_fini(zv);
1663e7cbe64fSgw 		return (error);
1664e7cbe64fSgw 	}
1665e7cbe64fSgw 
1666e7cbe64fSgw 	txg_wait_synced(dmu_objset_pool(os), 0);
1667e7cbe64fSgw 	return (0);
1668e7cbe64fSgw }
1669e7cbe64fSgw 
1670e7cbe64fSgw static int
1671e7cbe64fSgw zvol_dump_fini(zvol_state_t *zv)
1672e7cbe64fSgw {
1673e7cbe64fSgw 	dmu_tx_t *tx;
1674e7cbe64fSgw 	objset_t *os = zv->zv_objset;
1675e7cbe64fSgw 	nvlist_t *nv;
1676e7cbe64fSgw 	int error = 0;
167788b7b0f2SMatthew Ahrens 	uint64_t checksum, compress, refresrv, vbs;
1678e7cbe64fSgw 
1679b7e50089Smaybee 	/*
1680b7e50089Smaybee 	 * Attempt to restore the zvol back to its pre-dumpified state.
1681b7e50089Smaybee 	 * This is a best-effort attempt as it's possible that not all
1682b7e50089Smaybee 	 * of these properties were initialized during the dumpify process
1683b7e50089Smaybee 	 * (i.e. error during zvol_dump_init).
1684b7e50089Smaybee 	 */
1685b7e50089Smaybee 
1686e7cbe64fSgw 	tx = dmu_tx_create(os);
1687e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1688e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
1689e7cbe64fSgw 	if (error) {
1690e7cbe64fSgw 		dmu_tx_abort(tx);
1691e7cbe64fSgw 		return (error);
1692e7cbe64fSgw 	}
1693b7e50089Smaybee 	(void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx);
1694b7e50089Smaybee 	dmu_tx_commit(tx);
1695e7cbe64fSgw 
1696e7cbe64fSgw 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1697e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum);
1698e7cbe64fSgw 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1699e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress);
1700e7cbe64fSgw 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1701e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv);
170288b7b0f2SMatthew Ahrens 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
170388b7b0f2SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs);
1704e7cbe64fSgw 
1705e7cbe64fSgw 	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1706e7cbe64fSgw 	(void) nvlist_add_uint64(nv,
1707e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum);
1708e7cbe64fSgw 	(void) nvlist_add_uint64(nv,
1709e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress);
1710e7cbe64fSgw 	(void) nvlist_add_uint64(nv,
1711e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv);
171288b7b0f2SMatthew Ahrens 	(void) nvlist_add_uint64(nv,
171388b7b0f2SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), vbs);
1714e7cbe64fSgw 	(void) zfs_set_prop_nvlist(zv->zv_name, nv);
1715e7cbe64fSgw 	nvlist_free(nv);
1716e7cbe64fSgw 
1717b7e50089Smaybee 	zvol_free_extents(zv);
1718b7e50089Smaybee 	zv->zv_flags &= ~ZVOL_DUMPIFIED;
1719b7e50089Smaybee 	(void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END);
1720b7e50089Smaybee 
1721e7cbe64fSgw 	return (0);
1722e7cbe64fSgw }
1723