1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  23  * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
  24  * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
  25  */
  26 
  27 #ifndef _SYS_VDEV_IMPL_H
  28 #define _SYS_VDEV_IMPL_H
  29 
  30 #include <sys/avl.h>
  31 #include <sys/dmu.h>
  32 #include <sys/metaslab.h>
  33 #include <sys/nvpair.h>
  34 #include <sys/space_map.h>
  35 #include <sys/vdev.h>
  36 #include <sys/dkio.h>
  37 #include <sys/uberblock_impl.h>
  38 #include <sys/fs/zfs.h>
  39 #include <sys/cos.h>
  40 
  41 #ifdef  __cplusplus
  42 extern "C" {
  43 #endif
  44 
  45 /*
  46  * Virtual device descriptors.
  47  *
  48  * All storage pool operations go through the virtual device framework,
  49  * which provides data replication and I/O scheduling.
  50  */
  51 
  52 /*
  53  * Forward declarations that lots of things need.
  54  */
  55 typedef struct vdev_queue vdev_queue_t;
  56 typedef struct vdev_cache vdev_cache_t;
  57 typedef struct vdev_cache_entry vdev_cache_entry_t;
  58 struct abd;
  59 
  60 extern int zfs_vdev_queue_depth_pct;
  61 extern uint32_t zfs_vdev_async_write_max_active;
  62 
  63 /*
  64  * Virtual device operations
  65  */
  66 typedef int     vdev_open_func_t(vdev_t *vd, uint64_t *size, uint64_t *max_size,
  67     uint64_t *ashift);
  68 typedef void    vdev_close_func_t(vdev_t *vd);
  69 typedef uint64_t vdev_asize_func_t(vdev_t *vd, uint64_t psize);
  70 typedef void    vdev_io_start_func_t(zio_t *zio);
  71 typedef void    vdev_io_done_func_t(zio_t *zio);
  72 typedef void    vdev_state_change_func_t(vdev_t *vd, int, int);
  73 typedef void    vdev_hold_func_t(vdev_t *vd);
  74 typedef void    vdev_rele_func_t(vdev_t *vd);
  75 typedef void    vdev_trim_func_t(vdev_t *vd, zio_t *pio, void *trim_exts);
  76 
  77 typedef struct vdev_ops {
  78         vdev_open_func_t                *vdev_op_open;
  79         vdev_close_func_t               *vdev_op_close;
  80         vdev_asize_func_t               *vdev_op_asize;
  81         vdev_io_start_func_t            *vdev_op_io_start;
  82         vdev_io_done_func_t             *vdev_op_io_done;
  83         vdev_state_change_func_t        *vdev_op_state_change;
  84         vdev_hold_func_t                *vdev_op_hold;
  85         vdev_rele_func_t                *vdev_op_rele;
  86         vdev_trim_func_t                *vdev_op_trim;
  87         char                            vdev_op_type[16];
  88         boolean_t                       vdev_op_leaf;
  89 } vdev_ops_t;
  90 
  91 /*
  92  * Virtual device properties
  93  */
  94 struct vdev_cache_entry {
  95         struct abd      *ve_abd;
  96         uint64_t        ve_offset;
  97         uint64_t        ve_lastused;
  98         avl_node_t      ve_offset_node;
  99         avl_node_t      ve_lastused_node;
 100         uint32_t        ve_hits;
 101         uint16_t        ve_missed_update;
 102         zio_t           *ve_fill_io;
 103 };
 104 
 105 struct vdev_cache {
 106         avl_tree_t      vc_offset_tree;
 107         avl_tree_t      vc_lastused_tree;
 108         kmutex_t        vc_lock;
 109 };
 110 
 111 /*
 112  * Macros for conversion between zio priorities and vdev properties.
 113  * These rely on the specific corresponding order of the zio_priority_t
 114  * and vdev_prop_t enum definitions to simplify the conversion.
 115  */
 116 #define VDEV_PROP_TO_ZIO_PRIO_MIN(prp)  ((prp) - VDEV_PROP_READ_MINACTIVE)
 117 #define VDEV_ZIO_PRIO_TO_PROP_MIN(pri)  ((pri) + VDEV_PROP_READ_MINACTIVE)
 118 #define VDEV_PROP_MIN_VALID(prp)                \
 119         (((prp) >= VDEV_PROP_READ_MINACTIVE) &&      \
 120         ((prp) <= VDEV_PROP_SCRUB_MINACTIVE))
 121 #define VDEV_PROP_TO_ZIO_PRIO_MAX(prp)  ((prp) - VDEV_PROP_READ_MAXACTIVE)
 122 #define VDEV_ZIO_PRIO_TO_PROP_MAX(pri)  ((pri) + VDEV_PROP_READ_MAXACTIVE)
 123 #define VDEV_PROP_MAX_VALID(prp)                \
 124         (((prp) >= VDEV_PROP_READ_MAXACTIVE) &&      \
 125         ((prp) <= VDEV_PROP_SCRUB_MAXACTIVE))
 126 
 127 typedef struct vdev_queue_class {
 128         uint32_t        vqc_active;
 129 
 130         /*
 131          * If min/max active values are zero, we fall back on the global
 132          * corresponding tunables defined in vdev_queue.c; non-zero values
 133          * override the global tunables
 134          */
 135         uint32_t        vqc_min_active; /* min concurently active IOs */
 136         uint32_t        vqc_max_active; /* max concurently active IOs */
 137 
 138         /*
 139          * Sorted by offset or timestamp, depending on if the queue is
 140          * LBA-ordered vs FIFO.
 141          */
 142         avl_tree_t      vqc_queued_tree;
 143 } vdev_queue_class_t;
 144 
 145 struct vdev_queue {
 146         vdev_t          *vq_vdev;
 147 
 148         vdev_queue_class_t vq_class[ZIO_PRIORITY_NUM_QUEUEABLE];
 149         cos_t           *vq_cos;                /* assigned class of storage */
 150         uint64_t        vq_preferred_read;      /* property setting */
 151 
 152         avl_tree_t      vq_active_tree;
 153         avl_tree_t      vq_read_offset_tree;
 154         avl_tree_t      vq_write_offset_tree;
 155         uint64_t        vq_last_offset;
 156         hrtime_t        vq_io_complete_ts;      /* time last i/o completed */
 157         kmutex_t        vq_lock;
 158 };
 159 
 160 /*
 161  * vdev auxiliary kstat I/O statistics:
 162  * updates every spa_special_stat_update_ticks interval
 163  * it is used for adjust special vs normal data routing
 164  */
 165 typedef struct vdev_aux_stat {
 166         uint64_t nread;         /* number of bytes read */
 167         uint64_t nwritten;      /* number of bytes written */
 168         uint64_t reads;         /* number of read operations */
 169         uint64_t writes;        /* number of write operations */
 170         uint64_t rtime;         /* cumulative run (service) time */
 171         uint64_t wtime;         /* cumulative wait (pre-service) time */
 172         uint64_t rlentime;      /* cumulative run length*time product */
 173         uint64_t wlentime;      /* cumulative wait length*time product */
 174         uint64_t rlastupdate;   /* last time run queue changed */
 175         uint64_t wlastupdate;   /* last time wait queue changed */
 176         uint64_t rcnt;          /* count of elements in run state */
 177         uint64_t wcnt;          /* count of elements in wait state */
 178 } vdev_aux_stat_t;
 179 
 180 /*
 181  * Virtual device descriptor
 182  */
 183 struct vdev {
 184         /*
 185          * Common to all vdev types.
 186          */
 187         uint64_t        vdev_id;        /* child number in vdev parent  */
 188         uint64_t        vdev_guid;      /* unique ID for this vdev      */
 189         uint64_t        vdev_guid_sum;  /* self guid + all child guids  */
 190         uint64_t        vdev_orig_guid; /* orig. guid prior to remove   */
 191         uint64_t        vdev_asize;     /* allocatable device capacity  */
 192         uint64_t        vdev_min_asize; /* min acceptable asize         */
 193         uint64_t        vdev_max_asize; /* max acceptable asize         */
 194         uint64_t        vdev_ashift;    /* block alignment shift        */
 195         uint64_t        vdev_state;     /* see VDEV_STATE_* #defines    */
 196         uint64_t        vdev_prevstate; /* used when reopening a vdev   */
 197         vdev_ops_t      *vdev_ops;      /* vdev operations              */
 198         spa_t           *vdev_spa;      /* spa for this vdev            */
 199         void            *vdev_tsd;      /* type-specific data           */
 200         vnode_t         *vdev_name_vp;  /* vnode for pathname           */
 201         vnode_t         *vdev_devid_vp; /* vnode for devid              */
 202         vdev_t          *vdev_top;      /* top-level vdev               */
 203         vdev_t          *vdev_parent;   /* parent vdev                  */
 204         vdev_t          **vdev_child;   /* array of children            */
 205         uint64_t        vdev_children;  /* number of children           */
 206         vdev_stat_t     vdev_stat;      /* virtual device statistics    */
 207         boolean_t       vdev_expanding; /* expand the vdev?             */
 208         boolean_t       vdev_reopening; /* reopen in progress?          */
 209         int             vdev_open_error; /* error on last open          */
 210         kthread_t       *vdev_open_thread; /* thread opening children   */
 211         uint64_t        vdev_crtxg;     /* txg when top-level was added */
 212 
 213         /*
 214          * Top-level vdev state.
 215          */
 216         uint64_t        vdev_ms_array;  /* metaslab array object        */
 217         uint64_t        vdev_ms_shift;  /* metaslab size shift          */
 218         uint64_t        vdev_ms_count;  /* number of metaslabs          */
 219         metaslab_group_t *vdev_mg;      /* metaslab group               */
 220         metaslab_t      **vdev_ms;      /* metaslab array               */
 221         txg_list_t      vdev_ms_list;   /* per-txg dirty metaslab lists */
 222         txg_list_t      vdev_dtl_list;  /* per-txg dirty DTL lists      */
 223         txg_node_t      vdev_txg_node;  /* per-txg dirty vdev linkage   */
 224         boolean_t       vdev_remove_wanted; /* async remove wanted?     */
 225         boolean_t       vdev_probe_wanted; /* async probe wanted?       */
 226         list_node_t     vdev_config_dirty_node; /* config dirty list    */
 227         list_node_t     vdev_state_dirty_node; /* state dirty list      */
 228         uint64_t        vdev_deflate_ratio; /* deflation ratio (x512)   */
 229         uint64_t        vdev_islog;     /* is an intent log device      */
 230         uint64_t        vdev_removing;  /* device is being removed?     */
 231         boolean_t       vdev_ishole;    /* is a hole in the namespace   */
 232         uint64_t        vdev_top_zap;
 233         uint64_t        vdev_isspecial; /* is a special device  */
 234 
 235         boolean_t       vdev_man_trimming; /* manual trim is ongoing    */
 236         uint64_t        vdev_trim_prog; /* trim progress in bytes       */
 237 
 238         /*
 239          * Protects the vdev_scan_io_queue field itself as well as the
 240          * structure's contents (when present). The scn_status_lock in
 241          * dsl_scan_t must only ever be acquired stand-alone or inside
 242          * of vdev_scan_queue_lock, never in reverse.
 243          */
 244         kmutex_t                        vdev_scan_io_queue_lock;
 245         struct dsl_scan_io_queue        *vdev_scan_io_queue;
 246 
 247         /*
 248          * The queue depth parameters determine how many async writes are
 249          * still pending (i.e. allocated by net yet issued to disk) per
 250          * top-level (vdev_async_write_queue_depth) and the maximum allowed
 251          * (vdev_max_async_write_queue_depth). These values only apply to
 252          * top-level vdevs.
 253          */
 254         uint64_t        vdev_async_write_queue_depth;
 255         uint64_t        vdev_max_async_write_queue_depth;
 256 
 257         /*
 258          * Leaf vdev state.
 259          */
 260         range_tree_t    *vdev_dtl[DTL_TYPES]; /* dirty time logs        */
 261         space_map_t     *vdev_dtl_sm;   /* dirty time log space map     */
 262         txg_node_t      vdev_dtl_node;  /* per-txg dirty DTL linkage    */
 263         uint64_t        vdev_dtl_object; /* DTL object                  */
 264         uint64_t        vdev_psize;     /* physical device capacity     */
 265         uint64_t        vdev_wholedisk; /* true if this is a whole disk */
 266         uint64_t        vdev_offline;   /* persistent offline state     */
 267         uint64_t        vdev_faulted;   /* persistent faulted state     */
 268         uint64_t        vdev_degraded;  /* persistent degraded state    */
 269         uint64_t        vdev_removed;   /* persistent removed state     */
 270         uint64_t        vdev_resilver_txg; /* persistent resilvering state */
 271         uint64_t        vdev_nparity;   /* number of parity devices for raidz */
 272         uint64_t        vdev_l2ad_ddt;  /* L2ARC vdev is used to cache DDT */
 273         char            *vdev_path;     /* vdev path (if any)           */
 274         char            *vdev_devid;    /* vdev devid (if any)          */
 275         char            *vdev_physpath; /* vdev device path (if any)    */
 276         char            *vdev_fru;      /* physical FRU location        */
 277         int64_t         vdev_weight;    /* dynamic weight */
 278         uint64_t        vdev_not_present; /* not present during import  */
 279         uint64_t        vdev_unspare;   /* unspare when resilvering done */
 280         boolean_t       vdev_nowritecache; /* true if flushwritecache failed */
 281         boolean_t       vdev_notrim;    /* true if Unmap/TRIM is unsupported */
 282         boolean_t       vdev_checkremove; /* temporary online test      */
 283         boolean_t       vdev_forcefault; /* force online fault          */
 284         boolean_t       vdev_splitting; /* split or repair in progress  */
 285         boolean_t       vdev_delayed_close; /* delayed device close?    */
 286         boolean_t       vdev_tmpoffline; /* device taken offline temporarily? */
 287         boolean_t       vdev_detached;  /* device detached?             */
 288         boolean_t       vdev_cant_read; /* vdev is failing all reads    */
 289         boolean_t       vdev_cant_write; /* vdev is failing all writes  */
 290         boolean_t       vdev_isspare;   /* was a hot spare              */
 291         boolean_t       vdev_isl2cache; /* was a l2cache device         */
 292         boolean_t       vdev_isspecial_child; /* a child of top-level special */
 293         vdev_queue_t    vdev_queue;     /* I/O deadline schedule queue  */
 294         vdev_cache_t    vdev_cache;     /* physical block cache         */
 295         spa_aux_vdev_t  *vdev_aux;      /* for l2cache and spares vdevs */
 296         zio_t           *vdev_probe_zio; /* root of current probe       */
 297         vdev_aux_t      vdev_label_aux; /* on-disk aux state            */
 298         uint64_t        vdev_leaf_zap;
 299         boolean_t       vdev_is_ssd;    /* is solid state device        */
 300 
 301         char            *vdev_spare_group; /* spare group name */
 302 
 303         struct kstat    *vdev_iokstat; /* vdev kstat I/O statistics */
 304         vdev_aux_stat_t vdev_aux_stat; /* auxiliary vdev kstat I/O statistics */
 305         /*
 306          * For DTrace to work in userland (libzpool) context, these fields must
 307          * remain at the end of the structure.  DTrace will use the kernel's
 308          * CTF definition for 'struct vdev', and since the size of a kmutex_t is
 309          * larger in userland, the offsets for the rest of the fields would be
 310          * incorrect.
 311          */
 312         kmutex_t        vdev_dtl_lock;  /* vdev_dtl_{map,resilver}      */
 313         kmutex_t        vdev_stat_lock; /* vdev_stat                    */
 314         kmutex_t        vdev_probe_lock; /* protects vdev_probe_zio     */
 315         krwlock_t       vdev_tsd_lock;  /* protects vdev_tsd */
 316 };
 317 
 318 #define VDEV_RAIDZ_MAXPARITY    3
 319 
 320 #define VDEV_PAD_SIZE           (8 << 10)
 321 /* 2 padding areas (vl_pad1 and vl_pad2) to skip */
 322 #define VDEV_SKIP_SIZE          VDEV_PAD_SIZE * 2
 323 #define VDEV_PHYS_SIZE          (112 << 10)
 324 #define VDEV_UBERBLOCK_RING     (128 << 10)
 325 
 326 /* The largest uberblock we support is 8k. */
 327 #define MAX_UBERBLOCK_SHIFT (13)
 328 #define VDEV_UBERBLOCK_SHIFT(vd)        \
 329         MIN(MAX((vd)->vdev_top->vdev_ashift, UBERBLOCK_SHIFT), \
 330             MAX_UBERBLOCK_SHIFT)
 331 #define VDEV_UBERBLOCK_COUNT(vd)        \
 332         (VDEV_UBERBLOCK_RING >> VDEV_UBERBLOCK_SHIFT(vd))
 333 #define VDEV_UBERBLOCK_OFFSET(vd, n)    \
 334         offsetof(vdev_label_t, vl_uberblock[(n) << VDEV_UBERBLOCK_SHIFT(vd)])
 335 #define VDEV_UBERBLOCK_SIZE(vd)         (1ULL << VDEV_UBERBLOCK_SHIFT(vd))
 336 
 337 typedef struct vdev_phys {
 338         char            vp_nvlist[VDEV_PHYS_SIZE - sizeof (zio_eck_t)];
 339         zio_eck_t       vp_zbt;
 340 } vdev_phys_t;
 341 
 342 typedef struct vdev_label {
 343         char            vl_pad1[VDEV_PAD_SIZE];                 /*  8K */
 344         char            vl_pad2[VDEV_PAD_SIZE];                 /*  8K */
 345         vdev_phys_t     vl_vdev_phys;                           /* 112K */
 346         char            vl_uberblock[VDEV_UBERBLOCK_RING];      /* 128K */
 347 } vdev_label_t;                                                 /* 256K total */
 348 
 349 /*
 350  * vdev_dirty() flags
 351  */
 352 #define VDD_METASLAB    0x01
 353 #define VDD_DTL         0x02
 354 
 355 /* Offset of embedded boot loader region on each label */
 356 #define VDEV_BOOT_OFFSET        (2 * sizeof (vdev_label_t))
 357 /*
 358  * Size of embedded boot loader region on each label.
 359  * The total size of the first two labels plus the boot area is 4MB.
 360  */
 361 #define VDEV_BOOT_SIZE          (7ULL << 19)                      /* 3.5M */
 362 
 363 /*
 364  * Size of label regions at the start and end of each leaf device.
 365  */
 366 #define VDEV_LABEL_START_SIZE   (2 * sizeof (vdev_label_t) + VDEV_BOOT_SIZE)
 367 #define VDEV_LABEL_END_SIZE     (2 * sizeof (vdev_label_t))
 368 #define VDEV_LABELS             4
 369 #define VDEV_BEST_LABEL         VDEV_LABELS
 370 
 371 #define VDEV_ALLOC_LOAD         0
 372 #define VDEV_ALLOC_ADD          1
 373 #define VDEV_ALLOC_SPARE        2
 374 #define VDEV_ALLOC_L2CACHE      3
 375 #define VDEV_ALLOC_ROOTPOOL     4
 376 #define VDEV_ALLOC_SPLIT        5
 377 #define VDEV_ALLOC_ATTACH       6
 378 
 379 /*
 380  * Allocate or free a vdev
 381  */
 382 extern vdev_t *vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid,
 383     vdev_ops_t *ops);
 384 extern int vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *config,
 385     vdev_t *parent, uint_t id, int alloctype);
 386 extern void vdev_free(vdev_t *vd);
 387 
 388 /*
 389  * Add or remove children and parents
 390  */
 391 extern void vdev_add_child(vdev_t *pvd, vdev_t *cvd);
 392 extern void vdev_remove_child(vdev_t *pvd, vdev_t *cvd);
 393 extern void vdev_compact_children(vdev_t *pvd);
 394 extern vdev_t *vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops);
 395 extern void vdev_remove_parent(vdev_t *cvd);
 396 
 397 /*
 398  * vdev sync load and sync
 399  */
 400 extern void vdev_load_log_state(vdev_t *nvd, vdev_t *ovd);
 401 extern boolean_t vdev_log_state_valid(vdev_t *vd);
 402 extern void vdev_load(vdev_t *vd);
 403 extern int vdev_dtl_load(vdev_t *vd);
 404 extern void vdev_sync(vdev_t *vd, uint64_t txg);
 405 extern void vdev_sync_done(vdev_t *vd, uint64_t txg);
 406 extern void vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg);
 407 extern void vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg);
 408 
 409 /*
 410  * Available vdev types.
 411  */
 412 extern vdev_ops_t vdev_root_ops;
 413 extern vdev_ops_t vdev_mirror_ops;
 414 extern vdev_ops_t vdev_replacing_ops;
 415 extern vdev_ops_t vdev_raidz_ops;
 416 extern vdev_ops_t vdev_disk_ops;
 417 extern vdev_ops_t vdev_file_ops;
 418 extern vdev_ops_t vdev_missing_ops;
 419 extern vdev_ops_t vdev_hole_ops;
 420 extern vdev_ops_t vdev_spare_ops;
 421 
 422 extern uint_t vdev_count_leaf_vdevs(vdev_t *root);
 423 
 424 /*
 425  * Common size functions
 426  */
 427 extern uint64_t vdev_default_asize(vdev_t *vd, uint64_t psize);
 428 extern uint64_t vdev_get_min_asize(vdev_t *vd);
 429 extern void vdev_set_min_asize(vdev_t *vd);
 430 
 431 
 432 /*
 433  * Wrapper for getting vdev-specific properties that enforces proper
 434  * overriding: vdev-specific properties override CoS properties
 435  *
 436  * The value of 0 indicates that the property is not set (default).
 437  */
 438 extern uint64_t vdev_queue_get_prop_uint64(vdev_queue_t *vq, vdev_prop_t prop);
 439 
 440 /*
 441  * Global variables
 442  */
 443 /* zdb uses this tunable, so it must be declared here to make lint happy. */
 444 extern int zfs_vdev_cache_size;
 445 
 446 /*
 447  * The vdev_buf_t is used to translate between zio_t and buf_t, and back again.
 448  */
 449 typedef struct vdev_buf {
 450         buf_t   vb_buf;         /* buffer that describes the io */
 451         zio_t   *vb_io;         /* pointer back to the original zio_t */
 452 } vdev_buf_t;
 453 
 454 #ifdef  __cplusplus
 455 }
 456 #endif
 457 
 458 #endif  /* _SYS_VDEV_IMPL_H */