Branch data Line data Source code
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 : : /* callback for base bdev configuration */
104 : : raid_base_bdev_cb configure_cb;
105 : :
106 : : /* context of the callback */
107 : : void *configure_cb_ctx;
108 : : };
109 : :
110 : : struct raid_bdev_io;
111 : : typedef void (*raid_bdev_io_completion_cb)(struct raid_bdev_io *raid_io,
112 : : enum spdk_bdev_io_status status);
113 : :
114 : : /*
115 : : * raid_bdev_io is the context part of bdev_io. It contains the information
116 : : * related to bdev_io for a raid bdev
117 : : */
118 : : struct raid_bdev_io {
119 : : /* The raid bdev associated with this IO */
120 : : struct raid_bdev *raid_bdev;
121 : :
122 : : uint64_t offset_blocks;
123 : : uint64_t num_blocks;
124 : : struct iovec *iovs;
125 : : int iovcnt;
126 : : enum spdk_bdev_io_type type;
127 : : struct spdk_memory_domain *memory_domain;
128 : : void *memory_domain_ctx;
129 : : void *md_buf;
130 : :
131 : : /* WaitQ entry, used only in waitq logic */
132 : : struct spdk_bdev_io_wait_entry waitq_entry;
133 : :
134 : : /* Context of the original channel for this IO */
135 : : struct raid_bdev_io_channel *raid_ch;
136 : :
137 : : /* Used for tracking progress on io requests sent to member disks. */
138 : : uint64_t base_bdev_io_remaining;
139 : : uint8_t base_bdev_io_submitted;
140 : : enum spdk_bdev_io_status base_bdev_io_status;
141 : :
142 : : /* Private data for the raid module */
143 : : void *module_private;
144 : :
145 : : /* Custom completion callback. Overrides bdev_io completion if set. */
146 : : raid_bdev_io_completion_cb completion_cb;
147 : :
148 : : struct {
149 : : uint64_t offset;
150 : : struct iovec *iov;
151 : : struct iovec iov_copy;
152 : : } split;
153 : : };
154 : :
155 : : struct raid_bdev_process_request {
156 : : struct raid_bdev_process *process;
157 : : struct raid_base_bdev_info *target;
158 : : struct spdk_io_channel *target_ch;
159 : : uint64_t offset_blocks;
160 : : uint32_t num_blocks;
161 : : struct iovec iov;
162 : : void *md_buf;
163 : : /* bdev_io is raid_io's driver_ctx - don't reorder them!
164 : : * These are needed for re-using raid module I/O functions for process I/O. */
165 : : struct spdk_bdev_io bdev_io;
166 : : struct raid_bdev_io raid_io;
167 : : TAILQ_ENTRY(raid_bdev_process_request) link;
168 : : };
169 : :
170 : : /*
171 : : * raid_bdev is the single entity structure which contains SPDK block device
172 : : * and the information related to any raid bdev either configured or
173 : : * in configuring list. io device is created on this.
174 : : */
175 : : struct raid_bdev {
176 : : /* raid bdev device, this will get registered in bdev layer */
177 : : struct spdk_bdev bdev;
178 : :
179 : : /* the raid bdev descriptor, opened for internal use */
180 : : struct spdk_bdev_desc *self_desc;
181 : :
182 : : /* link of raid bdev to link it to global raid bdev list */
183 : : TAILQ_ENTRY(raid_bdev) global_link;
184 : :
185 : : /* array of base bdev info */
186 : : struct raid_base_bdev_info *base_bdev_info;
187 : :
188 : : /* lock to protect the base bdev array */
189 : : struct spdk_spinlock base_bdev_lock;
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 : : /* block length bit shift for optimized calculation */
201 : : uint32_t blocklen_shift;
202 : :
203 : : /* state of raid bdev */
204 : : enum raid_bdev_state state;
205 : :
206 : : /* number of base bdevs comprising raid bdev */
207 : : uint8_t num_base_bdevs;
208 : :
209 : : /* number of base bdevs discovered */
210 : : uint8_t num_base_bdevs_discovered;
211 : :
212 : : /*
213 : : * Number of operational base bdevs, i.e. how many we know/expect to be working. This
214 : : * will be less than num_base_bdevs when starting a degraded array.
215 : : */
216 : : uint8_t num_base_bdevs_operational;
217 : :
218 : : /* minimum number of viable base bdevs that are required by array to operate */
219 : : uint8_t min_base_bdevs_operational;
220 : :
221 : : /* Raid Level of this raid bdev */
222 : : enum raid_level level;
223 : :
224 : : /* Set to true if destroy of this raid bdev is started. */
225 : : bool destroy_started;
226 : :
227 : : /* Module for RAID-level specific operations */
228 : : struct raid_bdev_module *module;
229 : :
230 : : /* Private data for the raid module */
231 : : void *module_private;
232 : :
233 : : /* Superblock */
234 : : struct raid_bdev_superblock *sb;
235 : :
236 : : /* Raid bdev background process, e.g. rebuild */
237 : : struct raid_bdev_process *process;
238 : : };
239 : :
240 : : #define RAID_FOR_EACH_BASE_BDEV(r, i) \
241 : : for (i = r->base_bdev_info; i < r->base_bdev_info + r->num_base_bdevs; i++)
242 : :
243 : : struct raid_bdev_io_channel;
244 : :
245 : : /* TAIL head for raid bdev list */
246 : : TAILQ_HEAD(raid_all_tailq, raid_bdev);
247 : :
248 : : extern struct raid_all_tailq g_raid_bdev_list;
249 : :
250 : : typedef void (*raid_bdev_destruct_cb)(void *cb_ctx, int rc);
251 : :
252 : : int raid_bdev_create(const char *name, uint32_t strip_size, uint8_t num_base_bdevs,
253 : : enum raid_level level, bool superblock, const struct spdk_uuid *uuid,
254 : : struct raid_bdev **raid_bdev_out);
255 : : void raid_bdev_delete(struct raid_bdev *raid_bdev, raid_bdev_destruct_cb cb_fn, void *cb_ctx);
256 : : int raid_bdev_add_base_device(struct raid_bdev *raid_bdev, const char *name, uint8_t slot,
257 : : raid_base_bdev_cb cb_fn, void *cb_ctx);
258 : : struct raid_bdev *raid_bdev_find_by_name(const char *name);
259 : : enum raid_level raid_bdev_str_to_level(const char *str);
260 : : const char *raid_bdev_level_to_str(enum raid_level level);
261 : : enum raid_bdev_state raid_bdev_str_to_state(const char *str);
262 : : const char *raid_bdev_state_to_str(enum raid_bdev_state state);
263 : : const char *raid_bdev_process_to_str(enum raid_process_type value);
264 : : void raid_bdev_write_info_json(struct raid_bdev *raid_bdev, struct spdk_json_write_ctx *w);
265 : : int raid_bdev_remove_base_bdev(struct spdk_bdev *base_bdev, raid_base_bdev_cb cb_fn, void *cb_ctx);
266 : : int raid_bdev_attach_base_bdev(struct raid_bdev *raid_bdev, struct spdk_bdev *base_bdev,
267 : : 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 : : /*
296 : : * Called when the raid is starting, right before changing the state to
297 : : * online and registering the bdev. Parameters of the bdev like blockcnt
298 : : * should be set here.
299 : : *
300 : : * Non-zero return value will abort the startup process.
301 : : */
302 : : int (*start)(struct raid_bdev *raid_bdev);
303 : :
304 : : /*
305 : : * Called when the raid is stopping, right before changing the state to
306 : : * offline and unregistering the bdev. Optional.
307 : : *
308 : : * The function should return false if it is asynchronous. Then, after
309 : : * the async operation has completed and the module is fully stopped
310 : : * raid_bdev_module_stop_done() must be called.
311 : : */
312 : : bool (*stop)(struct raid_bdev *raid_bdev);
313 : :
314 : : /* Handler for R/W requests */
315 : : void (*submit_rw_request)(struct raid_bdev_io *raid_io);
316 : :
317 : : /* Handler for requests without payload (flush, unmap). Optional. */
318 : : void (*submit_null_payload_request)(struct raid_bdev_io *raid_io);
319 : :
320 : : /*
321 : : * Called when the bdev's IO channel is created to get the module's private IO channel.
322 : : * Optional.
323 : : */
324 : : struct spdk_io_channel *(*get_io_channel)(struct raid_bdev *raid_bdev);
325 : :
326 : : /*
327 : : * Called when a base_bdev is resized to resize the raid if the condition
328 : : * is satisfied.
329 : : */
330 : : void (*resize)(struct raid_bdev *raid_bdev);
331 : :
332 : : /* Handler for raid process requests. Required for raid modules with redundancy. */
333 : : int (*submit_process_request)(struct raid_bdev_process_request *process_req,
334 : : struct raid_bdev_io_channel *raid_ch);
335 : :
336 : : TAILQ_ENTRY(raid_bdev_module) link;
337 : : };
338 : :
339 : : void raid_bdev_module_list_add(struct raid_bdev_module *raid_module);
340 : :
341 : : #define __RAID_MODULE_REGISTER(line) __RAID_MODULE_REGISTER_(line)
342 : : #define __RAID_MODULE_REGISTER_(line) raid_module_register_##line
343 : :
344 : : #define RAID_MODULE_REGISTER(_module) \
345 : : __attribute__((constructor)) static void \
346 : : __RAID_MODULE_REGISTER(__LINE__)(void) \
347 : : { \
348 : : raid_bdev_module_list_add(_module); \
349 : : }
350 : :
351 : : bool raid_bdev_io_complete_part(struct raid_bdev_io *raid_io, uint64_t completed,
352 : : enum spdk_bdev_io_status status);
353 : : void raid_bdev_queue_io_wait(struct raid_bdev_io *raid_io, struct spdk_bdev *bdev,
354 : : struct spdk_io_channel *ch, spdk_bdev_io_wait_cb cb_fn);
355 : : void raid_bdev_io_complete(struct raid_bdev_io *raid_io, enum spdk_bdev_io_status status);
356 : : void raid_bdev_module_stop_done(struct raid_bdev *raid_bdev);
357 : : struct spdk_io_channel *raid_bdev_channel_get_base_channel(struct raid_bdev_io_channel *raid_ch,
358 : : uint8_t idx);
359 : : void *raid_bdev_channel_get_module_ctx(struct raid_bdev_io_channel *raid_ch);
360 : : void raid_bdev_process_request_complete(struct raid_bdev_process_request *process_req, int status);
361 : : void raid_bdev_io_init(struct raid_bdev_io *raid_io, struct raid_bdev_io_channel *raid_ch,
362 : : enum spdk_bdev_io_type type, uint64_t offset_blocks,
363 : : uint64_t num_blocks, struct iovec *iovs, int iovcnt, void *md_buf,
364 : : struct spdk_memory_domain *memory_domain, void *memory_domain_ctx);
365 : :
366 : : static inline uint8_t
367 : 3853 : raid_bdev_base_bdev_slot(struct raid_base_bdev_info *base_info)
368 : : {
369 : 3853 : return base_info - base_info->raid_bdev->base_bdev_info;
370 : : }
371 : :
372 : : /**
373 : : * Raid bdev I/O read/write wrapper for spdk_bdev_readv_blocks_ext function.
374 : : */
375 : : static inline int
376 : 5665043 : raid_bdev_readv_blocks_ext(struct raid_base_bdev_info *base_info, struct spdk_io_channel *ch,
377 : : struct iovec *iov, int iovcnt, uint64_t offset_blocks,
378 : : uint64_t num_blocks, spdk_bdev_io_completion_cb cb, void *cb_arg,
379 : : struct spdk_bdev_ext_io_opts *opts)
380 : : {
381 : 6299825 : return spdk_bdev_readv_blocks_ext(base_info->desc, ch, iov, iovcnt,
382 : 5665043 : base_info->data_offset + offset_blocks, num_blocks, cb, cb_arg, opts);
383 : : }
384 : :
385 : : /**
386 : : * Raid bdev I/O read/write wrapper for spdk_bdev_writev_blocks_ext function.
387 : : */
388 : : static inline int
389 : 11538893 : raid_bdev_writev_blocks_ext(struct raid_base_bdev_info *base_info, struct spdk_io_channel *ch,
390 : : struct iovec *iov, int iovcnt, uint64_t offset_blocks,
391 : : uint64_t num_blocks, spdk_bdev_io_completion_cb cb, void *cb_arg,
392 : : struct spdk_bdev_ext_io_opts *opts)
393 : : {
394 : 13860678 : return spdk_bdev_writev_blocks_ext(base_info->desc, ch, iov, iovcnt,
395 : 11538893 : base_info->data_offset + offset_blocks, num_blocks, cb, cb_arg, opts);
396 : : }
397 : :
398 : : /**
399 : : * Raid bdev I/O read/write wrapper for spdk_bdev_unmap_blocks function.
400 : : */
401 : : static inline int
402 : 1335080 : raid_bdev_unmap_blocks(struct raid_base_bdev_info *base_info, struct spdk_io_channel *ch,
403 : : uint64_t offset_blocks, uint64_t num_blocks,
404 : : spdk_bdev_io_completion_cb cb, void *cb_arg)
405 : : {
406 : 1335080 : return spdk_bdev_unmap_blocks(base_info->desc, ch, base_info->data_offset + offset_blocks,
407 : : num_blocks, cb, cb_arg);
408 : : }
409 : :
410 : : /**
411 : : * Raid bdev I/O read/write wrapper for spdk_bdev_flush_blocks function.
412 : : */
413 : : static inline int
414 : 454 : raid_bdev_flush_blocks(struct raid_base_bdev_info *base_info, struct spdk_io_channel *ch,
415 : : uint64_t offset_blocks, uint64_t num_blocks,
416 : : spdk_bdev_io_completion_cb cb, void *cb_arg)
417 : : {
418 : 454 : return spdk_bdev_flush_blocks(base_info->desc, ch, base_info->data_offset + offset_blocks,
419 : : num_blocks, cb, cb_arg);
420 : : }
421 : :
422 : : /*
423 : : * Definitions related to raid bdev superblock
424 : : */
425 : :
426 : : #define RAID_BDEV_SB_VERSION_MAJOR 1
427 : : #define RAID_BDEV_SB_VERSION_MINOR 0
428 : :
429 : : #define RAID_BDEV_SB_NAME_SIZE 64
430 : :
431 : : enum raid_bdev_sb_base_bdev_state {
432 : : RAID_SB_BASE_BDEV_MISSING = 0,
433 : : RAID_SB_BASE_BDEV_CONFIGURED = 1,
434 : : RAID_SB_BASE_BDEV_FAILED = 2,
435 : : RAID_SB_BASE_BDEV_SPARE = 3,
436 : : };
437 : :
438 : : struct raid_bdev_sb_base_bdev {
439 : : /* uuid of the base bdev */
440 : : struct spdk_uuid uuid;
441 : : /* offset in blocks from base device start to the start of raid data area */
442 : : uint64_t data_offset;
443 : : /* size in blocks of the base device raid data area */
444 : : uint64_t data_size;
445 : : /* state of the base bdev */
446 : : uint32_t state;
447 : : /* feature/status flags */
448 : : uint32_t flags;
449 : : /* slot number of this base bdev in the raid */
450 : : uint8_t slot;
451 : :
452 : : uint8_t reserved[23];
453 : : };
454 : : SPDK_STATIC_ASSERT(sizeof(struct raid_bdev_sb_base_bdev) == 64, "incorrect size");
455 : :
456 : : struct raid_bdev_superblock {
457 : : #define RAID_BDEV_SB_SIG "SPDKRAID"
458 : : uint8_t signature[8];
459 : : struct {
460 : : /* incremented when a breaking change in the superblock structure is made */
461 : : uint16_t major;
462 : : /* incremented for changes in the superblock that are backward compatible */
463 : : uint16_t minor;
464 : : } version;
465 : : /* length in bytes of the entire superblock */
466 : : uint32_t length;
467 : : /* crc32c checksum of the entire superblock */
468 : : uint32_t crc;
469 : : /* feature/status flags */
470 : : uint32_t flags;
471 : : /* unique id of the raid bdev */
472 : : struct spdk_uuid uuid;
473 : : /* name of the raid bdev */
474 : : uint8_t name[RAID_BDEV_SB_NAME_SIZE];
475 : : /* size of the raid bdev in blocks */
476 : : uint64_t raid_size;
477 : : /* the raid bdev block size - must be the same for all base bdevs */
478 : : uint32_t block_size;
479 : : /* the raid level */
480 : : uint32_t level;
481 : : /* strip (chunk) size in blocks */
482 : : uint32_t strip_size;
483 : : /* state of the raid */
484 : : uint32_t state;
485 : : /* sequence number, incremented on every superblock update */
486 : : uint64_t seq_number;
487 : : /* number of raid base devices */
488 : : uint8_t num_base_bdevs;
489 : :
490 : : uint8_t reserved[118];
491 : :
492 : : /* size of the base bdevs array */
493 : : uint8_t base_bdevs_size;
494 : : /* array of base bdev descriptors */
495 : : struct raid_bdev_sb_base_bdev base_bdevs[];
496 : : };
497 : : SPDK_STATIC_ASSERT(sizeof(struct raid_bdev_superblock) == 256, "incorrect size");
498 : :
499 : : #define RAID_BDEV_SB_MAX_LENGTH \
500 : : SPDK_ALIGN_CEIL((sizeof(struct raid_bdev_superblock) + UINT8_MAX * sizeof(struct raid_bdev_sb_base_bdev)), 0x1000)
501 : :
502 : : SPDK_STATIC_ASSERT(RAID_BDEV_SB_MAX_LENGTH < RAID_BDEV_MIN_DATA_OFFSET_SIZE,
503 : : "Incorrect min data offset");
504 : :
505 : : typedef void (*raid_bdev_write_sb_cb)(int status, struct raid_bdev *raid_bdev, void *ctx);
506 : : typedef void (*raid_bdev_load_sb_cb)(const struct raid_bdev_superblock *sb, int status, void *ctx);
507 : :
508 : : void raid_bdev_init_superblock(struct raid_bdev *raid_bdev);
509 : : void raid_bdev_write_superblock(struct raid_bdev *raid_bdev, raid_bdev_write_sb_cb cb,
510 : : void *cb_ctx);
511 : : int raid_bdev_load_base_bdev_superblock(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
512 : : raid_bdev_load_sb_cb cb, void *cb_ctx);
513 : :
514 : : struct spdk_raid_bdev_opts {
515 : : /* Size of the background process window in KiB */
516 : : uint32_t process_window_size_kb;
517 : : };
518 : :
519 : : void raid_bdev_get_opts(struct spdk_raid_bdev_opts *opts);
520 : : int raid_bdev_set_opts(const struct spdk_raid_bdev_opts *opts);
521 : :
522 : : #endif /* SPDK_BDEV_RAID_INTERNAL_H */
|