xref: /spdk/module/bdev/raid/bdev_raid.h (revision a0d24145bf3d795cf89adc414320b138fae480ab)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2018 Intel Corporation.
3  *   All rights reserved.
4  */
5 
6 #ifndef SPDK_BDEV_RAID_INTERNAL_H
7 #define SPDK_BDEV_RAID_INTERNAL_H
8 
9 #include "spdk/bdev_module.h"
10 
11 enum raid_level {
12 	INVALID_RAID_LEVEL	= -1,
13 	RAID0			= 0,
14 	RAID1			= 1,
15 	RAID5F			= 95, /* 0x5f */
16 	CONCAT			= 99,
17 };
18 
19 /*
20  * Raid state describes the state of the raid. This raid bdev can be either in
21  * configured list or configuring list
22  */
23 enum raid_bdev_state {
24 	/* raid bdev is ready and is seen by upper layers */
25 	RAID_BDEV_STATE_ONLINE,
26 
27 	/*
28 	 * raid bdev is configuring, not all underlying bdevs are present.
29 	 * And can't be seen by upper layers.
30 	 */
31 	RAID_BDEV_STATE_CONFIGURING,
32 
33 	/*
34 	 * In offline state, raid bdev layer will complete all incoming commands without
35 	 * submitting to underlying base nvme bdevs
36 	 */
37 	RAID_BDEV_STATE_OFFLINE,
38 
39 	/* raid bdev state max, new states should be added before this */
40 	RAID_BDEV_STATE_MAX
41 };
42 
43 /*
44  * raid_base_bdev_info contains information for the base bdevs which are part of some
45  * raid. This structure contains the per base bdev information. Whatever is
46  * required per base device for raid bdev will be kept here
47  */
48 struct raid_base_bdev_info {
49 	/* name of the bdev */
50 	char			*name;
51 
52 	/* pointer to base spdk bdev */
53 	struct spdk_bdev	*bdev;
54 
55 	/* pointer to base bdev descriptor opened by raid bdev */
56 	struct spdk_bdev_desc	*desc;
57 
58 	/*
59 	 * When underlying base device calls the hot plug function on drive removal,
60 	 * this flag will be set and later after doing some processing, base device
61 	 * descriptor will be closed
62 	 */
63 	bool			remove_scheduled;
64 
65 	/* Hold the number of blocks to know how large the base bdev is resized. */
66 	uint64_t		blockcnt;
67 };
68 
69 /*
70  * raid_bdev_io is the context part of bdev_io. It contains the information
71  * related to bdev_io for a raid bdev
72  */
73 struct raid_bdev_io {
74 	/* The raid bdev associated with this IO */
75 	struct raid_bdev *raid_bdev;
76 
77 	/* WaitQ entry, used only in waitq logic */
78 	struct spdk_bdev_io_wait_entry	waitq_entry;
79 
80 	/* Context of the original channel for this IO */
81 	struct raid_bdev_io_channel	*raid_ch;
82 
83 	/* Used for tracking progress on io requests sent to member disks. */
84 	uint64_t			base_bdev_io_remaining;
85 	uint8_t				base_bdev_io_submitted;
86 	uint8_t				base_bdev_io_status;
87 
88 	/* Private data for the raid module */
89 	void				*module_private;
90 };
91 
92 /*
93  * raid_bdev is the single entity structure which contains SPDK block device
94  * and the information related to any raid bdev either configured or
95  * in configuring list. io device is created on this.
96  */
97 struct raid_bdev {
98 	/* raid bdev device, this will get registered in bdev layer */
99 	struct spdk_bdev		bdev;
100 
101 	/* link of raid bdev to link it to global raid bdev list */
102 	TAILQ_ENTRY(raid_bdev)		global_link;
103 
104 	/* array of base bdev info */
105 	struct raid_base_bdev_info	*base_bdev_info;
106 
107 	/* strip size of raid bdev in blocks */
108 	uint32_t			strip_size;
109 
110 	/* strip size of raid bdev in KB */
111 	uint32_t			strip_size_kb;
112 
113 	/* strip size bit shift for optimized calculation */
114 	uint32_t			strip_size_shift;
115 
116 	/* block length bit shift for optimized calculation */
117 	uint32_t			blocklen_shift;
118 
119 	/* state of raid bdev */
120 	enum raid_bdev_state		state;
121 
122 	/* number of base bdevs comprising raid bdev  */
123 	uint8_t				num_base_bdevs;
124 
125 	/* number of base bdevs discovered */
126 	uint8_t				num_base_bdevs_discovered;
127 
128 	/* minimum number of viable base bdevs that are required by array to operate */
129 	uint8_t				min_base_bdevs_operational;
130 
131 	/* Raid Level of this raid bdev */
132 	enum raid_level			level;
133 
134 	/* Set to true if destroy of this raid bdev is started. */
135 	bool				destroy_started;
136 
137 	/* Module for RAID-level specific operations */
138 	struct raid_bdev_module		*module;
139 
140 	/* Private data for the raid module */
141 	void				*module_private;
142 };
143 
144 #define RAID_FOR_EACH_BASE_BDEV(r, i) \
145 	for (i = r->base_bdev_info; i < r->base_bdev_info + r->num_base_bdevs; i++)
146 
147 /*
148  * raid_bdev_io_channel is the context of spdk_io_channel for raid bdev device. It
149  * contains the relationship of raid bdev io channel with base bdev io channels.
150  */
151 struct raid_bdev_io_channel {
152 	/* Array of IO channels of base bdevs */
153 	struct spdk_io_channel	**base_channel;
154 
155 	/* Number of IO channels */
156 	uint8_t			num_channels;
157 
158 	/* Private raid module IO channel */
159 	struct spdk_io_channel	*module_channel;
160 };
161 
162 /* TAIL head for raid bdev list */
163 TAILQ_HEAD(raid_all_tailq, raid_bdev);
164 
165 extern struct raid_all_tailq		g_raid_bdev_list;
166 
167 typedef void (*raid_bdev_destruct_cb)(void *cb_ctx, int rc);
168 
169 int raid_bdev_create(const char *name, uint32_t strip_size, uint8_t num_base_bdevs,
170 		     enum raid_level level, struct raid_bdev **raid_bdev_out);
171 void raid_bdev_delete(struct raid_bdev *raid_bdev, raid_bdev_destruct_cb cb_fn, void *cb_ctx);
172 int raid_bdev_add_base_device(struct raid_bdev *raid_bdev, const char *name, uint8_t slot);
173 struct raid_bdev *raid_bdev_find_by_name(const char *name);
174 enum raid_level raid_bdev_str_to_level(const char *str);
175 const char *raid_bdev_level_to_str(enum raid_level level);
176 enum raid_bdev_state raid_bdev_str_to_state(const char *str);
177 const char *raid_bdev_state_to_str(enum raid_bdev_state state);
178 void raid_bdev_write_info_json(struct raid_bdev *raid_bdev, struct spdk_json_write_ctx *w);
179 
180 /*
181  * RAID module descriptor
182  */
183 struct raid_bdev_module {
184 	/* RAID level implemented by this module */
185 	enum raid_level level;
186 
187 	/* Minimum required number of base bdevs. Must be > 0. */
188 	uint8_t base_bdevs_min;
189 
190 	/*
191 	 * RAID constraint. Determines number of base bdevs that can be removed
192 	 * without failing the array.
193 	 */
194 	struct {
195 		enum {
196 			CONSTRAINT_UNSET = 0,
197 			CONSTRAINT_MAX_BASE_BDEVS_REMOVED,
198 			CONSTRAINT_MIN_BASE_BDEVS_OPERATIONAL,
199 		} type;
200 		uint8_t value;
201 	} base_bdevs_constraint;
202 
203 	/*
204 	 * Called when the raid is starting, right before changing the state to
205 	 * online and registering the bdev. Parameters of the bdev like blockcnt
206 	 * should be set here.
207 	 *
208 	 * Non-zero return value will abort the startup process.
209 	 */
210 	int (*start)(struct raid_bdev *raid_bdev);
211 
212 	/*
213 	 * Called when the raid is stopping, right before changing the state to
214 	 * offline and unregistering the bdev. Optional.
215 	 *
216 	 * The function should return false if it is asynchronous. Then, after
217 	 * the async operation has completed and the module is fully stopped
218 	 * raid_bdev_module_stop_done() must be called.
219 	 */
220 	bool (*stop)(struct raid_bdev *raid_bdev);
221 
222 	/* Handler for R/W requests */
223 	void (*submit_rw_request)(struct raid_bdev_io *raid_io);
224 
225 	/* Handler for requests without payload (flush, unmap). Optional. */
226 	void (*submit_null_payload_request)(struct raid_bdev_io *raid_io);
227 
228 	/*
229 	 * Called when the bdev's IO channel is created to get the module's private IO channel.
230 	 * Optional.
231 	 */
232 	struct spdk_io_channel *(*get_io_channel)(struct raid_bdev *raid_bdev);
233 
234 	/*
235 	 * Called when a base_bdev is resized to resize the raid if the condition
236 	 * is satisfied.
237 	 */
238 	void (*resize)(struct raid_bdev *raid_bdev);
239 
240 	TAILQ_ENTRY(raid_bdev_module) link;
241 };
242 
243 void raid_bdev_module_list_add(struct raid_bdev_module *raid_module);
244 
245 #define __RAID_MODULE_REGISTER(line) __RAID_MODULE_REGISTER_(line)
246 #define __RAID_MODULE_REGISTER_(line) raid_module_register_##line
247 
248 #define RAID_MODULE_REGISTER(_module)					\
249 __attribute__((constructor)) static void				\
250 __RAID_MODULE_REGISTER(__LINE__)(void)					\
251 {									\
252     raid_bdev_module_list_add(_module);					\
253 }
254 
255 bool raid_bdev_io_complete_part(struct raid_bdev_io *raid_io, uint64_t completed,
256 				enum spdk_bdev_io_status status);
257 void raid_bdev_queue_io_wait(struct raid_bdev_io *raid_io, struct spdk_bdev *bdev,
258 			     struct spdk_io_channel *ch, spdk_bdev_io_wait_cb cb_fn);
259 void raid_bdev_io_complete(struct raid_bdev_io *raid_io, enum spdk_bdev_io_status status);
260 void raid_bdev_module_stop_done(struct raid_bdev *raid_bdev);
261 
262 #endif /* SPDK_BDEV_RAID_INTERNAL_H */
263