1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5ea8dc4b6Seschrock * Common Development and Distribution License (the "License"). 6ea8dc4b6Seschrock * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 22f80ce222SChris Kirby * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23b77b9231SDan McDonald * 24b77b9231SDan McDonald * Portions Copyright 2010 Robert Milkowski 25b77b9231SDan McDonald * 26b77b9231SDan McDonald * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 27be6fd75aSMatthew Ahrens * Copyright (c) 2013 by Delphix. All rights reserved. 28*810e43b2SBill Pijewski * Copyright (c) 2013, Joyent, Inc. All rights reserved. 29fa9e4066Sahrens */ 30fa9e4066Sahrens 31fa9e4066Sahrens /* 32fa9e4066Sahrens * ZFS volume emulation driver. 33fa9e4066Sahrens * 34fa9e4066Sahrens * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes. 35fa9e4066Sahrens * Volumes are accessed through the symbolic links named: 36fa9e4066Sahrens * 37fa9e4066Sahrens * /dev/zvol/dsk/<pool_name>/<dataset_name> 38fa9e4066Sahrens * /dev/zvol/rdsk/<pool_name>/<dataset_name> 39fa9e4066Sahrens * 40681d9761SEric Taylor * These links are created by the /dev filesystem (sdev_zvolops.c). 41fa9e4066Sahrens * Volumes are persistent through reboot. No user command needs to be 42fa9e4066Sahrens * run before opening and using a device. 43fa9e4066Sahrens */ 44fa9e4066Sahrens 45fa9e4066Sahrens #include <sys/types.h> 46fa9e4066Sahrens #include <sys/param.h> 47fa9e4066Sahrens #include <sys/errno.h> 48fa9e4066Sahrens #include <sys/uio.h> 49fa9e4066Sahrens #include <sys/buf.h> 50fa9e4066Sahrens #include <sys/modctl.h> 51fa9e4066Sahrens #include <sys/open.h> 52fa9e4066Sahrens #include <sys/kmem.h> 53fa9e4066Sahrens #include <sys/conf.h> 54fa9e4066Sahrens #include <sys/cmn_err.h> 55fa9e4066Sahrens #include <sys/stat.h> 56fa9e4066Sahrens #include <sys/zap.h> 57fa9e4066Sahrens #include <sys/spa.h> 58*810e43b2SBill Pijewski #include <sys/spa_impl.h> 59fa9e4066Sahrens #include <sys/zio.h> 60e7cbe64fSgw #include <sys/dmu_traverse.h> 61e7cbe64fSgw #include <sys/dnode.h> 62e7cbe64fSgw #include <sys/dsl_dataset.h> 63fa9e4066Sahrens #include <sys/dsl_prop.h> 64fa9e4066Sahrens #include <sys/dkio.h> 65fa9e4066Sahrens #include <sys/efi_partition.h> 66fa9e4066Sahrens #include <sys/byteorder.h> 67fa9e4066Sahrens #include <sys/pathname.h> 68fa9e4066Sahrens #include <sys/ddi.h> 69fa9e4066Sahrens #include <sys/sunddi.h> 70fa9e4066Sahrens #include <sys/crc32.h> 71fa9e4066Sahrens #include <sys/dirent.h> 72fa9e4066Sahrens #include <sys/policy.h> 73fa9e4066Sahrens #include <sys/fs/zfs.h> 74fa9e4066Sahrens #include <sys/zfs_ioctl.h> 75fa9e4066Sahrens #include <sys/mkdev.h> 7622ac5be4Sperrin #include <sys/zil.h> 77c5c6ffa0Smaybee #include <sys/refcount.h> 78c2e6a7d6Sperrin #include <sys/zfs_znode.h> 79c2e6a7d6Sperrin #include <sys/zfs_rlock.h> 80e7cbe64fSgw #include <sys/vdev_disk.h> 81e7cbe64fSgw #include <sys/vdev_impl.h> 82*810e43b2SBill Pijewski #include <sys/vdev_raidz.h> 83e7cbe64fSgw #include <sys/zvol.h> 84e7cbe64fSgw #include <sys/dumphdr.h> 851209a471SNeil Perrin #include <sys/zil_impl.h> 8680901aeaSGeorge Wilson #include <sys/dbuf.h> 87*810e43b2SBill Pijewski #include <sys/dmu_tx.h> 88*810e43b2SBill Pijewski #include <sys/zfeature.h> 89*810e43b2SBill Pijewski #include <sys/zio_checksum.h> 90fa9e4066Sahrens 91fa9e4066Sahrens #include "zfs_namecheck.h" 92fa9e4066Sahrens 93c99e4bdcSChris Kirby void *zfsdev_state; 94503ad85cSMatthew Ahrens static char *zvol_tag = "zvol_tag"; 95fa9e4066Sahrens 96e7cbe64fSgw #define ZVOL_DUMPSIZE "dumpsize" 97e7cbe64fSgw 98fa9e4066Sahrens /* 99c99e4bdcSChris Kirby * This lock protects the zfsdev_state structure from being modified 100fa9e4066Sahrens * while it's being used, e.g. an open that comes in before a create 101fa9e4066Sahrens * finishes. It also protects temporary opens of the dataset so that, 102fa9e4066Sahrens * e.g., an open doesn't get a spurious EBUSY. 103fa9e4066Sahrens */ 104c99e4bdcSChris Kirby kmutex_t zfsdev_state_lock; 105fa9e4066Sahrens static uint32_t zvol_minors; 106fa9e4066Sahrens 107e7cbe64fSgw typedef struct zvol_extent { 10888b7b0f2SMatthew Ahrens list_node_t ze_node; 109e7cbe64fSgw dva_t ze_dva; /* dva associated with this extent */ 11088b7b0f2SMatthew Ahrens uint64_t ze_nblks; /* number of blocks in extent */ 111e7cbe64fSgw } zvol_extent_t; 112e7cbe64fSgw 113fa9e4066Sahrens /* 114fa9e4066Sahrens * The in-core state of each volume. 115fa9e4066Sahrens */ 116fa9e4066Sahrens typedef struct zvol_state { 117fa9e4066Sahrens char zv_name[MAXPATHLEN]; /* pool/dd name */ 118fa9e4066Sahrens uint64_t zv_volsize; /* amount of space we advertise */ 11967bd71c6Sperrin uint64_t zv_volblocksize; /* volume block size */ 120fa9e4066Sahrens minor_t zv_minor; /* minor number */ 121fa9e4066Sahrens uint8_t zv_min_bs; /* minimum addressable block shift */ 122701f66c4SEric Taylor uint8_t zv_flags; /* readonly, dumpified, etc. */ 123fa9e4066Sahrens objset_t *zv_objset; /* objset handle */ 124fa9e4066Sahrens uint32_t zv_open_count[OTYPCNT]; /* open counts */ 125fa9e4066Sahrens uint32_t zv_total_opens; /* total open count */ 12622ac5be4Sperrin zilog_t *zv_zilog; /* ZIL handle */ 12788b7b0f2SMatthew Ahrens list_t zv_extents; /* List of extents for dump */ 128c2e6a7d6Sperrin znode_t zv_znode; /* for range locking */ 12994d1a210STim Haley dmu_buf_t *zv_dbuf; /* bonus handle */ 130fa9e4066Sahrens } zvol_state_t; 131fa9e4066Sahrens 132e7cbe64fSgw /* 133e7cbe64fSgw * zvol specific flags 134e7cbe64fSgw */ 135e7cbe64fSgw #define ZVOL_RDONLY 0x1 136e7cbe64fSgw #define ZVOL_DUMPIFIED 0x2 137c7f714e2SEric Taylor #define ZVOL_EXCL 0x4 138701f66c4SEric Taylor #define ZVOL_WCE 0x8 139e7cbe64fSgw 14067bd71c6Sperrin /* 14167bd71c6Sperrin * zvol maximum transfer in one DMU tx. 14267bd71c6Sperrin */ 14367bd71c6Sperrin int zvol_maxphys = DMU_MAX_ACCESS/2; 14467bd71c6Sperrin 14592241e0bSTom Erickson extern int zfs_set_prop_nvlist(const char *, zprop_source_t, 1464445fffbSMatthew Ahrens nvlist_t *, nvlist_t *); 147681d9761SEric Taylor static int zvol_remove_zv(zvol_state_t *); 148feb08c6bSbillm static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio); 149e7cbe64fSgw static int zvol_dumpify(zvol_state_t *zv); 150e7cbe64fSgw static int zvol_dump_fini(zvol_state_t *zv); 151e7cbe64fSgw static int zvol_dump_init(zvol_state_t *zv, boolean_t resize); 15267bd71c6Sperrin 153fa9e4066Sahrens static void 154c61ea566SGeorge Wilson zvol_size_changed(zvol_state_t *zv, uint64_t volsize) 155fa9e4066Sahrens { 156c61ea566SGeorge Wilson dev_t dev = makedevice(ddi_driver_major(zfs_dip), zv->zv_minor); 157fa9e4066Sahrens 158c61ea566SGeorge Wilson zv->zv_volsize = volsize; 159fa9e4066Sahrens VERIFY(ddi_prop_update_int64(dev, zfs_dip, 160681d9761SEric Taylor "Size", volsize) == DDI_SUCCESS); 161fa9e4066Sahrens VERIFY(ddi_prop_update_int64(dev, zfs_dip, 162681d9761SEric Taylor "Nblocks", lbtodb(volsize)) == DDI_SUCCESS); 163e7cbe64fSgw 164e7cbe64fSgw /* Notify specfs to invalidate the cached size */ 165e7cbe64fSgw spec_size_invalidate(dev, VBLK); 166e7cbe64fSgw spec_size_invalidate(dev, VCHR); 167fa9e4066Sahrens } 168fa9e4066Sahrens 169fa9e4066Sahrens int 170e9dbad6fSeschrock zvol_check_volsize(uint64_t volsize, uint64_t blocksize) 171fa9e4066Sahrens { 172e9dbad6fSeschrock if (volsize == 0) 173be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 174fa9e4066Sahrens 175e9dbad6fSeschrock if (volsize % blocksize != 0) 176be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 1775c5460e9Seschrock 178fa9e4066Sahrens #ifdef _ILP32 179e9dbad6fSeschrock if (volsize - 1 > SPEC_MAXOFFSET_T) 180be6fd75aSMatthew Ahrens return (SET_ERROR(EOVERFLOW)); 181fa9e4066Sahrens #endif 182fa9e4066Sahrens return (0); 183fa9e4066Sahrens } 184fa9e4066Sahrens 185fa9e4066Sahrens int 186e9dbad6fSeschrock zvol_check_volblocksize(uint64_t volblocksize) 187fa9e4066Sahrens { 188e9dbad6fSeschrock if (volblocksize < SPA_MINBLOCKSIZE || 189e9dbad6fSeschrock volblocksize > SPA_MAXBLOCKSIZE || 190e9dbad6fSeschrock !ISP2(volblocksize)) 191be6fd75aSMatthew Ahrens return (SET_ERROR(EDOM)); 192fa9e4066Sahrens 193fa9e4066Sahrens return (0); 194fa9e4066Sahrens } 195fa9e4066Sahrens 196fa9e4066Sahrens int 197a2eea2e1Sahrens zvol_get_stats(objset_t *os, nvlist_t *nv) 198fa9e4066Sahrens { 199fa9e4066Sahrens int error; 200fa9e4066Sahrens dmu_object_info_t doi; 201a2eea2e1Sahrens uint64_t val; 202fa9e4066Sahrens 203a2eea2e1Sahrens error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val); 204fa9e4066Sahrens if (error) 205fa9e4066Sahrens return (error); 206fa9e4066Sahrens 207a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val); 208a2eea2e1Sahrens 209fa9e4066Sahrens error = dmu_object_info(os, ZVOL_OBJ, &doi); 210fa9e4066Sahrens 211a2eea2e1Sahrens if (error == 0) { 212a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE, 213a2eea2e1Sahrens doi.doi_data_block_size); 214a2eea2e1Sahrens } 215fa9e4066Sahrens 216fa9e4066Sahrens return (error); 217fa9e4066Sahrens } 218fa9e4066Sahrens 219fa9e4066Sahrens static zvol_state_t * 220e9dbad6fSeschrock zvol_minor_lookup(const char *name) 221fa9e4066Sahrens { 222fa9e4066Sahrens minor_t minor; 223fa9e4066Sahrens zvol_state_t *zv; 224fa9e4066Sahrens 225c99e4bdcSChris Kirby ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 226fa9e4066Sahrens 227c99e4bdcSChris Kirby for (minor = 1; minor <= ZFSDEV_MAX_MINOR; minor++) { 228c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 229fa9e4066Sahrens if (zv == NULL) 230fa9e4066Sahrens continue; 231fa9e4066Sahrens if (strcmp(zv->zv_name, name) == 0) 232f80ce222SChris Kirby return (zv); 233fa9e4066Sahrens } 234fa9e4066Sahrens 235f80ce222SChris Kirby return (NULL); 236fa9e4066Sahrens } 237fa9e4066Sahrens 238e7cbe64fSgw /* extent mapping arg */ 239e7cbe64fSgw struct maparg { 24088b7b0f2SMatthew Ahrens zvol_state_t *ma_zv; 24188b7b0f2SMatthew Ahrens uint64_t ma_blks; 242e7cbe64fSgw }; 243e7cbe64fSgw 244e7cbe64fSgw /*ARGSUSED*/ 245e7cbe64fSgw static int 2461b912ec7SGeorge Wilson zvol_map_block(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 247b24ab676SJeff Bonwick const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg) 248e7cbe64fSgw { 24988b7b0f2SMatthew Ahrens struct maparg *ma = arg; 25088b7b0f2SMatthew Ahrens zvol_extent_t *ze; 25188b7b0f2SMatthew Ahrens int bs = ma->ma_zv->zv_volblocksize; 252e7cbe64fSgw 25388b7b0f2SMatthew Ahrens if (bp == NULL || zb->zb_object != ZVOL_OBJ || zb->zb_level != 0) 25488b7b0f2SMatthew Ahrens return (0); 255e7cbe64fSgw 25688b7b0f2SMatthew Ahrens VERIFY3U(ma->ma_blks, ==, zb->zb_blkid); 25788b7b0f2SMatthew Ahrens ma->ma_blks++; 258e7cbe64fSgw 25988b7b0f2SMatthew Ahrens /* Abort immediately if we have encountered gang blocks */ 26088b7b0f2SMatthew Ahrens if (BP_IS_GANG(bp)) 261be6fd75aSMatthew Ahrens return (SET_ERROR(EFRAGS)); 262e7cbe64fSgw 26388b7b0f2SMatthew Ahrens /* 26488b7b0f2SMatthew Ahrens * See if the block is at the end of the previous extent. 26588b7b0f2SMatthew Ahrens */ 26688b7b0f2SMatthew Ahrens ze = list_tail(&ma->ma_zv->zv_extents); 26788b7b0f2SMatthew Ahrens if (ze && 26888b7b0f2SMatthew Ahrens DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) && 26988b7b0f2SMatthew Ahrens DVA_GET_OFFSET(BP_IDENTITY(bp)) == 27088b7b0f2SMatthew Ahrens DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) { 27188b7b0f2SMatthew Ahrens ze->ze_nblks++; 27288b7b0f2SMatthew Ahrens return (0); 273e7cbe64fSgw } 274e7cbe64fSgw 27588b7b0f2SMatthew Ahrens dprintf_bp(bp, "%s", "next blkptr:"); 276e7cbe64fSgw 27788b7b0f2SMatthew Ahrens /* start a new extent */ 27888b7b0f2SMatthew Ahrens ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP); 27988b7b0f2SMatthew Ahrens ze->ze_dva = bp->blk_dva[0]; /* structure assignment */ 28088b7b0f2SMatthew Ahrens ze->ze_nblks = 1; 28188b7b0f2SMatthew Ahrens list_insert_tail(&ma->ma_zv->zv_extents, ze); 28288b7b0f2SMatthew Ahrens return (0); 28388b7b0f2SMatthew Ahrens } 284e7cbe64fSgw 28588b7b0f2SMatthew Ahrens static void 28688b7b0f2SMatthew Ahrens zvol_free_extents(zvol_state_t *zv) 28788b7b0f2SMatthew Ahrens { 28888b7b0f2SMatthew Ahrens zvol_extent_t *ze; 289e7cbe64fSgw 29088b7b0f2SMatthew Ahrens while (ze = list_head(&zv->zv_extents)) { 29188b7b0f2SMatthew Ahrens list_remove(&zv->zv_extents, ze); 29288b7b0f2SMatthew Ahrens kmem_free(ze, sizeof (zvol_extent_t)); 293e7cbe64fSgw } 29488b7b0f2SMatthew Ahrens } 295e7cbe64fSgw 29688b7b0f2SMatthew Ahrens static int 29788b7b0f2SMatthew Ahrens zvol_get_lbas(zvol_state_t *zv) 29888b7b0f2SMatthew Ahrens { 2993adc9019SEric Taylor objset_t *os = zv->zv_objset; 30088b7b0f2SMatthew Ahrens struct maparg ma; 30188b7b0f2SMatthew Ahrens int err; 30288b7b0f2SMatthew Ahrens 30388b7b0f2SMatthew Ahrens ma.ma_zv = zv; 30488b7b0f2SMatthew Ahrens ma.ma_blks = 0; 30588b7b0f2SMatthew Ahrens zvol_free_extents(zv); 30688b7b0f2SMatthew Ahrens 3073adc9019SEric Taylor /* commit any in-flight changes before traversing the dataset */ 3083adc9019SEric Taylor txg_wait_synced(dmu_objset_pool(os), 0); 3093adc9019SEric Taylor err = traverse_dataset(dmu_objset_ds(os), 0, 31088b7b0f2SMatthew Ahrens TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma); 31188b7b0f2SMatthew Ahrens if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) { 31288b7b0f2SMatthew Ahrens zvol_free_extents(zv); 31388b7b0f2SMatthew Ahrens return (err ? err : EIO); 314e7cbe64fSgw } 31588b7b0f2SMatthew Ahrens 316e7cbe64fSgw return (0); 317e7cbe64fSgw } 318e7cbe64fSgw 319ecd6cf80Smarks /* ARGSUSED */ 320fa9e4066Sahrens void 321ecd6cf80Smarks zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 322fa9e4066Sahrens { 323da6c28aaSamw zfs_creat_t *zct = arg; 324da6c28aaSamw nvlist_t *nvprops = zct->zct_props; 325fa9e4066Sahrens int error; 326e9dbad6fSeschrock uint64_t volblocksize, volsize; 327fa9e4066Sahrens 328ecd6cf80Smarks VERIFY(nvlist_lookup_uint64(nvprops, 329e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0); 330ecd6cf80Smarks if (nvlist_lookup_uint64(nvprops, 331e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0) 332e9dbad6fSeschrock volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 333e9dbad6fSeschrock 334e9dbad6fSeschrock /* 335e7cbe64fSgw * These properties must be removed from the list so the generic 336e9dbad6fSeschrock * property setting step won't apply to them. 337e9dbad6fSeschrock */ 338ecd6cf80Smarks VERIFY(nvlist_remove_all(nvprops, 339e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0); 340ecd6cf80Smarks (void) nvlist_remove_all(nvprops, 341e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE)); 342e9dbad6fSeschrock 343e9dbad6fSeschrock error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize, 344fa9e4066Sahrens DMU_OT_NONE, 0, tx); 345fa9e4066Sahrens ASSERT(error == 0); 346fa9e4066Sahrens 347fa9e4066Sahrens error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP, 348fa9e4066Sahrens DMU_OT_NONE, 0, tx); 349fa9e4066Sahrens ASSERT(error == 0); 350fa9e4066Sahrens 351e9dbad6fSeschrock error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx); 352fa9e4066Sahrens ASSERT(error == 0); 353fa9e4066Sahrens } 354fa9e4066Sahrens 355b77b9231SDan McDonald /* 356b77b9231SDan McDonald * Replay a TX_TRUNCATE ZIL transaction if asked. TX_TRUNCATE is how we 357b77b9231SDan McDonald * implement DKIOCFREE/free-long-range. 358b77b9231SDan McDonald */ 359b77b9231SDan McDonald static int 360b77b9231SDan McDonald zvol_replay_truncate(zvol_state_t *zv, lr_truncate_t *lr, boolean_t byteswap) 361b77b9231SDan McDonald { 362b77b9231SDan McDonald uint64_t offset, length; 363b77b9231SDan McDonald 364b77b9231SDan McDonald if (byteswap) 365b77b9231SDan McDonald byteswap_uint64_array(lr, sizeof (*lr)); 366b77b9231SDan McDonald 367b77b9231SDan McDonald offset = lr->lr_offset; 368b77b9231SDan McDonald length = lr->lr_length; 369b77b9231SDan McDonald 370b77b9231SDan McDonald return (dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, offset, length)); 371b77b9231SDan McDonald } 372b77b9231SDan McDonald 37322ac5be4Sperrin /* 37422ac5be4Sperrin * Replay a TX_WRITE ZIL transaction that didn't get committed 37522ac5be4Sperrin * after a system failure 37622ac5be4Sperrin */ 37722ac5be4Sperrin static int 37822ac5be4Sperrin zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap) 37922ac5be4Sperrin { 38022ac5be4Sperrin objset_t *os = zv->zv_objset; 38122ac5be4Sperrin char *data = (char *)(lr + 1); /* data follows lr_write_t */ 382b24ab676SJeff Bonwick uint64_t offset, length; 38322ac5be4Sperrin dmu_tx_t *tx; 38422ac5be4Sperrin int error; 38522ac5be4Sperrin 38622ac5be4Sperrin if (byteswap) 38722ac5be4Sperrin byteswap_uint64_array(lr, sizeof (*lr)); 38822ac5be4Sperrin 389b24ab676SJeff Bonwick offset = lr->lr_offset; 390b24ab676SJeff Bonwick length = lr->lr_length; 391b24ab676SJeff Bonwick 392b24ab676SJeff Bonwick /* If it's a dmu_sync() block, write the whole block */ 393b24ab676SJeff Bonwick if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) { 394b24ab676SJeff Bonwick uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr); 395b24ab676SJeff Bonwick if (length < blocksize) { 396b24ab676SJeff Bonwick offset -= offset % blocksize; 397b24ab676SJeff Bonwick length = blocksize; 398b24ab676SJeff Bonwick } 399b24ab676SJeff Bonwick } 400975c32a0SNeil Perrin 40122ac5be4Sperrin tx = dmu_tx_create(os); 402b24ab676SJeff Bonwick dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length); 4031209a471SNeil Perrin error = dmu_tx_assign(tx, TXG_WAIT); 40422ac5be4Sperrin if (error) { 40522ac5be4Sperrin dmu_tx_abort(tx); 40622ac5be4Sperrin } else { 407b24ab676SJeff Bonwick dmu_write(os, ZVOL_OBJ, offset, length, data, tx); 40822ac5be4Sperrin dmu_tx_commit(tx); 40922ac5be4Sperrin } 41022ac5be4Sperrin 41122ac5be4Sperrin return (error); 41222ac5be4Sperrin } 41322ac5be4Sperrin 41422ac5be4Sperrin /* ARGSUSED */ 41522ac5be4Sperrin static int 41622ac5be4Sperrin zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap) 41722ac5be4Sperrin { 418be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 41922ac5be4Sperrin } 42022ac5be4Sperrin 42122ac5be4Sperrin /* 42222ac5be4Sperrin * Callback vectors for replaying records. 423b77b9231SDan McDonald * Only TX_WRITE and TX_TRUNCATE are needed for zvol. 42422ac5be4Sperrin */ 42522ac5be4Sperrin zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = { 42622ac5be4Sperrin zvol_replay_err, /* 0 no such transaction type */ 42722ac5be4Sperrin zvol_replay_err, /* TX_CREATE */ 42822ac5be4Sperrin zvol_replay_err, /* TX_MKDIR */ 42922ac5be4Sperrin zvol_replay_err, /* TX_MKXATTR */ 43022ac5be4Sperrin zvol_replay_err, /* TX_SYMLINK */ 43122ac5be4Sperrin zvol_replay_err, /* TX_REMOVE */ 43222ac5be4Sperrin zvol_replay_err, /* TX_RMDIR */ 43322ac5be4Sperrin zvol_replay_err, /* TX_LINK */ 43422ac5be4Sperrin zvol_replay_err, /* TX_RENAME */ 43522ac5be4Sperrin zvol_replay_write, /* TX_WRITE */ 436b77b9231SDan McDonald zvol_replay_truncate, /* TX_TRUNCATE */ 43722ac5be4Sperrin zvol_replay_err, /* TX_SETATTR */ 43822ac5be4Sperrin zvol_replay_err, /* TX_ACL */ 439975c32a0SNeil Perrin zvol_replay_err, /* TX_CREATE_ACL */ 440975c32a0SNeil Perrin zvol_replay_err, /* TX_CREATE_ATTR */ 441975c32a0SNeil Perrin zvol_replay_err, /* TX_CREATE_ACL_ATTR */ 442975c32a0SNeil Perrin zvol_replay_err, /* TX_MKDIR_ACL */ 443975c32a0SNeil Perrin zvol_replay_err, /* TX_MKDIR_ATTR */ 444975c32a0SNeil Perrin zvol_replay_err, /* TX_MKDIR_ACL_ATTR */ 445975c32a0SNeil Perrin zvol_replay_err, /* TX_WRITE2 */ 44622ac5be4Sperrin }; 44722ac5be4Sperrin 448681d9761SEric Taylor int 449681d9761SEric Taylor zvol_name2minor(const char *name, minor_t *minor) 450681d9761SEric Taylor { 451681d9761SEric Taylor zvol_state_t *zv; 452681d9761SEric Taylor 453c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 454681d9761SEric Taylor zv = zvol_minor_lookup(name); 455681d9761SEric Taylor if (minor && zv) 456681d9761SEric Taylor *minor = zv->zv_minor; 457c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 458681d9761SEric Taylor return (zv ? 0 : -1); 459681d9761SEric Taylor } 460681d9761SEric Taylor 461e7cbe64fSgw /* 462e7cbe64fSgw * Create a minor node (plus a whole lot more) for the specified volume. 463fa9e4066Sahrens */ 464fa9e4066Sahrens int 465681d9761SEric Taylor zvol_create_minor(const char *name) 466fa9e4066Sahrens { 467c99e4bdcSChris Kirby zfs_soft_state_t *zs; 468fa9e4066Sahrens zvol_state_t *zv; 469fa9e4066Sahrens objset_t *os; 47067bd71c6Sperrin dmu_object_info_t doi; 471fa9e4066Sahrens minor_t minor = 0; 472fa9e4066Sahrens char chrbuf[30], blkbuf[30]; 473fa9e4066Sahrens int error; 474fa9e4066Sahrens 475c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 476fa9e4066Sahrens 4771195e687SMark J Musante if (zvol_minor_lookup(name) != NULL) { 478c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 479be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 480fa9e4066Sahrens } 481fa9e4066Sahrens 482503ad85cSMatthew Ahrens /* lie and say we're read-only */ 4836e0cbcaaSMatthew Ahrens error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, FTAG, &os); 484fa9e4066Sahrens 485fa9e4066Sahrens if (error) { 486c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 487fa9e4066Sahrens return (error); 488fa9e4066Sahrens } 489fa9e4066Sahrens 490c99e4bdcSChris Kirby if ((minor = zfsdev_minor_alloc()) == 0) { 4916e0cbcaaSMatthew Ahrens dmu_objset_disown(os, FTAG); 492c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 493be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 494fa9e4066Sahrens } 495fa9e4066Sahrens 496c99e4bdcSChris Kirby if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS) { 4976e0cbcaaSMatthew Ahrens dmu_objset_disown(os, FTAG); 498c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 499be6fd75aSMatthew Ahrens return (SET_ERROR(EAGAIN)); 500fa9e4066Sahrens } 501e9dbad6fSeschrock (void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME, 502e9dbad6fSeschrock (char *)name); 503fa9e4066Sahrens 504681d9761SEric Taylor (void) snprintf(chrbuf, sizeof (chrbuf), "%u,raw", minor); 505fa9e4066Sahrens 506fa9e4066Sahrens if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR, 507fa9e4066Sahrens minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 508c99e4bdcSChris Kirby ddi_soft_state_free(zfsdev_state, minor); 5096e0cbcaaSMatthew Ahrens dmu_objset_disown(os, FTAG); 510c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 511be6fd75aSMatthew Ahrens return (SET_ERROR(EAGAIN)); 512fa9e4066Sahrens } 513fa9e4066Sahrens 514681d9761SEric Taylor (void) snprintf(blkbuf, sizeof (blkbuf), "%u", minor); 515fa9e4066Sahrens 516fa9e4066Sahrens if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK, 517fa9e4066Sahrens minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 518fa9e4066Sahrens ddi_remove_minor_node(zfs_dip, chrbuf); 519c99e4bdcSChris Kirby ddi_soft_state_free(zfsdev_state, minor); 5206e0cbcaaSMatthew Ahrens dmu_objset_disown(os, FTAG); 521c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 522be6fd75aSMatthew Ahrens return (SET_ERROR(EAGAIN)); 523fa9e4066Sahrens } 524fa9e4066Sahrens 525c99e4bdcSChris Kirby zs = ddi_get_soft_state(zfsdev_state, minor); 526c99e4bdcSChris Kirby zs->zss_type = ZSST_ZVOL; 527c99e4bdcSChris Kirby zv = zs->zss_data = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP); 528681d9761SEric Taylor (void) strlcpy(zv->zv_name, name, MAXPATHLEN); 529fa9e4066Sahrens zv->zv_min_bs = DEV_BSHIFT; 530fa9e4066Sahrens zv->zv_minor = minor; 531fa9e4066Sahrens zv->zv_objset = os; 532f9af39baSGeorge Wilson if (dmu_objset_is_snapshot(os) || !spa_writeable(dmu_objset_spa(os))) 533681d9761SEric Taylor zv->zv_flags |= ZVOL_RDONLY; 534c2e6a7d6Sperrin mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL); 535c2e6a7d6Sperrin avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare, 536c2e6a7d6Sperrin sizeof (rl_t), offsetof(rl_t, r_node)); 53788b7b0f2SMatthew Ahrens list_create(&zv->zv_extents, sizeof (zvol_extent_t), 53888b7b0f2SMatthew Ahrens offsetof(zvol_extent_t, ze_node)); 53967bd71c6Sperrin /* get and cache the blocksize */ 54067bd71c6Sperrin error = dmu_object_info(os, ZVOL_OBJ, &doi); 54167bd71c6Sperrin ASSERT(error == 0); 54267bd71c6Sperrin zv->zv_volblocksize = doi.doi_data_block_size; 54322ac5be4Sperrin 544f9af39baSGeorge Wilson if (spa_writeable(dmu_objset_spa(os))) { 545f9af39baSGeorge Wilson if (zil_replay_disable) 546f9af39baSGeorge Wilson zil_destroy(dmu_objset_zil(os), B_FALSE); 547f9af39baSGeorge Wilson else 548f9af39baSGeorge Wilson zil_replay(os, zv, zvol_replay_vector); 549f9af39baSGeorge Wilson } 5506e0cbcaaSMatthew Ahrens dmu_objset_disown(os, FTAG); 551681d9761SEric Taylor zv->zv_objset = NULL; 552fa9e4066Sahrens 553fa9e4066Sahrens zvol_minors++; 554fa9e4066Sahrens 555c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 556fa9e4066Sahrens 557fa9e4066Sahrens return (0); 558fa9e4066Sahrens } 559fa9e4066Sahrens 560fa9e4066Sahrens /* 561fa9e4066Sahrens * Remove minor node for the specified volume. 562fa9e4066Sahrens */ 563681d9761SEric Taylor static int 564681d9761SEric Taylor zvol_remove_zv(zvol_state_t *zv) 565681d9761SEric Taylor { 566681d9761SEric Taylor char nmbuf[20]; 567c99e4bdcSChris Kirby minor_t minor = zv->zv_minor; 568681d9761SEric Taylor 569c99e4bdcSChris Kirby ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 570681d9761SEric Taylor if (zv->zv_total_opens != 0) 571be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 572681d9761SEric Taylor 573c99e4bdcSChris Kirby (void) snprintf(nmbuf, sizeof (nmbuf), "%u,raw", minor); 574681d9761SEric Taylor ddi_remove_minor_node(zfs_dip, nmbuf); 575681d9761SEric Taylor 576c99e4bdcSChris Kirby (void) snprintf(nmbuf, sizeof (nmbuf), "%u", minor); 577681d9761SEric Taylor ddi_remove_minor_node(zfs_dip, nmbuf); 578681d9761SEric Taylor 579681d9761SEric Taylor avl_destroy(&zv->zv_znode.z_range_avl); 580681d9761SEric Taylor mutex_destroy(&zv->zv_znode.z_range_lock); 581681d9761SEric Taylor 582c99e4bdcSChris Kirby kmem_free(zv, sizeof (zvol_state_t)); 583c99e4bdcSChris Kirby 584c99e4bdcSChris Kirby ddi_soft_state_free(zfsdev_state, minor); 585681d9761SEric Taylor 586681d9761SEric Taylor zvol_minors--; 587681d9761SEric Taylor return (0); 588681d9761SEric Taylor } 589681d9761SEric Taylor 590fa9e4066Sahrens int 591e9dbad6fSeschrock zvol_remove_minor(const char *name) 592fa9e4066Sahrens { 593fa9e4066Sahrens zvol_state_t *zv; 594681d9761SEric Taylor int rc; 595fa9e4066Sahrens 596c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 597e9dbad6fSeschrock if ((zv = zvol_minor_lookup(name)) == NULL) { 598c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 599be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 600fa9e4066Sahrens } 601681d9761SEric Taylor rc = zvol_remove_zv(zv); 602c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 603681d9761SEric Taylor return (rc); 604681d9761SEric Taylor } 605fa9e4066Sahrens 606681d9761SEric Taylor int 607681d9761SEric Taylor zvol_first_open(zvol_state_t *zv) 608681d9761SEric Taylor { 609681d9761SEric Taylor objset_t *os; 610681d9761SEric Taylor uint64_t volsize; 611681d9761SEric Taylor int error; 612681d9761SEric Taylor uint64_t readonly; 613fa9e4066Sahrens 614681d9761SEric Taylor /* lie and say we're read-only */ 615681d9761SEric Taylor error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, B_TRUE, 616681d9761SEric Taylor zvol_tag, &os); 617681d9761SEric Taylor if (error) 618681d9761SEric Taylor return (error); 619fa9e4066Sahrens 620c61ea566SGeorge Wilson zv->zv_objset = os; 621681d9761SEric Taylor error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize); 622681d9761SEric Taylor if (error) { 623681d9761SEric Taylor ASSERT(error == 0); 624681d9761SEric Taylor dmu_objset_disown(os, zvol_tag); 625681d9761SEric Taylor return (error); 626681d9761SEric Taylor } 627c61ea566SGeorge Wilson 62894d1a210STim Haley error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf); 62994d1a210STim Haley if (error) { 63094d1a210STim Haley dmu_objset_disown(os, zvol_tag); 63194d1a210STim Haley return (error); 63294d1a210STim Haley } 633c61ea566SGeorge Wilson 634c61ea566SGeorge Wilson zvol_size_changed(zv, volsize); 635681d9761SEric Taylor zv->zv_zilog = zil_open(os, zvol_get_data); 636fa9e4066Sahrens 637681d9761SEric Taylor VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &readonly, 638681d9761SEric Taylor NULL) == 0); 639f9af39baSGeorge Wilson if (readonly || dmu_objset_is_snapshot(os) || 640f9af39baSGeorge Wilson !spa_writeable(dmu_objset_spa(os))) 641681d9761SEric Taylor zv->zv_flags |= ZVOL_RDONLY; 642681d9761SEric Taylor else 643681d9761SEric Taylor zv->zv_flags &= ~ZVOL_RDONLY; 644681d9761SEric Taylor return (error); 645681d9761SEric Taylor } 646fa9e4066Sahrens 647681d9761SEric Taylor void 648681d9761SEric Taylor zvol_last_close(zvol_state_t *zv) 649681d9761SEric Taylor { 65022ac5be4Sperrin zil_close(zv->zv_zilog); 65122ac5be4Sperrin zv->zv_zilog = NULL; 6522e2c1355SMatthew Ahrens 65394d1a210STim Haley dmu_buf_rele(zv->zv_dbuf, zvol_tag); 65494d1a210STim Haley zv->zv_dbuf = NULL; 6552e2c1355SMatthew Ahrens 6562e2c1355SMatthew Ahrens /* 6572e2c1355SMatthew Ahrens * Evict cached data 6582e2c1355SMatthew Ahrens */ 6592e2c1355SMatthew Ahrens if (dsl_dataset_is_dirty(dmu_objset_ds(zv->zv_objset)) && 6602e2c1355SMatthew Ahrens !(zv->zv_flags & ZVOL_RDONLY)) 6612e2c1355SMatthew Ahrens txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 6623b2aab18SMatthew Ahrens dmu_objset_evict_dbufs(zv->zv_objset); 6632e2c1355SMatthew Ahrens 664503ad85cSMatthew Ahrens dmu_objset_disown(zv->zv_objset, zvol_tag); 665fa9e4066Sahrens zv->zv_objset = NULL; 666fa9e4066Sahrens } 667fa9e4066Sahrens 668e7cbe64fSgw int 669e7cbe64fSgw zvol_prealloc(zvol_state_t *zv) 670e7cbe64fSgw { 671e7cbe64fSgw objset_t *os = zv->zv_objset; 672e7cbe64fSgw dmu_tx_t *tx; 673e7cbe64fSgw uint64_t refd, avail, usedobjs, availobjs; 674e7cbe64fSgw uint64_t resid = zv->zv_volsize; 675e7cbe64fSgw uint64_t off = 0; 676e7cbe64fSgw 677e7cbe64fSgw /* Check the space usage before attempting to allocate the space */ 678e7cbe64fSgw dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs); 679e7cbe64fSgw if (avail < zv->zv_volsize) 680be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 681e7cbe64fSgw 682e7cbe64fSgw /* Free old extents if they exist */ 683e7cbe64fSgw zvol_free_extents(zv); 684e7cbe64fSgw 685e7cbe64fSgw while (resid != 0) { 686e7cbe64fSgw int error; 687e7cbe64fSgw uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE); 688e7cbe64fSgw 689e7cbe64fSgw tx = dmu_tx_create(os); 690e7cbe64fSgw dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 691e7cbe64fSgw error = dmu_tx_assign(tx, TXG_WAIT); 692e7cbe64fSgw if (error) { 693e7cbe64fSgw dmu_tx_abort(tx); 694cdb0ab79Smaybee (void) dmu_free_long_range(os, ZVOL_OBJ, 0, off); 695e7cbe64fSgw return (error); 696e7cbe64fSgw } 69782c9918fSTim Haley dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx); 698e7cbe64fSgw dmu_tx_commit(tx); 699e7cbe64fSgw off += bytes; 700e7cbe64fSgw resid -= bytes; 701e7cbe64fSgw } 702e7cbe64fSgw txg_wait_synced(dmu_objset_pool(os), 0); 703e7cbe64fSgw 704e7cbe64fSgw return (0); 705e7cbe64fSgw } 706e7cbe64fSgw 7073b2aab18SMatthew Ahrens static int 708681d9761SEric Taylor zvol_update_volsize(objset_t *os, uint64_t volsize) 709e7cbe64fSgw { 710e7cbe64fSgw dmu_tx_t *tx; 711e7cbe64fSgw int error; 712e7cbe64fSgw 713c99e4bdcSChris Kirby ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 714e7cbe64fSgw 715681d9761SEric Taylor tx = dmu_tx_create(os); 716e7cbe64fSgw dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 717e7cbe64fSgw error = dmu_tx_assign(tx, TXG_WAIT); 718e7cbe64fSgw if (error) { 719e7cbe64fSgw dmu_tx_abort(tx); 720e7cbe64fSgw return (error); 721e7cbe64fSgw } 722e7cbe64fSgw 723681d9761SEric Taylor error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, 724e7cbe64fSgw &volsize, tx); 725e7cbe64fSgw dmu_tx_commit(tx); 726e7cbe64fSgw 727e7cbe64fSgw if (error == 0) 728681d9761SEric Taylor error = dmu_free_long_range(os, 729cdb0ab79Smaybee ZVOL_OBJ, volsize, DMU_OBJECT_END); 730681d9761SEric Taylor return (error); 731681d9761SEric Taylor } 732e7cbe64fSgw 733681d9761SEric Taylor void 734681d9761SEric Taylor zvol_remove_minors(const char *name) 735681d9761SEric Taylor { 736681d9761SEric Taylor zvol_state_t *zv; 737681d9761SEric Taylor char *namebuf; 738681d9761SEric Taylor minor_t minor; 739681d9761SEric Taylor 740681d9761SEric Taylor namebuf = kmem_zalloc(strlen(name) + 2, KM_SLEEP); 741681d9761SEric Taylor (void) strncpy(namebuf, name, strlen(name)); 742681d9761SEric Taylor (void) strcat(namebuf, "/"); 743c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 744c99e4bdcSChris Kirby for (minor = 1; minor <= ZFSDEV_MAX_MINOR; minor++) { 745681d9761SEric Taylor 746c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 747681d9761SEric Taylor if (zv == NULL) 748681d9761SEric Taylor continue; 749681d9761SEric Taylor if (strncmp(namebuf, zv->zv_name, strlen(namebuf)) == 0) 750681d9761SEric Taylor (void) zvol_remove_zv(zv); 751e7cbe64fSgw } 752681d9761SEric Taylor kmem_free(namebuf, strlen(name) + 2); 753681d9761SEric Taylor 754c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 755e7cbe64fSgw } 756e7cbe64fSgw 757c61ea566SGeorge Wilson static int 7583b2aab18SMatthew Ahrens zvol_update_live_volsize(zvol_state_t *zv, uint64_t volsize) 759fa9e4066Sahrens { 760e7cbe64fSgw uint64_t old_volsize = 0ULL; 7613b2aab18SMatthew Ahrens int error = 0; 762fa9e4066Sahrens 763c61ea566SGeorge Wilson ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 764c61ea566SGeorge Wilson 765e7cbe64fSgw /* 766e7cbe64fSgw * Reinitialize the dump area to the new size. If we 767681d9761SEric Taylor * failed to resize the dump area then restore it back to 768c61ea566SGeorge Wilson * its original size. We must set the new volsize prior 769c61ea566SGeorge Wilson * to calling dumpvp_resize() to ensure that the devices' 770c61ea566SGeorge Wilson * size(9P) is not visible by the dump subsystem. 771e7cbe64fSgw */ 7723b2aab18SMatthew Ahrens old_volsize = zv->zv_volsize; 7733b2aab18SMatthew Ahrens zvol_size_changed(zv, volsize); 7743b2aab18SMatthew Ahrens 7753b2aab18SMatthew Ahrens if (zv->zv_flags & ZVOL_DUMPIFIED) { 7763b2aab18SMatthew Ahrens if ((error = zvol_dumpify(zv)) != 0 || 7773b2aab18SMatthew Ahrens (error = dumpvp_resize()) != 0) { 7783b2aab18SMatthew Ahrens int dumpify_error; 7793b2aab18SMatthew Ahrens 7803b2aab18SMatthew Ahrens (void) zvol_update_volsize(zv->zv_objset, old_volsize); 7813b2aab18SMatthew Ahrens zvol_size_changed(zv, old_volsize); 7823b2aab18SMatthew Ahrens dumpify_error = zvol_dumpify(zv); 7833b2aab18SMatthew Ahrens error = dumpify_error ? dumpify_error : error; 784681d9761SEric Taylor } 785fa9e4066Sahrens } 786fa9e4066Sahrens 787573ca77eSGeorge Wilson /* 788573ca77eSGeorge Wilson * Generate a LUN expansion event. 789573ca77eSGeorge Wilson */ 7903b2aab18SMatthew Ahrens if (error == 0) { 791573ca77eSGeorge Wilson sysevent_id_t eid; 792573ca77eSGeorge Wilson nvlist_t *attr; 793573ca77eSGeorge Wilson char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 794573ca77eSGeorge Wilson 795681d9761SEric Taylor (void) snprintf(physpath, MAXPATHLEN, "%s%u", ZVOL_PSEUDO_DEV, 796573ca77eSGeorge Wilson zv->zv_minor); 797573ca77eSGeorge Wilson 798573ca77eSGeorge Wilson VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 799573ca77eSGeorge Wilson VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 800573ca77eSGeorge Wilson 801573ca77eSGeorge Wilson (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 802573ca77eSGeorge Wilson ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 803573ca77eSGeorge Wilson 804573ca77eSGeorge Wilson nvlist_free(attr); 805573ca77eSGeorge Wilson kmem_free(physpath, MAXPATHLEN); 806573ca77eSGeorge Wilson } 807c61ea566SGeorge Wilson return (error); 808c61ea566SGeorge Wilson } 809573ca77eSGeorge Wilson 810c61ea566SGeorge Wilson int 811c61ea566SGeorge Wilson zvol_set_volsize(const char *name, uint64_t volsize) 812c61ea566SGeorge Wilson { 813c61ea566SGeorge Wilson zvol_state_t *zv = NULL; 814c61ea566SGeorge Wilson objset_t *os; 815c61ea566SGeorge Wilson int error; 816c61ea566SGeorge Wilson dmu_object_info_t doi; 817c61ea566SGeorge Wilson uint64_t readonly; 8183b2aab18SMatthew Ahrens boolean_t owned = B_FALSE; 8193b2aab18SMatthew Ahrens 8203b2aab18SMatthew Ahrens error = dsl_prop_get_integer(name, 8213b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_READONLY), &readonly, NULL); 8223b2aab18SMatthew Ahrens if (error != 0) 8233b2aab18SMatthew Ahrens return (error); 8243b2aab18SMatthew Ahrens if (readonly) 825be6fd75aSMatthew Ahrens return (SET_ERROR(EROFS)); 826c61ea566SGeorge Wilson 827c61ea566SGeorge Wilson mutex_enter(&zfsdev_state_lock); 828c61ea566SGeorge Wilson zv = zvol_minor_lookup(name); 8293b2aab18SMatthew Ahrens 8303b2aab18SMatthew Ahrens if (zv == NULL || zv->zv_objset == NULL) { 8313b2aab18SMatthew Ahrens if ((error = dmu_objset_own(name, DMU_OST_ZVOL, B_FALSE, 8323b2aab18SMatthew Ahrens FTAG, &os)) != 0) { 8333b2aab18SMatthew Ahrens mutex_exit(&zfsdev_state_lock); 8343b2aab18SMatthew Ahrens return (error); 8353b2aab18SMatthew Ahrens } 8363b2aab18SMatthew Ahrens owned = B_TRUE; 8373b2aab18SMatthew Ahrens if (zv != NULL) 8383b2aab18SMatthew Ahrens zv->zv_objset = os; 8393b2aab18SMatthew Ahrens } else { 8403b2aab18SMatthew Ahrens os = zv->zv_objset; 841c61ea566SGeorge Wilson } 842c61ea566SGeorge Wilson 843c61ea566SGeorge Wilson if ((error = dmu_object_info(os, ZVOL_OBJ, &doi)) != 0 || 8443b2aab18SMatthew Ahrens (error = zvol_check_volsize(volsize, doi.doi_data_block_size)) != 0) 845c61ea566SGeorge Wilson goto out; 846c61ea566SGeorge Wilson 8473b2aab18SMatthew Ahrens error = zvol_update_volsize(os, volsize); 848c61ea566SGeorge Wilson 8493b2aab18SMatthew Ahrens if (error == 0 && zv != NULL) 8503b2aab18SMatthew Ahrens error = zvol_update_live_volsize(zv, volsize); 851bb0ade09Sahrens out: 8523b2aab18SMatthew Ahrens if (owned) { 8533b2aab18SMatthew Ahrens dmu_objset_disown(os, FTAG); 8543b2aab18SMatthew Ahrens if (zv != NULL) 8553b2aab18SMatthew Ahrens zv->zv_objset = NULL; 8563b2aab18SMatthew Ahrens } 857c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 858fa9e4066Sahrens return (error); 859fa9e4066Sahrens } 860fa9e4066Sahrens 861fa9e4066Sahrens /*ARGSUSED*/ 862fa9e4066Sahrens int 863fa9e4066Sahrens zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr) 864fa9e4066Sahrens { 865fa9e4066Sahrens zvol_state_t *zv; 866681d9761SEric Taylor int err = 0; 867fa9e4066Sahrens 868c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 869fa9e4066Sahrens 870c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(getminor(*devp), ZSST_ZVOL); 871fa9e4066Sahrens if (zv == NULL) { 872c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 873be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 874fa9e4066Sahrens } 875fa9e4066Sahrens 876681d9761SEric Taylor if (zv->zv_total_opens == 0) 877681d9761SEric Taylor err = zvol_first_open(zv); 878681d9761SEric Taylor if (err) { 879c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 880681d9761SEric Taylor return (err); 881681d9761SEric Taylor } 882681d9761SEric Taylor if ((flag & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) { 883be6fd75aSMatthew Ahrens err = SET_ERROR(EROFS); 884681d9761SEric Taylor goto out; 885fa9e4066Sahrens } 886c7f714e2SEric Taylor if (zv->zv_flags & ZVOL_EXCL) { 887be6fd75aSMatthew Ahrens err = SET_ERROR(EBUSY); 888681d9761SEric Taylor goto out; 889c7f714e2SEric Taylor } 890c7f714e2SEric Taylor if (flag & FEXCL) { 891c7f714e2SEric Taylor if (zv->zv_total_opens != 0) { 892be6fd75aSMatthew Ahrens err = SET_ERROR(EBUSY); 893681d9761SEric Taylor goto out; 894c7f714e2SEric Taylor } 895c7f714e2SEric Taylor zv->zv_flags |= ZVOL_EXCL; 896c7f714e2SEric Taylor } 897fa9e4066Sahrens 898fa9e4066Sahrens if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) { 899fa9e4066Sahrens zv->zv_open_count[otyp]++; 900fa9e4066Sahrens zv->zv_total_opens++; 901fa9e4066Sahrens } 902c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 903fa9e4066Sahrens 904681d9761SEric Taylor return (err); 905681d9761SEric Taylor out: 906681d9761SEric Taylor if (zv->zv_total_opens == 0) 907681d9761SEric Taylor zvol_last_close(zv); 908c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 909681d9761SEric Taylor return (err); 910fa9e4066Sahrens } 911fa9e4066Sahrens 912fa9e4066Sahrens /*ARGSUSED*/ 913fa9e4066Sahrens int 914fa9e4066Sahrens zvol_close(dev_t dev, int flag, int otyp, cred_t *cr) 915fa9e4066Sahrens { 916fa9e4066Sahrens minor_t minor = getminor(dev); 917fa9e4066Sahrens zvol_state_t *zv; 918681d9761SEric Taylor int error = 0; 919fa9e4066Sahrens 920c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 921fa9e4066Sahrens 922c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 923fa9e4066Sahrens if (zv == NULL) { 924c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 925be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 926fa9e4066Sahrens } 927fa9e4066Sahrens 928c7f714e2SEric Taylor if (zv->zv_flags & ZVOL_EXCL) { 929c7f714e2SEric Taylor ASSERT(zv->zv_total_opens == 1); 930c7f714e2SEric Taylor zv->zv_flags &= ~ZVOL_EXCL; 931fa9e4066Sahrens } 932fa9e4066Sahrens 933fa9e4066Sahrens /* 934fa9e4066Sahrens * If the open count is zero, this is a spurious close. 935fa9e4066Sahrens * That indicates a bug in the kernel / DDI framework. 936fa9e4066Sahrens */ 937fa9e4066Sahrens ASSERT(zv->zv_open_count[otyp] != 0); 938fa9e4066Sahrens ASSERT(zv->zv_total_opens != 0); 939fa9e4066Sahrens 940fa9e4066Sahrens /* 941fa9e4066Sahrens * You may get multiple opens, but only one close. 942fa9e4066Sahrens */ 943fa9e4066Sahrens zv->zv_open_count[otyp]--; 944fa9e4066Sahrens zv->zv_total_opens--; 945fa9e4066Sahrens 946681d9761SEric Taylor if (zv->zv_total_opens == 0) 947681d9761SEric Taylor zvol_last_close(zv); 948fa9e4066Sahrens 949c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 950681d9761SEric Taylor return (error); 951fa9e4066Sahrens } 952fa9e4066Sahrens 953feb08c6bSbillm static void 954b24ab676SJeff Bonwick zvol_get_done(zgd_t *zgd, int error) 95567bd71c6Sperrin { 956b24ab676SJeff Bonwick if (zgd->zgd_db) 957b24ab676SJeff Bonwick dmu_buf_rele(zgd->zgd_db, zgd); 958b24ab676SJeff Bonwick 959b24ab676SJeff Bonwick zfs_range_unlock(zgd->zgd_rl); 960b24ab676SJeff Bonwick 961b24ab676SJeff Bonwick if (error == 0 && zgd->zgd_bp) 962b24ab676SJeff Bonwick zil_add_block(zgd->zgd_zilog, zgd->zgd_bp); 96367bd71c6Sperrin 96467bd71c6Sperrin kmem_free(zgd, sizeof (zgd_t)); 96567bd71c6Sperrin } 96667bd71c6Sperrin 96767bd71c6Sperrin /* 96867bd71c6Sperrin * Get data to generate a TX_WRITE intent log record. 96967bd71c6Sperrin */ 970feb08c6bSbillm static int 97167bd71c6Sperrin zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 97267bd71c6Sperrin { 97367bd71c6Sperrin zvol_state_t *zv = arg; 97467bd71c6Sperrin objset_t *os = zv->zv_objset; 975b24ab676SJeff Bonwick uint64_t object = ZVOL_OBJ; 976b24ab676SJeff Bonwick uint64_t offset = lr->lr_offset; 977b24ab676SJeff Bonwick uint64_t size = lr->lr_length; /* length of user data */ 978b24ab676SJeff Bonwick blkptr_t *bp = &lr->lr_blkptr; 97967bd71c6Sperrin dmu_buf_t *db; 98067bd71c6Sperrin zgd_t *zgd; 98167bd71c6Sperrin int error; 98267bd71c6Sperrin 983b24ab676SJeff Bonwick ASSERT(zio != NULL); 984b24ab676SJeff Bonwick ASSERT(size != 0); 985b24ab676SJeff Bonwick 986b24ab676SJeff Bonwick zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP); 987b24ab676SJeff Bonwick zgd->zgd_zilog = zv->zv_zilog; 988b24ab676SJeff Bonwick zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER); 989feb08c6bSbillm 990c2e6a7d6Sperrin /* 991c2e6a7d6Sperrin * Write records come in two flavors: immediate and indirect. 992c2e6a7d6Sperrin * For small writes it's cheaper to store the data with the 993c2e6a7d6Sperrin * log record (immediate); for large writes it's cheaper to 994c2e6a7d6Sperrin * sync the data and get a pointer to it (indirect) so that 995c2e6a7d6Sperrin * we don't have to write the data twice. 996c2e6a7d6Sperrin */ 997b24ab676SJeff Bonwick if (buf != NULL) { /* immediate write */ 998b24ab676SJeff Bonwick error = dmu_read(os, object, offset, size, buf, 999b24ab676SJeff Bonwick DMU_READ_NO_PREFETCH); 1000b24ab676SJeff Bonwick } else { 1001b24ab676SJeff Bonwick size = zv->zv_volblocksize; 1002b24ab676SJeff Bonwick offset = P2ALIGN(offset, size); 100347cb52daSJeff Bonwick error = dmu_buf_hold(os, object, offset, zgd, &db, 100447cb52daSJeff Bonwick DMU_READ_NO_PREFETCH); 1005b24ab676SJeff Bonwick if (error == 0) { 100680901aeaSGeorge Wilson blkptr_t *obp = dmu_buf_get_blkptr(db); 100780901aeaSGeorge Wilson if (obp) { 100880901aeaSGeorge Wilson ASSERT(BP_IS_HOLE(bp)); 100980901aeaSGeorge Wilson *bp = *obp; 101080901aeaSGeorge Wilson } 101180901aeaSGeorge Wilson 1012b24ab676SJeff Bonwick zgd->zgd_db = db; 1013b24ab676SJeff Bonwick zgd->zgd_bp = bp; 101467bd71c6Sperrin 1015b24ab676SJeff Bonwick ASSERT(db->db_offset == offset); 1016b24ab676SJeff Bonwick ASSERT(db->db_size == size); 101767bd71c6Sperrin 1018b24ab676SJeff Bonwick error = dmu_sync(zio, lr->lr_common.lrc_txg, 1019b24ab676SJeff Bonwick zvol_get_done, zgd); 1020975c32a0SNeil Perrin 1021b24ab676SJeff Bonwick if (error == 0) 1022b24ab676SJeff Bonwick return (0); 1023b24ab676SJeff Bonwick } 1024975c32a0SNeil Perrin } 1025975c32a0SNeil Perrin 1026b24ab676SJeff Bonwick zvol_get_done(zgd, error); 1027b24ab676SJeff Bonwick 102867bd71c6Sperrin return (error); 102967bd71c6Sperrin } 103067bd71c6Sperrin 1031a24e15ceSperrin /* 1032a24e15ceSperrin * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions. 103322ac5be4Sperrin * 103422ac5be4Sperrin * We store data in the log buffers if it's small enough. 103567bd71c6Sperrin * Otherwise we will later flush the data out via dmu_sync(). 103622ac5be4Sperrin */ 103767bd71c6Sperrin ssize_t zvol_immediate_write_sz = 32768; 103822ac5be4Sperrin 1039feb08c6bSbillm static void 1040510b6c0eSNeil Perrin zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid, 1041510b6c0eSNeil Perrin boolean_t sync) 104222ac5be4Sperrin { 1043feb08c6bSbillm uint32_t blocksize = zv->zv_volblocksize; 10441209a471SNeil Perrin zilog_t *zilog = zv->zv_zilog; 1045510b6c0eSNeil Perrin boolean_t slogging; 1046e09fa4daSNeil Perrin ssize_t immediate_write_sz; 1047510b6c0eSNeil Perrin 1048b24ab676SJeff Bonwick if (zil_replaying(zilog, tx)) 10491209a471SNeil Perrin return; 10501209a471SNeil Perrin 1051e09fa4daSNeil Perrin immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT) 1052e09fa4daSNeil Perrin ? 0 : zvol_immediate_write_sz; 1053e09fa4daSNeil Perrin 1054e09fa4daSNeil Perrin slogging = spa_has_slogs(zilog->zl_spa) && 1055e09fa4daSNeil Perrin (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY); 1056feb08c6bSbillm 1057510b6c0eSNeil Perrin while (resid) { 1058510b6c0eSNeil Perrin itx_t *itx; 1059510b6c0eSNeil Perrin lr_write_t *lr; 1060510b6c0eSNeil Perrin ssize_t len; 1061510b6c0eSNeil Perrin itx_wr_state_t write_state; 1062510b6c0eSNeil Perrin 1063510b6c0eSNeil Perrin /* 1064510b6c0eSNeil Perrin * Unlike zfs_log_write() we can be called with 1065510b6c0eSNeil Perrin * upto DMU_MAX_ACCESS/2 (5MB) writes. 1066510b6c0eSNeil Perrin */ 1067e09fa4daSNeil Perrin if (blocksize > immediate_write_sz && !slogging && 1068510b6c0eSNeil Perrin resid >= blocksize && off % blocksize == 0) { 1069510b6c0eSNeil Perrin write_state = WR_INDIRECT; /* uses dmu_sync */ 1070510b6c0eSNeil Perrin len = blocksize; 1071510b6c0eSNeil Perrin } else if (sync) { 1072510b6c0eSNeil Perrin write_state = WR_COPIED; 1073510b6c0eSNeil Perrin len = MIN(ZIL_MAX_LOG_DATA, resid); 1074510b6c0eSNeil Perrin } else { 1075510b6c0eSNeil Perrin write_state = WR_NEED_COPY; 1076510b6c0eSNeil Perrin len = MIN(ZIL_MAX_LOG_DATA, resid); 1077510b6c0eSNeil Perrin } 1078510b6c0eSNeil Perrin 1079510b6c0eSNeil Perrin itx = zil_itx_create(TX_WRITE, sizeof (*lr) + 1080510b6c0eSNeil Perrin (write_state == WR_COPIED ? len : 0)); 1081feb08c6bSbillm lr = (lr_write_t *)&itx->itx_lr; 1082510b6c0eSNeil Perrin if (write_state == WR_COPIED && dmu_read(zv->zv_objset, 10837bfdf011SNeil Perrin ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) { 1084b24ab676SJeff Bonwick zil_itx_destroy(itx); 1085510b6c0eSNeil Perrin itx = zil_itx_create(TX_WRITE, sizeof (*lr)); 1086510b6c0eSNeil Perrin lr = (lr_write_t *)&itx->itx_lr; 1087510b6c0eSNeil Perrin write_state = WR_NEED_COPY; 1088510b6c0eSNeil Perrin } 1089510b6c0eSNeil Perrin 1090510b6c0eSNeil Perrin itx->itx_wr_state = write_state; 1091510b6c0eSNeil Perrin if (write_state == WR_NEED_COPY) 1092510b6c0eSNeil Perrin itx->itx_sod += len; 1093feb08c6bSbillm lr->lr_foid = ZVOL_OBJ; 1094feb08c6bSbillm lr->lr_offset = off; 1095510b6c0eSNeil Perrin lr->lr_length = len; 1096b24ab676SJeff Bonwick lr->lr_blkoff = 0; 1097feb08c6bSbillm BP_ZERO(&lr->lr_blkptr); 1098feb08c6bSbillm 1099510b6c0eSNeil Perrin itx->itx_private = zv; 1100510b6c0eSNeil Perrin itx->itx_sync = sync; 1101510b6c0eSNeil Perrin 11025002558fSNeil Perrin zil_itx_assign(zilog, itx, tx); 1103510b6c0eSNeil Perrin 1104510b6c0eSNeil Perrin off += len; 1105510b6c0eSNeil Perrin resid -= len; 110622ac5be4Sperrin } 110722ac5be4Sperrin } 110822ac5be4Sperrin 110988b7b0f2SMatthew Ahrens static int 1110*810e43b2SBill Pijewski zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t origoffset, 1111*810e43b2SBill Pijewski uint64_t size, boolean_t doread, boolean_t isdump) 1112e7cbe64fSgw { 1113e7cbe64fSgw vdev_disk_t *dvd; 1114e7cbe64fSgw int c; 1115e7cbe64fSgw int numerrors = 0; 1116e7cbe64fSgw 1117*810e43b2SBill Pijewski if (vd->vdev_ops == &vdev_mirror_ops || 1118*810e43b2SBill Pijewski vd->vdev_ops == &vdev_replacing_ops || 1119*810e43b2SBill Pijewski vd->vdev_ops == &vdev_spare_ops) { 1120*810e43b2SBill Pijewski for (c = 0; c < vd->vdev_children; c++) { 1121*810e43b2SBill Pijewski int err = zvol_dumpio_vdev(vd->vdev_child[c], 1122*810e43b2SBill Pijewski addr, offset, origoffset, size, doread, isdump); 1123*810e43b2SBill Pijewski if (err != 0) { 1124*810e43b2SBill Pijewski numerrors++; 1125*810e43b2SBill Pijewski } else if (doread) { 1126*810e43b2SBill Pijewski break; 1127*810e43b2SBill Pijewski } 1128e7cbe64fSgw } 1129e7cbe64fSgw } 1130e7cbe64fSgw 1131*810e43b2SBill Pijewski if (!vd->vdev_ops->vdev_op_leaf && vd->vdev_ops != &vdev_raidz_ops) 1132e7cbe64fSgw return (numerrors < vd->vdev_children ? 0 : EIO); 1133e7cbe64fSgw 1134dc0bb255SEric Taylor if (doread && !vdev_readable(vd)) 1135be6fd75aSMatthew Ahrens return (SET_ERROR(EIO)); 1136dc0bb255SEric Taylor else if (!doread && !vdev_writeable(vd)) 1137be6fd75aSMatthew Ahrens return (SET_ERROR(EIO)); 1138e7cbe64fSgw 1139*810e43b2SBill Pijewski if (vd->vdev_ops == &vdev_raidz_ops) { 1140*810e43b2SBill Pijewski return (vdev_raidz_physio(vd, 1141*810e43b2SBill Pijewski addr, size, offset, origoffset, doread, isdump)); 1142*810e43b2SBill Pijewski } 1143*810e43b2SBill Pijewski 1144e7cbe64fSgw offset += VDEV_LABEL_START_SIZE; 1145e7cbe64fSgw 1146e7cbe64fSgw if (ddi_in_panic() || isdump) { 114788b7b0f2SMatthew Ahrens ASSERT(!doread); 114888b7b0f2SMatthew Ahrens if (doread) 1149be6fd75aSMatthew Ahrens return (SET_ERROR(EIO)); 1150*810e43b2SBill Pijewski dvd = vd->vdev_tsd; 1151*810e43b2SBill Pijewski ASSERT3P(dvd, !=, NULL); 1152e7cbe64fSgw return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset), 1153e7cbe64fSgw lbtodb(size))); 1154e7cbe64fSgw } else { 1155*810e43b2SBill Pijewski dvd = vd->vdev_tsd; 1156*810e43b2SBill Pijewski ASSERT3P(dvd, !=, NULL); 1157*810e43b2SBill Pijewski return (vdev_disk_ldi_physio(dvd->vd_lh, addr, size, 1158*810e43b2SBill Pijewski offset, doread ? B_READ : B_WRITE)); 1159e7cbe64fSgw } 1160e7cbe64fSgw } 1161e7cbe64fSgw 116288b7b0f2SMatthew Ahrens static int 116388b7b0f2SMatthew Ahrens zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size, 116488b7b0f2SMatthew Ahrens boolean_t doread, boolean_t isdump) 1165e7cbe64fSgw { 1166e7cbe64fSgw vdev_t *vd; 1167e7cbe64fSgw int error; 116888b7b0f2SMatthew Ahrens zvol_extent_t *ze; 1169e7cbe64fSgw spa_t *spa = dmu_objset_spa(zv->zv_objset); 1170e7cbe64fSgw 117188b7b0f2SMatthew Ahrens /* Must be sector aligned, and not stradle a block boundary. */ 117288b7b0f2SMatthew Ahrens if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) || 117388b7b0f2SMatthew Ahrens P2BOUNDARY(offset, size, zv->zv_volblocksize)) { 1174be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 117588b7b0f2SMatthew Ahrens } 117688b7b0f2SMatthew Ahrens ASSERT(size <= zv->zv_volblocksize); 1177e7cbe64fSgw 117888b7b0f2SMatthew Ahrens /* Locate the extent this belongs to */ 117988b7b0f2SMatthew Ahrens ze = list_head(&zv->zv_extents); 118088b7b0f2SMatthew Ahrens while (offset >= ze->ze_nblks * zv->zv_volblocksize) { 118188b7b0f2SMatthew Ahrens offset -= ze->ze_nblks * zv->zv_volblocksize; 118288b7b0f2SMatthew Ahrens ze = list_next(&zv->zv_extents, ze); 118388b7b0f2SMatthew Ahrens } 118424cc0e1cSGeorge Wilson 11853b2aab18SMatthew Ahrens if (ze == NULL) 1186be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 11873b2aab18SMatthew Ahrens 118824cc0e1cSGeorge Wilson if (!ddi_in_panic()) 118924cc0e1cSGeorge Wilson spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 119024cc0e1cSGeorge Wilson 119188b7b0f2SMatthew Ahrens vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva)); 119288b7b0f2SMatthew Ahrens offset += DVA_GET_OFFSET(&ze->ze_dva); 1193*810e43b2SBill Pijewski error = zvol_dumpio_vdev(vd, addr, offset, DVA_GET_OFFSET(&ze->ze_dva), 1194*810e43b2SBill Pijewski size, doread, isdump); 119524cc0e1cSGeorge Wilson 119624cc0e1cSGeorge Wilson if (!ddi_in_panic()) 119724cc0e1cSGeorge Wilson spa_config_exit(spa, SCL_STATE, FTAG); 119824cc0e1cSGeorge Wilson 1199e7cbe64fSgw return (error); 1200e7cbe64fSgw } 1201e7cbe64fSgw 1202fa9e4066Sahrens int 1203fa9e4066Sahrens zvol_strategy(buf_t *bp) 1204fa9e4066Sahrens { 1205c99e4bdcSChris Kirby zfs_soft_state_t *zs = NULL; 1206c99e4bdcSChris Kirby zvol_state_t *zv; 1207fa9e4066Sahrens uint64_t off, volsize; 120888b7b0f2SMatthew Ahrens size_t resid; 1209fa9e4066Sahrens char *addr; 121022ac5be4Sperrin objset_t *os; 1211c2e6a7d6Sperrin rl_t *rl; 1212fa9e4066Sahrens int error = 0; 121388b7b0f2SMatthew Ahrens boolean_t doread = bp->b_flags & B_READ; 1214*810e43b2SBill Pijewski boolean_t is_dumpified; 1215510b6c0eSNeil Perrin boolean_t sync; 1216fa9e4066Sahrens 1217c99e4bdcSChris Kirby if (getminor(bp->b_edev) == 0) { 1218be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 1219c99e4bdcSChris Kirby } else { 1220c99e4bdcSChris Kirby zs = ddi_get_soft_state(zfsdev_state, getminor(bp->b_edev)); 1221c99e4bdcSChris Kirby if (zs == NULL) 1222be6fd75aSMatthew Ahrens error = SET_ERROR(ENXIO); 1223c99e4bdcSChris Kirby else if (zs->zss_type != ZSST_ZVOL) 1224be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 1225fa9e4066Sahrens } 1226fa9e4066Sahrens 1227c99e4bdcSChris Kirby if (error) { 1228c99e4bdcSChris Kirby bioerror(bp, error); 1229fa9e4066Sahrens biodone(bp); 1230fa9e4066Sahrens return (0); 1231fa9e4066Sahrens } 1232fa9e4066Sahrens 1233c99e4bdcSChris Kirby zv = zs->zss_data; 1234c99e4bdcSChris Kirby 1235681d9761SEric Taylor if (!(bp->b_flags & B_READ) && (zv->zv_flags & ZVOL_RDONLY)) { 1236fa9e4066Sahrens bioerror(bp, EROFS); 1237fa9e4066Sahrens biodone(bp); 1238fa9e4066Sahrens return (0); 1239fa9e4066Sahrens } 1240fa9e4066Sahrens 1241fa9e4066Sahrens off = ldbtob(bp->b_blkno); 1242fa9e4066Sahrens volsize = zv->zv_volsize; 1243fa9e4066Sahrens 124422ac5be4Sperrin os = zv->zv_objset; 124522ac5be4Sperrin ASSERT(os != NULL); 1246fa9e4066Sahrens 1247fa9e4066Sahrens bp_mapin(bp); 1248fa9e4066Sahrens addr = bp->b_un.b_addr; 1249fa9e4066Sahrens resid = bp->b_bcount; 1250fa9e4066Sahrens 125188b7b0f2SMatthew Ahrens if (resid > 0 && (off < 0 || off >= volsize)) { 125288b7b0f2SMatthew Ahrens bioerror(bp, EIO); 125388b7b0f2SMatthew Ahrens biodone(bp); 125488b7b0f2SMatthew Ahrens return (0); 125588b7b0f2SMatthew Ahrens } 125673ec3d9cSgw 1257*810e43b2SBill Pijewski is_dumpified = zv->zv_flags & ZVOL_DUMPIFIED; 125855da60b9SMark J Musante sync = ((!(bp->b_flags & B_ASYNC) && 125955da60b9SMark J Musante !(zv->zv_flags & ZVOL_WCE)) || 126055da60b9SMark J Musante (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)) && 1261*810e43b2SBill Pijewski !doread && !is_dumpified; 1262510b6c0eSNeil Perrin 1263a24e15ceSperrin /* 1264a24e15ceSperrin * There must be no buffer changes when doing a dmu_sync() because 1265a24e15ceSperrin * we can't change the data whilst calculating the checksum. 1266a24e15ceSperrin */ 1267c2e6a7d6Sperrin rl = zfs_range_lock(&zv->zv_znode, off, resid, 126888b7b0f2SMatthew Ahrens doread ? RL_READER : RL_WRITER); 1269fa9e4066Sahrens 1270e7cbe64fSgw while (resid != 0 && off < volsize) { 127188b7b0f2SMatthew Ahrens size_t size = MIN(resid, zvol_maxphys); 1272*810e43b2SBill Pijewski if (is_dumpified) { 1273e7cbe64fSgw size = MIN(size, P2END(off, zv->zv_volblocksize) - off); 127488b7b0f2SMatthew Ahrens error = zvol_dumpio(zv, addr, off, size, 127588b7b0f2SMatthew Ahrens doread, B_FALSE); 127688b7b0f2SMatthew Ahrens } else if (doread) { 12777bfdf011SNeil Perrin error = dmu_read(os, ZVOL_OBJ, off, size, addr, 12787bfdf011SNeil Perrin DMU_READ_PREFETCH); 1279fa9e4066Sahrens } else { 128022ac5be4Sperrin dmu_tx_t *tx = dmu_tx_create(os); 1281fa9e4066Sahrens dmu_tx_hold_write(tx, ZVOL_OBJ, off, size); 1282fa9e4066Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 1283fa9e4066Sahrens if (error) { 1284fa9e4066Sahrens dmu_tx_abort(tx); 1285fa9e4066Sahrens } else { 128622ac5be4Sperrin dmu_write(os, ZVOL_OBJ, off, size, addr, tx); 1287510b6c0eSNeil Perrin zvol_log_write(zv, tx, off, size, sync); 1288fa9e4066Sahrens dmu_tx_commit(tx); 1289fa9e4066Sahrens } 1290fa9e4066Sahrens } 1291b87f3af3Sperrin if (error) { 1292b87f3af3Sperrin /* convert checksum errors into IO errors */ 1293b87f3af3Sperrin if (error == ECKSUM) 1294be6fd75aSMatthew Ahrens error = SET_ERROR(EIO); 1295fa9e4066Sahrens break; 1296b87f3af3Sperrin } 1297fa9e4066Sahrens off += size; 1298fa9e4066Sahrens addr += size; 1299fa9e4066Sahrens resid -= size; 1300fa9e4066Sahrens } 1301c2e6a7d6Sperrin zfs_range_unlock(rl); 1302fa9e4066Sahrens 1303fa9e4066Sahrens if ((bp->b_resid = resid) == bp->b_bcount) 1304fa9e4066Sahrens bioerror(bp, off > volsize ? EINVAL : error); 1305fa9e4066Sahrens 1306510b6c0eSNeil Perrin if (sync) 13075002558fSNeil Perrin zil_commit(zv->zv_zilog, ZVOL_OBJ); 1308feb08c6bSbillm biodone(bp); 130922ac5be4Sperrin 1310fa9e4066Sahrens return (0); 1311fa9e4066Sahrens } 1312fa9e4066Sahrens 131367bd71c6Sperrin /* 131467bd71c6Sperrin * Set the buffer count to the zvol maximum transfer. 131567bd71c6Sperrin * Using our own routine instead of the default minphys() 131667bd71c6Sperrin * means that for larger writes we write bigger buffers on X86 131767bd71c6Sperrin * (128K instead of 56K) and flush the disk write cache less often 131867bd71c6Sperrin * (every zvol_maxphys - currently 1MB) instead of minphys (currently 131967bd71c6Sperrin * 56K on X86 and 128K on sparc). 132067bd71c6Sperrin */ 132167bd71c6Sperrin void 132267bd71c6Sperrin zvol_minphys(struct buf *bp) 132367bd71c6Sperrin { 132467bd71c6Sperrin if (bp->b_bcount > zvol_maxphys) 132567bd71c6Sperrin bp->b_bcount = zvol_maxphys; 132667bd71c6Sperrin } 132767bd71c6Sperrin 1328e7cbe64fSgw int 1329e7cbe64fSgw zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks) 1330e7cbe64fSgw { 1331e7cbe64fSgw minor_t minor = getminor(dev); 1332e7cbe64fSgw zvol_state_t *zv; 1333e7cbe64fSgw int error = 0; 1334e7cbe64fSgw uint64_t size; 1335e7cbe64fSgw uint64_t boff; 1336e7cbe64fSgw uint64_t resid; 1337e7cbe64fSgw 1338c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1339e7cbe64fSgw if (zv == NULL) 1340be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 1341e7cbe64fSgw 13423b2aab18SMatthew Ahrens if ((zv->zv_flags & ZVOL_DUMPIFIED) == 0) 1343be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 13443b2aab18SMatthew Ahrens 1345e7cbe64fSgw boff = ldbtob(blkno); 1346e7cbe64fSgw resid = ldbtob(nblocks); 134788b7b0f2SMatthew Ahrens 134888b7b0f2SMatthew Ahrens VERIFY3U(boff + resid, <=, zv->zv_volsize); 134988b7b0f2SMatthew Ahrens 1350e7cbe64fSgw while (resid) { 1351e7cbe64fSgw size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff); 135288b7b0f2SMatthew Ahrens error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE); 1353e7cbe64fSgw if (error) 1354e7cbe64fSgw break; 1355e7cbe64fSgw boff += size; 1356e7cbe64fSgw addr += size; 1357e7cbe64fSgw resid -= size; 1358e7cbe64fSgw } 1359e7cbe64fSgw 1360e7cbe64fSgw return (error); 1361e7cbe64fSgw } 1362e7cbe64fSgw 1363fa9e4066Sahrens /*ARGSUSED*/ 1364fa9e4066Sahrens int 1365feb08c6bSbillm zvol_read(dev_t dev, uio_t *uio, cred_t *cr) 1366fa9e4066Sahrens { 1367c7ca1008Sgw minor_t minor = getminor(dev); 1368c7ca1008Sgw zvol_state_t *zv; 136973ec3d9cSgw uint64_t volsize; 1370c2e6a7d6Sperrin rl_t *rl; 1371feb08c6bSbillm int error = 0; 1372fa9e4066Sahrens 1373c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1374c7ca1008Sgw if (zv == NULL) 1375be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 1376c7ca1008Sgw 137773ec3d9cSgw volsize = zv->zv_volsize; 137873ec3d9cSgw if (uio->uio_resid > 0 && 137973ec3d9cSgw (uio->uio_loffset < 0 || uio->uio_loffset >= volsize)) 1380be6fd75aSMatthew Ahrens return (SET_ERROR(EIO)); 138173ec3d9cSgw 138288b7b0f2SMatthew Ahrens if (zv->zv_flags & ZVOL_DUMPIFIED) { 138388b7b0f2SMatthew Ahrens error = physio(zvol_strategy, NULL, dev, B_READ, 138488b7b0f2SMatthew Ahrens zvol_minphys, uio); 138588b7b0f2SMatthew Ahrens return (error); 138688b7b0f2SMatthew Ahrens } 138788b7b0f2SMatthew Ahrens 1388c2e6a7d6Sperrin rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 1389c2e6a7d6Sperrin RL_READER); 139073ec3d9cSgw while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 1391feb08c6bSbillm uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 1392fa9e4066Sahrens 139373ec3d9cSgw /* don't read past the end */ 139473ec3d9cSgw if (bytes > volsize - uio->uio_loffset) 139573ec3d9cSgw bytes = volsize - uio->uio_loffset; 139673ec3d9cSgw 1397feb08c6bSbillm error = dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes); 1398b87f3af3Sperrin if (error) { 1399b87f3af3Sperrin /* convert checksum errors into IO errors */ 1400b87f3af3Sperrin if (error == ECKSUM) 1401be6fd75aSMatthew Ahrens error = SET_ERROR(EIO); 1402feb08c6bSbillm break; 1403b87f3af3Sperrin } 1404feb08c6bSbillm } 1405c2e6a7d6Sperrin zfs_range_unlock(rl); 1406feb08c6bSbillm return (error); 1407fa9e4066Sahrens } 1408fa9e4066Sahrens 1409fa9e4066Sahrens /*ARGSUSED*/ 1410fa9e4066Sahrens int 1411feb08c6bSbillm zvol_write(dev_t dev, uio_t *uio, cred_t *cr) 1412fa9e4066Sahrens { 1413c7ca1008Sgw minor_t minor = getminor(dev); 1414c7ca1008Sgw zvol_state_t *zv; 141573ec3d9cSgw uint64_t volsize; 1416c2e6a7d6Sperrin rl_t *rl; 1417feb08c6bSbillm int error = 0; 1418510b6c0eSNeil Perrin boolean_t sync; 1419feb08c6bSbillm 1420c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1421c7ca1008Sgw if (zv == NULL) 1422be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 1423c7ca1008Sgw 142473ec3d9cSgw volsize = zv->zv_volsize; 142573ec3d9cSgw if (uio->uio_resid > 0 && 142673ec3d9cSgw (uio->uio_loffset < 0 || uio->uio_loffset >= volsize)) 1427be6fd75aSMatthew Ahrens return (SET_ERROR(EIO)); 142873ec3d9cSgw 1429e7cbe64fSgw if (zv->zv_flags & ZVOL_DUMPIFIED) { 1430e7cbe64fSgw error = physio(zvol_strategy, NULL, dev, B_WRITE, 1431e7cbe64fSgw zvol_minphys, uio); 1432e7cbe64fSgw return (error); 1433e7cbe64fSgw } 1434e7cbe64fSgw 143555da60b9SMark J Musante sync = !(zv->zv_flags & ZVOL_WCE) || 143655da60b9SMark J Musante (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS); 1437510b6c0eSNeil Perrin 1438c2e6a7d6Sperrin rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 1439c2e6a7d6Sperrin RL_WRITER); 144073ec3d9cSgw while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 1441feb08c6bSbillm uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 1442feb08c6bSbillm uint64_t off = uio->uio_loffset; 1443feb08c6bSbillm dmu_tx_t *tx = dmu_tx_create(zv->zv_objset); 144473ec3d9cSgw 144573ec3d9cSgw if (bytes > volsize - off) /* don't write past the end */ 144673ec3d9cSgw bytes = volsize - off; 144773ec3d9cSgw 1448feb08c6bSbillm dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 1449feb08c6bSbillm error = dmu_tx_assign(tx, TXG_WAIT); 1450feb08c6bSbillm if (error) { 1451feb08c6bSbillm dmu_tx_abort(tx); 1452feb08c6bSbillm break; 1453feb08c6bSbillm } 145494d1a210STim Haley error = dmu_write_uio_dbuf(zv->zv_dbuf, uio, bytes, tx); 1455feb08c6bSbillm if (error == 0) 1456510b6c0eSNeil Perrin zvol_log_write(zv, tx, off, bytes, sync); 1457feb08c6bSbillm dmu_tx_commit(tx); 1458feb08c6bSbillm 1459feb08c6bSbillm if (error) 1460feb08c6bSbillm break; 1461feb08c6bSbillm } 1462c2e6a7d6Sperrin zfs_range_unlock(rl); 1463510b6c0eSNeil Perrin if (sync) 14645002558fSNeil Perrin zil_commit(zv->zv_zilog, ZVOL_OBJ); 1465feb08c6bSbillm return (error); 1466fa9e4066Sahrens } 1467fa9e4066Sahrens 1468c7f714e2SEric Taylor int 1469c7f714e2SEric Taylor zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs) 1470c7f714e2SEric Taylor { 1471c7f714e2SEric Taylor struct uuid uuid = EFI_RESERVED; 1472c7f714e2SEric Taylor efi_gpe_t gpe = { 0 }; 1473c7f714e2SEric Taylor uint32_t crc; 1474c7f714e2SEric Taylor dk_efi_t efi; 1475c7f714e2SEric Taylor int length; 1476c7f714e2SEric Taylor char *ptr; 1477c7f714e2SEric Taylor 1478c7f714e2SEric Taylor if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag)) 1479be6fd75aSMatthew Ahrens return (SET_ERROR(EFAULT)); 1480c7f714e2SEric Taylor ptr = (char *)(uintptr_t)efi.dki_data_64; 1481c7f714e2SEric Taylor length = efi.dki_length; 1482c7f714e2SEric Taylor /* 1483c7f714e2SEric Taylor * Some clients may attempt to request a PMBR for the 1484c7f714e2SEric Taylor * zvol. Currently this interface will return EINVAL to 1485c7f714e2SEric Taylor * such requests. These requests could be supported by 1486c7f714e2SEric Taylor * adding a check for lba == 0 and consing up an appropriate 1487c7f714e2SEric Taylor * PMBR. 1488c7f714e2SEric Taylor */ 1489c7f714e2SEric Taylor if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0) 1490be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 1491c7f714e2SEric Taylor 1492c7f714e2SEric Taylor gpe.efi_gpe_StartingLBA = LE_64(34ULL); 1493c7f714e2SEric Taylor gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1); 1494c7f714e2SEric Taylor UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid); 1495c7f714e2SEric Taylor 1496c7f714e2SEric Taylor if (efi.dki_lba == 1) { 1497c7f714e2SEric Taylor efi_gpt_t gpt = { 0 }; 1498c7f714e2SEric Taylor 1499c7f714e2SEric Taylor gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE); 1500c7f714e2SEric Taylor gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT); 1501c7f714e2SEric Taylor gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt)); 1502c7f714e2SEric Taylor gpt.efi_gpt_MyLBA = LE_64(1ULL); 1503c7f714e2SEric Taylor gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL); 1504c7f714e2SEric Taylor gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1); 1505c7f714e2SEric Taylor gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL); 1506c7f714e2SEric Taylor gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1); 1507c7f714e2SEric Taylor gpt.efi_gpt_SizeOfPartitionEntry = 1508c7f714e2SEric Taylor LE_32(sizeof (efi_gpe_t)); 1509c7f714e2SEric Taylor CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table); 1510c7f714e2SEric Taylor gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc); 1511c7f714e2SEric Taylor CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table); 1512c7f714e2SEric Taylor gpt.efi_gpt_HeaderCRC32 = LE_32(~crc); 1513c7f714e2SEric Taylor if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length), 1514c7f714e2SEric Taylor flag)) 1515be6fd75aSMatthew Ahrens return (SET_ERROR(EFAULT)); 1516c7f714e2SEric Taylor ptr += sizeof (gpt); 1517c7f714e2SEric Taylor length -= sizeof (gpt); 1518c7f714e2SEric Taylor } 1519c7f714e2SEric Taylor if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe), 1520c7f714e2SEric Taylor length), flag)) 1521be6fd75aSMatthew Ahrens return (SET_ERROR(EFAULT)); 1522c7f714e2SEric Taylor return (0); 1523c7f714e2SEric Taylor } 1524c7f714e2SEric Taylor 15253fb517f7SJames Moore /* 15263fb517f7SJames Moore * BEGIN entry points to allow external callers access to the volume. 15273fb517f7SJames Moore */ 15283fb517f7SJames Moore /* 15293fb517f7SJames Moore * Return the volume parameters needed for access from an external caller. 15303fb517f7SJames Moore * These values are invariant as long as the volume is held open. 15313fb517f7SJames Moore */ 15323fb517f7SJames Moore int 15333fb517f7SJames Moore zvol_get_volume_params(minor_t minor, uint64_t *blksize, 15343fb517f7SJames Moore uint64_t *max_xfer_len, void **minor_hdl, void **objset_hdl, void **zil_hdl, 15353fb517f7SJames Moore void **rl_hdl, void **bonus_hdl) 15363fb517f7SJames Moore { 15373fb517f7SJames Moore zvol_state_t *zv; 15383fb517f7SJames Moore 1539c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1540c99e4bdcSChris Kirby if (zv == NULL) 1541be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 15423fb517f7SJames Moore if (zv->zv_flags & ZVOL_DUMPIFIED) 1543be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 15443fb517f7SJames Moore 15453fb517f7SJames Moore ASSERT(blksize && max_xfer_len && minor_hdl && 15463fb517f7SJames Moore objset_hdl && zil_hdl && rl_hdl && bonus_hdl); 15473fb517f7SJames Moore 15483fb517f7SJames Moore *blksize = zv->zv_volblocksize; 15493fb517f7SJames Moore *max_xfer_len = (uint64_t)zvol_maxphys; 15503fb517f7SJames Moore *minor_hdl = zv; 15513fb517f7SJames Moore *objset_hdl = zv->zv_objset; 15523fb517f7SJames Moore *zil_hdl = zv->zv_zilog; 15533fb517f7SJames Moore *rl_hdl = &zv->zv_znode; 15543fb517f7SJames Moore *bonus_hdl = zv->zv_dbuf; 15553fb517f7SJames Moore return (0); 15563fb517f7SJames Moore } 15573fb517f7SJames Moore 15583fb517f7SJames Moore /* 15593fb517f7SJames Moore * Return the current volume size to an external caller. 15603fb517f7SJames Moore * The size can change while the volume is open. 15613fb517f7SJames Moore */ 15623fb517f7SJames Moore uint64_t 15633fb517f7SJames Moore zvol_get_volume_size(void *minor_hdl) 15643fb517f7SJames Moore { 15653fb517f7SJames Moore zvol_state_t *zv = minor_hdl; 15663fb517f7SJames Moore 15673fb517f7SJames Moore return (zv->zv_volsize); 15683fb517f7SJames Moore } 15693fb517f7SJames Moore 15703fb517f7SJames Moore /* 15713fb517f7SJames Moore * Return the current WCE setting to an external caller. 15723fb517f7SJames Moore * The WCE setting can change while the volume is open. 15733fb517f7SJames Moore */ 15743fb517f7SJames Moore int 15753fb517f7SJames Moore zvol_get_volume_wce(void *minor_hdl) 15763fb517f7SJames Moore { 15773fb517f7SJames Moore zvol_state_t *zv = minor_hdl; 15783fb517f7SJames Moore 15793fb517f7SJames Moore return ((zv->zv_flags & ZVOL_WCE) ? 1 : 0); 15803fb517f7SJames Moore } 15813fb517f7SJames Moore 15823fb517f7SJames Moore /* 15833fb517f7SJames Moore * Entry point for external callers to zvol_log_write 15843fb517f7SJames Moore */ 15853fb517f7SJames Moore void 15863fb517f7SJames Moore zvol_log_write_minor(void *minor_hdl, dmu_tx_t *tx, offset_t off, ssize_t resid, 15873fb517f7SJames Moore boolean_t sync) 15883fb517f7SJames Moore { 15893fb517f7SJames Moore zvol_state_t *zv = minor_hdl; 15903fb517f7SJames Moore 15913fb517f7SJames Moore zvol_log_write(zv, tx, off, resid, sync); 15923fb517f7SJames Moore } 15933fb517f7SJames Moore /* 15943fb517f7SJames Moore * END entry points to allow external callers access to the volume. 15953fb517f7SJames Moore */ 15963fb517f7SJames Moore 1597b77b9231SDan McDonald /* 1598b77b9231SDan McDonald * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE. 1599b77b9231SDan McDonald */ 1600b77b9231SDan McDonald static void 1601b77b9231SDan McDonald zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off, uint64_t len, 1602b77b9231SDan McDonald boolean_t sync) 1603b77b9231SDan McDonald { 1604b77b9231SDan McDonald itx_t *itx; 1605b77b9231SDan McDonald lr_truncate_t *lr; 1606b77b9231SDan McDonald zilog_t *zilog = zv->zv_zilog; 1607b77b9231SDan McDonald 1608b77b9231SDan McDonald if (zil_replaying(zilog, tx)) 1609b77b9231SDan McDonald return; 1610b77b9231SDan McDonald 1611b77b9231SDan McDonald itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr)); 1612b77b9231SDan McDonald lr = (lr_truncate_t *)&itx->itx_lr; 1613b77b9231SDan McDonald lr->lr_foid = ZVOL_OBJ; 1614b77b9231SDan McDonald lr->lr_offset = off; 1615b77b9231SDan McDonald lr->lr_length = len; 1616b77b9231SDan McDonald 1617b77b9231SDan McDonald itx->itx_sync = sync; 1618b77b9231SDan McDonald zil_itx_assign(zilog, itx, tx); 1619b77b9231SDan McDonald } 1620b77b9231SDan McDonald 1621fa9e4066Sahrens /* 1622fa9e4066Sahrens * Dirtbag ioctls to support mkfs(1M) for UFS filesystems. See dkio(7I). 1623b77b9231SDan McDonald * Also a dirtbag dkio ioctl for unmap/free-block functionality. 1624fa9e4066Sahrens */ 1625fa9e4066Sahrens /*ARGSUSED*/ 1626fa9e4066Sahrens int 1627fa9e4066Sahrens zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 1628fa9e4066Sahrens { 1629fa9e4066Sahrens zvol_state_t *zv; 1630af2c4821Smaybee struct dk_cinfo dki; 1631fa9e4066Sahrens struct dk_minfo dkm; 1632af2c4821Smaybee struct dk_callback *dkc; 1633fa9e4066Sahrens int error = 0; 1634e7cbe64fSgw rl_t *rl; 1635fa9e4066Sahrens 1636c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 1637fa9e4066Sahrens 1638c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(getminor(dev), ZSST_ZVOL); 1639fa9e4066Sahrens 1640fa9e4066Sahrens if (zv == NULL) { 1641c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 1642be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 1643fa9e4066Sahrens } 1644701f66c4SEric Taylor ASSERT(zv->zv_total_opens > 0); 1645fa9e4066Sahrens 1646fa9e4066Sahrens switch (cmd) { 1647fa9e4066Sahrens 1648fa9e4066Sahrens case DKIOCINFO: 1649af2c4821Smaybee bzero(&dki, sizeof (dki)); 1650af2c4821Smaybee (void) strcpy(dki.dki_cname, "zvol"); 1651af2c4821Smaybee (void) strcpy(dki.dki_dname, "zvol"); 1652af2c4821Smaybee dki.dki_ctype = DKC_UNKNOWN; 16533adc9019SEric Taylor dki.dki_unit = getminor(dev); 1654af2c4821Smaybee dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs); 1655c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 1656af2c4821Smaybee if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag)) 1657be6fd75aSMatthew Ahrens error = SET_ERROR(EFAULT); 1658fa9e4066Sahrens return (error); 1659fa9e4066Sahrens 1660fa9e4066Sahrens case DKIOCGMEDIAINFO: 1661fa9e4066Sahrens bzero(&dkm, sizeof (dkm)); 1662fa9e4066Sahrens dkm.dki_lbsize = 1U << zv->zv_min_bs; 1663fa9e4066Sahrens dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs; 1664fa9e4066Sahrens dkm.dki_media_type = DK_UNKNOWN; 1665c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 1666fa9e4066Sahrens if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag)) 1667be6fd75aSMatthew Ahrens error = SET_ERROR(EFAULT); 1668fa9e4066Sahrens return (error); 1669fa9e4066Sahrens 1670fa9e4066Sahrens case DKIOCGETEFI: 1671c7f714e2SEric Taylor { 1672c7f714e2SEric Taylor uint64_t vs = zv->zv_volsize; 1673c7f714e2SEric Taylor uint8_t bs = zv->zv_min_bs; 1674fa9e4066Sahrens 1675c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 1676c7f714e2SEric Taylor error = zvol_getefi((void *)arg, flag, vs, bs); 1677c7f714e2SEric Taylor return (error); 167868a5ac4dSmaybee } 1679fa9e4066Sahrens 1680feb08c6bSbillm case DKIOCFLUSHWRITECACHE: 1681af2c4821Smaybee dkc = (struct dk_callback *)arg; 1682c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 16835002558fSNeil Perrin zil_commit(zv->zv_zilog, ZVOL_OBJ); 1684af2c4821Smaybee if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) { 1685af2c4821Smaybee (*dkc->dkc_callback)(dkc->dkc_cookie, error); 1686af2c4821Smaybee error = 0; 1687af2c4821Smaybee } 1688701f66c4SEric Taylor return (error); 1689701f66c4SEric Taylor 1690701f66c4SEric Taylor case DKIOCGETWCE: 1691701f66c4SEric Taylor { 1692701f66c4SEric Taylor int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0; 1693701f66c4SEric Taylor if (ddi_copyout(&wce, (void *)arg, sizeof (int), 1694701f66c4SEric Taylor flag)) 1695be6fd75aSMatthew Ahrens error = SET_ERROR(EFAULT); 1696701f66c4SEric Taylor break; 1697701f66c4SEric Taylor } 1698701f66c4SEric Taylor case DKIOCSETWCE: 1699701f66c4SEric Taylor { 1700701f66c4SEric Taylor int wce; 1701701f66c4SEric Taylor if (ddi_copyin((void *)arg, &wce, sizeof (int), 1702701f66c4SEric Taylor flag)) { 1703be6fd75aSMatthew Ahrens error = SET_ERROR(EFAULT); 1704701f66c4SEric Taylor break; 1705701f66c4SEric Taylor } 1706701f66c4SEric Taylor if (wce) { 1707701f66c4SEric Taylor zv->zv_flags |= ZVOL_WCE; 1708c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 1709701f66c4SEric Taylor } else { 1710701f66c4SEric Taylor zv->zv_flags &= ~ZVOL_WCE; 1711c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 17125002558fSNeil Perrin zil_commit(zv->zv_zilog, ZVOL_OBJ); 1713701f66c4SEric Taylor } 1714701f66c4SEric Taylor return (0); 1715701f66c4SEric Taylor } 1716feb08c6bSbillm 1717b6130eadSmaybee case DKIOCGGEOM: 1718b6130eadSmaybee case DKIOCGVTOC: 1719e7cbe64fSgw /* 1720e7cbe64fSgw * commands using these (like prtvtoc) expect ENOTSUP 1721e7cbe64fSgw * since we're emulating an EFI label 1722e7cbe64fSgw */ 1723be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 1724b6130eadSmaybee break; 1725b6130eadSmaybee 1726e7cbe64fSgw case DKIOCDUMPINIT: 1727e7cbe64fSgw rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 1728e7cbe64fSgw RL_WRITER); 1729e7cbe64fSgw error = zvol_dumpify(zv); 1730e7cbe64fSgw zfs_range_unlock(rl); 1731e7cbe64fSgw break; 1732e7cbe64fSgw 1733e7cbe64fSgw case DKIOCDUMPFINI: 173406d5ae10SEric Taylor if (!(zv->zv_flags & ZVOL_DUMPIFIED)) 173506d5ae10SEric Taylor break; 1736e7cbe64fSgw rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 1737e7cbe64fSgw RL_WRITER); 1738e7cbe64fSgw error = zvol_dump_fini(zv); 1739e7cbe64fSgw zfs_range_unlock(rl); 1740e7cbe64fSgw break; 1741e7cbe64fSgw 1742b77b9231SDan McDonald case DKIOCFREE: 1743b77b9231SDan McDonald { 1744b77b9231SDan McDonald dkioc_free_t df; 1745b77b9231SDan McDonald dmu_tx_t *tx; 1746b77b9231SDan McDonald 1747b77b9231SDan McDonald if (ddi_copyin((void *)arg, &df, sizeof (df), flag)) { 1748be6fd75aSMatthew Ahrens error = SET_ERROR(EFAULT); 1749b77b9231SDan McDonald break; 1750b77b9231SDan McDonald } 1751b77b9231SDan McDonald 1752b77b9231SDan McDonald /* 1753b77b9231SDan McDonald * Apply Postel's Law to length-checking. If they overshoot, 1754b77b9231SDan McDonald * just blank out until the end, if there's a need to blank 1755b77b9231SDan McDonald * out anything. 1756b77b9231SDan McDonald */ 1757b77b9231SDan McDonald if (df.df_start >= zv->zv_volsize) 1758b77b9231SDan McDonald break; /* No need to do anything... */ 1759b77b9231SDan McDonald if (df.df_start + df.df_length > zv->zv_volsize) 1760b77b9231SDan McDonald df.df_length = DMU_OBJECT_END; 1761b77b9231SDan McDonald 1762b77b9231SDan McDonald rl = zfs_range_lock(&zv->zv_znode, df.df_start, df.df_length, 1763b77b9231SDan McDonald RL_WRITER); 1764b77b9231SDan McDonald tx = dmu_tx_create(zv->zv_objset); 1765b77b9231SDan McDonald error = dmu_tx_assign(tx, TXG_WAIT); 1766b77b9231SDan McDonald if (error != 0) { 1767b77b9231SDan McDonald dmu_tx_abort(tx); 1768b77b9231SDan McDonald } else { 1769b77b9231SDan McDonald zvol_log_truncate(zv, tx, df.df_start, 1770b77b9231SDan McDonald df.df_length, B_TRUE); 1771c08b1637SJosef 'Jeff' Sipek dmu_tx_commit(tx); 1772b77b9231SDan McDonald error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 1773b77b9231SDan McDonald df.df_start, df.df_length); 1774b77b9231SDan McDonald } 1775b77b9231SDan McDonald 1776b77b9231SDan McDonald zfs_range_unlock(rl); 1777b77b9231SDan McDonald 1778b77b9231SDan McDonald if (error == 0) { 1779b77b9231SDan McDonald /* 1780b77b9231SDan McDonald * If the write-cache is disabled or 'sync' property 1781b77b9231SDan McDonald * is set to 'always' then treat this as a synchronous 1782b77b9231SDan McDonald * operation (i.e. commit to zil). 1783b77b9231SDan McDonald */ 1784b77b9231SDan McDonald if (!(zv->zv_flags & ZVOL_WCE) || 1785b77b9231SDan McDonald (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)) 1786b77b9231SDan McDonald zil_commit(zv->zv_zilog, ZVOL_OBJ); 1787b77b9231SDan McDonald 1788b77b9231SDan McDonald /* 1789b77b9231SDan McDonald * If the caller really wants synchronous writes, and 1790b77b9231SDan McDonald * can't wait for them, don't return until the write 1791b77b9231SDan McDonald * is done. 1792b77b9231SDan McDonald */ 1793b77b9231SDan McDonald if (df.df_flags & DF_WAIT_SYNC) { 1794b77b9231SDan McDonald txg_wait_synced( 1795b77b9231SDan McDonald dmu_objset_pool(zv->zv_objset), 0); 1796b77b9231SDan McDonald } 1797b77b9231SDan McDonald } 1798b77b9231SDan McDonald break; 1799b77b9231SDan McDonald } 1800b77b9231SDan McDonald 1801fa9e4066Sahrens default: 1802be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTTY); 1803fa9e4066Sahrens break; 1804fa9e4066Sahrens 1805fa9e4066Sahrens } 1806c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 1807fa9e4066Sahrens return (error); 1808fa9e4066Sahrens } 1809fa9e4066Sahrens 1810fa9e4066Sahrens int 1811fa9e4066Sahrens zvol_busy(void) 1812fa9e4066Sahrens { 1813fa9e4066Sahrens return (zvol_minors != 0); 1814fa9e4066Sahrens } 1815fa9e4066Sahrens 1816fa9e4066Sahrens void 1817fa9e4066Sahrens zvol_init(void) 1818fa9e4066Sahrens { 1819c99e4bdcSChris Kirby VERIFY(ddi_soft_state_init(&zfsdev_state, sizeof (zfs_soft_state_t), 1820c99e4bdcSChris Kirby 1) == 0); 1821c99e4bdcSChris Kirby mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL); 1822fa9e4066Sahrens } 1823fa9e4066Sahrens 1824fa9e4066Sahrens void 1825fa9e4066Sahrens zvol_fini(void) 1826fa9e4066Sahrens { 1827c99e4bdcSChris Kirby mutex_destroy(&zfsdev_state_lock); 1828c99e4bdcSChris Kirby ddi_soft_state_fini(&zfsdev_state); 1829fa9e4066Sahrens } 1830e7cbe64fSgw 1831*810e43b2SBill Pijewski /*ARGSUSED*/ 1832*810e43b2SBill Pijewski static int 1833*810e43b2SBill Pijewski zfs_mvdev_dump_feature_check(void *arg, dmu_tx_t *tx) 1834*810e43b2SBill Pijewski { 1835*810e43b2SBill Pijewski spa_t *spa = dmu_tx_pool(tx)->dp_spa; 1836*810e43b2SBill Pijewski 1837*810e43b2SBill Pijewski if (spa_feature_is_active(spa, 1838*810e43b2SBill Pijewski &spa_feature_table[SPA_FEATURE_MULTI_VDEV_CRASH_DUMP])) 1839*810e43b2SBill Pijewski return (1); 1840*810e43b2SBill Pijewski return (0); 1841*810e43b2SBill Pijewski } 1842*810e43b2SBill Pijewski 1843*810e43b2SBill Pijewski /*ARGSUSED*/ 1844*810e43b2SBill Pijewski static void 1845*810e43b2SBill Pijewski zfs_mvdev_dump_activate_feature_sync(void *arg, dmu_tx_t *tx) 1846*810e43b2SBill Pijewski { 1847*810e43b2SBill Pijewski spa_t *spa = dmu_tx_pool(tx)->dp_spa; 1848*810e43b2SBill Pijewski 1849*810e43b2SBill Pijewski spa_feature_incr(spa, 1850*810e43b2SBill Pijewski &spa_feature_table[SPA_FEATURE_MULTI_VDEV_CRASH_DUMP], tx); 1851*810e43b2SBill Pijewski } 1852*810e43b2SBill Pijewski 1853e7cbe64fSgw static int 1854e7cbe64fSgw zvol_dump_init(zvol_state_t *zv, boolean_t resize) 1855e7cbe64fSgw { 1856e7cbe64fSgw dmu_tx_t *tx; 1857*810e43b2SBill Pijewski int error; 1858e7cbe64fSgw objset_t *os = zv->zv_objset; 1859*810e43b2SBill Pijewski spa_t *spa = dmu_objset_spa(os); 1860*810e43b2SBill Pijewski vdev_t *vd = spa->spa_root_vdev; 1861e7cbe64fSgw nvlist_t *nv = NULL; 1862*810e43b2SBill Pijewski uint64_t version = spa_version(spa); 1863*810e43b2SBill Pijewski enum zio_checksum checksum; 1864e7cbe64fSgw 1865c99e4bdcSChris Kirby ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 1866*810e43b2SBill Pijewski ASSERT(vd->vdev_ops == &vdev_root_ops); 1867*810e43b2SBill Pijewski 1868681d9761SEric Taylor error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0, 1869681d9761SEric Taylor DMU_OBJECT_END); 1870681d9761SEric Taylor /* wait for dmu_free_long_range to actually free the blocks */ 1871681d9761SEric Taylor txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 1872e7cbe64fSgw 1873*810e43b2SBill Pijewski /* 1874*810e43b2SBill Pijewski * If the pool on which the dump device is being initialized has more 1875*810e43b2SBill Pijewski * than one child vdev, check that the MULTI_VDEV_CRASH_DUMP feature is 1876*810e43b2SBill Pijewski * enabled. If so, bump that feature's counter to indicate that the 1877*810e43b2SBill Pijewski * feature is active. We also check the vdev type to handle the 1878*810e43b2SBill Pijewski * following case: 1879*810e43b2SBill Pijewski * # zpool create test raidz disk1 disk2 disk3 1880*810e43b2SBill Pijewski * Now have spa_root_vdev->vdev_children == 1 (the raidz vdev), 1881*810e43b2SBill Pijewski * the raidz vdev itself has 3 children. 1882*810e43b2SBill Pijewski */ 1883*810e43b2SBill Pijewski if (vd->vdev_children > 1 || vd->vdev_ops == &vdev_raidz_ops) { 1884*810e43b2SBill Pijewski if (!spa_feature_is_enabled(spa, 1885*810e43b2SBill Pijewski &spa_feature_table[SPA_FEATURE_MULTI_VDEV_CRASH_DUMP])) 1886*810e43b2SBill Pijewski return (SET_ERROR(ENOTSUP)); 1887*810e43b2SBill Pijewski (void) dsl_sync_task(spa_name(spa), 1888*810e43b2SBill Pijewski zfs_mvdev_dump_feature_check, 1889*810e43b2SBill Pijewski zfs_mvdev_dump_activate_feature_sync, NULL, 2); 1890*810e43b2SBill Pijewski } 1891*810e43b2SBill Pijewski 1892e7cbe64fSgw tx = dmu_tx_create(os); 1893e7cbe64fSgw dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 1894681d9761SEric Taylor dmu_tx_hold_bonus(tx, ZVOL_OBJ); 1895e7cbe64fSgw error = dmu_tx_assign(tx, TXG_WAIT); 1896e7cbe64fSgw if (error) { 1897e7cbe64fSgw dmu_tx_abort(tx); 1898e7cbe64fSgw return (error); 1899e7cbe64fSgw } 1900e7cbe64fSgw 1901*810e43b2SBill Pijewski /* 1902*810e43b2SBill Pijewski * If MULTI_VDEV_CRASH_DUMP is active, use the NOPARITY checksum 1903*810e43b2SBill Pijewski * function. Otherwise, use the old default -- OFF. 1904*810e43b2SBill Pijewski */ 1905*810e43b2SBill Pijewski checksum = spa_feature_is_active(spa, 1906*810e43b2SBill Pijewski &spa_feature_table[SPA_FEATURE_MULTI_VDEV_CRASH_DUMP]) ? 1907*810e43b2SBill Pijewski ZIO_CHECKSUM_NOPARITY : ZIO_CHECKSUM_OFF; 1908*810e43b2SBill Pijewski 1909e7cbe64fSgw /* 1910e7cbe64fSgw * If we are resizing the dump device then we only need to 1911e7cbe64fSgw * update the refreservation to match the newly updated 1912e7cbe64fSgw * zvolsize. Otherwise, we save off the original state of the 1913e7cbe64fSgw * zvol so that we can restore them if the zvol is ever undumpified. 1914e7cbe64fSgw */ 1915e7cbe64fSgw if (resize) { 1916e7cbe64fSgw error = zap_update(os, ZVOL_ZAP_OBJ, 1917e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 1918e7cbe64fSgw &zv->zv_volsize, tx); 1919e7cbe64fSgw } else { 1920afee20e4SGeorge Wilson uint64_t checksum, compress, refresrv, vbs, dedup; 192188b7b0f2SMatthew Ahrens 1922e7cbe64fSgw error = dsl_prop_get_integer(zv->zv_name, 1923e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL); 1924e7cbe64fSgw error = error ? error : dsl_prop_get_integer(zv->zv_name, 1925e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL); 1926e7cbe64fSgw error = error ? error : dsl_prop_get_integer(zv->zv_name, 1927e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL); 192888b7b0f2SMatthew Ahrens error = error ? error : dsl_prop_get_integer(zv->zv_name, 192988b7b0f2SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL); 19308d265e66SGeorge Wilson if (version >= SPA_VERSION_DEDUP) { 19318d265e66SGeorge Wilson error = error ? error : 19328d265e66SGeorge Wilson dsl_prop_get_integer(zv->zv_name, 19338d265e66SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), &dedup, NULL); 19348d265e66SGeorge Wilson } 1935e7cbe64fSgw 1936e7cbe64fSgw error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1937e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, 1938e7cbe64fSgw &compress, tx); 1939e7cbe64fSgw error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1940e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx); 1941e7cbe64fSgw error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1942e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 1943e7cbe64fSgw &refresrv, tx); 194488b7b0f2SMatthew Ahrens error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 194588b7b0f2SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, 194688b7b0f2SMatthew Ahrens &vbs, tx); 1947681d9761SEric Taylor error = error ? error : dmu_object_set_blocksize( 1948681d9761SEric Taylor os, ZVOL_OBJ, SPA_MAXBLOCKSIZE, 0, tx); 19498d265e66SGeorge Wilson if (version >= SPA_VERSION_DEDUP) { 19508d265e66SGeorge Wilson error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 19518d265e66SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, 19528d265e66SGeorge Wilson &dedup, tx); 19538d265e66SGeorge Wilson } 1954681d9761SEric Taylor if (error == 0) 1955681d9761SEric Taylor zv->zv_volblocksize = SPA_MAXBLOCKSIZE; 1956e7cbe64fSgw } 1957e7cbe64fSgw dmu_tx_commit(tx); 1958e7cbe64fSgw 1959e7cbe64fSgw /* 1960e7cbe64fSgw * We only need update the zvol's property if we are initializing 1961e7cbe64fSgw * the dump area for the first time. 1962e7cbe64fSgw */ 1963e7cbe64fSgw if (!resize) { 1964e7cbe64fSgw VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1965e7cbe64fSgw VERIFY(nvlist_add_uint64(nv, 1966e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0); 1967e7cbe64fSgw VERIFY(nvlist_add_uint64(nv, 1968e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), 1969e7cbe64fSgw ZIO_COMPRESS_OFF) == 0); 1970e7cbe64fSgw VERIFY(nvlist_add_uint64(nv, 1971e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_CHECKSUM), 1972*810e43b2SBill Pijewski checksum) == 0); 19738d265e66SGeorge Wilson if (version >= SPA_VERSION_DEDUP) { 19748d265e66SGeorge Wilson VERIFY(nvlist_add_uint64(nv, 19758d265e66SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), 19768d265e66SGeorge Wilson ZIO_CHECKSUM_OFF) == 0); 19778d265e66SGeorge Wilson } 1978e7cbe64fSgw 197992241e0bSTom Erickson error = zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL, 198092241e0bSTom Erickson nv, NULL); 1981e7cbe64fSgw nvlist_free(nv); 1982e7cbe64fSgw 1983e7cbe64fSgw if (error) 1984e7cbe64fSgw return (error); 1985e7cbe64fSgw } 1986e7cbe64fSgw 1987e7cbe64fSgw /* Allocate the space for the dump */ 1988e7cbe64fSgw error = zvol_prealloc(zv); 1989e7cbe64fSgw return (error); 1990e7cbe64fSgw } 1991e7cbe64fSgw 1992e7cbe64fSgw static int 1993e7cbe64fSgw zvol_dumpify(zvol_state_t *zv) 1994e7cbe64fSgw { 1995e7cbe64fSgw int error = 0; 1996e7cbe64fSgw uint64_t dumpsize = 0; 1997e7cbe64fSgw dmu_tx_t *tx; 1998e7cbe64fSgw objset_t *os = zv->zv_objset; 1999e7cbe64fSgw 2000681d9761SEric Taylor if (zv->zv_flags & ZVOL_RDONLY) 2001be6fd75aSMatthew Ahrens return (SET_ERROR(EROFS)); 2002e7cbe64fSgw 2003e7cbe64fSgw if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 2004e7cbe64fSgw 8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) { 20054445fffbSMatthew Ahrens boolean_t resize = (dumpsize > 0); 2006e7cbe64fSgw 2007e7cbe64fSgw if ((error = zvol_dump_init(zv, resize)) != 0) { 2008e7cbe64fSgw (void) zvol_dump_fini(zv); 2009e7cbe64fSgw return (error); 2010e7cbe64fSgw } 2011e7cbe64fSgw } 2012e7cbe64fSgw 2013e7cbe64fSgw /* 2014e7cbe64fSgw * Build up our lba mapping. 2015e7cbe64fSgw */ 2016e7cbe64fSgw error = zvol_get_lbas(zv); 2017e7cbe64fSgw if (error) { 2018e7cbe64fSgw (void) zvol_dump_fini(zv); 2019e7cbe64fSgw return (error); 2020e7cbe64fSgw } 2021e7cbe64fSgw 2022e7cbe64fSgw tx = dmu_tx_create(os); 2023e7cbe64fSgw dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 2024e7cbe64fSgw error = dmu_tx_assign(tx, TXG_WAIT); 2025e7cbe64fSgw if (error) { 2026e7cbe64fSgw dmu_tx_abort(tx); 2027e7cbe64fSgw (void) zvol_dump_fini(zv); 2028e7cbe64fSgw return (error); 2029e7cbe64fSgw } 2030e7cbe64fSgw 2031e7cbe64fSgw zv->zv_flags |= ZVOL_DUMPIFIED; 2032e7cbe64fSgw error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1, 2033e7cbe64fSgw &zv->zv_volsize, tx); 2034e7cbe64fSgw dmu_tx_commit(tx); 2035e7cbe64fSgw 2036e7cbe64fSgw if (error) { 2037e7cbe64fSgw (void) zvol_dump_fini(zv); 2038e7cbe64fSgw return (error); 2039e7cbe64fSgw } 2040e7cbe64fSgw 2041e7cbe64fSgw txg_wait_synced(dmu_objset_pool(os), 0); 2042e7cbe64fSgw return (0); 2043e7cbe64fSgw } 2044e7cbe64fSgw 2045e7cbe64fSgw static int 2046e7cbe64fSgw zvol_dump_fini(zvol_state_t *zv) 2047e7cbe64fSgw { 2048e7cbe64fSgw dmu_tx_t *tx; 2049e7cbe64fSgw objset_t *os = zv->zv_objset; 2050e7cbe64fSgw nvlist_t *nv; 2051e7cbe64fSgw int error = 0; 2052afee20e4SGeorge Wilson uint64_t checksum, compress, refresrv, vbs, dedup; 20538d265e66SGeorge Wilson uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset)); 2054e7cbe64fSgw 2055b7e50089Smaybee /* 2056b7e50089Smaybee * Attempt to restore the zvol back to its pre-dumpified state. 2057b7e50089Smaybee * This is a best-effort attempt as it's possible that not all 2058b7e50089Smaybee * of these properties were initialized during the dumpify process 2059b7e50089Smaybee * (i.e. error during zvol_dump_init). 2060b7e50089Smaybee */ 2061b7e50089Smaybee 2062e7cbe64fSgw tx = dmu_tx_create(os); 2063e7cbe64fSgw dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 2064e7cbe64fSgw error = dmu_tx_assign(tx, TXG_WAIT); 2065e7cbe64fSgw if (error) { 2066e7cbe64fSgw dmu_tx_abort(tx); 2067e7cbe64fSgw return (error); 2068e7cbe64fSgw } 2069b7e50089Smaybee (void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx); 2070b7e50089Smaybee dmu_tx_commit(tx); 2071e7cbe64fSgw 2072e7cbe64fSgw (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2073e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum); 2074e7cbe64fSgw (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2075e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress); 2076e7cbe64fSgw (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2077e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv); 207888b7b0f2SMatthew Ahrens (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 207988b7b0f2SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs); 2080e7cbe64fSgw 2081e7cbe64fSgw VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2082e7cbe64fSgw (void) nvlist_add_uint64(nv, 2083e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum); 2084e7cbe64fSgw (void) nvlist_add_uint64(nv, 2085e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress); 2086e7cbe64fSgw (void) nvlist_add_uint64(nv, 2087e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv); 20888d265e66SGeorge Wilson if (version >= SPA_VERSION_DEDUP && 20898d265e66SGeorge Wilson zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 20908d265e66SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, &dedup) == 0) { 20918d265e66SGeorge Wilson (void) nvlist_add_uint64(nv, 20928d265e66SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), dedup); 20938d265e66SGeorge Wilson } 209492241e0bSTom Erickson (void) zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL, 209592241e0bSTom Erickson nv, NULL); 2096e7cbe64fSgw nvlist_free(nv); 2097e7cbe64fSgw 2098b7e50089Smaybee zvol_free_extents(zv); 2099b7e50089Smaybee zv->zv_flags &= ~ZVOL_DUMPIFIED; 2100b7e50089Smaybee (void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END); 2101681d9761SEric Taylor /* wait for dmu_free_long_range to actually free the blocks */ 2102681d9761SEric Taylor txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 2103681d9761SEric Taylor tx = dmu_tx_create(os); 2104681d9761SEric Taylor dmu_tx_hold_bonus(tx, ZVOL_OBJ); 2105681d9761SEric Taylor error = dmu_tx_assign(tx, TXG_WAIT); 2106681d9761SEric Taylor if (error) { 2107681d9761SEric Taylor dmu_tx_abort(tx); 2108681d9761SEric Taylor return (error); 2109681d9761SEric Taylor } 2110b24ab676SJeff Bonwick if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0) 2111b24ab676SJeff Bonwick zv->zv_volblocksize = vbs; 2112681d9761SEric Taylor dmu_tx_commit(tx); 2113b7e50089Smaybee 2114e7cbe64fSgw return (0); 2115e7cbe64fSgw } 2116