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) 2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
23 * All rights reserved.
26 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
27 * Use is subject to license terms.
31 * ZFS volume emulation driver.
33 * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes.
34 * Volumes are accessed through the symbolic links named:
36 * /dev/zvol/dsk/<pool_name>/<dataset_name>
37 * /dev/zvol/rdsk/<pool_name>/<dataset_name>
39 * These links are created by the ZFS-specific devfsadm link generator.
40 * Volumes are persistent through reboot. No user command needs to be
41 * run before opening and using a device.
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/kernel.h>
47 #include <sys/errno.h>
53 #include <sys/cmn_err.h>
58 #include <sys/dmu_traverse.h>
59 #include <sys/dnode.h>
60 #include <sys/dsl_dataset.h>
61 #include <sys/dsl_prop.h>
63 #include <sys/byteorder.h>
64 #include <sys/sunddi.h>
65 #include <sys/dirent.h>
66 #include <sys/policy.h>
67 #include <sys/fs/zfs.h>
68 #include <sys/zfs_ioctl.h>
70 #include <sys/refcount.h>
71 #include <sys/zfs_znode.h>
72 #include <sys/zfs_rlock.h>
73 #include <sys/vdev_impl.h>
75 #include <geom/geom.h>
77 #include "zfs_namecheck.h"
79 #define ZVOL_DUMPSIZE "dumpsize"
81 struct g_class zfs_zvol_class = {
86 DECLARE_GEOM_CLASS(zfs_zvol_class, zfs_zvol);
89 * This lock protects the zvol_state structure from being modified
90 * while it's being used, e.g. an open that comes in before a create
91 * finishes. It also protects temporary opens of the dataset so that,
92 * e.g., an open doesn't get a spurious EBUSY.
94 static kmutex_t zvol_state_lock;
95 static uint32_t zvol_minors;
97 #define NUM_EXTENTS ((SPA_MAXBLOCKSIZE) / sizeof (zvol_extent_t))
99 typedef struct zvol_extent {
100 dva_t ze_dva; /* dva associated with this extent */
101 uint64_t ze_stride; /* extent stride */
102 uint64_t ze_size; /* number of blocks in extent */
106 * The list of extents associated with the dump device
108 typedef struct zvol_ext_list {
109 zvol_extent_t zl_extents[NUM_EXTENTS];
110 struct zvol_ext_list *zl_next;
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 struct g_provider *zv_provider; /* GEOM provider */
121 uint8_t zv_min_bs; /* minimum addressable block shift */
122 uint8_t zv_flags; /* readonly; dumpified */
123 objset_t *zv_objset; /* objset handle */
124 uint32_t zv_mode; /* DS_MODE_* flags at open time */
125 uint32_t zv_total_opens; /* total open count */
126 zilog_t *zv_zilog; /* ZIL handle */
127 zvol_ext_list_t *zv_list; /* List of extents for dump */
128 uint64_t zv_txg_assign; /* txg to assign during ZIL replay */
129 znode_t zv_znode; /* for range locking */
131 struct bio_queue_head zv_queue;
132 struct mtx zv_queue_mtx; /* zv_queue mutex */
136 * zvol specific flags
138 #define ZVOL_RDONLY 0x1
139 #define ZVOL_DUMPIFIED 0x2
140 #define ZVOL_EXCL 0x4
143 * zvol maximum transfer in one DMU tx.
145 int zvol_maxphys = DMU_MAX_ACCESS/2;
147 extern int zfs_set_prop_nvlist(const char *, nvlist_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, major_t maj)
156 struct g_provider *pp;
160 pp = zv->zv_provider;
163 if (zv->zv_volsize == pp->mediasize)
166 * Changing provider size is not really supported by GEOM, but it
167 * should be safe when provider is closed.
169 if (zv->zv_total_opens > 0)
171 pp->mediasize = zv->zv_volsize;
175 zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
180 if (volsize % blocksize != 0)
184 if (volsize - 1 > SPEC_MAXOFFSET_T)
191 zvol_check_volblocksize(uint64_t volblocksize)
193 if (volblocksize < SPA_MINBLOCKSIZE ||
194 volblocksize > SPA_MAXBLOCKSIZE ||
202 zvol_readonly_changed_cb(void *arg, uint64_t newval)
204 zvol_state_t *zv = arg;
207 zv->zv_flags |= ZVOL_RDONLY;
209 zv->zv_flags &= ~ZVOL_RDONLY;
213 zvol_get_stats(objset_t *os, nvlist_t *nv)
216 dmu_object_info_t doi;
220 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
224 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
226 error = dmu_object_info(os, ZVOL_OBJ, &doi);
229 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
230 doi.doi_data_block_size);
236 static zvol_state_t *
237 zvol_minor_lookup(const char *name)
239 struct g_provider *pp;
243 ASSERT(MUTEX_HELD(&zvol_state_lock));
245 LIST_FOREACH(gp, &zfs_zvol_class.geom, geom) {
246 LIST_FOREACH(pp, &gp->provider, provider) {
247 if (strcmp(pp->name + sizeof(ZVOL_DEV_DIR), name) == 0)
248 return (pp->private);
256 zvol_access(struct g_provider *pp, int acr, int acw, int ace)
261 mutex_enter(&zvol_state_lock);
265 if (acr <= 0 && acw <= 0 && ace <= 0)
267 mutex_exit(&zvol_state_lock);
271 ASSERT(zv->zv_objset != NULL);
274 ((zv->zv_flags & ZVOL_RDONLY) ||
275 (zv->zv_mode & DS_MODE_READONLY))) {
276 mutex_exit(&zvol_state_lock);
280 zv->zv_total_opens += acr + acw + ace;
281 zvol_size_changed(zv, 0);
283 mutex_exit(&zvol_state_lock);
289 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
291 * We store data in the log buffers if it's small enough.
292 * Otherwise we will later flush the data out via dmu_sync().
294 ssize_t zvol_immediate_write_sz = 32768;
297 zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t len)
299 uint32_t blocksize = zv->zv_volblocksize;
303 ssize_t nbytes = MIN(len, blocksize - P2PHASE(off, blocksize));
304 itx_t *itx = zil_itx_create(TX_WRITE, sizeof (*lr));
307 len > zvol_immediate_write_sz ? WR_INDIRECT : WR_NEED_COPY;
308 itx->itx_private = zv;
309 lr = (lr_write_t *)&itx->itx_lr;
310 lr->lr_foid = ZVOL_OBJ;
312 lr->lr_length = nbytes;
313 lr->lr_blkoff = off - P2ALIGN_TYPED(off, blocksize, uint64_t);
314 BP_ZERO(&lr->lr_blkptr);
316 (void) zil_itx_assign(zv->zv_zilog, itx, tx);
323 zvol_start(struct bio *bp)
327 switch (bp->bio_cmd) {
331 zv = bp->bio_to->private;
333 mtx_lock(&zv->zv_queue_mtx);
334 bioq_insert_tail(&zv->zv_queue, bp);
335 wakeup_one(&zv->zv_queue);
336 mtx_unlock(&zv->zv_queue_mtx);
341 g_io_deliver(bp, EOPNOTSUPP);
347 zvol_serve_one(zvol_state_t *zv, struct bio *bp)
349 uint64_t off, volsize;
357 off = bp->bio_offset;
358 volsize = zv->zv_volsize;
364 resid = bp->bio_length;
369 * There must be no buffer changes when doing a dmu_sync() because
370 * we can't change the data whilst calculating the checksum.
371 * A better approach than a per zvol rwlock would be to lock ranges.
373 reading = (bp->bio_cmd == BIO_READ);
374 rl = zfs_range_lock(&zv->zv_znode, off, resid,
375 reading ? RL_READER : RL_WRITER);
377 while (resid != 0 && off < volsize) {
379 size = MIN(resid, zvol_maxphys); /* zvol_maxphys per tx */
381 if (size > volsize - off) /* don't write past the end */
382 size = volsize - off;
385 error = dmu_read(os, ZVOL_OBJ, off, size, addr);
387 dmu_tx_t *tx = dmu_tx_create(os);
388 dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
389 error = dmu_tx_assign(tx, TXG_WAIT);
393 dmu_write(os, ZVOL_OBJ, off, size, addr, tx);
394 zvol_log_write(zv, tx, off, size);
399 /* convert checksum errors into IO errors */
408 zfs_range_unlock(rl);
410 bp->bio_completed = bp->bio_length - resid;
411 if (bp->bio_completed < bp->bio_length)
412 bp->bio_error = (off > volsize ? EINVAL : error);
416 zvol_worker(void *arg)
421 thread_lock(curthread);
422 sched_prio(curthread, PRIBIO);
423 thread_unlock(curthread);
427 mtx_lock(&zv->zv_queue_mtx);
428 bp = bioq_takefirst(&zv->zv_queue);
430 if (zv->zv_state == 1) {
432 wakeup(&zv->zv_state);
433 mtx_unlock(&zv->zv_queue_mtx);
436 msleep(&zv->zv_queue, &zv->zv_queue_mtx, PRIBIO | PDROP,
440 mtx_unlock(&zv->zv_queue_mtx);
441 switch (bp->bio_cmd) {
446 zvol_serve_one(zv, bp);
450 if (bp->bio_cmd == BIO_FLUSH && !zil_disable)
451 zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ);
453 g_io_deliver(bp, bp->bio_error);
458 zvol_init_extent(zvol_extent_t *ze, blkptr_t *bp)
460 ze->ze_dva = bp->blk_dva[0]; /* structure assignment */
465 /* extent mapping arg */
467 zvol_ext_list_t *ma_list;
468 zvol_extent_t *ma_extent;
474 zvol_map_block(traverse_blk_cache_t *bc, spa_t *spa, void *arg)
476 zbookmark_t *zb = &bc->bc_bookmark;
477 blkptr_t *bp = &bc->bc_blkptr;
478 void *data = bc->bc_data;
479 dnode_phys_t *dnp = bc->bc_dnode;
480 struct maparg *ma = (struct maparg *)arg;
483 /* If there is an error, then keep trying to make progress */
488 if (zb->zb_level == -1) {
489 ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET);
490 ASSERT3U(BP_GET_LEVEL(bp), ==, 0);
492 ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
493 ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
496 if (zb->zb_level > 0) {
498 blkptr_t *bpx, *bpend;
500 for (bpx = data, bpend = bpx + BP_GET_LSIZE(bp) / sizeof (*bpx);
501 bpx < bpend; bpx++) {
502 if (bpx->blk_birth != 0) {
503 fill += bpx->blk_fill;
505 ASSERT(bpx->blk_fill == 0);
508 ASSERT3U(fill, ==, bp->blk_fill);
511 if (zb->zb_level == 0 && dnp->dn_type == DMU_OT_DNODE) {
513 dnode_phys_t *dnx, *dnend;
515 for (dnx = data, dnend = dnx + (BP_GET_LSIZE(bp)>>DNODE_SHIFT);
516 dnx < dnend; dnx++) {
517 if (dnx->dn_type != DMU_OT_NONE)
520 ASSERT3U(fill, ==, bp->blk_fill);
524 if (zb->zb_level || dnp->dn_type == DMU_OT_DNODE)
527 /* Abort immediately if we have encountered gang blocks */
528 if (BP_IS_GANG(bp)) {
534 if (ma->ma_extent->ze_size == 0) {
535 zvol_init_extent(ma->ma_extent, bp);
539 stride = (DVA_GET_OFFSET(&bp->blk_dva[0])) -
540 ((DVA_GET_OFFSET(&ma->ma_extent->ze_dva)) +
541 (ma->ma_extent->ze_size - 1) * (ma->ma_extent->ze_stride));
542 if (DVA_GET_VDEV(BP_IDENTITY(bp)) ==
543 DVA_GET_VDEV(&ma->ma_extent->ze_dva)) {
544 if (ma->ma_extent->ze_stride == 0) {
545 /* second block in this extent */
546 ma->ma_extent->ze_stride = stride;
547 ma->ma_extent->ze_size++;
549 } else if (ma->ma_extent->ze_stride == stride) {
551 * the block we allocated has the same
554 ma->ma_extent->ze_size++;
560 * dtrace -n 'zfs-dprintf
561 * /stringof(arg0) == "zvol.c"/
563 * printf("%s: %s", stringof(arg1), stringof(arg3))
566 dprintf("ma_extent 0x%lx mrstride 0x%lx stride %lx\n",
567 ma->ma_extent->ze_size, ma->ma_extent->ze_stride, stride);
568 dprintf_bp(bp, "%s", "next blkptr:");
569 /* start a new extent */
570 if (ma->ma_extent == &ma->ma_list->zl_extents[NUM_EXTENTS - 1]) {
571 ma->ma_list->zl_next = kmem_zalloc(sizeof (zvol_ext_list_t),
573 ma->ma_list = ma->ma_list->zl_next;
574 ma->ma_extent = &ma->ma_list->zl_extents[0];
578 zvol_init_extent(ma->ma_extent, bp);
584 zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
586 zfs_creat_t *zct = arg;
587 nvlist_t *nvprops = zct->zct_props;
589 uint64_t volblocksize, volsize;
591 VERIFY(nvlist_lookup_uint64(nvprops,
592 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
593 if (nvlist_lookup_uint64(nvprops,
594 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
595 volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
598 * These properties must be removed from the list so the generic
599 * property setting step won't apply to them.
601 VERIFY(nvlist_remove_all(nvprops,
602 zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
603 (void) nvlist_remove_all(nvprops,
604 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
606 error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
610 error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
614 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
619 * Replay a TX_WRITE ZIL transaction that didn't get committed
620 * after a system failure
623 zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
625 objset_t *os = zv->zv_objset;
626 char *data = (char *)(lr + 1); /* data follows lr_write_t */
627 uint64_t off = lr->lr_offset;
628 uint64_t len = lr->lr_length;
633 byteswap_uint64_array(lr, sizeof (*lr));
635 tx = dmu_tx_create(os);
636 dmu_tx_hold_write(tx, ZVOL_OBJ, off, len);
637 error = dmu_tx_assign(tx, zv->zv_txg_assign);
641 dmu_write(os, ZVOL_OBJ, off, len, data, tx);
650 zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
656 * Callback vectors for replaying records.
657 * Only TX_WRITE is needed for zvol.
659 zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = {
660 zvol_replay_err, /* 0 no such transaction type */
661 zvol_replay_err, /* TX_CREATE */
662 zvol_replay_err, /* TX_MKDIR */
663 zvol_replay_err, /* TX_MKXATTR */
664 zvol_replay_err, /* TX_SYMLINK */
665 zvol_replay_err, /* TX_REMOVE */
666 zvol_replay_err, /* TX_RMDIR */
667 zvol_replay_err, /* TX_LINK */
668 zvol_replay_err, /* TX_RENAME */
669 zvol_replay_write, /* TX_WRITE */
670 zvol_replay_err, /* TX_TRUNCATE */
671 zvol_replay_err, /* TX_SETATTR */
672 zvol_replay_err, /* TX_ACL */
676 * reconstruct dva that gets us to the desired offset (offset
680 zvol_get_dva(zvol_state_t *zv, uint64_t offset, dva_t *dva)
687 if ((zl = zv->zv_list) == NULL)
690 ze = &zl->zl_extents[0];
691 while (offset >= ze->ze_size * zv->zv_volblocksize) {
692 offset -= ze->ze_size * zv->zv_volblocksize;
694 if (idx == NUM_EXTENTS - 1) {
695 /* we've reached the end of this array */
696 ASSERT(zl->zl_next != NULL);
697 if (zl->zl_next == NULL)
700 ze = &zl->zl_extents[0];
707 DVA_SET_VDEV(dva, DVA_GET_VDEV(&ze->ze_dva));
708 tmp = DVA_GET_OFFSET((&ze->ze_dva));
709 tmp += (ze->ze_stride * (offset / zv->zv_volblocksize));
710 DVA_SET_OFFSET(dva, tmp);
715 zvol_free_extents(zvol_state_t *zv)
718 zvol_ext_list_t *tmp;
720 if (zv->zv_list != NULL) {
724 kmem_free(zl, sizeof (zvol_ext_list_t));
732 zvol_get_lbas(zvol_state_t *zv)
740 ma.ma_list = zl = kmem_zalloc(sizeof (zvol_ext_list_t), KM_SLEEP);
741 ma.ma_extent = &ma.ma_list->zl_extents[0];
743 zv->zv_list = ma.ma_list;
745 err = traverse_zvol(zv->zv_objset, ADVANCE_PRE, zvol_map_block, &ma);
746 if (err == EINTR && ma.ma_gang) {
748 * We currently don't support dump devices when the pool
749 * is so fragmented that our allocation has resulted in
752 zvol_free_extents(zv);
755 ASSERT3U(err, ==, 0);
757 ze = &zl->zl_extents[0];
759 blocks += ze->ze_size;
760 if (ze == &zl->zl_extents[NUM_EXTENTS - 1]) {
762 ze = &zl->zl_extents[0];
767 if (blocks != (zv->zv_volsize / zv->zv_volblocksize)) {
768 zvol_free_extents(zv);
776 * Create a minor node (plus a whole lot more) for the specified volume.
779 zvol_create_minor(const char *name, major_t maj)
781 struct g_provider *pp;
785 dmu_object_info_t doi;
787 int ds_mode = DS_MODE_OWNER;
792 mutex_enter(&zvol_state_lock);
794 if ((zv = zvol_minor_lookup(name)) != NULL) {
799 if (strchr(name, '@') != 0)
800 ds_mode |= DS_MODE_READONLY;
802 error = dmu_objset_open(name, DMU_OST_ZVOL, ds_mode, &os);
806 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
808 dmu_objset_close(os);
812 gp = g_new_geomf(&zfs_zvol_class, "zfs::zvol::%s", name);
813 gp->start = zvol_start;
814 gp->access = zvol_access;
815 pp = g_new_providerf(gp, "%s/%s", ZVOL_DEV_DIR, name);
816 pp->mediasize = volsize;
817 pp->sectorsize = DEV_BSIZE;
819 zv = kmem_zalloc(sizeof(*zv), KM_SLEEP);
820 (void) strcpy(zv->zv_name, name);
821 zv->zv_min_bs = DEV_BSHIFT;
822 zv->zv_provider = pp;
823 zv->zv_volsize = pp->mediasize;
825 zv->zv_mode = ds_mode;
826 zv->zv_zilog = zil_open(os, zvol_get_data);
827 mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
828 avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
829 sizeof (rl_t), offsetof(rl_t, r_node));
830 /* get and cache the blocksize */
831 error = dmu_object_info(os, ZVOL_OBJ, &doi);
833 zv->zv_volblocksize = doi.doi_data_block_size;
835 zil_replay(os, zv, &zv->zv_txg_assign, zvol_replay_vector, NULL);
837 /* XXX this should handle the possible i/o error */
838 VERIFY(dsl_prop_register(dmu_objset_ds(zv->zv_objset),
839 "readonly", zvol_readonly_changed_cb, zv) == 0);
842 g_error_provider(pp, 0);
844 bioq_init(&zv->zv_queue);
845 mtx_init(&zv->zv_queue_mtx, "zvol", NULL, MTX_DEF);
847 kproc_kthread_add(zvol_worker, zv, &zfsproc, NULL, 0, 0, "zfskern",
848 "zvol %s", pp->name + strlen(ZVOL_DEV_DIR) + 1);
852 mutex_exit(&zvol_state_lock);
860 * Remove minor node for the specified volume.
863 zvol_remove_minor(const char *name)
865 struct g_provider *pp;
871 mutex_enter(&zvol_state_lock);
873 if ((zv = zvol_minor_lookup(name)) == NULL) {
878 if (zv->zv_total_opens != 0) {
883 VERIFY(dsl_prop_unregister(dmu_objset_ds(zv->zv_objset),
884 "readonly", zvol_readonly_changed_cb, zv) == 0);
886 mtx_lock(&zv->zv_queue_mtx);
888 wakeup_one(&zv->zv_queue);
889 while (zv->zv_state != 2)
890 msleep(&zv->zv_state, &zv->zv_queue_mtx, 0, "zvol:w", 0);
891 mtx_unlock(&zv->zv_queue_mtx);
892 mtx_destroy(&zv->zv_queue_mtx);
894 pp = zv->zv_provider;
896 g_wither_geom(pp->geom, ENXIO);
898 zil_close(zv->zv_zilog);
900 dmu_objset_close(zv->zv_objset);
901 zv->zv_objset = NULL;
902 avl_destroy(&zv->zv_znode.z_range_avl);
903 mutex_destroy(&zv->zv_znode.z_range_lock);
905 kmem_free(zv, sizeof(*zv));
909 mutex_exit(&zvol_state_lock);
917 zvol_prealloc(zvol_state_t *zv)
919 objset_t *os = zv->zv_objset;
922 uint64_t refd, avail, usedobjs, availobjs;
923 uint64_t resid = zv->zv_volsize;
926 /* Check the space usage before attempting to allocate the space */
927 dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs);
928 if (avail < zv->zv_volsize)
931 /* Free old extents if they exist */
932 zvol_free_extents(zv);
934 /* allocate the blocks by writing each one */
935 data = kmem_zalloc(SPA_MAXBLOCKSIZE, KM_SLEEP);
939 uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE);
941 tx = dmu_tx_create(os);
942 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
943 error = dmu_tx_assign(tx, TXG_WAIT);
946 kmem_free(data, SPA_MAXBLOCKSIZE);
947 (void) dmu_free_long_range(os, ZVOL_OBJ, 0, off);
950 dmu_write(os, ZVOL_OBJ, off, bytes, data, tx);
955 kmem_free(data, SPA_MAXBLOCKSIZE);
956 txg_wait_synced(dmu_objset_pool(os), 0);
962 zvol_update_volsize(zvol_state_t *zv, major_t maj, uint64_t volsize)
967 ASSERT(MUTEX_HELD(&zvol_state_lock));
969 tx = dmu_tx_create(zv->zv_objset);
970 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
971 error = dmu_tx_assign(tx, TXG_WAIT);
977 error = zap_update(zv->zv_objset, ZVOL_ZAP_OBJ, "size", 8, 1,
982 error = dmu_free_long_range(zv->zv_objset,
983 ZVOL_OBJ, volsize, DMU_OBJECT_END);
986 * If we are using a faked-up state (zv_provider == NULL) then don't
987 * try to update the in-core zvol state.
989 if (error == 0 && zv->zv_provider) {
990 zv->zv_volsize = volsize;
991 zvol_size_changed(zv, maj);
997 zvol_set_volsize(const char *name, major_t maj, uint64_t volsize)
1001 dmu_object_info_t doi;
1002 uint64_t old_volsize = 0ULL;
1003 zvol_state_t state = { 0 };
1007 mutex_enter(&zvol_state_lock);
1009 if ((zv = zvol_minor_lookup(name)) == NULL) {
1011 * If we are doing a "zfs clone -o volsize=", then the
1012 * minor node won't exist yet.
1014 error = dmu_objset_open(name, DMU_OST_ZVOL, DS_MODE_OWNER,
1020 old_volsize = zv->zv_volsize;
1022 if ((error = dmu_object_info(zv->zv_objset, ZVOL_OBJ, &doi)) != 0 ||
1023 (error = zvol_check_volsize(volsize,
1024 doi.doi_data_block_size)) != 0)
1027 if (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY)) {
1032 error = zvol_update_volsize(zv, maj, volsize);
1036 * Reinitialize the dump area to the new size. If we
1037 * failed to resize the dump area then restore the it back to
1038 * it's original size.
1040 if (error == 0 && zv->zv_flags & ZVOL_DUMPIFIED) {
1041 if ((error = zvol_dumpify(zv)) != 0 ||
1042 (error = dumpvp_resize()) != 0) {
1043 (void) zvol_update_volsize(zv, maj, old_volsize);
1044 error = zvol_dumpify(zv);
1050 if (state.zv_objset)
1051 dmu_objset_close(state.zv_objset);
1053 mutex_exit(&zvol_state_lock);
1054 g_topology_unlock();
1061 zvol_set_volblocksize(const char *name, uint64_t volblocksize)
1069 mutex_enter(&zvol_state_lock);
1071 if ((zv = zvol_minor_lookup(name)) == NULL) {
1075 if (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY)) {
1080 tx = dmu_tx_create(zv->zv_objset);
1081 dmu_tx_hold_bonus(tx, ZVOL_OBJ);
1082 error = dmu_tx_assign(tx, TXG_WAIT);
1086 error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ,
1087 volblocksize, 0, tx);
1088 if (error == ENOTSUP)
1093 mutex_exit(&zvol_state_lock);
1094 g_topology_unlock();
1101 zvol_get_done(dmu_buf_t *db, void *vzgd)
1103 zgd_t *zgd = (zgd_t *)vzgd;
1104 rl_t *rl = zgd->zgd_rl;
1106 dmu_buf_rele(db, vzgd);
1107 zfs_range_unlock(rl);
1108 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1109 kmem_free(zgd, sizeof (zgd_t));
1113 * Get data to generate a TX_WRITE intent log record.
1116 zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1118 zvol_state_t *zv = arg;
1119 objset_t *os = zv->zv_objset;
1123 uint64_t boff; /* block starting offset */
1124 int dlen = lr->lr_length; /* length of user data */
1131 * Write records come in two flavors: immediate and indirect.
1132 * For small writes it's cheaper to store the data with the
1133 * log record (immediate); for large writes it's cheaper to
1134 * sync the data and get a pointer to it (indirect) so that
1135 * we don't have to write the data twice.
1137 if (buf != NULL) /* immediate write */
1138 return (dmu_read(os, ZVOL_OBJ, lr->lr_offset, dlen, buf));
1140 zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP);
1141 zgd->zgd_zilog = zv->zv_zilog;
1142 zgd->zgd_bp = &lr->lr_blkptr;
1145 * Lock the range of the block to ensure that when the data is
1146 * written out and its checksum is being calculated that no other
1147 * thread can change the block.
1149 boff = P2ALIGN_TYPED(lr->lr_offset, zv->zv_volblocksize, uint64_t);
1150 rl = zfs_range_lock(&zv->zv_znode, boff, zv->zv_volblocksize,
1154 VERIFY(0 == dmu_buf_hold(os, ZVOL_OBJ, lr->lr_offset, zgd, &db));
1155 error = dmu_sync(zio, db, &lr->lr_blkptr,
1156 lr->lr_common.lrc_txg, zvol_get_done, zgd);
1158 zil_add_block(zv->zv_zilog, &lr->lr_blkptr);
1160 * If we get EINPROGRESS, then we need to wait for a
1161 * write IO initiated by dmu_sync() to complete before
1162 * we can release this dbuf. We will finish everything
1163 * up in the zvol_get_done() callback.
1165 if (error == EINPROGRESS)
1167 dmu_buf_rele(db, zgd);
1168 zfs_range_unlock(rl);
1169 kmem_free(zgd, sizeof (zgd_t));
1176 return (zvol_minors != 0);
1182 mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
1183 ZFS_LOG(1, "ZVOL Initialized.");
1189 mutex_destroy(&zvol_state_lock);
1190 ZFS_LOG(1, "ZVOL Deinitialized.");
1194 zvol_is_swap(zvol_state_t *zv)
1197 boolean_t ret = B_FALSE;
1203 devpathlen = strlen(ZVOL_FULL_DEV_DIR) + strlen(zv->zv_name) + 1;
1204 devpath = kmem_alloc(devpathlen, KM_SLEEP);
1205 (void) sprintf(devpath, "%s%s", ZVOL_FULL_DEV_DIR, zv->zv_name);
1206 error = lookupname(devpath, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp);
1207 kmem_free(devpath, devpathlen);
1209 ret = !error && IS_SWAPVP(common_specvp(vp));
1219 zvol_dump_init(zvol_state_t *zv, boolean_t resize)
1223 objset_t *os = zv->zv_objset;
1224 nvlist_t *nv = NULL;
1225 uint64_t checksum, compress, refresrv;
1227 ASSERT(MUTEX_HELD(&zvol_state_lock));
1229 tx = dmu_tx_create(os);
1230 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1231 error = dmu_tx_assign(tx, TXG_WAIT);
1238 * If we are resizing the dump device then we only need to
1239 * update the refreservation to match the newly updated
1240 * zvolsize. Otherwise, we save off the original state of the
1241 * zvol so that we can restore them if the zvol is ever undumpified.
1244 error = zap_update(os, ZVOL_ZAP_OBJ,
1245 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1246 &zv->zv_volsize, tx);
1248 error = dsl_prop_get_integer(zv->zv_name,
1249 zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL);
1250 error = error ? error : dsl_prop_get_integer(zv->zv_name,
1251 zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL);
1252 error = error ? error : dsl_prop_get_integer(zv->zv_name,
1253 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL);
1255 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1256 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1,
1258 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1259 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx);
1260 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1261 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1266 /* Truncate the file */
1268 error = dmu_free_long_range(zv->zv_objset,
1269 ZVOL_OBJ, 0, DMU_OBJECT_END);
1275 * We only need update the zvol's property if we are initializing
1276 * the dump area for the first time.
1279 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1280 VERIFY(nvlist_add_uint64(nv,
1281 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0);
1282 VERIFY(nvlist_add_uint64(nv,
1283 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
1284 ZIO_COMPRESS_OFF) == 0);
1285 VERIFY(nvlist_add_uint64(nv,
1286 zfs_prop_to_name(ZFS_PROP_CHECKSUM),
1287 ZIO_CHECKSUM_OFF) == 0);
1289 error = zfs_set_prop_nvlist(zv->zv_name, nv);
1296 /* Allocate the space for the dump */
1297 error = zvol_prealloc(zv);
1302 zvol_dumpify(zvol_state_t *zv)
1305 uint64_t dumpsize = 0;
1307 objset_t *os = zv->zv_objset;
1309 if (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY))
1313 * We do not support swap devices acting as dump devices.
1315 if (zvol_is_swap(zv))
1318 if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE,
1319 8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) {
1320 boolean_t resize = (dumpsize > 0) ? B_TRUE : B_FALSE;
1322 if ((error = zvol_dump_init(zv, resize)) != 0) {
1323 (void) zvol_dump_fini(zv);
1329 * Build up our lba mapping.
1331 error = zvol_get_lbas(zv);
1333 (void) zvol_dump_fini(zv);
1337 tx = dmu_tx_create(os);
1338 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1339 error = dmu_tx_assign(tx, TXG_WAIT);
1342 (void) zvol_dump_fini(zv);
1346 zv->zv_flags |= ZVOL_DUMPIFIED;
1347 error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1,
1348 &zv->zv_volsize, tx);
1352 (void) zvol_dump_fini(zv);
1356 txg_wait_synced(dmu_objset_pool(os), 0);
1361 zvol_dump_fini(zvol_state_t *zv)
1364 objset_t *os = zv->zv_objset;
1367 uint64_t checksum, compress, refresrv;
1370 * Attempt to restore the zvol back to its pre-dumpified state.
1371 * This is a best-effort attempt as it's possible that not all
1372 * of these properties were initialized during the dumpify process
1373 * (i.e. error during zvol_dump_init).
1376 tx = dmu_tx_create(os);
1377 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1378 error = dmu_tx_assign(tx, TXG_WAIT);
1383 (void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx);
1386 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1387 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum);
1388 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1389 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress);
1390 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
1391 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv);
1393 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1394 (void) nvlist_add_uint64(nv,
1395 zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum);
1396 (void) nvlist_add_uint64(nv,
1397 zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress);
1398 (void) nvlist_add_uint64(nv,
1399 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv);
1400 (void) zfs_set_prop_nvlist(zv->zv_name, nv);
1403 zvol_free_extents(zv);
1404 zv->zv_flags &= ~ZVOL_DUMPIFIED;
1405 (void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END);