1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5441d80aaSlling * Common Development and Distribution License (the "License"). 6441d80aaSlling * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 2205b2b3b8Smishra * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23fa9e4066Sahrens * Use is subject to license terms. 24fa9e4066Sahrens */ 25fa9e4066Sahrens 26fa9e4066Sahrens #ifndef _SYS_VDEV_IMPL_H 27fa9e4066Sahrens #define _SYS_VDEV_IMPL_H 28fa9e4066Sahrens 29fa9e4066Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 30fa9e4066Sahrens 31fa9e4066Sahrens #include <sys/avl.h> 32fa9e4066Sahrens #include <sys/dmu.h> 33fa9e4066Sahrens #include <sys/metaslab.h> 34fa9e4066Sahrens #include <sys/nvpair.h> 35fa9e4066Sahrens #include <sys/space_map.h> 36fa9e4066Sahrens #include <sys/vdev.h> 37fa9e4066Sahrens #include <sys/dkio.h> 38fa9e4066Sahrens #include <sys/uberblock_impl.h> 39fa9e4066Sahrens 40fa9e4066Sahrens #ifdef __cplusplus 41fa9e4066Sahrens extern "C" { 42fa9e4066Sahrens #endif 43fa9e4066Sahrens 44fa9e4066Sahrens /* 45fa9e4066Sahrens * Virtual device descriptors. 46fa9e4066Sahrens * 47fa9e4066Sahrens * All storage pool operations go through the virtual device framework, 48fa9e4066Sahrens * which provides data replication and I/O scheduling. 49fa9e4066Sahrens */ 50fa9e4066Sahrens 51fa9e4066Sahrens /* 52fa9e4066Sahrens * Forward declarations that lots of things need. 53fa9e4066Sahrens */ 54fa9e4066Sahrens typedef struct vdev_queue vdev_queue_t; 55fa9e4066Sahrens typedef struct vdev_cache vdev_cache_t; 56fa9e4066Sahrens typedef struct vdev_cache_entry vdev_cache_entry_t; 57fa9e4066Sahrens 58fa9e4066Sahrens /* 59fa9e4066Sahrens * Virtual device operations 60fa9e4066Sahrens */ 61fa9e4066Sahrens typedef int vdev_open_func_t(vdev_t *vd, uint64_t *size, uint64_t *ashift); 62fa9e4066Sahrens typedef void vdev_close_func_t(vdev_t *vd); 630a4e9518Sgw typedef int vdev_probe_func_t(vdev_t *vd); 64fa9e4066Sahrens typedef uint64_t vdev_asize_func_t(vdev_t *vd, uint64_t psize); 65fa9e4066Sahrens typedef void vdev_io_start_func_t(zio_t *zio); 66fa9e4066Sahrens typedef void vdev_io_done_func_t(zio_t *zio); 67fa9e4066Sahrens typedef void vdev_state_change_func_t(vdev_t *vd, int, int); 68fa9e4066Sahrens 69fa9e4066Sahrens typedef struct vdev_ops { 70fa9e4066Sahrens vdev_open_func_t *vdev_op_open; 71fa9e4066Sahrens vdev_close_func_t *vdev_op_close; 720a4e9518Sgw vdev_probe_func_t *vdev_op_probe; 73fa9e4066Sahrens vdev_asize_func_t *vdev_op_asize; 74fa9e4066Sahrens vdev_io_start_func_t *vdev_op_io_start; 75fa9e4066Sahrens vdev_io_done_func_t *vdev_op_io_done; 76fa9e4066Sahrens vdev_state_change_func_t *vdev_op_state_change; 77fa9e4066Sahrens char vdev_op_type[16]; 78fa9e4066Sahrens boolean_t vdev_op_leaf; 79fa9e4066Sahrens } vdev_ops_t; 80fa9e4066Sahrens 81fa9e4066Sahrens /* 82fa9e4066Sahrens * Virtual device properties 83fa9e4066Sahrens */ 84fa9e4066Sahrens struct vdev_cache_entry { 85fa9e4066Sahrens char *ve_data; 86fa9e4066Sahrens uint64_t ve_offset; 87fa9e4066Sahrens uint64_t ve_lastused; 88fa9e4066Sahrens avl_node_t ve_offset_node; 89fa9e4066Sahrens avl_node_t ve_lastused_node; 90fa9e4066Sahrens uint32_t ve_hits; 91fa9e4066Sahrens uint16_t ve_missed_update; 92fa9e4066Sahrens zio_t *ve_fill_io; 93fa9e4066Sahrens }; 94fa9e4066Sahrens 95fa9e4066Sahrens struct vdev_cache { 96fa9e4066Sahrens avl_tree_t vc_offset_tree; 97fa9e4066Sahrens avl_tree_t vc_lastused_tree; 98fa9e4066Sahrens kmutex_t vc_lock; 99fa9e4066Sahrens }; 100fa9e4066Sahrens 101fa9e4066Sahrens struct vdev_queue { 102fa9e4066Sahrens avl_tree_t vq_deadline_tree; 103fa9e4066Sahrens avl_tree_t vq_read_tree; 104fa9e4066Sahrens avl_tree_t vq_write_tree; 105fa9e4066Sahrens avl_tree_t vq_pending_tree; 106fa9e4066Sahrens kmutex_t vq_lock; 107fa9e4066Sahrens }; 108fa9e4066Sahrens 109fa9e4066Sahrens /* 110fa9e4066Sahrens * Virtual device descriptor 111fa9e4066Sahrens */ 112fa9e4066Sahrens struct vdev { 113fa9e4066Sahrens /* 114fa9e4066Sahrens * Common to all vdev types. 115fa9e4066Sahrens */ 116fa9e4066Sahrens uint64_t vdev_id; /* child number in vdev parent */ 117fa9e4066Sahrens uint64_t vdev_guid; /* unique ID for this vdev */ 118fa9e4066Sahrens uint64_t vdev_guid_sum; /* self guid + all child guids */ 119fa9e4066Sahrens uint64_t vdev_asize; /* allocatable device capacity */ 120fa9e4066Sahrens uint64_t vdev_ashift; /* block alignment shift */ 121fa9e4066Sahrens uint64_t vdev_state; /* see VDEV_STATE_* #defines */ 122560e6e96Seschrock uint64_t vdev_prevstate; /* used when reopening a vdev */ 123fa9e4066Sahrens vdev_ops_t *vdev_ops; /* vdev operations */ 124fa9e4066Sahrens spa_t *vdev_spa; /* spa for this vdev */ 125fa9e4066Sahrens void *vdev_tsd; /* type-specific data */ 126fa9e4066Sahrens vdev_t *vdev_top; /* top-level vdev */ 127fa9e4066Sahrens vdev_t *vdev_parent; /* parent vdev */ 128fa9e4066Sahrens vdev_t **vdev_child; /* array of children */ 129fa9e4066Sahrens uint64_t vdev_children; /* number of children */ 130fa9e4066Sahrens space_map_t vdev_dtl_map; /* dirty time log in-core state */ 131fa9e4066Sahrens space_map_t vdev_dtl_scrub; /* DTL for scrub repair writes */ 132fa9e4066Sahrens vdev_stat_t vdev_stat; /* virtual device statistics */ 133fa9e4066Sahrens 134fa9e4066Sahrens /* 135fa9e4066Sahrens * Top-level vdev state. 136fa9e4066Sahrens */ 137fa9e4066Sahrens uint64_t vdev_ms_array; /* metaslab array object */ 138fa9e4066Sahrens uint64_t vdev_ms_shift; /* metaslab size shift */ 139fa9e4066Sahrens uint64_t vdev_ms_count; /* number of metaslabs */ 140fa9e4066Sahrens metaslab_group_t *vdev_mg; /* metaslab group */ 141fa9e4066Sahrens metaslab_t **vdev_ms; /* metaslab array */ 142fa9e4066Sahrens txg_list_t vdev_ms_list; /* per-txg dirty metaslab lists */ 143fa9e4066Sahrens txg_list_t vdev_dtl_list; /* per-txg dirty DTL lists */ 144fa9e4066Sahrens txg_node_t vdev_txg_node; /* per-txg dirty vdev linkage */ 1453d7072f8Seschrock boolean_t vdev_remove_wanted; /* async remove wanted? */ 146fa9e4066Sahrens list_node_t vdev_dirty_node; /* config dirty list */ 14799653d4eSeschrock uint64_t vdev_deflate_ratio; /* deflation ratio (x512) */ 1488654d025Sperrin uint64_t vdev_islog; /* is an intent log device */ 149fa9e4066Sahrens 150fa9e4066Sahrens /* 151fa9e4066Sahrens * Leaf vdev state. 152fa9e4066Sahrens */ 153fa9e4066Sahrens uint64_t vdev_psize; /* physical device capacity */ 154fa9e4066Sahrens space_map_obj_t vdev_dtl; /* dirty time log on-disk state */ 155fa9e4066Sahrens txg_node_t vdev_dtl_node; /* per-txg dirty DTL linkage */ 156afefbcddSeschrock uint64_t vdev_wholedisk; /* true if this is a whole disk */ 1573d7072f8Seschrock uint64_t vdev_offline; /* persistent offline state */ 1583d7072f8Seschrock uint64_t vdev_faulted; /* persistent faulted state */ 1593d7072f8Seschrock uint64_t vdev_degraded; /* persistent degraded state */ 1603d7072f8Seschrock uint64_t vdev_removed; /* persistent removed state */ 16199653d4eSeschrock uint64_t vdev_nparity; /* number of parity devices for raidz */ 162fa9e4066Sahrens char *vdev_path; /* vdev path (if any) */ 163fa9e4066Sahrens char *vdev_devid; /* vdev devid (if any) */ 1643d7072f8Seschrock char *vdev_physpath; /* vdev device path (if any) */ 165fa9e4066Sahrens uint64_t vdev_fault_arg; /* fault injection paramater */ 166fa9e4066Sahrens int vdev_fault_mask; /* zio types to fault */ 167fa9e4066Sahrens uint8_t vdev_fault_mode; /* fault injection mode */ 168441d80aaSlling uint8_t vdev_tmpoffline; /* device taken offline temporarily? */ 169fa9e4066Sahrens uint8_t vdev_detached; /* device detached? */ 1708654d025Sperrin uint64_t vdev_isspare; /* was a hot spare */ 171*fa94a07fSbrendan uint64_t vdev_isl2cache; /* was a l2cache device */ 172fa9e4066Sahrens vdev_queue_t vdev_queue; /* I/O deadline schedule queue */ 173fa9e4066Sahrens vdev_cache_t vdev_cache; /* physical block cache */ 174ea8dc4b6Seschrock uint64_t vdev_not_present; /* not present during import */ 175ea8dc4b6Seschrock hrtime_t vdev_last_try; /* last reopen time */ 176b468a217Seschrock boolean_t vdev_nowritecache; /* true if flushwritecache failed */ 1773d7072f8Seschrock uint64_t vdev_unspare; /* unspare when resilvering done */ 1783d7072f8Seschrock boolean_t vdev_checkremove; /* temporary online test */ 1793d7072f8Seschrock boolean_t vdev_forcefault; /* force online fault */ 1800a4e9518Sgw boolean_t vdev_is_failing; /* device errors seen */ 181fa9e4066Sahrens 182fa9e4066Sahrens /* 183fa9e4066Sahrens * For DTrace to work in userland (libzpool) context, these fields must 184fa9e4066Sahrens * remain at the end of the structure. DTrace will use the kernel's 185fa9e4066Sahrens * CTF definition for 'struct vdev', and since the size of a kmutex_t is 186fa9e4066Sahrens * larger in userland, the offsets for the rest fields would be 187fa9e4066Sahrens * incorrect. 188fa9e4066Sahrens */ 189fa9e4066Sahrens kmutex_t vdev_dtl_lock; /* vdev_dtl_{map,resilver} */ 190fa9e4066Sahrens kmutex_t vdev_stat_lock; /* vdev_stat */ 191fa9e4066Sahrens }; 192fa9e4066Sahrens 193fa9e4066Sahrens #define VDEV_SKIP_SIZE (8 << 10) 194fa9e4066Sahrens #define VDEV_BOOT_HEADER_SIZE (8 << 10) 195fa9e4066Sahrens #define VDEV_PHYS_SIZE (112 << 10) 196ecc2d604Sbonwick #define VDEV_UBERBLOCK_RING (128 << 10) 197ecc2d604Sbonwick 198ecc2d604Sbonwick #define VDEV_UBERBLOCK_SHIFT(vd) \ 199ecc2d604Sbonwick MAX((vd)->vdev_top->vdev_ashift, UBERBLOCK_SHIFT) 200ecc2d604Sbonwick #define VDEV_UBERBLOCK_COUNT(vd) \ 201ecc2d604Sbonwick (VDEV_UBERBLOCK_RING >> VDEV_UBERBLOCK_SHIFT(vd)) 202ecc2d604Sbonwick #define VDEV_UBERBLOCK_OFFSET(vd, n) \ 203ecc2d604Sbonwick offsetof(vdev_label_t, vl_uberblock[(n) << VDEV_UBERBLOCK_SHIFT(vd)]) 204ecc2d604Sbonwick #define VDEV_UBERBLOCK_SIZE(vd) (1ULL << VDEV_UBERBLOCK_SHIFT(vd)) 205fa9e4066Sahrens 2065ad82045Snd /* ZFS boot block */ 2075ad82045Snd #define VDEV_BOOT_MAGIC 0x2f5b007b10cULL 208fa9e4066Sahrens #define VDEV_BOOT_VERSION 1 /* version number */ 209fa9e4066Sahrens 210fa9e4066Sahrens typedef struct vdev_boot_header { 211fa9e4066Sahrens uint64_t vb_magic; /* VDEV_BOOT_MAGIC */ 212fa9e4066Sahrens uint64_t vb_version; /* VDEV_BOOT_VERSION */ 213fa9e4066Sahrens uint64_t vb_offset; /* start offset (bytes) */ 214fa9e4066Sahrens uint64_t vb_size; /* size (bytes) */ 215fa9e4066Sahrens char vb_pad[VDEV_BOOT_HEADER_SIZE - 4 * sizeof (uint64_t)]; 216fa9e4066Sahrens } vdev_boot_header_t; 217fa9e4066Sahrens 218fa9e4066Sahrens typedef struct vdev_phys { 219fa9e4066Sahrens char vp_nvlist[VDEV_PHYS_SIZE - sizeof (zio_block_tail_t)]; 220fa9e4066Sahrens zio_block_tail_t vp_zbt; 221fa9e4066Sahrens } vdev_phys_t; 222fa9e4066Sahrens 223fa9e4066Sahrens typedef struct vdev_label { 224ecc2d604Sbonwick char vl_pad[VDEV_SKIP_SIZE]; /* 8K */ 225ecc2d604Sbonwick vdev_boot_header_t vl_boot_header; /* 8K */ 226ecc2d604Sbonwick vdev_phys_t vl_vdev_phys; /* 112K */ 227ecc2d604Sbonwick char vl_uberblock[VDEV_UBERBLOCK_RING]; /* 128K */ 228fa9e4066Sahrens } vdev_label_t; /* 256K total */ 229fa9e4066Sahrens 230ecc2d604Sbonwick /* 231ecc2d604Sbonwick * vdev_dirty() flags 232ecc2d604Sbonwick */ 233ecc2d604Sbonwick #define VDD_METASLAB 0x01 234ecc2d604Sbonwick #define VDD_DTL 0x02 235ecc2d604Sbonwick 236fa9e4066Sahrens /* 237fa9e4066Sahrens * Size and offset of embedded boot loader region on each label. 238fa9e4066Sahrens * The total size of the first two labels plus the boot area is 4MB. 239fa9e4066Sahrens */ 240fa9e4066Sahrens #define VDEV_BOOT_OFFSET (2 * sizeof (vdev_label_t)) 241fa9e4066Sahrens #define VDEV_BOOT_SIZE (7ULL << 19) /* 3.5M */ 242fa9e4066Sahrens 243fa9e4066Sahrens /* 244fa9e4066Sahrens * Size of label regions at the start and end of each leaf device. 245fa9e4066Sahrens */ 246fa9e4066Sahrens #define VDEV_LABEL_START_SIZE (2 * sizeof (vdev_label_t) + VDEV_BOOT_SIZE) 247fa9e4066Sahrens #define VDEV_LABEL_END_SIZE (2 * sizeof (vdev_label_t)) 248fa9e4066Sahrens #define VDEV_LABELS 4 249fa9e4066Sahrens 250fa9e4066Sahrens #define VDEV_ALLOC_LOAD 0 251fa9e4066Sahrens #define VDEV_ALLOC_ADD 1 25299653d4eSeschrock #define VDEV_ALLOC_SPARE 2 253*fa94a07fSbrendan #define VDEV_ALLOC_L2CACHE 3 254fa9e4066Sahrens 255fa9e4066Sahrens /* 256fa9e4066Sahrens * Allocate or free a vdev 257fa9e4066Sahrens */ 25899653d4eSeschrock extern int vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *config, 25999653d4eSeschrock vdev_t *parent, uint_t id, int alloctype); 260fa9e4066Sahrens extern void vdev_free(vdev_t *vd); 261fa9e4066Sahrens 262fa9e4066Sahrens /* 263fa9e4066Sahrens * Add or remove children and parents 264fa9e4066Sahrens */ 265fa9e4066Sahrens extern void vdev_add_child(vdev_t *pvd, vdev_t *cvd); 266fa9e4066Sahrens extern void vdev_remove_child(vdev_t *pvd, vdev_t *cvd); 267fa9e4066Sahrens extern void vdev_compact_children(vdev_t *pvd); 268fa9e4066Sahrens extern vdev_t *vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops); 269fa9e4066Sahrens extern void vdev_remove_parent(vdev_t *cvd); 270fa9e4066Sahrens 271fa9e4066Sahrens /* 272fa9e4066Sahrens * vdev sync load and sync 273fa9e4066Sahrens */ 274560e6e96Seschrock extern void vdev_load(vdev_t *vd); 275fa9e4066Sahrens extern void vdev_sync(vdev_t *vd, uint64_t txg); 276fa9e4066Sahrens extern void vdev_sync_done(vdev_t *vd, uint64_t txg); 277ecc2d604Sbonwick extern void vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg); 278fa9e4066Sahrens 279fa9e4066Sahrens /* 280fa9e4066Sahrens * Available vdev types. 281fa9e4066Sahrens */ 282fa9e4066Sahrens extern vdev_ops_t vdev_root_ops; 283fa9e4066Sahrens extern vdev_ops_t vdev_mirror_ops; 284fa9e4066Sahrens extern vdev_ops_t vdev_replacing_ops; 285fa9e4066Sahrens extern vdev_ops_t vdev_raidz_ops; 286fa9e4066Sahrens extern vdev_ops_t vdev_disk_ops; 287fa9e4066Sahrens extern vdev_ops_t vdev_file_ops; 288fa9e4066Sahrens extern vdev_ops_t vdev_missing_ops; 28999653d4eSeschrock extern vdev_ops_t vdev_spare_ops; 290fa9e4066Sahrens 291fa9e4066Sahrens /* 2922a79c5feSlling * Common size functions 293fa9e4066Sahrens */ 294fa9e4066Sahrens extern uint64_t vdev_default_asize(vdev_t *vd, uint64_t psize); 2952a79c5feSlling extern uint64_t vdev_get_rsize(vdev_t *vd); 296fa9e4066Sahrens 297e81c57b1Sahrens /* 298e81c57b1Sahrens * zdb uses this tunable, so it must be declared here to make lint happy. 299e81c57b1Sahrens */ 300e81c57b1Sahrens extern int zfs_vdev_cache_size; 301e81c57b1Sahrens 302fa9e4066Sahrens #ifdef __cplusplus 303fa9e4066Sahrens } 304fa9e4066Sahrens #endif 305fa9e4066Sahrens 306fa9e4066Sahrens #endif /* _SYS_VDEV_IMPL_H */ 307