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. 274445fffbSMatthew Ahrens * Copyright (c) 2012 by Delphix. All rights reserved. 28fa9e4066Sahrens */ 29fa9e4066Sahrens 30fa9e4066Sahrens /* 31fa9e4066Sahrens * ZFS volume emulation driver. 32fa9e4066Sahrens * 33fa9e4066Sahrens * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes. 34fa9e4066Sahrens * Volumes are accessed through the symbolic links named: 35fa9e4066Sahrens * 36fa9e4066Sahrens * /dev/zvol/dsk/<pool_name>/<dataset_name> 37fa9e4066Sahrens * /dev/zvol/rdsk/<pool_name>/<dataset_name> 38fa9e4066Sahrens * 39681d9761SEric Taylor * These links are created by the /dev filesystem (sdev_zvolops.c). 40fa9e4066Sahrens * Volumes are persistent through reboot. No user command needs to be 41fa9e4066Sahrens * run before opening and using a device. 42fa9e4066Sahrens */ 43fa9e4066Sahrens 44fa9e4066Sahrens #include <sys/types.h> 45fa9e4066Sahrens #include <sys/param.h> 46fa9e4066Sahrens #include <sys/errno.h> 47fa9e4066Sahrens #include <sys/uio.h> 48fa9e4066Sahrens #include <sys/buf.h> 49fa9e4066Sahrens #include <sys/modctl.h> 50fa9e4066Sahrens #include <sys/open.h> 51fa9e4066Sahrens #include <sys/kmem.h> 52fa9e4066Sahrens #include <sys/conf.h> 53fa9e4066Sahrens #include <sys/cmn_err.h> 54fa9e4066Sahrens #include <sys/stat.h> 55fa9e4066Sahrens #include <sys/zap.h> 56fa9e4066Sahrens #include <sys/spa.h> 57fa9e4066Sahrens #include <sys/zio.h> 58e7cbe64fSgw #include <sys/dmu_traverse.h> 59e7cbe64fSgw #include <sys/dnode.h> 60e7cbe64fSgw #include <sys/dsl_dataset.h> 61fa9e4066Sahrens #include <sys/dsl_prop.h> 62fa9e4066Sahrens #include <sys/dkio.h> 63fa9e4066Sahrens #include <sys/efi_partition.h> 64fa9e4066Sahrens #include <sys/byteorder.h> 65fa9e4066Sahrens #include <sys/pathname.h> 66fa9e4066Sahrens #include <sys/ddi.h> 67fa9e4066Sahrens #include <sys/sunddi.h> 68fa9e4066Sahrens #include <sys/crc32.h> 69fa9e4066Sahrens #include <sys/dirent.h> 70fa9e4066Sahrens #include <sys/policy.h> 71fa9e4066Sahrens #include <sys/fs/zfs.h> 72fa9e4066Sahrens #include <sys/zfs_ioctl.h> 73fa9e4066Sahrens #include <sys/mkdev.h> 7422ac5be4Sperrin #include <sys/zil.h> 75c5c6ffa0Smaybee #include <sys/refcount.h> 76c2e6a7d6Sperrin #include <sys/zfs_znode.h> 77c2e6a7d6Sperrin #include <sys/zfs_rlock.h> 78e7cbe64fSgw #include <sys/vdev_disk.h> 79e7cbe64fSgw #include <sys/vdev_impl.h> 80e7cbe64fSgw #include <sys/zvol.h> 81e7cbe64fSgw #include <sys/dumphdr.h> 821209a471SNeil Perrin #include <sys/zil_impl.h> 8380901aeaSGeorge Wilson #include <sys/dbuf.h> 84fa9e4066Sahrens 85fa9e4066Sahrens #include "zfs_namecheck.h" 86fa9e4066Sahrens 87c99e4bdcSChris Kirby void *zfsdev_state; 88503ad85cSMatthew Ahrens static char *zvol_tag = "zvol_tag"; 89fa9e4066Sahrens 90e7cbe64fSgw #define ZVOL_DUMPSIZE "dumpsize" 91e7cbe64fSgw 92fa9e4066Sahrens /* 93c99e4bdcSChris Kirby * This lock protects the zfsdev_state structure from being modified 94fa9e4066Sahrens * while it's being used, e.g. an open that comes in before a create 95fa9e4066Sahrens * finishes. It also protects temporary opens of the dataset so that, 96fa9e4066Sahrens * e.g., an open doesn't get a spurious EBUSY. 97fa9e4066Sahrens */ 98c99e4bdcSChris Kirby kmutex_t zfsdev_state_lock; 99fa9e4066Sahrens static uint32_t zvol_minors; 100fa9e4066Sahrens 101e7cbe64fSgw typedef struct zvol_extent { 10288b7b0f2SMatthew Ahrens list_node_t ze_node; 103e7cbe64fSgw dva_t ze_dva; /* dva associated with this extent */ 10488b7b0f2SMatthew Ahrens uint64_t ze_nblks; /* number of blocks in extent */ 105e7cbe64fSgw } zvol_extent_t; 106e7cbe64fSgw 107fa9e4066Sahrens /* 108fa9e4066Sahrens * The in-core state of each volume. 109fa9e4066Sahrens */ 110fa9e4066Sahrens typedef struct zvol_state { 111fa9e4066Sahrens char zv_name[MAXPATHLEN]; /* pool/dd name */ 112fa9e4066Sahrens uint64_t zv_volsize; /* amount of space we advertise */ 11367bd71c6Sperrin uint64_t zv_volblocksize; /* volume block size */ 114fa9e4066Sahrens minor_t zv_minor; /* minor number */ 115fa9e4066Sahrens uint8_t zv_min_bs; /* minimum addressable block shift */ 116701f66c4SEric Taylor uint8_t zv_flags; /* readonly, dumpified, etc. */ 117fa9e4066Sahrens objset_t *zv_objset; /* objset handle */ 118fa9e4066Sahrens uint32_t zv_open_count[OTYPCNT]; /* open counts */ 119fa9e4066Sahrens uint32_t zv_total_opens; /* total open count */ 12022ac5be4Sperrin zilog_t *zv_zilog; /* ZIL handle */ 12188b7b0f2SMatthew Ahrens list_t zv_extents; /* List of extents for dump */ 122c2e6a7d6Sperrin znode_t zv_znode; /* for range locking */ 12394d1a210STim Haley dmu_buf_t *zv_dbuf; /* bonus handle */ 124fa9e4066Sahrens } zvol_state_t; 125fa9e4066Sahrens 126e7cbe64fSgw /* 127e7cbe64fSgw * zvol specific flags 128e7cbe64fSgw */ 129e7cbe64fSgw #define ZVOL_RDONLY 0x1 130e7cbe64fSgw #define ZVOL_DUMPIFIED 0x2 131c7f714e2SEric Taylor #define ZVOL_EXCL 0x4 132701f66c4SEric Taylor #define ZVOL_WCE 0x8 133e7cbe64fSgw 13467bd71c6Sperrin /* 13567bd71c6Sperrin * zvol maximum transfer in one DMU tx. 13667bd71c6Sperrin */ 13767bd71c6Sperrin int zvol_maxphys = DMU_MAX_ACCESS/2; 13867bd71c6Sperrin 13992241e0bSTom Erickson extern int zfs_set_prop_nvlist(const char *, zprop_source_t, 1404445fffbSMatthew Ahrens nvlist_t *, nvlist_t *); 141681d9761SEric Taylor static int zvol_remove_zv(zvol_state_t *); 142feb08c6bSbillm static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio); 143e7cbe64fSgw static int zvol_dumpify(zvol_state_t *zv); 144e7cbe64fSgw static int zvol_dump_fini(zvol_state_t *zv); 145e7cbe64fSgw static int zvol_dump_init(zvol_state_t *zv, boolean_t resize); 14667bd71c6Sperrin 147fa9e4066Sahrens static void 148c61ea566SGeorge Wilson zvol_size_changed(zvol_state_t *zv, uint64_t volsize) 149fa9e4066Sahrens { 150c61ea566SGeorge Wilson dev_t dev = makedevice(ddi_driver_major(zfs_dip), zv->zv_minor); 151fa9e4066Sahrens 152c61ea566SGeorge Wilson zv->zv_volsize = volsize; 153fa9e4066Sahrens VERIFY(ddi_prop_update_int64(dev, zfs_dip, 154681d9761SEric Taylor "Size", volsize) == DDI_SUCCESS); 155fa9e4066Sahrens VERIFY(ddi_prop_update_int64(dev, zfs_dip, 156681d9761SEric Taylor "Nblocks", lbtodb(volsize)) == DDI_SUCCESS); 157e7cbe64fSgw 158e7cbe64fSgw /* Notify specfs to invalidate the cached size */ 159e7cbe64fSgw spec_size_invalidate(dev, VBLK); 160e7cbe64fSgw spec_size_invalidate(dev, VCHR); 161fa9e4066Sahrens } 162fa9e4066Sahrens 163fa9e4066Sahrens int 164e9dbad6fSeschrock zvol_check_volsize(uint64_t volsize, uint64_t blocksize) 165fa9e4066Sahrens { 166e9dbad6fSeschrock if (volsize == 0) 167fa9e4066Sahrens return (EINVAL); 168fa9e4066Sahrens 169e9dbad6fSeschrock if (volsize % blocksize != 0) 1705c5460e9Seschrock return (EINVAL); 1715c5460e9Seschrock 172fa9e4066Sahrens #ifdef _ILP32 173e9dbad6fSeschrock if (volsize - 1 > SPEC_MAXOFFSET_T) 174fa9e4066Sahrens return (EOVERFLOW); 175fa9e4066Sahrens #endif 176fa9e4066Sahrens return (0); 177fa9e4066Sahrens } 178fa9e4066Sahrens 179fa9e4066Sahrens int 180e9dbad6fSeschrock zvol_check_volblocksize(uint64_t volblocksize) 181fa9e4066Sahrens { 182e9dbad6fSeschrock if (volblocksize < SPA_MINBLOCKSIZE || 183e9dbad6fSeschrock volblocksize > SPA_MAXBLOCKSIZE || 184e9dbad6fSeschrock !ISP2(volblocksize)) 185fa9e4066Sahrens return (EDOM); 186fa9e4066Sahrens 187fa9e4066Sahrens return (0); 188fa9e4066Sahrens } 189fa9e4066Sahrens 190fa9e4066Sahrens int 191a2eea2e1Sahrens zvol_get_stats(objset_t *os, nvlist_t *nv) 192fa9e4066Sahrens { 193fa9e4066Sahrens int error; 194fa9e4066Sahrens dmu_object_info_t doi; 195a2eea2e1Sahrens uint64_t val; 196fa9e4066Sahrens 197a2eea2e1Sahrens error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val); 198fa9e4066Sahrens if (error) 199fa9e4066Sahrens return (error); 200fa9e4066Sahrens 201a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val); 202a2eea2e1Sahrens 203fa9e4066Sahrens error = dmu_object_info(os, ZVOL_OBJ, &doi); 204fa9e4066Sahrens 205a2eea2e1Sahrens if (error == 0) { 206a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE, 207a2eea2e1Sahrens doi.doi_data_block_size); 208a2eea2e1Sahrens } 209fa9e4066Sahrens 210fa9e4066Sahrens return (error); 211fa9e4066Sahrens } 212fa9e4066Sahrens 213fa9e4066Sahrens static zvol_state_t * 214e9dbad6fSeschrock zvol_minor_lookup(const char *name) 215fa9e4066Sahrens { 216fa9e4066Sahrens minor_t minor; 217fa9e4066Sahrens zvol_state_t *zv; 218fa9e4066Sahrens 219c99e4bdcSChris Kirby ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 220fa9e4066Sahrens 221c99e4bdcSChris Kirby for (minor = 1; minor <= ZFSDEV_MAX_MINOR; minor++) { 222c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 223fa9e4066Sahrens if (zv == NULL) 224fa9e4066Sahrens continue; 225fa9e4066Sahrens if (strcmp(zv->zv_name, name) == 0) 226f80ce222SChris Kirby return (zv); 227fa9e4066Sahrens } 228fa9e4066Sahrens 229f80ce222SChris Kirby return (NULL); 230fa9e4066Sahrens } 231fa9e4066Sahrens 232e7cbe64fSgw /* extent mapping arg */ 233e7cbe64fSgw struct maparg { 23488b7b0f2SMatthew Ahrens zvol_state_t *ma_zv; 23588b7b0f2SMatthew Ahrens uint64_t ma_blks; 236e7cbe64fSgw }; 237e7cbe64fSgw 238e7cbe64fSgw /*ARGSUSED*/ 239e7cbe64fSgw static int 2401b912ec7SGeorge Wilson zvol_map_block(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 241b24ab676SJeff Bonwick const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg) 242e7cbe64fSgw { 24388b7b0f2SMatthew Ahrens struct maparg *ma = arg; 24488b7b0f2SMatthew Ahrens zvol_extent_t *ze; 24588b7b0f2SMatthew Ahrens int bs = ma->ma_zv->zv_volblocksize; 246e7cbe64fSgw 24788b7b0f2SMatthew Ahrens if (bp == NULL || zb->zb_object != ZVOL_OBJ || zb->zb_level != 0) 24888b7b0f2SMatthew Ahrens return (0); 249e7cbe64fSgw 25088b7b0f2SMatthew Ahrens VERIFY3U(ma->ma_blks, ==, zb->zb_blkid); 25188b7b0f2SMatthew Ahrens ma->ma_blks++; 252e7cbe64fSgw 25388b7b0f2SMatthew Ahrens /* Abort immediately if we have encountered gang blocks */ 25488b7b0f2SMatthew Ahrens if (BP_IS_GANG(bp)) 25588b7b0f2SMatthew Ahrens return (EFRAGS); 256e7cbe64fSgw 25788b7b0f2SMatthew Ahrens /* 25888b7b0f2SMatthew Ahrens * See if the block is at the end of the previous extent. 25988b7b0f2SMatthew Ahrens */ 26088b7b0f2SMatthew Ahrens ze = list_tail(&ma->ma_zv->zv_extents); 26188b7b0f2SMatthew Ahrens if (ze && 26288b7b0f2SMatthew Ahrens DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) && 26388b7b0f2SMatthew Ahrens DVA_GET_OFFSET(BP_IDENTITY(bp)) == 26488b7b0f2SMatthew Ahrens DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) { 26588b7b0f2SMatthew Ahrens ze->ze_nblks++; 26688b7b0f2SMatthew Ahrens return (0); 267e7cbe64fSgw } 268e7cbe64fSgw 26988b7b0f2SMatthew Ahrens dprintf_bp(bp, "%s", "next blkptr:"); 270e7cbe64fSgw 27188b7b0f2SMatthew Ahrens /* start a new extent */ 27288b7b0f2SMatthew Ahrens ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP); 27388b7b0f2SMatthew Ahrens ze->ze_dva = bp->blk_dva[0]; /* structure assignment */ 27488b7b0f2SMatthew Ahrens ze->ze_nblks = 1; 27588b7b0f2SMatthew Ahrens list_insert_tail(&ma->ma_zv->zv_extents, ze); 27688b7b0f2SMatthew Ahrens return (0); 27788b7b0f2SMatthew Ahrens } 278e7cbe64fSgw 27988b7b0f2SMatthew Ahrens static void 28088b7b0f2SMatthew Ahrens zvol_free_extents(zvol_state_t *zv) 28188b7b0f2SMatthew Ahrens { 28288b7b0f2SMatthew Ahrens zvol_extent_t *ze; 283e7cbe64fSgw 28488b7b0f2SMatthew Ahrens while (ze = list_head(&zv->zv_extents)) { 28588b7b0f2SMatthew Ahrens list_remove(&zv->zv_extents, ze); 28688b7b0f2SMatthew Ahrens kmem_free(ze, sizeof (zvol_extent_t)); 287e7cbe64fSgw } 28888b7b0f2SMatthew Ahrens } 289e7cbe64fSgw 29088b7b0f2SMatthew Ahrens static int 29188b7b0f2SMatthew Ahrens zvol_get_lbas(zvol_state_t *zv) 29288b7b0f2SMatthew Ahrens { 2933adc9019SEric Taylor objset_t *os = zv->zv_objset; 29488b7b0f2SMatthew Ahrens struct maparg ma; 29588b7b0f2SMatthew Ahrens int err; 29688b7b0f2SMatthew Ahrens 29788b7b0f2SMatthew Ahrens ma.ma_zv = zv; 29888b7b0f2SMatthew Ahrens ma.ma_blks = 0; 29988b7b0f2SMatthew Ahrens zvol_free_extents(zv); 30088b7b0f2SMatthew Ahrens 3013adc9019SEric Taylor /* commit any in-flight changes before traversing the dataset */ 3023adc9019SEric Taylor txg_wait_synced(dmu_objset_pool(os), 0); 3033adc9019SEric Taylor err = traverse_dataset(dmu_objset_ds(os), 0, 30488b7b0f2SMatthew Ahrens TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma); 30588b7b0f2SMatthew Ahrens if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) { 30688b7b0f2SMatthew Ahrens zvol_free_extents(zv); 30788b7b0f2SMatthew Ahrens return (err ? err : EIO); 308e7cbe64fSgw } 30988b7b0f2SMatthew Ahrens 310e7cbe64fSgw return (0); 311e7cbe64fSgw } 312e7cbe64fSgw 313ecd6cf80Smarks /* ARGSUSED */ 314fa9e4066Sahrens void 315ecd6cf80Smarks zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 316fa9e4066Sahrens { 317da6c28aaSamw zfs_creat_t *zct = arg; 318da6c28aaSamw nvlist_t *nvprops = zct->zct_props; 319fa9e4066Sahrens int error; 320e9dbad6fSeschrock uint64_t volblocksize, volsize; 321fa9e4066Sahrens 322ecd6cf80Smarks VERIFY(nvlist_lookup_uint64(nvprops, 323e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0); 324ecd6cf80Smarks if (nvlist_lookup_uint64(nvprops, 325e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0) 326e9dbad6fSeschrock volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 327e9dbad6fSeschrock 328e9dbad6fSeschrock /* 329e7cbe64fSgw * These properties must be removed from the list so the generic 330e9dbad6fSeschrock * property setting step won't apply to them. 331e9dbad6fSeschrock */ 332ecd6cf80Smarks VERIFY(nvlist_remove_all(nvprops, 333e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0); 334ecd6cf80Smarks (void) nvlist_remove_all(nvprops, 335e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE)); 336e9dbad6fSeschrock 337e9dbad6fSeschrock error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize, 338fa9e4066Sahrens DMU_OT_NONE, 0, tx); 339fa9e4066Sahrens ASSERT(error == 0); 340fa9e4066Sahrens 341fa9e4066Sahrens error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP, 342fa9e4066Sahrens DMU_OT_NONE, 0, tx); 343fa9e4066Sahrens ASSERT(error == 0); 344fa9e4066Sahrens 345e9dbad6fSeschrock error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx); 346fa9e4066Sahrens ASSERT(error == 0); 347fa9e4066Sahrens } 348fa9e4066Sahrens 349b77b9231SDan McDonald /* 350b77b9231SDan McDonald * Replay a TX_TRUNCATE ZIL transaction if asked. TX_TRUNCATE is how we 351b77b9231SDan McDonald * implement DKIOCFREE/free-long-range. 352b77b9231SDan McDonald */ 353b77b9231SDan McDonald static int 354b77b9231SDan McDonald zvol_replay_truncate(zvol_state_t *zv, lr_truncate_t *lr, boolean_t byteswap) 355b77b9231SDan McDonald { 356b77b9231SDan McDonald uint64_t offset, length; 357b77b9231SDan McDonald 358b77b9231SDan McDonald if (byteswap) 359b77b9231SDan McDonald byteswap_uint64_array(lr, sizeof (*lr)); 360b77b9231SDan McDonald 361b77b9231SDan McDonald offset = lr->lr_offset; 362b77b9231SDan McDonald length = lr->lr_length; 363b77b9231SDan McDonald 364b77b9231SDan McDonald return (dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, offset, length)); 365b77b9231SDan McDonald } 366b77b9231SDan McDonald 36722ac5be4Sperrin /* 36822ac5be4Sperrin * Replay a TX_WRITE ZIL transaction that didn't get committed 36922ac5be4Sperrin * after a system failure 37022ac5be4Sperrin */ 37122ac5be4Sperrin static int 37222ac5be4Sperrin zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap) 37322ac5be4Sperrin { 37422ac5be4Sperrin objset_t *os = zv->zv_objset; 37522ac5be4Sperrin char *data = (char *)(lr + 1); /* data follows lr_write_t */ 376b24ab676SJeff Bonwick uint64_t offset, length; 37722ac5be4Sperrin dmu_tx_t *tx; 37822ac5be4Sperrin int error; 37922ac5be4Sperrin 38022ac5be4Sperrin if (byteswap) 38122ac5be4Sperrin byteswap_uint64_array(lr, sizeof (*lr)); 38222ac5be4Sperrin 383b24ab676SJeff Bonwick offset = lr->lr_offset; 384b24ab676SJeff Bonwick length = lr->lr_length; 385b24ab676SJeff Bonwick 386b24ab676SJeff Bonwick /* If it's a dmu_sync() block, write the whole block */ 387b24ab676SJeff Bonwick if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) { 388b24ab676SJeff Bonwick uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr); 389b24ab676SJeff Bonwick if (length < blocksize) { 390b24ab676SJeff Bonwick offset -= offset % blocksize; 391b24ab676SJeff Bonwick length = blocksize; 392b24ab676SJeff Bonwick } 393b24ab676SJeff Bonwick } 394975c32a0SNeil Perrin 39522ac5be4Sperrin tx = dmu_tx_create(os); 396b24ab676SJeff Bonwick dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length); 3971209a471SNeil Perrin error = dmu_tx_assign(tx, TXG_WAIT); 39822ac5be4Sperrin if (error) { 39922ac5be4Sperrin dmu_tx_abort(tx); 40022ac5be4Sperrin } else { 401b24ab676SJeff Bonwick dmu_write(os, ZVOL_OBJ, offset, length, data, tx); 40222ac5be4Sperrin dmu_tx_commit(tx); 40322ac5be4Sperrin } 40422ac5be4Sperrin 40522ac5be4Sperrin return (error); 40622ac5be4Sperrin } 40722ac5be4Sperrin 40822ac5be4Sperrin /* ARGSUSED */ 40922ac5be4Sperrin static int 41022ac5be4Sperrin zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap) 41122ac5be4Sperrin { 41222ac5be4Sperrin return (ENOTSUP); 41322ac5be4Sperrin } 41422ac5be4Sperrin 41522ac5be4Sperrin /* 41622ac5be4Sperrin * Callback vectors for replaying records. 417b77b9231SDan McDonald * Only TX_WRITE and TX_TRUNCATE are needed for zvol. 41822ac5be4Sperrin */ 41922ac5be4Sperrin zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = { 42022ac5be4Sperrin zvol_replay_err, /* 0 no such transaction type */ 42122ac5be4Sperrin zvol_replay_err, /* TX_CREATE */ 42222ac5be4Sperrin zvol_replay_err, /* TX_MKDIR */ 42322ac5be4Sperrin zvol_replay_err, /* TX_MKXATTR */ 42422ac5be4Sperrin zvol_replay_err, /* TX_SYMLINK */ 42522ac5be4Sperrin zvol_replay_err, /* TX_REMOVE */ 42622ac5be4Sperrin zvol_replay_err, /* TX_RMDIR */ 42722ac5be4Sperrin zvol_replay_err, /* TX_LINK */ 42822ac5be4Sperrin zvol_replay_err, /* TX_RENAME */ 42922ac5be4Sperrin zvol_replay_write, /* TX_WRITE */ 430b77b9231SDan McDonald zvol_replay_truncate, /* TX_TRUNCATE */ 43122ac5be4Sperrin zvol_replay_err, /* TX_SETATTR */ 43222ac5be4Sperrin zvol_replay_err, /* TX_ACL */ 433975c32a0SNeil Perrin zvol_replay_err, /* TX_CREATE_ACL */ 434975c32a0SNeil Perrin zvol_replay_err, /* TX_CREATE_ATTR */ 435975c32a0SNeil Perrin zvol_replay_err, /* TX_CREATE_ACL_ATTR */ 436975c32a0SNeil Perrin zvol_replay_err, /* TX_MKDIR_ACL */ 437975c32a0SNeil Perrin zvol_replay_err, /* TX_MKDIR_ATTR */ 438975c32a0SNeil Perrin zvol_replay_err, /* TX_MKDIR_ACL_ATTR */ 439975c32a0SNeil Perrin zvol_replay_err, /* TX_WRITE2 */ 44022ac5be4Sperrin }; 44122ac5be4Sperrin 442681d9761SEric Taylor int 443681d9761SEric Taylor zvol_name2minor(const char *name, minor_t *minor) 444681d9761SEric Taylor { 445681d9761SEric Taylor zvol_state_t *zv; 446681d9761SEric Taylor 447c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 448681d9761SEric Taylor zv = zvol_minor_lookup(name); 449681d9761SEric Taylor if (minor && zv) 450681d9761SEric Taylor *minor = zv->zv_minor; 451c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 452681d9761SEric Taylor return (zv ? 0 : -1); 453681d9761SEric Taylor } 454681d9761SEric Taylor 455e7cbe64fSgw /* 456e7cbe64fSgw * Create a minor node (plus a whole lot more) for the specified volume. 457fa9e4066Sahrens */ 458fa9e4066Sahrens int 459681d9761SEric Taylor zvol_create_minor(const char *name) 460fa9e4066Sahrens { 461c99e4bdcSChris Kirby zfs_soft_state_t *zs; 462fa9e4066Sahrens zvol_state_t *zv; 463fa9e4066Sahrens objset_t *os; 46467bd71c6Sperrin dmu_object_info_t doi; 465fa9e4066Sahrens minor_t minor = 0; 466fa9e4066Sahrens char chrbuf[30], blkbuf[30]; 467fa9e4066Sahrens int error; 468fa9e4066Sahrens 469c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 470fa9e4066Sahrens 4711195e687SMark J Musante if (zvol_minor_lookup(name) != NULL) { 472c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 473fa9e4066Sahrens return (EEXIST); 474fa9e4066Sahrens } 475fa9e4066Sahrens 476503ad85cSMatthew Ahrens /* lie and say we're read-only */ 4776e0cbcaaSMatthew Ahrens error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, FTAG, &os); 478fa9e4066Sahrens 479fa9e4066Sahrens if (error) { 480c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 481fa9e4066Sahrens return (error); 482fa9e4066Sahrens } 483fa9e4066Sahrens 484c99e4bdcSChris Kirby if ((minor = zfsdev_minor_alloc()) == 0) { 4856e0cbcaaSMatthew Ahrens dmu_objset_disown(os, FTAG); 486c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 487fa9e4066Sahrens return (ENXIO); 488fa9e4066Sahrens } 489fa9e4066Sahrens 490c99e4bdcSChris Kirby if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS) { 4916e0cbcaaSMatthew Ahrens dmu_objset_disown(os, FTAG); 492c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 493fa9e4066Sahrens return (EAGAIN); 494fa9e4066Sahrens } 495e9dbad6fSeschrock (void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME, 496e9dbad6fSeschrock (char *)name); 497fa9e4066Sahrens 498681d9761SEric Taylor (void) snprintf(chrbuf, sizeof (chrbuf), "%u,raw", minor); 499fa9e4066Sahrens 500fa9e4066Sahrens if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR, 501fa9e4066Sahrens minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 502c99e4bdcSChris Kirby ddi_soft_state_free(zfsdev_state, minor); 5036e0cbcaaSMatthew Ahrens dmu_objset_disown(os, FTAG); 504c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 505fa9e4066Sahrens return (EAGAIN); 506fa9e4066Sahrens } 507fa9e4066Sahrens 508681d9761SEric Taylor (void) snprintf(blkbuf, sizeof (blkbuf), "%u", minor); 509fa9e4066Sahrens 510fa9e4066Sahrens if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK, 511fa9e4066Sahrens minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 512fa9e4066Sahrens ddi_remove_minor_node(zfs_dip, chrbuf); 513c99e4bdcSChris Kirby ddi_soft_state_free(zfsdev_state, minor); 5146e0cbcaaSMatthew Ahrens dmu_objset_disown(os, FTAG); 515c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 516fa9e4066Sahrens return (EAGAIN); 517fa9e4066Sahrens } 518fa9e4066Sahrens 519c99e4bdcSChris Kirby zs = ddi_get_soft_state(zfsdev_state, minor); 520c99e4bdcSChris Kirby zs->zss_type = ZSST_ZVOL; 521c99e4bdcSChris Kirby zv = zs->zss_data = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP); 522681d9761SEric Taylor (void) strlcpy(zv->zv_name, name, MAXPATHLEN); 523fa9e4066Sahrens zv->zv_min_bs = DEV_BSHIFT; 524fa9e4066Sahrens zv->zv_minor = minor; 525fa9e4066Sahrens zv->zv_objset = os; 526f9af39baSGeorge Wilson if (dmu_objset_is_snapshot(os) || !spa_writeable(dmu_objset_spa(os))) 527681d9761SEric Taylor zv->zv_flags |= ZVOL_RDONLY; 528c2e6a7d6Sperrin mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL); 529c2e6a7d6Sperrin avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare, 530c2e6a7d6Sperrin sizeof (rl_t), offsetof(rl_t, r_node)); 53188b7b0f2SMatthew Ahrens list_create(&zv->zv_extents, sizeof (zvol_extent_t), 53288b7b0f2SMatthew Ahrens offsetof(zvol_extent_t, ze_node)); 53367bd71c6Sperrin /* get and cache the blocksize */ 53467bd71c6Sperrin error = dmu_object_info(os, ZVOL_OBJ, &doi); 53567bd71c6Sperrin ASSERT(error == 0); 53667bd71c6Sperrin zv->zv_volblocksize = doi.doi_data_block_size; 53722ac5be4Sperrin 538f9af39baSGeorge Wilson if (spa_writeable(dmu_objset_spa(os))) { 539f9af39baSGeorge Wilson if (zil_replay_disable) 540f9af39baSGeorge Wilson zil_destroy(dmu_objset_zil(os), B_FALSE); 541f9af39baSGeorge Wilson else 542f9af39baSGeorge Wilson zil_replay(os, zv, zvol_replay_vector); 543f9af39baSGeorge Wilson } 5446e0cbcaaSMatthew Ahrens dmu_objset_disown(os, FTAG); 545681d9761SEric Taylor zv->zv_objset = NULL; 546fa9e4066Sahrens 547fa9e4066Sahrens zvol_minors++; 548fa9e4066Sahrens 549c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 550fa9e4066Sahrens 551fa9e4066Sahrens return (0); 552fa9e4066Sahrens } 553fa9e4066Sahrens 554fa9e4066Sahrens /* 555fa9e4066Sahrens * Remove minor node for the specified volume. 556fa9e4066Sahrens */ 557681d9761SEric Taylor static int 558681d9761SEric Taylor zvol_remove_zv(zvol_state_t *zv) 559681d9761SEric Taylor { 560681d9761SEric Taylor char nmbuf[20]; 561c99e4bdcSChris Kirby minor_t minor = zv->zv_minor; 562681d9761SEric Taylor 563c99e4bdcSChris Kirby ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 564681d9761SEric Taylor if (zv->zv_total_opens != 0) 565681d9761SEric Taylor return (EBUSY); 566681d9761SEric Taylor 567c99e4bdcSChris Kirby (void) snprintf(nmbuf, sizeof (nmbuf), "%u,raw", minor); 568681d9761SEric Taylor ddi_remove_minor_node(zfs_dip, nmbuf); 569681d9761SEric Taylor 570c99e4bdcSChris Kirby (void) snprintf(nmbuf, sizeof (nmbuf), "%u", minor); 571681d9761SEric Taylor ddi_remove_minor_node(zfs_dip, nmbuf); 572681d9761SEric Taylor 573681d9761SEric Taylor avl_destroy(&zv->zv_znode.z_range_avl); 574681d9761SEric Taylor mutex_destroy(&zv->zv_znode.z_range_lock); 575681d9761SEric Taylor 576c99e4bdcSChris Kirby kmem_free(zv, sizeof (zvol_state_t)); 577c99e4bdcSChris Kirby 578c99e4bdcSChris Kirby ddi_soft_state_free(zfsdev_state, minor); 579681d9761SEric Taylor 580681d9761SEric Taylor zvol_minors--; 581681d9761SEric Taylor return (0); 582681d9761SEric Taylor } 583681d9761SEric Taylor 584fa9e4066Sahrens int 585e9dbad6fSeschrock zvol_remove_minor(const char *name) 586fa9e4066Sahrens { 587fa9e4066Sahrens zvol_state_t *zv; 588681d9761SEric Taylor int rc; 589fa9e4066Sahrens 590c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 591e9dbad6fSeschrock if ((zv = zvol_minor_lookup(name)) == NULL) { 592c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 593fa9e4066Sahrens return (ENXIO); 594fa9e4066Sahrens } 595681d9761SEric Taylor rc = zvol_remove_zv(zv); 596c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 597681d9761SEric Taylor return (rc); 598681d9761SEric Taylor } 599fa9e4066Sahrens 600681d9761SEric Taylor int 601681d9761SEric Taylor zvol_first_open(zvol_state_t *zv) 602681d9761SEric Taylor { 603681d9761SEric Taylor objset_t *os; 604681d9761SEric Taylor uint64_t volsize; 605681d9761SEric Taylor int error; 606681d9761SEric Taylor uint64_t readonly; 607fa9e4066Sahrens 608681d9761SEric Taylor /* lie and say we're read-only */ 609681d9761SEric Taylor error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, B_TRUE, 610681d9761SEric Taylor zvol_tag, &os); 611681d9761SEric Taylor if (error) 612681d9761SEric Taylor return (error); 613fa9e4066Sahrens 614c61ea566SGeorge Wilson zv->zv_objset = os; 615681d9761SEric Taylor error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize); 616681d9761SEric Taylor if (error) { 617681d9761SEric Taylor ASSERT(error == 0); 618681d9761SEric Taylor dmu_objset_disown(os, zvol_tag); 619681d9761SEric Taylor return (error); 620681d9761SEric Taylor } 621c61ea566SGeorge Wilson 62294d1a210STim Haley error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf); 62394d1a210STim Haley if (error) { 62494d1a210STim Haley dmu_objset_disown(os, zvol_tag); 62594d1a210STim Haley return (error); 62694d1a210STim Haley } 627c61ea566SGeorge Wilson 628c61ea566SGeorge Wilson zvol_size_changed(zv, volsize); 629681d9761SEric Taylor zv->zv_zilog = zil_open(os, zvol_get_data); 630fa9e4066Sahrens 631681d9761SEric Taylor VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &readonly, 632681d9761SEric Taylor NULL) == 0); 633f9af39baSGeorge Wilson if (readonly || dmu_objset_is_snapshot(os) || 634f9af39baSGeorge Wilson !spa_writeable(dmu_objset_spa(os))) 635681d9761SEric Taylor zv->zv_flags |= ZVOL_RDONLY; 636681d9761SEric Taylor else 637681d9761SEric Taylor zv->zv_flags &= ~ZVOL_RDONLY; 638681d9761SEric Taylor return (error); 639681d9761SEric Taylor } 640fa9e4066Sahrens 641681d9761SEric Taylor void 642681d9761SEric Taylor zvol_last_close(zvol_state_t *zv) 643681d9761SEric Taylor { 64422ac5be4Sperrin zil_close(zv->zv_zilog); 64522ac5be4Sperrin zv->zv_zilog = NULL; 6462e2c1355SMatthew Ahrens 64794d1a210STim Haley dmu_buf_rele(zv->zv_dbuf, zvol_tag); 64894d1a210STim Haley zv->zv_dbuf = NULL; 6492e2c1355SMatthew Ahrens 6502e2c1355SMatthew Ahrens /* 6512e2c1355SMatthew Ahrens * Evict cached data 6522e2c1355SMatthew Ahrens */ 6532e2c1355SMatthew Ahrens if (dsl_dataset_is_dirty(dmu_objset_ds(zv->zv_objset)) && 6542e2c1355SMatthew Ahrens !(zv->zv_flags & ZVOL_RDONLY)) 6552e2c1355SMatthew Ahrens txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 656*3b2aab18SMatthew Ahrens dmu_objset_evict_dbufs(zv->zv_objset); 6572e2c1355SMatthew Ahrens 658503ad85cSMatthew Ahrens dmu_objset_disown(zv->zv_objset, zvol_tag); 659fa9e4066Sahrens zv->zv_objset = NULL; 660fa9e4066Sahrens } 661fa9e4066Sahrens 662e7cbe64fSgw int 663e7cbe64fSgw zvol_prealloc(zvol_state_t *zv) 664e7cbe64fSgw { 665e7cbe64fSgw objset_t *os = zv->zv_objset; 666e7cbe64fSgw dmu_tx_t *tx; 667e7cbe64fSgw uint64_t refd, avail, usedobjs, availobjs; 668e7cbe64fSgw uint64_t resid = zv->zv_volsize; 669e7cbe64fSgw uint64_t off = 0; 670e7cbe64fSgw 671e7cbe64fSgw /* Check the space usage before attempting to allocate the space */ 672e7cbe64fSgw dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs); 673e7cbe64fSgw if (avail < zv->zv_volsize) 674e7cbe64fSgw return (ENOSPC); 675e7cbe64fSgw 676e7cbe64fSgw /* Free old extents if they exist */ 677e7cbe64fSgw zvol_free_extents(zv); 678e7cbe64fSgw 679e7cbe64fSgw while (resid != 0) { 680e7cbe64fSgw int error; 681e7cbe64fSgw uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE); 682e7cbe64fSgw 683e7cbe64fSgw tx = dmu_tx_create(os); 684e7cbe64fSgw dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 685e7cbe64fSgw error = dmu_tx_assign(tx, TXG_WAIT); 686e7cbe64fSgw if (error) { 687e7cbe64fSgw dmu_tx_abort(tx); 688cdb0ab79Smaybee (void) dmu_free_long_range(os, ZVOL_OBJ, 0, off); 689e7cbe64fSgw return (error); 690e7cbe64fSgw } 69182c9918fSTim Haley dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx); 692e7cbe64fSgw dmu_tx_commit(tx); 693e7cbe64fSgw off += bytes; 694e7cbe64fSgw resid -= bytes; 695e7cbe64fSgw } 696e7cbe64fSgw txg_wait_synced(dmu_objset_pool(os), 0); 697e7cbe64fSgw 698e7cbe64fSgw return (0); 699e7cbe64fSgw } 700e7cbe64fSgw 701*3b2aab18SMatthew Ahrens static int 702681d9761SEric Taylor zvol_update_volsize(objset_t *os, uint64_t volsize) 703e7cbe64fSgw { 704e7cbe64fSgw dmu_tx_t *tx; 705e7cbe64fSgw int error; 706e7cbe64fSgw 707c99e4bdcSChris Kirby ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 708e7cbe64fSgw 709681d9761SEric Taylor tx = dmu_tx_create(os); 710e7cbe64fSgw dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 711e7cbe64fSgw error = dmu_tx_assign(tx, TXG_WAIT); 712e7cbe64fSgw if (error) { 713e7cbe64fSgw dmu_tx_abort(tx); 714e7cbe64fSgw return (error); 715e7cbe64fSgw } 716e7cbe64fSgw 717681d9761SEric Taylor error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, 718e7cbe64fSgw &volsize, tx); 719e7cbe64fSgw dmu_tx_commit(tx); 720e7cbe64fSgw 721e7cbe64fSgw if (error == 0) 722681d9761SEric Taylor error = dmu_free_long_range(os, 723cdb0ab79Smaybee ZVOL_OBJ, volsize, DMU_OBJECT_END); 724681d9761SEric Taylor return (error); 725681d9761SEric Taylor } 726e7cbe64fSgw 727681d9761SEric Taylor void 728681d9761SEric Taylor zvol_remove_minors(const char *name) 729681d9761SEric Taylor { 730681d9761SEric Taylor zvol_state_t *zv; 731681d9761SEric Taylor char *namebuf; 732681d9761SEric Taylor minor_t minor; 733681d9761SEric Taylor 734681d9761SEric Taylor namebuf = kmem_zalloc(strlen(name) + 2, KM_SLEEP); 735681d9761SEric Taylor (void) strncpy(namebuf, name, strlen(name)); 736681d9761SEric Taylor (void) strcat(namebuf, "/"); 737c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 738c99e4bdcSChris Kirby for (minor = 1; minor <= ZFSDEV_MAX_MINOR; minor++) { 739681d9761SEric Taylor 740c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 741681d9761SEric Taylor if (zv == NULL) 742681d9761SEric Taylor continue; 743681d9761SEric Taylor if (strncmp(namebuf, zv->zv_name, strlen(namebuf)) == 0) 744681d9761SEric Taylor (void) zvol_remove_zv(zv); 745e7cbe64fSgw } 746681d9761SEric Taylor kmem_free(namebuf, strlen(name) + 2); 747681d9761SEric Taylor 748c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 749e7cbe64fSgw } 750e7cbe64fSgw 751c61ea566SGeorge Wilson static int 752*3b2aab18SMatthew Ahrens zvol_update_live_volsize(zvol_state_t *zv, uint64_t volsize) 753fa9e4066Sahrens { 754e7cbe64fSgw uint64_t old_volsize = 0ULL; 755*3b2aab18SMatthew Ahrens int error = 0; 756fa9e4066Sahrens 757c61ea566SGeorge Wilson ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 758c61ea566SGeorge Wilson 759e7cbe64fSgw /* 760e7cbe64fSgw * Reinitialize the dump area to the new size. If we 761681d9761SEric Taylor * failed to resize the dump area then restore it back to 762c61ea566SGeorge Wilson * its original size. We must set the new volsize prior 763c61ea566SGeorge Wilson * to calling dumpvp_resize() to ensure that the devices' 764c61ea566SGeorge Wilson * size(9P) is not visible by the dump subsystem. 765e7cbe64fSgw */ 766*3b2aab18SMatthew Ahrens old_volsize = zv->zv_volsize; 767*3b2aab18SMatthew Ahrens zvol_size_changed(zv, volsize); 768*3b2aab18SMatthew Ahrens 769*3b2aab18SMatthew Ahrens if (zv->zv_flags & ZVOL_DUMPIFIED) { 770*3b2aab18SMatthew Ahrens if ((error = zvol_dumpify(zv)) != 0 || 771*3b2aab18SMatthew Ahrens (error = dumpvp_resize()) != 0) { 772*3b2aab18SMatthew Ahrens int dumpify_error; 773*3b2aab18SMatthew Ahrens 774*3b2aab18SMatthew Ahrens (void) zvol_update_volsize(zv->zv_objset, old_volsize); 775*3b2aab18SMatthew Ahrens zvol_size_changed(zv, old_volsize); 776*3b2aab18SMatthew Ahrens dumpify_error = zvol_dumpify(zv); 777*3b2aab18SMatthew Ahrens error = dumpify_error ? dumpify_error : error; 778681d9761SEric Taylor } 779fa9e4066Sahrens } 780fa9e4066Sahrens 781573ca77eSGeorge Wilson /* 782573ca77eSGeorge Wilson * Generate a LUN expansion event. 783573ca77eSGeorge Wilson */ 784*3b2aab18SMatthew Ahrens if (error == 0) { 785573ca77eSGeorge Wilson sysevent_id_t eid; 786573ca77eSGeorge Wilson nvlist_t *attr; 787573ca77eSGeorge Wilson char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 788573ca77eSGeorge Wilson 789681d9761SEric Taylor (void) snprintf(physpath, MAXPATHLEN, "%s%u", ZVOL_PSEUDO_DEV, 790573ca77eSGeorge Wilson zv->zv_minor); 791573ca77eSGeorge Wilson 792573ca77eSGeorge Wilson VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 793573ca77eSGeorge Wilson VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 794573ca77eSGeorge Wilson 795573ca77eSGeorge Wilson (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 796573ca77eSGeorge Wilson ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 797573ca77eSGeorge Wilson 798573ca77eSGeorge Wilson nvlist_free(attr); 799573ca77eSGeorge Wilson kmem_free(physpath, MAXPATHLEN); 800573ca77eSGeorge Wilson } 801c61ea566SGeorge Wilson return (error); 802c61ea566SGeorge Wilson } 803573ca77eSGeorge Wilson 804c61ea566SGeorge Wilson int 805c61ea566SGeorge Wilson zvol_set_volsize(const char *name, uint64_t volsize) 806c61ea566SGeorge Wilson { 807c61ea566SGeorge Wilson zvol_state_t *zv = NULL; 808c61ea566SGeorge Wilson objset_t *os; 809c61ea566SGeorge Wilson int error; 810c61ea566SGeorge Wilson dmu_object_info_t doi; 811c61ea566SGeorge Wilson uint64_t readonly; 812*3b2aab18SMatthew Ahrens boolean_t owned = B_FALSE; 813*3b2aab18SMatthew Ahrens 814*3b2aab18SMatthew Ahrens error = dsl_prop_get_integer(name, 815*3b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_READONLY), &readonly, NULL); 816*3b2aab18SMatthew Ahrens if (error != 0) 817*3b2aab18SMatthew Ahrens return (error); 818*3b2aab18SMatthew Ahrens if (readonly) 819*3b2aab18SMatthew Ahrens return (EROFS); 820c61ea566SGeorge Wilson 821c61ea566SGeorge Wilson mutex_enter(&zfsdev_state_lock); 822c61ea566SGeorge Wilson zv = zvol_minor_lookup(name); 823*3b2aab18SMatthew Ahrens 824*3b2aab18SMatthew Ahrens if (zv == NULL || zv->zv_objset == NULL) { 825*3b2aab18SMatthew Ahrens if ((error = dmu_objset_own(name, DMU_OST_ZVOL, B_FALSE, 826*3b2aab18SMatthew Ahrens FTAG, &os)) != 0) { 827*3b2aab18SMatthew Ahrens mutex_exit(&zfsdev_state_lock); 828*3b2aab18SMatthew Ahrens return (error); 829*3b2aab18SMatthew Ahrens } 830*3b2aab18SMatthew Ahrens owned = B_TRUE; 831*3b2aab18SMatthew Ahrens if (zv != NULL) 832*3b2aab18SMatthew Ahrens zv->zv_objset = os; 833*3b2aab18SMatthew Ahrens } else { 834*3b2aab18SMatthew Ahrens os = zv->zv_objset; 835c61ea566SGeorge Wilson } 836c61ea566SGeorge Wilson 837c61ea566SGeorge Wilson if ((error = dmu_object_info(os, ZVOL_OBJ, &doi)) != 0 || 838*3b2aab18SMatthew Ahrens (error = zvol_check_volsize(volsize, doi.doi_data_block_size)) != 0) 839c61ea566SGeorge Wilson goto out; 840c61ea566SGeorge Wilson 841*3b2aab18SMatthew Ahrens error = zvol_update_volsize(os, volsize); 842c61ea566SGeorge Wilson 843*3b2aab18SMatthew Ahrens if (error == 0 && zv != NULL) 844*3b2aab18SMatthew Ahrens error = zvol_update_live_volsize(zv, volsize); 845bb0ade09Sahrens out: 846*3b2aab18SMatthew Ahrens if (owned) { 847*3b2aab18SMatthew Ahrens dmu_objset_disown(os, FTAG); 848*3b2aab18SMatthew Ahrens if (zv != NULL) 849*3b2aab18SMatthew Ahrens zv->zv_objset = NULL; 850*3b2aab18SMatthew Ahrens } 851c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 852fa9e4066Sahrens return (error); 853fa9e4066Sahrens } 854fa9e4066Sahrens 855fa9e4066Sahrens /*ARGSUSED*/ 856fa9e4066Sahrens int 857fa9e4066Sahrens zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr) 858fa9e4066Sahrens { 859fa9e4066Sahrens zvol_state_t *zv; 860681d9761SEric Taylor int err = 0; 861fa9e4066Sahrens 862c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 863fa9e4066Sahrens 864c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(getminor(*devp), ZSST_ZVOL); 865fa9e4066Sahrens if (zv == NULL) { 866c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 867fa9e4066Sahrens return (ENXIO); 868fa9e4066Sahrens } 869fa9e4066Sahrens 870681d9761SEric Taylor if (zv->zv_total_opens == 0) 871681d9761SEric Taylor err = zvol_first_open(zv); 872681d9761SEric Taylor if (err) { 873c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 874681d9761SEric Taylor return (err); 875681d9761SEric Taylor } 876681d9761SEric Taylor if ((flag & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) { 877681d9761SEric Taylor err = EROFS; 878681d9761SEric Taylor goto out; 879fa9e4066Sahrens } 880c7f714e2SEric Taylor if (zv->zv_flags & ZVOL_EXCL) { 881681d9761SEric Taylor err = EBUSY; 882681d9761SEric Taylor goto out; 883c7f714e2SEric Taylor } 884c7f714e2SEric Taylor if (flag & FEXCL) { 885c7f714e2SEric Taylor if (zv->zv_total_opens != 0) { 886681d9761SEric Taylor err = EBUSY; 887681d9761SEric Taylor goto out; 888c7f714e2SEric Taylor } 889c7f714e2SEric Taylor zv->zv_flags |= ZVOL_EXCL; 890c7f714e2SEric Taylor } 891fa9e4066Sahrens 892fa9e4066Sahrens if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) { 893fa9e4066Sahrens zv->zv_open_count[otyp]++; 894fa9e4066Sahrens zv->zv_total_opens++; 895fa9e4066Sahrens } 896c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 897fa9e4066Sahrens 898681d9761SEric Taylor return (err); 899681d9761SEric Taylor out: 900681d9761SEric Taylor if (zv->zv_total_opens == 0) 901681d9761SEric Taylor zvol_last_close(zv); 902c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 903681d9761SEric Taylor return (err); 904fa9e4066Sahrens } 905fa9e4066Sahrens 906fa9e4066Sahrens /*ARGSUSED*/ 907fa9e4066Sahrens int 908fa9e4066Sahrens zvol_close(dev_t dev, int flag, int otyp, cred_t *cr) 909fa9e4066Sahrens { 910fa9e4066Sahrens minor_t minor = getminor(dev); 911fa9e4066Sahrens zvol_state_t *zv; 912681d9761SEric Taylor int error = 0; 913fa9e4066Sahrens 914c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 915fa9e4066Sahrens 916c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 917fa9e4066Sahrens if (zv == NULL) { 918c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 919fa9e4066Sahrens return (ENXIO); 920fa9e4066Sahrens } 921fa9e4066Sahrens 922c7f714e2SEric Taylor if (zv->zv_flags & ZVOL_EXCL) { 923c7f714e2SEric Taylor ASSERT(zv->zv_total_opens == 1); 924c7f714e2SEric Taylor zv->zv_flags &= ~ZVOL_EXCL; 925fa9e4066Sahrens } 926fa9e4066Sahrens 927fa9e4066Sahrens /* 928fa9e4066Sahrens * If the open count is zero, this is a spurious close. 929fa9e4066Sahrens * That indicates a bug in the kernel / DDI framework. 930fa9e4066Sahrens */ 931fa9e4066Sahrens ASSERT(zv->zv_open_count[otyp] != 0); 932fa9e4066Sahrens ASSERT(zv->zv_total_opens != 0); 933fa9e4066Sahrens 934fa9e4066Sahrens /* 935fa9e4066Sahrens * You may get multiple opens, but only one close. 936fa9e4066Sahrens */ 937fa9e4066Sahrens zv->zv_open_count[otyp]--; 938fa9e4066Sahrens zv->zv_total_opens--; 939fa9e4066Sahrens 940681d9761SEric Taylor if (zv->zv_total_opens == 0) 941681d9761SEric Taylor zvol_last_close(zv); 942fa9e4066Sahrens 943c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 944681d9761SEric Taylor return (error); 945fa9e4066Sahrens } 946fa9e4066Sahrens 947feb08c6bSbillm static void 948b24ab676SJeff Bonwick zvol_get_done(zgd_t *zgd, int error) 94967bd71c6Sperrin { 950b24ab676SJeff Bonwick if (zgd->zgd_db) 951b24ab676SJeff Bonwick dmu_buf_rele(zgd->zgd_db, zgd); 952b24ab676SJeff Bonwick 953b24ab676SJeff Bonwick zfs_range_unlock(zgd->zgd_rl); 954b24ab676SJeff Bonwick 955b24ab676SJeff Bonwick if (error == 0 && zgd->zgd_bp) 956b24ab676SJeff Bonwick zil_add_block(zgd->zgd_zilog, zgd->zgd_bp); 95767bd71c6Sperrin 95867bd71c6Sperrin kmem_free(zgd, sizeof (zgd_t)); 95967bd71c6Sperrin } 96067bd71c6Sperrin 96167bd71c6Sperrin /* 96267bd71c6Sperrin * Get data to generate a TX_WRITE intent log record. 96367bd71c6Sperrin */ 964feb08c6bSbillm static int 96567bd71c6Sperrin zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 96667bd71c6Sperrin { 96767bd71c6Sperrin zvol_state_t *zv = arg; 96867bd71c6Sperrin objset_t *os = zv->zv_objset; 969b24ab676SJeff Bonwick uint64_t object = ZVOL_OBJ; 970b24ab676SJeff Bonwick uint64_t offset = lr->lr_offset; 971b24ab676SJeff Bonwick uint64_t size = lr->lr_length; /* length of user data */ 972b24ab676SJeff Bonwick blkptr_t *bp = &lr->lr_blkptr; 97367bd71c6Sperrin dmu_buf_t *db; 97467bd71c6Sperrin zgd_t *zgd; 97567bd71c6Sperrin int error; 97667bd71c6Sperrin 977b24ab676SJeff Bonwick ASSERT(zio != NULL); 978b24ab676SJeff Bonwick ASSERT(size != 0); 979b24ab676SJeff Bonwick 980b24ab676SJeff Bonwick zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP); 981b24ab676SJeff Bonwick zgd->zgd_zilog = zv->zv_zilog; 982b24ab676SJeff Bonwick zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER); 983feb08c6bSbillm 984c2e6a7d6Sperrin /* 985c2e6a7d6Sperrin * Write records come in two flavors: immediate and indirect. 986c2e6a7d6Sperrin * For small writes it's cheaper to store the data with the 987c2e6a7d6Sperrin * log record (immediate); for large writes it's cheaper to 988c2e6a7d6Sperrin * sync the data and get a pointer to it (indirect) so that 989c2e6a7d6Sperrin * we don't have to write the data twice. 990c2e6a7d6Sperrin */ 991b24ab676SJeff Bonwick if (buf != NULL) { /* immediate write */ 992b24ab676SJeff Bonwick error = dmu_read(os, object, offset, size, buf, 993b24ab676SJeff Bonwick DMU_READ_NO_PREFETCH); 994b24ab676SJeff Bonwick } else { 995b24ab676SJeff Bonwick size = zv->zv_volblocksize; 996b24ab676SJeff Bonwick offset = P2ALIGN(offset, size); 99747cb52daSJeff Bonwick error = dmu_buf_hold(os, object, offset, zgd, &db, 99847cb52daSJeff Bonwick DMU_READ_NO_PREFETCH); 999b24ab676SJeff Bonwick if (error == 0) { 100080901aeaSGeorge Wilson blkptr_t *obp = dmu_buf_get_blkptr(db); 100180901aeaSGeorge Wilson if (obp) { 100280901aeaSGeorge Wilson ASSERT(BP_IS_HOLE(bp)); 100380901aeaSGeorge Wilson *bp = *obp; 100480901aeaSGeorge Wilson } 100580901aeaSGeorge Wilson 1006b24ab676SJeff Bonwick zgd->zgd_db = db; 1007b24ab676SJeff Bonwick zgd->zgd_bp = bp; 100867bd71c6Sperrin 1009b24ab676SJeff Bonwick ASSERT(db->db_offset == offset); 1010b24ab676SJeff Bonwick ASSERT(db->db_size == size); 101167bd71c6Sperrin 1012b24ab676SJeff Bonwick error = dmu_sync(zio, lr->lr_common.lrc_txg, 1013b24ab676SJeff Bonwick zvol_get_done, zgd); 1014975c32a0SNeil Perrin 1015b24ab676SJeff Bonwick if (error == 0) 1016b24ab676SJeff Bonwick return (0); 1017b24ab676SJeff Bonwick } 1018975c32a0SNeil Perrin } 1019975c32a0SNeil Perrin 1020b24ab676SJeff Bonwick zvol_get_done(zgd, error); 1021b24ab676SJeff Bonwick 102267bd71c6Sperrin return (error); 102367bd71c6Sperrin } 102467bd71c6Sperrin 1025a24e15ceSperrin /* 1026a24e15ceSperrin * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions. 102722ac5be4Sperrin * 102822ac5be4Sperrin * We store data in the log buffers if it's small enough. 102967bd71c6Sperrin * Otherwise we will later flush the data out via dmu_sync(). 103022ac5be4Sperrin */ 103167bd71c6Sperrin ssize_t zvol_immediate_write_sz = 32768; 103222ac5be4Sperrin 1033feb08c6bSbillm static void 1034510b6c0eSNeil Perrin zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid, 1035510b6c0eSNeil Perrin boolean_t sync) 103622ac5be4Sperrin { 1037feb08c6bSbillm uint32_t blocksize = zv->zv_volblocksize; 10381209a471SNeil Perrin zilog_t *zilog = zv->zv_zilog; 1039510b6c0eSNeil Perrin boolean_t slogging; 1040e09fa4daSNeil Perrin ssize_t immediate_write_sz; 1041510b6c0eSNeil Perrin 1042b24ab676SJeff Bonwick if (zil_replaying(zilog, tx)) 10431209a471SNeil Perrin return; 10441209a471SNeil Perrin 1045e09fa4daSNeil Perrin immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT) 1046e09fa4daSNeil Perrin ? 0 : zvol_immediate_write_sz; 1047e09fa4daSNeil Perrin 1048e09fa4daSNeil Perrin slogging = spa_has_slogs(zilog->zl_spa) && 1049e09fa4daSNeil Perrin (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY); 1050feb08c6bSbillm 1051510b6c0eSNeil Perrin while (resid) { 1052510b6c0eSNeil Perrin itx_t *itx; 1053510b6c0eSNeil Perrin lr_write_t *lr; 1054510b6c0eSNeil Perrin ssize_t len; 1055510b6c0eSNeil Perrin itx_wr_state_t write_state; 1056510b6c0eSNeil Perrin 1057510b6c0eSNeil Perrin /* 1058510b6c0eSNeil Perrin * Unlike zfs_log_write() we can be called with 1059510b6c0eSNeil Perrin * upto DMU_MAX_ACCESS/2 (5MB) writes. 1060510b6c0eSNeil Perrin */ 1061e09fa4daSNeil Perrin if (blocksize > immediate_write_sz && !slogging && 1062510b6c0eSNeil Perrin resid >= blocksize && off % blocksize == 0) { 1063510b6c0eSNeil Perrin write_state = WR_INDIRECT; /* uses dmu_sync */ 1064510b6c0eSNeil Perrin len = blocksize; 1065510b6c0eSNeil Perrin } else if (sync) { 1066510b6c0eSNeil Perrin write_state = WR_COPIED; 1067510b6c0eSNeil Perrin len = MIN(ZIL_MAX_LOG_DATA, resid); 1068510b6c0eSNeil Perrin } else { 1069510b6c0eSNeil Perrin write_state = WR_NEED_COPY; 1070510b6c0eSNeil Perrin len = MIN(ZIL_MAX_LOG_DATA, resid); 1071510b6c0eSNeil Perrin } 1072510b6c0eSNeil Perrin 1073510b6c0eSNeil Perrin itx = zil_itx_create(TX_WRITE, sizeof (*lr) + 1074510b6c0eSNeil Perrin (write_state == WR_COPIED ? len : 0)); 1075feb08c6bSbillm lr = (lr_write_t *)&itx->itx_lr; 1076510b6c0eSNeil Perrin if (write_state == WR_COPIED && dmu_read(zv->zv_objset, 10777bfdf011SNeil Perrin ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) { 1078b24ab676SJeff Bonwick zil_itx_destroy(itx); 1079510b6c0eSNeil Perrin itx = zil_itx_create(TX_WRITE, sizeof (*lr)); 1080510b6c0eSNeil Perrin lr = (lr_write_t *)&itx->itx_lr; 1081510b6c0eSNeil Perrin write_state = WR_NEED_COPY; 1082510b6c0eSNeil Perrin } 1083510b6c0eSNeil Perrin 1084510b6c0eSNeil Perrin itx->itx_wr_state = write_state; 1085510b6c0eSNeil Perrin if (write_state == WR_NEED_COPY) 1086510b6c0eSNeil Perrin itx->itx_sod += len; 1087feb08c6bSbillm lr->lr_foid = ZVOL_OBJ; 1088feb08c6bSbillm lr->lr_offset = off; 1089510b6c0eSNeil Perrin lr->lr_length = len; 1090b24ab676SJeff Bonwick lr->lr_blkoff = 0; 1091feb08c6bSbillm BP_ZERO(&lr->lr_blkptr); 1092feb08c6bSbillm 1093510b6c0eSNeil Perrin itx->itx_private = zv; 1094510b6c0eSNeil Perrin itx->itx_sync = sync; 1095510b6c0eSNeil Perrin 10965002558fSNeil Perrin zil_itx_assign(zilog, itx, tx); 1097510b6c0eSNeil Perrin 1098510b6c0eSNeil Perrin off += len; 1099510b6c0eSNeil Perrin resid -= len; 110022ac5be4Sperrin } 110122ac5be4Sperrin } 110222ac5be4Sperrin 110388b7b0f2SMatthew Ahrens static int 110488b7b0f2SMatthew Ahrens zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t size, 110588b7b0f2SMatthew Ahrens boolean_t doread, boolean_t isdump) 1106e7cbe64fSgw { 1107e7cbe64fSgw vdev_disk_t *dvd; 1108e7cbe64fSgw int c; 1109e7cbe64fSgw int numerrors = 0; 1110e7cbe64fSgw 1111e7cbe64fSgw for (c = 0; c < vd->vdev_children; c++) { 111221ecdf64SLin Ling ASSERT(vd->vdev_ops == &vdev_mirror_ops || 111321ecdf64SLin Ling vd->vdev_ops == &vdev_replacing_ops || 111421ecdf64SLin Ling vd->vdev_ops == &vdev_spare_ops); 111588b7b0f2SMatthew Ahrens int err = zvol_dumpio_vdev(vd->vdev_child[c], 111688b7b0f2SMatthew Ahrens addr, offset, size, doread, isdump); 111788b7b0f2SMatthew Ahrens if (err != 0) { 1118e7cbe64fSgw numerrors++; 111988b7b0f2SMatthew Ahrens } else if (doread) { 1120e7cbe64fSgw break; 1121e7cbe64fSgw } 1122e7cbe64fSgw } 1123e7cbe64fSgw 1124e7cbe64fSgw if (!vd->vdev_ops->vdev_op_leaf) 1125e7cbe64fSgw return (numerrors < vd->vdev_children ? 0 : EIO); 1126e7cbe64fSgw 1127dc0bb255SEric Taylor if (doread && !vdev_readable(vd)) 1128dc0bb255SEric Taylor return (EIO); 1129dc0bb255SEric Taylor else if (!doread && !vdev_writeable(vd)) 1130e7cbe64fSgw return (EIO); 1131e7cbe64fSgw 1132e7cbe64fSgw dvd = vd->vdev_tsd; 1133e7cbe64fSgw ASSERT3P(dvd, !=, NULL); 1134e7cbe64fSgw offset += VDEV_LABEL_START_SIZE; 1135e7cbe64fSgw 1136e7cbe64fSgw if (ddi_in_panic() || isdump) { 113788b7b0f2SMatthew Ahrens ASSERT(!doread); 113888b7b0f2SMatthew Ahrens if (doread) 1139e7cbe64fSgw return (EIO); 1140e7cbe64fSgw return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset), 1141e7cbe64fSgw lbtodb(size))); 1142e7cbe64fSgw } else { 1143e7cbe64fSgw return (vdev_disk_physio(dvd->vd_lh, addr, size, offset, 114488b7b0f2SMatthew Ahrens doread ? B_READ : B_WRITE)); 1145e7cbe64fSgw } 1146e7cbe64fSgw } 1147e7cbe64fSgw 114888b7b0f2SMatthew Ahrens static int 114988b7b0f2SMatthew Ahrens zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size, 115088b7b0f2SMatthew Ahrens boolean_t doread, boolean_t isdump) 1151e7cbe64fSgw { 1152e7cbe64fSgw vdev_t *vd; 1153e7cbe64fSgw int error; 115488b7b0f2SMatthew Ahrens zvol_extent_t *ze; 1155e7cbe64fSgw spa_t *spa = dmu_objset_spa(zv->zv_objset); 1156e7cbe64fSgw 115788b7b0f2SMatthew Ahrens /* Must be sector aligned, and not stradle a block boundary. */ 115888b7b0f2SMatthew Ahrens if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) || 115988b7b0f2SMatthew Ahrens P2BOUNDARY(offset, size, zv->zv_volblocksize)) { 1160e7cbe64fSgw return (EINVAL); 116188b7b0f2SMatthew Ahrens } 116288b7b0f2SMatthew Ahrens ASSERT(size <= zv->zv_volblocksize); 1163e7cbe64fSgw 116488b7b0f2SMatthew Ahrens /* Locate the extent this belongs to */ 116588b7b0f2SMatthew Ahrens ze = list_head(&zv->zv_extents); 116688b7b0f2SMatthew Ahrens while (offset >= ze->ze_nblks * zv->zv_volblocksize) { 116788b7b0f2SMatthew Ahrens offset -= ze->ze_nblks * zv->zv_volblocksize; 116888b7b0f2SMatthew Ahrens ze = list_next(&zv->zv_extents, ze); 116988b7b0f2SMatthew Ahrens } 117024cc0e1cSGeorge Wilson 1171*3b2aab18SMatthew Ahrens if (ze == NULL) 1172*3b2aab18SMatthew Ahrens return (EINVAL); 1173*3b2aab18SMatthew Ahrens 117424cc0e1cSGeorge Wilson if (!ddi_in_panic()) 117524cc0e1cSGeorge Wilson spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 117624cc0e1cSGeorge Wilson 117788b7b0f2SMatthew Ahrens vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva)); 117888b7b0f2SMatthew Ahrens offset += DVA_GET_OFFSET(&ze->ze_dva); 117988b7b0f2SMatthew Ahrens error = zvol_dumpio_vdev(vd, addr, offset, size, doread, isdump); 118024cc0e1cSGeorge Wilson 118124cc0e1cSGeorge Wilson if (!ddi_in_panic()) 118224cc0e1cSGeorge Wilson spa_config_exit(spa, SCL_STATE, FTAG); 118324cc0e1cSGeorge Wilson 1184e7cbe64fSgw return (error); 1185e7cbe64fSgw } 1186e7cbe64fSgw 1187fa9e4066Sahrens int 1188fa9e4066Sahrens zvol_strategy(buf_t *bp) 1189fa9e4066Sahrens { 1190c99e4bdcSChris Kirby zfs_soft_state_t *zs = NULL; 1191c99e4bdcSChris Kirby zvol_state_t *zv; 1192fa9e4066Sahrens uint64_t off, volsize; 119388b7b0f2SMatthew Ahrens size_t resid; 1194fa9e4066Sahrens char *addr; 119522ac5be4Sperrin objset_t *os; 1196c2e6a7d6Sperrin rl_t *rl; 1197fa9e4066Sahrens int error = 0; 119888b7b0f2SMatthew Ahrens boolean_t doread = bp->b_flags & B_READ; 1199f80ce222SChris Kirby boolean_t is_dump; 1200510b6c0eSNeil Perrin boolean_t sync; 1201fa9e4066Sahrens 1202c99e4bdcSChris Kirby if (getminor(bp->b_edev) == 0) { 1203c99e4bdcSChris Kirby error = EINVAL; 1204c99e4bdcSChris Kirby } else { 1205c99e4bdcSChris Kirby zs = ddi_get_soft_state(zfsdev_state, getminor(bp->b_edev)); 1206c99e4bdcSChris Kirby if (zs == NULL) 1207c99e4bdcSChris Kirby error = ENXIO; 1208c99e4bdcSChris Kirby else if (zs->zss_type != ZSST_ZVOL) 1209c99e4bdcSChris Kirby error = EINVAL; 1210fa9e4066Sahrens } 1211fa9e4066Sahrens 1212c99e4bdcSChris Kirby if (error) { 1213c99e4bdcSChris Kirby bioerror(bp, error); 1214fa9e4066Sahrens biodone(bp); 1215fa9e4066Sahrens return (0); 1216fa9e4066Sahrens } 1217fa9e4066Sahrens 1218c99e4bdcSChris Kirby zv = zs->zss_data; 1219c99e4bdcSChris Kirby 1220681d9761SEric Taylor if (!(bp->b_flags & B_READ) && (zv->zv_flags & ZVOL_RDONLY)) { 1221fa9e4066Sahrens bioerror(bp, EROFS); 1222fa9e4066Sahrens biodone(bp); 1223fa9e4066Sahrens return (0); 1224fa9e4066Sahrens } 1225fa9e4066Sahrens 1226fa9e4066Sahrens off = ldbtob(bp->b_blkno); 1227fa9e4066Sahrens volsize = zv->zv_volsize; 1228fa9e4066Sahrens 122922ac5be4Sperrin os = zv->zv_objset; 123022ac5be4Sperrin ASSERT(os != NULL); 1231fa9e4066Sahrens 1232fa9e4066Sahrens bp_mapin(bp); 1233fa9e4066Sahrens addr = bp->b_un.b_addr; 1234fa9e4066Sahrens resid = bp->b_bcount; 1235fa9e4066Sahrens 123688b7b0f2SMatthew Ahrens if (resid > 0 && (off < 0 || off >= volsize)) { 123788b7b0f2SMatthew Ahrens bioerror(bp, EIO); 123888b7b0f2SMatthew Ahrens biodone(bp); 123988b7b0f2SMatthew Ahrens return (0); 124088b7b0f2SMatthew Ahrens } 124173ec3d9cSgw 1242f80ce222SChris Kirby is_dump = zv->zv_flags & ZVOL_DUMPIFIED; 124355da60b9SMark J Musante sync = ((!(bp->b_flags & B_ASYNC) && 124455da60b9SMark J Musante !(zv->zv_flags & ZVOL_WCE)) || 124555da60b9SMark J Musante (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)) && 124655da60b9SMark J Musante !doread && !is_dump; 1247510b6c0eSNeil Perrin 1248a24e15ceSperrin /* 1249a24e15ceSperrin * There must be no buffer changes when doing a dmu_sync() because 1250a24e15ceSperrin * we can't change the data whilst calculating the checksum. 1251a24e15ceSperrin */ 1252c2e6a7d6Sperrin rl = zfs_range_lock(&zv->zv_znode, off, resid, 125388b7b0f2SMatthew Ahrens doread ? RL_READER : RL_WRITER); 1254fa9e4066Sahrens 1255e7cbe64fSgw while (resid != 0 && off < volsize) { 125688b7b0f2SMatthew Ahrens size_t size = MIN(resid, zvol_maxphys); 1257e7cbe64fSgw if (is_dump) { 1258e7cbe64fSgw size = MIN(size, P2END(off, zv->zv_volblocksize) - off); 125988b7b0f2SMatthew Ahrens error = zvol_dumpio(zv, addr, off, size, 126088b7b0f2SMatthew Ahrens doread, B_FALSE); 126188b7b0f2SMatthew Ahrens } else if (doread) { 12627bfdf011SNeil Perrin error = dmu_read(os, ZVOL_OBJ, off, size, addr, 12637bfdf011SNeil Perrin DMU_READ_PREFETCH); 1264fa9e4066Sahrens } else { 126522ac5be4Sperrin dmu_tx_t *tx = dmu_tx_create(os); 1266fa9e4066Sahrens dmu_tx_hold_write(tx, ZVOL_OBJ, off, size); 1267fa9e4066Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 1268fa9e4066Sahrens if (error) { 1269fa9e4066Sahrens dmu_tx_abort(tx); 1270fa9e4066Sahrens } else { 127122ac5be4Sperrin dmu_write(os, ZVOL_OBJ, off, size, addr, tx); 1272510b6c0eSNeil Perrin zvol_log_write(zv, tx, off, size, sync); 1273fa9e4066Sahrens dmu_tx_commit(tx); 1274fa9e4066Sahrens } 1275fa9e4066Sahrens } 1276b87f3af3Sperrin if (error) { 1277b87f3af3Sperrin /* convert checksum errors into IO errors */ 1278b87f3af3Sperrin if (error == ECKSUM) 1279b87f3af3Sperrin error = EIO; 1280fa9e4066Sahrens break; 1281b87f3af3Sperrin } 1282fa9e4066Sahrens off += size; 1283fa9e4066Sahrens addr += size; 1284fa9e4066Sahrens resid -= size; 1285fa9e4066Sahrens } 1286c2e6a7d6Sperrin zfs_range_unlock(rl); 1287fa9e4066Sahrens 1288fa9e4066Sahrens if ((bp->b_resid = resid) == bp->b_bcount) 1289fa9e4066Sahrens bioerror(bp, off > volsize ? EINVAL : error); 1290fa9e4066Sahrens 1291510b6c0eSNeil Perrin if (sync) 12925002558fSNeil Perrin zil_commit(zv->zv_zilog, ZVOL_OBJ); 1293feb08c6bSbillm biodone(bp); 129422ac5be4Sperrin 1295fa9e4066Sahrens return (0); 1296fa9e4066Sahrens } 1297fa9e4066Sahrens 129867bd71c6Sperrin /* 129967bd71c6Sperrin * Set the buffer count to the zvol maximum transfer. 130067bd71c6Sperrin * Using our own routine instead of the default minphys() 130167bd71c6Sperrin * means that for larger writes we write bigger buffers on X86 130267bd71c6Sperrin * (128K instead of 56K) and flush the disk write cache less often 130367bd71c6Sperrin * (every zvol_maxphys - currently 1MB) instead of minphys (currently 130467bd71c6Sperrin * 56K on X86 and 128K on sparc). 130567bd71c6Sperrin */ 130667bd71c6Sperrin void 130767bd71c6Sperrin zvol_minphys(struct buf *bp) 130867bd71c6Sperrin { 130967bd71c6Sperrin if (bp->b_bcount > zvol_maxphys) 131067bd71c6Sperrin bp->b_bcount = zvol_maxphys; 131167bd71c6Sperrin } 131267bd71c6Sperrin 1313e7cbe64fSgw int 1314e7cbe64fSgw zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks) 1315e7cbe64fSgw { 1316e7cbe64fSgw minor_t minor = getminor(dev); 1317e7cbe64fSgw zvol_state_t *zv; 1318e7cbe64fSgw int error = 0; 1319e7cbe64fSgw uint64_t size; 1320e7cbe64fSgw uint64_t boff; 1321e7cbe64fSgw uint64_t resid; 1322e7cbe64fSgw 1323c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1324e7cbe64fSgw if (zv == NULL) 1325e7cbe64fSgw return (ENXIO); 1326e7cbe64fSgw 1327*3b2aab18SMatthew Ahrens if ((zv->zv_flags & ZVOL_DUMPIFIED) == 0) 1328*3b2aab18SMatthew Ahrens return (EINVAL); 1329*3b2aab18SMatthew Ahrens 1330e7cbe64fSgw boff = ldbtob(blkno); 1331e7cbe64fSgw resid = ldbtob(nblocks); 133288b7b0f2SMatthew Ahrens 133388b7b0f2SMatthew Ahrens VERIFY3U(boff + resid, <=, zv->zv_volsize); 133488b7b0f2SMatthew Ahrens 1335e7cbe64fSgw while (resid) { 1336e7cbe64fSgw size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff); 133788b7b0f2SMatthew Ahrens error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE); 1338e7cbe64fSgw if (error) 1339e7cbe64fSgw break; 1340e7cbe64fSgw boff += size; 1341e7cbe64fSgw addr += size; 1342e7cbe64fSgw resid -= size; 1343e7cbe64fSgw } 1344e7cbe64fSgw 1345e7cbe64fSgw return (error); 1346e7cbe64fSgw } 1347e7cbe64fSgw 1348fa9e4066Sahrens /*ARGSUSED*/ 1349fa9e4066Sahrens int 1350feb08c6bSbillm zvol_read(dev_t dev, uio_t *uio, cred_t *cr) 1351fa9e4066Sahrens { 1352c7ca1008Sgw minor_t minor = getminor(dev); 1353c7ca1008Sgw zvol_state_t *zv; 135473ec3d9cSgw uint64_t volsize; 1355c2e6a7d6Sperrin rl_t *rl; 1356feb08c6bSbillm int error = 0; 1357fa9e4066Sahrens 1358c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1359c7ca1008Sgw if (zv == NULL) 1360c7ca1008Sgw return (ENXIO); 1361c7ca1008Sgw 136273ec3d9cSgw volsize = zv->zv_volsize; 136373ec3d9cSgw if (uio->uio_resid > 0 && 136473ec3d9cSgw (uio->uio_loffset < 0 || uio->uio_loffset >= volsize)) 136573ec3d9cSgw return (EIO); 136673ec3d9cSgw 136788b7b0f2SMatthew Ahrens if (zv->zv_flags & ZVOL_DUMPIFIED) { 136888b7b0f2SMatthew Ahrens error = physio(zvol_strategy, NULL, dev, B_READ, 136988b7b0f2SMatthew Ahrens zvol_minphys, uio); 137088b7b0f2SMatthew Ahrens return (error); 137188b7b0f2SMatthew Ahrens } 137288b7b0f2SMatthew Ahrens 1373c2e6a7d6Sperrin rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 1374c2e6a7d6Sperrin RL_READER); 137573ec3d9cSgw while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 1376feb08c6bSbillm uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 1377fa9e4066Sahrens 137873ec3d9cSgw /* don't read past the end */ 137973ec3d9cSgw if (bytes > volsize - uio->uio_loffset) 138073ec3d9cSgw bytes = volsize - uio->uio_loffset; 138173ec3d9cSgw 1382feb08c6bSbillm error = dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes); 1383b87f3af3Sperrin if (error) { 1384b87f3af3Sperrin /* convert checksum errors into IO errors */ 1385b87f3af3Sperrin if (error == ECKSUM) 1386b87f3af3Sperrin error = EIO; 1387feb08c6bSbillm break; 1388b87f3af3Sperrin } 1389feb08c6bSbillm } 1390c2e6a7d6Sperrin zfs_range_unlock(rl); 1391feb08c6bSbillm return (error); 1392fa9e4066Sahrens } 1393fa9e4066Sahrens 1394fa9e4066Sahrens /*ARGSUSED*/ 1395fa9e4066Sahrens int 1396feb08c6bSbillm zvol_write(dev_t dev, uio_t *uio, cred_t *cr) 1397fa9e4066Sahrens { 1398c7ca1008Sgw minor_t minor = getminor(dev); 1399c7ca1008Sgw zvol_state_t *zv; 140073ec3d9cSgw uint64_t volsize; 1401c2e6a7d6Sperrin rl_t *rl; 1402feb08c6bSbillm int error = 0; 1403510b6c0eSNeil Perrin boolean_t sync; 1404feb08c6bSbillm 1405c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1406c7ca1008Sgw if (zv == NULL) 1407c7ca1008Sgw return (ENXIO); 1408c7ca1008Sgw 140973ec3d9cSgw volsize = zv->zv_volsize; 141073ec3d9cSgw if (uio->uio_resid > 0 && 141173ec3d9cSgw (uio->uio_loffset < 0 || uio->uio_loffset >= volsize)) 141273ec3d9cSgw return (EIO); 141373ec3d9cSgw 1414e7cbe64fSgw if (zv->zv_flags & ZVOL_DUMPIFIED) { 1415e7cbe64fSgw error = physio(zvol_strategy, NULL, dev, B_WRITE, 1416e7cbe64fSgw zvol_minphys, uio); 1417e7cbe64fSgw return (error); 1418e7cbe64fSgw } 1419e7cbe64fSgw 142055da60b9SMark J Musante sync = !(zv->zv_flags & ZVOL_WCE) || 142155da60b9SMark J Musante (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS); 1422510b6c0eSNeil Perrin 1423c2e6a7d6Sperrin rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 1424c2e6a7d6Sperrin RL_WRITER); 142573ec3d9cSgw while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 1426feb08c6bSbillm uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 1427feb08c6bSbillm uint64_t off = uio->uio_loffset; 1428feb08c6bSbillm dmu_tx_t *tx = dmu_tx_create(zv->zv_objset); 142973ec3d9cSgw 143073ec3d9cSgw if (bytes > volsize - off) /* don't write past the end */ 143173ec3d9cSgw bytes = volsize - off; 143273ec3d9cSgw 1433feb08c6bSbillm dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 1434feb08c6bSbillm error = dmu_tx_assign(tx, TXG_WAIT); 1435feb08c6bSbillm if (error) { 1436feb08c6bSbillm dmu_tx_abort(tx); 1437feb08c6bSbillm break; 1438feb08c6bSbillm } 143994d1a210STim Haley error = dmu_write_uio_dbuf(zv->zv_dbuf, uio, bytes, tx); 1440feb08c6bSbillm if (error == 0) 1441510b6c0eSNeil Perrin zvol_log_write(zv, tx, off, bytes, sync); 1442feb08c6bSbillm dmu_tx_commit(tx); 1443feb08c6bSbillm 1444feb08c6bSbillm if (error) 1445feb08c6bSbillm break; 1446feb08c6bSbillm } 1447c2e6a7d6Sperrin zfs_range_unlock(rl); 1448510b6c0eSNeil Perrin if (sync) 14495002558fSNeil Perrin zil_commit(zv->zv_zilog, ZVOL_OBJ); 1450feb08c6bSbillm return (error); 1451fa9e4066Sahrens } 1452fa9e4066Sahrens 1453c7f714e2SEric Taylor int 1454c7f714e2SEric Taylor zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs) 1455c7f714e2SEric Taylor { 1456c7f714e2SEric Taylor struct uuid uuid = EFI_RESERVED; 1457c7f714e2SEric Taylor efi_gpe_t gpe = { 0 }; 1458c7f714e2SEric Taylor uint32_t crc; 1459c7f714e2SEric Taylor dk_efi_t efi; 1460c7f714e2SEric Taylor int length; 1461c7f714e2SEric Taylor char *ptr; 1462c7f714e2SEric Taylor 1463c7f714e2SEric Taylor if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag)) 1464c7f714e2SEric Taylor return (EFAULT); 1465c7f714e2SEric Taylor ptr = (char *)(uintptr_t)efi.dki_data_64; 1466c7f714e2SEric Taylor length = efi.dki_length; 1467c7f714e2SEric Taylor /* 1468c7f714e2SEric Taylor * Some clients may attempt to request a PMBR for the 1469c7f714e2SEric Taylor * zvol. Currently this interface will return EINVAL to 1470c7f714e2SEric Taylor * such requests. These requests could be supported by 1471c7f714e2SEric Taylor * adding a check for lba == 0 and consing up an appropriate 1472c7f714e2SEric Taylor * PMBR. 1473c7f714e2SEric Taylor */ 1474c7f714e2SEric Taylor if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0) 1475c7f714e2SEric Taylor return (EINVAL); 1476c7f714e2SEric Taylor 1477c7f714e2SEric Taylor gpe.efi_gpe_StartingLBA = LE_64(34ULL); 1478c7f714e2SEric Taylor gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1); 1479c7f714e2SEric Taylor UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid); 1480c7f714e2SEric Taylor 1481c7f714e2SEric Taylor if (efi.dki_lba == 1) { 1482c7f714e2SEric Taylor efi_gpt_t gpt = { 0 }; 1483c7f714e2SEric Taylor 1484c7f714e2SEric Taylor gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE); 1485c7f714e2SEric Taylor gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT); 1486c7f714e2SEric Taylor gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt)); 1487c7f714e2SEric Taylor gpt.efi_gpt_MyLBA = LE_64(1ULL); 1488c7f714e2SEric Taylor gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL); 1489c7f714e2SEric Taylor gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1); 1490c7f714e2SEric Taylor gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL); 1491c7f714e2SEric Taylor gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1); 1492c7f714e2SEric Taylor gpt.efi_gpt_SizeOfPartitionEntry = 1493c7f714e2SEric Taylor LE_32(sizeof (efi_gpe_t)); 1494c7f714e2SEric Taylor CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table); 1495c7f714e2SEric Taylor gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc); 1496c7f714e2SEric Taylor CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table); 1497c7f714e2SEric Taylor gpt.efi_gpt_HeaderCRC32 = LE_32(~crc); 1498c7f714e2SEric Taylor if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length), 1499c7f714e2SEric Taylor flag)) 1500c7f714e2SEric Taylor return (EFAULT); 1501c7f714e2SEric Taylor ptr += sizeof (gpt); 1502c7f714e2SEric Taylor length -= sizeof (gpt); 1503c7f714e2SEric Taylor } 1504c7f714e2SEric Taylor if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe), 1505c7f714e2SEric Taylor length), flag)) 1506c7f714e2SEric Taylor return (EFAULT); 1507c7f714e2SEric Taylor return (0); 1508c7f714e2SEric Taylor } 1509c7f714e2SEric Taylor 15103fb517f7SJames Moore /* 15113fb517f7SJames Moore * BEGIN entry points to allow external callers access to the volume. 15123fb517f7SJames Moore */ 15133fb517f7SJames Moore /* 15143fb517f7SJames Moore * Return the volume parameters needed for access from an external caller. 15153fb517f7SJames Moore * These values are invariant as long as the volume is held open. 15163fb517f7SJames Moore */ 15173fb517f7SJames Moore int 15183fb517f7SJames Moore zvol_get_volume_params(minor_t minor, uint64_t *blksize, 15193fb517f7SJames Moore uint64_t *max_xfer_len, void **minor_hdl, void **objset_hdl, void **zil_hdl, 15203fb517f7SJames Moore void **rl_hdl, void **bonus_hdl) 15213fb517f7SJames Moore { 15223fb517f7SJames Moore zvol_state_t *zv; 15233fb517f7SJames Moore 1524c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1525c99e4bdcSChris Kirby if (zv == NULL) 15263fb517f7SJames Moore return (ENXIO); 15273fb517f7SJames Moore if (zv->zv_flags & ZVOL_DUMPIFIED) 15283fb517f7SJames Moore return (ENXIO); 15293fb517f7SJames Moore 15303fb517f7SJames Moore ASSERT(blksize && max_xfer_len && minor_hdl && 15313fb517f7SJames Moore objset_hdl && zil_hdl && rl_hdl && bonus_hdl); 15323fb517f7SJames Moore 15333fb517f7SJames Moore *blksize = zv->zv_volblocksize; 15343fb517f7SJames Moore *max_xfer_len = (uint64_t)zvol_maxphys; 15353fb517f7SJames Moore *minor_hdl = zv; 15363fb517f7SJames Moore *objset_hdl = zv->zv_objset; 15373fb517f7SJames Moore *zil_hdl = zv->zv_zilog; 15383fb517f7SJames Moore *rl_hdl = &zv->zv_znode; 15393fb517f7SJames Moore *bonus_hdl = zv->zv_dbuf; 15403fb517f7SJames Moore return (0); 15413fb517f7SJames Moore } 15423fb517f7SJames Moore 15433fb517f7SJames Moore /* 15443fb517f7SJames Moore * Return the current volume size to an external caller. 15453fb517f7SJames Moore * The size can change while the volume is open. 15463fb517f7SJames Moore */ 15473fb517f7SJames Moore uint64_t 15483fb517f7SJames Moore zvol_get_volume_size(void *minor_hdl) 15493fb517f7SJames Moore { 15503fb517f7SJames Moore zvol_state_t *zv = minor_hdl; 15513fb517f7SJames Moore 15523fb517f7SJames Moore return (zv->zv_volsize); 15533fb517f7SJames Moore } 15543fb517f7SJames Moore 15553fb517f7SJames Moore /* 15563fb517f7SJames Moore * Return the current WCE setting to an external caller. 15573fb517f7SJames Moore * The WCE setting can change while the volume is open. 15583fb517f7SJames Moore */ 15593fb517f7SJames Moore int 15603fb517f7SJames Moore zvol_get_volume_wce(void *minor_hdl) 15613fb517f7SJames Moore { 15623fb517f7SJames Moore zvol_state_t *zv = minor_hdl; 15633fb517f7SJames Moore 15643fb517f7SJames Moore return ((zv->zv_flags & ZVOL_WCE) ? 1 : 0); 15653fb517f7SJames Moore } 15663fb517f7SJames Moore 15673fb517f7SJames Moore /* 15683fb517f7SJames Moore * Entry point for external callers to zvol_log_write 15693fb517f7SJames Moore */ 15703fb517f7SJames Moore void 15713fb517f7SJames Moore zvol_log_write_minor(void *minor_hdl, dmu_tx_t *tx, offset_t off, ssize_t resid, 15723fb517f7SJames Moore boolean_t sync) 15733fb517f7SJames Moore { 15743fb517f7SJames Moore zvol_state_t *zv = minor_hdl; 15753fb517f7SJames Moore 15763fb517f7SJames Moore zvol_log_write(zv, tx, off, resid, sync); 15773fb517f7SJames Moore } 15783fb517f7SJames Moore /* 15793fb517f7SJames Moore * END entry points to allow external callers access to the volume. 15803fb517f7SJames Moore */ 15813fb517f7SJames Moore 1582b77b9231SDan McDonald /* 1583b77b9231SDan McDonald * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE. 1584b77b9231SDan McDonald */ 1585b77b9231SDan McDonald static void 1586b77b9231SDan McDonald zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off, uint64_t len, 1587b77b9231SDan McDonald boolean_t sync) 1588b77b9231SDan McDonald { 1589b77b9231SDan McDonald itx_t *itx; 1590b77b9231SDan McDonald lr_truncate_t *lr; 1591b77b9231SDan McDonald zilog_t *zilog = zv->zv_zilog; 1592b77b9231SDan McDonald 1593b77b9231SDan McDonald if (zil_replaying(zilog, tx)) 1594b77b9231SDan McDonald return; 1595b77b9231SDan McDonald 1596b77b9231SDan McDonald itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr)); 1597b77b9231SDan McDonald lr = (lr_truncate_t *)&itx->itx_lr; 1598b77b9231SDan McDonald lr->lr_foid = ZVOL_OBJ; 1599b77b9231SDan McDonald lr->lr_offset = off; 1600b77b9231SDan McDonald lr->lr_length = len; 1601b77b9231SDan McDonald 1602b77b9231SDan McDonald itx->itx_sync = sync; 1603b77b9231SDan McDonald zil_itx_assign(zilog, itx, tx); 1604b77b9231SDan McDonald } 1605b77b9231SDan McDonald 1606fa9e4066Sahrens /* 1607fa9e4066Sahrens * Dirtbag ioctls to support mkfs(1M) for UFS filesystems. See dkio(7I). 1608b77b9231SDan McDonald * Also a dirtbag dkio ioctl for unmap/free-block functionality. 1609fa9e4066Sahrens */ 1610fa9e4066Sahrens /*ARGSUSED*/ 1611fa9e4066Sahrens int 1612fa9e4066Sahrens zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 1613fa9e4066Sahrens { 1614fa9e4066Sahrens zvol_state_t *zv; 1615af2c4821Smaybee struct dk_cinfo dki; 1616fa9e4066Sahrens struct dk_minfo dkm; 1617af2c4821Smaybee struct dk_callback *dkc; 1618fa9e4066Sahrens int error = 0; 1619e7cbe64fSgw rl_t *rl; 1620fa9e4066Sahrens 1621c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 1622fa9e4066Sahrens 1623c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(getminor(dev), ZSST_ZVOL); 1624fa9e4066Sahrens 1625fa9e4066Sahrens if (zv == NULL) { 1626c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 1627fa9e4066Sahrens return (ENXIO); 1628fa9e4066Sahrens } 1629701f66c4SEric Taylor ASSERT(zv->zv_total_opens > 0); 1630fa9e4066Sahrens 1631fa9e4066Sahrens switch (cmd) { 1632fa9e4066Sahrens 1633fa9e4066Sahrens case DKIOCINFO: 1634af2c4821Smaybee bzero(&dki, sizeof (dki)); 1635af2c4821Smaybee (void) strcpy(dki.dki_cname, "zvol"); 1636af2c4821Smaybee (void) strcpy(dki.dki_dname, "zvol"); 1637af2c4821Smaybee dki.dki_ctype = DKC_UNKNOWN; 16383adc9019SEric Taylor dki.dki_unit = getminor(dev); 1639af2c4821Smaybee dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs); 1640c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 1641af2c4821Smaybee if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag)) 1642fa9e4066Sahrens error = EFAULT; 1643fa9e4066Sahrens return (error); 1644fa9e4066Sahrens 1645fa9e4066Sahrens case DKIOCGMEDIAINFO: 1646fa9e4066Sahrens bzero(&dkm, sizeof (dkm)); 1647fa9e4066Sahrens dkm.dki_lbsize = 1U << zv->zv_min_bs; 1648fa9e4066Sahrens dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs; 1649fa9e4066Sahrens dkm.dki_media_type = DK_UNKNOWN; 1650c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 1651fa9e4066Sahrens if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag)) 1652fa9e4066Sahrens error = EFAULT; 1653fa9e4066Sahrens return (error); 1654fa9e4066Sahrens 1655fa9e4066Sahrens case DKIOCGETEFI: 1656c7f714e2SEric Taylor { 1657c7f714e2SEric Taylor uint64_t vs = zv->zv_volsize; 1658c7f714e2SEric Taylor uint8_t bs = zv->zv_min_bs; 1659fa9e4066Sahrens 1660c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 1661c7f714e2SEric Taylor error = zvol_getefi((void *)arg, flag, vs, bs); 1662c7f714e2SEric Taylor return (error); 166368a5ac4dSmaybee } 1664fa9e4066Sahrens 1665feb08c6bSbillm case DKIOCFLUSHWRITECACHE: 1666af2c4821Smaybee dkc = (struct dk_callback *)arg; 1667c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 16685002558fSNeil Perrin zil_commit(zv->zv_zilog, ZVOL_OBJ); 1669af2c4821Smaybee if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) { 1670af2c4821Smaybee (*dkc->dkc_callback)(dkc->dkc_cookie, error); 1671af2c4821Smaybee error = 0; 1672af2c4821Smaybee } 1673701f66c4SEric Taylor return (error); 1674701f66c4SEric Taylor 1675701f66c4SEric Taylor case DKIOCGETWCE: 1676701f66c4SEric Taylor { 1677701f66c4SEric Taylor int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0; 1678701f66c4SEric Taylor if (ddi_copyout(&wce, (void *)arg, sizeof (int), 1679701f66c4SEric Taylor flag)) 1680701f66c4SEric Taylor error = EFAULT; 1681701f66c4SEric Taylor break; 1682701f66c4SEric Taylor } 1683701f66c4SEric Taylor case DKIOCSETWCE: 1684701f66c4SEric Taylor { 1685701f66c4SEric Taylor int wce; 1686701f66c4SEric Taylor if (ddi_copyin((void *)arg, &wce, sizeof (int), 1687701f66c4SEric Taylor flag)) { 1688701f66c4SEric Taylor error = EFAULT; 1689701f66c4SEric Taylor break; 1690701f66c4SEric Taylor } 1691701f66c4SEric Taylor if (wce) { 1692701f66c4SEric Taylor zv->zv_flags |= ZVOL_WCE; 1693c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 1694701f66c4SEric Taylor } else { 1695701f66c4SEric Taylor zv->zv_flags &= ~ZVOL_WCE; 1696c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 16975002558fSNeil Perrin zil_commit(zv->zv_zilog, ZVOL_OBJ); 1698701f66c4SEric Taylor } 1699701f66c4SEric Taylor return (0); 1700701f66c4SEric Taylor } 1701feb08c6bSbillm 1702b6130eadSmaybee case DKIOCGGEOM: 1703b6130eadSmaybee case DKIOCGVTOC: 1704e7cbe64fSgw /* 1705e7cbe64fSgw * commands using these (like prtvtoc) expect ENOTSUP 1706e7cbe64fSgw * since we're emulating an EFI label 1707e7cbe64fSgw */ 1708b6130eadSmaybee error = ENOTSUP; 1709b6130eadSmaybee break; 1710b6130eadSmaybee 1711e7cbe64fSgw case DKIOCDUMPINIT: 1712e7cbe64fSgw rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 1713e7cbe64fSgw RL_WRITER); 1714e7cbe64fSgw error = zvol_dumpify(zv); 1715e7cbe64fSgw zfs_range_unlock(rl); 1716e7cbe64fSgw break; 1717e7cbe64fSgw 1718e7cbe64fSgw case DKIOCDUMPFINI: 171906d5ae10SEric Taylor if (!(zv->zv_flags & ZVOL_DUMPIFIED)) 172006d5ae10SEric Taylor break; 1721e7cbe64fSgw rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 1722e7cbe64fSgw RL_WRITER); 1723e7cbe64fSgw error = zvol_dump_fini(zv); 1724e7cbe64fSgw zfs_range_unlock(rl); 1725e7cbe64fSgw break; 1726e7cbe64fSgw 1727b77b9231SDan McDonald case DKIOCFREE: 1728b77b9231SDan McDonald { 1729b77b9231SDan McDonald dkioc_free_t df; 1730b77b9231SDan McDonald dmu_tx_t *tx; 1731b77b9231SDan McDonald 1732b77b9231SDan McDonald if (ddi_copyin((void *)arg, &df, sizeof (df), flag)) { 1733b77b9231SDan McDonald error = EFAULT; 1734b77b9231SDan McDonald break; 1735b77b9231SDan McDonald } 1736b77b9231SDan McDonald 1737b77b9231SDan McDonald /* 1738b77b9231SDan McDonald * Apply Postel's Law to length-checking. If they overshoot, 1739b77b9231SDan McDonald * just blank out until the end, if there's a need to blank 1740b77b9231SDan McDonald * out anything. 1741b77b9231SDan McDonald */ 1742b77b9231SDan McDonald if (df.df_start >= zv->zv_volsize) 1743b77b9231SDan McDonald break; /* No need to do anything... */ 1744b77b9231SDan McDonald if (df.df_start + df.df_length > zv->zv_volsize) 1745b77b9231SDan McDonald df.df_length = DMU_OBJECT_END; 1746b77b9231SDan McDonald 1747b77b9231SDan McDonald rl = zfs_range_lock(&zv->zv_znode, df.df_start, df.df_length, 1748b77b9231SDan McDonald RL_WRITER); 1749b77b9231SDan McDonald tx = dmu_tx_create(zv->zv_objset); 1750b77b9231SDan McDonald error = dmu_tx_assign(tx, TXG_WAIT); 1751b77b9231SDan McDonald if (error != 0) { 1752b77b9231SDan McDonald dmu_tx_abort(tx); 1753b77b9231SDan McDonald } else { 1754b77b9231SDan McDonald zvol_log_truncate(zv, tx, df.df_start, 1755b77b9231SDan McDonald df.df_length, B_TRUE); 1756c08b1637SJosef 'Jeff' Sipek dmu_tx_commit(tx); 1757b77b9231SDan McDonald error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 1758b77b9231SDan McDonald df.df_start, df.df_length); 1759b77b9231SDan McDonald } 1760b77b9231SDan McDonald 1761b77b9231SDan McDonald zfs_range_unlock(rl); 1762b77b9231SDan McDonald 1763b77b9231SDan McDonald if (error == 0) { 1764b77b9231SDan McDonald /* 1765b77b9231SDan McDonald * If the write-cache is disabled or 'sync' property 1766b77b9231SDan McDonald * is set to 'always' then treat this as a synchronous 1767b77b9231SDan McDonald * operation (i.e. commit to zil). 1768b77b9231SDan McDonald */ 1769b77b9231SDan McDonald if (!(zv->zv_flags & ZVOL_WCE) || 1770b77b9231SDan McDonald (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)) 1771b77b9231SDan McDonald zil_commit(zv->zv_zilog, ZVOL_OBJ); 1772b77b9231SDan McDonald 1773b77b9231SDan McDonald /* 1774b77b9231SDan McDonald * If the caller really wants synchronous writes, and 1775b77b9231SDan McDonald * can't wait for them, don't return until the write 1776b77b9231SDan McDonald * is done. 1777b77b9231SDan McDonald */ 1778b77b9231SDan McDonald if (df.df_flags & DF_WAIT_SYNC) { 1779b77b9231SDan McDonald txg_wait_synced( 1780b77b9231SDan McDonald dmu_objset_pool(zv->zv_objset), 0); 1781b77b9231SDan McDonald } 1782b77b9231SDan McDonald } 1783b77b9231SDan McDonald break; 1784b77b9231SDan McDonald } 1785b77b9231SDan McDonald 1786fa9e4066Sahrens default: 178768a5ac4dSmaybee error = ENOTTY; 1788fa9e4066Sahrens break; 1789fa9e4066Sahrens 1790fa9e4066Sahrens } 1791c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 1792fa9e4066Sahrens return (error); 1793fa9e4066Sahrens } 1794fa9e4066Sahrens 1795fa9e4066Sahrens int 1796fa9e4066Sahrens zvol_busy(void) 1797fa9e4066Sahrens { 1798fa9e4066Sahrens return (zvol_minors != 0); 1799fa9e4066Sahrens } 1800fa9e4066Sahrens 1801fa9e4066Sahrens void 1802fa9e4066Sahrens zvol_init(void) 1803fa9e4066Sahrens { 1804c99e4bdcSChris Kirby VERIFY(ddi_soft_state_init(&zfsdev_state, sizeof (zfs_soft_state_t), 1805c99e4bdcSChris Kirby 1) == 0); 1806c99e4bdcSChris Kirby mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL); 1807fa9e4066Sahrens } 1808fa9e4066Sahrens 1809fa9e4066Sahrens void 1810fa9e4066Sahrens zvol_fini(void) 1811fa9e4066Sahrens { 1812c99e4bdcSChris Kirby mutex_destroy(&zfsdev_state_lock); 1813c99e4bdcSChris Kirby ddi_soft_state_fini(&zfsdev_state); 1814fa9e4066Sahrens } 1815e7cbe64fSgw 1816e7cbe64fSgw static int 1817e7cbe64fSgw zvol_dump_init(zvol_state_t *zv, boolean_t resize) 1818e7cbe64fSgw { 1819e7cbe64fSgw dmu_tx_t *tx; 1820e7cbe64fSgw int error = 0; 1821e7cbe64fSgw objset_t *os = zv->zv_objset; 1822e7cbe64fSgw nvlist_t *nv = NULL; 18238d265e66SGeorge Wilson uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset)); 1824e7cbe64fSgw 1825c99e4bdcSChris Kirby ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 1826681d9761SEric Taylor error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0, 1827681d9761SEric Taylor DMU_OBJECT_END); 1828681d9761SEric Taylor /* wait for dmu_free_long_range to actually free the blocks */ 1829681d9761SEric Taylor txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 1830e7cbe64fSgw 1831e7cbe64fSgw tx = dmu_tx_create(os); 1832e7cbe64fSgw dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 1833681d9761SEric Taylor dmu_tx_hold_bonus(tx, ZVOL_OBJ); 1834e7cbe64fSgw error = dmu_tx_assign(tx, TXG_WAIT); 1835e7cbe64fSgw if (error) { 1836e7cbe64fSgw dmu_tx_abort(tx); 1837e7cbe64fSgw return (error); 1838e7cbe64fSgw } 1839e7cbe64fSgw 1840e7cbe64fSgw /* 1841e7cbe64fSgw * If we are resizing the dump device then we only need to 1842e7cbe64fSgw * update the refreservation to match the newly updated 1843e7cbe64fSgw * zvolsize. Otherwise, we save off the original state of the 1844e7cbe64fSgw * zvol so that we can restore them if the zvol is ever undumpified. 1845e7cbe64fSgw */ 1846e7cbe64fSgw if (resize) { 1847e7cbe64fSgw error = zap_update(os, ZVOL_ZAP_OBJ, 1848e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 1849e7cbe64fSgw &zv->zv_volsize, tx); 1850e7cbe64fSgw } else { 1851afee20e4SGeorge Wilson uint64_t checksum, compress, refresrv, vbs, dedup; 185288b7b0f2SMatthew Ahrens 1853e7cbe64fSgw error = dsl_prop_get_integer(zv->zv_name, 1854e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL); 1855e7cbe64fSgw error = error ? error : dsl_prop_get_integer(zv->zv_name, 1856e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL); 1857e7cbe64fSgw error = error ? error : dsl_prop_get_integer(zv->zv_name, 1858e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL); 185988b7b0f2SMatthew Ahrens error = error ? error : dsl_prop_get_integer(zv->zv_name, 186088b7b0f2SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL); 18618d265e66SGeorge Wilson if (version >= SPA_VERSION_DEDUP) { 18628d265e66SGeorge Wilson error = error ? error : 18638d265e66SGeorge Wilson dsl_prop_get_integer(zv->zv_name, 18648d265e66SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), &dedup, NULL); 18658d265e66SGeorge Wilson } 1866e7cbe64fSgw 1867e7cbe64fSgw error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1868e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, 1869e7cbe64fSgw &compress, tx); 1870e7cbe64fSgw error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1871e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx); 1872e7cbe64fSgw error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1873e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 1874e7cbe64fSgw &refresrv, tx); 187588b7b0f2SMatthew Ahrens error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 187688b7b0f2SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, 187788b7b0f2SMatthew Ahrens &vbs, tx); 1878681d9761SEric Taylor error = error ? error : dmu_object_set_blocksize( 1879681d9761SEric Taylor os, ZVOL_OBJ, SPA_MAXBLOCKSIZE, 0, tx); 18808d265e66SGeorge Wilson if (version >= SPA_VERSION_DEDUP) { 18818d265e66SGeorge Wilson error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 18828d265e66SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, 18838d265e66SGeorge Wilson &dedup, tx); 18848d265e66SGeorge Wilson } 1885681d9761SEric Taylor if (error == 0) 1886681d9761SEric Taylor zv->zv_volblocksize = SPA_MAXBLOCKSIZE; 1887e7cbe64fSgw } 1888e7cbe64fSgw dmu_tx_commit(tx); 1889e7cbe64fSgw 1890e7cbe64fSgw /* 1891e7cbe64fSgw * We only need update the zvol's property if we are initializing 1892e7cbe64fSgw * the dump area for the first time. 1893e7cbe64fSgw */ 1894e7cbe64fSgw if (!resize) { 1895e7cbe64fSgw VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1896e7cbe64fSgw VERIFY(nvlist_add_uint64(nv, 1897e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0); 1898e7cbe64fSgw VERIFY(nvlist_add_uint64(nv, 1899e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), 1900e7cbe64fSgw ZIO_COMPRESS_OFF) == 0); 1901e7cbe64fSgw VERIFY(nvlist_add_uint64(nv, 1902e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_CHECKSUM), 1903e7cbe64fSgw ZIO_CHECKSUM_OFF) == 0); 19048d265e66SGeorge Wilson if (version >= SPA_VERSION_DEDUP) { 19058d265e66SGeorge Wilson VERIFY(nvlist_add_uint64(nv, 19068d265e66SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), 19078d265e66SGeorge Wilson ZIO_CHECKSUM_OFF) == 0); 19088d265e66SGeorge Wilson } 1909e7cbe64fSgw 191092241e0bSTom Erickson error = zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL, 191192241e0bSTom Erickson nv, NULL); 1912e7cbe64fSgw nvlist_free(nv); 1913e7cbe64fSgw 1914e7cbe64fSgw if (error) 1915e7cbe64fSgw return (error); 1916e7cbe64fSgw } 1917e7cbe64fSgw 1918e7cbe64fSgw /* Allocate the space for the dump */ 1919e7cbe64fSgw error = zvol_prealloc(zv); 1920e7cbe64fSgw return (error); 1921e7cbe64fSgw } 1922e7cbe64fSgw 1923e7cbe64fSgw static int 1924e7cbe64fSgw zvol_dumpify(zvol_state_t *zv) 1925e7cbe64fSgw { 1926e7cbe64fSgw int error = 0; 1927e7cbe64fSgw uint64_t dumpsize = 0; 1928e7cbe64fSgw dmu_tx_t *tx; 1929e7cbe64fSgw objset_t *os = zv->zv_objset; 1930e7cbe64fSgw 1931681d9761SEric Taylor if (zv->zv_flags & ZVOL_RDONLY) 1932e7cbe64fSgw return (EROFS); 1933e7cbe64fSgw 1934e7cbe64fSgw if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 1935e7cbe64fSgw 8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) { 19364445fffbSMatthew Ahrens boolean_t resize = (dumpsize > 0); 1937e7cbe64fSgw 1938e7cbe64fSgw if ((error = zvol_dump_init(zv, resize)) != 0) { 1939e7cbe64fSgw (void) zvol_dump_fini(zv); 1940e7cbe64fSgw return (error); 1941e7cbe64fSgw } 1942e7cbe64fSgw } 1943e7cbe64fSgw 1944e7cbe64fSgw /* 1945e7cbe64fSgw * Build up our lba mapping. 1946e7cbe64fSgw */ 1947e7cbe64fSgw error = zvol_get_lbas(zv); 1948e7cbe64fSgw if (error) { 1949e7cbe64fSgw (void) zvol_dump_fini(zv); 1950e7cbe64fSgw return (error); 1951e7cbe64fSgw } 1952e7cbe64fSgw 1953e7cbe64fSgw tx = dmu_tx_create(os); 1954e7cbe64fSgw dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 1955e7cbe64fSgw error = dmu_tx_assign(tx, TXG_WAIT); 1956e7cbe64fSgw if (error) { 1957e7cbe64fSgw dmu_tx_abort(tx); 1958e7cbe64fSgw (void) zvol_dump_fini(zv); 1959e7cbe64fSgw return (error); 1960e7cbe64fSgw } 1961e7cbe64fSgw 1962e7cbe64fSgw zv->zv_flags |= ZVOL_DUMPIFIED; 1963e7cbe64fSgw error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1, 1964e7cbe64fSgw &zv->zv_volsize, tx); 1965e7cbe64fSgw dmu_tx_commit(tx); 1966e7cbe64fSgw 1967e7cbe64fSgw if (error) { 1968e7cbe64fSgw (void) zvol_dump_fini(zv); 1969e7cbe64fSgw return (error); 1970e7cbe64fSgw } 1971e7cbe64fSgw 1972e7cbe64fSgw txg_wait_synced(dmu_objset_pool(os), 0); 1973e7cbe64fSgw return (0); 1974e7cbe64fSgw } 1975e7cbe64fSgw 1976e7cbe64fSgw static int 1977e7cbe64fSgw zvol_dump_fini(zvol_state_t *zv) 1978e7cbe64fSgw { 1979e7cbe64fSgw dmu_tx_t *tx; 1980e7cbe64fSgw objset_t *os = zv->zv_objset; 1981e7cbe64fSgw nvlist_t *nv; 1982e7cbe64fSgw int error = 0; 1983afee20e4SGeorge Wilson uint64_t checksum, compress, refresrv, vbs, dedup; 19848d265e66SGeorge Wilson uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset)); 1985e7cbe64fSgw 1986b7e50089Smaybee /* 1987b7e50089Smaybee * Attempt to restore the zvol back to its pre-dumpified state. 1988b7e50089Smaybee * This is a best-effort attempt as it's possible that not all 1989b7e50089Smaybee * of these properties were initialized during the dumpify process 1990b7e50089Smaybee * (i.e. error during zvol_dump_init). 1991b7e50089Smaybee */ 1992b7e50089Smaybee 1993e7cbe64fSgw tx = dmu_tx_create(os); 1994e7cbe64fSgw dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 1995e7cbe64fSgw error = dmu_tx_assign(tx, TXG_WAIT); 1996e7cbe64fSgw if (error) { 1997e7cbe64fSgw dmu_tx_abort(tx); 1998e7cbe64fSgw return (error); 1999e7cbe64fSgw } 2000b7e50089Smaybee (void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx); 2001b7e50089Smaybee dmu_tx_commit(tx); 2002e7cbe64fSgw 2003e7cbe64fSgw (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2004e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum); 2005e7cbe64fSgw (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2006e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress); 2007e7cbe64fSgw (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2008e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv); 200988b7b0f2SMatthew Ahrens (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 201088b7b0f2SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs); 2011e7cbe64fSgw 2012e7cbe64fSgw VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2013e7cbe64fSgw (void) nvlist_add_uint64(nv, 2014e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum); 2015e7cbe64fSgw (void) nvlist_add_uint64(nv, 2016e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress); 2017e7cbe64fSgw (void) nvlist_add_uint64(nv, 2018e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv); 20198d265e66SGeorge Wilson if (version >= SPA_VERSION_DEDUP && 20208d265e66SGeorge Wilson zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 20218d265e66SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, &dedup) == 0) { 20228d265e66SGeorge Wilson (void) nvlist_add_uint64(nv, 20238d265e66SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), dedup); 20248d265e66SGeorge Wilson } 202592241e0bSTom Erickson (void) zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL, 202692241e0bSTom Erickson nv, NULL); 2027e7cbe64fSgw nvlist_free(nv); 2028e7cbe64fSgw 2029b7e50089Smaybee zvol_free_extents(zv); 2030b7e50089Smaybee zv->zv_flags &= ~ZVOL_DUMPIFIED; 2031b7e50089Smaybee (void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END); 2032681d9761SEric Taylor /* wait for dmu_free_long_range to actually free the blocks */ 2033681d9761SEric Taylor txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 2034681d9761SEric Taylor tx = dmu_tx_create(os); 2035681d9761SEric Taylor dmu_tx_hold_bonus(tx, ZVOL_OBJ); 2036681d9761SEric Taylor error = dmu_tx_assign(tx, TXG_WAIT); 2037681d9761SEric Taylor if (error) { 2038681d9761SEric Taylor dmu_tx_abort(tx); 2039681d9761SEric Taylor return (error); 2040681d9761SEric Taylor } 2041b24ab676SJeff Bonwick if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0) 2042b24ab676SJeff Bonwick zv->zv_volblocksize = vbs; 2043681d9761SEric Taylor dmu_tx_commit(tx); 2044b7e50089Smaybee 2045e7cbe64fSgw return (0); 2046e7cbe64fSgw } 2047