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