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