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, 0, "GEOM_RAID stuff");
58 int g_raid_enable = 1;
59 SYSCTL_INT(_kern_geom_raid, OID_AUTO, enable, CTLFLAG_RWTUN,
60 &g_raid_enable, 0, "Enable on-disk metadata taste");
61 u_int g_raid_aggressive_spare = 0;
62 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, aggressive_spare, CTLFLAG_RWTUN,
63 &g_raid_aggressive_spare, 0, "Use disks without metadata as spare");
64 u_int g_raid_debug = 0;
65 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, debug, CTLFLAG_RWTUN, &g_raid_debug, 0,
67 int g_raid_read_err_thresh = 10;
68 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, read_err_thresh, CTLFLAG_RWTUN,
69 &g_raid_read_err_thresh, 0,
70 "Number of read errors equated to disk failure");
71 u_int g_raid_start_timeout = 30;
72 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, start_timeout, CTLFLAG_RWTUN,
73 &g_raid_start_timeout, 0,
74 "Time to wait for all array components");
75 static u_int g_raid_clean_time = 5;
76 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, clean_time, CTLFLAG_RWTUN,
77 &g_raid_clean_time, 0, "Mark volume as clean when idling");
78 static u_int g_raid_disconnect_on_failure = 1;
79 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, disconnect_on_failure, CTLFLAG_RWTUN,
80 &g_raid_disconnect_on_failure, 0, "Disconnect component on I/O failure.");
81 static u_int g_raid_name_format = 0;
82 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, name_format, CTLFLAG_RWTUN,
83 &g_raid_name_format, 0, "Providers name format.");
84 static u_int g_raid_idle_threshold = 1000000;
85 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, idle_threshold, CTLFLAG_RWTUN,
86 &g_raid_idle_threshold, 1000000,
87 "Time in microseconds to consider a volume idle.");
89 #define MSLEEP(rv, ident, mtx, priority, wmesg, timeout) do { \
90 G_RAID_DEBUG(4, "%s: Sleeping %p.", __func__, (ident)); \
91 rv = msleep((ident), (mtx), (priority), (wmesg), (timeout)); \
92 G_RAID_DEBUG(4, "%s: Woken up %p.", __func__, (ident)); \
95 LIST_HEAD(, g_raid_md_class) g_raid_md_classes =
96 LIST_HEAD_INITIALIZER(g_raid_md_classes);
98 LIST_HEAD(, g_raid_tr_class) g_raid_tr_classes =
99 LIST_HEAD_INITIALIZER(g_raid_tr_classes);
101 LIST_HEAD(, g_raid_volume) g_raid_volumes =
102 LIST_HEAD_INITIALIZER(g_raid_volumes);
104 static eventhandler_tag g_raid_post_sync = NULL;
105 static int g_raid_started = 0;
106 static int g_raid_shutdown = 0;
108 static int g_raid_destroy_geom(struct gctl_req *req, struct g_class *mp,
110 static g_taste_t g_raid_taste;
111 static void g_raid_init(struct g_class *mp);
112 static void g_raid_fini(struct g_class *mp);
114 struct g_class g_raid_class = {
115 .name = G_RAID_CLASS_NAME,
116 .version = G_VERSION,
117 .ctlreq = g_raid_ctl,
118 .taste = g_raid_taste,
119 .destroy_geom = g_raid_destroy_geom,
124 static void g_raid_destroy_provider(struct g_raid_volume *vol);
125 static int g_raid_update_disk(struct g_raid_disk *disk, u_int event);
126 static int g_raid_update_subdisk(struct g_raid_subdisk *subdisk, u_int event);
127 static int g_raid_update_volume(struct g_raid_volume *vol, u_int event);
128 static int g_raid_update_node(struct g_raid_softc *sc, u_int event);
129 static void g_raid_dumpconf(struct sbuf *sb, const char *indent,
130 struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
131 static void g_raid_start(struct bio *bp);
132 static void g_raid_start_request(struct bio *bp);
133 static void g_raid_disk_done(struct bio *bp);
134 static void g_raid_poll(struct g_raid_softc *sc);
137 g_raid_node_event2str(int event)
141 case G_RAID_NODE_E_WAKE:
143 case G_RAID_NODE_E_START:
151 g_raid_disk_state2str(int state)
155 case G_RAID_DISK_S_NONE:
157 case G_RAID_DISK_S_OFFLINE:
159 case G_RAID_DISK_S_DISABLED:
161 case G_RAID_DISK_S_FAILED:
163 case G_RAID_DISK_S_STALE_FAILED:
164 return ("STALE_FAILED");
165 case G_RAID_DISK_S_SPARE:
167 case G_RAID_DISK_S_STALE:
169 case G_RAID_DISK_S_ACTIVE:
177 g_raid_disk_event2str(int event)
181 case G_RAID_DISK_E_DISCONNECTED:
182 return ("DISCONNECTED");
189 g_raid_subdisk_state2str(int state)
193 case G_RAID_SUBDISK_S_NONE:
195 case G_RAID_SUBDISK_S_FAILED:
197 case G_RAID_SUBDISK_S_NEW:
199 case G_RAID_SUBDISK_S_REBUILD:
201 case G_RAID_SUBDISK_S_UNINITIALIZED:
202 return ("UNINITIALIZED");
203 case G_RAID_SUBDISK_S_STALE:
205 case G_RAID_SUBDISK_S_RESYNC:
207 case G_RAID_SUBDISK_S_ACTIVE:
215 g_raid_subdisk_event2str(int event)
219 case G_RAID_SUBDISK_E_NEW:
221 case G_RAID_SUBDISK_E_FAILED:
223 case G_RAID_SUBDISK_E_DISCONNECTED:
224 return ("DISCONNECTED");
231 g_raid_volume_state2str(int state)
235 case G_RAID_VOLUME_S_STARTING:
237 case G_RAID_VOLUME_S_BROKEN:
239 case G_RAID_VOLUME_S_DEGRADED:
241 case G_RAID_VOLUME_S_SUBOPTIMAL:
242 return ("SUBOPTIMAL");
243 case G_RAID_VOLUME_S_OPTIMAL:
245 case G_RAID_VOLUME_S_UNSUPPORTED:
246 return ("UNSUPPORTED");
247 case G_RAID_VOLUME_S_STOPPED:
255 g_raid_volume_event2str(int event)
259 case G_RAID_VOLUME_E_UP:
261 case G_RAID_VOLUME_E_DOWN:
263 case G_RAID_VOLUME_E_START:
265 case G_RAID_VOLUME_E_STARTMD:
273 g_raid_volume_level2str(int level, int qual)
277 case G_RAID_VOLUME_RL_RAID0:
279 case G_RAID_VOLUME_RL_RAID1:
281 case G_RAID_VOLUME_RL_RAID3:
282 if (qual == G_RAID_VOLUME_RLQ_R3P0)
284 if (qual == G_RAID_VOLUME_RLQ_R3PN)
287 case G_RAID_VOLUME_RL_RAID4:
288 if (qual == G_RAID_VOLUME_RLQ_R4P0)
290 if (qual == G_RAID_VOLUME_RLQ_R4PN)
293 case G_RAID_VOLUME_RL_RAID5:
294 if (qual == G_RAID_VOLUME_RLQ_R5RA)
296 if (qual == G_RAID_VOLUME_RLQ_R5RS)
298 if (qual == G_RAID_VOLUME_RLQ_R5LA)
300 if (qual == G_RAID_VOLUME_RLQ_R5LS)
303 case G_RAID_VOLUME_RL_RAID6:
304 if (qual == G_RAID_VOLUME_RLQ_R6RA)
306 if (qual == G_RAID_VOLUME_RLQ_R6RS)
308 if (qual == G_RAID_VOLUME_RLQ_R6LA)
310 if (qual == G_RAID_VOLUME_RLQ_R6LS)
313 case G_RAID_VOLUME_RL_RAIDMDF:
314 if (qual == G_RAID_VOLUME_RLQ_RMDFRA)
315 return ("RAIDMDF-RA");
316 if (qual == G_RAID_VOLUME_RLQ_RMDFRS)
317 return ("RAIDMDF-RS");
318 if (qual == G_RAID_VOLUME_RLQ_RMDFLA)
319 return ("RAIDMDF-LA");
320 if (qual == G_RAID_VOLUME_RLQ_RMDFLS)
321 return ("RAIDMDF-LS");
323 case G_RAID_VOLUME_RL_RAID1E:
324 if (qual == G_RAID_VOLUME_RLQ_R1EA)
326 if (qual == G_RAID_VOLUME_RLQ_R1EO)
329 case G_RAID_VOLUME_RL_SINGLE:
331 case G_RAID_VOLUME_RL_CONCAT:
333 case G_RAID_VOLUME_RL_RAID5E:
334 if (qual == G_RAID_VOLUME_RLQ_R5ERA)
335 return ("RAID5E-RA");
336 if (qual == G_RAID_VOLUME_RLQ_R5ERS)
337 return ("RAID5E-RS");
338 if (qual == G_RAID_VOLUME_RLQ_R5ELA)
339 return ("RAID5E-LA");
340 if (qual == G_RAID_VOLUME_RLQ_R5ELS)
341 return ("RAID5E-LS");
343 case G_RAID_VOLUME_RL_RAID5EE:
344 if (qual == G_RAID_VOLUME_RLQ_R5EERA)
345 return ("RAID5EE-RA");
346 if (qual == G_RAID_VOLUME_RLQ_R5EERS)
347 return ("RAID5EE-RS");
348 if (qual == G_RAID_VOLUME_RLQ_R5EELA)
349 return ("RAID5EE-LA");
350 if (qual == G_RAID_VOLUME_RLQ_R5EELS)
351 return ("RAID5EE-LS");
353 case G_RAID_VOLUME_RL_RAID5R:
354 if (qual == G_RAID_VOLUME_RLQ_R5RRA)
355 return ("RAID5R-RA");
356 if (qual == G_RAID_VOLUME_RLQ_R5RRS)
357 return ("RAID5R-RS");
358 if (qual == G_RAID_VOLUME_RLQ_R5RLA)
359 return ("RAID5R-LA");
360 if (qual == G_RAID_VOLUME_RLQ_R5RLS)
361 return ("RAID5R-LS");
369 g_raid_volume_str2level(const char *str, int *level, int *qual)
372 *level = G_RAID_VOLUME_RL_UNKNOWN;
373 *qual = G_RAID_VOLUME_RLQ_NONE;
374 if (strcasecmp(str, "RAID0") == 0)
375 *level = G_RAID_VOLUME_RL_RAID0;
376 else if (strcasecmp(str, "RAID1") == 0)
377 *level = G_RAID_VOLUME_RL_RAID1;
378 else if (strcasecmp(str, "RAID3-P0") == 0) {
379 *level = G_RAID_VOLUME_RL_RAID3;
380 *qual = G_RAID_VOLUME_RLQ_R3P0;
381 } else if (strcasecmp(str, "RAID3-PN") == 0 ||
382 strcasecmp(str, "RAID3") == 0) {
383 *level = G_RAID_VOLUME_RL_RAID3;
384 *qual = G_RAID_VOLUME_RLQ_R3PN;
385 } else if (strcasecmp(str, "RAID4-P0") == 0) {
386 *level = G_RAID_VOLUME_RL_RAID4;
387 *qual = G_RAID_VOLUME_RLQ_R4P0;
388 } else if (strcasecmp(str, "RAID4-PN") == 0 ||
389 strcasecmp(str, "RAID4") == 0) {
390 *level = G_RAID_VOLUME_RL_RAID4;
391 *qual = G_RAID_VOLUME_RLQ_R4PN;
392 } else if (strcasecmp(str, "RAID5-RA") == 0) {
393 *level = G_RAID_VOLUME_RL_RAID5;
394 *qual = G_RAID_VOLUME_RLQ_R5RA;
395 } else if (strcasecmp(str, "RAID5-RS") == 0) {
396 *level = G_RAID_VOLUME_RL_RAID5;
397 *qual = G_RAID_VOLUME_RLQ_R5RS;
398 } else if (strcasecmp(str, "RAID5") == 0 ||
399 strcasecmp(str, "RAID5-LA") == 0) {
400 *level = G_RAID_VOLUME_RL_RAID5;
401 *qual = G_RAID_VOLUME_RLQ_R5LA;
402 } else if (strcasecmp(str, "RAID5-LS") == 0) {
403 *level = G_RAID_VOLUME_RL_RAID5;
404 *qual = G_RAID_VOLUME_RLQ_R5LS;
405 } else if (strcasecmp(str, "RAID6-RA") == 0) {
406 *level = G_RAID_VOLUME_RL_RAID6;
407 *qual = G_RAID_VOLUME_RLQ_R6RA;
408 } else if (strcasecmp(str, "RAID6-RS") == 0) {
409 *level = G_RAID_VOLUME_RL_RAID6;
410 *qual = G_RAID_VOLUME_RLQ_R6RS;
411 } else if (strcasecmp(str, "RAID6") == 0 ||
412 strcasecmp(str, "RAID6-LA") == 0) {
413 *level = G_RAID_VOLUME_RL_RAID6;
414 *qual = G_RAID_VOLUME_RLQ_R6LA;
415 } else if (strcasecmp(str, "RAID6-LS") == 0) {
416 *level = G_RAID_VOLUME_RL_RAID6;
417 *qual = G_RAID_VOLUME_RLQ_R6LS;
418 } else if (strcasecmp(str, "RAIDMDF-RA") == 0) {
419 *level = G_RAID_VOLUME_RL_RAIDMDF;
420 *qual = G_RAID_VOLUME_RLQ_RMDFRA;
421 } else if (strcasecmp(str, "RAIDMDF-RS") == 0) {
422 *level = G_RAID_VOLUME_RL_RAIDMDF;
423 *qual = G_RAID_VOLUME_RLQ_RMDFRS;
424 } else if (strcasecmp(str, "RAIDMDF") == 0 ||
425 strcasecmp(str, "RAIDMDF-LA") == 0) {
426 *level = G_RAID_VOLUME_RL_RAIDMDF;
427 *qual = G_RAID_VOLUME_RLQ_RMDFLA;
428 } else if (strcasecmp(str, "RAIDMDF-LS") == 0) {
429 *level = G_RAID_VOLUME_RL_RAIDMDF;
430 *qual = G_RAID_VOLUME_RLQ_RMDFLS;
431 } else if (strcasecmp(str, "RAID10") == 0 ||
432 strcasecmp(str, "RAID1E") == 0 ||
433 strcasecmp(str, "RAID1E-A") == 0) {
434 *level = G_RAID_VOLUME_RL_RAID1E;
435 *qual = G_RAID_VOLUME_RLQ_R1EA;
436 } else if (strcasecmp(str, "RAID1E-O") == 0) {
437 *level = G_RAID_VOLUME_RL_RAID1E;
438 *qual = G_RAID_VOLUME_RLQ_R1EO;
439 } else if (strcasecmp(str, "SINGLE") == 0)
440 *level = G_RAID_VOLUME_RL_SINGLE;
441 else if (strcasecmp(str, "CONCAT") == 0)
442 *level = G_RAID_VOLUME_RL_CONCAT;
443 else if (strcasecmp(str, "RAID5E-RA") == 0) {
444 *level = G_RAID_VOLUME_RL_RAID5E;
445 *qual = G_RAID_VOLUME_RLQ_R5ERA;
446 } else if (strcasecmp(str, "RAID5E-RS") == 0) {
447 *level = G_RAID_VOLUME_RL_RAID5E;
448 *qual = G_RAID_VOLUME_RLQ_R5ERS;
449 } else if (strcasecmp(str, "RAID5E") == 0 ||
450 strcasecmp(str, "RAID5E-LA") == 0) {
451 *level = G_RAID_VOLUME_RL_RAID5E;
452 *qual = G_RAID_VOLUME_RLQ_R5ELA;
453 } else if (strcasecmp(str, "RAID5E-LS") == 0) {
454 *level = G_RAID_VOLUME_RL_RAID5E;
455 *qual = G_RAID_VOLUME_RLQ_R5ELS;
456 } else if (strcasecmp(str, "RAID5EE-RA") == 0) {
457 *level = G_RAID_VOLUME_RL_RAID5EE;
458 *qual = G_RAID_VOLUME_RLQ_R5EERA;
459 } else if (strcasecmp(str, "RAID5EE-RS") == 0) {
460 *level = G_RAID_VOLUME_RL_RAID5EE;
461 *qual = G_RAID_VOLUME_RLQ_R5EERS;
462 } else if (strcasecmp(str, "RAID5EE") == 0 ||
463 strcasecmp(str, "RAID5EE-LA") == 0) {
464 *level = G_RAID_VOLUME_RL_RAID5EE;
465 *qual = G_RAID_VOLUME_RLQ_R5EELA;
466 } else if (strcasecmp(str, "RAID5EE-LS") == 0) {
467 *level = G_RAID_VOLUME_RL_RAID5EE;
468 *qual = G_RAID_VOLUME_RLQ_R5EELS;
469 } else if (strcasecmp(str, "RAID5R-RA") == 0) {
470 *level = G_RAID_VOLUME_RL_RAID5R;
471 *qual = G_RAID_VOLUME_RLQ_R5RRA;
472 } else if (strcasecmp(str, "RAID5R-RS") == 0) {
473 *level = G_RAID_VOLUME_RL_RAID5R;
474 *qual = G_RAID_VOLUME_RLQ_R5RRS;
475 } else if (strcasecmp(str, "RAID5R") == 0 ||
476 strcasecmp(str, "RAID5R-LA") == 0) {
477 *level = G_RAID_VOLUME_RL_RAID5R;
478 *qual = G_RAID_VOLUME_RLQ_R5RLA;
479 } else if (strcasecmp(str, "RAID5R-LS") == 0) {
480 *level = G_RAID_VOLUME_RL_RAID5R;
481 *qual = G_RAID_VOLUME_RLQ_R5RLS;
488 g_raid_get_diskname(struct g_raid_disk *disk)
491 if (disk->d_consumer == NULL || disk->d_consumer->provider == NULL)
492 return ("[unknown]");
493 return (disk->d_consumer->provider->name);
497 g_raid_get_disk_info(struct g_raid_disk *disk)
499 struct g_consumer *cp = disk->d_consumer;
502 /* Read kernel dumping information. */
503 disk->d_kd.offset = 0;
504 disk->d_kd.length = OFF_MAX;
505 len = sizeof(disk->d_kd);
506 error = g_io_getattr("GEOM::kerneldump", cp, &len, &disk->d_kd);
508 disk->d_kd.di.dumper = NULL;
509 if (disk->d_kd.di.dumper == NULL)
510 G_RAID_DEBUG1(2, disk->d_softc,
511 "Dumping not supported by %s: %d.",
512 cp->provider->name, error);
514 /* Read BIO_DELETE support. */
515 error = g_getattr("GEOM::candelete", cp, &disk->d_candelete);
517 disk->d_candelete = 0;
518 if (!disk->d_candelete)
519 G_RAID_DEBUG1(2, disk->d_softc,
520 "BIO_DELETE not supported by %s: %d.",
521 cp->provider->name, error);
525 g_raid_report_disk_state(struct g_raid_disk *disk)
527 struct g_raid_subdisk *sd;
531 if (disk->d_consumer == NULL)
533 if (disk->d_state == G_RAID_DISK_S_DISABLED) {
534 s = G_STATE_ACTIVE; /* XXX */
535 } else if (disk->d_state == G_RAID_DISK_S_FAILED ||
536 disk->d_state == G_RAID_DISK_S_STALE_FAILED) {
539 state = G_RAID_SUBDISK_S_ACTIVE;
540 TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
541 if (sd->sd_state < state)
542 state = sd->sd_state;
544 if (state == G_RAID_SUBDISK_S_FAILED)
546 else if (state == G_RAID_SUBDISK_S_NEW ||
547 state == G_RAID_SUBDISK_S_REBUILD)
549 else if (state == G_RAID_SUBDISK_S_STALE ||
550 state == G_RAID_SUBDISK_S_RESYNC)
556 g_io_getattr("GEOM::setstate", disk->d_consumer, &len, &s);
557 G_RAID_DEBUG1(2, disk->d_softc, "Disk %s state reported as %d.",
558 g_raid_get_diskname(disk), s);
562 g_raid_change_disk_state(struct g_raid_disk *disk, int state)
565 G_RAID_DEBUG1(0, disk->d_softc, "Disk %s state changed from %s to %s.",
566 g_raid_get_diskname(disk),
567 g_raid_disk_state2str(disk->d_state),
568 g_raid_disk_state2str(state));
569 disk->d_state = state;
570 g_raid_report_disk_state(disk);
574 g_raid_change_subdisk_state(struct g_raid_subdisk *sd, int state)
577 G_RAID_DEBUG1(0, sd->sd_softc,
578 "Subdisk %s:%d-%s state changed from %s to %s.",
579 sd->sd_volume->v_name, sd->sd_pos,
580 sd->sd_disk ? g_raid_get_diskname(sd->sd_disk) : "[none]",
581 g_raid_subdisk_state2str(sd->sd_state),
582 g_raid_subdisk_state2str(state));
583 sd->sd_state = state;
585 g_raid_report_disk_state(sd->sd_disk);
589 g_raid_change_volume_state(struct g_raid_volume *vol, int state)
592 G_RAID_DEBUG1(0, vol->v_softc,
593 "Volume %s state changed from %s to %s.",
595 g_raid_volume_state2str(vol->v_state),
596 g_raid_volume_state2str(state));
597 vol->v_state = state;
601 * --- Events handling functions ---
602 * Events in geom_raid are used to maintain subdisks and volumes status
603 * from one thread to simplify locking.
606 g_raid_event_free(struct g_raid_event *ep)
613 g_raid_event_send(void *arg, int event, int flags)
615 struct g_raid_softc *sc;
616 struct g_raid_event *ep;
619 if ((flags & G_RAID_EVENT_VOLUME) != 0) {
620 sc = ((struct g_raid_volume *)arg)->v_softc;
621 } else if ((flags & G_RAID_EVENT_DISK) != 0) {
622 sc = ((struct g_raid_disk *)arg)->d_softc;
623 } else if ((flags & G_RAID_EVENT_SUBDISK) != 0) {
624 sc = ((struct g_raid_subdisk *)arg)->sd_softc;
628 ep = malloc(sizeof(*ep), M_RAID,
629 sx_xlocked(&sc->sc_lock) ? M_WAITOK : M_NOWAIT);
636 G_RAID_DEBUG1(4, sc, "Sending event %p. Waking up %p.", ep, sc);
637 mtx_lock(&sc->sc_queue_mtx);
638 TAILQ_INSERT_TAIL(&sc->sc_events, ep, e_next);
639 mtx_unlock(&sc->sc_queue_mtx);
642 if ((flags & G_RAID_EVENT_WAIT) == 0)
645 sx_assert(&sc->sc_lock, SX_XLOCKED);
646 G_RAID_DEBUG1(4, sc, "Sleeping on %p.", ep);
647 sx_xunlock(&sc->sc_lock);
648 while ((ep->e_flags & G_RAID_EVENT_DONE) == 0) {
649 mtx_lock(&sc->sc_queue_mtx);
650 MSLEEP(error, ep, &sc->sc_queue_mtx, PRIBIO | PDROP, "m:event",
654 g_raid_event_free(ep);
655 sx_xlock(&sc->sc_lock);
660 g_raid_event_cancel(struct g_raid_softc *sc, void *tgt)
662 struct g_raid_event *ep, *tmpep;
664 sx_assert(&sc->sc_lock, SX_XLOCKED);
666 mtx_lock(&sc->sc_queue_mtx);
667 TAILQ_FOREACH_SAFE(ep, &sc->sc_events, e_next, tmpep) {
668 if (ep->e_tgt != tgt)
670 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
671 if ((ep->e_flags & G_RAID_EVENT_WAIT) == 0)
672 g_raid_event_free(ep);
674 ep->e_error = ECANCELED;
678 mtx_unlock(&sc->sc_queue_mtx);
682 g_raid_event_check(struct g_raid_softc *sc, void *tgt)
684 struct g_raid_event *ep;
687 sx_assert(&sc->sc_lock, SX_XLOCKED);
689 mtx_lock(&sc->sc_queue_mtx);
690 TAILQ_FOREACH(ep, &sc->sc_events, e_next) {
691 if (ep->e_tgt != tgt)
696 mtx_unlock(&sc->sc_queue_mtx);
701 * Return the number of disks in given state.
702 * If state is equal to -1, count all connected disks.
705 g_raid_ndisks(struct g_raid_softc *sc, int state)
707 struct g_raid_disk *disk;
710 sx_assert(&sc->sc_lock, SX_LOCKED);
713 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
714 if (disk->d_state == state || state == -1)
721 * Return the number of subdisks in given state.
722 * If state is equal to -1, count all connected disks.
725 g_raid_nsubdisks(struct g_raid_volume *vol, int state)
727 struct g_raid_subdisk *subdisk;
728 struct g_raid_softc *sc;
732 sx_assert(&sc->sc_lock, SX_LOCKED);
735 for (i = 0; i < vol->v_disks_count; i++) {
736 subdisk = &vol->v_subdisks[i];
738 subdisk->sd_state != G_RAID_SUBDISK_S_NONE) ||
739 subdisk->sd_state == state)
746 * Return the first subdisk in given state.
747 * If state is equal to -1, then the first connected disks.
749 struct g_raid_subdisk *
750 g_raid_get_subdisk(struct g_raid_volume *vol, int state)
752 struct g_raid_subdisk *sd;
753 struct g_raid_softc *sc;
757 sx_assert(&sc->sc_lock, SX_LOCKED);
759 for (i = 0; i < vol->v_disks_count; i++) {
760 sd = &vol->v_subdisks[i];
762 sd->sd_state != G_RAID_SUBDISK_S_NONE) ||
763 sd->sd_state == state)
770 g_raid_open_consumer(struct g_raid_softc *sc, const char *name)
772 struct g_consumer *cp;
773 struct g_provider *pp;
777 if (strncmp(name, "/dev/", 5) == 0)
779 pp = g_provider_by_name(name);
782 cp = g_new_consumer(sc->sc_geom);
783 cp->flags |= G_CF_DIRECT_RECEIVE;
784 if (g_attach(cp, pp) != 0) {
785 g_destroy_consumer(cp);
788 if (g_access(cp, 1, 1, 1) != 0) {
790 g_destroy_consumer(cp);
797 g_raid_nrequests(struct g_raid_softc *sc, struct g_consumer *cp)
802 mtx_lock(&sc->sc_queue_mtx);
803 TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
804 if (bp->bio_from == cp)
807 mtx_unlock(&sc->sc_queue_mtx);
812 g_raid_nopens(struct g_raid_softc *sc)
814 struct g_raid_volume *vol;
818 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
819 if (vol->v_provider_open != 0)
826 g_raid_consumer_is_busy(struct g_raid_softc *sc, struct g_consumer *cp)
831 "I/O requests for %s exist, can't destroy it now.",
835 if (g_raid_nrequests(sc, cp) > 0) {
837 "I/O requests for %s in queue, can't destroy it now.",
845 g_raid_destroy_consumer(void *arg, int flags __unused)
847 struct g_consumer *cp;
852 G_RAID_DEBUG(1, "Consumer %s destroyed.", cp->provider->name);
854 g_destroy_consumer(cp);
858 g_raid_kill_consumer(struct g_raid_softc *sc, struct g_consumer *cp)
860 struct g_provider *pp;
863 g_topology_assert_not();
867 if (g_raid_consumer_is_busy(sc, cp))
872 if ((pp->geom->flags & G_GEOM_WITHER) == 0)
875 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
876 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
879 * After retaste event was send (inside g_access()), we can send
880 * event to detach and destroy consumer.
881 * A class, which has consumer to the given provider connected
882 * will not receive retaste event for the provider.
883 * This is the way how I ignore retaste events when I close
884 * consumers opened for write: I detach and destroy consumer
885 * after retaste event is sent.
887 g_post_event(g_raid_destroy_consumer, cp, M_WAITOK, NULL);
890 G_RAID_DEBUG(1, "Consumer %s destroyed.", pp->name);
892 g_destroy_consumer(cp);
898 g_raid_orphan(struct g_consumer *cp)
900 struct g_raid_disk *disk;
907 g_raid_event_send(disk, G_RAID_DISK_E_DISCONNECTED,
912 g_raid_clean(struct g_raid_volume *vol, int acw)
914 struct g_raid_softc *sc;
918 g_topology_assert_not();
919 sx_assert(&sc->sc_lock, SX_XLOCKED);
921 // if ((sc->sc_flags & G_RAID_DEVICE_FLAG_NOFAILSYNC) != 0)
925 if (vol->v_writes > 0)
927 if (acw > 0 || (acw == -1 &&
928 vol->v_provider != NULL && vol->v_provider->acw > 0)) {
929 timeout = g_raid_clean_time - (time_uptime - vol->v_last_write);
930 if (!g_raid_shutdown && timeout > 0)
934 G_RAID_DEBUG1(1, sc, "Volume %s marked as clean.",
936 g_raid_write_metadata(sc, vol, NULL, NULL);
940 g_raid_dirty(struct g_raid_volume *vol)
942 struct g_raid_softc *sc;
945 g_topology_assert_not();
946 sx_assert(&sc->sc_lock, SX_XLOCKED);
948 // if ((sc->sc_flags & G_RAID_DEVICE_FLAG_NOFAILSYNC) != 0)
951 G_RAID_DEBUG1(1, sc, "Volume %s marked as dirty.",
953 g_raid_write_metadata(sc, vol, NULL, NULL);
957 g_raid_tr_flush_common(struct g_raid_tr_object *tr, struct bio *bp)
959 struct g_raid_volume *vol;
960 struct g_raid_subdisk *sd;
961 struct bio_queue_head queue;
965 vol = tr->tro_volume;
968 * Allocate all bios before sending any request, so we can return
969 * ENOMEM in nice and clean way.
972 for (i = 0; i < vol->v_disks_count; i++) {
973 sd = &vol->v_subdisks[i];
974 if (sd->sd_state == G_RAID_SUBDISK_S_NONE ||
975 sd->sd_state == G_RAID_SUBDISK_S_FAILED)
977 cbp = g_clone_bio(bp);
980 cbp->bio_caller1 = sd;
981 bioq_insert_tail(&queue, cbp);
983 while ((cbp = bioq_takefirst(&queue)) != NULL) {
984 sd = cbp->bio_caller1;
985 cbp->bio_caller1 = NULL;
986 g_raid_subdisk_iostart(sd, cbp);
990 while ((cbp = bioq_takefirst(&queue)) != NULL)
992 if (bp->bio_error == 0)
993 bp->bio_error = ENOMEM;
994 g_raid_iodone(bp, bp->bio_error);
998 g_raid_tr_kerneldump_common_done(struct bio *bp)
1001 bp->bio_flags |= BIO_DONE;
1005 g_raid_tr_kerneldump_common(struct g_raid_tr_object *tr,
1006 void *virtual, vm_offset_t physical, off_t offset, size_t length)
1008 struct g_raid_softc *sc;
1009 struct g_raid_volume *vol;
1012 vol = tr->tro_volume;
1016 bp.bio_cmd = BIO_WRITE;
1017 bp.bio_done = g_raid_tr_kerneldump_common_done;
1018 bp.bio_attribute = NULL;
1019 bp.bio_offset = offset;
1020 bp.bio_length = length;
1021 bp.bio_data = virtual;
1022 bp.bio_to = vol->v_provider;
1025 while (!(bp.bio_flags & BIO_DONE)) {
1026 G_RAID_DEBUG1(4, sc, "Poll...");
1031 return (bp.bio_error != 0 ? EIO : 0);
1035 g_raid_dump(void *arg,
1036 void *virtual, vm_offset_t physical, 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,
1046 virtual, physical, offset, length);
1051 g_raid_kerneldump(struct g_raid_softc *sc, struct bio *bp)
1053 struct g_kerneldump *gkd;
1054 struct g_provider *pp;
1055 struct g_raid_volume *vol;
1057 gkd = (struct g_kerneldump*)bp->bio_data;
1060 g_trace(G_T_TOPOLOGY, "g_raid_kerneldump(%s, %jd, %jd)",
1061 pp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
1062 gkd->di.dumper = g_raid_dump;
1064 gkd->di.blocksize = vol->v_sectorsize;
1065 gkd->di.maxiosize = DFLTPHYS;
1066 gkd->di.mediaoffset = gkd->offset;
1067 if ((gkd->offset + gkd->length) > vol->v_mediasize)
1068 gkd->length = vol->v_mediasize - gkd->offset;
1069 gkd->di.mediasize = gkd->length;
1070 g_io_deliver(bp, 0);
1074 g_raid_candelete(struct g_raid_softc *sc, struct bio *bp)
1076 struct g_provider *pp;
1077 struct g_raid_volume *vol;
1078 struct g_raid_subdisk *sd;
1083 for (i = 0; i < vol->v_disks_count; i++) {
1084 sd = &vol->v_subdisks[i];
1085 if (sd->sd_state == G_RAID_SUBDISK_S_NONE)
1087 if (sd->sd_disk->d_candelete)
1090 val = i < vol->v_disks_count;
1091 g_handleattr(bp, "GEOM::candelete", &val, sizeof(val));
1095 g_raid_start(struct bio *bp)
1097 struct g_raid_softc *sc;
1099 sc = bp->bio_to->geom->softc;
1101 * If sc == NULL or there are no valid disks, provider's error
1102 * should be set and g_raid_start() should not be called at all.
1104 // KASSERT(sc != NULL && sc->sc_state == G_RAID_VOLUME_S_RUNNING,
1105 // ("Provider's error should be set (error=%d)(mirror=%s).",
1106 // bp->bio_to->error, bp->bio_to->name));
1107 G_RAID_LOGREQ(3, bp, "Request received.");
1109 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;
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;
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, 0, 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,
1412 void *virtual, vm_offset_t physical, 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,
1421 sd->sd_disk->d_kd.di.mediaoffset + sd->sd_offset + offset,
1426 g_raid_disk_done(struct bio *bp)
1428 struct g_raid_softc *sc;
1429 struct g_raid_subdisk *sd;
1431 sd = bp->bio_caller1;
1433 mtx_lock(&sc->sc_queue_mtx);
1434 bioq_insert_tail(&sc->sc_queue, bp);
1435 mtx_unlock(&sc->sc_queue_mtx);
1441 g_raid_disk_done_request(struct bio *bp)
1443 struct g_raid_softc *sc;
1444 struct g_raid_disk *disk;
1445 struct g_raid_subdisk *sd;
1446 struct g_raid_volume *vol;
1448 g_topology_assert_not();
1450 G_RAID_LOGREQ(3, bp, "Disk request done: %d.", bp->bio_error);
1451 sd = bp->bio_caller1;
1453 vol = sd->sd_volume;
1454 if (bp->bio_from != NULL) {
1455 bp->bio_from->index--;
1456 disk = bp->bio_from->private;
1458 g_raid_kill_consumer(sc, bp->bio_from);
1460 bp->bio_offset -= sd->sd_offset;
1462 G_RAID_TR_IODONE(vol->v_tr, sd, bp);
1466 g_raid_handle_event(struct g_raid_softc *sc, struct g_raid_event *ep)
1469 if ((ep->e_flags & G_RAID_EVENT_VOLUME) != 0)
1470 ep->e_error = g_raid_update_volume(ep->e_tgt, ep->e_event);
1471 else if ((ep->e_flags & G_RAID_EVENT_DISK) != 0)
1472 ep->e_error = g_raid_update_disk(ep->e_tgt, ep->e_event);
1473 else if ((ep->e_flags & G_RAID_EVENT_SUBDISK) != 0)
1474 ep->e_error = g_raid_update_subdisk(ep->e_tgt, ep->e_event);
1476 ep->e_error = g_raid_update_node(ep->e_tgt, ep->e_event);
1477 if ((ep->e_flags & G_RAID_EVENT_WAIT) == 0) {
1478 KASSERT(ep->e_error == 0,
1479 ("Error cannot be handled."));
1480 g_raid_event_free(ep);
1482 ep->e_flags |= G_RAID_EVENT_DONE;
1483 G_RAID_DEBUG1(4, sc, "Waking up %p.", ep);
1484 mtx_lock(&sc->sc_queue_mtx);
1486 mtx_unlock(&sc->sc_queue_mtx);
1494 g_raid_worker(void *arg)
1496 struct g_raid_softc *sc;
1497 struct g_raid_event *ep;
1498 struct g_raid_volume *vol;
1500 struct timeval now, t;
1504 thread_lock(curthread);
1505 sched_prio(curthread, PRIBIO);
1506 thread_unlock(curthread);
1508 sx_xlock(&sc->sc_lock);
1510 mtx_lock(&sc->sc_queue_mtx);
1512 * First take a look at events.
1513 * This is important to handle events before any I/O requests.
1518 ep = TAILQ_FIRST(&sc->sc_events);
1520 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
1521 else if ((bp = bioq_takefirst(&sc->sc_queue)) != NULL)
1524 getmicrouptime(&now);
1526 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1527 if (bioq_first(&vol->v_inflight) == NULL &&
1529 timevalcmp(&vol->v_last_done, &t, < ))
1530 t = vol->v_last_done;
1532 timevalsub(&t, &now);
1533 timeout = g_raid_idle_threshold +
1534 t.tv_sec * 1000000 + t.tv_usec;
1537 * Two steps to avoid overflows at HZ=1000
1538 * and idle timeouts > 2.1s. Some rounding
1539 * errors can occur, but they are < 1tick,
1540 * which is deemed to be close enough for
1543 int micpertic = 1000000 / hz;
1544 timeout = (timeout + micpertic - 1) / micpertic;
1545 sx_xunlock(&sc->sc_lock);
1546 MSLEEP(rv, sc, &sc->sc_queue_mtx,
1547 PRIBIO | PDROP, "-", timeout);
1548 sx_xlock(&sc->sc_lock);
1553 mtx_unlock(&sc->sc_queue_mtx);
1556 g_raid_handle_event(sc, ep);
1557 } else if (bp != NULL) {
1558 if (bp->bio_to != NULL &&
1559 bp->bio_to->geom == sc->sc_geom)
1560 g_raid_start_request(bp);
1562 g_raid_disk_done_request(bp);
1563 } else if (rv == EWOULDBLOCK) {
1564 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1565 g_raid_clean(vol, -1);
1566 if (bioq_first(&vol->v_inflight) == NULL &&
1568 t.tv_sec = g_raid_idle_threshold / 1000000;
1569 t.tv_usec = g_raid_idle_threshold % 1000000;
1570 timevaladd(&t, &vol->v_last_done);
1571 getmicrouptime(&now);
1572 if (timevalcmp(&t, &now, <= )) {
1573 G_RAID_TR_IDLE(vol->v_tr);
1574 vol->v_last_done = now;
1579 if (sc->sc_stopping == G_RAID_DESTROY_HARD)
1580 g_raid_destroy_node(sc, 1); /* May not return. */
1585 g_raid_poll(struct g_raid_softc *sc)
1587 struct g_raid_event *ep;
1590 sx_xlock(&sc->sc_lock);
1591 mtx_lock(&sc->sc_queue_mtx);
1593 * First take a look at events.
1594 * This is important to handle events before any I/O requests.
1596 ep = TAILQ_FIRST(&sc->sc_events);
1598 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
1599 mtx_unlock(&sc->sc_queue_mtx);
1600 g_raid_handle_event(sc, ep);
1603 bp = bioq_takefirst(&sc->sc_queue);
1605 mtx_unlock(&sc->sc_queue_mtx);
1606 if (bp->bio_from == NULL ||
1607 bp->bio_from->geom != sc->sc_geom)
1608 g_raid_start_request(bp);
1610 g_raid_disk_done_request(bp);
1613 sx_xunlock(&sc->sc_lock);
1617 g_raid_launch_provider(struct g_raid_volume *vol)
1619 struct g_raid_disk *disk;
1620 struct g_raid_subdisk *sd;
1621 struct g_raid_softc *sc;
1622 struct g_provider *pp;
1623 char name[G_RAID_MAX_VOLUMENAME];
1628 sx_assert(&sc->sc_lock, SX_LOCKED);
1631 /* Try to name provider with volume name. */
1632 snprintf(name, sizeof(name), "raid/%s", vol->v_name);
1633 if (g_raid_name_format == 0 || vol->v_name[0] == 0 ||
1634 g_provider_by_name(name) != NULL) {
1635 /* Otherwise use sequential volume number. */
1636 snprintf(name, sizeof(name), "raid/r%d", vol->v_global_id);
1639 pp = g_new_providerf(sc->sc_geom, "%s", name);
1640 pp->flags |= G_PF_DIRECT_RECEIVE;
1641 if (vol->v_tr->tro_class->trc_accept_unmapped) {
1642 pp->flags |= G_PF_ACCEPT_UNMAPPED;
1643 for (i = 0; i < vol->v_disks_count; i++) {
1644 sd = &vol->v_subdisks[i];
1645 if (sd->sd_state == G_RAID_SUBDISK_S_NONE)
1647 if ((sd->sd_disk->d_consumer->provider->flags &
1648 G_PF_ACCEPT_UNMAPPED) == 0)
1649 pp->flags &= ~G_PF_ACCEPT_UNMAPPED;
1653 pp->mediasize = vol->v_mediasize;
1654 pp->sectorsize = vol->v_sectorsize;
1656 pp->stripeoffset = 0;
1657 if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1 ||
1658 vol->v_raid_level == G_RAID_VOLUME_RL_RAID3 ||
1659 vol->v_raid_level == G_RAID_VOLUME_RL_SINGLE ||
1660 vol->v_raid_level == G_RAID_VOLUME_RL_CONCAT) {
1661 if ((disk = vol->v_subdisks[0].sd_disk) != NULL &&
1662 disk->d_consumer != NULL &&
1663 disk->d_consumer->provider != NULL) {
1664 pp->stripesize = disk->d_consumer->provider->stripesize;
1665 off = disk->d_consumer->provider->stripeoffset;
1666 pp->stripeoffset = off + vol->v_subdisks[0].sd_offset;
1668 pp->stripeoffset %= off;
1670 if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID3) {
1671 pp->stripesize *= (vol->v_disks_count - 1);
1672 pp->stripeoffset *= (vol->v_disks_count - 1);
1675 pp->stripesize = vol->v_strip_size;
1676 vol->v_provider = pp;
1677 g_error_provider(pp, 0);
1678 g_topology_unlock();
1679 G_RAID_DEBUG1(0, sc, "Provider %s for volume %s created.",
1680 pp->name, vol->v_name);
1684 g_raid_destroy_provider(struct g_raid_volume *vol)
1686 struct g_raid_softc *sc;
1687 struct g_provider *pp;
1688 struct bio *bp, *tmp;
1690 g_topology_assert_not();
1692 pp = vol->v_provider;
1693 KASSERT(pp != NULL, ("NULL provider (volume=%s).", vol->v_name));
1696 g_error_provider(pp, ENXIO);
1697 mtx_lock(&sc->sc_queue_mtx);
1698 TAILQ_FOREACH_SAFE(bp, &sc->sc_queue.queue, bio_queue, tmp) {
1699 if (bp->bio_to != pp)
1701 bioq_remove(&sc->sc_queue, bp);
1702 g_io_deliver(bp, ENXIO);
1704 mtx_unlock(&sc->sc_queue_mtx);
1705 G_RAID_DEBUG1(0, sc, "Provider %s for volume %s destroyed.",
1706 pp->name, vol->v_name);
1707 g_wither_provider(pp, ENXIO);
1708 g_topology_unlock();
1709 vol->v_provider = NULL;
1713 * Update device state.
1716 g_raid_update_volume(struct g_raid_volume *vol, u_int event)
1718 struct g_raid_softc *sc;
1721 sx_assert(&sc->sc_lock, SX_XLOCKED);
1723 G_RAID_DEBUG1(2, sc, "Event %s for volume %s.",
1724 g_raid_volume_event2str(event),
1727 case G_RAID_VOLUME_E_DOWN:
1728 if (vol->v_provider != NULL)
1729 g_raid_destroy_provider(vol);
1731 case G_RAID_VOLUME_E_UP:
1732 if (vol->v_provider == NULL)
1733 g_raid_launch_provider(vol);
1735 case G_RAID_VOLUME_E_START:
1737 G_RAID_TR_START(vol->v_tr);
1741 G_RAID_MD_VOLUME_EVENT(sc->sc_md, vol, event);
1745 /* Manage root mount release. */
1746 if (vol->v_starting) {
1747 vol->v_starting = 0;
1748 G_RAID_DEBUG1(1, sc, "root_mount_rel %p", vol->v_rootmount);
1749 root_mount_rel(vol->v_rootmount);
1750 vol->v_rootmount = NULL;
1752 if (vol->v_stopping && vol->v_provider_open == 0)
1753 g_raid_destroy_volume(vol);
1758 * Update subdisk state.
1761 g_raid_update_subdisk(struct g_raid_subdisk *sd, u_int event)
1763 struct g_raid_softc *sc;
1764 struct g_raid_volume *vol;
1767 vol = sd->sd_volume;
1768 sx_assert(&sc->sc_lock, SX_XLOCKED);
1770 G_RAID_DEBUG1(2, sc, "Event %s for subdisk %s:%d-%s.",
1771 g_raid_subdisk_event2str(event),
1772 vol->v_name, sd->sd_pos,
1773 sd->sd_disk ? g_raid_get_diskname(sd->sd_disk) : "[none]");
1775 G_RAID_TR_EVENT(vol->v_tr, sd, event);
1781 * Update disk state.
1784 g_raid_update_disk(struct g_raid_disk *disk, u_int event)
1786 struct g_raid_softc *sc;
1789 sx_assert(&sc->sc_lock, SX_XLOCKED);
1791 G_RAID_DEBUG1(2, sc, "Event %s for disk %s.",
1792 g_raid_disk_event2str(event),
1793 g_raid_get_diskname(disk));
1796 G_RAID_MD_EVENT(sc->sc_md, disk, event);
1804 g_raid_update_node(struct g_raid_softc *sc, u_int event)
1806 sx_assert(&sc->sc_lock, SX_XLOCKED);
1808 G_RAID_DEBUG1(2, sc, "Event %s for the array.",
1809 g_raid_node_event2str(event));
1811 if (event == G_RAID_NODE_E_WAKE)
1814 G_RAID_MD_EVENT(sc->sc_md, NULL, event);
1819 g_raid_access(struct g_provider *pp, int acr, int acw, int ace)
1821 struct g_raid_volume *vol;
1822 struct g_raid_softc *sc;
1823 int dcw, opens, error = 0;
1825 g_topology_assert();
1826 sc = pp->geom->softc;
1828 KASSERT(sc != NULL, ("NULL softc (provider=%s).", pp->name));
1829 KASSERT(vol != NULL, ("NULL volume (provider=%s).", pp->name));
1831 G_RAID_DEBUG1(2, sc, "Access request for %s: r%dw%de%d.", pp->name,
1833 dcw = pp->acw + acw;
1835 g_topology_unlock();
1836 sx_xlock(&sc->sc_lock);
1837 /* Deny new opens while dying. */
1838 if (sc->sc_stopping != 0 && (acr > 0 || acw > 0 || ace > 0)) {
1842 /* Deny write opens for read-only volumes. */
1843 if (vol->v_read_only && acw > 0) {
1848 g_raid_clean(vol, dcw);
1849 vol->v_provider_open += acr + acw + ace;
1850 /* Handle delayed node destruction. */
1851 if (sc->sc_stopping == G_RAID_DESTROY_DELAYED &&
1852 vol->v_provider_open == 0) {
1853 /* Count open volumes. */
1854 opens = g_raid_nopens(sc);
1856 sc->sc_stopping = G_RAID_DESTROY_HARD;
1857 /* Wake up worker to make it selfdestruct. */
1858 g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
1861 /* Handle open volume destruction. */
1862 if (vol->v_stopping && vol->v_provider_open == 0)
1863 g_raid_destroy_volume(vol);
1865 sx_xunlock(&sc->sc_lock);
1870 struct g_raid_softc *
1871 g_raid_create_node(struct g_class *mp,
1872 const char *name, struct g_raid_md_object *md)
1874 struct g_raid_softc *sc;
1878 g_topology_assert();
1879 G_RAID_DEBUG(1, "Creating array %s.", name);
1881 gp = g_new_geomf(mp, "%s", name);
1882 sc = malloc(sizeof(*sc), M_RAID, M_WAITOK | M_ZERO);
1883 gp->start = g_raid_start;
1884 gp->orphan = g_raid_orphan;
1885 gp->access = g_raid_access;
1886 gp->dumpconf = g_raid_dumpconf;
1891 TAILQ_INIT(&sc->sc_volumes);
1892 TAILQ_INIT(&sc->sc_disks);
1893 sx_init(&sc->sc_lock, "graid:lock");
1894 mtx_init(&sc->sc_queue_mtx, "graid:queue", NULL, MTX_DEF);
1895 TAILQ_INIT(&sc->sc_events);
1896 bioq_init(&sc->sc_queue);
1898 error = kproc_create(g_raid_worker, sc, &sc->sc_worker, 0, 0,
1901 G_RAID_DEBUG(0, "Cannot create kernel thread for %s.", name);
1902 mtx_destroy(&sc->sc_queue_mtx);
1903 sx_destroy(&sc->sc_lock);
1904 g_destroy_geom(sc->sc_geom);
1909 G_RAID_DEBUG1(0, sc, "Array %s created.", name);
1913 struct g_raid_volume *
1914 g_raid_create_volume(struct g_raid_softc *sc, const char *name, int id)
1916 struct g_raid_volume *vol, *vol1;
1919 G_RAID_DEBUG1(1, sc, "Creating volume %s.", name);
1920 vol = malloc(sizeof(*vol), M_RAID, M_WAITOK | M_ZERO);
1922 strlcpy(vol->v_name, name, G_RAID_MAX_VOLUMENAME);
1923 vol->v_state = G_RAID_VOLUME_S_STARTING;
1924 vol->v_raid_level = G_RAID_VOLUME_RL_UNKNOWN;
1925 vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_UNKNOWN;
1926 vol->v_rotate_parity = 1;
1927 bioq_init(&vol->v_inflight);
1928 bioq_init(&vol->v_locked);
1929 LIST_INIT(&vol->v_locks);
1930 for (i = 0; i < G_RAID_MAX_SUBDISKS; i++) {
1931 vol->v_subdisks[i].sd_softc = sc;
1932 vol->v_subdisks[i].sd_volume = vol;
1933 vol->v_subdisks[i].sd_pos = i;
1934 vol->v_subdisks[i].sd_state = G_RAID_DISK_S_NONE;
1937 /* Find free ID for this volume. */
1941 LIST_FOREACH(vol1, &g_raid_volumes, v_global_next) {
1942 if (vol1->v_global_id == id)
1947 for (id = 0; ; id++) {
1948 LIST_FOREACH(vol1, &g_raid_volumes, v_global_next) {
1949 if (vol1->v_global_id == id)
1956 vol->v_global_id = id;
1957 LIST_INSERT_HEAD(&g_raid_volumes, vol, v_global_next);
1958 g_topology_unlock();
1960 /* Delay root mounting. */
1961 vol->v_rootmount = root_mount_hold("GRAID");
1962 G_RAID_DEBUG1(1, sc, "root_mount_hold %p", vol->v_rootmount);
1963 vol->v_starting = 1;
1964 TAILQ_INSERT_TAIL(&sc->sc_volumes, vol, v_next);
1968 struct g_raid_disk *
1969 g_raid_create_disk(struct g_raid_softc *sc)
1971 struct g_raid_disk *disk;
1973 G_RAID_DEBUG1(1, sc, "Creating disk.");
1974 disk = malloc(sizeof(*disk), M_RAID, M_WAITOK | M_ZERO);
1976 disk->d_state = G_RAID_DISK_S_NONE;
1977 TAILQ_INIT(&disk->d_subdisks);
1978 TAILQ_INSERT_TAIL(&sc->sc_disks, disk, d_next);
1982 int g_raid_start_volume(struct g_raid_volume *vol)
1984 struct g_raid_tr_class *class;
1985 struct g_raid_tr_object *obj;
1988 G_RAID_DEBUG1(2, vol->v_softc, "Starting volume %s.", vol->v_name);
1989 LIST_FOREACH(class, &g_raid_tr_classes, trc_list) {
1990 if (!class->trc_enable)
1992 G_RAID_DEBUG1(2, vol->v_softc,
1993 "Tasting volume %s for %s transformation.",
1994 vol->v_name, class->name);
1995 obj = (void *)kobj_create((kobj_class_t)class, M_RAID,
1997 obj->tro_class = class;
1998 obj->tro_volume = vol;
1999 status = G_RAID_TR_TASTE(obj, vol);
2000 if (status != G_RAID_TR_TASTE_FAIL)
2002 kobj_delete((kobj_t)obj, M_RAID);
2004 if (class == NULL) {
2005 G_RAID_DEBUG1(0, vol->v_softc,
2006 "No transformation module found for %s.",
2009 g_raid_change_volume_state(vol, G_RAID_VOLUME_S_UNSUPPORTED);
2010 g_raid_event_send(vol, G_RAID_VOLUME_E_DOWN,
2011 G_RAID_EVENT_VOLUME);
2014 G_RAID_DEBUG1(2, vol->v_softc,
2015 "Transformation module %s chosen for %s.",
2016 class->name, vol->v_name);
2022 g_raid_destroy_node(struct g_raid_softc *sc, int worker)
2024 struct g_raid_volume *vol, *tmpv;
2025 struct g_raid_disk *disk, *tmpd;
2028 sc->sc_stopping = G_RAID_DESTROY_HARD;
2029 TAILQ_FOREACH_SAFE(vol, &sc->sc_volumes, v_next, tmpv) {
2030 if (g_raid_destroy_volume(vol))
2035 TAILQ_FOREACH_SAFE(disk, &sc->sc_disks, d_next, tmpd) {
2036 if (g_raid_destroy_disk(disk))
2042 G_RAID_MD_FREE(sc->sc_md);
2043 kobj_delete((kobj_t)sc->sc_md, M_RAID);
2046 if (sc->sc_geom != NULL) {
2047 G_RAID_DEBUG1(0, sc, "Array %s destroyed.", sc->sc_name);
2049 sc->sc_geom->softc = NULL;
2050 g_wither_geom(sc->sc_geom, ENXIO);
2051 g_topology_unlock();
2054 G_RAID_DEBUG(1, "Array destroyed.");
2056 g_raid_event_cancel(sc, sc);
2057 mtx_destroy(&sc->sc_queue_mtx);
2058 sx_xunlock(&sc->sc_lock);
2059 sx_destroy(&sc->sc_lock);
2060 wakeup(&sc->sc_stopping);
2062 curthread->td_pflags &= ~TDP_GEOM;
2063 G_RAID_DEBUG(1, "Thread exiting.");
2066 /* Wake up worker to make it selfdestruct. */
2067 g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
2073 g_raid_destroy_volume(struct g_raid_volume *vol)
2075 struct g_raid_softc *sc;
2076 struct g_raid_disk *disk;
2080 G_RAID_DEBUG1(2, sc, "Destroying volume %s.", vol->v_name);
2081 vol->v_stopping = 1;
2082 if (vol->v_state != G_RAID_VOLUME_S_STOPPED) {
2084 G_RAID_TR_STOP(vol->v_tr);
2087 vol->v_state = G_RAID_VOLUME_S_STOPPED;
2089 if (g_raid_event_check(sc, vol) != 0)
2091 if (vol->v_provider != NULL)
2093 if (vol->v_provider_open != 0)
2096 G_RAID_TR_FREE(vol->v_tr);
2097 kobj_delete((kobj_t)vol->v_tr, M_RAID);
2100 if (vol->v_rootmount)
2101 root_mount_rel(vol->v_rootmount);
2103 LIST_REMOVE(vol, v_global_next);
2104 g_topology_unlock();
2105 TAILQ_REMOVE(&sc->sc_volumes, vol, v_next);
2106 for (i = 0; i < G_RAID_MAX_SUBDISKS; i++) {
2107 g_raid_event_cancel(sc, &vol->v_subdisks[i]);
2108 disk = vol->v_subdisks[i].sd_disk;
2111 TAILQ_REMOVE(&disk->d_subdisks, &vol->v_subdisks[i], sd_next);
2113 G_RAID_DEBUG1(2, sc, "Volume %s destroyed.", vol->v_name);
2115 G_RAID_MD_FREE_VOLUME(sc->sc_md, vol);
2116 g_raid_event_cancel(sc, vol);
2118 if (sc->sc_stopping == G_RAID_DESTROY_HARD) {
2119 /* Wake up worker to let it selfdestruct. */
2120 g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
2126 g_raid_destroy_disk(struct g_raid_disk *disk)
2128 struct g_raid_softc *sc;
2129 struct g_raid_subdisk *sd, *tmp;
2132 G_RAID_DEBUG1(2, sc, "Destroying disk.");
2133 if (disk->d_consumer) {
2134 g_raid_kill_consumer(sc, disk->d_consumer);
2135 disk->d_consumer = NULL;
2137 TAILQ_FOREACH_SAFE(sd, &disk->d_subdisks, sd_next, tmp) {
2138 g_raid_change_subdisk_state(sd, G_RAID_SUBDISK_S_NONE);
2139 g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED,
2140 G_RAID_EVENT_SUBDISK);
2141 TAILQ_REMOVE(&disk->d_subdisks, sd, sd_next);
2144 TAILQ_REMOVE(&sc->sc_disks, disk, d_next);
2146 G_RAID_MD_FREE_DISK(sc->sc_md, disk);
2147 g_raid_event_cancel(sc, disk);
2153 g_raid_destroy(struct g_raid_softc *sc, int how)
2157 g_topology_assert_not();
2160 sx_assert(&sc->sc_lock, SX_XLOCKED);
2162 /* Count open volumes. */
2163 opens = g_raid_nopens(sc);
2165 /* React on some opened volumes. */
2168 case G_RAID_DESTROY_SOFT:
2169 G_RAID_DEBUG1(1, sc,
2170 "%d volumes are still open.",
2172 sx_xunlock(&sc->sc_lock);
2174 case G_RAID_DESTROY_DELAYED:
2175 G_RAID_DEBUG1(1, sc,
2176 "Array will be destroyed on last close.");
2177 sc->sc_stopping = G_RAID_DESTROY_DELAYED;
2178 sx_xunlock(&sc->sc_lock);
2180 case G_RAID_DESTROY_HARD:
2181 G_RAID_DEBUG1(1, sc,
2182 "%d volumes are still open.",
2187 /* Mark node for destruction. */
2188 sc->sc_stopping = G_RAID_DESTROY_HARD;
2189 /* Wake up worker to let it selfdestruct. */
2190 g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
2191 /* Sleep until node destroyed. */
2192 error = sx_sleep(&sc->sc_stopping, &sc->sc_lock,
2193 PRIBIO | PDROP, "r:destroy", hz * 3);
2194 return (error == EWOULDBLOCK ? EBUSY : 0);
2198 g_raid_taste_orphan(struct g_consumer *cp)
2201 KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
2202 cp->provider->name));
2205 static struct g_geom *
2206 g_raid_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
2208 struct g_consumer *cp;
2209 struct g_geom *gp, *geom;
2210 struct g_raid_md_class *class;
2211 struct g_raid_md_object *obj;
2214 g_topology_assert();
2215 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
2218 G_RAID_DEBUG(2, "Tasting provider %s.", pp->name);
2221 status = G_RAID_MD_TASTE_FAIL;
2222 gp = g_new_geomf(mp, "raid:taste");
2224 * This orphan function should be never called.
2226 gp->orphan = g_raid_taste_orphan;
2227 cp = g_new_consumer(gp);
2228 cp->flags |= G_CF_DIRECT_RECEIVE;
2230 if (g_access(cp, 1, 0, 0) != 0)
2233 LIST_FOREACH(class, &g_raid_md_classes, mdc_list) {
2234 if (!class->mdc_enable)
2236 G_RAID_DEBUG(2, "Tasting provider %s for %s metadata.",
2237 pp->name, class->name);
2238 obj = (void *)kobj_create((kobj_class_t)class, M_RAID,
2240 obj->mdo_class = class;
2241 status = G_RAID_MD_TASTE(obj, mp, cp, &geom);
2242 if (status != G_RAID_MD_TASTE_NEW)
2243 kobj_delete((kobj_t)obj, M_RAID);
2244 if (status != G_RAID_MD_TASTE_FAIL)
2248 if (status == G_RAID_MD_TASTE_FAIL)
2249 (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);