1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51544Seschrock * Common Development and Distribution License (the "License"). 61544Seschrock * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 21789Sahrens /* 2212095SChris.Kirby@sun.com * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23789Sahrens */ 24789Sahrens 25789Sahrens /* 26789Sahrens * ZFS volume emulation driver. 27789Sahrens * 28789Sahrens * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes. 29789Sahrens * Volumes are accessed through the symbolic links named: 30789Sahrens * 31789Sahrens * /dev/zvol/dsk/<pool_name>/<dataset_name> 32789Sahrens * /dev/zvol/rdsk/<pool_name>/<dataset_name> 33789Sahrens * 3410588SEric.Taylor@Sun.COM * These links are created by the /dev filesystem (sdev_zvolops.c). 35789Sahrens * Volumes are persistent through reboot. No user command needs to be 36789Sahrens * run before opening and using a device. 37789Sahrens */ 38789Sahrens 39789Sahrens #include <sys/types.h> 40789Sahrens #include <sys/param.h> 41789Sahrens #include <sys/errno.h> 42789Sahrens #include <sys/uio.h> 43789Sahrens #include <sys/buf.h> 44789Sahrens #include <sys/modctl.h> 45789Sahrens #include <sys/open.h> 46789Sahrens #include <sys/kmem.h> 47789Sahrens #include <sys/conf.h> 48789Sahrens #include <sys/cmn_err.h> 49789Sahrens #include <sys/stat.h> 50789Sahrens #include <sys/zap.h> 51789Sahrens #include <sys/spa.h> 52789Sahrens #include <sys/zio.h> 536423Sgw25295 #include <sys/dmu_traverse.h> 546423Sgw25295 #include <sys/dnode.h> 556423Sgw25295 #include <sys/dsl_dataset.h> 56789Sahrens #include <sys/dsl_prop.h> 57789Sahrens #include <sys/dkio.h> 58789Sahrens #include <sys/efi_partition.h> 59789Sahrens #include <sys/byteorder.h> 60789Sahrens #include <sys/pathname.h> 61789Sahrens #include <sys/ddi.h> 62789Sahrens #include <sys/sunddi.h> 63789Sahrens #include <sys/crc32.h> 64789Sahrens #include <sys/dirent.h> 65789Sahrens #include <sys/policy.h> 66789Sahrens #include <sys/fs/zfs.h> 67789Sahrens #include <sys/zfs_ioctl.h> 68789Sahrens #include <sys/mkdev.h> 691141Sperrin #include <sys/zil.h> 702237Smaybee #include <sys/refcount.h> 713755Sperrin #include <sys/zfs_znode.h> 723755Sperrin #include <sys/zfs_rlock.h> 736423Sgw25295 #include <sys/vdev_disk.h> 746423Sgw25295 #include <sys/vdev_impl.h> 756423Sgw25295 #include <sys/zvol.h> 766423Sgw25295 #include <sys/dumphdr.h> 778227SNeil.Perrin@Sun.COM #include <sys/zil_impl.h> 78789Sahrens 79789Sahrens #include "zfs_namecheck.h" 80789Sahrens 816423Sgw25295 static void *zvol_state; 8210298SMatthew.Ahrens@Sun.COM static char *zvol_tag = "zvol_tag"; 83789Sahrens 846423Sgw25295 #define ZVOL_DUMPSIZE "dumpsize" 85789Sahrens 86789Sahrens /* 87789Sahrens * This lock protects the zvol_state structure from being modified 88789Sahrens * while it's being used, e.g. an open that comes in before a create 89789Sahrens * finishes. It also protects temporary opens of the dataset so that, 90789Sahrens * e.g., an open doesn't get a spurious EBUSY. 91789Sahrens */ 92789Sahrens static kmutex_t zvol_state_lock; 93789Sahrens static uint32_t zvol_minors; 94789Sahrens 956423Sgw25295 typedef struct zvol_extent { 967837SMatthew.Ahrens@Sun.COM list_node_t ze_node; 976423Sgw25295 dva_t ze_dva; /* dva associated with this extent */ 987837SMatthew.Ahrens@Sun.COM uint64_t ze_nblks; /* number of blocks in extent */ 996423Sgw25295 } zvol_extent_t; 1006423Sgw25295 1016423Sgw25295 /* 102789Sahrens * The in-core state of each volume. 103789Sahrens */ 104789Sahrens typedef struct zvol_state { 105789Sahrens char zv_name[MAXPATHLEN]; /* pool/dd name */ 106789Sahrens uint64_t zv_volsize; /* amount of space we advertise */ 1073063Sperrin uint64_t zv_volblocksize; /* volume block size */ 108789Sahrens minor_t zv_minor; /* minor number */ 109789Sahrens uint8_t zv_min_bs; /* minimum addressable block shift */ 1109303SEric.Taylor@Sun.COM uint8_t zv_flags; /* readonly, dumpified, etc. */ 111789Sahrens objset_t *zv_objset; /* objset handle */ 112789Sahrens uint32_t zv_open_count[OTYPCNT]; /* open counts */ 113789Sahrens uint32_t zv_total_opens; /* total open count */ 1141141Sperrin zilog_t *zv_zilog; /* ZIL handle */ 1157837SMatthew.Ahrens@Sun.COM list_t zv_extents; /* List of extents for dump */ 1163755Sperrin znode_t zv_znode; /* for range locking */ 11712123STim.Haley@Sun.COM dmu_buf_t *zv_dbuf; /* bonus handle */ 118789Sahrens } zvol_state_t; 119789Sahrens 1203063Sperrin /* 1216423Sgw25295 * zvol specific flags 1226423Sgw25295 */ 1236423Sgw25295 #define ZVOL_RDONLY 0x1 1246423Sgw25295 #define ZVOL_DUMPIFIED 0x2 1257405SEric.Taylor@Sun.COM #define ZVOL_EXCL 0x4 1269303SEric.Taylor@Sun.COM #define ZVOL_WCE 0x8 1276423Sgw25295 1286423Sgw25295 /* 1293063Sperrin * zvol maximum transfer in one DMU tx. 1303063Sperrin */ 1313063Sperrin int zvol_maxphys = DMU_MAX_ACCESS/2; 1323063Sperrin 13311022STom.Erickson@Sun.COM extern int zfs_set_prop_nvlist(const char *, zprop_source_t, 13411022STom.Erickson@Sun.COM nvlist_t *, nvlist_t **); 13510588SEric.Taylor@Sun.COM static int zvol_remove_zv(zvol_state_t *); 1363638Sbillm static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio); 1376423Sgw25295 static int zvol_dumpify(zvol_state_t *zv); 1386423Sgw25295 static int zvol_dump_fini(zvol_state_t *zv); 1396423Sgw25295 static int zvol_dump_init(zvol_state_t *zv, boolean_t resize); 1403063Sperrin 141789Sahrens static void 14210588SEric.Taylor@Sun.COM zvol_size_changed(uint64_t volsize, major_t maj, minor_t min) 143789Sahrens { 14410588SEric.Taylor@Sun.COM dev_t dev = makedevice(maj, min); 145789Sahrens 146789Sahrens VERIFY(ddi_prop_update_int64(dev, zfs_dip, 14710588SEric.Taylor@Sun.COM "Size", volsize) == DDI_SUCCESS); 148789Sahrens VERIFY(ddi_prop_update_int64(dev, zfs_dip, 14910588SEric.Taylor@Sun.COM "Nblocks", lbtodb(volsize)) == DDI_SUCCESS); 1506423Sgw25295 1516423Sgw25295 /* Notify specfs to invalidate the cached size */ 1526423Sgw25295 spec_size_invalidate(dev, VBLK); 1536423Sgw25295 spec_size_invalidate(dev, VCHR); 154789Sahrens } 155789Sahrens 156789Sahrens int 1572676Seschrock zvol_check_volsize(uint64_t volsize, uint64_t blocksize) 158789Sahrens { 1592676Seschrock if (volsize == 0) 160789Sahrens return (EINVAL); 161789Sahrens 1622676Seschrock if (volsize % blocksize != 0) 1631133Seschrock return (EINVAL); 1641133Seschrock 165789Sahrens #ifdef _ILP32 1662676Seschrock if (volsize - 1 > SPEC_MAXOFFSET_T) 167789Sahrens return (EOVERFLOW); 168789Sahrens #endif 169789Sahrens return (0); 170789Sahrens } 171789Sahrens 172789Sahrens int 1732676Seschrock zvol_check_volblocksize(uint64_t volblocksize) 174789Sahrens { 1752676Seschrock if (volblocksize < SPA_MINBLOCKSIZE || 1762676Seschrock volblocksize > SPA_MAXBLOCKSIZE || 1772676Seschrock !ISP2(volblocksize)) 178789Sahrens return (EDOM); 179789Sahrens 180789Sahrens return (0); 181789Sahrens } 182789Sahrens 183789Sahrens int 1842885Sahrens zvol_get_stats(objset_t *os, nvlist_t *nv) 185789Sahrens { 186789Sahrens int error; 187789Sahrens dmu_object_info_t doi; 1882885Sahrens uint64_t val; 189789Sahrens 1902885Sahrens error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val); 191789Sahrens if (error) 192789Sahrens return (error); 193789Sahrens 1942885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val); 1952885Sahrens 196789Sahrens error = dmu_object_info(os, ZVOL_OBJ, &doi); 197789Sahrens 1982885Sahrens if (error == 0) { 1992885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE, 2002885Sahrens doi.doi_data_block_size); 2012885Sahrens } 202789Sahrens 203789Sahrens return (error); 204789Sahrens } 205789Sahrens 206789Sahrens /* 207789Sahrens * Find a free minor number. 208789Sahrens */ 209789Sahrens static minor_t 210789Sahrens zvol_minor_alloc(void) 211789Sahrens { 212789Sahrens minor_t minor; 213789Sahrens 214789Sahrens ASSERT(MUTEX_HELD(&zvol_state_lock)); 215789Sahrens 216789Sahrens for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) 217789Sahrens if (ddi_get_soft_state(zvol_state, minor) == NULL) 218789Sahrens return (minor); 219789Sahrens 220789Sahrens return (0); 221789Sahrens } 222789Sahrens 223789Sahrens static zvol_state_t * 2242676Seschrock zvol_minor_lookup(const char *name) 225789Sahrens { 226789Sahrens minor_t minor; 227789Sahrens zvol_state_t *zv; 228789Sahrens 229789Sahrens ASSERT(MUTEX_HELD(&zvol_state_lock)); 230789Sahrens 231789Sahrens for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) { 232789Sahrens zv = ddi_get_soft_state(zvol_state, minor); 233789Sahrens if (zv == NULL) 234789Sahrens continue; 235789Sahrens if (strcmp(zv->zv_name, name) == 0) 23612095SChris.Kirby@sun.com return (zv); 237789Sahrens } 238789Sahrens 23912095SChris.Kirby@sun.com return (NULL); 240789Sahrens } 241789Sahrens 2426423Sgw25295 /* extent mapping arg */ 2436423Sgw25295 struct maparg { 2447837SMatthew.Ahrens@Sun.COM zvol_state_t *ma_zv; 2457837SMatthew.Ahrens@Sun.COM uint64_t ma_blks; 2466423Sgw25295 }; 2476423Sgw25295 2486423Sgw25295 /*ARGSUSED*/ 2496423Sgw25295 static int 25010922SJeff.Bonwick@Sun.COM zvol_map_block(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 25110922SJeff.Bonwick@Sun.COM const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg) 2526423Sgw25295 { 2537837SMatthew.Ahrens@Sun.COM struct maparg *ma = arg; 2547837SMatthew.Ahrens@Sun.COM zvol_extent_t *ze; 2557837SMatthew.Ahrens@Sun.COM int bs = ma->ma_zv->zv_volblocksize; 2566423Sgw25295 2577837SMatthew.Ahrens@Sun.COM if (bp == NULL || zb->zb_object != ZVOL_OBJ || zb->zb_level != 0) 2586423Sgw25295 return (0); 2596423Sgw25295 2607837SMatthew.Ahrens@Sun.COM VERIFY3U(ma->ma_blks, ==, zb->zb_blkid); 2617837SMatthew.Ahrens@Sun.COM ma->ma_blks++; 2627837SMatthew.Ahrens@Sun.COM 2636423Sgw25295 /* Abort immediately if we have encountered gang blocks */ 2647837SMatthew.Ahrens@Sun.COM if (BP_IS_GANG(bp)) 2657837SMatthew.Ahrens@Sun.COM return (EFRAGS); 2666423Sgw25295 2677837SMatthew.Ahrens@Sun.COM /* 2687837SMatthew.Ahrens@Sun.COM * See if the block is at the end of the previous extent. 2697837SMatthew.Ahrens@Sun.COM */ 2707837SMatthew.Ahrens@Sun.COM ze = list_tail(&ma->ma_zv->zv_extents); 2717837SMatthew.Ahrens@Sun.COM if (ze && 2727837SMatthew.Ahrens@Sun.COM DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) && 2737837SMatthew.Ahrens@Sun.COM DVA_GET_OFFSET(BP_IDENTITY(bp)) == 2747837SMatthew.Ahrens@Sun.COM DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) { 2757837SMatthew.Ahrens@Sun.COM ze->ze_nblks++; 2766423Sgw25295 return (0); 2776423Sgw25295 } 2786423Sgw25295 2797837SMatthew.Ahrens@Sun.COM dprintf_bp(bp, "%s", "next blkptr:"); 2807837SMatthew.Ahrens@Sun.COM 2817837SMatthew.Ahrens@Sun.COM /* start a new extent */ 2827837SMatthew.Ahrens@Sun.COM ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP); 2837837SMatthew.Ahrens@Sun.COM ze->ze_dva = bp->blk_dva[0]; /* structure assignment */ 2847837SMatthew.Ahrens@Sun.COM ze->ze_nblks = 1; 2857837SMatthew.Ahrens@Sun.COM list_insert_tail(&ma->ma_zv->zv_extents, ze); 2867837SMatthew.Ahrens@Sun.COM return (0); 2877837SMatthew.Ahrens@Sun.COM } 2887837SMatthew.Ahrens@Sun.COM 2897837SMatthew.Ahrens@Sun.COM static void 2907837SMatthew.Ahrens@Sun.COM zvol_free_extents(zvol_state_t *zv) 2917837SMatthew.Ahrens@Sun.COM { 2927837SMatthew.Ahrens@Sun.COM zvol_extent_t *ze; 2937837SMatthew.Ahrens@Sun.COM 2947837SMatthew.Ahrens@Sun.COM while (ze = list_head(&zv->zv_extents)) { 2957837SMatthew.Ahrens@Sun.COM list_remove(&zv->zv_extents, ze); 2967837SMatthew.Ahrens@Sun.COM kmem_free(ze, sizeof (zvol_extent_t)); 2977837SMatthew.Ahrens@Sun.COM } 2987837SMatthew.Ahrens@Sun.COM } 2997837SMatthew.Ahrens@Sun.COM 3007837SMatthew.Ahrens@Sun.COM static int 3017837SMatthew.Ahrens@Sun.COM zvol_get_lbas(zvol_state_t *zv) 3027837SMatthew.Ahrens@Sun.COM { 30311689SEric.Taylor@Sun.COM objset_t *os = zv->zv_objset; 3047837SMatthew.Ahrens@Sun.COM struct maparg ma; 3057837SMatthew.Ahrens@Sun.COM int err; 3067837SMatthew.Ahrens@Sun.COM 3077837SMatthew.Ahrens@Sun.COM ma.ma_zv = zv; 3087837SMatthew.Ahrens@Sun.COM ma.ma_blks = 0; 3097837SMatthew.Ahrens@Sun.COM zvol_free_extents(zv); 3107837SMatthew.Ahrens@Sun.COM 31111689SEric.Taylor@Sun.COM /* commit any in-flight changes before traversing the dataset */ 31211689SEric.Taylor@Sun.COM txg_wait_synced(dmu_objset_pool(os), 0); 31311689SEric.Taylor@Sun.COM err = traverse_dataset(dmu_objset_ds(os), 0, 3147837SMatthew.Ahrens@Sun.COM TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma); 3157837SMatthew.Ahrens@Sun.COM if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) { 3167837SMatthew.Ahrens@Sun.COM zvol_free_extents(zv); 3177837SMatthew.Ahrens@Sun.COM return (err ? err : EIO); 3186423Sgw25295 } 3196423Sgw25295 3206423Sgw25295 return (0); 3216423Sgw25295 } 3226423Sgw25295 3234543Smarks /* ARGSUSED */ 324789Sahrens void 3254543Smarks zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 326789Sahrens { 3275331Samw zfs_creat_t *zct = arg; 3285331Samw nvlist_t *nvprops = zct->zct_props; 329789Sahrens int error; 3302676Seschrock uint64_t volblocksize, volsize; 331789Sahrens 3324543Smarks VERIFY(nvlist_lookup_uint64(nvprops, 3332676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0); 3344543Smarks if (nvlist_lookup_uint64(nvprops, 3352676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0) 3362676Seschrock volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 3372676Seschrock 3382676Seschrock /* 3396423Sgw25295 * These properties must be removed from the list so the generic 3402676Seschrock * property setting step won't apply to them. 3412676Seschrock */ 3424543Smarks VERIFY(nvlist_remove_all(nvprops, 3432676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0); 3444543Smarks (void) nvlist_remove_all(nvprops, 3452676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE)); 3462676Seschrock 3472676Seschrock error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize, 348789Sahrens DMU_OT_NONE, 0, tx); 349789Sahrens ASSERT(error == 0); 350789Sahrens 351789Sahrens error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP, 352789Sahrens DMU_OT_NONE, 0, tx); 353789Sahrens ASSERT(error == 0); 354789Sahrens 3552676Seschrock error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx); 356789Sahrens ASSERT(error == 0); 357789Sahrens } 358789Sahrens 359789Sahrens /* 3601141Sperrin * Replay a TX_WRITE ZIL transaction that didn't get committed 3611141Sperrin * after a system failure 3621141Sperrin */ 3631141Sperrin static int 3641141Sperrin zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap) 3651141Sperrin { 3661141Sperrin objset_t *os = zv->zv_objset; 3671141Sperrin char *data = (char *)(lr + 1); /* data follows lr_write_t */ 36810922SJeff.Bonwick@Sun.COM uint64_t offset, length; 3691141Sperrin dmu_tx_t *tx; 3701141Sperrin int error; 3711141Sperrin 3721141Sperrin if (byteswap) 3731141Sperrin byteswap_uint64_array(lr, sizeof (*lr)); 3741141Sperrin 37510922SJeff.Bonwick@Sun.COM offset = lr->lr_offset; 37610922SJeff.Bonwick@Sun.COM length = lr->lr_length; 37710922SJeff.Bonwick@Sun.COM 37810922SJeff.Bonwick@Sun.COM /* If it's a dmu_sync() block, write the whole block */ 37910922SJeff.Bonwick@Sun.COM if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) { 38010922SJeff.Bonwick@Sun.COM uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr); 38110922SJeff.Bonwick@Sun.COM if (length < blocksize) { 38210922SJeff.Bonwick@Sun.COM offset -= offset % blocksize; 38310922SJeff.Bonwick@Sun.COM length = blocksize; 38410922SJeff.Bonwick@Sun.COM } 38510922SJeff.Bonwick@Sun.COM } 38610800SNeil.Perrin@Sun.COM 3871141Sperrin tx = dmu_tx_create(os); 38810922SJeff.Bonwick@Sun.COM dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length); 3898227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_WAIT); 3901141Sperrin if (error) { 3911141Sperrin dmu_tx_abort(tx); 3921141Sperrin } else { 39310922SJeff.Bonwick@Sun.COM dmu_write(os, ZVOL_OBJ, offset, length, data, tx); 3941141Sperrin dmu_tx_commit(tx); 3951141Sperrin } 3961141Sperrin 3971141Sperrin return (error); 3981141Sperrin } 3991141Sperrin 4001141Sperrin /* ARGSUSED */ 4011141Sperrin static int 4021141Sperrin zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap) 4031141Sperrin { 4041141Sperrin return (ENOTSUP); 4051141Sperrin } 4061141Sperrin 4071141Sperrin /* 4081141Sperrin * Callback vectors for replaying records. 4091141Sperrin * Only TX_WRITE is needed for zvol. 4101141Sperrin */ 4111141Sperrin zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = { 4121141Sperrin zvol_replay_err, /* 0 no such transaction type */ 4131141Sperrin zvol_replay_err, /* TX_CREATE */ 4141141Sperrin zvol_replay_err, /* TX_MKDIR */ 4151141Sperrin zvol_replay_err, /* TX_MKXATTR */ 4161141Sperrin zvol_replay_err, /* TX_SYMLINK */ 4171141Sperrin zvol_replay_err, /* TX_REMOVE */ 4181141Sperrin zvol_replay_err, /* TX_RMDIR */ 4191141Sperrin zvol_replay_err, /* TX_LINK */ 4201141Sperrin zvol_replay_err, /* TX_RENAME */ 4211141Sperrin zvol_replay_write, /* TX_WRITE */ 4221141Sperrin zvol_replay_err, /* TX_TRUNCATE */ 4231141Sperrin zvol_replay_err, /* TX_SETATTR */ 4241141Sperrin zvol_replay_err, /* TX_ACL */ 42510800SNeil.Perrin@Sun.COM zvol_replay_err, /* TX_CREATE_ACL */ 42610800SNeil.Perrin@Sun.COM zvol_replay_err, /* TX_CREATE_ATTR */ 42710800SNeil.Perrin@Sun.COM zvol_replay_err, /* TX_CREATE_ACL_ATTR */ 42810800SNeil.Perrin@Sun.COM zvol_replay_err, /* TX_MKDIR_ACL */ 42910800SNeil.Perrin@Sun.COM zvol_replay_err, /* TX_MKDIR_ATTR */ 43010800SNeil.Perrin@Sun.COM zvol_replay_err, /* TX_MKDIR_ACL_ATTR */ 43110800SNeil.Perrin@Sun.COM zvol_replay_err, /* TX_WRITE2 */ 4321141Sperrin }; 4331141Sperrin 43410588SEric.Taylor@Sun.COM int 43510588SEric.Taylor@Sun.COM zvol_name2minor(const char *name, minor_t *minor) 43610588SEric.Taylor@Sun.COM { 43710588SEric.Taylor@Sun.COM zvol_state_t *zv; 43810588SEric.Taylor@Sun.COM 43910588SEric.Taylor@Sun.COM mutex_enter(&zvol_state_lock); 44010588SEric.Taylor@Sun.COM zv = zvol_minor_lookup(name); 44110588SEric.Taylor@Sun.COM if (minor && zv) 44210588SEric.Taylor@Sun.COM *minor = zv->zv_minor; 44310588SEric.Taylor@Sun.COM mutex_exit(&zvol_state_lock); 44410588SEric.Taylor@Sun.COM return (zv ? 0 : -1); 44510588SEric.Taylor@Sun.COM } 44610588SEric.Taylor@Sun.COM 4471141Sperrin /* 4486423Sgw25295 * Create a minor node (plus a whole lot more) for the specified volume. 449789Sahrens */ 450789Sahrens int 45110588SEric.Taylor@Sun.COM zvol_create_minor(const char *name) 452789Sahrens { 453789Sahrens zvol_state_t *zv; 454789Sahrens objset_t *os; 4553063Sperrin dmu_object_info_t doi; 456789Sahrens minor_t minor = 0; 457789Sahrens char chrbuf[30], blkbuf[30]; 458789Sahrens int error; 459789Sahrens 460789Sahrens mutex_enter(&zvol_state_lock); 461789Sahrens 46211422SMark.Musante@Sun.COM if (zvol_minor_lookup(name) != NULL) { 463789Sahrens mutex_exit(&zvol_state_lock); 464789Sahrens return (EEXIST); 465789Sahrens } 466789Sahrens 46710298SMatthew.Ahrens@Sun.COM /* lie and say we're read-only */ 46810298SMatthew.Ahrens@Sun.COM error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, zvol_tag, &os); 469789Sahrens 470789Sahrens if (error) { 471789Sahrens mutex_exit(&zvol_state_lock); 472789Sahrens return (error); 473789Sahrens } 474789Sahrens 47510588SEric.Taylor@Sun.COM if ((minor = zvol_minor_alloc()) == 0) { 47610298SMatthew.Ahrens@Sun.COM dmu_objset_disown(os, zvol_tag); 477789Sahrens mutex_exit(&zvol_state_lock); 478789Sahrens return (ENXIO); 479789Sahrens } 480789Sahrens 481789Sahrens if (ddi_soft_state_zalloc(zvol_state, minor) != DDI_SUCCESS) { 48210298SMatthew.Ahrens@Sun.COM dmu_objset_disown(os, zvol_tag); 483789Sahrens mutex_exit(&zvol_state_lock); 484789Sahrens return (EAGAIN); 485789Sahrens } 4862676Seschrock (void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME, 4872676Seschrock (char *)name); 488789Sahrens 48910588SEric.Taylor@Sun.COM (void) snprintf(chrbuf, sizeof (chrbuf), "%u,raw", minor); 490789Sahrens 491789Sahrens if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR, 492789Sahrens minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 493789Sahrens ddi_soft_state_free(zvol_state, minor); 49410298SMatthew.Ahrens@Sun.COM dmu_objset_disown(os, zvol_tag); 495789Sahrens mutex_exit(&zvol_state_lock); 496789Sahrens return (EAGAIN); 497789Sahrens } 498789Sahrens 49910588SEric.Taylor@Sun.COM (void) snprintf(blkbuf, sizeof (blkbuf), "%u", minor); 500789Sahrens 501789Sahrens if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK, 502789Sahrens minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 503789Sahrens ddi_remove_minor_node(zfs_dip, chrbuf); 504789Sahrens ddi_soft_state_free(zvol_state, minor); 50510298SMatthew.Ahrens@Sun.COM dmu_objset_disown(os, zvol_tag); 506789Sahrens mutex_exit(&zvol_state_lock); 507789Sahrens return (EAGAIN); 508789Sahrens } 509789Sahrens 510789Sahrens zv = ddi_get_soft_state(zvol_state, minor); 511789Sahrens 51210588SEric.Taylor@Sun.COM (void) strlcpy(zv->zv_name, name, MAXPATHLEN); 513789Sahrens zv->zv_min_bs = DEV_BSHIFT; 514789Sahrens zv->zv_minor = minor; 515789Sahrens zv->zv_objset = os; 51610588SEric.Taylor@Sun.COM if (dmu_objset_is_snapshot(os)) 51710588SEric.Taylor@Sun.COM zv->zv_flags |= ZVOL_RDONLY; 5183755Sperrin mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL); 5193755Sperrin avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare, 5203755Sperrin sizeof (rl_t), offsetof(rl_t, r_node)); 5217837SMatthew.Ahrens@Sun.COM list_create(&zv->zv_extents, sizeof (zvol_extent_t), 5227837SMatthew.Ahrens@Sun.COM offsetof(zvol_extent_t, ze_node)); 5233063Sperrin /* get and cache the blocksize */ 5243063Sperrin error = dmu_object_info(os, ZVOL_OBJ, &doi); 5253063Sperrin ASSERT(error == 0); 5263063Sperrin zv->zv_volblocksize = doi.doi_data_block_size; 5271141Sperrin 5288227SNeil.Perrin@Sun.COM zil_replay(os, zv, zvol_replay_vector); 52910588SEric.Taylor@Sun.COM dmu_objset_disown(os, zvol_tag); 53010588SEric.Taylor@Sun.COM zv->zv_objset = NULL; 531789Sahrens 532789Sahrens zvol_minors++; 533789Sahrens 534789Sahrens mutex_exit(&zvol_state_lock); 535789Sahrens 536789Sahrens return (0); 537789Sahrens } 538789Sahrens 539789Sahrens /* 540789Sahrens * Remove minor node for the specified volume. 541789Sahrens */ 54210588SEric.Taylor@Sun.COM static int 54310588SEric.Taylor@Sun.COM zvol_remove_zv(zvol_state_t *zv) 544789Sahrens { 54510588SEric.Taylor@Sun.COM char nmbuf[20]; 546789Sahrens 54710588SEric.Taylor@Sun.COM ASSERT(MUTEX_HELD(&zvol_state_lock)); 54810588SEric.Taylor@Sun.COM if (zv->zv_total_opens != 0) 549789Sahrens return (EBUSY); 550789Sahrens 55110588SEric.Taylor@Sun.COM (void) snprintf(nmbuf, sizeof (nmbuf), "%u,raw", zv->zv_minor); 55210588SEric.Taylor@Sun.COM ddi_remove_minor_node(zfs_dip, nmbuf); 553789Sahrens 55410588SEric.Taylor@Sun.COM (void) snprintf(nmbuf, sizeof (nmbuf), "%u", zv->zv_minor); 55510588SEric.Taylor@Sun.COM ddi_remove_minor_node(zfs_dip, nmbuf); 556789Sahrens 5573755Sperrin avl_destroy(&zv->zv_znode.z_range_avl); 5583755Sperrin mutex_destroy(&zv->zv_znode.z_range_lock); 559789Sahrens 560789Sahrens ddi_soft_state_free(zvol_state, zv->zv_minor); 561789Sahrens 562789Sahrens zvol_minors--; 56310588SEric.Taylor@Sun.COM return (0); 56410588SEric.Taylor@Sun.COM } 565789Sahrens 56610588SEric.Taylor@Sun.COM int 56710588SEric.Taylor@Sun.COM zvol_remove_minor(const char *name) 56810588SEric.Taylor@Sun.COM { 56910588SEric.Taylor@Sun.COM zvol_state_t *zv; 57010588SEric.Taylor@Sun.COM int rc; 57110588SEric.Taylor@Sun.COM 57210588SEric.Taylor@Sun.COM mutex_enter(&zvol_state_lock); 57310588SEric.Taylor@Sun.COM if ((zv = zvol_minor_lookup(name)) == NULL) { 57410588SEric.Taylor@Sun.COM mutex_exit(&zvol_state_lock); 57510588SEric.Taylor@Sun.COM return (ENXIO); 57610588SEric.Taylor@Sun.COM } 57710588SEric.Taylor@Sun.COM rc = zvol_remove_zv(zv); 578789Sahrens mutex_exit(&zvol_state_lock); 57910588SEric.Taylor@Sun.COM return (rc); 58010588SEric.Taylor@Sun.COM } 581789Sahrens 58210588SEric.Taylor@Sun.COM int 58310588SEric.Taylor@Sun.COM zvol_first_open(zvol_state_t *zv) 58410588SEric.Taylor@Sun.COM { 58510588SEric.Taylor@Sun.COM objset_t *os; 58610588SEric.Taylor@Sun.COM uint64_t volsize; 58710588SEric.Taylor@Sun.COM int error; 58810588SEric.Taylor@Sun.COM uint64_t readonly; 58910588SEric.Taylor@Sun.COM 59010588SEric.Taylor@Sun.COM /* lie and say we're read-only */ 59110588SEric.Taylor@Sun.COM error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, B_TRUE, 59210588SEric.Taylor@Sun.COM zvol_tag, &os); 59310588SEric.Taylor@Sun.COM if (error) 59410588SEric.Taylor@Sun.COM return (error); 59510588SEric.Taylor@Sun.COM 59610588SEric.Taylor@Sun.COM error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize); 59710588SEric.Taylor@Sun.COM if (error) { 59810588SEric.Taylor@Sun.COM ASSERT(error == 0); 59910588SEric.Taylor@Sun.COM dmu_objset_disown(os, zvol_tag); 60010588SEric.Taylor@Sun.COM return (error); 60110588SEric.Taylor@Sun.COM } 60210588SEric.Taylor@Sun.COM zv->zv_objset = os; 60312123STim.Haley@Sun.COM error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf); 60412123STim.Haley@Sun.COM if (error) { 60512123STim.Haley@Sun.COM dmu_objset_disown(os, zvol_tag); 60612123STim.Haley@Sun.COM return (error); 60712123STim.Haley@Sun.COM } 60810588SEric.Taylor@Sun.COM zv->zv_volsize = volsize; 60910588SEric.Taylor@Sun.COM zv->zv_zilog = zil_open(os, zvol_get_data); 61010588SEric.Taylor@Sun.COM zvol_size_changed(zv->zv_volsize, ddi_driver_major(zfs_dip), 61110588SEric.Taylor@Sun.COM zv->zv_minor); 61210588SEric.Taylor@Sun.COM 61310588SEric.Taylor@Sun.COM VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &readonly, 61410588SEric.Taylor@Sun.COM NULL) == 0); 61510588SEric.Taylor@Sun.COM if (readonly || dmu_objset_is_snapshot(os)) 61610588SEric.Taylor@Sun.COM zv->zv_flags |= ZVOL_RDONLY; 61710588SEric.Taylor@Sun.COM else 61810588SEric.Taylor@Sun.COM zv->zv_flags &= ~ZVOL_RDONLY; 61910588SEric.Taylor@Sun.COM return (error); 62010588SEric.Taylor@Sun.COM } 62110588SEric.Taylor@Sun.COM 62210588SEric.Taylor@Sun.COM void 62310588SEric.Taylor@Sun.COM zvol_last_close(zvol_state_t *zv) 62410588SEric.Taylor@Sun.COM { 62510588SEric.Taylor@Sun.COM zil_close(zv->zv_zilog); 62610588SEric.Taylor@Sun.COM zv->zv_zilog = NULL; 62712123STim.Haley@Sun.COM dmu_buf_rele(zv->zv_dbuf, zvol_tag); 62812123STim.Haley@Sun.COM zv->zv_dbuf = NULL; 62910588SEric.Taylor@Sun.COM dmu_objset_disown(zv->zv_objset, zvol_tag); 63010588SEric.Taylor@Sun.COM zv->zv_objset = NULL; 631789Sahrens } 632789Sahrens 6336423Sgw25295 int 6346423Sgw25295 zvol_prealloc(zvol_state_t *zv) 6356423Sgw25295 { 6366423Sgw25295 objset_t *os = zv->zv_objset; 6376423Sgw25295 dmu_tx_t *tx; 6386423Sgw25295 uint64_t refd, avail, usedobjs, availobjs; 6396423Sgw25295 uint64_t resid = zv->zv_volsize; 6406423Sgw25295 uint64_t off = 0; 6416423Sgw25295 6426423Sgw25295 /* Check the space usage before attempting to allocate the space */ 6436423Sgw25295 dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs); 6446423Sgw25295 if (avail < zv->zv_volsize) 6456423Sgw25295 return (ENOSPC); 6466423Sgw25295 6476423Sgw25295 /* Free old extents if they exist */ 6486423Sgw25295 zvol_free_extents(zv); 6496423Sgw25295 6506423Sgw25295 while (resid != 0) { 6516423Sgw25295 int error; 6526423Sgw25295 uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE); 6536423Sgw25295 6546423Sgw25295 tx = dmu_tx_create(os); 6556423Sgw25295 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 6566423Sgw25295 error = dmu_tx_assign(tx, TXG_WAIT); 6576423Sgw25295 if (error) { 6586423Sgw25295 dmu_tx_abort(tx); 6596992Smaybee (void) dmu_free_long_range(os, ZVOL_OBJ, 0, off); 6606423Sgw25295 return (error); 6616423Sgw25295 } 6627872STim.Haley@Sun.COM dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx); 6636423Sgw25295 dmu_tx_commit(tx); 6646423Sgw25295 off += bytes; 6656423Sgw25295 resid -= bytes; 6666423Sgw25295 } 6676423Sgw25295 txg_wait_synced(dmu_objset_pool(os), 0); 6686423Sgw25295 6696423Sgw25295 return (0); 6706423Sgw25295 } 6716423Sgw25295 6726423Sgw25295 int 67310588SEric.Taylor@Sun.COM zvol_update_volsize(objset_t *os, uint64_t volsize) 6746423Sgw25295 { 6756423Sgw25295 dmu_tx_t *tx; 6766423Sgw25295 int error; 6776423Sgw25295 6786423Sgw25295 ASSERT(MUTEX_HELD(&zvol_state_lock)); 6796423Sgw25295 68010588SEric.Taylor@Sun.COM tx = dmu_tx_create(os); 6816423Sgw25295 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 6826423Sgw25295 error = dmu_tx_assign(tx, TXG_WAIT); 6836423Sgw25295 if (error) { 6846423Sgw25295 dmu_tx_abort(tx); 6856423Sgw25295 return (error); 6866423Sgw25295 } 6876423Sgw25295 68810588SEric.Taylor@Sun.COM error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, 6896423Sgw25295 &volsize, tx); 6906423Sgw25295 dmu_tx_commit(tx); 6916423Sgw25295 6926423Sgw25295 if (error == 0) 69310588SEric.Taylor@Sun.COM error = dmu_free_long_range(os, 6946992Smaybee ZVOL_OBJ, volsize, DMU_OBJECT_END); 69510588SEric.Taylor@Sun.COM return (error); 69610588SEric.Taylor@Sun.COM } 69710588SEric.Taylor@Sun.COM 69810588SEric.Taylor@Sun.COM void 69910588SEric.Taylor@Sun.COM zvol_remove_minors(const char *name) 70010588SEric.Taylor@Sun.COM { 70110588SEric.Taylor@Sun.COM zvol_state_t *zv; 70210588SEric.Taylor@Sun.COM char *namebuf; 70310588SEric.Taylor@Sun.COM minor_t minor; 7046423Sgw25295 70510588SEric.Taylor@Sun.COM namebuf = kmem_zalloc(strlen(name) + 2, KM_SLEEP); 70610588SEric.Taylor@Sun.COM (void) strncpy(namebuf, name, strlen(name)); 70710588SEric.Taylor@Sun.COM (void) strcat(namebuf, "/"); 70810588SEric.Taylor@Sun.COM mutex_enter(&zvol_state_lock); 70910588SEric.Taylor@Sun.COM for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) { 71010588SEric.Taylor@Sun.COM 71110588SEric.Taylor@Sun.COM zv = ddi_get_soft_state(zvol_state, minor); 71210588SEric.Taylor@Sun.COM if (zv == NULL) 71310588SEric.Taylor@Sun.COM continue; 71410588SEric.Taylor@Sun.COM if (strncmp(namebuf, zv->zv_name, strlen(namebuf)) == 0) 71510588SEric.Taylor@Sun.COM (void) zvol_remove_zv(zv); 7166423Sgw25295 } 71710588SEric.Taylor@Sun.COM kmem_free(namebuf, strlen(name) + 2); 71810588SEric.Taylor@Sun.COM 71910588SEric.Taylor@Sun.COM mutex_exit(&zvol_state_lock); 7206423Sgw25295 } 7216423Sgw25295 722789Sahrens int 7234787Sahrens zvol_set_volsize(const char *name, major_t maj, uint64_t volsize) 724789Sahrens { 72510588SEric.Taylor@Sun.COM zvol_state_t *zv = NULL; 72610588SEric.Taylor@Sun.COM objset_t *os; 727789Sahrens int error; 7281133Seschrock dmu_object_info_t doi; 7296423Sgw25295 uint64_t old_volsize = 0ULL; 73010588SEric.Taylor@Sun.COM uint64_t readonly; 731789Sahrens 732789Sahrens mutex_enter(&zvol_state_lock); 73310588SEric.Taylor@Sun.COM zv = zvol_minor_lookup(name); 73410588SEric.Taylor@Sun.COM if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) { 73510588SEric.Taylor@Sun.COM mutex_exit(&zvol_state_lock); 73610588SEric.Taylor@Sun.COM return (error); 73710588SEric.Taylor@Sun.COM } 738789Sahrens 73910588SEric.Taylor@Sun.COM if ((error = dmu_object_info(os, ZVOL_OBJ, &doi)) != 0 || 7402676Seschrock (error = zvol_check_volsize(volsize, 7417265Sahrens doi.doi_data_block_size)) != 0) 7427265Sahrens goto out; 7431133Seschrock 74410588SEric.Taylor@Sun.COM VERIFY(dsl_prop_get_integer(name, "readonly", &readonly, 74510588SEric.Taylor@Sun.COM NULL) == 0); 74610588SEric.Taylor@Sun.COM if (readonly) { 7477265Sahrens error = EROFS; 7487265Sahrens goto out; 749789Sahrens } 750789Sahrens 75110588SEric.Taylor@Sun.COM error = zvol_update_volsize(os, volsize); 7526423Sgw25295 /* 7536423Sgw25295 * Reinitialize the dump area to the new size. If we 75410588SEric.Taylor@Sun.COM * failed to resize the dump area then restore it back to 75510588SEric.Taylor@Sun.COM * its original size. 7566423Sgw25295 */ 75710588SEric.Taylor@Sun.COM if (zv && error == 0) { 75810588SEric.Taylor@Sun.COM if (zv->zv_flags & ZVOL_DUMPIFIED) { 75910588SEric.Taylor@Sun.COM old_volsize = zv->zv_volsize; 76010588SEric.Taylor@Sun.COM zv->zv_volsize = volsize; 76110588SEric.Taylor@Sun.COM if ((error = zvol_dumpify(zv)) != 0 || 76210588SEric.Taylor@Sun.COM (error = dumpvp_resize()) != 0) { 76310588SEric.Taylor@Sun.COM (void) zvol_update_volsize(os, old_volsize); 76410588SEric.Taylor@Sun.COM zv->zv_volsize = old_volsize; 76510588SEric.Taylor@Sun.COM error = zvol_dumpify(zv); 76610588SEric.Taylor@Sun.COM } 76710588SEric.Taylor@Sun.COM } 76810588SEric.Taylor@Sun.COM if (error == 0) { 76910588SEric.Taylor@Sun.COM zv->zv_volsize = volsize; 77010588SEric.Taylor@Sun.COM zvol_size_changed(volsize, maj, zv->zv_minor); 7716423Sgw25295 } 772789Sahrens } 773789Sahrens 7749816SGeorge.Wilson@Sun.COM /* 7759816SGeorge.Wilson@Sun.COM * Generate a LUN expansion event. 7769816SGeorge.Wilson@Sun.COM */ 77710588SEric.Taylor@Sun.COM if (zv && error == 0) { 7789816SGeorge.Wilson@Sun.COM sysevent_id_t eid; 7799816SGeorge.Wilson@Sun.COM nvlist_t *attr; 7809816SGeorge.Wilson@Sun.COM char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 7819816SGeorge.Wilson@Sun.COM 78210588SEric.Taylor@Sun.COM (void) snprintf(physpath, MAXPATHLEN, "%s%u", ZVOL_PSEUDO_DEV, 7839816SGeorge.Wilson@Sun.COM zv->zv_minor); 7849816SGeorge.Wilson@Sun.COM 7859816SGeorge.Wilson@Sun.COM VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 7869816SGeorge.Wilson@Sun.COM VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 7879816SGeorge.Wilson@Sun.COM 7889816SGeorge.Wilson@Sun.COM (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 7899816SGeorge.Wilson@Sun.COM ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 7909816SGeorge.Wilson@Sun.COM 7919816SGeorge.Wilson@Sun.COM nvlist_free(attr); 7929816SGeorge.Wilson@Sun.COM kmem_free(physpath, MAXPATHLEN); 7939816SGeorge.Wilson@Sun.COM } 7949816SGeorge.Wilson@Sun.COM 7957265Sahrens out: 79610588SEric.Taylor@Sun.COM dmu_objset_rele(os, FTAG); 7977265Sahrens 798789Sahrens mutex_exit(&zvol_state_lock); 799789Sahrens 800789Sahrens return (error); 801789Sahrens } 802789Sahrens 803789Sahrens /*ARGSUSED*/ 804789Sahrens int 805789Sahrens zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr) 806789Sahrens { 807789Sahrens minor_t minor = getminor(*devp); 808789Sahrens zvol_state_t *zv; 80910588SEric.Taylor@Sun.COM int err = 0; 810789Sahrens 811789Sahrens if (minor == 0) /* This is the control device */ 812789Sahrens return (0); 813789Sahrens 814789Sahrens mutex_enter(&zvol_state_lock); 815789Sahrens 816789Sahrens zv = ddi_get_soft_state(zvol_state, minor); 817789Sahrens if (zv == NULL) { 818789Sahrens mutex_exit(&zvol_state_lock); 819789Sahrens return (ENXIO); 820789Sahrens } 821789Sahrens 82210588SEric.Taylor@Sun.COM if (zv->zv_total_opens == 0) 82310588SEric.Taylor@Sun.COM err = zvol_first_open(zv); 82410588SEric.Taylor@Sun.COM if (err) { 825789Sahrens mutex_exit(&zvol_state_lock); 82610588SEric.Taylor@Sun.COM return (err); 82710588SEric.Taylor@Sun.COM } 82810588SEric.Taylor@Sun.COM if ((flag & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) { 82910588SEric.Taylor@Sun.COM err = EROFS; 83010588SEric.Taylor@Sun.COM goto out; 831789Sahrens } 8327405SEric.Taylor@Sun.COM if (zv->zv_flags & ZVOL_EXCL) { 83310588SEric.Taylor@Sun.COM err = EBUSY; 83410588SEric.Taylor@Sun.COM goto out; 8357405SEric.Taylor@Sun.COM } 8367405SEric.Taylor@Sun.COM if (flag & FEXCL) { 8377405SEric.Taylor@Sun.COM if (zv->zv_total_opens != 0) { 83810588SEric.Taylor@Sun.COM err = EBUSY; 83910588SEric.Taylor@Sun.COM goto out; 8407405SEric.Taylor@Sun.COM } 8417405SEric.Taylor@Sun.COM zv->zv_flags |= ZVOL_EXCL; 8427405SEric.Taylor@Sun.COM } 843789Sahrens 844789Sahrens if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) { 845789Sahrens zv->zv_open_count[otyp]++; 846789Sahrens zv->zv_total_opens++; 847789Sahrens } 848789Sahrens mutex_exit(&zvol_state_lock); 849789Sahrens 85010588SEric.Taylor@Sun.COM return (err); 85110588SEric.Taylor@Sun.COM out: 85210588SEric.Taylor@Sun.COM if (zv->zv_total_opens == 0) 85310588SEric.Taylor@Sun.COM zvol_last_close(zv); 85410588SEric.Taylor@Sun.COM mutex_exit(&zvol_state_lock); 85510588SEric.Taylor@Sun.COM return (err); 856789Sahrens } 857789Sahrens 858789Sahrens /*ARGSUSED*/ 859789Sahrens int 860789Sahrens zvol_close(dev_t dev, int flag, int otyp, cred_t *cr) 861789Sahrens { 862789Sahrens minor_t minor = getminor(dev); 863789Sahrens zvol_state_t *zv; 86410588SEric.Taylor@Sun.COM int error = 0; 865789Sahrens 866789Sahrens if (minor == 0) /* This is the control device */ 867789Sahrens return (0); 868789Sahrens 869789Sahrens mutex_enter(&zvol_state_lock); 870789Sahrens 871789Sahrens zv = ddi_get_soft_state(zvol_state, minor); 872789Sahrens if (zv == NULL) { 873789Sahrens mutex_exit(&zvol_state_lock); 874789Sahrens return (ENXIO); 875789Sahrens } 876789Sahrens 8777405SEric.Taylor@Sun.COM if (zv->zv_flags & ZVOL_EXCL) { 8787405SEric.Taylor@Sun.COM ASSERT(zv->zv_total_opens == 1); 8797405SEric.Taylor@Sun.COM zv->zv_flags &= ~ZVOL_EXCL; 880789Sahrens } 881789Sahrens 882789Sahrens /* 883789Sahrens * If the open count is zero, this is a spurious close. 884789Sahrens * That indicates a bug in the kernel / DDI framework. 885789Sahrens */ 886789Sahrens ASSERT(zv->zv_open_count[otyp] != 0); 887789Sahrens ASSERT(zv->zv_total_opens != 0); 888789Sahrens 889789Sahrens /* 890789Sahrens * You may get multiple opens, but only one close. 891789Sahrens */ 892789Sahrens zv->zv_open_count[otyp]--; 893789Sahrens zv->zv_total_opens--; 894789Sahrens 89510588SEric.Taylor@Sun.COM if (zv->zv_total_opens == 0) 89610588SEric.Taylor@Sun.COM zvol_last_close(zv); 897789Sahrens 89810588SEric.Taylor@Sun.COM mutex_exit(&zvol_state_lock); 89910588SEric.Taylor@Sun.COM return (error); 900789Sahrens } 901789Sahrens 9023638Sbillm static void 90310922SJeff.Bonwick@Sun.COM zvol_get_done(zgd_t *zgd, int error) 9043063Sperrin { 90510922SJeff.Bonwick@Sun.COM if (zgd->zgd_db) 90610922SJeff.Bonwick@Sun.COM dmu_buf_rele(zgd->zgd_db, zgd); 9073063Sperrin 90810922SJeff.Bonwick@Sun.COM zfs_range_unlock(zgd->zgd_rl); 90910922SJeff.Bonwick@Sun.COM 91010922SJeff.Bonwick@Sun.COM if (error == 0 && zgd->zgd_bp) 91110922SJeff.Bonwick@Sun.COM zil_add_block(zgd->zgd_zilog, zgd->zgd_bp); 91210922SJeff.Bonwick@Sun.COM 9133063Sperrin kmem_free(zgd, sizeof (zgd_t)); 9143063Sperrin } 9153063Sperrin 9163063Sperrin /* 9173063Sperrin * Get data to generate a TX_WRITE intent log record. 9183063Sperrin */ 9193638Sbillm static int 9203063Sperrin zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 9213063Sperrin { 9223063Sperrin zvol_state_t *zv = arg; 9233063Sperrin objset_t *os = zv->zv_objset; 92410922SJeff.Bonwick@Sun.COM uint64_t object = ZVOL_OBJ; 92510922SJeff.Bonwick@Sun.COM uint64_t offset = lr->lr_offset; 92610922SJeff.Bonwick@Sun.COM uint64_t size = lr->lr_length; /* length of user data */ 92710922SJeff.Bonwick@Sun.COM blkptr_t *bp = &lr->lr_blkptr; 9283063Sperrin dmu_buf_t *db; 9293063Sperrin zgd_t *zgd; 9303063Sperrin int error; 9313063Sperrin 93210922SJeff.Bonwick@Sun.COM ASSERT(zio != NULL); 93310922SJeff.Bonwick@Sun.COM ASSERT(size != 0); 93410922SJeff.Bonwick@Sun.COM 93510922SJeff.Bonwick@Sun.COM zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP); 93610922SJeff.Bonwick@Sun.COM zgd->zgd_zilog = zv->zv_zilog; 93710922SJeff.Bonwick@Sun.COM zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER); 9383638Sbillm 9393755Sperrin /* 9403755Sperrin * Write records come in two flavors: immediate and indirect. 9413755Sperrin * For small writes it's cheaper to store the data with the 9423755Sperrin * log record (immediate); for large writes it's cheaper to 9433755Sperrin * sync the data and get a pointer to it (indirect) so that 9443755Sperrin * we don't have to write the data twice. 9453755Sperrin */ 94610922SJeff.Bonwick@Sun.COM if (buf != NULL) { /* immediate write */ 94710922SJeff.Bonwick@Sun.COM error = dmu_read(os, object, offset, size, buf, 94810922SJeff.Bonwick@Sun.COM DMU_READ_NO_PREFETCH); 94910922SJeff.Bonwick@Sun.COM } else { 95010922SJeff.Bonwick@Sun.COM size = zv->zv_volblocksize; 95110922SJeff.Bonwick@Sun.COM offset = P2ALIGN(offset, size); 952*12285SJeff.Bonwick@Sun.COM error = dmu_buf_hold(os, object, offset, zgd, &db, 953*12285SJeff.Bonwick@Sun.COM DMU_READ_NO_PREFETCH); 95410922SJeff.Bonwick@Sun.COM if (error == 0) { 95510922SJeff.Bonwick@Sun.COM zgd->zgd_db = db; 95610922SJeff.Bonwick@Sun.COM zgd->zgd_bp = bp; 9573063Sperrin 95810922SJeff.Bonwick@Sun.COM ASSERT(db->db_offset == offset); 95910922SJeff.Bonwick@Sun.COM ASSERT(db->db_size == size); 9603755Sperrin 96110922SJeff.Bonwick@Sun.COM error = dmu_sync(zio, lr->lr_common.lrc_txg, 96210922SJeff.Bonwick@Sun.COM zvol_get_done, zgd); 96310800SNeil.Perrin@Sun.COM 96410922SJeff.Bonwick@Sun.COM if (error == 0) 96510922SJeff.Bonwick@Sun.COM return (0); 96610922SJeff.Bonwick@Sun.COM } 96710800SNeil.Perrin@Sun.COM } 96810800SNeil.Perrin@Sun.COM 96910922SJeff.Bonwick@Sun.COM zvol_get_done(zgd, error); 97010922SJeff.Bonwick@Sun.COM 9713063Sperrin return (error); 9723063Sperrin } 9733063Sperrin 9741861Sperrin /* 9751861Sperrin * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions. 9761141Sperrin * 9771141Sperrin * We store data in the log buffers if it's small enough. 9783063Sperrin * Otherwise we will later flush the data out via dmu_sync(). 9791141Sperrin */ 9803063Sperrin ssize_t zvol_immediate_write_sz = 32768; 9811141Sperrin 9823638Sbillm static void 9839401SNeil.Perrin@Sun.COM zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid, 9849401SNeil.Perrin@Sun.COM boolean_t sync) 9851141Sperrin { 9863638Sbillm uint32_t blocksize = zv->zv_volblocksize; 9878227SNeil.Perrin@Sun.COM zilog_t *zilog = zv->zv_zilog; 9889401SNeil.Perrin@Sun.COM boolean_t slogging; 98910310SNeil.Perrin@Sun.COM ssize_t immediate_write_sz; 9909401SNeil.Perrin@Sun.COM 9919401SNeil.Perrin@Sun.COM if (zil_disable) 9929401SNeil.Perrin@Sun.COM return; 9931861Sperrin 99410922SJeff.Bonwick@Sun.COM if (zil_replaying(zilog, tx)) 9958227SNeil.Perrin@Sun.COM return; 9968227SNeil.Perrin@Sun.COM 99710310SNeil.Perrin@Sun.COM immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT) 99810310SNeil.Perrin@Sun.COM ? 0 : zvol_immediate_write_sz; 99910310SNeil.Perrin@Sun.COM 100010310SNeil.Perrin@Sun.COM slogging = spa_has_slogs(zilog->zl_spa) && 100110310SNeil.Perrin@Sun.COM (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY); 10029401SNeil.Perrin@Sun.COM 10039401SNeil.Perrin@Sun.COM while (resid) { 10049401SNeil.Perrin@Sun.COM itx_t *itx; 10059401SNeil.Perrin@Sun.COM lr_write_t *lr; 10069401SNeil.Perrin@Sun.COM ssize_t len; 10079401SNeil.Perrin@Sun.COM itx_wr_state_t write_state; 10083638Sbillm 10099401SNeil.Perrin@Sun.COM /* 10109401SNeil.Perrin@Sun.COM * Unlike zfs_log_write() we can be called with 10119401SNeil.Perrin@Sun.COM * upto DMU_MAX_ACCESS/2 (5MB) writes. 10129401SNeil.Perrin@Sun.COM */ 101310310SNeil.Perrin@Sun.COM if (blocksize > immediate_write_sz && !slogging && 10149401SNeil.Perrin@Sun.COM resid >= blocksize && off % blocksize == 0) { 10159401SNeil.Perrin@Sun.COM write_state = WR_INDIRECT; /* uses dmu_sync */ 10169401SNeil.Perrin@Sun.COM len = blocksize; 10179401SNeil.Perrin@Sun.COM } else if (sync) { 10189401SNeil.Perrin@Sun.COM write_state = WR_COPIED; 10199401SNeil.Perrin@Sun.COM len = MIN(ZIL_MAX_LOG_DATA, resid); 10209401SNeil.Perrin@Sun.COM } else { 10219401SNeil.Perrin@Sun.COM write_state = WR_NEED_COPY; 10229401SNeil.Perrin@Sun.COM len = MIN(ZIL_MAX_LOG_DATA, resid); 10239401SNeil.Perrin@Sun.COM } 10249401SNeil.Perrin@Sun.COM 10259401SNeil.Perrin@Sun.COM itx = zil_itx_create(TX_WRITE, sizeof (*lr) + 10269401SNeil.Perrin@Sun.COM (write_state == WR_COPIED ? len : 0)); 10273638Sbillm lr = (lr_write_t *)&itx->itx_lr; 10289401SNeil.Perrin@Sun.COM if (write_state == WR_COPIED && dmu_read(zv->zv_objset, 10299512SNeil.Perrin@Sun.COM ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) { 103010922SJeff.Bonwick@Sun.COM zil_itx_destroy(itx); 10319401SNeil.Perrin@Sun.COM itx = zil_itx_create(TX_WRITE, sizeof (*lr)); 10329401SNeil.Perrin@Sun.COM lr = (lr_write_t *)&itx->itx_lr; 10339401SNeil.Perrin@Sun.COM write_state = WR_NEED_COPY; 10349401SNeil.Perrin@Sun.COM } 10359401SNeil.Perrin@Sun.COM 10369401SNeil.Perrin@Sun.COM itx->itx_wr_state = write_state; 10379401SNeil.Perrin@Sun.COM if (write_state == WR_NEED_COPY) 10389401SNeil.Perrin@Sun.COM itx->itx_sod += len; 10393638Sbillm lr->lr_foid = ZVOL_OBJ; 10403638Sbillm lr->lr_offset = off; 10419401SNeil.Perrin@Sun.COM lr->lr_length = len; 104210922SJeff.Bonwick@Sun.COM lr->lr_blkoff = 0; 10433638Sbillm BP_ZERO(&lr->lr_blkptr); 10443638Sbillm 10459401SNeil.Perrin@Sun.COM itx->itx_private = zv; 10469401SNeil.Perrin@Sun.COM itx->itx_sync = sync; 10479401SNeil.Perrin@Sun.COM 10488227SNeil.Perrin@Sun.COM (void) zil_itx_assign(zilog, itx, tx); 10499401SNeil.Perrin@Sun.COM 10509401SNeil.Perrin@Sun.COM off += len; 10519401SNeil.Perrin@Sun.COM resid -= len; 10521141Sperrin } 10531141Sperrin } 10541141Sperrin 10557837SMatthew.Ahrens@Sun.COM static int 10567837SMatthew.Ahrens@Sun.COM zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t size, 10577837SMatthew.Ahrens@Sun.COM boolean_t doread, boolean_t isdump) 10586423Sgw25295 { 10596423Sgw25295 vdev_disk_t *dvd; 10606423Sgw25295 int c; 10616423Sgw25295 int numerrors = 0; 10626423Sgw25295 10636423Sgw25295 for (c = 0; c < vd->vdev_children; c++) { 10649790SLin.Ling@Sun.COM ASSERT(vd->vdev_ops == &vdev_mirror_ops || 10659790SLin.Ling@Sun.COM vd->vdev_ops == &vdev_replacing_ops || 10669790SLin.Ling@Sun.COM vd->vdev_ops == &vdev_spare_ops); 10677837SMatthew.Ahrens@Sun.COM int err = zvol_dumpio_vdev(vd->vdev_child[c], 10687837SMatthew.Ahrens@Sun.COM addr, offset, size, doread, isdump); 10697837SMatthew.Ahrens@Sun.COM if (err != 0) { 10706423Sgw25295 numerrors++; 10717837SMatthew.Ahrens@Sun.COM } else if (doread) { 10726423Sgw25295 break; 10736423Sgw25295 } 10746423Sgw25295 } 10756423Sgw25295 10766423Sgw25295 if (!vd->vdev_ops->vdev_op_leaf) 10776423Sgw25295 return (numerrors < vd->vdev_children ? 0 : EIO); 10786423Sgw25295 10797903SEric.Taylor@Sun.COM if (doread && !vdev_readable(vd)) 10807903SEric.Taylor@Sun.COM return (EIO); 10817903SEric.Taylor@Sun.COM else if (!doread && !vdev_writeable(vd)) 10826423Sgw25295 return (EIO); 10836423Sgw25295 10846423Sgw25295 dvd = vd->vdev_tsd; 10856423Sgw25295 ASSERT3P(dvd, !=, NULL); 10866423Sgw25295 offset += VDEV_LABEL_START_SIZE; 10876423Sgw25295 10886423Sgw25295 if (ddi_in_panic() || isdump) { 10897837SMatthew.Ahrens@Sun.COM ASSERT(!doread); 10907837SMatthew.Ahrens@Sun.COM if (doread) 10916423Sgw25295 return (EIO); 10926423Sgw25295 return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset), 10936423Sgw25295 lbtodb(size))); 10946423Sgw25295 } else { 10956423Sgw25295 return (vdev_disk_physio(dvd->vd_lh, addr, size, offset, 10967837SMatthew.Ahrens@Sun.COM doread ? B_READ : B_WRITE)); 10976423Sgw25295 } 10986423Sgw25295 } 10996423Sgw25295 11007837SMatthew.Ahrens@Sun.COM static int 11017837SMatthew.Ahrens@Sun.COM zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size, 11027837SMatthew.Ahrens@Sun.COM boolean_t doread, boolean_t isdump) 11036423Sgw25295 { 11046423Sgw25295 vdev_t *vd; 11056423Sgw25295 int error; 11067837SMatthew.Ahrens@Sun.COM zvol_extent_t *ze; 11076423Sgw25295 spa_t *spa = dmu_objset_spa(zv->zv_objset); 11086423Sgw25295 11097837SMatthew.Ahrens@Sun.COM /* Must be sector aligned, and not stradle a block boundary. */ 11107837SMatthew.Ahrens@Sun.COM if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) || 11117837SMatthew.Ahrens@Sun.COM P2BOUNDARY(offset, size, zv->zv_volblocksize)) { 11127837SMatthew.Ahrens@Sun.COM return (EINVAL); 11137837SMatthew.Ahrens@Sun.COM } 11146423Sgw25295 ASSERT(size <= zv->zv_volblocksize); 11156423Sgw25295 11167837SMatthew.Ahrens@Sun.COM /* Locate the extent this belongs to */ 11177837SMatthew.Ahrens@Sun.COM ze = list_head(&zv->zv_extents); 11187837SMatthew.Ahrens@Sun.COM while (offset >= ze->ze_nblks * zv->zv_volblocksize) { 11197837SMatthew.Ahrens@Sun.COM offset -= ze->ze_nblks * zv->zv_volblocksize; 11207837SMatthew.Ahrens@Sun.COM ze = list_next(&zv->zv_extents, ze); 11217837SMatthew.Ahrens@Sun.COM } 112211806SGeorge.Wilson@Sun.COM 112311806SGeorge.Wilson@Sun.COM if (!ddi_in_panic()) 112411806SGeorge.Wilson@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 112511806SGeorge.Wilson@Sun.COM 11267837SMatthew.Ahrens@Sun.COM vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva)); 11277837SMatthew.Ahrens@Sun.COM offset += DVA_GET_OFFSET(&ze->ze_dva); 11287837SMatthew.Ahrens@Sun.COM error = zvol_dumpio_vdev(vd, addr, offset, size, doread, isdump); 112911806SGeorge.Wilson@Sun.COM 113011806SGeorge.Wilson@Sun.COM if (!ddi_in_panic()) 113111806SGeorge.Wilson@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 113211806SGeorge.Wilson@Sun.COM 11336423Sgw25295 return (error); 11346423Sgw25295 } 11356423Sgw25295 11366423Sgw25295 int 1137789Sahrens zvol_strategy(buf_t *bp) 1138789Sahrens { 1139789Sahrens zvol_state_t *zv = ddi_get_soft_state(zvol_state, getminor(bp->b_edev)); 1140789Sahrens uint64_t off, volsize; 11417837SMatthew.Ahrens@Sun.COM size_t resid; 1142789Sahrens char *addr; 11431141Sperrin objset_t *os; 11443755Sperrin rl_t *rl; 1145789Sahrens int error = 0; 11467837SMatthew.Ahrens@Sun.COM boolean_t doread = bp->b_flags & B_READ; 114712095SChris.Kirby@sun.com boolean_t is_dump; 11489401SNeil.Perrin@Sun.COM boolean_t sync; 1149789Sahrens 1150789Sahrens if (zv == NULL) { 1151789Sahrens bioerror(bp, ENXIO); 1152789Sahrens biodone(bp); 1153789Sahrens return (0); 1154789Sahrens } 1155789Sahrens 1156789Sahrens if (getminor(bp->b_edev) == 0) { 1157789Sahrens bioerror(bp, EINVAL); 1158789Sahrens biodone(bp); 1159789Sahrens return (0); 1160789Sahrens } 1161789Sahrens 116210588SEric.Taylor@Sun.COM if (!(bp->b_flags & B_READ) && (zv->zv_flags & ZVOL_RDONLY)) { 1163789Sahrens bioerror(bp, EROFS); 1164789Sahrens biodone(bp); 1165789Sahrens return (0); 1166789Sahrens } 1167789Sahrens 1168789Sahrens off = ldbtob(bp->b_blkno); 1169789Sahrens volsize = zv->zv_volsize; 1170789Sahrens 11711141Sperrin os = zv->zv_objset; 11721141Sperrin ASSERT(os != NULL); 1173789Sahrens 1174789Sahrens bp_mapin(bp); 1175789Sahrens addr = bp->b_un.b_addr; 1176789Sahrens resid = bp->b_bcount; 1177789Sahrens 11787837SMatthew.Ahrens@Sun.COM if (resid > 0 && (off < 0 || off >= volsize)) { 11797837SMatthew.Ahrens@Sun.COM bioerror(bp, EIO); 11807837SMatthew.Ahrens@Sun.COM biodone(bp); 11817837SMatthew.Ahrens@Sun.COM return (0); 11827837SMatthew.Ahrens@Sun.COM } 11837013Sgw25295 118412095SChris.Kirby@sun.com is_dump = zv->zv_flags & ZVOL_DUMPIFIED; 11859401SNeil.Perrin@Sun.COM sync = !(bp->b_flags & B_ASYNC) && !doread && !is_dump && 11869401SNeil.Perrin@Sun.COM !(zv->zv_flags & ZVOL_WCE) && !zil_disable; 11879401SNeil.Perrin@Sun.COM 11881861Sperrin /* 11891861Sperrin * There must be no buffer changes when doing a dmu_sync() because 11901861Sperrin * we can't change the data whilst calculating the checksum. 11911861Sperrin */ 11923755Sperrin rl = zfs_range_lock(&zv->zv_znode, off, resid, 11937837SMatthew.Ahrens@Sun.COM doread ? RL_READER : RL_WRITER); 11946423Sgw25295 1195789Sahrens while (resid != 0 && off < volsize) { 11967837SMatthew.Ahrens@Sun.COM size_t size = MIN(resid, zvol_maxphys); 11976423Sgw25295 if (is_dump) { 11986423Sgw25295 size = MIN(size, P2END(off, zv->zv_volblocksize) - off); 11997837SMatthew.Ahrens@Sun.COM error = zvol_dumpio(zv, addr, off, size, 12007837SMatthew.Ahrens@Sun.COM doread, B_FALSE); 12017837SMatthew.Ahrens@Sun.COM } else if (doread) { 12029512SNeil.Perrin@Sun.COM error = dmu_read(os, ZVOL_OBJ, off, size, addr, 12039512SNeil.Perrin@Sun.COM DMU_READ_PREFETCH); 1204789Sahrens } else { 12051141Sperrin dmu_tx_t *tx = dmu_tx_create(os); 1206789Sahrens dmu_tx_hold_write(tx, ZVOL_OBJ, off, size); 1207789Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 1208789Sahrens if (error) { 1209789Sahrens dmu_tx_abort(tx); 1210789Sahrens } else { 12111141Sperrin dmu_write(os, ZVOL_OBJ, off, size, addr, tx); 12129401SNeil.Perrin@Sun.COM zvol_log_write(zv, tx, off, size, sync); 1213789Sahrens dmu_tx_commit(tx); 1214789Sahrens } 1215789Sahrens } 12167294Sperrin if (error) { 12177294Sperrin /* convert checksum errors into IO errors */ 12187294Sperrin if (error == ECKSUM) 12197294Sperrin error = EIO; 1220789Sahrens break; 12217294Sperrin } 1222789Sahrens off += size; 1223789Sahrens addr += size; 1224789Sahrens resid -= size; 1225789Sahrens } 12263755Sperrin zfs_range_unlock(rl); 1227789Sahrens 1228789Sahrens if ((bp->b_resid = resid) == bp->b_bcount) 1229789Sahrens bioerror(bp, off > volsize ? EINVAL : error); 1230789Sahrens 12319401SNeil.Perrin@Sun.COM if (sync) 12323638Sbillm zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ); 12333638Sbillm biodone(bp); 12341141Sperrin 1235789Sahrens return (0); 1236789Sahrens } 1237789Sahrens 12383063Sperrin /* 12393063Sperrin * Set the buffer count to the zvol maximum transfer. 12403063Sperrin * Using our own routine instead of the default minphys() 12413063Sperrin * means that for larger writes we write bigger buffers on X86 12423063Sperrin * (128K instead of 56K) and flush the disk write cache less often 12433063Sperrin * (every zvol_maxphys - currently 1MB) instead of minphys (currently 12443063Sperrin * 56K on X86 and 128K on sparc). 12453063Sperrin */ 12463063Sperrin void 12473063Sperrin zvol_minphys(struct buf *bp) 12483063Sperrin { 12493063Sperrin if (bp->b_bcount > zvol_maxphys) 12503063Sperrin bp->b_bcount = zvol_maxphys; 12513063Sperrin } 12523063Sperrin 12536423Sgw25295 int 12546423Sgw25295 zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks) 12556423Sgw25295 { 12566423Sgw25295 minor_t minor = getminor(dev); 12576423Sgw25295 zvol_state_t *zv; 12586423Sgw25295 int error = 0; 12596423Sgw25295 uint64_t size; 12606423Sgw25295 uint64_t boff; 12616423Sgw25295 uint64_t resid; 12626423Sgw25295 12636423Sgw25295 if (minor == 0) /* This is the control device */ 12646423Sgw25295 return (ENXIO); 12656423Sgw25295 12666423Sgw25295 zv = ddi_get_soft_state(zvol_state, minor); 12676423Sgw25295 if (zv == NULL) 12686423Sgw25295 return (ENXIO); 12696423Sgw25295 12706423Sgw25295 boff = ldbtob(blkno); 12716423Sgw25295 resid = ldbtob(nblocks); 12727837SMatthew.Ahrens@Sun.COM 12737837SMatthew.Ahrens@Sun.COM VERIFY3U(boff + resid, <=, zv->zv_volsize); 12747837SMatthew.Ahrens@Sun.COM 12756423Sgw25295 while (resid) { 12766423Sgw25295 size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff); 12777837SMatthew.Ahrens@Sun.COM error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE); 12786423Sgw25295 if (error) 12796423Sgw25295 break; 12806423Sgw25295 boff += size; 12816423Sgw25295 addr += size; 12826423Sgw25295 resid -= size; 12836423Sgw25295 } 12846423Sgw25295 12856423Sgw25295 return (error); 12866423Sgw25295 } 12876423Sgw25295 1288789Sahrens /*ARGSUSED*/ 1289789Sahrens int 12903638Sbillm zvol_read(dev_t dev, uio_t *uio, cred_t *cr) 1291789Sahrens { 12924107Sgw25295 minor_t minor = getminor(dev); 12934107Sgw25295 zvol_state_t *zv; 12947013Sgw25295 uint64_t volsize; 12953755Sperrin rl_t *rl; 12963638Sbillm int error = 0; 12973638Sbillm 12984107Sgw25295 if (minor == 0) /* This is the control device */ 12994107Sgw25295 return (ENXIO); 13004107Sgw25295 13014107Sgw25295 zv = ddi_get_soft_state(zvol_state, minor); 13024107Sgw25295 if (zv == NULL) 13034107Sgw25295 return (ENXIO); 13044107Sgw25295 13057013Sgw25295 volsize = zv->zv_volsize; 13067013Sgw25295 if (uio->uio_resid > 0 && 13077013Sgw25295 (uio->uio_loffset < 0 || uio->uio_loffset >= volsize)) 13087013Sgw25295 return (EIO); 13097013Sgw25295 13107837SMatthew.Ahrens@Sun.COM if (zv->zv_flags & ZVOL_DUMPIFIED) { 13117837SMatthew.Ahrens@Sun.COM error = physio(zvol_strategy, NULL, dev, B_READ, 13127837SMatthew.Ahrens@Sun.COM zvol_minphys, uio); 13137837SMatthew.Ahrens@Sun.COM return (error); 13147837SMatthew.Ahrens@Sun.COM } 13157837SMatthew.Ahrens@Sun.COM 13163755Sperrin rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 13173755Sperrin RL_READER); 13187013Sgw25295 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 13193638Sbillm uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 13203638Sbillm 13217013Sgw25295 /* don't read past the end */ 13227013Sgw25295 if (bytes > volsize - uio->uio_loffset) 13237013Sgw25295 bytes = volsize - uio->uio_loffset; 13247013Sgw25295 13253638Sbillm error = dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes); 13267294Sperrin if (error) { 13277294Sperrin /* convert checksum errors into IO errors */ 13287294Sperrin if (error == ECKSUM) 13297294Sperrin error = EIO; 13303638Sbillm break; 13317294Sperrin } 13323638Sbillm } 13333755Sperrin zfs_range_unlock(rl); 13343638Sbillm return (error); 1335789Sahrens } 1336789Sahrens 1337789Sahrens /*ARGSUSED*/ 1338789Sahrens int 13393638Sbillm zvol_write(dev_t dev, uio_t *uio, cred_t *cr) 1340789Sahrens { 13414107Sgw25295 minor_t minor = getminor(dev); 13424107Sgw25295 zvol_state_t *zv; 13437013Sgw25295 uint64_t volsize; 13443755Sperrin rl_t *rl; 13453638Sbillm int error = 0; 13469401SNeil.Perrin@Sun.COM boolean_t sync; 13473638Sbillm 13484107Sgw25295 if (minor == 0) /* This is the control device */ 13494107Sgw25295 return (ENXIO); 13504107Sgw25295 13514107Sgw25295 zv = ddi_get_soft_state(zvol_state, minor); 13524107Sgw25295 if (zv == NULL) 13534107Sgw25295 return (ENXIO); 13544107Sgw25295 13557013Sgw25295 volsize = zv->zv_volsize; 13567013Sgw25295 if (uio->uio_resid > 0 && 13577013Sgw25295 (uio->uio_loffset < 0 || uio->uio_loffset >= volsize)) 13587013Sgw25295 return (EIO); 13597013Sgw25295 13606423Sgw25295 if (zv->zv_flags & ZVOL_DUMPIFIED) { 13616423Sgw25295 error = physio(zvol_strategy, NULL, dev, B_WRITE, 13626423Sgw25295 zvol_minphys, uio); 13636423Sgw25295 return (error); 13646423Sgw25295 } 13656423Sgw25295 13669401SNeil.Perrin@Sun.COM sync = !(zv->zv_flags & ZVOL_WCE) && !zil_disable; 13679401SNeil.Perrin@Sun.COM 13683755Sperrin rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 13693755Sperrin RL_WRITER); 13707013Sgw25295 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 13713638Sbillm uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 13723638Sbillm uint64_t off = uio->uio_loffset; 13737013Sgw25295 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset); 1374789Sahrens 13757013Sgw25295 if (bytes > volsize - off) /* don't write past the end */ 13767013Sgw25295 bytes = volsize - off; 13777013Sgw25295 13783638Sbillm dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 13793638Sbillm error = dmu_tx_assign(tx, TXG_WAIT); 13803638Sbillm if (error) { 13813638Sbillm dmu_tx_abort(tx); 13823638Sbillm break; 13833638Sbillm } 138412123STim.Haley@Sun.COM error = dmu_write_uio_dbuf(zv->zv_dbuf, uio, bytes, tx); 13853638Sbillm if (error == 0) 13869401SNeil.Perrin@Sun.COM zvol_log_write(zv, tx, off, bytes, sync); 13873638Sbillm dmu_tx_commit(tx); 13883638Sbillm 13893638Sbillm if (error) 13903638Sbillm break; 13913638Sbillm } 13923755Sperrin zfs_range_unlock(rl); 13939401SNeil.Perrin@Sun.COM if (sync) 13948524SEric.Taylor@Sun.COM zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ); 13953638Sbillm return (error); 1396789Sahrens } 1397789Sahrens 13987405SEric.Taylor@Sun.COM int 13997405SEric.Taylor@Sun.COM zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs) 14007405SEric.Taylor@Sun.COM { 14017405SEric.Taylor@Sun.COM struct uuid uuid = EFI_RESERVED; 14027405SEric.Taylor@Sun.COM efi_gpe_t gpe = { 0 }; 14037405SEric.Taylor@Sun.COM uint32_t crc; 14047405SEric.Taylor@Sun.COM dk_efi_t efi; 14057405SEric.Taylor@Sun.COM int length; 14067405SEric.Taylor@Sun.COM char *ptr; 14077405SEric.Taylor@Sun.COM 14087405SEric.Taylor@Sun.COM if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag)) 14097405SEric.Taylor@Sun.COM return (EFAULT); 14107405SEric.Taylor@Sun.COM ptr = (char *)(uintptr_t)efi.dki_data_64; 14117405SEric.Taylor@Sun.COM length = efi.dki_length; 14127405SEric.Taylor@Sun.COM /* 14137405SEric.Taylor@Sun.COM * Some clients may attempt to request a PMBR for the 14147405SEric.Taylor@Sun.COM * zvol. Currently this interface will return EINVAL to 14157405SEric.Taylor@Sun.COM * such requests. These requests could be supported by 14167405SEric.Taylor@Sun.COM * adding a check for lba == 0 and consing up an appropriate 14177405SEric.Taylor@Sun.COM * PMBR. 14187405SEric.Taylor@Sun.COM */ 14197405SEric.Taylor@Sun.COM if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0) 14207405SEric.Taylor@Sun.COM return (EINVAL); 14217405SEric.Taylor@Sun.COM 14227405SEric.Taylor@Sun.COM gpe.efi_gpe_StartingLBA = LE_64(34ULL); 14237405SEric.Taylor@Sun.COM gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1); 14247405SEric.Taylor@Sun.COM UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid); 14257405SEric.Taylor@Sun.COM 14267405SEric.Taylor@Sun.COM if (efi.dki_lba == 1) { 14277405SEric.Taylor@Sun.COM efi_gpt_t gpt = { 0 }; 14287405SEric.Taylor@Sun.COM 14297405SEric.Taylor@Sun.COM gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE); 14307405SEric.Taylor@Sun.COM gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT); 14317405SEric.Taylor@Sun.COM gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt)); 14327405SEric.Taylor@Sun.COM gpt.efi_gpt_MyLBA = LE_64(1ULL); 14337405SEric.Taylor@Sun.COM gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL); 14347405SEric.Taylor@Sun.COM gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1); 14357405SEric.Taylor@Sun.COM gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL); 14367405SEric.Taylor@Sun.COM gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1); 14377405SEric.Taylor@Sun.COM gpt.efi_gpt_SizeOfPartitionEntry = 14387405SEric.Taylor@Sun.COM LE_32(sizeof (efi_gpe_t)); 14397405SEric.Taylor@Sun.COM CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table); 14407405SEric.Taylor@Sun.COM gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc); 14417405SEric.Taylor@Sun.COM CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table); 14427405SEric.Taylor@Sun.COM gpt.efi_gpt_HeaderCRC32 = LE_32(~crc); 14437405SEric.Taylor@Sun.COM if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length), 14447405SEric.Taylor@Sun.COM flag)) 14457405SEric.Taylor@Sun.COM return (EFAULT); 14467405SEric.Taylor@Sun.COM ptr += sizeof (gpt); 14477405SEric.Taylor@Sun.COM length -= sizeof (gpt); 14487405SEric.Taylor@Sun.COM } 14497405SEric.Taylor@Sun.COM if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe), 14507405SEric.Taylor@Sun.COM length), flag)) 14517405SEric.Taylor@Sun.COM return (EFAULT); 14527405SEric.Taylor@Sun.COM return (0); 14537405SEric.Taylor@Sun.COM } 14547405SEric.Taylor@Sun.COM 1455789Sahrens /* 1456789Sahrens * Dirtbag ioctls to support mkfs(1M) for UFS filesystems. See dkio(7I). 1457789Sahrens */ 1458789Sahrens /*ARGSUSED*/ 1459789Sahrens int 1460789Sahrens zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 1461789Sahrens { 1462789Sahrens zvol_state_t *zv; 14633897Smaybee struct dk_cinfo dki; 1464789Sahrens struct dk_minfo dkm; 14653897Smaybee struct dk_callback *dkc; 1466789Sahrens int error = 0; 14676423Sgw25295 rl_t *rl; 1468789Sahrens 1469789Sahrens mutex_enter(&zvol_state_lock); 1470789Sahrens 1471789Sahrens zv = ddi_get_soft_state(zvol_state, getminor(dev)); 1472789Sahrens 1473789Sahrens if (zv == NULL) { 1474789Sahrens mutex_exit(&zvol_state_lock); 1475789Sahrens return (ENXIO); 1476789Sahrens } 14779303SEric.Taylor@Sun.COM ASSERT(zv->zv_total_opens > 0); 1478789Sahrens 1479789Sahrens switch (cmd) { 1480789Sahrens 1481789Sahrens case DKIOCINFO: 14823897Smaybee bzero(&dki, sizeof (dki)); 14833897Smaybee (void) strcpy(dki.dki_cname, "zvol"); 14843897Smaybee (void) strcpy(dki.dki_dname, "zvol"); 14853897Smaybee dki.dki_ctype = DKC_UNKNOWN; 148611689SEric.Taylor@Sun.COM dki.dki_unit = getminor(dev); 14873897Smaybee dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs); 1488789Sahrens mutex_exit(&zvol_state_lock); 14893897Smaybee if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag)) 1490789Sahrens error = EFAULT; 1491789Sahrens return (error); 1492789Sahrens 1493789Sahrens case DKIOCGMEDIAINFO: 1494789Sahrens bzero(&dkm, sizeof (dkm)); 1495789Sahrens dkm.dki_lbsize = 1U << zv->zv_min_bs; 1496789Sahrens dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs; 1497789Sahrens dkm.dki_media_type = DK_UNKNOWN; 1498789Sahrens mutex_exit(&zvol_state_lock); 1499789Sahrens if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag)) 1500789Sahrens error = EFAULT; 1501789Sahrens return (error); 1502789Sahrens 1503789Sahrens case DKIOCGETEFI: 15047405SEric.Taylor@Sun.COM { 15057405SEric.Taylor@Sun.COM uint64_t vs = zv->zv_volsize; 15067405SEric.Taylor@Sun.COM uint8_t bs = zv->zv_min_bs; 15073016Smaybee 15083016Smaybee mutex_exit(&zvol_state_lock); 15097405SEric.Taylor@Sun.COM error = zvol_getefi((void *)arg, flag, vs, bs); 15107405SEric.Taylor@Sun.COM return (error); 15113016Smaybee } 1512789Sahrens 15133638Sbillm case DKIOCFLUSHWRITECACHE: 15143897Smaybee dkc = (struct dk_callback *)arg; 15159303SEric.Taylor@Sun.COM mutex_exit(&zvol_state_lock); 15163638Sbillm zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ); 15173897Smaybee if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) { 15183897Smaybee (*dkc->dkc_callback)(dkc->dkc_cookie, error); 15193897Smaybee error = 0; 15203897Smaybee } 15219303SEric.Taylor@Sun.COM return (error); 15229303SEric.Taylor@Sun.COM 15239303SEric.Taylor@Sun.COM case DKIOCGETWCE: 15249303SEric.Taylor@Sun.COM { 15259303SEric.Taylor@Sun.COM int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0; 15269303SEric.Taylor@Sun.COM if (ddi_copyout(&wce, (void *)arg, sizeof (int), 15279303SEric.Taylor@Sun.COM flag)) 15289303SEric.Taylor@Sun.COM error = EFAULT; 15299303SEric.Taylor@Sun.COM break; 15309303SEric.Taylor@Sun.COM } 15319303SEric.Taylor@Sun.COM case DKIOCSETWCE: 15329303SEric.Taylor@Sun.COM { 15339303SEric.Taylor@Sun.COM int wce; 15349303SEric.Taylor@Sun.COM if (ddi_copyin((void *)arg, &wce, sizeof (int), 15359303SEric.Taylor@Sun.COM flag)) { 15369303SEric.Taylor@Sun.COM error = EFAULT; 15379303SEric.Taylor@Sun.COM break; 15389303SEric.Taylor@Sun.COM } 15399303SEric.Taylor@Sun.COM if (wce) { 15409303SEric.Taylor@Sun.COM zv->zv_flags |= ZVOL_WCE; 15419303SEric.Taylor@Sun.COM mutex_exit(&zvol_state_lock); 15429303SEric.Taylor@Sun.COM } else { 15439303SEric.Taylor@Sun.COM zv->zv_flags &= ~ZVOL_WCE; 15449303SEric.Taylor@Sun.COM mutex_exit(&zvol_state_lock); 15459303SEric.Taylor@Sun.COM zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ); 15469303SEric.Taylor@Sun.COM } 15479303SEric.Taylor@Sun.COM return (0); 15489303SEric.Taylor@Sun.COM } 15493638Sbillm 15503245Smaybee case DKIOCGGEOM: 15513245Smaybee case DKIOCGVTOC: 15526423Sgw25295 /* 15536423Sgw25295 * commands using these (like prtvtoc) expect ENOTSUP 15546423Sgw25295 * since we're emulating an EFI label 15556423Sgw25295 */ 15563245Smaybee error = ENOTSUP; 15573245Smaybee break; 15583245Smaybee 15596423Sgw25295 case DKIOCDUMPINIT: 15606423Sgw25295 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 15616423Sgw25295 RL_WRITER); 15626423Sgw25295 error = zvol_dumpify(zv); 15636423Sgw25295 zfs_range_unlock(rl); 15646423Sgw25295 break; 15656423Sgw25295 15666423Sgw25295 case DKIOCDUMPFINI: 15679277SEric.Taylor@Sun.COM if (!(zv->zv_flags & ZVOL_DUMPIFIED)) 15689277SEric.Taylor@Sun.COM break; 15696423Sgw25295 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 15706423Sgw25295 RL_WRITER); 15716423Sgw25295 error = zvol_dump_fini(zv); 15726423Sgw25295 zfs_range_unlock(rl); 15736423Sgw25295 break; 15746423Sgw25295 1575789Sahrens default: 15763016Smaybee error = ENOTTY; 1577789Sahrens break; 1578789Sahrens 1579789Sahrens } 1580789Sahrens mutex_exit(&zvol_state_lock); 1581789Sahrens return (error); 1582789Sahrens } 1583789Sahrens 1584789Sahrens int 1585789Sahrens zvol_busy(void) 1586789Sahrens { 1587789Sahrens return (zvol_minors != 0); 1588789Sahrens } 1589789Sahrens 1590789Sahrens void 1591789Sahrens zvol_init(void) 1592789Sahrens { 1593789Sahrens VERIFY(ddi_soft_state_init(&zvol_state, sizeof (zvol_state_t), 1) == 0); 1594789Sahrens mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL); 1595789Sahrens } 1596789Sahrens 1597789Sahrens void 1598789Sahrens zvol_fini(void) 1599789Sahrens { 1600789Sahrens mutex_destroy(&zvol_state_lock); 1601789Sahrens ddi_soft_state_fini(&zvol_state); 1602789Sahrens } 16036423Sgw25295 16046423Sgw25295 static int 16056423Sgw25295 zvol_dump_init(zvol_state_t *zv, boolean_t resize) 16066423Sgw25295 { 16076423Sgw25295 dmu_tx_t *tx; 16086423Sgw25295 int error = 0; 16096423Sgw25295 objset_t *os = zv->zv_objset; 16106423Sgw25295 nvlist_t *nv = NULL; 161111656SGeorge.Wilson@Sun.COM uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset)); 16126423Sgw25295 16136423Sgw25295 ASSERT(MUTEX_HELD(&zvol_state_lock)); 161410588SEric.Taylor@Sun.COM error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0, 161510588SEric.Taylor@Sun.COM DMU_OBJECT_END); 161610588SEric.Taylor@Sun.COM /* wait for dmu_free_long_range to actually free the blocks */ 161710588SEric.Taylor@Sun.COM txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 16186423Sgw25295 16196423Sgw25295 tx = dmu_tx_create(os); 16206423Sgw25295 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 162110588SEric.Taylor@Sun.COM dmu_tx_hold_bonus(tx, ZVOL_OBJ); 16226423Sgw25295 error = dmu_tx_assign(tx, TXG_WAIT); 16236423Sgw25295 if (error) { 16246423Sgw25295 dmu_tx_abort(tx); 16256423Sgw25295 return (error); 16266423Sgw25295 } 16276423Sgw25295 16286423Sgw25295 /* 16296423Sgw25295 * If we are resizing the dump device then we only need to 16306423Sgw25295 * update the refreservation to match the newly updated 16316423Sgw25295 * zvolsize. Otherwise, we save off the original state of the 16326423Sgw25295 * zvol so that we can restore them if the zvol is ever undumpified. 16336423Sgw25295 */ 16346423Sgw25295 if (resize) { 16356423Sgw25295 error = zap_update(os, ZVOL_ZAP_OBJ, 16366423Sgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 16376423Sgw25295 &zv->zv_volsize, tx); 16386423Sgw25295 } else { 163911619SGeorge.Wilson@Sun.COM uint64_t checksum, compress, refresrv, vbs, dedup; 16407837SMatthew.Ahrens@Sun.COM 16416423Sgw25295 error = dsl_prop_get_integer(zv->zv_name, 16426423Sgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL); 16436423Sgw25295 error = error ? error : dsl_prop_get_integer(zv->zv_name, 16446423Sgw25295 zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL); 16456423Sgw25295 error = error ? error : dsl_prop_get_integer(zv->zv_name, 16466423Sgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL); 16477837SMatthew.Ahrens@Sun.COM error = error ? error : dsl_prop_get_integer(zv->zv_name, 16487837SMatthew.Ahrens@Sun.COM zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL); 164911656SGeorge.Wilson@Sun.COM if (version >= SPA_VERSION_DEDUP) { 165011656SGeorge.Wilson@Sun.COM error = error ? error : 165111656SGeorge.Wilson@Sun.COM dsl_prop_get_integer(zv->zv_name, 165211656SGeorge.Wilson@Sun.COM zfs_prop_to_name(ZFS_PROP_DEDUP), &dedup, NULL); 165311656SGeorge.Wilson@Sun.COM } 16546423Sgw25295 16556423Sgw25295 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 16566423Sgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, 16576423Sgw25295 &compress, tx); 16586423Sgw25295 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 16596423Sgw25295 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx); 16606423Sgw25295 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 16616423Sgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 16626423Sgw25295 &refresrv, tx); 16637837SMatthew.Ahrens@Sun.COM error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 16647837SMatthew.Ahrens@Sun.COM zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, 16657837SMatthew.Ahrens@Sun.COM &vbs, tx); 166610588SEric.Taylor@Sun.COM error = error ? error : dmu_object_set_blocksize( 166710588SEric.Taylor@Sun.COM os, ZVOL_OBJ, SPA_MAXBLOCKSIZE, 0, tx); 166811656SGeorge.Wilson@Sun.COM if (version >= SPA_VERSION_DEDUP) { 166911656SGeorge.Wilson@Sun.COM error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 167011656SGeorge.Wilson@Sun.COM zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, 167111656SGeorge.Wilson@Sun.COM &dedup, tx); 167211656SGeorge.Wilson@Sun.COM } 167310588SEric.Taylor@Sun.COM if (error == 0) 167410588SEric.Taylor@Sun.COM zv->zv_volblocksize = SPA_MAXBLOCKSIZE; 16756423Sgw25295 } 16766423Sgw25295 dmu_tx_commit(tx); 16776423Sgw25295 16786423Sgw25295 /* 16796423Sgw25295 * We only need update the zvol's property if we are initializing 16806423Sgw25295 * the dump area for the first time. 16816423Sgw25295 */ 16826423Sgw25295 if (!resize) { 16836423Sgw25295 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 16846423Sgw25295 VERIFY(nvlist_add_uint64(nv, 16856423Sgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0); 16866423Sgw25295 VERIFY(nvlist_add_uint64(nv, 16876423Sgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 16886423Sgw25295 ZIO_COMPRESS_OFF) == 0); 16896423Sgw25295 VERIFY(nvlist_add_uint64(nv, 16906423Sgw25295 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 16916423Sgw25295 ZIO_CHECKSUM_OFF) == 0); 169211656SGeorge.Wilson@Sun.COM if (version >= SPA_VERSION_DEDUP) { 169311656SGeorge.Wilson@Sun.COM VERIFY(nvlist_add_uint64(nv, 169411656SGeorge.Wilson@Sun.COM zfs_prop_to_name(ZFS_PROP_DEDUP), 169511656SGeorge.Wilson@Sun.COM ZIO_CHECKSUM_OFF) == 0); 169611656SGeorge.Wilson@Sun.COM } 16976423Sgw25295 169811022STom.Erickson@Sun.COM error = zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL, 169911022STom.Erickson@Sun.COM nv, NULL); 17006423Sgw25295 nvlist_free(nv); 17016423Sgw25295 17026423Sgw25295 if (error) 17036423Sgw25295 return (error); 17046423Sgw25295 } 17056423Sgw25295 17066423Sgw25295 /* Allocate the space for the dump */ 17076423Sgw25295 error = zvol_prealloc(zv); 17086423Sgw25295 return (error); 17096423Sgw25295 } 17106423Sgw25295 17116423Sgw25295 static int 17126423Sgw25295 zvol_dumpify(zvol_state_t *zv) 17136423Sgw25295 { 17146423Sgw25295 int error = 0; 17156423Sgw25295 uint64_t dumpsize = 0; 17166423Sgw25295 dmu_tx_t *tx; 17176423Sgw25295 objset_t *os = zv->zv_objset; 17186423Sgw25295 171910588SEric.Taylor@Sun.COM if (zv->zv_flags & ZVOL_RDONLY) 17206423Sgw25295 return (EROFS); 17216423Sgw25295 17226423Sgw25295 if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 17236423Sgw25295 8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) { 17246423Sgw25295 boolean_t resize = (dumpsize > 0) ? B_TRUE : B_FALSE; 17256423Sgw25295 17266423Sgw25295 if ((error = zvol_dump_init(zv, resize)) != 0) { 17276423Sgw25295 (void) zvol_dump_fini(zv); 17286423Sgw25295 return (error); 17296423Sgw25295 } 17306423Sgw25295 } 17316423Sgw25295 17326423Sgw25295 /* 17336423Sgw25295 * Build up our lba mapping. 17346423Sgw25295 */ 17356423Sgw25295 error = zvol_get_lbas(zv); 17366423Sgw25295 if (error) { 17376423Sgw25295 (void) zvol_dump_fini(zv); 17386423Sgw25295 return (error); 17396423Sgw25295 } 17406423Sgw25295 17416423Sgw25295 tx = dmu_tx_create(os); 17426423Sgw25295 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 17436423Sgw25295 error = dmu_tx_assign(tx, TXG_WAIT); 17446423Sgw25295 if (error) { 17456423Sgw25295 dmu_tx_abort(tx); 17466423Sgw25295 (void) zvol_dump_fini(zv); 17476423Sgw25295 return (error); 17486423Sgw25295 } 17496423Sgw25295 17506423Sgw25295 zv->zv_flags |= ZVOL_DUMPIFIED; 17516423Sgw25295 error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1, 17526423Sgw25295 &zv->zv_volsize, tx); 17536423Sgw25295 dmu_tx_commit(tx); 17546423Sgw25295 17556423Sgw25295 if (error) { 17566423Sgw25295 (void) zvol_dump_fini(zv); 17576423Sgw25295 return (error); 17586423Sgw25295 } 17596423Sgw25295 17606423Sgw25295 txg_wait_synced(dmu_objset_pool(os), 0); 17616423Sgw25295 return (0); 17626423Sgw25295 } 17636423Sgw25295 17646423Sgw25295 static int 17656423Sgw25295 zvol_dump_fini(zvol_state_t *zv) 17666423Sgw25295 { 17676423Sgw25295 dmu_tx_t *tx; 17686423Sgw25295 objset_t *os = zv->zv_objset; 17696423Sgw25295 nvlist_t *nv; 17706423Sgw25295 int error = 0; 177111619SGeorge.Wilson@Sun.COM uint64_t checksum, compress, refresrv, vbs, dedup; 177211656SGeorge.Wilson@Sun.COM uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset)); 17736423Sgw25295 17747080Smaybee /* 17757080Smaybee * Attempt to restore the zvol back to its pre-dumpified state. 17767080Smaybee * This is a best-effort attempt as it's possible that not all 17777080Smaybee * of these properties were initialized during the dumpify process 17787080Smaybee * (i.e. error during zvol_dump_init). 17797080Smaybee */ 17807080Smaybee 17816423Sgw25295 tx = dmu_tx_create(os); 17826423Sgw25295 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 17836423Sgw25295 error = dmu_tx_assign(tx, TXG_WAIT); 17846423Sgw25295 if (error) { 17856423Sgw25295 dmu_tx_abort(tx); 17866423Sgw25295 return (error); 17876423Sgw25295 } 17887080Smaybee (void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx); 17897080Smaybee dmu_tx_commit(tx); 17906423Sgw25295 17916423Sgw25295 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 17926423Sgw25295 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum); 17936423Sgw25295 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 17946423Sgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress); 17956423Sgw25295 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 17966423Sgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv); 17977837SMatthew.Ahrens@Sun.COM (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 17987837SMatthew.Ahrens@Sun.COM zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs); 17996423Sgw25295 18006423Sgw25295 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 18016423Sgw25295 (void) nvlist_add_uint64(nv, 18026423Sgw25295 zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum); 18036423Sgw25295 (void) nvlist_add_uint64(nv, 18046423Sgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress); 18056423Sgw25295 (void) nvlist_add_uint64(nv, 18066423Sgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv); 180711656SGeorge.Wilson@Sun.COM if (version >= SPA_VERSION_DEDUP && 180811656SGeorge.Wilson@Sun.COM zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 180911656SGeorge.Wilson@Sun.COM zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, &dedup) == 0) { 181011656SGeorge.Wilson@Sun.COM (void) nvlist_add_uint64(nv, 181111656SGeorge.Wilson@Sun.COM zfs_prop_to_name(ZFS_PROP_DEDUP), dedup); 181211656SGeorge.Wilson@Sun.COM } 181311022STom.Erickson@Sun.COM (void) zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL, 181411022STom.Erickson@Sun.COM nv, NULL); 18156423Sgw25295 nvlist_free(nv); 18166423Sgw25295 18177080Smaybee zvol_free_extents(zv); 18187080Smaybee zv->zv_flags &= ~ZVOL_DUMPIFIED; 18197080Smaybee (void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END); 182010588SEric.Taylor@Sun.COM /* wait for dmu_free_long_range to actually free the blocks */ 182110588SEric.Taylor@Sun.COM txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 182210588SEric.Taylor@Sun.COM tx = dmu_tx_create(os); 182310588SEric.Taylor@Sun.COM dmu_tx_hold_bonus(tx, ZVOL_OBJ); 182410588SEric.Taylor@Sun.COM error = dmu_tx_assign(tx, TXG_WAIT); 182510588SEric.Taylor@Sun.COM if (error) { 182610588SEric.Taylor@Sun.COM dmu_tx_abort(tx); 182710588SEric.Taylor@Sun.COM return (error); 182810588SEric.Taylor@Sun.COM } 182910922SJeff.Bonwick@Sun.COM if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0) 183010922SJeff.Bonwick@Sun.COM zv->zv_volblocksize = vbs; 183110588SEric.Taylor@Sun.COM dmu_tx_commit(tx); 18327080Smaybee 18336423Sgw25295 return (0); 18346423Sgw25295 } 1835