xref: /spdk/module/bdev/raid/bdev_raid.h (revision 838e61c3772fdefb17e1a0b8f9880e2bcb9c4c0d)
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 enum raid_level {
13 	INVALID_RAID_LEVEL	= -1,
14 	RAID0			= 0,
15 	RAID1			= 1,
16 	RAID5F			= 95, /* 0x5f */
17 	CONCAT			= 99,
18 };
19 
20 /*
21  * Raid state describes the state of the raid. This raid bdev can be either in
22  * configured list or configuring list
23  */
24 enum raid_bdev_state {
25 	/* raid bdev is ready and is seen by upper layers */
26 	RAID_BDEV_STATE_ONLINE,
27 
28 	/*
29 	 * raid bdev is configuring, not all underlying bdevs are present.
30 	 * And can't be seen by upper layers.
31 	 */
32 	RAID_BDEV_STATE_CONFIGURING,
33 
34 	/*
35 	 * In offline state, raid bdev layer will complete all incoming commands without
36 	 * submitting to underlying base nvme bdevs
37 	 */
38 	RAID_BDEV_STATE_OFFLINE,
39 
40 	/* raid bdev state max, new states should be added before this */
41 	RAID_BDEV_STATE_MAX
42 };
43 
44 /*
45  * raid_base_bdev_info contains information for the base bdevs which are part of some
46  * raid. This structure contains the per base bdev information. Whatever is
47  * required per base device for raid bdev will be kept here
48  */
49 struct raid_base_bdev_info {
50 	/* name of the bdev */
51 	char			*name;
52 
53 	/* pointer to base spdk bdev */
54 	struct spdk_bdev	*bdev;
55 
56 	/* pointer to base bdev descriptor opened by raid bdev */
57 	struct spdk_bdev_desc	*desc;
58 
59 	/*
60 	 * When underlying base device calls the hot plug function on drive removal,
61 	 * this flag will be set and later after doing some processing, base device
62 	 * descriptor will be closed
63 	 */
64 	bool			remove_scheduled;
65 
66 	/* Hold the number of blocks to know how large the base bdev is resized. */
67 	uint64_t		blockcnt;
68 };
69 
70 /*
71  * raid_bdev_io is the context part of bdev_io. It contains the information
72  * related to bdev_io for a raid bdev
73  */
74 struct raid_bdev_io {
75 	/* The raid bdev associated with this IO */
76 	struct raid_bdev *raid_bdev;
77 
78 	/* WaitQ entry, used only in waitq logic */
79 	struct spdk_bdev_io_wait_entry	waitq_entry;
80 
81 	/* Context of the original channel for this IO */
82 	struct raid_bdev_io_channel	*raid_ch;
83 
84 	/* Used for tracking progress on io requests sent to member disks. */
85 	uint64_t			base_bdev_io_remaining;
86 	uint8_t				base_bdev_io_submitted;
87 	uint8_t				base_bdev_io_status;
88 
89 	/* Private data for the raid module */
90 	void				*module_private;
91 };
92 
93 /*
94  * raid_bdev is the single entity structure which contains SPDK block device
95  * and the information related to any raid bdev either configured or
96  * in configuring list. io device is created on this.
97  */
98 struct raid_bdev {
99 	/* raid bdev device, this will get registered in bdev layer */
100 	struct spdk_bdev		bdev;
101 
102 	/* link of raid bdev to link it to global raid bdev list */
103 	TAILQ_ENTRY(raid_bdev)		global_link;
104 
105 	/* array of base bdev info */
106 	struct raid_base_bdev_info	*base_bdev_info;
107 
108 	/* strip size of raid bdev in blocks */
109 	uint32_t			strip_size;
110 
111 	/* strip size of raid bdev in KB */
112 	uint32_t			strip_size_kb;
113 
114 	/* strip size bit shift for optimized calculation */
115 	uint32_t			strip_size_shift;
116 
117 	/* block length bit shift for optimized calculation */
118 	uint32_t			blocklen_shift;
119 
120 	/* state of raid bdev */
121 	enum raid_bdev_state		state;
122 
123 	/* number of base bdevs comprising raid bdev  */
124 	uint8_t				num_base_bdevs;
125 
126 	/* number of base bdevs discovered */
127 	uint8_t				num_base_bdevs_discovered;
128 
129 	/* minimum number of viable base bdevs that are required by array to operate */
130 	uint8_t				min_base_bdevs_operational;
131 
132 	/* Raid Level of this raid bdev */
133 	enum raid_level			level;
134 
135 	/* Set to true if destroy of this raid bdev is started. */
136 	bool				destroy_started;
137 
138 	/* Module for RAID-level specific operations */
139 	struct raid_bdev_module		*module;
140 
141 	/* Private data for the raid module */
142 	void				*module_private;
143 };
144 
145 #define RAID_FOR_EACH_BASE_BDEV(r, i) \
146 	for (i = r->base_bdev_info; i < r->base_bdev_info + r->num_base_bdevs; i++)
147 
148 /*
149  * raid_bdev_io_channel is the context of spdk_io_channel for raid bdev device. It
150  * contains the relationship of raid bdev io channel with base bdev io channels.
151  */
152 struct raid_bdev_io_channel {
153 	/* Array of IO channels of base bdevs */
154 	struct spdk_io_channel	**base_channel;
155 
156 	/* Number of IO channels */
157 	uint8_t			num_channels;
158 
159 	/* Private raid module IO channel */
160 	struct spdk_io_channel	*module_channel;
161 };
162 
163 /* TAIL head for raid bdev list */
164 TAILQ_HEAD(raid_all_tailq, raid_bdev);
165 
166 extern struct raid_all_tailq		g_raid_bdev_list;
167 
168 typedef void (*raid_bdev_destruct_cb)(void *cb_ctx, int rc);
169 
170 int raid_bdev_create(const char *name, uint32_t strip_size, uint8_t num_base_bdevs,
171 		     enum raid_level level, struct raid_bdev **raid_bdev_out, const struct spdk_uuid *uuid);
172 void raid_bdev_delete(struct raid_bdev *raid_bdev, raid_bdev_destruct_cb cb_fn, void *cb_ctx);
173 int raid_bdev_add_base_device(struct raid_bdev *raid_bdev, const char *name, uint8_t slot);
174 struct raid_bdev *raid_bdev_find_by_name(const char *name);
175 enum raid_level raid_bdev_str_to_level(const char *str);
176 const char *raid_bdev_level_to_str(enum raid_level level);
177 enum raid_bdev_state raid_bdev_str_to_state(const char *str);
178 const char *raid_bdev_state_to_str(enum raid_bdev_state state);
179 void raid_bdev_write_info_json(struct raid_bdev *raid_bdev, struct spdk_json_write_ctx *w);
180 
181 /*
182  * RAID module descriptor
183  */
184 struct raid_bdev_module {
185 	/* RAID level implemented by this module */
186 	enum raid_level level;
187 
188 	/* Minimum required number of base bdevs. Must be > 0. */
189 	uint8_t base_bdevs_min;
190 
191 	/*
192 	 * RAID constraint. Determines number of base bdevs that can be removed
193 	 * without failing the array.
194 	 */
195 	struct {
196 		enum {
197 			CONSTRAINT_UNSET = 0,
198 			CONSTRAINT_MAX_BASE_BDEVS_REMOVED,
199 			CONSTRAINT_MIN_BASE_BDEVS_OPERATIONAL,
200 		} type;
201 		uint8_t value;
202 	} base_bdevs_constraint;
203 
204 	/* Set to true if this module supports memory domains. */
205 	bool memory_domains_supported;
206 
207 	/*
208 	 * Called when the raid is starting, right before changing the state to
209 	 * online and registering the bdev. Parameters of the bdev like blockcnt
210 	 * should be set here.
211 	 *
212 	 * Non-zero return value will abort the startup process.
213 	 */
214 	int (*start)(struct raid_bdev *raid_bdev);
215 
216 	/*
217 	 * Called when the raid is stopping, right before changing the state to
218 	 * offline and unregistering the bdev. Optional.
219 	 *
220 	 * The function should return false if it is asynchronous. Then, after
221 	 * the async operation has completed and the module is fully stopped
222 	 * raid_bdev_module_stop_done() must be called.
223 	 */
224 	bool (*stop)(struct raid_bdev *raid_bdev);
225 
226 	/* Handler for R/W requests */
227 	void (*submit_rw_request)(struct raid_bdev_io *raid_io);
228 
229 	/* Handler for requests without payload (flush, unmap). Optional. */
230 	void (*submit_null_payload_request)(struct raid_bdev_io *raid_io);
231 
232 	/*
233 	 * Called when the bdev's IO channel is created to get the module's private IO channel.
234 	 * Optional.
235 	 */
236 	struct spdk_io_channel *(*get_io_channel)(struct raid_bdev *raid_bdev);
237 
238 	/*
239 	 * Called when a base_bdev is resized to resize the raid if the condition
240 	 * is satisfied.
241 	 */
242 	void (*resize)(struct raid_bdev *raid_bdev);
243 
244 	TAILQ_ENTRY(raid_bdev_module) link;
245 };
246 
247 void raid_bdev_module_list_add(struct raid_bdev_module *raid_module);
248 
249 #define __RAID_MODULE_REGISTER(line) __RAID_MODULE_REGISTER_(line)
250 #define __RAID_MODULE_REGISTER_(line) raid_module_register_##line
251 
252 #define RAID_MODULE_REGISTER(_module)					\
253 __attribute__((constructor)) static void				\
254 __RAID_MODULE_REGISTER(__LINE__)(void)					\
255 {									\
256     raid_bdev_module_list_add(_module);					\
257 }
258 
259 bool raid_bdev_io_complete_part(struct raid_bdev_io *raid_io, uint64_t completed,
260 				enum spdk_bdev_io_status status);
261 void raid_bdev_queue_io_wait(struct raid_bdev_io *raid_io, struct spdk_bdev *bdev,
262 			     struct spdk_io_channel *ch, spdk_bdev_io_wait_cb cb_fn);
263 void raid_bdev_io_complete(struct raid_bdev_io *raid_io, enum spdk_bdev_io_status status);
264 void raid_bdev_module_stop_done(struct raid_bdev *raid_bdev);
265 
266 #endif /* SPDK_BDEV_RAID_INTERNAL_H */
267