xref: /titanic_51/usr/src/uts/common/fs/zfs/zvol.c (revision 20ed34cc7e9de957ef6251e20c0f2d8e43847ea0)
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 			/* convert checksum errors into IO errors */
1289 			if (error == ECKSUM)
1290 				error = EIO;
1291 			break;
1292 		}
1293 		off += size;
1294 		addr += size;
1295 		resid -= size;
1296 	}
1297 	zfs_range_unlock(rl);
1298 
1299 	if ((bp->b_resid = resid) == bp->b_bcount)
1300 		bioerror(bp, off > volsize ? EINVAL : error);
1301 
1302 	if (!(bp->b_flags & B_ASYNC) && !reading && !zil_disable && !is_dump)
1303 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1304 	biodone(bp);
1305 
1306 	return (0);
1307 }
1308 
1309 /*
1310  * Set the buffer count to the zvol maximum transfer.
1311  * Using our own routine instead of the default minphys()
1312  * means that for larger writes we write bigger buffers on X86
1313  * (128K instead of 56K) and flush the disk write cache less often
1314  * (every zvol_maxphys - currently 1MB) instead of minphys (currently
1315  * 56K on X86 and 128K on sparc).
1316  */
1317 void
1318 zvol_minphys(struct buf *bp)
1319 {
1320 	if (bp->b_bcount > zvol_maxphys)
1321 		bp->b_bcount = zvol_maxphys;
1322 }
1323 
1324 int
1325 zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks)
1326 {
1327 	minor_t minor = getminor(dev);
1328 	zvol_state_t *zv;
1329 	int error = 0;
1330 	uint64_t size;
1331 	uint64_t boff;
1332 	uint64_t resid;
1333 
1334 	if (minor == 0)			/* This is the control device */
1335 		return (ENXIO);
1336 
1337 	zv = ddi_get_soft_state(zvol_state, minor);
1338 	if (zv == NULL)
1339 		return (ENXIO);
1340 
1341 	boff = ldbtob(blkno);
1342 	resid = ldbtob(nblocks);
1343 	if (boff + resid > zv->zv_volsize) {
1344 		/* dump should know better than to write here */
1345 		ASSERT(blkno + resid <= zv->zv_volsize);
1346 		return (EIO);
1347 	}
1348 	while (resid) {
1349 		/* can't straddle a block boundary */
1350 		size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff);
1351 
1352 		error = zvol_physio(zv, B_WRITE, boff, size, addr, 1);
1353 		if (error)
1354 			break;
1355 		boff += size;
1356 		addr += size;
1357 		resid -= size;
1358 	}
1359 
1360 	return (error);
1361 }
1362 
1363 /*ARGSUSED*/
1364 int
1365 zvol_read(dev_t dev, uio_t *uio, cred_t *cr)
1366 {
1367 	minor_t minor = getminor(dev);
1368 	zvol_state_t *zv;
1369 	uint64_t volsize;
1370 	rl_t *rl;
1371 	int error = 0;
1372 
1373 	if (minor == 0)			/* This is the control device */
1374 		return (ENXIO);
1375 
1376 	zv = ddi_get_soft_state(zvol_state, minor);
1377 	if (zv == NULL)
1378 		return (ENXIO);
1379 
1380 	volsize = zv->zv_volsize;
1381 	if (uio->uio_resid > 0 &&
1382 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
1383 		return (EIO);
1384 
1385 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1386 	    RL_READER);
1387 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1388 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1389 
1390 		/* don't read past the end */
1391 		if (bytes > volsize - uio->uio_loffset)
1392 			bytes = volsize - uio->uio_loffset;
1393 
1394 		error =  dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes);
1395 		if (error) {
1396 			/* convert checksum errors into IO errors */
1397 			if (error == ECKSUM)
1398 				error = EIO;
1399 			break;
1400 		}
1401 	}
1402 	zfs_range_unlock(rl);
1403 	return (error);
1404 }
1405 
1406 /*ARGSUSED*/
1407 int
1408 zvol_write(dev_t dev, uio_t *uio, cred_t *cr)
1409 {
1410 	minor_t minor = getminor(dev);
1411 	zvol_state_t *zv;
1412 	uint64_t volsize;
1413 	rl_t *rl;
1414 	int error = 0;
1415 
1416 	if (minor == 0)			/* This is the control device */
1417 		return (ENXIO);
1418 
1419 	zv = ddi_get_soft_state(zvol_state, minor);
1420 	if (zv == NULL)
1421 		return (ENXIO);
1422 
1423 	volsize = zv->zv_volsize;
1424 	if (uio->uio_resid > 0 &&
1425 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
1426 		return (EIO);
1427 
1428 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
1429 		error = physio(zvol_strategy, NULL, dev, B_WRITE,
1430 		    zvol_minphys, uio);
1431 		return (error);
1432 	}
1433 
1434 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1435 	    RL_WRITER);
1436 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1437 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1438 		uint64_t off = uio->uio_loffset;
1439 		dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
1440 
1441 		if (bytes > volsize - off)	/* don't write past the end */
1442 			bytes = volsize - off;
1443 
1444 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
1445 		error = dmu_tx_assign(tx, TXG_WAIT);
1446 		if (error) {
1447 			dmu_tx_abort(tx);
1448 			break;
1449 		}
1450 		error = dmu_write_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes, tx);
1451 		if (error == 0)
1452 			zvol_log_write(zv, tx, off, bytes);
1453 		dmu_tx_commit(tx);
1454 
1455 		if (error)
1456 			break;
1457 	}
1458 	zfs_range_unlock(rl);
1459 	return (error);
1460 }
1461 
1462 /*
1463  * Dirtbag ioctls to support mkfs(1M) for UFS filesystems.  See dkio(7I).
1464  */
1465 /*ARGSUSED*/
1466 int
1467 zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
1468 {
1469 	zvol_state_t *zv;
1470 	struct dk_cinfo dki;
1471 	struct dk_minfo dkm;
1472 	dk_efi_t efi;
1473 	struct dk_callback *dkc;
1474 	struct uuid uuid = EFI_RESERVED;
1475 	uint32_t crc;
1476 	int error = 0;
1477 	rl_t *rl;
1478 
1479 	mutex_enter(&zvol_state_lock);
1480 
1481 	zv = ddi_get_soft_state(zvol_state, getminor(dev));
1482 
1483 	if (zv == NULL) {
1484 		mutex_exit(&zvol_state_lock);
1485 		return (ENXIO);
1486 	}
1487 
1488 	switch (cmd) {
1489 
1490 	case DKIOCINFO:
1491 		bzero(&dki, sizeof (dki));
1492 		(void) strcpy(dki.dki_cname, "zvol");
1493 		(void) strcpy(dki.dki_dname, "zvol");
1494 		dki.dki_ctype = DKC_UNKNOWN;
1495 		dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs);
1496 		mutex_exit(&zvol_state_lock);
1497 		if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag))
1498 			error = EFAULT;
1499 		return (error);
1500 
1501 	case DKIOCGMEDIAINFO:
1502 		bzero(&dkm, sizeof (dkm));
1503 		dkm.dki_lbsize = 1U << zv->zv_min_bs;
1504 		dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
1505 		dkm.dki_media_type = DK_UNKNOWN;
1506 		mutex_exit(&zvol_state_lock);
1507 		if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
1508 			error = EFAULT;
1509 		return (error);
1510 
1511 	case DKIOCGETEFI:
1512 		if (ddi_copyin((void *)arg, &efi, sizeof (dk_efi_t), flag)) {
1513 			mutex_exit(&zvol_state_lock);
1514 			return (EFAULT);
1515 		}
1516 		efi.dki_data = (void *)(uintptr_t)efi.dki_data_64;
1517 
1518 		/*
1519 		 * Some clients may attempt to request a PMBR for the
1520 		 * zvol.  Currently this interface will return ENOTTY to
1521 		 * such requests.  These requests could be supported by
1522 		 * adding a check for lba == 0 and consing up an appropriate
1523 		 * PMBR.
1524 		 */
1525 		if (efi.dki_lba == 1) {
1526 			efi_gpt_t gpt;
1527 			efi_gpe_t gpe;
1528 
1529 			bzero(&gpt, sizeof (gpt));
1530 			bzero(&gpe, sizeof (gpe));
1531 
1532 			if (efi.dki_length < sizeof (gpt)) {
1533 				mutex_exit(&zvol_state_lock);
1534 				return (EINVAL);
1535 			}
1536 
1537 			gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
1538 			gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
1539 			gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt));
1540 			gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL);
1541 			gpt.efi_gpt_LastUsableLBA =
1542 			    LE_64((zv->zv_volsize >> zv->zv_min_bs) - 1);
1543 			gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
1544 			gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL);
1545 			gpt.efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (gpe));
1546 
1547 			UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
1548 			gpe.efi_gpe_StartingLBA = gpt.efi_gpt_FirstUsableLBA;
1549 			gpe.efi_gpe_EndingLBA = gpt.efi_gpt_LastUsableLBA;
1550 
1551 			CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
1552 			gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
1553 
1554 			CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table);
1555 			gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
1556 
1557 			mutex_exit(&zvol_state_lock);
1558 			if (ddi_copyout(&gpt, efi.dki_data, sizeof (gpt), flag))
1559 				error = EFAULT;
1560 		} else if (efi.dki_lba == 2) {
1561 			efi_gpe_t gpe;
1562 
1563 			bzero(&gpe, sizeof (gpe));
1564 
1565 			if (efi.dki_length < sizeof (gpe)) {
1566 				mutex_exit(&zvol_state_lock);
1567 				return (EINVAL);
1568 			}
1569 
1570 			UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
1571 			gpe.efi_gpe_StartingLBA = LE_64(34ULL);
1572 			gpe.efi_gpe_EndingLBA =
1573 			    LE_64((zv->zv_volsize >> zv->zv_min_bs) - 1);
1574 
1575 			mutex_exit(&zvol_state_lock);
1576 			if (ddi_copyout(&gpe, efi.dki_data, sizeof (gpe), flag))
1577 				error = EFAULT;
1578 		} else {
1579 			mutex_exit(&zvol_state_lock);
1580 			error = EINVAL;
1581 		}
1582 		return (error);
1583 
1584 	case DKIOCFLUSHWRITECACHE:
1585 		dkc = (struct dk_callback *)arg;
1586 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1587 		if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) {
1588 			(*dkc->dkc_callback)(dkc->dkc_cookie, error);
1589 			error = 0;
1590 		}
1591 		break;
1592 
1593 	case DKIOCGGEOM:
1594 	case DKIOCGVTOC:
1595 		/*
1596 		 * commands using these (like prtvtoc) expect ENOTSUP
1597 		 * since we're emulating an EFI label
1598 		 */
1599 		error = ENOTSUP;
1600 		break;
1601 
1602 	case DKIOCDUMPINIT:
1603 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1604 		    RL_WRITER);
1605 		error = zvol_dumpify(zv);
1606 		zfs_range_unlock(rl);
1607 		break;
1608 
1609 	case DKIOCDUMPFINI:
1610 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1611 		    RL_WRITER);
1612 		error = zvol_dump_fini(zv);
1613 		zfs_range_unlock(rl);
1614 		break;
1615 
1616 	default:
1617 		error = ENOTTY;
1618 		break;
1619 
1620 	}
1621 	mutex_exit(&zvol_state_lock);
1622 	return (error);
1623 }
1624 
1625 int
1626 zvol_busy(void)
1627 {
1628 	return (zvol_minors != 0);
1629 }
1630 
1631 void
1632 zvol_init(void)
1633 {
1634 	VERIFY(ddi_soft_state_init(&zvol_state, sizeof (zvol_state_t), 1) == 0);
1635 	mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
1636 }
1637 
1638 void
1639 zvol_fini(void)
1640 {
1641 	mutex_destroy(&zvol_state_lock);
1642 	ddi_soft_state_fini(&zvol_state);
1643 }
1644 
1645 static boolean_t
1646 zvol_is_swap(zvol_state_t *zv)
1647 {
1648 	vnode_t *vp;
1649 	boolean_t ret = B_FALSE;
1650 	char *devpath;
1651 	size_t devpathlen;
1652 	int error;
1653 
1654 	devpathlen = strlen(ZVOL_FULL_DEV_DIR) + strlen(zv->zv_name) + 1;
1655 	devpath = kmem_alloc(devpathlen, KM_SLEEP);
1656 	(void) sprintf(devpath, "%s%s", ZVOL_FULL_DEV_DIR, zv->zv_name);
1657 	error = lookupname(devpath, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp);
1658 	kmem_free(devpath, devpathlen);
1659 
1660 	ret = !error && IS_SWAPVP(common_specvp(vp));
1661 
1662 	if (vp != NULL)
1663 		VN_RELE(vp);
1664 
1665 	return (ret);
1666 }
1667 
1668 static int
1669 zvol_dump_init(zvol_state_t *zv, boolean_t resize)
1670 {
1671 	dmu_tx_t *tx;
1672 	int error = 0;
1673 	objset_t *os = zv->zv_objset;
1674 	nvlist_t *nv = NULL;
1675 	uint64_t checksum, compress, refresrv;
1676 
1677 	ASSERT(MUTEX_HELD(&zvol_state_lock));
1678 
1679 	tx = dmu_tx_create(os);
1680 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1681 	error = dmu_tx_assign(tx, TXG_WAIT);
1682 	if (error) {
1683 		dmu_tx_abort(tx);
1684 		return (error);
1685 	}
1686 
1687 	/*
1688 	 * If we are resizing the dump device then we only need to
1689 	 * update the refreservation to match the newly updated
1690 	 * zvolsize. Otherwise, we save off the original state of the
1691 	 * zvol so that we can restore them if the zvol is ever undumpified.
1692 	 */
1693 	if (resize) {
1694 		error = zap_update(os, ZVOL_ZAP_OBJ,
1695 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1696 		    &zv->zv_volsize, tx);
1697 	} else {
1698 		error = dsl_prop_get_integer(zv->zv_name,
1699 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL);
1700 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
1701 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL);
1702 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
1703 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL);
1704 
1705 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1706 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1,
1707 		    &compress, tx);
1708 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1709 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx);
1710 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1711 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1712 		    &refresrv, tx);
1713 	}
1714 	dmu_tx_commit(tx);
1715 
1716 	/* Truncate the file */
1717 	if (!error)
1718 		error = dmu_free_long_range(zv->zv_objset,
1719 		    ZVOL_OBJ, 0, DMU_OBJECT_END);
1720 
1721 	if (error)
1722 		return (error);
1723 
1724 	/*
1725 	 * We only need update the zvol's property if we are initializing
1726 	 * the dump area for the first time.
1727 	 */
1728 	if (!resize) {
1729 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1730 		VERIFY(nvlist_add_uint64(nv,
1731 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0);
1732 		VERIFY(nvlist_add_uint64(nv,
1733 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
1734 		    ZIO_COMPRESS_OFF) == 0);
1735 		VERIFY(nvlist_add_uint64(nv,
1736 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
1737 		    ZIO_CHECKSUM_OFF) == 0);
1738 
1739 		error = zfs_set_prop_nvlist(zv->zv_name, nv);
1740 		nvlist_free(nv);
1741 
1742 		if (error)
1743 			return (error);
1744 	}
1745 
1746 	/* Allocate the space for the dump */
1747 	error = zvol_prealloc(zv);
1748 	return (error);
1749 }
1750 
1751 static int
1752 zvol_dumpify(zvol_state_t *zv)
1753 {
1754 	int error = 0;
1755 	uint64_t dumpsize = 0;
1756 	dmu_tx_t *tx;
1757 	objset_t *os = zv->zv_objset;
1758 
1759 	if (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY))
1760 		return (EROFS);
1761 
1762 	/*
1763 	 * We do not support swap devices acting as dump devices.
1764 	 */
1765 	if (zvol_is_swap(zv))
1766 		return (ENOTSUP);
1767 
1768 	if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE,
1769 	    8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) {
1770 		boolean_t resize = (dumpsize > 0) ? B_TRUE : B_FALSE;
1771 
1772 		if ((error = zvol_dump_init(zv, resize)) != 0) {
1773 			(void) zvol_dump_fini(zv);
1774 			return (error);
1775 		}
1776 	}
1777 
1778 	/*
1779 	 * Build up our lba mapping.
1780 	 */
1781 	error = zvol_get_lbas(zv);
1782 	if (error) {
1783 		(void) zvol_dump_fini(zv);
1784 		return (error);
1785 	}
1786 
1787 	tx = dmu_tx_create(os);
1788 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1789 	error = dmu_tx_assign(tx, TXG_WAIT);
1790 	if (error) {
1791 		dmu_tx_abort(tx);
1792 		(void) zvol_dump_fini(zv);
1793 		return (error);
1794 	}
1795 
1796 	zv->zv_flags |= ZVOL_DUMPIFIED;
1797 	error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1,
1798 	    &zv->zv_volsize, tx);
1799 	dmu_tx_commit(tx);
1800 
1801 	if (error) {
1802 		(void) zvol_dump_fini(zv);
1803 		return (error);
1804 	}
1805 
1806 	txg_wait_synced(dmu_objset_pool(os), 0);
1807 	return (0);
1808 }
1809 
1810 static int
1811 zvol_dump_fini(zvol_state_t *zv)
1812 {
1813 	dmu_tx_t *tx;
1814 	objset_t *os = zv->zv_objset;
1815 	nvlist_t *nv;
1816 	int error = 0;
1817 	uint64_t checksum, compress, refresrv;
1818 
1819 	/*
1820 	 * Attempt to restore the zvol back to its pre-dumpified state.
1821 	 * This is a best-effort attempt as it's possible that not all
1822 	 * of these properties were initialized during the dumpify process
1823 	 * (i.e. error during zvol_dump_init).
1824 	 */
1825 
1826 	tx = dmu_tx_create(os);
1827 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1828 	error = dmu_tx_assign(tx, TXG_WAIT);
1829 	if (error) {
1830 		dmu_tx_abort(tx);
1831 		return (error);
1832 	}
1833 	(void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx);
1834 	dmu_tx_commit(tx);
1835 
1836 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1837 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum);
1838 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1839 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress);
1840 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1841 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv);
1842 
1843 	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1844 	(void) nvlist_add_uint64(nv,
1845 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum);
1846 	(void) nvlist_add_uint64(nv,
1847 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress);
1848 	(void) nvlist_add_uint64(nv,
1849 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv);
1850 	(void) zfs_set_prop_nvlist(zv->zv_name, nv);
1851 	nvlist_free(nv);
1852 
1853 	zvol_free_extents(zv);
1854 	zv->zv_flags &= ~ZVOL_DUMPIFIED;
1855 	(void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END);
1856 
1857 	return (0);
1858 }
1859