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