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 /* 22e7cbe64fSgw * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23fa9e4066Sahrens * Use is subject to license terms. 24fa9e4066Sahrens */ 25fa9e4066Sahrens 26fa9e4066Sahrens /* 27fa9e4066Sahrens * ZFS volume emulation driver. 28fa9e4066Sahrens * 29fa9e4066Sahrens * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes. 30fa9e4066Sahrens * Volumes are accessed through the symbolic links named: 31fa9e4066Sahrens * 32fa9e4066Sahrens * /dev/zvol/dsk/<pool_name>/<dataset_name> 33fa9e4066Sahrens * /dev/zvol/rdsk/<pool_name>/<dataset_name> 34fa9e4066Sahrens * 35fa9e4066Sahrens * These links are created by the ZFS-specific devfsadm link generator. 36fa9e4066Sahrens * Volumes are persistent through reboot. No user command needs to be 37fa9e4066Sahrens * run before opening and using a device. 38fa9e4066Sahrens */ 39fa9e4066Sahrens 40fa9e4066Sahrens #include <sys/types.h> 41fa9e4066Sahrens #include <sys/param.h> 42fa9e4066Sahrens #include <sys/errno.h> 43fa9e4066Sahrens #include <sys/uio.h> 44fa9e4066Sahrens #include <sys/buf.h> 45fa9e4066Sahrens #include <sys/modctl.h> 46fa9e4066Sahrens #include <sys/open.h> 47fa9e4066Sahrens #include <sys/kmem.h> 48fa9e4066Sahrens #include <sys/conf.h> 49fa9e4066Sahrens #include <sys/cmn_err.h> 50fa9e4066Sahrens #include <sys/stat.h> 51fa9e4066Sahrens #include <sys/zap.h> 52fa9e4066Sahrens #include <sys/spa.h> 53fa9e4066Sahrens #include <sys/zio.h> 54e7cbe64fSgw #include <sys/dmu_traverse.h> 55e7cbe64fSgw #include <sys/dnode.h> 56e7cbe64fSgw #include <sys/dsl_dataset.h> 57fa9e4066Sahrens #include <sys/dsl_prop.h> 58fa9e4066Sahrens #include <sys/dkio.h> 59fa9e4066Sahrens #include <sys/efi_partition.h> 60fa9e4066Sahrens #include <sys/byteorder.h> 61fa9e4066Sahrens #include <sys/pathname.h> 62fa9e4066Sahrens #include <sys/ddi.h> 63fa9e4066Sahrens #include <sys/sunddi.h> 64fa9e4066Sahrens #include <sys/crc32.h> 65fa9e4066Sahrens #include <sys/dirent.h> 66fa9e4066Sahrens #include <sys/policy.h> 67fa9e4066Sahrens #include <sys/fs/zfs.h> 68fa9e4066Sahrens #include <sys/zfs_ioctl.h> 69fa9e4066Sahrens #include <sys/mkdev.h> 7022ac5be4Sperrin #include <sys/zil.h> 71c5c6ffa0Smaybee #include <sys/refcount.h> 72c2e6a7d6Sperrin #include <sys/zfs_znode.h> 73c2e6a7d6Sperrin #include <sys/zfs_rlock.h> 74e7cbe64fSgw #include <sys/vdev_disk.h> 75e7cbe64fSgw #include <sys/vdev_impl.h> 76e7cbe64fSgw #include <sys/zvol.h> 77e7cbe64fSgw #include <sys/dumphdr.h> 78*1209a471SNeil Perrin #include <sys/zil_impl.h> 79fa9e4066Sahrens 80fa9e4066Sahrens #include "zfs_namecheck.h" 81fa9e4066Sahrens 82fa9e4066Sahrens static void *zvol_state; 83fa9e4066Sahrens 84e7cbe64fSgw #define ZVOL_DUMPSIZE "dumpsize" 85e7cbe64fSgw 86fa9e4066Sahrens /* 87fa9e4066Sahrens * This lock protects the zvol_state structure from being modified 88fa9e4066Sahrens * while it's being used, e.g. an open that comes in before a create 89fa9e4066Sahrens * finishes. It also protects temporary opens of the dataset so that, 90fa9e4066Sahrens * e.g., an open doesn't get a spurious EBUSY. 91fa9e4066Sahrens */ 92fa9e4066Sahrens static kmutex_t zvol_state_lock; 93fa9e4066Sahrens static uint32_t zvol_minors; 94fa9e4066Sahrens 95e7cbe64fSgw typedef struct zvol_extent { 9688b7b0f2SMatthew Ahrens list_node_t ze_node; 97e7cbe64fSgw dva_t ze_dva; /* dva associated with this extent */ 9888b7b0f2SMatthew Ahrens uint64_t ze_nblks; /* number of blocks in extent */ 99e7cbe64fSgw } zvol_extent_t; 100e7cbe64fSgw 101fa9e4066Sahrens /* 102fa9e4066Sahrens * The in-core state of each volume. 103fa9e4066Sahrens */ 104fa9e4066Sahrens typedef struct zvol_state { 105fa9e4066Sahrens char zv_name[MAXPATHLEN]; /* pool/dd name */ 106fa9e4066Sahrens uint64_t zv_volsize; /* amount of space we advertise */ 10767bd71c6Sperrin uint64_t zv_volblocksize; /* volume block size */ 108fa9e4066Sahrens minor_t zv_minor; /* minor number */ 109fa9e4066Sahrens uint8_t zv_min_bs; /* minimum addressable block shift */ 110e7cbe64fSgw uint8_t zv_flags; /* readonly; dumpified */ 111fa9e4066Sahrens objset_t *zv_objset; /* objset handle */ 112fa9e4066Sahrens uint32_t zv_mode; /* DS_MODE_* flags at open time */ 113fa9e4066Sahrens uint32_t zv_open_count[OTYPCNT]; /* open counts */ 114fa9e4066Sahrens uint32_t zv_total_opens; /* total open count */ 11522ac5be4Sperrin zilog_t *zv_zilog; /* ZIL handle */ 11688b7b0f2SMatthew Ahrens list_t zv_extents; /* List of extents for dump */ 117c2e6a7d6Sperrin znode_t zv_znode; /* for range locking */ 118fa9e4066Sahrens } zvol_state_t; 119fa9e4066Sahrens 120e7cbe64fSgw /* 121e7cbe64fSgw * zvol specific flags 122e7cbe64fSgw */ 123e7cbe64fSgw #define ZVOL_RDONLY 0x1 124e7cbe64fSgw #define ZVOL_DUMPIFIED 0x2 125c7f714e2SEric Taylor #define ZVOL_EXCL 0x4 126e7cbe64fSgw 12767bd71c6Sperrin /* 12867bd71c6Sperrin * zvol maximum transfer in one DMU tx. 12967bd71c6Sperrin */ 13067bd71c6Sperrin int zvol_maxphys = DMU_MAX_ACCESS/2; 13167bd71c6Sperrin 132e7cbe64fSgw extern int zfs_set_prop_nvlist(const char *, nvlist_t *); 133feb08c6bSbillm static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio); 134e7cbe64fSgw static int zvol_dumpify(zvol_state_t *zv); 135e7cbe64fSgw static int zvol_dump_fini(zvol_state_t *zv); 136e7cbe64fSgw static int zvol_dump_init(zvol_state_t *zv, boolean_t resize); 13767bd71c6Sperrin 138fa9e4066Sahrens static void 13991ebeef5Sahrens zvol_size_changed(zvol_state_t *zv, major_t maj) 140fa9e4066Sahrens { 14191ebeef5Sahrens dev_t dev = makedevice(maj, zv->zv_minor); 142fa9e4066Sahrens 143fa9e4066Sahrens VERIFY(ddi_prop_update_int64(dev, zfs_dip, 144fa9e4066Sahrens "Size", zv->zv_volsize) == DDI_SUCCESS); 145fa9e4066Sahrens VERIFY(ddi_prop_update_int64(dev, zfs_dip, 146fa9e4066Sahrens "Nblocks", lbtodb(zv->zv_volsize)) == DDI_SUCCESS); 147e7cbe64fSgw 148e7cbe64fSgw /* Notify specfs to invalidate the cached size */ 149e7cbe64fSgw spec_size_invalidate(dev, VBLK); 150e7cbe64fSgw spec_size_invalidate(dev, VCHR); 151fa9e4066Sahrens } 152fa9e4066Sahrens 153fa9e4066Sahrens int 154e9dbad6fSeschrock zvol_check_volsize(uint64_t volsize, uint64_t blocksize) 155fa9e4066Sahrens { 156e9dbad6fSeschrock if (volsize == 0) 157fa9e4066Sahrens return (EINVAL); 158fa9e4066Sahrens 159e9dbad6fSeschrock if (volsize % blocksize != 0) 1605c5460e9Seschrock return (EINVAL); 1615c5460e9Seschrock 162fa9e4066Sahrens #ifdef _ILP32 163e9dbad6fSeschrock if (volsize - 1 > SPEC_MAXOFFSET_T) 164fa9e4066Sahrens return (EOVERFLOW); 165fa9e4066Sahrens #endif 166fa9e4066Sahrens return (0); 167fa9e4066Sahrens } 168fa9e4066Sahrens 169fa9e4066Sahrens int 170e9dbad6fSeschrock zvol_check_volblocksize(uint64_t volblocksize) 171fa9e4066Sahrens { 172e9dbad6fSeschrock if (volblocksize < SPA_MINBLOCKSIZE || 173e9dbad6fSeschrock volblocksize > SPA_MAXBLOCKSIZE || 174e9dbad6fSeschrock !ISP2(volblocksize)) 175fa9e4066Sahrens return (EDOM); 176fa9e4066Sahrens 177fa9e4066Sahrens return (0); 178fa9e4066Sahrens } 179fa9e4066Sahrens 180fa9e4066Sahrens static void 181fa9e4066Sahrens zvol_readonly_changed_cb(void *arg, uint64_t newval) 182fa9e4066Sahrens { 183fa9e4066Sahrens zvol_state_t *zv = arg; 184fa9e4066Sahrens 185e7cbe64fSgw if (newval) 186e7cbe64fSgw zv->zv_flags |= ZVOL_RDONLY; 187e7cbe64fSgw else 188e7cbe64fSgw zv->zv_flags &= ~ZVOL_RDONLY; 189fa9e4066Sahrens } 190fa9e4066Sahrens 191fa9e4066Sahrens int 192a2eea2e1Sahrens zvol_get_stats(objset_t *os, nvlist_t *nv) 193fa9e4066Sahrens { 194fa9e4066Sahrens int error; 195fa9e4066Sahrens dmu_object_info_t doi; 196a2eea2e1Sahrens uint64_t val; 197fa9e4066Sahrens 198fa9e4066Sahrens 199a2eea2e1Sahrens error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val); 200fa9e4066Sahrens if (error) 201fa9e4066Sahrens return (error); 202fa9e4066Sahrens 203a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val); 204a2eea2e1Sahrens 205fa9e4066Sahrens error = dmu_object_info(os, ZVOL_OBJ, &doi); 206fa9e4066Sahrens 207a2eea2e1Sahrens if (error == 0) { 208a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE, 209a2eea2e1Sahrens doi.doi_data_block_size); 210a2eea2e1Sahrens } 211fa9e4066Sahrens 212fa9e4066Sahrens return (error); 213fa9e4066Sahrens } 214fa9e4066Sahrens 215fa9e4066Sahrens /* 216fa9e4066Sahrens * Find a free minor number. 217fa9e4066Sahrens */ 218fa9e4066Sahrens static minor_t 219fa9e4066Sahrens zvol_minor_alloc(void) 220fa9e4066Sahrens { 221fa9e4066Sahrens minor_t minor; 222fa9e4066Sahrens 223fa9e4066Sahrens ASSERT(MUTEX_HELD(&zvol_state_lock)); 224fa9e4066Sahrens 225fa9e4066Sahrens for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) 226fa9e4066Sahrens if (ddi_get_soft_state(zvol_state, minor) == NULL) 227fa9e4066Sahrens return (minor); 228fa9e4066Sahrens 229fa9e4066Sahrens return (0); 230fa9e4066Sahrens } 231fa9e4066Sahrens 232fa9e4066Sahrens static zvol_state_t * 233e9dbad6fSeschrock zvol_minor_lookup(const char *name) 234fa9e4066Sahrens { 235fa9e4066Sahrens minor_t minor; 236fa9e4066Sahrens zvol_state_t *zv; 237fa9e4066Sahrens 238fa9e4066Sahrens ASSERT(MUTEX_HELD(&zvol_state_lock)); 239fa9e4066Sahrens 240fa9e4066Sahrens for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) { 241fa9e4066Sahrens zv = ddi_get_soft_state(zvol_state, minor); 242fa9e4066Sahrens if (zv == NULL) 243fa9e4066Sahrens continue; 244fa9e4066Sahrens if (strcmp(zv->zv_name, name) == 0) 245fa9e4066Sahrens break; 246fa9e4066Sahrens } 247fa9e4066Sahrens 248fa9e4066Sahrens return (zv); 249fa9e4066Sahrens } 250fa9e4066Sahrens 251e7cbe64fSgw /* extent mapping arg */ 252e7cbe64fSgw struct maparg { 25388b7b0f2SMatthew Ahrens zvol_state_t *ma_zv; 25488b7b0f2SMatthew Ahrens uint64_t ma_blks; 255e7cbe64fSgw }; 256e7cbe64fSgw 257e7cbe64fSgw /*ARGSUSED*/ 258e7cbe64fSgw static int 25988b7b0f2SMatthew Ahrens zvol_map_block(spa_t *spa, blkptr_t *bp, const zbookmark_t *zb, 26088b7b0f2SMatthew Ahrens const dnode_phys_t *dnp, void *arg) 261e7cbe64fSgw { 26288b7b0f2SMatthew Ahrens struct maparg *ma = arg; 26388b7b0f2SMatthew Ahrens zvol_extent_t *ze; 26488b7b0f2SMatthew Ahrens int bs = ma->ma_zv->zv_volblocksize; 265e7cbe64fSgw 26688b7b0f2SMatthew Ahrens if (bp == NULL || zb->zb_object != ZVOL_OBJ || zb->zb_level != 0) 26788b7b0f2SMatthew Ahrens return (0); 268e7cbe64fSgw 26988b7b0f2SMatthew Ahrens VERIFY3U(ma->ma_blks, ==, zb->zb_blkid); 27088b7b0f2SMatthew Ahrens ma->ma_blks++; 271e7cbe64fSgw 27288b7b0f2SMatthew Ahrens /* Abort immediately if we have encountered gang blocks */ 27388b7b0f2SMatthew Ahrens if (BP_IS_GANG(bp)) 27488b7b0f2SMatthew Ahrens return (EFRAGS); 275e7cbe64fSgw 27688b7b0f2SMatthew Ahrens /* 27788b7b0f2SMatthew Ahrens * See if the block is at the end of the previous extent. 27888b7b0f2SMatthew Ahrens */ 27988b7b0f2SMatthew Ahrens ze = list_tail(&ma->ma_zv->zv_extents); 28088b7b0f2SMatthew Ahrens if (ze && 28188b7b0f2SMatthew Ahrens DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) && 28288b7b0f2SMatthew Ahrens DVA_GET_OFFSET(BP_IDENTITY(bp)) == 28388b7b0f2SMatthew Ahrens DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) { 28488b7b0f2SMatthew Ahrens ze->ze_nblks++; 28588b7b0f2SMatthew Ahrens return (0); 286e7cbe64fSgw } 287e7cbe64fSgw 28888b7b0f2SMatthew Ahrens dprintf_bp(bp, "%s", "next blkptr:"); 289e7cbe64fSgw 29088b7b0f2SMatthew Ahrens /* start a new extent */ 29188b7b0f2SMatthew Ahrens ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP); 29288b7b0f2SMatthew Ahrens ze->ze_dva = bp->blk_dva[0]; /* structure assignment */ 29388b7b0f2SMatthew Ahrens ze->ze_nblks = 1; 29488b7b0f2SMatthew Ahrens list_insert_tail(&ma->ma_zv->zv_extents, ze); 29588b7b0f2SMatthew Ahrens return (0); 29688b7b0f2SMatthew Ahrens } 297e7cbe64fSgw 29888b7b0f2SMatthew Ahrens static void 29988b7b0f2SMatthew Ahrens zvol_free_extents(zvol_state_t *zv) 30088b7b0f2SMatthew Ahrens { 30188b7b0f2SMatthew Ahrens zvol_extent_t *ze; 302e7cbe64fSgw 30388b7b0f2SMatthew Ahrens while (ze = list_head(&zv->zv_extents)) { 30488b7b0f2SMatthew Ahrens list_remove(&zv->zv_extents, ze); 30588b7b0f2SMatthew Ahrens kmem_free(ze, sizeof (zvol_extent_t)); 306e7cbe64fSgw } 30788b7b0f2SMatthew Ahrens } 308e7cbe64fSgw 30988b7b0f2SMatthew Ahrens static int 31088b7b0f2SMatthew Ahrens zvol_get_lbas(zvol_state_t *zv) 31188b7b0f2SMatthew Ahrens { 31288b7b0f2SMatthew Ahrens struct maparg ma; 31388b7b0f2SMatthew Ahrens int err; 31488b7b0f2SMatthew Ahrens 31588b7b0f2SMatthew Ahrens ma.ma_zv = zv; 31688b7b0f2SMatthew Ahrens ma.ma_blks = 0; 31788b7b0f2SMatthew Ahrens zvol_free_extents(zv); 31888b7b0f2SMatthew Ahrens 31988b7b0f2SMatthew Ahrens err = traverse_dataset(dmu_objset_ds(zv->zv_objset), 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); 324e7cbe64fSgw } 32588b7b0f2SMatthew Ahrens 326e7cbe64fSgw return (0); 327e7cbe64fSgw } 328e7cbe64fSgw 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 /* 345e7cbe64fSgw * 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 36522ac5be4Sperrin /* 36622ac5be4Sperrin * Replay a TX_WRITE ZIL transaction that didn't get committed 36722ac5be4Sperrin * after a system failure 36822ac5be4Sperrin */ 36922ac5be4Sperrin static int 37022ac5be4Sperrin zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap) 37122ac5be4Sperrin { 37222ac5be4Sperrin objset_t *os = zv->zv_objset; 37322ac5be4Sperrin char *data = (char *)(lr + 1); /* data follows lr_write_t */ 37422ac5be4Sperrin uint64_t off = lr->lr_offset; 37522ac5be4Sperrin uint64_t len = lr->lr_length; 37622ac5be4Sperrin dmu_tx_t *tx; 37722ac5be4Sperrin int error; 37822ac5be4Sperrin 37922ac5be4Sperrin if (byteswap) 38022ac5be4Sperrin byteswap_uint64_array(lr, sizeof (*lr)); 38122ac5be4Sperrin 38222ac5be4Sperrin tx = dmu_tx_create(os); 38322ac5be4Sperrin dmu_tx_hold_write(tx, ZVOL_OBJ, off, len); 384*1209a471SNeil Perrin error = dmu_tx_assign(tx, TXG_WAIT); 38522ac5be4Sperrin if (error) { 38622ac5be4Sperrin dmu_tx_abort(tx); 38722ac5be4Sperrin } else { 38822ac5be4Sperrin dmu_write(os, ZVOL_OBJ, off, len, data, tx); 38922ac5be4Sperrin dmu_tx_commit(tx); 39022ac5be4Sperrin } 39122ac5be4Sperrin 39222ac5be4Sperrin return (error); 39322ac5be4Sperrin } 39422ac5be4Sperrin 39522ac5be4Sperrin /* ARGSUSED */ 39622ac5be4Sperrin static int 39722ac5be4Sperrin zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap) 39822ac5be4Sperrin { 39922ac5be4Sperrin return (ENOTSUP); 40022ac5be4Sperrin } 40122ac5be4Sperrin 40222ac5be4Sperrin /* 40322ac5be4Sperrin * Callback vectors for replaying records. 40422ac5be4Sperrin * Only TX_WRITE is needed for zvol. 40522ac5be4Sperrin */ 40622ac5be4Sperrin zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = { 40722ac5be4Sperrin zvol_replay_err, /* 0 no such transaction type */ 40822ac5be4Sperrin zvol_replay_err, /* TX_CREATE */ 40922ac5be4Sperrin zvol_replay_err, /* TX_MKDIR */ 41022ac5be4Sperrin zvol_replay_err, /* TX_MKXATTR */ 41122ac5be4Sperrin zvol_replay_err, /* TX_SYMLINK */ 41222ac5be4Sperrin zvol_replay_err, /* TX_REMOVE */ 41322ac5be4Sperrin zvol_replay_err, /* TX_RMDIR */ 41422ac5be4Sperrin zvol_replay_err, /* TX_LINK */ 41522ac5be4Sperrin zvol_replay_err, /* TX_RENAME */ 41622ac5be4Sperrin zvol_replay_write, /* TX_WRITE */ 41722ac5be4Sperrin zvol_replay_err, /* TX_TRUNCATE */ 41822ac5be4Sperrin zvol_replay_err, /* TX_SETATTR */ 41922ac5be4Sperrin zvol_replay_err, /* TX_ACL */ 42022ac5be4Sperrin }; 42122ac5be4Sperrin 422e7cbe64fSgw /* 423e7cbe64fSgw * Create a minor node (plus a whole lot more) for the specified volume. 424fa9e4066Sahrens */ 425fa9e4066Sahrens int 42691ebeef5Sahrens zvol_create_minor(const char *name, major_t maj) 427fa9e4066Sahrens { 428fa9e4066Sahrens zvol_state_t *zv; 429fa9e4066Sahrens objset_t *os; 43067bd71c6Sperrin dmu_object_info_t doi; 431fa9e4066Sahrens uint64_t volsize; 432fa9e4066Sahrens minor_t minor = 0; 433fa9e4066Sahrens struct pathname linkpath; 434745cd3c5Smaybee int ds_mode = DS_MODE_OWNER; 435fa9e4066Sahrens vnode_t *vp = NULL; 436fa9e4066Sahrens char *devpath; 437e7cbe64fSgw size_t devpathlen = strlen(ZVOL_FULL_DEV_DIR) + strlen(name) + 1; 438fa9e4066Sahrens char chrbuf[30], blkbuf[30]; 439fa9e4066Sahrens int error; 440fa9e4066Sahrens 441fa9e4066Sahrens mutex_enter(&zvol_state_lock); 442fa9e4066Sahrens 443fa9e4066Sahrens if ((zv = zvol_minor_lookup(name)) != NULL) { 444fa9e4066Sahrens mutex_exit(&zvol_state_lock); 445fa9e4066Sahrens return (EEXIST); 446fa9e4066Sahrens } 447fa9e4066Sahrens 448fa9e4066Sahrens if (strchr(name, '@') != 0) 449fa9e4066Sahrens ds_mode |= DS_MODE_READONLY; 450fa9e4066Sahrens 451fa9e4066Sahrens error = dmu_objset_open(name, DMU_OST_ZVOL, ds_mode, &os); 452fa9e4066Sahrens 453fa9e4066Sahrens if (error) { 454fa9e4066Sahrens mutex_exit(&zvol_state_lock); 455fa9e4066Sahrens return (error); 456fa9e4066Sahrens } 457fa9e4066Sahrens 458fa9e4066Sahrens error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize); 459fa9e4066Sahrens 460fa9e4066Sahrens if (error) { 461fa9e4066Sahrens dmu_objset_close(os); 462fa9e4066Sahrens mutex_exit(&zvol_state_lock); 463fa9e4066Sahrens return (error); 464fa9e4066Sahrens } 465fa9e4066Sahrens 466fa9e4066Sahrens /* 467fa9e4066Sahrens * If there's an existing /dev/zvol symlink, try to use the 468fa9e4066Sahrens * same minor number we used last time. 469fa9e4066Sahrens */ 470fa9e4066Sahrens devpath = kmem_alloc(devpathlen, KM_SLEEP); 471fa9e4066Sahrens 472e7cbe64fSgw (void) sprintf(devpath, "%s%s", ZVOL_FULL_DEV_DIR, name); 473fa9e4066Sahrens 474fa9e4066Sahrens error = lookupname(devpath, UIO_SYSSPACE, NO_FOLLOW, NULL, &vp); 475fa9e4066Sahrens 476fa9e4066Sahrens kmem_free(devpath, devpathlen); 477fa9e4066Sahrens 478fa9e4066Sahrens if (error == 0 && vp->v_type != VLNK) 479fa9e4066Sahrens error = EINVAL; 480fa9e4066Sahrens 481fa9e4066Sahrens if (error == 0) { 482fa9e4066Sahrens pn_alloc(&linkpath); 483fa9e4066Sahrens error = pn_getsymlink(vp, &linkpath, kcred); 484fa9e4066Sahrens if (error == 0) { 485fa9e4066Sahrens char *ms = strstr(linkpath.pn_path, ZVOL_PSEUDO_DEV); 486fa9e4066Sahrens if (ms != NULL) { 487fa9e4066Sahrens ms += strlen(ZVOL_PSEUDO_DEV); 488fa9e4066Sahrens minor = stoi(&ms); 489fa9e4066Sahrens } 490fa9e4066Sahrens } 491fa9e4066Sahrens pn_free(&linkpath); 492fa9e4066Sahrens } 493fa9e4066Sahrens 494fa9e4066Sahrens if (vp != NULL) 495fa9e4066Sahrens VN_RELE(vp); 496fa9e4066Sahrens 497fa9e4066Sahrens /* 498fa9e4066Sahrens * If we found a minor but it's already in use, we must pick a new one. 499fa9e4066Sahrens */ 500fa9e4066Sahrens if (minor != 0 && ddi_get_soft_state(zvol_state, minor) != NULL) 501fa9e4066Sahrens minor = 0; 502fa9e4066Sahrens 503fa9e4066Sahrens if (minor == 0) 504fa9e4066Sahrens minor = zvol_minor_alloc(); 505fa9e4066Sahrens 506fa9e4066Sahrens if (minor == 0) { 507fa9e4066Sahrens dmu_objset_close(os); 508fa9e4066Sahrens mutex_exit(&zvol_state_lock); 509fa9e4066Sahrens return (ENXIO); 510fa9e4066Sahrens } 511fa9e4066Sahrens 512fa9e4066Sahrens if (ddi_soft_state_zalloc(zvol_state, minor) != DDI_SUCCESS) { 513fa9e4066Sahrens dmu_objset_close(os); 514fa9e4066Sahrens mutex_exit(&zvol_state_lock); 515fa9e4066Sahrens return (EAGAIN); 516fa9e4066Sahrens } 517fa9e4066Sahrens 518e9dbad6fSeschrock (void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME, 519e9dbad6fSeschrock (char *)name); 520fa9e4066Sahrens 521fa9e4066Sahrens (void) sprintf(chrbuf, "%uc,raw", minor); 522fa9e4066Sahrens 523fa9e4066Sahrens if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR, 524fa9e4066Sahrens minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 525fa9e4066Sahrens ddi_soft_state_free(zvol_state, minor); 526fa9e4066Sahrens dmu_objset_close(os); 527fa9e4066Sahrens mutex_exit(&zvol_state_lock); 528fa9e4066Sahrens return (EAGAIN); 529fa9e4066Sahrens } 530fa9e4066Sahrens 531fa9e4066Sahrens (void) sprintf(blkbuf, "%uc", minor); 532fa9e4066Sahrens 533fa9e4066Sahrens if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK, 534fa9e4066Sahrens minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 535fa9e4066Sahrens ddi_remove_minor_node(zfs_dip, chrbuf); 536fa9e4066Sahrens ddi_soft_state_free(zvol_state, minor); 537fa9e4066Sahrens dmu_objset_close(os); 538fa9e4066Sahrens mutex_exit(&zvol_state_lock); 539fa9e4066Sahrens return (EAGAIN); 540fa9e4066Sahrens } 541fa9e4066Sahrens 542fa9e4066Sahrens zv = ddi_get_soft_state(zvol_state, minor); 543fa9e4066Sahrens 544fa9e4066Sahrens (void) strcpy(zv->zv_name, name); 545fa9e4066Sahrens zv->zv_min_bs = DEV_BSHIFT; 546fa9e4066Sahrens zv->zv_minor = minor; 547fa9e4066Sahrens zv->zv_volsize = volsize; 548fa9e4066Sahrens zv->zv_objset = os; 549fa9e4066Sahrens zv->zv_mode = ds_mode; 55067bd71c6Sperrin zv->zv_zilog = zil_open(os, zvol_get_data); 551c2e6a7d6Sperrin mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL); 552c2e6a7d6Sperrin avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare, 553c2e6a7d6Sperrin sizeof (rl_t), offsetof(rl_t, r_node)); 55488b7b0f2SMatthew Ahrens list_create(&zv->zv_extents, sizeof (zvol_extent_t), 55588b7b0f2SMatthew Ahrens offsetof(zvol_extent_t, ze_node)); 55667bd71c6Sperrin /* get and cache the blocksize */ 55767bd71c6Sperrin error = dmu_object_info(os, ZVOL_OBJ, &doi); 55867bd71c6Sperrin ASSERT(error == 0); 55967bd71c6Sperrin zv->zv_volblocksize = doi.doi_data_block_size; 56022ac5be4Sperrin 561*1209a471SNeil Perrin zil_replay(os, zv, zvol_replay_vector); 56291ebeef5Sahrens zvol_size_changed(zv, maj); 563fa9e4066Sahrens 564ea8dc4b6Seschrock /* XXX this should handle the possible i/o error */ 565fa9e4066Sahrens VERIFY(dsl_prop_register(dmu_objset_ds(zv->zv_objset), 566fa9e4066Sahrens "readonly", zvol_readonly_changed_cb, zv) == 0); 567fa9e4066Sahrens 568fa9e4066Sahrens zvol_minors++; 569fa9e4066Sahrens 570fa9e4066Sahrens mutex_exit(&zvol_state_lock); 571fa9e4066Sahrens 572fa9e4066Sahrens return (0); 573fa9e4066Sahrens } 574fa9e4066Sahrens 575fa9e4066Sahrens /* 576fa9e4066Sahrens * Remove minor node for the specified volume. 577fa9e4066Sahrens */ 578fa9e4066Sahrens int 579e9dbad6fSeschrock zvol_remove_minor(const char *name) 580fa9e4066Sahrens { 581fa9e4066Sahrens zvol_state_t *zv; 582fa9e4066Sahrens char namebuf[30]; 583fa9e4066Sahrens 584fa9e4066Sahrens mutex_enter(&zvol_state_lock); 585fa9e4066Sahrens 586e9dbad6fSeschrock if ((zv = zvol_minor_lookup(name)) == NULL) { 587fa9e4066Sahrens mutex_exit(&zvol_state_lock); 588fa9e4066Sahrens return (ENXIO); 589fa9e4066Sahrens } 590fa9e4066Sahrens 591fa9e4066Sahrens if (zv->zv_total_opens != 0) { 592fa9e4066Sahrens mutex_exit(&zvol_state_lock); 593fa9e4066Sahrens return (EBUSY); 594fa9e4066Sahrens } 595fa9e4066Sahrens 596fa9e4066Sahrens (void) sprintf(namebuf, "%uc,raw", zv->zv_minor); 597fa9e4066Sahrens ddi_remove_minor_node(zfs_dip, namebuf); 598fa9e4066Sahrens 599fa9e4066Sahrens (void) sprintf(namebuf, "%uc", zv->zv_minor); 600fa9e4066Sahrens ddi_remove_minor_node(zfs_dip, namebuf); 601fa9e4066Sahrens 602fa9e4066Sahrens VERIFY(dsl_prop_unregister(dmu_objset_ds(zv->zv_objset), 603fa9e4066Sahrens "readonly", zvol_readonly_changed_cb, zv) == 0); 604fa9e4066Sahrens 60522ac5be4Sperrin zil_close(zv->zv_zilog); 60622ac5be4Sperrin zv->zv_zilog = NULL; 607fa9e4066Sahrens dmu_objset_close(zv->zv_objset); 608fa9e4066Sahrens zv->zv_objset = NULL; 609c2e6a7d6Sperrin avl_destroy(&zv->zv_znode.z_range_avl); 610c2e6a7d6Sperrin mutex_destroy(&zv->zv_znode.z_range_lock); 611fa9e4066Sahrens 612fa9e4066Sahrens ddi_soft_state_free(zvol_state, zv->zv_minor); 613fa9e4066Sahrens 614fa9e4066Sahrens zvol_minors--; 615fa9e4066Sahrens 616fa9e4066Sahrens mutex_exit(&zvol_state_lock); 617fa9e4066Sahrens 618fa9e4066Sahrens return (0); 619fa9e4066Sahrens } 620fa9e4066Sahrens 621e7cbe64fSgw int 622e7cbe64fSgw zvol_prealloc(zvol_state_t *zv) 623e7cbe64fSgw { 624e7cbe64fSgw objset_t *os = zv->zv_objset; 625e7cbe64fSgw dmu_tx_t *tx; 626e7cbe64fSgw uint64_t refd, avail, usedobjs, availobjs; 627e7cbe64fSgw uint64_t resid = zv->zv_volsize; 628e7cbe64fSgw uint64_t off = 0; 629e7cbe64fSgw 630e7cbe64fSgw /* Check the space usage before attempting to allocate the space */ 631e7cbe64fSgw dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs); 632e7cbe64fSgw if (avail < zv->zv_volsize) 633e7cbe64fSgw return (ENOSPC); 634e7cbe64fSgw 635e7cbe64fSgw /* Free old extents if they exist */ 636e7cbe64fSgw zvol_free_extents(zv); 637e7cbe64fSgw 638e7cbe64fSgw while (resid != 0) { 639e7cbe64fSgw int error; 640e7cbe64fSgw uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE); 641e7cbe64fSgw 642e7cbe64fSgw tx = dmu_tx_create(os); 643e7cbe64fSgw dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 644e7cbe64fSgw error = dmu_tx_assign(tx, TXG_WAIT); 645e7cbe64fSgw if (error) { 646e7cbe64fSgw dmu_tx_abort(tx); 647cdb0ab79Smaybee (void) dmu_free_long_range(os, ZVOL_OBJ, 0, off); 648e7cbe64fSgw return (error); 649e7cbe64fSgw } 65082c9918fSTim Haley dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx); 651e7cbe64fSgw dmu_tx_commit(tx); 652e7cbe64fSgw off += bytes; 653e7cbe64fSgw resid -= bytes; 654e7cbe64fSgw } 655e7cbe64fSgw txg_wait_synced(dmu_objset_pool(os), 0); 656e7cbe64fSgw 657e7cbe64fSgw return (0); 658e7cbe64fSgw } 659e7cbe64fSgw 660e7cbe64fSgw int 661e7cbe64fSgw zvol_update_volsize(zvol_state_t *zv, major_t maj, uint64_t volsize) 662e7cbe64fSgw { 663e7cbe64fSgw dmu_tx_t *tx; 664e7cbe64fSgw int error; 665e7cbe64fSgw 666e7cbe64fSgw ASSERT(MUTEX_HELD(&zvol_state_lock)); 667e7cbe64fSgw 668e7cbe64fSgw tx = dmu_tx_create(zv->zv_objset); 669e7cbe64fSgw dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 670e7cbe64fSgw error = dmu_tx_assign(tx, TXG_WAIT); 671e7cbe64fSgw if (error) { 672e7cbe64fSgw dmu_tx_abort(tx); 673e7cbe64fSgw return (error); 674e7cbe64fSgw } 675e7cbe64fSgw 676e7cbe64fSgw error = zap_update(zv->zv_objset, ZVOL_ZAP_OBJ, "size", 8, 1, 677e7cbe64fSgw &volsize, tx); 678e7cbe64fSgw dmu_tx_commit(tx); 679e7cbe64fSgw 680e7cbe64fSgw if (error == 0) 681cdb0ab79Smaybee error = dmu_free_long_range(zv->zv_objset, 682cdb0ab79Smaybee ZVOL_OBJ, volsize, DMU_OBJECT_END); 683e7cbe64fSgw 684bb0ade09Sahrens /* 685bb0ade09Sahrens * If we are using a faked-up state (zv_minor == 0) then don't 686bb0ade09Sahrens * try to update the in-core zvol state. 687bb0ade09Sahrens */ 688bb0ade09Sahrens if (error == 0 && zv->zv_minor) { 689e7cbe64fSgw zv->zv_volsize = volsize; 690e7cbe64fSgw zvol_size_changed(zv, maj); 691e7cbe64fSgw } 692e7cbe64fSgw return (error); 693e7cbe64fSgw } 694e7cbe64fSgw 695fa9e4066Sahrens int 69691ebeef5Sahrens zvol_set_volsize(const char *name, major_t maj, uint64_t volsize) 697fa9e4066Sahrens { 698fa9e4066Sahrens zvol_state_t *zv; 699fa9e4066Sahrens int error; 7005c5460e9Seschrock dmu_object_info_t doi; 701e7cbe64fSgw uint64_t old_volsize = 0ULL; 702bb0ade09Sahrens zvol_state_t state = { 0 }; 703fa9e4066Sahrens 704fa9e4066Sahrens mutex_enter(&zvol_state_lock); 705fa9e4066Sahrens 706e9dbad6fSeschrock if ((zv = zvol_minor_lookup(name)) == NULL) { 707bb0ade09Sahrens /* 708bb0ade09Sahrens * If we are doing a "zfs clone -o volsize=", then the 709bb0ade09Sahrens * minor node won't exist yet. 710bb0ade09Sahrens */ 711bb0ade09Sahrens error = dmu_objset_open(name, DMU_OST_ZVOL, DS_MODE_OWNER, 712bb0ade09Sahrens &state.zv_objset); 713bb0ade09Sahrens if (error != 0) 714bb0ade09Sahrens goto out; 715bb0ade09Sahrens zv = &state; 716fa9e4066Sahrens } 717e7cbe64fSgw old_volsize = zv->zv_volsize; 718fa9e4066Sahrens 7195c5460e9Seschrock if ((error = dmu_object_info(zv->zv_objset, ZVOL_OBJ, &doi)) != 0 || 720e9dbad6fSeschrock (error = zvol_check_volsize(volsize, 721bb0ade09Sahrens doi.doi_data_block_size)) != 0) 722bb0ade09Sahrens goto out; 7235c5460e9Seschrock 724e7cbe64fSgw if (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY)) { 725bb0ade09Sahrens error = EROFS; 726bb0ade09Sahrens goto out; 727fa9e4066Sahrens } 728fa9e4066Sahrens 729e7cbe64fSgw error = zvol_update_volsize(zv, maj, volsize); 730fa9e4066Sahrens 731e7cbe64fSgw /* 732e7cbe64fSgw * Reinitialize the dump area to the new size. If we 733e7cbe64fSgw * failed to resize the dump area then restore the it back to 734e7cbe64fSgw * it's original size. 735e7cbe64fSgw */ 736e7cbe64fSgw if (error == 0 && zv->zv_flags & ZVOL_DUMPIFIED) { 737e7cbe64fSgw if ((error = zvol_dumpify(zv)) != 0 || 738e7cbe64fSgw (error = dumpvp_resize()) != 0) { 739e7cbe64fSgw (void) zvol_update_volsize(zv, maj, old_volsize); 740e7cbe64fSgw error = zvol_dumpify(zv); 741e7cbe64fSgw } 742fa9e4066Sahrens } 743fa9e4066Sahrens 744bb0ade09Sahrens out: 745bb0ade09Sahrens if (state.zv_objset) 746bb0ade09Sahrens dmu_objset_close(state.zv_objset); 747bb0ade09Sahrens 748fa9e4066Sahrens mutex_exit(&zvol_state_lock); 749fa9e4066Sahrens 750fa9e4066Sahrens return (error); 751fa9e4066Sahrens } 752fa9e4066Sahrens 753fa9e4066Sahrens int 754e9dbad6fSeschrock zvol_set_volblocksize(const char *name, uint64_t volblocksize) 755fa9e4066Sahrens { 756fa9e4066Sahrens zvol_state_t *zv; 757fa9e4066Sahrens dmu_tx_t *tx; 758fa9e4066Sahrens int error; 75988b7b0f2SMatthew Ahrens boolean_t needlock; 760fa9e4066Sahrens 76188b7b0f2SMatthew Ahrens /* 76288b7b0f2SMatthew Ahrens * The lock may already be held if we are being called from 76388b7b0f2SMatthew Ahrens * zvol_dump_init(). 76488b7b0f2SMatthew Ahrens */ 76588b7b0f2SMatthew Ahrens needlock = !MUTEX_HELD(&zvol_state_lock); 76688b7b0f2SMatthew Ahrens if (needlock) 76788b7b0f2SMatthew Ahrens mutex_enter(&zvol_state_lock); 768fa9e4066Sahrens 769e9dbad6fSeschrock if ((zv = zvol_minor_lookup(name)) == NULL) { 77088b7b0f2SMatthew Ahrens if (needlock) 77188b7b0f2SMatthew Ahrens mutex_exit(&zvol_state_lock); 772fa9e4066Sahrens return (ENXIO); 773fa9e4066Sahrens } 774e7cbe64fSgw if (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY)) { 77588b7b0f2SMatthew Ahrens if (needlock) 77688b7b0f2SMatthew Ahrens mutex_exit(&zvol_state_lock); 777fa9e4066Sahrens return (EROFS); 778fa9e4066Sahrens } 779fa9e4066Sahrens 780fa9e4066Sahrens tx = dmu_tx_create(zv->zv_objset); 781fa9e4066Sahrens dmu_tx_hold_bonus(tx, ZVOL_OBJ); 782fa9e4066Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 783fa9e4066Sahrens if (error) { 784fa9e4066Sahrens dmu_tx_abort(tx); 785fa9e4066Sahrens } else { 786fa9e4066Sahrens error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ, 787e9dbad6fSeschrock volblocksize, 0, tx); 788fa9e4066Sahrens if (error == ENOTSUP) 789fa9e4066Sahrens error = EBUSY; 790fa9e4066Sahrens dmu_tx_commit(tx); 79188b7b0f2SMatthew Ahrens if (error == 0) 79288b7b0f2SMatthew Ahrens zv->zv_volblocksize = volblocksize; 793fa9e4066Sahrens } 794fa9e4066Sahrens 79588b7b0f2SMatthew Ahrens if (needlock) 79688b7b0f2SMatthew Ahrens mutex_exit(&zvol_state_lock); 797fa9e4066Sahrens 798fa9e4066Sahrens return (error); 799fa9e4066Sahrens } 800fa9e4066Sahrens 801fa9e4066Sahrens /*ARGSUSED*/ 802fa9e4066Sahrens int 803fa9e4066Sahrens zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr) 804fa9e4066Sahrens { 805fa9e4066Sahrens minor_t minor = getminor(*devp); 806fa9e4066Sahrens zvol_state_t *zv; 807fa9e4066Sahrens 808fa9e4066Sahrens if (minor == 0) /* This is the control device */ 809fa9e4066Sahrens return (0); 810fa9e4066Sahrens 811fa9e4066Sahrens mutex_enter(&zvol_state_lock); 812fa9e4066Sahrens 813fa9e4066Sahrens zv = ddi_get_soft_state(zvol_state, minor); 814fa9e4066Sahrens if (zv == NULL) { 815fa9e4066Sahrens mutex_exit(&zvol_state_lock); 816fa9e4066Sahrens return (ENXIO); 817fa9e4066Sahrens } 818fa9e4066Sahrens 819fa9e4066Sahrens ASSERT(zv->zv_objset != NULL); 820fa9e4066Sahrens 821fa9e4066Sahrens if ((flag & FWRITE) && 822e7cbe64fSgw (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY))) { 823fa9e4066Sahrens mutex_exit(&zvol_state_lock); 824fa9e4066Sahrens return (EROFS); 825fa9e4066Sahrens } 826c7f714e2SEric Taylor if (zv->zv_flags & ZVOL_EXCL) { 827c7f714e2SEric Taylor mutex_exit(&zvol_state_lock); 828c7f714e2SEric Taylor return (EBUSY); 829c7f714e2SEric Taylor } 830c7f714e2SEric Taylor if (flag & FEXCL) { 831c7f714e2SEric Taylor if (zv->zv_total_opens != 0) { 832c7f714e2SEric Taylor mutex_exit(&zvol_state_lock); 833c7f714e2SEric Taylor return (EBUSY); 834c7f714e2SEric Taylor } 835c7f714e2SEric Taylor zv->zv_flags |= ZVOL_EXCL; 836c7f714e2SEric Taylor } 837fa9e4066Sahrens 838fa9e4066Sahrens if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) { 839fa9e4066Sahrens zv->zv_open_count[otyp]++; 840fa9e4066Sahrens zv->zv_total_opens++; 841fa9e4066Sahrens } 842fa9e4066Sahrens 843fa9e4066Sahrens mutex_exit(&zvol_state_lock); 844fa9e4066Sahrens 845fa9e4066Sahrens return (0); 846fa9e4066Sahrens } 847fa9e4066Sahrens 848fa9e4066Sahrens /*ARGSUSED*/ 849fa9e4066Sahrens int 850fa9e4066Sahrens zvol_close(dev_t dev, int flag, int otyp, cred_t *cr) 851fa9e4066Sahrens { 852fa9e4066Sahrens minor_t minor = getminor(dev); 853fa9e4066Sahrens zvol_state_t *zv; 854fa9e4066Sahrens 855fa9e4066Sahrens if (minor == 0) /* This is the control device */ 856fa9e4066Sahrens return (0); 857fa9e4066Sahrens 858fa9e4066Sahrens mutex_enter(&zvol_state_lock); 859fa9e4066Sahrens 860fa9e4066Sahrens zv = ddi_get_soft_state(zvol_state, minor); 861fa9e4066Sahrens if (zv == NULL) { 862fa9e4066Sahrens mutex_exit(&zvol_state_lock); 863fa9e4066Sahrens return (ENXIO); 864fa9e4066Sahrens } 865fa9e4066Sahrens 866c7f714e2SEric Taylor if (zv->zv_flags & ZVOL_EXCL) { 867c7f714e2SEric Taylor ASSERT(zv->zv_total_opens == 1); 868c7f714e2SEric Taylor zv->zv_flags &= ~ZVOL_EXCL; 869fa9e4066Sahrens } 870fa9e4066Sahrens 871fa9e4066Sahrens /* 872fa9e4066Sahrens * If the open count is zero, this is a spurious close. 873fa9e4066Sahrens * That indicates a bug in the kernel / DDI framework. 874fa9e4066Sahrens */ 875fa9e4066Sahrens ASSERT(zv->zv_open_count[otyp] != 0); 876fa9e4066Sahrens ASSERT(zv->zv_total_opens != 0); 877fa9e4066Sahrens 878fa9e4066Sahrens /* 879fa9e4066Sahrens * You may get multiple opens, but only one close. 880fa9e4066Sahrens */ 881fa9e4066Sahrens zv->zv_open_count[otyp]--; 882fa9e4066Sahrens zv->zv_total_opens--; 883fa9e4066Sahrens 884fa9e4066Sahrens mutex_exit(&zvol_state_lock); 885fa9e4066Sahrens 886fa9e4066Sahrens return (0); 887fa9e4066Sahrens } 888fa9e4066Sahrens 889feb08c6bSbillm static void 89067bd71c6Sperrin zvol_get_done(dmu_buf_t *db, void *vzgd) 89167bd71c6Sperrin { 89267bd71c6Sperrin zgd_t *zgd = (zgd_t *)vzgd; 893c2e6a7d6Sperrin rl_t *rl = zgd->zgd_rl; 89467bd71c6Sperrin 89567bd71c6Sperrin dmu_buf_rele(db, vzgd); 896c2e6a7d6Sperrin zfs_range_unlock(rl); 89717f17c2dSbonwick zil_add_block(zgd->zgd_zilog, zgd->zgd_bp); 89867bd71c6Sperrin kmem_free(zgd, sizeof (zgd_t)); 89967bd71c6Sperrin } 90067bd71c6Sperrin 90167bd71c6Sperrin /* 90267bd71c6Sperrin * Get data to generate a TX_WRITE intent log record. 90367bd71c6Sperrin */ 904feb08c6bSbillm static int 90567bd71c6Sperrin zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 90667bd71c6Sperrin { 90767bd71c6Sperrin zvol_state_t *zv = arg; 90867bd71c6Sperrin objset_t *os = zv->zv_objset; 90967bd71c6Sperrin dmu_buf_t *db; 910c2e6a7d6Sperrin rl_t *rl; 91167bd71c6Sperrin zgd_t *zgd; 912c2e6a7d6Sperrin uint64_t boff; /* block starting offset */ 913c2e6a7d6Sperrin int dlen = lr->lr_length; /* length of user data */ 91467bd71c6Sperrin int error; 91567bd71c6Sperrin 91667bd71c6Sperrin ASSERT(zio); 917c2e6a7d6Sperrin ASSERT(dlen != 0); 918feb08c6bSbillm 919c2e6a7d6Sperrin /* 920c2e6a7d6Sperrin * Write records come in two flavors: immediate and indirect. 921c2e6a7d6Sperrin * For small writes it's cheaper to store the data with the 922c2e6a7d6Sperrin * log record (immediate); for large writes it's cheaper to 923c2e6a7d6Sperrin * sync the data and get a pointer to it (indirect) so that 924c2e6a7d6Sperrin * we don't have to write the data twice. 925c2e6a7d6Sperrin */ 926c2e6a7d6Sperrin if (buf != NULL) /* immediate write */ 927c2e6a7d6Sperrin return (dmu_read(os, ZVOL_OBJ, lr->lr_offset, dlen, buf)); 92867bd71c6Sperrin 92967bd71c6Sperrin zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP); 93067bd71c6Sperrin zgd->zgd_zilog = zv->zv_zilog; 93167bd71c6Sperrin zgd->zgd_bp = &lr->lr_blkptr; 93267bd71c6Sperrin 93367bd71c6Sperrin /* 934c2e6a7d6Sperrin * Lock the range of the block to ensure that when the data is 935e7cbe64fSgw * written out and its checksum is being calculated that no other 936c2e6a7d6Sperrin * thread can change the block. 93767bd71c6Sperrin */ 938c2e6a7d6Sperrin boff = P2ALIGN_TYPED(lr->lr_offset, zv->zv_volblocksize, uint64_t); 939c2e6a7d6Sperrin rl = zfs_range_lock(&zv->zv_znode, boff, zv->zv_volblocksize, 940c2e6a7d6Sperrin RL_READER); 941c2e6a7d6Sperrin zgd->zgd_rl = rl; 942c2e6a7d6Sperrin 943c2e6a7d6Sperrin VERIFY(0 == dmu_buf_hold(os, ZVOL_OBJ, lr->lr_offset, zgd, &db)); 94467bd71c6Sperrin error = dmu_sync(zio, db, &lr->lr_blkptr, 94567bd71c6Sperrin lr->lr_common.lrc_txg, zvol_get_done, zgd); 946feb08c6bSbillm if (error == 0) 94717f17c2dSbonwick zil_add_block(zv->zv_zilog, &lr->lr_blkptr); 94867bd71c6Sperrin /* 94967bd71c6Sperrin * If we get EINPROGRESS, then we need to wait for a 95067bd71c6Sperrin * write IO initiated by dmu_sync() to complete before 95167bd71c6Sperrin * we can release this dbuf. We will finish everything 95267bd71c6Sperrin * up in the zvol_get_done() callback. 95367bd71c6Sperrin */ 95467bd71c6Sperrin if (error == EINPROGRESS) 95567bd71c6Sperrin return (0); 95667bd71c6Sperrin dmu_buf_rele(db, zgd); 957c2e6a7d6Sperrin zfs_range_unlock(rl); 95867bd71c6Sperrin kmem_free(zgd, sizeof (zgd_t)); 95967bd71c6Sperrin return (error); 96067bd71c6Sperrin } 96167bd71c6Sperrin 962a24e15ceSperrin /* 963a24e15ceSperrin * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions. 96422ac5be4Sperrin * 96522ac5be4Sperrin * We store data in the log buffers if it's small enough. 96667bd71c6Sperrin * Otherwise we will later flush the data out via dmu_sync(). 96722ac5be4Sperrin */ 96867bd71c6Sperrin ssize_t zvol_immediate_write_sz = 32768; 96922ac5be4Sperrin 970feb08c6bSbillm static void 971feb08c6bSbillm zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t len) 97222ac5be4Sperrin { 973feb08c6bSbillm uint32_t blocksize = zv->zv_volblocksize; 974*1209a471SNeil Perrin zilog_t *zilog = zv->zv_zilog; 97522ac5be4Sperrin lr_write_t *lr; 97622ac5be4Sperrin 977*1209a471SNeil Perrin if (zilog->zl_replay) { 978*1209a471SNeil Perrin dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 979*1209a471SNeil Perrin zilog->zl_replayed_seq[dmu_tx_get_txg(tx) & TXG_MASK] = 980*1209a471SNeil Perrin zilog->zl_replaying_seq; 981*1209a471SNeil Perrin return; 982*1209a471SNeil Perrin } 983*1209a471SNeil Perrin 984a24e15ceSperrin while (len) { 985feb08c6bSbillm ssize_t nbytes = MIN(len, blocksize - P2PHASE(off, blocksize)); 986feb08c6bSbillm itx_t *itx = zil_itx_create(TX_WRITE, sizeof (*lr)); 987feb08c6bSbillm 988feb08c6bSbillm itx->itx_wr_state = 989feb08c6bSbillm len > zvol_immediate_write_sz ? WR_INDIRECT : WR_NEED_COPY; 990feb08c6bSbillm itx->itx_private = zv; 991feb08c6bSbillm lr = (lr_write_t *)&itx->itx_lr; 992feb08c6bSbillm lr->lr_foid = ZVOL_OBJ; 993feb08c6bSbillm lr->lr_offset = off; 994feb08c6bSbillm lr->lr_length = nbytes; 995feb08c6bSbillm lr->lr_blkoff = off - P2ALIGN_TYPED(off, blocksize, uint64_t); 996feb08c6bSbillm BP_ZERO(&lr->lr_blkptr); 997feb08c6bSbillm 998*1209a471SNeil Perrin (void) zil_itx_assign(zilog, itx, tx); 999a24e15ceSperrin len -= nbytes; 1000a24e15ceSperrin off += nbytes; 100122ac5be4Sperrin } 100222ac5be4Sperrin } 100322ac5be4Sperrin 100488b7b0f2SMatthew Ahrens static int 100588b7b0f2SMatthew Ahrens zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t size, 100688b7b0f2SMatthew Ahrens boolean_t doread, boolean_t isdump) 1007e7cbe64fSgw { 1008e7cbe64fSgw vdev_disk_t *dvd; 1009e7cbe64fSgw int c; 1010e7cbe64fSgw int numerrors = 0; 1011e7cbe64fSgw 1012e7cbe64fSgw for (c = 0; c < vd->vdev_children; c++) { 101388b7b0f2SMatthew Ahrens ASSERT(vd->vdev_ops == &vdev_mirror_ops); 101488b7b0f2SMatthew Ahrens int err = zvol_dumpio_vdev(vd->vdev_child[c], 101588b7b0f2SMatthew Ahrens addr, offset, size, doread, isdump); 101688b7b0f2SMatthew Ahrens if (err != 0) { 1017e7cbe64fSgw numerrors++; 101888b7b0f2SMatthew Ahrens } else if (doread) { 1019e7cbe64fSgw break; 1020e7cbe64fSgw } 1021e7cbe64fSgw } 1022e7cbe64fSgw 1023e7cbe64fSgw if (!vd->vdev_ops->vdev_op_leaf) 1024e7cbe64fSgw return (numerrors < vd->vdev_children ? 0 : EIO); 1025e7cbe64fSgw 1026dc0bb255SEric Taylor if (doread && !vdev_readable(vd)) 1027dc0bb255SEric Taylor return (EIO); 1028dc0bb255SEric Taylor else if (!doread && !vdev_writeable(vd)) 1029e7cbe64fSgw return (EIO); 1030e7cbe64fSgw 1031e7cbe64fSgw dvd = vd->vdev_tsd; 1032e7cbe64fSgw ASSERT3P(dvd, !=, NULL); 1033e7cbe64fSgw offset += VDEV_LABEL_START_SIZE; 1034e7cbe64fSgw 1035e7cbe64fSgw if (ddi_in_panic() || isdump) { 103688b7b0f2SMatthew Ahrens ASSERT(!doread); 103788b7b0f2SMatthew Ahrens if (doread) 1038e7cbe64fSgw return (EIO); 1039e7cbe64fSgw return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset), 1040e7cbe64fSgw lbtodb(size))); 1041e7cbe64fSgw } else { 1042e7cbe64fSgw return (vdev_disk_physio(dvd->vd_lh, addr, size, offset, 104388b7b0f2SMatthew Ahrens doread ? B_READ : B_WRITE)); 1044e7cbe64fSgw } 1045e7cbe64fSgw } 1046e7cbe64fSgw 104788b7b0f2SMatthew Ahrens static int 104888b7b0f2SMatthew Ahrens zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size, 104988b7b0f2SMatthew Ahrens boolean_t doread, boolean_t isdump) 1050e7cbe64fSgw { 1051e7cbe64fSgw vdev_t *vd; 1052e7cbe64fSgw int error; 105388b7b0f2SMatthew Ahrens zvol_extent_t *ze; 1054e7cbe64fSgw spa_t *spa = dmu_objset_spa(zv->zv_objset); 1055e7cbe64fSgw 105688b7b0f2SMatthew Ahrens /* Must be sector aligned, and not stradle a block boundary. */ 105788b7b0f2SMatthew Ahrens if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) || 105888b7b0f2SMatthew Ahrens P2BOUNDARY(offset, size, zv->zv_volblocksize)) { 1059e7cbe64fSgw return (EINVAL); 106088b7b0f2SMatthew Ahrens } 106188b7b0f2SMatthew Ahrens ASSERT(size <= zv->zv_volblocksize); 1062e7cbe64fSgw 106388b7b0f2SMatthew Ahrens /* Locate the extent this belongs to */ 106488b7b0f2SMatthew Ahrens ze = list_head(&zv->zv_extents); 106588b7b0f2SMatthew Ahrens while (offset >= ze->ze_nblks * zv->zv_volblocksize) { 106688b7b0f2SMatthew Ahrens offset -= ze->ze_nblks * zv->zv_volblocksize; 106788b7b0f2SMatthew Ahrens ze = list_next(&zv->zv_extents, ze); 106888b7b0f2SMatthew Ahrens } 1069e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 107088b7b0f2SMatthew Ahrens vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva)); 107188b7b0f2SMatthew Ahrens offset += DVA_GET_OFFSET(&ze->ze_dva); 107288b7b0f2SMatthew Ahrens error = zvol_dumpio_vdev(vd, addr, offset, size, doread, isdump); 1073e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 1074e7cbe64fSgw return (error); 1075e7cbe64fSgw } 1076e7cbe64fSgw 1077fa9e4066Sahrens int 1078fa9e4066Sahrens zvol_strategy(buf_t *bp) 1079fa9e4066Sahrens { 1080fa9e4066Sahrens zvol_state_t *zv = ddi_get_soft_state(zvol_state, getminor(bp->b_edev)); 1081fa9e4066Sahrens uint64_t off, volsize; 108288b7b0f2SMatthew Ahrens size_t resid; 1083fa9e4066Sahrens char *addr; 108422ac5be4Sperrin objset_t *os; 1085c2e6a7d6Sperrin rl_t *rl; 1086fa9e4066Sahrens int error = 0; 108788b7b0f2SMatthew Ahrens boolean_t doread = bp->b_flags & B_READ; 108888b7b0f2SMatthew Ahrens boolean_t is_dump = zv->zv_flags & ZVOL_DUMPIFIED; 1089fa9e4066Sahrens 1090fa9e4066Sahrens if (zv == NULL) { 1091fa9e4066Sahrens bioerror(bp, ENXIO); 1092fa9e4066Sahrens biodone(bp); 1093fa9e4066Sahrens return (0); 1094fa9e4066Sahrens } 1095fa9e4066Sahrens 1096fa9e4066Sahrens if (getminor(bp->b_edev) == 0) { 1097fa9e4066Sahrens bioerror(bp, EINVAL); 1098fa9e4066Sahrens biodone(bp); 1099fa9e4066Sahrens return (0); 1100fa9e4066Sahrens } 1101fa9e4066Sahrens 1102e7cbe64fSgw if (!(bp->b_flags & B_READ) && 1103e7cbe64fSgw (zv->zv_flags & ZVOL_RDONLY || 1104e7cbe64fSgw zv->zv_mode & DS_MODE_READONLY)) { 1105fa9e4066Sahrens bioerror(bp, EROFS); 1106fa9e4066Sahrens biodone(bp); 1107fa9e4066Sahrens return (0); 1108fa9e4066Sahrens } 1109fa9e4066Sahrens 1110fa9e4066Sahrens off = ldbtob(bp->b_blkno); 1111fa9e4066Sahrens volsize = zv->zv_volsize; 1112fa9e4066Sahrens 111322ac5be4Sperrin os = zv->zv_objset; 111422ac5be4Sperrin ASSERT(os != NULL); 1115fa9e4066Sahrens 1116fa9e4066Sahrens bp_mapin(bp); 1117fa9e4066Sahrens addr = bp->b_un.b_addr; 1118fa9e4066Sahrens resid = bp->b_bcount; 1119fa9e4066Sahrens 112088b7b0f2SMatthew Ahrens if (resid > 0 && (off < 0 || off >= volsize)) { 112188b7b0f2SMatthew Ahrens bioerror(bp, EIO); 112288b7b0f2SMatthew Ahrens biodone(bp); 112388b7b0f2SMatthew Ahrens return (0); 112488b7b0f2SMatthew Ahrens } 112573ec3d9cSgw 1126a24e15ceSperrin /* 1127a24e15ceSperrin * There must be no buffer changes when doing a dmu_sync() because 1128a24e15ceSperrin * we can't change the data whilst calculating the checksum. 1129a24e15ceSperrin */ 1130c2e6a7d6Sperrin rl = zfs_range_lock(&zv->zv_znode, off, resid, 113188b7b0f2SMatthew Ahrens doread ? RL_READER : RL_WRITER); 1132fa9e4066Sahrens 1133e7cbe64fSgw while (resid != 0 && off < volsize) { 113488b7b0f2SMatthew Ahrens size_t size = MIN(resid, zvol_maxphys); 1135e7cbe64fSgw if (is_dump) { 1136e7cbe64fSgw size = MIN(size, P2END(off, zv->zv_volblocksize) - off); 113788b7b0f2SMatthew Ahrens error = zvol_dumpio(zv, addr, off, size, 113888b7b0f2SMatthew Ahrens doread, B_FALSE); 113988b7b0f2SMatthew Ahrens } else if (doread) { 1140a24e15ceSperrin error = dmu_read(os, ZVOL_OBJ, off, size, addr); 1141fa9e4066Sahrens } else { 114222ac5be4Sperrin dmu_tx_t *tx = dmu_tx_create(os); 1143fa9e4066Sahrens dmu_tx_hold_write(tx, ZVOL_OBJ, off, size); 1144fa9e4066Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 1145fa9e4066Sahrens if (error) { 1146fa9e4066Sahrens dmu_tx_abort(tx); 1147fa9e4066Sahrens } else { 114822ac5be4Sperrin dmu_write(os, ZVOL_OBJ, off, size, addr, tx); 1149feb08c6bSbillm zvol_log_write(zv, tx, off, size); 1150fa9e4066Sahrens dmu_tx_commit(tx); 1151fa9e4066Sahrens } 1152fa9e4066Sahrens } 1153b87f3af3Sperrin if (error) { 1154b87f3af3Sperrin /* convert checksum errors into IO errors */ 1155b87f3af3Sperrin if (error == ECKSUM) 1156b87f3af3Sperrin error = EIO; 1157fa9e4066Sahrens break; 1158b87f3af3Sperrin } 1159fa9e4066Sahrens off += size; 1160fa9e4066Sahrens addr += size; 1161fa9e4066Sahrens resid -= size; 1162fa9e4066Sahrens } 1163c2e6a7d6Sperrin zfs_range_unlock(rl); 1164fa9e4066Sahrens 1165fa9e4066Sahrens if ((bp->b_resid = resid) == bp->b_bcount) 1166fa9e4066Sahrens bioerror(bp, off > volsize ? EINVAL : error); 1167fa9e4066Sahrens 116888b7b0f2SMatthew Ahrens if (!(bp->b_flags & B_ASYNC) && !doread && !zil_disable && !is_dump) 1169feb08c6bSbillm zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ); 1170feb08c6bSbillm biodone(bp); 117122ac5be4Sperrin 1172fa9e4066Sahrens return (0); 1173fa9e4066Sahrens } 1174fa9e4066Sahrens 117567bd71c6Sperrin /* 117667bd71c6Sperrin * Set the buffer count to the zvol maximum transfer. 117767bd71c6Sperrin * Using our own routine instead of the default minphys() 117867bd71c6Sperrin * means that for larger writes we write bigger buffers on X86 117967bd71c6Sperrin * (128K instead of 56K) and flush the disk write cache less often 118067bd71c6Sperrin * (every zvol_maxphys - currently 1MB) instead of minphys (currently 118167bd71c6Sperrin * 56K on X86 and 128K on sparc). 118267bd71c6Sperrin */ 118367bd71c6Sperrin void 118467bd71c6Sperrin zvol_minphys(struct buf *bp) 118567bd71c6Sperrin { 118667bd71c6Sperrin if (bp->b_bcount > zvol_maxphys) 118767bd71c6Sperrin bp->b_bcount = zvol_maxphys; 118867bd71c6Sperrin } 118967bd71c6Sperrin 1190e7cbe64fSgw int 1191e7cbe64fSgw zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks) 1192e7cbe64fSgw { 1193e7cbe64fSgw minor_t minor = getminor(dev); 1194e7cbe64fSgw zvol_state_t *zv; 1195e7cbe64fSgw int error = 0; 1196e7cbe64fSgw uint64_t size; 1197e7cbe64fSgw uint64_t boff; 1198e7cbe64fSgw uint64_t resid; 1199e7cbe64fSgw 1200e7cbe64fSgw if (minor == 0) /* This is the control device */ 1201e7cbe64fSgw return (ENXIO); 1202e7cbe64fSgw 1203e7cbe64fSgw zv = ddi_get_soft_state(zvol_state, minor); 1204e7cbe64fSgw if (zv == NULL) 1205e7cbe64fSgw return (ENXIO); 1206e7cbe64fSgw 1207e7cbe64fSgw boff = ldbtob(blkno); 1208e7cbe64fSgw resid = ldbtob(nblocks); 120988b7b0f2SMatthew Ahrens 121088b7b0f2SMatthew Ahrens VERIFY3U(boff + resid, <=, zv->zv_volsize); 121188b7b0f2SMatthew Ahrens 1212e7cbe64fSgw while (resid) { 1213e7cbe64fSgw size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff); 121488b7b0f2SMatthew Ahrens error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE); 1215e7cbe64fSgw if (error) 1216e7cbe64fSgw break; 1217e7cbe64fSgw boff += size; 1218e7cbe64fSgw addr += size; 1219e7cbe64fSgw resid -= size; 1220e7cbe64fSgw } 1221e7cbe64fSgw 1222e7cbe64fSgw return (error); 1223e7cbe64fSgw } 1224e7cbe64fSgw 1225fa9e4066Sahrens /*ARGSUSED*/ 1226fa9e4066Sahrens int 1227feb08c6bSbillm zvol_read(dev_t dev, uio_t *uio, cred_t *cr) 1228fa9e4066Sahrens { 1229c7ca1008Sgw minor_t minor = getminor(dev); 1230c7ca1008Sgw zvol_state_t *zv; 123173ec3d9cSgw uint64_t volsize; 1232c2e6a7d6Sperrin rl_t *rl; 1233feb08c6bSbillm int error = 0; 1234fa9e4066Sahrens 1235c7ca1008Sgw if (minor == 0) /* This is the control device */ 1236c7ca1008Sgw return (ENXIO); 1237c7ca1008Sgw 1238c7ca1008Sgw zv = ddi_get_soft_state(zvol_state, minor); 1239c7ca1008Sgw if (zv == NULL) 1240c7ca1008Sgw return (ENXIO); 1241c7ca1008Sgw 124273ec3d9cSgw volsize = zv->zv_volsize; 124373ec3d9cSgw if (uio->uio_resid > 0 && 124473ec3d9cSgw (uio->uio_loffset < 0 || uio->uio_loffset >= volsize)) 124573ec3d9cSgw return (EIO); 124673ec3d9cSgw 124788b7b0f2SMatthew Ahrens if (zv->zv_flags & ZVOL_DUMPIFIED) { 124888b7b0f2SMatthew Ahrens error = physio(zvol_strategy, NULL, dev, B_READ, 124988b7b0f2SMatthew Ahrens zvol_minphys, uio); 125088b7b0f2SMatthew Ahrens return (error); 125188b7b0f2SMatthew Ahrens } 125288b7b0f2SMatthew Ahrens 1253c2e6a7d6Sperrin rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 1254c2e6a7d6Sperrin RL_READER); 125573ec3d9cSgw while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 1256feb08c6bSbillm uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 1257fa9e4066Sahrens 125873ec3d9cSgw /* don't read past the end */ 125973ec3d9cSgw if (bytes > volsize - uio->uio_loffset) 126073ec3d9cSgw bytes = volsize - uio->uio_loffset; 126173ec3d9cSgw 1262feb08c6bSbillm error = dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes); 1263b87f3af3Sperrin if (error) { 1264b87f3af3Sperrin /* convert checksum errors into IO errors */ 1265b87f3af3Sperrin if (error == ECKSUM) 1266b87f3af3Sperrin error = EIO; 1267feb08c6bSbillm break; 1268b87f3af3Sperrin } 1269feb08c6bSbillm } 1270c2e6a7d6Sperrin zfs_range_unlock(rl); 1271feb08c6bSbillm return (error); 1272fa9e4066Sahrens } 1273fa9e4066Sahrens 1274fa9e4066Sahrens /*ARGSUSED*/ 1275fa9e4066Sahrens int 1276feb08c6bSbillm zvol_write(dev_t dev, uio_t *uio, cred_t *cr) 1277fa9e4066Sahrens { 1278c7ca1008Sgw minor_t minor = getminor(dev); 1279c7ca1008Sgw zvol_state_t *zv; 128073ec3d9cSgw uint64_t volsize; 1281c2e6a7d6Sperrin rl_t *rl; 1282feb08c6bSbillm int error = 0; 1283feb08c6bSbillm 1284c7ca1008Sgw if (minor == 0) /* This is the control device */ 1285c7ca1008Sgw return (ENXIO); 1286c7ca1008Sgw 1287c7ca1008Sgw zv = ddi_get_soft_state(zvol_state, minor); 1288c7ca1008Sgw if (zv == NULL) 1289c7ca1008Sgw return (ENXIO); 1290c7ca1008Sgw 129173ec3d9cSgw volsize = zv->zv_volsize; 129273ec3d9cSgw if (uio->uio_resid > 0 && 129373ec3d9cSgw (uio->uio_loffset < 0 || uio->uio_loffset >= volsize)) 129473ec3d9cSgw return (EIO); 129573ec3d9cSgw 1296e7cbe64fSgw if (zv->zv_flags & ZVOL_DUMPIFIED) { 1297e7cbe64fSgw error = physio(zvol_strategy, NULL, dev, B_WRITE, 1298e7cbe64fSgw zvol_minphys, uio); 1299e7cbe64fSgw return (error); 1300e7cbe64fSgw } 1301e7cbe64fSgw 1302c2e6a7d6Sperrin rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 1303c2e6a7d6Sperrin RL_WRITER); 130473ec3d9cSgw while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 1305feb08c6bSbillm uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 1306feb08c6bSbillm uint64_t off = uio->uio_loffset; 1307feb08c6bSbillm dmu_tx_t *tx = dmu_tx_create(zv->zv_objset); 130873ec3d9cSgw 130973ec3d9cSgw if (bytes > volsize - off) /* don't write past the end */ 131073ec3d9cSgw bytes = volsize - off; 131173ec3d9cSgw 1312feb08c6bSbillm dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 1313feb08c6bSbillm error = dmu_tx_assign(tx, TXG_WAIT); 1314feb08c6bSbillm if (error) { 1315feb08c6bSbillm dmu_tx_abort(tx); 1316feb08c6bSbillm break; 1317feb08c6bSbillm } 1318feb08c6bSbillm error = dmu_write_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes, tx); 1319feb08c6bSbillm if (error == 0) 1320feb08c6bSbillm zvol_log_write(zv, tx, off, bytes); 1321feb08c6bSbillm dmu_tx_commit(tx); 1322feb08c6bSbillm 1323feb08c6bSbillm if (error) 1324feb08c6bSbillm break; 1325feb08c6bSbillm } 1326c2e6a7d6Sperrin zfs_range_unlock(rl); 1327feb08c6bSbillm return (error); 1328fa9e4066Sahrens } 1329fa9e4066Sahrens 1330c7f714e2SEric Taylor int 1331c7f714e2SEric Taylor zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs) 1332c7f714e2SEric Taylor { 1333c7f714e2SEric Taylor struct uuid uuid = EFI_RESERVED; 1334c7f714e2SEric Taylor efi_gpe_t gpe = { 0 }; 1335c7f714e2SEric Taylor uint32_t crc; 1336c7f714e2SEric Taylor dk_efi_t efi; 1337c7f714e2SEric Taylor int length; 1338c7f714e2SEric Taylor char *ptr; 1339c7f714e2SEric Taylor 1340c7f714e2SEric Taylor if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag)) 1341c7f714e2SEric Taylor return (EFAULT); 1342c7f714e2SEric Taylor ptr = (char *)(uintptr_t)efi.dki_data_64; 1343c7f714e2SEric Taylor length = efi.dki_length; 1344c7f714e2SEric Taylor /* 1345c7f714e2SEric Taylor * Some clients may attempt to request a PMBR for the 1346c7f714e2SEric Taylor * zvol. Currently this interface will return EINVAL to 1347c7f714e2SEric Taylor * such requests. These requests could be supported by 1348c7f714e2SEric Taylor * adding a check for lba == 0 and consing up an appropriate 1349c7f714e2SEric Taylor * PMBR. 1350c7f714e2SEric Taylor */ 1351c7f714e2SEric Taylor if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0) 1352c7f714e2SEric Taylor return (EINVAL); 1353c7f714e2SEric Taylor 1354c7f714e2SEric Taylor gpe.efi_gpe_StartingLBA = LE_64(34ULL); 1355c7f714e2SEric Taylor gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1); 1356c7f714e2SEric Taylor UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid); 1357c7f714e2SEric Taylor 1358c7f714e2SEric Taylor if (efi.dki_lba == 1) { 1359c7f714e2SEric Taylor efi_gpt_t gpt = { 0 }; 1360c7f714e2SEric Taylor 1361c7f714e2SEric Taylor gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE); 1362c7f714e2SEric Taylor gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT); 1363c7f714e2SEric Taylor gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt)); 1364c7f714e2SEric Taylor gpt.efi_gpt_MyLBA = LE_64(1ULL); 1365c7f714e2SEric Taylor gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL); 1366c7f714e2SEric Taylor gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1); 1367c7f714e2SEric Taylor gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL); 1368c7f714e2SEric Taylor gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1); 1369c7f714e2SEric Taylor gpt.efi_gpt_SizeOfPartitionEntry = 1370c7f714e2SEric Taylor LE_32(sizeof (efi_gpe_t)); 1371c7f714e2SEric Taylor CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table); 1372c7f714e2SEric Taylor gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc); 1373c7f714e2SEric Taylor CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table); 1374c7f714e2SEric Taylor gpt.efi_gpt_HeaderCRC32 = LE_32(~crc); 1375c7f714e2SEric Taylor if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length), 1376c7f714e2SEric Taylor flag)) 1377c7f714e2SEric Taylor return (EFAULT); 1378c7f714e2SEric Taylor ptr += sizeof (gpt); 1379c7f714e2SEric Taylor length -= sizeof (gpt); 1380c7f714e2SEric Taylor } 1381c7f714e2SEric Taylor if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe), 1382c7f714e2SEric Taylor length), flag)) 1383c7f714e2SEric Taylor return (EFAULT); 1384c7f714e2SEric Taylor return (0); 1385c7f714e2SEric Taylor } 1386c7f714e2SEric Taylor 1387fa9e4066Sahrens /* 1388fa9e4066Sahrens * Dirtbag ioctls to support mkfs(1M) for UFS filesystems. See dkio(7I). 1389fa9e4066Sahrens */ 1390fa9e4066Sahrens /*ARGSUSED*/ 1391fa9e4066Sahrens int 1392fa9e4066Sahrens zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 1393fa9e4066Sahrens { 1394fa9e4066Sahrens zvol_state_t *zv; 1395af2c4821Smaybee struct dk_cinfo dki; 1396fa9e4066Sahrens struct dk_minfo dkm; 1397af2c4821Smaybee struct dk_callback *dkc; 1398fa9e4066Sahrens int error = 0; 1399e7cbe64fSgw rl_t *rl; 1400fa9e4066Sahrens 1401fa9e4066Sahrens mutex_enter(&zvol_state_lock); 1402fa9e4066Sahrens 1403fa9e4066Sahrens zv = ddi_get_soft_state(zvol_state, getminor(dev)); 1404fa9e4066Sahrens 1405fa9e4066Sahrens if (zv == NULL) { 1406fa9e4066Sahrens mutex_exit(&zvol_state_lock); 1407fa9e4066Sahrens return (ENXIO); 1408fa9e4066Sahrens } 1409fa9e4066Sahrens 1410fa9e4066Sahrens switch (cmd) { 1411fa9e4066Sahrens 1412fa9e4066Sahrens case DKIOCINFO: 1413af2c4821Smaybee bzero(&dki, sizeof (dki)); 1414af2c4821Smaybee (void) strcpy(dki.dki_cname, "zvol"); 1415af2c4821Smaybee (void) strcpy(dki.dki_dname, "zvol"); 1416af2c4821Smaybee dki.dki_ctype = DKC_UNKNOWN; 1417af2c4821Smaybee dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs); 1418fa9e4066Sahrens mutex_exit(&zvol_state_lock); 1419af2c4821Smaybee if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag)) 1420fa9e4066Sahrens error = EFAULT; 1421fa9e4066Sahrens return (error); 1422fa9e4066Sahrens 1423fa9e4066Sahrens case DKIOCGMEDIAINFO: 1424fa9e4066Sahrens bzero(&dkm, sizeof (dkm)); 1425fa9e4066Sahrens dkm.dki_lbsize = 1U << zv->zv_min_bs; 1426fa9e4066Sahrens dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs; 1427fa9e4066Sahrens dkm.dki_media_type = DK_UNKNOWN; 1428fa9e4066Sahrens mutex_exit(&zvol_state_lock); 1429fa9e4066Sahrens if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag)) 1430fa9e4066Sahrens error = EFAULT; 1431fa9e4066Sahrens return (error); 1432fa9e4066Sahrens 1433fa9e4066Sahrens case DKIOCGETEFI: 1434c7f714e2SEric Taylor { 1435c7f714e2SEric Taylor uint64_t vs = zv->zv_volsize; 1436c7f714e2SEric Taylor uint8_t bs = zv->zv_min_bs; 1437fa9e4066Sahrens 143868a5ac4dSmaybee mutex_exit(&zvol_state_lock); 1439c7f714e2SEric Taylor error = zvol_getefi((void *)arg, flag, vs, bs); 1440c7f714e2SEric Taylor return (error); 144168a5ac4dSmaybee } 1442fa9e4066Sahrens 1443feb08c6bSbillm case DKIOCFLUSHWRITECACHE: 1444af2c4821Smaybee dkc = (struct dk_callback *)arg; 1445feb08c6bSbillm zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ); 1446af2c4821Smaybee if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) { 1447af2c4821Smaybee (*dkc->dkc_callback)(dkc->dkc_cookie, error); 1448af2c4821Smaybee error = 0; 1449af2c4821Smaybee } 1450feb08c6bSbillm break; 1451feb08c6bSbillm 1452b6130eadSmaybee case DKIOCGGEOM: 1453b6130eadSmaybee case DKIOCGVTOC: 1454e7cbe64fSgw /* 1455e7cbe64fSgw * commands using these (like prtvtoc) expect ENOTSUP 1456e7cbe64fSgw * since we're emulating an EFI label 1457e7cbe64fSgw */ 1458b6130eadSmaybee error = ENOTSUP; 1459b6130eadSmaybee break; 1460b6130eadSmaybee 1461e7cbe64fSgw case DKIOCDUMPINIT: 1462e7cbe64fSgw rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 1463e7cbe64fSgw RL_WRITER); 1464e7cbe64fSgw error = zvol_dumpify(zv); 1465e7cbe64fSgw zfs_range_unlock(rl); 1466e7cbe64fSgw break; 1467e7cbe64fSgw 1468e7cbe64fSgw case DKIOCDUMPFINI: 1469e7cbe64fSgw rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 1470e7cbe64fSgw RL_WRITER); 1471e7cbe64fSgw error = zvol_dump_fini(zv); 1472e7cbe64fSgw zfs_range_unlock(rl); 1473e7cbe64fSgw break; 1474e7cbe64fSgw 1475fa9e4066Sahrens default: 147668a5ac4dSmaybee error = ENOTTY; 1477fa9e4066Sahrens break; 1478fa9e4066Sahrens 1479fa9e4066Sahrens } 1480fa9e4066Sahrens mutex_exit(&zvol_state_lock); 1481fa9e4066Sahrens return (error); 1482fa9e4066Sahrens } 1483fa9e4066Sahrens 1484fa9e4066Sahrens int 1485fa9e4066Sahrens zvol_busy(void) 1486fa9e4066Sahrens { 1487fa9e4066Sahrens return (zvol_minors != 0); 1488fa9e4066Sahrens } 1489fa9e4066Sahrens 1490fa9e4066Sahrens void 1491fa9e4066Sahrens zvol_init(void) 1492fa9e4066Sahrens { 1493fa9e4066Sahrens VERIFY(ddi_soft_state_init(&zvol_state, sizeof (zvol_state_t), 1) == 0); 1494fa9e4066Sahrens mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL); 1495fa9e4066Sahrens } 1496fa9e4066Sahrens 1497fa9e4066Sahrens void 1498fa9e4066Sahrens zvol_fini(void) 1499fa9e4066Sahrens { 1500fa9e4066Sahrens mutex_destroy(&zvol_state_lock); 1501fa9e4066Sahrens ddi_soft_state_fini(&zvol_state); 1502fa9e4066Sahrens } 1503e7cbe64fSgw 1504e7cbe64fSgw static boolean_t 1505e7cbe64fSgw zvol_is_swap(zvol_state_t *zv) 1506e7cbe64fSgw { 1507e7cbe64fSgw vnode_t *vp; 1508e7cbe64fSgw boolean_t ret = B_FALSE; 1509e7cbe64fSgw char *devpath; 1510e7cbe64fSgw size_t devpathlen; 1511e7cbe64fSgw int error; 1512e7cbe64fSgw 1513e7cbe64fSgw devpathlen = strlen(ZVOL_FULL_DEV_DIR) + strlen(zv->zv_name) + 1; 1514e7cbe64fSgw devpath = kmem_alloc(devpathlen, KM_SLEEP); 1515e7cbe64fSgw (void) sprintf(devpath, "%s%s", ZVOL_FULL_DEV_DIR, zv->zv_name); 1516e7cbe64fSgw error = lookupname(devpath, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp); 1517e7cbe64fSgw kmem_free(devpath, devpathlen); 1518e7cbe64fSgw 1519e7cbe64fSgw ret = !error && IS_SWAPVP(common_specvp(vp)); 1520e7cbe64fSgw 1521e7cbe64fSgw if (vp != NULL) 1522e7cbe64fSgw VN_RELE(vp); 1523e7cbe64fSgw 1524e7cbe64fSgw return (ret); 1525e7cbe64fSgw } 1526e7cbe64fSgw 1527e7cbe64fSgw static int 1528e7cbe64fSgw zvol_dump_init(zvol_state_t *zv, boolean_t resize) 1529e7cbe64fSgw { 1530e7cbe64fSgw dmu_tx_t *tx; 1531e7cbe64fSgw int error = 0; 1532e7cbe64fSgw objset_t *os = zv->zv_objset; 1533e7cbe64fSgw nvlist_t *nv = NULL; 1534e7cbe64fSgw 1535e7cbe64fSgw ASSERT(MUTEX_HELD(&zvol_state_lock)); 1536e7cbe64fSgw 1537e7cbe64fSgw tx = dmu_tx_create(os); 1538e7cbe64fSgw dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 1539e7cbe64fSgw error = dmu_tx_assign(tx, TXG_WAIT); 1540e7cbe64fSgw if (error) { 1541e7cbe64fSgw dmu_tx_abort(tx); 1542e7cbe64fSgw return (error); 1543e7cbe64fSgw } 1544e7cbe64fSgw 1545e7cbe64fSgw /* 1546e7cbe64fSgw * If we are resizing the dump device then we only need to 1547e7cbe64fSgw * update the refreservation to match the newly updated 1548e7cbe64fSgw * zvolsize. Otherwise, we save off the original state of the 1549e7cbe64fSgw * zvol so that we can restore them if the zvol is ever undumpified. 1550e7cbe64fSgw */ 1551e7cbe64fSgw if (resize) { 1552e7cbe64fSgw error = zap_update(os, ZVOL_ZAP_OBJ, 1553e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 1554e7cbe64fSgw &zv->zv_volsize, tx); 1555e7cbe64fSgw } else { 155688b7b0f2SMatthew Ahrens uint64_t checksum, compress, refresrv, vbs; 155788b7b0f2SMatthew Ahrens 1558e7cbe64fSgw error = dsl_prop_get_integer(zv->zv_name, 1559e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL); 1560e7cbe64fSgw error = error ? error : dsl_prop_get_integer(zv->zv_name, 1561e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL); 1562e7cbe64fSgw error = error ? error : dsl_prop_get_integer(zv->zv_name, 1563e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL); 156488b7b0f2SMatthew Ahrens error = error ? error : dsl_prop_get_integer(zv->zv_name, 156588b7b0f2SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL); 1566e7cbe64fSgw 1567e7cbe64fSgw error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1568e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, 1569e7cbe64fSgw &compress, tx); 1570e7cbe64fSgw error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1571e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx); 1572e7cbe64fSgw error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1573e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 1574e7cbe64fSgw &refresrv, tx); 157588b7b0f2SMatthew Ahrens error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 157688b7b0f2SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, 157788b7b0f2SMatthew Ahrens &vbs, tx); 1578e7cbe64fSgw } 1579e7cbe64fSgw dmu_tx_commit(tx); 1580e7cbe64fSgw 1581e7cbe64fSgw /* Truncate the file */ 1582e7cbe64fSgw if (!error) 1583cdb0ab79Smaybee error = dmu_free_long_range(zv->zv_objset, 1584cdb0ab79Smaybee ZVOL_OBJ, 0, DMU_OBJECT_END); 1585e7cbe64fSgw 1586e7cbe64fSgw if (error) 1587e7cbe64fSgw return (error); 1588e7cbe64fSgw 1589e7cbe64fSgw /* 1590e7cbe64fSgw * We only need update the zvol's property if we are initializing 1591e7cbe64fSgw * the dump area for the first time. 1592e7cbe64fSgw */ 1593e7cbe64fSgw if (!resize) { 1594e7cbe64fSgw VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1595e7cbe64fSgw VERIFY(nvlist_add_uint64(nv, 1596e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0); 1597e7cbe64fSgw VERIFY(nvlist_add_uint64(nv, 1598e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), 1599e7cbe64fSgw ZIO_COMPRESS_OFF) == 0); 1600e7cbe64fSgw VERIFY(nvlist_add_uint64(nv, 1601e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_CHECKSUM), 1602e7cbe64fSgw ZIO_CHECKSUM_OFF) == 0); 160388b7b0f2SMatthew Ahrens VERIFY(nvlist_add_uint64(nv, 160488b7b0f2SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 160588b7b0f2SMatthew Ahrens SPA_MAXBLOCKSIZE) == 0); 1606e7cbe64fSgw 1607e7cbe64fSgw error = zfs_set_prop_nvlist(zv->zv_name, nv); 1608e7cbe64fSgw nvlist_free(nv); 1609e7cbe64fSgw 1610e7cbe64fSgw if (error) 1611e7cbe64fSgw return (error); 1612e7cbe64fSgw } 1613e7cbe64fSgw 1614e7cbe64fSgw /* Allocate the space for the dump */ 1615e7cbe64fSgw error = zvol_prealloc(zv); 1616e7cbe64fSgw return (error); 1617e7cbe64fSgw } 1618e7cbe64fSgw 1619e7cbe64fSgw static int 1620e7cbe64fSgw zvol_dumpify(zvol_state_t *zv) 1621e7cbe64fSgw { 1622e7cbe64fSgw int error = 0; 1623e7cbe64fSgw uint64_t dumpsize = 0; 1624e7cbe64fSgw dmu_tx_t *tx; 1625e7cbe64fSgw objset_t *os = zv->zv_objset; 1626e7cbe64fSgw 1627e7cbe64fSgw if (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY)) 1628e7cbe64fSgw return (EROFS); 1629e7cbe64fSgw 1630e7cbe64fSgw /* 1631e7cbe64fSgw * We do not support swap devices acting as dump devices. 1632e7cbe64fSgw */ 1633e7cbe64fSgw if (zvol_is_swap(zv)) 1634e7cbe64fSgw return (ENOTSUP); 1635e7cbe64fSgw 1636e7cbe64fSgw if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 1637e7cbe64fSgw 8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) { 1638e7cbe64fSgw boolean_t resize = (dumpsize > 0) ? B_TRUE : B_FALSE; 1639e7cbe64fSgw 1640e7cbe64fSgw if ((error = zvol_dump_init(zv, resize)) != 0) { 1641e7cbe64fSgw (void) zvol_dump_fini(zv); 1642e7cbe64fSgw return (error); 1643e7cbe64fSgw } 1644e7cbe64fSgw } 1645e7cbe64fSgw 1646e7cbe64fSgw /* 1647e7cbe64fSgw * Build up our lba mapping. 1648e7cbe64fSgw */ 1649e7cbe64fSgw error = zvol_get_lbas(zv); 1650e7cbe64fSgw if (error) { 1651e7cbe64fSgw (void) zvol_dump_fini(zv); 1652e7cbe64fSgw return (error); 1653e7cbe64fSgw } 1654e7cbe64fSgw 1655e7cbe64fSgw tx = dmu_tx_create(os); 1656e7cbe64fSgw dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 1657e7cbe64fSgw error = dmu_tx_assign(tx, TXG_WAIT); 1658e7cbe64fSgw if (error) { 1659e7cbe64fSgw dmu_tx_abort(tx); 1660e7cbe64fSgw (void) zvol_dump_fini(zv); 1661e7cbe64fSgw return (error); 1662e7cbe64fSgw } 1663e7cbe64fSgw 1664e7cbe64fSgw zv->zv_flags |= ZVOL_DUMPIFIED; 1665e7cbe64fSgw error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1, 1666e7cbe64fSgw &zv->zv_volsize, tx); 1667e7cbe64fSgw dmu_tx_commit(tx); 1668e7cbe64fSgw 1669e7cbe64fSgw if (error) { 1670e7cbe64fSgw (void) zvol_dump_fini(zv); 1671e7cbe64fSgw return (error); 1672e7cbe64fSgw } 1673e7cbe64fSgw 1674e7cbe64fSgw txg_wait_synced(dmu_objset_pool(os), 0); 1675e7cbe64fSgw return (0); 1676e7cbe64fSgw } 1677e7cbe64fSgw 1678e7cbe64fSgw static int 1679e7cbe64fSgw zvol_dump_fini(zvol_state_t *zv) 1680e7cbe64fSgw { 1681e7cbe64fSgw dmu_tx_t *tx; 1682e7cbe64fSgw objset_t *os = zv->zv_objset; 1683e7cbe64fSgw nvlist_t *nv; 1684e7cbe64fSgw int error = 0; 168588b7b0f2SMatthew Ahrens uint64_t checksum, compress, refresrv, vbs; 1686e7cbe64fSgw 1687b7e50089Smaybee /* 1688b7e50089Smaybee * Attempt to restore the zvol back to its pre-dumpified state. 1689b7e50089Smaybee * This is a best-effort attempt as it's possible that not all 1690b7e50089Smaybee * of these properties were initialized during the dumpify process 1691b7e50089Smaybee * (i.e. error during zvol_dump_init). 1692b7e50089Smaybee */ 1693b7e50089Smaybee 1694e7cbe64fSgw tx = dmu_tx_create(os); 1695e7cbe64fSgw dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 1696e7cbe64fSgw error = dmu_tx_assign(tx, TXG_WAIT); 1697e7cbe64fSgw if (error) { 1698e7cbe64fSgw dmu_tx_abort(tx); 1699e7cbe64fSgw return (error); 1700e7cbe64fSgw } 1701b7e50089Smaybee (void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx); 1702b7e50089Smaybee dmu_tx_commit(tx); 1703e7cbe64fSgw 1704e7cbe64fSgw (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 1705e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum); 1706e7cbe64fSgw (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 1707e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress); 1708e7cbe64fSgw (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 1709e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv); 171088b7b0f2SMatthew Ahrens (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 171188b7b0f2SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs); 1712e7cbe64fSgw 1713e7cbe64fSgw VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1714e7cbe64fSgw (void) nvlist_add_uint64(nv, 1715e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum); 1716e7cbe64fSgw (void) nvlist_add_uint64(nv, 1717e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress); 1718e7cbe64fSgw (void) nvlist_add_uint64(nv, 1719e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv); 172088b7b0f2SMatthew Ahrens (void) nvlist_add_uint64(nv, 172188b7b0f2SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), vbs); 1722e7cbe64fSgw (void) zfs_set_prop_nvlist(zv->zv_name, nv); 1723e7cbe64fSgw nvlist_free(nv); 1724e7cbe64fSgw 1725b7e50089Smaybee zvol_free_extents(zv); 1726b7e50089Smaybee zv->zv_flags &= ~ZVOL_DUMPIFIED; 1727b7e50089Smaybee (void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END); 1728b7e50089Smaybee 1729e7cbe64fSgw return (0); 1730e7cbe64fSgw } 1731