2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/limits.h>
38 #include <sys/mutex.h>
41 #include <sys/sysctl.h>
42 #include <sys/malloc.h>
43 #include <sys/eventhandler.h>
45 #include <geom/geom.h>
46 #include <geom/geom_dbg.h>
48 #include <sys/kthread.h>
49 #include <sys/sched.h>
50 #include <geom/raid/g_raid.h>
51 #include "g_raid_md_if.h"
52 #include "g_raid_tr_if.h"
54 static MALLOC_DEFINE(M_RAID, "raid_data", "GEOM_RAID Data");
56 SYSCTL_DECL(_kern_geom);
57 SYSCTL_NODE(_kern_geom, OID_AUTO, raid, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
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,
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.");
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)); \
96 LIST_HEAD(, g_raid_md_class) g_raid_md_classes =
97 LIST_HEAD_INITIALIZER(g_raid_md_classes);
99 LIST_HEAD(, g_raid_tr_class) g_raid_tr_classes =
100 LIST_HEAD_INITIALIZER(g_raid_tr_classes);
102 LIST_HEAD(, g_raid_volume) g_raid_volumes =
103 LIST_HEAD_INITIALIZER(g_raid_volumes);
105 static eventhandler_tag g_raid_post_sync = NULL;
106 static int g_raid_started = 0;
107 static int g_raid_shutdown = 0;
109 static int g_raid_destroy_geom(struct gctl_req *req, struct g_class *mp,
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);
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,
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);
138 g_raid_node_event2str(int event)
142 case G_RAID_NODE_E_WAKE:
144 case G_RAID_NODE_E_START:
152 g_raid_disk_state2str(int state)
156 case G_RAID_DISK_S_NONE:
158 case G_RAID_DISK_S_OFFLINE:
160 case G_RAID_DISK_S_DISABLED:
162 case G_RAID_DISK_S_FAILED:
164 case G_RAID_DISK_S_STALE_FAILED:
165 return ("STALE_FAILED");
166 case G_RAID_DISK_S_SPARE:
168 case G_RAID_DISK_S_STALE:
170 case G_RAID_DISK_S_ACTIVE:
178 g_raid_disk_event2str(int event)
182 case G_RAID_DISK_E_DISCONNECTED:
183 return ("DISCONNECTED");
190 g_raid_subdisk_state2str(int state)
194 case G_RAID_SUBDISK_S_NONE:
196 case G_RAID_SUBDISK_S_FAILED:
198 case G_RAID_SUBDISK_S_NEW:
200 case G_RAID_SUBDISK_S_REBUILD:
202 case G_RAID_SUBDISK_S_UNINITIALIZED:
203 return ("UNINITIALIZED");
204 case G_RAID_SUBDISK_S_STALE:
206 case G_RAID_SUBDISK_S_RESYNC:
208 case G_RAID_SUBDISK_S_ACTIVE:
216 g_raid_subdisk_event2str(int event)
220 case G_RAID_SUBDISK_E_NEW:
222 case G_RAID_SUBDISK_E_FAILED:
224 case G_RAID_SUBDISK_E_DISCONNECTED:
225 return ("DISCONNECTED");
232 g_raid_volume_state2str(int state)
236 case G_RAID_VOLUME_S_STARTING:
238 case G_RAID_VOLUME_S_BROKEN:
240 case G_RAID_VOLUME_S_DEGRADED:
242 case G_RAID_VOLUME_S_SUBOPTIMAL:
243 return ("SUBOPTIMAL");
244 case G_RAID_VOLUME_S_OPTIMAL:
246 case G_RAID_VOLUME_S_UNSUPPORTED:
247 return ("UNSUPPORTED");
248 case G_RAID_VOLUME_S_STOPPED:
256 g_raid_volume_event2str(int event)
260 case G_RAID_VOLUME_E_UP:
262 case G_RAID_VOLUME_E_DOWN:
264 case G_RAID_VOLUME_E_START:
266 case G_RAID_VOLUME_E_STARTMD:
274 g_raid_volume_level2str(int level, int qual)
278 case G_RAID_VOLUME_RL_RAID0:
280 case G_RAID_VOLUME_RL_RAID1:
282 case G_RAID_VOLUME_RL_RAID3:
283 if (qual == G_RAID_VOLUME_RLQ_R3P0)
285 if (qual == G_RAID_VOLUME_RLQ_R3PN)
288 case G_RAID_VOLUME_RL_RAID4:
289 if (qual == G_RAID_VOLUME_RLQ_R4P0)
291 if (qual == G_RAID_VOLUME_RLQ_R4PN)
294 case G_RAID_VOLUME_RL_RAID5:
295 if (qual == G_RAID_VOLUME_RLQ_R5RA)
297 if (qual == G_RAID_VOLUME_RLQ_R5RS)
299 if (qual == G_RAID_VOLUME_RLQ_R5LA)
301 if (qual == G_RAID_VOLUME_RLQ_R5LS)
304 case G_RAID_VOLUME_RL_RAID6:
305 if (qual == G_RAID_VOLUME_RLQ_R6RA)
307 if (qual == G_RAID_VOLUME_RLQ_R6RS)
309 if (qual == G_RAID_VOLUME_RLQ_R6LA)
311 if (qual == G_RAID_VOLUME_RLQ_R6LS)
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");
324 case G_RAID_VOLUME_RL_RAID1E:
325 if (qual == G_RAID_VOLUME_RLQ_R1EA)
327 if (qual == G_RAID_VOLUME_RLQ_R1EO)
330 case G_RAID_VOLUME_RL_SINGLE:
332 case G_RAID_VOLUME_RL_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");
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");
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");
370 g_raid_volume_str2level(const char *str, int *level, int *qual)
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;
489 g_raid_get_diskname(struct g_raid_disk *disk)
492 if (disk->d_consumer == NULL || disk->d_consumer->provider == NULL)
493 return ("[unknown]");
494 return (disk->d_consumer->provider->name);
498 g_raid_get_disk_info(struct g_raid_disk *disk)
500 struct g_consumer *cp = disk->d_consumer;
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);
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);
515 /* Read BIO_DELETE support. */
516 error = g_getattr("GEOM::candelete", cp, &disk->d_candelete);
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);
526 g_raid_report_disk_state(struct g_raid_disk *disk)
528 struct g_raid_subdisk *sd;
532 if (disk->d_consumer == NULL)
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) {
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;
545 if (state == G_RAID_SUBDISK_S_FAILED)
547 else if (state == G_RAID_SUBDISK_S_NEW ||
548 state == G_RAID_SUBDISK_S_REBUILD)
550 else if (state == G_RAID_SUBDISK_S_STALE ||
551 state == G_RAID_SUBDISK_S_RESYNC)
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);
563 g_raid_change_disk_state(struct g_raid_disk *disk, int state)
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);
575 g_raid_change_subdisk_state(struct g_raid_subdisk *sd, int state)
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;
586 g_raid_report_disk_state(sd->sd_disk);
590 g_raid_change_volume_state(struct g_raid_volume *vol, int state)
593 G_RAID_DEBUG1(0, vol->v_softc,
594 "Volume %s state changed from %s to %s.",
596 g_raid_volume_state2str(vol->v_state),
597 g_raid_volume_state2str(state));
598 vol->v_state = state;
602 * --- Events handling functions ---
603 * Events in geom_raid are used to maintain subdisks and volumes status
604 * from one thread to simplify locking.
607 g_raid_event_free(struct g_raid_event *ep)
614 g_raid_event_send(void *arg, int event, int flags)
616 struct g_raid_softc *sc;
617 struct g_raid_event *ep;
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;
629 ep = malloc(sizeof(*ep), M_RAID,
630 sx_xlocked(&sc->sc_lock) ? M_WAITOK : M_NOWAIT);
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);
643 if ((flags & G_RAID_EVENT_WAIT) == 0)
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",
655 g_raid_event_free(ep);
656 sx_xlock(&sc->sc_lock);
661 g_raid_event_cancel(struct g_raid_softc *sc, void *tgt)
663 struct g_raid_event *ep, *tmpep;
665 sx_assert(&sc->sc_lock, SX_XLOCKED);
667 mtx_lock(&sc->sc_queue_mtx);
668 TAILQ_FOREACH_SAFE(ep, &sc->sc_events, e_next, tmpep) {
669 if (ep->e_tgt != tgt)
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);
675 ep->e_error = ECANCELED;
679 mtx_unlock(&sc->sc_queue_mtx);
683 g_raid_event_check(struct g_raid_softc *sc, void *tgt)
685 struct g_raid_event *ep;
688 sx_assert(&sc->sc_lock, SX_XLOCKED);
690 mtx_lock(&sc->sc_queue_mtx);
691 TAILQ_FOREACH(ep, &sc->sc_events, e_next) {
692 if (ep->e_tgt != tgt)
697 mtx_unlock(&sc->sc_queue_mtx);
702 * Return the number of disks in given state.
703 * If state is equal to -1, count all connected disks.
706 g_raid_ndisks(struct g_raid_softc *sc, int state)
708 struct g_raid_disk *disk;
711 sx_assert(&sc->sc_lock, SX_LOCKED);
714 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
715 if (disk->d_state == state || state == -1)
722 * Return the number of subdisks in given state.
723 * If state is equal to -1, count all connected disks.
726 g_raid_nsubdisks(struct g_raid_volume *vol, int state)
728 struct g_raid_subdisk *subdisk;
729 struct g_raid_softc *sc __diagused;
733 sx_assert(&sc->sc_lock, SX_LOCKED);
736 for (i = 0; i < vol->v_disks_count; i++) {
737 subdisk = &vol->v_subdisks[i];
739 subdisk->sd_state != G_RAID_SUBDISK_S_NONE) ||
740 subdisk->sd_state == state)
747 * Return the first subdisk in given state.
748 * If state is equal to -1, then the first connected disks.
750 struct g_raid_subdisk *
751 g_raid_get_subdisk(struct g_raid_volume *vol, int state)
753 struct g_raid_subdisk *sd;
754 struct g_raid_softc *sc __diagused;
758 sx_assert(&sc->sc_lock, SX_LOCKED);
760 for (i = 0; i < vol->v_disks_count; i++) {
761 sd = &vol->v_subdisks[i];
763 sd->sd_state != G_RAID_SUBDISK_S_NONE) ||
764 sd->sd_state == state)
771 g_raid_open_consumer(struct g_raid_softc *sc, const char *name)
773 struct g_consumer *cp;
774 struct g_provider *pp;
778 if (strncmp(name, _PATH_DEV, 5) == 0)
780 pp = g_provider_by_name(name);
783 cp = g_new_consumer(sc->sc_geom);
784 cp->flags |= G_CF_DIRECT_RECEIVE;
785 if (g_attach(cp, pp) != 0) {
786 g_destroy_consumer(cp);
789 if (g_access(cp, 1, 1, 1) != 0) {
791 g_destroy_consumer(cp);
798 g_raid_nrequests(struct g_raid_softc *sc, struct g_consumer *cp)
803 mtx_lock(&sc->sc_queue_mtx);
804 TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
805 if (bp->bio_from == cp)
808 mtx_unlock(&sc->sc_queue_mtx);
813 g_raid_nopens(struct g_raid_softc *sc)
815 struct g_raid_volume *vol;
819 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
820 if (vol->v_provider_open != 0)
827 g_raid_consumer_is_busy(struct g_raid_softc *sc, struct g_consumer *cp)
832 "I/O requests for %s exist, can't destroy it now.",
836 if (g_raid_nrequests(sc, cp) > 0) {
838 "I/O requests for %s in queue, can't destroy it now.",
846 g_raid_destroy_consumer(void *arg, int flags __unused)
848 struct g_consumer *cp;
853 G_RAID_DEBUG(1, "Consumer %s destroyed.", cp->provider->name);
855 g_destroy_consumer(cp);
859 g_raid_kill_consumer(struct g_raid_softc *sc, struct g_consumer *cp)
861 struct g_provider *pp;
864 g_topology_assert_not();
868 if (g_raid_consumer_is_busy(sc, cp))
873 if ((pp->geom->flags & G_GEOM_WITHER) == 0)
876 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
877 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
880 * After retaste event was send (inside g_access()), we can send
881 * event to detach and destroy consumer.
882 * A class, which has consumer to the given provider connected
883 * will not receive retaste event for the provider.
884 * This is the way how I ignore retaste events when I close
885 * consumers opened for write: I detach and destroy consumer
886 * after retaste event is sent.
888 g_post_event(g_raid_destroy_consumer, cp, M_WAITOK, NULL);
891 G_RAID_DEBUG(1, "Consumer %s destroyed.", pp->name);
893 g_destroy_consumer(cp);
899 g_raid_orphan(struct g_consumer *cp)
901 struct g_raid_disk *disk;
908 g_raid_event_send(disk, G_RAID_DISK_E_DISCONNECTED,
913 g_raid_clean(struct g_raid_volume *vol, int acw)
915 struct g_raid_softc *sc;
919 g_topology_assert_not();
920 sx_assert(&sc->sc_lock, SX_XLOCKED);
922 // if ((sc->sc_flags & G_RAID_DEVICE_FLAG_NOFAILSYNC) != 0)
926 if (vol->v_writes > 0)
928 if (acw > 0 || (acw == -1 &&
929 vol->v_provider != NULL && vol->v_provider->acw > 0)) {
930 timeout = g_raid_clean_time - (time_uptime - vol->v_last_write);
931 if (!g_raid_shutdown && timeout > 0)
935 G_RAID_DEBUG1(1, sc, "Volume %s marked as clean.",
937 g_raid_write_metadata(sc, vol, NULL, NULL);
941 g_raid_dirty(struct g_raid_volume *vol)
943 struct g_raid_softc *sc;
946 g_topology_assert_not();
947 sx_assert(&sc->sc_lock, SX_XLOCKED);
949 // if ((sc->sc_flags & G_RAID_DEVICE_FLAG_NOFAILSYNC) != 0)
952 G_RAID_DEBUG1(1, sc, "Volume %s marked as dirty.",
954 g_raid_write_metadata(sc, vol, NULL, NULL);
958 g_raid_tr_flush_common(struct g_raid_tr_object *tr, struct bio *bp)
960 struct g_raid_volume *vol;
961 struct g_raid_subdisk *sd;
962 struct bio_queue_head queue;
966 vol = tr->tro_volume;
969 * Allocate all bios before sending any request, so we can return
970 * ENOMEM in nice and clean way.
973 for (i = 0; i < vol->v_disks_count; i++) {
974 sd = &vol->v_subdisks[i];
975 if (sd->sd_state == G_RAID_SUBDISK_S_NONE ||
976 sd->sd_state == G_RAID_SUBDISK_S_FAILED)
978 cbp = g_clone_bio(bp);
981 cbp->bio_caller1 = sd;
982 bioq_insert_tail(&queue, cbp);
984 while ((cbp = bioq_takefirst(&queue)) != NULL) {
985 sd = cbp->bio_caller1;
986 cbp->bio_caller1 = NULL;
987 g_raid_subdisk_iostart(sd, cbp);
991 while ((cbp = bioq_takefirst(&queue)) != NULL)
993 if (bp->bio_error == 0)
994 bp->bio_error = ENOMEM;
995 g_raid_iodone(bp, bp->bio_error);
999 g_raid_tr_kerneldump_common_done(struct bio *bp)
1002 bp->bio_flags |= BIO_DONE;
1006 g_raid_tr_kerneldump_common(struct g_raid_tr_object *tr,
1007 void *virtual, vm_offset_t physical, off_t offset, size_t length)
1009 struct g_raid_softc *sc;
1010 struct g_raid_volume *vol;
1013 vol = tr->tro_volume;
1017 bp.bio_cmd = BIO_WRITE;
1018 bp.bio_done = g_raid_tr_kerneldump_common_done;
1019 bp.bio_attribute = NULL;
1020 bp.bio_offset = offset;
1021 bp.bio_length = length;
1022 bp.bio_data = virtual;
1023 bp.bio_to = vol->v_provider;
1026 while (!(bp.bio_flags & BIO_DONE)) {
1027 G_RAID_DEBUG1(4, sc, "Poll...");
1032 return (bp.bio_error != 0 ? EIO : 0);
1036 g_raid_dump(void *arg, void *virtual, off_t offset, size_t length)
1038 struct g_raid_volume *vol;
1041 vol = (struct g_raid_volume *)arg;
1042 G_RAID_DEBUG1(3, vol->v_softc, "Dumping at off %llu len %llu.",
1043 (long long unsigned)offset, (long long unsigned)length);
1045 error = G_RAID_TR_KERNELDUMP(vol->v_tr, virtual, offset, length);
1050 g_raid_kerneldump(struct g_raid_softc *sc, struct bio *bp)
1052 struct g_kerneldump *gkd;
1053 struct g_provider *pp;
1054 struct g_raid_volume *vol;
1056 gkd = (struct g_kerneldump*)bp->bio_data;
1059 g_trace(G_T_TOPOLOGY, "g_raid_kerneldump(%s, %jd, %jd)",
1060 pp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
1061 gkd->di.dumper = g_raid_dump;
1063 gkd->di.blocksize = vol->v_sectorsize;
1064 gkd->di.maxiosize = DFLTPHYS;
1065 gkd->di.mediaoffset = gkd->offset;
1066 if ((gkd->offset + gkd->length) > vol->v_mediasize)
1067 gkd->length = vol->v_mediasize - gkd->offset;
1068 gkd->di.mediasize = gkd->length;
1069 g_io_deliver(bp, 0);
1073 g_raid_candelete(struct g_raid_softc *sc, struct bio *bp)
1075 struct g_provider *pp;
1076 struct g_raid_volume *vol;
1077 struct g_raid_subdisk *sd;
1082 for (i = 0; i < vol->v_disks_count; i++) {
1083 sd = &vol->v_subdisks[i];
1084 if (sd->sd_state == G_RAID_SUBDISK_S_NONE)
1086 if (sd->sd_disk->d_candelete)
1089 val = i < vol->v_disks_count;
1090 g_handleattr(bp, "GEOM::candelete", &val, sizeof(val));
1094 g_raid_start(struct bio *bp)
1096 struct g_raid_softc *sc;
1098 sc = bp->bio_to->geom->softc;
1100 * If sc == NULL or there are no valid disks, provider's error
1101 * should be set and g_raid_start() should not be called at all.
1103 // KASSERT(sc != NULL && sc->sc_state == G_RAID_VOLUME_S_RUNNING,
1104 // ("Provider's error should be set (error=%d)(mirror=%s).",
1105 // bp->bio_to->error, bp->bio_to->name));
1106 G_RAID_LOGREQ(3, bp, "Request received.");
1108 switch (bp->bio_cmd) {
1116 if (!strcmp(bp->bio_attribute, "GEOM::candelete"))
1117 g_raid_candelete(sc, bp);
1118 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
1119 g_raid_kerneldump(sc, bp);
1121 g_io_deliver(bp, EOPNOTSUPP);
1124 g_io_deliver(bp, EOPNOTSUPP);
1127 mtx_lock(&sc->sc_queue_mtx);
1128 bioq_insert_tail(&sc->sc_queue, bp);
1129 mtx_unlock(&sc->sc_queue_mtx);
1131 G_RAID_DEBUG1(4, sc, "Waking up %p.", sc);
1137 g_raid_bio_overlaps(const struct bio *bp, off_t lstart, off_t len)
1141 * (1) bp entirely below NO
1142 * (2) bp entirely above NO
1143 * (3) bp start below, but end in range YES
1144 * (4) bp entirely within YES
1145 * (5) bp starts within, ends above YES
1147 * lock range 10-19 (offset 10 length 10)
1148 * (1) 1-5: first if kicks it out
1149 * (2) 30-35: second if kicks it out
1150 * (3) 5-15: passes both ifs
1151 * (4) 12-14: passes both ifs
1152 * (5) 19-20: passes both
1154 off_t lend = lstart + len - 1;
1155 off_t bstart = bp->bio_offset;
1156 off_t bend = bp->bio_offset + bp->bio_length - 1;
1166 g_raid_is_in_locked_range(struct g_raid_volume *vol, const struct bio *bp)
1168 struct g_raid_lock *lp;
1170 sx_assert(&vol->v_softc->sc_lock, SX_LOCKED);
1172 LIST_FOREACH(lp, &vol->v_locks, l_next) {
1173 if (g_raid_bio_overlaps(bp, lp->l_offset, lp->l_length))
1180 g_raid_start_request(struct bio *bp)
1182 struct g_raid_softc *sc __diagused;
1183 struct g_raid_volume *vol;
1185 sc = bp->bio_to->geom->softc;
1186 sx_assert(&sc->sc_lock, SX_LOCKED);
1187 vol = bp->bio_to->private;
1190 * Check to see if this item is in a locked range. If so,
1191 * queue it to our locked queue and return. We'll requeue
1192 * it when the range is unlocked. Internal I/O for the
1193 * rebuild/rescan/recovery process is excluded from this
1194 * check so we can actually do the recovery.
1196 if (!(bp->bio_cflags & G_RAID_BIO_FLAG_SPECIAL) &&
1197 g_raid_is_in_locked_range(vol, bp)) {
1198 G_RAID_LOGREQ(3, bp, "Defer request.");
1199 bioq_insert_tail(&vol->v_locked, bp);
1204 * If we're actually going to do the write/delete, then
1205 * update the idle stats for the volume.
1207 if (bp->bio_cmd == BIO_WRITE || bp->bio_cmd == BIO_DELETE) {
1214 * Put request onto inflight queue, so we can check if new
1215 * synchronization requests don't collide with it. Then tell
1216 * the transformation layer to start the I/O.
1218 bioq_insert_tail(&vol->v_inflight, bp);
1219 G_RAID_LOGREQ(4, bp, "Request started");
1220 G_RAID_TR_IOSTART(vol->v_tr, bp);
1224 g_raid_finish_with_locked_ranges(struct g_raid_volume *vol, struct bio *bp)
1228 struct g_raid_lock *lp;
1230 vol->v_pending_lock = 0;
1231 LIST_FOREACH(lp, &vol->v_locks, l_next) {
1232 if (lp->l_pending) {
1236 TAILQ_FOREACH(nbp, &vol->v_inflight.queue, bio_queue) {
1237 if (g_raid_bio_overlaps(nbp, off, len))
1240 if (lp->l_pending) {
1241 vol->v_pending_lock = 1;
1242 G_RAID_DEBUG1(4, vol->v_softc,
1243 "Deferred lock(%jd, %jd) has %d pending",
1244 (intmax_t)off, (intmax_t)(off + len),
1248 G_RAID_DEBUG1(4, vol->v_softc,
1249 "Deferred lock of %jd to %jd completed",
1250 (intmax_t)off, (intmax_t)(off + len));
1251 G_RAID_TR_LOCKED(vol->v_tr, lp->l_callback_arg);
1257 g_raid_iodone(struct bio *bp, int error)
1259 struct g_raid_softc *sc __diagused;
1260 struct g_raid_volume *vol;
1262 sc = bp->bio_to->geom->softc;
1263 sx_assert(&sc->sc_lock, SX_LOCKED);
1264 vol = bp->bio_to->private;
1265 G_RAID_LOGREQ(3, bp, "Request done: %d.", error);
1267 /* Update stats if we done write/delete. */
1268 if (bp->bio_cmd == BIO_WRITE || bp->bio_cmd == BIO_DELETE) {
1270 vol->v_last_write = time_uptime;
1273 bioq_remove(&vol->v_inflight, bp);
1274 if (vol->v_pending_lock && g_raid_is_in_locked_range(vol, bp))
1275 g_raid_finish_with_locked_ranges(vol, bp);
1276 getmicrouptime(&vol->v_last_done);
1277 g_io_deliver(bp, error);
1281 g_raid_lock_range(struct g_raid_volume *vol, off_t off, off_t len,
1282 struct bio *ignore, void *argp)
1284 struct g_raid_softc *sc;
1285 struct g_raid_lock *lp;
1289 lp = malloc(sizeof(*lp), M_RAID, M_WAITOK | M_ZERO);
1290 LIST_INSERT_HEAD(&vol->v_locks, lp, l_next);
1293 lp->l_callback_arg = argp;
1296 TAILQ_FOREACH(bp, &vol->v_inflight.queue, bio_queue) {
1297 if (bp != ignore && g_raid_bio_overlaps(bp, off, len))
1302 * If there are any writes that are pending, we return EBUSY. All
1303 * callers will have to wait until all pending writes clear.
1305 if (lp->l_pending > 0) {
1306 vol->v_pending_lock = 1;
1307 G_RAID_DEBUG1(4, sc, "Locking range %jd to %jd deferred %d pend",
1308 (intmax_t)off, (intmax_t)(off+len), lp->l_pending);
1311 G_RAID_DEBUG1(4, sc, "Locking range %jd to %jd",
1312 (intmax_t)off, (intmax_t)(off+len));
1313 G_RAID_TR_LOCKED(vol->v_tr, lp->l_callback_arg);
1318 g_raid_unlock_range(struct g_raid_volume *vol, off_t off, off_t len)
1320 struct g_raid_lock *lp;
1321 struct g_raid_softc *sc;
1325 LIST_FOREACH(lp, &vol->v_locks, l_next) {
1326 if (lp->l_offset == off && lp->l_length == len) {
1327 LIST_REMOVE(lp, l_next);
1329 * Right now we just put them all back on the queue
1330 * and hope for the best. We hope this because any
1331 * locked ranges will go right back on this list
1332 * when the worker thread runs.
1335 G_RAID_DEBUG1(4, sc, "Unlocked %jd to %jd",
1336 (intmax_t)lp->l_offset,
1337 (intmax_t)(lp->l_offset+lp->l_length));
1338 mtx_lock(&sc->sc_queue_mtx);
1339 while ((bp = bioq_takefirst(&vol->v_locked)) != NULL)
1340 bioq_insert_tail(&sc->sc_queue, bp);
1341 mtx_unlock(&sc->sc_queue_mtx);
1350 g_raid_subdisk_iostart(struct g_raid_subdisk *sd, struct bio *bp)
1352 struct g_consumer *cp;
1353 struct g_raid_disk *disk, *tdisk;
1355 bp->bio_caller1 = sd;
1358 * Make sure that the disk is present. Generally it is a task of
1359 * transformation layers to not send requests to absent disks, but
1360 * it is better to be safe and report situation then sorry.
1362 if (sd->sd_disk == NULL) {
1363 G_RAID_LOGREQ(0, bp, "Warning! I/O request to an absent disk!");
1365 bp->bio_from = NULL;
1367 bp->bio_error = ENXIO;
1368 g_raid_disk_done(bp);
1372 if (disk->d_state != G_RAID_DISK_S_ACTIVE &&
1373 disk->d_state != G_RAID_DISK_S_FAILED) {
1374 G_RAID_LOGREQ(0, bp, "Warning! I/O request to a disk in a "
1375 "wrong state (%s)!", g_raid_disk_state2str(disk->d_state));
1379 cp = disk->d_consumer;
1381 bp->bio_to = cp->provider;
1384 /* Update average disks load. */
1385 TAILQ_FOREACH(tdisk, &sd->sd_softc->sc_disks, d_next) {
1386 if (tdisk->d_consumer == NULL)
1389 tdisk->d_load = (tdisk->d_consumer->index *
1390 G_RAID_SUBDISK_LOAD_SCALE + tdisk->d_load * 7) / 8;
1393 disk->d_last_offset = bp->bio_offset + bp->bio_length;
1395 G_RAID_LOGREQ(3, bp, "Sending dumping request.");
1396 if (bp->bio_cmd == BIO_WRITE) {
1397 bp->bio_error = g_raid_subdisk_kerneldump(sd,
1398 bp->bio_data, bp->bio_offset, bp->bio_length);
1400 bp->bio_error = EOPNOTSUPP;
1401 g_raid_disk_done(bp);
1403 bp->bio_done = g_raid_disk_done;
1404 bp->bio_offset += sd->sd_offset;
1405 G_RAID_LOGREQ(3, bp, "Sending request.");
1406 g_io_request(bp, cp);
1411 g_raid_subdisk_kerneldump(struct g_raid_subdisk *sd, void *virtual,
1412 off_t offset, size_t length)
1415 if (sd->sd_disk == NULL)
1417 if (sd->sd_disk->d_kd.di.dumper == NULL)
1418 return (EOPNOTSUPP);
1419 return (dump_write(&sd->sd_disk->d_kd.di, virtual,
1420 sd->sd_disk->d_kd.di.mediaoffset + sd->sd_offset + offset, length));
1424 g_raid_disk_done(struct bio *bp)
1426 struct g_raid_softc *sc;
1427 struct g_raid_subdisk *sd;
1429 sd = bp->bio_caller1;
1431 mtx_lock(&sc->sc_queue_mtx);
1432 bioq_insert_tail(&sc->sc_queue, bp);
1433 mtx_unlock(&sc->sc_queue_mtx);
1439 g_raid_disk_done_request(struct bio *bp)
1441 struct g_raid_softc *sc;
1442 struct g_raid_disk *disk;
1443 struct g_raid_subdisk *sd;
1444 struct g_raid_volume *vol;
1446 g_topology_assert_not();
1448 G_RAID_LOGREQ(3, bp, "Disk request done: %d.", bp->bio_error);
1449 sd = bp->bio_caller1;
1451 vol = sd->sd_volume;
1452 if (bp->bio_from != NULL) {
1453 bp->bio_from->index--;
1454 disk = bp->bio_from->private;
1456 g_raid_kill_consumer(sc, bp->bio_from);
1458 bp->bio_offset -= sd->sd_offset;
1460 G_RAID_TR_IODONE(vol->v_tr, sd, bp);
1464 g_raid_handle_event(struct g_raid_softc *sc, struct g_raid_event *ep)
1467 if ((ep->e_flags & G_RAID_EVENT_VOLUME) != 0)
1468 ep->e_error = g_raid_update_volume(ep->e_tgt, ep->e_event);
1469 else if ((ep->e_flags & G_RAID_EVENT_DISK) != 0)
1470 ep->e_error = g_raid_update_disk(ep->e_tgt, ep->e_event);
1471 else if ((ep->e_flags & G_RAID_EVENT_SUBDISK) != 0)
1472 ep->e_error = g_raid_update_subdisk(ep->e_tgt, ep->e_event);
1474 ep->e_error = g_raid_update_node(ep->e_tgt, ep->e_event);
1475 if ((ep->e_flags & G_RAID_EVENT_WAIT) == 0) {
1476 KASSERT(ep->e_error == 0,
1477 ("Error cannot be handled."));
1478 g_raid_event_free(ep);
1480 ep->e_flags |= G_RAID_EVENT_DONE;
1481 G_RAID_DEBUG1(4, sc, "Waking up %p.", ep);
1482 mtx_lock(&sc->sc_queue_mtx);
1484 mtx_unlock(&sc->sc_queue_mtx);
1492 g_raid_worker(void *arg)
1494 struct g_raid_softc *sc;
1495 struct g_raid_event *ep;
1496 struct g_raid_volume *vol;
1498 struct timeval now, t;
1502 thread_lock(curthread);
1503 sched_prio(curthread, PRIBIO);
1504 thread_unlock(curthread);
1506 sx_xlock(&sc->sc_lock);
1508 mtx_lock(&sc->sc_queue_mtx);
1510 * First take a look at events.
1511 * This is important to handle events before any I/O requests.
1516 ep = TAILQ_FIRST(&sc->sc_events);
1518 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
1519 else if ((bp = bioq_takefirst(&sc->sc_queue)) != NULL)
1522 getmicrouptime(&now);
1524 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1525 if (bioq_first(&vol->v_inflight) == NULL &&
1527 timevalcmp(&vol->v_last_done, &t, < ))
1528 t = vol->v_last_done;
1530 timevalsub(&t, &now);
1531 timeout = g_raid_idle_threshold +
1532 t.tv_sec * 1000000 + t.tv_usec;
1535 * Two steps to avoid overflows at HZ=1000
1536 * and idle timeouts > 2.1s. Some rounding
1537 * errors can occur, but they are < 1tick,
1538 * which is deemed to be close enough for
1541 int micpertic = 1000000 / hz;
1542 timeout = (timeout + micpertic - 1) / micpertic;
1543 sx_xunlock(&sc->sc_lock);
1544 MSLEEP(rv, sc, &sc->sc_queue_mtx,
1545 PRIBIO | PDROP, "-", timeout);
1546 sx_xlock(&sc->sc_lock);
1551 mtx_unlock(&sc->sc_queue_mtx);
1554 g_raid_handle_event(sc, ep);
1555 } else if (bp != NULL) {
1556 if (bp->bio_to != NULL &&
1557 bp->bio_to->geom == sc->sc_geom)
1558 g_raid_start_request(bp);
1560 g_raid_disk_done_request(bp);
1561 } else if (rv == EWOULDBLOCK) {
1562 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1563 g_raid_clean(vol, -1);
1564 if (bioq_first(&vol->v_inflight) == NULL &&
1566 t.tv_sec = g_raid_idle_threshold / 1000000;
1567 t.tv_usec = g_raid_idle_threshold % 1000000;
1568 timevaladd(&t, &vol->v_last_done);
1569 getmicrouptime(&now);
1570 if (timevalcmp(&t, &now, <= )) {
1571 G_RAID_TR_IDLE(vol->v_tr);
1572 vol->v_last_done = now;
1577 if (sc->sc_stopping == G_RAID_DESTROY_HARD)
1578 g_raid_destroy_node(sc, 1); /* May not return. */
1583 g_raid_poll(struct g_raid_softc *sc)
1585 struct g_raid_event *ep;
1588 sx_xlock(&sc->sc_lock);
1589 mtx_lock(&sc->sc_queue_mtx);
1591 * First take a look at events.
1592 * This is important to handle events before any I/O requests.
1594 ep = TAILQ_FIRST(&sc->sc_events);
1596 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
1597 mtx_unlock(&sc->sc_queue_mtx);
1598 g_raid_handle_event(sc, ep);
1601 bp = bioq_takefirst(&sc->sc_queue);
1603 mtx_unlock(&sc->sc_queue_mtx);
1604 if (bp->bio_from == NULL ||
1605 bp->bio_from->geom != sc->sc_geom)
1606 g_raid_start_request(bp);
1608 g_raid_disk_done_request(bp);
1611 sx_xunlock(&sc->sc_lock);
1615 g_raid_launch_provider(struct g_raid_volume *vol)
1617 struct g_raid_disk *disk;
1618 struct g_raid_subdisk *sd;
1619 struct g_raid_softc *sc;
1620 struct g_provider *pp;
1621 char name[G_RAID_MAX_VOLUMENAME];
1626 sx_assert(&sc->sc_lock, SX_LOCKED);
1629 /* Try to name provider with volume name. */
1630 snprintf(name, sizeof(name), "raid/%s", vol->v_name);
1631 if (g_raid_name_format == 0 || vol->v_name[0] == 0 ||
1632 g_provider_by_name(name) != NULL) {
1633 /* Otherwise use sequential volume number. */
1634 snprintf(name, sizeof(name), "raid/r%d", vol->v_global_id);
1637 pp = g_new_providerf(sc->sc_geom, "%s", name);
1638 pp->flags |= G_PF_DIRECT_RECEIVE;
1639 if (vol->v_tr->tro_class->trc_accept_unmapped) {
1640 pp->flags |= G_PF_ACCEPT_UNMAPPED;
1641 for (i = 0; i < vol->v_disks_count; i++) {
1642 sd = &vol->v_subdisks[i];
1643 if (sd->sd_state == G_RAID_SUBDISK_S_NONE)
1645 if ((sd->sd_disk->d_consumer->provider->flags &
1646 G_PF_ACCEPT_UNMAPPED) == 0)
1647 pp->flags &= ~G_PF_ACCEPT_UNMAPPED;
1651 pp->mediasize = vol->v_mediasize;
1652 pp->sectorsize = vol->v_sectorsize;
1654 pp->stripeoffset = 0;
1655 if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1 ||
1656 vol->v_raid_level == G_RAID_VOLUME_RL_RAID3 ||
1657 vol->v_raid_level == G_RAID_VOLUME_RL_SINGLE ||
1658 vol->v_raid_level == G_RAID_VOLUME_RL_CONCAT) {
1659 if ((disk = vol->v_subdisks[0].sd_disk) != NULL &&
1660 disk->d_consumer != NULL &&
1661 disk->d_consumer->provider != NULL) {
1662 pp->stripesize = disk->d_consumer->provider->stripesize;
1663 off = disk->d_consumer->provider->stripeoffset;
1664 pp->stripeoffset = off + vol->v_subdisks[0].sd_offset;
1666 pp->stripeoffset %= off;
1668 if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID3) {
1669 pp->stripesize *= (vol->v_disks_count - 1);
1670 pp->stripeoffset *= (vol->v_disks_count - 1);
1673 pp->stripesize = vol->v_strip_size;
1674 vol->v_provider = pp;
1675 g_error_provider(pp, 0);
1676 g_topology_unlock();
1677 G_RAID_DEBUG1(0, sc, "Provider %s for volume %s created.",
1678 pp->name, vol->v_name);
1682 g_raid_destroy_provider(struct g_raid_volume *vol)
1684 struct g_raid_softc *sc;
1685 struct g_provider *pp;
1686 struct bio *bp, *tmp;
1688 g_topology_assert_not();
1690 pp = vol->v_provider;
1691 KASSERT(pp != NULL, ("NULL provider (volume=%s).", vol->v_name));
1694 g_error_provider(pp, ENXIO);
1695 mtx_lock(&sc->sc_queue_mtx);
1696 TAILQ_FOREACH_SAFE(bp, &sc->sc_queue.queue, bio_queue, tmp) {
1697 if (bp->bio_to != pp)
1699 bioq_remove(&sc->sc_queue, bp);
1700 g_io_deliver(bp, ENXIO);
1702 mtx_unlock(&sc->sc_queue_mtx);
1703 G_RAID_DEBUG1(0, sc, "Provider %s for volume %s destroyed.",
1704 pp->name, vol->v_name);
1705 g_wither_provider(pp, ENXIO);
1706 g_topology_unlock();
1707 vol->v_provider = NULL;
1711 * Update device state.
1714 g_raid_update_volume(struct g_raid_volume *vol, u_int event)
1716 struct g_raid_softc *sc;
1719 sx_assert(&sc->sc_lock, SX_XLOCKED);
1721 G_RAID_DEBUG1(2, sc, "Event %s for volume %s.",
1722 g_raid_volume_event2str(event),
1725 case G_RAID_VOLUME_E_DOWN:
1726 if (vol->v_provider != NULL)
1727 g_raid_destroy_provider(vol);
1729 case G_RAID_VOLUME_E_UP:
1730 if (vol->v_provider == NULL)
1731 g_raid_launch_provider(vol);
1733 case G_RAID_VOLUME_E_START:
1735 G_RAID_TR_START(vol->v_tr);
1739 G_RAID_MD_VOLUME_EVENT(sc->sc_md, vol, event);
1743 /* Manage root mount release. */
1744 if (vol->v_starting) {
1745 vol->v_starting = 0;
1746 G_RAID_DEBUG1(1, sc, "root_mount_rel %p", vol->v_rootmount);
1747 root_mount_rel(vol->v_rootmount);
1748 vol->v_rootmount = NULL;
1750 if (vol->v_stopping && vol->v_provider_open == 0)
1751 g_raid_destroy_volume(vol);
1756 * Update subdisk state.
1759 g_raid_update_subdisk(struct g_raid_subdisk *sd, u_int event)
1761 struct g_raid_softc *sc;
1762 struct g_raid_volume *vol;
1765 vol = sd->sd_volume;
1766 sx_assert(&sc->sc_lock, SX_XLOCKED);
1768 G_RAID_DEBUG1(2, sc, "Event %s for subdisk %s:%d-%s.",
1769 g_raid_subdisk_event2str(event),
1770 vol->v_name, sd->sd_pos,
1771 sd->sd_disk ? g_raid_get_diskname(sd->sd_disk) : "[none]");
1773 G_RAID_TR_EVENT(vol->v_tr, sd, event);
1779 * Update disk state.
1782 g_raid_update_disk(struct g_raid_disk *disk, u_int event)
1784 struct g_raid_softc *sc;
1787 sx_assert(&sc->sc_lock, SX_XLOCKED);
1789 G_RAID_DEBUG1(2, sc, "Event %s for disk %s.",
1790 g_raid_disk_event2str(event),
1791 g_raid_get_diskname(disk));
1794 G_RAID_MD_EVENT(sc->sc_md, disk, event);
1802 g_raid_update_node(struct g_raid_softc *sc, u_int event)
1804 sx_assert(&sc->sc_lock, SX_XLOCKED);
1806 G_RAID_DEBUG1(2, sc, "Event %s for the array.",
1807 g_raid_node_event2str(event));
1809 if (event == G_RAID_NODE_E_WAKE)
1812 G_RAID_MD_EVENT(sc->sc_md, NULL, event);
1817 g_raid_access(struct g_provider *pp, int acr, int acw, int ace)
1819 struct g_raid_volume *vol;
1820 struct g_raid_softc *sc;
1821 int dcw, opens, error = 0;
1823 g_topology_assert();
1824 sc = pp->geom->softc;
1826 KASSERT(sc != NULL, ("NULL softc (provider=%s).", pp->name));
1827 KASSERT(vol != NULL, ("NULL volume (provider=%s).", pp->name));
1829 G_RAID_DEBUG1(2, sc, "Access request for %s: r%dw%de%d.", pp->name,
1831 dcw = pp->acw + acw;
1833 g_topology_unlock();
1834 sx_xlock(&sc->sc_lock);
1835 /* Deny new opens while dying. */
1836 if (sc->sc_stopping != 0 && (acr > 0 || acw > 0 || ace > 0)) {
1840 /* Deny write opens for read-only volumes. */
1841 if (vol->v_read_only && acw > 0) {
1846 g_raid_clean(vol, dcw);
1847 vol->v_provider_open += acr + acw + ace;
1848 /* Handle delayed node destruction. */
1849 if (sc->sc_stopping == G_RAID_DESTROY_DELAYED &&
1850 vol->v_provider_open == 0) {
1851 /* Count open volumes. */
1852 opens = g_raid_nopens(sc);
1854 sc->sc_stopping = G_RAID_DESTROY_HARD;
1855 /* Wake up worker to make it selfdestruct. */
1856 g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
1859 /* Handle open volume destruction. */
1860 if (vol->v_stopping && vol->v_provider_open == 0)
1861 g_raid_destroy_volume(vol);
1863 sx_xunlock(&sc->sc_lock);
1868 struct g_raid_softc *
1869 g_raid_create_node(struct g_class *mp,
1870 const char *name, struct g_raid_md_object *md)
1872 struct g_raid_softc *sc;
1876 g_topology_assert();
1877 G_RAID_DEBUG(1, "Creating array %s.", name);
1879 gp = g_new_geomf(mp, "%s", name);
1880 sc = malloc(sizeof(*sc), M_RAID, M_WAITOK | M_ZERO);
1881 gp->start = g_raid_start;
1882 gp->orphan = g_raid_orphan;
1883 gp->access = g_raid_access;
1884 gp->dumpconf = g_raid_dumpconf;
1889 TAILQ_INIT(&sc->sc_volumes);
1890 TAILQ_INIT(&sc->sc_disks);
1891 sx_init(&sc->sc_lock, "graid:lock");
1892 mtx_init(&sc->sc_queue_mtx, "graid:queue", NULL, MTX_DEF);
1893 TAILQ_INIT(&sc->sc_events);
1894 bioq_init(&sc->sc_queue);
1896 error = kproc_create(g_raid_worker, sc, &sc->sc_worker, 0, 0,
1899 G_RAID_DEBUG(0, "Cannot create kernel thread for %s.", name);
1900 mtx_destroy(&sc->sc_queue_mtx);
1901 sx_destroy(&sc->sc_lock);
1902 g_destroy_geom(sc->sc_geom);
1907 G_RAID_DEBUG1(0, sc, "Array %s created.", name);
1911 struct g_raid_volume *
1912 g_raid_create_volume(struct g_raid_softc *sc, const char *name, int id)
1914 struct g_raid_volume *vol, *vol1;
1917 G_RAID_DEBUG1(1, sc, "Creating volume %s.", name);
1918 vol = malloc(sizeof(*vol), M_RAID, M_WAITOK | M_ZERO);
1920 strlcpy(vol->v_name, name, G_RAID_MAX_VOLUMENAME);
1921 vol->v_state = G_RAID_VOLUME_S_STARTING;
1922 vol->v_raid_level = G_RAID_VOLUME_RL_UNKNOWN;
1923 vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_UNKNOWN;
1924 vol->v_rotate_parity = 1;
1925 bioq_init(&vol->v_inflight);
1926 bioq_init(&vol->v_locked);
1927 LIST_INIT(&vol->v_locks);
1928 for (i = 0; i < G_RAID_MAX_SUBDISKS; i++) {
1929 vol->v_subdisks[i].sd_softc = sc;
1930 vol->v_subdisks[i].sd_volume = vol;
1931 vol->v_subdisks[i].sd_pos = i;
1932 vol->v_subdisks[i].sd_state = G_RAID_DISK_S_NONE;
1935 /* Find free ID for this volume. */
1939 LIST_FOREACH(vol1, &g_raid_volumes, v_global_next) {
1940 if (vol1->v_global_id == id)
1945 for (id = 0; ; id++) {
1946 LIST_FOREACH(vol1, &g_raid_volumes, v_global_next) {
1947 if (vol1->v_global_id == id)
1954 vol->v_global_id = id;
1955 LIST_INSERT_HEAD(&g_raid_volumes, vol, v_global_next);
1956 g_topology_unlock();
1958 /* Delay root mounting. */
1959 vol->v_rootmount = root_mount_hold("GRAID");
1960 G_RAID_DEBUG1(1, sc, "root_mount_hold %p", vol->v_rootmount);
1961 vol->v_starting = 1;
1962 TAILQ_INSERT_TAIL(&sc->sc_volumes, vol, v_next);
1966 struct g_raid_disk *
1967 g_raid_create_disk(struct g_raid_softc *sc)
1969 struct g_raid_disk *disk;
1971 G_RAID_DEBUG1(1, sc, "Creating disk.");
1972 disk = malloc(sizeof(*disk), M_RAID, M_WAITOK | M_ZERO);
1974 disk->d_state = G_RAID_DISK_S_NONE;
1975 TAILQ_INIT(&disk->d_subdisks);
1976 TAILQ_INSERT_TAIL(&sc->sc_disks, disk, d_next);
1980 int g_raid_start_volume(struct g_raid_volume *vol)
1982 struct g_raid_tr_class *class;
1983 struct g_raid_tr_object *obj;
1986 G_RAID_DEBUG1(2, vol->v_softc, "Starting volume %s.", vol->v_name);
1987 LIST_FOREACH(class, &g_raid_tr_classes, trc_list) {
1988 if (!class->trc_enable)
1990 G_RAID_DEBUG1(2, vol->v_softc,
1991 "Tasting volume %s for %s transformation.",
1992 vol->v_name, class->name);
1993 obj = (void *)kobj_create((kobj_class_t)class, M_RAID,
1995 obj->tro_class = class;
1996 obj->tro_volume = vol;
1997 status = G_RAID_TR_TASTE(obj, vol);
1998 if (status != G_RAID_TR_TASTE_FAIL)
2000 kobj_delete((kobj_t)obj, M_RAID);
2002 if (class == NULL) {
2003 G_RAID_DEBUG1(0, vol->v_softc,
2004 "No transformation module found for %s.",
2007 g_raid_change_volume_state(vol, G_RAID_VOLUME_S_UNSUPPORTED);
2008 g_raid_event_send(vol, G_RAID_VOLUME_E_DOWN,
2009 G_RAID_EVENT_VOLUME);
2012 G_RAID_DEBUG1(2, vol->v_softc,
2013 "Transformation module %s chosen for %s.",
2014 class->name, vol->v_name);
2020 g_raid_destroy_node(struct g_raid_softc *sc, int worker)
2022 struct g_raid_volume *vol, *tmpv;
2023 struct g_raid_disk *disk, *tmpd;
2026 sc->sc_stopping = G_RAID_DESTROY_HARD;
2027 TAILQ_FOREACH_SAFE(vol, &sc->sc_volumes, v_next, tmpv) {
2028 if (g_raid_destroy_volume(vol))
2033 TAILQ_FOREACH_SAFE(disk, &sc->sc_disks, d_next, tmpd) {
2034 if (g_raid_destroy_disk(disk))
2040 G_RAID_MD_FREE(sc->sc_md);
2041 kobj_delete((kobj_t)sc->sc_md, M_RAID);
2044 if (sc->sc_geom != NULL) {
2045 G_RAID_DEBUG1(0, sc, "Array %s destroyed.", sc->sc_name);
2047 sc->sc_geom->softc = NULL;
2048 g_wither_geom(sc->sc_geom, ENXIO);
2049 g_topology_unlock();
2052 G_RAID_DEBUG(1, "Array destroyed.");
2054 g_raid_event_cancel(sc, sc);
2055 mtx_destroy(&sc->sc_queue_mtx);
2056 sx_xunlock(&sc->sc_lock);
2057 sx_destroy(&sc->sc_lock);
2058 wakeup(&sc->sc_stopping);
2060 curthread->td_pflags &= ~TDP_GEOM;
2061 G_RAID_DEBUG(1, "Thread exiting.");
2064 /* Wake up worker to make it selfdestruct. */
2065 g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
2071 g_raid_destroy_volume(struct g_raid_volume *vol)
2073 struct g_raid_softc *sc;
2074 struct g_raid_disk *disk;
2078 G_RAID_DEBUG1(2, sc, "Destroying volume %s.", vol->v_name);
2079 vol->v_stopping = 1;
2080 if (vol->v_state != G_RAID_VOLUME_S_STOPPED) {
2082 G_RAID_TR_STOP(vol->v_tr);
2085 vol->v_state = G_RAID_VOLUME_S_STOPPED;
2087 if (g_raid_event_check(sc, vol) != 0)
2089 if (vol->v_provider != NULL)
2091 if (vol->v_provider_open != 0)
2094 G_RAID_TR_FREE(vol->v_tr);
2095 kobj_delete((kobj_t)vol->v_tr, M_RAID);
2098 if (vol->v_rootmount)
2099 root_mount_rel(vol->v_rootmount);
2101 LIST_REMOVE(vol, v_global_next);
2102 g_topology_unlock();
2103 TAILQ_REMOVE(&sc->sc_volumes, vol, v_next);
2104 for (i = 0; i < G_RAID_MAX_SUBDISKS; i++) {
2105 g_raid_event_cancel(sc, &vol->v_subdisks[i]);
2106 disk = vol->v_subdisks[i].sd_disk;
2109 TAILQ_REMOVE(&disk->d_subdisks, &vol->v_subdisks[i], sd_next);
2111 G_RAID_DEBUG1(2, sc, "Volume %s destroyed.", vol->v_name);
2113 G_RAID_MD_FREE_VOLUME(sc->sc_md, vol);
2114 g_raid_event_cancel(sc, vol);
2116 if (sc->sc_stopping == G_RAID_DESTROY_HARD) {
2117 /* Wake up worker to let it selfdestruct. */
2118 g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
2124 g_raid_destroy_disk(struct g_raid_disk *disk)
2126 struct g_raid_softc *sc;
2127 struct g_raid_subdisk *sd, *tmp;
2130 G_RAID_DEBUG1(2, sc, "Destroying disk.");
2131 if (disk->d_consumer) {
2132 g_raid_kill_consumer(sc, disk->d_consumer);
2133 disk->d_consumer = NULL;
2135 TAILQ_FOREACH_SAFE(sd, &disk->d_subdisks, sd_next, tmp) {
2136 g_raid_change_subdisk_state(sd, G_RAID_SUBDISK_S_NONE);
2137 g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED,
2138 G_RAID_EVENT_SUBDISK);
2139 TAILQ_REMOVE(&disk->d_subdisks, sd, sd_next);
2142 TAILQ_REMOVE(&sc->sc_disks, disk, d_next);
2144 G_RAID_MD_FREE_DISK(sc->sc_md, disk);
2145 g_raid_event_cancel(sc, disk);
2151 g_raid_destroy(struct g_raid_softc *sc, int how)
2155 g_topology_assert_not();
2158 sx_assert(&sc->sc_lock, SX_XLOCKED);
2160 /* Count open volumes. */
2161 opens = g_raid_nopens(sc);
2163 /* React on some opened volumes. */
2166 case G_RAID_DESTROY_SOFT:
2167 G_RAID_DEBUG1(1, sc,
2168 "%d volumes are still open.",
2170 sx_xunlock(&sc->sc_lock);
2172 case G_RAID_DESTROY_DELAYED:
2173 G_RAID_DEBUG1(1, sc,
2174 "Array will be destroyed on last close.");
2175 sc->sc_stopping = G_RAID_DESTROY_DELAYED;
2176 sx_xunlock(&sc->sc_lock);
2178 case G_RAID_DESTROY_HARD:
2179 G_RAID_DEBUG1(1, sc,
2180 "%d volumes are still open.",
2185 /* Mark node for destruction. */
2186 sc->sc_stopping = G_RAID_DESTROY_HARD;
2187 /* Wake up worker to let it selfdestruct. */
2188 g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
2189 /* Sleep until node destroyed. */
2190 error = sx_sleep(&sc->sc_stopping, &sc->sc_lock,
2191 PRIBIO | PDROP, "r:destroy", hz * 3);
2192 return (error == EWOULDBLOCK ? EBUSY : 0);
2196 g_raid_taste_orphan(struct g_consumer *cp)
2199 KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
2200 cp->provider->name));
2203 static struct g_geom *
2204 g_raid_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
2206 struct g_consumer *cp;
2207 struct g_geom *gp, *geom;
2208 struct g_raid_md_class *class;
2209 struct g_raid_md_object *obj;
2212 g_topology_assert();
2213 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
2216 G_RAID_DEBUG(2, "Tasting provider %s.", pp->name);
2219 status = G_RAID_MD_TASTE_FAIL;
2220 gp = g_new_geomf(mp, "raid:taste");
2222 * This orphan function should be never called.
2224 gp->orphan = g_raid_taste_orphan;
2225 cp = g_new_consumer(gp);
2226 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
2227 if (g_attach(cp, pp) != 0)
2229 if (g_access(cp, 1, 0, 0) != 0)
2232 LIST_FOREACH(class, &g_raid_md_classes, mdc_list) {
2233 if (!class->mdc_enable)
2235 G_RAID_DEBUG(2, "Tasting provider %s for %s metadata.",
2236 pp->name, class->name);
2237 obj = (void *)kobj_create((kobj_class_t)class, M_RAID,
2239 obj->mdo_class = class;
2240 status = G_RAID_MD_TASTE(obj, mp, cp, &geom);
2241 if (status != G_RAID_MD_TASTE_NEW)
2242 kobj_delete((kobj_t)obj, M_RAID);
2243 if (status != G_RAID_MD_TASTE_FAIL)
2247 if (status == G_RAID_MD_TASTE_FAIL)
2248 (void)g_access(cp, -1, 0, 0);
2252 g_destroy_consumer(cp);
2254 G_RAID_DEBUG(2, "Tasting provider %s done.", pp->name);
2259 g_raid_create_node_format(const char *format, struct gctl_req *req,
2262 struct g_raid_md_class *class;
2263 struct g_raid_md_object *obj;
2266 G_RAID_DEBUG(2, "Creating array for %s metadata.", format);
2267 LIST_FOREACH(class, &g_raid_md_classes, mdc_list) {
2268 if (strcasecmp(class->name, format) == 0)
2271 if (class == NULL) {
2272 G_RAID_DEBUG(1, "No support for %s metadata.", format);
2273 return (G_RAID_MD_TASTE_FAIL);
2275 obj = (void *)kobj_create((kobj_class_t)class, M_RAID,
2277 obj->mdo_class = class;
2278 status = G_RAID_MD_CREATE_REQ(obj, &g_raid_class, req, gp);
2279 if (status != G_RAID_MD_TASTE_NEW)
2280 kobj_delete((kobj_t)obj, M_RAID);
2285 g_raid_destroy_geom(struct gctl_req *req __unused,
2286 struct g_class *mp __unused, struct g_geom *gp)
2288 struct g_raid_softc *sc;
2291 g_topology_unlock();
2293 sx_xlock(&sc->sc_lock);
2295 error = g_raid_destroy(gp->softc, G_RAID_DESTROY_SOFT);
2300 void g_raid_write_metadata(struct g_raid_softc *sc, struct g_raid_volume *vol,
2301 struct g_raid_subdisk *sd, struct g_raid_disk *disk)
2304 if (sc->sc_stopping == G_RAID_DESTROY_HARD)
2307 G_RAID_MD_WRITE(sc->sc_md, vol, sd, disk);
2310 void g_raid_fail_disk(struct g_raid_softc *sc,
2311 struct g_raid_subdisk *sd, struct g_raid_disk *disk)
2317 G_RAID_DEBUG1(0, sc, "Warning! Fail request to an absent disk!");
2320 if (disk->d_state != G_RAID_DISK_S_ACTIVE) {
2321 G_RAID_DEBUG1(0, sc, "Warning! Fail request to a disk in a "
2322 "wrong state (%s)!", g_raid_disk_state2str(disk->d_state));
2326 G_RAID_MD_FAIL_DISK(sc->sc_md, sd, disk);
2330 g_raid_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
2331 struct g_consumer *cp, struct g_provider *pp)
2333 struct g_raid_softc *sc;
2334 struct g_raid_volume *vol;
2335 struct g_raid_subdisk *sd;
2336 struct g_raid_disk *disk;
2339 g_topology_assert();
2346 g_topology_unlock();
2347 sx_xlock(&sc->sc_lock);
2348 sbuf_printf(sb, "%s<descr>%s %s volume</descr>\n", indent,
2349 sc->sc_md->mdo_class->name,
2350 g_raid_volume_level2str(vol->v_raid_level,
2351 vol->v_raid_level_qualifier));
2352 sbuf_printf(sb, "%s<Label>%s</Label>\n", indent,
2354 sbuf_printf(sb, "%s<RAIDLevel>%s</RAIDLevel>\n", indent,
2355 g_raid_volume_level2str(vol->v_raid_level,
2356 vol->v_raid_level_qualifier));
2358 "%s<Transformation>%s</Transformation>\n", indent,
2359 vol->v_tr ? vol->v_tr->tro_class->name : "NONE");
2360 sbuf_printf(sb, "%s<Components>%u</Components>\n", indent,
2361 vol->v_disks_count);
2362 sbuf_printf(sb, "%s<Strip>%u</Strip>\n", indent,
2364 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
2365 g_raid_volume_state2str(vol->v_state));
2366 sbuf_printf(sb, "%s<Dirty>%s</Dirty>\n", indent,
2367 vol->v_dirty ? "Yes" : "No");
2368 sbuf_printf(sb, "%s<Subdisks>", indent);
2369 for (i = 0; i < vol->v_disks_count; i++) {
2370 sd = &vol->v_subdisks[i];
2371 if (sd->sd_disk != NULL &&
2372 sd->sd_disk->d_consumer != NULL) {
2373 sbuf_printf(sb, "%s ",
2374 g_raid_get_diskname(sd->sd_disk));
2376 sbuf_cat(sb, "NONE ");
2378 sbuf_printf(sb, "(%s",
2379 g_raid_subdisk_state2str(sd->sd_state));
2380 if (sd->sd_state == G_RAID_SUBDISK_S_REBUILD ||
2381 sd->sd_state == G_RAID_SUBDISK_S_RESYNC) {
2382 sbuf_printf(sb, " %d%%",
2383 (int)(sd->sd_rebuild_pos * 100 /
2387 if (i + 1 < vol->v_disks_count)
2390 sbuf_cat(sb, "</Subdisks>\n");
2391 sx_xunlock(&sc->sc_lock);
2393 } else if (cp != NULL) {
2397 g_topology_unlock();
2398 sx_xlock(&sc->sc_lock);
2399 sbuf_printf(sb, "%s<State>%s", indent,
2400 g_raid_disk_state2str(disk->d_state));
2401 if (!TAILQ_EMPTY(&disk->d_subdisks)) {
2403 TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
2404 sbuf_printf(sb, "%s",
2405 g_raid_subdisk_state2str(sd->sd_state));
2406 if (sd->sd_state == G_RAID_SUBDISK_S_REBUILD ||
2407 sd->sd_state == G_RAID_SUBDISK_S_RESYNC) {
2408 sbuf_printf(sb, " %d%%",
2409 (int)(sd->sd_rebuild_pos * 100 /
2412 if (TAILQ_NEXT(sd, sd_next))
2417 sbuf_cat(sb, "</State>\n");
2418 sbuf_printf(sb, "%s<Subdisks>", indent);
2419 TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
2420 sbuf_printf(sb, "r%d(%s):%d@%ju",
2421 sd->sd_volume->v_global_id,
2422 sd->sd_volume->v_name,
2423 sd->sd_pos, (uintmax_t)sd->sd_offset);
2424 if (TAILQ_NEXT(sd, sd_next))
2427 sbuf_cat(sb, "</Subdisks>\n");
2428 sbuf_printf(sb, "%s<ReadErrors>%d</ReadErrors>\n", indent,
2430 sx_xunlock(&sc->sc_lock);
2433 g_topology_unlock();
2434 sx_xlock(&sc->sc_lock);
2436 sbuf_printf(sb, "%s<Metadata>%s</Metadata>\n", indent,
2437 sc->sc_md->mdo_class->name);
2439 if (!TAILQ_EMPTY(&sc->sc_volumes)) {
2441 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2442 if (vol->v_state < s)
2445 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
2446 g_raid_volume_state2str(s));
2448 sx_xunlock(&sc->sc_lock);
2454 g_raid_shutdown_post_sync(void *arg, int howto)
2457 struct g_geom *gp, *gp2;
2458 struct g_raid_softc *sc;
2459 struct g_raid_volume *vol;
2463 g_raid_shutdown = 1;
2464 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
2465 if ((sc = gp->softc) == NULL)
2467 g_topology_unlock();
2468 sx_xlock(&sc->sc_lock);
2469 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next)
2470 g_raid_clean(vol, -1);
2472 g_raid_destroy(sc, G_RAID_DESTROY_DELAYED);
2475 g_topology_unlock();
2479 g_raid_init(struct g_class *mp)
2482 g_raid_post_sync = EVENTHANDLER_REGISTER(shutdown_post_sync,
2483 g_raid_shutdown_post_sync, mp, SHUTDOWN_PRI_FIRST);
2484 if (g_raid_post_sync == NULL)
2485 G_RAID_DEBUG(0, "Warning! Cannot register shutdown event.");
2490 g_raid_fini(struct g_class *mp)
2493 if (g_raid_post_sync != NULL)
2494 EVENTHANDLER_DEREGISTER(shutdown_post_sync, g_raid_post_sync);
2499 g_raid_md_modevent(module_t mod, int type, void *arg)
2501 struct g_raid_md_class *class, *c, *nc;
2508 c = LIST_FIRST(&g_raid_md_classes);
2509 if (c == NULL || c->mdc_priority > class->mdc_priority)
2510 LIST_INSERT_HEAD(&g_raid_md_classes, class, mdc_list);
2512 while ((nc = LIST_NEXT(c, mdc_list)) != NULL &&
2513 nc->mdc_priority < class->mdc_priority)
2515 LIST_INSERT_AFTER(c, class, mdc_list);
2518 g_retaste(&g_raid_class);
2521 LIST_REMOVE(class, mdc_list);
2532 g_raid_tr_modevent(module_t mod, int type, void *arg)
2534 struct g_raid_tr_class *class, *c, *nc;
2541 c = LIST_FIRST(&g_raid_tr_classes);
2542 if (c == NULL || c->trc_priority > class->trc_priority)
2543 LIST_INSERT_HEAD(&g_raid_tr_classes, class, trc_list);
2545 while ((nc = LIST_NEXT(c, trc_list)) != NULL &&
2546 nc->trc_priority < class->trc_priority)
2548 LIST_INSERT_AFTER(c, class, trc_list);
2552 LIST_REMOVE(class, trc_list);
2563 * Use local implementation of DECLARE_GEOM_CLASS(g_raid_class, g_raid)
2564 * to reduce module priority, allowing submodules to register them first.
2566 static moduledata_t g_raid_mod = {
2571 DECLARE_MODULE(g_raid, g_raid_mod, SI_SUB_DRIVERS, SI_ORDER_THIRD);
2572 MODULE_VERSION(geom_raid, 0);