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