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