1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5ea8dc4b6Seschrock * Common Development and Distribution License (the "License"). 6ea8dc4b6Seschrock * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 22f80ce222SChris Kirby * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23fa9e4066Sahrens */ 24fa9e4066Sahrens 25*55da60b9SMark J Musante /* Portions Copyright 2010 Robert Milkowski */ 26*55da60b9SMark J Musante 27fa9e4066Sahrens /* 28fa9e4066Sahrens * ZFS volume emulation driver. 29fa9e4066Sahrens * 30fa9e4066Sahrens * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes. 31fa9e4066Sahrens * Volumes are accessed through the symbolic links named: 32fa9e4066Sahrens * 33fa9e4066Sahrens * /dev/zvol/dsk/<pool_name>/<dataset_name> 34fa9e4066Sahrens * /dev/zvol/rdsk/<pool_name>/<dataset_name> 35fa9e4066Sahrens * 36681d9761SEric Taylor * These links are created by the /dev filesystem (sdev_zvolops.c). 37fa9e4066Sahrens * Volumes are persistent through reboot. No user command needs to be 38fa9e4066Sahrens * run before opening and using a device. 39fa9e4066Sahrens */ 40fa9e4066Sahrens 41fa9e4066Sahrens #include <sys/types.h> 42fa9e4066Sahrens #include <sys/param.h> 43fa9e4066Sahrens #include <sys/errno.h> 44fa9e4066Sahrens #include <sys/uio.h> 45fa9e4066Sahrens #include <sys/buf.h> 46fa9e4066Sahrens #include <sys/modctl.h> 47fa9e4066Sahrens #include <sys/open.h> 48fa9e4066Sahrens #include <sys/kmem.h> 49fa9e4066Sahrens #include <sys/conf.h> 50fa9e4066Sahrens #include <sys/cmn_err.h> 51fa9e4066Sahrens #include <sys/stat.h> 52fa9e4066Sahrens #include <sys/zap.h> 53fa9e4066Sahrens #include <sys/spa.h> 54fa9e4066Sahrens #include <sys/zio.h> 55e7cbe64fSgw25295 #include <sys/dmu_traverse.h> 56e7cbe64fSgw25295 #include <sys/dnode.h> 57e7cbe64fSgw25295 #include <sys/dsl_dataset.h> 58fa9e4066Sahrens #include <sys/dsl_prop.h> 59fa9e4066Sahrens #include <sys/dkio.h> 60fa9e4066Sahrens #include <sys/efi_partition.h> 61fa9e4066Sahrens #include <sys/byteorder.h> 62fa9e4066Sahrens #include <sys/pathname.h> 63fa9e4066Sahrens #include <sys/ddi.h> 64fa9e4066Sahrens #include <sys/sunddi.h> 65fa9e4066Sahrens #include <sys/crc32.h> 66fa9e4066Sahrens #include <sys/dirent.h> 67fa9e4066Sahrens #include <sys/policy.h> 68fa9e4066Sahrens #include <sys/fs/zfs.h> 69fa9e4066Sahrens #include <sys/zfs_ioctl.h> 70fa9e4066Sahrens #include <sys/mkdev.h> 7122ac5be4Sperrin #include <sys/zil.h> 72c5c6ffa0Smaybee #include <sys/refcount.h> 73c2e6a7d6Sperrin #include <sys/zfs_znode.h> 74c2e6a7d6Sperrin #include <sys/zfs_rlock.h> 75e7cbe64fSgw25295 #include <sys/vdev_disk.h> 76e7cbe64fSgw25295 #include <sys/vdev_impl.h> 77e7cbe64fSgw25295 #include <sys/zvol.h> 78e7cbe64fSgw25295 #include <sys/dumphdr.h> 791209a471SNeil Perrin #include <sys/zil_impl.h> 80fa9e4066Sahrens 81fa9e4066Sahrens #include "zfs_namecheck.h" 82fa9e4066Sahrens 83fa9e4066Sahrens static void *zvol_state; 84503ad85cSMatthew Ahrens static char *zvol_tag = "zvol_tag"; 85fa9e4066Sahrens 86e7cbe64fSgw25295 #define ZVOL_DUMPSIZE "dumpsize" 87e7cbe64fSgw25295 88fa9e4066Sahrens /* 89fa9e4066Sahrens * This lock protects the zvol_state structure from being modified 90fa9e4066Sahrens * while it's being used, e.g. an open that comes in before a create 91fa9e4066Sahrens * finishes. It also protects temporary opens of the dataset so that, 92fa9e4066Sahrens * e.g., an open doesn't get a spurious EBUSY. 93fa9e4066Sahrens */ 94fa9e4066Sahrens static kmutex_t zvol_state_lock; 95fa9e4066Sahrens static uint32_t zvol_minors; 96fa9e4066Sahrens 97e7cbe64fSgw25295 typedef struct zvol_extent { 9888b7b0f2SMatthew Ahrens list_node_t ze_node; 99e7cbe64fSgw25295 dva_t ze_dva; /* dva associated with this extent */ 10088b7b0f2SMatthew Ahrens uint64_t ze_nblks; /* number of blocks in extent */ 101e7cbe64fSgw25295 } zvol_extent_t; 102e7cbe64fSgw25295 103e7cbe64fSgw25295 /* 104fa9e4066Sahrens * The in-core state of each volume. 105fa9e4066Sahrens */ 106fa9e4066Sahrens typedef struct zvol_state { 107fa9e4066Sahrens char zv_name[MAXPATHLEN]; /* pool/dd name */ 108fa9e4066Sahrens uint64_t zv_volsize; /* amount of space we advertise */ 10967bd71c6Sperrin uint64_t zv_volblocksize; /* volume block size */ 110fa9e4066Sahrens minor_t zv_minor; /* minor number */ 111fa9e4066Sahrens uint8_t zv_min_bs; /* minimum addressable block shift */ 112701f66c4SEric Taylor uint8_t zv_flags; /* readonly, dumpified, etc. */ 113fa9e4066Sahrens objset_t *zv_objset; /* objset handle */ 114fa9e4066Sahrens uint32_t zv_open_count[OTYPCNT]; /* open counts */ 115fa9e4066Sahrens uint32_t zv_total_opens; /* total open count */ 11622ac5be4Sperrin zilog_t *zv_zilog; /* ZIL handle */ 11788b7b0f2SMatthew Ahrens list_t zv_extents; /* List of extents for dump */ 118c2e6a7d6Sperrin znode_t zv_znode; /* for range locking */ 11994d1a210STim Haley dmu_buf_t *zv_dbuf; /* bonus handle */ 120fa9e4066Sahrens } zvol_state_t; 121fa9e4066Sahrens 12267bd71c6Sperrin /* 123e7cbe64fSgw25295 * zvol specific flags 124e7cbe64fSgw25295 */ 125e7cbe64fSgw25295 #define ZVOL_RDONLY 0x1 126e7cbe64fSgw25295 #define ZVOL_DUMPIFIED 0x2 127c7f714e2SEric Taylor #define ZVOL_EXCL 0x4 128701f66c4SEric Taylor #define ZVOL_WCE 0x8 129e7cbe64fSgw25295 130e7cbe64fSgw25295 /* 13167bd71c6Sperrin * zvol maximum transfer in one DMU tx. 13267bd71c6Sperrin */ 13367bd71c6Sperrin int zvol_maxphys = DMU_MAX_ACCESS/2; 13467bd71c6Sperrin 13592241e0bSTom Erickson extern int zfs_set_prop_nvlist(const char *, zprop_source_t, 13692241e0bSTom Erickson nvlist_t *, nvlist_t **); 137681d9761SEric Taylor static int zvol_remove_zv(zvol_state_t *); 138feb08c6bSbillm static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio); 139e7cbe64fSgw25295 static int zvol_dumpify(zvol_state_t *zv); 140e7cbe64fSgw25295 static int zvol_dump_fini(zvol_state_t *zv); 141e7cbe64fSgw25295 static int zvol_dump_init(zvol_state_t *zv, boolean_t resize); 14267bd71c6Sperrin 143fa9e4066Sahrens static void 144681d9761SEric Taylor zvol_size_changed(uint64_t volsize, major_t maj, minor_t min) 145fa9e4066Sahrens { 146681d9761SEric Taylor dev_t dev = makedevice(maj, min); 147fa9e4066Sahrens 148fa9e4066Sahrens VERIFY(ddi_prop_update_int64(dev, zfs_dip, 149681d9761SEric Taylor "Size", volsize) == DDI_SUCCESS); 150fa9e4066Sahrens VERIFY(ddi_prop_update_int64(dev, zfs_dip, 151681d9761SEric Taylor "Nblocks", lbtodb(volsize)) == DDI_SUCCESS); 152e7cbe64fSgw25295 153e7cbe64fSgw25295 /* Notify specfs to invalidate the cached size */ 154e7cbe64fSgw25295 spec_size_invalidate(dev, VBLK); 155e7cbe64fSgw25295 spec_size_invalidate(dev, VCHR); 156fa9e4066Sahrens } 157fa9e4066Sahrens 158fa9e4066Sahrens int 159e9dbad6fSeschrock zvol_check_volsize(uint64_t volsize, uint64_t blocksize) 160fa9e4066Sahrens { 161e9dbad6fSeschrock if (volsize == 0) 162fa9e4066Sahrens return (EINVAL); 163fa9e4066Sahrens 164e9dbad6fSeschrock if (volsize % blocksize != 0) 1655c5460e9Seschrock return (EINVAL); 1665c5460e9Seschrock 167fa9e4066Sahrens #ifdef _ILP32 168e9dbad6fSeschrock if (volsize - 1 > SPEC_MAXOFFSET_T) 169fa9e4066Sahrens return (EOVERFLOW); 170fa9e4066Sahrens #endif 171fa9e4066Sahrens return (0); 172fa9e4066Sahrens } 173fa9e4066Sahrens 174fa9e4066Sahrens int 175e9dbad6fSeschrock zvol_check_volblocksize(uint64_t volblocksize) 176fa9e4066Sahrens { 177e9dbad6fSeschrock if (volblocksize < SPA_MINBLOCKSIZE || 178e9dbad6fSeschrock volblocksize > SPA_MAXBLOCKSIZE || 179e9dbad6fSeschrock !ISP2(volblocksize)) 180fa9e4066Sahrens return (EDOM); 181fa9e4066Sahrens 182fa9e4066Sahrens return (0); 183fa9e4066Sahrens } 184fa9e4066Sahrens 185fa9e4066Sahrens int 186a2eea2e1Sahrens zvol_get_stats(objset_t *os, nvlist_t *nv) 187fa9e4066Sahrens { 188fa9e4066Sahrens int error; 189fa9e4066Sahrens dmu_object_info_t doi; 190a2eea2e1Sahrens uint64_t val; 191fa9e4066Sahrens 192a2eea2e1Sahrens error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val); 193fa9e4066Sahrens if (error) 194fa9e4066Sahrens return (error); 195fa9e4066Sahrens 196a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val); 197a2eea2e1Sahrens 198fa9e4066Sahrens error = dmu_object_info(os, ZVOL_OBJ, &doi); 199fa9e4066Sahrens 200a2eea2e1Sahrens if (error == 0) { 201a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE, 202a2eea2e1Sahrens doi.doi_data_block_size); 203a2eea2e1Sahrens } 204fa9e4066Sahrens 205fa9e4066Sahrens return (error); 206fa9e4066Sahrens } 207fa9e4066Sahrens 208fa9e4066Sahrens /* 209fa9e4066Sahrens * Find a free minor number. 210fa9e4066Sahrens */ 211fa9e4066Sahrens static minor_t 212fa9e4066Sahrens zvol_minor_alloc(void) 213fa9e4066Sahrens { 214fa9e4066Sahrens minor_t minor; 215fa9e4066Sahrens 216fa9e4066Sahrens ASSERT(MUTEX_HELD(&zvol_state_lock)); 217fa9e4066Sahrens 218fa9e4066Sahrens for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) 219fa9e4066Sahrens if (ddi_get_soft_state(zvol_state, minor) == NULL) 220fa9e4066Sahrens return (minor); 221fa9e4066Sahrens 222fa9e4066Sahrens return (0); 223fa9e4066Sahrens } 224fa9e4066Sahrens 225fa9e4066Sahrens static zvol_state_t * 226e9dbad6fSeschrock zvol_minor_lookup(const char *name) 227fa9e4066Sahrens { 228fa9e4066Sahrens minor_t minor; 229fa9e4066Sahrens zvol_state_t *zv; 230fa9e4066Sahrens 231fa9e4066Sahrens ASSERT(MUTEX_HELD(&zvol_state_lock)); 232fa9e4066Sahrens 233fa9e4066Sahrens for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) { 234fa9e4066Sahrens zv = ddi_get_soft_state(zvol_state, minor); 235fa9e4066Sahrens if (zv == NULL) 236fa9e4066Sahrens continue; 237fa9e4066Sahrens if (strcmp(zv->zv_name, name) == 0) 238f80ce222SChris Kirby return (zv); 239fa9e4066Sahrens } 240fa9e4066Sahrens 241f80ce222SChris Kirby return (NULL); 242fa9e4066Sahrens } 243fa9e4066Sahrens 244e7cbe64fSgw25295 /* extent mapping arg */ 245e7cbe64fSgw25295 struct maparg { 24688b7b0f2SMatthew Ahrens zvol_state_t *ma_zv; 24788b7b0f2SMatthew Ahrens uint64_t ma_blks; 248e7cbe64fSgw25295 }; 249e7cbe64fSgw25295 250e7cbe64fSgw25295 /*ARGSUSED*/ 251e7cbe64fSgw25295 static int 252b24ab676SJeff Bonwick zvol_map_block(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 253b24ab676SJeff Bonwick const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg) 254e7cbe64fSgw25295 { 25588b7b0f2SMatthew Ahrens struct maparg *ma = arg; 25688b7b0f2SMatthew Ahrens zvol_extent_t *ze; 25788b7b0f2SMatthew Ahrens int bs = ma->ma_zv->zv_volblocksize; 258e7cbe64fSgw25295 25988b7b0f2SMatthew Ahrens if (bp == NULL || zb->zb_object != ZVOL_OBJ || zb->zb_level != 0) 260e7cbe64fSgw25295 return (0); 261e7cbe64fSgw25295 26288b7b0f2SMatthew Ahrens VERIFY3U(ma->ma_blks, ==, zb->zb_blkid); 26388b7b0f2SMatthew Ahrens ma->ma_blks++; 26488b7b0f2SMatthew Ahrens 265e7cbe64fSgw25295 /* Abort immediately if we have encountered gang blocks */ 26688b7b0f2SMatthew Ahrens if (BP_IS_GANG(bp)) 26788b7b0f2SMatthew Ahrens return (EFRAGS); 268e7cbe64fSgw25295 269e7cbe64fSgw25295 /* 27088b7b0f2SMatthew Ahrens * See if the block is at the end of the previous extent. 271e7cbe64fSgw25295 */ 27288b7b0f2SMatthew Ahrens ze = list_tail(&ma->ma_zv->zv_extents); 27388b7b0f2SMatthew Ahrens if (ze && 27488b7b0f2SMatthew Ahrens DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) && 27588b7b0f2SMatthew Ahrens DVA_GET_OFFSET(BP_IDENTITY(bp)) == 27688b7b0f2SMatthew Ahrens DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) { 27788b7b0f2SMatthew Ahrens ze->ze_nblks++; 27888b7b0f2SMatthew Ahrens return (0); 27988b7b0f2SMatthew Ahrens } 28088b7b0f2SMatthew Ahrens 281e7cbe64fSgw25295 dprintf_bp(bp, "%s", "next blkptr:"); 28288b7b0f2SMatthew Ahrens 283e7cbe64fSgw25295 /* start a new extent */ 28488b7b0f2SMatthew Ahrens ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP); 28588b7b0f2SMatthew Ahrens ze->ze_dva = bp->blk_dva[0]; /* structure assignment */ 28688b7b0f2SMatthew Ahrens ze->ze_nblks = 1; 28788b7b0f2SMatthew Ahrens list_insert_tail(&ma->ma_zv->zv_extents, ze); 28888b7b0f2SMatthew Ahrens return (0); 289e7cbe64fSgw25295 } 29088b7b0f2SMatthew Ahrens 29188b7b0f2SMatthew Ahrens static void 29288b7b0f2SMatthew Ahrens zvol_free_extents(zvol_state_t *zv) 29388b7b0f2SMatthew Ahrens { 29488b7b0f2SMatthew Ahrens zvol_extent_t *ze; 29588b7b0f2SMatthew Ahrens 29688b7b0f2SMatthew Ahrens while (ze = list_head(&zv->zv_extents)) { 29788b7b0f2SMatthew Ahrens list_remove(&zv->zv_extents, ze); 29888b7b0f2SMatthew Ahrens kmem_free(ze, sizeof (zvol_extent_t)); 29988b7b0f2SMatthew Ahrens } 30088b7b0f2SMatthew Ahrens } 30188b7b0f2SMatthew Ahrens 30288b7b0f2SMatthew Ahrens static int 30388b7b0f2SMatthew Ahrens zvol_get_lbas(zvol_state_t *zv) 30488b7b0f2SMatthew Ahrens { 3053adc9019SEric Taylor objset_t *os = zv->zv_objset; 30688b7b0f2SMatthew Ahrens struct maparg ma; 30788b7b0f2SMatthew Ahrens int err; 30888b7b0f2SMatthew Ahrens 30988b7b0f2SMatthew Ahrens ma.ma_zv = zv; 31088b7b0f2SMatthew Ahrens ma.ma_blks = 0; 31188b7b0f2SMatthew Ahrens zvol_free_extents(zv); 31288b7b0f2SMatthew Ahrens 3133adc9019SEric Taylor /* commit any in-flight changes before traversing the dataset */ 3143adc9019SEric Taylor txg_wait_synced(dmu_objset_pool(os), 0); 3153adc9019SEric Taylor err = traverse_dataset(dmu_objset_ds(os), 0, 31688b7b0f2SMatthew Ahrens TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma); 31788b7b0f2SMatthew Ahrens if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) { 31888b7b0f2SMatthew Ahrens zvol_free_extents(zv); 31988b7b0f2SMatthew Ahrens return (err ? err : EIO); 32088b7b0f2SMatthew Ahrens } 32188b7b0f2SMatthew Ahrens 322e7cbe64fSgw25295 return (0); 323e7cbe64fSgw25295 } 324e7cbe64fSgw25295 325ecd6cf80Smarks /* ARGSUSED */ 326fa9e4066Sahrens void 327ecd6cf80Smarks zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 328fa9e4066Sahrens { 329da6c28aaSamw zfs_creat_t *zct = arg; 330da6c28aaSamw nvlist_t *nvprops = zct->zct_props; 331fa9e4066Sahrens int error; 332e9dbad6fSeschrock uint64_t volblocksize, volsize; 333fa9e4066Sahrens 334ecd6cf80Smarks VERIFY(nvlist_lookup_uint64(nvprops, 335e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0); 336ecd6cf80Smarks if (nvlist_lookup_uint64(nvprops, 337e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0) 338e9dbad6fSeschrock volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 339e9dbad6fSeschrock 340e9dbad6fSeschrock /* 341e7cbe64fSgw25295 * These properties must be removed from the list so the generic 342e9dbad6fSeschrock * property setting step won't apply to them. 343e9dbad6fSeschrock */ 344ecd6cf80Smarks VERIFY(nvlist_remove_all(nvprops, 345e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0); 346ecd6cf80Smarks (void) nvlist_remove_all(nvprops, 347e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE)); 348e9dbad6fSeschrock 349e9dbad6fSeschrock error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize, 350fa9e4066Sahrens DMU_OT_NONE, 0, tx); 351fa9e4066Sahrens ASSERT(error == 0); 352fa9e4066Sahrens 353fa9e4066Sahrens error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP, 354fa9e4066Sahrens DMU_OT_NONE, 0, tx); 355fa9e4066Sahrens ASSERT(error == 0); 356fa9e4066Sahrens 357e9dbad6fSeschrock error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx); 358fa9e4066Sahrens ASSERT(error == 0); 359fa9e4066Sahrens } 360fa9e4066Sahrens 361fa9e4066Sahrens /* 36222ac5be4Sperrin * Replay a TX_WRITE ZIL transaction that didn't get committed 36322ac5be4Sperrin * after a system failure 36422ac5be4Sperrin */ 36522ac5be4Sperrin static int 36622ac5be4Sperrin zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap) 36722ac5be4Sperrin { 36822ac5be4Sperrin objset_t *os = zv->zv_objset; 36922ac5be4Sperrin char *data = (char *)(lr + 1); /* data follows lr_write_t */ 370b24ab676SJeff Bonwick uint64_t offset, length; 37122ac5be4Sperrin dmu_tx_t *tx; 37222ac5be4Sperrin int error; 37322ac5be4Sperrin 37422ac5be4Sperrin if (byteswap) 37522ac5be4Sperrin byteswap_uint64_array(lr, sizeof (*lr)); 37622ac5be4Sperrin 377b24ab676SJeff Bonwick offset = lr->lr_offset; 378b24ab676SJeff Bonwick length = lr->lr_length; 379b24ab676SJeff Bonwick 380b24ab676SJeff Bonwick /* If it's a dmu_sync() block, write the whole block */ 381b24ab676SJeff Bonwick if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) { 382b24ab676SJeff Bonwick uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr); 383b24ab676SJeff Bonwick if (length < blocksize) { 384b24ab676SJeff Bonwick offset -= offset % blocksize; 385b24ab676SJeff Bonwick length = blocksize; 386b24ab676SJeff Bonwick } 387b24ab676SJeff Bonwick } 388975c32a0SNeil Perrin 38922ac5be4Sperrin tx = dmu_tx_create(os); 390b24ab676SJeff Bonwick dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length); 3911209a471SNeil Perrin error = dmu_tx_assign(tx, TXG_WAIT); 39222ac5be4Sperrin if (error) { 39322ac5be4Sperrin dmu_tx_abort(tx); 39422ac5be4Sperrin } else { 395b24ab676SJeff Bonwick dmu_write(os, ZVOL_OBJ, offset, length, data, tx); 39622ac5be4Sperrin dmu_tx_commit(tx); 39722ac5be4Sperrin } 39822ac5be4Sperrin 39922ac5be4Sperrin return (error); 40022ac5be4Sperrin } 40122ac5be4Sperrin 40222ac5be4Sperrin /* ARGSUSED */ 40322ac5be4Sperrin static int 40422ac5be4Sperrin zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap) 40522ac5be4Sperrin { 40622ac5be4Sperrin return (ENOTSUP); 40722ac5be4Sperrin } 40822ac5be4Sperrin 40922ac5be4Sperrin /* 41022ac5be4Sperrin * Callback vectors for replaying records. 41122ac5be4Sperrin * Only TX_WRITE is needed for zvol. 41222ac5be4Sperrin */ 41322ac5be4Sperrin zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = { 41422ac5be4Sperrin zvol_replay_err, /* 0 no such transaction type */ 41522ac5be4Sperrin zvol_replay_err, /* TX_CREATE */ 41622ac5be4Sperrin zvol_replay_err, /* TX_MKDIR */ 41722ac5be4Sperrin zvol_replay_err, /* TX_MKXATTR */ 41822ac5be4Sperrin zvol_replay_err, /* TX_SYMLINK */ 41922ac5be4Sperrin zvol_replay_err, /* TX_REMOVE */ 42022ac5be4Sperrin zvol_replay_err, /* TX_RMDIR */ 42122ac5be4Sperrin zvol_replay_err, /* TX_LINK */ 42222ac5be4Sperrin zvol_replay_err, /* TX_RENAME */ 42322ac5be4Sperrin zvol_replay_write, /* TX_WRITE */ 42422ac5be4Sperrin zvol_replay_err, /* TX_TRUNCATE */ 42522ac5be4Sperrin zvol_replay_err, /* TX_SETATTR */ 42622ac5be4Sperrin zvol_replay_err, /* TX_ACL */ 427975c32a0SNeil Perrin zvol_replay_err, /* TX_CREATE_ACL */ 428975c32a0SNeil Perrin zvol_replay_err, /* TX_CREATE_ATTR */ 429975c32a0SNeil Perrin zvol_replay_err, /* TX_CREATE_ACL_ATTR */ 430975c32a0SNeil Perrin zvol_replay_err, /* TX_MKDIR_ACL */ 431975c32a0SNeil Perrin zvol_replay_err, /* TX_MKDIR_ATTR */ 432975c32a0SNeil Perrin zvol_replay_err, /* TX_MKDIR_ACL_ATTR */ 433975c32a0SNeil Perrin zvol_replay_err, /* TX_WRITE2 */ 43422ac5be4Sperrin }; 43522ac5be4Sperrin 436681d9761SEric Taylor int 437681d9761SEric Taylor zvol_name2minor(const char *name, minor_t *minor) 438681d9761SEric Taylor { 439681d9761SEric Taylor zvol_state_t *zv; 440681d9761SEric Taylor 441681d9761SEric Taylor mutex_enter(&zvol_state_lock); 442681d9761SEric Taylor zv = zvol_minor_lookup(name); 443681d9761SEric Taylor if (minor && zv) 444681d9761SEric Taylor *minor = zv->zv_minor; 445681d9761SEric Taylor mutex_exit(&zvol_state_lock); 446681d9761SEric Taylor return (zv ? 0 : -1); 447681d9761SEric Taylor } 448681d9761SEric Taylor 44922ac5be4Sperrin /* 450e7cbe64fSgw25295 * Create a minor node (plus a whole lot more) for the specified volume. 451fa9e4066Sahrens */ 452fa9e4066Sahrens int 453681d9761SEric Taylor zvol_create_minor(const char *name) 454fa9e4066Sahrens { 455fa9e4066Sahrens zvol_state_t *zv; 456fa9e4066Sahrens objset_t *os; 45767bd71c6Sperrin dmu_object_info_t doi; 458fa9e4066Sahrens minor_t minor = 0; 459fa9e4066Sahrens char chrbuf[30], blkbuf[30]; 460fa9e4066Sahrens int error; 461fa9e4066Sahrens 462fa9e4066Sahrens mutex_enter(&zvol_state_lock); 463fa9e4066Sahrens 4641195e687SMark J Musante if (zvol_minor_lookup(name) != NULL) { 465fa9e4066Sahrens mutex_exit(&zvol_state_lock); 466fa9e4066Sahrens return (EEXIST); 467fa9e4066Sahrens } 468fa9e4066Sahrens 469503ad85cSMatthew Ahrens /* lie and say we're read-only */ 470503ad85cSMatthew Ahrens error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, zvol_tag, &os); 471fa9e4066Sahrens 472fa9e4066Sahrens if (error) { 473fa9e4066Sahrens mutex_exit(&zvol_state_lock); 474fa9e4066Sahrens return (error); 475fa9e4066Sahrens } 476fa9e4066Sahrens 477681d9761SEric Taylor if ((minor = zvol_minor_alloc()) == 0) { 478503ad85cSMatthew Ahrens dmu_objset_disown(os, zvol_tag); 479fa9e4066Sahrens mutex_exit(&zvol_state_lock); 480fa9e4066Sahrens return (ENXIO); 481fa9e4066Sahrens } 482fa9e4066Sahrens 483fa9e4066Sahrens if (ddi_soft_state_zalloc(zvol_state, minor) != DDI_SUCCESS) { 484503ad85cSMatthew Ahrens dmu_objset_disown(os, zvol_tag); 485fa9e4066Sahrens mutex_exit(&zvol_state_lock); 486fa9e4066Sahrens return (EAGAIN); 487fa9e4066Sahrens } 488e9dbad6fSeschrock (void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME, 489e9dbad6fSeschrock (char *)name); 490fa9e4066Sahrens 491681d9761SEric Taylor (void) snprintf(chrbuf, sizeof (chrbuf), "%u,raw", minor); 492fa9e4066Sahrens 493fa9e4066Sahrens if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR, 494fa9e4066Sahrens minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 495fa9e4066Sahrens ddi_soft_state_free(zvol_state, minor); 496503ad85cSMatthew Ahrens dmu_objset_disown(os, zvol_tag); 497fa9e4066Sahrens mutex_exit(&zvol_state_lock); 498fa9e4066Sahrens return (EAGAIN); 499fa9e4066Sahrens } 500fa9e4066Sahrens 501681d9761SEric Taylor (void) snprintf(blkbuf, sizeof (blkbuf), "%u", minor); 502fa9e4066Sahrens 503fa9e4066Sahrens if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK, 504fa9e4066Sahrens minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 505fa9e4066Sahrens ddi_remove_minor_node(zfs_dip, chrbuf); 506fa9e4066Sahrens ddi_soft_state_free(zvol_state, minor); 507503ad85cSMatthew Ahrens dmu_objset_disown(os, zvol_tag); 508fa9e4066Sahrens mutex_exit(&zvol_state_lock); 509fa9e4066Sahrens return (EAGAIN); 510fa9e4066Sahrens } 511fa9e4066Sahrens 512fa9e4066Sahrens zv = ddi_get_soft_state(zvol_state, minor); 513fa9e4066Sahrens 514681d9761SEric Taylor (void) strlcpy(zv->zv_name, name, MAXPATHLEN); 515fa9e4066Sahrens zv->zv_min_bs = DEV_BSHIFT; 516fa9e4066Sahrens zv->zv_minor = minor; 517fa9e4066Sahrens zv->zv_objset = os; 518681d9761SEric Taylor if (dmu_objset_is_snapshot(os)) 519681d9761SEric Taylor zv->zv_flags |= ZVOL_RDONLY; 520c2e6a7d6Sperrin mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL); 521c2e6a7d6Sperrin avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare, 522c2e6a7d6Sperrin sizeof (rl_t), offsetof(rl_t, r_node)); 52388b7b0f2SMatthew Ahrens list_create(&zv->zv_extents, sizeof (zvol_extent_t), 52488b7b0f2SMatthew Ahrens offsetof(zvol_extent_t, ze_node)); 52567bd71c6Sperrin /* get and cache the blocksize */ 52667bd71c6Sperrin error = dmu_object_info(os, ZVOL_OBJ, &doi); 52767bd71c6Sperrin ASSERT(error == 0); 52867bd71c6Sperrin zv->zv_volblocksize = doi.doi_data_block_size; 52922ac5be4Sperrin 530*55da60b9SMark J Musante if (zil_replay_disable) 531*55da60b9SMark J Musante zil_destroy(dmu_objset_zil(os), B_FALSE); 532*55da60b9SMark J Musante else 5331209a471SNeil Perrin zil_replay(os, zv, zvol_replay_vector); 534681d9761SEric Taylor dmu_objset_disown(os, zvol_tag); 535681d9761SEric Taylor zv->zv_objset = NULL; 536fa9e4066Sahrens 537fa9e4066Sahrens zvol_minors++; 538fa9e4066Sahrens 539fa9e4066Sahrens mutex_exit(&zvol_state_lock); 540fa9e4066Sahrens 541fa9e4066Sahrens return (0); 542fa9e4066Sahrens } 543fa9e4066Sahrens 544fa9e4066Sahrens /* 545fa9e4066Sahrens * Remove minor node for the specified volume. 546fa9e4066Sahrens */ 547681d9761SEric Taylor static int 548681d9761SEric Taylor zvol_remove_zv(zvol_state_t *zv) 549fa9e4066Sahrens { 550681d9761SEric Taylor char nmbuf[20]; 551fa9e4066Sahrens 552681d9761SEric Taylor ASSERT(MUTEX_HELD(&zvol_state_lock)); 553681d9761SEric Taylor if (zv->zv_total_opens != 0) 554fa9e4066Sahrens return (EBUSY); 555fa9e4066Sahrens 556681d9761SEric Taylor (void) snprintf(nmbuf, sizeof (nmbuf), "%u,raw", zv->zv_minor); 557681d9761SEric Taylor ddi_remove_minor_node(zfs_dip, nmbuf); 558fa9e4066Sahrens 559681d9761SEric Taylor (void) snprintf(nmbuf, sizeof (nmbuf), "%u", zv->zv_minor); 560681d9761SEric Taylor ddi_remove_minor_node(zfs_dip, nmbuf); 561fa9e4066Sahrens 562c2e6a7d6Sperrin avl_destroy(&zv->zv_znode.z_range_avl); 563c2e6a7d6Sperrin mutex_destroy(&zv->zv_znode.z_range_lock); 564fa9e4066Sahrens 565fa9e4066Sahrens ddi_soft_state_free(zvol_state, zv->zv_minor); 566fa9e4066Sahrens 567fa9e4066Sahrens zvol_minors--; 568fa9e4066Sahrens return (0); 569fa9e4066Sahrens } 570fa9e4066Sahrens 571e7cbe64fSgw25295 int 572681d9761SEric Taylor zvol_remove_minor(const char *name) 573681d9761SEric Taylor { 574681d9761SEric Taylor zvol_state_t *zv; 575681d9761SEric Taylor int rc; 576681d9761SEric Taylor 577681d9761SEric Taylor mutex_enter(&zvol_state_lock); 578681d9761SEric Taylor if ((zv = zvol_minor_lookup(name)) == NULL) { 579681d9761SEric Taylor mutex_exit(&zvol_state_lock); 580681d9761SEric Taylor return (ENXIO); 581681d9761SEric Taylor } 582681d9761SEric Taylor rc = zvol_remove_zv(zv); 583681d9761SEric Taylor mutex_exit(&zvol_state_lock); 584681d9761SEric Taylor return (rc); 585681d9761SEric Taylor } 586681d9761SEric Taylor 587681d9761SEric Taylor int 588681d9761SEric Taylor zvol_first_open(zvol_state_t *zv) 589681d9761SEric Taylor { 590681d9761SEric Taylor objset_t *os; 591681d9761SEric Taylor uint64_t volsize; 592681d9761SEric Taylor int error; 593681d9761SEric Taylor uint64_t readonly; 594681d9761SEric Taylor 595681d9761SEric Taylor /* lie and say we're read-only */ 596681d9761SEric Taylor error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, B_TRUE, 597681d9761SEric Taylor zvol_tag, &os); 598681d9761SEric Taylor if (error) 599681d9761SEric Taylor return (error); 600681d9761SEric Taylor 601681d9761SEric Taylor error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize); 602681d9761SEric Taylor if (error) { 603681d9761SEric Taylor ASSERT(error == 0); 604681d9761SEric Taylor dmu_objset_disown(os, zvol_tag); 605681d9761SEric Taylor return (error); 606681d9761SEric Taylor } 607681d9761SEric Taylor zv->zv_objset = os; 60894d1a210STim Haley error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf); 60994d1a210STim Haley if (error) { 61094d1a210STim Haley dmu_objset_disown(os, zvol_tag); 61194d1a210STim Haley return (error); 61294d1a210STim Haley } 613681d9761SEric Taylor zv->zv_volsize = volsize; 614681d9761SEric Taylor zv->zv_zilog = zil_open(os, zvol_get_data); 615681d9761SEric Taylor zvol_size_changed(zv->zv_volsize, ddi_driver_major(zfs_dip), 616681d9761SEric Taylor zv->zv_minor); 617681d9761SEric Taylor 618681d9761SEric Taylor VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &readonly, 619681d9761SEric Taylor NULL) == 0); 620681d9761SEric Taylor if (readonly || dmu_objset_is_snapshot(os)) 621681d9761SEric Taylor zv->zv_flags |= ZVOL_RDONLY; 622681d9761SEric Taylor else 623681d9761SEric Taylor zv->zv_flags &= ~ZVOL_RDONLY; 624681d9761SEric Taylor return (error); 625681d9761SEric Taylor } 626681d9761SEric Taylor 627681d9761SEric Taylor void 628681d9761SEric Taylor zvol_last_close(zvol_state_t *zv) 629681d9761SEric Taylor { 630681d9761SEric Taylor zil_close(zv->zv_zilog); 631681d9761SEric Taylor zv->zv_zilog = NULL; 63294d1a210STim Haley dmu_buf_rele(zv->zv_dbuf, zvol_tag); 63394d1a210STim Haley zv->zv_dbuf = NULL; 634681d9761SEric Taylor dmu_objset_disown(zv->zv_objset, zvol_tag); 635681d9761SEric Taylor zv->zv_objset = NULL; 636681d9761SEric Taylor } 637681d9761SEric Taylor 638681d9761SEric Taylor int 639e7cbe64fSgw25295 zvol_prealloc(zvol_state_t *zv) 640e7cbe64fSgw25295 { 641e7cbe64fSgw25295 objset_t *os = zv->zv_objset; 642e7cbe64fSgw25295 dmu_tx_t *tx; 643e7cbe64fSgw25295 uint64_t refd, avail, usedobjs, availobjs; 644e7cbe64fSgw25295 uint64_t resid = zv->zv_volsize; 645e7cbe64fSgw25295 uint64_t off = 0; 646e7cbe64fSgw25295 647e7cbe64fSgw25295 /* Check the space usage before attempting to allocate the space */ 648e7cbe64fSgw25295 dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs); 649e7cbe64fSgw25295 if (avail < zv->zv_volsize) 650e7cbe64fSgw25295 return (ENOSPC); 651e7cbe64fSgw25295 652e7cbe64fSgw25295 /* Free old extents if they exist */ 653e7cbe64fSgw25295 zvol_free_extents(zv); 654e7cbe64fSgw25295 655e7cbe64fSgw25295 while (resid != 0) { 656e7cbe64fSgw25295 int error; 657e7cbe64fSgw25295 uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE); 658e7cbe64fSgw25295 659e7cbe64fSgw25295 tx = dmu_tx_create(os); 660e7cbe64fSgw25295 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 661e7cbe64fSgw25295 error = dmu_tx_assign(tx, TXG_WAIT); 662e7cbe64fSgw25295 if (error) { 663e7cbe64fSgw25295 dmu_tx_abort(tx); 664cdb0ab79Smaybee (void) dmu_free_long_range(os, ZVOL_OBJ, 0, off); 665e7cbe64fSgw25295 return (error); 666e7cbe64fSgw25295 } 66782c9918fSTim Haley dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx); 668e7cbe64fSgw25295 dmu_tx_commit(tx); 669e7cbe64fSgw25295 off += bytes; 670e7cbe64fSgw25295 resid -= bytes; 671e7cbe64fSgw25295 } 672e7cbe64fSgw25295 txg_wait_synced(dmu_objset_pool(os), 0); 673e7cbe64fSgw25295 674e7cbe64fSgw25295 return (0); 675e7cbe64fSgw25295 } 676e7cbe64fSgw25295 677e7cbe64fSgw25295 int 678681d9761SEric Taylor zvol_update_volsize(objset_t *os, uint64_t volsize) 679e7cbe64fSgw25295 { 680e7cbe64fSgw25295 dmu_tx_t *tx; 681e7cbe64fSgw25295 int error; 682e7cbe64fSgw25295 683e7cbe64fSgw25295 ASSERT(MUTEX_HELD(&zvol_state_lock)); 684e7cbe64fSgw25295 685681d9761SEric Taylor tx = dmu_tx_create(os); 686e7cbe64fSgw25295 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 687e7cbe64fSgw25295 error = dmu_tx_assign(tx, TXG_WAIT); 688e7cbe64fSgw25295 if (error) { 689e7cbe64fSgw25295 dmu_tx_abort(tx); 690e7cbe64fSgw25295 return (error); 691e7cbe64fSgw25295 } 692e7cbe64fSgw25295 693681d9761SEric Taylor error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, 694e7cbe64fSgw25295 &volsize, tx); 695e7cbe64fSgw25295 dmu_tx_commit(tx); 696e7cbe64fSgw25295 697e7cbe64fSgw25295 if (error == 0) 698681d9761SEric Taylor error = dmu_free_long_range(os, 699cdb0ab79Smaybee ZVOL_OBJ, volsize, DMU_OBJECT_END); 700e7cbe64fSgw25295 return (error); 701e7cbe64fSgw25295 } 702e7cbe64fSgw25295 703681d9761SEric Taylor void 704681d9761SEric Taylor zvol_remove_minors(const char *name) 705681d9761SEric Taylor { 706681d9761SEric Taylor zvol_state_t *zv; 707681d9761SEric Taylor char *namebuf; 708681d9761SEric Taylor minor_t minor; 709681d9761SEric Taylor 710681d9761SEric Taylor namebuf = kmem_zalloc(strlen(name) + 2, KM_SLEEP); 711681d9761SEric Taylor (void) strncpy(namebuf, name, strlen(name)); 712681d9761SEric Taylor (void) strcat(namebuf, "/"); 713681d9761SEric Taylor mutex_enter(&zvol_state_lock); 714681d9761SEric Taylor for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) { 715681d9761SEric Taylor 716681d9761SEric Taylor zv = ddi_get_soft_state(zvol_state, minor); 717681d9761SEric Taylor if (zv == NULL) 718681d9761SEric Taylor continue; 719681d9761SEric Taylor if (strncmp(namebuf, zv->zv_name, strlen(namebuf)) == 0) 720681d9761SEric Taylor (void) zvol_remove_zv(zv); 721681d9761SEric Taylor } 722681d9761SEric Taylor kmem_free(namebuf, strlen(name) + 2); 723681d9761SEric Taylor 724681d9761SEric Taylor mutex_exit(&zvol_state_lock); 725681d9761SEric Taylor } 726681d9761SEric Taylor 727fa9e4066Sahrens int 72891ebeef5Sahrens zvol_set_volsize(const char *name, major_t maj, uint64_t volsize) 729fa9e4066Sahrens { 730681d9761SEric Taylor zvol_state_t *zv = NULL; 731681d9761SEric Taylor objset_t *os; 732fa9e4066Sahrens int error; 7335c5460e9Seschrock dmu_object_info_t doi; 734e7cbe64fSgw25295 uint64_t old_volsize = 0ULL; 735681d9761SEric Taylor uint64_t readonly; 736fa9e4066Sahrens 737fa9e4066Sahrens mutex_enter(&zvol_state_lock); 738681d9761SEric Taylor zv = zvol_minor_lookup(name); 739681d9761SEric Taylor if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) { 740681d9761SEric Taylor mutex_exit(&zvol_state_lock); 741681d9761SEric Taylor return (error); 742fa9e4066Sahrens } 743fa9e4066Sahrens 744681d9761SEric Taylor if ((error = dmu_object_info(os, ZVOL_OBJ, &doi)) != 0 || 745e9dbad6fSeschrock (error = zvol_check_volsize(volsize, 746bb0ade09Sahrens doi.doi_data_block_size)) != 0) 747bb0ade09Sahrens goto out; 7485c5460e9Seschrock 749681d9761SEric Taylor VERIFY(dsl_prop_get_integer(name, "readonly", &readonly, 750681d9761SEric Taylor NULL) == 0); 751681d9761SEric Taylor if (readonly) { 752bb0ade09Sahrens error = EROFS; 753bb0ade09Sahrens goto out; 754fa9e4066Sahrens } 755fa9e4066Sahrens 756681d9761SEric Taylor error = zvol_update_volsize(os, volsize); 757e7cbe64fSgw25295 /* 758e7cbe64fSgw25295 * Reinitialize the dump area to the new size. If we 759681d9761SEric Taylor * failed to resize the dump area then restore it back to 760681d9761SEric Taylor * its original size. 761e7cbe64fSgw25295 */ 762681d9761SEric Taylor if (zv && error == 0) { 763681d9761SEric Taylor if (zv->zv_flags & ZVOL_DUMPIFIED) { 764681d9761SEric Taylor old_volsize = zv->zv_volsize; 765681d9761SEric Taylor zv->zv_volsize = volsize; 766e7cbe64fSgw25295 if ((error = zvol_dumpify(zv)) != 0 || 767e7cbe64fSgw25295 (error = dumpvp_resize()) != 0) { 768681d9761SEric Taylor (void) zvol_update_volsize(os, old_volsize); 769681d9761SEric Taylor zv->zv_volsize = old_volsize; 770e7cbe64fSgw25295 error = zvol_dumpify(zv); 771fa9e4066Sahrens } 772fa9e4066Sahrens } 773681d9761SEric Taylor if (error == 0) { 774681d9761SEric Taylor zv->zv_volsize = volsize; 775681d9761SEric Taylor zvol_size_changed(volsize, maj, zv->zv_minor); 776681d9761SEric Taylor } 777681d9761SEric Taylor } 778fa9e4066Sahrens 779573ca77eSGeorge Wilson /* 780573ca77eSGeorge Wilson * Generate a LUN expansion event. 781573ca77eSGeorge Wilson */ 782681d9761SEric Taylor if (zv && error == 0) { 783573ca77eSGeorge Wilson sysevent_id_t eid; 784573ca77eSGeorge Wilson nvlist_t *attr; 785573ca77eSGeorge Wilson char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 786573ca77eSGeorge Wilson 787681d9761SEric Taylor (void) snprintf(physpath, MAXPATHLEN, "%s%u", ZVOL_PSEUDO_DEV, 788573ca77eSGeorge Wilson zv->zv_minor); 789573ca77eSGeorge Wilson 790573ca77eSGeorge Wilson VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 791573ca77eSGeorge Wilson VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 792573ca77eSGeorge Wilson 793573ca77eSGeorge Wilson (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 794573ca77eSGeorge Wilson ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 795573ca77eSGeorge Wilson 796573ca77eSGeorge Wilson nvlist_free(attr); 797573ca77eSGeorge Wilson kmem_free(physpath, MAXPATHLEN); 798573ca77eSGeorge Wilson } 799573ca77eSGeorge Wilson 800bb0ade09Sahrens out: 801681d9761SEric Taylor dmu_objset_rele(os, FTAG); 802bb0ade09Sahrens 803fa9e4066Sahrens mutex_exit(&zvol_state_lock); 804fa9e4066Sahrens 805fa9e4066Sahrens return (error); 806fa9e4066Sahrens } 807fa9e4066Sahrens 808fa9e4066Sahrens /*ARGSUSED*/ 809fa9e4066Sahrens int 810fa9e4066Sahrens zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr) 811fa9e4066Sahrens { 812fa9e4066Sahrens minor_t minor = getminor(*devp); 813fa9e4066Sahrens zvol_state_t *zv; 814681d9761SEric Taylor int err = 0; 815fa9e4066Sahrens 816fa9e4066Sahrens if (minor == 0) /* This is the control device */ 817fa9e4066Sahrens return (0); 818fa9e4066Sahrens 819fa9e4066Sahrens mutex_enter(&zvol_state_lock); 820fa9e4066Sahrens 821fa9e4066Sahrens zv = ddi_get_soft_state(zvol_state, minor); 822fa9e4066Sahrens if (zv == NULL) { 823fa9e4066Sahrens mutex_exit(&zvol_state_lock); 824fa9e4066Sahrens return (ENXIO); 825fa9e4066Sahrens } 826fa9e4066Sahrens 827681d9761SEric Taylor if (zv->zv_total_opens == 0) 828681d9761SEric Taylor err = zvol_first_open(zv); 829681d9761SEric Taylor if (err) { 830fa9e4066Sahrens mutex_exit(&zvol_state_lock); 831681d9761SEric Taylor return (err); 832681d9761SEric Taylor } 833681d9761SEric Taylor if ((flag & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) { 834681d9761SEric Taylor err = EROFS; 835681d9761SEric Taylor goto out; 836fa9e4066Sahrens } 837c7f714e2SEric Taylor if (zv->zv_flags & ZVOL_EXCL) { 838681d9761SEric Taylor err = EBUSY; 839681d9761SEric Taylor goto out; 840c7f714e2SEric Taylor } 841c7f714e2SEric Taylor if (flag & FEXCL) { 842c7f714e2SEric Taylor if (zv->zv_total_opens != 0) { 843681d9761SEric Taylor err = EBUSY; 844681d9761SEric Taylor goto out; 845c7f714e2SEric Taylor } 846c7f714e2SEric Taylor zv->zv_flags |= ZVOL_EXCL; 847c7f714e2SEric Taylor } 848fa9e4066Sahrens 849fa9e4066Sahrens if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) { 850fa9e4066Sahrens zv->zv_open_count[otyp]++; 851fa9e4066Sahrens zv->zv_total_opens++; 852fa9e4066Sahrens } 853fa9e4066Sahrens mutex_exit(&zvol_state_lock); 854fa9e4066Sahrens 855681d9761SEric Taylor return (err); 856681d9761SEric Taylor out: 857681d9761SEric Taylor if (zv->zv_total_opens == 0) 858681d9761SEric Taylor zvol_last_close(zv); 859681d9761SEric Taylor mutex_exit(&zvol_state_lock); 860681d9761SEric Taylor return (err); 861fa9e4066Sahrens } 862fa9e4066Sahrens 863fa9e4066Sahrens /*ARGSUSED*/ 864fa9e4066Sahrens int 865fa9e4066Sahrens zvol_close(dev_t dev, int flag, int otyp, cred_t *cr) 866fa9e4066Sahrens { 867fa9e4066Sahrens minor_t minor = getminor(dev); 868fa9e4066Sahrens zvol_state_t *zv; 869681d9761SEric Taylor int error = 0; 870fa9e4066Sahrens 871fa9e4066Sahrens if (minor == 0) /* This is the control device */ 872fa9e4066Sahrens return (0); 873fa9e4066Sahrens 874fa9e4066Sahrens mutex_enter(&zvol_state_lock); 875fa9e4066Sahrens 876fa9e4066Sahrens zv = ddi_get_soft_state(zvol_state, minor); 877fa9e4066Sahrens if (zv == NULL) { 878fa9e4066Sahrens mutex_exit(&zvol_state_lock); 879fa9e4066Sahrens return (ENXIO); 880fa9e4066Sahrens } 881fa9e4066Sahrens 882c7f714e2SEric Taylor if (zv->zv_flags & ZVOL_EXCL) { 883c7f714e2SEric Taylor ASSERT(zv->zv_total_opens == 1); 884c7f714e2SEric Taylor zv->zv_flags &= ~ZVOL_EXCL; 885fa9e4066Sahrens } 886fa9e4066Sahrens 887fa9e4066Sahrens /* 888fa9e4066Sahrens * If the open count is zero, this is a spurious close. 889fa9e4066Sahrens * That indicates a bug in the kernel / DDI framework. 890fa9e4066Sahrens */ 891fa9e4066Sahrens ASSERT(zv->zv_open_count[otyp] != 0); 892fa9e4066Sahrens ASSERT(zv->zv_total_opens != 0); 893fa9e4066Sahrens 894fa9e4066Sahrens /* 895fa9e4066Sahrens * You may get multiple opens, but only one close. 896fa9e4066Sahrens */ 897fa9e4066Sahrens zv->zv_open_count[otyp]--; 898fa9e4066Sahrens zv->zv_total_opens--; 899fa9e4066Sahrens 900681d9761SEric Taylor if (zv->zv_total_opens == 0) 901681d9761SEric Taylor zvol_last_close(zv); 902fa9e4066Sahrens 903681d9761SEric Taylor mutex_exit(&zvol_state_lock); 904681d9761SEric Taylor return (error); 905fa9e4066Sahrens } 906fa9e4066Sahrens 907feb08c6bSbillm static void 908b24ab676SJeff Bonwick zvol_get_done(zgd_t *zgd, int error) 90967bd71c6Sperrin { 910b24ab676SJeff Bonwick if (zgd->zgd_db) 911b24ab676SJeff Bonwick dmu_buf_rele(zgd->zgd_db, zgd); 91267bd71c6Sperrin 913b24ab676SJeff Bonwick zfs_range_unlock(zgd->zgd_rl); 914b24ab676SJeff Bonwick 915b24ab676SJeff Bonwick if (error == 0 && zgd->zgd_bp) 91617f17c2dSbonwick zil_add_block(zgd->zgd_zilog, zgd->zgd_bp); 917b24ab676SJeff Bonwick 91867bd71c6Sperrin kmem_free(zgd, sizeof (zgd_t)); 91967bd71c6Sperrin } 92067bd71c6Sperrin 92167bd71c6Sperrin /* 92267bd71c6Sperrin * Get data to generate a TX_WRITE intent log record. 92367bd71c6Sperrin */ 924feb08c6bSbillm static int 92567bd71c6Sperrin zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 92667bd71c6Sperrin { 92767bd71c6Sperrin zvol_state_t *zv = arg; 92867bd71c6Sperrin objset_t *os = zv->zv_objset; 929b24ab676SJeff Bonwick uint64_t object = ZVOL_OBJ; 930b24ab676SJeff Bonwick uint64_t offset = lr->lr_offset; 931b24ab676SJeff Bonwick uint64_t size = lr->lr_length; /* length of user data */ 932b24ab676SJeff Bonwick blkptr_t *bp = &lr->lr_blkptr; 93367bd71c6Sperrin dmu_buf_t *db; 93467bd71c6Sperrin zgd_t *zgd; 93567bd71c6Sperrin int error; 93667bd71c6Sperrin 937b24ab676SJeff Bonwick ASSERT(zio != NULL); 938b24ab676SJeff Bonwick ASSERT(size != 0); 939b24ab676SJeff Bonwick 940b24ab676SJeff Bonwick zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP); 941b24ab676SJeff Bonwick zgd->zgd_zilog = zv->zv_zilog; 942b24ab676SJeff Bonwick zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER); 943feb08c6bSbillm 944c2e6a7d6Sperrin /* 945c2e6a7d6Sperrin * Write records come in two flavors: immediate and indirect. 946c2e6a7d6Sperrin * For small writes it's cheaper to store the data with the 947c2e6a7d6Sperrin * log record (immediate); for large writes it's cheaper to 948c2e6a7d6Sperrin * sync the data and get a pointer to it (indirect) so that 949c2e6a7d6Sperrin * we don't have to write the data twice. 950c2e6a7d6Sperrin */ 951b24ab676SJeff Bonwick if (buf != NULL) { /* immediate write */ 952b24ab676SJeff Bonwick error = dmu_read(os, object, offset, size, buf, 953b24ab676SJeff Bonwick DMU_READ_NO_PREFETCH); 954b24ab676SJeff Bonwick } else { 955b24ab676SJeff Bonwick size = zv->zv_volblocksize; 956b24ab676SJeff Bonwick offset = P2ALIGN(offset, size); 95747cb52daSJeff Bonwick error = dmu_buf_hold(os, object, offset, zgd, &db, 95847cb52daSJeff Bonwick DMU_READ_NO_PREFETCH); 959975c32a0SNeil Perrin if (error == 0) { 960b24ab676SJeff Bonwick zgd->zgd_db = db; 961b24ab676SJeff Bonwick zgd->zgd_bp = bp; 962b24ab676SJeff Bonwick 963b24ab676SJeff Bonwick ASSERT(db->db_offset == offset); 964b24ab676SJeff Bonwick ASSERT(db->db_size == size); 965b24ab676SJeff Bonwick 966b24ab676SJeff Bonwick error = dmu_sync(zio, lr->lr_common.lrc_txg, 967b24ab676SJeff Bonwick zvol_get_done, zgd); 968b24ab676SJeff Bonwick 969b24ab676SJeff Bonwick if (error == 0) 970b24ab676SJeff Bonwick return (0); 971b24ab676SJeff Bonwick } 972975c32a0SNeil Perrin } 973975c32a0SNeil Perrin 974b24ab676SJeff Bonwick zvol_get_done(zgd, error); 975b24ab676SJeff Bonwick 97667bd71c6Sperrin return (error); 97767bd71c6Sperrin } 97867bd71c6Sperrin 979a24e15ceSperrin /* 980a24e15ceSperrin * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions. 98122ac5be4Sperrin * 98222ac5be4Sperrin * We store data in the log buffers if it's small enough. 98367bd71c6Sperrin * Otherwise we will later flush the data out via dmu_sync(). 98422ac5be4Sperrin */ 98567bd71c6Sperrin ssize_t zvol_immediate_write_sz = 32768; 98622ac5be4Sperrin 987feb08c6bSbillm static void 988510b6c0eSNeil Perrin zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid, 989510b6c0eSNeil Perrin boolean_t sync) 99022ac5be4Sperrin { 991feb08c6bSbillm uint32_t blocksize = zv->zv_volblocksize; 9921209a471SNeil Perrin zilog_t *zilog = zv->zv_zilog; 993510b6c0eSNeil Perrin boolean_t slogging; 994e09fa4daSNeil Perrin ssize_t immediate_write_sz; 995510b6c0eSNeil Perrin 996b24ab676SJeff Bonwick if (zil_replaying(zilog, tx)) 9971209a471SNeil Perrin return; 9981209a471SNeil Perrin 999e09fa4daSNeil Perrin immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT) 1000e09fa4daSNeil Perrin ? 0 : zvol_immediate_write_sz; 1001e09fa4daSNeil Perrin 1002e09fa4daSNeil Perrin slogging = spa_has_slogs(zilog->zl_spa) && 1003e09fa4daSNeil Perrin (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY); 1004feb08c6bSbillm 1005510b6c0eSNeil Perrin while (resid) { 1006510b6c0eSNeil Perrin itx_t *itx; 1007510b6c0eSNeil Perrin lr_write_t *lr; 1008510b6c0eSNeil Perrin ssize_t len; 1009510b6c0eSNeil Perrin itx_wr_state_t write_state; 1010510b6c0eSNeil Perrin 1011510b6c0eSNeil Perrin /* 1012510b6c0eSNeil Perrin * Unlike zfs_log_write() we can be called with 1013510b6c0eSNeil Perrin * upto DMU_MAX_ACCESS/2 (5MB) writes. 1014510b6c0eSNeil Perrin */ 1015e09fa4daSNeil Perrin if (blocksize > immediate_write_sz && !slogging && 1016510b6c0eSNeil Perrin resid >= blocksize && off % blocksize == 0) { 1017510b6c0eSNeil Perrin write_state = WR_INDIRECT; /* uses dmu_sync */ 1018510b6c0eSNeil Perrin len = blocksize; 1019510b6c0eSNeil Perrin } else if (sync) { 1020510b6c0eSNeil Perrin write_state = WR_COPIED; 1021510b6c0eSNeil Perrin len = MIN(ZIL_MAX_LOG_DATA, resid); 1022510b6c0eSNeil Perrin } else { 1023510b6c0eSNeil Perrin write_state = WR_NEED_COPY; 1024510b6c0eSNeil Perrin len = MIN(ZIL_MAX_LOG_DATA, resid); 1025510b6c0eSNeil Perrin } 1026510b6c0eSNeil Perrin 1027510b6c0eSNeil Perrin itx = zil_itx_create(TX_WRITE, sizeof (*lr) + 1028510b6c0eSNeil Perrin (write_state == WR_COPIED ? len : 0)); 102922ac5be4Sperrin lr = (lr_write_t *)&itx->itx_lr; 1030510b6c0eSNeil Perrin if (write_state == WR_COPIED && dmu_read(zv->zv_objset, 10317bfdf011SNeil Perrin ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) { 1032b24ab676SJeff Bonwick zil_itx_destroy(itx); 1033510b6c0eSNeil Perrin itx = zil_itx_create(TX_WRITE, sizeof (*lr)); 1034510b6c0eSNeil Perrin lr = (lr_write_t *)&itx->itx_lr; 1035510b6c0eSNeil Perrin write_state = WR_NEED_COPY; 1036510b6c0eSNeil Perrin } 1037510b6c0eSNeil Perrin 1038510b6c0eSNeil Perrin itx->itx_wr_state = write_state; 1039510b6c0eSNeil Perrin if (write_state == WR_NEED_COPY) 1040510b6c0eSNeil Perrin itx->itx_sod += len; 104122ac5be4Sperrin lr->lr_foid = ZVOL_OBJ; 104222ac5be4Sperrin lr->lr_offset = off; 1043510b6c0eSNeil Perrin lr->lr_length = len; 1044b24ab676SJeff Bonwick lr->lr_blkoff = 0; 104522ac5be4Sperrin BP_ZERO(&lr->lr_blkptr); 1046feb08c6bSbillm 1047510b6c0eSNeil Perrin itx->itx_private = zv; 1048510b6c0eSNeil Perrin itx->itx_sync = sync; 1049510b6c0eSNeil Perrin 10501209a471SNeil Perrin (void) zil_itx_assign(zilog, itx, tx); 1051510b6c0eSNeil Perrin 1052510b6c0eSNeil Perrin off += len; 1053510b6c0eSNeil Perrin resid -= len; 1054a24e15ceSperrin } 105522ac5be4Sperrin } 105622ac5be4Sperrin 105788b7b0f2SMatthew Ahrens static int 105888b7b0f2SMatthew Ahrens zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t size, 105988b7b0f2SMatthew Ahrens boolean_t doread, boolean_t isdump) 1060e7cbe64fSgw25295 { 1061e7cbe64fSgw25295 vdev_disk_t *dvd; 1062e7cbe64fSgw25295 int c; 1063e7cbe64fSgw25295 int numerrors = 0; 1064e7cbe64fSgw25295 1065e7cbe64fSgw25295 for (c = 0; c < vd->vdev_children; c++) { 106621ecdf64SLin Ling ASSERT(vd->vdev_ops == &vdev_mirror_ops || 106721ecdf64SLin Ling vd->vdev_ops == &vdev_replacing_ops || 106821ecdf64SLin Ling vd->vdev_ops == &vdev_spare_ops); 106988b7b0f2SMatthew Ahrens int err = zvol_dumpio_vdev(vd->vdev_child[c], 107088b7b0f2SMatthew Ahrens addr, offset, size, doread, isdump); 107188b7b0f2SMatthew Ahrens if (err != 0) { 1072e7cbe64fSgw25295 numerrors++; 107388b7b0f2SMatthew Ahrens } else if (doread) { 1074e7cbe64fSgw25295 break; 1075e7cbe64fSgw25295 } 1076e7cbe64fSgw25295 } 1077e7cbe64fSgw25295 1078e7cbe64fSgw25295 if (!vd->vdev_ops->vdev_op_leaf) 1079e7cbe64fSgw25295 return (numerrors < vd->vdev_children ? 0 : EIO); 1080e7cbe64fSgw25295 1081dc0bb255SEric Taylor if (doread && !vdev_readable(vd)) 1082dc0bb255SEric Taylor return (EIO); 1083dc0bb255SEric Taylor else if (!doread && !vdev_writeable(vd)) 1084e7cbe64fSgw25295 return (EIO); 1085e7cbe64fSgw25295 1086e7cbe64fSgw25295 dvd = vd->vdev_tsd; 1087e7cbe64fSgw25295 ASSERT3P(dvd, !=, NULL); 1088e7cbe64fSgw25295 offset += VDEV_LABEL_START_SIZE; 1089e7cbe64fSgw25295 1090e7cbe64fSgw25295 if (ddi_in_panic() || isdump) { 109188b7b0f2SMatthew Ahrens ASSERT(!doread); 109288b7b0f2SMatthew Ahrens if (doread) 1093e7cbe64fSgw25295 return (EIO); 1094e7cbe64fSgw25295 return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset), 1095e7cbe64fSgw25295 lbtodb(size))); 1096e7cbe64fSgw25295 } else { 1097e7cbe64fSgw25295 return (vdev_disk_physio(dvd->vd_lh, addr, size, offset, 109888b7b0f2SMatthew Ahrens doread ? B_READ : B_WRITE)); 1099e7cbe64fSgw25295 } 1100e7cbe64fSgw25295 } 1101e7cbe64fSgw25295 110288b7b0f2SMatthew Ahrens static int 110388b7b0f2SMatthew Ahrens zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size, 110488b7b0f2SMatthew Ahrens boolean_t doread, boolean_t isdump) 1105e7cbe64fSgw25295 { 1106e7cbe64fSgw25295 vdev_t *vd; 1107e7cbe64fSgw25295 int error; 110888b7b0f2SMatthew Ahrens zvol_extent_t *ze; 1109e7cbe64fSgw25295 spa_t *spa = dmu_objset_spa(zv->zv_objset); 1110e7cbe64fSgw25295 111188b7b0f2SMatthew Ahrens /* Must be sector aligned, and not stradle a block boundary. */ 111288b7b0f2SMatthew Ahrens if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) || 111388b7b0f2SMatthew Ahrens P2BOUNDARY(offset, size, zv->zv_volblocksize)) { 111488b7b0f2SMatthew Ahrens return (EINVAL); 111588b7b0f2SMatthew Ahrens } 1116e7cbe64fSgw25295 ASSERT(size <= zv->zv_volblocksize); 1117e7cbe64fSgw25295 111888b7b0f2SMatthew Ahrens /* Locate the extent this belongs to */ 111988b7b0f2SMatthew Ahrens ze = list_head(&zv->zv_extents); 112088b7b0f2SMatthew Ahrens while (offset >= ze->ze_nblks * zv->zv_volblocksize) { 112188b7b0f2SMatthew Ahrens offset -= ze->ze_nblks * zv->zv_volblocksize; 112288b7b0f2SMatthew Ahrens ze = list_next(&zv->zv_extents, ze); 112388b7b0f2SMatthew Ahrens } 112424cc0e1cSGeorge Wilson 112524cc0e1cSGeorge Wilson if (!ddi_in_panic()) 1126e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 112724cc0e1cSGeorge Wilson 112888b7b0f2SMatthew Ahrens vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva)); 112988b7b0f2SMatthew Ahrens offset += DVA_GET_OFFSET(&ze->ze_dva); 113088b7b0f2SMatthew Ahrens error = zvol_dumpio_vdev(vd, addr, offset, size, doread, isdump); 113124cc0e1cSGeorge Wilson 113224cc0e1cSGeorge Wilson if (!ddi_in_panic()) 1133e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 113424cc0e1cSGeorge Wilson 1135e7cbe64fSgw25295 return (error); 1136e7cbe64fSgw25295 } 1137e7cbe64fSgw25295 1138e7cbe64fSgw25295 int 1139fa9e4066Sahrens zvol_strategy(buf_t *bp) 1140fa9e4066Sahrens { 1141fa9e4066Sahrens zvol_state_t *zv = ddi_get_soft_state(zvol_state, getminor(bp->b_edev)); 1142fa9e4066Sahrens uint64_t off, volsize; 114388b7b0f2SMatthew Ahrens size_t resid; 1144fa9e4066Sahrens char *addr; 114522ac5be4Sperrin objset_t *os; 1146c2e6a7d6Sperrin rl_t *rl; 1147fa9e4066Sahrens int error = 0; 114888b7b0f2SMatthew Ahrens boolean_t doread = bp->b_flags & B_READ; 1149f80ce222SChris Kirby boolean_t is_dump; 1150510b6c0eSNeil Perrin boolean_t sync; 1151fa9e4066Sahrens 1152fa9e4066Sahrens if (zv == NULL) { 1153fa9e4066Sahrens bioerror(bp, ENXIO); 1154fa9e4066Sahrens biodone(bp); 1155fa9e4066Sahrens return (0); 1156fa9e4066Sahrens } 1157fa9e4066Sahrens 1158fa9e4066Sahrens if (getminor(bp->b_edev) == 0) { 1159fa9e4066Sahrens bioerror(bp, EINVAL); 1160fa9e4066Sahrens biodone(bp); 1161fa9e4066Sahrens return (0); 1162fa9e4066Sahrens } 1163fa9e4066Sahrens 1164681d9761SEric Taylor if (!(bp->b_flags & B_READ) && (zv->zv_flags & ZVOL_RDONLY)) { 1165fa9e4066Sahrens bioerror(bp, EROFS); 1166fa9e4066Sahrens biodone(bp); 1167fa9e4066Sahrens return (0); 1168fa9e4066Sahrens } 1169fa9e4066Sahrens 1170fa9e4066Sahrens off = ldbtob(bp->b_blkno); 1171fa9e4066Sahrens volsize = zv->zv_volsize; 1172fa9e4066Sahrens 117322ac5be4Sperrin os = zv->zv_objset; 117422ac5be4Sperrin ASSERT(os != NULL); 1175fa9e4066Sahrens 1176fa9e4066Sahrens bp_mapin(bp); 1177fa9e4066Sahrens addr = bp->b_un.b_addr; 1178fa9e4066Sahrens resid = bp->b_bcount; 1179fa9e4066Sahrens 118088b7b0f2SMatthew Ahrens if (resid > 0 && (off < 0 || off >= volsize)) { 118188b7b0f2SMatthew Ahrens bioerror(bp, EIO); 118288b7b0f2SMatthew Ahrens biodone(bp); 118388b7b0f2SMatthew Ahrens return (0); 118488b7b0f2SMatthew Ahrens } 118573ec3d9cSgw25295 1186f80ce222SChris Kirby is_dump = zv->zv_flags & ZVOL_DUMPIFIED; 1187*55da60b9SMark J Musante sync = ((!(bp->b_flags & B_ASYNC) && 1188*55da60b9SMark J Musante !(zv->zv_flags & ZVOL_WCE)) || 1189*55da60b9SMark J Musante (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)) && 1190*55da60b9SMark J Musante !doread && !is_dump; 1191510b6c0eSNeil Perrin 1192a24e15ceSperrin /* 1193a24e15ceSperrin * There must be no buffer changes when doing a dmu_sync() because 1194a24e15ceSperrin * we can't change the data whilst calculating the checksum. 1195a24e15ceSperrin */ 1196c2e6a7d6Sperrin rl = zfs_range_lock(&zv->zv_znode, off, resid, 119788b7b0f2SMatthew Ahrens doread ? RL_READER : RL_WRITER); 1198e7cbe64fSgw25295 1199fa9e4066Sahrens while (resid != 0 && off < volsize) { 120088b7b0f2SMatthew Ahrens size_t size = MIN(resid, zvol_maxphys); 1201e7cbe64fSgw25295 if (is_dump) { 1202e7cbe64fSgw25295 size = MIN(size, P2END(off, zv->zv_volblocksize) - off); 120388b7b0f2SMatthew Ahrens error = zvol_dumpio(zv, addr, off, size, 120488b7b0f2SMatthew Ahrens doread, B_FALSE); 120588b7b0f2SMatthew Ahrens } else if (doread) { 12067bfdf011SNeil Perrin error = dmu_read(os, ZVOL_OBJ, off, size, addr, 12077bfdf011SNeil Perrin DMU_READ_PREFETCH); 1208fa9e4066Sahrens } else { 120922ac5be4Sperrin dmu_tx_t *tx = dmu_tx_create(os); 1210fa9e4066Sahrens dmu_tx_hold_write(tx, ZVOL_OBJ, off, size); 1211fa9e4066Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 1212fa9e4066Sahrens if (error) { 1213fa9e4066Sahrens dmu_tx_abort(tx); 1214fa9e4066Sahrens } else { 121522ac5be4Sperrin dmu_write(os, ZVOL_OBJ, off, size, addr, tx); 1216510b6c0eSNeil Perrin zvol_log_write(zv, tx, off, size, sync); 1217fa9e4066Sahrens dmu_tx_commit(tx); 1218fa9e4066Sahrens } 1219fa9e4066Sahrens } 1220b87f3af3Sperrin if (error) { 1221b87f3af3Sperrin /* convert checksum errors into IO errors */ 1222b87f3af3Sperrin if (error == ECKSUM) 1223b87f3af3Sperrin error = EIO; 1224fa9e4066Sahrens break; 1225b87f3af3Sperrin } 1226fa9e4066Sahrens off += size; 1227fa9e4066Sahrens addr += size; 1228fa9e4066Sahrens resid -= size; 1229fa9e4066Sahrens } 1230c2e6a7d6Sperrin zfs_range_unlock(rl); 1231fa9e4066Sahrens 1232fa9e4066Sahrens if ((bp->b_resid = resid) == bp->b_bcount) 1233fa9e4066Sahrens bioerror(bp, off > volsize ? EINVAL : error); 1234fa9e4066Sahrens 1235510b6c0eSNeil Perrin if (sync) 1236feb08c6bSbillm zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ); 1237feb08c6bSbillm biodone(bp); 123822ac5be4Sperrin 1239fa9e4066Sahrens return (0); 1240fa9e4066Sahrens } 1241fa9e4066Sahrens 124267bd71c6Sperrin /* 124367bd71c6Sperrin * Set the buffer count to the zvol maximum transfer. 124467bd71c6Sperrin * Using our own routine instead of the default minphys() 124567bd71c6Sperrin * means that for larger writes we write bigger buffers on X86 124667bd71c6Sperrin * (128K instead of 56K) and flush the disk write cache less often 124767bd71c6Sperrin * (every zvol_maxphys - currently 1MB) instead of minphys (currently 124867bd71c6Sperrin * 56K on X86 and 128K on sparc). 124967bd71c6Sperrin */ 125067bd71c6Sperrin void 125167bd71c6Sperrin zvol_minphys(struct buf *bp) 125267bd71c6Sperrin { 125367bd71c6Sperrin if (bp->b_bcount > zvol_maxphys) 125467bd71c6Sperrin bp->b_bcount = zvol_maxphys; 125567bd71c6Sperrin } 125667bd71c6Sperrin 1257e7cbe64fSgw25295 int 1258e7cbe64fSgw25295 zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks) 1259e7cbe64fSgw25295 { 1260e7cbe64fSgw25295 minor_t minor = getminor(dev); 1261e7cbe64fSgw25295 zvol_state_t *zv; 1262e7cbe64fSgw25295 int error = 0; 1263e7cbe64fSgw25295 uint64_t size; 1264e7cbe64fSgw25295 uint64_t boff; 1265e7cbe64fSgw25295 uint64_t resid; 1266e7cbe64fSgw25295 1267e7cbe64fSgw25295 if (minor == 0) /* This is the control device */ 1268e7cbe64fSgw25295 return (ENXIO); 1269e7cbe64fSgw25295 1270e7cbe64fSgw25295 zv = ddi_get_soft_state(zvol_state, minor); 1271e7cbe64fSgw25295 if (zv == NULL) 1272e7cbe64fSgw25295 return (ENXIO); 1273e7cbe64fSgw25295 1274e7cbe64fSgw25295 boff = ldbtob(blkno); 1275e7cbe64fSgw25295 resid = ldbtob(nblocks); 1276e7cbe64fSgw25295 127788b7b0f2SMatthew Ahrens VERIFY3U(boff + resid, <=, zv->zv_volsize); 127888b7b0f2SMatthew Ahrens 127988b7b0f2SMatthew Ahrens while (resid) { 128088b7b0f2SMatthew Ahrens size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff); 128188b7b0f2SMatthew Ahrens error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE); 1282e7cbe64fSgw25295 if (error) 1283e7cbe64fSgw25295 break; 1284e7cbe64fSgw25295 boff += size; 1285e7cbe64fSgw25295 addr += size; 1286e7cbe64fSgw25295 resid -= size; 1287e7cbe64fSgw25295 } 1288e7cbe64fSgw25295 1289e7cbe64fSgw25295 return (error); 1290e7cbe64fSgw25295 } 1291e7cbe64fSgw25295 1292fa9e4066Sahrens /*ARGSUSED*/ 1293fa9e4066Sahrens int 1294feb08c6bSbillm zvol_read(dev_t dev, uio_t *uio, cred_t *cr) 1295fa9e4066Sahrens { 1296c7ca1008Sgw25295 minor_t minor = getminor(dev); 1297c7ca1008Sgw25295 zvol_state_t *zv; 129873ec3d9cSgw25295 uint64_t volsize; 1299c2e6a7d6Sperrin rl_t *rl; 1300feb08c6bSbillm int error = 0; 1301feb08c6bSbillm 1302c7ca1008Sgw25295 if (minor == 0) /* This is the control device */ 1303c7ca1008Sgw25295 return (ENXIO); 1304c7ca1008Sgw25295 1305c7ca1008Sgw25295 zv = ddi_get_soft_state(zvol_state, minor); 1306c7ca1008Sgw25295 if (zv == NULL) 1307c7ca1008Sgw25295 return (ENXIO); 1308c7ca1008Sgw25295 130973ec3d9cSgw25295 volsize = zv->zv_volsize; 131073ec3d9cSgw25295 if (uio->uio_resid > 0 && 131173ec3d9cSgw25295 (uio->uio_loffset < 0 || uio->uio_loffset >= volsize)) 131273ec3d9cSgw25295 return (EIO); 131373ec3d9cSgw25295 131488b7b0f2SMatthew Ahrens if (zv->zv_flags & ZVOL_DUMPIFIED) { 131588b7b0f2SMatthew Ahrens error = physio(zvol_strategy, NULL, dev, B_READ, 131688b7b0f2SMatthew Ahrens zvol_minphys, uio); 131788b7b0f2SMatthew Ahrens return (error); 131888b7b0f2SMatthew Ahrens } 131988b7b0f2SMatthew Ahrens 1320c2e6a7d6Sperrin rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 1321c2e6a7d6Sperrin RL_READER); 132273ec3d9cSgw25295 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 1323feb08c6bSbillm uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 1324feb08c6bSbillm 132573ec3d9cSgw25295 /* don't read past the end */ 132673ec3d9cSgw25295 if (bytes > volsize - uio->uio_loffset) 132773ec3d9cSgw25295 bytes = volsize - uio->uio_loffset; 132873ec3d9cSgw25295 1329feb08c6bSbillm error = dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes); 1330b87f3af3Sperrin if (error) { 1331b87f3af3Sperrin /* convert checksum errors into IO errors */ 1332b87f3af3Sperrin if (error == ECKSUM) 1333b87f3af3Sperrin error = EIO; 1334feb08c6bSbillm break; 1335feb08c6bSbillm } 1336b87f3af3Sperrin } 1337c2e6a7d6Sperrin zfs_range_unlock(rl); 1338feb08c6bSbillm return (error); 1339fa9e4066Sahrens } 1340fa9e4066Sahrens 1341fa9e4066Sahrens /*ARGSUSED*/ 1342fa9e4066Sahrens int 1343feb08c6bSbillm zvol_write(dev_t dev, uio_t *uio, cred_t *cr) 1344fa9e4066Sahrens { 1345c7ca1008Sgw25295 minor_t minor = getminor(dev); 1346c7ca1008Sgw25295 zvol_state_t *zv; 134773ec3d9cSgw25295 uint64_t volsize; 1348c2e6a7d6Sperrin rl_t *rl; 1349feb08c6bSbillm int error = 0; 1350510b6c0eSNeil Perrin boolean_t sync; 1351fa9e4066Sahrens 1352c7ca1008Sgw25295 if (minor == 0) /* This is the control device */ 1353c7ca1008Sgw25295 return (ENXIO); 1354c7ca1008Sgw25295 1355c7ca1008Sgw25295 zv = ddi_get_soft_state(zvol_state, minor); 1356c7ca1008Sgw25295 if (zv == NULL) 1357c7ca1008Sgw25295 return (ENXIO); 1358c7ca1008Sgw25295 135973ec3d9cSgw25295 volsize = zv->zv_volsize; 136073ec3d9cSgw25295 if (uio->uio_resid > 0 && 136173ec3d9cSgw25295 (uio->uio_loffset < 0 || uio->uio_loffset >= volsize)) 136273ec3d9cSgw25295 return (EIO); 136373ec3d9cSgw25295 1364e7cbe64fSgw25295 if (zv->zv_flags & ZVOL_DUMPIFIED) { 1365e7cbe64fSgw25295 error = physio(zvol_strategy, NULL, dev, B_WRITE, 1366e7cbe64fSgw25295 zvol_minphys, uio); 1367e7cbe64fSgw25295 return (error); 1368e7cbe64fSgw25295 } 1369e7cbe64fSgw25295 1370*55da60b9SMark J Musante sync = !(zv->zv_flags & ZVOL_WCE) || 1371*55da60b9SMark J Musante (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS); 1372510b6c0eSNeil Perrin 1373c2e6a7d6Sperrin rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 1374c2e6a7d6Sperrin RL_WRITER); 137573ec3d9cSgw25295 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 1376feb08c6bSbillm uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 1377feb08c6bSbillm uint64_t off = uio->uio_loffset; 1378feb08c6bSbillm dmu_tx_t *tx = dmu_tx_create(zv->zv_objset); 137973ec3d9cSgw25295 138073ec3d9cSgw25295 if (bytes > volsize - off) /* don't write past the end */ 138173ec3d9cSgw25295 bytes = volsize - off; 138273ec3d9cSgw25295 1383feb08c6bSbillm dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 1384feb08c6bSbillm error = dmu_tx_assign(tx, TXG_WAIT); 1385feb08c6bSbillm if (error) { 1386feb08c6bSbillm dmu_tx_abort(tx); 1387feb08c6bSbillm break; 1388feb08c6bSbillm } 138994d1a210STim Haley error = dmu_write_uio_dbuf(zv->zv_dbuf, uio, bytes, tx); 1390feb08c6bSbillm if (error == 0) 1391510b6c0eSNeil Perrin zvol_log_write(zv, tx, off, bytes, sync); 1392feb08c6bSbillm dmu_tx_commit(tx); 1393feb08c6bSbillm 1394feb08c6bSbillm if (error) 1395feb08c6bSbillm break; 1396feb08c6bSbillm } 1397c2e6a7d6Sperrin zfs_range_unlock(rl); 1398510b6c0eSNeil Perrin if (sync) 1399e08bf2c6SEric Taylor zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ); 1400feb08c6bSbillm return (error); 1401fa9e4066Sahrens } 1402fa9e4066Sahrens 1403c7f714e2SEric Taylor int 1404c7f714e2SEric Taylor zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs) 1405c7f714e2SEric Taylor { 1406c7f714e2SEric Taylor struct uuid uuid = EFI_RESERVED; 1407c7f714e2SEric Taylor efi_gpe_t gpe = { 0 }; 1408c7f714e2SEric Taylor uint32_t crc; 1409c7f714e2SEric Taylor dk_efi_t efi; 1410c7f714e2SEric Taylor int length; 1411c7f714e2SEric Taylor char *ptr; 1412c7f714e2SEric Taylor 1413c7f714e2SEric Taylor if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag)) 1414c7f714e2SEric Taylor return (EFAULT); 1415c7f714e2SEric Taylor ptr = (char *)(uintptr_t)efi.dki_data_64; 1416c7f714e2SEric Taylor length = efi.dki_length; 1417c7f714e2SEric Taylor /* 1418c7f714e2SEric Taylor * Some clients may attempt to request a PMBR for the 1419c7f714e2SEric Taylor * zvol. Currently this interface will return EINVAL to 1420c7f714e2SEric Taylor * such requests. These requests could be supported by 1421c7f714e2SEric Taylor * adding a check for lba == 0 and consing up an appropriate 1422c7f714e2SEric Taylor * PMBR. 1423c7f714e2SEric Taylor */ 1424c7f714e2SEric Taylor if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0) 1425c7f714e2SEric Taylor return (EINVAL); 1426c7f714e2SEric Taylor 1427c7f714e2SEric Taylor gpe.efi_gpe_StartingLBA = LE_64(34ULL); 1428c7f714e2SEric Taylor gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1); 1429c7f714e2SEric Taylor UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid); 1430c7f714e2SEric Taylor 1431c7f714e2SEric Taylor if (efi.dki_lba == 1) { 1432c7f714e2SEric Taylor efi_gpt_t gpt = { 0 }; 1433c7f714e2SEric Taylor 1434c7f714e2SEric Taylor gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE); 1435c7f714e2SEric Taylor gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT); 1436c7f714e2SEric Taylor gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt)); 1437c7f714e2SEric Taylor gpt.efi_gpt_MyLBA = LE_64(1ULL); 1438c7f714e2SEric Taylor gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL); 1439c7f714e2SEric Taylor gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1); 1440c7f714e2SEric Taylor gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL); 1441c7f714e2SEric Taylor gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1); 1442c7f714e2SEric Taylor gpt.efi_gpt_SizeOfPartitionEntry = 1443c7f714e2SEric Taylor LE_32(sizeof (efi_gpe_t)); 1444c7f714e2SEric Taylor CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table); 1445c7f714e2SEric Taylor gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc); 1446c7f714e2SEric Taylor CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table); 1447c7f714e2SEric Taylor gpt.efi_gpt_HeaderCRC32 = LE_32(~crc); 1448c7f714e2SEric Taylor if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length), 1449c7f714e2SEric Taylor flag)) 1450c7f714e2SEric Taylor return (EFAULT); 1451c7f714e2SEric Taylor ptr += sizeof (gpt); 1452c7f714e2SEric Taylor length -= sizeof (gpt); 1453c7f714e2SEric Taylor } 1454c7f714e2SEric Taylor if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe), 1455c7f714e2SEric Taylor length), flag)) 1456c7f714e2SEric Taylor return (EFAULT); 1457c7f714e2SEric Taylor return (0); 1458c7f714e2SEric Taylor } 1459c7f714e2SEric Taylor 1460fa9e4066Sahrens /* 1461fa9e4066Sahrens * Dirtbag ioctls to support mkfs(1M) for UFS filesystems. See dkio(7I). 1462fa9e4066Sahrens */ 1463fa9e4066Sahrens /*ARGSUSED*/ 1464fa9e4066Sahrens int 1465fa9e4066Sahrens zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 1466fa9e4066Sahrens { 1467fa9e4066Sahrens zvol_state_t *zv; 1468af2c4821Smaybee struct dk_cinfo dki; 1469fa9e4066Sahrens struct dk_minfo dkm; 1470af2c4821Smaybee struct dk_callback *dkc; 1471fa9e4066Sahrens int error = 0; 1472e7cbe64fSgw25295 rl_t *rl; 1473fa9e4066Sahrens 1474fa9e4066Sahrens mutex_enter(&zvol_state_lock); 1475fa9e4066Sahrens 1476fa9e4066Sahrens zv = ddi_get_soft_state(zvol_state, getminor(dev)); 1477fa9e4066Sahrens 1478fa9e4066Sahrens if (zv == NULL) { 1479fa9e4066Sahrens mutex_exit(&zvol_state_lock); 1480fa9e4066Sahrens return (ENXIO); 1481fa9e4066Sahrens } 1482701f66c4SEric Taylor ASSERT(zv->zv_total_opens > 0); 1483fa9e4066Sahrens 1484fa9e4066Sahrens switch (cmd) { 1485fa9e4066Sahrens 1486fa9e4066Sahrens case DKIOCINFO: 1487af2c4821Smaybee bzero(&dki, sizeof (dki)); 1488af2c4821Smaybee (void) strcpy(dki.dki_cname, "zvol"); 1489af2c4821Smaybee (void) strcpy(dki.dki_dname, "zvol"); 1490af2c4821Smaybee dki.dki_ctype = DKC_UNKNOWN; 14913adc9019SEric Taylor dki.dki_unit = getminor(dev); 1492af2c4821Smaybee dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs); 1493fa9e4066Sahrens mutex_exit(&zvol_state_lock); 1494af2c4821Smaybee if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag)) 1495fa9e4066Sahrens error = EFAULT; 1496fa9e4066Sahrens return (error); 1497fa9e4066Sahrens 1498fa9e4066Sahrens case DKIOCGMEDIAINFO: 1499fa9e4066Sahrens bzero(&dkm, sizeof (dkm)); 1500fa9e4066Sahrens dkm.dki_lbsize = 1U << zv->zv_min_bs; 1501fa9e4066Sahrens dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs; 1502fa9e4066Sahrens dkm.dki_media_type = DK_UNKNOWN; 1503fa9e4066Sahrens mutex_exit(&zvol_state_lock); 1504fa9e4066Sahrens if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag)) 1505fa9e4066Sahrens error = EFAULT; 1506fa9e4066Sahrens return (error); 1507fa9e4066Sahrens 1508fa9e4066Sahrens case DKIOCGETEFI: 1509c7f714e2SEric Taylor { 1510c7f714e2SEric Taylor uint64_t vs = zv->zv_volsize; 1511c7f714e2SEric Taylor uint8_t bs = zv->zv_min_bs; 1512fa9e4066Sahrens 1513fa9e4066Sahrens mutex_exit(&zvol_state_lock); 1514c7f714e2SEric Taylor error = zvol_getefi((void *)arg, flag, vs, bs); 1515fa9e4066Sahrens return (error); 1516c7f714e2SEric Taylor } 1517fa9e4066Sahrens 1518feb08c6bSbillm case DKIOCFLUSHWRITECACHE: 1519af2c4821Smaybee dkc = (struct dk_callback *)arg; 1520701f66c4SEric Taylor mutex_exit(&zvol_state_lock); 1521feb08c6bSbillm zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ); 1522af2c4821Smaybee if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) { 1523af2c4821Smaybee (*dkc->dkc_callback)(dkc->dkc_cookie, error); 1524af2c4821Smaybee error = 0; 1525af2c4821Smaybee } 1526701f66c4SEric Taylor return (error); 1527701f66c4SEric Taylor 1528701f66c4SEric Taylor case DKIOCGETWCE: 1529701f66c4SEric Taylor { 1530701f66c4SEric Taylor int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0; 1531701f66c4SEric Taylor if (ddi_copyout(&wce, (void *)arg, sizeof (int), 1532701f66c4SEric Taylor flag)) 1533701f66c4SEric Taylor error = EFAULT; 1534feb08c6bSbillm break; 1535701f66c4SEric Taylor } 1536701f66c4SEric Taylor case DKIOCSETWCE: 1537701f66c4SEric Taylor { 1538701f66c4SEric Taylor int wce; 1539701f66c4SEric Taylor if (ddi_copyin((void *)arg, &wce, sizeof (int), 1540701f66c4SEric Taylor flag)) { 1541701f66c4SEric Taylor error = EFAULT; 1542701f66c4SEric Taylor break; 1543701f66c4SEric Taylor } 1544701f66c4SEric Taylor if (wce) { 1545701f66c4SEric Taylor zv->zv_flags |= ZVOL_WCE; 1546701f66c4SEric Taylor mutex_exit(&zvol_state_lock); 1547701f66c4SEric Taylor } else { 1548701f66c4SEric Taylor zv->zv_flags &= ~ZVOL_WCE; 1549701f66c4SEric Taylor mutex_exit(&zvol_state_lock); 1550701f66c4SEric Taylor zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ); 1551701f66c4SEric Taylor } 1552701f66c4SEric Taylor return (0); 1553701f66c4SEric Taylor } 1554feb08c6bSbillm 1555b6130eadSmaybee case DKIOCGGEOM: 1556b6130eadSmaybee case DKIOCGVTOC: 1557e7cbe64fSgw25295 /* 1558e7cbe64fSgw25295 * commands using these (like prtvtoc) expect ENOTSUP 1559e7cbe64fSgw25295 * since we're emulating an EFI label 1560e7cbe64fSgw25295 */ 1561b6130eadSmaybee error = ENOTSUP; 1562b6130eadSmaybee break; 1563b6130eadSmaybee 1564e7cbe64fSgw25295 case DKIOCDUMPINIT: 1565e7cbe64fSgw25295 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 1566e7cbe64fSgw25295 RL_WRITER); 1567e7cbe64fSgw25295 error = zvol_dumpify(zv); 1568e7cbe64fSgw25295 zfs_range_unlock(rl); 1569e7cbe64fSgw25295 break; 1570e7cbe64fSgw25295 1571e7cbe64fSgw25295 case DKIOCDUMPFINI: 157206d5ae10SEric Taylor if (!(zv->zv_flags & ZVOL_DUMPIFIED)) 157306d5ae10SEric Taylor break; 1574e7cbe64fSgw25295 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 1575e7cbe64fSgw25295 RL_WRITER); 1576e7cbe64fSgw25295 error = zvol_dump_fini(zv); 1577e7cbe64fSgw25295 zfs_range_unlock(rl); 1578e7cbe64fSgw25295 break; 1579e7cbe64fSgw25295 1580fa9e4066Sahrens default: 158168a5ac4dSmaybee error = ENOTTY; 1582fa9e4066Sahrens break; 1583fa9e4066Sahrens 1584fa9e4066Sahrens } 1585fa9e4066Sahrens mutex_exit(&zvol_state_lock); 1586fa9e4066Sahrens return (error); 1587fa9e4066Sahrens } 1588fa9e4066Sahrens 1589fa9e4066Sahrens int 1590fa9e4066Sahrens zvol_busy(void) 1591fa9e4066Sahrens { 1592fa9e4066Sahrens return (zvol_minors != 0); 1593fa9e4066Sahrens } 1594fa9e4066Sahrens 1595fa9e4066Sahrens void 1596fa9e4066Sahrens zvol_init(void) 1597fa9e4066Sahrens { 1598fa9e4066Sahrens VERIFY(ddi_soft_state_init(&zvol_state, sizeof (zvol_state_t), 1) == 0); 1599fa9e4066Sahrens mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL); 1600fa9e4066Sahrens } 1601fa9e4066Sahrens 1602fa9e4066Sahrens void 1603fa9e4066Sahrens zvol_fini(void) 1604fa9e4066Sahrens { 1605fa9e4066Sahrens mutex_destroy(&zvol_state_lock); 1606fa9e4066Sahrens ddi_soft_state_fini(&zvol_state); 1607fa9e4066Sahrens } 1608e7cbe64fSgw25295 1609e7cbe64fSgw25295 static int 1610e7cbe64fSgw25295 zvol_dump_init(zvol_state_t *zv, boolean_t resize) 1611e7cbe64fSgw25295 { 1612e7cbe64fSgw25295 dmu_tx_t *tx; 1613e7cbe64fSgw25295 int error = 0; 1614e7cbe64fSgw25295 objset_t *os = zv->zv_objset; 1615e7cbe64fSgw25295 nvlist_t *nv = NULL; 16168d265e66SGeorge Wilson uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset)); 1617e7cbe64fSgw25295 1618e7cbe64fSgw25295 ASSERT(MUTEX_HELD(&zvol_state_lock)); 1619681d9761SEric Taylor error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0, 1620681d9761SEric Taylor DMU_OBJECT_END); 1621681d9761SEric Taylor /* wait for dmu_free_long_range to actually free the blocks */ 1622681d9761SEric Taylor txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 1623e7cbe64fSgw25295 1624e7cbe64fSgw25295 tx = dmu_tx_create(os); 1625e7cbe64fSgw25295 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 1626681d9761SEric Taylor dmu_tx_hold_bonus(tx, ZVOL_OBJ); 1627e7cbe64fSgw25295 error = dmu_tx_assign(tx, TXG_WAIT); 1628e7cbe64fSgw25295 if (error) { 1629e7cbe64fSgw25295 dmu_tx_abort(tx); 1630e7cbe64fSgw25295 return (error); 1631e7cbe64fSgw25295 } 1632e7cbe64fSgw25295 1633e7cbe64fSgw25295 /* 1634e7cbe64fSgw25295 * If we are resizing the dump device then we only need to 1635e7cbe64fSgw25295 * update the refreservation to match the newly updated 1636e7cbe64fSgw25295 * zvolsize. Otherwise, we save off the original state of the 1637e7cbe64fSgw25295 * zvol so that we can restore them if the zvol is ever undumpified. 1638e7cbe64fSgw25295 */ 1639e7cbe64fSgw25295 if (resize) { 1640e7cbe64fSgw25295 error = zap_update(os, ZVOL_ZAP_OBJ, 1641e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 1642e7cbe64fSgw25295 &zv->zv_volsize, tx); 1643e7cbe64fSgw25295 } else { 1644afee20e4SGeorge Wilson uint64_t checksum, compress, refresrv, vbs, dedup; 164588b7b0f2SMatthew Ahrens 1646e7cbe64fSgw25295 error = dsl_prop_get_integer(zv->zv_name, 1647e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL); 1648e7cbe64fSgw25295 error = error ? error : dsl_prop_get_integer(zv->zv_name, 1649e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL); 1650e7cbe64fSgw25295 error = error ? error : dsl_prop_get_integer(zv->zv_name, 1651e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL); 165288b7b0f2SMatthew Ahrens error = error ? error : dsl_prop_get_integer(zv->zv_name, 165388b7b0f2SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL); 16548d265e66SGeorge Wilson if (version >= SPA_VERSION_DEDUP) { 16558d265e66SGeorge Wilson error = error ? error : 16568d265e66SGeorge Wilson dsl_prop_get_integer(zv->zv_name, 1657afee20e4SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), &dedup, NULL); 16588d265e66SGeorge Wilson } 1659e7cbe64fSgw25295 1660e7cbe64fSgw25295 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1661e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, 1662e7cbe64fSgw25295 &compress, tx); 1663e7cbe64fSgw25295 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1664e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx); 1665e7cbe64fSgw25295 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1666e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 1667e7cbe64fSgw25295 &refresrv, tx); 166888b7b0f2SMatthew Ahrens error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 166988b7b0f2SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, 167088b7b0f2SMatthew Ahrens &vbs, tx); 1671681d9761SEric Taylor error = error ? error : dmu_object_set_blocksize( 1672681d9761SEric Taylor os, ZVOL_OBJ, SPA_MAXBLOCKSIZE, 0, tx); 16738d265e66SGeorge Wilson if (version >= SPA_VERSION_DEDUP) { 1674afee20e4SGeorge Wilson error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 1675afee20e4SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, 1676afee20e4SGeorge Wilson &dedup, tx); 16778d265e66SGeorge Wilson } 1678681d9761SEric Taylor if (error == 0) 1679681d9761SEric Taylor zv->zv_volblocksize = SPA_MAXBLOCKSIZE; 1680e7cbe64fSgw25295 } 1681e7cbe64fSgw25295 dmu_tx_commit(tx); 1682e7cbe64fSgw25295 1683e7cbe64fSgw25295 /* 1684e7cbe64fSgw25295 * We only need update the zvol's property if we are initializing 1685e7cbe64fSgw25295 * the dump area for the first time. 1686e7cbe64fSgw25295 */ 1687e7cbe64fSgw25295 if (!resize) { 1688e7cbe64fSgw25295 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1689e7cbe64fSgw25295 VERIFY(nvlist_add_uint64(nv, 1690e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0); 1691e7cbe64fSgw25295 VERIFY(nvlist_add_uint64(nv, 1692e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 1693e7cbe64fSgw25295 ZIO_COMPRESS_OFF) == 0); 1694e7cbe64fSgw25295 VERIFY(nvlist_add_uint64(nv, 1695e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 1696e7cbe64fSgw25295 ZIO_CHECKSUM_OFF) == 0); 16978d265e66SGeorge Wilson if (version >= SPA_VERSION_DEDUP) { 1698afee20e4SGeorge Wilson VERIFY(nvlist_add_uint64(nv, 1699afee20e4SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), 1700afee20e4SGeorge Wilson ZIO_CHECKSUM_OFF) == 0); 17018d265e66SGeorge Wilson } 1702e7cbe64fSgw25295 170392241e0bSTom Erickson error = zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL, 170492241e0bSTom Erickson nv, NULL); 1705e7cbe64fSgw25295 nvlist_free(nv); 1706e7cbe64fSgw25295 1707e7cbe64fSgw25295 if (error) 1708e7cbe64fSgw25295 return (error); 1709e7cbe64fSgw25295 } 1710e7cbe64fSgw25295 1711e7cbe64fSgw25295 /* Allocate the space for the dump */ 1712e7cbe64fSgw25295 error = zvol_prealloc(zv); 1713e7cbe64fSgw25295 return (error); 1714e7cbe64fSgw25295 } 1715e7cbe64fSgw25295 1716e7cbe64fSgw25295 static int 1717e7cbe64fSgw25295 zvol_dumpify(zvol_state_t *zv) 1718e7cbe64fSgw25295 { 1719e7cbe64fSgw25295 int error = 0; 1720e7cbe64fSgw25295 uint64_t dumpsize = 0; 1721e7cbe64fSgw25295 dmu_tx_t *tx; 1722e7cbe64fSgw25295 objset_t *os = zv->zv_objset; 1723e7cbe64fSgw25295 1724681d9761SEric Taylor if (zv->zv_flags & ZVOL_RDONLY) 1725e7cbe64fSgw25295 return (EROFS); 1726e7cbe64fSgw25295 1727e7cbe64fSgw25295 if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 1728e7cbe64fSgw25295 8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) { 1729e7cbe64fSgw25295 boolean_t resize = (dumpsize > 0) ? B_TRUE : B_FALSE; 1730e7cbe64fSgw25295 1731e7cbe64fSgw25295 if ((error = zvol_dump_init(zv, resize)) != 0) { 1732e7cbe64fSgw25295 (void) zvol_dump_fini(zv); 1733e7cbe64fSgw25295 return (error); 1734e7cbe64fSgw25295 } 1735e7cbe64fSgw25295 } 1736e7cbe64fSgw25295 1737e7cbe64fSgw25295 /* 1738e7cbe64fSgw25295 * Build up our lba mapping. 1739e7cbe64fSgw25295 */ 1740e7cbe64fSgw25295 error = zvol_get_lbas(zv); 1741e7cbe64fSgw25295 if (error) { 1742e7cbe64fSgw25295 (void) zvol_dump_fini(zv); 1743e7cbe64fSgw25295 return (error); 1744e7cbe64fSgw25295 } 1745e7cbe64fSgw25295 1746e7cbe64fSgw25295 tx = dmu_tx_create(os); 1747e7cbe64fSgw25295 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 1748e7cbe64fSgw25295 error = dmu_tx_assign(tx, TXG_WAIT); 1749e7cbe64fSgw25295 if (error) { 1750e7cbe64fSgw25295 dmu_tx_abort(tx); 1751e7cbe64fSgw25295 (void) zvol_dump_fini(zv); 1752e7cbe64fSgw25295 return (error); 1753e7cbe64fSgw25295 } 1754e7cbe64fSgw25295 1755e7cbe64fSgw25295 zv->zv_flags |= ZVOL_DUMPIFIED; 1756e7cbe64fSgw25295 error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1, 1757e7cbe64fSgw25295 &zv->zv_volsize, tx); 1758e7cbe64fSgw25295 dmu_tx_commit(tx); 1759e7cbe64fSgw25295 1760e7cbe64fSgw25295 if (error) { 1761e7cbe64fSgw25295 (void) zvol_dump_fini(zv); 1762e7cbe64fSgw25295 return (error); 1763e7cbe64fSgw25295 } 1764e7cbe64fSgw25295 1765e7cbe64fSgw25295 txg_wait_synced(dmu_objset_pool(os), 0); 1766e7cbe64fSgw25295 return (0); 1767e7cbe64fSgw25295 } 1768e7cbe64fSgw25295 1769e7cbe64fSgw25295 static int 1770e7cbe64fSgw25295 zvol_dump_fini(zvol_state_t *zv) 1771e7cbe64fSgw25295 { 1772e7cbe64fSgw25295 dmu_tx_t *tx; 1773e7cbe64fSgw25295 objset_t *os = zv->zv_objset; 1774e7cbe64fSgw25295 nvlist_t *nv; 1775e7cbe64fSgw25295 int error = 0; 1776afee20e4SGeorge Wilson uint64_t checksum, compress, refresrv, vbs, dedup; 17778d265e66SGeorge Wilson uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset)); 1778e7cbe64fSgw25295 1779b7e50089Smaybee /* 1780b7e50089Smaybee * Attempt to restore the zvol back to its pre-dumpified state. 1781b7e50089Smaybee * This is a best-effort attempt as it's possible that not all 1782b7e50089Smaybee * of these properties were initialized during the dumpify process 1783b7e50089Smaybee * (i.e. error during zvol_dump_init). 1784b7e50089Smaybee */ 1785b7e50089Smaybee 1786e7cbe64fSgw25295 tx = dmu_tx_create(os); 1787e7cbe64fSgw25295 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 1788e7cbe64fSgw25295 error = dmu_tx_assign(tx, TXG_WAIT); 1789e7cbe64fSgw25295 if (error) { 1790e7cbe64fSgw25295 dmu_tx_abort(tx); 1791e7cbe64fSgw25295 return (error); 1792e7cbe64fSgw25295 } 1793b7e50089Smaybee (void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx); 1794b7e50089Smaybee dmu_tx_commit(tx); 1795e7cbe64fSgw25295 1796e7cbe64fSgw25295 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 1797e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum); 1798e7cbe64fSgw25295 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 1799e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress); 1800e7cbe64fSgw25295 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 1801e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv); 180288b7b0f2SMatthew Ahrens (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 180388b7b0f2SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs); 1804e7cbe64fSgw25295 1805e7cbe64fSgw25295 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 1806e7cbe64fSgw25295 (void) nvlist_add_uint64(nv, 1807e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum); 1808e7cbe64fSgw25295 (void) nvlist_add_uint64(nv, 1809e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress); 1810e7cbe64fSgw25295 (void) nvlist_add_uint64(nv, 1811e7cbe64fSgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv); 18128d265e66SGeorge Wilson if (version >= SPA_VERSION_DEDUP && 18138d265e66SGeorge Wilson zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 18148d265e66SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, &dedup) == 0) { 1815afee20e4SGeorge Wilson (void) nvlist_add_uint64(nv, 1816afee20e4SGeorge Wilson zfs_prop_to_name(ZFS_PROP_DEDUP), dedup); 18178d265e66SGeorge Wilson } 181892241e0bSTom Erickson (void) zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL, 181992241e0bSTom Erickson nv, NULL); 1820e7cbe64fSgw25295 nvlist_free(nv); 1821e7cbe64fSgw25295 1822b7e50089Smaybee zvol_free_extents(zv); 1823b7e50089Smaybee zv->zv_flags &= ~ZVOL_DUMPIFIED; 1824b7e50089Smaybee (void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END); 1825681d9761SEric Taylor /* wait for dmu_free_long_range to actually free the blocks */ 1826681d9761SEric Taylor txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 1827681d9761SEric Taylor tx = dmu_tx_create(os); 1828681d9761SEric Taylor dmu_tx_hold_bonus(tx, ZVOL_OBJ); 1829681d9761SEric Taylor error = dmu_tx_assign(tx, TXG_WAIT); 1830681d9761SEric Taylor if (error) { 1831681d9761SEric Taylor dmu_tx_abort(tx); 1832681d9761SEric Taylor return (error); 1833681d9761SEric Taylor } 1834b24ab676SJeff Bonwick if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0) 1835b24ab676SJeff Bonwick zv->zv_volblocksize = vbs; 1836681d9761SEric Taylor dmu_tx_commit(tx); 1837b7e50089Smaybee 1838e7cbe64fSgw25295 return (0); 1839e7cbe64fSgw25295 } 1840