xref: /titanic_44/usr/src/uts/common/fs/zfs/zvol.c (revision 85b65b39e9a6fea849facdcfc7d06f5ece340e36)
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 2007 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/dsl_prop.h>
57 #include <sys/dkio.h>
58 #include <sys/efi_partition.h>
59 #include <sys/byteorder.h>
60 #include <sys/pathname.h>
61 #include <sys/ddi.h>
62 #include <sys/sunddi.h>
63 #include <sys/crc32.h>
64 #include <sys/dirent.h>
65 #include <sys/policy.h>
66 #include <sys/fs/zfs.h>
67 #include <sys/zfs_ioctl.h>
68 #include <sys/mkdev.h>
69 #include <sys/zil.h>
70 #include <sys/refcount.h>
71 #include <sys/zfs_znode.h>
72 #include <sys/zfs_rlock.h>
73 
74 #include "zfs_namecheck.h"
75 
76 #define	ZVOL_OBJ		1ULL
77 #define	ZVOL_ZAP_OBJ		2ULL
78 
79 static void *zvol_state;
80 
81 /*
82  * This lock protects the zvol_state structure from being modified
83  * while it's being used, e.g. an open that comes in before a create
84  * finishes.  It also protects temporary opens of the dataset so that,
85  * e.g., an open doesn't get a spurious EBUSY.
86  */
87 static kmutex_t zvol_state_lock;
88 static uint32_t zvol_minors;
89 
90 /*
91  * The in-core state of each volume.
92  */
93 typedef struct zvol_state {
94 	char		zv_name[MAXPATHLEN]; /* pool/dd name */
95 	uint64_t	zv_volsize;	/* amount of space we advertise */
96 	uint64_t	zv_volblocksize; /* volume block size */
97 	minor_t		zv_minor;	/* minor number */
98 	uint8_t		zv_min_bs;	/* minimum addressable block shift */
99 	uint8_t		zv_readonly;	/* hard readonly; like write-protect */
100 	objset_t	*zv_objset;	/* objset handle */
101 	uint32_t	zv_mode;	/* DS_MODE_* flags at open time */
102 	uint32_t	zv_open_count[OTYPCNT];	/* open counts */
103 	uint32_t	zv_total_opens;	/* total open count */
104 	zilog_t		*zv_zilog;	/* ZIL handle */
105 	uint64_t	zv_txg_assign;	/* txg to assign during ZIL replay */
106 	znode_t		zv_znode;	/* for range locking */
107 } zvol_state_t;
108 
109 /*
110  * zvol maximum transfer in one DMU tx.
111  */
112 int zvol_maxphys = DMU_MAX_ACCESS/2;
113 
114 static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio);
115 
116 static void
117 zvol_size_changed(zvol_state_t *zv, dev_t dev)
118 {
119 	dev = makedevice(getmajor(dev), zv->zv_minor);
120 
121 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
122 	    "Size", zv->zv_volsize) == DDI_SUCCESS);
123 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
124 	    "Nblocks", lbtodb(zv->zv_volsize)) == DDI_SUCCESS);
125 }
126 
127 int
128 zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
129 {
130 	if (volsize == 0)
131 		return (EINVAL);
132 
133 	if (volsize % blocksize != 0)
134 		return (EINVAL);
135 
136 #ifdef _ILP32
137 	if (volsize - 1 > SPEC_MAXOFFSET_T)
138 		return (EOVERFLOW);
139 #endif
140 	return (0);
141 }
142 
143 int
144 zvol_check_volblocksize(uint64_t volblocksize)
145 {
146 	if (volblocksize < SPA_MINBLOCKSIZE ||
147 	    volblocksize > SPA_MAXBLOCKSIZE ||
148 	    !ISP2(volblocksize))
149 		return (EDOM);
150 
151 	return (0);
152 }
153 
154 static void
155 zvol_readonly_changed_cb(void *arg, uint64_t newval)
156 {
157 	zvol_state_t *zv = arg;
158 
159 	zv->zv_readonly = (uint8_t)newval;
160 }
161 
162 int
163 zvol_get_stats(objset_t *os, nvlist_t *nv)
164 {
165 	int error;
166 	dmu_object_info_t doi;
167 	uint64_t val;
168 
169 
170 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
171 	if (error)
172 		return (error);
173 
174 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
175 
176 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
177 
178 	if (error == 0) {
179 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
180 		    doi.doi_data_block_size);
181 	}
182 
183 	return (error);
184 }
185 
186 /*
187  * Find a free minor number.
188  */
189 static minor_t
190 zvol_minor_alloc(void)
191 {
192 	minor_t minor;
193 
194 	ASSERT(MUTEX_HELD(&zvol_state_lock));
195 
196 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++)
197 		if (ddi_get_soft_state(zvol_state, minor) == NULL)
198 			return (minor);
199 
200 	return (0);
201 }
202 
203 static zvol_state_t *
204 zvol_minor_lookup(const char *name)
205 {
206 	minor_t minor;
207 	zvol_state_t *zv;
208 
209 	ASSERT(MUTEX_HELD(&zvol_state_lock));
210 
211 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) {
212 		zv = ddi_get_soft_state(zvol_state, minor);
213 		if (zv == NULL)
214 			continue;
215 		if (strcmp(zv->zv_name, name) == 0)
216 			break;
217 	}
218 
219 	return (zv);
220 }
221 
222 void
223 zvol_create_cb(objset_t *os, void *arg, dmu_tx_t *tx)
224 {
225 	zfs_create_data_t *zc = arg;
226 	int error;
227 	uint64_t volblocksize, volsize;
228 
229 	VERIFY(nvlist_lookup_uint64(zc->zc_props,
230 	    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
231 	if (nvlist_lookup_uint64(zc->zc_props,
232 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
233 		volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
234 
235 	/*
236 	 * These properites must be removed from the list so the generic
237 	 * property setting step won't apply to them.
238 	 */
239 	VERIFY(nvlist_remove_all(zc->zc_props,
240 	    zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
241 	(void) nvlist_remove_all(zc->zc_props,
242 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
243 
244 	error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
245 	    DMU_OT_NONE, 0, tx);
246 	ASSERT(error == 0);
247 
248 	error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
249 	    DMU_OT_NONE, 0, tx);
250 	ASSERT(error == 0);
251 
252 	error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
253 	ASSERT(error == 0);
254 }
255 
256 /*
257  * Replay a TX_WRITE ZIL transaction that didn't get committed
258  * after a system failure
259  */
260 static int
261 zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
262 {
263 	objset_t *os = zv->zv_objset;
264 	char *data = (char *)(lr + 1);	/* data follows lr_write_t */
265 	uint64_t off = lr->lr_offset;
266 	uint64_t len = lr->lr_length;
267 	dmu_tx_t *tx;
268 	int error;
269 
270 	if (byteswap)
271 		byteswap_uint64_array(lr, sizeof (*lr));
272 
273 	tx = dmu_tx_create(os);
274 	dmu_tx_hold_write(tx, ZVOL_OBJ, off, len);
275 	error = dmu_tx_assign(tx, zv->zv_txg_assign);
276 	if (error) {
277 		dmu_tx_abort(tx);
278 	} else {
279 		dmu_write(os, ZVOL_OBJ, off, len, data, tx);
280 		dmu_tx_commit(tx);
281 	}
282 
283 	return (error);
284 }
285 
286 /* ARGSUSED */
287 static int
288 zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
289 {
290 	return (ENOTSUP);
291 }
292 
293 /*
294  * Callback vectors for replaying records.
295  * Only TX_WRITE is needed for zvol.
296  */
297 zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = {
298 	zvol_replay_err,	/* 0 no such transaction type */
299 	zvol_replay_err,	/* TX_CREATE */
300 	zvol_replay_err,	/* TX_MKDIR */
301 	zvol_replay_err,	/* TX_MKXATTR */
302 	zvol_replay_err,	/* TX_SYMLINK */
303 	zvol_replay_err,	/* TX_REMOVE */
304 	zvol_replay_err,	/* TX_RMDIR */
305 	zvol_replay_err,	/* TX_LINK */
306 	zvol_replay_err,	/* TX_RENAME */
307 	zvol_replay_write,	/* TX_WRITE */
308 	zvol_replay_err,	/* TX_TRUNCATE */
309 	zvol_replay_err,	/* TX_SETATTR */
310 	zvol_replay_err,	/* TX_ACL */
311 };
312 
313 /*
314  * Create a minor node for the specified volume.
315  */
316 int
317 zvol_create_minor(const char *name, dev_t dev)
318 {
319 	zvol_state_t *zv;
320 	objset_t *os;
321 	dmu_object_info_t doi;
322 	uint64_t volsize;
323 	minor_t minor = 0;
324 	struct pathname linkpath;
325 	int ds_mode = DS_MODE_PRIMARY;
326 	vnode_t *vp = NULL;
327 	char *devpath;
328 	size_t devpathlen = strlen(ZVOL_FULL_DEV_DIR) + 1 + strlen(name) + 1;
329 	char chrbuf[30], blkbuf[30];
330 	int error;
331 
332 	mutex_enter(&zvol_state_lock);
333 
334 	if ((zv = zvol_minor_lookup(name)) != NULL) {
335 		mutex_exit(&zvol_state_lock);
336 		return (EEXIST);
337 	}
338 
339 	if (strchr(name, '@') != 0)
340 		ds_mode |= DS_MODE_READONLY;
341 
342 	error = dmu_objset_open(name, DMU_OST_ZVOL, ds_mode, &os);
343 
344 	if (error) {
345 		mutex_exit(&zvol_state_lock);
346 		return (error);
347 	}
348 
349 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
350 
351 	if (error) {
352 		dmu_objset_close(os);
353 		mutex_exit(&zvol_state_lock);
354 		return (error);
355 	}
356 
357 	/*
358 	 * If there's an existing /dev/zvol symlink, try to use the
359 	 * same minor number we used last time.
360 	 */
361 	devpath = kmem_alloc(devpathlen, KM_SLEEP);
362 
363 	(void) sprintf(devpath, "%s/%s", ZVOL_FULL_DEV_DIR, name);
364 
365 	error = lookupname(devpath, UIO_SYSSPACE, NO_FOLLOW, NULL, &vp);
366 
367 	kmem_free(devpath, devpathlen);
368 
369 	if (error == 0 && vp->v_type != VLNK)
370 		error = EINVAL;
371 
372 	if (error == 0) {
373 		pn_alloc(&linkpath);
374 		error = pn_getsymlink(vp, &linkpath, kcred);
375 		if (error == 0) {
376 			char *ms = strstr(linkpath.pn_path, ZVOL_PSEUDO_DEV);
377 			if (ms != NULL) {
378 				ms += strlen(ZVOL_PSEUDO_DEV);
379 				minor = stoi(&ms);
380 			}
381 		}
382 		pn_free(&linkpath);
383 	}
384 
385 	if (vp != NULL)
386 		VN_RELE(vp);
387 
388 	/*
389 	 * If we found a minor but it's already in use, we must pick a new one.
390 	 */
391 	if (minor != 0 && ddi_get_soft_state(zvol_state, minor) != NULL)
392 		minor = 0;
393 
394 	if (minor == 0)
395 		minor = zvol_minor_alloc();
396 
397 	if (minor == 0) {
398 		dmu_objset_close(os);
399 		mutex_exit(&zvol_state_lock);
400 		return (ENXIO);
401 	}
402 
403 	if (ddi_soft_state_zalloc(zvol_state, minor) != DDI_SUCCESS) {
404 		dmu_objset_close(os);
405 		mutex_exit(&zvol_state_lock);
406 		return (EAGAIN);
407 	}
408 
409 	(void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME,
410 	    (char *)name);
411 
412 	(void) sprintf(chrbuf, "%uc,raw", minor);
413 
414 	if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR,
415 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
416 		ddi_soft_state_free(zvol_state, minor);
417 		dmu_objset_close(os);
418 		mutex_exit(&zvol_state_lock);
419 		return (EAGAIN);
420 	}
421 
422 	(void) sprintf(blkbuf, "%uc", minor);
423 
424 	if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK,
425 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
426 		ddi_remove_minor_node(zfs_dip, chrbuf);
427 		ddi_soft_state_free(zvol_state, minor);
428 		dmu_objset_close(os);
429 		mutex_exit(&zvol_state_lock);
430 		return (EAGAIN);
431 	}
432 
433 	zv = ddi_get_soft_state(zvol_state, minor);
434 
435 	(void) strcpy(zv->zv_name, name);
436 	zv->zv_min_bs = DEV_BSHIFT;
437 	zv->zv_minor = minor;
438 	zv->zv_volsize = volsize;
439 	zv->zv_objset = os;
440 	zv->zv_mode = ds_mode;
441 	zv->zv_zilog = zil_open(os, zvol_get_data);
442 	mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
443 	avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
444 	    sizeof (rl_t), offsetof(rl_t, r_node));
445 
446 
447 	/* get and cache the blocksize */
448 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
449 	ASSERT(error == 0);
450 	zv->zv_volblocksize = doi.doi_data_block_size;
451 
452 	zil_replay(os, zv, &zv->zv_txg_assign, zvol_replay_vector);
453 
454 	zvol_size_changed(zv, dev);
455 
456 	/* XXX this should handle the possible i/o error */
457 	VERIFY(dsl_prop_register(dmu_objset_ds(zv->zv_objset),
458 	    "readonly", zvol_readonly_changed_cb, zv) == 0);
459 
460 	zvol_minors++;
461 
462 	mutex_exit(&zvol_state_lock);
463 
464 	return (0);
465 }
466 
467 /*
468  * Remove minor node for the specified volume.
469  */
470 int
471 zvol_remove_minor(const char *name)
472 {
473 	zvol_state_t *zv;
474 	char namebuf[30];
475 
476 	mutex_enter(&zvol_state_lock);
477 
478 	if ((zv = zvol_minor_lookup(name)) == NULL) {
479 		mutex_exit(&zvol_state_lock);
480 		return (ENXIO);
481 	}
482 
483 	if (zv->zv_total_opens != 0) {
484 		mutex_exit(&zvol_state_lock);
485 		return (EBUSY);
486 	}
487 
488 	(void) sprintf(namebuf, "%uc,raw", zv->zv_minor);
489 	ddi_remove_minor_node(zfs_dip, namebuf);
490 
491 	(void) sprintf(namebuf, "%uc", zv->zv_minor);
492 	ddi_remove_minor_node(zfs_dip, namebuf);
493 
494 	VERIFY(dsl_prop_unregister(dmu_objset_ds(zv->zv_objset),
495 	    "readonly", zvol_readonly_changed_cb, zv) == 0);
496 
497 	zil_close(zv->zv_zilog);
498 	zv->zv_zilog = NULL;
499 	dmu_objset_close(zv->zv_objset);
500 	zv->zv_objset = NULL;
501 	avl_destroy(&zv->zv_znode.z_range_avl);
502 	mutex_destroy(&zv->zv_znode.z_range_lock);
503 
504 	ddi_soft_state_free(zvol_state, zv->zv_minor);
505 
506 	zvol_minors--;
507 
508 	mutex_exit(&zvol_state_lock);
509 
510 	return (0);
511 }
512 
513 int
514 zvol_set_volsize(const char *name, dev_t dev, uint64_t volsize)
515 {
516 	zvol_state_t *zv;
517 	dmu_tx_t *tx;
518 	int error;
519 	dmu_object_info_t doi;
520 
521 	mutex_enter(&zvol_state_lock);
522 
523 	if ((zv = zvol_minor_lookup(name)) == NULL) {
524 		mutex_exit(&zvol_state_lock);
525 		return (ENXIO);
526 	}
527 
528 	if ((error = dmu_object_info(zv->zv_objset, ZVOL_OBJ, &doi)) != 0 ||
529 	    (error = zvol_check_volsize(volsize,
530 	    doi.doi_data_block_size)) != 0) {
531 		mutex_exit(&zvol_state_lock);
532 		return (error);
533 	}
534 
535 	if (zv->zv_readonly || (zv->zv_mode & DS_MODE_READONLY)) {
536 		mutex_exit(&zvol_state_lock);
537 		return (EROFS);
538 	}
539 
540 	tx = dmu_tx_create(zv->zv_objset);
541 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
542 	dmu_tx_hold_free(tx, ZVOL_OBJ, volsize, DMU_OBJECT_END);
543 	error = dmu_tx_assign(tx, TXG_WAIT);
544 	if (error) {
545 		dmu_tx_abort(tx);
546 		mutex_exit(&zvol_state_lock);
547 		return (error);
548 	}
549 
550 	error = zap_update(zv->zv_objset, ZVOL_ZAP_OBJ, "size", 8, 1,
551 	    &volsize, tx);
552 	if (error == 0) {
553 		error = dmu_free_range(zv->zv_objset, ZVOL_OBJ, volsize,
554 		    DMU_OBJECT_END, tx);
555 	}
556 
557 	dmu_tx_commit(tx);
558 
559 	if (error == 0) {
560 		zv->zv_volsize = volsize;
561 		zvol_size_changed(zv, dev);
562 	}
563 
564 	mutex_exit(&zvol_state_lock);
565 
566 	return (error);
567 }
568 
569 int
570 zvol_set_volblocksize(const char *name, uint64_t volblocksize)
571 {
572 	zvol_state_t *zv;
573 	dmu_tx_t *tx;
574 	int error;
575 
576 	mutex_enter(&zvol_state_lock);
577 
578 	if ((zv = zvol_minor_lookup(name)) == NULL) {
579 		mutex_exit(&zvol_state_lock);
580 		return (ENXIO);
581 	}
582 
583 	if (zv->zv_readonly || (zv->zv_mode & DS_MODE_READONLY)) {
584 		mutex_exit(&zvol_state_lock);
585 		return (EROFS);
586 	}
587 
588 	tx = dmu_tx_create(zv->zv_objset);
589 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
590 	error = dmu_tx_assign(tx, TXG_WAIT);
591 	if (error) {
592 		dmu_tx_abort(tx);
593 	} else {
594 		error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ,
595 		    volblocksize, 0, tx);
596 		if (error == ENOTSUP)
597 			error = EBUSY;
598 		dmu_tx_commit(tx);
599 	}
600 
601 	mutex_exit(&zvol_state_lock);
602 
603 	return (error);
604 }
605 
606 /*ARGSUSED*/
607 int
608 zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr)
609 {
610 	minor_t minor = getminor(*devp);
611 	zvol_state_t *zv;
612 
613 	if (minor == 0)			/* This is the control device */
614 		return (0);
615 
616 	mutex_enter(&zvol_state_lock);
617 
618 	zv = ddi_get_soft_state(zvol_state, minor);
619 	if (zv == NULL) {
620 		mutex_exit(&zvol_state_lock);
621 		return (ENXIO);
622 	}
623 
624 	ASSERT(zv->zv_objset != NULL);
625 
626 	if ((flag & FWRITE) &&
627 	    (zv->zv_readonly || (zv->zv_mode & DS_MODE_READONLY))) {
628 		mutex_exit(&zvol_state_lock);
629 		return (EROFS);
630 	}
631 
632 	if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) {
633 		zv->zv_open_count[otyp]++;
634 		zv->zv_total_opens++;
635 	}
636 
637 	mutex_exit(&zvol_state_lock);
638 
639 	return (0);
640 }
641 
642 /*ARGSUSED*/
643 int
644 zvol_close(dev_t dev, int flag, int otyp, cred_t *cr)
645 {
646 	minor_t minor = getminor(dev);
647 	zvol_state_t *zv;
648 
649 	if (minor == 0)		/* This is the control device */
650 		return (0);
651 
652 	mutex_enter(&zvol_state_lock);
653 
654 	zv = ddi_get_soft_state(zvol_state, minor);
655 	if (zv == NULL) {
656 		mutex_exit(&zvol_state_lock);
657 		return (ENXIO);
658 	}
659 
660 	/*
661 	 * The next statement is a workaround for the following DDI bug:
662 	 * 6343604 specfs race: multiple "last-close" of the same device
663 	 */
664 	if (zv->zv_total_opens == 0) {
665 		mutex_exit(&zvol_state_lock);
666 		return (0);
667 	}
668 
669 	/*
670 	 * If the open count is zero, this is a spurious close.
671 	 * That indicates a bug in the kernel / DDI framework.
672 	 */
673 	ASSERT(zv->zv_open_count[otyp] != 0);
674 	ASSERT(zv->zv_total_opens != 0);
675 
676 	/*
677 	 * You may get multiple opens, but only one close.
678 	 */
679 	zv->zv_open_count[otyp]--;
680 	zv->zv_total_opens--;
681 
682 	mutex_exit(&zvol_state_lock);
683 
684 	return (0);
685 }
686 
687 static void
688 zvol_get_done(dmu_buf_t *db, void *vzgd)
689 {
690 	zgd_t *zgd = (zgd_t *)vzgd;
691 	rl_t *rl = zgd->zgd_rl;
692 
693 	dmu_buf_rele(db, vzgd);
694 	zfs_range_unlock(rl);
695 	zil_add_vdev(zgd->zgd_zilog, DVA_GET_VDEV(BP_IDENTITY(zgd->zgd_bp)));
696 	kmem_free(zgd, sizeof (zgd_t));
697 }
698 
699 /*
700  * Get data to generate a TX_WRITE intent log record.
701  */
702 static int
703 zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
704 {
705 	zvol_state_t *zv = arg;
706 	objset_t *os = zv->zv_objset;
707 	dmu_buf_t *db;
708 	rl_t *rl;
709 	zgd_t *zgd;
710 	uint64_t boff; 			/* block starting offset */
711 	int dlen = lr->lr_length;	/* length of user data */
712 	int error;
713 
714 	ASSERT(zio);
715 	ASSERT(dlen != 0);
716 
717 	/*
718 	 * Write records come in two flavors: immediate and indirect.
719 	 * For small writes it's cheaper to store the data with the
720 	 * log record (immediate); for large writes it's cheaper to
721 	 * sync the data and get a pointer to it (indirect) so that
722 	 * we don't have to write the data twice.
723 	 */
724 	if (buf != NULL) /* immediate write */
725 		return (dmu_read(os, ZVOL_OBJ, lr->lr_offset, dlen, buf));
726 
727 	zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP);
728 	zgd->zgd_zilog = zv->zv_zilog;
729 	zgd->zgd_bp = &lr->lr_blkptr;
730 
731 	/*
732 	 * Lock the range of the block to ensure that when the data is
733 	 * written out and it's checksum is being calculated that no other
734 	 * thread can change the block.
735 	 */
736 	boff = P2ALIGN_TYPED(lr->lr_offset, zv->zv_volblocksize, uint64_t);
737 	rl = zfs_range_lock(&zv->zv_znode, boff, zv->zv_volblocksize,
738 	    RL_READER);
739 	zgd->zgd_rl = rl;
740 
741 	VERIFY(0 == dmu_buf_hold(os, ZVOL_OBJ, lr->lr_offset, zgd, &db));
742 	error = dmu_sync(zio, db, &lr->lr_blkptr,
743 	    lr->lr_common.lrc_txg, zvol_get_done, zgd);
744 	if (error == 0)
745 		zil_add_vdev(zv->zv_zilog,
746 		    DVA_GET_VDEV(BP_IDENTITY(&lr->lr_blkptr)));
747 	/*
748 	 * If we get EINPROGRESS, then we need to wait for a
749 	 * write IO initiated by dmu_sync() to complete before
750 	 * we can release this dbuf.  We will finish everything
751 	 * up in the zvol_get_done() callback.
752 	 */
753 	if (error == EINPROGRESS)
754 		return (0);
755 	dmu_buf_rele(db, zgd);
756 	zfs_range_unlock(rl);
757 	kmem_free(zgd, sizeof (zgd_t));
758 	return (error);
759 }
760 
761 /*
762  * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
763  *
764  * We store data in the log buffers if it's small enough.
765  * Otherwise we will later flush the data out via dmu_sync().
766  */
767 ssize_t zvol_immediate_write_sz = 32768;
768 
769 static void
770 zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t len)
771 {
772 	uint32_t blocksize = zv->zv_volblocksize;
773 	lr_write_t *lr;
774 
775 	while (len) {
776 		ssize_t nbytes = MIN(len, blocksize - P2PHASE(off, blocksize));
777 		itx_t *itx = zil_itx_create(TX_WRITE, sizeof (*lr));
778 
779 		itx->itx_wr_state =
780 		    len > zvol_immediate_write_sz ?  WR_INDIRECT : WR_NEED_COPY;
781 		itx->itx_private = zv;
782 		lr = (lr_write_t *)&itx->itx_lr;
783 		lr->lr_foid = ZVOL_OBJ;
784 		lr->lr_offset = off;
785 		lr->lr_length = nbytes;
786 		lr->lr_blkoff = off - P2ALIGN_TYPED(off, blocksize, uint64_t);
787 		BP_ZERO(&lr->lr_blkptr);
788 
789 		(void) zil_itx_assign(zv->zv_zilog, itx, tx);
790 		len -= nbytes;
791 		off += nbytes;
792 	}
793 }
794 
795 int
796 zvol_strategy(buf_t *bp)
797 {
798 	zvol_state_t *zv = ddi_get_soft_state(zvol_state, getminor(bp->b_edev));
799 	uint64_t off, volsize;
800 	size_t size, resid;
801 	char *addr;
802 	objset_t *os;
803 	rl_t *rl;
804 	int error = 0;
805 	int sync;
806 	boolean_t reading;
807 
808 	if (zv == NULL) {
809 		bioerror(bp, ENXIO);
810 		biodone(bp);
811 		return (0);
812 	}
813 
814 	if (getminor(bp->b_edev) == 0) {
815 		bioerror(bp, EINVAL);
816 		biodone(bp);
817 		return (0);
818 	}
819 
820 	if ((zv->zv_readonly || (zv->zv_mode & DS_MODE_READONLY)) &&
821 	    !(bp->b_flags & B_READ)) {
822 		bioerror(bp, EROFS);
823 		biodone(bp);
824 		return (0);
825 	}
826 
827 	off = ldbtob(bp->b_blkno);
828 	volsize = zv->zv_volsize;
829 
830 	os = zv->zv_objset;
831 	ASSERT(os != NULL);
832 	sync = !(bp->b_flags & B_ASYNC) && !(zil_disable);
833 
834 	bp_mapin(bp);
835 	addr = bp->b_un.b_addr;
836 	resid = bp->b_bcount;
837 
838 	/*
839 	 * There must be no buffer changes when doing a dmu_sync() because
840 	 * we can't change the data whilst calculating the checksum.
841 	 * A better approach than a per zvol rwlock would be to lock ranges.
842 	 */
843 	reading = bp->b_flags & B_READ;
844 	rl = zfs_range_lock(&zv->zv_znode, off, resid,
845 	    reading ? RL_READER : RL_WRITER);
846 
847 	while (resid != 0 && off < volsize) {
848 
849 		size = MIN(resid, zvol_maxphys); /* zvol_maxphys per tx */
850 
851 		if (size > volsize - off)	/* don't write past the end */
852 			size = volsize - off;
853 
854 		if (reading) {
855 			error = dmu_read(os, ZVOL_OBJ, off, size, addr);
856 		} else {
857 			dmu_tx_t *tx = dmu_tx_create(os);
858 			dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
859 			error = dmu_tx_assign(tx, TXG_WAIT);
860 			if (error) {
861 				dmu_tx_abort(tx);
862 			} else {
863 				dmu_write(os, ZVOL_OBJ, off, size, addr, tx);
864 				zvol_log_write(zv, tx, off, size);
865 				dmu_tx_commit(tx);
866 			}
867 		}
868 		if (error)
869 			break;
870 		off += size;
871 		addr += size;
872 		resid -= size;
873 	}
874 	zfs_range_unlock(rl);
875 
876 	if ((bp->b_resid = resid) == bp->b_bcount)
877 		bioerror(bp, off > volsize ? EINVAL : error);
878 
879 	if (sync)
880 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
881 
882 	biodone(bp);
883 
884 	return (0);
885 }
886 
887 /*
888  * Set the buffer count to the zvol maximum transfer.
889  * Using our own routine instead of the default minphys()
890  * means that for larger writes we write bigger buffers on X86
891  * (128K instead of 56K) and flush the disk write cache less often
892  * (every zvol_maxphys - currently 1MB) instead of minphys (currently
893  * 56K on X86 and 128K on sparc).
894  */
895 void
896 zvol_minphys(struct buf *bp)
897 {
898 	if (bp->b_bcount > zvol_maxphys)
899 		bp->b_bcount = zvol_maxphys;
900 }
901 
902 /*ARGSUSED*/
903 int
904 zvol_read(dev_t dev, uio_t *uio, cred_t *cr)
905 {
906 	minor_t minor = getminor(dev);
907 	zvol_state_t *zv;
908 	rl_t *rl;
909 	int error = 0;
910 
911 	if (minor == 0)			/* This is the control device */
912 		return (ENXIO);
913 
914 	zv = ddi_get_soft_state(zvol_state, minor);
915 	if (zv == NULL)
916 		return (ENXIO);
917 
918 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
919 	    RL_READER);
920 	while (uio->uio_resid > 0) {
921 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
922 
923 		error =  dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes);
924 		if (error)
925 			break;
926 	}
927 	zfs_range_unlock(rl);
928 	return (error);
929 }
930 
931 /*ARGSUSED*/
932 int
933 zvol_write(dev_t dev, uio_t *uio, cred_t *cr)
934 {
935 	minor_t minor = getminor(dev);
936 	zvol_state_t *zv;
937 	rl_t *rl;
938 	int error = 0;
939 
940 	if (minor == 0)			/* This is the control device */
941 		return (ENXIO);
942 
943 	zv = ddi_get_soft_state(zvol_state, minor);
944 	if (zv == NULL)
945 		return (ENXIO);
946 
947 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
948 	    RL_WRITER);
949 	while (uio->uio_resid > 0) {
950 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
951 		uint64_t off = uio->uio_loffset;
952 
953 		dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
954 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
955 		error = dmu_tx_assign(tx, TXG_WAIT);
956 		if (error) {
957 			dmu_tx_abort(tx);
958 			break;
959 		}
960 		error = dmu_write_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes, tx);
961 		if (error == 0)
962 			zvol_log_write(zv, tx, off, bytes);
963 		dmu_tx_commit(tx);
964 
965 		if (error)
966 			break;
967 	}
968 	zfs_range_unlock(rl);
969 	return (error);
970 }
971 
972 /*
973  * Dirtbag ioctls to support mkfs(1M) for UFS filesystems.  See dkio(7I).
974  */
975 /*ARGSUSED*/
976 int
977 zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
978 {
979 	zvol_state_t *zv;
980 	struct dk_cinfo dki;
981 	struct dk_minfo dkm;
982 	dk_efi_t efi;
983 	struct dk_callback *dkc;
984 	struct uuid uuid = EFI_RESERVED;
985 	uint32_t crc;
986 	int error = 0;
987 
988 	mutex_enter(&zvol_state_lock);
989 
990 	zv = ddi_get_soft_state(zvol_state, getminor(dev));
991 
992 	if (zv == NULL) {
993 		mutex_exit(&zvol_state_lock);
994 		return (ENXIO);
995 	}
996 
997 	switch (cmd) {
998 
999 	case DKIOCINFO:
1000 		bzero(&dki, sizeof (dki));
1001 		(void) strcpy(dki.dki_cname, "zvol");
1002 		(void) strcpy(dki.dki_dname, "zvol");
1003 		dki.dki_ctype = DKC_UNKNOWN;
1004 		dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs);
1005 		mutex_exit(&zvol_state_lock);
1006 		if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag))
1007 			error = EFAULT;
1008 		return (error);
1009 
1010 	case DKIOCGMEDIAINFO:
1011 		bzero(&dkm, sizeof (dkm));
1012 		dkm.dki_lbsize = 1U << zv->zv_min_bs;
1013 		dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
1014 		dkm.dki_media_type = DK_UNKNOWN;
1015 		mutex_exit(&zvol_state_lock);
1016 		if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
1017 			error = EFAULT;
1018 		return (error);
1019 
1020 	case DKIOCGETEFI:
1021 		if (ddi_copyin((void *)arg, &efi, sizeof (dk_efi_t), flag)) {
1022 			mutex_exit(&zvol_state_lock);
1023 			return (EFAULT);
1024 		}
1025 		efi.dki_data = (void *)(uintptr_t)efi.dki_data_64;
1026 
1027 		/*
1028 		 * Some clients may attempt to request a PMBR for the
1029 		 * zvol.  Currently this interface will return ENOTTY to
1030 		 * such requests.  These requests could be supported by
1031 		 * adding a check for lba == 0 and consing up an appropriate
1032 		 * RMBR.
1033 		 */
1034 		if (efi.dki_lba == 1) {
1035 			efi_gpt_t gpt;
1036 			efi_gpe_t gpe;
1037 
1038 			bzero(&gpt, sizeof (gpt));
1039 			bzero(&gpe, sizeof (gpe));
1040 
1041 			if (efi.dki_length < sizeof (gpt)) {
1042 				mutex_exit(&zvol_state_lock);
1043 				return (EINVAL);
1044 			}
1045 
1046 			gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
1047 			gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
1048 			gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt));
1049 			gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL);
1050 			gpt.efi_gpt_LastUsableLBA =
1051 			    LE_64((zv->zv_volsize >> zv->zv_min_bs) - 1);
1052 			gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
1053 			gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL);
1054 			gpt.efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (gpe));
1055 
1056 			UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
1057 			gpe.efi_gpe_StartingLBA = gpt.efi_gpt_FirstUsableLBA;
1058 			gpe.efi_gpe_EndingLBA = gpt.efi_gpt_LastUsableLBA;
1059 
1060 			CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
1061 			gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
1062 
1063 			CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table);
1064 			gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
1065 
1066 			mutex_exit(&zvol_state_lock);
1067 			if (ddi_copyout(&gpt, efi.dki_data, sizeof (gpt), flag))
1068 				error = EFAULT;
1069 		} else if (efi.dki_lba == 2) {
1070 			efi_gpe_t gpe;
1071 
1072 			bzero(&gpe, sizeof (gpe));
1073 
1074 			if (efi.dki_length < sizeof (gpe)) {
1075 				mutex_exit(&zvol_state_lock);
1076 				return (EINVAL);
1077 			}
1078 
1079 			UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
1080 			gpe.efi_gpe_StartingLBA = LE_64(34ULL);
1081 			gpe.efi_gpe_EndingLBA =
1082 			    LE_64((zv->zv_volsize >> zv->zv_min_bs) - 1);
1083 
1084 			mutex_exit(&zvol_state_lock);
1085 			if (ddi_copyout(&gpe, efi.dki_data, sizeof (gpe), flag))
1086 				error = EFAULT;
1087 		} else {
1088 			mutex_exit(&zvol_state_lock);
1089 			error = EINVAL;
1090 		}
1091 		return (error);
1092 
1093 	case DKIOCFLUSHWRITECACHE:
1094 		dkc = (struct dk_callback *)arg;
1095 		zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
1096 		if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) {
1097 			(*dkc->dkc_callback)(dkc->dkc_cookie, error);
1098 			error = 0;
1099 		}
1100 		break;
1101 
1102 	case DKIOCGGEOM:
1103 	case DKIOCGVTOC:
1104 		/* commands using these (like prtvtoc) expect ENOTSUP */
1105 		error = ENOTSUP;
1106 		break;
1107 
1108 	default:
1109 		error = ENOTTY;
1110 		break;
1111 
1112 	}
1113 	mutex_exit(&zvol_state_lock);
1114 	return (error);
1115 }
1116 
1117 int
1118 zvol_busy(void)
1119 {
1120 	return (zvol_minors != 0);
1121 }
1122 
1123 void
1124 zvol_init(void)
1125 {
1126 	VERIFY(ddi_soft_state_init(&zvol_state, sizeof (zvol_state_t), 1) == 0);
1127 	mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
1128 }
1129 
1130 void
1131 zvol_fini(void)
1132 {
1133 	mutex_destroy(&zvol_state_lock);
1134 	ddi_soft_state_fini(&zvol_state);
1135 }
1136