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