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.
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.
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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Portions Copyright 2010 Robert Milkowski
26 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
27 * Copyright (c) 2013 by Delphix. All rights reserved.
28 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
32 * ZFS volume emulation driver.
34 * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes.
35 * Volumes are accessed through the symbolic links named:
37 * /dev/zvol/dsk/<pool_name>/<dataset_name>
38 * /dev/zvol/rdsk/<pool_name>/<dataset_name>
40 * These links are created by the /dev filesystem (sdev_zvolops.c).
41 * Volumes are persistent through reboot. No user command needs to be
42 * run before opening and using a device.
45 #include <sys/types.h>
46 #include <sys/param.h>
47 #include <sys/errno.h>
50 #include <sys/modctl.h>
54 #include <sys/cmn_err.h>
58 #include <sys/spa_impl.h>
60 #include <sys/dmu_traverse.h>
61 #include <sys/dnode.h>
62 #include <sys/dsl_dataset.h>
63 #include <sys/dsl_prop.h>
65 #include <sys/efi_partition.h>
66 #include <sys/byteorder.h>
67 #include <sys/pathname.h>
69 #include <sys/sunddi.h>
70 #include <sys/crc32.h>
71 #include <sys/dirent.h>
72 #include <sys/policy.h>
73 #include <sys/fs/zfs.h>
74 #include <sys/zfs_ioctl.h>
75 #include <sys/mkdev.h>
77 #include <sys/refcount.h>
78 #include <sys/zfs_znode.h>
79 #include <sys/zfs_rlock.h>
80 #include <sys/vdev_disk.h>
81 #include <sys/vdev_impl.h>
82 #include <sys/vdev_raidz.h>
84 #include <sys/dumphdr.h>
85 #include <sys/zil_impl.h>
87 #include <sys/dmu_tx.h>
88 #include <sys/zfeature.h>
89 #include <sys/zio_checksum.h>
91 #include "zfs_namecheck.h"
94 static char *zvol_tag
= "zvol_tag";
96 #define ZVOL_DUMPSIZE "dumpsize"
99 * This lock protects the zfsdev_state structure from being modified
100 * while it's being used, e.g. an open that comes in before a create
101 * finishes. It also protects temporary opens of the dataset so that,
102 * e.g., an open doesn't get a spurious EBUSY.
104 kmutex_t zfsdev_state_lock
;
105 static uint32_t zvol_minors
;
107 typedef struct zvol_extent
{
109 dva_t ze_dva
; /* dva associated with this extent */
110 uint64_t ze_nblks
; /* number of blocks in extent */
114 * The in-core state of each volume.
116 typedef struct zvol_state
{
117 char zv_name
[MAXPATHLEN
]; /* pool/dd name */
118 uint64_t zv_volsize
; /* amount of space we advertise */
119 uint64_t zv_volblocksize
; /* volume block size */
120 minor_t zv_minor
; /* minor number */
121 uint8_t zv_min_bs
; /* minimum addressable block shift */
122 uint8_t zv_flags
; /* readonly, dumpified, etc. */
123 objset_t
*zv_objset
; /* objset handle */
124 uint32_t zv_open_count
[OTYPCNT
]; /* open counts */
125 uint32_t zv_total_opens
; /* total open count */
126 zilog_t
*zv_zilog
; /* ZIL handle */
127 list_t zv_extents
; /* List of extents for dump */
128 znode_t zv_znode
; /* for range locking */
129 dmu_buf_t
*zv_dbuf
; /* bonus handle */
133 * zvol specific flags
135 #define ZVOL_RDONLY 0x1
136 #define ZVOL_DUMPIFIED 0x2
137 #define ZVOL_EXCL 0x4
141 * zvol maximum transfer in one DMU tx.
143 int zvol_maxphys
= DMU_MAX_ACCESS
/2;
145 extern int zfs_set_prop_nvlist(const char *, zprop_source_t
,
146 nvlist_t
*, nvlist_t
*);
147 static int zvol_remove_zv(zvol_state_t
*);
148 static int zvol_get_data(void *arg
, lr_write_t
*lr
, char *buf
, zio_t
*zio
);
149 static int zvol_dumpify(zvol_state_t
*zv
);
150 static int zvol_dump_fini(zvol_state_t
*zv
);
151 static int zvol_dump_init(zvol_state_t
*zv
, boolean_t resize
);
154 zvol_size_changed(zvol_state_t
*zv
, uint64_t volsize
)
156 dev_t dev
= makedevice(ddi_driver_major(zfs_dip
), zv
->zv_minor
);
158 zv
->zv_volsize
= volsize
;
159 VERIFY(ddi_prop_update_int64(dev
, zfs_dip
,
160 "Size", volsize
) == DDI_SUCCESS
);
161 VERIFY(ddi_prop_update_int64(dev
, zfs_dip
,
162 "Nblocks", lbtodb(volsize
)) == DDI_SUCCESS
);
164 /* Notify specfs to invalidate the cached size */
165 spec_size_invalidate(dev
, VBLK
);
166 spec_size_invalidate(dev
, VCHR
);
170 zvol_check_volsize(uint64_t volsize
, uint64_t blocksize
)
173 return (SET_ERROR(EINVAL
));
175 if (volsize
% blocksize
!= 0)
176 return (SET_ERROR(EINVAL
));
179 if (volsize
- 1 > SPEC_MAXOFFSET_T
)
180 return (SET_ERROR(EOVERFLOW
));
186 zvol_check_volblocksize(uint64_t volblocksize
)
188 if (volblocksize
< SPA_MINBLOCKSIZE
||
189 volblocksize
> SPA_MAXBLOCKSIZE
||
191 return (SET_ERROR(EDOM
));
197 zvol_get_stats(objset_t
*os
, nvlist_t
*nv
)
200 dmu_object_info_t doi
;
203 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &val
);
207 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_VOLSIZE
, val
);
209 error
= dmu_object_info(os
, ZVOL_OBJ
, &doi
);
212 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_VOLBLOCKSIZE
,
213 doi
.doi_data_block_size
);
219 static zvol_state_t
*
220 zvol_minor_lookup(const char *name
)
225 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
227 for (minor
= 1; minor
<= ZFSDEV_MAX_MINOR
; minor
++) {
228 zv
= zfsdev_get_soft_state(minor
, ZSST_ZVOL
);
231 if (strcmp(zv
->zv_name
, name
) == 0)
238 /* extent mapping arg */
246 zvol_map_block(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
247 const zbookmark_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
249 struct maparg
*ma
= arg
;
251 int bs
= ma
->ma_zv
->zv_volblocksize
;
253 if (BP_IS_HOLE(bp
) ||
254 zb
->zb_object
!= ZVOL_OBJ
|| zb
->zb_level
!= 0)
257 VERIFY(!BP_IS_EMBEDDED(bp
));
259 VERIFY3U(ma
->ma_blks
, ==, zb
->zb_blkid
);
262 /* Abort immediately if we have encountered gang blocks */
264 return (SET_ERROR(EFRAGS
));
267 * See if the block is at the end of the previous extent.
269 ze
= list_tail(&ma
->ma_zv
->zv_extents
);
271 DVA_GET_VDEV(BP_IDENTITY(bp
)) == DVA_GET_VDEV(&ze
->ze_dva
) &&
272 DVA_GET_OFFSET(BP_IDENTITY(bp
)) ==
273 DVA_GET_OFFSET(&ze
->ze_dva
) + ze
->ze_nblks
* bs
) {
278 dprintf_bp(bp
, "%s", "next blkptr:");
280 /* start a new extent */
281 ze
= kmem_zalloc(sizeof (zvol_extent_t
), KM_SLEEP
);
282 ze
->ze_dva
= bp
->blk_dva
[0]; /* structure assignment */
284 list_insert_tail(&ma
->ma_zv
->zv_extents
, ze
);
289 zvol_free_extents(zvol_state_t
*zv
)
293 while (ze
= list_head(&zv
->zv_extents
)) {
294 list_remove(&zv
->zv_extents
, ze
);
295 kmem_free(ze
, sizeof (zvol_extent_t
));
300 zvol_get_lbas(zvol_state_t
*zv
)
302 objset_t
*os
= zv
->zv_objset
;
308 zvol_free_extents(zv
);
310 /* commit any in-flight changes before traversing the dataset */
311 txg_wait_synced(dmu_objset_pool(os
), 0);
312 err
= traverse_dataset(dmu_objset_ds(os
), 0,
313 TRAVERSE_PRE
| TRAVERSE_PREFETCH_METADATA
, zvol_map_block
, &ma
);
314 if (err
|| ma
.ma_blks
!= (zv
->zv_volsize
/ zv
->zv_volblocksize
)) {
315 zvol_free_extents(zv
);
316 return (err
? err
: EIO
);
324 zvol_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
326 zfs_creat_t
*zct
= arg
;
327 nvlist_t
*nvprops
= zct
->zct_props
;
329 uint64_t volblocksize
, volsize
;
331 VERIFY(nvlist_lookup_uint64(nvprops
,
332 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &volsize
) == 0);
333 if (nvlist_lookup_uint64(nvprops
,
334 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
), &volblocksize
) != 0)
335 volblocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
338 * These properties must be removed from the list so the generic
339 * property setting step won't apply to them.
341 VERIFY(nvlist_remove_all(nvprops
,
342 zfs_prop_to_name(ZFS_PROP_VOLSIZE
)) == 0);
343 (void) nvlist_remove_all(nvprops
,
344 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
));
346 error
= dmu_object_claim(os
, ZVOL_OBJ
, DMU_OT_ZVOL
, volblocksize
,
350 error
= zap_create_claim(os
, ZVOL_ZAP_OBJ
, DMU_OT_ZVOL_PROP
,
354 error
= zap_update(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
, tx
);
359 * Replay a TX_TRUNCATE ZIL transaction if asked. TX_TRUNCATE is how we
360 * implement DKIOCFREE/free-long-range.
363 zvol_replay_truncate(zvol_state_t
*zv
, lr_truncate_t
*lr
, boolean_t byteswap
)
365 uint64_t offset
, length
;
368 byteswap_uint64_array(lr
, sizeof (*lr
));
370 offset
= lr
->lr_offset
;
371 length
= lr
->lr_length
;
373 return (dmu_free_long_range(zv
->zv_objset
, ZVOL_OBJ
, offset
, length
));
377 * Replay a TX_WRITE ZIL transaction that didn't get committed
378 * after a system failure
381 zvol_replay_write(zvol_state_t
*zv
, lr_write_t
*lr
, boolean_t byteswap
)
383 objset_t
*os
= zv
->zv_objset
;
384 char *data
= (char *)(lr
+ 1); /* data follows lr_write_t */
385 uint64_t offset
, length
;
390 byteswap_uint64_array(lr
, sizeof (*lr
));
392 offset
= lr
->lr_offset
;
393 length
= lr
->lr_length
;
395 /* If it's a dmu_sync() block, write the whole block */
396 if (lr
->lr_common
.lrc_reclen
== sizeof (lr_write_t
)) {
397 uint64_t blocksize
= BP_GET_LSIZE(&lr
->lr_blkptr
);
398 if (length
< blocksize
) {
399 offset
-= offset
% blocksize
;
404 tx
= dmu_tx_create(os
);
405 dmu_tx_hold_write(tx
, ZVOL_OBJ
, offset
, length
);
406 error
= dmu_tx_assign(tx
, TXG_WAIT
);
410 dmu_write(os
, ZVOL_OBJ
, offset
, length
, data
, tx
);
419 zvol_replay_err(zvol_state_t
*zv
, lr_t
*lr
, boolean_t byteswap
)
421 return (SET_ERROR(ENOTSUP
));
425 * Callback vectors for replaying records.
426 * Only TX_WRITE and TX_TRUNCATE are needed for zvol.
428 zil_replay_func_t
*zvol_replay_vector
[TX_MAX_TYPE
] = {
429 zvol_replay_err
, /* 0 no such transaction type */
430 zvol_replay_err
, /* TX_CREATE */
431 zvol_replay_err
, /* TX_MKDIR */
432 zvol_replay_err
, /* TX_MKXATTR */
433 zvol_replay_err
, /* TX_SYMLINK */
434 zvol_replay_err
, /* TX_REMOVE */
435 zvol_replay_err
, /* TX_RMDIR */
436 zvol_replay_err
, /* TX_LINK */
437 zvol_replay_err
, /* TX_RENAME */
438 zvol_replay_write
, /* TX_WRITE */
439 zvol_replay_truncate
, /* TX_TRUNCATE */
440 zvol_replay_err
, /* TX_SETATTR */
441 zvol_replay_err
, /* TX_ACL */
442 zvol_replay_err
, /* TX_CREATE_ACL */
443 zvol_replay_err
, /* TX_CREATE_ATTR */
444 zvol_replay_err
, /* TX_CREATE_ACL_ATTR */
445 zvol_replay_err
, /* TX_MKDIR_ACL */
446 zvol_replay_err
, /* TX_MKDIR_ATTR */
447 zvol_replay_err
, /* TX_MKDIR_ACL_ATTR */
448 zvol_replay_err
, /* TX_WRITE2 */
452 zvol_name2minor(const char *name
, minor_t
*minor
)
456 mutex_enter(&zfsdev_state_lock
);
457 zv
= zvol_minor_lookup(name
);
459 *minor
= zv
->zv_minor
;
460 mutex_exit(&zfsdev_state_lock
);
461 return (zv
? 0 : -1);
465 * Create a minor node (plus a whole lot more) for the specified volume.
468 zvol_create_minor(const char *name
)
470 zfs_soft_state_t
*zs
;
473 dmu_object_info_t doi
;
475 char chrbuf
[30], blkbuf
[30];
478 mutex_enter(&zfsdev_state_lock
);
480 if (zvol_minor_lookup(name
) != NULL
) {
481 mutex_exit(&zfsdev_state_lock
);
482 return (SET_ERROR(EEXIST
));
485 /* lie and say we're read-only */
486 error
= dmu_objset_own(name
, DMU_OST_ZVOL
, B_TRUE
, FTAG
, &os
);
489 mutex_exit(&zfsdev_state_lock
);
493 if ((minor
= zfsdev_minor_alloc()) == 0) {
494 dmu_objset_disown(os
, FTAG
);
495 mutex_exit(&zfsdev_state_lock
);
496 return (SET_ERROR(ENXIO
));
499 if (ddi_soft_state_zalloc(zfsdev_state
, minor
) != DDI_SUCCESS
) {
500 dmu_objset_disown(os
, FTAG
);
501 mutex_exit(&zfsdev_state_lock
);
502 return (SET_ERROR(EAGAIN
));
504 (void) ddi_prop_update_string(minor
, zfs_dip
, ZVOL_PROP_NAME
,
507 (void) snprintf(chrbuf
, sizeof (chrbuf
), "%u,raw", minor
);
509 if (ddi_create_minor_node(zfs_dip
, chrbuf
, S_IFCHR
,
510 minor
, DDI_PSEUDO
, 0) == DDI_FAILURE
) {
511 ddi_soft_state_free(zfsdev_state
, minor
);
512 dmu_objset_disown(os
, FTAG
);
513 mutex_exit(&zfsdev_state_lock
);
514 return (SET_ERROR(EAGAIN
));
517 (void) snprintf(blkbuf
, sizeof (blkbuf
), "%u", minor
);
519 if (ddi_create_minor_node(zfs_dip
, blkbuf
, S_IFBLK
,
520 minor
, DDI_PSEUDO
, 0) == DDI_FAILURE
) {
521 ddi_remove_minor_node(zfs_dip
, chrbuf
);
522 ddi_soft_state_free(zfsdev_state
, minor
);
523 dmu_objset_disown(os
, FTAG
);
524 mutex_exit(&zfsdev_state_lock
);
525 return (SET_ERROR(EAGAIN
));
528 zs
= ddi_get_soft_state(zfsdev_state
, minor
);
529 zs
->zss_type
= ZSST_ZVOL
;
530 zv
= zs
->zss_data
= kmem_zalloc(sizeof (zvol_state_t
), KM_SLEEP
);
531 (void) strlcpy(zv
->zv_name
, name
, MAXPATHLEN
);
532 zv
->zv_min_bs
= DEV_BSHIFT
;
533 zv
->zv_minor
= minor
;
535 if (dmu_objset_is_snapshot(os
) || !spa_writeable(dmu_objset_spa(os
)))
536 zv
->zv_flags
|= ZVOL_RDONLY
;
537 mutex_init(&zv
->zv_znode
.z_range_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
538 avl_create(&zv
->zv_znode
.z_range_avl
, zfs_range_compare
,
539 sizeof (rl_t
), offsetof(rl_t
, r_node
));
540 list_create(&zv
->zv_extents
, sizeof (zvol_extent_t
),
541 offsetof(zvol_extent_t
, ze_node
));
542 /* get and cache the blocksize */
543 error
= dmu_object_info(os
, ZVOL_OBJ
, &doi
);
545 zv
->zv_volblocksize
= doi
.doi_data_block_size
;
547 if (spa_writeable(dmu_objset_spa(os
))) {
548 if (zil_replay_disable
)
549 zil_destroy(dmu_objset_zil(os
), B_FALSE
);
551 zil_replay(os
, zv
, zvol_replay_vector
);
553 dmu_objset_disown(os
, FTAG
);
554 zv
->zv_objset
= NULL
;
558 mutex_exit(&zfsdev_state_lock
);
564 * Remove minor node for the specified volume.
567 zvol_remove_zv(zvol_state_t
*zv
)
570 minor_t minor
= zv
->zv_minor
;
572 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
573 if (zv
->zv_total_opens
!= 0)
574 return (SET_ERROR(EBUSY
));
576 (void) snprintf(nmbuf
, sizeof (nmbuf
), "%u,raw", minor
);
577 ddi_remove_minor_node(zfs_dip
, nmbuf
);
579 (void) snprintf(nmbuf
, sizeof (nmbuf
), "%u", minor
);
580 ddi_remove_minor_node(zfs_dip
, nmbuf
);
582 avl_destroy(&zv
->zv_znode
.z_range_avl
);
583 mutex_destroy(&zv
->zv_znode
.z_range_lock
);
585 kmem_free(zv
, sizeof (zvol_state_t
));
587 ddi_soft_state_free(zfsdev_state
, minor
);
594 zvol_remove_minor(const char *name
)
599 mutex_enter(&zfsdev_state_lock
);
600 if ((zv
= zvol_minor_lookup(name
)) == NULL
) {
601 mutex_exit(&zfsdev_state_lock
);
602 return (SET_ERROR(ENXIO
));
604 rc
= zvol_remove_zv(zv
);
605 mutex_exit(&zfsdev_state_lock
);
610 zvol_first_open(zvol_state_t
*zv
)
617 /* lie and say we're read-only */
618 error
= dmu_objset_own(zv
->zv_name
, DMU_OST_ZVOL
, B_TRUE
,
624 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
);
627 dmu_objset_disown(os
, zvol_tag
);
631 error
= dmu_bonus_hold(os
, ZVOL_OBJ
, zvol_tag
, &zv
->zv_dbuf
);
633 dmu_objset_disown(os
, zvol_tag
);
637 zvol_size_changed(zv
, volsize
);
638 zv
->zv_zilog
= zil_open(os
, zvol_get_data
);
640 VERIFY(dsl_prop_get_integer(zv
->zv_name
, "readonly", &readonly
,
642 if (readonly
|| dmu_objset_is_snapshot(os
) ||
643 !spa_writeable(dmu_objset_spa(os
)))
644 zv
->zv_flags
|= ZVOL_RDONLY
;
646 zv
->zv_flags
&= ~ZVOL_RDONLY
;
651 zvol_last_close(zvol_state_t
*zv
)
653 zil_close(zv
->zv_zilog
);
656 dmu_buf_rele(zv
->zv_dbuf
, zvol_tag
);
662 if (dsl_dataset_is_dirty(dmu_objset_ds(zv
->zv_objset
)) &&
663 !(zv
->zv_flags
& ZVOL_RDONLY
))
664 txg_wait_synced(dmu_objset_pool(zv
->zv_objset
), 0);
665 dmu_objset_evict_dbufs(zv
->zv_objset
);
667 dmu_objset_disown(zv
->zv_objset
, zvol_tag
);
668 zv
->zv_objset
= NULL
;
672 zvol_prealloc(zvol_state_t
*zv
)
674 objset_t
*os
= zv
->zv_objset
;
676 uint64_t refd
, avail
, usedobjs
, availobjs
;
677 uint64_t resid
= zv
->zv_volsize
;
680 /* Check the space usage before attempting to allocate the space */
681 dmu_objset_space(os
, &refd
, &avail
, &usedobjs
, &availobjs
);
682 if (avail
< zv
->zv_volsize
)
683 return (SET_ERROR(ENOSPC
));
685 /* Free old extents if they exist */
686 zvol_free_extents(zv
);
690 uint64_t bytes
= MIN(resid
, SPA_MAXBLOCKSIZE
);
692 tx
= dmu_tx_create(os
);
693 dmu_tx_hold_write(tx
, ZVOL_OBJ
, off
, bytes
);
694 error
= dmu_tx_assign(tx
, TXG_WAIT
);
697 (void) dmu_free_long_range(os
, ZVOL_OBJ
, 0, off
);
700 dmu_prealloc(os
, ZVOL_OBJ
, off
, bytes
, tx
);
705 txg_wait_synced(dmu_objset_pool(os
), 0);
711 zvol_update_volsize(objset_t
*os
, uint64_t volsize
)
716 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
718 tx
= dmu_tx_create(os
);
719 dmu_tx_hold_zap(tx
, ZVOL_ZAP_OBJ
, TRUE
, NULL
);
720 error
= dmu_tx_assign(tx
, TXG_WAIT
);
726 error
= zap_update(os
, ZVOL_ZAP_OBJ
, "size", 8, 1,
731 error
= dmu_free_long_range(os
,
732 ZVOL_OBJ
, volsize
, DMU_OBJECT_END
);
737 zvol_remove_minors(const char *name
)
743 namebuf
= kmem_zalloc(strlen(name
) + 2, KM_SLEEP
);
744 (void) strncpy(namebuf
, name
, strlen(name
));
745 (void) strcat(namebuf
, "/");
746 mutex_enter(&zfsdev_state_lock
);
747 for (minor
= 1; minor
<= ZFSDEV_MAX_MINOR
; minor
++) {
749 zv
= zfsdev_get_soft_state(minor
, ZSST_ZVOL
);
752 if (strncmp(namebuf
, zv
->zv_name
, strlen(namebuf
)) == 0)
753 (void) zvol_remove_zv(zv
);
755 kmem_free(namebuf
, strlen(name
) + 2);
757 mutex_exit(&zfsdev_state_lock
);
761 zvol_update_live_volsize(zvol_state_t
*zv
, uint64_t volsize
)
763 uint64_t old_volsize
= 0ULL;
766 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
769 * Reinitialize the dump area to the new size. If we
770 * failed to resize the dump area then restore it back to
771 * its original size. We must set the new volsize prior
772 * to calling dumpvp_resize() to ensure that the devices'
773 * size(9P) is not visible by the dump subsystem.
775 old_volsize
= zv
->zv_volsize
;
776 zvol_size_changed(zv
, volsize
);
778 if (zv
->zv_flags
& ZVOL_DUMPIFIED
) {
779 if ((error
= zvol_dumpify(zv
)) != 0 ||
780 (error
= dumpvp_resize()) != 0) {
783 (void) zvol_update_volsize(zv
->zv_objset
, old_volsize
);
784 zvol_size_changed(zv
, old_volsize
);
785 dumpify_error
= zvol_dumpify(zv
);
786 error
= dumpify_error
? dumpify_error
: error
;
791 * Generate a LUN expansion event.
796 char *physpath
= kmem_zalloc(MAXPATHLEN
, KM_SLEEP
);
798 (void) snprintf(physpath
, MAXPATHLEN
, "%s%u", ZVOL_PSEUDO_DEV
,
801 VERIFY(nvlist_alloc(&attr
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
802 VERIFY(nvlist_add_string(attr
, DEV_PHYS_PATH
, physpath
) == 0);
804 (void) ddi_log_sysevent(zfs_dip
, SUNW_VENDOR
, EC_DEV_STATUS
,
805 ESC_DEV_DLE
, attr
, &eid
, DDI_SLEEP
);
808 kmem_free(physpath
, MAXPATHLEN
);
814 zvol_set_volsize(const char *name
, uint64_t volsize
)
816 zvol_state_t
*zv
= NULL
;
819 dmu_object_info_t doi
;
821 boolean_t owned
= B_FALSE
;
823 error
= dsl_prop_get_integer(name
,
824 zfs_prop_to_name(ZFS_PROP_READONLY
), &readonly
, NULL
);
828 return (SET_ERROR(EROFS
));
830 mutex_enter(&zfsdev_state_lock
);
831 zv
= zvol_minor_lookup(name
);
833 if (zv
== NULL
|| zv
->zv_objset
== NULL
) {
834 if ((error
= dmu_objset_own(name
, DMU_OST_ZVOL
, B_FALSE
,
836 mutex_exit(&zfsdev_state_lock
);
846 if ((error
= dmu_object_info(os
, ZVOL_OBJ
, &doi
)) != 0 ||
847 (error
= zvol_check_volsize(volsize
, doi
.doi_data_block_size
)) != 0)
850 error
= zvol_update_volsize(os
, volsize
);
852 if (error
== 0 && zv
!= NULL
)
853 error
= zvol_update_live_volsize(zv
, volsize
);
856 dmu_objset_disown(os
, FTAG
);
858 zv
->zv_objset
= NULL
;
860 mutex_exit(&zfsdev_state_lock
);
866 zvol_open(dev_t
*devp
, int flag
, int otyp
, cred_t
*cr
)
871 mutex_enter(&zfsdev_state_lock
);
873 zv
= zfsdev_get_soft_state(getminor(*devp
), ZSST_ZVOL
);
875 mutex_exit(&zfsdev_state_lock
);
876 return (SET_ERROR(ENXIO
));
879 if (zv
->zv_total_opens
== 0)
880 err
= zvol_first_open(zv
);
882 mutex_exit(&zfsdev_state_lock
);
885 if ((flag
& FWRITE
) && (zv
->zv_flags
& ZVOL_RDONLY
)) {
886 err
= SET_ERROR(EROFS
);
889 if (zv
->zv_flags
& ZVOL_EXCL
) {
890 err
= SET_ERROR(EBUSY
);
894 if (zv
->zv_total_opens
!= 0) {
895 err
= SET_ERROR(EBUSY
);
898 zv
->zv_flags
|= ZVOL_EXCL
;
901 if (zv
->zv_open_count
[otyp
] == 0 || otyp
== OTYP_LYR
) {
902 zv
->zv_open_count
[otyp
]++;
903 zv
->zv_total_opens
++;
905 mutex_exit(&zfsdev_state_lock
);
909 if (zv
->zv_total_opens
== 0)
911 mutex_exit(&zfsdev_state_lock
);
917 zvol_close(dev_t dev
, int flag
, int otyp
, cred_t
*cr
)
919 minor_t minor
= getminor(dev
);
923 mutex_enter(&zfsdev_state_lock
);
925 zv
= zfsdev_get_soft_state(minor
, ZSST_ZVOL
);
927 mutex_exit(&zfsdev_state_lock
);
928 return (SET_ERROR(ENXIO
));
931 if (zv
->zv_flags
& ZVOL_EXCL
) {
932 ASSERT(zv
->zv_total_opens
== 1);
933 zv
->zv_flags
&= ~ZVOL_EXCL
;
937 * If the open count is zero, this is a spurious close.
938 * That indicates a bug in the kernel / DDI framework.
940 ASSERT(zv
->zv_open_count
[otyp
] != 0);
941 ASSERT(zv
->zv_total_opens
!= 0);
944 * You may get multiple opens, but only one close.
946 zv
->zv_open_count
[otyp
]--;
947 zv
->zv_total_opens
--;
949 if (zv
->zv_total_opens
== 0)
952 mutex_exit(&zfsdev_state_lock
);
957 zvol_get_done(zgd_t
*zgd
, int error
)
960 dmu_buf_rele(zgd
->zgd_db
, zgd
);
962 zfs_range_unlock(zgd
->zgd_rl
);
964 if (error
== 0 && zgd
->zgd_bp
)
965 zil_add_block(zgd
->zgd_zilog
, zgd
->zgd_bp
);
967 kmem_free(zgd
, sizeof (zgd_t
));
971 * Get data to generate a TX_WRITE intent log record.
974 zvol_get_data(void *arg
, lr_write_t
*lr
, char *buf
, zio_t
*zio
)
976 zvol_state_t
*zv
= arg
;
977 objset_t
*os
= zv
->zv_objset
;
978 uint64_t object
= ZVOL_OBJ
;
979 uint64_t offset
= lr
->lr_offset
;
980 uint64_t size
= lr
->lr_length
; /* length of user data */
981 blkptr_t
*bp
= &lr
->lr_blkptr
;
989 zgd
= kmem_zalloc(sizeof (zgd_t
), KM_SLEEP
);
990 zgd
->zgd_zilog
= zv
->zv_zilog
;
991 zgd
->zgd_rl
= zfs_range_lock(&zv
->zv_znode
, offset
, size
, RL_READER
);
994 * Write records come in two flavors: immediate and indirect.
995 * For small writes it's cheaper to store the data with the
996 * log record (immediate); for large writes it's cheaper to
997 * sync the data and get a pointer to it (indirect) so that
998 * we don't have to write the data twice.
1000 if (buf
!= NULL
) { /* immediate write */
1001 error
= dmu_read(os
, object
, offset
, size
, buf
,
1002 DMU_READ_NO_PREFETCH
);
1004 size
= zv
->zv_volblocksize
;
1005 offset
= P2ALIGN(offset
, size
);
1006 error
= dmu_buf_hold(os
, object
, offset
, zgd
, &db
,
1007 DMU_READ_NO_PREFETCH
);
1009 blkptr_t
*obp
= dmu_buf_get_blkptr(db
);
1011 ASSERT(BP_IS_HOLE(bp
));
1018 ASSERT(db
->db_offset
== offset
);
1019 ASSERT(db
->db_size
== size
);
1021 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
1022 zvol_get_done
, zgd
);
1029 zvol_get_done(zgd
, error
);
1035 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
1037 * We store data in the log buffers if it's small enough.
1038 * Otherwise we will later flush the data out via dmu_sync().
1040 ssize_t zvol_immediate_write_sz
= 32768;
1043 zvol_log_write(zvol_state_t
*zv
, dmu_tx_t
*tx
, offset_t off
, ssize_t resid
,
1046 uint32_t blocksize
= zv
->zv_volblocksize
;
1047 zilog_t
*zilog
= zv
->zv_zilog
;
1049 ssize_t immediate_write_sz
;
1051 if (zil_replaying(zilog
, tx
))
1054 immediate_write_sz
= (zilog
->zl_logbias
== ZFS_LOGBIAS_THROUGHPUT
)
1055 ? 0 : zvol_immediate_write_sz
;
1057 slogging
= spa_has_slogs(zilog
->zl_spa
) &&
1058 (zilog
->zl_logbias
== ZFS_LOGBIAS_LATENCY
);
1064 itx_wr_state_t write_state
;
1067 * Unlike zfs_log_write() we can be called with
1068 * upto DMU_MAX_ACCESS/2 (5MB) writes.
1070 if (blocksize
> immediate_write_sz
&& !slogging
&&
1071 resid
>= blocksize
&& off
% blocksize
== 0) {
1072 write_state
= WR_INDIRECT
; /* uses dmu_sync */
1075 write_state
= WR_COPIED
;
1076 len
= MIN(ZIL_MAX_LOG_DATA
, resid
);
1078 write_state
= WR_NEED_COPY
;
1079 len
= MIN(ZIL_MAX_LOG_DATA
, resid
);
1082 itx
= zil_itx_create(TX_WRITE
, sizeof (*lr
) +
1083 (write_state
== WR_COPIED
? len
: 0));
1084 lr
= (lr_write_t
*)&itx
->itx_lr
;
1085 if (write_state
== WR_COPIED
&& dmu_read(zv
->zv_objset
,
1086 ZVOL_OBJ
, off
, len
, lr
+ 1, DMU_READ_NO_PREFETCH
) != 0) {
1087 zil_itx_destroy(itx
);
1088 itx
= zil_itx_create(TX_WRITE
, sizeof (*lr
));
1089 lr
= (lr_write_t
*)&itx
->itx_lr
;
1090 write_state
= WR_NEED_COPY
;
1093 itx
->itx_wr_state
= write_state
;
1094 if (write_state
== WR_NEED_COPY
)
1095 itx
->itx_sod
+= len
;
1096 lr
->lr_foid
= ZVOL_OBJ
;
1097 lr
->lr_offset
= off
;
1098 lr
->lr_length
= len
;
1100 BP_ZERO(&lr
->lr_blkptr
);
1102 itx
->itx_private
= zv
;
1103 itx
->itx_sync
= sync
;
1105 zil_itx_assign(zilog
, itx
, tx
);
1113 zvol_dumpio_vdev(vdev_t
*vd
, void *addr
, uint64_t offset
, uint64_t origoffset
,
1114 uint64_t size
, boolean_t doread
, boolean_t isdump
)
1120 if (vd
->vdev_ops
== &vdev_mirror_ops
||
1121 vd
->vdev_ops
== &vdev_replacing_ops
||
1122 vd
->vdev_ops
== &vdev_spare_ops
) {
1123 for (c
= 0; c
< vd
->vdev_children
; c
++) {
1124 int err
= zvol_dumpio_vdev(vd
->vdev_child
[c
],
1125 addr
, offset
, origoffset
, size
, doread
, isdump
);
1128 } else if (doread
) {
1134 if (!vd
->vdev_ops
->vdev_op_leaf
&& vd
->vdev_ops
!= &vdev_raidz_ops
)
1135 return (numerrors
< vd
->vdev_children
? 0 : EIO
);
1137 if (doread
&& !vdev_readable(vd
))
1138 return (SET_ERROR(EIO
));
1139 else if (!doread
&& !vdev_writeable(vd
))
1140 return (SET_ERROR(EIO
));
1142 if (vd
->vdev_ops
== &vdev_raidz_ops
) {
1143 return (vdev_raidz_physio(vd
,
1144 addr
, size
, offset
, origoffset
, doread
, isdump
));
1147 offset
+= VDEV_LABEL_START_SIZE
;
1149 if (ddi_in_panic() || isdump
) {
1152 return (SET_ERROR(EIO
));
1154 ASSERT3P(dvd
, !=, NULL
);
1155 return (ldi_dump(dvd
->vd_lh
, addr
, lbtodb(offset
),
1159 ASSERT3P(dvd
, !=, NULL
);
1160 return (vdev_disk_ldi_physio(dvd
->vd_lh
, addr
, size
,
1161 offset
, doread
? B_READ
: B_WRITE
));
1166 zvol_dumpio(zvol_state_t
*zv
, void *addr
, uint64_t offset
, uint64_t size
,
1167 boolean_t doread
, boolean_t isdump
)
1172 spa_t
*spa
= dmu_objset_spa(zv
->zv_objset
);
1174 /* Must be sector aligned, and not stradle a block boundary. */
1175 if (P2PHASE(offset
, DEV_BSIZE
) || P2PHASE(size
, DEV_BSIZE
) ||
1176 P2BOUNDARY(offset
, size
, zv
->zv_volblocksize
)) {
1177 return (SET_ERROR(EINVAL
));
1179 ASSERT(size
<= zv
->zv_volblocksize
);
1181 /* Locate the extent this belongs to */
1182 ze
= list_head(&zv
->zv_extents
);
1183 while (offset
>= ze
->ze_nblks
* zv
->zv_volblocksize
) {
1184 offset
-= ze
->ze_nblks
* zv
->zv_volblocksize
;
1185 ze
= list_next(&zv
->zv_extents
, ze
);
1189 return (SET_ERROR(EINVAL
));
1191 if (!ddi_in_panic())
1192 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
1194 vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(&ze
->ze_dva
));
1195 offset
+= DVA_GET_OFFSET(&ze
->ze_dva
);
1196 error
= zvol_dumpio_vdev(vd
, addr
, offset
, DVA_GET_OFFSET(&ze
->ze_dva
),
1197 size
, doread
, isdump
);
1199 if (!ddi_in_panic())
1200 spa_config_exit(spa
, SCL_STATE
, FTAG
);
1206 zvol_strategy(buf_t
*bp
)
1208 zfs_soft_state_t
*zs
= NULL
;
1210 uint64_t off
, volsize
;
1216 boolean_t doread
= bp
->b_flags
& B_READ
;
1217 boolean_t is_dumpified
;
1220 if (getminor(bp
->b_edev
) == 0) {
1221 error
= SET_ERROR(EINVAL
);
1223 zs
= ddi_get_soft_state(zfsdev_state
, getminor(bp
->b_edev
));
1225 error
= SET_ERROR(ENXIO
);
1226 else if (zs
->zss_type
!= ZSST_ZVOL
)
1227 error
= SET_ERROR(EINVAL
);
1231 bioerror(bp
, error
);
1238 if (!(bp
->b_flags
& B_READ
) && (zv
->zv_flags
& ZVOL_RDONLY
)) {
1239 bioerror(bp
, EROFS
);
1244 off
= ldbtob(bp
->b_blkno
);
1245 volsize
= zv
->zv_volsize
;
1251 addr
= bp
->b_un
.b_addr
;
1252 resid
= bp
->b_bcount
;
1254 if (resid
> 0 && (off
< 0 || off
>= volsize
)) {
1260 is_dumpified
= zv
->zv_flags
& ZVOL_DUMPIFIED
;
1261 sync
= ((!(bp
->b_flags
& B_ASYNC
) &&
1262 !(zv
->zv_flags
& ZVOL_WCE
)) ||
1263 (zv
->zv_objset
->os_sync
== ZFS_SYNC_ALWAYS
)) &&
1264 !doread
&& !is_dumpified
;
1267 * There must be no buffer changes when doing a dmu_sync() because
1268 * we can't change the data whilst calculating the checksum.
1270 rl
= zfs_range_lock(&zv
->zv_znode
, off
, resid
,
1271 doread
? RL_READER
: RL_WRITER
);
1273 while (resid
!= 0 && off
< volsize
) {
1274 size_t size
= MIN(resid
, zvol_maxphys
);
1276 size
= MIN(size
, P2END(off
, zv
->zv_volblocksize
) - off
);
1277 error
= zvol_dumpio(zv
, addr
, off
, size
,
1279 } else if (doread
) {
1280 error
= dmu_read(os
, ZVOL_OBJ
, off
, size
, addr
,
1283 dmu_tx_t
*tx
= dmu_tx_create(os
);
1284 dmu_tx_hold_write(tx
, ZVOL_OBJ
, off
, size
);
1285 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1289 dmu_write(os
, ZVOL_OBJ
, off
, size
, addr
, tx
);
1290 zvol_log_write(zv
, tx
, off
, size
, sync
);
1295 /* convert checksum errors into IO errors */
1296 if (error
== ECKSUM
)
1297 error
= SET_ERROR(EIO
);
1304 zfs_range_unlock(rl
);
1306 if ((bp
->b_resid
= resid
) == bp
->b_bcount
)
1307 bioerror(bp
, off
> volsize
? EINVAL
: error
);
1310 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
1317 * Set the buffer count to the zvol maximum transfer.
1318 * Using our own routine instead of the default minphys()
1319 * means that for larger writes we write bigger buffers on X86
1320 * (128K instead of 56K) and flush the disk write cache less often
1321 * (every zvol_maxphys - currently 1MB) instead of minphys (currently
1322 * 56K on X86 and 128K on sparc).
1325 zvol_minphys(struct buf
*bp
)
1327 if (bp
->b_bcount
> zvol_maxphys
)
1328 bp
->b_bcount
= zvol_maxphys
;
1332 zvol_dump(dev_t dev
, caddr_t addr
, daddr_t blkno
, int nblocks
)
1334 minor_t minor
= getminor(dev
);
1341 zv
= zfsdev_get_soft_state(minor
, ZSST_ZVOL
);
1343 return (SET_ERROR(ENXIO
));
1345 if ((zv
->zv_flags
& ZVOL_DUMPIFIED
) == 0)
1346 return (SET_ERROR(EINVAL
));
1348 boff
= ldbtob(blkno
);
1349 resid
= ldbtob(nblocks
);
1351 VERIFY3U(boff
+ resid
, <=, zv
->zv_volsize
);
1354 size
= MIN(resid
, P2END(boff
, zv
->zv_volblocksize
) - boff
);
1355 error
= zvol_dumpio(zv
, addr
, boff
, size
, B_FALSE
, B_TRUE
);
1368 zvol_read(dev_t dev
, uio_t
*uio
, cred_t
*cr
)
1370 minor_t minor
= getminor(dev
);
1376 zv
= zfsdev_get_soft_state(minor
, ZSST_ZVOL
);
1378 return (SET_ERROR(ENXIO
));
1380 volsize
= zv
->zv_volsize
;
1381 if (uio
->uio_resid
> 0 &&
1382 (uio
->uio_loffset
< 0 || uio
->uio_loffset
>= volsize
))
1383 return (SET_ERROR(EIO
));
1385 if (zv
->zv_flags
& ZVOL_DUMPIFIED
) {
1386 error
= physio(zvol_strategy
, NULL
, dev
, B_READ
,
1391 rl
= zfs_range_lock(&zv
->zv_znode
, uio
->uio_loffset
, uio
->uio_resid
,
1393 while (uio
->uio_resid
> 0 && uio
->uio_loffset
< volsize
) {
1394 uint64_t bytes
= MIN(uio
->uio_resid
, DMU_MAX_ACCESS
>> 1);
1396 /* don't read past the end */
1397 if (bytes
> volsize
- uio
->uio_loffset
)
1398 bytes
= volsize
- uio
->uio_loffset
;
1400 error
= dmu_read_uio(zv
->zv_objset
, ZVOL_OBJ
, uio
, bytes
);
1402 /* convert checksum errors into IO errors */
1403 if (error
== ECKSUM
)
1404 error
= SET_ERROR(EIO
);
1408 zfs_range_unlock(rl
);
1414 zvol_write(dev_t dev
, uio_t
*uio
, cred_t
*cr
)
1416 minor_t minor
= getminor(dev
);
1423 zv
= zfsdev_get_soft_state(minor
, ZSST_ZVOL
);
1425 return (SET_ERROR(ENXIO
));
1427 volsize
= zv
->zv_volsize
;
1428 if (uio
->uio_resid
> 0 &&
1429 (uio
->uio_loffset
< 0 || uio
->uio_loffset
>= volsize
))
1430 return (SET_ERROR(EIO
));
1432 if (zv
->zv_flags
& ZVOL_DUMPIFIED
) {
1433 error
= physio(zvol_strategy
, NULL
, dev
, B_WRITE
,
1438 sync
= !(zv
->zv_flags
& ZVOL_WCE
) ||
1439 (zv
->zv_objset
->os_sync
== ZFS_SYNC_ALWAYS
);
1441 rl
= zfs_range_lock(&zv
->zv_znode
, uio
->uio_loffset
, uio
->uio_resid
,
1443 while (uio
->uio_resid
> 0 && uio
->uio_loffset
< volsize
) {
1444 uint64_t bytes
= MIN(uio
->uio_resid
, DMU_MAX_ACCESS
>> 1);
1445 uint64_t off
= uio
->uio_loffset
;
1446 dmu_tx_t
*tx
= dmu_tx_create(zv
->zv_objset
);
1448 if (bytes
> volsize
- off
) /* don't write past the end */
1449 bytes
= volsize
- off
;
1451 dmu_tx_hold_write(tx
, ZVOL_OBJ
, off
, bytes
);
1452 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1457 error
= dmu_write_uio_dbuf(zv
->zv_dbuf
, uio
, bytes
, tx
);
1459 zvol_log_write(zv
, tx
, off
, bytes
, sync
);
1465 zfs_range_unlock(rl
);
1467 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
1472 zvol_getefi(void *arg
, int flag
, uint64_t vs
, uint8_t bs
)
1474 struct uuid uuid
= EFI_RESERVED
;
1475 efi_gpe_t gpe
= { 0 };
1481 if (ddi_copyin(arg
, &efi
, sizeof (dk_efi_t
), flag
))
1482 return (SET_ERROR(EFAULT
));
1483 ptr
= (char *)(uintptr_t)efi
.dki_data_64
;
1484 length
= efi
.dki_length
;
1486 * Some clients may attempt to request a PMBR for the
1487 * zvol. Currently this interface will return EINVAL to
1488 * such requests. These requests could be supported by
1489 * adding a check for lba == 0 and consing up an appropriate
1492 if (efi
.dki_lba
< 1 || efi
.dki_lba
> 2 || length
<= 0)
1493 return (SET_ERROR(EINVAL
));
1495 gpe
.efi_gpe_StartingLBA
= LE_64(34ULL);
1496 gpe
.efi_gpe_EndingLBA
= LE_64((vs
>> bs
) - 1);
1497 UUID_LE_CONVERT(gpe
.efi_gpe_PartitionTypeGUID
, uuid
);
1499 if (efi
.dki_lba
== 1) {
1500 efi_gpt_t gpt
= { 0 };
1502 gpt
.efi_gpt_Signature
= LE_64(EFI_SIGNATURE
);
1503 gpt
.efi_gpt_Revision
= LE_32(EFI_VERSION_CURRENT
);
1504 gpt
.efi_gpt_HeaderSize
= LE_32(sizeof (gpt
));
1505 gpt
.efi_gpt_MyLBA
= LE_64(1ULL);
1506 gpt
.efi_gpt_FirstUsableLBA
= LE_64(34ULL);
1507 gpt
.efi_gpt_LastUsableLBA
= LE_64((vs
>> bs
) - 1);
1508 gpt
.efi_gpt_PartitionEntryLBA
= LE_64(2ULL);
1509 gpt
.efi_gpt_NumberOfPartitionEntries
= LE_32(1);
1510 gpt
.efi_gpt_SizeOfPartitionEntry
=
1511 LE_32(sizeof (efi_gpe_t
));
1512 CRC32(crc
, &gpe
, sizeof (gpe
), -1U, crc32_table
);
1513 gpt
.efi_gpt_PartitionEntryArrayCRC32
= LE_32(~crc
);
1514 CRC32(crc
, &gpt
, sizeof (gpt
), -1U, crc32_table
);
1515 gpt
.efi_gpt_HeaderCRC32
= LE_32(~crc
);
1516 if (ddi_copyout(&gpt
, ptr
, MIN(sizeof (gpt
), length
),
1518 return (SET_ERROR(EFAULT
));
1519 ptr
+= sizeof (gpt
);
1520 length
-= sizeof (gpt
);
1522 if (length
> 0 && ddi_copyout(&gpe
, ptr
, MIN(sizeof (gpe
),
1524 return (SET_ERROR(EFAULT
));
1529 * BEGIN entry points to allow external callers access to the volume.
1532 * Return the volume parameters needed for access from an external caller.
1533 * These values are invariant as long as the volume is held open.
1536 zvol_get_volume_params(minor_t minor
, uint64_t *blksize
,
1537 uint64_t *max_xfer_len
, void **minor_hdl
, void **objset_hdl
, void **zil_hdl
,
1538 void **rl_hdl
, void **bonus_hdl
)
1542 zv
= zfsdev_get_soft_state(minor
, ZSST_ZVOL
);
1544 return (SET_ERROR(ENXIO
));
1545 if (zv
->zv_flags
& ZVOL_DUMPIFIED
)
1546 return (SET_ERROR(ENXIO
));
1548 ASSERT(blksize
&& max_xfer_len
&& minor_hdl
&&
1549 objset_hdl
&& zil_hdl
&& rl_hdl
&& bonus_hdl
);
1551 *blksize
= zv
->zv_volblocksize
;
1552 *max_xfer_len
= (uint64_t)zvol_maxphys
;
1554 *objset_hdl
= zv
->zv_objset
;
1555 *zil_hdl
= zv
->zv_zilog
;
1556 *rl_hdl
= &zv
->zv_znode
;
1557 *bonus_hdl
= zv
->zv_dbuf
;
1562 * Return the current volume size to an external caller.
1563 * The size can change while the volume is open.
1566 zvol_get_volume_size(void *minor_hdl
)
1568 zvol_state_t
*zv
= minor_hdl
;
1570 return (zv
->zv_volsize
);
1574 * Return the current WCE setting to an external caller.
1575 * The WCE setting can change while the volume is open.
1578 zvol_get_volume_wce(void *minor_hdl
)
1580 zvol_state_t
*zv
= minor_hdl
;
1582 return ((zv
->zv_flags
& ZVOL_WCE
) ? 1 : 0);
1586 * Entry point for external callers to zvol_log_write
1589 zvol_log_write_minor(void *minor_hdl
, dmu_tx_t
*tx
, offset_t off
, ssize_t resid
,
1592 zvol_state_t
*zv
= minor_hdl
;
1594 zvol_log_write(zv
, tx
, off
, resid
, sync
);
1597 * END entry points to allow external callers access to the volume.
1601 * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE.
1604 zvol_log_truncate(zvol_state_t
*zv
, dmu_tx_t
*tx
, uint64_t off
, uint64_t len
,
1609 zilog_t
*zilog
= zv
->zv_zilog
;
1611 if (zil_replaying(zilog
, tx
))
1614 itx
= zil_itx_create(TX_TRUNCATE
, sizeof (*lr
));
1615 lr
= (lr_truncate_t
*)&itx
->itx_lr
;
1616 lr
->lr_foid
= ZVOL_OBJ
;
1617 lr
->lr_offset
= off
;
1618 lr
->lr_length
= len
;
1620 itx
->itx_sync
= sync
;
1621 zil_itx_assign(zilog
, itx
, tx
);
1625 * Dirtbag ioctls to support mkfs(1M) for UFS filesystems. See dkio(7I).
1626 * Also a dirtbag dkio ioctl for unmap/free-block functionality.
1630 zvol_ioctl(dev_t dev
, int cmd
, intptr_t arg
, int flag
, cred_t
*cr
, int *rvalp
)
1633 struct dk_callback
*dkc
;
1637 mutex_enter(&zfsdev_state_lock
);
1639 zv
= zfsdev_get_soft_state(getminor(dev
), ZSST_ZVOL
);
1642 mutex_exit(&zfsdev_state_lock
);
1643 return (SET_ERROR(ENXIO
));
1645 ASSERT(zv
->zv_total_opens
> 0);
1651 struct dk_cinfo dki
;
1653 bzero(&dki
, sizeof (dki
));
1654 (void) strcpy(dki
.dki_cname
, "zvol");
1655 (void) strcpy(dki
.dki_dname
, "zvol");
1656 dki
.dki_ctype
= DKC_UNKNOWN
;
1657 dki
.dki_unit
= getminor(dev
);
1658 dki
.dki_maxtransfer
= 1 << (SPA_MAXBLOCKSHIFT
- zv
->zv_min_bs
);
1659 mutex_exit(&zfsdev_state_lock
);
1660 if (ddi_copyout(&dki
, (void *)arg
, sizeof (dki
), flag
))
1661 error
= SET_ERROR(EFAULT
);
1665 case DKIOCGMEDIAINFO
:
1667 struct dk_minfo dkm
;
1669 bzero(&dkm
, sizeof (dkm
));
1670 dkm
.dki_lbsize
= 1U << zv
->zv_min_bs
;
1671 dkm
.dki_capacity
= zv
->zv_volsize
>> zv
->zv_min_bs
;
1672 dkm
.dki_media_type
= DK_UNKNOWN
;
1673 mutex_exit(&zfsdev_state_lock
);
1674 if (ddi_copyout(&dkm
, (void *)arg
, sizeof (dkm
), flag
))
1675 error
= SET_ERROR(EFAULT
);
1679 case DKIOCGMEDIAINFOEXT
:
1681 struct dk_minfo_ext dkmext
;
1683 bzero(&dkmext
, sizeof (dkmext
));
1684 dkmext
.dki_lbsize
= 1U << zv
->zv_min_bs
;
1685 dkmext
.dki_pbsize
= zv
->zv_volblocksize
;
1686 dkmext
.dki_capacity
= zv
->zv_volsize
>> zv
->zv_min_bs
;
1687 dkmext
.dki_media_type
= DK_UNKNOWN
;
1688 mutex_exit(&zfsdev_state_lock
);
1689 if (ddi_copyout(&dkmext
, (void *)arg
, sizeof (dkmext
), flag
))
1690 error
= SET_ERROR(EFAULT
);
1696 uint64_t vs
= zv
->zv_volsize
;
1697 uint8_t bs
= zv
->zv_min_bs
;
1699 mutex_exit(&zfsdev_state_lock
);
1700 error
= zvol_getefi((void *)arg
, flag
, vs
, bs
);
1704 case DKIOCFLUSHWRITECACHE
:
1705 dkc
= (struct dk_callback
*)arg
;
1706 mutex_exit(&zfsdev_state_lock
);
1707 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
1708 if ((flag
& FKIOCTL
) && dkc
!= NULL
&& dkc
->dkc_callback
) {
1709 (*dkc
->dkc_callback
)(dkc
->dkc_cookie
, error
);
1716 int wce
= (zv
->zv_flags
& ZVOL_WCE
) ? 1 : 0;
1717 if (ddi_copyout(&wce
, (void *)arg
, sizeof (int),
1719 error
= SET_ERROR(EFAULT
);
1725 if (ddi_copyin((void *)arg
, &wce
, sizeof (int),
1727 error
= SET_ERROR(EFAULT
);
1731 zv
->zv_flags
|= ZVOL_WCE
;
1732 mutex_exit(&zfsdev_state_lock
);
1734 zv
->zv_flags
&= ~ZVOL_WCE
;
1735 mutex_exit(&zfsdev_state_lock
);
1736 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
1744 * commands using these (like prtvtoc) expect ENOTSUP
1745 * since we're emulating an EFI label
1747 error
= SET_ERROR(ENOTSUP
);
1751 rl
= zfs_range_lock(&zv
->zv_znode
, 0, zv
->zv_volsize
,
1753 error
= zvol_dumpify(zv
);
1754 zfs_range_unlock(rl
);
1758 if (!(zv
->zv_flags
& ZVOL_DUMPIFIED
))
1760 rl
= zfs_range_lock(&zv
->zv_znode
, 0, zv
->zv_volsize
,
1762 error
= zvol_dump_fini(zv
);
1763 zfs_range_unlock(rl
);
1771 if (ddi_copyin((void *)arg
, &df
, sizeof (df
), flag
)) {
1772 error
= SET_ERROR(EFAULT
);
1777 * Apply Postel's Law to length-checking. If they overshoot,
1778 * just blank out until the end, if there's a need to blank
1781 if (df
.df_start
>= zv
->zv_volsize
)
1782 break; /* No need to do anything... */
1783 if (df
.df_start
+ df
.df_length
> zv
->zv_volsize
)
1784 df
.df_length
= DMU_OBJECT_END
;
1786 rl
= zfs_range_lock(&zv
->zv_znode
, df
.df_start
, df
.df_length
,
1788 tx
= dmu_tx_create(zv
->zv_objset
);
1789 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1793 zvol_log_truncate(zv
, tx
, df
.df_start
,
1794 df
.df_length
, B_TRUE
);
1796 error
= dmu_free_long_range(zv
->zv_objset
, ZVOL_OBJ
,
1797 df
.df_start
, df
.df_length
);
1800 zfs_range_unlock(rl
);
1804 * If the write-cache is disabled or 'sync' property
1805 * is set to 'always' then treat this as a synchronous
1806 * operation (i.e. commit to zil).
1808 if (!(zv
->zv_flags
& ZVOL_WCE
) ||
1809 (zv
->zv_objset
->os_sync
== ZFS_SYNC_ALWAYS
))
1810 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
1813 * If the caller really wants synchronous writes, and
1814 * can't wait for them, don't return until the write
1817 if (df
.df_flags
& DF_WAIT_SYNC
) {
1819 dmu_objset_pool(zv
->zv_objset
), 0);
1826 error
= SET_ERROR(ENOTTY
);
1830 mutex_exit(&zfsdev_state_lock
);
1837 return (zvol_minors
!= 0);
1843 VERIFY(ddi_soft_state_init(&zfsdev_state
, sizeof (zfs_soft_state_t
),
1845 mutex_init(&zfsdev_state_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1851 mutex_destroy(&zfsdev_state_lock
);
1852 ddi_soft_state_fini(&zfsdev_state
);
1857 zfs_mvdev_dump_feature_check(void *arg
, dmu_tx_t
*tx
)
1859 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
1861 if (spa_feature_is_active(spa
, SPA_FEATURE_MULTI_VDEV_CRASH_DUMP
))
1868 zfs_mvdev_dump_activate_feature_sync(void *arg
, dmu_tx_t
*tx
)
1870 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
1872 spa_feature_incr(spa
, SPA_FEATURE_MULTI_VDEV_CRASH_DUMP
, tx
);
1876 zvol_dump_init(zvol_state_t
*zv
, boolean_t resize
)
1880 objset_t
*os
= zv
->zv_objset
;
1881 spa_t
*spa
= dmu_objset_spa(os
);
1882 vdev_t
*vd
= spa
->spa_root_vdev
;
1883 nvlist_t
*nv
= NULL
;
1884 uint64_t version
= spa_version(spa
);
1885 enum zio_checksum checksum
;
1887 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
1888 ASSERT(vd
->vdev_ops
== &vdev_root_ops
);
1890 error
= dmu_free_long_range(zv
->zv_objset
, ZVOL_OBJ
, 0,
1892 /* wait for dmu_free_long_range to actually free the blocks */
1893 txg_wait_synced(dmu_objset_pool(zv
->zv_objset
), 0);
1896 * If the pool on which the dump device is being initialized has more
1897 * than one child vdev, check that the MULTI_VDEV_CRASH_DUMP feature is
1898 * enabled. If so, bump that feature's counter to indicate that the
1899 * feature is active. We also check the vdev type to handle the
1901 * # zpool create test raidz disk1 disk2 disk3
1902 * Now have spa_root_vdev->vdev_children == 1 (the raidz vdev),
1903 * the raidz vdev itself has 3 children.
1905 if (vd
->vdev_children
> 1 || vd
->vdev_ops
== &vdev_raidz_ops
) {
1906 if (!spa_feature_is_enabled(spa
,
1907 SPA_FEATURE_MULTI_VDEV_CRASH_DUMP
))
1908 return (SET_ERROR(ENOTSUP
));
1909 (void) dsl_sync_task(spa_name(spa
),
1910 zfs_mvdev_dump_feature_check
,
1911 zfs_mvdev_dump_activate_feature_sync
, NULL
, 2);
1914 tx
= dmu_tx_create(os
);
1915 dmu_tx_hold_zap(tx
, ZVOL_ZAP_OBJ
, TRUE
, NULL
);
1916 dmu_tx_hold_bonus(tx
, ZVOL_OBJ
);
1917 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1924 * If MULTI_VDEV_CRASH_DUMP is active, use the NOPARITY checksum
1925 * function. Otherwise, use the old default -- OFF.
1927 checksum
= spa_feature_is_active(spa
,
1928 SPA_FEATURE_MULTI_VDEV_CRASH_DUMP
) ? ZIO_CHECKSUM_NOPARITY
:
1932 * If we are resizing the dump device then we only need to
1933 * update the refreservation to match the newly updated
1934 * zvolsize. Otherwise, we save off the original state of the
1935 * zvol so that we can restore them if the zvol is ever undumpified.
1938 error
= zap_update(os
, ZVOL_ZAP_OBJ
,
1939 zfs_prop_to_name(ZFS_PROP_REFRESERVATION
), 8, 1,
1940 &zv
->zv_volsize
, tx
);
1942 uint64_t checksum
, compress
, refresrv
, vbs
, dedup
;
1944 error
= dsl_prop_get_integer(zv
->zv_name
,
1945 zfs_prop_to_name(ZFS_PROP_COMPRESSION
), &compress
, NULL
);
1946 error
= error
? error
: dsl_prop_get_integer(zv
->zv_name
,
1947 zfs_prop_to_name(ZFS_PROP_CHECKSUM
), &checksum
, NULL
);
1948 error
= error
? error
: dsl_prop_get_integer(zv
->zv_name
,
1949 zfs_prop_to_name(ZFS_PROP_REFRESERVATION
), &refresrv
, NULL
);
1950 error
= error
? error
: dsl_prop_get_integer(zv
->zv_name
,
1951 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
), &vbs
, NULL
);
1952 if (version
>= SPA_VERSION_DEDUP
) {
1953 error
= error
? error
:
1954 dsl_prop_get_integer(zv
->zv_name
,
1955 zfs_prop_to_name(ZFS_PROP_DEDUP
), &dedup
, NULL
);
1958 error
= error
? error
: zap_update(os
, ZVOL_ZAP_OBJ
,
1959 zfs_prop_to_name(ZFS_PROP_COMPRESSION
), 8, 1,
1961 error
= error
? error
: zap_update(os
, ZVOL_ZAP_OBJ
,
1962 zfs_prop_to_name(ZFS_PROP_CHECKSUM
), 8, 1, &checksum
, tx
);
1963 error
= error
? error
: zap_update(os
, ZVOL_ZAP_OBJ
,
1964 zfs_prop_to_name(ZFS_PROP_REFRESERVATION
), 8, 1,
1966 error
= error
? error
: zap_update(os
, ZVOL_ZAP_OBJ
,
1967 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
), 8, 1,
1969 error
= error
? error
: dmu_object_set_blocksize(
1970 os
, ZVOL_OBJ
, SPA_MAXBLOCKSIZE
, 0, tx
);
1971 if (version
>= SPA_VERSION_DEDUP
) {
1972 error
= error
? error
: zap_update(os
, ZVOL_ZAP_OBJ
,
1973 zfs_prop_to_name(ZFS_PROP_DEDUP
), 8, 1,
1977 zv
->zv_volblocksize
= SPA_MAXBLOCKSIZE
;
1982 * We only need update the zvol's property if we are initializing
1983 * the dump area for the first time.
1986 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1987 VERIFY(nvlist_add_uint64(nv
,
1988 zfs_prop_to_name(ZFS_PROP_REFRESERVATION
), 0) == 0);
1989 VERIFY(nvlist_add_uint64(nv
,
1990 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
1991 ZIO_COMPRESS_OFF
) == 0);
1992 VERIFY(nvlist_add_uint64(nv
,
1993 zfs_prop_to_name(ZFS_PROP_CHECKSUM
),
1995 if (version
>= SPA_VERSION_DEDUP
) {
1996 VERIFY(nvlist_add_uint64(nv
,
1997 zfs_prop_to_name(ZFS_PROP_DEDUP
),
1998 ZIO_CHECKSUM_OFF
) == 0);
2001 error
= zfs_set_prop_nvlist(zv
->zv_name
, ZPROP_SRC_LOCAL
,
2009 /* Allocate the space for the dump */
2010 error
= zvol_prealloc(zv
);
2015 zvol_dumpify(zvol_state_t
*zv
)
2018 uint64_t dumpsize
= 0;
2020 objset_t
*os
= zv
->zv_objset
;
2022 if (zv
->zv_flags
& ZVOL_RDONLY
)
2023 return (SET_ERROR(EROFS
));
2025 if (zap_lookup(zv
->zv_objset
, ZVOL_ZAP_OBJ
, ZVOL_DUMPSIZE
,
2026 8, 1, &dumpsize
) != 0 || dumpsize
!= zv
->zv_volsize
) {
2027 boolean_t resize
= (dumpsize
> 0);
2029 if ((error
= zvol_dump_init(zv
, resize
)) != 0) {
2030 (void) zvol_dump_fini(zv
);
2036 * Build up our lba mapping.
2038 error
= zvol_get_lbas(zv
);
2040 (void) zvol_dump_fini(zv
);
2044 tx
= dmu_tx_create(os
);
2045 dmu_tx_hold_zap(tx
, ZVOL_ZAP_OBJ
, TRUE
, NULL
);
2046 error
= dmu_tx_assign(tx
, TXG_WAIT
);
2049 (void) zvol_dump_fini(zv
);
2053 zv
->zv_flags
|= ZVOL_DUMPIFIED
;
2054 error
= zap_update(os
, ZVOL_ZAP_OBJ
, ZVOL_DUMPSIZE
, 8, 1,
2055 &zv
->zv_volsize
, tx
);
2059 (void) zvol_dump_fini(zv
);
2063 txg_wait_synced(dmu_objset_pool(os
), 0);
2068 zvol_dump_fini(zvol_state_t
*zv
)
2071 objset_t
*os
= zv
->zv_objset
;
2074 uint64_t checksum
, compress
, refresrv
, vbs
, dedup
;
2075 uint64_t version
= spa_version(dmu_objset_spa(zv
->zv_objset
));
2078 * Attempt to restore the zvol back to its pre-dumpified state.
2079 * This is a best-effort attempt as it's possible that not all
2080 * of these properties were initialized during the dumpify process
2081 * (i.e. error during zvol_dump_init).
2084 tx
= dmu_tx_create(os
);
2085 dmu_tx_hold_zap(tx
, ZVOL_ZAP_OBJ
, TRUE
, NULL
);
2086 error
= dmu_tx_assign(tx
, TXG_WAIT
);
2091 (void) zap_remove(os
, ZVOL_ZAP_OBJ
, ZVOL_DUMPSIZE
, tx
);
2094 (void) zap_lookup(zv
->zv_objset
, ZVOL_ZAP_OBJ
,
2095 zfs_prop_to_name(ZFS_PROP_CHECKSUM
), 8, 1, &checksum
);
2096 (void) zap_lookup(zv
->zv_objset
, ZVOL_ZAP_OBJ
,
2097 zfs_prop_to_name(ZFS_PROP_COMPRESSION
), 8, 1, &compress
);
2098 (void) zap_lookup(zv
->zv_objset
, ZVOL_ZAP_OBJ
,
2099 zfs_prop_to_name(ZFS_PROP_REFRESERVATION
), 8, 1, &refresrv
);
2100 (void) zap_lookup(zv
->zv_objset
, ZVOL_ZAP_OBJ
,
2101 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
), 8, 1, &vbs
);
2103 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2104 (void) nvlist_add_uint64(nv
,
2105 zfs_prop_to_name(ZFS_PROP_CHECKSUM
), checksum
);
2106 (void) nvlist_add_uint64(nv
,
2107 zfs_prop_to_name(ZFS_PROP_COMPRESSION
), compress
);
2108 (void) nvlist_add_uint64(nv
,
2109 zfs_prop_to_name(ZFS_PROP_REFRESERVATION
), refresrv
);
2110 if (version
>= SPA_VERSION_DEDUP
&&
2111 zap_lookup(zv
->zv_objset
, ZVOL_ZAP_OBJ
,
2112 zfs_prop_to_name(ZFS_PROP_DEDUP
), 8, 1, &dedup
) == 0) {
2113 (void) nvlist_add_uint64(nv
,
2114 zfs_prop_to_name(ZFS_PROP_DEDUP
), dedup
);
2116 (void) zfs_set_prop_nvlist(zv
->zv_name
, ZPROP_SRC_LOCAL
,
2120 zvol_free_extents(zv
);
2121 zv
->zv_flags
&= ~ZVOL_DUMPIFIED
;
2122 (void) dmu_free_long_range(os
, ZVOL_OBJ
, 0, DMU_OBJECT_END
);
2123 /* wait for dmu_free_long_range to actually free the blocks */
2124 txg_wait_synced(dmu_objset_pool(zv
->zv_objset
), 0);
2125 tx
= dmu_tx_create(os
);
2126 dmu_tx_hold_bonus(tx
, ZVOL_OBJ
);
2127 error
= dmu_tx_assign(tx
, TXG_WAIT
);
2132 if (dmu_object_set_blocksize(os
, ZVOL_OBJ
, vbs
, 0, tx
) == 0)
2133 zv
->zv_volblocksize
= vbs
;