1700 Add SCSI UNMAP support
[unleashed.git] / usr / src / uts / common / fs / zfs / zvol.c
blobe44423dee78a87b971eeacfe483edc22d352014d
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Portions Copyright 2010 Robert Milkowski
26 * Copyright 2017 Nexenta Systems, Inc. All rights reserved.
27 * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
28 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
29 * Copyright (c) 2014 Integros [integros.com]
33 * ZFS volume emulation driver.
35 * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes.
36 * Volumes are accessed through the symbolic links named:
38 * /dev/zvol/dsk/<pool_name>/<dataset_name>
39 * /dev/zvol/rdsk/<pool_name>/<dataset_name>
41 * These links are created by the /dev filesystem (sdev_zvolops.c).
42 * Volumes are persistent through reboot. No user command needs to be
43 * run before opening and using a device.
46 #include <sys/types.h>
47 #include <sys/param.h>
48 #include <sys/errno.h>
49 #include <sys/uio.h>
50 #include <sys/buf.h>
51 #include <sys/modctl.h>
52 #include <sys/open.h>
53 #include <sys/kmem.h>
54 #include <sys/conf.h>
55 #include <sys/cmn_err.h>
56 #include <sys/stat.h>
57 #include <sys/zap.h>
58 #include <sys/spa.h>
59 #include <sys/spa_impl.h>
60 #include <sys/zio.h>
61 #include <sys/dmu_traverse.h>
62 #include <sys/dnode.h>
63 #include <sys/dsl_dataset.h>
64 #include <sys/dsl_prop.h>
65 #include <sys/dkio.h>
66 #include <sys/efi_partition.h>
67 #include <sys/byteorder.h>
68 #include <sys/pathname.h>
69 #include <sys/ddi.h>
70 #include <sys/sunddi.h>
71 #include <sys/crc32.h>
72 #include <sys/dirent.h>
73 #include <sys/policy.h>
74 #include <sys/fs/zfs.h>
75 #include <sys/zfs_ioctl.h>
76 #include <sys/mkdev.h>
77 #include <sys/zil.h>
78 #include <sys/refcount.h>
79 #include <sys/zfs_znode.h>
80 #include <sys/zfs_rlock.h>
81 #include <sys/vdev_disk.h>
82 #include <sys/vdev_impl.h>
83 #include <sys/vdev_raidz.h>
84 #include <sys/zvol.h>
85 #include <sys/dumphdr.h>
86 #include <sys/zil_impl.h>
87 #include <sys/dbuf.h>
88 #include <sys/dmu_tx.h>
89 #include <sys/zfeature.h>
90 #include <sys/zio_checksum.h>
91 #include <sys/zil_impl.h>
92 #include <sys/dkioc_free_util.h>
94 #include "zfs_namecheck.h"
96 void *zfsdev_state;
97 static char *zvol_tag = "zvol_tag";
99 #define ZVOL_DUMPSIZE "dumpsize"
102 * This lock protects the zfsdev_state structure from being modified
103 * while it's being used, e.g. an open that comes in before a create
104 * finishes. It also protects temporary opens of the dataset so that,
105 * e.g., an open doesn't get a spurious EBUSY.
107 kmutex_t zfsdev_state_lock;
108 static uint32_t zvol_minors;
110 typedef struct zvol_extent {
111 list_node_t ze_node;
112 dva_t ze_dva; /* dva associated with this extent */
113 uint64_t ze_nblks; /* number of blocks in extent */
114 } zvol_extent_t;
117 * The in-core state of each volume.
119 typedef struct zvol_state {
120 char zv_name[MAXPATHLEN]; /* pool/dd name */
121 uint64_t zv_volsize; /* amount of space we advertise */
122 uint64_t zv_volblocksize; /* volume block size */
123 minor_t zv_minor; /* minor number */
124 uint8_t zv_min_bs; /* minimum addressable block shift */
125 uint8_t zv_flags; /* readonly, dumpified, etc. */
126 objset_t *zv_objset; /* objset handle */
127 uint32_t zv_open_count[OTYPCNT]; /* open counts */
128 uint32_t zv_total_opens; /* total open count */
129 zilog_t *zv_zilog; /* ZIL handle */
130 list_t zv_extents; /* List of extents for dump */
131 znode_t zv_znode; /* for range locking */
132 dnode_t *zv_dn; /* dnode hold */
133 } zvol_state_t;
136 * zvol specific flags
138 #define ZVOL_RDONLY 0x1
139 #define ZVOL_DUMPIFIED 0x2
140 #define ZVOL_EXCL 0x4
141 #define ZVOL_WCE 0x8
144 * zvol maximum transfer in one DMU tx.
146 int zvol_maxphys = DMU_MAX_ACCESS/2;
149 * Toggle unmap functionality.
151 boolean_t zvol_unmap_enabled = B_TRUE;
154 * If true, unmaps requested as synchronous are executed synchronously,
155 * otherwise all unmaps are asynchronous.
157 boolean_t zvol_unmap_sync_enabled = B_FALSE;
159 extern int zfs_set_prop_nvlist(const char *, zprop_source_t,
160 nvlist_t *, nvlist_t *);
161 static int zvol_remove_zv(zvol_state_t *);
162 static int zvol_get_data(void *arg, lr_write_t *lr, char *buf,
163 struct lwb *lwb, zio_t *zio);
164 static int zvol_dumpify(zvol_state_t *zv);
165 static int zvol_dump_fini(zvol_state_t *zv);
166 static int zvol_dump_init(zvol_state_t *zv, boolean_t resize);
168 static void
169 zvol_size_changed(zvol_state_t *zv, uint64_t volsize)
171 dev_t dev = makedevice(ddi_driver_major(zfs_dip), zv->zv_minor);
173 zv->zv_volsize = volsize;
174 VERIFY(ddi_prop_update_int64(dev, zfs_dip,
175 "Size", volsize) == DDI_SUCCESS);
176 VERIFY(ddi_prop_update_int64(dev, zfs_dip,
177 "Nblocks", lbtodb(volsize)) == DDI_SUCCESS);
179 /* Notify specfs to invalidate the cached size */
180 spec_size_invalidate(dev, VBLK);
181 spec_size_invalidate(dev, VCHR);
185 zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
187 if (volsize == 0)
188 return (SET_ERROR(EINVAL));
190 if (volsize % blocksize != 0)
191 return (SET_ERROR(EINVAL));
193 #ifdef _ILP32
194 if (volsize - 1 > SPEC_MAXOFFSET_T)
195 return (SET_ERROR(EOVERFLOW));
196 #endif
197 return (0);
201 zvol_check_volblocksize(uint64_t volblocksize)
203 if (volblocksize < SPA_MINBLOCKSIZE ||
204 volblocksize > SPA_OLD_MAXBLOCKSIZE ||
205 !ISP2(volblocksize))
206 return (SET_ERROR(EDOM));
208 return (0);
212 zvol_get_stats(objset_t *os, nvlist_t *nv)
214 int error;
215 dmu_object_info_t doi;
216 uint64_t val;
218 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
219 if (error)
220 return (error);
222 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
224 error = dmu_object_info(os, ZVOL_OBJ, &doi);
226 if (error == 0) {
227 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
228 doi.doi_data_block_size);
231 return (error);
234 static zvol_state_t *
235 zvol_minor_lookup(const char *name)
237 minor_t minor;
238 zvol_state_t *zv;
240 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
242 for (minor = 1; minor <= ZFSDEV_MAX_MINOR; minor++) {
243 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
244 if (zv == NULL)
245 continue;
246 if (strcmp(zv->zv_name, name) == 0)
247 return (zv);
250 return (NULL);
253 /* extent mapping arg */
254 struct maparg {
255 zvol_state_t *ma_zv;
256 uint64_t ma_blks;
259 /*ARGSUSED*/
260 static int
261 zvol_map_block(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
262 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
264 struct maparg *ma = arg;
265 zvol_extent_t *ze;
266 int bs = ma->ma_zv->zv_volblocksize;
268 if (bp == NULL || BP_IS_HOLE(bp) ||
269 zb->zb_object != ZVOL_OBJ || zb->zb_level != 0)
270 return (0);
272 VERIFY(!BP_IS_EMBEDDED(bp));
274 VERIFY3U(ma->ma_blks, ==, zb->zb_blkid);
275 ma->ma_blks++;
277 /* Abort immediately if we have encountered gang blocks */
278 if (BP_IS_GANG(bp))
279 return (SET_ERROR(EFRAGS));
282 * See if the block is at the end of the previous extent.
284 ze = list_tail(&ma->ma_zv->zv_extents);
285 if (ze &&
286 DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) &&
287 DVA_GET_OFFSET(BP_IDENTITY(bp)) ==
288 DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) {
289 ze->ze_nblks++;
290 return (0);
293 dprintf_bp(bp, "%s", "next blkptr:");
295 /* start a new extent */
296 ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP);
297 ze->ze_dva = bp->blk_dva[0]; /* structure assignment */
298 ze->ze_nblks = 1;
299 list_insert_tail(&ma->ma_zv->zv_extents, ze);
300 return (0);
303 static void
304 zvol_free_extents(zvol_state_t *zv)
306 zvol_extent_t *ze;
308 while (ze = list_head(&zv->zv_extents)) {
309 list_remove(&zv->zv_extents, ze);
310 kmem_free(ze, sizeof (zvol_extent_t));
314 static int
315 zvol_get_lbas(zvol_state_t *zv)
317 objset_t *os = zv->zv_objset;
318 struct maparg ma;
319 int err;
321 ma.ma_zv = zv;
322 ma.ma_blks = 0;
323 zvol_free_extents(zv);
325 /* commit any in-flight changes before traversing the dataset */
326 txg_wait_synced(dmu_objset_pool(os), 0);
327 err = traverse_dataset(dmu_objset_ds(os), 0,
328 TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma);
329 if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) {
330 zvol_free_extents(zv);
331 return (err ? err : EIO);
334 return (0);
337 /* ARGSUSED */
338 void
339 zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
341 zfs_creat_t *zct = arg;
342 nvlist_t *nvprops = zct->zct_props;
343 int error;
344 uint64_t volblocksize, volsize;
346 VERIFY(nvlist_lookup_uint64(nvprops,
347 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
348 if (nvlist_lookup_uint64(nvprops,
349 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
350 volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
353 * These properties must be removed from the list so the generic
354 * property setting step won't apply to them.
356 VERIFY(nvlist_remove_all(nvprops,
357 zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
358 (void) nvlist_remove_all(nvprops,
359 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
361 error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
362 DMU_OT_NONE, 0, tx);
363 ASSERT(error == 0);
365 error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
366 DMU_OT_NONE, 0, tx);
367 ASSERT(error == 0);
369 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
370 ASSERT(error == 0);
374 * Replay a TX_TRUNCATE ZIL transaction if asked. TX_TRUNCATE is how we
375 * implement DKIOCFREE/free-long-range.
377 static int
378 zvol_replay_truncate(void *arg1, void *arg2, boolean_t byteswap)
380 zvol_state_t *zv = arg1;
381 lr_truncate_t *lr = arg2;
382 uint64_t offset, length;
384 if (byteswap)
385 byteswap_uint64_array(lr, sizeof (*lr));
387 offset = lr->lr_offset;
388 length = lr->lr_length;
390 return (dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, offset, length));
394 * Replay a TX_WRITE ZIL transaction that didn't get committed
395 * after a system failure
397 static int
398 zvol_replay_write(void *arg1, void *arg2, boolean_t byteswap)
400 zvol_state_t *zv = arg1;
401 lr_write_t *lr = arg2;
402 objset_t *os = zv->zv_objset;
403 char *data = (char *)(lr + 1); /* data follows lr_write_t */
404 uint64_t offset, length;
405 dmu_tx_t *tx;
406 int error;
408 if (byteswap)
409 byteswap_uint64_array(lr, sizeof (*lr));
411 offset = lr->lr_offset;
412 length = lr->lr_length;
414 /* If it's a dmu_sync() block, write the whole block */
415 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
416 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
417 if (length < blocksize) {
418 offset -= offset % blocksize;
419 length = blocksize;
423 tx = dmu_tx_create(os);
424 dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length);
425 error = dmu_tx_assign(tx, TXG_WAIT);
426 if (error) {
427 dmu_tx_abort(tx);
428 } else {
429 dmu_write(os, ZVOL_OBJ, offset, length, data, tx);
430 dmu_tx_commit(tx);
433 return (error);
436 /* ARGSUSED */
437 static int
438 zvol_replay_err(void *arg1, void *arg2, boolean_t byteswap)
440 return (SET_ERROR(ENOTSUP));
444 * Callback vectors for replaying records.
445 * Only TX_WRITE and TX_TRUNCATE are needed for zvol.
447 zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = {
448 zvol_replay_err, /* 0 no such transaction type */
449 zvol_replay_err, /* TX_CREATE */
450 zvol_replay_err, /* TX_MKDIR */
451 zvol_replay_err, /* TX_MKXATTR */
452 zvol_replay_err, /* TX_SYMLINK */
453 zvol_replay_err, /* TX_REMOVE */
454 zvol_replay_err, /* TX_RMDIR */
455 zvol_replay_err, /* TX_LINK */
456 zvol_replay_err, /* TX_RENAME */
457 zvol_replay_write, /* TX_WRITE */
458 zvol_replay_truncate, /* TX_TRUNCATE */
459 zvol_replay_err, /* TX_SETATTR */
460 zvol_replay_err, /* TX_ACL */
461 zvol_replay_err, /* TX_CREATE_ACL */
462 zvol_replay_err, /* TX_CREATE_ATTR */
463 zvol_replay_err, /* TX_CREATE_ACL_ATTR */
464 zvol_replay_err, /* TX_MKDIR_ACL */
465 zvol_replay_err, /* TX_MKDIR_ATTR */
466 zvol_replay_err, /* TX_MKDIR_ACL_ATTR */
467 zvol_replay_err, /* TX_WRITE2 */
471 zvol_name2minor(const char *name, minor_t *minor)
473 zvol_state_t *zv;
475 mutex_enter(&zfsdev_state_lock);
476 zv = zvol_minor_lookup(name);
477 if (minor && zv)
478 *minor = zv->zv_minor;
479 mutex_exit(&zfsdev_state_lock);
480 return (zv ? 0 : -1);
484 * Create a minor node (plus a whole lot more) for the specified volume.
487 zvol_create_minor(const char *name)
489 zfs_soft_state_t *zs;
490 zvol_state_t *zv;
491 objset_t *os;
492 dmu_object_info_t doi;
493 minor_t minor = 0;
494 char chrbuf[30], blkbuf[30];
495 int error;
497 mutex_enter(&zfsdev_state_lock);
499 if (zvol_minor_lookup(name) != NULL) {
500 mutex_exit(&zfsdev_state_lock);
501 return (SET_ERROR(EEXIST));
504 /* lie and say we're read-only */
505 error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, FTAG, &os);
507 if (error) {
508 mutex_exit(&zfsdev_state_lock);
509 return (error);
512 if ((minor = zfsdev_minor_alloc()) == 0) {
513 dmu_objset_disown(os, FTAG);
514 mutex_exit(&zfsdev_state_lock);
515 return (SET_ERROR(ENXIO));
518 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS) {
519 dmu_objset_disown(os, FTAG);
520 mutex_exit(&zfsdev_state_lock);
521 return (SET_ERROR(EAGAIN));
523 (void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME,
524 (char *)name);
526 (void) snprintf(chrbuf, sizeof (chrbuf), "%u,raw", minor);
528 if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR,
529 minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
530 ddi_soft_state_free(zfsdev_state, minor);
531 dmu_objset_disown(os, FTAG);
532 mutex_exit(&zfsdev_state_lock);
533 return (SET_ERROR(EAGAIN));
536 (void) snprintf(blkbuf, sizeof (blkbuf), "%u", minor);
538 if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK,
539 minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
540 ddi_remove_minor_node(zfs_dip, chrbuf);
541 ddi_soft_state_free(zfsdev_state, minor);
542 dmu_objset_disown(os, FTAG);
543 mutex_exit(&zfsdev_state_lock);
544 return (SET_ERROR(EAGAIN));
547 zs = ddi_get_soft_state(zfsdev_state, minor);
548 zs->zss_type = ZSST_ZVOL;
549 zv = zs->zss_data = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP);
550 (void) strlcpy(zv->zv_name, name, MAXPATHLEN);
551 zv->zv_min_bs = DEV_BSHIFT;
552 zv->zv_minor = minor;
553 zv->zv_objset = os;
554 if (dmu_objset_is_snapshot(os) || !spa_writeable(dmu_objset_spa(os)))
555 zv->zv_flags |= ZVOL_RDONLY;
556 mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
557 avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
558 sizeof (rl_t), offsetof(rl_t, r_node));
559 list_create(&zv->zv_extents, sizeof (zvol_extent_t),
560 offsetof(zvol_extent_t, ze_node));
561 /* get and cache the blocksize */
562 error = dmu_object_info(os, ZVOL_OBJ, &doi);
563 ASSERT(error == 0);
564 zv->zv_volblocksize = doi.doi_data_block_size;
566 if (spa_writeable(dmu_objset_spa(os))) {
567 if (zil_replay_disable)
568 zil_destroy(dmu_objset_zil(os), B_FALSE);
569 else
570 zil_replay(os, zv, zvol_replay_vector);
572 dmu_objset_disown(os, FTAG);
573 zv->zv_objset = NULL;
575 zvol_minors++;
577 mutex_exit(&zfsdev_state_lock);
579 return (0);
583 * Remove minor node for the specified volume.
585 static int
586 zvol_remove_zv(zvol_state_t *zv)
588 char nmbuf[20];
589 minor_t minor = zv->zv_minor;
591 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
592 if (zv->zv_total_opens != 0)
593 return (SET_ERROR(EBUSY));
595 (void) snprintf(nmbuf, sizeof (nmbuf), "%u,raw", minor);
596 ddi_remove_minor_node(zfs_dip, nmbuf);
598 (void) snprintf(nmbuf, sizeof (nmbuf), "%u", minor);
599 ddi_remove_minor_node(zfs_dip, nmbuf);
601 avl_destroy(&zv->zv_znode.z_range_avl);
602 mutex_destroy(&zv->zv_znode.z_range_lock);
604 kmem_free(zv, sizeof (zvol_state_t));
606 ddi_soft_state_free(zfsdev_state, minor);
608 zvol_minors--;
609 return (0);
613 zvol_remove_minor(const char *name)
615 zvol_state_t *zv;
616 int rc;
618 mutex_enter(&zfsdev_state_lock);
619 if ((zv = zvol_minor_lookup(name)) == NULL) {
620 mutex_exit(&zfsdev_state_lock);
621 return (SET_ERROR(ENXIO));
623 rc = zvol_remove_zv(zv);
624 mutex_exit(&zfsdev_state_lock);
625 return (rc);
629 zvol_first_open(zvol_state_t *zv)
631 objset_t *os;
632 uint64_t volsize;
633 int error;
634 uint64_t readonly;
636 /* lie and say we're read-only */
637 error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, B_TRUE,
638 zvol_tag, &os);
639 if (error)
640 return (error);
642 zv->zv_objset = os;
643 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
644 if (error) {
645 ASSERT(error == 0);
646 dmu_objset_disown(os, zvol_tag);
647 return (error);
650 error = dnode_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dn);
651 if (error) {
652 dmu_objset_disown(os, zvol_tag);
653 return (error);
656 zvol_size_changed(zv, volsize);
657 zv->zv_zilog = zil_open(os, zvol_get_data);
659 VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &readonly,
660 NULL) == 0);
661 if (readonly || dmu_objset_is_snapshot(os) ||
662 !spa_writeable(dmu_objset_spa(os)))
663 zv->zv_flags |= ZVOL_RDONLY;
664 else
665 zv->zv_flags &= ~ZVOL_RDONLY;
666 return (error);
669 void
670 zvol_last_close(zvol_state_t *zv)
672 zil_close(zv->zv_zilog);
673 zv->zv_zilog = NULL;
675 dnode_rele(zv->zv_dn, zvol_tag);
676 zv->zv_dn = NULL;
679 * Evict cached data
681 if (dsl_dataset_is_dirty(dmu_objset_ds(zv->zv_objset)) &&
682 !(zv->zv_flags & ZVOL_RDONLY))
683 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
684 dmu_objset_evict_dbufs(zv->zv_objset);
686 dmu_objset_disown(zv->zv_objset, zvol_tag);
687 zv->zv_objset = NULL;
691 zvol_prealloc(zvol_state_t *zv)
693 objset_t *os = zv->zv_objset;
694 dmu_tx_t *tx;
695 uint64_t refd, avail, usedobjs, availobjs;
696 uint64_t resid = zv->zv_volsize;
697 uint64_t off = 0;
699 /* Check the space usage before attempting to allocate the space */
700 dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs);
701 if (avail < zv->zv_volsize)
702 return (SET_ERROR(ENOSPC));
704 /* Free old extents if they exist */
705 zvol_free_extents(zv);
707 while (resid != 0) {
708 int error;
709 uint64_t bytes = MIN(resid, SPA_OLD_MAXBLOCKSIZE);
711 tx = dmu_tx_create(os);
712 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
713 error = dmu_tx_assign(tx, TXG_WAIT);
714 if (error) {
715 dmu_tx_abort(tx);
716 (void) dmu_free_long_range(os, ZVOL_OBJ, 0, off);
717 return (error);
719 dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx);
720 dmu_tx_commit(tx);
721 off += bytes;
722 resid -= bytes;
724 txg_wait_synced(dmu_objset_pool(os), 0);
726 return (0);
729 static int
730 zvol_update_volsize(objset_t *os, uint64_t volsize)
732 dmu_tx_t *tx;
733 int error;
735 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
737 tx = dmu_tx_create(os);
738 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
739 dmu_tx_mark_netfree(tx);
740 error = dmu_tx_assign(tx, TXG_WAIT);
741 if (error) {
742 dmu_tx_abort(tx);
743 return (error);
746 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
747 &volsize, tx);
748 dmu_tx_commit(tx);
750 if (error == 0)
751 error = dmu_free_long_range(os,
752 ZVOL_OBJ, volsize, DMU_OBJECT_END);
753 return (error);
756 void
757 zvol_remove_minors(const char *name)
759 zvol_state_t *zv;
760 char *namebuf;
761 minor_t minor;
763 namebuf = kmem_zalloc(strlen(name) + 2, KM_SLEEP);
764 (void) strncpy(namebuf, name, strlen(name));
765 (void) strcat(namebuf, "/");
766 mutex_enter(&zfsdev_state_lock);
767 for (minor = 1; minor <= ZFSDEV_MAX_MINOR; minor++) {
769 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
770 if (zv == NULL)
771 continue;
772 if (strncmp(namebuf, zv->zv_name, strlen(namebuf)) == 0)
773 (void) zvol_remove_zv(zv);
775 kmem_free(namebuf, strlen(name) + 2);
777 mutex_exit(&zfsdev_state_lock);
780 static int
781 zvol_update_live_volsize(zvol_state_t *zv, uint64_t volsize)
783 uint64_t old_volsize = 0ULL;
784 int error = 0;
786 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
789 * Reinitialize the dump area to the new size. If we
790 * failed to resize the dump area then restore it back to
791 * its original size. We must set the new volsize prior
792 * to calling dumpvp_resize() to ensure that the devices'
793 * size(9P) is not visible by the dump subsystem.
795 old_volsize = zv->zv_volsize;
796 zvol_size_changed(zv, volsize);
798 if (zv->zv_flags & ZVOL_DUMPIFIED) {
799 if ((error = zvol_dumpify(zv)) != 0 ||
800 (error = dumpvp_resize()) != 0) {
801 int dumpify_error;
803 (void) zvol_update_volsize(zv->zv_objset, old_volsize);
804 zvol_size_changed(zv, old_volsize);
805 dumpify_error = zvol_dumpify(zv);
806 error = dumpify_error ? dumpify_error : error;
811 * Generate a LUN expansion event.
813 if (error == 0) {
814 sysevent_id_t eid;
815 nvlist_t *attr;
816 char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
818 (void) snprintf(physpath, MAXPATHLEN, "%s%u", ZVOL_PSEUDO_DEV,
819 zv->zv_minor);
821 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
822 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
824 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
825 ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
827 nvlist_free(attr);
828 kmem_free(physpath, MAXPATHLEN);
830 return (error);
834 zvol_set_volsize(const char *name, uint64_t volsize)
836 zvol_state_t *zv = NULL;
837 objset_t *os;
838 int error;
839 dmu_object_info_t doi;
840 uint64_t readonly;
841 boolean_t owned = B_FALSE;
843 error = dsl_prop_get_integer(name,
844 zfs_prop_to_name(ZFS_PROP_READONLY), &readonly, NULL);
845 if (error != 0)
846 return (error);
847 if (readonly)
848 return (SET_ERROR(EROFS));
850 mutex_enter(&zfsdev_state_lock);
851 zv = zvol_minor_lookup(name);
853 if (zv == NULL || zv->zv_objset == NULL) {
854 if ((error = dmu_objset_own(name, DMU_OST_ZVOL, B_FALSE,
855 FTAG, &os)) != 0) {
856 mutex_exit(&zfsdev_state_lock);
857 return (error);
859 owned = B_TRUE;
860 if (zv != NULL)
861 zv->zv_objset = os;
862 } else {
863 os = zv->zv_objset;
866 if ((error = dmu_object_info(os, ZVOL_OBJ, &doi)) != 0 ||
867 (error = zvol_check_volsize(volsize, doi.doi_data_block_size)) != 0)
868 goto out;
870 error = zvol_update_volsize(os, volsize);
872 if (error == 0 && zv != NULL)
873 error = zvol_update_live_volsize(zv, volsize);
874 out:
875 if (owned) {
876 dmu_objset_disown(os, FTAG);
877 if (zv != NULL)
878 zv->zv_objset = NULL;
880 mutex_exit(&zfsdev_state_lock);
881 return (error);
884 /*ARGSUSED*/
886 zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr)
888 zvol_state_t *zv;
889 int err = 0;
891 mutex_enter(&zfsdev_state_lock);
893 zv = zfsdev_get_soft_state(getminor(*devp), ZSST_ZVOL);
894 if (zv == NULL) {
895 mutex_exit(&zfsdev_state_lock);
896 return (SET_ERROR(ENXIO));
899 if (zv->zv_total_opens == 0)
900 err = zvol_first_open(zv);
901 if (err) {
902 mutex_exit(&zfsdev_state_lock);
903 return (err);
905 if ((flag & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
906 err = SET_ERROR(EROFS);
907 goto out;
909 if (zv->zv_flags & ZVOL_EXCL) {
910 err = SET_ERROR(EBUSY);
911 goto out;
913 if (flag & FEXCL) {
914 if (zv->zv_total_opens != 0) {
915 err = SET_ERROR(EBUSY);
916 goto out;
918 zv->zv_flags |= ZVOL_EXCL;
921 if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) {
922 zv->zv_open_count[otyp]++;
923 zv->zv_total_opens++;
925 mutex_exit(&zfsdev_state_lock);
927 return (err);
928 out:
929 if (zv->zv_total_opens == 0)
930 zvol_last_close(zv);
931 mutex_exit(&zfsdev_state_lock);
932 return (err);
935 /*ARGSUSED*/
937 zvol_close(dev_t dev, int flag, int otyp, cred_t *cr)
939 minor_t minor = getminor(dev);
940 zvol_state_t *zv;
941 int error = 0;
943 mutex_enter(&zfsdev_state_lock);
945 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
946 if (zv == NULL) {
947 mutex_exit(&zfsdev_state_lock);
948 return (SET_ERROR(ENXIO));
951 if (zv->zv_flags & ZVOL_EXCL) {
952 ASSERT(zv->zv_total_opens == 1);
953 zv->zv_flags &= ~ZVOL_EXCL;
957 * If the open count is zero, this is a spurious close.
958 * That indicates a bug in the kernel / DDI framework.
960 ASSERT(zv->zv_open_count[otyp] != 0);
961 ASSERT(zv->zv_total_opens != 0);
964 * You may get multiple opens, but only one close.
966 zv->zv_open_count[otyp]--;
967 zv->zv_total_opens--;
969 if (zv->zv_total_opens == 0)
970 zvol_last_close(zv);
972 mutex_exit(&zfsdev_state_lock);
973 return (error);
976 static void
977 zvol_get_done(zgd_t *zgd, int error)
979 if (zgd->zgd_db)
980 dmu_buf_rele(zgd->zgd_db, zgd);
982 zfs_range_unlock(zgd->zgd_rl);
984 if (error == 0 && zgd->zgd_bp)
985 zil_lwb_add_block(zgd->zgd_lwb, zgd->zgd_bp);
987 kmem_free(zgd, sizeof (zgd_t));
991 * Get data to generate a TX_WRITE intent log record.
993 static int
994 zvol_get_data(void *arg, lr_write_t *lr, char *buf, struct lwb *lwb, zio_t *zio)
996 zvol_state_t *zv = arg;
997 uint64_t offset = lr->lr_offset;
998 uint64_t size = lr->lr_length; /* length of user data */
999 dmu_buf_t *db;
1000 zgd_t *zgd;
1001 int error;
1003 ASSERT3P(lwb, !=, NULL);
1004 ASSERT3P(zio, !=, NULL);
1005 ASSERT3U(size, !=, 0);
1007 zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1008 zgd->zgd_lwb = lwb;
1011 * Write records come in two flavors: immediate and indirect.
1012 * For small writes it's cheaper to store the data with the
1013 * log record (immediate); for large writes it's cheaper to
1014 * sync the data and get a pointer to it (indirect) so that
1015 * we don't have to write the data twice.
1017 if (buf != NULL) { /* immediate write */
1018 zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size,
1019 RL_READER);
1020 error = dmu_read_by_dnode(zv->zv_dn, offset, size, buf,
1021 DMU_READ_NO_PREFETCH);
1022 } else { /* indirect write */
1024 * Have to lock the whole block to ensure when it's written out
1025 * and its checksum is being calculated that no one can change
1026 * the data. Contrarily to zfs_get_data we need not re-check
1027 * blocksize after we get the lock because it cannot be changed.
1029 size = zv->zv_volblocksize;
1030 offset = P2ALIGN(offset, size);
1031 zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size,
1032 RL_READER);
1033 error = dmu_buf_hold_by_dnode(zv->zv_dn, offset, zgd, &db,
1034 DMU_READ_NO_PREFETCH);
1035 if (error == 0) {
1036 blkptr_t *bp = &lr->lr_blkptr;
1038 zgd->zgd_db = db;
1039 zgd->zgd_bp = bp;
1041 ASSERT(db->db_offset == offset);
1042 ASSERT(db->db_size == size);
1044 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1045 zvol_get_done, zgd);
1047 if (error == 0)
1048 return (0);
1052 zvol_get_done(zgd, error);
1054 return (error);
1058 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
1060 * We store data in the log buffers if it's small enough.
1061 * Otherwise we will later flush the data out via dmu_sync().
1063 ssize_t zvol_immediate_write_sz = 32768;
1065 static void
1066 zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid,
1067 boolean_t sync)
1069 uint32_t blocksize = zv->zv_volblocksize;
1070 zilog_t *zilog = zv->zv_zilog;
1071 itx_wr_state_t write_state;
1073 if (zil_replaying(zilog, tx))
1074 return;
1076 if (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
1077 write_state = WR_INDIRECT;
1078 else if (!spa_has_slogs(zilog->zl_spa) &&
1079 resid >= blocksize && blocksize > zvol_immediate_write_sz)
1080 write_state = WR_INDIRECT;
1081 else if (sync)
1082 write_state = WR_COPIED;
1083 else
1084 write_state = WR_NEED_COPY;
1086 while (resid) {
1087 itx_t *itx;
1088 lr_write_t *lr;
1089 itx_wr_state_t wr_state = write_state;
1090 ssize_t len = resid;
1092 if (wr_state == WR_COPIED && resid > ZIL_MAX_COPIED_DATA)
1093 wr_state = WR_NEED_COPY;
1094 else if (wr_state == WR_INDIRECT)
1095 len = MIN(blocksize - P2PHASE(off, blocksize), resid);
1097 itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
1098 (wr_state == WR_COPIED ? len : 0));
1099 lr = (lr_write_t *)&itx->itx_lr;
1100 if (wr_state == WR_COPIED && dmu_read_by_dnode(zv->zv_dn,
1101 off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
1102 zil_itx_destroy(itx);
1103 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1104 lr = (lr_write_t *)&itx->itx_lr;
1105 wr_state = WR_NEED_COPY;
1108 itx->itx_wr_state = wr_state;
1109 lr->lr_foid = ZVOL_OBJ;
1110 lr->lr_offset = off;
1111 lr->lr_length = len;
1112 lr->lr_blkoff = 0;
1113 BP_ZERO(&lr->lr_blkptr);
1115 itx->itx_private = zv;
1116 itx->itx_sync = sync;
1118 zil_itx_assign(zilog, itx, tx);
1120 off += len;
1121 resid -= len;
1125 static int
1126 zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t origoffset,
1127 uint64_t size, boolean_t doread, boolean_t isdump)
1129 vdev_disk_t *dvd;
1130 int c;
1131 int numerrors = 0;
1133 if (vd->vdev_ops == &vdev_mirror_ops ||
1134 vd->vdev_ops == &vdev_replacing_ops ||
1135 vd->vdev_ops == &vdev_spare_ops) {
1136 for (c = 0; c < vd->vdev_children; c++) {
1137 int err = zvol_dumpio_vdev(vd->vdev_child[c],
1138 addr, offset, origoffset, size, doread, isdump);
1139 if (err != 0) {
1140 numerrors++;
1141 } else if (doread) {
1142 break;
1147 if (!vd->vdev_ops->vdev_op_leaf && vd->vdev_ops != &vdev_raidz_ops)
1148 return (numerrors < vd->vdev_children ? 0 : EIO);
1150 if (doread && !vdev_readable(vd))
1151 return (SET_ERROR(EIO));
1152 else if (!doread && !vdev_writeable(vd))
1153 return (SET_ERROR(EIO));
1155 if (vd->vdev_ops == &vdev_raidz_ops) {
1156 return (vdev_raidz_physio(vd,
1157 addr, size, offset, origoffset, doread, isdump));
1160 offset += VDEV_LABEL_START_SIZE;
1162 if (ddi_in_panic() || isdump) {
1163 ASSERT(!doread);
1164 if (doread)
1165 return (SET_ERROR(EIO));
1166 dvd = vd->vdev_tsd;
1167 ASSERT3P(dvd, !=, NULL);
1168 return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset),
1169 lbtodb(size)));
1170 } else {
1171 dvd = vd->vdev_tsd;
1172 ASSERT3P(dvd, !=, NULL);
1173 return (vdev_disk_ldi_physio(dvd->vd_lh, addr, size,
1174 offset, doread ? B_READ : B_WRITE));
1178 static int
1179 zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size,
1180 boolean_t doread, boolean_t isdump)
1182 vdev_t *vd;
1183 int error;
1184 zvol_extent_t *ze;
1185 spa_t *spa = dmu_objset_spa(zv->zv_objset);
1187 /* Must be sector aligned, and not stradle a block boundary. */
1188 if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) ||
1189 P2BOUNDARY(offset, size, zv->zv_volblocksize)) {
1190 return (SET_ERROR(EINVAL));
1192 ASSERT(size <= zv->zv_volblocksize);
1194 /* Locate the extent this belongs to */
1195 ze = list_head(&zv->zv_extents);
1196 while (offset >= ze->ze_nblks * zv->zv_volblocksize) {
1197 offset -= ze->ze_nblks * zv->zv_volblocksize;
1198 ze = list_next(&zv->zv_extents, ze);
1201 if (ze == NULL)
1202 return (SET_ERROR(EINVAL));
1204 if (!ddi_in_panic())
1205 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
1207 vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva));
1208 offset += DVA_GET_OFFSET(&ze->ze_dva);
1209 error = zvol_dumpio_vdev(vd, addr, offset, DVA_GET_OFFSET(&ze->ze_dva),
1210 size, doread, isdump);
1212 if (!ddi_in_panic())
1213 spa_config_exit(spa, SCL_STATE, FTAG);
1215 return (error);
1219 zvol_strategy(buf_t *bp)
1221 zfs_soft_state_t *zs = NULL;
1222 zvol_state_t *zv;
1223 uint64_t off, volsize;
1224 size_t resid;
1225 char *addr;
1226 objset_t *os;
1227 rl_t *rl;
1228 int error = 0;
1229 boolean_t doread = bp->b_flags & B_READ;
1230 boolean_t is_dumpified;
1231 boolean_t sync;
1233 if (getminor(bp->b_edev) == 0) {
1234 error = SET_ERROR(EINVAL);
1235 } else {
1236 zs = ddi_get_soft_state(zfsdev_state, getminor(bp->b_edev));
1237 if (zs == NULL)
1238 error = SET_ERROR(ENXIO);
1239 else if (zs->zss_type != ZSST_ZVOL)
1240 error = SET_ERROR(EINVAL);
1243 if (error) {
1244 bioerror(bp, error);
1245 biodone(bp);
1246 return (0);
1249 zv = zs->zss_data;
1251 if (!(bp->b_flags & B_READ) && (zv->zv_flags & ZVOL_RDONLY)) {
1252 bioerror(bp, EROFS);
1253 biodone(bp);
1254 return (0);
1257 off = ldbtob(bp->b_blkno);
1258 volsize = zv->zv_volsize;
1260 os = zv->zv_objset;
1261 ASSERT(os != NULL);
1263 bp_mapin(bp);
1264 addr = bp->b_un.b_addr;
1265 resid = bp->b_bcount;
1267 if (resid > 0 && (off < 0 || off >= volsize)) {
1268 bioerror(bp, EIO);
1269 biodone(bp);
1270 return (0);
1273 is_dumpified = zv->zv_flags & ZVOL_DUMPIFIED;
1274 sync = ((!(bp->b_flags & B_ASYNC) &&
1275 !(zv->zv_flags & ZVOL_WCE)) ||
1276 (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)) &&
1277 !doread && !is_dumpified;
1280 * There must be no buffer changes when doing a dmu_sync() because
1281 * we can't change the data whilst calculating the checksum.
1283 rl = zfs_range_lock(&zv->zv_znode, off, resid,
1284 doread ? RL_READER : RL_WRITER);
1286 while (resid != 0 && off < volsize) {
1287 size_t size = MIN(resid, zvol_maxphys);
1288 if (is_dumpified) {
1289 size = MIN(size, P2END(off, zv->zv_volblocksize) - off);
1290 error = zvol_dumpio(zv, addr, off, size,
1291 doread, B_FALSE);
1292 } else if (doread) {
1293 error = dmu_read(os, ZVOL_OBJ, off, size, addr,
1294 DMU_READ_PREFETCH);
1295 } else {
1296 dmu_tx_t *tx = dmu_tx_create(os);
1297 dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
1298 error = dmu_tx_assign(tx, TXG_WAIT);
1299 if (error) {
1300 dmu_tx_abort(tx);
1301 } else {
1302 dmu_write(os, ZVOL_OBJ, off, size, addr, tx);
1303 zvol_log_write(zv, tx, off, size, sync);
1304 dmu_tx_commit(tx);
1307 if (error) {
1308 /* convert checksum errors into IO errors */
1309 if (error == ECKSUM)
1310 error = SET_ERROR(EIO);
1311 break;
1313 off += size;
1314 addr += size;
1315 resid -= size;
1317 zfs_range_unlock(rl);
1319 if ((bp->b_resid = resid) == bp->b_bcount)
1320 bioerror(bp, off > volsize ? EINVAL : error);
1322 if (sync)
1323 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1324 biodone(bp);
1326 return (0);
1330 * Set the buffer count to the zvol maximum transfer.
1331 * Using our own routine instead of the default minphys()
1332 * means that for larger writes we write bigger buffers on X86
1333 * (128K instead of 56K) and flush the disk write cache less often
1334 * (every zvol_maxphys - currently 1MB) instead of minphys (currently
1335 * 56K on X86 and 128K on sparc).
1337 void
1338 zvol_minphys(struct buf *bp)
1340 if (bp->b_bcount > zvol_maxphys)
1341 bp->b_bcount = zvol_maxphys;
1345 zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks)
1347 minor_t minor = getminor(dev);
1348 zvol_state_t *zv;
1349 int error = 0;
1350 uint64_t size;
1351 uint64_t boff;
1352 uint64_t resid;
1354 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1355 if (zv == NULL)
1356 return (SET_ERROR(ENXIO));
1358 if ((zv->zv_flags & ZVOL_DUMPIFIED) == 0)
1359 return (SET_ERROR(EINVAL));
1361 boff = ldbtob(blkno);
1362 resid = ldbtob(nblocks);
1364 VERIFY3U(boff + resid, <=, zv->zv_volsize);
1366 while (resid) {
1367 size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff);
1368 error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE);
1369 if (error)
1370 break;
1371 boff += size;
1372 addr += size;
1373 resid -= size;
1376 return (error);
1379 /*ARGSUSED*/
1381 zvol_read(dev_t dev, uio_t *uio, cred_t *cr)
1383 minor_t minor = getminor(dev);
1384 zvol_state_t *zv;
1385 uint64_t volsize;
1386 rl_t *rl;
1387 int error = 0;
1389 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1390 if (zv == NULL)
1391 return (SET_ERROR(ENXIO));
1393 volsize = zv->zv_volsize;
1394 if (uio->uio_resid > 0 &&
1395 (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
1396 return (SET_ERROR(EIO));
1398 if (zv->zv_flags & ZVOL_DUMPIFIED) {
1399 error = physio(zvol_strategy, NULL, dev, B_READ,
1400 zvol_minphys, uio);
1401 return (error);
1404 rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1405 RL_READER);
1406 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1407 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1409 /* don't read past the end */
1410 if (bytes > volsize - uio->uio_loffset)
1411 bytes = volsize - uio->uio_loffset;
1413 error = dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes);
1414 if (error) {
1415 /* convert checksum errors into IO errors */
1416 if (error == ECKSUM)
1417 error = SET_ERROR(EIO);
1418 break;
1421 zfs_range_unlock(rl);
1422 return (error);
1425 /*ARGSUSED*/
1427 zvol_write(dev_t dev, uio_t *uio, cred_t *cr)
1429 minor_t minor = getminor(dev);
1430 zvol_state_t *zv;
1431 uint64_t volsize;
1432 rl_t *rl;
1433 int error = 0;
1434 boolean_t sync;
1436 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1437 if (zv == NULL)
1438 return (SET_ERROR(ENXIO));
1440 volsize = zv->zv_volsize;
1441 if (uio->uio_resid > 0 &&
1442 (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
1443 return (SET_ERROR(EIO));
1445 if (zv->zv_flags & ZVOL_DUMPIFIED) {
1446 error = physio(zvol_strategy, NULL, dev, B_WRITE,
1447 zvol_minphys, uio);
1448 return (error);
1451 sync = !(zv->zv_flags & ZVOL_WCE) ||
1452 (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS);
1454 rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1455 RL_WRITER);
1456 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1457 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1458 uint64_t off = uio->uio_loffset;
1459 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
1461 if (bytes > volsize - off) /* don't write past the end */
1462 bytes = volsize - off;
1464 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
1465 error = dmu_tx_assign(tx, TXG_WAIT);
1466 if (error) {
1467 dmu_tx_abort(tx);
1468 break;
1470 error = dmu_write_uio_dnode(zv->zv_dn, uio, bytes, tx);
1471 if (error == 0)
1472 zvol_log_write(zv, tx, off, bytes, sync);
1473 dmu_tx_commit(tx);
1475 if (error)
1476 break;
1478 zfs_range_unlock(rl);
1479 if (sync)
1480 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1481 return (error);
1485 zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs)
1487 struct uuid uuid = EFI_RESERVED;
1488 efi_gpe_t gpe = { 0 };
1489 uint32_t crc;
1490 dk_efi_t efi;
1491 int length;
1492 char *ptr;
1494 if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag))
1495 return (SET_ERROR(EFAULT));
1496 ptr = (char *)(uintptr_t)efi.dki_data_64;
1497 length = efi.dki_length;
1499 * Some clients may attempt to request a PMBR for the
1500 * zvol. Currently this interface will return EINVAL to
1501 * such requests. These requests could be supported by
1502 * adding a check for lba == 0 and consing up an appropriate
1503 * PMBR.
1505 if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0)
1506 return (SET_ERROR(EINVAL));
1508 gpe.efi_gpe_StartingLBA = LE_64(34ULL);
1509 gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1);
1510 UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
1512 if (efi.dki_lba == 1) {
1513 efi_gpt_t gpt = { 0 };
1515 gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
1516 gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
1517 gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt));
1518 gpt.efi_gpt_MyLBA = LE_64(1ULL);
1519 gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL);
1520 gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1);
1521 gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL);
1522 gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
1523 gpt.efi_gpt_SizeOfPartitionEntry =
1524 LE_32(sizeof (efi_gpe_t));
1525 CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
1526 gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
1527 CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table);
1528 gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
1529 if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length),
1530 flag))
1531 return (SET_ERROR(EFAULT));
1532 ptr += sizeof (gpt);
1533 length -= sizeof (gpt);
1535 if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe),
1536 length), flag))
1537 return (SET_ERROR(EFAULT));
1538 return (0);
1542 * BEGIN entry points to allow external callers access to the volume.
1545 * Return the volume parameters needed for access from an external caller.
1546 * These values are invariant as long as the volume is held open.
1549 zvol_get_volume_params(minor_t minor, uint64_t *blksize,
1550 uint64_t *max_xfer_len, void **minor_hdl, void **objset_hdl, void **zil_hdl,
1551 void **rl_hdl, void **dnode_hdl)
1553 zvol_state_t *zv;
1555 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1556 if (zv == NULL)
1557 return (SET_ERROR(ENXIO));
1558 if (zv->zv_flags & ZVOL_DUMPIFIED)
1559 return (SET_ERROR(ENXIO));
1561 ASSERT(blksize && max_xfer_len && minor_hdl &&
1562 objset_hdl && zil_hdl && rl_hdl && dnode_hdl);
1564 *blksize = zv->zv_volblocksize;
1565 *max_xfer_len = (uint64_t)zvol_maxphys;
1566 *minor_hdl = zv;
1567 *objset_hdl = zv->zv_objset;
1568 *zil_hdl = zv->zv_zilog;
1569 *rl_hdl = &zv->zv_znode;
1570 *dnode_hdl = zv->zv_dn;
1571 return (0);
1575 * Return the current volume size to an external caller.
1576 * The size can change while the volume is open.
1578 uint64_t
1579 zvol_get_volume_size(void *minor_hdl)
1581 zvol_state_t *zv = minor_hdl;
1583 return (zv->zv_volsize);
1587 * Return the current WCE setting to an external caller.
1588 * The WCE setting can change while the volume is open.
1591 zvol_get_volume_wce(void *minor_hdl)
1593 zvol_state_t *zv = minor_hdl;
1595 return ((zv->zv_flags & ZVOL_WCE) ? 1 : 0);
1599 * Entry point for external callers to zvol_log_write
1601 void
1602 zvol_log_write_minor(void *minor_hdl, dmu_tx_t *tx, offset_t off, ssize_t resid,
1603 boolean_t sync)
1605 zvol_state_t *zv = minor_hdl;
1607 zvol_log_write(zv, tx, off, resid, sync);
1610 * END entry points to allow external callers access to the volume.
1614 * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE.
1616 static void
1617 zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off, uint64_t len,
1618 boolean_t sync)
1620 itx_t *itx;
1621 lr_truncate_t *lr;
1622 zilog_t *zilog = zv->zv_zilog;
1624 if (zil_replaying(zilog, tx))
1625 return;
1627 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1628 lr = (lr_truncate_t *)&itx->itx_lr;
1629 lr->lr_foid = ZVOL_OBJ;
1630 lr->lr_offset = off;
1631 lr->lr_length = len;
1633 itx->itx_sync = sync;
1634 zil_itx_assign(zilog, itx, tx);
1638 * Dirtbag ioctls to support mkfs(1M) for UFS filesystems. See dkio(7I).
1639 * Also a dirtbag dkio ioctl for unmap/free-block functionality.
1641 /*ARGSUSED*/
1643 zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
1645 zvol_state_t *zv;
1646 struct dk_callback *dkc;
1647 int error = 0;
1648 rl_t *rl;
1650 mutex_enter(&zfsdev_state_lock);
1652 zv = zfsdev_get_soft_state(getminor(dev), ZSST_ZVOL);
1654 if (zv == NULL) {
1655 mutex_exit(&zfsdev_state_lock);
1656 return (SET_ERROR(ENXIO));
1658 ASSERT(zv->zv_total_opens > 0);
1660 switch (cmd) {
1662 case DKIOCINFO:
1664 struct dk_cinfo dki;
1666 bzero(&dki, sizeof (dki));
1667 (void) strcpy(dki.dki_cname, "zvol");
1668 (void) strcpy(dki.dki_dname, "zvol");
1669 dki.dki_ctype = DKC_UNKNOWN;
1670 dki.dki_unit = getminor(dev);
1671 dki.dki_maxtransfer =
1672 1 << (SPA_OLD_MAXBLOCKSHIFT - zv->zv_min_bs);
1673 mutex_exit(&zfsdev_state_lock);
1674 if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag))
1675 error = SET_ERROR(EFAULT);
1676 return (error);
1679 case DKIOCGMEDIAINFO:
1681 struct dk_minfo dkm;
1683 bzero(&dkm, sizeof (dkm));
1684 dkm.dki_lbsize = 1U << zv->zv_min_bs;
1685 dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
1686 dkm.dki_media_type = DK_UNKNOWN;
1687 mutex_exit(&zfsdev_state_lock);
1688 if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
1689 error = SET_ERROR(EFAULT);
1690 return (error);
1693 case DKIOCGMEDIAINFOEXT:
1695 struct dk_minfo_ext dkmext;
1697 bzero(&dkmext, sizeof (dkmext));
1698 dkmext.dki_lbsize = 1U << zv->zv_min_bs;
1699 dkmext.dki_pbsize = zv->zv_volblocksize;
1700 dkmext.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
1701 dkmext.dki_media_type = DK_UNKNOWN;
1702 mutex_exit(&zfsdev_state_lock);
1703 if (ddi_copyout(&dkmext, (void *)arg, sizeof (dkmext), flag))
1704 error = SET_ERROR(EFAULT);
1705 return (error);
1708 case DKIOCGETEFI:
1710 uint64_t vs = zv->zv_volsize;
1711 uint8_t bs = zv->zv_min_bs;
1713 mutex_exit(&zfsdev_state_lock);
1714 error = zvol_getefi((void *)arg, flag, vs, bs);
1715 return (error);
1718 case DKIOCFLUSHWRITECACHE:
1719 dkc = (struct dk_callback *)arg;
1720 mutex_exit(&zfsdev_state_lock);
1721 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1722 if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) {
1723 (*dkc->dkc_callback)(dkc->dkc_cookie, error);
1724 error = 0;
1726 return (error);
1728 case DKIOCGETWCE:
1730 int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0;
1731 if (ddi_copyout(&wce, (void *)arg, sizeof (int),
1732 flag))
1733 error = SET_ERROR(EFAULT);
1734 break;
1736 case DKIOCSETWCE:
1738 int wce;
1739 if (ddi_copyin((void *)arg, &wce, sizeof (int),
1740 flag)) {
1741 error = SET_ERROR(EFAULT);
1742 break;
1744 if (wce) {
1745 zv->zv_flags |= ZVOL_WCE;
1746 mutex_exit(&zfsdev_state_lock);
1747 } else {
1748 zv->zv_flags &= ~ZVOL_WCE;
1749 mutex_exit(&zfsdev_state_lock);
1750 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1752 return (0);
1755 case DKIOCGGEOM:
1756 case DKIOCGVTOC:
1758 * commands using these (like prtvtoc) expect ENOTSUP
1759 * since we're emulating an EFI label
1761 error = SET_ERROR(ENOTSUP);
1762 break;
1764 case DKIOCDUMPINIT:
1765 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1766 RL_WRITER);
1767 error = zvol_dumpify(zv);
1768 zfs_range_unlock(rl);
1769 break;
1771 case DKIOCDUMPFINI:
1772 if (!(zv->zv_flags & ZVOL_DUMPIFIED))
1773 break;
1774 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1775 RL_WRITER);
1776 error = zvol_dump_fini(zv);
1777 zfs_range_unlock(rl);
1778 break;
1780 case DKIOCFREE:
1782 dkioc_free_list_t *dfl;
1783 dmu_tx_t *tx;
1785 if (!zvol_unmap_enabled)
1786 break;
1788 if (!(flag & FKIOCTL)) {
1789 error = dfl_copyin((void *)arg, &dfl, flag, KM_SLEEP);
1790 if (error != 0)
1791 break;
1792 } else {
1793 dfl = (dkioc_free_list_t *)arg;
1794 ASSERT3U(dfl->dfl_num_exts, <=, DFL_COPYIN_MAX_EXTS);
1795 if (dfl->dfl_num_exts > DFL_COPYIN_MAX_EXTS) {
1796 error = SET_ERROR(EINVAL);
1797 break;
1801 mutex_exit(&zfsdev_state_lock);
1803 for (int i = 0; i < dfl->dfl_num_exts; i++) {
1804 uint64_t start = dfl->dfl_exts[i].dfle_start,
1805 length = dfl->dfl_exts[i].dfle_length,
1806 end = start + length;
1809 * Apply Postel's Law to length-checking. If they
1810 * overshoot, just blank out until the end, if there's
1811 * a need to blank out anything.
1813 if (start >= zv->zv_volsize)
1814 continue; /* No need to do anything... */
1815 if (end > zv->zv_volsize) {
1816 end = DMU_OBJECT_END;
1817 length = end - start;
1820 rl = zfs_range_lock(&zv->zv_znode, start, length,
1821 RL_WRITER);
1822 tx = dmu_tx_create(zv->zv_objset);
1823 error = dmu_tx_assign(tx, TXG_WAIT);
1824 if (error != 0) {
1825 dmu_tx_abort(tx);
1826 } else {
1827 zvol_log_truncate(zv, tx, start, length,
1828 B_TRUE);
1829 dmu_tx_commit(tx);
1830 error = dmu_free_long_range(zv->zv_objset,
1831 ZVOL_OBJ, start, length);
1834 zfs_range_unlock(rl);
1836 if (error != 0)
1837 break;
1841 * If the write-cache is disabled, 'sync' property
1842 * is set to 'always', or if the caller is asking for
1843 * a synchronous free, commit this operation to the zil.
1844 * This will sync any previous uncommitted writes to the
1845 * zvol object.
1846 * Can be overridden by the zvol_unmap_sync_enabled tunable.
1848 if ((error == 0) && zvol_unmap_sync_enabled &&
1849 (!(zv->zv_flags & ZVOL_WCE) ||
1850 (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS) ||
1851 (dfl->dfl_flags & DF_WAIT_SYNC))) {
1852 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1855 if (!(flag & FKIOCTL))
1856 dfl_free(dfl);
1858 return (error);
1861 default:
1862 error = SET_ERROR(ENOTTY);
1863 break;
1866 mutex_exit(&zfsdev_state_lock);
1867 return (error);
1871 zvol_busy(void)
1873 return (zvol_minors != 0);
1876 void
1877 zvol_init(void)
1879 VERIFY(ddi_soft_state_init(&zfsdev_state, sizeof (zfs_soft_state_t),
1880 1) == 0);
1881 mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL);
1884 void
1885 zvol_fini(void)
1887 mutex_destroy(&zfsdev_state_lock);
1888 ddi_soft_state_fini(&zfsdev_state);
1891 /*ARGSUSED*/
1892 static int
1893 zfs_mvdev_dump_feature_check(void *arg, dmu_tx_t *tx)
1895 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
1897 if (spa_feature_is_active(spa, SPA_FEATURE_MULTI_VDEV_CRASH_DUMP))
1898 return (1);
1899 return (0);
1902 /*ARGSUSED*/
1903 static void
1904 zfs_mvdev_dump_activate_feature_sync(void *arg, dmu_tx_t *tx)
1906 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
1908 spa_feature_incr(spa, SPA_FEATURE_MULTI_VDEV_CRASH_DUMP, tx);
1911 static int
1912 zvol_dump_init(zvol_state_t *zv, boolean_t resize)
1914 dmu_tx_t *tx;
1915 int error;
1916 objset_t *os = zv->zv_objset;
1917 spa_t *spa = dmu_objset_spa(os);
1918 vdev_t *vd = spa->spa_root_vdev;
1919 nvlist_t *nv = NULL;
1920 uint64_t version = spa_version(spa);
1921 uint64_t checksum, compress, refresrv, vbs, dedup;
1923 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
1924 ASSERT(vd->vdev_ops == &vdev_root_ops);
1926 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0,
1927 DMU_OBJECT_END);
1928 if (error != 0)
1929 return (error);
1930 /* wait for dmu_free_long_range to actually free the blocks */
1931 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
1934 * If the pool on which the dump device is being initialized has more
1935 * than one child vdev, check that the MULTI_VDEV_CRASH_DUMP feature is
1936 * enabled. If so, bump that feature's counter to indicate that the
1937 * feature is active. We also check the vdev type to handle the
1938 * following case:
1939 * # zpool create test raidz disk1 disk2 disk3
1940 * Now have spa_root_vdev->vdev_children == 1 (the raidz vdev),
1941 * the raidz vdev itself has 3 children.
1943 if (vd->vdev_children > 1 || vd->vdev_ops == &vdev_raidz_ops) {
1944 if (!spa_feature_is_enabled(spa,
1945 SPA_FEATURE_MULTI_VDEV_CRASH_DUMP))
1946 return (SET_ERROR(ENOTSUP));
1947 (void) dsl_sync_task(spa_name(spa),
1948 zfs_mvdev_dump_feature_check,
1949 zfs_mvdev_dump_activate_feature_sync, NULL,
1950 2, ZFS_SPACE_CHECK_RESERVED);
1953 if (!resize) {
1954 error = dsl_prop_get_integer(zv->zv_name,
1955 zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL);
1956 if (error == 0) {
1957 error = dsl_prop_get_integer(zv->zv_name,
1958 zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum,
1959 NULL);
1961 if (error == 0) {
1962 error = dsl_prop_get_integer(zv->zv_name,
1963 zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
1964 &refresrv, NULL);
1966 if (error == 0) {
1967 error = dsl_prop_get_integer(zv->zv_name,
1968 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs,
1969 NULL);
1971 if (version >= SPA_VERSION_DEDUP && error == 0) {
1972 error = dsl_prop_get_integer(zv->zv_name,
1973 zfs_prop_to_name(ZFS_PROP_DEDUP), &dedup, NULL);
1976 if (error != 0)
1977 return (error);
1979 tx = dmu_tx_create(os);
1980 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1981 dmu_tx_hold_bonus(tx, ZVOL_OBJ);
1982 error = dmu_tx_assign(tx, TXG_WAIT);
1983 if (error != 0) {
1984 dmu_tx_abort(tx);
1985 return (error);
1989 * If we are resizing the dump device then we only need to
1990 * update the refreservation to match the newly updated
1991 * zvolsize. Otherwise, we save off the original state of the
1992 * zvol so that we can restore them if the zvol is ever undumpified.
1994 if (resize) {
1995 error = zap_update(os, ZVOL_ZAP_OBJ,
1996 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1997 &zv->zv_volsize, tx);
1998 } else {
1999 error = zap_update(os, ZVOL_ZAP_OBJ,
2000 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1,
2001 &compress, tx);
2002 if (error == 0) {
2003 error = zap_update(os, ZVOL_ZAP_OBJ,
2004 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1,
2005 &checksum, tx);
2007 if (error == 0) {
2008 error = zap_update(os, ZVOL_ZAP_OBJ,
2009 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
2010 &refresrv, tx);
2012 if (error == 0) {
2013 error = zap_update(os, ZVOL_ZAP_OBJ,
2014 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1,
2015 &vbs, tx);
2017 if (error == 0) {
2018 error = dmu_object_set_blocksize(
2019 os, ZVOL_OBJ, SPA_OLD_MAXBLOCKSIZE, 0, tx);
2021 if (version >= SPA_VERSION_DEDUP && error == 0) {
2022 error = zap_update(os, ZVOL_ZAP_OBJ,
2023 zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1,
2024 &dedup, tx);
2026 if (error == 0)
2027 zv->zv_volblocksize = SPA_OLD_MAXBLOCKSIZE;
2029 dmu_tx_commit(tx);
2032 * We only need update the zvol's property if we are initializing
2033 * the dump area for the first time.
2035 if (error == 0 && !resize) {
2037 * If MULTI_VDEV_CRASH_DUMP is active, use the NOPARITY checksum
2038 * function. Otherwise, use the old default -- OFF.
2040 checksum = spa_feature_is_active(spa,
2041 SPA_FEATURE_MULTI_VDEV_CRASH_DUMP) ? ZIO_CHECKSUM_NOPARITY :
2042 ZIO_CHECKSUM_OFF;
2044 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2045 VERIFY(nvlist_add_uint64(nv,
2046 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0);
2047 VERIFY(nvlist_add_uint64(nv,
2048 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
2049 ZIO_COMPRESS_OFF) == 0);
2050 VERIFY(nvlist_add_uint64(nv,
2051 zfs_prop_to_name(ZFS_PROP_CHECKSUM),
2052 checksum) == 0);
2053 if (version >= SPA_VERSION_DEDUP) {
2054 VERIFY(nvlist_add_uint64(nv,
2055 zfs_prop_to_name(ZFS_PROP_DEDUP),
2056 ZIO_CHECKSUM_OFF) == 0);
2059 error = zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
2060 nv, NULL);
2061 nvlist_free(nv);
2064 /* Allocate the space for the dump */
2065 if (error == 0)
2066 error = zvol_prealloc(zv);
2067 return (error);
2070 static int
2071 zvol_dumpify(zvol_state_t *zv)
2073 int error = 0;
2074 uint64_t dumpsize = 0;
2075 dmu_tx_t *tx;
2076 objset_t *os = zv->zv_objset;
2078 if (zv->zv_flags & ZVOL_RDONLY)
2079 return (SET_ERROR(EROFS));
2081 if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE,
2082 8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) {
2083 boolean_t resize = (dumpsize > 0);
2085 if ((error = zvol_dump_init(zv, resize)) != 0) {
2086 (void) zvol_dump_fini(zv);
2087 return (error);
2092 * Build up our lba mapping.
2094 error = zvol_get_lbas(zv);
2095 if (error) {
2096 (void) zvol_dump_fini(zv);
2097 return (error);
2100 tx = dmu_tx_create(os);
2101 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
2102 error = dmu_tx_assign(tx, TXG_WAIT);
2103 if (error) {
2104 dmu_tx_abort(tx);
2105 (void) zvol_dump_fini(zv);
2106 return (error);
2109 zv->zv_flags |= ZVOL_DUMPIFIED;
2110 error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1,
2111 &zv->zv_volsize, tx);
2112 dmu_tx_commit(tx);
2114 if (error) {
2115 (void) zvol_dump_fini(zv);
2116 return (error);
2119 txg_wait_synced(dmu_objset_pool(os), 0);
2120 return (0);
2123 static int
2124 zvol_dump_fini(zvol_state_t *zv)
2126 dmu_tx_t *tx;
2127 objset_t *os = zv->zv_objset;
2128 nvlist_t *nv;
2129 int error = 0;
2130 uint64_t checksum, compress, refresrv, vbs, dedup;
2131 uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset));
2134 * Attempt to restore the zvol back to its pre-dumpified state.
2135 * This is a best-effort attempt as it's possible that not all
2136 * of these properties were initialized during the dumpify process
2137 * (i.e. error during zvol_dump_init).
2140 tx = dmu_tx_create(os);
2141 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
2142 error = dmu_tx_assign(tx, TXG_WAIT);
2143 if (error) {
2144 dmu_tx_abort(tx);
2145 return (error);
2147 (void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx);
2148 dmu_tx_commit(tx);
2150 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2151 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum);
2152 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2153 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress);
2154 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2155 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv);
2156 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2157 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs);
2159 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2160 (void) nvlist_add_uint64(nv,
2161 zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum);
2162 (void) nvlist_add_uint64(nv,
2163 zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress);
2164 (void) nvlist_add_uint64(nv,
2165 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv);
2166 if (version >= SPA_VERSION_DEDUP &&
2167 zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2168 zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, &dedup) == 0) {
2169 (void) nvlist_add_uint64(nv,
2170 zfs_prop_to_name(ZFS_PROP_DEDUP), dedup);
2172 (void) zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
2173 nv, NULL);
2174 nvlist_free(nv);
2176 zvol_free_extents(zv);
2177 zv->zv_flags &= ~ZVOL_DUMPIFIED;
2178 (void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END);
2179 /* wait for dmu_free_long_range to actually free the blocks */
2180 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
2181 tx = dmu_tx_create(os);
2182 dmu_tx_hold_bonus(tx, ZVOL_OBJ);
2183 error = dmu_tx_assign(tx, TXG_WAIT);
2184 if (error) {
2185 dmu_tx_abort(tx);
2186 return (error);
2188 if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0)
2189 zv->zv_volblocksize = vbs;
2190 dmu_tx_commit(tx);
2192 return (0);