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