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