1 /*- 2 * Copyright (c) 2011 Alexander Motin <mav@FreeBSD.org> 3 * Copyright (c) 2000 - 2008 Søren Schmidt <sos@FreeBSD.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/param.h> 32 #include <sys/bio.h> 33 #include <sys/endian.h> 34 #include <sys/kernel.h> 35 #include <sys/kobj.h> 36 #include <sys/limits.h> 37 #include <sys/lock.h> 38 #include <sys/malloc.h> 39 #include <sys/mutex.h> 40 #include <sys/systm.h> 41 #include <sys/taskqueue.h> 42 #include <geom/geom.h> 43 #include "geom/raid/g_raid.h" 44 #include "g_raid_md_if.h" 45 46 static MALLOC_DEFINE(M_MD_SII, "md_sii_data", "GEOM_RAID SiI metadata"); 47 48 struct sii_raid_conf { 49 uint16_t ata_params_00_53[54]; 50 uint64_t total_sectors; /* 54 - 57 */ 51 uint16_t ata_params_58_81[72]; 52 uint16_t product_id; /* 130 */ 53 uint16_t vendor_id; /* 131 */ 54 uint16_t version_minor; /* 132 */ 55 uint16_t version_major; /* 133 */ 56 uint8_t timestamp[6]; /* 134 - 136 */ 57 uint16_t strip_sectors; /* 137 */ 58 uint16_t dummy_2; 59 uint8_t disk_number; /* 139 */ 60 uint8_t type; 61 #define SII_T_RAID0 0x00 62 #define SII_T_RAID1 0x01 63 #define SII_T_RAID01 0x02 64 #define SII_T_SPARE 0x03 65 #define SII_T_CONCAT 0x04 66 #define SII_T_RAID5 0x10 67 #define SII_T_RESERVED 0xfd 68 #define SII_T_JBOD 0xff 69 70 uint8_t raid0_disks; /* 140 */ 71 uint8_t raid0_ident; 72 uint8_t raid1_disks; /* 141 */ 73 uint8_t raid1_ident; 74 uint64_t rebuild_lba; /* 142 - 145 */ 75 uint32_t generation; /* 146 - 147 */ 76 uint8_t disk_status; /* 148 */ 77 #define SII_S_CURRENT 0x01 78 #define SII_S_REBUILD 0x02 79 #define SII_S_DROPPED 0x03 80 #define SII_S_REMOVED 0x04 81 82 uint8_t raid_status; 83 #define SII_S_ONLINE 0x01 84 #define SII_S_AVAILABLE 0x02 85 86 uint8_t raid_location; /* 149 */ 87 uint8_t disk_location; 88 uint8_t auto_rebuild; /* 150 */ 89 #define SII_R_REBUILD 0x00 90 #define SII_R_NOREBUILD 0xff 91 92 uint8_t dummy_3; 93 uint8_t name[16]; /* 151 - 158 */ 94 uint16_t checksum; /* 159 */ 95 uint16_t ata_params_160_255[96]; 96 } __packed; 97 98 struct g_raid_md_sii_perdisk { 99 struct sii_raid_conf *pd_meta; 100 int pd_disk_pos; 101 off_t pd_disk_size; 102 }; 103 104 struct g_raid_md_sii_object { 105 struct g_raid_md_object mdio_base; 106 uint8_t mdio_timestamp[6]; 107 uint8_t mdio_location; 108 uint32_t mdio_generation; 109 struct sii_raid_conf *mdio_meta; 110 struct callout mdio_start_co; /* STARTING state timer. */ 111 int mdio_total_disks; 112 int mdio_disks_present; 113 int mdio_started; 114 int mdio_incomplete; 115 struct root_hold_token *mdio_rootmount; /* Root mount delay token. */ 116 }; 117 118 static g_raid_md_create_t g_raid_md_create_sii; 119 static g_raid_md_taste_t g_raid_md_taste_sii; 120 static g_raid_md_event_t g_raid_md_event_sii; 121 static g_raid_md_ctl_t g_raid_md_ctl_sii; 122 static g_raid_md_write_t g_raid_md_write_sii; 123 static g_raid_md_fail_disk_t g_raid_md_fail_disk_sii; 124 static g_raid_md_free_disk_t g_raid_md_free_disk_sii; 125 static g_raid_md_free_t g_raid_md_free_sii; 126 127 static kobj_method_t g_raid_md_sii_methods[] = { 128 KOBJMETHOD(g_raid_md_create, g_raid_md_create_sii), 129 KOBJMETHOD(g_raid_md_taste, g_raid_md_taste_sii), 130 KOBJMETHOD(g_raid_md_event, g_raid_md_event_sii), 131 KOBJMETHOD(g_raid_md_ctl, g_raid_md_ctl_sii), 132 KOBJMETHOD(g_raid_md_write, g_raid_md_write_sii), 133 KOBJMETHOD(g_raid_md_fail_disk, g_raid_md_fail_disk_sii), 134 KOBJMETHOD(g_raid_md_free_disk, g_raid_md_free_disk_sii), 135 KOBJMETHOD(g_raid_md_free, g_raid_md_free_sii), 136 { 0, 0 } 137 }; 138 139 static struct g_raid_md_class g_raid_md_sii_class = { 140 "SiI", 141 g_raid_md_sii_methods, 142 sizeof(struct g_raid_md_sii_object), 143 .mdc_enable = 1, 144 .mdc_priority = 100 145 }; 146 147 static void 148 g_raid_md_sii_print(struct sii_raid_conf *meta) 149 { 150 151 if (g_raid_debug < 1) 152 return; 153 154 printf("********* ATA SiI RAID Metadata *********\n"); 155 printf("total_sectors %llu\n", 156 (long long unsigned)meta->total_sectors); 157 printf("product_id 0x%04x\n", meta->product_id); 158 printf("vendor_id 0x%04x\n", meta->vendor_id); 159 printf("version_minor 0x%04x\n", meta->version_minor); 160 printf("version_major 0x%04x\n", meta->version_major); 161 printf("timestamp 0x%02x%02x%02x%02x%02x%02x\n", 162 meta->timestamp[5], meta->timestamp[4], meta->timestamp[3], 163 meta->timestamp[2], meta->timestamp[1], meta->timestamp[0]); 164 printf("strip_sectors %d\n", meta->strip_sectors); 165 printf("disk_number %d\n", meta->disk_number); 166 printf("type 0x%02x\n", meta->type); 167 printf("raid0_disks %d\n", meta->raid0_disks); 168 printf("raid0_ident %d\n", meta->raid0_ident); 169 printf("raid1_disks %d\n", meta->raid1_disks); 170 printf("raid1_ident %d\n", meta->raid1_ident); 171 printf("rebuild_lba %llu\n", 172 (long long unsigned)meta->rebuild_lba); 173 printf("generation %d\n", meta->generation); 174 printf("disk_status %d\n", meta->disk_status); 175 printf("raid_status %d\n", meta->raid_status); 176 printf("raid_location %d\n", meta->raid_location); 177 printf("disk_location %d\n", meta->disk_location); 178 printf("auto_rebuild %d\n", meta->auto_rebuild); 179 printf("name <%.16s>\n", meta->name); 180 printf("checksum 0x%04x\n", meta->checksum); 181 printf("=================================================\n"); 182 } 183 184 static struct sii_raid_conf * 185 sii_meta_copy(struct sii_raid_conf *meta) 186 { 187 struct sii_raid_conf *nmeta; 188 189 nmeta = malloc(sizeof(*meta), M_MD_SII, M_WAITOK); 190 memcpy(nmeta, meta, sizeof(*meta)); 191 return (nmeta); 192 } 193 194 static int 195 sii_meta_total_disks(struct sii_raid_conf *meta) 196 { 197 198 switch (meta->type) { 199 case SII_T_RAID0: 200 case SII_T_RAID5: 201 case SII_T_CONCAT: 202 return (meta->raid0_disks); 203 case SII_T_RAID1: 204 return (meta->raid1_disks); 205 case SII_T_RAID01: 206 return (meta->raid0_disks * meta->raid1_disks); 207 case SII_T_SPARE: 208 case SII_T_JBOD: 209 return (1); 210 } 211 return (0); 212 } 213 214 static int 215 sii_meta_disk_pos(struct sii_raid_conf *meta, struct sii_raid_conf *pdmeta) 216 { 217 218 if (pdmeta->type == SII_T_SPARE) 219 return (-3); 220 221 if (memcmp(&meta->timestamp, &pdmeta->timestamp, 6) != 0) 222 return (-1); 223 224 switch (pdmeta->type) { 225 case SII_T_RAID0: 226 case SII_T_RAID1: 227 case SII_T_RAID5: 228 case SII_T_CONCAT: 229 return (pdmeta->disk_number); 230 case SII_T_RAID01: 231 return (pdmeta->raid1_ident * pdmeta->raid1_disks + 232 pdmeta->raid0_ident); 233 case SII_T_JBOD: 234 return (0); 235 } 236 return (-1); 237 } 238 239 static void 240 sii_meta_get_name(struct sii_raid_conf *meta, char *buf) 241 { 242 int i; 243 244 strncpy(buf, meta->name, 16); 245 buf[16] = 0; 246 for (i = 15; i >= 0; i--) { 247 if (buf[i] > 0x20) 248 break; 249 buf[i] = 0; 250 } 251 } 252 253 static void 254 sii_meta_put_name(struct sii_raid_conf *meta, char *buf) 255 { 256 257 memset(meta->name, 0x20, 16); 258 memcpy(meta->name, buf, MIN(strlen(buf), 16)); 259 } 260 261 static struct sii_raid_conf * 262 sii_meta_read(struct g_consumer *cp) 263 { 264 struct g_provider *pp; 265 struct sii_raid_conf *meta; 266 char *buf; 267 int error, i; 268 uint16_t checksum, *ptr; 269 270 pp = cp->provider; 271 272 /* Read the anchor sector. */ 273 buf = g_read_data(cp, 274 pp->mediasize - pp->sectorsize, pp->sectorsize, &error); 275 if (buf == NULL) { 276 G_RAID_DEBUG(1, "Cannot read metadata from %s (error=%d).", 277 pp->name, error); 278 return (NULL); 279 } 280 meta = malloc(sizeof(*meta), M_MD_SII, M_WAITOK); 281 memcpy(meta, buf, min(sizeof(*meta), pp->sectorsize)); 282 g_free(buf); 283 284 /* Check vendor ID. */ 285 if (meta->vendor_id != 0x1095) { 286 G_RAID_DEBUG(1, "SiI vendor ID check failed on %s (0x%04x)", 287 pp->name, meta->vendor_id); 288 free(meta, M_MD_SII); 289 return (NULL); 290 } 291 292 /* Check metadata major version. */ 293 if (meta->version_major != 2) { 294 G_RAID_DEBUG(1, "SiI version check failed on %s (%d.%d)", 295 pp->name, meta->version_major, meta->version_minor); 296 free(meta, M_MD_SII); 297 return (NULL); 298 } 299 300 /* Check metadata checksum. */ 301 for (checksum = 0, ptr = (uint16_t *)meta, i = 0; i <= 159; i++) 302 checksum += *ptr++; 303 if (checksum != 0) { 304 G_RAID_DEBUG(1, "SiI checksum check failed on %s", pp->name); 305 free(meta, M_MD_SII); 306 return (NULL); 307 } 308 309 /* Check raid type. */ 310 if (meta->type != SII_T_RAID0 && meta->type != SII_T_RAID1 && 311 meta->type != SII_T_RAID01 && meta->type != SII_T_SPARE && 312 meta->type != SII_T_RAID5 && meta->type != SII_T_CONCAT && 313 meta->type != SII_T_JBOD) { 314 G_RAID_DEBUG(1, "SiI unknown RAID level on %s (0x%02x)", 315 pp->name, meta->type); 316 free(meta, M_MD_SII); 317 return (NULL); 318 } 319 320 return (meta); 321 } 322 323 static int 324 sii_meta_write(struct g_consumer *cp, struct sii_raid_conf *meta) 325 { 326 struct g_provider *pp; 327 char *buf; 328 int error, i; 329 uint16_t checksum, *ptr; 330 331 pp = cp->provider; 332 333 /* Recalculate checksum for case if metadata were changed. */ 334 meta->checksum = 0; 335 for (checksum = 0, ptr = (uint16_t *)meta, i = 0; i < 159; i++) 336 checksum += *ptr++; 337 meta->checksum -= checksum; 338 339 /* Create and fill buffer. */ 340 buf = malloc(pp->sectorsize, M_MD_SII, M_WAITOK | M_ZERO); 341 memcpy(buf, meta, sizeof(*meta)); 342 343 /* Write 4 copies of metadata. */ 344 for (i = 0; i < 4; i++) { 345 error = g_write_data(cp, 346 pp->mediasize - (pp->sectorsize * (1 + 0x200 * i)), 347 buf, pp->sectorsize); 348 if (error != 0) { 349 G_RAID_DEBUG(1, "Cannot write metadata to %s (error=%d).", 350 pp->name, error); 351 break; 352 } 353 } 354 355 free(buf, M_MD_SII); 356 return (error); 357 } 358 359 static int 360 sii_meta_erase(struct g_consumer *cp) 361 { 362 struct g_provider *pp; 363 char *buf; 364 int error, i; 365 366 pp = cp->provider; 367 buf = malloc(pp->sectorsize, M_MD_SII, M_WAITOK | M_ZERO); 368 /* Write 4 copies of metadata. */ 369 for (i = 0; i < 4; i++) { 370 error = g_write_data(cp, 371 pp->mediasize - (pp->sectorsize * (1 + 0x200 * i)), 372 buf, pp->sectorsize); 373 if (error != 0) { 374 G_RAID_DEBUG(1, "Cannot erase metadata on %s (error=%d).", 375 pp->name, error); 376 } 377 } 378 free(buf, M_MD_SII); 379 return (error); 380 } 381 382 static int 383 sii_meta_write_spare(struct g_consumer *cp) 384 { 385 struct sii_raid_conf *meta; 386 int error; 387 388 meta = malloc(sizeof(*meta), M_MD_SII, M_WAITOK | M_ZERO); 389 meta->total_sectors = cp->provider->mediasize / 390 cp->provider->sectorsize - 0x800; 391 meta->vendor_id = 0x1095; 392 meta->version_minor = 0; 393 meta->version_major = 2; 394 meta->timestamp[0] = arc4random(); 395 meta->timestamp[1] = arc4random(); 396 meta->timestamp[2] = arc4random(); 397 meta->timestamp[3] = arc4random(); 398 meta->timestamp[4] = arc4random(); 399 meta->timestamp[5] = arc4random(); 400 meta->type = SII_T_SPARE; 401 meta->generation = 1; 402 meta->raid1_ident = 0xff; 403 meta->raid_location = arc4random(); 404 error = sii_meta_write(cp, meta); 405 free(meta, M_MD_SII); 406 return (error); 407 } 408 409 static struct g_raid_disk * 410 g_raid_md_sii_get_disk(struct g_raid_softc *sc, int id) 411 { 412 struct g_raid_disk *disk; 413 struct g_raid_md_sii_perdisk *pd; 414 415 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 416 pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data; 417 if (pd->pd_disk_pos == id) 418 break; 419 } 420 return (disk); 421 } 422 423 static int 424 g_raid_md_sii_supported(int level, int qual, int disks, int force) 425 { 426 427 if (disks > 8) 428 return (0); 429 switch (level) { 430 case G_RAID_VOLUME_RL_RAID0: 431 if (disks < 1) 432 return (0); 433 if (!force && (disks < 2 || disks > 6)) 434 return (0); 435 break; 436 case G_RAID_VOLUME_RL_RAID1: 437 if (disks < 1) 438 return (0); 439 if (!force && (disks != 2)) 440 return (0); 441 break; 442 case G_RAID_VOLUME_RL_RAID1E: 443 if (disks < 2) 444 return (0); 445 if (disks % 2 != 0) 446 return (0); 447 if (!force && (disks < 4)) 448 return (0); 449 break; 450 case G_RAID_VOLUME_RL_SINGLE: 451 if (disks != 1) 452 return (0); 453 break; 454 case G_RAID_VOLUME_RL_CONCAT: 455 if (disks < 2) 456 return (0); 457 break; 458 case G_RAID_VOLUME_RL_RAID5: 459 if (disks < 3) 460 return (0); 461 if (qual != G_RAID_VOLUME_RLQ_R5LS) 462 return (0); 463 break; 464 default: 465 return (0); 466 } 467 if (level != G_RAID_VOLUME_RL_RAID5 && qual != G_RAID_VOLUME_RLQ_NONE) 468 return (0); 469 return (1); 470 } 471 472 static int 473 g_raid_md_sii_start_disk(struct g_raid_disk *disk) 474 { 475 struct g_raid_softc *sc; 476 struct g_raid_subdisk *sd, *tmpsd; 477 struct g_raid_disk *olddisk, *tmpdisk; 478 struct g_raid_md_object *md; 479 struct g_raid_md_sii_object *mdi; 480 struct g_raid_md_sii_perdisk *pd, *oldpd; 481 struct sii_raid_conf *meta; 482 int disk_pos, resurrection = 0; 483 484 sc = disk->d_softc; 485 md = sc->sc_md; 486 mdi = (struct g_raid_md_sii_object *)md; 487 meta = mdi->mdio_meta; 488 pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data; 489 olddisk = NULL; 490 491 /* Find disk position in metadata by it's serial. */ 492 if (pd->pd_meta != NULL) 493 disk_pos = sii_meta_disk_pos(meta, pd->pd_meta); 494 else 495 disk_pos = -3; 496 if (disk_pos < 0) { 497 G_RAID_DEBUG1(1, sc, "Unknown, probably new or stale disk"); 498 /* If we are in the start process, that's all for now. */ 499 if (!mdi->mdio_started) 500 goto nofit; 501 /* 502 * If we have already started - try to get use of the disk. 503 * Try to replace OFFLINE disks first, then FAILED. 504 */ 505 TAILQ_FOREACH(tmpdisk, &sc->sc_disks, d_next) { 506 if (tmpdisk->d_state != G_RAID_DISK_S_OFFLINE && 507 tmpdisk->d_state != G_RAID_DISK_S_FAILED) 508 continue; 509 /* Make sure this disk is big enough. */ 510 TAILQ_FOREACH(sd, &tmpdisk->d_subdisks, sd_next) { 511 if (sd->sd_offset + sd->sd_size + 512 > 512 pd->pd_disk_size) { 513 G_RAID_DEBUG1(1, sc, 514 "Disk too small (%ju < %ju)", 515 pd->pd_disk_size, 516 sd->sd_offset + sd->sd_size + 512); 517 break; 518 } 519 } 520 if (sd != NULL) 521 continue; 522 if (tmpdisk->d_state == G_RAID_DISK_S_OFFLINE) { 523 olddisk = tmpdisk; 524 break; 525 } else if (olddisk == NULL) 526 olddisk = tmpdisk; 527 } 528 if (olddisk == NULL) { 529 nofit: 530 if (disk_pos == -3 || pd->pd_disk_pos == -3) { 531 g_raid_change_disk_state(disk, 532 G_RAID_DISK_S_SPARE); 533 return (1); 534 } else { 535 g_raid_change_disk_state(disk, 536 G_RAID_DISK_S_STALE); 537 return (0); 538 } 539 } 540 oldpd = (struct g_raid_md_sii_perdisk *)olddisk->d_md_data; 541 disk_pos = oldpd->pd_disk_pos; 542 resurrection = 1; 543 } 544 545 if (olddisk == NULL) { 546 /* Find placeholder by position. */ 547 olddisk = g_raid_md_sii_get_disk(sc, disk_pos); 548 if (olddisk == NULL) 549 panic("No disk at position %d!", disk_pos); 550 if (olddisk->d_state != G_RAID_DISK_S_OFFLINE) { 551 G_RAID_DEBUG1(1, sc, "More then one disk for pos %d", 552 disk_pos); 553 g_raid_change_disk_state(disk, G_RAID_DISK_S_STALE); 554 return (0); 555 } 556 oldpd = (struct g_raid_md_sii_perdisk *)olddisk->d_md_data; 557 } 558 559 /* Replace failed disk or placeholder with new disk. */ 560 TAILQ_FOREACH_SAFE(sd, &olddisk->d_subdisks, sd_next, tmpsd) { 561 TAILQ_REMOVE(&olddisk->d_subdisks, sd, sd_next); 562 TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next); 563 sd->sd_disk = disk; 564 } 565 oldpd->pd_disk_pos = -2; 566 pd->pd_disk_pos = disk_pos; 567 568 /* If it was placeholder -- destroy it. */ 569 if (olddisk->d_state == G_RAID_DISK_S_OFFLINE) { 570 g_raid_destroy_disk(olddisk); 571 } else { 572 /* Otherwise, make it STALE_FAILED. */ 573 g_raid_change_disk_state(olddisk, G_RAID_DISK_S_STALE_FAILED); 574 } 575 576 /* Welcome the new disk. */ 577 if (resurrection) 578 g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE); 579 else if (pd->pd_meta->disk_status == SII_S_CURRENT || 580 pd->pd_meta->disk_status == SII_S_REBUILD) 581 g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE); 582 else 583 g_raid_change_disk_state(disk, G_RAID_DISK_S_FAILED); 584 TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) { 585 586 /* 587 * Different disks may have different sizes, 588 * in concat mode. Update from real disk size. 589 */ 590 if (meta->type == SII_T_CONCAT || meta->type == SII_T_JBOD) 591 sd->sd_size = pd->pd_disk_size - 0x800 * 512; 592 593 if (resurrection) { 594 /* New or ex-spare disk. */ 595 g_raid_change_subdisk_state(sd, 596 G_RAID_SUBDISK_S_NEW); 597 } else if (pd->pd_meta->disk_status == SII_S_REBUILD) { 598 /* Rebuilding disk. */ 599 g_raid_change_subdisk_state(sd, 600 G_RAID_SUBDISK_S_REBUILD); 601 if (pd->pd_meta->generation == meta->generation) 602 sd->sd_rebuild_pos = pd->pd_meta->rebuild_lba * 512; 603 else 604 sd->sd_rebuild_pos = 0; 605 } else if (pd->pd_meta->disk_status == SII_S_CURRENT) { 606 if (pd->pd_meta->raid_status == SII_S_ONLINE || 607 pd->pd_meta->generation != meta->generation) { 608 /* Dirty or resyncing disk. */ 609 g_raid_change_subdisk_state(sd, 610 G_RAID_SUBDISK_S_STALE); 611 } else { 612 /* Up to date disk. */ 613 g_raid_change_subdisk_state(sd, 614 G_RAID_SUBDISK_S_ACTIVE); 615 } 616 } else { 617 g_raid_change_subdisk_state(sd, 618 G_RAID_SUBDISK_S_FAILED); 619 } 620 g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW, 621 G_RAID_EVENT_SUBDISK); 622 } 623 624 /* Update status of our need for spare. */ 625 if (mdi->mdio_started) { 626 mdi->mdio_incomplete = 627 (g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE) < 628 mdi->mdio_total_disks); 629 } 630 631 return (resurrection); 632 } 633 634 static void 635 g_disk_md_sii_retaste(void *arg, int pending) 636 { 637 638 G_RAID_DEBUG(1, "Array is not complete, trying to retaste."); 639 g_retaste(&g_raid_class); 640 free(arg, M_MD_SII); 641 } 642 643 static void 644 g_raid_md_sii_refill(struct g_raid_softc *sc) 645 { 646 struct g_raid_md_object *md; 647 struct g_raid_md_sii_object *mdi; 648 struct g_raid_disk *disk; 649 struct task *task; 650 int update, na; 651 652 md = sc->sc_md; 653 mdi = (struct g_raid_md_sii_object *)md; 654 update = 0; 655 do { 656 /* Make sure we miss anything. */ 657 na = g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE); 658 if (na == mdi->mdio_total_disks) 659 break; 660 661 G_RAID_DEBUG1(1, md->mdo_softc, 662 "Array is not complete (%d of %d), " 663 "trying to refill.", na, mdi->mdio_total_disks); 664 665 /* Try to get use some of STALE disks. */ 666 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 667 if (disk->d_state == G_RAID_DISK_S_STALE) { 668 update += g_raid_md_sii_start_disk(disk); 669 if (disk->d_state == G_RAID_DISK_S_ACTIVE) 670 break; 671 } 672 } 673 if (disk != NULL) 674 continue; 675 676 /* Try to get use some of SPARE disks. */ 677 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 678 if (disk->d_state == G_RAID_DISK_S_SPARE) { 679 update += g_raid_md_sii_start_disk(disk); 680 if (disk->d_state == G_RAID_DISK_S_ACTIVE) 681 break; 682 } 683 } 684 } while (disk != NULL); 685 686 /* Write new metadata if we changed something. */ 687 if (update) 688 g_raid_md_write_sii(md, NULL, NULL, NULL); 689 690 /* Update status of our need for spare. */ 691 mdi->mdio_incomplete = (g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE) < 692 mdi->mdio_total_disks); 693 694 /* Request retaste hoping to find spare. */ 695 if (mdi->mdio_incomplete) { 696 task = malloc(sizeof(struct task), 697 M_MD_SII, M_WAITOK | M_ZERO); 698 TASK_INIT(task, 0, g_disk_md_sii_retaste, task); 699 taskqueue_enqueue(taskqueue_swi, task); 700 } 701 } 702 703 static void 704 g_raid_md_sii_start(struct g_raid_softc *sc) 705 { 706 struct g_raid_md_object *md; 707 struct g_raid_md_sii_object *mdi; 708 struct g_raid_md_sii_perdisk *pd; 709 struct sii_raid_conf *meta; 710 struct g_raid_volume *vol; 711 struct g_raid_subdisk *sd; 712 struct g_raid_disk *disk, *best; 713 off_t size; 714 int j, disk_pos; 715 uint32_t gendiff, bestgendiff; 716 char buf[17]; 717 718 md = sc->sc_md; 719 mdi = (struct g_raid_md_sii_object *)md; 720 meta = mdi->mdio_meta; 721 722 /* Create volumes and subdisks. */ 723 sii_meta_get_name(meta, buf); 724 vol = g_raid_create_volume(sc, buf, -1); 725 vol->v_mediasize = (off_t)meta->total_sectors * 512; 726 vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_NONE; 727 if (meta->type == SII_T_RAID0) { 728 vol->v_raid_level = G_RAID_VOLUME_RL_RAID0; 729 size = vol->v_mediasize / mdi->mdio_total_disks; 730 } else if (meta->type == SII_T_RAID1) { 731 vol->v_raid_level = G_RAID_VOLUME_RL_RAID1; 732 size = vol->v_mediasize; 733 } else if (meta->type == SII_T_RAID01) { 734 vol->v_raid_level = G_RAID_VOLUME_RL_RAID1E; 735 size = vol->v_mediasize / (mdi->mdio_total_disks / 2); 736 } else if (meta->type == SII_T_CONCAT) { 737 if (mdi->mdio_total_disks == 1) 738 vol->v_raid_level = G_RAID_VOLUME_RL_SINGLE; 739 else 740 vol->v_raid_level = G_RAID_VOLUME_RL_CONCAT; 741 size = 0; 742 } else if (meta->type == SII_T_RAID5) { 743 vol->v_raid_level = G_RAID_VOLUME_RL_RAID5; 744 vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_R5LS; 745 size = vol->v_mediasize / (mdi->mdio_total_disks - 1); 746 } else if (meta->type == SII_T_JBOD) { 747 vol->v_raid_level = G_RAID_VOLUME_RL_SINGLE; 748 size = 0; 749 } else { 750 vol->v_raid_level = G_RAID_VOLUME_RL_UNKNOWN; 751 size = 0; 752 } 753 vol->v_strip_size = meta->strip_sectors * 512; //ZZZ 754 vol->v_disks_count = mdi->mdio_total_disks; 755 vol->v_sectorsize = 512; //ZZZ 756 for (j = 0; j < vol->v_disks_count; j++) { 757 sd = &vol->v_subdisks[j]; 758 sd->sd_offset = 0; 759 sd->sd_size = size; 760 } 761 g_raid_start_volume(vol); 762 763 /* Create disk placeholders to store data for later writing. */ 764 for (disk_pos = 0; disk_pos < mdi->mdio_total_disks; disk_pos++) { 765 pd = malloc(sizeof(*pd), M_MD_SII, M_WAITOK | M_ZERO); 766 pd->pd_disk_pos = disk_pos; 767 disk = g_raid_create_disk(sc); 768 disk->d_md_data = (void *)pd; 769 disk->d_state = G_RAID_DISK_S_OFFLINE; 770 sd = &vol->v_subdisks[disk_pos]; 771 sd->sd_disk = disk; 772 TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next); 773 } 774 775 /* 776 * Make all disks found till the moment take their places 777 * in order of their generation numbers. 778 */ 779 do { 780 best = NULL; 781 bestgendiff = 0xffffffff; 782 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 783 if (disk->d_state != G_RAID_DISK_S_NONE) 784 continue; 785 pd = disk->d_md_data; 786 if (pd->pd_meta == NULL) 787 gendiff = 0xfffffffe; 788 else 789 gendiff = meta->generation - 790 pd->pd_meta->generation; 791 if (gendiff < bestgendiff) { 792 best = disk; 793 bestgendiff = gendiff; 794 } 795 } 796 if (best != NULL) 797 g_raid_md_sii_start_disk(best); 798 } while (best != NULL); 799 800 mdi->mdio_started = 1; 801 G_RAID_DEBUG1(0, sc, "Array started."); 802 g_raid_md_write_sii(md, NULL, NULL, NULL); 803 804 /* Pickup any STALE/SPARE disks to refill array if needed. */ 805 g_raid_md_sii_refill(sc); 806 807 g_raid_event_send(vol, G_RAID_VOLUME_E_START, G_RAID_EVENT_VOLUME); 808 809 callout_stop(&mdi->mdio_start_co); 810 G_RAID_DEBUG1(1, sc, "root_mount_rel %p", mdi->mdio_rootmount); 811 root_mount_rel(mdi->mdio_rootmount); 812 mdi->mdio_rootmount = NULL; 813 } 814 815 static void 816 g_raid_md_sii_new_disk(struct g_raid_disk *disk) 817 { 818 struct g_raid_softc *sc; 819 struct g_raid_md_object *md; 820 struct g_raid_md_sii_object *mdi; 821 struct sii_raid_conf *pdmeta; 822 struct g_raid_md_sii_perdisk *pd; 823 824 sc = disk->d_softc; 825 md = sc->sc_md; 826 mdi = (struct g_raid_md_sii_object *)md; 827 pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data; 828 pdmeta = pd->pd_meta; 829 830 if (mdi->mdio_started) { 831 if (g_raid_md_sii_start_disk(disk)) 832 g_raid_md_write_sii(md, NULL, NULL, NULL); 833 } else { 834 if (mdi->mdio_meta == NULL || 835 ((int32_t)(pdmeta->generation - mdi->mdio_generation)) > 0) { 836 G_RAID_DEBUG1(1, sc, "Newer disk"); 837 if (mdi->mdio_meta != NULL) 838 free(mdi->mdio_meta, M_MD_SII); 839 mdi->mdio_meta = sii_meta_copy(pdmeta); 840 mdi->mdio_generation = mdi->mdio_meta->generation; 841 mdi->mdio_total_disks = sii_meta_total_disks(pdmeta); 842 mdi->mdio_disks_present = 1; 843 } else if (pdmeta->generation == mdi->mdio_generation) { 844 mdi->mdio_disks_present++; 845 G_RAID_DEBUG1(1, sc, "Matching disk (%d of %d up)", 846 mdi->mdio_disks_present, 847 mdi->mdio_total_disks); 848 } else { 849 G_RAID_DEBUG1(1, sc, "Older disk"); 850 } 851 852 /* If we collected all needed disks - start array. */ 853 if (mdi->mdio_disks_present == mdi->mdio_total_disks) 854 g_raid_md_sii_start(sc); 855 } 856 } 857 858 static void 859 g_raid_sii_go(void *arg) 860 { 861 struct g_raid_softc *sc; 862 struct g_raid_md_object *md; 863 struct g_raid_md_sii_object *mdi; 864 865 sc = arg; 866 md = sc->sc_md; 867 mdi = (struct g_raid_md_sii_object *)md; 868 if (!mdi->mdio_started) { 869 G_RAID_DEBUG1(0, sc, "Force array start due to timeout."); 870 g_raid_event_send(sc, G_RAID_NODE_E_START, 0); 871 } 872 } 873 874 static int 875 g_raid_md_create_sii(struct g_raid_md_object *md, struct g_class *mp, 876 struct g_geom **gp) 877 { 878 struct g_raid_softc *sc; 879 struct g_raid_md_sii_object *mdi; 880 char name[32]; 881 882 mdi = (struct g_raid_md_sii_object *)md; 883 mdi->mdio_timestamp[5] = arc4random(); 884 mdi->mdio_timestamp[4] = arc4random(); 885 mdi->mdio_timestamp[3] = arc4random(); 886 mdi->mdio_timestamp[2] = arc4random(); 887 mdi->mdio_timestamp[1] = arc4random(); 888 mdi->mdio_timestamp[0] = arc4random(); 889 mdi->mdio_location = arc4random(); 890 mdi->mdio_generation = 0; 891 snprintf(name, sizeof(name), "SiI-%02x%02x%02x%02x%02x%02x", 892 mdi->mdio_timestamp[5], mdi->mdio_timestamp[4], 893 mdi->mdio_timestamp[3], mdi->mdio_timestamp[2], 894 mdi->mdio_timestamp[1], mdi->mdio_timestamp[0]); 895 sc = g_raid_create_node(mp, name, md); 896 if (sc == NULL) 897 return (G_RAID_MD_TASTE_FAIL); 898 md->mdo_softc = sc; 899 *gp = sc->sc_geom; 900 return (G_RAID_MD_TASTE_NEW); 901 } 902 903 static int 904 g_raid_md_taste_sii(struct g_raid_md_object *md, struct g_class *mp, 905 struct g_consumer *cp, struct g_geom **gp) 906 { 907 struct g_consumer *rcp; 908 struct g_provider *pp; 909 struct g_raid_md_sii_object *mdi, *mdi1; 910 struct g_raid_softc *sc; 911 struct g_raid_disk *disk; 912 struct sii_raid_conf *meta; 913 struct g_raid_md_sii_perdisk *pd; 914 struct g_geom *geom; 915 int error, disk_pos, result, spare, len; 916 char name[32]; 917 uint16_t vendor; 918 919 G_RAID_DEBUG(1, "Tasting SiI on %s", cp->provider->name); 920 mdi = (struct g_raid_md_sii_object *)md; 921 pp = cp->provider; 922 923 /* Read metadata from device. */ 924 meta = NULL; 925 vendor = 0xffff; 926 if (g_access(cp, 1, 0, 0) != 0) 927 return (G_RAID_MD_TASTE_FAIL); 928 g_topology_unlock(); 929 len = 2; 930 if (pp->geom->rank == 1) 931 g_io_getattr("GEOM::hba_vendor", cp, &len, &vendor); 932 meta = sii_meta_read(cp); 933 g_topology_lock(); 934 g_access(cp, -1, 0, 0); 935 if (meta == NULL) { 936 if (g_raid_aggressive_spare) { 937 if (vendor == 0x1095) { 938 G_RAID_DEBUG(1, 939 "No SiI metadata, forcing spare."); 940 spare = 2; 941 goto search; 942 } else { 943 G_RAID_DEBUG(1, 944 "SiI vendor mismatch 0x%04x != 0x1095", 945 vendor); 946 } 947 } 948 return (G_RAID_MD_TASTE_FAIL); 949 } 950 951 /* Check this disk position in obtained metadata. */ 952 disk_pos = sii_meta_disk_pos(meta, meta); 953 if (disk_pos == -1) { 954 G_RAID_DEBUG(1, "SiI disk position not found"); 955 goto fail1; 956 } 957 958 /* Metadata valid. Print it. */ 959 g_raid_md_sii_print(meta); 960 G_RAID_DEBUG(1, "SiI disk position %d", disk_pos); 961 spare = (meta->type == SII_T_SPARE) ? 1 : 0; 962 963 search: 964 /* Search for matching node. */ 965 sc = NULL; 966 mdi1 = NULL; 967 LIST_FOREACH(geom, &mp->geom, geom) { 968 sc = geom->softc; 969 if (sc == NULL) 970 continue; 971 if (sc->sc_stopping != 0) 972 continue; 973 if (sc->sc_md->mdo_class != md->mdo_class) 974 continue; 975 mdi1 = (struct g_raid_md_sii_object *)sc->sc_md; 976 if (spare) { 977 if (mdi1->mdio_incomplete) 978 break; 979 } else { 980 if (mdi1->mdio_location == meta->raid_location && 981 memcmp(&mdi1->mdio_timestamp, 982 &meta->timestamp, 6) == 0) 983 break; 984 } 985 } 986 987 /* Found matching node. */ 988 if (geom != NULL) { 989 G_RAID_DEBUG(1, "Found matching array %s", sc->sc_name); 990 result = G_RAID_MD_TASTE_EXISTING; 991 992 } else if (spare) { /* Not found needy node -- left for later. */ 993 G_RAID_DEBUG(1, "Spare is not needed at this time"); 994 goto fail1; 995 996 } else { /* Not found matching node -- create one. */ 997 result = G_RAID_MD_TASTE_NEW; 998 memcpy(&mdi->mdio_timestamp, &meta->timestamp, 6); 999 mdi->mdio_location = meta->raid_location; 1000 snprintf(name, sizeof(name), "SiI-%02x%02x%02x%02x%02x%02x", 1001 mdi->mdio_timestamp[5], mdi->mdio_timestamp[4], 1002 mdi->mdio_timestamp[3], mdi->mdio_timestamp[2], 1003 mdi->mdio_timestamp[1], mdi->mdio_timestamp[0]); 1004 sc = g_raid_create_node(mp, name, md); 1005 md->mdo_softc = sc; 1006 geom = sc->sc_geom; 1007 callout_init(&mdi->mdio_start_co, 1); 1008 callout_reset(&mdi->mdio_start_co, g_raid_start_timeout * hz, 1009 g_raid_sii_go, sc); 1010 mdi->mdio_rootmount = root_mount_hold("GRAID-SiI"); 1011 G_RAID_DEBUG1(1, sc, "root_mount_hold %p", mdi->mdio_rootmount); 1012 } 1013 1014 rcp = g_new_consumer(geom); 1015 g_attach(rcp, pp); 1016 if (g_access(rcp, 1, 1, 1) != 0) 1017 ; //goto fail1; 1018 1019 g_topology_unlock(); 1020 sx_xlock(&sc->sc_lock); 1021 1022 pd = malloc(sizeof(*pd), M_MD_SII, M_WAITOK | M_ZERO); 1023 pd->pd_meta = meta; 1024 if (spare == 2) { 1025 pd->pd_disk_pos = -3; 1026 } else { 1027 pd->pd_disk_pos = -1; 1028 } 1029 pd->pd_disk_size = pp->mediasize; 1030 disk = g_raid_create_disk(sc); 1031 disk->d_md_data = (void *)pd; 1032 disk->d_consumer = rcp; 1033 rcp->private = disk; 1034 1035 /* Read kernel dumping information. */ 1036 disk->d_kd.offset = 0; 1037 disk->d_kd.length = OFF_MAX; 1038 len = sizeof(disk->d_kd); 1039 error = g_io_getattr("GEOM::kerneldump", rcp, &len, &disk->d_kd); 1040 if (disk->d_kd.di.dumper == NULL) 1041 G_RAID_DEBUG1(2, sc, "Dumping not supported by %s: %d.", 1042 rcp->provider->name, error); 1043 1044 g_raid_md_sii_new_disk(disk); 1045 1046 sx_xunlock(&sc->sc_lock); 1047 g_topology_lock(); 1048 *gp = geom; 1049 return (result); 1050 fail1: 1051 free(meta, M_MD_SII); 1052 return (G_RAID_MD_TASTE_FAIL); 1053 } 1054 1055 static int 1056 g_raid_md_event_sii(struct g_raid_md_object *md, 1057 struct g_raid_disk *disk, u_int event) 1058 { 1059 struct g_raid_softc *sc; 1060 struct g_raid_subdisk *sd; 1061 struct g_raid_md_sii_object *mdi; 1062 struct g_raid_md_sii_perdisk *pd; 1063 1064 sc = md->mdo_softc; 1065 mdi = (struct g_raid_md_sii_object *)md; 1066 if (disk == NULL) { 1067 switch (event) { 1068 case G_RAID_NODE_E_START: 1069 if (!mdi->mdio_started) 1070 g_raid_md_sii_start(sc); 1071 return (0); 1072 } 1073 return (-1); 1074 } 1075 pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data; 1076 switch (event) { 1077 case G_RAID_DISK_E_DISCONNECTED: 1078 /* If disk was assigned, just update statuses. */ 1079 if (pd->pd_disk_pos >= 0) { 1080 g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE); 1081 if (disk->d_consumer) { 1082 g_raid_kill_consumer(sc, disk->d_consumer); 1083 disk->d_consumer = NULL; 1084 } 1085 TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) { 1086 g_raid_change_subdisk_state(sd, 1087 G_RAID_SUBDISK_S_NONE); 1088 g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED, 1089 G_RAID_EVENT_SUBDISK); 1090 } 1091 } else { 1092 /* Otherwise -- delete. */ 1093 g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE); 1094 g_raid_destroy_disk(disk); 1095 } 1096 1097 /* Write updated metadata to all disks. */ 1098 g_raid_md_write_sii(md, NULL, NULL, NULL); 1099 1100 /* Check if anything left except placeholders. */ 1101 if (g_raid_ndisks(sc, -1) == 1102 g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE)) 1103 g_raid_destroy_node(sc, 0); 1104 else 1105 g_raid_md_sii_refill(sc); 1106 return (0); 1107 } 1108 return (-2); 1109 } 1110 1111 static int 1112 g_raid_md_ctl_sii(struct g_raid_md_object *md, 1113 struct gctl_req *req) 1114 { 1115 struct g_raid_softc *sc; 1116 struct g_raid_volume *vol; 1117 struct g_raid_subdisk *sd; 1118 struct g_raid_disk *disk; 1119 struct g_raid_md_sii_object *mdi; 1120 struct g_raid_md_sii_perdisk *pd; 1121 struct g_consumer *cp; 1122 struct g_provider *pp; 1123 char arg[16]; 1124 const char *verb, *volname, *levelname, *diskname; 1125 int *nargs, *force; 1126 off_t size, sectorsize, strip; 1127 intmax_t *sizearg, *striparg; 1128 int numdisks, i, len, level, qual, update; 1129 int error; 1130 1131 sc = md->mdo_softc; 1132 mdi = (struct g_raid_md_sii_object *)md; 1133 verb = gctl_get_param(req, "verb", NULL); 1134 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 1135 error = 0; 1136 if (strcmp(verb, "label") == 0) { 1137 1138 if (*nargs < 4) { 1139 gctl_error(req, "Invalid number of arguments."); 1140 return (-1); 1141 } 1142 volname = gctl_get_asciiparam(req, "arg1"); 1143 if (volname == NULL) { 1144 gctl_error(req, "No volume name."); 1145 return (-2); 1146 } 1147 levelname = gctl_get_asciiparam(req, "arg2"); 1148 if (levelname == NULL) { 1149 gctl_error(req, "No RAID level."); 1150 return (-3); 1151 } 1152 if (strcasecmp(levelname, "RAID5") == 0) 1153 levelname = "RAID5-LS"; 1154 if (g_raid_volume_str2level(levelname, &level, &qual)) { 1155 gctl_error(req, "Unknown RAID level '%s'.", levelname); 1156 return (-4); 1157 } 1158 numdisks = *nargs - 3; 1159 force = gctl_get_paraml(req, "force", sizeof(*force)); 1160 if (!g_raid_md_sii_supported(level, qual, numdisks, 1161 force ? *force : 0)) { 1162 gctl_error(req, "Unsupported RAID level " 1163 "(0x%02x/0x%02x), or number of disks (%d).", 1164 level, qual, numdisks); 1165 return (-5); 1166 } 1167 1168 /* Search for disks, connect them and probe. */ 1169 size = 0x7fffffffffffffffllu; 1170 sectorsize = 0; 1171 for (i = 0; i < numdisks; i++) { 1172 snprintf(arg, sizeof(arg), "arg%d", i + 3); 1173 diskname = gctl_get_asciiparam(req, arg); 1174 if (diskname == NULL) { 1175 gctl_error(req, "No disk name (%s).", arg); 1176 error = -6; 1177 break; 1178 } 1179 if (strcmp(diskname, "NONE") == 0) { 1180 cp = NULL; 1181 pp = NULL; 1182 } else { 1183 g_topology_lock(); 1184 cp = g_raid_open_consumer(sc, diskname); 1185 if (cp == NULL) { 1186 gctl_error(req, "Can't open '%s'.", 1187 diskname); 1188 g_topology_unlock(); 1189 error = -7; 1190 break; 1191 } 1192 pp = cp->provider; 1193 } 1194 pd = malloc(sizeof(*pd), M_MD_SII, M_WAITOK | M_ZERO); 1195 pd->pd_disk_pos = i; 1196 disk = g_raid_create_disk(sc); 1197 disk->d_md_data = (void *)pd; 1198 disk->d_consumer = cp; 1199 if (cp == NULL) 1200 continue; 1201 cp->private = disk; 1202 g_topology_unlock(); 1203 1204 /* Read kernel dumping information. */ 1205 disk->d_kd.offset = 0; 1206 disk->d_kd.length = OFF_MAX; 1207 len = sizeof(disk->d_kd); 1208 g_io_getattr("GEOM::kerneldump", cp, &len, &disk->d_kd); 1209 if (disk->d_kd.di.dumper == NULL) 1210 G_RAID_DEBUG1(2, sc, 1211 "Dumping not supported by %s.", 1212 cp->provider->name); 1213 1214 pd->pd_disk_size = pp->mediasize; 1215 if (size > pp->mediasize) 1216 size = pp->mediasize; 1217 if (sectorsize < pp->sectorsize) 1218 sectorsize = pp->sectorsize; 1219 } 1220 if (error != 0) 1221 return (error); 1222 1223 if (sectorsize <= 0) { 1224 gctl_error(req, "Can't get sector size."); 1225 return (-8); 1226 } 1227 1228 /* Reserve space for metadata. */ 1229 size -= 0x800 * sectorsize; 1230 1231 /* Handle size argument. */ 1232 len = sizeof(*sizearg); 1233 sizearg = gctl_get_param(req, "size", &len); 1234 if (sizearg != NULL && len == sizeof(*sizearg) && 1235 *sizearg > 0) { 1236 if (*sizearg > size) { 1237 gctl_error(req, "Size too big %lld > %lld.", 1238 (long long)*sizearg, (long long)size); 1239 return (-9); 1240 } 1241 size = *sizearg; 1242 } 1243 1244 /* Handle strip argument. */ 1245 strip = 131072; 1246 len = sizeof(*striparg); 1247 striparg = gctl_get_param(req, "strip", &len); 1248 if (striparg != NULL && len == sizeof(*striparg) && 1249 *striparg > 0) { 1250 if (*striparg < sectorsize) { 1251 gctl_error(req, "Strip size too small."); 1252 return (-10); 1253 } 1254 if (*striparg % sectorsize != 0) { 1255 gctl_error(req, "Incorrect strip size."); 1256 return (-11); 1257 } 1258 if (strip > 65535 * sectorsize) { 1259 gctl_error(req, "Strip size too big."); 1260 return (-12); 1261 } 1262 strip = *striparg; 1263 } 1264 1265 /* Round size down to strip or sector. */ 1266 if (level == G_RAID_VOLUME_RL_RAID1) 1267 size -= (size % sectorsize); 1268 else if (level == G_RAID_VOLUME_RL_RAID1E && 1269 (numdisks & 1) != 0) 1270 size -= (size % (2 * strip)); 1271 else 1272 size -= (size % strip); 1273 if (size <= 0) { 1274 gctl_error(req, "Size too small."); 1275 return (-13); 1276 } 1277 if (size > 0xffffffffffffllu * sectorsize) { 1278 gctl_error(req, "Size too big."); 1279 return (-14); 1280 } 1281 1282 /* We have all we need, create things: volume, ... */ 1283 mdi->mdio_total_disks = numdisks; 1284 mdi->mdio_started = 1; 1285 vol = g_raid_create_volume(sc, volname, -1); 1286 vol->v_md_data = (void *)(intptr_t)0; 1287 vol->v_raid_level = level; 1288 vol->v_raid_level_qualifier = qual; 1289 vol->v_strip_size = strip; 1290 vol->v_disks_count = numdisks; 1291 if (level == G_RAID_VOLUME_RL_RAID0 || 1292 level == G_RAID_VOLUME_RL_CONCAT || 1293 level == G_RAID_VOLUME_RL_SINGLE) 1294 vol->v_mediasize = size * numdisks; 1295 else if (level == G_RAID_VOLUME_RL_RAID1) 1296 vol->v_mediasize = size; 1297 else if (level == G_RAID_VOLUME_RL_RAID5) 1298 vol->v_mediasize = size * (numdisks - 1); 1299 else { /* RAID1E */ 1300 vol->v_mediasize = ((size * numdisks) / strip / 2) * 1301 strip; 1302 } 1303 vol->v_sectorsize = sectorsize; 1304 g_raid_start_volume(vol); 1305 1306 /* , and subdisks. */ 1307 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 1308 pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data; 1309 sd = &vol->v_subdisks[pd->pd_disk_pos]; 1310 sd->sd_disk = disk; 1311 sd->sd_offset = 0; 1312 sd->sd_size = size; 1313 TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next); 1314 if (sd->sd_disk->d_consumer != NULL) { 1315 g_raid_change_disk_state(disk, 1316 G_RAID_DISK_S_ACTIVE); 1317 g_raid_change_subdisk_state(sd, 1318 G_RAID_SUBDISK_S_ACTIVE); 1319 g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW, 1320 G_RAID_EVENT_SUBDISK); 1321 } else { 1322 g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE); 1323 } 1324 } 1325 1326 /* Write metadata based on created entities. */ 1327 G_RAID_DEBUG1(0, sc, "Array started."); 1328 g_raid_md_write_sii(md, NULL, NULL, NULL); 1329 1330 /* Pickup any STALE/SPARE disks to refill array if needed. */ 1331 g_raid_md_sii_refill(sc); 1332 1333 g_raid_event_send(vol, G_RAID_VOLUME_E_START, 1334 G_RAID_EVENT_VOLUME); 1335 return (0); 1336 } 1337 if (strcmp(verb, "delete") == 0) { 1338 1339 /* Check if some volume is still open. */ 1340 force = gctl_get_paraml(req, "force", sizeof(*force)); 1341 if (force != NULL && *force == 0 && 1342 g_raid_nopens(sc) != 0) { 1343 gctl_error(req, "Some volume is still open."); 1344 return (-4); 1345 } 1346 1347 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 1348 if (disk->d_consumer) 1349 sii_meta_erase(disk->d_consumer); 1350 } 1351 g_raid_destroy_node(sc, 0); 1352 return (0); 1353 } 1354 if (strcmp(verb, "remove") == 0 || 1355 strcmp(verb, "fail") == 0) { 1356 if (*nargs < 2) { 1357 gctl_error(req, "Invalid number of arguments."); 1358 return (-1); 1359 } 1360 for (i = 1; i < *nargs; i++) { 1361 snprintf(arg, sizeof(arg), "arg%d", i); 1362 diskname = gctl_get_asciiparam(req, arg); 1363 if (diskname == NULL) { 1364 gctl_error(req, "No disk name (%s).", arg); 1365 error = -2; 1366 break; 1367 } 1368 if (strncmp(diskname, "/dev/", 5) == 0) 1369 diskname += 5; 1370 1371 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 1372 if (disk->d_consumer != NULL && 1373 disk->d_consumer->provider != NULL && 1374 strcmp(disk->d_consumer->provider->name, 1375 diskname) == 0) 1376 break; 1377 } 1378 if (disk == NULL) { 1379 gctl_error(req, "Disk '%s' not found.", 1380 diskname); 1381 error = -3; 1382 break; 1383 } 1384 1385 if (strcmp(verb, "fail") == 0) { 1386 g_raid_md_fail_disk_sii(md, NULL, disk); 1387 continue; 1388 } 1389 1390 pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data; 1391 1392 /* Erase metadata on deleting disk. */ 1393 sii_meta_erase(disk->d_consumer); 1394 1395 /* If disk was assigned, just update statuses. */ 1396 if (pd->pd_disk_pos >= 0) { 1397 g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE); 1398 g_raid_kill_consumer(sc, disk->d_consumer); 1399 disk->d_consumer = NULL; 1400 TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) { 1401 g_raid_change_subdisk_state(sd, 1402 G_RAID_SUBDISK_S_NONE); 1403 g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED, 1404 G_RAID_EVENT_SUBDISK); 1405 } 1406 } else { 1407 /* Otherwise -- delete. */ 1408 g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE); 1409 g_raid_destroy_disk(disk); 1410 } 1411 } 1412 1413 /* Write updated metadata to remaining disks. */ 1414 g_raid_md_write_sii(md, NULL, NULL, NULL); 1415 1416 /* Check if anything left except placeholders. */ 1417 if (g_raid_ndisks(sc, -1) == 1418 g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE)) 1419 g_raid_destroy_node(sc, 0); 1420 else 1421 g_raid_md_sii_refill(sc); 1422 return (error); 1423 } 1424 if (strcmp(verb, "insert") == 0) { 1425 if (*nargs < 2) { 1426 gctl_error(req, "Invalid number of arguments."); 1427 return (-1); 1428 } 1429 update = 0; 1430 for (i = 1; i < *nargs; i++) { 1431 /* Get disk name. */ 1432 snprintf(arg, sizeof(arg), "arg%d", i); 1433 diskname = gctl_get_asciiparam(req, arg); 1434 if (diskname == NULL) { 1435 gctl_error(req, "No disk name (%s).", arg); 1436 error = -3; 1437 break; 1438 } 1439 1440 /* Try to find provider with specified name. */ 1441 g_topology_lock(); 1442 cp = g_raid_open_consumer(sc, diskname); 1443 if (cp == NULL) { 1444 gctl_error(req, "Can't open disk '%s'.", 1445 diskname); 1446 g_topology_unlock(); 1447 error = -4; 1448 break; 1449 } 1450 pp = cp->provider; 1451 1452 pd = malloc(sizeof(*pd), M_MD_SII, M_WAITOK | M_ZERO); 1453 pd->pd_disk_pos = -3; 1454 pd->pd_disk_size = pp->mediasize; 1455 1456 disk = g_raid_create_disk(sc); 1457 disk->d_consumer = cp; 1458 disk->d_md_data = (void *)pd; 1459 cp->private = disk; 1460 g_topology_unlock(); 1461 1462 /* Read kernel dumping information. */ 1463 disk->d_kd.offset = 0; 1464 disk->d_kd.length = OFF_MAX; 1465 len = sizeof(disk->d_kd); 1466 g_io_getattr("GEOM::kerneldump", cp, &len, &disk->d_kd); 1467 if (disk->d_kd.di.dumper == NULL) 1468 G_RAID_DEBUG1(2, sc, 1469 "Dumping not supported by %s.", 1470 cp->provider->name); 1471 1472 /* Welcome the "new" disk. */ 1473 update += g_raid_md_sii_start_disk(disk); 1474 if (disk->d_state == G_RAID_DISK_S_SPARE) { 1475 sii_meta_write_spare(cp); 1476 g_raid_destroy_disk(disk); 1477 } else if (disk->d_state != G_RAID_DISK_S_ACTIVE) { 1478 gctl_error(req, "Disk '%s' doesn't fit.", 1479 diskname); 1480 g_raid_destroy_disk(disk); 1481 error = -8; 1482 break; 1483 } 1484 } 1485 1486 /* Write new metadata if we changed something. */ 1487 if (update) 1488 g_raid_md_write_sii(md, NULL, NULL, NULL); 1489 return (error); 1490 } 1491 gctl_error(req, "Command '%s' is not supported.", verb); 1492 return (-100); 1493 } 1494 1495 static int 1496 g_raid_md_write_sii(struct g_raid_md_object *md, struct g_raid_volume *tvol, 1497 struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk) 1498 { 1499 struct g_raid_softc *sc; 1500 struct g_raid_volume *vol; 1501 struct g_raid_subdisk *sd; 1502 struct g_raid_disk *disk; 1503 struct g_raid_md_sii_object *mdi; 1504 struct g_raid_md_sii_perdisk *pd; 1505 struct sii_raid_conf *meta; 1506 int i; 1507 1508 sc = md->mdo_softc; 1509 mdi = (struct g_raid_md_sii_object *)md; 1510 1511 if (sc->sc_stopping == G_RAID_DESTROY_HARD) 1512 return (0); 1513 1514 /* Bump generation. Newly written metadata may differ from previous. */ 1515 mdi->mdio_generation++; 1516 1517 /* There is only one volume. */ 1518 vol = TAILQ_FIRST(&sc->sc_volumes); 1519 1520 /* Fill global fields. */ 1521 meta = malloc(sizeof(*meta), M_MD_SII, M_WAITOK | M_ZERO); 1522 if (mdi->mdio_meta) 1523 memcpy(meta, mdi->mdio_meta, sizeof(*meta)); 1524 meta->total_sectors = vol->v_mediasize / vol->v_sectorsize; 1525 meta->vendor_id = 0x1095; 1526 meta->version_minor = 0; 1527 meta->version_major = 2; 1528 memcpy(&meta->timestamp, &mdi->mdio_timestamp, 6); 1529 meta->strip_sectors = vol->v_strip_size / vol->v_sectorsize; 1530 if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID0) { 1531 meta->type = SII_T_RAID0; 1532 meta->raid0_disks = vol->v_disks_count; 1533 meta->raid1_disks = 0xff; 1534 } else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1) { 1535 meta->type = SII_T_RAID1; 1536 meta->raid0_disks = 0xff; 1537 meta->raid1_disks = vol->v_disks_count; 1538 } else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E) { 1539 meta->type = SII_T_RAID01; 1540 meta->raid0_disks = vol->v_disks_count / 2; 1541 meta->raid1_disks = 2; 1542 } else if (vol->v_raid_level == G_RAID_VOLUME_RL_CONCAT || 1543 vol->v_raid_level == G_RAID_VOLUME_RL_SINGLE) { 1544 meta->type = SII_T_JBOD; 1545 meta->raid0_disks = vol->v_disks_count; 1546 meta->raid1_disks = 0xff; 1547 } else { 1548 meta->type = SII_T_RAID5; 1549 meta->raid0_disks = vol->v_disks_count; 1550 meta->raid1_disks = 0xff; 1551 } 1552 meta->generation = mdi->mdio_generation; 1553 meta->raid_status = vol->v_dirty ? SII_S_ONLINE : SII_S_AVAILABLE; 1554 for (i = 0; i < vol->v_disks_count; i++) { 1555 sd = &vol->v_subdisks[i]; 1556 if (sd->sd_state == G_RAID_SUBDISK_S_STALE || 1557 sd->sd_state == G_RAID_SUBDISK_S_RESYNC) 1558 meta->raid_status = SII_S_ONLINE; 1559 } 1560 meta->raid_location = mdi->mdio_location; 1561 sii_meta_put_name(meta, vol->v_name); 1562 1563 /* We are done. Print meta data and store them to disks. */ 1564 if (mdi->mdio_meta != NULL) 1565 free(mdi->mdio_meta, M_MD_SII); 1566 mdi->mdio_meta = meta; 1567 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 1568 pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data; 1569 if (disk->d_state != G_RAID_DISK_S_ACTIVE) 1570 continue; 1571 if (pd->pd_meta != NULL) { 1572 free(pd->pd_meta, M_MD_SII); 1573 pd->pd_meta = NULL; 1574 } 1575 pd->pd_meta = sii_meta_copy(meta); 1576 if ((sd = TAILQ_FIRST(&disk->d_subdisks)) != NULL) { 1577 if (sd->sd_state < G_RAID_SUBDISK_S_NEW) 1578 pd->pd_meta->disk_status = SII_S_DROPPED; 1579 else if (sd->sd_state < G_RAID_SUBDISK_S_STALE) { 1580 pd->pd_meta->disk_status = SII_S_REBUILD; 1581 pd->pd_meta->rebuild_lba = 1582 sd->sd_rebuild_pos / vol->v_sectorsize; 1583 } else 1584 pd->pd_meta->disk_status = SII_S_CURRENT; 1585 if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1) { 1586 pd->pd_meta->disk_number = sd->sd_pos; 1587 pd->pd_meta->raid0_ident = 0xff; 1588 pd->pd_meta->raid1_ident = 0; 1589 } else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E) { 1590 pd->pd_meta->disk_number = sd->sd_pos / meta->raid1_disks; 1591 pd->pd_meta->raid0_ident = sd->sd_pos % meta->raid1_disks; 1592 pd->pd_meta->raid1_ident = sd->sd_pos / meta->raid1_disks; 1593 } else { 1594 pd->pd_meta->disk_number = sd->sd_pos; 1595 pd->pd_meta->raid0_ident = 0; 1596 pd->pd_meta->raid1_ident = 0xff; 1597 } 1598 } 1599 G_RAID_DEBUG(1, "Writing SiI metadata to %s", 1600 g_raid_get_diskname(disk)); 1601 g_raid_md_sii_print(pd->pd_meta); 1602 sii_meta_write(disk->d_consumer, pd->pd_meta); 1603 } 1604 return (0); 1605 } 1606 1607 static int 1608 g_raid_md_fail_disk_sii(struct g_raid_md_object *md, 1609 struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk) 1610 { 1611 struct g_raid_softc *sc; 1612 struct g_raid_md_sii_perdisk *pd; 1613 struct g_raid_subdisk *sd; 1614 1615 sc = md->mdo_softc; 1616 pd = (struct g_raid_md_sii_perdisk *)tdisk->d_md_data; 1617 1618 /* We can't fail disk that is not a part of array now. */ 1619 if (pd->pd_disk_pos < 0) 1620 return (-1); 1621 1622 /* 1623 * Mark disk as failed in metadata and try to write that metadata 1624 * to the disk itself to prevent it's later resurrection as STALE. 1625 */ 1626 if (tdisk->d_consumer) { 1627 if (pd->pd_meta) { 1628 pd->pd_meta->disk_status = SII_S_REMOVED; 1629 sii_meta_write(tdisk->d_consumer, pd->pd_meta); 1630 } else 1631 sii_meta_erase(tdisk->d_consumer); 1632 } 1633 1634 /* Change states. */ 1635 g_raid_change_disk_state(tdisk, G_RAID_DISK_S_FAILED); 1636 TAILQ_FOREACH(sd, &tdisk->d_subdisks, sd_next) { 1637 g_raid_change_subdisk_state(sd, 1638 G_RAID_SUBDISK_S_FAILED); 1639 g_raid_event_send(sd, G_RAID_SUBDISK_E_FAILED, 1640 G_RAID_EVENT_SUBDISK); 1641 } 1642 1643 /* Write updated metadata to remaining disks. */ 1644 g_raid_md_write_sii(md, NULL, NULL, tdisk); 1645 1646 /* Check if anything left except placeholders. */ 1647 if (g_raid_ndisks(sc, -1) == 1648 g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE)) 1649 g_raid_destroy_node(sc, 0); 1650 else 1651 g_raid_md_sii_refill(sc); 1652 return (0); 1653 } 1654 1655 static int 1656 g_raid_md_free_disk_sii(struct g_raid_md_object *md, 1657 struct g_raid_disk *disk) 1658 { 1659 struct g_raid_md_sii_perdisk *pd; 1660 1661 pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data; 1662 if (pd->pd_meta != NULL) { 1663 free(pd->pd_meta, M_MD_SII); 1664 pd->pd_meta = NULL; 1665 } 1666 free(pd, M_MD_SII); 1667 disk->d_md_data = NULL; 1668 return (0); 1669 } 1670 1671 static int 1672 g_raid_md_free_sii(struct g_raid_md_object *md) 1673 { 1674 struct g_raid_md_sii_object *mdi; 1675 1676 mdi = (struct g_raid_md_sii_object *)md; 1677 if (!mdi->mdio_started) { 1678 mdi->mdio_started = 0; 1679 callout_stop(&mdi->mdio_start_co); 1680 G_RAID_DEBUG1(1, md->mdo_softc, 1681 "root_mount_rel %p", mdi->mdio_rootmount); 1682 root_mount_rel(mdi->mdio_rootmount); 1683 mdi->mdio_rootmount = NULL; 1684 } 1685 if (mdi->mdio_meta != NULL) { 1686 free(mdi->mdio_meta, M_MD_SII); 1687 mdi->mdio_meta = NULL; 1688 } 1689 return (0); 1690 } 1691 1692 G_RAID_MD_DECLARE(sii, "SiI"); 1693