1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2011 Alexander Motin <mav@FreeBSD.org> 5 * Copyright (c) 2000 - 2008 Søren Schmidt <sos@FreeBSD.org> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <sys/param.h> 34 #include <sys/bio.h> 35 #include <sys/endian.h> 36 #include <sys/kernel.h> 37 #include <sys/kobj.h> 38 #include <sys/limits.h> 39 #include <sys/lock.h> 40 #include <sys/malloc.h> 41 #include <sys/mutex.h> 42 #include <sys/systm.h> 43 #include <sys/taskqueue.h> 44 #include <geom/geom.h> 45 #include <geom/geom_dbg.h> 46 #include "geom/raid/g_raid.h" 47 #include "g_raid_md_if.h" 48 49 static MALLOC_DEFINE(M_MD_NVIDIA, "md_nvidia_data", "GEOM_RAID NVIDIA metadata"); 50 51 struct nvidia_raid_conf { 52 uint8_t nvidia_id[8]; 53 #define NVIDIA_MAGIC "NVIDIA " 54 55 uint32_t config_size; 56 uint32_t checksum; 57 uint16_t version; 58 uint8_t disk_number; 59 uint8_t dummy_0; 60 uint32_t total_sectors; 61 uint32_t sector_size; 62 uint8_t name[16]; 63 uint8_t revision[4]; 64 uint32_t disk_status; 65 66 uint32_t magic_0; 67 #define NVIDIA_MAGIC0 0x00640044 68 69 uint64_t volume_id[2]; 70 uint8_t state; 71 #define NVIDIA_S_IDLE 0 72 #define NVIDIA_S_INIT 2 73 #define NVIDIA_S_REBUILD 3 74 #define NVIDIA_S_UPGRADE 4 75 #define NVIDIA_S_SYNC 5 76 uint8_t array_width; 77 uint8_t total_disks; 78 uint8_t orig_array_width; 79 uint16_t type; 80 #define NVIDIA_T_RAID0 0x0080 81 #define NVIDIA_T_RAID1 0x0081 82 #define NVIDIA_T_RAID3 0x0083 83 #define NVIDIA_T_RAID5 0x0085 /* RLQ = 00/02? */ 84 #define NVIDIA_T_RAID5_SYM 0x0095 /* RLQ = 03 */ 85 #define NVIDIA_T_RAID10 0x008a 86 #define NVIDIA_T_RAID01 0x8180 87 #define NVIDIA_T_CONCAT 0x00ff 88 89 uint16_t dummy_3; 90 uint32_t strip_sectors; 91 uint32_t strip_bytes; 92 uint32_t strip_shift; 93 uint32_t strip_mask; 94 uint32_t stripe_sectors; 95 uint32_t stripe_bytes; 96 uint32_t rebuild_lba; 97 uint32_t orig_type; 98 uint32_t orig_total_sectors; 99 uint32_t status; 100 #define NVIDIA_S_BOOTABLE 0x00000001 101 #define NVIDIA_S_DEGRADED 0x00000002 102 103 uint32_t filler[98]; 104 } __packed; 105 106 struct g_raid_md_nvidia_perdisk { 107 struct nvidia_raid_conf *pd_meta; 108 int pd_disk_pos; 109 off_t pd_disk_size; 110 }; 111 112 struct g_raid_md_nvidia_object { 113 struct g_raid_md_object mdio_base; 114 uint64_t mdio_volume_id[2]; 115 struct nvidia_raid_conf *mdio_meta; 116 struct callout mdio_start_co; /* STARTING state timer. */ 117 int mdio_total_disks; 118 int mdio_disks_present; 119 int mdio_started; 120 int mdio_incomplete; 121 struct root_hold_token *mdio_rootmount; /* Root mount delay token. */ 122 }; 123 124 static g_raid_md_create_t g_raid_md_create_nvidia; 125 static g_raid_md_taste_t g_raid_md_taste_nvidia; 126 static g_raid_md_event_t g_raid_md_event_nvidia; 127 static g_raid_md_ctl_t g_raid_md_ctl_nvidia; 128 static g_raid_md_write_t g_raid_md_write_nvidia; 129 static g_raid_md_fail_disk_t g_raid_md_fail_disk_nvidia; 130 static g_raid_md_free_disk_t g_raid_md_free_disk_nvidia; 131 static g_raid_md_free_t g_raid_md_free_nvidia; 132 133 static kobj_method_t g_raid_md_nvidia_methods[] = { 134 KOBJMETHOD(g_raid_md_create, g_raid_md_create_nvidia), 135 KOBJMETHOD(g_raid_md_taste, g_raid_md_taste_nvidia), 136 KOBJMETHOD(g_raid_md_event, g_raid_md_event_nvidia), 137 KOBJMETHOD(g_raid_md_ctl, g_raid_md_ctl_nvidia), 138 KOBJMETHOD(g_raid_md_write, g_raid_md_write_nvidia), 139 KOBJMETHOD(g_raid_md_fail_disk, g_raid_md_fail_disk_nvidia), 140 KOBJMETHOD(g_raid_md_free_disk, g_raid_md_free_disk_nvidia), 141 KOBJMETHOD(g_raid_md_free, g_raid_md_free_nvidia), 142 { 0, 0 } 143 }; 144 145 static struct g_raid_md_class g_raid_md_nvidia_class = { 146 "NVIDIA", 147 g_raid_md_nvidia_methods, 148 sizeof(struct g_raid_md_nvidia_object), 149 .mdc_enable = 1, 150 .mdc_priority = 100 151 }; 152 153 static int NVIDIANodeID = 1; 154 155 static void 156 g_raid_md_nvidia_print(struct nvidia_raid_conf *meta) 157 { 158 159 if (g_raid_debug < 1) 160 return; 161 162 printf("********* ATA NVIDIA RAID Metadata *********\n"); 163 printf("nvidia_id <%.8s>\n", meta->nvidia_id); 164 printf("config_size %u\n", meta->config_size); 165 printf("checksum 0x%08x\n", meta->checksum); 166 printf("version 0x%04x\n", meta->version); 167 printf("disk_number %d\n", meta->disk_number); 168 printf("dummy_0 0x%02x\n", meta->dummy_0); 169 printf("total_sectors %u\n", meta->total_sectors); 170 printf("sector_size %u\n", meta->sector_size); 171 printf("name <%.16s>\n", meta->name); 172 printf("revision 0x%02x%02x%02x%02x\n", 173 meta->revision[0], meta->revision[1], 174 meta->revision[2], meta->revision[3]); 175 printf("disk_status 0x%08x\n", meta->disk_status); 176 printf("magic_0 0x%08x\n", meta->magic_0); 177 printf("volume_id 0x%016jx%016jx\n", 178 meta->volume_id[1], meta->volume_id[0]); 179 printf("state 0x%02x\n", meta->state); 180 printf("array_width %u\n", meta->array_width); 181 printf("total_disks %u\n", meta->total_disks); 182 printf("orig_array_width %u\n", meta->orig_array_width); 183 printf("type 0x%04x\n", meta->type); 184 printf("dummy_3 0x%04x\n", meta->dummy_3); 185 printf("strip_sectors %u\n", meta->strip_sectors); 186 printf("strip_bytes %u\n", meta->strip_bytes); 187 printf("strip_shift %u\n", meta->strip_shift); 188 printf("strip_mask 0x%08x\n", meta->strip_mask); 189 printf("stripe_sectors %u\n", meta->stripe_sectors); 190 printf("stripe_bytes %u\n", meta->stripe_bytes); 191 printf("rebuild_lba %u\n", meta->rebuild_lba); 192 printf("orig_type 0x%04x\n", meta->orig_type); 193 printf("orig_total_sectors %u\n", meta->orig_total_sectors); 194 printf("status 0x%08x\n", meta->status); 195 printf("=================================================\n"); 196 } 197 198 static struct nvidia_raid_conf * 199 nvidia_meta_copy(struct nvidia_raid_conf *meta) 200 { 201 struct nvidia_raid_conf *nmeta; 202 203 nmeta = malloc(sizeof(*meta), M_MD_NVIDIA, M_WAITOK); 204 memcpy(nmeta, meta, sizeof(*meta)); 205 return (nmeta); 206 } 207 208 static int 209 nvidia_meta_translate_disk(struct nvidia_raid_conf *meta, int md_disk_pos) 210 { 211 int disk_pos; 212 213 if (md_disk_pos >= 0 && meta->type == NVIDIA_T_RAID01) { 214 disk_pos = (md_disk_pos / meta->array_width) + 215 (md_disk_pos % meta->array_width) * meta->array_width; 216 } else 217 disk_pos = md_disk_pos; 218 return (disk_pos); 219 } 220 221 static void 222 nvidia_meta_get_name(struct nvidia_raid_conf *meta, char *buf) 223 { 224 int i; 225 226 strncpy(buf, meta->name, 16); 227 buf[16] = 0; 228 for (i = 15; i >= 0; i--) { 229 if (buf[i] > 0x20) 230 break; 231 buf[i] = 0; 232 } 233 } 234 235 static void 236 nvidia_meta_put_name(struct nvidia_raid_conf *meta, char *buf) 237 { 238 239 memset(meta->name, 0x20, 16); 240 memcpy(meta->name, buf, MIN(strlen(buf), 16)); 241 } 242 243 static struct nvidia_raid_conf * 244 nvidia_meta_read(struct g_consumer *cp) 245 { 246 struct g_provider *pp; 247 struct nvidia_raid_conf *meta; 248 char *buf; 249 int error, i; 250 uint32_t checksum, *ptr; 251 252 pp = cp->provider; 253 254 /* Read the anchor sector. */ 255 buf = g_read_data(cp, 256 pp->mediasize - 2 * pp->sectorsize, pp->sectorsize, &error); 257 if (buf == NULL) { 258 G_RAID_DEBUG(1, "Cannot read metadata from %s (error=%d).", 259 pp->name, error); 260 return (NULL); 261 } 262 meta = (struct nvidia_raid_conf *)buf; 263 264 /* Check if this is an NVIDIA RAID struct */ 265 if (strncmp(meta->nvidia_id, NVIDIA_MAGIC, strlen(NVIDIA_MAGIC))) { 266 G_RAID_DEBUG(1, "NVIDIA signature check failed on %s", pp->name); 267 g_free(buf); 268 return (NULL); 269 } 270 if (meta->config_size > 128 || 271 meta->config_size < 30) { 272 G_RAID_DEBUG(1, "NVIDIA metadata size looks wrong: %d", 273 meta->config_size); 274 g_free(buf); 275 return (NULL); 276 } 277 meta = malloc(sizeof(*meta), M_MD_NVIDIA, M_WAITOK); 278 memcpy(meta, buf, min(sizeof(*meta), pp->sectorsize)); 279 g_free(buf); 280 281 /* Check metadata checksum. */ 282 for (checksum = 0, ptr = (uint32_t *)meta, 283 i = 0; i < meta->config_size; i++) 284 checksum += *ptr++; 285 if (checksum != 0) { 286 G_RAID_DEBUG(1, "NVIDIA checksum check failed on %s", pp->name); 287 free(meta, M_MD_NVIDIA); 288 return (NULL); 289 } 290 291 /* Check volume state. */ 292 if (meta->state != NVIDIA_S_IDLE && meta->state != NVIDIA_S_INIT && 293 meta->state != NVIDIA_S_REBUILD && meta->state != NVIDIA_S_SYNC) { 294 G_RAID_DEBUG(1, "NVIDIA unknown state on %s (0x%02x)", 295 pp->name, meta->state); 296 free(meta, M_MD_NVIDIA); 297 return (NULL); 298 } 299 300 /* Check raid type. */ 301 if (meta->type != NVIDIA_T_RAID0 && meta->type != NVIDIA_T_RAID1 && 302 meta->type != NVIDIA_T_RAID3 && meta->type != NVIDIA_T_RAID5 && 303 meta->type != NVIDIA_T_RAID5_SYM && 304 meta->type != NVIDIA_T_RAID01 && meta->type != NVIDIA_T_CONCAT) { 305 G_RAID_DEBUG(1, "NVIDIA unknown RAID level on %s (0x%02x)", 306 pp->name, meta->type); 307 free(meta, M_MD_NVIDIA); 308 return (NULL); 309 } 310 311 return (meta); 312 } 313 314 static int 315 nvidia_meta_write(struct g_consumer *cp, struct nvidia_raid_conf *meta) 316 { 317 struct g_provider *pp; 318 char *buf; 319 int error, i; 320 uint32_t checksum, *ptr; 321 322 pp = cp->provider; 323 324 /* Recalculate checksum for case if metadata were changed. */ 325 meta->checksum = 0; 326 for (checksum = 0, ptr = (uint32_t *)meta, 327 i = 0; i < meta->config_size; i++) 328 checksum += *ptr++; 329 meta->checksum -= checksum; 330 331 /* Create and fill buffer. */ 332 buf = malloc(pp->sectorsize, M_MD_NVIDIA, M_WAITOK | M_ZERO); 333 memcpy(buf, meta, sizeof(*meta)); 334 335 /* Write metadata. */ 336 error = g_write_data(cp, 337 pp->mediasize - 2 * pp->sectorsize, buf, pp->sectorsize); 338 if (error != 0) { 339 G_RAID_DEBUG(1, "Cannot write metadata to %s (error=%d).", 340 pp->name, error); 341 } 342 343 free(buf, M_MD_NVIDIA); 344 return (error); 345 } 346 347 static int 348 nvidia_meta_erase(struct g_consumer *cp) 349 { 350 struct g_provider *pp; 351 char *buf; 352 int error; 353 354 pp = cp->provider; 355 buf = malloc(pp->sectorsize, M_MD_NVIDIA, M_WAITOK | M_ZERO); 356 error = g_write_data(cp, 357 pp->mediasize - 2 * pp->sectorsize, buf, pp->sectorsize); 358 if (error != 0) { 359 G_RAID_DEBUG(1, "Cannot erase metadata on %s (error=%d).", 360 pp->name, error); 361 } 362 free(buf, M_MD_NVIDIA); 363 return (error); 364 } 365 366 static struct g_raid_disk * 367 g_raid_md_nvidia_get_disk(struct g_raid_softc *sc, int id) 368 { 369 struct g_raid_disk *disk; 370 struct g_raid_md_nvidia_perdisk *pd; 371 372 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 373 pd = (struct g_raid_md_nvidia_perdisk *)disk->d_md_data; 374 if (pd->pd_disk_pos == id) 375 break; 376 } 377 return (disk); 378 } 379 380 static int 381 g_raid_md_nvidia_supported(int level, int qual, int disks, int force) 382 { 383 384 switch (level) { 385 case G_RAID_VOLUME_RL_RAID0: 386 if (disks < 1) 387 return (0); 388 if (!force && (disks < 2 || disks > 6)) 389 return (0); 390 break; 391 case G_RAID_VOLUME_RL_RAID1: 392 if (disks < 1) 393 return (0); 394 if (!force && (disks != 2)) 395 return (0); 396 break; 397 case G_RAID_VOLUME_RL_RAID1E: 398 if (disks < 2) 399 return (0); 400 if (disks % 2 != 0) 401 return (0); 402 if (!force && (disks < 4)) 403 return (0); 404 break; 405 case G_RAID_VOLUME_RL_SINGLE: 406 if (disks != 1) 407 return (0); 408 break; 409 case G_RAID_VOLUME_RL_CONCAT: 410 if (disks < 2) 411 return (0); 412 break; 413 case G_RAID_VOLUME_RL_RAID5: 414 if (disks < 3) 415 return (0); 416 if (qual != G_RAID_VOLUME_RLQ_R5LA && 417 qual != G_RAID_VOLUME_RLQ_R5LS) 418 return (0); 419 break; 420 default: 421 return (0); 422 } 423 if (level != G_RAID_VOLUME_RL_RAID5 && qual != G_RAID_VOLUME_RLQ_NONE) 424 return (0); 425 return (1); 426 } 427 428 static int 429 g_raid_md_nvidia_start_disk(struct g_raid_disk *disk) 430 { 431 struct g_raid_softc *sc; 432 struct g_raid_subdisk *sd, *tmpsd; 433 struct g_raid_disk *olddisk, *tmpdisk; 434 struct g_raid_md_object *md; 435 struct g_raid_md_nvidia_object *mdi; 436 struct g_raid_md_nvidia_perdisk *pd, *oldpd; 437 struct nvidia_raid_conf *meta; 438 int disk_pos, resurrection = 0; 439 440 sc = disk->d_softc; 441 md = sc->sc_md; 442 mdi = (struct g_raid_md_nvidia_object *)md; 443 meta = mdi->mdio_meta; 444 pd = (struct g_raid_md_nvidia_perdisk *)disk->d_md_data; 445 olddisk = NULL; 446 447 /* Find disk position in metadata by its serial. */ 448 if (pd->pd_meta != NULL) { 449 disk_pos = pd->pd_meta->disk_number; 450 if (disk_pos >= meta->total_disks || mdi->mdio_started) 451 disk_pos = -3; 452 } else 453 disk_pos = -3; 454 /* For RAID0+1 we need to translate order. */ 455 disk_pos = nvidia_meta_translate_disk(meta, disk_pos); 456 if (disk_pos < 0) { 457 G_RAID_DEBUG1(1, sc, "Unknown, probably new or stale disk"); 458 /* If we are in the start process, that's all for now. */ 459 if (!mdi->mdio_started) 460 goto nofit; 461 /* 462 * If we have already started - try to get use of the disk. 463 * Try to replace OFFLINE disks first, then FAILED. 464 */ 465 TAILQ_FOREACH(tmpdisk, &sc->sc_disks, d_next) { 466 if (tmpdisk->d_state != G_RAID_DISK_S_OFFLINE && 467 tmpdisk->d_state != G_RAID_DISK_S_FAILED) 468 continue; 469 /* Make sure this disk is big enough. */ 470 TAILQ_FOREACH(sd, &tmpdisk->d_subdisks, sd_next) { 471 if (sd->sd_offset + sd->sd_size + 2 * 512 > 472 pd->pd_disk_size) { 473 G_RAID_DEBUG1(1, sc, 474 "Disk too small (%ju < %ju)", 475 pd->pd_disk_size, 476 sd->sd_offset + sd->sd_size + 512); 477 break; 478 } 479 } 480 if (sd != NULL) 481 continue; 482 if (tmpdisk->d_state == G_RAID_DISK_S_OFFLINE) { 483 olddisk = tmpdisk; 484 break; 485 } else if (olddisk == NULL) 486 olddisk = tmpdisk; 487 } 488 if (olddisk == NULL) { 489 nofit: 490 g_raid_change_disk_state(disk, G_RAID_DISK_S_SPARE); 491 return (1); 492 } 493 oldpd = (struct g_raid_md_nvidia_perdisk *)olddisk->d_md_data; 494 disk_pos = oldpd->pd_disk_pos; 495 resurrection = 1; 496 } 497 498 if (olddisk == NULL) { 499 /* Find placeholder by position. */ 500 olddisk = g_raid_md_nvidia_get_disk(sc, disk_pos); 501 if (olddisk == NULL) 502 panic("No disk at position %d!", disk_pos); 503 if (olddisk->d_state != G_RAID_DISK_S_OFFLINE) { 504 G_RAID_DEBUG1(1, sc, "More than one disk for pos %d", 505 disk_pos); 506 g_raid_change_disk_state(disk, G_RAID_DISK_S_STALE); 507 return (0); 508 } 509 oldpd = (struct g_raid_md_nvidia_perdisk *)olddisk->d_md_data; 510 } 511 512 /* Replace failed disk or placeholder with new disk. */ 513 TAILQ_FOREACH_SAFE(sd, &olddisk->d_subdisks, sd_next, tmpsd) { 514 TAILQ_REMOVE(&olddisk->d_subdisks, sd, sd_next); 515 TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next); 516 sd->sd_disk = disk; 517 } 518 oldpd->pd_disk_pos = -2; 519 pd->pd_disk_pos = disk_pos; 520 521 /* If it was placeholder -- destroy it. */ 522 if (olddisk->d_state == G_RAID_DISK_S_OFFLINE) { 523 g_raid_destroy_disk(olddisk); 524 } else { 525 /* Otherwise, make it STALE_FAILED. */ 526 g_raid_change_disk_state(olddisk, G_RAID_DISK_S_STALE_FAILED); 527 } 528 529 /* Welcome the new disk. */ 530 if (resurrection) 531 g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE); 532 else// if (pd->pd_meta->disk_status == NVIDIA_S_CURRENT || 533 //pd->pd_meta->disk_status == NVIDIA_S_REBUILD) 534 g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE); 535 // else 536 // g_raid_change_disk_state(disk, G_RAID_DISK_S_FAILED); 537 TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) { 538 /* 539 * Different disks may have different sizes, 540 * in concat mode. Update from real disk size. 541 */ 542 if (meta->type == NVIDIA_T_CONCAT) 543 sd->sd_size = pd->pd_disk_size - 0x800 * 512; 544 545 if (resurrection) { 546 /* New or ex-spare disk. */ 547 g_raid_change_subdisk_state(sd, 548 G_RAID_SUBDISK_S_NEW); 549 } else if (meta->state == NVIDIA_S_REBUILD && 550 (pd->pd_meta->disk_status & 0x100)) { 551 /* Rebuilding disk. */ 552 g_raid_change_subdisk_state(sd, 553 G_RAID_SUBDISK_S_REBUILD); 554 sd->sd_rebuild_pos = (off_t)pd->pd_meta->rebuild_lba / 555 meta->array_width * pd->pd_meta->sector_size; 556 } else if (meta->state == NVIDIA_S_SYNC) { 557 /* Resyncing/dirty disk. */ 558 g_raid_change_subdisk_state(sd, 559 G_RAID_SUBDISK_S_RESYNC); 560 sd->sd_rebuild_pos = (off_t)pd->pd_meta->rebuild_lba / 561 meta->array_width * pd->pd_meta->sector_size; 562 } else { 563 /* Up to date disk. */ 564 g_raid_change_subdisk_state(sd, 565 G_RAID_SUBDISK_S_ACTIVE); 566 } 567 g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW, 568 G_RAID_EVENT_SUBDISK); 569 } 570 571 /* Update status of our need for spare. */ 572 if (mdi->mdio_started) { 573 mdi->mdio_incomplete = 574 (g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE) < 575 mdi->mdio_total_disks); 576 } 577 578 return (resurrection); 579 } 580 581 static void 582 g_disk_md_nvidia_retaste(void *arg, int pending) 583 { 584 585 G_RAID_DEBUG(1, "Array is not complete, trying to retaste."); 586 g_retaste(&g_raid_class); 587 free(arg, M_MD_NVIDIA); 588 } 589 590 static void 591 g_raid_md_nvidia_refill(struct g_raid_softc *sc) 592 { 593 struct g_raid_md_object *md; 594 struct g_raid_md_nvidia_object *mdi; 595 struct g_raid_disk *disk; 596 struct task *task; 597 int update, na; 598 599 md = sc->sc_md; 600 mdi = (struct g_raid_md_nvidia_object *)md; 601 update = 0; 602 do { 603 /* Make sure we miss anything. */ 604 na = g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE); 605 if (na == mdi->mdio_total_disks) 606 break; 607 608 G_RAID_DEBUG1(1, md->mdo_softc, 609 "Array is not complete (%d of %d), " 610 "trying to refill.", na, mdi->mdio_total_disks); 611 612 /* Try to get use some of STALE disks. */ 613 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 614 if (disk->d_state == G_RAID_DISK_S_STALE) { 615 update += g_raid_md_nvidia_start_disk(disk); 616 if (disk->d_state == G_RAID_DISK_S_ACTIVE) 617 break; 618 } 619 } 620 if (disk != NULL) 621 continue; 622 623 /* Try to get use some of SPARE disks. */ 624 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 625 if (disk->d_state == G_RAID_DISK_S_SPARE) { 626 update += g_raid_md_nvidia_start_disk(disk); 627 if (disk->d_state == G_RAID_DISK_S_ACTIVE) 628 break; 629 } 630 } 631 } while (disk != NULL); 632 633 /* Write new metadata if we changed something. */ 634 if (update) 635 g_raid_md_write_nvidia(md, NULL, NULL, NULL); 636 637 /* Update status of our need for spare. */ 638 mdi->mdio_incomplete = (g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE) < 639 mdi->mdio_total_disks); 640 641 /* Request retaste hoping to find spare. */ 642 if (mdi->mdio_incomplete) { 643 task = malloc(sizeof(struct task), 644 M_MD_NVIDIA, M_WAITOK | M_ZERO); 645 TASK_INIT(task, 0, g_disk_md_nvidia_retaste, task); 646 taskqueue_enqueue(taskqueue_swi, task); 647 } 648 } 649 650 static void 651 g_raid_md_nvidia_start(struct g_raid_softc *sc) 652 { 653 struct g_raid_md_object *md; 654 struct g_raid_md_nvidia_object *mdi; 655 struct g_raid_md_nvidia_perdisk *pd; 656 struct nvidia_raid_conf *meta; 657 struct g_raid_volume *vol; 658 struct g_raid_subdisk *sd; 659 struct g_raid_disk *disk; 660 off_t size; 661 int j, disk_pos; 662 char buf[17]; 663 664 md = sc->sc_md; 665 mdi = (struct g_raid_md_nvidia_object *)md; 666 meta = mdi->mdio_meta; 667 668 /* Create volumes and subdisks. */ 669 nvidia_meta_get_name(meta, buf); 670 vol = g_raid_create_volume(sc, buf, -1); 671 vol->v_mediasize = (off_t)meta->total_sectors * 512; 672 vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_NONE; 673 if (meta->type == NVIDIA_T_RAID0) { 674 vol->v_raid_level = G_RAID_VOLUME_RL_RAID0; 675 size = vol->v_mediasize / mdi->mdio_total_disks; 676 } else if (meta->type == NVIDIA_T_RAID1) { 677 vol->v_raid_level = G_RAID_VOLUME_RL_RAID1; 678 size = vol->v_mediasize; 679 } else if (meta->type == NVIDIA_T_RAID01) { 680 vol->v_raid_level = G_RAID_VOLUME_RL_RAID1E; 681 size = vol->v_mediasize / (mdi->mdio_total_disks / 2); 682 } else if (meta->type == NVIDIA_T_CONCAT) { 683 if (mdi->mdio_total_disks == 1) 684 vol->v_raid_level = G_RAID_VOLUME_RL_SINGLE; 685 else 686 vol->v_raid_level = G_RAID_VOLUME_RL_CONCAT; 687 size = 0; 688 } else if (meta->type == NVIDIA_T_RAID5) { 689 vol->v_raid_level = G_RAID_VOLUME_RL_RAID5; 690 vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_R5LA; 691 size = vol->v_mediasize / (mdi->mdio_total_disks - 1); 692 } else if (meta->type == NVIDIA_T_RAID5_SYM) { 693 vol->v_raid_level = G_RAID_VOLUME_RL_RAID5; 694 vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_R5LS; 695 size = vol->v_mediasize / (mdi->mdio_total_disks - 1); 696 } else { 697 vol->v_raid_level = G_RAID_VOLUME_RL_UNKNOWN; 698 size = 0; 699 } 700 vol->v_strip_size = meta->strip_sectors * 512; //ZZZ 701 vol->v_disks_count = mdi->mdio_total_disks; 702 vol->v_sectorsize = 512; //ZZZ 703 for (j = 0; j < vol->v_disks_count; j++) { 704 sd = &vol->v_subdisks[j]; 705 sd->sd_offset = 0; 706 sd->sd_size = size; 707 } 708 g_raid_start_volume(vol); 709 710 /* Create disk placeholders to store data for later writing. */ 711 for (disk_pos = 0; disk_pos < mdi->mdio_total_disks; disk_pos++) { 712 pd = malloc(sizeof(*pd), M_MD_NVIDIA, M_WAITOK | M_ZERO); 713 pd->pd_disk_pos = disk_pos; 714 disk = g_raid_create_disk(sc); 715 disk->d_md_data = (void *)pd; 716 disk->d_state = G_RAID_DISK_S_OFFLINE; 717 sd = &vol->v_subdisks[disk_pos]; 718 sd->sd_disk = disk; 719 TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next); 720 } 721 722 /* Make all disks found till the moment take their places. */ 723 do { 724 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 725 if (disk->d_state == G_RAID_DISK_S_NONE) { 726 g_raid_md_nvidia_start_disk(disk); 727 break; 728 } 729 } 730 } while (disk != NULL); 731 732 mdi->mdio_started = 1; 733 G_RAID_DEBUG1(0, sc, "Array started."); 734 g_raid_md_write_nvidia(md, NULL, NULL, NULL); 735 736 /* Pickup any STALE/SPARE disks to refill array if needed. */ 737 g_raid_md_nvidia_refill(sc); 738 739 g_raid_event_send(vol, G_RAID_VOLUME_E_START, G_RAID_EVENT_VOLUME); 740 741 callout_stop(&mdi->mdio_start_co); 742 G_RAID_DEBUG1(1, sc, "root_mount_rel %p", mdi->mdio_rootmount); 743 root_mount_rel(mdi->mdio_rootmount); 744 mdi->mdio_rootmount = NULL; 745 } 746 747 static void 748 g_raid_md_nvidia_new_disk(struct g_raid_disk *disk) 749 { 750 struct g_raid_softc *sc; 751 struct g_raid_md_object *md; 752 struct g_raid_md_nvidia_object *mdi; 753 struct nvidia_raid_conf *pdmeta; 754 struct g_raid_md_nvidia_perdisk *pd; 755 756 sc = disk->d_softc; 757 md = sc->sc_md; 758 mdi = (struct g_raid_md_nvidia_object *)md; 759 pd = (struct g_raid_md_nvidia_perdisk *)disk->d_md_data; 760 pdmeta = pd->pd_meta; 761 762 if (mdi->mdio_started) { 763 if (g_raid_md_nvidia_start_disk(disk)) 764 g_raid_md_write_nvidia(md, NULL, NULL, NULL); 765 } else { 766 if (mdi->mdio_meta == NULL || 767 mdi->mdio_meta->disk_number >= mdi->mdio_meta->total_disks) { 768 G_RAID_DEBUG1(1, sc, "Newer disk"); 769 if (mdi->mdio_meta != NULL) 770 free(mdi->mdio_meta, M_MD_NVIDIA); 771 mdi->mdio_meta = nvidia_meta_copy(pdmeta); 772 mdi->mdio_total_disks = pdmeta->total_disks; 773 mdi->mdio_disks_present = 1; 774 } else if (pdmeta->disk_number < mdi->mdio_meta->total_disks) { 775 mdi->mdio_disks_present++; 776 G_RAID_DEBUG1(1, sc, "Matching disk (%d of %d up)", 777 mdi->mdio_disks_present, 778 mdi->mdio_total_disks); 779 } else 780 G_RAID_DEBUG1(1, sc, "Spare disk"); 781 782 /* If we collected all needed disks - start array. */ 783 if (mdi->mdio_disks_present == mdi->mdio_total_disks) 784 g_raid_md_nvidia_start(sc); 785 } 786 } 787 788 static void 789 g_raid_nvidia_go(void *arg) 790 { 791 struct g_raid_softc *sc; 792 struct g_raid_md_object *md; 793 struct g_raid_md_nvidia_object *mdi; 794 795 sc = arg; 796 md = sc->sc_md; 797 mdi = (struct g_raid_md_nvidia_object *)md; 798 if (!mdi->mdio_started) { 799 G_RAID_DEBUG1(0, sc, "Force array start due to timeout."); 800 g_raid_event_send(sc, G_RAID_NODE_E_START, 0); 801 } 802 } 803 804 static int 805 g_raid_md_create_nvidia(struct g_raid_md_object *md, struct g_class *mp, 806 struct g_geom **gp) 807 { 808 struct g_raid_softc *sc; 809 struct g_raid_md_nvidia_object *mdi; 810 char name[32]; 811 812 mdi = (struct g_raid_md_nvidia_object *)md; 813 arc4rand(&mdi->mdio_volume_id, 16, 0); 814 snprintf(name, sizeof(name), "NVIDIA-%d", 815 atomic_fetchadd_int(&NVIDIANodeID, 1)); 816 sc = g_raid_create_node(mp, name, md); 817 if (sc == NULL) 818 return (G_RAID_MD_TASTE_FAIL); 819 md->mdo_softc = sc; 820 *gp = sc->sc_geom; 821 return (G_RAID_MD_TASTE_NEW); 822 } 823 824 static int 825 g_raid_md_taste_nvidia(struct g_raid_md_object *md, struct g_class *mp, 826 struct g_consumer *cp, struct g_geom **gp) 827 { 828 struct g_consumer *rcp; 829 struct g_provider *pp; 830 struct g_raid_md_nvidia_object *mdi, *mdi1; 831 struct g_raid_softc *sc; 832 struct g_raid_disk *disk; 833 struct nvidia_raid_conf *meta; 834 struct g_raid_md_nvidia_perdisk *pd; 835 struct g_geom *geom; 836 int result, spare, len; 837 char name[32]; 838 uint16_t vendor; 839 840 G_RAID_DEBUG(1, "Tasting NVIDIA on %s", cp->provider->name); 841 mdi = (struct g_raid_md_nvidia_object *)md; 842 pp = cp->provider; 843 844 /* Read metadata from device. */ 845 meta = NULL; 846 g_topology_unlock(); 847 vendor = 0xffff; 848 len = sizeof(vendor); 849 if (pp->geom->rank == 1) 850 g_io_getattr("GEOM::hba_vendor", cp, &len, &vendor); 851 meta = nvidia_meta_read(cp); 852 g_topology_lock(); 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 /* There is no return after this point, so we close passed consumer. */ 922 g_access(cp, -1, 0, 0); 923 924 rcp = g_new_consumer(geom); 925 rcp->flags |= G_CF_DIRECT_RECEIVE; 926 g_attach(rcp, pp); 927 if (g_access(rcp, 1, 1, 1) != 0) 928 ; //goto fail1; 929 930 g_topology_unlock(); 931 sx_xlock(&sc->sc_lock); 932 933 pd = malloc(sizeof(*pd), M_MD_NVIDIA, M_WAITOK | M_ZERO); 934 pd->pd_meta = meta; 935 if (spare == 2) { 936 pd->pd_disk_pos = -3; 937 } else { 938 pd->pd_disk_pos = -1; 939 } 940 pd->pd_disk_size = pp->mediasize; 941 disk = g_raid_create_disk(sc); 942 disk->d_md_data = (void *)pd; 943 disk->d_consumer = rcp; 944 rcp->private = disk; 945 946 g_raid_get_disk_info(disk); 947 948 g_raid_md_nvidia_new_disk(disk); 949 950 sx_xunlock(&sc->sc_lock); 951 g_topology_lock(); 952 *gp = geom; 953 return (result); 954 fail1: 955 free(meta, M_MD_NVIDIA); 956 return (G_RAID_MD_TASTE_FAIL); 957 } 958 959 static int 960 g_raid_md_event_nvidia(struct g_raid_md_object *md, 961 struct g_raid_disk *disk, u_int event) 962 { 963 struct g_raid_softc *sc; 964 struct g_raid_subdisk *sd; 965 struct g_raid_md_nvidia_object *mdi; 966 struct g_raid_md_nvidia_perdisk *pd; 967 968 sc = md->mdo_softc; 969 mdi = (struct g_raid_md_nvidia_object *)md; 970 if (disk == NULL) { 971 switch (event) { 972 case G_RAID_NODE_E_START: 973 if (!mdi->mdio_started) { 974 /* Bump volume ID to drop missing disks. */ 975 arc4rand(&mdi->mdio_volume_id, 16, 0); 976 g_raid_md_nvidia_start(sc); 977 } 978 return (0); 979 } 980 return (-1); 981 } 982 pd = (struct g_raid_md_nvidia_perdisk *)disk->d_md_data; 983 switch (event) { 984 case G_RAID_DISK_E_DISCONNECTED: 985 /* If disk was assigned, just update statuses. */ 986 if (pd->pd_disk_pos >= 0) { 987 g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE); 988 if (disk->d_consumer) { 989 g_raid_kill_consumer(sc, disk->d_consumer); 990 disk->d_consumer = NULL; 991 } 992 TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) { 993 g_raid_change_subdisk_state(sd, 994 G_RAID_SUBDISK_S_NONE); 995 g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED, 996 G_RAID_EVENT_SUBDISK); 997 } 998 } else { 999 /* Otherwise -- delete. */ 1000 g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE); 1001 g_raid_destroy_disk(disk); 1002 } 1003 1004 if (mdi->mdio_started) { 1005 /* Bump volume ID to prevent disk resurrection. */ 1006 if (pd->pd_disk_pos >= 0) 1007 arc4rand(&mdi->mdio_volume_id, 16, 0); 1008 1009 /* Write updated metadata to all disks. */ 1010 g_raid_md_write_nvidia(md, NULL, NULL, NULL); 1011 } 1012 1013 /* Check if anything left except placeholders. */ 1014 if (g_raid_ndisks(sc, -1) == 1015 g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE)) 1016 g_raid_destroy_node(sc, 0); 1017 else 1018 g_raid_md_nvidia_refill(sc); 1019 return (0); 1020 } 1021 return (-2); 1022 } 1023 1024 static int 1025 g_raid_md_ctl_nvidia(struct g_raid_md_object *md, 1026 struct gctl_req *req) 1027 { 1028 struct g_raid_softc *sc; 1029 struct g_raid_volume *vol; 1030 struct g_raid_subdisk *sd; 1031 struct g_raid_disk *disk; 1032 struct g_raid_md_nvidia_object *mdi; 1033 struct g_raid_md_nvidia_perdisk *pd; 1034 struct g_consumer *cp; 1035 struct g_provider *pp; 1036 char arg[16]; 1037 const char *verb, *volname, *levelname, *diskname; 1038 int *nargs, *force; 1039 off_t size, sectorsize, strip, volsize; 1040 intmax_t *sizearg, *striparg; 1041 int numdisks, i, len, level, qual, update; 1042 int error; 1043 1044 sc = md->mdo_softc; 1045 mdi = (struct g_raid_md_nvidia_object *)md; 1046 verb = gctl_get_param(req, "verb", NULL); 1047 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 1048 error = 0; 1049 if (strcmp(verb, "label") == 0) { 1050 if (*nargs < 4) { 1051 gctl_error(req, "Invalid number of arguments."); 1052 return (-1); 1053 } 1054 volname = gctl_get_asciiparam(req, "arg1"); 1055 if (volname == NULL) { 1056 gctl_error(req, "No volume name."); 1057 return (-2); 1058 } 1059 levelname = gctl_get_asciiparam(req, "arg2"); 1060 if (levelname == NULL) { 1061 gctl_error(req, "No RAID level."); 1062 return (-3); 1063 } 1064 if (strcasecmp(levelname, "RAID5") == 0) 1065 levelname = "RAID5-LS"; 1066 if (g_raid_volume_str2level(levelname, &level, &qual)) { 1067 gctl_error(req, "Unknown RAID level '%s'.", levelname); 1068 return (-4); 1069 } 1070 numdisks = *nargs - 3; 1071 force = gctl_get_paraml(req, "force", sizeof(*force)); 1072 if (!g_raid_md_nvidia_supported(level, qual, numdisks, 1073 force ? *force : 0)) { 1074 gctl_error(req, "Unsupported RAID level " 1075 "(0x%02x/0x%02x), or number of disks (%d).", 1076 level, qual, numdisks); 1077 return (-5); 1078 } 1079 1080 /* Search for disks, connect them and probe. */ 1081 size = 0x7fffffffffffffffllu; 1082 sectorsize = 0; 1083 for (i = 0; i < numdisks; i++) { 1084 snprintf(arg, sizeof(arg), "arg%d", i + 3); 1085 diskname = gctl_get_asciiparam(req, arg); 1086 if (diskname == NULL) { 1087 gctl_error(req, "No disk name (%s).", arg); 1088 error = -6; 1089 break; 1090 } 1091 if (strcmp(diskname, "NONE") == 0) { 1092 cp = NULL; 1093 pp = NULL; 1094 } else { 1095 g_topology_lock(); 1096 cp = g_raid_open_consumer(sc, diskname); 1097 if (cp == NULL) { 1098 gctl_error(req, "Can't open '%s'.", 1099 diskname); 1100 g_topology_unlock(); 1101 error = -7; 1102 break; 1103 } 1104 pp = cp->provider; 1105 } 1106 pd = malloc(sizeof(*pd), M_MD_NVIDIA, M_WAITOK | M_ZERO); 1107 pd->pd_disk_pos = i; 1108 disk = g_raid_create_disk(sc); 1109 disk->d_md_data = (void *)pd; 1110 disk->d_consumer = cp; 1111 if (cp == NULL) 1112 continue; 1113 cp->private = disk; 1114 g_topology_unlock(); 1115 1116 g_raid_get_disk_info(disk); 1117 1118 pd->pd_disk_size = pp->mediasize; 1119 if (size > pp->mediasize) 1120 size = pp->mediasize; 1121 if (sectorsize < pp->sectorsize) 1122 sectorsize = pp->sectorsize; 1123 } 1124 if (error != 0) 1125 return (error); 1126 1127 if (sectorsize <= 0) { 1128 gctl_error(req, "Can't get sector size."); 1129 return (-8); 1130 } 1131 1132 /* Reserve space for metadata. */ 1133 size -= 2 * sectorsize; 1134 1135 /* Handle size argument. */ 1136 len = sizeof(*sizearg); 1137 sizearg = gctl_get_param(req, "size", &len); 1138 if (sizearg != NULL && len == sizeof(*sizearg) && 1139 *sizearg > 0) { 1140 if (*sizearg > size) { 1141 gctl_error(req, "Size too big %lld > %lld.", 1142 (long long)*sizearg, (long long)size); 1143 return (-9); 1144 } 1145 size = *sizearg; 1146 } 1147 1148 /* Handle strip argument. */ 1149 strip = 131072; 1150 len = sizeof(*striparg); 1151 striparg = gctl_get_param(req, "strip", &len); 1152 if (striparg != NULL && len == sizeof(*striparg) && 1153 *striparg > 0) { 1154 if (*striparg < sectorsize) { 1155 gctl_error(req, "Strip size too small."); 1156 return (-10); 1157 } 1158 if (*striparg % sectorsize != 0) { 1159 gctl_error(req, "Incorrect strip size."); 1160 return (-11); 1161 } 1162 if (strip > 65535 * sectorsize) { 1163 gctl_error(req, "Strip size too big."); 1164 return (-12); 1165 } 1166 strip = *striparg; 1167 } 1168 1169 /* Round size down to strip or sector. */ 1170 if (level == G_RAID_VOLUME_RL_RAID1) 1171 size -= (size % sectorsize); 1172 else if (level == G_RAID_VOLUME_RL_RAID1E && 1173 (numdisks & 1) != 0) 1174 size -= (size % (2 * strip)); 1175 else 1176 size -= (size % strip); 1177 if (size <= 0) { 1178 gctl_error(req, "Size too small."); 1179 return (-13); 1180 } 1181 1182 if (level == G_RAID_VOLUME_RL_RAID0 || 1183 level == G_RAID_VOLUME_RL_CONCAT || 1184 level == G_RAID_VOLUME_RL_SINGLE) 1185 volsize = size * numdisks; 1186 else if (level == G_RAID_VOLUME_RL_RAID1) 1187 volsize = size; 1188 else if (level == G_RAID_VOLUME_RL_RAID5) 1189 volsize = size * (numdisks - 1); 1190 else { /* RAID1E */ 1191 volsize = ((size * numdisks) / strip / 2) * 1192 strip; 1193 } 1194 if (volsize > 0xffffffffllu * sectorsize) { 1195 gctl_error(req, "Size too big."); 1196 return (-14); 1197 } 1198 1199 /* We have all we need, create things: volume, ... */ 1200 mdi->mdio_total_disks = numdisks; 1201 mdi->mdio_started = 1; 1202 vol = g_raid_create_volume(sc, volname, -1); 1203 vol->v_md_data = (void *)(intptr_t)0; 1204 vol->v_raid_level = level; 1205 vol->v_raid_level_qualifier = qual; 1206 vol->v_strip_size = strip; 1207 vol->v_disks_count = numdisks; 1208 vol->v_mediasize = volsize; 1209 vol->v_sectorsize = sectorsize; 1210 g_raid_start_volume(vol); 1211 1212 /* , and subdisks. */ 1213 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 1214 pd = (struct g_raid_md_nvidia_perdisk *)disk->d_md_data; 1215 sd = &vol->v_subdisks[pd->pd_disk_pos]; 1216 sd->sd_disk = disk; 1217 sd->sd_offset = 0; 1218 sd->sd_size = size; 1219 TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next); 1220 if (sd->sd_disk->d_consumer != NULL) { 1221 g_raid_change_disk_state(disk, 1222 G_RAID_DISK_S_ACTIVE); 1223 g_raid_change_subdisk_state(sd, 1224 G_RAID_SUBDISK_S_ACTIVE); 1225 g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW, 1226 G_RAID_EVENT_SUBDISK); 1227 } else { 1228 g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE); 1229 } 1230 } 1231 1232 /* Write metadata based on created entities. */ 1233 G_RAID_DEBUG1(0, sc, "Array started."); 1234 g_raid_md_write_nvidia(md, NULL, NULL, NULL); 1235 1236 /* Pickup any STALE/SPARE disks to refill array if needed. */ 1237 g_raid_md_nvidia_refill(sc); 1238 1239 g_raid_event_send(vol, G_RAID_VOLUME_E_START, 1240 G_RAID_EVENT_VOLUME); 1241 return (0); 1242 } 1243 if (strcmp(verb, "delete") == 0) { 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, _PATH_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