xref: /titanic_50/usr/src/uts/common/fs/zfs/zvol.c (revision 47f258d370fe585ec2f4768e76dcfc71031dcbab)
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 #include <sys/zil.h>
72 
73 #include "zfs_namecheck.h"
74 
75 #define	ZVOL_OBJ		1ULL
76 #define	ZVOL_ZAP_OBJ		2ULL
77 
78 static void *zvol_state;
79 
80 /*
81  * This lock protects the zvol_state structure from being modified
82  * while it's being used, e.g. an open that comes in before a create
83  * finishes.  It also protects temporary opens of the dataset so that,
84  * e.g., an open doesn't get a spurious EBUSY.
85  */
86 static kmutex_t zvol_state_lock;
87 static uint32_t zvol_minors;
88 
89 /*
90  * The in-core state of each volume.
91  */
92 typedef struct zvol_state {
93 	char		zv_name[MAXPATHLEN]; /* pool/dd name */
94 	uint64_t	zv_volsize;	/* amount of space we advertise */
95 	minor_t		zv_minor;	/* minor number */
96 	uint8_t		zv_min_bs;	/* minimum addressable block shift */
97 	uint8_t		zv_readonly;	/* hard readonly; like write-protect */
98 	objset_t	*zv_objset;	/* objset handle */
99 	uint32_t	zv_mode;	/* DS_MODE_* flags at open time */
100 	uint32_t	zv_open_count[OTYPCNT];	/* open counts */
101 	uint32_t	zv_total_opens;	/* total open count */
102 	zilog_t		*zv_zilog;	/* ZIL handle */
103 	uint64_t	zv_txg_assign;	/* txg to assign during ZIL replay */
104 } zvol_state_t;
105 
106 static void
107 zvol_size_changed(zvol_state_t *zv, dev_t dev)
108 {
109 	dev = makedevice(getmajor(dev), zv->zv_minor);
110 
111 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
112 	    "Size", zv->zv_volsize) == DDI_SUCCESS);
113 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
114 	    "Nblocks", lbtodb(zv->zv_volsize)) == DDI_SUCCESS);
115 }
116 
117 int
118 zvol_check_volsize(zfs_cmd_t *zc, uint64_t blocksize)
119 {
120 	if (zc->zc_volsize == 0)
121 		return (EINVAL);
122 
123 	if (zc->zc_volsize % blocksize != 0)
124 		return (EINVAL);
125 
126 #ifdef _ILP32
127 	if (zc->zc_volsize - 1 > SPEC_MAXOFFSET_T)
128 		return (EOVERFLOW);
129 #endif
130 	return (0);
131 }
132 
133 int
134 zvol_check_volblocksize(zfs_cmd_t *zc)
135 {
136 	if (zc->zc_volblocksize < SPA_MINBLOCKSIZE ||
137 	    zc->zc_volblocksize > SPA_MAXBLOCKSIZE ||
138 	    !ISP2(zc->zc_volblocksize))
139 		return (EDOM);
140 
141 	return (0);
142 }
143 
144 static void
145 zvol_readonly_changed_cb(void *arg, uint64_t newval)
146 {
147 	zvol_state_t *zv = arg;
148 
149 	zv->zv_readonly = (uint8_t)newval;
150 }
151 
152 int
153 zvol_get_stats(zfs_cmd_t *zc, objset_t *os)
154 {
155 	int error;
156 	dmu_object_info_t doi;
157 
158 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &zc->zc_volsize);
159 
160 	if (error)
161 		return (error);
162 
163 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
164 
165 	if (error == 0)
166 		zc->zc_volblocksize = doi.doi_data_block_size;
167 
168 	error = dsl_prop_get_integer(zc->zc_name, "readonly",
169 			&zc->zc_zfs_stats.zs_readonly,
170 			zc->zc_zfs_stats.zs_readonly_setpoint);
171 
172 	return (error);
173 }
174 
175 /*
176  * Find a free minor number.
177  */
178 static minor_t
179 zvol_minor_alloc(void)
180 {
181 	minor_t minor;
182 
183 	ASSERT(MUTEX_HELD(&zvol_state_lock));
184 
185 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++)
186 		if (ddi_get_soft_state(zvol_state, minor) == NULL)
187 			return (minor);
188 
189 	return (0);
190 }
191 
192 static zvol_state_t *
193 zvol_minor_lookup(char *name)
194 {
195 	minor_t minor;
196 	zvol_state_t *zv;
197 
198 	ASSERT(MUTEX_HELD(&zvol_state_lock));
199 
200 	for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) {
201 		zv = ddi_get_soft_state(zvol_state, minor);
202 		if (zv == NULL)
203 			continue;
204 		if (strcmp(zv->zv_name, name) == 0)
205 			break;
206 	}
207 
208 	return (zv);
209 }
210 
211 void
212 zvol_create_cb(objset_t *os, void *arg, dmu_tx_t *tx)
213 {
214 	zfs_cmd_t *zc = arg;
215 	int error;
216 
217 	error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, zc->zc_volblocksize,
218 	    DMU_OT_NONE, 0, tx);
219 	ASSERT(error == 0);
220 
221 	error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
222 	    DMU_OT_NONE, 0, tx);
223 	ASSERT(error == 0);
224 
225 	error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &zc->zc_volsize, tx);
226 	ASSERT(error == 0);
227 }
228 
229 /*
230  * Replay a TX_WRITE ZIL transaction that didn't get committed
231  * after a system failure
232  */
233 static int
234 zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
235 {
236 	objset_t *os = zv->zv_objset;
237 	char *data = (char *)(lr + 1);	/* data follows lr_write_t */
238 	uint64_t off = lr->lr_offset;
239 	uint64_t len = lr->lr_length;
240 	dmu_tx_t *tx;
241 	int error;
242 
243 	if (byteswap)
244 		byteswap_uint64_array(lr, sizeof (*lr));
245 
246 restart:
247 	tx = dmu_tx_create(os);
248 	dmu_tx_hold_write(tx, ZVOL_OBJ, off, len);
249 	error = dmu_tx_assign(tx, zv->zv_txg_assign);
250 	if (error) {
251 		dmu_tx_abort(tx);
252 		if (error == ERESTART && zv->zv_txg_assign == TXG_NOWAIT) {
253 			txg_wait_open(dmu_objset_pool(os), 0);
254 			goto restart;
255 		}
256 	} else {
257 		dmu_write(os, ZVOL_OBJ, off, len, data, tx);
258 		dmu_tx_commit(tx);
259 	}
260 
261 	return (error);
262 }
263 
264 /* ARGSUSED */
265 static int
266 zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
267 {
268 	return (ENOTSUP);
269 }
270 
271 /*
272  * Callback vectors for replaying records.
273  * Only TX_WRITE is needed for zvol.
274  */
275 zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = {
276 	zvol_replay_err,	/* 0 no such transaction type */
277 	zvol_replay_err,	/* TX_CREATE */
278 	zvol_replay_err,	/* TX_MKDIR */
279 	zvol_replay_err,	/* TX_MKXATTR */
280 	zvol_replay_err,	/* TX_SYMLINK */
281 	zvol_replay_err,	/* TX_REMOVE */
282 	zvol_replay_err,	/* TX_RMDIR */
283 	zvol_replay_err,	/* TX_LINK */
284 	zvol_replay_err,	/* TX_RENAME */
285 	zvol_replay_write,	/* TX_WRITE */
286 	zvol_replay_err,	/* TX_TRUNCATE */
287 	zvol_replay_err,	/* TX_SETATTR */
288 	zvol_replay_err,	/* TX_ACL */
289 };
290 
291 /*
292  * Create a minor node for the specified volume.
293  */
294 int
295 zvol_create_minor(zfs_cmd_t *zc)
296 {
297 	char *name = zc->zc_name;
298 	dev_t dev = zc->zc_dev;
299 	zvol_state_t *zv;
300 	objset_t *os;
301 	uint64_t volsize;
302 	minor_t minor = 0;
303 	struct pathname linkpath;
304 	int ds_mode = DS_MODE_PRIMARY;
305 	vnode_t *vp = NULL;
306 	char *devpath;
307 	size_t devpathlen = strlen(ZVOL_FULL_DEV_DIR) + 1 + strlen(name) + 1;
308 	char chrbuf[30], blkbuf[30];
309 	int error;
310 
311 	mutex_enter(&zvol_state_lock);
312 
313 	if ((zv = zvol_minor_lookup(name)) != NULL) {
314 		mutex_exit(&zvol_state_lock);
315 		return (EEXIST);
316 	}
317 
318 	if (strchr(name, '@') != 0)
319 		ds_mode |= DS_MODE_READONLY;
320 
321 	error = dmu_objset_open(name, DMU_OST_ZVOL, ds_mode, &os);
322 
323 	if (error) {
324 		mutex_exit(&zvol_state_lock);
325 		return (error);
326 	}
327 
328 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
329 
330 	if (error) {
331 		dmu_objset_close(os);
332 		mutex_exit(&zvol_state_lock);
333 		return (error);
334 	}
335 
336 	/*
337 	 * If there's an existing /dev/zvol symlink, try to use the
338 	 * same minor number we used last time.
339 	 */
340 	devpath = kmem_alloc(devpathlen, KM_SLEEP);
341 
342 	(void) sprintf(devpath, "%s/%s", ZVOL_FULL_DEV_DIR, name);
343 
344 	error = lookupname(devpath, UIO_SYSSPACE, NO_FOLLOW, NULL, &vp);
345 
346 	kmem_free(devpath, devpathlen);
347 
348 	if (error == 0 && vp->v_type != VLNK)
349 		error = EINVAL;
350 
351 	if (error == 0) {
352 		pn_alloc(&linkpath);
353 		error = pn_getsymlink(vp, &linkpath, kcred);
354 		if (error == 0) {
355 			char *ms = strstr(linkpath.pn_path, ZVOL_PSEUDO_DEV);
356 			if (ms != NULL) {
357 				ms += strlen(ZVOL_PSEUDO_DEV);
358 				minor = stoi(&ms);
359 			}
360 		}
361 		pn_free(&linkpath);
362 	}
363 
364 	if (vp != NULL)
365 		VN_RELE(vp);
366 
367 	/*
368 	 * If we found a minor but it's already in use, we must pick a new one.
369 	 */
370 	if (minor != 0 && ddi_get_soft_state(zvol_state, minor) != NULL)
371 		minor = 0;
372 
373 	if (minor == 0)
374 		minor = zvol_minor_alloc();
375 
376 	if (minor == 0) {
377 		dmu_objset_close(os);
378 		mutex_exit(&zvol_state_lock);
379 		return (ENXIO);
380 	}
381 
382 	if (ddi_soft_state_zalloc(zvol_state, minor) != DDI_SUCCESS) {
383 		dmu_objset_close(os);
384 		mutex_exit(&zvol_state_lock);
385 		return (EAGAIN);
386 	}
387 
388 	(void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME, name);
389 
390 	(void) sprintf(chrbuf, "%uc,raw", minor);
391 
392 	if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR,
393 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
394 		ddi_soft_state_free(zvol_state, minor);
395 		dmu_objset_close(os);
396 		mutex_exit(&zvol_state_lock);
397 		return (EAGAIN);
398 	}
399 
400 	(void) sprintf(blkbuf, "%uc", minor);
401 
402 	if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK,
403 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
404 		ddi_remove_minor_node(zfs_dip, chrbuf);
405 		ddi_soft_state_free(zvol_state, minor);
406 		dmu_objset_close(os);
407 		mutex_exit(&zvol_state_lock);
408 		return (EAGAIN);
409 	}
410 
411 	zv = ddi_get_soft_state(zvol_state, minor);
412 
413 	(void) strcpy(zv->zv_name, name);
414 	zv->zv_min_bs = DEV_BSHIFT;
415 	zv->zv_minor = minor;
416 	zv->zv_volsize = volsize;
417 	zv->zv_objset = os;
418 	zv->zv_mode = ds_mode;
419 	zv->zv_zilog = zil_open(os, NULL);
420 
421 	zil_replay(os, zv, &zv->zv_txg_assign, zvol_replay_vector, NULL);
422 
423 	zvol_size_changed(zv, dev);
424 
425 	VERIFY(dsl_prop_register(dmu_objset_ds(zv->zv_objset),
426 	    "readonly", zvol_readonly_changed_cb, zv) == 0);
427 
428 	zvol_minors++;
429 
430 	mutex_exit(&zvol_state_lock);
431 
432 	return (0);
433 }
434 
435 /*
436  * Remove minor node for the specified volume.
437  */
438 int
439 zvol_remove_minor(zfs_cmd_t *zc)
440 {
441 	zvol_state_t *zv;
442 	char namebuf[30];
443 
444 	mutex_enter(&zvol_state_lock);
445 
446 	if ((zv = zvol_minor_lookup(zc->zc_name)) == NULL) {
447 		mutex_exit(&zvol_state_lock);
448 		return (ENXIO);
449 	}
450 
451 	if (zv->zv_total_opens != 0) {
452 		mutex_exit(&zvol_state_lock);
453 		return (EBUSY);
454 	}
455 
456 	(void) sprintf(namebuf, "%uc,raw", zv->zv_minor);
457 	ddi_remove_minor_node(zfs_dip, namebuf);
458 
459 	(void) sprintf(namebuf, "%uc", zv->zv_minor);
460 	ddi_remove_minor_node(zfs_dip, namebuf);
461 
462 	VERIFY(dsl_prop_unregister(dmu_objset_ds(zv->zv_objset),
463 	    "readonly", zvol_readonly_changed_cb, zv) == 0);
464 
465 	zil_close(zv->zv_zilog);
466 	zv->zv_zilog = NULL;
467 	dmu_objset_close(zv->zv_objset);
468 	zv->zv_objset = NULL;
469 
470 	ddi_soft_state_free(zvol_state, zv->zv_minor);
471 
472 	zvol_minors--;
473 
474 	mutex_exit(&zvol_state_lock);
475 
476 	return (0);
477 }
478 
479 int
480 zvol_set_volsize(zfs_cmd_t *zc)
481 {
482 	zvol_state_t *zv;
483 	dev_t dev = zc->zc_dev;
484 	dmu_tx_t *tx;
485 	int error;
486 	dmu_object_info_t doi;
487 
488 	mutex_enter(&zvol_state_lock);
489 
490 	if ((zv = zvol_minor_lookup(zc->zc_name)) == NULL) {
491 		mutex_exit(&zvol_state_lock);
492 		return (ENXIO);
493 	}
494 
495 	if ((error = dmu_object_info(zv->zv_objset, ZVOL_OBJ, &doi)) != 0 ||
496 	    (error = zvol_check_volsize(zc, doi.doi_data_block_size)) != 0) {
497 		mutex_exit(&zvol_state_lock);
498 		return (error);
499 	}
500 
501 	if (zv->zv_readonly || (zv->zv_mode & DS_MODE_READONLY)) {
502 		mutex_exit(&zvol_state_lock);
503 		return (EROFS);
504 	}
505 
506 	tx = dmu_tx_create(zv->zv_objset);
507 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, 1);
508 	dmu_tx_hold_free(tx, ZVOL_OBJ, zc->zc_volsize, DMU_OBJECT_END);
509 	error = dmu_tx_assign(tx, TXG_WAIT);
510 	if (error) {
511 		dmu_tx_abort(tx);
512 		mutex_exit(&zvol_state_lock);
513 		return (error);
514 	}
515 
516 	error = zap_update(zv->zv_objset, ZVOL_ZAP_OBJ, "size", 8, 1,
517 	    &zc->zc_volsize, tx);
518 	if (error == 0)
519 		dmu_free_range(zv->zv_objset, ZVOL_OBJ, zc->zc_volsize,
520 		    DMU_OBJECT_END, tx);
521 
522 	dmu_tx_commit(tx);
523 
524 	if (error == 0) {
525 		zv->zv_volsize = zc->zc_volsize;
526 		zvol_size_changed(zv, dev);
527 	}
528 
529 	mutex_exit(&zvol_state_lock);
530 
531 	return (error);
532 }
533 
534 int
535 zvol_set_volblocksize(zfs_cmd_t *zc)
536 {
537 	zvol_state_t *zv;
538 	dmu_tx_t *tx;
539 	int error;
540 
541 	mutex_enter(&zvol_state_lock);
542 
543 	if ((zv = zvol_minor_lookup(zc->zc_name)) == NULL) {
544 		mutex_exit(&zvol_state_lock);
545 		return (ENXIO);
546 	}
547 
548 	if (zv->zv_readonly || (zv->zv_mode & DS_MODE_READONLY)) {
549 		mutex_exit(&zvol_state_lock);
550 		return (EROFS);
551 	}
552 
553 	tx = dmu_tx_create(zv->zv_objset);
554 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
555 	error = dmu_tx_assign(tx, TXG_WAIT);
556 	if (error) {
557 		dmu_tx_abort(tx);
558 	} else {
559 		error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ,
560 		    zc->zc_volblocksize, 0, tx);
561 		if (error == ENOTSUP)
562 			error = EBUSY;
563 		dmu_tx_commit(tx);
564 	}
565 
566 	mutex_exit(&zvol_state_lock);
567 
568 	return (error);
569 }
570 
571 /*ARGSUSED*/
572 int
573 zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr)
574 {
575 	minor_t minor = getminor(*devp);
576 	zvol_state_t *zv;
577 
578 	if (minor == 0)			/* This is the control device */
579 		return (0);
580 
581 	mutex_enter(&zvol_state_lock);
582 
583 	zv = ddi_get_soft_state(zvol_state, minor);
584 	if (zv == NULL) {
585 		mutex_exit(&zvol_state_lock);
586 		return (ENXIO);
587 	}
588 
589 	ASSERT(zv->zv_objset != NULL);
590 
591 	if ((flag & FWRITE) &&
592 	    (zv->zv_readonly || (zv->zv_mode & DS_MODE_READONLY))) {
593 		mutex_exit(&zvol_state_lock);
594 		return (EROFS);
595 	}
596 
597 	if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) {
598 		zv->zv_open_count[otyp]++;
599 		zv->zv_total_opens++;
600 	}
601 
602 	mutex_exit(&zvol_state_lock);
603 
604 	return (0);
605 }
606 
607 /*ARGSUSED*/
608 int
609 zvol_close(dev_t dev, int flag, int otyp, cred_t *cr)
610 {
611 	minor_t minor = getminor(dev);
612 	zvol_state_t *zv;
613 
614 	if (minor == 0)		/* This is the control device */
615 		return (0);
616 
617 	mutex_enter(&zvol_state_lock);
618 
619 	zv = ddi_get_soft_state(zvol_state, minor);
620 	if (zv == NULL) {
621 		mutex_exit(&zvol_state_lock);
622 		return (ENXIO);
623 	}
624 
625 	/*
626 	 * The next statement is a workaround for the following DDI bug:
627 	 * 6343604 specfs race: multiple "last-close" of the same device
628 	 */
629 	if (zv->zv_total_opens == 0) {
630 		mutex_exit(&zvol_state_lock);
631 		return (0);
632 	}
633 
634 	/*
635 	 * If the open count is zero, this is a spurious close.
636 	 * That indicates a bug in the kernel / DDI framework.
637 	 */
638 	ASSERT(zv->zv_open_count[otyp] != 0);
639 	ASSERT(zv->zv_total_opens != 0);
640 
641 	/*
642 	 * You may get multiple opens, but only one close.
643 	 */
644 	zv->zv_open_count[otyp]--;
645 	zv->zv_total_opens--;
646 
647 	mutex_exit(&zvol_state_lock);
648 
649 	return (0);
650 }
651 
652 /*
653  * zvol_log_write() handles synchronous page writes using
654  * TX_WRITE ZIL transactions.
655  *
656  * We store data in the log buffers if it's small enough.
657  * Otherwise we flush the data out via dmu_sync().
658  */
659 ssize_t zvol_immediate_write_sz = 65536;
660 
661 int
662 zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t len,
663     char *addr)
664 {
665 	itx_t *itx;
666 	lr_write_t *lr;
667 	objset_t *os = zv->zv_objset;
668 	int dlen;
669 	int error;
670 
671 	dlen = (len <= zvol_immediate_write_sz ? len : 0);
672 	itx = zil_itx_create(TX_WRITE, sizeof (*lr) + dlen);
673 	lr = (lr_write_t *)&itx->itx_lr;
674 	lr->lr_foid = ZVOL_OBJ;
675 	lr->lr_offset = off;
676 	lr->lr_length = len;
677 	lr->lr_blkoff = 0;
678 	BP_ZERO(&lr->lr_blkptr);
679 
680 	/*
681 	 * Get the data as we know we'll be writing it immediately
682 	 */
683 	if (dlen) { /* immediate write */
684 		bcopy(addr, (char *)itx + offsetof(itx_t, itx_lr) +
685 		    sizeof (*lr), len);
686 	} else {
687 		txg_suspend(dmu_objset_pool(os));
688 		error = dmu_sync(os, ZVOL_OBJ, off, &lr->lr_blkoff,
689 		    &lr->lr_blkptr, dmu_tx_get_txg(tx));
690 		txg_resume(dmu_objset_pool(os));
691 		if (error) {
692 			kmem_free(itx, offsetof(itx_t, itx_lr));
693 			return (error);
694 		}
695 	}
696 	itx->itx_data_copied = 1;
697 
698 	(void) zil_itx_assign(zv->zv_zilog, itx, tx);
699 
700 	return (0);
701 }
702 
703 int
704 zvol_strategy(buf_t *bp)
705 {
706 	zvol_state_t *zv = ddi_get_soft_state(zvol_state, getminor(bp->b_edev));
707 	uint64_t off, volsize;
708 	size_t size, resid;
709 	char *addr;
710 	objset_t *os;
711 	int error = 0;
712 	int sync;
713 
714 	if (zv == NULL) {
715 		bioerror(bp, ENXIO);
716 		biodone(bp);
717 		return (0);
718 	}
719 
720 	if (getminor(bp->b_edev) == 0) {
721 		bioerror(bp, EINVAL);
722 		biodone(bp);
723 		return (0);
724 	}
725 
726 	if (zv->zv_readonly && !(bp->b_flags & B_READ)) {
727 		bioerror(bp, EROFS);
728 		biodone(bp);
729 		return (0);
730 	}
731 
732 	off = ldbtob(bp->b_blkno);
733 	volsize = zv->zv_volsize;
734 
735 	os = zv->zv_objset;
736 	ASSERT(os != NULL);
737 	sync = !(bp->b_flags & B_ASYNC) && !(zil_disable);
738 
739 	bp_mapin(bp);
740 	addr = bp->b_un.b_addr;
741 	resid = bp->b_bcount;
742 
743 	while (resid != 0 && off < volsize) {
744 
745 		size = MIN(resid, 1UL << 20);	/* cap at 1MB per tx */
746 
747 		if (size > volsize - off)	/* don't write past the end */
748 			size = volsize - off;
749 
750 		if (bp->b_flags & B_READ) {
751 			error = dmu_read_canfail(os, ZVOL_OBJ,
752 			    off, size, addr);
753 		} else {
754 			dmu_tx_t *tx = dmu_tx_create(os);
755 			dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
756 			error = dmu_tx_assign(tx, TXG_WAIT);
757 			if (error) {
758 				dmu_tx_abort(tx);
759 			} else {
760 				dmu_write(os, ZVOL_OBJ, off, size, addr, tx);
761 				if (sync) {
762 					/* use the ZIL to commit this write */
763 					error = zvol_log_write(zv, tx, off,
764 					    size, addr);
765 					if (error) {
766 						txg_wait_synced(
767 						    dmu_objset_pool(os), 0);
768 						sync = B_FALSE;
769 					}
770 				}
771 				dmu_tx_commit(tx);
772 			}
773 		}
774 		if (error)
775 			break;
776 		off += size;
777 		addr += size;
778 		resid -= size;
779 	}
780 
781 	if ((bp->b_resid = resid) == bp->b_bcount)
782 		bioerror(bp, off > volsize ? EINVAL : error);
783 
784 	biodone(bp);
785 
786 	if (sync)
787 		zil_commit(zv->zv_zilog, UINT64_MAX, FDSYNC);
788 
789 	return (0);
790 }
791 
792 /*ARGSUSED*/
793 int
794 zvol_read(dev_t dev, uio_t *uiop, cred_t *cr)
795 {
796 	return (physio(zvol_strategy, NULL, dev, B_READ, minphys, uiop));
797 }
798 
799 /*ARGSUSED*/
800 int
801 zvol_write(dev_t dev, uio_t *uiop, cred_t *cr)
802 {
803 	return (physio(zvol_strategy, NULL, dev, B_WRITE, minphys, uiop));
804 }
805 
806 /*ARGSUSED*/
807 int
808 zvol_aread(dev_t dev, struct aio_req *aio, cred_t *cr)
809 {
810 	return (aphysio(zvol_strategy, anocancel, dev, B_READ, minphys, aio));
811 }
812 
813 /*ARGSUSED*/
814 int
815 zvol_awrite(dev_t dev, struct aio_req *aio, cred_t *cr)
816 {
817 	return (aphysio(zvol_strategy, anocancel, dev, B_WRITE, minphys, aio));
818 }
819 
820 /*
821  * Dirtbag ioctls to support mkfs(1M) for UFS filesystems.  See dkio(7I).
822  */
823 /*ARGSUSED*/
824 int
825 zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
826 {
827 	zvol_state_t *zv;
828 	struct dk_cinfo dkc;
829 	struct dk_minfo dkm;
830 	dk_efi_t efi;
831 	efi_gpt_t gpt;
832 	efi_gpe_t gpe;
833 	struct uuid uuid = EFI_RESERVED;
834 	uint32_t crc;
835 	int error = 0;
836 
837 	mutex_enter(&zvol_state_lock);
838 
839 	zv = ddi_get_soft_state(zvol_state, getminor(dev));
840 
841 	if (zv == NULL) {
842 		mutex_exit(&zvol_state_lock);
843 		return (ENXIO);
844 	}
845 
846 	switch (cmd) {
847 
848 	case DKIOCINFO:
849 		bzero(&dkc, sizeof (dkc));
850 		(void) strcpy(dkc.dki_cname, "zvol");
851 		(void) strcpy(dkc.dki_dname, "zvol");
852 		dkc.dki_ctype = DKC_UNKNOWN;
853 		dkc.dki_maxtransfer = 1 << 15;
854 		mutex_exit(&zvol_state_lock);
855 		if (ddi_copyout(&dkc, (void *)arg, sizeof (dkc), flag))
856 			error = EFAULT;
857 		return (error);
858 
859 	case DKIOCGMEDIAINFO:
860 		bzero(&dkm, sizeof (dkm));
861 		dkm.dki_lbsize = 1U << zv->zv_min_bs;
862 		dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
863 		dkm.dki_media_type = DK_UNKNOWN;
864 		mutex_exit(&zvol_state_lock);
865 		if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
866 			error = EFAULT;
867 		return (error);
868 
869 	case DKIOCGETEFI:
870 		if (ddi_copyin((void *)arg, &efi, sizeof (dk_efi_t), flag)) {
871 			mutex_exit(&zvol_state_lock);
872 			return (EFAULT);
873 		}
874 
875 		bzero(&gpt, sizeof (gpt));
876 		bzero(&gpe, sizeof (gpe));
877 
878 		efi.dki_data = (void *)(uintptr_t)efi.dki_data_64;
879 
880 		if (efi.dki_length < sizeof (gpt) + sizeof (gpe)) {
881 			mutex_exit(&zvol_state_lock);
882 			return (EINVAL);
883 		}
884 
885 		efi.dki_length = sizeof (gpt) + sizeof (gpe);
886 
887 		gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
888 		gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
889 		gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt));
890 		gpt.efi_gpt_FirstUsableLBA = LE_64(0ULL);
891 		gpt.efi_gpt_LastUsableLBA =
892 		    LE_64((zv->zv_volsize >> zv->zv_min_bs) - 1);
893 		gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
894 		gpt.efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (gpe));
895 
896 		UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
897 		gpe.efi_gpe_StartingLBA = gpt.efi_gpt_FirstUsableLBA;
898 		gpe.efi_gpe_EndingLBA = gpt.efi_gpt_LastUsableLBA;
899 
900 		CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
901 		gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
902 
903 		CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table);
904 		gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
905 
906 		mutex_exit(&zvol_state_lock);
907 		if (ddi_copyout(&gpt, efi.dki_data, sizeof (gpt), flag) ||
908 		    ddi_copyout(&gpe, efi.dki_data + 1, sizeof (gpe), flag))
909 			error = EFAULT;
910 		return (error);
911 
912 	default:
913 		error = ENOTSUP;
914 		break;
915 
916 	}
917 	mutex_exit(&zvol_state_lock);
918 	return (error);
919 }
920 
921 int
922 zvol_busy(void)
923 {
924 	return (zvol_minors != 0);
925 }
926 
927 void
928 zvol_init(void)
929 {
930 	VERIFY(ddi_soft_state_init(&zvol_state, sizeof (zvol_state_t), 1) == 0);
931 	mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
932 }
933 
934 void
935 zvol_fini(void)
936 {
937 	mutex_destroy(&zvol_state_lock);
938 	ddi_soft_state_fini(&zvol_state);
939 }
940