g_raid.c revision 242323
1219974Smav/*- 2219974Smav * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org> 3219974Smav * All rights reserved. 4219974Smav * 5219974Smav * Redistribution and use in source and binary forms, with or without 6219974Smav * modification, are permitted provided that the following conditions 7219974Smav * are met: 8219974Smav * 1. Redistributions of source code must retain the above copyright 9219974Smav * notice, this list of conditions and the following disclaimer. 10219974Smav * 2. Redistributions in binary form must reproduce the above copyright 11219974Smav * notice, this list of conditions and the following disclaimer in the 12219974Smav * documentation and/or other materials provided with the distribution. 13219974Smav * 14219974Smav * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 15219974Smav * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16219974Smav * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17219974Smav * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 18219974Smav * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19219974Smav * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20219974Smav * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21219974Smav * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22219974Smav * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23219974Smav * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24219974Smav * SUCH DAMAGE. 25219974Smav */ 26219974Smav 27219974Smav#include <sys/cdefs.h> 28219974Smav__FBSDID("$FreeBSD: head/sys/geom/raid/g_raid.c 242323 2012-10-29 18:04:38Z mav $"); 29219974Smav 30219974Smav#include <sys/param.h> 31219974Smav#include <sys/systm.h> 32219974Smav#include <sys/kernel.h> 33219974Smav#include <sys/module.h> 34219974Smav#include <sys/limits.h> 35219974Smav#include <sys/lock.h> 36219974Smav#include <sys/mutex.h> 37219974Smav#include <sys/bio.h> 38223921Sae#include <sys/sbuf.h> 39219974Smav#include <sys/sysctl.h> 40219974Smav#include <sys/malloc.h> 41219974Smav#include <sys/eventhandler.h> 42219974Smav#include <vm/uma.h> 43219974Smav#include <geom/geom.h> 44219974Smav#include <sys/proc.h> 45219974Smav#include <sys/kthread.h> 46219974Smav#include <sys/sched.h> 47219974Smav#include <geom/raid/g_raid.h> 48219974Smav#include "g_raid_md_if.h" 49219974Smav#include "g_raid_tr_if.h" 50219974Smav 51219974Smavstatic MALLOC_DEFINE(M_RAID, "raid_data", "GEOM_RAID Data"); 52219974Smav 53219974SmavSYSCTL_DECL(_kern_geom); 54219974SmavSYSCTL_NODE(_kern_geom, OID_AUTO, raid, CTLFLAG_RW, 0, "GEOM_RAID stuff"); 55240465Smavint g_raid_enable = 1; 56240465SmavTUNABLE_INT("kern.geom.raid.enable", &g_raid_enable); 57240465SmavSYSCTL_INT(_kern_geom_raid, OID_AUTO, enable, CTLFLAG_RW, 58240465Smav &g_raid_enable, 0, "Enable on-disk metadata taste"); 59219974Smavu_int g_raid_aggressive_spare = 0; 60219974SmavTUNABLE_INT("kern.geom.raid.aggressive_spare", &g_raid_aggressive_spare); 61219974SmavSYSCTL_UINT(_kern_geom_raid, OID_AUTO, aggressive_spare, CTLFLAG_RW, 62219974Smav &g_raid_aggressive_spare, 0, "Use disks without metadata as spare"); 63220790Smavu_int g_raid_debug = 0; 64219974SmavTUNABLE_INT("kern.geom.raid.debug", &g_raid_debug); 65219974SmavSYSCTL_UINT(_kern_geom_raid, OID_AUTO, debug, CTLFLAG_RW, &g_raid_debug, 0, 66219974Smav "Debug level"); 67219974Smavint g_raid_read_err_thresh = 10; 68219974SmavTUNABLE_INT("kern.geom.raid.read_err_thresh", &g_raid_read_err_thresh); 69219974SmavSYSCTL_UINT(_kern_geom_raid, OID_AUTO, read_err_thresh, CTLFLAG_RW, 70219974Smav &g_raid_read_err_thresh, 0, 71219974Smav "Number of read errors equated to disk failure"); 72219974Smavu_int g_raid_start_timeout = 30; 73219974SmavTUNABLE_INT("kern.geom.raid.start_timeout", &g_raid_start_timeout); 74219974SmavSYSCTL_UINT(_kern_geom_raid, OID_AUTO, start_timeout, CTLFLAG_RW, 75219974Smav &g_raid_start_timeout, 0, 76219974Smav "Time to wait for all array components"); 77219974Smavstatic u_int g_raid_clean_time = 5; 78219974SmavTUNABLE_INT("kern.geom.raid.clean_time", &g_raid_clean_time); 79219974SmavSYSCTL_UINT(_kern_geom_raid, OID_AUTO, clean_time, CTLFLAG_RW, 80219974Smav &g_raid_clean_time, 0, "Mark volume as clean when idling"); 81219974Smavstatic u_int g_raid_disconnect_on_failure = 1; 82219974SmavTUNABLE_INT("kern.geom.raid.disconnect_on_failure", 83219974Smav &g_raid_disconnect_on_failure); 84219974SmavSYSCTL_UINT(_kern_geom_raid, OID_AUTO, disconnect_on_failure, CTLFLAG_RW, 85219974Smav &g_raid_disconnect_on_failure, 0, "Disconnect component on I/O failure."); 86219974Smavstatic u_int g_raid_name_format = 0; 87219974SmavTUNABLE_INT("kern.geom.raid.name_format", &g_raid_name_format); 88219974SmavSYSCTL_UINT(_kern_geom_raid, OID_AUTO, name_format, CTLFLAG_RW, 89219974Smav &g_raid_name_format, 0, "Providers name format."); 90219974Smavstatic u_int g_raid_idle_threshold = 1000000; 91219974SmavTUNABLE_INT("kern.geom.raid.idle_threshold", &g_raid_idle_threshold); 92219974SmavSYSCTL_UINT(_kern_geom_raid, OID_AUTO, idle_threshold, CTLFLAG_RW, 93219974Smav &g_raid_idle_threshold, 1000000, 94219974Smav "Time in microseconds to consider a volume idle."); 95219974Smav 96219974Smav#define MSLEEP(rv, ident, mtx, priority, wmesg, timeout) do { \ 97219974Smav G_RAID_DEBUG(4, "%s: Sleeping %p.", __func__, (ident)); \ 98219974Smav rv = msleep((ident), (mtx), (priority), (wmesg), (timeout)); \ 99219974Smav G_RAID_DEBUG(4, "%s: Woken up %p.", __func__, (ident)); \ 100219974Smav} while (0) 101219974Smav 102219974SmavLIST_HEAD(, g_raid_md_class) g_raid_md_classes = 103219974Smav LIST_HEAD_INITIALIZER(g_raid_md_classes); 104219974Smav 105219974SmavLIST_HEAD(, g_raid_tr_class) g_raid_tr_classes = 106219974Smav LIST_HEAD_INITIALIZER(g_raid_tr_classes); 107219974Smav 108219974SmavLIST_HEAD(, g_raid_volume) g_raid_volumes = 109219974Smav LIST_HEAD_INITIALIZER(g_raid_volumes); 110219974Smav 111242314Smavstatic eventhandler_tag g_raid_post_sync = NULL; 112219974Smavstatic int g_raid_started = 0; 113242314Smavstatic int g_raid_shutdown = 0; 114219974Smav 115219974Smavstatic int g_raid_destroy_geom(struct gctl_req *req, struct g_class *mp, 116219974Smav struct g_geom *gp); 117219974Smavstatic g_taste_t g_raid_taste; 118219974Smavstatic void g_raid_init(struct g_class *mp); 119219974Smavstatic void g_raid_fini(struct g_class *mp); 120219974Smav 121219974Smavstruct g_class g_raid_class = { 122219974Smav .name = G_RAID_CLASS_NAME, 123219974Smav .version = G_VERSION, 124219974Smav .ctlreq = g_raid_ctl, 125219974Smav .taste = g_raid_taste, 126219974Smav .destroy_geom = g_raid_destroy_geom, 127219974Smav .init = g_raid_init, 128219974Smav .fini = g_raid_fini 129219974Smav}; 130219974Smav 131219974Smavstatic void g_raid_destroy_provider(struct g_raid_volume *vol); 132219974Smavstatic int g_raid_update_disk(struct g_raid_disk *disk, u_int event); 133219974Smavstatic int g_raid_update_subdisk(struct g_raid_subdisk *subdisk, u_int event); 134219974Smavstatic int g_raid_update_volume(struct g_raid_volume *vol, u_int event); 135219974Smavstatic int g_raid_update_node(struct g_raid_softc *sc, u_int event); 136219974Smavstatic void g_raid_dumpconf(struct sbuf *sb, const char *indent, 137219974Smav struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp); 138219974Smavstatic void g_raid_start(struct bio *bp); 139219974Smavstatic void g_raid_start_request(struct bio *bp); 140219974Smavstatic void g_raid_disk_done(struct bio *bp); 141219974Smavstatic void g_raid_poll(struct g_raid_softc *sc); 142219974Smav 143219974Smavstatic const char * 144219974Smavg_raid_node_event2str(int event) 145219974Smav{ 146219974Smav 147219974Smav switch (event) { 148219974Smav case G_RAID_NODE_E_WAKE: 149219974Smav return ("WAKE"); 150219974Smav case G_RAID_NODE_E_START: 151219974Smav return ("START"); 152219974Smav default: 153219974Smav return ("INVALID"); 154219974Smav } 155219974Smav} 156219974Smav 157219974Smavconst char * 158219974Smavg_raid_disk_state2str(int state) 159219974Smav{ 160219974Smav 161219974Smav switch (state) { 162219974Smav case G_RAID_DISK_S_NONE: 163219974Smav return ("NONE"); 164219974Smav case G_RAID_DISK_S_OFFLINE: 165219974Smav return ("OFFLINE"); 166219974Smav case G_RAID_DISK_S_FAILED: 167219974Smav return ("FAILED"); 168219974Smav case G_RAID_DISK_S_STALE_FAILED: 169219974Smav return ("STALE_FAILED"); 170219974Smav case G_RAID_DISK_S_SPARE: 171219974Smav return ("SPARE"); 172219974Smav case G_RAID_DISK_S_STALE: 173219974Smav return ("STALE"); 174219974Smav case G_RAID_DISK_S_ACTIVE: 175219974Smav return ("ACTIVE"); 176219974Smav default: 177219974Smav return ("INVALID"); 178219974Smav } 179219974Smav} 180219974Smav 181219974Smavstatic const char * 182219974Smavg_raid_disk_event2str(int event) 183219974Smav{ 184219974Smav 185219974Smav switch (event) { 186219974Smav case G_RAID_DISK_E_DISCONNECTED: 187219974Smav return ("DISCONNECTED"); 188219974Smav default: 189219974Smav return ("INVALID"); 190219974Smav } 191219974Smav} 192219974Smav 193219974Smavconst char * 194219974Smavg_raid_subdisk_state2str(int state) 195219974Smav{ 196219974Smav 197219974Smav switch (state) { 198219974Smav case G_RAID_SUBDISK_S_NONE: 199219974Smav return ("NONE"); 200219974Smav case G_RAID_SUBDISK_S_FAILED: 201219974Smav return ("FAILED"); 202219974Smav case G_RAID_SUBDISK_S_NEW: 203219974Smav return ("NEW"); 204219974Smav case G_RAID_SUBDISK_S_REBUILD: 205219974Smav return ("REBUILD"); 206219974Smav case G_RAID_SUBDISK_S_UNINITIALIZED: 207219974Smav return ("UNINITIALIZED"); 208219974Smav case G_RAID_SUBDISK_S_STALE: 209219974Smav return ("STALE"); 210219974Smav case G_RAID_SUBDISK_S_RESYNC: 211219974Smav return ("RESYNC"); 212219974Smav case G_RAID_SUBDISK_S_ACTIVE: 213219974Smav return ("ACTIVE"); 214219974Smav default: 215219974Smav return ("INVALID"); 216219974Smav } 217219974Smav} 218219974Smav 219219974Smavstatic const char * 220219974Smavg_raid_subdisk_event2str(int event) 221219974Smav{ 222219974Smav 223219974Smav switch (event) { 224219974Smav case G_RAID_SUBDISK_E_NEW: 225219974Smav return ("NEW"); 226239175Smav case G_RAID_SUBDISK_E_FAILED: 227239175Smav return ("FAILED"); 228219974Smav case G_RAID_SUBDISK_E_DISCONNECTED: 229219974Smav return ("DISCONNECTED"); 230219974Smav default: 231219974Smav return ("INVALID"); 232219974Smav } 233219974Smav} 234219974Smav 235219974Smavconst char * 236219974Smavg_raid_volume_state2str(int state) 237219974Smav{ 238219974Smav 239219974Smav switch (state) { 240219974Smav case G_RAID_VOLUME_S_STARTING: 241219974Smav return ("STARTING"); 242219974Smav case G_RAID_VOLUME_S_BROKEN: 243219974Smav return ("BROKEN"); 244219974Smav case G_RAID_VOLUME_S_DEGRADED: 245219974Smav return ("DEGRADED"); 246219974Smav case G_RAID_VOLUME_S_SUBOPTIMAL: 247219974Smav return ("SUBOPTIMAL"); 248219974Smav case G_RAID_VOLUME_S_OPTIMAL: 249219974Smav return ("OPTIMAL"); 250219974Smav case G_RAID_VOLUME_S_UNSUPPORTED: 251219974Smav return ("UNSUPPORTED"); 252219974Smav case G_RAID_VOLUME_S_STOPPED: 253219974Smav return ("STOPPED"); 254219974Smav default: 255219974Smav return ("INVALID"); 256219974Smav } 257219974Smav} 258219974Smav 259219974Smavstatic const char * 260219974Smavg_raid_volume_event2str(int event) 261219974Smav{ 262219974Smav 263219974Smav switch (event) { 264219974Smav case G_RAID_VOLUME_E_UP: 265219974Smav return ("UP"); 266219974Smav case G_RAID_VOLUME_E_DOWN: 267219974Smav return ("DOWN"); 268219974Smav case G_RAID_VOLUME_E_START: 269219974Smav return ("START"); 270219974Smav case G_RAID_VOLUME_E_STARTMD: 271219974Smav return ("STARTMD"); 272219974Smav default: 273219974Smav return ("INVALID"); 274219974Smav } 275219974Smav} 276219974Smav 277219974Smavconst char * 278219974Smavg_raid_volume_level2str(int level, int qual) 279219974Smav{ 280219974Smav 281219974Smav switch (level) { 282219974Smav case G_RAID_VOLUME_RL_RAID0: 283219974Smav return ("RAID0"); 284219974Smav case G_RAID_VOLUME_RL_RAID1: 285219974Smav return ("RAID1"); 286219974Smav case G_RAID_VOLUME_RL_RAID3: 287234603Smav if (qual == G_RAID_VOLUME_RLQ_R3P0) 288234603Smav return ("RAID3-P0"); 289234603Smav if (qual == G_RAID_VOLUME_RLQ_R3PN) 290234603Smav return ("RAID3-PN"); 291219974Smav return ("RAID3"); 292219974Smav case G_RAID_VOLUME_RL_RAID4: 293234603Smav if (qual == G_RAID_VOLUME_RLQ_R4P0) 294234610Smav return ("RAID4-P0"); 295234603Smav if (qual == G_RAID_VOLUME_RLQ_R4PN) 296234610Smav return ("RAID4-PN"); 297219974Smav return ("RAID4"); 298219974Smav case G_RAID_VOLUME_RL_RAID5: 299234458Smav if (qual == G_RAID_VOLUME_RLQ_R5RA) 300234603Smav return ("RAID5-RA"); 301234458Smav if (qual == G_RAID_VOLUME_RLQ_R5RS) 302234603Smav return ("RAID5-RS"); 303234458Smav if (qual == G_RAID_VOLUME_RLQ_R5LA) 304234603Smav return ("RAID5-LA"); 305234458Smav if (qual == G_RAID_VOLUME_RLQ_R5LS) 306234603Smav return ("RAID5-LS"); 307219974Smav return ("RAID5"); 308219974Smav case G_RAID_VOLUME_RL_RAID6: 309234603Smav if (qual == G_RAID_VOLUME_RLQ_R6RA) 310234603Smav return ("RAID6-RA"); 311234603Smav if (qual == G_RAID_VOLUME_RLQ_R6RS) 312234603Smav return ("RAID6-RS"); 313234603Smav if (qual == G_RAID_VOLUME_RLQ_R6LA) 314234603Smav return ("RAID6-LA"); 315234603Smav if (qual == G_RAID_VOLUME_RLQ_R6LS) 316234603Smav return ("RAID6-LS"); 317219974Smav return ("RAID6"); 318234603Smav case G_RAID_VOLUME_RL_RAIDMDF: 319234603Smav if (qual == G_RAID_VOLUME_RLQ_RMDFRA) 320234603Smav return ("RAIDMDF-RA"); 321234603Smav if (qual == G_RAID_VOLUME_RLQ_RMDFRS) 322234603Smav return ("RAIDMDF-RS"); 323234603Smav if (qual == G_RAID_VOLUME_RLQ_RMDFLA) 324234603Smav return ("RAIDMDF-LA"); 325234603Smav if (qual == G_RAID_VOLUME_RLQ_RMDFLS) 326234603Smav return ("RAIDMDF-LS"); 327234603Smav return ("RAIDMDF"); 328219974Smav case G_RAID_VOLUME_RL_RAID1E: 329234603Smav if (qual == G_RAID_VOLUME_RLQ_R1EA) 330234603Smav return ("RAID1E-A"); 331234603Smav if (qual == G_RAID_VOLUME_RLQ_R1EO) 332234603Smav return ("RAID1E-O"); 333219974Smav return ("RAID1E"); 334219974Smav case G_RAID_VOLUME_RL_SINGLE: 335219974Smav return ("SINGLE"); 336219974Smav case G_RAID_VOLUME_RL_CONCAT: 337219974Smav return ("CONCAT"); 338219974Smav case G_RAID_VOLUME_RL_RAID5E: 339234603Smav if (qual == G_RAID_VOLUME_RLQ_R5ERA) 340234603Smav return ("RAID5E-RA"); 341234603Smav if (qual == G_RAID_VOLUME_RLQ_R5ERS) 342234603Smav return ("RAID5E-RS"); 343234603Smav if (qual == G_RAID_VOLUME_RLQ_R5ELA) 344234603Smav return ("RAID5E-LA"); 345234603Smav if (qual == G_RAID_VOLUME_RLQ_R5ELS) 346234603Smav return ("RAID5E-LS"); 347219974Smav return ("RAID5E"); 348219974Smav case G_RAID_VOLUME_RL_RAID5EE: 349234603Smav if (qual == G_RAID_VOLUME_RLQ_R5EERA) 350234603Smav return ("RAID5EE-RA"); 351234603Smav if (qual == G_RAID_VOLUME_RLQ_R5EERS) 352234603Smav return ("RAID5EE-RS"); 353234603Smav if (qual == G_RAID_VOLUME_RLQ_R5EELA) 354234603Smav return ("RAID5EE-LA"); 355234603Smav if (qual == G_RAID_VOLUME_RLQ_R5EELS) 356234603Smav return ("RAID5EE-LS"); 357219974Smav return ("RAID5EE"); 358234603Smav case G_RAID_VOLUME_RL_RAID5R: 359234603Smav if (qual == G_RAID_VOLUME_RLQ_R5RRA) 360234603Smav return ("RAID5R-RA"); 361234603Smav if (qual == G_RAID_VOLUME_RLQ_R5RRS) 362234603Smav return ("RAID5R-RS"); 363234603Smav if (qual == G_RAID_VOLUME_RLQ_R5RLA) 364234603Smav return ("RAID5R-LA"); 365234603Smav if (qual == G_RAID_VOLUME_RLQ_R5RLS) 366234603Smav return ("RAID5R-LS"); 367234603Smav return ("RAID5E"); 368219974Smav default: 369219974Smav return ("UNKNOWN"); 370219974Smav } 371219974Smav} 372219974Smav 373219974Smavint 374219974Smavg_raid_volume_str2level(const char *str, int *level, int *qual) 375219974Smav{ 376219974Smav 377219974Smav *level = G_RAID_VOLUME_RL_UNKNOWN; 378219974Smav *qual = G_RAID_VOLUME_RLQ_NONE; 379219974Smav if (strcasecmp(str, "RAID0") == 0) 380219974Smav *level = G_RAID_VOLUME_RL_RAID0; 381219974Smav else if (strcasecmp(str, "RAID1") == 0) 382219974Smav *level = G_RAID_VOLUME_RL_RAID1; 383234603Smav else if (strcasecmp(str, "RAID3-P0") == 0) { 384219974Smav *level = G_RAID_VOLUME_RL_RAID3; 385234603Smav *qual = G_RAID_VOLUME_RLQ_R3P0; 386234993Smav } else if (strcasecmp(str, "RAID3-PN") == 0 || 387234603Smav strcasecmp(str, "RAID3") == 0) { 388234603Smav *level = G_RAID_VOLUME_RL_RAID3; 389234993Smav *qual = G_RAID_VOLUME_RLQ_R3PN; 390234603Smav } else if (strcasecmp(str, "RAID4-P0") == 0) { 391219974Smav *level = G_RAID_VOLUME_RL_RAID4; 392234603Smav *qual = G_RAID_VOLUME_RLQ_R4P0; 393234993Smav } else if (strcasecmp(str, "RAID4-PN") == 0 || 394234603Smav strcasecmp(str, "RAID4") == 0) { 395234603Smav *level = G_RAID_VOLUME_RL_RAID4; 396234993Smav *qual = G_RAID_VOLUME_RLQ_R4PN; 397234603Smav } else if (strcasecmp(str, "RAID5-RA") == 0) { 398219974Smav *level = G_RAID_VOLUME_RL_RAID5; 399234458Smav *qual = G_RAID_VOLUME_RLQ_R5RA; 400234603Smav } else if (strcasecmp(str, "RAID5-RS") == 0) { 401234458Smav *level = G_RAID_VOLUME_RL_RAID5; 402234458Smav *qual = G_RAID_VOLUME_RLQ_R5RS; 403234458Smav } else if (strcasecmp(str, "RAID5") == 0 || 404234603Smav strcasecmp(str, "RAID5-LA") == 0) { 405234458Smav *level = G_RAID_VOLUME_RL_RAID5; 406234458Smav *qual = G_RAID_VOLUME_RLQ_R5LA; 407234603Smav } else if (strcasecmp(str, "RAID5-LS") == 0) { 408234458Smav *level = G_RAID_VOLUME_RL_RAID5; 409234458Smav *qual = G_RAID_VOLUME_RLQ_R5LS; 410234603Smav } else if (strcasecmp(str, "RAID6-RA") == 0) { 411219974Smav *level = G_RAID_VOLUME_RL_RAID6; 412234603Smav *qual = G_RAID_VOLUME_RLQ_R6RA; 413234603Smav } else if (strcasecmp(str, "RAID6-RS") == 0) { 414234603Smav *level = G_RAID_VOLUME_RL_RAID6; 415234603Smav *qual = G_RAID_VOLUME_RLQ_R6RS; 416234603Smav } else if (strcasecmp(str, "RAID6") == 0 || 417234603Smav strcasecmp(str, "RAID6-LA") == 0) { 418234603Smav *level = G_RAID_VOLUME_RL_RAID6; 419234603Smav *qual = G_RAID_VOLUME_RLQ_R6LA; 420234603Smav } else if (strcasecmp(str, "RAID6-LS") == 0) { 421234603Smav *level = G_RAID_VOLUME_RL_RAID6; 422234603Smav *qual = G_RAID_VOLUME_RLQ_R6LS; 423234603Smav } else if (strcasecmp(str, "RAIDMDF-RA") == 0) { 424234603Smav *level = G_RAID_VOLUME_RL_RAIDMDF; 425234603Smav *qual = G_RAID_VOLUME_RLQ_RMDFRA; 426234603Smav } else if (strcasecmp(str, "RAIDMDF-RS") == 0) { 427234603Smav *level = G_RAID_VOLUME_RL_RAIDMDF; 428234603Smav *qual = G_RAID_VOLUME_RLQ_RMDFRS; 429234603Smav } else if (strcasecmp(str, "RAIDMDF") == 0 || 430234603Smav strcasecmp(str, "RAIDMDF-LA") == 0) { 431234603Smav *level = G_RAID_VOLUME_RL_RAIDMDF; 432234603Smav *qual = G_RAID_VOLUME_RLQ_RMDFLA; 433234603Smav } else if (strcasecmp(str, "RAIDMDF-LS") == 0) { 434234603Smav *level = G_RAID_VOLUME_RL_RAIDMDF; 435234603Smav *qual = G_RAID_VOLUME_RLQ_RMDFLS; 436234603Smav } else if (strcasecmp(str, "RAID10") == 0 || 437234603Smav strcasecmp(str, "RAID1E") == 0 || 438234603Smav strcasecmp(str, "RAID1E-A") == 0) { 439219974Smav *level = G_RAID_VOLUME_RL_RAID1E; 440234603Smav *qual = G_RAID_VOLUME_RLQ_R1EA; 441234603Smav } else if (strcasecmp(str, "RAID1E-O") == 0) { 442234603Smav *level = G_RAID_VOLUME_RL_RAID1E; 443234603Smav *qual = G_RAID_VOLUME_RLQ_R1EO; 444234603Smav } else if (strcasecmp(str, "SINGLE") == 0) 445219974Smav *level = G_RAID_VOLUME_RL_SINGLE; 446219974Smav else if (strcasecmp(str, "CONCAT") == 0) 447219974Smav *level = G_RAID_VOLUME_RL_CONCAT; 448234603Smav else if (strcasecmp(str, "RAID5E-RA") == 0) { 449219974Smav *level = G_RAID_VOLUME_RL_RAID5E; 450234603Smav *qual = G_RAID_VOLUME_RLQ_R5ERA; 451234603Smav } else if (strcasecmp(str, "RAID5E-RS") == 0) { 452234603Smav *level = G_RAID_VOLUME_RL_RAID5E; 453234603Smav *qual = G_RAID_VOLUME_RLQ_R5ERS; 454234603Smav } else if (strcasecmp(str, "RAID5E") == 0 || 455234603Smav strcasecmp(str, "RAID5E-LA") == 0) { 456234603Smav *level = G_RAID_VOLUME_RL_RAID5E; 457234603Smav *qual = G_RAID_VOLUME_RLQ_R5ELA; 458234603Smav } else if (strcasecmp(str, "RAID5E-LS") == 0) { 459234603Smav *level = G_RAID_VOLUME_RL_RAID5E; 460234603Smav *qual = G_RAID_VOLUME_RLQ_R5ELS; 461234603Smav } else if (strcasecmp(str, "RAID5EE-RA") == 0) { 462219974Smav *level = G_RAID_VOLUME_RL_RAID5EE; 463234603Smav *qual = G_RAID_VOLUME_RLQ_R5EERA; 464234603Smav } else if (strcasecmp(str, "RAID5EE-RS") == 0) { 465234603Smav *level = G_RAID_VOLUME_RL_RAID5EE; 466234603Smav *qual = G_RAID_VOLUME_RLQ_R5EERS; 467234603Smav } else if (strcasecmp(str, "RAID5EE") == 0 || 468234603Smav strcasecmp(str, "RAID5EE-LA") == 0) { 469234603Smav *level = G_RAID_VOLUME_RL_RAID5EE; 470234603Smav *qual = G_RAID_VOLUME_RLQ_R5EELA; 471234603Smav } else if (strcasecmp(str, "RAID5EE-LS") == 0) { 472234603Smav *level = G_RAID_VOLUME_RL_RAID5EE; 473234603Smav *qual = G_RAID_VOLUME_RLQ_R5EELS; 474234603Smav } else if (strcasecmp(str, "RAID5R-RA") == 0) { 475234603Smav *level = G_RAID_VOLUME_RL_RAID5R; 476234603Smav *qual = G_RAID_VOLUME_RLQ_R5RRA; 477234603Smav } else if (strcasecmp(str, "RAID5R-RS") == 0) { 478234603Smav *level = G_RAID_VOLUME_RL_RAID5R; 479234603Smav *qual = G_RAID_VOLUME_RLQ_R5RRS; 480234603Smav } else if (strcasecmp(str, "RAID5R") == 0 || 481234603Smav strcasecmp(str, "RAID5R-LA") == 0) { 482234603Smav *level = G_RAID_VOLUME_RL_RAID5R; 483234603Smav *qual = G_RAID_VOLUME_RLQ_R5RLA; 484234603Smav } else if (strcasecmp(str, "RAID5R-LS") == 0) { 485234603Smav *level = G_RAID_VOLUME_RL_RAID5R; 486234603Smav *qual = G_RAID_VOLUME_RLQ_R5RLS; 487234603Smav } else 488219974Smav return (-1); 489219974Smav return (0); 490219974Smav} 491219974Smav 492219974Smavconst char * 493219974Smavg_raid_get_diskname(struct g_raid_disk *disk) 494219974Smav{ 495219974Smav 496219974Smav if (disk->d_consumer == NULL || disk->d_consumer->provider == NULL) 497219974Smav return ("[unknown]"); 498219974Smav return (disk->d_consumer->provider->name); 499219974Smav} 500219974Smav 501219974Smavvoid 502242323Smavg_raid_get_disk_info(struct g_raid_disk *disk) 503242323Smav{ 504242323Smav struct g_consumer *cp = disk->d_consumer; 505242323Smav int error, len; 506242323Smav 507242323Smav /* Read kernel dumping information. */ 508242323Smav disk->d_kd.offset = 0; 509242323Smav disk->d_kd.length = OFF_MAX; 510242323Smav len = sizeof(disk->d_kd); 511242323Smav error = g_io_getattr("GEOM::kerneldump", cp, &len, &disk->d_kd); 512242323Smav if (error) 513242323Smav disk->d_kd.di.dumper = NULL; 514242323Smav if (disk->d_kd.di.dumper == NULL) 515242323Smav G_RAID_DEBUG1(2, disk->d_softc, 516242323Smav "Dumping not supported by %s: %d.", 517242323Smav cp->provider->name, error); 518242323Smav 519242323Smav /* Read BIO_DELETE support. */ 520242323Smav error = g_getattr("GEOM::candelete", cp, &disk->d_candelete); 521242323Smav if (error) 522242323Smav disk->d_candelete = 0; 523242323Smav if (!disk->d_candelete) 524242323Smav G_RAID_DEBUG1(2, disk->d_softc, 525242323Smav "BIO_DELETE not supported by %s: %d.", 526242323Smav cp->provider->name, error); 527242323Smav} 528242323Smav 529242323Smavvoid 530219974Smavg_raid_report_disk_state(struct g_raid_disk *disk) 531219974Smav{ 532219974Smav struct g_raid_subdisk *sd; 533219974Smav int len, state; 534219974Smav uint32_t s; 535219974Smav 536219974Smav if (disk->d_consumer == NULL) 537219974Smav return; 538219974Smav if (disk->d_state == G_RAID_DISK_S_FAILED || 539219974Smav disk->d_state == G_RAID_DISK_S_STALE_FAILED) { 540219974Smav s = G_STATE_FAILED; 541219974Smav } else { 542219974Smav state = G_RAID_SUBDISK_S_ACTIVE; 543219974Smav TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) { 544219974Smav if (sd->sd_state < state) 545219974Smav state = sd->sd_state; 546219974Smav } 547219974Smav if (state == G_RAID_SUBDISK_S_FAILED) 548219974Smav s = G_STATE_FAILED; 549219974Smav else if (state == G_RAID_SUBDISK_S_NEW || 550219974Smav state == G_RAID_SUBDISK_S_REBUILD) 551219974Smav s = G_STATE_REBUILD; 552219974Smav else if (state == G_RAID_SUBDISK_S_STALE || 553219974Smav state == G_RAID_SUBDISK_S_RESYNC) 554219974Smav s = G_STATE_RESYNC; 555219974Smav else 556219974Smav s = G_STATE_ACTIVE; 557219974Smav } 558219974Smav len = sizeof(s); 559219974Smav g_io_getattr("GEOM::setstate", disk->d_consumer, &len, &s); 560219974Smav G_RAID_DEBUG1(2, disk->d_softc, "Disk %s state reported as %d.", 561219974Smav g_raid_get_diskname(disk), s); 562219974Smav} 563219974Smav 564219974Smavvoid 565219974Smavg_raid_change_disk_state(struct g_raid_disk *disk, int state) 566219974Smav{ 567219974Smav 568219974Smav G_RAID_DEBUG1(0, disk->d_softc, "Disk %s state changed from %s to %s.", 569219974Smav g_raid_get_diskname(disk), 570219974Smav g_raid_disk_state2str(disk->d_state), 571219974Smav g_raid_disk_state2str(state)); 572219974Smav disk->d_state = state; 573219974Smav g_raid_report_disk_state(disk); 574219974Smav} 575219974Smav 576219974Smavvoid 577219974Smavg_raid_change_subdisk_state(struct g_raid_subdisk *sd, int state) 578219974Smav{ 579219974Smav 580219974Smav G_RAID_DEBUG1(0, sd->sd_softc, 581219974Smav "Subdisk %s:%d-%s state changed from %s to %s.", 582219974Smav sd->sd_volume->v_name, sd->sd_pos, 583219974Smav sd->sd_disk ? g_raid_get_diskname(sd->sd_disk) : "[none]", 584219974Smav g_raid_subdisk_state2str(sd->sd_state), 585219974Smav g_raid_subdisk_state2str(state)); 586219974Smav sd->sd_state = state; 587219974Smav if (sd->sd_disk) 588219974Smav g_raid_report_disk_state(sd->sd_disk); 589219974Smav} 590219974Smav 591219974Smavvoid 592219974Smavg_raid_change_volume_state(struct g_raid_volume *vol, int state) 593219974Smav{ 594219974Smav 595219974Smav G_RAID_DEBUG1(0, vol->v_softc, 596219974Smav "Volume %s state changed from %s to %s.", 597219974Smav vol->v_name, 598219974Smav g_raid_volume_state2str(vol->v_state), 599219974Smav g_raid_volume_state2str(state)); 600219974Smav vol->v_state = state; 601219974Smav} 602219974Smav 603219974Smav/* 604219974Smav * --- Events handling functions --- 605219974Smav * Events in geom_raid are used to maintain subdisks and volumes status 606219974Smav * from one thread to simplify locking. 607219974Smav */ 608219974Smavstatic void 609219974Smavg_raid_event_free(struct g_raid_event *ep) 610219974Smav{ 611219974Smav 612219974Smav free(ep, M_RAID); 613219974Smav} 614219974Smav 615219974Smavint 616219974Smavg_raid_event_send(void *arg, int event, int flags) 617219974Smav{ 618219974Smav struct g_raid_softc *sc; 619219974Smav struct g_raid_event *ep; 620219974Smav int error; 621219974Smav 622219974Smav if ((flags & G_RAID_EVENT_VOLUME) != 0) { 623219974Smav sc = ((struct g_raid_volume *)arg)->v_softc; 624219974Smav } else if ((flags & G_RAID_EVENT_DISK) != 0) { 625219974Smav sc = ((struct g_raid_disk *)arg)->d_softc; 626219974Smav } else if ((flags & G_RAID_EVENT_SUBDISK) != 0) { 627219974Smav sc = ((struct g_raid_subdisk *)arg)->sd_softc; 628219974Smav } else { 629219974Smav sc = arg; 630219974Smav } 631219974Smav ep = malloc(sizeof(*ep), M_RAID, 632219974Smav sx_xlocked(&sc->sc_lock) ? M_WAITOK : M_NOWAIT); 633219974Smav if (ep == NULL) 634219974Smav return (ENOMEM); 635219974Smav ep->e_tgt = arg; 636219974Smav ep->e_event = event; 637219974Smav ep->e_flags = flags; 638219974Smav ep->e_error = 0; 639219974Smav G_RAID_DEBUG1(4, sc, "Sending event %p. Waking up %p.", ep, sc); 640219974Smav mtx_lock(&sc->sc_queue_mtx); 641219974Smav TAILQ_INSERT_TAIL(&sc->sc_events, ep, e_next); 642219974Smav mtx_unlock(&sc->sc_queue_mtx); 643219974Smav wakeup(sc); 644219974Smav 645219974Smav if ((flags & G_RAID_EVENT_WAIT) == 0) 646219974Smav return (0); 647219974Smav 648219974Smav sx_assert(&sc->sc_lock, SX_XLOCKED); 649219974Smav G_RAID_DEBUG1(4, sc, "Sleeping on %p.", ep); 650219974Smav sx_xunlock(&sc->sc_lock); 651219974Smav while ((ep->e_flags & G_RAID_EVENT_DONE) == 0) { 652219974Smav mtx_lock(&sc->sc_queue_mtx); 653219974Smav MSLEEP(error, ep, &sc->sc_queue_mtx, PRIBIO | PDROP, "m:event", 654219974Smav hz * 5); 655219974Smav } 656219974Smav error = ep->e_error; 657219974Smav g_raid_event_free(ep); 658219974Smav sx_xlock(&sc->sc_lock); 659219974Smav return (error); 660219974Smav} 661219974Smav 662219974Smavstatic void 663219974Smavg_raid_event_cancel(struct g_raid_softc *sc, void *tgt) 664219974Smav{ 665219974Smav struct g_raid_event *ep, *tmpep; 666219974Smav 667219974Smav sx_assert(&sc->sc_lock, SX_XLOCKED); 668219974Smav 669219974Smav mtx_lock(&sc->sc_queue_mtx); 670219974Smav TAILQ_FOREACH_SAFE(ep, &sc->sc_events, e_next, tmpep) { 671219974Smav if (ep->e_tgt != tgt) 672219974Smav continue; 673219974Smav TAILQ_REMOVE(&sc->sc_events, ep, e_next); 674219974Smav if ((ep->e_flags & G_RAID_EVENT_WAIT) == 0) 675219974Smav g_raid_event_free(ep); 676219974Smav else { 677219974Smav ep->e_error = ECANCELED; 678219974Smav wakeup(ep); 679219974Smav } 680219974Smav } 681219974Smav mtx_unlock(&sc->sc_queue_mtx); 682219974Smav} 683219974Smav 684219974Smavstatic int 685219974Smavg_raid_event_check(struct g_raid_softc *sc, void *tgt) 686219974Smav{ 687219974Smav struct g_raid_event *ep; 688219974Smav int res = 0; 689219974Smav 690219974Smav sx_assert(&sc->sc_lock, SX_XLOCKED); 691219974Smav 692219974Smav mtx_lock(&sc->sc_queue_mtx); 693219974Smav TAILQ_FOREACH(ep, &sc->sc_events, e_next) { 694219974Smav if (ep->e_tgt != tgt) 695219974Smav continue; 696219974Smav res = 1; 697219974Smav break; 698219974Smav } 699219974Smav mtx_unlock(&sc->sc_queue_mtx); 700219974Smav return (res); 701219974Smav} 702219974Smav 703219974Smav/* 704219974Smav * Return the number of disks in given state. 705219974Smav * If state is equal to -1, count all connected disks. 706219974Smav */ 707219974Smavu_int 708219974Smavg_raid_ndisks(struct g_raid_softc *sc, int state) 709219974Smav{ 710219974Smav struct g_raid_disk *disk; 711219974Smav u_int n; 712219974Smav 713219974Smav sx_assert(&sc->sc_lock, SX_LOCKED); 714219974Smav 715219974Smav n = 0; 716219974Smav TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 717219974Smav if (disk->d_state == state || state == -1) 718219974Smav n++; 719219974Smav } 720219974Smav return (n); 721219974Smav} 722219974Smav 723219974Smav/* 724219974Smav * Return the number of subdisks in given state. 725219974Smav * If state is equal to -1, count all connected disks. 726219974Smav */ 727219974Smavu_int 728219974Smavg_raid_nsubdisks(struct g_raid_volume *vol, int state) 729219974Smav{ 730219974Smav struct g_raid_subdisk *subdisk; 731219974Smav struct g_raid_softc *sc; 732219974Smav u_int i, n ; 733219974Smav 734219974Smav sc = vol->v_softc; 735219974Smav sx_assert(&sc->sc_lock, SX_LOCKED); 736219974Smav 737219974Smav n = 0; 738219974Smav for (i = 0; i < vol->v_disks_count; i++) { 739219974Smav subdisk = &vol->v_subdisks[i]; 740219974Smav if ((state == -1 && 741219974Smav subdisk->sd_state != G_RAID_SUBDISK_S_NONE) || 742219974Smav subdisk->sd_state == state) 743219974Smav n++; 744219974Smav } 745219974Smav return (n); 746219974Smav} 747219974Smav 748219974Smav/* 749219974Smav * Return the first subdisk in given state. 750219974Smav * If state is equal to -1, then the first connected disks. 751219974Smav */ 752219974Smavstruct g_raid_subdisk * 753219974Smavg_raid_get_subdisk(struct g_raid_volume *vol, int state) 754219974Smav{ 755219974Smav struct g_raid_subdisk *sd; 756219974Smav struct g_raid_softc *sc; 757219974Smav u_int i; 758219974Smav 759219974Smav sc = vol->v_softc; 760219974Smav sx_assert(&sc->sc_lock, SX_LOCKED); 761219974Smav 762219974Smav for (i = 0; i < vol->v_disks_count; i++) { 763219974Smav sd = &vol->v_subdisks[i]; 764219974Smav if ((state == -1 && 765219974Smav sd->sd_state != G_RAID_SUBDISK_S_NONE) || 766219974Smav sd->sd_state == state) 767219974Smav return (sd); 768219974Smav } 769219974Smav return (NULL); 770219974Smav} 771219974Smav 772219974Smavstruct g_consumer * 773219974Smavg_raid_open_consumer(struct g_raid_softc *sc, const char *name) 774219974Smav{ 775219974Smav struct g_consumer *cp; 776219974Smav struct g_provider *pp; 777219974Smav 778219974Smav g_topology_assert(); 779219974Smav 780219974Smav if (strncmp(name, "/dev/", 5) == 0) 781219974Smav name += 5; 782219974Smav pp = g_provider_by_name(name); 783219974Smav if (pp == NULL) 784219974Smav return (NULL); 785219974Smav cp = g_new_consumer(sc->sc_geom); 786219974Smav if (g_attach(cp, pp) != 0) { 787219974Smav g_destroy_consumer(cp); 788219974Smav return (NULL); 789219974Smav } 790219974Smav if (g_access(cp, 1, 1, 1) != 0) { 791219974Smav g_detach(cp); 792219974Smav g_destroy_consumer(cp); 793219974Smav return (NULL); 794219974Smav } 795219974Smav return (cp); 796219974Smav} 797219974Smav 798219974Smavstatic u_int 799219974Smavg_raid_nrequests(struct g_raid_softc *sc, struct g_consumer *cp) 800219974Smav{ 801219974Smav struct bio *bp; 802219974Smav u_int nreqs = 0; 803219974Smav 804219974Smav mtx_lock(&sc->sc_queue_mtx); 805219974Smav TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) { 806219974Smav if (bp->bio_from == cp) 807219974Smav nreqs++; 808219974Smav } 809219974Smav mtx_unlock(&sc->sc_queue_mtx); 810219974Smav return (nreqs); 811219974Smav} 812219974Smav 813219974Smavu_int 814219974Smavg_raid_nopens(struct g_raid_softc *sc) 815219974Smav{ 816219974Smav struct g_raid_volume *vol; 817219974Smav u_int opens; 818219974Smav 819219974Smav opens = 0; 820219974Smav TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) { 821219974Smav if (vol->v_provider_open != 0) 822219974Smav opens++; 823219974Smav } 824219974Smav return (opens); 825219974Smav} 826219974Smav 827219974Smavstatic int 828219974Smavg_raid_consumer_is_busy(struct g_raid_softc *sc, struct g_consumer *cp) 829219974Smav{ 830219974Smav 831219974Smav if (cp->index > 0) { 832219974Smav G_RAID_DEBUG1(2, sc, 833219974Smav "I/O requests for %s exist, can't destroy it now.", 834219974Smav cp->provider->name); 835219974Smav return (1); 836219974Smav } 837219974Smav if (g_raid_nrequests(sc, cp) > 0) { 838219974Smav G_RAID_DEBUG1(2, sc, 839219974Smav "I/O requests for %s in queue, can't destroy it now.", 840219974Smav cp->provider->name); 841219974Smav return (1); 842219974Smav } 843219974Smav return (0); 844219974Smav} 845219974Smav 846219974Smavstatic void 847219974Smavg_raid_destroy_consumer(void *arg, int flags __unused) 848219974Smav{ 849219974Smav struct g_consumer *cp; 850219974Smav 851219974Smav g_topology_assert(); 852219974Smav 853219974Smav cp = arg; 854219974Smav G_RAID_DEBUG(1, "Consumer %s destroyed.", cp->provider->name); 855219974Smav g_detach(cp); 856219974Smav g_destroy_consumer(cp); 857219974Smav} 858219974Smav 859219974Smavvoid 860219974Smavg_raid_kill_consumer(struct g_raid_softc *sc, struct g_consumer *cp) 861219974Smav{ 862219974Smav struct g_provider *pp; 863219974Smav int retaste_wait; 864219974Smav 865219974Smav g_topology_assert_not(); 866219974Smav 867219974Smav g_topology_lock(); 868219974Smav cp->private = NULL; 869219974Smav if (g_raid_consumer_is_busy(sc, cp)) 870219974Smav goto out; 871219974Smav pp = cp->provider; 872219974Smav retaste_wait = 0; 873219974Smav if (cp->acw == 1) { 874219974Smav if ((pp->geom->flags & G_GEOM_WITHER) == 0) 875219974Smav retaste_wait = 1; 876219974Smav } 877219974Smav if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) 878219974Smav g_access(cp, -cp->acr, -cp->acw, -cp->ace); 879219974Smav if (retaste_wait) { 880219974Smav /* 881219974Smav * After retaste event was send (inside g_access()), we can send 882219974Smav * event to detach and destroy consumer. 883219974Smav * A class, which has consumer to the given provider connected 884219974Smav * will not receive retaste event for the provider. 885219974Smav * This is the way how I ignore retaste events when I close 886219974Smav * consumers opened for write: I detach and destroy consumer 887219974Smav * after retaste event is sent. 888219974Smav */ 889219974Smav g_post_event(g_raid_destroy_consumer, cp, M_WAITOK, NULL); 890219974Smav goto out; 891219974Smav } 892219974Smav G_RAID_DEBUG(1, "Consumer %s destroyed.", pp->name); 893219974Smav g_detach(cp); 894219974Smav g_destroy_consumer(cp); 895219974Smavout: 896219974Smav g_topology_unlock(); 897219974Smav} 898219974Smav 899219974Smavstatic void 900219974Smavg_raid_orphan(struct g_consumer *cp) 901219974Smav{ 902219974Smav struct g_raid_disk *disk; 903219974Smav 904219974Smav g_topology_assert(); 905219974Smav 906219974Smav disk = cp->private; 907219974Smav if (disk == NULL) 908219974Smav return; 909219974Smav g_raid_event_send(disk, G_RAID_DISK_E_DISCONNECTED, 910219974Smav G_RAID_EVENT_DISK); 911219974Smav} 912219974Smav 913242314Smavstatic void 914219974Smavg_raid_clean(struct g_raid_volume *vol, int acw) 915219974Smav{ 916219974Smav struct g_raid_softc *sc; 917219974Smav int timeout; 918219974Smav 919219974Smav sc = vol->v_softc; 920219974Smav g_topology_assert_not(); 921219974Smav sx_assert(&sc->sc_lock, SX_XLOCKED); 922219974Smav 923219974Smav// if ((sc->sc_flags & G_RAID_DEVICE_FLAG_NOFAILSYNC) != 0) 924242314Smav// return; 925219974Smav if (!vol->v_dirty) 926242314Smav return; 927219974Smav if (vol->v_writes > 0) 928242314Smav return; 929219974Smav if (acw > 0 || (acw == -1 && 930219974Smav vol->v_provider != NULL && vol->v_provider->acw > 0)) { 931219974Smav timeout = g_raid_clean_time - (time_uptime - vol->v_last_write); 932242314Smav if (!g_raid_shutdown && timeout > 0) 933242314Smav return; 934219974Smav } 935219974Smav vol->v_dirty = 0; 936219974Smav G_RAID_DEBUG1(1, sc, "Volume %s marked as clean.", 937219974Smav vol->v_name); 938219974Smav g_raid_write_metadata(sc, vol, NULL, NULL); 939219974Smav} 940219974Smav 941219974Smavstatic void 942219974Smavg_raid_dirty(struct g_raid_volume *vol) 943219974Smav{ 944219974Smav struct g_raid_softc *sc; 945219974Smav 946219974Smav sc = vol->v_softc; 947219974Smav g_topology_assert_not(); 948219974Smav sx_assert(&sc->sc_lock, SX_XLOCKED); 949219974Smav 950219974Smav// if ((sc->sc_flags & G_RAID_DEVICE_FLAG_NOFAILSYNC) != 0) 951219974Smav// return; 952219974Smav vol->v_dirty = 1; 953219974Smav G_RAID_DEBUG1(1, sc, "Volume %s marked as dirty.", 954219974Smav vol->v_name); 955219974Smav g_raid_write_metadata(sc, vol, NULL, NULL); 956219974Smav} 957219974Smav 958219974Smavvoid 959219974Smavg_raid_tr_flush_common(struct g_raid_tr_object *tr, struct bio *bp) 960219974Smav{ 961219974Smav struct g_raid_softc *sc; 962219974Smav struct g_raid_volume *vol; 963219974Smav struct g_raid_subdisk *sd; 964219974Smav struct bio_queue_head queue; 965219974Smav struct bio *cbp; 966219974Smav int i; 967219974Smav 968219974Smav vol = tr->tro_volume; 969219974Smav sc = vol->v_softc; 970219974Smav 971219974Smav /* 972219974Smav * Allocate all bios before sending any request, so we can return 973219974Smav * ENOMEM in nice and clean way. 974219974Smav */ 975219974Smav bioq_init(&queue); 976219974Smav for (i = 0; i < vol->v_disks_count; i++) { 977219974Smav sd = &vol->v_subdisks[i]; 978219974Smav if (sd->sd_state == G_RAID_SUBDISK_S_NONE || 979219974Smav sd->sd_state == G_RAID_SUBDISK_S_FAILED) 980219974Smav continue; 981219974Smav cbp = g_clone_bio(bp); 982219974Smav if (cbp == NULL) 983219974Smav goto failure; 984219974Smav cbp->bio_caller1 = sd; 985219974Smav bioq_insert_tail(&queue, cbp); 986219974Smav } 987219974Smav for (cbp = bioq_first(&queue); cbp != NULL; 988219974Smav cbp = bioq_first(&queue)) { 989219974Smav bioq_remove(&queue, cbp); 990219974Smav sd = cbp->bio_caller1; 991219974Smav cbp->bio_caller1 = NULL; 992219974Smav g_raid_subdisk_iostart(sd, cbp); 993219974Smav } 994219974Smav return; 995219974Smavfailure: 996219974Smav for (cbp = bioq_first(&queue); cbp != NULL; 997219974Smav cbp = bioq_first(&queue)) { 998219974Smav bioq_remove(&queue, cbp); 999219974Smav g_destroy_bio(cbp); 1000219974Smav } 1001219974Smav if (bp->bio_error == 0) 1002219974Smav bp->bio_error = ENOMEM; 1003219974Smav g_raid_iodone(bp, bp->bio_error); 1004219974Smav} 1005219974Smav 1006219974Smavstatic void 1007219974Smavg_raid_tr_kerneldump_common_done(struct bio *bp) 1008219974Smav{ 1009219974Smav 1010219974Smav bp->bio_flags |= BIO_DONE; 1011219974Smav} 1012219974Smav 1013219974Smavint 1014219974Smavg_raid_tr_kerneldump_common(struct g_raid_tr_object *tr, 1015219974Smav void *virtual, vm_offset_t physical, off_t offset, size_t length) 1016219974Smav{ 1017219974Smav struct g_raid_softc *sc; 1018219974Smav struct g_raid_volume *vol; 1019219974Smav struct bio bp; 1020219974Smav 1021219974Smav vol = tr->tro_volume; 1022219974Smav sc = vol->v_softc; 1023219974Smav 1024219974Smav bzero(&bp, sizeof(bp)); 1025219974Smav bp.bio_cmd = BIO_WRITE; 1026219974Smav bp.bio_done = g_raid_tr_kerneldump_common_done; 1027219974Smav bp.bio_attribute = NULL; 1028219974Smav bp.bio_offset = offset; 1029219974Smav bp.bio_length = length; 1030219974Smav bp.bio_data = virtual; 1031219974Smav bp.bio_to = vol->v_provider; 1032219974Smav 1033219974Smav g_raid_start(&bp); 1034219974Smav while (!(bp.bio_flags & BIO_DONE)) { 1035219974Smav G_RAID_DEBUG1(4, sc, "Poll..."); 1036219974Smav g_raid_poll(sc); 1037219974Smav DELAY(10); 1038219974Smav } 1039219974Smav 1040219974Smav return (bp.bio_error != 0 ? EIO : 0); 1041219974Smav} 1042219974Smav 1043219974Smavstatic int 1044219974Smavg_raid_dump(void *arg, 1045219974Smav void *virtual, vm_offset_t physical, off_t offset, size_t length) 1046219974Smav{ 1047219974Smav struct g_raid_volume *vol; 1048219974Smav int error; 1049219974Smav 1050219974Smav vol = (struct g_raid_volume *)arg; 1051219974Smav G_RAID_DEBUG1(3, vol->v_softc, "Dumping at off %llu len %llu.", 1052219974Smav (long long unsigned)offset, (long long unsigned)length); 1053219974Smav 1054219974Smav error = G_RAID_TR_KERNELDUMP(vol->v_tr, 1055219974Smav virtual, physical, offset, length); 1056219974Smav return (error); 1057219974Smav} 1058219974Smav 1059219974Smavstatic void 1060219974Smavg_raid_kerneldump(struct g_raid_softc *sc, struct bio *bp) 1061219974Smav{ 1062219974Smav struct g_kerneldump *gkd; 1063219974Smav struct g_provider *pp; 1064219974Smav struct g_raid_volume *vol; 1065219974Smav 1066219974Smav gkd = (struct g_kerneldump*)bp->bio_data; 1067219974Smav pp = bp->bio_to; 1068219974Smav vol = pp->private; 1069219974Smav g_trace(G_T_TOPOLOGY, "g_raid_kerneldump(%s, %jd, %jd)", 1070219974Smav pp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length); 1071219974Smav gkd->di.dumper = g_raid_dump; 1072219974Smav gkd->di.priv = vol; 1073219974Smav gkd->di.blocksize = vol->v_sectorsize; 1074219974Smav gkd->di.maxiosize = DFLTPHYS; 1075219974Smav gkd->di.mediaoffset = gkd->offset; 1076219974Smav if ((gkd->offset + gkd->length) > vol->v_mediasize) 1077219974Smav gkd->length = vol->v_mediasize - gkd->offset; 1078219974Smav gkd->di.mediasize = gkd->length; 1079219974Smav g_io_deliver(bp, 0); 1080219974Smav} 1081219974Smav 1082219974Smavstatic void 1083242323Smavg_raid_candelete(struct g_raid_softc *sc, struct bio *bp) 1084242323Smav{ 1085242323Smav struct g_provider *pp; 1086242323Smav struct g_raid_volume *vol; 1087242323Smav struct g_raid_subdisk *sd; 1088242323Smav int *val; 1089242323Smav int i; 1090242323Smav 1091242323Smav val = (int *)bp->bio_data; 1092242323Smav pp = bp->bio_to; 1093242323Smav vol = pp->private; 1094242323Smav *val = 0; 1095242323Smav for (i = 0; i < vol->v_disks_count; i++) { 1096242323Smav sd = &vol->v_subdisks[i]; 1097242323Smav if (sd->sd_state == G_RAID_SUBDISK_S_NONE) 1098242323Smav continue; 1099242323Smav if (sd->sd_disk->d_candelete) { 1100242323Smav *val = 1; 1101242323Smav break; 1102242323Smav } 1103242323Smav } 1104242323Smav g_io_deliver(bp, 0); 1105242323Smav} 1106242323Smav 1107242323Smavstatic void 1108219974Smavg_raid_start(struct bio *bp) 1109219974Smav{ 1110219974Smav struct g_raid_softc *sc; 1111219974Smav 1112219974Smav sc = bp->bio_to->geom->softc; 1113219974Smav /* 1114219974Smav * If sc == NULL or there are no valid disks, provider's error 1115219974Smav * should be set and g_raid_start() should not be called at all. 1116219974Smav */ 1117219974Smav// KASSERT(sc != NULL && sc->sc_state == G_RAID_VOLUME_S_RUNNING, 1118219974Smav// ("Provider's error should be set (error=%d)(mirror=%s).", 1119219974Smav// bp->bio_to->error, bp->bio_to->name)); 1120219974Smav G_RAID_LOGREQ(3, bp, "Request received."); 1121219974Smav 1122219974Smav switch (bp->bio_cmd) { 1123219974Smav case BIO_READ: 1124219974Smav case BIO_WRITE: 1125219974Smav case BIO_DELETE: 1126219974Smav case BIO_FLUSH: 1127219974Smav break; 1128219974Smav case BIO_GETATTR: 1129242323Smav if (!strcmp(bp->bio_attribute, "GEOM::candelete")) 1130242323Smav g_raid_candelete(sc, bp); 1131242323Smav else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump")) 1132219974Smav g_raid_kerneldump(sc, bp); 1133219974Smav else 1134219974Smav g_io_deliver(bp, EOPNOTSUPP); 1135219974Smav return; 1136219974Smav default: 1137219974Smav g_io_deliver(bp, EOPNOTSUPP); 1138219974Smav return; 1139219974Smav } 1140219974Smav mtx_lock(&sc->sc_queue_mtx); 1141219974Smav bioq_disksort(&sc->sc_queue, bp); 1142219974Smav mtx_unlock(&sc->sc_queue_mtx); 1143219974Smav if (!dumping) { 1144219974Smav G_RAID_DEBUG1(4, sc, "Waking up %p.", sc); 1145219974Smav wakeup(sc); 1146219974Smav } 1147219974Smav} 1148219974Smav 1149219974Smavstatic int 1150219974Smavg_raid_bio_overlaps(const struct bio *bp, off_t lstart, off_t len) 1151219974Smav{ 1152219974Smav /* 1153219974Smav * 5 cases: 1154219974Smav * (1) bp entirely below NO 1155219974Smav * (2) bp entirely above NO 1156219974Smav * (3) bp start below, but end in range YES 1157219974Smav * (4) bp entirely within YES 1158219974Smav * (5) bp starts within, ends above YES 1159219974Smav * 1160219974Smav * lock range 10-19 (offset 10 length 10) 1161219974Smav * (1) 1-5: first if kicks it out 1162219974Smav * (2) 30-35: second if kicks it out 1163219974Smav * (3) 5-15: passes both ifs 1164219974Smav * (4) 12-14: passes both ifs 1165219974Smav * (5) 19-20: passes both 1166219974Smav */ 1167219974Smav off_t lend = lstart + len - 1; 1168219974Smav off_t bstart = bp->bio_offset; 1169219974Smav off_t bend = bp->bio_offset + bp->bio_length - 1; 1170219974Smav 1171219974Smav if (bend < lstart) 1172219974Smav return (0); 1173219974Smav if (lend < bstart) 1174219974Smav return (0); 1175219974Smav return (1); 1176219974Smav} 1177219974Smav 1178219974Smavstatic int 1179219974Smavg_raid_is_in_locked_range(struct g_raid_volume *vol, const struct bio *bp) 1180219974Smav{ 1181219974Smav struct g_raid_lock *lp; 1182219974Smav 1183219974Smav sx_assert(&vol->v_softc->sc_lock, SX_LOCKED); 1184219974Smav 1185219974Smav LIST_FOREACH(lp, &vol->v_locks, l_next) { 1186219974Smav if (g_raid_bio_overlaps(bp, lp->l_offset, lp->l_length)) 1187219974Smav return (1); 1188219974Smav } 1189219974Smav return (0); 1190219974Smav} 1191219974Smav 1192219974Smavstatic void 1193219974Smavg_raid_start_request(struct bio *bp) 1194219974Smav{ 1195219974Smav struct g_raid_softc *sc; 1196219974Smav struct g_raid_volume *vol; 1197219974Smav 1198219974Smav sc = bp->bio_to->geom->softc; 1199219974Smav sx_assert(&sc->sc_lock, SX_LOCKED); 1200219974Smav vol = bp->bio_to->private; 1201219974Smav 1202219974Smav /* 1203219974Smav * Check to see if this item is in a locked range. If so, 1204219974Smav * queue it to our locked queue and return. We'll requeue 1205219974Smav * it when the range is unlocked. Internal I/O for the 1206219974Smav * rebuild/rescan/recovery process is excluded from this 1207219974Smav * check so we can actually do the recovery. 1208219974Smav */ 1209219974Smav if (!(bp->bio_cflags & G_RAID_BIO_FLAG_SPECIAL) && 1210219974Smav g_raid_is_in_locked_range(vol, bp)) { 1211219974Smav G_RAID_LOGREQ(3, bp, "Defer request."); 1212219974Smav bioq_insert_tail(&vol->v_locked, bp); 1213219974Smav return; 1214219974Smav } 1215219974Smav 1216219974Smav /* 1217219974Smav * If we're actually going to do the write/delete, then 1218219974Smav * update the idle stats for the volume. 1219219974Smav */ 1220219974Smav if (bp->bio_cmd == BIO_WRITE || bp->bio_cmd == BIO_DELETE) { 1221219974Smav if (!vol->v_dirty) 1222219974Smav g_raid_dirty(vol); 1223219974Smav vol->v_writes++; 1224219974Smav } 1225219974Smav 1226219974Smav /* 1227219974Smav * Put request onto inflight queue, so we can check if new 1228219974Smav * synchronization requests don't collide with it. Then tell 1229219974Smav * the transformation layer to start the I/O. 1230219974Smav */ 1231219974Smav bioq_insert_tail(&vol->v_inflight, bp); 1232219974Smav G_RAID_LOGREQ(4, bp, "Request started"); 1233219974Smav G_RAID_TR_IOSTART(vol->v_tr, bp); 1234219974Smav} 1235219974Smav 1236219974Smavstatic void 1237219974Smavg_raid_finish_with_locked_ranges(struct g_raid_volume *vol, struct bio *bp) 1238219974Smav{ 1239219974Smav off_t off, len; 1240219974Smav struct bio *nbp; 1241219974Smav struct g_raid_lock *lp; 1242219974Smav 1243219974Smav vol->v_pending_lock = 0; 1244219974Smav LIST_FOREACH(lp, &vol->v_locks, l_next) { 1245219974Smav if (lp->l_pending) { 1246219974Smav off = lp->l_offset; 1247219974Smav len = lp->l_length; 1248219974Smav lp->l_pending = 0; 1249219974Smav TAILQ_FOREACH(nbp, &vol->v_inflight.queue, bio_queue) { 1250219974Smav if (g_raid_bio_overlaps(nbp, off, len)) 1251219974Smav lp->l_pending++; 1252219974Smav } 1253219974Smav if (lp->l_pending) { 1254219974Smav vol->v_pending_lock = 1; 1255219974Smav G_RAID_DEBUG1(4, vol->v_softc, 1256219974Smav "Deferred lock(%jd, %jd) has %d pending", 1257219974Smav (intmax_t)off, (intmax_t)(off + len), 1258219974Smav lp->l_pending); 1259219974Smav continue; 1260219974Smav } 1261219974Smav G_RAID_DEBUG1(4, vol->v_softc, 1262219974Smav "Deferred lock of %jd to %jd completed", 1263219974Smav (intmax_t)off, (intmax_t)(off + len)); 1264219974Smav G_RAID_TR_LOCKED(vol->v_tr, lp->l_callback_arg); 1265219974Smav } 1266219974Smav } 1267219974Smav} 1268219974Smav 1269219974Smavvoid 1270219974Smavg_raid_iodone(struct bio *bp, int error) 1271219974Smav{ 1272219974Smav struct g_raid_softc *sc; 1273219974Smav struct g_raid_volume *vol; 1274219974Smav 1275219974Smav sc = bp->bio_to->geom->softc; 1276219974Smav sx_assert(&sc->sc_lock, SX_LOCKED); 1277219974Smav vol = bp->bio_to->private; 1278219974Smav G_RAID_LOGREQ(3, bp, "Request done: %d.", error); 1279219974Smav 1280219974Smav /* Update stats if we done write/delete. */ 1281219974Smav if (bp->bio_cmd == BIO_WRITE || bp->bio_cmd == BIO_DELETE) { 1282219974Smav vol->v_writes--; 1283219974Smav vol->v_last_write = time_uptime; 1284219974Smav } 1285219974Smav 1286219974Smav bioq_remove(&vol->v_inflight, bp); 1287219974Smav if (vol->v_pending_lock && g_raid_is_in_locked_range(vol, bp)) 1288219974Smav g_raid_finish_with_locked_ranges(vol, bp); 1289219974Smav getmicrouptime(&vol->v_last_done); 1290219974Smav g_io_deliver(bp, error); 1291219974Smav} 1292219974Smav 1293219974Smavint 1294219974Smavg_raid_lock_range(struct g_raid_volume *vol, off_t off, off_t len, 1295219974Smav struct bio *ignore, void *argp) 1296219974Smav{ 1297219974Smav struct g_raid_softc *sc; 1298219974Smav struct g_raid_lock *lp; 1299219974Smav struct bio *bp; 1300219974Smav 1301219974Smav sc = vol->v_softc; 1302219974Smav lp = malloc(sizeof(*lp), M_RAID, M_WAITOK | M_ZERO); 1303219974Smav LIST_INSERT_HEAD(&vol->v_locks, lp, l_next); 1304219974Smav lp->l_offset = off; 1305219974Smav lp->l_length = len; 1306219974Smav lp->l_callback_arg = argp; 1307219974Smav 1308219974Smav lp->l_pending = 0; 1309219974Smav TAILQ_FOREACH(bp, &vol->v_inflight.queue, bio_queue) { 1310219974Smav if (bp != ignore && g_raid_bio_overlaps(bp, off, len)) 1311219974Smav lp->l_pending++; 1312219974Smav } 1313219974Smav 1314219974Smav /* 1315219974Smav * If there are any writes that are pending, we return EBUSY. All 1316219974Smav * callers will have to wait until all pending writes clear. 1317219974Smav */ 1318219974Smav if (lp->l_pending > 0) { 1319219974Smav vol->v_pending_lock = 1; 1320219974Smav G_RAID_DEBUG1(4, sc, "Locking range %jd to %jd deferred %d pend", 1321219974Smav (intmax_t)off, (intmax_t)(off+len), lp->l_pending); 1322219974Smav return (EBUSY); 1323219974Smav } 1324219974Smav G_RAID_DEBUG1(4, sc, "Locking range %jd to %jd", 1325219974Smav (intmax_t)off, (intmax_t)(off+len)); 1326219974Smav G_RAID_TR_LOCKED(vol->v_tr, lp->l_callback_arg); 1327219974Smav return (0); 1328219974Smav} 1329219974Smav 1330219974Smavint 1331219974Smavg_raid_unlock_range(struct g_raid_volume *vol, off_t off, off_t len) 1332219974Smav{ 1333219974Smav struct g_raid_lock *lp; 1334219974Smav struct g_raid_softc *sc; 1335219974Smav struct bio *bp; 1336219974Smav 1337219974Smav sc = vol->v_softc; 1338219974Smav LIST_FOREACH(lp, &vol->v_locks, l_next) { 1339219974Smav if (lp->l_offset == off && lp->l_length == len) { 1340219974Smav LIST_REMOVE(lp, l_next); 1341219974Smav /* XXX 1342219974Smav * Right now we just put them all back on the queue 1343219974Smav * and hope for the best. We hope this because any 1344219974Smav * locked ranges will go right back on this list 1345219974Smav * when the worker thread runs. 1346219974Smav * XXX 1347219974Smav */ 1348219974Smav G_RAID_DEBUG1(4, sc, "Unlocked %jd to %jd", 1349219974Smav (intmax_t)lp->l_offset, 1350219974Smav (intmax_t)(lp->l_offset+lp->l_length)); 1351219974Smav mtx_lock(&sc->sc_queue_mtx); 1352219974Smav while ((bp = bioq_takefirst(&vol->v_locked)) != NULL) 1353219974Smav bioq_disksort(&sc->sc_queue, bp); 1354219974Smav mtx_unlock(&sc->sc_queue_mtx); 1355219974Smav free(lp, M_RAID); 1356219974Smav return (0); 1357219974Smav } 1358219974Smav } 1359219974Smav return (EINVAL); 1360219974Smav} 1361219974Smav 1362219974Smavvoid 1363219974Smavg_raid_subdisk_iostart(struct g_raid_subdisk *sd, struct bio *bp) 1364219974Smav{ 1365219974Smav struct g_consumer *cp; 1366219974Smav struct g_raid_disk *disk, *tdisk; 1367219974Smav 1368219974Smav bp->bio_caller1 = sd; 1369219974Smav 1370219974Smav /* 1371219974Smav * Make sure that the disk is present. Generally it is a task of 1372219974Smav * transformation layers to not send requests to absent disks, but 1373219974Smav * it is better to be safe and report situation then sorry. 1374219974Smav */ 1375219974Smav if (sd->sd_disk == NULL) { 1376219974Smav G_RAID_LOGREQ(0, bp, "Warning! I/O request to an absent disk!"); 1377219974Smavnodisk: 1378219974Smav bp->bio_from = NULL; 1379219974Smav bp->bio_to = NULL; 1380219974Smav bp->bio_error = ENXIO; 1381219974Smav g_raid_disk_done(bp); 1382219974Smav return; 1383219974Smav } 1384219974Smav disk = sd->sd_disk; 1385219974Smav if (disk->d_state != G_RAID_DISK_S_ACTIVE && 1386219974Smav disk->d_state != G_RAID_DISK_S_FAILED) { 1387219974Smav G_RAID_LOGREQ(0, bp, "Warning! I/O request to a disk in a " 1388219974Smav "wrong state (%s)!", g_raid_disk_state2str(disk->d_state)); 1389219974Smav goto nodisk; 1390219974Smav } 1391219974Smav 1392219974Smav cp = disk->d_consumer; 1393219974Smav bp->bio_from = cp; 1394219974Smav bp->bio_to = cp->provider; 1395219974Smav cp->index++; 1396219974Smav 1397219974Smav /* Update average disks load. */ 1398219974Smav TAILQ_FOREACH(tdisk, &sd->sd_softc->sc_disks, d_next) { 1399219974Smav if (tdisk->d_consumer == NULL) 1400219974Smav tdisk->d_load = 0; 1401219974Smav else 1402219974Smav tdisk->d_load = (tdisk->d_consumer->index * 1403219974Smav G_RAID_SUBDISK_LOAD_SCALE + tdisk->d_load * 7) / 8; 1404219974Smav } 1405219974Smav 1406219974Smav disk->d_last_offset = bp->bio_offset + bp->bio_length; 1407219974Smav if (dumping) { 1408219974Smav G_RAID_LOGREQ(3, bp, "Sending dumping request."); 1409219974Smav if (bp->bio_cmd == BIO_WRITE) { 1410219974Smav bp->bio_error = g_raid_subdisk_kerneldump(sd, 1411219974Smav bp->bio_data, 0, bp->bio_offset, bp->bio_length); 1412219974Smav } else 1413219974Smav bp->bio_error = EOPNOTSUPP; 1414219974Smav g_raid_disk_done(bp); 1415219974Smav } else { 1416219974Smav bp->bio_done = g_raid_disk_done; 1417219974Smav bp->bio_offset += sd->sd_offset; 1418219974Smav G_RAID_LOGREQ(3, bp, "Sending request."); 1419219974Smav g_io_request(bp, cp); 1420219974Smav } 1421219974Smav} 1422219974Smav 1423219974Smavint 1424219974Smavg_raid_subdisk_kerneldump(struct g_raid_subdisk *sd, 1425219974Smav void *virtual, vm_offset_t physical, off_t offset, size_t length) 1426219974Smav{ 1427219974Smav 1428219974Smav if (sd->sd_disk == NULL) 1429219974Smav return (ENXIO); 1430219974Smav if (sd->sd_disk->d_kd.di.dumper == NULL) 1431219974Smav return (EOPNOTSUPP); 1432219974Smav return (dump_write(&sd->sd_disk->d_kd.di, 1433219974Smav virtual, physical, 1434219974Smav sd->sd_disk->d_kd.di.mediaoffset + sd->sd_offset + offset, 1435219974Smav length)); 1436219974Smav} 1437219974Smav 1438219974Smavstatic void 1439219974Smavg_raid_disk_done(struct bio *bp) 1440219974Smav{ 1441219974Smav struct g_raid_softc *sc; 1442219974Smav struct g_raid_subdisk *sd; 1443219974Smav 1444219974Smav sd = bp->bio_caller1; 1445219974Smav sc = sd->sd_softc; 1446219974Smav mtx_lock(&sc->sc_queue_mtx); 1447219974Smav bioq_disksort(&sc->sc_queue, bp); 1448219974Smav mtx_unlock(&sc->sc_queue_mtx); 1449219974Smav if (!dumping) 1450219974Smav wakeup(sc); 1451219974Smav} 1452219974Smav 1453219974Smavstatic void 1454219974Smavg_raid_disk_done_request(struct bio *bp) 1455219974Smav{ 1456219974Smav struct g_raid_softc *sc; 1457219974Smav struct g_raid_disk *disk; 1458219974Smav struct g_raid_subdisk *sd; 1459219974Smav struct g_raid_volume *vol; 1460219974Smav 1461219974Smav g_topology_assert_not(); 1462219974Smav 1463219974Smav G_RAID_LOGREQ(3, bp, "Disk request done: %d.", bp->bio_error); 1464219974Smav sd = bp->bio_caller1; 1465219974Smav sc = sd->sd_softc; 1466219974Smav vol = sd->sd_volume; 1467219974Smav if (bp->bio_from != NULL) { 1468219974Smav bp->bio_from->index--; 1469219974Smav disk = bp->bio_from->private; 1470219974Smav if (disk == NULL) 1471219974Smav g_raid_kill_consumer(sc, bp->bio_from); 1472219974Smav } 1473219974Smav bp->bio_offset -= sd->sd_offset; 1474219974Smav 1475219974Smav G_RAID_TR_IODONE(vol->v_tr, sd, bp); 1476219974Smav} 1477219974Smav 1478219974Smavstatic void 1479219974Smavg_raid_handle_event(struct g_raid_softc *sc, struct g_raid_event *ep) 1480219974Smav{ 1481219974Smav 1482219974Smav if ((ep->e_flags & G_RAID_EVENT_VOLUME) != 0) 1483219974Smav ep->e_error = g_raid_update_volume(ep->e_tgt, ep->e_event); 1484219974Smav else if ((ep->e_flags & G_RAID_EVENT_DISK) != 0) 1485219974Smav ep->e_error = g_raid_update_disk(ep->e_tgt, ep->e_event); 1486219974Smav else if ((ep->e_flags & G_RAID_EVENT_SUBDISK) != 0) 1487219974Smav ep->e_error = g_raid_update_subdisk(ep->e_tgt, ep->e_event); 1488219974Smav else 1489219974Smav ep->e_error = g_raid_update_node(ep->e_tgt, ep->e_event); 1490219974Smav if ((ep->e_flags & G_RAID_EVENT_WAIT) == 0) { 1491219974Smav KASSERT(ep->e_error == 0, 1492219974Smav ("Error cannot be handled.")); 1493219974Smav g_raid_event_free(ep); 1494219974Smav } else { 1495219974Smav ep->e_flags |= G_RAID_EVENT_DONE; 1496219974Smav G_RAID_DEBUG1(4, sc, "Waking up %p.", ep); 1497219974Smav mtx_lock(&sc->sc_queue_mtx); 1498219974Smav wakeup(ep); 1499219974Smav mtx_unlock(&sc->sc_queue_mtx); 1500219974Smav } 1501219974Smav} 1502219974Smav 1503219974Smav/* 1504219974Smav * Worker thread. 1505219974Smav */ 1506219974Smavstatic void 1507219974Smavg_raid_worker(void *arg) 1508219974Smav{ 1509219974Smav struct g_raid_softc *sc; 1510219974Smav struct g_raid_event *ep; 1511219974Smav struct g_raid_volume *vol; 1512219974Smav struct bio *bp; 1513219974Smav struct timeval now, t; 1514219974Smav int timeout, rv; 1515219974Smav 1516219974Smav sc = arg; 1517219974Smav thread_lock(curthread); 1518219974Smav sched_prio(curthread, PRIBIO); 1519219974Smav thread_unlock(curthread); 1520219974Smav 1521219974Smav sx_xlock(&sc->sc_lock); 1522219974Smav for (;;) { 1523219974Smav mtx_lock(&sc->sc_queue_mtx); 1524219974Smav /* 1525219974Smav * First take a look at events. 1526219974Smav * This is important to handle events before any I/O requests. 1527219974Smav */ 1528219974Smav bp = NULL; 1529219974Smav vol = NULL; 1530219974Smav rv = 0; 1531219974Smav ep = TAILQ_FIRST(&sc->sc_events); 1532219974Smav if (ep != NULL) 1533219974Smav TAILQ_REMOVE(&sc->sc_events, ep, e_next); 1534219974Smav else if ((bp = bioq_takefirst(&sc->sc_queue)) != NULL) 1535219974Smav ; 1536219974Smav else { 1537219974Smav getmicrouptime(&now); 1538219974Smav t = now; 1539219974Smav TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) { 1540219974Smav if (bioq_first(&vol->v_inflight) == NULL && 1541219974Smav vol->v_tr && 1542219974Smav timevalcmp(&vol->v_last_done, &t, < )) 1543219974Smav t = vol->v_last_done; 1544219974Smav } 1545219974Smav timevalsub(&t, &now); 1546219974Smav timeout = g_raid_idle_threshold + 1547219974Smav t.tv_sec * 1000000 + t.tv_usec; 1548219974Smav if (timeout > 0) { 1549219974Smav /* 1550219974Smav * Two steps to avoid overflows at HZ=1000 1551219974Smav * and idle timeouts > 2.1s. Some rounding 1552219974Smav * errors can occur, but they are < 1tick, 1553219974Smav * which is deemed to be close enough for 1554219974Smav * this purpose. 1555219974Smav */ 1556219974Smav int micpertic = 1000000 / hz; 1557219974Smav timeout = (timeout + micpertic - 1) / micpertic; 1558219974Smav sx_xunlock(&sc->sc_lock); 1559219974Smav MSLEEP(rv, sc, &sc->sc_queue_mtx, 1560219974Smav PRIBIO | PDROP, "-", timeout); 1561219974Smav sx_xlock(&sc->sc_lock); 1562219974Smav goto process; 1563219974Smav } else 1564219974Smav rv = EWOULDBLOCK; 1565219974Smav } 1566219974Smav mtx_unlock(&sc->sc_queue_mtx); 1567219974Smavprocess: 1568219974Smav if (ep != NULL) { 1569219974Smav g_raid_handle_event(sc, ep); 1570219974Smav } else if (bp != NULL) { 1571219974Smav if (bp->bio_to != NULL && 1572219974Smav bp->bio_to->geom == sc->sc_geom) 1573219974Smav g_raid_start_request(bp); 1574219974Smav else 1575219974Smav g_raid_disk_done_request(bp); 1576219974Smav } else if (rv == EWOULDBLOCK) { 1577219974Smav TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) { 1578242314Smav g_raid_clean(vol, -1); 1579219974Smav if (bioq_first(&vol->v_inflight) == NULL && 1580219974Smav vol->v_tr) { 1581219974Smav t.tv_sec = g_raid_idle_threshold / 1000000; 1582219974Smav t.tv_usec = g_raid_idle_threshold % 1000000; 1583219974Smav timevaladd(&t, &vol->v_last_done); 1584219974Smav getmicrouptime(&now); 1585219974Smav if (timevalcmp(&t, &now, <= )) { 1586219974Smav G_RAID_TR_IDLE(vol->v_tr); 1587219974Smav vol->v_last_done = now; 1588219974Smav } 1589219974Smav } 1590219974Smav } 1591219974Smav } 1592219974Smav if (sc->sc_stopping == G_RAID_DESTROY_HARD) 1593219974Smav g_raid_destroy_node(sc, 1); /* May not return. */ 1594219974Smav } 1595219974Smav} 1596219974Smav 1597219974Smavstatic void 1598219974Smavg_raid_poll(struct g_raid_softc *sc) 1599219974Smav{ 1600219974Smav struct g_raid_event *ep; 1601219974Smav struct bio *bp; 1602219974Smav 1603219974Smav sx_xlock(&sc->sc_lock); 1604219974Smav mtx_lock(&sc->sc_queue_mtx); 1605219974Smav /* 1606219974Smav * First take a look at events. 1607219974Smav * This is important to handle events before any I/O requests. 1608219974Smav */ 1609219974Smav ep = TAILQ_FIRST(&sc->sc_events); 1610219974Smav if (ep != NULL) { 1611219974Smav TAILQ_REMOVE(&sc->sc_events, ep, e_next); 1612219974Smav mtx_unlock(&sc->sc_queue_mtx); 1613219974Smav g_raid_handle_event(sc, ep); 1614219974Smav goto out; 1615219974Smav } 1616219974Smav bp = bioq_takefirst(&sc->sc_queue); 1617219974Smav if (bp != NULL) { 1618219974Smav mtx_unlock(&sc->sc_queue_mtx); 1619219974Smav if (bp->bio_from == NULL || 1620219974Smav bp->bio_from->geom != sc->sc_geom) 1621219974Smav g_raid_start_request(bp); 1622219974Smav else 1623219974Smav g_raid_disk_done_request(bp); 1624219974Smav } 1625219974Smavout: 1626219974Smav sx_xunlock(&sc->sc_lock); 1627219974Smav} 1628219974Smav 1629219974Smavstatic void 1630219974Smavg_raid_launch_provider(struct g_raid_volume *vol) 1631219974Smav{ 1632219974Smav struct g_raid_disk *disk; 1633219974Smav struct g_raid_softc *sc; 1634219974Smav struct g_provider *pp; 1635219974Smav char name[G_RAID_MAX_VOLUMENAME]; 1636219974Smav off_t off; 1637219974Smav 1638219974Smav sc = vol->v_softc; 1639219974Smav sx_assert(&sc->sc_lock, SX_LOCKED); 1640219974Smav 1641219974Smav g_topology_lock(); 1642219974Smav /* Try to name provider with volume name. */ 1643219974Smav snprintf(name, sizeof(name), "raid/%s", vol->v_name); 1644219974Smav if (g_raid_name_format == 0 || vol->v_name[0] == 0 || 1645219974Smav g_provider_by_name(name) != NULL) { 1646219974Smav /* Otherwise use sequential volume number. */ 1647219974Smav snprintf(name, sizeof(name), "raid/r%d", vol->v_global_id); 1648219974Smav } 1649219974Smav pp = g_new_providerf(sc->sc_geom, "%s", name); 1650219974Smav pp->private = vol; 1651219974Smav pp->mediasize = vol->v_mediasize; 1652219974Smav pp->sectorsize = vol->v_sectorsize; 1653219974Smav pp->stripesize = 0; 1654219974Smav pp->stripeoffset = 0; 1655219974Smav if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1 || 1656219974Smav vol->v_raid_level == G_RAID_VOLUME_RL_RAID3 || 1657219974Smav vol->v_raid_level == G_RAID_VOLUME_RL_SINGLE || 1658219974Smav vol->v_raid_level == G_RAID_VOLUME_RL_CONCAT) { 1659219974Smav if ((disk = vol->v_subdisks[0].sd_disk) != NULL && 1660219974Smav disk->d_consumer != NULL && 1661219974Smav disk->d_consumer->provider != NULL) { 1662219974Smav pp->stripesize = disk->d_consumer->provider->stripesize; 1663219974Smav off = disk->d_consumer->provider->stripeoffset; 1664219974Smav pp->stripeoffset = off + vol->v_subdisks[0].sd_offset; 1665219974Smav if (off > 0) 1666219974Smav pp->stripeoffset %= off; 1667219974Smav } 1668219974Smav if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID3) { 1669219974Smav pp->stripesize *= (vol->v_disks_count - 1); 1670219974Smav pp->stripeoffset *= (vol->v_disks_count - 1); 1671219974Smav } 1672219974Smav } else 1673219974Smav pp->stripesize = vol->v_strip_size; 1674219974Smav vol->v_provider = pp; 1675219974Smav g_error_provider(pp, 0); 1676219974Smav g_topology_unlock(); 1677219974Smav G_RAID_DEBUG1(0, sc, "Provider %s for volume %s created.", 1678219974Smav pp->name, vol->v_name); 1679219974Smav} 1680219974Smav 1681219974Smavstatic void 1682219974Smavg_raid_destroy_provider(struct g_raid_volume *vol) 1683219974Smav{ 1684219974Smav struct g_raid_softc *sc; 1685219974Smav struct g_provider *pp; 1686219974Smav struct bio *bp, *tmp; 1687219974Smav 1688219974Smav g_topology_assert_not(); 1689219974Smav sc = vol->v_softc; 1690219974Smav pp = vol->v_provider; 1691219974Smav KASSERT(pp != NULL, ("NULL provider (volume=%s).", vol->v_name)); 1692219974Smav 1693219974Smav g_topology_lock(); 1694219974Smav g_error_provider(pp, ENXIO); 1695219974Smav mtx_lock(&sc->sc_queue_mtx); 1696219974Smav TAILQ_FOREACH_SAFE(bp, &sc->sc_queue.queue, bio_queue, tmp) { 1697219974Smav if (bp->bio_to != pp) 1698219974Smav continue; 1699219974Smav bioq_remove(&sc->sc_queue, bp); 1700219974Smav g_io_deliver(bp, ENXIO); 1701219974Smav } 1702219974Smav mtx_unlock(&sc->sc_queue_mtx); 1703219974Smav G_RAID_DEBUG1(0, sc, "Provider %s for volume %s destroyed.", 1704219974Smav pp->name, vol->v_name); 1705219974Smav g_wither_provider(pp, ENXIO); 1706219974Smav g_topology_unlock(); 1707219974Smav vol->v_provider = NULL; 1708219974Smav} 1709219974Smav 1710219974Smav/* 1711219974Smav * Update device state. 1712219974Smav */ 1713219974Smavstatic int 1714219974Smavg_raid_update_volume(struct g_raid_volume *vol, u_int event) 1715219974Smav{ 1716219974Smav struct g_raid_softc *sc; 1717219974Smav 1718219974Smav sc = vol->v_softc; 1719219974Smav sx_assert(&sc->sc_lock, SX_XLOCKED); 1720219974Smav 1721219974Smav G_RAID_DEBUG1(2, sc, "Event %s for volume %s.", 1722219974Smav g_raid_volume_event2str(event), 1723219974Smav vol->v_name); 1724219974Smav switch (event) { 1725219974Smav case G_RAID_VOLUME_E_DOWN: 1726219974Smav if (vol->v_provider != NULL) 1727219974Smav g_raid_destroy_provider(vol); 1728219974Smav break; 1729219974Smav case G_RAID_VOLUME_E_UP: 1730219974Smav if (vol->v_provider == NULL) 1731219974Smav g_raid_launch_provider(vol); 1732219974Smav break; 1733219974Smav case G_RAID_VOLUME_E_START: 1734219974Smav if (vol->v_tr) 1735219974Smav G_RAID_TR_START(vol->v_tr); 1736219974Smav return (0); 1737219974Smav default: 1738219974Smav if (sc->sc_md) 1739219974Smav G_RAID_MD_VOLUME_EVENT(sc->sc_md, vol, event); 1740219974Smav return (0); 1741219974Smav } 1742219974Smav 1743219974Smav /* Manage root mount release. */ 1744219974Smav if (vol->v_starting) { 1745219974Smav vol->v_starting = 0; 1746219974Smav G_RAID_DEBUG1(1, sc, "root_mount_rel %p", vol->v_rootmount); 1747219974Smav root_mount_rel(vol->v_rootmount); 1748219974Smav vol->v_rootmount = NULL; 1749219974Smav } 1750219974Smav if (vol->v_stopping && vol->v_provider_open == 0) 1751219974Smav g_raid_destroy_volume(vol); 1752219974Smav return (0); 1753219974Smav} 1754219974Smav 1755219974Smav/* 1756219974Smav * Update subdisk state. 1757219974Smav */ 1758219974Smavstatic int 1759219974Smavg_raid_update_subdisk(struct g_raid_subdisk *sd, u_int event) 1760219974Smav{ 1761219974Smav struct g_raid_softc *sc; 1762219974Smav struct g_raid_volume *vol; 1763219974Smav 1764219974Smav sc = sd->sd_softc; 1765219974Smav vol = sd->sd_volume; 1766219974Smav sx_assert(&sc->sc_lock, SX_XLOCKED); 1767219974Smav 1768219974Smav G_RAID_DEBUG1(2, sc, "Event %s for subdisk %s:%d-%s.", 1769219974Smav g_raid_subdisk_event2str(event), 1770219974Smav vol->v_name, sd->sd_pos, 1771219974Smav sd->sd_disk ? g_raid_get_diskname(sd->sd_disk) : "[none]"); 1772219974Smav if (vol->v_tr) 1773219974Smav G_RAID_TR_EVENT(vol->v_tr, sd, event); 1774219974Smav 1775219974Smav return (0); 1776219974Smav} 1777219974Smav 1778219974Smav/* 1779219974Smav * Update disk state. 1780219974Smav */ 1781219974Smavstatic int 1782219974Smavg_raid_update_disk(struct g_raid_disk *disk, u_int event) 1783219974Smav{ 1784219974Smav struct g_raid_softc *sc; 1785219974Smav 1786219974Smav sc = disk->d_softc; 1787219974Smav sx_assert(&sc->sc_lock, SX_XLOCKED); 1788219974Smav 1789219974Smav G_RAID_DEBUG1(2, sc, "Event %s for disk %s.", 1790219974Smav g_raid_disk_event2str(event), 1791219974Smav g_raid_get_diskname(disk)); 1792219974Smav 1793219974Smav if (sc->sc_md) 1794219974Smav G_RAID_MD_EVENT(sc->sc_md, disk, event); 1795219974Smav return (0); 1796219974Smav} 1797219974Smav 1798219974Smav/* 1799219974Smav * Node event. 1800219974Smav */ 1801219974Smavstatic int 1802219974Smavg_raid_update_node(struct g_raid_softc *sc, u_int event) 1803219974Smav{ 1804219974Smav sx_assert(&sc->sc_lock, SX_XLOCKED); 1805219974Smav 1806219974Smav G_RAID_DEBUG1(2, sc, "Event %s for the array.", 1807219974Smav g_raid_node_event2str(event)); 1808219974Smav 1809219974Smav if (event == G_RAID_NODE_E_WAKE) 1810219974Smav return (0); 1811219974Smav if (sc->sc_md) 1812219974Smav G_RAID_MD_EVENT(sc->sc_md, NULL, event); 1813219974Smav return (0); 1814219974Smav} 1815219974Smav 1816219974Smavstatic int 1817219974Smavg_raid_access(struct g_provider *pp, int acr, int acw, int ace) 1818219974Smav{ 1819219974Smav struct g_raid_volume *vol; 1820219974Smav struct g_raid_softc *sc; 1821220210Smav int dcw, opens, error = 0; 1822219974Smav 1823219974Smav g_topology_assert(); 1824219974Smav sc = pp->geom->softc; 1825219974Smav vol = pp->private; 1826219974Smav KASSERT(sc != NULL, ("NULL softc (provider=%s).", pp->name)); 1827219974Smav KASSERT(vol != NULL, ("NULL volume (provider=%s).", pp->name)); 1828219974Smav 1829219974Smav G_RAID_DEBUG1(2, sc, "Access request for %s: r%dw%de%d.", pp->name, 1830219974Smav acr, acw, ace); 1831219974Smav dcw = pp->acw + acw; 1832219974Smav 1833219974Smav g_topology_unlock(); 1834219974Smav sx_xlock(&sc->sc_lock); 1835219974Smav /* Deny new opens while dying. */ 1836219974Smav if (sc->sc_stopping != 0 && (acr > 0 || acw > 0 || ace > 0)) { 1837219974Smav error = ENXIO; 1838219974Smav goto out; 1839219974Smav } 1840242314Smav if (dcw == 0) 1841219974Smav g_raid_clean(vol, dcw); 1842219974Smav vol->v_provider_open += acr + acw + ace; 1843219974Smav /* Handle delayed node destruction. */ 1844219974Smav if (sc->sc_stopping == G_RAID_DESTROY_DELAYED && 1845219974Smav vol->v_provider_open == 0) { 1846219974Smav /* Count open volumes. */ 1847219974Smav opens = g_raid_nopens(sc); 1848219974Smav if (opens == 0) { 1849219974Smav sc->sc_stopping = G_RAID_DESTROY_HARD; 1850219974Smav /* Wake up worker to make it selfdestruct. */ 1851219974Smav g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0); 1852219974Smav } 1853219974Smav } 1854219974Smav /* Handle open volume destruction. */ 1855219974Smav if (vol->v_stopping && vol->v_provider_open == 0) 1856219974Smav g_raid_destroy_volume(vol); 1857219974Smavout: 1858219974Smav sx_xunlock(&sc->sc_lock); 1859219974Smav g_topology_lock(); 1860219974Smav return (error); 1861219974Smav} 1862219974Smav 1863219974Smavstruct g_raid_softc * 1864219974Smavg_raid_create_node(struct g_class *mp, 1865219974Smav const char *name, struct g_raid_md_object *md) 1866219974Smav{ 1867219974Smav struct g_raid_softc *sc; 1868219974Smav struct g_geom *gp; 1869219974Smav int error; 1870219974Smav 1871219974Smav g_topology_assert(); 1872219974Smav G_RAID_DEBUG(1, "Creating array %s.", name); 1873219974Smav 1874219974Smav gp = g_new_geomf(mp, "%s", name); 1875219974Smav sc = malloc(sizeof(*sc), M_RAID, M_WAITOK | M_ZERO); 1876219974Smav gp->start = g_raid_start; 1877219974Smav gp->orphan = g_raid_orphan; 1878219974Smav gp->access = g_raid_access; 1879219974Smav gp->dumpconf = g_raid_dumpconf; 1880219974Smav 1881219974Smav sc->sc_md = md; 1882219974Smav sc->sc_geom = gp; 1883219974Smav sc->sc_flags = 0; 1884219974Smav TAILQ_INIT(&sc->sc_volumes); 1885219974Smav TAILQ_INIT(&sc->sc_disks); 1886234816Smav sx_init(&sc->sc_lock, "graid:lock"); 1887234816Smav mtx_init(&sc->sc_queue_mtx, "graid:queue", NULL, MTX_DEF); 1888219974Smav TAILQ_INIT(&sc->sc_events); 1889219974Smav bioq_init(&sc->sc_queue); 1890219974Smav gp->softc = sc; 1891219974Smav error = kproc_create(g_raid_worker, sc, &sc->sc_worker, 0, 0, 1892219974Smav "g_raid %s", name); 1893219974Smav if (error != 0) { 1894219974Smav G_RAID_DEBUG(0, "Cannot create kernel thread for %s.", name); 1895219974Smav mtx_destroy(&sc->sc_queue_mtx); 1896219974Smav sx_destroy(&sc->sc_lock); 1897219974Smav g_destroy_geom(sc->sc_geom); 1898219974Smav free(sc, M_RAID); 1899219974Smav return (NULL); 1900219974Smav } 1901219974Smav 1902219974Smav G_RAID_DEBUG1(0, sc, "Array %s created.", name); 1903219974Smav return (sc); 1904219974Smav} 1905219974Smav 1906219974Smavstruct g_raid_volume * 1907219974Smavg_raid_create_volume(struct g_raid_softc *sc, const char *name, int id) 1908219974Smav{ 1909219974Smav struct g_raid_volume *vol, *vol1; 1910219974Smav int i; 1911219974Smav 1912219974Smav G_RAID_DEBUG1(1, sc, "Creating volume %s.", name); 1913219974Smav vol = malloc(sizeof(*vol), M_RAID, M_WAITOK | M_ZERO); 1914219974Smav vol->v_softc = sc; 1915219974Smav strlcpy(vol->v_name, name, G_RAID_MAX_VOLUMENAME); 1916219974Smav vol->v_state = G_RAID_VOLUME_S_STARTING; 1917219974Smav vol->v_raid_level = G_RAID_VOLUME_RL_UNKNOWN; 1918219974Smav vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_UNKNOWN; 1919235076Smav vol->v_rotate_parity = 1; 1920219974Smav bioq_init(&vol->v_inflight); 1921219974Smav bioq_init(&vol->v_locked); 1922219974Smav LIST_INIT(&vol->v_locks); 1923219974Smav for (i = 0; i < G_RAID_MAX_SUBDISKS; i++) { 1924219974Smav vol->v_subdisks[i].sd_softc = sc; 1925219974Smav vol->v_subdisks[i].sd_volume = vol; 1926219974Smav vol->v_subdisks[i].sd_pos = i; 1927219974Smav vol->v_subdisks[i].sd_state = G_RAID_DISK_S_NONE; 1928219974Smav } 1929219974Smav 1930219974Smav /* Find free ID for this volume. */ 1931219974Smav g_topology_lock(); 1932219974Smav vol1 = vol; 1933219974Smav if (id >= 0) { 1934219974Smav LIST_FOREACH(vol1, &g_raid_volumes, v_global_next) { 1935219974Smav if (vol1->v_global_id == id) 1936219974Smav break; 1937219974Smav } 1938219974Smav } 1939219974Smav if (vol1 != NULL) { 1940219974Smav for (id = 0; ; id++) { 1941219974Smav LIST_FOREACH(vol1, &g_raid_volumes, v_global_next) { 1942219974Smav if (vol1->v_global_id == id) 1943219974Smav break; 1944219974Smav } 1945219974Smav if (vol1 == NULL) 1946219974Smav break; 1947219974Smav } 1948219974Smav } 1949219974Smav vol->v_global_id = id; 1950219974Smav LIST_INSERT_HEAD(&g_raid_volumes, vol, v_global_next); 1951219974Smav g_topology_unlock(); 1952219974Smav 1953219974Smav /* Delay root mounting. */ 1954219974Smav vol->v_rootmount = root_mount_hold("GRAID"); 1955219974Smav G_RAID_DEBUG1(1, sc, "root_mount_hold %p", vol->v_rootmount); 1956219974Smav vol->v_starting = 1; 1957219974Smav TAILQ_INSERT_TAIL(&sc->sc_volumes, vol, v_next); 1958219974Smav return (vol); 1959219974Smav} 1960219974Smav 1961219974Smavstruct g_raid_disk * 1962219974Smavg_raid_create_disk(struct g_raid_softc *sc) 1963219974Smav{ 1964219974Smav struct g_raid_disk *disk; 1965219974Smav 1966219974Smav G_RAID_DEBUG1(1, sc, "Creating disk."); 1967219974Smav disk = malloc(sizeof(*disk), M_RAID, M_WAITOK | M_ZERO); 1968219974Smav disk->d_softc = sc; 1969219974Smav disk->d_state = G_RAID_DISK_S_NONE; 1970219974Smav TAILQ_INIT(&disk->d_subdisks); 1971219974Smav TAILQ_INSERT_TAIL(&sc->sc_disks, disk, d_next); 1972219974Smav return (disk); 1973219974Smav} 1974219974Smav 1975219974Smavint g_raid_start_volume(struct g_raid_volume *vol) 1976219974Smav{ 1977219974Smav struct g_raid_tr_class *class; 1978219974Smav struct g_raid_tr_object *obj; 1979219974Smav int status; 1980219974Smav 1981219974Smav G_RAID_DEBUG1(2, vol->v_softc, "Starting volume %s.", vol->v_name); 1982219974Smav LIST_FOREACH(class, &g_raid_tr_classes, trc_list) { 1983240465Smav if (!class->trc_enable) 1984240465Smav continue; 1985219974Smav G_RAID_DEBUG1(2, vol->v_softc, 1986219974Smav "Tasting volume %s for %s transformation.", 1987219974Smav vol->v_name, class->name); 1988219974Smav obj = (void *)kobj_create((kobj_class_t)class, M_RAID, 1989219974Smav M_WAITOK); 1990219974Smav obj->tro_class = class; 1991219974Smav obj->tro_volume = vol; 1992219974Smav status = G_RAID_TR_TASTE(obj, vol); 1993219974Smav if (status != G_RAID_TR_TASTE_FAIL) 1994219974Smav break; 1995219974Smav kobj_delete((kobj_t)obj, M_RAID); 1996219974Smav } 1997219974Smav if (class == NULL) { 1998219974Smav G_RAID_DEBUG1(0, vol->v_softc, 1999219974Smav "No transformation module found for %s.", 2000219974Smav vol->v_name); 2001219974Smav vol->v_tr = NULL; 2002219974Smav g_raid_change_volume_state(vol, G_RAID_VOLUME_S_UNSUPPORTED); 2003219974Smav g_raid_event_send(vol, G_RAID_VOLUME_E_DOWN, 2004219974Smav G_RAID_EVENT_VOLUME); 2005219974Smav return (-1); 2006219974Smav } 2007219974Smav G_RAID_DEBUG1(2, vol->v_softc, 2008219974Smav "Transformation module %s chosen for %s.", 2009219974Smav class->name, vol->v_name); 2010219974Smav vol->v_tr = obj; 2011219974Smav return (0); 2012219974Smav} 2013219974Smav 2014219974Smavint 2015219974Smavg_raid_destroy_node(struct g_raid_softc *sc, int worker) 2016219974Smav{ 2017219974Smav struct g_raid_volume *vol, *tmpv; 2018219974Smav struct g_raid_disk *disk, *tmpd; 2019219974Smav int error = 0; 2020219974Smav 2021219974Smav sc->sc_stopping = G_RAID_DESTROY_HARD; 2022219974Smav TAILQ_FOREACH_SAFE(vol, &sc->sc_volumes, v_next, tmpv) { 2023219974Smav if (g_raid_destroy_volume(vol)) 2024219974Smav error = EBUSY; 2025219974Smav } 2026219974Smav if (error) 2027219974Smav return (error); 2028219974Smav TAILQ_FOREACH_SAFE(disk, &sc->sc_disks, d_next, tmpd) { 2029219974Smav if (g_raid_destroy_disk(disk)) 2030219974Smav error = EBUSY; 2031219974Smav } 2032219974Smav if (error) 2033219974Smav return (error); 2034219974Smav if (sc->sc_md) { 2035219974Smav G_RAID_MD_FREE(sc->sc_md); 2036219974Smav kobj_delete((kobj_t)sc->sc_md, M_RAID); 2037219974Smav sc->sc_md = NULL; 2038219974Smav } 2039219974Smav if (sc->sc_geom != NULL) { 2040219974Smav G_RAID_DEBUG1(0, sc, "Array %s destroyed.", sc->sc_name); 2041219974Smav g_topology_lock(); 2042219974Smav sc->sc_geom->softc = NULL; 2043219974Smav g_wither_geom(sc->sc_geom, ENXIO); 2044219974Smav g_topology_unlock(); 2045219974Smav sc->sc_geom = NULL; 2046219974Smav } else 2047219974Smav G_RAID_DEBUG(1, "Array destroyed."); 2048219974Smav if (worker) { 2049219974Smav g_raid_event_cancel(sc, sc); 2050219974Smav mtx_destroy(&sc->sc_queue_mtx); 2051219974Smav sx_xunlock(&sc->sc_lock); 2052219974Smav sx_destroy(&sc->sc_lock); 2053219974Smav wakeup(&sc->sc_stopping); 2054219974Smav free(sc, M_RAID); 2055219974Smav curthread->td_pflags &= ~TDP_GEOM; 2056219974Smav G_RAID_DEBUG(1, "Thread exiting."); 2057219974Smav kproc_exit(0); 2058219974Smav } else { 2059219974Smav /* Wake up worker to make it selfdestruct. */ 2060219974Smav g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0); 2061219974Smav } 2062219974Smav return (0); 2063219974Smav} 2064219974Smav 2065219974Smavint 2066219974Smavg_raid_destroy_volume(struct g_raid_volume *vol) 2067219974Smav{ 2068219974Smav struct g_raid_softc *sc; 2069219974Smav struct g_raid_disk *disk; 2070219974Smav int i; 2071219974Smav 2072219974Smav sc = vol->v_softc; 2073219974Smav G_RAID_DEBUG1(2, sc, "Destroying volume %s.", vol->v_name); 2074219974Smav vol->v_stopping = 1; 2075219974Smav if (vol->v_state != G_RAID_VOLUME_S_STOPPED) { 2076219974Smav if (vol->v_tr) { 2077219974Smav G_RAID_TR_STOP(vol->v_tr); 2078219974Smav return (EBUSY); 2079219974Smav } else 2080219974Smav vol->v_state = G_RAID_VOLUME_S_STOPPED; 2081219974Smav } 2082219974Smav if (g_raid_event_check(sc, vol) != 0) 2083219974Smav return (EBUSY); 2084219974Smav if (vol->v_provider != NULL) 2085219974Smav return (EBUSY); 2086219974Smav if (vol->v_provider_open != 0) 2087219974Smav return (EBUSY); 2088219974Smav if (vol->v_tr) { 2089219974Smav G_RAID_TR_FREE(vol->v_tr); 2090219974Smav kobj_delete((kobj_t)vol->v_tr, M_RAID); 2091219974Smav vol->v_tr = NULL; 2092219974Smav } 2093219974Smav if (vol->v_rootmount) 2094219974Smav root_mount_rel(vol->v_rootmount); 2095219974Smav g_topology_lock(); 2096219974Smav LIST_REMOVE(vol, v_global_next); 2097219974Smav g_topology_unlock(); 2098219974Smav TAILQ_REMOVE(&sc->sc_volumes, vol, v_next); 2099219974Smav for (i = 0; i < G_RAID_MAX_SUBDISKS; i++) { 2100219974Smav g_raid_event_cancel(sc, &vol->v_subdisks[i]); 2101219974Smav disk = vol->v_subdisks[i].sd_disk; 2102219974Smav if (disk == NULL) 2103219974Smav continue; 2104219974Smav TAILQ_REMOVE(&disk->d_subdisks, &vol->v_subdisks[i], sd_next); 2105219974Smav } 2106219974Smav G_RAID_DEBUG1(2, sc, "Volume %s destroyed.", vol->v_name); 2107219974Smav if (sc->sc_md) 2108219974Smav G_RAID_MD_FREE_VOLUME(sc->sc_md, vol); 2109219974Smav g_raid_event_cancel(sc, vol); 2110219974Smav free(vol, M_RAID); 2111219974Smav if (sc->sc_stopping == G_RAID_DESTROY_HARD) { 2112219974Smav /* Wake up worker to let it selfdestruct. */ 2113219974Smav g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0); 2114219974Smav } 2115219974Smav return (0); 2116219974Smav} 2117219974Smav 2118219974Smavint 2119219974Smavg_raid_destroy_disk(struct g_raid_disk *disk) 2120219974Smav{ 2121219974Smav struct g_raid_softc *sc; 2122219974Smav struct g_raid_subdisk *sd, *tmp; 2123219974Smav 2124219974Smav sc = disk->d_softc; 2125219974Smav G_RAID_DEBUG1(2, sc, "Destroying disk."); 2126219974Smav if (disk->d_consumer) { 2127219974Smav g_raid_kill_consumer(sc, disk->d_consumer); 2128219974Smav disk->d_consumer = NULL; 2129219974Smav } 2130219974Smav TAILQ_FOREACH_SAFE(sd, &disk->d_subdisks, sd_next, tmp) { 2131219974Smav g_raid_change_subdisk_state(sd, G_RAID_SUBDISK_S_NONE); 2132219974Smav g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED, 2133219974Smav G_RAID_EVENT_SUBDISK); 2134219974Smav TAILQ_REMOVE(&disk->d_subdisks, sd, sd_next); 2135219974Smav sd->sd_disk = NULL; 2136219974Smav } 2137219974Smav TAILQ_REMOVE(&sc->sc_disks, disk, d_next); 2138219974Smav if (sc->sc_md) 2139219974Smav G_RAID_MD_FREE_DISK(sc->sc_md, disk); 2140219974Smav g_raid_event_cancel(sc, disk); 2141219974Smav free(disk, M_RAID); 2142219974Smav return (0); 2143219974Smav} 2144219974Smav 2145219974Smavint 2146219974Smavg_raid_destroy(struct g_raid_softc *sc, int how) 2147219974Smav{ 2148219974Smav int opens; 2149219974Smav 2150219974Smav g_topology_assert_not(); 2151219974Smav if (sc == NULL) 2152219974Smav return (ENXIO); 2153219974Smav sx_assert(&sc->sc_lock, SX_XLOCKED); 2154219974Smav 2155219974Smav /* Count open volumes. */ 2156219974Smav opens = g_raid_nopens(sc); 2157219974Smav 2158219974Smav /* React on some opened volumes. */ 2159219974Smav if (opens > 0) { 2160219974Smav switch (how) { 2161219974Smav case G_RAID_DESTROY_SOFT: 2162219974Smav G_RAID_DEBUG1(1, sc, 2163219974Smav "%d volumes are still open.", 2164219974Smav opens); 2165219974Smav return (EBUSY); 2166219974Smav case G_RAID_DESTROY_DELAYED: 2167219974Smav G_RAID_DEBUG1(1, sc, 2168219974Smav "Array will be destroyed on last close."); 2169219974Smav sc->sc_stopping = G_RAID_DESTROY_DELAYED; 2170219974Smav return (EBUSY); 2171219974Smav case G_RAID_DESTROY_HARD: 2172219974Smav G_RAID_DEBUG1(1, sc, 2173219974Smav "%d volumes are still open.", 2174219974Smav opens); 2175219974Smav } 2176219974Smav } 2177219974Smav 2178219974Smav /* Mark node for destruction. */ 2179219974Smav sc->sc_stopping = G_RAID_DESTROY_HARD; 2180219974Smav /* Wake up worker to let it selfdestruct. */ 2181219974Smav g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0); 2182219974Smav /* Sleep until node destroyed. */ 2183219974Smav sx_sleep(&sc->sc_stopping, &sc->sc_lock, 2184219974Smav PRIBIO | PDROP, "r:destroy", 0); 2185219974Smav return (0); 2186219974Smav} 2187219974Smav 2188219974Smavstatic void 2189219974Smavg_raid_taste_orphan(struct g_consumer *cp) 2190219974Smav{ 2191219974Smav 2192219974Smav KASSERT(1 == 0, ("%s called while tasting %s.", __func__, 2193219974Smav cp->provider->name)); 2194219974Smav} 2195219974Smav 2196219974Smavstatic struct g_geom * 2197219974Smavg_raid_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) 2198219974Smav{ 2199219974Smav struct g_consumer *cp; 2200219974Smav struct g_geom *gp, *geom; 2201219974Smav struct g_raid_md_class *class; 2202219974Smav struct g_raid_md_object *obj; 2203219974Smav int status; 2204219974Smav 2205219974Smav g_topology_assert(); 2206219974Smav g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name); 2207240465Smav if (!g_raid_enable) 2208240465Smav return (NULL); 2209219974Smav G_RAID_DEBUG(2, "Tasting provider %s.", pp->name); 2210219974Smav 2211234940Smav gp = g_new_geomf(mp, "raid:taste"); 2212219974Smav /* 2213219974Smav * This orphan function should be never called. 2214219974Smav */ 2215219974Smav gp->orphan = g_raid_taste_orphan; 2216219974Smav cp = g_new_consumer(gp); 2217219974Smav g_attach(cp, pp); 2218219974Smav 2219219974Smav geom = NULL; 2220219974Smav LIST_FOREACH(class, &g_raid_md_classes, mdc_list) { 2221240465Smav if (!class->mdc_enable) 2222240465Smav continue; 2223219974Smav G_RAID_DEBUG(2, "Tasting provider %s for %s metadata.", 2224219974Smav pp->name, class->name); 2225219974Smav obj = (void *)kobj_create((kobj_class_t)class, M_RAID, 2226219974Smav M_WAITOK); 2227219974Smav obj->mdo_class = class; 2228219974Smav status = G_RAID_MD_TASTE(obj, mp, cp, &geom); 2229219974Smav if (status != G_RAID_MD_TASTE_NEW) 2230219974Smav kobj_delete((kobj_t)obj, M_RAID); 2231219974Smav if (status != G_RAID_MD_TASTE_FAIL) 2232219974Smav break; 2233219974Smav } 2234219974Smav 2235219974Smav g_detach(cp); 2236219974Smav g_destroy_consumer(cp); 2237219974Smav g_destroy_geom(gp); 2238219974Smav G_RAID_DEBUG(2, "Tasting provider %s done.", pp->name); 2239219974Smav return (geom); 2240219974Smav} 2241219974Smav 2242219974Smavint 2243234940Smavg_raid_create_node_format(const char *format, struct gctl_req *req, 2244234940Smav struct g_geom **gp) 2245219974Smav{ 2246219974Smav struct g_raid_md_class *class; 2247219974Smav struct g_raid_md_object *obj; 2248219974Smav int status; 2249219974Smav 2250219974Smav G_RAID_DEBUG(2, "Creating array for %s metadata.", format); 2251219974Smav LIST_FOREACH(class, &g_raid_md_classes, mdc_list) { 2252219974Smav if (strcasecmp(class->name, format) == 0) 2253219974Smav break; 2254219974Smav } 2255219974Smav if (class == NULL) { 2256219974Smav G_RAID_DEBUG(1, "No support for %s metadata.", format); 2257219974Smav return (G_RAID_MD_TASTE_FAIL); 2258219974Smav } 2259219974Smav obj = (void *)kobj_create((kobj_class_t)class, M_RAID, 2260219974Smav M_WAITOK); 2261219974Smav obj->mdo_class = class; 2262234940Smav status = G_RAID_MD_CREATE_REQ(obj, &g_raid_class, req, gp); 2263219974Smav if (status != G_RAID_MD_TASTE_NEW) 2264219974Smav kobj_delete((kobj_t)obj, M_RAID); 2265219974Smav return (status); 2266219974Smav} 2267219974Smav 2268219974Smavstatic int 2269219974Smavg_raid_destroy_geom(struct gctl_req *req __unused, 2270219974Smav struct g_class *mp __unused, struct g_geom *gp) 2271219974Smav{ 2272219974Smav struct g_raid_softc *sc; 2273219974Smav int error; 2274219974Smav 2275219974Smav g_topology_unlock(); 2276219974Smav sc = gp->softc; 2277219974Smav sx_xlock(&sc->sc_lock); 2278219974Smav g_cancel_event(sc); 2279219974Smav error = g_raid_destroy(gp->softc, G_RAID_DESTROY_SOFT); 2280219974Smav if (error != 0) 2281219974Smav sx_xunlock(&sc->sc_lock); 2282219974Smav g_topology_lock(); 2283219974Smav return (error); 2284219974Smav} 2285219974Smav 2286219974Smavvoid g_raid_write_metadata(struct g_raid_softc *sc, struct g_raid_volume *vol, 2287219974Smav struct g_raid_subdisk *sd, struct g_raid_disk *disk) 2288219974Smav{ 2289219974Smav 2290219974Smav if (sc->sc_stopping == G_RAID_DESTROY_HARD) 2291219974Smav return; 2292219974Smav if (sc->sc_md) 2293219974Smav G_RAID_MD_WRITE(sc->sc_md, vol, sd, disk); 2294219974Smav} 2295219974Smav 2296219974Smavvoid g_raid_fail_disk(struct g_raid_softc *sc, 2297219974Smav struct g_raid_subdisk *sd, struct g_raid_disk *disk) 2298219974Smav{ 2299219974Smav 2300219974Smav if (disk == NULL) 2301219974Smav disk = sd->sd_disk; 2302219974Smav if (disk == NULL) { 2303219974Smav G_RAID_DEBUG1(0, sc, "Warning! Fail request to an absent disk!"); 2304219974Smav return; 2305219974Smav } 2306219974Smav if (disk->d_state != G_RAID_DISK_S_ACTIVE) { 2307219974Smav G_RAID_DEBUG1(0, sc, "Warning! Fail request to a disk in a " 2308219974Smav "wrong state (%s)!", g_raid_disk_state2str(disk->d_state)); 2309219974Smav return; 2310219974Smav } 2311219974Smav if (sc->sc_md) 2312219974Smav G_RAID_MD_FAIL_DISK(sc->sc_md, sd, disk); 2313219974Smav} 2314219974Smav 2315219974Smavstatic void 2316219974Smavg_raid_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 2317219974Smav struct g_consumer *cp, struct g_provider *pp) 2318219974Smav{ 2319219974Smav struct g_raid_softc *sc; 2320219974Smav struct g_raid_volume *vol; 2321219974Smav struct g_raid_subdisk *sd; 2322219974Smav struct g_raid_disk *disk; 2323219974Smav int i, s; 2324219974Smav 2325219974Smav g_topology_assert(); 2326219974Smav 2327219974Smav sc = gp->softc; 2328219974Smav if (sc == NULL) 2329219974Smav return; 2330219974Smav if (pp != NULL) { 2331219974Smav vol = pp->private; 2332219974Smav g_topology_unlock(); 2333219974Smav sx_xlock(&sc->sc_lock); 2334219974Smav sbuf_printf(sb, "%s<Label>%s</Label>\n", indent, 2335219974Smav vol->v_name); 2336219974Smav sbuf_printf(sb, "%s<RAIDLevel>%s</RAIDLevel>\n", indent, 2337219974Smav g_raid_volume_level2str(vol->v_raid_level, 2338219974Smav vol->v_raid_level_qualifier)); 2339219974Smav sbuf_printf(sb, 2340219974Smav "%s<Transformation>%s</Transformation>\n", indent, 2341219974Smav vol->v_tr ? vol->v_tr->tro_class->name : "NONE"); 2342219974Smav sbuf_printf(sb, "%s<Components>%u</Components>\n", indent, 2343219974Smav vol->v_disks_count); 2344219974Smav sbuf_printf(sb, "%s<Strip>%u</Strip>\n", indent, 2345219974Smav vol->v_strip_size); 2346219974Smav sbuf_printf(sb, "%s<State>%s</State>\n", indent, 2347219974Smav g_raid_volume_state2str(vol->v_state)); 2348219974Smav sbuf_printf(sb, "%s<Dirty>%s</Dirty>\n", indent, 2349219974Smav vol->v_dirty ? "Yes" : "No"); 2350219974Smav sbuf_printf(sb, "%s<Subdisks>", indent); 2351219974Smav for (i = 0; i < vol->v_disks_count; i++) { 2352219974Smav sd = &vol->v_subdisks[i]; 2353219974Smav if (sd->sd_disk != NULL && 2354219974Smav sd->sd_disk->d_consumer != NULL) { 2355219974Smav sbuf_printf(sb, "%s ", 2356219974Smav g_raid_get_diskname(sd->sd_disk)); 2357219974Smav } else { 2358219974Smav sbuf_printf(sb, "NONE "); 2359219974Smav } 2360219974Smav sbuf_printf(sb, "(%s", 2361219974Smav g_raid_subdisk_state2str(sd->sd_state)); 2362219974Smav if (sd->sd_state == G_RAID_SUBDISK_S_REBUILD || 2363219974Smav sd->sd_state == G_RAID_SUBDISK_S_RESYNC) { 2364219974Smav sbuf_printf(sb, " %d%%", 2365219974Smav (int)(sd->sd_rebuild_pos * 100 / 2366219974Smav sd->sd_size)); 2367219974Smav } 2368219974Smav sbuf_printf(sb, ")"); 2369219974Smav if (i + 1 < vol->v_disks_count) 2370219974Smav sbuf_printf(sb, ", "); 2371219974Smav } 2372219974Smav sbuf_printf(sb, "</Subdisks>\n"); 2373219974Smav sx_xunlock(&sc->sc_lock); 2374219974Smav g_topology_lock(); 2375219974Smav } else if (cp != NULL) { 2376219974Smav disk = cp->private; 2377219974Smav if (disk == NULL) 2378219974Smav return; 2379219974Smav g_topology_unlock(); 2380219974Smav sx_xlock(&sc->sc_lock); 2381219974Smav sbuf_printf(sb, "%s<State>%s", indent, 2382219974Smav g_raid_disk_state2str(disk->d_state)); 2383219974Smav if (!TAILQ_EMPTY(&disk->d_subdisks)) { 2384219974Smav sbuf_printf(sb, " ("); 2385219974Smav TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) { 2386219974Smav sbuf_printf(sb, "%s", 2387219974Smav g_raid_subdisk_state2str(sd->sd_state)); 2388219974Smav if (sd->sd_state == G_RAID_SUBDISK_S_REBUILD || 2389219974Smav sd->sd_state == G_RAID_SUBDISK_S_RESYNC) { 2390219974Smav sbuf_printf(sb, " %d%%", 2391219974Smav (int)(sd->sd_rebuild_pos * 100 / 2392219974Smav sd->sd_size)); 2393219974Smav } 2394219974Smav if (TAILQ_NEXT(sd, sd_next)) 2395219974Smav sbuf_printf(sb, ", "); 2396219974Smav } 2397219974Smav sbuf_printf(sb, ")"); 2398219974Smav } 2399219974Smav sbuf_printf(sb, "</State>\n"); 2400219974Smav sbuf_printf(sb, "%s<Subdisks>", indent); 2401219974Smav TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) { 2402219974Smav sbuf_printf(sb, "r%d(%s):%d@%ju", 2403219974Smav sd->sd_volume->v_global_id, 2404219974Smav sd->sd_volume->v_name, 2405219974Smav sd->sd_pos, sd->sd_offset); 2406219974Smav if (TAILQ_NEXT(sd, sd_next)) 2407219974Smav sbuf_printf(sb, ", "); 2408219974Smav } 2409219974Smav sbuf_printf(sb, "</Subdisks>\n"); 2410219974Smav sbuf_printf(sb, "%s<ReadErrors>%d</ReadErrors>\n", indent, 2411219974Smav disk->d_read_errs); 2412219974Smav sx_xunlock(&sc->sc_lock); 2413219974Smav g_topology_lock(); 2414219974Smav } else { 2415219974Smav g_topology_unlock(); 2416219974Smav sx_xlock(&sc->sc_lock); 2417219974Smav if (sc->sc_md) { 2418219974Smav sbuf_printf(sb, "%s<Metadata>%s</Metadata>\n", indent, 2419219974Smav sc->sc_md->mdo_class->name); 2420219974Smav } 2421219974Smav if (!TAILQ_EMPTY(&sc->sc_volumes)) { 2422219974Smav s = 0xff; 2423219974Smav TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) { 2424219974Smav if (vol->v_state < s) 2425219974Smav s = vol->v_state; 2426219974Smav } 2427219974Smav sbuf_printf(sb, "%s<State>%s</State>\n", indent, 2428219974Smav g_raid_volume_state2str(s)); 2429219974Smav } 2430219974Smav sx_xunlock(&sc->sc_lock); 2431219974Smav g_topology_lock(); 2432219974Smav } 2433219974Smav} 2434219974Smav 2435219974Smavstatic void 2436242314Smavg_raid_shutdown_post_sync(void *arg, int howto) 2437219974Smav{ 2438219974Smav struct g_class *mp; 2439219974Smav struct g_geom *gp, *gp2; 2440219974Smav struct g_raid_softc *sc; 2441242314Smav struct g_raid_volume *vol; 2442219974Smav int error; 2443219974Smav 2444219974Smav mp = arg; 2445219974Smav DROP_GIANT(); 2446219974Smav g_topology_lock(); 2447242314Smav g_raid_shutdown = 1; 2448219974Smav LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) { 2449219974Smav if ((sc = gp->softc) == NULL) 2450219974Smav continue; 2451219974Smav g_topology_unlock(); 2452219974Smav sx_xlock(&sc->sc_lock); 2453242314Smav TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) 2454242314Smav g_raid_clean(vol, -1); 2455219974Smav g_cancel_event(sc); 2456219974Smav error = g_raid_destroy(sc, G_RAID_DESTROY_DELAYED); 2457219974Smav if (error != 0) 2458219974Smav sx_xunlock(&sc->sc_lock); 2459219974Smav g_topology_lock(); 2460219974Smav } 2461219974Smav g_topology_unlock(); 2462219974Smav PICKUP_GIANT(); 2463219974Smav} 2464219974Smav 2465219974Smavstatic void 2466219974Smavg_raid_init(struct g_class *mp) 2467219974Smav{ 2468219974Smav 2469242314Smav g_raid_post_sync = EVENTHANDLER_REGISTER(shutdown_post_sync, 2470242314Smav g_raid_shutdown_post_sync, mp, SHUTDOWN_PRI_FIRST); 2471242314Smav if (g_raid_post_sync == NULL) 2472219974Smav G_RAID_DEBUG(0, "Warning! Cannot register shutdown event."); 2473219974Smav g_raid_started = 1; 2474219974Smav} 2475219974Smav 2476219974Smavstatic void 2477219974Smavg_raid_fini(struct g_class *mp) 2478219974Smav{ 2479219974Smav 2480242314Smav if (g_raid_post_sync != NULL) 2481242314Smav EVENTHANDLER_DEREGISTER(shutdown_post_sync, g_raid_post_sync); 2482219974Smav g_raid_started = 0; 2483219974Smav} 2484219974Smav 2485219974Smavint 2486219974Smavg_raid_md_modevent(module_t mod, int type, void *arg) 2487219974Smav{ 2488219974Smav struct g_raid_md_class *class, *c, *nc; 2489219974Smav int error; 2490219974Smav 2491219974Smav error = 0; 2492219974Smav class = arg; 2493219974Smav switch (type) { 2494219974Smav case MOD_LOAD: 2495219974Smav c = LIST_FIRST(&g_raid_md_classes); 2496219974Smav if (c == NULL || c->mdc_priority > class->mdc_priority) 2497219974Smav LIST_INSERT_HEAD(&g_raid_md_classes, class, mdc_list); 2498219974Smav else { 2499219974Smav while ((nc = LIST_NEXT(c, mdc_list)) != NULL && 2500219974Smav nc->mdc_priority < class->mdc_priority) 2501219974Smav c = nc; 2502219974Smav LIST_INSERT_AFTER(c, class, mdc_list); 2503219974Smav } 2504219974Smav if (g_raid_started) 2505219974Smav g_retaste(&g_raid_class); 2506219974Smav break; 2507219974Smav case MOD_UNLOAD: 2508219974Smav LIST_REMOVE(class, mdc_list); 2509219974Smav break; 2510219974Smav default: 2511219974Smav error = EOPNOTSUPP; 2512219974Smav break; 2513219974Smav } 2514219974Smav 2515219974Smav return (error); 2516219974Smav} 2517219974Smav 2518219974Smavint 2519219974Smavg_raid_tr_modevent(module_t mod, int type, void *arg) 2520219974Smav{ 2521219974Smav struct g_raid_tr_class *class, *c, *nc; 2522219974Smav int error; 2523219974Smav 2524219974Smav error = 0; 2525219974Smav class = arg; 2526219974Smav switch (type) { 2527219974Smav case MOD_LOAD: 2528219974Smav c = LIST_FIRST(&g_raid_tr_classes); 2529219974Smav if (c == NULL || c->trc_priority > class->trc_priority) 2530219974Smav LIST_INSERT_HEAD(&g_raid_tr_classes, class, trc_list); 2531219974Smav else { 2532219974Smav while ((nc = LIST_NEXT(c, trc_list)) != NULL && 2533219974Smav nc->trc_priority < class->trc_priority) 2534219974Smav c = nc; 2535219974Smav LIST_INSERT_AFTER(c, class, trc_list); 2536219974Smav } 2537219974Smav break; 2538219974Smav case MOD_UNLOAD: 2539219974Smav LIST_REMOVE(class, trc_list); 2540219974Smav break; 2541219974Smav default: 2542219974Smav error = EOPNOTSUPP; 2543219974Smav break; 2544219974Smav } 2545219974Smav 2546219974Smav return (error); 2547219974Smav} 2548219974Smav 2549219974Smav/* 2550219974Smav * Use local implementation of DECLARE_GEOM_CLASS(g_raid_class, g_raid) 2551219974Smav * to reduce module priority, allowing submodules to register them first. 2552219974Smav */ 2553219974Smavstatic moduledata_t g_raid_mod = { 2554219974Smav "g_raid", 2555219974Smav g_modevent, 2556219974Smav &g_raid_class 2557219974Smav}; 2558219974SmavDECLARE_MODULE(g_raid, g_raid_mod, SI_SUB_DRIVERS, SI_ORDER_THIRD); 2559219974SmavMODULE_VERSION(geom_raid, 0); 2560