1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51485Slling * Common Development and Distribution License (the "License"). 61485Slling * 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 /* 2212247SGeorge.Wilson@Sun.COM * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23789Sahrens */ 24789Sahrens 25789Sahrens #ifndef _SYS_VDEV_IMPL_H 26789Sahrens #define _SYS_VDEV_IMPL_H 27789Sahrens 28789Sahrens #include <sys/avl.h> 29789Sahrens #include <sys/dmu.h> 30789Sahrens #include <sys/metaslab.h> 31789Sahrens #include <sys/nvpair.h> 32789Sahrens #include <sys/space_map.h> 33789Sahrens #include <sys/vdev.h> 34789Sahrens #include <sys/dkio.h> 35789Sahrens #include <sys/uberblock_impl.h> 36789Sahrens 37789Sahrens #ifdef __cplusplus 38789Sahrens extern "C" { 39789Sahrens #endif 40789Sahrens 41789Sahrens /* 42789Sahrens * Virtual device descriptors. 43789Sahrens * 44789Sahrens * All storage pool operations go through the virtual device framework, 45789Sahrens * which provides data replication and I/O scheduling. 46789Sahrens */ 47789Sahrens 48789Sahrens /* 49789Sahrens * Forward declarations that lots of things need. 50789Sahrens */ 51789Sahrens typedef struct vdev_queue vdev_queue_t; 52789Sahrens typedef struct vdev_cache vdev_cache_t; 53789Sahrens typedef struct vdev_cache_entry vdev_cache_entry_t; 54789Sahrens 55789Sahrens /* 56789Sahrens * Virtual device operations 57789Sahrens */ 58789Sahrens typedef int vdev_open_func_t(vdev_t *vd, uint64_t *size, uint64_t *ashift); 59789Sahrens typedef void vdev_close_func_t(vdev_t *vd); 60789Sahrens typedef uint64_t vdev_asize_func_t(vdev_t *vd, uint64_t psize); 615530Sbonwick typedef int vdev_io_start_func_t(zio_t *zio); 627754SJeff.Bonwick@Sun.COM typedef void vdev_io_done_func_t(zio_t *zio); 63789Sahrens typedef void vdev_state_change_func_t(vdev_t *vd, int, int); 6411958SGeorge.Wilson@Sun.COM typedef void vdev_hold_func_t(vdev_t *vd); 6511958SGeorge.Wilson@Sun.COM typedef void vdev_rele_func_t(vdev_t *vd); 66789Sahrens 67789Sahrens typedef struct vdev_ops { 68789Sahrens vdev_open_func_t *vdev_op_open; 69789Sahrens vdev_close_func_t *vdev_op_close; 70789Sahrens vdev_asize_func_t *vdev_op_asize; 71789Sahrens vdev_io_start_func_t *vdev_op_io_start; 72789Sahrens vdev_io_done_func_t *vdev_op_io_done; 73789Sahrens vdev_state_change_func_t *vdev_op_state_change; 7411958SGeorge.Wilson@Sun.COM vdev_hold_func_t *vdev_op_hold; 7511958SGeorge.Wilson@Sun.COM vdev_rele_func_t *vdev_op_rele; 76789Sahrens char vdev_op_type[16]; 77789Sahrens boolean_t vdev_op_leaf; 78789Sahrens } vdev_ops_t; 79789Sahrens 80789Sahrens /* 81789Sahrens * Virtual device properties 82789Sahrens */ 83789Sahrens struct vdev_cache_entry { 84789Sahrens char *ve_data; 85789Sahrens uint64_t ve_offset; 86789Sahrens uint64_t ve_lastused; 87789Sahrens avl_node_t ve_offset_node; 88789Sahrens avl_node_t ve_lastused_node; 89789Sahrens uint32_t ve_hits; 90789Sahrens uint16_t ve_missed_update; 91789Sahrens zio_t *ve_fill_io; 92789Sahrens }; 93789Sahrens 94789Sahrens struct vdev_cache { 95789Sahrens avl_tree_t vc_offset_tree; 96789Sahrens avl_tree_t vc_lastused_tree; 97789Sahrens kmutex_t vc_lock; 98789Sahrens }; 99789Sahrens 100789Sahrens struct vdev_queue { 101789Sahrens avl_tree_t vq_deadline_tree; 102789Sahrens avl_tree_t vq_read_tree; 103789Sahrens avl_tree_t vq_write_tree; 104789Sahrens avl_tree_t vq_pending_tree; 105789Sahrens kmutex_t vq_lock; 106789Sahrens }; 107789Sahrens 108789Sahrens /* 109789Sahrens * Virtual device descriptor 110789Sahrens */ 111789Sahrens struct vdev { 112789Sahrens /* 113789Sahrens * Common to all vdev types. 114789Sahrens */ 115789Sahrens uint64_t vdev_id; /* child number in vdev parent */ 116789Sahrens uint64_t vdev_guid; /* unique ID for this vdev */ 117789Sahrens uint64_t vdev_guid_sum; /* self guid + all child guids */ 11811422SMark.Musante@Sun.COM uint64_t vdev_orig_guid; /* orig. guid prior to remove */ 119789Sahrens uint64_t vdev_asize; /* allocatable device capacity */ 1209816SGeorge.Wilson@Sun.COM uint64_t vdev_min_asize; /* min acceptable asize */ 121789Sahrens uint64_t vdev_ashift; /* block alignment shift */ 122789Sahrens uint64_t vdev_state; /* see VDEV_STATE_* #defines */ 1231986Seschrock uint64_t vdev_prevstate; /* used when reopening a vdev */ 124789Sahrens vdev_ops_t *vdev_ops; /* vdev operations */ 125789Sahrens spa_t *vdev_spa; /* spa for this vdev */ 126789Sahrens void *vdev_tsd; /* type-specific data */ 12711958SGeorge.Wilson@Sun.COM vnode_t *vdev_name_vp; /* vnode for pathname */ 12811958SGeorge.Wilson@Sun.COM vnode_t *vdev_devid_vp; /* vnode for devid */ 129789Sahrens vdev_t *vdev_top; /* top-level vdev */ 130789Sahrens vdev_t *vdev_parent; /* parent vdev */ 131789Sahrens vdev_t **vdev_child; /* array of children */ 132789Sahrens uint64_t vdev_children; /* number of children */ 1338241SJeff.Bonwick@Sun.COM space_map_t vdev_dtl[DTL_TYPES]; /* in-core dirty time logs */ 134789Sahrens vdev_stat_t vdev_stat; /* virtual device statistics */ 1359816SGeorge.Wilson@Sun.COM boolean_t vdev_expanding; /* expand the vdev? */ 13610850SGeorge.Wilson@Sun.COM boolean_t vdev_reopening; /* reopen in progress? */ 1379846SEric.Taylor@Sun.COM int vdev_open_error; /* error on last open */ 1389846SEric.Taylor@Sun.COM kthread_t *vdev_open_thread; /* thread opening children */ 13910594SGeorge.Wilson@Sun.COM uint64_t vdev_crtxg; /* txg when top-level was added */ 140789Sahrens 141789Sahrens /* 142789Sahrens * Top-level vdev state. 143789Sahrens */ 144789Sahrens uint64_t vdev_ms_array; /* metaslab array object */ 145789Sahrens uint64_t vdev_ms_shift; /* metaslab size shift */ 146789Sahrens uint64_t vdev_ms_count; /* number of metaslabs */ 147789Sahrens metaslab_group_t *vdev_mg; /* metaslab group */ 148789Sahrens metaslab_t **vdev_ms; /* metaslab array */ 149789Sahrens txg_list_t vdev_ms_list; /* per-txg dirty metaslab lists */ 150789Sahrens txg_list_t vdev_dtl_list; /* per-txg dirty DTL lists */ 151789Sahrens txg_node_t vdev_txg_node; /* per-txg dirty vdev linkage */ 1524451Seschrock boolean_t vdev_remove_wanted; /* async remove wanted? */ 1537754SJeff.Bonwick@Sun.COM boolean_t vdev_probe_wanted; /* async probe wanted? */ 15412296SLin.Ling@Sun.COM uint64_t vdev_removing; /* device is being removed? */ 1557754SJeff.Bonwick@Sun.COM list_node_t vdev_config_dirty_node; /* config dirty list */ 1567754SJeff.Bonwick@Sun.COM list_node_t vdev_state_dirty_node; /* state dirty list */ 1572082Seschrock uint64_t vdev_deflate_ratio; /* deflation ratio (x512) */ 1584527Sperrin uint64_t vdev_islog; /* is an intent log device */ 15910594SGeorge.Wilson@Sun.COM uint64_t vdev_ishole; /* is a hole in the namespace */ 160789Sahrens 161789Sahrens /* 162789Sahrens * Leaf vdev state. 163789Sahrens */ 164789Sahrens uint64_t vdev_psize; /* physical device capacity */ 1658241SJeff.Bonwick@Sun.COM space_map_obj_t vdev_dtl_smo; /* dirty time log space map obj */ 166789Sahrens txg_node_t vdev_dtl_node; /* per-txg dirty DTL linkage */ 1671171Seschrock uint64_t vdev_wholedisk; /* true if this is a whole disk */ 1684451Seschrock uint64_t vdev_offline; /* persistent offline state */ 1694451Seschrock uint64_t vdev_faulted; /* persistent faulted state */ 1704451Seschrock uint64_t vdev_degraded; /* persistent degraded state */ 1714451Seschrock uint64_t vdev_removed; /* persistent removed state */ 172*13037SMark.Musante@Sun.COM uint64_t vdev_resilvering; /* persistent resilvering state */ 1732082Seschrock uint64_t vdev_nparity; /* number of parity devices for raidz */ 174789Sahrens char *vdev_path; /* vdev path (if any) */ 175789Sahrens char *vdev_devid; /* vdev devid (if any) */ 1764451Seschrock char *vdev_physpath; /* vdev device path (if any) */ 1779425SEric.Schrock@Sun.COM char *vdev_fru; /* physical FRU location */ 1787754SJeff.Bonwick@Sun.COM uint64_t vdev_not_present; /* not present during import */ 1797754SJeff.Bonwick@Sun.COM uint64_t vdev_unspare; /* unspare when resilvering done */ 1807754SJeff.Bonwick@Sun.COM hrtime_t vdev_last_try; /* last reopen time */ 1817754SJeff.Bonwick@Sun.COM boolean_t vdev_nowritecache; /* true if flushwritecache failed */ 1827754SJeff.Bonwick@Sun.COM boolean_t vdev_checkremove; /* temporary online test */ 1837754SJeff.Bonwick@Sun.COM boolean_t vdev_forcefault; /* force online fault */ 18411422SMark.Musante@Sun.COM boolean_t vdev_splitting; /* split or repair in progress */ 18512247SGeorge.Wilson@Sun.COM boolean_t vdev_delayed_close; /* delayed device close? */ 1861485Slling uint8_t vdev_tmpoffline; /* device taken offline temporarily? */ 187789Sahrens uint8_t vdev_detached; /* device detached? */ 1887754SJeff.Bonwick@Sun.COM uint8_t vdev_cant_read; /* vdev is failing all reads */ 1897754SJeff.Bonwick@Sun.COM uint8_t vdev_cant_write; /* vdev is failing all writes */ 1904527Sperrin uint64_t vdev_isspare; /* was a hot spare */ 1915450Sbrendan uint64_t vdev_isl2cache; /* was a l2cache device */ 192789Sahrens vdev_queue_t vdev_queue; /* I/O deadline schedule queue */ 193789Sahrens vdev_cache_t vdev_cache; /* physical block cache */ 1946643Seschrock spa_aux_vdev_t *vdev_aux; /* for l2cache vdevs */ 1957754SJeff.Bonwick@Sun.COM zio_t *vdev_probe_zio; /* root of current probe */ 19610817SEric.Schrock@Sun.COM vdev_aux_t vdev_label_aux; /* on-disk aux state */ 197789Sahrens 198789Sahrens /* 199789Sahrens * For DTrace to work in userland (libzpool) context, these fields must 200789Sahrens * remain at the end of the structure. DTrace will use the kernel's 201789Sahrens * CTF definition for 'struct vdev', and since the size of a kmutex_t is 202789Sahrens * larger in userland, the offsets for the rest fields would be 203789Sahrens * incorrect. 204789Sahrens */ 205789Sahrens kmutex_t vdev_dtl_lock; /* vdev_dtl_{map,resilver} */ 206789Sahrens kmutex_t vdev_stat_lock; /* vdev_stat */ 2077754SJeff.Bonwick@Sun.COM kmutex_t vdev_probe_lock; /* protects vdev_probe_zio */ 208789Sahrens }; 209789Sahrens 21010922SJeff.Bonwick@Sun.COM #define VDEV_RAIDZ_MAXPARITY 3 21110922SJeff.Bonwick@Sun.COM 2129056SLin.Ling@Sun.COM #define VDEV_PAD_SIZE (8 << 10) 2139056SLin.Ling@Sun.COM /* 2 padding areas (vl_pad1 and vl_pad2) to skip */ 2149056SLin.Ling@Sun.COM #define VDEV_SKIP_SIZE VDEV_PAD_SIZE * 2 215789Sahrens #define VDEV_PHYS_SIZE (112 << 10) 2161732Sbonwick #define VDEV_UBERBLOCK_RING (128 << 10) 2171732Sbonwick 2181732Sbonwick #define VDEV_UBERBLOCK_SHIFT(vd) \ 2191732Sbonwick MAX((vd)->vdev_top->vdev_ashift, UBERBLOCK_SHIFT) 2201732Sbonwick #define VDEV_UBERBLOCK_COUNT(vd) \ 2211732Sbonwick (VDEV_UBERBLOCK_RING >> VDEV_UBERBLOCK_SHIFT(vd)) 2221732Sbonwick #define VDEV_UBERBLOCK_OFFSET(vd, n) \ 2231732Sbonwick offsetof(vdev_label_t, vl_uberblock[(n) << VDEV_UBERBLOCK_SHIFT(vd)]) 2241732Sbonwick #define VDEV_UBERBLOCK_SIZE(vd) (1ULL << VDEV_UBERBLOCK_SHIFT(vd)) 225789Sahrens 226789Sahrens typedef struct vdev_phys { 22711670SNeil.Perrin@Sun.COM char vp_nvlist[VDEV_PHYS_SIZE - sizeof (zio_eck_t)]; 22811670SNeil.Perrin@Sun.COM zio_eck_t vp_zbt; 229789Sahrens } vdev_phys_t; 230789Sahrens 231789Sahrens typedef struct vdev_label { 2329056SLin.Ling@Sun.COM char vl_pad1[VDEV_PAD_SIZE]; /* 8K */ 2339056SLin.Ling@Sun.COM char vl_pad2[VDEV_PAD_SIZE]; /* 8K */ 2341732Sbonwick vdev_phys_t vl_vdev_phys; /* 112K */ 2351732Sbonwick char vl_uberblock[VDEV_UBERBLOCK_RING]; /* 128K */ 236789Sahrens } vdev_label_t; /* 256K total */ 237789Sahrens 238789Sahrens /* 2391732Sbonwick * vdev_dirty() flags 2401732Sbonwick */ 2411732Sbonwick #define VDD_METASLAB 0x01 2421732Sbonwick #define VDD_DTL 0x02 2431732Sbonwick 2441732Sbonwick /* 245789Sahrens * Size and offset of embedded boot loader region on each label. 246789Sahrens * The total size of the first two labels plus the boot area is 4MB. 247789Sahrens */ 248789Sahrens #define VDEV_BOOT_OFFSET (2 * sizeof (vdev_label_t)) 249789Sahrens #define VDEV_BOOT_SIZE (7ULL << 19) /* 3.5M */ 250789Sahrens 251789Sahrens /* 252789Sahrens * Size of label regions at the start and end of each leaf device. 253789Sahrens */ 254789Sahrens #define VDEV_LABEL_START_SIZE (2 * sizeof (vdev_label_t) + VDEV_BOOT_SIZE) 255789Sahrens #define VDEV_LABEL_END_SIZE (2 * sizeof (vdev_label_t)) 256789Sahrens #define VDEV_LABELS 4 257789Sahrens 258789Sahrens #define VDEV_ALLOC_LOAD 0 259789Sahrens #define VDEV_ALLOC_ADD 1 2602082Seschrock #define VDEV_ALLOC_SPARE 2 2615450Sbrendan #define VDEV_ALLOC_L2CACHE 3 2629790SLin.Ling@Sun.COM #define VDEV_ALLOC_ROOTPOOL 4 26311422SMark.Musante@Sun.COM #define VDEV_ALLOC_SPLIT 5 264789Sahrens 265789Sahrens /* 266789Sahrens * Allocate or free a vdev 267789Sahrens */ 26810594SGeorge.Wilson@Sun.COM extern vdev_t *vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, 26910594SGeorge.Wilson@Sun.COM vdev_ops_t *ops); 2702082Seschrock extern int vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *config, 2712082Seschrock vdev_t *parent, uint_t id, int alloctype); 272789Sahrens extern void vdev_free(vdev_t *vd); 273789Sahrens 274789Sahrens /* 275789Sahrens * Add or remove children and parents 276789Sahrens */ 277789Sahrens extern void vdev_add_child(vdev_t *pvd, vdev_t *cvd); 278789Sahrens extern void vdev_remove_child(vdev_t *pvd, vdev_t *cvd); 279789Sahrens extern void vdev_compact_children(vdev_t *pvd); 280789Sahrens extern vdev_t *vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops); 281789Sahrens extern void vdev_remove_parent(vdev_t *cvd); 282789Sahrens 283789Sahrens /* 284789Sahrens * vdev sync load and sync 285789Sahrens */ 28610594SGeorge.Wilson@Sun.COM extern void vdev_load_log_state(vdev_t *nvd, vdev_t *ovd); 28712949SGeorge.Wilson@Sun.COM extern boolean_t vdev_log_state_valid(vdev_t *vd); 2881986Seschrock extern void vdev_load(vdev_t *vd); 289789Sahrens extern void vdev_sync(vdev_t *vd, uint64_t txg); 290789Sahrens extern void vdev_sync_done(vdev_t *vd, uint64_t txg); 2911732Sbonwick extern void vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg); 292789Sahrens 293789Sahrens /* 294789Sahrens * Available vdev types. 295789Sahrens */ 296789Sahrens extern vdev_ops_t vdev_root_ops; 297789Sahrens extern vdev_ops_t vdev_mirror_ops; 298789Sahrens extern vdev_ops_t vdev_replacing_ops; 299789Sahrens extern vdev_ops_t vdev_raidz_ops; 300789Sahrens extern vdev_ops_t vdev_disk_ops; 301789Sahrens extern vdev_ops_t vdev_file_ops; 302789Sahrens extern vdev_ops_t vdev_missing_ops; 30310594SGeorge.Wilson@Sun.COM extern vdev_ops_t vdev_hole_ops; 3042082Seschrock extern vdev_ops_t vdev_spare_ops; 305789Sahrens 306789Sahrens /* 3071175Slling * Common size functions 308789Sahrens */ 309789Sahrens extern uint64_t vdev_default_asize(vdev_t *vd, uint64_t psize); 3109816SGeorge.Wilson@Sun.COM extern uint64_t vdev_get_min_asize(vdev_t *vd); 3119816SGeorge.Wilson@Sun.COM extern void vdev_set_min_asize(vdev_t *vd); 312789Sahrens 3133060Sahrens /* 3143060Sahrens * zdb uses this tunable, so it must be declared here to make lint happy. 3153060Sahrens */ 3163060Sahrens extern int zfs_vdev_cache_size; 3173060Sahrens 318789Sahrens #ifdef __cplusplus 319789Sahrens } 320789Sahrens #endif 321789Sahrens 322789Sahrens #endif /* _SYS_VDEV_IMPL_H */ 323