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 /* 22*f80ce222SChris Kirby * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23fa9e4066Sahrens */ 24fa9e4066Sahrens 25fa9e4066Sahrens /* 26fa9e4066Sahrens * ZFS volume emulation driver. 27fa9e4066Sahrens * 28fa9e4066Sahrens * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes. 29fa9e4066Sahrens * Volumes are accessed through the symbolic links named: 30fa9e4066Sahrens * 31fa9e4066Sahrens * /dev/zvol/dsk/<pool_name>/<dataset_name> 32fa9e4066Sahrens * /dev/zvol/rdsk/<pool_name>/<dataset_name> 33fa9e4066Sahrens * 34681d9761SEric Taylor * These links are created by the /dev filesystem (sdev_zvolops.c). 35fa9e4066Sahrens * Volumes are persistent through reboot. No user command needs to be 36fa9e4066Sahrens * run before opening and using a device. 37fa9e4066Sahrens */ 38fa9e4066Sahrens 39fa9e4066Sahrens #include <sys/types.h> 40fa9e4066Sahrens #include <sys/param.h> 41fa9e4066Sahrens #include <sys/errno.h> 42fa9e4066Sahrens #include <sys/uio.h> 43fa9e4066Sahrens #include <sys/buf.h> 44fa9e4066Sahrens #include <sys/modctl.h> 45fa9e4066Sahrens #include <sys/open.h> 46fa9e4066Sahrens #include <sys/kmem.h> 47fa9e4066Sahrens #include <sys/conf.h> 48fa9e4066Sahrens #include <sys/cmn_err.h> 49fa9e4066Sahrens #include <sys/stat.h> 50fa9e4066Sahrens #include <sys/zap.h> 51fa9e4066Sahrens #include <sys/spa.h> 52fa9e4066Sahrens #include <sys/zio.h> 53e7cbe64fSgw25295 #include <sys/dmu_traverse.h> 54e7cbe64fSgw25295 #include <sys/dnode.h> 55e7cbe64fSgw25295 #include <sys/dsl_dataset.h> 56fa9e4066Sahrens #include <sys/dsl_prop.h> 57fa9e4066Sahrens #include <sys/dkio.h> 58fa9e4066Sahrens #include <sys/efi_partition.h> 59fa9e4066Sahrens #include <sys/byteorder.h> 60fa9e4066Sahrens #include <sys/pathname.h> 61fa9e4066Sahrens #include <sys/ddi.h> 62fa9e4066Sahrens #include <sys/sunddi.h> 63fa9e4066Sahrens #include <sys/crc32.h> 64fa9e4066Sahrens #include <sys/dirent.h> 65fa9e4066Sahrens #include <sys/policy.h> 66fa9e4066Sahrens #include <sys/fs/zfs.h> 67fa9e4066Sahrens #include <sys/zfs_ioctl.h> 68fa9e4066Sahrens #include <sys/mkdev.h> 6922ac5be4Sperrin #include <sys/zil.h> 70c5c6ffa0Smaybee #include <sys/refcount.h> 71c2e6a7d6Sperrin #include <sys/zfs_znode.h> 72c2e6a7d6Sperrin #include <sys/zfs_rlock.h> 73e7cbe64fSgw25295 #include <sys/vdev_disk.h> 74e7cbe64fSgw25295 #include <sys/vdev_impl.h> 75e7cbe64fSgw25295 #include <sys/zvol.h> 76e7cbe64fSgw25295 #include <sys/dumphdr.h> 771209a471SNeil Perrin #include <sys/zil_impl.h> 78fa9e4066Sahrens 79fa9e4066Sahrens #include "zfs_namecheck.h" 80fa9e4066Sahrens 81fa9e4066Sahrens static void *zvol_state; 82503ad85cSMatthew Ahrens static char *zvol_tag = "zvol_tag"; 83fa9e4066Sahrens 84e7cbe64fSgw25295 #define ZVOL_DUMPSIZE "dumpsize" 85e7cbe64fSgw25295 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 95e7cbe64fSgw25295 typedef struct zvol_extent { 9688b7b0f2SMatthew Ahrens list_node_t ze_node; 97e7cbe64fSgw25295 dva_t ze_dva; /* dva associated with this extent */ 9888b7b0f2SMatthew Ahrens uint64_t ze_nblks; /* number of blocks in extent */ 99e7cbe64fSgw25295 } zvol_extent_t; 100e7cbe64fSgw25295 101e7cbe64fSgw25295 /* 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 */ 110701f66c4SEric Taylor uint8_t zv_flags; /* readonly, dumpified, etc. */ 111fa9e4066Sahrens objset_t *zv_objset; /* objset handle */ 112fa9e4066Sahrens uint32_t zv_open_count[OTYPCNT]; /* open counts */ 113fa9e4066Sahrens uint32_t zv_total_opens; /* total open count */ 11422ac5be4Sperrin zilog_t *zv_zilog; /* ZIL handle */ 11588b7b0f2SMatthew Ahrens list_t zv_extents; /* List of extents for dump */ 116c2e6a7d6Sperrin znode_t zv_znode; /* for range locking */ 117fa9e4066Sahrens } zvol_state_t; 118fa9e4066Sahrens 11967bd71c6Sperrin /* 120e7cbe64fSgw25295 * zvol specific flags 121e7cbe64fSgw25295 */ 122e7cbe64fSgw25295 #define ZVOL_RDONLY 0x1 123e7cbe64fSgw25295 #define ZVOL_DUMPIFIED 0x2 124c7f714e2SEric Taylor #define ZVOL_EXCL 0x4 125701f66c4SEric Taylor #define ZVOL_WCE 0x8 126e7cbe64fSgw25295 127e7cbe64fSgw25295 /* 12867bd71c6Sperrin * zvol maximum transfer in one DMU tx. 12967bd71c6Sperrin */ 13067bd71c6Sperrin int zvol_maxphys = DMU_MAX_ACCESS/2; 13167bd71c6Sperrin 13292241e0bSTom Erickson extern int zfs_set_prop_nvlist(const char *, zprop_source_t, 13392241e0bSTom Erickson nvlist_t *, nvlist_t **); 134681d9761SEric Taylor static int zvol_remove_zv(zvol_state_t *); 135feb08c6bSbillm static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio); 136e7cbe64fSgw25295 static int zvol_dumpify(zvol_state_t *zv); 137e7cbe64fSgw25295 static int zvol_dump_fini(zvol_state_t *zv); 138e7cbe64fSgw25295 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); 149e7cbe64fSgw25295 150e7cbe64fSgw25295 /* Notify specfs to invalidate the cached size */ 151e7cbe64fSgw25295 spec_size_invalidate(dev, VBLK); 152e7cbe64fSgw25295 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) 235*f80ce222SChris Kirby return (zv); 236fa9e4066Sahrens } 237fa9e4066Sahrens 238*f80ce222SChris Kirby return (NULL); 239fa9e4066Sahrens } 240fa9e4066Sahrens 241e7cbe64fSgw25295 /* extent mapping arg */ 242e7cbe64fSgw25295 struct maparg { 24388b7b0f2SMatthew Ahrens zvol_state_t *ma_zv; 24488b7b0f2SMatthew Ahrens uint64_t ma_blks; 245e7cbe64fSgw25295 }; 246e7cbe64fSgw25295 247e7cbe64fSgw25295 /*ARGSUSED*/ 248e7cbe64fSgw25295 static int 249b24ab676SJeff Bonwick zvol_map_block(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 250b24ab676SJeff Bonwick const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg) 251e7cbe64fSgw25295 { 25288b7b0f2SMatthew Ahrens struct maparg *ma = arg; 25388b7b0f2SMatthew Ahrens zvol_extent_t *ze; 25488b7b0f2SMatthew Ahrens int bs = ma->ma_zv->zv_volblocksize; 255e7cbe64fSgw25295 25688b7b0f2SMatthew Ahrens if (bp == NULL || zb->zb_object != ZVOL_OBJ || zb->zb_level != 0) 257e7cbe64fSgw25295 return (0); 258e7cbe64fSgw25295 25988b7b0f2SMatthew Ahrens VERIFY3U(ma->ma_blks, ==, zb->zb_blkid); 26088b7b0f2SMatthew Ahrens ma->ma_blks++; 26188b7b0f2SMatthew Ahrens 262e7cbe64fSgw25295 /* Abort immediately if we have encountered gang blocks */ 26388b7b0f2SMatthew Ahrens if (BP_IS_GANG(bp)) 26488b7b0f2SMatthew Ahrens return (EFRAGS); 265e7cbe64fSgw25295 266e7cbe64fSgw25295 /* 26788b7b0f2SMatthew Ahrens * See if the block is at the end of the previous extent. 268e7cbe64fSgw25295 */ 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); 27688b7b0f2SMatthew Ahrens } 27788b7b0f2SMatthew Ahrens 278e7cbe64fSgw25295 dprintf_bp(bp, "%s", "next blkptr:"); 27988b7b0f2SMatthew Ahrens 280e7cbe64fSgw25295 /* 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); 286e7cbe64fSgw25295 } 28788b7b0f2SMatthew Ahrens 28888b7b0f2SMatthew Ahrens static void 28988b7b0f2SMatthew Ahrens zvol_free_extents(zvol_state_t *zv) 29088b7b0f2SMatthew Ahrens { 29188b7b0f2SMatthew Ahrens zvol_extent_t *ze; 29288b7b0f2SMatthew Ahrens 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)); 29688b7b0f2SMatthew Ahrens } 29788b7b0f2SMatthew Ahrens } 29888b7b0f2SMatthew Ahrens 29988b7b0f2SMatthew Ahrens static int 30088b7b0f2SMatthew Ahrens zvol_get_lbas(zvol_state_t *zv) 30188b7b0f2SMatthew Ahrens { 3023adc9019SEric Taylor objset_t *os = zv->zv_objset; 30388b7b0f2SMatthew Ahrens struct maparg ma; 30488b7b0f2SMatthew Ahrens int err; 30588b7b0f2SMatthew Ahrens 30688b7b0f2SMatthew Ahrens ma.ma_zv = zv; 30788b7b0f2SMatthew Ahrens ma.ma_blks = 0; 30888b7b0f2SMatthew Ahrens zvol_free_extents(zv); 30988b7b0f2SMatthew Ahrens 3103adc9019SEric Taylor /* commit any in-flight changes before traversing the dataset */ 3113adc9019SEric Taylor txg_wait_synced(dmu_objset_pool(os), 0); 3123adc9019SEric Taylor err = traverse_dataset(dmu_objset_ds(os), 0, 31388b7b0f2SMatthew Ahrens TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma); 31488b7b0f2SMatthew Ahrens if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) { 31588b7b0f2SMatthew Ahrens zvol_free_extents(zv); 31688b7b0f2SMatthew Ahrens return (err ? err : EIO); 31788b7b0f2SMatthew Ahrens } 31888b7b0f2SMatthew Ahrens 319e7cbe64fSgw25295 return (0); 320e7cbe64fSgw25295 } 321e7cbe64fSgw25295 322ecd6cf80Smarks /* ARGSUSED */ 323fa9e4066Sahrens void 324ecd6cf80Smarks zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 325fa9e4066Sahrens { 326da6c28aaSamw zfs_creat_t *zct = arg; 327da6c28aaSamw nvlist_t *nvprops = zct->zct_props; 328fa9e4066Sahrens int error; 329e9dbad6fSeschrock uint64_t volblocksize, volsize; 330fa9e4066Sahrens 331ecd6cf80Smarks VERIFY(nvlist_lookup_uint64(nvprops, 332e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0); 333ecd6cf80Smarks if (nvlist_lookup_uint64(nvprops, 334e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0) 335e9dbad6fSeschrock volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 336e9dbad6fSeschrock 337e9dbad6fSeschrock /* 338e7cbe64fSgw25295 * These properties must be removed from the list so the generic 339e9dbad6fSeschrock * property setting step won't apply to them. 340e9dbad6fSeschrock */ 341ecd6cf80Smarks VERIFY(nvlist_remove_all(nvprops, 342e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0); 343ecd6cf80Smarks (void) nvlist_remove_all(nvprops, 344e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE)); 345e9dbad6fSeschrock 346e9dbad6fSeschrock error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize, 347fa9e4066Sahrens DMU_OT_NONE, 0, tx); 348fa9e4066Sahrens ASSERT(error == 0); 349fa9e4066Sahrens 350fa9e4066Sahrens error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP, 351fa9e4066Sahrens DMU_OT_NONE, 0, tx); 352fa9e4066Sahrens ASSERT(error == 0); 353fa9e4066Sahrens 354e9dbad6fSeschrock error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx); 355fa9e4066Sahrens ASSERT(error == 0); 356fa9e4066Sahrens } 357fa9e4066Sahrens 358fa9e4066Sahrens /* 35922ac5be4Sperrin * Replay a TX_WRITE ZIL transaction that didn't get committed 36022ac5be4Sperrin * after a system failure 36122ac5be4Sperrin */ 36222ac5be4Sperrin static int 36322ac5be4Sperrin zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap) 36422ac5be4Sperrin { 36522ac5be4Sperrin objset_t *os = zv->zv_objset; 36622ac5be4Sperrin char *data = (char *)(lr + 1); /* data follows lr_write_t */ 367b24ab676SJeff Bonwick uint64_t offset, length; 36822ac5be4Sperrin dmu_tx_t *tx; 36922ac5be4Sperrin int error; 37022ac5be4Sperrin 37122ac5be4Sperrin if (byteswap) 37222ac5be4Sperrin byteswap_uint64_array(lr, sizeof (*lr)); 37322ac5be4Sperrin 374b24ab676SJeff Bonwick offset = lr->lr_offset; 375b24ab676SJeff Bonwick length = lr->lr_length; 376b24ab676SJeff Bonwick 377b24ab676SJeff Bonwick /* If it's a dmu_sync() block, write the whole block */ 378b24ab676SJeff Bonwick if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) { 379b24ab676SJeff Bonwick uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr); 380b24ab676SJeff Bonwick if (length < blocksize) { 381b24ab676SJeff Bonwick offset -= offset % blocksize; 382b24ab676SJeff Bonwick length = blocksize; 383b24ab676SJeff Bonwick } 384b24ab676SJeff Bonwick } 385975c32a0SNeil Perrin 38622ac5be4Sperrin tx = dmu_tx_create(os); 387b24ab676SJeff Bonwick dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length); 3881209a471SNeil Perrin error = dmu_tx_assign(tx, TXG_WAIT); 38922ac5be4Sperrin if (error) { 39022ac5be4Sperrin dmu_tx_abort(tx); 39122ac5be4Sperrin } else { 392b24ab676SJeff Bonwick dmu_write(os, ZVOL_OBJ, offset, length, data, tx); 39322ac5be4Sperrin dmu_tx_commit(tx); 39422ac5be4Sperrin } 39522ac5be4Sperrin 39622ac5be4Sperrin return (error); 39722ac5be4Sperrin } 39822ac5be4Sperrin 39922ac5be4Sperrin /* ARGSUSED */ 40022ac5be4Sperrin static int 40122ac5be4Sperrin zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap) 40222ac5be4Sperrin { 40322ac5be4Sperrin return (ENOTSUP); 40422ac5be4Sperrin } 40522ac5be4Sperrin 40622ac5be4Sperrin /* 40722ac5be4Sperrin * Callback vectors for replaying records. 40822ac5be4Sperrin * Only TX_WRITE is needed for zvol. 40922ac5be4Sperrin */ 41022ac5be4Sperrin zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = { 41122ac5be4Sperrin zvol_replay_err, /* 0 no such transaction type */ 41222ac5be4Sperrin zvol_replay_err, /* TX_CREATE */ 41322ac5be4Sperrin zvol_replay_err, /* TX_MKDIR */ 41422ac5be4Sperrin zvol_replay_err, /* TX_MKXATTR */ 41522ac5be4Sperrin zvol_replay_err, /* TX_SYMLINK */ 41622ac5be4Sperrin zvol_replay_err, /* TX_REMOVE */ 41722ac5be4Sperrin zvol_replay_err, /* TX_RMDIR */ 41822ac5be4Sperrin zvol_replay_err, /* TX_LINK */ 41922ac5be4Sperrin zvol_replay_err, /* TX_RENAME */ 42022ac5be4Sperrin zvol_replay_write, /* TX_WRITE */ 42122ac5be4Sperrin zvol_replay_err, /* TX_TRUNCATE */ 42222ac5be4Sperrin zvol_replay_err, /* TX_SETATTR */ 42322ac5be4Sperrin zvol_replay_err, /* TX_ACL */ 424975c32a0SNeil Perrin zvol_replay_err, /* TX_CREATE_ACL */ 425975c32a0SNeil Perrin zvol_replay_err, /* TX_CREATE_ATTR */ 426975c32a0SNeil Perrin zvol_replay_err, /* TX_CREATE_ACL_ATTR */ 427975c32a0SNeil Perrin zvol_replay_err, /* TX_MKDIR_ACL */ 428975c32a0SNeil Perrin zvol_replay_err, /* TX_MKDIR_ATTR */ 429975c32a0SNeil Perrin zvol_replay_err, /* TX_MKDIR_ACL_ATTR */ 430975c32a0SNeil Perrin zvol_replay_err, /* TX_WRITE2 */ 43122ac5be4Sperrin }; 43222ac5be4Sperrin 433681d9761SEric Taylor int 434681d9761SEric Taylor zvol_name2minor(const char *name, minor_t *minor) 435681d9761SEric Taylor { 436681d9761SEric Taylor zvol_state_t *zv; 437681d9761SEric Taylor 438681d9761SEric Taylor mutex_enter(&zvol_state_lock); 439681d9761SEric Taylor zv = zvol_minor_lookup(name); 440681d9761SEric Taylor if (minor && zv) 441681d9761SEric Taylor *minor = zv->zv_minor; 442681d9761SEric Taylor mutex_exit(&zvol_state_lock); 443681d9761SEric Taylor return (zv ? 0 : -1); 444681d9761SEric Taylor } 445681d9761SEric Taylor 44622ac5be4Sperrin /* 447e7cbe64fSgw25295 * Create a minor node (plus a whole lot more) for the specified volume. 448fa9e4066Sahrens */ 449fa9e4066Sahrens int 450681d9761SEric Taylor zvol_create_minor(const char *name) 451fa9e4066Sahrens { 452fa9e4066Sahrens zvol_state_t *zv; 453fa9e4066Sahrens objset_t *os; 45467bd71c6Sperrin dmu_object_info_t doi; 455fa9e4066Sahrens minor_t minor = 0; 456fa9e4066Sahrens char chrbuf[30], blkbuf[30]; 457fa9e4066Sahrens int error; 458fa9e4066Sahrens 459fa9e4066Sahrens mutex_enter(&zvol_state_lock); 460fa9e4066Sahrens 4611195e687SMark J Musante if (zvol_minor_lookup(name) != NULL) { 462fa9e4066Sahrens mutex_exit(&zvol_state_lock); 463fa9e4066Sahrens return (EEXIST); 464fa9e4066Sahrens } 465fa9e4066Sahrens 466503ad85cSMatthew Ahrens /* lie and say we're read-only */ 467503ad85cSMatthew Ahrens error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, zvol_tag, &os); 468fa9e4066Sahrens 469fa9e4066Sahrens if (error) { 470fa9e4066Sahrens mutex_exit(&zvol_state_lock); 471fa9e4066Sahrens return (error); 472fa9e4066Sahrens } 473fa9e4066Sahrens 474681d9761SEric Taylor if ((minor = zvol_minor_alloc()) == 0) { 475503ad85cSMatthew Ahrens dmu_objset_disown(os, zvol_tag); 476fa9e4066Sahrens mutex_exit(&zvol_state_lock); 477fa9e4066Sahrens return (ENXIO); 478fa9e4066Sahrens } 479fa9e4066Sahrens 480fa9e4066Sahrens if (ddi_soft_state_zalloc(zvol_state, minor) != DDI_SUCCESS) { 481503ad85cSMatthew Ahrens dmu_objset_disown(os, zvol_tag); 482fa9e4066Sahrens mutex_exit(&zvol_state_lock); 483fa9e4066Sahrens return (EAGAIN); 484fa9e4066Sahrens } 485e9dbad6fSeschrock (void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME, 486e9dbad6fSeschrock (char *)name); 487fa9e4066Sahrens 488681d9761SEric Taylor (void) snprintf(chrbuf, sizeof (chrbuf), "%u,raw", minor); 489fa9e4066Sahrens 490fa9e4066Sahrens if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR, 491fa9e4066Sahrens minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 492fa9e4066Sahrens ddi_soft_state_free(zvol_state, minor); 493503ad85cSMatthew Ahrens dmu_objset_disown(os, zvol_tag); 494fa9e4066Sahrens mutex_exit(&zvol_state_lock); 495fa9e4066Sahrens return (EAGAIN); 496fa9e4066Sahrens } 497fa9e4066Sahrens 498681d9761SEric Taylor (void) snprintf(blkbuf, sizeof (blkbuf), "%u", minor); 499fa9e4066Sahrens 500fa9e4066Sahrens if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK, 501fa9e4066Sahrens minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 502fa9e4066Sahrens ddi_remove_minor_node(zfs_dip, chrbuf); 503fa9e4066Sahrens ddi_soft_state_free(zvol_state, minor); 504503ad85cSMatthew Ahrens dmu_objset_disown(os, zvol_tag); 505fa9e4066Sahrens mutex_exit(&zvol_state_lock); 506fa9e4066Sahrens return (EAGAIN); 507fa9e4066Sahrens } 508fa9e4066Sahrens 509fa9e4066Sahrens zv = ddi_get_soft_state(zvol_state, minor); 510fa9e4066Sahrens 511681d9761SEric Taylor (void) strlcpy(zv->zv_name, name, MAXPATHLEN); 512fa9e4066Sahrens zv->zv_min_bs = DEV_BSHIFT; 513fa9e4066Sahrens zv->zv_minor = minor; 514fa9e4066Sahrens zv->zv_objset = os; 515681d9761SEric Taylor if (dmu_objset_is_snapshot(os)) 516681d9761SEric Taylor zv->zv_flags |= ZVOL_RDONLY; 517c2e6a7d6Sperrin mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL); 518c2e6a7d6Sperrin avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare, 519c2e6a7d6Sperrin sizeof (rl_t), offsetof(rl_t, r_node)); 52088b7b0f2SMatthew Ahrens list_create(&zv->zv_extents, sizeof (zvol_extent_t), 52188b7b0f2SMatthew Ahrens offsetof(zvol_extent_t, ze_node)); 52267bd71c6Sperrin /* get and cache the blocksize */ 52367bd71c6Sperrin error = dmu_object_info(os, ZVOL_OBJ, &doi); 52467bd71c6Sperrin ASSERT(error == 0); 52567bd71c6Sperrin zv->zv_volblocksize = doi.doi_data_block_size; 52622ac5be4Sperrin 5271209a471SNeil Perrin zil_replay(os, zv, zvol_replay_vector); 528681d9761SEric Taylor dmu_objset_disown(os, zvol_tag); 529681d9761SEric Taylor zv->zv_objset = NULL; 530fa9e4066Sahrens 531fa9e4066Sahrens zvol_minors++; 532fa9e4066Sahrens 533fa9e4066Sahrens mutex_exit(&zvol_state_lock); 534fa9e4066Sahrens 535fa9e4066Sahrens return (0); 536fa9e4066Sahrens } 537fa9e4066Sahrens 538fa9e4066Sahrens /* 539fa9e4066Sahrens * Remove minor node for the specified volume. 540fa9e4066Sahrens */ 541681d9761SEric Taylor static int 542681d9761SEric Taylor zvol_remove_zv(zvol_state_t *zv) 543fa9e4066Sahrens { 544681d9761SEric Taylor char nmbuf[20]; 545fa9e4066Sahrens 546681d9761SEric Taylor ASSERT(MUTEX_HELD(&zvol_state_lock)); 547681d9761SEric Taylor if (zv->zv_total_opens != 0) 548fa9e4066Sahrens return (EBUSY); 549fa9e4066Sahrens 550681d9761SEric Taylor (void) snprintf(nmbuf, sizeof (nmbuf), "%u,raw", zv->zv_minor); 551681d9761SEric Taylor ddi_remove_minor_node(zfs_dip, nmbuf); 552fa9e4066Sahrens 553681d9761SEric Taylor (void) snprintf(nmbuf, sizeof (nmbuf), "%u", zv->zv_minor); 554681d9761SEric Taylor ddi_remove_minor_node(zfs_dip, nmbuf); 555fa9e4066Sahrens 556c2e6a7d6Sperrin avl_destroy(&zv->zv_znode.z_range_avl); 557c2e6a7d6Sperrin mutex_destroy(&zv->zv_znode.z_range_lock); 558fa9e4066Sahrens 559fa9e4066Sahrens ddi_soft_state_free(zvol_state, zv->zv_minor); 560fa9e4066Sahrens 561fa9e4066Sahrens zvol_minors--; 562fa9e4066Sahrens return (0); 563fa9e4066Sahrens } 564fa9e4066Sahrens 565e7cbe64fSgw25295 int 566681d9761SEric Taylor zvol_remove_minor(const char *name) 567681d9761SEric Taylor { 568681d9761SEric Taylor zvol_state_t *zv; 569681d9761SEric Taylor int rc; 570681d9761SEric Taylor 571681d9761SEric Taylor mutex_enter(&zvol_state_lock); 572681d9761SEric Taylor if ((zv = zvol_minor_lookup(name)) == NULL) { 573681d9761SEric Taylor mutex_exit(&zvol_state_lock); 574681d9761SEric Taylor return (ENXIO); 575681d9761SEric Taylor } 576681d9761SEric Taylor rc = zvol_remove_zv(zv); 577681d9761SEric Taylor mutex_exit(&zvol_state_lock); 578681d9761SEric Taylor return (rc); 579681d9761SEric Taylor } 580681d9761SEric Taylor 581681d9761SEric Taylor int 582681d9761SEric Taylor zvol_first_open(zvol_state_t *zv) 583681d9761SEric Taylor { 584681d9761SEric Taylor objset_t *os; 585681d9761SEric Taylor uint64_t volsize; 586681d9761SEric Taylor int error; 587681d9761SEric Taylor uint64_t readonly; 588681d9761SEric Taylor 589681d9761SEric Taylor /* lie and say we're read-only */ 590681d9761SEric Taylor error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, B_TRUE, 591681d9761SEric Taylor zvol_tag, &os); 592681d9761SEric Taylor if (error) 593681d9761SEric Taylor return (error); 594681d9761SEric Taylor 595681d9761SEric Taylor error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize); 596681d9761SEric Taylor if (error) { 597681d9761SEric Taylor ASSERT(error == 0); 598681d9761SEric Taylor dmu_objset_disown(os, zvol_tag); 599681d9761SEric Taylor return (error); 600681d9761SEric Taylor } 601681d9761SEric Taylor zv->zv_objset = os; 602681d9761SEric Taylor zv->zv_volsize = volsize; 603681d9761SEric Taylor zv->zv_zilog = zil_open(os, zvol_get_data); 604681d9761SEric Taylor zvol_size_changed(zv->zv_volsize, ddi_driver_major(zfs_dip), 605681d9761SEric Taylor zv->zv_minor); 606681d9761SEric Taylor 607681d9761SEric Taylor VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &readonly, 608681d9761SEric Taylor NULL) == 0); 609681d9761SEric Taylor if (readonly || dmu_objset_is_snapshot(os)) 610681d9761SEric Taylor zv->zv_flags |= ZVOL_RDONLY; 611681d9761SEric Taylor else 612681d9761SEric Taylor zv->zv_flags &= ~ZVOL_RDONLY; 613681d9761SEric Taylor return (error); 614681d9761SEric Taylor } 615681d9761SEric Taylor 616681d9761SEric Taylor void 617681d9761SEric Taylor zvol_last_close(zvol_state_t *zv) 618681d9761SEric Taylor { 619681d9761SEric Taylor zil_close(zv->zv_zilog); 620681d9761SEric Taylor zv->zv_zilog = NULL; 621681d9761SEric Taylor dmu_objset_disown(zv->zv_objset, zvol_tag); 622681d9761SEric Taylor zv->zv_objset = NULL; 623681d9761SEric Taylor } 624681d9761SEric Taylor 625681d9761SEric Taylor int 626e7cbe64fSgw25295 zvol_prealloc(zvol_state_t *zv) 627e7cbe64fSgw25295 { 628e7cbe64fSgw25295 objset_t *os = zv->zv_objset; 629e7cbe64fSgw25295 dmu_tx_t *tx; 630e7cbe64fSgw25295 uint64_t refd, avail, usedobjs, availobjs; 631e7cbe64fSgw25295 uint64_t resid = zv->zv_volsize; 632e7cbe64fSgw25295 uint64_t off = 0; 633e7cbe64fSgw25295 634e7cbe64fSgw25295 /* Check the space usage before attempting to allocate the space */ 635e7cbe64fSgw25295 dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs); 636e7cbe64fSgw25295 if (avail < zv->zv_volsize) 637e7cbe64fSgw25295 return (ENOSPC); 638e7cbe64fSgw25295 639e7cbe64fSgw25295 /* Free old extents if they exist */ 640e7cbe64fSgw25295 zvol_free_extents(zv); 641e7cbe64fSgw25295 642e7cbe64fSgw25295 while (resid != 0) { 643e7cbe64fSgw25295 int error; 644e7cbe64fSgw25295 uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE); 645e7cbe64fSgw25295 646e7cbe64fSgw25295 tx = dmu_tx_create(os); 647e7cbe64fSgw25295 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 648e7cbe64fSgw25295 error = dmu_tx_assign(tx, TXG_WAIT); 649e7cbe64fSgw25295 if (error) { 650e7cbe64fSgw25295 dmu_tx_abort(tx); 651cdb0ab79Smaybee (void) dmu_free_long_range(os, ZVOL_OBJ, 0, off); 652e7cbe64fSgw25295 return (error); 653e7cbe64fSgw25295 } 65482c9918fSTim Haley dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx); 655e7cbe64fSgw25295 dmu_tx_commit(tx); 656e7cbe64fSgw25295 off += bytes; 657e7cbe64fSgw25295 resid -= bytes; 658e7cbe64fSgw25295 } 659e7cbe64fSgw25295 txg_wait_synced(dmu_objset_pool(os), 0); 660e7cbe64fSgw25295 661e7cbe64fSgw25295 return (0); 662e7cbe64fSgw25295 } 663e7cbe64fSgw25295 664e7cbe64fSgw25295 int 665681d9761SEric Taylor zvol_update_volsize(objset_t *os, uint64_t volsize) 666e7cbe64fSgw25295 { 667e7cbe64fSgw25295 dmu_tx_t *tx; 668e7cbe64fSgw25295 int error; 669e7cbe64fSgw25295 670e7cbe64fSgw25295 ASSERT(MUTEX_HELD(&zvol_state_lock)); 671e7cbe64fSgw25295 672681d9761SEric Taylor tx = dmu_tx_create(os); 673e7cbe64fSgw25295 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 674e7cbe64fSgw25295 error = dmu_tx_assign(tx, TXG_WAIT); 675e7cbe64fSgw25295 if (error) { 676e7cbe64fSgw25295 dmu_tx_abort(tx); 677e7cbe64fSgw25295 return (error); 678e7cbe64fSgw25295 } 679e7cbe64fSgw25295 680681d9761SEric Taylor error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, 681e7cbe64fSgw25295 &volsize, tx); 682e7cbe64fSgw25295 dmu_tx_commit(tx); 683e7cbe64fSgw25295 684e7cbe64fSgw25295 if (error == 0) 685681d9761SEric Taylor error = dmu_free_long_range(os, 686cdb0ab79Smaybee ZVOL_OBJ, volsize, DMU_OBJECT_END); 687e7cbe64fSgw25295 return (error); 688e7cbe64fSgw25295 } 689e7cbe64fSgw25295 690681d9761SEric Taylor void 691681d9761SEric Taylor zvol_remove_minors(const char *name) 692681d9761SEric Taylor { 693681d9761SEric Taylor zvol_state_t *zv; 694681d9761SEric Taylor char *namebuf; 695681d9761SEric Taylor minor_t minor; 696681d9761SEric Taylor 697681d9761SEric Taylor namebuf = kmem_zalloc(strlen(name) + 2, KM_SLEEP); 698681d9761SEric Taylor (void) strncpy(namebuf, name, strlen(name)); 699681d9761SEric Taylor (void) strcat(namebuf, "/"); 700681d9761SEric Taylor mutex_enter(&zvol_state_lock); 701681d9761SEric Taylor for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) { 702681d9761SEric Taylor 703681d9761SEric Taylor zv = ddi_get_soft_state(zvol_state, minor); 704681d9761SEric Taylor if (zv == NULL) 705681d9761SEric Taylor continue; 706681d9761SEric Taylor if (strncmp(namebuf, zv->zv_name, strlen(namebuf)) == 0) 707681d9761SEric Taylor (void) zvol_remove_zv(zv); 708681d9761SEric Taylor } 709681d9761SEric Taylor kmem_free(namebuf, strlen(name) + 2); 710681d9761SEric Taylor 711681d9761SEric Taylor mutex_exit(&zvol_state_lock); 712681d9761SEric Taylor } 713681d9761SEric Taylor 714fa9e4066Sahrens int 71591ebeef5Sahrens zvol_set_volsize(const char *name, major_t maj, uint64_t volsize) 716fa9e4066Sahrens { 717681d9761SEric Taylor zvol_state_t *zv = NULL; 718681d9761SEric Taylor objset_t *os; 719fa9e4066Sahrens int error; 7205c5460e9Seschrock dmu_object_info_t doi; 721e7cbe64fSgw25295 uint64_t old_volsize = 0ULL; 722681d9761SEric Taylor uint64_t readonly; 723fa9e4066Sahrens 724fa9e4066Sahrens mutex_enter(&zvol_state_lock); 725681d9761SEric Taylor zv = zvol_minor_lookup(name); 726681d9761SEric Taylor if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) { 727681d9761SEric Taylor mutex_exit(&zvol_state_lock); 728681d9761SEric Taylor return (error); 729fa9e4066Sahrens } 730fa9e4066Sahrens 731681d9761SEric Taylor if ((error = dmu_object_info(os, ZVOL_OBJ, &doi)) != 0 || 732e9dbad6fSeschrock (error = zvol_check_volsize(volsize, 733bb0ade09Sahrens doi.doi_data_block_size)) != 0) 734bb0ade09Sahrens goto out; 7355c5460e9Seschrock 736681d9761SEric Taylor VERIFY(dsl_prop_get_integer(name, "readonly", &readonly, 737681d9761SEric Taylor NULL) == 0); 738681d9761SEric Taylor if (readonly) { 739bb0ade09Sahrens error = EROFS; 740bb0ade09Sahrens goto out; 741fa9e4066Sahrens } 742fa9e4066Sahrens 743681d9761SEric Taylor error = zvol_update_volsize(os, volsize); 744e7cbe64fSgw25295 /* 745e7cbe64fSgw25295 * Reinitialize the dump area to the new size. If we 746681d9761SEric Taylor * failed to resize the dump area then restore it back to 747681d9761SEric Taylor * its original size. 748e7cbe64fSgw25295 */ 749681d9761SEric Taylor if (zv && error == 0) { 750681d9761SEric Taylor if (zv->zv_flags & ZVOL_DUMPIFIED) { 751681d9761SEric Taylor old_volsize = zv->zv_volsize; 752681d9761SEric Taylor zv->zv_volsize = volsize; 753e7cbe64fSgw25295 if ((error = zvol_dumpify(zv)) != 0 || 754e7cbe64fSgw25295 (error = dumpvp_resize()) != 0) { 755681d9761SEric Taylor (void) zvol_update_volsize(os, old_volsize); 756681d9761SEric Taylor zv->zv_volsize = old_volsize; 757e7cbe64fSgw25295 error = zvol_dumpify(zv); 758fa9e4066Sahrens } 759fa9e4066Sahrens } 760681d9761SEric Taylor if (error == 0) { 761681d9761SEric Taylor zv->zv_volsize = volsize; 762681d9761SEric Taylor zvol_size_changed(volsize, maj, zv->zv_minor); 763681d9761SEric Taylor } 764681d9761SEric Taylor } 765fa9e4066Sahrens 766573ca77eSGeorge Wilson /* 767573ca77eSGeorge Wilson * Generate a LUN expansion event. 768573ca77eSGeorge Wilson */ 769681d9761SEric Taylor if (zv && error == 0) { 770573ca77eSGeorge Wilson sysevent_id_t eid; 771573ca77eSGeorge Wilson nvlist_t *attr; 772573ca77eSGeorge Wilson char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 773573ca77eSGeorge Wilson 774681d9761SEric Taylor (void) snprintf(physpath, MAXPATHLEN, "%s%u", ZVOL_PSEUDO_DEV, 775573ca77eSGeorge Wilson zv->zv_minor); 776573ca77eSGeorge Wilson 777573ca77eSGeorge Wilson VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 778573ca77eSGeorge Wilson VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 779573ca77eSGeorge Wilson 780573ca77eSGeorge Wilson (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 781573ca77eSGeorge Wilson ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 782573ca77eSGeorge Wilson 783573ca77eSGeorge Wilson nvlist_free(attr); 784573ca77eSGeorge Wilson kmem_free(physpath, MAXPATHLEN); 785573ca77eSGeorge Wilson } 786573ca77eSGeorge Wilson 787bb0ade09Sahrens out: 788681d9761SEric Taylor dmu_objset_rele(os, FTAG); 789bb0ade09Sahrens 790fa9e4066Sahrens mutex_exit(&zvol_state_lock); 791fa9e4066Sahrens 792fa9e4066Sahrens return (error); 793fa9e4066Sahrens } 794fa9e4066Sahrens 795fa9e4066Sahrens /*ARGSUSED*/ 796fa9e4066Sahrens int 797fa9e4066Sahrens zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr) 798fa9e4066Sahrens { 799fa9e4066Sahrens minor_t minor = getminor(*devp); 800fa9e4066Sahrens zvol_state_t *zv; 801681d9761SEric Taylor int err = 0; 802fa9e4066Sahrens 803fa9e4066Sahrens if (minor == 0) /* This is the control device */ 804fa9e4066Sahrens return (0); 805fa9e4066Sahrens 806fa9e4066Sahrens mutex_enter(&zvol_state_lock); 807fa9e4066Sahrens 808fa9e4066Sahrens zv = ddi_get_soft_state(zvol_state, minor); 809fa9e4066Sahrens if (zv == NULL) { 810fa9e4066Sahrens mutex_exit(&zvol_state_lock); 811fa9e4066Sahrens return (ENXIO); 812fa9e4066Sahrens } 813fa9e4066Sahrens 814681d9761SEric Taylor if (zv->zv_total_opens == 0) 815681d9761SEric Taylor err = zvol_first_open(zv); 816681d9761SEric Taylor if (err) { 817fa9e4066Sahrens mutex_exit(&zvol_state_lock); 818681d9761SEric Taylor return (err); 819681d9761SEric Taylor } 820681d9761SEric Taylor if ((flag & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) { 821681d9761SEric Taylor err = EROFS; 822681d9761SEric Taylor goto out; 823fa9e4066Sahrens } 824c7f714e2SEric Taylor if (zv->zv_flags & ZVOL_EXCL) { 825681d9761SEric Taylor err = EBUSY; 826681d9761SEric Taylor goto out; 827c7f714e2SEric Taylor } 828c7f714e2SEric Taylor if (flag & FEXCL) { 829c7f714e2SEric Taylor if (zv->zv_total_opens != 0) { 830681d9761SEric Taylor err = EBUSY; 831681d9761SEric Taylor goto out; 832c7f714e2SEric Taylor } 833c7f714e2SEric Taylor zv->zv_flags |= ZVOL_EXCL; 834c7f714e2SEric Taylor } 835fa9e4066Sahrens 836fa9e4066Sahrens if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) { 837fa9e4066Sahrens zv->zv_open_count[otyp]++; 838fa9e4066Sahrens zv->zv_total_opens++; 839fa9e4066Sahrens } 840fa9e4066Sahrens mutex_exit(&zvol_state_lock); 841fa9e4066Sahrens 842681d9761SEric Taylor return (err); 843681d9761SEric Taylor out: 844681d9761SEric Taylor if (zv->zv_total_opens == 0) 845681d9761SEric Taylor zvol_last_close(zv); 846681d9761SEric Taylor mutex_exit(&zvol_state_lock); 847681d9761SEric Taylor return (err); 848fa9e4066Sahrens } 849fa9e4066Sahrens 850fa9e4066Sahrens /*ARGSUSED*/ 851fa9e4066Sahrens int 852fa9e4066Sahrens zvol_close(dev_t dev, int flag, int otyp, cred_t *cr) 853fa9e4066Sahrens { 854fa9e4066Sahrens minor_t minor = getminor(dev); 855fa9e4066Sahrens zvol_state_t *zv; 856681d9761SEric Taylor int error = 0; 857fa9e4066Sahrens 858fa9e4066Sahrens if (minor == 0) /* This is the control device */ 859fa9e4066Sahrens return (0); 860fa9e4066Sahrens 861fa9e4066Sahrens mutex_enter(&zvol_state_lock); 862fa9e4066Sahrens 863fa9e4066Sahrens zv = ddi_get_soft_state(zvol_state, minor); 864fa9e4066Sahrens if (zv == NULL) { 865fa9e4066Sahrens mutex_exit(&zvol_state_lock); 866fa9e4066Sahrens return (ENXIO); 867fa9e4066Sahrens } 868fa9e4066Sahrens 869c7f714e2SEric Taylor if (zv->zv_flags & ZVOL_EXCL) { 870c7f714e2SEric Taylor ASSERT(zv->zv_total_opens == 1); 871c7f714e2SEric Taylor zv->zv_flags &= ~ZVOL_EXCL; 872fa9e4066Sahrens } 873fa9e4066Sahrens 874fa9e4066Sahrens /* 875fa9e4066Sahrens * If the open count is zero, this is a spurious close. 876fa9e4066Sahrens * That indicates a bug in the kernel / DDI framework. 877fa9e4066Sahrens */ 878fa9e4066Sahrens ASSERT(zv->zv_open_count[otyp] != 0); 879fa9e4066Sahrens ASSERT(zv->zv_total_opens != 0); 880fa9e4066Sahrens 881fa9e4066Sahrens /* 882fa9e4066Sahrens * You may get multiple opens, but only one close. 883fa9e4066Sahrens */ 884fa9e4066Sahrens zv->zv_open_count[otyp]--; 885fa9e4066Sahrens zv->zv_total_opens--; 886fa9e4066Sahrens 887681d9761SEric Taylor if (zv->zv_total_opens == 0) 888681d9761SEric Taylor zvol_last_close(zv); 889fa9e4066Sahrens 890681d9761SEric Taylor mutex_exit(&zvol_state_lock); 891681d9761SEric Taylor return (error); 892fa9e4066Sahrens } 893fa9e4066Sahrens 894feb08c6bSbillm static void 895b24ab676SJeff Bonwick zvol_get_done(zgd_t *zgd, int error) 89667bd71c6Sperrin { 897b24ab676SJeff Bonwick if (zgd->zgd_db) 898b24ab676SJeff Bonwick dmu_buf_rele(zgd->zgd_db, zgd); 89967bd71c6Sperrin 900b24ab676SJeff Bonwick zfs_range_unlock(zgd->zgd_rl); 901b24ab676SJeff Bonwick 902b24ab676SJeff Bonwick if (error == 0 && zgd->zgd_bp) 90317f17c2dSbonwick zil_add_block(zgd->zgd_zilog, zgd->zgd_bp); 904b24ab676SJeff Bonwick 90567bd71c6Sperrin kmem_free(zgd, sizeof (zgd_t)); 90667bd71c6Sperrin } 90767bd71c6Sperrin 90867bd71c6Sperrin /* 90967bd71c6Sperrin * Get data to generate a TX_WRITE intent log record. 91067bd71c6Sperrin */ 911feb08c6bSbillm static int 91267bd71c6Sperrin zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 91367bd71c6Sperrin { 91467bd71c6Sperrin zvol_state_t *zv = arg; 91567bd71c6Sperrin objset_t *os = zv->zv_objset; 916b24ab676SJeff Bonwick uint64_t object = ZVOL_OBJ; 917b24ab676SJeff Bonwick uint64_t offset = lr->lr_offset; 918b24ab676SJeff Bonwick uint64_t size = lr->lr_length; /* length of user data */ 919b24ab676SJeff Bonwick blkptr_t *bp = &lr->lr_blkptr; 92067bd71c6Sperrin dmu_buf_t *db; 92167bd71c6Sperrin zgd_t *zgd; 92267bd71c6Sperrin int error; 92367bd71c6Sperrin 924b24ab676SJeff Bonwick ASSERT(zio != NULL); 925b24ab676SJeff Bonwick ASSERT(size != 0); 926b24ab676SJeff Bonwick 927b24ab676SJeff Bonwick zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP); 928b24ab676SJeff Bonwick zgd->zgd_zilog = zv->zv_zilog; 929b24ab676SJeff Bonwick zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER); 930feb08c6bSbillm 931c2e6a7d6Sperrin /* 932c2e6a7d6Sperrin * Write records come in two flavors: immediate and indirect. 933c2e6a7d6Sperrin * For small writes it's cheaper to store the data with the 934c2e6a7d6Sperrin * log record (immediate); for large writes it's cheaper to 935c2e6a7d6Sperrin * sync the data and get a pointer to it (indirect) so that 936c2e6a7d6Sperrin * we don't have to write the data twice. 937c2e6a7d6Sperrin */ 938b24ab676SJeff Bonwick if (buf != NULL) { /* immediate write */ 939b24ab676SJeff Bonwick error = dmu_read(os, object, offset, size, buf, 940b24ab676SJeff Bonwick DMU_READ_NO_PREFETCH); 941b24ab676SJeff Bonwick } else { 942b24ab676SJeff Bonwick size = zv->zv_volblocksize; 943b24ab676SJeff Bonwick offset = P2ALIGN(offset, size); 944b24ab676SJeff Bonwick error = dmu_buf_hold(os, object, offset, zgd, &db); 945975c32a0SNeil Perrin if (error == 0) { 946b24ab676SJeff Bonwick zgd->zgd_db = db; 947b24ab676SJeff Bonwick zgd->zgd_bp = bp; 948b24ab676SJeff Bonwick 949b24ab676SJeff Bonwick ASSERT(db->db_offset == offset); 950b24ab676SJeff Bonwick ASSERT(db->db_size == size); 951b24ab676SJeff Bonwick 952b24ab676SJeff Bonwick error = dmu_sync(zio, lr->lr_common.lrc_txg, 953b24ab676SJeff Bonwick zvol_get_done, zgd); 954b24ab676SJeff Bonwick 955b24ab676SJeff Bonwick if (error == 0) 956b24ab676SJeff Bonwick return (0); 957b24ab676SJeff Bonwick } 958975c32a0SNeil Perrin } 959975c32a0SNeil Perrin 960b24ab676SJeff Bonwick zvol_get_done(zgd, error); 961b24ab676SJeff Bonwick 96267bd71c6Sperrin return (error); 96367bd71c6Sperrin } 96467bd71c6Sperrin 965a24e15ceSperrin /* 966a24e15ceSperrin * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions. 96722ac5be4Sperrin * 96822ac5be4Sperrin * We store data in the log buffers if it's small enough. 96967bd71c6Sperrin * Otherwise we will later flush the data out via dmu_sync(). 97022ac5be4Sperrin */ 97167bd71c6Sperrin ssize_t zvol_immediate_write_sz = 32768; 97222ac5be4Sperrin 973feb08c6bSbillm static void 974510b6c0eSNeil Perrin zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid, 975510b6c0eSNeil Perrin boolean_t sync) 97622ac5be4Sperrin { 977feb08c6bSbillm uint32_t blocksize = zv->zv_volblocksize; 9781209a471SNeil Perrin zilog_t *zilog = zv->zv_zilog; 979510b6c0eSNeil Perrin boolean_t slogging; 980e09fa4daSNeil Perrin ssize_t immediate_write_sz; 981510b6c0eSNeil Perrin 982510b6c0eSNeil Perrin if (zil_disable) 983510b6c0eSNeil Perrin return; 984a24e15ceSperrin 985b24ab676SJeff Bonwick if (zil_replaying(zilog, tx)) 9861209a471SNeil Perrin return; 9871209a471SNeil Perrin 988e09fa4daSNeil Perrin immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT) 989e09fa4daSNeil Perrin ? 0 : zvol_immediate_write_sz; 990e09fa4daSNeil Perrin 991e09fa4daSNeil Perrin slogging = spa_has_slogs(zilog->zl_spa) && 992e09fa4daSNeil Perrin (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY); 993feb08c6bSbillm 994510b6c0eSNeil Perrin while (resid) { 995510b6c0eSNeil Perrin itx_t *itx; 996510b6c0eSNeil Perrin lr_write_t *lr; 997510b6c0eSNeil Perrin ssize_t len; 998510b6c0eSNeil Perrin itx_wr_state_t write_state; 999510b6c0eSNeil Perrin 1000510b6c0eSNeil Perrin /* 1001510b6c0eSNeil Perrin * Unlike zfs_log_write() we can be called with 1002510b6c0eSNeil Perrin * upto DMU_MAX_ACCESS/2 (5MB) writes. 1003510b6c0eSNeil Perrin */ 1004e09fa4daSNeil Perrin if (blocksize > immediate_write_sz && !slogging && 1005510b6c0eSNeil Perrin resid >= blocksize && off % blocksize == 0) { 1006510b6c0eSNeil Perrin write_state = WR_INDIRECT; /* uses dmu_sync */ 1007510b6c0eSNeil Perrin len = blocksize; 1008510b6c0eSNeil Perrin } else if (sync) { 1009510b6c0eSNeil Perrin write_state = WR_COPIED; 1010510b6c0eSNeil Perrin len = MIN(ZIL_MAX_LOG_DATA, resid); 1011510b6c0eSNeil Perrin } else { 1012510b6c0eSNeil Perrin write_state = WR_NEED_COPY; 1013510b6c0eSNeil Perrin len = MIN(ZIL_MAX_LOG_DATA, resid); 1014510b6c0eSNeil Perrin } 1015510b6c0eSNeil Perrin 1016510b6c0eSNeil Perrin itx = zil_itx_create(TX_WRITE, sizeof (*lr) + 1017510b6c0eSNeil Perrin (write_state == WR_COPIED ? len : 0)); 101822ac5be4Sperrin lr = (lr_write_t *)&itx->itx_lr; 1019510b6c0eSNeil Perrin if (write_state == WR_COPIED && dmu_read(zv->zv_objset, 10207bfdf011SNeil Perrin ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) { 1021b24ab676SJeff Bonwick zil_itx_destroy(itx); 1022510b6c0eSNeil Perrin itx = zil_itx_create(TX_WRITE, sizeof (*lr)); 1023510b6c0eSNeil Perrin lr = (lr_write_t *)&itx->itx_lr; 1024510b6c0eSNeil Perrin write_state = WR_NEED_COPY; 1025510b6c0eSNeil Perrin } 1026510b6c0eSNeil Perrin 1027510b6c0eSNeil Perrin itx->itx_wr_state = write_state; 1028510b6c0eSNeil Perrin if (write_state == WR_NEED_COPY) 1029510b6c0eSNeil Perrin itx->itx_sod += len; 103022ac5be4Sperrin lr->lr_foid = ZVOL_OBJ; 103122ac5be4Sperrin lr->lr_offset = off; 1032510b6c0eSNeil Perrin lr->lr_length = len; 1033b24ab676SJeff Bonwick lr->lr_blkoff = 0; 103422ac5be4Sperrin BP_ZERO(&lr->lr_blkptr); 1035feb08c6bSbillm 1036510b6c0eSNeil Perrin itx->itx_private = zv; 1037510b6c0eSNeil Perrin itx->itx_sync = sync; 1038510b6c0eSNeil Perrin 10391209a471SNeil Perrin (void) zil_itx_assign(zilog, itx, tx); 1040510b6c0eSNeil Perrin 1041510b6c0eSNeil Perrin off += len; 1042510b6c0eSNeil Perrin resid -= len; 1043a24e15ceSperrin } 104422ac5be4Sperrin } 104522ac5be4Sperrin 104688b7b0f2SMatthew Ahrens static int 104788b7b0f2SMatthew Ahrens zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t size, 104888b7b0f2SMatthew Ahrens boolean_t doread, boolean_t isdump) 1049e7cbe64fSgw25295 { 1050e7cbe64fSgw25295 vdev_disk_t *dvd; 1051e7cbe64fSgw25295 int c; 1052e7cbe64fSgw25295 int numerrors = 0; 1053e7cbe64fSgw25295 1054e7cbe64fSgw25295 for (c = 0; c < vd->vdev_children; c++) { 105521ecdf64SLin Ling ASSERT(vd->vdev_ops == &vdev_mirror_ops || 105621ecdf64SLin Ling vd->vdev_ops == &vdev_replacing_ops || 105721ecdf64SLin Ling vd->vdev_ops == &vdev_spare_ops); 105888b7b0f2SMatthew Ahrens int err = zvol_dumpio_vdev(vd->vdev_child[c], 105988b7b0f2SMatthew Ahrens addr, offset, size, doread, isdump); 106088b7b0f2SMatthew Ahrens if (err != 0) { 1061e7cbe64fSgw25295 numerrors++; 106288b7b0f2SMatthew Ahrens } else if (doread) { 1063e7cbe64fSgw25295 break; 1064e7cbe64fSgw25295 } 1065e7cbe64fSgw25295 } 1066e7cbe64fSgw25295 1067e7cbe64fSgw25295 if (!vd->vdev_ops->vdev_op_leaf) 1068e7cbe64fSgw25295 return (numerrors < vd->vdev_children ? 0 : EIO); 1069e7cbe64fSgw25295 1070dc0bb255SEric Taylor if (doread && !vdev_readable(vd)) 1071dc0bb255SEric Taylor return (EIO); 1072dc0bb255SEric Taylor else if (!doread && !vdev_writeable(vd)) 1073e7cbe64fSgw25295 return (EIO); 1074e7cbe64fSgw25295 1075e7cbe64fSgw25295 dvd = vd->vdev_tsd; 1076e7cbe64fSgw25295 ASSERT3P(dvd, !=, NULL); 1077e7cbe64fSgw25295 offset += VDEV_LABEL_START_SIZE; 1078e7cbe64fSgw25295 1079e7cbe64fSgw25295 if (ddi_in_panic() || isdump) { 108088b7b0f2SMatthew Ahrens ASSERT(!doread); 108188b7b0f2SMatthew Ahrens if (doread) 1082e7cbe64fSgw25295 return (EIO); 1083e7cbe64fSgw25295 return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset), 1084e7cbe64fSgw25295 lbtodb(size))); 1085e7cbe64fSgw25295 } else { 1086e7cbe64fSgw25295 return (vdev_disk_physio(dvd->vd_lh, addr, size, offset, 108788b7b0f2SMatthew Ahrens doread ? B_READ : B_WRITE)); 1088e7cbe64fSgw25295 } 1089e7cbe64fSgw25295 } 1090e7cbe64fSgw25295 109188b7b0f2SMatthew Ahrens static int 109288b7b0f2SMatthew Ahrens zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size, 109388b7b0f2SMatthew Ahrens boolean_t doread, boolean_t isdump) 1094e7cbe64fSgw25295 { 1095e7cbe64fSgw25295 vdev_t *vd; 1096e7cbe64fSgw25295 int error; 109788b7b0f2SMatthew Ahrens zvol_extent_t *ze; 1098e7cbe64fSgw25295 spa_t *spa = dmu_objset_spa(zv->zv_objset); 1099e7cbe64fSgw25295 110088b7b0f2SMatthew Ahrens /* Must be sector aligned, and not stradle a block boundary. */ 110188b7b0f2SMatthew Ahrens if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) || 110288b7b0f2SMatthew Ahrens P2BOUNDARY(offset, size, zv->zv_volblocksize)) { 110388b7b0f2SMatthew Ahrens return (EINVAL); 110488b7b0f2SMatthew Ahrens } 1105e7cbe64fSgw25295 ASSERT(size <= zv->zv_volblocksize); 1106e7cbe64fSgw25295 110788b7b0f2SMatthew Ahrens /* Locate the extent this belongs to */ 110888b7b0f2SMatthew Ahrens ze = list_head(&zv->zv_extents); 110988b7b0f2SMatthew Ahrens while (offset >= ze->ze_nblks * zv->zv_volblocksize) { 111088b7b0f2SMatthew Ahrens offset -= ze->ze_nblks * zv->zv_volblocksize; 111188b7b0f2SMatthew Ahrens ze = list_next(&zv->zv_extents, ze); 111288b7b0f2SMatthew Ahrens } 111324cc0e1cSGeorge Wilson 111424cc0e1cSGeorge Wilson if (!ddi_in_panic()) 1115e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 111624cc0e1cSGeorge Wilson 111788b7b0f2SMatthew Ahrens vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva)); 111888b7b0f2SMatthew Ahrens offset += DVA_GET_OFFSET(&ze->ze_dva); 111988b7b0f2SMatthew Ahrens error = zvol_dumpio_vdev(vd, addr, offset, size, doread, isdump); 112024cc0e1cSGeorge Wilson 112124cc0e1cSGeorge Wilson if (!ddi_in_panic()) 1122e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 112324cc0e1cSGeorge Wilson 1124e7cbe64fSgw25295 return (error); 1125e7cbe64fSgw25295 } 1126e7cbe64fSgw25295 1127e7cbe64fSgw25295 int 1128fa9e4066Sahrens zvol_strategy(buf_t *bp) 1129fa9e4066Sahrens { 1130fa9e4066Sahrens zvol_state_t *zv = ddi_get_soft_state(zvol_state, getminor(bp->b_edev)); 1131fa9e4066Sahrens uint64_t off, volsize; 113288b7b0f2SMatthew Ahrens size_t resid; 1133fa9e4066Sahrens char *addr; 113422ac5be4Sperrin objset_t *os; 1135c2e6a7d6Sperrin rl_t *rl; 1136fa9e4066Sahrens int error = 0; 113788b7b0f2SMatthew Ahrens boolean_t doread = bp->b_flags & B_READ; 1138*f80ce222SChris Kirby boolean_t is_dump; 1139510b6c0eSNeil Perrin boolean_t sync; 1140fa9e4066Sahrens 1141fa9e4066Sahrens if (zv == NULL) { 1142fa9e4066Sahrens bioerror(bp, ENXIO); 1143fa9e4066Sahrens biodone(bp); 1144fa9e4066Sahrens return (0); 1145fa9e4066Sahrens } 1146fa9e4066Sahrens 1147fa9e4066Sahrens if (getminor(bp->b_edev) == 0) { 1148fa9e4066Sahrens bioerror(bp, EINVAL); 1149fa9e4066Sahrens biodone(bp); 1150fa9e4066Sahrens return (0); 1151fa9e4066Sahrens } 1152fa9e4066Sahrens 1153681d9761SEric Taylor if (!(bp->b_flags & B_READ) && (zv->zv_flags & ZVOL_RDONLY)) { 1154fa9e4066Sahrens bioerror(bp, EROFS); 1155fa9e4066Sahrens biodone(bp); 1156fa9e4066Sahrens return (0); 1157fa9e4066Sahrens } 1158fa9e4066Sahrens 1159fa9e4066Sahrens off = ldbtob(bp->b_blkno); 1160fa9e4066Sahrens volsize = zv->zv_volsize; 1161fa9e4066Sahrens 116222ac5be4Sperrin os = zv->zv_objset; 116322ac5be4Sperrin ASSERT(os != NULL); 1164fa9e4066Sahrens 1165fa9e4066Sahrens bp_mapin(bp); 1166fa9e4066Sahrens addr = bp->b_un.b_addr; 1167fa9e4066Sahrens resid = bp->b_bcount; 1168fa9e4066Sahrens 116988b7b0f2SMatthew Ahrens if (resid > 0 && (off < 0 || off >= volsize)) { 117088b7b0f2SMatthew Ahrens bioerror(bp, EIO); 117188b7b0f2SMatthew Ahrens biodone(bp); 117288b7b0f2SMatthew Ahrens return (0); 117388b7b0f2SMatthew Ahrens } 117473ec3d9cSgw25295 1175*f80ce222SChris Kirby is_dump = zv->zv_flags & ZVOL_DUMPIFIED; 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); 1185e7cbe64fSgw25295 1186fa9e4066Sahrens while (resid != 0 && off < volsize) { 118788b7b0f2SMatthew Ahrens size_t size = MIN(resid, zvol_maxphys); 1188e7cbe64fSgw25295 if (is_dump) { 1189e7cbe64fSgw25295 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 1244e7cbe64fSgw25295 int 1245e7cbe64fSgw25295 zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks) 1246e7cbe64fSgw25295 { 1247e7cbe64fSgw25295 minor_t minor = getminor(dev); 1248e7cbe64fSgw25295 zvol_state_t *zv; 1249e7cbe64fSgw25295 int error = 0; 1250e7cbe64fSgw25295 uint64_t size; 1251e7cbe64fSgw25295 uint64_t boff; 1252e7cbe64fSgw25295 uint64_t resid; 1253e7cbe64fSgw25295 1254e7cbe64fSgw25295 if (minor == 0) /* This is the control device */ 1255e7cbe64fSgw25295 return (ENXIO); 1256e7cbe64fSgw25295 1257e7cbe64fSgw25295 zv = ddi_get_soft_state(zvol_state, minor); 1258e7cbe64fSgw25295 if (zv == NULL) 1259e7cbe64fSgw25295 return (ENXIO); 1260e7cbe64fSgw25295 1261e7cbe64fSgw25295 boff = ldbtob(blkno); 1262e7cbe64fSgw25295 resid = ldbtob(nblocks); 1263e7cbe64fSgw25295 126488b7b0f2SMatthew Ahrens VERIFY3U(boff + resid, <=, zv->zv_volsize); 126588b7b0f2SMatthew Ahrens 126688b7b0f2SMatthew Ahrens while (resid) { 126788b7b0f2SMatthew Ahrens size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff); 126888b7b0f2SMatthew Ahrens error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE); 1269e7cbe64fSgw25295 if (error) 1270e7cbe64fSgw25295 break; 1271e7cbe64fSgw25295 boff += size; 1272e7cbe64fSgw25295 addr += size; 1273e7cbe64fSgw25295 resid -= size; 1274e7cbe64fSgw25295 } 1275e7cbe64fSgw25295 1276e7cbe64fSgw25295 return (error); 1277e7cbe64fSgw25295 } 1278e7cbe64fSgw25295 1279fa9e4066Sahrens /*ARGSUSED*/ 1280fa9e4066Sahrens int 1281feb08c6bSbillm zvol_read(dev_t dev, uio_t *uio, cred_t *cr) 1282fa9e4066Sahrens { 1283c7ca1008Sgw25295 minor_t minor = getminor(dev); 1284c7ca1008Sgw25295 zvol_state_t *zv; 128573ec3d9cSgw25295 uint64_t volsize; 1286c2e6a7d6Sperrin rl_t *rl; 1287feb08c6bSbillm int error = 0; 1288feb08c6bSbillm 1289c7ca1008Sgw25295 if (minor == 0) /* This is the control device */ 1290c7ca1008Sgw25295 return (ENXIO); 1291c7ca1008Sgw25295 1292c7ca1008Sgw25295 zv = ddi_get_soft_state(zvol_state, minor); 1293c7ca1008Sgw25295 if (zv == NULL) 1294c7ca1008Sgw25295 return (ENXIO); 1295c7ca1008Sgw25295 129673ec3d9cSgw25295 volsize = zv->zv_volsize; 129773ec3d9cSgw25295 if (uio->uio_resid > 0 && 129873ec3d9cSgw25295 (uio->uio_loffset < 0 || uio->uio_loffset >= volsize)) 129973ec3d9cSgw25295 return (EIO); 130073ec3d9cSgw25295 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); 130973ec3d9cSgw25295 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 1310feb08c6bSbillm uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 1311feb08c6bSbillm 131273ec3d9cSgw25295 /* don't read past the end */ 131373ec3d9cSgw25295 if (bytes > volsize - uio->uio_loffset) 131473ec3d9cSgw25295 bytes = volsize - uio->uio_loffset; 131573ec3d9cSgw25295 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; 1322feb08c6bSbillm } 1323b87f3af3Sperrin } 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 { 1332c7ca1008Sgw25295 minor_t minor = getminor(dev); 1333c7ca1008Sgw25295 zvol_state_t *zv; 133473ec3d9cSgw25295 uint64_t volsize; 1335c2e6a7d6Sperrin rl_t *rl; 1336feb08c6bSbillm int error = 0; 1337510b6c0eSNeil Perrin boolean_t sync; 1338fa9e4066Sahrens 1339c7ca1008Sgw25295 if (minor == 0) /* This is the control device */ 1340c7ca1008Sgw25295 return (ENXIO); 1341c7ca1008Sgw25295 1342c7ca1008Sgw25295 zv = ddi_get_soft_state(zvol_state, minor); 1343c7ca1008Sgw25295 if (zv == NULL) 1344c7ca1008Sgw25295 return (ENXIO); 1345c7ca1008Sgw25295 134673ec3d9cSgw25295 volsize = zv->zv_volsize; 134773ec3d9cSgw25295 if (uio->uio_resid > 0 && 134873ec3d9cSgw25295 (uio->uio_loffset < 0 || uio->uio_loffset >= volsize)) 134973ec3d9cSgw25295 return (EIO); 135073ec3d9cSgw25295 1351e7cbe64fSgw25295 if (zv->zv_flags & ZVOL_DUMPIFIED) { 1352e7cbe64fSgw25295 error = physio(zvol_strategy, NULL, dev, B_WRITE, 1353e7cbe64fSgw25295 zvol_minphys, uio); 1354e7cbe64fSgw25295 return (error); 1355e7cbe64fSgw25295 } 1356e7cbe64fSgw25295 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); 136173ec3d9cSgw25295 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); 136573ec3d9cSgw25295 136673ec3d9cSgw25295 if (bytes > volsize - off) /* don't write past the end */ 136773ec3d9cSgw25295 bytes = volsize - off; 136873ec3d9cSgw25295 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; 1458e7cbe64fSgw25295 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; 14773adc9019SEric Taylor dki.dki_unit = getminor(dev); 1478af2c4821Smaybee dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs); 1479fa9e4066Sahrens mutex_exit(&zvol_state_lock); 1480af2c4821Smaybee if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag)) 1481fa9e4066Sahrens error = EFAULT; 1482fa9e4066Sahrens return (error); 1483fa9e4066Sahrens 1484fa9e4066Sahrens case DKIOCGMEDIAINFO: 1485fa9e4066Sahrens bzero(&dkm, sizeof (dkm)); 1486fa9e4066Sahrens dkm.dki_lbsize = 1U << zv->zv_min_bs; 1487fa9e4066Sahrens dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs; 1488fa9e4066Sahrens dkm.dki_media_type = DK_UNKNOWN; 1489fa9e4066Sahrens mutex_exit(&zvol_state_lock); 1490fa9e4066Sahrens if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag)) 1491fa9e4066Sahrens error = EFAULT; 1492fa9e4066Sahrens return (error); 1493fa9e4066Sahrens 1494fa9e4066Sahrens case DKIOCGETEFI: 1495c7f714e2SEric Taylor { 1496c7f714e2SEric Taylor uint64_t vs = zv->zv_volsize; 1497c7f714e2SEric Taylor uint8_t bs = zv->zv_min_bs; 1498fa9e4066Sahrens 1499fa9e4066Sahrens mutex_exit(&zvol_state_lock); 1500c7f714e2SEric Taylor error = zvol_getefi((void *)arg, flag, vs, bs); 1501fa9e4066Sahrens return (error); 1502c7f714e2SEric Taylor } 1503fa9e4066Sahrens 1504feb08c6bSbillm case DKIOCFLUSHWRITECACHE: 1505af2c4821Smaybee dkc = (struct dk_callback *)arg; 1506701f66c4SEric Taylor mutex_exit(&zvol_state_lock); 1507feb08c6bSbillm zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ); 1508af2c4821Smaybee if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) { 1509af2c4821Smaybee (*dkc->dkc_callback)(dkc->dkc_cookie, error); 1510af2c4821Smaybee error = 0; 1511af2c4821Smaybee } 1512701f66c4SEric Taylor return (error); 1513701f66c4SEric Taylor 1514701f66c4SEric Taylor case DKIOCGETWCE: 1515701f66c4SEric Taylor { 1516701f66c4SEric Taylor int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0; 1517701f66c4SEric Taylor if (ddi_copyout(&wce, (void *)arg, sizeof (int), 1518701f66c4SEric Taylor flag)) 1519701f66c4SEric Taylor error = EFAULT; 1520feb08c6bSbillm break; 1521701f66c4SEric Taylor } 1522701f66c4SEric Taylor case DKIOCSETWCE: 1523701f66c4SEric Taylor { 1524701f66c4SEric Taylor int wce; 1525701f66c4SEric Taylor if (ddi_copyin((void *)arg, &wce, sizeof (int), 1526701f66c4SEric Taylor flag)) { 1527701f66c4SEric Taylor error = EFAULT; 1528701f66c4SEric Taylor break; 1529701f66c4SEric Taylor } 1530701f66c4SEric Taylor if (wce) { 1531701f66c4SEric Taylor zv->zv_flags |= ZVOL_WCE; 1532701f66c4SEric Taylor mutex_exit(&zvol_state_lock); 1533701f66c4SEric Taylor } else { 1534701f66c4SEric Taylor zv->zv_flags &= ~ZVOL_WCE; 1535701f66c4SEric Taylor mutex_exit(&zvol_state_lock); 1536701f66c4SEric Taylor zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ); 1537701f66c4SEric Taylor } 1538701f66c4SEric Taylor return (0); 1539701f66c4SEric Taylor } 1540feb08c6bSbillm 1541b6130eadSmaybee case DKIOCGGEOM: 1542b6130eadSmaybee case DKIOCGVTOC: 1543e7cbe64fSgw25295 /* 1544e7cbe64fSgw25295 * commands using these (like prtvtoc) expect ENOTSUP 1545e7cbe64fSgw25295 * since we're emulating an EFI label 1546e7cbe64fSgw25295 */ 1547b6130eadSmaybee error = ENOTSUP; 1548b6130eadSmaybee break; 1549b6130eadSmaybee 1550e7cbe64fSgw25295 case DKIOCDUMPINIT: 1551e7cbe64fSgw25295 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 1552e7cbe64fSgw25295 RL_WRITER); 1553e7cbe64fSgw25295 error = zvol_dumpify(zv); 1554e7cbe64fSgw25295 zfs_range_unlock(rl); 1555e7cbe64fSgw25295 break; 1556e7cbe64fSgw25295 1557e7cbe64fSgw25295 case DKIOCDUMPFINI: 155806d5ae10SEric Taylor if (!(zv->zv_flags & ZVOL_DUMPIFIED)) 155906d5ae10SEric Taylor break; 1560e7cbe64fSgw25295 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 1561e7cbe64fSgw25295 RL_WRITER); 1562e7cbe64fSgw25295 error = zvol_dump_fini(zv); 1563e7cbe64fSgw25295 zfs_range_unlock(rl); 1564e7cbe64fSgw25295 break; 1565e7cbe64fSgw25295 1566fa9e4066Sahrens default: 156768a5ac4dSmaybee error = ENOTTY; 1568fa9e4066Sahrens break; 1569fa9e4066Sahrens 1570fa9e4066Sahrens } 1571fa9e4066Sahrens mutex_exit(&zvol_state_lock); 1572fa9e4066Sahrens return (error); 1573fa9e4066Sahrens } 1574fa9e4066Sahrens 1575fa9e4066Sahrens int 1576fa9e4066Sahrens zvol_busy(void) 1577fa9e4066Sahrens { 1578fa9e4066Sahrens return (zvol_minors != 0); 1579fa9e4066Sahrens } 1580fa9e4066Sahrens 1581fa9e4066Sahrens void 1582fa9e4066Sahrens zvol_init(void) 1583fa9e4066Sahrens { 1584fa9e4066Sahrens VERIFY(ddi_soft_state_init(&zvol_state, sizeof (zvol_state_t), 1) == 0); 1585fa9e4066Sahrens mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL); 1586fa9e4066Sahrens } 1587fa9e4066Sahrens 1588fa9e4066Sahrens void 1589fa9e4066Sahrens zvol_fini(void) 1590fa9e4066Sahrens { 1591fa9e4066Sahrens mutex_destroy(&zvol_state_lock); 1592fa9e4066Sahrens ddi_soft_state_fini(&zvol_state); 1593fa9e4066Sahrens } 1594e7cbe64fSgw25295 1595e7cbe64fSgw25295 static int 1596e7cbe64fSgw25295 zvol_dump_init(zvol_state_t *zv, boolean_t resize) 1597e7cbe64fSgw25295 { 1598e7cbe64fSgw25295 dmu_tx_t *tx; 1599e7cbe64fSgw25295 int error = 0; 1600e7cbe64fSgw25295 objset_t *os = zv->zv_objset; 1601e7cbe64fSgw25295 nvlist_t *nv = NULL; 16028d265e66SGeorge Wilson uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset)); 1603e7cbe64fSgw25295 1604e7cbe64fSgw25295 ASSERT(MUTEX_HELD(&zvol_state_lock)); 1605681d9761SEric Taylor error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0, 1606681d9761SEric Taylor DMU_OBJECT_END); 1607681d9761SEric Taylor /* wait for dmu_free_long_range to actually free the blocks */ 1608681d9761SEric Taylor txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 1609e7cbe64fSgw25295 1610e7cbe64fSgw25295 tx = dmu_tx_create(os); 1611e7cbe64fSgw25295 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 1612681d9761SEric Taylor dmu_tx_hold_bonus(tx, ZVOL_OBJ); 1613e7cbe64fSgw25295 error = dmu_tx_assign(tx, TXG_WAIT); 1614e7cbe64fSgw25295 if (error) { 1615e7cbe64fSgw25295 dmu_tx_abort(tx); 1616e7cbe64fSgw25295 return (error); 1617e7cbe64fSgw25295 } 1618e7cbe64fSgw25295 1619e7cbe64fSgw25295 /* 1620e7cbe64fSgw25295 * If we are resizing the dump device then we only need to 1621e7cbe64fSgw25295 * update the refreservation to match the newly updated 1622e7cbe64fSgw25295 * zvolsize. Otherwise, we save off the original state of the 1623e7cbe64fSgw25295 * zvol so that we can restore them if the zvol is ever undumpified. 1624e7cbe64fSgw25295 */ 1625e7cbe64fSgw25295 if (resize) { 1626e7cbe64fSgw25295 error = zap_update(os, ZVOL_ZAP_OBJ, 1627e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 1628e7cbe64fSgw25295 &zv->zv_volsize, tx); 1629e7cbe64fSgw25295 } else { 1630afee20e4SGeorge Wilson uint64_t checksum, compress, refresrv, vbs, dedup; 163188b7b0f2SMatthew Ahrens 1632e7cbe64fSgw25295 error = dsl_prop_get_integer(zv->zv_name, 1633e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL); 1634e7cbe64fSgw25295 error = error ? error : dsl_prop_get_integer(zv->zv_name, 1635e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL); 1636e7cbe64fSgw25295 error = error ? error : dsl_prop_get_integer(zv->zv_name, 1637e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL); 163888b7b0f2SMatthew Ahrens error = error ? error : dsl_prop_get_integer(zv->zv_name, 163988b7b0f2SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL); 16408d265e66SGeorge Wilson if (version >= SPA_VERSION_DEDUP) { 16418d265e66SGeorge Wilson error = error ? error : 16428d265e66SGeorge Wilson dsl_prop_get_integer(zv->zv_name, 1643afee20e4SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), &dedup, NULL); 16448d265e66SGeorge Wilson } 1645e7cbe64fSgw25295 1646e7cbe64fSgw25295 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1647e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, 1648e7cbe64fSgw25295 &compress, tx); 1649e7cbe64fSgw25295 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1650e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx); 1651e7cbe64fSgw25295 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1652e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 1653e7cbe64fSgw25295 &refresrv, tx); 165488b7b0f2SMatthew Ahrens error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 165588b7b0f2SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, 165688b7b0f2SMatthew Ahrens &vbs, tx); 1657681d9761SEric Taylor error = error ? error : dmu_object_set_blocksize( 1658681d9761SEric Taylor os, ZVOL_OBJ, SPA_MAXBLOCKSIZE, 0, tx); 16598d265e66SGeorge Wilson if (version >= SPA_VERSION_DEDUP) { 1660afee20e4SGeorge Wilson error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1661afee20e4SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, 1662afee20e4SGeorge Wilson &dedup, tx); 16638d265e66SGeorge Wilson } 1664681d9761SEric Taylor if (error == 0) 1665681d9761SEric Taylor zv->zv_volblocksize = SPA_MAXBLOCKSIZE; 1666e7cbe64fSgw25295 } 1667e7cbe64fSgw25295 dmu_tx_commit(tx); 1668e7cbe64fSgw25295 1669e7cbe64fSgw25295 /* 1670e7cbe64fSgw25295 * We only need update the zvol's property if we are initializing 1671e7cbe64fSgw25295 * the dump area for the first time. 1672e7cbe64fSgw25295 */ 1673e7cbe64fSgw25295 if (!resize) { 1674e7cbe64fSgw25295 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1675e7cbe64fSgw25295 VERIFY(nvlist_add_uint64(nv, 1676e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0); 1677e7cbe64fSgw25295 VERIFY(nvlist_add_uint64(nv, 1678e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 1679e7cbe64fSgw25295 ZIO_COMPRESS_OFF) == 0); 1680e7cbe64fSgw25295 VERIFY(nvlist_add_uint64(nv, 1681e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 1682e7cbe64fSgw25295 ZIO_CHECKSUM_OFF) == 0); 16838d265e66SGeorge Wilson if (version >= SPA_VERSION_DEDUP) { 1684afee20e4SGeorge Wilson VERIFY(nvlist_add_uint64(nv, 1685afee20e4SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), 1686afee20e4SGeorge Wilson ZIO_CHECKSUM_OFF) == 0); 16878d265e66SGeorge Wilson } 1688e7cbe64fSgw25295 168992241e0bSTom Erickson error = zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL, 169092241e0bSTom Erickson nv, NULL); 1691e7cbe64fSgw25295 nvlist_free(nv); 1692e7cbe64fSgw25295 1693e7cbe64fSgw25295 if (error) 1694e7cbe64fSgw25295 return (error); 1695e7cbe64fSgw25295 } 1696e7cbe64fSgw25295 1697e7cbe64fSgw25295 /* Allocate the space for the dump */ 1698e7cbe64fSgw25295 error = zvol_prealloc(zv); 1699e7cbe64fSgw25295 return (error); 1700e7cbe64fSgw25295 } 1701e7cbe64fSgw25295 1702e7cbe64fSgw25295 static int 1703e7cbe64fSgw25295 zvol_dumpify(zvol_state_t *zv) 1704e7cbe64fSgw25295 { 1705e7cbe64fSgw25295 int error = 0; 1706e7cbe64fSgw25295 uint64_t dumpsize = 0; 1707e7cbe64fSgw25295 dmu_tx_t *tx; 1708e7cbe64fSgw25295 objset_t *os = zv->zv_objset; 1709e7cbe64fSgw25295 1710681d9761SEric Taylor if (zv->zv_flags & ZVOL_RDONLY) 1711e7cbe64fSgw25295 return (EROFS); 1712e7cbe64fSgw25295 1713e7cbe64fSgw25295 if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 1714e7cbe64fSgw25295 8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) { 1715e7cbe64fSgw25295 boolean_t resize = (dumpsize > 0) ? B_TRUE : B_FALSE; 1716e7cbe64fSgw25295 1717e7cbe64fSgw25295 if ((error = zvol_dump_init(zv, resize)) != 0) { 1718e7cbe64fSgw25295 (void) zvol_dump_fini(zv); 1719e7cbe64fSgw25295 return (error); 1720e7cbe64fSgw25295 } 1721e7cbe64fSgw25295 } 1722e7cbe64fSgw25295 1723e7cbe64fSgw25295 /* 1724e7cbe64fSgw25295 * Build up our lba mapping. 1725e7cbe64fSgw25295 */ 1726e7cbe64fSgw25295 error = zvol_get_lbas(zv); 1727e7cbe64fSgw25295 if (error) { 1728e7cbe64fSgw25295 (void) zvol_dump_fini(zv); 1729e7cbe64fSgw25295 return (error); 1730e7cbe64fSgw25295 } 1731e7cbe64fSgw25295 1732e7cbe64fSgw25295 tx = dmu_tx_create(os); 1733e7cbe64fSgw25295 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 1734e7cbe64fSgw25295 error = dmu_tx_assign(tx, TXG_WAIT); 1735e7cbe64fSgw25295 if (error) { 1736e7cbe64fSgw25295 dmu_tx_abort(tx); 1737e7cbe64fSgw25295 (void) zvol_dump_fini(zv); 1738e7cbe64fSgw25295 return (error); 1739e7cbe64fSgw25295 } 1740e7cbe64fSgw25295 1741e7cbe64fSgw25295 zv->zv_flags |= ZVOL_DUMPIFIED; 1742e7cbe64fSgw25295 error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1, 1743e7cbe64fSgw25295 &zv->zv_volsize, tx); 1744e7cbe64fSgw25295 dmu_tx_commit(tx); 1745e7cbe64fSgw25295 1746e7cbe64fSgw25295 if (error) { 1747e7cbe64fSgw25295 (void) zvol_dump_fini(zv); 1748e7cbe64fSgw25295 return (error); 1749e7cbe64fSgw25295 } 1750e7cbe64fSgw25295 1751e7cbe64fSgw25295 txg_wait_synced(dmu_objset_pool(os), 0); 1752e7cbe64fSgw25295 return (0); 1753e7cbe64fSgw25295 } 1754e7cbe64fSgw25295 1755e7cbe64fSgw25295 static int 1756e7cbe64fSgw25295 zvol_dump_fini(zvol_state_t *zv) 1757e7cbe64fSgw25295 { 1758e7cbe64fSgw25295 dmu_tx_t *tx; 1759e7cbe64fSgw25295 objset_t *os = zv->zv_objset; 1760e7cbe64fSgw25295 nvlist_t *nv; 1761e7cbe64fSgw25295 int error = 0; 1762afee20e4SGeorge Wilson uint64_t checksum, compress, refresrv, vbs, dedup; 17638d265e66SGeorge Wilson uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset)); 1764e7cbe64fSgw25295 1765b7e50089Smaybee /* 1766b7e50089Smaybee * Attempt to restore the zvol back to its pre-dumpified state. 1767b7e50089Smaybee * This is a best-effort attempt as it's possible that not all 1768b7e50089Smaybee * of these properties were initialized during the dumpify process 1769b7e50089Smaybee * (i.e. error during zvol_dump_init). 1770b7e50089Smaybee */ 1771b7e50089Smaybee 1772e7cbe64fSgw25295 tx = dmu_tx_create(os); 1773e7cbe64fSgw25295 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 1774e7cbe64fSgw25295 error = dmu_tx_assign(tx, TXG_WAIT); 1775e7cbe64fSgw25295 if (error) { 1776e7cbe64fSgw25295 dmu_tx_abort(tx); 1777e7cbe64fSgw25295 return (error); 1778e7cbe64fSgw25295 } 1779b7e50089Smaybee (void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx); 1780b7e50089Smaybee dmu_tx_commit(tx); 1781e7cbe64fSgw25295 1782e7cbe64fSgw25295 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 1783e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum); 1784e7cbe64fSgw25295 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 1785e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress); 1786e7cbe64fSgw25295 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 1787e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv); 178888b7b0f2SMatthew Ahrens (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 178988b7b0f2SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs); 1790e7cbe64fSgw25295 1791e7cbe64fSgw25295 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1792e7cbe64fSgw25295 (void) nvlist_add_uint64(nv, 1793e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum); 1794e7cbe64fSgw25295 (void) nvlist_add_uint64(nv, 1795e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress); 1796e7cbe64fSgw25295 (void) nvlist_add_uint64(nv, 1797e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv); 17988d265e66SGeorge Wilson if (version >= SPA_VERSION_DEDUP && 17998d265e66SGeorge Wilson zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 18008d265e66SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, &dedup) == 0) { 1801afee20e4SGeorge Wilson (void) nvlist_add_uint64(nv, 1802afee20e4SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), dedup); 18038d265e66SGeorge Wilson } 180492241e0bSTom Erickson (void) zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL, 180592241e0bSTom Erickson nv, NULL); 1806e7cbe64fSgw25295 nvlist_free(nv); 1807e7cbe64fSgw25295 1808b7e50089Smaybee zvol_free_extents(zv); 1809b7e50089Smaybee zv->zv_flags &= ~ZVOL_DUMPIFIED; 1810b7e50089Smaybee (void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END); 1811681d9761SEric Taylor /* wait for dmu_free_long_range to actually free the blocks */ 1812681d9761SEric Taylor txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 1813681d9761SEric Taylor tx = dmu_tx_create(os); 1814681d9761SEric Taylor dmu_tx_hold_bonus(tx, ZVOL_OBJ); 1815681d9761SEric Taylor error = dmu_tx_assign(tx, TXG_WAIT); 1816681d9761SEric Taylor if (error) { 1817681d9761SEric Taylor dmu_tx_abort(tx); 1818681d9761SEric Taylor return (error); 1819681d9761SEric Taylor } 1820b24ab676SJeff Bonwick if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0) 1821b24ab676SJeff Bonwick zv->zv_volblocksize = vbs; 1822681d9761SEric Taylor dmu_tx_commit(tx); 1823b7e50089Smaybee 1824e7cbe64fSgw25295 return (0); 1825e7cbe64fSgw25295 } 1826