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