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