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 /* 226423Sgw25295 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens /* 27789Sahrens * ZFS volume emulation driver. 28789Sahrens * 29789Sahrens * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes. 30789Sahrens * Volumes are accessed through the symbolic links named: 31789Sahrens * 32789Sahrens * /dev/zvol/dsk/<pool_name>/<dataset_name> 33789Sahrens * /dev/zvol/rdsk/<pool_name>/<dataset_name> 34789Sahrens * 35789Sahrens * These links are created by the ZFS-specific devfsadm link generator. 36789Sahrens * Volumes are persistent through reboot. No user command needs to be 37789Sahrens * run before opening and using a device. 38789Sahrens */ 39789Sahrens 40789Sahrens #include <sys/types.h> 41789Sahrens #include <sys/param.h> 42789Sahrens #include <sys/errno.h> 43789Sahrens #include <sys/uio.h> 44789Sahrens #include <sys/buf.h> 45789Sahrens #include <sys/modctl.h> 46789Sahrens #include <sys/open.h> 47789Sahrens #include <sys/kmem.h> 48789Sahrens #include <sys/conf.h> 49789Sahrens #include <sys/cmn_err.h> 50789Sahrens #include <sys/stat.h> 51789Sahrens #include <sys/zap.h> 52789Sahrens #include <sys/spa.h> 53789Sahrens #include <sys/zio.h> 546423Sgw25295 #include <sys/dmu_traverse.h> 556423Sgw25295 #include <sys/dnode.h> 566423Sgw25295 #include <sys/dsl_dataset.h> 57789Sahrens #include <sys/dsl_prop.h> 58789Sahrens #include <sys/dkio.h> 59789Sahrens #include <sys/efi_partition.h> 60789Sahrens #include <sys/byteorder.h> 61789Sahrens #include <sys/pathname.h> 62789Sahrens #include <sys/ddi.h> 63789Sahrens #include <sys/sunddi.h> 64789Sahrens #include <sys/crc32.h> 65789Sahrens #include <sys/dirent.h> 66789Sahrens #include <sys/policy.h> 67789Sahrens #include <sys/fs/zfs.h> 68789Sahrens #include <sys/zfs_ioctl.h> 69789Sahrens #include <sys/mkdev.h> 701141Sperrin #include <sys/zil.h> 712237Smaybee #include <sys/refcount.h> 723755Sperrin #include <sys/zfs_znode.h> 733755Sperrin #include <sys/zfs_rlock.h> 746423Sgw25295 #include <sys/vdev_disk.h> 756423Sgw25295 #include <sys/vdev_impl.h> 766423Sgw25295 #include <sys/zvol.h> 776423Sgw25295 #include <sys/dumphdr.h> 78789Sahrens 79789Sahrens #include "zfs_namecheck.h" 80789Sahrens 816423Sgw25295 static void *zvol_state; 82789Sahrens 836423Sgw25295 #define ZVOL_DUMPSIZE "dumpsize" 84789Sahrens 85789Sahrens /* 86789Sahrens * This lock protects the zvol_state structure from being modified 87789Sahrens * while it's being used, e.g. an open that comes in before a create 88789Sahrens * finishes. It also protects temporary opens of the dataset so that, 89789Sahrens * e.g., an open doesn't get a spurious EBUSY. 90789Sahrens */ 91789Sahrens static kmutex_t zvol_state_lock; 92789Sahrens static uint32_t zvol_minors; 93789Sahrens 946423Sgw25295 typedef struct zvol_extent { 957837SMatthew.Ahrens@Sun.COM list_node_t ze_node; 966423Sgw25295 dva_t ze_dva; /* dva associated with this extent */ 977837SMatthew.Ahrens@Sun.COM uint64_t ze_nblks; /* number of blocks in extent */ 986423Sgw25295 } zvol_extent_t; 996423Sgw25295 1006423Sgw25295 /* 101789Sahrens * The in-core state of each volume. 102789Sahrens */ 103789Sahrens typedef struct zvol_state { 104789Sahrens char zv_name[MAXPATHLEN]; /* pool/dd name */ 105789Sahrens uint64_t zv_volsize; /* amount of space we advertise */ 1063063Sperrin uint64_t zv_volblocksize; /* volume block size */ 107789Sahrens minor_t zv_minor; /* minor number */ 108789Sahrens uint8_t zv_min_bs; /* minimum addressable block shift */ 1096423Sgw25295 uint8_t zv_flags; /* readonly; dumpified */ 110789Sahrens objset_t *zv_objset; /* objset handle */ 111789Sahrens uint32_t zv_mode; /* DS_MODE_* flags at open time */ 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 */ 1161141Sperrin uint64_t zv_txg_assign; /* txg to assign during ZIL replay */ 1173755Sperrin znode_t zv_znode; /* for range locking */ 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 1266423Sgw25295 1276423Sgw25295 /* 1283063Sperrin * zvol maximum transfer in one DMU tx. 1293063Sperrin */ 1303063Sperrin int zvol_maxphys = DMU_MAX_ACCESS/2; 1313063Sperrin 1326423Sgw25295 extern int zfs_set_prop_nvlist(const char *, nvlist_t *); 1333638Sbillm static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio); 1346423Sgw25295 static int zvol_dumpify(zvol_state_t *zv); 1356423Sgw25295 static int zvol_dump_fini(zvol_state_t *zv); 1366423Sgw25295 static int zvol_dump_init(zvol_state_t *zv, boolean_t resize); 1373063Sperrin 138789Sahrens static void 1394787Sahrens zvol_size_changed(zvol_state_t *zv, major_t maj) 140789Sahrens { 1414787Sahrens dev_t dev = makedevice(maj, zv->zv_minor); 142789Sahrens 143789Sahrens VERIFY(ddi_prop_update_int64(dev, zfs_dip, 144789Sahrens "Size", zv->zv_volsize) == DDI_SUCCESS); 145789Sahrens VERIFY(ddi_prop_update_int64(dev, zfs_dip, 146789Sahrens "Nblocks", lbtodb(zv->zv_volsize)) == DDI_SUCCESS); 1476423Sgw25295 1486423Sgw25295 /* Notify specfs to invalidate the cached size */ 1496423Sgw25295 spec_size_invalidate(dev, VBLK); 1506423Sgw25295 spec_size_invalidate(dev, VCHR); 151789Sahrens } 152789Sahrens 153789Sahrens int 1542676Seschrock zvol_check_volsize(uint64_t volsize, uint64_t blocksize) 155789Sahrens { 1562676Seschrock if (volsize == 0) 157789Sahrens return (EINVAL); 158789Sahrens 1592676Seschrock if (volsize % blocksize != 0) 1601133Seschrock return (EINVAL); 1611133Seschrock 162789Sahrens #ifdef _ILP32 1632676Seschrock if (volsize - 1 > SPEC_MAXOFFSET_T) 164789Sahrens return (EOVERFLOW); 165789Sahrens #endif 166789Sahrens return (0); 167789Sahrens } 168789Sahrens 169789Sahrens int 1702676Seschrock zvol_check_volblocksize(uint64_t volblocksize) 171789Sahrens { 1722676Seschrock if (volblocksize < SPA_MINBLOCKSIZE || 1732676Seschrock volblocksize > SPA_MAXBLOCKSIZE || 1742676Seschrock !ISP2(volblocksize)) 175789Sahrens return (EDOM); 176789Sahrens 177789Sahrens return (0); 178789Sahrens } 179789Sahrens 180789Sahrens static void 181789Sahrens zvol_readonly_changed_cb(void *arg, uint64_t newval) 182789Sahrens { 183789Sahrens zvol_state_t *zv = arg; 184789Sahrens 1856423Sgw25295 if (newval) 1866423Sgw25295 zv->zv_flags |= ZVOL_RDONLY; 1876423Sgw25295 else 1886423Sgw25295 zv->zv_flags &= ~ZVOL_RDONLY; 189789Sahrens } 190789Sahrens 191789Sahrens int 1922885Sahrens zvol_get_stats(objset_t *os, nvlist_t *nv) 193789Sahrens { 194789Sahrens int error; 195789Sahrens dmu_object_info_t doi; 1962885Sahrens uint64_t val; 197789Sahrens 198789Sahrens 1992885Sahrens error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val); 200789Sahrens if (error) 201789Sahrens return (error); 202789Sahrens 2032885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val); 2042885Sahrens 205789Sahrens error = dmu_object_info(os, ZVOL_OBJ, &doi); 206789Sahrens 2072885Sahrens if (error == 0) { 2082885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE, 2092885Sahrens doi.doi_data_block_size); 2102885Sahrens } 211789Sahrens 212789Sahrens return (error); 213789Sahrens } 214789Sahrens 215789Sahrens /* 216789Sahrens * Find a free minor number. 217789Sahrens */ 218789Sahrens static minor_t 219789Sahrens zvol_minor_alloc(void) 220789Sahrens { 221789Sahrens minor_t minor; 222789Sahrens 223789Sahrens ASSERT(MUTEX_HELD(&zvol_state_lock)); 224789Sahrens 225789Sahrens for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) 226789Sahrens if (ddi_get_soft_state(zvol_state, minor) == NULL) 227789Sahrens return (minor); 228789Sahrens 229789Sahrens return (0); 230789Sahrens } 231789Sahrens 232789Sahrens static zvol_state_t * 2332676Seschrock zvol_minor_lookup(const char *name) 234789Sahrens { 235789Sahrens minor_t minor; 236789Sahrens zvol_state_t *zv; 237789Sahrens 238789Sahrens ASSERT(MUTEX_HELD(&zvol_state_lock)); 239789Sahrens 240789Sahrens for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) { 241789Sahrens zv = ddi_get_soft_state(zvol_state, minor); 242789Sahrens if (zv == NULL) 243789Sahrens continue; 244789Sahrens if (strcmp(zv->zv_name, name) == 0) 245789Sahrens break; 246789Sahrens } 247789Sahrens 248789Sahrens return (zv); 249789Sahrens } 250789Sahrens 2516423Sgw25295 /* extent mapping arg */ 2526423Sgw25295 struct maparg { 2537837SMatthew.Ahrens@Sun.COM zvol_state_t *ma_zv; 2547837SMatthew.Ahrens@Sun.COM uint64_t ma_blks; 2556423Sgw25295 }; 2566423Sgw25295 2576423Sgw25295 /*ARGSUSED*/ 2586423Sgw25295 static int 2597837SMatthew.Ahrens@Sun.COM zvol_map_block(spa_t *spa, blkptr_t *bp, const zbookmark_t *zb, 2607837SMatthew.Ahrens@Sun.COM const dnode_phys_t *dnp, void *arg) 2616423Sgw25295 { 2627837SMatthew.Ahrens@Sun.COM struct maparg *ma = arg; 2637837SMatthew.Ahrens@Sun.COM zvol_extent_t *ze; 2647837SMatthew.Ahrens@Sun.COM int bs = ma->ma_zv->zv_volblocksize; 2656423Sgw25295 2667837SMatthew.Ahrens@Sun.COM if (bp == NULL || zb->zb_object != ZVOL_OBJ || zb->zb_level != 0) 2676423Sgw25295 return (0); 2686423Sgw25295 2697837SMatthew.Ahrens@Sun.COM VERIFY3U(ma->ma_blks, ==, zb->zb_blkid); 2707837SMatthew.Ahrens@Sun.COM ma->ma_blks++; 2717837SMatthew.Ahrens@Sun.COM 2726423Sgw25295 /* Abort immediately if we have encountered gang blocks */ 2737837SMatthew.Ahrens@Sun.COM if (BP_IS_GANG(bp)) 2747837SMatthew.Ahrens@Sun.COM return (EFRAGS); 2756423Sgw25295 2767837SMatthew.Ahrens@Sun.COM /* 2777837SMatthew.Ahrens@Sun.COM * See if the block is at the end of the previous extent. 2787837SMatthew.Ahrens@Sun.COM */ 2797837SMatthew.Ahrens@Sun.COM ze = list_tail(&ma->ma_zv->zv_extents); 2807837SMatthew.Ahrens@Sun.COM if (ze && 2817837SMatthew.Ahrens@Sun.COM DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) && 2827837SMatthew.Ahrens@Sun.COM DVA_GET_OFFSET(BP_IDENTITY(bp)) == 2837837SMatthew.Ahrens@Sun.COM DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) { 2847837SMatthew.Ahrens@Sun.COM ze->ze_nblks++; 2856423Sgw25295 return (0); 2866423Sgw25295 } 2876423Sgw25295 2887837SMatthew.Ahrens@Sun.COM dprintf_bp(bp, "%s", "next blkptr:"); 2897837SMatthew.Ahrens@Sun.COM 2907837SMatthew.Ahrens@Sun.COM /* start a new extent */ 2917837SMatthew.Ahrens@Sun.COM ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP); 2927837SMatthew.Ahrens@Sun.COM ze->ze_dva = bp->blk_dva[0]; /* structure assignment */ 2937837SMatthew.Ahrens@Sun.COM ze->ze_nblks = 1; 2947837SMatthew.Ahrens@Sun.COM list_insert_tail(&ma->ma_zv->zv_extents, ze); 2957837SMatthew.Ahrens@Sun.COM return (0); 2967837SMatthew.Ahrens@Sun.COM } 2977837SMatthew.Ahrens@Sun.COM 2987837SMatthew.Ahrens@Sun.COM static void 2997837SMatthew.Ahrens@Sun.COM zvol_free_extents(zvol_state_t *zv) 3007837SMatthew.Ahrens@Sun.COM { 3017837SMatthew.Ahrens@Sun.COM zvol_extent_t *ze; 3027837SMatthew.Ahrens@Sun.COM 3037837SMatthew.Ahrens@Sun.COM while (ze = list_head(&zv->zv_extents)) { 3047837SMatthew.Ahrens@Sun.COM list_remove(&zv->zv_extents, ze); 3057837SMatthew.Ahrens@Sun.COM kmem_free(ze, sizeof (zvol_extent_t)); 3067837SMatthew.Ahrens@Sun.COM } 3077837SMatthew.Ahrens@Sun.COM } 3087837SMatthew.Ahrens@Sun.COM 3097837SMatthew.Ahrens@Sun.COM static int 3107837SMatthew.Ahrens@Sun.COM zvol_get_lbas(zvol_state_t *zv) 3117837SMatthew.Ahrens@Sun.COM { 3127837SMatthew.Ahrens@Sun.COM struct maparg ma; 3137837SMatthew.Ahrens@Sun.COM int err; 3147837SMatthew.Ahrens@Sun.COM 3157837SMatthew.Ahrens@Sun.COM ma.ma_zv = zv; 3167837SMatthew.Ahrens@Sun.COM ma.ma_blks = 0; 3177837SMatthew.Ahrens@Sun.COM zvol_free_extents(zv); 3187837SMatthew.Ahrens@Sun.COM 3197837SMatthew.Ahrens@Sun.COM err = traverse_dataset(dmu_objset_ds(zv->zv_objset), 0, 3207837SMatthew.Ahrens@Sun.COM TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma); 3217837SMatthew.Ahrens@Sun.COM if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) { 3227837SMatthew.Ahrens@Sun.COM zvol_free_extents(zv); 3237837SMatthew.Ahrens@Sun.COM return (err ? err : EIO); 3246423Sgw25295 } 3256423Sgw25295 3266423Sgw25295 return (0); 3276423Sgw25295 } 3286423Sgw25295 3294543Smarks /* ARGSUSED */ 330789Sahrens void 3314543Smarks zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 332789Sahrens { 3335331Samw zfs_creat_t *zct = arg; 3345331Samw nvlist_t *nvprops = zct->zct_props; 335789Sahrens int error; 3362676Seschrock uint64_t volblocksize, volsize; 337789Sahrens 3384543Smarks VERIFY(nvlist_lookup_uint64(nvprops, 3392676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0); 3404543Smarks if (nvlist_lookup_uint64(nvprops, 3412676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0) 3422676Seschrock volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 3432676Seschrock 3442676Seschrock /* 3456423Sgw25295 * These properties must be removed from the list so the generic 3462676Seschrock * property setting step won't apply to them. 3472676Seschrock */ 3484543Smarks VERIFY(nvlist_remove_all(nvprops, 3492676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0); 3504543Smarks (void) nvlist_remove_all(nvprops, 3512676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE)); 3522676Seschrock 3532676Seschrock error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize, 354789Sahrens DMU_OT_NONE, 0, tx); 355789Sahrens ASSERT(error == 0); 356789Sahrens 357789Sahrens error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP, 358789Sahrens DMU_OT_NONE, 0, tx); 359789Sahrens ASSERT(error == 0); 360789Sahrens 3612676Seschrock error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx); 362789Sahrens ASSERT(error == 0); 363789Sahrens } 364789Sahrens 365789Sahrens /* 3661141Sperrin * Replay a TX_WRITE ZIL transaction that didn't get committed 3671141Sperrin * after a system failure 3681141Sperrin */ 3691141Sperrin static int 3701141Sperrin zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap) 3711141Sperrin { 3721141Sperrin objset_t *os = zv->zv_objset; 3731141Sperrin char *data = (char *)(lr + 1); /* data follows lr_write_t */ 3741141Sperrin uint64_t off = lr->lr_offset; 3751141Sperrin uint64_t len = lr->lr_length; 3761141Sperrin dmu_tx_t *tx; 3771141Sperrin int error; 3781141Sperrin 3791141Sperrin if (byteswap) 3801141Sperrin byteswap_uint64_array(lr, sizeof (*lr)); 3811141Sperrin 3821141Sperrin tx = dmu_tx_create(os); 3831141Sperrin dmu_tx_hold_write(tx, ZVOL_OBJ, off, len); 3841141Sperrin error = dmu_tx_assign(tx, zv->zv_txg_assign); 3851141Sperrin if (error) { 3861141Sperrin dmu_tx_abort(tx); 3871141Sperrin } else { 3881141Sperrin dmu_write(os, ZVOL_OBJ, off, len, data, tx); 3891141Sperrin dmu_tx_commit(tx); 3901141Sperrin } 3911141Sperrin 3921141Sperrin return (error); 3931141Sperrin } 3941141Sperrin 3951141Sperrin /* ARGSUSED */ 3961141Sperrin static int 3971141Sperrin zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap) 3981141Sperrin { 3991141Sperrin return (ENOTSUP); 4001141Sperrin } 4011141Sperrin 4021141Sperrin /* 4031141Sperrin * Callback vectors for replaying records. 4041141Sperrin * Only TX_WRITE is needed for zvol. 4051141Sperrin */ 4061141Sperrin zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = { 4071141Sperrin zvol_replay_err, /* 0 no such transaction type */ 4081141Sperrin zvol_replay_err, /* TX_CREATE */ 4091141Sperrin zvol_replay_err, /* TX_MKDIR */ 4101141Sperrin zvol_replay_err, /* TX_MKXATTR */ 4111141Sperrin zvol_replay_err, /* TX_SYMLINK */ 4121141Sperrin zvol_replay_err, /* TX_REMOVE */ 4131141Sperrin zvol_replay_err, /* TX_RMDIR */ 4141141Sperrin zvol_replay_err, /* TX_LINK */ 4151141Sperrin zvol_replay_err, /* TX_RENAME */ 4161141Sperrin zvol_replay_write, /* TX_WRITE */ 4171141Sperrin zvol_replay_err, /* TX_TRUNCATE */ 4181141Sperrin zvol_replay_err, /* TX_SETATTR */ 4191141Sperrin zvol_replay_err, /* TX_ACL */ 4201141Sperrin }; 4211141Sperrin 4221141Sperrin /* 4236423Sgw25295 * Create a minor node (plus a whole lot more) for the specified volume. 424789Sahrens */ 425789Sahrens int 4264787Sahrens zvol_create_minor(const char *name, major_t maj) 427789Sahrens { 428789Sahrens zvol_state_t *zv; 429789Sahrens objset_t *os; 4303063Sperrin dmu_object_info_t doi; 431789Sahrens uint64_t volsize; 432789Sahrens minor_t minor = 0; 433789Sahrens struct pathname linkpath; 4346689Smaybee int ds_mode = DS_MODE_OWNER; 435789Sahrens vnode_t *vp = NULL; 436789Sahrens char *devpath; 4376423Sgw25295 size_t devpathlen = strlen(ZVOL_FULL_DEV_DIR) + strlen(name) + 1; 438789Sahrens char chrbuf[30], blkbuf[30]; 439789Sahrens int error; 440789Sahrens 441789Sahrens mutex_enter(&zvol_state_lock); 442789Sahrens 443789Sahrens if ((zv = zvol_minor_lookup(name)) != NULL) { 444789Sahrens mutex_exit(&zvol_state_lock); 445789Sahrens return (EEXIST); 446789Sahrens } 447789Sahrens 448789Sahrens if (strchr(name, '@') != 0) 449789Sahrens ds_mode |= DS_MODE_READONLY; 450789Sahrens 451789Sahrens error = dmu_objset_open(name, DMU_OST_ZVOL, ds_mode, &os); 452789Sahrens 453789Sahrens if (error) { 454789Sahrens mutex_exit(&zvol_state_lock); 455789Sahrens return (error); 456789Sahrens } 457789Sahrens 458789Sahrens error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize); 459789Sahrens 460789Sahrens if (error) { 461789Sahrens dmu_objset_close(os); 462789Sahrens mutex_exit(&zvol_state_lock); 463789Sahrens return (error); 464789Sahrens } 465789Sahrens 466789Sahrens /* 467789Sahrens * If there's an existing /dev/zvol symlink, try to use the 468789Sahrens * same minor number we used last time. 469789Sahrens */ 470789Sahrens devpath = kmem_alloc(devpathlen, KM_SLEEP); 471789Sahrens 4726423Sgw25295 (void) sprintf(devpath, "%s%s", ZVOL_FULL_DEV_DIR, name); 473789Sahrens 474789Sahrens error = lookupname(devpath, UIO_SYSSPACE, NO_FOLLOW, NULL, &vp); 475789Sahrens 476789Sahrens kmem_free(devpath, devpathlen); 477789Sahrens 478789Sahrens if (error == 0 && vp->v_type != VLNK) 479789Sahrens error = EINVAL; 480789Sahrens 481789Sahrens if (error == 0) { 482789Sahrens pn_alloc(&linkpath); 483789Sahrens error = pn_getsymlink(vp, &linkpath, kcred); 484789Sahrens if (error == 0) { 485789Sahrens char *ms = strstr(linkpath.pn_path, ZVOL_PSEUDO_DEV); 486789Sahrens if (ms != NULL) { 487789Sahrens ms += strlen(ZVOL_PSEUDO_DEV); 488789Sahrens minor = stoi(&ms); 489789Sahrens } 490789Sahrens } 491789Sahrens pn_free(&linkpath); 492789Sahrens } 493789Sahrens 494789Sahrens if (vp != NULL) 495789Sahrens VN_RELE(vp); 496789Sahrens 497789Sahrens /* 498789Sahrens * If we found a minor but it's already in use, we must pick a new one. 499789Sahrens */ 500789Sahrens if (minor != 0 && ddi_get_soft_state(zvol_state, minor) != NULL) 501789Sahrens minor = 0; 502789Sahrens 503789Sahrens if (minor == 0) 504789Sahrens minor = zvol_minor_alloc(); 505789Sahrens 506789Sahrens if (minor == 0) { 507789Sahrens dmu_objset_close(os); 508789Sahrens mutex_exit(&zvol_state_lock); 509789Sahrens return (ENXIO); 510789Sahrens } 511789Sahrens 512789Sahrens if (ddi_soft_state_zalloc(zvol_state, minor) != DDI_SUCCESS) { 513789Sahrens dmu_objset_close(os); 514789Sahrens mutex_exit(&zvol_state_lock); 515789Sahrens return (EAGAIN); 516789Sahrens } 517789Sahrens 5182676Seschrock (void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME, 5192676Seschrock (char *)name); 520789Sahrens 521789Sahrens (void) sprintf(chrbuf, "%uc,raw", minor); 522789Sahrens 523789Sahrens if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR, 524789Sahrens minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 525789Sahrens ddi_soft_state_free(zvol_state, minor); 526789Sahrens dmu_objset_close(os); 527789Sahrens mutex_exit(&zvol_state_lock); 528789Sahrens return (EAGAIN); 529789Sahrens } 530789Sahrens 531789Sahrens (void) sprintf(blkbuf, "%uc", minor); 532789Sahrens 533789Sahrens if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK, 534789Sahrens minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 535789Sahrens ddi_remove_minor_node(zfs_dip, chrbuf); 536789Sahrens ddi_soft_state_free(zvol_state, minor); 537789Sahrens dmu_objset_close(os); 538789Sahrens mutex_exit(&zvol_state_lock); 539789Sahrens return (EAGAIN); 540789Sahrens } 541789Sahrens 542789Sahrens zv = ddi_get_soft_state(zvol_state, minor); 543789Sahrens 544789Sahrens (void) strcpy(zv->zv_name, name); 545789Sahrens zv->zv_min_bs = DEV_BSHIFT; 546789Sahrens zv->zv_minor = minor; 547789Sahrens zv->zv_volsize = volsize; 548789Sahrens zv->zv_objset = os; 549789Sahrens zv->zv_mode = ds_mode; 5503063Sperrin zv->zv_zilog = zil_open(os, zvol_get_data); 5513755Sperrin mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL); 5523755Sperrin avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare, 5533755Sperrin sizeof (rl_t), offsetof(rl_t, r_node)); 5547837SMatthew.Ahrens@Sun.COM list_create(&zv->zv_extents, sizeof (zvol_extent_t), 5557837SMatthew.Ahrens@Sun.COM offsetof(zvol_extent_t, ze_node)); 5563063Sperrin /* get and cache the blocksize */ 5573063Sperrin error = dmu_object_info(os, ZVOL_OBJ, &doi); 5583063Sperrin ASSERT(error == 0); 5593063Sperrin zv->zv_volblocksize = doi.doi_data_block_size; 5601141Sperrin 5617638SNeil.Perrin@Sun.COM zil_replay(os, zv, &zv->zv_txg_assign, zvol_replay_vector, NULL); 5624787Sahrens zvol_size_changed(zv, maj); 563789Sahrens 5641544Seschrock /* XXX this should handle the possible i/o error */ 565789Sahrens VERIFY(dsl_prop_register(dmu_objset_ds(zv->zv_objset), 566789Sahrens "readonly", zvol_readonly_changed_cb, zv) == 0); 567789Sahrens 568789Sahrens zvol_minors++; 569789Sahrens 570789Sahrens mutex_exit(&zvol_state_lock); 571789Sahrens 572789Sahrens return (0); 573789Sahrens } 574789Sahrens 575789Sahrens /* 576789Sahrens * Remove minor node for the specified volume. 577789Sahrens */ 578789Sahrens int 5792676Seschrock zvol_remove_minor(const char *name) 580789Sahrens { 581789Sahrens zvol_state_t *zv; 582789Sahrens char namebuf[30]; 583789Sahrens 584789Sahrens mutex_enter(&zvol_state_lock); 585789Sahrens 5862676Seschrock if ((zv = zvol_minor_lookup(name)) == NULL) { 587789Sahrens mutex_exit(&zvol_state_lock); 588789Sahrens return (ENXIO); 589789Sahrens } 590789Sahrens 591789Sahrens if (zv->zv_total_opens != 0) { 592789Sahrens mutex_exit(&zvol_state_lock); 593789Sahrens return (EBUSY); 594789Sahrens } 595789Sahrens 596789Sahrens (void) sprintf(namebuf, "%uc,raw", zv->zv_minor); 597789Sahrens ddi_remove_minor_node(zfs_dip, namebuf); 598789Sahrens 599789Sahrens (void) sprintf(namebuf, "%uc", zv->zv_minor); 600789Sahrens ddi_remove_minor_node(zfs_dip, namebuf); 601789Sahrens 602789Sahrens VERIFY(dsl_prop_unregister(dmu_objset_ds(zv->zv_objset), 603789Sahrens "readonly", zvol_readonly_changed_cb, zv) == 0); 604789Sahrens 6051141Sperrin zil_close(zv->zv_zilog); 6061141Sperrin zv->zv_zilog = NULL; 607789Sahrens dmu_objset_close(zv->zv_objset); 608789Sahrens zv->zv_objset = NULL; 6093755Sperrin avl_destroy(&zv->zv_znode.z_range_avl); 6103755Sperrin mutex_destroy(&zv->zv_znode.z_range_lock); 611789Sahrens 612789Sahrens ddi_soft_state_free(zvol_state, zv->zv_minor); 613789Sahrens 614789Sahrens zvol_minors--; 615789Sahrens 616789Sahrens mutex_exit(&zvol_state_lock); 617789Sahrens 618789Sahrens return (0); 619789Sahrens } 620789Sahrens 6216423Sgw25295 int 6226423Sgw25295 zvol_prealloc(zvol_state_t *zv) 6236423Sgw25295 { 6246423Sgw25295 objset_t *os = zv->zv_objset; 6256423Sgw25295 dmu_tx_t *tx; 6266423Sgw25295 uint64_t refd, avail, usedobjs, availobjs; 6276423Sgw25295 uint64_t resid = zv->zv_volsize; 6286423Sgw25295 uint64_t off = 0; 6296423Sgw25295 6306423Sgw25295 /* Check the space usage before attempting to allocate the space */ 6316423Sgw25295 dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs); 6326423Sgw25295 if (avail < zv->zv_volsize) 6336423Sgw25295 return (ENOSPC); 6346423Sgw25295 6356423Sgw25295 /* Free old extents if they exist */ 6366423Sgw25295 zvol_free_extents(zv); 6376423Sgw25295 6386423Sgw25295 while (resid != 0) { 6396423Sgw25295 int error; 6406423Sgw25295 uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE); 6416423Sgw25295 6426423Sgw25295 tx = dmu_tx_create(os); 6436423Sgw25295 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 6446423Sgw25295 error = dmu_tx_assign(tx, TXG_WAIT); 6456423Sgw25295 if (error) { 6466423Sgw25295 dmu_tx_abort(tx); 6476992Smaybee (void) dmu_free_long_range(os, ZVOL_OBJ, 0, off); 6486423Sgw25295 return (error); 6496423Sgw25295 } 650*7872STim.Haley@Sun.COM dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx); 6516423Sgw25295 dmu_tx_commit(tx); 6526423Sgw25295 off += bytes; 6536423Sgw25295 resid -= bytes; 6546423Sgw25295 } 6556423Sgw25295 txg_wait_synced(dmu_objset_pool(os), 0); 6566423Sgw25295 6576423Sgw25295 return (0); 6586423Sgw25295 } 6596423Sgw25295 6606423Sgw25295 int 6616423Sgw25295 zvol_update_volsize(zvol_state_t *zv, major_t maj, uint64_t volsize) 6626423Sgw25295 { 6636423Sgw25295 dmu_tx_t *tx; 6646423Sgw25295 int error; 6656423Sgw25295 6666423Sgw25295 ASSERT(MUTEX_HELD(&zvol_state_lock)); 6676423Sgw25295 6686423Sgw25295 tx = dmu_tx_create(zv->zv_objset); 6696423Sgw25295 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 6706423Sgw25295 error = dmu_tx_assign(tx, TXG_WAIT); 6716423Sgw25295 if (error) { 6726423Sgw25295 dmu_tx_abort(tx); 6736423Sgw25295 return (error); 6746423Sgw25295 } 6756423Sgw25295 6766423Sgw25295 error = zap_update(zv->zv_objset, ZVOL_ZAP_OBJ, "size", 8, 1, 6776423Sgw25295 &volsize, tx); 6786423Sgw25295 dmu_tx_commit(tx); 6796423Sgw25295 6806423Sgw25295 if (error == 0) 6816992Smaybee error = dmu_free_long_range(zv->zv_objset, 6826992Smaybee ZVOL_OBJ, volsize, DMU_OBJECT_END); 6836423Sgw25295 6847265Sahrens /* 6857265Sahrens * If we are using a faked-up state (zv_minor == 0) then don't 6867265Sahrens * try to update the in-core zvol state. 6877265Sahrens */ 6887265Sahrens if (error == 0 && zv->zv_minor) { 6896423Sgw25295 zv->zv_volsize = volsize; 6906423Sgw25295 zvol_size_changed(zv, maj); 6916423Sgw25295 } 6926423Sgw25295 return (error); 6936423Sgw25295 } 6946423Sgw25295 695789Sahrens int 6964787Sahrens zvol_set_volsize(const char *name, major_t maj, uint64_t volsize) 697789Sahrens { 698789Sahrens zvol_state_t *zv; 699789Sahrens int error; 7001133Seschrock dmu_object_info_t doi; 7016423Sgw25295 uint64_t old_volsize = 0ULL; 7027265Sahrens zvol_state_t state = { 0 }; 703789Sahrens 704789Sahrens mutex_enter(&zvol_state_lock); 705789Sahrens 7062676Seschrock if ((zv = zvol_minor_lookup(name)) == NULL) { 7077265Sahrens /* 7087265Sahrens * If we are doing a "zfs clone -o volsize=", then the 7097265Sahrens * minor node won't exist yet. 7107265Sahrens */ 7117265Sahrens error = dmu_objset_open(name, DMU_OST_ZVOL, DS_MODE_OWNER, 7127265Sahrens &state.zv_objset); 7137265Sahrens if (error != 0) 7147265Sahrens goto out; 7157265Sahrens zv = &state; 716789Sahrens } 7176423Sgw25295 old_volsize = zv->zv_volsize; 718789Sahrens 7191133Seschrock if ((error = dmu_object_info(zv->zv_objset, ZVOL_OBJ, &doi)) != 0 || 7202676Seschrock (error = zvol_check_volsize(volsize, 7217265Sahrens doi.doi_data_block_size)) != 0) 7227265Sahrens goto out; 7231133Seschrock 7246423Sgw25295 if (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY)) { 7257265Sahrens error = EROFS; 7267265Sahrens goto out; 727789Sahrens } 728789Sahrens 7296423Sgw25295 error = zvol_update_volsize(zv, maj, volsize); 730789Sahrens 7316423Sgw25295 /* 7326423Sgw25295 * Reinitialize the dump area to the new size. If we 7336423Sgw25295 * failed to resize the dump area then restore the it back to 7346423Sgw25295 * it's original size. 7356423Sgw25295 */ 7366423Sgw25295 if (error == 0 && zv->zv_flags & ZVOL_DUMPIFIED) { 7376423Sgw25295 if ((error = zvol_dumpify(zv)) != 0 || 7386423Sgw25295 (error = dumpvp_resize()) != 0) { 7396423Sgw25295 (void) zvol_update_volsize(zv, maj, old_volsize); 7406423Sgw25295 error = zvol_dumpify(zv); 7416423Sgw25295 } 742789Sahrens } 743789Sahrens 7447265Sahrens out: 7457265Sahrens if (state.zv_objset) 7467265Sahrens dmu_objset_close(state.zv_objset); 7477265Sahrens 748789Sahrens mutex_exit(&zvol_state_lock); 749789Sahrens 750789Sahrens return (error); 751789Sahrens } 752789Sahrens 753789Sahrens int 7542676Seschrock zvol_set_volblocksize(const char *name, uint64_t volblocksize) 755789Sahrens { 756789Sahrens zvol_state_t *zv; 757789Sahrens dmu_tx_t *tx; 758789Sahrens int error; 7597837SMatthew.Ahrens@Sun.COM boolean_t needlock; 760789Sahrens 7617837SMatthew.Ahrens@Sun.COM /* 7627837SMatthew.Ahrens@Sun.COM * The lock may already be held if we are being called from 7637837SMatthew.Ahrens@Sun.COM * zvol_dump_init(). 7647837SMatthew.Ahrens@Sun.COM */ 7657837SMatthew.Ahrens@Sun.COM needlock = !MUTEX_HELD(&zvol_state_lock); 7667837SMatthew.Ahrens@Sun.COM if (needlock) 7677837SMatthew.Ahrens@Sun.COM mutex_enter(&zvol_state_lock); 768789Sahrens 7692676Seschrock if ((zv = zvol_minor_lookup(name)) == NULL) { 7707837SMatthew.Ahrens@Sun.COM if (needlock) 7717837SMatthew.Ahrens@Sun.COM mutex_exit(&zvol_state_lock); 772789Sahrens return (ENXIO); 773789Sahrens } 7746423Sgw25295 if (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY)) { 7757837SMatthew.Ahrens@Sun.COM if (needlock) 7767837SMatthew.Ahrens@Sun.COM mutex_exit(&zvol_state_lock); 777789Sahrens return (EROFS); 778789Sahrens } 779789Sahrens 780789Sahrens tx = dmu_tx_create(zv->zv_objset); 781789Sahrens dmu_tx_hold_bonus(tx, ZVOL_OBJ); 782789Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 783789Sahrens if (error) { 784789Sahrens dmu_tx_abort(tx); 785789Sahrens } else { 786789Sahrens error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ, 7872676Seschrock volblocksize, 0, tx); 788789Sahrens if (error == ENOTSUP) 789789Sahrens error = EBUSY; 790789Sahrens dmu_tx_commit(tx); 7917837SMatthew.Ahrens@Sun.COM if (error == 0) 7927837SMatthew.Ahrens@Sun.COM zv->zv_volblocksize = volblocksize; 793789Sahrens } 794789Sahrens 7957837SMatthew.Ahrens@Sun.COM if (needlock) 7967837SMatthew.Ahrens@Sun.COM mutex_exit(&zvol_state_lock); 797789Sahrens 798789Sahrens return (error); 799789Sahrens } 800789Sahrens 801789Sahrens /*ARGSUSED*/ 802789Sahrens int 803789Sahrens zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr) 804789Sahrens { 805789Sahrens minor_t minor = getminor(*devp); 806789Sahrens zvol_state_t *zv; 807789Sahrens 808789Sahrens if (minor == 0) /* This is the control device */ 809789Sahrens return (0); 810789Sahrens 811789Sahrens mutex_enter(&zvol_state_lock); 812789Sahrens 813789Sahrens zv = ddi_get_soft_state(zvol_state, minor); 814789Sahrens if (zv == NULL) { 815789Sahrens mutex_exit(&zvol_state_lock); 816789Sahrens return (ENXIO); 817789Sahrens } 818789Sahrens 819789Sahrens ASSERT(zv->zv_objset != NULL); 820789Sahrens 821789Sahrens if ((flag & FWRITE) && 8226423Sgw25295 (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY))) { 823789Sahrens mutex_exit(&zvol_state_lock); 824789Sahrens return (EROFS); 825789Sahrens } 8267405SEric.Taylor@Sun.COM if (zv->zv_flags & ZVOL_EXCL) { 8277405SEric.Taylor@Sun.COM mutex_exit(&zvol_state_lock); 8287405SEric.Taylor@Sun.COM return (EBUSY); 8297405SEric.Taylor@Sun.COM } 8307405SEric.Taylor@Sun.COM if (flag & FEXCL) { 8317405SEric.Taylor@Sun.COM if (zv->zv_total_opens != 0) { 8327405SEric.Taylor@Sun.COM mutex_exit(&zvol_state_lock); 8337405SEric.Taylor@Sun.COM return (EBUSY); 8347405SEric.Taylor@Sun.COM } 8357405SEric.Taylor@Sun.COM zv->zv_flags |= ZVOL_EXCL; 8367405SEric.Taylor@Sun.COM } 837789Sahrens 838789Sahrens if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) { 839789Sahrens zv->zv_open_count[otyp]++; 840789Sahrens zv->zv_total_opens++; 841789Sahrens } 842789Sahrens 843789Sahrens mutex_exit(&zvol_state_lock); 844789Sahrens 845789Sahrens return (0); 846789Sahrens } 847789Sahrens 848789Sahrens /*ARGSUSED*/ 849789Sahrens int 850789Sahrens zvol_close(dev_t dev, int flag, int otyp, cred_t *cr) 851789Sahrens { 852789Sahrens minor_t minor = getminor(dev); 853789Sahrens zvol_state_t *zv; 854789Sahrens 855789Sahrens if (minor == 0) /* This is the control device */ 856789Sahrens return (0); 857789Sahrens 858789Sahrens mutex_enter(&zvol_state_lock); 859789Sahrens 860789Sahrens zv = ddi_get_soft_state(zvol_state, minor); 861789Sahrens if (zv == NULL) { 862789Sahrens mutex_exit(&zvol_state_lock); 863789Sahrens return (ENXIO); 864789Sahrens } 865789Sahrens 8667405SEric.Taylor@Sun.COM if (zv->zv_flags & ZVOL_EXCL) { 8677405SEric.Taylor@Sun.COM ASSERT(zv->zv_total_opens == 1); 8687405SEric.Taylor@Sun.COM zv->zv_flags &= ~ZVOL_EXCL; 869789Sahrens } 870789Sahrens 871789Sahrens /* 872789Sahrens * If the open count is zero, this is a spurious close. 873789Sahrens * That indicates a bug in the kernel / DDI framework. 874789Sahrens */ 875789Sahrens ASSERT(zv->zv_open_count[otyp] != 0); 876789Sahrens ASSERT(zv->zv_total_opens != 0); 877789Sahrens 878789Sahrens /* 879789Sahrens * You may get multiple opens, but only one close. 880789Sahrens */ 881789Sahrens zv->zv_open_count[otyp]--; 882789Sahrens zv->zv_total_opens--; 883789Sahrens 884789Sahrens mutex_exit(&zvol_state_lock); 885789Sahrens 886789Sahrens return (0); 887789Sahrens } 888789Sahrens 8893638Sbillm static void 8903063Sperrin zvol_get_done(dmu_buf_t *db, void *vzgd) 8913063Sperrin { 8923063Sperrin zgd_t *zgd = (zgd_t *)vzgd; 8933755Sperrin rl_t *rl = zgd->zgd_rl; 8943063Sperrin 8953063Sperrin dmu_buf_rele(db, vzgd); 8963755Sperrin zfs_range_unlock(rl); 8975688Sbonwick zil_add_block(zgd->zgd_zilog, zgd->zgd_bp); 8983063Sperrin kmem_free(zgd, sizeof (zgd_t)); 8993063Sperrin } 9003063Sperrin 9013063Sperrin /* 9023063Sperrin * Get data to generate a TX_WRITE intent log record. 9033063Sperrin */ 9043638Sbillm static int 9053063Sperrin zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 9063063Sperrin { 9073063Sperrin zvol_state_t *zv = arg; 9083063Sperrin objset_t *os = zv->zv_objset; 9093063Sperrin dmu_buf_t *db; 9103755Sperrin rl_t *rl; 9113063Sperrin zgd_t *zgd; 9123755Sperrin uint64_t boff; /* block starting offset */ 9133755Sperrin int dlen = lr->lr_length; /* length of user data */ 9143063Sperrin int error; 9153063Sperrin 9163063Sperrin ASSERT(zio); 9173755Sperrin ASSERT(dlen != 0); 9183638Sbillm 9193755Sperrin /* 9203755Sperrin * Write records come in two flavors: immediate and indirect. 9213755Sperrin * For small writes it's cheaper to store the data with the 9223755Sperrin * log record (immediate); for large writes it's cheaper to 9233755Sperrin * sync the data and get a pointer to it (indirect) so that 9243755Sperrin * we don't have to write the data twice. 9253755Sperrin */ 9263755Sperrin if (buf != NULL) /* immediate write */ 9273755Sperrin return (dmu_read(os, ZVOL_OBJ, lr->lr_offset, dlen, buf)); 9283063Sperrin 9293063Sperrin zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP); 9303063Sperrin zgd->zgd_zilog = zv->zv_zilog; 9313063Sperrin zgd->zgd_bp = &lr->lr_blkptr; 9323063Sperrin 9333755Sperrin /* 9343755Sperrin * Lock the range of the block to ensure that when the data is 9356423Sgw25295 * written out and its checksum is being calculated that no other 9363755Sperrin * thread can change the block. 9373755Sperrin */ 9383755Sperrin boff = P2ALIGN_TYPED(lr->lr_offset, zv->zv_volblocksize, uint64_t); 9393755Sperrin rl = zfs_range_lock(&zv->zv_znode, boff, zv->zv_volblocksize, 9403755Sperrin RL_READER); 9413755Sperrin zgd->zgd_rl = rl; 9423755Sperrin 9433063Sperrin VERIFY(0 == dmu_buf_hold(os, ZVOL_OBJ, lr->lr_offset, zgd, &db)); 9443063Sperrin error = dmu_sync(zio, db, &lr->lr_blkptr, 9453063Sperrin lr->lr_common.lrc_txg, zvol_get_done, zgd); 9463638Sbillm if (error == 0) 9475688Sbonwick zil_add_block(zv->zv_zilog, &lr->lr_blkptr); 9483063Sperrin /* 9493063Sperrin * If we get EINPROGRESS, then we need to wait for a 9503063Sperrin * write IO initiated by dmu_sync() to complete before 9513063Sperrin * we can release this dbuf. We will finish everything 9523063Sperrin * up in the zvol_get_done() callback. 9533063Sperrin */ 9543063Sperrin if (error == EINPROGRESS) 9553063Sperrin return (0); 9563063Sperrin dmu_buf_rele(db, zgd); 9573755Sperrin zfs_range_unlock(rl); 9583063Sperrin kmem_free(zgd, sizeof (zgd_t)); 9593063Sperrin return (error); 9603063Sperrin } 9613063Sperrin 9621861Sperrin /* 9631861Sperrin * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions. 9641141Sperrin * 9651141Sperrin * We store data in the log buffers if it's small enough. 9663063Sperrin * Otherwise we will later flush the data out via dmu_sync(). 9671141Sperrin */ 9683063Sperrin ssize_t zvol_immediate_write_sz = 32768; 9691141Sperrin 9703638Sbillm static void 9713638Sbillm zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t len) 9721141Sperrin { 9733638Sbillm uint32_t blocksize = zv->zv_volblocksize; 9741141Sperrin lr_write_t *lr; 9751861Sperrin 9761861Sperrin while (len) { 9773638Sbillm ssize_t nbytes = MIN(len, blocksize - P2PHASE(off, blocksize)); 9783638Sbillm itx_t *itx = zil_itx_create(TX_WRITE, sizeof (*lr)); 9793638Sbillm 9803638Sbillm itx->itx_wr_state = 9813638Sbillm len > zvol_immediate_write_sz ? WR_INDIRECT : WR_NEED_COPY; 9823638Sbillm itx->itx_private = zv; 9833638Sbillm lr = (lr_write_t *)&itx->itx_lr; 9843638Sbillm lr->lr_foid = ZVOL_OBJ; 9853638Sbillm lr->lr_offset = off; 9863638Sbillm lr->lr_length = nbytes; 9873638Sbillm lr->lr_blkoff = off - P2ALIGN_TYPED(off, blocksize, uint64_t); 9883638Sbillm BP_ZERO(&lr->lr_blkptr); 9893638Sbillm 9903638Sbillm (void) zil_itx_assign(zv->zv_zilog, itx, tx); 9911861Sperrin len -= nbytes; 9921861Sperrin off += nbytes; 9931141Sperrin } 9941141Sperrin } 9951141Sperrin 9967837SMatthew.Ahrens@Sun.COM static int 9977837SMatthew.Ahrens@Sun.COM zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t size, 9987837SMatthew.Ahrens@Sun.COM boolean_t doread, boolean_t isdump) 9996423Sgw25295 { 10006423Sgw25295 vdev_disk_t *dvd; 10016423Sgw25295 int c; 10026423Sgw25295 int numerrors = 0; 10036423Sgw25295 10046423Sgw25295 for (c = 0; c < vd->vdev_children; c++) { 10057837SMatthew.Ahrens@Sun.COM ASSERT(vd->vdev_ops == &vdev_mirror_ops); 10067837SMatthew.Ahrens@Sun.COM int err = zvol_dumpio_vdev(vd->vdev_child[c], 10077837SMatthew.Ahrens@Sun.COM addr, offset, size, doread, isdump); 10087837SMatthew.Ahrens@Sun.COM ASSERT3U(err, ==, 0); 10097837SMatthew.Ahrens@Sun.COM if (err != 0) { 10106423Sgw25295 numerrors++; 10117837SMatthew.Ahrens@Sun.COM } else if (doread) { 10126423Sgw25295 break; 10136423Sgw25295 } 10146423Sgw25295 } 10156423Sgw25295 10166423Sgw25295 if (!vd->vdev_ops->vdev_op_leaf) 10176423Sgw25295 return (numerrors < vd->vdev_children ? 0 : EIO); 10186423Sgw25295 10197837SMatthew.Ahrens@Sun.COM ASSERT(vdev_writeable(vd)); 10206423Sgw25295 if (!vdev_writeable(vd)) 10216423Sgw25295 return (EIO); 10226423Sgw25295 10236423Sgw25295 dvd = vd->vdev_tsd; 10246423Sgw25295 ASSERT3P(dvd, !=, NULL); 10256423Sgw25295 offset += VDEV_LABEL_START_SIZE; 10266423Sgw25295 10276423Sgw25295 if (ddi_in_panic() || isdump) { 10287837SMatthew.Ahrens@Sun.COM ASSERT(!doread); 10297837SMatthew.Ahrens@Sun.COM if (doread) 10306423Sgw25295 return (EIO); 10316423Sgw25295 return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset), 10326423Sgw25295 lbtodb(size))); 10336423Sgw25295 } else { 10346423Sgw25295 return (vdev_disk_physio(dvd->vd_lh, addr, size, offset, 10357837SMatthew.Ahrens@Sun.COM doread ? B_READ : B_WRITE)); 10366423Sgw25295 } 10376423Sgw25295 } 10386423Sgw25295 10397837SMatthew.Ahrens@Sun.COM static int 10407837SMatthew.Ahrens@Sun.COM zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size, 10417837SMatthew.Ahrens@Sun.COM boolean_t doread, boolean_t isdump) 10426423Sgw25295 { 10436423Sgw25295 vdev_t *vd; 10446423Sgw25295 int error; 10457837SMatthew.Ahrens@Sun.COM zvol_extent_t *ze; 10466423Sgw25295 spa_t *spa = dmu_objset_spa(zv->zv_objset); 10476423Sgw25295 10487837SMatthew.Ahrens@Sun.COM /* Must be sector aligned, and not stradle a block boundary. */ 10497837SMatthew.Ahrens@Sun.COM if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) || 10507837SMatthew.Ahrens@Sun.COM P2BOUNDARY(offset, size, zv->zv_volblocksize)) { 10517837SMatthew.Ahrens@Sun.COM return (EINVAL); 10527837SMatthew.Ahrens@Sun.COM } 10536423Sgw25295 ASSERT(size <= zv->zv_volblocksize); 10546423Sgw25295 10557837SMatthew.Ahrens@Sun.COM /* Locate the extent this belongs to */ 10567837SMatthew.Ahrens@Sun.COM ze = list_head(&zv->zv_extents); 10577837SMatthew.Ahrens@Sun.COM while (offset >= ze->ze_nblks * zv->zv_volblocksize) { 10587837SMatthew.Ahrens@Sun.COM offset -= ze->ze_nblks * zv->zv_volblocksize; 10597837SMatthew.Ahrens@Sun.COM ze = list_next(&zv->zv_extents, ze); 10607837SMatthew.Ahrens@Sun.COM } 10617754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 10627837SMatthew.Ahrens@Sun.COM vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva)); 10637837SMatthew.Ahrens@Sun.COM offset += DVA_GET_OFFSET(&ze->ze_dva); 10647837SMatthew.Ahrens@Sun.COM error = zvol_dumpio_vdev(vd, addr, offset, size, doread, isdump); 10657754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 10666423Sgw25295 return (error); 10676423Sgw25295 } 10686423Sgw25295 10696423Sgw25295 int 1070789Sahrens zvol_strategy(buf_t *bp) 1071789Sahrens { 1072789Sahrens zvol_state_t *zv = ddi_get_soft_state(zvol_state, getminor(bp->b_edev)); 1073789Sahrens uint64_t off, volsize; 10747837SMatthew.Ahrens@Sun.COM size_t resid; 1075789Sahrens char *addr; 10761141Sperrin objset_t *os; 10773755Sperrin rl_t *rl; 1078789Sahrens int error = 0; 10797837SMatthew.Ahrens@Sun.COM boolean_t doread = bp->b_flags & B_READ; 10807837SMatthew.Ahrens@Sun.COM boolean_t is_dump = zv->zv_flags & ZVOL_DUMPIFIED; 1081789Sahrens 1082789Sahrens if (zv == NULL) { 1083789Sahrens bioerror(bp, ENXIO); 1084789Sahrens biodone(bp); 1085789Sahrens return (0); 1086789Sahrens } 1087789Sahrens 1088789Sahrens if (getminor(bp->b_edev) == 0) { 1089789Sahrens bioerror(bp, EINVAL); 1090789Sahrens biodone(bp); 1091789Sahrens return (0); 1092789Sahrens } 1093789Sahrens 10946423Sgw25295 if (!(bp->b_flags & B_READ) && 10956423Sgw25295 (zv->zv_flags & ZVOL_RDONLY || 10966423Sgw25295 zv->zv_mode & DS_MODE_READONLY)) { 1097789Sahrens bioerror(bp, EROFS); 1098789Sahrens biodone(bp); 1099789Sahrens return (0); 1100789Sahrens } 1101789Sahrens 1102789Sahrens off = ldbtob(bp->b_blkno); 1103789Sahrens volsize = zv->zv_volsize; 1104789Sahrens 11051141Sperrin os = zv->zv_objset; 11061141Sperrin ASSERT(os != NULL); 1107789Sahrens 1108789Sahrens bp_mapin(bp); 1109789Sahrens addr = bp->b_un.b_addr; 1110789Sahrens resid = bp->b_bcount; 1111789Sahrens 11127837SMatthew.Ahrens@Sun.COM if (resid > 0 && (off < 0 || off >= volsize)) { 11137837SMatthew.Ahrens@Sun.COM bioerror(bp, EIO); 11147837SMatthew.Ahrens@Sun.COM biodone(bp); 11157837SMatthew.Ahrens@Sun.COM return (0); 11167837SMatthew.Ahrens@Sun.COM } 11177013Sgw25295 11181861Sperrin /* 11191861Sperrin * There must be no buffer changes when doing a dmu_sync() because 11201861Sperrin * we can't change the data whilst calculating the checksum. 11211861Sperrin */ 11223755Sperrin rl = zfs_range_lock(&zv->zv_znode, off, resid, 11237837SMatthew.Ahrens@Sun.COM doread ? RL_READER : RL_WRITER); 11246423Sgw25295 1125789Sahrens while (resid != 0 && off < volsize) { 11267837SMatthew.Ahrens@Sun.COM size_t size = MIN(resid, zvol_maxphys); 11276423Sgw25295 if (is_dump) { 11286423Sgw25295 size = MIN(size, P2END(off, zv->zv_volblocksize) - off); 11297837SMatthew.Ahrens@Sun.COM error = zvol_dumpio(zv, addr, off, size, 11307837SMatthew.Ahrens@Sun.COM doread, B_FALSE); 11317837SMatthew.Ahrens@Sun.COM } else if (doread) { 11321861Sperrin error = dmu_read(os, ZVOL_OBJ, off, size, addr); 1133789Sahrens } else { 11341141Sperrin dmu_tx_t *tx = dmu_tx_create(os); 1135789Sahrens dmu_tx_hold_write(tx, ZVOL_OBJ, off, size); 1136789Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 1137789Sahrens if (error) { 1138789Sahrens dmu_tx_abort(tx); 1139789Sahrens } else { 11401141Sperrin dmu_write(os, ZVOL_OBJ, off, size, addr, tx); 11413638Sbillm zvol_log_write(zv, tx, off, size); 1142789Sahrens dmu_tx_commit(tx); 1143789Sahrens } 1144789Sahrens } 11457294Sperrin if (error) { 11467294Sperrin /* convert checksum errors into IO errors */ 11477294Sperrin if (error == ECKSUM) 11487294Sperrin error = EIO; 1149789Sahrens break; 11507294Sperrin } 1151789Sahrens off += size; 1152789Sahrens addr += size; 1153789Sahrens resid -= size; 1154789Sahrens } 11553755Sperrin zfs_range_unlock(rl); 1156789Sahrens 1157789Sahrens if ((bp->b_resid = resid) == bp->b_bcount) 1158789Sahrens bioerror(bp, off > volsize ? EINVAL : error); 1159789Sahrens 11607837SMatthew.Ahrens@Sun.COM if (!(bp->b_flags & B_ASYNC) && !doread && !zil_disable && !is_dump) 11613638Sbillm zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ); 11623638Sbillm biodone(bp); 11631141Sperrin 1164789Sahrens return (0); 1165789Sahrens } 1166789Sahrens 11673063Sperrin /* 11683063Sperrin * Set the buffer count to the zvol maximum transfer. 11693063Sperrin * Using our own routine instead of the default minphys() 11703063Sperrin * means that for larger writes we write bigger buffers on X86 11713063Sperrin * (128K instead of 56K) and flush the disk write cache less often 11723063Sperrin * (every zvol_maxphys - currently 1MB) instead of minphys (currently 11733063Sperrin * 56K on X86 and 128K on sparc). 11743063Sperrin */ 11753063Sperrin void 11763063Sperrin zvol_minphys(struct buf *bp) 11773063Sperrin { 11783063Sperrin if (bp->b_bcount > zvol_maxphys) 11793063Sperrin bp->b_bcount = zvol_maxphys; 11803063Sperrin } 11813063Sperrin 11826423Sgw25295 int 11836423Sgw25295 zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks) 11846423Sgw25295 { 11856423Sgw25295 minor_t minor = getminor(dev); 11866423Sgw25295 zvol_state_t *zv; 11876423Sgw25295 int error = 0; 11886423Sgw25295 uint64_t size; 11896423Sgw25295 uint64_t boff; 11906423Sgw25295 uint64_t resid; 11916423Sgw25295 11926423Sgw25295 if (minor == 0) /* This is the control device */ 11936423Sgw25295 return (ENXIO); 11946423Sgw25295 11956423Sgw25295 zv = ddi_get_soft_state(zvol_state, minor); 11966423Sgw25295 if (zv == NULL) 11976423Sgw25295 return (ENXIO); 11986423Sgw25295 11996423Sgw25295 boff = ldbtob(blkno); 12006423Sgw25295 resid = ldbtob(nblocks); 12017837SMatthew.Ahrens@Sun.COM 12027837SMatthew.Ahrens@Sun.COM VERIFY3U(boff + resid, <=, zv->zv_volsize); 12037837SMatthew.Ahrens@Sun.COM 12046423Sgw25295 while (resid) { 12056423Sgw25295 size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff); 12067837SMatthew.Ahrens@Sun.COM error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE); 12076423Sgw25295 if (error) 12086423Sgw25295 break; 12096423Sgw25295 boff += size; 12106423Sgw25295 addr += size; 12116423Sgw25295 resid -= size; 12126423Sgw25295 } 12136423Sgw25295 12146423Sgw25295 return (error); 12156423Sgw25295 } 12166423Sgw25295 1217789Sahrens /*ARGSUSED*/ 1218789Sahrens int 12193638Sbillm zvol_read(dev_t dev, uio_t *uio, cred_t *cr) 1220789Sahrens { 12214107Sgw25295 minor_t minor = getminor(dev); 12224107Sgw25295 zvol_state_t *zv; 12237013Sgw25295 uint64_t volsize; 12243755Sperrin rl_t *rl; 12253638Sbillm int error = 0; 12263638Sbillm 12274107Sgw25295 if (minor == 0) /* This is the control device */ 12284107Sgw25295 return (ENXIO); 12294107Sgw25295 12304107Sgw25295 zv = ddi_get_soft_state(zvol_state, minor); 12314107Sgw25295 if (zv == NULL) 12324107Sgw25295 return (ENXIO); 12334107Sgw25295 12347013Sgw25295 volsize = zv->zv_volsize; 12357013Sgw25295 if (uio->uio_resid > 0 && 12367013Sgw25295 (uio->uio_loffset < 0 || uio->uio_loffset >= volsize)) 12377013Sgw25295 return (EIO); 12387013Sgw25295 12397837SMatthew.Ahrens@Sun.COM if (zv->zv_flags & ZVOL_DUMPIFIED) { 12407837SMatthew.Ahrens@Sun.COM error = physio(zvol_strategy, NULL, dev, B_READ, 12417837SMatthew.Ahrens@Sun.COM zvol_minphys, uio); 12427837SMatthew.Ahrens@Sun.COM return (error); 12437837SMatthew.Ahrens@Sun.COM } 12447837SMatthew.Ahrens@Sun.COM 12453755Sperrin rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 12463755Sperrin RL_READER); 12477013Sgw25295 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 12483638Sbillm uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 12493638Sbillm 12507013Sgw25295 /* don't read past the end */ 12517013Sgw25295 if (bytes > volsize - uio->uio_loffset) 12527013Sgw25295 bytes = volsize - uio->uio_loffset; 12537013Sgw25295 12543638Sbillm error = dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes); 12557294Sperrin if (error) { 12567294Sperrin /* convert checksum errors into IO errors */ 12577294Sperrin if (error == ECKSUM) 12587294Sperrin error = EIO; 12593638Sbillm break; 12607294Sperrin } 12613638Sbillm } 12623755Sperrin zfs_range_unlock(rl); 12633638Sbillm return (error); 1264789Sahrens } 1265789Sahrens 1266789Sahrens /*ARGSUSED*/ 1267789Sahrens int 12683638Sbillm zvol_write(dev_t dev, uio_t *uio, cred_t *cr) 1269789Sahrens { 12704107Sgw25295 minor_t minor = getminor(dev); 12714107Sgw25295 zvol_state_t *zv; 12727013Sgw25295 uint64_t volsize; 12733755Sperrin rl_t *rl; 12743638Sbillm int error = 0; 12753638Sbillm 12764107Sgw25295 if (minor == 0) /* This is the control device */ 12774107Sgw25295 return (ENXIO); 12784107Sgw25295 12794107Sgw25295 zv = ddi_get_soft_state(zvol_state, minor); 12804107Sgw25295 if (zv == NULL) 12814107Sgw25295 return (ENXIO); 12824107Sgw25295 12837013Sgw25295 volsize = zv->zv_volsize; 12847013Sgw25295 if (uio->uio_resid > 0 && 12857013Sgw25295 (uio->uio_loffset < 0 || uio->uio_loffset >= volsize)) 12867013Sgw25295 return (EIO); 12877013Sgw25295 12886423Sgw25295 if (zv->zv_flags & ZVOL_DUMPIFIED) { 12896423Sgw25295 error = physio(zvol_strategy, NULL, dev, B_WRITE, 12906423Sgw25295 zvol_minphys, uio); 12916423Sgw25295 return (error); 12926423Sgw25295 } 12936423Sgw25295 12943755Sperrin rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 12953755Sperrin RL_WRITER); 12967013Sgw25295 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 12973638Sbillm uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 12983638Sbillm uint64_t off = uio->uio_loffset; 12997013Sgw25295 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset); 1300789Sahrens 13017013Sgw25295 if (bytes > volsize - off) /* don't write past the end */ 13027013Sgw25295 bytes = volsize - off; 13037013Sgw25295 13043638Sbillm dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 13053638Sbillm error = dmu_tx_assign(tx, TXG_WAIT); 13063638Sbillm if (error) { 13073638Sbillm dmu_tx_abort(tx); 13083638Sbillm break; 13093638Sbillm } 13103638Sbillm error = dmu_write_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes, tx); 13113638Sbillm if (error == 0) 13123638Sbillm zvol_log_write(zv, tx, off, bytes); 13133638Sbillm dmu_tx_commit(tx); 13143638Sbillm 13153638Sbillm if (error) 13163638Sbillm break; 13173638Sbillm } 13183755Sperrin zfs_range_unlock(rl); 13193638Sbillm return (error); 1320789Sahrens } 1321789Sahrens 13227405SEric.Taylor@Sun.COM int 13237405SEric.Taylor@Sun.COM zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs) 13247405SEric.Taylor@Sun.COM { 13257405SEric.Taylor@Sun.COM struct uuid uuid = EFI_RESERVED; 13267405SEric.Taylor@Sun.COM efi_gpe_t gpe = { 0 }; 13277405SEric.Taylor@Sun.COM uint32_t crc; 13287405SEric.Taylor@Sun.COM dk_efi_t efi; 13297405SEric.Taylor@Sun.COM int length; 13307405SEric.Taylor@Sun.COM char *ptr; 13317405SEric.Taylor@Sun.COM 13327405SEric.Taylor@Sun.COM if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag)) 13337405SEric.Taylor@Sun.COM return (EFAULT); 13347405SEric.Taylor@Sun.COM ptr = (char *)(uintptr_t)efi.dki_data_64; 13357405SEric.Taylor@Sun.COM length = efi.dki_length; 13367405SEric.Taylor@Sun.COM /* 13377405SEric.Taylor@Sun.COM * Some clients may attempt to request a PMBR for the 13387405SEric.Taylor@Sun.COM * zvol. Currently this interface will return EINVAL to 13397405SEric.Taylor@Sun.COM * such requests. These requests could be supported by 13407405SEric.Taylor@Sun.COM * adding a check for lba == 0 and consing up an appropriate 13417405SEric.Taylor@Sun.COM * PMBR. 13427405SEric.Taylor@Sun.COM */ 13437405SEric.Taylor@Sun.COM if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0) 13447405SEric.Taylor@Sun.COM return (EINVAL); 13457405SEric.Taylor@Sun.COM 13467405SEric.Taylor@Sun.COM gpe.efi_gpe_StartingLBA = LE_64(34ULL); 13477405SEric.Taylor@Sun.COM gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1); 13487405SEric.Taylor@Sun.COM UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid); 13497405SEric.Taylor@Sun.COM 13507405SEric.Taylor@Sun.COM if (efi.dki_lba == 1) { 13517405SEric.Taylor@Sun.COM efi_gpt_t gpt = { 0 }; 13527405SEric.Taylor@Sun.COM 13537405SEric.Taylor@Sun.COM gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE); 13547405SEric.Taylor@Sun.COM gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT); 13557405SEric.Taylor@Sun.COM gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt)); 13567405SEric.Taylor@Sun.COM gpt.efi_gpt_MyLBA = LE_64(1ULL); 13577405SEric.Taylor@Sun.COM gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL); 13587405SEric.Taylor@Sun.COM gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1); 13597405SEric.Taylor@Sun.COM gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL); 13607405SEric.Taylor@Sun.COM gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1); 13617405SEric.Taylor@Sun.COM gpt.efi_gpt_SizeOfPartitionEntry = 13627405SEric.Taylor@Sun.COM LE_32(sizeof (efi_gpe_t)); 13637405SEric.Taylor@Sun.COM CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table); 13647405SEric.Taylor@Sun.COM gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc); 13657405SEric.Taylor@Sun.COM CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table); 13667405SEric.Taylor@Sun.COM gpt.efi_gpt_HeaderCRC32 = LE_32(~crc); 13677405SEric.Taylor@Sun.COM if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length), 13687405SEric.Taylor@Sun.COM flag)) 13697405SEric.Taylor@Sun.COM return (EFAULT); 13707405SEric.Taylor@Sun.COM ptr += sizeof (gpt); 13717405SEric.Taylor@Sun.COM length -= sizeof (gpt); 13727405SEric.Taylor@Sun.COM } 13737405SEric.Taylor@Sun.COM if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe), 13747405SEric.Taylor@Sun.COM length), flag)) 13757405SEric.Taylor@Sun.COM return (EFAULT); 13767405SEric.Taylor@Sun.COM return (0); 13777405SEric.Taylor@Sun.COM } 13787405SEric.Taylor@Sun.COM 1379789Sahrens /* 1380789Sahrens * Dirtbag ioctls to support mkfs(1M) for UFS filesystems. See dkio(7I). 1381789Sahrens */ 1382789Sahrens /*ARGSUSED*/ 1383789Sahrens int 1384789Sahrens zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 1385789Sahrens { 1386789Sahrens zvol_state_t *zv; 13873897Smaybee struct dk_cinfo dki; 1388789Sahrens struct dk_minfo dkm; 13893897Smaybee struct dk_callback *dkc; 1390789Sahrens int error = 0; 13916423Sgw25295 rl_t *rl; 1392789Sahrens 1393789Sahrens mutex_enter(&zvol_state_lock); 1394789Sahrens 1395789Sahrens zv = ddi_get_soft_state(zvol_state, getminor(dev)); 1396789Sahrens 1397789Sahrens if (zv == NULL) { 1398789Sahrens mutex_exit(&zvol_state_lock); 1399789Sahrens return (ENXIO); 1400789Sahrens } 1401789Sahrens 1402789Sahrens switch (cmd) { 1403789Sahrens 1404789Sahrens case DKIOCINFO: 14053897Smaybee bzero(&dki, sizeof (dki)); 14063897Smaybee (void) strcpy(dki.dki_cname, "zvol"); 14073897Smaybee (void) strcpy(dki.dki_dname, "zvol"); 14083897Smaybee dki.dki_ctype = DKC_UNKNOWN; 14093897Smaybee dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs); 1410789Sahrens mutex_exit(&zvol_state_lock); 14113897Smaybee if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag)) 1412789Sahrens error = EFAULT; 1413789Sahrens return (error); 1414789Sahrens 1415789Sahrens case DKIOCGMEDIAINFO: 1416789Sahrens bzero(&dkm, sizeof (dkm)); 1417789Sahrens dkm.dki_lbsize = 1U << zv->zv_min_bs; 1418789Sahrens dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs; 1419789Sahrens dkm.dki_media_type = DK_UNKNOWN; 1420789Sahrens mutex_exit(&zvol_state_lock); 1421789Sahrens if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag)) 1422789Sahrens error = EFAULT; 1423789Sahrens return (error); 1424789Sahrens 1425789Sahrens case DKIOCGETEFI: 14267405SEric.Taylor@Sun.COM { 14277405SEric.Taylor@Sun.COM uint64_t vs = zv->zv_volsize; 14287405SEric.Taylor@Sun.COM uint8_t bs = zv->zv_min_bs; 14293016Smaybee 14303016Smaybee mutex_exit(&zvol_state_lock); 14317405SEric.Taylor@Sun.COM error = zvol_getefi((void *)arg, flag, vs, bs); 14327405SEric.Taylor@Sun.COM return (error); 14333016Smaybee } 1434789Sahrens 14353638Sbillm case DKIOCFLUSHWRITECACHE: 14363897Smaybee dkc = (struct dk_callback *)arg; 14373638Sbillm zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ); 14383897Smaybee if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) { 14393897Smaybee (*dkc->dkc_callback)(dkc->dkc_cookie, error); 14403897Smaybee error = 0; 14413897Smaybee } 14423638Sbillm break; 14433638Sbillm 14443245Smaybee case DKIOCGGEOM: 14453245Smaybee case DKIOCGVTOC: 14466423Sgw25295 /* 14476423Sgw25295 * commands using these (like prtvtoc) expect ENOTSUP 14486423Sgw25295 * since we're emulating an EFI label 14496423Sgw25295 */ 14503245Smaybee error = ENOTSUP; 14513245Smaybee break; 14523245Smaybee 14536423Sgw25295 case DKIOCDUMPINIT: 14546423Sgw25295 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 14556423Sgw25295 RL_WRITER); 14566423Sgw25295 error = zvol_dumpify(zv); 14576423Sgw25295 zfs_range_unlock(rl); 14586423Sgw25295 break; 14596423Sgw25295 14606423Sgw25295 case DKIOCDUMPFINI: 14616423Sgw25295 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 14626423Sgw25295 RL_WRITER); 14636423Sgw25295 error = zvol_dump_fini(zv); 14646423Sgw25295 zfs_range_unlock(rl); 14656423Sgw25295 break; 14666423Sgw25295 1467789Sahrens default: 14683016Smaybee error = ENOTTY; 1469789Sahrens break; 1470789Sahrens 1471789Sahrens } 1472789Sahrens mutex_exit(&zvol_state_lock); 1473789Sahrens return (error); 1474789Sahrens } 1475789Sahrens 1476789Sahrens int 1477789Sahrens zvol_busy(void) 1478789Sahrens { 1479789Sahrens return (zvol_minors != 0); 1480789Sahrens } 1481789Sahrens 1482789Sahrens void 1483789Sahrens zvol_init(void) 1484789Sahrens { 1485789Sahrens VERIFY(ddi_soft_state_init(&zvol_state, sizeof (zvol_state_t), 1) == 0); 1486789Sahrens mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL); 1487789Sahrens } 1488789Sahrens 1489789Sahrens void 1490789Sahrens zvol_fini(void) 1491789Sahrens { 1492789Sahrens mutex_destroy(&zvol_state_lock); 1493789Sahrens ddi_soft_state_fini(&zvol_state); 1494789Sahrens } 14956423Sgw25295 14966423Sgw25295 static boolean_t 14976423Sgw25295 zvol_is_swap(zvol_state_t *zv) 14986423Sgw25295 { 14996423Sgw25295 vnode_t *vp; 15006423Sgw25295 boolean_t ret = B_FALSE; 15016423Sgw25295 char *devpath; 15026423Sgw25295 size_t devpathlen; 15036423Sgw25295 int error; 15046423Sgw25295 15056423Sgw25295 devpathlen = strlen(ZVOL_FULL_DEV_DIR) + strlen(zv->zv_name) + 1; 15066423Sgw25295 devpath = kmem_alloc(devpathlen, KM_SLEEP); 15076423Sgw25295 (void) sprintf(devpath, "%s%s", ZVOL_FULL_DEV_DIR, zv->zv_name); 15086423Sgw25295 error = lookupname(devpath, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp); 15096423Sgw25295 kmem_free(devpath, devpathlen); 15106423Sgw25295 15116423Sgw25295 ret = !error && IS_SWAPVP(common_specvp(vp)); 15126423Sgw25295 15136423Sgw25295 if (vp != NULL) 15146423Sgw25295 VN_RELE(vp); 15156423Sgw25295 15166423Sgw25295 return (ret); 15176423Sgw25295 } 15186423Sgw25295 15196423Sgw25295 static int 15206423Sgw25295 zvol_dump_init(zvol_state_t *zv, boolean_t resize) 15216423Sgw25295 { 15226423Sgw25295 dmu_tx_t *tx; 15236423Sgw25295 int error = 0; 15246423Sgw25295 objset_t *os = zv->zv_objset; 15256423Sgw25295 nvlist_t *nv = NULL; 15266423Sgw25295 15276423Sgw25295 ASSERT(MUTEX_HELD(&zvol_state_lock)); 15286423Sgw25295 15296423Sgw25295 tx = dmu_tx_create(os); 15306423Sgw25295 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 15316423Sgw25295 error = dmu_tx_assign(tx, TXG_WAIT); 15326423Sgw25295 if (error) { 15336423Sgw25295 dmu_tx_abort(tx); 15346423Sgw25295 return (error); 15356423Sgw25295 } 15366423Sgw25295 15376423Sgw25295 /* 15386423Sgw25295 * If we are resizing the dump device then we only need to 15396423Sgw25295 * update the refreservation to match the newly updated 15406423Sgw25295 * zvolsize. Otherwise, we save off the original state of the 15416423Sgw25295 * zvol so that we can restore them if the zvol is ever undumpified. 15426423Sgw25295 */ 15436423Sgw25295 if (resize) { 15446423Sgw25295 error = zap_update(os, ZVOL_ZAP_OBJ, 15456423Sgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 15466423Sgw25295 &zv->zv_volsize, tx); 15476423Sgw25295 } else { 15487837SMatthew.Ahrens@Sun.COM uint64_t checksum, compress, refresrv, vbs; 15497837SMatthew.Ahrens@Sun.COM 15506423Sgw25295 error = dsl_prop_get_integer(zv->zv_name, 15516423Sgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL); 15526423Sgw25295 error = error ? error : dsl_prop_get_integer(zv->zv_name, 15536423Sgw25295 zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL); 15546423Sgw25295 error = error ? error : dsl_prop_get_integer(zv->zv_name, 15556423Sgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL); 15567837SMatthew.Ahrens@Sun.COM error = error ? error : dsl_prop_get_integer(zv->zv_name, 15577837SMatthew.Ahrens@Sun.COM zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL); 15586423Sgw25295 15596423Sgw25295 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 15606423Sgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, 15616423Sgw25295 &compress, tx); 15626423Sgw25295 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 15636423Sgw25295 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx); 15646423Sgw25295 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 15656423Sgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 15666423Sgw25295 &refresrv, tx); 15677837SMatthew.Ahrens@Sun.COM error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 15687837SMatthew.Ahrens@Sun.COM zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, 15697837SMatthew.Ahrens@Sun.COM &vbs, tx); 15706423Sgw25295 } 15716423Sgw25295 dmu_tx_commit(tx); 15726423Sgw25295 15736423Sgw25295 /* Truncate the file */ 15746423Sgw25295 if (!error) 15756992Smaybee error = dmu_free_long_range(zv->zv_objset, 15766992Smaybee ZVOL_OBJ, 0, DMU_OBJECT_END); 15776423Sgw25295 15786423Sgw25295 if (error) 15796423Sgw25295 return (error); 15806423Sgw25295 15816423Sgw25295 /* 15826423Sgw25295 * We only need update the zvol's property if we are initializing 15836423Sgw25295 * the dump area for the first time. 15846423Sgw25295 */ 15856423Sgw25295 if (!resize) { 15866423Sgw25295 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 15876423Sgw25295 VERIFY(nvlist_add_uint64(nv, 15886423Sgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0); 15896423Sgw25295 VERIFY(nvlist_add_uint64(nv, 15906423Sgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 15916423Sgw25295 ZIO_COMPRESS_OFF) == 0); 15926423Sgw25295 VERIFY(nvlist_add_uint64(nv, 15936423Sgw25295 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 15946423Sgw25295 ZIO_CHECKSUM_OFF) == 0); 15957837SMatthew.Ahrens@Sun.COM VERIFY(nvlist_add_uint64(nv, 15967837SMatthew.Ahrens@Sun.COM zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 15977837SMatthew.Ahrens@Sun.COM SPA_MAXBLOCKSIZE) == 0); 15986423Sgw25295 15996423Sgw25295 error = zfs_set_prop_nvlist(zv->zv_name, nv); 16006423Sgw25295 nvlist_free(nv); 16016423Sgw25295 16026423Sgw25295 if (error) 16036423Sgw25295 return (error); 16046423Sgw25295 } 16056423Sgw25295 16066423Sgw25295 /* Allocate the space for the dump */ 16076423Sgw25295 error = zvol_prealloc(zv); 16086423Sgw25295 return (error); 16096423Sgw25295 } 16106423Sgw25295 16116423Sgw25295 static int 16126423Sgw25295 zvol_dumpify(zvol_state_t *zv) 16136423Sgw25295 { 16146423Sgw25295 int error = 0; 16156423Sgw25295 uint64_t dumpsize = 0; 16166423Sgw25295 dmu_tx_t *tx; 16176423Sgw25295 objset_t *os = zv->zv_objset; 16186423Sgw25295 16196423Sgw25295 if (zv->zv_flags & ZVOL_RDONLY || (zv->zv_mode & DS_MODE_READONLY)) 16206423Sgw25295 return (EROFS); 16216423Sgw25295 16226423Sgw25295 /* 16236423Sgw25295 * We do not support swap devices acting as dump devices. 16246423Sgw25295 */ 16256423Sgw25295 if (zvol_is_swap(zv)) 16266423Sgw25295 return (ENOTSUP); 16276423Sgw25295 16286423Sgw25295 if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 16296423Sgw25295 8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) { 16306423Sgw25295 boolean_t resize = (dumpsize > 0) ? B_TRUE : B_FALSE; 16316423Sgw25295 16326423Sgw25295 if ((error = zvol_dump_init(zv, resize)) != 0) { 16336423Sgw25295 (void) zvol_dump_fini(zv); 16346423Sgw25295 return (error); 16356423Sgw25295 } 16366423Sgw25295 } 16376423Sgw25295 16386423Sgw25295 /* 16396423Sgw25295 * Build up our lba mapping. 16406423Sgw25295 */ 16416423Sgw25295 error = zvol_get_lbas(zv); 16426423Sgw25295 if (error) { 16436423Sgw25295 (void) zvol_dump_fini(zv); 16446423Sgw25295 return (error); 16456423Sgw25295 } 16466423Sgw25295 16476423Sgw25295 tx = dmu_tx_create(os); 16486423Sgw25295 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 16496423Sgw25295 error = dmu_tx_assign(tx, TXG_WAIT); 16506423Sgw25295 if (error) { 16516423Sgw25295 dmu_tx_abort(tx); 16526423Sgw25295 (void) zvol_dump_fini(zv); 16536423Sgw25295 return (error); 16546423Sgw25295 } 16556423Sgw25295 16566423Sgw25295 zv->zv_flags |= ZVOL_DUMPIFIED; 16576423Sgw25295 error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1, 16586423Sgw25295 &zv->zv_volsize, tx); 16596423Sgw25295 dmu_tx_commit(tx); 16606423Sgw25295 16616423Sgw25295 if (error) { 16626423Sgw25295 (void) zvol_dump_fini(zv); 16636423Sgw25295 return (error); 16646423Sgw25295 } 16656423Sgw25295 16666423Sgw25295 txg_wait_synced(dmu_objset_pool(os), 0); 16676423Sgw25295 return (0); 16686423Sgw25295 } 16696423Sgw25295 16706423Sgw25295 static int 16716423Sgw25295 zvol_dump_fini(zvol_state_t *zv) 16726423Sgw25295 { 16736423Sgw25295 dmu_tx_t *tx; 16746423Sgw25295 objset_t *os = zv->zv_objset; 16756423Sgw25295 nvlist_t *nv; 16766423Sgw25295 int error = 0; 16777837SMatthew.Ahrens@Sun.COM uint64_t checksum, compress, refresrv, vbs; 16786423Sgw25295 16797080Smaybee /* 16807080Smaybee * Attempt to restore the zvol back to its pre-dumpified state. 16817080Smaybee * This is a best-effort attempt as it's possible that not all 16827080Smaybee * of these properties were initialized during the dumpify process 16837080Smaybee * (i.e. error during zvol_dump_init). 16847080Smaybee */ 16857080Smaybee 16866423Sgw25295 tx = dmu_tx_create(os); 16876423Sgw25295 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 16886423Sgw25295 error = dmu_tx_assign(tx, TXG_WAIT); 16896423Sgw25295 if (error) { 16906423Sgw25295 dmu_tx_abort(tx); 16916423Sgw25295 return (error); 16926423Sgw25295 } 16937080Smaybee (void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx); 16947080Smaybee dmu_tx_commit(tx); 16956423Sgw25295 16966423Sgw25295 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 16976423Sgw25295 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum); 16986423Sgw25295 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 16996423Sgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress); 17006423Sgw25295 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 17016423Sgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv); 17027837SMatthew.Ahrens@Sun.COM (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 17037837SMatthew.Ahrens@Sun.COM zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs); 17046423Sgw25295 17056423Sgw25295 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 17066423Sgw25295 (void) nvlist_add_uint64(nv, 17076423Sgw25295 zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum); 17086423Sgw25295 (void) nvlist_add_uint64(nv, 17096423Sgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress); 17106423Sgw25295 (void) nvlist_add_uint64(nv, 17116423Sgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv); 17127837SMatthew.Ahrens@Sun.COM (void) nvlist_add_uint64(nv, 17137837SMatthew.Ahrens@Sun.COM zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), vbs); 17146423Sgw25295 (void) zfs_set_prop_nvlist(zv->zv_name, nv); 17156423Sgw25295 nvlist_free(nv); 17166423Sgw25295 17177080Smaybee zvol_free_extents(zv); 17187080Smaybee zv->zv_flags &= ~ZVOL_DUMPIFIED; 17197080Smaybee (void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END); 17207080Smaybee 17216423Sgw25295 return (0); 17226423Sgw25295 } 1723