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