xref: /titanic_41/usr/src/uts/common/fs/zfs/zvol.c (revision 88df2d76721d60b8b7cad14f9380446d06569f7c)
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 	krwlock_t	zv_dslock;	/* dmu_sync() rwlock */
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 	rw_init(&zv->zv_dslock, NULL, RW_DEFAULT, NULL);
418 
419 	zil_replay(os, zv, &zv->zv_txg_assign, zvol_replay_vector, NULL);
420 
421 	zvol_size_changed(zv, dev);
422 
423 	/* XXX this should handle the possible i/o error */
424 	VERIFY(dsl_prop_register(dmu_objset_ds(zv->zv_objset),
425 	    "readonly", zvol_readonly_changed_cb, zv) == 0);
426 
427 	zvol_minors++;
428 
429 	mutex_exit(&zvol_state_lock);
430 
431 	return (0);
432 }
433 
434 /*
435  * Remove minor node for the specified volume.
436  */
437 int
438 zvol_remove_minor(zfs_cmd_t *zc)
439 {
440 	zvol_state_t *zv;
441 	char namebuf[30];
442 
443 	mutex_enter(&zvol_state_lock);
444 
445 	if ((zv = zvol_minor_lookup(zc->zc_name)) == NULL) {
446 		mutex_exit(&zvol_state_lock);
447 		return (ENXIO);
448 	}
449 
450 	if (zv->zv_total_opens != 0) {
451 		mutex_exit(&zvol_state_lock);
452 		return (EBUSY);
453 	}
454 
455 	(void) sprintf(namebuf, "%uc,raw", zv->zv_minor);
456 	ddi_remove_minor_node(zfs_dip, namebuf);
457 
458 	(void) sprintf(namebuf, "%uc", zv->zv_minor);
459 	ddi_remove_minor_node(zfs_dip, namebuf);
460 
461 	VERIFY(dsl_prop_unregister(dmu_objset_ds(zv->zv_objset),
462 	    "readonly", zvol_readonly_changed_cb, zv) == 0);
463 
464 	zil_close(zv->zv_zilog);
465 	zv->zv_zilog = NULL;
466 	dmu_objset_close(zv->zv_objset);
467 	zv->zv_objset = NULL;
468 
469 	ddi_soft_state_free(zvol_state, zv->zv_minor);
470 
471 	zvol_minors--;
472 
473 	mutex_exit(&zvol_state_lock);
474 
475 	return (0);
476 }
477 
478 int
479 zvol_set_volsize(zfs_cmd_t *zc)
480 {
481 	zvol_state_t *zv;
482 	dev_t dev = zc->zc_dev;
483 	dmu_tx_t *tx;
484 	int error;
485 	dmu_object_info_t doi;
486 
487 	mutex_enter(&zvol_state_lock);
488 
489 	if ((zv = zvol_minor_lookup(zc->zc_name)) == NULL) {
490 		mutex_exit(&zvol_state_lock);
491 		return (ENXIO);
492 	}
493 
494 	if ((error = dmu_object_info(zv->zv_objset, ZVOL_OBJ, &doi)) != 0 ||
495 	    (error = zvol_check_volsize(zc, doi.doi_data_block_size)) != 0) {
496 		mutex_exit(&zvol_state_lock);
497 		return (error);
498 	}
499 
500 	if (zv->zv_readonly || (zv->zv_mode & DS_MODE_READONLY)) {
501 		mutex_exit(&zvol_state_lock);
502 		return (EROFS);
503 	}
504 
505 	tx = dmu_tx_create(zv->zv_objset);
506 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
507 	dmu_tx_hold_free(tx, ZVOL_OBJ, zc->zc_volsize, DMU_OBJECT_END);
508 	error = dmu_tx_assign(tx, TXG_WAIT);
509 	if (error) {
510 		dmu_tx_abort(tx);
511 		mutex_exit(&zvol_state_lock);
512 		return (error);
513 	}
514 
515 	error = zap_update(zv->zv_objset, ZVOL_ZAP_OBJ, "size", 8, 1,
516 	    &zc->zc_volsize, tx);
517 	if (error == 0) {
518 		error = dmu_free_range(zv->zv_objset, ZVOL_OBJ, zc->zc_volsize,
519 		    DMU_OBJECT_END, tx);
520 	}
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  * Create and return an immediate write ZIL transaction.
654  */
655 itx_t *
656 zvol_immediate_itx(offset_t off, ssize_t len, char *addr)
657 {
658 	itx_t *itx;
659 	lr_write_t *lr;
660 
661 	itx = zil_itx_create(TX_WRITE, sizeof (*lr) + len);
662 	lr = (lr_write_t *)&itx->itx_lr;
663 	lr->lr_foid = ZVOL_OBJ;
664 	lr->lr_offset = off;
665 	lr->lr_length = len;
666 	lr->lr_blkoff = 0;
667 	BP_ZERO(&lr->lr_blkptr);
668 	bcopy(addr, (char *)itx + offsetof(itx_t, itx_lr) +
669 	    sizeof (*lr), len);
670 	itx->itx_wr_state = WR_COPIED;
671 	return (itx);
672 }
673 
674 /*
675  * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
676  *
677  * We store data in the log buffers if it's small enough.
678  * Otherwise we flush the data out via dmu_sync().
679  */
680 ssize_t zvol_immediate_write_sz = 65536;
681 
682 int
683 zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t len,
684     char *addr)
685 {
686 	dmu_object_info_t doi;
687 	ssize_t nbytes;
688 	itx_t *itx;
689 	lr_write_t *lr;
690 	objset_t *os;
691 	uint64_t txg;
692 	int error;
693 	uint32_t blocksize;
694 
695 	/* handle common case */
696 	if (len <= zvol_immediate_write_sz) {
697 		itx = zvol_immediate_itx(off, len, addr);
698 		(void) zil_itx_assign(zv->zv_zilog, itx, tx);
699 		return (0);
700 	}
701 
702 	txg = dmu_tx_get_txg(tx);
703 	os = zv->zv_objset;
704 
705 	/*
706 	 * We need to dmu_sync() each block in the range.
707 	 * For this we need the blocksize.
708 	 */
709 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
710 	if (error)
711 		return (error);
712 	blocksize = doi.doi_data_block_size;
713 
714 	/*
715 	 * We need to immediate write or dmu_sync() each block in the range.
716 	 */
717 	while (len) {
718 		nbytes = MIN(len, blocksize - P2PHASE(off, blocksize));
719 		if (nbytes <= zvol_immediate_write_sz) {
720 			itx = zvol_immediate_itx(off, nbytes, addr);
721 		} else {
722 			itx = zil_itx_create(TX_WRITE, sizeof (*lr));
723 			lr = (lr_write_t *)&itx->itx_lr;
724 			lr->lr_foid = ZVOL_OBJ;
725 			lr->lr_offset = off;
726 			lr->lr_length = nbytes;
727 			lr->lr_blkoff = 0;
728 			BP_ZERO(&lr->lr_blkptr);
729 
730 			txg_suspend(dmu_objset_pool(os));
731 			error = dmu_sync(os, ZVOL_OBJ, off, &lr->lr_blkoff,
732 			    &lr->lr_blkptr, txg);
733 			txg_resume(dmu_objset_pool(os));
734 			if (error) {
735 				kmem_free(itx, offsetof(itx_t, itx_lr));
736 				return (error);
737 			}
738 			itx->itx_wr_state = WR_COPIED;
739 		}
740 		(void) zil_itx_assign(zv->zv_zilog, itx, tx);
741 		len -= nbytes;
742 		off += nbytes;
743 	}
744 	return (0);
745 }
746 
747 int
748 zvol_strategy(buf_t *bp)
749 {
750 	zvol_state_t *zv = ddi_get_soft_state(zvol_state, getminor(bp->b_edev));
751 	uint64_t off, volsize;
752 	size_t size, resid;
753 	char *addr;
754 	objset_t *os;
755 	int error = 0;
756 	int sync;
757 	int reading;
758 	int txg_sync_needed = B_FALSE;
759 
760 	if (zv == NULL) {
761 		bioerror(bp, ENXIO);
762 		biodone(bp);
763 		return (0);
764 	}
765 
766 	if (getminor(bp->b_edev) == 0) {
767 		bioerror(bp, EINVAL);
768 		biodone(bp);
769 		return (0);
770 	}
771 
772 	if (zv->zv_readonly && !(bp->b_flags & B_READ)) {
773 		bioerror(bp, EROFS);
774 		biodone(bp);
775 		return (0);
776 	}
777 
778 	off = ldbtob(bp->b_blkno);
779 	volsize = zv->zv_volsize;
780 
781 	os = zv->zv_objset;
782 	ASSERT(os != NULL);
783 	sync = !(bp->b_flags & B_ASYNC) && !(zil_disable);
784 
785 	bp_mapin(bp);
786 	addr = bp->b_un.b_addr;
787 	resid = bp->b_bcount;
788 
789 	/*
790 	 * There must be no buffer changes when doing a dmu_sync() because
791 	 * we can't change the data whilst calculating the checksum.
792 	 * A better approach than a per zvol rwlock would be to lock ranges.
793 	 */
794 	reading = bp->b_flags & B_READ;
795 	if (reading || resid <= zvol_immediate_write_sz)
796 		rw_enter(&zv->zv_dslock, RW_READER);
797 	else
798 		rw_enter(&zv->zv_dslock, RW_WRITER);
799 
800 	while (resid != 0 && off < volsize) {
801 
802 		size = MIN(resid, 1UL << 20);	/* cap at 1MB per tx */
803 
804 		if (size > volsize - off)	/* don't write past the end */
805 			size = volsize - off;
806 
807 		if (reading) {
808 			error = dmu_read(os, ZVOL_OBJ, off, size, addr);
809 		} else {
810 			dmu_tx_t *tx = dmu_tx_create(os);
811 			dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
812 			error = dmu_tx_assign(tx, TXG_WAIT);
813 			if (error) {
814 				dmu_tx_abort(tx);
815 			} else {
816 				dmu_write(os, ZVOL_OBJ, off, size, addr, tx);
817 				if (sync) {
818 					/* use the ZIL to commit this write */
819 					if (zvol_log_write(zv, tx, off, size,
820 					    addr) != 0) {
821 						txg_sync_needed = B_TRUE;
822 					}
823 				}
824 				dmu_tx_commit(tx);
825 			}
826 		}
827 		if (error)
828 			break;
829 		off += size;
830 		addr += size;
831 		resid -= size;
832 	}
833 	rw_exit(&zv->zv_dslock);
834 
835 	if ((bp->b_resid = resid) == bp->b_bcount)
836 		bioerror(bp, off > volsize ? EINVAL : error);
837 
838 	biodone(bp);
839 
840 	if (sync) {
841 		if (txg_sync_needed)
842 			txg_wait_synced(dmu_objset_pool(os), 0);
843 		else
844 			zil_commit(zv->zv_zilog, UINT64_MAX, FDSYNC);
845 	}
846 
847 	return (0);
848 }
849 
850 /*ARGSUSED*/
851 int
852 zvol_read(dev_t dev, uio_t *uiop, cred_t *cr)
853 {
854 	return (physio(zvol_strategy, NULL, dev, B_READ, minphys, uiop));
855 }
856 
857 /*ARGSUSED*/
858 int
859 zvol_write(dev_t dev, uio_t *uiop, cred_t *cr)
860 {
861 	return (physio(zvol_strategy, NULL, dev, B_WRITE, minphys, uiop));
862 }
863 
864 /*ARGSUSED*/
865 int
866 zvol_aread(dev_t dev, struct aio_req *aio, cred_t *cr)
867 {
868 	return (aphysio(zvol_strategy, anocancel, dev, B_READ, minphys, aio));
869 }
870 
871 /*ARGSUSED*/
872 int
873 zvol_awrite(dev_t dev, struct aio_req *aio, cred_t *cr)
874 {
875 	return (aphysio(zvol_strategy, anocancel, dev, B_WRITE, minphys, aio));
876 }
877 
878 /*
879  * Dirtbag ioctls to support mkfs(1M) for UFS filesystems.  See dkio(7I).
880  */
881 /*ARGSUSED*/
882 int
883 zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
884 {
885 	zvol_state_t *zv;
886 	struct dk_cinfo dkc;
887 	struct dk_minfo dkm;
888 	dk_efi_t efi;
889 	efi_gpt_t gpt;
890 	efi_gpe_t gpe;
891 	struct uuid uuid = EFI_RESERVED;
892 	uint32_t crc;
893 	int error = 0;
894 
895 	mutex_enter(&zvol_state_lock);
896 
897 	zv = ddi_get_soft_state(zvol_state, getminor(dev));
898 
899 	if (zv == NULL) {
900 		mutex_exit(&zvol_state_lock);
901 		return (ENXIO);
902 	}
903 
904 	switch (cmd) {
905 
906 	case DKIOCINFO:
907 		bzero(&dkc, sizeof (dkc));
908 		(void) strcpy(dkc.dki_cname, "zvol");
909 		(void) strcpy(dkc.dki_dname, "zvol");
910 		dkc.dki_ctype = DKC_UNKNOWN;
911 		dkc.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs);
912 		mutex_exit(&zvol_state_lock);
913 		if (ddi_copyout(&dkc, (void *)arg, sizeof (dkc), flag))
914 			error = EFAULT;
915 		return (error);
916 
917 	case DKIOCGMEDIAINFO:
918 		bzero(&dkm, sizeof (dkm));
919 		dkm.dki_lbsize = 1U << zv->zv_min_bs;
920 		dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
921 		dkm.dki_media_type = DK_UNKNOWN;
922 		mutex_exit(&zvol_state_lock);
923 		if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
924 			error = EFAULT;
925 		return (error);
926 
927 	case DKIOCGETEFI:
928 		if (ddi_copyin((void *)arg, &efi, sizeof (dk_efi_t), flag)) {
929 			mutex_exit(&zvol_state_lock);
930 			return (EFAULT);
931 		}
932 
933 		bzero(&gpt, sizeof (gpt));
934 		bzero(&gpe, sizeof (gpe));
935 
936 		efi.dki_data = (void *)(uintptr_t)efi.dki_data_64;
937 
938 		if (efi.dki_length < sizeof (gpt) + sizeof (gpe)) {
939 			mutex_exit(&zvol_state_lock);
940 			return (EINVAL);
941 		}
942 
943 		efi.dki_length = sizeof (gpt) + sizeof (gpe);
944 
945 		gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
946 		gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
947 		gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt));
948 		gpt.efi_gpt_FirstUsableLBA = LE_64(0ULL);
949 		gpt.efi_gpt_LastUsableLBA =
950 		    LE_64((zv->zv_volsize >> zv->zv_min_bs) - 1);
951 		gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
952 		gpt.efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (gpe));
953 
954 		UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
955 		gpe.efi_gpe_StartingLBA = gpt.efi_gpt_FirstUsableLBA;
956 		gpe.efi_gpe_EndingLBA = gpt.efi_gpt_LastUsableLBA;
957 
958 		CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
959 		gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
960 
961 		CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table);
962 		gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
963 
964 		mutex_exit(&zvol_state_lock);
965 		if (ddi_copyout(&gpt, efi.dki_data, sizeof (gpt), flag) ||
966 		    ddi_copyout(&gpe, efi.dki_data + 1, sizeof (gpe), flag))
967 			error = EFAULT;
968 		return (error);
969 
970 	default:
971 		error = ENOTSUP;
972 		break;
973 
974 	}
975 	mutex_exit(&zvol_state_lock);
976 	return (error);
977 }
978 
979 int
980 zvol_busy(void)
981 {
982 	return (zvol_minors != 0);
983 }
984 
985 void
986 zvol_init(void)
987 {
988 	VERIFY(ddi_soft_state_init(&zvol_state, sizeof (zvol_state_t), 1) == 0);
989 	mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
990 }
991 
992 void
993 zvol_fini(void)
994 {
995 	mutex_destroy(&zvol_state_lock);
996 	ddi_soft_state_fini(&zvol_state);
997 }
998