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>
47 #include <sys/kthread.h>
48 #include <sys/sched.h>
49 #include <geom/raid/g_raid.h>
50 #include "g_raid_md_if.h"
51 #include "g_raid_tr_if.h"
53 static MALLOC_DEFINE(M_RAID, "raid_data", "GEOM_RAID Data");
55 SYSCTL_DECL(_kern_geom);
56 SYSCTL_NODE(_kern_geom, OID_AUTO, raid, CTLFLAG_RW, 0, "GEOM_RAID stuff");
57 int g_raid_enable = 1;
58 SYSCTL_INT(_kern_geom_raid, OID_AUTO, enable, CTLFLAG_RWTUN,
59 &g_raid_enable, 0, "Enable on-disk metadata taste");
60 u_int g_raid_aggressive_spare = 0;
61 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, aggressive_spare, CTLFLAG_RWTUN,
62 &g_raid_aggressive_spare, 0, "Use disks without metadata as spare");
63 u_int g_raid_debug = 0;
64 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, debug, CTLFLAG_RWTUN, &g_raid_debug, 0,
66 int g_raid_read_err_thresh = 10;
67 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, read_err_thresh, CTLFLAG_RWTUN,
68 &g_raid_read_err_thresh, 0,
69 "Number of read errors equated to disk failure");
70 u_int g_raid_start_timeout = 30;
71 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, start_timeout, CTLFLAG_RWTUN,
72 &g_raid_start_timeout, 0,
73 "Time to wait for all array components");
74 static u_int g_raid_clean_time = 5;
75 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, clean_time, CTLFLAG_RWTUN,
76 &g_raid_clean_time, 0, "Mark volume as clean when idling");
77 static u_int g_raid_disconnect_on_failure = 1;
78 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, disconnect_on_failure, CTLFLAG_RWTUN,
79 &g_raid_disconnect_on_failure, 0, "Disconnect component on I/O failure.");
80 static u_int g_raid_name_format = 0;
81 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, name_format, CTLFLAG_RWTUN,
82 &g_raid_name_format, 0, "Providers name format.");
83 static u_int g_raid_idle_threshold = 1000000;
84 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, idle_threshold, CTLFLAG_RWTUN,
85 &g_raid_idle_threshold, 1000000,
86 "Time in microseconds to consider a volume idle.");
88 #define MSLEEP(rv, ident, mtx, priority, wmesg, timeout) do { \
89 G_RAID_DEBUG(4, "%s: Sleeping %p.", __func__, (ident)); \
90 rv = msleep((ident), (mtx), (priority), (wmesg), (timeout)); \
91 G_RAID_DEBUG(4, "%s: Woken up %p.", __func__, (ident)); \
94 LIST_HEAD(, g_raid_md_class) g_raid_md_classes =
95 LIST_HEAD_INITIALIZER(g_raid_md_classes);
97 LIST_HEAD(, g_raid_tr_class) g_raid_tr_classes =
98 LIST_HEAD_INITIALIZER(g_raid_tr_classes);
100 LIST_HEAD(, g_raid_volume) g_raid_volumes =
101 LIST_HEAD_INITIALIZER(g_raid_volumes);
103 static eventhandler_tag g_raid_post_sync = NULL;
104 static int g_raid_started = 0;
105 static int g_raid_shutdown = 0;
107 static int g_raid_destroy_geom(struct gctl_req *req, struct g_class *mp,
109 static g_taste_t g_raid_taste;
110 static void g_raid_init(struct g_class *mp);
111 static void g_raid_fini(struct g_class *mp);
113 struct g_class g_raid_class = {
114 .name = G_RAID_CLASS_NAME,
115 .version = G_VERSION,
116 .ctlreq = g_raid_ctl,
117 .taste = g_raid_taste,
118 .destroy_geom = g_raid_destroy_geom,
123 static void g_raid_destroy_provider(struct g_raid_volume *vol);
124 static int g_raid_update_disk(struct g_raid_disk *disk, u_int event);
125 static int g_raid_update_subdisk(struct g_raid_subdisk *subdisk, u_int event);
126 static int g_raid_update_volume(struct g_raid_volume *vol, u_int event);
127 static int g_raid_update_node(struct g_raid_softc *sc, u_int event);
128 static void g_raid_dumpconf(struct sbuf *sb, const char *indent,
129 struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
130 static void g_raid_start(struct bio *bp);
131 static void g_raid_start_request(struct bio *bp);
132 static void g_raid_disk_done(struct bio *bp);
133 static void g_raid_poll(struct g_raid_softc *sc);
136 g_raid_node_event2str(int event)
140 case G_RAID_NODE_E_WAKE:
142 case G_RAID_NODE_E_START:
150 g_raid_disk_state2str(int state)
154 case G_RAID_DISK_S_NONE:
156 case G_RAID_DISK_S_OFFLINE:
158 case G_RAID_DISK_S_DISABLED:
160 case G_RAID_DISK_S_FAILED:
162 case G_RAID_DISK_S_STALE_FAILED:
163 return ("STALE_FAILED");
164 case G_RAID_DISK_S_SPARE:
166 case G_RAID_DISK_S_STALE:
168 case G_RAID_DISK_S_ACTIVE:
176 g_raid_disk_event2str(int event)
180 case G_RAID_DISK_E_DISCONNECTED:
181 return ("DISCONNECTED");
188 g_raid_subdisk_state2str(int state)
192 case G_RAID_SUBDISK_S_NONE:
194 case G_RAID_SUBDISK_S_FAILED:
196 case G_RAID_SUBDISK_S_NEW:
198 case G_RAID_SUBDISK_S_REBUILD:
200 case G_RAID_SUBDISK_S_UNINITIALIZED:
201 return ("UNINITIALIZED");
202 case G_RAID_SUBDISK_S_STALE:
204 case G_RAID_SUBDISK_S_RESYNC:
206 case G_RAID_SUBDISK_S_ACTIVE:
214 g_raid_subdisk_event2str(int event)
218 case G_RAID_SUBDISK_E_NEW:
220 case G_RAID_SUBDISK_E_FAILED:
222 case G_RAID_SUBDISK_E_DISCONNECTED:
223 return ("DISCONNECTED");
230 g_raid_volume_state2str(int state)
234 case G_RAID_VOLUME_S_STARTING:
236 case G_RAID_VOLUME_S_BROKEN:
238 case G_RAID_VOLUME_S_DEGRADED:
240 case G_RAID_VOLUME_S_SUBOPTIMAL:
241 return ("SUBOPTIMAL");
242 case G_RAID_VOLUME_S_OPTIMAL:
244 case G_RAID_VOLUME_S_UNSUPPORTED:
245 return ("UNSUPPORTED");
246 case G_RAID_VOLUME_S_STOPPED:
254 g_raid_volume_event2str(int event)
258 case G_RAID_VOLUME_E_UP:
260 case G_RAID_VOLUME_E_DOWN:
262 case G_RAID_VOLUME_E_START:
264 case G_RAID_VOLUME_E_STARTMD:
272 g_raid_volume_level2str(int level, int qual)
276 case G_RAID_VOLUME_RL_RAID0:
278 case G_RAID_VOLUME_RL_RAID1:
280 case G_RAID_VOLUME_RL_RAID3:
281 if (qual == G_RAID_VOLUME_RLQ_R3P0)
283 if (qual == G_RAID_VOLUME_RLQ_R3PN)
286 case G_RAID_VOLUME_RL_RAID4:
287 if (qual == G_RAID_VOLUME_RLQ_R4P0)
289 if (qual == G_RAID_VOLUME_RLQ_R4PN)
292 case G_RAID_VOLUME_RL_RAID5:
293 if (qual == G_RAID_VOLUME_RLQ_R5RA)
295 if (qual == G_RAID_VOLUME_RLQ_R5RS)
297 if (qual == G_RAID_VOLUME_RLQ_R5LA)
299 if (qual == G_RAID_VOLUME_RLQ_R5LS)
302 case G_RAID_VOLUME_RL_RAID6:
303 if (qual == G_RAID_VOLUME_RLQ_R6RA)
305 if (qual == G_RAID_VOLUME_RLQ_R6RS)
307 if (qual == G_RAID_VOLUME_RLQ_R6LA)
309 if (qual == G_RAID_VOLUME_RLQ_R6LS)
312 case G_RAID_VOLUME_RL_RAIDMDF:
313 if (qual == G_RAID_VOLUME_RLQ_RMDFRA)
314 return ("RAIDMDF-RA");
315 if (qual == G_RAID_VOLUME_RLQ_RMDFRS)
316 return ("RAIDMDF-RS");
317 if (qual == G_RAID_VOLUME_RLQ_RMDFLA)
318 return ("RAIDMDF-LA");
319 if (qual == G_RAID_VOLUME_RLQ_RMDFLS)
320 return ("RAIDMDF-LS");
322 case G_RAID_VOLUME_RL_RAID1E:
323 if (qual == G_RAID_VOLUME_RLQ_R1EA)
325 if (qual == G_RAID_VOLUME_RLQ_R1EO)
328 case G_RAID_VOLUME_RL_SINGLE:
330 case G_RAID_VOLUME_RL_CONCAT:
332 case G_RAID_VOLUME_RL_RAID5E:
333 if (qual == G_RAID_VOLUME_RLQ_R5ERA)
334 return ("RAID5E-RA");
335 if (qual == G_RAID_VOLUME_RLQ_R5ERS)
336 return ("RAID5E-RS");
337 if (qual == G_RAID_VOLUME_RLQ_R5ELA)
338 return ("RAID5E-LA");
339 if (qual == G_RAID_VOLUME_RLQ_R5ELS)
340 return ("RAID5E-LS");
342 case G_RAID_VOLUME_RL_RAID5EE:
343 if (qual == G_RAID_VOLUME_RLQ_R5EERA)
344 return ("RAID5EE-RA");
345 if (qual == G_RAID_VOLUME_RLQ_R5EERS)
346 return ("RAID5EE-RS");
347 if (qual == G_RAID_VOLUME_RLQ_R5EELA)
348 return ("RAID5EE-LA");
349 if (qual == G_RAID_VOLUME_RLQ_R5EELS)
350 return ("RAID5EE-LS");
352 case G_RAID_VOLUME_RL_RAID5R:
353 if (qual == G_RAID_VOLUME_RLQ_R5RRA)
354 return ("RAID5R-RA");
355 if (qual == G_RAID_VOLUME_RLQ_R5RRS)
356 return ("RAID5R-RS");
357 if (qual == G_RAID_VOLUME_RLQ_R5RLA)
358 return ("RAID5R-LA");
359 if (qual == G_RAID_VOLUME_RLQ_R5RLS)
360 return ("RAID5R-LS");
368 g_raid_volume_str2level(const char *str, int *level, int *qual)
371 *level = G_RAID_VOLUME_RL_UNKNOWN;
372 *qual = G_RAID_VOLUME_RLQ_NONE;
373 if (strcasecmp(str, "RAID0") == 0)
374 *level = G_RAID_VOLUME_RL_RAID0;
375 else if (strcasecmp(str, "RAID1") == 0)
376 *level = G_RAID_VOLUME_RL_RAID1;
377 else if (strcasecmp(str, "RAID3-P0") == 0) {
378 *level = G_RAID_VOLUME_RL_RAID3;
379 *qual = G_RAID_VOLUME_RLQ_R3P0;
380 } else if (strcasecmp(str, "RAID3-PN") == 0 ||
381 strcasecmp(str, "RAID3") == 0) {
382 *level = G_RAID_VOLUME_RL_RAID3;
383 *qual = G_RAID_VOLUME_RLQ_R3PN;
384 } else if (strcasecmp(str, "RAID4-P0") == 0) {
385 *level = G_RAID_VOLUME_RL_RAID4;
386 *qual = G_RAID_VOLUME_RLQ_R4P0;
387 } else if (strcasecmp(str, "RAID4-PN") == 0 ||
388 strcasecmp(str, "RAID4") == 0) {
389 *level = G_RAID_VOLUME_RL_RAID4;
390 *qual = G_RAID_VOLUME_RLQ_R4PN;
391 } else if (strcasecmp(str, "RAID5-RA") == 0) {
392 *level = G_RAID_VOLUME_RL_RAID5;
393 *qual = G_RAID_VOLUME_RLQ_R5RA;
394 } else if (strcasecmp(str, "RAID5-RS") == 0) {
395 *level = G_RAID_VOLUME_RL_RAID5;
396 *qual = G_RAID_VOLUME_RLQ_R5RS;
397 } else if (strcasecmp(str, "RAID5") == 0 ||
398 strcasecmp(str, "RAID5-LA") == 0) {
399 *level = G_RAID_VOLUME_RL_RAID5;
400 *qual = G_RAID_VOLUME_RLQ_R5LA;
401 } else if (strcasecmp(str, "RAID5-LS") == 0) {
402 *level = G_RAID_VOLUME_RL_RAID5;
403 *qual = G_RAID_VOLUME_RLQ_R5LS;
404 } else if (strcasecmp(str, "RAID6-RA") == 0) {
405 *level = G_RAID_VOLUME_RL_RAID6;
406 *qual = G_RAID_VOLUME_RLQ_R6RA;
407 } else if (strcasecmp(str, "RAID6-RS") == 0) {
408 *level = G_RAID_VOLUME_RL_RAID6;
409 *qual = G_RAID_VOLUME_RLQ_R6RS;
410 } else if (strcasecmp(str, "RAID6") == 0 ||
411 strcasecmp(str, "RAID6-LA") == 0) {
412 *level = G_RAID_VOLUME_RL_RAID6;
413 *qual = G_RAID_VOLUME_RLQ_R6LA;
414 } else if (strcasecmp(str, "RAID6-LS") == 0) {
415 *level = G_RAID_VOLUME_RL_RAID6;
416 *qual = G_RAID_VOLUME_RLQ_R6LS;
417 } else if (strcasecmp(str, "RAIDMDF-RA") == 0) {
418 *level = G_RAID_VOLUME_RL_RAIDMDF;
419 *qual = G_RAID_VOLUME_RLQ_RMDFRA;
420 } else if (strcasecmp(str, "RAIDMDF-RS") == 0) {
421 *level = G_RAID_VOLUME_RL_RAIDMDF;
422 *qual = G_RAID_VOLUME_RLQ_RMDFRS;
423 } else if (strcasecmp(str, "RAIDMDF") == 0 ||
424 strcasecmp(str, "RAIDMDF-LA") == 0) {
425 *level = G_RAID_VOLUME_RL_RAIDMDF;
426 *qual = G_RAID_VOLUME_RLQ_RMDFLA;
427 } else if (strcasecmp(str, "RAIDMDF-LS") == 0) {
428 *level = G_RAID_VOLUME_RL_RAIDMDF;
429 *qual = G_RAID_VOLUME_RLQ_RMDFLS;
430 } else if (strcasecmp(str, "RAID10") == 0 ||
431 strcasecmp(str, "RAID1E") == 0 ||
432 strcasecmp(str, "RAID1E-A") == 0) {
433 *level = G_RAID_VOLUME_RL_RAID1E;
434 *qual = G_RAID_VOLUME_RLQ_R1EA;
435 } else if (strcasecmp(str, "RAID1E-O") == 0) {
436 *level = G_RAID_VOLUME_RL_RAID1E;
437 *qual = G_RAID_VOLUME_RLQ_R1EO;
438 } else if (strcasecmp(str, "SINGLE") == 0)
439 *level = G_RAID_VOLUME_RL_SINGLE;
440 else if (strcasecmp(str, "CONCAT") == 0)
441 *level = G_RAID_VOLUME_RL_CONCAT;
442 else if (strcasecmp(str, "RAID5E-RA") == 0) {
443 *level = G_RAID_VOLUME_RL_RAID5E;
444 *qual = G_RAID_VOLUME_RLQ_R5ERA;
445 } else if (strcasecmp(str, "RAID5E-RS") == 0) {
446 *level = G_RAID_VOLUME_RL_RAID5E;
447 *qual = G_RAID_VOLUME_RLQ_R5ERS;
448 } else if (strcasecmp(str, "RAID5E") == 0 ||
449 strcasecmp(str, "RAID5E-LA") == 0) {
450 *level = G_RAID_VOLUME_RL_RAID5E;
451 *qual = G_RAID_VOLUME_RLQ_R5ELA;
452 } else if (strcasecmp(str, "RAID5E-LS") == 0) {
453 *level = G_RAID_VOLUME_RL_RAID5E;
454 *qual = G_RAID_VOLUME_RLQ_R5ELS;
455 } else if (strcasecmp(str, "RAID5EE-RA") == 0) {
456 *level = G_RAID_VOLUME_RL_RAID5EE;
457 *qual = G_RAID_VOLUME_RLQ_R5EERA;
458 } else if (strcasecmp(str, "RAID5EE-RS") == 0) {
459 *level = G_RAID_VOLUME_RL_RAID5EE;
460 *qual = G_RAID_VOLUME_RLQ_R5EERS;
461 } else if (strcasecmp(str, "RAID5EE") == 0 ||
462 strcasecmp(str, "RAID5EE-LA") == 0) {
463 *level = G_RAID_VOLUME_RL_RAID5EE;
464 *qual = G_RAID_VOLUME_RLQ_R5EELA;
465 } else if (strcasecmp(str, "RAID5EE-LS") == 0) {
466 *level = G_RAID_VOLUME_RL_RAID5EE;
467 *qual = G_RAID_VOLUME_RLQ_R5EELS;
468 } else if (strcasecmp(str, "RAID5R-RA") == 0) {
469 *level = G_RAID_VOLUME_RL_RAID5R;
470 *qual = G_RAID_VOLUME_RLQ_R5RRA;
471 } else if (strcasecmp(str, "RAID5R-RS") == 0) {
472 *level = G_RAID_VOLUME_RL_RAID5R;
473 *qual = G_RAID_VOLUME_RLQ_R5RRS;
474 } else if (strcasecmp(str, "RAID5R") == 0 ||
475 strcasecmp(str, "RAID5R-LA") == 0) {
476 *level = G_RAID_VOLUME_RL_RAID5R;
477 *qual = G_RAID_VOLUME_RLQ_R5RLA;
478 } else if (strcasecmp(str, "RAID5R-LS") == 0) {
479 *level = G_RAID_VOLUME_RL_RAID5R;
480 *qual = G_RAID_VOLUME_RLQ_R5RLS;
487 g_raid_get_diskname(struct g_raid_disk *disk)
490 if (disk->d_consumer == NULL || disk->d_consumer->provider == NULL)
491 return ("[unknown]");
492 return (disk->d_consumer->provider->name);
496 g_raid_get_disk_info(struct g_raid_disk *disk)
498 struct g_consumer *cp = disk->d_consumer;
501 /* Read kernel dumping information. */
502 disk->d_kd.offset = 0;
503 disk->d_kd.length = OFF_MAX;
504 len = sizeof(disk->d_kd);
505 error = g_io_getattr("GEOM::kerneldump", cp, &len, &disk->d_kd);
507 disk->d_kd.di.dumper = NULL;
508 if (disk->d_kd.di.dumper == NULL)
509 G_RAID_DEBUG1(2, disk->d_softc,
510 "Dumping not supported by %s: %d.",
511 cp->provider->name, error);
513 /* Read BIO_DELETE support. */
514 error = g_getattr("GEOM::candelete", cp, &disk->d_candelete);
516 disk->d_candelete = 0;
517 if (!disk->d_candelete)
518 G_RAID_DEBUG1(2, disk->d_softc,
519 "BIO_DELETE not supported by %s: %d.",
520 cp->provider->name, error);
524 g_raid_report_disk_state(struct g_raid_disk *disk)
526 struct g_raid_subdisk *sd;
530 if (disk->d_consumer == NULL)
532 if (disk->d_state == G_RAID_DISK_S_DISABLED) {
533 s = G_STATE_ACTIVE; /* XXX */
534 } else if (disk->d_state == G_RAID_DISK_S_FAILED ||
535 disk->d_state == G_RAID_DISK_S_STALE_FAILED) {
538 state = G_RAID_SUBDISK_S_ACTIVE;
539 TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
540 if (sd->sd_state < state)
541 state = sd->sd_state;
543 if (state == G_RAID_SUBDISK_S_FAILED)
545 else if (state == G_RAID_SUBDISK_S_NEW ||
546 state == G_RAID_SUBDISK_S_REBUILD)
548 else if (state == G_RAID_SUBDISK_S_STALE ||
549 state == G_RAID_SUBDISK_S_RESYNC)
555 g_io_getattr("GEOM::setstate", disk->d_consumer, &len, &s);
556 G_RAID_DEBUG1(2, disk->d_softc, "Disk %s state reported as %d.",
557 g_raid_get_diskname(disk), s);
561 g_raid_change_disk_state(struct g_raid_disk *disk, int state)
564 G_RAID_DEBUG1(0, disk->d_softc, "Disk %s state changed from %s to %s.",
565 g_raid_get_diskname(disk),
566 g_raid_disk_state2str(disk->d_state),
567 g_raid_disk_state2str(state));
568 disk->d_state = state;
569 g_raid_report_disk_state(disk);
573 g_raid_change_subdisk_state(struct g_raid_subdisk *sd, int state)
576 G_RAID_DEBUG1(0, sd->sd_softc,
577 "Subdisk %s:%d-%s state changed from %s to %s.",
578 sd->sd_volume->v_name, sd->sd_pos,
579 sd->sd_disk ? g_raid_get_diskname(sd->sd_disk) : "[none]",
580 g_raid_subdisk_state2str(sd->sd_state),
581 g_raid_subdisk_state2str(state));
582 sd->sd_state = state;
584 g_raid_report_disk_state(sd->sd_disk);
588 g_raid_change_volume_state(struct g_raid_volume *vol, int state)
591 G_RAID_DEBUG1(0, vol->v_softc,
592 "Volume %s state changed from %s to %s.",
594 g_raid_volume_state2str(vol->v_state),
595 g_raid_volume_state2str(state));
596 vol->v_state = state;
600 * --- Events handling functions ---
601 * Events in geom_raid are used to maintain subdisks and volumes status
602 * from one thread to simplify locking.
605 g_raid_event_free(struct g_raid_event *ep)
612 g_raid_event_send(void *arg, int event, int flags)
614 struct g_raid_softc *sc;
615 struct g_raid_event *ep;
618 if ((flags & G_RAID_EVENT_VOLUME) != 0) {
619 sc = ((struct g_raid_volume *)arg)->v_softc;
620 } else if ((flags & G_RAID_EVENT_DISK) != 0) {
621 sc = ((struct g_raid_disk *)arg)->d_softc;
622 } else if ((flags & G_RAID_EVENT_SUBDISK) != 0) {
623 sc = ((struct g_raid_subdisk *)arg)->sd_softc;
627 ep = malloc(sizeof(*ep), M_RAID,
628 sx_xlocked(&sc->sc_lock) ? M_WAITOK : M_NOWAIT);
635 G_RAID_DEBUG1(4, sc, "Sending event %p. Waking up %p.", ep, sc);
636 mtx_lock(&sc->sc_queue_mtx);
637 TAILQ_INSERT_TAIL(&sc->sc_events, ep, e_next);
638 mtx_unlock(&sc->sc_queue_mtx);
641 if ((flags & G_RAID_EVENT_WAIT) == 0)
644 sx_assert(&sc->sc_lock, SX_XLOCKED);
645 G_RAID_DEBUG1(4, sc, "Sleeping on %p.", ep);
646 sx_xunlock(&sc->sc_lock);
647 while ((ep->e_flags & G_RAID_EVENT_DONE) == 0) {
648 mtx_lock(&sc->sc_queue_mtx);
649 MSLEEP(error, ep, &sc->sc_queue_mtx, PRIBIO | PDROP, "m:event",
653 g_raid_event_free(ep);
654 sx_xlock(&sc->sc_lock);
659 g_raid_event_cancel(struct g_raid_softc *sc, void *tgt)
661 struct g_raid_event *ep, *tmpep;
663 sx_assert(&sc->sc_lock, SX_XLOCKED);
665 mtx_lock(&sc->sc_queue_mtx);
666 TAILQ_FOREACH_SAFE(ep, &sc->sc_events, e_next, tmpep) {
667 if (ep->e_tgt != tgt)
669 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
670 if ((ep->e_flags & G_RAID_EVENT_WAIT) == 0)
671 g_raid_event_free(ep);
673 ep->e_error = ECANCELED;
677 mtx_unlock(&sc->sc_queue_mtx);
681 g_raid_event_check(struct g_raid_softc *sc, void *tgt)
683 struct g_raid_event *ep;
686 sx_assert(&sc->sc_lock, SX_XLOCKED);
688 mtx_lock(&sc->sc_queue_mtx);
689 TAILQ_FOREACH(ep, &sc->sc_events, e_next) {
690 if (ep->e_tgt != tgt)
695 mtx_unlock(&sc->sc_queue_mtx);
700 * Return the number of disks in given state.
701 * If state is equal to -1, count all connected disks.
704 g_raid_ndisks(struct g_raid_softc *sc, int state)
706 struct g_raid_disk *disk;
709 sx_assert(&sc->sc_lock, SX_LOCKED);
712 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
713 if (disk->d_state == state || state == -1)
720 * Return the number of subdisks in given state.
721 * If state is equal to -1, count all connected disks.
724 g_raid_nsubdisks(struct g_raid_volume *vol, int state)
726 struct g_raid_subdisk *subdisk;
727 struct g_raid_softc *sc;
731 sx_assert(&sc->sc_lock, SX_LOCKED);
734 for (i = 0; i < vol->v_disks_count; i++) {
735 subdisk = &vol->v_subdisks[i];
737 subdisk->sd_state != G_RAID_SUBDISK_S_NONE) ||
738 subdisk->sd_state == state)
745 * Return the first subdisk in given state.
746 * If state is equal to -1, then the first connected disks.
748 struct g_raid_subdisk *
749 g_raid_get_subdisk(struct g_raid_volume *vol, int state)
751 struct g_raid_subdisk *sd;
752 struct g_raid_softc *sc;
756 sx_assert(&sc->sc_lock, SX_LOCKED);
758 for (i = 0; i < vol->v_disks_count; i++) {
759 sd = &vol->v_subdisks[i];
761 sd->sd_state != G_RAID_SUBDISK_S_NONE) ||
762 sd->sd_state == state)
769 g_raid_open_consumer(struct g_raid_softc *sc, const char *name)
771 struct g_consumer *cp;
772 struct g_provider *pp;
776 if (strncmp(name, "/dev/", 5) == 0)
778 pp = g_provider_by_name(name);
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);
787 if (g_access(cp, 1, 1, 1) != 0) {
789 g_destroy_consumer(cp);
796 g_raid_nrequests(struct g_raid_softc *sc, struct g_consumer *cp)
801 mtx_lock(&sc->sc_queue_mtx);
802 TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
803 if (bp->bio_from == cp)
806 mtx_unlock(&sc->sc_queue_mtx);
811 g_raid_nopens(struct g_raid_softc *sc)
813 struct g_raid_volume *vol;
817 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
818 if (vol->v_provider_open != 0)
825 g_raid_consumer_is_busy(struct g_raid_softc *sc, struct g_consumer *cp)
830 "I/O requests for %s exist, can't destroy it now.",
834 if (g_raid_nrequests(sc, cp) > 0) {
836 "I/O requests for %s in queue, can't destroy it now.",
844 g_raid_destroy_consumer(void *arg, int flags __unused)
846 struct g_consumer *cp;
851 G_RAID_DEBUG(1, "Consumer %s destroyed.", cp->provider->name);
853 g_destroy_consumer(cp);
857 g_raid_kill_consumer(struct g_raid_softc *sc, struct g_consumer *cp)
859 struct g_provider *pp;
862 g_topology_assert_not();
866 if (g_raid_consumer_is_busy(sc, cp))
871 if ((pp->geom->flags & G_GEOM_WITHER) == 0)
874 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
875 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
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.
886 g_post_event(g_raid_destroy_consumer, cp, M_WAITOK, NULL);
889 G_RAID_DEBUG(1, "Consumer %s destroyed.", pp->name);
891 g_destroy_consumer(cp);
897 g_raid_orphan(struct g_consumer *cp)
899 struct g_raid_disk *disk;
906 g_raid_event_send(disk, G_RAID_DISK_E_DISCONNECTED,
911 g_raid_clean(struct g_raid_volume *vol, int acw)
913 struct g_raid_softc *sc;
917 g_topology_assert_not();
918 sx_assert(&sc->sc_lock, SX_XLOCKED);
920 // if ((sc->sc_flags & G_RAID_DEVICE_FLAG_NOFAILSYNC) != 0)
924 if (vol->v_writes > 0)
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)
933 G_RAID_DEBUG1(1, sc, "Volume %s marked as clean.",
935 g_raid_write_metadata(sc, vol, NULL, NULL);
939 g_raid_dirty(struct g_raid_volume *vol)
941 struct g_raid_softc *sc;
944 g_topology_assert_not();
945 sx_assert(&sc->sc_lock, SX_XLOCKED);
947 // if ((sc->sc_flags & G_RAID_DEVICE_FLAG_NOFAILSYNC) != 0)
950 G_RAID_DEBUG1(1, sc, "Volume %s marked as dirty.",
952 g_raid_write_metadata(sc, vol, NULL, NULL);
956 g_raid_tr_flush_common(struct g_raid_tr_object *tr, struct bio *bp)
958 struct g_raid_volume *vol;
959 struct g_raid_subdisk *sd;
960 struct bio_queue_head queue;
964 vol = tr->tro_volume;
967 * Allocate all bios before sending any request, so we can return
968 * ENOMEM in nice and clean way.
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)
976 cbp = g_clone_bio(bp);
979 cbp->bio_caller1 = sd;
980 bioq_insert_tail(&queue, cbp);
982 while ((cbp = bioq_takefirst(&queue)) != NULL) {
983 sd = cbp->bio_caller1;
984 cbp->bio_caller1 = NULL;
985 g_raid_subdisk_iostart(sd, cbp);
989 while ((cbp = bioq_takefirst(&queue)) != NULL)
991 if (bp->bio_error == 0)
992 bp->bio_error = ENOMEM;
993 g_raid_iodone(bp, bp->bio_error);
997 g_raid_tr_kerneldump_common_done(struct bio *bp)
1000 bp->bio_flags |= BIO_DONE;
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)
1007 struct g_raid_softc *sc;
1008 struct g_raid_volume *vol;
1011 vol = tr->tro_volume;
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;
1024 while (!(bp.bio_flags & BIO_DONE)) {
1025 G_RAID_DEBUG1(4, sc, "Poll...");
1030 return (bp.bio_error != 0 ? EIO : 0);
1034 g_raid_dump(void *arg,
1035 void *virtual, vm_offset_t physical, off_t offset, size_t length)
1037 struct g_raid_volume *vol;
1040 vol = (struct g_raid_volume *)arg;
1041 G_RAID_DEBUG1(3, vol->v_softc, "Dumping at off %llu len %llu.",
1042 (long long unsigned)offset, (long long unsigned)length);
1044 error = G_RAID_TR_KERNELDUMP(vol->v_tr,
1045 virtual, physical, 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) {
1115 if (!strcmp(bp->bio_attribute, "GEOM::candelete"))
1116 g_raid_candelete(sc, bp);
1117 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
1118 g_raid_kerneldump(sc, bp);
1120 g_io_deliver(bp, EOPNOTSUPP);
1123 g_io_deliver(bp, EOPNOTSUPP);
1126 mtx_lock(&sc->sc_queue_mtx);
1127 bioq_insert_tail(&sc->sc_queue, bp);
1128 mtx_unlock(&sc->sc_queue_mtx);
1130 G_RAID_DEBUG1(4, sc, "Waking up %p.", sc);
1136 g_raid_bio_overlaps(const struct bio *bp, off_t lstart, off_t len)
1140 * (1) bp entirely below NO
1141 * (2) bp entirely above NO
1142 * (3) bp start below, but end in range YES
1143 * (4) bp entirely within YES
1144 * (5) bp starts within, ends above YES
1146 * lock range 10-19 (offset 10 length 10)
1147 * (1) 1-5: first if kicks it out
1148 * (2) 30-35: second if kicks it out
1149 * (3) 5-15: passes both ifs
1150 * (4) 12-14: passes both ifs
1151 * (5) 19-20: passes both
1153 off_t lend = lstart + len - 1;
1154 off_t bstart = bp->bio_offset;
1155 off_t bend = bp->bio_offset + bp->bio_length - 1;
1165 g_raid_is_in_locked_range(struct g_raid_volume *vol, const struct bio *bp)
1167 struct g_raid_lock *lp;
1169 sx_assert(&vol->v_softc->sc_lock, SX_LOCKED);
1171 LIST_FOREACH(lp, &vol->v_locks, l_next) {
1172 if (g_raid_bio_overlaps(bp, lp->l_offset, lp->l_length))
1179 g_raid_start_request(struct bio *bp)
1181 struct g_raid_softc *sc;
1182 struct g_raid_volume *vol;
1184 sc = bp->bio_to->geom->softc;
1185 sx_assert(&sc->sc_lock, SX_LOCKED);
1186 vol = bp->bio_to->private;
1189 * Check to see if this item is in a locked range. If so,
1190 * queue it to our locked queue and return. We'll requeue
1191 * it when the range is unlocked. Internal I/O for the
1192 * rebuild/rescan/recovery process is excluded from this
1193 * check so we can actually do the recovery.
1195 if (!(bp->bio_cflags & G_RAID_BIO_FLAG_SPECIAL) &&
1196 g_raid_is_in_locked_range(vol, bp)) {
1197 G_RAID_LOGREQ(3, bp, "Defer request.");
1198 bioq_insert_tail(&vol->v_locked, bp);
1203 * If we're actually going to do the write/delete, then
1204 * update the idle stats for the volume.
1206 if (bp->bio_cmd == BIO_WRITE || bp->bio_cmd == BIO_DELETE) {
1213 * Put request onto inflight queue, so we can check if new
1214 * synchronization requests don't collide with it. Then tell
1215 * the transformation layer to start the I/O.
1217 bioq_insert_tail(&vol->v_inflight, bp);
1218 G_RAID_LOGREQ(4, bp, "Request started");
1219 G_RAID_TR_IOSTART(vol->v_tr, bp);
1223 g_raid_finish_with_locked_ranges(struct g_raid_volume *vol, struct bio *bp)
1227 struct g_raid_lock *lp;
1229 vol->v_pending_lock = 0;
1230 LIST_FOREACH(lp, &vol->v_locks, l_next) {
1231 if (lp->l_pending) {
1235 TAILQ_FOREACH(nbp, &vol->v_inflight.queue, bio_queue) {
1236 if (g_raid_bio_overlaps(nbp, off, len))
1239 if (lp->l_pending) {
1240 vol->v_pending_lock = 1;
1241 G_RAID_DEBUG1(4, vol->v_softc,
1242 "Deferred lock(%jd, %jd) has %d pending",
1243 (intmax_t)off, (intmax_t)(off + len),
1247 G_RAID_DEBUG1(4, vol->v_softc,
1248 "Deferred lock of %jd to %jd completed",
1249 (intmax_t)off, (intmax_t)(off + len));
1250 G_RAID_TR_LOCKED(vol->v_tr, lp->l_callback_arg);
1256 g_raid_iodone(struct bio *bp, int error)
1258 struct g_raid_softc *sc;
1259 struct g_raid_volume *vol;
1261 sc = bp->bio_to->geom->softc;
1262 sx_assert(&sc->sc_lock, SX_LOCKED);
1263 vol = bp->bio_to->private;
1264 G_RAID_LOGREQ(3, bp, "Request done: %d.", error);
1266 /* Update stats if we done write/delete. */
1267 if (bp->bio_cmd == BIO_WRITE || bp->bio_cmd == BIO_DELETE) {
1269 vol->v_last_write = time_uptime;
1272 bioq_remove(&vol->v_inflight, bp);
1273 if (vol->v_pending_lock && g_raid_is_in_locked_range(vol, bp))
1274 g_raid_finish_with_locked_ranges(vol, bp);
1275 getmicrouptime(&vol->v_last_done);
1276 g_io_deliver(bp, error);
1280 g_raid_lock_range(struct g_raid_volume *vol, off_t off, off_t len,
1281 struct bio *ignore, void *argp)
1283 struct g_raid_softc *sc;
1284 struct g_raid_lock *lp;
1288 lp = malloc(sizeof(*lp), M_RAID, M_WAITOK | M_ZERO);
1289 LIST_INSERT_HEAD(&vol->v_locks, lp, l_next);
1292 lp->l_callback_arg = argp;
1295 TAILQ_FOREACH(bp, &vol->v_inflight.queue, bio_queue) {
1296 if (bp != ignore && g_raid_bio_overlaps(bp, off, len))
1301 * If there are any writes that are pending, we return EBUSY. All
1302 * callers will have to wait until all pending writes clear.
1304 if (lp->l_pending > 0) {
1305 vol->v_pending_lock = 1;
1306 G_RAID_DEBUG1(4, sc, "Locking range %jd to %jd deferred %d pend",
1307 (intmax_t)off, (intmax_t)(off+len), lp->l_pending);
1310 G_RAID_DEBUG1(4, sc, "Locking range %jd to %jd",
1311 (intmax_t)off, (intmax_t)(off+len));
1312 G_RAID_TR_LOCKED(vol->v_tr, lp->l_callback_arg);
1317 g_raid_unlock_range(struct g_raid_volume *vol, off_t off, off_t len)
1319 struct g_raid_lock *lp;
1320 struct g_raid_softc *sc;
1324 LIST_FOREACH(lp, &vol->v_locks, l_next) {
1325 if (lp->l_offset == off && lp->l_length == len) {
1326 LIST_REMOVE(lp, l_next);
1328 * Right now we just put them all back on the queue
1329 * and hope for the best. We hope this because any
1330 * locked ranges will go right back on this list
1331 * when the worker thread runs.
1334 G_RAID_DEBUG1(4, sc, "Unlocked %jd to %jd",
1335 (intmax_t)lp->l_offset,
1336 (intmax_t)(lp->l_offset+lp->l_length));
1337 mtx_lock(&sc->sc_queue_mtx);
1338 while ((bp = bioq_takefirst(&vol->v_locked)) != NULL)
1339 bioq_insert_tail(&sc->sc_queue, bp);
1340 mtx_unlock(&sc->sc_queue_mtx);
1349 g_raid_subdisk_iostart(struct g_raid_subdisk *sd, struct bio *bp)
1351 struct g_consumer *cp;
1352 struct g_raid_disk *disk, *tdisk;
1354 bp->bio_caller1 = sd;
1357 * Make sure that the disk is present. Generally it is a task of
1358 * transformation layers to not send requests to absent disks, but
1359 * it is better to be safe and report situation then sorry.
1361 if (sd->sd_disk == NULL) {
1362 G_RAID_LOGREQ(0, bp, "Warning! I/O request to an absent disk!");
1364 bp->bio_from = NULL;
1366 bp->bio_error = ENXIO;
1367 g_raid_disk_done(bp);
1371 if (disk->d_state != G_RAID_DISK_S_ACTIVE &&
1372 disk->d_state != G_RAID_DISK_S_FAILED) {
1373 G_RAID_LOGREQ(0, bp, "Warning! I/O request to a disk in a "
1374 "wrong state (%s)!", g_raid_disk_state2str(disk->d_state));
1378 cp = disk->d_consumer;
1380 bp->bio_to = cp->provider;
1383 /* Update average disks load. */
1384 TAILQ_FOREACH(tdisk, &sd->sd_softc->sc_disks, d_next) {
1385 if (tdisk->d_consumer == NULL)
1388 tdisk->d_load = (tdisk->d_consumer->index *
1389 G_RAID_SUBDISK_LOAD_SCALE + tdisk->d_load * 7) / 8;
1392 disk->d_last_offset = bp->bio_offset + bp->bio_length;
1394 G_RAID_LOGREQ(3, bp, "Sending dumping request.");
1395 if (bp->bio_cmd == BIO_WRITE) {
1396 bp->bio_error = g_raid_subdisk_kerneldump(sd,
1397 bp->bio_data, 0, bp->bio_offset, bp->bio_length);
1399 bp->bio_error = EOPNOTSUPP;
1400 g_raid_disk_done(bp);
1402 bp->bio_done = g_raid_disk_done;
1403 bp->bio_offset += sd->sd_offset;
1404 G_RAID_LOGREQ(3, bp, "Sending request.");
1405 g_io_request(bp, cp);
1410 g_raid_subdisk_kerneldump(struct g_raid_subdisk *sd,
1411 void *virtual, vm_offset_t physical, off_t offset, size_t length)
1414 if (sd->sd_disk == NULL)
1416 if (sd->sd_disk->d_kd.di.dumper == NULL)
1417 return (EOPNOTSUPP);
1418 return (dump_write(&sd->sd_disk->d_kd.di,
1420 sd->sd_disk->d_kd.di.mediaoffset + sd->sd_offset + offset,
1425 g_raid_disk_done(struct bio *bp)
1427 struct g_raid_softc *sc;
1428 struct g_raid_subdisk *sd;
1430 sd = bp->bio_caller1;
1432 mtx_lock(&sc->sc_queue_mtx);
1433 bioq_insert_tail(&sc->sc_queue, bp);
1434 mtx_unlock(&sc->sc_queue_mtx);
1440 g_raid_disk_done_request(struct bio *bp)
1442 struct g_raid_softc *sc;
1443 struct g_raid_disk *disk;
1444 struct g_raid_subdisk *sd;
1445 struct g_raid_volume *vol;
1447 g_topology_assert_not();
1449 G_RAID_LOGREQ(3, bp, "Disk request done: %d.", bp->bio_error);
1450 sd = bp->bio_caller1;
1452 vol = sd->sd_volume;
1453 if (bp->bio_from != NULL) {
1454 bp->bio_from->index--;
1455 disk = bp->bio_from->private;
1457 g_raid_kill_consumer(sc, bp->bio_from);
1459 bp->bio_offset -= sd->sd_offset;
1461 G_RAID_TR_IODONE(vol->v_tr, sd, bp);
1465 g_raid_handle_event(struct g_raid_softc *sc, struct g_raid_event *ep)
1468 if ((ep->e_flags & G_RAID_EVENT_VOLUME) != 0)
1469 ep->e_error = g_raid_update_volume(ep->e_tgt, ep->e_event);
1470 else if ((ep->e_flags & G_RAID_EVENT_DISK) != 0)
1471 ep->e_error = g_raid_update_disk(ep->e_tgt, ep->e_event);
1472 else if ((ep->e_flags & G_RAID_EVENT_SUBDISK) != 0)
1473 ep->e_error = g_raid_update_subdisk(ep->e_tgt, ep->e_event);
1475 ep->e_error = g_raid_update_node(ep->e_tgt, ep->e_event);
1476 if ((ep->e_flags & G_RAID_EVENT_WAIT) == 0) {
1477 KASSERT(ep->e_error == 0,
1478 ("Error cannot be handled."));
1479 g_raid_event_free(ep);
1481 ep->e_flags |= G_RAID_EVENT_DONE;
1482 G_RAID_DEBUG1(4, sc, "Waking up %p.", ep);
1483 mtx_lock(&sc->sc_queue_mtx);
1485 mtx_unlock(&sc->sc_queue_mtx);
1493 g_raid_worker(void *arg)
1495 struct g_raid_softc *sc;
1496 struct g_raid_event *ep;
1497 struct g_raid_volume *vol;
1499 struct timeval now, t;
1503 thread_lock(curthread);
1504 sched_prio(curthread, PRIBIO);
1505 thread_unlock(curthread);
1507 sx_xlock(&sc->sc_lock);
1509 mtx_lock(&sc->sc_queue_mtx);
1511 * First take a look at events.
1512 * This is important to handle events before any I/O requests.
1517 ep = TAILQ_FIRST(&sc->sc_events);
1519 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
1520 else if ((bp = bioq_takefirst(&sc->sc_queue)) != NULL)
1523 getmicrouptime(&now);
1525 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1526 if (bioq_first(&vol->v_inflight) == NULL &&
1528 timevalcmp(&vol->v_last_done, &t, < ))
1529 t = vol->v_last_done;
1531 timevalsub(&t, &now);
1532 timeout = g_raid_idle_threshold +
1533 t.tv_sec * 1000000 + t.tv_usec;
1536 * Two steps to avoid overflows at HZ=1000
1537 * and idle timeouts > 2.1s. Some rounding
1538 * errors can occur, but they are < 1tick,
1539 * which is deemed to be close enough for
1542 int micpertic = 1000000 / hz;
1543 timeout = (timeout + micpertic - 1) / micpertic;
1544 sx_xunlock(&sc->sc_lock);
1545 MSLEEP(rv, sc, &sc->sc_queue_mtx,
1546 PRIBIO | PDROP, "-", timeout);
1547 sx_xlock(&sc->sc_lock);
1552 mtx_unlock(&sc->sc_queue_mtx);
1555 g_raid_handle_event(sc, ep);
1556 } else if (bp != NULL) {
1557 if (bp->bio_to != NULL &&
1558 bp->bio_to->geom == sc->sc_geom)
1559 g_raid_start_request(bp);
1561 g_raid_disk_done_request(bp);
1562 } else if (rv == EWOULDBLOCK) {
1563 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1564 g_raid_clean(vol, -1);
1565 if (bioq_first(&vol->v_inflight) == NULL &&
1567 t.tv_sec = g_raid_idle_threshold / 1000000;
1568 t.tv_usec = g_raid_idle_threshold % 1000000;
1569 timevaladd(&t, &vol->v_last_done);
1570 getmicrouptime(&now);
1571 if (timevalcmp(&t, &now, <= )) {
1572 G_RAID_TR_IDLE(vol->v_tr);
1573 vol->v_last_done = now;
1578 if (sc->sc_stopping == G_RAID_DESTROY_HARD)
1579 g_raid_destroy_node(sc, 1); /* May not return. */
1584 g_raid_poll(struct g_raid_softc *sc)
1586 struct g_raid_event *ep;
1589 sx_xlock(&sc->sc_lock);
1590 mtx_lock(&sc->sc_queue_mtx);
1592 * First take a look at events.
1593 * This is important to handle events before any I/O requests.
1595 ep = TAILQ_FIRST(&sc->sc_events);
1597 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
1598 mtx_unlock(&sc->sc_queue_mtx);
1599 g_raid_handle_event(sc, ep);
1602 bp = bioq_takefirst(&sc->sc_queue);
1604 mtx_unlock(&sc->sc_queue_mtx);
1605 if (bp->bio_from == NULL ||
1606 bp->bio_from->geom != sc->sc_geom)
1607 g_raid_start_request(bp);
1609 g_raid_disk_done_request(bp);
1612 sx_xunlock(&sc->sc_lock);
1616 g_raid_launch_provider(struct g_raid_volume *vol)
1618 struct g_raid_disk *disk;
1619 struct g_raid_subdisk *sd;
1620 struct g_raid_softc *sc;
1621 struct g_provider *pp;
1622 char name[G_RAID_MAX_VOLUMENAME];
1627 sx_assert(&sc->sc_lock, SX_LOCKED);
1630 /* Try to name provider with volume name. */
1631 snprintf(name, sizeof(name), "raid/%s", vol->v_name);
1632 if (g_raid_name_format == 0 || vol->v_name[0] == 0 ||
1633 g_provider_by_name(name) != NULL) {
1634 /* Otherwise use sequential volume number. */
1635 snprintf(name, sizeof(name), "raid/r%d", vol->v_global_id);
1638 pp = g_new_providerf(sc->sc_geom, "%s", name);
1639 pp->flags |= G_PF_DIRECT_RECEIVE;
1640 if (vol->v_tr->tro_class->trc_accept_unmapped) {
1641 pp->flags |= G_PF_ACCEPT_UNMAPPED;
1642 for (i = 0; i < vol->v_disks_count; i++) {
1643 sd = &vol->v_subdisks[i];
1644 if (sd->sd_state == G_RAID_SUBDISK_S_NONE)
1646 if ((sd->sd_disk->d_consumer->provider->flags &
1647 G_PF_ACCEPT_UNMAPPED) == 0)
1648 pp->flags &= ~G_PF_ACCEPT_UNMAPPED;
1652 pp->mediasize = vol->v_mediasize;
1653 pp->sectorsize = vol->v_sectorsize;
1655 pp->stripeoffset = 0;
1656 if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1 ||
1657 vol->v_raid_level == G_RAID_VOLUME_RL_RAID3 ||
1658 vol->v_raid_level == G_RAID_VOLUME_RL_SINGLE ||
1659 vol->v_raid_level == G_RAID_VOLUME_RL_CONCAT) {
1660 if ((disk = vol->v_subdisks[0].sd_disk) != NULL &&
1661 disk->d_consumer != NULL &&
1662 disk->d_consumer->provider != NULL) {
1663 pp->stripesize = disk->d_consumer->provider->stripesize;
1664 off = disk->d_consumer->provider->stripeoffset;
1665 pp->stripeoffset = off + vol->v_subdisks[0].sd_offset;
1667 pp->stripeoffset %= off;
1669 if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID3) {
1670 pp->stripesize *= (vol->v_disks_count - 1);
1671 pp->stripeoffset *= (vol->v_disks_count - 1);
1674 pp->stripesize = vol->v_strip_size;
1675 vol->v_provider = pp;
1676 g_error_provider(pp, 0);
1677 g_topology_unlock();
1678 G_RAID_DEBUG1(0, sc, "Provider %s for volume %s created.",
1679 pp->name, vol->v_name);
1683 g_raid_destroy_provider(struct g_raid_volume *vol)
1685 struct g_raid_softc *sc;
1686 struct g_provider *pp;
1687 struct bio *bp, *tmp;
1689 g_topology_assert_not();
1691 pp = vol->v_provider;
1692 KASSERT(pp != NULL, ("NULL provider (volume=%s).", vol->v_name));
1695 g_error_provider(pp, ENXIO);
1696 mtx_lock(&sc->sc_queue_mtx);
1697 TAILQ_FOREACH_SAFE(bp, &sc->sc_queue.queue, bio_queue, tmp) {
1698 if (bp->bio_to != pp)
1700 bioq_remove(&sc->sc_queue, bp);
1701 g_io_deliver(bp, ENXIO);
1703 mtx_unlock(&sc->sc_queue_mtx);
1704 G_RAID_DEBUG1(0, sc, "Provider %s for volume %s destroyed.",
1705 pp->name, vol->v_name);
1706 g_wither_provider(pp, ENXIO);
1707 g_topology_unlock();
1708 vol->v_provider = NULL;
1712 * Update device state.
1715 g_raid_update_volume(struct g_raid_volume *vol, u_int event)
1717 struct g_raid_softc *sc;
1720 sx_assert(&sc->sc_lock, SX_XLOCKED);
1722 G_RAID_DEBUG1(2, sc, "Event %s for volume %s.",
1723 g_raid_volume_event2str(event),
1726 case G_RAID_VOLUME_E_DOWN:
1727 if (vol->v_provider != NULL)
1728 g_raid_destroy_provider(vol);
1730 case G_RAID_VOLUME_E_UP:
1731 if (vol->v_provider == NULL)
1732 g_raid_launch_provider(vol);
1734 case G_RAID_VOLUME_E_START:
1736 G_RAID_TR_START(vol->v_tr);
1740 G_RAID_MD_VOLUME_EVENT(sc->sc_md, vol, event);
1744 /* Manage root mount release. */
1745 if (vol->v_starting) {
1746 vol->v_starting = 0;
1747 G_RAID_DEBUG1(1, sc, "root_mount_rel %p", vol->v_rootmount);
1748 root_mount_rel(vol->v_rootmount);
1749 vol->v_rootmount = NULL;
1751 if (vol->v_stopping && vol->v_provider_open == 0)
1752 g_raid_destroy_volume(vol);
1757 * Update subdisk state.
1760 g_raid_update_subdisk(struct g_raid_subdisk *sd, u_int event)
1762 struct g_raid_softc *sc;
1763 struct g_raid_volume *vol;
1766 vol = sd->sd_volume;
1767 sx_assert(&sc->sc_lock, SX_XLOCKED);
1769 G_RAID_DEBUG1(2, sc, "Event %s for subdisk %s:%d-%s.",
1770 g_raid_subdisk_event2str(event),
1771 vol->v_name, sd->sd_pos,
1772 sd->sd_disk ? g_raid_get_diskname(sd->sd_disk) : "[none]");
1774 G_RAID_TR_EVENT(vol->v_tr, sd, event);
1780 * Update disk state.
1783 g_raid_update_disk(struct g_raid_disk *disk, u_int event)
1785 struct g_raid_softc *sc;
1788 sx_assert(&sc->sc_lock, SX_XLOCKED);
1790 G_RAID_DEBUG1(2, sc, "Event %s for disk %s.",
1791 g_raid_disk_event2str(event),
1792 g_raid_get_diskname(disk));
1795 G_RAID_MD_EVENT(sc->sc_md, disk, event);
1803 g_raid_update_node(struct g_raid_softc *sc, u_int event)
1805 sx_assert(&sc->sc_lock, SX_XLOCKED);
1807 G_RAID_DEBUG1(2, sc, "Event %s for the array.",
1808 g_raid_node_event2str(event));
1810 if (event == G_RAID_NODE_E_WAKE)
1813 G_RAID_MD_EVENT(sc->sc_md, NULL, event);
1818 g_raid_access(struct g_provider *pp, int acr, int acw, int ace)
1820 struct g_raid_volume *vol;
1821 struct g_raid_softc *sc;
1822 int dcw, opens, error = 0;
1824 g_topology_assert();
1825 sc = pp->geom->softc;
1827 KASSERT(sc != NULL, ("NULL softc (provider=%s).", pp->name));
1828 KASSERT(vol != NULL, ("NULL volume (provider=%s).", pp->name));
1830 G_RAID_DEBUG1(2, sc, "Access request for %s: r%dw%de%d.", pp->name,
1832 dcw = pp->acw + acw;
1834 g_topology_unlock();
1835 sx_xlock(&sc->sc_lock);
1836 /* Deny new opens while dying. */
1837 if (sc->sc_stopping != 0 && (acr > 0 || acw > 0 || ace > 0)) {
1841 /* Deny write opens for read-only volumes. */
1842 if (vol->v_read_only && acw > 0) {
1847 g_raid_clean(vol, dcw);
1848 vol->v_provider_open += acr + acw + ace;
1849 /* Handle delayed node destruction. */
1850 if (sc->sc_stopping == G_RAID_DESTROY_DELAYED &&
1851 vol->v_provider_open == 0) {
1852 /* Count open volumes. */
1853 opens = g_raid_nopens(sc);
1855 sc->sc_stopping = G_RAID_DESTROY_HARD;
1856 /* Wake up worker to make it selfdestruct. */
1857 g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
1860 /* Handle open volume destruction. */
1861 if (vol->v_stopping && vol->v_provider_open == 0)
1862 g_raid_destroy_volume(vol);
1864 sx_xunlock(&sc->sc_lock);
1869 struct g_raid_softc *
1870 g_raid_create_node(struct g_class *mp,
1871 const char *name, struct g_raid_md_object *md)
1873 struct g_raid_softc *sc;
1877 g_topology_assert();
1878 G_RAID_DEBUG(1, "Creating array %s.", name);
1880 gp = g_new_geomf(mp, "%s", name);
1881 sc = malloc(sizeof(*sc), M_RAID, M_WAITOK | M_ZERO);
1882 gp->start = g_raid_start;
1883 gp->orphan = g_raid_orphan;
1884 gp->access = g_raid_access;
1885 gp->dumpconf = g_raid_dumpconf;
1890 TAILQ_INIT(&sc->sc_volumes);
1891 TAILQ_INIT(&sc->sc_disks);
1892 sx_init(&sc->sc_lock, "graid:lock");
1893 mtx_init(&sc->sc_queue_mtx, "graid:queue", NULL, MTX_DEF);
1894 TAILQ_INIT(&sc->sc_events);
1895 bioq_init(&sc->sc_queue);
1897 error = kproc_create(g_raid_worker, sc, &sc->sc_worker, 0, 0,
1900 G_RAID_DEBUG(0, "Cannot create kernel thread for %s.", name);
1901 mtx_destroy(&sc->sc_queue_mtx);
1902 sx_destroy(&sc->sc_lock);
1903 g_destroy_geom(sc->sc_geom);
1908 G_RAID_DEBUG1(0, sc, "Array %s created.", name);
1912 struct g_raid_volume *
1913 g_raid_create_volume(struct g_raid_softc *sc, const char *name, int id)
1915 struct g_raid_volume *vol, *vol1;
1918 G_RAID_DEBUG1(1, sc, "Creating volume %s.", name);
1919 vol = malloc(sizeof(*vol), M_RAID, M_WAITOK | M_ZERO);
1921 strlcpy(vol->v_name, name, G_RAID_MAX_VOLUMENAME);
1922 vol->v_state = G_RAID_VOLUME_S_STARTING;
1923 vol->v_raid_level = G_RAID_VOLUME_RL_UNKNOWN;
1924 vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_UNKNOWN;
1925 vol->v_rotate_parity = 1;
1926 bioq_init(&vol->v_inflight);
1927 bioq_init(&vol->v_locked);
1928 LIST_INIT(&vol->v_locks);
1929 for (i = 0; i < G_RAID_MAX_SUBDISKS; i++) {
1930 vol->v_subdisks[i].sd_softc = sc;
1931 vol->v_subdisks[i].sd_volume = vol;
1932 vol->v_subdisks[i].sd_pos = i;
1933 vol->v_subdisks[i].sd_state = G_RAID_DISK_S_NONE;
1936 /* Find free ID for this volume. */
1940 LIST_FOREACH(vol1, &g_raid_volumes, v_global_next) {
1941 if (vol1->v_global_id == id)
1946 for (id = 0; ; id++) {
1947 LIST_FOREACH(vol1, &g_raid_volumes, v_global_next) {
1948 if (vol1->v_global_id == id)
1955 vol->v_global_id = id;
1956 LIST_INSERT_HEAD(&g_raid_volumes, vol, v_global_next);
1957 g_topology_unlock();
1959 /* Delay root mounting. */
1960 vol->v_rootmount = root_mount_hold("GRAID");
1961 G_RAID_DEBUG1(1, sc, "root_mount_hold %p", vol->v_rootmount);
1962 vol->v_starting = 1;
1963 TAILQ_INSERT_TAIL(&sc->sc_volumes, vol, v_next);
1967 struct g_raid_disk *
1968 g_raid_create_disk(struct g_raid_softc *sc)
1970 struct g_raid_disk *disk;
1972 G_RAID_DEBUG1(1, sc, "Creating disk.");
1973 disk = malloc(sizeof(*disk), M_RAID, M_WAITOK | M_ZERO);
1975 disk->d_state = G_RAID_DISK_S_NONE;
1976 TAILQ_INIT(&disk->d_subdisks);
1977 TAILQ_INSERT_TAIL(&sc->sc_disks, disk, d_next);
1981 int g_raid_start_volume(struct g_raid_volume *vol)
1983 struct g_raid_tr_class *class;
1984 struct g_raid_tr_object *obj;
1987 G_RAID_DEBUG1(2, vol->v_softc, "Starting volume %s.", vol->v_name);
1988 LIST_FOREACH(class, &g_raid_tr_classes, trc_list) {
1989 if (!class->trc_enable)
1991 G_RAID_DEBUG1(2, vol->v_softc,
1992 "Tasting volume %s for %s transformation.",
1993 vol->v_name, class->name);
1994 obj = (void *)kobj_create((kobj_class_t)class, M_RAID,
1996 obj->tro_class = class;
1997 obj->tro_volume = vol;
1998 status = G_RAID_TR_TASTE(obj, vol);
1999 if (status != G_RAID_TR_TASTE_FAIL)
2001 kobj_delete((kobj_t)obj, M_RAID);
2003 if (class == NULL) {
2004 G_RAID_DEBUG1(0, vol->v_softc,
2005 "No transformation module found for %s.",
2008 g_raid_change_volume_state(vol, G_RAID_VOLUME_S_UNSUPPORTED);
2009 g_raid_event_send(vol, G_RAID_VOLUME_E_DOWN,
2010 G_RAID_EVENT_VOLUME);
2013 G_RAID_DEBUG1(2, vol->v_softc,
2014 "Transformation module %s chosen for %s.",
2015 class->name, vol->v_name);
2021 g_raid_destroy_node(struct g_raid_softc *sc, int worker)
2023 struct g_raid_volume *vol, *tmpv;
2024 struct g_raid_disk *disk, *tmpd;
2027 sc->sc_stopping = G_RAID_DESTROY_HARD;
2028 TAILQ_FOREACH_SAFE(vol, &sc->sc_volumes, v_next, tmpv) {
2029 if (g_raid_destroy_volume(vol))
2034 TAILQ_FOREACH_SAFE(disk, &sc->sc_disks, d_next, tmpd) {
2035 if (g_raid_destroy_disk(disk))
2041 G_RAID_MD_FREE(sc->sc_md);
2042 kobj_delete((kobj_t)sc->sc_md, M_RAID);
2045 if (sc->sc_geom != NULL) {
2046 G_RAID_DEBUG1(0, sc, "Array %s destroyed.", sc->sc_name);
2048 sc->sc_geom->softc = NULL;
2049 g_wither_geom(sc->sc_geom, ENXIO);
2050 g_topology_unlock();
2053 G_RAID_DEBUG(1, "Array destroyed.");
2055 g_raid_event_cancel(sc, sc);
2056 mtx_destroy(&sc->sc_queue_mtx);
2057 sx_xunlock(&sc->sc_lock);
2058 sx_destroy(&sc->sc_lock);
2059 wakeup(&sc->sc_stopping);
2061 curthread->td_pflags &= ~TDP_GEOM;
2062 G_RAID_DEBUG(1, "Thread exiting.");
2065 /* Wake up worker to make it selfdestruct. */
2066 g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
2072 g_raid_destroy_volume(struct g_raid_volume *vol)
2074 struct g_raid_softc *sc;
2075 struct g_raid_disk *disk;
2079 G_RAID_DEBUG1(2, sc, "Destroying volume %s.", vol->v_name);
2080 vol->v_stopping = 1;
2081 if (vol->v_state != G_RAID_VOLUME_S_STOPPED) {
2083 G_RAID_TR_STOP(vol->v_tr);
2086 vol->v_state = G_RAID_VOLUME_S_STOPPED;
2088 if (g_raid_event_check(sc, vol) != 0)
2090 if (vol->v_provider != NULL)
2092 if (vol->v_provider_open != 0)
2095 G_RAID_TR_FREE(vol->v_tr);
2096 kobj_delete((kobj_t)vol->v_tr, M_RAID);
2099 if (vol->v_rootmount)
2100 root_mount_rel(vol->v_rootmount);
2102 LIST_REMOVE(vol, v_global_next);
2103 g_topology_unlock();
2104 TAILQ_REMOVE(&sc->sc_volumes, vol, v_next);
2105 for (i = 0; i < G_RAID_MAX_SUBDISKS; i++) {
2106 g_raid_event_cancel(sc, &vol->v_subdisks[i]);
2107 disk = vol->v_subdisks[i].sd_disk;
2110 TAILQ_REMOVE(&disk->d_subdisks, &vol->v_subdisks[i], sd_next);
2112 G_RAID_DEBUG1(2, sc, "Volume %s destroyed.", vol->v_name);
2114 G_RAID_MD_FREE_VOLUME(sc->sc_md, vol);
2115 g_raid_event_cancel(sc, vol);
2117 if (sc->sc_stopping == G_RAID_DESTROY_HARD) {
2118 /* Wake up worker to let it selfdestruct. */
2119 g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
2125 g_raid_destroy_disk(struct g_raid_disk *disk)
2127 struct g_raid_softc *sc;
2128 struct g_raid_subdisk *sd, *tmp;
2131 G_RAID_DEBUG1(2, sc, "Destroying disk.");
2132 if (disk->d_consumer) {
2133 g_raid_kill_consumer(sc, disk->d_consumer);
2134 disk->d_consumer = NULL;
2136 TAILQ_FOREACH_SAFE(sd, &disk->d_subdisks, sd_next, tmp) {
2137 g_raid_change_subdisk_state(sd, G_RAID_SUBDISK_S_NONE);
2138 g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED,
2139 G_RAID_EVENT_SUBDISK);
2140 TAILQ_REMOVE(&disk->d_subdisks, sd, sd_next);
2143 TAILQ_REMOVE(&sc->sc_disks, disk, d_next);
2145 G_RAID_MD_FREE_DISK(sc->sc_md, disk);
2146 g_raid_event_cancel(sc, disk);
2152 g_raid_destroy(struct g_raid_softc *sc, int how)
2156 g_topology_assert_not();
2159 sx_assert(&sc->sc_lock, SX_XLOCKED);
2161 /* Count open volumes. */
2162 opens = g_raid_nopens(sc);
2164 /* React on some opened volumes. */
2167 case G_RAID_DESTROY_SOFT:
2168 G_RAID_DEBUG1(1, sc,
2169 "%d volumes are still open.",
2171 sx_xunlock(&sc->sc_lock);
2173 case G_RAID_DESTROY_DELAYED:
2174 G_RAID_DEBUG1(1, sc,
2175 "Array will be destroyed on last close.");
2176 sc->sc_stopping = G_RAID_DESTROY_DELAYED;
2177 sx_xunlock(&sc->sc_lock);
2179 case G_RAID_DESTROY_HARD:
2180 G_RAID_DEBUG1(1, sc,
2181 "%d volumes are still open.",
2186 /* Mark node for destruction. */
2187 sc->sc_stopping = G_RAID_DESTROY_HARD;
2188 /* Wake up worker to let it selfdestruct. */
2189 g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
2190 /* Sleep until node destroyed. */
2191 error = sx_sleep(&sc->sc_stopping, &sc->sc_lock,
2192 PRIBIO | PDROP, "r:destroy", hz * 3);
2193 return (error == EWOULDBLOCK ? EBUSY : 0);
2197 g_raid_taste_orphan(struct g_consumer *cp)
2200 KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
2201 cp->provider->name));
2204 static struct g_geom *
2205 g_raid_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
2207 struct g_consumer *cp;
2208 struct g_geom *gp, *geom;
2209 struct g_raid_md_class *class;
2210 struct g_raid_md_object *obj;
2213 g_topology_assert();
2214 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
2217 G_RAID_DEBUG(2, "Tasting provider %s.", pp->name);
2220 status = G_RAID_MD_TASTE_FAIL;
2221 gp = g_new_geomf(mp, "raid:taste");
2223 * This orphan function should be never called.
2225 gp->orphan = g_raid_taste_orphan;
2226 cp = g_new_consumer(gp);
2227 cp->flags |= G_CF_DIRECT_RECEIVE;
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);
2251 g_destroy_consumer(cp);
2253 G_RAID_DEBUG(2, "Tasting provider %s done.", pp->name);
2258 g_raid_create_node_format(const char *format, struct gctl_req *req,
2261 struct g_raid_md_class *class;
2262 struct g_raid_md_object *obj;
2265 G_RAID_DEBUG(2, "Creating array for %s metadata.", format);
2266 LIST_FOREACH(class, &g_raid_md_classes, mdc_list) {
2267 if (strcasecmp(class->name, format) == 0)
2270 if (class == NULL) {
2271 G_RAID_DEBUG(1, "No support for %s metadata.", format);
2272 return (G_RAID_MD_TASTE_FAIL);
2274 obj = (void *)kobj_create((kobj_class_t)class, M_RAID,
2276 obj->mdo_class = class;
2277 status = G_RAID_MD_CREATE_REQ(obj, &g_raid_class, req, gp);
2278 if (status != G_RAID_MD_TASTE_NEW)
2279 kobj_delete((kobj_t)obj, M_RAID);
2284 g_raid_destroy_geom(struct gctl_req *req __unused,
2285 struct g_class *mp __unused, struct g_geom *gp)
2287 struct g_raid_softc *sc;
2290 g_topology_unlock();
2292 sx_xlock(&sc->sc_lock);
2294 error = g_raid_destroy(gp->softc, G_RAID_DESTROY_SOFT);
2299 void g_raid_write_metadata(struct g_raid_softc *sc, struct g_raid_volume *vol,
2300 struct g_raid_subdisk *sd, struct g_raid_disk *disk)
2303 if (sc->sc_stopping == G_RAID_DESTROY_HARD)
2306 G_RAID_MD_WRITE(sc->sc_md, vol, sd, disk);
2309 void g_raid_fail_disk(struct g_raid_softc *sc,
2310 struct g_raid_subdisk *sd, struct g_raid_disk *disk)
2316 G_RAID_DEBUG1(0, sc, "Warning! Fail request to an absent disk!");
2319 if (disk->d_state != G_RAID_DISK_S_ACTIVE) {
2320 G_RAID_DEBUG1(0, sc, "Warning! Fail request to a disk in a "
2321 "wrong state (%s)!", g_raid_disk_state2str(disk->d_state));
2325 G_RAID_MD_FAIL_DISK(sc->sc_md, sd, disk);
2329 g_raid_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
2330 struct g_consumer *cp, struct g_provider *pp)
2332 struct g_raid_softc *sc;
2333 struct g_raid_volume *vol;
2334 struct g_raid_subdisk *sd;
2335 struct g_raid_disk *disk;
2338 g_topology_assert();
2345 g_topology_unlock();
2346 sx_xlock(&sc->sc_lock);
2347 sbuf_printf(sb, "%s<descr>%s %s volume</descr>\n", indent,
2348 sc->sc_md->mdo_class->name,
2349 g_raid_volume_level2str(vol->v_raid_level,
2350 vol->v_raid_level_qualifier));
2351 sbuf_printf(sb, "%s<Label>%s</Label>\n", indent,
2353 sbuf_printf(sb, "%s<RAIDLevel>%s</RAIDLevel>\n", indent,
2354 g_raid_volume_level2str(vol->v_raid_level,
2355 vol->v_raid_level_qualifier));
2357 "%s<Transformation>%s</Transformation>\n", indent,
2358 vol->v_tr ? vol->v_tr->tro_class->name : "NONE");
2359 sbuf_printf(sb, "%s<Components>%u</Components>\n", indent,
2360 vol->v_disks_count);
2361 sbuf_printf(sb, "%s<Strip>%u</Strip>\n", indent,
2363 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
2364 g_raid_volume_state2str(vol->v_state));
2365 sbuf_printf(sb, "%s<Dirty>%s</Dirty>\n", indent,
2366 vol->v_dirty ? "Yes" : "No");
2367 sbuf_printf(sb, "%s<Subdisks>", indent);
2368 for (i = 0; i < vol->v_disks_count; i++) {
2369 sd = &vol->v_subdisks[i];
2370 if (sd->sd_disk != NULL &&
2371 sd->sd_disk->d_consumer != NULL) {
2372 sbuf_printf(sb, "%s ",
2373 g_raid_get_diskname(sd->sd_disk));
2375 sbuf_printf(sb, "NONE ");
2377 sbuf_printf(sb, "(%s",
2378 g_raid_subdisk_state2str(sd->sd_state));
2379 if (sd->sd_state == G_RAID_SUBDISK_S_REBUILD ||
2380 sd->sd_state == G_RAID_SUBDISK_S_RESYNC) {
2381 sbuf_printf(sb, " %d%%",
2382 (int)(sd->sd_rebuild_pos * 100 /
2385 sbuf_printf(sb, ")");
2386 if (i + 1 < vol->v_disks_count)
2387 sbuf_printf(sb, ", ");
2389 sbuf_printf(sb, "</Subdisks>\n");
2390 sx_xunlock(&sc->sc_lock);
2392 } else if (cp != NULL) {
2396 g_topology_unlock();
2397 sx_xlock(&sc->sc_lock);
2398 sbuf_printf(sb, "%s<State>%s", indent,
2399 g_raid_disk_state2str(disk->d_state));
2400 if (!TAILQ_EMPTY(&disk->d_subdisks)) {
2401 sbuf_printf(sb, " (");
2402 TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
2403 sbuf_printf(sb, "%s",
2404 g_raid_subdisk_state2str(sd->sd_state));
2405 if (sd->sd_state == G_RAID_SUBDISK_S_REBUILD ||
2406 sd->sd_state == G_RAID_SUBDISK_S_RESYNC) {
2407 sbuf_printf(sb, " %d%%",
2408 (int)(sd->sd_rebuild_pos * 100 /
2411 if (TAILQ_NEXT(sd, sd_next))
2412 sbuf_printf(sb, ", ");
2414 sbuf_printf(sb, ")");
2416 sbuf_printf(sb, "</State>\n");
2417 sbuf_printf(sb, "%s<Subdisks>", indent);
2418 TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
2419 sbuf_printf(sb, "r%d(%s):%d@%ju",
2420 sd->sd_volume->v_global_id,
2421 sd->sd_volume->v_name,
2422 sd->sd_pos, (uintmax_t)sd->sd_offset);
2423 if (TAILQ_NEXT(sd, sd_next))
2424 sbuf_printf(sb, ", ");
2426 sbuf_printf(sb, "</Subdisks>\n");
2427 sbuf_printf(sb, "%s<ReadErrors>%d</ReadErrors>\n", indent,
2429 sx_xunlock(&sc->sc_lock);
2432 g_topology_unlock();
2433 sx_xlock(&sc->sc_lock);
2435 sbuf_printf(sb, "%s<Metadata>%s</Metadata>\n", indent,
2436 sc->sc_md->mdo_class->name);
2438 if (!TAILQ_EMPTY(&sc->sc_volumes)) {
2440 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2441 if (vol->v_state < s)
2444 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
2445 g_raid_volume_state2str(s));
2447 sx_xunlock(&sc->sc_lock);
2453 g_raid_shutdown_post_sync(void *arg, int howto)
2456 struct g_geom *gp, *gp2;
2457 struct g_raid_softc *sc;
2458 struct g_raid_volume *vol;
2462 g_raid_shutdown = 1;
2463 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
2464 if ((sc = gp->softc) == NULL)
2466 g_topology_unlock();
2467 sx_xlock(&sc->sc_lock);
2468 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next)
2469 g_raid_clean(vol, -1);
2471 g_raid_destroy(sc, G_RAID_DESTROY_DELAYED);
2474 g_topology_unlock();
2478 g_raid_init(struct g_class *mp)
2481 g_raid_post_sync = EVENTHANDLER_REGISTER(shutdown_post_sync,
2482 g_raid_shutdown_post_sync, mp, SHUTDOWN_PRI_FIRST);
2483 if (g_raid_post_sync == NULL)
2484 G_RAID_DEBUG(0, "Warning! Cannot register shutdown event.");
2489 g_raid_fini(struct g_class *mp)
2492 if (g_raid_post_sync != NULL)
2493 EVENTHANDLER_DEREGISTER(shutdown_post_sync, g_raid_post_sync);
2498 g_raid_md_modevent(module_t mod, int type, void *arg)
2500 struct g_raid_md_class *class, *c, *nc;
2507 c = LIST_FIRST(&g_raid_md_classes);
2508 if (c == NULL || c->mdc_priority > class->mdc_priority)
2509 LIST_INSERT_HEAD(&g_raid_md_classes, class, mdc_list);
2511 while ((nc = LIST_NEXT(c, mdc_list)) != NULL &&
2512 nc->mdc_priority < class->mdc_priority)
2514 LIST_INSERT_AFTER(c, class, mdc_list);
2517 g_retaste(&g_raid_class);
2520 LIST_REMOVE(class, mdc_list);
2531 g_raid_tr_modevent(module_t mod, int type, void *arg)
2533 struct g_raid_tr_class *class, *c, *nc;
2540 c = LIST_FIRST(&g_raid_tr_classes);
2541 if (c == NULL || c->trc_priority > class->trc_priority)
2542 LIST_INSERT_HEAD(&g_raid_tr_classes, class, trc_list);
2544 while ((nc = LIST_NEXT(c, trc_list)) != NULL &&
2545 nc->trc_priority < class->trc_priority)
2547 LIST_INSERT_AFTER(c, class, trc_list);
2551 LIST_REMOVE(class, trc_list);
2562 * Use local implementation of DECLARE_GEOM_CLASS(g_raid_class, g_raid)
2563 * to reduce module priority, allowing submodules to register them first.
2565 static moduledata_t g_raid_mod = {
2570 DECLARE_MODULE(g_raid, g_raid_mod, SI_SUB_DRIVERS, SI_ORDER_THIRD);
2571 MODULE_VERSION(geom_raid, 0);