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