xref: /titanic_41/usr/src/uts/common/fs/zfs/zvol.c (revision 6dfee4834394825da35b977ca71cdc965bc7b6a4)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * ZFS volume emulation driver.
31  *
32  * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes.
33  * Volumes are accessed through the symbolic links named:
34  *
35  * /dev/zvol/dsk/<pool_name>/<dataset_name>
36  * /dev/zvol/rdsk/<pool_name>/<dataset_name>
37  *
38  * These links are created by the ZFS-specific devfsadm link generator.
39  * Volumes are persistent through reboot.  No user command needs to be
40  * run before opening and using a device.
41  */
42 
43 #include <sys/types.h>
44 #include <sys/param.h>
45 #include <sys/errno.h>
46 #include <sys/aio_req.h>
47 #include <sys/uio.h>
48 #include <sys/buf.h>
49 #include <sys/modctl.h>
50 #include <sys/open.h>
51 #include <sys/kmem.h>
52 #include <sys/conf.h>
53 #include <sys/cmn_err.h>
54 #include <sys/stat.h>
55 #include <sys/zap.h>
56 #include <sys/spa.h>
57 #include <sys/zio.h>
58 #include <sys/dsl_prop.h>
59 #include <sys/dkio.h>
60 #include <sys/efi_partition.h>
61 #include <sys/byteorder.h>
62 #include <sys/pathname.h>
63 #include <sys/ddi.h>
64 #include <sys/sunddi.h>
65 #include <sys/crc32.h>
66 #include <sys/dirent.h>
67 #include <sys/policy.h>
68 #include <sys/fs/zfs.h>
69 #include <sys/zfs_ioctl.h>
70 #include <sys/mkdev.h>
71 
72 #include "zfs_namecheck.h"
73 
74 #define	ZVOL_OBJ		1ULL
75 #define	ZVOL_ZAP_OBJ		2ULL
76 
77 static void *zvol_state;
78 
79 /*
80  * This lock protects the zvol_state structure from being modified
81  * while it's being used, e.g. an open that comes in before a create
82  * finishes.  It also protects temporary opens of the dataset so that,
83  * e.g., an open doesn't get a spurious EBUSY.
84  */
85 static kmutex_t zvol_state_lock;
86 static uint32_t zvol_minors;
87 
88 /*
89  * The in-core state of each volume.
90  */
91 typedef struct zvol_state {
92 	char		zv_name[MAXPATHLEN]; /* pool/dd name */
93 	uint64_t	zv_volsize;	/* amount of space we advertise */
94 	minor_t		zv_minor;	/* minor number */
95 	uint8_t		zv_min_bs;	/* minimum addressable block shift */
96 	uint8_t		zv_readonly;	/* hard readonly; like write-protect */
97 	objset_t	*zv_objset;	/* objset handle */
98 	uint32_t	zv_mode;	/* DS_MODE_* flags at open time */
99 	uint32_t	zv_open_count[OTYPCNT];	/* open counts */
100 	uint32_t	zv_total_opens;	/* total open count */
101 } zvol_state_t;
102 
103 static void
104 zvol_size_changed(zvol_state_t *zv, dev_t dev)
105 {
106 	dev = makedevice(getmajor(dev), zv->zv_minor);
107 
108 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
109 	    "Size", zv->zv_volsize) == DDI_SUCCESS);
110 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
111 	    "Nblocks", lbtodb(zv->zv_volsize)) == DDI_SUCCESS);
112 }
113 
114 int
115 zvol_check_volsize(zfs_cmd_t *zc)
116 {
117 	if (zc->zc_volsize == 0)
118 		return (EINVAL);
119 
120 	zc->zc_volsize = P2ROUNDUP(zc->zc_volsize, SPA_MAXBLOCKSIZE);
121 #ifdef _ILP32
122 	if (zc->zc_volsize - 1 > SPEC_MAXOFFSET_T)
123 		return (EOVERFLOW);
124 #endif
125 	return (0);
126 }
127 
128 int
129 zvol_check_volblocksize(zfs_cmd_t *zc)
130 {
131 	if (zc->zc_volblocksize < SPA_MINBLOCKSIZE ||
132 	    zc->zc_volblocksize > SPA_MAXBLOCKSIZE ||
133 	    !ISP2(zc->zc_volblocksize))
134 		return (EDOM);
135 
136 	return (0);
137 }
138 
139 static void
140 zvol_readonly_changed_cb(void *arg, uint64_t newval)
141 {
142 	zvol_state_t *zv = arg;
143 
144 	zv->zv_readonly = (uint8_t)newval;
145 }
146 
147 int
148 zvol_get_stats(zfs_cmd_t *zc, objset_t *os)
149 {
150 	int error;
151 	dmu_object_info_t doi;
152 
153 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &zc->zc_volsize);
154 
155 	if (error)
156 		return (error);
157 
158 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
159 
160 	if (error == 0)
161 		zc->zc_volblocksize = doi.doi_data_block_size;
162 
163 	return (error);
164 }
165 
166 /*
167  * Find a free minor number.
168  */
169 static minor_t
170 zvol_minor_alloc(void)
171 {
172 	minor_t minor;
173 
174 	ASSERT(MUTEX_HELD(&zvol_state_lock));
175 
176 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++)
177 		if (ddi_get_soft_state(zvol_state, minor) == NULL)
178 			return (minor);
179 
180 	return (0);
181 }
182 
183 static zvol_state_t *
184 zvol_minor_lookup(char *name)
185 {
186 	minor_t minor;
187 	zvol_state_t *zv;
188 
189 	ASSERT(MUTEX_HELD(&zvol_state_lock));
190 
191 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) {
192 		zv = ddi_get_soft_state(zvol_state, minor);
193 		if (zv == NULL)
194 			continue;
195 		if (strcmp(zv->zv_name, name) == 0)
196 			break;
197 	}
198 
199 	return (zv);
200 }
201 
202 void
203 zvol_create_cb(objset_t *os, void *arg, dmu_tx_t *tx)
204 {
205 	zfs_cmd_t *zc = arg;
206 	int error;
207 
208 	error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, zc->zc_volblocksize,
209 	    DMU_OT_NONE, 0, tx);
210 	ASSERT(error == 0);
211 
212 	error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
213 	    DMU_OT_NONE, 0, tx);
214 	ASSERT(error == 0);
215 
216 	error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &zc->zc_volsize, tx);
217 	ASSERT(error == 0);
218 }
219 
220 /*
221  * Create a minor node for the specified volume.
222  */
223 int
224 zvol_create_minor(zfs_cmd_t *zc)
225 {
226 	char *name = zc->zc_name;
227 	dev_t dev = zc->zc_dev;
228 	zvol_state_t *zv;
229 	objset_t *os;
230 	uint64_t volsize;
231 	minor_t minor = 0;
232 	struct pathname linkpath;
233 	int ds_mode = DS_MODE_PRIMARY;
234 	vnode_t *vp = NULL;
235 	char *devpath;
236 	size_t devpathlen = strlen(ZVOL_FULL_DEV_DIR) + 1 + strlen(name) + 1;
237 	char chrbuf[30], blkbuf[30];
238 	int error;
239 
240 	mutex_enter(&zvol_state_lock);
241 
242 	if ((zv = zvol_minor_lookup(name)) != NULL) {
243 		mutex_exit(&zvol_state_lock);
244 		return (EEXIST);
245 	}
246 
247 	if (strchr(name, '@') != 0)
248 		ds_mode |= DS_MODE_READONLY;
249 
250 	error = dmu_objset_open(name, DMU_OST_ZVOL, ds_mode, &os);
251 
252 	if (error) {
253 		mutex_exit(&zvol_state_lock);
254 		return (error);
255 	}
256 
257 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
258 
259 	if (error) {
260 		dmu_objset_close(os);
261 		mutex_exit(&zvol_state_lock);
262 		return (error);
263 	}
264 
265 	/*
266 	 * If there's an existing /dev/zvol symlink, try to use the
267 	 * same minor number we used last time.
268 	 */
269 	devpath = kmem_alloc(devpathlen, KM_SLEEP);
270 
271 	(void) sprintf(devpath, "%s/%s", ZVOL_FULL_DEV_DIR, name);
272 
273 	error = lookupname(devpath, UIO_SYSSPACE, NO_FOLLOW, NULL, &vp);
274 
275 	kmem_free(devpath, devpathlen);
276 
277 	if (error == 0 && vp->v_type != VLNK)
278 		error = EINVAL;
279 
280 	if (error == 0) {
281 		pn_alloc(&linkpath);
282 		error = pn_getsymlink(vp, &linkpath, kcred);
283 		if (error == 0) {
284 			char *ms = strstr(linkpath.pn_path, ZVOL_PSEUDO_DEV);
285 			if (ms != NULL) {
286 				ms += strlen(ZVOL_PSEUDO_DEV);
287 				minor = stoi(&ms);
288 			}
289 		}
290 		pn_free(&linkpath);
291 	}
292 
293 	if (vp != NULL)
294 		VN_RELE(vp);
295 
296 	/*
297 	 * If we found a minor but it's already in use, we must pick a new one.
298 	 */
299 	if (minor != 0 && ddi_get_soft_state(zvol_state, minor) != NULL)
300 		minor = 0;
301 
302 	if (minor == 0)
303 		minor = zvol_minor_alloc();
304 
305 	if (minor == 0) {
306 		dmu_objset_close(os);
307 		mutex_exit(&zvol_state_lock);
308 		return (ENXIO);
309 	}
310 
311 	if (ddi_soft_state_zalloc(zvol_state, minor) != DDI_SUCCESS) {
312 		dmu_objset_close(os);
313 		mutex_exit(&zvol_state_lock);
314 		return (EAGAIN);
315 	}
316 
317 	(void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME, name);
318 
319 	(void) sprintf(chrbuf, "%uc,raw", minor);
320 
321 	if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR,
322 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
323 		ddi_soft_state_free(zvol_state, minor);
324 		dmu_objset_close(os);
325 		mutex_exit(&zvol_state_lock);
326 		return (EAGAIN);
327 	}
328 
329 	(void) sprintf(blkbuf, "%uc", minor);
330 
331 	if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK,
332 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
333 		ddi_remove_minor_node(zfs_dip, chrbuf);
334 		ddi_soft_state_free(zvol_state, minor);
335 		dmu_objset_close(os);
336 		mutex_exit(&zvol_state_lock);
337 		return (EAGAIN);
338 	}
339 
340 	zv = ddi_get_soft_state(zvol_state, minor);
341 
342 	(void) strcpy(zv->zv_name, name);
343 	zv->zv_min_bs = DEV_BSHIFT;
344 	zv->zv_minor = minor;
345 	zv->zv_volsize = volsize;
346 	zv->zv_objset = os;
347 	zv->zv_mode = ds_mode;
348 
349 	zvol_size_changed(zv, dev);
350 
351 	VERIFY(dsl_prop_register(dmu_objset_ds(zv->zv_objset),
352 	    "readonly", zvol_readonly_changed_cb, zv) == 0);
353 
354 	zvol_minors++;
355 
356 	mutex_exit(&zvol_state_lock);
357 
358 	return (0);
359 }
360 
361 /*
362  * Remove minor node for the specified volume.
363  */
364 int
365 zvol_remove_minor(zfs_cmd_t *zc)
366 {
367 	zvol_state_t *zv;
368 	char namebuf[30];
369 
370 	mutex_enter(&zvol_state_lock);
371 
372 	if ((zv = zvol_minor_lookup(zc->zc_name)) == NULL) {
373 		mutex_exit(&zvol_state_lock);
374 		return (ENXIO);
375 	}
376 
377 	if (zv->zv_total_opens != 0) {
378 		mutex_exit(&zvol_state_lock);
379 		return (EBUSY);
380 	}
381 
382 	(void) sprintf(namebuf, "%uc,raw", zv->zv_minor);
383 	ddi_remove_minor_node(zfs_dip, namebuf);
384 
385 	(void) sprintf(namebuf, "%uc", zv->zv_minor);
386 	ddi_remove_minor_node(zfs_dip, namebuf);
387 
388 	VERIFY(dsl_prop_unregister(dmu_objset_ds(zv->zv_objset),
389 	    "readonly", zvol_readonly_changed_cb, zv) == 0);
390 
391 	dmu_objset_close(zv->zv_objset);
392 
393 	zv->zv_objset = NULL;
394 
395 	ddi_soft_state_free(zvol_state, zv->zv_minor);
396 
397 	zvol_minors--;
398 
399 	mutex_exit(&zvol_state_lock);
400 
401 	return (0);
402 }
403 
404 int
405 zvol_set_volsize(zfs_cmd_t *zc)
406 {
407 	zvol_state_t *zv;
408 	dev_t dev = zc->zc_dev;
409 	dmu_tx_t *tx;
410 	int error;
411 
412 	if ((error = zvol_check_volsize(zc)) != 0)
413 		return (error);
414 
415 	mutex_enter(&zvol_state_lock);
416 
417 	if ((zv = zvol_minor_lookup(zc->zc_name)) == NULL) {
418 		mutex_exit(&zvol_state_lock);
419 		return (ENXIO);
420 	}
421 
422 	if (zv->zv_readonly || (zv->zv_mode & DS_MODE_READONLY)) {
423 		mutex_exit(&zvol_state_lock);
424 		return (EROFS);
425 	}
426 
427 	tx = dmu_tx_create(zv->zv_objset);
428 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, 1);
429 	dmu_tx_hold_free(tx, ZVOL_OBJ, zc->zc_volsize, DMU_OBJECT_END);
430 	error = dmu_tx_assign(tx, TXG_WAIT);
431 	if (error) {
432 		dmu_tx_abort(tx);
433 		mutex_exit(&zvol_state_lock);
434 		return (error);
435 	}
436 
437 	error = zap_update(zv->zv_objset, ZVOL_ZAP_OBJ, "size", 8, 1,
438 	    &zc->zc_volsize, tx);
439 	if (error == 0)
440 		dmu_free_range(zv->zv_objset, ZVOL_OBJ, zc->zc_volsize,
441 		    DMU_OBJECT_END, tx);
442 
443 	dmu_tx_commit(tx);
444 
445 	if (error == 0) {
446 		zv->zv_volsize = zc->zc_volsize;
447 		zvol_size_changed(zv, dev);
448 	}
449 
450 	mutex_exit(&zvol_state_lock);
451 
452 	return (error);
453 }
454 
455 int
456 zvol_set_volblocksize(zfs_cmd_t *zc)
457 {
458 	zvol_state_t *zv;
459 	dmu_tx_t *tx;
460 	int error;
461 
462 	mutex_enter(&zvol_state_lock);
463 
464 	if ((zv = zvol_minor_lookup(zc->zc_name)) == NULL) {
465 		mutex_exit(&zvol_state_lock);
466 		return (ENXIO);
467 	}
468 
469 	if (zv->zv_readonly || (zv->zv_mode & DS_MODE_READONLY)) {
470 		mutex_exit(&zvol_state_lock);
471 		return (EROFS);
472 	}
473 
474 	tx = dmu_tx_create(zv->zv_objset);
475 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
476 	error = dmu_tx_assign(tx, TXG_WAIT);
477 	if (error) {
478 		dmu_tx_abort(tx);
479 	} else {
480 		error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ,
481 		    zc->zc_volblocksize, 0, tx);
482 		if (error == ENOTSUP)
483 			error = EBUSY;
484 		dmu_tx_commit(tx);
485 	}
486 
487 	mutex_exit(&zvol_state_lock);
488 
489 	return (error);
490 }
491 
492 /*ARGSUSED*/
493 int
494 zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr)
495 {
496 	minor_t minor = getminor(*devp);
497 	zvol_state_t *zv;
498 
499 	if (minor == 0)			/* This is the control device */
500 		return (0);
501 
502 	mutex_enter(&zvol_state_lock);
503 
504 	zv = ddi_get_soft_state(zvol_state, minor);
505 	if (zv == NULL) {
506 		mutex_exit(&zvol_state_lock);
507 		return (ENXIO);
508 	}
509 
510 	ASSERT(zv->zv_objset != NULL);
511 
512 	if ((flag & FWRITE) &&
513 	    (zv->zv_readonly || (zv->zv_mode & DS_MODE_READONLY))) {
514 		mutex_exit(&zvol_state_lock);
515 		return (EROFS);
516 	}
517 
518 	if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) {
519 		zv->zv_open_count[otyp]++;
520 		zv->zv_total_opens++;
521 	}
522 
523 	mutex_exit(&zvol_state_lock);
524 
525 	return (0);
526 }
527 
528 /*ARGSUSED*/
529 int
530 zvol_close(dev_t dev, int flag, int otyp, cred_t *cr)
531 {
532 	minor_t minor = getminor(dev);
533 	zvol_state_t *zv;
534 
535 	if (minor == 0)		/* This is the control device */
536 		return (0);
537 
538 	mutex_enter(&zvol_state_lock);
539 
540 	zv = ddi_get_soft_state(zvol_state, minor);
541 	if (zv == NULL) {
542 		mutex_exit(&zvol_state_lock);
543 		return (ENXIO);
544 	}
545 
546 	/*
547 	 * The next statement is a workaround for the following DDI bug:
548 	 * 6343604 specfs race: multiple "last-close" of the same device
549 	 */
550 	if (zv->zv_total_opens == 0) {
551 		mutex_exit(&zvol_state_lock);
552 		return (0);
553 	}
554 
555 	/*
556 	 * If the open count is zero, this is a spurious close.
557 	 * That indicates a bug in the kernel / DDI framework.
558 	 */
559 	ASSERT(zv->zv_open_count[otyp] != 0);
560 	ASSERT(zv->zv_total_opens != 0);
561 
562 	/*
563 	 * You may get multiple opens, but only one close.
564 	 */
565 	zv->zv_open_count[otyp]--;
566 	zv->zv_total_opens--;
567 
568 	mutex_exit(&zvol_state_lock);
569 
570 	return (0);
571 }
572 
573 int
574 zvol_strategy(buf_t *bp)
575 {
576 	zvol_state_t *zv = ddi_get_soft_state(zvol_state, getminor(bp->b_edev));
577 	uint64_t off, volsize;
578 	size_t size, resid;
579 	char *addr;
580 	int error = 0;
581 
582 	if (zv == NULL) {
583 		bioerror(bp, ENXIO);
584 		biodone(bp);
585 		return (0);
586 	}
587 
588 	if (getminor(bp->b_edev) == 0) {
589 		bioerror(bp, EINVAL);
590 		biodone(bp);
591 		return (0);
592 	}
593 
594 	if (zv->zv_readonly && !(bp->b_flags & B_READ)) {
595 		bioerror(bp, EROFS);
596 		biodone(bp);
597 		return (0);
598 	}
599 
600 	off = ldbtob(bp->b_blkno);
601 	volsize = zv->zv_volsize;
602 
603 	ASSERT(zv->zv_objset != NULL);
604 
605 	bp_mapin(bp);
606 	addr = bp->b_un.b_addr;
607 	resid = bp->b_bcount;
608 
609 	while (resid != 0 && off < volsize) {
610 
611 		size = MIN(resid, 1UL << 20);	/* cap at 1MB per tx */
612 
613 		if (size > volsize - off)	/* don't write past the end */
614 			size = volsize - off;
615 
616 		if (bp->b_flags & B_READ) {
617 			error = dmu_read_canfail(zv->zv_objset, ZVOL_OBJ,
618 			    off, size, addr);
619 		} else {
620 			dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
621 			dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
622 			error = dmu_tx_assign(tx, TXG_WAIT);
623 			if (error) {
624 				dmu_tx_abort(tx);
625 			} else {
626 				dmu_write(zv->zv_objset, ZVOL_OBJ,
627 				    off, size, addr, tx);
628 				dmu_tx_commit(tx);
629 			}
630 		}
631 		if (error)
632 			break;
633 		off += size;
634 		addr += size;
635 		resid -= size;
636 	}
637 
638 	if ((bp->b_resid = resid) == bp->b_bcount)
639 		bioerror(bp, off > volsize ? EINVAL : error);
640 
641 	biodone(bp);
642 	return (0);
643 }
644 
645 /*ARGSUSED*/
646 int
647 zvol_read(dev_t dev, uio_t *uiop, cred_t *cr)
648 {
649 	return (physio(zvol_strategy, NULL, dev, B_READ, minphys, uiop));
650 }
651 
652 /*ARGSUSED*/
653 int
654 zvol_write(dev_t dev, uio_t *uiop, cred_t *cr)
655 {
656 	return (physio(zvol_strategy, NULL, dev, B_WRITE, minphys, uiop));
657 }
658 
659 /*ARGSUSED*/
660 int
661 zvol_aread(dev_t dev, struct aio_req *aio, cred_t *cr)
662 {
663 	return (aphysio(zvol_strategy, anocancel, dev, B_READ, minphys, aio));
664 }
665 
666 /*ARGSUSED*/
667 int
668 zvol_awrite(dev_t dev, struct aio_req *aio, cred_t *cr)
669 {
670 	return (aphysio(zvol_strategy, anocancel, dev, B_WRITE, minphys, aio));
671 }
672 
673 /*
674  * Dirtbag ioctls to support mkfs(1M) for UFS filesystems.  See dkio(7I).
675  */
676 /*ARGSUSED*/
677 int
678 zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
679 {
680 	zvol_state_t *zv;
681 	struct dk_cinfo dkc;
682 	struct dk_minfo dkm;
683 	dk_efi_t efi;
684 	efi_gpt_t gpt;
685 	efi_gpe_t gpe;
686 	struct uuid uuid = EFI_RESERVED;
687 	uint32_t crc;
688 	int error = 0;
689 
690 	mutex_enter(&zvol_state_lock);
691 
692 	zv = ddi_get_soft_state(zvol_state, getminor(dev));
693 
694 	if (zv == NULL) {
695 		mutex_exit(&zvol_state_lock);
696 		return (ENXIO);
697 	}
698 
699 	switch (cmd) {
700 
701 	case DKIOCINFO:
702 		bzero(&dkc, sizeof (dkc));
703 		(void) strcpy(dkc.dki_cname, "zvol");
704 		(void) strcpy(dkc.dki_dname, "zvol");
705 		dkc.dki_ctype = DKC_UNKNOWN;
706 		dkc.dki_maxtransfer = 1 << 15;
707 		mutex_exit(&zvol_state_lock);
708 		if (ddi_copyout(&dkc, (void *)arg, sizeof (dkc), flag))
709 			error = EFAULT;
710 		return (error);
711 
712 	case DKIOCGMEDIAINFO:
713 		bzero(&dkm, sizeof (dkm));
714 		dkm.dki_lbsize = 1U << zv->zv_min_bs;
715 		dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
716 		dkm.dki_media_type = DK_UNKNOWN;
717 		mutex_exit(&zvol_state_lock);
718 		if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
719 			error = EFAULT;
720 		return (error);
721 
722 	case DKIOCGETEFI:
723 		if (ddi_copyin((void *)arg, &efi, sizeof (dk_efi_t), flag)) {
724 			mutex_exit(&zvol_state_lock);
725 			return (EFAULT);
726 		}
727 
728 		bzero(&gpt, sizeof (gpt));
729 		bzero(&gpe, sizeof (gpe));
730 
731 		efi.dki_data = (void *)(uintptr_t)efi.dki_data_64;
732 
733 		if (efi.dki_length < sizeof (gpt) + sizeof (gpe)) {
734 			mutex_exit(&zvol_state_lock);
735 			return (EINVAL);
736 		}
737 
738 		efi.dki_length = sizeof (gpt) + sizeof (gpe);
739 
740 		gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
741 		gpt.efi_gpt_Revision = LE_32(EFI_VERSION102);
742 		gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt));
743 		gpt.efi_gpt_FirstUsableLBA = LE_64(0ULL);
744 		gpt.efi_gpt_LastUsableLBA =
745 		    LE_64((zv->zv_volsize >> zv->zv_min_bs) - 1);
746 		gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
747 		gpt.efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (gpe));
748 
749 		UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
750 		gpe.efi_gpe_StartingLBA = gpt.efi_gpt_FirstUsableLBA;
751 		gpe.efi_gpe_EndingLBA = gpt.efi_gpt_LastUsableLBA;
752 
753 		CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
754 		gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
755 
756 		CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table);
757 		gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
758 
759 		mutex_exit(&zvol_state_lock);
760 		if (ddi_copyout(&gpt, efi.dki_data, sizeof (gpt), flag) ||
761 		    ddi_copyout(&gpe, efi.dki_data + 1, sizeof (gpe), flag))
762 			error = EFAULT;
763 		return (error);
764 
765 	default:
766 		error = ENOTSUP;
767 		break;
768 
769 	}
770 	mutex_exit(&zvol_state_lock);
771 	return (error);
772 }
773 
774 int
775 zvol_busy(void)
776 {
777 	return (zvol_minors != 0);
778 }
779 
780 void
781 zvol_init(void)
782 {
783 	VERIFY(ddi_soft_state_init(&zvol_state, sizeof (zvol_state_t), 1) == 0);
784 	mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
785 }
786 
787 void
788 zvol_fini(void)
789 {
790 	mutex_destroy(&zvol_state_lock);
791 	ddi_soft_state_fini(&zvol_state);
792 }
793