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. 27b7edcb94SMatthew Ahrens * Copyright (c) 2012, 2017 by Delphix. All rights reserved. 28810e43b2SBill Pijewski * Copyright (c) 2013, Joyent, Inc. All rights reserved. 29c3d26abcSMatthew Ahrens * Copyright (c) 2014 Integros [integros.com] 30fa9e4066Sahrens */ 31fa9e4066Sahrens 32fa9e4066Sahrens /* 33fa9e4066Sahrens * ZFS volume emulation driver. 34fa9e4066Sahrens * 35fa9e4066Sahrens * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes. 36fa9e4066Sahrens * Volumes are accessed through the symbolic links named: 37fa9e4066Sahrens * 38fa9e4066Sahrens * /dev/zvol/dsk/<pool_name>/<dataset_name> 39fa9e4066Sahrens * /dev/zvol/rdsk/<pool_name>/<dataset_name> 40fa9e4066Sahrens * 41681d9761SEric Taylor * These links are created by the /dev filesystem (sdev_zvolops.c). 42fa9e4066Sahrens * Volumes are persistent through reboot. No user command needs to be 43fa9e4066Sahrens * run before opening and using a device. 44fa9e4066Sahrens */ 45fa9e4066Sahrens 46fa9e4066Sahrens #include <sys/types.h> 47fa9e4066Sahrens #include <sys/param.h> 48fa9e4066Sahrens #include <sys/errno.h> 49fa9e4066Sahrens #include <sys/uio.h> 50fa9e4066Sahrens #include <sys/buf.h> 51fa9e4066Sahrens #include <sys/modctl.h> 52fa9e4066Sahrens #include <sys/open.h> 53fa9e4066Sahrens #include <sys/kmem.h> 54fa9e4066Sahrens #include <sys/conf.h> 55fa9e4066Sahrens #include <sys/cmn_err.h> 56fa9e4066Sahrens #include <sys/stat.h> 57fa9e4066Sahrens #include <sys/zap.h> 58fa9e4066Sahrens #include <sys/spa.h> 59810e43b2SBill Pijewski #include <sys/spa_impl.h> 60fa9e4066Sahrens #include <sys/zio.h> 61e7cbe64fSgw #include <sys/dmu_traverse.h> 62e7cbe64fSgw #include <sys/dnode.h> 63e7cbe64fSgw #include <sys/dsl_dataset.h> 64fa9e4066Sahrens #include <sys/dsl_prop.h> 65fa9e4066Sahrens #include <sys/dkio.h> 66fa9e4066Sahrens #include <sys/efi_partition.h> 67fa9e4066Sahrens #include <sys/byteorder.h> 68fa9e4066Sahrens #include <sys/pathname.h> 69fa9e4066Sahrens #include <sys/ddi.h> 70fa9e4066Sahrens #include <sys/sunddi.h> 71fa9e4066Sahrens #include <sys/crc32.h> 72fa9e4066Sahrens #include <sys/dirent.h> 73fa9e4066Sahrens #include <sys/policy.h> 74fa9e4066Sahrens #include <sys/fs/zfs.h> 75fa9e4066Sahrens #include <sys/zfs_ioctl.h> 76fa9e4066Sahrens #include <sys/mkdev.h> 7722ac5be4Sperrin #include <sys/zil.h> 78c5c6ffa0Smaybee #include <sys/refcount.h> 79c2e6a7d6Sperrin #include <sys/zfs_znode.h> 80c2e6a7d6Sperrin #include <sys/zfs_rlock.h> 81e7cbe64fSgw #include <sys/vdev_disk.h> 82e7cbe64fSgw #include <sys/vdev_impl.h> 83810e43b2SBill Pijewski #include <sys/vdev_raidz.h> 84e7cbe64fSgw #include <sys/zvol.h> 85e7cbe64fSgw #include <sys/dumphdr.h> 861209a471SNeil Perrin #include <sys/zil_impl.h> 8780901aeaSGeorge Wilson #include <sys/dbuf.h> 88810e43b2SBill Pijewski #include <sys/dmu_tx.h> 89810e43b2SBill Pijewski #include <sys/zfeature.h> 90810e43b2SBill Pijewski #include <sys/zio_checksum.h> 911271e4b1SPrakash Surya #include <sys/zil_impl.h> 92fa9e4066Sahrens 93fa9e4066Sahrens #include "zfs_namecheck.h" 94fa9e4066Sahrens 95c99e4bdcSChris Kirby void *zfsdev_state; 96503ad85cSMatthew Ahrens static char *zvol_tag = "zvol_tag"; 97fa9e4066Sahrens 98e7cbe64fSgw #define ZVOL_DUMPSIZE "dumpsize" 99e7cbe64fSgw 100fa9e4066Sahrens /* 101c99e4bdcSChris Kirby * This lock protects the zfsdev_state structure from being modified 102fa9e4066Sahrens * while it's being used, e.g. an open that comes in before a create 103fa9e4066Sahrens * finishes. It also protects temporary opens of the dataset so that, 104fa9e4066Sahrens * e.g., an open doesn't get a spurious EBUSY. 105fa9e4066Sahrens */ 106c99e4bdcSChris Kirby kmutex_t zfsdev_state_lock; 107fa9e4066Sahrens static uint32_t zvol_minors; 108fa9e4066Sahrens 109e7cbe64fSgw typedef struct zvol_extent { 11088b7b0f2SMatthew Ahrens list_node_t ze_node; 111e7cbe64fSgw dva_t ze_dva; /* dva associated with this extent */ 11288b7b0f2SMatthew Ahrens uint64_t ze_nblks; /* number of blocks in extent */ 113e7cbe64fSgw } zvol_extent_t; 114e7cbe64fSgw 115fa9e4066Sahrens /* 116fa9e4066Sahrens * The in-core state of each volume. 117fa9e4066Sahrens */ 118fa9e4066Sahrens typedef struct zvol_state { 119fa9e4066Sahrens char zv_name[MAXPATHLEN]; /* pool/dd name */ 120fa9e4066Sahrens uint64_t zv_volsize; /* amount of space we advertise */ 12167bd71c6Sperrin uint64_t zv_volblocksize; /* volume block size */ 122fa9e4066Sahrens minor_t zv_minor; /* minor number */ 123fa9e4066Sahrens uint8_t zv_min_bs; /* minimum addressable block shift */ 124701f66c4SEric Taylor uint8_t zv_flags; /* readonly, dumpified, etc. */ 125fa9e4066Sahrens objset_t *zv_objset; /* objset handle */ 126fa9e4066Sahrens uint32_t zv_open_count[OTYPCNT]; /* open counts */ 127fa9e4066Sahrens uint32_t zv_total_opens; /* total open count */ 12822ac5be4Sperrin zilog_t *zv_zilog; /* ZIL handle */ 12988b7b0f2SMatthew Ahrens list_t zv_extents; /* List of extents for dump */ 130c2e6a7d6Sperrin znode_t zv_znode; /* for range locking */ 131*8dfe5547SRichard Yao dnode_t *zv_dn; /* dnode hold */ 132fa9e4066Sahrens } zvol_state_t; 133fa9e4066Sahrens 134e7cbe64fSgw /* 135e7cbe64fSgw * zvol specific flags 136e7cbe64fSgw */ 137e7cbe64fSgw #define ZVOL_RDONLY 0x1 138e7cbe64fSgw #define ZVOL_DUMPIFIED 0x2 139c7f714e2SEric Taylor #define ZVOL_EXCL 0x4 140701f66c4SEric Taylor #define ZVOL_WCE 0x8 141e7cbe64fSgw 14267bd71c6Sperrin /* 14367bd71c6Sperrin * zvol maximum transfer in one DMU tx. 14467bd71c6Sperrin */ 14567bd71c6Sperrin int zvol_maxphys = DMU_MAX_ACCESS/2; 14667bd71c6Sperrin 147893c83baSGeorge Wilson /* 148893c83baSGeorge Wilson * Toggle unmap functionality. 149893c83baSGeorge Wilson */ 150893c83baSGeorge Wilson boolean_t zvol_unmap_enabled = B_TRUE; 151893c83baSGeorge Wilson 1521c9272b8SStephen Blinick /* 1531c9272b8SStephen Blinick * If true, unmaps requested as synchronous are executed synchronously, 1541c9272b8SStephen Blinick * otherwise all unmaps are asynchronous. 1551c9272b8SStephen Blinick */ 1561c9272b8SStephen Blinick boolean_t zvol_unmap_sync_enabled = B_FALSE; 1571c9272b8SStephen Blinick 15892241e0bSTom Erickson extern int zfs_set_prop_nvlist(const char *, zprop_source_t, 1594445fffbSMatthew Ahrens nvlist_t *, nvlist_t *); 160681d9761SEric Taylor static int zvol_remove_zv(zvol_state_t *); 1611271e4b1SPrakash Surya static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, 1621271e4b1SPrakash Surya struct lwb *lwb, zio_t *zio); 163e7cbe64fSgw static int zvol_dumpify(zvol_state_t *zv); 164e7cbe64fSgw static int zvol_dump_fini(zvol_state_t *zv); 165e7cbe64fSgw static int zvol_dump_init(zvol_state_t *zv, boolean_t resize); 16667bd71c6Sperrin 167fa9e4066Sahrens static void 168c61ea566SGeorge Wilson zvol_size_changed(zvol_state_t *zv, uint64_t volsize) 169fa9e4066Sahrens { 170c61ea566SGeorge Wilson dev_t dev = makedevice(ddi_driver_major(zfs_dip), zv->zv_minor); 171fa9e4066Sahrens 172c61ea566SGeorge Wilson zv->zv_volsize = volsize; 173fa9e4066Sahrens VERIFY(ddi_prop_update_int64(dev, zfs_dip, 174681d9761SEric Taylor "Size", volsize) == DDI_SUCCESS); 175fa9e4066Sahrens VERIFY(ddi_prop_update_int64(dev, zfs_dip, 176681d9761SEric Taylor "Nblocks", lbtodb(volsize)) == DDI_SUCCESS); 177e7cbe64fSgw 178e7cbe64fSgw /* Notify specfs to invalidate the cached size */ 179e7cbe64fSgw spec_size_invalidate(dev, VBLK); 180e7cbe64fSgw spec_size_invalidate(dev, VCHR); 181fa9e4066Sahrens } 182fa9e4066Sahrens 183fa9e4066Sahrens int 184e9dbad6fSeschrock zvol_check_volsize(uint64_t volsize, uint64_t blocksize) 185fa9e4066Sahrens { 186e9dbad6fSeschrock if (volsize == 0) 187be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 188fa9e4066Sahrens 189e9dbad6fSeschrock if (volsize % blocksize != 0) 190be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 1915c5460e9Seschrock 192fa9e4066Sahrens #ifdef _ILP32 193e9dbad6fSeschrock if (volsize - 1 > SPEC_MAXOFFSET_T) 194be6fd75aSMatthew Ahrens return (SET_ERROR(EOVERFLOW)); 195fa9e4066Sahrens #endif 196fa9e4066Sahrens return (0); 197fa9e4066Sahrens } 198fa9e4066Sahrens 199fa9e4066Sahrens int 200e9dbad6fSeschrock zvol_check_volblocksize(uint64_t volblocksize) 201fa9e4066Sahrens { 202e9dbad6fSeschrock if (volblocksize < SPA_MINBLOCKSIZE || 203b5152584SMatthew Ahrens volblocksize > SPA_OLD_MAXBLOCKSIZE || 204e9dbad6fSeschrock !ISP2(volblocksize)) 205be6fd75aSMatthew Ahrens return (SET_ERROR(EDOM)); 206fa9e4066Sahrens 207fa9e4066Sahrens return (0); 208fa9e4066Sahrens } 209fa9e4066Sahrens 210fa9e4066Sahrens int 211a2eea2e1Sahrens zvol_get_stats(objset_t *os, nvlist_t *nv) 212fa9e4066Sahrens { 213fa9e4066Sahrens int error; 214fa9e4066Sahrens dmu_object_info_t doi; 215a2eea2e1Sahrens uint64_t val; 216fa9e4066Sahrens 217a2eea2e1Sahrens error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val); 218fa9e4066Sahrens if (error) 219fa9e4066Sahrens return (error); 220fa9e4066Sahrens 221a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val); 222a2eea2e1Sahrens 223fa9e4066Sahrens error = dmu_object_info(os, ZVOL_OBJ, &doi); 224fa9e4066Sahrens 225a2eea2e1Sahrens if (error == 0) { 226a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE, 227a2eea2e1Sahrens doi.doi_data_block_size); 228a2eea2e1Sahrens } 229fa9e4066Sahrens 230fa9e4066Sahrens return (error); 231fa9e4066Sahrens } 232fa9e4066Sahrens 233fa9e4066Sahrens static zvol_state_t * 234e9dbad6fSeschrock zvol_minor_lookup(const char *name) 235fa9e4066Sahrens { 236fa9e4066Sahrens minor_t minor; 237fa9e4066Sahrens zvol_state_t *zv; 238fa9e4066Sahrens 239c99e4bdcSChris Kirby ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 240fa9e4066Sahrens 241c99e4bdcSChris Kirby for (minor = 1; minor <= ZFSDEV_MAX_MINOR; minor++) { 242c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 243fa9e4066Sahrens if (zv == NULL) 244fa9e4066Sahrens continue; 245fa9e4066Sahrens if (strcmp(zv->zv_name, name) == 0) 246f80ce222SChris Kirby return (zv); 247fa9e4066Sahrens } 248fa9e4066Sahrens 249f80ce222SChris Kirby return (NULL); 250fa9e4066Sahrens } 251fa9e4066Sahrens 252e7cbe64fSgw /* extent mapping arg */ 253e7cbe64fSgw struct maparg { 25488b7b0f2SMatthew Ahrens zvol_state_t *ma_zv; 25588b7b0f2SMatthew Ahrens uint64_t ma_blks; 256e7cbe64fSgw }; 257e7cbe64fSgw 258e7cbe64fSgw /*ARGSUSED*/ 259e7cbe64fSgw static int 2601b912ec7SGeorge Wilson zvol_map_block(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 2617802d7bfSMatthew Ahrens const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) 262e7cbe64fSgw { 26388b7b0f2SMatthew Ahrens struct maparg *ma = arg; 26488b7b0f2SMatthew Ahrens zvol_extent_t *ze; 26588b7b0f2SMatthew Ahrens int bs = ma->ma_zv->zv_volblocksize; 266e7cbe64fSgw 267a2cdcdd2SPaul Dagnelie if (bp == NULL || BP_IS_HOLE(bp) || 26843466aaeSMax Grossman zb->zb_object != ZVOL_OBJ || zb->zb_level != 0) 26988b7b0f2SMatthew Ahrens return (0); 270e7cbe64fSgw 2715d7b4d43SMatthew Ahrens VERIFY(!BP_IS_EMBEDDED(bp)); 2725d7b4d43SMatthew Ahrens 27388b7b0f2SMatthew Ahrens VERIFY3U(ma->ma_blks, ==, zb->zb_blkid); 27488b7b0f2SMatthew Ahrens ma->ma_blks++; 275e7cbe64fSgw 27688b7b0f2SMatthew Ahrens /* Abort immediately if we have encountered gang blocks */ 27788b7b0f2SMatthew Ahrens if (BP_IS_GANG(bp)) 278be6fd75aSMatthew Ahrens return (SET_ERROR(EFRAGS)); 279e7cbe64fSgw 28088b7b0f2SMatthew Ahrens /* 28188b7b0f2SMatthew Ahrens * See if the block is at the end of the previous extent. 28288b7b0f2SMatthew Ahrens */ 28388b7b0f2SMatthew Ahrens ze = list_tail(&ma->ma_zv->zv_extents); 28488b7b0f2SMatthew Ahrens if (ze && 28588b7b0f2SMatthew Ahrens DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) && 28688b7b0f2SMatthew Ahrens DVA_GET_OFFSET(BP_IDENTITY(bp)) == 28788b7b0f2SMatthew Ahrens DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) { 28888b7b0f2SMatthew Ahrens ze->ze_nblks++; 28988b7b0f2SMatthew Ahrens return (0); 290e7cbe64fSgw } 291e7cbe64fSgw 29288b7b0f2SMatthew Ahrens dprintf_bp(bp, "%s", "next blkptr:"); 293e7cbe64fSgw 29488b7b0f2SMatthew Ahrens /* start a new extent */ 29588b7b0f2SMatthew Ahrens ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP); 29688b7b0f2SMatthew Ahrens ze->ze_dva = bp->blk_dva[0]; /* structure assignment */ 29788b7b0f2SMatthew Ahrens ze->ze_nblks = 1; 29888b7b0f2SMatthew Ahrens list_insert_tail(&ma->ma_zv->zv_extents, ze); 29988b7b0f2SMatthew Ahrens return (0); 30088b7b0f2SMatthew Ahrens } 301e7cbe64fSgw 30288b7b0f2SMatthew Ahrens static void 30388b7b0f2SMatthew Ahrens zvol_free_extents(zvol_state_t *zv) 30488b7b0f2SMatthew Ahrens { 30588b7b0f2SMatthew Ahrens zvol_extent_t *ze; 306e7cbe64fSgw 30788b7b0f2SMatthew Ahrens while (ze = list_head(&zv->zv_extents)) { 30888b7b0f2SMatthew Ahrens list_remove(&zv->zv_extents, ze); 30988b7b0f2SMatthew Ahrens kmem_free(ze, sizeof (zvol_extent_t)); 310e7cbe64fSgw } 31188b7b0f2SMatthew Ahrens } 312e7cbe64fSgw 31388b7b0f2SMatthew Ahrens static int 31488b7b0f2SMatthew Ahrens zvol_get_lbas(zvol_state_t *zv) 31588b7b0f2SMatthew Ahrens { 3163adc9019SEric Taylor objset_t *os = zv->zv_objset; 31788b7b0f2SMatthew Ahrens struct maparg ma; 31888b7b0f2SMatthew Ahrens int err; 31988b7b0f2SMatthew Ahrens 32088b7b0f2SMatthew Ahrens ma.ma_zv = zv; 32188b7b0f2SMatthew Ahrens ma.ma_blks = 0; 32288b7b0f2SMatthew Ahrens zvol_free_extents(zv); 32388b7b0f2SMatthew Ahrens 3243adc9019SEric Taylor /* commit any in-flight changes before traversing the dataset */ 3253adc9019SEric Taylor txg_wait_synced(dmu_objset_pool(os), 0); 3263adc9019SEric Taylor err = traverse_dataset(dmu_objset_ds(os), 0, 32788b7b0f2SMatthew Ahrens TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma); 32888b7b0f2SMatthew Ahrens if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) { 32988b7b0f2SMatthew Ahrens zvol_free_extents(zv); 33088b7b0f2SMatthew Ahrens return (err ? err : EIO); 331e7cbe64fSgw } 33288b7b0f2SMatthew Ahrens 333e7cbe64fSgw return (0); 334e7cbe64fSgw } 335e7cbe64fSgw 336ecd6cf80Smarks /* ARGSUSED */ 337fa9e4066Sahrens void 338ecd6cf80Smarks zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 339fa9e4066Sahrens { 340da6c28aaSamw zfs_creat_t *zct = arg; 341da6c28aaSamw nvlist_t *nvprops = zct->zct_props; 342fa9e4066Sahrens int error; 343e9dbad6fSeschrock uint64_t volblocksize, volsize; 344fa9e4066Sahrens 345ecd6cf80Smarks VERIFY(nvlist_lookup_uint64(nvprops, 346e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0); 347ecd6cf80Smarks if (nvlist_lookup_uint64(nvprops, 348e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0) 349e9dbad6fSeschrock volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 350e9dbad6fSeschrock 351e9dbad6fSeschrock /* 352e7cbe64fSgw * These properties must be removed from the list so the generic 353e9dbad6fSeschrock * property setting step won't apply to them. 354e9dbad6fSeschrock */ 355ecd6cf80Smarks VERIFY(nvlist_remove_all(nvprops, 356e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0); 357ecd6cf80Smarks (void) nvlist_remove_all(nvprops, 358e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE)); 359e9dbad6fSeschrock 360e9dbad6fSeschrock error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize, 361fa9e4066Sahrens DMU_OT_NONE, 0, tx); 362fa9e4066Sahrens ASSERT(error == 0); 363fa9e4066Sahrens 364fa9e4066Sahrens error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP, 365fa9e4066Sahrens DMU_OT_NONE, 0, tx); 366fa9e4066Sahrens ASSERT(error == 0); 367fa9e4066Sahrens 368e9dbad6fSeschrock error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx); 369fa9e4066Sahrens ASSERT(error == 0); 370fa9e4066Sahrens } 371fa9e4066Sahrens 372b77b9231SDan McDonald /* 373b77b9231SDan McDonald * Replay a TX_TRUNCATE ZIL transaction if asked. TX_TRUNCATE is how we 374b77b9231SDan McDonald * implement DKIOCFREE/free-long-range. 375b77b9231SDan McDonald */ 376b77b9231SDan McDonald static int 3773f7978d0SAlan Somers zvol_replay_truncate(void *arg1, void *arg2, boolean_t byteswap) 378b77b9231SDan McDonald { 3793f7978d0SAlan Somers zvol_state_t *zv = arg1; 3803f7978d0SAlan Somers lr_truncate_t *lr = arg2; 381b77b9231SDan McDonald uint64_t offset, length; 382b77b9231SDan McDonald 383b77b9231SDan McDonald if (byteswap) 384b77b9231SDan McDonald byteswap_uint64_array(lr, sizeof (*lr)); 385b77b9231SDan McDonald 386b77b9231SDan McDonald offset = lr->lr_offset; 387b77b9231SDan McDonald length = lr->lr_length; 388b77b9231SDan McDonald 389b77b9231SDan McDonald return (dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, offset, length)); 390b77b9231SDan McDonald } 391b77b9231SDan McDonald 39222ac5be4Sperrin /* 39322ac5be4Sperrin * Replay a TX_WRITE ZIL transaction that didn't get committed 39422ac5be4Sperrin * after a system failure 39522ac5be4Sperrin */ 39622ac5be4Sperrin static int 3973f7978d0SAlan Somers zvol_replay_write(void *arg1, void *arg2, boolean_t byteswap) 39822ac5be4Sperrin { 3993f7978d0SAlan Somers zvol_state_t *zv = arg1; 4003f7978d0SAlan Somers lr_write_t *lr = arg2; 40122ac5be4Sperrin objset_t *os = zv->zv_objset; 40222ac5be4Sperrin char *data = (char *)(lr + 1); /* data follows lr_write_t */ 403b24ab676SJeff Bonwick uint64_t offset, length; 40422ac5be4Sperrin dmu_tx_t *tx; 40522ac5be4Sperrin int error; 40622ac5be4Sperrin 40722ac5be4Sperrin if (byteswap) 40822ac5be4Sperrin byteswap_uint64_array(lr, sizeof (*lr)); 40922ac5be4Sperrin 410b24ab676SJeff Bonwick offset = lr->lr_offset; 411b24ab676SJeff Bonwick length = lr->lr_length; 412b24ab676SJeff Bonwick 413b24ab676SJeff Bonwick /* If it's a dmu_sync() block, write the whole block */ 414b24ab676SJeff Bonwick if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) { 415b24ab676SJeff Bonwick uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr); 416b24ab676SJeff Bonwick if (length < blocksize) { 417b24ab676SJeff Bonwick offset -= offset % blocksize; 418b24ab676SJeff Bonwick length = blocksize; 419b24ab676SJeff Bonwick } 420b24ab676SJeff Bonwick } 421975c32a0SNeil Perrin 42222ac5be4Sperrin tx = dmu_tx_create(os); 423b24ab676SJeff Bonwick dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length); 4241209a471SNeil Perrin error = dmu_tx_assign(tx, TXG_WAIT); 42522ac5be4Sperrin if (error) { 42622ac5be4Sperrin dmu_tx_abort(tx); 42722ac5be4Sperrin } else { 428b24ab676SJeff Bonwick dmu_write(os, ZVOL_OBJ, offset, length, data, tx); 42922ac5be4Sperrin dmu_tx_commit(tx); 43022ac5be4Sperrin } 43122ac5be4Sperrin 43222ac5be4Sperrin return (error); 43322ac5be4Sperrin } 43422ac5be4Sperrin 43522ac5be4Sperrin /* ARGSUSED */ 43622ac5be4Sperrin static int 4373f7978d0SAlan Somers zvol_replay_err(void *arg1, void *arg2, boolean_t byteswap) 43822ac5be4Sperrin { 439be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 44022ac5be4Sperrin } 44122ac5be4Sperrin 44222ac5be4Sperrin /* 44322ac5be4Sperrin * Callback vectors for replaying records. 444b77b9231SDan McDonald * Only TX_WRITE and TX_TRUNCATE are needed for zvol. 44522ac5be4Sperrin */ 44622ac5be4Sperrin zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = { 44722ac5be4Sperrin zvol_replay_err, /* 0 no such transaction type */ 44822ac5be4Sperrin zvol_replay_err, /* TX_CREATE */ 44922ac5be4Sperrin zvol_replay_err, /* TX_MKDIR */ 45022ac5be4Sperrin zvol_replay_err, /* TX_MKXATTR */ 45122ac5be4Sperrin zvol_replay_err, /* TX_SYMLINK */ 45222ac5be4Sperrin zvol_replay_err, /* TX_REMOVE */ 45322ac5be4Sperrin zvol_replay_err, /* TX_RMDIR */ 45422ac5be4Sperrin zvol_replay_err, /* TX_LINK */ 45522ac5be4Sperrin zvol_replay_err, /* TX_RENAME */ 45622ac5be4Sperrin zvol_replay_write, /* TX_WRITE */ 457b77b9231SDan McDonald zvol_replay_truncate, /* TX_TRUNCATE */ 45822ac5be4Sperrin zvol_replay_err, /* TX_SETATTR */ 45922ac5be4Sperrin zvol_replay_err, /* TX_ACL */ 460975c32a0SNeil Perrin zvol_replay_err, /* TX_CREATE_ACL */ 461975c32a0SNeil Perrin zvol_replay_err, /* TX_CREATE_ATTR */ 462975c32a0SNeil Perrin zvol_replay_err, /* TX_CREATE_ACL_ATTR */ 463975c32a0SNeil Perrin zvol_replay_err, /* TX_MKDIR_ACL */ 464975c32a0SNeil Perrin zvol_replay_err, /* TX_MKDIR_ATTR */ 465975c32a0SNeil Perrin zvol_replay_err, /* TX_MKDIR_ACL_ATTR */ 466975c32a0SNeil Perrin zvol_replay_err, /* TX_WRITE2 */ 46722ac5be4Sperrin }; 46822ac5be4Sperrin 469681d9761SEric Taylor int 470681d9761SEric Taylor zvol_name2minor(const char *name, minor_t *minor) 471681d9761SEric Taylor { 472681d9761SEric Taylor zvol_state_t *zv; 473681d9761SEric Taylor 474c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 475681d9761SEric Taylor zv = zvol_minor_lookup(name); 476681d9761SEric Taylor if (minor && zv) 477681d9761SEric Taylor *minor = zv->zv_minor; 478c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 479681d9761SEric Taylor return (zv ? 0 : -1); 480681d9761SEric Taylor } 481681d9761SEric Taylor 482e7cbe64fSgw /* 483e7cbe64fSgw * Create a minor node (plus a whole lot more) for the specified volume. 484fa9e4066Sahrens */ 485fa9e4066Sahrens int 486681d9761SEric Taylor zvol_create_minor(const char *name) 487fa9e4066Sahrens { 488c99e4bdcSChris Kirby zfs_soft_state_t *zs; 489fa9e4066Sahrens zvol_state_t *zv; 490fa9e4066Sahrens objset_t *os; 49167bd71c6Sperrin dmu_object_info_t doi; 492fa9e4066Sahrens minor_t minor = 0; 493fa9e4066Sahrens char chrbuf[30], blkbuf[30]; 494fa9e4066Sahrens int error; 495fa9e4066Sahrens 496c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 497fa9e4066Sahrens 4981195e687SMark J Musante if (zvol_minor_lookup(name) != NULL) { 499c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 500be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 501fa9e4066Sahrens } 502fa9e4066Sahrens 503503ad85cSMatthew Ahrens /* lie and say we're read-only */ 5046e0cbcaaSMatthew Ahrens error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, FTAG, &os); 505fa9e4066Sahrens 506fa9e4066Sahrens if (error) { 507c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 508fa9e4066Sahrens return (error); 509fa9e4066Sahrens } 510fa9e4066Sahrens 511c99e4bdcSChris Kirby if ((minor = zfsdev_minor_alloc()) == 0) { 5126e0cbcaaSMatthew Ahrens dmu_objset_disown(os, FTAG); 513c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 514be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 515fa9e4066Sahrens } 516fa9e4066Sahrens 517c99e4bdcSChris Kirby if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS) { 5186e0cbcaaSMatthew Ahrens dmu_objset_disown(os, FTAG); 519c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 520be6fd75aSMatthew Ahrens return (SET_ERROR(EAGAIN)); 521fa9e4066Sahrens } 522e9dbad6fSeschrock (void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME, 523e9dbad6fSeschrock (char *)name); 524fa9e4066Sahrens 525681d9761SEric Taylor (void) snprintf(chrbuf, sizeof (chrbuf), "%u,raw", minor); 526fa9e4066Sahrens 527fa9e4066Sahrens if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR, 528fa9e4066Sahrens minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 529c99e4bdcSChris Kirby ddi_soft_state_free(zfsdev_state, minor); 5306e0cbcaaSMatthew Ahrens dmu_objset_disown(os, FTAG); 531c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 532be6fd75aSMatthew Ahrens return (SET_ERROR(EAGAIN)); 533fa9e4066Sahrens } 534fa9e4066Sahrens 535681d9761SEric Taylor (void) snprintf(blkbuf, sizeof (blkbuf), "%u", minor); 536fa9e4066Sahrens 537fa9e4066Sahrens if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK, 538fa9e4066Sahrens minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 539fa9e4066Sahrens ddi_remove_minor_node(zfs_dip, chrbuf); 540c99e4bdcSChris Kirby ddi_soft_state_free(zfsdev_state, minor); 5416e0cbcaaSMatthew Ahrens dmu_objset_disown(os, FTAG); 542c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 543be6fd75aSMatthew Ahrens return (SET_ERROR(EAGAIN)); 544fa9e4066Sahrens } 545fa9e4066Sahrens 546c99e4bdcSChris Kirby zs = ddi_get_soft_state(zfsdev_state, minor); 547c99e4bdcSChris Kirby zs->zss_type = ZSST_ZVOL; 548c99e4bdcSChris Kirby zv = zs->zss_data = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP); 549681d9761SEric Taylor (void) strlcpy(zv->zv_name, name, MAXPATHLEN); 550fa9e4066Sahrens zv->zv_min_bs = DEV_BSHIFT; 551fa9e4066Sahrens zv->zv_minor = minor; 552fa9e4066Sahrens zv->zv_objset = os; 553f9af39baSGeorge Wilson if (dmu_objset_is_snapshot(os) || !spa_writeable(dmu_objset_spa(os))) 554681d9761SEric Taylor zv->zv_flags |= ZVOL_RDONLY; 555c2e6a7d6Sperrin mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL); 556c2e6a7d6Sperrin avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare, 557c2e6a7d6Sperrin sizeof (rl_t), offsetof(rl_t, r_node)); 55888b7b0f2SMatthew Ahrens list_create(&zv->zv_extents, sizeof (zvol_extent_t), 55988b7b0f2SMatthew Ahrens offsetof(zvol_extent_t, ze_node)); 56067bd71c6Sperrin /* get and cache the blocksize */ 56167bd71c6Sperrin error = dmu_object_info(os, ZVOL_OBJ, &doi); 56267bd71c6Sperrin ASSERT(error == 0); 56367bd71c6Sperrin zv->zv_volblocksize = doi.doi_data_block_size; 56422ac5be4Sperrin 565f9af39baSGeorge Wilson if (spa_writeable(dmu_objset_spa(os))) { 566f9af39baSGeorge Wilson if (zil_replay_disable) 567f9af39baSGeorge Wilson zil_destroy(dmu_objset_zil(os), B_FALSE); 568f9af39baSGeorge Wilson else 569f9af39baSGeorge Wilson zil_replay(os, zv, zvol_replay_vector); 570f9af39baSGeorge Wilson } 5716e0cbcaaSMatthew Ahrens dmu_objset_disown(os, FTAG); 572681d9761SEric Taylor zv->zv_objset = NULL; 573fa9e4066Sahrens 574fa9e4066Sahrens zvol_minors++; 575fa9e4066Sahrens 576c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 577fa9e4066Sahrens 578fa9e4066Sahrens return (0); 579fa9e4066Sahrens } 580fa9e4066Sahrens 581fa9e4066Sahrens /* 582fa9e4066Sahrens * Remove minor node for the specified volume. 583fa9e4066Sahrens */ 584681d9761SEric Taylor static int 585681d9761SEric Taylor zvol_remove_zv(zvol_state_t *zv) 586681d9761SEric Taylor { 587681d9761SEric Taylor char nmbuf[20]; 588c99e4bdcSChris Kirby minor_t minor = zv->zv_minor; 589681d9761SEric Taylor 590c99e4bdcSChris Kirby ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 591681d9761SEric Taylor if (zv->zv_total_opens != 0) 592be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 593681d9761SEric Taylor 594c99e4bdcSChris Kirby (void) snprintf(nmbuf, sizeof (nmbuf), "%u,raw", minor); 595681d9761SEric Taylor ddi_remove_minor_node(zfs_dip, nmbuf); 596681d9761SEric Taylor 597c99e4bdcSChris Kirby (void) snprintf(nmbuf, sizeof (nmbuf), "%u", minor); 598681d9761SEric Taylor ddi_remove_minor_node(zfs_dip, nmbuf); 599681d9761SEric Taylor 600681d9761SEric Taylor avl_destroy(&zv->zv_znode.z_range_avl); 601681d9761SEric Taylor mutex_destroy(&zv->zv_znode.z_range_lock); 602681d9761SEric Taylor 603c99e4bdcSChris Kirby kmem_free(zv, sizeof (zvol_state_t)); 604c99e4bdcSChris Kirby 605c99e4bdcSChris Kirby ddi_soft_state_free(zfsdev_state, minor); 606681d9761SEric Taylor 607681d9761SEric Taylor zvol_minors--; 608681d9761SEric Taylor return (0); 609681d9761SEric Taylor } 610681d9761SEric Taylor 611fa9e4066Sahrens int 612e9dbad6fSeschrock zvol_remove_minor(const char *name) 613fa9e4066Sahrens { 614fa9e4066Sahrens zvol_state_t *zv; 615681d9761SEric Taylor int rc; 616fa9e4066Sahrens 617c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 618e9dbad6fSeschrock if ((zv = zvol_minor_lookup(name)) == NULL) { 619c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 620be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 621fa9e4066Sahrens } 622681d9761SEric Taylor rc = zvol_remove_zv(zv); 623c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 624681d9761SEric Taylor return (rc); 625681d9761SEric Taylor } 626fa9e4066Sahrens 627681d9761SEric Taylor int 628681d9761SEric Taylor zvol_first_open(zvol_state_t *zv) 629681d9761SEric Taylor { 630681d9761SEric Taylor objset_t *os; 631681d9761SEric Taylor uint64_t volsize; 632681d9761SEric Taylor int error; 633681d9761SEric Taylor uint64_t readonly; 634fa9e4066Sahrens 635681d9761SEric Taylor /* lie and say we're read-only */ 636681d9761SEric Taylor error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, B_TRUE, 637681d9761SEric Taylor zvol_tag, &os); 638681d9761SEric Taylor if (error) 639681d9761SEric Taylor return (error); 640fa9e4066Sahrens 641c61ea566SGeorge Wilson zv->zv_objset = os; 642681d9761SEric Taylor error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize); 643681d9761SEric Taylor if (error) { 644681d9761SEric Taylor ASSERT(error == 0); 645681d9761SEric Taylor dmu_objset_disown(os, zvol_tag); 646681d9761SEric Taylor return (error); 647681d9761SEric Taylor } 648c61ea566SGeorge Wilson 649*8dfe5547SRichard Yao error = dnode_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dn); 65094d1a210STim Haley if (error) { 65194d1a210STim Haley dmu_objset_disown(os, zvol_tag); 65294d1a210STim Haley return (error); 65394d1a210STim Haley } 654c61ea566SGeorge Wilson 655c61ea566SGeorge Wilson zvol_size_changed(zv, volsize); 656681d9761SEric Taylor zv->zv_zilog = zil_open(os, zvol_get_data); 657fa9e4066Sahrens 658681d9761SEric Taylor VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &readonly, 659681d9761SEric Taylor NULL) == 0); 660f9af39baSGeorge Wilson if (readonly || dmu_objset_is_snapshot(os) || 661f9af39baSGeorge Wilson !spa_writeable(dmu_objset_spa(os))) 662681d9761SEric Taylor zv->zv_flags |= ZVOL_RDONLY; 663681d9761SEric Taylor else 664681d9761SEric Taylor zv->zv_flags &= ~ZVOL_RDONLY; 665681d9761SEric Taylor return (error); 666681d9761SEric Taylor } 667fa9e4066Sahrens 668681d9761SEric Taylor void 669681d9761SEric Taylor zvol_last_close(zvol_state_t *zv) 670681d9761SEric Taylor { 67122ac5be4Sperrin zil_close(zv->zv_zilog); 67222ac5be4Sperrin zv->zv_zilog = NULL; 6732e2c1355SMatthew Ahrens 674*8dfe5547SRichard Yao dnode_rele(zv->zv_dn, zvol_tag); 675*8dfe5547SRichard Yao zv->zv_dn = NULL; 6762e2c1355SMatthew Ahrens 6772e2c1355SMatthew Ahrens /* 6782e2c1355SMatthew Ahrens * Evict cached data 6792e2c1355SMatthew Ahrens */ 6802e2c1355SMatthew Ahrens if (dsl_dataset_is_dirty(dmu_objset_ds(zv->zv_objset)) && 6812e2c1355SMatthew Ahrens !(zv->zv_flags & ZVOL_RDONLY)) 6822e2c1355SMatthew Ahrens txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 6833b2aab18SMatthew Ahrens dmu_objset_evict_dbufs(zv->zv_objset); 6842e2c1355SMatthew Ahrens 685503ad85cSMatthew Ahrens dmu_objset_disown(zv->zv_objset, zvol_tag); 686fa9e4066Sahrens zv->zv_objset = NULL; 687fa9e4066Sahrens } 688fa9e4066Sahrens 689e7cbe64fSgw int 690e7cbe64fSgw zvol_prealloc(zvol_state_t *zv) 691e7cbe64fSgw { 692e7cbe64fSgw objset_t *os = zv->zv_objset; 693e7cbe64fSgw dmu_tx_t *tx; 694e7cbe64fSgw uint64_t refd, avail, usedobjs, availobjs; 695e7cbe64fSgw uint64_t resid = zv->zv_volsize; 696e7cbe64fSgw uint64_t off = 0; 697e7cbe64fSgw 698e7cbe64fSgw /* Check the space usage before attempting to allocate the space */ 699e7cbe64fSgw dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs); 700e7cbe64fSgw if (avail < zv->zv_volsize) 701be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 702e7cbe64fSgw 703e7cbe64fSgw /* Free old extents if they exist */ 704e7cbe64fSgw zvol_free_extents(zv); 705e7cbe64fSgw 706e7cbe64fSgw while (resid != 0) { 707e7cbe64fSgw int error; 708b5152584SMatthew Ahrens uint64_t bytes = MIN(resid, SPA_OLD_MAXBLOCKSIZE); 709e7cbe64fSgw 710e7cbe64fSgw tx = dmu_tx_create(os); 711e7cbe64fSgw dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 712e7cbe64fSgw error = dmu_tx_assign(tx, TXG_WAIT); 713e7cbe64fSgw if (error) { 714e7cbe64fSgw dmu_tx_abort(tx); 715cdb0ab79Smaybee (void) dmu_free_long_range(os, ZVOL_OBJ, 0, off); 716e7cbe64fSgw return (error); 717e7cbe64fSgw } 71882c9918fSTim Haley dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx); 719e7cbe64fSgw dmu_tx_commit(tx); 720e7cbe64fSgw off += bytes; 721e7cbe64fSgw resid -= bytes; 722e7cbe64fSgw } 723e7cbe64fSgw txg_wait_synced(dmu_objset_pool(os), 0); 724e7cbe64fSgw 725e7cbe64fSgw return (0); 726e7cbe64fSgw } 727e7cbe64fSgw 7283b2aab18SMatthew Ahrens static int 729681d9761SEric Taylor zvol_update_volsize(objset_t *os, uint64_t volsize) 730e7cbe64fSgw { 731e7cbe64fSgw dmu_tx_t *tx; 732e7cbe64fSgw int error; 733e7cbe64fSgw 734c99e4bdcSChris Kirby ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 735e7cbe64fSgw 736681d9761SEric Taylor tx = dmu_tx_create(os); 737e7cbe64fSgw dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 7384bb73804SMatthew Ahrens dmu_tx_mark_netfree(tx); 739e7cbe64fSgw error = dmu_tx_assign(tx, TXG_WAIT); 740e7cbe64fSgw if (error) { 741e7cbe64fSgw dmu_tx_abort(tx); 742e7cbe64fSgw return (error); 743e7cbe64fSgw } 744e7cbe64fSgw 745681d9761SEric Taylor error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, 746e7cbe64fSgw &volsize, tx); 747e7cbe64fSgw dmu_tx_commit(tx); 748e7cbe64fSgw 749e7cbe64fSgw if (error == 0) 750681d9761SEric Taylor error = dmu_free_long_range(os, 751cdb0ab79Smaybee ZVOL_OBJ, volsize, DMU_OBJECT_END); 752681d9761SEric Taylor return (error); 753681d9761SEric Taylor } 754e7cbe64fSgw 755681d9761SEric Taylor void 756681d9761SEric Taylor zvol_remove_minors(const char *name) 757681d9761SEric Taylor { 758681d9761SEric Taylor zvol_state_t *zv; 759681d9761SEric Taylor char *namebuf; 760681d9761SEric Taylor minor_t minor; 761681d9761SEric Taylor 762681d9761SEric Taylor namebuf = kmem_zalloc(strlen(name) + 2, KM_SLEEP); 763681d9761SEric Taylor (void) strncpy(namebuf, name, strlen(name)); 764681d9761SEric Taylor (void) strcat(namebuf, "/"); 765c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 766c99e4bdcSChris Kirby for (minor = 1; minor <= ZFSDEV_MAX_MINOR; minor++) { 767681d9761SEric Taylor 768c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 769681d9761SEric Taylor if (zv == NULL) 770681d9761SEric Taylor continue; 771681d9761SEric Taylor if (strncmp(namebuf, zv->zv_name, strlen(namebuf)) == 0) 772681d9761SEric Taylor (void) zvol_remove_zv(zv); 773e7cbe64fSgw } 774681d9761SEric Taylor kmem_free(namebuf, strlen(name) + 2); 775681d9761SEric Taylor 776c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 777e7cbe64fSgw } 778e7cbe64fSgw 779c61ea566SGeorge Wilson static int 7803b2aab18SMatthew Ahrens zvol_update_live_volsize(zvol_state_t *zv, uint64_t volsize) 781fa9e4066Sahrens { 782e7cbe64fSgw uint64_t old_volsize = 0ULL; 7833b2aab18SMatthew Ahrens int error = 0; 784fa9e4066Sahrens 785c61ea566SGeorge Wilson ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 786c61ea566SGeorge Wilson 787e7cbe64fSgw /* 788e7cbe64fSgw * Reinitialize the dump area to the new size. If we 789681d9761SEric Taylor * failed to resize the dump area then restore it back to 790c61ea566SGeorge Wilson * its original size. We must set the new volsize prior 791c61ea566SGeorge Wilson * to calling dumpvp_resize() to ensure that the devices' 792c61ea566SGeorge Wilson * size(9P) is not visible by the dump subsystem. 793e7cbe64fSgw */ 7943b2aab18SMatthew Ahrens old_volsize = zv->zv_volsize; 7953b2aab18SMatthew Ahrens zvol_size_changed(zv, volsize); 7963b2aab18SMatthew Ahrens 7973b2aab18SMatthew Ahrens if (zv->zv_flags & ZVOL_DUMPIFIED) { 7983b2aab18SMatthew Ahrens if ((error = zvol_dumpify(zv)) != 0 || 7993b2aab18SMatthew Ahrens (error = dumpvp_resize()) != 0) { 8003b2aab18SMatthew Ahrens int dumpify_error; 8013b2aab18SMatthew Ahrens 8023b2aab18SMatthew Ahrens (void) zvol_update_volsize(zv->zv_objset, old_volsize); 8033b2aab18SMatthew Ahrens zvol_size_changed(zv, old_volsize); 8043b2aab18SMatthew Ahrens dumpify_error = zvol_dumpify(zv); 8053b2aab18SMatthew Ahrens error = dumpify_error ? dumpify_error : error; 806681d9761SEric Taylor } 807fa9e4066Sahrens } 808fa9e4066Sahrens 809573ca77eSGeorge Wilson /* 810573ca77eSGeorge Wilson * Generate a LUN expansion event. 811573ca77eSGeorge Wilson */ 8123b2aab18SMatthew Ahrens if (error == 0) { 813573ca77eSGeorge Wilson sysevent_id_t eid; 814573ca77eSGeorge Wilson nvlist_t *attr; 815573ca77eSGeorge Wilson char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 816573ca77eSGeorge Wilson 817681d9761SEric Taylor (void) snprintf(physpath, MAXPATHLEN, "%s%u", ZVOL_PSEUDO_DEV, 818573ca77eSGeorge Wilson zv->zv_minor); 819573ca77eSGeorge Wilson 820573ca77eSGeorge Wilson VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 821573ca77eSGeorge Wilson VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 822573ca77eSGeorge Wilson 823573ca77eSGeorge Wilson (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 824573ca77eSGeorge Wilson ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 825573ca77eSGeorge Wilson 826573ca77eSGeorge Wilson nvlist_free(attr); 827573ca77eSGeorge Wilson kmem_free(physpath, MAXPATHLEN); 828573ca77eSGeorge Wilson } 829c61ea566SGeorge Wilson return (error); 830c61ea566SGeorge Wilson } 831573ca77eSGeorge Wilson 832c61ea566SGeorge Wilson int 833c61ea566SGeorge Wilson zvol_set_volsize(const char *name, uint64_t volsize) 834c61ea566SGeorge Wilson { 835c61ea566SGeorge Wilson zvol_state_t *zv = NULL; 836c61ea566SGeorge Wilson objset_t *os; 837c61ea566SGeorge Wilson int error; 838c61ea566SGeorge Wilson dmu_object_info_t doi; 839c61ea566SGeorge Wilson uint64_t readonly; 8403b2aab18SMatthew Ahrens boolean_t owned = B_FALSE; 8413b2aab18SMatthew Ahrens 8423b2aab18SMatthew Ahrens error = dsl_prop_get_integer(name, 8433b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_READONLY), &readonly, NULL); 8443b2aab18SMatthew Ahrens if (error != 0) 8453b2aab18SMatthew Ahrens return (error); 8463b2aab18SMatthew Ahrens if (readonly) 847be6fd75aSMatthew Ahrens return (SET_ERROR(EROFS)); 848c61ea566SGeorge Wilson 849c61ea566SGeorge Wilson mutex_enter(&zfsdev_state_lock); 850c61ea566SGeorge Wilson zv = zvol_minor_lookup(name); 8513b2aab18SMatthew Ahrens 8523b2aab18SMatthew Ahrens if (zv == NULL || zv->zv_objset == NULL) { 8533b2aab18SMatthew Ahrens if ((error = dmu_objset_own(name, DMU_OST_ZVOL, B_FALSE, 8543b2aab18SMatthew Ahrens FTAG, &os)) != 0) { 8553b2aab18SMatthew Ahrens mutex_exit(&zfsdev_state_lock); 8563b2aab18SMatthew Ahrens return (error); 8573b2aab18SMatthew Ahrens } 8583b2aab18SMatthew Ahrens owned = B_TRUE; 8593b2aab18SMatthew Ahrens if (zv != NULL) 8603b2aab18SMatthew Ahrens zv->zv_objset = os; 8613b2aab18SMatthew Ahrens } else { 8623b2aab18SMatthew Ahrens os = zv->zv_objset; 863c61ea566SGeorge Wilson } 864c61ea566SGeorge Wilson 865c61ea566SGeorge Wilson if ((error = dmu_object_info(os, ZVOL_OBJ, &doi)) != 0 || 8663b2aab18SMatthew Ahrens (error = zvol_check_volsize(volsize, doi.doi_data_block_size)) != 0) 867c61ea566SGeorge Wilson goto out; 868c61ea566SGeorge Wilson 8693b2aab18SMatthew Ahrens error = zvol_update_volsize(os, volsize); 870c61ea566SGeorge Wilson 8713b2aab18SMatthew Ahrens if (error == 0 && zv != NULL) 8723b2aab18SMatthew Ahrens error = zvol_update_live_volsize(zv, volsize); 873bb0ade09Sahrens out: 8743b2aab18SMatthew Ahrens if (owned) { 8753b2aab18SMatthew Ahrens dmu_objset_disown(os, FTAG); 8763b2aab18SMatthew Ahrens if (zv != NULL) 8773b2aab18SMatthew Ahrens zv->zv_objset = NULL; 8783b2aab18SMatthew Ahrens } 879c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 880fa9e4066Sahrens return (error); 881fa9e4066Sahrens } 882fa9e4066Sahrens 883fa9e4066Sahrens /*ARGSUSED*/ 884fa9e4066Sahrens int 885fa9e4066Sahrens zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr) 886fa9e4066Sahrens { 887fa9e4066Sahrens zvol_state_t *zv; 888681d9761SEric Taylor int err = 0; 889fa9e4066Sahrens 890c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 891fa9e4066Sahrens 892c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(getminor(*devp), ZSST_ZVOL); 893fa9e4066Sahrens if (zv == NULL) { 894c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 895be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 896fa9e4066Sahrens } 897fa9e4066Sahrens 898681d9761SEric Taylor if (zv->zv_total_opens == 0) 899681d9761SEric Taylor err = zvol_first_open(zv); 900681d9761SEric Taylor if (err) { 901c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 902681d9761SEric Taylor return (err); 903681d9761SEric Taylor } 904681d9761SEric Taylor if ((flag & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) { 905be6fd75aSMatthew Ahrens err = SET_ERROR(EROFS); 906681d9761SEric Taylor goto out; 907fa9e4066Sahrens } 908c7f714e2SEric Taylor if (zv->zv_flags & ZVOL_EXCL) { 909be6fd75aSMatthew Ahrens err = SET_ERROR(EBUSY); 910681d9761SEric Taylor goto out; 911c7f714e2SEric Taylor } 912c7f714e2SEric Taylor if (flag & FEXCL) { 913c7f714e2SEric Taylor if (zv->zv_total_opens != 0) { 914be6fd75aSMatthew Ahrens err = SET_ERROR(EBUSY); 915681d9761SEric Taylor goto out; 916c7f714e2SEric Taylor } 917c7f714e2SEric Taylor zv->zv_flags |= ZVOL_EXCL; 918c7f714e2SEric Taylor } 919fa9e4066Sahrens 920fa9e4066Sahrens if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) { 921fa9e4066Sahrens zv->zv_open_count[otyp]++; 922fa9e4066Sahrens zv->zv_total_opens++; 923fa9e4066Sahrens } 924c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 925fa9e4066Sahrens 926681d9761SEric Taylor return (err); 927681d9761SEric Taylor out: 928681d9761SEric Taylor if (zv->zv_total_opens == 0) 929681d9761SEric Taylor zvol_last_close(zv); 930c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 931681d9761SEric Taylor return (err); 932fa9e4066Sahrens } 933fa9e4066Sahrens 934fa9e4066Sahrens /*ARGSUSED*/ 935fa9e4066Sahrens int 936fa9e4066Sahrens zvol_close(dev_t dev, int flag, int otyp, cred_t *cr) 937fa9e4066Sahrens { 938fa9e4066Sahrens minor_t minor = getminor(dev); 939fa9e4066Sahrens zvol_state_t *zv; 940681d9761SEric Taylor int error = 0; 941fa9e4066Sahrens 942c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 943fa9e4066Sahrens 944c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 945fa9e4066Sahrens if (zv == NULL) { 946c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 947be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 948fa9e4066Sahrens } 949fa9e4066Sahrens 950c7f714e2SEric Taylor if (zv->zv_flags & ZVOL_EXCL) { 951c7f714e2SEric Taylor ASSERT(zv->zv_total_opens == 1); 952c7f714e2SEric Taylor zv->zv_flags &= ~ZVOL_EXCL; 953fa9e4066Sahrens } 954fa9e4066Sahrens 955fa9e4066Sahrens /* 956fa9e4066Sahrens * If the open count is zero, this is a spurious close. 957fa9e4066Sahrens * That indicates a bug in the kernel / DDI framework. 958fa9e4066Sahrens */ 959fa9e4066Sahrens ASSERT(zv->zv_open_count[otyp] != 0); 960fa9e4066Sahrens ASSERT(zv->zv_total_opens != 0); 961fa9e4066Sahrens 962fa9e4066Sahrens /* 963fa9e4066Sahrens * You may get multiple opens, but only one close. 964fa9e4066Sahrens */ 965fa9e4066Sahrens zv->zv_open_count[otyp]--; 966fa9e4066Sahrens zv->zv_total_opens--; 967fa9e4066Sahrens 968681d9761SEric Taylor if (zv->zv_total_opens == 0) 969681d9761SEric Taylor zvol_last_close(zv); 970fa9e4066Sahrens 971c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 972681d9761SEric Taylor return (error); 973fa9e4066Sahrens } 974fa9e4066Sahrens 975feb08c6bSbillm static void 976b24ab676SJeff Bonwick zvol_get_done(zgd_t *zgd, int error) 97767bd71c6Sperrin { 978b24ab676SJeff Bonwick if (zgd->zgd_db) 979b24ab676SJeff Bonwick dmu_buf_rele(zgd->zgd_db, zgd); 980b24ab676SJeff Bonwick 981b24ab676SJeff Bonwick zfs_range_unlock(zgd->zgd_rl); 982b24ab676SJeff Bonwick 983b24ab676SJeff Bonwick if (error == 0 && zgd->zgd_bp) 9841271e4b1SPrakash Surya zil_lwb_add_block(zgd->zgd_lwb, zgd->zgd_bp); 98567bd71c6Sperrin 98667bd71c6Sperrin kmem_free(zgd, sizeof (zgd_t)); 98767bd71c6Sperrin } 98867bd71c6Sperrin 98967bd71c6Sperrin /* 99067bd71c6Sperrin * Get data to generate a TX_WRITE intent log record. 99167bd71c6Sperrin */ 992feb08c6bSbillm static int 9931271e4b1SPrakash Surya zvol_get_data(void *arg, lr_write_t *lr, char *buf, struct lwb *lwb, zio_t *zio) 99467bd71c6Sperrin { 99567bd71c6Sperrin zvol_state_t *zv = arg; 996b24ab676SJeff Bonwick uint64_t offset = lr->lr_offset; 997b24ab676SJeff Bonwick uint64_t size = lr->lr_length; /* length of user data */ 99867bd71c6Sperrin dmu_buf_t *db; 99967bd71c6Sperrin zgd_t *zgd; 100067bd71c6Sperrin int error; 100167bd71c6Sperrin 10021271e4b1SPrakash Surya ASSERT3P(lwb, !=, NULL); 10031271e4b1SPrakash Surya ASSERT3P(zio, !=, NULL); 10041271e4b1SPrakash Surya ASSERT3U(size, !=, 0); 1005b24ab676SJeff Bonwick 1006b24ab676SJeff Bonwick zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP); 10071271e4b1SPrakash Surya zgd->zgd_lwb = lwb; 1008feb08c6bSbillm 1009c2e6a7d6Sperrin /* 1010c2e6a7d6Sperrin * Write records come in two flavors: immediate and indirect. 1011c2e6a7d6Sperrin * For small writes it's cheaper to store the data with the 1012c2e6a7d6Sperrin * log record (immediate); for large writes it's cheaper to 1013c2e6a7d6Sperrin * sync the data and get a pointer to it (indirect) so that 1014c2e6a7d6Sperrin * we don't have to write the data twice. 1015c2e6a7d6Sperrin */ 101642b14111SLOLi if (buf != NULL) { /* immediate write */ 101742b14111SLOLi zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, 101842b14111SLOLi RL_READER); 1019*8dfe5547SRichard Yao error = dmu_read_by_dnode(zv->zv_dn, offset, size, buf, 1020b24ab676SJeff Bonwick DMU_READ_NO_PREFETCH); 102142b14111SLOLi } else { /* indirect write */ 102242b14111SLOLi /* 102342b14111SLOLi * Have to lock the whole block to ensure when it's written out 102442b14111SLOLi * and its checksum is being calculated that no one can change 102542b14111SLOLi * the data. Contrarily to zfs_get_data we need not re-check 102642b14111SLOLi * blocksize after we get the lock because it cannot be changed. 102742b14111SLOLi */ 1028b24ab676SJeff Bonwick size = zv->zv_volblocksize; 1029b24ab676SJeff Bonwick offset = P2ALIGN(offset, size); 103042b14111SLOLi zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, 103142b14111SLOLi RL_READER); 1032*8dfe5547SRichard Yao error = dmu_buf_hold_by_dnode(zv->zv_dn, offset, zgd, &db, 103347cb52daSJeff Bonwick DMU_READ_NO_PREFETCH); 1034b24ab676SJeff Bonwick if (error == 0) { 1035b7edcb94SMatthew Ahrens blkptr_t *bp = &lr->lr_blkptr; 103680901aeaSGeorge Wilson 1037b24ab676SJeff Bonwick zgd->zgd_db = db; 1038b24ab676SJeff Bonwick zgd->zgd_bp = bp; 103967bd71c6Sperrin 1040b24ab676SJeff Bonwick ASSERT(db->db_offset == offset); 1041b24ab676SJeff Bonwick ASSERT(db->db_size == size); 104267bd71c6Sperrin 1043b24ab676SJeff Bonwick error = dmu_sync(zio, lr->lr_common.lrc_txg, 1044b24ab676SJeff Bonwick zvol_get_done, zgd); 1045975c32a0SNeil Perrin 1046b24ab676SJeff Bonwick if (error == 0) 1047b24ab676SJeff Bonwick return (0); 1048b24ab676SJeff Bonwick } 1049975c32a0SNeil Perrin } 1050975c32a0SNeil Perrin 1051b24ab676SJeff Bonwick zvol_get_done(zgd, error); 1052b24ab676SJeff Bonwick 105367bd71c6Sperrin return (error); 105467bd71c6Sperrin } 105567bd71c6Sperrin 1056a24e15ceSperrin /* 1057a24e15ceSperrin * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions. 105822ac5be4Sperrin * 105922ac5be4Sperrin * We store data in the log buffers if it's small enough. 106067bd71c6Sperrin * Otherwise we will later flush the data out via dmu_sync(). 106122ac5be4Sperrin */ 106267bd71c6Sperrin ssize_t zvol_immediate_write_sz = 32768; 106322ac5be4Sperrin 1064feb08c6bSbillm static void 1065510b6c0eSNeil Perrin zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid, 1066510b6c0eSNeil Perrin boolean_t sync) 106722ac5be4Sperrin { 1068feb08c6bSbillm uint32_t blocksize = zv->zv_volblocksize; 10691209a471SNeil Perrin zilog_t *zilog = zv->zv_zilog; 1070c5ee4681SAlexander Motin itx_wr_state_t write_state; 1071510b6c0eSNeil Perrin 1072b24ab676SJeff Bonwick if (zil_replaying(zilog, tx)) 10731209a471SNeil Perrin return; 10741209a471SNeil Perrin 1075c5ee4681SAlexander Motin if (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT) 1076c5ee4681SAlexander Motin write_state = WR_INDIRECT; 1077c5ee4681SAlexander Motin else if (!spa_has_slogs(zilog->zl_spa) && 1078c5ee4681SAlexander Motin resid >= blocksize && blocksize > zvol_immediate_write_sz) 1079c5ee4681SAlexander Motin write_state = WR_INDIRECT; 1080c5ee4681SAlexander Motin else if (sync) 1081c5ee4681SAlexander Motin write_state = WR_COPIED; 1082c5ee4681SAlexander Motin else 1083c5ee4681SAlexander Motin write_state = WR_NEED_COPY; 1084feb08c6bSbillm 1085510b6c0eSNeil Perrin while (resid) { 1086510b6c0eSNeil Perrin itx_t *itx; 1087510b6c0eSNeil Perrin lr_write_t *lr; 1088c5ee4681SAlexander Motin itx_wr_state_t wr_state = write_state; 1089c5ee4681SAlexander Motin ssize_t len = resid; 1090c5ee4681SAlexander Motin 1091c5ee4681SAlexander Motin if (wr_state == WR_COPIED && resid > ZIL_MAX_COPIED_DATA) 1092c5ee4681SAlexander Motin wr_state = WR_NEED_COPY; 1093c5ee4681SAlexander Motin else if (wr_state == WR_INDIRECT) 1094c5ee4681SAlexander Motin len = MIN(blocksize - P2PHASE(off, blocksize), resid); 1095510b6c0eSNeil Perrin 1096510b6c0eSNeil Perrin itx = zil_itx_create(TX_WRITE, sizeof (*lr) + 1097c5ee4681SAlexander Motin (wr_state == WR_COPIED ? len : 0)); 1098feb08c6bSbillm lr = (lr_write_t *)&itx->itx_lr; 1099*8dfe5547SRichard Yao if (wr_state == WR_COPIED && dmu_read_by_dnode(zv->zv_dn, 1100*8dfe5547SRichard Yao off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) { 1101b24ab676SJeff Bonwick zil_itx_destroy(itx); 1102510b6c0eSNeil Perrin itx = zil_itx_create(TX_WRITE, sizeof (*lr)); 1103510b6c0eSNeil Perrin lr = (lr_write_t *)&itx->itx_lr; 1104c5ee4681SAlexander Motin wr_state = WR_NEED_COPY; 1105510b6c0eSNeil Perrin } 1106510b6c0eSNeil Perrin 1107c5ee4681SAlexander Motin itx->itx_wr_state = wr_state; 1108feb08c6bSbillm lr->lr_foid = ZVOL_OBJ; 1109feb08c6bSbillm lr->lr_offset = off; 1110510b6c0eSNeil Perrin lr->lr_length = len; 1111b24ab676SJeff Bonwick lr->lr_blkoff = 0; 1112feb08c6bSbillm BP_ZERO(&lr->lr_blkptr); 1113feb08c6bSbillm 1114510b6c0eSNeil Perrin itx->itx_private = zv; 1115510b6c0eSNeil Perrin itx->itx_sync = sync; 1116510b6c0eSNeil Perrin 11175002558fSNeil Perrin zil_itx_assign(zilog, itx, tx); 1118510b6c0eSNeil Perrin 1119510b6c0eSNeil Perrin off += len; 1120510b6c0eSNeil Perrin resid -= len; 112122ac5be4Sperrin } 112222ac5be4Sperrin } 112322ac5be4Sperrin 112488b7b0f2SMatthew Ahrens static int 1125810e43b2SBill Pijewski zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t origoffset, 1126810e43b2SBill Pijewski uint64_t size, boolean_t doread, boolean_t isdump) 1127e7cbe64fSgw { 1128e7cbe64fSgw vdev_disk_t *dvd; 1129e7cbe64fSgw int c; 1130e7cbe64fSgw int numerrors = 0; 1131e7cbe64fSgw 1132810e43b2SBill Pijewski if (vd->vdev_ops == &vdev_mirror_ops || 1133810e43b2SBill Pijewski vd->vdev_ops == &vdev_replacing_ops || 1134810e43b2SBill Pijewski vd->vdev_ops == &vdev_spare_ops) { 1135810e43b2SBill Pijewski for (c = 0; c < vd->vdev_children; c++) { 1136810e43b2SBill Pijewski int err = zvol_dumpio_vdev(vd->vdev_child[c], 1137810e43b2SBill Pijewski addr, offset, origoffset, size, doread, isdump); 1138810e43b2SBill Pijewski if (err != 0) { 1139810e43b2SBill Pijewski numerrors++; 1140810e43b2SBill Pijewski } else if (doread) { 1141810e43b2SBill Pijewski break; 1142810e43b2SBill Pijewski } 1143e7cbe64fSgw } 1144e7cbe64fSgw } 1145e7cbe64fSgw 1146810e43b2SBill Pijewski if (!vd->vdev_ops->vdev_op_leaf && vd->vdev_ops != &vdev_raidz_ops) 1147e7cbe64fSgw return (numerrors < vd->vdev_children ? 0 : EIO); 1148e7cbe64fSgw 1149dc0bb255SEric Taylor if (doread && !vdev_readable(vd)) 1150be6fd75aSMatthew Ahrens return (SET_ERROR(EIO)); 1151dc0bb255SEric Taylor else if (!doread && !vdev_writeable(vd)) 1152be6fd75aSMatthew Ahrens return (SET_ERROR(EIO)); 1153e7cbe64fSgw 1154810e43b2SBill Pijewski if (vd->vdev_ops == &vdev_raidz_ops) { 1155810e43b2SBill Pijewski return (vdev_raidz_physio(vd, 1156810e43b2SBill Pijewski addr, size, offset, origoffset, doread, isdump)); 1157810e43b2SBill Pijewski } 1158810e43b2SBill Pijewski 1159e7cbe64fSgw offset += VDEV_LABEL_START_SIZE; 1160e7cbe64fSgw 1161e7cbe64fSgw if (ddi_in_panic() || isdump) { 116288b7b0f2SMatthew Ahrens ASSERT(!doread); 116388b7b0f2SMatthew Ahrens if (doread) 1164be6fd75aSMatthew Ahrens return (SET_ERROR(EIO)); 1165810e43b2SBill Pijewski dvd = vd->vdev_tsd; 1166810e43b2SBill Pijewski ASSERT3P(dvd, !=, NULL); 1167e7cbe64fSgw return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset), 1168e7cbe64fSgw lbtodb(size))); 1169e7cbe64fSgw } else { 1170810e43b2SBill Pijewski dvd = vd->vdev_tsd; 1171810e43b2SBill Pijewski ASSERT3P(dvd, !=, NULL); 1172810e43b2SBill Pijewski return (vdev_disk_ldi_physio(dvd->vd_lh, addr, size, 1173810e43b2SBill Pijewski offset, doread ? B_READ : B_WRITE)); 1174e7cbe64fSgw } 1175e7cbe64fSgw } 1176e7cbe64fSgw 117788b7b0f2SMatthew Ahrens static int 117888b7b0f2SMatthew Ahrens zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size, 117988b7b0f2SMatthew Ahrens boolean_t doread, boolean_t isdump) 1180e7cbe64fSgw { 1181e7cbe64fSgw vdev_t *vd; 1182e7cbe64fSgw int error; 118388b7b0f2SMatthew Ahrens zvol_extent_t *ze; 1184e7cbe64fSgw spa_t *spa = dmu_objset_spa(zv->zv_objset); 1185e7cbe64fSgw 118688b7b0f2SMatthew Ahrens /* Must be sector aligned, and not stradle a block boundary. */ 118788b7b0f2SMatthew Ahrens if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) || 118888b7b0f2SMatthew Ahrens P2BOUNDARY(offset, size, zv->zv_volblocksize)) { 1189be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 119088b7b0f2SMatthew Ahrens } 119188b7b0f2SMatthew Ahrens ASSERT(size <= zv->zv_volblocksize); 1192e7cbe64fSgw 119388b7b0f2SMatthew Ahrens /* Locate the extent this belongs to */ 119488b7b0f2SMatthew Ahrens ze = list_head(&zv->zv_extents); 119588b7b0f2SMatthew Ahrens while (offset >= ze->ze_nblks * zv->zv_volblocksize) { 119688b7b0f2SMatthew Ahrens offset -= ze->ze_nblks * zv->zv_volblocksize; 119788b7b0f2SMatthew Ahrens ze = list_next(&zv->zv_extents, ze); 119888b7b0f2SMatthew Ahrens } 119924cc0e1cSGeorge Wilson 12003b2aab18SMatthew Ahrens if (ze == NULL) 1201be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 12023b2aab18SMatthew Ahrens 120324cc0e1cSGeorge Wilson if (!ddi_in_panic()) 120424cc0e1cSGeorge Wilson spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 120524cc0e1cSGeorge Wilson 120688b7b0f2SMatthew Ahrens vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva)); 120788b7b0f2SMatthew Ahrens offset += DVA_GET_OFFSET(&ze->ze_dva); 1208810e43b2SBill Pijewski error = zvol_dumpio_vdev(vd, addr, offset, DVA_GET_OFFSET(&ze->ze_dva), 1209810e43b2SBill Pijewski size, doread, isdump); 121024cc0e1cSGeorge Wilson 121124cc0e1cSGeorge Wilson if (!ddi_in_panic()) 121224cc0e1cSGeorge Wilson spa_config_exit(spa, SCL_STATE, FTAG); 121324cc0e1cSGeorge Wilson 1214e7cbe64fSgw return (error); 1215e7cbe64fSgw } 1216e7cbe64fSgw 1217fa9e4066Sahrens int 1218fa9e4066Sahrens zvol_strategy(buf_t *bp) 1219fa9e4066Sahrens { 1220c99e4bdcSChris Kirby zfs_soft_state_t *zs = NULL; 1221c99e4bdcSChris Kirby zvol_state_t *zv; 1222fa9e4066Sahrens uint64_t off, volsize; 122388b7b0f2SMatthew Ahrens size_t resid; 1224fa9e4066Sahrens char *addr; 122522ac5be4Sperrin objset_t *os; 1226c2e6a7d6Sperrin rl_t *rl; 1227fa9e4066Sahrens int error = 0; 122888b7b0f2SMatthew Ahrens boolean_t doread = bp->b_flags & B_READ; 1229810e43b2SBill Pijewski boolean_t is_dumpified; 1230510b6c0eSNeil Perrin boolean_t sync; 1231fa9e4066Sahrens 1232c99e4bdcSChris Kirby if (getminor(bp->b_edev) == 0) { 1233be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 1234c99e4bdcSChris Kirby } else { 1235c99e4bdcSChris Kirby zs = ddi_get_soft_state(zfsdev_state, getminor(bp->b_edev)); 1236c99e4bdcSChris Kirby if (zs == NULL) 1237be6fd75aSMatthew Ahrens error = SET_ERROR(ENXIO); 1238c99e4bdcSChris Kirby else if (zs->zss_type != ZSST_ZVOL) 1239be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 1240fa9e4066Sahrens } 1241fa9e4066Sahrens 1242c99e4bdcSChris Kirby if (error) { 1243c99e4bdcSChris Kirby bioerror(bp, error); 1244fa9e4066Sahrens biodone(bp); 1245fa9e4066Sahrens return (0); 1246fa9e4066Sahrens } 1247fa9e4066Sahrens 1248c99e4bdcSChris Kirby zv = zs->zss_data; 1249c99e4bdcSChris Kirby 1250681d9761SEric Taylor if (!(bp->b_flags & B_READ) && (zv->zv_flags & ZVOL_RDONLY)) { 1251fa9e4066Sahrens bioerror(bp, EROFS); 1252fa9e4066Sahrens biodone(bp); 1253fa9e4066Sahrens return (0); 1254fa9e4066Sahrens } 1255fa9e4066Sahrens 1256fa9e4066Sahrens off = ldbtob(bp->b_blkno); 1257fa9e4066Sahrens volsize = zv->zv_volsize; 1258fa9e4066Sahrens 125922ac5be4Sperrin os = zv->zv_objset; 126022ac5be4Sperrin ASSERT(os != NULL); 1261fa9e4066Sahrens 1262fa9e4066Sahrens bp_mapin(bp); 1263fa9e4066Sahrens addr = bp->b_un.b_addr; 1264fa9e4066Sahrens resid = bp->b_bcount; 1265fa9e4066Sahrens 126688b7b0f2SMatthew Ahrens if (resid > 0 && (off < 0 || off >= volsize)) { 126788b7b0f2SMatthew Ahrens bioerror(bp, EIO); 126888b7b0f2SMatthew Ahrens biodone(bp); 126988b7b0f2SMatthew Ahrens return (0); 127088b7b0f2SMatthew Ahrens } 127173ec3d9cSgw 1272810e43b2SBill Pijewski is_dumpified = zv->zv_flags & ZVOL_DUMPIFIED; 127355da60b9SMark J Musante sync = ((!(bp->b_flags & B_ASYNC) && 127455da60b9SMark J Musante !(zv->zv_flags & ZVOL_WCE)) || 127555da60b9SMark J Musante (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)) && 1276810e43b2SBill Pijewski !doread && !is_dumpified; 1277510b6c0eSNeil Perrin 1278a24e15ceSperrin /* 1279a24e15ceSperrin * There must be no buffer changes when doing a dmu_sync() because 1280a24e15ceSperrin * we can't change the data whilst calculating the checksum. 1281a24e15ceSperrin */ 1282c2e6a7d6Sperrin rl = zfs_range_lock(&zv->zv_znode, off, resid, 128388b7b0f2SMatthew Ahrens doread ? RL_READER : RL_WRITER); 1284fa9e4066Sahrens 1285e7cbe64fSgw while (resid != 0 && off < volsize) { 128688b7b0f2SMatthew Ahrens size_t size = MIN(resid, zvol_maxphys); 1287810e43b2SBill Pijewski if (is_dumpified) { 1288e7cbe64fSgw size = MIN(size, P2END(off, zv->zv_volblocksize) - off); 128988b7b0f2SMatthew Ahrens error = zvol_dumpio(zv, addr, off, size, 129088b7b0f2SMatthew Ahrens doread, B_FALSE); 129188b7b0f2SMatthew Ahrens } else if (doread) { 12927bfdf011SNeil Perrin error = dmu_read(os, ZVOL_OBJ, off, size, addr, 12937bfdf011SNeil Perrin DMU_READ_PREFETCH); 1294fa9e4066Sahrens } else { 129522ac5be4Sperrin dmu_tx_t *tx = dmu_tx_create(os); 1296fa9e4066Sahrens dmu_tx_hold_write(tx, ZVOL_OBJ, off, size); 1297fa9e4066Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 1298fa9e4066Sahrens if (error) { 1299fa9e4066Sahrens dmu_tx_abort(tx); 1300fa9e4066Sahrens } else { 130122ac5be4Sperrin dmu_write(os, ZVOL_OBJ, off, size, addr, tx); 1302510b6c0eSNeil Perrin zvol_log_write(zv, tx, off, size, sync); 1303fa9e4066Sahrens dmu_tx_commit(tx); 1304fa9e4066Sahrens } 1305fa9e4066Sahrens } 1306b87f3af3Sperrin if (error) { 1307b87f3af3Sperrin /* convert checksum errors into IO errors */ 1308b87f3af3Sperrin if (error == ECKSUM) 1309be6fd75aSMatthew Ahrens error = SET_ERROR(EIO); 1310fa9e4066Sahrens break; 1311b87f3af3Sperrin } 1312fa9e4066Sahrens off += size; 1313fa9e4066Sahrens addr += size; 1314fa9e4066Sahrens resid -= size; 1315fa9e4066Sahrens } 1316c2e6a7d6Sperrin zfs_range_unlock(rl); 1317fa9e4066Sahrens 1318fa9e4066Sahrens if ((bp->b_resid = resid) == bp->b_bcount) 1319fa9e4066Sahrens bioerror(bp, off > volsize ? EINVAL : error); 1320fa9e4066Sahrens 1321510b6c0eSNeil Perrin if (sync) 13225002558fSNeil Perrin zil_commit(zv->zv_zilog, ZVOL_OBJ); 1323feb08c6bSbillm biodone(bp); 132422ac5be4Sperrin 1325fa9e4066Sahrens return (0); 1326fa9e4066Sahrens } 1327fa9e4066Sahrens 132867bd71c6Sperrin /* 132967bd71c6Sperrin * Set the buffer count to the zvol maximum transfer. 133067bd71c6Sperrin * Using our own routine instead of the default minphys() 133167bd71c6Sperrin * means that for larger writes we write bigger buffers on X86 133267bd71c6Sperrin * (128K instead of 56K) and flush the disk write cache less often 133367bd71c6Sperrin * (every zvol_maxphys - currently 1MB) instead of minphys (currently 133467bd71c6Sperrin * 56K on X86 and 128K on sparc). 133567bd71c6Sperrin */ 133667bd71c6Sperrin void 133767bd71c6Sperrin zvol_minphys(struct buf *bp) 133867bd71c6Sperrin { 133967bd71c6Sperrin if (bp->b_bcount > zvol_maxphys) 134067bd71c6Sperrin bp->b_bcount = zvol_maxphys; 134167bd71c6Sperrin } 134267bd71c6Sperrin 1343e7cbe64fSgw int 1344e7cbe64fSgw zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks) 1345e7cbe64fSgw { 1346e7cbe64fSgw minor_t minor = getminor(dev); 1347e7cbe64fSgw zvol_state_t *zv; 1348e7cbe64fSgw int error = 0; 1349e7cbe64fSgw uint64_t size; 1350e7cbe64fSgw uint64_t boff; 1351e7cbe64fSgw uint64_t resid; 1352e7cbe64fSgw 1353c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1354e7cbe64fSgw if (zv == NULL) 1355be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 1356e7cbe64fSgw 13573b2aab18SMatthew Ahrens if ((zv->zv_flags & ZVOL_DUMPIFIED) == 0) 1358be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 13593b2aab18SMatthew Ahrens 1360e7cbe64fSgw boff = ldbtob(blkno); 1361e7cbe64fSgw resid = ldbtob(nblocks); 136288b7b0f2SMatthew Ahrens 136388b7b0f2SMatthew Ahrens VERIFY3U(boff + resid, <=, zv->zv_volsize); 136488b7b0f2SMatthew Ahrens 1365e7cbe64fSgw while (resid) { 1366e7cbe64fSgw size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff); 136788b7b0f2SMatthew Ahrens error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE); 1368e7cbe64fSgw if (error) 1369e7cbe64fSgw break; 1370e7cbe64fSgw boff += size; 1371e7cbe64fSgw addr += size; 1372e7cbe64fSgw resid -= size; 1373e7cbe64fSgw } 1374e7cbe64fSgw 1375e7cbe64fSgw return (error); 1376e7cbe64fSgw } 1377e7cbe64fSgw 1378fa9e4066Sahrens /*ARGSUSED*/ 1379fa9e4066Sahrens int 1380feb08c6bSbillm zvol_read(dev_t dev, uio_t *uio, cred_t *cr) 1381fa9e4066Sahrens { 1382c7ca1008Sgw minor_t minor = getminor(dev); 1383c7ca1008Sgw zvol_state_t *zv; 138473ec3d9cSgw uint64_t volsize; 1385c2e6a7d6Sperrin rl_t *rl; 1386feb08c6bSbillm int error = 0; 1387fa9e4066Sahrens 1388c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1389c7ca1008Sgw if (zv == NULL) 1390be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 1391c7ca1008Sgw 139273ec3d9cSgw volsize = zv->zv_volsize; 139373ec3d9cSgw if (uio->uio_resid > 0 && 139473ec3d9cSgw (uio->uio_loffset < 0 || uio->uio_loffset >= volsize)) 1395be6fd75aSMatthew Ahrens return (SET_ERROR(EIO)); 139673ec3d9cSgw 139788b7b0f2SMatthew Ahrens if (zv->zv_flags & ZVOL_DUMPIFIED) { 139888b7b0f2SMatthew Ahrens error = physio(zvol_strategy, NULL, dev, B_READ, 139988b7b0f2SMatthew Ahrens zvol_minphys, uio); 140088b7b0f2SMatthew Ahrens return (error); 140188b7b0f2SMatthew Ahrens } 140288b7b0f2SMatthew Ahrens 1403c2e6a7d6Sperrin rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 1404c2e6a7d6Sperrin RL_READER); 140573ec3d9cSgw while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 1406feb08c6bSbillm uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 1407fa9e4066Sahrens 140873ec3d9cSgw /* don't read past the end */ 140973ec3d9cSgw if (bytes > volsize - uio->uio_loffset) 141073ec3d9cSgw bytes = volsize - uio->uio_loffset; 141173ec3d9cSgw 1412feb08c6bSbillm error = dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes); 1413b87f3af3Sperrin if (error) { 1414b87f3af3Sperrin /* convert checksum errors into IO errors */ 1415b87f3af3Sperrin if (error == ECKSUM) 1416be6fd75aSMatthew Ahrens error = SET_ERROR(EIO); 1417feb08c6bSbillm break; 1418b87f3af3Sperrin } 1419feb08c6bSbillm } 1420c2e6a7d6Sperrin zfs_range_unlock(rl); 1421feb08c6bSbillm return (error); 1422fa9e4066Sahrens } 1423fa9e4066Sahrens 1424fa9e4066Sahrens /*ARGSUSED*/ 1425fa9e4066Sahrens int 1426feb08c6bSbillm zvol_write(dev_t dev, uio_t *uio, cred_t *cr) 1427fa9e4066Sahrens { 1428c7ca1008Sgw minor_t minor = getminor(dev); 1429c7ca1008Sgw zvol_state_t *zv; 143073ec3d9cSgw uint64_t volsize; 1431c2e6a7d6Sperrin rl_t *rl; 1432feb08c6bSbillm int error = 0; 1433510b6c0eSNeil Perrin boolean_t sync; 1434feb08c6bSbillm 1435c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1436c7ca1008Sgw if (zv == NULL) 1437be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 1438c7ca1008Sgw 143973ec3d9cSgw volsize = zv->zv_volsize; 144073ec3d9cSgw if (uio->uio_resid > 0 && 144173ec3d9cSgw (uio->uio_loffset < 0 || uio->uio_loffset >= volsize)) 1442be6fd75aSMatthew Ahrens return (SET_ERROR(EIO)); 144373ec3d9cSgw 1444e7cbe64fSgw if (zv->zv_flags & ZVOL_DUMPIFIED) { 1445e7cbe64fSgw error = physio(zvol_strategy, NULL, dev, B_WRITE, 1446e7cbe64fSgw zvol_minphys, uio); 1447e7cbe64fSgw return (error); 1448e7cbe64fSgw } 1449e7cbe64fSgw 145055da60b9SMark J Musante sync = !(zv->zv_flags & ZVOL_WCE) || 145155da60b9SMark J Musante (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS); 1452510b6c0eSNeil Perrin 1453c2e6a7d6Sperrin rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 1454c2e6a7d6Sperrin RL_WRITER); 145573ec3d9cSgw while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 1456feb08c6bSbillm uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 1457feb08c6bSbillm uint64_t off = uio->uio_loffset; 1458feb08c6bSbillm dmu_tx_t *tx = dmu_tx_create(zv->zv_objset); 145973ec3d9cSgw 146073ec3d9cSgw if (bytes > volsize - off) /* don't write past the end */ 146173ec3d9cSgw bytes = volsize - off; 146273ec3d9cSgw 1463feb08c6bSbillm dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 1464feb08c6bSbillm error = dmu_tx_assign(tx, TXG_WAIT); 1465feb08c6bSbillm if (error) { 1466feb08c6bSbillm dmu_tx_abort(tx); 1467feb08c6bSbillm break; 1468feb08c6bSbillm } 1469*8dfe5547SRichard Yao error = dmu_write_uio_dnode(zv->zv_dn, uio, bytes, tx); 1470feb08c6bSbillm if (error == 0) 1471510b6c0eSNeil Perrin zvol_log_write(zv, tx, off, bytes, sync); 1472feb08c6bSbillm dmu_tx_commit(tx); 1473feb08c6bSbillm 1474feb08c6bSbillm if (error) 1475feb08c6bSbillm break; 1476feb08c6bSbillm } 1477c2e6a7d6Sperrin zfs_range_unlock(rl); 1478510b6c0eSNeil Perrin if (sync) 14795002558fSNeil Perrin zil_commit(zv->zv_zilog, ZVOL_OBJ); 1480feb08c6bSbillm return (error); 1481fa9e4066Sahrens } 1482fa9e4066Sahrens 1483c7f714e2SEric Taylor int 1484c7f714e2SEric Taylor zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs) 1485c7f714e2SEric Taylor { 1486c7f714e2SEric Taylor struct uuid uuid = EFI_RESERVED; 1487c7f714e2SEric Taylor efi_gpe_t gpe = { 0 }; 1488c7f714e2SEric Taylor uint32_t crc; 1489c7f714e2SEric Taylor dk_efi_t efi; 1490c7f714e2SEric Taylor int length; 1491c7f714e2SEric Taylor char *ptr; 1492c7f714e2SEric Taylor 1493c7f714e2SEric Taylor if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag)) 1494be6fd75aSMatthew Ahrens return (SET_ERROR(EFAULT)); 1495c7f714e2SEric Taylor ptr = (char *)(uintptr_t)efi.dki_data_64; 1496c7f714e2SEric Taylor length = efi.dki_length; 1497c7f714e2SEric Taylor /* 1498c7f714e2SEric Taylor * Some clients may attempt to request a PMBR for the 1499c7f714e2SEric Taylor * zvol. Currently this interface will return EINVAL to 1500c7f714e2SEric Taylor * such requests. These requests could be supported by 1501c7f714e2SEric Taylor * adding a check for lba == 0 and consing up an appropriate 1502c7f714e2SEric Taylor * PMBR. 1503c7f714e2SEric Taylor */ 1504c7f714e2SEric Taylor if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0) 1505be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 1506c7f714e2SEric Taylor 1507c7f714e2SEric Taylor gpe.efi_gpe_StartingLBA = LE_64(34ULL); 1508c7f714e2SEric Taylor gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1); 1509c7f714e2SEric Taylor UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid); 1510c7f714e2SEric Taylor 1511c7f714e2SEric Taylor if (efi.dki_lba == 1) { 1512c7f714e2SEric Taylor efi_gpt_t gpt = { 0 }; 1513c7f714e2SEric Taylor 1514c7f714e2SEric Taylor gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE); 1515c7f714e2SEric Taylor gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT); 1516c7f714e2SEric Taylor gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt)); 1517c7f714e2SEric Taylor gpt.efi_gpt_MyLBA = LE_64(1ULL); 1518c7f714e2SEric Taylor gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL); 1519c7f714e2SEric Taylor gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1); 1520c7f714e2SEric Taylor gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL); 1521c7f714e2SEric Taylor gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1); 1522c7f714e2SEric Taylor gpt.efi_gpt_SizeOfPartitionEntry = 1523c7f714e2SEric Taylor LE_32(sizeof (efi_gpe_t)); 1524c7f714e2SEric Taylor CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table); 1525c7f714e2SEric Taylor gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc); 1526c7f714e2SEric Taylor CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table); 1527c7f714e2SEric Taylor gpt.efi_gpt_HeaderCRC32 = LE_32(~crc); 1528c7f714e2SEric Taylor if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length), 1529c7f714e2SEric Taylor flag)) 1530be6fd75aSMatthew Ahrens return (SET_ERROR(EFAULT)); 1531c7f714e2SEric Taylor ptr += sizeof (gpt); 1532c7f714e2SEric Taylor length -= sizeof (gpt); 1533c7f714e2SEric Taylor } 1534c7f714e2SEric Taylor if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe), 1535c7f714e2SEric Taylor length), flag)) 1536be6fd75aSMatthew Ahrens return (SET_ERROR(EFAULT)); 1537c7f714e2SEric Taylor return (0); 1538c7f714e2SEric Taylor } 1539c7f714e2SEric Taylor 15403fb517f7SJames Moore /* 15413fb517f7SJames Moore * BEGIN entry points to allow external callers access to the volume. 15423fb517f7SJames Moore */ 15433fb517f7SJames Moore /* 15443fb517f7SJames Moore * Return the volume parameters needed for access from an external caller. 15453fb517f7SJames Moore * These values are invariant as long as the volume is held open. 15463fb517f7SJames Moore */ 15473fb517f7SJames Moore int 15483fb517f7SJames Moore zvol_get_volume_params(minor_t minor, uint64_t *blksize, 15493fb517f7SJames Moore uint64_t *max_xfer_len, void **minor_hdl, void **objset_hdl, void **zil_hdl, 1550*8dfe5547SRichard Yao void **rl_hdl, void **dnode_hdl) 15513fb517f7SJames Moore { 15523fb517f7SJames Moore zvol_state_t *zv; 15533fb517f7SJames Moore 1554c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1555c99e4bdcSChris Kirby if (zv == NULL) 1556be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 15573fb517f7SJames Moore if (zv->zv_flags & ZVOL_DUMPIFIED) 1558be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 15593fb517f7SJames Moore 15603fb517f7SJames Moore ASSERT(blksize && max_xfer_len && minor_hdl && 1561*8dfe5547SRichard Yao objset_hdl && zil_hdl && rl_hdl && dnode_hdl); 15623fb517f7SJames Moore 15633fb517f7SJames Moore *blksize = zv->zv_volblocksize; 15643fb517f7SJames Moore *max_xfer_len = (uint64_t)zvol_maxphys; 15653fb517f7SJames Moore *minor_hdl = zv; 15663fb517f7SJames Moore *objset_hdl = zv->zv_objset; 15673fb517f7SJames Moore *zil_hdl = zv->zv_zilog; 15683fb517f7SJames Moore *rl_hdl = &zv->zv_znode; 1569*8dfe5547SRichard Yao *dnode_hdl = zv->zv_dn; 15703fb517f7SJames Moore return (0); 15713fb517f7SJames Moore } 15723fb517f7SJames Moore 15733fb517f7SJames Moore /* 15743fb517f7SJames Moore * Return the current volume size to an external caller. 15753fb517f7SJames Moore * The size can change while the volume is open. 15763fb517f7SJames Moore */ 15773fb517f7SJames Moore uint64_t 15783fb517f7SJames Moore zvol_get_volume_size(void *minor_hdl) 15793fb517f7SJames Moore { 15803fb517f7SJames Moore zvol_state_t *zv = minor_hdl; 15813fb517f7SJames Moore 15823fb517f7SJames Moore return (zv->zv_volsize); 15833fb517f7SJames Moore } 15843fb517f7SJames Moore 15853fb517f7SJames Moore /* 15863fb517f7SJames Moore * Return the current WCE setting to an external caller. 15873fb517f7SJames Moore * The WCE setting can change while the volume is open. 15883fb517f7SJames Moore */ 15893fb517f7SJames Moore int 15903fb517f7SJames Moore zvol_get_volume_wce(void *minor_hdl) 15913fb517f7SJames Moore { 15923fb517f7SJames Moore zvol_state_t *zv = minor_hdl; 15933fb517f7SJames Moore 15943fb517f7SJames Moore return ((zv->zv_flags & ZVOL_WCE) ? 1 : 0); 15953fb517f7SJames Moore } 15963fb517f7SJames Moore 15973fb517f7SJames Moore /* 15983fb517f7SJames Moore * Entry point for external callers to zvol_log_write 15993fb517f7SJames Moore */ 16003fb517f7SJames Moore void 16013fb517f7SJames Moore zvol_log_write_minor(void *minor_hdl, dmu_tx_t *tx, offset_t off, ssize_t resid, 16023fb517f7SJames Moore boolean_t sync) 16033fb517f7SJames Moore { 16043fb517f7SJames Moore zvol_state_t *zv = minor_hdl; 16053fb517f7SJames Moore 16063fb517f7SJames Moore zvol_log_write(zv, tx, off, resid, sync); 16073fb517f7SJames Moore } 16083fb517f7SJames Moore /* 16093fb517f7SJames Moore * END entry points to allow external callers access to the volume. 16103fb517f7SJames Moore */ 16113fb517f7SJames Moore 1612b77b9231SDan McDonald /* 1613b77b9231SDan McDonald * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE. 1614b77b9231SDan McDonald */ 1615b77b9231SDan McDonald static void 1616b77b9231SDan McDonald zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off, uint64_t len, 1617b77b9231SDan McDonald boolean_t sync) 1618b77b9231SDan McDonald { 1619b77b9231SDan McDonald itx_t *itx; 1620b77b9231SDan McDonald lr_truncate_t *lr; 1621b77b9231SDan McDonald zilog_t *zilog = zv->zv_zilog; 1622b77b9231SDan McDonald 1623b77b9231SDan McDonald if (zil_replaying(zilog, tx)) 1624b77b9231SDan McDonald return; 1625b77b9231SDan McDonald 1626b77b9231SDan McDonald itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr)); 1627b77b9231SDan McDonald lr = (lr_truncate_t *)&itx->itx_lr; 1628b77b9231SDan McDonald lr->lr_foid = ZVOL_OBJ; 1629b77b9231SDan McDonald lr->lr_offset = off; 1630b77b9231SDan McDonald lr->lr_length = len; 1631b77b9231SDan McDonald 1632b77b9231SDan McDonald itx->itx_sync = sync; 1633b77b9231SDan McDonald zil_itx_assign(zilog, itx, tx); 1634b77b9231SDan McDonald } 1635b77b9231SDan McDonald 1636fa9e4066Sahrens /* 1637fa9e4066Sahrens * Dirtbag ioctls to support mkfs(1M) for UFS filesystems. See dkio(7I). 1638b77b9231SDan McDonald * Also a dirtbag dkio ioctl for unmap/free-block functionality. 1639fa9e4066Sahrens */ 1640fa9e4066Sahrens /*ARGSUSED*/ 1641fa9e4066Sahrens int 1642fa9e4066Sahrens zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 1643fa9e4066Sahrens { 1644fa9e4066Sahrens zvol_state_t *zv; 1645af2c4821Smaybee struct dk_callback *dkc; 1646fa9e4066Sahrens int error = 0; 1647e7cbe64fSgw rl_t *rl; 1648fa9e4066Sahrens 1649c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 1650fa9e4066Sahrens 1651c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(getminor(dev), ZSST_ZVOL); 1652fa9e4066Sahrens 1653fa9e4066Sahrens if (zv == NULL) { 1654c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 1655be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 1656fa9e4066Sahrens } 1657701f66c4SEric Taylor ASSERT(zv->zv_total_opens > 0); 1658fa9e4066Sahrens 1659fa9e4066Sahrens switch (cmd) { 1660fa9e4066Sahrens 1661fa9e4066Sahrens case DKIOCINFO: 1662a0b60564SGeorge Wilson { 1663a0b60564SGeorge Wilson struct dk_cinfo dki; 1664a0b60564SGeorge Wilson 1665af2c4821Smaybee bzero(&dki, sizeof (dki)); 1666af2c4821Smaybee (void) strcpy(dki.dki_cname, "zvol"); 1667af2c4821Smaybee (void) strcpy(dki.dki_dname, "zvol"); 1668af2c4821Smaybee dki.dki_ctype = DKC_UNKNOWN; 16693adc9019SEric Taylor dki.dki_unit = getminor(dev); 1670b5152584SMatthew Ahrens dki.dki_maxtransfer = 1671b5152584SMatthew Ahrens 1 << (SPA_OLD_MAXBLOCKSHIFT - zv->zv_min_bs); 1672c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 1673af2c4821Smaybee if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag)) 1674be6fd75aSMatthew Ahrens error = SET_ERROR(EFAULT); 1675fa9e4066Sahrens return (error); 1676a0b60564SGeorge Wilson } 1677fa9e4066Sahrens 1678fa9e4066Sahrens case DKIOCGMEDIAINFO: 1679a0b60564SGeorge Wilson { 1680a0b60564SGeorge Wilson struct dk_minfo dkm; 1681a0b60564SGeorge Wilson 1682fa9e4066Sahrens bzero(&dkm, sizeof (dkm)); 1683fa9e4066Sahrens dkm.dki_lbsize = 1U << zv->zv_min_bs; 1684fa9e4066Sahrens dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs; 1685fa9e4066Sahrens dkm.dki_media_type = DK_UNKNOWN; 1686c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 1687fa9e4066Sahrens if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag)) 1688be6fd75aSMatthew Ahrens error = SET_ERROR(EFAULT); 1689fa9e4066Sahrens return (error); 1690a0b60564SGeorge Wilson } 1691a0b60564SGeorge Wilson 1692a0b60564SGeorge Wilson case DKIOCGMEDIAINFOEXT: 1693a0b60564SGeorge Wilson { 1694a0b60564SGeorge Wilson struct dk_minfo_ext dkmext; 1695a0b60564SGeorge Wilson 1696a0b60564SGeorge Wilson bzero(&dkmext, sizeof (dkmext)); 1697a0b60564SGeorge Wilson dkmext.dki_lbsize = 1U << zv->zv_min_bs; 1698a0b60564SGeorge Wilson dkmext.dki_pbsize = zv->zv_volblocksize; 1699a0b60564SGeorge Wilson dkmext.dki_capacity = zv->zv_volsize >> zv->zv_min_bs; 1700a0b60564SGeorge Wilson dkmext.dki_media_type = DK_UNKNOWN; 1701a0b60564SGeorge Wilson mutex_exit(&zfsdev_state_lock); 1702a0b60564SGeorge Wilson if (ddi_copyout(&dkmext, (void *)arg, sizeof (dkmext), flag)) 1703a0b60564SGeorge Wilson error = SET_ERROR(EFAULT); 1704a0b60564SGeorge Wilson return (error); 1705a0b60564SGeorge Wilson } 1706fa9e4066Sahrens 1707fa9e4066Sahrens case DKIOCGETEFI: 1708a0b60564SGeorge Wilson { 1709a0b60564SGeorge Wilson uint64_t vs = zv->zv_volsize; 1710a0b60564SGeorge Wilson uint8_t bs = zv->zv_min_bs; 1711fa9e4066Sahrens 1712a0b60564SGeorge Wilson mutex_exit(&zfsdev_state_lock); 1713a0b60564SGeorge Wilson error = zvol_getefi((void *)arg, flag, vs, bs); 1714a0b60564SGeorge Wilson return (error); 1715a0b60564SGeorge Wilson } 1716fa9e4066Sahrens 1717feb08c6bSbillm case DKIOCFLUSHWRITECACHE: 1718af2c4821Smaybee dkc = (struct dk_callback *)arg; 1719c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 17205002558fSNeil Perrin zil_commit(zv->zv_zilog, ZVOL_OBJ); 1721af2c4821Smaybee if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) { 1722af2c4821Smaybee (*dkc->dkc_callback)(dkc->dkc_cookie, error); 1723af2c4821Smaybee error = 0; 1724af2c4821Smaybee } 1725701f66c4SEric Taylor return (error); 1726701f66c4SEric Taylor 1727701f66c4SEric Taylor case DKIOCGETWCE: 1728a0b60564SGeorge Wilson { 1729a0b60564SGeorge Wilson int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0; 1730a0b60564SGeorge Wilson if (ddi_copyout(&wce, (void *)arg, sizeof (int), 1731a0b60564SGeorge Wilson flag)) 1732a0b60564SGeorge Wilson error = SET_ERROR(EFAULT); 1733a0b60564SGeorge Wilson break; 1734a0b60564SGeorge Wilson } 1735a0b60564SGeorge Wilson case DKIOCSETWCE: 1736a0b60564SGeorge Wilson { 1737a0b60564SGeorge Wilson int wce; 1738a0b60564SGeorge Wilson if (ddi_copyin((void *)arg, &wce, sizeof (int), 1739a0b60564SGeorge Wilson flag)) { 1740a0b60564SGeorge Wilson error = SET_ERROR(EFAULT); 1741701f66c4SEric Taylor break; 1742701f66c4SEric Taylor } 1743a0b60564SGeorge Wilson if (wce) { 1744a0b60564SGeorge Wilson zv->zv_flags |= ZVOL_WCE; 1745a0b60564SGeorge Wilson mutex_exit(&zfsdev_state_lock); 1746a0b60564SGeorge Wilson } else { 1747a0b60564SGeorge Wilson zv->zv_flags &= ~ZVOL_WCE; 1748a0b60564SGeorge Wilson mutex_exit(&zfsdev_state_lock); 1749a0b60564SGeorge Wilson zil_commit(zv->zv_zilog, ZVOL_OBJ); 1750701f66c4SEric Taylor } 1751a0b60564SGeorge Wilson return (0); 1752a0b60564SGeorge Wilson } 1753feb08c6bSbillm 1754b6130eadSmaybee case DKIOCGGEOM: 1755b6130eadSmaybee case DKIOCGVTOC: 1756e7cbe64fSgw /* 1757e7cbe64fSgw * commands using these (like prtvtoc) expect ENOTSUP 1758e7cbe64fSgw * since we're emulating an EFI label 1759e7cbe64fSgw */ 1760be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 1761b6130eadSmaybee break; 1762b6130eadSmaybee 1763e7cbe64fSgw case DKIOCDUMPINIT: 1764e7cbe64fSgw rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 1765e7cbe64fSgw RL_WRITER); 1766e7cbe64fSgw error = zvol_dumpify(zv); 1767e7cbe64fSgw zfs_range_unlock(rl); 1768e7cbe64fSgw break; 1769e7cbe64fSgw 1770e7cbe64fSgw case DKIOCDUMPFINI: 177106d5ae10SEric Taylor if (!(zv->zv_flags & ZVOL_DUMPIFIED)) 177206d5ae10SEric Taylor break; 1773e7cbe64fSgw rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 1774e7cbe64fSgw RL_WRITER); 1775e7cbe64fSgw error = zvol_dump_fini(zv); 1776e7cbe64fSgw zfs_range_unlock(rl); 1777e7cbe64fSgw break; 1778e7cbe64fSgw 1779b77b9231SDan McDonald case DKIOCFREE: 1780b77b9231SDan McDonald { 1781b77b9231SDan McDonald dkioc_free_t df; 1782b77b9231SDan McDonald dmu_tx_t *tx; 1783b77b9231SDan McDonald 1784893c83baSGeorge Wilson if (!zvol_unmap_enabled) 1785893c83baSGeorge Wilson break; 1786893c83baSGeorge Wilson 1787b77b9231SDan McDonald if (ddi_copyin((void *)arg, &df, sizeof (df), flag)) { 1788be6fd75aSMatthew Ahrens error = SET_ERROR(EFAULT); 1789b77b9231SDan McDonald break; 1790b77b9231SDan McDonald } 1791b77b9231SDan McDonald 1792b77b9231SDan McDonald /* 1793b77b9231SDan McDonald * Apply Postel's Law to length-checking. If they overshoot, 1794b77b9231SDan McDonald * just blank out until the end, if there's a need to blank 1795b77b9231SDan McDonald * out anything. 1796b77b9231SDan McDonald */ 1797b77b9231SDan McDonald if (df.df_start >= zv->zv_volsize) 1798b77b9231SDan McDonald break; /* No need to do anything... */ 1799574e2414SGeorge Wilson 1800574e2414SGeorge Wilson mutex_exit(&zfsdev_state_lock); 1801b77b9231SDan McDonald 1802b77b9231SDan McDonald rl = zfs_range_lock(&zv->zv_znode, df.df_start, df.df_length, 1803b77b9231SDan McDonald RL_WRITER); 1804b77b9231SDan McDonald tx = dmu_tx_create(zv->zv_objset); 18054bb73804SMatthew Ahrens dmu_tx_mark_netfree(tx); 1806b77b9231SDan McDonald error = dmu_tx_assign(tx, TXG_WAIT); 1807b77b9231SDan McDonald if (error != 0) { 1808b77b9231SDan McDonald dmu_tx_abort(tx); 1809b77b9231SDan McDonald } else { 1810b77b9231SDan McDonald zvol_log_truncate(zv, tx, df.df_start, 1811b77b9231SDan McDonald df.df_length, B_TRUE); 1812c08b1637SJosef 'Jeff' Sipek dmu_tx_commit(tx); 1813b77b9231SDan McDonald error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 1814b77b9231SDan McDonald df.df_start, df.df_length); 1815b77b9231SDan McDonald } 1816b77b9231SDan McDonald 1817b77b9231SDan McDonald zfs_range_unlock(rl); 1818b77b9231SDan McDonald 18191c9272b8SStephen Blinick /* 18201c9272b8SStephen Blinick * If the write-cache is disabled, 'sync' property 18211c9272b8SStephen Blinick * is set to 'always', or if the caller is asking for 18221c9272b8SStephen Blinick * a synchronous free, commit this operation to the zil. 18231c9272b8SStephen Blinick * This will sync any previous uncommitted writes to the 18241c9272b8SStephen Blinick * zvol object. 18251c9272b8SStephen Blinick * Can be overridden by the zvol_unmap_sync_enabled tunable. 18261c9272b8SStephen Blinick */ 18271c9272b8SStephen Blinick if ((error == 0) && zvol_unmap_sync_enabled && 18281c9272b8SStephen Blinick (!(zv->zv_flags & ZVOL_WCE) || 18291c9272b8SStephen Blinick (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS) || 18301c9272b8SStephen Blinick (df.df_flags & DF_WAIT_SYNC))) { 18311c9272b8SStephen Blinick zil_commit(zv->zv_zilog, ZVOL_OBJ); 1832b77b9231SDan McDonald } 18331c9272b8SStephen Blinick 1834574e2414SGeorge Wilson return (error); 1835b77b9231SDan McDonald } 1836b77b9231SDan McDonald 1837fa9e4066Sahrens default: 1838be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTTY); 1839fa9e4066Sahrens break; 1840fa9e4066Sahrens 1841fa9e4066Sahrens } 1842c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 1843fa9e4066Sahrens return (error); 1844fa9e4066Sahrens } 1845fa9e4066Sahrens 1846fa9e4066Sahrens int 1847fa9e4066Sahrens zvol_busy(void) 1848fa9e4066Sahrens { 1849fa9e4066Sahrens return (zvol_minors != 0); 1850fa9e4066Sahrens } 1851fa9e4066Sahrens 1852fa9e4066Sahrens void 1853fa9e4066Sahrens zvol_init(void) 1854fa9e4066Sahrens { 1855c99e4bdcSChris Kirby VERIFY(ddi_soft_state_init(&zfsdev_state, sizeof (zfs_soft_state_t), 1856c99e4bdcSChris Kirby 1) == 0); 1857c99e4bdcSChris Kirby mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL); 1858fa9e4066Sahrens } 1859fa9e4066Sahrens 1860fa9e4066Sahrens void 1861fa9e4066Sahrens zvol_fini(void) 1862fa9e4066Sahrens { 1863c99e4bdcSChris Kirby mutex_destroy(&zfsdev_state_lock); 1864c99e4bdcSChris Kirby ddi_soft_state_fini(&zfsdev_state); 1865fa9e4066Sahrens } 1866e7cbe64fSgw 1867810e43b2SBill Pijewski /*ARGSUSED*/ 1868810e43b2SBill Pijewski static int 1869810e43b2SBill Pijewski zfs_mvdev_dump_feature_check(void *arg, dmu_tx_t *tx) 1870810e43b2SBill Pijewski { 1871810e43b2SBill Pijewski spa_t *spa = dmu_tx_pool(tx)->dp_spa; 1872810e43b2SBill Pijewski 18732acef22dSMatthew Ahrens if (spa_feature_is_active(spa, SPA_FEATURE_MULTI_VDEV_CRASH_DUMP)) 1874810e43b2SBill Pijewski return (1); 1875810e43b2SBill Pijewski return (0); 1876810e43b2SBill Pijewski } 1877810e43b2SBill Pijewski 1878810e43b2SBill Pijewski /*ARGSUSED*/ 1879810e43b2SBill Pijewski static void 1880810e43b2SBill Pijewski zfs_mvdev_dump_activate_feature_sync(void *arg, dmu_tx_t *tx) 1881810e43b2SBill Pijewski { 1882810e43b2SBill Pijewski spa_t *spa = dmu_tx_pool(tx)->dp_spa; 1883810e43b2SBill Pijewski 18842acef22dSMatthew Ahrens spa_feature_incr(spa, SPA_FEATURE_MULTI_VDEV_CRASH_DUMP, tx); 1885810e43b2SBill Pijewski } 1886810e43b2SBill Pijewski 1887e7cbe64fSgw static int 1888e7cbe64fSgw zvol_dump_init(zvol_state_t *zv, boolean_t resize) 1889e7cbe64fSgw { 1890e7cbe64fSgw dmu_tx_t *tx; 1891810e43b2SBill Pijewski int error; 1892e7cbe64fSgw objset_t *os = zv->zv_objset; 1893810e43b2SBill Pijewski spa_t *spa = dmu_objset_spa(os); 1894810e43b2SBill Pijewski vdev_t *vd = spa->spa_root_vdev; 1895e7cbe64fSgw nvlist_t *nv = NULL; 1896810e43b2SBill Pijewski uint64_t version = spa_version(spa); 1897b10bba72SGeorge Wilson uint64_t checksum, compress, refresrv, vbs, dedup; 1898e7cbe64fSgw 1899c99e4bdcSChris Kirby ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 1900810e43b2SBill Pijewski ASSERT(vd->vdev_ops == &vdev_root_ops); 1901810e43b2SBill Pijewski 1902681d9761SEric Taylor error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0, 1903681d9761SEric Taylor DMU_OBJECT_END); 1904b10bba72SGeorge Wilson if (error != 0) 1905b10bba72SGeorge Wilson return (error); 1906681d9761SEric Taylor /* wait for dmu_free_long_range to actually free the blocks */ 1907681d9761SEric Taylor txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 1908e7cbe64fSgw 1909810e43b2SBill Pijewski /* 1910810e43b2SBill Pijewski * If the pool on which the dump device is being initialized has more 1911810e43b2SBill Pijewski * than one child vdev, check that the MULTI_VDEV_CRASH_DUMP feature is 1912810e43b2SBill Pijewski * enabled. If so, bump that feature's counter to indicate that the 1913810e43b2SBill Pijewski * feature is active. We also check the vdev type to handle the 1914810e43b2SBill Pijewski * following case: 1915810e43b2SBill Pijewski * # zpool create test raidz disk1 disk2 disk3 1916810e43b2SBill Pijewski * Now have spa_root_vdev->vdev_children == 1 (the raidz vdev), 1917810e43b2SBill Pijewski * the raidz vdev itself has 3 children. 1918810e43b2SBill Pijewski */ 1919810e43b2SBill Pijewski if (vd->vdev_children > 1 || vd->vdev_ops == &vdev_raidz_ops) { 1920810e43b2SBill Pijewski if (!spa_feature_is_enabled(spa, 19212acef22dSMatthew Ahrens SPA_FEATURE_MULTI_VDEV_CRASH_DUMP)) 1922810e43b2SBill Pijewski return (SET_ERROR(ENOTSUP)); 1923810e43b2SBill Pijewski (void) dsl_sync_task(spa_name(spa), 1924810e43b2SBill Pijewski zfs_mvdev_dump_feature_check, 19257d46dc6cSMatthew Ahrens zfs_mvdev_dump_activate_feature_sync, NULL, 19267d46dc6cSMatthew Ahrens 2, ZFS_SPACE_CHECK_RESERVED); 1927810e43b2SBill Pijewski } 1928810e43b2SBill Pijewski 1929b10bba72SGeorge Wilson if (!resize) { 1930b10bba72SGeorge Wilson error = dsl_prop_get_integer(zv->zv_name, 1931b10bba72SGeorge Wilson zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL); 1932b10bba72SGeorge Wilson if (error == 0) { 1933b10bba72SGeorge Wilson error = dsl_prop_get_integer(zv->zv_name, 1934b10bba72SGeorge Wilson zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, 1935b10bba72SGeorge Wilson NULL); 1936b10bba72SGeorge Wilson } 1937b10bba72SGeorge Wilson if (error == 0) { 1938b10bba72SGeorge Wilson error = dsl_prop_get_integer(zv->zv_name, 1939b10bba72SGeorge Wilson zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 1940b10bba72SGeorge Wilson &refresrv, NULL); 1941b10bba72SGeorge Wilson } 1942b10bba72SGeorge Wilson if (error == 0) { 1943b10bba72SGeorge Wilson error = dsl_prop_get_integer(zv->zv_name, 1944b10bba72SGeorge Wilson zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, 1945b10bba72SGeorge Wilson NULL); 1946b10bba72SGeorge Wilson } 1947b10bba72SGeorge Wilson if (version >= SPA_VERSION_DEDUP && error == 0) { 1948b10bba72SGeorge Wilson error = dsl_prop_get_integer(zv->zv_name, 1949b10bba72SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), &dedup, NULL); 1950b10bba72SGeorge Wilson } 1951b10bba72SGeorge Wilson } 1952b10bba72SGeorge Wilson if (error != 0) 1953b10bba72SGeorge Wilson return (error); 1954b10bba72SGeorge Wilson 1955e7cbe64fSgw tx = dmu_tx_create(os); 1956e7cbe64fSgw dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 1957681d9761SEric Taylor dmu_tx_hold_bonus(tx, ZVOL_OBJ); 1958e7cbe64fSgw error = dmu_tx_assign(tx, TXG_WAIT); 1959b10bba72SGeorge Wilson if (error != 0) { 1960e7cbe64fSgw dmu_tx_abort(tx); 1961e7cbe64fSgw return (error); 1962e7cbe64fSgw } 1963e7cbe64fSgw 1964e7cbe64fSgw /* 1965e7cbe64fSgw * If we are resizing the dump device then we only need to 1966e7cbe64fSgw * update the refreservation to match the newly updated 1967e7cbe64fSgw * zvolsize. Otherwise, we save off the original state of the 1968e7cbe64fSgw * zvol so that we can restore them if the zvol is ever undumpified. 1969e7cbe64fSgw */ 1970e7cbe64fSgw if (resize) { 1971e7cbe64fSgw error = zap_update(os, ZVOL_ZAP_OBJ, 1972e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 1973e7cbe64fSgw &zv->zv_volsize, tx); 1974e7cbe64fSgw } else { 1975b10bba72SGeorge Wilson error = zap_update(os, ZVOL_ZAP_OBJ, 1976e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, 1977e7cbe64fSgw &compress, tx); 1978b10bba72SGeorge Wilson if (error == 0) { 1979b10bba72SGeorge Wilson error = zap_update(os, ZVOL_ZAP_OBJ, 1980b10bba72SGeorge Wilson zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, 1981b10bba72SGeorge Wilson &checksum, tx); 1982b10bba72SGeorge Wilson } 1983b10bba72SGeorge Wilson if (error == 0) { 1984b10bba72SGeorge Wilson error = zap_update(os, ZVOL_ZAP_OBJ, 1985b10bba72SGeorge Wilson zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 1986b10bba72SGeorge Wilson &refresrv, tx); 1987b10bba72SGeorge Wilson } 1988b10bba72SGeorge Wilson if (error == 0) { 1989b10bba72SGeorge Wilson error = zap_update(os, ZVOL_ZAP_OBJ, 1990b10bba72SGeorge Wilson zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, 1991b10bba72SGeorge Wilson &vbs, tx); 1992b10bba72SGeorge Wilson } 1993b10bba72SGeorge Wilson if (error == 0) { 1994b10bba72SGeorge Wilson error = dmu_object_set_blocksize( 1995b10bba72SGeorge Wilson os, ZVOL_OBJ, SPA_OLD_MAXBLOCKSIZE, 0, tx); 1996b10bba72SGeorge Wilson } 1997b10bba72SGeorge Wilson if (version >= SPA_VERSION_DEDUP && error == 0) { 1998b10bba72SGeorge Wilson error = zap_update(os, ZVOL_ZAP_OBJ, 19998d265e66SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, 20008d265e66SGeorge Wilson &dedup, tx); 20018d265e66SGeorge Wilson } 2002681d9761SEric Taylor if (error == 0) 2003b5152584SMatthew Ahrens zv->zv_volblocksize = SPA_OLD_MAXBLOCKSIZE; 2004e7cbe64fSgw } 2005e7cbe64fSgw dmu_tx_commit(tx); 2006e7cbe64fSgw 2007e7cbe64fSgw /* 2008e7cbe64fSgw * We only need update the zvol's property if we are initializing 2009e7cbe64fSgw * the dump area for the first time. 2010e7cbe64fSgw */ 2011b10bba72SGeorge Wilson if (error == 0 && !resize) { 2012b10bba72SGeorge Wilson /* 2013b10bba72SGeorge Wilson * If MULTI_VDEV_CRASH_DUMP is active, use the NOPARITY checksum 2014b10bba72SGeorge Wilson * function. Otherwise, use the old default -- OFF. 2015b10bba72SGeorge Wilson */ 2016b10bba72SGeorge Wilson checksum = spa_feature_is_active(spa, 2017b10bba72SGeorge Wilson SPA_FEATURE_MULTI_VDEV_CRASH_DUMP) ? ZIO_CHECKSUM_NOPARITY : 2018b10bba72SGeorge Wilson ZIO_CHECKSUM_OFF; 2019b10bba72SGeorge Wilson 2020e7cbe64fSgw VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2021e7cbe64fSgw VERIFY(nvlist_add_uint64(nv, 2022e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0); 2023e7cbe64fSgw VERIFY(nvlist_add_uint64(nv, 2024e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), 2025e7cbe64fSgw ZIO_COMPRESS_OFF) == 0); 2026e7cbe64fSgw VERIFY(nvlist_add_uint64(nv, 2027e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_CHECKSUM), 2028810e43b2SBill Pijewski checksum) == 0); 20298d265e66SGeorge Wilson if (version >= SPA_VERSION_DEDUP) { 20308d265e66SGeorge Wilson VERIFY(nvlist_add_uint64(nv, 20318d265e66SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), 20328d265e66SGeorge Wilson ZIO_CHECKSUM_OFF) == 0); 20338d265e66SGeorge Wilson } 2034e7cbe64fSgw 203592241e0bSTom Erickson error = zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL, 203692241e0bSTom Erickson nv, NULL); 2037e7cbe64fSgw nvlist_free(nv); 2038e7cbe64fSgw } 2039e7cbe64fSgw 2040e7cbe64fSgw /* Allocate the space for the dump */ 2041b10bba72SGeorge Wilson if (error == 0) 2042b10bba72SGeorge Wilson error = zvol_prealloc(zv); 2043e7cbe64fSgw return (error); 2044e7cbe64fSgw } 2045e7cbe64fSgw 2046e7cbe64fSgw static int 2047e7cbe64fSgw zvol_dumpify(zvol_state_t *zv) 2048e7cbe64fSgw { 2049e7cbe64fSgw int error = 0; 2050e7cbe64fSgw uint64_t dumpsize = 0; 2051e7cbe64fSgw dmu_tx_t *tx; 2052e7cbe64fSgw objset_t *os = zv->zv_objset; 2053e7cbe64fSgw 2054681d9761SEric Taylor if (zv->zv_flags & ZVOL_RDONLY) 2055be6fd75aSMatthew Ahrens return (SET_ERROR(EROFS)); 2056e7cbe64fSgw 2057e7cbe64fSgw if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 2058e7cbe64fSgw 8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) { 20594445fffbSMatthew Ahrens boolean_t resize = (dumpsize > 0); 2060e7cbe64fSgw 2061e7cbe64fSgw if ((error = zvol_dump_init(zv, resize)) != 0) { 2062e7cbe64fSgw (void) zvol_dump_fini(zv); 2063e7cbe64fSgw return (error); 2064e7cbe64fSgw } 2065e7cbe64fSgw } 2066e7cbe64fSgw 2067e7cbe64fSgw /* 2068e7cbe64fSgw * Build up our lba mapping. 2069e7cbe64fSgw */ 2070e7cbe64fSgw error = zvol_get_lbas(zv); 2071e7cbe64fSgw if (error) { 2072e7cbe64fSgw (void) zvol_dump_fini(zv); 2073e7cbe64fSgw return (error); 2074e7cbe64fSgw } 2075e7cbe64fSgw 2076e7cbe64fSgw tx = dmu_tx_create(os); 2077e7cbe64fSgw dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 2078e7cbe64fSgw error = dmu_tx_assign(tx, TXG_WAIT); 2079e7cbe64fSgw if (error) { 2080e7cbe64fSgw dmu_tx_abort(tx); 2081e7cbe64fSgw (void) zvol_dump_fini(zv); 2082e7cbe64fSgw return (error); 2083e7cbe64fSgw } 2084e7cbe64fSgw 2085e7cbe64fSgw zv->zv_flags |= ZVOL_DUMPIFIED; 2086e7cbe64fSgw error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1, 2087e7cbe64fSgw &zv->zv_volsize, tx); 2088e7cbe64fSgw dmu_tx_commit(tx); 2089e7cbe64fSgw 2090e7cbe64fSgw if (error) { 2091e7cbe64fSgw (void) zvol_dump_fini(zv); 2092e7cbe64fSgw return (error); 2093e7cbe64fSgw } 2094e7cbe64fSgw 2095e7cbe64fSgw txg_wait_synced(dmu_objset_pool(os), 0); 2096e7cbe64fSgw return (0); 2097e7cbe64fSgw } 2098e7cbe64fSgw 2099e7cbe64fSgw static int 2100e7cbe64fSgw zvol_dump_fini(zvol_state_t *zv) 2101e7cbe64fSgw { 2102e7cbe64fSgw dmu_tx_t *tx; 2103e7cbe64fSgw objset_t *os = zv->zv_objset; 2104e7cbe64fSgw nvlist_t *nv; 2105e7cbe64fSgw int error = 0; 2106afee20e4SGeorge Wilson uint64_t checksum, compress, refresrv, vbs, dedup; 21078d265e66SGeorge Wilson uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset)); 2108e7cbe64fSgw 2109b7e50089Smaybee /* 2110b7e50089Smaybee * Attempt to restore the zvol back to its pre-dumpified state. 2111b7e50089Smaybee * This is a best-effort attempt as it's possible that not all 2112b7e50089Smaybee * of these properties were initialized during the dumpify process 2113b7e50089Smaybee * (i.e. error during zvol_dump_init). 2114b7e50089Smaybee */ 2115b7e50089Smaybee 2116e7cbe64fSgw tx = dmu_tx_create(os); 2117e7cbe64fSgw dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 2118e7cbe64fSgw error = dmu_tx_assign(tx, TXG_WAIT); 2119e7cbe64fSgw if (error) { 2120e7cbe64fSgw dmu_tx_abort(tx); 2121e7cbe64fSgw return (error); 2122e7cbe64fSgw } 2123b7e50089Smaybee (void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx); 2124b7e50089Smaybee dmu_tx_commit(tx); 2125e7cbe64fSgw 2126e7cbe64fSgw (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2127e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum); 2128e7cbe64fSgw (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2129e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress); 2130e7cbe64fSgw (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2131e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv); 213288b7b0f2SMatthew Ahrens (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 213388b7b0f2SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs); 2134e7cbe64fSgw 2135e7cbe64fSgw VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2136e7cbe64fSgw (void) nvlist_add_uint64(nv, 2137e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum); 2138e7cbe64fSgw (void) nvlist_add_uint64(nv, 2139e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress); 2140e7cbe64fSgw (void) nvlist_add_uint64(nv, 2141e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv); 21428d265e66SGeorge Wilson if (version >= SPA_VERSION_DEDUP && 21438d265e66SGeorge Wilson zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 21448d265e66SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, &dedup) == 0) { 21458d265e66SGeorge Wilson (void) nvlist_add_uint64(nv, 21468d265e66SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), dedup); 21478d265e66SGeorge Wilson } 214892241e0bSTom Erickson (void) zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL, 214992241e0bSTom Erickson nv, NULL); 2150e7cbe64fSgw nvlist_free(nv); 2151e7cbe64fSgw 2152b7e50089Smaybee zvol_free_extents(zv); 2153b7e50089Smaybee zv->zv_flags &= ~ZVOL_DUMPIFIED; 2154b7e50089Smaybee (void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END); 2155681d9761SEric Taylor /* wait for dmu_free_long_range to actually free the blocks */ 2156681d9761SEric Taylor txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 2157681d9761SEric Taylor tx = dmu_tx_create(os); 2158681d9761SEric Taylor dmu_tx_hold_bonus(tx, ZVOL_OBJ); 2159681d9761SEric Taylor error = dmu_tx_assign(tx, TXG_WAIT); 2160681d9761SEric Taylor if (error) { 2161681d9761SEric Taylor dmu_tx_abort(tx); 2162681d9761SEric Taylor return (error); 2163681d9761SEric Taylor } 2164b24ab676SJeff Bonwick if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0) 2165b24ab676SJeff Bonwick zv->zv_volblocksize = vbs; 2166681d9761SEric Taylor dmu_tx_commit(tx); 2167b7e50089Smaybee 2168e7cbe64fSgw return (0); 2169e7cbe64fSgw } 2170