xref: /illumos-gate/usr/src/uts/common/fs/zfs/zvol.c (revision b24ab676)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
22e08bf2c6SEric Taylor  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens /*
27fa9e4066Sahrens  * ZFS volume emulation driver.
28fa9e4066Sahrens  *
29fa9e4066Sahrens  * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes.
30fa9e4066Sahrens  * Volumes are accessed through the symbolic links named:
31fa9e4066Sahrens  *
32fa9e4066Sahrens  * /dev/zvol/dsk/<pool_name>/<dataset_name>
33fa9e4066Sahrens  * /dev/zvol/rdsk/<pool_name>/<dataset_name>
34fa9e4066Sahrens  *
35681d9761SEric Taylor  * These links are created by the /dev filesystem (sdev_zvolops.c).
36fa9e4066Sahrens  * Volumes are persistent through reboot.  No user command needs to be
37fa9e4066Sahrens  * run before opening and using a device.
38fa9e4066Sahrens  */
39fa9e4066Sahrens 
40fa9e4066Sahrens #include <sys/types.h>
41fa9e4066Sahrens #include <sys/param.h>
42fa9e4066Sahrens #include <sys/errno.h>
43fa9e4066Sahrens #include <sys/uio.h>
44fa9e4066Sahrens #include <sys/buf.h>
45fa9e4066Sahrens #include <sys/modctl.h>
46fa9e4066Sahrens #include <sys/open.h>
47fa9e4066Sahrens #include <sys/kmem.h>
48fa9e4066Sahrens #include <sys/conf.h>
49fa9e4066Sahrens #include <sys/cmn_err.h>
50fa9e4066Sahrens #include <sys/stat.h>
51fa9e4066Sahrens #include <sys/zap.h>
52fa9e4066Sahrens #include <sys/spa.h>
53fa9e4066Sahrens #include <sys/zio.h>
54e7cbe64fSgw #include <sys/dmu_traverse.h>
55e7cbe64fSgw #include <sys/dnode.h>
56e7cbe64fSgw #include <sys/dsl_dataset.h>
57fa9e4066Sahrens #include <sys/dsl_prop.h>
58fa9e4066Sahrens #include <sys/dkio.h>
59fa9e4066Sahrens #include <sys/efi_partition.h>
60fa9e4066Sahrens #include <sys/byteorder.h>
61fa9e4066Sahrens #include <sys/pathname.h>
62fa9e4066Sahrens #include <sys/ddi.h>
63fa9e4066Sahrens #include <sys/sunddi.h>
64fa9e4066Sahrens #include <sys/crc32.h>
65fa9e4066Sahrens #include <sys/dirent.h>
66fa9e4066Sahrens #include <sys/policy.h>
67fa9e4066Sahrens #include <sys/fs/zfs.h>
68fa9e4066Sahrens #include <sys/zfs_ioctl.h>
69fa9e4066Sahrens #include <sys/mkdev.h>
7022ac5be4Sperrin #include <sys/zil.h>
71c5c6ffa0Smaybee #include <sys/refcount.h>
72c2e6a7d6Sperrin #include <sys/zfs_znode.h>
73c2e6a7d6Sperrin #include <sys/zfs_rlock.h>
74e7cbe64fSgw #include <sys/vdev_disk.h>
75e7cbe64fSgw #include <sys/vdev_impl.h>
76e7cbe64fSgw #include <sys/zvol.h>
77e7cbe64fSgw #include <sys/dumphdr.h>
781209a471SNeil Perrin #include <sys/zil_impl.h>
79fa9e4066Sahrens 
80fa9e4066Sahrens #include "zfs_namecheck.h"
81fa9e4066Sahrens 
82fa9e4066Sahrens static void *zvol_state;
83503ad85cSMatthew Ahrens static char *zvol_tag = "zvol_tag";
84fa9e4066Sahrens 
85e7cbe64fSgw #define	ZVOL_DUMPSIZE		"dumpsize"
86e7cbe64fSgw 
87fa9e4066Sahrens /*
88fa9e4066Sahrens  * This lock protects the zvol_state structure from being modified
89fa9e4066Sahrens  * while it's being used, e.g. an open that comes in before a create
90fa9e4066Sahrens  * finishes.  It also protects temporary opens of the dataset so that,
91fa9e4066Sahrens  * e.g., an open doesn't get a spurious EBUSY.
92fa9e4066Sahrens  */
93fa9e4066Sahrens static kmutex_t zvol_state_lock;
94fa9e4066Sahrens static uint32_t zvol_minors;
95fa9e4066Sahrens 
96e7cbe64fSgw typedef struct zvol_extent {
9788b7b0f2SMatthew Ahrens 	list_node_t	ze_node;
98e7cbe64fSgw 	dva_t		ze_dva;		/* dva associated with this extent */
9988b7b0f2SMatthew Ahrens 	uint64_t	ze_nblks;	/* number of blocks in extent */
100e7cbe64fSgw } zvol_extent_t;
101e7cbe64fSgw 
102fa9e4066Sahrens /*
103fa9e4066Sahrens  * The in-core state of each volume.
104fa9e4066Sahrens  */
105fa9e4066Sahrens typedef struct zvol_state {
106fa9e4066Sahrens 	char		zv_name[MAXPATHLEN]; /* pool/dd name */
107fa9e4066Sahrens 	uint64_t	zv_volsize;	/* amount of space we advertise */
10867bd71c6Sperrin 	uint64_t	zv_volblocksize; /* volume block size */
109fa9e4066Sahrens 	minor_t		zv_minor;	/* minor number */
110fa9e4066Sahrens 	uint8_t		zv_min_bs;	/* minimum addressable block shift */
111701f66c4SEric Taylor 	uint8_t		zv_flags;	/* readonly, dumpified, etc. */
112fa9e4066Sahrens 	objset_t	*zv_objset;	/* objset handle */
113fa9e4066Sahrens 	uint32_t	zv_open_count[OTYPCNT];	/* open counts */
114fa9e4066Sahrens 	uint32_t	zv_total_opens;	/* total open count */
11522ac5be4Sperrin 	zilog_t		*zv_zilog;	/* ZIL handle */
11688b7b0f2SMatthew Ahrens 	list_t		zv_extents;	/* List of extents for dump */
117c2e6a7d6Sperrin 	znode_t		zv_znode;	/* for range locking */
118fa9e4066Sahrens } zvol_state_t;
119fa9e4066Sahrens 
120e7cbe64fSgw /*
121e7cbe64fSgw  * zvol specific flags
122e7cbe64fSgw  */
123e7cbe64fSgw #define	ZVOL_RDONLY	0x1
124e7cbe64fSgw #define	ZVOL_DUMPIFIED	0x2
125c7f714e2SEric Taylor #define	ZVOL_EXCL	0x4
126701f66c4SEric Taylor #define	ZVOL_WCE	0x8
127e7cbe64fSgw 
12867bd71c6Sperrin /*
12967bd71c6Sperrin  * zvol maximum transfer in one DMU tx.
13067bd71c6Sperrin  */
13167bd71c6Sperrin int zvol_maxphys = DMU_MAX_ACCESS/2;
13267bd71c6Sperrin 
133681d9761SEric Taylor static int zvol_remove_zv(zvol_state_t *);
134e7cbe64fSgw extern int zfs_set_prop_nvlist(const char *, nvlist_t *);
135feb08c6bSbillm static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio);
136e7cbe64fSgw static int zvol_dumpify(zvol_state_t *zv);
137e7cbe64fSgw static int zvol_dump_fini(zvol_state_t *zv);
138e7cbe64fSgw static int zvol_dump_init(zvol_state_t *zv, boolean_t resize);
13967bd71c6Sperrin 
140fa9e4066Sahrens static void
141681d9761SEric Taylor zvol_size_changed(uint64_t volsize, major_t maj, minor_t min)
142fa9e4066Sahrens {
143681d9761SEric Taylor 	dev_t dev = makedevice(maj, min);
144fa9e4066Sahrens 
145fa9e4066Sahrens 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
146681d9761SEric Taylor 	    "Size", volsize) == DDI_SUCCESS);
147fa9e4066Sahrens 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
148681d9761SEric Taylor 	    "Nblocks", lbtodb(volsize)) == DDI_SUCCESS);
149e7cbe64fSgw 
150e7cbe64fSgw 	/* Notify specfs to invalidate the cached size */
151e7cbe64fSgw 	spec_size_invalidate(dev, VBLK);
152e7cbe64fSgw 	spec_size_invalidate(dev, VCHR);
153fa9e4066Sahrens }
154fa9e4066Sahrens 
155fa9e4066Sahrens int
156e9dbad6fSeschrock zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
157fa9e4066Sahrens {
158e9dbad6fSeschrock 	if (volsize == 0)
159fa9e4066Sahrens 		return (EINVAL);
160fa9e4066Sahrens 
161e9dbad6fSeschrock 	if (volsize % blocksize != 0)
1625c5460e9Seschrock 		return (EINVAL);
1635c5460e9Seschrock 
164fa9e4066Sahrens #ifdef _ILP32
165e9dbad6fSeschrock 	if (volsize - 1 > SPEC_MAXOFFSET_T)
166fa9e4066Sahrens 		return (EOVERFLOW);
167fa9e4066Sahrens #endif
168fa9e4066Sahrens 	return (0);
169fa9e4066Sahrens }
170fa9e4066Sahrens 
171fa9e4066Sahrens int
172e9dbad6fSeschrock zvol_check_volblocksize(uint64_t volblocksize)
173fa9e4066Sahrens {
174e9dbad6fSeschrock 	if (volblocksize < SPA_MINBLOCKSIZE ||
175e9dbad6fSeschrock 	    volblocksize > SPA_MAXBLOCKSIZE ||
176e9dbad6fSeschrock 	    !ISP2(volblocksize))
177fa9e4066Sahrens 		return (EDOM);
178fa9e4066Sahrens 
179fa9e4066Sahrens 	return (0);
180fa9e4066Sahrens }
181fa9e4066Sahrens 
182fa9e4066Sahrens int
183a2eea2e1Sahrens zvol_get_stats(objset_t *os, nvlist_t *nv)
184fa9e4066Sahrens {
185fa9e4066Sahrens 	int error;
186fa9e4066Sahrens 	dmu_object_info_t doi;
187a2eea2e1Sahrens 	uint64_t val;
188fa9e4066Sahrens 
189a2eea2e1Sahrens 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
190fa9e4066Sahrens 	if (error)
191fa9e4066Sahrens 		return (error);
192fa9e4066Sahrens 
193a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
194a2eea2e1Sahrens 
195fa9e4066Sahrens 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
196fa9e4066Sahrens 
197a2eea2e1Sahrens 	if (error == 0) {
198a2eea2e1Sahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
199a2eea2e1Sahrens 		    doi.doi_data_block_size);
200a2eea2e1Sahrens 	}
201fa9e4066Sahrens 
202fa9e4066Sahrens 	return (error);
203fa9e4066Sahrens }
204fa9e4066Sahrens 
205fa9e4066Sahrens /*
206fa9e4066Sahrens  * Find a free minor number.
207fa9e4066Sahrens  */
208fa9e4066Sahrens static minor_t
209fa9e4066Sahrens zvol_minor_alloc(void)
210fa9e4066Sahrens {
211fa9e4066Sahrens 	minor_t minor;
212fa9e4066Sahrens 
213fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&zvol_state_lock));
214fa9e4066Sahrens 
215fa9e4066Sahrens 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++)
216fa9e4066Sahrens 		if (ddi_get_soft_state(zvol_state, minor) == NULL)
217fa9e4066Sahrens 			return (minor);
218fa9e4066Sahrens 
219fa9e4066Sahrens 	return (0);
220fa9e4066Sahrens }
221fa9e4066Sahrens 
222fa9e4066Sahrens static zvol_state_t *
223e9dbad6fSeschrock zvol_minor_lookup(const char *name)
224fa9e4066Sahrens {
225fa9e4066Sahrens 	minor_t minor;
226fa9e4066Sahrens 	zvol_state_t *zv;
227fa9e4066Sahrens 
228fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&zvol_state_lock));
229fa9e4066Sahrens 
230fa9e4066Sahrens 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) {
231fa9e4066Sahrens 		zv = ddi_get_soft_state(zvol_state, minor);
232fa9e4066Sahrens 		if (zv == NULL)
233fa9e4066Sahrens 			continue;
234fa9e4066Sahrens 		if (strcmp(zv->zv_name, name) == 0)
235fa9e4066Sahrens 			break;
236fa9e4066Sahrens 	}
237fa9e4066Sahrens 
238fa9e4066Sahrens 	return (zv);
239fa9e4066Sahrens }
240fa9e4066Sahrens 
241e7cbe64fSgw /* extent mapping arg */
242e7cbe64fSgw struct maparg {
24388b7b0f2SMatthew Ahrens 	zvol_state_t	*ma_zv;
24488b7b0f2SMatthew Ahrens 	uint64_t	ma_blks;
245e7cbe64fSgw };
246e7cbe64fSgw 
247e7cbe64fSgw /*ARGSUSED*/
248e7cbe64fSgw static int
249*b24ab676SJeff Bonwick zvol_map_block(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
250*b24ab676SJeff Bonwick     const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
251e7cbe64fSgw {
25288b7b0f2SMatthew Ahrens 	struct maparg *ma = arg;
25388b7b0f2SMatthew Ahrens 	zvol_extent_t *ze;
25488b7b0f2SMatthew Ahrens 	int bs = ma->ma_zv->zv_volblocksize;
255e7cbe64fSgw 
25688b7b0f2SMatthew Ahrens 	if (bp == NULL || zb->zb_object != ZVOL_OBJ || zb->zb_level != 0)
25788b7b0f2SMatthew Ahrens 		return (0);
258e7cbe64fSgw 
25988b7b0f2SMatthew Ahrens 	VERIFY3U(ma->ma_blks, ==, zb->zb_blkid);
26088b7b0f2SMatthew Ahrens 	ma->ma_blks++;
261e7cbe64fSgw 
26288b7b0f2SMatthew Ahrens 	/* Abort immediately if we have encountered gang blocks */
26388b7b0f2SMatthew Ahrens 	if (BP_IS_GANG(bp))
26488b7b0f2SMatthew Ahrens 		return (EFRAGS);
265e7cbe64fSgw 
26688b7b0f2SMatthew Ahrens 	/*
26788b7b0f2SMatthew Ahrens 	 * See if the block is at the end of the previous extent.
26888b7b0f2SMatthew Ahrens 	 */
26988b7b0f2SMatthew Ahrens 	ze = list_tail(&ma->ma_zv->zv_extents);
27088b7b0f2SMatthew Ahrens 	if (ze &&
27188b7b0f2SMatthew Ahrens 	    DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) &&
27288b7b0f2SMatthew Ahrens 	    DVA_GET_OFFSET(BP_IDENTITY(bp)) ==
27388b7b0f2SMatthew Ahrens 	    DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) {
27488b7b0f2SMatthew Ahrens 		ze->ze_nblks++;
27588b7b0f2SMatthew Ahrens 		return (0);
276e7cbe64fSgw 	}
277e7cbe64fSgw 
27888b7b0f2SMatthew Ahrens 	dprintf_bp(bp, "%s", "next blkptr:");
279e7cbe64fSgw 
28088b7b0f2SMatthew Ahrens 	/* start a new extent */
28188b7b0f2SMatthew Ahrens 	ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP);
28288b7b0f2SMatthew Ahrens 	ze->ze_dva = bp->blk_dva[0];	/* structure assignment */
28388b7b0f2SMatthew Ahrens 	ze->ze_nblks = 1;
28488b7b0f2SMatthew Ahrens 	list_insert_tail(&ma->ma_zv->zv_extents, ze);
28588b7b0f2SMatthew Ahrens 	return (0);
28688b7b0f2SMatthew Ahrens }
287e7cbe64fSgw 
28888b7b0f2SMatthew Ahrens static void
28988b7b0f2SMatthew Ahrens zvol_free_extents(zvol_state_t *zv)
29088b7b0f2SMatthew Ahrens {
29188b7b0f2SMatthew Ahrens 	zvol_extent_t *ze;
292e7cbe64fSgw 
29388b7b0f2SMatthew Ahrens 	while (ze = list_head(&zv->zv_extents)) {
29488b7b0f2SMatthew Ahrens 		list_remove(&zv->zv_extents, ze);
29588b7b0f2SMatthew Ahrens 		kmem_free(ze, sizeof (zvol_extent_t));
296e7cbe64fSgw 	}
29788b7b0f2SMatthew Ahrens }
298e7cbe64fSgw 
29988b7b0f2SMatthew Ahrens static int
30088b7b0f2SMatthew Ahrens zvol_get_lbas(zvol_state_t *zv)
30188b7b0f2SMatthew Ahrens {
30288b7b0f2SMatthew Ahrens 	struct maparg	ma;
30388b7b0f2SMatthew Ahrens 	int		err;
30488b7b0f2SMatthew Ahrens 
30588b7b0f2SMatthew Ahrens 	ma.ma_zv = zv;
30688b7b0f2SMatthew Ahrens 	ma.ma_blks = 0;
30788b7b0f2SMatthew Ahrens 	zvol_free_extents(zv);
30888b7b0f2SMatthew Ahrens 
30988b7b0f2SMatthew Ahrens 	err = traverse_dataset(dmu_objset_ds(zv->zv_objset), 0,
31088b7b0f2SMatthew Ahrens 	    TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma);
31188b7b0f2SMatthew Ahrens 	if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) {
31288b7b0f2SMatthew Ahrens 		zvol_free_extents(zv);
31388b7b0f2SMatthew Ahrens 		return (err ? err : EIO);
314e7cbe64fSgw 	}
31588b7b0f2SMatthew Ahrens 
316e7cbe64fSgw 	return (0);
317e7cbe64fSgw }
318e7cbe64fSgw 
319ecd6cf80Smarks /* ARGSUSED */
320fa9e4066Sahrens void
321ecd6cf80Smarks zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
322fa9e4066Sahrens {
323da6c28aaSamw 	zfs_creat_t *zct = arg;
324da6c28aaSamw 	nvlist_t *nvprops = zct->zct_props;
325fa9e4066Sahrens 	int error;
326e9dbad6fSeschrock 	uint64_t volblocksize, volsize;
327fa9e4066Sahrens 
328ecd6cf80Smarks 	VERIFY(nvlist_lookup_uint64(nvprops,
329e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
330ecd6cf80Smarks 	if (nvlist_lookup_uint64(nvprops,
331e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
332e9dbad6fSeschrock 		volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
333e9dbad6fSeschrock 
334e9dbad6fSeschrock 	/*
335e7cbe64fSgw 	 * These properties must be removed from the list so the generic
336e9dbad6fSeschrock 	 * property setting step won't apply to them.
337e9dbad6fSeschrock 	 */
338ecd6cf80Smarks 	VERIFY(nvlist_remove_all(nvprops,
339e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
340ecd6cf80Smarks 	(void) nvlist_remove_all(nvprops,
341e9dbad6fSeschrock 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
342e9dbad6fSeschrock 
343e9dbad6fSeschrock 	error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
344fa9e4066Sahrens 	    DMU_OT_NONE, 0, tx);
345fa9e4066Sahrens 	ASSERT(error == 0);
346fa9e4066Sahrens 
347fa9e4066Sahrens 	error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
348fa9e4066Sahrens 	    DMU_OT_NONE, 0, tx);
349fa9e4066Sahrens 	ASSERT(error == 0);
350fa9e4066Sahrens 
351e9dbad6fSeschrock 	error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
352fa9e4066Sahrens 	ASSERT(error == 0);
353fa9e4066Sahrens }
354fa9e4066Sahrens 
35522ac5be4Sperrin /*
35622ac5be4Sperrin  * Replay a TX_WRITE ZIL transaction that didn't get committed
35722ac5be4Sperrin  * after a system failure
35822ac5be4Sperrin  */
35922ac5be4Sperrin static int
36022ac5be4Sperrin zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
36122ac5be4Sperrin {
36222ac5be4Sperrin 	objset_t *os = zv->zv_objset;
36322ac5be4Sperrin 	char *data = (char *)(lr + 1);	/* data follows lr_write_t */
364*b24ab676SJeff Bonwick 	uint64_t offset, length;
36522ac5be4Sperrin 	dmu_tx_t *tx;
36622ac5be4Sperrin 	int error;
36722ac5be4Sperrin 
36822ac5be4Sperrin 	if (byteswap)
36922ac5be4Sperrin 		byteswap_uint64_array(lr, sizeof (*lr));
37022ac5be4Sperrin 
371*b24ab676SJeff Bonwick 	offset = lr->lr_offset;
372*b24ab676SJeff Bonwick 	length = lr->lr_length;
373*b24ab676SJeff Bonwick 
374*b24ab676SJeff Bonwick 	/* If it's a dmu_sync() block, write the whole block */
375*b24ab676SJeff Bonwick 	if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
376*b24ab676SJeff Bonwick 		uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
377*b24ab676SJeff Bonwick 		if (length < blocksize) {
378*b24ab676SJeff Bonwick 			offset -= offset % blocksize;
379*b24ab676SJeff Bonwick 			length = blocksize;
380*b24ab676SJeff Bonwick 		}
381*b24ab676SJeff Bonwick 	}
382975c32a0SNeil Perrin 
38322ac5be4Sperrin 	tx = dmu_tx_create(os);
384*b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length);
3851209a471SNeil Perrin 	error = dmu_tx_assign(tx, TXG_WAIT);
38622ac5be4Sperrin 	if (error) {
38722ac5be4Sperrin 		dmu_tx_abort(tx);
38822ac5be4Sperrin 	} else {
389*b24ab676SJeff Bonwick 		dmu_write(os, ZVOL_OBJ, offset, length, data, tx);
39022ac5be4Sperrin 		dmu_tx_commit(tx);
39122ac5be4Sperrin 	}
39222ac5be4Sperrin 
39322ac5be4Sperrin 	return (error);
39422ac5be4Sperrin }
39522ac5be4Sperrin 
39622ac5be4Sperrin /* ARGSUSED */
39722ac5be4Sperrin static int
39822ac5be4Sperrin zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
39922ac5be4Sperrin {
40022ac5be4Sperrin 	return (ENOTSUP);
40122ac5be4Sperrin }
40222ac5be4Sperrin 
40322ac5be4Sperrin /*
40422ac5be4Sperrin  * Callback vectors for replaying records.
40522ac5be4Sperrin  * Only TX_WRITE is needed for zvol.
40622ac5be4Sperrin  */
40722ac5be4Sperrin zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = {
40822ac5be4Sperrin 	zvol_replay_err,	/* 0 no such transaction type */
40922ac5be4Sperrin 	zvol_replay_err,	/* TX_CREATE */
41022ac5be4Sperrin 	zvol_replay_err,	/* TX_MKDIR */
41122ac5be4Sperrin 	zvol_replay_err,	/* TX_MKXATTR */
41222ac5be4Sperrin 	zvol_replay_err,	/* TX_SYMLINK */
41322ac5be4Sperrin 	zvol_replay_err,	/* TX_REMOVE */
41422ac5be4Sperrin 	zvol_replay_err,	/* TX_RMDIR */
41522ac5be4Sperrin 	zvol_replay_err,	/* TX_LINK */
41622ac5be4Sperrin 	zvol_replay_err,	/* TX_RENAME */
41722ac5be4Sperrin 	zvol_replay_write,	/* TX_WRITE */
41822ac5be4Sperrin 	zvol_replay_err,	/* TX_TRUNCATE */
41922ac5be4Sperrin 	zvol_replay_err,	/* TX_SETATTR */
42022ac5be4Sperrin 	zvol_replay_err,	/* TX_ACL */
421975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_CREATE_ACL */
422975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_CREATE_ATTR */
423975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_CREATE_ACL_ATTR */
424975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_MKDIR_ACL */
425975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_MKDIR_ATTR */
426975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_MKDIR_ACL_ATTR */
427975c32a0SNeil Perrin 	zvol_replay_err,	/* TX_WRITE2 */
42822ac5be4Sperrin };
42922ac5be4Sperrin 
430681d9761SEric Taylor int
431681d9761SEric Taylor zvol_name2minor(const char *name, minor_t *minor)
432681d9761SEric Taylor {
433681d9761SEric Taylor 	zvol_state_t *zv;
434681d9761SEric Taylor 
435681d9761SEric Taylor 	mutex_enter(&zvol_state_lock);
436681d9761SEric Taylor 	zv = zvol_minor_lookup(name);
437681d9761SEric Taylor 	if (minor && zv)
438681d9761SEric Taylor 		*minor = zv->zv_minor;
439681d9761SEric Taylor 	mutex_exit(&zvol_state_lock);
440681d9761SEric Taylor 	return (zv ? 0 : -1);
441681d9761SEric Taylor }
442681d9761SEric Taylor 
443e7cbe64fSgw /*
444e7cbe64fSgw  * Create a minor node (plus a whole lot more) for the specified volume.
445fa9e4066Sahrens  */
446fa9e4066Sahrens int
447681d9761SEric Taylor zvol_create_minor(const char *name)
448fa9e4066Sahrens {
449fa9e4066Sahrens 	zvol_state_t *zv;
450fa9e4066Sahrens 	objset_t *os;
45167bd71c6Sperrin 	dmu_object_info_t doi;
452fa9e4066Sahrens 	minor_t minor = 0;
453fa9e4066Sahrens 	char chrbuf[30], blkbuf[30];
454fa9e4066Sahrens 	int error;
455fa9e4066Sahrens 
456fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
457fa9e4066Sahrens 
458fa9e4066Sahrens 	if ((zv = zvol_minor_lookup(name)) != NULL) {
459fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
460fa9e4066Sahrens 		return (EEXIST);
461fa9e4066Sahrens 	}
462fa9e4066Sahrens 
463503ad85cSMatthew Ahrens 	/* lie and say we're read-only */
464503ad85cSMatthew Ahrens 	error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, zvol_tag, &os);
465fa9e4066Sahrens 
466fa9e4066Sahrens 	if (error) {
467fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
468fa9e4066Sahrens 		return (error);
469fa9e4066Sahrens 	}
470fa9e4066Sahrens 
471681d9761SEric Taylor 	if ((minor = zvol_minor_alloc()) == 0) {
472503ad85cSMatthew Ahrens 		dmu_objset_disown(os, zvol_tag);
473fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
474fa9e4066Sahrens 		return (ENXIO);
475fa9e4066Sahrens 	}
476fa9e4066Sahrens 
477fa9e4066Sahrens 	if (ddi_soft_state_zalloc(zvol_state, minor) != DDI_SUCCESS) {
478503ad85cSMatthew Ahrens 		dmu_objset_disown(os, zvol_tag);
479fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
480fa9e4066Sahrens 		return (EAGAIN);
481fa9e4066Sahrens 	}
482e9dbad6fSeschrock 	(void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME,
483e9dbad6fSeschrock 	    (char *)name);
484fa9e4066Sahrens 
485681d9761SEric Taylor 	(void) snprintf(chrbuf, sizeof (chrbuf), "%u,raw", minor);
486fa9e4066Sahrens 
487fa9e4066Sahrens 	if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR,
488fa9e4066Sahrens 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
489fa9e4066Sahrens 		ddi_soft_state_free(zvol_state, minor);
490503ad85cSMatthew Ahrens 		dmu_objset_disown(os, zvol_tag);
491fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
492fa9e4066Sahrens 		return (EAGAIN);
493fa9e4066Sahrens 	}
494fa9e4066Sahrens 
495681d9761SEric Taylor 	(void) snprintf(blkbuf, sizeof (blkbuf), "%u", minor);
496fa9e4066Sahrens 
497fa9e4066Sahrens 	if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK,
498fa9e4066Sahrens 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
499fa9e4066Sahrens 		ddi_remove_minor_node(zfs_dip, chrbuf);
500fa9e4066Sahrens 		ddi_soft_state_free(zvol_state, minor);
501503ad85cSMatthew Ahrens 		dmu_objset_disown(os, zvol_tag);
502fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
503fa9e4066Sahrens 		return (EAGAIN);
504fa9e4066Sahrens 	}
505fa9e4066Sahrens 
506fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
507fa9e4066Sahrens 
508681d9761SEric Taylor 	(void) strlcpy(zv->zv_name, name, MAXPATHLEN);
509fa9e4066Sahrens 	zv->zv_min_bs = DEV_BSHIFT;
510fa9e4066Sahrens 	zv->zv_minor = minor;
511fa9e4066Sahrens 	zv->zv_objset = os;
512681d9761SEric Taylor 	if (dmu_objset_is_snapshot(os))
513681d9761SEric Taylor 		zv->zv_flags |= ZVOL_RDONLY;
514c2e6a7d6Sperrin 	mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
515c2e6a7d6Sperrin 	avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
516c2e6a7d6Sperrin 	    sizeof (rl_t), offsetof(rl_t, r_node));
51788b7b0f2SMatthew Ahrens 	list_create(&zv->zv_extents, sizeof (zvol_extent_t),
51888b7b0f2SMatthew Ahrens 	    offsetof(zvol_extent_t, ze_node));
51967bd71c6Sperrin 	/* get and cache the blocksize */
52067bd71c6Sperrin 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
52167bd71c6Sperrin 	ASSERT(error == 0);
52267bd71c6Sperrin 	zv->zv_volblocksize = doi.doi_data_block_size;
52322ac5be4Sperrin 
5241209a471SNeil Perrin 	zil_replay(os, zv, zvol_replay_vector);
525681d9761SEric Taylor 	dmu_objset_disown(os, zvol_tag);
526681d9761SEric Taylor 	zv->zv_objset = NULL;
527fa9e4066Sahrens 
528fa9e4066Sahrens 	zvol_minors++;
529fa9e4066Sahrens 
530fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
531fa9e4066Sahrens 
532fa9e4066Sahrens 	return (0);
533fa9e4066Sahrens }
534fa9e4066Sahrens 
535fa9e4066Sahrens /*
536fa9e4066Sahrens  * Remove minor node for the specified volume.
537fa9e4066Sahrens  */
538681d9761SEric Taylor static int
539681d9761SEric Taylor zvol_remove_zv(zvol_state_t *zv)
540681d9761SEric Taylor {
541681d9761SEric Taylor 	char nmbuf[20];
542681d9761SEric Taylor 
543681d9761SEric Taylor 	ASSERT(MUTEX_HELD(&zvol_state_lock));
544681d9761SEric Taylor 	if (zv->zv_total_opens != 0)
545681d9761SEric Taylor 		return (EBUSY);
546681d9761SEric Taylor 
547681d9761SEric Taylor 	(void) snprintf(nmbuf, sizeof (nmbuf), "%u,raw", zv->zv_minor);
548681d9761SEric Taylor 	ddi_remove_minor_node(zfs_dip, nmbuf);
549681d9761SEric Taylor 
550681d9761SEric Taylor 	(void) snprintf(nmbuf, sizeof (nmbuf), "%u", zv->zv_minor);
551681d9761SEric Taylor 	ddi_remove_minor_node(zfs_dip, nmbuf);
552681d9761SEric Taylor 
553681d9761SEric Taylor 	avl_destroy(&zv->zv_znode.z_range_avl);
554681d9761SEric Taylor 	mutex_destroy(&zv->zv_znode.z_range_lock);
555681d9761SEric Taylor 
556681d9761SEric Taylor 	ddi_soft_state_free(zvol_state, zv->zv_minor);
557681d9761SEric Taylor 
558681d9761SEric Taylor 	zvol_minors--;
559681d9761SEric Taylor 	return (0);
560681d9761SEric Taylor }
561681d9761SEric Taylor 
562fa9e4066Sahrens int
563e9dbad6fSeschrock zvol_remove_minor(const char *name)
564fa9e4066Sahrens {
565fa9e4066Sahrens 	zvol_state_t *zv;
566681d9761SEric Taylor 	int rc;
567fa9e4066Sahrens 
568fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
569e9dbad6fSeschrock 	if ((zv = zvol_minor_lookup(name)) == NULL) {
570fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
571fa9e4066Sahrens 		return (ENXIO);
572fa9e4066Sahrens 	}
573681d9761SEric Taylor 	rc = zvol_remove_zv(zv);
574681d9761SEric Taylor 	mutex_exit(&zvol_state_lock);
575681d9761SEric Taylor 	return (rc);
576681d9761SEric Taylor }
577fa9e4066Sahrens 
578681d9761SEric Taylor int
579681d9761SEric Taylor zvol_first_open(zvol_state_t *zv)
580681d9761SEric Taylor {
581681d9761SEric Taylor 	objset_t *os;
582681d9761SEric Taylor 	uint64_t volsize;
583681d9761SEric Taylor 	int error;
584681d9761SEric Taylor 	uint64_t readonly;
585fa9e4066Sahrens 
586681d9761SEric Taylor 	/* lie and say we're read-only */
587681d9761SEric Taylor 	error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, B_TRUE,
588681d9761SEric Taylor 	    zvol_tag, &os);
589681d9761SEric Taylor 	if (error)
590681d9761SEric Taylor 		return (error);
591fa9e4066Sahrens 
592681d9761SEric Taylor 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
593681d9761SEric Taylor 	if (error) {
594681d9761SEric Taylor 		ASSERT(error == 0);
595681d9761SEric Taylor 		dmu_objset_disown(os, zvol_tag);
596681d9761SEric Taylor 		return (error);
597681d9761SEric Taylor 	}
598681d9761SEric Taylor 	zv->zv_objset = os;
599681d9761SEric Taylor 	zv->zv_volsize = volsize;
600681d9761SEric Taylor 	zv->zv_zilog = zil_open(os, zvol_get_data);
601681d9761SEric Taylor 	zvol_size_changed(zv->zv_volsize, ddi_driver_major(zfs_dip),
602681d9761SEric Taylor 	    zv->zv_minor);
603fa9e4066Sahrens 
604681d9761SEric Taylor 	VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &readonly,
605681d9761SEric Taylor 	    NULL) == 0);
606681d9761SEric Taylor 	if (readonly || dmu_objset_is_snapshot(os))
607681d9761SEric Taylor 		zv->zv_flags |= ZVOL_RDONLY;
608681d9761SEric Taylor 	else
609681d9761SEric Taylor 		zv->zv_flags &= ~ZVOL_RDONLY;
610681d9761SEric Taylor 	return (error);
611681d9761SEric Taylor }
612fa9e4066Sahrens 
613681d9761SEric Taylor void
614681d9761SEric Taylor zvol_last_close(zvol_state_t *zv)
615681d9761SEric Taylor {
61622ac5be4Sperrin 	zil_close(zv->zv_zilog);
61722ac5be4Sperrin 	zv->zv_zilog = NULL;
618503ad85cSMatthew Ahrens 	dmu_objset_disown(zv->zv_objset, zvol_tag);
619fa9e4066Sahrens 	zv->zv_objset = NULL;
620fa9e4066Sahrens }
621fa9e4066Sahrens 
622e7cbe64fSgw int
623e7cbe64fSgw zvol_prealloc(zvol_state_t *zv)
624e7cbe64fSgw {
625e7cbe64fSgw 	objset_t *os = zv->zv_objset;
626e7cbe64fSgw 	dmu_tx_t *tx;
627e7cbe64fSgw 	uint64_t refd, avail, usedobjs, availobjs;
628e7cbe64fSgw 	uint64_t resid = zv->zv_volsize;
629e7cbe64fSgw 	uint64_t off = 0;
630e7cbe64fSgw 
631e7cbe64fSgw 	/* Check the space usage before attempting to allocate the space */
632e7cbe64fSgw 	dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs);
633e7cbe64fSgw 	if (avail < zv->zv_volsize)
634e7cbe64fSgw 		return (ENOSPC);
635e7cbe64fSgw 
636e7cbe64fSgw 	/* Free old extents if they exist */
637e7cbe64fSgw 	zvol_free_extents(zv);
638e7cbe64fSgw 
639e7cbe64fSgw 	while (resid != 0) {
640e7cbe64fSgw 		int error;
641e7cbe64fSgw 		uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE);
642e7cbe64fSgw 
643e7cbe64fSgw 		tx = dmu_tx_create(os);
644e7cbe64fSgw 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
645e7cbe64fSgw 		error = dmu_tx_assign(tx, TXG_WAIT);
646e7cbe64fSgw 		if (error) {
647e7cbe64fSgw 			dmu_tx_abort(tx);
648cdb0ab79Smaybee 			(void) dmu_free_long_range(os, ZVOL_OBJ, 0, off);
649e7cbe64fSgw 			return (error);
650e7cbe64fSgw 		}
65182c9918fSTim Haley 		dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx);
652e7cbe64fSgw 		dmu_tx_commit(tx);
653e7cbe64fSgw 		off += bytes;
654e7cbe64fSgw 		resid -= bytes;
655e7cbe64fSgw 	}
656e7cbe64fSgw 	txg_wait_synced(dmu_objset_pool(os), 0);
657e7cbe64fSgw 
658e7cbe64fSgw 	return (0);
659e7cbe64fSgw }
660e7cbe64fSgw 
661e7cbe64fSgw int
662681d9761SEric Taylor zvol_update_volsize(objset_t *os, uint64_t volsize)
663e7cbe64fSgw {
664e7cbe64fSgw 	dmu_tx_t *tx;
665e7cbe64fSgw 	int error;
666e7cbe64fSgw 
667e7cbe64fSgw 	ASSERT(MUTEX_HELD(&zvol_state_lock));
668e7cbe64fSgw 
669681d9761SEric Taylor 	tx = dmu_tx_create(os);
670e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
671e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
672e7cbe64fSgw 	if (error) {
673e7cbe64fSgw 		dmu_tx_abort(tx);
674e7cbe64fSgw 		return (error);
675e7cbe64fSgw 	}
676e7cbe64fSgw 
677681d9761SEric Taylor 	error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
678e7cbe64fSgw 	    &volsize, tx);
679e7cbe64fSgw 	dmu_tx_commit(tx);
680e7cbe64fSgw 
681e7cbe64fSgw 	if (error == 0)
682681d9761SEric Taylor 		error = dmu_free_long_range(os,
683cdb0ab79Smaybee 		    ZVOL_OBJ, volsize, DMU_OBJECT_END);
684681d9761SEric Taylor 	return (error);
685681d9761SEric Taylor }
686e7cbe64fSgw 
687681d9761SEric Taylor void
688681d9761SEric Taylor zvol_remove_minors(const char *name)
689681d9761SEric Taylor {
690681d9761SEric Taylor 	zvol_state_t *zv;
691681d9761SEric Taylor 	char *namebuf;
692681d9761SEric Taylor 	minor_t minor;
693681d9761SEric Taylor 
694681d9761SEric Taylor 	namebuf = kmem_zalloc(strlen(name) + 2, KM_SLEEP);
695681d9761SEric Taylor 	(void) strncpy(namebuf, name, strlen(name));
696681d9761SEric Taylor 	(void) strcat(namebuf, "/");
697681d9761SEric Taylor 	mutex_enter(&zvol_state_lock);
698681d9761SEric Taylor 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) {
699681d9761SEric Taylor 
700681d9761SEric Taylor 		zv = ddi_get_soft_state(zvol_state, minor);
701681d9761SEric Taylor 		if (zv == NULL)
702681d9761SEric Taylor 			continue;
703681d9761SEric Taylor 		if (strncmp(namebuf, zv->zv_name, strlen(namebuf)) == 0)
704681d9761SEric Taylor 			(void) zvol_remove_zv(zv);
705e7cbe64fSgw 	}
706681d9761SEric Taylor 	kmem_free(namebuf, strlen(name) + 2);
707681d9761SEric Taylor 
708681d9761SEric Taylor 	mutex_exit(&zvol_state_lock);
709e7cbe64fSgw }
710e7cbe64fSgw 
711fa9e4066Sahrens int
71291ebeef5Sahrens zvol_set_volsize(const char *name, major_t maj, uint64_t volsize)
713fa9e4066Sahrens {
714681d9761SEric Taylor 	zvol_state_t *zv = NULL;
715681d9761SEric Taylor 	objset_t *os;
716fa9e4066Sahrens 	int error;
7175c5460e9Seschrock 	dmu_object_info_t doi;
718e7cbe64fSgw 	uint64_t old_volsize = 0ULL;
719681d9761SEric Taylor 	uint64_t readonly;
720fa9e4066Sahrens 
721fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
722681d9761SEric Taylor 	zv = zvol_minor_lookup(name);
723681d9761SEric Taylor 	if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) {
724681d9761SEric Taylor 		mutex_exit(&zvol_state_lock);
725681d9761SEric Taylor 		return (error);
726fa9e4066Sahrens 	}
727fa9e4066Sahrens 
728681d9761SEric Taylor 	if ((error = dmu_object_info(os, ZVOL_OBJ, &doi)) != 0 ||
729e9dbad6fSeschrock 	    (error = zvol_check_volsize(volsize,
730bb0ade09Sahrens 	    doi.doi_data_block_size)) != 0)
731bb0ade09Sahrens 		goto out;
7325c5460e9Seschrock 
733681d9761SEric Taylor 	VERIFY(dsl_prop_get_integer(name, "readonly", &readonly,
734681d9761SEric Taylor 	    NULL) == 0);
735681d9761SEric Taylor 	if (readonly) {
736bb0ade09Sahrens 		error = EROFS;
737bb0ade09Sahrens 		goto out;
738fa9e4066Sahrens 	}
739fa9e4066Sahrens 
740681d9761SEric Taylor 	error = zvol_update_volsize(os, volsize);
741e7cbe64fSgw 	/*
742e7cbe64fSgw 	 * Reinitialize the dump area to the new size. If we
743681d9761SEric Taylor 	 * failed to resize the dump area then restore it back to
744681d9761SEric Taylor 	 * its original size.
745e7cbe64fSgw 	 */
746681d9761SEric Taylor 	if (zv && error == 0) {
747681d9761SEric Taylor 		if (zv->zv_flags & ZVOL_DUMPIFIED) {
748681d9761SEric Taylor 			old_volsize = zv->zv_volsize;
749681d9761SEric Taylor 			zv->zv_volsize = volsize;
750681d9761SEric Taylor 			if ((error = zvol_dumpify(zv)) != 0 ||
751681d9761SEric Taylor 			    (error = dumpvp_resize()) != 0) {
752681d9761SEric Taylor 				(void) zvol_update_volsize(os, old_volsize);
753681d9761SEric Taylor 				zv->zv_volsize = old_volsize;
754681d9761SEric Taylor 				error = zvol_dumpify(zv);
755681d9761SEric Taylor 			}
756681d9761SEric Taylor 		}
757681d9761SEric Taylor 		if (error == 0) {
758681d9761SEric Taylor 			zv->zv_volsize = volsize;
759681d9761SEric Taylor 			zvol_size_changed(volsize, maj, zv->zv_minor);
760e7cbe64fSgw 		}
761fa9e4066Sahrens 	}
762fa9e4066Sahrens 
763573ca77eSGeorge Wilson 	/*
764573ca77eSGeorge Wilson 	 * Generate a LUN expansion event.
765573ca77eSGeorge Wilson 	 */
766681d9761SEric Taylor 	if (zv && error == 0) {
767573ca77eSGeorge Wilson 		sysevent_id_t eid;
768573ca77eSGeorge Wilson 		nvlist_t *attr;
769573ca77eSGeorge Wilson 		char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
770573ca77eSGeorge Wilson 
771681d9761SEric Taylor 		(void) snprintf(physpath, MAXPATHLEN, "%s%u", ZVOL_PSEUDO_DEV,
772573ca77eSGeorge Wilson 		    zv->zv_minor);
773573ca77eSGeorge Wilson 
774573ca77eSGeorge Wilson 		VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
775573ca77eSGeorge Wilson 		VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
776573ca77eSGeorge Wilson 
777573ca77eSGeorge Wilson 		(void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
778573ca77eSGeorge Wilson 		    ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
779573ca77eSGeorge Wilson 
780573ca77eSGeorge Wilson 		nvlist_free(attr);
781573ca77eSGeorge Wilson 		kmem_free(physpath, MAXPATHLEN);
782573ca77eSGeorge Wilson 	}
783573ca77eSGeorge Wilson 
784bb0ade09Sahrens out:
785681d9761SEric Taylor 	dmu_objset_rele(os, FTAG);
786bb0ade09Sahrens 
787fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
788fa9e4066Sahrens 
789fa9e4066Sahrens 	return (error);
790fa9e4066Sahrens }
791fa9e4066Sahrens 
792fa9e4066Sahrens /*ARGSUSED*/
793fa9e4066Sahrens int
794fa9e4066Sahrens zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr)
795fa9e4066Sahrens {
796fa9e4066Sahrens 	minor_t minor = getminor(*devp);
797fa9e4066Sahrens 	zvol_state_t *zv;
798681d9761SEric Taylor 	int err = 0;
799fa9e4066Sahrens 
800fa9e4066Sahrens 	if (minor == 0)			/* This is the control device */
801fa9e4066Sahrens 		return (0);
802fa9e4066Sahrens 
803fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
804fa9e4066Sahrens 
805fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
806fa9e4066Sahrens 	if (zv == NULL) {
807fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
808fa9e4066Sahrens 		return (ENXIO);
809fa9e4066Sahrens 	}
810fa9e4066Sahrens 
811681d9761SEric Taylor 	if (zv->zv_total_opens == 0)
812681d9761SEric Taylor 		err = zvol_first_open(zv);
813681d9761SEric Taylor 	if (err) {
814fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
815681d9761SEric Taylor 		return (err);
816681d9761SEric Taylor 	}
817681d9761SEric Taylor 	if ((flag & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
818681d9761SEric Taylor 		err = EROFS;
819681d9761SEric Taylor 		goto out;
820fa9e4066Sahrens 	}
821c7f714e2SEric Taylor 	if (zv->zv_flags & ZVOL_EXCL) {
822681d9761SEric Taylor 		err = EBUSY;
823681d9761SEric Taylor 		goto out;
824c7f714e2SEric Taylor 	}
825c7f714e2SEric Taylor 	if (flag & FEXCL) {
826c7f714e2SEric Taylor 		if (zv->zv_total_opens != 0) {
827681d9761SEric Taylor 			err = EBUSY;
828681d9761SEric Taylor 			goto out;
829c7f714e2SEric Taylor 		}
830c7f714e2SEric Taylor 		zv->zv_flags |= ZVOL_EXCL;
831c7f714e2SEric Taylor 	}
832fa9e4066Sahrens 
833fa9e4066Sahrens 	if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) {
834fa9e4066Sahrens 		zv->zv_open_count[otyp]++;
835fa9e4066Sahrens 		zv->zv_total_opens++;
836fa9e4066Sahrens 	}
837fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
838fa9e4066Sahrens 
839681d9761SEric Taylor 	return (err);
840681d9761SEric Taylor out:
841681d9761SEric Taylor 	if (zv->zv_total_opens == 0)
842681d9761SEric Taylor 		zvol_last_close(zv);
843681d9761SEric Taylor 	mutex_exit(&zvol_state_lock);
844681d9761SEric Taylor 	return (err);
845fa9e4066Sahrens }
846fa9e4066Sahrens 
847fa9e4066Sahrens /*ARGSUSED*/
848fa9e4066Sahrens int
849fa9e4066Sahrens zvol_close(dev_t dev, int flag, int otyp, cred_t *cr)
850fa9e4066Sahrens {
851fa9e4066Sahrens 	minor_t minor = getminor(dev);
852fa9e4066Sahrens 	zvol_state_t *zv;
853681d9761SEric Taylor 	int error = 0;
854fa9e4066Sahrens 
855fa9e4066Sahrens 	if (minor == 0)		/* This is the control device */
856fa9e4066Sahrens 		return (0);
857fa9e4066Sahrens 
858fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
859fa9e4066Sahrens 
860fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, minor);
861fa9e4066Sahrens 	if (zv == NULL) {
862fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
863fa9e4066Sahrens 		return (ENXIO);
864fa9e4066Sahrens 	}
865fa9e4066Sahrens 
866c7f714e2SEric Taylor 	if (zv->zv_flags & ZVOL_EXCL) {
867c7f714e2SEric Taylor 		ASSERT(zv->zv_total_opens == 1);
868c7f714e2SEric Taylor 		zv->zv_flags &= ~ZVOL_EXCL;
869fa9e4066Sahrens 	}
870fa9e4066Sahrens 
871fa9e4066Sahrens 	/*
872fa9e4066Sahrens 	 * If the open count is zero, this is a spurious close.
873fa9e4066Sahrens 	 * That indicates a bug in the kernel / DDI framework.
874fa9e4066Sahrens 	 */
875fa9e4066Sahrens 	ASSERT(zv->zv_open_count[otyp] != 0);
876fa9e4066Sahrens 	ASSERT(zv->zv_total_opens != 0);
877fa9e4066Sahrens 
878fa9e4066Sahrens 	/*
879fa9e4066Sahrens 	 * You may get multiple opens, but only one close.
880fa9e4066Sahrens 	 */
881fa9e4066Sahrens 	zv->zv_open_count[otyp]--;
882fa9e4066Sahrens 	zv->zv_total_opens--;
883fa9e4066Sahrens 
884681d9761SEric Taylor 	if (zv->zv_total_opens == 0)
885681d9761SEric Taylor 		zvol_last_close(zv);
886fa9e4066Sahrens 
887681d9761SEric Taylor 	mutex_exit(&zvol_state_lock);
888681d9761SEric Taylor 	return (error);
889fa9e4066Sahrens }
890fa9e4066Sahrens 
891feb08c6bSbillm static void
892*b24ab676SJeff Bonwick zvol_get_done(zgd_t *zgd, int error)
89367bd71c6Sperrin {
894*b24ab676SJeff Bonwick 	if (zgd->zgd_db)
895*b24ab676SJeff Bonwick 		dmu_buf_rele(zgd->zgd_db, zgd);
896*b24ab676SJeff Bonwick 
897*b24ab676SJeff Bonwick 	zfs_range_unlock(zgd->zgd_rl);
898*b24ab676SJeff Bonwick 
899*b24ab676SJeff Bonwick 	if (error == 0 && zgd->zgd_bp)
900*b24ab676SJeff Bonwick 		zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
90167bd71c6Sperrin 
90267bd71c6Sperrin 	kmem_free(zgd, sizeof (zgd_t));
90367bd71c6Sperrin }
90467bd71c6Sperrin 
90567bd71c6Sperrin /*
90667bd71c6Sperrin  * Get data to generate a TX_WRITE intent log record.
90767bd71c6Sperrin  */
908feb08c6bSbillm static int
90967bd71c6Sperrin zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
91067bd71c6Sperrin {
91167bd71c6Sperrin 	zvol_state_t *zv = arg;
91267bd71c6Sperrin 	objset_t *os = zv->zv_objset;
913*b24ab676SJeff Bonwick 	uint64_t object = ZVOL_OBJ;
914*b24ab676SJeff Bonwick 	uint64_t offset = lr->lr_offset;
915*b24ab676SJeff Bonwick 	uint64_t size = lr->lr_length;	/* length of user data */
916*b24ab676SJeff Bonwick 	blkptr_t *bp = &lr->lr_blkptr;
91767bd71c6Sperrin 	dmu_buf_t *db;
91867bd71c6Sperrin 	zgd_t *zgd;
91967bd71c6Sperrin 	int error;
92067bd71c6Sperrin 
921*b24ab676SJeff Bonwick 	ASSERT(zio != NULL);
922*b24ab676SJeff Bonwick 	ASSERT(size != 0);
923*b24ab676SJeff Bonwick 
924*b24ab676SJeff Bonwick 	zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
925*b24ab676SJeff Bonwick 	zgd->zgd_zilog = zv->zv_zilog;
926*b24ab676SJeff Bonwick 	zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
927feb08c6bSbillm 
928c2e6a7d6Sperrin 	/*
929c2e6a7d6Sperrin 	 * Write records come in two flavors: immediate and indirect.
930c2e6a7d6Sperrin 	 * For small writes it's cheaper to store the data with the
931c2e6a7d6Sperrin 	 * log record (immediate); for large writes it's cheaper to
932c2e6a7d6Sperrin 	 * sync the data and get a pointer to it (indirect) so that
933c2e6a7d6Sperrin 	 * we don't have to write the data twice.
934c2e6a7d6Sperrin 	 */
935*b24ab676SJeff Bonwick 	if (buf != NULL) {	/* immediate write */
936*b24ab676SJeff Bonwick 		error = dmu_read(os, object, offset, size, buf,
937*b24ab676SJeff Bonwick 		    DMU_READ_NO_PREFETCH);
938*b24ab676SJeff Bonwick 	} else {
939*b24ab676SJeff Bonwick 		size = zv->zv_volblocksize;
940*b24ab676SJeff Bonwick 		offset = P2ALIGN(offset, size);
941*b24ab676SJeff Bonwick 		error = dmu_buf_hold(os, object, offset, zgd, &db);
942*b24ab676SJeff Bonwick 		if (error == 0) {
943*b24ab676SJeff Bonwick 			zgd->zgd_db = db;
944*b24ab676SJeff Bonwick 			zgd->zgd_bp = bp;
94567bd71c6Sperrin 
946*b24ab676SJeff Bonwick 			ASSERT(db->db_offset == offset);
947*b24ab676SJeff Bonwick 			ASSERT(db->db_size == size);
94867bd71c6Sperrin 
949*b24ab676SJeff Bonwick 			error = dmu_sync(zio, lr->lr_common.lrc_txg,
950*b24ab676SJeff Bonwick 			    zvol_get_done, zgd);
951975c32a0SNeil Perrin 
952*b24ab676SJeff Bonwick 			if (error == 0)
953*b24ab676SJeff Bonwick 				return (0);
954*b24ab676SJeff Bonwick 		}
955975c32a0SNeil Perrin 	}
956975c32a0SNeil Perrin 
957*b24ab676SJeff Bonwick 	zvol_get_done(zgd, error);
958*b24ab676SJeff Bonwick 
95967bd71c6Sperrin 	return (error);
96067bd71c6Sperrin }
96167bd71c6Sperrin 
962a24e15ceSperrin /*
963a24e15ceSperrin  * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
96422ac5be4Sperrin  *
96522ac5be4Sperrin  * We store data in the log buffers if it's small enough.
96667bd71c6Sperrin  * Otherwise we will later flush the data out via dmu_sync().
96722ac5be4Sperrin  */
96867bd71c6Sperrin ssize_t zvol_immediate_write_sz = 32768;
96922ac5be4Sperrin 
970feb08c6bSbillm static void
971510b6c0eSNeil Perrin zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid,
972510b6c0eSNeil Perrin     boolean_t sync)
97322ac5be4Sperrin {
974feb08c6bSbillm 	uint32_t blocksize = zv->zv_volblocksize;
9751209a471SNeil Perrin 	zilog_t *zilog = zv->zv_zilog;
976510b6c0eSNeil Perrin 	boolean_t slogging;
977e09fa4daSNeil Perrin 	ssize_t immediate_write_sz;
978510b6c0eSNeil Perrin 
979510b6c0eSNeil Perrin 	if (zil_disable)
980510b6c0eSNeil Perrin 		return;
98122ac5be4Sperrin 
982*b24ab676SJeff Bonwick 	if (zil_replaying(zilog, tx))
9831209a471SNeil Perrin 		return;
9841209a471SNeil Perrin 
985e09fa4daSNeil Perrin 	immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
986e09fa4daSNeil Perrin 	    ? 0 : zvol_immediate_write_sz;
987e09fa4daSNeil Perrin 
988e09fa4daSNeil Perrin 	slogging = spa_has_slogs(zilog->zl_spa) &&
989e09fa4daSNeil Perrin 	    (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
990feb08c6bSbillm 
991510b6c0eSNeil Perrin 	while (resid) {
992510b6c0eSNeil Perrin 		itx_t *itx;
993510b6c0eSNeil Perrin 		lr_write_t *lr;
994510b6c0eSNeil Perrin 		ssize_t len;
995510b6c0eSNeil Perrin 		itx_wr_state_t write_state;
996510b6c0eSNeil Perrin 
997510b6c0eSNeil Perrin 		/*
998510b6c0eSNeil Perrin 		 * Unlike zfs_log_write() we can be called with
999510b6c0eSNeil Perrin 		 * upto DMU_MAX_ACCESS/2 (5MB) writes.
1000510b6c0eSNeil Perrin 		 */
1001e09fa4daSNeil Perrin 		if (blocksize > immediate_write_sz && !slogging &&
1002510b6c0eSNeil Perrin 		    resid >= blocksize && off % blocksize == 0) {
1003510b6c0eSNeil Perrin 			write_state = WR_INDIRECT; /* uses dmu_sync */
1004510b6c0eSNeil Perrin 			len = blocksize;
1005510b6c0eSNeil Perrin 		} else if (sync) {
1006510b6c0eSNeil Perrin 			write_state = WR_COPIED;
1007510b6c0eSNeil Perrin 			len = MIN(ZIL_MAX_LOG_DATA, resid);
1008510b6c0eSNeil Perrin 		} else {
1009510b6c0eSNeil Perrin 			write_state = WR_NEED_COPY;
1010510b6c0eSNeil Perrin 			len = MIN(ZIL_MAX_LOG_DATA, resid);
1011510b6c0eSNeil Perrin 		}
1012510b6c0eSNeil Perrin 
1013510b6c0eSNeil Perrin 		itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
1014510b6c0eSNeil Perrin 		    (write_state == WR_COPIED ? len : 0));
1015feb08c6bSbillm 		lr = (lr_write_t *)&itx->itx_lr;
1016510b6c0eSNeil Perrin 		if (write_state == WR_COPIED && dmu_read(zv->zv_objset,
10177bfdf011SNeil Perrin 		    ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
1018*b24ab676SJeff Bonwick 			zil_itx_destroy(itx);
1019510b6c0eSNeil Perrin 			itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1020510b6c0eSNeil Perrin 			lr = (lr_write_t *)&itx->itx_lr;
1021510b6c0eSNeil Perrin 			write_state = WR_NEED_COPY;
1022510b6c0eSNeil Perrin 		}
1023510b6c0eSNeil Perrin 
1024510b6c0eSNeil Perrin 		itx->itx_wr_state = write_state;
1025510b6c0eSNeil Perrin 		if (write_state == WR_NEED_COPY)
1026510b6c0eSNeil Perrin 			itx->itx_sod += len;
1027feb08c6bSbillm 		lr->lr_foid = ZVOL_OBJ;
1028feb08c6bSbillm 		lr->lr_offset = off;
1029510b6c0eSNeil Perrin 		lr->lr_length = len;
1030*b24ab676SJeff Bonwick 		lr->lr_blkoff = 0;
1031feb08c6bSbillm 		BP_ZERO(&lr->lr_blkptr);
1032feb08c6bSbillm 
1033510b6c0eSNeil Perrin 		itx->itx_private = zv;
1034510b6c0eSNeil Perrin 		itx->itx_sync = sync;
1035510b6c0eSNeil Perrin 
10361209a471SNeil Perrin 		(void) zil_itx_assign(zilog, itx, tx);
1037510b6c0eSNeil Perrin 
1038510b6c0eSNeil Perrin 		off += len;
1039510b6c0eSNeil Perrin 		resid -= len;
104022ac5be4Sperrin 	}
104122ac5be4Sperrin }
104222ac5be4Sperrin 
104388b7b0f2SMatthew Ahrens static int
104488b7b0f2SMatthew Ahrens zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t size,
104588b7b0f2SMatthew Ahrens     boolean_t doread, boolean_t isdump)
1046e7cbe64fSgw {
1047e7cbe64fSgw 	vdev_disk_t *dvd;
1048e7cbe64fSgw 	int c;
1049e7cbe64fSgw 	int numerrors = 0;
1050e7cbe64fSgw 
1051e7cbe64fSgw 	for (c = 0; c < vd->vdev_children; c++) {
105221ecdf64SLin Ling 		ASSERT(vd->vdev_ops == &vdev_mirror_ops ||
105321ecdf64SLin Ling 		    vd->vdev_ops == &vdev_replacing_ops ||
105421ecdf64SLin Ling 		    vd->vdev_ops == &vdev_spare_ops);
105588b7b0f2SMatthew Ahrens 		int err = zvol_dumpio_vdev(vd->vdev_child[c],
105688b7b0f2SMatthew Ahrens 		    addr, offset, size, doread, isdump);
105788b7b0f2SMatthew Ahrens 		if (err != 0) {
1058e7cbe64fSgw 			numerrors++;
105988b7b0f2SMatthew Ahrens 		} else if (doread) {
1060e7cbe64fSgw 			break;
1061e7cbe64fSgw 		}
1062e7cbe64fSgw 	}
1063e7cbe64fSgw 
1064e7cbe64fSgw 	if (!vd->vdev_ops->vdev_op_leaf)
1065e7cbe64fSgw 		return (numerrors < vd->vdev_children ? 0 : EIO);
1066e7cbe64fSgw 
1067dc0bb255SEric Taylor 	if (doread && !vdev_readable(vd))
1068dc0bb255SEric Taylor 		return (EIO);
1069dc0bb255SEric Taylor 	else if (!doread && !vdev_writeable(vd))
1070e7cbe64fSgw 		return (EIO);
1071e7cbe64fSgw 
1072e7cbe64fSgw 	dvd = vd->vdev_tsd;
1073e7cbe64fSgw 	ASSERT3P(dvd, !=, NULL);
1074e7cbe64fSgw 	offset += VDEV_LABEL_START_SIZE;
1075e7cbe64fSgw 
1076e7cbe64fSgw 	if (ddi_in_panic() || isdump) {
107788b7b0f2SMatthew Ahrens 		ASSERT(!doread);
107888b7b0f2SMatthew Ahrens 		if (doread)
1079e7cbe64fSgw 			return (EIO);
1080e7cbe64fSgw 		return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset),
1081e7cbe64fSgw 		    lbtodb(size)));
1082e7cbe64fSgw 	} else {
1083e7cbe64fSgw 		return (vdev_disk_physio(dvd->vd_lh, addr, size, offset,
108488b7b0f2SMatthew Ahrens 		    doread ? B_READ : B_WRITE));
1085e7cbe64fSgw 	}
1086e7cbe64fSgw }
1087e7cbe64fSgw 
108888b7b0f2SMatthew Ahrens static int
108988b7b0f2SMatthew Ahrens zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size,
109088b7b0f2SMatthew Ahrens     boolean_t doread, boolean_t isdump)
1091e7cbe64fSgw {
1092e7cbe64fSgw 	vdev_t *vd;
1093e7cbe64fSgw 	int error;
109488b7b0f2SMatthew Ahrens 	zvol_extent_t *ze;
1095e7cbe64fSgw 	spa_t *spa = dmu_objset_spa(zv->zv_objset);
1096e7cbe64fSgw 
109788b7b0f2SMatthew Ahrens 	/* Must be sector aligned, and not stradle a block boundary. */
109888b7b0f2SMatthew Ahrens 	if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) ||
109988b7b0f2SMatthew Ahrens 	    P2BOUNDARY(offset, size, zv->zv_volblocksize)) {
1100e7cbe64fSgw 		return (EINVAL);
110188b7b0f2SMatthew Ahrens 	}
110288b7b0f2SMatthew Ahrens 	ASSERT(size <= zv->zv_volblocksize);
1103e7cbe64fSgw 
110488b7b0f2SMatthew Ahrens 	/* Locate the extent this belongs to */
110588b7b0f2SMatthew Ahrens 	ze = list_head(&zv->zv_extents);
110688b7b0f2SMatthew Ahrens 	while (offset >= ze->ze_nblks * zv->zv_volblocksize) {
110788b7b0f2SMatthew Ahrens 		offset -= ze->ze_nblks * zv->zv_volblocksize;
110888b7b0f2SMatthew Ahrens 		ze = list_next(&zv->zv_extents, ze);
110988b7b0f2SMatthew Ahrens 	}
1110e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
111188b7b0f2SMatthew Ahrens 	vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva));
111288b7b0f2SMatthew Ahrens 	offset += DVA_GET_OFFSET(&ze->ze_dva);
111388b7b0f2SMatthew Ahrens 	error = zvol_dumpio_vdev(vd, addr, offset, size, doread, isdump);
1114e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
1115e7cbe64fSgw 	return (error);
1116e7cbe64fSgw }
1117e7cbe64fSgw 
1118fa9e4066Sahrens int
1119fa9e4066Sahrens zvol_strategy(buf_t *bp)
1120fa9e4066Sahrens {
1121fa9e4066Sahrens 	zvol_state_t *zv = ddi_get_soft_state(zvol_state, getminor(bp->b_edev));
1122fa9e4066Sahrens 	uint64_t off, volsize;
112388b7b0f2SMatthew Ahrens 	size_t resid;
1124fa9e4066Sahrens 	char *addr;
112522ac5be4Sperrin 	objset_t *os;
1126c2e6a7d6Sperrin 	rl_t *rl;
1127fa9e4066Sahrens 	int error = 0;
112888b7b0f2SMatthew Ahrens 	boolean_t doread = bp->b_flags & B_READ;
112988b7b0f2SMatthew Ahrens 	boolean_t is_dump = zv->zv_flags & ZVOL_DUMPIFIED;
1130510b6c0eSNeil Perrin 	boolean_t sync;
1131fa9e4066Sahrens 
1132fa9e4066Sahrens 	if (zv == NULL) {
1133fa9e4066Sahrens 		bioerror(bp, ENXIO);
1134fa9e4066Sahrens 		biodone(bp);
1135fa9e4066Sahrens 		return (0);
1136fa9e4066Sahrens 	}
1137fa9e4066Sahrens 
1138fa9e4066Sahrens 	if (getminor(bp->b_edev) == 0) {
1139fa9e4066Sahrens 		bioerror(bp, EINVAL);
1140fa9e4066Sahrens 		biodone(bp);
1141fa9e4066Sahrens 		return (0);
1142fa9e4066Sahrens 	}
1143fa9e4066Sahrens 
1144681d9761SEric Taylor 	if (!(bp->b_flags & B_READ) && (zv->zv_flags & ZVOL_RDONLY)) {
1145fa9e4066Sahrens 		bioerror(bp, EROFS);
1146fa9e4066Sahrens 		biodone(bp);
1147fa9e4066Sahrens 		return (0);
1148fa9e4066Sahrens 	}
1149fa9e4066Sahrens 
1150fa9e4066Sahrens 	off = ldbtob(bp->b_blkno);
1151fa9e4066Sahrens 	volsize = zv->zv_volsize;
1152fa9e4066Sahrens 
115322ac5be4Sperrin 	os = zv->zv_objset;
115422ac5be4Sperrin 	ASSERT(os != NULL);
1155fa9e4066Sahrens 
1156fa9e4066Sahrens 	bp_mapin(bp);
1157fa9e4066Sahrens 	addr = bp->b_un.b_addr;
1158fa9e4066Sahrens 	resid = bp->b_bcount;
1159fa9e4066Sahrens 
116088b7b0f2SMatthew Ahrens 	if (resid > 0 && (off < 0 || off >= volsize)) {
116188b7b0f2SMatthew Ahrens 		bioerror(bp, EIO);
116288b7b0f2SMatthew Ahrens 		biodone(bp);
116388b7b0f2SMatthew Ahrens 		return (0);
116488b7b0f2SMatthew Ahrens 	}
116573ec3d9cSgw 
1166510b6c0eSNeil Perrin 	sync = !(bp->b_flags & B_ASYNC) && !doread && !is_dump &&
1167510b6c0eSNeil Perrin 	    !(zv->zv_flags & ZVOL_WCE) && !zil_disable;
1168510b6c0eSNeil Perrin 
1169a24e15ceSperrin 	/*
1170a24e15ceSperrin 	 * There must be no buffer changes when doing a dmu_sync() because
1171a24e15ceSperrin 	 * we can't change the data whilst calculating the checksum.
1172a24e15ceSperrin 	 */
1173c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, off, resid,
117488b7b0f2SMatthew Ahrens 	    doread ? RL_READER : RL_WRITER);
1175fa9e4066Sahrens 
1176e7cbe64fSgw 	while (resid != 0 && off < volsize) {
117788b7b0f2SMatthew Ahrens 		size_t size = MIN(resid, zvol_maxphys);
1178e7cbe64fSgw 		if (is_dump) {
1179e7cbe64fSgw 			size = MIN(size, P2END(off, zv->zv_volblocksize) - off);
118088b7b0f2SMatthew Ahrens 			error = zvol_dumpio(zv, addr, off, size,
118188b7b0f2SMatthew Ahrens 			    doread, B_FALSE);
118288b7b0f2SMatthew Ahrens 		} else if (doread) {
11837bfdf011SNeil Perrin 			error = dmu_read(os, ZVOL_OBJ, off, size, addr,
11847bfdf011SNeil Perrin 			    DMU_READ_PREFETCH);
1185fa9e4066Sahrens 		} else {
118622ac5be4Sperrin 			dmu_tx_t *tx = dmu_tx_create(os);
1187fa9e4066Sahrens 			dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
1188fa9e4066Sahrens 			error = dmu_tx_assign(tx, TXG_WAIT);
1189fa9e4066Sahrens 			if (error) {
1190fa9e4066Sahrens 				dmu_tx_abort(tx);
1191fa9e4066Sahrens 			} else {
119222ac5be4Sperrin 				dmu_write(os, ZVOL_OBJ, off, size, addr, tx);
1193510b6c0eSNeil Perrin 				zvol_log_write(zv, tx, off, size, sync);
1194fa9e4066Sahrens 				dmu_tx_commit(tx);
1195fa9e4066Sahrens 			}
1196fa9e4066Sahrens 		}
1197b87f3af3Sperrin 		if (error) {
1198b87f3af3Sperrin 			/* convert checksum errors into IO errors */
1199b87f3af3Sperrin 			if (error == ECKSUM)
1200b87f3af3Sperrin 				error = EIO;
1201fa9e4066Sahrens 			break;
1202b87f3af3Sperrin 		}
1203fa9e4066Sahrens 		off += size;
1204fa9e4066Sahrens 		addr += size;
1205fa9e4066Sahrens 		resid -= size;
1206fa9e4066Sahrens 	}
1207c2e6a7d6Sperrin 	zfs_range_unlock(rl);
1208fa9e4066Sahrens 
1209fa9e4066Sahrens 	if ((bp->b_resid = resid) == bp->b_bcount)
1210fa9e4066Sahrens 		bioerror(bp, off > volsize ? EINVAL : error);
1211fa9e4066Sahrens 
1212510b6c0eSNeil Perrin 	if (sync)
1213feb08c6bSbillm 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1214feb08c6bSbillm 	biodone(bp);
121522ac5be4Sperrin 
1216fa9e4066Sahrens 	return (0);
1217fa9e4066Sahrens }
1218fa9e4066Sahrens 
121967bd71c6Sperrin /*
122067bd71c6Sperrin  * Set the buffer count to the zvol maximum transfer.
122167bd71c6Sperrin  * Using our own routine instead of the default minphys()
122267bd71c6Sperrin  * means that for larger writes we write bigger buffers on X86
122367bd71c6Sperrin  * (128K instead of 56K) and flush the disk write cache less often
122467bd71c6Sperrin  * (every zvol_maxphys - currently 1MB) instead of minphys (currently
122567bd71c6Sperrin  * 56K on X86 and 128K on sparc).
122667bd71c6Sperrin  */
122767bd71c6Sperrin void
122867bd71c6Sperrin zvol_minphys(struct buf *bp)
122967bd71c6Sperrin {
123067bd71c6Sperrin 	if (bp->b_bcount > zvol_maxphys)
123167bd71c6Sperrin 		bp->b_bcount = zvol_maxphys;
123267bd71c6Sperrin }
123367bd71c6Sperrin 
1234e7cbe64fSgw int
1235e7cbe64fSgw zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks)
1236e7cbe64fSgw {
1237e7cbe64fSgw 	minor_t minor = getminor(dev);
1238e7cbe64fSgw 	zvol_state_t *zv;
1239e7cbe64fSgw 	int error = 0;
1240e7cbe64fSgw 	uint64_t size;
1241e7cbe64fSgw 	uint64_t boff;
1242e7cbe64fSgw 	uint64_t resid;
1243e7cbe64fSgw 
1244e7cbe64fSgw 	if (minor == 0)			/* This is the control device */
1245e7cbe64fSgw 		return (ENXIO);
1246e7cbe64fSgw 
1247e7cbe64fSgw 	zv = ddi_get_soft_state(zvol_state, minor);
1248e7cbe64fSgw 	if (zv == NULL)
1249e7cbe64fSgw 		return (ENXIO);
1250e7cbe64fSgw 
1251e7cbe64fSgw 	boff = ldbtob(blkno);
1252e7cbe64fSgw 	resid = ldbtob(nblocks);
125388b7b0f2SMatthew Ahrens 
125488b7b0f2SMatthew Ahrens 	VERIFY3U(boff + resid, <=, zv->zv_volsize);
125588b7b0f2SMatthew Ahrens 
1256e7cbe64fSgw 	while (resid) {
1257e7cbe64fSgw 		size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff);
125888b7b0f2SMatthew Ahrens 		error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE);
1259e7cbe64fSgw 		if (error)
1260e7cbe64fSgw 			break;
1261e7cbe64fSgw 		boff += size;
1262e7cbe64fSgw 		addr += size;
1263e7cbe64fSgw 		resid -= size;
1264e7cbe64fSgw 	}
1265e7cbe64fSgw 
1266e7cbe64fSgw 	return (error);
1267e7cbe64fSgw }
1268e7cbe64fSgw 
1269fa9e4066Sahrens /*ARGSUSED*/
1270fa9e4066Sahrens int
1271feb08c6bSbillm zvol_read(dev_t dev, uio_t *uio, cred_t *cr)
1272fa9e4066Sahrens {
1273c7ca1008Sgw 	minor_t minor = getminor(dev);
1274c7ca1008Sgw 	zvol_state_t *zv;
127573ec3d9cSgw 	uint64_t volsize;
1276c2e6a7d6Sperrin 	rl_t *rl;
1277feb08c6bSbillm 	int error = 0;
1278fa9e4066Sahrens 
1279c7ca1008Sgw 	if (minor == 0)			/* This is the control device */
1280c7ca1008Sgw 		return (ENXIO);
1281c7ca1008Sgw 
1282c7ca1008Sgw 	zv = ddi_get_soft_state(zvol_state, minor);
1283c7ca1008Sgw 	if (zv == NULL)
1284c7ca1008Sgw 		return (ENXIO);
1285c7ca1008Sgw 
128673ec3d9cSgw 	volsize = zv->zv_volsize;
128773ec3d9cSgw 	if (uio->uio_resid > 0 &&
128873ec3d9cSgw 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
128973ec3d9cSgw 		return (EIO);
129073ec3d9cSgw 
129188b7b0f2SMatthew Ahrens 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
129288b7b0f2SMatthew Ahrens 		error = physio(zvol_strategy, NULL, dev, B_READ,
129388b7b0f2SMatthew Ahrens 		    zvol_minphys, uio);
129488b7b0f2SMatthew Ahrens 		return (error);
129588b7b0f2SMatthew Ahrens 	}
129688b7b0f2SMatthew Ahrens 
1297c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1298c2e6a7d6Sperrin 	    RL_READER);
129973ec3d9cSgw 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1300feb08c6bSbillm 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1301fa9e4066Sahrens 
130273ec3d9cSgw 		/* don't read past the end */
130373ec3d9cSgw 		if (bytes > volsize - uio->uio_loffset)
130473ec3d9cSgw 			bytes = volsize - uio->uio_loffset;
130573ec3d9cSgw 
1306feb08c6bSbillm 		error =  dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes);
1307b87f3af3Sperrin 		if (error) {
1308b87f3af3Sperrin 			/* convert checksum errors into IO errors */
1309b87f3af3Sperrin 			if (error == ECKSUM)
1310b87f3af3Sperrin 				error = EIO;
1311feb08c6bSbillm 			break;
1312b87f3af3Sperrin 		}
1313feb08c6bSbillm 	}
1314c2e6a7d6Sperrin 	zfs_range_unlock(rl);
1315feb08c6bSbillm 	return (error);
1316fa9e4066Sahrens }
1317fa9e4066Sahrens 
1318fa9e4066Sahrens /*ARGSUSED*/
1319fa9e4066Sahrens int
1320feb08c6bSbillm zvol_write(dev_t dev, uio_t *uio, cred_t *cr)
1321fa9e4066Sahrens {
1322c7ca1008Sgw 	minor_t minor = getminor(dev);
1323c7ca1008Sgw 	zvol_state_t *zv;
132473ec3d9cSgw 	uint64_t volsize;
1325c2e6a7d6Sperrin 	rl_t *rl;
1326feb08c6bSbillm 	int error = 0;
1327510b6c0eSNeil Perrin 	boolean_t sync;
1328feb08c6bSbillm 
1329c7ca1008Sgw 	if (minor == 0)			/* This is the control device */
1330c7ca1008Sgw 		return (ENXIO);
1331c7ca1008Sgw 
1332c7ca1008Sgw 	zv = ddi_get_soft_state(zvol_state, minor);
1333c7ca1008Sgw 	if (zv == NULL)
1334c7ca1008Sgw 		return (ENXIO);
1335c7ca1008Sgw 
133673ec3d9cSgw 	volsize = zv->zv_volsize;
133773ec3d9cSgw 	if (uio->uio_resid > 0 &&
133873ec3d9cSgw 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
133973ec3d9cSgw 		return (EIO);
134073ec3d9cSgw 
1341e7cbe64fSgw 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
1342e7cbe64fSgw 		error = physio(zvol_strategy, NULL, dev, B_WRITE,
1343e7cbe64fSgw 		    zvol_minphys, uio);
1344e7cbe64fSgw 		return (error);
1345e7cbe64fSgw 	}
1346e7cbe64fSgw 
1347510b6c0eSNeil Perrin 	sync = !(zv->zv_flags & ZVOL_WCE) && !zil_disable;
1348510b6c0eSNeil Perrin 
1349c2e6a7d6Sperrin 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1350c2e6a7d6Sperrin 	    RL_WRITER);
135173ec3d9cSgw 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1352feb08c6bSbillm 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1353feb08c6bSbillm 		uint64_t off = uio->uio_loffset;
1354feb08c6bSbillm 		dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
135573ec3d9cSgw 
135673ec3d9cSgw 		if (bytes > volsize - off)	/* don't write past the end */
135773ec3d9cSgw 			bytes = volsize - off;
135873ec3d9cSgw 
1359feb08c6bSbillm 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
1360feb08c6bSbillm 		error = dmu_tx_assign(tx, TXG_WAIT);
1361feb08c6bSbillm 		if (error) {
1362feb08c6bSbillm 			dmu_tx_abort(tx);
1363feb08c6bSbillm 			break;
1364feb08c6bSbillm 		}
1365feb08c6bSbillm 		error = dmu_write_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes, tx);
1366feb08c6bSbillm 		if (error == 0)
1367510b6c0eSNeil Perrin 			zvol_log_write(zv, tx, off, bytes, sync);
1368feb08c6bSbillm 		dmu_tx_commit(tx);
1369feb08c6bSbillm 
1370feb08c6bSbillm 		if (error)
1371feb08c6bSbillm 			break;
1372feb08c6bSbillm 	}
1373c2e6a7d6Sperrin 	zfs_range_unlock(rl);
1374510b6c0eSNeil Perrin 	if (sync)
1375e08bf2c6SEric Taylor 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1376feb08c6bSbillm 	return (error);
1377fa9e4066Sahrens }
1378fa9e4066Sahrens 
1379c7f714e2SEric Taylor int
1380c7f714e2SEric Taylor zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs)
1381c7f714e2SEric Taylor {
1382c7f714e2SEric Taylor 	struct uuid uuid = EFI_RESERVED;
1383c7f714e2SEric Taylor 	efi_gpe_t gpe = { 0 };
1384c7f714e2SEric Taylor 	uint32_t crc;
1385c7f714e2SEric Taylor 	dk_efi_t efi;
1386c7f714e2SEric Taylor 	int length;
1387c7f714e2SEric Taylor 	char *ptr;
1388c7f714e2SEric Taylor 
1389c7f714e2SEric Taylor 	if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag))
1390c7f714e2SEric Taylor 		return (EFAULT);
1391c7f714e2SEric Taylor 	ptr = (char *)(uintptr_t)efi.dki_data_64;
1392c7f714e2SEric Taylor 	length = efi.dki_length;
1393c7f714e2SEric Taylor 	/*
1394c7f714e2SEric Taylor 	 * Some clients may attempt to request a PMBR for the
1395c7f714e2SEric Taylor 	 * zvol.  Currently this interface will return EINVAL to
1396c7f714e2SEric Taylor 	 * such requests.  These requests could be supported by
1397c7f714e2SEric Taylor 	 * adding a check for lba == 0 and consing up an appropriate
1398c7f714e2SEric Taylor 	 * PMBR.
1399c7f714e2SEric Taylor 	 */
1400c7f714e2SEric Taylor 	if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0)
1401c7f714e2SEric Taylor 		return (EINVAL);
1402c7f714e2SEric Taylor 
1403c7f714e2SEric Taylor 	gpe.efi_gpe_StartingLBA = LE_64(34ULL);
1404c7f714e2SEric Taylor 	gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1);
1405c7f714e2SEric Taylor 	UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
1406c7f714e2SEric Taylor 
1407c7f714e2SEric Taylor 	if (efi.dki_lba == 1) {
1408c7f714e2SEric Taylor 		efi_gpt_t gpt = { 0 };
1409c7f714e2SEric Taylor 
1410c7f714e2SEric Taylor 		gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
1411c7f714e2SEric Taylor 		gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
1412c7f714e2SEric Taylor 		gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt));
1413c7f714e2SEric Taylor 		gpt.efi_gpt_MyLBA = LE_64(1ULL);
1414c7f714e2SEric Taylor 		gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL);
1415c7f714e2SEric Taylor 		gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1);
1416c7f714e2SEric Taylor 		gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL);
1417c7f714e2SEric Taylor 		gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
1418c7f714e2SEric Taylor 		gpt.efi_gpt_SizeOfPartitionEntry =
1419c7f714e2SEric Taylor 		    LE_32(sizeof (efi_gpe_t));
1420c7f714e2SEric Taylor 		CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
1421c7f714e2SEric Taylor 		gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
1422c7f714e2SEric Taylor 		CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table);
1423c7f714e2SEric Taylor 		gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
1424c7f714e2SEric Taylor 		if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length),
1425c7f714e2SEric Taylor 		    flag))
1426c7f714e2SEric Taylor 			return (EFAULT);
1427c7f714e2SEric Taylor 		ptr += sizeof (gpt);
1428c7f714e2SEric Taylor 		length -= sizeof (gpt);
1429c7f714e2SEric Taylor 	}
1430c7f714e2SEric Taylor 	if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe),
1431c7f714e2SEric Taylor 	    length), flag))
1432c7f714e2SEric Taylor 		return (EFAULT);
1433c7f714e2SEric Taylor 	return (0);
1434c7f714e2SEric Taylor }
1435c7f714e2SEric Taylor 
1436fa9e4066Sahrens /*
1437fa9e4066Sahrens  * Dirtbag ioctls to support mkfs(1M) for UFS filesystems.  See dkio(7I).
1438fa9e4066Sahrens  */
1439fa9e4066Sahrens /*ARGSUSED*/
1440fa9e4066Sahrens int
1441fa9e4066Sahrens zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
1442fa9e4066Sahrens {
1443fa9e4066Sahrens 	zvol_state_t *zv;
1444af2c4821Smaybee 	struct dk_cinfo dki;
1445fa9e4066Sahrens 	struct dk_minfo dkm;
1446af2c4821Smaybee 	struct dk_callback *dkc;
1447fa9e4066Sahrens 	int error = 0;
1448e7cbe64fSgw 	rl_t *rl;
1449fa9e4066Sahrens 
1450fa9e4066Sahrens 	mutex_enter(&zvol_state_lock);
1451fa9e4066Sahrens 
1452fa9e4066Sahrens 	zv = ddi_get_soft_state(zvol_state, getminor(dev));
1453fa9e4066Sahrens 
1454fa9e4066Sahrens 	if (zv == NULL) {
1455fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
1456fa9e4066Sahrens 		return (ENXIO);
1457fa9e4066Sahrens 	}
1458701f66c4SEric Taylor 	ASSERT(zv->zv_total_opens > 0);
1459fa9e4066Sahrens 
1460fa9e4066Sahrens 	switch (cmd) {
1461fa9e4066Sahrens 
1462fa9e4066Sahrens 	case DKIOCINFO:
1463af2c4821Smaybee 		bzero(&dki, sizeof (dki));
1464af2c4821Smaybee 		(void) strcpy(dki.dki_cname, "zvol");
1465af2c4821Smaybee 		(void) strcpy(dki.dki_dname, "zvol");
1466af2c4821Smaybee 		dki.dki_ctype = DKC_UNKNOWN;
1467af2c4821Smaybee 		dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs);
1468fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
1469af2c4821Smaybee 		if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag))
1470fa9e4066Sahrens 			error = EFAULT;
1471fa9e4066Sahrens 		return (error);
1472fa9e4066Sahrens 
1473fa9e4066Sahrens 	case DKIOCGMEDIAINFO:
1474fa9e4066Sahrens 		bzero(&dkm, sizeof (dkm));
1475fa9e4066Sahrens 		dkm.dki_lbsize = 1U << zv->zv_min_bs;
1476fa9e4066Sahrens 		dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
1477fa9e4066Sahrens 		dkm.dki_media_type = DK_UNKNOWN;
1478fa9e4066Sahrens 		mutex_exit(&zvol_state_lock);
1479fa9e4066Sahrens 		if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
1480fa9e4066Sahrens 			error = EFAULT;
1481fa9e4066Sahrens 		return (error);
1482fa9e4066Sahrens 
1483fa9e4066Sahrens 	case DKIOCGETEFI:
1484c7f714e2SEric Taylor 		{
1485c7f714e2SEric Taylor 			uint64_t vs = zv->zv_volsize;
1486c7f714e2SEric Taylor 			uint8_t bs = zv->zv_min_bs;
1487fa9e4066Sahrens 
148868a5ac4dSmaybee 			mutex_exit(&zvol_state_lock);
1489c7f714e2SEric Taylor 			error = zvol_getefi((void *)arg, flag, vs, bs);
1490c7f714e2SEric Taylor 			return (error);
149168a5ac4dSmaybee 		}
1492fa9e4066Sahrens 
1493feb08c6bSbillm 	case DKIOCFLUSHWRITECACHE:
1494af2c4821Smaybee 		dkc = (struct dk_callback *)arg;
1495701f66c4SEric Taylor 		mutex_exit(&zvol_state_lock);
1496feb08c6bSbillm 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1497af2c4821Smaybee 		if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) {
1498af2c4821Smaybee 			(*dkc->dkc_callback)(dkc->dkc_cookie, error);
1499af2c4821Smaybee 			error = 0;
1500af2c4821Smaybee 		}
1501701f66c4SEric Taylor 		return (error);
1502701f66c4SEric Taylor 
1503701f66c4SEric Taylor 	case DKIOCGETWCE:
1504701f66c4SEric Taylor 		{
1505701f66c4SEric Taylor 			int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0;
1506701f66c4SEric Taylor 			if (ddi_copyout(&wce, (void *)arg, sizeof (int),
1507701f66c4SEric Taylor 			    flag))
1508701f66c4SEric Taylor 				error = EFAULT;
1509701f66c4SEric Taylor 			break;
1510701f66c4SEric Taylor 		}
1511701f66c4SEric Taylor 	case DKIOCSETWCE:
1512701f66c4SEric Taylor 		{
1513701f66c4SEric Taylor 			int wce;
1514701f66c4SEric Taylor 			if (ddi_copyin((void *)arg, &wce, sizeof (int),
1515701f66c4SEric Taylor 			    flag)) {
1516701f66c4SEric Taylor 				error = EFAULT;
1517701f66c4SEric Taylor 				break;
1518701f66c4SEric Taylor 			}
1519701f66c4SEric Taylor 			if (wce) {
1520701f66c4SEric Taylor 				zv->zv_flags |= ZVOL_WCE;
1521701f66c4SEric Taylor 				mutex_exit(&zvol_state_lock);
1522701f66c4SEric Taylor 			} else {
1523701f66c4SEric Taylor 				zv->zv_flags &= ~ZVOL_WCE;
1524701f66c4SEric Taylor 				mutex_exit(&zvol_state_lock);
1525701f66c4SEric Taylor 				zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1526701f66c4SEric Taylor 			}
1527701f66c4SEric Taylor 			return (0);
1528701f66c4SEric Taylor 		}
1529feb08c6bSbillm 
1530b6130eadSmaybee 	case DKIOCGGEOM:
1531b6130eadSmaybee 	case DKIOCGVTOC:
1532e7cbe64fSgw 		/*
1533e7cbe64fSgw 		 * commands using these (like prtvtoc) expect ENOTSUP
1534e7cbe64fSgw 		 * since we're emulating an EFI label
1535e7cbe64fSgw 		 */
1536b6130eadSmaybee 		error = ENOTSUP;
1537b6130eadSmaybee 		break;
1538b6130eadSmaybee 
1539e7cbe64fSgw 	case DKIOCDUMPINIT:
1540e7cbe64fSgw 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1541e7cbe64fSgw 		    RL_WRITER);
1542e7cbe64fSgw 		error = zvol_dumpify(zv);
1543e7cbe64fSgw 		zfs_range_unlock(rl);
1544e7cbe64fSgw 		break;
1545e7cbe64fSgw 
1546e7cbe64fSgw 	case DKIOCDUMPFINI:
154706d5ae10SEric Taylor 		if (!(zv->zv_flags & ZVOL_DUMPIFIED))
154806d5ae10SEric Taylor 			break;
1549e7cbe64fSgw 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1550e7cbe64fSgw 		    RL_WRITER);
1551e7cbe64fSgw 		error = zvol_dump_fini(zv);
1552e7cbe64fSgw 		zfs_range_unlock(rl);
1553e7cbe64fSgw 		break;
1554e7cbe64fSgw 
1555fa9e4066Sahrens 	default:
155668a5ac4dSmaybee 		error = ENOTTY;
1557fa9e4066Sahrens 		break;
1558fa9e4066Sahrens 
1559fa9e4066Sahrens 	}
1560fa9e4066Sahrens 	mutex_exit(&zvol_state_lock);
1561fa9e4066Sahrens 	return (error);
1562fa9e4066Sahrens }
1563fa9e4066Sahrens 
1564fa9e4066Sahrens int
1565fa9e4066Sahrens zvol_busy(void)
1566fa9e4066Sahrens {
1567fa9e4066Sahrens 	return (zvol_minors != 0);
1568fa9e4066Sahrens }
1569fa9e4066Sahrens 
1570fa9e4066Sahrens void
1571fa9e4066Sahrens zvol_init(void)
1572fa9e4066Sahrens {
1573fa9e4066Sahrens 	VERIFY(ddi_soft_state_init(&zvol_state, sizeof (zvol_state_t), 1) == 0);
1574fa9e4066Sahrens 	mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
1575fa9e4066Sahrens }
1576fa9e4066Sahrens 
1577fa9e4066Sahrens void
1578fa9e4066Sahrens zvol_fini(void)
1579fa9e4066Sahrens {
1580fa9e4066Sahrens 	mutex_destroy(&zvol_state_lock);
1581fa9e4066Sahrens 	ddi_soft_state_fini(&zvol_state);
1582fa9e4066Sahrens }
1583e7cbe64fSgw 
1584e7cbe64fSgw static int
1585e7cbe64fSgw zvol_dump_init(zvol_state_t *zv, boolean_t resize)
1586e7cbe64fSgw {
1587e7cbe64fSgw 	dmu_tx_t *tx;
1588e7cbe64fSgw 	int error = 0;
1589e7cbe64fSgw 	objset_t *os = zv->zv_objset;
1590e7cbe64fSgw 	nvlist_t *nv = NULL;
1591e7cbe64fSgw 
1592e7cbe64fSgw 	ASSERT(MUTEX_HELD(&zvol_state_lock));
1593681d9761SEric Taylor 	error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0,
1594681d9761SEric Taylor 	    DMU_OBJECT_END);
1595681d9761SEric Taylor 	/* wait for dmu_free_long_range to actually free the blocks */
1596681d9761SEric Taylor 	txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
1597e7cbe64fSgw 
1598e7cbe64fSgw 	tx = dmu_tx_create(os);
1599e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1600681d9761SEric Taylor 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
1601e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
1602e7cbe64fSgw 	if (error) {
1603e7cbe64fSgw 		dmu_tx_abort(tx);
1604e7cbe64fSgw 		return (error);
1605e7cbe64fSgw 	}
1606e7cbe64fSgw 
1607e7cbe64fSgw 	/*
1608e7cbe64fSgw 	 * If we are resizing the dump device then we only need to
1609e7cbe64fSgw 	 * update the refreservation to match the newly updated
1610e7cbe64fSgw 	 * zvolsize. Otherwise, we save off the original state of the
1611e7cbe64fSgw 	 * zvol so that we can restore them if the zvol is ever undumpified.
1612e7cbe64fSgw 	 */
1613e7cbe64fSgw 	if (resize) {
1614e7cbe64fSgw 		error = zap_update(os, ZVOL_ZAP_OBJ,
1615e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1616e7cbe64fSgw 		    &zv->zv_volsize, tx);
1617e7cbe64fSgw 	} else {
161888b7b0f2SMatthew Ahrens 		uint64_t checksum, compress, refresrv, vbs;
161988b7b0f2SMatthew Ahrens 
1620e7cbe64fSgw 		error = dsl_prop_get_integer(zv->zv_name,
1621e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL);
1622e7cbe64fSgw 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
1623e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL);
1624e7cbe64fSgw 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
1625e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL);
162688b7b0f2SMatthew Ahrens 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
162788b7b0f2SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL);
1628e7cbe64fSgw 
1629e7cbe64fSgw 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1630e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1,
1631e7cbe64fSgw 		    &compress, tx);
1632e7cbe64fSgw 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1633e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx);
1634e7cbe64fSgw 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1635e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1636e7cbe64fSgw 		    &refresrv, tx);
163788b7b0f2SMatthew Ahrens 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
163888b7b0f2SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1,
163988b7b0f2SMatthew Ahrens 		    &vbs, tx);
1640681d9761SEric Taylor 		error = error ? error : dmu_object_set_blocksize(
1641681d9761SEric Taylor 		    os, ZVOL_OBJ, SPA_MAXBLOCKSIZE, 0, tx);
1642681d9761SEric Taylor 		if (error == 0)
1643681d9761SEric Taylor 			zv->zv_volblocksize = SPA_MAXBLOCKSIZE;
1644e7cbe64fSgw 	}
1645e7cbe64fSgw 	dmu_tx_commit(tx);
1646e7cbe64fSgw 
1647e7cbe64fSgw 	/*
1648e7cbe64fSgw 	 * We only need update the zvol's property if we are initializing
1649e7cbe64fSgw 	 * the dump area for the first time.
1650e7cbe64fSgw 	 */
1651e7cbe64fSgw 	if (!resize) {
1652e7cbe64fSgw 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1653e7cbe64fSgw 		VERIFY(nvlist_add_uint64(nv,
1654e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0);
1655e7cbe64fSgw 		VERIFY(nvlist_add_uint64(nv,
1656e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
1657e7cbe64fSgw 		    ZIO_COMPRESS_OFF) == 0);
1658e7cbe64fSgw 		VERIFY(nvlist_add_uint64(nv,
1659e7cbe64fSgw 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
1660e7cbe64fSgw 		    ZIO_CHECKSUM_OFF) == 0);
1661e7cbe64fSgw 
1662e7cbe64fSgw 		error = zfs_set_prop_nvlist(zv->zv_name, nv);
1663e7cbe64fSgw 		nvlist_free(nv);
1664e7cbe64fSgw 
1665e7cbe64fSgw 		if (error)
1666e7cbe64fSgw 			return (error);
1667e7cbe64fSgw 	}
1668e7cbe64fSgw 
1669e7cbe64fSgw 	/* Allocate the space for the dump */
1670e7cbe64fSgw 	error = zvol_prealloc(zv);
1671e7cbe64fSgw 	return (error);
1672e7cbe64fSgw }
1673e7cbe64fSgw 
1674e7cbe64fSgw static int
1675e7cbe64fSgw zvol_dumpify(zvol_state_t *zv)
1676e7cbe64fSgw {
1677e7cbe64fSgw 	int error = 0;
1678e7cbe64fSgw 	uint64_t dumpsize = 0;
1679e7cbe64fSgw 	dmu_tx_t *tx;
1680e7cbe64fSgw 	objset_t *os = zv->zv_objset;
1681e7cbe64fSgw 
1682681d9761SEric Taylor 	if (zv->zv_flags & ZVOL_RDONLY)
1683e7cbe64fSgw 		return (EROFS);
1684e7cbe64fSgw 
1685e7cbe64fSgw 	if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE,
1686e7cbe64fSgw 	    8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) {
1687e7cbe64fSgw 		boolean_t resize = (dumpsize > 0) ? B_TRUE : B_FALSE;
1688e7cbe64fSgw 
1689e7cbe64fSgw 		if ((error = zvol_dump_init(zv, resize)) != 0) {
1690e7cbe64fSgw 			(void) zvol_dump_fini(zv);
1691e7cbe64fSgw 			return (error);
1692e7cbe64fSgw 		}
1693e7cbe64fSgw 	}
1694e7cbe64fSgw 
1695e7cbe64fSgw 	/*
1696e7cbe64fSgw 	 * Build up our lba mapping.
1697e7cbe64fSgw 	 */
1698e7cbe64fSgw 	error = zvol_get_lbas(zv);
1699e7cbe64fSgw 	if (error) {
1700e7cbe64fSgw 		(void) zvol_dump_fini(zv);
1701e7cbe64fSgw 		return (error);
1702e7cbe64fSgw 	}
1703e7cbe64fSgw 
1704e7cbe64fSgw 	tx = dmu_tx_create(os);
1705e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1706e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
1707e7cbe64fSgw 	if (error) {
1708e7cbe64fSgw 		dmu_tx_abort(tx);
1709e7cbe64fSgw 		(void) zvol_dump_fini(zv);
1710e7cbe64fSgw 		return (error);
1711e7cbe64fSgw 	}
1712e7cbe64fSgw 
1713e7cbe64fSgw 	zv->zv_flags |= ZVOL_DUMPIFIED;
1714e7cbe64fSgw 	error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1,
1715e7cbe64fSgw 	    &zv->zv_volsize, tx);
1716e7cbe64fSgw 	dmu_tx_commit(tx);
1717e7cbe64fSgw 
1718e7cbe64fSgw 	if (error) {
1719e7cbe64fSgw 		(void) zvol_dump_fini(zv);
1720e7cbe64fSgw 		return (error);
1721e7cbe64fSgw 	}
1722e7cbe64fSgw 
1723e7cbe64fSgw 	txg_wait_synced(dmu_objset_pool(os), 0);
1724e7cbe64fSgw 	return (0);
1725e7cbe64fSgw }
1726e7cbe64fSgw 
1727e7cbe64fSgw static int
1728e7cbe64fSgw zvol_dump_fini(zvol_state_t *zv)
1729e7cbe64fSgw {
1730e7cbe64fSgw 	dmu_tx_t *tx;
1731e7cbe64fSgw 	objset_t *os = zv->zv_objset;
1732e7cbe64fSgw 	nvlist_t *nv;
1733e7cbe64fSgw 	int error = 0;
173488b7b0f2SMatthew Ahrens 	uint64_t checksum, compress, refresrv, vbs;
1735e7cbe64fSgw 
1736b7e50089Smaybee 	/*
1737b7e50089Smaybee 	 * Attempt to restore the zvol back to its pre-dumpified state.
1738b7e50089Smaybee 	 * This is a best-effort attempt as it's possible that not all
1739b7e50089Smaybee 	 * of these properties were initialized during the dumpify process
1740b7e50089Smaybee 	 * (i.e. error during zvol_dump_init).
1741b7e50089Smaybee 	 */
1742b7e50089Smaybee 
1743e7cbe64fSgw 	tx = dmu_tx_create(os);
1744e7cbe64fSgw 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1745e7cbe64fSgw 	error = dmu_tx_assign(tx, TXG_WAIT);
1746e7cbe64fSgw 	if (error) {
1747e7cbe64fSgw 		dmu_tx_abort(tx);
1748e7cbe64fSgw 		return (error);
1749e7cbe64fSgw 	}
1750b7e50089Smaybee 	(void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx);
1751b7e50089Smaybee 	dmu_tx_commit(tx);
1752e7cbe64fSgw 
1753e7cbe64fSgw 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1754e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum);
1755e7cbe64fSgw 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1756e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress);
1757e7cbe64fSgw 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1758e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv);
175988b7b0f2SMatthew Ahrens 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
176088b7b0f2SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs);
1761e7cbe64fSgw 
1762e7cbe64fSgw 	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1763e7cbe64fSgw 	(void) nvlist_add_uint64(nv,
1764e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum);
1765e7cbe64fSgw 	(void) nvlist_add_uint64(nv,
1766e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress);
1767e7cbe64fSgw 	(void) nvlist_add_uint64(nv,
1768e7cbe64fSgw 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv);
1769e7cbe64fSgw 	(void) zfs_set_prop_nvlist(zv->zv_name, nv);
1770e7cbe64fSgw 	nvlist_free(nv);
1771e7cbe64fSgw 
1772b7e50089Smaybee 	zvol_free_extents(zv);
1773b7e50089Smaybee 	zv->zv_flags &= ~ZVOL_DUMPIFIED;
1774b7e50089Smaybee 	(void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END);
1775681d9761SEric Taylor 	/* wait for dmu_free_long_range to actually free the blocks */
1776681d9761SEric Taylor 	txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
1777681d9761SEric Taylor 	tx = dmu_tx_create(os);
1778681d9761SEric Taylor 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
1779681d9761SEric Taylor 	error = dmu_tx_assign(tx, TXG_WAIT);
1780681d9761SEric Taylor 	if (error) {
1781681d9761SEric Taylor 		dmu_tx_abort(tx);
1782681d9761SEric Taylor 		return (error);
1783681d9761SEric Taylor 	}
1784*b24ab676SJeff Bonwick 	if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0)
1785*b24ab676SJeff Bonwick 		zv->zv_volblocksize = vbs;
1786681d9761SEric Taylor 	dmu_tx_commit(tx);
1787b7e50089Smaybee 
1788e7cbe64fSgw 	return (0);
1789e7cbe64fSgw }
1790