xref: /freebsd/sys/geom/raid/g_raid.c (revision 2d9c41daac0b1e7f35a88ee187ae98a233a30267)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org>
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  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/bio.h>
32 #include <sys/eventhandler.h>
33 #include <sys/kernel.h>
34 #include <sys/kthread.h>
35 #include <sys/limits.h>
36 #include <sys/lock.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/mutex.h>
40 #include <sys/proc.h>
41 #include <sys/reboot.h>
42 #include <sys/sbuf.h>
43 #include <sys/sched.h>
44 #include <sys/sysctl.h>
45 
46 #include <vm/uma.h>
47 
48 #include <geom/geom.h>
49 #include <geom/geom_dbg.h>
50 #include <geom/geom_disk.h>
51 #include <geom/raid/g_raid.h>
52 #include "g_raid_md_if.h"
53 #include "g_raid_tr_if.h"
54 
55 static MALLOC_DEFINE(M_RAID, "raid_data", "GEOM_RAID Data");
56 
57 SYSCTL_DECL(_kern_geom);
58 SYSCTL_NODE(_kern_geom, OID_AUTO, raid, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
59     "GEOM_RAID stuff");
60 int g_raid_enable = 1;
61 SYSCTL_INT(_kern_geom_raid, OID_AUTO, enable, CTLFLAG_RWTUN,
62     &g_raid_enable, 0, "Enable on-disk metadata taste");
63 u_int g_raid_aggressive_spare = 0;
64 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, aggressive_spare, CTLFLAG_RWTUN,
65     &g_raid_aggressive_spare, 0, "Use disks without metadata as spare");
66 u_int g_raid_debug = 0;
67 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, debug, CTLFLAG_RWTUN, &g_raid_debug, 0,
68     "Debug level");
69 int g_raid_read_err_thresh = 10;
70 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, read_err_thresh, CTLFLAG_RWTUN,
71     &g_raid_read_err_thresh, 0,
72     "Number of read errors equated to disk failure");
73 u_int g_raid_start_timeout = 30;
74 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, start_timeout, CTLFLAG_RWTUN,
75     &g_raid_start_timeout, 0,
76     "Time to wait for all array components");
77 static u_int g_raid_clean_time = 5;
78 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, clean_time, CTLFLAG_RWTUN,
79     &g_raid_clean_time, 0, "Mark volume as clean when idling");
80 static u_int g_raid_disconnect_on_failure = 1;
81 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, disconnect_on_failure, CTLFLAG_RWTUN,
82     &g_raid_disconnect_on_failure, 0, "Disconnect component on I/O failure.");
83 static u_int g_raid_name_format = 0;
84 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, name_format, CTLFLAG_RWTUN,
85     &g_raid_name_format, 0, "Providers name format.");
86 static u_int g_raid_idle_threshold = 1000000;
87 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, idle_threshold, CTLFLAG_RWTUN,
88     &g_raid_idle_threshold, 1000000,
89     "Time in microseconds to consider a volume idle.");
90 
91 #define	MSLEEP(rv, ident, mtx, priority, wmesg, timeout)	do {	\
92 	G_RAID_DEBUG(4, "%s: Sleeping %p.", __func__, (ident));		\
93 	rv = msleep((ident), (mtx), (priority), (wmesg), (timeout));	\
94 	G_RAID_DEBUG(4, "%s: Woken up %p.", __func__, (ident));		\
95 } while (0)
96 
97 LIST_HEAD(, g_raid_md_class) g_raid_md_classes =
98     LIST_HEAD_INITIALIZER(g_raid_md_classes);
99 
100 LIST_HEAD(, g_raid_tr_class) g_raid_tr_classes =
101     LIST_HEAD_INITIALIZER(g_raid_tr_classes);
102 
103 LIST_HEAD(, g_raid_volume) g_raid_volumes =
104     LIST_HEAD_INITIALIZER(g_raid_volumes);
105 
106 static eventhandler_tag g_raid_post_sync = NULL;
107 static int g_raid_started = 0;
108 static int g_raid_shutdown = 0;
109 
110 static int g_raid_destroy_geom(struct gctl_req *req, struct g_class *mp,
111     struct g_geom *gp);
112 static g_taste_t g_raid_taste;
113 static void g_raid_init(struct g_class *mp);
114 static void g_raid_fini(struct g_class *mp);
115 
116 struct g_class g_raid_class = {
117 	.name = G_RAID_CLASS_NAME,
118 	.version = G_VERSION,
119 	.ctlreq = g_raid_ctl,
120 	.taste = g_raid_taste,
121 	.destroy_geom = g_raid_destroy_geom,
122 	.init = g_raid_init,
123 	.fini = g_raid_fini
124 };
125 
126 static void g_raid_destroy_provider(struct g_raid_volume *vol);
127 static int g_raid_update_disk(struct g_raid_disk *disk, u_int event);
128 static int g_raid_update_subdisk(struct g_raid_subdisk *subdisk, u_int event);
129 static int g_raid_update_volume(struct g_raid_volume *vol, u_int event);
130 static int g_raid_update_node(struct g_raid_softc *sc, u_int event);
131 static void g_raid_dumpconf(struct sbuf *sb, const char *indent,
132     struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
133 static void g_raid_start(struct bio *bp);
134 static void g_raid_start_request(struct bio *bp);
135 static void g_raid_disk_done(struct bio *bp);
136 static void g_raid_poll(struct g_raid_softc *sc);
137 
138 static const char *
g_raid_node_event2str(int event)139 g_raid_node_event2str(int event)
140 {
141 
142 	switch (event) {
143 	case G_RAID_NODE_E_WAKE:
144 		return ("WAKE");
145 	case G_RAID_NODE_E_START:
146 		return ("START");
147 	default:
148 		return ("INVALID");
149 	}
150 }
151 
152 const char *
g_raid_disk_state2str(int state)153 g_raid_disk_state2str(int state)
154 {
155 
156 	switch (state) {
157 	case G_RAID_DISK_S_NONE:
158 		return ("NONE");
159 	case G_RAID_DISK_S_OFFLINE:
160 		return ("OFFLINE");
161 	case G_RAID_DISK_S_DISABLED:
162 		return ("DISABLED");
163 	case G_RAID_DISK_S_FAILED:
164 		return ("FAILED");
165 	case G_RAID_DISK_S_STALE_FAILED:
166 		return ("STALE_FAILED");
167 	case G_RAID_DISK_S_SPARE:
168 		return ("SPARE");
169 	case G_RAID_DISK_S_STALE:
170 		return ("STALE");
171 	case G_RAID_DISK_S_ACTIVE:
172 		return ("ACTIVE");
173 	default:
174 		return ("INVALID");
175 	}
176 }
177 
178 static const char *
g_raid_disk_event2str(int event)179 g_raid_disk_event2str(int event)
180 {
181 
182 	switch (event) {
183 	case G_RAID_DISK_E_DISCONNECTED:
184 		return ("DISCONNECTED");
185 	default:
186 		return ("INVALID");
187 	}
188 }
189 
190 const char *
g_raid_subdisk_state2str(int state)191 g_raid_subdisk_state2str(int state)
192 {
193 
194 	switch (state) {
195 	case G_RAID_SUBDISK_S_NONE:
196 		return ("NONE");
197 	case G_RAID_SUBDISK_S_FAILED:
198 		return ("FAILED");
199 	case G_RAID_SUBDISK_S_NEW:
200 		return ("NEW");
201 	case G_RAID_SUBDISK_S_REBUILD:
202 		return ("REBUILD");
203 	case G_RAID_SUBDISK_S_UNINITIALIZED:
204 		return ("UNINITIALIZED");
205 	case G_RAID_SUBDISK_S_STALE:
206 		return ("STALE");
207 	case G_RAID_SUBDISK_S_RESYNC:
208 		return ("RESYNC");
209 	case G_RAID_SUBDISK_S_ACTIVE:
210 		return ("ACTIVE");
211 	default:
212 		return ("INVALID");
213 	}
214 }
215 
216 static const char *
g_raid_subdisk_event2str(int event)217 g_raid_subdisk_event2str(int event)
218 {
219 
220 	switch (event) {
221 	case G_RAID_SUBDISK_E_NEW:
222 		return ("NEW");
223 	case G_RAID_SUBDISK_E_FAILED:
224 		return ("FAILED");
225 	case G_RAID_SUBDISK_E_DISCONNECTED:
226 		return ("DISCONNECTED");
227 	default:
228 		return ("INVALID");
229 	}
230 }
231 
232 const char *
g_raid_volume_state2str(int state)233 g_raid_volume_state2str(int state)
234 {
235 
236 	switch (state) {
237 	case G_RAID_VOLUME_S_STARTING:
238 		return ("STARTING");
239 	case G_RAID_VOLUME_S_BROKEN:
240 		return ("BROKEN");
241 	case G_RAID_VOLUME_S_DEGRADED:
242 		return ("DEGRADED");
243 	case G_RAID_VOLUME_S_SUBOPTIMAL:
244 		return ("SUBOPTIMAL");
245 	case G_RAID_VOLUME_S_OPTIMAL:
246 		return ("OPTIMAL");
247 	case G_RAID_VOLUME_S_UNSUPPORTED:
248 		return ("UNSUPPORTED");
249 	case G_RAID_VOLUME_S_STOPPED:
250 		return ("STOPPED");
251 	default:
252 		return ("INVALID");
253 	}
254 }
255 
256 static const char *
g_raid_volume_event2str(int event)257 g_raid_volume_event2str(int event)
258 {
259 
260 	switch (event) {
261 	case G_RAID_VOLUME_E_UP:
262 		return ("UP");
263 	case G_RAID_VOLUME_E_DOWN:
264 		return ("DOWN");
265 	case G_RAID_VOLUME_E_START:
266 		return ("START");
267 	case G_RAID_VOLUME_E_STARTMD:
268 		return ("STARTMD");
269 	default:
270 		return ("INVALID");
271 	}
272 }
273 
274 const char *
g_raid_volume_level2str(int level,int qual)275 g_raid_volume_level2str(int level, int qual)
276 {
277 
278 	switch (level) {
279 	case G_RAID_VOLUME_RL_RAID0:
280 		return ("RAID0");
281 	case G_RAID_VOLUME_RL_RAID1:
282 		return ("RAID1");
283 	case G_RAID_VOLUME_RL_RAID3:
284 		if (qual == G_RAID_VOLUME_RLQ_R3P0)
285 			return ("RAID3-P0");
286 		if (qual == G_RAID_VOLUME_RLQ_R3PN)
287 			return ("RAID3-PN");
288 		return ("RAID3");
289 	case G_RAID_VOLUME_RL_RAID4:
290 		if (qual == G_RAID_VOLUME_RLQ_R4P0)
291 			return ("RAID4-P0");
292 		if (qual == G_RAID_VOLUME_RLQ_R4PN)
293 			return ("RAID4-PN");
294 		return ("RAID4");
295 	case G_RAID_VOLUME_RL_RAID5:
296 		if (qual == G_RAID_VOLUME_RLQ_R5RA)
297 			return ("RAID5-RA");
298 		if (qual == G_RAID_VOLUME_RLQ_R5RS)
299 			return ("RAID5-RS");
300 		if (qual == G_RAID_VOLUME_RLQ_R5LA)
301 			return ("RAID5-LA");
302 		if (qual == G_RAID_VOLUME_RLQ_R5LS)
303 			return ("RAID5-LS");
304 		return ("RAID5");
305 	case G_RAID_VOLUME_RL_RAID6:
306 		if (qual == G_RAID_VOLUME_RLQ_R6RA)
307 			return ("RAID6-RA");
308 		if (qual == G_RAID_VOLUME_RLQ_R6RS)
309 			return ("RAID6-RS");
310 		if (qual == G_RAID_VOLUME_RLQ_R6LA)
311 			return ("RAID6-LA");
312 		if (qual == G_RAID_VOLUME_RLQ_R6LS)
313 			return ("RAID6-LS");
314 		return ("RAID6");
315 	case G_RAID_VOLUME_RL_RAIDMDF:
316 		if (qual == G_RAID_VOLUME_RLQ_RMDFRA)
317 			return ("RAIDMDF-RA");
318 		if (qual == G_RAID_VOLUME_RLQ_RMDFRS)
319 			return ("RAIDMDF-RS");
320 		if (qual == G_RAID_VOLUME_RLQ_RMDFLA)
321 			return ("RAIDMDF-LA");
322 		if (qual == G_RAID_VOLUME_RLQ_RMDFLS)
323 			return ("RAIDMDF-LS");
324 		return ("RAIDMDF");
325 	case G_RAID_VOLUME_RL_RAID1E:
326 		if (qual == G_RAID_VOLUME_RLQ_R1EA)
327 			return ("RAID1E-A");
328 		if (qual == G_RAID_VOLUME_RLQ_R1EO)
329 			return ("RAID1E-O");
330 		return ("RAID1E");
331 	case G_RAID_VOLUME_RL_SINGLE:
332 		return ("SINGLE");
333 	case G_RAID_VOLUME_RL_CONCAT:
334 		return ("CONCAT");
335 	case G_RAID_VOLUME_RL_RAID5E:
336 		if (qual == G_RAID_VOLUME_RLQ_R5ERA)
337 			return ("RAID5E-RA");
338 		if (qual == G_RAID_VOLUME_RLQ_R5ERS)
339 			return ("RAID5E-RS");
340 		if (qual == G_RAID_VOLUME_RLQ_R5ELA)
341 			return ("RAID5E-LA");
342 		if (qual == G_RAID_VOLUME_RLQ_R5ELS)
343 			return ("RAID5E-LS");
344 		return ("RAID5E");
345 	case G_RAID_VOLUME_RL_RAID5EE:
346 		if (qual == G_RAID_VOLUME_RLQ_R5EERA)
347 			return ("RAID5EE-RA");
348 		if (qual == G_RAID_VOLUME_RLQ_R5EERS)
349 			return ("RAID5EE-RS");
350 		if (qual == G_RAID_VOLUME_RLQ_R5EELA)
351 			return ("RAID5EE-LA");
352 		if (qual == G_RAID_VOLUME_RLQ_R5EELS)
353 			return ("RAID5EE-LS");
354 		return ("RAID5EE");
355 	case G_RAID_VOLUME_RL_RAID5R:
356 		if (qual == G_RAID_VOLUME_RLQ_R5RRA)
357 			return ("RAID5R-RA");
358 		if (qual == G_RAID_VOLUME_RLQ_R5RRS)
359 			return ("RAID5R-RS");
360 		if (qual == G_RAID_VOLUME_RLQ_R5RLA)
361 			return ("RAID5R-LA");
362 		if (qual == G_RAID_VOLUME_RLQ_R5RLS)
363 			return ("RAID5R-LS");
364 		return ("RAID5E");
365 	default:
366 		return ("UNKNOWN");
367 	}
368 }
369 
370 int
g_raid_volume_str2level(const char * str,int * level,int * qual)371 g_raid_volume_str2level(const char *str, int *level, int *qual)
372 {
373 
374 	*level = G_RAID_VOLUME_RL_UNKNOWN;
375 	*qual = G_RAID_VOLUME_RLQ_NONE;
376 	if (strcasecmp(str, "RAID0") == 0)
377 		*level = G_RAID_VOLUME_RL_RAID0;
378 	else if (strcasecmp(str, "RAID1") == 0)
379 		*level = G_RAID_VOLUME_RL_RAID1;
380 	else if (strcasecmp(str, "RAID3-P0") == 0) {
381 		*level = G_RAID_VOLUME_RL_RAID3;
382 		*qual = G_RAID_VOLUME_RLQ_R3P0;
383 	} else if (strcasecmp(str, "RAID3-PN") == 0 ||
384 		   strcasecmp(str, "RAID3") == 0) {
385 		*level = G_RAID_VOLUME_RL_RAID3;
386 		*qual = G_RAID_VOLUME_RLQ_R3PN;
387 	} else if (strcasecmp(str, "RAID4-P0") == 0) {
388 		*level = G_RAID_VOLUME_RL_RAID4;
389 		*qual = G_RAID_VOLUME_RLQ_R4P0;
390 	} else if (strcasecmp(str, "RAID4-PN") == 0 ||
391 		   strcasecmp(str, "RAID4") == 0) {
392 		*level = G_RAID_VOLUME_RL_RAID4;
393 		*qual = G_RAID_VOLUME_RLQ_R4PN;
394 	} else if (strcasecmp(str, "RAID5-RA") == 0) {
395 		*level = G_RAID_VOLUME_RL_RAID5;
396 		*qual = G_RAID_VOLUME_RLQ_R5RA;
397 	} else if (strcasecmp(str, "RAID5-RS") == 0) {
398 		*level = G_RAID_VOLUME_RL_RAID5;
399 		*qual = G_RAID_VOLUME_RLQ_R5RS;
400 	} else if (strcasecmp(str, "RAID5") == 0 ||
401 		   strcasecmp(str, "RAID5-LA") == 0) {
402 		*level = G_RAID_VOLUME_RL_RAID5;
403 		*qual = G_RAID_VOLUME_RLQ_R5LA;
404 	} else if (strcasecmp(str, "RAID5-LS") == 0) {
405 		*level = G_RAID_VOLUME_RL_RAID5;
406 		*qual = G_RAID_VOLUME_RLQ_R5LS;
407 	} else if (strcasecmp(str, "RAID6-RA") == 0) {
408 		*level = G_RAID_VOLUME_RL_RAID6;
409 		*qual = G_RAID_VOLUME_RLQ_R6RA;
410 	} else if (strcasecmp(str, "RAID6-RS") == 0) {
411 		*level = G_RAID_VOLUME_RL_RAID6;
412 		*qual = G_RAID_VOLUME_RLQ_R6RS;
413 	} else if (strcasecmp(str, "RAID6") == 0 ||
414 		   strcasecmp(str, "RAID6-LA") == 0) {
415 		*level = G_RAID_VOLUME_RL_RAID6;
416 		*qual = G_RAID_VOLUME_RLQ_R6LA;
417 	} else if (strcasecmp(str, "RAID6-LS") == 0) {
418 		*level = G_RAID_VOLUME_RL_RAID6;
419 		*qual = G_RAID_VOLUME_RLQ_R6LS;
420 	} else if (strcasecmp(str, "RAIDMDF-RA") == 0) {
421 		*level = G_RAID_VOLUME_RL_RAIDMDF;
422 		*qual = G_RAID_VOLUME_RLQ_RMDFRA;
423 	} else if (strcasecmp(str, "RAIDMDF-RS") == 0) {
424 		*level = G_RAID_VOLUME_RL_RAIDMDF;
425 		*qual = G_RAID_VOLUME_RLQ_RMDFRS;
426 	} else if (strcasecmp(str, "RAIDMDF") == 0 ||
427 		   strcasecmp(str, "RAIDMDF-LA") == 0) {
428 		*level = G_RAID_VOLUME_RL_RAIDMDF;
429 		*qual = G_RAID_VOLUME_RLQ_RMDFLA;
430 	} else if (strcasecmp(str, "RAIDMDF-LS") == 0) {
431 		*level = G_RAID_VOLUME_RL_RAIDMDF;
432 		*qual = G_RAID_VOLUME_RLQ_RMDFLS;
433 	} else if (strcasecmp(str, "RAID10") == 0 ||
434 		   strcasecmp(str, "RAID1E") == 0 ||
435 		   strcasecmp(str, "RAID1E-A") == 0) {
436 		*level = G_RAID_VOLUME_RL_RAID1E;
437 		*qual = G_RAID_VOLUME_RLQ_R1EA;
438 	} else if (strcasecmp(str, "RAID1E-O") == 0) {
439 		*level = G_RAID_VOLUME_RL_RAID1E;
440 		*qual = G_RAID_VOLUME_RLQ_R1EO;
441 	} else if (strcasecmp(str, "SINGLE") == 0)
442 		*level = G_RAID_VOLUME_RL_SINGLE;
443 	else if (strcasecmp(str, "CONCAT") == 0)
444 		*level = G_RAID_VOLUME_RL_CONCAT;
445 	else if (strcasecmp(str, "RAID5E-RA") == 0) {
446 		*level = G_RAID_VOLUME_RL_RAID5E;
447 		*qual = G_RAID_VOLUME_RLQ_R5ERA;
448 	} else if (strcasecmp(str, "RAID5E-RS") == 0) {
449 		*level = G_RAID_VOLUME_RL_RAID5E;
450 		*qual = G_RAID_VOLUME_RLQ_R5ERS;
451 	} else if (strcasecmp(str, "RAID5E") == 0 ||
452 		   strcasecmp(str, "RAID5E-LA") == 0) {
453 		*level = G_RAID_VOLUME_RL_RAID5E;
454 		*qual = G_RAID_VOLUME_RLQ_R5ELA;
455 	} else if (strcasecmp(str, "RAID5E-LS") == 0) {
456 		*level = G_RAID_VOLUME_RL_RAID5E;
457 		*qual = G_RAID_VOLUME_RLQ_R5ELS;
458 	} else if (strcasecmp(str, "RAID5EE-RA") == 0) {
459 		*level = G_RAID_VOLUME_RL_RAID5EE;
460 		*qual = G_RAID_VOLUME_RLQ_R5EERA;
461 	} else if (strcasecmp(str, "RAID5EE-RS") == 0) {
462 		*level = G_RAID_VOLUME_RL_RAID5EE;
463 		*qual = G_RAID_VOLUME_RLQ_R5EERS;
464 	} else if (strcasecmp(str, "RAID5EE") == 0 ||
465 		   strcasecmp(str, "RAID5EE-LA") == 0) {
466 		*level = G_RAID_VOLUME_RL_RAID5EE;
467 		*qual = G_RAID_VOLUME_RLQ_R5EELA;
468 	} else if (strcasecmp(str, "RAID5EE-LS") == 0) {
469 		*level = G_RAID_VOLUME_RL_RAID5EE;
470 		*qual = G_RAID_VOLUME_RLQ_R5EELS;
471 	} else if (strcasecmp(str, "RAID5R-RA") == 0) {
472 		*level = G_RAID_VOLUME_RL_RAID5R;
473 		*qual = G_RAID_VOLUME_RLQ_R5RRA;
474 	} else if (strcasecmp(str, "RAID5R-RS") == 0) {
475 		*level = G_RAID_VOLUME_RL_RAID5R;
476 		*qual = G_RAID_VOLUME_RLQ_R5RRS;
477 	} else if (strcasecmp(str, "RAID5R") == 0 ||
478 		   strcasecmp(str, "RAID5R-LA") == 0) {
479 		*level = G_RAID_VOLUME_RL_RAID5R;
480 		*qual = G_RAID_VOLUME_RLQ_R5RLA;
481 	} else if (strcasecmp(str, "RAID5R-LS") == 0) {
482 		*level = G_RAID_VOLUME_RL_RAID5R;
483 		*qual = G_RAID_VOLUME_RLQ_R5RLS;
484 	} else
485 		return (-1);
486 	return (0);
487 }
488 
489 const char *
g_raid_get_diskname(struct g_raid_disk * disk)490 g_raid_get_diskname(struct g_raid_disk *disk)
491 {
492 
493 	if (disk->d_consumer == NULL || disk->d_consumer->provider == NULL)
494 		return ("[unknown]");
495 	return (disk->d_consumer->provider->name);
496 }
497 
498 void
g_raid_get_disk_info(struct g_raid_disk * disk)499 g_raid_get_disk_info(struct g_raid_disk *disk)
500 {
501 	struct g_consumer *cp = disk->d_consumer;
502 	int error, len;
503 
504 	/* Read kernel dumping information. */
505 	disk->d_kd.offset = 0;
506 	disk->d_kd.length = OFF_MAX;
507 	len = sizeof(disk->d_kd);
508 	error = g_io_getattr("GEOM::kerneldump", cp, &len, &disk->d_kd);
509 	if (error)
510 		disk->d_kd.di.dumper = NULL;
511 	if (disk->d_kd.di.dumper == NULL)
512 		G_RAID_DEBUG1(2, disk->d_softc,
513 		    "Dumping not supported by %s: %d.",
514 		    cp->provider->name, error);
515 
516 	/* Read BIO_DELETE support. */
517 	error = g_getattr("GEOM::candelete", cp, &disk->d_candelete);
518 	if (error)
519 		disk->d_candelete = 0;
520 	if (!disk->d_candelete)
521 		G_RAID_DEBUG1(2, disk->d_softc,
522 		    "BIO_DELETE not supported by %s: %d.",
523 		    cp->provider->name, error);
524 
525 	/* Read rotation rate. */
526 	error = g_getattr("GEOM::rotation_rate", cp, &disk->d_rotation_rate);
527 	if (error)
528 		disk->d_rotation_rate = DISK_RR_UNKNOWN;
529 }
530 
531 void
g_raid_report_disk_state(struct g_raid_disk * disk)532 g_raid_report_disk_state(struct g_raid_disk *disk)
533 {
534 	struct g_raid_subdisk *sd;
535 	int len, state;
536 	uint32_t s;
537 
538 	if (disk->d_consumer == NULL)
539 		return;
540 	if (disk->d_state == G_RAID_DISK_S_DISABLED) {
541 		s = G_STATE_ACTIVE; /* XXX */
542 	} else if (disk->d_state == G_RAID_DISK_S_FAILED ||
543 	    disk->d_state == G_RAID_DISK_S_STALE_FAILED) {
544 		s = G_STATE_FAILED;
545 	} else {
546 		state = G_RAID_SUBDISK_S_ACTIVE;
547 		TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
548 			if (sd->sd_state < state)
549 				state = sd->sd_state;
550 		}
551 		if (state == G_RAID_SUBDISK_S_FAILED)
552 			s = G_STATE_FAILED;
553 		else if (state == G_RAID_SUBDISK_S_NEW ||
554 		    state == G_RAID_SUBDISK_S_REBUILD)
555 			s = G_STATE_REBUILD;
556 		else if (state == G_RAID_SUBDISK_S_STALE ||
557 		    state == G_RAID_SUBDISK_S_RESYNC)
558 			s = G_STATE_RESYNC;
559 		else
560 			s = G_STATE_ACTIVE;
561 	}
562 	len = sizeof(s);
563 	g_io_getattr("GEOM::setstate", disk->d_consumer, &len, &s);
564 	G_RAID_DEBUG1(2, disk->d_softc, "Disk %s state reported as %d.",
565 	    g_raid_get_diskname(disk), s);
566 }
567 
568 void
g_raid_change_disk_state(struct g_raid_disk * disk,int state)569 g_raid_change_disk_state(struct g_raid_disk *disk, int state)
570 {
571 
572 	G_RAID_DEBUG1(0, disk->d_softc, "Disk %s state changed from %s to %s.",
573 	    g_raid_get_diskname(disk),
574 	    g_raid_disk_state2str(disk->d_state),
575 	    g_raid_disk_state2str(state));
576 	disk->d_state = state;
577 	g_raid_report_disk_state(disk);
578 }
579 
580 void
g_raid_change_subdisk_state(struct g_raid_subdisk * sd,int state)581 g_raid_change_subdisk_state(struct g_raid_subdisk *sd, int state)
582 {
583 
584 	G_RAID_DEBUG1(0, sd->sd_softc,
585 	    "Subdisk %s:%d-%s state changed from %s to %s.",
586 	    sd->sd_volume->v_name, sd->sd_pos,
587 	    sd->sd_disk ? g_raid_get_diskname(sd->sd_disk) : "[none]",
588 	    g_raid_subdisk_state2str(sd->sd_state),
589 	    g_raid_subdisk_state2str(state));
590 	sd->sd_state = state;
591 	if (sd->sd_disk)
592 		g_raid_report_disk_state(sd->sd_disk);
593 }
594 
595 void
g_raid_change_volume_state(struct g_raid_volume * vol,int state)596 g_raid_change_volume_state(struct g_raid_volume *vol, int state)
597 {
598 
599 	G_RAID_DEBUG1(0, vol->v_softc,
600 	    "Volume %s state changed from %s to %s.",
601 	    vol->v_name,
602 	    g_raid_volume_state2str(vol->v_state),
603 	    g_raid_volume_state2str(state));
604 	vol->v_state = state;
605 }
606 
607 /*
608  * --- Events handling functions ---
609  * Events in geom_raid are used to maintain subdisks and volumes status
610  * from one thread to simplify locking.
611  */
612 static void
g_raid_event_free(struct g_raid_event * ep)613 g_raid_event_free(struct g_raid_event *ep)
614 {
615 
616 	free(ep, M_RAID);
617 }
618 
619 int
g_raid_event_send(void * arg,int event,int flags)620 g_raid_event_send(void *arg, int event, int flags)
621 {
622 	struct g_raid_softc *sc;
623 	struct g_raid_event *ep;
624 	int error;
625 
626 	if ((flags & G_RAID_EVENT_VOLUME) != 0) {
627 		sc = ((struct g_raid_volume *)arg)->v_softc;
628 	} else if ((flags & G_RAID_EVENT_DISK) != 0) {
629 		sc = ((struct g_raid_disk *)arg)->d_softc;
630 	} else if ((flags & G_RAID_EVENT_SUBDISK) != 0) {
631 		sc = ((struct g_raid_subdisk *)arg)->sd_softc;
632 	} else {
633 		sc = arg;
634 	}
635 	ep = malloc(sizeof(*ep), M_RAID,
636 	    sx_xlocked(&sc->sc_lock) ? M_WAITOK : M_NOWAIT);
637 	if (ep == NULL)
638 		return (ENOMEM);
639 	ep->e_tgt = arg;
640 	ep->e_event = event;
641 	ep->e_flags = flags;
642 	ep->e_error = 0;
643 	G_RAID_DEBUG1(4, sc, "Sending event %p. Waking up %p.", ep, sc);
644 	mtx_lock(&sc->sc_queue_mtx);
645 	TAILQ_INSERT_TAIL(&sc->sc_events, ep, e_next);
646 	mtx_unlock(&sc->sc_queue_mtx);
647 	wakeup(sc);
648 
649 	if ((flags & G_RAID_EVENT_WAIT) == 0)
650 		return (0);
651 
652 	sx_assert(&sc->sc_lock, SX_XLOCKED);
653 	G_RAID_DEBUG1(4, sc, "Sleeping on %p.", ep);
654 	sx_xunlock(&sc->sc_lock);
655 	while ((ep->e_flags & G_RAID_EVENT_DONE) == 0) {
656 		mtx_lock(&sc->sc_queue_mtx);
657 		MSLEEP(error, ep, &sc->sc_queue_mtx, PRIBIO | PDROP, "m:event",
658 		    hz * 5);
659 	}
660 	error = ep->e_error;
661 	g_raid_event_free(ep);
662 	sx_xlock(&sc->sc_lock);
663 	return (error);
664 }
665 
666 static void
g_raid_event_cancel(struct g_raid_softc * sc,void * tgt)667 g_raid_event_cancel(struct g_raid_softc *sc, void *tgt)
668 {
669 	struct g_raid_event *ep, *tmpep;
670 
671 	sx_assert(&sc->sc_lock, SX_XLOCKED);
672 
673 	mtx_lock(&sc->sc_queue_mtx);
674 	TAILQ_FOREACH_SAFE(ep, &sc->sc_events, e_next, tmpep) {
675 		if (ep->e_tgt != tgt)
676 			continue;
677 		TAILQ_REMOVE(&sc->sc_events, ep, e_next);
678 		if ((ep->e_flags & G_RAID_EVENT_WAIT) == 0)
679 			g_raid_event_free(ep);
680 		else {
681 			ep->e_error = ECANCELED;
682 			wakeup(ep);
683 		}
684 	}
685 	mtx_unlock(&sc->sc_queue_mtx);
686 }
687 
688 static int
g_raid_event_check(struct g_raid_softc * sc,void * tgt)689 g_raid_event_check(struct g_raid_softc *sc, void *tgt)
690 {
691 	struct g_raid_event *ep;
692 	int	res = 0;
693 
694 	sx_assert(&sc->sc_lock, SX_XLOCKED);
695 
696 	mtx_lock(&sc->sc_queue_mtx);
697 	TAILQ_FOREACH(ep, &sc->sc_events, e_next) {
698 		if (ep->e_tgt != tgt)
699 			continue;
700 		res = 1;
701 		break;
702 	}
703 	mtx_unlock(&sc->sc_queue_mtx);
704 	return (res);
705 }
706 
707 /*
708  * Return the number of disks in given state.
709  * If state is equal to -1, count all connected disks.
710  */
711 u_int
g_raid_ndisks(struct g_raid_softc * sc,int state)712 g_raid_ndisks(struct g_raid_softc *sc, int state)
713 {
714 	struct g_raid_disk *disk;
715 	u_int n;
716 
717 	sx_assert(&sc->sc_lock, SX_LOCKED);
718 
719 	n = 0;
720 	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
721 		if (disk->d_state == state || state == -1)
722 			n++;
723 	}
724 	return (n);
725 }
726 
727 /*
728  * Return the number of subdisks in given state.
729  * If state is equal to -1, count all connected disks.
730  */
731 u_int
g_raid_nsubdisks(struct g_raid_volume * vol,int state)732 g_raid_nsubdisks(struct g_raid_volume *vol, int state)
733 {
734 	struct g_raid_subdisk *subdisk;
735 	struct g_raid_softc *sc __diagused;
736 	u_int i, n ;
737 
738 	sc = vol->v_softc;
739 	sx_assert(&sc->sc_lock, SX_LOCKED);
740 
741 	n = 0;
742 	for (i = 0; i < vol->v_disks_count; i++) {
743 		subdisk = &vol->v_subdisks[i];
744 		if ((state == -1 &&
745 		     subdisk->sd_state != G_RAID_SUBDISK_S_NONE) ||
746 		    subdisk->sd_state == state)
747 			n++;
748 	}
749 	return (n);
750 }
751 
752 /*
753  * Return the first subdisk in given state.
754  * If state is equal to -1, then the first connected disks.
755  */
756 struct g_raid_subdisk *
g_raid_get_subdisk(struct g_raid_volume * vol,int state)757 g_raid_get_subdisk(struct g_raid_volume *vol, int state)
758 {
759 	struct g_raid_subdisk *sd;
760 	struct g_raid_softc *sc __diagused;
761 	u_int i;
762 
763 	sc = vol->v_softc;
764 	sx_assert(&sc->sc_lock, SX_LOCKED);
765 
766 	for (i = 0; i < vol->v_disks_count; i++) {
767 		sd = &vol->v_subdisks[i];
768 		if ((state == -1 &&
769 		     sd->sd_state != G_RAID_SUBDISK_S_NONE) ||
770 		    sd->sd_state == state)
771 			return (sd);
772 	}
773 	return (NULL);
774 }
775 
776 struct g_consumer *
g_raid_open_consumer(struct g_raid_softc * sc,const char * name)777 g_raid_open_consumer(struct g_raid_softc *sc, const char *name)
778 {
779 	struct g_consumer *cp;
780 	struct g_provider *pp;
781 
782 	g_topology_assert();
783 
784 	pp = g_provider_by_name(name);
785 	if (pp == NULL)
786 		return (NULL);
787 	cp = g_new_consumer(sc->sc_geom);
788 	cp->flags |= G_CF_DIRECT_RECEIVE;
789 	if (g_attach(cp, pp) != 0) {
790 		g_destroy_consumer(cp);
791 		return (NULL);
792 	}
793 	if (g_access(cp, 1, 1, 1) != 0) {
794 		g_detach(cp);
795 		g_destroy_consumer(cp);
796 		return (NULL);
797 	}
798 	return (cp);
799 }
800 
801 static u_int
g_raid_nrequests(struct g_raid_softc * sc,struct g_consumer * cp)802 g_raid_nrequests(struct g_raid_softc *sc, struct g_consumer *cp)
803 {
804 	struct bio *bp;
805 	u_int nreqs = 0;
806 
807 	mtx_lock(&sc->sc_queue_mtx);
808 	TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
809 		if (bp->bio_from == cp)
810 			nreqs++;
811 	}
812 	mtx_unlock(&sc->sc_queue_mtx);
813 	return (nreqs);
814 }
815 
816 u_int
g_raid_nopens(struct g_raid_softc * sc)817 g_raid_nopens(struct g_raid_softc *sc)
818 {
819 	struct g_raid_volume *vol;
820 	u_int opens;
821 
822 	opens = 0;
823 	TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
824 		if (vol->v_provider_open != 0)
825 			opens++;
826 	}
827 	return (opens);
828 }
829 
830 static int
g_raid_consumer_is_busy(struct g_raid_softc * sc,struct g_consumer * cp)831 g_raid_consumer_is_busy(struct g_raid_softc *sc, struct g_consumer *cp)
832 {
833 
834 	if (cp->index > 0) {
835 		G_RAID_DEBUG1(2, sc,
836 		    "I/O requests for %s exist, can't destroy it now.",
837 		    cp->provider->name);
838 		return (1);
839 	}
840 	if (g_raid_nrequests(sc, cp) > 0) {
841 		G_RAID_DEBUG1(2, sc,
842 		    "I/O requests for %s in queue, can't destroy it now.",
843 		    cp->provider->name);
844 		return (1);
845 	}
846 	return (0);
847 }
848 
849 static void
g_raid_destroy_consumer(void * arg,int flags __unused)850 g_raid_destroy_consumer(void *arg, int flags __unused)
851 {
852 	struct g_consumer *cp;
853 
854 	g_topology_assert();
855 
856 	cp = arg;
857 	G_RAID_DEBUG(1, "Consumer %s destroyed.", cp->provider->name);
858 	g_detach(cp);
859 	g_destroy_consumer(cp);
860 }
861 
862 void
g_raid_kill_consumer(struct g_raid_softc * sc,struct g_consumer * cp)863 g_raid_kill_consumer(struct g_raid_softc *sc, struct g_consumer *cp)
864 {
865 	struct g_provider *pp;
866 	int retaste_wait;
867 
868 	g_topology_assert_not();
869 
870 	g_topology_lock();
871 	cp->private = NULL;
872 	if (g_raid_consumer_is_busy(sc, cp))
873 		goto out;
874 	pp = cp->provider;
875 	retaste_wait = 0;
876 	if (cp->acw == 1) {
877 		if ((pp->geom->flags & G_GEOM_WITHER) == 0)
878 			retaste_wait = 1;
879 	}
880 	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
881 		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
882 	if (retaste_wait) {
883 		/*
884 		 * After retaste event was send (inside g_access()), we can send
885 		 * event to detach and destroy consumer.
886 		 * A class, which has consumer to the given provider connected
887 		 * will not receive retaste event for the provider.
888 		 * This is the way how I ignore retaste events when I close
889 		 * consumers opened for write: I detach and destroy consumer
890 		 * after retaste event is sent.
891 		 */
892 		g_post_event(g_raid_destroy_consumer, cp, M_WAITOK, NULL);
893 		goto out;
894 	}
895 	G_RAID_DEBUG(1, "Consumer %s destroyed.", pp->name);
896 	g_detach(cp);
897 	g_destroy_consumer(cp);
898 out:
899 	g_topology_unlock();
900 }
901 
902 static void
g_raid_orphan(struct g_consumer * cp)903 g_raid_orphan(struct g_consumer *cp)
904 {
905 	struct g_raid_disk *disk;
906 
907 	g_topology_assert();
908 
909 	disk = cp->private;
910 	if (disk == NULL)
911 		return;
912 	g_raid_event_send(disk, G_RAID_DISK_E_DISCONNECTED,
913 	    G_RAID_EVENT_DISK);
914 }
915 
916 static void
g_raid_clean(struct g_raid_volume * vol,int acw)917 g_raid_clean(struct g_raid_volume *vol, int acw)
918 {
919 	struct g_raid_softc *sc;
920 	int timeout;
921 
922 	sc = vol->v_softc;
923 	g_topology_assert_not();
924 	sx_assert(&sc->sc_lock, SX_XLOCKED);
925 
926 //	if ((sc->sc_flags & G_RAID_DEVICE_FLAG_NOFAILSYNC) != 0)
927 //		return;
928 	if (!vol->v_dirty)
929 		return;
930 	if (vol->v_writes > 0)
931 		return;
932 	if (acw > 0 || (acw == -1 &&
933 	    vol->v_provider != NULL && vol->v_provider->acw > 0)) {
934 		timeout = g_raid_clean_time - (time_uptime - vol->v_last_write);
935 		if (!g_raid_shutdown && timeout > 0)
936 			return;
937 	}
938 	vol->v_dirty = 0;
939 	G_RAID_DEBUG1(1, sc, "Volume %s marked as clean.",
940 	    vol->v_name);
941 	g_raid_write_metadata(sc, vol, NULL, NULL);
942 }
943 
944 static void
g_raid_dirty(struct g_raid_volume * vol)945 g_raid_dirty(struct g_raid_volume *vol)
946 {
947 	struct g_raid_softc *sc;
948 
949 	sc = vol->v_softc;
950 	g_topology_assert_not();
951 	sx_assert(&sc->sc_lock, SX_XLOCKED);
952 
953 //	if ((sc->sc_flags & G_RAID_DEVICE_FLAG_NOFAILSYNC) != 0)
954 //		return;
955 	vol->v_dirty = 1;
956 	G_RAID_DEBUG1(1, sc, "Volume %s marked as dirty.",
957 	    vol->v_name);
958 	g_raid_write_metadata(sc, vol, NULL, NULL);
959 }
960 
961 void
g_raid_tr_flush_common(struct g_raid_tr_object * tr,struct bio * bp)962 g_raid_tr_flush_common(struct g_raid_tr_object *tr, struct bio *bp)
963 {
964 	struct g_raid_volume *vol;
965 	struct g_raid_subdisk *sd;
966 	struct bio_queue_head queue;
967 	struct bio *cbp;
968 	int i;
969 
970 	vol = tr->tro_volume;
971 
972 	/*
973 	 * Allocate all bios before sending any request, so we can return
974 	 * ENOMEM in nice and clean way.
975 	 */
976 	bioq_init(&queue);
977 	for (i = 0; i < vol->v_disks_count; i++) {
978 		sd = &vol->v_subdisks[i];
979 		if (sd->sd_state == G_RAID_SUBDISK_S_NONE ||
980 		    sd->sd_state == G_RAID_SUBDISK_S_FAILED)
981 			continue;
982 		cbp = g_clone_bio(bp);
983 		if (cbp == NULL)
984 			goto failure;
985 		cbp->bio_caller1 = sd;
986 		bioq_insert_tail(&queue, cbp);
987 	}
988 	while ((cbp = bioq_takefirst(&queue)) != NULL) {
989 		sd = cbp->bio_caller1;
990 		cbp->bio_caller1 = NULL;
991 		g_raid_subdisk_iostart(sd, cbp);
992 	}
993 	return;
994 failure:
995 	while ((cbp = bioq_takefirst(&queue)) != NULL)
996 		g_destroy_bio(cbp);
997 	if (bp->bio_error == 0)
998 		bp->bio_error = ENOMEM;
999 	g_raid_iodone(bp, bp->bio_error);
1000 }
1001 
1002 static void
g_raid_tr_kerneldump_common_done(struct bio * bp)1003 g_raid_tr_kerneldump_common_done(struct bio *bp)
1004 {
1005 
1006 	bp->bio_flags |= BIO_DONE;
1007 }
1008 
1009 int
g_raid_tr_kerneldump_common(struct g_raid_tr_object * tr,void * virtual,vm_offset_t physical,off_t offset,size_t length)1010 g_raid_tr_kerneldump_common(struct g_raid_tr_object *tr,
1011     void *virtual, vm_offset_t physical, off_t offset, size_t length)
1012 {
1013 	struct g_raid_softc *sc;
1014 	struct g_raid_volume *vol;
1015 	struct bio bp;
1016 
1017 	vol = tr->tro_volume;
1018 	sc = vol->v_softc;
1019 
1020 	g_reset_bio(&bp);
1021 	bp.bio_cmd = BIO_WRITE;
1022 	bp.bio_done = g_raid_tr_kerneldump_common_done;
1023 	bp.bio_attribute = NULL;
1024 	bp.bio_offset = offset;
1025 	bp.bio_length = length;
1026 	bp.bio_data = virtual;
1027 	bp.bio_to = vol->v_provider;
1028 
1029 	g_raid_start(&bp);
1030 	while (!(bp.bio_flags & BIO_DONE)) {
1031 		G_RAID_DEBUG1(4, sc, "Poll...");
1032 		g_raid_poll(sc);
1033 		DELAY(10);
1034 	}
1035 
1036 	return (bp.bio_error != 0 ? EIO : 0);
1037 }
1038 
1039 static int
g_raid_dump(void * arg,void * virtual,off_t offset,size_t length)1040 g_raid_dump(void *arg, void *virtual, off_t offset, size_t length)
1041 {
1042 	struct g_raid_volume *vol;
1043 	int error;
1044 
1045 	vol = (struct g_raid_volume *)arg;
1046 	G_RAID_DEBUG1(3, vol->v_softc, "Dumping at off %llu len %llu.",
1047 	    (long long unsigned)offset, (long long unsigned)length);
1048 
1049 	error = G_RAID_TR_KERNELDUMP(vol->v_tr, virtual, offset, length);
1050 	return (error);
1051 }
1052 
1053 static void
g_raid_kerneldump(struct g_raid_softc * sc,struct bio * bp)1054 g_raid_kerneldump(struct g_raid_softc *sc, struct bio *bp)
1055 {
1056 	struct g_kerneldump *gkd;
1057 	struct g_provider *pp;
1058 	struct g_raid_volume *vol;
1059 
1060 	gkd = (struct g_kerneldump*)bp->bio_data;
1061 	pp = bp->bio_to;
1062 	vol = pp->private;
1063 	g_trace(G_T_TOPOLOGY, "g_raid_kerneldump(%s, %jd, %jd)",
1064 		pp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
1065 	gkd->di.dumper = g_raid_dump;
1066 	gkd->di.priv = vol;
1067 	gkd->di.blocksize = vol->v_sectorsize;
1068 	gkd->di.maxiosize = DFLTPHYS;
1069 	gkd->di.mediaoffset = gkd->offset;
1070 	if ((gkd->offset + gkd->length) > vol->v_mediasize)
1071 		gkd->length = vol->v_mediasize - gkd->offset;
1072 	gkd->di.mediasize = gkd->length;
1073 	g_io_deliver(bp, 0);
1074 }
1075 
1076 static void
g_raid_candelete(struct g_raid_softc * sc,struct bio * bp)1077 g_raid_candelete(struct g_raid_softc *sc, struct bio *bp)
1078 {
1079 	struct g_provider *pp;
1080 	struct g_raid_volume *vol;
1081 	struct g_raid_subdisk *sd;
1082 	int i, val;
1083 
1084 	pp = bp->bio_to;
1085 	vol = pp->private;
1086 	for (i = 0; i < vol->v_disks_count; i++) {
1087 		sd = &vol->v_subdisks[i];
1088 		if (sd->sd_state == G_RAID_SUBDISK_S_NONE)
1089 			continue;
1090 		if (sd->sd_disk->d_candelete)
1091 			break;
1092 	}
1093 	val = i < vol->v_disks_count;
1094 	g_handleattr(bp, "GEOM::candelete", &val, sizeof(val));
1095 }
1096 
1097 static void
g_raid_rotation_rate(struct g_raid_softc * sc,struct bio * bp)1098 g_raid_rotation_rate(struct g_raid_softc *sc, struct bio *bp)
1099 {
1100 	struct g_raid_volume *vol;
1101 	struct g_raid_subdisk *sd;
1102 	bool first = true;
1103 	uint16_t rr = DISK_RR_UNKNOWN;
1104 	int i;
1105 
1106 	vol = bp->bio_to->private;
1107 	for (i = 0; i < vol->v_disks_count; i++) {
1108 		sd = &vol->v_subdisks[i];
1109 		if (sd->sd_state == G_RAID_SUBDISK_S_NONE)
1110 			continue;
1111 		if (first)
1112 			rr = sd->sd_disk->d_rotation_rate;
1113 		else if (rr != sd->sd_disk->d_rotation_rate) {
1114 			rr = DISK_RR_UNKNOWN;
1115 			break;
1116 		}
1117 		first = false;
1118 	}
1119 	g_handleattr(bp, "GEOM::rotation_rate", &rr, sizeof(rr));
1120 }
1121 
1122 static void
g_raid_start(struct bio * bp)1123 g_raid_start(struct bio *bp)
1124 {
1125 	struct g_raid_softc *sc;
1126 
1127 	sc = bp->bio_to->geom->softc;
1128 	/*
1129 	 * If sc == NULL or there are no valid disks, provider's error
1130 	 * should be set and g_raid_start() should not be called at all.
1131 	 */
1132 //	KASSERT(sc != NULL && sc->sc_state == G_RAID_VOLUME_S_RUNNING,
1133 //	    ("Provider's error should be set (error=%d)(mirror=%s).",
1134 //	    bp->bio_to->error, bp->bio_to->name));
1135 	G_RAID_LOGREQ(3, bp, "Request received.");
1136 
1137 	switch (bp->bio_cmd) {
1138 	case BIO_READ:
1139 	case BIO_WRITE:
1140 	case BIO_DELETE:
1141 	case BIO_FLUSH:
1142 	case BIO_SPEEDUP:
1143 		break;
1144 	case BIO_GETATTR:
1145 		if (!strcmp(bp->bio_attribute, "GEOM::candelete"))
1146 			g_raid_candelete(sc, bp);
1147 		else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
1148 			g_raid_kerneldump(sc, bp);
1149 		else if (!strcmp(bp->bio_attribute, "GEOM::rotation_rate"))
1150 			g_raid_rotation_rate(sc, bp);
1151 		else
1152 			g_io_deliver(bp, EOPNOTSUPP);
1153 		return;
1154 	default:
1155 		g_io_deliver(bp, EOPNOTSUPP);
1156 		return;
1157 	}
1158 	mtx_lock(&sc->sc_queue_mtx);
1159 	bioq_insert_tail(&sc->sc_queue, bp);
1160 	mtx_unlock(&sc->sc_queue_mtx);
1161 	if (!dumping) {
1162 		G_RAID_DEBUG1(4, sc, "Waking up %p.", sc);
1163 		wakeup(sc);
1164 	}
1165 }
1166 
1167 static int
g_raid_bio_overlaps(const struct bio * bp,off_t lstart,off_t len)1168 g_raid_bio_overlaps(const struct bio *bp, off_t lstart, off_t len)
1169 {
1170 	/*
1171 	 * 5 cases:
1172 	 * (1) bp entirely below NO
1173 	 * (2) bp entirely above NO
1174 	 * (3) bp start below, but end in range YES
1175 	 * (4) bp entirely within YES
1176 	 * (5) bp starts within, ends above YES
1177 	 *
1178 	 * lock range 10-19 (offset 10 length 10)
1179 	 * (1) 1-5: first if kicks it out
1180 	 * (2) 30-35: second if kicks it out
1181 	 * (3) 5-15: passes both ifs
1182 	 * (4) 12-14: passes both ifs
1183 	 * (5) 19-20: passes both
1184 	 */
1185 	off_t lend = lstart + len - 1;
1186 	off_t bstart = bp->bio_offset;
1187 	off_t bend = bp->bio_offset + bp->bio_length - 1;
1188 
1189 	if (bend < lstart)
1190 		return (0);
1191 	if (lend < bstart)
1192 		return (0);
1193 	return (1);
1194 }
1195 
1196 static int
g_raid_is_in_locked_range(struct g_raid_volume * vol,const struct bio * bp)1197 g_raid_is_in_locked_range(struct g_raid_volume *vol, const struct bio *bp)
1198 {
1199 	struct g_raid_lock *lp;
1200 
1201 	sx_assert(&vol->v_softc->sc_lock, SX_LOCKED);
1202 
1203 	LIST_FOREACH(lp, &vol->v_locks, l_next) {
1204 		if (g_raid_bio_overlaps(bp, lp->l_offset, lp->l_length))
1205 			return (1);
1206 	}
1207 	return (0);
1208 }
1209 
1210 static void
g_raid_start_request(struct bio * bp)1211 g_raid_start_request(struct bio *bp)
1212 {
1213 	struct g_raid_softc *sc __diagused;
1214 	struct g_raid_volume *vol;
1215 
1216 	sc = bp->bio_to->geom->softc;
1217 	sx_assert(&sc->sc_lock, SX_LOCKED);
1218 	vol = bp->bio_to->private;
1219 
1220 	/*
1221 	 * Check to see if this item is in a locked range.  If so,
1222 	 * queue it to our locked queue and return.  We'll requeue
1223 	 * it when the range is unlocked.  Internal I/O for the
1224 	 * rebuild/rescan/recovery process is excluded from this
1225 	 * check so we can actually do the recovery.
1226 	 */
1227 	if (!(bp->bio_cflags & G_RAID_BIO_FLAG_SPECIAL) &&
1228 	    g_raid_is_in_locked_range(vol, bp)) {
1229 		G_RAID_LOGREQ(3, bp, "Defer request.");
1230 		bioq_insert_tail(&vol->v_locked, bp);
1231 		return;
1232 	}
1233 
1234 	/*
1235 	 * If we're actually going to do the write/delete, then
1236 	 * update the idle stats for the volume.
1237 	 */
1238 	if (bp->bio_cmd == BIO_WRITE || bp->bio_cmd == BIO_DELETE) {
1239 		if (!vol->v_dirty)
1240 			g_raid_dirty(vol);
1241 		vol->v_writes++;
1242 	}
1243 
1244 	/*
1245 	 * Put request onto inflight queue, so we can check if new
1246 	 * synchronization requests don't collide with it.  Then tell
1247 	 * the transformation layer to start the I/O.
1248 	 */
1249 	bioq_insert_tail(&vol->v_inflight, bp);
1250 	G_RAID_LOGREQ(4, bp, "Request started");
1251 	G_RAID_TR_IOSTART(vol->v_tr, bp);
1252 }
1253 
1254 static void
g_raid_finish_with_locked_ranges(struct g_raid_volume * vol,struct bio * bp)1255 g_raid_finish_with_locked_ranges(struct g_raid_volume *vol, struct bio *bp)
1256 {
1257 	off_t off, len;
1258 	struct bio *nbp;
1259 	struct g_raid_lock *lp;
1260 
1261 	vol->v_pending_lock = 0;
1262 	LIST_FOREACH(lp, &vol->v_locks, l_next) {
1263 		if (lp->l_pending) {
1264 			off = lp->l_offset;
1265 			len = lp->l_length;
1266 			lp->l_pending = 0;
1267 			TAILQ_FOREACH(nbp, &vol->v_inflight.queue, bio_queue) {
1268 				if (g_raid_bio_overlaps(nbp, off, len))
1269 					lp->l_pending++;
1270 			}
1271 			if (lp->l_pending) {
1272 				vol->v_pending_lock = 1;
1273 				G_RAID_DEBUG1(4, vol->v_softc,
1274 				    "Deferred lock(%jd, %jd) has %d pending",
1275 				    (intmax_t)off, (intmax_t)(off + len),
1276 				    lp->l_pending);
1277 				continue;
1278 			}
1279 			G_RAID_DEBUG1(4, vol->v_softc,
1280 			    "Deferred lock of %jd to %jd completed",
1281 			    (intmax_t)off, (intmax_t)(off + len));
1282 			G_RAID_TR_LOCKED(vol->v_tr, lp->l_callback_arg);
1283 		}
1284 	}
1285 }
1286 
1287 void
g_raid_iodone(struct bio * bp,int error)1288 g_raid_iodone(struct bio *bp, int error)
1289 {
1290 	struct g_raid_softc *sc __diagused;
1291 	struct g_raid_volume *vol;
1292 
1293 	sc = bp->bio_to->geom->softc;
1294 	sx_assert(&sc->sc_lock, SX_LOCKED);
1295 	vol = bp->bio_to->private;
1296 	G_RAID_LOGREQ(3, bp, "Request done: %d.", error);
1297 
1298 	/* Update stats if we done write/delete. */
1299 	if (bp->bio_cmd == BIO_WRITE || bp->bio_cmd == BIO_DELETE) {
1300 		vol->v_writes--;
1301 		vol->v_last_write = time_uptime;
1302 	}
1303 
1304 	bioq_remove(&vol->v_inflight, bp);
1305 	if (vol->v_pending_lock && g_raid_is_in_locked_range(vol, bp))
1306 		g_raid_finish_with_locked_ranges(vol, bp);
1307 	getmicrouptime(&vol->v_last_done);
1308 	g_io_deliver(bp, error);
1309 }
1310 
1311 int
g_raid_lock_range(struct g_raid_volume * vol,off_t off,off_t len,struct bio * ignore,void * argp)1312 g_raid_lock_range(struct g_raid_volume *vol, off_t off, off_t len,
1313     struct bio *ignore, void *argp)
1314 {
1315 	struct g_raid_softc *sc;
1316 	struct g_raid_lock *lp;
1317 	struct bio *bp;
1318 
1319 	sc = vol->v_softc;
1320 	lp = malloc(sizeof(*lp), M_RAID, M_WAITOK | M_ZERO);
1321 	LIST_INSERT_HEAD(&vol->v_locks, lp, l_next);
1322 	lp->l_offset = off;
1323 	lp->l_length = len;
1324 	lp->l_callback_arg = argp;
1325 
1326 	lp->l_pending = 0;
1327 	TAILQ_FOREACH(bp, &vol->v_inflight.queue, bio_queue) {
1328 		if (bp != ignore && g_raid_bio_overlaps(bp, off, len))
1329 			lp->l_pending++;
1330 	}
1331 
1332 	/*
1333 	 * If there are any writes that are pending, we return EBUSY.  All
1334 	 * callers will have to wait until all pending writes clear.
1335 	 */
1336 	if (lp->l_pending > 0) {
1337 		vol->v_pending_lock = 1;
1338 		G_RAID_DEBUG1(4, sc, "Locking range %jd to %jd deferred %d pend",
1339 		    (intmax_t)off, (intmax_t)(off+len), lp->l_pending);
1340 		return (EBUSY);
1341 	}
1342 	G_RAID_DEBUG1(4, sc, "Locking range %jd to %jd",
1343 	    (intmax_t)off, (intmax_t)(off+len));
1344 	G_RAID_TR_LOCKED(vol->v_tr, lp->l_callback_arg);
1345 	return (0);
1346 }
1347 
1348 int
g_raid_unlock_range(struct g_raid_volume * vol,off_t off,off_t len)1349 g_raid_unlock_range(struct g_raid_volume *vol, off_t off, off_t len)
1350 {
1351 	struct g_raid_lock *lp;
1352 	struct g_raid_softc *sc;
1353 	struct bio *bp;
1354 
1355 	sc = vol->v_softc;
1356 	LIST_FOREACH(lp, &vol->v_locks, l_next) {
1357 		if (lp->l_offset == off && lp->l_length == len) {
1358 			LIST_REMOVE(lp, l_next);
1359 			/* XXX
1360 			 * Right now we just put them all back on the queue
1361 			 * and hope for the best.  We hope this because any
1362 			 * locked ranges will go right back on this list
1363 			 * when the worker thread runs.
1364 			 * XXX
1365 			 */
1366 			G_RAID_DEBUG1(4, sc, "Unlocked %jd to %jd",
1367 			    (intmax_t)lp->l_offset,
1368 			    (intmax_t)(lp->l_offset+lp->l_length));
1369 			mtx_lock(&sc->sc_queue_mtx);
1370 			while ((bp = bioq_takefirst(&vol->v_locked)) != NULL)
1371 				bioq_insert_tail(&sc->sc_queue, bp);
1372 			mtx_unlock(&sc->sc_queue_mtx);
1373 			free(lp, M_RAID);
1374 			return (0);
1375 		}
1376 	}
1377 	return (EINVAL);
1378 }
1379 
1380 void
g_raid_subdisk_iostart(struct g_raid_subdisk * sd,struct bio * bp)1381 g_raid_subdisk_iostart(struct g_raid_subdisk *sd, struct bio *bp)
1382 {
1383 	struct g_consumer *cp;
1384 	struct g_raid_disk *disk, *tdisk;
1385 
1386 	bp->bio_caller1 = sd;
1387 
1388 	/*
1389 	 * Make sure that the disk is present. Generally it is a task of
1390 	 * transformation layers to not send requests to absent disks, but
1391 	 * it is better to be safe and report situation then sorry.
1392 	 */
1393 	if (sd->sd_disk == NULL) {
1394 		G_RAID_LOGREQ(0, bp, "Warning! I/O request to an absent disk!");
1395 nodisk:
1396 		bp->bio_from = NULL;
1397 		bp->bio_to = NULL;
1398 		bp->bio_error = ENXIO;
1399 		g_raid_disk_done(bp);
1400 		return;
1401 	}
1402 	disk = sd->sd_disk;
1403 	if (disk->d_state != G_RAID_DISK_S_ACTIVE &&
1404 	    disk->d_state != G_RAID_DISK_S_FAILED) {
1405 		G_RAID_LOGREQ(0, bp, "Warning! I/O request to a disk in a "
1406 		    "wrong state (%s)!", g_raid_disk_state2str(disk->d_state));
1407 		goto nodisk;
1408 	}
1409 
1410 	cp = disk->d_consumer;
1411 	bp->bio_from = cp;
1412 	bp->bio_to = cp->provider;
1413 	cp->index++;
1414 
1415 	/* Update average disks load. */
1416 	TAILQ_FOREACH(tdisk, &sd->sd_softc->sc_disks, d_next) {
1417 		if (tdisk->d_consumer == NULL)
1418 			tdisk->d_load = 0;
1419 		else
1420 			tdisk->d_load = (tdisk->d_consumer->index *
1421 			    G_RAID_SUBDISK_LOAD_SCALE + tdisk->d_load * 7) / 8;
1422 	}
1423 
1424 	disk->d_last_offset = bp->bio_offset + bp->bio_length;
1425 	if (dumping) {
1426 		G_RAID_LOGREQ(3, bp, "Sending dumping request.");
1427 		if (bp->bio_cmd == BIO_WRITE) {
1428 			bp->bio_error = g_raid_subdisk_kerneldump(sd,
1429 			    bp->bio_data, bp->bio_offset, bp->bio_length);
1430 		} else
1431 			bp->bio_error = EOPNOTSUPP;
1432 		g_raid_disk_done(bp);
1433 	} else {
1434 		bp->bio_done = g_raid_disk_done;
1435 		bp->bio_offset += sd->sd_offset;
1436 		G_RAID_LOGREQ(3, bp, "Sending request.");
1437 		g_io_request(bp, cp);
1438 	}
1439 }
1440 
1441 int
g_raid_subdisk_kerneldump(struct g_raid_subdisk * sd,void * virtual,off_t offset,size_t length)1442 g_raid_subdisk_kerneldump(struct g_raid_subdisk *sd, void *virtual,
1443     off_t offset, size_t length)
1444 {
1445 
1446 	if (sd->sd_disk == NULL)
1447 		return (ENXIO);
1448 	if (sd->sd_disk->d_kd.di.dumper == NULL)
1449 		return (EOPNOTSUPP);
1450 	return (dump_write(&sd->sd_disk->d_kd.di, virtual,
1451 	    sd->sd_disk->d_kd.di.mediaoffset + sd->sd_offset + offset, length));
1452 }
1453 
1454 static void
g_raid_disk_done(struct bio * bp)1455 g_raid_disk_done(struct bio *bp)
1456 {
1457 	struct g_raid_softc *sc;
1458 	struct g_raid_subdisk *sd;
1459 
1460 	sd = bp->bio_caller1;
1461 	sc = sd->sd_softc;
1462 	mtx_lock(&sc->sc_queue_mtx);
1463 	bioq_insert_tail(&sc->sc_queue, bp);
1464 	mtx_unlock(&sc->sc_queue_mtx);
1465 	if (!dumping)
1466 		wakeup(sc);
1467 }
1468 
1469 static void
g_raid_disk_done_request(struct bio * bp)1470 g_raid_disk_done_request(struct bio *bp)
1471 {
1472 	struct g_raid_softc *sc;
1473 	struct g_raid_disk *disk;
1474 	struct g_raid_subdisk *sd;
1475 	struct g_raid_volume *vol;
1476 
1477 	g_topology_assert_not();
1478 
1479 	G_RAID_LOGREQ(3, bp, "Disk request done: %d.", bp->bio_error);
1480 	sd = bp->bio_caller1;
1481 	sc = sd->sd_softc;
1482 	vol = sd->sd_volume;
1483 	if (bp->bio_from != NULL) {
1484 		bp->bio_from->index--;
1485 		disk = bp->bio_from->private;
1486 		if (disk == NULL)
1487 			g_raid_kill_consumer(sc, bp->bio_from);
1488 	}
1489 	bp->bio_offset -= sd->sd_offset;
1490 
1491 	G_RAID_TR_IODONE(vol->v_tr, sd, bp);
1492 }
1493 
1494 static void
g_raid_handle_event(struct g_raid_softc * sc,struct g_raid_event * ep)1495 g_raid_handle_event(struct g_raid_softc *sc, struct g_raid_event *ep)
1496 {
1497 
1498 	if ((ep->e_flags & G_RAID_EVENT_VOLUME) != 0)
1499 		ep->e_error = g_raid_update_volume(ep->e_tgt, ep->e_event);
1500 	else if ((ep->e_flags & G_RAID_EVENT_DISK) != 0)
1501 		ep->e_error = g_raid_update_disk(ep->e_tgt, ep->e_event);
1502 	else if ((ep->e_flags & G_RAID_EVENT_SUBDISK) != 0)
1503 		ep->e_error = g_raid_update_subdisk(ep->e_tgt, ep->e_event);
1504 	else
1505 		ep->e_error = g_raid_update_node(ep->e_tgt, ep->e_event);
1506 	if ((ep->e_flags & G_RAID_EVENT_WAIT) == 0) {
1507 		KASSERT(ep->e_error == 0,
1508 		    ("Error cannot be handled."));
1509 		g_raid_event_free(ep);
1510 	} else {
1511 		ep->e_flags |= G_RAID_EVENT_DONE;
1512 		G_RAID_DEBUG1(4, sc, "Waking up %p.", ep);
1513 		mtx_lock(&sc->sc_queue_mtx);
1514 		wakeup(ep);
1515 		mtx_unlock(&sc->sc_queue_mtx);
1516 	}
1517 }
1518 
1519 /*
1520  * Worker thread.
1521  */
1522 static void
g_raid_worker(void * arg)1523 g_raid_worker(void *arg)
1524 {
1525 	struct g_raid_softc *sc;
1526 	struct g_raid_event *ep;
1527 	struct g_raid_volume *vol;
1528 	struct bio *bp;
1529 	struct timeval now, t;
1530 	int timeout, rv;
1531 
1532 	sc = arg;
1533 	thread_lock(curthread);
1534 	sched_prio(curthread, PRIBIO);
1535 	thread_unlock(curthread);
1536 
1537 	sx_xlock(&sc->sc_lock);
1538 	for (;;) {
1539 		mtx_lock(&sc->sc_queue_mtx);
1540 		/*
1541 		 * First take a look at events.
1542 		 * This is important to handle events before any I/O requests.
1543 		 */
1544 		bp = NULL;
1545 		vol = NULL;
1546 		rv = 0;
1547 		ep = TAILQ_FIRST(&sc->sc_events);
1548 		if (ep != NULL)
1549 			TAILQ_REMOVE(&sc->sc_events, ep, e_next);
1550 		else if ((bp = bioq_takefirst(&sc->sc_queue)) != NULL)
1551 			;
1552 		else {
1553 			getmicrouptime(&now);
1554 			t = now;
1555 			TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1556 				if (bioq_first(&vol->v_inflight) == NULL &&
1557 				    vol->v_tr &&
1558 				    timevalcmp(&vol->v_last_done, &t, < ))
1559 					t = vol->v_last_done;
1560 			}
1561 			timevalsub(&t, &now);
1562 			timeout = g_raid_idle_threshold +
1563 			    t.tv_sec * 1000000 + t.tv_usec;
1564 			if (timeout > 0) {
1565 				/*
1566 				 * Two steps to avoid overflows at HZ=1000
1567 				 * and idle timeouts > 2.1s.  Some rounding
1568 				 * errors can occur, but they are < 1tick,
1569 				 * which is deemed to be close enough for
1570 				 * this purpose.
1571 				 */
1572 				int micpertic = 1000000 / hz;
1573 				timeout = (timeout + micpertic - 1) / micpertic;
1574 				sx_xunlock(&sc->sc_lock);
1575 				MSLEEP(rv, sc, &sc->sc_queue_mtx,
1576 				    PRIBIO | PDROP, "-", timeout);
1577 				sx_xlock(&sc->sc_lock);
1578 				goto process;
1579 			} else
1580 				rv = EWOULDBLOCK;
1581 		}
1582 		mtx_unlock(&sc->sc_queue_mtx);
1583 process:
1584 		if (ep != NULL) {
1585 			g_raid_handle_event(sc, ep);
1586 		} else if (bp != NULL) {
1587 			if (bp->bio_to != NULL &&
1588 			    bp->bio_to->geom == sc->sc_geom)
1589 				g_raid_start_request(bp);
1590 			else
1591 				g_raid_disk_done_request(bp);
1592 		} else if (rv == EWOULDBLOCK) {
1593 			TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1594 				g_raid_clean(vol, -1);
1595 				if (bioq_first(&vol->v_inflight) == NULL &&
1596 				    vol->v_tr) {
1597 					t.tv_sec = g_raid_idle_threshold / 1000000;
1598 					t.tv_usec = g_raid_idle_threshold % 1000000;
1599 					timevaladd(&t, &vol->v_last_done);
1600 					getmicrouptime(&now);
1601 					if (timevalcmp(&t, &now, <= )) {
1602 						G_RAID_TR_IDLE(vol->v_tr);
1603 						vol->v_last_done = now;
1604 					}
1605 				}
1606 			}
1607 		}
1608 		if (sc->sc_stopping == G_RAID_DESTROY_HARD)
1609 			g_raid_destroy_node(sc, 1);	/* May not return. */
1610 	}
1611 }
1612 
1613 static void
g_raid_poll(struct g_raid_softc * sc)1614 g_raid_poll(struct g_raid_softc *sc)
1615 {
1616 	struct g_raid_event *ep;
1617 	struct bio *bp;
1618 
1619 	sx_xlock(&sc->sc_lock);
1620 	mtx_lock(&sc->sc_queue_mtx);
1621 	/*
1622 	 * First take a look at events.
1623 	 * This is important to handle events before any I/O requests.
1624 	 */
1625 	ep = TAILQ_FIRST(&sc->sc_events);
1626 	if (ep != NULL) {
1627 		TAILQ_REMOVE(&sc->sc_events, ep, e_next);
1628 		mtx_unlock(&sc->sc_queue_mtx);
1629 		g_raid_handle_event(sc, ep);
1630 		goto out;
1631 	}
1632 	bp = bioq_takefirst(&sc->sc_queue);
1633 	if (bp != NULL) {
1634 		mtx_unlock(&sc->sc_queue_mtx);
1635 		if (bp->bio_from == NULL ||
1636 		    bp->bio_from->geom != sc->sc_geom)
1637 			g_raid_start_request(bp);
1638 		else
1639 			g_raid_disk_done_request(bp);
1640 	}
1641 out:
1642 	sx_xunlock(&sc->sc_lock);
1643 }
1644 
1645 static void
g_raid_launch_provider(struct g_raid_volume * vol)1646 g_raid_launch_provider(struct g_raid_volume *vol)
1647 {
1648 	struct g_raid_disk *disk;
1649 	struct g_raid_subdisk *sd;
1650 	struct g_raid_softc *sc;
1651 	struct g_provider *pp;
1652 	char name[G_RAID_MAX_VOLUMENAME];
1653 	off_t off;
1654 	int i;
1655 
1656 	sc = vol->v_softc;
1657 	sx_assert(&sc->sc_lock, SX_LOCKED);
1658 
1659 	g_topology_lock();
1660 	/* Try to name provider with volume name. */
1661 	snprintf(name, sizeof(name), "raid/%s", vol->v_name);
1662 	if (g_raid_name_format == 0 || vol->v_name[0] == 0 ||
1663 	    g_provider_by_name(name) != NULL) {
1664 		/* Otherwise use sequential volume number. */
1665 		snprintf(name, sizeof(name), "raid/r%d", vol->v_global_id);
1666 	}
1667 
1668 	pp = g_new_providerf(sc->sc_geom, "%s", name);
1669 	pp->flags |= G_PF_DIRECT_RECEIVE;
1670 	if (vol->v_tr->tro_class->trc_accept_unmapped) {
1671 		pp->flags |= G_PF_ACCEPT_UNMAPPED;
1672 		for (i = 0; i < vol->v_disks_count; i++) {
1673 			sd = &vol->v_subdisks[i];
1674 			if (sd->sd_state == G_RAID_SUBDISK_S_NONE)
1675 				continue;
1676 			if ((sd->sd_disk->d_consumer->provider->flags &
1677 			    G_PF_ACCEPT_UNMAPPED) == 0)
1678 				pp->flags &= ~G_PF_ACCEPT_UNMAPPED;
1679 		}
1680 	}
1681 	pp->private = vol;
1682 	pp->mediasize = vol->v_mediasize;
1683 	pp->sectorsize = vol->v_sectorsize;
1684 	pp->stripesize = 0;
1685 	pp->stripeoffset = 0;
1686 	if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1 ||
1687 	    vol->v_raid_level == G_RAID_VOLUME_RL_RAID3 ||
1688 	    vol->v_raid_level == G_RAID_VOLUME_RL_SINGLE ||
1689 	    vol->v_raid_level == G_RAID_VOLUME_RL_CONCAT) {
1690 		if ((disk = vol->v_subdisks[0].sd_disk) != NULL &&
1691 		    disk->d_consumer != NULL &&
1692 		    disk->d_consumer->provider != NULL) {
1693 			pp->stripesize = disk->d_consumer->provider->stripesize;
1694 			off = disk->d_consumer->provider->stripeoffset;
1695 			pp->stripeoffset = off + vol->v_subdisks[0].sd_offset;
1696 			if (off > 0)
1697 				pp->stripeoffset %= off;
1698 		}
1699 		if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID3) {
1700 			pp->stripesize *= (vol->v_disks_count - 1);
1701 			pp->stripeoffset *= (vol->v_disks_count - 1);
1702 		}
1703 	} else
1704 		pp->stripesize = vol->v_strip_size;
1705 	vol->v_provider = pp;
1706 	g_error_provider(pp, 0);
1707 	g_topology_unlock();
1708 	G_RAID_DEBUG1(0, sc, "Provider %s for volume %s created.",
1709 	    pp->name, vol->v_name);
1710 }
1711 
1712 static void
g_raid_destroy_provider(struct g_raid_volume * vol)1713 g_raid_destroy_provider(struct g_raid_volume *vol)
1714 {
1715 	struct g_raid_softc *sc;
1716 	struct g_provider *pp;
1717 	struct bio *bp, *tmp;
1718 
1719 	g_topology_assert_not();
1720 	sc = vol->v_softc;
1721 	pp = vol->v_provider;
1722 	KASSERT(pp != NULL, ("NULL provider (volume=%s).", vol->v_name));
1723 
1724 	g_topology_lock();
1725 	g_error_provider(pp, ENXIO);
1726 	mtx_lock(&sc->sc_queue_mtx);
1727 	TAILQ_FOREACH_SAFE(bp, &sc->sc_queue.queue, bio_queue, tmp) {
1728 		if (bp->bio_to != pp)
1729 			continue;
1730 		bioq_remove(&sc->sc_queue, bp);
1731 		g_io_deliver(bp, ENXIO);
1732 	}
1733 	mtx_unlock(&sc->sc_queue_mtx);
1734 	G_RAID_DEBUG1(0, sc, "Provider %s for volume %s destroyed.",
1735 	    pp->name, vol->v_name);
1736 	g_wither_provider(pp, ENXIO);
1737 	g_topology_unlock();
1738 	vol->v_provider = NULL;
1739 }
1740 
1741 /*
1742  * Update device state.
1743  */
1744 static int
g_raid_update_volume(struct g_raid_volume * vol,u_int event)1745 g_raid_update_volume(struct g_raid_volume *vol, u_int event)
1746 {
1747 	struct g_raid_softc *sc;
1748 
1749 	sc = vol->v_softc;
1750 	sx_assert(&sc->sc_lock, SX_XLOCKED);
1751 
1752 	G_RAID_DEBUG1(2, sc, "Event %s for volume %s.",
1753 	    g_raid_volume_event2str(event),
1754 	    vol->v_name);
1755 	switch (event) {
1756 	case G_RAID_VOLUME_E_DOWN:
1757 		if (vol->v_provider != NULL)
1758 			g_raid_destroy_provider(vol);
1759 		break;
1760 	case G_RAID_VOLUME_E_UP:
1761 		if (vol->v_provider == NULL)
1762 			g_raid_launch_provider(vol);
1763 		break;
1764 	case G_RAID_VOLUME_E_START:
1765 		if (vol->v_tr)
1766 			G_RAID_TR_START(vol->v_tr);
1767 		return (0);
1768 	default:
1769 		if (sc->sc_md)
1770 			G_RAID_MD_VOLUME_EVENT(sc->sc_md, vol, event);
1771 		return (0);
1772 	}
1773 
1774 	/* Manage root mount release. */
1775 	if (vol->v_starting) {
1776 		vol->v_starting = 0;
1777 		G_RAID_DEBUG1(1, sc, "root_mount_rel %p", vol->v_rootmount);
1778 		root_mount_rel(vol->v_rootmount);
1779 		vol->v_rootmount = NULL;
1780 	}
1781 	if (vol->v_stopping && vol->v_provider_open == 0)
1782 		g_raid_destroy_volume(vol);
1783 	return (0);
1784 }
1785 
1786 /*
1787  * Update subdisk state.
1788  */
1789 static int
g_raid_update_subdisk(struct g_raid_subdisk * sd,u_int event)1790 g_raid_update_subdisk(struct g_raid_subdisk *sd, u_int event)
1791 {
1792 	struct g_raid_softc *sc;
1793 	struct g_raid_volume *vol;
1794 
1795 	sc = sd->sd_softc;
1796 	vol = sd->sd_volume;
1797 	sx_assert(&sc->sc_lock, SX_XLOCKED);
1798 
1799 	G_RAID_DEBUG1(2, sc, "Event %s for subdisk %s:%d-%s.",
1800 	    g_raid_subdisk_event2str(event),
1801 	    vol->v_name, sd->sd_pos,
1802 	    sd->sd_disk ? g_raid_get_diskname(sd->sd_disk) : "[none]");
1803 	if (vol->v_tr)
1804 		G_RAID_TR_EVENT(vol->v_tr, sd, event);
1805 
1806 	return (0);
1807 }
1808 
1809 /*
1810  * Update disk state.
1811  */
1812 static int
g_raid_update_disk(struct g_raid_disk * disk,u_int event)1813 g_raid_update_disk(struct g_raid_disk *disk, u_int event)
1814 {
1815 	struct g_raid_softc *sc;
1816 
1817 	sc = disk->d_softc;
1818 	sx_assert(&sc->sc_lock, SX_XLOCKED);
1819 
1820 	G_RAID_DEBUG1(2, sc, "Event %s for disk %s.",
1821 	    g_raid_disk_event2str(event),
1822 	    g_raid_get_diskname(disk));
1823 
1824 	if (sc->sc_md)
1825 		G_RAID_MD_EVENT(sc->sc_md, disk, event);
1826 	return (0);
1827 }
1828 
1829 /*
1830  * Node event.
1831  */
1832 static int
g_raid_update_node(struct g_raid_softc * sc,u_int event)1833 g_raid_update_node(struct g_raid_softc *sc, u_int event)
1834 {
1835 	sx_assert(&sc->sc_lock, SX_XLOCKED);
1836 
1837 	G_RAID_DEBUG1(2, sc, "Event %s for the array.",
1838 	    g_raid_node_event2str(event));
1839 
1840 	if (event == G_RAID_NODE_E_WAKE)
1841 		return (0);
1842 	if (sc->sc_md)
1843 		G_RAID_MD_EVENT(sc->sc_md, NULL, event);
1844 	return (0);
1845 }
1846 
1847 static int
g_raid_access(struct g_provider * pp,int acr,int acw,int ace)1848 g_raid_access(struct g_provider *pp, int acr, int acw, int ace)
1849 {
1850 	struct g_raid_volume *vol;
1851 	struct g_raid_softc *sc;
1852 	int dcw, opens, error = 0;
1853 
1854 	g_topology_assert();
1855 	sc = pp->geom->softc;
1856 	vol = pp->private;
1857 	KASSERT(sc != NULL, ("NULL softc (provider=%s).", pp->name));
1858 	KASSERT(vol != NULL, ("NULL volume (provider=%s).", pp->name));
1859 
1860 	G_RAID_DEBUG1(2, sc, "Access request for %s: r%dw%de%d.", pp->name,
1861 	    acr, acw, ace);
1862 	dcw = pp->acw + acw;
1863 
1864 	g_topology_unlock();
1865 	sx_xlock(&sc->sc_lock);
1866 	/* Deny new opens while dying. */
1867 	if (sc->sc_stopping != 0 && (acr > 0 || acw > 0 || ace > 0)) {
1868 		error = ENXIO;
1869 		goto out;
1870 	}
1871 	/* Deny write opens for read-only volumes. */
1872 	if (vol->v_read_only && acw > 0) {
1873 		error = EROFS;
1874 		goto out;
1875 	}
1876 	if (dcw == 0)
1877 		g_raid_clean(vol, dcw);
1878 	vol->v_provider_open += acr + acw + ace;
1879 	/* Handle delayed node destruction. */
1880 	if (sc->sc_stopping == G_RAID_DESTROY_DELAYED &&
1881 	    vol->v_provider_open == 0) {
1882 		/* Count open volumes. */
1883 		opens = g_raid_nopens(sc);
1884 		if (opens == 0) {
1885 			sc->sc_stopping = G_RAID_DESTROY_HARD;
1886 			/* Wake up worker to make it selfdestruct. */
1887 			g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
1888 		}
1889 	}
1890 	/* Handle open volume destruction. */
1891 	if (vol->v_stopping && vol->v_provider_open == 0)
1892 		g_raid_destroy_volume(vol);
1893 out:
1894 	sx_xunlock(&sc->sc_lock);
1895 	g_topology_lock();
1896 	return (error);
1897 }
1898 
1899 struct g_raid_softc *
g_raid_create_node(struct g_class * mp,const char * name,struct g_raid_md_object * md)1900 g_raid_create_node(struct g_class *mp,
1901     const char *name, struct g_raid_md_object *md)
1902 {
1903 	struct g_raid_softc *sc;
1904 	struct g_geom *gp;
1905 	int error;
1906 
1907 	g_topology_assert();
1908 	G_RAID_DEBUG(1, "Creating array %s.", name);
1909 
1910 	gp = g_new_geom(mp, name);
1911 	sc = malloc(sizeof(*sc), M_RAID, M_WAITOK | M_ZERO);
1912 	gp->start = g_raid_start;
1913 	gp->orphan = g_raid_orphan;
1914 	gp->access = g_raid_access;
1915 	gp->dumpconf = g_raid_dumpconf;
1916 
1917 	sc->sc_md = md;
1918 	sc->sc_geom = gp;
1919 	sc->sc_flags = 0;
1920 	TAILQ_INIT(&sc->sc_volumes);
1921 	TAILQ_INIT(&sc->sc_disks);
1922 	sx_init(&sc->sc_lock, "graid:lock");
1923 	mtx_init(&sc->sc_queue_mtx, "graid:queue", NULL, MTX_DEF);
1924 	TAILQ_INIT(&sc->sc_events);
1925 	bioq_init(&sc->sc_queue);
1926 	gp->softc = sc;
1927 	error = kproc_create(g_raid_worker, sc, &sc->sc_worker, 0, 0,
1928 	    "g_raid %s", name);
1929 	if (error != 0) {
1930 		G_RAID_DEBUG(0, "Cannot create kernel thread for %s.", name);
1931 		mtx_destroy(&sc->sc_queue_mtx);
1932 		sx_destroy(&sc->sc_lock);
1933 		g_destroy_geom(sc->sc_geom);
1934 		free(sc, M_RAID);
1935 		return (NULL);
1936 	}
1937 
1938 	G_RAID_DEBUG1(0, sc, "Array %s created.", name);
1939 	return (sc);
1940 }
1941 
1942 struct g_raid_volume *
g_raid_create_volume(struct g_raid_softc * sc,const char * name,int id)1943 g_raid_create_volume(struct g_raid_softc *sc, const char *name, int id)
1944 {
1945 	struct g_raid_volume	*vol, *vol1;
1946 	int i;
1947 
1948 	G_RAID_DEBUG1(1, sc, "Creating volume %s.", name);
1949 	vol = malloc(sizeof(*vol), M_RAID, M_WAITOK | M_ZERO);
1950 	vol->v_softc = sc;
1951 	strlcpy(vol->v_name, name, G_RAID_MAX_VOLUMENAME);
1952 	vol->v_state = G_RAID_VOLUME_S_STARTING;
1953 	vol->v_raid_level = G_RAID_VOLUME_RL_UNKNOWN;
1954 	vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_UNKNOWN;
1955 	vol->v_rotate_parity = 1;
1956 	bioq_init(&vol->v_inflight);
1957 	bioq_init(&vol->v_locked);
1958 	LIST_INIT(&vol->v_locks);
1959 	for (i = 0; i < G_RAID_MAX_SUBDISKS; i++) {
1960 		vol->v_subdisks[i].sd_softc = sc;
1961 		vol->v_subdisks[i].sd_volume = vol;
1962 		vol->v_subdisks[i].sd_pos = i;
1963 		vol->v_subdisks[i].sd_state = G_RAID_DISK_S_NONE;
1964 	}
1965 
1966 	/* Find free ID for this volume. */
1967 	g_topology_lock();
1968 	vol1 = vol;
1969 	if (id >= 0) {
1970 		LIST_FOREACH(vol1, &g_raid_volumes, v_global_next) {
1971 			if (vol1->v_global_id == id)
1972 				break;
1973 		}
1974 	}
1975 	if (vol1 != NULL) {
1976 		for (id = 0; ; id++) {
1977 			LIST_FOREACH(vol1, &g_raid_volumes, v_global_next) {
1978 				if (vol1->v_global_id == id)
1979 					break;
1980 			}
1981 			if (vol1 == NULL)
1982 				break;
1983 		}
1984 	}
1985 	vol->v_global_id = id;
1986 	LIST_INSERT_HEAD(&g_raid_volumes, vol, v_global_next);
1987 	g_topology_unlock();
1988 
1989 	/* Delay root mounting. */
1990 	vol->v_rootmount = root_mount_hold("GRAID");
1991 	G_RAID_DEBUG1(1, sc, "root_mount_hold %p", vol->v_rootmount);
1992 	vol->v_starting = 1;
1993 	TAILQ_INSERT_TAIL(&sc->sc_volumes, vol, v_next);
1994 	return (vol);
1995 }
1996 
1997 struct g_raid_disk *
g_raid_create_disk(struct g_raid_softc * sc)1998 g_raid_create_disk(struct g_raid_softc *sc)
1999 {
2000 	struct g_raid_disk	*disk;
2001 
2002 	G_RAID_DEBUG1(1, sc, "Creating disk.");
2003 	disk = malloc(sizeof(*disk), M_RAID, M_WAITOK | M_ZERO);
2004 	disk->d_softc = sc;
2005 	disk->d_state = G_RAID_DISK_S_NONE;
2006 	TAILQ_INIT(&disk->d_subdisks);
2007 	TAILQ_INSERT_TAIL(&sc->sc_disks, disk, d_next);
2008 	return (disk);
2009 }
2010 
g_raid_start_volume(struct g_raid_volume * vol)2011 int g_raid_start_volume(struct g_raid_volume *vol)
2012 {
2013 	struct g_raid_tr_class *class;
2014 	struct g_raid_tr_object *obj;
2015 	int status;
2016 
2017 	G_RAID_DEBUG1(2, vol->v_softc, "Starting volume %s.", vol->v_name);
2018 	LIST_FOREACH(class, &g_raid_tr_classes, trc_list) {
2019 		if (!class->trc_enable)
2020 			continue;
2021 		G_RAID_DEBUG1(2, vol->v_softc,
2022 		    "Tasting volume %s for %s transformation.",
2023 		    vol->v_name, class->name);
2024 		obj = (void *)kobj_create((kobj_class_t)class, M_RAID,
2025 		    M_WAITOK);
2026 		obj->tro_class = class;
2027 		obj->tro_volume = vol;
2028 		status = G_RAID_TR_TASTE(obj, vol);
2029 		if (status != G_RAID_TR_TASTE_FAIL)
2030 			break;
2031 		kobj_delete((kobj_t)obj, M_RAID);
2032 	}
2033 	if (class == NULL) {
2034 		G_RAID_DEBUG1(0, vol->v_softc,
2035 		    "No transformation module found for %s.",
2036 		    vol->v_name);
2037 		vol->v_tr = NULL;
2038 		g_raid_change_volume_state(vol, G_RAID_VOLUME_S_UNSUPPORTED);
2039 		g_raid_event_send(vol, G_RAID_VOLUME_E_DOWN,
2040 		    G_RAID_EVENT_VOLUME);
2041 		return (-1);
2042 	}
2043 	G_RAID_DEBUG1(2, vol->v_softc,
2044 	    "Transformation module %s chosen for %s.",
2045 	    class->name, vol->v_name);
2046 	vol->v_tr = obj;
2047 	return (0);
2048 }
2049 
2050 int
g_raid_destroy_node(struct g_raid_softc * sc,int worker)2051 g_raid_destroy_node(struct g_raid_softc *sc, int worker)
2052 {
2053 	struct g_raid_volume *vol, *tmpv;
2054 	struct g_raid_disk *disk, *tmpd;
2055 	int error = 0;
2056 
2057 	sc->sc_stopping = G_RAID_DESTROY_HARD;
2058 	TAILQ_FOREACH_SAFE(vol, &sc->sc_volumes, v_next, tmpv) {
2059 		if (g_raid_destroy_volume(vol))
2060 			error = EBUSY;
2061 	}
2062 	if (error)
2063 		return (error);
2064 	TAILQ_FOREACH_SAFE(disk, &sc->sc_disks, d_next, tmpd) {
2065 		if (g_raid_destroy_disk(disk))
2066 			error = EBUSY;
2067 	}
2068 	if (error)
2069 		return (error);
2070 	if (sc->sc_md) {
2071 		G_RAID_MD_FREE(sc->sc_md);
2072 		kobj_delete((kobj_t)sc->sc_md, M_RAID);
2073 		sc->sc_md = NULL;
2074 	}
2075 	if (sc->sc_geom != NULL) {
2076 		G_RAID_DEBUG1(0, sc, "Array %s destroyed.", sc->sc_name);
2077 		g_topology_lock();
2078 		sc->sc_geom->softc = NULL;
2079 		g_wither_geom(sc->sc_geom, ENXIO);
2080 		g_topology_unlock();
2081 		sc->sc_geom = NULL;
2082 	} else
2083 		G_RAID_DEBUG(1, "Array destroyed.");
2084 	if (worker) {
2085 		g_raid_event_cancel(sc, sc);
2086 		mtx_destroy(&sc->sc_queue_mtx);
2087 		sx_xunlock(&sc->sc_lock);
2088 		sx_destroy(&sc->sc_lock);
2089 		wakeup(&sc->sc_stopping);
2090 		free(sc, M_RAID);
2091 		curthread->td_pflags &= ~TDP_GEOM;
2092 		G_RAID_DEBUG(1, "Thread exiting.");
2093 		kproc_exit(0);
2094 	} else {
2095 		/* Wake up worker to make it selfdestruct. */
2096 		g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
2097 	}
2098 	return (0);
2099 }
2100 
2101 int
g_raid_destroy_volume(struct g_raid_volume * vol)2102 g_raid_destroy_volume(struct g_raid_volume *vol)
2103 {
2104 	struct g_raid_softc *sc;
2105 	struct g_raid_disk *disk;
2106 	int i;
2107 
2108 	sc = vol->v_softc;
2109 	G_RAID_DEBUG1(2, sc, "Destroying volume %s.", vol->v_name);
2110 	vol->v_stopping = 1;
2111 	if (vol->v_state != G_RAID_VOLUME_S_STOPPED) {
2112 		if (vol->v_tr) {
2113 			G_RAID_TR_STOP(vol->v_tr);
2114 			return (EBUSY);
2115 		} else
2116 			vol->v_state = G_RAID_VOLUME_S_STOPPED;
2117 	}
2118 	if (g_raid_event_check(sc, vol) != 0)
2119 		return (EBUSY);
2120 	if (vol->v_provider != NULL)
2121 		return (EBUSY);
2122 	if (vol->v_provider_open != 0)
2123 		return (EBUSY);
2124 	if (vol->v_tr) {
2125 		G_RAID_TR_FREE(vol->v_tr);
2126 		kobj_delete((kobj_t)vol->v_tr, M_RAID);
2127 		vol->v_tr = NULL;
2128 	}
2129 	if (vol->v_rootmount)
2130 		root_mount_rel(vol->v_rootmount);
2131 	g_topology_lock();
2132 	LIST_REMOVE(vol, v_global_next);
2133 	g_topology_unlock();
2134 	TAILQ_REMOVE(&sc->sc_volumes, vol, v_next);
2135 	for (i = 0; i < G_RAID_MAX_SUBDISKS; i++) {
2136 		g_raid_event_cancel(sc, &vol->v_subdisks[i]);
2137 		disk = vol->v_subdisks[i].sd_disk;
2138 		if (disk == NULL)
2139 			continue;
2140 		TAILQ_REMOVE(&disk->d_subdisks, &vol->v_subdisks[i], sd_next);
2141 	}
2142 	G_RAID_DEBUG1(2, sc, "Volume %s destroyed.", vol->v_name);
2143 	if (sc->sc_md)
2144 		G_RAID_MD_FREE_VOLUME(sc->sc_md, vol);
2145 	g_raid_event_cancel(sc, vol);
2146 	free(vol, M_RAID);
2147 	if (sc->sc_stopping == G_RAID_DESTROY_HARD) {
2148 		/* Wake up worker to let it selfdestruct. */
2149 		g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
2150 	}
2151 	return (0);
2152 }
2153 
2154 int
g_raid_destroy_disk(struct g_raid_disk * disk)2155 g_raid_destroy_disk(struct g_raid_disk *disk)
2156 {
2157 	struct g_raid_softc *sc;
2158 	struct g_raid_subdisk *sd, *tmp;
2159 
2160 	sc = disk->d_softc;
2161 	G_RAID_DEBUG1(2, sc, "Destroying disk.");
2162 	if (disk->d_consumer) {
2163 		g_raid_kill_consumer(sc, disk->d_consumer);
2164 		disk->d_consumer = NULL;
2165 	}
2166 	TAILQ_FOREACH_SAFE(sd, &disk->d_subdisks, sd_next, tmp) {
2167 		g_raid_change_subdisk_state(sd, G_RAID_SUBDISK_S_NONE);
2168 		g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED,
2169 		    G_RAID_EVENT_SUBDISK);
2170 		TAILQ_REMOVE(&disk->d_subdisks, sd, sd_next);
2171 		sd->sd_disk = NULL;
2172 	}
2173 	TAILQ_REMOVE(&sc->sc_disks, disk, d_next);
2174 	if (sc->sc_md)
2175 		G_RAID_MD_FREE_DISK(sc->sc_md, disk);
2176 	g_raid_event_cancel(sc, disk);
2177 	free(disk, M_RAID);
2178 	return (0);
2179 }
2180 
2181 int
g_raid_destroy(struct g_raid_softc * sc,int how)2182 g_raid_destroy(struct g_raid_softc *sc, int how)
2183 {
2184 	int error, opens;
2185 
2186 	g_topology_assert_not();
2187 	if (sc == NULL)
2188 		return (ENXIO);
2189 	sx_assert(&sc->sc_lock, SX_XLOCKED);
2190 
2191 	/* Count open volumes. */
2192 	opens = g_raid_nopens(sc);
2193 
2194 	/* React on some opened volumes. */
2195 	if (opens > 0) {
2196 		switch (how) {
2197 		case G_RAID_DESTROY_SOFT:
2198 			G_RAID_DEBUG1(1, sc,
2199 			    "%d volumes are still open.",
2200 			    opens);
2201 			sx_xunlock(&sc->sc_lock);
2202 			return (EBUSY);
2203 		case G_RAID_DESTROY_DELAYED:
2204 			G_RAID_DEBUG1(1, sc,
2205 			    "Array will be destroyed on last close.");
2206 			sc->sc_stopping = G_RAID_DESTROY_DELAYED;
2207 			sx_xunlock(&sc->sc_lock);
2208 			return (EBUSY);
2209 		case G_RAID_DESTROY_HARD:
2210 			G_RAID_DEBUG1(1, sc,
2211 			    "%d volumes are still open.",
2212 			    opens);
2213 		}
2214 	}
2215 
2216 	/* Mark node for destruction. */
2217 	sc->sc_stopping = G_RAID_DESTROY_HARD;
2218 	/* Wake up worker to let it selfdestruct. */
2219 	g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
2220 	/* Sleep until node destroyed. */
2221 	error = sx_sleep(&sc->sc_stopping, &sc->sc_lock,
2222 	    PRIBIO | PDROP, "r:destroy", hz * 3);
2223 	return (error == EWOULDBLOCK ? EBUSY : 0);
2224 }
2225 
2226 static void
g_raid_taste_orphan(struct g_consumer * cp)2227 g_raid_taste_orphan(struct g_consumer *cp)
2228 {
2229 
2230 	KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
2231 	    cp->provider->name));
2232 }
2233 
2234 static struct g_geom *
g_raid_taste(struct g_class * mp,struct g_provider * pp,int flags __unused)2235 g_raid_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
2236 {
2237 	struct g_consumer *cp;
2238 	struct g_geom *gp, *geom;
2239 	struct g_raid_md_class *class;
2240 	struct g_raid_md_object *obj;
2241 	int status;
2242 
2243 	g_topology_assert();
2244 	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
2245 	if (!g_raid_enable)
2246 		return (NULL);
2247 	G_RAID_DEBUG(2, "Tasting provider %s.", pp->name);
2248 
2249 	geom = NULL;
2250 	status = G_RAID_MD_TASTE_FAIL;
2251 	gp = g_new_geom(mp, "raid:taste");
2252 	/*
2253 	 * This orphan function should be never called.
2254 	 */
2255 	gp->orphan = g_raid_taste_orphan;
2256 	cp = g_new_consumer(gp);
2257 	cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
2258 	if (g_attach(cp, pp) != 0)
2259 		goto ofail2;
2260 	if (g_access(cp, 1, 0, 0) != 0)
2261 		goto ofail;
2262 
2263 	LIST_FOREACH(class, &g_raid_md_classes, mdc_list) {
2264 		if (!class->mdc_enable)
2265 			continue;
2266 		G_RAID_DEBUG(2, "Tasting provider %s for %s metadata.",
2267 		    pp->name, class->name);
2268 		obj = (void *)kobj_create((kobj_class_t)class, M_RAID,
2269 		    M_WAITOK);
2270 		obj->mdo_class = class;
2271 		status = G_RAID_MD_TASTE(obj, mp, cp, &geom);
2272 		if (status != G_RAID_MD_TASTE_NEW)
2273 			kobj_delete((kobj_t)obj, M_RAID);
2274 		if (status != G_RAID_MD_TASTE_FAIL)
2275 			break;
2276 	}
2277 
2278 	if (status == G_RAID_MD_TASTE_FAIL)
2279 		(void)g_access(cp, -1, 0, 0);
2280 ofail:
2281 	g_detach(cp);
2282 ofail2:
2283 	g_destroy_consumer(cp);
2284 	g_destroy_geom(gp);
2285 	G_RAID_DEBUG(2, "Tasting provider %s done.", pp->name);
2286 	return (geom);
2287 }
2288 
2289 int
g_raid_create_node_format(const char * format,struct gctl_req * req,struct g_geom ** gp)2290 g_raid_create_node_format(const char *format, struct gctl_req *req,
2291     struct g_geom **gp)
2292 {
2293 	struct g_raid_md_class *class;
2294 	struct g_raid_md_object *obj;
2295 	int status;
2296 
2297 	G_RAID_DEBUG(2, "Creating array for %s metadata.", format);
2298 	LIST_FOREACH(class, &g_raid_md_classes, mdc_list) {
2299 		if (strcasecmp(class->name, format) == 0)
2300 			break;
2301 	}
2302 	if (class == NULL) {
2303 		G_RAID_DEBUG(1, "No support for %s metadata.", format);
2304 		return (G_RAID_MD_TASTE_FAIL);
2305 	}
2306 	obj = (void *)kobj_create((kobj_class_t)class, M_RAID,
2307 	    M_WAITOK);
2308 	obj->mdo_class = class;
2309 	status = G_RAID_MD_CREATE_REQ(obj, &g_raid_class, req, gp);
2310 	if (status != G_RAID_MD_TASTE_NEW)
2311 		kobj_delete((kobj_t)obj, M_RAID);
2312 	return (status);
2313 }
2314 
2315 static int
g_raid_destroy_geom(struct gctl_req * req __unused,struct g_class * mp __unused,struct g_geom * gp)2316 g_raid_destroy_geom(struct gctl_req *req __unused,
2317     struct g_class *mp __unused, struct g_geom *gp)
2318 {
2319 	struct g_raid_softc *sc;
2320 	int error;
2321 
2322 	g_topology_unlock();
2323 	sc = gp->softc;
2324 	sx_xlock(&sc->sc_lock);
2325 	g_cancel_event(sc);
2326 	error = g_raid_destroy(gp->softc, G_RAID_DESTROY_SOFT);
2327 	g_topology_lock();
2328 	return (error);
2329 }
2330 
g_raid_write_metadata(struct g_raid_softc * sc,struct g_raid_volume * vol,struct g_raid_subdisk * sd,struct g_raid_disk * disk)2331 void g_raid_write_metadata(struct g_raid_softc *sc, struct g_raid_volume *vol,
2332     struct g_raid_subdisk *sd, struct g_raid_disk *disk)
2333 {
2334 
2335 	if (sc->sc_stopping == G_RAID_DESTROY_HARD)
2336 		return;
2337 	if (sc->sc_md)
2338 		G_RAID_MD_WRITE(sc->sc_md, vol, sd, disk);
2339 }
2340 
g_raid_fail_disk(struct g_raid_softc * sc,struct g_raid_subdisk * sd,struct g_raid_disk * disk)2341 void g_raid_fail_disk(struct g_raid_softc *sc,
2342     struct g_raid_subdisk *sd, struct g_raid_disk *disk)
2343 {
2344 
2345 	if (disk == NULL)
2346 		disk = sd->sd_disk;
2347 	if (disk == NULL) {
2348 		G_RAID_DEBUG1(0, sc, "Warning! Fail request to an absent disk!");
2349 		return;
2350 	}
2351 	if (disk->d_state != G_RAID_DISK_S_ACTIVE) {
2352 		G_RAID_DEBUG1(0, sc, "Warning! Fail request to a disk in a "
2353 		    "wrong state (%s)!", g_raid_disk_state2str(disk->d_state));
2354 		return;
2355 	}
2356 	if (sc->sc_md)
2357 		G_RAID_MD_FAIL_DISK(sc->sc_md, sd, disk);
2358 }
2359 
2360 static void
g_raid_dumpconf(struct sbuf * sb,const char * indent,struct g_geom * gp,struct g_consumer * cp,struct g_provider * pp)2361 g_raid_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
2362     struct g_consumer *cp, struct g_provider *pp)
2363 {
2364 	struct g_raid_softc *sc;
2365 	struct g_raid_volume *vol;
2366 	struct g_raid_subdisk *sd;
2367 	struct g_raid_disk *disk;
2368 	int i, s;
2369 
2370 	g_topology_assert();
2371 
2372 	sc = gp->softc;
2373 	if (sc == NULL)
2374 		return;
2375 	if (pp != NULL) {
2376 		vol = pp->private;
2377 		g_topology_unlock();
2378 		sx_xlock(&sc->sc_lock);
2379 		sbuf_printf(sb, "%s<descr>%s %s volume</descr>\n", indent,
2380 		    sc->sc_md->mdo_class->name,
2381 		    g_raid_volume_level2str(vol->v_raid_level,
2382 		    vol->v_raid_level_qualifier));
2383 		sbuf_printf(sb, "%s<Label>%s</Label>\n", indent,
2384 		    vol->v_name);
2385 		sbuf_printf(sb, "%s<RAIDLevel>%s</RAIDLevel>\n", indent,
2386 		    g_raid_volume_level2str(vol->v_raid_level,
2387 		    vol->v_raid_level_qualifier));
2388 		sbuf_printf(sb,
2389 		    "%s<Transformation>%s</Transformation>\n", indent,
2390 		    vol->v_tr ? vol->v_tr->tro_class->name : "NONE");
2391 		sbuf_printf(sb, "%s<Components>%u</Components>\n", indent,
2392 		    vol->v_disks_count);
2393 		sbuf_printf(sb, "%s<Strip>%u</Strip>\n", indent,
2394 		    vol->v_strip_size);
2395 		sbuf_printf(sb, "%s<State>%s</State>\n", indent,
2396 		    g_raid_volume_state2str(vol->v_state));
2397 		sbuf_printf(sb, "%s<Dirty>%s</Dirty>\n", indent,
2398 		    vol->v_dirty ? "Yes" : "No");
2399 		sbuf_printf(sb, "%s<Subdisks>", indent);
2400 		for (i = 0; i < vol->v_disks_count; i++) {
2401 			sd = &vol->v_subdisks[i];
2402 			if (sd->sd_disk != NULL &&
2403 			    sd->sd_disk->d_consumer != NULL) {
2404 				sbuf_printf(sb, "%s ",
2405 				    g_raid_get_diskname(sd->sd_disk));
2406 			} else {
2407 				sbuf_cat(sb, "NONE ");
2408 			}
2409 			sbuf_printf(sb, "(%s",
2410 			    g_raid_subdisk_state2str(sd->sd_state));
2411 			if (sd->sd_state == G_RAID_SUBDISK_S_REBUILD ||
2412 			    sd->sd_state == G_RAID_SUBDISK_S_RESYNC) {
2413 				sbuf_printf(sb, " %d%%",
2414 				    (int)(sd->sd_rebuild_pos * 100 /
2415 				     sd->sd_size));
2416 			}
2417 			sbuf_cat(sb, ")");
2418 			if (i + 1 < vol->v_disks_count)
2419 				sbuf_cat(sb, ", ");
2420 		}
2421 		sbuf_cat(sb, "</Subdisks>\n");
2422 		sx_xunlock(&sc->sc_lock);
2423 		g_topology_lock();
2424 	} else if (cp != NULL) {
2425 		disk = cp->private;
2426 		if (disk == NULL)
2427 			return;
2428 		g_topology_unlock();
2429 		sx_xlock(&sc->sc_lock);
2430 		sbuf_printf(sb, "%s<State>%s", indent,
2431 		    g_raid_disk_state2str(disk->d_state));
2432 		if (!TAILQ_EMPTY(&disk->d_subdisks)) {
2433 			sbuf_cat(sb, " (");
2434 			TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
2435 				sbuf_printf(sb, "%s",
2436 				    g_raid_subdisk_state2str(sd->sd_state));
2437 				if (sd->sd_state == G_RAID_SUBDISK_S_REBUILD ||
2438 				    sd->sd_state == G_RAID_SUBDISK_S_RESYNC) {
2439 					sbuf_printf(sb, " %d%%",
2440 					    (int)(sd->sd_rebuild_pos * 100 /
2441 					     sd->sd_size));
2442 				}
2443 				if (TAILQ_NEXT(sd, sd_next))
2444 					sbuf_cat(sb, ", ");
2445 			}
2446 			sbuf_cat(sb, ")");
2447 		}
2448 		sbuf_cat(sb, "</State>\n");
2449 		sbuf_printf(sb, "%s<Subdisks>", indent);
2450 		TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
2451 			sbuf_printf(sb, "r%d(%s):%d@%ju",
2452 			    sd->sd_volume->v_global_id,
2453 			    sd->sd_volume->v_name,
2454 			    sd->sd_pos, (uintmax_t)sd->sd_offset);
2455 			if (TAILQ_NEXT(sd, sd_next))
2456 				sbuf_cat(sb, ", ");
2457 		}
2458 		sbuf_cat(sb, "</Subdisks>\n");
2459 		sbuf_printf(sb, "%s<ReadErrors>%d</ReadErrors>\n", indent,
2460 		    disk->d_read_errs);
2461 		sx_xunlock(&sc->sc_lock);
2462 		g_topology_lock();
2463 	} else {
2464 		g_topology_unlock();
2465 		sx_xlock(&sc->sc_lock);
2466 		if (sc->sc_md) {
2467 			sbuf_printf(sb, "%s<Metadata>%s</Metadata>\n", indent,
2468 			    sc->sc_md->mdo_class->name);
2469 		}
2470 		if (!TAILQ_EMPTY(&sc->sc_volumes)) {
2471 			s = 0xff;
2472 			TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2473 				if (vol->v_state < s)
2474 					s = vol->v_state;
2475 			}
2476 			sbuf_printf(sb, "%s<State>%s</State>\n", indent,
2477 			    g_raid_volume_state2str(s));
2478 		}
2479 		sx_xunlock(&sc->sc_lock);
2480 		g_topology_lock();
2481 	}
2482 }
2483 
2484 static void
g_raid_shutdown_post_sync(void * arg,int howto)2485 g_raid_shutdown_post_sync(void *arg, int howto)
2486 {
2487 	struct g_class *mp;
2488 	struct g_geom *gp, *gp2;
2489 	struct g_raid_softc *sc;
2490 	struct g_raid_volume *vol;
2491 
2492 	if ((howto & RB_NOSYNC) != 0)
2493 		return;
2494 
2495 	mp = arg;
2496 	g_topology_lock();
2497 	g_raid_shutdown = 1;
2498 	LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
2499 		if ((sc = gp->softc) == NULL)
2500 			continue;
2501 		g_topology_unlock();
2502 		sx_xlock(&sc->sc_lock);
2503 		TAILQ_FOREACH(vol, &sc->sc_volumes, v_next)
2504 			g_raid_clean(vol, -1);
2505 		g_cancel_event(sc);
2506 		g_raid_destroy(sc, G_RAID_DESTROY_DELAYED);
2507 		g_topology_lock();
2508 	}
2509 	g_topology_unlock();
2510 }
2511 
2512 static void
g_raid_init(struct g_class * mp)2513 g_raid_init(struct g_class *mp)
2514 {
2515 
2516 	g_raid_post_sync = EVENTHANDLER_REGISTER(shutdown_post_sync,
2517 	    g_raid_shutdown_post_sync, mp, SHUTDOWN_PRI_FIRST);
2518 	if (g_raid_post_sync == NULL)
2519 		G_RAID_DEBUG(0, "Warning! Cannot register shutdown event.");
2520 	g_raid_started = 1;
2521 }
2522 
2523 static void
g_raid_fini(struct g_class * mp)2524 g_raid_fini(struct g_class *mp)
2525 {
2526 
2527 	if (g_raid_post_sync != NULL)
2528 		EVENTHANDLER_DEREGISTER(shutdown_post_sync, g_raid_post_sync);
2529 	g_raid_started = 0;
2530 }
2531 
2532 int
g_raid_md_modevent(module_t mod,int type,void * arg)2533 g_raid_md_modevent(module_t mod, int type, void *arg)
2534 {
2535 	struct g_raid_md_class *class, *c, *nc;
2536 	int error;
2537 
2538 	error = 0;
2539 	class = arg;
2540 	switch (type) {
2541 	case MOD_LOAD:
2542 		c = LIST_FIRST(&g_raid_md_classes);
2543 		if (c == NULL || c->mdc_priority > class->mdc_priority)
2544 			LIST_INSERT_HEAD(&g_raid_md_classes, class, mdc_list);
2545 		else {
2546 			while ((nc = LIST_NEXT(c, mdc_list)) != NULL &&
2547 			    nc->mdc_priority < class->mdc_priority)
2548 				c = nc;
2549 			LIST_INSERT_AFTER(c, class, mdc_list);
2550 		}
2551 		if (g_raid_started)
2552 			g_retaste(&g_raid_class);
2553 		break;
2554 	case MOD_UNLOAD:
2555 		LIST_REMOVE(class, mdc_list);
2556 		break;
2557 	default:
2558 		error = EOPNOTSUPP;
2559 		break;
2560 	}
2561 
2562 	return (error);
2563 }
2564 
2565 int
g_raid_tr_modevent(module_t mod,int type,void * arg)2566 g_raid_tr_modevent(module_t mod, int type, void *arg)
2567 {
2568 	struct g_raid_tr_class *class, *c, *nc;
2569 	int error;
2570 
2571 	error = 0;
2572 	class = arg;
2573 	switch (type) {
2574 	case MOD_LOAD:
2575 		c = LIST_FIRST(&g_raid_tr_classes);
2576 		if (c == NULL || c->trc_priority > class->trc_priority)
2577 			LIST_INSERT_HEAD(&g_raid_tr_classes, class, trc_list);
2578 		else {
2579 			while ((nc = LIST_NEXT(c, trc_list)) != NULL &&
2580 			    nc->trc_priority < class->trc_priority)
2581 				c = nc;
2582 			LIST_INSERT_AFTER(c, class, trc_list);
2583 		}
2584 		break;
2585 	case MOD_UNLOAD:
2586 		LIST_REMOVE(class, trc_list);
2587 		break;
2588 	default:
2589 		error = EOPNOTSUPP;
2590 		break;
2591 	}
2592 
2593 	return (error);
2594 }
2595 
2596 /*
2597  * Use local implementation of DECLARE_GEOM_CLASS(g_raid_class, g_raid)
2598  * to reduce module priority, allowing submodules to register them first.
2599  */
2600 static moduledata_t g_raid_mod = {
2601 	"g_raid",
2602 	g_modevent,
2603 	&g_raid_class
2604 };
2605 DECLARE_MODULE(g_raid, g_raid_mod, SI_SUB_DRIVERS, SI_ORDER_FOURTH);
2606 MODULE_VERSION(geom_raid, 0);
2607