zvol.c revision 276081
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_OLD_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_OLD_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 = 1872 1 << (SPA_OLD_MAXBLOCKSHIFT - zv->zv_min_bs); 1873 mutex_exit(&spa_namespace_lock); 1874 if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag)) 1875 error = SET_ERROR(EFAULT); 1876 return (error); 1877 } 1878 1879 case DKIOCGMEDIAINFO: 1880 { 1881 struct dk_minfo dkm; 1882 1883 bzero(&dkm, sizeof (dkm)); 1884 dkm.dki_lbsize = 1U << zv->zv_min_bs; 1885 dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs; 1886 dkm.dki_media_type = DK_UNKNOWN; 1887 mutex_exit(&spa_namespace_lock); 1888 if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag)) 1889 error = SET_ERROR(EFAULT); 1890 return (error); 1891 } 1892 1893 case DKIOCGMEDIAINFOEXT: 1894 { 1895 struct dk_minfo_ext dkmext; 1896 1897 bzero(&dkmext, sizeof (dkmext)); 1898 dkmext.dki_lbsize = 1U << zv->zv_min_bs; 1899 dkmext.dki_pbsize = zv->zv_volblocksize; 1900 dkmext.dki_capacity = zv->zv_volsize >> zv->zv_min_bs; 1901 dkmext.dki_media_type = DK_UNKNOWN; 1902 mutex_exit(&spa_namespace_lock); 1903 if (ddi_copyout(&dkmext, (void *)arg, sizeof (dkmext), flag)) 1904 error = SET_ERROR(EFAULT); 1905 return (error); 1906 } 1907 1908 case DKIOCGETEFI: 1909 { 1910 uint64_t vs = zv->zv_volsize; 1911 uint8_t bs = zv->zv_min_bs; 1912 1913 mutex_exit(&spa_namespace_lock); 1914 error = zvol_getefi((void *)arg, flag, vs, bs); 1915 return (error); 1916 } 1917 1918 case DKIOCFLUSHWRITECACHE: 1919 dkc = (struct dk_callback *)arg; 1920 mutex_exit(&spa_namespace_lock); 1921 zil_commit(zv->zv_zilog, ZVOL_OBJ); 1922 if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) { 1923 (*dkc->dkc_callback)(dkc->dkc_cookie, error); 1924 error = 0; 1925 } 1926 return (error); 1927 1928 case DKIOCGETWCE: 1929 { 1930 int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0; 1931 if (ddi_copyout(&wce, (void *)arg, sizeof (int), 1932 flag)) 1933 error = SET_ERROR(EFAULT); 1934 break; 1935 } 1936 case DKIOCSETWCE: 1937 { 1938 int wce; 1939 if (ddi_copyin((void *)arg, &wce, sizeof (int), 1940 flag)) { 1941 error = SET_ERROR(EFAULT); 1942 break; 1943 } 1944 if (wce) { 1945 zv->zv_flags |= ZVOL_WCE; 1946 mutex_exit(&spa_namespace_lock); 1947 } else { 1948 zv->zv_flags &= ~ZVOL_WCE; 1949 mutex_exit(&spa_namespace_lock); 1950 zil_commit(zv->zv_zilog, ZVOL_OBJ); 1951 } 1952 return (0); 1953 } 1954 1955 case DKIOCGGEOM: 1956 case DKIOCGVTOC: 1957 /* 1958 * commands using these (like prtvtoc) expect ENOTSUP 1959 * since we're emulating an EFI label 1960 */ 1961 error = SET_ERROR(ENOTSUP); 1962 break; 1963 1964 case DKIOCDUMPINIT: 1965 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 1966 RL_WRITER); 1967 error = zvol_dumpify(zv); 1968 zfs_range_unlock(rl); 1969 break; 1970 1971 case DKIOCDUMPFINI: 1972 if (!(zv->zv_flags & ZVOL_DUMPIFIED)) 1973 break; 1974 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 1975 RL_WRITER); 1976 error = zvol_dump_fini(zv); 1977 zfs_range_unlock(rl); 1978 break; 1979 1980 case DKIOCFREE: 1981 { 1982 dkioc_free_t df; 1983 dmu_tx_t *tx; 1984 1985 if (!zvol_unmap_enabled) 1986 break; 1987 1988 if (ddi_copyin((void *)arg, &df, sizeof (df), flag)) { 1989 error = SET_ERROR(EFAULT); 1990 break; 1991 } 1992 1993 /* 1994 * Apply Postel's Law to length-checking. If they overshoot, 1995 * just blank out until the end, if there's a need to blank 1996 * out anything. 1997 */ 1998 if (df.df_start >= zv->zv_volsize) 1999 break; /* No need to do anything... */ 2000 if (df.df_start + df.df_length > zv->zv_volsize) 2001 df.df_length = DMU_OBJECT_END; 2002 2003 rl = zfs_range_lock(&zv->zv_znode, df.df_start, df.df_length, 2004 RL_WRITER); 2005 tx = dmu_tx_create(zv->zv_objset); 2006 dmu_tx_mark_netfree(tx); 2007 error = dmu_tx_assign(tx, TXG_WAIT); 2008 if (error != 0) { 2009 dmu_tx_abort(tx); 2010 } else { 2011 zvol_log_truncate(zv, tx, df.df_start, 2012 df.df_length, B_TRUE); 2013 dmu_tx_commit(tx); 2014 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 2015 df.df_start, df.df_length); 2016 } 2017 2018 zfs_range_unlock(rl); 2019 2020 if (error == 0) { 2021 /* 2022 * If the write-cache is disabled or 'sync' property 2023 * is set to 'always' then treat this as a synchronous 2024 * operation (i.e. commit to zil). 2025 */ 2026 if (!(zv->zv_flags & ZVOL_WCE) || 2027 (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)) 2028 zil_commit(zv->zv_zilog, ZVOL_OBJ); 2029 2030 /* 2031 * If the caller really wants synchronous writes, and 2032 * can't wait for them, don't return until the write 2033 * is done. 2034 */ 2035 if (df.df_flags & DF_WAIT_SYNC) { 2036 txg_wait_synced( 2037 dmu_objset_pool(zv->zv_objset), 0); 2038 } 2039 } 2040 break; 2041 } 2042 2043 default: 2044 error = SET_ERROR(ENOTTY); 2045 break; 2046 2047 } 2048 mutex_exit(&spa_namespace_lock); 2049 return (error); 2050} 2051#endif /* sun */ 2052 2053int 2054zvol_busy(void) 2055{ 2056 return (zvol_minors != 0); 2057} 2058 2059void 2060zvol_init(void) 2061{ 2062 VERIFY(ddi_soft_state_init(&zfsdev_state, sizeof (zfs_soft_state_t), 2063 1) == 0); 2064 ZFS_LOG(1, "ZVOL Initialized."); 2065} 2066 2067void 2068zvol_fini(void) 2069{ 2070 ddi_soft_state_fini(&zfsdev_state); 2071 ZFS_LOG(1, "ZVOL Deinitialized."); 2072} 2073 2074#ifdef sun 2075/*ARGSUSED*/ 2076static int 2077zfs_mvdev_dump_feature_check(void *arg, dmu_tx_t *tx) 2078{ 2079 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 2080 2081 if (spa_feature_is_active(spa, SPA_FEATURE_MULTI_VDEV_CRASH_DUMP)) 2082 return (1); 2083 return (0); 2084} 2085 2086/*ARGSUSED*/ 2087static void 2088zfs_mvdev_dump_activate_feature_sync(void *arg, dmu_tx_t *tx) 2089{ 2090 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 2091 2092 spa_feature_incr(spa, SPA_FEATURE_MULTI_VDEV_CRASH_DUMP, tx); 2093} 2094 2095static int 2096zvol_dump_init(zvol_state_t *zv, boolean_t resize) 2097{ 2098 dmu_tx_t *tx; 2099 int error; 2100 objset_t *os = zv->zv_objset; 2101 spa_t *spa = dmu_objset_spa(os); 2102 vdev_t *vd = spa->spa_root_vdev; 2103 nvlist_t *nv = NULL; 2104 uint64_t version = spa_version(spa); 2105 enum zio_checksum checksum; 2106 2107 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 2108 ASSERT(vd->vdev_ops == &vdev_root_ops); 2109 2110 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0, 2111 DMU_OBJECT_END); 2112 /* wait for dmu_free_long_range to actually free the blocks */ 2113 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 2114 2115 /* 2116 * If the pool on which the dump device is being initialized has more 2117 * than one child vdev, check that the MULTI_VDEV_CRASH_DUMP feature is 2118 * enabled. If so, bump that feature's counter to indicate that the 2119 * feature is active. We also check the vdev type to handle the 2120 * following case: 2121 * # zpool create test raidz disk1 disk2 disk3 2122 * Now have spa_root_vdev->vdev_children == 1 (the raidz vdev), 2123 * the raidz vdev itself has 3 children. 2124 */ 2125 if (vd->vdev_children > 1 || vd->vdev_ops == &vdev_raidz_ops) { 2126 if (!spa_feature_is_enabled(spa, 2127 SPA_FEATURE_MULTI_VDEV_CRASH_DUMP)) 2128 return (SET_ERROR(ENOTSUP)); 2129 (void) dsl_sync_task(spa_name(spa), 2130 zfs_mvdev_dump_feature_check, 2131 zfs_mvdev_dump_activate_feature_sync, NULL, 2132 2, ZFS_SPACE_CHECK_RESERVED); 2133 } 2134 2135 tx = dmu_tx_create(os); 2136 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 2137 dmu_tx_hold_bonus(tx, ZVOL_OBJ); 2138 error = dmu_tx_assign(tx, TXG_WAIT); 2139 if (error) { 2140 dmu_tx_abort(tx); 2141 return (error); 2142 } 2143 2144 /* 2145 * If MULTI_VDEV_CRASH_DUMP is active, use the NOPARITY checksum 2146 * function. Otherwise, use the old default -- OFF. 2147 */ 2148 checksum = spa_feature_is_active(spa, 2149 SPA_FEATURE_MULTI_VDEV_CRASH_DUMP) ? ZIO_CHECKSUM_NOPARITY : 2150 ZIO_CHECKSUM_OFF; 2151 2152 /* 2153 * If we are resizing the dump device then we only need to 2154 * update the refreservation to match the newly updated 2155 * zvolsize. Otherwise, we save off the original state of the 2156 * zvol so that we can restore them if the zvol is ever undumpified. 2157 */ 2158 if (resize) { 2159 error = zap_update(os, ZVOL_ZAP_OBJ, 2160 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 2161 &zv->zv_volsize, tx); 2162 } else { 2163 uint64_t checksum, compress, refresrv, vbs, dedup; 2164 2165 error = dsl_prop_get_integer(zv->zv_name, 2166 zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL); 2167 error = error ? error : dsl_prop_get_integer(zv->zv_name, 2168 zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL); 2169 error = error ? error : dsl_prop_get_integer(zv->zv_name, 2170 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL); 2171 error = error ? error : dsl_prop_get_integer(zv->zv_name, 2172 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL); 2173 if (version >= SPA_VERSION_DEDUP) { 2174 error = error ? error : 2175 dsl_prop_get_integer(zv->zv_name, 2176 zfs_prop_to_name(ZFS_PROP_DEDUP), &dedup, NULL); 2177 } 2178 2179 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 2180 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, 2181 &compress, tx); 2182 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 2183 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx); 2184 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 2185 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 2186 &refresrv, tx); 2187 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 2188 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, 2189 &vbs, tx); 2190 error = error ? error : dmu_object_set_blocksize( 2191 os, ZVOL_OBJ, SPA_OLD_MAXBLOCKSIZE, 0, tx); 2192 if (version >= SPA_VERSION_DEDUP) { 2193 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 2194 zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, 2195 &dedup, tx); 2196 } 2197 if (error == 0) 2198 zv->zv_volblocksize = SPA_OLD_MAXBLOCKSIZE; 2199 } 2200 dmu_tx_commit(tx); 2201 2202 /* 2203 * We only need update the zvol's property if we are initializing 2204 * the dump area for the first time. 2205 */ 2206 if (!resize) { 2207 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2208 VERIFY(nvlist_add_uint64(nv, 2209 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0); 2210 VERIFY(nvlist_add_uint64(nv, 2211 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 2212 ZIO_COMPRESS_OFF) == 0); 2213 VERIFY(nvlist_add_uint64(nv, 2214 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 2215 checksum) == 0); 2216 if (version >= SPA_VERSION_DEDUP) { 2217 VERIFY(nvlist_add_uint64(nv, 2218 zfs_prop_to_name(ZFS_PROP_DEDUP), 2219 ZIO_CHECKSUM_OFF) == 0); 2220 } 2221 2222 error = zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL, 2223 nv, NULL); 2224 nvlist_free(nv); 2225 2226 if (error) 2227 return (error); 2228 } 2229 2230 /* Allocate the space for the dump */ 2231 error = zvol_prealloc(zv); 2232 return (error); 2233} 2234 2235static int 2236zvol_dumpify(zvol_state_t *zv) 2237{ 2238 int error = 0; 2239 uint64_t dumpsize = 0; 2240 dmu_tx_t *tx; 2241 objset_t *os = zv->zv_objset; 2242 2243 if (zv->zv_flags & ZVOL_RDONLY) 2244 return (SET_ERROR(EROFS)); 2245 2246 if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 2247 8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) { 2248 boolean_t resize = (dumpsize > 0); 2249 2250 if ((error = zvol_dump_init(zv, resize)) != 0) { 2251 (void) zvol_dump_fini(zv); 2252 return (error); 2253 } 2254 } 2255 2256 /* 2257 * Build up our lba mapping. 2258 */ 2259 error = zvol_get_lbas(zv); 2260 if (error) { 2261 (void) zvol_dump_fini(zv); 2262 return (error); 2263 } 2264 2265 tx = dmu_tx_create(os); 2266 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 2267 error = dmu_tx_assign(tx, TXG_WAIT); 2268 if (error) { 2269 dmu_tx_abort(tx); 2270 (void) zvol_dump_fini(zv); 2271 return (error); 2272 } 2273 2274 zv->zv_flags |= ZVOL_DUMPIFIED; 2275 error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1, 2276 &zv->zv_volsize, tx); 2277 dmu_tx_commit(tx); 2278 2279 if (error) { 2280 (void) zvol_dump_fini(zv); 2281 return (error); 2282 } 2283 2284 txg_wait_synced(dmu_objset_pool(os), 0); 2285 return (0); 2286} 2287 2288static int 2289zvol_dump_fini(zvol_state_t *zv) 2290{ 2291 dmu_tx_t *tx; 2292 objset_t *os = zv->zv_objset; 2293 nvlist_t *nv; 2294 int error = 0; 2295 uint64_t checksum, compress, refresrv, vbs, dedup; 2296 uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset)); 2297 2298 /* 2299 * Attempt to restore the zvol back to its pre-dumpified state. 2300 * This is a best-effort attempt as it's possible that not all 2301 * of these properties were initialized during the dumpify process 2302 * (i.e. error during zvol_dump_init). 2303 */ 2304 2305 tx = dmu_tx_create(os); 2306 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 2307 error = dmu_tx_assign(tx, TXG_WAIT); 2308 if (error) { 2309 dmu_tx_abort(tx); 2310 return (error); 2311 } 2312 (void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx); 2313 dmu_tx_commit(tx); 2314 2315 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2316 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum); 2317 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2318 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress); 2319 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2320 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv); 2321 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2322 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs); 2323 2324 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2325 (void) nvlist_add_uint64(nv, 2326 zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum); 2327 (void) nvlist_add_uint64(nv, 2328 zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress); 2329 (void) nvlist_add_uint64(nv, 2330 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv); 2331 if (version >= SPA_VERSION_DEDUP && 2332 zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2333 zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, &dedup) == 0) { 2334 (void) nvlist_add_uint64(nv, 2335 zfs_prop_to_name(ZFS_PROP_DEDUP), dedup); 2336 } 2337 (void) zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL, 2338 nv, NULL); 2339 nvlist_free(nv); 2340 2341 zvol_free_extents(zv); 2342 zv->zv_flags &= ~ZVOL_DUMPIFIED; 2343 (void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END); 2344 /* wait for dmu_free_long_range to actually free the blocks */ 2345 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 2346 tx = dmu_tx_create(os); 2347 dmu_tx_hold_bonus(tx, ZVOL_OBJ); 2348 error = dmu_tx_assign(tx, TXG_WAIT); 2349 if (error) { 2350 dmu_tx_abort(tx); 2351 return (error); 2352 } 2353 if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0) 2354 zv->zv_volblocksize = vbs; 2355 dmu_tx_commit(tx); 2356 2357 return (0); 2358} 2359#endif /* sun */ 2360 2361static void 2362zvol_geom_run(zvol_state_t *zv) 2363{ 2364 struct g_provider *pp; 2365 2366 pp = zv->zv_provider; 2367 g_error_provider(pp, 0); 2368 2369 kproc_kthread_add(zvol_geom_worker, zv, &zfsproc, NULL, 0, 0, 2370 "zfskern", "zvol %s", pp->name + sizeof(ZVOL_DRIVER)); 2371} 2372 2373static void 2374zvol_geom_destroy(zvol_state_t *zv) 2375{ 2376 struct g_provider *pp; 2377 2378 g_topology_assert(); 2379 2380 mtx_lock(&zv->zv_queue_mtx); 2381 zv->zv_state = 1; 2382 wakeup_one(&zv->zv_queue); 2383 while (zv->zv_state != 2) 2384 msleep(&zv->zv_state, &zv->zv_queue_mtx, 0, "zvol:w", 0); 2385 mtx_destroy(&zv->zv_queue_mtx); 2386 2387 pp = zv->zv_provider; 2388 zv->zv_provider = NULL; 2389 pp->private = NULL; 2390 g_wither_geom(pp->geom, ENXIO); 2391} 2392 2393static int 2394zvol_geom_access(struct g_provider *pp, int acr, int acw, int ace) 2395{ 2396 int count, error, flags; 2397 2398 g_topology_assert(); 2399 2400 /* 2401 * To make it easier we expect either open or close, but not both 2402 * at the same time. 2403 */ 2404 KASSERT((acr >= 0 && acw >= 0 && ace >= 0) || 2405 (acr <= 0 && acw <= 0 && ace <= 0), 2406 ("Unsupported access request to %s (acr=%d, acw=%d, ace=%d).", 2407 pp->name, acr, acw, ace)); 2408 2409 if (pp->private == NULL) { 2410 if (acr <= 0 && acw <= 0 && ace <= 0) 2411 return (0); 2412 return (pp->error); 2413 } 2414 2415 /* 2416 * We don't pass FEXCL flag to zvol_open()/zvol_close() if ace != 0, 2417 * because GEOM already handles that and handles it a bit differently. 2418 * GEOM allows for multiple read/exclusive consumers and ZFS allows 2419 * only one exclusive consumer, no matter if it is reader or writer. 2420 * I like better the way GEOM works so I'll leave it for GEOM to 2421 * decide what to do. 2422 */ 2423 2424 count = acr + acw + ace; 2425 if (count == 0) 2426 return (0); 2427 2428 flags = 0; 2429 if (acr != 0 || ace != 0) 2430 flags |= FREAD; 2431 if (acw != 0) 2432 flags |= FWRITE; 2433 2434 g_topology_unlock(); 2435 if (count > 0) 2436 error = zvol_open(pp, flags, count); 2437 else 2438 error = zvol_close(pp, flags, -count); 2439 g_topology_lock(); 2440 return (error); 2441} 2442 2443static void 2444zvol_geom_start(struct bio *bp) 2445{ 2446 zvol_state_t *zv; 2447 boolean_t first; 2448 2449 zv = bp->bio_to->private; 2450 ASSERT(zv != NULL); 2451 switch (bp->bio_cmd) { 2452 case BIO_FLUSH: 2453 if (!THREAD_CAN_SLEEP()) 2454 goto enqueue; 2455 zil_commit(zv->zv_zilog, ZVOL_OBJ); 2456 g_io_deliver(bp, 0); 2457 break; 2458 case BIO_READ: 2459 case BIO_WRITE: 2460 case BIO_DELETE: 2461 if (!THREAD_CAN_SLEEP()) 2462 goto enqueue; 2463 zvol_strategy(bp); 2464 break; 2465 case BIO_GETATTR: { 2466 spa_t *spa = dmu_objset_spa(zv->zv_objset); 2467 uint64_t refd, avail, usedobjs, availobjs, val; 2468 2469 if (g_handleattr_int(bp, "GEOM::candelete", 1)) 2470 return; 2471 if (strcmp(bp->bio_attribute, "blocksavail") == 0) { 2472 dmu_objset_space(zv->zv_objset, &refd, &avail, 2473 &usedobjs, &availobjs); 2474 if (g_handleattr_off_t(bp, "blocksavail", 2475 avail / DEV_BSIZE)) 2476 return; 2477 } else if (strcmp(bp->bio_attribute, "blocksused") == 0) { 2478 dmu_objset_space(zv->zv_objset, &refd, &avail, 2479 &usedobjs, &availobjs); 2480 if (g_handleattr_off_t(bp, "blocksused", 2481 refd / DEV_BSIZE)) 2482 return; 2483 } else if (strcmp(bp->bio_attribute, "poolblocksavail") == 0) { 2484 avail = metaslab_class_get_space(spa_normal_class(spa)); 2485 avail -= metaslab_class_get_alloc(spa_normal_class(spa)); 2486 if (g_handleattr_off_t(bp, "poolblocksavail", 2487 avail / DEV_BSIZE)) 2488 return; 2489 } else if (strcmp(bp->bio_attribute, "poolblocksused") == 0) { 2490 refd = metaslab_class_get_alloc(spa_normal_class(spa)); 2491 if (g_handleattr_off_t(bp, "poolblocksused", 2492 refd / DEV_BSIZE)) 2493 return; 2494 } 2495 /* FALLTHROUGH */ 2496 } 2497 default: 2498 g_io_deliver(bp, EOPNOTSUPP); 2499 break; 2500 } 2501 return; 2502 2503enqueue: 2504 mtx_lock(&zv->zv_queue_mtx); 2505 first = (bioq_first(&zv->zv_queue) == NULL); 2506 bioq_insert_tail(&zv->zv_queue, bp); 2507 mtx_unlock(&zv->zv_queue_mtx); 2508 if (first) 2509 wakeup_one(&zv->zv_queue); 2510} 2511 2512static void 2513zvol_geom_worker(void *arg) 2514{ 2515 zvol_state_t *zv; 2516 struct bio *bp; 2517 2518 thread_lock(curthread); 2519 sched_prio(curthread, PRIBIO); 2520 thread_unlock(curthread); 2521 2522 zv = arg; 2523 for (;;) { 2524 mtx_lock(&zv->zv_queue_mtx); 2525 bp = bioq_takefirst(&zv->zv_queue); 2526 if (bp == NULL) { 2527 if (zv->zv_state == 1) { 2528 zv->zv_state = 2; 2529 wakeup(&zv->zv_state); 2530 mtx_unlock(&zv->zv_queue_mtx); 2531 kthread_exit(); 2532 } 2533 msleep(&zv->zv_queue, &zv->zv_queue_mtx, PRIBIO | PDROP, 2534 "zvol:io", 0); 2535 continue; 2536 } 2537 mtx_unlock(&zv->zv_queue_mtx); 2538 switch (bp->bio_cmd) { 2539 case BIO_FLUSH: 2540 zil_commit(zv->zv_zilog, ZVOL_OBJ); 2541 g_io_deliver(bp, 0); 2542 break; 2543 case BIO_READ: 2544 case BIO_WRITE: 2545 zvol_strategy(bp); 2546 break; 2547 } 2548 } 2549} 2550 2551extern boolean_t dataset_name_hidden(const char *name); 2552 2553static int 2554zvol_create_snapshots(objset_t *os, const char *name) 2555{ 2556 uint64_t cookie, obj; 2557 char *sname; 2558 int error, len; 2559 2560 cookie = obj = 0; 2561 sname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 2562 2563#if 0 2564 (void) dmu_objset_find(name, dmu_objset_prefetch, NULL, 2565 DS_FIND_SNAPSHOTS); 2566#endif 2567 2568 for (;;) { 2569 len = snprintf(sname, MAXPATHLEN, "%s@", name); 2570 if (len >= MAXPATHLEN) { 2571 dmu_objset_rele(os, FTAG); 2572 error = ENAMETOOLONG; 2573 break; 2574 } 2575 2576 dsl_pool_config_enter(dmu_objset_pool(os), FTAG); 2577 error = dmu_snapshot_list_next(os, MAXPATHLEN - len, 2578 sname + len, &obj, &cookie, NULL); 2579 dsl_pool_config_exit(dmu_objset_pool(os), FTAG); 2580 if (error != 0) { 2581 if (error == ENOENT) 2582 error = 0; 2583 break; 2584 } 2585 2586 if ((error = zvol_create_minor(sname)) != 0) { 2587 printf("ZFS WARNING: Unable to create ZVOL %s (error=%d).\n", 2588 sname, error); 2589 break; 2590 } 2591 } 2592 2593 kmem_free(sname, MAXPATHLEN); 2594 return (error); 2595} 2596 2597int 2598zvol_create_minors(const char *name) 2599{ 2600 uint64_t cookie; 2601 objset_t *os; 2602 char *osname, *p; 2603 int error, len; 2604 2605 if (dataset_name_hidden(name)) 2606 return (0); 2607 2608 if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) { 2609 printf("ZFS WARNING: Unable to put hold on %s (error=%d).\n", 2610 name, error); 2611 return (error); 2612 } 2613 if (dmu_objset_type(os) == DMU_OST_ZVOL) { 2614 dsl_dataset_long_hold(os->os_dsl_dataset, FTAG); 2615 dsl_pool_rele(dmu_objset_pool(os), FTAG); 2616 error = zvol_create_minor(name); 2617 if (error == 0 || error == EEXIST) { 2618 error = zvol_create_snapshots(os, name); 2619 } else { 2620 printf("ZFS WARNING: Unable to create ZVOL %s (error=%d).\n", 2621 name, error); 2622 } 2623 dsl_dataset_long_rele(os->os_dsl_dataset, FTAG); 2624 dsl_dataset_rele(os->os_dsl_dataset, FTAG); 2625 return (error); 2626 } 2627 if (dmu_objset_type(os) != DMU_OST_ZFS) { 2628 dmu_objset_rele(os, FTAG); 2629 return (0); 2630 } 2631 2632 osname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 2633 if (snprintf(osname, MAXPATHLEN, "%s/", name) >= MAXPATHLEN) { 2634 dmu_objset_rele(os, FTAG); 2635 kmem_free(osname, MAXPATHLEN); 2636 return (ENOENT); 2637 } 2638 p = osname + strlen(osname); 2639 len = MAXPATHLEN - (p - osname); 2640 2641#if 0 2642 /* Prefetch the datasets. */ 2643 cookie = 0; 2644 while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0) { 2645 if (!dataset_name_hidden(osname)) 2646 (void) dmu_objset_prefetch(osname, NULL); 2647 } 2648#endif 2649 2650 cookie = 0; 2651 while (dmu_dir_list_next(os, MAXPATHLEN - (p - osname), p, NULL, 2652 &cookie) == 0) { 2653 dmu_objset_rele(os, FTAG); 2654 (void)zvol_create_minors(osname); 2655 if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) { 2656 printf("ZFS WARNING: Unable to put hold on %s (error=%d).\n", 2657 name, error); 2658 return (error); 2659 } 2660 } 2661 2662 dmu_objset_rele(os, FTAG); 2663 kmem_free(osname, MAXPATHLEN); 2664 return (0); 2665} 2666 2667static void 2668zvol_rename_minor(zvol_state_t *zv, const char *newname) 2669{ 2670 struct g_geom *gp; 2671 struct g_provider *pp; 2672 struct cdev *dev; 2673 2674 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 2675 2676 if (zv->zv_volmode == ZFS_VOLMODE_GEOM) { 2677 g_topology_lock(); 2678 pp = zv->zv_provider; 2679 ASSERT(pp != NULL); 2680 gp = pp->geom; 2681 ASSERT(gp != NULL); 2682 2683 zv->zv_provider = NULL; 2684 g_wither_provider(pp, ENXIO); 2685 2686 pp = g_new_providerf(gp, "%s/%s", ZVOL_DRIVER, newname); 2687 pp->flags |= G_PF_DIRECT_RECEIVE | G_PF_DIRECT_SEND; 2688 pp->sectorsize = DEV_BSIZE; 2689 pp->mediasize = zv->zv_volsize; 2690 pp->private = zv; 2691 zv->zv_provider = pp; 2692 g_error_provider(pp, 0); 2693 g_topology_unlock(); 2694 } else if (zv->zv_volmode == ZFS_VOLMODE_DEV) { 2695 dev = zv->zv_dev; 2696 ASSERT(dev != NULL); 2697 zv->zv_dev = NULL; 2698 destroy_dev(dev); 2699 2700 if (make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, 2701 &dev, &zvol_cdevsw, NULL, UID_ROOT, GID_OPERATOR, 2702 0640, "%s/%s", ZVOL_DRIVER, newname) == 0) { 2703 zv->zv_dev = dev; 2704 dev->si_iosize_max = MAXPHYS; 2705 dev->si_drv2 = zv; 2706 } 2707 } 2708 strlcpy(zv->zv_name, newname, sizeof(zv->zv_name)); 2709} 2710 2711void 2712zvol_rename_minors(const char *oldname, const char *newname) 2713{ 2714 char name[MAXPATHLEN]; 2715 struct g_provider *pp; 2716 struct g_geom *gp; 2717 size_t oldnamelen, newnamelen; 2718 zvol_state_t *zv; 2719 char *namebuf; 2720 boolean_t locked = B_FALSE; 2721 2722 oldnamelen = strlen(oldname); 2723 newnamelen = strlen(newname); 2724 2725 DROP_GIANT(); 2726 /* See comment in zvol_open(). */ 2727 if (!MUTEX_HELD(&spa_namespace_lock)) { 2728 mutex_enter(&spa_namespace_lock); 2729 locked = B_TRUE; 2730 } 2731 2732 LIST_FOREACH(zv, &all_zvols, zv_links) { 2733 if (strcmp(zv->zv_name, oldname) == 0) { 2734 zvol_rename_minor(zv, newname); 2735 } else if (strncmp(zv->zv_name, oldname, oldnamelen) == 0 && 2736 (zv->zv_name[oldnamelen] == '/' || 2737 zv->zv_name[oldnamelen] == '@')) { 2738 snprintf(name, sizeof(name), "%s%c%s", newname, 2739 zv->zv_name[oldnamelen], 2740 zv->zv_name + oldnamelen + 1); 2741 zvol_rename_minor(zv, name); 2742 } 2743 } 2744 2745 if (locked) 2746 mutex_exit(&spa_namespace_lock); 2747 PICKUP_GIANT(); 2748} 2749 2750static int 2751zvol_d_open(struct cdev *dev, int flags, int fmt, struct thread *td) 2752{ 2753 zvol_state_t *zv; 2754 int err = 0; 2755 2756 mutex_enter(&spa_namespace_lock); 2757 zv = dev->si_drv2; 2758 if (zv == NULL) { 2759 mutex_exit(&spa_namespace_lock); 2760 return(ENXIO); /* zvol_create_minor() not done yet */ 2761 } 2762 2763 if (zv->zv_total_opens == 0) 2764 err = zvol_first_open(zv); 2765 if (err) { 2766 mutex_exit(&spa_namespace_lock); 2767 return (err); 2768 } 2769 if ((flags & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) { 2770 err = SET_ERROR(EROFS); 2771 goto out; 2772 } 2773 if (zv->zv_flags & ZVOL_EXCL) { 2774 err = SET_ERROR(EBUSY); 2775 goto out; 2776 } 2777#ifdef FEXCL 2778 if (flags & FEXCL) { 2779 if (zv->zv_total_opens != 0) { 2780 err = SET_ERROR(EBUSY); 2781 goto out; 2782 } 2783 zv->zv_flags |= ZVOL_EXCL; 2784 } 2785#endif 2786 2787 zv->zv_total_opens++; 2788 mutex_exit(&spa_namespace_lock); 2789 return (err); 2790out: 2791 if (zv->zv_total_opens == 0) 2792 zvol_last_close(zv); 2793 mutex_exit(&spa_namespace_lock); 2794 return (err); 2795} 2796 2797static int 2798zvol_d_close(struct cdev *dev, int flags, int fmt, struct thread *td) 2799{ 2800 zvol_state_t *zv; 2801 int err = 0; 2802 2803 mutex_enter(&spa_namespace_lock); 2804 zv = dev->si_drv2; 2805 if (zv == NULL) { 2806 mutex_exit(&spa_namespace_lock); 2807 return(ENXIO); 2808 } 2809 2810 if (zv->zv_flags & ZVOL_EXCL) { 2811 ASSERT(zv->zv_total_opens == 1); 2812 zv->zv_flags &= ~ZVOL_EXCL; 2813 } 2814 2815 /* 2816 * If the open count is zero, this is a spurious close. 2817 * That indicates a bug in the kernel / DDI framework. 2818 */ 2819 ASSERT(zv->zv_total_opens != 0); 2820 2821 /* 2822 * You may get multiple opens, but only one close. 2823 */ 2824 zv->zv_total_opens--; 2825 2826 if (zv->zv_total_opens == 0) 2827 zvol_last_close(zv); 2828 2829 mutex_exit(&spa_namespace_lock); 2830 return (0); 2831} 2832 2833static int 2834zvol_d_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 2835{ 2836 zvol_state_t *zv; 2837 rl_t *rl; 2838 off_t offset, length, chunk; 2839 int i, error; 2840 u_int u; 2841 2842 zv = dev->si_drv2; 2843 2844 error = 0; 2845 KASSERT(zv->zv_total_opens > 0, 2846 ("Device with zero access count in zvol_d_ioctl")); 2847 2848 i = IOCPARM_LEN(cmd); 2849 switch (cmd) { 2850 case DIOCGSECTORSIZE: 2851 *(u_int *)data = DEV_BSIZE; 2852 break; 2853 case DIOCGMEDIASIZE: 2854 *(off_t *)data = zv->zv_volsize; 2855 break; 2856 case DIOCGFLUSH: 2857 zil_commit(zv->zv_zilog, ZVOL_OBJ); 2858 break; 2859 case DIOCGDELETE: 2860 if (!zvol_unmap_enabled) 2861 break; 2862 2863 offset = ((off_t *)data)[0]; 2864 length = ((off_t *)data)[1]; 2865 if ((offset % DEV_BSIZE) != 0 || (length % DEV_BSIZE) != 0 || 2866 offset < 0 || offset >= zv->zv_volsize || 2867 length <= 0) { 2868 printf("%s: offset=%jd length=%jd\n", __func__, offset, 2869 length); 2870 error = EINVAL; 2871 break; 2872 } 2873 2874 rl = zfs_range_lock(&zv->zv_znode, offset, length, RL_WRITER); 2875 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset); 2876 error = dmu_tx_assign(tx, TXG_WAIT); 2877 if (error != 0) { 2878 dmu_tx_abort(tx); 2879 } else { 2880 zvol_log_truncate(zv, tx, offset, length, B_TRUE); 2881 dmu_tx_commit(tx); 2882 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 2883 offset, length); 2884 } 2885 zfs_range_unlock(rl); 2886 if (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS) 2887 zil_commit(zv->zv_zilog, ZVOL_OBJ); 2888 break; 2889 case DIOCGSTRIPESIZE: 2890 *(off_t *)data = zv->zv_volblocksize; 2891 break; 2892 case DIOCGSTRIPEOFFSET: 2893 *(off_t *)data = 0; 2894 break; 2895 case DIOCGATTR: { 2896 spa_t *spa = dmu_objset_spa(zv->zv_objset); 2897 struct diocgattr_arg *arg = (struct diocgattr_arg *)data; 2898 uint64_t refd, avail, usedobjs, availobjs; 2899 2900 if (strcmp(arg->name, "blocksavail") == 0) { 2901 dmu_objset_space(zv->zv_objset, &refd, &avail, 2902 &usedobjs, &availobjs); 2903 arg->value.off = avail / DEV_BSIZE; 2904 } else if (strcmp(arg->name, "blocksused") == 0) { 2905 dmu_objset_space(zv->zv_objset, &refd, &avail, 2906 &usedobjs, &availobjs); 2907 arg->value.off = refd / DEV_BSIZE; 2908 } else if (strcmp(arg->name, "poolblocksavail") == 0) { 2909 avail = metaslab_class_get_space(spa_normal_class(spa)); 2910 avail -= metaslab_class_get_alloc(spa_normal_class(spa)); 2911 arg->value.off = avail / DEV_BSIZE; 2912 } else if (strcmp(arg->name, "poolblocksused") == 0) { 2913 refd = metaslab_class_get_alloc(spa_normal_class(spa)); 2914 arg->value.off = refd / DEV_BSIZE; 2915 } else 2916 error = ENOIOCTL; 2917 break; 2918 } 2919 case FIOSEEKHOLE: 2920 case FIOSEEKDATA: { 2921 off_t *off = (off_t *)data; 2922 uint64_t noff; 2923 boolean_t hole; 2924 2925 hole = (cmd == FIOSEEKHOLE); 2926 noff = *off; 2927 error = dmu_offset_next(zv->zv_objset, ZVOL_OBJ, hole, &noff); 2928 *off = noff; 2929 break; 2930 } 2931 default: 2932 error = ENOIOCTL; 2933 } 2934 2935 return (error); 2936} 2937