1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright (C) 2018 Intel Corporation. 3 * All rights reserved. 4 */ 5 6 #ifndef SPDK_BDEV_RAID_INTERNAL_H 7 #define SPDK_BDEV_RAID_INTERNAL_H 8 9 #include "spdk/bdev_module.h" 10 #include "spdk/uuid.h" 11 12 #define RAID_BDEV_MIN_DATA_OFFSET_SIZE (1024*1024) /* 1 MiB */ 13 14 enum raid_level { 15 INVALID_RAID_LEVEL = -1, 16 RAID0 = 0, 17 RAID1 = 1, 18 RAID5F = 95, /* 0x5f */ 19 CONCAT = 99, 20 }; 21 22 /* 23 * Raid state describes the state of the raid. This raid bdev can be either in 24 * configured list or configuring list 25 */ 26 enum raid_bdev_state { 27 /* raid bdev is ready and is seen by upper layers */ 28 RAID_BDEV_STATE_ONLINE, 29 30 /* 31 * raid bdev is configuring, not all underlying bdevs are present. 32 * And can't be seen by upper layers. 33 */ 34 RAID_BDEV_STATE_CONFIGURING, 35 36 /* 37 * In offline state, raid bdev layer will complete all incoming commands without 38 * submitting to underlying base nvme bdevs 39 */ 40 RAID_BDEV_STATE_OFFLINE, 41 42 /* raid bdev state max, new states should be added before this */ 43 RAID_BDEV_STATE_MAX 44 }; 45 46 enum raid_process_type { 47 RAID_PROCESS_NONE, 48 RAID_PROCESS_REBUILD, 49 RAID_PROCESS_MAX 50 }; 51 52 typedef void (*raid_base_bdev_cb)(void *ctx, int status); 53 54 /* 55 * raid_base_bdev_info contains information for the base bdevs which are part of some 56 * raid. This structure contains the per base bdev information. Whatever is 57 * required per base device for raid bdev will be kept here 58 */ 59 struct raid_base_bdev_info { 60 /* The raid bdev that this base bdev belongs to */ 61 struct raid_bdev *raid_bdev; 62 63 /* name of the bdev */ 64 char *name; 65 66 /* uuid of the bdev */ 67 struct spdk_uuid uuid; 68 69 /* 70 * Pointer to base bdev descriptor opened by raid bdev. This is NULL when the bdev for 71 * this slot is missing. 72 */ 73 struct spdk_bdev_desc *desc; 74 75 /* offset in blocks from the start of the base bdev to the start of the data region */ 76 uint64_t data_offset; 77 78 /* size in blocks of the base bdev's data region */ 79 uint64_t data_size; 80 81 /* 82 * When underlying base device calls the hot plug function on drive removal, 83 * this flag will be set and later after doing some processing, base device 84 * descriptor will be closed 85 */ 86 bool remove_scheduled; 87 88 /* callback for base bdev removal */ 89 raid_base_bdev_cb remove_cb; 90 91 /* context of the callback */ 92 void *remove_cb_ctx; 93 94 /* Hold the number of blocks to know how large the base bdev is resized. */ 95 uint64_t blockcnt; 96 97 /* io channel for the app thread */ 98 struct spdk_io_channel *app_thread_ch; 99 100 /* Set to true when base bdev has completed the configuration process */ 101 bool is_configured; 102 103 /* Set to true if this base bdev is the target of a background process */ 104 bool is_process_target; 105 106 /* callback for base bdev configuration */ 107 raid_base_bdev_cb configure_cb; 108 109 /* context of the callback */ 110 void *configure_cb_ctx; 111 }; 112 113 struct raid_bdev_io; 114 typedef void (*raid_bdev_io_completion_cb)(struct raid_bdev_io *raid_io, 115 enum spdk_bdev_io_status status); 116 117 /* 118 * raid_bdev_io is the context part of bdev_io. It contains the information 119 * related to bdev_io for a raid bdev 120 */ 121 struct raid_bdev_io { 122 /* The raid bdev associated with this IO */ 123 struct raid_bdev *raid_bdev; 124 125 uint64_t offset_blocks; 126 uint64_t num_blocks; 127 struct iovec *iovs; 128 int iovcnt; 129 enum spdk_bdev_io_type type; 130 struct spdk_memory_domain *memory_domain; 131 void *memory_domain_ctx; 132 void *md_buf; 133 134 /* WaitQ entry, used only in waitq logic */ 135 struct spdk_bdev_io_wait_entry waitq_entry; 136 137 /* Context of the original channel for this IO */ 138 struct raid_bdev_io_channel *raid_ch; 139 140 /* Used for tracking progress on io requests sent to member disks. */ 141 uint64_t base_bdev_io_remaining; 142 uint8_t base_bdev_io_submitted; 143 enum spdk_bdev_io_status base_bdev_io_status; 144 145 /* Private data for the raid module */ 146 void *module_private; 147 148 /* Custom completion callback. Overrides bdev_io completion if set. */ 149 raid_bdev_io_completion_cb completion_cb; 150 151 struct { 152 uint64_t offset; 153 struct iovec *iov; 154 struct iovec iov_copy; 155 } split; 156 }; 157 158 struct raid_bdev_process_request { 159 struct raid_bdev_process *process; 160 struct raid_base_bdev_info *target; 161 struct spdk_io_channel *target_ch; 162 uint64_t offset_blocks; 163 uint32_t num_blocks; 164 struct iovec iov; 165 void *md_buf; 166 /* bdev_io is raid_io's driver_ctx - don't reorder them! 167 * These are needed for re-using raid module I/O functions for process I/O. */ 168 struct spdk_bdev_io bdev_io; 169 struct raid_bdev_io raid_io; 170 TAILQ_ENTRY(raid_bdev_process_request) link; 171 }; 172 173 /* 174 * raid_bdev is the single entity structure which contains SPDK block device 175 * and the information related to any raid bdev either configured or 176 * in configuring list. io device is created on this. 177 */ 178 struct raid_bdev { 179 /* raid bdev device, this will get registered in bdev layer */ 180 struct spdk_bdev bdev; 181 182 /* the raid bdev descriptor, opened for internal use */ 183 struct spdk_bdev_desc *self_desc; 184 185 /* link of raid bdev to link it to global raid bdev list */ 186 TAILQ_ENTRY(raid_bdev) global_link; 187 188 /* array of base bdev info */ 189 struct raid_base_bdev_info *base_bdev_info; 190 191 /* strip size of raid bdev in blocks */ 192 uint32_t strip_size; 193 194 /* strip size of raid bdev in KB */ 195 uint32_t strip_size_kb; 196 197 /* strip size bit shift for optimized calculation */ 198 uint32_t strip_size_shift; 199 200 /* state of raid bdev */ 201 enum raid_bdev_state state; 202 203 /* number of base bdevs comprising raid bdev */ 204 uint8_t num_base_bdevs; 205 206 /* number of base bdevs discovered */ 207 uint8_t num_base_bdevs_discovered; 208 209 /* 210 * Number of operational base bdevs, i.e. how many we know/expect to be working. This 211 * will be less than num_base_bdevs when starting a degraded array. 212 */ 213 uint8_t num_base_bdevs_operational; 214 215 /* minimum number of viable base bdevs that are required by array to operate */ 216 uint8_t min_base_bdevs_operational; 217 218 /* Raid Level of this raid bdev */ 219 enum raid_level level; 220 221 /* Set to true if destroy of this raid bdev is started. */ 222 bool destroy_started; 223 224 /* Module for RAID-level specific operations */ 225 struct raid_bdev_module *module; 226 227 /* Private data for the raid module */ 228 void *module_private; 229 230 /* Superblock */ 231 bool superblock_enabled; 232 struct raid_bdev_superblock *sb; 233 234 /* Superblock buffer used for I/O */ 235 void *sb_io_buf; 236 uint32_t sb_io_buf_size; 237 238 /* Raid bdev background process, e.g. rebuild */ 239 struct raid_bdev_process *process; 240 }; 241 242 #define RAID_FOR_EACH_BASE_BDEV(r, i) \ 243 for (i = r->base_bdev_info; i < r->base_bdev_info + r->num_base_bdevs; i++) 244 245 struct raid_bdev_io_channel; 246 247 /* TAIL head for raid bdev list */ 248 TAILQ_HEAD(raid_all_tailq, raid_bdev); 249 250 extern struct raid_all_tailq g_raid_bdev_list; 251 252 typedef void (*raid_bdev_destruct_cb)(void *cb_ctx, int rc); 253 254 int raid_bdev_create(const char *name, uint32_t strip_size, uint8_t num_base_bdevs, 255 enum raid_level level, bool superblock, const struct spdk_uuid *uuid, 256 struct raid_bdev **raid_bdev_out); 257 void raid_bdev_delete(struct raid_bdev *raid_bdev, raid_bdev_destruct_cb cb_fn, void *cb_ctx); 258 int raid_bdev_add_base_bdev(struct raid_bdev *raid_bdev, const char *name, 259 raid_base_bdev_cb cb_fn, void *cb_ctx); 260 struct raid_bdev *raid_bdev_find_by_name(const char *name); 261 enum raid_level raid_bdev_str_to_level(const char *str); 262 const char *raid_bdev_level_to_str(enum raid_level level); 263 enum raid_bdev_state raid_bdev_str_to_state(const char *str); 264 const char *raid_bdev_state_to_str(enum raid_bdev_state state); 265 const char *raid_bdev_process_to_str(enum raid_process_type value); 266 void raid_bdev_write_info_json(struct raid_bdev *raid_bdev, struct spdk_json_write_ctx *w); 267 int raid_bdev_remove_base_bdev(struct spdk_bdev *base_bdev, raid_base_bdev_cb cb_fn, void *cb_ctx); 268 269 /* 270 * RAID module descriptor 271 */ 272 struct raid_bdev_module { 273 /* RAID level implemented by this module */ 274 enum raid_level level; 275 276 /* Minimum required number of base bdevs. Must be > 0. */ 277 uint8_t base_bdevs_min; 278 279 /* 280 * RAID constraint. Determines number of base bdevs that can be removed 281 * without failing the array. 282 */ 283 struct { 284 enum { 285 CONSTRAINT_UNSET = 0, 286 CONSTRAINT_MAX_BASE_BDEVS_REMOVED, 287 CONSTRAINT_MIN_BASE_BDEVS_OPERATIONAL, 288 } type; 289 uint8_t value; 290 } base_bdevs_constraint; 291 292 /* Set to true if this module supports memory domains. */ 293 bool memory_domains_supported; 294 295 /* Set to true if this module supports DIF/DIX */ 296 bool dif_supported; 297 298 /* 299 * Called when the raid is starting, right before changing the state to 300 * online and registering the bdev. Parameters of the bdev like blockcnt 301 * should be set here. 302 * 303 * Non-zero return value will abort the startup process. 304 */ 305 int (*start)(struct raid_bdev *raid_bdev); 306 307 /* 308 * Called when the raid is stopping, right before changing the state to 309 * offline and unregistering the bdev. Optional. 310 * 311 * The function should return false if it is asynchronous. Then, after 312 * the async operation has completed and the module is fully stopped 313 * raid_bdev_module_stop_done() must be called. 314 */ 315 bool (*stop)(struct raid_bdev *raid_bdev); 316 317 /* Handler for R/W requests */ 318 void (*submit_rw_request)(struct raid_bdev_io *raid_io); 319 320 /* Handler for requests without payload (flush, unmap). Optional. */ 321 void (*submit_null_payload_request)(struct raid_bdev_io *raid_io); 322 323 /* 324 * Called when the bdev's IO channel is created to get the module's private IO channel. 325 * Optional. 326 */ 327 struct spdk_io_channel *(*get_io_channel)(struct raid_bdev *raid_bdev); 328 329 /* 330 * Called when a base_bdev is resized to resize the raid if the condition 331 * is satisfied. Optional. 332 * 333 * Returns true if the resize was performed. 334 */ 335 bool (*resize)(struct raid_bdev *raid_bdev); 336 337 /* Handler for raid process requests. Required for raid modules with redundancy. */ 338 int (*submit_process_request)(struct raid_bdev_process_request *process_req, 339 struct raid_bdev_io_channel *raid_ch); 340 341 TAILQ_ENTRY(raid_bdev_module) link; 342 }; 343 344 void raid_bdev_module_list_add(struct raid_bdev_module *raid_module); 345 346 #define __RAID_MODULE_REGISTER(line) __RAID_MODULE_REGISTER_(line) 347 #define __RAID_MODULE_REGISTER_(line) raid_module_register_##line 348 349 #define RAID_MODULE_REGISTER(_module) \ 350 __attribute__((constructor)) static void \ 351 __RAID_MODULE_REGISTER(__LINE__)(void) \ 352 { \ 353 raid_bdev_module_list_add(_module); \ 354 } 355 356 bool raid_bdev_io_complete_part(struct raid_bdev_io *raid_io, uint64_t completed, 357 enum spdk_bdev_io_status status); 358 void raid_bdev_queue_io_wait(struct raid_bdev_io *raid_io, struct spdk_bdev *bdev, 359 struct spdk_io_channel *ch, spdk_bdev_io_wait_cb cb_fn); 360 void raid_bdev_io_complete(struct raid_bdev_io *raid_io, enum spdk_bdev_io_status status); 361 void raid_bdev_module_stop_done(struct raid_bdev *raid_bdev); 362 struct spdk_io_channel *raid_bdev_channel_get_base_channel(struct raid_bdev_io_channel *raid_ch, 363 uint8_t idx); 364 void *raid_bdev_channel_get_module_ctx(struct raid_bdev_io_channel *raid_ch); 365 struct raid_base_bdev_info *raid_bdev_channel_get_base_info(struct raid_bdev_io_channel *raid_ch, 366 struct spdk_bdev *base_bdev); 367 void raid_bdev_process_request_complete(struct raid_bdev_process_request *process_req, int status); 368 void raid_bdev_io_init(struct raid_bdev_io *raid_io, struct raid_bdev_io_channel *raid_ch, 369 enum spdk_bdev_io_type type, uint64_t offset_blocks, 370 uint64_t num_blocks, struct iovec *iovs, int iovcnt, void *md_buf, 371 struct spdk_memory_domain *memory_domain, void *memory_domain_ctx); 372 373 static inline uint8_t 374 raid_bdev_base_bdev_slot(struct raid_base_bdev_info *base_info) 375 { 376 return base_info - base_info->raid_bdev->base_bdev_info; 377 } 378 379 int raid_bdev_remap_dix_reftag(void *md_buf, uint64_t num_blocks, 380 struct spdk_bdev *bdev, uint32_t remapped_offset); 381 int raid_bdev_verify_dix_reftag(struct iovec *iovs, int iovcnt, void *md_buf, 382 uint64_t num_blocks, struct spdk_bdev *bdev, uint32_t offset_blocks); 383 384 /** 385 * Raid bdev I/O read/write wrapper for spdk_bdev_readv_blocks_ext function. 386 */ 387 static inline int 388 raid_bdev_readv_blocks_ext(struct raid_base_bdev_info *base_info, struct spdk_io_channel *ch, 389 struct iovec *iov, int iovcnt, uint64_t offset_blocks, 390 uint64_t num_blocks, spdk_bdev_io_completion_cb cb, void *cb_arg, 391 struct spdk_bdev_ext_io_opts *opts) 392 { 393 return spdk_bdev_readv_blocks_ext(base_info->desc, ch, iov, iovcnt, 394 base_info->data_offset + offset_blocks, num_blocks, cb, cb_arg, opts); 395 } 396 397 /** 398 * Raid bdev I/O read/write wrapper for spdk_bdev_writev_blocks_ext function. 399 */ 400 static inline int 401 raid_bdev_writev_blocks_ext(struct raid_base_bdev_info *base_info, struct spdk_io_channel *ch, 402 struct iovec *iov, int iovcnt, uint64_t offset_blocks, 403 uint64_t num_blocks, spdk_bdev_io_completion_cb cb, void *cb_arg, 404 struct spdk_bdev_ext_io_opts *opts) 405 { 406 int rc; 407 uint64_t remapped_offset_blocks = base_info->data_offset + offset_blocks; 408 409 if (spdk_unlikely(spdk_bdev_get_dif_type(&base_info->raid_bdev->bdev) != SPDK_DIF_DISABLE && 410 (base_info->raid_bdev->bdev.dif_check_flags & SPDK_DIF_FLAGS_REFTAG_CHECK))) { 411 rc = raid_bdev_remap_dix_reftag(opts->metadata, num_blocks, &base_info->raid_bdev->bdev, 412 remapped_offset_blocks); 413 if (rc != 0) { 414 return rc; 415 } 416 } 417 418 return spdk_bdev_writev_blocks_ext(base_info->desc, ch, iov, iovcnt, 419 remapped_offset_blocks, num_blocks, cb, cb_arg, opts); 420 } 421 422 /** 423 * Raid bdev I/O read/write wrapper for spdk_bdev_unmap_blocks function. 424 */ 425 static inline int 426 raid_bdev_unmap_blocks(struct raid_base_bdev_info *base_info, struct spdk_io_channel *ch, 427 uint64_t offset_blocks, uint64_t num_blocks, 428 spdk_bdev_io_completion_cb cb, void *cb_arg) 429 { 430 return spdk_bdev_unmap_blocks(base_info->desc, ch, base_info->data_offset + offset_blocks, 431 num_blocks, cb, cb_arg); 432 } 433 434 /** 435 * Raid bdev I/O read/write wrapper for spdk_bdev_flush_blocks function. 436 */ 437 static inline int 438 raid_bdev_flush_blocks(struct raid_base_bdev_info *base_info, struct spdk_io_channel *ch, 439 uint64_t offset_blocks, uint64_t num_blocks, 440 spdk_bdev_io_completion_cb cb, void *cb_arg) 441 { 442 return spdk_bdev_flush_blocks(base_info->desc, ch, base_info->data_offset + offset_blocks, 443 num_blocks, cb, cb_arg); 444 } 445 446 /* 447 * Definitions related to raid bdev superblock 448 */ 449 450 #define RAID_BDEV_SB_VERSION_MAJOR 1 451 #define RAID_BDEV_SB_VERSION_MINOR 0 452 453 #define RAID_BDEV_SB_NAME_SIZE 64 454 455 enum raid_bdev_sb_base_bdev_state { 456 RAID_SB_BASE_BDEV_MISSING = 0, 457 RAID_SB_BASE_BDEV_CONFIGURED = 1, 458 RAID_SB_BASE_BDEV_FAILED = 2, 459 RAID_SB_BASE_BDEV_SPARE = 3, 460 }; 461 462 struct raid_bdev_sb_base_bdev { 463 /* uuid of the base bdev */ 464 struct spdk_uuid uuid; 465 /* offset in blocks from base device start to the start of raid data area */ 466 uint64_t data_offset; 467 /* size in blocks of the base device raid data area */ 468 uint64_t data_size; 469 /* state of the base bdev */ 470 uint32_t state; 471 /* feature/status flags */ 472 uint32_t flags; 473 /* slot number of this base bdev in the raid */ 474 uint8_t slot; 475 476 uint8_t reserved[23]; 477 }; 478 SPDK_STATIC_ASSERT(sizeof(struct raid_bdev_sb_base_bdev) == 64, "incorrect size"); 479 480 struct raid_bdev_superblock { 481 #define RAID_BDEV_SB_SIG "SPDKRAID" 482 uint8_t signature[8]; 483 struct { 484 /* incremented when a breaking change in the superblock structure is made */ 485 uint16_t major; 486 /* incremented for changes in the superblock that are backward compatible */ 487 uint16_t minor; 488 } version; 489 /* length in bytes of the entire superblock */ 490 uint32_t length; 491 /* crc32c checksum of the entire superblock */ 492 uint32_t crc; 493 /* feature/status flags */ 494 uint32_t flags; 495 /* unique id of the raid bdev */ 496 struct spdk_uuid uuid; 497 /* name of the raid bdev */ 498 uint8_t name[RAID_BDEV_SB_NAME_SIZE]; 499 /* size of the raid bdev in blocks */ 500 uint64_t raid_size; 501 /* the raid bdev block size - must be the same for all base bdevs */ 502 uint32_t block_size; 503 /* the raid level */ 504 uint32_t level; 505 /* strip (chunk) size in blocks */ 506 uint32_t strip_size; 507 /* state of the raid */ 508 uint32_t state; 509 /* sequence number, incremented on every superblock update */ 510 uint64_t seq_number; 511 /* number of raid base devices */ 512 uint8_t num_base_bdevs; 513 514 uint8_t reserved[118]; 515 516 /* size of the base bdevs array */ 517 uint8_t base_bdevs_size; 518 /* array of base bdev descriptors */ 519 struct raid_bdev_sb_base_bdev base_bdevs[]; 520 }; 521 SPDK_STATIC_ASSERT(sizeof(struct raid_bdev_superblock) == 256, "incorrect size"); 522 523 #define RAID_BDEV_SB_MAX_LENGTH (sizeof(struct raid_bdev_superblock) + UINT8_MAX * sizeof(struct raid_bdev_sb_base_bdev)) 524 525 SPDK_STATIC_ASSERT(RAID_BDEV_SB_MAX_LENGTH < RAID_BDEV_MIN_DATA_OFFSET_SIZE, 526 "Incorrect min data offset"); 527 528 typedef void (*raid_bdev_write_sb_cb)(int status, struct raid_bdev *raid_bdev, void *ctx); 529 typedef void (*raid_bdev_load_sb_cb)(const struct raid_bdev_superblock *sb, int status, void *ctx); 530 531 int raid_bdev_alloc_superblock(struct raid_bdev *raid_bdev, uint32_t block_size); 532 void raid_bdev_free_superblock(struct raid_bdev *raid_bdev); 533 void raid_bdev_init_superblock(struct raid_bdev *raid_bdev); 534 void raid_bdev_write_superblock(struct raid_bdev *raid_bdev, raid_bdev_write_sb_cb cb, 535 void *cb_ctx); 536 int raid_bdev_load_base_bdev_superblock(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 537 raid_bdev_load_sb_cb cb, void *cb_ctx); 538 539 struct spdk_raid_bdev_opts { 540 /* Size of the background process window in KiB */ 541 uint32_t process_window_size_kb; 542 }; 543 544 void raid_bdev_get_opts(struct spdk_raid_bdev_opts *opts); 545 int raid_bdev_set_opts(const struct spdk_raid_bdev_opts *opts); 546 547 #endif /* SPDK_BDEV_RAID_INTERNAL_H */ 548