xref: /spdk/module/bdev/raid/bdev_raid.h (revision 0ed85362c8132a2d1927757fbcade66b6660d26a)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef SPDK_BDEV_RAID_INTERNAL_H
35 #define SPDK_BDEV_RAID_INTERNAL_H
36 
37 #include "spdk/bdev_module.h"
38 
39 enum raid_level {
40 	INVALID_RAID_LEVEL	= -1,
41 	RAID0			= 0,
42 	RAID5			= 5,
43 };
44 
45 /*
46  * Raid state describes the state of the raid. This raid bdev can be either in
47  * configured list or configuring list
48  */
49 enum raid_bdev_state {
50 	/* raid bdev is ready and is seen by upper layers */
51 	RAID_BDEV_STATE_ONLINE,
52 
53 	/*
54 	 * raid bdev is configuring, not all underlying bdevs are present.
55 	 * And can't be seen by upper layers.
56 	 */
57 	RAID_BDEV_STATE_CONFIGURING,
58 
59 	/*
60 	 * In offline state, raid bdev layer will complete all incoming commands without
61 	 * submitting to underlying base nvme bdevs
62 	 */
63 	RAID_BDEV_STATE_OFFLINE,
64 
65 	/* raid bdev max, new states should be added before this */
66 	RAID_BDEV_MAX
67 };
68 
69 /*
70  * raid_base_bdev_info contains information for the base bdevs which are part of some
71  * raid. This structure contains the per base bdev information. Whatever is
72  * required per base device for raid bdev will be kept here
73  */
74 struct raid_base_bdev_info {
75 	/* pointer to base spdk bdev */
76 	struct spdk_bdev	*bdev;
77 
78 	/* pointer to base bdev descriptor opened by raid bdev */
79 	struct spdk_bdev_desc	*desc;
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 	/* thread where base device is opened */
89 	struct spdk_thread	*thread;
90 };
91 
92 /*
93  * raid_bdev_io is the context part of bdev_io. It contains the information
94  * related to bdev_io for a raid bdev
95  */
96 struct raid_bdev_io {
97 	/* The raid bdev associated with this IO */
98 	struct raid_bdev *raid_bdev;
99 
100 	/* WaitQ entry, used only in waitq logic */
101 	struct spdk_bdev_io_wait_entry	waitq_entry;
102 
103 	/* Context of the original channel for this IO */
104 	struct raid_bdev_io_channel	*raid_ch;
105 
106 	/* Used for tracking progress on io requests sent to member disks. */
107 	uint64_t			base_bdev_io_remaining;
108 	uint8_t				base_bdev_io_submitted;
109 	uint8_t				base_bdev_io_status;
110 };
111 
112 /*
113  * raid_bdev is the single entity structure which contains SPDK block device
114  * and the information related to any raid bdev either configured or
115  * in configuring list. io device is created on this.
116  */
117 struct raid_bdev {
118 	/* raid bdev device, this will get registered in bdev layer */
119 	struct spdk_bdev		bdev;
120 
121 	/* link of raid bdev to link it to configured, configuring or offline list */
122 	TAILQ_ENTRY(raid_bdev)		state_link;
123 
124 	/* link of raid bdev to link it to global raid bdev list */
125 	TAILQ_ENTRY(raid_bdev)		global_link;
126 
127 	/* pointer to config file entry */
128 	struct raid_bdev_config		*config;
129 
130 	/* array of base bdev info */
131 	struct raid_base_bdev_info	*base_bdev_info;
132 
133 	/* strip size of raid bdev in blocks */
134 	uint32_t			strip_size;
135 
136 	/* strip size of raid bdev in KB */
137 	uint32_t			strip_size_kb;
138 
139 	/* strip size bit shift for optimized calculation */
140 	uint32_t			strip_size_shift;
141 
142 	/* block length bit shift for optimized calculation */
143 	uint32_t			blocklen_shift;
144 
145 	/* state of raid bdev */
146 	enum raid_bdev_state		state;
147 
148 	/* number of base bdevs comprising raid bdev  */
149 	uint8_t				num_base_bdevs;
150 
151 	/* number of base bdevs discovered */
152 	uint8_t				num_base_bdevs_discovered;
153 
154 	/* Raid Level of this raid bdev */
155 	enum raid_level			level;
156 
157 	/* Set to true if destruct is called for this raid bdev */
158 	bool				destruct_called;
159 
160 	/* Set to true if destroy of this raid bdev is started. */
161 	bool				destroy_started;
162 
163 	/* Module for RAID-level specific operations */
164 	struct raid_bdev_module		*module;
165 
166 	/* Private data for the raid module */
167 	void				*module_private;
168 };
169 
170 #define RAID_FOR_EACH_BASE_BDEV(r, i) \
171 	for (i = r->base_bdev_info; i < r->base_bdev_info + r->num_base_bdevs; i++)
172 
173 /*
174  * raid_base_bdev_config is the per base bdev data structure which contains
175  * information w.r.t to per base bdev during parsing config
176  */
177 struct raid_base_bdev_config {
178 	/* base bdev name from config file */
179 	char				*name;
180 };
181 
182 /*
183  * raid_bdev_config contains the raid bdev config related information after
184  * parsing the config file
185  */
186 struct raid_bdev_config {
187 	/* base bdev config per underlying bdev */
188 	struct raid_base_bdev_config	*base_bdev;
189 
190 	/* Points to already created raid bdev  */
191 	struct raid_bdev		*raid_bdev;
192 
193 	char				*name;
194 
195 	/* strip size of this raid bdev  in kilo bytes */
196 	uint32_t			strip_size;
197 
198 	/* number of base bdevs */
199 	uint8_t				num_base_bdevs;
200 
201 	/* raid level */
202 	enum raid_level			level;
203 
204 	TAILQ_ENTRY(raid_bdev_config)	link;
205 };
206 
207 /*
208  * raid_config is the top level structure representing the raid bdev config as read
209  * from config file for all raids
210  */
211 struct raid_config {
212 	/* raid bdev  context from config file */
213 	TAILQ_HEAD(, raid_bdev_config) raid_bdev_config_head;
214 
215 	/* total raid bdev  from config file */
216 	uint8_t total_raid_bdev;
217 };
218 
219 /*
220  * raid_bdev_io_channel is the context of spdk_io_channel for raid bdev device. It
221  * contains the relationship of raid bdev io channel with base bdev io channels.
222  */
223 struct raid_bdev_io_channel {
224 	/* Array of IO channels of base bdevs */
225 	struct spdk_io_channel	**base_channel;
226 
227 	/* Number of IO channels */
228 	uint8_t			num_channels;
229 };
230 
231 /* TAIL heads for various raid bdev lists */
232 TAILQ_HEAD(raid_configured_tailq, raid_bdev);
233 TAILQ_HEAD(raid_configuring_tailq, raid_bdev);
234 TAILQ_HEAD(raid_all_tailq, raid_bdev);
235 TAILQ_HEAD(raid_offline_tailq, raid_bdev);
236 
237 extern struct raid_configured_tailq	g_raid_bdev_configured_list;
238 extern struct raid_configuring_tailq	g_raid_bdev_configuring_list;
239 extern struct raid_all_tailq		g_raid_bdev_list;
240 extern struct raid_offline_tailq	g_raid_bdev_offline_list;
241 extern struct raid_config		g_raid_config;
242 
243 typedef void (*raid_bdev_destruct_cb)(void *cb_ctx, int rc);
244 
245 int raid_bdev_create(struct raid_bdev_config *raid_cfg);
246 int raid_bdev_add_base_devices(struct raid_bdev_config *raid_cfg);
247 void raid_bdev_remove_base_devices(struct raid_bdev_config *raid_cfg,
248 				   raid_bdev_destruct_cb cb_fn, void *cb_ctx);
249 int raid_bdev_config_add(const char *raid_name, uint32_t strip_size, uint8_t num_base_bdevs,
250 			 enum raid_level level, struct raid_bdev_config **_raid_cfg);
251 int raid_bdev_config_add_base_bdev(struct raid_bdev_config *raid_cfg,
252 				   const char *base_bdev_name, uint8_t slot);
253 void raid_bdev_config_cleanup(struct raid_bdev_config *raid_cfg);
254 struct raid_bdev_config *raid_bdev_config_find_by_name(const char *raid_name);
255 enum raid_level raid_bdev_parse_raid_level(const char *str);
256 const char *raid_bdev_level_to_str(enum raid_level level);
257 
258 /*
259  * RAID module descriptor
260  */
261 struct raid_bdev_module {
262 	/* RAID level implemented by this module */
263 	enum raid_level level;
264 
265 	/* Minimum required number of base bdevs. Must be > 0. */
266 	uint8_t base_bdevs_min;
267 
268 	/*
269 	 * Maximum number of base bdevs that can be removed without failing
270 	 * the array.
271 	 */
272 	uint8_t base_bdevs_max_degraded;
273 
274 	/*
275 	 * Called when the raid is starting, right before changing the state to
276 	 * online and registering the bdev. Parameters of the bdev like blockcnt
277 	 * should be set here.
278 	 *
279 	 * Non-zero return value will abort the startup process.
280 	 */
281 	int (*start)(struct raid_bdev *raid_bdev);
282 
283 	/*
284 	 * Called when the raid is stopping, right before changing the state to
285 	 * offline and unregistering the bdev. Optional.
286 	 */
287 	void (*stop)(struct raid_bdev *raid_bdev);
288 
289 	/* Handler for R/W requests */
290 	void (*submit_rw_request)(struct raid_bdev_io *raid_io);
291 
292 	/* Handler for requests without payload (flush, unmap). Optional. */
293 	void (*submit_null_payload_request)(struct raid_bdev_io *raid_io);
294 
295 	TAILQ_ENTRY(raid_bdev_module) link;
296 };
297 
298 void raid_bdev_module_list_add(struct raid_bdev_module *raid_module);
299 
300 #define __RAID_MODULE_REGISTER(line) __RAID_MODULE_REGISTER_(line)
301 #define __RAID_MODULE_REGISTER_(line) raid_module_register_##line
302 
303 #define RAID_MODULE_REGISTER(_module)					\
304 __attribute__((constructor)) static void				\
305 __RAID_MODULE_REGISTER(__LINE__)(void)					\
306 {									\
307     raid_bdev_module_list_add(_module);					\
308 }
309 
310 bool
311 raid_bdev_io_complete_part(struct raid_bdev_io *raid_io, uint64_t completed,
312 			   enum spdk_bdev_io_status status);
313 void
314 raid_bdev_queue_io_wait(struct raid_bdev_io *raid_io, struct spdk_bdev *bdev,
315 			struct spdk_io_channel *ch, spdk_bdev_io_wait_cb cb_fn);
316 void
317 raid_bdev_io_complete(struct raid_bdev_io *raid_io, enum spdk_bdev_io_status status);
318 
319 #endif /* SPDK_BDEV_RAID_INTERNAL_H */
320