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