xref: /spdk/module/bdev/raid/bdev_raid.h (revision 307b8c112ffd90a26d53dd15fad67bd9038ef526)
1  /*   SPDX-License-Identifier: BSD-3-Clause
2   *   Copyright (c) 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 max, new states should be added before this */
39  	RAID_BDEV_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  	/* pointer to base spdk bdev */
49  	struct spdk_bdev	*bdev;
50  
51  	/* pointer to base bdev descriptor opened by raid bdev */
52  	struct spdk_bdev_desc	*desc;
53  
54  	/*
55  	 * When underlying base device calls the hot plug function on drive removal,
56  	 * this flag will be set and later after doing some processing, base device
57  	 * descriptor will be closed
58  	 */
59  	bool			remove_scheduled;
60  
61  	/* thread where base device is opened */
62  	struct spdk_thread	*thread;
63  };
64  
65  /*
66   * raid_bdev_io is the context part of bdev_io. It contains the information
67   * related to bdev_io for a raid bdev
68   */
69  struct raid_bdev_io {
70  	/* The raid bdev associated with this IO */
71  	struct raid_bdev *raid_bdev;
72  
73  	/* WaitQ entry, used only in waitq logic */
74  	struct spdk_bdev_io_wait_entry	waitq_entry;
75  
76  	/* Context of the original channel for this IO */
77  	struct raid_bdev_io_channel	*raid_ch;
78  
79  	/* Used for tracking progress on io requests sent to member disks. */
80  	uint64_t			base_bdev_io_remaining;
81  	uint8_t				base_bdev_io_submitted;
82  	uint8_t				base_bdev_io_status;
83  
84  	/* Private data for the raid module */
85  	void				*module_private;
86  };
87  
88  /*
89   * raid_bdev is the single entity structure which contains SPDK block device
90   * and the information related to any raid bdev either configured or
91   * in configuring list. io device is created on this.
92   */
93  struct raid_bdev {
94  	/* raid bdev device, this will get registered in bdev layer */
95  	struct spdk_bdev		bdev;
96  
97  	/* link of raid bdev to link it to configured, configuring or offline list */
98  	TAILQ_ENTRY(raid_bdev)		state_link;
99  
100  	/* link of raid bdev to link it to global raid bdev list */
101  	TAILQ_ENTRY(raid_bdev)		global_link;
102  
103  	/* pointer to config file entry */
104  	struct raid_bdev_config		*config;
105  
106  	/* array of base bdev info */
107  	struct raid_base_bdev_info	*base_bdev_info;
108  
109  	/* strip size of raid bdev in blocks */
110  	uint32_t			strip_size;
111  
112  	/* strip size of raid bdev in KB */
113  	uint32_t			strip_size_kb;
114  
115  	/* strip size bit shift for optimized calculation */
116  	uint32_t			strip_size_shift;
117  
118  	/* block length bit shift for optimized calculation */
119  	uint32_t			blocklen_shift;
120  
121  	/* state of raid bdev */
122  	enum raid_bdev_state		state;
123  
124  	/* number of base bdevs comprising raid bdev  */
125  	uint8_t				num_base_bdevs;
126  
127  	/* number of base bdevs discovered */
128  	uint8_t				num_base_bdevs_discovered;
129  
130  	/* Raid Level of this raid bdev */
131  	enum raid_level			level;
132  
133  	/* Set to true if destruct is called for this raid bdev */
134  	bool				destruct_called;
135  
136  	/* Set to true if destroy of this raid bdev is started. */
137  	bool				destroy_started;
138  
139  	/* Module for RAID-level specific operations */
140  	struct raid_bdev_module		*module;
141  
142  	/* Private data for the raid module */
143  	void				*module_private;
144  };
145  
146  #define RAID_FOR_EACH_BASE_BDEV(r, i) \
147  	for (i = r->base_bdev_info; i < r->base_bdev_info + r->num_base_bdevs; i++)
148  
149  /*
150   * raid_base_bdev_config is the per base bdev data structure which contains
151   * information w.r.t to per base bdev during parsing config
152   */
153  struct raid_base_bdev_config {
154  	/* base bdev name from config file */
155  	char				*name;
156  };
157  
158  /*
159   * raid_bdev_config contains the raid bdev config related information after
160   * parsing the config file
161   */
162  struct raid_bdev_config {
163  	/* base bdev config per underlying bdev */
164  	struct raid_base_bdev_config	*base_bdev;
165  
166  	/* Points to already created raid bdev  */
167  	struct raid_bdev		*raid_bdev;
168  
169  	char				*name;
170  
171  	/* strip size of this raid bdev in KB */
172  	uint32_t			strip_size;
173  
174  	/* number of base bdevs */
175  	uint8_t				num_base_bdevs;
176  
177  	/* raid level */
178  	enum raid_level			level;
179  
180  	TAILQ_ENTRY(raid_bdev_config)	link;
181  };
182  
183  /*
184   * raid_config is the top level structure representing the raid bdev config as read
185   * from config file for all raids
186   */
187  struct raid_config {
188  	/* raid bdev  context from config file */
189  	TAILQ_HEAD(, raid_bdev_config) raid_bdev_config_head;
190  
191  	/* total raid bdev  from config file */
192  	uint8_t total_raid_bdev;
193  };
194  
195  /*
196   * raid_bdev_io_channel is the context of spdk_io_channel for raid bdev device. It
197   * contains the relationship of raid bdev io channel with base bdev io channels.
198   */
199  struct raid_bdev_io_channel {
200  	/* Array of IO channels of base bdevs */
201  	struct spdk_io_channel	**base_channel;
202  
203  	/* Number of IO channels */
204  	uint8_t			num_channels;
205  
206  	/* Private raid module IO channel */
207  	struct spdk_io_channel	*module_channel;
208  };
209  
210  /* TAIL heads for various raid bdev lists */
211  TAILQ_HEAD(raid_configured_tailq, raid_bdev);
212  TAILQ_HEAD(raid_configuring_tailq, raid_bdev);
213  TAILQ_HEAD(raid_all_tailq, raid_bdev);
214  TAILQ_HEAD(raid_offline_tailq, raid_bdev);
215  
216  extern struct raid_configured_tailq	g_raid_bdev_configured_list;
217  extern struct raid_configuring_tailq	g_raid_bdev_configuring_list;
218  extern struct raid_all_tailq		g_raid_bdev_list;
219  extern struct raid_offline_tailq	g_raid_bdev_offline_list;
220  extern struct raid_config		g_raid_config;
221  
222  typedef void (*raid_bdev_destruct_cb)(void *cb_ctx, int rc);
223  
224  int raid_bdev_create(struct raid_bdev_config *raid_cfg);
225  int raid_bdev_add_base_devices(struct raid_bdev_config *raid_cfg);
226  void raid_bdev_remove_base_devices(struct raid_bdev_config *raid_cfg,
227  				   raid_bdev_destruct_cb cb_fn, void *cb_ctx);
228  int raid_bdev_config_add(const char *raid_name, uint32_t strip_size, uint8_t num_base_bdevs,
229  			 enum raid_level level, struct raid_bdev_config **_raid_cfg);
230  int raid_bdev_config_add_base_bdev(struct raid_bdev_config *raid_cfg,
231  				   const char *base_bdev_name, uint8_t slot);
232  void raid_bdev_config_cleanup(struct raid_bdev_config *raid_cfg);
233  struct raid_bdev_config *raid_bdev_config_find_by_name(const char *raid_name);
234  enum raid_level raid_bdev_parse_raid_level(const char *str);
235  const char *raid_bdev_level_to_str(enum raid_level level);
236  
237  /*
238   * RAID module descriptor
239   */
240  struct raid_bdev_module {
241  	/* RAID level implemented by this module */
242  	enum raid_level level;
243  
244  	/* Minimum required number of base bdevs. Must be > 0. */
245  	uint8_t base_bdevs_min;
246  
247  	/*
248  	 * Maximum number of base bdevs that can be removed without failing
249  	 * the array.
250  	 */
251  	uint8_t base_bdevs_max_degraded;
252  
253  	/*
254  	 * Called when the raid is starting, right before changing the state to
255  	 * online and registering the bdev. Parameters of the bdev like blockcnt
256  	 * should be set here.
257  	 *
258  	 * Non-zero return value will abort the startup process.
259  	 */
260  	int (*start)(struct raid_bdev *raid_bdev);
261  
262  	/*
263  	 * Called when the raid is stopping, right before changing the state to
264  	 * offline and unregistering the bdev. Optional.
265  	 *
266  	 * The function should return false if it is asynchronous. Then, after
267  	 * the async operation has completed and the module is fully stopped
268  	 * raid_bdev_module_stop_done() must be called.
269  	 */
270  	bool (*stop)(struct raid_bdev *raid_bdev);
271  
272  	/* Handler for R/W requests */
273  	void (*submit_rw_request)(struct raid_bdev_io *raid_io);
274  
275  	/* Handler for requests without payload (flush, unmap). Optional. */
276  	void (*submit_null_payload_request)(struct raid_bdev_io *raid_io);
277  
278  	/*
279  	 * Called when the bdev's IO channel is created to get the module's private IO channel.
280  	 * Optional.
281  	 */
282  	struct spdk_io_channel *(*get_io_channel)(struct raid_bdev *raid_bdev);
283  
284  	TAILQ_ENTRY(raid_bdev_module) link;
285  };
286  
287  void raid_bdev_module_list_add(struct raid_bdev_module *raid_module);
288  
289  #define __RAID_MODULE_REGISTER(line) __RAID_MODULE_REGISTER_(line)
290  #define __RAID_MODULE_REGISTER_(line) raid_module_register_##line
291  
292  #define RAID_MODULE_REGISTER(_module)					\
293  __attribute__((constructor)) static void				\
294  __RAID_MODULE_REGISTER(__LINE__)(void)					\
295  {									\
296      raid_bdev_module_list_add(_module);					\
297  }
298  
299  bool raid_bdev_io_complete_part(struct raid_bdev_io *raid_io, uint64_t completed,
300  				enum spdk_bdev_io_status status);
301  void raid_bdev_queue_io_wait(struct raid_bdev_io *raid_io, struct spdk_bdev *bdev,
302  			     struct spdk_io_channel *ch, spdk_bdev_io_wait_cb cb_fn);
303  void raid_bdev_io_complete(struct raid_bdev_io *raid_io, enum spdk_bdev_io_status status);
304  void raid_bdev_module_stop_done(struct raid_bdev *raid_bdev);
305  
306  #endif /* SPDK_BDEV_RAID_INTERNAL_H */
307