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