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 25*12294SMark.Musante@Sun.COM /* Portions Copyright 2010 Robert Milkowski */ 26*12294SMark.Musante@Sun.COM 27789Sahrens /* 28789Sahrens * ZFS volume emulation driver. 29789Sahrens * 30789Sahrens * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes. 31789Sahrens * Volumes are accessed through the symbolic links named: 32789Sahrens * 33789Sahrens * /dev/zvol/dsk/<pool_name>/<dataset_name> 34789Sahrens * /dev/zvol/rdsk/<pool_name>/<dataset_name> 35789Sahrens * 3610588SEric.Taylor@Sun.COM * These links are created by the /dev filesystem (sdev_zvolops.c). 37789Sahrens * Volumes are persistent through reboot. No user command needs to be 38789Sahrens * run before opening and using a device. 39789Sahrens */ 40789Sahrens 41789Sahrens #include <sys/types.h> 42789Sahrens #include <sys/param.h> 43789Sahrens #include <sys/errno.h> 44789Sahrens #include <sys/uio.h> 45789Sahrens #include <sys/buf.h> 46789Sahrens #include <sys/modctl.h> 47789Sahrens #include <sys/open.h> 48789Sahrens #include <sys/kmem.h> 49789Sahrens #include <sys/conf.h> 50789Sahrens #include <sys/cmn_err.h> 51789Sahrens #include <sys/stat.h> 52789Sahrens #include <sys/zap.h> 53789Sahrens #include <sys/spa.h> 54789Sahrens #include <sys/zio.h> 556423Sgw25295 #include <sys/dmu_traverse.h> 566423Sgw25295 #include <sys/dnode.h> 576423Sgw25295 #include <sys/dsl_dataset.h> 58789Sahrens #include <sys/dsl_prop.h> 59789Sahrens #include <sys/dkio.h> 60789Sahrens #include <sys/efi_partition.h> 61789Sahrens #include <sys/byteorder.h> 62789Sahrens #include <sys/pathname.h> 63789Sahrens #include <sys/ddi.h> 64789Sahrens #include <sys/sunddi.h> 65789Sahrens #include <sys/crc32.h> 66789Sahrens #include <sys/dirent.h> 67789Sahrens #include <sys/policy.h> 68789Sahrens #include <sys/fs/zfs.h> 69789Sahrens #include <sys/zfs_ioctl.h> 70789Sahrens #include <sys/mkdev.h> 711141Sperrin #include <sys/zil.h> 722237Smaybee #include <sys/refcount.h> 733755Sperrin #include <sys/zfs_znode.h> 743755Sperrin #include <sys/zfs_rlock.h> 756423Sgw25295 #include <sys/vdev_disk.h> 766423Sgw25295 #include <sys/vdev_impl.h> 776423Sgw25295 #include <sys/zvol.h> 786423Sgw25295 #include <sys/dumphdr.h> 798227SNeil.Perrin@Sun.COM #include <sys/zil_impl.h> 80789Sahrens 81789Sahrens #include "zfs_namecheck.h" 82789Sahrens 836423Sgw25295 static void *zvol_state; 8410298SMatthew.Ahrens@Sun.COM static char *zvol_tag = "zvol_tag"; 85789Sahrens 866423Sgw25295 #define ZVOL_DUMPSIZE "dumpsize" 87789Sahrens 88789Sahrens /* 89789Sahrens * This lock protects the zvol_state structure from being modified 90789Sahrens * while it's being used, e.g. an open that comes in before a create 91789Sahrens * finishes. It also protects temporary opens of the dataset so that, 92789Sahrens * e.g., an open doesn't get a spurious EBUSY. 93789Sahrens */ 94789Sahrens static kmutex_t zvol_state_lock; 95789Sahrens static uint32_t zvol_minors; 96789Sahrens 976423Sgw25295 typedef struct zvol_extent { 987837SMatthew.Ahrens@Sun.COM list_node_t ze_node; 996423Sgw25295 dva_t ze_dva; /* dva associated with this extent */ 1007837SMatthew.Ahrens@Sun.COM uint64_t ze_nblks; /* number of blocks in extent */ 1016423Sgw25295 } zvol_extent_t; 1026423Sgw25295 1036423Sgw25295 /* 104789Sahrens * The in-core state of each volume. 105789Sahrens */ 106789Sahrens typedef struct zvol_state { 107789Sahrens char zv_name[MAXPATHLEN]; /* pool/dd name */ 108789Sahrens uint64_t zv_volsize; /* amount of space we advertise */ 1093063Sperrin uint64_t zv_volblocksize; /* volume block size */ 110789Sahrens minor_t zv_minor; /* minor number */ 111789Sahrens uint8_t zv_min_bs; /* minimum addressable block shift */ 1129303SEric.Taylor@Sun.COM uint8_t zv_flags; /* readonly, dumpified, etc. */ 113789Sahrens objset_t *zv_objset; /* objset handle */ 114789Sahrens uint32_t zv_open_count[OTYPCNT]; /* open counts */ 115789Sahrens uint32_t zv_total_opens; /* total open count */ 1161141Sperrin zilog_t *zv_zilog; /* ZIL handle */ 1177837SMatthew.Ahrens@Sun.COM list_t zv_extents; /* List of extents for dump */ 1183755Sperrin znode_t zv_znode; /* for range locking */ 11912123STim.Haley@Sun.COM dmu_buf_t *zv_dbuf; /* bonus handle */ 120789Sahrens } zvol_state_t; 121789Sahrens 1223063Sperrin /* 1236423Sgw25295 * zvol specific flags 1246423Sgw25295 */ 1256423Sgw25295 #define ZVOL_RDONLY 0x1 1266423Sgw25295 #define ZVOL_DUMPIFIED 0x2 1277405SEric.Taylor@Sun.COM #define ZVOL_EXCL 0x4 1289303SEric.Taylor@Sun.COM #define ZVOL_WCE 0x8 1296423Sgw25295 1306423Sgw25295 /* 1313063Sperrin * zvol maximum transfer in one DMU tx. 1323063Sperrin */ 1333063Sperrin int zvol_maxphys = DMU_MAX_ACCESS/2; 1343063Sperrin 13511022STom.Erickson@Sun.COM extern int zfs_set_prop_nvlist(const char *, zprop_source_t, 13611022STom.Erickson@Sun.COM nvlist_t *, nvlist_t **); 13710588SEric.Taylor@Sun.COM static int zvol_remove_zv(zvol_state_t *); 1383638Sbillm static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio); 1396423Sgw25295 static int zvol_dumpify(zvol_state_t *zv); 1406423Sgw25295 static int zvol_dump_fini(zvol_state_t *zv); 1416423Sgw25295 static int zvol_dump_init(zvol_state_t *zv, boolean_t resize); 1423063Sperrin 143789Sahrens static void 14410588SEric.Taylor@Sun.COM zvol_size_changed(uint64_t volsize, major_t maj, minor_t min) 145789Sahrens { 14610588SEric.Taylor@Sun.COM dev_t dev = makedevice(maj, min); 147789Sahrens 148789Sahrens VERIFY(ddi_prop_update_int64(dev, zfs_dip, 14910588SEric.Taylor@Sun.COM "Size", volsize) == DDI_SUCCESS); 150789Sahrens VERIFY(ddi_prop_update_int64(dev, zfs_dip, 15110588SEric.Taylor@Sun.COM "Nblocks", lbtodb(volsize)) == DDI_SUCCESS); 1526423Sgw25295 1536423Sgw25295 /* Notify specfs to invalidate the cached size */ 1546423Sgw25295 spec_size_invalidate(dev, VBLK); 1556423Sgw25295 spec_size_invalidate(dev, VCHR); 156789Sahrens } 157789Sahrens 158789Sahrens int 1592676Seschrock zvol_check_volsize(uint64_t volsize, uint64_t blocksize) 160789Sahrens { 1612676Seschrock if (volsize == 0) 162789Sahrens return (EINVAL); 163789Sahrens 1642676Seschrock if (volsize % blocksize != 0) 1651133Seschrock return (EINVAL); 1661133Seschrock 167789Sahrens #ifdef _ILP32 1682676Seschrock if (volsize - 1 > SPEC_MAXOFFSET_T) 169789Sahrens return (EOVERFLOW); 170789Sahrens #endif 171789Sahrens return (0); 172789Sahrens } 173789Sahrens 174789Sahrens int 1752676Seschrock zvol_check_volblocksize(uint64_t volblocksize) 176789Sahrens { 1772676Seschrock if (volblocksize < SPA_MINBLOCKSIZE || 1782676Seschrock volblocksize > SPA_MAXBLOCKSIZE || 1792676Seschrock !ISP2(volblocksize)) 180789Sahrens return (EDOM); 181789Sahrens 182789Sahrens return (0); 183789Sahrens } 184789Sahrens 185789Sahrens int 1862885Sahrens zvol_get_stats(objset_t *os, nvlist_t *nv) 187789Sahrens { 188789Sahrens int error; 189789Sahrens dmu_object_info_t doi; 1902885Sahrens uint64_t val; 191789Sahrens 1922885Sahrens error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val); 193789Sahrens if (error) 194789Sahrens return (error); 195789Sahrens 1962885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val); 1972885Sahrens 198789Sahrens error = dmu_object_info(os, ZVOL_OBJ, &doi); 199789Sahrens 2002885Sahrens if (error == 0) { 2012885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE, 2022885Sahrens doi.doi_data_block_size); 2032885Sahrens } 204789Sahrens 205789Sahrens return (error); 206789Sahrens } 207789Sahrens 208789Sahrens /* 209789Sahrens * Find a free minor number. 210789Sahrens */ 211789Sahrens static minor_t 212789Sahrens zvol_minor_alloc(void) 213789Sahrens { 214789Sahrens minor_t minor; 215789Sahrens 216789Sahrens ASSERT(MUTEX_HELD(&zvol_state_lock)); 217789Sahrens 218789Sahrens for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) 219789Sahrens if (ddi_get_soft_state(zvol_state, minor) == NULL) 220789Sahrens return (minor); 221789Sahrens 222789Sahrens return (0); 223789Sahrens } 224789Sahrens 225789Sahrens static zvol_state_t * 2262676Seschrock zvol_minor_lookup(const char *name) 227789Sahrens { 228789Sahrens minor_t minor; 229789Sahrens zvol_state_t *zv; 230789Sahrens 231789Sahrens ASSERT(MUTEX_HELD(&zvol_state_lock)); 232789Sahrens 233789Sahrens for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) { 234789Sahrens zv = ddi_get_soft_state(zvol_state, minor); 235789Sahrens if (zv == NULL) 236789Sahrens continue; 237789Sahrens if (strcmp(zv->zv_name, name) == 0) 23812095SChris.Kirby@sun.com return (zv); 239789Sahrens } 240789Sahrens 24112095SChris.Kirby@sun.com return (NULL); 242789Sahrens } 243789Sahrens 2446423Sgw25295 /* extent mapping arg */ 2456423Sgw25295 struct maparg { 2467837SMatthew.Ahrens@Sun.COM zvol_state_t *ma_zv; 2477837SMatthew.Ahrens@Sun.COM uint64_t ma_blks; 2486423Sgw25295 }; 2496423Sgw25295 2506423Sgw25295 /*ARGSUSED*/ 2516423Sgw25295 static int 25210922SJeff.Bonwick@Sun.COM zvol_map_block(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 25310922SJeff.Bonwick@Sun.COM const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg) 2546423Sgw25295 { 2557837SMatthew.Ahrens@Sun.COM struct maparg *ma = arg; 2567837SMatthew.Ahrens@Sun.COM zvol_extent_t *ze; 2577837SMatthew.Ahrens@Sun.COM int bs = ma->ma_zv->zv_volblocksize; 2586423Sgw25295 2597837SMatthew.Ahrens@Sun.COM if (bp == NULL || zb->zb_object != ZVOL_OBJ || zb->zb_level != 0) 2606423Sgw25295 return (0); 2616423Sgw25295 2627837SMatthew.Ahrens@Sun.COM VERIFY3U(ma->ma_blks, ==, zb->zb_blkid); 2637837SMatthew.Ahrens@Sun.COM ma->ma_blks++; 2647837SMatthew.Ahrens@Sun.COM 2656423Sgw25295 /* Abort immediately if we have encountered gang blocks */ 2667837SMatthew.Ahrens@Sun.COM if (BP_IS_GANG(bp)) 2677837SMatthew.Ahrens@Sun.COM return (EFRAGS); 2686423Sgw25295 2697837SMatthew.Ahrens@Sun.COM /* 2707837SMatthew.Ahrens@Sun.COM * See if the block is at the end of the previous extent. 2717837SMatthew.Ahrens@Sun.COM */ 2727837SMatthew.Ahrens@Sun.COM ze = list_tail(&ma->ma_zv->zv_extents); 2737837SMatthew.Ahrens@Sun.COM if (ze && 2747837SMatthew.Ahrens@Sun.COM DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) && 2757837SMatthew.Ahrens@Sun.COM DVA_GET_OFFSET(BP_IDENTITY(bp)) == 2767837SMatthew.Ahrens@Sun.COM DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) { 2777837SMatthew.Ahrens@Sun.COM ze->ze_nblks++; 2786423Sgw25295 return (0); 2796423Sgw25295 } 2806423Sgw25295 2817837SMatthew.Ahrens@Sun.COM dprintf_bp(bp, "%s", "next blkptr:"); 2827837SMatthew.Ahrens@Sun.COM 2837837SMatthew.Ahrens@Sun.COM /* start a new extent */ 2847837SMatthew.Ahrens@Sun.COM ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP); 2857837SMatthew.Ahrens@Sun.COM ze->ze_dva = bp->blk_dva[0]; /* structure assignment */ 2867837SMatthew.Ahrens@Sun.COM ze->ze_nblks = 1; 2877837SMatthew.Ahrens@Sun.COM list_insert_tail(&ma->ma_zv->zv_extents, ze); 2887837SMatthew.Ahrens@Sun.COM return (0); 2897837SMatthew.Ahrens@Sun.COM } 2907837SMatthew.Ahrens@Sun.COM 2917837SMatthew.Ahrens@Sun.COM static void 2927837SMatthew.Ahrens@Sun.COM zvol_free_extents(zvol_state_t *zv) 2937837SMatthew.Ahrens@Sun.COM { 2947837SMatthew.Ahrens@Sun.COM zvol_extent_t *ze; 2957837SMatthew.Ahrens@Sun.COM 2967837SMatthew.Ahrens@Sun.COM while (ze = list_head(&zv->zv_extents)) { 2977837SMatthew.Ahrens@Sun.COM list_remove(&zv->zv_extents, ze); 2987837SMatthew.Ahrens@Sun.COM kmem_free(ze, sizeof (zvol_extent_t)); 2997837SMatthew.Ahrens@Sun.COM } 3007837SMatthew.Ahrens@Sun.COM } 3017837SMatthew.Ahrens@Sun.COM 3027837SMatthew.Ahrens@Sun.COM static int 3037837SMatthew.Ahrens@Sun.COM zvol_get_lbas(zvol_state_t *zv) 3047837SMatthew.Ahrens@Sun.COM { 30511689SEric.Taylor@Sun.COM objset_t *os = zv->zv_objset; 3067837SMatthew.Ahrens@Sun.COM struct maparg ma; 3077837SMatthew.Ahrens@Sun.COM int err; 3087837SMatthew.Ahrens@Sun.COM 3097837SMatthew.Ahrens@Sun.COM ma.ma_zv = zv; 3107837SMatthew.Ahrens@Sun.COM ma.ma_blks = 0; 3117837SMatthew.Ahrens@Sun.COM zvol_free_extents(zv); 3127837SMatthew.Ahrens@Sun.COM 31311689SEric.Taylor@Sun.COM /* commit any in-flight changes before traversing the dataset */ 31411689SEric.Taylor@Sun.COM txg_wait_synced(dmu_objset_pool(os), 0); 31511689SEric.Taylor@Sun.COM err = traverse_dataset(dmu_objset_ds(os), 0, 3167837SMatthew.Ahrens@Sun.COM TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma); 3177837SMatthew.Ahrens@Sun.COM if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) { 3187837SMatthew.Ahrens@Sun.COM zvol_free_extents(zv); 3197837SMatthew.Ahrens@Sun.COM return (err ? err : EIO); 3206423Sgw25295 } 3216423Sgw25295 3226423Sgw25295 return (0); 3236423Sgw25295 } 3246423Sgw25295 3254543Smarks /* ARGSUSED */ 326789Sahrens void 3274543Smarks zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 328789Sahrens { 3295331Samw zfs_creat_t *zct = arg; 3305331Samw nvlist_t *nvprops = zct->zct_props; 331789Sahrens int error; 3322676Seschrock uint64_t volblocksize, volsize; 333789Sahrens 3344543Smarks VERIFY(nvlist_lookup_uint64(nvprops, 3352676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0); 3364543Smarks if (nvlist_lookup_uint64(nvprops, 3372676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0) 3382676Seschrock volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 3392676Seschrock 3402676Seschrock /* 3416423Sgw25295 * These properties must be removed from the list so the generic 3422676Seschrock * property setting step won't apply to them. 3432676Seschrock */ 3444543Smarks VERIFY(nvlist_remove_all(nvprops, 3452676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0); 3464543Smarks (void) nvlist_remove_all(nvprops, 3472676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE)); 3482676Seschrock 3492676Seschrock error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize, 350789Sahrens DMU_OT_NONE, 0, tx); 351789Sahrens ASSERT(error == 0); 352789Sahrens 353789Sahrens error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP, 354789Sahrens DMU_OT_NONE, 0, tx); 355789Sahrens ASSERT(error == 0); 356789Sahrens 3572676Seschrock error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx); 358789Sahrens ASSERT(error == 0); 359789Sahrens } 360789Sahrens 361789Sahrens /* 3621141Sperrin * Replay a TX_WRITE ZIL transaction that didn't get committed 3631141Sperrin * after a system failure 3641141Sperrin */ 3651141Sperrin static int 3661141Sperrin zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap) 3671141Sperrin { 3681141Sperrin objset_t *os = zv->zv_objset; 3691141Sperrin char *data = (char *)(lr + 1); /* data follows lr_write_t */ 37010922SJeff.Bonwick@Sun.COM uint64_t offset, length; 3711141Sperrin dmu_tx_t *tx; 3721141Sperrin int error; 3731141Sperrin 3741141Sperrin if (byteswap) 3751141Sperrin byteswap_uint64_array(lr, sizeof (*lr)); 3761141Sperrin 37710922SJeff.Bonwick@Sun.COM offset = lr->lr_offset; 37810922SJeff.Bonwick@Sun.COM length = lr->lr_length; 37910922SJeff.Bonwick@Sun.COM 38010922SJeff.Bonwick@Sun.COM /* If it's a dmu_sync() block, write the whole block */ 38110922SJeff.Bonwick@Sun.COM if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) { 38210922SJeff.Bonwick@Sun.COM uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr); 38310922SJeff.Bonwick@Sun.COM if (length < blocksize) { 38410922SJeff.Bonwick@Sun.COM offset -= offset % blocksize; 38510922SJeff.Bonwick@Sun.COM length = blocksize; 38610922SJeff.Bonwick@Sun.COM } 38710922SJeff.Bonwick@Sun.COM } 38810800SNeil.Perrin@Sun.COM 3891141Sperrin tx = dmu_tx_create(os); 39010922SJeff.Bonwick@Sun.COM dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length); 3918227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_WAIT); 3921141Sperrin if (error) { 3931141Sperrin dmu_tx_abort(tx); 3941141Sperrin } else { 39510922SJeff.Bonwick@Sun.COM dmu_write(os, ZVOL_OBJ, offset, length, data, tx); 3961141Sperrin dmu_tx_commit(tx); 3971141Sperrin } 3981141Sperrin 3991141Sperrin return (error); 4001141Sperrin } 4011141Sperrin 4021141Sperrin /* ARGSUSED */ 4031141Sperrin static int 4041141Sperrin zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap) 4051141Sperrin { 4061141Sperrin return (ENOTSUP); 4071141Sperrin } 4081141Sperrin 4091141Sperrin /* 4101141Sperrin * Callback vectors for replaying records. 4111141Sperrin * Only TX_WRITE is needed for zvol. 4121141Sperrin */ 4131141Sperrin zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = { 4141141Sperrin zvol_replay_err, /* 0 no such transaction type */ 4151141Sperrin zvol_replay_err, /* TX_CREATE */ 4161141Sperrin zvol_replay_err, /* TX_MKDIR */ 4171141Sperrin zvol_replay_err, /* TX_MKXATTR */ 4181141Sperrin zvol_replay_err, /* TX_SYMLINK */ 4191141Sperrin zvol_replay_err, /* TX_REMOVE */ 4201141Sperrin zvol_replay_err, /* TX_RMDIR */ 4211141Sperrin zvol_replay_err, /* TX_LINK */ 4221141Sperrin zvol_replay_err, /* TX_RENAME */ 4231141Sperrin zvol_replay_write, /* TX_WRITE */ 4241141Sperrin zvol_replay_err, /* TX_TRUNCATE */ 4251141Sperrin zvol_replay_err, /* TX_SETATTR */ 4261141Sperrin zvol_replay_err, /* TX_ACL */ 42710800SNeil.Perrin@Sun.COM zvol_replay_err, /* TX_CREATE_ACL */ 42810800SNeil.Perrin@Sun.COM zvol_replay_err, /* TX_CREATE_ATTR */ 42910800SNeil.Perrin@Sun.COM zvol_replay_err, /* TX_CREATE_ACL_ATTR */ 43010800SNeil.Perrin@Sun.COM zvol_replay_err, /* TX_MKDIR_ACL */ 43110800SNeil.Perrin@Sun.COM zvol_replay_err, /* TX_MKDIR_ATTR */ 43210800SNeil.Perrin@Sun.COM zvol_replay_err, /* TX_MKDIR_ACL_ATTR */ 43310800SNeil.Perrin@Sun.COM zvol_replay_err, /* TX_WRITE2 */ 4341141Sperrin }; 4351141Sperrin 43610588SEric.Taylor@Sun.COM int 43710588SEric.Taylor@Sun.COM zvol_name2minor(const char *name, minor_t *minor) 43810588SEric.Taylor@Sun.COM { 43910588SEric.Taylor@Sun.COM zvol_state_t *zv; 44010588SEric.Taylor@Sun.COM 44110588SEric.Taylor@Sun.COM mutex_enter(&zvol_state_lock); 44210588SEric.Taylor@Sun.COM zv = zvol_minor_lookup(name); 44310588SEric.Taylor@Sun.COM if (minor && zv) 44410588SEric.Taylor@Sun.COM *minor = zv->zv_minor; 44510588SEric.Taylor@Sun.COM mutex_exit(&zvol_state_lock); 44610588SEric.Taylor@Sun.COM return (zv ? 0 : -1); 44710588SEric.Taylor@Sun.COM } 44810588SEric.Taylor@Sun.COM 4491141Sperrin /* 4506423Sgw25295 * Create a minor node (plus a whole lot more) for the specified volume. 451789Sahrens */ 452789Sahrens int 45310588SEric.Taylor@Sun.COM zvol_create_minor(const char *name) 454789Sahrens { 455789Sahrens zvol_state_t *zv; 456789Sahrens objset_t *os; 4573063Sperrin dmu_object_info_t doi; 458789Sahrens minor_t minor = 0; 459789Sahrens char chrbuf[30], blkbuf[30]; 460789Sahrens int error; 461789Sahrens 462789Sahrens mutex_enter(&zvol_state_lock); 463789Sahrens 46411422SMark.Musante@Sun.COM if (zvol_minor_lookup(name) != NULL) { 465789Sahrens mutex_exit(&zvol_state_lock); 466789Sahrens return (EEXIST); 467789Sahrens } 468789Sahrens 46910298SMatthew.Ahrens@Sun.COM /* lie and say we're read-only */ 47010298SMatthew.Ahrens@Sun.COM error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, zvol_tag, &os); 471789Sahrens 472789Sahrens if (error) { 473789Sahrens mutex_exit(&zvol_state_lock); 474789Sahrens return (error); 475789Sahrens } 476789Sahrens 47710588SEric.Taylor@Sun.COM if ((minor = zvol_minor_alloc()) == 0) { 47810298SMatthew.Ahrens@Sun.COM dmu_objset_disown(os, zvol_tag); 479789Sahrens mutex_exit(&zvol_state_lock); 480789Sahrens return (ENXIO); 481789Sahrens } 482789Sahrens 483789Sahrens if (ddi_soft_state_zalloc(zvol_state, minor) != DDI_SUCCESS) { 48410298SMatthew.Ahrens@Sun.COM dmu_objset_disown(os, zvol_tag); 485789Sahrens mutex_exit(&zvol_state_lock); 486789Sahrens return (EAGAIN); 487789Sahrens } 4882676Seschrock (void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME, 4892676Seschrock (char *)name); 490789Sahrens 49110588SEric.Taylor@Sun.COM (void) snprintf(chrbuf, sizeof (chrbuf), "%u,raw", minor); 492789Sahrens 493789Sahrens if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR, 494789Sahrens minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 495789Sahrens ddi_soft_state_free(zvol_state, minor); 49610298SMatthew.Ahrens@Sun.COM dmu_objset_disown(os, zvol_tag); 497789Sahrens mutex_exit(&zvol_state_lock); 498789Sahrens return (EAGAIN); 499789Sahrens } 500789Sahrens 50110588SEric.Taylor@Sun.COM (void) snprintf(blkbuf, sizeof (blkbuf), "%u", minor); 502789Sahrens 503789Sahrens if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK, 504789Sahrens minor, DDI_PSEUDO, 0) == DDI_FAILURE) { 505789Sahrens ddi_remove_minor_node(zfs_dip, chrbuf); 506789Sahrens ddi_soft_state_free(zvol_state, minor); 50710298SMatthew.Ahrens@Sun.COM dmu_objset_disown(os, zvol_tag); 508789Sahrens mutex_exit(&zvol_state_lock); 509789Sahrens return (EAGAIN); 510789Sahrens } 511789Sahrens 512789Sahrens zv = ddi_get_soft_state(zvol_state, minor); 513789Sahrens 51410588SEric.Taylor@Sun.COM (void) strlcpy(zv->zv_name, name, MAXPATHLEN); 515789Sahrens zv->zv_min_bs = DEV_BSHIFT; 516789Sahrens zv->zv_minor = minor; 517789Sahrens zv->zv_objset = os; 51810588SEric.Taylor@Sun.COM if (dmu_objset_is_snapshot(os)) 51910588SEric.Taylor@Sun.COM zv->zv_flags |= ZVOL_RDONLY; 5203755Sperrin mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL); 5213755Sperrin avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare, 5223755Sperrin sizeof (rl_t), offsetof(rl_t, r_node)); 5237837SMatthew.Ahrens@Sun.COM list_create(&zv->zv_extents, sizeof (zvol_extent_t), 5247837SMatthew.Ahrens@Sun.COM offsetof(zvol_extent_t, ze_node)); 5253063Sperrin /* get and cache the blocksize */ 5263063Sperrin error = dmu_object_info(os, ZVOL_OBJ, &doi); 5273063Sperrin ASSERT(error == 0); 5283063Sperrin zv->zv_volblocksize = doi.doi_data_block_size; 5291141Sperrin 530*12294SMark.Musante@Sun.COM if (zil_replay_disable) 531*12294SMark.Musante@Sun.COM zil_destroy(dmu_objset_zil(os), B_FALSE); 532*12294SMark.Musante@Sun.COM else 533*12294SMark.Musante@Sun.COM zil_replay(os, zv, zvol_replay_vector); 53410588SEric.Taylor@Sun.COM dmu_objset_disown(os, zvol_tag); 53510588SEric.Taylor@Sun.COM zv->zv_objset = NULL; 536789Sahrens 537789Sahrens zvol_minors++; 538789Sahrens 539789Sahrens mutex_exit(&zvol_state_lock); 540789Sahrens 541789Sahrens return (0); 542789Sahrens } 543789Sahrens 544789Sahrens /* 545789Sahrens * Remove minor node for the specified volume. 546789Sahrens */ 54710588SEric.Taylor@Sun.COM static int 54810588SEric.Taylor@Sun.COM zvol_remove_zv(zvol_state_t *zv) 549789Sahrens { 55010588SEric.Taylor@Sun.COM char nmbuf[20]; 551789Sahrens 55210588SEric.Taylor@Sun.COM ASSERT(MUTEX_HELD(&zvol_state_lock)); 55310588SEric.Taylor@Sun.COM if (zv->zv_total_opens != 0) 554789Sahrens return (EBUSY); 555789Sahrens 55610588SEric.Taylor@Sun.COM (void) snprintf(nmbuf, sizeof (nmbuf), "%u,raw", zv->zv_minor); 55710588SEric.Taylor@Sun.COM ddi_remove_minor_node(zfs_dip, nmbuf); 558789Sahrens 55910588SEric.Taylor@Sun.COM (void) snprintf(nmbuf, sizeof (nmbuf), "%u", zv->zv_minor); 56010588SEric.Taylor@Sun.COM ddi_remove_minor_node(zfs_dip, nmbuf); 561789Sahrens 5623755Sperrin avl_destroy(&zv->zv_znode.z_range_avl); 5633755Sperrin mutex_destroy(&zv->zv_znode.z_range_lock); 564789Sahrens 565789Sahrens ddi_soft_state_free(zvol_state, zv->zv_minor); 566789Sahrens 567789Sahrens zvol_minors--; 56810588SEric.Taylor@Sun.COM return (0); 56910588SEric.Taylor@Sun.COM } 570789Sahrens 57110588SEric.Taylor@Sun.COM int 57210588SEric.Taylor@Sun.COM zvol_remove_minor(const char *name) 57310588SEric.Taylor@Sun.COM { 57410588SEric.Taylor@Sun.COM zvol_state_t *zv; 57510588SEric.Taylor@Sun.COM int rc; 57610588SEric.Taylor@Sun.COM 57710588SEric.Taylor@Sun.COM mutex_enter(&zvol_state_lock); 57810588SEric.Taylor@Sun.COM if ((zv = zvol_minor_lookup(name)) == NULL) { 57910588SEric.Taylor@Sun.COM mutex_exit(&zvol_state_lock); 58010588SEric.Taylor@Sun.COM return (ENXIO); 58110588SEric.Taylor@Sun.COM } 58210588SEric.Taylor@Sun.COM rc = zvol_remove_zv(zv); 583789Sahrens mutex_exit(&zvol_state_lock); 58410588SEric.Taylor@Sun.COM return (rc); 58510588SEric.Taylor@Sun.COM } 586789Sahrens 58710588SEric.Taylor@Sun.COM int 58810588SEric.Taylor@Sun.COM zvol_first_open(zvol_state_t *zv) 58910588SEric.Taylor@Sun.COM { 59010588SEric.Taylor@Sun.COM objset_t *os; 59110588SEric.Taylor@Sun.COM uint64_t volsize; 59210588SEric.Taylor@Sun.COM int error; 59310588SEric.Taylor@Sun.COM uint64_t readonly; 59410588SEric.Taylor@Sun.COM 59510588SEric.Taylor@Sun.COM /* lie and say we're read-only */ 59610588SEric.Taylor@Sun.COM error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, B_TRUE, 59710588SEric.Taylor@Sun.COM zvol_tag, &os); 59810588SEric.Taylor@Sun.COM if (error) 59910588SEric.Taylor@Sun.COM return (error); 60010588SEric.Taylor@Sun.COM 60110588SEric.Taylor@Sun.COM error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize); 60210588SEric.Taylor@Sun.COM if (error) { 60310588SEric.Taylor@Sun.COM ASSERT(error == 0); 60410588SEric.Taylor@Sun.COM dmu_objset_disown(os, zvol_tag); 60510588SEric.Taylor@Sun.COM return (error); 60610588SEric.Taylor@Sun.COM } 60710588SEric.Taylor@Sun.COM zv->zv_objset = os; 60812123STim.Haley@Sun.COM error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf); 60912123STim.Haley@Sun.COM if (error) { 61012123STim.Haley@Sun.COM dmu_objset_disown(os, zvol_tag); 61112123STim.Haley@Sun.COM return (error); 61212123STim.Haley@Sun.COM } 61310588SEric.Taylor@Sun.COM zv->zv_volsize = volsize; 61410588SEric.Taylor@Sun.COM zv->zv_zilog = zil_open(os, zvol_get_data); 61510588SEric.Taylor@Sun.COM zvol_size_changed(zv->zv_volsize, ddi_driver_major(zfs_dip), 61610588SEric.Taylor@Sun.COM zv->zv_minor); 61710588SEric.Taylor@Sun.COM 61810588SEric.Taylor@Sun.COM VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &readonly, 61910588SEric.Taylor@Sun.COM NULL) == 0); 62010588SEric.Taylor@Sun.COM if (readonly || dmu_objset_is_snapshot(os)) 62110588SEric.Taylor@Sun.COM zv->zv_flags |= ZVOL_RDONLY; 62210588SEric.Taylor@Sun.COM else 62310588SEric.Taylor@Sun.COM zv->zv_flags &= ~ZVOL_RDONLY; 62410588SEric.Taylor@Sun.COM return (error); 62510588SEric.Taylor@Sun.COM } 62610588SEric.Taylor@Sun.COM 62710588SEric.Taylor@Sun.COM void 62810588SEric.Taylor@Sun.COM zvol_last_close(zvol_state_t *zv) 62910588SEric.Taylor@Sun.COM { 63010588SEric.Taylor@Sun.COM zil_close(zv->zv_zilog); 63110588SEric.Taylor@Sun.COM zv->zv_zilog = NULL; 63212123STim.Haley@Sun.COM dmu_buf_rele(zv->zv_dbuf, zvol_tag); 63312123STim.Haley@Sun.COM zv->zv_dbuf = NULL; 63410588SEric.Taylor@Sun.COM dmu_objset_disown(zv->zv_objset, zvol_tag); 63510588SEric.Taylor@Sun.COM zv->zv_objset = NULL; 636789Sahrens } 637789Sahrens 6386423Sgw25295 int 6396423Sgw25295 zvol_prealloc(zvol_state_t *zv) 6406423Sgw25295 { 6416423Sgw25295 objset_t *os = zv->zv_objset; 6426423Sgw25295 dmu_tx_t *tx; 6436423Sgw25295 uint64_t refd, avail, usedobjs, availobjs; 6446423Sgw25295 uint64_t resid = zv->zv_volsize; 6456423Sgw25295 uint64_t off = 0; 6466423Sgw25295 6476423Sgw25295 /* Check the space usage before attempting to allocate the space */ 6486423Sgw25295 dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs); 6496423Sgw25295 if (avail < zv->zv_volsize) 6506423Sgw25295 return (ENOSPC); 6516423Sgw25295 6526423Sgw25295 /* Free old extents if they exist */ 6536423Sgw25295 zvol_free_extents(zv); 6546423Sgw25295 6556423Sgw25295 while (resid != 0) { 6566423Sgw25295 int error; 6576423Sgw25295 uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE); 6586423Sgw25295 6596423Sgw25295 tx = dmu_tx_create(os); 6606423Sgw25295 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 6616423Sgw25295 error = dmu_tx_assign(tx, TXG_WAIT); 6626423Sgw25295 if (error) { 6636423Sgw25295 dmu_tx_abort(tx); 6646992Smaybee (void) dmu_free_long_range(os, ZVOL_OBJ, 0, off); 6656423Sgw25295 return (error); 6666423Sgw25295 } 6677872STim.Haley@Sun.COM dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx); 6686423Sgw25295 dmu_tx_commit(tx); 6696423Sgw25295 off += bytes; 6706423Sgw25295 resid -= bytes; 6716423Sgw25295 } 6726423Sgw25295 txg_wait_synced(dmu_objset_pool(os), 0); 6736423Sgw25295 6746423Sgw25295 return (0); 6756423Sgw25295 } 6766423Sgw25295 6776423Sgw25295 int 67810588SEric.Taylor@Sun.COM zvol_update_volsize(objset_t *os, uint64_t volsize) 6796423Sgw25295 { 6806423Sgw25295 dmu_tx_t *tx; 6816423Sgw25295 int error; 6826423Sgw25295 6836423Sgw25295 ASSERT(MUTEX_HELD(&zvol_state_lock)); 6846423Sgw25295 68510588SEric.Taylor@Sun.COM tx = dmu_tx_create(os); 6866423Sgw25295 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 6876423Sgw25295 error = dmu_tx_assign(tx, TXG_WAIT); 6886423Sgw25295 if (error) { 6896423Sgw25295 dmu_tx_abort(tx); 6906423Sgw25295 return (error); 6916423Sgw25295 } 6926423Sgw25295 69310588SEric.Taylor@Sun.COM error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, 6946423Sgw25295 &volsize, tx); 6956423Sgw25295 dmu_tx_commit(tx); 6966423Sgw25295 6976423Sgw25295 if (error == 0) 69810588SEric.Taylor@Sun.COM error = dmu_free_long_range(os, 6996992Smaybee ZVOL_OBJ, volsize, DMU_OBJECT_END); 70010588SEric.Taylor@Sun.COM return (error); 70110588SEric.Taylor@Sun.COM } 70210588SEric.Taylor@Sun.COM 70310588SEric.Taylor@Sun.COM void 70410588SEric.Taylor@Sun.COM zvol_remove_minors(const char *name) 70510588SEric.Taylor@Sun.COM { 70610588SEric.Taylor@Sun.COM zvol_state_t *zv; 70710588SEric.Taylor@Sun.COM char *namebuf; 70810588SEric.Taylor@Sun.COM minor_t minor; 7096423Sgw25295 71010588SEric.Taylor@Sun.COM namebuf = kmem_zalloc(strlen(name) + 2, KM_SLEEP); 71110588SEric.Taylor@Sun.COM (void) strncpy(namebuf, name, strlen(name)); 71210588SEric.Taylor@Sun.COM (void) strcat(namebuf, "/"); 71310588SEric.Taylor@Sun.COM mutex_enter(&zvol_state_lock); 71410588SEric.Taylor@Sun.COM for (minor = 1; minor <= ZVOL_MAX_MINOR; minor++) { 71510588SEric.Taylor@Sun.COM 71610588SEric.Taylor@Sun.COM zv = ddi_get_soft_state(zvol_state, minor); 71710588SEric.Taylor@Sun.COM if (zv == NULL) 71810588SEric.Taylor@Sun.COM continue; 71910588SEric.Taylor@Sun.COM if (strncmp(namebuf, zv->zv_name, strlen(namebuf)) == 0) 72010588SEric.Taylor@Sun.COM (void) zvol_remove_zv(zv); 7216423Sgw25295 } 72210588SEric.Taylor@Sun.COM kmem_free(namebuf, strlen(name) + 2); 72310588SEric.Taylor@Sun.COM 72410588SEric.Taylor@Sun.COM mutex_exit(&zvol_state_lock); 7256423Sgw25295 } 7266423Sgw25295 727789Sahrens int 7284787Sahrens zvol_set_volsize(const char *name, major_t maj, uint64_t volsize) 729789Sahrens { 73010588SEric.Taylor@Sun.COM zvol_state_t *zv = NULL; 73110588SEric.Taylor@Sun.COM objset_t *os; 732789Sahrens int error; 7331133Seschrock dmu_object_info_t doi; 7346423Sgw25295 uint64_t old_volsize = 0ULL; 73510588SEric.Taylor@Sun.COM uint64_t readonly; 736789Sahrens 737789Sahrens mutex_enter(&zvol_state_lock); 73810588SEric.Taylor@Sun.COM zv = zvol_minor_lookup(name); 73910588SEric.Taylor@Sun.COM if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) { 74010588SEric.Taylor@Sun.COM mutex_exit(&zvol_state_lock); 74110588SEric.Taylor@Sun.COM return (error); 74210588SEric.Taylor@Sun.COM } 743789Sahrens 74410588SEric.Taylor@Sun.COM if ((error = dmu_object_info(os, ZVOL_OBJ, &doi)) != 0 || 7452676Seschrock (error = zvol_check_volsize(volsize, 7467265Sahrens doi.doi_data_block_size)) != 0) 7477265Sahrens goto out; 7481133Seschrock 74910588SEric.Taylor@Sun.COM VERIFY(dsl_prop_get_integer(name, "readonly", &readonly, 75010588SEric.Taylor@Sun.COM NULL) == 0); 75110588SEric.Taylor@Sun.COM if (readonly) { 7527265Sahrens error = EROFS; 7537265Sahrens goto out; 754789Sahrens } 755789Sahrens 75610588SEric.Taylor@Sun.COM error = zvol_update_volsize(os, volsize); 7576423Sgw25295 /* 7586423Sgw25295 * Reinitialize the dump area to the new size. If we 75910588SEric.Taylor@Sun.COM * failed to resize the dump area then restore it back to 76010588SEric.Taylor@Sun.COM * its original size. 7616423Sgw25295 */ 76210588SEric.Taylor@Sun.COM if (zv && error == 0) { 76310588SEric.Taylor@Sun.COM if (zv->zv_flags & ZVOL_DUMPIFIED) { 76410588SEric.Taylor@Sun.COM old_volsize = zv->zv_volsize; 76510588SEric.Taylor@Sun.COM zv->zv_volsize = volsize; 76610588SEric.Taylor@Sun.COM if ((error = zvol_dumpify(zv)) != 0 || 76710588SEric.Taylor@Sun.COM (error = dumpvp_resize()) != 0) { 76810588SEric.Taylor@Sun.COM (void) zvol_update_volsize(os, old_volsize); 76910588SEric.Taylor@Sun.COM zv->zv_volsize = old_volsize; 77010588SEric.Taylor@Sun.COM error = zvol_dumpify(zv); 77110588SEric.Taylor@Sun.COM } 77210588SEric.Taylor@Sun.COM } 77310588SEric.Taylor@Sun.COM if (error == 0) { 77410588SEric.Taylor@Sun.COM zv->zv_volsize = volsize; 77510588SEric.Taylor@Sun.COM zvol_size_changed(volsize, maj, zv->zv_minor); 7766423Sgw25295 } 777789Sahrens } 778789Sahrens 7799816SGeorge.Wilson@Sun.COM /* 7809816SGeorge.Wilson@Sun.COM * Generate a LUN expansion event. 7819816SGeorge.Wilson@Sun.COM */ 78210588SEric.Taylor@Sun.COM if (zv && error == 0) { 7839816SGeorge.Wilson@Sun.COM sysevent_id_t eid; 7849816SGeorge.Wilson@Sun.COM nvlist_t *attr; 7859816SGeorge.Wilson@Sun.COM char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 7869816SGeorge.Wilson@Sun.COM 78710588SEric.Taylor@Sun.COM (void) snprintf(physpath, MAXPATHLEN, "%s%u", ZVOL_PSEUDO_DEV, 7889816SGeorge.Wilson@Sun.COM zv->zv_minor); 7899816SGeorge.Wilson@Sun.COM 7909816SGeorge.Wilson@Sun.COM VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0); 7919816SGeorge.Wilson@Sun.COM VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0); 7929816SGeorge.Wilson@Sun.COM 7939816SGeorge.Wilson@Sun.COM (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS, 7949816SGeorge.Wilson@Sun.COM ESC_DEV_DLE, attr, &eid, DDI_SLEEP); 7959816SGeorge.Wilson@Sun.COM 7969816SGeorge.Wilson@Sun.COM nvlist_free(attr); 7979816SGeorge.Wilson@Sun.COM kmem_free(physpath, MAXPATHLEN); 7989816SGeorge.Wilson@Sun.COM } 7999816SGeorge.Wilson@Sun.COM 8007265Sahrens out: 80110588SEric.Taylor@Sun.COM dmu_objset_rele(os, FTAG); 8027265Sahrens 803789Sahrens mutex_exit(&zvol_state_lock); 804789Sahrens 805789Sahrens return (error); 806789Sahrens } 807789Sahrens 808789Sahrens /*ARGSUSED*/ 809789Sahrens int 810789Sahrens zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr) 811789Sahrens { 812789Sahrens minor_t minor = getminor(*devp); 813789Sahrens zvol_state_t *zv; 81410588SEric.Taylor@Sun.COM int err = 0; 815789Sahrens 816789Sahrens if (minor == 0) /* This is the control device */ 817789Sahrens return (0); 818789Sahrens 819789Sahrens mutex_enter(&zvol_state_lock); 820789Sahrens 821789Sahrens zv = ddi_get_soft_state(zvol_state, minor); 822789Sahrens if (zv == NULL) { 823789Sahrens mutex_exit(&zvol_state_lock); 824789Sahrens return (ENXIO); 825789Sahrens } 826789Sahrens 82710588SEric.Taylor@Sun.COM if (zv->zv_total_opens == 0) 82810588SEric.Taylor@Sun.COM err = zvol_first_open(zv); 82910588SEric.Taylor@Sun.COM if (err) { 830789Sahrens mutex_exit(&zvol_state_lock); 83110588SEric.Taylor@Sun.COM return (err); 83210588SEric.Taylor@Sun.COM } 83310588SEric.Taylor@Sun.COM if ((flag & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) { 83410588SEric.Taylor@Sun.COM err = EROFS; 83510588SEric.Taylor@Sun.COM goto out; 836789Sahrens } 8377405SEric.Taylor@Sun.COM if (zv->zv_flags & ZVOL_EXCL) { 83810588SEric.Taylor@Sun.COM err = EBUSY; 83910588SEric.Taylor@Sun.COM goto out; 8407405SEric.Taylor@Sun.COM } 8417405SEric.Taylor@Sun.COM if (flag & FEXCL) { 8427405SEric.Taylor@Sun.COM if (zv->zv_total_opens != 0) { 84310588SEric.Taylor@Sun.COM err = EBUSY; 84410588SEric.Taylor@Sun.COM goto out; 8457405SEric.Taylor@Sun.COM } 8467405SEric.Taylor@Sun.COM zv->zv_flags |= ZVOL_EXCL; 8477405SEric.Taylor@Sun.COM } 848789Sahrens 849789Sahrens if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) { 850789Sahrens zv->zv_open_count[otyp]++; 851789Sahrens zv->zv_total_opens++; 852789Sahrens } 853789Sahrens mutex_exit(&zvol_state_lock); 854789Sahrens 85510588SEric.Taylor@Sun.COM return (err); 85610588SEric.Taylor@Sun.COM out: 85710588SEric.Taylor@Sun.COM if (zv->zv_total_opens == 0) 85810588SEric.Taylor@Sun.COM zvol_last_close(zv); 85910588SEric.Taylor@Sun.COM mutex_exit(&zvol_state_lock); 86010588SEric.Taylor@Sun.COM return (err); 861789Sahrens } 862789Sahrens 863789Sahrens /*ARGSUSED*/ 864789Sahrens int 865789Sahrens zvol_close(dev_t dev, int flag, int otyp, cred_t *cr) 866789Sahrens { 867789Sahrens minor_t minor = getminor(dev); 868789Sahrens zvol_state_t *zv; 86910588SEric.Taylor@Sun.COM int error = 0; 870789Sahrens 871789Sahrens if (minor == 0) /* This is the control device */ 872789Sahrens return (0); 873789Sahrens 874789Sahrens mutex_enter(&zvol_state_lock); 875789Sahrens 876789Sahrens zv = ddi_get_soft_state(zvol_state, minor); 877789Sahrens if (zv == NULL) { 878789Sahrens mutex_exit(&zvol_state_lock); 879789Sahrens return (ENXIO); 880789Sahrens } 881789Sahrens 8827405SEric.Taylor@Sun.COM if (zv->zv_flags & ZVOL_EXCL) { 8837405SEric.Taylor@Sun.COM ASSERT(zv->zv_total_opens == 1); 8847405SEric.Taylor@Sun.COM zv->zv_flags &= ~ZVOL_EXCL; 885789Sahrens } 886789Sahrens 887789Sahrens /* 888789Sahrens * If the open count is zero, this is a spurious close. 889789Sahrens * That indicates a bug in the kernel / DDI framework. 890789Sahrens */ 891789Sahrens ASSERT(zv->zv_open_count[otyp] != 0); 892789Sahrens ASSERT(zv->zv_total_opens != 0); 893789Sahrens 894789Sahrens /* 895789Sahrens * You may get multiple opens, but only one close. 896789Sahrens */ 897789Sahrens zv->zv_open_count[otyp]--; 898789Sahrens zv->zv_total_opens--; 899789Sahrens 90010588SEric.Taylor@Sun.COM if (zv->zv_total_opens == 0) 90110588SEric.Taylor@Sun.COM zvol_last_close(zv); 902789Sahrens 90310588SEric.Taylor@Sun.COM mutex_exit(&zvol_state_lock); 90410588SEric.Taylor@Sun.COM return (error); 905789Sahrens } 906789Sahrens 9073638Sbillm static void 90810922SJeff.Bonwick@Sun.COM zvol_get_done(zgd_t *zgd, int error) 9093063Sperrin { 91010922SJeff.Bonwick@Sun.COM if (zgd->zgd_db) 91110922SJeff.Bonwick@Sun.COM dmu_buf_rele(zgd->zgd_db, zgd); 9123063Sperrin 91310922SJeff.Bonwick@Sun.COM zfs_range_unlock(zgd->zgd_rl); 91410922SJeff.Bonwick@Sun.COM 91510922SJeff.Bonwick@Sun.COM if (error == 0 && zgd->zgd_bp) 91610922SJeff.Bonwick@Sun.COM zil_add_block(zgd->zgd_zilog, zgd->zgd_bp); 91710922SJeff.Bonwick@Sun.COM 9183063Sperrin kmem_free(zgd, sizeof (zgd_t)); 9193063Sperrin } 9203063Sperrin 9213063Sperrin /* 9223063Sperrin * Get data to generate a TX_WRITE intent log record. 9233063Sperrin */ 9243638Sbillm static int 9253063Sperrin zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 9263063Sperrin { 9273063Sperrin zvol_state_t *zv = arg; 9283063Sperrin objset_t *os = zv->zv_objset; 92910922SJeff.Bonwick@Sun.COM uint64_t object = ZVOL_OBJ; 93010922SJeff.Bonwick@Sun.COM uint64_t offset = lr->lr_offset; 93110922SJeff.Bonwick@Sun.COM uint64_t size = lr->lr_length; /* length of user data */ 93210922SJeff.Bonwick@Sun.COM blkptr_t *bp = &lr->lr_blkptr; 9333063Sperrin dmu_buf_t *db; 9343063Sperrin zgd_t *zgd; 9353063Sperrin int error; 9363063Sperrin 93710922SJeff.Bonwick@Sun.COM ASSERT(zio != NULL); 93810922SJeff.Bonwick@Sun.COM ASSERT(size != 0); 93910922SJeff.Bonwick@Sun.COM 94010922SJeff.Bonwick@Sun.COM zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP); 94110922SJeff.Bonwick@Sun.COM zgd->zgd_zilog = zv->zv_zilog; 94210922SJeff.Bonwick@Sun.COM zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER); 9433638Sbillm 9443755Sperrin /* 9453755Sperrin * Write records come in two flavors: immediate and indirect. 9463755Sperrin * For small writes it's cheaper to store the data with the 9473755Sperrin * log record (immediate); for large writes it's cheaper to 9483755Sperrin * sync the data and get a pointer to it (indirect) so that 9493755Sperrin * we don't have to write the data twice. 9503755Sperrin */ 95110922SJeff.Bonwick@Sun.COM if (buf != NULL) { /* immediate write */ 95210922SJeff.Bonwick@Sun.COM error = dmu_read(os, object, offset, size, buf, 95310922SJeff.Bonwick@Sun.COM DMU_READ_NO_PREFETCH); 95410922SJeff.Bonwick@Sun.COM } else { 95510922SJeff.Bonwick@Sun.COM size = zv->zv_volblocksize; 95610922SJeff.Bonwick@Sun.COM offset = P2ALIGN(offset, size); 95712285SJeff.Bonwick@Sun.COM error = dmu_buf_hold(os, object, offset, zgd, &db, 95812285SJeff.Bonwick@Sun.COM DMU_READ_NO_PREFETCH); 95910922SJeff.Bonwick@Sun.COM if (error == 0) { 96010922SJeff.Bonwick@Sun.COM zgd->zgd_db = db; 96110922SJeff.Bonwick@Sun.COM zgd->zgd_bp = bp; 9623063Sperrin 96310922SJeff.Bonwick@Sun.COM ASSERT(db->db_offset == offset); 96410922SJeff.Bonwick@Sun.COM ASSERT(db->db_size == size); 9653755Sperrin 96610922SJeff.Bonwick@Sun.COM error = dmu_sync(zio, lr->lr_common.lrc_txg, 96710922SJeff.Bonwick@Sun.COM zvol_get_done, zgd); 96810800SNeil.Perrin@Sun.COM 96910922SJeff.Bonwick@Sun.COM if (error == 0) 97010922SJeff.Bonwick@Sun.COM return (0); 97110922SJeff.Bonwick@Sun.COM } 97210800SNeil.Perrin@Sun.COM } 97310800SNeil.Perrin@Sun.COM 97410922SJeff.Bonwick@Sun.COM zvol_get_done(zgd, error); 97510922SJeff.Bonwick@Sun.COM 9763063Sperrin return (error); 9773063Sperrin } 9783063Sperrin 9791861Sperrin /* 9801861Sperrin * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions. 9811141Sperrin * 9821141Sperrin * We store data in the log buffers if it's small enough. 9833063Sperrin * Otherwise we will later flush the data out via dmu_sync(). 9841141Sperrin */ 9853063Sperrin ssize_t zvol_immediate_write_sz = 32768; 9861141Sperrin 9873638Sbillm static void 9889401SNeil.Perrin@Sun.COM zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid, 9899401SNeil.Perrin@Sun.COM boolean_t sync) 9901141Sperrin { 9913638Sbillm uint32_t blocksize = zv->zv_volblocksize; 9928227SNeil.Perrin@Sun.COM zilog_t *zilog = zv->zv_zilog; 9939401SNeil.Perrin@Sun.COM boolean_t slogging; 99410310SNeil.Perrin@Sun.COM ssize_t immediate_write_sz; 9959401SNeil.Perrin@Sun.COM 99610922SJeff.Bonwick@Sun.COM if (zil_replaying(zilog, tx)) 9978227SNeil.Perrin@Sun.COM return; 9988227SNeil.Perrin@Sun.COM 99910310SNeil.Perrin@Sun.COM immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT) 100010310SNeil.Perrin@Sun.COM ? 0 : zvol_immediate_write_sz; 100110310SNeil.Perrin@Sun.COM 100210310SNeil.Perrin@Sun.COM slogging = spa_has_slogs(zilog->zl_spa) && 100310310SNeil.Perrin@Sun.COM (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY); 10049401SNeil.Perrin@Sun.COM 10059401SNeil.Perrin@Sun.COM while (resid) { 10069401SNeil.Perrin@Sun.COM itx_t *itx; 10079401SNeil.Perrin@Sun.COM lr_write_t *lr; 10089401SNeil.Perrin@Sun.COM ssize_t len; 10099401SNeil.Perrin@Sun.COM itx_wr_state_t write_state; 10103638Sbillm 10119401SNeil.Perrin@Sun.COM /* 10129401SNeil.Perrin@Sun.COM * Unlike zfs_log_write() we can be called with 10139401SNeil.Perrin@Sun.COM * upto DMU_MAX_ACCESS/2 (5MB) writes. 10149401SNeil.Perrin@Sun.COM */ 101510310SNeil.Perrin@Sun.COM if (blocksize > immediate_write_sz && !slogging && 10169401SNeil.Perrin@Sun.COM resid >= blocksize && off % blocksize == 0) { 10179401SNeil.Perrin@Sun.COM write_state = WR_INDIRECT; /* uses dmu_sync */ 10189401SNeil.Perrin@Sun.COM len = blocksize; 10199401SNeil.Perrin@Sun.COM } else if (sync) { 10209401SNeil.Perrin@Sun.COM write_state = WR_COPIED; 10219401SNeil.Perrin@Sun.COM len = MIN(ZIL_MAX_LOG_DATA, resid); 10229401SNeil.Perrin@Sun.COM } else { 10239401SNeil.Perrin@Sun.COM write_state = WR_NEED_COPY; 10249401SNeil.Perrin@Sun.COM len = MIN(ZIL_MAX_LOG_DATA, resid); 10259401SNeil.Perrin@Sun.COM } 10269401SNeil.Perrin@Sun.COM 10279401SNeil.Perrin@Sun.COM itx = zil_itx_create(TX_WRITE, sizeof (*lr) + 10289401SNeil.Perrin@Sun.COM (write_state == WR_COPIED ? len : 0)); 10293638Sbillm lr = (lr_write_t *)&itx->itx_lr; 10309401SNeil.Perrin@Sun.COM if (write_state == WR_COPIED && dmu_read(zv->zv_objset, 10319512SNeil.Perrin@Sun.COM ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) { 103210922SJeff.Bonwick@Sun.COM zil_itx_destroy(itx); 10339401SNeil.Perrin@Sun.COM itx = zil_itx_create(TX_WRITE, sizeof (*lr)); 10349401SNeil.Perrin@Sun.COM lr = (lr_write_t *)&itx->itx_lr; 10359401SNeil.Perrin@Sun.COM write_state = WR_NEED_COPY; 10369401SNeil.Perrin@Sun.COM } 10379401SNeil.Perrin@Sun.COM 10389401SNeil.Perrin@Sun.COM itx->itx_wr_state = write_state; 10399401SNeil.Perrin@Sun.COM if (write_state == WR_NEED_COPY) 10409401SNeil.Perrin@Sun.COM itx->itx_sod += len; 10413638Sbillm lr->lr_foid = ZVOL_OBJ; 10423638Sbillm lr->lr_offset = off; 10439401SNeil.Perrin@Sun.COM lr->lr_length = len; 104410922SJeff.Bonwick@Sun.COM lr->lr_blkoff = 0; 10453638Sbillm BP_ZERO(&lr->lr_blkptr); 10463638Sbillm 10479401SNeil.Perrin@Sun.COM itx->itx_private = zv; 10489401SNeil.Perrin@Sun.COM itx->itx_sync = sync; 10499401SNeil.Perrin@Sun.COM 10508227SNeil.Perrin@Sun.COM (void) zil_itx_assign(zilog, itx, tx); 10519401SNeil.Perrin@Sun.COM 10529401SNeil.Perrin@Sun.COM off += len; 10539401SNeil.Perrin@Sun.COM resid -= len; 10541141Sperrin } 10551141Sperrin } 10561141Sperrin 10577837SMatthew.Ahrens@Sun.COM static int 10587837SMatthew.Ahrens@Sun.COM zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t size, 10597837SMatthew.Ahrens@Sun.COM boolean_t doread, boolean_t isdump) 10606423Sgw25295 { 10616423Sgw25295 vdev_disk_t *dvd; 10626423Sgw25295 int c; 10636423Sgw25295 int numerrors = 0; 10646423Sgw25295 10656423Sgw25295 for (c = 0; c < vd->vdev_children; c++) { 10669790SLin.Ling@Sun.COM ASSERT(vd->vdev_ops == &vdev_mirror_ops || 10679790SLin.Ling@Sun.COM vd->vdev_ops == &vdev_replacing_ops || 10689790SLin.Ling@Sun.COM vd->vdev_ops == &vdev_spare_ops); 10697837SMatthew.Ahrens@Sun.COM int err = zvol_dumpio_vdev(vd->vdev_child[c], 10707837SMatthew.Ahrens@Sun.COM addr, offset, size, doread, isdump); 10717837SMatthew.Ahrens@Sun.COM if (err != 0) { 10726423Sgw25295 numerrors++; 10737837SMatthew.Ahrens@Sun.COM } else if (doread) { 10746423Sgw25295 break; 10756423Sgw25295 } 10766423Sgw25295 } 10776423Sgw25295 10786423Sgw25295 if (!vd->vdev_ops->vdev_op_leaf) 10796423Sgw25295 return (numerrors < vd->vdev_children ? 0 : EIO); 10806423Sgw25295 10817903SEric.Taylor@Sun.COM if (doread && !vdev_readable(vd)) 10827903SEric.Taylor@Sun.COM return (EIO); 10837903SEric.Taylor@Sun.COM else if (!doread && !vdev_writeable(vd)) 10846423Sgw25295 return (EIO); 10856423Sgw25295 10866423Sgw25295 dvd = vd->vdev_tsd; 10876423Sgw25295 ASSERT3P(dvd, !=, NULL); 10886423Sgw25295 offset += VDEV_LABEL_START_SIZE; 10896423Sgw25295 10906423Sgw25295 if (ddi_in_panic() || isdump) { 10917837SMatthew.Ahrens@Sun.COM ASSERT(!doread); 10927837SMatthew.Ahrens@Sun.COM if (doread) 10936423Sgw25295 return (EIO); 10946423Sgw25295 return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset), 10956423Sgw25295 lbtodb(size))); 10966423Sgw25295 } else { 10976423Sgw25295 return (vdev_disk_physio(dvd->vd_lh, addr, size, offset, 10987837SMatthew.Ahrens@Sun.COM doread ? B_READ : B_WRITE)); 10996423Sgw25295 } 11006423Sgw25295 } 11016423Sgw25295 11027837SMatthew.Ahrens@Sun.COM static int 11037837SMatthew.Ahrens@Sun.COM zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size, 11047837SMatthew.Ahrens@Sun.COM boolean_t doread, boolean_t isdump) 11056423Sgw25295 { 11066423Sgw25295 vdev_t *vd; 11076423Sgw25295 int error; 11087837SMatthew.Ahrens@Sun.COM zvol_extent_t *ze; 11096423Sgw25295 spa_t *spa = dmu_objset_spa(zv->zv_objset); 11106423Sgw25295 11117837SMatthew.Ahrens@Sun.COM /* Must be sector aligned, and not stradle a block boundary. */ 11127837SMatthew.Ahrens@Sun.COM if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) || 11137837SMatthew.Ahrens@Sun.COM P2BOUNDARY(offset, size, zv->zv_volblocksize)) { 11147837SMatthew.Ahrens@Sun.COM return (EINVAL); 11157837SMatthew.Ahrens@Sun.COM } 11166423Sgw25295 ASSERT(size <= zv->zv_volblocksize); 11176423Sgw25295 11187837SMatthew.Ahrens@Sun.COM /* Locate the extent this belongs to */ 11197837SMatthew.Ahrens@Sun.COM ze = list_head(&zv->zv_extents); 11207837SMatthew.Ahrens@Sun.COM while (offset >= ze->ze_nblks * zv->zv_volblocksize) { 11217837SMatthew.Ahrens@Sun.COM offset -= ze->ze_nblks * zv->zv_volblocksize; 11227837SMatthew.Ahrens@Sun.COM ze = list_next(&zv->zv_extents, ze); 11237837SMatthew.Ahrens@Sun.COM } 112411806SGeorge.Wilson@Sun.COM 112511806SGeorge.Wilson@Sun.COM if (!ddi_in_panic()) 112611806SGeorge.Wilson@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 112711806SGeorge.Wilson@Sun.COM 11287837SMatthew.Ahrens@Sun.COM vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva)); 11297837SMatthew.Ahrens@Sun.COM offset += DVA_GET_OFFSET(&ze->ze_dva); 11307837SMatthew.Ahrens@Sun.COM error = zvol_dumpio_vdev(vd, addr, offset, size, doread, isdump); 113111806SGeorge.Wilson@Sun.COM 113211806SGeorge.Wilson@Sun.COM if (!ddi_in_panic()) 113311806SGeorge.Wilson@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 113411806SGeorge.Wilson@Sun.COM 11356423Sgw25295 return (error); 11366423Sgw25295 } 11376423Sgw25295 11386423Sgw25295 int 1139789Sahrens zvol_strategy(buf_t *bp) 1140789Sahrens { 1141789Sahrens zvol_state_t *zv = ddi_get_soft_state(zvol_state, getminor(bp->b_edev)); 1142789Sahrens uint64_t off, volsize; 11437837SMatthew.Ahrens@Sun.COM size_t resid; 1144789Sahrens char *addr; 11451141Sperrin objset_t *os; 11463755Sperrin rl_t *rl; 1147789Sahrens int error = 0; 11487837SMatthew.Ahrens@Sun.COM boolean_t doread = bp->b_flags & B_READ; 114912095SChris.Kirby@sun.com boolean_t is_dump; 11509401SNeil.Perrin@Sun.COM boolean_t sync; 1151789Sahrens 1152789Sahrens if (zv == NULL) { 1153789Sahrens bioerror(bp, ENXIO); 1154789Sahrens biodone(bp); 1155789Sahrens return (0); 1156789Sahrens } 1157789Sahrens 1158789Sahrens if (getminor(bp->b_edev) == 0) { 1159789Sahrens bioerror(bp, EINVAL); 1160789Sahrens biodone(bp); 1161789Sahrens return (0); 1162789Sahrens } 1163789Sahrens 116410588SEric.Taylor@Sun.COM if (!(bp->b_flags & B_READ) && (zv->zv_flags & ZVOL_RDONLY)) { 1165789Sahrens bioerror(bp, EROFS); 1166789Sahrens biodone(bp); 1167789Sahrens return (0); 1168789Sahrens } 1169789Sahrens 1170789Sahrens off = ldbtob(bp->b_blkno); 1171789Sahrens volsize = zv->zv_volsize; 1172789Sahrens 11731141Sperrin os = zv->zv_objset; 11741141Sperrin ASSERT(os != NULL); 1175789Sahrens 1176789Sahrens bp_mapin(bp); 1177789Sahrens addr = bp->b_un.b_addr; 1178789Sahrens resid = bp->b_bcount; 1179789Sahrens 11807837SMatthew.Ahrens@Sun.COM if (resid > 0 && (off < 0 || off >= volsize)) { 11817837SMatthew.Ahrens@Sun.COM bioerror(bp, EIO); 11827837SMatthew.Ahrens@Sun.COM biodone(bp); 11837837SMatthew.Ahrens@Sun.COM return (0); 11847837SMatthew.Ahrens@Sun.COM } 11857013Sgw25295 118612095SChris.Kirby@sun.com is_dump = zv->zv_flags & ZVOL_DUMPIFIED; 1187*12294SMark.Musante@Sun.COM sync = ((!(bp->b_flags & B_ASYNC) && 1188*12294SMark.Musante@Sun.COM !(zv->zv_flags & ZVOL_WCE)) || 1189*12294SMark.Musante@Sun.COM (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)) && 1190*12294SMark.Musante@Sun.COM !doread && !is_dump; 11919401SNeil.Perrin@Sun.COM 11921861Sperrin /* 11931861Sperrin * There must be no buffer changes when doing a dmu_sync() because 11941861Sperrin * we can't change the data whilst calculating the checksum. 11951861Sperrin */ 11963755Sperrin rl = zfs_range_lock(&zv->zv_znode, off, resid, 11977837SMatthew.Ahrens@Sun.COM doread ? RL_READER : RL_WRITER); 11986423Sgw25295 1199789Sahrens while (resid != 0 && off < volsize) { 12007837SMatthew.Ahrens@Sun.COM size_t size = MIN(resid, zvol_maxphys); 12016423Sgw25295 if (is_dump) { 12026423Sgw25295 size = MIN(size, P2END(off, zv->zv_volblocksize) - off); 12037837SMatthew.Ahrens@Sun.COM error = zvol_dumpio(zv, addr, off, size, 12047837SMatthew.Ahrens@Sun.COM doread, B_FALSE); 12057837SMatthew.Ahrens@Sun.COM } else if (doread) { 12069512SNeil.Perrin@Sun.COM error = dmu_read(os, ZVOL_OBJ, off, size, addr, 12079512SNeil.Perrin@Sun.COM DMU_READ_PREFETCH); 1208789Sahrens } else { 12091141Sperrin dmu_tx_t *tx = dmu_tx_create(os); 1210789Sahrens dmu_tx_hold_write(tx, ZVOL_OBJ, off, size); 1211789Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 1212789Sahrens if (error) { 1213789Sahrens dmu_tx_abort(tx); 1214789Sahrens } else { 12151141Sperrin dmu_write(os, ZVOL_OBJ, off, size, addr, tx); 12169401SNeil.Perrin@Sun.COM zvol_log_write(zv, tx, off, size, sync); 1217789Sahrens dmu_tx_commit(tx); 1218789Sahrens } 1219789Sahrens } 12207294Sperrin if (error) { 12217294Sperrin /* convert checksum errors into IO errors */ 12227294Sperrin if (error == ECKSUM) 12237294Sperrin error = EIO; 1224789Sahrens break; 12257294Sperrin } 1226789Sahrens off += size; 1227789Sahrens addr += size; 1228789Sahrens resid -= size; 1229789Sahrens } 12303755Sperrin zfs_range_unlock(rl); 1231789Sahrens 1232789Sahrens if ((bp->b_resid = resid) == bp->b_bcount) 1233789Sahrens bioerror(bp, off > volsize ? EINVAL : error); 1234789Sahrens 12359401SNeil.Perrin@Sun.COM if (sync) 12363638Sbillm zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ); 12373638Sbillm biodone(bp); 12381141Sperrin 1239789Sahrens return (0); 1240789Sahrens } 1241789Sahrens 12423063Sperrin /* 12433063Sperrin * Set the buffer count to the zvol maximum transfer. 12443063Sperrin * Using our own routine instead of the default minphys() 12453063Sperrin * means that for larger writes we write bigger buffers on X86 12463063Sperrin * (128K instead of 56K) and flush the disk write cache less often 12473063Sperrin * (every zvol_maxphys - currently 1MB) instead of minphys (currently 12483063Sperrin * 56K on X86 and 128K on sparc). 12493063Sperrin */ 12503063Sperrin void 12513063Sperrin zvol_minphys(struct buf *bp) 12523063Sperrin { 12533063Sperrin if (bp->b_bcount > zvol_maxphys) 12543063Sperrin bp->b_bcount = zvol_maxphys; 12553063Sperrin } 12563063Sperrin 12576423Sgw25295 int 12586423Sgw25295 zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks) 12596423Sgw25295 { 12606423Sgw25295 minor_t minor = getminor(dev); 12616423Sgw25295 zvol_state_t *zv; 12626423Sgw25295 int error = 0; 12636423Sgw25295 uint64_t size; 12646423Sgw25295 uint64_t boff; 12656423Sgw25295 uint64_t resid; 12666423Sgw25295 12676423Sgw25295 if (minor == 0) /* This is the control device */ 12686423Sgw25295 return (ENXIO); 12696423Sgw25295 12706423Sgw25295 zv = ddi_get_soft_state(zvol_state, minor); 12716423Sgw25295 if (zv == NULL) 12726423Sgw25295 return (ENXIO); 12736423Sgw25295 12746423Sgw25295 boff = ldbtob(blkno); 12756423Sgw25295 resid = ldbtob(nblocks); 12767837SMatthew.Ahrens@Sun.COM 12777837SMatthew.Ahrens@Sun.COM VERIFY3U(boff + resid, <=, zv->zv_volsize); 12787837SMatthew.Ahrens@Sun.COM 12796423Sgw25295 while (resid) { 12806423Sgw25295 size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff); 12817837SMatthew.Ahrens@Sun.COM error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE); 12826423Sgw25295 if (error) 12836423Sgw25295 break; 12846423Sgw25295 boff += size; 12856423Sgw25295 addr += size; 12866423Sgw25295 resid -= size; 12876423Sgw25295 } 12886423Sgw25295 12896423Sgw25295 return (error); 12906423Sgw25295 } 12916423Sgw25295 1292789Sahrens /*ARGSUSED*/ 1293789Sahrens int 12943638Sbillm zvol_read(dev_t dev, uio_t *uio, cred_t *cr) 1295789Sahrens { 12964107Sgw25295 minor_t minor = getminor(dev); 12974107Sgw25295 zvol_state_t *zv; 12987013Sgw25295 uint64_t volsize; 12993755Sperrin rl_t *rl; 13003638Sbillm int error = 0; 13013638Sbillm 13024107Sgw25295 if (minor == 0) /* This is the control device */ 13034107Sgw25295 return (ENXIO); 13044107Sgw25295 13054107Sgw25295 zv = ddi_get_soft_state(zvol_state, minor); 13064107Sgw25295 if (zv == NULL) 13074107Sgw25295 return (ENXIO); 13084107Sgw25295 13097013Sgw25295 volsize = zv->zv_volsize; 13107013Sgw25295 if (uio->uio_resid > 0 && 13117013Sgw25295 (uio->uio_loffset < 0 || uio->uio_loffset >= volsize)) 13127013Sgw25295 return (EIO); 13137013Sgw25295 13147837SMatthew.Ahrens@Sun.COM if (zv->zv_flags & ZVOL_DUMPIFIED) { 13157837SMatthew.Ahrens@Sun.COM error = physio(zvol_strategy, NULL, dev, B_READ, 13167837SMatthew.Ahrens@Sun.COM zvol_minphys, uio); 13177837SMatthew.Ahrens@Sun.COM return (error); 13187837SMatthew.Ahrens@Sun.COM } 13197837SMatthew.Ahrens@Sun.COM 13203755Sperrin rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 13213755Sperrin RL_READER); 13227013Sgw25295 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 13233638Sbillm uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 13243638Sbillm 13257013Sgw25295 /* don't read past the end */ 13267013Sgw25295 if (bytes > volsize - uio->uio_loffset) 13277013Sgw25295 bytes = volsize - uio->uio_loffset; 13287013Sgw25295 13293638Sbillm error = dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes); 13307294Sperrin if (error) { 13317294Sperrin /* convert checksum errors into IO errors */ 13327294Sperrin if (error == ECKSUM) 13337294Sperrin error = EIO; 13343638Sbillm break; 13357294Sperrin } 13363638Sbillm } 13373755Sperrin zfs_range_unlock(rl); 13383638Sbillm return (error); 1339789Sahrens } 1340789Sahrens 1341789Sahrens /*ARGSUSED*/ 1342789Sahrens int 13433638Sbillm zvol_write(dev_t dev, uio_t *uio, cred_t *cr) 1344789Sahrens { 13454107Sgw25295 minor_t minor = getminor(dev); 13464107Sgw25295 zvol_state_t *zv; 13477013Sgw25295 uint64_t volsize; 13483755Sperrin rl_t *rl; 13493638Sbillm int error = 0; 13509401SNeil.Perrin@Sun.COM boolean_t sync; 13513638Sbillm 13524107Sgw25295 if (minor == 0) /* This is the control device */ 13534107Sgw25295 return (ENXIO); 13544107Sgw25295 13554107Sgw25295 zv = ddi_get_soft_state(zvol_state, minor); 13564107Sgw25295 if (zv == NULL) 13574107Sgw25295 return (ENXIO); 13584107Sgw25295 13597013Sgw25295 volsize = zv->zv_volsize; 13607013Sgw25295 if (uio->uio_resid > 0 && 13617013Sgw25295 (uio->uio_loffset < 0 || uio->uio_loffset >= volsize)) 13627013Sgw25295 return (EIO); 13637013Sgw25295 13646423Sgw25295 if (zv->zv_flags & ZVOL_DUMPIFIED) { 13656423Sgw25295 error = physio(zvol_strategy, NULL, dev, B_WRITE, 13666423Sgw25295 zvol_minphys, uio); 13676423Sgw25295 return (error); 13686423Sgw25295 } 13696423Sgw25295 1370*12294SMark.Musante@Sun.COM sync = !(zv->zv_flags & ZVOL_WCE) || 1371*12294SMark.Musante@Sun.COM (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS); 13729401SNeil.Perrin@Sun.COM 13733755Sperrin rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid, 13743755Sperrin RL_WRITER); 13757013Sgw25295 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) { 13763638Sbillm uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1); 13773638Sbillm uint64_t off = uio->uio_loffset; 13787013Sgw25295 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset); 1379789Sahrens 13807013Sgw25295 if (bytes > volsize - off) /* don't write past the end */ 13817013Sgw25295 bytes = volsize - off; 13827013Sgw25295 13833638Sbillm dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes); 13843638Sbillm error = dmu_tx_assign(tx, TXG_WAIT); 13853638Sbillm if (error) { 13863638Sbillm dmu_tx_abort(tx); 13873638Sbillm break; 13883638Sbillm } 138912123STim.Haley@Sun.COM error = dmu_write_uio_dbuf(zv->zv_dbuf, uio, bytes, tx); 13903638Sbillm if (error == 0) 13919401SNeil.Perrin@Sun.COM zvol_log_write(zv, tx, off, bytes, sync); 13923638Sbillm dmu_tx_commit(tx); 13933638Sbillm 13943638Sbillm if (error) 13953638Sbillm break; 13963638Sbillm } 13973755Sperrin zfs_range_unlock(rl); 13989401SNeil.Perrin@Sun.COM if (sync) 13998524SEric.Taylor@Sun.COM zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ); 14003638Sbillm return (error); 1401789Sahrens } 1402789Sahrens 14037405SEric.Taylor@Sun.COM int 14047405SEric.Taylor@Sun.COM zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs) 14057405SEric.Taylor@Sun.COM { 14067405SEric.Taylor@Sun.COM struct uuid uuid = EFI_RESERVED; 14077405SEric.Taylor@Sun.COM efi_gpe_t gpe = { 0 }; 14087405SEric.Taylor@Sun.COM uint32_t crc; 14097405SEric.Taylor@Sun.COM dk_efi_t efi; 14107405SEric.Taylor@Sun.COM int length; 14117405SEric.Taylor@Sun.COM char *ptr; 14127405SEric.Taylor@Sun.COM 14137405SEric.Taylor@Sun.COM if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag)) 14147405SEric.Taylor@Sun.COM return (EFAULT); 14157405SEric.Taylor@Sun.COM ptr = (char *)(uintptr_t)efi.dki_data_64; 14167405SEric.Taylor@Sun.COM length = efi.dki_length; 14177405SEric.Taylor@Sun.COM /* 14187405SEric.Taylor@Sun.COM * Some clients may attempt to request a PMBR for the 14197405SEric.Taylor@Sun.COM * zvol. Currently this interface will return EINVAL to 14207405SEric.Taylor@Sun.COM * such requests. These requests could be supported by 14217405SEric.Taylor@Sun.COM * adding a check for lba == 0 and consing up an appropriate 14227405SEric.Taylor@Sun.COM * PMBR. 14237405SEric.Taylor@Sun.COM */ 14247405SEric.Taylor@Sun.COM if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0) 14257405SEric.Taylor@Sun.COM return (EINVAL); 14267405SEric.Taylor@Sun.COM 14277405SEric.Taylor@Sun.COM gpe.efi_gpe_StartingLBA = LE_64(34ULL); 14287405SEric.Taylor@Sun.COM gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1); 14297405SEric.Taylor@Sun.COM UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid); 14307405SEric.Taylor@Sun.COM 14317405SEric.Taylor@Sun.COM if (efi.dki_lba == 1) { 14327405SEric.Taylor@Sun.COM efi_gpt_t gpt = { 0 }; 14337405SEric.Taylor@Sun.COM 14347405SEric.Taylor@Sun.COM gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE); 14357405SEric.Taylor@Sun.COM gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT); 14367405SEric.Taylor@Sun.COM gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt)); 14377405SEric.Taylor@Sun.COM gpt.efi_gpt_MyLBA = LE_64(1ULL); 14387405SEric.Taylor@Sun.COM gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL); 14397405SEric.Taylor@Sun.COM gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1); 14407405SEric.Taylor@Sun.COM gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL); 14417405SEric.Taylor@Sun.COM gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1); 14427405SEric.Taylor@Sun.COM gpt.efi_gpt_SizeOfPartitionEntry = 14437405SEric.Taylor@Sun.COM LE_32(sizeof (efi_gpe_t)); 14447405SEric.Taylor@Sun.COM CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table); 14457405SEric.Taylor@Sun.COM gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc); 14467405SEric.Taylor@Sun.COM CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table); 14477405SEric.Taylor@Sun.COM gpt.efi_gpt_HeaderCRC32 = LE_32(~crc); 14487405SEric.Taylor@Sun.COM if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length), 14497405SEric.Taylor@Sun.COM flag)) 14507405SEric.Taylor@Sun.COM return (EFAULT); 14517405SEric.Taylor@Sun.COM ptr += sizeof (gpt); 14527405SEric.Taylor@Sun.COM length -= sizeof (gpt); 14537405SEric.Taylor@Sun.COM } 14547405SEric.Taylor@Sun.COM if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe), 14557405SEric.Taylor@Sun.COM length), flag)) 14567405SEric.Taylor@Sun.COM return (EFAULT); 14577405SEric.Taylor@Sun.COM return (0); 14587405SEric.Taylor@Sun.COM } 14597405SEric.Taylor@Sun.COM 1460789Sahrens /* 1461789Sahrens * Dirtbag ioctls to support mkfs(1M) for UFS filesystems. See dkio(7I). 1462789Sahrens */ 1463789Sahrens /*ARGSUSED*/ 1464789Sahrens int 1465789Sahrens zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 1466789Sahrens { 1467789Sahrens zvol_state_t *zv; 14683897Smaybee struct dk_cinfo dki; 1469789Sahrens struct dk_minfo dkm; 14703897Smaybee struct dk_callback *dkc; 1471789Sahrens int error = 0; 14726423Sgw25295 rl_t *rl; 1473789Sahrens 1474789Sahrens mutex_enter(&zvol_state_lock); 1475789Sahrens 1476789Sahrens zv = ddi_get_soft_state(zvol_state, getminor(dev)); 1477789Sahrens 1478789Sahrens if (zv == NULL) { 1479789Sahrens mutex_exit(&zvol_state_lock); 1480789Sahrens return (ENXIO); 1481789Sahrens } 14829303SEric.Taylor@Sun.COM ASSERT(zv->zv_total_opens > 0); 1483789Sahrens 1484789Sahrens switch (cmd) { 1485789Sahrens 1486789Sahrens case DKIOCINFO: 14873897Smaybee bzero(&dki, sizeof (dki)); 14883897Smaybee (void) strcpy(dki.dki_cname, "zvol"); 14893897Smaybee (void) strcpy(dki.dki_dname, "zvol"); 14903897Smaybee dki.dki_ctype = DKC_UNKNOWN; 149111689SEric.Taylor@Sun.COM dki.dki_unit = getminor(dev); 14923897Smaybee dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs); 1493789Sahrens mutex_exit(&zvol_state_lock); 14943897Smaybee if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag)) 1495789Sahrens error = EFAULT; 1496789Sahrens return (error); 1497789Sahrens 1498789Sahrens case DKIOCGMEDIAINFO: 1499789Sahrens bzero(&dkm, sizeof (dkm)); 1500789Sahrens dkm.dki_lbsize = 1U << zv->zv_min_bs; 1501789Sahrens dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs; 1502789Sahrens dkm.dki_media_type = DK_UNKNOWN; 1503789Sahrens mutex_exit(&zvol_state_lock); 1504789Sahrens if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag)) 1505789Sahrens error = EFAULT; 1506789Sahrens return (error); 1507789Sahrens 1508789Sahrens case DKIOCGETEFI: 15097405SEric.Taylor@Sun.COM { 15107405SEric.Taylor@Sun.COM uint64_t vs = zv->zv_volsize; 15117405SEric.Taylor@Sun.COM uint8_t bs = zv->zv_min_bs; 15123016Smaybee 15133016Smaybee mutex_exit(&zvol_state_lock); 15147405SEric.Taylor@Sun.COM error = zvol_getefi((void *)arg, flag, vs, bs); 15157405SEric.Taylor@Sun.COM return (error); 15163016Smaybee } 1517789Sahrens 15183638Sbillm case DKIOCFLUSHWRITECACHE: 15193897Smaybee dkc = (struct dk_callback *)arg; 15209303SEric.Taylor@Sun.COM mutex_exit(&zvol_state_lock); 15213638Sbillm zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ); 15223897Smaybee if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) { 15233897Smaybee (*dkc->dkc_callback)(dkc->dkc_cookie, error); 15243897Smaybee error = 0; 15253897Smaybee } 15269303SEric.Taylor@Sun.COM return (error); 15279303SEric.Taylor@Sun.COM 15289303SEric.Taylor@Sun.COM case DKIOCGETWCE: 15299303SEric.Taylor@Sun.COM { 15309303SEric.Taylor@Sun.COM int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0; 15319303SEric.Taylor@Sun.COM if (ddi_copyout(&wce, (void *)arg, sizeof (int), 15329303SEric.Taylor@Sun.COM flag)) 15339303SEric.Taylor@Sun.COM error = EFAULT; 15349303SEric.Taylor@Sun.COM break; 15359303SEric.Taylor@Sun.COM } 15369303SEric.Taylor@Sun.COM case DKIOCSETWCE: 15379303SEric.Taylor@Sun.COM { 15389303SEric.Taylor@Sun.COM int wce; 15399303SEric.Taylor@Sun.COM if (ddi_copyin((void *)arg, &wce, sizeof (int), 15409303SEric.Taylor@Sun.COM flag)) { 15419303SEric.Taylor@Sun.COM error = EFAULT; 15429303SEric.Taylor@Sun.COM break; 15439303SEric.Taylor@Sun.COM } 15449303SEric.Taylor@Sun.COM if (wce) { 15459303SEric.Taylor@Sun.COM zv->zv_flags |= ZVOL_WCE; 15469303SEric.Taylor@Sun.COM mutex_exit(&zvol_state_lock); 15479303SEric.Taylor@Sun.COM } else { 15489303SEric.Taylor@Sun.COM zv->zv_flags &= ~ZVOL_WCE; 15499303SEric.Taylor@Sun.COM mutex_exit(&zvol_state_lock); 15509303SEric.Taylor@Sun.COM zil_commit(zv->zv_zilog, UINT64_MAX, ZVOL_OBJ); 15519303SEric.Taylor@Sun.COM } 15529303SEric.Taylor@Sun.COM return (0); 15539303SEric.Taylor@Sun.COM } 15543638Sbillm 15553245Smaybee case DKIOCGGEOM: 15563245Smaybee case DKIOCGVTOC: 15576423Sgw25295 /* 15586423Sgw25295 * commands using these (like prtvtoc) expect ENOTSUP 15596423Sgw25295 * since we're emulating an EFI label 15606423Sgw25295 */ 15613245Smaybee error = ENOTSUP; 15623245Smaybee break; 15633245Smaybee 15646423Sgw25295 case DKIOCDUMPINIT: 15656423Sgw25295 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 15666423Sgw25295 RL_WRITER); 15676423Sgw25295 error = zvol_dumpify(zv); 15686423Sgw25295 zfs_range_unlock(rl); 15696423Sgw25295 break; 15706423Sgw25295 15716423Sgw25295 case DKIOCDUMPFINI: 15729277SEric.Taylor@Sun.COM if (!(zv->zv_flags & ZVOL_DUMPIFIED)) 15739277SEric.Taylor@Sun.COM break; 15746423Sgw25295 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize, 15756423Sgw25295 RL_WRITER); 15766423Sgw25295 error = zvol_dump_fini(zv); 15776423Sgw25295 zfs_range_unlock(rl); 15786423Sgw25295 break; 15796423Sgw25295 1580789Sahrens default: 15813016Smaybee error = ENOTTY; 1582789Sahrens break; 1583789Sahrens 1584789Sahrens } 1585789Sahrens mutex_exit(&zvol_state_lock); 1586789Sahrens return (error); 1587789Sahrens } 1588789Sahrens 1589789Sahrens int 1590789Sahrens zvol_busy(void) 1591789Sahrens { 1592789Sahrens return (zvol_minors != 0); 1593789Sahrens } 1594789Sahrens 1595789Sahrens void 1596789Sahrens zvol_init(void) 1597789Sahrens { 1598789Sahrens VERIFY(ddi_soft_state_init(&zvol_state, sizeof (zvol_state_t), 1) == 0); 1599789Sahrens mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL); 1600789Sahrens } 1601789Sahrens 1602789Sahrens void 1603789Sahrens zvol_fini(void) 1604789Sahrens { 1605789Sahrens mutex_destroy(&zvol_state_lock); 1606789Sahrens ddi_soft_state_fini(&zvol_state); 1607789Sahrens } 16086423Sgw25295 16096423Sgw25295 static int 16106423Sgw25295 zvol_dump_init(zvol_state_t *zv, boolean_t resize) 16116423Sgw25295 { 16126423Sgw25295 dmu_tx_t *tx; 16136423Sgw25295 int error = 0; 16146423Sgw25295 objset_t *os = zv->zv_objset; 16156423Sgw25295 nvlist_t *nv = NULL; 161611656SGeorge.Wilson@Sun.COM uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset)); 16176423Sgw25295 16186423Sgw25295 ASSERT(MUTEX_HELD(&zvol_state_lock)); 161910588SEric.Taylor@Sun.COM error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0, 162010588SEric.Taylor@Sun.COM DMU_OBJECT_END); 162110588SEric.Taylor@Sun.COM /* wait for dmu_free_long_range to actually free the blocks */ 162210588SEric.Taylor@Sun.COM txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 16236423Sgw25295 16246423Sgw25295 tx = dmu_tx_create(os); 16256423Sgw25295 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 162610588SEric.Taylor@Sun.COM dmu_tx_hold_bonus(tx, ZVOL_OBJ); 16276423Sgw25295 error = dmu_tx_assign(tx, TXG_WAIT); 16286423Sgw25295 if (error) { 16296423Sgw25295 dmu_tx_abort(tx); 16306423Sgw25295 return (error); 16316423Sgw25295 } 16326423Sgw25295 16336423Sgw25295 /* 16346423Sgw25295 * If we are resizing the dump device then we only need to 16356423Sgw25295 * update the refreservation to match the newly updated 16366423Sgw25295 * zvolsize. Otherwise, we save off the original state of the 16376423Sgw25295 * zvol so that we can restore them if the zvol is ever undumpified. 16386423Sgw25295 */ 16396423Sgw25295 if (resize) { 16406423Sgw25295 error = zap_update(os, ZVOL_ZAP_OBJ, 16416423Sgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 16426423Sgw25295 &zv->zv_volsize, tx); 16436423Sgw25295 } else { 164411619SGeorge.Wilson@Sun.COM uint64_t checksum, compress, refresrv, vbs, dedup; 16457837SMatthew.Ahrens@Sun.COM 16466423Sgw25295 error = dsl_prop_get_integer(zv->zv_name, 16476423Sgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL); 16486423Sgw25295 error = error ? error : dsl_prop_get_integer(zv->zv_name, 16496423Sgw25295 zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL); 16506423Sgw25295 error = error ? error : dsl_prop_get_integer(zv->zv_name, 16516423Sgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL); 16527837SMatthew.Ahrens@Sun.COM error = error ? error : dsl_prop_get_integer(zv->zv_name, 16537837SMatthew.Ahrens@Sun.COM zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL); 165411656SGeorge.Wilson@Sun.COM if (version >= SPA_VERSION_DEDUP) { 165511656SGeorge.Wilson@Sun.COM error = error ? error : 165611656SGeorge.Wilson@Sun.COM dsl_prop_get_integer(zv->zv_name, 165711656SGeorge.Wilson@Sun.COM zfs_prop_to_name(ZFS_PROP_DEDUP), &dedup, NULL); 165811656SGeorge.Wilson@Sun.COM } 16596423Sgw25295 16606423Sgw25295 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 16616423Sgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, 16626423Sgw25295 &compress, tx); 16636423Sgw25295 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 16646423Sgw25295 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx); 16656423Sgw25295 error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 16666423Sgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, 16676423Sgw25295 &refresrv, tx); 16687837SMatthew.Ahrens@Sun.COM error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 16697837SMatthew.Ahrens@Sun.COM zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, 16707837SMatthew.Ahrens@Sun.COM &vbs, tx); 167110588SEric.Taylor@Sun.COM error = error ? error : dmu_object_set_blocksize( 167210588SEric.Taylor@Sun.COM os, ZVOL_OBJ, SPA_MAXBLOCKSIZE, 0, tx); 167311656SGeorge.Wilson@Sun.COM if (version >= SPA_VERSION_DEDUP) { 167411656SGeorge.Wilson@Sun.COM error = error ? error : zap_update(os, ZVOL_ZAP_OBJ, 167511656SGeorge.Wilson@Sun.COM zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, 167611656SGeorge.Wilson@Sun.COM &dedup, tx); 167711656SGeorge.Wilson@Sun.COM } 167810588SEric.Taylor@Sun.COM if (error == 0) 167910588SEric.Taylor@Sun.COM zv->zv_volblocksize = SPA_MAXBLOCKSIZE; 16806423Sgw25295 } 16816423Sgw25295 dmu_tx_commit(tx); 16826423Sgw25295 16836423Sgw25295 /* 16846423Sgw25295 * We only need update the zvol's property if we are initializing 16856423Sgw25295 * the dump area for the first time. 16866423Sgw25295 */ 16876423Sgw25295 if (!resize) { 16886423Sgw25295 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 16896423Sgw25295 VERIFY(nvlist_add_uint64(nv, 16906423Sgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0); 16916423Sgw25295 VERIFY(nvlist_add_uint64(nv, 16926423Sgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 16936423Sgw25295 ZIO_COMPRESS_OFF) == 0); 16946423Sgw25295 VERIFY(nvlist_add_uint64(nv, 16956423Sgw25295 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 16966423Sgw25295 ZIO_CHECKSUM_OFF) == 0); 169711656SGeorge.Wilson@Sun.COM if (version >= SPA_VERSION_DEDUP) { 169811656SGeorge.Wilson@Sun.COM VERIFY(nvlist_add_uint64(nv, 169911656SGeorge.Wilson@Sun.COM zfs_prop_to_name(ZFS_PROP_DEDUP), 170011656SGeorge.Wilson@Sun.COM ZIO_CHECKSUM_OFF) == 0); 170111656SGeorge.Wilson@Sun.COM } 17026423Sgw25295 170311022STom.Erickson@Sun.COM error = zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL, 170411022STom.Erickson@Sun.COM nv, NULL); 17056423Sgw25295 nvlist_free(nv); 17066423Sgw25295 17076423Sgw25295 if (error) 17086423Sgw25295 return (error); 17096423Sgw25295 } 17106423Sgw25295 17116423Sgw25295 /* Allocate the space for the dump */ 17126423Sgw25295 error = zvol_prealloc(zv); 17136423Sgw25295 return (error); 17146423Sgw25295 } 17156423Sgw25295 17166423Sgw25295 static int 17176423Sgw25295 zvol_dumpify(zvol_state_t *zv) 17186423Sgw25295 { 17196423Sgw25295 int error = 0; 17206423Sgw25295 uint64_t dumpsize = 0; 17216423Sgw25295 dmu_tx_t *tx; 17226423Sgw25295 objset_t *os = zv->zv_objset; 17236423Sgw25295 172410588SEric.Taylor@Sun.COM if (zv->zv_flags & ZVOL_RDONLY) 17256423Sgw25295 return (EROFS); 17266423Sgw25295 17276423Sgw25295 if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 17286423Sgw25295 8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) { 17296423Sgw25295 boolean_t resize = (dumpsize > 0) ? B_TRUE : B_FALSE; 17306423Sgw25295 17316423Sgw25295 if ((error = zvol_dump_init(zv, resize)) != 0) { 17326423Sgw25295 (void) zvol_dump_fini(zv); 17336423Sgw25295 return (error); 17346423Sgw25295 } 17356423Sgw25295 } 17366423Sgw25295 17376423Sgw25295 /* 17386423Sgw25295 * Build up our lba mapping. 17396423Sgw25295 */ 17406423Sgw25295 error = zvol_get_lbas(zv); 17416423Sgw25295 if (error) { 17426423Sgw25295 (void) zvol_dump_fini(zv); 17436423Sgw25295 return (error); 17446423Sgw25295 } 17456423Sgw25295 17466423Sgw25295 tx = dmu_tx_create(os); 17476423Sgw25295 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 17486423Sgw25295 error = dmu_tx_assign(tx, TXG_WAIT); 17496423Sgw25295 if (error) { 17506423Sgw25295 dmu_tx_abort(tx); 17516423Sgw25295 (void) zvol_dump_fini(zv); 17526423Sgw25295 return (error); 17536423Sgw25295 } 17546423Sgw25295 17556423Sgw25295 zv->zv_flags |= ZVOL_DUMPIFIED; 17566423Sgw25295 error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1, 17576423Sgw25295 &zv->zv_volsize, tx); 17586423Sgw25295 dmu_tx_commit(tx); 17596423Sgw25295 17606423Sgw25295 if (error) { 17616423Sgw25295 (void) zvol_dump_fini(zv); 17626423Sgw25295 return (error); 17636423Sgw25295 } 17646423Sgw25295 17656423Sgw25295 txg_wait_synced(dmu_objset_pool(os), 0); 17666423Sgw25295 return (0); 17676423Sgw25295 } 17686423Sgw25295 17696423Sgw25295 static int 17706423Sgw25295 zvol_dump_fini(zvol_state_t *zv) 17716423Sgw25295 { 17726423Sgw25295 dmu_tx_t *tx; 17736423Sgw25295 objset_t *os = zv->zv_objset; 17746423Sgw25295 nvlist_t *nv; 17756423Sgw25295 int error = 0; 177611619SGeorge.Wilson@Sun.COM uint64_t checksum, compress, refresrv, vbs, dedup; 177711656SGeorge.Wilson@Sun.COM uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset)); 17786423Sgw25295 17797080Smaybee /* 17807080Smaybee * Attempt to restore the zvol back to its pre-dumpified state. 17817080Smaybee * This is a best-effort attempt as it's possible that not all 17827080Smaybee * of these properties were initialized during the dumpify process 17837080Smaybee * (i.e. error during zvol_dump_init). 17847080Smaybee */ 17857080Smaybee 17866423Sgw25295 tx = dmu_tx_create(os); 17876423Sgw25295 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); 17886423Sgw25295 error = dmu_tx_assign(tx, TXG_WAIT); 17896423Sgw25295 if (error) { 17906423Sgw25295 dmu_tx_abort(tx); 17916423Sgw25295 return (error); 17926423Sgw25295 } 17937080Smaybee (void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx); 17947080Smaybee dmu_tx_commit(tx); 17956423Sgw25295 17966423Sgw25295 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 17976423Sgw25295 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum); 17986423Sgw25295 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 17996423Sgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress); 18006423Sgw25295 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 18016423Sgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv); 18027837SMatthew.Ahrens@Sun.COM (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 18037837SMatthew.Ahrens@Sun.COM zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs); 18046423Sgw25295 18056423Sgw25295 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 18066423Sgw25295 (void) nvlist_add_uint64(nv, 18076423Sgw25295 zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum); 18086423Sgw25295 (void) nvlist_add_uint64(nv, 18096423Sgw25295 zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress); 18106423Sgw25295 (void) nvlist_add_uint64(nv, 18116423Sgw25295 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv); 181211656SGeorge.Wilson@Sun.COM if (version >= SPA_VERSION_DEDUP && 181311656SGeorge.Wilson@Sun.COM zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, 181411656SGeorge.Wilson@Sun.COM zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, &dedup) == 0) { 181511656SGeorge.Wilson@Sun.COM (void) nvlist_add_uint64(nv, 181611656SGeorge.Wilson@Sun.COM zfs_prop_to_name(ZFS_PROP_DEDUP), dedup); 181711656SGeorge.Wilson@Sun.COM } 181811022STom.Erickson@Sun.COM (void) zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL, 181911022STom.Erickson@Sun.COM nv, NULL); 18206423Sgw25295 nvlist_free(nv); 18216423Sgw25295 18227080Smaybee zvol_free_extents(zv); 18237080Smaybee zv->zv_flags &= ~ZVOL_DUMPIFIED; 18247080Smaybee (void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END); 182510588SEric.Taylor@Sun.COM /* wait for dmu_free_long_range to actually free the blocks */ 182610588SEric.Taylor@Sun.COM txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); 182710588SEric.Taylor@Sun.COM tx = dmu_tx_create(os); 182810588SEric.Taylor@Sun.COM dmu_tx_hold_bonus(tx, ZVOL_OBJ); 182910588SEric.Taylor@Sun.COM error = dmu_tx_assign(tx, TXG_WAIT); 183010588SEric.Taylor@Sun.COM if (error) { 183110588SEric.Taylor@Sun.COM dmu_tx_abort(tx); 183210588SEric.Taylor@Sun.COM return (error); 183310588SEric.Taylor@Sun.COM } 183410922SJeff.Bonwick@Sun.COM if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0) 183510922SJeff.Bonwick@Sun.COM zv->zv_volblocksize = vbs; 183610588SEric.Taylor@Sun.COM dmu_tx_commit(tx); 18377080Smaybee 18386423Sgw25295 return (0); 18396423Sgw25295 } 1840