xref: /spdk/module/bdev/raid/bdev_raid.h (revision 9889ab2dc80e40dae92dcef361d53dcba722043d)
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 	/* Original channel for this IO, used in queuing logic */
97 	struct spdk_io_channel		*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 
158 /*
159  * raid_base_bdev_config is the per base bdev data structure which contains
160  * information w.r.t to per base bdev during parsing config
161  */
162 struct raid_base_bdev_config {
163 	/* base bdev name from config file */
164 	char				*name;
165 };
166 
167 /*
168  * raid_bdev_config contains the raid bdev  config related information after
169  * parsing the config file
170  */
171 struct raid_bdev_config {
172 	/* base bdev config per underlying bdev */
173 	struct raid_base_bdev_config	*base_bdev;
174 
175 	/* Points to already created raid bdev  */
176 	struct raid_bdev		*raid_bdev;
177 
178 	char				*name;
179 
180 	/* strip size of this raid bdev  in kilo bytes */
181 	uint32_t			strip_size;
182 
183 	/* number of base bdevs */
184 	uint8_t				num_base_bdevs;
185 
186 	/* raid level */
187 	enum raid_level			level;
188 
189 	TAILQ_ENTRY(raid_bdev_config)	link;
190 };
191 
192 /*
193  * raid_config is the top level structure representing the raid bdev config as read
194  * from config file for all raids
195  */
196 struct raid_config {
197 	/* raid bdev  context from config file */
198 	TAILQ_HEAD(, raid_bdev_config) raid_bdev_config_head;
199 
200 	/* total raid bdev  from config file */
201 	uint8_t total_raid_bdev;
202 };
203 
204 /*
205  * raid_bdev_io_channel is the context of spdk_io_channel for raid bdev device. It
206  * contains the relationship of raid bdev io channel with base bdev io channels.
207  */
208 struct raid_bdev_io_channel {
209 	/* Array of IO channels of base bdevs */
210 	struct spdk_io_channel	**base_channel;
211 
212 	/* Number of IO channels */
213 	uint8_t			num_channels;
214 };
215 
216 /* TAIL heads for various raid bdev lists */
217 TAILQ_HEAD(raid_configured_tailq, raid_bdev);
218 TAILQ_HEAD(raid_configuring_tailq, raid_bdev);
219 TAILQ_HEAD(raid_all_tailq, raid_bdev);
220 TAILQ_HEAD(raid_offline_tailq, raid_bdev);
221 
222 extern struct raid_configured_tailq	g_raid_bdev_configured_list;
223 extern struct raid_configuring_tailq	g_raid_bdev_configuring_list;
224 extern struct raid_all_tailq		g_raid_bdev_list;
225 extern struct raid_offline_tailq	g_raid_bdev_offline_list;
226 extern struct raid_config		g_raid_config;
227 
228 typedef void (*raid_bdev_destruct_cb)(void *cb_ctx, int rc);
229 
230 int raid_bdev_create(struct raid_bdev_config *raid_cfg);
231 int raid_bdev_add_base_devices(struct raid_bdev_config *raid_cfg);
232 void raid_bdev_remove_base_devices(struct raid_bdev_config *raid_cfg,
233 				   raid_bdev_destruct_cb cb_fn, void *cb_ctx);
234 int raid_bdev_config_add(const char *raid_name, uint32_t strip_size, uint8_t num_base_bdevs,
235 			 enum raid_level level, struct raid_bdev_config **_raid_cfg);
236 int raid_bdev_config_add_base_bdev(struct raid_bdev_config *raid_cfg,
237 				   const char *base_bdev_name, uint8_t slot);
238 void raid_bdev_config_cleanup(struct raid_bdev_config *raid_cfg);
239 struct raid_bdev_config *raid_bdev_config_find_by_name(const char *raid_name);
240 enum raid_level raid_bdev_parse_raid_level(const char *str);
241 const char *raid_bdev_level_to_str(enum raid_level level);
242 
243 void
244 raid0_start_rw_request(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io);
245 void
246 raid0_submit_null_payload_request(void *_bdev_io);
247 void
248 raid_bdev_base_io_completion(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg);
249 void
250 raid_bdev_queue_io_wait(struct spdk_bdev_io *raid_bdev_io, uint8_t pd_idx,
251 			spdk_bdev_io_wait_cb cb_fn, int ret);
252 
253 #endif /* SPDK_BDEV_RAID_INTERNAL_H */
254