xref: /spdk/module/bdev/raid/bdev_raid.h (revision 139e4c078310061c953d79d3dde96db40179233e)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef SPDK_BDEV_RAID_INTERNAL_H
35 #define SPDK_BDEV_RAID_INTERNAL_H
36 
37 #include "spdk/bdev_module.h"
38 
39 /*
40  * Raid state describes the state of the raid. This raid bdev can be either in
41  * configured list or configuring list
42  */
43 enum raid_bdev_state {
44 	/* raid bdev is ready and is seen by upper layers */
45 	RAID_BDEV_STATE_ONLINE,
46 
47 	/*
48 	 * raid bdev is configuring, not all underlying bdevs are present.
49 	 * And can't be seen by upper layers.
50 	 */
51 	RAID_BDEV_STATE_CONFIGURING,
52 
53 	/*
54 	 * In offline state, raid bdev layer will complete all incoming commands without
55 	 * submitting to underlying base nvme bdevs
56 	 */
57 	RAID_BDEV_STATE_OFFLINE,
58 
59 	/* raid bdev max, new states should be added before this */
60 	RAID_BDEV_MAX
61 };
62 
63 /*
64  * raid_base_bdev_info contains information for the base bdevs which are part of some
65  * raid. This structure contains the per base bdev information. Whatever is
66  * required per base device for raid bdev will be kept here
67  */
68 struct raid_base_bdev_info {
69 	/* pointer to base spdk bdev */
70 	struct spdk_bdev	*bdev;
71 
72 	/* pointer to base bdev descriptor opened by raid bdev */
73 	struct spdk_bdev_desc	*desc;
74 
75 	/*
76 	 * When underlying base device calls the hot plug function on drive removal,
77 	 * this flag will be set and later after doing some processing, base device
78 	 * descriptor will be closed
79 	 */
80 	bool			remove_scheduled;
81 };
82 
83 /*
84  * raid_bdev is the single entity structure which contains SPDK block device
85  * and the information related to any raid bdev either configured or
86  * in configuring list. io device is created on this.
87  */
88 struct raid_bdev {
89 	/* raid bdev device, this will get registered in bdev layer */
90 	struct spdk_bdev		bdev;
91 
92 	/* link of raid bdev to link it to configured, configuring or offline list */
93 	TAILQ_ENTRY(raid_bdev)		state_link;
94 
95 	/* link of raid bdev to link it to global raid bdev list */
96 	TAILQ_ENTRY(raid_bdev)		global_link;
97 
98 	/* pointer to config file entry */
99 	struct raid_bdev_config		*config;
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 	/* Raid Level of this raid bdev */
126 	uint8_t				raid_level;
127 
128 	/* Set to true if destruct is called for this raid bdev */
129 	bool				destruct_called;
130 
131 	/* Set to true if destroy of this raid bdev is started. */
132 	bool				destroy_started;
133 };
134 
135 /*
136  * raid_bdev_io is the context part of bdev_io. It contains the information
137  * related to bdev_io for a raid bdev
138  */
139 struct raid_bdev_io {
140 	/* WaitQ entry, used only in waitq logic */
141 	struct spdk_bdev_io_wait_entry	waitq_entry;
142 
143 	/* Original channel for this IO, used in queuing logic */
144 	struct spdk_io_channel		*ch;
145 
146 	/* Used for tracking progress on io requests sent to member disks. */
147 	uint8_t				base_bdev_io_submitted;
148 	uint8_t				base_bdev_io_completed;
149 	uint8_t				base_bdev_io_expected;
150 	uint8_t				base_bdev_io_status;
151 };
152 
153 /*
154  * raid_base_bdev_config is the per base bdev data structure which contains
155  * information w.r.t to per base bdev during parsing config
156  */
157 struct raid_base_bdev_config {
158 	/* base bdev name from config file */
159 	char				*name;
160 };
161 
162 /*
163  * raid_bdev_config contains the raid bdev  config related information after
164  * parsing the config file
165  */
166 struct raid_bdev_config {
167 	/* base bdev config per underlying bdev */
168 	struct raid_base_bdev_config  *base_bdev;
169 
170 	/* Points to already created raid bdev  */
171 	struct raid_bdev              *raid_bdev;
172 
173 	char                          *name;
174 
175 	/* strip size of this raid bdev  in kilo bytes */
176 	uint32_t                      strip_size;
177 
178 	/* number of base bdevs */
179 	uint8_t                       num_base_bdevs;
180 
181 	/* raid level */
182 	uint8_t                       raid_level;
183 
184 	TAILQ_ENTRY(raid_bdev_config) link;
185 };
186 
187 /*
188  * raid_config is the top level structure representing the raid bdev config as read
189  * from config file for all raids
190  */
191 struct raid_config {
192 	/* raid bdev  context from config file */
193 	TAILQ_HEAD(, raid_bdev_config) raid_bdev_config_head;
194 
195 	/* total raid bdev  from config file */
196 	uint8_t total_raid_bdev;
197 };
198 
199 /*
200  * raid_bdev_io_channel is the context of spdk_io_channel for raid bdev device. It
201  * contains the relationship of raid bdev io channel with base bdev io channels.
202  */
203 struct raid_bdev_io_channel {
204 	/* Array of IO channels of base bdevs */
205 	struct spdk_io_channel	**base_channel;
206 
207 	/* Number of IO channels */
208 	uint8_t			num_channels;
209 };
210 
211 /* TAIL heads for various raid bdev lists */
212 TAILQ_HEAD(raid_configured_tailq, raid_bdev);
213 TAILQ_HEAD(raid_configuring_tailq, raid_bdev);
214 TAILQ_HEAD(raid_all_tailq, raid_bdev);
215 TAILQ_HEAD(raid_offline_tailq, raid_bdev);
216 
217 extern struct raid_configured_tailq	g_raid_bdev_configured_list;
218 extern struct raid_configuring_tailq	g_raid_bdev_configuring_list;
219 extern struct raid_all_tailq		g_raid_bdev_list;
220 extern struct raid_offline_tailq	g_raid_bdev_offline_list;
221 extern struct raid_config		g_raid_config;
222 
223 typedef void (*raid_bdev_destruct_cb)(void *cb_ctx, int rc);
224 
225 int raid_bdev_create(struct raid_bdev_config *raid_cfg);
226 int raid_bdev_add_base_devices(struct raid_bdev_config *raid_cfg);
227 void raid_bdev_remove_base_devices(struct raid_bdev_config *raid_cfg,
228 				   raid_bdev_destruct_cb cb_fn, void *cb_ctx);
229 int raid_bdev_config_add(const char *raid_name, uint32_t strip_size, uint8_t num_base_bdevs,
230 			 uint8_t raid_level, struct raid_bdev_config **_raid_cfg);
231 int raid_bdev_config_add_base_bdev(struct raid_bdev_config *raid_cfg,
232 				   const char *base_bdev_name, uint8_t slot);
233 void raid_bdev_config_cleanup(struct raid_bdev_config *raid_cfg);
234 struct raid_bdev_config *raid_bdev_config_find_by_name(const char *raid_name);
235 
236 #endif /* SPDK_BDEV_RAID_INTERNAL_H */
237