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