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