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