xref: /titanic_44/usr/src/uts/common/fs/zfs/zvol.c (revision 7ff178cd8db129d385d3177eb20744d3b6efc59b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 /* Portions Copyright 2010 Robert Milkowski */
26 
27 /*
28  * ZFS volume emulation driver.
29  *
30  * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes.
31  * Volumes are accessed through the symbolic links named:
32  *
33  * /dev/zvol/dsk/<pool_name>/<dataset_name>
34  * /dev/zvol/rdsk/<pool_name>/<dataset_name>
35  *
36  * These links are created by the /dev filesystem (sdev_zvolops.c).
37  * Volumes are persistent through reboot.  No user command needs to be
38  * run before opening and using a device.
39  */
40 
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <sys/errno.h>
44 #include <sys/uio.h>
45 #include <sys/buf.h>
46 #include <sys/modctl.h>
47 #include <sys/open.h>
48 #include <sys/kmem.h>
49 #include <sys/conf.h>
50 #include <sys/cmn_err.h>
51 #include <sys/stat.h>
52 #include <sys/zap.h>
53 #include <sys/spa.h>
54 #include <sys/zio.h>
55 #include <sys/dmu_traverse.h>
56 #include <sys/dnode.h>
57 #include <sys/dsl_dataset.h>
58 #include <sys/dsl_prop.h>
59 #include <sys/dkio.h>
60 #include <sys/efi_partition.h>
61 #include <sys/byteorder.h>
62 #include <sys/pathname.h>
63 #include <sys/ddi.h>
64 #include <sys/sunddi.h>
65 #include <sys/crc32.h>
66 #include <sys/dirent.h>
67 #include <sys/policy.h>
68 #include <sys/fs/zfs.h>
69 #include <sys/zfs_ioctl.h>
70 #include <sys/mkdev.h>
71 #include <sys/zil.h>
72 #include <sys/refcount.h>
73 #include <sys/zfs_znode.h>
74 #include <sys/zfs_rlock.h>
75 #include <sys/vdev_disk.h>
76 #include <sys/vdev_impl.h>
77 #include <sys/zvol.h>
78 #include <sys/dumphdr.h>
79 #include <sys/zil_impl.h>
80 
81 #include "zfs_namecheck.h"
82 
83 void *zfsdev_state;
84 static char *zvol_tag = "zvol_tag";
85 
86 #define	ZVOL_DUMPSIZE		"dumpsize"
87 
88 /*
89  * This lock protects the zfsdev_state structure from being modified
90  * while it's being used, e.g. an open that comes in before a create
91  * finishes.  It also protects temporary opens of the dataset so that,
92  * e.g., an open doesn't get a spurious EBUSY.
93  */
94 kmutex_t zfsdev_state_lock;
95 static uint32_t zvol_minors;
96 
97 typedef struct zvol_extent {
98 	list_node_t	ze_node;
99 	dva_t		ze_dva;		/* dva associated with this extent */
100 	uint64_t	ze_nblks;	/* number of blocks in extent */
101 } zvol_extent_t;
102 
103 /*
104  * The in-core state of each volume.
105  */
106 typedef struct zvol_state {
107 	char		zv_name[MAXPATHLEN]; /* pool/dd name */
108 	uint64_t	zv_volsize;	/* amount of space we advertise */
109 	uint64_t	zv_volblocksize; /* volume block size */
110 	minor_t		zv_minor;	/* minor number */
111 	uint8_t		zv_min_bs;	/* minimum addressable block shift */
112 	uint8_t		zv_flags;	/* readonly, dumpified, etc. */
113 	objset_t	*zv_objset;	/* objset handle */
114 	uint32_t	zv_open_count[OTYPCNT];	/* open counts */
115 	uint32_t	zv_total_opens;	/* total open count */
116 	zilog_t		*zv_zilog;	/* ZIL handle */
117 	list_t		zv_extents;	/* List of extents for dump */
118 	znode_t		zv_znode;	/* for range locking */
119 	dmu_buf_t	*zv_dbuf;	/* bonus handle */
120 } zvol_state_t;
121 
122 /*
123  * zvol specific flags
124  */
125 #define	ZVOL_RDONLY	0x1
126 #define	ZVOL_DUMPIFIED	0x2
127 #define	ZVOL_EXCL	0x4
128 #define	ZVOL_WCE	0x8
129 
130 /*
131  * zvol maximum transfer in one DMU tx.
132  */
133 int zvol_maxphys = DMU_MAX_ACCESS/2;
134 
135 extern int zfs_set_prop_nvlist(const char *, zprop_source_t,
136     nvlist_t *, nvlist_t **);
137 static int zvol_remove_zv(zvol_state_t *);
138 static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio);
139 static int zvol_dumpify(zvol_state_t *zv);
140 static int zvol_dump_fini(zvol_state_t *zv);
141 static int zvol_dump_init(zvol_state_t *zv, boolean_t resize);
142 
143 static void
144 zvol_size_changed(uint64_t volsize, major_t maj, minor_t min)
145 {
146 	dev_t dev = makedevice(maj, min);
147 
148 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
149 	    "Size", volsize) == DDI_SUCCESS);
150 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
151 	    "Nblocks", lbtodb(volsize)) == DDI_SUCCESS);
152 
153 	/* Notify specfs to invalidate the cached size */
154 	spec_size_invalidate(dev, VBLK);
155 	spec_size_invalidate(dev, VCHR);
156 }
157 
158 int
159 zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
160 {
161 	if (volsize == 0)
162 		return (EINVAL);
163 
164 	if (volsize % blocksize != 0)
165 		return (EINVAL);
166 
167 #ifdef _ILP32
168 	if (volsize - 1 > SPEC_MAXOFFSET_T)
169 		return (EOVERFLOW);
170 #endif
171 	return (0);
172 }
173 
174 int
175 zvol_check_volblocksize(uint64_t volblocksize)
176 {
177 	if (volblocksize < SPA_MINBLOCKSIZE ||
178 	    volblocksize > SPA_MAXBLOCKSIZE ||
179 	    !ISP2(volblocksize))
180 		return (EDOM);
181 
182 	return (0);
183 }
184 
185 int
186 zvol_get_stats(objset_t *os, nvlist_t *nv)
187 {
188 	int error;
189 	dmu_object_info_t doi;
190 	uint64_t val;
191 
192 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
193 	if (error)
194 		return (error);
195 
196 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
197 
198 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
199 
200 	if (error == 0) {
201 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
202 		    doi.doi_data_block_size);
203 	}
204 
205 	return (error);
206 }
207 
208 static zvol_state_t *
209 zvol_minor_lookup(const char *name)
210 {
211 	minor_t minor;
212 	zvol_state_t *zv;
213 
214 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
215 
216 	for (minor = 1; minor <= ZFSDEV_MAX_MINOR; minor++) {
217 		zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
218 		if (zv == NULL)
219 			continue;
220 		if (strcmp(zv->zv_name, name) == 0)
221 			return (zv);
222 	}
223 
224 	return (NULL);
225 }
226 
227 /* extent mapping arg */
228 struct maparg {
229 	zvol_state_t	*ma_zv;
230 	uint64_t	ma_blks;
231 };
232 
233 /*ARGSUSED*/
234 static int
235 zvol_map_block(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, arc_buf_t *pbuf,
236     const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
237 {
238 	struct maparg *ma = arg;
239 	zvol_extent_t *ze;
240 	int bs = ma->ma_zv->zv_volblocksize;
241 
242 	if (bp == NULL || zb->zb_object != ZVOL_OBJ || zb->zb_level != 0)
243 		return (0);
244 
245 	VERIFY3U(ma->ma_blks, ==, zb->zb_blkid);
246 	ma->ma_blks++;
247 
248 	/* Abort immediately if we have encountered gang blocks */
249 	if (BP_IS_GANG(bp))
250 		return (EFRAGS);
251 
252 	/*
253 	 * See if the block is at the end of the previous extent.
254 	 */
255 	ze = list_tail(&ma->ma_zv->zv_extents);
256 	if (ze &&
257 	    DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) &&
258 	    DVA_GET_OFFSET(BP_IDENTITY(bp)) ==
259 	    DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) {
260 		ze->ze_nblks++;
261 		return (0);
262 	}
263 
264 	dprintf_bp(bp, "%s", "next blkptr:");
265 
266 	/* start a new extent */
267 	ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP);
268 	ze->ze_dva = bp->blk_dva[0];	/* structure assignment */
269 	ze->ze_nblks = 1;
270 	list_insert_tail(&ma->ma_zv->zv_extents, ze);
271 	return (0);
272 }
273 
274 static void
275 zvol_free_extents(zvol_state_t *zv)
276 {
277 	zvol_extent_t *ze;
278 
279 	while (ze = list_head(&zv->zv_extents)) {
280 		list_remove(&zv->zv_extents, ze);
281 		kmem_free(ze, sizeof (zvol_extent_t));
282 	}
283 }
284 
285 static int
286 zvol_get_lbas(zvol_state_t *zv)
287 {
288 	objset_t *os = zv->zv_objset;
289 	struct maparg	ma;
290 	int		err;
291 
292 	ma.ma_zv = zv;
293 	ma.ma_blks = 0;
294 	zvol_free_extents(zv);
295 
296 	/* commit any in-flight changes before traversing the dataset */
297 	txg_wait_synced(dmu_objset_pool(os), 0);
298 	err = traverse_dataset(dmu_objset_ds(os), 0,
299 	    TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma);
300 	if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) {
301 		zvol_free_extents(zv);
302 		return (err ? err : EIO);
303 	}
304 
305 	return (0);
306 }
307 
308 /* ARGSUSED */
309 void
310 zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
311 {
312 	zfs_creat_t *zct = arg;
313 	nvlist_t *nvprops = zct->zct_props;
314 	int error;
315 	uint64_t volblocksize, volsize;
316 
317 	VERIFY(nvlist_lookup_uint64(nvprops,
318 	    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
319 	if (nvlist_lookup_uint64(nvprops,
320 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
321 		volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
322 
323 	/*
324 	 * These properties must be removed from the list so the generic
325 	 * property setting step won't apply to them.
326 	 */
327 	VERIFY(nvlist_remove_all(nvprops,
328 	    zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
329 	(void) nvlist_remove_all(nvprops,
330 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
331 
332 	error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
333 	    DMU_OT_NONE, 0, tx);
334 	ASSERT(error == 0);
335 
336 	error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
337 	    DMU_OT_NONE, 0, tx);
338 	ASSERT(error == 0);
339 
340 	error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
341 	ASSERT(error == 0);
342 }
343 
344 /*
345  * Replay a TX_WRITE ZIL transaction that didn't get committed
346  * after a system failure
347  */
348 static int
349 zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
350 {
351 	objset_t *os = zv->zv_objset;
352 	char *data = (char *)(lr + 1);	/* data follows lr_write_t */
353 	uint64_t offset, length;
354 	dmu_tx_t *tx;
355 	int error;
356 
357 	if (byteswap)
358 		byteswap_uint64_array(lr, sizeof (*lr));
359 
360 	offset = lr->lr_offset;
361 	length = lr->lr_length;
362 
363 	/* If it's a dmu_sync() block, write the whole block */
364 	if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
365 		uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
366 		if (length < blocksize) {
367 			offset -= offset % blocksize;
368 			length = blocksize;
369 		}
370 	}
371 
372 	tx = dmu_tx_create(os);
373 	dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length);
374 	error = dmu_tx_assign(tx, TXG_WAIT);
375 	if (error) {
376 		dmu_tx_abort(tx);
377 	} else {
378 		dmu_write(os, ZVOL_OBJ, offset, length, data, tx);
379 		dmu_tx_commit(tx);
380 	}
381 
382 	return (error);
383 }
384 
385 /* ARGSUSED */
386 static int
387 zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
388 {
389 	return (ENOTSUP);
390 }
391 
392 /*
393  * Callback vectors for replaying records.
394  * Only TX_WRITE is needed for zvol.
395  */
396 zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = {
397 	zvol_replay_err,	/* 0 no such transaction type */
398 	zvol_replay_err,	/* TX_CREATE */
399 	zvol_replay_err,	/* TX_MKDIR */
400 	zvol_replay_err,	/* TX_MKXATTR */
401 	zvol_replay_err,	/* TX_SYMLINK */
402 	zvol_replay_err,	/* TX_REMOVE */
403 	zvol_replay_err,	/* TX_RMDIR */
404 	zvol_replay_err,	/* TX_LINK */
405 	zvol_replay_err,	/* TX_RENAME */
406 	zvol_replay_write,	/* TX_WRITE */
407 	zvol_replay_err,	/* TX_TRUNCATE */
408 	zvol_replay_err,	/* TX_SETATTR */
409 	zvol_replay_err,	/* TX_ACL */
410 	zvol_replay_err,	/* TX_CREATE_ACL */
411 	zvol_replay_err,	/* TX_CREATE_ATTR */
412 	zvol_replay_err,	/* TX_CREATE_ACL_ATTR */
413 	zvol_replay_err,	/* TX_MKDIR_ACL */
414 	zvol_replay_err,	/* TX_MKDIR_ATTR */
415 	zvol_replay_err,	/* TX_MKDIR_ACL_ATTR */
416 	zvol_replay_err,	/* TX_WRITE2 */
417 };
418 
419 int
420 zvol_name2minor(const char *name, minor_t *minor)
421 {
422 	zvol_state_t *zv;
423 
424 	mutex_enter(&zfsdev_state_lock);
425 	zv = zvol_minor_lookup(name);
426 	if (minor && zv)
427 		*minor = zv->zv_minor;
428 	mutex_exit(&zfsdev_state_lock);
429 	return (zv ? 0 : -1);
430 }
431 
432 /*
433  * Create a minor node (plus a whole lot more) for the specified volume.
434  */
435 int
436 zvol_create_minor(const char *name)
437 {
438 	zfs_soft_state_t *zs;
439 	zvol_state_t *zv;
440 	objset_t *os;
441 	dmu_object_info_t doi;
442 	minor_t minor = 0;
443 	char chrbuf[30], blkbuf[30];
444 	int error;
445 
446 	mutex_enter(&zfsdev_state_lock);
447 
448 	if (zvol_minor_lookup(name) != NULL) {
449 		mutex_exit(&zfsdev_state_lock);
450 		return (EEXIST);
451 	}
452 
453 	/* lie and say we're read-only */
454 	error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, zvol_tag, &os);
455 
456 	if (error) {
457 		mutex_exit(&zfsdev_state_lock);
458 		return (error);
459 	}
460 
461 	if ((minor = zfsdev_minor_alloc()) == 0) {
462 		dmu_objset_disown(os, zvol_tag);
463 		mutex_exit(&zfsdev_state_lock);
464 		return (ENXIO);
465 	}
466 
467 	if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS) {
468 		dmu_objset_disown(os, zvol_tag);
469 		mutex_exit(&zfsdev_state_lock);
470 		return (EAGAIN);
471 	}
472 	(void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME,
473 	    (char *)name);
474 
475 	(void) snprintf(chrbuf, sizeof (chrbuf), "%u,raw", minor);
476 
477 	if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR,
478 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
479 		ddi_soft_state_free(zfsdev_state, minor);
480 		dmu_objset_disown(os, zvol_tag);
481 		mutex_exit(&zfsdev_state_lock);
482 		return (EAGAIN);
483 	}
484 
485 	(void) snprintf(blkbuf, sizeof (blkbuf), "%u", minor);
486 
487 	if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK,
488 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
489 		ddi_remove_minor_node(zfs_dip, chrbuf);
490 		ddi_soft_state_free(zfsdev_state, minor);
491 		dmu_objset_disown(os, zvol_tag);
492 		mutex_exit(&zfsdev_state_lock);
493 		return (EAGAIN);
494 	}
495 
496 	zs = ddi_get_soft_state(zfsdev_state, minor);
497 	zs->zss_type = ZSST_ZVOL;
498 	zv = zs->zss_data = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP);
499 	(void) strlcpy(zv->zv_name, name, MAXPATHLEN);
500 	zv->zv_min_bs = DEV_BSHIFT;
501 	zv->zv_minor = minor;
502 	zv->zv_objset = os;
503 	if (dmu_objset_is_snapshot(os))
504 		zv->zv_flags |= ZVOL_RDONLY;
505 	mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
506 	avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
507 	    sizeof (rl_t), offsetof(rl_t, r_node));
508 	list_create(&zv->zv_extents, sizeof (zvol_extent_t),
509 	    offsetof(zvol_extent_t, ze_node));
510 	/* get and cache the blocksize */
511 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
512 	ASSERT(error == 0);
513 	zv->zv_volblocksize = doi.doi_data_block_size;
514 
515 	if (zil_replay_disable)
516 		zil_destroy(dmu_objset_zil(os), B_FALSE);
517 	else
518 		zil_replay(os, zv, zvol_replay_vector);
519 	dmu_objset_disown(os, zvol_tag);
520 	zv->zv_objset = NULL;
521 
522 	zvol_minors++;
523 
524 	mutex_exit(&zfsdev_state_lock);
525 
526 	return (0);
527 }
528 
529 /*
530  * Remove minor node for the specified volume.
531  */
532 static int
533 zvol_remove_zv(zvol_state_t *zv)
534 {
535 	char nmbuf[20];
536 	minor_t minor = zv->zv_minor;
537 
538 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
539 	if (zv->zv_total_opens != 0)
540 		return (EBUSY);
541 
542 	(void) snprintf(nmbuf, sizeof (nmbuf), "%u,raw", minor);
543 	ddi_remove_minor_node(zfs_dip, nmbuf);
544 
545 	(void) snprintf(nmbuf, sizeof (nmbuf), "%u", minor);
546 	ddi_remove_minor_node(zfs_dip, nmbuf);
547 
548 	avl_destroy(&zv->zv_znode.z_range_avl);
549 	mutex_destroy(&zv->zv_znode.z_range_lock);
550 
551 	kmem_free(zv, sizeof (zvol_state_t));
552 
553 	ddi_soft_state_free(zfsdev_state, minor);
554 
555 	zvol_minors--;
556 	return (0);
557 }
558 
559 int
560 zvol_remove_minor(const char *name)
561 {
562 	zvol_state_t *zv;
563 	int rc;
564 
565 	mutex_enter(&zfsdev_state_lock);
566 	if ((zv = zvol_minor_lookup(name)) == NULL) {
567 		mutex_exit(&zfsdev_state_lock);
568 		return (ENXIO);
569 	}
570 	rc = zvol_remove_zv(zv);
571 	mutex_exit(&zfsdev_state_lock);
572 	return (rc);
573 }
574 
575 int
576 zvol_first_open(zvol_state_t *zv)
577 {
578 	objset_t *os;
579 	uint64_t volsize;
580 	int error;
581 	uint64_t readonly;
582 
583 	/* lie and say we're read-only */
584 	error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, B_TRUE,
585 	    zvol_tag, &os);
586 	if (error)
587 		return (error);
588 
589 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
590 	if (error) {
591 		ASSERT(error == 0);
592 		dmu_objset_disown(os, zvol_tag);
593 		return (error);
594 	}
595 	zv->zv_objset = os;
596 	error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf);
597 	if (error) {
598 		dmu_objset_disown(os, zvol_tag);
599 		return (error);
600 	}
601 	zv->zv_volsize = volsize;
602 	zv->zv_zilog = zil_open(os, zvol_get_data);
603 	zvol_size_changed(zv->zv_volsize, ddi_driver_major(zfs_dip),
604 	    zv->zv_minor);
605 
606 	VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &readonly,
607 	    NULL) == 0);
608 	if (readonly || dmu_objset_is_snapshot(os))
609 		zv->zv_flags |= ZVOL_RDONLY;
610 	else
611 		zv->zv_flags &= ~ZVOL_RDONLY;
612 	return (error);
613 }
614 
615 void
616 zvol_last_close(zvol_state_t *zv)
617 {
618 	zil_close(zv->zv_zilog);
619 	zv->zv_zilog = NULL;
620 	dmu_buf_rele(zv->zv_dbuf, zvol_tag);
621 	zv->zv_dbuf = NULL;
622 	dmu_objset_disown(zv->zv_objset, zvol_tag);
623 	zv->zv_objset = NULL;
624 }
625 
626 int
627 zvol_prealloc(zvol_state_t *zv)
628 {
629 	objset_t *os = zv->zv_objset;
630 	dmu_tx_t *tx;
631 	uint64_t refd, avail, usedobjs, availobjs;
632 	uint64_t resid = zv->zv_volsize;
633 	uint64_t off = 0;
634 
635 	/* Check the space usage before attempting to allocate the space */
636 	dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs);
637 	if (avail < zv->zv_volsize)
638 		return (ENOSPC);
639 
640 	/* Free old extents if they exist */
641 	zvol_free_extents(zv);
642 
643 	while (resid != 0) {
644 		int error;
645 		uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE);
646 
647 		tx = dmu_tx_create(os);
648 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
649 		error = dmu_tx_assign(tx, TXG_WAIT);
650 		if (error) {
651 			dmu_tx_abort(tx);
652 			(void) dmu_free_long_range(os, ZVOL_OBJ, 0, off);
653 			return (error);
654 		}
655 		dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx);
656 		dmu_tx_commit(tx);
657 		off += bytes;
658 		resid -= bytes;
659 	}
660 	txg_wait_synced(dmu_objset_pool(os), 0);
661 
662 	return (0);
663 }
664 
665 int
666 zvol_update_volsize(objset_t *os, uint64_t volsize)
667 {
668 	dmu_tx_t *tx;
669 	int error;
670 
671 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
672 
673 	tx = dmu_tx_create(os);
674 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
675 	error = dmu_tx_assign(tx, TXG_WAIT);
676 	if (error) {
677 		dmu_tx_abort(tx);
678 		return (error);
679 	}
680 
681 	error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
682 	    &volsize, tx);
683 	dmu_tx_commit(tx);
684 
685 	if (error == 0)
686 		error = dmu_free_long_range(os,
687 		    ZVOL_OBJ, volsize, DMU_OBJECT_END);
688 	return (error);
689 }
690 
691 void
692 zvol_remove_minors(const char *name)
693 {
694 	zvol_state_t *zv;
695 	char *namebuf;
696 	minor_t minor;
697 
698 	namebuf = kmem_zalloc(strlen(name) + 2, KM_SLEEP);
699 	(void) strncpy(namebuf, name, strlen(name));
700 	(void) strcat(namebuf, "/");
701 	mutex_enter(&zfsdev_state_lock);
702 	for (minor = 1; minor <= ZFSDEV_MAX_MINOR; minor++) {
703 
704 		zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
705 		if (zv == NULL)
706 			continue;
707 		if (strncmp(namebuf, zv->zv_name, strlen(namebuf)) == 0)
708 			(void) zvol_remove_zv(zv);
709 	}
710 	kmem_free(namebuf, strlen(name) + 2);
711 
712 	mutex_exit(&zfsdev_state_lock);
713 }
714 
715 int
716 zvol_set_volsize(const char *name, major_t maj, uint64_t volsize)
717 {
718 	zvol_state_t *zv = NULL;
719 	objset_t *os;
720 	int error;
721 	dmu_object_info_t doi;
722 	uint64_t old_volsize = 0ULL;
723 	uint64_t readonly;
724 
725 	mutex_enter(&zfsdev_state_lock);
726 	zv = zvol_minor_lookup(name);
727 	if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) {
728 		mutex_exit(&zfsdev_state_lock);
729 		return (error);
730 	}
731 
732 	if ((error = dmu_object_info(os, ZVOL_OBJ, &doi)) != 0 ||
733 	    (error = zvol_check_volsize(volsize,
734 	    doi.doi_data_block_size)) != 0)
735 		goto out;
736 
737 	VERIFY(dsl_prop_get_integer(name, "readonly", &readonly,
738 	    NULL) == 0);
739 	if (readonly) {
740 		error = EROFS;
741 		goto out;
742 	}
743 
744 	error = zvol_update_volsize(os, volsize);
745 	/*
746 	 * Reinitialize the dump area to the new size. If we
747 	 * failed to resize the dump area then restore it back to
748 	 * its original size.
749 	 */
750 	if (zv && error == 0) {
751 		if (zv->zv_flags & ZVOL_DUMPIFIED) {
752 			old_volsize = zv->zv_volsize;
753 			zv->zv_volsize = volsize;
754 			if ((error = zvol_dumpify(zv)) != 0 ||
755 			    (error = dumpvp_resize()) != 0) {
756 				(void) zvol_update_volsize(os, old_volsize);
757 				zv->zv_volsize = old_volsize;
758 				error = zvol_dumpify(zv);
759 			}
760 		}
761 		if (error == 0) {
762 			zv->zv_volsize = volsize;
763 			zvol_size_changed(volsize, maj, zv->zv_minor);
764 		}
765 	}
766 
767 	/*
768 	 * Generate a LUN expansion event.
769 	 */
770 	if (zv && error == 0) {
771 		sysevent_id_t eid;
772 		nvlist_t *attr;
773 		char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
774 
775 		(void) snprintf(physpath, MAXPATHLEN, "%s%u", ZVOL_PSEUDO_DEV,
776 		    zv->zv_minor);
777 
778 		VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
779 		VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
780 
781 		(void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
782 		    ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
783 
784 		nvlist_free(attr);
785 		kmem_free(physpath, MAXPATHLEN);
786 	}
787 
788 out:
789 	dmu_objset_rele(os, FTAG);
790 
791 	mutex_exit(&zfsdev_state_lock);
792 
793 	return (error);
794 }
795 
796 /*ARGSUSED*/
797 int
798 zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr)
799 {
800 	zvol_state_t *zv;
801 	int err = 0;
802 
803 	mutex_enter(&zfsdev_state_lock);
804 
805 	zv = zfsdev_get_soft_state(getminor(*devp), ZSST_ZVOL);
806 	if (zv == NULL) {
807 		mutex_exit(&zfsdev_state_lock);
808 		return (ENXIO);
809 	}
810 
811 	if (zv->zv_total_opens == 0)
812 		err = zvol_first_open(zv);
813 	if (err) {
814 		mutex_exit(&zfsdev_state_lock);
815 		return (err);
816 	}
817 	if ((flag & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
818 		err = EROFS;
819 		goto out;
820 	}
821 	if (zv->zv_flags & ZVOL_EXCL) {
822 		err = EBUSY;
823 		goto out;
824 	}
825 	if (flag & FEXCL) {
826 		if (zv->zv_total_opens != 0) {
827 			err = EBUSY;
828 			goto out;
829 		}
830 		zv->zv_flags |= ZVOL_EXCL;
831 	}
832 
833 	if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) {
834 		zv->zv_open_count[otyp]++;
835 		zv->zv_total_opens++;
836 	}
837 	mutex_exit(&zfsdev_state_lock);
838 
839 	return (err);
840 out:
841 	if (zv->zv_total_opens == 0)
842 		zvol_last_close(zv);
843 	mutex_exit(&zfsdev_state_lock);
844 	return (err);
845 }
846 
847 /*ARGSUSED*/
848 int
849 zvol_close(dev_t dev, int flag, int otyp, cred_t *cr)
850 {
851 	minor_t minor = getminor(dev);
852 	zvol_state_t *zv;
853 	int error = 0;
854 
855 	mutex_enter(&zfsdev_state_lock);
856 
857 	zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
858 	if (zv == NULL) {
859 		mutex_exit(&zfsdev_state_lock);
860 		return (ENXIO);
861 	}
862 
863 	if (zv->zv_flags & ZVOL_EXCL) {
864 		ASSERT(zv->zv_total_opens == 1);
865 		zv->zv_flags &= ~ZVOL_EXCL;
866 	}
867 
868 	/*
869 	 * If the open count is zero, this is a spurious close.
870 	 * That indicates a bug in the kernel / DDI framework.
871 	 */
872 	ASSERT(zv->zv_open_count[otyp] != 0);
873 	ASSERT(zv->zv_total_opens != 0);
874 
875 	/*
876 	 * You may get multiple opens, but only one close.
877 	 */
878 	zv->zv_open_count[otyp]--;
879 	zv->zv_total_opens--;
880 
881 	if (zv->zv_total_opens == 0)
882 		zvol_last_close(zv);
883 
884 	mutex_exit(&zfsdev_state_lock);
885 	return (error);
886 }
887 
888 static void
889 zvol_get_done(zgd_t *zgd, int error)
890 {
891 	if (zgd->zgd_db)
892 		dmu_buf_rele(zgd->zgd_db, zgd);
893 
894 	zfs_range_unlock(zgd->zgd_rl);
895 
896 	if (error == 0 && zgd->zgd_bp)
897 		zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
898 
899 	kmem_free(zgd, sizeof (zgd_t));
900 }
901 
902 /*
903  * Get data to generate a TX_WRITE intent log record.
904  */
905 static int
906 zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
907 {
908 	zvol_state_t *zv = arg;
909 	objset_t *os = zv->zv_objset;
910 	uint64_t object = ZVOL_OBJ;
911 	uint64_t offset = lr->lr_offset;
912 	uint64_t size = lr->lr_length;	/* length of user data */
913 	blkptr_t *bp = &lr->lr_blkptr;
914 	dmu_buf_t *db;
915 	zgd_t *zgd;
916 	int error;
917 
918 	ASSERT(zio != NULL);
919 	ASSERT(size != 0);
920 
921 	zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
922 	zgd->zgd_zilog = zv->zv_zilog;
923 	zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
924 
925 	/*
926 	 * Write records come in two flavors: immediate and indirect.
927 	 * For small writes it's cheaper to store the data with the
928 	 * log record (immediate); for large writes it's cheaper to
929 	 * sync the data and get a pointer to it (indirect) so that
930 	 * we don't have to write the data twice.
931 	 */
932 	if (buf != NULL) {	/* immediate write */
933 		error = dmu_read(os, object, offset, size, buf,
934 		    DMU_READ_NO_PREFETCH);
935 	} else {
936 		size = zv->zv_volblocksize;
937 		offset = P2ALIGN(offset, size);
938 		error = dmu_buf_hold(os, object, offset, zgd, &db,
939 		    DMU_READ_NO_PREFETCH);
940 		if (error == 0) {
941 			zgd->zgd_db = db;
942 			zgd->zgd_bp = bp;
943 
944 			ASSERT(db->db_offset == offset);
945 			ASSERT(db->db_size == size);
946 
947 			error = dmu_sync(zio, lr->lr_common.lrc_txg,
948 			    zvol_get_done, zgd);
949 
950 			if (error == 0)
951 				return (0);
952 		}
953 	}
954 
955 	zvol_get_done(zgd, error);
956 
957 	return (error);
958 }
959 
960 /*
961  * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
962  *
963  * We store data in the log buffers if it's small enough.
964  * Otherwise we will later flush the data out via dmu_sync().
965  */
966 ssize_t zvol_immediate_write_sz = 32768;
967 
968 static void
969 zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid,
970     boolean_t sync)
971 {
972 	uint32_t blocksize = zv->zv_volblocksize;
973 	zilog_t *zilog = zv->zv_zilog;
974 	boolean_t slogging;
975 	ssize_t immediate_write_sz;
976 
977 	if (zil_replaying(zilog, tx))
978 		return;
979 
980 	immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
981 	    ? 0 : zvol_immediate_write_sz;
982 
983 	slogging = spa_has_slogs(zilog->zl_spa) &&
984 	    (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
985 
986 	while (resid) {
987 		itx_t *itx;
988 		lr_write_t *lr;
989 		ssize_t len;
990 		itx_wr_state_t write_state;
991 
992 		/*
993 		 * Unlike zfs_log_write() we can be called with
994 		 * upto DMU_MAX_ACCESS/2 (5MB) writes.
995 		 */
996 		if (blocksize > immediate_write_sz && !slogging &&
997 		    resid >= blocksize && off % blocksize == 0) {
998 			write_state = WR_INDIRECT; /* uses dmu_sync */
999 			len = blocksize;
1000 		} else if (sync) {
1001 			write_state = WR_COPIED;
1002 			len = MIN(ZIL_MAX_LOG_DATA, resid);
1003 		} else {
1004 			write_state = WR_NEED_COPY;
1005 			len = MIN(ZIL_MAX_LOG_DATA, resid);
1006 		}
1007 
1008 		itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
1009 		    (write_state == WR_COPIED ? len : 0));
1010 		lr = (lr_write_t *)&itx->itx_lr;
1011 		if (write_state == WR_COPIED && dmu_read(zv->zv_objset,
1012 		    ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
1013 			zil_itx_destroy(itx);
1014 			itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1015 			lr = (lr_write_t *)&itx->itx_lr;
1016 			write_state = WR_NEED_COPY;
1017 		}
1018 
1019 		itx->itx_wr_state = write_state;
1020 		if (write_state == WR_NEED_COPY)
1021 			itx->itx_sod += len;
1022 		lr->lr_foid = ZVOL_OBJ;
1023 		lr->lr_offset = off;
1024 		lr->lr_length = len;
1025 		lr->lr_blkoff = 0;
1026 		BP_ZERO(&lr->lr_blkptr);
1027 
1028 		itx->itx_private = zv;
1029 		itx->itx_sync = sync;
1030 
1031 		(void) zil_itx_assign(zilog, itx, tx);
1032 
1033 		off += len;
1034 		resid -= len;
1035 	}
1036 }
1037 
1038 static int
1039 zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t size,
1040     boolean_t doread, boolean_t isdump)
1041 {
1042 	vdev_disk_t *dvd;
1043 	int c;
1044 	int numerrors = 0;
1045 
1046 	for (c = 0; c < vd->vdev_children; c++) {
1047 		ASSERT(vd->vdev_ops == &vdev_mirror_ops ||
1048 		    vd->vdev_ops == &vdev_replacing_ops ||
1049 		    vd->vdev_ops == &vdev_spare_ops);
1050 		int err = zvol_dumpio_vdev(vd->vdev_child[c],
1051 		    addr, offset, size, doread, isdump);
1052 		if (err != 0) {
1053 			numerrors++;
1054 		} else if (doread) {
1055 			break;
1056 		}
1057 	}
1058 
1059 	if (!vd->vdev_ops->vdev_op_leaf)
1060 		return (numerrors < vd->vdev_children ? 0 : EIO);
1061 
1062 	if (doread && !vdev_readable(vd))
1063 		return (EIO);
1064 	else if (!doread && !vdev_writeable(vd))
1065 		return (EIO);
1066 
1067 	dvd = vd->vdev_tsd;
1068 	ASSERT3P(dvd, !=, NULL);
1069 	offset += VDEV_LABEL_START_SIZE;
1070 
1071 	if (ddi_in_panic() || isdump) {
1072 		ASSERT(!doread);
1073 		if (doread)
1074 			return (EIO);
1075 		return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset),
1076 		    lbtodb(size)));
1077 	} else {
1078 		return (vdev_disk_physio(dvd->vd_lh, addr, size, offset,
1079 		    doread ? B_READ : B_WRITE));
1080 	}
1081 }
1082 
1083 static int
1084 zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size,
1085     boolean_t doread, boolean_t isdump)
1086 {
1087 	vdev_t *vd;
1088 	int error;
1089 	zvol_extent_t *ze;
1090 	spa_t *spa = dmu_objset_spa(zv->zv_objset);
1091 
1092 	/* Must be sector aligned, and not stradle a block boundary. */
1093 	if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) ||
1094 	    P2BOUNDARY(offset, size, zv->zv_volblocksize)) {
1095 		return (EINVAL);
1096 	}
1097 	ASSERT(size <= zv->zv_volblocksize);
1098 
1099 	/* Locate the extent this belongs to */
1100 	ze = list_head(&zv->zv_extents);
1101 	while (offset >= ze->ze_nblks * zv->zv_volblocksize) {
1102 		offset -= ze->ze_nblks * zv->zv_volblocksize;
1103 		ze = list_next(&zv->zv_extents, ze);
1104 	}
1105 
1106 	if (!ddi_in_panic())
1107 		spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
1108 
1109 	vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva));
1110 	offset += DVA_GET_OFFSET(&ze->ze_dva);
1111 	error = zvol_dumpio_vdev(vd, addr, offset, size, doread, isdump);
1112 
1113 	if (!ddi_in_panic())
1114 		spa_config_exit(spa, SCL_STATE, FTAG);
1115 
1116 	return (error);
1117 }
1118 
1119 int
1120 zvol_strategy(buf_t *bp)
1121 {
1122 	zfs_soft_state_t *zs = NULL;
1123 	zvol_state_t *zv;
1124 	uint64_t off, volsize;
1125 	size_t resid;
1126 	char *addr;
1127 	objset_t *os;
1128 	rl_t *rl;
1129 	int error = 0;
1130 	boolean_t doread = bp->b_flags & B_READ;
1131 	boolean_t is_dump;
1132 	boolean_t sync;
1133 
1134 	if (getminor(bp->b_edev) == 0) {
1135 		error = EINVAL;
1136 	} else {
1137 		zs = ddi_get_soft_state(zfsdev_state, getminor(bp->b_edev));
1138 		if (zs == NULL)
1139 			error = ENXIO;
1140 		else if (zs->zss_type != ZSST_ZVOL)
1141 			error = EINVAL;
1142 	}
1143 
1144 	if (error) {
1145 		bioerror(bp, error);
1146 		biodone(bp);
1147 		return (0);
1148 	}
1149 
1150 	zv = zs->zss_data;
1151 
1152 	if (!(bp->b_flags & B_READ) && (zv->zv_flags & ZVOL_RDONLY)) {
1153 		bioerror(bp, EROFS);
1154 		biodone(bp);
1155 		return (0);
1156 	}
1157 
1158 	off = ldbtob(bp->b_blkno);
1159 	volsize = zv->zv_volsize;
1160 
1161 	os = zv->zv_objset;
1162 	ASSERT(os != NULL);
1163 
1164 	bp_mapin(bp);
1165 	addr = bp->b_un.b_addr;
1166 	resid = bp->b_bcount;
1167 
1168 	if (resid > 0 && (off < 0 || off >= volsize)) {
1169 		bioerror(bp, EIO);
1170 		biodone(bp);
1171 		return (0);
1172 	}
1173 
1174 	is_dump = zv->zv_flags & ZVOL_DUMPIFIED;
1175 	sync = ((!(bp->b_flags & B_ASYNC) &&
1176 	    !(zv->zv_flags & ZVOL_WCE)) ||
1177 	    (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)) &&
1178 	    !doread && !is_dump;
1179 
1180 	/*
1181 	 * There must be no buffer changes when doing a dmu_sync() because
1182 	 * we can't change the data whilst calculating the checksum.
1183 	 */
1184 	rl = zfs_range_lock(&zv->zv_znode, off, resid,
1185 	    doread ? RL_READER : RL_WRITER);
1186 
1187 	while (resid != 0 && off < volsize) {
1188 		size_t size = MIN(resid, zvol_maxphys);
1189 		if (is_dump) {
1190 			size = MIN(size, P2END(off, zv->zv_volblocksize) - off);
1191 			error = zvol_dumpio(zv, addr, off, size,
1192 			    doread, B_FALSE);
1193 		} else if (doread) {
1194 			error = dmu_read(os, ZVOL_OBJ, off, size, addr,
1195 			    DMU_READ_PREFETCH);
1196 		} else {
1197 			dmu_tx_t *tx = dmu_tx_create(os);
1198 			dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
1199 			error = dmu_tx_assign(tx, TXG_WAIT);
1200 			if (error) {
1201 				dmu_tx_abort(tx);
1202 			} else {
1203 				dmu_write(os, ZVOL_OBJ, off, size, addr, tx);
1204 				zvol_log_write(zv, tx, off, size, sync);
1205 				dmu_tx_commit(tx);
1206 			}
1207 		}
1208 		if (error) {
1209 			/* convert checksum errors into IO errors */
1210 			if (error == ECKSUM)
1211 				error = EIO;
1212 			break;
1213 		}
1214 		off += size;
1215 		addr += size;
1216 		resid -= size;
1217 	}
1218 	zfs_range_unlock(rl);
1219 
1220 	if ((bp->b_resid = resid) == bp->b_bcount)
1221 		bioerror(bp, off > volsize ? EINVAL : error);
1222 
1223 	if (sync)
1224 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1225 	biodone(bp);
1226 
1227 	return (0);
1228 }
1229 
1230 /*
1231  * Set the buffer count to the zvol maximum transfer.
1232  * Using our own routine instead of the default minphys()
1233  * means that for larger writes we write bigger buffers on X86
1234  * (128K instead of 56K) and flush the disk write cache less often
1235  * (every zvol_maxphys - currently 1MB) instead of minphys (currently
1236  * 56K on X86 and 128K on sparc).
1237  */
1238 void
1239 zvol_minphys(struct buf *bp)
1240 {
1241 	if (bp->b_bcount > zvol_maxphys)
1242 		bp->b_bcount = zvol_maxphys;
1243 }
1244 
1245 int
1246 zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks)
1247 {
1248 	minor_t minor = getminor(dev);
1249 	zvol_state_t *zv;
1250 	int error = 0;
1251 	uint64_t size;
1252 	uint64_t boff;
1253 	uint64_t resid;
1254 
1255 	zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1256 	if (zv == NULL)
1257 		return (ENXIO);
1258 
1259 	boff = ldbtob(blkno);
1260 	resid = ldbtob(nblocks);
1261 
1262 	VERIFY3U(boff + resid, <=, zv->zv_volsize);
1263 
1264 	while (resid) {
1265 		size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff);
1266 		error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE);
1267 		if (error)
1268 			break;
1269 		boff += size;
1270 		addr += size;
1271 		resid -= size;
1272 	}
1273 
1274 	return (error);
1275 }
1276 
1277 /*ARGSUSED*/
1278 int
1279 zvol_read(dev_t dev, uio_t *uio, cred_t *cr)
1280 {
1281 	minor_t minor = getminor(dev);
1282 	zvol_state_t *zv;
1283 	uint64_t volsize;
1284 	rl_t *rl;
1285 	int error = 0;
1286 
1287 	zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1288 	if (zv == NULL)
1289 		return (ENXIO);
1290 
1291 	volsize = zv->zv_volsize;
1292 	if (uio->uio_resid > 0 &&
1293 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
1294 		return (EIO);
1295 
1296 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
1297 		error = physio(zvol_strategy, NULL, dev, B_READ,
1298 		    zvol_minphys, uio);
1299 		return (error);
1300 	}
1301 
1302 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1303 	    RL_READER);
1304 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1305 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1306 
1307 		/* don't read past the end */
1308 		if (bytes > volsize - uio->uio_loffset)
1309 			bytes = volsize - uio->uio_loffset;
1310 
1311 		error =  dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes);
1312 		if (error) {
1313 			/* convert checksum errors into IO errors */
1314 			if (error == ECKSUM)
1315 				error = EIO;
1316 			break;
1317 		}
1318 	}
1319 	zfs_range_unlock(rl);
1320 	return (error);
1321 }
1322 
1323 /*ARGSUSED*/
1324 int
1325 zvol_write(dev_t dev, uio_t *uio, cred_t *cr)
1326 {
1327 	minor_t minor = getminor(dev);
1328 	zvol_state_t *zv;
1329 	uint64_t volsize;
1330 	rl_t *rl;
1331 	int error = 0;
1332 	boolean_t sync;
1333 
1334 	zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1335 	if (zv == NULL)
1336 		return (ENXIO);
1337 
1338 	volsize = zv->zv_volsize;
1339 	if (uio->uio_resid > 0 &&
1340 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
1341 		return (EIO);
1342 
1343 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
1344 		error = physio(zvol_strategy, NULL, dev, B_WRITE,
1345 		    zvol_minphys, uio);
1346 		return (error);
1347 	}
1348 
1349 	sync = !(zv->zv_flags & ZVOL_WCE) ||
1350 	    (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS);
1351 
1352 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1353 	    RL_WRITER);
1354 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1355 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1356 		uint64_t off = uio->uio_loffset;
1357 		dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
1358 
1359 		if (bytes > volsize - off)	/* don't write past the end */
1360 			bytes = volsize - off;
1361 
1362 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
1363 		error = dmu_tx_assign(tx, TXG_WAIT);
1364 		if (error) {
1365 			dmu_tx_abort(tx);
1366 			break;
1367 		}
1368 		error = dmu_write_uio_dbuf(zv->zv_dbuf, uio, bytes, tx);
1369 		if (error == 0)
1370 			zvol_log_write(zv, tx, off, bytes, sync);
1371 		dmu_tx_commit(tx);
1372 
1373 		if (error)
1374 			break;
1375 	}
1376 	zfs_range_unlock(rl);
1377 	if (sync)
1378 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1379 	return (error);
1380 }
1381 
1382 int
1383 zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs)
1384 {
1385 	struct uuid uuid = EFI_RESERVED;
1386 	efi_gpe_t gpe = { 0 };
1387 	uint32_t crc;
1388 	dk_efi_t efi;
1389 	int length;
1390 	char *ptr;
1391 
1392 	if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag))
1393 		return (EFAULT);
1394 	ptr = (char *)(uintptr_t)efi.dki_data_64;
1395 	length = efi.dki_length;
1396 	/*
1397 	 * Some clients may attempt to request a PMBR for the
1398 	 * zvol.  Currently this interface will return EINVAL to
1399 	 * such requests.  These requests could be supported by
1400 	 * adding a check for lba == 0 and consing up an appropriate
1401 	 * PMBR.
1402 	 */
1403 	if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0)
1404 		return (EINVAL);
1405 
1406 	gpe.efi_gpe_StartingLBA = LE_64(34ULL);
1407 	gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1);
1408 	UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
1409 
1410 	if (efi.dki_lba == 1) {
1411 		efi_gpt_t gpt = { 0 };
1412 
1413 		gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
1414 		gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
1415 		gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt));
1416 		gpt.efi_gpt_MyLBA = LE_64(1ULL);
1417 		gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL);
1418 		gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1);
1419 		gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL);
1420 		gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
1421 		gpt.efi_gpt_SizeOfPartitionEntry =
1422 		    LE_32(sizeof (efi_gpe_t));
1423 		CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
1424 		gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
1425 		CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table);
1426 		gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
1427 		if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length),
1428 		    flag))
1429 			return (EFAULT);
1430 		ptr += sizeof (gpt);
1431 		length -= sizeof (gpt);
1432 	}
1433 	if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe),
1434 	    length), flag))
1435 		return (EFAULT);
1436 	return (0);
1437 }
1438 
1439 /*
1440  * BEGIN entry points to allow external callers access to the volume.
1441  */
1442 /*
1443  * Return the volume parameters needed for access from an external caller.
1444  * These values are invariant as long as the volume is held open.
1445  */
1446 int
1447 zvol_get_volume_params(minor_t minor, uint64_t *blksize,
1448     uint64_t *max_xfer_len, void **minor_hdl, void **objset_hdl, void **zil_hdl,
1449     void **rl_hdl, void **bonus_hdl)
1450 {
1451 	zvol_state_t *zv;
1452 
1453 	zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1454 	if (zv == NULL)
1455 		return (ENXIO);
1456 	if (zv->zv_flags & ZVOL_DUMPIFIED)
1457 		return (ENXIO);
1458 
1459 	ASSERT(blksize && max_xfer_len && minor_hdl &&
1460 	    objset_hdl && zil_hdl && rl_hdl && bonus_hdl);
1461 
1462 	*blksize = zv->zv_volblocksize;
1463 	*max_xfer_len = (uint64_t)zvol_maxphys;
1464 	*minor_hdl = zv;
1465 	*objset_hdl = zv->zv_objset;
1466 	*zil_hdl = zv->zv_zilog;
1467 	*rl_hdl = &zv->zv_znode;
1468 	*bonus_hdl = zv->zv_dbuf;
1469 	return (0);
1470 }
1471 
1472 /*
1473  * Return the current volume size to an external caller.
1474  * The size can change while the volume is open.
1475  */
1476 uint64_t
1477 zvol_get_volume_size(void *minor_hdl)
1478 {
1479 	zvol_state_t *zv = minor_hdl;
1480 
1481 	return (zv->zv_volsize);
1482 }
1483 
1484 /*
1485  * Return the current WCE setting to an external caller.
1486  * The WCE setting can change while the volume is open.
1487  */
1488 int
1489 zvol_get_volume_wce(void *minor_hdl)
1490 {
1491 	zvol_state_t *zv = minor_hdl;
1492 
1493 	return ((zv->zv_flags & ZVOL_WCE) ? 1 : 0);
1494 }
1495 
1496 /*
1497  * Entry point for external callers to zvol_log_write
1498  */
1499 void
1500 zvol_log_write_minor(void *minor_hdl, dmu_tx_t *tx, offset_t off, ssize_t resid,
1501     boolean_t sync)
1502 {
1503 	zvol_state_t *zv = minor_hdl;
1504 
1505 	zvol_log_write(zv, tx, off, resid, sync);
1506 }
1507 /*
1508  * END entry points to allow external callers access to the volume.
1509  */
1510 
1511 /*
1512  * Dirtbag ioctls to support mkfs(1M) for UFS filesystems.  See dkio(7I).
1513  */
1514 /*ARGSUSED*/
1515 int
1516 zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
1517 {
1518 	zvol_state_t *zv;
1519 	struct dk_cinfo dki;
1520 	struct dk_minfo dkm;
1521 	struct dk_callback *dkc;
1522 	int error = 0;
1523 	rl_t *rl;
1524 
1525 	mutex_enter(&zfsdev_state_lock);
1526 
1527 	zv = zfsdev_get_soft_state(getminor(dev), ZSST_ZVOL);
1528 
1529 	if (zv == NULL) {
1530 		mutex_exit(&zfsdev_state_lock);
1531 		return (ENXIO);
1532 	}
1533 	ASSERT(zv->zv_total_opens > 0);
1534 
1535 	switch (cmd) {
1536 
1537 	case DKIOCINFO:
1538 		bzero(&dki, sizeof (dki));
1539 		(void) strcpy(dki.dki_cname, "zvol");
1540 		(void) strcpy(dki.dki_dname, "zvol");
1541 		dki.dki_ctype = DKC_UNKNOWN;
1542 		dki.dki_unit = getminor(dev);
1543 		dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs);
1544 		mutex_exit(&zfsdev_state_lock);
1545 		if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag))
1546 			error = EFAULT;
1547 		return (error);
1548 
1549 	case DKIOCGMEDIAINFO:
1550 		bzero(&dkm, sizeof (dkm));
1551 		dkm.dki_lbsize = 1U << zv->zv_min_bs;
1552 		dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
1553 		dkm.dki_media_type = DK_UNKNOWN;
1554 		mutex_exit(&zfsdev_state_lock);
1555 		if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
1556 			error = EFAULT;
1557 		return (error);
1558 
1559 	case DKIOCGETEFI:
1560 		{
1561 			uint64_t vs = zv->zv_volsize;
1562 			uint8_t bs = zv->zv_min_bs;
1563 
1564 			mutex_exit(&zfsdev_state_lock);
1565 			error = zvol_getefi((void *)arg, flag, vs, bs);
1566 			return (error);
1567 		}
1568 
1569 	case DKIOCFLUSHWRITECACHE:
1570 		dkc = (struct dk_callback *)arg;
1571 		mutex_exit(&zfsdev_state_lock);
1572 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1573 		if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) {
1574 			(*dkc->dkc_callback)(dkc->dkc_cookie, error);
1575 			error = 0;
1576 		}
1577 		return (error);
1578 
1579 	case DKIOCGETWCE:
1580 		{
1581 			int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0;
1582 			if (ddi_copyout(&wce, (void *)arg, sizeof (int),
1583 			    flag))
1584 				error = EFAULT;
1585 			break;
1586 		}
1587 	case DKIOCSETWCE:
1588 		{
1589 			int wce;
1590 			if (ddi_copyin((void *)arg, &wce, sizeof (int),
1591 			    flag)) {
1592 				error = EFAULT;
1593 				break;
1594 			}
1595 			if (wce) {
1596 				zv->zv_flags |= ZVOL_WCE;
1597 				mutex_exit(&zfsdev_state_lock);
1598 			} else {
1599 				zv->zv_flags &= ~ZVOL_WCE;
1600 				mutex_exit(&zfsdev_state_lock);
1601 				zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1602 			}
1603 			return (0);
1604 		}
1605 
1606 	case DKIOCGGEOM:
1607 	case DKIOCGVTOC:
1608 		/*
1609 		 * commands using these (like prtvtoc) expect ENOTSUP
1610 		 * since we're emulating an EFI label
1611 		 */
1612 		error = ENOTSUP;
1613 		break;
1614 
1615 	case DKIOCDUMPINIT:
1616 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1617 		    RL_WRITER);
1618 		error = zvol_dumpify(zv);
1619 		zfs_range_unlock(rl);
1620 		break;
1621 
1622 	case DKIOCDUMPFINI:
1623 		if (!(zv->zv_flags & ZVOL_DUMPIFIED))
1624 			break;
1625 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1626 		    RL_WRITER);
1627 		error = zvol_dump_fini(zv);
1628 		zfs_range_unlock(rl);
1629 		break;
1630 
1631 	default:
1632 		error = ENOTTY;
1633 		break;
1634 
1635 	}
1636 	mutex_exit(&zfsdev_state_lock);
1637 	return (error);
1638 }
1639 
1640 int
1641 zvol_busy(void)
1642 {
1643 	return (zvol_minors != 0);
1644 }
1645 
1646 void
1647 zvol_init(void)
1648 {
1649 	VERIFY(ddi_soft_state_init(&zfsdev_state, sizeof (zfs_soft_state_t),
1650 	    1) == 0);
1651 	mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL);
1652 }
1653 
1654 void
1655 zvol_fini(void)
1656 {
1657 	mutex_destroy(&zfsdev_state_lock);
1658 	ddi_soft_state_fini(&zfsdev_state);
1659 }
1660 
1661 static int
1662 zvol_dump_init(zvol_state_t *zv, boolean_t resize)
1663 {
1664 	dmu_tx_t *tx;
1665 	int error = 0;
1666 	objset_t *os = zv->zv_objset;
1667 	nvlist_t *nv = NULL;
1668 	uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset));
1669 
1670 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
1671 	error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0,
1672 	    DMU_OBJECT_END);
1673 	/* wait for dmu_free_long_range to actually free the blocks */
1674 	txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
1675 
1676 	tx = dmu_tx_create(os);
1677 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1678 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
1679 	error = dmu_tx_assign(tx, TXG_WAIT);
1680 	if (error) {
1681 		dmu_tx_abort(tx);
1682 		return (error);
1683 	}
1684 
1685 	/*
1686 	 * If we are resizing the dump device then we only need to
1687 	 * update the refreservation to match the newly updated
1688 	 * zvolsize. Otherwise, we save off the original state of the
1689 	 * zvol so that we can restore them if the zvol is ever undumpified.
1690 	 */
1691 	if (resize) {
1692 		error = zap_update(os, ZVOL_ZAP_OBJ,
1693 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1694 		    &zv->zv_volsize, tx);
1695 	} else {
1696 		uint64_t checksum, compress, refresrv, vbs, dedup;
1697 
1698 		error = dsl_prop_get_integer(zv->zv_name,
1699 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL);
1700 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
1701 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL);
1702 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
1703 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL);
1704 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
1705 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL);
1706 		if (version >= SPA_VERSION_DEDUP) {
1707 			error = error ? error :
1708 			    dsl_prop_get_integer(zv->zv_name,
1709 			    zfs_prop_to_name(ZFS_PROP_DEDUP), &dedup, NULL);
1710 		}
1711 
1712 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1713 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1,
1714 		    &compress, tx);
1715 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1716 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx);
1717 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1718 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1719 		    &refresrv, tx);
1720 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1721 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1,
1722 		    &vbs, tx);
1723 		error = error ? error : dmu_object_set_blocksize(
1724 		    os, ZVOL_OBJ, SPA_MAXBLOCKSIZE, 0, tx);
1725 		if (version >= SPA_VERSION_DEDUP) {
1726 			error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1727 			    zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1,
1728 			    &dedup, tx);
1729 		}
1730 		if (error == 0)
1731 			zv->zv_volblocksize = SPA_MAXBLOCKSIZE;
1732 	}
1733 	dmu_tx_commit(tx);
1734 
1735 	/*
1736 	 * We only need update the zvol's property if we are initializing
1737 	 * the dump area for the first time.
1738 	 */
1739 	if (!resize) {
1740 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1741 		VERIFY(nvlist_add_uint64(nv,
1742 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0);
1743 		VERIFY(nvlist_add_uint64(nv,
1744 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
1745 		    ZIO_COMPRESS_OFF) == 0);
1746 		VERIFY(nvlist_add_uint64(nv,
1747 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
1748 		    ZIO_CHECKSUM_OFF) == 0);
1749 		if (version >= SPA_VERSION_DEDUP) {
1750 			VERIFY(nvlist_add_uint64(nv,
1751 			    zfs_prop_to_name(ZFS_PROP_DEDUP),
1752 			    ZIO_CHECKSUM_OFF) == 0);
1753 		}
1754 
1755 		error = zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
1756 		    nv, NULL);
1757 		nvlist_free(nv);
1758 
1759 		if (error)
1760 			return (error);
1761 	}
1762 
1763 	/* Allocate the space for the dump */
1764 	error = zvol_prealloc(zv);
1765 	return (error);
1766 }
1767 
1768 static int
1769 zvol_dumpify(zvol_state_t *zv)
1770 {
1771 	int error = 0;
1772 	uint64_t dumpsize = 0;
1773 	dmu_tx_t *tx;
1774 	objset_t *os = zv->zv_objset;
1775 
1776 	if (zv->zv_flags & ZVOL_RDONLY)
1777 		return (EROFS);
1778 
1779 	if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE,
1780 	    8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) {
1781 		boolean_t resize = (dumpsize > 0) ? B_TRUE : B_FALSE;
1782 
1783 		if ((error = zvol_dump_init(zv, resize)) != 0) {
1784 			(void) zvol_dump_fini(zv);
1785 			return (error);
1786 		}
1787 	}
1788 
1789 	/*
1790 	 * Build up our lba mapping.
1791 	 */
1792 	error = zvol_get_lbas(zv);
1793 	if (error) {
1794 		(void) zvol_dump_fini(zv);
1795 		return (error);
1796 	}
1797 
1798 	tx = dmu_tx_create(os);
1799 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1800 	error = dmu_tx_assign(tx, TXG_WAIT);
1801 	if (error) {
1802 		dmu_tx_abort(tx);
1803 		(void) zvol_dump_fini(zv);
1804 		return (error);
1805 	}
1806 
1807 	zv->zv_flags |= ZVOL_DUMPIFIED;
1808 	error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1,
1809 	    &zv->zv_volsize, tx);
1810 	dmu_tx_commit(tx);
1811 
1812 	if (error) {
1813 		(void) zvol_dump_fini(zv);
1814 		return (error);
1815 	}
1816 
1817 	txg_wait_synced(dmu_objset_pool(os), 0);
1818 	return (0);
1819 }
1820 
1821 static int
1822 zvol_dump_fini(zvol_state_t *zv)
1823 {
1824 	dmu_tx_t *tx;
1825 	objset_t *os = zv->zv_objset;
1826 	nvlist_t *nv;
1827 	int error = 0;
1828 	uint64_t checksum, compress, refresrv, vbs, dedup;
1829 	uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset));
1830 
1831 	/*
1832 	 * Attempt to restore the zvol back to its pre-dumpified state.
1833 	 * This is a best-effort attempt as it's possible that not all
1834 	 * of these properties were initialized during the dumpify process
1835 	 * (i.e. error during zvol_dump_init).
1836 	 */
1837 
1838 	tx = dmu_tx_create(os);
1839 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1840 	error = dmu_tx_assign(tx, TXG_WAIT);
1841 	if (error) {
1842 		dmu_tx_abort(tx);
1843 		return (error);
1844 	}
1845 	(void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx);
1846 	dmu_tx_commit(tx);
1847 
1848 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1849 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum);
1850 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1851 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress);
1852 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1853 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv);
1854 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1855 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs);
1856 
1857 	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1858 	(void) nvlist_add_uint64(nv,
1859 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum);
1860 	(void) nvlist_add_uint64(nv,
1861 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress);
1862 	(void) nvlist_add_uint64(nv,
1863 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv);
1864 	if (version >= SPA_VERSION_DEDUP &&
1865 	    zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1866 	    zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, &dedup) == 0) {
1867 		(void) nvlist_add_uint64(nv,
1868 		    zfs_prop_to_name(ZFS_PROP_DEDUP), dedup);
1869 	}
1870 	(void) zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
1871 	    nv, NULL);
1872 	nvlist_free(nv);
1873 
1874 	zvol_free_extents(zv);
1875 	zv->zv_flags &= ~ZVOL_DUMPIFIED;
1876 	(void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END);
1877 	/* wait for dmu_free_long_range to actually free the blocks */
1878 	txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
1879 	tx = dmu_tx_create(os);
1880 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
1881 	error = dmu_tx_assign(tx, TXG_WAIT);
1882 	if (error) {
1883 		dmu_tx_abort(tx);
1884 		return (error);
1885 	}
1886 	if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0)
1887 		zv->zv_volblocksize = vbs;
1888 	dmu_tx_commit(tx);
1889 
1890 	return (0);
1891 }
1892