xref: /titanic_50/usr/src/uts/common/fs/zfs/zvol.c (revision ff17c8bf86c3e567734be83f90267edee20f580f)
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 	if (error == 0) {
847 		zv->zv_volsize = volsize;
848 		zvol_size_changed(zv, maj);
849 	}
850 	return (error);
851 }
852 
853 int
854 zvol_set_volsize(const char *name, major_t maj, uint64_t volsize)
855 {
856 	zvol_state_t *zv;
857 	int error;
858 	dmu_object_info_t doi;
859 	uint64_t old_volsize = 0ULL;
860 
861 	mutex_enter(&zvol_state_lock);
862 
863 	if ((zv = zvol_minor_lookup(name)) == NULL) {
864 		mutex_exit(&zvol_state_lock);
865 		return (ENXIO);
866 	}
867 	old_volsize = zv->zv_volsize;
868 
869 	if ((error = dmu_object_info(zv->zv_objset, ZVOL_OBJ, &doi)) != 0 ||
870 	    (error = zvol_check_volsize(volsize,
871 	    doi.doi_data_block_size)) != 0) {
872 		mutex_exit(&zvol_state_lock);
873 		return (error);
874 	}
875 
876 	if (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY)) {
877 		mutex_exit(&zvol_state_lock);
878 		return (EROFS);
879 	}
880 
881 	error = zvol_update_volsize(zv, maj, volsize);
882 
883 	/*
884 	 * Reinitialize the dump area to the new size. If we
885 	 * failed to resize the dump area then restore the it back to
886 	 * it's original size.
887 	 */
888 	if (error == 0 && zv->zv_flags & ZVOL_DUMPIFIED) {
889 		if ((error = zvol_dumpify(zv)) != 0 ||
890 		    (error = dumpvp_resize()) != 0) {
891 			(void) zvol_update_volsize(zv, maj, old_volsize);
892 			error = zvol_dumpify(zv);
893 		}
894 	}
895 
896 	mutex_exit(&zvol_state_lock);
897 
898 	return (error);
899 }
900 
901 int
902 zvol_set_volblocksize(const char *name, uint64_t volblocksize)
903 {
904 	zvol_state_t *zv;
905 	dmu_tx_t *tx;
906 	int error;
907 
908 	mutex_enter(&zvol_state_lock);
909 
910 	if ((zv = zvol_minor_lookup(name)) == NULL) {
911 		mutex_exit(&zvol_state_lock);
912 		return (ENXIO);
913 	}
914 	if (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY)) {
915 		mutex_exit(&zvol_state_lock);
916 		return (EROFS);
917 	}
918 
919 	tx = dmu_tx_create(zv->zv_objset);
920 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
921 	error = dmu_tx_assign(tx, TXG_WAIT);
922 	if (error) {
923 		dmu_tx_abort(tx);
924 	} else {
925 		error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ,
926 		    volblocksize, 0, tx);
927 		if (error == ENOTSUP)
928 			error = EBUSY;
929 		dmu_tx_commit(tx);
930 	}
931 
932 	mutex_exit(&zvol_state_lock);
933 
934 	return (error);
935 }
936 
937 /*ARGSUSED*/
938 int
939 zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr)
940 {
941 	minor_t minor = getminor(*devp);
942 	zvol_state_t *zv;
943 
944 	if (minor == 0)			/* This is the control device */
945 		return (0);
946 
947 	mutex_enter(&zvol_state_lock);
948 
949 	zv = ddi_get_soft_state(zvol_state, minor);
950 	if (zv == NULL) {
951 		mutex_exit(&zvol_state_lock);
952 		return (ENXIO);
953 	}
954 
955 	ASSERT(zv->zv_objset != NULL);
956 
957 	if ((flag & FWRITE) &&
958 	    (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY))) {
959 		mutex_exit(&zvol_state_lock);
960 		return (EROFS);
961 	}
962 
963 	if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) {
964 		zv->zv_open_count[otyp]++;
965 		zv->zv_total_opens++;
966 	}
967 
968 	mutex_exit(&zvol_state_lock);
969 
970 	return (0);
971 }
972 
973 /*ARGSUSED*/
974 int
975 zvol_close(dev_t dev, int flag, int otyp, cred_t *cr)
976 {
977 	minor_t minor = getminor(dev);
978 	zvol_state_t *zv;
979 
980 	if (minor == 0)		/* This is the control device */
981 		return (0);
982 
983 	mutex_enter(&zvol_state_lock);
984 
985 	zv = ddi_get_soft_state(zvol_state, minor);
986 	if (zv == NULL) {
987 		mutex_exit(&zvol_state_lock);
988 		return (ENXIO);
989 	}
990 
991 	/*
992 	 * The next statement is a workaround for the following DDI bug:
993 	 * 6343604 specfs race: multiple "last-close" of the same device
994 	 */
995 	if (zv->zv_total_opens == 0) {
996 		mutex_exit(&zvol_state_lock);
997 		return (0);
998 	}
999 
1000 	/*
1001 	 * If the open count is zero, this is a spurious close.
1002 	 * That indicates a bug in the kernel / DDI framework.
1003 	 */
1004 	ASSERT(zv->zv_open_count[otyp] != 0);
1005 	ASSERT(zv->zv_total_opens != 0);
1006 
1007 	/*
1008 	 * You may get multiple opens, but only one close.
1009 	 */
1010 	zv->zv_open_count[otyp]--;
1011 	zv->zv_total_opens--;
1012 
1013 	mutex_exit(&zvol_state_lock);
1014 
1015 	return (0);
1016 }
1017 
1018 static void
1019 zvol_get_done(dmu_buf_t *db, void *vzgd)
1020 {
1021 	zgd_t *zgd = (zgd_t *)vzgd;
1022 	rl_t *rl = zgd->zgd_rl;
1023 
1024 	dmu_buf_rele(db, vzgd);
1025 	zfs_range_unlock(rl);
1026 	zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1027 	kmem_free(zgd, sizeof (zgd_t));
1028 }
1029 
1030 /*
1031  * Get data to generate a TX_WRITE intent log record.
1032  */
1033 static int
1034 zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1035 {
1036 	zvol_state_t *zv = arg;
1037 	objset_t *os = zv->zv_objset;
1038 	dmu_buf_t *db;
1039 	rl_t *rl;
1040 	zgd_t *zgd;
1041 	uint64_t boff; 			/* block starting offset */
1042 	int dlen = lr->lr_length;	/* length of user data */
1043 	int error;
1044 
1045 	ASSERT(zio);
1046 	ASSERT(dlen != 0);
1047 
1048 	/*
1049 	 * Write records come in two flavors: immediate and indirect.
1050 	 * For small writes it's cheaper to store the data with the
1051 	 * log record (immediate); for large writes it's cheaper to
1052 	 * sync the data and get a pointer to it (indirect) so that
1053 	 * we don't have to write the data twice.
1054 	 */
1055 	if (buf != NULL) /* immediate write */
1056 		return (dmu_read(os, ZVOL_OBJ, lr->lr_offset, dlen, buf));
1057 
1058 	zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP);
1059 	zgd->zgd_zilog = zv->zv_zilog;
1060 	zgd->zgd_bp = &lr->lr_blkptr;
1061 
1062 	/*
1063 	 * Lock the range of the block to ensure that when the data is
1064 	 * written out and its checksum is being calculated that no other
1065 	 * thread can change the block.
1066 	 */
1067 	boff = P2ALIGN_TYPED(lr->lr_offset, zv->zv_volblocksize, uint64_t);
1068 	rl = zfs_range_lock(&zv->zv_znode, boff, zv->zv_volblocksize,
1069 	    RL_READER);
1070 	zgd->zgd_rl = rl;
1071 
1072 	VERIFY(0 == dmu_buf_hold(os, ZVOL_OBJ, lr->lr_offset, zgd, &db));
1073 	error = dmu_sync(zio, db, &lr->lr_blkptr,
1074 	    lr->lr_common.lrc_txg, zvol_get_done, zgd);
1075 	if (error == 0)
1076 		zil_add_block(zv->zv_zilog, &lr->lr_blkptr);
1077 	/*
1078 	 * If we get EINPROGRESS, then we need to wait for a
1079 	 * write IO initiated by dmu_sync() to complete before
1080 	 * we can release this dbuf.  We will finish everything
1081 	 * up in the zvol_get_done() callback.
1082 	 */
1083 	if (error == EINPROGRESS)
1084 		return (0);
1085 	dmu_buf_rele(db, zgd);
1086 	zfs_range_unlock(rl);
1087 	kmem_free(zgd, sizeof (zgd_t));
1088 	return (error);
1089 }
1090 
1091 /*
1092  * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
1093  *
1094  * We store data in the log buffers if it's small enough.
1095  * Otherwise we will later flush the data out via dmu_sync().
1096  */
1097 ssize_t zvol_immediate_write_sz = 32768;
1098 
1099 static void
1100 zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t len)
1101 {
1102 	uint32_t blocksize = zv->zv_volblocksize;
1103 	lr_write_t *lr;
1104 
1105 	while (len) {
1106 		ssize_t nbytes = MIN(len, blocksize - P2PHASE(off, blocksize));
1107 		itx_t *itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1108 
1109 		itx->itx_wr_state =
1110 		    len > zvol_immediate_write_sz ?  WR_INDIRECT : WR_NEED_COPY;
1111 		itx->itx_private = zv;
1112 		lr = (lr_write_t *)&itx->itx_lr;
1113 		lr->lr_foid = ZVOL_OBJ;
1114 		lr->lr_offset = off;
1115 		lr->lr_length = nbytes;
1116 		lr->lr_blkoff = off - P2ALIGN_TYPED(off, blocksize, uint64_t);
1117 		BP_ZERO(&lr->lr_blkptr);
1118 
1119 		(void) zil_itx_assign(zv->zv_zilog, itx, tx);
1120 		len -= nbytes;
1121 		off += nbytes;
1122 	}
1123 }
1124 
1125 int
1126 zvol_dumpio(vdev_t *vd, uint64_t size, uint64_t offset, void *addr,
1127     int bflags, int isdump)
1128 {
1129 	vdev_disk_t *dvd;
1130 	int direction;
1131 	int c;
1132 	int numerrors = 0;
1133 
1134 	for (c = 0; c < vd->vdev_children; c++) {
1135 		if (zvol_dumpio(vd->vdev_child[c], size, offset,
1136 		    addr, bflags, isdump) != 0) {
1137 			numerrors++;
1138 		} else if (bflags & B_READ) {
1139 			break;
1140 		}
1141 	}
1142 
1143 	if (!vd->vdev_ops->vdev_op_leaf)
1144 		return (numerrors < vd->vdev_children ? 0 : EIO);
1145 
1146 	if (!vdev_writeable(vd))
1147 		return (EIO);
1148 
1149 	dvd = vd->vdev_tsd;
1150 	ASSERT3P(dvd, !=, NULL);
1151 	direction = bflags & (B_WRITE | B_READ);
1152 	ASSERT(ISP2(direction));
1153 	offset += VDEV_LABEL_START_SIZE;
1154 
1155 	if (ddi_in_panic() || isdump) {
1156 		if (direction & B_READ)
1157 			return (EIO);
1158 		return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset),
1159 		    lbtodb(size)));
1160 	} else {
1161 		return (vdev_disk_physio(dvd->vd_lh, addr, size, offset,
1162 		    direction));
1163 	}
1164 }
1165 
1166 int
1167 zvol_physio(zvol_state_t *zv, int bflags, uint64_t off,
1168     uint64_t size, void *addr, int isdump)
1169 {
1170 	dva_t dva;
1171 	vdev_t *vd;
1172 	int error;
1173 	spa_t *spa = dmu_objset_spa(zv->zv_objset);
1174 
1175 	ASSERT(size <= zv->zv_volblocksize);
1176 
1177 	/* restrict requests to multiples of the system block size */
1178 	if (P2PHASE(off, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE))
1179 		return (EINVAL);
1180 
1181 	if (zvol_get_dva(zv, off, &dva) != 0)
1182 		return (EIO);
1183 
1184 	spa_config_enter(spa, RW_READER, FTAG);
1185 	vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva));
1186 
1187 	error = zvol_dumpio(vd, size,
1188 	    DVA_GET_OFFSET(&dva) + (off % zv->zv_volblocksize),
1189 	    addr, bflags & (B_READ | B_WRITE | B_PHYS), isdump);
1190 
1191 	spa_config_exit(spa, FTAG);
1192 	return (error);
1193 }
1194 
1195 int
1196 zvol_strategy(buf_t *bp)
1197 {
1198 	zvol_state_t *zv = ddi_get_soft_state(zvol_state, getminor(bp->b_edev));
1199 	uint64_t off, volsize;
1200 	size_t size, resid;
1201 	char *addr;
1202 	objset_t *os;
1203 	rl_t *rl;
1204 	int error = 0;
1205 	boolean_t reading, is_dump = zv->zv_flags & ZVOL_DUMPIFIED;
1206 
1207 	if (zv == NULL) {
1208 		bioerror(bp, ENXIO);
1209 		biodone(bp);
1210 		return (0);
1211 	}
1212 
1213 	if (getminor(bp->b_edev) == 0) {
1214 		bioerror(bp, EINVAL);
1215 		biodone(bp);
1216 		return (0);
1217 	}
1218 
1219 	if (!(bp->b_flags & B_READ) &&
1220 	    (zv->zv_flags & ZVOL_RDONLY ||
1221 	    zv->zv_mode & DS_MODE_READONLY)) {
1222 		bioerror(bp, EROFS);
1223 		biodone(bp);
1224 		return (0);
1225 	}
1226 
1227 	off = ldbtob(bp->b_blkno);
1228 	volsize = zv->zv_volsize;
1229 
1230 	os = zv->zv_objset;
1231 	ASSERT(os != NULL);
1232 
1233 	bp_mapin(bp);
1234 	addr = bp->b_un.b_addr;
1235 	resid = bp->b_bcount;
1236 
1237 	if (resid > 0 && (off < 0 || off >= volsize))
1238 		return (EIO);
1239 
1240 	/*
1241 	 * There must be no buffer changes when doing a dmu_sync() because
1242 	 * we can't change the data whilst calculating the checksum.
1243 	 */
1244 	reading = bp->b_flags & B_READ;
1245 	rl = zfs_range_lock(&zv->zv_znode, off, resid,
1246 	    reading ? RL_READER : RL_WRITER);
1247 
1248 	if (resid > volsize - off)	/* don't write past the end */
1249 		resid = volsize - off;
1250 
1251 	while (resid != 0 && off < volsize) {
1252 
1253 		size = MIN(resid, zvol_maxphys);
1254 		if (is_dump) {
1255 			/* can't straddle a block boundary */
1256 			size = MIN(size, P2END(off, zv->zv_volblocksize) - off);
1257 			error = zvol_physio(zv, bp->b_flags, off, size,
1258 			    addr, 0);
1259 		} else if (reading) {
1260 			error = dmu_read(os, ZVOL_OBJ, off, size, addr);
1261 		} else {
1262 			dmu_tx_t *tx = dmu_tx_create(os);
1263 			dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
1264 			error = dmu_tx_assign(tx, TXG_WAIT);
1265 			if (error) {
1266 				dmu_tx_abort(tx);
1267 			} else {
1268 				dmu_write(os, ZVOL_OBJ, off, size, addr, tx);
1269 				zvol_log_write(zv, tx, off, size);
1270 				dmu_tx_commit(tx);
1271 			}
1272 		}
1273 		if (error)
1274 			break;
1275 		off += size;
1276 		addr += size;
1277 		resid -= size;
1278 	}
1279 	zfs_range_unlock(rl);
1280 
1281 	if ((bp->b_resid = resid) == bp->b_bcount)
1282 		bioerror(bp, off > volsize ? EINVAL : error);
1283 
1284 	if (!(bp->b_flags & B_ASYNC) && !reading && !zil_disable && !is_dump)
1285 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1286 	biodone(bp);
1287 
1288 	return (0);
1289 }
1290 
1291 /*
1292  * Set the buffer count to the zvol maximum transfer.
1293  * Using our own routine instead of the default minphys()
1294  * means that for larger writes we write bigger buffers on X86
1295  * (128K instead of 56K) and flush the disk write cache less often
1296  * (every zvol_maxphys - currently 1MB) instead of minphys (currently
1297  * 56K on X86 and 128K on sparc).
1298  */
1299 void
1300 zvol_minphys(struct buf *bp)
1301 {
1302 	if (bp->b_bcount > zvol_maxphys)
1303 		bp->b_bcount = zvol_maxphys;
1304 }
1305 
1306 int
1307 zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks)
1308 {
1309 	minor_t minor = getminor(dev);
1310 	zvol_state_t *zv;
1311 	int error = 0;
1312 	uint64_t size;
1313 	uint64_t boff;
1314 	uint64_t resid;
1315 
1316 	if (minor == 0)			/* This is the control device */
1317 		return (ENXIO);
1318 
1319 	zv = ddi_get_soft_state(zvol_state, minor);
1320 	if (zv == NULL)
1321 		return (ENXIO);
1322 
1323 	boff = ldbtob(blkno);
1324 	resid = ldbtob(nblocks);
1325 	if (boff + resid > zv->zv_volsize) {
1326 		/* dump should know better than to write here */
1327 		ASSERT(blkno + resid <= zv->zv_volsize);
1328 		return (EIO);
1329 	}
1330 	while (resid) {
1331 		/* can't straddle a block boundary */
1332 		size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff);
1333 
1334 		error = zvol_physio(zv, B_WRITE, boff, size, addr, 1);
1335 		if (error)
1336 			break;
1337 		boff += size;
1338 		addr += size;
1339 		resid -= size;
1340 	}
1341 
1342 	return (error);
1343 }
1344 
1345 /*ARGSUSED*/
1346 int
1347 zvol_read(dev_t dev, uio_t *uio, cred_t *cr)
1348 {
1349 	minor_t minor = getminor(dev);
1350 	zvol_state_t *zv;
1351 	uint64_t volsize;
1352 	rl_t *rl;
1353 	int error = 0;
1354 
1355 	if (minor == 0)			/* This is the control device */
1356 		return (ENXIO);
1357 
1358 	zv = ddi_get_soft_state(zvol_state, minor);
1359 	if (zv == NULL)
1360 		return (ENXIO);
1361 
1362 	volsize = zv->zv_volsize;
1363 	if (uio->uio_resid > 0 &&
1364 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
1365 		return (EIO);
1366 
1367 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1368 	    RL_READER);
1369 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1370 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1371 
1372 		/* don't read past the end */
1373 		if (bytes > volsize - uio->uio_loffset)
1374 			bytes = volsize - uio->uio_loffset;
1375 
1376 		error =  dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes);
1377 		if (error)
1378 			break;
1379 	}
1380 	zfs_range_unlock(rl);
1381 	return (error);
1382 }
1383 
1384 /*ARGSUSED*/
1385 int
1386 zvol_write(dev_t dev, uio_t *uio, cred_t *cr)
1387 {
1388 	minor_t minor = getminor(dev);
1389 	zvol_state_t *zv;
1390 	uint64_t volsize;
1391 	rl_t *rl;
1392 	int error = 0;
1393 
1394 	if (minor == 0)			/* This is the control device */
1395 		return (ENXIO);
1396 
1397 	zv = ddi_get_soft_state(zvol_state, minor);
1398 	if (zv == NULL)
1399 		return (ENXIO);
1400 
1401 	volsize = zv->zv_volsize;
1402 	if (uio->uio_resid > 0 &&
1403 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
1404 		return (EIO);
1405 
1406 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
1407 		error = physio(zvol_strategy, NULL, dev, B_WRITE,
1408 		    zvol_minphys, uio);
1409 		return (error);
1410 	}
1411 
1412 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1413 	    RL_WRITER);
1414 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1415 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1416 		uint64_t off = uio->uio_loffset;
1417 		dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
1418 
1419 		if (bytes > volsize - off)	/* don't write past the end */
1420 			bytes = volsize - off;
1421 
1422 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
1423 		error = dmu_tx_assign(tx, TXG_WAIT);
1424 		if (error) {
1425 			dmu_tx_abort(tx);
1426 			break;
1427 		}
1428 		error = dmu_write_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes, tx);
1429 		if (error == 0)
1430 			zvol_log_write(zv, tx, off, bytes);
1431 		dmu_tx_commit(tx);
1432 
1433 		if (error)
1434 			break;
1435 	}
1436 	zfs_range_unlock(rl);
1437 	return (error);
1438 }
1439 
1440 /*
1441  * Dirtbag ioctls to support mkfs(1M) for UFS filesystems.  See dkio(7I).
1442  */
1443 /*ARGSUSED*/
1444 int
1445 zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
1446 {
1447 	zvol_state_t *zv;
1448 	struct dk_cinfo dki;
1449 	struct dk_minfo dkm;
1450 	dk_efi_t efi;
1451 	struct dk_callback *dkc;
1452 	struct uuid uuid = EFI_RESERVED;
1453 	uint32_t crc;
1454 	int error = 0;
1455 	rl_t *rl;
1456 
1457 	mutex_enter(&zvol_state_lock);
1458 
1459 	zv = ddi_get_soft_state(zvol_state, getminor(dev));
1460 
1461 	if (zv == NULL) {
1462 		mutex_exit(&zvol_state_lock);
1463 		return (ENXIO);
1464 	}
1465 
1466 	switch (cmd) {
1467 
1468 	case DKIOCINFO:
1469 		bzero(&dki, sizeof (dki));
1470 		(void) strcpy(dki.dki_cname, "zvol");
1471 		(void) strcpy(dki.dki_dname, "zvol");
1472 		dki.dki_ctype = DKC_UNKNOWN;
1473 		dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs);
1474 		mutex_exit(&zvol_state_lock);
1475 		if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag))
1476 			error = EFAULT;
1477 		return (error);
1478 
1479 	case DKIOCGMEDIAINFO:
1480 		bzero(&dkm, sizeof (dkm));
1481 		dkm.dki_lbsize = 1U << zv->zv_min_bs;
1482 		dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
1483 		dkm.dki_media_type = DK_UNKNOWN;
1484 		mutex_exit(&zvol_state_lock);
1485 		if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
1486 			error = EFAULT;
1487 		return (error);
1488 
1489 	case DKIOCGETEFI:
1490 		if (ddi_copyin((void *)arg, &efi, sizeof (dk_efi_t), flag)) {
1491 			mutex_exit(&zvol_state_lock);
1492 			return (EFAULT);
1493 		}
1494 		efi.dki_data = (void *)(uintptr_t)efi.dki_data_64;
1495 
1496 		/*
1497 		 * Some clients may attempt to request a PMBR for the
1498 		 * zvol.  Currently this interface will return ENOTTY to
1499 		 * such requests.  These requests could be supported by
1500 		 * adding a check for lba == 0 and consing up an appropriate
1501 		 * PMBR.
1502 		 */
1503 		if (efi.dki_lba == 1) {
1504 			efi_gpt_t gpt;
1505 			efi_gpe_t gpe;
1506 
1507 			bzero(&gpt, sizeof (gpt));
1508 			bzero(&gpe, sizeof (gpe));
1509 
1510 			if (efi.dki_length < sizeof (gpt)) {
1511 				mutex_exit(&zvol_state_lock);
1512 				return (EINVAL);
1513 			}
1514 
1515 			gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
1516 			gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
1517 			gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt));
1518 			gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL);
1519 			gpt.efi_gpt_LastUsableLBA =
1520 			    LE_64((zv->zv_volsize >> zv->zv_min_bs) - 1);
1521 			gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
1522 			gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL);
1523 			gpt.efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (gpe));
1524 
1525 			UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
1526 			gpe.efi_gpe_StartingLBA = gpt.efi_gpt_FirstUsableLBA;
1527 			gpe.efi_gpe_EndingLBA = gpt.efi_gpt_LastUsableLBA;
1528 
1529 			CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
1530 			gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
1531 
1532 			CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table);
1533 			gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
1534 
1535 			mutex_exit(&zvol_state_lock);
1536 			if (ddi_copyout(&gpt, efi.dki_data, sizeof (gpt), flag))
1537 				error = EFAULT;
1538 		} else if (efi.dki_lba == 2) {
1539 			efi_gpe_t gpe;
1540 
1541 			bzero(&gpe, sizeof (gpe));
1542 
1543 			if (efi.dki_length < sizeof (gpe)) {
1544 				mutex_exit(&zvol_state_lock);
1545 				return (EINVAL);
1546 			}
1547 
1548 			UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
1549 			gpe.efi_gpe_StartingLBA = LE_64(34ULL);
1550 			gpe.efi_gpe_EndingLBA =
1551 			    LE_64((zv->zv_volsize >> zv->zv_min_bs) - 1);
1552 
1553 			mutex_exit(&zvol_state_lock);
1554 			if (ddi_copyout(&gpe, efi.dki_data, sizeof (gpe), flag))
1555 				error = EFAULT;
1556 		} else {
1557 			mutex_exit(&zvol_state_lock);
1558 			error = EINVAL;
1559 		}
1560 		return (error);
1561 
1562 	case DKIOCFLUSHWRITECACHE:
1563 		dkc = (struct dk_callback *)arg;
1564 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1565 		if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) {
1566 			(*dkc->dkc_callback)(dkc->dkc_cookie, error);
1567 			error = 0;
1568 		}
1569 		break;
1570 
1571 	case DKIOCGGEOM:
1572 	case DKIOCGVTOC:
1573 		/*
1574 		 * commands using these (like prtvtoc) expect ENOTSUP
1575 		 * since we're emulating an EFI label
1576 		 */
1577 		error = ENOTSUP;
1578 		break;
1579 
1580 	case DKIOCDUMPINIT:
1581 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1582 		    RL_WRITER);
1583 		error = zvol_dumpify(zv);
1584 		zfs_range_unlock(rl);
1585 		break;
1586 
1587 	case DKIOCDUMPFINI:
1588 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1589 		    RL_WRITER);
1590 		error = zvol_dump_fini(zv);
1591 		zfs_range_unlock(rl);
1592 		break;
1593 
1594 	default:
1595 		error = ENOTTY;
1596 		break;
1597 
1598 	}
1599 	mutex_exit(&zvol_state_lock);
1600 	return (error);
1601 }
1602 
1603 int
1604 zvol_busy(void)
1605 {
1606 	return (zvol_minors != 0);
1607 }
1608 
1609 void
1610 zvol_init(void)
1611 {
1612 	VERIFY(ddi_soft_state_init(&zvol_state, sizeof (zvol_state_t), 1) == 0);
1613 	mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
1614 }
1615 
1616 void
1617 zvol_fini(void)
1618 {
1619 	mutex_destroy(&zvol_state_lock);
1620 	ddi_soft_state_fini(&zvol_state);
1621 }
1622 
1623 static boolean_t
1624 zvol_is_swap(zvol_state_t *zv)
1625 {
1626 	vnode_t *vp;
1627 	boolean_t ret = B_FALSE;
1628 	char *devpath;
1629 	size_t devpathlen;
1630 	int error;
1631 
1632 	devpathlen = strlen(ZVOL_FULL_DEV_DIR) + strlen(zv->zv_name) + 1;
1633 	devpath = kmem_alloc(devpathlen, KM_SLEEP);
1634 	(void) sprintf(devpath, "%s%s", ZVOL_FULL_DEV_DIR, zv->zv_name);
1635 	error = lookupname(devpath, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp);
1636 	kmem_free(devpath, devpathlen);
1637 
1638 	ret = !error && IS_SWAPVP(common_specvp(vp));
1639 
1640 	if (vp != NULL)
1641 		VN_RELE(vp);
1642 
1643 	return (ret);
1644 }
1645 
1646 static int
1647 zvol_dump_init(zvol_state_t *zv, boolean_t resize)
1648 {
1649 	dmu_tx_t *tx;
1650 	int error = 0;
1651 	objset_t *os = zv->zv_objset;
1652 	nvlist_t *nv = NULL;
1653 	uint64_t checksum, compress, refresrv;
1654 
1655 	ASSERT(MUTEX_HELD(&zvol_state_lock));
1656 
1657 	tx = dmu_tx_create(os);
1658 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1659 	error = dmu_tx_assign(tx, TXG_WAIT);
1660 	if (error) {
1661 		dmu_tx_abort(tx);
1662 		return (error);
1663 	}
1664 
1665 	/*
1666 	 * If we are resizing the dump device then we only need to
1667 	 * update the refreservation to match the newly updated
1668 	 * zvolsize. Otherwise, we save off the original state of the
1669 	 * zvol so that we can restore them if the zvol is ever undumpified.
1670 	 */
1671 	if (resize) {
1672 		error = zap_update(os, ZVOL_ZAP_OBJ,
1673 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1674 		    &zv->zv_volsize, tx);
1675 	} else {
1676 		error = dsl_prop_get_integer(zv->zv_name,
1677 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL);
1678 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
1679 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL);
1680 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
1681 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL);
1682 
1683 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1684 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1,
1685 		    &compress, tx);
1686 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1687 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx);
1688 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1689 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1690 		    &refresrv, tx);
1691 	}
1692 	dmu_tx_commit(tx);
1693 
1694 	/* Truncate the file */
1695 	if (!error)
1696 		error = dmu_free_long_range(zv->zv_objset,
1697 		    ZVOL_OBJ, 0, DMU_OBJECT_END);
1698 
1699 	if (error)
1700 		return (error);
1701 
1702 	/*
1703 	 * We only need update the zvol's property if we are initializing
1704 	 * the dump area for the first time.
1705 	 */
1706 	if (!resize) {
1707 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1708 		VERIFY(nvlist_add_uint64(nv,
1709 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0);
1710 		VERIFY(nvlist_add_uint64(nv,
1711 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
1712 		    ZIO_COMPRESS_OFF) == 0);
1713 		VERIFY(nvlist_add_uint64(nv,
1714 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
1715 		    ZIO_CHECKSUM_OFF) == 0);
1716 
1717 		error = zfs_set_prop_nvlist(zv->zv_name, nv);
1718 		nvlist_free(nv);
1719 
1720 		if (error)
1721 			return (error);
1722 	}
1723 
1724 	/* Allocate the space for the dump */
1725 	error = zvol_prealloc(zv);
1726 	return (error);
1727 }
1728 
1729 static int
1730 zvol_dumpify(zvol_state_t *zv)
1731 {
1732 	int error = 0;
1733 	uint64_t dumpsize = 0;
1734 	dmu_tx_t *tx;
1735 	objset_t *os = zv->zv_objset;
1736 
1737 	if (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY))
1738 		return (EROFS);
1739 
1740 	/*
1741 	 * We do not support swap devices acting as dump devices.
1742 	 */
1743 	if (zvol_is_swap(zv))
1744 		return (ENOTSUP);
1745 
1746 	if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE,
1747 	    8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) {
1748 		boolean_t resize = (dumpsize > 0) ? B_TRUE : B_FALSE;
1749 
1750 		if ((error = zvol_dump_init(zv, resize)) != 0) {
1751 			(void) zvol_dump_fini(zv);
1752 			return (error);
1753 		}
1754 	}
1755 
1756 	/*
1757 	 * Build up our lba mapping.
1758 	 */
1759 	error = zvol_get_lbas(zv);
1760 	if (error) {
1761 		(void) zvol_dump_fini(zv);
1762 		return (error);
1763 	}
1764 
1765 	tx = dmu_tx_create(os);
1766 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1767 	error = dmu_tx_assign(tx, TXG_WAIT);
1768 	if (error) {
1769 		dmu_tx_abort(tx);
1770 		(void) zvol_dump_fini(zv);
1771 		return (error);
1772 	}
1773 
1774 	zv->zv_flags |= ZVOL_DUMPIFIED;
1775 	error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1,
1776 	    &zv->zv_volsize, tx);
1777 	dmu_tx_commit(tx);
1778 
1779 	if (error) {
1780 		(void) zvol_dump_fini(zv);
1781 		return (error);
1782 	}
1783 
1784 	txg_wait_synced(dmu_objset_pool(os), 0);
1785 	return (0);
1786 }
1787 
1788 static int
1789 zvol_dump_fini(zvol_state_t *zv)
1790 {
1791 	dmu_tx_t *tx;
1792 	objset_t *os = zv->zv_objset;
1793 	nvlist_t *nv;
1794 	int error = 0;
1795 	uint64_t checksum, compress, refresrv;
1796 
1797 	/*
1798 	 * Attempt to restore the zvol back to its pre-dumpified state.
1799 	 * This is a best-effort attempt as it's possible that not all
1800 	 * of these properties were initialized during the dumpify process
1801 	 * (i.e. error during zvol_dump_init).
1802 	 */
1803 
1804 	tx = dmu_tx_create(os);
1805 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1806 	error = dmu_tx_assign(tx, TXG_WAIT);
1807 	if (error) {
1808 		dmu_tx_abort(tx);
1809 		return (error);
1810 	}
1811 	(void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx);
1812 	dmu_tx_commit(tx);
1813 
1814 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1815 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum);
1816 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1817 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress);
1818 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1819 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv);
1820 
1821 	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1822 	(void) nvlist_add_uint64(nv,
1823 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum);
1824 	(void) nvlist_add_uint64(nv,
1825 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress);
1826 	(void) nvlist_add_uint64(nv,
1827 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv);
1828 	(void) zfs_set_prop_nvlist(zv->zv_name, nv);
1829 	nvlist_free(nv);
1830 
1831 	zvol_free_extents(zv);
1832 	zv->zv_flags &= ~ZVOL_DUMPIFIED;
1833 	(void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END);
1834 
1835 	return (0);
1836 }
1837