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