xref: /illumos-gate/usr/src/uts/common/fs/zfs/zvol.c (revision 5c5460e9570bdcad6b03b01c30ceb683a77ceb88)
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, uint64_t blocksize)
116 {
117 	if (zc->zc_volsize == 0)
118 		return (EINVAL);
119 
120 	if (zc->zc_volsize % blocksize != 0)
121 		return (EINVAL);
122 
123 #ifdef _ILP32
124 	if (zc->zc_volsize - 1 > SPEC_MAXOFFSET_T)
125 		return (EOVERFLOW);
126 #endif
127 	return (0);
128 }
129 
130 int
131 zvol_check_volblocksize(zfs_cmd_t *zc)
132 {
133 	if (zc->zc_volblocksize < SPA_MINBLOCKSIZE ||
134 	    zc->zc_volblocksize > SPA_MAXBLOCKSIZE ||
135 	    !ISP2(zc->zc_volblocksize))
136 		return (EDOM);
137 
138 	return (0);
139 }
140 
141 static void
142 zvol_readonly_changed_cb(void *arg, uint64_t newval)
143 {
144 	zvol_state_t *zv = arg;
145 
146 	zv->zv_readonly = (uint8_t)newval;
147 }
148 
149 int
150 zvol_get_stats(zfs_cmd_t *zc, objset_t *os)
151 {
152 	int error;
153 	dmu_object_info_t doi;
154 
155 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &zc->zc_volsize);
156 
157 	if (error)
158 		return (error);
159 
160 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
161 
162 	if (error == 0)
163 		zc->zc_volblocksize = doi.doi_data_block_size;
164 
165 	return (error);
166 }
167 
168 /*
169  * Find a free minor number.
170  */
171 static minor_t
172 zvol_minor_alloc(void)
173 {
174 	minor_t minor;
175 
176 	ASSERT(MUTEX_HELD(&zvol_state_lock));
177 
178 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++)
179 		if (ddi_get_soft_state(zvol_state, minor) == NULL)
180 			return (minor);
181 
182 	return (0);
183 }
184 
185 static zvol_state_t *
186 zvol_minor_lookup(char *name)
187 {
188 	minor_t minor;
189 	zvol_state_t *zv;
190 
191 	ASSERT(MUTEX_HELD(&zvol_state_lock));
192 
193 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) {
194 		zv = ddi_get_soft_state(zvol_state, minor);
195 		if (zv == NULL)
196 			continue;
197 		if (strcmp(zv->zv_name, name) == 0)
198 			break;
199 	}
200 
201 	return (zv);
202 }
203 
204 void
205 zvol_create_cb(objset_t *os, void *arg, dmu_tx_t *tx)
206 {
207 	zfs_cmd_t *zc = arg;
208 	int error;
209 
210 	error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, zc->zc_volblocksize,
211 	    DMU_OT_NONE, 0, tx);
212 	ASSERT(error == 0);
213 
214 	error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
215 	    DMU_OT_NONE, 0, tx);
216 	ASSERT(error == 0);
217 
218 	error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &zc->zc_volsize, tx);
219 	ASSERT(error == 0);
220 }
221 
222 /*
223  * Create a minor node for the specified volume.
224  */
225 int
226 zvol_create_minor(zfs_cmd_t *zc)
227 {
228 	char *name = zc->zc_name;
229 	dev_t dev = zc->zc_dev;
230 	zvol_state_t *zv;
231 	objset_t *os;
232 	uint64_t volsize;
233 	minor_t minor = 0;
234 	struct pathname linkpath;
235 	int ds_mode = DS_MODE_PRIMARY;
236 	vnode_t *vp = NULL;
237 	char *devpath;
238 	size_t devpathlen = strlen(ZVOL_FULL_DEV_DIR) + 1 + strlen(name) + 1;
239 	char chrbuf[30], blkbuf[30];
240 	int error;
241 
242 	mutex_enter(&zvol_state_lock);
243 
244 	if ((zv = zvol_minor_lookup(name)) != NULL) {
245 		mutex_exit(&zvol_state_lock);
246 		return (EEXIST);
247 	}
248 
249 	if (strchr(name, '@') != 0)
250 		ds_mode |= DS_MODE_READONLY;
251 
252 	error = dmu_objset_open(name, DMU_OST_ZVOL, ds_mode, &os);
253 
254 	if (error) {
255 		mutex_exit(&zvol_state_lock);
256 		return (error);
257 	}
258 
259 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
260 
261 	if (error) {
262 		dmu_objset_close(os);
263 		mutex_exit(&zvol_state_lock);
264 		return (error);
265 	}
266 
267 	/*
268 	 * If there's an existing /dev/zvol symlink, try to use the
269 	 * same minor number we used last time.
270 	 */
271 	devpath = kmem_alloc(devpathlen, KM_SLEEP);
272 
273 	(void) sprintf(devpath, "%s/%s", ZVOL_FULL_DEV_DIR, name);
274 
275 	error = lookupname(devpath, UIO_SYSSPACE, NO_FOLLOW, NULL, &vp);
276 
277 	kmem_free(devpath, devpathlen);
278 
279 	if (error == 0 && vp->v_type != VLNK)
280 		error = EINVAL;
281 
282 	if (error == 0) {
283 		pn_alloc(&linkpath);
284 		error = pn_getsymlink(vp, &linkpath, kcred);
285 		if (error == 0) {
286 			char *ms = strstr(linkpath.pn_path, ZVOL_PSEUDO_DEV);
287 			if (ms != NULL) {
288 				ms += strlen(ZVOL_PSEUDO_DEV);
289 				minor = stoi(&ms);
290 			}
291 		}
292 		pn_free(&linkpath);
293 	}
294 
295 	if (vp != NULL)
296 		VN_RELE(vp);
297 
298 	/*
299 	 * If we found a minor but it's already in use, we must pick a new one.
300 	 */
301 	if (minor != 0 && ddi_get_soft_state(zvol_state, minor) != NULL)
302 		minor = 0;
303 
304 	if (minor == 0)
305 		minor = zvol_minor_alloc();
306 
307 	if (minor == 0) {
308 		dmu_objset_close(os);
309 		mutex_exit(&zvol_state_lock);
310 		return (ENXIO);
311 	}
312 
313 	if (ddi_soft_state_zalloc(zvol_state, minor) != DDI_SUCCESS) {
314 		dmu_objset_close(os);
315 		mutex_exit(&zvol_state_lock);
316 		return (EAGAIN);
317 	}
318 
319 	(void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME, name);
320 
321 	(void) sprintf(chrbuf, "%uc,raw", minor);
322 
323 	if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR,
324 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
325 		ddi_soft_state_free(zvol_state, minor);
326 		dmu_objset_close(os);
327 		mutex_exit(&zvol_state_lock);
328 		return (EAGAIN);
329 	}
330 
331 	(void) sprintf(blkbuf, "%uc", minor);
332 
333 	if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK,
334 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
335 		ddi_remove_minor_node(zfs_dip, chrbuf);
336 		ddi_soft_state_free(zvol_state, minor);
337 		dmu_objset_close(os);
338 		mutex_exit(&zvol_state_lock);
339 		return (EAGAIN);
340 	}
341 
342 	zv = ddi_get_soft_state(zvol_state, minor);
343 
344 	(void) strcpy(zv->zv_name, name);
345 	zv->zv_min_bs = DEV_BSHIFT;
346 	zv->zv_minor = minor;
347 	zv->zv_volsize = volsize;
348 	zv->zv_objset = os;
349 	zv->zv_mode = ds_mode;
350 
351 	zvol_size_changed(zv, dev);
352 
353 	VERIFY(dsl_prop_register(dmu_objset_ds(zv->zv_objset),
354 	    "readonly", zvol_readonly_changed_cb, zv) == 0);
355 
356 	zvol_minors++;
357 
358 	mutex_exit(&zvol_state_lock);
359 
360 	return (0);
361 }
362 
363 /*
364  * Remove minor node for the specified volume.
365  */
366 int
367 zvol_remove_minor(zfs_cmd_t *zc)
368 {
369 	zvol_state_t *zv;
370 	char namebuf[30];
371 
372 	mutex_enter(&zvol_state_lock);
373 
374 	if ((zv = zvol_minor_lookup(zc->zc_name)) == NULL) {
375 		mutex_exit(&zvol_state_lock);
376 		return (ENXIO);
377 	}
378 
379 	if (zv->zv_total_opens != 0) {
380 		mutex_exit(&zvol_state_lock);
381 		return (EBUSY);
382 	}
383 
384 	(void) sprintf(namebuf, "%uc,raw", zv->zv_minor);
385 	ddi_remove_minor_node(zfs_dip, namebuf);
386 
387 	(void) sprintf(namebuf, "%uc", zv->zv_minor);
388 	ddi_remove_minor_node(zfs_dip, namebuf);
389 
390 	VERIFY(dsl_prop_unregister(dmu_objset_ds(zv->zv_objset),
391 	    "readonly", zvol_readonly_changed_cb, zv) == 0);
392 
393 	dmu_objset_close(zv->zv_objset);
394 
395 	zv->zv_objset = NULL;
396 
397 	ddi_soft_state_free(zvol_state, zv->zv_minor);
398 
399 	zvol_minors--;
400 
401 	mutex_exit(&zvol_state_lock);
402 
403 	return (0);
404 }
405 
406 int
407 zvol_set_volsize(zfs_cmd_t *zc)
408 {
409 	zvol_state_t *zv;
410 	dev_t dev = zc->zc_dev;
411 	dmu_tx_t *tx;
412 	int error;
413 	dmu_object_info_t doi;
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 ((error = dmu_object_info(zv->zv_objset, ZVOL_OBJ, &doi)) != 0 ||
423 	    (error = zvol_check_volsize(zc, doi.doi_data_block_size)) != 0) {
424 		mutex_exit(&zvol_state_lock);
425 		return (error);
426 	}
427 
428 	if (zv->zv_readonly || (zv->zv_mode & DS_MODE_READONLY)) {
429 		mutex_exit(&zvol_state_lock);
430 		return (EROFS);
431 	}
432 
433 	tx = dmu_tx_create(zv->zv_objset);
434 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, 1);
435 	dmu_tx_hold_free(tx, ZVOL_OBJ, zc->zc_volsize, DMU_OBJECT_END);
436 	error = dmu_tx_assign(tx, TXG_WAIT);
437 	if (error) {
438 		dmu_tx_abort(tx);
439 		mutex_exit(&zvol_state_lock);
440 		return (error);
441 	}
442 
443 	error = zap_update(zv->zv_objset, ZVOL_ZAP_OBJ, "size", 8, 1,
444 	    &zc->zc_volsize, tx);
445 	if (error == 0)
446 		dmu_free_range(zv->zv_objset, ZVOL_OBJ, zc->zc_volsize,
447 		    DMU_OBJECT_END, tx);
448 
449 	dmu_tx_commit(tx);
450 
451 	if (error == 0) {
452 		zv->zv_volsize = zc->zc_volsize;
453 		zvol_size_changed(zv, dev);
454 	}
455 
456 	mutex_exit(&zvol_state_lock);
457 
458 	return (error);
459 }
460 
461 int
462 zvol_set_volblocksize(zfs_cmd_t *zc)
463 {
464 	zvol_state_t *zv;
465 	dmu_tx_t *tx;
466 	int error;
467 
468 	mutex_enter(&zvol_state_lock);
469 
470 	if ((zv = zvol_minor_lookup(zc->zc_name)) == NULL) {
471 		mutex_exit(&zvol_state_lock);
472 		return (ENXIO);
473 	}
474 
475 	if (zv->zv_readonly || (zv->zv_mode & DS_MODE_READONLY)) {
476 		mutex_exit(&zvol_state_lock);
477 		return (EROFS);
478 	}
479 
480 	tx = dmu_tx_create(zv->zv_objset);
481 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
482 	error = dmu_tx_assign(tx, TXG_WAIT);
483 	if (error) {
484 		dmu_tx_abort(tx);
485 	} else {
486 		error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ,
487 		    zc->zc_volblocksize, 0, tx);
488 		if (error == ENOTSUP)
489 			error = EBUSY;
490 		dmu_tx_commit(tx);
491 	}
492 
493 	mutex_exit(&zvol_state_lock);
494 
495 	return (error);
496 }
497 
498 /*ARGSUSED*/
499 int
500 zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr)
501 {
502 	minor_t minor = getminor(*devp);
503 	zvol_state_t *zv;
504 
505 	if (minor == 0)			/* This is the control device */
506 		return (0);
507 
508 	mutex_enter(&zvol_state_lock);
509 
510 	zv = ddi_get_soft_state(zvol_state, minor);
511 	if (zv == NULL) {
512 		mutex_exit(&zvol_state_lock);
513 		return (ENXIO);
514 	}
515 
516 	ASSERT(zv->zv_objset != NULL);
517 
518 	if ((flag & FWRITE) &&
519 	    (zv->zv_readonly || (zv->zv_mode & DS_MODE_READONLY))) {
520 		mutex_exit(&zvol_state_lock);
521 		return (EROFS);
522 	}
523 
524 	if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) {
525 		zv->zv_open_count[otyp]++;
526 		zv->zv_total_opens++;
527 	}
528 
529 	mutex_exit(&zvol_state_lock);
530 
531 	return (0);
532 }
533 
534 /*ARGSUSED*/
535 int
536 zvol_close(dev_t dev, int flag, int otyp, cred_t *cr)
537 {
538 	minor_t minor = getminor(dev);
539 	zvol_state_t *zv;
540 
541 	if (minor == 0)		/* This is the control device */
542 		return (0);
543 
544 	mutex_enter(&zvol_state_lock);
545 
546 	zv = ddi_get_soft_state(zvol_state, minor);
547 	if (zv == NULL) {
548 		mutex_exit(&zvol_state_lock);
549 		return (ENXIO);
550 	}
551 
552 	/*
553 	 * The next statement is a workaround for the following DDI bug:
554 	 * 6343604 specfs race: multiple "last-close" of the same device
555 	 */
556 	if (zv->zv_total_opens == 0) {
557 		mutex_exit(&zvol_state_lock);
558 		return (0);
559 	}
560 
561 	/*
562 	 * If the open count is zero, this is a spurious close.
563 	 * That indicates a bug in the kernel / DDI framework.
564 	 */
565 	ASSERT(zv->zv_open_count[otyp] != 0);
566 	ASSERT(zv->zv_total_opens != 0);
567 
568 	/*
569 	 * You may get multiple opens, but only one close.
570 	 */
571 	zv->zv_open_count[otyp]--;
572 	zv->zv_total_opens--;
573 
574 	mutex_exit(&zvol_state_lock);
575 
576 	return (0);
577 }
578 
579 int
580 zvol_strategy(buf_t *bp)
581 {
582 	zvol_state_t *zv = ddi_get_soft_state(zvol_state, getminor(bp->b_edev));
583 	uint64_t off, volsize;
584 	size_t size, resid;
585 	char *addr;
586 	int error = 0;
587 
588 	if (zv == NULL) {
589 		bioerror(bp, ENXIO);
590 		biodone(bp);
591 		return (0);
592 	}
593 
594 	if (getminor(bp->b_edev) == 0) {
595 		bioerror(bp, EINVAL);
596 		biodone(bp);
597 		return (0);
598 	}
599 
600 	if (zv->zv_readonly && !(bp->b_flags & B_READ)) {
601 		bioerror(bp, EROFS);
602 		biodone(bp);
603 		return (0);
604 	}
605 
606 	off = ldbtob(bp->b_blkno);
607 	volsize = zv->zv_volsize;
608 
609 	ASSERT(zv->zv_objset != NULL);
610 
611 	bp_mapin(bp);
612 	addr = bp->b_un.b_addr;
613 	resid = bp->b_bcount;
614 
615 	while (resid != 0 && off < volsize) {
616 
617 		size = MIN(resid, 1UL << 20);	/* cap at 1MB per tx */
618 
619 		if (size > volsize - off)	/* don't write past the end */
620 			size = volsize - off;
621 
622 		if (bp->b_flags & B_READ) {
623 			error = dmu_read_canfail(zv->zv_objset, ZVOL_OBJ,
624 			    off, size, addr);
625 		} else {
626 			dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
627 			dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
628 			error = dmu_tx_assign(tx, TXG_WAIT);
629 			if (error) {
630 				dmu_tx_abort(tx);
631 			} else {
632 				dmu_write(zv->zv_objset, ZVOL_OBJ,
633 				    off, size, addr, tx);
634 				dmu_tx_commit(tx);
635 			}
636 		}
637 		if (error)
638 			break;
639 		off += size;
640 		addr += size;
641 		resid -= size;
642 	}
643 
644 	if ((bp->b_resid = resid) == bp->b_bcount)
645 		bioerror(bp, off > volsize ? EINVAL : error);
646 
647 	biodone(bp);
648 	return (0);
649 }
650 
651 /*ARGSUSED*/
652 int
653 zvol_read(dev_t dev, uio_t *uiop, cred_t *cr)
654 {
655 	return (physio(zvol_strategy, NULL, dev, B_READ, minphys, uiop));
656 }
657 
658 /*ARGSUSED*/
659 int
660 zvol_write(dev_t dev, uio_t *uiop, cred_t *cr)
661 {
662 	return (physio(zvol_strategy, NULL, dev, B_WRITE, minphys, uiop));
663 }
664 
665 /*ARGSUSED*/
666 int
667 zvol_aread(dev_t dev, struct aio_req *aio, cred_t *cr)
668 {
669 	return (aphysio(zvol_strategy, anocancel, dev, B_READ, minphys, aio));
670 }
671 
672 /*ARGSUSED*/
673 int
674 zvol_awrite(dev_t dev, struct aio_req *aio, cred_t *cr)
675 {
676 	return (aphysio(zvol_strategy, anocancel, dev, B_WRITE, minphys, aio));
677 }
678 
679 /*
680  * Dirtbag ioctls to support mkfs(1M) for UFS filesystems.  See dkio(7I).
681  */
682 /*ARGSUSED*/
683 int
684 zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
685 {
686 	zvol_state_t *zv;
687 	struct dk_cinfo dkc;
688 	struct dk_minfo dkm;
689 	dk_efi_t efi;
690 	efi_gpt_t gpt;
691 	efi_gpe_t gpe;
692 	struct uuid uuid = EFI_RESERVED;
693 	uint32_t crc;
694 	int error = 0;
695 
696 	mutex_enter(&zvol_state_lock);
697 
698 	zv = ddi_get_soft_state(zvol_state, getminor(dev));
699 
700 	if (zv == NULL) {
701 		mutex_exit(&zvol_state_lock);
702 		return (ENXIO);
703 	}
704 
705 	switch (cmd) {
706 
707 	case DKIOCINFO:
708 		bzero(&dkc, sizeof (dkc));
709 		(void) strcpy(dkc.dki_cname, "zvol");
710 		(void) strcpy(dkc.dki_dname, "zvol");
711 		dkc.dki_ctype = DKC_UNKNOWN;
712 		dkc.dki_maxtransfer = 1 << 15;
713 		mutex_exit(&zvol_state_lock);
714 		if (ddi_copyout(&dkc, (void *)arg, sizeof (dkc), flag))
715 			error = EFAULT;
716 		return (error);
717 
718 	case DKIOCGMEDIAINFO:
719 		bzero(&dkm, sizeof (dkm));
720 		dkm.dki_lbsize = 1U << zv->zv_min_bs;
721 		dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
722 		dkm.dki_media_type = DK_UNKNOWN;
723 		mutex_exit(&zvol_state_lock);
724 		if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
725 			error = EFAULT;
726 		return (error);
727 
728 	case DKIOCGETEFI:
729 		if (ddi_copyin((void *)arg, &efi, sizeof (dk_efi_t), flag)) {
730 			mutex_exit(&zvol_state_lock);
731 			return (EFAULT);
732 		}
733 
734 		bzero(&gpt, sizeof (gpt));
735 		bzero(&gpe, sizeof (gpe));
736 
737 		efi.dki_data = (void *)(uintptr_t)efi.dki_data_64;
738 
739 		if (efi.dki_length < sizeof (gpt) + sizeof (gpe)) {
740 			mutex_exit(&zvol_state_lock);
741 			return (EINVAL);
742 		}
743 
744 		efi.dki_length = sizeof (gpt) + sizeof (gpe);
745 
746 		gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
747 		gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
748 		gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt));
749 		gpt.efi_gpt_FirstUsableLBA = LE_64(0ULL);
750 		gpt.efi_gpt_LastUsableLBA =
751 		    LE_64((zv->zv_volsize >> zv->zv_min_bs) - 1);
752 		gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
753 		gpt.efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (gpe));
754 
755 		UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
756 		gpe.efi_gpe_StartingLBA = gpt.efi_gpt_FirstUsableLBA;
757 		gpe.efi_gpe_EndingLBA = gpt.efi_gpt_LastUsableLBA;
758 
759 		CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
760 		gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
761 
762 		CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table);
763 		gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
764 
765 		mutex_exit(&zvol_state_lock);
766 		if (ddi_copyout(&gpt, efi.dki_data, sizeof (gpt), flag) ||
767 		    ddi_copyout(&gpe, efi.dki_data + 1, sizeof (gpe), flag))
768 			error = EFAULT;
769 		return (error);
770 
771 	default:
772 		error = ENOTSUP;
773 		break;
774 
775 	}
776 	mutex_exit(&zvol_state_lock);
777 	return (error);
778 }
779 
780 int
781 zvol_busy(void)
782 {
783 	return (zvol_minors != 0);
784 }
785 
786 void
787 zvol_init(void)
788 {
789 	VERIFY(ddi_soft_state_init(&zvol_state, sizeof (zvol_state_t), 1) == 0);
790 	mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
791 }
792 
793 void
794 zvol_fini(void)
795 {
796 	mutex_destroy(&zvol_state_lock);
797 	ddi_soft_state_fini(&zvol_state);
798 }
799