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 /* 22e08bf2c6SEric Taylor * Copyright 2009 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 * 35681d9761SEric Taylor * These links are created by the /dev filesystem (sdev_zvolops.c). 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> 781209a471SNeil Perrin #include <sys/zil_impl.h> 79fa9e4066Sahrens 80fa9e4066Sahrens #include "zfs_namecheck.h" 81fa9e4066Sahrens 82fa9e4066Sahrens static void *zvol_state; 83503ad85cSMatthew Ahrens static char *zvol_tag = "zvol_tag"; 84fa9e4066Sahrens 85e7cbe64fSgw #define ZVOL_DUMPSIZE "dumpsize" 86e7cbe64fSgw 87fa9e4066Sahrens /* 88fa9e4066Sahrens * This lock protects the zvol_state structure from being modified 89fa9e4066Sahrens * while it's being used, e.g. an open that comes in before a create 90fa9e4066Sahrens * finishes. It also protects temporary opens of the dataset so that, 91fa9e4066Sahrens * e.g., an open doesn't get a spurious EBUSY. 92fa9e4066Sahrens */ 93fa9e4066Sahrens static kmutex_t zvol_state_lock; 94fa9e4066Sahrens static uint32_t zvol_minors; 95fa9e4066Sahrens 96e7cbe64fSgw typedef struct zvol_extent { 9788b7b0f2SMatthew Ahrens list_node_t ze_node; 98e7cbe64fSgw dva_t ze_dva; /* dva associated with this extent */ 9988b7b0f2SMatthew Ahrens uint64_t ze_nblks; /* number of blocks in extent */ 100e7cbe64fSgw } zvol_extent_t; 101e7cbe64fSgw 102fa9e4066Sahrens /* 103fa9e4066Sahrens * The in-core state of each volume. 104fa9e4066Sahrens */ 105fa9e4066Sahrens typedef struct zvol_state { 106fa9e4066Sahrens char zv_name[MAXPATHLEN]; /* pool/dd name */ 107fa9e4066Sahrens uint64_t zv_volsize; /* amount of space we advertise */ 10867bd71c6Sperrin uint64_t zv_volblocksize; /* volume block size */ 109fa9e4066Sahrens minor_t zv_minor; /* minor number */ 110fa9e4066Sahrens uint8_t zv_min_bs; /* minimum addressable block shift */ 111701f66c4SEric Taylor uint8_t zv_flags; /* readonly, dumpified, etc. */ 112fa9e4066Sahrens objset_t *zv_objset; /* objset handle */ 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 126701f66c4SEric Taylor #define ZVOL_WCE 0x8 127e7cbe64fSgw 12867bd71c6Sperrin /* 12967bd71c6Sperrin * zvol maximum transfer in one DMU tx. 13067bd71c6Sperrin */ 13167bd71c6Sperrin int zvol_maxphys = DMU_MAX_ACCESS/2; 13267bd71c6Sperrin 133681d9761SEric Taylor static int zvol_remove_zv(zvol_state_t *); 134e7cbe64fSgw extern int zfs_set_prop_nvlist(const char *, nvlist_t *); 135feb08c6bSbillm static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio); 136e7cbe64fSgw static int zvol_dumpify(zvol_state_t *zv); 137e7cbe64fSgw static int zvol_dump_fini(zvol_state_t *zv); 138e7cbe64fSgw static int zvol_dump_init(zvol_state_t *zv, boolean_t resize); 13967bd71c6Sperrin 140fa9e4066Sahrens static void 141681d9761SEric Taylor zvol_size_changed(uint64_t volsize, major_t maj, minor_t min) 142fa9e4066Sahrens { 143681d9761SEric Taylor dev_t dev = makedevice(maj, min); 144fa9e4066Sahrens 145fa9e4066Sahrens VERIFY(ddi_prop_update_int64(dev, zfs_dip, 146681d9761SEric Taylor "Size", volsize) == DDI_SUCCESS); 147fa9e4066Sahrens VERIFY(ddi_prop_update_int64(dev, zfs_dip, 148681d9761SEric Taylor "Nblocks", lbtodb(volsize)) == DDI_SUCCESS); 149e7cbe64fSgw 150e7cbe64fSgw /* Notify specfs to invalidate the cached size */ 151e7cbe64fSgw spec_size_invalidate(dev, VBLK); 152e7cbe64fSgw spec_size_invalidate(dev, VCHR); 153fa9e4066Sahrens } 154fa9e4066Sahrens 155fa9e4066Sahrens int 156e9dbad6fSeschrock zvol_check_volsize(uint64_t volsize, uint64_t blocksize) 157fa9e4066Sahrens { 158e9dbad6fSeschrock if (volsize == 0) 159fa9e4066Sahrens return (EINVAL); 160fa9e4066Sahrens 161e9dbad6fSeschrock if (volsize % blocksize != 0) 1625c5460e9Seschrock return (EINVAL); 1635c5460e9Seschrock 164fa9e4066Sahrens #ifdef _ILP32 165e9dbad6fSeschrock if (volsize - 1 > SPEC_MAXOFFSET_T) 166fa9e4066Sahrens return (EOVERFLOW); 167fa9e4066Sahrens #endif 168fa9e4066Sahrens return (0); 169fa9e4066Sahrens } 170fa9e4066Sahrens 171fa9e4066Sahrens int 172e9dbad6fSeschrock zvol_check_volblocksize(uint64_t volblocksize) 173fa9e4066Sahrens { 174e9dbad6fSeschrock if (volblocksize < SPA_MINBLOCKSIZE || 175e9dbad6fSeschrock volblocksize > SPA_MAXBLOCKSIZE || 176e9dbad6fSeschrock !ISP2(volblocksize)) 177fa9e4066Sahrens return (EDOM); 178fa9e4066Sahrens 179fa9e4066Sahrens return (0); 180fa9e4066Sahrens } 181fa9e4066Sahrens 182fa9e4066Sahrens int 183a2eea2e1Sahrens zvol_get_stats(objset_t *os, nvlist_t *nv) 184fa9e4066Sahrens { 185fa9e4066Sahrens int error; 186fa9e4066Sahrens dmu_object_info_t doi; 187a2eea2e1Sahrens uint64_t val; 188fa9e4066Sahrens 189a2eea2e1Sahrens error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val); 190fa9e4066Sahrens if (error) 191fa9e4066Sahrens return (error); 192fa9e4066Sahrens 193a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val); 194a2eea2e1Sahrens 195fa9e4066Sahrens error = dmu_object_info(os, ZVOL_OBJ, &doi); 196fa9e4066Sahrens 197a2eea2e1Sahrens if (error == 0) { 198a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE, 199a2eea2e1Sahrens doi.doi_data_block_size); 200a2eea2e1Sahrens } 201fa9e4066Sahrens 202fa9e4066Sahrens return (error); 203fa9e4066Sahrens } 204fa9e4066Sahrens 205fa9e4066Sahrens /* 206fa9e4066Sahrens * Find a free minor number. 207fa9e4066Sahrens */ 208fa9e4066Sahrens static minor_t 209fa9e4066Sahrens zvol_minor_alloc(void) 210fa9e4066Sahrens { 211fa9e4066Sahrens minor_t minor; 212fa9e4066Sahrens 213fa9e4066Sahrens ASSERT(MUTEX_HELD(&zvol_state_lock)); 214fa9e4066Sahrens 215fa9e4066Sahrens for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) 216fa9e4066Sahrens if (ddi_get_soft_state(zvol_state, minor) == NULL) 217fa9e4066Sahrens return (minor); 218fa9e4066Sahrens 219fa9e4066Sahrens return (0); 220fa9e4066Sahrens } 221fa9e4066Sahrens 222fa9e4066Sahrens static zvol_state_t * 223e9dbad6fSeschrock zvol_minor_lookup(const char *name) 224fa9e4066Sahrens { 225fa9e4066Sahrens minor_t minor; 226fa9e4066Sahrens zvol_state_t *zv; 227fa9e4066Sahrens 228fa9e4066Sahrens ASSERT(MUTEX_HELD(&zvol_state_lock)); 229fa9e4066Sahrens 230fa9e4066Sahrens for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) { 231fa9e4066Sahrens zv = ddi_get_soft_state(zvol_state, minor); 232fa9e4066Sahrens if (zv == NULL) 233fa9e4066Sahrens continue; 234fa9e4066Sahrens if (strcmp(zv->zv_name, name) == 0) 235fa9e4066Sahrens break; 236fa9e4066Sahrens } 237fa9e4066Sahrens 238fa9e4066Sahrens return (zv); 239fa9e4066Sahrens } 240fa9e4066Sahrens 241e7cbe64fSgw /* extent mapping arg */ 242e7cbe64fSgw struct maparg { 24388b7b0f2SMatthew Ahrens zvol_state_t *ma_zv; 24488b7b0f2SMatthew Ahrens uint64_t ma_blks; 245e7cbe64fSgw }; 246e7cbe64fSgw 247e7cbe64fSgw /*ARGSUSED*/ 248e7cbe64fSgw static int 24988b7b0f2SMatthew Ahrens zvol_map_block(spa_t *spa, blkptr_t *bp, const zbookmark_t *zb, 25088b7b0f2SMatthew Ahrens const dnode_phys_t *dnp, void *arg) 251e7cbe64fSgw { 25288b7b0f2SMatthew Ahrens struct maparg *ma = arg; 25388b7b0f2SMatthew Ahrens zvol_extent_t *ze; 25488b7b0f2SMatthew Ahrens int bs = ma->ma_zv->zv_volblocksize; 255e7cbe64fSgw 25688b7b0f2SMatthew Ahrens if (bp == NULL || zb->zb_object != ZVOL_OBJ || zb->zb_level != 0) 25788b7b0f2SMatthew Ahrens return (0); 258e7cbe64fSgw 25988b7b0f2SMatthew Ahrens VERIFY3U(ma->ma_blks, ==, zb->zb_blkid); 26088b7b0f2SMatthew Ahrens ma->ma_blks++; 261e7cbe64fSgw 26288b7b0f2SMatthew Ahrens /* Abort immediately if we have encountered gang blocks */ 26388b7b0f2SMatthew Ahrens if (BP_IS_GANG(bp)) 26488b7b0f2SMatthew Ahrens return (EFRAGS); 265e7cbe64fSgw 26688b7b0f2SMatthew Ahrens /* 26788b7b0f2SMatthew Ahrens * See if the block is at the end of the previous extent. 26888b7b0f2SMatthew Ahrens */ 26988b7b0f2SMatthew Ahrens ze = list_tail(&ma->ma_zv->zv_extents); 27088b7b0f2SMatthew Ahrens if (ze && 27188b7b0f2SMatthew Ahrens DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) && 27288b7b0f2SMatthew Ahrens DVA_GET_OFFSET(BP_IDENTITY(bp)) == 27388b7b0f2SMatthew Ahrens DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) { 27488b7b0f2SMatthew Ahrens ze->ze_nblks++; 27588b7b0f2SMatthew Ahrens return (0); 276e7cbe64fSgw } 277e7cbe64fSgw 27888b7b0f2SMatthew Ahrens dprintf_bp(bp, "%s", "next blkptr:"); 279e7cbe64fSgw 28088b7b0f2SMatthew Ahrens /* start a new extent */ 28188b7b0f2SMatthew Ahrens ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP); 28288b7b0f2SMatthew Ahrens ze->ze_dva = bp->blk_dva[0]; /* structure assignment */ 28388b7b0f2SMatthew Ahrens ze->ze_nblks = 1; 28488b7b0f2SMatthew Ahrens list_insert_tail(&ma->ma_zv->zv_extents, ze); 28588b7b0f2SMatthew Ahrens return (0); 28688b7b0f2SMatthew Ahrens } 287e7cbe64fSgw 28888b7b0f2SMatthew Ahrens static void 28988b7b0f2SMatthew Ahrens zvol_free_extents(zvol_state_t *zv) 29088b7b0f2SMatthew Ahrens { 29188b7b0f2SMatthew Ahrens zvol_extent_t *ze; 292e7cbe64fSgw 29388b7b0f2SMatthew Ahrens while (ze = list_head(&zv->zv_extents)) { 29488b7b0f2SMatthew Ahrens list_remove(&zv->zv_extents, ze); 29588b7b0f2SMatthew Ahrens kmem_free(ze, sizeof (zvol_extent_t)); 296e7cbe64fSgw } 29788b7b0f2SMatthew Ahrens } 298e7cbe64fSgw 29988b7b0f2SMatthew Ahrens static int 30088b7b0f2SMatthew Ahrens zvol_get_lbas(zvol_state_t *zv) 30188b7b0f2SMatthew Ahrens { 30288b7b0f2SMatthew Ahrens struct maparg ma; 30388b7b0f2SMatthew Ahrens int err; 30488b7b0f2SMatthew Ahrens 30588b7b0f2SMatthew Ahrens ma.ma_zv = zv; 30688b7b0f2SMatthew Ahrens ma.ma_blks = 0; 30788b7b0f2SMatthew Ahrens zvol_free_extents(zv); 30888b7b0f2SMatthew Ahrens 30988b7b0f2SMatthew Ahrens err = traverse_dataset(dmu_objset_ds(zv->zv_objset), 0, 31088b7b0f2SMatthew Ahrens TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma); 31188b7b0f2SMatthew Ahrens if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) { 31288b7b0f2SMatthew Ahrens zvol_free_extents(zv); 31388b7b0f2SMatthew Ahrens return (err ? err : EIO); 314e7cbe64fSgw } 31588b7b0f2SMatthew Ahrens 316e7cbe64fSgw return (0); 317e7cbe64fSgw } 318e7cbe64fSgw 319ecd6cf80Smarks /* ARGSUSED */ 320fa9e4066Sahrens void 321ecd6cf80Smarks zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 322fa9e4066Sahrens { 323da6c28aaSamw zfs_creat_t *zct = arg; 324da6c28aaSamw nvlist_t *nvprops = zct->zct_props; 325fa9e4066Sahrens int error; 326e9dbad6fSeschrock uint64_t volblocksize, volsize; 327fa9e4066Sahrens 328ecd6cf80Smarks VERIFY(nvlist_lookup_uint64(nvprops, 329e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0); 330ecd6cf80Smarks if (nvlist_lookup_uint64(nvprops, 331e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0) 332e9dbad6fSeschrock volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 333e9dbad6fSeschrock 334e9dbad6fSeschrock /* 335e7cbe64fSgw * These properties must be removed from the list so the generic 336e9dbad6fSeschrock * property setting step won't apply to them. 337e9dbad6fSeschrock */ 338ecd6cf80Smarks VERIFY(nvlist_remove_all(nvprops, 339e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0); 340ecd6cf80Smarks (void) nvlist_remove_all(nvprops, 341e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE)); 342e9dbad6fSeschrock 343e9dbad6fSeschrock error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize, 344fa9e4066Sahrens DMU_OT_NONE, 0, tx); 345fa9e4066Sahrens ASSERT(error == 0); 346fa9e4066Sahrens 347fa9e4066Sahrens error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP, 348fa9e4066Sahrens DMU_OT_NONE, 0, tx); 349fa9e4066Sahrens ASSERT(error == 0); 350fa9e4066Sahrens 351e9dbad6fSeschrock error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx); 352fa9e4066Sahrens ASSERT(error == 0); 353fa9e4066Sahrens } 354fa9e4066Sahrens 35522ac5be4Sperrin /* 35622ac5be4Sperrin * Replay a TX_WRITE ZIL transaction that didn't get committed 35722ac5be4Sperrin * after a system failure 35822ac5be4Sperrin */ 35922ac5be4Sperrin static int 36022ac5be4Sperrin zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap) 36122ac5be4Sperrin { 36222ac5be4Sperrin objset_t *os = zv->zv_objset; 36322ac5be4Sperrin char *data = (char *)(lr + 1); /* data follows lr_write_t */ 36422ac5be4Sperrin uint64_t off = lr->lr_offset; 36522ac5be4Sperrin uint64_t len = lr->lr_length; 36622ac5be4Sperrin dmu_tx_t *tx; 36722ac5be4Sperrin int error; 36822ac5be4Sperrin 36922ac5be4Sperrin if (byteswap) 37022ac5be4Sperrin byteswap_uint64_array(lr, sizeof (*lr)); 37122ac5be4Sperrin 372*975c32a0SNeil Perrin /* If it's a dmu_sync() block get the data and write the whole block */ 373*975c32a0SNeil Perrin if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) 374*975c32a0SNeil Perrin zil_get_replay_data(dmu_objset_zil(os), lr); 375*975c32a0SNeil Perrin 37622ac5be4Sperrin tx = dmu_tx_create(os); 37722ac5be4Sperrin dmu_tx_hold_write(tx, ZVOL_OBJ, off, len); 3781209a471SNeil Perrin error = dmu_tx_assign(tx, TXG_WAIT); 37922ac5be4Sperrin if (error) { 38022ac5be4Sperrin dmu_tx_abort(tx); 38122ac5be4Sperrin } else { 38222ac5be4Sperrin dmu_write(os, ZVOL_OBJ, off, len, data, tx); 38322ac5be4Sperrin dmu_tx_commit(tx); 38422ac5be4Sperrin } 38522ac5be4Sperrin 38622ac5be4Sperrin return (error); 38722ac5be4Sperrin } 38822ac5be4Sperrin 38922ac5be4Sperrin /* ARGSUSED */ 39022ac5be4Sperrin static int 39122ac5be4Sperrin zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap) 39222ac5be4Sperrin { 39322ac5be4Sperrin return (ENOTSUP); 39422ac5be4Sperrin } 39522ac5be4Sperrin 39622ac5be4Sperrin /* 39722ac5be4Sperrin * Callback vectors for replaying records. 39822ac5be4Sperrin * Only TX_WRITE is needed for zvol. 39922ac5be4Sperrin */ 40022ac5be4Sperrin zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = { 40122ac5be4Sperrin zvol_replay_err, /* 0 no such transaction type */ 40222ac5be4Sperrin zvol_replay_err, /* TX_CREATE */ 40322ac5be4Sperrin zvol_replay_err, /* TX_MKDIR */ 40422ac5be4Sperrin zvol_replay_err, /* TX_MKXATTR */ 40522ac5be4Sperrin zvol_replay_err, /* TX_SYMLINK */ 40622ac5be4Sperrin zvol_replay_err, /* TX_REMOVE */ 40722ac5be4Sperrin zvol_replay_err, /* TX_RMDIR */ 40822ac5be4Sperrin zvol_replay_err, /* TX_LINK */ 40922ac5be4Sperrin zvol_replay_err, /* TX_RENAME */ 41022ac5be4Sperrin zvol_replay_write, /* TX_WRITE */ 41122ac5be4Sperrin zvol_replay_err, /* TX_TRUNCATE */ 41222ac5be4Sperrin zvol_replay_err, /* TX_SETATTR */ 41322ac5be4Sperrin zvol_replay_err, /* TX_ACL */ 414*975c32a0SNeil Perrin zvol_replay_err, /* TX_CREATE_ACL */ 415*975c32a0SNeil Perrin zvol_replay_err, /* TX_CREATE_ATTR */ 416*975c32a0SNeil Perrin zvol_replay_err, /* TX_CREATE_ACL_ATTR */ 417*975c32a0SNeil Perrin zvol_replay_err, /* TX_MKDIR_ACL */ 418*975c32a0SNeil Perrin zvol_replay_err, /* TX_MKDIR_ATTR */ 419*975c32a0SNeil Perrin zvol_replay_err, /* TX_MKDIR_ACL_ATTR */ 420*975c32a0SNeil Perrin zvol_replay_err, /* TX_WRITE2 */ 42122ac5be4Sperrin }; 42222ac5be4Sperrin 423681d9761SEric Taylor int 424681d9761SEric Taylor zvol_name2minor(const char *name, minor_t *minor) 425681d9761SEric Taylor { 426681d9761SEric Taylor zvol_state_t *zv; 427681d9761SEric Taylor 428681d9761SEric Taylor mutex_enter(&zvol_state_lock); 429681d9761SEric Taylor zv = zvol_minor_lookup(name); 430681d9761SEric Taylor if (minor && zv) 431681d9761SEric Taylor *minor = zv->zv_minor; 432681d9761SEric Taylor mutex_exit(&zvol_state_lock); 433681d9761SEric Taylor return (zv ? 0 : -1); 434681d9761SEric Taylor } 435681d9761SEric Taylor 436e7cbe64fSgw /* 437e7cbe64fSgw * Create a minor node (plus a whole lot more) for the specified volume. 438fa9e4066Sahrens */ 439fa9e4066Sahrens int 440681d9761SEric Taylor zvol_create_minor(const char *name) 441fa9e4066Sahrens { 442fa9e4066Sahrens zvol_state_t *zv; 443fa9e4066Sahrens objset_t *os; 44467bd71c6Sperrin dmu_object_info_t doi; 445fa9e4066Sahrens minor_t minor = 0; 446fa9e4066Sahrens char chrbuf[30], blkbuf[30]; 447fa9e4066Sahrens int error; 448fa9e4066Sahrens 449fa9e4066Sahrens mutex_enter(&zvol_state_lock); 450fa9e4066Sahrens 451fa9e4066Sahrens if ((zv = zvol_minor_lookup(name)) != NULL) { 452fa9e4066Sahrens mutex_exit(&zvol_state_lock); 453fa9e4066Sahrens return (EEXIST); 454fa9e4066Sahrens } 455fa9e4066Sahrens 456503ad85cSMatthew Ahrens /* lie and say we're read-only */ 457503ad85cSMatthew Ahrens error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, zvol_tag, &os); 458fa9e4066Sahrens 459fa9e4066Sahrens if (error) { 460fa9e4066Sahrens mutex_exit(&zvol_state_lock); 461fa9e4066Sahrens return (error); 462fa9e4066Sahrens } 463fa9e4066Sahrens 464681d9761SEric Taylor if ((minor = zvol_minor_alloc()) == 0) { 465503ad85cSMatthew Ahrens dmu_objset_disown(os, zvol_tag); 466fa9e4066Sahrens mutex_exit(&zvol_state_lock); 467fa9e4066Sahrens return (ENXIO); 468fa9e4066Sahrens } 469fa9e4066Sahrens 470fa9e4066Sahrens if (ddi_soft_state_zalloc(zvol_state, minor) != DDI_SUCCESS) { 471503ad85cSMatthew Ahrens dmu_objset_disown(os, zvol_tag); 472fa9e4066Sahrens mutex_exit(&zvol_state_lock); 473fa9e4066Sahrens return (EAGAIN); 474fa9e4066Sahrens } 475e9dbad6fSeschrock (void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME, 476e9dbad6fSeschrock (char *)name); 477fa9e4066Sahrens 478681d9761SEric Taylor (void) snprintf(chrbuf, sizeof (chrbuf), "%u,raw", minor); 479fa9e4066Sahrens 480fa9e4066Sahrens if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR, 481fa9e4066Sahrens minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 482fa9e4066Sahrens ddi_soft_state_free(zvol_state, minor); 483503ad85cSMatthew Ahrens dmu_objset_disown(os, zvol_tag); 484fa9e4066Sahrens mutex_exit(&zvol_state_lock); 485fa9e4066Sahrens return (EAGAIN); 486fa9e4066Sahrens } 487fa9e4066Sahrens 488681d9761SEric Taylor (void) snprintf(blkbuf, sizeof (blkbuf), "%u", minor); 489fa9e4066Sahrens 490fa9e4066Sahrens if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK, 491fa9e4066Sahrens minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 492fa9e4066Sahrens ddi_remove_minor_node(zfs_dip, chrbuf); 493fa9e4066Sahrens ddi_soft_state_free(zvol_state, minor); 494503ad85cSMatthew Ahrens dmu_objset_disown(os, zvol_tag); 495fa9e4066Sahrens mutex_exit(&zvol_state_lock); 496fa9e4066Sahrens return (EAGAIN); 497fa9e4066Sahrens } 498fa9e4066Sahrens 499fa9e4066Sahrens zv = ddi_get_soft_state(zvol_state, minor); 500fa9e4066Sahrens 501681d9761SEric Taylor (void) strlcpy(zv->zv_name, name, MAXPATHLEN); 502fa9e4066Sahrens zv->zv_min_bs = DEV_BSHIFT; 503fa9e4066Sahrens zv->zv_minor = minor; 504fa9e4066Sahrens zv->zv_objset = os; 505681d9761SEric Taylor if (dmu_objset_is_snapshot(os)) 506681d9761SEric Taylor zv->zv_flags |= ZVOL_RDONLY; 507c2e6a7d6Sperrin mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL); 508c2e6a7d6Sperrin avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare, 509c2e6a7d6Sperrin sizeof (rl_t), offsetof(rl_t, r_node)); 51088b7b0f2SMatthew Ahrens list_create(&zv->zv_extents, sizeof (zvol_extent_t), 51188b7b0f2SMatthew Ahrens offsetof(zvol_extent_t, ze_node)); 51267bd71c6Sperrin /* get and cache the blocksize */ 51367bd71c6Sperrin error = dmu_object_info(os, ZVOL_OBJ, &doi); 51467bd71c6Sperrin ASSERT(error == 0); 51567bd71c6Sperrin zv->zv_volblocksize = doi.doi_data_block_size; 51622ac5be4Sperrin 5171209a471SNeil Perrin zil_replay(os, zv, zvol_replay_vector); 518681d9761SEric Taylor dmu_objset_disown(os, zvol_tag); 519681d9761SEric Taylor zv->zv_objset = NULL; 520fa9e4066Sahrens 521fa9e4066Sahrens zvol_minors++; 522fa9e4066Sahrens 523fa9e4066Sahrens mutex_exit(&zvol_state_lock); 524fa9e4066Sahrens 525fa9e4066Sahrens return (0); 526fa9e4066Sahrens } 527fa9e4066Sahrens 528fa9e4066Sahrens /* 529fa9e4066Sahrens * Remove minor node for the specified volume. 530fa9e4066Sahrens */ 531681d9761SEric Taylor static int 532681d9761SEric Taylor zvol_remove_zv(zvol_state_t *zv) 533681d9761SEric Taylor { 534681d9761SEric Taylor char nmbuf[20]; 535681d9761SEric Taylor 536681d9761SEric Taylor ASSERT(MUTEX_HELD(&zvol_state_lock)); 537681d9761SEric Taylor if (zv->zv_total_opens != 0) 538681d9761SEric Taylor return (EBUSY); 539681d9761SEric Taylor 540681d9761SEric Taylor (void) snprintf(nmbuf, sizeof (nmbuf), "%u,raw", zv->zv_minor); 541681d9761SEric Taylor ddi_remove_minor_node(zfs_dip, nmbuf); 542681d9761SEric Taylor 543681d9761SEric Taylor (void) snprintf(nmbuf, sizeof (nmbuf), "%u", zv->zv_minor); 544681d9761SEric Taylor ddi_remove_minor_node(zfs_dip, nmbuf); 545681d9761SEric Taylor 546681d9761SEric Taylor avl_destroy(&zv->zv_znode.z_range_avl); 547681d9761SEric Taylor mutex_destroy(&zv->zv_znode.z_range_lock); 548681d9761SEric Taylor 549681d9761SEric Taylor ddi_soft_state_free(zvol_state, zv->zv_minor); 550681d9761SEric Taylor 551681d9761SEric Taylor zvol_minors--; 552681d9761SEric Taylor return (0); 553681d9761SEric Taylor } 554681d9761SEric Taylor 555fa9e4066Sahrens int 556e9dbad6fSeschrock zvol_remove_minor(const char *name) 557fa9e4066Sahrens { 558fa9e4066Sahrens zvol_state_t *zv; 559681d9761SEric Taylor int rc; 560fa9e4066Sahrens 561fa9e4066Sahrens mutex_enter(&zvol_state_lock); 562e9dbad6fSeschrock if ((zv = zvol_minor_lookup(name)) == NULL) { 563fa9e4066Sahrens mutex_exit(&zvol_state_lock); 564fa9e4066Sahrens return (ENXIO); 565fa9e4066Sahrens } 566681d9761SEric Taylor rc = zvol_remove_zv(zv); 567681d9761SEric Taylor mutex_exit(&zvol_state_lock); 568681d9761SEric Taylor return (rc); 569681d9761SEric Taylor } 570fa9e4066Sahrens 571681d9761SEric Taylor int 572681d9761SEric Taylor zvol_first_open(zvol_state_t *zv) 573681d9761SEric Taylor { 574681d9761SEric Taylor objset_t *os; 575681d9761SEric Taylor uint64_t volsize; 576681d9761SEric Taylor int error; 577681d9761SEric Taylor uint64_t readonly; 578fa9e4066Sahrens 579681d9761SEric Taylor /* lie and say we're read-only */ 580681d9761SEric Taylor error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, B_TRUE, 581681d9761SEric Taylor zvol_tag, &os); 582681d9761SEric Taylor if (error) 583681d9761SEric Taylor return (error); 584fa9e4066Sahrens 585681d9761SEric Taylor error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize); 586681d9761SEric Taylor if (error) { 587681d9761SEric Taylor ASSERT(error == 0); 588681d9761SEric Taylor dmu_objset_disown(os, zvol_tag); 589681d9761SEric Taylor return (error); 590681d9761SEric Taylor } 591681d9761SEric Taylor zv->zv_objset = os; 592681d9761SEric Taylor zv->zv_volsize = volsize; 593681d9761SEric Taylor zv->zv_zilog = zil_open(os, zvol_get_data); 594681d9761SEric Taylor zvol_size_changed(zv->zv_volsize, ddi_driver_major(zfs_dip), 595681d9761SEric Taylor zv->zv_minor); 596fa9e4066Sahrens 597681d9761SEric Taylor VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &readonly, 598681d9761SEric Taylor NULL) == 0); 599681d9761SEric Taylor if (readonly || dmu_objset_is_snapshot(os)) 600681d9761SEric Taylor zv->zv_flags |= ZVOL_RDONLY; 601681d9761SEric Taylor else 602681d9761SEric Taylor zv->zv_flags &= ~ZVOL_RDONLY; 603681d9761SEric Taylor return (error); 604681d9761SEric Taylor } 605fa9e4066Sahrens 606681d9761SEric Taylor void 607681d9761SEric Taylor zvol_last_close(zvol_state_t *zv) 608681d9761SEric Taylor { 60922ac5be4Sperrin zil_close(zv->zv_zilog); 61022ac5be4Sperrin zv->zv_zilog = NULL; 611503ad85cSMatthew Ahrens dmu_objset_disown(zv->zv_objset, zvol_tag); 612fa9e4066Sahrens zv->zv_objset = NULL; 613fa9e4066Sahrens } 614fa9e4066Sahrens 615e7cbe64fSgw int 616e7cbe64fSgw zvol_prealloc(zvol_state_t *zv) 617e7cbe64fSgw { 618e7cbe64fSgw objset_t *os = zv->zv_objset; 619e7cbe64fSgw dmu_tx_t *tx; 620e7cbe64fSgw uint64_t refd, avail, usedobjs, availobjs; 621e7cbe64fSgw uint64_t resid = zv->zv_volsize; 622e7cbe64fSgw uint64_t off = 0; 623e7cbe64fSgw 624e7cbe64fSgw /* Check the space usage before attempting to allocate the space */ 625e7cbe64fSgw dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs); 626e7cbe64fSgw if (avail < zv->zv_volsize) 627e7cbe64fSgw return (ENOSPC); 628e7cbe64fSgw 629e7cbe64fSgw /* Free old extents if they exist */ 630e7cbe64fSgw zvol_free_extents(zv); 631e7cbe64fSgw 632e7cbe64fSgw while (resid != 0) { 633e7cbe64fSgw int error; 634e7cbe64fSgw uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE); 635e7cbe64fSgw 636e7cbe64fSgw tx = dmu_tx_create(os); 637e7cbe64fSgw dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 638e7cbe64fSgw error = dmu_tx_assign(tx, TXG_WAIT); 639e7cbe64fSgw if (error) { 640e7cbe64fSgw dmu_tx_abort(tx); 641cdb0ab79Smaybee (void) dmu_free_long_range(os, ZVOL_OBJ, 0, off); 642e7cbe64fSgw return (error); 643e7cbe64fSgw } 64482c9918fSTim Haley dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx); 645e7cbe64fSgw dmu_tx_commit(tx); 646e7cbe64fSgw off += bytes; 647e7cbe64fSgw resid -= bytes; 648e7cbe64fSgw } 649e7cbe64fSgw txg_wait_synced(dmu_objset_pool(os), 0); 650e7cbe64fSgw 651e7cbe64fSgw return (0); 652e7cbe64fSgw } 653e7cbe64fSgw 654e7cbe64fSgw int 655681d9761SEric Taylor zvol_update_volsize(objset_t *os, uint64_t volsize) 656e7cbe64fSgw { 657e7cbe64fSgw dmu_tx_t *tx; 658e7cbe64fSgw int error; 659e7cbe64fSgw 660e7cbe64fSgw ASSERT(MUTEX_HELD(&zvol_state_lock)); 661e7cbe64fSgw 662681d9761SEric Taylor tx = dmu_tx_create(os); 663e7cbe64fSgw dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 664e7cbe64fSgw error = dmu_tx_assign(tx, TXG_WAIT); 665e7cbe64fSgw if (error) { 666e7cbe64fSgw dmu_tx_abort(tx); 667e7cbe64fSgw return (error); 668e7cbe64fSgw } 669e7cbe64fSgw 670681d9761SEric Taylor error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, 671e7cbe64fSgw &volsize, tx); 672e7cbe64fSgw dmu_tx_commit(tx); 673e7cbe64fSgw 674e7cbe64fSgw if (error == 0) 675681d9761SEric Taylor error = dmu_free_long_range(os, 676cdb0ab79Smaybee ZVOL_OBJ, volsize, DMU_OBJECT_END); 677681d9761SEric Taylor return (error); 678681d9761SEric Taylor } 679e7cbe64fSgw 680681d9761SEric Taylor void 681681d9761SEric Taylor zvol_remove_minors(const char *name) 682681d9761SEric Taylor { 683681d9761SEric Taylor zvol_state_t *zv; 684681d9761SEric Taylor char *namebuf; 685681d9761SEric Taylor minor_t minor; 686681d9761SEric Taylor 687681d9761SEric Taylor namebuf = kmem_zalloc(strlen(name) + 2, KM_SLEEP); 688681d9761SEric Taylor (void) strncpy(namebuf, name, strlen(name)); 689681d9761SEric Taylor (void) strcat(namebuf, "/"); 690681d9761SEric Taylor mutex_enter(&zvol_state_lock); 691681d9761SEric Taylor for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) { 692681d9761SEric Taylor 693681d9761SEric Taylor zv = ddi_get_soft_state(zvol_state, minor); 694681d9761SEric Taylor if (zv == NULL) 695681d9761SEric Taylor continue; 696681d9761SEric Taylor if (strncmp(namebuf, zv->zv_name, strlen(namebuf)) == 0) 697681d9761SEric Taylor (void) zvol_remove_zv(zv); 698e7cbe64fSgw } 699681d9761SEric Taylor kmem_free(namebuf, strlen(name) + 2); 700681d9761SEric Taylor 701681d9761SEric Taylor mutex_exit(&zvol_state_lock); 702e7cbe64fSgw } 703e7cbe64fSgw 704fa9e4066Sahrens int 70591ebeef5Sahrens zvol_set_volsize(const char *name, major_t maj, uint64_t volsize) 706fa9e4066Sahrens { 707681d9761SEric Taylor zvol_state_t *zv = NULL; 708681d9761SEric Taylor objset_t *os; 709fa9e4066Sahrens int error; 7105c5460e9Seschrock dmu_object_info_t doi; 711e7cbe64fSgw uint64_t old_volsize = 0ULL; 712681d9761SEric Taylor uint64_t readonly; 713fa9e4066Sahrens 714fa9e4066Sahrens mutex_enter(&zvol_state_lock); 715681d9761SEric Taylor zv = zvol_minor_lookup(name); 716681d9761SEric Taylor if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) { 717681d9761SEric Taylor mutex_exit(&zvol_state_lock); 718681d9761SEric Taylor return (error); 719fa9e4066Sahrens } 720fa9e4066Sahrens 721681d9761SEric Taylor if ((error = dmu_object_info(os, ZVOL_OBJ, &doi)) != 0 || 722e9dbad6fSeschrock (error = zvol_check_volsize(volsize, 723bb0ade09Sahrens doi.doi_data_block_size)) != 0) 724bb0ade09Sahrens goto out; 7255c5460e9Seschrock 726681d9761SEric Taylor VERIFY(dsl_prop_get_integer(name, "readonly", &readonly, 727681d9761SEric Taylor NULL) == 0); 728681d9761SEric Taylor if (readonly) { 729bb0ade09Sahrens error = EROFS; 730bb0ade09Sahrens goto out; 731fa9e4066Sahrens } 732fa9e4066Sahrens 733681d9761SEric Taylor error = zvol_update_volsize(os, volsize); 734e7cbe64fSgw /* 735e7cbe64fSgw * Reinitialize the dump area to the new size. If we 736681d9761SEric Taylor * failed to resize the dump area then restore it back to 737681d9761SEric Taylor * its original size. 738e7cbe64fSgw */ 739681d9761SEric Taylor if (zv && error == 0) { 740681d9761SEric Taylor if (zv->zv_flags & ZVOL_DUMPIFIED) { 741681d9761SEric Taylor old_volsize = zv->zv_volsize; 742681d9761SEric Taylor zv->zv_volsize = volsize; 743681d9761SEric Taylor if ((error = zvol_dumpify(zv)) != 0 || 744681d9761SEric Taylor (error = dumpvp_resize()) != 0) { 745681d9761SEric Taylor (void) zvol_update_volsize(os, old_volsize); 746681d9761SEric Taylor zv->zv_volsize = old_volsize; 747681d9761SEric Taylor error = zvol_dumpify(zv); 748681d9761SEric Taylor } 749681d9761SEric Taylor } 750681d9761SEric Taylor if (error == 0) { 751681d9761SEric Taylor zv->zv_volsize = volsize; 752681d9761SEric Taylor zvol_size_changed(volsize, maj, zv->zv_minor); 753e7cbe64fSgw } 754fa9e4066Sahrens } 755fa9e4066Sahrens 756573ca77eSGeorge Wilson /* 757573ca77eSGeorge Wilson * Generate a LUN expansion event. 758573ca77eSGeorge Wilson */ 759681d9761SEric Taylor if (zv && error == 0) { 760573ca77eSGeorge Wilson sysevent_id_t eid; 761573ca77eSGeorge Wilson nvlist_t *attr; 762573ca77eSGeorge Wilson char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 763573ca77eSGeorge Wilson 764681d9761SEric Taylor (void) snprintf(physpath, MAXPATHLEN, "%s%u", ZVOL_PSEUDO_DEV, 765573ca77eSGeorge Wilson zv->zv_minor); 766573ca77eSGeorge Wilson 767573ca77eSGeorge Wilson VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 768573ca77eSGeorge Wilson VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 769573ca77eSGeorge Wilson 770573ca77eSGeorge Wilson (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 771573ca77eSGeorge Wilson ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 772573ca77eSGeorge Wilson 773573ca77eSGeorge Wilson nvlist_free(attr); 774573ca77eSGeorge Wilson kmem_free(physpath, MAXPATHLEN); 775573ca77eSGeorge Wilson } 776573ca77eSGeorge Wilson 777bb0ade09Sahrens out: 778681d9761SEric Taylor dmu_objset_rele(os, FTAG); 779bb0ade09Sahrens 780fa9e4066Sahrens mutex_exit(&zvol_state_lock); 781fa9e4066Sahrens 782fa9e4066Sahrens return (error); 783fa9e4066Sahrens } 784fa9e4066Sahrens 785fa9e4066Sahrens /*ARGSUSED*/ 786fa9e4066Sahrens int 787fa9e4066Sahrens zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr) 788fa9e4066Sahrens { 789fa9e4066Sahrens minor_t minor = getminor(*devp); 790fa9e4066Sahrens zvol_state_t *zv; 791681d9761SEric Taylor int err = 0; 792fa9e4066Sahrens 793fa9e4066Sahrens if (minor == 0) /* This is the control device */ 794fa9e4066Sahrens return (0); 795fa9e4066Sahrens 796fa9e4066Sahrens mutex_enter(&zvol_state_lock); 797fa9e4066Sahrens 798fa9e4066Sahrens zv = ddi_get_soft_state(zvol_state, minor); 799fa9e4066Sahrens if (zv == NULL) { 800fa9e4066Sahrens mutex_exit(&zvol_state_lock); 801fa9e4066Sahrens return (ENXIO); 802fa9e4066Sahrens } 803fa9e4066Sahrens 804681d9761SEric Taylor if (zv->zv_total_opens == 0) 805681d9761SEric Taylor err = zvol_first_open(zv); 806681d9761SEric Taylor if (err) { 807fa9e4066Sahrens mutex_exit(&zvol_state_lock); 808681d9761SEric Taylor return (err); 809681d9761SEric Taylor } 810681d9761SEric Taylor if ((flag & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) { 811681d9761SEric Taylor err = EROFS; 812681d9761SEric Taylor goto out; 813fa9e4066Sahrens } 814c7f714e2SEric Taylor if (zv->zv_flags & ZVOL_EXCL) { 815681d9761SEric Taylor err = EBUSY; 816681d9761SEric Taylor goto out; 817c7f714e2SEric Taylor } 818c7f714e2SEric Taylor if (flag & FEXCL) { 819c7f714e2SEric Taylor if (zv->zv_total_opens != 0) { 820681d9761SEric Taylor err = EBUSY; 821681d9761SEric Taylor goto out; 822c7f714e2SEric Taylor } 823c7f714e2SEric Taylor zv->zv_flags |= ZVOL_EXCL; 824c7f714e2SEric Taylor } 825fa9e4066Sahrens 826fa9e4066Sahrens if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) { 827fa9e4066Sahrens zv->zv_open_count[otyp]++; 828fa9e4066Sahrens zv->zv_total_opens++; 829fa9e4066Sahrens } 830fa9e4066Sahrens mutex_exit(&zvol_state_lock); 831fa9e4066Sahrens 832681d9761SEric Taylor return (err); 833681d9761SEric Taylor out: 834681d9761SEric Taylor if (zv->zv_total_opens == 0) 835681d9761SEric Taylor zvol_last_close(zv); 836681d9761SEric Taylor mutex_exit(&zvol_state_lock); 837681d9761SEric Taylor return (err); 838fa9e4066Sahrens } 839fa9e4066Sahrens 840fa9e4066Sahrens /*ARGSUSED*/ 841fa9e4066Sahrens int 842fa9e4066Sahrens zvol_close(dev_t dev, int flag, int otyp, cred_t *cr) 843fa9e4066Sahrens { 844fa9e4066Sahrens minor_t minor = getminor(dev); 845fa9e4066Sahrens zvol_state_t *zv; 846681d9761SEric Taylor int error = 0; 847fa9e4066Sahrens 848fa9e4066Sahrens if (minor == 0) /* This is the control device */ 849fa9e4066Sahrens return (0); 850fa9e4066Sahrens 851fa9e4066Sahrens mutex_enter(&zvol_state_lock); 852fa9e4066Sahrens 853fa9e4066Sahrens zv = ddi_get_soft_state(zvol_state, minor); 854fa9e4066Sahrens if (zv == NULL) { 855fa9e4066Sahrens mutex_exit(&zvol_state_lock); 856fa9e4066Sahrens return (ENXIO); 857fa9e4066Sahrens } 858fa9e4066Sahrens 859c7f714e2SEric Taylor if (zv->zv_flags & ZVOL_EXCL) { 860c7f714e2SEric Taylor ASSERT(zv->zv_total_opens == 1); 861c7f714e2SEric Taylor zv->zv_flags &= ~ZVOL_EXCL; 862fa9e4066Sahrens } 863fa9e4066Sahrens 864fa9e4066Sahrens /* 865fa9e4066Sahrens * If the open count is zero, this is a spurious close. 866fa9e4066Sahrens * That indicates a bug in the kernel / DDI framework. 867fa9e4066Sahrens */ 868fa9e4066Sahrens ASSERT(zv->zv_open_count[otyp] != 0); 869fa9e4066Sahrens ASSERT(zv->zv_total_opens != 0); 870fa9e4066Sahrens 871fa9e4066Sahrens /* 872fa9e4066Sahrens * You may get multiple opens, but only one close. 873fa9e4066Sahrens */ 874fa9e4066Sahrens zv->zv_open_count[otyp]--; 875fa9e4066Sahrens zv->zv_total_opens--; 876fa9e4066Sahrens 877681d9761SEric Taylor if (zv->zv_total_opens == 0) 878681d9761SEric Taylor zvol_last_close(zv); 879fa9e4066Sahrens 880681d9761SEric Taylor mutex_exit(&zvol_state_lock); 881681d9761SEric Taylor return (error); 882fa9e4066Sahrens } 883fa9e4066Sahrens 884feb08c6bSbillm static void 88567bd71c6Sperrin zvol_get_done(dmu_buf_t *db, void *vzgd) 88667bd71c6Sperrin { 88767bd71c6Sperrin zgd_t *zgd = (zgd_t *)vzgd; 888c2e6a7d6Sperrin rl_t *rl = zgd->zgd_rl; 88967bd71c6Sperrin 89067bd71c6Sperrin dmu_buf_rele(db, vzgd); 891c2e6a7d6Sperrin zfs_range_unlock(rl); 89217f17c2dSbonwick zil_add_block(zgd->zgd_zilog, zgd->zgd_bp); 89367bd71c6Sperrin kmem_free(zgd, sizeof (zgd_t)); 89467bd71c6Sperrin } 89567bd71c6Sperrin 89667bd71c6Sperrin /* 89767bd71c6Sperrin * Get data to generate a TX_WRITE intent log record. 89867bd71c6Sperrin */ 899feb08c6bSbillm static int 90067bd71c6Sperrin zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 90167bd71c6Sperrin { 90267bd71c6Sperrin zvol_state_t *zv = arg; 90367bd71c6Sperrin objset_t *os = zv->zv_objset; 90467bd71c6Sperrin dmu_buf_t *db; 905c2e6a7d6Sperrin rl_t *rl; 90667bd71c6Sperrin zgd_t *zgd; 907c2e6a7d6Sperrin uint64_t boff; /* block starting offset */ 908c2e6a7d6Sperrin int dlen = lr->lr_length; /* length of user data */ 90967bd71c6Sperrin int error; 91067bd71c6Sperrin 91167bd71c6Sperrin ASSERT(zio); 912c2e6a7d6Sperrin ASSERT(dlen != 0); 913feb08c6bSbillm 914c2e6a7d6Sperrin /* 915c2e6a7d6Sperrin * Write records come in two flavors: immediate and indirect. 916c2e6a7d6Sperrin * For small writes it's cheaper to store the data with the 917c2e6a7d6Sperrin * log record (immediate); for large writes it's cheaper to 918c2e6a7d6Sperrin * sync the data and get a pointer to it (indirect) so that 919c2e6a7d6Sperrin * we don't have to write the data twice. 920c2e6a7d6Sperrin */ 921c2e6a7d6Sperrin if (buf != NULL) /* immediate write */ 9227bfdf011SNeil Perrin return (dmu_read(os, ZVOL_OBJ, lr->lr_offset, dlen, buf, 9237bfdf011SNeil Perrin DMU_READ_NO_PREFETCH)); 92467bd71c6Sperrin 92567bd71c6Sperrin zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP); 92667bd71c6Sperrin zgd->zgd_zilog = zv->zv_zilog; 92767bd71c6Sperrin zgd->zgd_bp = &lr->lr_blkptr; 92867bd71c6Sperrin 92967bd71c6Sperrin /* 930c2e6a7d6Sperrin * Lock the range of the block to ensure that when the data is 931e7cbe64fSgw * written out and its checksum is being calculated that no other 932c2e6a7d6Sperrin * thread can change the block. 93367bd71c6Sperrin */ 934c2e6a7d6Sperrin boff = P2ALIGN_TYPED(lr->lr_offset, zv->zv_volblocksize, uint64_t); 935c2e6a7d6Sperrin rl = zfs_range_lock(&zv->zv_znode, boff, zv->zv_volblocksize, 936c2e6a7d6Sperrin RL_READER); 937c2e6a7d6Sperrin zgd->zgd_rl = rl; 938c2e6a7d6Sperrin 939c2e6a7d6Sperrin VERIFY(0 == dmu_buf_hold(os, ZVOL_OBJ, lr->lr_offset, zgd, &db)); 940*975c32a0SNeil Perrin 94167bd71c6Sperrin error = dmu_sync(zio, db, &lr->lr_blkptr, 94267bd71c6Sperrin lr->lr_common.lrc_txg, zvol_get_done, zgd); 943*975c32a0SNeil Perrin if (error == 0) { 944*975c32a0SNeil Perrin /* 945*975c32a0SNeil Perrin * dmu_sync() can compress a block of zeros to a null blkptr 946*975c32a0SNeil Perrin * but the block size still needs to be passed through to 947*975c32a0SNeil Perrin * replay. 948*975c32a0SNeil Perrin */ 949*975c32a0SNeil Perrin BP_SET_LSIZE(&lr->lr_blkptr, db->db_size); 95017f17c2dSbonwick zil_add_block(zv->zv_zilog, &lr->lr_blkptr); 951*975c32a0SNeil Perrin } 952*975c32a0SNeil Perrin 95367bd71c6Sperrin /* 95467bd71c6Sperrin * If we get EINPROGRESS, then we need to wait for a 95567bd71c6Sperrin * write IO initiated by dmu_sync() to complete before 95667bd71c6Sperrin * we can release this dbuf. We will finish everything 95767bd71c6Sperrin * up in the zvol_get_done() callback. 95867bd71c6Sperrin */ 95967bd71c6Sperrin if (error == EINPROGRESS) 96067bd71c6Sperrin return (0); 96167bd71c6Sperrin dmu_buf_rele(db, zgd); 962c2e6a7d6Sperrin zfs_range_unlock(rl); 96367bd71c6Sperrin kmem_free(zgd, sizeof (zgd_t)); 96467bd71c6Sperrin return (error); 96567bd71c6Sperrin } 96667bd71c6Sperrin 967a24e15ceSperrin /* 968a24e15ceSperrin * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions. 96922ac5be4Sperrin * 97022ac5be4Sperrin * We store data in the log buffers if it's small enough. 97167bd71c6Sperrin * Otherwise we will later flush the data out via dmu_sync(). 97222ac5be4Sperrin */ 97367bd71c6Sperrin ssize_t zvol_immediate_write_sz = 32768; 97422ac5be4Sperrin 975feb08c6bSbillm static void 976510b6c0eSNeil Perrin zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid, 977510b6c0eSNeil Perrin boolean_t sync) 97822ac5be4Sperrin { 979feb08c6bSbillm uint32_t blocksize = zv->zv_volblocksize; 9801209a471SNeil Perrin zilog_t *zilog = zv->zv_zilog; 981510b6c0eSNeil Perrin boolean_t slogging; 982e09fa4daSNeil Perrin ssize_t immediate_write_sz; 983510b6c0eSNeil Perrin 984510b6c0eSNeil Perrin if (zil_disable) 985510b6c0eSNeil Perrin return; 98622ac5be4Sperrin 9871209a471SNeil Perrin if (zilog->zl_replay) { 9881209a471SNeil Perrin dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 9891209a471SNeil Perrin zilog->zl_replayed_seq[dmu_tx_get_txg(tx) & TXG_MASK] = 9901209a471SNeil Perrin zilog->zl_replaying_seq; 9911209a471SNeil Perrin return; 9921209a471SNeil Perrin } 9931209a471SNeil Perrin 994e09fa4daSNeil Perrin immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT) 995e09fa4daSNeil Perrin ? 0 : zvol_immediate_write_sz; 996e09fa4daSNeil Perrin 997e09fa4daSNeil Perrin slogging = spa_has_slogs(zilog->zl_spa) && 998e09fa4daSNeil Perrin (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY); 999feb08c6bSbillm 1000510b6c0eSNeil Perrin while (resid) { 1001510b6c0eSNeil Perrin itx_t *itx; 1002510b6c0eSNeil Perrin lr_write_t *lr; 1003510b6c0eSNeil Perrin ssize_t len; 1004510b6c0eSNeil Perrin itx_wr_state_t write_state; 1005510b6c0eSNeil Perrin 1006510b6c0eSNeil Perrin /* 1007510b6c0eSNeil Perrin * Unlike zfs_log_write() we can be called with 1008510b6c0eSNeil Perrin * upto DMU_MAX_ACCESS/2 (5MB) writes. 1009510b6c0eSNeil Perrin */ 1010e09fa4daSNeil Perrin if (blocksize > immediate_write_sz && !slogging && 1011510b6c0eSNeil Perrin resid >= blocksize && off % blocksize == 0) { 1012510b6c0eSNeil Perrin write_state = WR_INDIRECT; /* uses dmu_sync */ 1013510b6c0eSNeil Perrin len = blocksize; 1014510b6c0eSNeil Perrin } else if (sync) { 1015510b6c0eSNeil Perrin write_state = WR_COPIED; 1016510b6c0eSNeil Perrin len = MIN(ZIL_MAX_LOG_DATA, resid); 1017510b6c0eSNeil Perrin } else { 1018510b6c0eSNeil Perrin write_state = WR_NEED_COPY; 1019510b6c0eSNeil Perrin len = MIN(ZIL_MAX_LOG_DATA, resid); 1020510b6c0eSNeil Perrin } 1021510b6c0eSNeil Perrin 1022510b6c0eSNeil Perrin itx = zil_itx_create(TX_WRITE, sizeof (*lr) + 1023510b6c0eSNeil Perrin (write_state == WR_COPIED ? len : 0)); 1024feb08c6bSbillm lr = (lr_write_t *)&itx->itx_lr; 1025510b6c0eSNeil Perrin if (write_state == WR_COPIED && dmu_read(zv->zv_objset, 10267bfdf011SNeil Perrin ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) { 1027510b6c0eSNeil Perrin kmem_free(itx, offsetof(itx_t, itx_lr) + 1028510b6c0eSNeil Perrin itx->itx_lr.lrc_reclen); 1029510b6c0eSNeil Perrin itx = zil_itx_create(TX_WRITE, sizeof (*lr)); 1030510b6c0eSNeil Perrin lr = (lr_write_t *)&itx->itx_lr; 1031510b6c0eSNeil Perrin write_state = WR_NEED_COPY; 1032510b6c0eSNeil Perrin } 1033510b6c0eSNeil Perrin 1034510b6c0eSNeil Perrin itx->itx_wr_state = write_state; 1035510b6c0eSNeil Perrin if (write_state == WR_NEED_COPY) 1036510b6c0eSNeil Perrin itx->itx_sod += len; 1037feb08c6bSbillm lr->lr_foid = ZVOL_OBJ; 1038feb08c6bSbillm lr->lr_offset = off; 1039510b6c0eSNeil Perrin lr->lr_length = len; 1040feb08c6bSbillm lr->lr_blkoff = off - P2ALIGN_TYPED(off, blocksize, uint64_t); 1041feb08c6bSbillm BP_ZERO(&lr->lr_blkptr); 1042feb08c6bSbillm 1043510b6c0eSNeil Perrin itx->itx_private = zv; 1044510b6c0eSNeil Perrin itx->itx_sync = sync; 1045510b6c0eSNeil Perrin 10461209a471SNeil Perrin (void) zil_itx_assign(zilog, itx, tx); 1047510b6c0eSNeil Perrin 1048510b6c0eSNeil Perrin off += len; 1049510b6c0eSNeil Perrin resid -= len; 105022ac5be4Sperrin } 105122ac5be4Sperrin } 105222ac5be4Sperrin 105388b7b0f2SMatthew Ahrens static int 105488b7b0f2SMatthew Ahrens zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t size, 105588b7b0f2SMatthew Ahrens boolean_t doread, boolean_t isdump) 1056e7cbe64fSgw { 1057e7cbe64fSgw vdev_disk_t *dvd; 1058e7cbe64fSgw int c; 1059e7cbe64fSgw int numerrors = 0; 1060e7cbe64fSgw 1061e7cbe64fSgw for (c = 0; c < vd->vdev_children; c++) { 106221ecdf64SLin Ling ASSERT(vd->vdev_ops == &vdev_mirror_ops || 106321ecdf64SLin Ling vd->vdev_ops == &vdev_replacing_ops || 106421ecdf64SLin Ling vd->vdev_ops == &vdev_spare_ops); 106588b7b0f2SMatthew Ahrens int err = zvol_dumpio_vdev(vd->vdev_child[c], 106688b7b0f2SMatthew Ahrens addr, offset, size, doread, isdump); 106788b7b0f2SMatthew Ahrens if (err != 0) { 1068e7cbe64fSgw numerrors++; 106988b7b0f2SMatthew Ahrens } else if (doread) { 1070e7cbe64fSgw break; 1071e7cbe64fSgw } 1072e7cbe64fSgw } 1073e7cbe64fSgw 1074e7cbe64fSgw if (!vd->vdev_ops->vdev_op_leaf) 1075e7cbe64fSgw return (numerrors < vd->vdev_children ? 0 : EIO); 1076e7cbe64fSgw 1077dc0bb255SEric Taylor if (doread && !vdev_readable(vd)) 1078dc0bb255SEric Taylor return (EIO); 1079dc0bb255SEric Taylor else if (!doread && !vdev_writeable(vd)) 1080e7cbe64fSgw return (EIO); 1081e7cbe64fSgw 1082e7cbe64fSgw dvd = vd->vdev_tsd; 1083e7cbe64fSgw ASSERT3P(dvd, !=, NULL); 1084e7cbe64fSgw offset += VDEV_LABEL_START_SIZE; 1085e7cbe64fSgw 1086e7cbe64fSgw if (ddi_in_panic() || isdump) { 108788b7b0f2SMatthew Ahrens ASSERT(!doread); 108888b7b0f2SMatthew Ahrens if (doread) 1089e7cbe64fSgw return (EIO); 1090e7cbe64fSgw return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset), 1091e7cbe64fSgw lbtodb(size))); 1092e7cbe64fSgw } else { 1093e7cbe64fSgw return (vdev_disk_physio(dvd->vd_lh, addr, size, offset, 109488b7b0f2SMatthew Ahrens doread ? B_READ : B_WRITE)); 1095e7cbe64fSgw } 1096e7cbe64fSgw } 1097e7cbe64fSgw 109888b7b0f2SMatthew Ahrens static int 109988b7b0f2SMatthew Ahrens zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size, 110088b7b0f2SMatthew Ahrens boolean_t doread, boolean_t isdump) 1101e7cbe64fSgw { 1102e7cbe64fSgw vdev_t *vd; 1103e7cbe64fSgw int error; 110488b7b0f2SMatthew Ahrens zvol_extent_t *ze; 1105e7cbe64fSgw spa_t *spa = dmu_objset_spa(zv->zv_objset); 1106e7cbe64fSgw 110788b7b0f2SMatthew Ahrens /* Must be sector aligned, and not stradle a block boundary. */ 110888b7b0f2SMatthew Ahrens if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) || 110988b7b0f2SMatthew Ahrens P2BOUNDARY(offset, size, zv->zv_volblocksize)) { 1110e7cbe64fSgw return (EINVAL); 111188b7b0f2SMatthew Ahrens } 111288b7b0f2SMatthew Ahrens ASSERT(size <= zv->zv_volblocksize); 1113e7cbe64fSgw 111488b7b0f2SMatthew Ahrens /* Locate the extent this belongs to */ 111588b7b0f2SMatthew Ahrens ze = list_head(&zv->zv_extents); 111688b7b0f2SMatthew Ahrens while (offset >= ze->ze_nblks * zv->zv_volblocksize) { 111788b7b0f2SMatthew Ahrens offset -= ze->ze_nblks * zv->zv_volblocksize; 111888b7b0f2SMatthew Ahrens ze = list_next(&zv->zv_extents, ze); 111988b7b0f2SMatthew Ahrens } 1120e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 112188b7b0f2SMatthew Ahrens vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva)); 112288b7b0f2SMatthew Ahrens offset += DVA_GET_OFFSET(&ze->ze_dva); 112388b7b0f2SMatthew Ahrens error = zvol_dumpio_vdev(vd, addr, offset, size, doread, isdump); 1124e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 1125e7cbe64fSgw return (error); 1126e7cbe64fSgw } 1127e7cbe64fSgw 1128fa9e4066Sahrens int 1129fa9e4066Sahrens zvol_strategy(buf_t *bp) 1130fa9e4066Sahrens { 1131fa9e4066Sahrens zvol_state_t *zv = ddi_get_soft_state(zvol_state, getminor(bp->b_edev)); 1132fa9e4066Sahrens uint64_t off, volsize; 113388b7b0f2SMatthew Ahrens size_t resid; 1134fa9e4066Sahrens char *addr; 113522ac5be4Sperrin objset_t *os; 1136c2e6a7d6Sperrin rl_t *rl; 1137fa9e4066Sahrens int error = 0; 113888b7b0f2SMatthew Ahrens boolean_t doread = bp->b_flags & B_READ; 113988b7b0f2SMatthew Ahrens boolean_t is_dump = zv->zv_flags & ZVOL_DUMPIFIED; 1140510b6c0eSNeil Perrin boolean_t sync; 1141fa9e4066Sahrens 1142fa9e4066Sahrens if (zv == NULL) { 1143fa9e4066Sahrens bioerror(bp, ENXIO); 1144fa9e4066Sahrens biodone(bp); 1145fa9e4066Sahrens return (0); 1146fa9e4066Sahrens } 1147fa9e4066Sahrens 1148fa9e4066Sahrens if (getminor(bp->b_edev) == 0) { 1149fa9e4066Sahrens bioerror(bp, EINVAL); 1150fa9e4066Sahrens biodone(bp); 1151fa9e4066Sahrens return (0); 1152fa9e4066Sahrens } 1153fa9e4066Sahrens 1154681d9761SEric Taylor if (!(bp->b_flags & B_READ) && (zv->zv_flags & ZVOL_RDONLY)) { 1155fa9e4066Sahrens bioerror(bp, EROFS); 1156fa9e4066Sahrens biodone(bp); 1157fa9e4066Sahrens return (0); 1158fa9e4066Sahrens } 1159fa9e4066Sahrens 1160fa9e4066Sahrens off = ldbtob(bp->b_blkno); 1161fa9e4066Sahrens volsize = zv->zv_volsize; 1162fa9e4066Sahrens 116322ac5be4Sperrin os = zv->zv_objset; 116422ac5be4Sperrin ASSERT(os != NULL); 1165fa9e4066Sahrens 1166fa9e4066Sahrens bp_mapin(bp); 1167fa9e4066Sahrens addr = bp->b_un.b_addr; 1168fa9e4066Sahrens resid = bp->b_bcount; 1169fa9e4066Sahrens 117088b7b0f2SMatthew Ahrens if (resid > 0 && (off < 0 || off >= volsize)) { 117188b7b0f2SMatthew Ahrens bioerror(bp, EIO); 117288b7b0f2SMatthew Ahrens biodone(bp); 117388b7b0f2SMatthew Ahrens return (0); 117488b7b0f2SMatthew Ahrens } 117573ec3d9cSgw 1176510b6c0eSNeil Perrin sync = !(bp->b_flags & B_ASYNC) && !doread && !is_dump && 1177510b6c0eSNeil Perrin !(zv->zv_flags & ZVOL_WCE) && !zil_disable; 1178510b6c0eSNeil Perrin 1179a24e15ceSperrin /* 1180a24e15ceSperrin * There must be no buffer changes when doing a dmu_sync() because 1181a24e15ceSperrin * we can't change the data whilst calculating the checksum. 1182a24e15ceSperrin */ 1183c2e6a7d6Sperrin rl = zfs_range_lock(&zv->zv_znode, off, resid, 118488b7b0f2SMatthew Ahrens doread ? RL_READER : RL_WRITER); 1185fa9e4066Sahrens 1186e7cbe64fSgw while (resid != 0 && off < volsize) { 118788b7b0f2SMatthew Ahrens size_t size = MIN(resid, zvol_maxphys); 1188e7cbe64fSgw if (is_dump) { 1189e7cbe64fSgw size = MIN(size, P2END(off, zv->zv_volblocksize) - off); 119088b7b0f2SMatthew Ahrens error = zvol_dumpio(zv, addr, off, size, 119188b7b0f2SMatthew Ahrens doread, B_FALSE); 119288b7b0f2SMatthew Ahrens } else if (doread) { 11937bfdf011SNeil Perrin error = dmu_read(os, ZVOL_OBJ, off, size, addr, 11947bfdf011SNeil Perrin DMU_READ_PREFETCH); 1195fa9e4066Sahrens } else { 119622ac5be4Sperrin dmu_tx_t *tx = dmu_tx_create(os); 1197fa9e4066Sahrens dmu_tx_hold_write(tx, ZVOL_OBJ, off, size); 1198fa9e4066Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 1199fa9e4066Sahrens if (error) { 1200fa9e4066Sahrens dmu_tx_abort(tx); 1201fa9e4066Sahrens } else { 120222ac5be4Sperrin dmu_write(os, ZVOL_OBJ, off, size, addr, tx); 1203510b6c0eSNeil Perrin zvol_log_write(zv, tx, off, size, sync); 1204fa9e4066Sahrens dmu_tx_commit(tx); 1205fa9e4066Sahrens } 1206fa9e4066Sahrens } 1207b87f3af3Sperrin if (error) { 1208b87f3af3Sperrin /* convert checksum errors into IO errors */ 1209b87f3af3Sperrin if (error == ECKSUM) 1210b87f3af3Sperrin error = EIO; 1211fa9e4066Sahrens break; 1212b87f3af3Sperrin } 1213fa9e4066Sahrens off += size; 1214fa9e4066Sahrens addr += size; 1215fa9e4066Sahrens resid -= size; 1216fa9e4066Sahrens } 1217c2e6a7d6Sperrin zfs_range_unlock(rl); 1218fa9e4066Sahrens 1219fa9e4066Sahrens if ((bp->b_resid = resid) == bp->b_bcount) 1220fa9e4066Sahrens bioerror(bp, off > volsize ? EINVAL : error); 1221fa9e4066Sahrens 1222510b6c0eSNeil Perrin if (sync) 1223feb08c6bSbillm zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ); 1224feb08c6bSbillm biodone(bp); 122522ac5be4Sperrin 1226fa9e4066Sahrens return (0); 1227fa9e4066Sahrens } 1228fa9e4066Sahrens 122967bd71c6Sperrin /* 123067bd71c6Sperrin * Set the buffer count to the zvol maximum transfer. 123167bd71c6Sperrin * Using our own routine instead of the default minphys() 123267bd71c6Sperrin * means that for larger writes we write bigger buffers on X86 123367bd71c6Sperrin * (128K instead of 56K) and flush the disk write cache less often 123467bd71c6Sperrin * (every zvol_maxphys - currently 1MB) instead of minphys (currently 123567bd71c6Sperrin * 56K on X86 and 128K on sparc). 123667bd71c6Sperrin */ 123767bd71c6Sperrin void 123867bd71c6Sperrin zvol_minphys(struct buf *bp) 123967bd71c6Sperrin { 124067bd71c6Sperrin if (bp->b_bcount > zvol_maxphys) 124167bd71c6Sperrin bp->b_bcount = zvol_maxphys; 124267bd71c6Sperrin } 124367bd71c6Sperrin 1244e7cbe64fSgw int 1245e7cbe64fSgw zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks) 1246e7cbe64fSgw { 1247e7cbe64fSgw minor_t minor = getminor(dev); 1248e7cbe64fSgw zvol_state_t *zv; 1249e7cbe64fSgw int error = 0; 1250e7cbe64fSgw uint64_t size; 1251e7cbe64fSgw uint64_t boff; 1252e7cbe64fSgw uint64_t resid; 1253e7cbe64fSgw 1254e7cbe64fSgw if (minor == 0) /* This is the control device */ 1255e7cbe64fSgw return (ENXIO); 1256e7cbe64fSgw 1257e7cbe64fSgw zv = ddi_get_soft_state(zvol_state, minor); 1258e7cbe64fSgw if (zv == NULL) 1259e7cbe64fSgw return (ENXIO); 1260e7cbe64fSgw 1261e7cbe64fSgw boff = ldbtob(blkno); 1262e7cbe64fSgw resid = ldbtob(nblocks); 126388b7b0f2SMatthew Ahrens 126488b7b0f2SMatthew Ahrens VERIFY3U(boff + resid, <=, zv->zv_volsize); 126588b7b0f2SMatthew Ahrens 1266e7cbe64fSgw while (resid) { 1267e7cbe64fSgw size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff); 126888b7b0f2SMatthew Ahrens error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE); 1269e7cbe64fSgw if (error) 1270e7cbe64fSgw break; 1271e7cbe64fSgw boff += size; 1272e7cbe64fSgw addr += size; 1273e7cbe64fSgw resid -= size; 1274e7cbe64fSgw } 1275e7cbe64fSgw 1276e7cbe64fSgw return (error); 1277e7cbe64fSgw } 1278e7cbe64fSgw 1279fa9e4066Sahrens /*ARGSUSED*/ 1280fa9e4066Sahrens int 1281feb08c6bSbillm zvol_read(dev_t dev, uio_t *uio, cred_t *cr) 1282fa9e4066Sahrens { 1283c7ca1008Sgw minor_t minor = getminor(dev); 1284c7ca1008Sgw zvol_state_t *zv; 128573ec3d9cSgw uint64_t volsize; 1286c2e6a7d6Sperrin rl_t *rl; 1287feb08c6bSbillm int error = 0; 1288fa9e4066Sahrens 1289c7ca1008Sgw if (minor == 0) /* This is the control device */ 1290c7ca1008Sgw return (ENXIO); 1291c7ca1008Sgw 1292c7ca1008Sgw zv = ddi_get_soft_state(zvol_state, minor); 1293c7ca1008Sgw if (zv == NULL) 1294c7ca1008Sgw return (ENXIO); 1295c7ca1008Sgw 129673ec3d9cSgw volsize = zv->zv_volsize; 129773ec3d9cSgw if (uio->uio_resid > 0 && 129873ec3d9cSgw (uio->uio_loffset < 0 || uio->uio_loffset >= volsize)) 129973ec3d9cSgw return (EIO); 130073ec3d9cSgw 130188b7b0f2SMatthew Ahrens if (zv->zv_flags & ZVOL_DUMPIFIED) { 130288b7b0f2SMatthew Ahrens error = physio(zvol_strategy, NULL, dev, B_READ, 130388b7b0f2SMatthew Ahrens zvol_minphys, uio); 130488b7b0f2SMatthew Ahrens return (error); 130588b7b0f2SMatthew Ahrens } 130688b7b0f2SMatthew Ahrens 1307c2e6a7d6Sperrin rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 1308c2e6a7d6Sperrin RL_READER); 130973ec3d9cSgw while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 1310feb08c6bSbillm uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 1311fa9e4066Sahrens 131273ec3d9cSgw /* don't read past the end */ 131373ec3d9cSgw if (bytes > volsize - uio->uio_loffset) 131473ec3d9cSgw bytes = volsize - uio->uio_loffset; 131573ec3d9cSgw 1316feb08c6bSbillm error = dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes); 1317b87f3af3Sperrin if (error) { 1318b87f3af3Sperrin /* convert checksum errors into IO errors */ 1319b87f3af3Sperrin if (error == ECKSUM) 1320b87f3af3Sperrin error = EIO; 1321feb08c6bSbillm break; 1322b87f3af3Sperrin } 1323feb08c6bSbillm } 1324c2e6a7d6Sperrin zfs_range_unlock(rl); 1325feb08c6bSbillm return (error); 1326fa9e4066Sahrens } 1327fa9e4066Sahrens 1328fa9e4066Sahrens /*ARGSUSED*/ 1329fa9e4066Sahrens int 1330feb08c6bSbillm zvol_write(dev_t dev, uio_t *uio, cred_t *cr) 1331fa9e4066Sahrens { 1332c7ca1008Sgw minor_t minor = getminor(dev); 1333c7ca1008Sgw zvol_state_t *zv; 133473ec3d9cSgw uint64_t volsize; 1335c2e6a7d6Sperrin rl_t *rl; 1336feb08c6bSbillm int error = 0; 1337510b6c0eSNeil Perrin boolean_t sync; 1338feb08c6bSbillm 1339c7ca1008Sgw if (minor == 0) /* This is the control device */ 1340c7ca1008Sgw return (ENXIO); 1341c7ca1008Sgw 1342c7ca1008Sgw zv = ddi_get_soft_state(zvol_state, minor); 1343c7ca1008Sgw if (zv == NULL) 1344c7ca1008Sgw return (ENXIO); 1345c7ca1008Sgw 134673ec3d9cSgw volsize = zv->zv_volsize; 134773ec3d9cSgw if (uio->uio_resid > 0 && 134873ec3d9cSgw (uio->uio_loffset < 0 || uio->uio_loffset >= volsize)) 134973ec3d9cSgw return (EIO); 135073ec3d9cSgw 1351e7cbe64fSgw if (zv->zv_flags & ZVOL_DUMPIFIED) { 1352e7cbe64fSgw error = physio(zvol_strategy, NULL, dev, B_WRITE, 1353e7cbe64fSgw zvol_minphys, uio); 1354e7cbe64fSgw return (error); 1355e7cbe64fSgw } 1356e7cbe64fSgw 1357510b6c0eSNeil Perrin sync = !(zv->zv_flags & ZVOL_WCE) && !zil_disable; 1358510b6c0eSNeil Perrin 1359c2e6a7d6Sperrin rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 1360c2e6a7d6Sperrin RL_WRITER); 136173ec3d9cSgw while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 1362feb08c6bSbillm uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 1363feb08c6bSbillm uint64_t off = uio->uio_loffset; 1364feb08c6bSbillm dmu_tx_t *tx = dmu_tx_create(zv->zv_objset); 136573ec3d9cSgw 136673ec3d9cSgw if (bytes > volsize - off) /* don't write past the end */ 136773ec3d9cSgw bytes = volsize - off; 136873ec3d9cSgw 1369feb08c6bSbillm dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 1370feb08c6bSbillm error = dmu_tx_assign(tx, TXG_WAIT); 1371feb08c6bSbillm if (error) { 1372feb08c6bSbillm dmu_tx_abort(tx); 1373feb08c6bSbillm break; 1374feb08c6bSbillm } 1375feb08c6bSbillm error = dmu_write_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes, tx); 1376feb08c6bSbillm if (error == 0) 1377510b6c0eSNeil Perrin zvol_log_write(zv, tx, off, bytes, sync); 1378feb08c6bSbillm dmu_tx_commit(tx); 1379feb08c6bSbillm 1380feb08c6bSbillm if (error) 1381feb08c6bSbillm break; 1382feb08c6bSbillm } 1383c2e6a7d6Sperrin zfs_range_unlock(rl); 1384510b6c0eSNeil Perrin if (sync) 1385e08bf2c6SEric Taylor zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ); 1386feb08c6bSbillm return (error); 1387fa9e4066Sahrens } 1388fa9e4066Sahrens 1389c7f714e2SEric Taylor int 1390c7f714e2SEric Taylor zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs) 1391c7f714e2SEric Taylor { 1392c7f714e2SEric Taylor struct uuid uuid = EFI_RESERVED; 1393c7f714e2SEric Taylor efi_gpe_t gpe = { 0 }; 1394c7f714e2SEric Taylor uint32_t crc; 1395c7f714e2SEric Taylor dk_efi_t efi; 1396c7f714e2SEric Taylor int length; 1397c7f714e2SEric Taylor char *ptr; 1398c7f714e2SEric Taylor 1399c7f714e2SEric Taylor if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag)) 1400c7f714e2SEric Taylor return (EFAULT); 1401c7f714e2SEric Taylor ptr = (char *)(uintptr_t)efi.dki_data_64; 1402c7f714e2SEric Taylor length = efi.dki_length; 1403c7f714e2SEric Taylor /* 1404c7f714e2SEric Taylor * Some clients may attempt to request a PMBR for the 1405c7f714e2SEric Taylor * zvol. Currently this interface will return EINVAL to 1406c7f714e2SEric Taylor * such requests. These requests could be supported by 1407c7f714e2SEric Taylor * adding a check for lba == 0 and consing up an appropriate 1408c7f714e2SEric Taylor * PMBR. 1409c7f714e2SEric Taylor */ 1410c7f714e2SEric Taylor if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0) 1411c7f714e2SEric Taylor return (EINVAL); 1412c7f714e2SEric Taylor 1413c7f714e2SEric Taylor gpe.efi_gpe_StartingLBA = LE_64(34ULL); 1414c7f714e2SEric Taylor gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1); 1415c7f714e2SEric Taylor UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid); 1416c7f714e2SEric Taylor 1417c7f714e2SEric Taylor if (efi.dki_lba == 1) { 1418c7f714e2SEric Taylor efi_gpt_t gpt = { 0 }; 1419c7f714e2SEric Taylor 1420c7f714e2SEric Taylor gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE); 1421c7f714e2SEric Taylor gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT); 1422c7f714e2SEric Taylor gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt)); 1423c7f714e2SEric Taylor gpt.efi_gpt_MyLBA = LE_64(1ULL); 1424c7f714e2SEric Taylor gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL); 1425c7f714e2SEric Taylor gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1); 1426c7f714e2SEric Taylor gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL); 1427c7f714e2SEric Taylor gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1); 1428c7f714e2SEric Taylor gpt.efi_gpt_SizeOfPartitionEntry = 1429c7f714e2SEric Taylor LE_32(sizeof (efi_gpe_t)); 1430c7f714e2SEric Taylor CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table); 1431c7f714e2SEric Taylor gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc); 1432c7f714e2SEric Taylor CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table); 1433c7f714e2SEric Taylor gpt.efi_gpt_HeaderCRC32 = LE_32(~crc); 1434c7f714e2SEric Taylor if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length), 1435c7f714e2SEric Taylor flag)) 1436c7f714e2SEric Taylor return (EFAULT); 1437c7f714e2SEric Taylor ptr += sizeof (gpt); 1438c7f714e2SEric Taylor length -= sizeof (gpt); 1439c7f714e2SEric Taylor } 1440c7f714e2SEric Taylor if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe), 1441c7f714e2SEric Taylor length), flag)) 1442c7f714e2SEric Taylor return (EFAULT); 1443c7f714e2SEric Taylor return (0); 1444c7f714e2SEric Taylor } 1445c7f714e2SEric Taylor 1446fa9e4066Sahrens /* 1447fa9e4066Sahrens * Dirtbag ioctls to support mkfs(1M) for UFS filesystems. See dkio(7I). 1448fa9e4066Sahrens */ 1449fa9e4066Sahrens /*ARGSUSED*/ 1450fa9e4066Sahrens int 1451fa9e4066Sahrens zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 1452fa9e4066Sahrens { 1453fa9e4066Sahrens zvol_state_t *zv; 1454af2c4821Smaybee struct dk_cinfo dki; 1455fa9e4066Sahrens struct dk_minfo dkm; 1456af2c4821Smaybee struct dk_callback *dkc; 1457fa9e4066Sahrens int error = 0; 1458e7cbe64fSgw rl_t *rl; 1459fa9e4066Sahrens 1460fa9e4066Sahrens mutex_enter(&zvol_state_lock); 1461fa9e4066Sahrens 1462fa9e4066Sahrens zv = ddi_get_soft_state(zvol_state, getminor(dev)); 1463fa9e4066Sahrens 1464fa9e4066Sahrens if (zv == NULL) { 1465fa9e4066Sahrens mutex_exit(&zvol_state_lock); 1466fa9e4066Sahrens return (ENXIO); 1467fa9e4066Sahrens } 1468701f66c4SEric Taylor ASSERT(zv->zv_total_opens > 0); 1469fa9e4066Sahrens 1470fa9e4066Sahrens switch (cmd) { 1471fa9e4066Sahrens 1472fa9e4066Sahrens case DKIOCINFO: 1473af2c4821Smaybee bzero(&dki, sizeof (dki)); 1474af2c4821Smaybee (void) strcpy(dki.dki_cname, "zvol"); 1475af2c4821Smaybee (void) strcpy(dki.dki_dname, "zvol"); 1476af2c4821Smaybee dki.dki_ctype = DKC_UNKNOWN; 1477af2c4821Smaybee dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs); 1478fa9e4066Sahrens mutex_exit(&zvol_state_lock); 1479af2c4821Smaybee if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag)) 1480fa9e4066Sahrens error = EFAULT; 1481fa9e4066Sahrens return (error); 1482fa9e4066Sahrens 1483fa9e4066Sahrens case DKIOCGMEDIAINFO: 1484fa9e4066Sahrens bzero(&dkm, sizeof (dkm)); 1485fa9e4066Sahrens dkm.dki_lbsize = 1U << zv->zv_min_bs; 1486fa9e4066Sahrens dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs; 1487fa9e4066Sahrens dkm.dki_media_type = DK_UNKNOWN; 1488fa9e4066Sahrens mutex_exit(&zvol_state_lock); 1489fa9e4066Sahrens if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag)) 1490fa9e4066Sahrens error = EFAULT; 1491fa9e4066Sahrens return (error); 1492fa9e4066Sahrens 1493fa9e4066Sahrens case DKIOCGETEFI: 1494c7f714e2SEric Taylor { 1495c7f714e2SEric Taylor uint64_t vs = zv->zv_volsize; 1496c7f714e2SEric Taylor uint8_t bs = zv->zv_min_bs; 1497fa9e4066Sahrens 149868a5ac4dSmaybee mutex_exit(&zvol_state_lock); 1499c7f714e2SEric Taylor error = zvol_getefi((void *)arg, flag, vs, bs); 1500c7f714e2SEric Taylor return (error); 150168a5ac4dSmaybee } 1502fa9e4066Sahrens 1503feb08c6bSbillm case DKIOCFLUSHWRITECACHE: 1504af2c4821Smaybee dkc = (struct dk_callback *)arg; 1505701f66c4SEric Taylor mutex_exit(&zvol_state_lock); 1506feb08c6bSbillm zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ); 1507af2c4821Smaybee if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) { 1508af2c4821Smaybee (*dkc->dkc_callback)(dkc->dkc_cookie, error); 1509af2c4821Smaybee error = 0; 1510af2c4821Smaybee } 1511701f66c4SEric Taylor return (error); 1512701f66c4SEric Taylor 1513701f66c4SEric Taylor case DKIOCGETWCE: 1514701f66c4SEric Taylor { 1515701f66c4SEric Taylor int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0; 1516701f66c4SEric Taylor if (ddi_copyout(&wce, (void *)arg, sizeof (int), 1517701f66c4SEric Taylor flag)) 1518701f66c4SEric Taylor error = EFAULT; 1519701f66c4SEric Taylor break; 1520701f66c4SEric Taylor } 1521701f66c4SEric Taylor case DKIOCSETWCE: 1522701f66c4SEric Taylor { 1523701f66c4SEric Taylor int wce; 1524701f66c4SEric Taylor if (ddi_copyin((void *)arg, &wce, sizeof (int), 1525701f66c4SEric Taylor flag)) { 1526701f66c4SEric Taylor error = EFAULT; 1527701f66c4SEric Taylor break; 1528701f66c4SEric Taylor } 1529701f66c4SEric Taylor if (wce) { 1530701f66c4SEric Taylor zv->zv_flags |= ZVOL_WCE; 1531701f66c4SEric Taylor mutex_exit(&zvol_state_lock); 1532701f66c4SEric Taylor } else { 1533701f66c4SEric Taylor zv->zv_flags &= ~ZVOL_WCE; 1534701f66c4SEric Taylor mutex_exit(&zvol_state_lock); 1535701f66c4SEric Taylor zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ); 1536701f66c4SEric Taylor } 1537701f66c4SEric Taylor return (0); 1538701f66c4SEric Taylor } 1539feb08c6bSbillm 1540b6130eadSmaybee case DKIOCGGEOM: 1541b6130eadSmaybee case DKIOCGVTOC: 1542e7cbe64fSgw /* 1543e7cbe64fSgw * commands using these (like prtvtoc) expect ENOTSUP 1544e7cbe64fSgw * since we're emulating an EFI label 1545e7cbe64fSgw */ 1546b6130eadSmaybee error = ENOTSUP; 1547b6130eadSmaybee break; 1548b6130eadSmaybee 1549e7cbe64fSgw case DKIOCDUMPINIT: 1550e7cbe64fSgw rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 1551e7cbe64fSgw RL_WRITER); 1552e7cbe64fSgw error = zvol_dumpify(zv); 1553e7cbe64fSgw zfs_range_unlock(rl); 1554e7cbe64fSgw break; 1555e7cbe64fSgw 1556e7cbe64fSgw case DKIOCDUMPFINI: 155706d5ae10SEric Taylor if (!(zv->zv_flags & ZVOL_DUMPIFIED)) 155806d5ae10SEric Taylor break; 1559e7cbe64fSgw rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 1560e7cbe64fSgw RL_WRITER); 1561e7cbe64fSgw error = zvol_dump_fini(zv); 1562e7cbe64fSgw zfs_range_unlock(rl); 1563e7cbe64fSgw break; 1564e7cbe64fSgw 1565fa9e4066Sahrens default: 156668a5ac4dSmaybee error = ENOTTY; 1567fa9e4066Sahrens break; 1568fa9e4066Sahrens 1569fa9e4066Sahrens } 1570fa9e4066Sahrens mutex_exit(&zvol_state_lock); 1571fa9e4066Sahrens return (error); 1572fa9e4066Sahrens } 1573fa9e4066Sahrens 1574fa9e4066Sahrens int 1575fa9e4066Sahrens zvol_busy(void) 1576fa9e4066Sahrens { 1577fa9e4066Sahrens return (zvol_minors != 0); 1578fa9e4066Sahrens } 1579fa9e4066Sahrens 1580fa9e4066Sahrens void 1581fa9e4066Sahrens zvol_init(void) 1582fa9e4066Sahrens { 1583fa9e4066Sahrens VERIFY(ddi_soft_state_init(&zvol_state, sizeof (zvol_state_t), 1) == 0); 1584fa9e4066Sahrens mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL); 1585fa9e4066Sahrens } 1586fa9e4066Sahrens 1587fa9e4066Sahrens void 1588fa9e4066Sahrens zvol_fini(void) 1589fa9e4066Sahrens { 1590fa9e4066Sahrens mutex_destroy(&zvol_state_lock); 1591fa9e4066Sahrens ddi_soft_state_fini(&zvol_state); 1592fa9e4066Sahrens } 1593e7cbe64fSgw 1594e7cbe64fSgw static int 1595e7cbe64fSgw zvol_dump_init(zvol_state_t *zv, boolean_t resize) 1596e7cbe64fSgw { 1597e7cbe64fSgw dmu_tx_t *tx; 1598e7cbe64fSgw int error = 0; 1599e7cbe64fSgw objset_t *os = zv->zv_objset; 1600e7cbe64fSgw nvlist_t *nv = NULL; 1601e7cbe64fSgw 1602e7cbe64fSgw ASSERT(MUTEX_HELD(&zvol_state_lock)); 1603681d9761SEric Taylor error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0, 1604681d9761SEric Taylor DMU_OBJECT_END); 1605681d9761SEric Taylor /* wait for dmu_free_long_range to actually free the blocks */ 1606681d9761SEric Taylor txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 1607e7cbe64fSgw 1608e7cbe64fSgw tx = dmu_tx_create(os); 1609e7cbe64fSgw dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 1610681d9761SEric Taylor dmu_tx_hold_bonus(tx, ZVOL_OBJ); 1611e7cbe64fSgw error = dmu_tx_assign(tx, TXG_WAIT); 1612e7cbe64fSgw if (error) { 1613e7cbe64fSgw dmu_tx_abort(tx); 1614e7cbe64fSgw return (error); 1615e7cbe64fSgw } 1616e7cbe64fSgw 1617e7cbe64fSgw /* 1618e7cbe64fSgw * If we are resizing the dump device then we only need to 1619e7cbe64fSgw * update the refreservation to match the newly updated 1620e7cbe64fSgw * zvolsize. Otherwise, we save off the original state of the 1621e7cbe64fSgw * zvol so that we can restore them if the zvol is ever undumpified. 1622e7cbe64fSgw */ 1623e7cbe64fSgw if (resize) { 1624e7cbe64fSgw error = zap_update(os, ZVOL_ZAP_OBJ, 1625e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 1626e7cbe64fSgw &zv->zv_volsize, tx); 1627e7cbe64fSgw } else { 162888b7b0f2SMatthew Ahrens uint64_t checksum, compress, refresrv, vbs; 162988b7b0f2SMatthew Ahrens 1630e7cbe64fSgw error = dsl_prop_get_integer(zv->zv_name, 1631e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL); 1632e7cbe64fSgw error = error ? error : dsl_prop_get_integer(zv->zv_name, 1633e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL); 1634e7cbe64fSgw error = error ? error : dsl_prop_get_integer(zv->zv_name, 1635e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL); 163688b7b0f2SMatthew Ahrens error = error ? error : dsl_prop_get_integer(zv->zv_name, 163788b7b0f2SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL); 1638e7cbe64fSgw 1639e7cbe64fSgw error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1640e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, 1641e7cbe64fSgw &compress, tx); 1642e7cbe64fSgw error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1643e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx); 1644e7cbe64fSgw error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1645e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 1646e7cbe64fSgw &refresrv, tx); 164788b7b0f2SMatthew Ahrens error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 164888b7b0f2SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, 164988b7b0f2SMatthew Ahrens &vbs, tx); 1650681d9761SEric Taylor error = error ? error : dmu_object_set_blocksize( 1651681d9761SEric Taylor os, ZVOL_OBJ, SPA_MAXBLOCKSIZE, 0, tx); 1652681d9761SEric Taylor if (error == 0) 1653681d9761SEric Taylor zv->zv_volblocksize = SPA_MAXBLOCKSIZE; 1654e7cbe64fSgw } 1655e7cbe64fSgw dmu_tx_commit(tx); 1656e7cbe64fSgw 1657e7cbe64fSgw /* 1658e7cbe64fSgw * We only need update the zvol's property if we are initializing 1659e7cbe64fSgw * the dump area for the first time. 1660e7cbe64fSgw */ 1661e7cbe64fSgw if (!resize) { 1662e7cbe64fSgw VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1663e7cbe64fSgw VERIFY(nvlist_add_uint64(nv, 1664e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0); 1665e7cbe64fSgw VERIFY(nvlist_add_uint64(nv, 1666e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), 1667e7cbe64fSgw ZIO_COMPRESS_OFF) == 0); 1668e7cbe64fSgw VERIFY(nvlist_add_uint64(nv, 1669e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_CHECKSUM), 1670e7cbe64fSgw ZIO_CHECKSUM_OFF) == 0); 1671e7cbe64fSgw 1672e7cbe64fSgw error = zfs_set_prop_nvlist(zv->zv_name, nv); 1673e7cbe64fSgw nvlist_free(nv); 1674e7cbe64fSgw 1675e7cbe64fSgw if (error) 1676e7cbe64fSgw return (error); 1677e7cbe64fSgw } 1678e7cbe64fSgw 1679e7cbe64fSgw /* Allocate the space for the dump */ 1680e7cbe64fSgw error = zvol_prealloc(zv); 1681e7cbe64fSgw return (error); 1682e7cbe64fSgw } 1683e7cbe64fSgw 1684e7cbe64fSgw static int 1685e7cbe64fSgw zvol_dumpify(zvol_state_t *zv) 1686e7cbe64fSgw { 1687e7cbe64fSgw int error = 0; 1688e7cbe64fSgw uint64_t dumpsize = 0; 1689e7cbe64fSgw dmu_tx_t *tx; 1690e7cbe64fSgw objset_t *os = zv->zv_objset; 1691e7cbe64fSgw 1692681d9761SEric Taylor if (zv->zv_flags & ZVOL_RDONLY) 1693e7cbe64fSgw return (EROFS); 1694e7cbe64fSgw 1695e7cbe64fSgw if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 1696e7cbe64fSgw 8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) { 1697e7cbe64fSgw boolean_t resize = (dumpsize > 0) ? B_TRUE : B_FALSE; 1698e7cbe64fSgw 1699e7cbe64fSgw if ((error = zvol_dump_init(zv, resize)) != 0) { 1700e7cbe64fSgw (void) zvol_dump_fini(zv); 1701e7cbe64fSgw return (error); 1702e7cbe64fSgw } 1703e7cbe64fSgw } 1704e7cbe64fSgw 1705e7cbe64fSgw /* 1706e7cbe64fSgw * Build up our lba mapping. 1707e7cbe64fSgw */ 1708e7cbe64fSgw error = zvol_get_lbas(zv); 1709e7cbe64fSgw if (error) { 1710e7cbe64fSgw (void) zvol_dump_fini(zv); 1711e7cbe64fSgw return (error); 1712e7cbe64fSgw } 1713e7cbe64fSgw 1714e7cbe64fSgw tx = dmu_tx_create(os); 1715e7cbe64fSgw dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 1716e7cbe64fSgw error = dmu_tx_assign(tx, TXG_WAIT); 1717e7cbe64fSgw if (error) { 1718e7cbe64fSgw dmu_tx_abort(tx); 1719e7cbe64fSgw (void) zvol_dump_fini(zv); 1720e7cbe64fSgw return (error); 1721e7cbe64fSgw } 1722e7cbe64fSgw 1723e7cbe64fSgw zv->zv_flags |= ZVOL_DUMPIFIED; 1724e7cbe64fSgw error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1, 1725e7cbe64fSgw &zv->zv_volsize, tx); 1726e7cbe64fSgw dmu_tx_commit(tx); 1727e7cbe64fSgw 1728e7cbe64fSgw if (error) { 1729e7cbe64fSgw (void) zvol_dump_fini(zv); 1730e7cbe64fSgw return (error); 1731e7cbe64fSgw } 1732e7cbe64fSgw 1733e7cbe64fSgw txg_wait_synced(dmu_objset_pool(os), 0); 1734e7cbe64fSgw return (0); 1735e7cbe64fSgw } 1736e7cbe64fSgw 1737e7cbe64fSgw static int 1738e7cbe64fSgw zvol_dump_fini(zvol_state_t *zv) 1739e7cbe64fSgw { 1740e7cbe64fSgw dmu_tx_t *tx; 1741e7cbe64fSgw objset_t *os = zv->zv_objset; 1742e7cbe64fSgw nvlist_t *nv; 1743e7cbe64fSgw int error = 0; 174488b7b0f2SMatthew Ahrens uint64_t checksum, compress, refresrv, vbs; 1745e7cbe64fSgw 1746b7e50089Smaybee /* 1747b7e50089Smaybee * Attempt to restore the zvol back to its pre-dumpified state. 1748b7e50089Smaybee * This is a best-effort attempt as it's possible that not all 1749b7e50089Smaybee * of these properties were initialized during the dumpify process 1750b7e50089Smaybee * (i.e. error during zvol_dump_init). 1751b7e50089Smaybee */ 1752b7e50089Smaybee 1753e7cbe64fSgw tx = dmu_tx_create(os); 1754e7cbe64fSgw dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 1755e7cbe64fSgw error = dmu_tx_assign(tx, TXG_WAIT); 1756e7cbe64fSgw if (error) { 1757e7cbe64fSgw dmu_tx_abort(tx); 1758e7cbe64fSgw return (error); 1759e7cbe64fSgw } 1760b7e50089Smaybee (void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx); 1761b7e50089Smaybee dmu_tx_commit(tx); 1762e7cbe64fSgw 1763e7cbe64fSgw (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 1764e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum); 1765e7cbe64fSgw (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 1766e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress); 1767e7cbe64fSgw (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 1768e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv); 176988b7b0f2SMatthew Ahrens (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 177088b7b0f2SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs); 1771e7cbe64fSgw 1772e7cbe64fSgw VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1773e7cbe64fSgw (void) nvlist_add_uint64(nv, 1774e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum); 1775e7cbe64fSgw (void) nvlist_add_uint64(nv, 1776e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress); 1777e7cbe64fSgw (void) nvlist_add_uint64(nv, 1778e7cbe64fSgw zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv); 1779e7cbe64fSgw (void) zfs_set_prop_nvlist(zv->zv_name, nv); 1780e7cbe64fSgw nvlist_free(nv); 1781e7cbe64fSgw 1782b7e50089Smaybee zvol_free_extents(zv); 1783b7e50089Smaybee zv->zv_flags &= ~ZVOL_DUMPIFIED; 1784b7e50089Smaybee (void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END); 1785681d9761SEric Taylor /* wait for dmu_free_long_range to actually free the blocks */ 1786681d9761SEric Taylor txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 1787681d9761SEric Taylor tx = dmu_tx_create(os); 1788681d9761SEric Taylor dmu_tx_hold_bonus(tx, ZVOL_OBJ); 1789681d9761SEric Taylor error = dmu_tx_assign(tx, TXG_WAIT); 1790681d9761SEric Taylor if (error) { 1791681d9761SEric Taylor dmu_tx_abort(tx); 1792681d9761SEric Taylor return (error); 1793681d9761SEric Taylor } 17945c987a37SChris Kirby (void) dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx); 1795681d9761SEric Taylor dmu_tx_commit(tx); 1796b7e50089Smaybee 1797e7cbe64fSgw return (0); 1798e7cbe64fSgw } 1799