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. 27*4445fffbSMatthew 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> 83fa9e4066Sahrens 84fa9e4066Sahrens #include "zfs_namecheck.h" 85fa9e4066Sahrens 86c99e4bdcSChris Kirby void *zfsdev_state; 87503ad85cSMatthew Ahrens static char *zvol_tag = "zvol_tag"; 88fa9e4066Sahrens 89e7cbe64fSgw #define ZVOL_DUMPSIZE "dumpsize" 90e7cbe64fSgw 91fa9e4066Sahrens /* 92c99e4bdcSChris Kirby * This lock protects the zfsdev_state structure from being modified 93fa9e4066Sahrens * while it's being used, e.g. an open that comes in before a create 94fa9e4066Sahrens * finishes. It also protects temporary opens of the dataset so that, 95fa9e4066Sahrens * e.g., an open doesn't get a spurious EBUSY. 96fa9e4066Sahrens */ 97c99e4bdcSChris Kirby kmutex_t zfsdev_state_lock; 98fa9e4066Sahrens static uint32_t zvol_minors; 99fa9e4066Sahrens 100e7cbe64fSgw typedef struct zvol_extent { 10188b7b0f2SMatthew Ahrens list_node_t ze_node; 102e7cbe64fSgw dva_t ze_dva; /* dva associated with this extent */ 10388b7b0f2SMatthew Ahrens uint64_t ze_nblks; /* number of blocks in extent */ 104e7cbe64fSgw } zvol_extent_t; 105e7cbe64fSgw 106fa9e4066Sahrens /* 107fa9e4066Sahrens * The in-core state of each volume. 108fa9e4066Sahrens */ 109fa9e4066Sahrens typedef struct zvol_state { 110fa9e4066Sahrens char zv_name[MAXPATHLEN]; /* pool/dd name */ 111fa9e4066Sahrens uint64_t zv_volsize; /* amount of space we advertise */ 11267bd71c6Sperrin uint64_t zv_volblocksize; /* volume block size */ 113fa9e4066Sahrens minor_t zv_minor; /* minor number */ 114fa9e4066Sahrens uint8_t zv_min_bs; /* minimum addressable block shift */ 115701f66c4SEric Taylor uint8_t zv_flags; /* readonly, dumpified, etc. */ 116fa9e4066Sahrens objset_t *zv_objset; /* objset handle */ 117fa9e4066Sahrens uint32_t zv_open_count[OTYPCNT]; /* open counts */ 118fa9e4066Sahrens uint32_t zv_total_opens; /* total open count */ 11922ac5be4Sperrin zilog_t *zv_zilog; /* ZIL handle */ 12088b7b0f2SMatthew Ahrens list_t zv_extents; /* List of extents for dump */ 121c2e6a7d6Sperrin znode_t zv_znode; /* for range locking */ 12294d1a210STim Haley dmu_buf_t *zv_dbuf; /* bonus handle */ 123fa9e4066Sahrens } zvol_state_t; 124fa9e4066Sahrens 125e7cbe64fSgw /* 126e7cbe64fSgw * zvol specific flags 127e7cbe64fSgw */ 128e7cbe64fSgw #define ZVOL_RDONLY 0x1 129e7cbe64fSgw #define ZVOL_DUMPIFIED 0x2 130c7f714e2SEric Taylor #define ZVOL_EXCL 0x4 131701f66c4SEric Taylor #define ZVOL_WCE 0x8 132e7cbe64fSgw 13367bd71c6Sperrin /* 13467bd71c6Sperrin * zvol maximum transfer in one DMU tx. 13567bd71c6Sperrin */ 13667bd71c6Sperrin int zvol_maxphys = DMU_MAX_ACCESS/2; 13767bd71c6Sperrin 13892241e0bSTom Erickson extern int zfs_set_prop_nvlist(const char *, zprop_source_t, 139*4445fffbSMatthew Ahrens nvlist_t *, nvlist_t *); 140681d9761SEric Taylor static int zvol_remove_zv(zvol_state_t *); 141feb08c6bSbillm static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio); 142e7cbe64fSgw static int zvol_dumpify(zvol_state_t *zv); 143e7cbe64fSgw static int zvol_dump_fini(zvol_state_t *zv); 144e7cbe64fSgw static int zvol_dump_init(zvol_state_t *zv, boolean_t resize); 14567bd71c6Sperrin 146fa9e4066Sahrens static void 147681d9761SEric Taylor zvol_size_changed(uint64_t volsize, major_t maj, minor_t min) 148fa9e4066Sahrens { 149681d9761SEric Taylor dev_t dev = makedevice(maj, min); 150fa9e4066Sahrens 151fa9e4066Sahrens VERIFY(ddi_prop_update_int64(dev, zfs_dip, 152681d9761SEric Taylor "Size", volsize) == DDI_SUCCESS); 153fa9e4066Sahrens VERIFY(ddi_prop_update_int64(dev, zfs_dip, 154681d9761SEric Taylor "Nblocks", lbtodb(volsize)) == DDI_SUCCESS); 155e7cbe64fSgw 156e7cbe64fSgw /* Notify specfs to invalidate the cached size */ 157e7cbe64fSgw spec_size_invalidate(dev, VBLK); 158e7cbe64fSgw spec_size_invalidate(dev, VCHR); 159fa9e4066Sahrens } 160fa9e4066Sahrens 161fa9e4066Sahrens int 162e9dbad6fSeschrock zvol_check_volsize(uint64_t volsize, uint64_t blocksize) 163fa9e4066Sahrens { 164e9dbad6fSeschrock if (volsize == 0) 165fa9e4066Sahrens return (EINVAL); 166fa9e4066Sahrens 167e9dbad6fSeschrock if (volsize % blocksize != 0) 1685c5460e9Seschrock return (EINVAL); 1695c5460e9Seschrock 170fa9e4066Sahrens #ifdef _ILP32 171e9dbad6fSeschrock if (volsize - 1 > SPEC_MAXOFFSET_T) 172fa9e4066Sahrens return (EOVERFLOW); 173fa9e4066Sahrens #endif 174fa9e4066Sahrens return (0); 175fa9e4066Sahrens } 176fa9e4066Sahrens 177fa9e4066Sahrens int 178e9dbad6fSeschrock zvol_check_volblocksize(uint64_t volblocksize) 179fa9e4066Sahrens { 180e9dbad6fSeschrock if (volblocksize < SPA_MINBLOCKSIZE || 181e9dbad6fSeschrock volblocksize > SPA_MAXBLOCKSIZE || 182e9dbad6fSeschrock !ISP2(volblocksize)) 183fa9e4066Sahrens return (EDOM); 184fa9e4066Sahrens 185fa9e4066Sahrens return (0); 186fa9e4066Sahrens } 187fa9e4066Sahrens 188fa9e4066Sahrens int 189a2eea2e1Sahrens zvol_get_stats(objset_t *os, nvlist_t *nv) 190fa9e4066Sahrens { 191fa9e4066Sahrens int error; 192fa9e4066Sahrens dmu_object_info_t doi; 193a2eea2e1Sahrens uint64_t val; 194fa9e4066Sahrens 195a2eea2e1Sahrens error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val); 196fa9e4066Sahrens if (error) 197fa9e4066Sahrens return (error); 198fa9e4066Sahrens 199a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val); 200a2eea2e1Sahrens 201fa9e4066Sahrens error = dmu_object_info(os, ZVOL_OBJ, &doi); 202fa9e4066Sahrens 203a2eea2e1Sahrens if (error == 0) { 204a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE, 205a2eea2e1Sahrens doi.doi_data_block_size); 206a2eea2e1Sahrens } 207fa9e4066Sahrens 208fa9e4066Sahrens return (error); 209fa9e4066Sahrens } 210fa9e4066Sahrens 211fa9e4066Sahrens static zvol_state_t * 212e9dbad6fSeschrock zvol_minor_lookup(const char *name) 213fa9e4066Sahrens { 214fa9e4066Sahrens minor_t minor; 215fa9e4066Sahrens zvol_state_t *zv; 216fa9e4066Sahrens 217c99e4bdcSChris Kirby ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 218fa9e4066Sahrens 219c99e4bdcSChris Kirby for (minor = 1; minor <= ZFSDEV_MAX_MINOR; minor++) { 220c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 221fa9e4066Sahrens if (zv == NULL) 222fa9e4066Sahrens continue; 223fa9e4066Sahrens if (strcmp(zv->zv_name, name) == 0) 224f80ce222SChris Kirby return (zv); 225fa9e4066Sahrens } 226fa9e4066Sahrens 227f80ce222SChris Kirby return (NULL); 228fa9e4066Sahrens } 229fa9e4066Sahrens 230e7cbe64fSgw /* extent mapping arg */ 231e7cbe64fSgw struct maparg { 23288b7b0f2SMatthew Ahrens zvol_state_t *ma_zv; 23388b7b0f2SMatthew Ahrens uint64_t ma_blks; 234e7cbe64fSgw }; 235e7cbe64fSgw 236e7cbe64fSgw /*ARGSUSED*/ 237e7cbe64fSgw static int 2383f9d6ad7SLin Ling zvol_map_block(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, arc_buf_t *pbuf, 239b24ab676SJeff Bonwick const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg) 240e7cbe64fSgw { 24188b7b0f2SMatthew Ahrens struct maparg *ma = arg; 24288b7b0f2SMatthew Ahrens zvol_extent_t *ze; 24388b7b0f2SMatthew Ahrens int bs = ma->ma_zv->zv_volblocksize; 244e7cbe64fSgw 24588b7b0f2SMatthew Ahrens if (bp == NULL || zb->zb_object != ZVOL_OBJ || zb->zb_level != 0) 24688b7b0f2SMatthew Ahrens return (0); 247e7cbe64fSgw 24888b7b0f2SMatthew Ahrens VERIFY3U(ma->ma_blks, ==, zb->zb_blkid); 24988b7b0f2SMatthew Ahrens ma->ma_blks++; 250e7cbe64fSgw 25188b7b0f2SMatthew Ahrens /* Abort immediately if we have encountered gang blocks */ 25288b7b0f2SMatthew Ahrens if (BP_IS_GANG(bp)) 25388b7b0f2SMatthew Ahrens return (EFRAGS); 254e7cbe64fSgw 25588b7b0f2SMatthew Ahrens /* 25688b7b0f2SMatthew Ahrens * See if the block is at the end of the previous extent. 25788b7b0f2SMatthew Ahrens */ 25888b7b0f2SMatthew Ahrens ze = list_tail(&ma->ma_zv->zv_extents); 25988b7b0f2SMatthew Ahrens if (ze && 26088b7b0f2SMatthew Ahrens DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) && 26188b7b0f2SMatthew Ahrens DVA_GET_OFFSET(BP_IDENTITY(bp)) == 26288b7b0f2SMatthew Ahrens DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) { 26388b7b0f2SMatthew Ahrens ze->ze_nblks++; 26488b7b0f2SMatthew Ahrens return (0); 265e7cbe64fSgw } 266e7cbe64fSgw 26788b7b0f2SMatthew Ahrens dprintf_bp(bp, "%s", "next blkptr:"); 268e7cbe64fSgw 26988b7b0f2SMatthew Ahrens /* start a new extent */ 27088b7b0f2SMatthew Ahrens ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP); 27188b7b0f2SMatthew Ahrens ze->ze_dva = bp->blk_dva[0]; /* structure assignment */ 27288b7b0f2SMatthew Ahrens ze->ze_nblks = 1; 27388b7b0f2SMatthew Ahrens list_insert_tail(&ma->ma_zv->zv_extents, ze); 27488b7b0f2SMatthew Ahrens return (0); 27588b7b0f2SMatthew Ahrens } 276e7cbe64fSgw 27788b7b0f2SMatthew Ahrens static void 27888b7b0f2SMatthew Ahrens zvol_free_extents(zvol_state_t *zv) 27988b7b0f2SMatthew Ahrens { 28088b7b0f2SMatthew Ahrens zvol_extent_t *ze; 281e7cbe64fSgw 28288b7b0f2SMatthew Ahrens while (ze = list_head(&zv->zv_extents)) { 28388b7b0f2SMatthew Ahrens list_remove(&zv->zv_extents, ze); 28488b7b0f2SMatthew Ahrens kmem_free(ze, sizeof (zvol_extent_t)); 285e7cbe64fSgw } 28688b7b0f2SMatthew Ahrens } 287e7cbe64fSgw 28888b7b0f2SMatthew Ahrens static int 28988b7b0f2SMatthew Ahrens zvol_get_lbas(zvol_state_t *zv) 29088b7b0f2SMatthew Ahrens { 2913adc9019SEric Taylor objset_t *os = zv->zv_objset; 29288b7b0f2SMatthew Ahrens struct maparg ma; 29388b7b0f2SMatthew Ahrens int err; 29488b7b0f2SMatthew Ahrens 29588b7b0f2SMatthew Ahrens ma.ma_zv = zv; 29688b7b0f2SMatthew Ahrens ma.ma_blks = 0; 29788b7b0f2SMatthew Ahrens zvol_free_extents(zv); 29888b7b0f2SMatthew Ahrens 2993adc9019SEric Taylor /* commit any in-flight changes before traversing the dataset */ 3003adc9019SEric Taylor txg_wait_synced(dmu_objset_pool(os), 0); 3013adc9019SEric Taylor err = traverse_dataset(dmu_objset_ds(os), 0, 30288b7b0f2SMatthew Ahrens TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma); 30388b7b0f2SMatthew Ahrens if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) { 30488b7b0f2SMatthew Ahrens zvol_free_extents(zv); 30588b7b0f2SMatthew Ahrens return (err ? err : EIO); 306e7cbe64fSgw } 30788b7b0f2SMatthew Ahrens 308e7cbe64fSgw return (0); 309e7cbe64fSgw } 310e7cbe64fSgw 311ecd6cf80Smarks /* ARGSUSED */ 312fa9e4066Sahrens void 313ecd6cf80Smarks zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 314fa9e4066Sahrens { 315da6c28aaSamw zfs_creat_t *zct = arg; 316da6c28aaSamw nvlist_t *nvprops = zct->zct_props; 317fa9e4066Sahrens int error; 318e9dbad6fSeschrock uint64_t volblocksize, volsize; 319fa9e4066Sahrens 320ecd6cf80Smarks VERIFY(nvlist_lookup_uint64(nvprops, 321e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0); 322ecd6cf80Smarks if (nvlist_lookup_uint64(nvprops, 323e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0) 324e9dbad6fSeschrock volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 325e9dbad6fSeschrock 326e9dbad6fSeschrock /* 327e7cbe64fSgw * These properties must be removed from the list so the generic 328e9dbad6fSeschrock * property setting step won't apply to them. 329e9dbad6fSeschrock */ 330ecd6cf80Smarks VERIFY(nvlist_remove_all(nvprops, 331e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0); 332ecd6cf80Smarks (void) nvlist_remove_all(nvprops, 333e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE)); 334e9dbad6fSeschrock 335e9dbad6fSeschrock error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize, 336fa9e4066Sahrens DMU_OT_NONE, 0, tx); 337fa9e4066Sahrens ASSERT(error == 0); 338fa9e4066Sahrens 339fa9e4066Sahrens error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP, 340fa9e4066Sahrens DMU_OT_NONE, 0, tx); 341fa9e4066Sahrens ASSERT(error == 0); 342fa9e4066Sahrens 343e9dbad6fSeschrock error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx); 344fa9e4066Sahrens ASSERT(error == 0); 345fa9e4066Sahrens } 346fa9e4066Sahrens 347b77b9231SDan McDonald /* 348b77b9231SDan McDonald * Replay a TX_TRUNCATE ZIL transaction if asked. TX_TRUNCATE is how we 349b77b9231SDan McDonald * implement DKIOCFREE/free-long-range. 350b77b9231SDan McDonald */ 351b77b9231SDan McDonald static int 352b77b9231SDan McDonald zvol_replay_truncate(zvol_state_t *zv, lr_truncate_t *lr, boolean_t byteswap) 353b77b9231SDan McDonald { 354b77b9231SDan McDonald uint64_t offset, length; 355b77b9231SDan McDonald 356b77b9231SDan McDonald if (byteswap) 357b77b9231SDan McDonald byteswap_uint64_array(lr, sizeof (*lr)); 358b77b9231SDan McDonald 359b77b9231SDan McDonald offset = lr->lr_offset; 360b77b9231SDan McDonald length = lr->lr_length; 361b77b9231SDan McDonald 362b77b9231SDan McDonald return (dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, offset, length)); 363b77b9231SDan McDonald } 364b77b9231SDan McDonald 36522ac5be4Sperrin /* 36622ac5be4Sperrin * Replay a TX_WRITE ZIL transaction that didn't get committed 36722ac5be4Sperrin * after a system failure 36822ac5be4Sperrin */ 36922ac5be4Sperrin static int 37022ac5be4Sperrin zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap) 37122ac5be4Sperrin { 37222ac5be4Sperrin objset_t *os = zv->zv_objset; 37322ac5be4Sperrin char *data = (char *)(lr + 1); /* data follows lr_write_t */ 374b24ab676SJeff Bonwick uint64_t offset, length; 37522ac5be4Sperrin dmu_tx_t *tx; 37622ac5be4Sperrin int error; 37722ac5be4Sperrin 37822ac5be4Sperrin if (byteswap) 37922ac5be4Sperrin byteswap_uint64_array(lr, sizeof (*lr)); 38022ac5be4Sperrin 381b24ab676SJeff Bonwick offset = lr->lr_offset; 382b24ab676SJeff Bonwick length = lr->lr_length; 383b24ab676SJeff Bonwick 384b24ab676SJeff Bonwick /* If it's a dmu_sync() block, write the whole block */ 385b24ab676SJeff Bonwick if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) { 386b24ab676SJeff Bonwick uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr); 387b24ab676SJeff Bonwick if (length < blocksize) { 388b24ab676SJeff Bonwick offset -= offset % blocksize; 389b24ab676SJeff Bonwick length = blocksize; 390b24ab676SJeff Bonwick } 391b24ab676SJeff Bonwick } 392975c32a0SNeil Perrin 39322ac5be4Sperrin tx = dmu_tx_create(os); 394b24ab676SJeff Bonwick dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length); 3951209a471SNeil Perrin error = dmu_tx_assign(tx, TXG_WAIT); 39622ac5be4Sperrin if (error) { 39722ac5be4Sperrin dmu_tx_abort(tx); 39822ac5be4Sperrin } else { 399b24ab676SJeff Bonwick dmu_write(os, ZVOL_OBJ, offset, length, data, tx); 40022ac5be4Sperrin dmu_tx_commit(tx); 40122ac5be4Sperrin } 40222ac5be4Sperrin 40322ac5be4Sperrin return (error); 40422ac5be4Sperrin } 40522ac5be4Sperrin 40622ac5be4Sperrin /* ARGSUSED */ 40722ac5be4Sperrin static int 40822ac5be4Sperrin zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap) 40922ac5be4Sperrin { 41022ac5be4Sperrin return (ENOTSUP); 41122ac5be4Sperrin } 41222ac5be4Sperrin 41322ac5be4Sperrin /* 41422ac5be4Sperrin * Callback vectors for replaying records. 415b77b9231SDan McDonald * Only TX_WRITE and TX_TRUNCATE are needed for zvol. 41622ac5be4Sperrin */ 41722ac5be4Sperrin zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = { 41822ac5be4Sperrin zvol_replay_err, /* 0 no such transaction type */ 41922ac5be4Sperrin zvol_replay_err, /* TX_CREATE */ 42022ac5be4Sperrin zvol_replay_err, /* TX_MKDIR */ 42122ac5be4Sperrin zvol_replay_err, /* TX_MKXATTR */ 42222ac5be4Sperrin zvol_replay_err, /* TX_SYMLINK */ 42322ac5be4Sperrin zvol_replay_err, /* TX_REMOVE */ 42422ac5be4Sperrin zvol_replay_err, /* TX_RMDIR */ 42522ac5be4Sperrin zvol_replay_err, /* TX_LINK */ 42622ac5be4Sperrin zvol_replay_err, /* TX_RENAME */ 42722ac5be4Sperrin zvol_replay_write, /* TX_WRITE */ 428b77b9231SDan McDonald zvol_replay_truncate, /* TX_TRUNCATE */ 42922ac5be4Sperrin zvol_replay_err, /* TX_SETATTR */ 43022ac5be4Sperrin zvol_replay_err, /* TX_ACL */ 431975c32a0SNeil Perrin zvol_replay_err, /* TX_CREATE_ACL */ 432975c32a0SNeil Perrin zvol_replay_err, /* TX_CREATE_ATTR */ 433975c32a0SNeil Perrin zvol_replay_err, /* TX_CREATE_ACL_ATTR */ 434975c32a0SNeil Perrin zvol_replay_err, /* TX_MKDIR_ACL */ 435975c32a0SNeil Perrin zvol_replay_err, /* TX_MKDIR_ATTR */ 436975c32a0SNeil Perrin zvol_replay_err, /* TX_MKDIR_ACL_ATTR */ 437975c32a0SNeil Perrin zvol_replay_err, /* TX_WRITE2 */ 43822ac5be4Sperrin }; 43922ac5be4Sperrin 440681d9761SEric Taylor int 441681d9761SEric Taylor zvol_name2minor(const char *name, minor_t *minor) 442681d9761SEric Taylor { 443681d9761SEric Taylor zvol_state_t *zv; 444681d9761SEric Taylor 445c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 446681d9761SEric Taylor zv = zvol_minor_lookup(name); 447681d9761SEric Taylor if (minor && zv) 448681d9761SEric Taylor *minor = zv->zv_minor; 449c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 450681d9761SEric Taylor return (zv ? 0 : -1); 451681d9761SEric Taylor } 452681d9761SEric Taylor 453e7cbe64fSgw /* 454e7cbe64fSgw * Create a minor node (plus a whole lot more) for the specified volume. 455fa9e4066Sahrens */ 456fa9e4066Sahrens int 457681d9761SEric Taylor zvol_create_minor(const char *name) 458fa9e4066Sahrens { 459c99e4bdcSChris Kirby zfs_soft_state_t *zs; 460fa9e4066Sahrens zvol_state_t *zv; 461fa9e4066Sahrens objset_t *os; 46267bd71c6Sperrin dmu_object_info_t doi; 463fa9e4066Sahrens minor_t minor = 0; 464fa9e4066Sahrens char chrbuf[30], blkbuf[30]; 465fa9e4066Sahrens int error; 466fa9e4066Sahrens 467c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 468fa9e4066Sahrens 4691195e687SMark J Musante if (zvol_minor_lookup(name) != NULL) { 470c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 471fa9e4066Sahrens return (EEXIST); 472fa9e4066Sahrens } 473fa9e4066Sahrens 474503ad85cSMatthew Ahrens /* lie and say we're read-only */ 4756e0cbcaaSMatthew Ahrens error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, FTAG, &os); 476fa9e4066Sahrens 477fa9e4066Sahrens if (error) { 478c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 479fa9e4066Sahrens return (error); 480fa9e4066Sahrens } 481fa9e4066Sahrens 482c99e4bdcSChris Kirby if ((minor = zfsdev_minor_alloc()) == 0) { 4836e0cbcaaSMatthew Ahrens dmu_objset_disown(os, FTAG); 484c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 485fa9e4066Sahrens return (ENXIO); 486fa9e4066Sahrens } 487fa9e4066Sahrens 488c99e4bdcSChris Kirby if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS) { 4896e0cbcaaSMatthew Ahrens dmu_objset_disown(os, FTAG); 490c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 491fa9e4066Sahrens return (EAGAIN); 492fa9e4066Sahrens } 493e9dbad6fSeschrock (void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME, 494e9dbad6fSeschrock (char *)name); 495fa9e4066Sahrens 496681d9761SEric Taylor (void) snprintf(chrbuf, sizeof (chrbuf), "%u,raw", minor); 497fa9e4066Sahrens 498fa9e4066Sahrens if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR, 499fa9e4066Sahrens minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 500c99e4bdcSChris Kirby ddi_soft_state_free(zfsdev_state, minor); 5016e0cbcaaSMatthew Ahrens dmu_objset_disown(os, FTAG); 502c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 503fa9e4066Sahrens return (EAGAIN); 504fa9e4066Sahrens } 505fa9e4066Sahrens 506681d9761SEric Taylor (void) snprintf(blkbuf, sizeof (blkbuf), "%u", minor); 507fa9e4066Sahrens 508fa9e4066Sahrens if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK, 509fa9e4066Sahrens minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 510fa9e4066Sahrens ddi_remove_minor_node(zfs_dip, chrbuf); 511c99e4bdcSChris Kirby ddi_soft_state_free(zfsdev_state, minor); 5126e0cbcaaSMatthew Ahrens dmu_objset_disown(os, FTAG); 513c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 514fa9e4066Sahrens return (EAGAIN); 515fa9e4066Sahrens } 516fa9e4066Sahrens 517c99e4bdcSChris Kirby zs = ddi_get_soft_state(zfsdev_state, minor); 518c99e4bdcSChris Kirby zs->zss_type = ZSST_ZVOL; 519c99e4bdcSChris Kirby zv = zs->zss_data = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP); 520681d9761SEric Taylor (void) strlcpy(zv->zv_name, name, MAXPATHLEN); 521fa9e4066Sahrens zv->zv_min_bs = DEV_BSHIFT; 522fa9e4066Sahrens zv->zv_minor = minor; 523fa9e4066Sahrens zv->zv_objset = os; 524f9af39baSGeorge Wilson if (dmu_objset_is_snapshot(os) || !spa_writeable(dmu_objset_spa(os))) 525681d9761SEric Taylor zv->zv_flags |= ZVOL_RDONLY; 526c2e6a7d6Sperrin mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL); 527c2e6a7d6Sperrin avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare, 528c2e6a7d6Sperrin sizeof (rl_t), offsetof(rl_t, r_node)); 52988b7b0f2SMatthew Ahrens list_create(&zv->zv_extents, sizeof (zvol_extent_t), 53088b7b0f2SMatthew Ahrens offsetof(zvol_extent_t, ze_node)); 53167bd71c6Sperrin /* get and cache the blocksize */ 53267bd71c6Sperrin error = dmu_object_info(os, ZVOL_OBJ, &doi); 53367bd71c6Sperrin ASSERT(error == 0); 53467bd71c6Sperrin zv->zv_volblocksize = doi.doi_data_block_size; 53522ac5be4Sperrin 536f9af39baSGeorge Wilson if (spa_writeable(dmu_objset_spa(os))) { 537f9af39baSGeorge Wilson if (zil_replay_disable) 538f9af39baSGeorge Wilson zil_destroy(dmu_objset_zil(os), B_FALSE); 539f9af39baSGeorge Wilson else 540f9af39baSGeorge Wilson zil_replay(os, zv, zvol_replay_vector); 541f9af39baSGeorge Wilson } 5426e0cbcaaSMatthew Ahrens dmu_objset_disown(os, FTAG); 543681d9761SEric Taylor zv->zv_objset = NULL; 544fa9e4066Sahrens 545fa9e4066Sahrens zvol_minors++; 546fa9e4066Sahrens 547c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 548fa9e4066Sahrens 549fa9e4066Sahrens return (0); 550fa9e4066Sahrens } 551fa9e4066Sahrens 552fa9e4066Sahrens /* 553fa9e4066Sahrens * Remove minor node for the specified volume. 554fa9e4066Sahrens */ 555681d9761SEric Taylor static int 556681d9761SEric Taylor zvol_remove_zv(zvol_state_t *zv) 557681d9761SEric Taylor { 558681d9761SEric Taylor char nmbuf[20]; 559c99e4bdcSChris Kirby minor_t minor = zv->zv_minor; 560681d9761SEric Taylor 561c99e4bdcSChris Kirby ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 562681d9761SEric Taylor if (zv->zv_total_opens != 0) 563681d9761SEric Taylor return (EBUSY); 564681d9761SEric Taylor 565c99e4bdcSChris Kirby (void) snprintf(nmbuf, sizeof (nmbuf), "%u,raw", minor); 566681d9761SEric Taylor ddi_remove_minor_node(zfs_dip, nmbuf); 567681d9761SEric Taylor 568c99e4bdcSChris Kirby (void) snprintf(nmbuf, sizeof (nmbuf), "%u", minor); 569681d9761SEric Taylor ddi_remove_minor_node(zfs_dip, nmbuf); 570681d9761SEric Taylor 571681d9761SEric Taylor avl_destroy(&zv->zv_znode.z_range_avl); 572681d9761SEric Taylor mutex_destroy(&zv->zv_znode.z_range_lock); 573681d9761SEric Taylor 574c99e4bdcSChris Kirby kmem_free(zv, sizeof (zvol_state_t)); 575c99e4bdcSChris Kirby 576c99e4bdcSChris Kirby ddi_soft_state_free(zfsdev_state, minor); 577681d9761SEric Taylor 578681d9761SEric Taylor zvol_minors--; 579681d9761SEric Taylor return (0); 580681d9761SEric Taylor } 581681d9761SEric Taylor 582fa9e4066Sahrens int 583e9dbad6fSeschrock zvol_remove_minor(const char *name) 584fa9e4066Sahrens { 585fa9e4066Sahrens zvol_state_t *zv; 586681d9761SEric Taylor int rc; 587fa9e4066Sahrens 588c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 589e9dbad6fSeschrock if ((zv = zvol_minor_lookup(name)) == NULL) { 590c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 591fa9e4066Sahrens return (ENXIO); 592fa9e4066Sahrens } 593681d9761SEric Taylor rc = zvol_remove_zv(zv); 594c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 595681d9761SEric Taylor return (rc); 596681d9761SEric Taylor } 597fa9e4066Sahrens 598681d9761SEric Taylor int 599681d9761SEric Taylor zvol_first_open(zvol_state_t *zv) 600681d9761SEric Taylor { 601681d9761SEric Taylor objset_t *os; 602681d9761SEric Taylor uint64_t volsize; 603681d9761SEric Taylor int error; 604681d9761SEric Taylor uint64_t readonly; 605fa9e4066Sahrens 606681d9761SEric Taylor /* lie and say we're read-only */ 607681d9761SEric Taylor error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, B_TRUE, 608681d9761SEric Taylor zvol_tag, &os); 609681d9761SEric Taylor if (error) 610681d9761SEric Taylor return (error); 611fa9e4066Sahrens 612681d9761SEric Taylor error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize); 613681d9761SEric Taylor if (error) { 614681d9761SEric Taylor ASSERT(error == 0); 615681d9761SEric Taylor dmu_objset_disown(os, zvol_tag); 616681d9761SEric Taylor return (error); 617681d9761SEric Taylor } 618681d9761SEric Taylor zv->zv_objset = os; 61994d1a210STim Haley error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf); 62094d1a210STim Haley if (error) { 62194d1a210STim Haley dmu_objset_disown(os, zvol_tag); 62294d1a210STim Haley return (error); 62394d1a210STim Haley } 624681d9761SEric Taylor zv->zv_volsize = volsize; 625681d9761SEric Taylor zv->zv_zilog = zil_open(os, zvol_get_data); 626681d9761SEric Taylor zvol_size_changed(zv->zv_volsize, ddi_driver_major(zfs_dip), 627681d9761SEric Taylor zv->zv_minor); 628fa9e4066Sahrens 629681d9761SEric Taylor VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &readonly, 630681d9761SEric Taylor NULL) == 0); 631f9af39baSGeorge Wilson if (readonly || dmu_objset_is_snapshot(os) || 632f9af39baSGeorge Wilson !spa_writeable(dmu_objset_spa(os))) 633681d9761SEric Taylor zv->zv_flags |= ZVOL_RDONLY; 634681d9761SEric Taylor else 635681d9761SEric Taylor zv->zv_flags &= ~ZVOL_RDONLY; 636681d9761SEric Taylor return (error); 637681d9761SEric Taylor } 638fa9e4066Sahrens 639681d9761SEric Taylor void 640681d9761SEric Taylor zvol_last_close(zvol_state_t *zv) 641681d9761SEric Taylor { 64222ac5be4Sperrin zil_close(zv->zv_zilog); 64322ac5be4Sperrin zv->zv_zilog = NULL; 64494d1a210STim Haley dmu_buf_rele(zv->zv_dbuf, zvol_tag); 64594d1a210STim Haley zv->zv_dbuf = NULL; 646503ad85cSMatthew Ahrens dmu_objset_disown(zv->zv_objset, zvol_tag); 647fa9e4066Sahrens zv->zv_objset = NULL; 648fa9e4066Sahrens } 649fa9e4066Sahrens 650e7cbe64fSgw int 651e7cbe64fSgw zvol_prealloc(zvol_state_t *zv) 652e7cbe64fSgw { 653e7cbe64fSgw objset_t *os = zv->zv_objset; 654e7cbe64fSgw dmu_tx_t *tx; 655e7cbe64fSgw uint64_t refd, avail, usedobjs, availobjs; 656e7cbe64fSgw uint64_t resid = zv->zv_volsize; 657e7cbe64fSgw uint64_t off = 0; 658e7cbe64fSgw 659e7cbe64fSgw /* Check the space usage before attempting to allocate the space */ 660e7cbe64fSgw dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs); 661e7cbe64fSgw if (avail < zv->zv_volsize) 662e7cbe64fSgw return (ENOSPC); 663e7cbe64fSgw 664e7cbe64fSgw /* Free old extents if they exist */ 665e7cbe64fSgw zvol_free_extents(zv); 666e7cbe64fSgw 667e7cbe64fSgw while (resid != 0) { 668e7cbe64fSgw int error; 669e7cbe64fSgw uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE); 670e7cbe64fSgw 671e7cbe64fSgw tx = dmu_tx_create(os); 672e7cbe64fSgw dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 673e7cbe64fSgw error = dmu_tx_assign(tx, TXG_WAIT); 674e7cbe64fSgw if (error) { 675e7cbe64fSgw dmu_tx_abort(tx); 676cdb0ab79Smaybee (void) dmu_free_long_range(os, ZVOL_OBJ, 0, off); 677e7cbe64fSgw return (error); 678e7cbe64fSgw } 67982c9918fSTim Haley dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx); 680e7cbe64fSgw dmu_tx_commit(tx); 681e7cbe64fSgw off += bytes; 682e7cbe64fSgw resid -= bytes; 683e7cbe64fSgw } 684e7cbe64fSgw txg_wait_synced(dmu_objset_pool(os), 0); 685e7cbe64fSgw 686e7cbe64fSgw return (0); 687e7cbe64fSgw } 688e7cbe64fSgw 689e7cbe64fSgw int 690681d9761SEric Taylor zvol_update_volsize(objset_t *os, uint64_t volsize) 691e7cbe64fSgw { 692e7cbe64fSgw dmu_tx_t *tx; 693e7cbe64fSgw int error; 694e7cbe64fSgw 695c99e4bdcSChris Kirby ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 696e7cbe64fSgw 697681d9761SEric Taylor tx = dmu_tx_create(os); 698e7cbe64fSgw dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 699e7cbe64fSgw error = dmu_tx_assign(tx, TXG_WAIT); 700e7cbe64fSgw if (error) { 701e7cbe64fSgw dmu_tx_abort(tx); 702e7cbe64fSgw return (error); 703e7cbe64fSgw } 704e7cbe64fSgw 705681d9761SEric Taylor error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, 706e7cbe64fSgw &volsize, tx); 707e7cbe64fSgw dmu_tx_commit(tx); 708e7cbe64fSgw 709e7cbe64fSgw if (error == 0) 710681d9761SEric Taylor error = dmu_free_long_range(os, 711cdb0ab79Smaybee ZVOL_OBJ, volsize, DMU_OBJECT_END); 712681d9761SEric Taylor return (error); 713681d9761SEric Taylor } 714e7cbe64fSgw 715681d9761SEric Taylor void 716681d9761SEric Taylor zvol_remove_minors(const char *name) 717681d9761SEric Taylor { 718681d9761SEric Taylor zvol_state_t *zv; 719681d9761SEric Taylor char *namebuf; 720681d9761SEric Taylor minor_t minor; 721681d9761SEric Taylor 722681d9761SEric Taylor namebuf = kmem_zalloc(strlen(name) + 2, KM_SLEEP); 723681d9761SEric Taylor (void) strncpy(namebuf, name, strlen(name)); 724681d9761SEric Taylor (void) strcat(namebuf, "/"); 725c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 726c99e4bdcSChris Kirby for (minor = 1; minor <= ZFSDEV_MAX_MINOR; minor++) { 727681d9761SEric Taylor 728c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 729681d9761SEric Taylor if (zv == NULL) 730681d9761SEric Taylor continue; 731681d9761SEric Taylor if (strncmp(namebuf, zv->zv_name, strlen(namebuf)) == 0) 732681d9761SEric Taylor (void) zvol_remove_zv(zv); 733e7cbe64fSgw } 734681d9761SEric Taylor kmem_free(namebuf, strlen(name) + 2); 735681d9761SEric Taylor 736c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 737e7cbe64fSgw } 738e7cbe64fSgw 739fa9e4066Sahrens int 74091ebeef5Sahrens zvol_set_volsize(const char *name, major_t maj, uint64_t volsize) 741fa9e4066Sahrens { 742681d9761SEric Taylor zvol_state_t *zv = NULL; 743681d9761SEric Taylor objset_t *os; 744fa9e4066Sahrens int error; 7455c5460e9Seschrock dmu_object_info_t doi; 746e7cbe64fSgw uint64_t old_volsize = 0ULL; 747681d9761SEric Taylor uint64_t readonly; 748fa9e4066Sahrens 749c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 750681d9761SEric Taylor zv = zvol_minor_lookup(name); 751681d9761SEric Taylor if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) { 752c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 753681d9761SEric Taylor return (error); 754fa9e4066Sahrens } 755fa9e4066Sahrens 756681d9761SEric Taylor if ((error = dmu_object_info(os, ZVOL_OBJ, &doi)) != 0 || 757e9dbad6fSeschrock (error = zvol_check_volsize(volsize, 758bb0ade09Sahrens doi.doi_data_block_size)) != 0) 759bb0ade09Sahrens goto out; 7605c5460e9Seschrock 761681d9761SEric Taylor VERIFY(dsl_prop_get_integer(name, "readonly", &readonly, 762681d9761SEric Taylor NULL) == 0); 763681d9761SEric Taylor if (readonly) { 764bb0ade09Sahrens error = EROFS; 765bb0ade09Sahrens goto out; 766fa9e4066Sahrens } 767fa9e4066Sahrens 768681d9761SEric Taylor error = zvol_update_volsize(os, volsize); 769e7cbe64fSgw /* 770e7cbe64fSgw * Reinitialize the dump area to the new size. If we 771681d9761SEric Taylor * failed to resize the dump area then restore it back to 772681d9761SEric Taylor * its original size. 773e7cbe64fSgw */ 774681d9761SEric Taylor if (zv && error == 0) { 775681d9761SEric Taylor if (zv->zv_flags & ZVOL_DUMPIFIED) { 776681d9761SEric Taylor old_volsize = zv->zv_volsize; 777681d9761SEric Taylor zv->zv_volsize = volsize; 778681d9761SEric Taylor if ((error = zvol_dumpify(zv)) != 0 || 779681d9761SEric Taylor (error = dumpvp_resize()) != 0) { 780681d9761SEric Taylor (void) zvol_update_volsize(os, old_volsize); 781681d9761SEric Taylor zv->zv_volsize = old_volsize; 782681d9761SEric Taylor error = zvol_dumpify(zv); 783681d9761SEric Taylor } 784681d9761SEric Taylor } 785681d9761SEric Taylor if (error == 0) { 786681d9761SEric Taylor zv->zv_volsize = volsize; 787681d9761SEric Taylor zvol_size_changed(volsize, maj, zv->zv_minor); 788e7cbe64fSgw } 789fa9e4066Sahrens } 790fa9e4066Sahrens 791573ca77eSGeorge Wilson /* 792573ca77eSGeorge Wilson * Generate a LUN expansion event. 793573ca77eSGeorge Wilson */ 794681d9761SEric Taylor if (zv && error == 0) { 795573ca77eSGeorge Wilson sysevent_id_t eid; 796573ca77eSGeorge Wilson nvlist_t *attr; 797573ca77eSGeorge Wilson char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 798573ca77eSGeorge Wilson 799681d9761SEric Taylor (void) snprintf(physpath, MAXPATHLEN, "%s%u", ZVOL_PSEUDO_DEV, 800573ca77eSGeorge Wilson zv->zv_minor); 801573ca77eSGeorge Wilson 802573ca77eSGeorge Wilson VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 803573ca77eSGeorge Wilson VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 804573ca77eSGeorge Wilson 805573ca77eSGeorge Wilson (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 806573ca77eSGeorge Wilson ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 807573ca77eSGeorge Wilson 808573ca77eSGeorge Wilson nvlist_free(attr); 809573ca77eSGeorge Wilson kmem_free(physpath, MAXPATHLEN); 810573ca77eSGeorge Wilson } 811573ca77eSGeorge Wilson 812bb0ade09Sahrens out: 813681d9761SEric Taylor dmu_objset_rele(os, FTAG); 814bb0ade09Sahrens 815c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 816fa9e4066Sahrens 817fa9e4066Sahrens return (error); 818fa9e4066Sahrens } 819fa9e4066Sahrens 820fa9e4066Sahrens /*ARGSUSED*/ 821fa9e4066Sahrens int 822fa9e4066Sahrens zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr) 823fa9e4066Sahrens { 824fa9e4066Sahrens zvol_state_t *zv; 825681d9761SEric Taylor int err = 0; 826fa9e4066Sahrens 827c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 828fa9e4066Sahrens 829c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(getminor(*devp), ZSST_ZVOL); 830fa9e4066Sahrens if (zv == NULL) { 831c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 832fa9e4066Sahrens return (ENXIO); 833fa9e4066Sahrens } 834fa9e4066Sahrens 835681d9761SEric Taylor if (zv->zv_total_opens == 0) 836681d9761SEric Taylor err = zvol_first_open(zv); 837681d9761SEric Taylor if (err) { 838c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 839681d9761SEric Taylor return (err); 840681d9761SEric Taylor } 841681d9761SEric Taylor if ((flag & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) { 842681d9761SEric Taylor err = EROFS; 843681d9761SEric Taylor goto out; 844fa9e4066Sahrens } 845c7f714e2SEric Taylor if (zv->zv_flags & ZVOL_EXCL) { 846681d9761SEric Taylor err = EBUSY; 847681d9761SEric Taylor goto out; 848c7f714e2SEric Taylor } 849c7f714e2SEric Taylor if (flag & FEXCL) { 850c7f714e2SEric Taylor if (zv->zv_total_opens != 0) { 851681d9761SEric Taylor err = EBUSY; 852681d9761SEric Taylor goto out; 853c7f714e2SEric Taylor } 854c7f714e2SEric Taylor zv->zv_flags |= ZVOL_EXCL; 855c7f714e2SEric Taylor } 856fa9e4066Sahrens 857fa9e4066Sahrens if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) { 858fa9e4066Sahrens zv->zv_open_count[otyp]++; 859fa9e4066Sahrens zv->zv_total_opens++; 860fa9e4066Sahrens } 861c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 862fa9e4066Sahrens 863681d9761SEric Taylor return (err); 864681d9761SEric Taylor out: 865681d9761SEric Taylor if (zv->zv_total_opens == 0) 866681d9761SEric Taylor zvol_last_close(zv); 867c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 868681d9761SEric Taylor return (err); 869fa9e4066Sahrens } 870fa9e4066Sahrens 871fa9e4066Sahrens /*ARGSUSED*/ 872fa9e4066Sahrens int 873fa9e4066Sahrens zvol_close(dev_t dev, int flag, int otyp, cred_t *cr) 874fa9e4066Sahrens { 875fa9e4066Sahrens minor_t minor = getminor(dev); 876fa9e4066Sahrens zvol_state_t *zv; 877681d9761SEric Taylor int error = 0; 878fa9e4066Sahrens 879c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 880fa9e4066Sahrens 881c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 882fa9e4066Sahrens if (zv == NULL) { 883c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 884fa9e4066Sahrens return (ENXIO); 885fa9e4066Sahrens } 886fa9e4066Sahrens 887c7f714e2SEric Taylor if (zv->zv_flags & ZVOL_EXCL) { 888c7f714e2SEric Taylor ASSERT(zv->zv_total_opens == 1); 889c7f714e2SEric Taylor zv->zv_flags &= ~ZVOL_EXCL; 890fa9e4066Sahrens } 891fa9e4066Sahrens 892fa9e4066Sahrens /* 893fa9e4066Sahrens * If the open count is zero, this is a spurious close. 894fa9e4066Sahrens * That indicates a bug in the kernel / DDI framework. 895fa9e4066Sahrens */ 896fa9e4066Sahrens ASSERT(zv->zv_open_count[otyp] != 0); 897fa9e4066Sahrens ASSERT(zv->zv_total_opens != 0); 898fa9e4066Sahrens 899fa9e4066Sahrens /* 900fa9e4066Sahrens * You may get multiple opens, but only one close. 901fa9e4066Sahrens */ 902fa9e4066Sahrens zv->zv_open_count[otyp]--; 903fa9e4066Sahrens zv->zv_total_opens--; 904fa9e4066Sahrens 905681d9761SEric Taylor if (zv->zv_total_opens == 0) 906681d9761SEric Taylor zvol_last_close(zv); 907fa9e4066Sahrens 908c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 909681d9761SEric Taylor return (error); 910fa9e4066Sahrens } 911fa9e4066Sahrens 912feb08c6bSbillm static void 913b24ab676SJeff Bonwick zvol_get_done(zgd_t *zgd, int error) 91467bd71c6Sperrin { 915b24ab676SJeff Bonwick if (zgd->zgd_db) 916b24ab676SJeff Bonwick dmu_buf_rele(zgd->zgd_db, zgd); 917b24ab676SJeff Bonwick 918b24ab676SJeff Bonwick zfs_range_unlock(zgd->zgd_rl); 919b24ab676SJeff Bonwick 920b24ab676SJeff Bonwick if (error == 0 && zgd->zgd_bp) 921b24ab676SJeff Bonwick zil_add_block(zgd->zgd_zilog, zgd->zgd_bp); 92267bd71c6Sperrin 92367bd71c6Sperrin kmem_free(zgd, sizeof (zgd_t)); 92467bd71c6Sperrin } 92567bd71c6Sperrin 92667bd71c6Sperrin /* 92767bd71c6Sperrin * Get data to generate a TX_WRITE intent log record. 92867bd71c6Sperrin */ 929feb08c6bSbillm static int 93067bd71c6Sperrin zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 93167bd71c6Sperrin { 93267bd71c6Sperrin zvol_state_t *zv = arg; 93367bd71c6Sperrin objset_t *os = zv->zv_objset; 934b24ab676SJeff Bonwick uint64_t object = ZVOL_OBJ; 935b24ab676SJeff Bonwick uint64_t offset = lr->lr_offset; 936b24ab676SJeff Bonwick uint64_t size = lr->lr_length; /* length of user data */ 937b24ab676SJeff Bonwick blkptr_t *bp = &lr->lr_blkptr; 93867bd71c6Sperrin dmu_buf_t *db; 93967bd71c6Sperrin zgd_t *zgd; 94067bd71c6Sperrin int error; 94167bd71c6Sperrin 942b24ab676SJeff Bonwick ASSERT(zio != NULL); 943b24ab676SJeff Bonwick ASSERT(size != 0); 944b24ab676SJeff Bonwick 945b24ab676SJeff Bonwick zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP); 946b24ab676SJeff Bonwick zgd->zgd_zilog = zv->zv_zilog; 947b24ab676SJeff Bonwick zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER); 948feb08c6bSbillm 949c2e6a7d6Sperrin /* 950c2e6a7d6Sperrin * Write records come in two flavors: immediate and indirect. 951c2e6a7d6Sperrin * For small writes it's cheaper to store the data with the 952c2e6a7d6Sperrin * log record (immediate); for large writes it's cheaper to 953c2e6a7d6Sperrin * sync the data and get a pointer to it (indirect) so that 954c2e6a7d6Sperrin * we don't have to write the data twice. 955c2e6a7d6Sperrin */ 956b24ab676SJeff Bonwick if (buf != NULL) { /* immediate write */ 957b24ab676SJeff Bonwick error = dmu_read(os, object, offset, size, buf, 958b24ab676SJeff Bonwick DMU_READ_NO_PREFETCH); 959b24ab676SJeff Bonwick } else { 960b24ab676SJeff Bonwick size = zv->zv_volblocksize; 961b24ab676SJeff Bonwick offset = P2ALIGN(offset, size); 96247cb52daSJeff Bonwick error = dmu_buf_hold(os, object, offset, zgd, &db, 96347cb52daSJeff Bonwick DMU_READ_NO_PREFETCH); 964b24ab676SJeff Bonwick if (error == 0) { 965b24ab676SJeff Bonwick zgd->zgd_db = db; 966b24ab676SJeff Bonwick zgd->zgd_bp = bp; 96767bd71c6Sperrin 968b24ab676SJeff Bonwick ASSERT(db->db_offset == offset); 969b24ab676SJeff Bonwick ASSERT(db->db_size == size); 97067bd71c6Sperrin 971b24ab676SJeff Bonwick error = dmu_sync(zio, lr->lr_common.lrc_txg, 972b24ab676SJeff Bonwick zvol_get_done, zgd); 973975c32a0SNeil Perrin 974b24ab676SJeff Bonwick if (error == 0) 975b24ab676SJeff Bonwick return (0); 976b24ab676SJeff Bonwick } 977975c32a0SNeil Perrin } 978975c32a0SNeil Perrin 979b24ab676SJeff Bonwick zvol_get_done(zgd, error); 980b24ab676SJeff Bonwick 98167bd71c6Sperrin return (error); 98267bd71c6Sperrin } 98367bd71c6Sperrin 984a24e15ceSperrin /* 985a24e15ceSperrin * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions. 98622ac5be4Sperrin * 98722ac5be4Sperrin * We store data in the log buffers if it's small enough. 98867bd71c6Sperrin * Otherwise we will later flush the data out via dmu_sync(). 98922ac5be4Sperrin */ 99067bd71c6Sperrin ssize_t zvol_immediate_write_sz = 32768; 99122ac5be4Sperrin 992feb08c6bSbillm static void 993510b6c0eSNeil Perrin zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid, 994510b6c0eSNeil Perrin boolean_t sync) 99522ac5be4Sperrin { 996feb08c6bSbillm uint32_t blocksize = zv->zv_volblocksize; 9971209a471SNeil Perrin zilog_t *zilog = zv->zv_zilog; 998510b6c0eSNeil Perrin boolean_t slogging; 999e09fa4daSNeil Perrin ssize_t immediate_write_sz; 1000510b6c0eSNeil Perrin 1001b24ab676SJeff Bonwick if (zil_replaying(zilog, tx)) 10021209a471SNeil Perrin return; 10031209a471SNeil Perrin 1004e09fa4daSNeil Perrin immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT) 1005e09fa4daSNeil Perrin ? 0 : zvol_immediate_write_sz; 1006e09fa4daSNeil Perrin 1007e09fa4daSNeil Perrin slogging = spa_has_slogs(zilog->zl_spa) && 1008e09fa4daSNeil Perrin (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY); 1009feb08c6bSbillm 1010510b6c0eSNeil Perrin while (resid) { 1011510b6c0eSNeil Perrin itx_t *itx; 1012510b6c0eSNeil Perrin lr_write_t *lr; 1013510b6c0eSNeil Perrin ssize_t len; 1014510b6c0eSNeil Perrin itx_wr_state_t write_state; 1015510b6c0eSNeil Perrin 1016510b6c0eSNeil Perrin /* 1017510b6c0eSNeil Perrin * Unlike zfs_log_write() we can be called with 1018510b6c0eSNeil Perrin * upto DMU_MAX_ACCESS/2 (5MB) writes. 1019510b6c0eSNeil Perrin */ 1020e09fa4daSNeil Perrin if (blocksize > immediate_write_sz && !slogging && 1021510b6c0eSNeil Perrin resid >= blocksize && off % blocksize == 0) { 1022510b6c0eSNeil Perrin write_state = WR_INDIRECT; /* uses dmu_sync */ 1023510b6c0eSNeil Perrin len = blocksize; 1024510b6c0eSNeil Perrin } else if (sync) { 1025510b6c0eSNeil Perrin write_state = WR_COPIED; 1026510b6c0eSNeil Perrin len = MIN(ZIL_MAX_LOG_DATA, resid); 1027510b6c0eSNeil Perrin } else { 1028510b6c0eSNeil Perrin write_state = WR_NEED_COPY; 1029510b6c0eSNeil Perrin len = MIN(ZIL_MAX_LOG_DATA, resid); 1030510b6c0eSNeil Perrin } 1031510b6c0eSNeil Perrin 1032510b6c0eSNeil Perrin itx = zil_itx_create(TX_WRITE, sizeof (*lr) + 1033510b6c0eSNeil Perrin (write_state == WR_COPIED ? len : 0)); 1034feb08c6bSbillm lr = (lr_write_t *)&itx->itx_lr; 1035510b6c0eSNeil Perrin if (write_state == WR_COPIED && dmu_read(zv->zv_objset, 10367bfdf011SNeil Perrin ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) { 1037b24ab676SJeff Bonwick zil_itx_destroy(itx); 1038510b6c0eSNeil Perrin itx = zil_itx_create(TX_WRITE, sizeof (*lr)); 1039510b6c0eSNeil Perrin lr = (lr_write_t *)&itx->itx_lr; 1040510b6c0eSNeil Perrin write_state = WR_NEED_COPY; 1041510b6c0eSNeil Perrin } 1042510b6c0eSNeil Perrin 1043510b6c0eSNeil Perrin itx->itx_wr_state = write_state; 1044510b6c0eSNeil Perrin if (write_state == WR_NEED_COPY) 1045510b6c0eSNeil Perrin itx->itx_sod += len; 1046feb08c6bSbillm lr->lr_foid = ZVOL_OBJ; 1047feb08c6bSbillm lr->lr_offset = off; 1048510b6c0eSNeil Perrin lr->lr_length = len; 1049b24ab676SJeff Bonwick lr->lr_blkoff = 0; 1050feb08c6bSbillm BP_ZERO(&lr->lr_blkptr); 1051feb08c6bSbillm 1052510b6c0eSNeil Perrin itx->itx_private = zv; 1053510b6c0eSNeil Perrin itx->itx_sync = sync; 1054510b6c0eSNeil Perrin 10555002558fSNeil Perrin zil_itx_assign(zilog, itx, tx); 1056510b6c0eSNeil Perrin 1057510b6c0eSNeil Perrin off += len; 1058510b6c0eSNeil Perrin resid -= len; 105922ac5be4Sperrin } 106022ac5be4Sperrin } 106122ac5be4Sperrin 106288b7b0f2SMatthew Ahrens static int 106388b7b0f2SMatthew Ahrens zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t size, 106488b7b0f2SMatthew Ahrens boolean_t doread, boolean_t isdump) 1065e7cbe64fSgw { 1066e7cbe64fSgw vdev_disk_t *dvd; 1067e7cbe64fSgw int c; 1068e7cbe64fSgw int numerrors = 0; 1069e7cbe64fSgw 1070e7cbe64fSgw for (c = 0; c < vd->vdev_children; c++) { 107121ecdf64SLin Ling ASSERT(vd->vdev_ops == &vdev_mirror_ops || 107221ecdf64SLin Ling vd->vdev_ops == &vdev_replacing_ops || 107321ecdf64SLin Ling vd->vdev_ops == &vdev_spare_ops); 107488b7b0f2SMatthew Ahrens int err = zvol_dumpio_vdev(vd->vdev_child[c], 107588b7b0f2SMatthew Ahrens addr, offset, size, doread, isdump); 107688b7b0f2SMatthew Ahrens if (err != 0) { 1077e7cbe64fSgw numerrors++; 107888b7b0f2SMatthew Ahrens } else if (doread) { 1079e7cbe64fSgw break; 1080e7cbe64fSgw } 1081e7cbe64fSgw } 1082e7cbe64fSgw 1083e7cbe64fSgw if (!vd->vdev_ops->vdev_op_leaf) 1084e7cbe64fSgw return (numerrors < vd->vdev_children ? 0 : EIO); 1085e7cbe64fSgw 1086dc0bb255SEric Taylor if (doread && !vdev_readable(vd)) 1087dc0bb255SEric Taylor return (EIO); 1088dc0bb255SEric Taylor else if (!doread && !vdev_writeable(vd)) 1089e7cbe64fSgw return (EIO); 1090e7cbe64fSgw 1091e7cbe64fSgw dvd = vd->vdev_tsd; 1092e7cbe64fSgw ASSERT3P(dvd, !=, NULL); 1093e7cbe64fSgw offset += VDEV_LABEL_START_SIZE; 1094e7cbe64fSgw 1095e7cbe64fSgw if (ddi_in_panic() || isdump) { 109688b7b0f2SMatthew Ahrens ASSERT(!doread); 109788b7b0f2SMatthew Ahrens if (doread) 1098e7cbe64fSgw return (EIO); 1099e7cbe64fSgw return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset), 1100e7cbe64fSgw lbtodb(size))); 1101e7cbe64fSgw } else { 1102e7cbe64fSgw return (vdev_disk_physio(dvd->vd_lh, addr, size, offset, 110388b7b0f2SMatthew Ahrens doread ? B_READ : B_WRITE)); 1104e7cbe64fSgw } 1105e7cbe64fSgw } 1106e7cbe64fSgw 110788b7b0f2SMatthew Ahrens static int 110888b7b0f2SMatthew Ahrens zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size, 110988b7b0f2SMatthew Ahrens boolean_t doread, boolean_t isdump) 1110e7cbe64fSgw { 1111e7cbe64fSgw vdev_t *vd; 1112e7cbe64fSgw int error; 111388b7b0f2SMatthew Ahrens zvol_extent_t *ze; 1114e7cbe64fSgw spa_t *spa = dmu_objset_spa(zv->zv_objset); 1115e7cbe64fSgw 111688b7b0f2SMatthew Ahrens /* Must be sector aligned, and not stradle a block boundary. */ 111788b7b0f2SMatthew Ahrens if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) || 111888b7b0f2SMatthew Ahrens P2BOUNDARY(offset, size, zv->zv_volblocksize)) { 1119e7cbe64fSgw return (EINVAL); 112088b7b0f2SMatthew Ahrens } 112188b7b0f2SMatthew Ahrens ASSERT(size <= zv->zv_volblocksize); 1122e7cbe64fSgw 112388b7b0f2SMatthew Ahrens /* Locate the extent this belongs to */ 112488b7b0f2SMatthew Ahrens ze = list_head(&zv->zv_extents); 112588b7b0f2SMatthew Ahrens while (offset >= ze->ze_nblks * zv->zv_volblocksize) { 112688b7b0f2SMatthew Ahrens offset -= ze->ze_nblks * zv->zv_volblocksize; 112788b7b0f2SMatthew Ahrens ze = list_next(&zv->zv_extents, ze); 112888b7b0f2SMatthew Ahrens } 112924cc0e1cSGeorge Wilson 113024cc0e1cSGeorge Wilson if (!ddi_in_panic()) 113124cc0e1cSGeorge Wilson spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 113224cc0e1cSGeorge Wilson 113388b7b0f2SMatthew Ahrens vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva)); 113488b7b0f2SMatthew Ahrens offset += DVA_GET_OFFSET(&ze->ze_dva); 113588b7b0f2SMatthew Ahrens error = zvol_dumpio_vdev(vd, addr, offset, size, doread, isdump); 113624cc0e1cSGeorge Wilson 113724cc0e1cSGeorge Wilson if (!ddi_in_panic()) 113824cc0e1cSGeorge Wilson spa_config_exit(spa, SCL_STATE, FTAG); 113924cc0e1cSGeorge Wilson 1140e7cbe64fSgw return (error); 1141e7cbe64fSgw } 1142e7cbe64fSgw 1143fa9e4066Sahrens int 1144fa9e4066Sahrens zvol_strategy(buf_t *bp) 1145fa9e4066Sahrens { 1146c99e4bdcSChris Kirby zfs_soft_state_t *zs = NULL; 1147c99e4bdcSChris Kirby zvol_state_t *zv; 1148fa9e4066Sahrens uint64_t off, volsize; 114988b7b0f2SMatthew Ahrens size_t resid; 1150fa9e4066Sahrens char *addr; 115122ac5be4Sperrin objset_t *os; 1152c2e6a7d6Sperrin rl_t *rl; 1153fa9e4066Sahrens int error = 0; 115488b7b0f2SMatthew Ahrens boolean_t doread = bp->b_flags & B_READ; 1155f80ce222SChris Kirby boolean_t is_dump; 1156510b6c0eSNeil Perrin boolean_t sync; 1157fa9e4066Sahrens 1158c99e4bdcSChris Kirby if (getminor(bp->b_edev) == 0) { 1159c99e4bdcSChris Kirby error = EINVAL; 1160c99e4bdcSChris Kirby } else { 1161c99e4bdcSChris Kirby zs = ddi_get_soft_state(zfsdev_state, getminor(bp->b_edev)); 1162c99e4bdcSChris Kirby if (zs == NULL) 1163c99e4bdcSChris Kirby error = ENXIO; 1164c99e4bdcSChris Kirby else if (zs->zss_type != ZSST_ZVOL) 1165c99e4bdcSChris Kirby error = EINVAL; 1166fa9e4066Sahrens } 1167fa9e4066Sahrens 1168c99e4bdcSChris Kirby if (error) { 1169c99e4bdcSChris Kirby bioerror(bp, error); 1170fa9e4066Sahrens biodone(bp); 1171fa9e4066Sahrens return (0); 1172fa9e4066Sahrens } 1173fa9e4066Sahrens 1174c99e4bdcSChris Kirby zv = zs->zss_data; 1175c99e4bdcSChris Kirby 1176681d9761SEric Taylor if (!(bp->b_flags & B_READ) && (zv->zv_flags & ZVOL_RDONLY)) { 1177fa9e4066Sahrens bioerror(bp, EROFS); 1178fa9e4066Sahrens biodone(bp); 1179fa9e4066Sahrens return (0); 1180fa9e4066Sahrens } 1181fa9e4066Sahrens 1182fa9e4066Sahrens off = ldbtob(bp->b_blkno); 1183fa9e4066Sahrens volsize = zv->zv_volsize; 1184fa9e4066Sahrens 118522ac5be4Sperrin os = zv->zv_objset; 118622ac5be4Sperrin ASSERT(os != NULL); 1187fa9e4066Sahrens 1188fa9e4066Sahrens bp_mapin(bp); 1189fa9e4066Sahrens addr = bp->b_un.b_addr; 1190fa9e4066Sahrens resid = bp->b_bcount; 1191fa9e4066Sahrens 119288b7b0f2SMatthew Ahrens if (resid > 0 && (off < 0 || off >= volsize)) { 119388b7b0f2SMatthew Ahrens bioerror(bp, EIO); 119488b7b0f2SMatthew Ahrens biodone(bp); 119588b7b0f2SMatthew Ahrens return (0); 119688b7b0f2SMatthew Ahrens } 119773ec3d9cSgw 1198f80ce222SChris Kirby is_dump = zv->zv_flags & ZVOL_DUMPIFIED; 119955da60b9SMark J Musante sync = ((!(bp->b_flags & B_ASYNC) && 120055da60b9SMark J Musante !(zv->zv_flags & ZVOL_WCE)) || 120155da60b9SMark J Musante (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)) && 120255da60b9SMark J Musante !doread && !is_dump; 1203510b6c0eSNeil Perrin 1204a24e15ceSperrin /* 1205a24e15ceSperrin * There must be no buffer changes when doing a dmu_sync() because 1206a24e15ceSperrin * we can't change the data whilst calculating the checksum. 1207a24e15ceSperrin */ 1208c2e6a7d6Sperrin rl = zfs_range_lock(&zv->zv_znode, off, resid, 120988b7b0f2SMatthew Ahrens doread ? RL_READER : RL_WRITER); 1210fa9e4066Sahrens 1211e7cbe64fSgw while (resid != 0 && off < volsize) { 121288b7b0f2SMatthew Ahrens size_t size = MIN(resid, zvol_maxphys); 1213e7cbe64fSgw if (is_dump) { 1214e7cbe64fSgw size = MIN(size, P2END(off, zv->zv_volblocksize) - off); 121588b7b0f2SMatthew Ahrens error = zvol_dumpio(zv, addr, off, size, 121688b7b0f2SMatthew Ahrens doread, B_FALSE); 121788b7b0f2SMatthew Ahrens } else if (doread) { 12187bfdf011SNeil Perrin error = dmu_read(os, ZVOL_OBJ, off, size, addr, 12197bfdf011SNeil Perrin DMU_READ_PREFETCH); 1220fa9e4066Sahrens } else { 122122ac5be4Sperrin dmu_tx_t *tx = dmu_tx_create(os); 1222fa9e4066Sahrens dmu_tx_hold_write(tx, ZVOL_OBJ, off, size); 1223fa9e4066Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 1224fa9e4066Sahrens if (error) { 1225fa9e4066Sahrens dmu_tx_abort(tx); 1226fa9e4066Sahrens } else { 122722ac5be4Sperrin dmu_write(os, ZVOL_OBJ, off, size, addr, tx); 1228510b6c0eSNeil Perrin zvol_log_write(zv, tx, off, size, sync); 1229fa9e4066Sahrens dmu_tx_commit(tx); 1230fa9e4066Sahrens } 1231fa9e4066Sahrens } 1232b87f3af3Sperrin if (error) { 1233b87f3af3Sperrin /* convert checksum errors into IO errors */ 1234b87f3af3Sperrin if (error == ECKSUM) 1235b87f3af3Sperrin error = EIO; 1236fa9e4066Sahrens break; 1237b87f3af3Sperrin } 1238fa9e4066Sahrens off += size; 1239fa9e4066Sahrens addr += size; 1240fa9e4066Sahrens resid -= size; 1241fa9e4066Sahrens } 1242c2e6a7d6Sperrin zfs_range_unlock(rl); 1243fa9e4066Sahrens 1244fa9e4066Sahrens if ((bp->b_resid = resid) == bp->b_bcount) 1245fa9e4066Sahrens bioerror(bp, off > volsize ? EINVAL : error); 1246fa9e4066Sahrens 1247510b6c0eSNeil Perrin if (sync) 12485002558fSNeil Perrin zil_commit(zv->zv_zilog, ZVOL_OBJ); 1249feb08c6bSbillm biodone(bp); 125022ac5be4Sperrin 1251fa9e4066Sahrens return (0); 1252fa9e4066Sahrens } 1253fa9e4066Sahrens 125467bd71c6Sperrin /* 125567bd71c6Sperrin * Set the buffer count to the zvol maximum transfer. 125667bd71c6Sperrin * Using our own routine instead of the default minphys() 125767bd71c6Sperrin * means that for larger writes we write bigger buffers on X86 125867bd71c6Sperrin * (128K instead of 56K) and flush the disk write cache less often 125967bd71c6Sperrin * (every zvol_maxphys - currently 1MB) instead of minphys (currently 126067bd71c6Sperrin * 56K on X86 and 128K on sparc). 126167bd71c6Sperrin */ 126267bd71c6Sperrin void 126367bd71c6Sperrin zvol_minphys(struct buf *bp) 126467bd71c6Sperrin { 126567bd71c6Sperrin if (bp->b_bcount > zvol_maxphys) 126667bd71c6Sperrin bp->b_bcount = zvol_maxphys; 126767bd71c6Sperrin } 126867bd71c6Sperrin 1269e7cbe64fSgw int 1270e7cbe64fSgw zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks) 1271e7cbe64fSgw { 1272e7cbe64fSgw minor_t minor = getminor(dev); 1273e7cbe64fSgw zvol_state_t *zv; 1274e7cbe64fSgw int error = 0; 1275e7cbe64fSgw uint64_t size; 1276e7cbe64fSgw uint64_t boff; 1277e7cbe64fSgw uint64_t resid; 1278e7cbe64fSgw 1279c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1280e7cbe64fSgw if (zv == NULL) 1281e7cbe64fSgw return (ENXIO); 1282e7cbe64fSgw 1283e7cbe64fSgw boff = ldbtob(blkno); 1284e7cbe64fSgw resid = ldbtob(nblocks); 128588b7b0f2SMatthew Ahrens 128688b7b0f2SMatthew Ahrens VERIFY3U(boff + resid, <=, zv->zv_volsize); 128788b7b0f2SMatthew Ahrens 1288e7cbe64fSgw while (resid) { 1289e7cbe64fSgw size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff); 129088b7b0f2SMatthew Ahrens error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE); 1291e7cbe64fSgw if (error) 1292e7cbe64fSgw break; 1293e7cbe64fSgw boff += size; 1294e7cbe64fSgw addr += size; 1295e7cbe64fSgw resid -= size; 1296e7cbe64fSgw } 1297e7cbe64fSgw 1298e7cbe64fSgw return (error); 1299e7cbe64fSgw } 1300e7cbe64fSgw 1301fa9e4066Sahrens /*ARGSUSED*/ 1302fa9e4066Sahrens int 1303feb08c6bSbillm zvol_read(dev_t dev, uio_t *uio, cred_t *cr) 1304fa9e4066Sahrens { 1305c7ca1008Sgw minor_t minor = getminor(dev); 1306c7ca1008Sgw zvol_state_t *zv; 130773ec3d9cSgw uint64_t volsize; 1308c2e6a7d6Sperrin rl_t *rl; 1309feb08c6bSbillm int error = 0; 1310fa9e4066Sahrens 1311c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1312c7ca1008Sgw if (zv == NULL) 1313c7ca1008Sgw return (ENXIO); 1314c7ca1008Sgw 131573ec3d9cSgw volsize = zv->zv_volsize; 131673ec3d9cSgw if (uio->uio_resid > 0 && 131773ec3d9cSgw (uio->uio_loffset < 0 || uio->uio_loffset >= volsize)) 131873ec3d9cSgw return (EIO); 131973ec3d9cSgw 132088b7b0f2SMatthew Ahrens if (zv->zv_flags & ZVOL_DUMPIFIED) { 132188b7b0f2SMatthew Ahrens error = physio(zvol_strategy, NULL, dev, B_READ, 132288b7b0f2SMatthew Ahrens zvol_minphys, uio); 132388b7b0f2SMatthew Ahrens return (error); 132488b7b0f2SMatthew Ahrens } 132588b7b0f2SMatthew Ahrens 1326c2e6a7d6Sperrin rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 1327c2e6a7d6Sperrin RL_READER); 132873ec3d9cSgw while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 1329feb08c6bSbillm uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 1330fa9e4066Sahrens 133173ec3d9cSgw /* don't read past the end */ 133273ec3d9cSgw if (bytes > volsize - uio->uio_loffset) 133373ec3d9cSgw bytes = volsize - uio->uio_loffset; 133473ec3d9cSgw 1335feb08c6bSbillm error = dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes); 1336b87f3af3Sperrin if (error) { 1337b87f3af3Sperrin /* convert checksum errors into IO errors */ 1338b87f3af3Sperrin if (error == ECKSUM) 1339b87f3af3Sperrin error = EIO; 1340feb08c6bSbillm break; 1341b87f3af3Sperrin } 1342feb08c6bSbillm } 1343c2e6a7d6Sperrin zfs_range_unlock(rl); 1344feb08c6bSbillm return (error); 1345fa9e4066Sahrens } 1346fa9e4066Sahrens 1347fa9e4066Sahrens /*ARGSUSED*/ 1348fa9e4066Sahrens int 1349feb08c6bSbillm zvol_write(dev_t dev, uio_t *uio, cred_t *cr) 1350fa9e4066Sahrens { 1351c7ca1008Sgw minor_t minor = getminor(dev); 1352c7ca1008Sgw zvol_state_t *zv; 135373ec3d9cSgw uint64_t volsize; 1354c2e6a7d6Sperrin rl_t *rl; 1355feb08c6bSbillm int error = 0; 1356510b6c0eSNeil Perrin boolean_t sync; 1357feb08c6bSbillm 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 1367e7cbe64fSgw if (zv->zv_flags & ZVOL_DUMPIFIED) { 1368e7cbe64fSgw error = physio(zvol_strategy, NULL, dev, B_WRITE, 1369e7cbe64fSgw zvol_minphys, uio); 1370e7cbe64fSgw return (error); 1371e7cbe64fSgw } 1372e7cbe64fSgw 137355da60b9SMark J Musante sync = !(zv->zv_flags & ZVOL_WCE) || 137455da60b9SMark J Musante (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS); 1375510b6c0eSNeil Perrin 1376c2e6a7d6Sperrin rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 1377c2e6a7d6Sperrin RL_WRITER); 137873ec3d9cSgw while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 1379feb08c6bSbillm uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 1380feb08c6bSbillm uint64_t off = uio->uio_loffset; 1381feb08c6bSbillm dmu_tx_t *tx = dmu_tx_create(zv->zv_objset); 138273ec3d9cSgw 138373ec3d9cSgw if (bytes > volsize - off) /* don't write past the end */ 138473ec3d9cSgw bytes = volsize - off; 138573ec3d9cSgw 1386feb08c6bSbillm dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 1387feb08c6bSbillm error = dmu_tx_assign(tx, TXG_WAIT); 1388feb08c6bSbillm if (error) { 1389feb08c6bSbillm dmu_tx_abort(tx); 1390feb08c6bSbillm break; 1391feb08c6bSbillm } 139294d1a210STim Haley error = dmu_write_uio_dbuf(zv->zv_dbuf, uio, bytes, tx); 1393feb08c6bSbillm if (error == 0) 1394510b6c0eSNeil Perrin zvol_log_write(zv, tx, off, bytes, sync); 1395feb08c6bSbillm dmu_tx_commit(tx); 1396feb08c6bSbillm 1397feb08c6bSbillm if (error) 1398feb08c6bSbillm break; 1399feb08c6bSbillm } 1400c2e6a7d6Sperrin zfs_range_unlock(rl); 1401510b6c0eSNeil Perrin if (sync) 14025002558fSNeil Perrin zil_commit(zv->zv_zilog, ZVOL_OBJ); 1403feb08c6bSbillm return (error); 1404fa9e4066Sahrens } 1405fa9e4066Sahrens 1406c7f714e2SEric Taylor int 1407c7f714e2SEric Taylor zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs) 1408c7f714e2SEric Taylor { 1409c7f714e2SEric Taylor struct uuid uuid = EFI_RESERVED; 1410c7f714e2SEric Taylor efi_gpe_t gpe = { 0 }; 1411c7f714e2SEric Taylor uint32_t crc; 1412c7f714e2SEric Taylor dk_efi_t efi; 1413c7f714e2SEric Taylor int length; 1414c7f714e2SEric Taylor char *ptr; 1415c7f714e2SEric Taylor 1416c7f714e2SEric Taylor if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag)) 1417c7f714e2SEric Taylor return (EFAULT); 1418c7f714e2SEric Taylor ptr = (char *)(uintptr_t)efi.dki_data_64; 1419c7f714e2SEric Taylor length = efi.dki_length; 1420c7f714e2SEric Taylor /* 1421c7f714e2SEric Taylor * Some clients may attempt to request a PMBR for the 1422c7f714e2SEric Taylor * zvol. Currently this interface will return EINVAL to 1423c7f714e2SEric Taylor * such requests. These requests could be supported by 1424c7f714e2SEric Taylor * adding a check for lba == 0 and consing up an appropriate 1425c7f714e2SEric Taylor * PMBR. 1426c7f714e2SEric Taylor */ 1427c7f714e2SEric Taylor if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0) 1428c7f714e2SEric Taylor return (EINVAL); 1429c7f714e2SEric Taylor 1430c7f714e2SEric Taylor gpe.efi_gpe_StartingLBA = LE_64(34ULL); 1431c7f714e2SEric Taylor gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1); 1432c7f714e2SEric Taylor UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid); 1433c7f714e2SEric Taylor 1434c7f714e2SEric Taylor if (efi.dki_lba == 1) { 1435c7f714e2SEric Taylor efi_gpt_t gpt = { 0 }; 1436c7f714e2SEric Taylor 1437c7f714e2SEric Taylor gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE); 1438c7f714e2SEric Taylor gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT); 1439c7f714e2SEric Taylor gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt)); 1440c7f714e2SEric Taylor gpt.efi_gpt_MyLBA = LE_64(1ULL); 1441c7f714e2SEric Taylor gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL); 1442c7f714e2SEric Taylor gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1); 1443c7f714e2SEric Taylor gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL); 1444c7f714e2SEric Taylor gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1); 1445c7f714e2SEric Taylor gpt.efi_gpt_SizeOfPartitionEntry = 1446c7f714e2SEric Taylor LE_32(sizeof (efi_gpe_t)); 1447c7f714e2SEric Taylor CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table); 1448c7f714e2SEric Taylor gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc); 1449c7f714e2SEric Taylor CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table); 1450c7f714e2SEric Taylor gpt.efi_gpt_HeaderCRC32 = LE_32(~crc); 1451c7f714e2SEric Taylor if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length), 1452c7f714e2SEric Taylor flag)) 1453c7f714e2SEric Taylor return (EFAULT); 1454c7f714e2SEric Taylor ptr += sizeof (gpt); 1455c7f714e2SEric Taylor length -= sizeof (gpt); 1456c7f714e2SEric Taylor } 1457c7f714e2SEric Taylor if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe), 1458c7f714e2SEric Taylor length), flag)) 1459c7f714e2SEric Taylor return (EFAULT); 1460c7f714e2SEric Taylor return (0); 1461c7f714e2SEric Taylor } 1462c7f714e2SEric Taylor 14633fb517f7SJames Moore /* 14643fb517f7SJames Moore * BEGIN entry points to allow external callers access to the volume. 14653fb517f7SJames Moore */ 14663fb517f7SJames Moore /* 14673fb517f7SJames Moore * Return the volume parameters needed for access from an external caller. 14683fb517f7SJames Moore * These values are invariant as long as the volume is held open. 14693fb517f7SJames Moore */ 14703fb517f7SJames Moore int 14713fb517f7SJames Moore zvol_get_volume_params(minor_t minor, uint64_t *blksize, 14723fb517f7SJames Moore uint64_t *max_xfer_len, void **minor_hdl, void **objset_hdl, void **zil_hdl, 14733fb517f7SJames Moore void **rl_hdl, void **bonus_hdl) 14743fb517f7SJames Moore { 14753fb517f7SJames Moore zvol_state_t *zv; 14763fb517f7SJames Moore 1477c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1478c99e4bdcSChris Kirby if (zv == NULL) 14793fb517f7SJames Moore return (ENXIO); 14803fb517f7SJames Moore if (zv->zv_flags & ZVOL_DUMPIFIED) 14813fb517f7SJames Moore return (ENXIO); 14823fb517f7SJames Moore 14833fb517f7SJames Moore ASSERT(blksize && max_xfer_len && minor_hdl && 14843fb517f7SJames Moore objset_hdl && zil_hdl && rl_hdl && bonus_hdl); 14853fb517f7SJames Moore 14863fb517f7SJames Moore *blksize = zv->zv_volblocksize; 14873fb517f7SJames Moore *max_xfer_len = (uint64_t)zvol_maxphys; 14883fb517f7SJames Moore *minor_hdl = zv; 14893fb517f7SJames Moore *objset_hdl = zv->zv_objset; 14903fb517f7SJames Moore *zil_hdl = zv->zv_zilog; 14913fb517f7SJames Moore *rl_hdl = &zv->zv_znode; 14923fb517f7SJames Moore *bonus_hdl = zv->zv_dbuf; 14933fb517f7SJames Moore return (0); 14943fb517f7SJames Moore } 14953fb517f7SJames Moore 14963fb517f7SJames Moore /* 14973fb517f7SJames Moore * Return the current volume size to an external caller. 14983fb517f7SJames Moore * The size can change while the volume is open. 14993fb517f7SJames Moore */ 15003fb517f7SJames Moore uint64_t 15013fb517f7SJames Moore zvol_get_volume_size(void *minor_hdl) 15023fb517f7SJames Moore { 15033fb517f7SJames Moore zvol_state_t *zv = minor_hdl; 15043fb517f7SJames Moore 15053fb517f7SJames Moore return (zv->zv_volsize); 15063fb517f7SJames Moore } 15073fb517f7SJames Moore 15083fb517f7SJames Moore /* 15093fb517f7SJames Moore * Return the current WCE setting to an external caller. 15103fb517f7SJames Moore * The WCE setting can change while the volume is open. 15113fb517f7SJames Moore */ 15123fb517f7SJames Moore int 15133fb517f7SJames Moore zvol_get_volume_wce(void *minor_hdl) 15143fb517f7SJames Moore { 15153fb517f7SJames Moore zvol_state_t *zv = minor_hdl; 15163fb517f7SJames Moore 15173fb517f7SJames Moore return ((zv->zv_flags & ZVOL_WCE) ? 1 : 0); 15183fb517f7SJames Moore } 15193fb517f7SJames Moore 15203fb517f7SJames Moore /* 15213fb517f7SJames Moore * Entry point for external callers to zvol_log_write 15223fb517f7SJames Moore */ 15233fb517f7SJames Moore void 15243fb517f7SJames Moore zvol_log_write_minor(void *minor_hdl, dmu_tx_t *tx, offset_t off, ssize_t resid, 15253fb517f7SJames Moore boolean_t sync) 15263fb517f7SJames Moore { 15273fb517f7SJames Moore zvol_state_t *zv = minor_hdl; 15283fb517f7SJames Moore 15293fb517f7SJames Moore zvol_log_write(zv, tx, off, resid, sync); 15303fb517f7SJames Moore } 15313fb517f7SJames Moore /* 15323fb517f7SJames Moore * END entry points to allow external callers access to the volume. 15333fb517f7SJames Moore */ 15343fb517f7SJames Moore 1535b77b9231SDan McDonald /* 1536b77b9231SDan McDonald * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE. 1537b77b9231SDan McDonald */ 1538b77b9231SDan McDonald static void 1539b77b9231SDan McDonald zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off, uint64_t len, 1540b77b9231SDan McDonald boolean_t sync) 1541b77b9231SDan McDonald { 1542b77b9231SDan McDonald itx_t *itx; 1543b77b9231SDan McDonald lr_truncate_t *lr; 1544b77b9231SDan McDonald zilog_t *zilog = zv->zv_zilog; 1545b77b9231SDan McDonald 1546b77b9231SDan McDonald if (zil_replaying(zilog, tx)) 1547b77b9231SDan McDonald return; 1548b77b9231SDan McDonald 1549b77b9231SDan McDonald itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr)); 1550b77b9231SDan McDonald lr = (lr_truncate_t *)&itx->itx_lr; 1551b77b9231SDan McDonald lr->lr_foid = ZVOL_OBJ; 1552b77b9231SDan McDonald lr->lr_offset = off; 1553b77b9231SDan McDonald lr->lr_length = len; 1554b77b9231SDan McDonald 1555b77b9231SDan McDonald itx->itx_sync = sync; 1556b77b9231SDan McDonald zil_itx_assign(zilog, itx, tx); 1557b77b9231SDan McDonald } 1558b77b9231SDan McDonald 1559fa9e4066Sahrens /* 1560fa9e4066Sahrens * Dirtbag ioctls to support mkfs(1M) for UFS filesystems. See dkio(7I). 1561b77b9231SDan McDonald * Also a dirtbag dkio ioctl for unmap/free-block functionality. 1562fa9e4066Sahrens */ 1563fa9e4066Sahrens /*ARGSUSED*/ 1564fa9e4066Sahrens int 1565fa9e4066Sahrens zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 1566fa9e4066Sahrens { 1567fa9e4066Sahrens zvol_state_t *zv; 1568af2c4821Smaybee struct dk_cinfo dki; 1569fa9e4066Sahrens struct dk_minfo dkm; 1570af2c4821Smaybee struct dk_callback *dkc; 1571fa9e4066Sahrens int error = 0; 1572e7cbe64fSgw rl_t *rl; 1573fa9e4066Sahrens 1574c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 1575fa9e4066Sahrens 1576c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(getminor(dev), ZSST_ZVOL); 1577fa9e4066Sahrens 1578fa9e4066Sahrens if (zv == NULL) { 1579c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 1580fa9e4066Sahrens return (ENXIO); 1581fa9e4066Sahrens } 1582701f66c4SEric Taylor ASSERT(zv->zv_total_opens > 0); 1583fa9e4066Sahrens 1584fa9e4066Sahrens switch (cmd) { 1585fa9e4066Sahrens 1586fa9e4066Sahrens case DKIOCINFO: 1587af2c4821Smaybee bzero(&dki, sizeof (dki)); 1588af2c4821Smaybee (void) strcpy(dki.dki_cname, "zvol"); 1589af2c4821Smaybee (void) strcpy(dki.dki_dname, "zvol"); 1590af2c4821Smaybee dki.dki_ctype = DKC_UNKNOWN; 15913adc9019SEric Taylor dki.dki_unit = getminor(dev); 1592af2c4821Smaybee dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs); 1593c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 1594af2c4821Smaybee if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag)) 1595fa9e4066Sahrens error = EFAULT; 1596fa9e4066Sahrens return (error); 1597fa9e4066Sahrens 1598fa9e4066Sahrens case DKIOCGMEDIAINFO: 1599fa9e4066Sahrens bzero(&dkm, sizeof (dkm)); 1600fa9e4066Sahrens dkm.dki_lbsize = 1U << zv->zv_min_bs; 1601fa9e4066Sahrens dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs; 1602fa9e4066Sahrens dkm.dki_media_type = DK_UNKNOWN; 1603c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 1604fa9e4066Sahrens if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag)) 1605fa9e4066Sahrens error = EFAULT; 1606fa9e4066Sahrens return (error); 1607fa9e4066Sahrens 1608fa9e4066Sahrens case DKIOCGETEFI: 1609c7f714e2SEric Taylor { 1610c7f714e2SEric Taylor uint64_t vs = zv->zv_volsize; 1611c7f714e2SEric Taylor uint8_t bs = zv->zv_min_bs; 1612fa9e4066Sahrens 1613c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 1614c7f714e2SEric Taylor error = zvol_getefi((void *)arg, flag, vs, bs); 1615c7f714e2SEric Taylor return (error); 161668a5ac4dSmaybee } 1617fa9e4066Sahrens 1618feb08c6bSbillm case DKIOCFLUSHWRITECACHE: 1619af2c4821Smaybee dkc = (struct dk_callback *)arg; 1620c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 16215002558fSNeil Perrin zil_commit(zv->zv_zilog, ZVOL_OBJ); 1622af2c4821Smaybee if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) { 1623af2c4821Smaybee (*dkc->dkc_callback)(dkc->dkc_cookie, error); 1624af2c4821Smaybee error = 0; 1625af2c4821Smaybee } 1626701f66c4SEric Taylor return (error); 1627701f66c4SEric Taylor 1628701f66c4SEric Taylor case DKIOCGETWCE: 1629701f66c4SEric Taylor { 1630701f66c4SEric Taylor int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0; 1631701f66c4SEric Taylor if (ddi_copyout(&wce, (void *)arg, sizeof (int), 1632701f66c4SEric Taylor flag)) 1633701f66c4SEric Taylor error = EFAULT; 1634701f66c4SEric Taylor break; 1635701f66c4SEric Taylor } 1636701f66c4SEric Taylor case DKIOCSETWCE: 1637701f66c4SEric Taylor { 1638701f66c4SEric Taylor int wce; 1639701f66c4SEric Taylor if (ddi_copyin((void *)arg, &wce, sizeof (int), 1640701f66c4SEric Taylor flag)) { 1641701f66c4SEric Taylor error = EFAULT; 1642701f66c4SEric Taylor break; 1643701f66c4SEric Taylor } 1644701f66c4SEric Taylor if (wce) { 1645701f66c4SEric Taylor zv->zv_flags |= ZVOL_WCE; 1646c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 1647701f66c4SEric Taylor } else { 1648701f66c4SEric Taylor zv->zv_flags &= ~ZVOL_WCE; 1649c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 16505002558fSNeil Perrin zil_commit(zv->zv_zilog, ZVOL_OBJ); 1651701f66c4SEric Taylor } 1652701f66c4SEric Taylor return (0); 1653701f66c4SEric Taylor } 1654feb08c6bSbillm 1655b6130eadSmaybee case DKIOCGGEOM: 1656b6130eadSmaybee case DKIOCGVTOC: 1657e7cbe64fSgw /* 1658e7cbe64fSgw * commands using these (like prtvtoc) expect ENOTSUP 1659e7cbe64fSgw * since we're emulating an EFI label 1660e7cbe64fSgw */ 1661b6130eadSmaybee error = ENOTSUP; 1662b6130eadSmaybee break; 1663b6130eadSmaybee 1664e7cbe64fSgw case DKIOCDUMPINIT: 1665e7cbe64fSgw rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 1666e7cbe64fSgw RL_WRITER); 1667e7cbe64fSgw error = zvol_dumpify(zv); 1668e7cbe64fSgw zfs_range_unlock(rl); 1669e7cbe64fSgw break; 1670e7cbe64fSgw 1671e7cbe64fSgw case DKIOCDUMPFINI: 167206d5ae10SEric Taylor if (!(zv->zv_flags & ZVOL_DUMPIFIED)) 167306d5ae10SEric Taylor break; 1674e7cbe64fSgw rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 1675e7cbe64fSgw RL_WRITER); 1676e7cbe64fSgw error = zvol_dump_fini(zv); 1677e7cbe64fSgw zfs_range_unlock(rl); 1678e7cbe64fSgw break; 1679e7cbe64fSgw 1680b77b9231SDan McDonald case DKIOCFREE: 1681b77b9231SDan McDonald { 1682b77b9231SDan McDonald dkioc_free_t df; 1683b77b9231SDan McDonald dmu_tx_t *tx; 1684b77b9231SDan McDonald 1685b77b9231SDan McDonald if (ddi_copyin((void *)arg, &df, sizeof (df), flag)) { 1686b77b9231SDan McDonald error = EFAULT; 1687b77b9231SDan McDonald break; 1688b77b9231SDan McDonald } 1689b77b9231SDan McDonald 1690b77b9231SDan McDonald /* 1691b77b9231SDan McDonald * Apply Postel's Law to length-checking. If they overshoot, 1692b77b9231SDan McDonald * just blank out until the end, if there's a need to blank 1693b77b9231SDan McDonald * out anything. 1694b77b9231SDan McDonald */ 1695b77b9231SDan McDonald if (df.df_start >= zv->zv_volsize) 1696b77b9231SDan McDonald break; /* No need to do anything... */ 1697b77b9231SDan McDonald if (df.df_start + df.df_length > zv->zv_volsize) 1698b77b9231SDan McDonald df.df_length = DMU_OBJECT_END; 1699b77b9231SDan McDonald 1700b77b9231SDan McDonald rl = zfs_range_lock(&zv->zv_znode, df.df_start, df.df_length, 1701b77b9231SDan McDonald RL_WRITER); 1702b77b9231SDan McDonald tx = dmu_tx_create(zv->zv_objset); 1703b77b9231SDan McDonald error = dmu_tx_assign(tx, TXG_WAIT); 1704b77b9231SDan McDonald if (error != 0) { 1705b77b9231SDan McDonald dmu_tx_abort(tx); 1706b77b9231SDan McDonald } else { 1707b77b9231SDan McDonald zvol_log_truncate(zv, tx, df.df_start, 1708b77b9231SDan McDonald df.df_length, B_TRUE); 1709c08b1637SJosef 'Jeff' Sipek dmu_tx_commit(tx); 1710b77b9231SDan McDonald error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 1711b77b9231SDan McDonald df.df_start, df.df_length); 1712b77b9231SDan McDonald } 1713b77b9231SDan McDonald 1714b77b9231SDan McDonald zfs_range_unlock(rl); 1715b77b9231SDan McDonald 1716b77b9231SDan McDonald if (error == 0) { 1717b77b9231SDan McDonald /* 1718b77b9231SDan McDonald * If the write-cache is disabled or 'sync' property 1719b77b9231SDan McDonald * is set to 'always' then treat this as a synchronous 1720b77b9231SDan McDonald * operation (i.e. commit to zil). 1721b77b9231SDan McDonald */ 1722b77b9231SDan McDonald if (!(zv->zv_flags & ZVOL_WCE) || 1723b77b9231SDan McDonald (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)) 1724b77b9231SDan McDonald zil_commit(zv->zv_zilog, ZVOL_OBJ); 1725b77b9231SDan McDonald 1726b77b9231SDan McDonald /* 1727b77b9231SDan McDonald * If the caller really wants synchronous writes, and 1728b77b9231SDan McDonald * can't wait for them, don't return until the write 1729b77b9231SDan McDonald * is done. 1730b77b9231SDan McDonald */ 1731b77b9231SDan McDonald if (df.df_flags & DF_WAIT_SYNC) { 1732b77b9231SDan McDonald txg_wait_synced( 1733b77b9231SDan McDonald dmu_objset_pool(zv->zv_objset), 0); 1734b77b9231SDan McDonald } 1735b77b9231SDan McDonald } 1736b77b9231SDan McDonald break; 1737b77b9231SDan McDonald } 1738b77b9231SDan McDonald 1739fa9e4066Sahrens default: 174068a5ac4dSmaybee error = ENOTTY; 1741fa9e4066Sahrens break; 1742fa9e4066Sahrens 1743fa9e4066Sahrens } 1744c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 1745fa9e4066Sahrens return (error); 1746fa9e4066Sahrens } 1747fa9e4066Sahrens 1748fa9e4066Sahrens int 1749fa9e4066Sahrens zvol_busy(void) 1750fa9e4066Sahrens { 1751fa9e4066Sahrens return (zvol_minors != 0); 1752fa9e4066Sahrens } 1753fa9e4066Sahrens 1754fa9e4066Sahrens void 1755fa9e4066Sahrens zvol_init(void) 1756fa9e4066Sahrens { 1757c99e4bdcSChris Kirby VERIFY(ddi_soft_state_init(&zfsdev_state, sizeof (zfs_soft_state_t), 1758c99e4bdcSChris Kirby 1) == 0); 1759c99e4bdcSChris Kirby mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL); 1760fa9e4066Sahrens } 1761fa9e4066Sahrens 1762fa9e4066Sahrens void 1763fa9e4066Sahrens zvol_fini(void) 1764fa9e4066Sahrens { 1765c99e4bdcSChris Kirby mutex_destroy(&zfsdev_state_lock); 1766c99e4bdcSChris Kirby ddi_soft_state_fini(&zfsdev_state); 1767fa9e4066Sahrens } 1768e7cbe64fSgw 1769e7cbe64fSgw static int 1770e7cbe64fSgw zvol_dump_init(zvol_state_t *zv, boolean_t resize) 1771e7cbe64fSgw { 1772e7cbe64fSgw dmu_tx_t *tx; 1773e7cbe64fSgw int error = 0; 1774e7cbe64fSgw objset_t *os = zv->zv_objset; 1775e7cbe64fSgw nvlist_t *nv = NULL; 17768d265e66SGeorge Wilson uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset)); 1777e7cbe64fSgw 1778c99e4bdcSChris Kirby ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 1779681d9761SEric Taylor error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0, 1780681d9761SEric Taylor DMU_OBJECT_END); 1781681d9761SEric Taylor /* wait for dmu_free_long_range to actually free the blocks */ 1782681d9761SEric Taylor txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 1783e7cbe64fSgw 1784e7cbe64fSgw tx = dmu_tx_create(os); 1785e7cbe64fSgw dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 1786681d9761SEric Taylor dmu_tx_hold_bonus(tx, ZVOL_OBJ); 1787e7cbe64fSgw error = dmu_tx_assign(tx, TXG_WAIT); 1788e7cbe64fSgw if (error) { 1789e7cbe64fSgw dmu_tx_abort(tx); 1790e7cbe64fSgw return (error); 1791e7cbe64fSgw } 1792e7cbe64fSgw 1793e7cbe64fSgw /* 1794e7cbe64fSgw * If we are resizing the dump device then we only need to 1795e7cbe64fSgw * update the refreservation to match the newly updated 1796e7cbe64fSgw * zvolsize. Otherwise, we save off the original state of the 1797e7cbe64fSgw * zvol so that we can restore them if the zvol is ever undumpified. 1798e7cbe64fSgw */ 1799e7cbe64fSgw if (resize) { 1800e7cbe64fSgw error = zap_update(os, ZVOL_ZAP_OBJ, 1801e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 1802e7cbe64fSgw &zv->zv_volsize, tx); 1803e7cbe64fSgw } else { 1804afee20e4SGeorge Wilson uint64_t checksum, compress, refresrv, vbs, dedup; 180588b7b0f2SMatthew Ahrens 1806e7cbe64fSgw error = dsl_prop_get_integer(zv->zv_name, 1807e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL); 1808e7cbe64fSgw error = error ? error : dsl_prop_get_integer(zv->zv_name, 1809e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL); 1810e7cbe64fSgw error = error ? error : dsl_prop_get_integer(zv->zv_name, 1811e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL); 181288b7b0f2SMatthew Ahrens error = error ? error : dsl_prop_get_integer(zv->zv_name, 181388b7b0f2SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL); 18148d265e66SGeorge Wilson if (version >= SPA_VERSION_DEDUP) { 18158d265e66SGeorge Wilson error = error ? error : 18168d265e66SGeorge Wilson dsl_prop_get_integer(zv->zv_name, 18178d265e66SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), &dedup, NULL); 18188d265e66SGeorge Wilson } 1819e7cbe64fSgw 1820e7cbe64fSgw error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1821e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, 1822e7cbe64fSgw &compress, tx); 1823e7cbe64fSgw error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1824e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx); 1825e7cbe64fSgw error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1826e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 1827e7cbe64fSgw &refresrv, tx); 182888b7b0f2SMatthew Ahrens error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 182988b7b0f2SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, 183088b7b0f2SMatthew Ahrens &vbs, tx); 1831681d9761SEric Taylor error = error ? error : dmu_object_set_blocksize( 1832681d9761SEric Taylor os, ZVOL_OBJ, SPA_MAXBLOCKSIZE, 0, tx); 18338d265e66SGeorge Wilson if (version >= SPA_VERSION_DEDUP) { 18348d265e66SGeorge Wilson error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 18358d265e66SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, 18368d265e66SGeorge Wilson &dedup, tx); 18378d265e66SGeorge Wilson } 1838681d9761SEric Taylor if (error == 0) 1839681d9761SEric Taylor zv->zv_volblocksize = SPA_MAXBLOCKSIZE; 1840e7cbe64fSgw } 1841e7cbe64fSgw dmu_tx_commit(tx); 1842e7cbe64fSgw 1843e7cbe64fSgw /* 1844e7cbe64fSgw * We only need update the zvol's property if we are initializing 1845e7cbe64fSgw * the dump area for the first time. 1846e7cbe64fSgw */ 1847e7cbe64fSgw if (!resize) { 1848e7cbe64fSgw VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1849e7cbe64fSgw VERIFY(nvlist_add_uint64(nv, 1850e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0); 1851e7cbe64fSgw VERIFY(nvlist_add_uint64(nv, 1852e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), 1853e7cbe64fSgw ZIO_COMPRESS_OFF) == 0); 1854e7cbe64fSgw VERIFY(nvlist_add_uint64(nv, 1855e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_CHECKSUM), 1856e7cbe64fSgw ZIO_CHECKSUM_OFF) == 0); 18578d265e66SGeorge Wilson if (version >= SPA_VERSION_DEDUP) { 18588d265e66SGeorge Wilson VERIFY(nvlist_add_uint64(nv, 18598d265e66SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), 18608d265e66SGeorge Wilson ZIO_CHECKSUM_OFF) == 0); 18618d265e66SGeorge Wilson } 1862e7cbe64fSgw 186392241e0bSTom Erickson error = zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL, 186492241e0bSTom Erickson nv, NULL); 1865e7cbe64fSgw nvlist_free(nv); 1866e7cbe64fSgw 1867e7cbe64fSgw if (error) 1868e7cbe64fSgw return (error); 1869e7cbe64fSgw } 1870e7cbe64fSgw 1871e7cbe64fSgw /* Allocate the space for the dump */ 1872e7cbe64fSgw error = zvol_prealloc(zv); 1873e7cbe64fSgw return (error); 1874e7cbe64fSgw } 1875e7cbe64fSgw 1876e7cbe64fSgw static int 1877e7cbe64fSgw zvol_dumpify(zvol_state_t *zv) 1878e7cbe64fSgw { 1879e7cbe64fSgw int error = 0; 1880e7cbe64fSgw uint64_t dumpsize = 0; 1881e7cbe64fSgw dmu_tx_t *tx; 1882e7cbe64fSgw objset_t *os = zv->zv_objset; 1883e7cbe64fSgw 1884681d9761SEric Taylor if (zv->zv_flags & ZVOL_RDONLY) 1885e7cbe64fSgw return (EROFS); 1886e7cbe64fSgw 1887e7cbe64fSgw if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 1888e7cbe64fSgw 8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) { 1889*4445fffbSMatthew Ahrens boolean_t resize = (dumpsize > 0); 1890e7cbe64fSgw 1891e7cbe64fSgw if ((error = zvol_dump_init(zv, resize)) != 0) { 1892e7cbe64fSgw (void) zvol_dump_fini(zv); 1893e7cbe64fSgw return (error); 1894e7cbe64fSgw } 1895e7cbe64fSgw } 1896e7cbe64fSgw 1897e7cbe64fSgw /* 1898e7cbe64fSgw * Build up our lba mapping. 1899e7cbe64fSgw */ 1900e7cbe64fSgw error = zvol_get_lbas(zv); 1901e7cbe64fSgw if (error) { 1902e7cbe64fSgw (void) zvol_dump_fini(zv); 1903e7cbe64fSgw return (error); 1904e7cbe64fSgw } 1905e7cbe64fSgw 1906e7cbe64fSgw tx = dmu_tx_create(os); 1907e7cbe64fSgw dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 1908e7cbe64fSgw error = dmu_tx_assign(tx, TXG_WAIT); 1909e7cbe64fSgw if (error) { 1910e7cbe64fSgw dmu_tx_abort(tx); 1911e7cbe64fSgw (void) zvol_dump_fini(zv); 1912e7cbe64fSgw return (error); 1913e7cbe64fSgw } 1914e7cbe64fSgw 1915e7cbe64fSgw zv->zv_flags |= ZVOL_DUMPIFIED; 1916e7cbe64fSgw error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1, 1917e7cbe64fSgw &zv->zv_volsize, tx); 1918e7cbe64fSgw dmu_tx_commit(tx); 1919e7cbe64fSgw 1920e7cbe64fSgw if (error) { 1921e7cbe64fSgw (void) zvol_dump_fini(zv); 1922e7cbe64fSgw return (error); 1923e7cbe64fSgw } 1924e7cbe64fSgw 1925e7cbe64fSgw txg_wait_synced(dmu_objset_pool(os), 0); 1926e7cbe64fSgw return (0); 1927e7cbe64fSgw } 1928e7cbe64fSgw 1929e7cbe64fSgw static int 1930e7cbe64fSgw zvol_dump_fini(zvol_state_t *zv) 1931e7cbe64fSgw { 1932e7cbe64fSgw dmu_tx_t *tx; 1933e7cbe64fSgw objset_t *os = zv->zv_objset; 1934e7cbe64fSgw nvlist_t *nv; 1935e7cbe64fSgw int error = 0; 1936afee20e4SGeorge Wilson uint64_t checksum, compress, refresrv, vbs, dedup; 19378d265e66SGeorge Wilson uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset)); 1938e7cbe64fSgw 1939b7e50089Smaybee /* 1940b7e50089Smaybee * Attempt to restore the zvol back to its pre-dumpified state. 1941b7e50089Smaybee * This is a best-effort attempt as it's possible that not all 1942b7e50089Smaybee * of these properties were initialized during the dumpify process 1943b7e50089Smaybee * (i.e. error during zvol_dump_init). 1944b7e50089Smaybee */ 1945b7e50089Smaybee 1946e7cbe64fSgw tx = dmu_tx_create(os); 1947e7cbe64fSgw dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 1948e7cbe64fSgw error = dmu_tx_assign(tx, TXG_WAIT); 1949e7cbe64fSgw if (error) { 1950e7cbe64fSgw dmu_tx_abort(tx); 1951e7cbe64fSgw return (error); 1952e7cbe64fSgw } 1953b7e50089Smaybee (void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx); 1954b7e50089Smaybee dmu_tx_commit(tx); 1955e7cbe64fSgw 1956e7cbe64fSgw (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 1957e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum); 1958e7cbe64fSgw (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 1959e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress); 1960e7cbe64fSgw (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 1961e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv); 196288b7b0f2SMatthew Ahrens (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 196388b7b0f2SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs); 1964e7cbe64fSgw 1965e7cbe64fSgw VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1966e7cbe64fSgw (void) nvlist_add_uint64(nv, 1967e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum); 1968e7cbe64fSgw (void) nvlist_add_uint64(nv, 1969e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress); 1970e7cbe64fSgw (void) nvlist_add_uint64(nv, 1971e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv); 19728d265e66SGeorge Wilson if (version >= SPA_VERSION_DEDUP && 19738d265e66SGeorge Wilson zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 19748d265e66SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, &dedup) == 0) { 19758d265e66SGeorge Wilson (void) nvlist_add_uint64(nv, 19768d265e66SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), dedup); 19778d265e66SGeorge Wilson } 197892241e0bSTom Erickson (void) zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL, 197992241e0bSTom Erickson nv, NULL); 1980e7cbe64fSgw nvlist_free(nv); 1981e7cbe64fSgw 1982b7e50089Smaybee zvol_free_extents(zv); 1983b7e50089Smaybee zv->zv_flags &= ~ZVOL_DUMPIFIED; 1984b7e50089Smaybee (void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END); 1985681d9761SEric Taylor /* wait for dmu_free_long_range to actually free the blocks */ 1986681d9761SEric Taylor txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 1987681d9761SEric Taylor tx = dmu_tx_create(os); 1988681d9761SEric Taylor dmu_tx_hold_bonus(tx, ZVOL_OBJ); 1989681d9761SEric Taylor error = dmu_tx_assign(tx, TXG_WAIT); 1990681d9761SEric Taylor if (error) { 1991681d9761SEric Taylor dmu_tx_abort(tx); 1992681d9761SEric Taylor return (error); 1993681d9761SEric Taylor } 1994b24ab676SJeff Bonwick if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0) 1995b24ab676SJeff Bonwick zv->zv_volblocksize = vbs; 1996681d9761SEric Taylor dmu_tx_commit(tx); 1997b7e50089Smaybee 1998e7cbe64fSgw return (0); 1999e7cbe64fSgw } 2000