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 * Copyright (c) 2006-2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
25 * All rights reserved.
27 * Portions Copyright 2010 Robert Milkowski
29 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
30 * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
31 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
32 * Copyright (c) 2014 Integros [integros.com]
35 /* Portions Copyright 2011 Martin Matuska <mm@FreeBSD.org> */
38 * ZFS volume emulation driver.
40 * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes.
41 * Volumes are accessed through the symbolic links named:
43 * /dev/zvol/<pool_name>/<dataset_name>
45 * Volumes are persistent through reboot. No user command needs to be
46 * run before opening and using a device.
48 * On FreeBSD ZVOLs are simply GEOM providers like any other storage device
49 * in the system. Except when they're simply character devices (volmode=dev).
52 #include <sys/types.h>
53 #include <sys/param.h>
54 #include <sys/kernel.h>
55 #include <sys/errno.h>
61 #include <sys/cmn_err.h>
66 #include <sys/spa_impl.h>
69 #include <sys/dmu_traverse.h>
70 #include <sys/dnode.h>
71 #include <sys/dsl_dataset.h>
72 #include <sys/dsl_prop.h>
73 #include <sys/dsl_dir.h>
74 #include <sys/byteorder.h>
75 #include <sys/sunddi.h>
76 #include <sys/dirent.h>
77 #include <sys/policy.h>
78 #include <sys/queue.h>
79 #include <sys/fs/zfs.h>
80 #include <sys/zfs_ioctl.h>
82 #include <sys/zfs_znode.h>
83 #include <sys/zfs_rlock.h>
84 #include <sys/vdev_impl.h>
85 #include <sys/vdev_raidz.h>
87 #include <sys/zil_impl.h>
88 #include <sys/dataset_kstats.h>
90 #include <sys/dmu_tx.h>
91 #include <sys/zfeature.h>
92 #include <sys/zio_checksum.h>
93 #include <sys/zil_impl.h>
94 #include <sys/filio.h>
96 #include <geom/geom.h>
98 #include <sys/zvol_impl.h>
100 #include "zfs_namecheck.h"
102 #define ZVOL_DUMPSIZE "dumpsize"
104 #ifdef ZVOL_LOCK_DEBUG
105 #define ZVOL_RW_READER RW_WRITER
106 #define ZVOL_RW_READ_HELD RW_WRITE_HELD
108 #define ZVOL_RW_READER RW_READER
109 #define ZVOL_RW_READ_HELD RW_READ_HELD
112 enum zvol_geom_state {
118 struct zvol_state_os {
120 #define zso_dev _zso_state._zso_dev
121 #define zso_geom _zso_state._zso_geom
124 struct zvol_state_dev {
125 struct cdev *zsd_cdev;
126 uint64_t zsd_sync_cnt;
130 struct zvol_state_geom {
131 struct g_provider *zsg_provider;
132 struct bio_queue_head zsg_queue;
133 struct mtx zsg_queue_mtx;
134 enum zvol_geom_state zsg_state;
139 static uint32_t zvol_minors;
141 SYSCTL_DECL(_vfs_zfs);
142 SYSCTL_NODE(_vfs_zfs, OID_AUTO, vol, CTLFLAG_RW, 0, "ZFS VOLUME");
143 SYSCTL_INT(_vfs_zfs_vol, OID_AUTO, mode, CTLFLAG_RWTUN, &zvol_volmode, 0,
144 "Expose as GEOM providers (1), device files (2) or neither");
145 static boolean_t zpool_on_zvol = B_FALSE;
146 SYSCTL_INT(_vfs_zfs_vol, OID_AUTO, recursive, CTLFLAG_RWTUN, &zpool_on_zvol, 0,
147 "Allow zpools to use zvols as vdevs (DANGEROUS)");
150 * Toggle unmap functionality.
152 boolean_t zvol_unmap_enabled = B_TRUE;
154 SYSCTL_INT(_vfs_zfs_vol, OID_AUTO, unmap_enabled, CTLFLAG_RWTUN,
155 &zvol_unmap_enabled, 0, "Enable UNMAP functionality");
158 * zvol maximum transfer in one DMU tx.
160 int zvol_maxphys = DMU_MAX_ACCESS / 2;
162 static void zvol_ensure_zilog(zvol_state_t *zv);
164 static d_open_t zvol_cdev_open;
165 static d_close_t zvol_cdev_close;
166 static d_ioctl_t zvol_cdev_ioctl;
167 static d_read_t zvol_cdev_read;
168 static d_write_t zvol_cdev_write;
169 static d_strategy_t zvol_geom_bio_strategy;
171 static struct cdevsw zvol_cdevsw = {
173 .d_version = D_VERSION,
174 .d_flags = D_DISK | D_TRACKCLOSE,
175 .d_open = zvol_cdev_open,
176 .d_close = zvol_cdev_close,
177 .d_ioctl = zvol_cdev_ioctl,
178 .d_read = zvol_cdev_read,
179 .d_write = zvol_cdev_write,
180 .d_strategy = zvol_geom_bio_strategy,
183 extern uint_t zfs_geom_probe_vdev_key;
185 struct g_class zfs_zvol_class = {
187 .version = G_VERSION,
190 DECLARE_GEOM_CLASS(zfs_zvol_class, zfs_zvol);
192 static int zvol_geom_open(struct g_provider *pp, int flag, int count);
193 static int zvol_geom_close(struct g_provider *pp, int flag, int count);
194 static void zvol_geom_run(zvol_state_t *zv);
195 static void zvol_geom_destroy(zvol_state_t *zv);
196 static int zvol_geom_access(struct g_provider *pp, int acr, int acw, int ace);
197 static void zvol_geom_worker(void *arg);
198 static void zvol_geom_bio_start(struct bio *bp);
199 static int zvol_geom_bio_getattr(struct bio *bp);
200 /* static d_strategy_t zvol_geom_bio_strategy; (declared elsewhere) */
203 * GEOM mode implementation
208 zvol_geom_open(struct g_provider *pp, int flag, int count)
212 boolean_t drop_suspend = B_TRUE;
213 boolean_t drop_namespace = B_FALSE;
215 if (!zpool_on_zvol && tsd_get(zfs_geom_probe_vdev_key) != NULL) {
217 * if zfs_geom_probe_vdev_key is set, that means that zfs is
218 * attempting to probe geom providers while looking for a
219 * replacement for a missing VDEV. In this case, the
220 * spa_namespace_lock will not be held, but it is still illegal
221 * to use a zvol as a vdev. Deadlocks can result if another
222 * thread has spa_namespace_lock
224 return (SET_ERROR(EOPNOTSUPP));
228 rw_enter(&zvol_state_lock, ZVOL_RW_READER);
232 mutex_exit(&spa_namespace_lock);
233 rw_exit(&zvol_state_lock);
234 return (SET_ERROR(ENXIO));
237 if (zv->zv_open_count == 0 && !mutex_owned(&spa_namespace_lock)) {
239 * We need to guarantee that the namespace lock is held
240 * to avoid spurious failures in zvol_first_open
242 drop_namespace = B_TRUE;
243 if (!mutex_tryenter(&spa_namespace_lock)) {
244 rw_exit(&zvol_state_lock);
245 mutex_enter(&spa_namespace_lock);
249 mutex_enter(&zv->zv_state_lock);
251 ASSERT(zv->zv_zso->zso_volmode == ZFS_VOLMODE_GEOM);
254 * make sure zvol is not suspended during first open
255 * (hold zv_suspend_lock) and respect proper lock acquisition
256 * ordering - zv_suspend_lock before zv_state_lock
258 if (zv->zv_open_count == 0) {
259 if (!rw_tryenter(&zv->zv_suspend_lock, ZVOL_RW_READER)) {
260 mutex_exit(&zv->zv_state_lock);
261 rw_enter(&zv->zv_suspend_lock, ZVOL_RW_READER);
262 mutex_enter(&zv->zv_state_lock);
263 /* check to see if zv_suspend_lock is needed */
264 if (zv->zv_open_count != 0) {
265 rw_exit(&zv->zv_suspend_lock);
266 drop_suspend = B_FALSE;
270 drop_suspend = B_FALSE;
272 rw_exit(&zvol_state_lock);
274 ASSERT(MUTEX_HELD(&zv->zv_state_lock));
276 if (zv->zv_open_count == 0) {
277 ASSERT(ZVOL_RW_READ_HELD(&zv->zv_suspend_lock));
278 err = zvol_first_open(zv, !(flag & FWRITE));
281 pp->mediasize = zv->zv_volsize;
282 pp->stripeoffset = 0;
283 pp->stripesize = zv->zv_volblocksize;
287 * Check for a bad on-disk format version now since we
288 * lied about owning the dataset readonly before.
290 if ((flag & FWRITE) && ((zv->zv_flags & ZVOL_RDONLY) ||
291 dmu_objset_incompatible_encryption_version(zv->zv_objset))) {
295 if (zv->zv_flags & ZVOL_EXCL) {
301 if (zv->zv_open_count != 0) {
305 zv->zv_flags |= ZVOL_EXCL;
309 zv->zv_open_count += count;
311 mutex_exit(&spa_namespace_lock);
312 mutex_exit(&zv->zv_state_lock);
314 rw_exit(&zv->zv_suspend_lock);
318 if (zv->zv_open_count == 0)
322 mutex_exit(&spa_namespace_lock);
323 mutex_exit(&zv->zv_state_lock);
325 rw_exit(&zv->zv_suspend_lock);
326 return (SET_ERROR(err));
331 zvol_geom_close(struct g_provider *pp, int flag, int count)
334 boolean_t drop_suspend = B_TRUE;
336 rw_enter(&zvol_state_lock, ZVOL_RW_READER);
339 rw_exit(&zvol_state_lock);
340 return (SET_ERROR(ENXIO));
343 mutex_enter(&zv->zv_state_lock);
344 if (zv->zv_flags & ZVOL_EXCL) {
345 ASSERT(zv->zv_open_count == 1);
346 zv->zv_flags &= ~ZVOL_EXCL;
349 ASSERT(zv->zv_zso->zso_volmode == ZFS_VOLMODE_GEOM);
352 * If the open count is zero, this is a spurious close.
353 * That indicates a bug in the kernel / DDI framework.
355 ASSERT(zv->zv_open_count > 0);
358 * make sure zvol is not suspended during last close
359 * (hold zv_suspend_lock) and respect proper lock acquisition
360 * ordering - zv_suspend_lock before zv_state_lock
362 if ((zv->zv_open_count - count) == 0) {
363 if (!rw_tryenter(&zv->zv_suspend_lock, ZVOL_RW_READER)) {
364 mutex_exit(&zv->zv_state_lock);
365 rw_enter(&zv->zv_suspend_lock, ZVOL_RW_READER);
366 mutex_enter(&zv->zv_state_lock);
367 /* check to see if zv_suspend_lock is needed */
368 if (zv->zv_open_count != 1) {
369 rw_exit(&zv->zv_suspend_lock);
370 drop_suspend = B_FALSE;
374 drop_suspend = B_FALSE;
376 rw_exit(&zvol_state_lock);
378 ASSERT(MUTEX_HELD(&zv->zv_state_lock));
381 * You may get multiple opens, but only one close.
383 zv->zv_open_count -= count;
385 if (zv->zv_open_count == 0) {
386 ASSERT(ZVOL_RW_READ_HELD(&zv->zv_suspend_lock));
390 mutex_exit(&zv->zv_state_lock);
393 rw_exit(&zv->zv_suspend_lock);
398 zvol_geom_run(zvol_state_t *zv)
400 struct zvol_state_geom *zsg = &zv->zv_zso->zso_geom;
401 struct g_provider *pp = zsg->zsg_provider;
403 ASSERT(zv->zv_zso->zso_volmode == ZFS_VOLMODE_GEOM);
405 g_error_provider(pp, 0);
407 kproc_kthread_add(zvol_geom_worker, zv, &system_proc, NULL, 0, 0,
408 "zfskern", "zvol %s", pp->name + sizeof (ZVOL_DRIVER));
412 zvol_geom_destroy(zvol_state_t *zv)
414 struct zvol_state_geom *zsg = &zv->zv_zso->zso_geom;
415 struct g_provider *pp = zsg->zsg_provider;
417 ASSERT(zv->zv_zso->zso_volmode == ZFS_VOLMODE_GEOM);
421 mutex_enter(&zv->zv_state_lock);
422 VERIFY(zsg->zsg_state == ZVOL_GEOM_RUNNING);
423 mutex_exit(&zv->zv_state_lock);
424 zsg->zsg_provider = NULL;
426 g_wither_geom(pp->geom, ENXIO);
430 zvol_geom_access(struct g_provider *pp, int acr, int acw, int ace)
432 int count, error, flags;
437 * To make it easier we expect either open or close, but not both
440 KASSERT((acr >= 0 && acw >= 0 && ace >= 0) ||
441 (acr <= 0 && acw <= 0 && ace <= 0),
442 ("Unsupported access request to %s (acr=%d, acw=%d, ace=%d).",
443 pp->name, acr, acw, ace));
445 if (pp->private == NULL) {
446 if (acr <= 0 && acw <= 0 && ace <= 0)
452 * We don't pass FEXCL flag to zvol_geom_open()/zvol_geom_close() if
453 * ace != 0, because GEOM already handles that and handles it a bit
454 * differently. GEOM allows for multiple read/exclusive consumers and
455 * ZFS allows only one exclusive consumer, no matter if it is reader or
456 * writer. I like better the way GEOM works so I'll leave it for GEOM
457 * to decide what to do.
460 count = acr + acw + ace;
465 if (acr != 0 || ace != 0)
472 error = zvol_geom_open(pp, flags, count);
474 error = zvol_geom_close(pp, flags, -count);
480 zvol_geom_worker(void *arg)
482 zvol_state_t *zv = arg;
483 struct zvol_state_geom *zsg = &zv->zv_zso->zso_geom;
486 ASSERT(zv->zv_zso->zso_volmode == ZFS_VOLMODE_GEOM);
488 thread_lock(curthread);
489 sched_prio(curthread, PRIBIO);
490 thread_unlock(curthread);
493 mtx_lock(&zsg->zsg_queue_mtx);
494 bp = bioq_takefirst(&zsg->zsg_queue);
496 if (zsg->zsg_state == ZVOL_GEOM_STOPPED) {
497 zsg->zsg_state = ZVOL_GEOM_RUNNING;
498 wakeup(&zsg->zsg_state);
499 mtx_unlock(&zsg->zsg_queue_mtx);
502 msleep(&zsg->zsg_queue, &zsg->zsg_queue_mtx,
503 PRIBIO | PDROP, "zvol:io", 0);
506 mtx_unlock(&zsg->zsg_queue_mtx);
507 zvol_geom_bio_strategy(bp);
512 zvol_geom_bio_start(struct bio *bp)
514 zvol_state_t *zv = bp->bio_to->private;
515 struct zvol_state_geom *zsg = &zv->zv_zso->zso_geom;
518 if (bp->bio_cmd == BIO_GETATTR) {
519 if (zvol_geom_bio_getattr(bp))
520 g_io_deliver(bp, EOPNOTSUPP);
524 if (!THREAD_CAN_SLEEP()) {
525 mtx_lock(&zsg->zsg_queue_mtx);
526 first = (bioq_first(&zsg->zsg_queue) == NULL);
527 bioq_insert_tail(&zsg->zsg_queue, bp);
528 mtx_unlock(&zsg->zsg_queue_mtx);
530 wakeup_one(&zsg->zsg_queue);
534 zvol_geom_bio_strategy(bp);
538 zvol_geom_bio_getattr(struct bio *bp)
542 zv = bp->bio_to->private;
545 spa_t *spa = dmu_objset_spa(zv->zv_objset);
546 uint64_t refd, avail, usedobjs, availobjs;
548 if (g_handleattr_int(bp, "GEOM::candelete", 1))
550 if (strcmp(bp->bio_attribute, "blocksavail") == 0) {
551 dmu_objset_space(zv->zv_objset, &refd, &avail,
552 &usedobjs, &availobjs);
553 if (g_handleattr_off_t(bp, "blocksavail", avail / DEV_BSIZE))
555 } else if (strcmp(bp->bio_attribute, "blocksused") == 0) {
556 dmu_objset_space(zv->zv_objset, &refd, &avail,
557 &usedobjs, &availobjs);
558 if (g_handleattr_off_t(bp, "blocksused", refd / DEV_BSIZE))
560 } else if (strcmp(bp->bio_attribute, "poolblocksavail") == 0) {
561 avail = metaslab_class_get_space(spa_normal_class(spa));
562 avail -= metaslab_class_get_alloc(spa_normal_class(spa));
563 if (g_handleattr_off_t(bp, "poolblocksavail",
566 } else if (strcmp(bp->bio_attribute, "poolblocksused") == 0) {
567 refd = metaslab_class_get_alloc(spa_normal_class(spa));
568 if (g_handleattr_off_t(bp, "poolblocksused", refd / DEV_BSIZE))
575 zvol_geom_bio_strategy(struct bio *bp)
578 uint64_t off, volsize;
582 zfs_locked_range_t *lr;
584 boolean_t doread = B_FALSE;
585 boolean_t is_dumpified;
589 zv = bp->bio_to->private;
591 zv = bp->bio_dev->si_drv2;
594 error = SET_ERROR(ENXIO);
598 rw_enter(&zv->zv_suspend_lock, ZVOL_RW_READER);
600 switch (bp->bio_cmd) {
607 if (zv->zv_flags & ZVOL_RDONLY) {
608 error = SET_ERROR(EROFS);
611 zvol_ensure_zilog(zv);
612 if (bp->bio_cmd == BIO_FLUSH)
620 off = bp->bio_offset;
621 volsize = zv->zv_volsize;
627 resid = bp->bio_length;
629 if (resid > 0 && off >= volsize) {
630 error = SET_ERROR(EIO);
634 is_dumpified = B_FALSE;
635 sync = !doread && !is_dumpified &&
636 zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS;
639 * There must be no buffer changes when doing a dmu_sync() because
640 * we can't change the data whilst calculating the checksum.
642 lr = zfs_rangelock_enter(&zv->zv_rangelock, off, resid,
643 doread ? RL_READER : RL_WRITER);
645 if (bp->bio_cmd == BIO_DELETE) {
646 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
647 error = dmu_tx_assign(tx, TXG_WAIT);
651 zvol_log_truncate(zv, tx, off, resid, sync);
653 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ,
659 while (resid != 0 && off < volsize) {
660 size_t size = MIN(resid, zvol_maxphys);
662 error = dmu_read(os, ZVOL_OBJ, off, size, addr,
665 dmu_tx_t *tx = dmu_tx_create(os);
666 dmu_tx_hold_write_by_dnode(tx, zv->zv_dn, off, size);
667 error = dmu_tx_assign(tx, TXG_WAIT);
671 dmu_write(os, ZVOL_OBJ, off, size, addr, tx);
672 zvol_log_write(zv, tx, off, size, sync);
677 /* convert checksum errors into IO errors */
679 error = SET_ERROR(EIO);
687 zfs_rangelock_exit(lr);
689 bp->bio_completed = bp->bio_length - resid;
690 if (bp->bio_completed < bp->bio_length && off > volsize)
693 switch (bp->bio_cmd) {
697 dataset_kstats_update_read_kstats(&zv->zv_kstat,
701 dataset_kstats_update_write_kstats(&zv->zv_kstat,
712 zil_commit(zv->zv_zilog, ZVOL_OBJ);
715 rw_exit(&zv->zv_suspend_lock);
718 g_io_deliver(bp, error);
720 biofinish(bp, NULL, error);
724 * Character device mode implementation
728 zvol_cdev_read(struct cdev *dev, struct uio *uio, int ioflag)
732 zfs_locked_range_t *lr;
737 volsize = zv->zv_volsize;
739 * uio_loffset == volsize isn't an error as
740 * its required for EOF processing.
742 if (uio->uio_resid > 0 &&
743 (uio->uio_loffset < 0 || uio->uio_loffset > volsize))
744 return (SET_ERROR(EIO));
746 lr = zfs_rangelock_enter(&zv->zv_rangelock, uio->uio_loffset,
747 uio->uio_resid, RL_READER);
748 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
749 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
751 /* don't read past the end */
752 if (bytes > volsize - uio->uio_loffset)
753 bytes = volsize - uio->uio_loffset;
755 error = dmu_read_uio_dnode(zv->zv_dn, uio, bytes);
757 /* convert checksum errors into IO errors */
759 error = SET_ERROR(EIO);
763 zfs_rangelock_exit(lr);
769 zvol_cdev_write(struct cdev *dev, struct uio *uio, int ioflag)
773 zfs_locked_range_t *lr;
779 volsize = zv->zv_volsize;
781 if (uio->uio_resid > 0 &&
782 (uio->uio_loffset < 0 || uio->uio_loffset > volsize))
783 return (SET_ERROR(EIO));
785 sync = (ioflag & IO_SYNC) ||
786 (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS);
788 rw_enter(&zv->zv_suspend_lock, ZVOL_RW_READER);
789 zvol_ensure_zilog(zv);
791 lr = zfs_rangelock_enter(&zv->zv_rangelock, uio->uio_loffset,
792 uio->uio_resid, RL_WRITER);
793 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
794 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
795 uint64_t off = uio->uio_loffset;
796 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
798 if (bytes > volsize - off) /* don't write past the end */
799 bytes = volsize - off;
801 dmu_tx_hold_write_by_dnode(tx, zv->zv_dn, off, bytes);
802 error = dmu_tx_assign(tx, TXG_WAIT);
807 error = dmu_write_uio_dnode(zv->zv_dn, uio, bytes, tx);
809 zvol_log_write(zv, tx, off, bytes, sync);
815 zfs_rangelock_exit(lr);
817 zil_commit(zv->zv_zilog, ZVOL_OBJ);
818 rw_exit(&zv->zv_suspend_lock);
823 zvol_cdev_open(struct cdev *dev, int flags, int fmt, struct thread *td)
826 struct zvol_state_dev *zsd;
828 boolean_t drop_suspend = B_TRUE;
830 rw_enter(&zvol_state_lock, ZVOL_RW_READER);
833 rw_exit(&zvol_state_lock);
834 return (SET_ERROR(ENXIO));
837 mutex_enter(&zv->zv_state_lock);
839 ASSERT(zv->zv_zso->zso_volmode == ZFS_VOLMODE_DEV);
842 * make sure zvol is not suspended during first open
843 * (hold zv_suspend_lock) and respect proper lock acquisition
844 * ordering - zv_suspend_lock before zv_state_lock
846 if (zv->zv_open_count == 0) {
847 if (!rw_tryenter(&zv->zv_suspend_lock, ZVOL_RW_READER)) {
848 mutex_exit(&zv->zv_state_lock);
849 rw_enter(&zv->zv_suspend_lock, ZVOL_RW_READER);
850 mutex_enter(&zv->zv_state_lock);
851 /* check to see if zv_suspend_lock is needed */
852 if (zv->zv_open_count != 0) {
853 rw_exit(&zv->zv_suspend_lock);
854 drop_suspend = B_FALSE;
858 drop_suspend = B_FALSE;
860 rw_exit(&zvol_state_lock);
862 ASSERT(MUTEX_HELD(&zv->zv_state_lock));
864 if (zv->zv_open_count == 0) {
865 ASSERT(ZVOL_RW_READ_HELD(&zv->zv_suspend_lock));
866 err = zvol_first_open(zv, !(flags & FWRITE));
871 if ((flags & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
875 if (zv->zv_flags & ZVOL_EXCL) {
881 if (zv->zv_open_count != 0) {
885 zv->zv_flags |= ZVOL_EXCL;
890 if (flags & (FSYNC | FDSYNC)) {
891 zsd = &zv->zv_zso->zso_dev;
893 if (zsd->zsd_sync_cnt == 1)
894 zil_async_to_sync(zv->zv_zilog, ZVOL_OBJ);
897 mutex_exit(&zv->zv_state_lock);
899 rw_exit(&zv->zv_suspend_lock);
903 if (zv->zv_open_count == 0)
906 mutex_exit(&zv->zv_state_lock);
908 rw_exit(&zv->zv_suspend_lock);
909 return (SET_ERROR(err));
913 zvol_cdev_close(struct cdev *dev, int flags, int fmt, struct thread *td)
916 struct zvol_state_dev *zsd;
917 boolean_t drop_suspend = B_TRUE;
919 rw_enter(&zvol_state_lock, ZVOL_RW_READER);
922 rw_exit(&zvol_state_lock);
923 return (SET_ERROR(ENXIO));
926 mutex_enter(&zv->zv_state_lock);
927 if (zv->zv_flags & ZVOL_EXCL) {
928 ASSERT(zv->zv_open_count == 1);
929 zv->zv_flags &= ~ZVOL_EXCL;
932 ASSERT(zv->zv_zso->zso_volmode == ZFS_VOLMODE_DEV);
935 * If the open count is zero, this is a spurious close.
936 * That indicates a bug in the kernel / DDI framework.
938 ASSERT(zv->zv_open_count > 0);
940 * make sure zvol is not suspended during last close
941 * (hold zv_suspend_lock) and respect proper lock acquisition
942 * ordering - zv_suspend_lock before zv_state_lock
944 if (zv->zv_open_count == 1) {
945 if (!rw_tryenter(&zv->zv_suspend_lock, ZVOL_RW_READER)) {
946 mutex_exit(&zv->zv_state_lock);
947 rw_enter(&zv->zv_suspend_lock, ZVOL_RW_READER);
948 mutex_enter(&zv->zv_state_lock);
949 /* check to see if zv_suspend_lock is needed */
950 if (zv->zv_open_count != 1) {
951 rw_exit(&zv->zv_suspend_lock);
952 drop_suspend = B_FALSE;
956 drop_suspend = B_FALSE;
958 rw_exit(&zvol_state_lock);
960 ASSERT(MUTEX_HELD(&zv->zv_state_lock));
963 * You may get multiple opens, but only one close.
966 if (flags & (FSYNC | FDSYNC)) {
967 zsd = &zv->zv_zso->zso_dev;
971 if (zv->zv_open_count == 0) {
972 ASSERT(ZVOL_RW_READ_HELD(&zv->zv_suspend_lock));
976 mutex_exit(&zv->zv_state_lock);
979 rw_exit(&zv->zv_suspend_lock);
984 zvol_cdev_ioctl(struct cdev *dev, ulong_t cmd, caddr_t data,
985 int fflag, struct thread *td)
988 zfs_locked_range_t *lr;
989 off_t offset, length;
996 KASSERT(zv->zv_open_count > 0,
997 ("Device with zero access count in %s", __func__));
999 i = IOCPARM_LEN(cmd);
1001 case DIOCGSECTORSIZE:
1002 *(uint32_t *)data = DEV_BSIZE;
1004 case DIOCGMEDIASIZE:
1005 *(off_t *)data = zv->zv_volsize;
1008 rw_enter(&zv->zv_suspend_lock, ZVOL_RW_READER);
1009 if (zv->zv_zilog != NULL)
1010 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1011 rw_exit(&zv->zv_suspend_lock);
1014 if (!zvol_unmap_enabled)
1017 offset = ((off_t *)data)[0];
1018 length = ((off_t *)data)[1];
1019 if ((offset % DEV_BSIZE) != 0 || (length % DEV_BSIZE) != 0 ||
1020 offset < 0 || offset >= zv->zv_volsize ||
1022 printf("%s: offset=%jd length=%jd\n", __func__, offset,
1027 rw_enter(&zv->zv_suspend_lock, ZVOL_RW_READER);
1028 zvol_ensure_zilog(zv);
1029 lr = zfs_rangelock_enter(&zv->zv_rangelock, offset, length,
1031 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
1032 error = dmu_tx_assign(tx, TXG_WAIT);
1037 sync = (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS);
1038 zvol_log_truncate(zv, tx, offset, length, sync);
1040 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ,
1043 zfs_rangelock_exit(lr);
1045 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1046 rw_exit(&zv->zv_suspend_lock);
1048 case DIOCGSTRIPESIZE:
1049 *(off_t *)data = zv->zv_volblocksize;
1051 case DIOCGSTRIPEOFFSET:
1055 spa_t *spa = dmu_objset_spa(zv->zv_objset);
1056 struct diocgattr_arg *arg = (struct diocgattr_arg *)data;
1057 uint64_t refd, avail, usedobjs, availobjs;
1059 if (strcmp(arg->name, "GEOM::candelete") == 0)
1061 else if (strcmp(arg->name, "blocksavail") == 0) {
1062 dmu_objset_space(zv->zv_objset, &refd, &avail,
1063 &usedobjs, &availobjs);
1064 arg->value.off = avail / DEV_BSIZE;
1065 } else if (strcmp(arg->name, "blocksused") == 0) {
1066 dmu_objset_space(zv->zv_objset, &refd, &avail,
1067 &usedobjs, &availobjs);
1068 arg->value.off = refd / DEV_BSIZE;
1069 } else if (strcmp(arg->name, "poolblocksavail") == 0) {
1070 avail = metaslab_class_get_space(spa_normal_class(spa));
1071 avail -= metaslab_class_get_alloc(
1072 spa_normal_class(spa));
1073 arg->value.off = avail / DEV_BSIZE;
1074 } else if (strcmp(arg->name, "poolblocksused") == 0) {
1075 refd = metaslab_class_get_alloc(spa_normal_class(spa));
1076 arg->value.off = refd / DEV_BSIZE;
1083 off_t *off = (off_t *)data;
1087 hole = (cmd == FIOSEEKHOLE);
1089 error = dmu_offset_next(zv->zv_objset, ZVOL_OBJ, hole, &noff);
1105 zvol_ensure_zilog(zvol_state_t *zv)
1107 ASSERT(ZVOL_RW_READ_HELD(&zv->zv_suspend_lock));
1110 * Open a ZIL if this is the first time we have written to this
1111 * zvol. We protect zv->zv_zilog with zv_suspend_lock rather
1112 * than zv_state_lock so that we don't need to acquire an
1113 * additional lock in this path.
1115 if (zv->zv_zilog == NULL) {
1116 if (!rw_tryupgrade(&zv->zv_suspend_lock)) {
1117 rw_exit(&zv->zv_suspend_lock);
1118 rw_enter(&zv->zv_suspend_lock, RW_WRITER);
1120 if (zv->zv_zilog == NULL) {
1121 zv->zv_zilog = zil_open(zv->zv_objset,
1123 zv->zv_flags |= ZVOL_WRITTEN_TO;
1125 rw_downgrade(&zv->zv_suspend_lock);
1130 zvol_is_zvol_impl(const char *device)
1132 return (device && strncmp(device, ZVOL_DIR, strlen(ZVOL_DIR)) == 0);
1136 zvol_rename_minor(zvol_state_t *zv, const char *newname)
1138 ASSERT(RW_LOCK_HELD(&zvol_state_lock));
1139 ASSERT(MUTEX_HELD(&zv->zv_state_lock));
1141 /* move to new hashtable entry */
1142 zv->zv_hash = zvol_name_hash(zv->zv_name);
1143 hlist_del(&zv->zv_hlink);
1144 hlist_add_head(&zv->zv_hlink, ZVOL_HT_HEAD(zv->zv_hash));
1146 if (zv->zv_zso->zso_volmode == ZFS_VOLMODE_GEOM) {
1147 struct zvol_state_geom *zsg = &zv->zv_zso->zso_geom;
1148 struct g_provider *pp = zsg->zsg_provider;
1155 zsg->zsg_provider = NULL;
1156 g_wither_provider(pp, ENXIO);
1158 pp = g_new_providerf(gp, "%s/%s", ZVOL_DRIVER, newname);
1159 pp->flags |= G_PF_DIRECT_RECEIVE | G_PF_DIRECT_SEND;
1160 pp->sectorsize = DEV_BSIZE;
1161 pp->mediasize = zv->zv_volsize;
1163 zsg->zsg_provider = pp;
1164 g_error_provider(pp, 0);
1165 g_topology_unlock();
1166 } else if (zv->zv_zso->zso_volmode == ZFS_VOLMODE_DEV) {
1167 struct zvol_state_dev *zsd = &zv->zv_zso->zso_dev;
1169 struct make_dev_args args;
1171 dev = zsd->zsd_cdev;
1174 dev = zsd->zsd_cdev = NULL;
1175 if (zv->zv_open_count > 0) {
1176 zv->zv_flags &= ~ZVOL_EXCL;
1177 zv->zv_open_count = 0;
1178 /* XXX need suspend lock but lock order */
1179 zvol_last_close(zv);
1183 make_dev_args_init(&args);
1184 args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK;
1185 args.mda_devsw = &zvol_cdevsw;
1187 args.mda_uid = UID_ROOT;
1188 args.mda_gid = GID_OPERATOR;
1189 args.mda_mode = 0640;
1190 args.mda_si_drv2 = zv;
1191 if (make_dev_s(&args, &dev, "%s/%s", ZVOL_DRIVER, newname)
1193 dev->si_iosize_max = MAXPHYS;
1194 zsd->zsd_cdev = dev;
1197 strlcpy(zv->zv_name, newname, sizeof (zv->zv_name));
1201 * Remove minor node for the specified volume.
1204 zvol_free(zvol_state_t *zv)
1206 ASSERT(!RW_LOCK_HELD(&zv->zv_suspend_lock));
1207 ASSERT(!MUTEX_HELD(&zv->zv_state_lock));
1208 ASSERT(zv->zv_open_count == 0);
1210 ZFS_LOG(1, "ZVOL %s destroyed.", zv->zv_name);
1212 rw_destroy(&zv->zv_suspend_lock);
1213 zfs_rangelock_fini(&zv->zv_rangelock);
1215 if (zv->zv_zso->zso_volmode == ZFS_VOLMODE_GEOM) {
1216 struct zvol_state_geom *zsg = &zv->zv_zso->zso_geom;
1219 zvol_geom_destroy(zv);
1220 g_topology_unlock();
1221 mtx_destroy(&zsg->zsg_queue_mtx);
1222 } else if (zv->zv_zso->zso_volmode == ZFS_VOLMODE_DEV) {
1223 struct zvol_state_dev *zsd = &zv->zv_zso->zso_dev;
1224 struct cdev *dev = zsd->zsd_cdev;
1230 mutex_destroy(&zv->zv_state_lock);
1231 dataset_kstats_destroy(&zv->zv_kstat);
1232 kmem_free(zv->zv_zso, sizeof (struct zvol_state_os));
1233 kmem_free(zv, sizeof (zvol_state_t));
1238 * Create a minor node (plus a whole lot more) for the specified volume.
1241 zvol_create_minor_impl(const char *name)
1245 dmu_object_info_t *doi;
1247 uint64_t volmode, hash;
1250 ZFS_LOG(1, "Creating ZVOL %s...", name);
1252 hash = zvol_name_hash(name);
1253 if ((zv = zvol_find_by_name_hash(name, hash, RW_NONE)) != NULL) {
1254 ASSERT(MUTEX_HELD(&zv->zv_state_lock));
1255 mutex_exit(&zv->zv_state_lock);
1256 return (SET_ERROR(EEXIST));
1260 /* lie and say we're read-only */
1261 error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, B_TRUE, FTAG, &os);
1262 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
1267 error = dmu_object_info(os, ZVOL_OBJ, doi);
1269 goto out_dmu_objset_disown;
1271 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
1273 goto out_dmu_objset_disown;
1275 error = dsl_prop_get_integer(name,
1276 zfs_prop_to_name(ZFS_PROP_VOLMODE), &volmode, NULL);
1277 if (error != 0 || volmode == ZFS_VOLMODE_DEFAULT)
1278 volmode = zvol_volmode;
1280 * zvol_alloc equivalent ...
1282 zv = kmem_zalloc(sizeof (*zv), KM_SLEEP);
1284 mutex_init(&zv->zv_state_lock, NULL, MUTEX_DEFAULT, NULL);
1285 zv->zv_zso = kmem_zalloc(sizeof (struct zvol_state_os), KM_SLEEP);
1286 zv->zv_zso->zso_volmode = volmode;
1287 if (zv->zv_zso->zso_volmode == ZFS_VOLMODE_GEOM) {
1288 struct zvol_state_geom *zsg = &zv->zv_zso->zso_geom;
1289 struct g_provider *pp;
1292 zsg->zsg_state = ZVOL_GEOM_UNINIT;
1293 mtx_init(&zsg->zsg_queue_mtx, "zvol", NULL, MTX_DEF);
1296 gp = g_new_geomf(&zfs_zvol_class, "zfs::zvol::%s", name);
1297 gp->start = zvol_geom_bio_start;
1298 gp->access = zvol_geom_access;
1299 pp = g_new_providerf(gp, "%s/%s", ZVOL_DRIVER, name);
1300 /* TODO: NULL check? */
1301 pp->flags |= G_PF_DIRECT_RECEIVE | G_PF_DIRECT_SEND;
1302 pp->sectorsize = DEV_BSIZE;
1306 zsg->zsg_provider = pp;
1307 bioq_init(&zsg->zsg_queue);
1308 } else if (zv->zv_zso->zso_volmode == ZFS_VOLMODE_DEV) {
1309 struct zvol_state_dev *zsd = &zv->zv_zso->zso_dev;
1311 struct make_dev_args args;
1313 make_dev_args_init(&args);
1314 args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK;
1315 args.mda_devsw = &zvol_cdevsw;
1317 args.mda_uid = UID_ROOT;
1318 args.mda_gid = GID_OPERATOR;
1319 args.mda_mode = 0640;
1320 args.mda_si_drv2 = zv;
1321 error = make_dev_s(&args, &dev, "%s/%s", ZVOL_DRIVER, name);
1323 mutex_destroy(&zv->zv_state_lock);
1324 kmem_free(zv->zv_zso, sizeof (struct zvol_state_os));
1325 kmem_free(zv, sizeof (*zv));
1326 dmu_objset_disown(os, B_TRUE, FTAG);
1329 dev->si_iosize_max = MAXPHYS;
1330 zsd->zsd_cdev = dev;
1332 (void) strlcpy(zv->zv_name, name, MAXPATHLEN);
1333 rw_init(&zv->zv_suspend_lock, NULL, RW_DEFAULT, NULL);
1334 zfs_rangelock_init(&zv->zv_rangelock, NULL, NULL);
1336 if (dmu_objset_is_snapshot(os) || !spa_writeable(dmu_objset_spa(os)))
1337 zv->zv_flags |= ZVOL_RDONLY;
1339 zv->zv_volblocksize = doi->doi_data_block_size;
1340 zv->zv_volsize = volsize;
1343 if (spa_writeable(dmu_objset_spa(os))) {
1344 if (zil_replay_disable)
1345 zil_destroy(dmu_objset_zil(os), B_FALSE);
1347 zil_replay(os, zv, zvol_replay_vector);
1349 ASSERT3P(zv->zv_kstat.dk_kstats, ==, NULL);
1350 dataset_kstats_create(&zv->zv_kstat, zv->zv_objset);
1352 /* XXX do prefetch */
1354 zv->zv_objset = NULL;
1355 out_dmu_objset_disown:
1356 dmu_objset_disown(os, B_TRUE, FTAG);
1358 if (zv->zv_zso->zso_volmode == ZFS_VOLMODE_GEOM) {
1361 g_topology_unlock();
1364 kmem_free(doi, sizeof (dmu_object_info_t));
1366 rw_enter(&zvol_state_lock, RW_WRITER);
1369 rw_exit(&zvol_state_lock);
1371 ZFS_LOG(1, "ZVOL %s created.", name);
1378 zvol_clear_private(zvol_state_t *zv)
1380 ASSERT(RW_LOCK_HELD(&zvol_state_lock));
1381 if (zv->zv_zso->zso_volmode == ZFS_VOLMODE_GEOM) {
1382 struct zvol_state_geom *zsg = &zv->zv_zso->zso_geom;
1383 struct g_provider *pp = zsg->zsg_provider;
1385 if (pp == NULL) /* XXX when? */
1388 mtx_lock(&zsg->zsg_queue_mtx);
1389 zsg->zsg_state = ZVOL_GEOM_STOPPED;
1391 wakeup_one(&zsg->zsg_queue);
1392 while (zsg->zsg_state != ZVOL_GEOM_RUNNING)
1393 msleep(&zsg->zsg_state,
1394 &zsg->zsg_queue_mtx,
1396 mtx_unlock(&zsg->zsg_queue_mtx);
1397 ASSERT(!RW_LOCK_HELD(&zv->zv_suspend_lock));
1402 zvol_update_volsize(zvol_state_t *zv, uint64_t volsize)
1404 zv->zv_volsize = volsize;
1405 if (zv->zv_zso->zso_volmode == ZFS_VOLMODE_GEOM) {
1406 struct zvol_state_geom *zsg = &zv->zv_zso->zso_geom;
1407 struct g_provider *pp = zsg->zsg_provider;
1409 if (pp == NULL) /* XXX when? */
1415 * Do not invoke resize event when initial size was zero.
1416 * ZVOL initializes the size on first open, this is not
1419 if (pp->mediasize == 0)
1420 pp->mediasize = zv->zv_volsize;
1422 g_resize_provider(pp, zv->zv_volsize);
1424 g_topology_unlock();
1430 zvol_set_disk_ro_impl(zvol_state_t *zv, int flags)
1432 // XXX? set_disk_ro(zv->zv_zso->zvo_disk, flags);
1436 zvol_set_capacity_impl(zvol_state_t *zv, uint64_t capacity)
1438 // XXX? set_capacity(zv->zv_zso->zvo_disk, capacity);
1441 const static zvol_platform_ops_t zvol_freebsd_ops = {
1442 .zv_free = zvol_free,
1443 .zv_rename_minor = zvol_rename_minor,
1444 .zv_create_minor = zvol_create_minor_impl,
1445 .zv_update_volsize = zvol_update_volsize,
1446 .zv_clear_private = zvol_clear_private,
1447 .zv_is_zvol = zvol_is_zvol_impl,
1448 .zv_set_disk_ro = zvol_set_disk_ro_impl,
1449 .zv_set_capacity = zvol_set_capacity_impl,
1459 return (zvol_minors != 0);
1466 zvol_register_ops(&zvol_freebsd_ops);