zvol.c revision 275892
1/* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21/* 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23 * 24 * Copyright (c) 2006-2010 Pawel Jakub Dawidek <pjd@FreeBSD.org> 25 * All rights reserved. 26 * 27 * Portions Copyright 2010 Robert Milkowski 28 * 29 * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 30 * Copyright (c) 2012, 2014 by Delphix. All rights reserved. 31 * Copyright (c) 2013, Joyent, Inc. All rights reserved. 32 */ 33 34/* Portions Copyright 2011 Martin Matuska <mm@FreeBSD.org> */ 35 36/* 37 * ZFS volume emulation driver. 38 * 39 * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes. 40 * Volumes are accessed through the symbolic links named: 41 * 42 * /dev/zvol/dsk/<pool_name>/<dataset_name> 43 * /dev/zvol/rdsk/<pool_name>/<dataset_name> 44 * 45 * These links are created by the /dev filesystem (sdev_zvolops.c). 46 * Volumes are persistent through reboot. No user command needs to be 47 * run before opening and using a device. 48 * 49 * FreeBSD notes. 50 * On FreeBSD ZVOLs are simply GEOM providers like any other storage device 51 * in the system. 52 */ 53 54#include <sys/types.h> 55#include <sys/param.h> 56#include <sys/kernel.h> 57#include <sys/errno.h> 58#include <sys/uio.h> 59#include <sys/bio.h> 60#include <sys/buf.h> 61#include <sys/kmem.h> 62#include <sys/conf.h> 63#include <sys/cmn_err.h> 64#include <sys/stat.h> 65#include <sys/zap.h> 66#include <sys/spa.h> 67#include <sys/spa_impl.h> 68#include <sys/zio.h> 69#include <sys/disk.h> 70#include <sys/dmu_traverse.h> 71#include <sys/dnode.h> 72#include <sys/dsl_dataset.h> 73#include <sys/dsl_prop.h> 74#include <sys/dkio.h> 75#include <sys/byteorder.h> 76#include <sys/sunddi.h> 77#include <sys/dirent.h> 78#include <sys/policy.h> 79#include <sys/queue.h> 80#include <sys/fs/zfs.h> 81#include <sys/zfs_ioctl.h> 82#include <sys/zil.h> 83#include <sys/refcount.h> 84#include <sys/zfs_znode.h> 85#include <sys/zfs_rlock.h> 86#include <sys/vdev_impl.h> 87#include <sys/vdev_raidz.h> 88#include <sys/zvol.h> 89#include <sys/zil_impl.h> 90#include <sys/dbuf.h> 91#include <sys/dmu_tx.h> 92#include <sys/zfeature.h> 93#include <sys/zio_checksum.h> 94#include <sys/filio.h> 95 96#include <geom/geom.h> 97 98#include "zfs_namecheck.h" 99 100struct g_class zfs_zvol_class = { 101 .name = "ZFS::ZVOL", 102 .version = G_VERSION, 103}; 104 105DECLARE_GEOM_CLASS(zfs_zvol_class, zfs_zvol); 106 107void *zfsdev_state; 108static char *zvol_tag = "zvol_tag"; 109 110#define ZVOL_DUMPSIZE "dumpsize" 111 112/* 113 * The spa_namespace_lock protects the zfsdev_state structure from being 114 * modified while it's being used, e.g. an open that comes in before a 115 * create finishes. It also protects temporary opens of the dataset so that, 116 * e.g., an open doesn't get a spurious EBUSY. 117 */ 118static uint32_t zvol_minors; 119 120SYSCTL_DECL(_vfs_zfs); 121SYSCTL_NODE(_vfs_zfs, OID_AUTO, vol, CTLFLAG_RW, 0, "ZFS VOLUME"); 122static int volmode = ZFS_VOLMODE_GEOM; 123TUNABLE_INT("vfs.zfs.vol.mode", &volmode); 124SYSCTL_INT(_vfs_zfs_vol, OID_AUTO, mode, CTLFLAG_RWTUN, &volmode, 0, 125 "Expose as GEOM providers (1), device files (2) or neither"); 126 127typedef struct zvol_extent { 128 list_node_t ze_node; 129 dva_t ze_dva; /* dva associated with this extent */ 130 uint64_t ze_nblks; /* number of blocks in extent */ 131} zvol_extent_t; 132 133/* 134 * The in-core state of each volume. 135 */ 136typedef struct zvol_state { 137 LIST_ENTRY(zvol_state) zv_links; 138 char zv_name[MAXPATHLEN]; /* pool/dd name */ 139 uint64_t zv_volsize; /* amount of space we advertise */ 140 uint64_t zv_volblocksize; /* volume block size */ 141 struct cdev *zv_dev; /* non-GEOM device */ 142 struct g_provider *zv_provider; /* GEOM provider */ 143 uint8_t zv_min_bs; /* minimum addressable block shift */ 144 uint8_t zv_flags; /* readonly, dumpified, etc. */ 145 objset_t *zv_objset; /* objset handle */ 146 uint32_t zv_total_opens; /* total open count */ 147 zilog_t *zv_zilog; /* ZIL handle */ 148 list_t zv_extents; /* List of extents for dump */ 149 znode_t zv_znode; /* for range locking */ 150 dmu_buf_t *zv_dbuf; /* bonus handle */ 151 int zv_state; 152 int zv_volmode; /* Provide GEOM or cdev */ 153 struct bio_queue_head zv_queue; 154 struct mtx zv_queue_mtx; /* zv_queue mutex */ 155} zvol_state_t; 156 157static LIST_HEAD(, zvol_state) all_zvols; 158 159/* 160 * zvol specific flags 161 */ 162#define ZVOL_RDONLY 0x1 163#define ZVOL_DUMPIFIED 0x2 164#define ZVOL_EXCL 0x4 165#define ZVOL_WCE 0x8 166 167/* 168 * zvol maximum transfer in one DMU tx. 169 */ 170int zvol_maxphys = DMU_MAX_ACCESS/2; 171 172/* 173 * Toggle unmap functionality. 174 */ 175boolean_t zvol_unmap_enabled = B_TRUE; 176SYSCTL_INT(_vfs_zfs_vol, OID_AUTO, unmap_enabled, CTLFLAG_RWTUN, 177 &zvol_unmap_enabled, 0, 178 "Enable UNMAP functionality"); 179 180static d_open_t zvol_d_open; 181static d_close_t zvol_d_close; 182static d_read_t zvol_read; 183static d_write_t zvol_write; 184static d_ioctl_t zvol_d_ioctl; 185static d_strategy_t zvol_strategy; 186 187static struct cdevsw zvol_cdevsw = { 188 .d_version = D_VERSION, 189 .d_open = zvol_d_open, 190 .d_close = zvol_d_close, 191 .d_read = zvol_read, 192 .d_write = zvol_write, 193 .d_ioctl = zvol_d_ioctl, 194 .d_strategy = zvol_strategy, 195 .d_name = "zvol", 196 .d_flags = D_DISK | D_TRACKCLOSE, 197}; 198 199extern int zfs_set_prop_nvlist(const char *, zprop_source_t, 200 nvlist_t *, nvlist_t *); 201static void zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off, 202 uint64_t len, boolean_t sync); 203static int zvol_remove_zv(zvol_state_t *); 204static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio); 205static int zvol_dumpify(zvol_state_t *zv); 206static int zvol_dump_fini(zvol_state_t *zv); 207static int zvol_dump_init(zvol_state_t *zv, boolean_t resize); 208 209static void zvol_geom_run(zvol_state_t *zv); 210static void zvol_geom_destroy(zvol_state_t *zv); 211static int zvol_geom_access(struct g_provider *pp, int acr, int acw, int ace); 212static void zvol_geom_start(struct bio *bp); 213static void zvol_geom_worker(void *arg); 214 215static void 216zvol_size_changed(zvol_state_t *zv) 217{ 218#ifdef sun 219 dev_t dev = makedevice(maj, min); 220 221 VERIFY(ddi_prop_update_int64(dev, zfs_dip, 222 "Size", volsize) == DDI_SUCCESS); 223 VERIFY(ddi_prop_update_int64(dev, zfs_dip, 224 "Nblocks", lbtodb(volsize)) == DDI_SUCCESS); 225 226 /* Notify specfs to invalidate the cached size */ 227 spec_size_invalidate(dev, VBLK); 228 spec_size_invalidate(dev, VCHR); 229#else /* !sun */ 230 if (zv->zv_volmode == ZFS_VOLMODE_GEOM) { 231 struct g_provider *pp; 232 233 pp = zv->zv_provider; 234 if (pp == NULL) 235 return; 236 g_topology_lock(); 237 g_resize_provider(pp, zv->zv_volsize); 238 g_topology_unlock(); 239 } 240#endif /* !sun */ 241} 242 243int 244zvol_check_volsize(uint64_t volsize, uint64_t blocksize) 245{ 246 if (volsize == 0) 247 return (SET_ERROR(EINVAL)); 248 249 if (volsize % blocksize != 0) 250 return (SET_ERROR(EINVAL)); 251 252#ifdef _ILP32 253 if (volsize - 1 > SPEC_MAXOFFSET_T) 254 return (SET_ERROR(EOVERFLOW)); 255#endif 256 return (0); 257} 258 259int 260zvol_check_volblocksize(uint64_t volblocksize) 261{ 262 if (volblocksize < SPA_MINBLOCKSIZE || 263 volblocksize > SPA_MAXBLOCKSIZE || 264 !ISP2(volblocksize)) 265 return (SET_ERROR(EDOM)); 266 267 return (0); 268} 269 270int 271zvol_get_stats(objset_t *os, nvlist_t *nv) 272{ 273 int error; 274 dmu_object_info_t doi; 275 uint64_t val; 276 277 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val); 278 if (error) 279 return (error); 280 281 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val); 282 283 error = dmu_object_info(os, ZVOL_OBJ, &doi); 284 285 if (error == 0) { 286 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE, 287 doi.doi_data_block_size); 288 } 289 290 return (error); 291} 292 293static zvol_state_t * 294zvol_minor_lookup(const char *name) 295{ 296 zvol_state_t *zv; 297 298 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 299 300 LIST_FOREACH(zv, &all_zvols, zv_links) { 301 if (strcmp(zv->zv_name, name) == 0) 302 break; 303 } 304 305 return (zv); 306} 307 308/* extent mapping arg */ 309struct maparg { 310 zvol_state_t *ma_zv; 311 uint64_t ma_blks; 312}; 313 314/*ARGSUSED*/ 315static int 316zvol_map_block(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 317 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) 318{ 319 struct maparg *ma = arg; 320 zvol_extent_t *ze; 321 int bs = ma->ma_zv->zv_volblocksize; 322 323 if (BP_IS_HOLE(bp) || 324 zb->zb_object != ZVOL_OBJ || zb->zb_level != 0) 325 return (0); 326 327 VERIFY(!BP_IS_EMBEDDED(bp)); 328 329 VERIFY3U(ma->ma_blks, ==, zb->zb_blkid); 330 ma->ma_blks++; 331 332 /* Abort immediately if we have encountered gang blocks */ 333 if (BP_IS_GANG(bp)) 334 return (SET_ERROR(EFRAGS)); 335 336 /* 337 * See if the block is at the end of the previous extent. 338 */ 339 ze = list_tail(&ma->ma_zv->zv_extents); 340 if (ze && 341 DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) && 342 DVA_GET_OFFSET(BP_IDENTITY(bp)) == 343 DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) { 344 ze->ze_nblks++; 345 return (0); 346 } 347 348 dprintf_bp(bp, "%s", "next blkptr:"); 349 350 /* start a new extent */ 351 ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP); 352 ze->ze_dva = bp->blk_dva[0]; /* structure assignment */ 353 ze->ze_nblks = 1; 354 list_insert_tail(&ma->ma_zv->zv_extents, ze); 355 return (0); 356} 357 358static void 359zvol_free_extents(zvol_state_t *zv) 360{ 361 zvol_extent_t *ze; 362 363 while (ze = list_head(&zv->zv_extents)) { 364 list_remove(&zv->zv_extents, ze); 365 kmem_free(ze, sizeof (zvol_extent_t)); 366 } 367} 368 369static int 370zvol_get_lbas(zvol_state_t *zv) 371{ 372 objset_t *os = zv->zv_objset; 373 struct maparg ma; 374 int err; 375 376 ma.ma_zv = zv; 377 ma.ma_blks = 0; 378 zvol_free_extents(zv); 379 380 /* commit any in-flight changes before traversing the dataset */ 381 txg_wait_synced(dmu_objset_pool(os), 0); 382 err = traverse_dataset(dmu_objset_ds(os), 0, 383 TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma); 384 if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) { 385 zvol_free_extents(zv); 386 return (err ? err : EIO); 387 } 388 389 return (0); 390} 391 392/* ARGSUSED */ 393void 394zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 395{ 396 zfs_creat_t *zct = arg; 397 nvlist_t *nvprops = zct->zct_props; 398 int error; 399 uint64_t volblocksize, volsize; 400 401 VERIFY(nvlist_lookup_uint64(nvprops, 402 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0); 403 if (nvlist_lookup_uint64(nvprops, 404 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0) 405 volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 406 407 /* 408 * These properties must be removed from the list so the generic 409 * property setting step won't apply to them. 410 */ 411 VERIFY(nvlist_remove_all(nvprops, 412 zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0); 413 (void) nvlist_remove_all(nvprops, 414 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE)); 415 416 error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize, 417 DMU_OT_NONE, 0, tx); 418 ASSERT(error == 0); 419 420 error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP, 421 DMU_OT_NONE, 0, tx); 422 ASSERT(error == 0); 423 424 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx); 425 ASSERT(error == 0); 426} 427 428/* 429 * Replay a TX_TRUNCATE ZIL transaction if asked. TX_TRUNCATE is how we 430 * implement DKIOCFREE/free-long-range. 431 */ 432static int 433zvol_replay_truncate(zvol_state_t *zv, lr_truncate_t *lr, boolean_t byteswap) 434{ 435 uint64_t offset, length; 436 437 if (byteswap) 438 byteswap_uint64_array(lr, sizeof (*lr)); 439 440 offset = lr->lr_offset; 441 length = lr->lr_length; 442 443 return (dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, offset, length)); 444} 445 446/* 447 * Replay a TX_WRITE ZIL transaction that didn't get committed 448 * after a system failure 449 */ 450static int 451zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap) 452{ 453 objset_t *os = zv->zv_objset; 454 char *data = (char *)(lr + 1); /* data follows lr_write_t */ 455 uint64_t offset, length; 456 dmu_tx_t *tx; 457 int error; 458 459 if (byteswap) 460 byteswap_uint64_array(lr, sizeof (*lr)); 461 462 offset = lr->lr_offset; 463 length = lr->lr_length; 464 465 /* If it's a dmu_sync() block, write the whole block */ 466 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) { 467 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr); 468 if (length < blocksize) { 469 offset -= offset % blocksize; 470 length = blocksize; 471 } 472 } 473 474 tx = dmu_tx_create(os); 475 dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length); 476 error = dmu_tx_assign(tx, TXG_WAIT); 477 if (error) { 478 dmu_tx_abort(tx); 479 } else { 480 dmu_write(os, ZVOL_OBJ, offset, length, data, tx); 481 dmu_tx_commit(tx); 482 } 483 484 return (error); 485} 486 487/* ARGSUSED */ 488static int 489zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap) 490{ 491 return (SET_ERROR(ENOTSUP)); 492} 493 494/* 495 * Callback vectors for replaying records. 496 * Only TX_WRITE and TX_TRUNCATE are needed for zvol. 497 */ 498zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = { 499 zvol_replay_err, /* 0 no such transaction type */ 500 zvol_replay_err, /* TX_CREATE */ 501 zvol_replay_err, /* TX_MKDIR */ 502 zvol_replay_err, /* TX_MKXATTR */ 503 zvol_replay_err, /* TX_SYMLINK */ 504 zvol_replay_err, /* TX_REMOVE */ 505 zvol_replay_err, /* TX_RMDIR */ 506 zvol_replay_err, /* TX_LINK */ 507 zvol_replay_err, /* TX_RENAME */ 508 zvol_replay_write, /* TX_WRITE */ 509 zvol_replay_truncate, /* TX_TRUNCATE */ 510 zvol_replay_err, /* TX_SETATTR */ 511 zvol_replay_err, /* TX_ACL */ 512 zvol_replay_err, /* TX_CREATE_ACL */ 513 zvol_replay_err, /* TX_CREATE_ATTR */ 514 zvol_replay_err, /* TX_CREATE_ACL_ATTR */ 515 zvol_replay_err, /* TX_MKDIR_ACL */ 516 zvol_replay_err, /* TX_MKDIR_ATTR */ 517 zvol_replay_err, /* TX_MKDIR_ACL_ATTR */ 518 zvol_replay_err, /* TX_WRITE2 */ 519}; 520 521#ifdef sun 522int 523zvol_name2minor(const char *name, minor_t *minor) 524{ 525 zvol_state_t *zv; 526 527 mutex_enter(&spa_namespace_lock); 528 zv = zvol_minor_lookup(name); 529 if (minor && zv) 530 *minor = zv->zv_minor; 531 mutex_exit(&spa_namespace_lock); 532 return (zv ? 0 : -1); 533} 534#endif /* sun */ 535 536/* 537 * Create a minor node (plus a whole lot more) for the specified volume. 538 */ 539int 540zvol_create_minor(const char *name) 541{ 542 zfs_soft_state_t *zs; 543 zvol_state_t *zv; 544 objset_t *os; 545 struct cdev *dev; 546 struct g_provider *pp; 547 struct g_geom *gp; 548 dmu_object_info_t doi; 549 uint64_t volsize, mode; 550 int error; 551 552 ZFS_LOG(1, "Creating ZVOL %s...", name); 553 554 mutex_enter(&spa_namespace_lock); 555 556 if (zvol_minor_lookup(name) != NULL) { 557 mutex_exit(&spa_namespace_lock); 558 return (SET_ERROR(EEXIST)); 559 } 560 561 /* lie and say we're read-only */ 562 error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, FTAG, &os); 563 564 if (error) { 565 mutex_exit(&spa_namespace_lock); 566 return (error); 567 } 568 569#ifdef sun 570 if ((minor = zfsdev_minor_alloc()) == 0) { 571 dmu_objset_disown(os, FTAG); 572 mutex_exit(&spa_namespace_lock); 573 return (SET_ERROR(ENXIO)); 574 } 575 576 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS) { 577 dmu_objset_disown(os, FTAG); 578 mutex_exit(&spa_namespace_lock); 579 return (SET_ERROR(EAGAIN)); 580 } 581 (void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME, 582 (char *)name); 583 584 (void) snprintf(chrbuf, sizeof (chrbuf), "%u,raw", minor); 585 586 if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR, 587 minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 588 ddi_soft_state_free(zfsdev_state, minor); 589 dmu_objset_disown(os, FTAG); 590 mutex_exit(&spa_namespace_lock); 591 return (SET_ERROR(EAGAIN)); 592 } 593 594 (void) snprintf(blkbuf, sizeof (blkbuf), "%u", minor); 595 596 if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK, 597 minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 598 ddi_remove_minor_node(zfs_dip, chrbuf); 599 ddi_soft_state_free(zfsdev_state, minor); 600 dmu_objset_disown(os, FTAG); 601 mutex_exit(&spa_namespace_lock); 602 return (SET_ERROR(EAGAIN)); 603 } 604 605 zs = ddi_get_soft_state(zfsdev_state, minor); 606 zs->zss_type = ZSST_ZVOL; 607 zv = zs->zss_data = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP); 608#else /* !sun */ 609 610 zv = kmem_zalloc(sizeof(*zv), KM_SLEEP); 611 zv->zv_state = 0; 612 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize); 613 if (error) { 614 kmem_free(zv, sizeof(*zv)); 615 dmu_objset_disown(os, zvol_tag); 616 mutex_exit(&spa_namespace_lock); 617 return (error); 618 } 619 error = dsl_prop_get_integer(name, 620 zfs_prop_to_name(ZFS_PROP_VOLMODE), &mode, NULL); 621 if (error != 0 || mode == ZFS_VOLMODE_DEFAULT) 622 mode = volmode; 623 624 DROP_GIANT(); 625 zv->zv_volsize = volsize; 626 zv->zv_volmode = mode; 627 if (zv->zv_volmode == ZFS_VOLMODE_GEOM) { 628 g_topology_lock(); 629 gp = g_new_geomf(&zfs_zvol_class, "zfs::zvol::%s", name); 630 gp->start = zvol_geom_start; 631 gp->access = zvol_geom_access; 632 pp = g_new_providerf(gp, "%s/%s", ZVOL_DRIVER, name); 633 pp->flags |= G_PF_DIRECT_RECEIVE | G_PF_DIRECT_SEND; 634 pp->sectorsize = DEV_BSIZE; 635 pp->mediasize = zv->zv_volsize; 636 pp->private = zv; 637 638 zv->zv_provider = pp; 639 bioq_init(&zv->zv_queue); 640 mtx_init(&zv->zv_queue_mtx, "zvol", NULL, MTX_DEF); 641 } else if (zv->zv_volmode == ZFS_VOLMODE_DEV) { 642 if (make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, 643 &dev, &zvol_cdevsw, NULL, UID_ROOT, GID_OPERATOR, 644 0640, "%s/%s", ZVOL_DRIVER, name) != 0) { 645 kmem_free(zv, sizeof(*zv)); 646 dmu_objset_disown(os, FTAG); 647 mutex_exit(&spa_namespace_lock); 648 return (SET_ERROR(ENXIO)); 649 } 650 zv->zv_dev = dev; 651 dev->si_iosize_max = MAXPHYS; 652 dev->si_drv2 = zv; 653 } 654 LIST_INSERT_HEAD(&all_zvols, zv, zv_links); 655#endif /* !sun */ 656 657 (void) strlcpy(zv->zv_name, name, MAXPATHLEN); 658 zv->zv_min_bs = DEV_BSHIFT; 659 zv->zv_objset = os; 660 if (dmu_objset_is_snapshot(os) || !spa_writeable(dmu_objset_spa(os))) 661 zv->zv_flags |= ZVOL_RDONLY; 662 mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL); 663 avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare, 664 sizeof (rl_t), offsetof(rl_t, r_node)); 665 list_create(&zv->zv_extents, sizeof (zvol_extent_t), 666 offsetof(zvol_extent_t, ze_node)); 667 /* get and cache the blocksize */ 668 error = dmu_object_info(os, ZVOL_OBJ, &doi); 669 ASSERT(error == 0); 670 zv->zv_volblocksize = doi.doi_data_block_size; 671 672 if (spa_writeable(dmu_objset_spa(os))) { 673 if (zil_replay_disable) 674 zil_destroy(dmu_objset_zil(os), B_FALSE); 675 else 676 zil_replay(os, zv, zvol_replay_vector); 677 } 678 dmu_objset_disown(os, FTAG); 679 zv->zv_objset = NULL; 680 681 zvol_minors++; 682 683 mutex_exit(&spa_namespace_lock); 684 685#ifndef sun 686 if (zv->zv_volmode == ZFS_VOLMODE_GEOM) { 687 zvol_geom_run(zv); 688 g_topology_unlock(); 689 } 690 PICKUP_GIANT(); 691#endif 692 693 ZFS_LOG(1, "ZVOL %s created.", name); 694 695 return (0); 696} 697 698/* 699 * Remove minor node for the specified volume. 700 */ 701static int 702zvol_remove_zv(zvol_state_t *zv) 703{ 704#ifdef sun 705 minor_t minor = zv->zv_minor; 706#endif 707 708 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 709 if (zv->zv_total_opens != 0) 710 return (SET_ERROR(EBUSY)); 711 712 ZFS_LOG(1, "ZVOL %s destroyed.", zv->zv_name); 713 714#ifdef sun 715 (void) snprintf(nmbuf, sizeof (nmbuf), "%u,raw", minor); 716 ddi_remove_minor_node(zfs_dip, nmbuf); 717#else 718 LIST_REMOVE(zv, zv_links); 719 if (zv->zv_volmode == ZFS_VOLMODE_GEOM) { 720 g_topology_lock(); 721 zvol_geom_destroy(zv); 722 g_topology_unlock(); 723 } else if (zv->zv_volmode == ZFS_VOLMODE_DEV) 724 destroy_dev(zv->zv_dev); 725#endif /* sun */ 726 727 avl_destroy(&zv->zv_znode.z_range_avl); 728 mutex_destroy(&zv->zv_znode.z_range_lock); 729 730 kmem_free(zv, sizeof(*zv)); 731 732 zvol_minors--; 733 return (0); 734} 735 736int 737zvol_remove_minor(const char *name) 738{ 739 zvol_state_t *zv; 740 int rc; 741 742 mutex_enter(&spa_namespace_lock); 743 if ((zv = zvol_minor_lookup(name)) == NULL) { 744 mutex_exit(&spa_namespace_lock); 745 return (SET_ERROR(ENXIO)); 746 } 747 rc = zvol_remove_zv(zv); 748 mutex_exit(&spa_namespace_lock); 749 return (rc); 750} 751 752int 753zvol_first_open(zvol_state_t *zv) 754{ 755 objset_t *os; 756 uint64_t volsize; 757 int error; 758 uint64_t readonly; 759 760 /* lie and say we're read-only */ 761 error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, B_TRUE, 762 zvol_tag, &os); 763 if (error) 764 return (error); 765 766 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize); 767 if (error) { 768 ASSERT(error == 0); 769 dmu_objset_disown(os, zvol_tag); 770 return (error); 771 } 772 zv->zv_objset = os; 773 error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf); 774 if (error) { 775 dmu_objset_disown(os, zvol_tag); 776 return (error); 777 } 778 zv->zv_volsize = volsize; 779 zv->zv_zilog = zil_open(os, zvol_get_data); 780 zvol_size_changed(zv); 781 782 VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &readonly, 783 NULL) == 0); 784 if (readonly || dmu_objset_is_snapshot(os) || 785 !spa_writeable(dmu_objset_spa(os))) 786 zv->zv_flags |= ZVOL_RDONLY; 787 else 788 zv->zv_flags &= ~ZVOL_RDONLY; 789 return (error); 790} 791 792void 793zvol_last_close(zvol_state_t *zv) 794{ 795 zil_close(zv->zv_zilog); 796 zv->zv_zilog = NULL; 797 798 dmu_buf_rele(zv->zv_dbuf, zvol_tag); 799 zv->zv_dbuf = NULL; 800 801 /* 802 * Evict cached data 803 */ 804 if (dsl_dataset_is_dirty(dmu_objset_ds(zv->zv_objset)) && 805 !(zv->zv_flags & ZVOL_RDONLY)) 806 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 807 dmu_objset_evict_dbufs(zv->zv_objset); 808 809 dmu_objset_disown(zv->zv_objset, zvol_tag); 810 zv->zv_objset = NULL; 811} 812 813#ifdef sun 814int 815zvol_prealloc(zvol_state_t *zv) 816{ 817 objset_t *os = zv->zv_objset; 818 dmu_tx_t *tx; 819 uint64_t refd, avail, usedobjs, availobjs; 820 uint64_t resid = zv->zv_volsize; 821 uint64_t off = 0; 822 823 /* Check the space usage before attempting to allocate the space */ 824 dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs); 825 if (avail < zv->zv_volsize) 826 return (SET_ERROR(ENOSPC)); 827 828 /* Free old extents if they exist */ 829 zvol_free_extents(zv); 830 831 while (resid != 0) { 832 int error; 833 uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE); 834 835 tx = dmu_tx_create(os); 836 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 837 error = dmu_tx_assign(tx, TXG_WAIT); 838 if (error) { 839 dmu_tx_abort(tx); 840 (void) dmu_free_long_range(os, ZVOL_OBJ, 0, off); 841 return (error); 842 } 843 dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx); 844 dmu_tx_commit(tx); 845 off += bytes; 846 resid -= bytes; 847 } 848 txg_wait_synced(dmu_objset_pool(os), 0); 849 850 return (0); 851} 852#endif /* sun */ 853 854static int 855zvol_update_volsize(objset_t *os, uint64_t volsize) 856{ 857 dmu_tx_t *tx; 858 int error; 859 860 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 861 862 tx = dmu_tx_create(os); 863 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 864 dmu_tx_mark_netfree(tx); 865 error = dmu_tx_assign(tx, TXG_WAIT); 866 if (error) { 867 dmu_tx_abort(tx); 868 return (error); 869 } 870 871 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, 872 &volsize, tx); 873 dmu_tx_commit(tx); 874 875 if (error == 0) 876 error = dmu_free_long_range(os, 877 ZVOL_OBJ, volsize, DMU_OBJECT_END); 878 return (error); 879} 880 881void 882zvol_remove_minors(const char *name) 883{ 884 zvol_state_t *zv, *tzv; 885 size_t namelen; 886 887 namelen = strlen(name); 888 889 DROP_GIANT(); 890 mutex_enter(&spa_namespace_lock); 891 892 LIST_FOREACH_SAFE(zv, &all_zvols, zv_links, tzv) { 893 if (strcmp(zv->zv_name, name) == 0 || 894 (strncmp(zv->zv_name, name, namelen) == 0 && 895 strlen(zv->zv_name) > namelen && (zv->zv_name[namelen] == '/' || 896 zv->zv_name[namelen] == '@'))) { 897 (void) zvol_remove_zv(zv); 898 } 899 } 900 901 mutex_exit(&spa_namespace_lock); 902 PICKUP_GIANT(); 903} 904 905int 906zvol_set_volsize(const char *name, major_t maj, uint64_t volsize) 907{ 908 zvol_state_t *zv = NULL; 909 objset_t *os; 910 int error; 911 dmu_object_info_t doi; 912 uint64_t old_volsize = 0ULL; 913 uint64_t readonly; 914 915 mutex_enter(&spa_namespace_lock); 916 zv = zvol_minor_lookup(name); 917 if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) { 918 mutex_exit(&spa_namespace_lock); 919 return (error); 920 } 921 922 if ((error = dmu_object_info(os, ZVOL_OBJ, &doi)) != 0 || 923 (error = zvol_check_volsize(volsize, 924 doi.doi_data_block_size)) != 0) 925 goto out; 926 927 VERIFY(dsl_prop_get_integer(name, "readonly", &readonly, 928 NULL) == 0); 929 if (readonly) { 930 error = EROFS; 931 goto out; 932 } 933 934 error = zvol_update_volsize(os, volsize); 935 /* 936 * Reinitialize the dump area to the new size. If we 937 * failed to resize the dump area then restore it back to 938 * its original size. 939 */ 940 if (zv && error == 0) { 941#ifdef ZVOL_DUMP 942 if (zv->zv_flags & ZVOL_DUMPIFIED) { 943 old_volsize = zv->zv_volsize; 944 zv->zv_volsize = volsize; 945 if ((error = zvol_dumpify(zv)) != 0 || 946 (error = dumpvp_resize()) != 0) { 947 (void) zvol_update_volsize(os, old_volsize); 948 zv->zv_volsize = old_volsize; 949 error = zvol_dumpify(zv); 950 } 951 } 952#endif /* ZVOL_DUMP */ 953 if (error == 0) { 954 zv->zv_volsize = volsize; 955 zvol_size_changed(zv); 956 } 957 } 958 959#ifdef sun 960 /* 961 * Generate a LUN expansion event. 962 */ 963 if (zv && error == 0) { 964 sysevent_id_t eid; 965 nvlist_t *attr; 966 char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 967 968 (void) snprintf(physpath, MAXPATHLEN, "%s%u", ZVOL_PSEUDO_DEV, 969 zv->zv_minor); 970 971 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 972 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 973 974 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 975 ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 976 977 nvlist_free(attr); 978 kmem_free(physpath, MAXPATHLEN); 979 } 980#endif /* sun */ 981 982out: 983 dmu_objset_rele(os, FTAG); 984 985 mutex_exit(&spa_namespace_lock); 986 987 return (error); 988} 989 990/*ARGSUSED*/ 991static int 992zvol_open(struct g_provider *pp, int flag, int count) 993{ 994 zvol_state_t *zv; 995 int err = 0; 996 boolean_t locked = B_FALSE; 997 998 /* 999 * Protect against recursively entering spa_namespace_lock 1000 * when spa_open() is used for a pool on a (local) ZVOL(s). 1001 * This is needed since we replaced upstream zfsdev_state_lock 1002 * with spa_namespace_lock in the ZVOL code. 1003 * We are using the same trick as spa_open(). 1004 * Note that calls in zvol_first_open which need to resolve 1005 * pool name to a spa object will enter spa_open() 1006 * recursively, but that function already has all the 1007 * necessary protection. 1008 */ 1009 if (!MUTEX_HELD(&spa_namespace_lock)) { 1010 mutex_enter(&spa_namespace_lock); 1011 locked = B_TRUE; 1012 } 1013 1014 zv = pp->private; 1015 if (zv == NULL) { 1016 if (locked) 1017 mutex_exit(&spa_namespace_lock); 1018 return (SET_ERROR(ENXIO)); 1019 } 1020 1021 if (zv->zv_total_opens == 0) { 1022 err = zvol_first_open(zv); 1023 if (err) { 1024 if (locked) 1025 mutex_exit(&spa_namespace_lock); 1026 return (err); 1027 } 1028 pp->mediasize = zv->zv_volsize; 1029 pp->stripeoffset = 0; 1030 pp->stripesize = zv->zv_volblocksize; 1031 } 1032 if ((flag & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) { 1033 err = SET_ERROR(EROFS); 1034 goto out; 1035 } 1036 if (zv->zv_flags & ZVOL_EXCL) { 1037 err = SET_ERROR(EBUSY); 1038 goto out; 1039 } 1040#ifdef FEXCL 1041 if (flag & FEXCL) { 1042 if (zv->zv_total_opens != 0) { 1043 err = SET_ERROR(EBUSY); 1044 goto out; 1045 } 1046 zv->zv_flags |= ZVOL_EXCL; 1047 } 1048#endif 1049 1050 zv->zv_total_opens += count; 1051 if (locked) 1052 mutex_exit(&spa_namespace_lock); 1053 1054 return (err); 1055out: 1056 if (zv->zv_total_opens == 0) 1057 zvol_last_close(zv); 1058 if (locked) 1059 mutex_exit(&spa_namespace_lock); 1060 return (err); 1061} 1062 1063/*ARGSUSED*/ 1064static int 1065zvol_close(struct g_provider *pp, int flag, int count) 1066{ 1067 zvol_state_t *zv; 1068 int error = 0; 1069 boolean_t locked = B_FALSE; 1070 1071 /* See comment in zvol_open(). */ 1072 if (!MUTEX_HELD(&spa_namespace_lock)) { 1073 mutex_enter(&spa_namespace_lock); 1074 locked = B_TRUE; 1075 } 1076 1077 zv = pp->private; 1078 if (zv == NULL) { 1079 if (locked) 1080 mutex_exit(&spa_namespace_lock); 1081 return (SET_ERROR(ENXIO)); 1082 } 1083 1084 if (zv->zv_flags & ZVOL_EXCL) { 1085 ASSERT(zv->zv_total_opens == 1); 1086 zv->zv_flags &= ~ZVOL_EXCL; 1087 } 1088 1089 /* 1090 * If the open count is zero, this is a spurious close. 1091 * That indicates a bug in the kernel / DDI framework. 1092 */ 1093 ASSERT(zv->zv_total_opens != 0); 1094 1095 /* 1096 * You may get multiple opens, but only one close. 1097 */ 1098 zv->zv_total_opens -= count; 1099 1100 if (zv->zv_total_opens == 0) 1101 zvol_last_close(zv); 1102 1103 if (locked) 1104 mutex_exit(&spa_namespace_lock); 1105 return (error); 1106} 1107 1108static void 1109zvol_get_done(zgd_t *zgd, int error) 1110{ 1111 if (zgd->zgd_db) 1112 dmu_buf_rele(zgd->zgd_db, zgd); 1113 1114 zfs_range_unlock(zgd->zgd_rl); 1115 1116 if (error == 0 && zgd->zgd_bp) 1117 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp); 1118 1119 kmem_free(zgd, sizeof (zgd_t)); 1120} 1121 1122/* 1123 * Get data to generate a TX_WRITE intent log record. 1124 */ 1125static int 1126zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 1127{ 1128 zvol_state_t *zv = arg; 1129 objset_t *os = zv->zv_objset; 1130 uint64_t object = ZVOL_OBJ; 1131 uint64_t offset = lr->lr_offset; 1132 uint64_t size = lr->lr_length; /* length of user data */ 1133 blkptr_t *bp = &lr->lr_blkptr; 1134 dmu_buf_t *db; 1135 zgd_t *zgd; 1136 int error; 1137 1138 ASSERT(zio != NULL); 1139 ASSERT(size != 0); 1140 1141 zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP); 1142 zgd->zgd_zilog = zv->zv_zilog; 1143 zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER); 1144 1145 /* 1146 * Write records come in two flavors: immediate and indirect. 1147 * For small writes it's cheaper to store the data with the 1148 * log record (immediate); for large writes it's cheaper to 1149 * sync the data and get a pointer to it (indirect) so that 1150 * we don't have to write the data twice. 1151 */ 1152 if (buf != NULL) { /* immediate write */ 1153 error = dmu_read(os, object, offset, size, buf, 1154 DMU_READ_NO_PREFETCH); 1155 } else { 1156 size = zv->zv_volblocksize; 1157 offset = P2ALIGN(offset, size); 1158 error = dmu_buf_hold(os, object, offset, zgd, &db, 1159 DMU_READ_NO_PREFETCH); 1160 if (error == 0) { 1161 blkptr_t *obp = dmu_buf_get_blkptr(db); 1162 if (obp) { 1163 ASSERT(BP_IS_HOLE(bp)); 1164 *bp = *obp; 1165 } 1166 1167 zgd->zgd_db = db; 1168 zgd->zgd_bp = bp; 1169 1170 ASSERT(db->db_offset == offset); 1171 ASSERT(db->db_size == size); 1172 1173 error = dmu_sync(zio, lr->lr_common.lrc_txg, 1174 zvol_get_done, zgd); 1175 1176 if (error == 0) 1177 return (0); 1178 } 1179 } 1180 1181 zvol_get_done(zgd, error); 1182 1183 return (error); 1184} 1185 1186/* 1187 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions. 1188 * 1189 * We store data in the log buffers if it's small enough. 1190 * Otherwise we will later flush the data out via dmu_sync(). 1191 */ 1192ssize_t zvol_immediate_write_sz = 32768; 1193 1194static void 1195zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid, 1196 boolean_t sync) 1197{ 1198 uint32_t blocksize = zv->zv_volblocksize; 1199 zilog_t *zilog = zv->zv_zilog; 1200 boolean_t slogging; 1201 ssize_t immediate_write_sz; 1202 1203 if (zil_replaying(zilog, tx)) 1204 return; 1205 1206 immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT) 1207 ? 0 : zvol_immediate_write_sz; 1208 1209 slogging = spa_has_slogs(zilog->zl_spa) && 1210 (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY); 1211 1212 while (resid) { 1213 itx_t *itx; 1214 lr_write_t *lr; 1215 ssize_t len; 1216 itx_wr_state_t write_state; 1217 1218 /* 1219 * Unlike zfs_log_write() we can be called with 1220 * upto DMU_MAX_ACCESS/2 (5MB) writes. 1221 */ 1222 if (blocksize > immediate_write_sz && !slogging && 1223 resid >= blocksize && off % blocksize == 0) { 1224 write_state = WR_INDIRECT; /* uses dmu_sync */ 1225 len = blocksize; 1226 } else if (sync) { 1227 write_state = WR_COPIED; 1228 len = MIN(ZIL_MAX_LOG_DATA, resid); 1229 } else { 1230 write_state = WR_NEED_COPY; 1231 len = MIN(ZIL_MAX_LOG_DATA, resid); 1232 } 1233 1234 itx = zil_itx_create(TX_WRITE, sizeof (*lr) + 1235 (write_state == WR_COPIED ? len : 0)); 1236 lr = (lr_write_t *)&itx->itx_lr; 1237 if (write_state == WR_COPIED && dmu_read(zv->zv_objset, 1238 ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) { 1239 zil_itx_destroy(itx); 1240 itx = zil_itx_create(TX_WRITE, sizeof (*lr)); 1241 lr = (lr_write_t *)&itx->itx_lr; 1242 write_state = WR_NEED_COPY; 1243 } 1244 1245 itx->itx_wr_state = write_state; 1246 if (write_state == WR_NEED_COPY) 1247 itx->itx_sod += len; 1248 lr->lr_foid = ZVOL_OBJ; 1249 lr->lr_offset = off; 1250 lr->lr_length = len; 1251 lr->lr_blkoff = 0; 1252 BP_ZERO(&lr->lr_blkptr); 1253 1254 itx->itx_private = zv; 1255 itx->itx_sync = sync; 1256 1257 zil_itx_assign(zilog, itx, tx); 1258 1259 off += len; 1260 resid -= len; 1261 } 1262} 1263 1264#ifdef sun 1265static int 1266zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t origoffset, 1267 uint64_t size, boolean_t doread, boolean_t isdump) 1268{ 1269 vdev_disk_t *dvd; 1270 int c; 1271 int numerrors = 0; 1272 1273 if (vd->vdev_ops == &vdev_mirror_ops || 1274 vd->vdev_ops == &vdev_replacing_ops || 1275 vd->vdev_ops == &vdev_spare_ops) { 1276 for (c = 0; c < vd->vdev_children; c++) { 1277 int err = zvol_dumpio_vdev(vd->vdev_child[c], 1278 addr, offset, origoffset, size, doread, isdump); 1279 if (err != 0) { 1280 numerrors++; 1281 } else if (doread) { 1282 break; 1283 } 1284 } 1285 } 1286 1287 if (!vd->vdev_ops->vdev_op_leaf && vd->vdev_ops != &vdev_raidz_ops) 1288 return (numerrors < vd->vdev_children ? 0 : EIO); 1289 1290 if (doread && !vdev_readable(vd)) 1291 return (SET_ERROR(EIO)); 1292 else if (!doread && !vdev_writeable(vd)) 1293 return (SET_ERROR(EIO)); 1294 1295 if (vd->vdev_ops == &vdev_raidz_ops) { 1296 return (vdev_raidz_physio(vd, 1297 addr, size, offset, origoffset, doread, isdump)); 1298 } 1299 1300 offset += VDEV_LABEL_START_SIZE; 1301 1302 if (ddi_in_panic() || isdump) { 1303 ASSERT(!doread); 1304 if (doread) 1305 return (SET_ERROR(EIO)); 1306 dvd = vd->vdev_tsd; 1307 ASSERT3P(dvd, !=, NULL); 1308 return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset), 1309 lbtodb(size))); 1310 } else { 1311 dvd = vd->vdev_tsd; 1312 ASSERT3P(dvd, !=, NULL); 1313 return (vdev_disk_ldi_physio(dvd->vd_lh, addr, size, 1314 offset, doread ? B_READ : B_WRITE)); 1315 } 1316} 1317 1318static int 1319zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size, 1320 boolean_t doread, boolean_t isdump) 1321{ 1322 vdev_t *vd; 1323 int error; 1324 zvol_extent_t *ze; 1325 spa_t *spa = dmu_objset_spa(zv->zv_objset); 1326 1327 /* Must be sector aligned, and not stradle a block boundary. */ 1328 if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) || 1329 P2BOUNDARY(offset, size, zv->zv_volblocksize)) { 1330 return (SET_ERROR(EINVAL)); 1331 } 1332 ASSERT(size <= zv->zv_volblocksize); 1333 1334 /* Locate the extent this belongs to */ 1335 ze = list_head(&zv->zv_extents); 1336 while (offset >= ze->ze_nblks * zv->zv_volblocksize) { 1337 offset -= ze->ze_nblks * zv->zv_volblocksize; 1338 ze = list_next(&zv->zv_extents, ze); 1339 } 1340 1341 if (ze == NULL) 1342 return (SET_ERROR(EINVAL)); 1343 1344 if (!ddi_in_panic()) 1345 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 1346 1347 vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva)); 1348 offset += DVA_GET_OFFSET(&ze->ze_dva); 1349 error = zvol_dumpio_vdev(vd, addr, offset, DVA_GET_OFFSET(&ze->ze_dva), 1350 size, doread, isdump); 1351 1352 if (!ddi_in_panic()) 1353 spa_config_exit(spa, SCL_STATE, FTAG); 1354 1355 return (error); 1356} 1357#endif /* sun */ 1358 1359void 1360zvol_strategy(struct bio *bp) 1361{ 1362 zvol_state_t *zv; 1363 uint64_t off, volsize; 1364 size_t resid; 1365 char *addr; 1366 objset_t *os; 1367 rl_t *rl; 1368 int error = 0; 1369 boolean_t doread = 0; 1370 boolean_t is_dumpified; 1371 boolean_t sync; 1372 1373 if (bp->bio_to) 1374 zv = bp->bio_to->private; 1375 else 1376 zv = bp->bio_dev->si_drv2; 1377 1378 if (zv == NULL) { 1379 error = ENXIO; 1380 goto out; 1381 } 1382 1383 if (bp->bio_cmd != BIO_READ && (zv->zv_flags & ZVOL_RDONLY)) { 1384 error = EROFS; 1385 goto out; 1386 } 1387 1388 switch (bp->bio_cmd) { 1389 case BIO_FLUSH: 1390 goto sync; 1391 case BIO_READ: 1392 doread = 1; 1393 case BIO_WRITE: 1394 case BIO_DELETE: 1395 break; 1396 default: 1397 error = EOPNOTSUPP; 1398 goto out; 1399 } 1400 1401 off = bp->bio_offset; 1402 volsize = zv->zv_volsize; 1403 1404 os = zv->zv_objset; 1405 ASSERT(os != NULL); 1406 1407 addr = bp->bio_data; 1408 resid = bp->bio_length; 1409 1410 if (resid > 0 && (off < 0 || off >= volsize)) { 1411 error = EIO; 1412 goto out; 1413 } 1414 1415#ifdef illumos 1416 is_dumpified = zv->zv_flags & ZVOL_DUMPIFIED; 1417#else 1418 is_dumpified = B_FALSE; 1419#endif 1420 sync = !doread && !is_dumpified && 1421 zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS; 1422 1423 /* 1424 * There must be no buffer changes when doing a dmu_sync() because 1425 * we can't change the data whilst calculating the checksum. 1426 */ 1427 rl = zfs_range_lock(&zv->zv_znode, off, resid, 1428 doread ? RL_READER : RL_WRITER); 1429 1430 if (bp->bio_cmd == BIO_DELETE) { 1431 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset); 1432 error = dmu_tx_assign(tx, TXG_WAIT); 1433 if (error != 0) { 1434 dmu_tx_abort(tx); 1435 } else { 1436 zvol_log_truncate(zv, tx, off, resid, B_TRUE); 1437 dmu_tx_commit(tx); 1438 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 1439 off, resid); 1440 resid = 0; 1441 } 1442 goto unlock; 1443 } 1444 1445 while (resid != 0 && off < volsize) { 1446 size_t size = MIN(resid, zvol_maxphys); 1447#ifdef illumos 1448 if (is_dumpified) { 1449 size = MIN(size, P2END(off, zv->zv_volblocksize) - off); 1450 error = zvol_dumpio(zv, addr, off, size, 1451 doread, B_FALSE); 1452 } else if (doread) { 1453#else 1454 if (doread) { 1455#endif 1456 error = dmu_read(os, ZVOL_OBJ, off, size, addr, 1457 DMU_READ_PREFETCH); 1458 } else { 1459 dmu_tx_t *tx = dmu_tx_create(os); 1460 dmu_tx_hold_write(tx, ZVOL_OBJ, off, size); 1461 error = dmu_tx_assign(tx, TXG_WAIT); 1462 if (error) { 1463 dmu_tx_abort(tx); 1464 } else { 1465 dmu_write(os, ZVOL_OBJ, off, size, addr, tx); 1466 zvol_log_write(zv, tx, off, size, sync); 1467 dmu_tx_commit(tx); 1468 } 1469 } 1470 if (error) { 1471 /* convert checksum errors into IO errors */ 1472 if (error == ECKSUM) 1473 error = SET_ERROR(EIO); 1474 break; 1475 } 1476 off += size; 1477 addr += size; 1478 resid -= size; 1479 } 1480unlock: 1481 zfs_range_unlock(rl); 1482 1483 bp->bio_completed = bp->bio_length - resid; 1484 if (bp->bio_completed < bp->bio_length && off > volsize) 1485 error = EINVAL; 1486 1487 if (sync) { 1488sync: 1489 zil_commit(zv->zv_zilog, ZVOL_OBJ); 1490 } 1491out: 1492 if (bp->bio_to) 1493 g_io_deliver(bp, error); 1494 else 1495 biofinish(bp, NULL, error); 1496} 1497 1498#ifdef sun 1499/* 1500 * Set the buffer count to the zvol maximum transfer. 1501 * Using our own routine instead of the default minphys() 1502 * means that for larger writes we write bigger buffers on X86 1503 * (128K instead of 56K) and flush the disk write cache less often 1504 * (every zvol_maxphys - currently 1MB) instead of minphys (currently 1505 * 56K on X86 and 128K on sparc). 1506 */ 1507void 1508zvol_minphys(struct buf *bp) 1509{ 1510 if (bp->b_bcount > zvol_maxphys) 1511 bp->b_bcount = zvol_maxphys; 1512} 1513 1514int 1515zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks) 1516{ 1517 minor_t minor = getminor(dev); 1518 zvol_state_t *zv; 1519 int error = 0; 1520 uint64_t size; 1521 uint64_t boff; 1522 uint64_t resid; 1523 1524 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1525 if (zv == NULL) 1526 return (SET_ERROR(ENXIO)); 1527 1528 if ((zv->zv_flags & ZVOL_DUMPIFIED) == 0) 1529 return (SET_ERROR(EINVAL)); 1530 1531 boff = ldbtob(blkno); 1532 resid = ldbtob(nblocks); 1533 1534 VERIFY3U(boff + resid, <=, zv->zv_volsize); 1535 1536 while (resid) { 1537 size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff); 1538 error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE); 1539 if (error) 1540 break; 1541 boff += size; 1542 addr += size; 1543 resid -= size; 1544 } 1545 1546 return (error); 1547} 1548 1549/*ARGSUSED*/ 1550int 1551zvol_read(dev_t dev, uio_t *uio, cred_t *cr) 1552{ 1553 minor_t minor = getminor(dev); 1554#else 1555int 1556zvol_read(struct cdev *dev, struct uio *uio, int ioflag) 1557{ 1558#endif 1559 zvol_state_t *zv; 1560 uint64_t volsize; 1561 rl_t *rl; 1562 int error = 0; 1563 1564#ifdef sun 1565 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1566 if (zv == NULL) 1567 return (SET_ERROR(ENXIO)); 1568#else 1569 zv = dev->si_drv2; 1570#endif 1571 1572 volsize = zv->zv_volsize; 1573 if (uio->uio_resid > 0 && 1574 (uio->uio_loffset < 0 || uio->uio_loffset > volsize)) 1575 return (SET_ERROR(EIO)); 1576 1577#ifdef illumos 1578 if (zv->zv_flags & ZVOL_DUMPIFIED) { 1579 error = physio(zvol_strategy, NULL, dev, B_READ, 1580 zvol_minphys, uio); 1581 return (error); 1582 } 1583#endif 1584 1585 rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 1586 RL_READER); 1587 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 1588 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 1589 1590 /* don't read past the end */ 1591 if (bytes > volsize - uio->uio_loffset) 1592 bytes = volsize - uio->uio_loffset; 1593 1594 error = dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes); 1595 if (error) { 1596 /* convert checksum errors into IO errors */ 1597 if (error == ECKSUM) 1598 error = SET_ERROR(EIO); 1599 break; 1600 } 1601 } 1602 zfs_range_unlock(rl); 1603 return (error); 1604} 1605 1606#ifdef sun 1607/*ARGSUSED*/ 1608int 1609zvol_write(dev_t dev, uio_t *uio, cred_t *cr) 1610{ 1611 minor_t minor = getminor(dev); 1612#else 1613int 1614zvol_write(struct cdev *dev, struct uio *uio, int ioflag) 1615{ 1616#endif 1617 zvol_state_t *zv; 1618 uint64_t volsize; 1619 rl_t *rl; 1620 int error = 0; 1621 boolean_t sync; 1622 1623#ifdef sun 1624 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1625 if (zv == NULL) 1626 return (SET_ERROR(ENXIO)); 1627#else 1628 zv = dev->si_drv2; 1629#endif 1630 1631 volsize = zv->zv_volsize; 1632 if (uio->uio_resid > 0 && 1633 (uio->uio_loffset < 0 || uio->uio_loffset > volsize)) 1634 return (SET_ERROR(EIO)); 1635 1636#ifdef illumos 1637 if (zv->zv_flags & ZVOL_DUMPIFIED) { 1638 error = physio(zvol_strategy, NULL, dev, B_WRITE, 1639 zvol_minphys, uio); 1640 return (error); 1641 } 1642#endif 1643 1644#ifdef sun 1645 sync = !(zv->zv_flags & ZVOL_WCE) || 1646#else 1647 sync = (ioflag & IO_SYNC) || 1648#endif 1649 (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS); 1650 1651 rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 1652 RL_WRITER); 1653 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 1654 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 1655 uint64_t off = uio->uio_loffset; 1656 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset); 1657 1658 if (bytes > volsize - off) /* don't write past the end */ 1659 bytes = volsize - off; 1660 1661 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 1662 error = dmu_tx_assign(tx, TXG_WAIT); 1663 if (error) { 1664 dmu_tx_abort(tx); 1665 break; 1666 } 1667 error = dmu_write_uio_dbuf(zv->zv_dbuf, uio, bytes, tx); 1668 if (error == 0) 1669 zvol_log_write(zv, tx, off, bytes, sync); 1670 dmu_tx_commit(tx); 1671 1672 if (error) 1673 break; 1674 } 1675 zfs_range_unlock(rl); 1676 if (sync) 1677 zil_commit(zv->zv_zilog, ZVOL_OBJ); 1678 return (error); 1679} 1680 1681#ifdef sun 1682int 1683zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs) 1684{ 1685 struct uuid uuid = EFI_RESERVED; 1686 efi_gpe_t gpe = { 0 }; 1687 uint32_t crc; 1688 dk_efi_t efi; 1689 int length; 1690 char *ptr; 1691 1692 if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag)) 1693 return (SET_ERROR(EFAULT)); 1694 ptr = (char *)(uintptr_t)efi.dki_data_64; 1695 length = efi.dki_length; 1696 /* 1697 * Some clients may attempt to request a PMBR for the 1698 * zvol. Currently this interface will return EINVAL to 1699 * such requests. These requests could be supported by 1700 * adding a check for lba == 0 and consing up an appropriate 1701 * PMBR. 1702 */ 1703 if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0) 1704 return (SET_ERROR(EINVAL)); 1705 1706 gpe.efi_gpe_StartingLBA = LE_64(34ULL); 1707 gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1); 1708 UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid); 1709 1710 if (efi.dki_lba == 1) { 1711 efi_gpt_t gpt = { 0 }; 1712 1713 gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE); 1714 gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT); 1715 gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt)); 1716 gpt.efi_gpt_MyLBA = LE_64(1ULL); 1717 gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL); 1718 gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1); 1719 gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL); 1720 gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1); 1721 gpt.efi_gpt_SizeOfPartitionEntry = 1722 LE_32(sizeof (efi_gpe_t)); 1723 CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table); 1724 gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc); 1725 CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table); 1726 gpt.efi_gpt_HeaderCRC32 = LE_32(~crc); 1727 if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length), 1728 flag)) 1729 return (SET_ERROR(EFAULT)); 1730 ptr += sizeof (gpt); 1731 length -= sizeof (gpt); 1732 } 1733 if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe), 1734 length), flag)) 1735 return (SET_ERROR(EFAULT)); 1736 return (0); 1737} 1738 1739/* 1740 * BEGIN entry points to allow external callers access to the volume. 1741 */ 1742/* 1743 * Return the volume parameters needed for access from an external caller. 1744 * These values are invariant as long as the volume is held open. 1745 */ 1746int 1747zvol_get_volume_params(minor_t minor, uint64_t *blksize, 1748 uint64_t *max_xfer_len, void **minor_hdl, void **objset_hdl, void **zil_hdl, 1749 void **rl_hdl, void **bonus_hdl) 1750{ 1751 zvol_state_t *zv; 1752 1753 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1754 if (zv == NULL) 1755 return (SET_ERROR(ENXIO)); 1756 if (zv->zv_flags & ZVOL_DUMPIFIED) 1757 return (SET_ERROR(ENXIO)); 1758 1759 ASSERT(blksize && max_xfer_len && minor_hdl && 1760 objset_hdl && zil_hdl && rl_hdl && bonus_hdl); 1761 1762 *blksize = zv->zv_volblocksize; 1763 *max_xfer_len = (uint64_t)zvol_maxphys; 1764 *minor_hdl = zv; 1765 *objset_hdl = zv->zv_objset; 1766 *zil_hdl = zv->zv_zilog; 1767 *rl_hdl = &zv->zv_znode; 1768 *bonus_hdl = zv->zv_dbuf; 1769 return (0); 1770} 1771 1772/* 1773 * Return the current volume size to an external caller. 1774 * The size can change while the volume is open. 1775 */ 1776uint64_t 1777zvol_get_volume_size(void *minor_hdl) 1778{ 1779 zvol_state_t *zv = minor_hdl; 1780 1781 return (zv->zv_volsize); 1782} 1783 1784/* 1785 * Return the current WCE setting to an external caller. 1786 * The WCE setting can change while the volume is open. 1787 */ 1788int 1789zvol_get_volume_wce(void *minor_hdl) 1790{ 1791 zvol_state_t *zv = minor_hdl; 1792 1793 return ((zv->zv_flags & ZVOL_WCE) ? 1 : 0); 1794} 1795 1796/* 1797 * Entry point for external callers to zvol_log_write 1798 */ 1799void 1800zvol_log_write_minor(void *minor_hdl, dmu_tx_t *tx, offset_t off, ssize_t resid, 1801 boolean_t sync) 1802{ 1803 zvol_state_t *zv = minor_hdl; 1804 1805 zvol_log_write(zv, tx, off, resid, sync); 1806} 1807/* 1808 * END entry points to allow external callers access to the volume. 1809 */ 1810#endif /* sun */ 1811 1812/* 1813 * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE. 1814 */ 1815static void 1816zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off, uint64_t len, 1817 boolean_t sync) 1818{ 1819 itx_t *itx; 1820 lr_truncate_t *lr; 1821 zilog_t *zilog = zv->zv_zilog; 1822 1823 if (zil_replaying(zilog, tx)) 1824 return; 1825 1826 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr)); 1827 lr = (lr_truncate_t *)&itx->itx_lr; 1828 lr->lr_foid = ZVOL_OBJ; 1829 lr->lr_offset = off; 1830 lr->lr_length = len; 1831 1832 itx->itx_sync = sync; 1833 zil_itx_assign(zilog, itx, tx); 1834} 1835 1836#ifdef sun 1837/* 1838 * Dirtbag ioctls to support mkfs(1M) for UFS filesystems. See dkio(7I). 1839 * Also a dirtbag dkio ioctl for unmap/free-block functionality. 1840 */ 1841/*ARGSUSED*/ 1842int 1843zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 1844{ 1845 zvol_state_t *zv; 1846 struct dk_callback *dkc; 1847 int error = 0; 1848 rl_t *rl; 1849 1850 mutex_enter(&spa_namespace_lock); 1851 1852 zv = zfsdev_get_soft_state(getminor(dev), ZSST_ZVOL); 1853 1854 if (zv == NULL) { 1855 mutex_exit(&spa_namespace_lock); 1856 return (SET_ERROR(ENXIO)); 1857 } 1858 ASSERT(zv->zv_total_opens > 0); 1859 1860 switch (cmd) { 1861 1862 case DKIOCINFO: 1863 { 1864 struct dk_cinfo dki; 1865 1866 bzero(&dki, sizeof (dki)); 1867 (void) strcpy(dki.dki_cname, "zvol"); 1868 (void) strcpy(dki.dki_dname, "zvol"); 1869 dki.dki_ctype = DKC_UNKNOWN; 1870 dki.dki_unit = getminor(dev); 1871 dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs); 1872 mutex_exit(&spa_namespace_lock); 1873 if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag)) 1874 error = SET_ERROR(EFAULT); 1875 return (error); 1876 } 1877 1878 case DKIOCGMEDIAINFO: 1879 { 1880 struct dk_minfo dkm; 1881 1882 bzero(&dkm, sizeof (dkm)); 1883 dkm.dki_lbsize = 1U << zv->zv_min_bs; 1884 dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs; 1885 dkm.dki_media_type = DK_UNKNOWN; 1886 mutex_exit(&spa_namespace_lock); 1887 if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag)) 1888 error = SET_ERROR(EFAULT); 1889 return (error); 1890 } 1891 1892 case DKIOCGMEDIAINFOEXT: 1893 { 1894 struct dk_minfo_ext dkmext; 1895 1896 bzero(&dkmext, sizeof (dkmext)); 1897 dkmext.dki_lbsize = 1U << zv->zv_min_bs; 1898 dkmext.dki_pbsize = zv->zv_volblocksize; 1899 dkmext.dki_capacity = zv->zv_volsize >> zv->zv_min_bs; 1900 dkmext.dki_media_type = DK_UNKNOWN; 1901 mutex_exit(&spa_namespace_lock); 1902 if (ddi_copyout(&dkmext, (void *)arg, sizeof (dkmext), flag)) 1903 error = SET_ERROR(EFAULT); 1904 return (error); 1905 } 1906 1907 case DKIOCGETEFI: 1908 { 1909 uint64_t vs = zv->zv_volsize; 1910 uint8_t bs = zv->zv_min_bs; 1911 1912 mutex_exit(&spa_namespace_lock); 1913 error = zvol_getefi((void *)arg, flag, vs, bs); 1914 return (error); 1915 } 1916 1917 case DKIOCFLUSHWRITECACHE: 1918 dkc = (struct dk_callback *)arg; 1919 mutex_exit(&spa_namespace_lock); 1920 zil_commit(zv->zv_zilog, ZVOL_OBJ); 1921 if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) { 1922 (*dkc->dkc_callback)(dkc->dkc_cookie, error); 1923 error = 0; 1924 } 1925 return (error); 1926 1927 case DKIOCGETWCE: 1928 { 1929 int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0; 1930 if (ddi_copyout(&wce, (void *)arg, sizeof (int), 1931 flag)) 1932 error = SET_ERROR(EFAULT); 1933 break; 1934 } 1935 case DKIOCSETWCE: 1936 { 1937 int wce; 1938 if (ddi_copyin((void *)arg, &wce, sizeof (int), 1939 flag)) { 1940 error = SET_ERROR(EFAULT); 1941 break; 1942 } 1943 if (wce) { 1944 zv->zv_flags |= ZVOL_WCE; 1945 mutex_exit(&spa_namespace_lock); 1946 } else { 1947 zv->zv_flags &= ~ZVOL_WCE; 1948 mutex_exit(&spa_namespace_lock); 1949 zil_commit(zv->zv_zilog, ZVOL_OBJ); 1950 } 1951 return (0); 1952 } 1953 1954 case DKIOCGGEOM: 1955 case DKIOCGVTOC: 1956 /* 1957 * commands using these (like prtvtoc) expect ENOTSUP 1958 * since we're emulating an EFI label 1959 */ 1960 error = SET_ERROR(ENOTSUP); 1961 break; 1962 1963 case DKIOCDUMPINIT: 1964 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 1965 RL_WRITER); 1966 error = zvol_dumpify(zv); 1967 zfs_range_unlock(rl); 1968 break; 1969 1970 case DKIOCDUMPFINI: 1971 if (!(zv->zv_flags & ZVOL_DUMPIFIED)) 1972 break; 1973 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 1974 RL_WRITER); 1975 error = zvol_dump_fini(zv); 1976 zfs_range_unlock(rl); 1977 break; 1978 1979 case DKIOCFREE: 1980 { 1981 dkioc_free_t df; 1982 dmu_tx_t *tx; 1983 1984 if (!zvol_unmap_enabled) 1985 break; 1986 1987 if (ddi_copyin((void *)arg, &df, sizeof (df), flag)) { 1988 error = SET_ERROR(EFAULT); 1989 break; 1990 } 1991 1992 /* 1993 * Apply Postel's Law to length-checking. If they overshoot, 1994 * just blank out until the end, if there's a need to blank 1995 * out anything. 1996 */ 1997 if (df.df_start >= zv->zv_volsize) 1998 break; /* No need to do anything... */ 1999 if (df.df_start + df.df_length > zv->zv_volsize) 2000 df.df_length = DMU_OBJECT_END; 2001 2002 rl = zfs_range_lock(&zv->zv_znode, df.df_start, df.df_length, 2003 RL_WRITER); 2004 tx = dmu_tx_create(zv->zv_objset); 2005 dmu_tx_mark_netfree(tx); 2006 error = dmu_tx_assign(tx, TXG_WAIT); 2007 if (error != 0) { 2008 dmu_tx_abort(tx); 2009 } else { 2010 zvol_log_truncate(zv, tx, df.df_start, 2011 df.df_length, B_TRUE); 2012 dmu_tx_commit(tx); 2013 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 2014 df.df_start, df.df_length); 2015 } 2016 2017 zfs_range_unlock(rl); 2018 2019 if (error == 0) { 2020 /* 2021 * If the write-cache is disabled or 'sync' property 2022 * is set to 'always' then treat this as a synchronous 2023 * operation (i.e. commit to zil). 2024 */ 2025 if (!(zv->zv_flags & ZVOL_WCE) || 2026 (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)) 2027 zil_commit(zv->zv_zilog, ZVOL_OBJ); 2028 2029 /* 2030 * If the caller really wants synchronous writes, and 2031 * can't wait for them, don't return until the write 2032 * is done. 2033 */ 2034 if (df.df_flags & DF_WAIT_SYNC) { 2035 txg_wait_synced( 2036 dmu_objset_pool(zv->zv_objset), 0); 2037 } 2038 } 2039 break; 2040 } 2041 2042 default: 2043 error = SET_ERROR(ENOTTY); 2044 break; 2045 2046 } 2047 mutex_exit(&spa_namespace_lock); 2048 return (error); 2049} 2050#endif /* sun */ 2051 2052int 2053zvol_busy(void) 2054{ 2055 return (zvol_minors != 0); 2056} 2057 2058void 2059zvol_init(void) 2060{ 2061 VERIFY(ddi_soft_state_init(&zfsdev_state, sizeof (zfs_soft_state_t), 2062 1) == 0); 2063 ZFS_LOG(1, "ZVOL Initialized."); 2064} 2065 2066void 2067zvol_fini(void) 2068{ 2069 ddi_soft_state_fini(&zfsdev_state); 2070 ZFS_LOG(1, "ZVOL Deinitialized."); 2071} 2072 2073#ifdef sun 2074/*ARGSUSED*/ 2075static int 2076zfs_mvdev_dump_feature_check(void *arg, dmu_tx_t *tx) 2077{ 2078 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 2079 2080 if (spa_feature_is_active(spa, SPA_FEATURE_MULTI_VDEV_CRASH_DUMP)) 2081 return (1); 2082 return (0); 2083} 2084 2085/*ARGSUSED*/ 2086static void 2087zfs_mvdev_dump_activate_feature_sync(void *arg, dmu_tx_t *tx) 2088{ 2089 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 2090 2091 spa_feature_incr(spa, SPA_FEATURE_MULTI_VDEV_CRASH_DUMP, tx); 2092} 2093 2094static int 2095zvol_dump_init(zvol_state_t *zv, boolean_t resize) 2096{ 2097 dmu_tx_t *tx; 2098 int error; 2099 objset_t *os = zv->zv_objset; 2100 spa_t *spa = dmu_objset_spa(os); 2101 vdev_t *vd = spa->spa_root_vdev; 2102 nvlist_t *nv = NULL; 2103 uint64_t version = spa_version(spa); 2104 enum zio_checksum checksum; 2105 2106 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 2107 ASSERT(vd->vdev_ops == &vdev_root_ops); 2108 2109 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0, 2110 DMU_OBJECT_END); 2111 /* wait for dmu_free_long_range to actually free the blocks */ 2112 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 2113 2114 /* 2115 * If the pool on which the dump device is being initialized has more 2116 * than one child vdev, check that the MULTI_VDEV_CRASH_DUMP feature is 2117 * enabled. If so, bump that feature's counter to indicate that the 2118 * feature is active. We also check the vdev type to handle the 2119 * following case: 2120 * # zpool create test raidz disk1 disk2 disk3 2121 * Now have spa_root_vdev->vdev_children == 1 (the raidz vdev), 2122 * the raidz vdev itself has 3 children. 2123 */ 2124 if (vd->vdev_children > 1 || vd->vdev_ops == &vdev_raidz_ops) { 2125 if (!spa_feature_is_enabled(spa, 2126 SPA_FEATURE_MULTI_VDEV_CRASH_DUMP)) 2127 return (SET_ERROR(ENOTSUP)); 2128 (void) dsl_sync_task(spa_name(spa), 2129 zfs_mvdev_dump_feature_check, 2130 zfs_mvdev_dump_activate_feature_sync, NULL, 2131 2, ZFS_SPACE_CHECK_RESERVED); 2132 } 2133 2134 tx = dmu_tx_create(os); 2135 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 2136 dmu_tx_hold_bonus(tx, ZVOL_OBJ); 2137 error = dmu_tx_assign(tx, TXG_WAIT); 2138 if (error) { 2139 dmu_tx_abort(tx); 2140 return (error); 2141 } 2142 2143 /* 2144 * If MULTI_VDEV_CRASH_DUMP is active, use the NOPARITY checksum 2145 * function. Otherwise, use the old default -- OFF. 2146 */ 2147 checksum = spa_feature_is_active(spa, 2148 SPA_FEATURE_MULTI_VDEV_CRASH_DUMP) ? ZIO_CHECKSUM_NOPARITY : 2149 ZIO_CHECKSUM_OFF; 2150 2151 /* 2152 * If we are resizing the dump device then we only need to 2153 * update the refreservation to match the newly updated 2154 * zvolsize. Otherwise, we save off the original state of the 2155 * zvol so that we can restore them if the zvol is ever undumpified. 2156 */ 2157 if (resize) { 2158 error = zap_update(os, ZVOL_ZAP_OBJ, 2159 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 2160 &zv->zv_volsize, tx); 2161 } else { 2162 uint64_t checksum, compress, refresrv, vbs, dedup; 2163 2164 error = dsl_prop_get_integer(zv->zv_name, 2165 zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL); 2166 error = error ? error : dsl_prop_get_integer(zv->zv_name, 2167 zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL); 2168 error = error ? error : dsl_prop_get_integer(zv->zv_name, 2169 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL); 2170 error = error ? error : dsl_prop_get_integer(zv->zv_name, 2171 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL); 2172 if (version >= SPA_VERSION_DEDUP) { 2173 error = error ? error : 2174 dsl_prop_get_integer(zv->zv_name, 2175 zfs_prop_to_name(ZFS_PROP_DEDUP), &dedup, NULL); 2176 } 2177 2178 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 2179 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, 2180 &compress, tx); 2181 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 2182 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx); 2183 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 2184 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 2185 &refresrv, tx); 2186 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 2187 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, 2188 &vbs, tx); 2189 error = error ? error : dmu_object_set_blocksize( 2190 os, ZVOL_OBJ, SPA_MAXBLOCKSIZE, 0, tx); 2191 if (version >= SPA_VERSION_DEDUP) { 2192 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 2193 zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, 2194 &dedup, tx); 2195 } 2196 if (error == 0) 2197 zv->zv_volblocksize = SPA_MAXBLOCKSIZE; 2198 } 2199 dmu_tx_commit(tx); 2200 2201 /* 2202 * We only need update the zvol's property if we are initializing 2203 * the dump area for the first time. 2204 */ 2205 if (!resize) { 2206 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2207 VERIFY(nvlist_add_uint64(nv, 2208 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0); 2209 VERIFY(nvlist_add_uint64(nv, 2210 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 2211 ZIO_COMPRESS_OFF) == 0); 2212 VERIFY(nvlist_add_uint64(nv, 2213 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 2214 checksum) == 0); 2215 if (version >= SPA_VERSION_DEDUP) { 2216 VERIFY(nvlist_add_uint64(nv, 2217 zfs_prop_to_name(ZFS_PROP_DEDUP), 2218 ZIO_CHECKSUM_OFF) == 0); 2219 } 2220 2221 error = zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL, 2222 nv, NULL); 2223 nvlist_free(nv); 2224 2225 if (error) 2226 return (error); 2227 } 2228 2229 /* Allocate the space for the dump */ 2230 error = zvol_prealloc(zv); 2231 return (error); 2232} 2233 2234static int 2235zvol_dumpify(zvol_state_t *zv) 2236{ 2237 int error = 0; 2238 uint64_t dumpsize = 0; 2239 dmu_tx_t *tx; 2240 objset_t *os = zv->zv_objset; 2241 2242 if (zv->zv_flags & ZVOL_RDONLY) 2243 return (SET_ERROR(EROFS)); 2244 2245 if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 2246 8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) { 2247 boolean_t resize = (dumpsize > 0); 2248 2249 if ((error = zvol_dump_init(zv, resize)) != 0) { 2250 (void) zvol_dump_fini(zv); 2251 return (error); 2252 } 2253 } 2254 2255 /* 2256 * Build up our lba mapping. 2257 */ 2258 error = zvol_get_lbas(zv); 2259 if (error) { 2260 (void) zvol_dump_fini(zv); 2261 return (error); 2262 } 2263 2264 tx = dmu_tx_create(os); 2265 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 2266 error = dmu_tx_assign(tx, TXG_WAIT); 2267 if (error) { 2268 dmu_tx_abort(tx); 2269 (void) zvol_dump_fini(zv); 2270 return (error); 2271 } 2272 2273 zv->zv_flags |= ZVOL_DUMPIFIED; 2274 error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1, 2275 &zv->zv_volsize, tx); 2276 dmu_tx_commit(tx); 2277 2278 if (error) { 2279 (void) zvol_dump_fini(zv); 2280 return (error); 2281 } 2282 2283 txg_wait_synced(dmu_objset_pool(os), 0); 2284 return (0); 2285} 2286 2287static int 2288zvol_dump_fini(zvol_state_t *zv) 2289{ 2290 dmu_tx_t *tx; 2291 objset_t *os = zv->zv_objset; 2292 nvlist_t *nv; 2293 int error = 0; 2294 uint64_t checksum, compress, refresrv, vbs, dedup; 2295 uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset)); 2296 2297 /* 2298 * Attempt to restore the zvol back to its pre-dumpified state. 2299 * This is a best-effort attempt as it's possible that not all 2300 * of these properties were initialized during the dumpify process 2301 * (i.e. error during zvol_dump_init). 2302 */ 2303 2304 tx = dmu_tx_create(os); 2305 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 2306 error = dmu_tx_assign(tx, TXG_WAIT); 2307 if (error) { 2308 dmu_tx_abort(tx); 2309 return (error); 2310 } 2311 (void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx); 2312 dmu_tx_commit(tx); 2313 2314 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2315 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum); 2316 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2317 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress); 2318 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2319 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv); 2320 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2321 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs); 2322 2323 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2324 (void) nvlist_add_uint64(nv, 2325 zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum); 2326 (void) nvlist_add_uint64(nv, 2327 zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress); 2328 (void) nvlist_add_uint64(nv, 2329 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv); 2330 if (version >= SPA_VERSION_DEDUP && 2331 zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2332 zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, &dedup) == 0) { 2333 (void) nvlist_add_uint64(nv, 2334 zfs_prop_to_name(ZFS_PROP_DEDUP), dedup); 2335 } 2336 (void) zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL, 2337 nv, NULL); 2338 nvlist_free(nv); 2339 2340 zvol_free_extents(zv); 2341 zv->zv_flags &= ~ZVOL_DUMPIFIED; 2342 (void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END); 2343 /* wait for dmu_free_long_range to actually free the blocks */ 2344 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 2345 tx = dmu_tx_create(os); 2346 dmu_tx_hold_bonus(tx, ZVOL_OBJ); 2347 error = dmu_tx_assign(tx, TXG_WAIT); 2348 if (error) { 2349 dmu_tx_abort(tx); 2350 return (error); 2351 } 2352 if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0) 2353 zv->zv_volblocksize = vbs; 2354 dmu_tx_commit(tx); 2355 2356 return (0); 2357} 2358#endif /* sun */ 2359 2360static void 2361zvol_geom_run(zvol_state_t *zv) 2362{ 2363 struct g_provider *pp; 2364 2365 pp = zv->zv_provider; 2366 g_error_provider(pp, 0); 2367 2368 kproc_kthread_add(zvol_geom_worker, zv, &zfsproc, NULL, 0, 0, 2369 "zfskern", "zvol %s", pp->name + sizeof(ZVOL_DRIVER)); 2370} 2371 2372static void 2373zvol_geom_destroy(zvol_state_t *zv) 2374{ 2375 struct g_provider *pp; 2376 2377 g_topology_assert(); 2378 2379 mtx_lock(&zv->zv_queue_mtx); 2380 zv->zv_state = 1; 2381 wakeup_one(&zv->zv_queue); 2382 while (zv->zv_state != 2) 2383 msleep(&zv->zv_state, &zv->zv_queue_mtx, 0, "zvol:w", 0); 2384 mtx_destroy(&zv->zv_queue_mtx); 2385 2386 pp = zv->zv_provider; 2387 zv->zv_provider = NULL; 2388 pp->private = NULL; 2389 g_wither_geom(pp->geom, ENXIO); 2390} 2391 2392static int 2393zvol_geom_access(struct g_provider *pp, int acr, int acw, int ace) 2394{ 2395 int count, error, flags; 2396 2397 g_topology_assert(); 2398 2399 /* 2400 * To make it easier we expect either open or close, but not both 2401 * at the same time. 2402 */ 2403 KASSERT((acr >= 0 && acw >= 0 && ace >= 0) || 2404 (acr <= 0 && acw <= 0 && ace <= 0), 2405 ("Unsupported access request to %s (acr=%d, acw=%d, ace=%d).", 2406 pp->name, acr, acw, ace)); 2407 2408 if (pp->private == NULL) { 2409 if (acr <= 0 && acw <= 0 && ace <= 0) 2410 return (0); 2411 return (pp->error); 2412 } 2413 2414 /* 2415 * We don't pass FEXCL flag to zvol_open()/zvol_close() if ace != 0, 2416 * because GEOM already handles that and handles it a bit differently. 2417 * GEOM allows for multiple read/exclusive consumers and ZFS allows 2418 * only one exclusive consumer, no matter if it is reader or writer. 2419 * I like better the way GEOM works so I'll leave it for GEOM to 2420 * decide what to do. 2421 */ 2422 2423 count = acr + acw + ace; 2424 if (count == 0) 2425 return (0); 2426 2427 flags = 0; 2428 if (acr != 0 || ace != 0) 2429 flags |= FREAD; 2430 if (acw != 0) 2431 flags |= FWRITE; 2432 2433 g_topology_unlock(); 2434 if (count > 0) 2435 error = zvol_open(pp, flags, count); 2436 else 2437 error = zvol_close(pp, flags, -count); 2438 g_topology_lock(); 2439 return (error); 2440} 2441 2442static void 2443zvol_geom_start(struct bio *bp) 2444{ 2445 zvol_state_t *zv; 2446 boolean_t first; 2447 2448 zv = bp->bio_to->private; 2449 ASSERT(zv != NULL); 2450 switch (bp->bio_cmd) { 2451 case BIO_FLUSH: 2452 if (!THREAD_CAN_SLEEP()) 2453 goto enqueue; 2454 zil_commit(zv->zv_zilog, ZVOL_OBJ); 2455 g_io_deliver(bp, 0); 2456 break; 2457 case BIO_READ: 2458 case BIO_WRITE: 2459 case BIO_DELETE: 2460 if (!THREAD_CAN_SLEEP()) 2461 goto enqueue; 2462 zvol_strategy(bp); 2463 break; 2464 case BIO_GETATTR: { 2465 spa_t *spa = dmu_objset_spa(zv->zv_objset); 2466 uint64_t refd, avail, usedobjs, availobjs, val; 2467 2468 if (g_handleattr_int(bp, "GEOM::candelete", 1)) 2469 return; 2470 if (strcmp(bp->bio_attribute, "blocksavail") == 0) { 2471 dmu_objset_space(zv->zv_objset, &refd, &avail, 2472 &usedobjs, &availobjs); 2473 if (g_handleattr_off_t(bp, "blocksavail", 2474 avail / DEV_BSIZE)) 2475 return; 2476 } else if (strcmp(bp->bio_attribute, "blocksused") == 0) { 2477 dmu_objset_space(zv->zv_objset, &refd, &avail, 2478 &usedobjs, &availobjs); 2479 if (g_handleattr_off_t(bp, "blocksused", 2480 refd / DEV_BSIZE)) 2481 return; 2482 } else if (strcmp(bp->bio_attribute, "poolblocksavail") == 0) { 2483 avail = metaslab_class_get_space(spa_normal_class(spa)); 2484 avail -= metaslab_class_get_alloc(spa_normal_class(spa)); 2485 if (g_handleattr_off_t(bp, "poolblocksavail", 2486 avail / DEV_BSIZE)) 2487 return; 2488 } else if (strcmp(bp->bio_attribute, "poolblocksused") == 0) { 2489 refd = metaslab_class_get_alloc(spa_normal_class(spa)); 2490 if (g_handleattr_off_t(bp, "poolblocksused", 2491 refd / DEV_BSIZE)) 2492 return; 2493 } 2494 /* FALLTHROUGH */ 2495 } 2496 default: 2497 g_io_deliver(bp, EOPNOTSUPP); 2498 break; 2499 } 2500 return; 2501 2502enqueue: 2503 mtx_lock(&zv->zv_queue_mtx); 2504 first = (bioq_first(&zv->zv_queue) == NULL); 2505 bioq_insert_tail(&zv->zv_queue, bp); 2506 mtx_unlock(&zv->zv_queue_mtx); 2507 if (first) 2508 wakeup_one(&zv->zv_queue); 2509} 2510 2511static void 2512zvol_geom_worker(void *arg) 2513{ 2514 zvol_state_t *zv; 2515 struct bio *bp; 2516 2517 thread_lock(curthread); 2518 sched_prio(curthread, PRIBIO); 2519 thread_unlock(curthread); 2520 2521 zv = arg; 2522 for (;;) { 2523 mtx_lock(&zv->zv_queue_mtx); 2524 bp = bioq_takefirst(&zv->zv_queue); 2525 if (bp == NULL) { 2526 if (zv->zv_state == 1) { 2527 zv->zv_state = 2; 2528 wakeup(&zv->zv_state); 2529 mtx_unlock(&zv->zv_queue_mtx); 2530 kthread_exit(); 2531 } 2532 msleep(&zv->zv_queue, &zv->zv_queue_mtx, PRIBIO | PDROP, 2533 "zvol:io", 0); 2534 continue; 2535 } 2536 mtx_unlock(&zv->zv_queue_mtx); 2537 switch (bp->bio_cmd) { 2538 case BIO_FLUSH: 2539 zil_commit(zv->zv_zilog, ZVOL_OBJ); 2540 g_io_deliver(bp, 0); 2541 break; 2542 case BIO_READ: 2543 case BIO_WRITE: 2544 zvol_strategy(bp); 2545 break; 2546 } 2547 } 2548} 2549 2550extern boolean_t dataset_name_hidden(const char *name); 2551 2552static int 2553zvol_create_snapshots(objset_t *os, const char *name) 2554{ 2555 uint64_t cookie, obj; 2556 char *sname; 2557 int error, len; 2558 2559 cookie = obj = 0; 2560 sname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 2561 2562#if 0 2563 (void) dmu_objset_find(name, dmu_objset_prefetch, NULL, 2564 DS_FIND_SNAPSHOTS); 2565#endif 2566 2567 for (;;) { 2568 len = snprintf(sname, MAXPATHLEN, "%s@", name); 2569 if (len >= MAXPATHLEN) { 2570 dmu_objset_rele(os, FTAG); 2571 error = ENAMETOOLONG; 2572 break; 2573 } 2574 2575 dsl_pool_config_enter(dmu_objset_pool(os), FTAG); 2576 error = dmu_snapshot_list_next(os, MAXPATHLEN - len, 2577 sname + len, &obj, &cookie, NULL); 2578 dsl_pool_config_exit(dmu_objset_pool(os), FTAG); 2579 if (error != 0) { 2580 if (error == ENOENT) 2581 error = 0; 2582 break; 2583 } 2584 2585 if ((error = zvol_create_minor(sname)) != 0) { 2586 printf("ZFS WARNING: Unable to create ZVOL %s (error=%d).\n", 2587 sname, error); 2588 break; 2589 } 2590 } 2591 2592 kmem_free(sname, MAXPATHLEN); 2593 return (error); 2594} 2595 2596int 2597zvol_create_minors(const char *name) 2598{ 2599 uint64_t cookie; 2600 objset_t *os; 2601 char *osname, *p; 2602 int error, len; 2603 2604 if (dataset_name_hidden(name)) 2605 return (0); 2606 2607 if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) { 2608 printf("ZFS WARNING: Unable to put hold on %s (error=%d).\n", 2609 name, error); 2610 return (error); 2611 } 2612 if (dmu_objset_type(os) == DMU_OST_ZVOL) { 2613 dsl_dataset_long_hold(os->os_dsl_dataset, FTAG); 2614 dsl_pool_rele(dmu_objset_pool(os), FTAG); 2615 error = zvol_create_minor(name); 2616 if (error == 0 || error == EEXIST) { 2617 error = zvol_create_snapshots(os, name); 2618 } else { 2619 printf("ZFS WARNING: Unable to create ZVOL %s (error=%d).\n", 2620 name, error); 2621 } 2622 dsl_dataset_long_rele(os->os_dsl_dataset, FTAG); 2623 dsl_dataset_rele(os->os_dsl_dataset, FTAG); 2624 return (error); 2625 } 2626 if (dmu_objset_type(os) != DMU_OST_ZFS) { 2627 dmu_objset_rele(os, FTAG); 2628 return (0); 2629 } 2630 2631 osname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 2632 if (snprintf(osname, MAXPATHLEN, "%s/", name) >= MAXPATHLEN) { 2633 dmu_objset_rele(os, FTAG); 2634 kmem_free(osname, MAXPATHLEN); 2635 return (ENOENT); 2636 } 2637 p = osname + strlen(osname); 2638 len = MAXPATHLEN - (p - osname); 2639 2640#if 0 2641 /* Prefetch the datasets. */ 2642 cookie = 0; 2643 while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0) { 2644 if (!dataset_name_hidden(osname)) 2645 (void) dmu_objset_prefetch(osname, NULL); 2646 } 2647#endif 2648 2649 cookie = 0; 2650 while (dmu_dir_list_next(os, MAXPATHLEN - (p - osname), p, NULL, 2651 &cookie) == 0) { 2652 dmu_objset_rele(os, FTAG); 2653 (void)zvol_create_minors(osname); 2654 if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) { 2655 printf("ZFS WARNING: Unable to put hold on %s (error=%d).\n", 2656 name, error); 2657 return (error); 2658 } 2659 } 2660 2661 dmu_objset_rele(os, FTAG); 2662 kmem_free(osname, MAXPATHLEN); 2663 return (0); 2664} 2665 2666static void 2667zvol_rename_minor(zvol_state_t *zv, const char *newname) 2668{ 2669 struct g_geom *gp; 2670 struct g_provider *pp; 2671 struct cdev *dev; 2672 2673 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 2674 2675 if (zv->zv_volmode == ZFS_VOLMODE_GEOM) { 2676 g_topology_lock(); 2677 pp = zv->zv_provider; 2678 ASSERT(pp != NULL); 2679 gp = pp->geom; 2680 ASSERT(gp != NULL); 2681 2682 zv->zv_provider = NULL; 2683 g_wither_provider(pp, ENXIO); 2684 2685 pp = g_new_providerf(gp, "%s/%s", ZVOL_DRIVER, newname); 2686 pp->flags |= G_PF_DIRECT_RECEIVE | G_PF_DIRECT_SEND; 2687 pp->sectorsize = DEV_BSIZE; 2688 pp->mediasize = zv->zv_volsize; 2689 pp->private = zv; 2690 zv->zv_provider = pp; 2691 g_error_provider(pp, 0); 2692 g_topology_unlock(); 2693 } else if (zv->zv_volmode == ZFS_VOLMODE_DEV) { 2694 dev = zv->zv_dev; 2695 ASSERT(dev != NULL); 2696 zv->zv_dev = NULL; 2697 destroy_dev(dev); 2698 2699 if (make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, 2700 &dev, &zvol_cdevsw, NULL, UID_ROOT, GID_OPERATOR, 2701 0640, "%s/%s", ZVOL_DRIVER, newname) == 0) { 2702 zv->zv_dev = dev; 2703 dev->si_iosize_max = MAXPHYS; 2704 dev->si_drv2 = zv; 2705 } 2706 } 2707 strlcpy(zv->zv_name, newname, sizeof(zv->zv_name)); 2708} 2709 2710void 2711zvol_rename_minors(const char *oldname, const char *newname) 2712{ 2713 char name[MAXPATHLEN]; 2714 struct g_provider *pp; 2715 struct g_geom *gp; 2716 size_t oldnamelen, newnamelen; 2717 zvol_state_t *zv; 2718 char *namebuf; 2719 boolean_t locked = B_FALSE; 2720 2721 oldnamelen = strlen(oldname); 2722 newnamelen = strlen(newname); 2723 2724 DROP_GIANT(); 2725 /* See comment in zvol_open(). */ 2726 if (!MUTEX_HELD(&spa_namespace_lock)) { 2727 mutex_enter(&spa_namespace_lock); 2728 locked = B_TRUE; 2729 } 2730 2731 LIST_FOREACH(zv, &all_zvols, zv_links) { 2732 if (strcmp(zv->zv_name, oldname) == 0) { 2733 zvol_rename_minor(zv, newname); 2734 } else if (strncmp(zv->zv_name, oldname, oldnamelen) == 0 && 2735 (zv->zv_name[oldnamelen] == '/' || 2736 zv->zv_name[oldnamelen] == '@')) { 2737 snprintf(name, sizeof(name), "%s%c%s", newname, 2738 zv->zv_name[oldnamelen], 2739 zv->zv_name + oldnamelen + 1); 2740 zvol_rename_minor(zv, name); 2741 } 2742 } 2743 2744 if (locked) 2745 mutex_exit(&spa_namespace_lock); 2746 PICKUP_GIANT(); 2747} 2748 2749static int 2750zvol_d_open(struct cdev *dev, int flags, int fmt, struct thread *td) 2751{ 2752 zvol_state_t *zv; 2753 int err = 0; 2754 2755 mutex_enter(&spa_namespace_lock); 2756 zv = dev->si_drv2; 2757 if (zv == NULL) { 2758 mutex_exit(&spa_namespace_lock); 2759 return(ENXIO); /* zvol_create_minor() not done yet */ 2760 } 2761 2762 if (zv->zv_total_opens == 0) 2763 err = zvol_first_open(zv); 2764 if (err) { 2765 mutex_exit(&spa_namespace_lock); 2766 return (err); 2767 } 2768 if ((flags & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) { 2769 err = SET_ERROR(EROFS); 2770 goto out; 2771 } 2772 if (zv->zv_flags & ZVOL_EXCL) { 2773 err = SET_ERROR(EBUSY); 2774 goto out; 2775 } 2776#ifdef FEXCL 2777 if (flags & FEXCL) { 2778 if (zv->zv_total_opens != 0) { 2779 err = SET_ERROR(EBUSY); 2780 goto out; 2781 } 2782 zv->zv_flags |= ZVOL_EXCL; 2783 } 2784#endif 2785 2786 zv->zv_total_opens++; 2787 mutex_exit(&spa_namespace_lock); 2788 return (err); 2789out: 2790 if (zv->zv_total_opens == 0) 2791 zvol_last_close(zv); 2792 mutex_exit(&spa_namespace_lock); 2793 return (err); 2794} 2795 2796static int 2797zvol_d_close(struct cdev *dev, int flags, int fmt, struct thread *td) 2798{ 2799 zvol_state_t *zv; 2800 int err = 0; 2801 2802 mutex_enter(&spa_namespace_lock); 2803 zv = dev->si_drv2; 2804 if (zv == NULL) { 2805 mutex_exit(&spa_namespace_lock); 2806 return(ENXIO); 2807 } 2808 2809 if (zv->zv_flags & ZVOL_EXCL) { 2810 ASSERT(zv->zv_total_opens == 1); 2811 zv->zv_flags &= ~ZVOL_EXCL; 2812 } 2813 2814 /* 2815 * If the open count is zero, this is a spurious close. 2816 * That indicates a bug in the kernel / DDI framework. 2817 */ 2818 ASSERT(zv->zv_total_opens != 0); 2819 2820 /* 2821 * You may get multiple opens, but only one close. 2822 */ 2823 zv->zv_total_opens--; 2824 2825 if (zv->zv_total_opens == 0) 2826 zvol_last_close(zv); 2827 2828 mutex_exit(&spa_namespace_lock); 2829 return (0); 2830} 2831 2832static int 2833zvol_d_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 2834{ 2835 zvol_state_t *zv; 2836 rl_t *rl; 2837 off_t offset, length, chunk; 2838 int i, error; 2839 u_int u; 2840 2841 zv = dev->si_drv2; 2842 2843 error = 0; 2844 KASSERT(zv->zv_total_opens > 0, 2845 ("Device with zero access count in zvol_d_ioctl")); 2846 2847 i = IOCPARM_LEN(cmd); 2848 switch (cmd) { 2849 case DIOCGSECTORSIZE: 2850 *(u_int *)data = DEV_BSIZE; 2851 break; 2852 case DIOCGMEDIASIZE: 2853 *(off_t *)data = zv->zv_volsize; 2854 break; 2855 case DIOCGFLUSH: 2856 zil_commit(zv->zv_zilog, ZVOL_OBJ); 2857 break; 2858 case DIOCGDELETE: 2859 if (!zvol_unmap_enabled) 2860 break; 2861 2862 offset = ((off_t *)data)[0]; 2863 length = ((off_t *)data)[1]; 2864 if ((offset % DEV_BSIZE) != 0 || (length % DEV_BSIZE) != 0 || 2865 offset < 0 || offset >= zv->zv_volsize || 2866 length <= 0) { 2867 printf("%s: offset=%jd length=%jd\n", __func__, offset, 2868 length); 2869 error = EINVAL; 2870 break; 2871 } 2872 2873 rl = zfs_range_lock(&zv->zv_znode, offset, length, RL_WRITER); 2874 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset); 2875 error = dmu_tx_assign(tx, TXG_WAIT); 2876 if (error != 0) { 2877 dmu_tx_abort(tx); 2878 } else { 2879 zvol_log_truncate(zv, tx, offset, length, B_TRUE); 2880 dmu_tx_commit(tx); 2881 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 2882 offset, length); 2883 } 2884 zfs_range_unlock(rl); 2885 if (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS) 2886 zil_commit(zv->zv_zilog, ZVOL_OBJ); 2887 break; 2888 case DIOCGSTRIPESIZE: 2889 *(off_t *)data = zv->zv_volblocksize; 2890 break; 2891 case DIOCGSTRIPEOFFSET: 2892 *(off_t *)data = 0; 2893 break; 2894 case DIOCGATTR: { 2895 spa_t *spa = dmu_objset_spa(zv->zv_objset); 2896 struct diocgattr_arg *arg = (struct diocgattr_arg *)data; 2897 uint64_t refd, avail, usedobjs, availobjs; 2898 2899 if (strcmp(arg->name, "blocksavail") == 0) { 2900 dmu_objset_space(zv->zv_objset, &refd, &avail, 2901 &usedobjs, &availobjs); 2902 arg->value.off = avail / DEV_BSIZE; 2903 } else if (strcmp(arg->name, "blocksused") == 0) { 2904 dmu_objset_space(zv->zv_objset, &refd, &avail, 2905 &usedobjs, &availobjs); 2906 arg->value.off = refd / DEV_BSIZE; 2907 } else if (strcmp(arg->name, "poolblocksavail") == 0) { 2908 avail = metaslab_class_get_space(spa_normal_class(spa)); 2909 avail -= metaslab_class_get_alloc(spa_normal_class(spa)); 2910 arg->value.off = avail / DEV_BSIZE; 2911 } else if (strcmp(arg->name, "poolblocksused") == 0) { 2912 refd = metaslab_class_get_alloc(spa_normal_class(spa)); 2913 arg->value.off = refd / DEV_BSIZE; 2914 } else 2915 error = ENOIOCTL; 2916 break; 2917 } 2918 case FIOSEEKHOLE: 2919 case FIOSEEKDATA: { 2920 off_t *off = (off_t *)data; 2921 uint64_t noff; 2922 boolean_t hole; 2923 2924 hole = (cmd == FIOSEEKHOLE); 2925 noff = *off; 2926 error = dmu_offset_next(zv->zv_objset, ZVOL_OBJ, hole, &noff); 2927 *off = noff; 2928 break; 2929 } 2930 default: 2931 error = ENOIOCTL; 2932 } 2933 2934 return (error); 2935} 2936