xref: /illumos-gate/usr/src/uts/common/fs/zfs/zvol.c (revision eb633035)
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 /*
22f80ce222SChris Kirby  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23b77b9231SDan McDonald  *
24b77b9231SDan McDonald  * Portions Copyright 2010 Robert Milkowski
25b77b9231SDan McDonald  *
26047c81d3SSaso Kiselkov  * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
27b7edcb94SMatthew Ahrens  * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
28c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
29455e370cSJohn Levon  * Copyright 2019 Joyent, Inc.
30fa9e4066Sahrens  */
31fa9e4066Sahrens 
32fa9e4066Sahrens /*
33fa9e4066Sahrens  * ZFS volume emulation driver.
34fa9e4066Sahrens  *
35fa9e4066Sahrens  * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes.
36fa9e4066Sahrens  * Volumes are accessed through the symbolic links named:
37fa9e4066Sahrens  *
38fa9e4066Sahrens  * /dev/zvol/dsk/<pool_name>/<dataset_name>
39fa9e4066Sahrens  * /dev/zvol/rdsk/<pool_name>/<dataset_name>
40fa9e4066Sahrens  *
41681d9761SEric Taylor  * These links are created by the /dev filesystem (sdev_zvolops.c).
42fa9e4066Sahrens  * Volumes are persistent through reboot.  No user command needs to be
43fa9e4066Sahrens  * run before opening and using a device.
44fa9e4066Sahrens  */
45fa9e4066Sahrens 
46fa9e4066Sahrens #include <sys/types.h>
47fa9e4066Sahrens #include <sys/param.h>
48fa9e4066Sahrens #include <sys/errno.h>
49fa9e4066Sahrens #include <sys/uio.h>
50fa9e4066Sahrens #include <sys/buf.h>
51fa9e4066Sahrens #include <sys/modctl.h>
52fa9e4066Sahrens #include <sys/open.h>
53fa9e4066Sahrens #include <sys/kmem.h>
54fa9e4066Sahrens #include <sys/conf.h>
55fa9e4066Sahrens #include <sys/cmn_err.h>
56fa9e4066Sahrens #include <sys/stat.h>
57fa9e4066Sahrens #include <sys/zap.h>
58fa9e4066Sahrens #include <sys/spa.h>
59810e43b2SBill Pijewski #include <sys/spa_impl.h>
60fa9e4066Sahrens #include <sys/zio.h>
61e7cbe64fSgw #include <sys/dmu_traverse.h>
62e7cbe64fSgw #include <sys/dnode.h>
63e7cbe64fSgw #include <sys/dsl_dataset.h>
64fa9e4066Sahrens #include <sys/dsl_prop.h>
65fa9e4066Sahrens #include <sys/dkio.h>
66fa9e4066Sahrens #include <sys/efi_partition.h>
67fa9e4066Sahrens #include <sys/byteorder.h>
68fa9e4066Sahrens #include <sys/pathname.h>
69fa9e4066Sahrens #include <sys/ddi.h>
70fa9e4066Sahrens #include <sys/sunddi.h>
71fa9e4066Sahrens #include <sys/crc32.h>
72fa9e4066Sahrens #include <sys/dirent.h>
73fa9e4066Sahrens #include <sys/policy.h>
74fa9e4066Sahrens #include <sys/fs/zfs.h>
75fa9e4066Sahrens #include <sys/zfs_ioctl.h>
76fa9e4066Sahrens #include <sys/mkdev.h>
7722ac5be4Sperrin #include <sys/zil.h>
78c5c6ffa0Smaybee #include <sys/refcount.h>
79c2e6a7d6Sperrin #include <sys/zfs_znode.h>
80c2e6a7d6Sperrin #include <sys/zfs_rlock.h>
81e7cbe64fSgw #include <sys/vdev_disk.h>
82e7cbe64fSgw #include <sys/vdev_impl.h>
83810e43b2SBill Pijewski #include <sys/vdev_raidz.h>
84e7cbe64fSgw #include <sys/zvol.h>
85e7cbe64fSgw #include <sys/dumphdr.h>
861209a471SNeil Perrin #include <sys/zil_impl.h>
8780901aeaSGeorge Wilson #include <sys/dbuf.h>
88810e43b2SBill Pijewski #include <sys/dmu_tx.h>
89810e43b2SBill Pijewski #include <sys/zfeature.h>
90810e43b2SBill Pijewski #include <sys/zio_checksum.h>
911271e4b1SPrakash Surya #include <sys/zil_impl.h>
92c3377ee9SJohn Levon #include <sys/smt.h>
93047c81d3SSaso Kiselkov #include <sys/dkioc_free_util.h>
9479315247SMatthew Ahrens #include <sys/zfs_rlock.h>
95fa9e4066Sahrens 
96fa9e4066Sahrens #include "zfs_namecheck.h"
97fa9e4066Sahrens 
98c99e4bdcSChris Kirby void *zfsdev_state;
99503ad85cSMatthew Ahrens static char *zvol_tag = "zvol_tag";
100fa9e4066Sahrens 
101e7cbe64fSgw #define	ZVOL_DUMPSIZE		"dumpsize"
102e7cbe64fSgw 
103fa9e4066Sahrens /*
104c99e4bdcSChris Kirby  * This lock protects the zfsdev_state structure from being modified
105fa9e4066Sahrens  * while it's being used, e.g. an open that comes in before a create
106fa9e4066Sahrens  * finishes.  It also protects temporary opens of the dataset so that,
107fa9e4066Sahrens  * e.g., an open doesn't get a spurious EBUSY.
108fa9e4066Sahrens  */
109c99e4bdcSChris Kirby kmutex_t zfsdev_state_lock;
110fa9e4066Sahrens static uint32_t zvol_minors;
111fa9e4066Sahrens 
112e7cbe64fSgw typedef struct zvol_extent {
11388b7b0f2SMatthew Ahrens 	list_node_t	ze_node;
114e7cbe64fSgw 	dva_t		ze_dva;		/* dva associated with this extent */
11588b7b0f2SMatthew Ahrens 	uint64_t	ze_nblks;	/* number of blocks in extent */
116e7cbe64fSgw } zvol_extent_t;
117e7cbe64fSgw 
118fa9e4066Sahrens /*
119fa9e4066Sahrens  * The in-core state of each volume.
120fa9e4066Sahrens  */
121fa9e4066Sahrens typedef struct zvol_state {
122fa9e4066Sahrens 	char		zv_name[MAXPATHLEN]; /* pool/dd name */
123fa9e4066Sahrens 	uint64_t	zv_volsize;	/* amount of space we advertise */
12467bd71c6Sperrin 	uint64_t	zv_volblocksize; /* volume block size */
125fa9e4066Sahrens 	minor_t		zv_minor;	/* minor number */
126fa9e4066Sahrens 	uint8_t		zv_min_bs;	/* minimum addressable block shift */
127701f66c4SEric Taylor 	uint8_t		zv_flags;	/* readonly, dumpified, etc. */
128fa9e4066Sahrens 	objset_t	*zv_objset;	/* objset handle */
129fa9e4066Sahrens 	uint32_t	zv_open_count[OTYPCNT];	/* open counts */
130fa9e4066Sahrens 	uint32_t	zv_total_opens;	/* total open count */
13122ac5be4Sperrin 	zilog_t		*zv_zilog;	/* ZIL handle */
13288b7b0f2SMatthew Ahrens 	list_t		zv_extents;	/* List of extents for dump */
13379315247SMatthew Ahrens 	rangelock_t	zv_rangelock;
1348dfe5547SRichard Yao 	dnode_t		*zv_dn;		/* dnode hold */
135fa9e4066Sahrens } zvol_state_t;
136fa9e4066Sahrens 
137e7cbe64fSgw /*
138e7cbe64fSgw  * zvol specific flags
139e7cbe64fSgw  */
140e7cbe64fSgw #define	ZVOL_RDONLY	0x1
141e7cbe64fSgw #define	ZVOL_DUMPIFIED	0x2
142c7f714e2SEric Taylor #define	ZVOL_EXCL	0x4
143701f66c4SEric Taylor #define	ZVOL_WCE	0x8
144e7cbe64fSgw 
14567bd71c6Sperrin /*
14667bd71c6Sperrin  * zvol maximum transfer in one DMU tx.
14767bd71c6Sperrin  */
14867bd71c6Sperrin int zvol_maxphys = DMU_MAX_ACCESS/2;
14967bd71c6Sperrin 
150893c83baSGeorge Wilson /*
151893c83baSGeorge Wilson  * Toggle unmap functionality.
152893c83baSGeorge Wilson  */
153893c83baSGeorge Wilson boolean_t zvol_unmap_enabled = B_TRUE;
154893c83baSGeorge Wilson 
1551c9272b8SStephen Blinick /*
1561c9272b8SStephen Blinick  * If true, unmaps requested as synchronous are executed synchronously,
1571c9272b8SStephen Blinick  * otherwise all unmaps are asynchronous.
1581c9272b8SStephen Blinick  */
1591c9272b8SStephen Blinick boolean_t zvol_unmap_sync_enabled = B_FALSE;
1601c9272b8SStephen Blinick 
16192241e0bSTom Erickson extern int zfs_set_prop_nvlist(const char *, zprop_source_t,
1624445fffbSMatthew Ahrens     nvlist_t *, nvlist_t *);
163681d9761SEric Taylor static int zvol_remove_zv(zvol_state_t *);
1641271e4b1SPrakash Surya static int zvol_get_data(void *arg, lr_write_t *lr, char *buf,
1651271e4b1SPrakash Surya     struct lwb *lwb, zio_t *zio);
166e7cbe64fSgw static int zvol_dumpify(zvol_state_t *zv);
167e7cbe64fSgw static int zvol_dump_fini(zvol_state_t *zv);
168e7cbe64fSgw static int zvol_dump_init(zvol_state_t *zv, boolean_t resize);
16967bd71c6Sperrin 
170fa9e4066Sahrens static void
171c61ea566SGeorge Wilson zvol_size_changed(zvol_state_t *zv, uint64_t volsize)
172fa9e4066Sahrens {
173c61ea566SGeorge Wilson 	dev_t dev = makedevice(ddi_driver_major(zfs_dip), zv->zv_minor);
174fa9e4066Sahrens 
175c61ea566SGeorge Wilson 	zv->zv_volsize = volsize;
176fa9e4066Sahrens 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
177681d9761SEric Taylor 	    "Size", volsize) == DDI_SUCCESS);
178fa9e4066Sahrens 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
179681d9761SEric Taylor 	    "Nblocks", lbtodb(volsize)) == DDI_SUCCESS);
180e7cbe64fSgw 
181e7cbe64fSgw 	/* Notify specfs to invalidate the cached size */
182e7cbe64fSgw 	spec_size_invalidate(dev, VBLK);
183e7cbe64fSgw 	spec_size_invalidate(dev, VCHR);
184fa9e4066Sahrens }
185fa9e4066Sahrens 
186fa9e4066Sahrens int
187e9dbad6fSeschrock zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
188fa9e4066Sahrens {
189e9dbad6fSeschrock 	if (volsize == 0)
190be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
191fa9e4066Sahrens 
192e9dbad6fSeschrock 	if (volsize % blocksize != 0)
193be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1945c5460e9Seschrock 
195fa9e4066Sahrens #ifdef _ILP32
196e9dbad6fSeschrock 	if (volsize - 1 > SPEC_MAXOFFSET_T)
197be6fd75aSMatthew Ahrens 		return (SET_ERROR(EOVERFLOW));
198fa9e4066Sahrens #endif
199fa9e4066Sahrens 	return (0);
200fa9e4066Sahrens }
201fa9e4066Sahrens 
202fa9e4066Sahrens int
203e9dbad6fSeschrock zvol_check_volblocksize(uint64_t volblocksize)
204fa9e4066Sahrens {
205e9dbad6fSeschrock 	if (volblocksize < SPA_MINBLOCKSIZE ||
206b5152584SMatthew Ahrens 	    volblocksize > SPA_OLD_MAXBLOCKSIZE ||
207e9dbad6fSeschrock 	    !ISP2(volblocksize))
208be6fd75aSMatthew Ahrens 		return (SET_ERROR(EDOM));
209fa9e4066Sahrens 
210fa9e4066Sahrens 	return (0);
211fa9e4066Sahrens }
212fa9e4066Sahrens 
213fa9e4066Sahrens int
214a2eea2e1Sahrens zvol_get_stats(objset_t *os, nvlist_t *nv)
215fa9e4066Sahrens {
216fa9e4066Sahrens 	int error;
217fa9e4066Sahrens 	dmu_object_info_t doi;
218a2eea2e1Sahrens 	uint64_t val;
219fa9e4066Sahrens 
220a2eea2e1Sahrens 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
221fa9e4066Sahrens 	if (error)
222fa9e4066Sahrens 		return (error);
223fa9e4066Sahrens 
224a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
225a2eea2e1Sahrens 
226fa9e4066Sahrens 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
227fa9e4066Sahrens 
228a2eea2e1Sahrens 	if (error == 0) {
229a2eea2e1Sahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
230a2eea2e1Sahrens 		    doi.doi_data_block_size);
231a2eea2e1Sahrens 	}
232fa9e4066Sahrens 
233fa9e4066Sahrens 	return (error);
234fa9e4066Sahrens }
235fa9e4066Sahrens 
236fa9e4066Sahrens static zvol_state_t *
237e9dbad6fSeschrock zvol_minor_lookup(const char *name)
238fa9e4066Sahrens {
239fa9e4066Sahrens 	minor_t minor;
240fa9e4066Sahrens 	zvol_state_t *zv;
241fa9e4066Sahrens 
242c99e4bdcSChris Kirby 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
243fa9e4066Sahrens 
244c99e4bdcSChris Kirby 	for (minor = 1; minor <= ZFSDEV_MAX_MINOR; minor++) {
245c99e4bdcSChris Kirby 		zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
246fa9e4066Sahrens 		if (zv == NULL)
247fa9e4066Sahrens 			continue;
248fa9e4066Sahrens 		if (strcmp(zv->zv_name, name) == 0)
249f80ce222SChris Kirby 			return (zv);
250fa9e4066Sahrens 	}
251fa9e4066Sahrens 
252f80ce222SChris Kirby 	return (NULL);
253fa9e4066Sahrens }
254fa9e4066Sahrens 
255e7cbe64fSgw /* extent mapping arg */
256e7cbe64fSgw struct maparg {
25788b7b0f2SMatthew Ahrens 	zvol_state_t	*ma_zv;
25888b7b0f2SMatthew Ahrens 	uint64_t	ma_blks;
259e7cbe64fSgw };
260e7cbe64fSgw 
261e7cbe64fSgw /*ARGSUSED*/
262e7cbe64fSgw static int
2631b912ec7SGeorge Wilson zvol_map_block(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
2647802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
265e7cbe64fSgw {
26688b7b0f2SMatthew Ahrens 	struct maparg *ma = arg;
26788b7b0f2SMatthew Ahrens 	zvol_extent_t *ze;
26888b7b0f2SMatthew Ahrens 	int bs = ma->ma_zv->zv_volblocksize;
269e7cbe64fSgw 
270a2cdcdd2SPaul Dagnelie 	if (bp == NULL || BP_IS_HOLE(bp) ||
27143466aaeSMax Grossman 	    zb->zb_object != ZVOL_OBJ || zb->zb_level != 0)
27288b7b0f2SMatthew Ahrens 		return (0);
273e7cbe64fSgw 
2745d7b4d43SMatthew Ahrens 	VERIFY(!BP_IS_EMBEDDED(bp));
2755d7b4d43SMatthew Ahrens 
27688b7b0f2SMatthew Ahrens 	VERIFY3U(ma->ma_blks, ==, zb->zb_blkid);
27788b7b0f2SMatthew Ahrens 	ma->ma_blks++;
278e7cbe64fSgw 
27988b7b0f2SMatthew Ahrens 	/* Abort immediately if we have encountered gang blocks */
28088b7b0f2SMatthew Ahrens 	if (BP_IS_GANG(bp))
281be6fd75aSMatthew Ahrens 		return (SET_ERROR(EFRAGS));
282e7cbe64fSgw 
28388b7b0f2SMatthew Ahrens 	/*
28488b7b0f2SMatthew Ahrens 	 * See if the block is at the end of the previous extent.
28588b7b0f2SMatthew Ahrens 	 */
28688b7b0f2SMatthew Ahrens 	ze = list_tail(&ma->ma_zv->zv_extents);
28788b7b0f2SMatthew Ahrens 	if (ze &&
28888b7b0f2SMatthew Ahrens 	    DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) &&
28988b7b0f2SMatthew Ahrens 	    DVA_GET_OFFSET(BP_IDENTITY(bp)) ==
29088b7b0f2SMatthew Ahrens 	    DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) {
29188b7b0f2SMatthew Ahrens 		ze->ze_nblks++;
29288b7b0f2SMatthew Ahrens 		return (0);
293e7cbe64fSgw 	}
294e7cbe64fSgw 
29588b7b0f2SMatthew Ahrens 	dprintf_bp(bp, "%s", "next blkptr:");
296e7cbe64fSgw 
29788b7b0f2SMatthew Ahrens 	/* start a new extent */
29888b7b0f2SMatthew Ahrens 	ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP);
29988b7b0f2SMatthew Ahrens 	ze->ze_dva = bp->blk_dva[0];	/* structure assignment */
30088b7b0f2SMatthew Ahrens 	ze->ze_nblks = 1;
30188b7b0f2SMatthew Ahrens 	list_insert_tail(&ma->ma_zv->zv_extents, ze);
30288b7b0f2SMatthew Ahrens 	return (0);
30388b7b0f2SMatthew Ahrens }
304e7cbe64fSgw 
30588b7b0f2SMatthew Ahrens static void
30688b7b0f2SMatthew Ahrens zvol_free_extents(zvol_state_t *zv)
30788b7b0f2SMatthew Ahrens {
30888b7b0f2SMatthew Ahrens 	zvol_extent_t *ze;
309e7cbe64fSgw 
31088b7b0f2SMatthew Ahrens 	while (ze = list_head(&zv->zv_extents)) {
31188b7b0f2SMatthew Ahrens 		list_remove(&zv->zv_extents, ze);
31288b7b0f2SMatthew Ahrens 		kmem_free(ze, sizeof (zvol_extent_t));
313e7cbe64fSgw 	}
31488b7b0f2SMatthew Ahrens }
315e7cbe64fSgw 
31688b7b0f2SMatthew Ahrens static int
31788b7b0f2SMatthew Ahrens zvol_get_lbas(zvol_state_t *zv)
31888b7b0f2SMatthew Ahrens {
3193adc9019SEric Taylor 	objset_t *os = zv->zv_objset;
32088b7b0f2SMatthew Ahrens 	struct maparg	ma;
32188b7b0f2SMatthew Ahrens 	int		err;
32288b7b0f2SMatthew Ahrens 
32388b7b0f2SMatthew Ahrens 	ma.ma_zv = zv;
32488b7b0f2SMatthew Ahrens 	ma.ma_blks = 0;
32588b7b0f2SMatthew Ahrens 	zvol_free_extents(zv);
32688b7b0f2SMatthew Ahrens 
3273adc9019SEric Taylor 	/* commit any in-flight changes before traversing the dataset */
3283adc9019SEric Taylor 	txg_wait_synced(dmu_objset_pool(os), 0);
3293adc9019SEric Taylor 	err = traverse_dataset(dmu_objset_ds(os), 0,
33088b7b0f2SMatthew Ahrens 	    TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma);
33188b7b0f2SMatthew Ahrens 	if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) {
33288b7b0f2SMatthew Ahrens 		zvol_free_extents(zv);
33388b7b0f2SMatthew Ahrens 		return (err ? err : EIO);
334e7cbe64fSgw 	}
33588b7b0f2SMatthew Ahrens 
336e7cbe64fSgw 	return (0);
337e7cbe64fSgw }
338e7cbe64fSgw 
339ecd6cf80Smarks /* ARGSUSED */
340fa9e4066Sahrens void
341ecd6cf80Smarks zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
342fa9e4066Sahrens {
343da6c28aaSamw 	zfs_creat_t *zct = arg;
344da6c28aaSamw 	nvlist_t *nvprops = zct->zct_props;
345fa9e4066Sahrens 	int error;
346e9dbad6fSeschrock 	uint64_t volblocksize, volsize;
347fa9e4066Sahrens 
348ecd6cf80Smarks 	VERIFY(nvlist_lookup_uint64(nvprops,
349e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
350ecd6cf80Smarks 	if (nvlist_lookup_uint64(nvprops,
351e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
352e9dbad6fSeschrock 		volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
353e9dbad6fSeschrock 
354e9dbad6fSeschrock 	/*
355e7cbe64fSgw 	 * These properties must be removed from the list so the generic
356e9dbad6fSeschrock 	 * property setting step won't apply to them.
357e9dbad6fSeschrock 	 */
358ecd6cf80Smarks 	VERIFY(nvlist_remove_all(nvprops,
359e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
360ecd6cf80Smarks 	(void) nvlist_remove_all(nvprops,
361e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
362e9dbad6fSeschrock 
363e9dbad6fSeschrock 	error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
364fa9e4066Sahrens 	    DMU_OT_NONE, 0, tx);
365fa9e4066Sahrens 	ASSERT(error == 0);
366fa9e4066Sahrens 
367fa9e4066Sahrens 	error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
368fa9e4066Sahrens 	    DMU_OT_NONE, 0, tx);
369fa9e4066Sahrens 	ASSERT(error == 0);
370fa9e4066Sahrens 
371e9dbad6fSeschrock 	error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
372fa9e4066Sahrens 	ASSERT(error == 0);
373fa9e4066Sahrens }
374fa9e4066Sahrens 
375b77b9231SDan McDonald /*
376b77b9231SDan McDonald  * Replay a TX_TRUNCATE ZIL transaction if asked.  TX_TRUNCATE is how we
377b77b9231SDan McDonald  * implement DKIOCFREE/free-long-range.
378b77b9231SDan McDonald  */
379b77b9231SDan McDonald static int
3803f7978d0SAlan Somers zvol_replay_truncate(void *arg1, void *arg2, boolean_t byteswap)
381b77b9231SDan McDonald {
3823f7978d0SAlan Somers 	zvol_state_t *zv = arg1;
3833f7978d0SAlan Somers 	lr_truncate_t *lr = arg2;
384b77b9231SDan McDonald 	uint64_t offset, length;
385b77b9231SDan McDonald 
386b77b9231SDan McDonald 	if (byteswap)
387b77b9231SDan McDonald 		byteswap_uint64_array(lr, sizeof (*lr));
388b77b9231SDan McDonald 
389b77b9231SDan McDonald 	offset = lr->lr_offset;
390b77b9231SDan McDonald 	length = lr->lr_length;
391b77b9231SDan McDonald 
392b77b9231SDan McDonald 	return (dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, offset, length));
393b77b9231SDan McDonald }
394b77b9231SDan McDonald 
39522ac5be4Sperrin /*
39622ac5be4Sperrin  * Replay a TX_WRITE ZIL transaction that didn't get committed
39722ac5be4Sperrin  * after a system failure
39822ac5be4Sperrin  */
399*eb633035STom Caputi /* ARGSUSED */
40022ac5be4Sperrin static int
4013f7978d0SAlan Somers zvol_replay_write(void *arg1, void *arg2, boolean_t byteswap)
40222ac5be4Sperrin {
4033f7978d0SAlan Somers 	zvol_state_t *zv = arg1;
4043f7978d0SAlan Somers 	lr_write_t *lr = arg2;
40522ac5be4Sperrin 	objset_t *os = zv->zv_objset;
40622ac5be4Sperrin 	char *data = (char *)(lr + 1);	/* data follows lr_write_t */
407b24ab676SJeff Bonwick 	uint64_t offset, length;
40822ac5be4Sperrin 	dmu_tx_t *tx;
40922ac5be4Sperrin 	int error;
41022ac5be4Sperrin 
41122ac5be4Sperrin 	if (byteswap)
41222ac5be4Sperrin 		byteswap_uint64_array(lr, sizeof (*lr));
41322ac5be4Sperrin 
414b24ab676SJeff Bonwick 	offset = lr->lr_offset;
415b24ab676SJeff Bonwick 	length = lr->lr_length;
416b24ab676SJeff Bonwick 
417b24ab676SJeff Bonwick 	/* If it's a dmu_sync() block, write the whole block */
418b24ab676SJeff Bonwick 	if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
419b24ab676SJeff Bonwick 		uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
420b24ab676SJeff Bonwick 		if (length < blocksize) {
421b24ab676SJeff Bonwick 			offset -= offset % blocksize;
422b24ab676SJeff Bonwick 			length = blocksize;
423b24ab676SJeff Bonwick 		}
424b24ab676SJeff Bonwick 	}
425975c32a0SNeil Perrin 
42622ac5be4Sperrin 	tx = dmu_tx_create(os);
427b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length);
4281209a471SNeil Perrin 	error = dmu_tx_assign(tx, TXG_WAIT);
42922ac5be4Sperrin 	if (error) {
43022ac5be4Sperrin 		dmu_tx_abort(tx);
43122ac5be4Sperrin 	} else {
432b24ab676SJeff Bonwick 		dmu_write(os, ZVOL_OBJ, offset, length, data, tx);
43322ac5be4Sperrin 		dmu_tx_commit(tx);
43422ac5be4Sperrin 	}
43522ac5be4Sperrin 
43622ac5be4Sperrin 	return (error);
43722ac5be4Sperrin }
43822ac5be4Sperrin 
43922ac5be4Sperrin /* ARGSUSED */
44022ac5be4Sperrin static int
4413f7978d0SAlan Somers zvol_replay_err(void *arg1, void *arg2, boolean_t byteswap)
44222ac5be4Sperrin {
443be6fd75aSMatthew Ahrens 	return (SET_ERROR(ENOTSUP));
44422ac5be4Sperrin }
44522ac5be4Sperrin 
44622ac5be4Sperrin /*
44722ac5be4Sperrin  * Callback vectors for replaying records.
448b77b9231SDan McDonald  * Only TX_WRITE and TX_TRUNCATE are needed for zvol.
44922ac5be4Sperrin  */
45022ac5be4Sperrin zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = {
45122ac5be4Sperrin 	zvol_replay_err,	/* 0 no such transaction type */
45222ac5be4Sperrin 	zvol_replay_err,	/* TX_CREATE */
45322ac5be4Sperrin 	zvol_replay_err,	/* TX_MKDIR */
45422ac5be4Sperrin 	zvol_replay_err,	/* TX_MKXATTR */
45522ac5be4Sperrin 	zvol_replay_err,	/* TX_SYMLINK */
45622ac5be4Sperrin 	zvol_replay_err,	/* TX_REMOVE */
45722ac5be4Sperrin 	zvol_replay_err,	/* TX_RMDIR */
45822ac5be4Sperrin 	zvol_replay_err,	/* TX_LINK */
45922ac5be4Sperrin 	zvol_replay_err,	/* TX_RENAME */
46022ac5be4Sperrin 	zvol_replay_write,	/* TX_WRITE */
461b77b9231SDan McDonald 	zvol_replay_truncate,	/* TX_TRUNCATE */
46222ac5be4Sperrin 	zvol_replay_err,	/* TX_SETATTR */
46322ac5be4Sperrin 	zvol_replay_err,	/* TX_ACL */
464975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_CREATE_ACL */
465975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_CREATE_ATTR */
466975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_CREATE_ACL_ATTR */
467975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_MKDIR_ACL */
468975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_MKDIR_ATTR */
469975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_MKDIR_ACL_ATTR */
470975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_WRITE2 */
47122ac5be4Sperrin };
47222ac5be4Sperrin 
473681d9761SEric Taylor int
474681d9761SEric Taylor zvol_name2minor(const char *name, minor_t *minor)
475681d9761SEric Taylor {
476681d9761SEric Taylor 	zvol_state_t *zv;
477681d9761SEric Taylor 
478c99e4bdcSChris Kirby 	mutex_enter(&zfsdev_state_lock);
479681d9761SEric Taylor 	zv = zvol_minor_lookup(name);
480681d9761SEric Taylor 	if (minor && zv)
481681d9761SEric Taylor 		*minor = zv->zv_minor;
482c99e4bdcSChris Kirby 	mutex_exit(&zfsdev_state_lock);
483681d9761SEric Taylor 	return (zv ? 0 : -1);
484681d9761SEric Taylor }
485681d9761SEric Taylor 
486e7cbe64fSgw /*
487e7cbe64fSgw  * Create a minor node (plus a whole lot more) for the specified volume.
488fa9e4066Sahrens  */
489fa9e4066Sahrens int
490681d9761SEric Taylor zvol_create_minor(const char *name)
491fa9e4066Sahrens {
492c99e4bdcSChris Kirby 	zfs_soft_state_t *zs;
493fa9e4066Sahrens 	zvol_state_t *zv;
494fa9e4066Sahrens 	objset_t *os;
49567bd71c6Sperrin 	dmu_object_info_t doi;
496fa9e4066Sahrens 	minor_t minor = 0;
497fa9e4066Sahrens 	char chrbuf[30], blkbuf[30];
498fa9e4066Sahrens 	int error;
499fa9e4066Sahrens 
500c99e4bdcSChris Kirby 	mutex_enter(&zfsdev_state_lock);
501fa9e4066Sahrens 
5021195e687SMark J Musante 	if (zvol_minor_lookup(name) != NULL) {
503c99e4bdcSChris Kirby 		mutex_exit(&zfsdev_state_lock);
504be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
505fa9e4066Sahrens 	}
506fa9e4066Sahrens 
507503ad85cSMatthew Ahrens 	/* lie and say we're read-only */
508*eb633035STom Caputi 	error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, B_TRUE, FTAG, &os);
509fa9e4066Sahrens 
510fa9e4066Sahrens 	if (error) {
511c99e4bdcSChris Kirby 		mutex_exit(&zfsdev_state_lock);
512fa9e4066Sahrens 		return (error);
513fa9e4066Sahrens 	}
514fa9e4066Sahrens 
515c99e4bdcSChris Kirby 	if ((minor = zfsdev_minor_alloc()) == 0) {
516*eb633035STom Caputi 		dmu_objset_disown(os, 1, FTAG);
517c99e4bdcSChris Kirby 		mutex_exit(&zfsdev_state_lock);
518be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
519fa9e4066Sahrens 	}
520fa9e4066Sahrens 
521c99e4bdcSChris Kirby 	if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS) {
522*eb633035STom Caputi 		dmu_objset_disown(os, 1, FTAG);
523c99e4bdcSChris Kirby 		mutex_exit(&zfsdev_state_lock);
524be6fd75aSMatthew Ahrens 		return (SET_ERROR(EAGAIN));
525fa9e4066Sahrens 	}
526e9dbad6fSeschrock 	(void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME,
527e9dbad6fSeschrock 	    (char *)name);
528fa9e4066Sahrens 
529681d9761SEric Taylor 	(void) snprintf(chrbuf, sizeof (chrbuf), "%u,raw", minor);
530fa9e4066Sahrens 
531fa9e4066Sahrens 	if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR,
532fa9e4066Sahrens 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
533c99e4bdcSChris Kirby 		ddi_soft_state_free(zfsdev_state, minor);
534*eb633035STom Caputi 		dmu_objset_disown(os, 1, FTAG);
535c99e4bdcSChris Kirby 		mutex_exit(&zfsdev_state_lock);
536be6fd75aSMatthew Ahrens 		return (SET_ERROR(EAGAIN));
537fa9e4066Sahrens 	}
538fa9e4066Sahrens 
539681d9761SEric Taylor 	(void) snprintf(blkbuf, sizeof (blkbuf), "%u", minor);
540fa9e4066Sahrens 
541fa9e4066Sahrens 	if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK,
542fa9e4066Sahrens 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
543fa9e4066Sahrens 		ddi_remove_minor_node(zfs_dip, chrbuf);
544c99e4bdcSChris Kirby 		ddi_soft_state_free(zfsdev_state, minor);
545*eb633035STom Caputi 		dmu_objset_disown(os, 1, FTAG);
546c99e4bdcSChris Kirby 		mutex_exit(&zfsdev_state_lock);
547be6fd75aSMatthew Ahrens 		return (SET_ERROR(EAGAIN));
548fa9e4066Sahrens 	}
549fa9e4066Sahrens 
550c99e4bdcSChris Kirby 	zs = ddi_get_soft_state(zfsdev_state, minor);
551c99e4bdcSChris Kirby 	zs->zss_type = ZSST_ZVOL;
552c99e4bdcSChris Kirby 	zv = zs->zss_data = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP);
553681d9761SEric Taylor 	(void) strlcpy(zv->zv_name, name, MAXPATHLEN);
554fa9e4066Sahrens 	zv->zv_min_bs = DEV_BSHIFT;
555fa9e4066Sahrens 	zv->zv_minor = minor;
556fa9e4066Sahrens 	zv->zv_objset = os;
557f9af39baSGeorge Wilson 	if (dmu_objset_is_snapshot(os) || !spa_writeable(dmu_objset_spa(os)))
558681d9761SEric Taylor 		zv->zv_flags |= ZVOL_RDONLY;
55979315247SMatthew Ahrens 	rangelock_init(&zv->zv_rangelock, NULL, NULL);
56088b7b0f2SMatthew Ahrens 	list_create(&zv->zv_extents, sizeof (zvol_extent_t),
56188b7b0f2SMatthew Ahrens 	    offsetof(zvol_extent_t, ze_node));
56267bd71c6Sperrin 	/* get and cache the blocksize */
56367bd71c6Sperrin 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
56467bd71c6Sperrin 	ASSERT(error == 0);
56567bd71c6Sperrin 	zv->zv_volblocksize = doi.doi_data_block_size;
56622ac5be4Sperrin 
567f9af39baSGeorge Wilson 	if (spa_writeable(dmu_objset_spa(os))) {
568f9af39baSGeorge Wilson 		if (zil_replay_disable)
569f9af39baSGeorge Wilson 			zil_destroy(dmu_objset_zil(os), B_FALSE);
570f9af39baSGeorge Wilson 		else
571f9af39baSGeorge Wilson 			zil_replay(os, zv, zvol_replay_vector);
572f9af39baSGeorge Wilson 	}
573*eb633035STom Caputi 	dmu_objset_disown(os, 1, FTAG);
574681d9761SEric Taylor 	zv->zv_objset = NULL;
575fa9e4066Sahrens 
576fa9e4066Sahrens 	zvol_minors++;
577fa9e4066Sahrens 
578c99e4bdcSChris Kirby 	mutex_exit(&zfsdev_state_lock);
579fa9e4066Sahrens 
580fa9e4066Sahrens 	return (0);
581fa9e4066Sahrens }
582fa9e4066Sahrens 
583fa9e4066Sahrens /*
584fa9e4066Sahrens  * Remove minor node for the specified volume.
585fa9e4066Sahrens  */
586681d9761SEric Taylor static int
587681d9761SEric Taylor zvol_remove_zv(zvol_state_t *zv)
588681d9761SEric Taylor {
589681d9761SEric Taylor 	char nmbuf[20];
590c99e4bdcSChris Kirby 	minor_t minor = zv->zv_minor;
591681d9761SEric Taylor 
592c99e4bdcSChris Kirby 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
593681d9761SEric Taylor 	if (zv->zv_total_opens != 0)
594be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
595681d9761SEric Taylor 
596c99e4bdcSChris Kirby 	(void) snprintf(nmbuf, sizeof (nmbuf), "%u,raw", minor);
597681d9761SEric Taylor 	ddi_remove_minor_node(zfs_dip, nmbuf);
598681d9761SEric Taylor 
599c99e4bdcSChris Kirby 	(void) snprintf(nmbuf, sizeof (nmbuf), "%u", minor);
600681d9761SEric Taylor 	ddi_remove_minor_node(zfs_dip, nmbuf);
601681d9761SEric Taylor 
60279315247SMatthew Ahrens 	rangelock_fini(&zv->zv_rangelock);
603681d9761SEric Taylor 
604c99e4bdcSChris Kirby 	kmem_free(zv, sizeof (zvol_state_t));
605c99e4bdcSChris Kirby 
606c99e4bdcSChris Kirby 	ddi_soft_state_free(zfsdev_state, minor);
607681d9761SEric Taylor 
608681d9761SEric Taylor 	zvol_minors--;
609681d9761SEric Taylor 	return (0);
610681d9761SEric Taylor }
611681d9761SEric Taylor 
612fa9e4066Sahrens int
613e9dbad6fSeschrock zvol_remove_minor(const char *name)
614fa9e4066Sahrens {
615fa9e4066Sahrens 	zvol_state_t *zv;
616681d9761SEric Taylor 	int rc;
617fa9e4066Sahrens 
618c99e4bdcSChris Kirby 	mutex_enter(&zfsdev_state_lock);
619e9dbad6fSeschrock 	if ((zv = zvol_minor_lookup(name)) == NULL) {
620c99e4bdcSChris Kirby 		mutex_exit(&zfsdev_state_lock);
621be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
622fa9e4066Sahrens 	}
623681d9761SEric Taylor 	rc = zvol_remove_zv(zv);
624c99e4bdcSChris Kirby 	mutex_exit(&zfsdev_state_lock);
625681d9761SEric Taylor 	return (rc);
626681d9761SEric Taylor }
627fa9e4066Sahrens 
628681d9761SEric Taylor int
629681d9761SEric Taylor zvol_first_open(zvol_state_t *zv)
630681d9761SEric Taylor {
631681d9761SEric Taylor 	objset_t *os;
632681d9761SEric Taylor 	uint64_t volsize;
633681d9761SEric Taylor 	int error;
634681d9761SEric Taylor 	uint64_t readonly;
635fa9e4066Sahrens 
636681d9761SEric Taylor 	/* lie and say we're read-only */
637*eb633035STom Caputi 	error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, B_TRUE, B_TRUE,
638681d9761SEric Taylor 	    zvol_tag, &os);
639681d9761SEric Taylor 	if (error)
640681d9761SEric Taylor 		return (error);
641fa9e4066Sahrens 
642c61ea566SGeorge Wilson 	zv->zv_objset = os;
643681d9761SEric Taylor 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
644681d9761SEric Taylor 	if (error) {
645681d9761SEric Taylor 		ASSERT(error == 0);
646*eb633035STom Caputi 		dmu_objset_disown(os, 1, zvol_tag);
647681d9761SEric Taylor 		return (error);
648681d9761SEric Taylor 	}
649c61ea566SGeorge Wilson 
6508dfe5547SRichard Yao 	error = dnode_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dn);
65194d1a210STim Haley 	if (error) {
652*eb633035STom Caputi 		dmu_objset_disown(os, 1, zvol_tag);
65394d1a210STim Haley 		return (error);
65494d1a210STim Haley 	}
655c61ea566SGeorge Wilson 
656c61ea566SGeorge Wilson 	zvol_size_changed(zv, volsize);
657681d9761SEric Taylor 	zv->zv_zilog = zil_open(os, zvol_get_data);
658fa9e4066Sahrens 
659681d9761SEric Taylor 	VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &readonly,
660681d9761SEric Taylor 	    NULL) == 0);
661f9af39baSGeorge Wilson 	if (readonly || dmu_objset_is_snapshot(os) ||
662f9af39baSGeorge Wilson 	    !spa_writeable(dmu_objset_spa(os)))
663681d9761SEric Taylor 		zv->zv_flags |= ZVOL_RDONLY;
664681d9761SEric Taylor 	else
665681d9761SEric Taylor 		zv->zv_flags &= ~ZVOL_RDONLY;
666681d9761SEric Taylor 	return (error);
667681d9761SEric Taylor }
668fa9e4066Sahrens 
669681d9761SEric Taylor void
670681d9761SEric Taylor zvol_last_close(zvol_state_t *zv)
671681d9761SEric Taylor {
67222ac5be4Sperrin 	zil_close(zv->zv_zilog);
67322ac5be4Sperrin 	zv->zv_zilog = NULL;
6742e2c1355SMatthew Ahrens 
6758dfe5547SRichard Yao 	dnode_rele(zv->zv_dn, zvol_tag);
6768dfe5547SRichard Yao 	zv->zv_dn = NULL;
6772e2c1355SMatthew Ahrens 
6782e2c1355SMatthew Ahrens 	/*
6792e2c1355SMatthew Ahrens 	 * Evict cached data
6802e2c1355SMatthew Ahrens 	 */
6812e2c1355SMatthew Ahrens 	if (dsl_dataset_is_dirty(dmu_objset_ds(zv->zv_objset)) &&
6822e2c1355SMatthew Ahrens 	    !(zv->zv_flags & ZVOL_RDONLY))
6832e2c1355SMatthew Ahrens 		txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
6843b2aab18SMatthew Ahrens 	dmu_objset_evict_dbufs(zv->zv_objset);
6852e2c1355SMatthew Ahrens 
686*eb633035STom Caputi 	dmu_objset_disown(zv->zv_objset, 1, zvol_tag);
687fa9e4066Sahrens 	zv->zv_objset = NULL;
688fa9e4066Sahrens }
689fa9e4066Sahrens 
690e7cbe64fSgw int
691e7cbe64fSgw zvol_prealloc(zvol_state_t *zv)
692e7cbe64fSgw {
693e7cbe64fSgw 	objset_t *os = zv->zv_objset;
694e7cbe64fSgw 	dmu_tx_t *tx;
695e7cbe64fSgw 	uint64_t refd, avail, usedobjs, availobjs;
696e7cbe64fSgw 	uint64_t resid = zv->zv_volsize;
697e7cbe64fSgw 	uint64_t off = 0;
698e7cbe64fSgw 
699e7cbe64fSgw 	/* Check the space usage before attempting to allocate the space */
700e7cbe64fSgw 	dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs);
701e7cbe64fSgw 	if (avail < zv->zv_volsize)
702be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
703e7cbe64fSgw 
704e7cbe64fSgw 	/* Free old extents if they exist */
705e7cbe64fSgw 	zvol_free_extents(zv);
706e7cbe64fSgw 
707e7cbe64fSgw 	while (resid != 0) {
708e7cbe64fSgw 		int error;
709b5152584SMatthew Ahrens 		uint64_t bytes = MIN(resid, SPA_OLD_MAXBLOCKSIZE);
710e7cbe64fSgw 
711e7cbe64fSgw 		tx = dmu_tx_create(os);
712e7cbe64fSgw 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
713e7cbe64fSgw 		error = dmu_tx_assign(tx, TXG_WAIT);
714e7cbe64fSgw 		if (error) {
715e7cbe64fSgw 			dmu_tx_abort(tx);
716cdb0ab79Smaybee 			(void) dmu_free_long_range(os, ZVOL_OBJ, 0, off);
717e7cbe64fSgw 			return (error);
718e7cbe64fSgw 		}
71982c9918fSTim Haley 		dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx);
720e7cbe64fSgw 		dmu_tx_commit(tx);
721e7cbe64fSgw 		off += bytes;
722e7cbe64fSgw 		resid -= bytes;
723e7cbe64fSgw 	}
724e7cbe64fSgw 	txg_wait_synced(dmu_objset_pool(os), 0);
725e7cbe64fSgw 
726e7cbe64fSgw 	return (0);
727e7cbe64fSgw }
728e7cbe64fSgw 
7293b2aab18SMatthew Ahrens static int
730681d9761SEric Taylor zvol_update_volsize(objset_t *os, uint64_t volsize)
731e7cbe64fSgw {
732e7cbe64fSgw 	dmu_tx_t *tx;
733e7cbe64fSgw 	int error;
734*eb633035STom Caputi 	uint64_t txg;
735e7cbe64fSgw 
736c99e4bdcSChris Kirby 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
737e7cbe64fSgw 
738681d9761SEric Taylor 	tx = dmu_tx_create(os);
739e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
7404bb73804SMatthew Ahrens 	dmu_tx_mark_netfree(tx);
741e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
742e7cbe64fSgw 	if (error) {
743e7cbe64fSgw 		dmu_tx_abort(tx);
744e7cbe64fSgw 		return (error);
745e7cbe64fSgw 	}
746*eb633035STom Caputi 	txg = dmu_tx_get_txg(tx);
747e7cbe64fSgw 
748681d9761SEric Taylor 	error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
749e7cbe64fSgw 	    &volsize, tx);
750e7cbe64fSgw 	dmu_tx_commit(tx);
751e7cbe64fSgw 
752*eb633035STom Caputi 	txg_wait_synced(dmu_objset_pool(os), txg);
753*eb633035STom Caputi 
754e7cbe64fSgw 	if (error == 0)
755681d9761SEric Taylor 		error = dmu_free_long_range(os,
756cdb0ab79Smaybee 		    ZVOL_OBJ, volsize, DMU_OBJECT_END);
757681d9761SEric Taylor 	return (error);
758681d9761SEric Taylor }
759e7cbe64fSgw 
760681d9761SEric Taylor void
761681d9761SEric Taylor zvol_remove_minors(const char *name)
762681d9761SEric Taylor {
763681d9761SEric Taylor 	zvol_state_t *zv;
764681d9761SEric Taylor 	char *namebuf;
765681d9761SEric Taylor 	minor_t minor;
766681d9761SEric Taylor 
767681d9761SEric Taylor 	namebuf = kmem_zalloc(strlen(name) + 2, KM_SLEEP);
768681d9761SEric Taylor 	(void) strncpy(namebuf, name, strlen(name));
769681d9761SEric Taylor 	(void) strcat(namebuf, "/");
770c99e4bdcSChris Kirby 	mutex_enter(&zfsdev_state_lock);
771c99e4bdcSChris Kirby 	for (minor = 1; minor <= ZFSDEV_MAX_MINOR; minor++) {
772681d9761SEric Taylor 
773c99e4bdcSChris Kirby 		zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
774681d9761SEric Taylor 		if (zv == NULL)
775681d9761SEric Taylor 			continue;
776681d9761SEric Taylor 		if (strncmp(namebuf, zv->zv_name, strlen(namebuf)) == 0)
777681d9761SEric Taylor 			(void) zvol_remove_zv(zv);
778e7cbe64fSgw 	}
779681d9761SEric Taylor 	kmem_free(namebuf, strlen(name) + 2);
780681d9761SEric Taylor 
781c99e4bdcSChris Kirby 	mutex_exit(&zfsdev_state_lock);
782e7cbe64fSgw }
783e7cbe64fSgw 
784c61ea566SGeorge Wilson static int
7853b2aab18SMatthew Ahrens zvol_update_live_volsize(zvol_state_t *zv, uint64_t volsize)
786fa9e4066Sahrens {
787e7cbe64fSgw 	uint64_t old_volsize = 0ULL;
7883b2aab18SMatthew Ahrens 	int error = 0;
789fa9e4066Sahrens 
790c61ea566SGeorge Wilson 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
791c61ea566SGeorge Wilson 
792e7cbe64fSgw 	/*
793e7cbe64fSgw 	 * Reinitialize the dump area to the new size. If we
794681d9761SEric Taylor 	 * failed to resize the dump area then restore it back to
795c61ea566SGeorge Wilson 	 * its original size.  We must set the new volsize prior
796c61ea566SGeorge Wilson 	 * to calling dumpvp_resize() to ensure that the devices'
797c61ea566SGeorge Wilson 	 * size(9P) is not visible by the dump subsystem.
798e7cbe64fSgw 	 */
7993b2aab18SMatthew Ahrens 	old_volsize = zv->zv_volsize;
8003b2aab18SMatthew Ahrens 	zvol_size_changed(zv, volsize);
8013b2aab18SMatthew Ahrens 
8023b2aab18SMatthew Ahrens 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
8033b2aab18SMatthew Ahrens 		if ((error = zvol_dumpify(zv)) != 0 ||
8043b2aab18SMatthew Ahrens 		    (error = dumpvp_resize()) != 0) {
8053b2aab18SMatthew Ahrens 			int dumpify_error;
8063b2aab18SMatthew Ahrens 
8073b2aab18SMatthew Ahrens 			(void) zvol_update_volsize(zv->zv_objset, old_volsize);
8083b2aab18SMatthew Ahrens 			zvol_size_changed(zv, old_volsize);
8093b2aab18SMatthew Ahrens 			dumpify_error = zvol_dumpify(zv);
8103b2aab18SMatthew Ahrens 			error = dumpify_error ? dumpify_error : error;
811681d9761SEric Taylor 		}
812fa9e4066Sahrens 	}
813fa9e4066Sahrens 
814573ca77eSGeorge Wilson 	/*
815573ca77eSGeorge Wilson 	 * Generate a LUN expansion event.
816573ca77eSGeorge Wilson 	 */
8173b2aab18SMatthew Ahrens 	if (error == 0) {
818573ca77eSGeorge Wilson 		sysevent_id_t eid;
819573ca77eSGeorge Wilson 		nvlist_t *attr;
820573ca77eSGeorge Wilson 		char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
821573ca77eSGeorge Wilson 
822681d9761SEric Taylor 		(void) snprintf(physpath, MAXPATHLEN, "%s%u", ZVOL_PSEUDO_DEV,
823573ca77eSGeorge Wilson 		    zv->zv_minor);
824573ca77eSGeorge Wilson 
825573ca77eSGeorge Wilson 		VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
826573ca77eSGeorge Wilson 		VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
827573ca77eSGeorge Wilson 
828573ca77eSGeorge Wilson 		(void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
829573ca77eSGeorge Wilson 		    ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
830573ca77eSGeorge Wilson 
831573ca77eSGeorge Wilson 		nvlist_free(attr);
832573ca77eSGeorge Wilson 		kmem_free(physpath, MAXPATHLEN);
833573ca77eSGeorge Wilson 	}
834c61ea566SGeorge Wilson 	return (error);
835c61ea566SGeorge Wilson }
836573ca77eSGeorge Wilson 
837c61ea566SGeorge Wilson int
838c61ea566SGeorge Wilson zvol_set_volsize(const char *name, uint64_t volsize)
839c61ea566SGeorge Wilson {
840c61ea566SGeorge Wilson 	zvol_state_t *zv = NULL;
841c61ea566SGeorge Wilson 	objset_t *os;
842c61ea566SGeorge Wilson 	int error;
843c61ea566SGeorge Wilson 	dmu_object_info_t doi;
844c61ea566SGeorge Wilson 	uint64_t readonly;
8453b2aab18SMatthew Ahrens 	boolean_t owned = B_FALSE;
8463b2aab18SMatthew Ahrens 
8473b2aab18SMatthew Ahrens 	error = dsl_prop_get_integer(name,
8483b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_READONLY), &readonly, NULL);
8493b2aab18SMatthew Ahrens 	if (error != 0)
8503b2aab18SMatthew Ahrens 		return (error);
8513b2aab18SMatthew Ahrens 	if (readonly)
852be6fd75aSMatthew Ahrens 		return (SET_ERROR(EROFS));
853c61ea566SGeorge Wilson 
854c61ea566SGeorge Wilson 	mutex_enter(&zfsdev_state_lock);
855c61ea566SGeorge Wilson 	zv = zvol_minor_lookup(name);
8563b2aab18SMatthew Ahrens 
8573b2aab18SMatthew Ahrens 	if (zv == NULL || zv->zv_objset == NULL) {
858*eb633035STom Caputi 		if ((error = dmu_objset_own(name, DMU_OST_ZVOL, B_FALSE, B_TRUE,
8593b2aab18SMatthew Ahrens 		    FTAG, &os)) != 0) {
8603b2aab18SMatthew Ahrens 			mutex_exit(&zfsdev_state_lock);
8613b2aab18SMatthew Ahrens 			return (error);
8623b2aab18SMatthew Ahrens 		}
8633b2aab18SMatthew Ahrens 		owned = B_TRUE;
8643b2aab18SMatthew Ahrens 		if (zv != NULL)
8653b2aab18SMatthew Ahrens 			zv->zv_objset = os;
8663b2aab18SMatthew Ahrens 	} else {
8673b2aab18SMatthew Ahrens 		os = zv->zv_objset;
868c61ea566SGeorge Wilson 	}
869c61ea566SGeorge Wilson 
870c61ea566SGeorge Wilson 	if ((error = dmu_object_info(os, ZVOL_OBJ, &doi)) != 0 ||
8713b2aab18SMatthew Ahrens 	    (error = zvol_check_volsize(volsize, doi.doi_data_block_size)) != 0)
872c61ea566SGeorge Wilson 		goto out;
873c61ea566SGeorge Wilson 
8743b2aab18SMatthew Ahrens 	error = zvol_update_volsize(os, volsize);
875c61ea566SGeorge Wilson 
8763b2aab18SMatthew Ahrens 	if (error == 0 && zv != NULL)
8773b2aab18SMatthew Ahrens 		error = zvol_update_live_volsize(zv, volsize);
878bb0ade09Sahrens out:
8793b2aab18SMatthew Ahrens 	if (owned) {
880*eb633035STom Caputi 		dmu_objset_disown(os, B_TRUE, FTAG);
8813b2aab18SMatthew Ahrens 		if (zv != NULL)
8823b2aab18SMatthew Ahrens 			zv->zv_objset = NULL;
8833b2aab18SMatthew Ahrens 	}
884c99e4bdcSChris Kirby 	mutex_exit(&zfsdev_state_lock);
885fa9e4066Sahrens 	return (error);
886fa9e4066Sahrens }
887fa9e4066Sahrens 
888fa9e4066Sahrens /*ARGSUSED*/
889fa9e4066Sahrens int
890fa9e4066Sahrens zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr)
891fa9e4066Sahrens {
892fa9e4066Sahrens 	zvol_state_t *zv;
893681d9761SEric Taylor 	int err = 0;
894fa9e4066Sahrens 
895c99e4bdcSChris Kirby 	mutex_enter(&zfsdev_state_lock);
896fa9e4066Sahrens 
897c99e4bdcSChris Kirby 	zv = zfsdev_get_soft_state(getminor(*devp), ZSST_ZVOL);
898fa9e4066Sahrens 	if (zv == NULL) {
899c99e4bdcSChris Kirby 		mutex_exit(&zfsdev_state_lock);
900be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
901fa9e4066Sahrens 	}
902fa9e4066Sahrens 
903681d9761SEric Taylor 	if (zv->zv_total_opens == 0)
904681d9761SEric Taylor 		err = zvol_first_open(zv);
905681d9761SEric Taylor 	if (err) {
906c99e4bdcSChris Kirby 		mutex_exit(&zfsdev_state_lock);
907681d9761SEric Taylor 		return (err);
908681d9761SEric Taylor 	}
909*eb633035STom Caputi 	/*
910*eb633035STom Caputi 	 * Check for a bad on-disk format version now since we
911*eb633035STom Caputi 	 * lied about owning the dataset readonly before.
912*eb633035STom Caputi 	 */
913*eb633035STom Caputi 	if ((flag & FWRITE) && ((zv->zv_flags & ZVOL_RDONLY) ||
914*eb633035STom Caputi 	    dmu_objset_incompatible_encryption_version(zv->zv_objset))) {
915be6fd75aSMatthew Ahrens 		err = SET_ERROR(EROFS);
916681d9761SEric Taylor 		goto out;
917fa9e4066Sahrens 	}
918c7f714e2SEric Taylor 	if (zv->zv_flags & ZVOL_EXCL) {
919be6fd75aSMatthew Ahrens 		err = SET_ERROR(EBUSY);
920681d9761SEric Taylor 		goto out;
921c7f714e2SEric Taylor 	}
922c7f714e2SEric Taylor 	if (flag & FEXCL) {
923c7f714e2SEric Taylor 		if (zv->zv_total_opens != 0) {
924be6fd75aSMatthew Ahrens 			err = SET_ERROR(EBUSY);
925681d9761SEric Taylor 			goto out;
926c7f714e2SEric Taylor 		}
927c7f714e2SEric Taylor 		zv->zv_flags |= ZVOL_EXCL;
928c7f714e2SEric Taylor 	}
929fa9e4066Sahrens 
930fa9e4066Sahrens 	if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) {
931fa9e4066Sahrens 		zv->zv_open_count[otyp]++;
932fa9e4066Sahrens 		zv->zv_total_opens++;
933fa9e4066Sahrens 	}
934c99e4bdcSChris Kirby 	mutex_exit(&zfsdev_state_lock);
935fa9e4066Sahrens 
936681d9761SEric Taylor 	return (err);
937681d9761SEric Taylor out:
938681d9761SEric Taylor 	if (zv->zv_total_opens == 0)
939681d9761SEric Taylor 		zvol_last_close(zv);
940c99e4bdcSChris Kirby 	mutex_exit(&zfsdev_state_lock);
941681d9761SEric Taylor 	return (err);
942fa9e4066Sahrens }
943fa9e4066Sahrens 
944fa9e4066Sahrens /*ARGSUSED*/
945fa9e4066Sahrens int
946fa9e4066Sahrens zvol_close(dev_t dev, int flag, int otyp, cred_t *cr)
947fa9e4066Sahrens {
948fa9e4066Sahrens 	minor_t minor = getminor(dev);
949fa9e4066Sahrens 	zvol_state_t *zv;
950681d9761SEric Taylor 	int error = 0;
951fa9e4066Sahrens 
952c99e4bdcSChris Kirby 	mutex_enter(&zfsdev_state_lock);
953fa9e4066Sahrens 
954c99e4bdcSChris Kirby 	zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
955fa9e4066Sahrens 	if (zv == NULL) {
956c99e4bdcSChris Kirby 		mutex_exit(&zfsdev_state_lock);
957be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
958fa9e4066Sahrens 	}
959fa9e4066Sahrens 
960c7f714e2SEric Taylor 	if (zv->zv_flags & ZVOL_EXCL) {
961c7f714e2SEric Taylor 		ASSERT(zv->zv_total_opens == 1);
962c7f714e2SEric Taylor 		zv->zv_flags &= ~ZVOL_EXCL;
963fa9e4066Sahrens 	}
964fa9e4066Sahrens 
965fa9e4066Sahrens 	/*
966fa9e4066Sahrens 	 * If the open count is zero, this is a spurious close.
967fa9e4066Sahrens 	 * That indicates a bug in the kernel / DDI framework.
968fa9e4066Sahrens 	 */
969fa9e4066Sahrens 	ASSERT(zv->zv_open_count[otyp] != 0);
970fa9e4066Sahrens 	ASSERT(zv->zv_total_opens != 0);
971fa9e4066Sahrens 
972fa9e4066Sahrens 	/*
973fa9e4066Sahrens 	 * You may get multiple opens, but only one close.
974fa9e4066Sahrens 	 */
975fa9e4066Sahrens 	zv->zv_open_count[otyp]--;
976fa9e4066Sahrens 	zv->zv_total_opens--;
977fa9e4066Sahrens 
978681d9761SEric Taylor 	if (zv->zv_total_opens == 0)
979681d9761SEric Taylor 		zvol_last_close(zv);
980fa9e4066Sahrens 
981c99e4bdcSChris Kirby 	mutex_exit(&zfsdev_state_lock);
982681d9761SEric Taylor 	return (error);
983fa9e4066Sahrens }
984fa9e4066Sahrens 
985cab3a55eSPrakash Surya /* ARGSUSED */
986feb08c6bSbillm static void
987b24ab676SJeff Bonwick zvol_get_done(zgd_t *zgd, int error)
98867bd71c6Sperrin {
989b24ab676SJeff Bonwick 	if (zgd->zgd_db)
990b24ab676SJeff Bonwick 		dmu_buf_rele(zgd->zgd_db, zgd);
991b24ab676SJeff Bonwick 
99279315247SMatthew Ahrens 	rangelock_exit(zgd->zgd_lr);
993b24ab676SJeff Bonwick 
99467bd71c6Sperrin 	kmem_free(zgd, sizeof (zgd_t));
99567bd71c6Sperrin }
99667bd71c6Sperrin 
99767bd71c6Sperrin /*
99867bd71c6Sperrin  * Get data to generate a TX_WRITE intent log record.
99967bd71c6Sperrin  */
1000feb08c6bSbillm static int
10011271e4b1SPrakash Surya zvol_get_data(void *arg, lr_write_t *lr, char *buf, struct lwb *lwb, zio_t *zio)
100267bd71c6Sperrin {
100367bd71c6Sperrin 	zvol_state_t *zv = arg;
1004b24ab676SJeff Bonwick 	uint64_t offset = lr->lr_offset;
1005b24ab676SJeff Bonwick 	uint64_t size = lr->lr_length;	/* length of user data */
100667bd71c6Sperrin 	dmu_buf_t *db;
100767bd71c6Sperrin 	zgd_t *zgd;
100867bd71c6Sperrin 	int error;
100967bd71c6Sperrin 
10101271e4b1SPrakash Surya 	ASSERT3P(lwb, !=, NULL);
10111271e4b1SPrakash Surya 	ASSERT3P(zio, !=, NULL);
10121271e4b1SPrakash Surya 	ASSERT3U(size, !=, 0);
1013b24ab676SJeff Bonwick 
1014b24ab676SJeff Bonwick 	zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
10151271e4b1SPrakash Surya 	zgd->zgd_lwb = lwb;
1016feb08c6bSbillm 
1017c2e6a7d6Sperrin 	/*
1018c2e6a7d6Sperrin 	 * Write records come in two flavors: immediate and indirect.
1019c2e6a7d6Sperrin 	 * For small writes it's cheaper to store the data with the
1020c2e6a7d6Sperrin 	 * log record (immediate); for large writes it's cheaper to
1021c2e6a7d6Sperrin 	 * sync the data and get a pointer to it (indirect) so that
1022c2e6a7d6Sperrin 	 * we don't have to write the data twice.
1023c2e6a7d6Sperrin 	 */
102442b14111SLOLi 	if (buf != NULL) { /* immediate write */
102579315247SMatthew Ahrens 		zgd->zgd_lr = rangelock_enter(&zv->zv_rangelock, offset, size,
102642b14111SLOLi 		    RL_READER);
10278dfe5547SRichard Yao 		error = dmu_read_by_dnode(zv->zv_dn, offset, size, buf,
1028b24ab676SJeff Bonwick 		    DMU_READ_NO_PREFETCH);
102942b14111SLOLi 	} else { /* indirect write */
103042b14111SLOLi 		/*
103142b14111SLOLi 		 * Have to lock the whole block to ensure when it's written out
103242b14111SLOLi 		 * and its checksum is being calculated that no one can change
103342b14111SLOLi 		 * the data. Contrarily to zfs_get_data we need not re-check
103442b14111SLOLi 		 * blocksize after we get the lock because it cannot be changed.
103542b14111SLOLi 		 */
1036b24ab676SJeff Bonwick 		size = zv->zv_volblocksize;
1037b24ab676SJeff Bonwick 		offset = P2ALIGN(offset, size);
103879315247SMatthew Ahrens 		zgd->zgd_lr = rangelock_enter(&zv->zv_rangelock, offset, size,
103942b14111SLOLi 		    RL_READER);
10408dfe5547SRichard Yao 		error = dmu_buf_hold_by_dnode(zv->zv_dn, offset, zgd, &db,
104147cb52daSJeff Bonwick 		    DMU_READ_NO_PREFETCH);
1042b24ab676SJeff Bonwick 		if (error == 0) {
1043b7edcb94SMatthew Ahrens 			blkptr_t *bp = &lr->lr_blkptr;
104480901aeaSGeorge Wilson 
1045b24ab676SJeff Bonwick 			zgd->zgd_db = db;
1046b24ab676SJeff Bonwick 			zgd->zgd_bp = bp;
104767bd71c6Sperrin 
1048b24ab676SJeff Bonwick 			ASSERT(db->db_offset == offset);
1049b24ab676SJeff Bonwick 			ASSERT(db->db_size == size);
105067bd71c6Sperrin 
1051b24ab676SJeff Bonwick 			error = dmu_sync(zio, lr->lr_common.lrc_txg,
1052b24ab676SJeff Bonwick 			    zvol_get_done, zgd);
1053975c32a0SNeil Perrin 
1054b24ab676SJeff Bonwick 			if (error == 0)
1055b24ab676SJeff Bonwick 				return (0);
1056b24ab676SJeff Bonwick 		}
1057975c32a0SNeil Perrin 	}
1058975c32a0SNeil Perrin 
1059b24ab676SJeff Bonwick 	zvol_get_done(zgd, error);
1060b24ab676SJeff Bonwick 
106167bd71c6Sperrin 	return (error);
106267bd71c6Sperrin }
106367bd71c6Sperrin 
1064a24e15ceSperrin /*
1065a24e15ceSperrin  * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
106622ac5be4Sperrin  *
106722ac5be4Sperrin  * We store data in the log buffers if it's small enough.
106867bd71c6Sperrin  * Otherwise we will later flush the data out via dmu_sync().
106922ac5be4Sperrin  */
107067bd71c6Sperrin ssize_t zvol_immediate_write_sz = 32768;
107122ac5be4Sperrin 
1072feb08c6bSbillm static void
1073510b6c0eSNeil Perrin zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid,
1074510b6c0eSNeil Perrin     boolean_t sync)
107522ac5be4Sperrin {
1076feb08c6bSbillm 	uint32_t blocksize = zv->zv_volblocksize;
10771209a471SNeil Perrin 	zilog_t *zilog = zv->zv_zilog;
1078c5ee4681SAlexander Motin 	itx_wr_state_t write_state;
1079510b6c0eSNeil Perrin 
1080b24ab676SJeff Bonwick 	if (zil_replaying(zilog, tx))
10811209a471SNeil Perrin 		return;
10821209a471SNeil Perrin 
1083c5ee4681SAlexander Motin 	if (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
1084c5ee4681SAlexander Motin 		write_state = WR_INDIRECT;
1085c5ee4681SAlexander Motin 	else if (!spa_has_slogs(zilog->zl_spa) &&
1086c5ee4681SAlexander Motin 	    resid >= blocksize && blocksize > zvol_immediate_write_sz)
1087c5ee4681SAlexander Motin 		write_state = WR_INDIRECT;
1088c5ee4681SAlexander Motin 	else if (sync)
1089c5ee4681SAlexander Motin 		write_state = WR_COPIED;
1090c5ee4681SAlexander Motin 	else
1091c5ee4681SAlexander Motin 		write_state = WR_NEED_COPY;
1092feb08c6bSbillm 
1093510b6c0eSNeil Perrin 	while (resid) {
1094510b6c0eSNeil Perrin 		itx_t *itx;
1095510b6c0eSNeil Perrin 		lr_write_t *lr;
1096c5ee4681SAlexander Motin 		itx_wr_state_t wr_state = write_state;
1097c5ee4681SAlexander Motin 		ssize_t len = resid;
1098c5ee4681SAlexander Motin 
1099c5ee4681SAlexander Motin 		if (wr_state == WR_COPIED && resid > ZIL_MAX_COPIED_DATA)
1100c5ee4681SAlexander Motin 			wr_state = WR_NEED_COPY;
1101c5ee4681SAlexander Motin 		else if (wr_state == WR_INDIRECT)
1102c5ee4681SAlexander Motin 			len = MIN(blocksize - P2PHASE(off, blocksize), resid);
1103510b6c0eSNeil Perrin 
1104510b6c0eSNeil Perrin 		itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
1105c5ee4681SAlexander Motin 		    (wr_state == WR_COPIED ? len : 0));
1106feb08c6bSbillm 		lr = (lr_write_t *)&itx->itx_lr;
11078dfe5547SRichard Yao 		if (wr_state == WR_COPIED && dmu_read_by_dnode(zv->zv_dn,
11088dfe5547SRichard Yao 		    off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
1109b24ab676SJeff Bonwick 			zil_itx_destroy(itx);
1110510b6c0eSNeil Perrin 			itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1111510b6c0eSNeil Perrin 			lr = (lr_write_t *)&itx->itx_lr;
1112c5ee4681SAlexander Motin 			wr_state = WR_NEED_COPY;
1113510b6c0eSNeil Perrin 		}
1114510b6c0eSNeil Perrin 
1115c5ee4681SAlexander Motin 		itx->itx_wr_state = wr_state;
1116feb08c6bSbillm 		lr->lr_foid = ZVOL_OBJ;
1117feb08c6bSbillm 		lr->lr_offset = off;
1118510b6c0eSNeil Perrin 		lr->lr_length = len;
1119b24ab676SJeff Bonwick 		lr->lr_blkoff = 0;
1120feb08c6bSbillm 		BP_ZERO(&lr->lr_blkptr);
1121feb08c6bSbillm 
1122510b6c0eSNeil Perrin 		itx->itx_private = zv;
1123510b6c0eSNeil Perrin 		itx->itx_sync = sync;
1124510b6c0eSNeil Perrin 
11255002558fSNeil Perrin 		zil_itx_assign(zilog, itx, tx);
1126510b6c0eSNeil Perrin 
1127510b6c0eSNeil Perrin 		off += len;
1128510b6c0eSNeil Perrin 		resid -= len;
112922ac5be4Sperrin 	}
113022ac5be4Sperrin }
113122ac5be4Sperrin 
113288b7b0f2SMatthew Ahrens static int
1133810e43b2SBill Pijewski zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t origoffset,
1134810e43b2SBill Pijewski     uint64_t size, boolean_t doread, boolean_t isdump)
1135e7cbe64fSgw {
1136e7cbe64fSgw 	vdev_disk_t *dvd;
1137e7cbe64fSgw 	int c;
1138e7cbe64fSgw 	int numerrors = 0;
1139e7cbe64fSgw 
1140810e43b2SBill Pijewski 	if (vd->vdev_ops == &vdev_mirror_ops ||
1141810e43b2SBill Pijewski 	    vd->vdev_ops == &vdev_replacing_ops ||
1142810e43b2SBill Pijewski 	    vd->vdev_ops == &vdev_spare_ops) {
1143810e43b2SBill Pijewski 		for (c = 0; c < vd->vdev_children; c++) {
1144810e43b2SBill Pijewski 			int err = zvol_dumpio_vdev(vd->vdev_child[c],
1145810e43b2SBill Pijewski 			    addr, offset, origoffset, size, doread, isdump);
1146810e43b2SBill Pijewski 			if (err != 0) {
1147810e43b2SBill Pijewski 				numerrors++;
1148810e43b2SBill Pijewski 			} else if (doread) {
1149810e43b2SBill Pijewski 				break;
1150810e43b2SBill Pijewski 			}
1151e7cbe64fSgw 		}
1152e7cbe64fSgw 	}
1153e7cbe64fSgw 
1154810e43b2SBill Pijewski 	if (!vd->vdev_ops->vdev_op_leaf && vd->vdev_ops != &vdev_raidz_ops)
1155e7cbe64fSgw 		return (numerrors < vd->vdev_children ? 0 : EIO);
1156e7cbe64fSgw 
1157dc0bb255SEric Taylor 	if (doread && !vdev_readable(vd))
1158be6fd75aSMatthew Ahrens 		return (SET_ERROR(EIO));
1159dc0bb255SEric Taylor 	else if (!doread && !vdev_writeable(vd))
1160be6fd75aSMatthew Ahrens 		return (SET_ERROR(EIO));
1161e7cbe64fSgw 
1162810e43b2SBill Pijewski 	if (vd->vdev_ops == &vdev_raidz_ops) {
1163810e43b2SBill Pijewski 		return (vdev_raidz_physio(vd,
1164810e43b2SBill Pijewski 		    addr, size, offset, origoffset, doread, isdump));
1165810e43b2SBill Pijewski 	}
1166810e43b2SBill Pijewski 
1167e7cbe64fSgw 	offset += VDEV_LABEL_START_SIZE;
1168e7cbe64fSgw 
1169e7cbe64fSgw 	if (ddi_in_panic() || isdump) {
117088b7b0f2SMatthew Ahrens 		ASSERT(!doread);
117188b7b0f2SMatthew Ahrens 		if (doread)
1172be6fd75aSMatthew Ahrens 			return (SET_ERROR(EIO));
1173810e43b2SBill Pijewski 		dvd = vd->vdev_tsd;
1174810e43b2SBill Pijewski 		ASSERT3P(dvd, !=, NULL);
1175e7cbe64fSgw 		return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset),
1176e7cbe64fSgw 		    lbtodb(size)));
1177e7cbe64fSgw 	} else {
1178810e43b2SBill Pijewski 		dvd = vd->vdev_tsd;
1179810e43b2SBill Pijewski 		ASSERT3P(dvd, !=, NULL);
1180810e43b2SBill Pijewski 		return (vdev_disk_ldi_physio(dvd->vd_lh, addr, size,
1181810e43b2SBill Pijewski 		    offset, doread ? B_READ : B_WRITE));
1182e7cbe64fSgw 	}
1183e7cbe64fSgw }
1184e7cbe64fSgw 
118588b7b0f2SMatthew Ahrens static int
118688b7b0f2SMatthew Ahrens zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size,
118788b7b0f2SMatthew Ahrens     boolean_t doread, boolean_t isdump)
1188e7cbe64fSgw {
1189e7cbe64fSgw 	vdev_t *vd;
1190e7cbe64fSgw 	int error;
119188b7b0f2SMatthew Ahrens 	zvol_extent_t *ze;
1192e7cbe64fSgw 	spa_t *spa = dmu_objset_spa(zv->zv_objset);
1193e7cbe64fSgw 
119488b7b0f2SMatthew Ahrens 	/* Must be sector aligned, and not stradle a block boundary. */
119588b7b0f2SMatthew Ahrens 	if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) ||
119688b7b0f2SMatthew Ahrens 	    P2BOUNDARY(offset, size, zv->zv_volblocksize)) {
1197be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
119888b7b0f2SMatthew Ahrens 	}
119988b7b0f2SMatthew Ahrens 	ASSERT(size <= zv->zv_volblocksize);
1200e7cbe64fSgw 
120188b7b0f2SMatthew Ahrens 	/* Locate the extent this belongs to */
120288b7b0f2SMatthew Ahrens 	ze = list_head(&zv->zv_extents);
120388b7b0f2SMatthew Ahrens 	while (offset >= ze->ze_nblks * zv->zv_volblocksize) {
120488b7b0f2SMatthew Ahrens 		offset -= ze->ze_nblks * zv->zv_volblocksize;
120588b7b0f2SMatthew Ahrens 		ze = list_next(&zv->zv_extents, ze);
120688b7b0f2SMatthew Ahrens 	}
120724cc0e1cSGeorge Wilson 
12083b2aab18SMatthew Ahrens 	if (ze == NULL)
1209be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
12103b2aab18SMatthew Ahrens 
121124cc0e1cSGeorge Wilson 	if (!ddi_in_panic())
121224cc0e1cSGeorge Wilson 		spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
121324cc0e1cSGeorge Wilson 
121488b7b0f2SMatthew Ahrens 	vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva));
121588b7b0f2SMatthew Ahrens 	offset += DVA_GET_OFFSET(&ze->ze_dva);
1216810e43b2SBill Pijewski 	error = zvol_dumpio_vdev(vd, addr, offset, DVA_GET_OFFSET(&ze->ze_dva),
1217810e43b2SBill Pijewski 	    size, doread, isdump);
121824cc0e1cSGeorge Wilson 
121924cc0e1cSGeorge Wilson 	if (!ddi_in_panic())
122024cc0e1cSGeorge Wilson 		spa_config_exit(spa, SCL_STATE, FTAG);
122124cc0e1cSGeorge Wilson 
1222e7cbe64fSgw 	return (error);
1223e7cbe64fSgw }
1224e7cbe64fSgw 
1225fa9e4066Sahrens int
1226fa9e4066Sahrens zvol_strategy(buf_t *bp)
1227fa9e4066Sahrens {
1228c99e4bdcSChris Kirby 	zfs_soft_state_t *zs = NULL;
1229c99e4bdcSChris Kirby 	zvol_state_t *zv;
1230fa9e4066Sahrens 	uint64_t off, volsize;
123188b7b0f2SMatthew Ahrens 	size_t resid;
1232fa9e4066Sahrens 	char *addr;
123322ac5be4Sperrin 	objset_t *os;
1234fa9e4066Sahrens 	int error = 0;
123588b7b0f2SMatthew Ahrens 	boolean_t doread = bp->b_flags & B_READ;
1236810e43b2SBill Pijewski 	boolean_t is_dumpified;
1237510b6c0eSNeil Perrin 	boolean_t sync;
1238fa9e4066Sahrens 
1239c99e4bdcSChris Kirby 	if (getminor(bp->b_edev) == 0) {
1240be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
1241c99e4bdcSChris Kirby 	} else {
1242c99e4bdcSChris Kirby 		zs = ddi_get_soft_state(zfsdev_state, getminor(bp->b_edev));
1243c99e4bdcSChris Kirby 		if (zs == NULL)
1244be6fd75aSMatthew Ahrens 			error = SET_ERROR(ENXIO);
1245c99e4bdcSChris Kirby 		else if (zs->zss_type != ZSST_ZVOL)
1246be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
1247fa9e4066Sahrens 	}
1248fa9e4066Sahrens 
1249c99e4bdcSChris Kirby 	if (error) {
1250c99e4bdcSChris Kirby 		bioerror(bp, error);
1251fa9e4066Sahrens 		biodone(bp);
1252fa9e4066Sahrens 		return (0);
1253fa9e4066Sahrens 	}
1254fa9e4066Sahrens 
1255c99e4bdcSChris Kirby 	zv = zs->zss_data;
1256c99e4bdcSChris Kirby 
1257681d9761SEric Taylor 	if (!(bp->b_flags & B_READ) && (zv->zv_flags & ZVOL_RDONLY)) {
1258fa9e4066Sahrens 		bioerror(bp, EROFS);
1259fa9e4066Sahrens 		biodone(bp);
1260fa9e4066Sahrens 		return (0);
1261fa9e4066Sahrens 	}
1262fa9e4066Sahrens 
1263fa9e4066Sahrens 	off = ldbtob(bp->b_blkno);
1264fa9e4066Sahrens 	volsize = zv->zv_volsize;
1265fa9e4066Sahrens 
126622ac5be4Sperrin 	os = zv->zv_objset;
126722ac5be4Sperrin 	ASSERT(os != NULL);
1268fa9e4066Sahrens 
1269fa9e4066Sahrens 	bp_mapin(bp);
1270fa9e4066Sahrens 	addr = bp->b_un.b_addr;
1271fa9e4066Sahrens 	resid = bp->b_bcount;
1272fa9e4066Sahrens 
127388b7b0f2SMatthew Ahrens 	if (resid > 0 && (off < 0 || off >= volsize)) {
127488b7b0f2SMatthew Ahrens 		bioerror(bp, EIO);
127588b7b0f2SMatthew Ahrens 		biodone(bp);
127688b7b0f2SMatthew Ahrens 		return (0);
127788b7b0f2SMatthew Ahrens 	}
127873ec3d9cSgw 
1279810e43b2SBill Pijewski 	is_dumpified = zv->zv_flags & ZVOL_DUMPIFIED;
128055da60b9SMark J Musante 	sync = ((!(bp->b_flags & B_ASYNC) &&
128155da60b9SMark J Musante 	    !(zv->zv_flags & ZVOL_WCE)) ||
128255da60b9SMark J Musante 	    (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)) &&
1283810e43b2SBill Pijewski 	    !doread && !is_dumpified;
1284510b6c0eSNeil Perrin 
1285c3377ee9SJohn Levon 	smt_begin_unsafe();
1286455e370cSJohn Levon 
1287a24e15ceSperrin 	/*
1288a24e15ceSperrin 	 * There must be no buffer changes when doing a dmu_sync() because
1289a24e15ceSperrin 	 * we can't change the data whilst calculating the checksum.
1290a24e15ceSperrin 	 */
129179315247SMatthew Ahrens 	locked_range_t *lr = rangelock_enter(&zv->zv_rangelock, off, resid,
129288b7b0f2SMatthew Ahrens 	    doread ? RL_READER : RL_WRITER);
1293fa9e4066Sahrens 
1294e7cbe64fSgw 	while (resid != 0 && off < volsize) {
129588b7b0f2SMatthew Ahrens 		size_t size = MIN(resid, zvol_maxphys);
1296810e43b2SBill Pijewski 		if (is_dumpified) {
1297e7cbe64fSgw 			size = MIN(size, P2END(off, zv->zv_volblocksize) - off);
129888b7b0f2SMatthew Ahrens 			error = zvol_dumpio(zv, addr, off, size,
129988b7b0f2SMatthew Ahrens 			    doread, B_FALSE);
130088b7b0f2SMatthew Ahrens 		} else if (doread) {
13017bfdf011SNeil Perrin 			error = dmu_read(os, ZVOL_OBJ, off, size, addr,
13027bfdf011SNeil Perrin 			    DMU_READ_PREFETCH);
1303fa9e4066Sahrens 		} else {
130422ac5be4Sperrin 			dmu_tx_t *tx = dmu_tx_create(os);
1305fa9e4066Sahrens 			dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
1306fa9e4066Sahrens 			error = dmu_tx_assign(tx, TXG_WAIT);
1307fa9e4066Sahrens 			if (error) {
1308fa9e4066Sahrens 				dmu_tx_abort(tx);
1309fa9e4066Sahrens 			} else {
131022ac5be4Sperrin 				dmu_write(os, ZVOL_OBJ, off, size, addr, tx);
1311510b6c0eSNeil Perrin 				zvol_log_write(zv, tx, off, size, sync);
1312fa9e4066Sahrens 				dmu_tx_commit(tx);
1313fa9e4066Sahrens 			}
1314fa9e4066Sahrens 		}
1315b87f3af3Sperrin 		if (error) {
1316b87f3af3Sperrin 			/* convert checksum errors into IO errors */
1317b87f3af3Sperrin 			if (error == ECKSUM)
1318be6fd75aSMatthew Ahrens 				error = SET_ERROR(EIO);
1319fa9e4066Sahrens 			break;
1320b87f3af3Sperrin 		}
1321fa9e4066Sahrens 		off += size;
1322fa9e4066Sahrens 		addr += size;
1323fa9e4066Sahrens 		resid -= size;
1324fa9e4066Sahrens 	}
132579315247SMatthew Ahrens 	rangelock_exit(lr);
1326fa9e4066Sahrens 
1327fa9e4066Sahrens 	if ((bp->b_resid = resid) == bp->b_bcount)
1328fa9e4066Sahrens 		bioerror(bp, off > volsize ? EINVAL : error);
1329fa9e4066Sahrens 
1330510b6c0eSNeil Perrin 	if (sync)
13315002558fSNeil Perrin 		zil_commit(zv->zv_zilog, ZVOL_OBJ);
1332feb08c6bSbillm 	biodone(bp);
133322ac5be4Sperrin 
1334c3377ee9SJohn Levon 	smt_end_unsafe();
1335455e370cSJohn Levon 
1336fa9e4066Sahrens 	return (0);
1337fa9e4066Sahrens }
1338fa9e4066Sahrens 
133967bd71c6Sperrin /*
134067bd71c6Sperrin  * Set the buffer count to the zvol maximum transfer.
134167bd71c6Sperrin  * Using our own routine instead of the default minphys()
134267bd71c6Sperrin  * means that for larger writes we write bigger buffers on X86
134367bd71c6Sperrin  * (128K instead of 56K) and flush the disk write cache less often
134467bd71c6Sperrin  * (every zvol_maxphys - currently 1MB) instead of minphys (currently
134567bd71c6Sperrin  * 56K on X86 and 128K on sparc).
134667bd71c6Sperrin  */
134767bd71c6Sperrin void
134867bd71c6Sperrin zvol_minphys(struct buf *bp)
134967bd71c6Sperrin {
135067bd71c6Sperrin 	if (bp->b_bcount > zvol_maxphys)
135167bd71c6Sperrin 		bp->b_bcount = zvol_maxphys;
135267bd71c6Sperrin }
135367bd71c6Sperrin 
1354e7cbe64fSgw int
1355e7cbe64fSgw zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks)
1356e7cbe64fSgw {
1357e7cbe64fSgw 	minor_t minor = getminor(dev);
1358e7cbe64fSgw 	zvol_state_t *zv;
1359e7cbe64fSgw 	int error = 0;
1360e7cbe64fSgw 	uint64_t size;
1361e7cbe64fSgw 	uint64_t boff;
1362e7cbe64fSgw 	uint64_t resid;
1363e7cbe64fSgw 
1364c99e4bdcSChris Kirby 	zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1365e7cbe64fSgw 	if (zv == NULL)
1366be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
1367e7cbe64fSgw 
13683b2aab18SMatthew Ahrens 	if ((zv->zv_flags & ZVOL_DUMPIFIED) == 0)
1369be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
13703b2aab18SMatthew Ahrens 
1371e7cbe64fSgw 	boff = ldbtob(blkno);
1372e7cbe64fSgw 	resid = ldbtob(nblocks);
137388b7b0f2SMatthew Ahrens 
137488b7b0f2SMatthew Ahrens 	VERIFY3U(boff + resid, <=, zv->zv_volsize);
137588b7b0f2SMatthew Ahrens 
1376e7cbe64fSgw 	while (resid) {
1377e7cbe64fSgw 		size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff);
137888b7b0f2SMatthew Ahrens 		error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE);
1379e7cbe64fSgw 		if (error)
1380e7cbe64fSgw 			break;
1381e7cbe64fSgw 		boff += size;
1382e7cbe64fSgw 		addr += size;
1383e7cbe64fSgw 		resid -= size;
1384e7cbe64fSgw 	}
1385e7cbe64fSgw 
1386e7cbe64fSgw 	return (error);
1387e7cbe64fSgw }
1388e7cbe64fSgw 
1389fa9e4066Sahrens /*ARGSUSED*/
1390fa9e4066Sahrens int
1391feb08c6bSbillm zvol_read(dev_t dev, uio_t *uio, cred_t *cr)
1392fa9e4066Sahrens {
1393c7ca1008Sgw 	minor_t minor = getminor(dev);
1394c7ca1008Sgw 	zvol_state_t *zv;
139573ec3d9cSgw 	uint64_t volsize;
1396feb08c6bSbillm 	int error = 0;
1397fa9e4066Sahrens 
1398c99e4bdcSChris Kirby 	zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1399c7ca1008Sgw 	if (zv == NULL)
1400be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
1401c7ca1008Sgw 
140273ec3d9cSgw 	volsize = zv->zv_volsize;
140373ec3d9cSgw 	if (uio->uio_resid > 0 &&
140473ec3d9cSgw 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
1405be6fd75aSMatthew Ahrens 		return (SET_ERROR(EIO));
140673ec3d9cSgw 
140788b7b0f2SMatthew Ahrens 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
140888b7b0f2SMatthew Ahrens 		error = physio(zvol_strategy, NULL, dev, B_READ,
140988b7b0f2SMatthew Ahrens 		    zvol_minphys, uio);
141088b7b0f2SMatthew Ahrens 		return (error);
141188b7b0f2SMatthew Ahrens 	}
141288b7b0f2SMatthew Ahrens 
1413c3377ee9SJohn Levon 	smt_begin_unsafe();
1414455e370cSJohn Levon 
141579315247SMatthew Ahrens 	locked_range_t *lr = rangelock_enter(&zv->zv_rangelock,
141679315247SMatthew Ahrens 	    uio->uio_loffset, uio->uio_resid, RL_READER);
141773ec3d9cSgw 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1418feb08c6bSbillm 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1419fa9e4066Sahrens 
142073ec3d9cSgw 		/* don't read past the end */
142173ec3d9cSgw 		if (bytes > volsize - uio->uio_loffset)
142273ec3d9cSgw 			bytes = volsize - uio->uio_loffset;
142373ec3d9cSgw 
1424feb08c6bSbillm 		error =  dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes);
1425b87f3af3Sperrin 		if (error) {
1426b87f3af3Sperrin 			/* convert checksum errors into IO errors */
1427b87f3af3Sperrin 			if (error == ECKSUM)
1428be6fd75aSMatthew Ahrens 				error = SET_ERROR(EIO);
1429feb08c6bSbillm 			break;
1430b87f3af3Sperrin 		}
1431feb08c6bSbillm 	}
143279315247SMatthew Ahrens 	rangelock_exit(lr);
143379315247SMatthew Ahrens 
1434c3377ee9SJohn Levon 	smt_end_unsafe();
1435455e370cSJohn Levon 
1436feb08c6bSbillm 	return (error);
1437fa9e4066Sahrens }
1438fa9e4066Sahrens 
1439fa9e4066Sahrens /*ARGSUSED*/
1440fa9e4066Sahrens int
1441feb08c6bSbillm zvol_write(dev_t dev, uio_t *uio, cred_t *cr)
1442fa9e4066Sahrens {
1443c7ca1008Sgw 	minor_t minor = getminor(dev);
1444c7ca1008Sgw 	zvol_state_t *zv;
144573ec3d9cSgw 	uint64_t volsize;
1446feb08c6bSbillm 	int error = 0;
1447510b6c0eSNeil Perrin 	boolean_t sync;
1448feb08c6bSbillm 
1449c99e4bdcSChris Kirby 	zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1450c7ca1008Sgw 	if (zv == NULL)
1451be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
1452c7ca1008Sgw 
145373ec3d9cSgw 	volsize = zv->zv_volsize;
145473ec3d9cSgw 	if (uio->uio_resid > 0 &&
145573ec3d9cSgw 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
1456be6fd75aSMatthew Ahrens 		return (SET_ERROR(EIO));
145773ec3d9cSgw 
1458e7cbe64fSgw 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
1459e7cbe64fSgw 		error = physio(zvol_strategy, NULL, dev, B_WRITE,
1460e7cbe64fSgw 		    zvol_minphys, uio);
1461e7cbe64fSgw 		return (error);
1462e7cbe64fSgw 	}
1463e7cbe64fSgw 
1464c3377ee9SJohn Levon 	smt_begin_unsafe();
1465455e370cSJohn Levon 
146655da60b9SMark J Musante 	sync = !(zv->zv_flags & ZVOL_WCE) ||
146755da60b9SMark J Musante 	    (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS);
1468510b6c0eSNeil Perrin 
146979315247SMatthew Ahrens 	locked_range_t *lr = rangelock_enter(&zv->zv_rangelock,
147079315247SMatthew Ahrens 	    uio->uio_loffset, uio->uio_resid, RL_WRITER);
147173ec3d9cSgw 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1472feb08c6bSbillm 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1473feb08c6bSbillm 		uint64_t off = uio->uio_loffset;
1474feb08c6bSbillm 		dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
147573ec3d9cSgw 
147673ec3d9cSgw 		if (bytes > volsize - off)	/* don't write past the end */
147773ec3d9cSgw 			bytes = volsize - off;
147873ec3d9cSgw 
1479feb08c6bSbillm 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
1480feb08c6bSbillm 		error = dmu_tx_assign(tx, TXG_WAIT);
1481feb08c6bSbillm 		if (error) {
1482feb08c6bSbillm 			dmu_tx_abort(tx);
1483feb08c6bSbillm 			break;
1484feb08c6bSbillm 		}
14858dfe5547SRichard Yao 		error = dmu_write_uio_dnode(zv->zv_dn, uio, bytes, tx);
1486feb08c6bSbillm 		if (error == 0)
1487510b6c0eSNeil Perrin 			zvol_log_write(zv, tx, off, bytes, sync);
1488feb08c6bSbillm 		dmu_tx_commit(tx);
1489feb08c6bSbillm 
1490feb08c6bSbillm 		if (error)
1491feb08c6bSbillm 			break;
1492feb08c6bSbillm 	}
149379315247SMatthew Ahrens 	rangelock_exit(lr);
149479315247SMatthew Ahrens 
1495510b6c0eSNeil Perrin 	if (sync)
14965002558fSNeil Perrin 		zil_commit(zv->zv_zilog, ZVOL_OBJ);
1497455e370cSJohn Levon 
1498c3377ee9SJohn Levon 	smt_end_unsafe();
1499455e370cSJohn Levon 
1500feb08c6bSbillm 	return (error);
1501fa9e4066Sahrens }
1502fa9e4066Sahrens 
1503c7f714e2SEric Taylor int
1504c7f714e2SEric Taylor zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs)
1505c7f714e2SEric Taylor {
1506c7f714e2SEric Taylor 	struct uuid uuid = EFI_RESERVED;
1507c7f714e2SEric Taylor 	efi_gpe_t gpe = { 0 };
1508c7f714e2SEric Taylor 	uint32_t crc;
1509c7f714e2SEric Taylor 	dk_efi_t efi;
1510c7f714e2SEric Taylor 	int length;
1511c7f714e2SEric Taylor 	char *ptr;
1512c7f714e2SEric Taylor 
1513c7f714e2SEric Taylor 	if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag))
1514be6fd75aSMatthew Ahrens 		return (SET_ERROR(EFAULT));
1515c7f714e2SEric Taylor 	ptr = (char *)(uintptr_t)efi.dki_data_64;
1516c7f714e2SEric Taylor 	length = efi.dki_length;
1517c7f714e2SEric Taylor 	/*
1518c7f714e2SEric Taylor 	 * Some clients may attempt to request a PMBR for the
1519c7f714e2SEric Taylor 	 * zvol.  Currently this interface will return EINVAL to
1520c7f714e2SEric Taylor 	 * such requests.  These requests could be supported by
1521c7f714e2SEric Taylor 	 * adding a check for lba == 0 and consing up an appropriate
1522c7f714e2SEric Taylor 	 * PMBR.
1523c7f714e2SEric Taylor 	 */
1524c7f714e2SEric Taylor 	if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0)
1525be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1526c7f714e2SEric Taylor 
1527c7f714e2SEric Taylor 	gpe.efi_gpe_StartingLBA = LE_64(34ULL);
1528c7f714e2SEric Taylor 	gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1);
1529c7f714e2SEric Taylor 	UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
1530c7f714e2SEric Taylor 
1531c7f714e2SEric Taylor 	if (efi.dki_lba == 1) {
1532c7f714e2SEric Taylor 		efi_gpt_t gpt = { 0 };
1533c7f714e2SEric Taylor 
1534c7f714e2SEric Taylor 		gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
1535c7f714e2SEric Taylor 		gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
1536fd797736SJohn Levon 		gpt.efi_gpt_HeaderSize = LE_32(EFI_HEADER_SIZE);
1537c7f714e2SEric Taylor 		gpt.efi_gpt_MyLBA = LE_64(1ULL);
1538c7f714e2SEric Taylor 		gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL);
1539c7f714e2SEric Taylor 		gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1);
1540c7f714e2SEric Taylor 		gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL);
1541c7f714e2SEric Taylor 		gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
1542c7f714e2SEric Taylor 		gpt.efi_gpt_SizeOfPartitionEntry =
1543c7f714e2SEric Taylor 		    LE_32(sizeof (efi_gpe_t));
1544c7f714e2SEric Taylor 		CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
1545c7f714e2SEric Taylor 		gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
1546fd797736SJohn Levon 		CRC32(crc, &gpt, EFI_HEADER_SIZE, -1U, crc32_table);
1547c7f714e2SEric Taylor 		gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
1548c7f714e2SEric Taylor 		if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length),
1549c7f714e2SEric Taylor 		    flag))
1550be6fd75aSMatthew Ahrens 			return (SET_ERROR(EFAULT));
1551c7f714e2SEric Taylor 		ptr += sizeof (gpt);
1552c7f714e2SEric Taylor 		length -= sizeof (gpt);
1553c7f714e2SEric Taylor 	}
1554c7f714e2SEric Taylor 	if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe),
1555c7f714e2SEric Taylor 	    length), flag))
1556be6fd75aSMatthew Ahrens 		return (SET_ERROR(EFAULT));
1557c7f714e2SEric Taylor 	return (0);
1558c7f714e2SEric Taylor }
1559c7f714e2SEric Taylor 
15603fb517f7SJames Moore /*
15613fb517f7SJames Moore  * BEGIN entry points to allow external callers access to the volume.
15623fb517f7SJames Moore  */
15633fb517f7SJames Moore /*
15643fb517f7SJames Moore  * Return the volume parameters needed for access from an external caller.
15653fb517f7SJames Moore  * These values are invariant as long as the volume is held open.
15663fb517f7SJames Moore  */
15673fb517f7SJames Moore int
15683fb517f7SJames Moore zvol_get_volume_params(minor_t minor, uint64_t *blksize,
15693fb517f7SJames Moore     uint64_t *max_xfer_len, void **minor_hdl, void **objset_hdl, void **zil_hdl,
15708dfe5547SRichard Yao     void **rl_hdl, void **dnode_hdl)
15713fb517f7SJames Moore {
15723fb517f7SJames Moore 	zvol_state_t *zv;
15733fb517f7SJames Moore 
1574c99e4bdcSChris Kirby 	zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1575c99e4bdcSChris Kirby 	if (zv == NULL)
1576be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
15773fb517f7SJames Moore 	if (zv->zv_flags & ZVOL_DUMPIFIED)
1578be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
15793fb517f7SJames Moore 
15803fb517f7SJames Moore 	ASSERT(blksize && max_xfer_len && minor_hdl &&
15818dfe5547SRichard Yao 	    objset_hdl && zil_hdl && rl_hdl && dnode_hdl);
15823fb517f7SJames Moore 
15833fb517f7SJames Moore 	*blksize = zv->zv_volblocksize;
15843fb517f7SJames Moore 	*max_xfer_len = (uint64_t)zvol_maxphys;
15853fb517f7SJames Moore 	*minor_hdl = zv;
15863fb517f7SJames Moore 	*objset_hdl = zv->zv_objset;
15873fb517f7SJames Moore 	*zil_hdl = zv->zv_zilog;
158879315247SMatthew Ahrens 	*rl_hdl = &zv->zv_rangelock;
15898dfe5547SRichard Yao 	*dnode_hdl = zv->zv_dn;
15903fb517f7SJames Moore 	return (0);
15913fb517f7SJames Moore }
15923fb517f7SJames Moore 
15933fb517f7SJames Moore /*
15943fb517f7SJames Moore  * Return the current volume size to an external caller.
15953fb517f7SJames Moore  * The size can change while the volume is open.
15963fb517f7SJames Moore  */
15973fb517f7SJames Moore uint64_t
15983fb517f7SJames Moore zvol_get_volume_size(void *minor_hdl)
15993fb517f7SJames Moore {
16003fb517f7SJames Moore 	zvol_state_t *zv = minor_hdl;
16013fb517f7SJames Moore 
16023fb517f7SJames Moore 	return (zv->zv_volsize);
16033fb517f7SJames Moore }
16043fb517f7SJames Moore 
16053fb517f7SJames Moore /*
16063fb517f7SJames Moore  * Return the current WCE setting to an external caller.
16073fb517f7SJames Moore  * The WCE setting can change while the volume is open.
16083fb517f7SJames Moore  */
16093fb517f7SJames Moore int
16103fb517f7SJames Moore zvol_get_volume_wce(void *minor_hdl)
16113fb517f7SJames Moore {
16123fb517f7SJames Moore 	zvol_state_t *zv = minor_hdl;
16133fb517f7SJames Moore 
16143fb517f7SJames Moore 	return ((zv->zv_flags & ZVOL_WCE) ? 1 : 0);
16153fb517f7SJames Moore }
16163fb517f7SJames Moore 
16173fb517f7SJames Moore /*
16183fb517f7SJames Moore  * Entry point for external callers to zvol_log_write
16193fb517f7SJames Moore  */
16203fb517f7SJames Moore void
16213fb517f7SJames Moore zvol_log_write_minor(void *minor_hdl, dmu_tx_t *tx, offset_t off, ssize_t resid,
16223fb517f7SJames Moore     boolean_t sync)
16233fb517f7SJames Moore {
16243fb517f7SJames Moore 	zvol_state_t *zv = minor_hdl;
16253fb517f7SJames Moore 
16263fb517f7SJames Moore 	zvol_log_write(zv, tx, off, resid, sync);
16273fb517f7SJames Moore }
16283fb517f7SJames Moore /*
16293fb517f7SJames Moore  * END entry points to allow external callers access to the volume.
16303fb517f7SJames Moore  */
16313fb517f7SJames Moore 
1632b77b9231SDan McDonald /*
1633b77b9231SDan McDonald  * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE.
1634b77b9231SDan McDonald  */
1635b77b9231SDan McDonald static void
1636b77b9231SDan McDonald zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off, uint64_t len,
1637b77b9231SDan McDonald     boolean_t sync)
1638b77b9231SDan McDonald {
1639b77b9231SDan McDonald 	itx_t *itx;
1640b77b9231SDan McDonald 	lr_truncate_t *lr;
1641b77b9231SDan McDonald 	zilog_t *zilog = zv->zv_zilog;
1642b77b9231SDan McDonald 
1643b77b9231SDan McDonald 	if (zil_replaying(zilog, tx))
1644b77b9231SDan McDonald 		return;
1645b77b9231SDan McDonald 
1646b77b9231SDan McDonald 	itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1647b77b9231SDan McDonald 	lr = (lr_truncate_t *)&itx->itx_lr;
1648b77b9231SDan McDonald 	lr->lr_foid = ZVOL_OBJ;
1649b77b9231SDan McDonald 	lr->lr_offset = off;
1650b77b9231SDan McDonald 	lr->lr_length = len;
1651b77b9231SDan McDonald 
1652b77b9231SDan McDonald 	itx->itx_sync = sync;
1653b77b9231SDan McDonald 	zil_itx_assign(zilog, itx, tx);
1654b77b9231SDan McDonald }
1655b77b9231SDan McDonald 
1656fa9e4066Sahrens /*
1657fa9e4066Sahrens  * Dirtbag ioctls to support mkfs(1M) for UFS filesystems.  See dkio(7I).
1658b77b9231SDan McDonald  * Also a dirtbag dkio ioctl for unmap/free-block functionality.
1659fa9e4066Sahrens  */
1660fa9e4066Sahrens /*ARGSUSED*/
1661fa9e4066Sahrens int
1662fa9e4066Sahrens zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
1663fa9e4066Sahrens {
1664fa9e4066Sahrens 	zvol_state_t *zv;
1665af2c4821Smaybee 	struct dk_callback *dkc;
1666fa9e4066Sahrens 	int error = 0;
166779315247SMatthew Ahrens 	locked_range_t *lr;
1668fa9e4066Sahrens 
1669c99e4bdcSChris Kirby 	mutex_enter(&zfsdev_state_lock);
1670fa9e4066Sahrens 
1671c99e4bdcSChris Kirby 	zv = zfsdev_get_soft_state(getminor(dev), ZSST_ZVOL);
1672fa9e4066Sahrens 
1673fa9e4066Sahrens 	if (zv == NULL) {
1674c99e4bdcSChris Kirby 		mutex_exit(&zfsdev_state_lock);
1675be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
1676fa9e4066Sahrens 	}
1677701f66c4SEric Taylor 	ASSERT(zv->zv_total_opens > 0);
1678fa9e4066Sahrens 
1679fa9e4066Sahrens 	switch (cmd) {
1680fa9e4066Sahrens 
1681fa9e4066Sahrens 	case DKIOCINFO:
1682a0b60564SGeorge Wilson 	{
1683a0b60564SGeorge Wilson 		struct dk_cinfo dki;
1684a0b60564SGeorge Wilson 
1685af2c4821Smaybee 		bzero(&dki, sizeof (dki));
1686af2c4821Smaybee 		(void) strcpy(dki.dki_cname, "zvol");
1687af2c4821Smaybee 		(void) strcpy(dki.dki_dname, "zvol");
1688af2c4821Smaybee 		dki.dki_ctype = DKC_UNKNOWN;
16893adc9019SEric Taylor 		dki.dki_unit = getminor(dev);
1690b5152584SMatthew Ahrens 		dki.dki_maxtransfer =
1691b5152584SMatthew Ahrens 		    1 << (SPA_OLD_MAXBLOCKSHIFT - zv->zv_min_bs);
1692c99e4bdcSChris Kirby 		mutex_exit(&zfsdev_state_lock);
1693af2c4821Smaybee 		if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag))
1694be6fd75aSMatthew Ahrens 			error = SET_ERROR(EFAULT);
1695fa9e4066Sahrens 		return (error);
1696a0b60564SGeorge Wilson 	}
1697fa9e4066Sahrens 
1698fa9e4066Sahrens 	case DKIOCGMEDIAINFO:
1699a0b60564SGeorge Wilson 	{
1700a0b60564SGeorge Wilson 		struct dk_minfo dkm;
1701a0b60564SGeorge Wilson 
1702fa9e4066Sahrens 		bzero(&dkm, sizeof (dkm));
1703fa9e4066Sahrens 		dkm.dki_lbsize = 1U << zv->zv_min_bs;
1704fa9e4066Sahrens 		dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
1705fa9e4066Sahrens 		dkm.dki_media_type = DK_UNKNOWN;
1706c99e4bdcSChris Kirby 		mutex_exit(&zfsdev_state_lock);
1707fa9e4066Sahrens 		if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
1708be6fd75aSMatthew Ahrens 			error = SET_ERROR(EFAULT);
1709fa9e4066Sahrens 		return (error);
1710a0b60564SGeorge Wilson 	}
1711a0b60564SGeorge Wilson 
1712a0b60564SGeorge Wilson 	case DKIOCGMEDIAINFOEXT:
1713a0b60564SGeorge Wilson 	{
1714a0b60564SGeorge Wilson 		struct dk_minfo_ext dkmext;
1715a0b60564SGeorge Wilson 
1716a0b60564SGeorge Wilson 		bzero(&dkmext, sizeof (dkmext));
1717a0b60564SGeorge Wilson 		dkmext.dki_lbsize = 1U << zv->zv_min_bs;
1718a0b60564SGeorge Wilson 		dkmext.dki_pbsize = zv->zv_volblocksize;
1719a0b60564SGeorge Wilson 		dkmext.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
1720a0b60564SGeorge Wilson 		dkmext.dki_media_type = DK_UNKNOWN;
1721a0b60564SGeorge Wilson 		mutex_exit(&zfsdev_state_lock);
1722a0b60564SGeorge Wilson 		if (ddi_copyout(&dkmext, (void *)arg, sizeof (dkmext), flag))
1723a0b60564SGeorge Wilson 			error = SET_ERROR(EFAULT);
1724a0b60564SGeorge Wilson 		return (error);
1725a0b60564SGeorge Wilson 	}
1726fa9e4066Sahrens 
1727fa9e4066Sahrens 	case DKIOCGETEFI:
1728a0b60564SGeorge Wilson 	{
1729a0b60564SGeorge Wilson 		uint64_t vs = zv->zv_volsize;
1730a0b60564SGeorge Wilson 		uint8_t bs = zv->zv_min_bs;
1731fa9e4066Sahrens 
1732a0b60564SGeorge Wilson 		mutex_exit(&zfsdev_state_lock);
1733a0b60564SGeorge Wilson 		error = zvol_getefi((void *)arg, flag, vs, bs);
1734a0b60564SGeorge Wilson 		return (error);
1735a0b60564SGeorge Wilson 	}
1736fa9e4066Sahrens 
1737feb08c6bSbillm 	case DKIOCFLUSHWRITECACHE:
1738af2c4821Smaybee 		dkc = (struct dk_callback *)arg;
1739c99e4bdcSChris Kirby 		mutex_exit(&zfsdev_state_lock);
1740455e370cSJohn Levon 
1741c3377ee9SJohn Levon 		smt_begin_unsafe();
1742455e370cSJohn Levon 
17435002558fSNeil Perrin 		zil_commit(zv->zv_zilog, ZVOL_OBJ);
1744af2c4821Smaybee 		if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) {
1745af2c4821Smaybee 			(*dkc->dkc_callback)(dkc->dkc_cookie, error);
1746af2c4821Smaybee 			error = 0;
1747af2c4821Smaybee 		}
1748455e370cSJohn Levon 
1749c3377ee9SJohn Levon 		smt_end_unsafe();
1750455e370cSJohn Levon 
1751701f66c4SEric Taylor 		return (error);
1752701f66c4SEric Taylor 
1753701f66c4SEric Taylor 	case DKIOCGETWCE:
1754a0b60564SGeorge Wilson 	{
1755a0b60564SGeorge Wilson 		int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0;
1756a0b60564SGeorge Wilson 		if (ddi_copyout(&wce, (void *)arg, sizeof (int),
1757a0b60564SGeorge Wilson 		    flag))
1758a0b60564SGeorge Wilson 			error = SET_ERROR(EFAULT);
1759a0b60564SGeorge Wilson 		break;
1760a0b60564SGeorge Wilson 	}
1761a0b60564SGeorge Wilson 	case DKIOCSETWCE:
1762a0b60564SGeorge Wilson 	{
1763a0b60564SGeorge Wilson 		int wce;
1764a0b60564SGeorge Wilson 		if (ddi_copyin((void *)arg, &wce, sizeof (int),
1765a0b60564SGeorge Wilson 		    flag)) {
1766a0b60564SGeorge Wilson 			error = SET_ERROR(EFAULT);
1767701f66c4SEric Taylor 			break;
1768701f66c4SEric Taylor 		}
1769a0b60564SGeorge Wilson 		if (wce) {
1770a0b60564SGeorge Wilson 			zv->zv_flags |= ZVOL_WCE;
1771a0b60564SGeorge Wilson 			mutex_exit(&zfsdev_state_lock);
1772a0b60564SGeorge Wilson 		} else {
1773a0b60564SGeorge Wilson 			zv->zv_flags &= ~ZVOL_WCE;
1774a0b60564SGeorge Wilson 			mutex_exit(&zfsdev_state_lock);
1775c3377ee9SJohn Levon 			smt_begin_unsafe();
1776a0b60564SGeorge Wilson 			zil_commit(zv->zv_zilog, ZVOL_OBJ);
1777c3377ee9SJohn Levon 			smt_end_unsafe();
1778701f66c4SEric Taylor 		}
1779a0b60564SGeorge Wilson 		return (0);
1780a0b60564SGeorge Wilson 	}
1781feb08c6bSbillm 
1782b6130eadSmaybee 	case DKIOCGGEOM:
1783b6130eadSmaybee 	case DKIOCGVTOC:
1784e7cbe64fSgw 		/*
1785e7cbe64fSgw 		 * commands using these (like prtvtoc) expect ENOTSUP
1786e7cbe64fSgw 		 * since we're emulating an EFI label
1787e7cbe64fSgw 		 */
1788be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENOTSUP);
1789b6130eadSmaybee 		break;
1790b6130eadSmaybee 
1791e7cbe64fSgw 	case DKIOCDUMPINIT:
179279315247SMatthew Ahrens 		lr = rangelock_enter(&zv->zv_rangelock, 0, zv->zv_volsize,
1793e7cbe64fSgw 		    RL_WRITER);
1794e7cbe64fSgw 		error = zvol_dumpify(zv);
179579315247SMatthew Ahrens 		rangelock_exit(lr);
1796e7cbe64fSgw 		break;
1797e7cbe64fSgw 
1798e7cbe64fSgw 	case DKIOCDUMPFINI:
179906d5ae10SEric Taylor 		if (!(zv->zv_flags & ZVOL_DUMPIFIED))
180006d5ae10SEric Taylor 			break;
180179315247SMatthew Ahrens 		lr = rangelock_enter(&zv->zv_rangelock, 0, zv->zv_volsize,
1802e7cbe64fSgw 		    RL_WRITER);
1803e7cbe64fSgw 		error = zvol_dump_fini(zv);
180479315247SMatthew Ahrens 		rangelock_exit(lr);
1805e7cbe64fSgw 		break;
1806e7cbe64fSgw 
1807b77b9231SDan McDonald 	case DKIOCFREE:
1808b77b9231SDan McDonald 	{
1809047c81d3SSaso Kiselkov 		dkioc_free_list_t *dfl;
1810b77b9231SDan McDonald 		dmu_tx_t *tx;
1811b77b9231SDan McDonald 
1812893c83baSGeorge Wilson 		if (!zvol_unmap_enabled)
1813893c83baSGeorge Wilson 			break;
1814893c83baSGeorge Wilson 
1815047c81d3SSaso Kiselkov 		if (!(flag & FKIOCTL)) {
1816047c81d3SSaso Kiselkov 			error = dfl_copyin((void *)arg, &dfl, flag, KM_SLEEP);
1817047c81d3SSaso Kiselkov 			if (error != 0)
1818047c81d3SSaso Kiselkov 				break;
1819047c81d3SSaso Kiselkov 		} else {
1820047c81d3SSaso Kiselkov 			dfl = (dkioc_free_list_t *)arg;
1821047c81d3SSaso Kiselkov 			ASSERT3U(dfl->dfl_num_exts, <=, DFL_COPYIN_MAX_EXTS);
1822047c81d3SSaso Kiselkov 			if (dfl->dfl_num_exts > DFL_COPYIN_MAX_EXTS) {
1823047c81d3SSaso Kiselkov 				error = SET_ERROR(EINVAL);
1824047c81d3SSaso Kiselkov 				break;
1825047c81d3SSaso Kiselkov 			}
1826b77b9231SDan McDonald 		}
1827b77b9231SDan McDonald 
1828574e2414SGeorge Wilson 		mutex_exit(&zfsdev_state_lock);
1829b77b9231SDan McDonald 
1830c3377ee9SJohn Levon 		smt_begin_unsafe();
1831455e370cSJohn Levon 
1832047c81d3SSaso Kiselkov 		for (int i = 0; i < dfl->dfl_num_exts; i++) {
1833047c81d3SSaso Kiselkov 			uint64_t start = dfl->dfl_exts[i].dfle_start,
1834047c81d3SSaso Kiselkov 			    length = dfl->dfl_exts[i].dfle_length,
1835047c81d3SSaso Kiselkov 			    end = start + length;
1836047c81d3SSaso Kiselkov 
1837047c81d3SSaso Kiselkov 			/*
1838047c81d3SSaso Kiselkov 			 * Apply Postel's Law to length-checking.  If they
1839047c81d3SSaso Kiselkov 			 * overshoot, just blank out until the end, if there's
1840047c81d3SSaso Kiselkov 			 * a need to blank out anything.
1841047c81d3SSaso Kiselkov 			 */
1842047c81d3SSaso Kiselkov 			if (start >= zv->zv_volsize)
1843047c81d3SSaso Kiselkov 				continue;	/* No need to do anything... */
1844047c81d3SSaso Kiselkov 			if (end > zv->zv_volsize) {
1845047c81d3SSaso Kiselkov 				end = DMU_OBJECT_END;
1846047c81d3SSaso Kiselkov 				length = end - start;
1847047c81d3SSaso Kiselkov 			}
1848b77b9231SDan McDonald 
184979315247SMatthew Ahrens 			lr = rangelock_enter(&zv->zv_rangelock, start, length,
1850047c81d3SSaso Kiselkov 			    RL_WRITER);
1851047c81d3SSaso Kiselkov 			tx = dmu_tx_create(zv->zv_objset);
1852047c81d3SSaso Kiselkov 			error = dmu_tx_assign(tx, TXG_WAIT);
1853047c81d3SSaso Kiselkov 			if (error != 0) {
1854047c81d3SSaso Kiselkov 				dmu_tx_abort(tx);
1855047c81d3SSaso Kiselkov 			} else {
1856047c81d3SSaso Kiselkov 				zvol_log_truncate(zv, tx, start, length,
1857047c81d3SSaso Kiselkov 				    B_TRUE);
1858047c81d3SSaso Kiselkov 				dmu_tx_commit(tx);
1859047c81d3SSaso Kiselkov 				error = dmu_free_long_range(zv->zv_objset,
1860047c81d3SSaso Kiselkov 				    ZVOL_OBJ, start, length);
1861047c81d3SSaso Kiselkov 			}
1862047c81d3SSaso Kiselkov 
186379315247SMatthew Ahrens 			rangelock_exit(lr);
1864047c81d3SSaso Kiselkov 
1865047c81d3SSaso Kiselkov 			if (error != 0)
1866047c81d3SSaso Kiselkov 				break;
1867047c81d3SSaso Kiselkov 		}
1868b77b9231SDan McDonald 
18691c9272b8SStephen Blinick 		/*
18701c9272b8SStephen Blinick 		 * If the write-cache is disabled, 'sync' property
18711c9272b8SStephen Blinick 		 * is set to 'always', or if the caller is asking for
18721c9272b8SStephen Blinick 		 * a synchronous free, commit this operation to the zil.
18731c9272b8SStephen Blinick 		 * This will sync any previous uncommitted writes to the
18741c9272b8SStephen Blinick 		 * zvol object.
18751c9272b8SStephen Blinick 		 * Can be overridden by the zvol_unmap_sync_enabled tunable.
18761c9272b8SStephen Blinick 		 */
18771c9272b8SStephen Blinick 		if ((error == 0) && zvol_unmap_sync_enabled &&
18781c9272b8SStephen Blinick 		    (!(zv->zv_flags & ZVOL_WCE) ||
18791c9272b8SStephen Blinick 		    (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS) ||
1880047c81d3SSaso Kiselkov 		    (dfl->dfl_flags & DF_WAIT_SYNC))) {
18811c9272b8SStephen Blinick 			zil_commit(zv->zv_zilog, ZVOL_OBJ);
1882b77b9231SDan McDonald 		}
18831c9272b8SStephen Blinick 
1884047c81d3SSaso Kiselkov 		if (!(flag & FKIOCTL))
1885047c81d3SSaso Kiselkov 			dfl_free(dfl);
1886047c81d3SSaso Kiselkov 
1887c3377ee9SJohn Levon 		smt_end_unsafe();
1888455e370cSJohn Levon 
1889574e2414SGeorge Wilson 		return (error);
1890b77b9231SDan McDonald 	}
1891b77b9231SDan McDonald 
1892fa9e4066Sahrens 	default:
1893be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENOTTY);
1894fa9e4066Sahrens 		break;
1895fa9e4066Sahrens 
1896fa9e4066Sahrens 	}
1897c99e4bdcSChris Kirby 	mutex_exit(&zfsdev_state_lock);
1898fa9e4066Sahrens 	return (error);
1899fa9e4066Sahrens }
1900fa9e4066Sahrens 
1901fa9e4066Sahrens int
1902fa9e4066Sahrens zvol_busy(void)
1903fa9e4066Sahrens {
1904fa9e4066Sahrens 	return (zvol_minors != 0);
1905fa9e4066Sahrens }
1906fa9e4066Sahrens 
1907fa9e4066Sahrens void
1908fa9e4066Sahrens zvol_init(void)
1909fa9e4066Sahrens {
1910c99e4bdcSChris Kirby 	VERIFY(ddi_soft_state_init(&zfsdev_state, sizeof (zfs_soft_state_t),
1911c99e4bdcSChris Kirby 	    1) == 0);
1912c99e4bdcSChris Kirby 	mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL);
1913fa9e4066Sahrens }
1914fa9e4066Sahrens 
1915fa9e4066Sahrens void
1916fa9e4066Sahrens zvol_fini(void)
1917fa9e4066Sahrens {
1918c99e4bdcSChris Kirby 	mutex_destroy(&zfsdev_state_lock);
1919c99e4bdcSChris Kirby 	ddi_soft_state_fini(&zfsdev_state);
1920fa9e4066Sahrens }
1921e7cbe64fSgw 
1922810e43b2SBill Pijewski /*ARGSUSED*/
1923810e43b2SBill Pijewski static int
1924810e43b2SBill Pijewski zfs_mvdev_dump_feature_check(void *arg, dmu_tx_t *tx)
1925810e43b2SBill Pijewski {
1926810e43b2SBill Pijewski 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
1927810e43b2SBill Pijewski 
19282acef22dSMatthew Ahrens 	if (spa_feature_is_active(spa, SPA_FEATURE_MULTI_VDEV_CRASH_DUMP))
1929810e43b2SBill Pijewski 		return (1);
1930810e43b2SBill Pijewski 	return (0);
1931810e43b2SBill Pijewski }
1932810e43b2SBill Pijewski 
1933810e43b2SBill Pijewski /*ARGSUSED*/
1934810e43b2SBill Pijewski static void
1935810e43b2SBill Pijewski zfs_mvdev_dump_activate_feature_sync(void *arg, dmu_tx_t *tx)
1936810e43b2SBill Pijewski {
1937810e43b2SBill Pijewski 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
1938810e43b2SBill Pijewski 
19392acef22dSMatthew Ahrens 	spa_feature_incr(spa, SPA_FEATURE_MULTI_VDEV_CRASH_DUMP, tx);
1940810e43b2SBill Pijewski }
1941810e43b2SBill Pijewski 
1942e7cbe64fSgw static int
1943e7cbe64fSgw zvol_dump_init(zvol_state_t *zv, boolean_t resize)
1944e7cbe64fSgw {
1945e7cbe64fSgw 	dmu_tx_t *tx;
1946810e43b2SBill Pijewski 	int error;
1947e7cbe64fSgw 	objset_t *os = zv->zv_objset;
1948810e43b2SBill Pijewski 	spa_t *spa = dmu_objset_spa(os);
1949810e43b2SBill Pijewski 	vdev_t *vd = spa->spa_root_vdev;
1950e7cbe64fSgw 	nvlist_t *nv = NULL;
1951810e43b2SBill Pijewski 	uint64_t version = spa_version(spa);
1952b10bba72SGeorge Wilson 	uint64_t checksum, compress, refresrv, vbs, dedup;
1953e7cbe64fSgw 
1954c99e4bdcSChris Kirby 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
1955810e43b2SBill Pijewski 	ASSERT(vd->vdev_ops == &vdev_root_ops);
1956810e43b2SBill Pijewski 
1957681d9761SEric Taylor 	error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0,
1958681d9761SEric Taylor 	    DMU_OBJECT_END);
1959b10bba72SGeorge Wilson 	if (error != 0)
1960b10bba72SGeorge Wilson 		return (error);
1961681d9761SEric Taylor 	/* wait for dmu_free_long_range to actually free the blocks */
1962681d9761SEric Taylor 	txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
1963e7cbe64fSgw 
1964810e43b2SBill Pijewski 	/*
1965810e43b2SBill Pijewski 	 * If the pool on which the dump device is being initialized has more
1966810e43b2SBill Pijewski 	 * than one child vdev, check that the MULTI_VDEV_CRASH_DUMP feature is
1967810e43b2SBill Pijewski 	 * enabled.  If so, bump that feature's counter to indicate that the
1968810e43b2SBill Pijewski 	 * feature is active. We also check the vdev type to handle the
1969810e43b2SBill Pijewski 	 * following case:
1970810e43b2SBill Pijewski 	 *   # zpool create test raidz disk1 disk2 disk3
1971810e43b2SBill Pijewski 	 *   Now have spa_root_vdev->vdev_children == 1 (the raidz vdev),
1972810e43b2SBill Pijewski 	 *   the raidz vdev itself has 3 children.
1973810e43b2SBill Pijewski 	 */
1974810e43b2SBill Pijewski 	if (vd->vdev_children > 1 || vd->vdev_ops == &vdev_raidz_ops) {
1975810e43b2SBill Pijewski 		if (!spa_feature_is_enabled(spa,
19762acef22dSMatthew Ahrens 		    SPA_FEATURE_MULTI_VDEV_CRASH_DUMP))
1977810e43b2SBill Pijewski 			return (SET_ERROR(ENOTSUP));
1978810e43b2SBill Pijewski 		(void) dsl_sync_task(spa_name(spa),
1979810e43b2SBill Pijewski 		    zfs_mvdev_dump_feature_check,
19807d46dc6cSMatthew Ahrens 		    zfs_mvdev_dump_activate_feature_sync, NULL,
19817d46dc6cSMatthew Ahrens 		    2, ZFS_SPACE_CHECK_RESERVED);
1982810e43b2SBill Pijewski 	}
1983810e43b2SBill Pijewski 
1984b10bba72SGeorge Wilson 	if (!resize) {
1985b10bba72SGeorge Wilson 		error = dsl_prop_get_integer(zv->zv_name,
1986b10bba72SGeorge Wilson 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL);
1987b10bba72SGeorge Wilson 		if (error == 0) {
1988b10bba72SGeorge Wilson 			error = dsl_prop_get_integer(zv->zv_name,
1989b10bba72SGeorge Wilson 			    zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum,
1990b10bba72SGeorge Wilson 			    NULL);
1991b10bba72SGeorge Wilson 		}
1992b10bba72SGeorge Wilson 		if (error == 0) {
1993b10bba72SGeorge Wilson 			error = dsl_prop_get_integer(zv->zv_name,
1994b10bba72SGeorge Wilson 			    zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
1995b10bba72SGeorge Wilson 			    &refresrv, NULL);
1996b10bba72SGeorge Wilson 		}
1997b10bba72SGeorge Wilson 		if (error == 0) {
1998b10bba72SGeorge Wilson 			error = dsl_prop_get_integer(zv->zv_name,
1999b10bba72SGeorge Wilson 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs,
2000b10bba72SGeorge Wilson 			    NULL);
2001b10bba72SGeorge Wilson 		}
2002b10bba72SGeorge Wilson 		if (version >= SPA_VERSION_DEDUP && error == 0) {
2003b10bba72SGeorge Wilson 			error = dsl_prop_get_integer(zv->zv_name,
2004b10bba72SGeorge Wilson 			    zfs_prop_to_name(ZFS_PROP_DEDUP), &dedup, NULL);
2005b10bba72SGeorge Wilson 		}
2006b10bba72SGeorge Wilson 	}
2007b10bba72SGeorge Wilson 	if (error != 0)
2008b10bba72SGeorge Wilson 		return (error);
2009b10bba72SGeorge Wilson 
2010e7cbe64fSgw 	tx = dmu_tx_create(os);
2011e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
2012681d9761SEric Taylor 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
2013e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
2014b10bba72SGeorge Wilson 	if (error != 0) {
2015e7cbe64fSgw 		dmu_tx_abort(tx);
2016e7cbe64fSgw 		return (error);
2017e7cbe64fSgw 	}
2018e7cbe64fSgw 
2019e7cbe64fSgw 	/*
2020e7cbe64fSgw 	 * If we are resizing the dump device then we only need to
2021e7cbe64fSgw 	 * update the refreservation to match the newly updated
2022e7cbe64fSgw 	 * zvolsize. Otherwise, we save off the original state of the
2023e7cbe64fSgw 	 * zvol so that we can restore them if the zvol is ever undumpified.
2024e7cbe64fSgw 	 */
2025e7cbe64fSgw 	if (resize) {
2026e7cbe64fSgw 		error = zap_update(os, ZVOL_ZAP_OBJ,
2027e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
2028e7cbe64fSgw 		    &zv->zv_volsize, tx);
2029e7cbe64fSgw 	} else {
2030b10bba72SGeorge Wilson 		error = zap_update(os, ZVOL_ZAP_OBJ,
2031e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1,
2032e7cbe64fSgw 		    &compress, tx);
2033b10bba72SGeorge Wilson 		if (error == 0) {
2034b10bba72SGeorge Wilson 			error = zap_update(os, ZVOL_ZAP_OBJ,
2035b10bba72SGeorge Wilson 			    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1,
2036b10bba72SGeorge Wilson 			    &checksum, tx);
2037b10bba72SGeorge Wilson 		}
2038b10bba72SGeorge Wilson 		if (error == 0) {
2039b10bba72SGeorge Wilson 			error = zap_update(os, ZVOL_ZAP_OBJ,
2040b10bba72SGeorge Wilson 			    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
2041b10bba72SGeorge Wilson 			    &refresrv, tx);
2042b10bba72SGeorge Wilson 		}
2043b10bba72SGeorge Wilson 		if (error == 0) {
2044b10bba72SGeorge Wilson 			error = zap_update(os, ZVOL_ZAP_OBJ,
2045b10bba72SGeorge Wilson 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1,
2046b10bba72SGeorge Wilson 			    &vbs, tx);
2047b10bba72SGeorge Wilson 		}
2048b10bba72SGeorge Wilson 		if (error == 0) {
2049b10bba72SGeorge Wilson 			error = dmu_object_set_blocksize(
2050b10bba72SGeorge Wilson 			    os, ZVOL_OBJ, SPA_OLD_MAXBLOCKSIZE, 0, tx);
2051b10bba72SGeorge Wilson 		}
2052b10bba72SGeorge Wilson 		if (version >= SPA_VERSION_DEDUP && error == 0) {
2053b10bba72SGeorge Wilson 			error = zap_update(os, ZVOL_ZAP_OBJ,
20548d265e66SGeorge Wilson 			    zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1,
20558d265e66SGeorge Wilson 			    &dedup, tx);
20568d265e66SGeorge Wilson 		}
2057681d9761SEric Taylor 		if (error == 0)
2058b5152584SMatthew Ahrens 			zv->zv_volblocksize = SPA_OLD_MAXBLOCKSIZE;
2059e7cbe64fSgw 	}
2060e7cbe64fSgw 	dmu_tx_commit(tx);
2061e7cbe64fSgw 
2062e7cbe64fSgw 	/*
2063e7cbe64fSgw 	 * We only need update the zvol's property if we are initializing
2064e7cbe64fSgw 	 * the dump area for the first time.
2065e7cbe64fSgw 	 */
2066b10bba72SGeorge Wilson 	if (error == 0 && !resize) {
2067b10bba72SGeorge Wilson 		/*
2068b10bba72SGeorge Wilson 		 * If MULTI_VDEV_CRASH_DUMP is active, use the NOPARITY checksum
2069b10bba72SGeorge Wilson 		 * function.  Otherwise, use the old default -- OFF.
2070b10bba72SGeorge Wilson 		 */
2071b10bba72SGeorge Wilson 		checksum = spa_feature_is_active(spa,
2072b10bba72SGeorge Wilson 		    SPA_FEATURE_MULTI_VDEV_CRASH_DUMP) ? ZIO_CHECKSUM_NOPARITY :
2073b10bba72SGeorge Wilson 		    ZIO_CHECKSUM_OFF;
2074b10bba72SGeorge Wilson 
2075e7cbe64fSgw 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2076e7cbe64fSgw 		VERIFY(nvlist_add_uint64(nv,
2077e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0);
2078e7cbe64fSgw 		VERIFY(nvlist_add_uint64(nv,
2079e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
2080e7cbe64fSgw 		    ZIO_COMPRESS_OFF) == 0);
2081e7cbe64fSgw 		VERIFY(nvlist_add_uint64(nv,
2082e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
2083810e43b2SBill Pijewski 		    checksum) == 0);
20848d265e66SGeorge Wilson 		if (version >= SPA_VERSION_DEDUP) {
20858d265e66SGeorge Wilson 			VERIFY(nvlist_add_uint64(nv,
20868d265e66SGeorge Wilson 			    zfs_prop_to_name(ZFS_PROP_DEDUP),
20878d265e66SGeorge Wilson 			    ZIO_CHECKSUM_OFF) == 0);
20888d265e66SGeorge Wilson 		}
2089e7cbe64fSgw 
209092241e0bSTom Erickson 		error = zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
209192241e0bSTom Erickson 		    nv, NULL);
2092e7cbe64fSgw 		nvlist_free(nv);
2093e7cbe64fSgw 	}
2094e7cbe64fSgw 
2095e7cbe64fSgw 	/* Allocate the space for the dump */
2096b10bba72SGeorge Wilson 	if (error == 0)
2097b10bba72SGeorge Wilson 		error = zvol_prealloc(zv);
2098e7cbe64fSgw 	return (error);
2099e7cbe64fSgw }
2100e7cbe64fSgw 
2101e7cbe64fSgw static int
2102e7cbe64fSgw zvol_dumpify(zvol_state_t *zv)
2103e7cbe64fSgw {
2104e7cbe64fSgw 	int error = 0;
2105e7cbe64fSgw 	uint64_t dumpsize = 0;
2106e7cbe64fSgw 	dmu_tx_t *tx;
2107e7cbe64fSgw 	objset_t *os = zv->zv_objset;
2108e7cbe64fSgw 
2109681d9761SEric Taylor 	if (zv->zv_flags & ZVOL_RDONLY)
2110be6fd75aSMatthew Ahrens 		return (SET_ERROR(EROFS));
2111e7cbe64fSgw 
2112*eb633035STom Caputi 	if (os->os_encrypted)
2113*eb633035STom Caputi 		return (SET_ERROR(ENOTSUP));
2114*eb633035STom Caputi 
2115e7cbe64fSgw 	if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE,
2116e7cbe64fSgw 	    8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) {
21174445fffbSMatthew Ahrens 		boolean_t resize = (dumpsize > 0);
2118e7cbe64fSgw 
2119e7cbe64fSgw 		if ((error = zvol_dump_init(zv, resize)) != 0) {
2120e7cbe64fSgw 			(void) zvol_dump_fini(zv);
2121e7cbe64fSgw 			return (error);
2122e7cbe64fSgw 		}
2123e7cbe64fSgw 	}
2124e7cbe64fSgw 
2125e7cbe64fSgw 	/*
2126e7cbe64fSgw 	 * Build up our lba mapping.
2127e7cbe64fSgw 	 */
2128e7cbe64fSgw 	error = zvol_get_lbas(zv);
2129e7cbe64fSgw 	if (error) {
2130e7cbe64fSgw 		(void) zvol_dump_fini(zv);
2131e7cbe64fSgw 		return (error);
2132e7cbe64fSgw 	}
2133e7cbe64fSgw 
2134e7cbe64fSgw 	tx = dmu_tx_create(os);
2135e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
2136e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
2137e7cbe64fSgw 	if (error) {
2138e7cbe64fSgw 		dmu_tx_abort(tx);
2139e7cbe64fSgw 		(void) zvol_dump_fini(zv);
2140e7cbe64fSgw 		return (error);
2141e7cbe64fSgw 	}
2142e7cbe64fSgw 
2143e7cbe64fSgw 	zv->zv_flags |= ZVOL_DUMPIFIED;
2144e7cbe64fSgw 	error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1,
2145e7cbe64fSgw 	    &zv->zv_volsize, tx);
2146e7cbe64fSgw 	dmu_tx_commit(tx);
2147e7cbe64fSgw 
2148e7cbe64fSgw 	if (error) {
2149e7cbe64fSgw 		(void) zvol_dump_fini(zv);
2150e7cbe64fSgw 		return (error);
2151e7cbe64fSgw 	}
2152e7cbe64fSgw 
2153e7cbe64fSgw 	txg_wait_synced(dmu_objset_pool(os), 0);
2154e7cbe64fSgw 	return (0);
2155e7cbe64fSgw }
2156e7cbe64fSgw 
2157e7cbe64fSgw static int
2158e7cbe64fSgw zvol_dump_fini(zvol_state_t *zv)
2159e7cbe64fSgw {
2160e7cbe64fSgw 	dmu_tx_t *tx;
2161e7cbe64fSgw 	objset_t *os = zv->zv_objset;
2162e7cbe64fSgw 	nvlist_t *nv;
2163e7cbe64fSgw 	int error = 0;
2164afee20e4SGeorge Wilson 	uint64_t checksum, compress, refresrv, vbs, dedup;
21658d265e66SGeorge Wilson 	uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset));
2166e7cbe64fSgw 
2167b7e50089Smaybee 	/*
2168b7e50089Smaybee 	 * Attempt to restore the zvol back to its pre-dumpified state.
2169b7e50089Smaybee 	 * This is a best-effort attempt as it's possible that not all
2170b7e50089Smaybee 	 * of these properties were initialized during the dumpify process
2171b7e50089Smaybee 	 * (i.e. error during zvol_dump_init).
2172b7e50089Smaybee 	 */
2173b7e50089Smaybee 
2174e7cbe64fSgw 	tx = dmu_tx_create(os);
2175e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
2176e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
2177e7cbe64fSgw 	if (error) {
2178e7cbe64fSgw 		dmu_tx_abort(tx);
2179e7cbe64fSgw 		return (error);
2180e7cbe64fSgw 	}
2181b7e50089Smaybee 	(void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx);
2182b7e50089Smaybee 	dmu_tx_commit(tx);
2183e7cbe64fSgw 
2184e7cbe64fSgw 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2185e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum);
2186e7cbe64fSgw 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2187e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress);
2188e7cbe64fSgw 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2189e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv);
219088b7b0f2SMatthew Ahrens 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
219188b7b0f2SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs);
2192e7cbe64fSgw 
2193e7cbe64fSgw 	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2194e7cbe64fSgw 	(void) nvlist_add_uint64(nv,
2195e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum);
2196e7cbe64fSgw 	(void) nvlist_add_uint64(nv,
2197e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress);
2198e7cbe64fSgw 	(void) nvlist_add_uint64(nv,
2199e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv);
22008d265e66SGeorge Wilson 	if (version >= SPA_VERSION_DEDUP &&
22018d265e66SGeorge Wilson 	    zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
22028d265e66SGeorge Wilson 	    zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, &dedup) == 0) {
22038d265e66SGeorge Wilson 		(void) nvlist_add_uint64(nv,
22048d265e66SGeorge Wilson 		    zfs_prop_to_name(ZFS_PROP_DEDUP), dedup);
22058d265e66SGeorge Wilson 	}
220692241e0bSTom Erickson 	(void) zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
220792241e0bSTom Erickson 	    nv, NULL);
2208e7cbe64fSgw 	nvlist_free(nv);
2209e7cbe64fSgw 
2210b7e50089Smaybee 	zvol_free_extents(zv);
2211b7e50089Smaybee 	zv->zv_flags &= ~ZVOL_DUMPIFIED;
2212b7e50089Smaybee 	(void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END);
2213681d9761SEric Taylor 	/* wait for dmu_free_long_range to actually free the blocks */
2214681d9761SEric Taylor 	txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
2215681d9761SEric Taylor 	tx = dmu_tx_create(os);
2216681d9761SEric Taylor 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
2217681d9761SEric Taylor 	error = dmu_tx_assign(tx, TXG_WAIT);
2218681d9761SEric Taylor 	if (error) {
2219681d9761SEric Taylor 		dmu_tx_abort(tx);
2220681d9761SEric Taylor 		return (error);
2221681d9761SEric Taylor 	}
2222b24ab676SJeff Bonwick 	if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0)
2223b24ab676SJeff Bonwick 		zv->zv_volblocksize = vbs;
2224681d9761SEric Taylor 	dmu_tx_commit(tx);
2225b7e50089Smaybee 
2226e7cbe64fSgw 	return (0);
2227e7cbe64fSgw }
2228