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