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