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. 277802d7bfSMatthew Ahrens * Copyright (c) 2012, 2014 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> 61e7cbe64fSgw25295 #include <sys/dmu_traverse.h> 62e7cbe64fSgw25295 #include <sys/dnode.h> 63e7cbe64fSgw25295 #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> 81e7cbe64fSgw25295 #include <sys/vdev_disk.h> 82e7cbe64fSgw25295 #include <sys/vdev_impl.h> 83810e43b2SBill Pijewski #include <sys/vdev_raidz.h> 84e7cbe64fSgw25295 #include <sys/zvol.h> 85e7cbe64fSgw25295 #include <sys/dumphdr.h> 861209a471SNeil Perrin #include <sys/zil_impl.h> 8780901aeaSGeorge Wilson #include <sys/dbuf.h> 88d78b796cSAndreas Jaekel #include <sys/zfs_events.h> 89810e43b2SBill Pijewski #include <sys/dmu_tx.h> 90810e43b2SBill Pijewski #include <sys/zfeature.h> 91810e43b2SBill Pijewski #include <sys/zio_checksum.h> 92fa9e4066Sahrens 93fa9e4066Sahrens #include "zfs_namecheck.h" 94fa9e4066Sahrens 95c99e4bdcSChris Kirby void *zfsdev_state; 96503ad85cSMatthew Ahrens static char *zvol_tag = "zvol_tag"; 97fa9e4066Sahrens 98e7cbe64fSgw25295 #define ZVOL_DUMPSIZE "dumpsize" 99e7cbe64fSgw25295 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 109e7cbe64fSgw25295 typedef struct zvol_extent { 11088b7b0f2SMatthew Ahrens list_node_t ze_node; 111e7cbe64fSgw25295 dva_t ze_dva; /* dva associated with this extent */ 11288b7b0f2SMatthew Ahrens uint64_t ze_nblks; /* number of blocks in extent */ 113e7cbe64fSgw25295 } zvol_extent_t; 114e7cbe64fSgw25295 115e7cbe64fSgw25295 /* 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 */ 13194d1a210STim Haley dmu_buf_t *zv_dbuf; /* bonus handle */ 132fa9e4066Sahrens } zvol_state_t; 133fa9e4066Sahrens 13467bd71c6Sperrin /* 135e7cbe64fSgw25295 * zvol specific flags 136e7cbe64fSgw25295 */ 137e7cbe64fSgw25295 #define ZVOL_RDONLY 0x1 138e7cbe64fSgw25295 #define ZVOL_DUMPIFIED 0x2 139c7f714e2SEric Taylor #define ZVOL_EXCL 0x4 140701f66c4SEric Taylor #define ZVOL_WCE 0x8 141e7cbe64fSgw25295 142e7cbe64fSgw25295 /* 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 15292241e0bSTom Erickson extern int zfs_set_prop_nvlist(const char *, zprop_source_t, 1534445fffbSMatthew Ahrens nvlist_t *, nvlist_t *); 154681d9761SEric Taylor static int zvol_remove_zv(zvol_state_t *); 155feb08c6bSbillm static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio); 156e7cbe64fSgw25295 static int zvol_dumpify(zvol_state_t *zv); 157e7cbe64fSgw25295 static int zvol_dump_fini(zvol_state_t *zv); 158e7cbe64fSgw25295 static int zvol_dump_init(zvol_state_t *zv, boolean_t resize); 15967bd71c6Sperrin 160fa9e4066Sahrens static void 161c61ea566SGeorge Wilson zvol_size_changed(zvol_state_t *zv, uint64_t volsize) 162fa9e4066Sahrens { 163c61ea566SGeorge Wilson dev_t dev = makedevice(ddi_driver_major(zfs_dip), zv->zv_minor); 164fa9e4066Sahrens 165c61ea566SGeorge Wilson zv->zv_volsize = volsize; 166fa9e4066Sahrens VERIFY(ddi_prop_update_int64(dev, zfs_dip, 167681d9761SEric Taylor "Size", volsize) == DDI_SUCCESS); 168fa9e4066Sahrens VERIFY(ddi_prop_update_int64(dev, zfs_dip, 169681d9761SEric Taylor "Nblocks", lbtodb(volsize)) == DDI_SUCCESS); 170e7cbe64fSgw25295 171e7cbe64fSgw25295 /* Notify specfs to invalidate the cached size */ 172e7cbe64fSgw25295 spec_size_invalidate(dev, VBLK); 173e7cbe64fSgw25295 spec_size_invalidate(dev, VCHR); 174fa9e4066Sahrens } 175fa9e4066Sahrens 176fa9e4066Sahrens int 177e9dbad6fSeschrock zvol_check_volsize(uint64_t volsize, uint64_t blocksize) 178fa9e4066Sahrens { 179e9dbad6fSeschrock if (volsize == 0) 180be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 181fa9e4066Sahrens 182e9dbad6fSeschrock if (volsize % blocksize != 0) 183be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 1845c5460e9Seschrock 185fa9e4066Sahrens #ifdef _ILP32 186e9dbad6fSeschrock if (volsize - 1 > SPEC_MAXOFFSET_T) 187be6fd75aSMatthew Ahrens return (SET_ERROR(EOVERFLOW)); 188fa9e4066Sahrens #endif 189fa9e4066Sahrens return (0); 190fa9e4066Sahrens } 191fa9e4066Sahrens 192fa9e4066Sahrens int 193e9dbad6fSeschrock zvol_check_volblocksize(uint64_t volblocksize) 194fa9e4066Sahrens { 195e9dbad6fSeschrock if (volblocksize < SPA_MINBLOCKSIZE || 196b5152584SMatthew Ahrens volblocksize > SPA_OLD_MAXBLOCKSIZE || 197e9dbad6fSeschrock !ISP2(volblocksize)) 198be6fd75aSMatthew Ahrens return (SET_ERROR(EDOM)); 199fa9e4066Sahrens 200fa9e4066Sahrens return (0); 201fa9e4066Sahrens } 202fa9e4066Sahrens 203fa9e4066Sahrens int 204a2eea2e1Sahrens zvol_get_stats(objset_t *os, nvlist_t *nv) 205fa9e4066Sahrens { 206fa9e4066Sahrens int error; 207fa9e4066Sahrens dmu_object_info_t doi; 208a2eea2e1Sahrens uint64_t val; 209fa9e4066Sahrens 210a2eea2e1Sahrens error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val); 211fa9e4066Sahrens if (error) 212fa9e4066Sahrens return (error); 213fa9e4066Sahrens 214a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val); 215a2eea2e1Sahrens 216fa9e4066Sahrens error = dmu_object_info(os, ZVOL_OBJ, &doi); 217fa9e4066Sahrens 218a2eea2e1Sahrens if (error == 0) { 219a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE, 220a2eea2e1Sahrens doi.doi_data_block_size); 221a2eea2e1Sahrens } 222fa9e4066Sahrens 223fa9e4066Sahrens return (error); 224fa9e4066Sahrens } 225fa9e4066Sahrens 226fa9e4066Sahrens static zvol_state_t * 227e9dbad6fSeschrock zvol_minor_lookup(const char *name) 228fa9e4066Sahrens { 229fa9e4066Sahrens minor_t minor; 230fa9e4066Sahrens zvol_state_t *zv; 231fa9e4066Sahrens 232c99e4bdcSChris Kirby ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 233fa9e4066Sahrens 234c99e4bdcSChris Kirby for (minor = 1; minor <= ZFSDEV_MAX_MINOR; minor++) { 235c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 236fa9e4066Sahrens if (zv == NULL) 237fa9e4066Sahrens continue; 238fa9e4066Sahrens if (strcmp(zv->zv_name, name) == 0) 239f80ce222SChris Kirby return (zv); 240fa9e4066Sahrens } 241fa9e4066Sahrens 242f80ce222SChris Kirby return (NULL); 243fa9e4066Sahrens } 244fa9e4066Sahrens 245e7cbe64fSgw25295 /* extent mapping arg */ 246e7cbe64fSgw25295 struct maparg { 24788b7b0f2SMatthew Ahrens zvol_state_t *ma_zv; 24888b7b0f2SMatthew Ahrens uint64_t ma_blks; 249e7cbe64fSgw25295 }; 250e7cbe64fSgw25295 251e7cbe64fSgw25295 /*ARGSUSED*/ 252e7cbe64fSgw25295 static int 2531b912ec7SGeorge Wilson zvol_map_block(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 2547802d7bfSMatthew Ahrens const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) 255e7cbe64fSgw25295 { 25688b7b0f2SMatthew Ahrens struct maparg *ma = arg; 25788b7b0f2SMatthew Ahrens zvol_extent_t *ze; 25888b7b0f2SMatthew Ahrens int bs = ma->ma_zv->zv_volblocksize; 259e7cbe64fSgw25295 260a2cdcdd2SPaul Dagnelie if (bp == NULL || BP_IS_HOLE(bp) || 26143466aaeSMax Grossman zb->zb_object != ZVOL_OBJ || zb->zb_level != 0) 262e7cbe64fSgw25295 return (0); 263e7cbe64fSgw25295 2645d7b4d43SMatthew Ahrens VERIFY(!BP_IS_EMBEDDED(bp)); 2655d7b4d43SMatthew Ahrens 26688b7b0f2SMatthew Ahrens VERIFY3U(ma->ma_blks, ==, zb->zb_blkid); 26788b7b0f2SMatthew Ahrens ma->ma_blks++; 26888b7b0f2SMatthew Ahrens 269e7cbe64fSgw25295 /* Abort immediately if we have encountered gang blocks */ 27088b7b0f2SMatthew Ahrens if (BP_IS_GANG(bp)) 271be6fd75aSMatthew Ahrens return (SET_ERROR(EFRAGS)); 272e7cbe64fSgw25295 273e7cbe64fSgw25295 /* 27488b7b0f2SMatthew Ahrens * See if the block is at the end of the previous extent. 275e7cbe64fSgw25295 */ 27688b7b0f2SMatthew Ahrens ze = list_tail(&ma->ma_zv->zv_extents); 27788b7b0f2SMatthew Ahrens if (ze && 27888b7b0f2SMatthew Ahrens DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) && 27988b7b0f2SMatthew Ahrens DVA_GET_OFFSET(BP_IDENTITY(bp)) == 28088b7b0f2SMatthew Ahrens DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) { 28188b7b0f2SMatthew Ahrens ze->ze_nblks++; 28288b7b0f2SMatthew Ahrens return (0); 28388b7b0f2SMatthew Ahrens } 28488b7b0f2SMatthew Ahrens 285e7cbe64fSgw25295 dprintf_bp(bp, "%s", "next blkptr:"); 28688b7b0f2SMatthew Ahrens 287e7cbe64fSgw25295 /* start a new extent */ 28888b7b0f2SMatthew Ahrens ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP); 28988b7b0f2SMatthew Ahrens ze->ze_dva = bp->blk_dva[0]; /* structure assignment */ 29088b7b0f2SMatthew Ahrens ze->ze_nblks = 1; 29188b7b0f2SMatthew Ahrens list_insert_tail(&ma->ma_zv->zv_extents, ze); 29288b7b0f2SMatthew Ahrens return (0); 293e7cbe64fSgw25295 } 29488b7b0f2SMatthew Ahrens 29588b7b0f2SMatthew Ahrens static void 29688b7b0f2SMatthew Ahrens zvol_free_extents(zvol_state_t *zv) 29788b7b0f2SMatthew Ahrens { 29888b7b0f2SMatthew Ahrens zvol_extent_t *ze; 29988b7b0f2SMatthew Ahrens 30088b7b0f2SMatthew Ahrens while (ze = list_head(&zv->zv_extents)) { 30188b7b0f2SMatthew Ahrens list_remove(&zv->zv_extents, ze); 30288b7b0f2SMatthew Ahrens kmem_free(ze, sizeof (zvol_extent_t)); 30388b7b0f2SMatthew Ahrens } 30488b7b0f2SMatthew Ahrens } 30588b7b0f2SMatthew Ahrens 30688b7b0f2SMatthew Ahrens static int 30788b7b0f2SMatthew Ahrens zvol_get_lbas(zvol_state_t *zv) 30888b7b0f2SMatthew Ahrens { 3093adc9019SEric Taylor objset_t *os = zv->zv_objset; 31088b7b0f2SMatthew Ahrens struct maparg ma; 31188b7b0f2SMatthew Ahrens int err; 31288b7b0f2SMatthew Ahrens 31388b7b0f2SMatthew Ahrens ma.ma_zv = zv; 31488b7b0f2SMatthew Ahrens ma.ma_blks = 0; 31588b7b0f2SMatthew Ahrens zvol_free_extents(zv); 31688b7b0f2SMatthew Ahrens 3173adc9019SEric Taylor /* commit any in-flight changes before traversing the dataset */ 3183adc9019SEric Taylor txg_wait_synced(dmu_objset_pool(os), 0); 3193adc9019SEric Taylor err = traverse_dataset(dmu_objset_ds(os), 0, 32088b7b0f2SMatthew Ahrens TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma); 32188b7b0f2SMatthew Ahrens if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) { 32288b7b0f2SMatthew Ahrens zvol_free_extents(zv); 32388b7b0f2SMatthew Ahrens return (err ? err : EIO); 32488b7b0f2SMatthew Ahrens } 32588b7b0f2SMatthew Ahrens 326e7cbe64fSgw25295 return (0); 327e7cbe64fSgw25295 } 328e7cbe64fSgw25295 329ecd6cf80Smarks /* ARGSUSED */ 330fa9e4066Sahrens void 331ecd6cf80Smarks zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 332fa9e4066Sahrens { 333da6c28aaSamw zfs_creat_t *zct = arg; 334da6c28aaSamw nvlist_t *nvprops = zct->zct_props; 335fa9e4066Sahrens int error; 336e9dbad6fSeschrock uint64_t volblocksize, volsize; 337fa9e4066Sahrens 338ecd6cf80Smarks VERIFY(nvlist_lookup_uint64(nvprops, 339e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0); 340ecd6cf80Smarks if (nvlist_lookup_uint64(nvprops, 341e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0) 342e9dbad6fSeschrock volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 343e9dbad6fSeschrock 344e9dbad6fSeschrock /* 345e7cbe64fSgw25295 * These properties must be removed from the list so the generic 346e9dbad6fSeschrock * property setting step won't apply to them. 347e9dbad6fSeschrock */ 348ecd6cf80Smarks VERIFY(nvlist_remove_all(nvprops, 349e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0); 350ecd6cf80Smarks (void) nvlist_remove_all(nvprops, 351e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE)); 352e9dbad6fSeschrock 353e9dbad6fSeschrock error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize, 354fa9e4066Sahrens DMU_OT_NONE, 0, tx); 355fa9e4066Sahrens ASSERT(error == 0); 356fa9e4066Sahrens 357fa9e4066Sahrens error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP, 358fa9e4066Sahrens DMU_OT_NONE, 0, tx); 359fa9e4066Sahrens ASSERT(error == 0); 360fa9e4066Sahrens 361e9dbad6fSeschrock error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx); 362fa9e4066Sahrens ASSERT(error == 0); 363fa9e4066Sahrens } 364fa9e4066Sahrens 365fa9e4066Sahrens /* 366b77b9231SDan McDonald * Replay a TX_TRUNCATE ZIL transaction if asked. TX_TRUNCATE is how we 367b77b9231SDan McDonald * implement DKIOCFREE/free-long-range. 368b77b9231SDan McDonald */ 369b77b9231SDan McDonald static int 370b77b9231SDan McDonald zvol_replay_truncate(zvol_state_t *zv, lr_truncate_t *lr, boolean_t byteswap) 371b77b9231SDan McDonald { 372b77b9231SDan McDonald uint64_t offset, length; 373b77b9231SDan McDonald 374b77b9231SDan McDonald if (byteswap) 375b77b9231SDan McDonald byteswap_uint64_array(lr, sizeof (*lr)); 376b77b9231SDan McDonald 377b77b9231SDan McDonald offset = lr->lr_offset; 378b77b9231SDan McDonald length = lr->lr_length; 379b77b9231SDan McDonald 380b77b9231SDan McDonald return (dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, offset, length)); 381b77b9231SDan McDonald } 382b77b9231SDan McDonald 383b77b9231SDan McDonald /* 38422ac5be4Sperrin * Replay a TX_WRITE ZIL transaction that didn't get committed 38522ac5be4Sperrin * after a system failure 38622ac5be4Sperrin */ 38722ac5be4Sperrin static int 38822ac5be4Sperrin zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap) 38922ac5be4Sperrin { 39022ac5be4Sperrin objset_t *os = zv->zv_objset; 39122ac5be4Sperrin char *data = (char *)(lr + 1); /* data follows lr_write_t */ 392b24ab676SJeff Bonwick uint64_t offset, length; 39322ac5be4Sperrin dmu_tx_t *tx; 39422ac5be4Sperrin int error; 39522ac5be4Sperrin 39622ac5be4Sperrin if (byteswap) 39722ac5be4Sperrin byteswap_uint64_array(lr, sizeof (*lr)); 39822ac5be4Sperrin 399b24ab676SJeff Bonwick offset = lr->lr_offset; 400b24ab676SJeff Bonwick length = lr->lr_length; 401b24ab676SJeff Bonwick 402b24ab676SJeff Bonwick /* If it's a dmu_sync() block, write the whole block */ 403b24ab676SJeff Bonwick if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) { 404b24ab676SJeff Bonwick uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr); 405b24ab676SJeff Bonwick if (length < blocksize) { 406b24ab676SJeff Bonwick offset -= offset % blocksize; 407b24ab676SJeff Bonwick length = blocksize; 408b24ab676SJeff Bonwick } 409b24ab676SJeff Bonwick } 410975c32a0SNeil Perrin 41122ac5be4Sperrin tx = dmu_tx_create(os); 412b24ab676SJeff Bonwick dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length); 4131209a471SNeil Perrin error = dmu_tx_assign(tx, TXG_WAIT); 41422ac5be4Sperrin if (error) { 41522ac5be4Sperrin dmu_tx_abort(tx); 41622ac5be4Sperrin } else { 417b24ab676SJeff Bonwick dmu_write(os, ZVOL_OBJ, offset, length, data, tx); 41822ac5be4Sperrin dmu_tx_commit(tx); 41922ac5be4Sperrin } 42022ac5be4Sperrin 42122ac5be4Sperrin return (error); 42222ac5be4Sperrin } 42322ac5be4Sperrin 42422ac5be4Sperrin /* ARGSUSED */ 42522ac5be4Sperrin static int 42622ac5be4Sperrin zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap) 42722ac5be4Sperrin { 428be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 42922ac5be4Sperrin } 43022ac5be4Sperrin 43122ac5be4Sperrin /* 43222ac5be4Sperrin * Callback vectors for replaying records. 433b77b9231SDan McDonald * Only TX_WRITE and TX_TRUNCATE are needed for zvol. 43422ac5be4Sperrin */ 43522ac5be4Sperrin zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = { 43622ac5be4Sperrin zvol_replay_err, /* 0 no such transaction type */ 43722ac5be4Sperrin zvol_replay_err, /* TX_CREATE */ 43822ac5be4Sperrin zvol_replay_err, /* TX_MKDIR */ 43922ac5be4Sperrin zvol_replay_err, /* TX_MKXATTR */ 44022ac5be4Sperrin zvol_replay_err, /* TX_SYMLINK */ 44122ac5be4Sperrin zvol_replay_err, /* TX_REMOVE */ 44222ac5be4Sperrin zvol_replay_err, /* TX_RMDIR */ 44322ac5be4Sperrin zvol_replay_err, /* TX_LINK */ 44422ac5be4Sperrin zvol_replay_err, /* TX_RENAME */ 44522ac5be4Sperrin zvol_replay_write, /* TX_WRITE */ 446b77b9231SDan McDonald zvol_replay_truncate, /* TX_TRUNCATE */ 44722ac5be4Sperrin zvol_replay_err, /* TX_SETATTR */ 44822ac5be4Sperrin zvol_replay_err, /* TX_ACL */ 449975c32a0SNeil Perrin zvol_replay_err, /* TX_CREATE_ACL */ 450975c32a0SNeil Perrin zvol_replay_err, /* TX_CREATE_ATTR */ 451975c32a0SNeil Perrin zvol_replay_err, /* TX_CREATE_ACL_ATTR */ 452975c32a0SNeil Perrin zvol_replay_err, /* TX_MKDIR_ACL */ 453975c32a0SNeil Perrin zvol_replay_err, /* TX_MKDIR_ATTR */ 454975c32a0SNeil Perrin zvol_replay_err, /* TX_MKDIR_ACL_ATTR */ 455975c32a0SNeil Perrin zvol_replay_err, /* TX_WRITE2 */ 45622ac5be4Sperrin }; 45722ac5be4Sperrin 458681d9761SEric Taylor int 459681d9761SEric Taylor zvol_name2minor(const char *name, minor_t *minor) 460681d9761SEric Taylor { 461681d9761SEric Taylor zvol_state_t *zv; 462681d9761SEric Taylor 463c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 464681d9761SEric Taylor zv = zvol_minor_lookup(name); 465681d9761SEric Taylor if (minor && zv) 466681d9761SEric Taylor *minor = zv->zv_minor; 467c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 468681d9761SEric Taylor return (zv ? 0 : -1); 469681d9761SEric Taylor } 470681d9761SEric Taylor 47122ac5be4Sperrin /* 472e7cbe64fSgw25295 * Create a minor node (plus a whole lot more) for the specified volume. 473fa9e4066Sahrens */ 474fa9e4066Sahrens int 475681d9761SEric Taylor zvol_create_minor(const char *name) 476fa9e4066Sahrens { 477c99e4bdcSChris Kirby zfs_soft_state_t *zs; 478fa9e4066Sahrens zvol_state_t *zv; 479fa9e4066Sahrens objset_t *os; 48067bd71c6Sperrin dmu_object_info_t doi; 481fa9e4066Sahrens minor_t minor = 0; 482fa9e4066Sahrens char chrbuf[30], blkbuf[30]; 483fa9e4066Sahrens int error; 484fa9e4066Sahrens 485c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 486fa9e4066Sahrens 4871195e687SMark J Musante if (zvol_minor_lookup(name) != NULL) { 488c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 489be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 490fa9e4066Sahrens } 491fa9e4066Sahrens 492503ad85cSMatthew Ahrens /* lie and say we're read-only */ 4936e0cbcaaSMatthew Ahrens error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, FTAG, &os); 494fa9e4066Sahrens 495fa9e4066Sahrens if (error) { 496c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 497fa9e4066Sahrens return (error); 498fa9e4066Sahrens } 499fa9e4066Sahrens 500c99e4bdcSChris Kirby if ((minor = zfsdev_minor_alloc()) == 0) { 5016e0cbcaaSMatthew Ahrens dmu_objset_disown(os, FTAG); 502c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 503be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 504fa9e4066Sahrens } 505fa9e4066Sahrens 506c99e4bdcSChris Kirby if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS) { 5076e0cbcaaSMatthew Ahrens dmu_objset_disown(os, FTAG); 508c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 509be6fd75aSMatthew Ahrens return (SET_ERROR(EAGAIN)); 510fa9e4066Sahrens } 511e9dbad6fSeschrock (void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME, 512e9dbad6fSeschrock (char *)name); 513fa9e4066Sahrens 514681d9761SEric Taylor (void) snprintf(chrbuf, sizeof (chrbuf), "%u,raw", minor); 515fa9e4066Sahrens 516fa9e4066Sahrens if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR, 517fa9e4066Sahrens minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 518c99e4bdcSChris Kirby ddi_soft_state_free(zfsdev_state, minor); 5196e0cbcaaSMatthew Ahrens dmu_objset_disown(os, FTAG); 520c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 521be6fd75aSMatthew Ahrens return (SET_ERROR(EAGAIN)); 522fa9e4066Sahrens } 523fa9e4066Sahrens 524681d9761SEric Taylor (void) snprintf(blkbuf, sizeof (blkbuf), "%u", minor); 525fa9e4066Sahrens 526fa9e4066Sahrens if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK, 527fa9e4066Sahrens minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 528fa9e4066Sahrens ddi_remove_minor_node(zfs_dip, chrbuf); 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 535c99e4bdcSChris Kirby zs = ddi_get_soft_state(zfsdev_state, minor); 536c99e4bdcSChris Kirby zs->zss_type = ZSST_ZVOL; 537c99e4bdcSChris Kirby zv = zs->zss_data = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP); 538681d9761SEric Taylor (void) strlcpy(zv->zv_name, name, MAXPATHLEN); 539fa9e4066Sahrens zv->zv_min_bs = DEV_BSHIFT; 540fa9e4066Sahrens zv->zv_minor = minor; 541fa9e4066Sahrens zv->zv_objset = os; 542f9af39baSGeorge Wilson if (dmu_objset_is_snapshot(os) || !spa_writeable(dmu_objset_spa(os))) 543681d9761SEric Taylor zv->zv_flags |= ZVOL_RDONLY; 544c2e6a7d6Sperrin mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL); 545c2e6a7d6Sperrin avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare, 546c2e6a7d6Sperrin sizeof (rl_t), offsetof(rl_t, r_node)); 54788b7b0f2SMatthew Ahrens list_create(&zv->zv_extents, sizeof (zvol_extent_t), 54888b7b0f2SMatthew Ahrens offsetof(zvol_extent_t, ze_node)); 54967bd71c6Sperrin /* get and cache the blocksize */ 55067bd71c6Sperrin error = dmu_object_info(os, ZVOL_OBJ, &doi); 55167bd71c6Sperrin ASSERT(error == 0); 55267bd71c6Sperrin zv->zv_volblocksize = doi.doi_data_block_size; 55322ac5be4Sperrin 554f9af39baSGeorge Wilson if (spa_writeable(dmu_objset_spa(os))) { 55555da60b9SMark J Musante if (zil_replay_disable) 55655da60b9SMark J Musante zil_destroy(dmu_objset_zil(os), B_FALSE); 55755da60b9SMark J Musante else 5581209a471SNeil Perrin zil_replay(os, zv, zvol_replay_vector); 559f9af39baSGeorge Wilson } 5606e0cbcaaSMatthew Ahrens dmu_objset_disown(os, FTAG); 561681d9761SEric Taylor zv->zv_objset = NULL; 562fa9e4066Sahrens 563fa9e4066Sahrens zvol_minors++; 564fa9e4066Sahrens 565c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 566fa9e4066Sahrens 567fa9e4066Sahrens return (0); 568fa9e4066Sahrens } 569fa9e4066Sahrens 570fa9e4066Sahrens /* 571fa9e4066Sahrens * Remove minor node for the specified volume. 572fa9e4066Sahrens */ 573681d9761SEric Taylor static int 574681d9761SEric Taylor zvol_remove_zv(zvol_state_t *zv) 575fa9e4066Sahrens { 576681d9761SEric Taylor char nmbuf[20]; 577c99e4bdcSChris Kirby minor_t minor = zv->zv_minor; 578fa9e4066Sahrens 579c99e4bdcSChris Kirby ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 580681d9761SEric Taylor if (zv->zv_total_opens != 0) 581be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 582fa9e4066Sahrens 583c99e4bdcSChris Kirby (void) snprintf(nmbuf, sizeof (nmbuf), "%u,raw", minor); 584681d9761SEric Taylor ddi_remove_minor_node(zfs_dip, nmbuf); 585fa9e4066Sahrens 586c99e4bdcSChris Kirby (void) snprintf(nmbuf, sizeof (nmbuf), "%u", minor); 587681d9761SEric Taylor ddi_remove_minor_node(zfs_dip, nmbuf); 588fa9e4066Sahrens 589c2e6a7d6Sperrin avl_destroy(&zv->zv_znode.z_range_avl); 590c2e6a7d6Sperrin mutex_destroy(&zv->zv_znode.z_range_lock); 591fa9e4066Sahrens 592c99e4bdcSChris Kirby kmem_free(zv, sizeof (zvol_state_t)); 593c99e4bdcSChris Kirby 594c99e4bdcSChris Kirby ddi_soft_state_free(zfsdev_state, minor); 595fa9e4066Sahrens 596fa9e4066Sahrens zvol_minors--; 597fa9e4066Sahrens return (0); 598fa9e4066Sahrens } 599fa9e4066Sahrens 600e7cbe64fSgw25295 int 601681d9761SEric Taylor zvol_remove_minor(const char *name) 602681d9761SEric Taylor { 603681d9761SEric Taylor zvol_state_t *zv; 604681d9761SEric Taylor int rc; 605681d9761SEric Taylor 606c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 607681d9761SEric Taylor if ((zv = zvol_minor_lookup(name)) == NULL) { 608c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 609be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 610681d9761SEric Taylor } 611681d9761SEric Taylor rc = zvol_remove_zv(zv); 612c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 613681d9761SEric Taylor return (rc); 614681d9761SEric Taylor } 615681d9761SEric Taylor 616681d9761SEric Taylor int 617681d9761SEric Taylor zvol_first_open(zvol_state_t *zv) 618681d9761SEric Taylor { 619681d9761SEric Taylor objset_t *os; 620681d9761SEric Taylor uint64_t volsize; 621681d9761SEric Taylor int error; 622681d9761SEric Taylor uint64_t readonly; 623681d9761SEric Taylor 624681d9761SEric Taylor /* lie and say we're read-only */ 625681d9761SEric Taylor error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, B_TRUE, 626681d9761SEric Taylor zvol_tag, &os); 627681d9761SEric Taylor if (error) 628681d9761SEric Taylor return (error); 629681d9761SEric Taylor 630c61ea566SGeorge Wilson zv->zv_objset = os; 631681d9761SEric Taylor error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize); 632681d9761SEric Taylor if (error) { 633681d9761SEric Taylor ASSERT(error == 0); 634681d9761SEric Taylor dmu_objset_disown(os, zvol_tag); 635681d9761SEric Taylor return (error); 636681d9761SEric Taylor } 637c61ea566SGeorge Wilson 63894d1a210STim Haley error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf); 63994d1a210STim Haley if (error) { 64094d1a210STim Haley dmu_objset_disown(os, zvol_tag); 64194d1a210STim Haley return (error); 64294d1a210STim Haley } 643c61ea566SGeorge Wilson 644c61ea566SGeorge Wilson zvol_size_changed(zv, volsize); 645681d9761SEric Taylor zv->zv_zilog = zil_open(os, zvol_get_data); 646681d9761SEric Taylor 647681d9761SEric Taylor VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &readonly, 648681d9761SEric Taylor NULL) == 0); 649f9af39baSGeorge Wilson if (readonly || dmu_objset_is_snapshot(os) || 650f9af39baSGeorge Wilson !spa_writeable(dmu_objset_spa(os))) 651681d9761SEric Taylor zv->zv_flags |= ZVOL_RDONLY; 652681d9761SEric Taylor else 653681d9761SEric Taylor zv->zv_flags &= ~ZVOL_RDONLY; 654681d9761SEric Taylor return (error); 655681d9761SEric Taylor } 656681d9761SEric Taylor 657681d9761SEric Taylor void 658681d9761SEric Taylor zvol_last_close(zvol_state_t *zv) 659681d9761SEric Taylor { 660681d9761SEric Taylor zil_close(zv->zv_zilog); 661681d9761SEric Taylor zv->zv_zilog = NULL; 6622e2c1355SMatthew Ahrens 66394d1a210STim Haley dmu_buf_rele(zv->zv_dbuf, zvol_tag); 66494d1a210STim Haley zv->zv_dbuf = NULL; 6652e2c1355SMatthew Ahrens 6662e2c1355SMatthew Ahrens /* 6672e2c1355SMatthew Ahrens * Evict cached data 6682e2c1355SMatthew Ahrens */ 6692e2c1355SMatthew Ahrens if (dsl_dataset_is_dirty(dmu_objset_ds(zv->zv_objset)) && 6702e2c1355SMatthew Ahrens !(zv->zv_flags & ZVOL_RDONLY)) 6712e2c1355SMatthew Ahrens txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 6723b2aab18SMatthew Ahrens dmu_objset_evict_dbufs(zv->zv_objset); 6732e2c1355SMatthew Ahrens 674681d9761SEric Taylor dmu_objset_disown(zv->zv_objset, zvol_tag); 675681d9761SEric Taylor zv->zv_objset = NULL; 676681d9761SEric Taylor } 677681d9761SEric Taylor 678681d9761SEric Taylor int 679e7cbe64fSgw25295 zvol_prealloc(zvol_state_t *zv) 680e7cbe64fSgw25295 { 681e7cbe64fSgw25295 objset_t *os = zv->zv_objset; 682e7cbe64fSgw25295 dmu_tx_t *tx; 683e7cbe64fSgw25295 uint64_t refd, avail, usedobjs, availobjs; 684e7cbe64fSgw25295 uint64_t resid = zv->zv_volsize; 685e7cbe64fSgw25295 uint64_t off = 0; 686e7cbe64fSgw25295 687e7cbe64fSgw25295 /* Check the space usage before attempting to allocate the space */ 688e7cbe64fSgw25295 dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs); 689e7cbe64fSgw25295 if (avail < zv->zv_volsize) 690be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 691e7cbe64fSgw25295 692e7cbe64fSgw25295 /* Free old extents if they exist */ 693e7cbe64fSgw25295 zvol_free_extents(zv); 694e7cbe64fSgw25295 695e7cbe64fSgw25295 while (resid != 0) { 696e7cbe64fSgw25295 int error; 697b5152584SMatthew Ahrens uint64_t bytes = MIN(resid, SPA_OLD_MAXBLOCKSIZE); 698e7cbe64fSgw25295 699e7cbe64fSgw25295 tx = dmu_tx_create(os); 700e7cbe64fSgw25295 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 701e7cbe64fSgw25295 error = dmu_tx_assign(tx, TXG_WAIT); 702e7cbe64fSgw25295 if (error) { 703e7cbe64fSgw25295 dmu_tx_abort(tx); 704cdb0ab79Smaybee (void) dmu_free_long_range(os, ZVOL_OBJ, 0, off); 705e7cbe64fSgw25295 return (error); 706e7cbe64fSgw25295 } 70782c9918fSTim Haley dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx); 708e7cbe64fSgw25295 dmu_tx_commit(tx); 709e7cbe64fSgw25295 off += bytes; 710e7cbe64fSgw25295 resid -= bytes; 711e7cbe64fSgw25295 } 712e7cbe64fSgw25295 txg_wait_synced(dmu_objset_pool(os), 0); 713e7cbe64fSgw25295 714e7cbe64fSgw25295 return (0); 715e7cbe64fSgw25295 } 716e7cbe64fSgw25295 7173b2aab18SMatthew Ahrens static int 718681d9761SEric Taylor zvol_update_volsize(objset_t *os, uint64_t volsize) 719e7cbe64fSgw25295 { 720e7cbe64fSgw25295 dmu_tx_t *tx; 721e7cbe64fSgw25295 int error; 722e7cbe64fSgw25295 723c99e4bdcSChris Kirby ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 724e7cbe64fSgw25295 725681d9761SEric Taylor tx = dmu_tx_create(os); 726e7cbe64fSgw25295 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 7274bb73804SMatthew Ahrens dmu_tx_mark_netfree(tx); 728e7cbe64fSgw25295 error = dmu_tx_assign(tx, TXG_WAIT); 729e7cbe64fSgw25295 if (error) { 730e7cbe64fSgw25295 dmu_tx_abort(tx); 731e7cbe64fSgw25295 return (error); 732e7cbe64fSgw25295 } 733e7cbe64fSgw25295 734681d9761SEric Taylor error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, 735e7cbe64fSgw25295 &volsize, tx); 736e7cbe64fSgw25295 dmu_tx_commit(tx); 737e7cbe64fSgw25295 738e7cbe64fSgw25295 if (error == 0) 739681d9761SEric Taylor error = dmu_free_long_range(os, 740cdb0ab79Smaybee ZVOL_OBJ, volsize, DMU_OBJECT_END); 741e7cbe64fSgw25295 return (error); 742e7cbe64fSgw25295 } 743e7cbe64fSgw25295 744681d9761SEric Taylor void 745681d9761SEric Taylor zvol_remove_minors(const char *name) 746681d9761SEric Taylor { 747681d9761SEric Taylor zvol_state_t *zv; 748681d9761SEric Taylor char *namebuf; 749681d9761SEric Taylor minor_t minor; 750681d9761SEric Taylor 751681d9761SEric Taylor namebuf = kmem_zalloc(strlen(name) + 2, KM_SLEEP); 752681d9761SEric Taylor (void) strncpy(namebuf, name, strlen(name)); 753681d9761SEric Taylor (void) strcat(namebuf, "/"); 754c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 755c99e4bdcSChris Kirby for (minor = 1; minor <= ZFSDEV_MAX_MINOR; minor++) { 756681d9761SEric Taylor 757c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 758681d9761SEric Taylor if (zv == NULL) 759681d9761SEric Taylor continue; 760681d9761SEric Taylor if (strncmp(namebuf, zv->zv_name, strlen(namebuf)) == 0) 761681d9761SEric Taylor (void) zvol_remove_zv(zv); 762681d9761SEric Taylor } 763681d9761SEric Taylor kmem_free(namebuf, strlen(name) + 2); 764681d9761SEric Taylor 765c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 766681d9761SEric Taylor } 767681d9761SEric Taylor 768c61ea566SGeorge Wilson static int 7693b2aab18SMatthew Ahrens zvol_update_live_volsize(zvol_state_t *zv, uint64_t volsize) 770fa9e4066Sahrens { 771e7cbe64fSgw25295 uint64_t old_volsize = 0ULL; 7723b2aab18SMatthew Ahrens int error = 0; 773fa9e4066Sahrens 774c61ea566SGeorge Wilson ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 775c61ea566SGeorge Wilson 776e7cbe64fSgw25295 /* 777e7cbe64fSgw25295 * Reinitialize the dump area to the new size. If we 778681d9761SEric Taylor * failed to resize the dump area then restore it back to 779c61ea566SGeorge Wilson * its original size. We must set the new volsize prior 780c61ea566SGeorge Wilson * to calling dumpvp_resize() to ensure that the devices' 781c61ea566SGeorge Wilson * size(9P) is not visible by the dump subsystem. 782e7cbe64fSgw25295 */ 783681d9761SEric Taylor old_volsize = zv->zv_volsize; 784c61ea566SGeorge Wilson zvol_size_changed(zv, volsize); 785c61ea566SGeorge Wilson 786c61ea566SGeorge Wilson if (zv->zv_flags & ZVOL_DUMPIFIED) { 787e7cbe64fSgw25295 if ((error = zvol_dumpify(zv)) != 0 || 788e7cbe64fSgw25295 (error = dumpvp_resize()) != 0) { 789c61ea566SGeorge Wilson int dumpify_error; 790c61ea566SGeorge Wilson 7913b2aab18SMatthew Ahrens (void) zvol_update_volsize(zv->zv_objset, old_volsize); 792c61ea566SGeorge Wilson zvol_size_changed(zv, old_volsize); 793c61ea566SGeorge Wilson dumpify_error = zvol_dumpify(zv); 794c61ea566SGeorge Wilson error = dumpify_error ? dumpify_error : error; 795fa9e4066Sahrens } 796fa9e4066Sahrens } 797fa9e4066Sahrens 798573ca77eSGeorge Wilson /* 799573ca77eSGeorge Wilson * Generate a LUN expansion event. 800573ca77eSGeorge Wilson */ 8013b2aab18SMatthew Ahrens if (error == 0) { 802573ca77eSGeorge Wilson sysevent_id_t eid; 803573ca77eSGeorge Wilson nvlist_t *attr; 804573ca77eSGeorge Wilson char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 805573ca77eSGeorge Wilson 806681d9761SEric Taylor (void) snprintf(physpath, MAXPATHLEN, "%s%u", ZVOL_PSEUDO_DEV, 807573ca77eSGeorge Wilson zv->zv_minor); 808573ca77eSGeorge Wilson 809573ca77eSGeorge Wilson VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 810573ca77eSGeorge Wilson VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 811573ca77eSGeorge Wilson 812573ca77eSGeorge Wilson (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 813573ca77eSGeorge Wilson ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 814573ca77eSGeorge Wilson 815573ca77eSGeorge Wilson nvlist_free(attr); 816573ca77eSGeorge Wilson kmem_free(physpath, MAXPATHLEN); 817573ca77eSGeorge Wilson } 818c61ea566SGeorge Wilson return (error); 819c61ea566SGeorge Wilson } 820573ca77eSGeorge Wilson 821c61ea566SGeorge Wilson int 822c61ea566SGeorge Wilson zvol_set_volsize(const char *name, uint64_t volsize) 823c61ea566SGeorge Wilson { 824c61ea566SGeorge Wilson zvol_state_t *zv = NULL; 825c61ea566SGeorge Wilson objset_t *os; 826c61ea566SGeorge Wilson int error; 827c61ea566SGeorge Wilson dmu_object_info_t doi; 828c61ea566SGeorge Wilson uint64_t readonly; 8293b2aab18SMatthew Ahrens boolean_t owned = B_FALSE; 8303b2aab18SMatthew Ahrens 8313b2aab18SMatthew Ahrens error = dsl_prop_get_integer(name, 8323b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_READONLY), &readonly, NULL); 8333b2aab18SMatthew Ahrens if (error != 0) 8343b2aab18SMatthew Ahrens return (error); 8353b2aab18SMatthew Ahrens if (readonly) 836be6fd75aSMatthew Ahrens return (SET_ERROR(EROFS)); 837c61ea566SGeorge Wilson 838c61ea566SGeorge Wilson mutex_enter(&zfsdev_state_lock); 839c61ea566SGeorge Wilson zv = zvol_minor_lookup(name); 8403b2aab18SMatthew Ahrens 8413b2aab18SMatthew Ahrens if (zv == NULL || zv->zv_objset == NULL) { 8423b2aab18SMatthew Ahrens if ((error = dmu_objset_own(name, DMU_OST_ZVOL, B_FALSE, 8433b2aab18SMatthew Ahrens FTAG, &os)) != 0) { 844c61ea566SGeorge Wilson mutex_exit(&zfsdev_state_lock); 845c61ea566SGeorge Wilson return (error); 846c61ea566SGeorge Wilson } 8473b2aab18SMatthew Ahrens owned = B_TRUE; 8483b2aab18SMatthew Ahrens if (zv != NULL) 8493b2aab18SMatthew Ahrens zv->zv_objset = os; 8503b2aab18SMatthew Ahrens } else { 8513b2aab18SMatthew Ahrens os = zv->zv_objset; 852c61ea566SGeorge Wilson } 853c61ea566SGeorge Wilson 8543b2aab18SMatthew Ahrens if ((error = dmu_object_info(os, ZVOL_OBJ, &doi)) != 0 || 8553b2aab18SMatthew Ahrens (error = zvol_check_volsize(volsize, doi.doi_data_block_size)) != 0) 8563b2aab18SMatthew Ahrens goto out; 8573b2aab18SMatthew Ahrens 8583b2aab18SMatthew Ahrens error = zvol_update_volsize(os, volsize); 8593b2aab18SMatthew Ahrens 8603b2aab18SMatthew Ahrens if (error == 0 && zv != NULL) 8613b2aab18SMatthew Ahrens error = zvol_update_live_volsize(zv, volsize); 862bb0ade09Sahrens out: 8633b2aab18SMatthew Ahrens if (owned) { 8643b2aab18SMatthew Ahrens dmu_objset_disown(os, FTAG); 8653b2aab18SMatthew Ahrens if (zv != NULL) 8663b2aab18SMatthew Ahrens zv->zv_objset = NULL; 8673b2aab18SMatthew Ahrens } 868c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 869fa9e4066Sahrens return (error); 870fa9e4066Sahrens } 871fa9e4066Sahrens 872fa9e4066Sahrens /*ARGSUSED*/ 873fa9e4066Sahrens int 874fa9e4066Sahrens zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr) 875fa9e4066Sahrens { 876fa9e4066Sahrens zvol_state_t *zv; 877681d9761SEric Taylor int err = 0; 878fa9e4066Sahrens 879c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 880fa9e4066Sahrens 881c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(getminor(*devp), ZSST_ZVOL); 882fa9e4066Sahrens if (zv == NULL) { 883c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 884be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 885fa9e4066Sahrens } 886fa9e4066Sahrens 887681d9761SEric Taylor if (zv->zv_total_opens == 0) 888681d9761SEric Taylor err = zvol_first_open(zv); 889681d9761SEric Taylor if (err) { 890c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 891681d9761SEric Taylor return (err); 892681d9761SEric Taylor } 893681d9761SEric Taylor if ((flag & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) { 894be6fd75aSMatthew Ahrens err = SET_ERROR(EROFS); 895681d9761SEric Taylor goto out; 896fa9e4066Sahrens } 897c7f714e2SEric Taylor if (zv->zv_flags & ZVOL_EXCL) { 898be6fd75aSMatthew Ahrens err = SET_ERROR(EBUSY); 899681d9761SEric Taylor goto out; 900c7f714e2SEric Taylor } 901c7f714e2SEric Taylor if (flag & FEXCL) { 902c7f714e2SEric Taylor if (zv->zv_total_opens != 0) { 903be6fd75aSMatthew Ahrens err = SET_ERROR(EBUSY); 904681d9761SEric Taylor goto out; 905c7f714e2SEric Taylor } 906c7f714e2SEric Taylor zv->zv_flags |= ZVOL_EXCL; 907c7f714e2SEric Taylor } 908fa9e4066Sahrens 909fa9e4066Sahrens if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) { 910fa9e4066Sahrens zv->zv_open_count[otyp]++; 911fa9e4066Sahrens zv->zv_total_opens++; 912fa9e4066Sahrens } 913c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 914fa9e4066Sahrens 915681d9761SEric Taylor return (err); 916681d9761SEric Taylor out: 917681d9761SEric Taylor if (zv->zv_total_opens == 0) 918681d9761SEric Taylor zvol_last_close(zv); 919c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 920681d9761SEric Taylor return (err); 921fa9e4066Sahrens } 922fa9e4066Sahrens 923fa9e4066Sahrens /*ARGSUSED*/ 924fa9e4066Sahrens int 925fa9e4066Sahrens zvol_close(dev_t dev, int flag, int otyp, cred_t *cr) 926fa9e4066Sahrens { 927fa9e4066Sahrens minor_t minor = getminor(dev); 928fa9e4066Sahrens zvol_state_t *zv; 929681d9761SEric Taylor int error = 0; 930fa9e4066Sahrens 931c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 932fa9e4066Sahrens 933c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 934fa9e4066Sahrens if (zv == NULL) { 935c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 936be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 937fa9e4066Sahrens } 938fa9e4066Sahrens 939c7f714e2SEric Taylor if (zv->zv_flags & ZVOL_EXCL) { 940c7f714e2SEric Taylor ASSERT(zv->zv_total_opens == 1); 941c7f714e2SEric Taylor zv->zv_flags &= ~ZVOL_EXCL; 942fa9e4066Sahrens } 943fa9e4066Sahrens 944fa9e4066Sahrens /* 945fa9e4066Sahrens * If the open count is zero, this is a spurious close. 946fa9e4066Sahrens * That indicates a bug in the kernel / DDI framework. 947fa9e4066Sahrens */ 948fa9e4066Sahrens ASSERT(zv->zv_open_count[otyp] != 0); 949fa9e4066Sahrens ASSERT(zv->zv_total_opens != 0); 950fa9e4066Sahrens 951fa9e4066Sahrens /* 952fa9e4066Sahrens * You may get multiple opens, but only one close. 953fa9e4066Sahrens */ 954fa9e4066Sahrens zv->zv_open_count[otyp]--; 955fa9e4066Sahrens zv->zv_total_opens--; 956fa9e4066Sahrens 957681d9761SEric Taylor if (zv->zv_total_opens == 0) 958681d9761SEric Taylor zvol_last_close(zv); 959fa9e4066Sahrens 960c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 961681d9761SEric Taylor return (error); 962fa9e4066Sahrens } 963fa9e4066Sahrens 964feb08c6bSbillm static void 965b24ab676SJeff Bonwick zvol_get_done(zgd_t *zgd, int error) 96667bd71c6Sperrin { 967b24ab676SJeff Bonwick if (zgd->zgd_db) 968b24ab676SJeff Bonwick dmu_buf_rele(zgd->zgd_db, zgd); 96967bd71c6Sperrin 970b24ab676SJeff Bonwick zfs_range_unlock(zgd->zgd_rl); 971b24ab676SJeff Bonwick 972b24ab676SJeff Bonwick if (error == 0 && zgd->zgd_bp) 97317f17c2dSbonwick zil_add_block(zgd->zgd_zilog, zgd->zgd_bp); 974b24ab676SJeff Bonwick 97567bd71c6Sperrin kmem_free(zgd, sizeof (zgd_t)); 97667bd71c6Sperrin } 97767bd71c6Sperrin 97867bd71c6Sperrin /* 97967bd71c6Sperrin * Get data to generate a TX_WRITE intent log record. 98067bd71c6Sperrin */ 981feb08c6bSbillm static int 98267bd71c6Sperrin zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 98367bd71c6Sperrin { 98467bd71c6Sperrin zvol_state_t *zv = arg; 98567bd71c6Sperrin objset_t *os = zv->zv_objset; 986b24ab676SJeff Bonwick uint64_t object = ZVOL_OBJ; 987b24ab676SJeff Bonwick uint64_t offset = lr->lr_offset; 988b24ab676SJeff Bonwick uint64_t size = lr->lr_length; /* length of user data */ 989b24ab676SJeff Bonwick blkptr_t *bp = &lr->lr_blkptr; 99067bd71c6Sperrin dmu_buf_t *db; 99167bd71c6Sperrin zgd_t *zgd; 99267bd71c6Sperrin int error; 99367bd71c6Sperrin 994b24ab676SJeff Bonwick ASSERT(zio != NULL); 995b24ab676SJeff Bonwick ASSERT(size != 0); 996b24ab676SJeff Bonwick 997b24ab676SJeff Bonwick zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP); 998b24ab676SJeff Bonwick zgd->zgd_zilog = zv->zv_zilog; 999b24ab676SJeff Bonwick zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER); 1000feb08c6bSbillm 1001c2e6a7d6Sperrin /* 1002c2e6a7d6Sperrin * Write records come in two flavors: immediate and indirect. 1003c2e6a7d6Sperrin * For small writes it's cheaper to store the data with the 1004c2e6a7d6Sperrin * log record (immediate); for large writes it's cheaper to 1005c2e6a7d6Sperrin * sync the data and get a pointer to it (indirect) so that 1006c2e6a7d6Sperrin * we don't have to write the data twice. 1007c2e6a7d6Sperrin */ 1008b24ab676SJeff Bonwick if (buf != NULL) { /* immediate write */ 1009b24ab676SJeff Bonwick error = dmu_read(os, object, offset, size, buf, 1010b24ab676SJeff Bonwick DMU_READ_NO_PREFETCH); 1011b24ab676SJeff Bonwick } else { 1012b24ab676SJeff Bonwick size = zv->zv_volblocksize; 1013b24ab676SJeff Bonwick offset = P2ALIGN(offset, size); 101447cb52daSJeff Bonwick error = dmu_buf_hold(os, object, offset, zgd, &db, 101547cb52daSJeff Bonwick DMU_READ_NO_PREFETCH); 1016975c32a0SNeil Perrin if (error == 0) { 101780901aeaSGeorge Wilson blkptr_t *obp = dmu_buf_get_blkptr(db); 101880901aeaSGeorge Wilson if (obp) { 101980901aeaSGeorge Wilson ASSERT(BP_IS_HOLE(bp)); 102080901aeaSGeorge Wilson *bp = *obp; 102180901aeaSGeorge Wilson } 102280901aeaSGeorge Wilson 1023b24ab676SJeff Bonwick zgd->zgd_db = db; 1024b24ab676SJeff Bonwick zgd->zgd_bp = bp; 1025b24ab676SJeff Bonwick 1026b24ab676SJeff Bonwick ASSERT(db->db_offset == offset); 1027b24ab676SJeff Bonwick ASSERT(db->db_size == size); 1028b24ab676SJeff Bonwick 1029b24ab676SJeff Bonwick error = dmu_sync(zio, lr->lr_common.lrc_txg, 1030b24ab676SJeff Bonwick zvol_get_done, zgd); 1031b24ab676SJeff Bonwick 1032b24ab676SJeff Bonwick if (error == 0) 1033b24ab676SJeff Bonwick return (0); 1034b24ab676SJeff Bonwick } 1035975c32a0SNeil Perrin } 1036975c32a0SNeil Perrin 1037b24ab676SJeff Bonwick zvol_get_done(zgd, error); 1038b24ab676SJeff Bonwick 103967bd71c6Sperrin return (error); 104067bd71c6Sperrin } 104167bd71c6Sperrin 1042a24e15ceSperrin /* 1043a24e15ceSperrin * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions. 104422ac5be4Sperrin * 104522ac5be4Sperrin * We store data in the log buffers if it's small enough. 104667bd71c6Sperrin * Otherwise we will later flush the data out via dmu_sync(). 104722ac5be4Sperrin */ 104867bd71c6Sperrin ssize_t zvol_immediate_write_sz = 32768; 104922ac5be4Sperrin 1050feb08c6bSbillm static void 1051510b6c0eSNeil Perrin zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid, 1052510b6c0eSNeil Perrin boolean_t sync) 105322ac5be4Sperrin { 1054feb08c6bSbillm uint32_t blocksize = zv->zv_volblocksize; 10551209a471SNeil Perrin zilog_t *zilog = zv->zv_zilog; 1056510b6c0eSNeil Perrin boolean_t slogging; 1057e09fa4daSNeil Perrin ssize_t immediate_write_sz; 1058510b6c0eSNeil Perrin 1059b24ab676SJeff Bonwick if (zil_replaying(zilog, tx)) 10601209a471SNeil Perrin return; 10611209a471SNeil Perrin 1062e09fa4daSNeil Perrin immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT) 1063e09fa4daSNeil Perrin ? 0 : zvol_immediate_write_sz; 1064e09fa4daSNeil Perrin 1065e09fa4daSNeil Perrin slogging = spa_has_slogs(zilog->zl_spa) && 1066e09fa4daSNeil Perrin (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY); 1067feb08c6bSbillm 1068510b6c0eSNeil Perrin while (resid) { 1069510b6c0eSNeil Perrin itx_t *itx; 1070510b6c0eSNeil Perrin lr_write_t *lr; 1071510b6c0eSNeil Perrin ssize_t len; 1072510b6c0eSNeil Perrin itx_wr_state_t write_state; 1073510b6c0eSNeil Perrin 1074510b6c0eSNeil Perrin /* 1075510b6c0eSNeil Perrin * Unlike zfs_log_write() we can be called with 1076510b6c0eSNeil Perrin * upto DMU_MAX_ACCESS/2 (5MB) writes. 1077510b6c0eSNeil Perrin */ 1078e09fa4daSNeil Perrin if (blocksize > immediate_write_sz && !slogging && 1079510b6c0eSNeil Perrin resid >= blocksize && off % blocksize == 0) { 1080510b6c0eSNeil Perrin write_state = WR_INDIRECT; /* uses dmu_sync */ 1081510b6c0eSNeil Perrin len = blocksize; 1082510b6c0eSNeil Perrin } else if (sync) { 1083510b6c0eSNeil Perrin write_state = WR_COPIED; 1084510b6c0eSNeil Perrin len = MIN(ZIL_MAX_LOG_DATA, resid); 1085510b6c0eSNeil Perrin } else { 1086510b6c0eSNeil Perrin write_state = WR_NEED_COPY; 1087510b6c0eSNeil Perrin len = MIN(ZIL_MAX_LOG_DATA, resid); 1088510b6c0eSNeil Perrin } 1089510b6c0eSNeil Perrin 1090510b6c0eSNeil Perrin itx = zil_itx_create(TX_WRITE, sizeof (*lr) + 1091510b6c0eSNeil Perrin (write_state == WR_COPIED ? len : 0)); 109222ac5be4Sperrin lr = (lr_write_t *)&itx->itx_lr; 1093510b6c0eSNeil Perrin if (write_state == WR_COPIED && dmu_read(zv->zv_objset, 10947bfdf011SNeil Perrin ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) { 1095b24ab676SJeff Bonwick zil_itx_destroy(itx); 1096510b6c0eSNeil Perrin itx = zil_itx_create(TX_WRITE, sizeof (*lr)); 1097510b6c0eSNeil Perrin lr = (lr_write_t *)&itx->itx_lr; 1098510b6c0eSNeil Perrin write_state = WR_NEED_COPY; 1099510b6c0eSNeil Perrin } 1100510b6c0eSNeil Perrin 1101510b6c0eSNeil Perrin itx->itx_wr_state = write_state; 1102510b6c0eSNeil Perrin if (write_state == WR_NEED_COPY) 1103510b6c0eSNeil Perrin itx->itx_sod += len; 110422ac5be4Sperrin lr->lr_foid = ZVOL_OBJ; 110522ac5be4Sperrin lr->lr_offset = off; 1106510b6c0eSNeil Perrin lr->lr_length = len; 1107b24ab676SJeff Bonwick lr->lr_blkoff = 0; 110822ac5be4Sperrin BP_ZERO(&lr->lr_blkptr); 1109feb08c6bSbillm 1110510b6c0eSNeil Perrin itx->itx_private = zv; 1111510b6c0eSNeil Perrin itx->itx_sync = sync; 1112510b6c0eSNeil Perrin 11135002558fSNeil Perrin zil_itx_assign(zilog, itx, tx); 1114510b6c0eSNeil Perrin 1115d78b796cSAndreas Jaekel rw_enter(&rz_zev_rwlock, RW_READER); 1116d78b796cSAndreas Jaekel if (rz_zev_callbacks && rz_zev_callbacks->rz_zev_zvol_write) 1117d78b796cSAndreas Jaekel rz_zev_callbacks->rz_zev_zvol_write(zv->zv_name, 1118*e206ace3SAndreas Jaekel zv->zv_objset, tx, off, len); 1119d78b796cSAndreas Jaekel rw_exit(&rz_zev_rwlock); 1120d78b796cSAndreas Jaekel 1121510b6c0eSNeil Perrin off += len; 1122510b6c0eSNeil Perrin resid -= len; 1123a24e15ceSperrin } 112422ac5be4Sperrin } 112522ac5be4Sperrin 112688b7b0f2SMatthew Ahrens static int 1127810e43b2SBill Pijewski zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t origoffset, 1128810e43b2SBill Pijewski uint64_t size, boolean_t doread, boolean_t isdump) 1129e7cbe64fSgw25295 { 1130e7cbe64fSgw25295 vdev_disk_t *dvd; 1131e7cbe64fSgw25295 int c; 1132e7cbe64fSgw25295 int numerrors = 0; 1133e7cbe64fSgw25295 1134810e43b2SBill Pijewski if (vd->vdev_ops == &vdev_mirror_ops || 113521ecdf64SLin Ling vd->vdev_ops == &vdev_replacing_ops || 1136810e43b2SBill Pijewski vd->vdev_ops == &vdev_spare_ops) { 1137810e43b2SBill Pijewski for (c = 0; c < vd->vdev_children; c++) { 113888b7b0f2SMatthew Ahrens int err = zvol_dumpio_vdev(vd->vdev_child[c], 1139810e43b2SBill Pijewski addr, offset, origoffset, size, doread, isdump); 114088b7b0f2SMatthew Ahrens if (err != 0) { 1141e7cbe64fSgw25295 numerrors++; 114288b7b0f2SMatthew Ahrens } else if (doread) { 1143e7cbe64fSgw25295 break; 1144e7cbe64fSgw25295 } 1145e7cbe64fSgw25295 } 1146810e43b2SBill Pijewski } 1147e7cbe64fSgw25295 1148810e43b2SBill Pijewski if (!vd->vdev_ops->vdev_op_leaf && vd->vdev_ops != &vdev_raidz_ops) 1149e7cbe64fSgw25295 return (numerrors < vd->vdev_children ? 0 : EIO); 1150e7cbe64fSgw25295 1151dc0bb255SEric Taylor if (doread && !vdev_readable(vd)) 1152be6fd75aSMatthew Ahrens return (SET_ERROR(EIO)); 1153dc0bb255SEric Taylor else if (!doread && !vdev_writeable(vd)) 1154be6fd75aSMatthew Ahrens return (SET_ERROR(EIO)); 1155e7cbe64fSgw25295 1156810e43b2SBill Pijewski if (vd->vdev_ops == &vdev_raidz_ops) { 1157810e43b2SBill Pijewski return (vdev_raidz_physio(vd, 1158810e43b2SBill Pijewski addr, size, offset, origoffset, doread, isdump)); 1159810e43b2SBill Pijewski } 1160810e43b2SBill Pijewski 1161e7cbe64fSgw25295 offset += VDEV_LABEL_START_SIZE; 1162e7cbe64fSgw25295 1163e7cbe64fSgw25295 if (ddi_in_panic() || isdump) { 116488b7b0f2SMatthew Ahrens ASSERT(!doread); 116588b7b0f2SMatthew Ahrens if (doread) 1166be6fd75aSMatthew Ahrens return (SET_ERROR(EIO)); 1167810e43b2SBill Pijewski dvd = vd->vdev_tsd; 1168810e43b2SBill Pijewski ASSERT3P(dvd, !=, NULL); 1169e7cbe64fSgw25295 return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset), 1170e7cbe64fSgw25295 lbtodb(size))); 1171e7cbe64fSgw25295 } else { 1172810e43b2SBill Pijewski dvd = vd->vdev_tsd; 1173810e43b2SBill Pijewski ASSERT3P(dvd, !=, NULL); 1174810e43b2SBill Pijewski return (vdev_disk_ldi_physio(dvd->vd_lh, addr, size, 1175810e43b2SBill Pijewski offset, doread ? B_READ : B_WRITE)); 1176e7cbe64fSgw25295 } 1177e7cbe64fSgw25295 } 1178e7cbe64fSgw25295 117988b7b0f2SMatthew Ahrens static int 118088b7b0f2SMatthew Ahrens zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size, 118188b7b0f2SMatthew Ahrens boolean_t doread, boolean_t isdump) 1182e7cbe64fSgw25295 { 1183e7cbe64fSgw25295 vdev_t *vd; 1184e7cbe64fSgw25295 int error; 118588b7b0f2SMatthew Ahrens zvol_extent_t *ze; 1186e7cbe64fSgw25295 spa_t *spa = dmu_objset_spa(zv->zv_objset); 1187e7cbe64fSgw25295 118888b7b0f2SMatthew Ahrens /* Must be sector aligned, and not stradle a block boundary. */ 118988b7b0f2SMatthew Ahrens if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) || 119088b7b0f2SMatthew Ahrens P2BOUNDARY(offset, size, zv->zv_volblocksize)) { 1191be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 119288b7b0f2SMatthew Ahrens } 1193e7cbe64fSgw25295 ASSERT(size <= zv->zv_volblocksize); 1194e7cbe64fSgw25295 119588b7b0f2SMatthew Ahrens /* Locate the extent this belongs to */ 119688b7b0f2SMatthew Ahrens ze = list_head(&zv->zv_extents); 119788b7b0f2SMatthew Ahrens while (offset >= ze->ze_nblks * zv->zv_volblocksize) { 119888b7b0f2SMatthew Ahrens offset -= ze->ze_nblks * zv->zv_volblocksize; 119988b7b0f2SMatthew Ahrens ze = list_next(&zv->zv_extents, ze); 120088b7b0f2SMatthew Ahrens } 120124cc0e1cSGeorge Wilson 12023b2aab18SMatthew Ahrens if (ze == NULL) 1203be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 12043b2aab18SMatthew Ahrens 120524cc0e1cSGeorge Wilson if (!ddi_in_panic()) 1206e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 120724cc0e1cSGeorge Wilson 120888b7b0f2SMatthew Ahrens vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva)); 120988b7b0f2SMatthew Ahrens offset += DVA_GET_OFFSET(&ze->ze_dva); 1210810e43b2SBill Pijewski error = zvol_dumpio_vdev(vd, addr, offset, DVA_GET_OFFSET(&ze->ze_dva), 1211810e43b2SBill Pijewski size, doread, isdump); 121224cc0e1cSGeorge Wilson 121324cc0e1cSGeorge Wilson if (!ddi_in_panic()) 1214e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 121524cc0e1cSGeorge Wilson 1216e7cbe64fSgw25295 return (error); 1217e7cbe64fSgw25295 } 1218e7cbe64fSgw25295 1219e7cbe64fSgw25295 int 1220fa9e4066Sahrens zvol_strategy(buf_t *bp) 1221fa9e4066Sahrens { 1222c99e4bdcSChris Kirby zfs_soft_state_t *zs = NULL; 1223c99e4bdcSChris Kirby zvol_state_t *zv; 1224fa9e4066Sahrens uint64_t off, volsize; 122588b7b0f2SMatthew Ahrens size_t resid; 1226fa9e4066Sahrens char *addr; 122722ac5be4Sperrin objset_t *os; 1228c2e6a7d6Sperrin rl_t *rl; 1229fa9e4066Sahrens int error = 0; 123088b7b0f2SMatthew Ahrens boolean_t doread = bp->b_flags & B_READ; 1231810e43b2SBill Pijewski boolean_t is_dumpified; 1232510b6c0eSNeil Perrin boolean_t sync; 1233fa9e4066Sahrens 1234c99e4bdcSChris Kirby if (getminor(bp->b_edev) == 0) { 1235be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 1236c99e4bdcSChris Kirby } else { 1237c99e4bdcSChris Kirby zs = ddi_get_soft_state(zfsdev_state, getminor(bp->b_edev)); 1238c99e4bdcSChris Kirby if (zs == NULL) 1239be6fd75aSMatthew Ahrens error = SET_ERROR(ENXIO); 1240c99e4bdcSChris Kirby else if (zs->zss_type != ZSST_ZVOL) 1241be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 1242c99e4bdcSChris Kirby } 1243c99e4bdcSChris Kirby 1244c99e4bdcSChris Kirby if (error) { 1245c99e4bdcSChris Kirby bioerror(bp, error); 1246fa9e4066Sahrens biodone(bp); 1247fa9e4066Sahrens return (0); 1248fa9e4066Sahrens } 1249fa9e4066Sahrens 1250c99e4bdcSChris Kirby zv = zs->zss_data; 1251fa9e4066Sahrens 1252681d9761SEric Taylor if (!(bp->b_flags & B_READ) && (zv->zv_flags & ZVOL_RDONLY)) { 1253fa9e4066Sahrens bioerror(bp, EROFS); 1254fa9e4066Sahrens biodone(bp); 1255fa9e4066Sahrens return (0); 1256fa9e4066Sahrens } 1257fa9e4066Sahrens 1258fa9e4066Sahrens off = ldbtob(bp->b_blkno); 1259fa9e4066Sahrens volsize = zv->zv_volsize; 1260fa9e4066Sahrens 126122ac5be4Sperrin os = zv->zv_objset; 126222ac5be4Sperrin ASSERT(os != NULL); 1263fa9e4066Sahrens 1264fa9e4066Sahrens bp_mapin(bp); 1265fa9e4066Sahrens addr = bp->b_un.b_addr; 1266fa9e4066Sahrens resid = bp->b_bcount; 1267fa9e4066Sahrens 126888b7b0f2SMatthew Ahrens if (resid > 0 && (off < 0 || off >= volsize)) { 126988b7b0f2SMatthew Ahrens bioerror(bp, EIO); 127088b7b0f2SMatthew Ahrens biodone(bp); 127188b7b0f2SMatthew Ahrens return (0); 127288b7b0f2SMatthew Ahrens } 127373ec3d9cSgw25295 1274810e43b2SBill Pijewski is_dumpified = zv->zv_flags & ZVOL_DUMPIFIED; 127555da60b9SMark J Musante sync = ((!(bp->b_flags & B_ASYNC) && 127655da60b9SMark J Musante !(zv->zv_flags & ZVOL_WCE)) || 127755da60b9SMark J Musante (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)) && 1278810e43b2SBill Pijewski !doread && !is_dumpified; 1279510b6c0eSNeil Perrin 1280a24e15ceSperrin /* 1281a24e15ceSperrin * There must be no buffer changes when doing a dmu_sync() because 1282a24e15ceSperrin * we can't change the data whilst calculating the checksum. 1283a24e15ceSperrin */ 1284c2e6a7d6Sperrin rl = zfs_range_lock(&zv->zv_znode, off, resid, 128588b7b0f2SMatthew Ahrens doread ? RL_READER : RL_WRITER); 1286e7cbe64fSgw25295 1287fa9e4066Sahrens while (resid != 0 && off < volsize) { 128888b7b0f2SMatthew Ahrens size_t size = MIN(resid, zvol_maxphys); 1289810e43b2SBill Pijewski if (is_dumpified) { 1290e7cbe64fSgw25295 size = MIN(size, P2END(off, zv->zv_volblocksize) - off); 129188b7b0f2SMatthew Ahrens error = zvol_dumpio(zv, addr, off, size, 129288b7b0f2SMatthew Ahrens doread, B_FALSE); 129388b7b0f2SMatthew Ahrens } else if (doread) { 12947bfdf011SNeil Perrin error = dmu_read(os, ZVOL_OBJ, off, size, addr, 12957bfdf011SNeil Perrin DMU_READ_PREFETCH); 1296fa9e4066Sahrens } else { 129722ac5be4Sperrin dmu_tx_t *tx = dmu_tx_create(os); 1298fa9e4066Sahrens dmu_tx_hold_write(tx, ZVOL_OBJ, off, size); 1299fa9e4066Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 1300fa9e4066Sahrens if (error) { 1301fa9e4066Sahrens dmu_tx_abort(tx); 1302fa9e4066Sahrens } else { 130322ac5be4Sperrin dmu_write(os, ZVOL_OBJ, off, size, addr, tx); 1304510b6c0eSNeil Perrin zvol_log_write(zv, tx, off, size, sync); 1305fa9e4066Sahrens dmu_tx_commit(tx); 1306fa9e4066Sahrens } 1307fa9e4066Sahrens } 1308b87f3af3Sperrin if (error) { 1309b87f3af3Sperrin /* convert checksum errors into IO errors */ 1310b87f3af3Sperrin if (error == ECKSUM) 1311be6fd75aSMatthew Ahrens error = SET_ERROR(EIO); 1312fa9e4066Sahrens break; 1313b87f3af3Sperrin } 1314fa9e4066Sahrens off += size; 1315fa9e4066Sahrens addr += size; 1316fa9e4066Sahrens resid -= size; 1317fa9e4066Sahrens } 1318c2e6a7d6Sperrin zfs_range_unlock(rl); 1319fa9e4066Sahrens 1320fa9e4066Sahrens if ((bp->b_resid = resid) == bp->b_bcount) 1321fa9e4066Sahrens bioerror(bp, off > volsize ? EINVAL : error); 1322fa9e4066Sahrens 1323510b6c0eSNeil Perrin if (sync) 13245002558fSNeil Perrin zil_commit(zv->zv_zilog, ZVOL_OBJ); 1325feb08c6bSbillm biodone(bp); 132622ac5be4Sperrin 1327fa9e4066Sahrens return (0); 1328fa9e4066Sahrens } 1329fa9e4066Sahrens 133067bd71c6Sperrin /* 133167bd71c6Sperrin * Set the buffer count to the zvol maximum transfer. 133267bd71c6Sperrin * Using our own routine instead of the default minphys() 133367bd71c6Sperrin * means that for larger writes we write bigger buffers on X86 133467bd71c6Sperrin * (128K instead of 56K) and flush the disk write cache less often 133567bd71c6Sperrin * (every zvol_maxphys - currently 1MB) instead of minphys (currently 133667bd71c6Sperrin * 56K on X86 and 128K on sparc). 133767bd71c6Sperrin */ 133867bd71c6Sperrin void 133967bd71c6Sperrin zvol_minphys(struct buf *bp) 134067bd71c6Sperrin { 134167bd71c6Sperrin if (bp->b_bcount > zvol_maxphys) 134267bd71c6Sperrin bp->b_bcount = zvol_maxphys; 134367bd71c6Sperrin } 134467bd71c6Sperrin 1345e7cbe64fSgw25295 int 1346e7cbe64fSgw25295 zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks) 1347e7cbe64fSgw25295 { 1348e7cbe64fSgw25295 minor_t minor = getminor(dev); 1349e7cbe64fSgw25295 zvol_state_t *zv; 1350e7cbe64fSgw25295 int error = 0; 1351e7cbe64fSgw25295 uint64_t size; 1352e7cbe64fSgw25295 uint64_t boff; 1353e7cbe64fSgw25295 uint64_t resid; 1354e7cbe64fSgw25295 1355c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1356e7cbe64fSgw25295 if (zv == NULL) 1357be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 1358e7cbe64fSgw25295 13593b2aab18SMatthew Ahrens if ((zv->zv_flags & ZVOL_DUMPIFIED) == 0) 1360be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 13613b2aab18SMatthew Ahrens 1362e7cbe64fSgw25295 boff = ldbtob(blkno); 1363e7cbe64fSgw25295 resid = ldbtob(nblocks); 1364e7cbe64fSgw25295 136588b7b0f2SMatthew Ahrens VERIFY3U(boff + resid, <=, zv->zv_volsize); 136688b7b0f2SMatthew Ahrens 136788b7b0f2SMatthew Ahrens while (resid) { 136888b7b0f2SMatthew Ahrens size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff); 136988b7b0f2SMatthew Ahrens error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE); 1370e7cbe64fSgw25295 if (error) 1371e7cbe64fSgw25295 break; 1372e7cbe64fSgw25295 boff += size; 1373e7cbe64fSgw25295 addr += size; 1374e7cbe64fSgw25295 resid -= size; 1375e7cbe64fSgw25295 } 1376e7cbe64fSgw25295 1377e7cbe64fSgw25295 return (error); 1378e7cbe64fSgw25295 } 1379e7cbe64fSgw25295 1380fa9e4066Sahrens /*ARGSUSED*/ 1381fa9e4066Sahrens int 1382feb08c6bSbillm zvol_read(dev_t dev, uio_t *uio, cred_t *cr) 1383fa9e4066Sahrens { 1384c7ca1008Sgw25295 minor_t minor = getminor(dev); 1385c7ca1008Sgw25295 zvol_state_t *zv; 138673ec3d9cSgw25295 uint64_t volsize; 1387c2e6a7d6Sperrin rl_t *rl; 1388feb08c6bSbillm int error = 0; 1389feb08c6bSbillm 1390c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1391c7ca1008Sgw25295 if (zv == NULL) 1392be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 1393c7ca1008Sgw25295 139473ec3d9cSgw25295 volsize = zv->zv_volsize; 139573ec3d9cSgw25295 if (uio->uio_resid > 0 && 139673ec3d9cSgw25295 (uio->uio_loffset < 0 || uio->uio_loffset >= volsize)) 1397be6fd75aSMatthew Ahrens return (SET_ERROR(EIO)); 139873ec3d9cSgw25295 139988b7b0f2SMatthew Ahrens if (zv->zv_flags & ZVOL_DUMPIFIED) { 140088b7b0f2SMatthew Ahrens error = physio(zvol_strategy, NULL, dev, B_READ, 140188b7b0f2SMatthew Ahrens zvol_minphys, uio); 140288b7b0f2SMatthew Ahrens return (error); 140388b7b0f2SMatthew Ahrens } 140488b7b0f2SMatthew Ahrens 1405c2e6a7d6Sperrin rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 1406c2e6a7d6Sperrin RL_READER); 140773ec3d9cSgw25295 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 1408feb08c6bSbillm uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 1409feb08c6bSbillm 141073ec3d9cSgw25295 /* don't read past the end */ 141173ec3d9cSgw25295 if (bytes > volsize - uio->uio_loffset) 141273ec3d9cSgw25295 bytes = volsize - uio->uio_loffset; 141373ec3d9cSgw25295 1414feb08c6bSbillm error = dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes); 1415b87f3af3Sperrin if (error) { 1416b87f3af3Sperrin /* convert checksum errors into IO errors */ 1417b87f3af3Sperrin if (error == ECKSUM) 1418be6fd75aSMatthew Ahrens error = SET_ERROR(EIO); 1419feb08c6bSbillm break; 1420feb08c6bSbillm } 1421b87f3af3Sperrin } 1422c2e6a7d6Sperrin zfs_range_unlock(rl); 1423feb08c6bSbillm return (error); 1424fa9e4066Sahrens } 1425fa9e4066Sahrens 1426fa9e4066Sahrens /*ARGSUSED*/ 1427fa9e4066Sahrens int 1428feb08c6bSbillm zvol_write(dev_t dev, uio_t *uio, cred_t *cr) 1429fa9e4066Sahrens { 1430c7ca1008Sgw25295 minor_t minor = getminor(dev); 1431c7ca1008Sgw25295 zvol_state_t *zv; 143273ec3d9cSgw25295 uint64_t volsize; 1433c2e6a7d6Sperrin rl_t *rl; 1434feb08c6bSbillm int error = 0; 1435510b6c0eSNeil Perrin boolean_t sync; 1436fa9e4066Sahrens 1437c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1438c7ca1008Sgw25295 if (zv == NULL) 1439be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 1440c7ca1008Sgw25295 144173ec3d9cSgw25295 volsize = zv->zv_volsize; 144273ec3d9cSgw25295 if (uio->uio_resid > 0 && 144373ec3d9cSgw25295 (uio->uio_loffset < 0 || uio->uio_loffset >= volsize)) 1444be6fd75aSMatthew Ahrens return (SET_ERROR(EIO)); 144573ec3d9cSgw25295 1446e7cbe64fSgw25295 if (zv->zv_flags & ZVOL_DUMPIFIED) { 1447e7cbe64fSgw25295 error = physio(zvol_strategy, NULL, dev, B_WRITE, 1448e7cbe64fSgw25295 zvol_minphys, uio); 1449e7cbe64fSgw25295 return (error); 1450e7cbe64fSgw25295 } 1451e7cbe64fSgw25295 145255da60b9SMark J Musante sync = !(zv->zv_flags & ZVOL_WCE) || 145355da60b9SMark J Musante (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS); 1454510b6c0eSNeil Perrin 1455c2e6a7d6Sperrin rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 1456c2e6a7d6Sperrin RL_WRITER); 145773ec3d9cSgw25295 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 1458feb08c6bSbillm uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 1459feb08c6bSbillm uint64_t off = uio->uio_loffset; 1460feb08c6bSbillm dmu_tx_t *tx = dmu_tx_create(zv->zv_objset); 146173ec3d9cSgw25295 146273ec3d9cSgw25295 if (bytes > volsize - off) /* don't write past the end */ 146373ec3d9cSgw25295 bytes = volsize - off; 146473ec3d9cSgw25295 1465feb08c6bSbillm dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 1466feb08c6bSbillm error = dmu_tx_assign(tx, TXG_WAIT); 1467feb08c6bSbillm if (error) { 1468feb08c6bSbillm dmu_tx_abort(tx); 1469feb08c6bSbillm break; 1470feb08c6bSbillm } 147194d1a210STim Haley error = dmu_write_uio_dbuf(zv->zv_dbuf, uio, bytes, tx); 1472feb08c6bSbillm if (error == 0) 1473510b6c0eSNeil Perrin zvol_log_write(zv, tx, off, bytes, sync); 1474feb08c6bSbillm dmu_tx_commit(tx); 1475feb08c6bSbillm 1476feb08c6bSbillm if (error) 1477feb08c6bSbillm break; 1478feb08c6bSbillm } 1479c2e6a7d6Sperrin zfs_range_unlock(rl); 1480510b6c0eSNeil Perrin if (sync) 14815002558fSNeil Perrin zil_commit(zv->zv_zilog, ZVOL_OBJ); 1482feb08c6bSbillm return (error); 1483fa9e4066Sahrens } 1484fa9e4066Sahrens 1485c7f714e2SEric Taylor int 1486c7f714e2SEric Taylor zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs) 1487c7f714e2SEric Taylor { 1488c7f714e2SEric Taylor struct uuid uuid = EFI_RESERVED; 1489c7f714e2SEric Taylor efi_gpe_t gpe = { 0 }; 1490c7f714e2SEric Taylor uint32_t crc; 1491c7f714e2SEric Taylor dk_efi_t efi; 1492c7f714e2SEric Taylor int length; 1493c7f714e2SEric Taylor char *ptr; 1494c7f714e2SEric Taylor 1495c7f714e2SEric Taylor if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag)) 1496be6fd75aSMatthew Ahrens return (SET_ERROR(EFAULT)); 1497c7f714e2SEric Taylor ptr = (char *)(uintptr_t)efi.dki_data_64; 1498c7f714e2SEric Taylor length = efi.dki_length; 1499c7f714e2SEric Taylor /* 1500c7f714e2SEric Taylor * Some clients may attempt to request a PMBR for the 1501c7f714e2SEric Taylor * zvol. Currently this interface will return EINVAL to 1502c7f714e2SEric Taylor * such requests. These requests could be supported by 1503c7f714e2SEric Taylor * adding a check for lba == 0 and consing up an appropriate 1504c7f714e2SEric Taylor * PMBR. 1505c7f714e2SEric Taylor */ 1506c7f714e2SEric Taylor if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0) 1507be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 1508c7f714e2SEric Taylor 1509c7f714e2SEric Taylor gpe.efi_gpe_StartingLBA = LE_64(34ULL); 1510c7f714e2SEric Taylor gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1); 1511c7f714e2SEric Taylor UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid); 1512c7f714e2SEric Taylor 1513c7f714e2SEric Taylor if (efi.dki_lba == 1) { 1514c7f714e2SEric Taylor efi_gpt_t gpt = { 0 }; 1515c7f714e2SEric Taylor 1516c7f714e2SEric Taylor gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE); 1517c7f714e2SEric Taylor gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT); 1518c7f714e2SEric Taylor gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt)); 1519c7f714e2SEric Taylor gpt.efi_gpt_MyLBA = LE_64(1ULL); 1520c7f714e2SEric Taylor gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL); 1521c7f714e2SEric Taylor gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1); 1522c7f714e2SEric Taylor gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL); 1523c7f714e2SEric Taylor gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1); 1524c7f714e2SEric Taylor gpt.efi_gpt_SizeOfPartitionEntry = 1525c7f714e2SEric Taylor LE_32(sizeof (efi_gpe_t)); 1526c7f714e2SEric Taylor CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table); 1527c7f714e2SEric Taylor gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc); 1528c7f714e2SEric Taylor CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table); 1529c7f714e2SEric Taylor gpt.efi_gpt_HeaderCRC32 = LE_32(~crc); 1530c7f714e2SEric Taylor if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length), 1531c7f714e2SEric Taylor flag)) 1532be6fd75aSMatthew Ahrens return (SET_ERROR(EFAULT)); 1533c7f714e2SEric Taylor ptr += sizeof (gpt); 1534c7f714e2SEric Taylor length -= sizeof (gpt); 1535c7f714e2SEric Taylor } 1536c7f714e2SEric Taylor if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe), 1537c7f714e2SEric Taylor length), flag)) 1538be6fd75aSMatthew Ahrens return (SET_ERROR(EFAULT)); 1539c7f714e2SEric Taylor return (0); 1540c7f714e2SEric Taylor } 1541c7f714e2SEric Taylor 1542fa9e4066Sahrens /* 15433fb517f7SJames Moore * BEGIN entry points to allow external callers access to the volume. 15443fb517f7SJames Moore */ 15453fb517f7SJames Moore /* 15463fb517f7SJames Moore * Return the volume parameters needed for access from an external caller. 15473fb517f7SJames Moore * These values are invariant as long as the volume is held open. 15483fb517f7SJames Moore */ 15493fb517f7SJames Moore int 15503fb517f7SJames Moore zvol_get_volume_params(minor_t minor, uint64_t *blksize, 15513fb517f7SJames Moore uint64_t *max_xfer_len, void **minor_hdl, void **objset_hdl, void **zil_hdl, 15523fb517f7SJames Moore void **rl_hdl, void **bonus_hdl) 15533fb517f7SJames Moore { 15543fb517f7SJames Moore zvol_state_t *zv; 15553fb517f7SJames Moore 1556c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(minor, ZSST_ZVOL); 1557c99e4bdcSChris Kirby if (zv == NULL) 1558be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 15593fb517f7SJames Moore if (zv->zv_flags & ZVOL_DUMPIFIED) 1560be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 15613fb517f7SJames Moore 15623fb517f7SJames Moore ASSERT(blksize && max_xfer_len && minor_hdl && 15633fb517f7SJames Moore objset_hdl && zil_hdl && rl_hdl && bonus_hdl); 15643fb517f7SJames Moore 15653fb517f7SJames Moore *blksize = zv->zv_volblocksize; 15663fb517f7SJames Moore *max_xfer_len = (uint64_t)zvol_maxphys; 15673fb517f7SJames Moore *minor_hdl = zv; 15683fb517f7SJames Moore *objset_hdl = zv->zv_objset; 15693fb517f7SJames Moore *zil_hdl = zv->zv_zilog; 15703fb517f7SJames Moore *rl_hdl = &zv->zv_znode; 15713fb517f7SJames Moore *bonus_hdl = zv->zv_dbuf; 15723fb517f7SJames Moore return (0); 15733fb517f7SJames Moore } 15743fb517f7SJames Moore 15753fb517f7SJames Moore /* 15763fb517f7SJames Moore * Return the current volume size to an external caller. 15773fb517f7SJames Moore * The size can change while the volume is open. 15783fb517f7SJames Moore */ 15793fb517f7SJames Moore uint64_t 15803fb517f7SJames Moore zvol_get_volume_size(void *minor_hdl) 15813fb517f7SJames Moore { 15823fb517f7SJames Moore zvol_state_t *zv = minor_hdl; 15833fb517f7SJames Moore 15843fb517f7SJames Moore return (zv->zv_volsize); 15853fb517f7SJames Moore } 15863fb517f7SJames Moore 15873fb517f7SJames Moore /* 15883fb517f7SJames Moore * Return the current WCE setting to an external caller. 15893fb517f7SJames Moore * The WCE setting can change while the volume is open. 15903fb517f7SJames Moore */ 15913fb517f7SJames Moore int 15923fb517f7SJames Moore zvol_get_volume_wce(void *minor_hdl) 15933fb517f7SJames Moore { 15943fb517f7SJames Moore zvol_state_t *zv = minor_hdl; 15953fb517f7SJames Moore 15963fb517f7SJames Moore return ((zv->zv_flags & ZVOL_WCE) ? 1 : 0); 15973fb517f7SJames Moore } 15983fb517f7SJames Moore 15993fb517f7SJames Moore /* 16003fb517f7SJames Moore * Entry point for external callers to zvol_log_write 16013fb517f7SJames Moore */ 16023fb517f7SJames Moore void 16033fb517f7SJames Moore zvol_log_write_minor(void *minor_hdl, dmu_tx_t *tx, offset_t off, ssize_t resid, 16043fb517f7SJames Moore boolean_t sync) 16053fb517f7SJames Moore { 16063fb517f7SJames Moore zvol_state_t *zv = minor_hdl; 16073fb517f7SJames Moore 16083fb517f7SJames Moore zvol_log_write(zv, tx, off, resid, sync); 16093fb517f7SJames Moore } 16103fb517f7SJames Moore /* 16113fb517f7SJames Moore * END entry points to allow external callers access to the volume. 16123fb517f7SJames Moore */ 16133fb517f7SJames Moore 16143fb517f7SJames Moore /* 1615b77b9231SDan McDonald * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE. 1616b77b9231SDan McDonald */ 1617b77b9231SDan McDonald static void 1618b77b9231SDan McDonald zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off, uint64_t len, 1619b77b9231SDan McDonald boolean_t sync) 1620b77b9231SDan McDonald { 1621b77b9231SDan McDonald itx_t *itx; 1622b77b9231SDan McDonald lr_truncate_t *lr; 1623b77b9231SDan McDonald zilog_t *zilog = zv->zv_zilog; 1624b77b9231SDan McDonald 1625b77b9231SDan McDonald if (zil_replaying(zilog, tx)) 1626b77b9231SDan McDonald return; 1627b77b9231SDan McDonald 1628b77b9231SDan McDonald itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr)); 1629b77b9231SDan McDonald lr = (lr_truncate_t *)&itx->itx_lr; 1630b77b9231SDan McDonald lr->lr_foid = ZVOL_OBJ; 1631b77b9231SDan McDonald lr->lr_offset = off; 1632b77b9231SDan McDonald lr->lr_length = len; 1633b77b9231SDan McDonald 1634b77b9231SDan McDonald itx->itx_sync = sync; 1635b77b9231SDan McDonald zil_itx_assign(zilog, itx, tx); 1636d78b796cSAndreas Jaekel 1637d78b796cSAndreas Jaekel rw_enter(&rz_zev_rwlock, RW_READER); 1638d78b796cSAndreas Jaekel if (rz_zev_callbacks && rz_zev_callbacks->rz_zev_zvol_truncate) 1639d78b796cSAndreas Jaekel rz_zev_callbacks->rz_zev_zvol_truncate(zv->zv_name, 1640*e206ace3SAndreas Jaekel zv->zv_objset, tx, off, len); 1641d78b796cSAndreas Jaekel rw_exit(&rz_zev_rwlock); 1642b77b9231SDan McDonald } 1643b77b9231SDan McDonald 1644b77b9231SDan McDonald /* 1645fa9e4066Sahrens * Dirtbag ioctls to support mkfs(1M) for UFS filesystems. See dkio(7I). 1646b77b9231SDan McDonald * Also a dirtbag dkio ioctl for unmap/free-block functionality. 1647fa9e4066Sahrens */ 1648fa9e4066Sahrens /*ARGSUSED*/ 1649fa9e4066Sahrens int 1650fa9e4066Sahrens zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 1651fa9e4066Sahrens { 1652fa9e4066Sahrens zvol_state_t *zv; 1653af2c4821Smaybee struct dk_callback *dkc; 1654fa9e4066Sahrens int error = 0; 1655e7cbe64fSgw25295 rl_t *rl; 1656fa9e4066Sahrens 1657c99e4bdcSChris Kirby mutex_enter(&zfsdev_state_lock); 1658fa9e4066Sahrens 1659c99e4bdcSChris Kirby zv = zfsdev_get_soft_state(getminor(dev), ZSST_ZVOL); 1660fa9e4066Sahrens 1661fa9e4066Sahrens if (zv == NULL) { 1662c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 1663be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 1664fa9e4066Sahrens } 1665701f66c4SEric Taylor ASSERT(zv->zv_total_opens > 0); 1666fa9e4066Sahrens 1667fa9e4066Sahrens switch (cmd) { 1668fa9e4066Sahrens 1669fa9e4066Sahrens case DKIOCINFO: 1670a0b60564SGeorge Wilson { 1671a0b60564SGeorge Wilson struct dk_cinfo dki; 1672a0b60564SGeorge Wilson 1673af2c4821Smaybee bzero(&dki, sizeof (dki)); 1674af2c4821Smaybee (void) strcpy(dki.dki_cname, "zvol"); 1675af2c4821Smaybee (void) strcpy(dki.dki_dname, "zvol"); 1676af2c4821Smaybee dki.dki_ctype = DKC_UNKNOWN; 16773adc9019SEric Taylor dki.dki_unit = getminor(dev); 1678b5152584SMatthew Ahrens dki.dki_maxtransfer = 1679b5152584SMatthew Ahrens 1 << (SPA_OLD_MAXBLOCKSHIFT - zv->zv_min_bs); 1680c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 1681af2c4821Smaybee if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag)) 1682be6fd75aSMatthew Ahrens error = SET_ERROR(EFAULT); 1683fa9e4066Sahrens return (error); 1684a0b60564SGeorge Wilson } 1685fa9e4066Sahrens 1686fa9e4066Sahrens case DKIOCGMEDIAINFO: 1687a0b60564SGeorge Wilson { 1688a0b60564SGeorge Wilson struct dk_minfo dkm; 1689a0b60564SGeorge Wilson 1690fa9e4066Sahrens bzero(&dkm, sizeof (dkm)); 1691fa9e4066Sahrens dkm.dki_lbsize = 1U << zv->zv_min_bs; 1692fa9e4066Sahrens dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs; 1693fa9e4066Sahrens dkm.dki_media_type = DK_UNKNOWN; 1694c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 1695fa9e4066Sahrens if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag)) 1696be6fd75aSMatthew Ahrens error = SET_ERROR(EFAULT); 1697fa9e4066Sahrens return (error); 1698a0b60564SGeorge Wilson } 1699a0b60564SGeorge Wilson 1700a0b60564SGeorge Wilson case DKIOCGMEDIAINFOEXT: 1701a0b60564SGeorge Wilson { 1702a0b60564SGeorge Wilson struct dk_minfo_ext dkmext; 1703a0b60564SGeorge Wilson 1704a0b60564SGeorge Wilson bzero(&dkmext, sizeof (dkmext)); 1705a0b60564SGeorge Wilson dkmext.dki_lbsize = 1U << zv->zv_min_bs; 1706a0b60564SGeorge Wilson dkmext.dki_pbsize = zv->zv_volblocksize; 1707a0b60564SGeorge Wilson dkmext.dki_capacity = zv->zv_volsize >> zv->zv_min_bs; 1708a0b60564SGeorge Wilson dkmext.dki_media_type = DK_UNKNOWN; 1709a0b60564SGeorge Wilson mutex_exit(&zfsdev_state_lock); 1710a0b60564SGeorge Wilson if (ddi_copyout(&dkmext, (void *)arg, sizeof (dkmext), flag)) 1711a0b60564SGeorge Wilson error = SET_ERROR(EFAULT); 1712a0b60564SGeorge Wilson return (error); 1713a0b60564SGeorge Wilson } 1714fa9e4066Sahrens 1715fa9e4066Sahrens case DKIOCGETEFI: 1716c7f714e2SEric Taylor { 1717c7f714e2SEric Taylor uint64_t vs = zv->zv_volsize; 1718c7f714e2SEric Taylor uint8_t bs = zv->zv_min_bs; 1719fa9e4066Sahrens 1720c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 1721c7f714e2SEric Taylor error = zvol_getefi((void *)arg, flag, vs, bs); 1722fa9e4066Sahrens return (error); 1723c7f714e2SEric Taylor } 1724fa9e4066Sahrens 1725feb08c6bSbillm case DKIOCFLUSHWRITECACHE: 1726af2c4821Smaybee dkc = (struct dk_callback *)arg; 1727c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 17285002558fSNeil Perrin zil_commit(zv->zv_zilog, ZVOL_OBJ); 1729af2c4821Smaybee if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) { 1730af2c4821Smaybee (*dkc->dkc_callback)(dkc->dkc_cookie, error); 1731af2c4821Smaybee error = 0; 1732af2c4821Smaybee } 1733701f66c4SEric Taylor return (error); 1734701f66c4SEric Taylor 1735701f66c4SEric Taylor case DKIOCGETWCE: 1736701f66c4SEric Taylor { 1737701f66c4SEric Taylor int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0; 1738701f66c4SEric Taylor if (ddi_copyout(&wce, (void *)arg, sizeof (int), 1739701f66c4SEric Taylor flag)) 1740be6fd75aSMatthew Ahrens error = SET_ERROR(EFAULT); 1741feb08c6bSbillm break; 1742701f66c4SEric Taylor } 1743701f66c4SEric Taylor case DKIOCSETWCE: 1744701f66c4SEric Taylor { 1745701f66c4SEric Taylor int wce; 1746701f66c4SEric Taylor if (ddi_copyin((void *)arg, &wce, sizeof (int), 1747701f66c4SEric Taylor flag)) { 1748be6fd75aSMatthew Ahrens error = SET_ERROR(EFAULT); 1749701f66c4SEric Taylor break; 1750701f66c4SEric Taylor } 1751701f66c4SEric Taylor if (wce) { 1752701f66c4SEric Taylor zv->zv_flags |= ZVOL_WCE; 1753c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 1754701f66c4SEric Taylor } else { 1755701f66c4SEric Taylor zv->zv_flags &= ~ZVOL_WCE; 1756c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 17575002558fSNeil Perrin zil_commit(zv->zv_zilog, ZVOL_OBJ); 1758701f66c4SEric Taylor } 1759701f66c4SEric Taylor return (0); 1760701f66c4SEric Taylor } 1761feb08c6bSbillm 1762b6130eadSmaybee case DKIOCGGEOM: 1763b6130eadSmaybee case DKIOCGVTOC: 1764e7cbe64fSgw25295 /* 1765e7cbe64fSgw25295 * commands using these (like prtvtoc) expect ENOTSUP 1766e7cbe64fSgw25295 * since we're emulating an EFI label 1767e7cbe64fSgw25295 */ 1768be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTSUP); 1769b6130eadSmaybee break; 1770b6130eadSmaybee 1771e7cbe64fSgw25295 case DKIOCDUMPINIT: 1772e7cbe64fSgw25295 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 1773e7cbe64fSgw25295 RL_WRITER); 1774e7cbe64fSgw25295 error = zvol_dumpify(zv); 1775e7cbe64fSgw25295 zfs_range_unlock(rl); 1776e7cbe64fSgw25295 break; 1777e7cbe64fSgw25295 1778e7cbe64fSgw25295 case DKIOCDUMPFINI: 177906d5ae10SEric Taylor if (!(zv->zv_flags & ZVOL_DUMPIFIED)) 178006d5ae10SEric Taylor break; 1781e7cbe64fSgw25295 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 1782e7cbe64fSgw25295 RL_WRITER); 1783e7cbe64fSgw25295 error = zvol_dump_fini(zv); 1784e7cbe64fSgw25295 zfs_range_unlock(rl); 1785e7cbe64fSgw25295 break; 1786e7cbe64fSgw25295 1787b77b9231SDan McDonald case DKIOCFREE: 1788b77b9231SDan McDonald { 1789b77b9231SDan McDonald dkioc_free_t df; 1790b77b9231SDan McDonald dmu_tx_t *tx; 1791b77b9231SDan McDonald 1792893c83baSGeorge Wilson if (!zvol_unmap_enabled) 1793893c83baSGeorge Wilson break; 1794893c83baSGeorge Wilson 1795b77b9231SDan McDonald if (ddi_copyin((void *)arg, &df, sizeof (df), flag)) { 1796be6fd75aSMatthew Ahrens error = SET_ERROR(EFAULT); 1797b77b9231SDan McDonald break; 1798b77b9231SDan McDonald } 1799b77b9231SDan McDonald 1800b77b9231SDan McDonald /* 1801b77b9231SDan McDonald * Apply Postel's Law to length-checking. If they overshoot, 1802b77b9231SDan McDonald * just blank out until the end, if there's a need to blank 1803b77b9231SDan McDonald * out anything. 1804b77b9231SDan McDonald */ 1805b77b9231SDan McDonald if (df.df_start >= zv->zv_volsize) 1806b77b9231SDan McDonald break; /* No need to do anything... */ 1807574e2414SGeorge Wilson 1808574e2414SGeorge Wilson mutex_exit(&zfsdev_state_lock); 1809b77b9231SDan McDonald 1810b77b9231SDan McDonald rl = zfs_range_lock(&zv->zv_znode, df.df_start, df.df_length, 1811b77b9231SDan McDonald RL_WRITER); 1812b77b9231SDan McDonald tx = dmu_tx_create(zv->zv_objset); 18134bb73804SMatthew Ahrens dmu_tx_mark_netfree(tx); 1814b77b9231SDan McDonald error = dmu_tx_assign(tx, TXG_WAIT); 1815b77b9231SDan McDonald if (error != 0) { 1816b77b9231SDan McDonald dmu_tx_abort(tx); 1817b77b9231SDan McDonald } else { 1818b77b9231SDan McDonald zvol_log_truncate(zv, tx, df.df_start, 1819b77b9231SDan McDonald df.df_length, B_TRUE); 1820c08b1637SJosef 'Jeff' Sipek dmu_tx_commit(tx); 1821b77b9231SDan McDonald error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 1822b77b9231SDan McDonald df.df_start, df.df_length); 1823b77b9231SDan McDonald } 1824b77b9231SDan McDonald 1825b77b9231SDan McDonald zfs_range_unlock(rl); 1826b77b9231SDan McDonald 1827b77b9231SDan McDonald if (error == 0) { 1828b77b9231SDan McDonald /* 1829b77b9231SDan McDonald * If the write-cache is disabled or 'sync' property 1830b77b9231SDan McDonald * is set to 'always' then treat this as a synchronous 1831b77b9231SDan McDonald * operation (i.e. commit to zil). 1832b77b9231SDan McDonald */ 1833b77b9231SDan McDonald if (!(zv->zv_flags & ZVOL_WCE) || 1834b77b9231SDan McDonald (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)) 1835b77b9231SDan McDonald zil_commit(zv->zv_zilog, ZVOL_OBJ); 1836b77b9231SDan McDonald 1837b77b9231SDan McDonald /* 1838b77b9231SDan McDonald * If the caller really wants synchronous writes, and 1839b77b9231SDan McDonald * can't wait for them, don't return until the write 1840b77b9231SDan McDonald * is done. 1841b77b9231SDan McDonald */ 1842b77b9231SDan McDonald if (df.df_flags & DF_WAIT_SYNC) { 1843b77b9231SDan McDonald txg_wait_synced( 1844b77b9231SDan McDonald dmu_objset_pool(zv->zv_objset), 0); 1845b77b9231SDan McDonald } 1846b77b9231SDan McDonald } 1847574e2414SGeorge Wilson return (error); 1848b77b9231SDan McDonald } 1849b77b9231SDan McDonald 1850fa9e4066Sahrens default: 1851be6fd75aSMatthew Ahrens error = SET_ERROR(ENOTTY); 1852fa9e4066Sahrens break; 1853fa9e4066Sahrens 1854fa9e4066Sahrens } 1855c99e4bdcSChris Kirby mutex_exit(&zfsdev_state_lock); 1856fa9e4066Sahrens return (error); 1857fa9e4066Sahrens } 1858fa9e4066Sahrens 1859fa9e4066Sahrens int 1860fa9e4066Sahrens zvol_busy(void) 1861fa9e4066Sahrens { 1862fa9e4066Sahrens return (zvol_minors != 0); 1863fa9e4066Sahrens } 1864fa9e4066Sahrens 1865fa9e4066Sahrens void 1866fa9e4066Sahrens zvol_init(void) 1867fa9e4066Sahrens { 1868c99e4bdcSChris Kirby VERIFY(ddi_soft_state_init(&zfsdev_state, sizeof (zfs_soft_state_t), 1869c99e4bdcSChris Kirby 1) == 0); 1870c99e4bdcSChris Kirby mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL); 1871fa9e4066Sahrens } 1872fa9e4066Sahrens 1873fa9e4066Sahrens void 1874fa9e4066Sahrens zvol_fini(void) 1875fa9e4066Sahrens { 1876c99e4bdcSChris Kirby mutex_destroy(&zfsdev_state_lock); 1877c99e4bdcSChris Kirby ddi_soft_state_fini(&zfsdev_state); 1878fa9e4066Sahrens } 1879e7cbe64fSgw25295 1880810e43b2SBill Pijewski /*ARGSUSED*/ 1881810e43b2SBill Pijewski static int 1882810e43b2SBill Pijewski zfs_mvdev_dump_feature_check(void *arg, dmu_tx_t *tx) 1883810e43b2SBill Pijewski { 1884810e43b2SBill Pijewski spa_t *spa = dmu_tx_pool(tx)->dp_spa; 1885810e43b2SBill Pijewski 18862acef22dSMatthew Ahrens if (spa_feature_is_active(spa, SPA_FEATURE_MULTI_VDEV_CRASH_DUMP)) 1887810e43b2SBill Pijewski return (1); 1888810e43b2SBill Pijewski return (0); 1889810e43b2SBill Pijewski } 1890810e43b2SBill Pijewski 1891810e43b2SBill Pijewski /*ARGSUSED*/ 1892810e43b2SBill Pijewski static void 1893810e43b2SBill Pijewski zfs_mvdev_dump_activate_feature_sync(void *arg, dmu_tx_t *tx) 1894810e43b2SBill Pijewski { 1895810e43b2SBill Pijewski spa_t *spa = dmu_tx_pool(tx)->dp_spa; 1896810e43b2SBill Pijewski 18972acef22dSMatthew Ahrens spa_feature_incr(spa, SPA_FEATURE_MULTI_VDEV_CRASH_DUMP, tx); 1898810e43b2SBill Pijewski } 1899810e43b2SBill Pijewski 1900e7cbe64fSgw25295 static int 1901e7cbe64fSgw25295 zvol_dump_init(zvol_state_t *zv, boolean_t resize) 1902e7cbe64fSgw25295 { 1903e7cbe64fSgw25295 dmu_tx_t *tx; 1904810e43b2SBill Pijewski int error; 1905e7cbe64fSgw25295 objset_t *os = zv->zv_objset; 1906810e43b2SBill Pijewski spa_t *spa = dmu_objset_spa(os); 1907810e43b2SBill Pijewski vdev_t *vd = spa->spa_root_vdev; 1908e7cbe64fSgw25295 nvlist_t *nv = NULL; 1909810e43b2SBill Pijewski uint64_t version = spa_version(spa); 1910b10bba72SGeorge Wilson uint64_t checksum, compress, refresrv, vbs, dedup; 1911e7cbe64fSgw25295 1912c99e4bdcSChris Kirby ASSERT(MUTEX_HELD(&zfsdev_state_lock)); 1913810e43b2SBill Pijewski ASSERT(vd->vdev_ops == &vdev_root_ops); 1914810e43b2SBill Pijewski 1915681d9761SEric Taylor error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0, 1916681d9761SEric Taylor DMU_OBJECT_END); 1917b10bba72SGeorge Wilson if (error != 0) 1918b10bba72SGeorge Wilson return (error); 1919681d9761SEric Taylor /* wait for dmu_free_long_range to actually free the blocks */ 1920681d9761SEric Taylor txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 1921e7cbe64fSgw25295 1922810e43b2SBill Pijewski /* 1923810e43b2SBill Pijewski * If the pool on which the dump device is being initialized has more 1924810e43b2SBill Pijewski * than one child vdev, check that the MULTI_VDEV_CRASH_DUMP feature is 1925810e43b2SBill Pijewski * enabled. If so, bump that feature's counter to indicate that the 1926810e43b2SBill Pijewski * feature is active. We also check the vdev type to handle the 1927810e43b2SBill Pijewski * following case: 1928810e43b2SBill Pijewski * # zpool create test raidz disk1 disk2 disk3 1929810e43b2SBill Pijewski * Now have spa_root_vdev->vdev_children == 1 (the raidz vdev), 1930810e43b2SBill Pijewski * the raidz vdev itself has 3 children. 1931810e43b2SBill Pijewski */ 1932810e43b2SBill Pijewski if (vd->vdev_children > 1 || vd->vdev_ops == &vdev_raidz_ops) { 1933810e43b2SBill Pijewski if (!spa_feature_is_enabled(spa, 19342acef22dSMatthew Ahrens SPA_FEATURE_MULTI_VDEV_CRASH_DUMP)) 1935810e43b2SBill Pijewski return (SET_ERROR(ENOTSUP)); 1936810e43b2SBill Pijewski (void) dsl_sync_task(spa_name(spa), 1937810e43b2SBill Pijewski zfs_mvdev_dump_feature_check, 19387d46dc6cSMatthew Ahrens zfs_mvdev_dump_activate_feature_sync, NULL, 19397d46dc6cSMatthew Ahrens 2, ZFS_SPACE_CHECK_RESERVED); 1940810e43b2SBill Pijewski } 1941810e43b2SBill Pijewski 1942b10bba72SGeorge Wilson if (!resize) { 1943b10bba72SGeorge Wilson error = dsl_prop_get_integer(zv->zv_name, 1944b10bba72SGeorge Wilson zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL); 1945b10bba72SGeorge Wilson if (error == 0) { 1946b10bba72SGeorge Wilson error = dsl_prop_get_integer(zv->zv_name, 1947b10bba72SGeorge Wilson zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, 1948b10bba72SGeorge Wilson NULL); 1949b10bba72SGeorge Wilson } 1950b10bba72SGeorge Wilson if (error == 0) { 1951b10bba72SGeorge Wilson error = dsl_prop_get_integer(zv->zv_name, 1952b10bba72SGeorge Wilson zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 1953b10bba72SGeorge Wilson &refresrv, NULL); 1954b10bba72SGeorge Wilson } 1955b10bba72SGeorge Wilson if (error == 0) { 1956b10bba72SGeorge Wilson error = dsl_prop_get_integer(zv->zv_name, 1957b10bba72SGeorge Wilson zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, 1958b10bba72SGeorge Wilson NULL); 1959b10bba72SGeorge Wilson } 1960b10bba72SGeorge Wilson if (version >= SPA_VERSION_DEDUP && error == 0) { 1961b10bba72SGeorge Wilson error = dsl_prop_get_integer(zv->zv_name, 1962b10bba72SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), &dedup, NULL); 1963b10bba72SGeorge Wilson } 1964b10bba72SGeorge Wilson } 1965b10bba72SGeorge Wilson if (error != 0) 1966b10bba72SGeorge Wilson return (error); 1967b10bba72SGeorge Wilson 1968e7cbe64fSgw25295 tx = dmu_tx_create(os); 1969e7cbe64fSgw25295 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 1970681d9761SEric Taylor dmu_tx_hold_bonus(tx, ZVOL_OBJ); 1971e7cbe64fSgw25295 error = dmu_tx_assign(tx, TXG_WAIT); 1972b10bba72SGeorge Wilson if (error != 0) { 1973e7cbe64fSgw25295 dmu_tx_abort(tx); 1974e7cbe64fSgw25295 return (error); 1975e7cbe64fSgw25295 } 1976e7cbe64fSgw25295 1977e7cbe64fSgw25295 /* 1978e7cbe64fSgw25295 * If we are resizing the dump device then we only need to 1979e7cbe64fSgw25295 * update the refreservation to match the newly updated 1980e7cbe64fSgw25295 * zvolsize. Otherwise, we save off the original state of the 1981e7cbe64fSgw25295 * zvol so that we can restore them if the zvol is ever undumpified. 1982e7cbe64fSgw25295 */ 1983e7cbe64fSgw25295 if (resize) { 1984e7cbe64fSgw25295 error = zap_update(os, ZVOL_ZAP_OBJ, 1985e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 1986e7cbe64fSgw25295 &zv->zv_volsize, tx); 1987e7cbe64fSgw25295 } else { 1988b10bba72SGeorge Wilson error = zap_update(os, ZVOL_ZAP_OBJ, 1989e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, 1990e7cbe64fSgw25295 &compress, tx); 1991b10bba72SGeorge Wilson if (error == 0) { 1992b10bba72SGeorge Wilson error = zap_update(os, ZVOL_ZAP_OBJ, 1993b10bba72SGeorge Wilson zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, 1994b10bba72SGeorge Wilson &checksum, tx); 1995b10bba72SGeorge Wilson } 1996b10bba72SGeorge Wilson if (error == 0) { 1997b10bba72SGeorge Wilson error = zap_update(os, ZVOL_ZAP_OBJ, 1998e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 1999e7cbe64fSgw25295 &refresrv, tx); 2000b10bba72SGeorge Wilson } 2001b10bba72SGeorge Wilson if (error == 0) { 2002b10bba72SGeorge Wilson error = zap_update(os, ZVOL_ZAP_OBJ, 200388b7b0f2SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, 200488b7b0f2SMatthew Ahrens &vbs, tx); 2005b10bba72SGeorge Wilson } 2006b10bba72SGeorge Wilson if (error == 0) { 2007b10bba72SGeorge Wilson error = dmu_object_set_blocksize( 2008b5152584SMatthew Ahrens os, ZVOL_OBJ, SPA_OLD_MAXBLOCKSIZE, 0, tx); 2009b10bba72SGeorge Wilson } 2010b10bba72SGeorge Wilson if (version >= SPA_VERSION_DEDUP && error == 0) { 2011b10bba72SGeorge Wilson error = zap_update(os, ZVOL_ZAP_OBJ, 2012afee20e4SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, 2013afee20e4SGeorge Wilson &dedup, tx); 20148d265e66SGeorge Wilson } 2015681d9761SEric Taylor if (error == 0) 2016b5152584SMatthew Ahrens zv->zv_volblocksize = SPA_OLD_MAXBLOCKSIZE; 2017e7cbe64fSgw25295 } 2018e7cbe64fSgw25295 dmu_tx_commit(tx); 2019e7cbe64fSgw25295 2020e7cbe64fSgw25295 /* 2021e7cbe64fSgw25295 * We only need update the zvol's property if we are initializing 2022e7cbe64fSgw25295 * the dump area for the first time. 2023e7cbe64fSgw25295 */ 2024b10bba72SGeorge Wilson if (error == 0 && !resize) { 2025b10bba72SGeorge Wilson /* 2026b10bba72SGeorge Wilson * If MULTI_VDEV_CRASH_DUMP is active, use the NOPARITY checksum 2027b10bba72SGeorge Wilson * function. Otherwise, use the old default -- OFF. 2028b10bba72SGeorge Wilson */ 2029b10bba72SGeorge Wilson checksum = spa_feature_is_active(spa, 2030b10bba72SGeorge Wilson SPA_FEATURE_MULTI_VDEV_CRASH_DUMP) ? ZIO_CHECKSUM_NOPARITY : 2031b10bba72SGeorge Wilson ZIO_CHECKSUM_OFF; 2032b10bba72SGeorge Wilson 2033e7cbe64fSgw25295 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2034e7cbe64fSgw25295 VERIFY(nvlist_add_uint64(nv, 2035e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0); 2036e7cbe64fSgw25295 VERIFY(nvlist_add_uint64(nv, 2037e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 2038e7cbe64fSgw25295 ZIO_COMPRESS_OFF) == 0); 2039e7cbe64fSgw25295 VERIFY(nvlist_add_uint64(nv, 2040e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 2041810e43b2SBill Pijewski checksum) == 0); 20428d265e66SGeorge Wilson if (version >= SPA_VERSION_DEDUP) { 2043afee20e4SGeorge Wilson VERIFY(nvlist_add_uint64(nv, 2044afee20e4SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), 2045afee20e4SGeorge Wilson ZIO_CHECKSUM_OFF) == 0); 20468d265e66SGeorge Wilson } 2047e7cbe64fSgw25295 204892241e0bSTom Erickson error = zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL, 204992241e0bSTom Erickson nv, NULL); 2050e7cbe64fSgw25295 nvlist_free(nv); 2051e7cbe64fSgw25295 } 2052e7cbe64fSgw25295 2053e7cbe64fSgw25295 /* Allocate the space for the dump */ 2054b10bba72SGeorge Wilson if (error == 0) 2055e7cbe64fSgw25295 error = zvol_prealloc(zv); 2056e7cbe64fSgw25295 return (error); 2057e7cbe64fSgw25295 } 2058e7cbe64fSgw25295 2059e7cbe64fSgw25295 static int 2060e7cbe64fSgw25295 zvol_dumpify(zvol_state_t *zv) 2061e7cbe64fSgw25295 { 2062e7cbe64fSgw25295 int error = 0; 2063e7cbe64fSgw25295 uint64_t dumpsize = 0; 2064e7cbe64fSgw25295 dmu_tx_t *tx; 2065e7cbe64fSgw25295 objset_t *os = zv->zv_objset; 2066e7cbe64fSgw25295 2067681d9761SEric Taylor if (zv->zv_flags & ZVOL_RDONLY) 2068be6fd75aSMatthew Ahrens return (SET_ERROR(EROFS)); 2069e7cbe64fSgw25295 2070e7cbe64fSgw25295 if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 2071e7cbe64fSgw25295 8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) { 20724445fffbSMatthew Ahrens boolean_t resize = (dumpsize > 0); 2073e7cbe64fSgw25295 2074e7cbe64fSgw25295 if ((error = zvol_dump_init(zv, resize)) != 0) { 2075e7cbe64fSgw25295 (void) zvol_dump_fini(zv); 2076e7cbe64fSgw25295 return (error); 2077e7cbe64fSgw25295 } 2078e7cbe64fSgw25295 } 2079e7cbe64fSgw25295 2080e7cbe64fSgw25295 /* 2081e7cbe64fSgw25295 * Build up our lba mapping. 2082e7cbe64fSgw25295 */ 2083e7cbe64fSgw25295 error = zvol_get_lbas(zv); 2084e7cbe64fSgw25295 if (error) { 2085e7cbe64fSgw25295 (void) zvol_dump_fini(zv); 2086e7cbe64fSgw25295 return (error); 2087e7cbe64fSgw25295 } 2088e7cbe64fSgw25295 2089e7cbe64fSgw25295 tx = dmu_tx_create(os); 2090e7cbe64fSgw25295 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 2091e7cbe64fSgw25295 error = dmu_tx_assign(tx, TXG_WAIT); 2092e7cbe64fSgw25295 if (error) { 2093e7cbe64fSgw25295 dmu_tx_abort(tx); 2094e7cbe64fSgw25295 (void) zvol_dump_fini(zv); 2095e7cbe64fSgw25295 return (error); 2096e7cbe64fSgw25295 } 2097e7cbe64fSgw25295 2098e7cbe64fSgw25295 zv->zv_flags |= ZVOL_DUMPIFIED; 2099e7cbe64fSgw25295 error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1, 2100e7cbe64fSgw25295 &zv->zv_volsize, tx); 2101e7cbe64fSgw25295 dmu_tx_commit(tx); 2102e7cbe64fSgw25295 2103e7cbe64fSgw25295 if (error) { 2104e7cbe64fSgw25295 (void) zvol_dump_fini(zv); 2105e7cbe64fSgw25295 return (error); 2106e7cbe64fSgw25295 } 2107e7cbe64fSgw25295 2108e7cbe64fSgw25295 txg_wait_synced(dmu_objset_pool(os), 0); 2109e7cbe64fSgw25295 return (0); 2110e7cbe64fSgw25295 } 2111e7cbe64fSgw25295 2112e7cbe64fSgw25295 static int 2113e7cbe64fSgw25295 zvol_dump_fini(zvol_state_t *zv) 2114e7cbe64fSgw25295 { 2115e7cbe64fSgw25295 dmu_tx_t *tx; 2116e7cbe64fSgw25295 objset_t *os = zv->zv_objset; 2117e7cbe64fSgw25295 nvlist_t *nv; 2118e7cbe64fSgw25295 int error = 0; 2119afee20e4SGeorge Wilson uint64_t checksum, compress, refresrv, vbs, dedup; 21208d265e66SGeorge Wilson uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset)); 2121e7cbe64fSgw25295 2122b7e50089Smaybee /* 2123b7e50089Smaybee * Attempt to restore the zvol back to its pre-dumpified state. 2124b7e50089Smaybee * This is a best-effort attempt as it's possible that not all 2125b7e50089Smaybee * of these properties were initialized during the dumpify process 2126b7e50089Smaybee * (i.e. error during zvol_dump_init). 2127b7e50089Smaybee */ 2128b7e50089Smaybee 2129e7cbe64fSgw25295 tx = dmu_tx_create(os); 2130e7cbe64fSgw25295 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 2131e7cbe64fSgw25295 error = dmu_tx_assign(tx, TXG_WAIT); 2132e7cbe64fSgw25295 if (error) { 2133e7cbe64fSgw25295 dmu_tx_abort(tx); 2134e7cbe64fSgw25295 return (error); 2135e7cbe64fSgw25295 } 2136b7e50089Smaybee (void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx); 2137b7e50089Smaybee dmu_tx_commit(tx); 2138e7cbe64fSgw25295 2139e7cbe64fSgw25295 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2140e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum); 2141e7cbe64fSgw25295 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2142e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress); 2143e7cbe64fSgw25295 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 2144e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv); 214588b7b0f2SMatthew Ahrens (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 214688b7b0f2SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs); 2147e7cbe64fSgw25295 2148e7cbe64fSgw25295 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2149e7cbe64fSgw25295 (void) nvlist_add_uint64(nv, 2150e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum); 2151e7cbe64fSgw25295 (void) nvlist_add_uint64(nv, 2152e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress); 2153e7cbe64fSgw25295 (void) nvlist_add_uint64(nv, 2154e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv); 21558d265e66SGeorge Wilson if (version >= SPA_VERSION_DEDUP && 21568d265e66SGeorge Wilson zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 21578d265e66SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, &dedup) == 0) { 2158afee20e4SGeorge Wilson (void) nvlist_add_uint64(nv, 2159afee20e4SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), dedup); 21608d265e66SGeorge Wilson } 216192241e0bSTom Erickson (void) zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL, 216292241e0bSTom Erickson nv, NULL); 2163e7cbe64fSgw25295 nvlist_free(nv); 2164e7cbe64fSgw25295 2165b7e50089Smaybee zvol_free_extents(zv); 2166b7e50089Smaybee zv->zv_flags &= ~ZVOL_DUMPIFIED; 2167b7e50089Smaybee (void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END); 2168681d9761SEric Taylor /* wait for dmu_free_long_range to actually free the blocks */ 2169681d9761SEric Taylor txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 2170681d9761SEric Taylor tx = dmu_tx_create(os); 2171681d9761SEric Taylor dmu_tx_hold_bonus(tx, ZVOL_OBJ); 2172681d9761SEric Taylor error = dmu_tx_assign(tx, TXG_WAIT); 2173681d9761SEric Taylor if (error) { 2174681d9761SEric Taylor dmu_tx_abort(tx); 2175681d9761SEric Taylor return (error); 2176681d9761SEric Taylor } 2177b24ab676SJeff Bonwick if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0) 2178b24ab676SJeff Bonwick zv->zv_volblocksize = vbs; 2179681d9761SEric Taylor dmu_tx_commit(tx); 2180b7e50089Smaybee 2181e7cbe64fSgw25295 return (0); 2182e7cbe64fSgw25295 } 2183