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