xref: /spdk/module/bdev/raid/bdev_raid.h (revision 6665722214804000d0052fd47fe5ac5ed6f5180b)
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 
66 /*
67  * raid_bdev_io is the context part of bdev_io. It contains the information
68  * related to bdev_io for a raid bdev
69  */
70 struct raid_bdev_io {
71 	/* The raid bdev associated with this IO */
72 	struct raid_bdev *raid_bdev;
73 
74 	/* WaitQ entry, used only in waitq logic */
75 	struct spdk_bdev_io_wait_entry	waitq_entry;
76 
77 	/* Context of the original channel for this IO */
78 	struct raid_bdev_io_channel	*raid_ch;
79 
80 	/* Used for tracking progress on io requests sent to member disks. */
81 	uint64_t			base_bdev_io_remaining;
82 	uint8_t				base_bdev_io_submitted;
83 	uint8_t				base_bdev_io_status;
84 
85 	/* Private data for the raid module */
86 	void				*module_private;
87 };
88 
89 /*
90  * raid_bdev is the single entity structure which contains SPDK block device
91  * and the information related to any raid bdev either configured or
92  * in configuring list. io device is created on this.
93  */
94 struct raid_bdev {
95 	/* raid bdev device, this will get registered in bdev layer */
96 	struct spdk_bdev		bdev;
97 
98 	/* link of raid bdev to link it to global raid bdev list */
99 	TAILQ_ENTRY(raid_bdev)		global_link;
100 
101 	/* array of base bdev info */
102 	struct raid_base_bdev_info	*base_bdev_info;
103 
104 	/* strip size of raid bdev in blocks */
105 	uint32_t			strip_size;
106 
107 	/* strip size of raid bdev in KB */
108 	uint32_t			strip_size_kb;
109 
110 	/* strip size bit shift for optimized calculation */
111 	uint32_t			strip_size_shift;
112 
113 	/* block length bit shift for optimized calculation */
114 	uint32_t			blocklen_shift;
115 
116 	/* state of raid bdev */
117 	enum raid_bdev_state		state;
118 
119 	/* number of base bdevs comprising raid bdev  */
120 	uint8_t				num_base_bdevs;
121 
122 	/* number of base bdevs discovered */
123 	uint8_t				num_base_bdevs_discovered;
124 
125 	/* minimum number of viable base bdevs that are required by array to operate */
126 	uint8_t				min_base_bdevs_operational;
127 
128 	/* Raid Level of this raid bdev */
129 	enum raid_level			level;
130 
131 	/* Set to true if destroy of this raid bdev is started. */
132 	bool				destroy_started;
133 
134 	/* Module for RAID-level specific operations */
135 	struct raid_bdev_module		*module;
136 
137 	/* Private data for the raid module */
138 	void				*module_private;
139 };
140 
141 #define RAID_FOR_EACH_BASE_BDEV(r, i) \
142 	for (i = r->base_bdev_info; i < r->base_bdev_info + r->num_base_bdevs; i++)
143 
144 /*
145  * raid_bdev_io_channel is the context of spdk_io_channel for raid bdev device. It
146  * contains the relationship of raid bdev io channel with base bdev io channels.
147  */
148 struct raid_bdev_io_channel {
149 	/* Array of IO channels of base bdevs */
150 	struct spdk_io_channel	**base_channel;
151 
152 	/* Number of IO channels */
153 	uint8_t			num_channels;
154 
155 	/* Private raid module IO channel */
156 	struct spdk_io_channel	*module_channel;
157 };
158 
159 /* TAIL head for raid bdev list */
160 TAILQ_HEAD(raid_all_tailq, raid_bdev);
161 
162 extern struct raid_all_tailq		g_raid_bdev_list;
163 
164 typedef void (*raid_bdev_destruct_cb)(void *cb_ctx, int rc);
165 
166 int raid_bdev_create(const char *name, uint32_t strip_size, uint8_t num_base_bdevs,
167 		     enum raid_level level, struct raid_bdev **raid_bdev_out);
168 void raid_bdev_delete(struct raid_bdev *raid_bdev, raid_bdev_destruct_cb cb_fn, void *cb_ctx);
169 int raid_bdev_add_base_device(struct raid_bdev *raid_bdev, const char *name, uint8_t slot);
170 struct raid_bdev *raid_bdev_find_by_name(const char *name);
171 enum raid_level raid_bdev_str_to_level(const char *str);
172 const char *raid_bdev_level_to_str(enum raid_level level);
173 enum raid_bdev_state raid_bdev_str_to_state(const char *str);
174 const char *raid_bdev_state_to_str(enum raid_bdev_state state);
175 void raid_bdev_write_info_json(struct raid_bdev *raid_bdev, struct spdk_json_write_ctx *w);
176 
177 /*
178  * RAID module descriptor
179  */
180 struct raid_bdev_module {
181 	/* RAID level implemented by this module */
182 	enum raid_level level;
183 
184 	/* Minimum required number of base bdevs. Must be > 0. */
185 	uint8_t base_bdevs_min;
186 
187 	/*
188 	 * RAID constraint. Determines number of base bdevs that can be removed
189 	 * without failing the array.
190 	 */
191 	struct {
192 		enum {
193 			CONSTRAINT_UNSET = 0,
194 			CONSTRAINT_MAX_BASE_BDEVS_REMOVED,
195 			CONSTRAINT_MIN_BASE_BDEVS_OPERATIONAL,
196 		} type;
197 		uint8_t value;
198 	} base_bdevs_constraint;
199 
200 	/*
201 	 * Called when the raid is starting, right before changing the state to
202 	 * online and registering the bdev. Parameters of the bdev like blockcnt
203 	 * should be set here.
204 	 *
205 	 * Non-zero return value will abort the startup process.
206 	 */
207 	int (*start)(struct raid_bdev *raid_bdev);
208 
209 	/*
210 	 * Called when the raid is stopping, right before changing the state to
211 	 * offline and unregistering the bdev. Optional.
212 	 *
213 	 * The function should return false if it is asynchronous. Then, after
214 	 * the async operation has completed and the module is fully stopped
215 	 * raid_bdev_module_stop_done() must be called.
216 	 */
217 	bool (*stop)(struct raid_bdev *raid_bdev);
218 
219 	/* Handler for R/W requests */
220 	void (*submit_rw_request)(struct raid_bdev_io *raid_io);
221 
222 	/* Handler for requests without payload (flush, unmap). Optional. */
223 	void (*submit_null_payload_request)(struct raid_bdev_io *raid_io);
224 
225 	/*
226 	 * Called when the bdev's IO channel is created to get the module's private IO channel.
227 	 * Optional.
228 	 */
229 	struct spdk_io_channel *(*get_io_channel)(struct raid_bdev *raid_bdev);
230 
231 	TAILQ_ENTRY(raid_bdev_module) link;
232 };
233 
234 void raid_bdev_module_list_add(struct raid_bdev_module *raid_module);
235 
236 #define __RAID_MODULE_REGISTER(line) __RAID_MODULE_REGISTER_(line)
237 #define __RAID_MODULE_REGISTER_(line) raid_module_register_##line
238 
239 #define RAID_MODULE_REGISTER(_module)					\
240 __attribute__((constructor)) static void				\
241 __RAID_MODULE_REGISTER(__LINE__)(void)					\
242 {									\
243     raid_bdev_module_list_add(_module);					\
244 }
245 
246 bool raid_bdev_io_complete_part(struct raid_bdev_io *raid_io, uint64_t completed,
247 				enum spdk_bdev_io_status status);
248 void raid_bdev_queue_io_wait(struct raid_bdev_io *raid_io, struct spdk_bdev *bdev,
249 			     struct spdk_io_channel *ch, spdk_bdev_io_wait_cb cb_fn);
250 void raid_bdev_io_complete(struct raid_bdev_io *raid_io, enum spdk_bdev_io_status status);
251 void raid_bdev_module_stop_done(struct raid_bdev *raid_bdev);
252 
253 #endif /* SPDK_BDEV_RAID_INTERNAL_H */
254